Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-xtensa.c
Line
Count
Source
1
/* Xtensa-specific support for 32-bit ELF.
2
   Copyright (C) 2003-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or
7
   modify it under the terms of the GNU General Public License as
8
   published by the Free Software Foundation; either version 3 of the
9
   License, or (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful, but
12
   WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
   General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19
   02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "bfd.h"
23
24
#include <stdarg.h>
25
26
#include "bfdlink.h"
27
#include "libbfd.h"
28
#include "elf-bfd.h"
29
#include "elf/xtensa.h"
30
#include "splay-tree.h"
31
#include "xtensa-isa.h"
32
#include "xtensa-dynconfig.h"
33
34
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
35
8.20k
#define OCTETS_PER_BYTE(ABFD, SEC) 1
36
37
0
#define XTENSA_NO_NOP_REMOVAL 0
38
39
#ifndef XTHAL_ABI_UNDEFINED
40
0
#define XTHAL_ABI_UNDEFINED -1
41
#endif
42
43
/* Local helper functions.  */
44
45
static bool add_extra_plt_sections (struct bfd_link_info *, int);
46
static char *vsprint_msg (const char *, const char *, int, ...) ATTRIBUTE_PRINTF(2,4);
47
static bfd_reloc_status_type bfd_elf_xtensa_reloc
48
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49
static bool do_fix_for_relocatable_link
50
  (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *);
51
static void do_fix_for_final_link
52
  (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *, bfd_vma *);
53
54
/* Local functions to handle Xtensa configurability.  */
55
56
static bool is_indirect_call_opcode (xtensa_opcode);
57
static bool is_direct_call_opcode (xtensa_opcode);
58
static bool is_windowed_call_opcode (xtensa_opcode);
59
static xtensa_opcode get_const16_opcode (void);
60
static xtensa_opcode get_l32r_opcode (void);
61
static bfd_vma l32r_offset (bfd_vma, bfd_vma);
62
static int get_relocation_opnd (xtensa_opcode, int);
63
static int get_relocation_slot (int);
64
static xtensa_opcode get_relocation_opcode
65
  (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
66
static bool is_l32r_relocation
67
  (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
68
static bool is_alt_relocation (int);
69
static bool is_operand_relocation (int);
70
static bfd_size_type insn_decode_len
71
  (bfd_byte *, bfd_size_type, bfd_size_type);
72
static int insn_num_slots
73
  (bfd_byte *, bfd_size_type, bfd_size_type);
74
static xtensa_opcode insn_decode_opcode
75
  (bfd_byte *, bfd_size_type, bfd_size_type, int);
76
static bool check_branch_target_aligned
77
  (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
78
static bool check_loop_aligned
79
  (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
80
static bool check_branch_target_aligned_address (bfd_vma, int);
81
static bfd_size_type get_asm_simplify_size
82
  (bfd_byte *, bfd_size_type, bfd_size_type);
83
84
/* Functions for link-time code simplifications.  */
85
86
static bfd_reloc_status_type elf_xtensa_do_asm_simplify
87
  (bfd_byte *, bfd_vma, bfd_vma, char **);
88
static bfd_reloc_status_type contract_asm_expansion
89
  (bfd_byte *, bfd_vma, Elf_Internal_Rela *, char **);
90
static xtensa_opcode swap_callx_for_call_opcode (xtensa_opcode);
91
static xtensa_opcode get_expanded_call_opcode (bfd_byte *, int, bool *);
92
93
/* Access to internal relocations, section contents and symbols.  */
94
95
static Elf_Internal_Rela *retrieve_internal_relocs
96
  (bfd *, asection *, bool);
97
static void pin_internal_relocs (asection *, Elf_Internal_Rela *);
98
static void release_internal_relocs (asection *, Elf_Internal_Rela *);
99
static bfd_byte *retrieve_contents (bfd *, asection *, bool);
100
static void pin_contents (asection *, bfd_byte *);
101
static void release_contents (asection *, bfd_byte *);
102
static Elf_Internal_Sym *retrieve_local_syms (bfd *);
103
104
/* Miscellaneous utility functions.  */
105
106
static asection *elf_xtensa_get_plt_section (struct bfd_link_info *, int);
107
static asection *elf_xtensa_get_gotplt_section (struct bfd_link_info *, int);
108
static asection *get_elf_r_symndx_section (bfd *, unsigned long);
109
static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
110
  (bfd *, unsigned long);
111
static bfd_vma get_elf_r_symndx_offset (bfd *, unsigned long);
112
static bool is_reloc_sym_weak (bfd *, Elf_Internal_Rela *);
113
static bool pcrel_reloc_fits (xtensa_opcode, int, bfd_vma, bfd_vma);
114
static bool xtensa_is_property_section (asection *);
115
static bool xtensa_is_insntable_section (asection *);
116
static bool xtensa_is_littable_section (asection *);
117
static bool xtensa_is_proptable_section (asection *);
118
static int internal_reloc_compare (const void *, const void *);
119
static int internal_reloc_matches (const void *, const void *);
120
static asection *xtensa_get_property_section (asection *, const char *);
121
static flagword xtensa_get_property_predef_flags (asection *);
122
123
/* Other functions called directly by the linker.  */
124
125
typedef void (*deps_callback_t)
126
  (asection *, bfd_vma, asection *, bfd_vma, void *);
127
extern bool xtensa_callback_required_dependence
128
  (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
129
130
131
/* Globally visible flag for choosing size optimization of NOP removal
132
   instead of branch-target-aware minimization for NOP removal.
133
   When nonzero, narrow all instructions and remove all NOPs possible
134
   around longcall expansions.  */
135
136
int elf32xtensa_size_opt;
137
138
139
/* The "new_section_hook" is used to set up a per-section
140
   "xtensa_relax_info" data structure with additional information used
141
   during relaxation.  */
142
143
typedef struct xtensa_relax_info_struct xtensa_relax_info;
144
145
146
/* The GNU tools do not easily allow extending interfaces to pass around
147
   the pointer to the Xtensa ISA information, so instead we add a global
148
   variable here (in BFD) that can be used by any of the tools that need
149
   this information. */
150
151
xtensa_isa xtensa_default_isa;
152
153
154
/* When this is true, relocations may have been modified to refer to
155
   symbols from other input files.  The per-section list of "fix"
156
   records needs to be checked when resolving relocations.  */
157
158
static bool relaxing_section = false;
159
160
/* When this is true, during final links, literals that cannot be
161
   coalesced and their relocations may be moved to other sections.  */
162
163
int elf32xtensa_no_literal_movement = 1;
164
165
/* Place property records for a section into individual property section
166
   with xt.prop. prefix.  */
167
168
bool elf32xtensa_separate_props = false;
169
170
/* Xtensa ABI.  It affects PLT entry code.  */
171
172
int elf32xtensa_abi = XTHAL_ABI_UNDEFINED;
173
174
/* Rename one of the generic section flags to better document how it
175
   is used here.  */
176
/* Whether relocations have been processed.  */
177
0
#define reloc_done sec_flg0
178

179
static reloc_howto_type elf_howto_table[] =
180
{
181
  HOWTO (R_XTENSA_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
182
   bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
183
   false, 0, 0, false),
184
  HOWTO (R_XTENSA_32, 0, 4, 32, false, 0, complain_overflow_bitfield,
185
   bfd_elf_xtensa_reloc, "R_XTENSA_32",
186
   true, 0xffffffff, 0xffffffff, false),
187
188
  /* Replace a 32-bit value with a value from the runtime linker (only
189
     used by linker-generated stub functions).  The r_addend value is
190
     special: 1 means to substitute a pointer to the runtime linker's
191
     dynamic resolver function; 2 means to substitute the link map for
192
     the shared object.  */
193
  HOWTO (R_XTENSA_RTLD, 0, 4, 32, false, 0, complain_overflow_dont,
194
   NULL, "R_XTENSA_RTLD", false, 0, 0, false),
195
196
  HOWTO (R_XTENSA_GLOB_DAT, 0, 4, 32, false, 0, complain_overflow_bitfield,
197
   bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
198
   false, 0, 0xffffffff, false),
199
  HOWTO (R_XTENSA_JMP_SLOT, 0, 4, 32, false, 0, complain_overflow_bitfield,
200
   bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
201
   false, 0, 0xffffffff, false),
202
  HOWTO (R_XTENSA_RELATIVE, 0, 4, 32, false, 0, complain_overflow_bitfield,
203
   bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
204
   false, 0, 0xffffffff, false),
205
  HOWTO (R_XTENSA_PLT, 0, 4, 32, false, 0, complain_overflow_bitfield,
206
   bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
207
   false, 0, 0xffffffff, false),
208
209
  EMPTY_HOWTO (7),
210
211
  /* Old relocations for backward compatibility.  */
212
  HOWTO (R_XTENSA_OP0, 0, 0, 0, true, 0, complain_overflow_dont,
213
   bfd_elf_xtensa_reloc, "R_XTENSA_OP0", false, 0, 0, true),
214
  HOWTO (R_XTENSA_OP1, 0, 0, 0, true, 0, complain_overflow_dont,
215
   bfd_elf_xtensa_reloc, "R_XTENSA_OP1", false, 0, 0, true),
216
  HOWTO (R_XTENSA_OP2, 0, 0, 0, true, 0, complain_overflow_dont,
217
   bfd_elf_xtensa_reloc, "R_XTENSA_OP2", false, 0, 0, true),
218
219
  /* Assembly auto-expansion.  */
220
  HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, true, 0, complain_overflow_dont,
221
   bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND", false, 0, 0, true),
222
  /* Relax assembly auto-expansion.  */
223
  HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, true, 0, complain_overflow_dont,
224
   bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY", false, 0, 0, true),
225
226
  EMPTY_HOWTO (13),
227
228
  HOWTO (R_XTENSA_32_PCREL, 0, 4, 32, true, 0, complain_overflow_bitfield,
229
   bfd_elf_xtensa_reloc, "R_XTENSA_32_PCREL",
230
   false, 0, 0xffffffff, true),
231
232
  /* GNU extension to record C++ vtable hierarchy.  */
233
  HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont,
234
   NULL, "R_XTENSA_GNU_VTINHERIT",
235
   false, 0, 0, false),
236
  /* GNU extension to record C++ vtable member usage.  */
237
  HOWTO (R_XTENSA_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont,
238
   _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
239
   false, 0, 0, false),
240
241
  /* Relocations for supporting difference of symbols.  */
242
  HOWTO (R_XTENSA_DIFF8, 0, 1, 8, false, 0, complain_overflow_signed,
243
   bfd_elf_xtensa_reloc, "R_XTENSA_DIFF8", false, 0, 0xff, false),
244
  HOWTO (R_XTENSA_DIFF16, 0, 2, 16, false, 0, complain_overflow_signed,
245
   bfd_elf_xtensa_reloc, "R_XTENSA_DIFF16", false, 0, 0xffff, false),
246
  HOWTO (R_XTENSA_DIFF32, 0, 4, 32, false, 0, complain_overflow_signed,
247
   bfd_elf_xtensa_reloc, "R_XTENSA_DIFF32", false, 0, 0xffffffff, false),
248
249
  /* General immediate operand relocations.  */
250
  HOWTO (R_XTENSA_SLOT0_OP, 0, 0, 0, true, 0, complain_overflow_dont,
251
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_OP", false, 0, 0, true),
252
  HOWTO (R_XTENSA_SLOT1_OP, 0, 0, 0, true, 0, complain_overflow_dont,
253
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_OP", false, 0, 0, true),
254
  HOWTO (R_XTENSA_SLOT2_OP, 0, 0, 0, true, 0, complain_overflow_dont,
255
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_OP", false, 0, 0, true),
256
  HOWTO (R_XTENSA_SLOT3_OP, 0, 0, 0, true, 0, complain_overflow_dont,
257
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_OP", false, 0, 0, true),
258
  HOWTO (R_XTENSA_SLOT4_OP, 0, 0, 0, true, 0, complain_overflow_dont,
259
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_OP", false, 0, 0, true),
260
  HOWTO (R_XTENSA_SLOT5_OP, 0, 0, 0, true, 0, complain_overflow_dont,
261
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_OP", false, 0, 0, true),
262
  HOWTO (R_XTENSA_SLOT6_OP, 0, 0, 0, true, 0, complain_overflow_dont,
263
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_OP", false, 0, 0, true),
264
  HOWTO (R_XTENSA_SLOT7_OP, 0, 0, 0, true, 0, complain_overflow_dont,
265
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_OP", false, 0, 0, true),
266
  HOWTO (R_XTENSA_SLOT8_OP, 0, 0, 0, true, 0, complain_overflow_dont,
267
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_OP", false, 0, 0, true),
268
  HOWTO (R_XTENSA_SLOT9_OP, 0, 0, 0, true, 0, complain_overflow_dont,
269
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_OP", false, 0, 0, true),
270
  HOWTO (R_XTENSA_SLOT10_OP, 0, 0, 0, true, 0, complain_overflow_dont,
271
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_OP", false, 0, 0, true),
272
  HOWTO (R_XTENSA_SLOT11_OP, 0, 0, 0, true, 0, complain_overflow_dont,
273
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_OP", false, 0, 0, true),
274
  HOWTO (R_XTENSA_SLOT12_OP, 0, 0, 0, true, 0, complain_overflow_dont,
275
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_OP", false, 0, 0, true),
276
  HOWTO (R_XTENSA_SLOT13_OP, 0, 0, 0, true, 0, complain_overflow_dont,
277
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_OP", false, 0, 0, true),
278
  HOWTO (R_XTENSA_SLOT14_OP, 0, 0, 0, true, 0, complain_overflow_dont,
279
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_OP", false, 0, 0, true),
280
281
  /* "Alternate" relocations.  The meaning of these is opcode-specific.  */
282
  HOWTO (R_XTENSA_SLOT0_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
283
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_ALT", false, 0, 0, true),
284
  HOWTO (R_XTENSA_SLOT1_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
285
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_ALT", false, 0, 0, true),
286
  HOWTO (R_XTENSA_SLOT2_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
287
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_ALT", false, 0, 0, true),
288
  HOWTO (R_XTENSA_SLOT3_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
289
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_ALT", false, 0, 0, true),
290
  HOWTO (R_XTENSA_SLOT4_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
291
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_ALT", false, 0, 0, true),
292
  HOWTO (R_XTENSA_SLOT5_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
293
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_ALT", false, 0, 0, true),
294
  HOWTO (R_XTENSA_SLOT6_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
295
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_ALT", false, 0, 0, true),
296
  HOWTO (R_XTENSA_SLOT7_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
297
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_ALT", false, 0, 0, true),
298
  HOWTO (R_XTENSA_SLOT8_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
299
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_ALT", false, 0, 0, true),
300
  HOWTO (R_XTENSA_SLOT9_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
301
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_ALT", false, 0, 0, true),
302
  HOWTO (R_XTENSA_SLOT10_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
303
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_ALT", false, 0, 0, true),
304
  HOWTO (R_XTENSA_SLOT11_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
305
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_ALT", false, 0, 0, true),
306
  HOWTO (R_XTENSA_SLOT12_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
307
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_ALT", false, 0, 0, true),
308
  HOWTO (R_XTENSA_SLOT13_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
309
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_ALT", false, 0, 0, true),
310
  HOWTO (R_XTENSA_SLOT14_ALT, 0, 0, 0, true, 0, complain_overflow_dont,
311
   bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_ALT", false, 0, 0, true),
312
313
  /* TLS relocations.  */
314
  HOWTO (R_XTENSA_TLSDESC_FN, 0, 4, 32, false, 0, complain_overflow_dont,
315
   bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_FN",
316
   false, 0, 0xffffffff, false),
317
  HOWTO (R_XTENSA_TLSDESC_ARG, 0, 4, 32, false, 0, complain_overflow_dont,
318
   bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_ARG",
319
   false, 0, 0xffffffff, false),
320
  HOWTO (R_XTENSA_TLS_DTPOFF, 0, 4, 32, false, 0, complain_overflow_dont,
321
   bfd_elf_xtensa_reloc, "R_XTENSA_TLS_DTPOFF",
322
   false, 0, 0xffffffff, false),
323
  HOWTO (R_XTENSA_TLS_TPOFF, 0, 4, 32, false, 0, complain_overflow_dont,
324
   bfd_elf_xtensa_reloc, "R_XTENSA_TLS_TPOFF",
325
   false, 0, 0xffffffff, false),
326
  HOWTO (R_XTENSA_TLS_FUNC, 0, 0, 0, false, 0, complain_overflow_dont,
327
   bfd_elf_xtensa_reloc, "R_XTENSA_TLS_FUNC",
328
   false, 0, 0, false),
329
  HOWTO (R_XTENSA_TLS_ARG, 0, 0, 0, false, 0, complain_overflow_dont,
330
   bfd_elf_xtensa_reloc, "R_XTENSA_TLS_ARG",
331
   false, 0, 0, false),
332
  HOWTO (R_XTENSA_TLS_CALL, 0, 0, 0, false, 0, complain_overflow_dont,
333
   bfd_elf_xtensa_reloc, "R_XTENSA_TLS_CALL",
334
   false, 0, 0, false),
335
336
  HOWTO (R_XTENSA_PDIFF8, 0, 1, 8, false, 0, complain_overflow_bitfield,
337
   bfd_elf_xtensa_reloc, "R_XTENSA_PDIFF8", false, 0, 0xff, false),
338
  HOWTO (R_XTENSA_PDIFF16, 0, 2, 16, false, 0, complain_overflow_bitfield,
339
   bfd_elf_xtensa_reloc, "R_XTENSA_PDIFF16", false, 0, 0xffff, false),
340
  HOWTO (R_XTENSA_PDIFF32, 0, 4, 32, false, 0, complain_overflow_bitfield,
341
   bfd_elf_xtensa_reloc, "R_XTENSA_PDIFF32", false, 0, 0xffffffff, false),
342
343
  HOWTO (R_XTENSA_NDIFF8, 0, 1, 8, false, 0, complain_overflow_bitfield,
344
   bfd_elf_xtensa_reloc, "R_XTENSA_NDIFF8", false, 0, 0xff, false),
345
  HOWTO (R_XTENSA_NDIFF16, 0, 2, 16, false, 0, complain_overflow_bitfield,
346
   bfd_elf_xtensa_reloc, "R_XTENSA_NDIFF16", false, 0, 0xffff, false),
347
  HOWTO (R_XTENSA_NDIFF32, 0, 4, 32, false, 0, complain_overflow_bitfield,
348
   bfd_elf_xtensa_reloc, "R_XTENSA_NDIFF32", false, 0, 0xffffffff, false),
349
  HOWTO (R_XTENSA_PDIFF_ULEB128, 0, 0, 0, false, 0, complain_overflow_dont,
350
   bfd_elf_xtensa_reloc, "R_XTENSA_PDIFF_ULEB128", false, 0, 0, false),
351
352
  /* Absolute 32-bit address with the addend taken only from r_addend.
353
     Unlike R_XTENSA_32 this is not partial_inplace, so the contents of
354
     the relocated word do not contribute to the result.  */
355
  HOWTO (R_XTENSA_32_ABS, 0, 4, 32, false, 0, complain_overflow_bitfield,
356
   bfd_elf_xtensa_reloc, "R_XTENSA_32_ABS",
357
   false, 0, 0xffffffff, false),
358
};
359
360
#if DEBUG_GEN_RELOC
361
#define TRACE(str) \
362
  fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
363
#else
364
#define TRACE(str)
365
#endif
366
367
static reloc_howto_type *
368
elf_xtensa_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
369
            bfd_reloc_code_real_type code)
370
0
{
371
0
  switch (code)
372
0
    {
373
0
    case BFD_RELOC_NONE:
374
0
      TRACE ("BFD_RELOC_NONE");
375
0
      return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
376
377
0
    case BFD_RELOC_32:
378
0
      TRACE ("BFD_RELOC_32");
379
0
      return &elf_howto_table[(unsigned) R_XTENSA_32 ];
380
381
0
    case BFD_RELOC_32_PCREL:
382
0
      TRACE ("BFD_RELOC_32_PCREL");
383
0
      return &elf_howto_table[(unsigned) R_XTENSA_32_PCREL ];
384
385
0
    case BFD_RELOC_XTENSA_DIFF8:
386
0
      TRACE ("BFD_RELOC_XTENSA_DIFF8");
387
0
      return &elf_howto_table[(unsigned) R_XTENSA_DIFF8 ];
388
389
0
    case BFD_RELOC_XTENSA_DIFF16:
390
0
      TRACE ("BFD_RELOC_XTENSA_DIFF16");
391
0
      return &elf_howto_table[(unsigned) R_XTENSA_DIFF16 ];
392
393
0
    case BFD_RELOC_XTENSA_DIFF32:
394
0
      TRACE ("BFD_RELOC_XTENSA_DIFF32");
395
0
      return &elf_howto_table[(unsigned) R_XTENSA_DIFF32 ];
396
397
0
    case BFD_RELOC_XTENSA_PDIFF8:
398
0
      TRACE ("BFD_RELOC_XTENSA_PDIFF8");
399
0
      return &elf_howto_table[(unsigned) R_XTENSA_PDIFF8 ];
400
401
0
    case BFD_RELOC_XTENSA_PDIFF16:
402
0
      TRACE ("BFD_RELOC_XTENSA_PDIFF16");
403
0
      return &elf_howto_table[(unsigned) R_XTENSA_PDIFF16 ];
404
405
0
    case BFD_RELOC_XTENSA_PDIFF32:
406
0
      TRACE ("BFD_RELOC_XTENSA_PDIFF32");
407
0
      return &elf_howto_table[(unsigned) R_XTENSA_PDIFF32 ];
408
409
0
    case BFD_RELOC_XTENSA_NDIFF8:
410
0
      TRACE ("BFD_RELOC_XTENSA_NDIFF8");
411
0
      return &elf_howto_table[(unsigned) R_XTENSA_NDIFF8 ];
412
413
0
    case BFD_RELOC_XTENSA_NDIFF16:
414
0
      TRACE ("BFD_RELOC_XTENSA_NDIFF16");
415
0
      return &elf_howto_table[(unsigned) R_XTENSA_NDIFF16 ];
416
417
0
    case BFD_RELOC_XTENSA_NDIFF32:
418
0
      TRACE ("BFD_RELOC_XTENSA_NDIFF32");
419
0
      return &elf_howto_table[(unsigned) R_XTENSA_NDIFF32 ];
420
421
0
    case BFD_RELOC_XTENSA_PDIFF_ULEB128:
422
0
      TRACE ("BFD_RELOC_XTENSA_PDIFF_ULEB128");
423
0
      return &elf_howto_table[(unsigned) R_XTENSA_PDIFF_ULEB128 ];
424
425
0
    case BFD_RELOC_XTENSA_32_ABS:
426
0
      TRACE ("BFD_RELOC_XTENSA_32_ABS");
427
0
      return &elf_howto_table[(unsigned) R_XTENSA_32_ABS ];
428
429
0
    case BFD_RELOC_XTENSA_RTLD:
430
0
      TRACE ("BFD_RELOC_XTENSA_RTLD");
431
0
      return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
432
433
0
    case BFD_RELOC_GLOB_DAT:
434
0
      TRACE ("BFD_RELOC_GLOB_DAT");
435
0
      return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
436
437
0
    case BFD_RELOC_JMP_SLOT:
438
0
      TRACE ("BFD_RELOC_JMP_SLOT");
439
0
      return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
440
441
0
    case BFD_RELOC_RELATIVE:
442
0
      TRACE ("BFD_RELOC_RELATIVE");
443
0
      return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
444
445
0
    case BFD_RELOC_XTENSA_PLT:
446
0
      TRACE ("BFD_RELOC_XTENSA_PLT");
447
0
      return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
448
449
0
    case BFD_RELOC_XTENSA_OP0:
450
0
      TRACE ("BFD_RELOC_XTENSA_OP0");
451
0
      return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
452
453
0
    case BFD_RELOC_XTENSA_OP1:
454
0
      TRACE ("BFD_RELOC_XTENSA_OP1");
455
0
      return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
456
457
0
    case BFD_RELOC_XTENSA_OP2:
458
0
      TRACE ("BFD_RELOC_XTENSA_OP2");
459
0
      return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
460
461
0
    case BFD_RELOC_XTENSA_ASM_EXPAND:
462
0
      TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
463
0
      return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
464
465
0
    case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
466
0
      TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
467
0
      return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
468
469
0
    case BFD_RELOC_VTABLE_INHERIT:
470
0
      TRACE ("BFD_RELOC_VTABLE_INHERIT");
471
0
      return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
472
473
0
    case BFD_RELOC_VTABLE_ENTRY:
474
0
      TRACE ("BFD_RELOC_VTABLE_ENTRY");
475
0
      return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
476
477
0
    case BFD_RELOC_XTENSA_TLSDESC_FN:
478
0
      TRACE ("BFD_RELOC_XTENSA_TLSDESC_FN");
479
0
      return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_FN ];
480
481
0
    case BFD_RELOC_XTENSA_TLSDESC_ARG:
482
0
      TRACE ("BFD_RELOC_XTENSA_TLSDESC_ARG");
483
0
      return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_ARG ];
484
485
0
    case BFD_RELOC_XTENSA_TLS_DTPOFF:
486
0
      TRACE ("BFD_RELOC_XTENSA_TLS_DTPOFF");
487
0
      return &elf_howto_table[(unsigned) R_XTENSA_TLS_DTPOFF ];
488
489
0
    case BFD_RELOC_XTENSA_TLS_TPOFF:
490
0
      TRACE ("BFD_RELOC_XTENSA_TLS_TPOFF");
491
0
      return &elf_howto_table[(unsigned) R_XTENSA_TLS_TPOFF ];
492
493
0
    case BFD_RELOC_XTENSA_TLS_FUNC:
494
0
      TRACE ("BFD_RELOC_XTENSA_TLS_FUNC");
495
0
      return &elf_howto_table[(unsigned) R_XTENSA_TLS_FUNC ];
496
497
0
    case BFD_RELOC_XTENSA_TLS_ARG:
498
0
      TRACE ("BFD_RELOC_XTENSA_TLS_ARG");
499
0
      return &elf_howto_table[(unsigned) R_XTENSA_TLS_ARG ];
500
501
0
    case BFD_RELOC_XTENSA_TLS_CALL:
502
0
      TRACE ("BFD_RELOC_XTENSA_TLS_CALL");
503
0
      return &elf_howto_table[(unsigned) R_XTENSA_TLS_CALL ];
504
505
0
    default:
506
0
      if (code >= BFD_RELOC_XTENSA_SLOT0_OP
507
0
    && code <= BFD_RELOC_XTENSA_SLOT14_OP)
508
0
  {
509
0
    unsigned n = (R_XTENSA_SLOT0_OP +
510
0
      (code - BFD_RELOC_XTENSA_SLOT0_OP));
511
0
    return &elf_howto_table[n];
512
0
  }
513
514
0
      if (code >= BFD_RELOC_XTENSA_SLOT0_ALT
515
0
    && code <= BFD_RELOC_XTENSA_SLOT14_ALT)
516
0
  {
517
0
    unsigned n = (R_XTENSA_SLOT0_ALT +
518
0
      (code - BFD_RELOC_XTENSA_SLOT0_ALT));
519
0
    return &elf_howto_table[n];
520
0
  }
521
522
0
      break;
523
0
    }
524
525
  /* xgettext:c-format */
526
0
  _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
527
0
  bfd_set_error (bfd_error_bad_value);
528
0
  TRACE ("Unknown");
529
0
  return NULL;
530
0
}
531
532
static reloc_howto_type *
533
elf_xtensa_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
534
            const char *r_name)
535
0
{
536
0
  unsigned int i;
537
538
0
  for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
539
0
    if (elf_howto_table[i].name != NULL
540
0
  && strcasecmp (elf_howto_table[i].name, r_name) == 0)
541
0
      return &elf_howto_table[i];
542
543
0
  return NULL;
544
0
}
545
546
547
/* Given an ELF "rela" relocation, find the corresponding howto and record
548
   it in the BFD internal arelent representation of the relocation.  */
549
550
static bool
551
elf_xtensa_info_to_howto_rela (bfd *abfd,
552
             arelent *cache_ptr,
553
             Elf_Internal_Rela *dst)
554
23.0k
{
555
23.0k
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
556
557
23.0k
  if (r_type >= (unsigned int) R_XTENSA_max)
558
21
    {
559
      /* xgettext:c-format */
560
21
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
561
21
        abfd, r_type);
562
21
      bfd_set_error (bfd_error_bad_value);
563
21
      return false;
564
21
    }
565
23.0k
  cache_ptr->howto = &elf_howto_table[r_type];
566
23.0k
  return true;
567
23.0k
}
568
569

570
/* Functions for the Xtensa ELF linker.  */
571
572
/* The name of the dynamic interpreter.  This is put in the .interp
573
   section.  */
574
575
0
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
576
577
/* The size in bytes of an entry in the procedure linkage table.
578
   (This does _not_ include the space for the literals associated with
579
   the PLT entry.) */
580
581
0
#define PLT_ENTRY_SIZE 16
582
583
/* For _really_ large PLTs, we may need to alternate between literals
584
   and code to keep the literals within the 256K range of the L32R
585
   instructions in the code.  It's unlikely that anyone would ever need
586
   such a big PLT, but an arbitrary limit on the PLT size would be bad.
587
   Thus, we split the PLT into chunks.  Since there's very little
588
   overhead (2 extra literals) for each chunk, the chunk size is kept
589
   small so that the code for handling multiple chunks get used and
590
   tested regularly.  With 254 entries, there are 1K of literals for
591
   each chunk, and that seems like a nice round number.  */
592
593
0
#define PLT_ENTRIES_PER_CHUNK 254
594
595
/* PLT entries are actually used as stub functions for lazy symbol
596
   resolution.  Once the symbol is resolved, the stub function is never
597
   invoked.  Note: the 32-byte frame size used here cannot be changed
598
   without a corresponding change in the runtime linker.  */
599
600
static const bfd_byte elf_xtensa_be_plt_entry[][PLT_ENTRY_SIZE] =
601
{
602
    {
603
      0x6c, 0x10, 0x04, /* entry sp, 32 */
604
      0x18, 0x00, 0x00, /* l32r  a8, [got entry for rtld's resolver] */
605
      0x1a, 0x00, 0x00, /* l32r  a10, [got entry for rtld's link map] */
606
      0x1b, 0x00, 0x00, /* l32r  a11, [literal for reloc index] */
607
      0x0a, 0x80, 0x00, /* jx    a8 */
608
      0     /* unused */
609
    },
610
    {
611
      0x18, 0x00, 0x00, /* l32r  a8, [got entry for rtld's resolver] */
612
      0x1a, 0x00, 0x00, /* l32r  a10, [got entry for rtld's link map] */
613
      0x1b, 0x00, 0x00, /* l32r  a11, [literal for reloc index] */
614
      0x0a, 0x80, 0x00, /* jx    a8 */
615
      0     /* unused */
616
    }
617
};
618
619
static const bfd_byte elf_xtensa_le_plt_entry[][PLT_ENTRY_SIZE] =
620
{
621
    {
622
      0x36, 0x41, 0x00, /* entry sp, 32 */
623
      0x81, 0x00, 0x00, /* l32r  a8, [got entry for rtld's resolver] */
624
      0xa1, 0x00, 0x00, /* l32r  a10, [got entry for rtld's link map] */
625
      0xb1, 0x00, 0x00, /* l32r  a11, [literal for reloc index] */
626
      0xa0, 0x08, 0x00, /* jx    a8 */
627
      0     /* unused */
628
    },
629
    {
630
      0x81, 0x00, 0x00, /* l32r  a8, [got entry for rtld's resolver] */
631
      0xa1, 0x00, 0x00, /* l32r  a10, [got entry for rtld's link map] */
632
      0xb1, 0x00, 0x00, /* l32r  a11, [literal for reloc index] */
633
      0xa0, 0x08, 0x00, /* jx    a8 */
634
      0     /* unused */
635
    }
636
};
637
638
/* The size of the thread control block.  */
639
#define TCB_SIZE  8
640
641
struct elf_xtensa_link_hash_entry
642
{
643
  struct elf_link_hash_entry elf;
644
645
  bfd_signed_vma tlsfunc_refcount;
646
647
0
#define GOT_UNKNOWN 0
648
0
#define GOT_NORMAL  1
649
0
#define GOT_TLS_GD  2  /* global or local dynamic */
650
0
#define GOT_TLS_IE  4  /* initial or local exec */
651
0
#define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
652
  unsigned char tls_type;
653
};
654
655
0
#define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
656
657
struct elf_xtensa_obj_tdata
658
{
659
  struct elf_obj_tdata root;
660
661
  /* tls_type for each local got entry.  */
662
  char *local_got_tls_type;
663
664
  bfd_signed_vma *local_tlsfunc_refcounts;
665
};
666
667
#define elf_xtensa_tdata(abfd) \
668
0
  ((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
669
670
#define elf_xtensa_local_got_tls_type(abfd) \
671
0
  (elf_xtensa_tdata (abfd)->local_got_tls_type)
672
673
#define elf_xtensa_local_tlsfunc_refcounts(abfd) \
674
0
  (elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
675
676
#define is_xtensa_elf(bfd) \
677
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
678
0
   && elf_tdata (bfd) != NULL \
679
0
   && elf_object_id (bfd) == XTENSA_ELF_DATA)
680
681
static bool
682
elf_xtensa_mkobject (bfd *abfd)
683
49.9k
{
684
49.9k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_xtensa_obj_tdata));
685
49.9k
}
686
687
/* Xtensa ELF linker hash table.  */
688
689
struct elf_xtensa_link_hash_table
690
{
691
  struct elf_link_hash_table elf;
692
693
  /* Short-cuts to get to dynamic linker sections.  */
694
  asection *sgotloc;
695
  asection *spltlittbl;
696
697
  /* Total count of PLT relocations seen during check_relocs.
698
     The actual PLT code must be split into multiple sections and all
699
     the sections have to be created before size_dynamic_sections,
700
     where we figure out the exact number of PLT entries that will be
701
     needed.  It is OK if this count is an overestimate, e.g., some
702
     relocations may be removed by GC.  */
703
  int plt_reloc_count;
704
705
  struct elf_xtensa_link_hash_entry *tlsbase;
706
};
707
708
/* Get the Xtensa ELF linker hash table from a link_info structure.  */
709
710
#define elf_xtensa_hash_table(p) \
711
0
  ((is_elf_hash_table ((p)->hash)          \
712
0
    && elf_hash_table_id (elf_hash_table (p)) == XTENSA_ELF_DATA) \
713
0
   ? (struct elf_xtensa_link_hash_table *) (p)->hash : NULL)
714
715
/* Create an entry in an Xtensa ELF linker hash table.  */
716
717
static struct bfd_hash_entry *
718
elf_xtensa_link_hash_newfunc (struct bfd_hash_entry *entry,
719
            struct bfd_hash_table *table,
720
            const char *string)
721
0
{
722
  /* Allocate the structure if it has not already been allocated by a
723
     subclass.  */
724
0
  if (entry == NULL)
725
0
    {
726
0
      entry = bfd_hash_allocate (table,
727
0
         sizeof (struct elf_xtensa_link_hash_entry));
728
0
      if (entry == NULL)
729
0
  return entry;
730
0
    }
731
732
  /* Call the allocation method of the superclass.  */
733
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
734
0
  if (entry != NULL)
735
0
    {
736
0
      struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (entry);
737
0
      eh->tlsfunc_refcount = 0;
738
0
      eh->tls_type = GOT_UNKNOWN;
739
0
    }
740
741
0
  return entry;
742
0
}
743
744
/* Create an Xtensa ELF linker hash table.  */
745
746
static struct bfd_link_hash_table *
747
elf_xtensa_link_hash_table_create (bfd *abfd)
748
0
{
749
0
  struct elf_link_hash_entry *tlsbase;
750
0
  struct elf_xtensa_link_hash_table *ret;
751
0
  size_t amt = sizeof (struct elf_xtensa_link_hash_table);
752
753
0
  ret = bfd_zmalloc (amt);
754
0
  if (ret == NULL)
755
0
    return NULL;
756
757
0
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
758
0
              elf_xtensa_link_hash_newfunc,
759
0
              sizeof (struct elf_xtensa_link_hash_entry)))
760
0
    {
761
0
      free (ret);
762
0
      return NULL;
763
0
    }
764
765
  /* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
766
     for it later.  */
767
0
  tlsbase = elf_link_hash_lookup (&ret->elf, "_TLS_MODULE_BASE_",
768
0
          true, false, false);
769
0
  tlsbase->root.type = bfd_link_hash_new;
770
0
  tlsbase->root.u.undef.abfd = NULL;
771
0
  tlsbase->non_elf = 0;
772
0
  ret->elf.dt_pltgot_required = true;
773
0
  ret->tlsbase = elf_xtensa_hash_entry (tlsbase);
774
0
  ret->tlsbase->tls_type = GOT_UNKNOWN;
775
776
0
  return &ret->elf.root;
777
0
}
778
779
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
780
781
static void
782
elf_xtensa_copy_indirect_symbol (struct bfd_link_info *info,
783
         struct elf_link_hash_entry *dir,
784
         struct elf_link_hash_entry *ind)
785
0
{
786
0
  struct elf_xtensa_link_hash_entry *edir, *eind;
787
788
0
  edir = elf_xtensa_hash_entry (dir);
789
0
  eind = elf_xtensa_hash_entry (ind);
790
791
0
  if (ind->root.type == bfd_link_hash_indirect)
792
0
    {
793
0
      edir->tlsfunc_refcount += eind->tlsfunc_refcount;
794
0
      eind->tlsfunc_refcount = 0;
795
796
0
      if (dir->got.refcount <= 0)
797
0
  {
798
0
    edir->tls_type = eind->tls_type;
799
0
    eind->tls_type = GOT_UNKNOWN;
800
0
  }
801
0
    }
802
803
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
804
0
}
805
806
static inline bool
807
elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry *h,
808
           struct bfd_link_info *info)
809
0
{
810
  /* Check if we should do dynamic things to this symbol.  The
811
     "ignore_protected" argument need not be set, because Xtensa code
812
     does not require special handling of STV_PROTECTED to make function
813
     pointer comparisons work properly.  The PLT addresses are never
814
     used for function pointers.  */
815
816
0
  return _bfd_elf_dynamic_symbol_p (h, info, 0);
817
0
}
818
819

820
static int
821
property_table_compare (const void *ap, const void *bp)
822
0
{
823
0
  const property_table_entry *a = (const property_table_entry *) ap;
824
0
  const property_table_entry *b = (const property_table_entry *) bp;
825
826
0
  if (a->address == b->address)
827
0
    {
828
0
      if (a->size != b->size)
829
0
  return (a->size - b->size);
830
831
0
      if ((a->flags & XTENSA_PROP_ALIGN) != (b->flags & XTENSA_PROP_ALIGN))
832
0
  return ((b->flags & XTENSA_PROP_ALIGN)
833
0
    - (a->flags & XTENSA_PROP_ALIGN));
834
835
0
      if ((a->flags & XTENSA_PROP_ALIGN)
836
0
    && (GET_XTENSA_PROP_ALIGNMENT (a->flags)
837
0
        != GET_XTENSA_PROP_ALIGNMENT (b->flags)))
838
0
  return (GET_XTENSA_PROP_ALIGNMENT (a->flags)
839
0
    - GET_XTENSA_PROP_ALIGNMENT (b->flags));
840
841
0
      if ((a->flags & XTENSA_PROP_UNREACHABLE)
842
0
    != (b->flags & XTENSA_PROP_UNREACHABLE))
843
0
  return ((b->flags & XTENSA_PROP_UNREACHABLE)
844
0
    - (a->flags & XTENSA_PROP_UNREACHABLE));
845
846
0
      return (a->flags - b->flags);
847
0
    }
848
849
0
  return (a->address - b->address);
850
0
}
851
852
853
static int
854
property_table_matches (const void *ap, const void *bp)
855
0
{
856
0
  const property_table_entry *a = (const property_table_entry *) ap;
857
0
  const property_table_entry *b = (const property_table_entry *) bp;
858
859
  /* Check if one entry overlaps with the other.  */
860
0
  if ((b->address >= a->address && b->address < (a->address + a->size))
861
0
      || (a->address >= b->address && a->address < (b->address + b->size)))
862
0
    return 0;
863
864
0
  return (a->address - b->address);
865
0
}
866
867
868
/* Addend of relocation IREL.  R_XTENSA_32 is the only partial_inplace
869
   relocation, so only for it does the relocated word in CONTENTS
870
   contribute.  Other relocations, such as R_XTENSA_32_ABS, take the
871
   addend from r_addend alone.  */
872
873
static bfd_vma
874
xtensa_reloc_addend (bfd *abfd, bfd_byte *contents,
875
         const Elf_Internal_Rela *irel)
876
0
{
877
0
  bfd_vma addend = irel->r_addend;
878
879
0
  if (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32)
880
0
    addend += bfd_get_32 (abfd, contents + irel->r_offset);
881
0
  return addend;
882
0
}
883
884
885
/* Get the literal table or property table entries for the given
886
   section.  Sets TABLE_P and returns the number of entries.  On
887
   error, returns a negative value.  */
888
889
int
890
xtensa_read_table_entries (bfd *abfd,
891
         asection *section,
892
         property_table_entry **table_p,
893
         const char *sec_name,
894
         bool output_addr)
895
11.9k
{
896
11.9k
  asection *table_section;
897
11.9k
  bfd_size_type table_size = 0;
898
11.9k
  bfd_byte *table_data;
899
11.9k
  property_table_entry *blocks;
900
11.9k
  int blk, block_count;
901
11.9k
  bfd_size_type num_records;
902
11.9k
  Elf_Internal_Rela *internal_relocs, *irel, *rel_end;
903
11.9k
  bfd_vma section_addr, off;
904
11.9k
  flagword predef_flags;
905
11.9k
  bfd_size_type table_entry_size, section_limit;
906
907
11.9k
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
908
11.9k
      || !section
909
11.9k
      || !(section->flags & SEC_ALLOC)
910
48
      || (section->flags & SEC_DEBUGGING))
911
11.8k
    {
912
11.8k
      *table_p = NULL;
913
11.8k
      return 0;
914
11.8k
    }
915
916
48
  table_section = xtensa_get_property_section (section, sec_name);
917
48
  if (table_section)
918
0
    table_size = table_section->size;
919
920
48
  if (table_size == 0)
921
48
    {
922
48
      *table_p = NULL;
923
48
      return 0;
924
48
    }
925
926
0
  predef_flags = xtensa_get_property_predef_flags (table_section);
927
0
  table_entry_size = 12;
928
0
  if (predef_flags)
929
0
    table_entry_size -= 4;
930
931
0
  num_records = table_size / table_entry_size;
932
933
0
  table_data = retrieve_contents (abfd, table_section, true);
934
0
  if (table_data == NULL)
935
0
    {
936
0
      *table_p = NULL;
937
0
      return 0;
938
0
    }
939
940
0
  blocks = (property_table_entry *)
941
0
    bfd_malloc (num_records * sizeof (property_table_entry));
942
0
  block_count = 0;
943
944
0
  if (output_addr)
945
0
    section_addr = section->output_section->vma + section->output_offset;
946
0
  else
947
0
    section_addr = section->vma;
948
949
0
  internal_relocs = retrieve_internal_relocs (abfd, table_section, true);
950
0
  if (internal_relocs && !table_section->reloc_done)
951
0
    {
952
0
      qsort (internal_relocs, table_section->reloc_count,
953
0
       sizeof (Elf_Internal_Rela), internal_reloc_compare);
954
0
      irel = internal_relocs;
955
0
    }
956
0
  else
957
0
    irel = NULL;
958
959
0
  section_limit = bfd_get_section_limit (abfd, section);
960
0
  rel_end = internal_relocs + table_section->reloc_count;
961
962
0
  for (off = 0; off < table_size; off += table_entry_size)
963
0
    {
964
0
      bfd_vma address = bfd_get_32 (abfd, table_data + off);
965
966
      /* Skip any relocations before the current offset.  This should help
967
   avoid confusion caused by unexpected relocations for the preceding
968
   table entry.  */
969
0
      while (irel &&
970
0
       (irel->r_offset < off
971
0
        || (irel->r_offset == off
972
0
      && ELF32_R_TYPE (irel->r_info) == R_XTENSA_NONE)))
973
0
  {
974
0
    irel += 1;
975
0
    if (irel >= rel_end)
976
0
      irel = 0;
977
0
  }
978
979
0
      if (irel && irel->r_offset == off)
980
0
  {
981
0
    bfd_vma sym_off;
982
0
    unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
983
0
    BFD_ASSERT (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32
984
0
          || ELF32_R_TYPE (irel->r_info) == R_XTENSA_32_ABS);
985
986
0
    if (get_elf_r_symndx_section (abfd, r_symndx) != section)
987
0
      continue;
988
989
0
    sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
990
0
    BFD_ASSERT (sym_off == 0);
991
0
    address = (section_addr + sym_off
992
0
         + xtensa_reloc_addend (abfd, table_data, irel));
993
0
  }
994
0
      else
995
0
  {
996
0
    if (address < section_addr
997
0
        || address >= section_addr + section_limit)
998
0
      continue;
999
0
  }
1000
1001
0
      blocks[block_count].address = address;
1002
0
      blocks[block_count].size = bfd_get_32 (abfd, table_data + off + 4);
1003
0
      if (predef_flags)
1004
0
  blocks[block_count].flags = predef_flags;
1005
0
      else
1006
0
  blocks[block_count].flags = bfd_get_32 (abfd, table_data + off + 8);
1007
0
      block_count++;
1008
0
    }
1009
1010
0
  release_contents (table_section, table_data);
1011
0
  release_internal_relocs (table_section, internal_relocs);
1012
1013
0
  if (block_count > 0)
1014
0
    {
1015
      /* Now sort them into address order for easy reference.  */
1016
0
      qsort (blocks, block_count, sizeof (property_table_entry),
1017
0
       property_table_compare);
1018
1019
      /* Check that the table contents are valid.  Problems may occur,
1020
   for example, if an unrelocated object file is stripped.  */
1021
0
      for (blk = 1; blk < block_count; blk++)
1022
0
  {
1023
    /* The only circumstance where two entries may legitimately
1024
       have the same address is when one of them is a zero-size
1025
       placeholder to mark a place where fill can be inserted.
1026
       The zero-size entry should come first.  */
1027
0
    if (blocks[blk - 1].address == blocks[blk].address &&
1028
0
        blocks[blk - 1].size != 0)
1029
0
      {
1030
        /* xgettext:c-format */
1031
0
        _bfd_error_handler (_("%pB(%pA): invalid property table"),
1032
0
          abfd, section);
1033
0
        bfd_set_error (bfd_error_bad_value);
1034
0
        free (blocks);
1035
0
        return -1;
1036
0
      }
1037
0
  }
1038
0
    }
1039
1040
0
  *table_p = blocks;
1041
0
  return block_count;
1042
0
}
1043
1044
1045
static property_table_entry *
1046
elf_xtensa_find_property_entry (property_table_entry *property_table,
1047
        int property_table_size,
1048
        bfd_vma addr)
1049
0
{
1050
0
  property_table_entry entry;
1051
0
  property_table_entry *rv;
1052
1053
0
  if (property_table_size == 0)
1054
0
    return NULL;
1055
1056
0
  entry.address = addr;
1057
0
  entry.size = 1;
1058
0
  entry.flags = 0;
1059
1060
0
  rv = bsearch (&entry, property_table, property_table_size,
1061
0
    sizeof (property_table_entry), property_table_matches);
1062
0
  return rv;
1063
0
}
1064
1065
1066
static bool
1067
elf_xtensa_in_literal_pool (property_table_entry *lit_table,
1068
          int lit_table_size,
1069
          bfd_vma addr)
1070
0
{
1071
0
  if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
1072
0
    return true;
1073
1074
0
  return false;
1075
0
}
1076
1077

1078
/* Look through the relocs for a section during the first phase, and
1079
   calculate needed space in the dynamic reloc sections.  */
1080
1081
static bool
1082
elf_xtensa_check_relocs (bfd *abfd,
1083
       struct bfd_link_info *info,
1084
       asection *sec,
1085
       const Elf_Internal_Rela *relocs)
1086
0
{
1087
0
  struct elf_xtensa_link_hash_table *htab;
1088
0
  Elf_Internal_Shdr *symtab_hdr;
1089
0
  struct elf_link_hash_entry **sym_hashes;
1090
0
  const Elf_Internal_Rela *rel;
1091
0
  const Elf_Internal_Rela *rel_end;
1092
1093
0
  if (bfd_link_relocatable (info))
1094
0
    return true;
1095
1096
0
  BFD_ASSERT (is_xtensa_elf (abfd));
1097
1098
0
  htab = elf_xtensa_hash_table (info);
1099
0
  if (htab == NULL)
1100
0
    return false;
1101
1102
0
  symtab_hdr = &elf_symtab_hdr (abfd);
1103
0
  sym_hashes = elf_sym_hashes (abfd);
1104
1105
0
  rel_end = relocs + sec->reloc_count;
1106
0
  for (rel = relocs; rel < rel_end; rel++)
1107
0
    {
1108
0
      unsigned int r_type;
1109
0
      unsigned r_symndx;
1110
0
      struct elf_link_hash_entry *h = NULL;
1111
0
      struct elf_xtensa_link_hash_entry *eh;
1112
0
      int tls_type, old_tls_type;
1113
0
      bool is_got = false;
1114
0
      bool is_plt = false;
1115
0
      bool is_tlsfunc = false;
1116
1117
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1118
0
      r_type = ELF32_R_TYPE (rel->r_info);
1119
1120
0
      if (r_symndx >= symtab_hdr->sh_info)
1121
0
  {
1122
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1123
0
    while (h->root.type == bfd_link_hash_indirect
1124
0
     || h->root.type == bfd_link_hash_warning)
1125
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
1126
0
  }
1127
0
      eh = elf_xtensa_hash_entry (h);
1128
1129
0
      switch (r_type)
1130
0
  {
1131
0
  case R_XTENSA_TLSDESC_FN:
1132
0
    if (bfd_link_dll (info))
1133
0
      {
1134
0
        tls_type = GOT_TLS_GD;
1135
0
        is_got = true;
1136
0
        is_tlsfunc = true;
1137
0
      }
1138
0
    else
1139
0
      tls_type = GOT_TLS_IE;
1140
0
    break;
1141
1142
0
  case R_XTENSA_TLSDESC_ARG:
1143
0
    if (bfd_link_dll (info))
1144
0
      {
1145
0
        tls_type = GOT_TLS_GD;
1146
0
        is_got = true;
1147
0
      }
1148
0
    else
1149
0
      {
1150
0
        tls_type = GOT_TLS_IE;
1151
0
        if (h && elf_xtensa_hash_entry (h) != htab->tlsbase
1152
0
      && elf_xtensa_dynamic_symbol_p (h, info))
1153
0
    is_got = true;
1154
0
      }
1155
0
    break;
1156
1157
0
  case R_XTENSA_TLS_DTPOFF:
1158
0
    if (bfd_link_dll (info))
1159
0
      tls_type = GOT_TLS_GD;
1160
0
    else
1161
0
      tls_type = GOT_TLS_IE;
1162
0
    break;
1163
1164
0
  case R_XTENSA_TLS_TPOFF:
1165
0
    tls_type = GOT_TLS_IE;
1166
0
    if (bfd_link_pic (info))
1167
0
      info->flags |= DF_STATIC_TLS;
1168
0
    if (bfd_link_dll (info) || elf_xtensa_dynamic_symbol_p (h, info))
1169
0
      is_got = true;
1170
0
    break;
1171
1172
0
  case R_XTENSA_32:
1173
0
  case R_XTENSA_32_ABS:
1174
0
    tls_type = GOT_NORMAL;
1175
0
    is_got = true;
1176
0
    break;
1177
1178
0
  case R_XTENSA_PLT:
1179
0
    tls_type = GOT_NORMAL;
1180
0
    is_plt = true;
1181
0
    break;
1182
1183
0
  case R_XTENSA_GNU_VTINHERIT:
1184
    /* This relocation describes the C++ object vtable hierarchy.
1185
       Reconstruct it for later use during GC.  */
1186
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1187
0
      return false;
1188
0
    continue;
1189
1190
0
  case R_XTENSA_GNU_VTENTRY:
1191
    /* This relocation describes which C++ vtable entries are actually
1192
       used.  Record for later use during GC.  */
1193
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1194
0
      return false;
1195
0
    continue;
1196
1197
0
  default:
1198
    /* Nothing to do for any other relocations.  */
1199
0
    continue;
1200
0
  }
1201
1202
0
      if (h)
1203
0
  {
1204
0
    if (is_plt)
1205
0
      {
1206
0
        if (h->plt.refcount <= 0)
1207
0
    {
1208
0
      h->needs_plt = 1;
1209
0
      h->plt.refcount = 1;
1210
0
    }
1211
0
        else
1212
0
    h->plt.refcount += 1;
1213
1214
        /* Keep track of the total PLT relocation count even if we
1215
     don't yet know whether the dynamic sections will be
1216
     created.  */
1217
0
        htab->plt_reloc_count += 1;
1218
1219
0
        if (elf_hash_table (info)->dynamic_sections_created)
1220
0
    {
1221
0
      if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1222
0
        return false;
1223
0
    }
1224
0
      }
1225
0
    else if (is_got)
1226
0
      {
1227
0
        if (h->got.refcount <= 0)
1228
0
    h->got.refcount = 1;
1229
0
        else
1230
0
    h->got.refcount += 1;
1231
0
      }
1232
1233
0
    if (is_tlsfunc)
1234
0
      eh->tlsfunc_refcount += 1;
1235
1236
0
    old_tls_type = eh->tls_type;
1237
0
  }
1238
0
      else
1239
0
  {
1240
    /* Allocate storage the first time.  */
1241
0
    if (elf_local_got_refcounts (abfd) == NULL)
1242
0
      {
1243
0
        bfd_size_type size = symtab_hdr->sh_info;
1244
0
        void *mem;
1245
1246
0
        mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1247
0
        if (mem == NULL)
1248
0
    return false;
1249
0
        elf_local_got_refcounts (abfd) = (bfd_signed_vma *) mem;
1250
1251
0
        mem = bfd_zalloc (abfd, size);
1252
0
        if (mem == NULL)
1253
0
    return false;
1254
0
        elf_xtensa_local_got_tls_type (abfd) = (char *) mem;
1255
1256
0
        mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1257
0
        if (mem == NULL)
1258
0
    return false;
1259
0
        elf_xtensa_local_tlsfunc_refcounts (abfd)
1260
0
    = (bfd_signed_vma *) mem;
1261
0
      }
1262
1263
    /* This is a global offset table entry for a local symbol.  */
1264
0
    if (is_got || is_plt)
1265
0
      elf_local_got_refcounts (abfd) [r_symndx] += 1;
1266
1267
0
    if (is_tlsfunc)
1268
0
      elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx] += 1;
1269
1270
0
    old_tls_type = elf_xtensa_local_got_tls_type (abfd) [r_symndx];
1271
0
  }
1272
1273
0
      if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1274
0
  tls_type |= old_tls_type;
1275
      /* If a TLS symbol is accessed using IE at least once,
1276
   there is no point to use a dynamic model for it.  */
1277
0
      else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1278
0
         && ((old_tls_type & GOT_TLS_GD) == 0
1279
0
       || (tls_type & GOT_TLS_IE) == 0))
1280
0
  {
1281
0
    if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GD))
1282
0
      tls_type = old_tls_type;
1283
0
    else if ((old_tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_GD))
1284
0
      tls_type |= old_tls_type;
1285
0
    else
1286
0
      {
1287
0
        _bfd_error_handler
1288
    /* xgettext:c-format */
1289
0
    (_("%pB: `%s' accessed both as normal and thread local symbol"),
1290
0
     abfd,
1291
0
     h ? h->root.root.string : "<local>");
1292
0
        return false;
1293
0
      }
1294
0
  }
1295
1296
0
      if (old_tls_type != tls_type)
1297
0
  {
1298
0
    if (eh)
1299
0
      eh->tls_type = tls_type;
1300
0
    else
1301
0
      elf_xtensa_local_got_tls_type (abfd) [r_symndx] = tls_type;
1302
0
  }
1303
0
    }
1304
1305
0
  return true;
1306
0
}
1307
1308
1309
static void
1310
elf_xtensa_make_sym_local (struct bfd_link_info *info,
1311
         struct elf_link_hash_entry *h)
1312
0
{
1313
0
  if (bfd_link_pic (info))
1314
0
    {
1315
0
      if (h->plt.refcount > 0)
1316
0
  {
1317
    /* For shared objects, there's no need for PLT entries for local
1318
       symbols (use RELATIVE relocs instead of JMP_SLOT relocs).  */
1319
0
    if (h->got.refcount < 0)
1320
0
      h->got.refcount = 0;
1321
0
    h->got.refcount += h->plt.refcount;
1322
0
    h->plt.refcount = 0;
1323
0
  }
1324
0
    }
1325
0
  else
1326
0
    {
1327
      /* Don't need any dynamic relocations at all.  */
1328
0
      h->plt.refcount = 0;
1329
0
      h->got.refcount = 0;
1330
0
    }
1331
0
}
1332
1333
1334
static void
1335
elf_xtensa_hide_symbol (struct bfd_link_info *info,
1336
      struct elf_link_hash_entry *h,
1337
      bool force_local)
1338
0
{
1339
  /* For a shared link, move the plt refcount to the got refcount to leave
1340
     space for RELATIVE relocs.  */
1341
0
  elf_xtensa_make_sym_local (info, h);
1342
1343
0
  _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1344
0
}
1345
1346
1347
/* Return the section that should be marked against GC for a given
1348
   relocation.  */
1349
1350
static asection *
1351
elf_xtensa_gc_mark_hook (asection *sec,
1352
       struct bfd_link_info *info,
1353
       struct elf_reloc_cookie *cookie,
1354
       struct elf_link_hash_entry *h,
1355
       unsigned int symndx)
1356
0
{
1357
  /* Nothing refers to a property section, so it is kept alive by
1358
     elf_xtensa_gc_mark_extra_sections instead, and its relocations must
1359
     not cause other sections to be marked.  (This relies on
1360
     elf_xtensa_discard_info to remove property table entries that
1361
     describe discarded sections, which is needed anyway because a
1362
     property section may describe more than one section.)  */
1363
0
  if (xtensa_is_property_section (sec))
1364
0
    return NULL;
1365
1366
0
  if (h != NULL)
1367
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
1368
0
      {
1369
0
      case R_XTENSA_GNU_VTINHERIT:
1370
0
      case R_XTENSA_GNU_VTENTRY:
1371
0
  return NULL;
1372
0
      }
1373
1374
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
1375
0
}
1376
1377
1378
/* Property sections are not referenced by anything, so this pass marks
1379
   the ones describing sections that have been marked.  */
1380
1381
static bool
1382
elf_xtensa_gc_mark_extra_sections (struct bfd_link_info *info,
1383
           elf_gc_mark_hook_fn gc_mark_hook)
1384
0
{
1385
0
  static const char *const prop_names[] = XTENSA_PROPERTY_SEC_NAMES;
1386
0
  bfd *sub;
1387
0
  bool warned = false;
1388
1389
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
1390
1391
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
1392
0
    {
1393
0
      asection *sec;
1394
1395
0
      if (!is_xtensa_elf (sub))
1396
0
  continue;
1397
1398
0
      for (sec = sub->sections; sec != NULL; sec = sec->next)
1399
0
  {
1400
0
    size_t i;
1401
1402
0
    if (!sec->gc_mark
1403
0
        || (sec->flags & SEC_ALLOC) == 0
1404
0
        || xtensa_is_property_section (sec))
1405
0
      continue;
1406
1407
0
    for (i = 0; i < sizeof (prop_names) / sizeof (prop_names[0]); i++)
1408
0
      {
1409
0
        asection *prop_sec =
1410
0
    xtensa_get_property_section (sec, prop_names[i]);
1411
1412
        /* Set "gc_mark" directly rather than calling _bfd_elf_gc_mark:
1413
     a property section can be a member of the COMDAT group of
1414
     the section it describes, and marking a group member retains
1415
     the entire group.  Nothing else needs to be marked because
1416
     elf_xtensa_gc_mark_hook ignores relocations in property
1417
     sections.  */
1418
0
        if (prop_sec != NULL)
1419
0
    {
1420
0
      if (!warned && (prop_sec->flags & SEC_KEEP) != 0)
1421
0
        {
1422
0
          info->callbacks->einfo
1423
0
      (_("%P: warning: KEEP should not be used for Xtensa "
1424
0
         "property section %s\n"), prop_names[i]);
1425
0
          warned = true;
1426
0
        }
1427
0
      prop_sec->gc_mark = 1;
1428
0
    }
1429
0
      }
1430
0
  }
1431
0
    }
1432
1433
0
  return true;
1434
0
}
1435
1436
1437
/* Create all the dynamic sections.  */
1438
1439
static bool
1440
elf_xtensa_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1441
0
{
1442
0
  struct elf_xtensa_link_hash_table *htab;
1443
0
  flagword flags, noalloc_flags;
1444
1445
0
  htab = elf_xtensa_hash_table (info);
1446
0
  if (htab == NULL)
1447
0
    return false;
1448
1449
  /* First do all the standard stuff.  */
1450
0
  if (! _bfd_elf_create_dynamic_sections (dynobj, info))
1451
0
    return false;
1452
1453
  /* Create any extra PLT sections in case check_relocs has already
1454
     been called on all the non-dynamic input files.  */
1455
0
  if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1456
0
    return false;
1457
1458
0
  noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
1459
0
       | SEC_LINKER_CREATED | SEC_READONLY);
1460
0
  flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
1461
1462
  /* Mark the ".got.plt" section READONLY.  */
1463
0
  if (htab->elf.sgotplt == NULL
1464
0
      || !bfd_set_section_flags (htab->elf.sgotplt, flags))
1465
0
    return false;
1466
1467
  /* Create ".got.loc" (literal tables for use by dynamic linker).  */
1468
0
  htab->sgotloc = bfd_make_section_anyway_with_flags (dynobj, ".got.loc",
1469
0
                  flags);
1470
0
  if (htab->sgotloc == NULL
1471
0
      || !bfd_set_section_alignment (htab->sgotloc, 2))
1472
0
    return false;
1473
1474
  /* Create ".xt.lit.plt" (literal table for ".got.plt*").  */
1475
0
  htab->spltlittbl = bfd_make_section_anyway_with_flags (dynobj, ".xt.lit.plt",
1476
0
               noalloc_flags);
1477
0
  if (htab->spltlittbl == NULL
1478
0
      || !bfd_set_section_alignment (htab->spltlittbl, 2))
1479
0
    return false;
1480
1481
0
  return true;
1482
0
}
1483
1484
1485
static bool
1486
add_extra_plt_sections (struct bfd_link_info *info, int count)
1487
0
{
1488
0
  bfd *dynobj = elf_hash_table (info)->dynobj;
1489
0
  int chunk;
1490
1491
  /* Iterate over all chunks except 0 which uses the standard ".plt" and
1492
     ".got.plt" sections.  */
1493
0
  for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
1494
0
    {
1495
0
      char *sname;
1496
0
      flagword flags;
1497
0
      asection *s;
1498
1499
      /* Stop when we find a section has already been created.  */
1500
0
      if (elf_xtensa_get_plt_section (info, chunk))
1501
0
  break;
1502
1503
0
      flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1504
0
         | SEC_LINKER_CREATED | SEC_READONLY);
1505
1506
0
      sname = (char *) bfd_malloc (10);
1507
0
      sprintf (sname, ".plt.%u", chunk);
1508
0
      s = bfd_make_section_anyway_with_flags (dynobj, sname, flags | SEC_CODE);
1509
0
      if (s == NULL
1510
0
    || !bfd_set_section_alignment (s, 2))
1511
0
  return false;
1512
1513
0
      sname = (char *) bfd_malloc (14);
1514
0
      sprintf (sname, ".got.plt.%u", chunk);
1515
0
      s = bfd_make_section_anyway_with_flags (dynobj, sname, flags);
1516
0
      if (s == NULL
1517
0
    || !bfd_set_section_alignment (s, 2))
1518
0
  return false;
1519
0
    }
1520
1521
0
  return true;
1522
0
}
1523
1524
1525
/* Adjust a symbol defined by a dynamic object and referenced by a
1526
   regular object.  The current definition is in some section of the
1527
   dynamic object, but we're not including those sections.  We have to
1528
   change the definition to something the rest of the link can
1529
   understand.  */
1530
1531
static bool
1532
elf_xtensa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1533
          struct elf_link_hash_entry *h)
1534
0
{
1535
  /* If this is a weak symbol, and there is a real definition, the
1536
     processor independent code will have arranged for us to see the
1537
     real definition first, and we can just use the same value.  */
1538
0
  if (h->is_weakalias)
1539
0
    {
1540
0
      struct elf_link_hash_entry *def = weakdef (h);
1541
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1542
0
      h->root.u.def.section = def->root.u.def.section;
1543
0
      h->root.u.def.value = def->root.u.def.value;
1544
0
      return true;
1545
0
    }
1546
1547
  /* This is a reference to a symbol defined by a dynamic object.  The
1548
     reference must go through the GOT, so there's no need for COPY relocs,
1549
     .dynbss, etc.  */
1550
1551
0
  return true;
1552
0
}
1553
1554
1555
static bool
1556
elf_xtensa_allocate_dynrelocs (struct elf_link_hash_entry *h, void *arg)
1557
0
{
1558
0
  struct bfd_link_info *info;
1559
0
  struct elf_xtensa_link_hash_table *htab;
1560
0
  struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (h);
1561
1562
0
  if (h->root.type == bfd_link_hash_indirect)
1563
0
    return true;
1564
1565
0
  info = (struct bfd_link_info *) arg;
1566
0
  htab = elf_xtensa_hash_table (info);
1567
0
  if (htab == NULL)
1568
0
    return false;
1569
1570
  /* If we saw any use of an IE model for this symbol, we can then optimize
1571
     away GOT entries for any TLSDESC_FN relocs.  */
1572
0
  if ((eh->tls_type & GOT_TLS_IE) != 0)
1573
0
    {
1574
0
      BFD_ASSERT (h->got.refcount >= eh->tlsfunc_refcount);
1575
0
      h->got.refcount -= eh->tlsfunc_refcount;
1576
0
    }
1577
1578
0
  if (! elf_xtensa_dynamic_symbol_p (h, info))
1579
0
    elf_xtensa_make_sym_local (info, h);
1580
1581
0
  if (! elf_xtensa_dynamic_symbol_p (h, info)
1582
0
      && h->root.type == bfd_link_hash_undefweak)
1583
0
    return true;
1584
1585
0
  if (h->plt.refcount > 0)
1586
0
    htab->elf.srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1587
1588
0
  if (h->got.refcount > 0)
1589
0
    htab->elf.srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
1590
1591
0
  return true;
1592
0
}
1593
1594
1595
static void
1596
elf_xtensa_allocate_local_got_size (struct bfd_link_info *info)
1597
0
{
1598
0
  struct elf_xtensa_link_hash_table *htab;
1599
0
  bfd *i;
1600
1601
0
  htab = elf_xtensa_hash_table (info);
1602
0
  if (htab == NULL)
1603
0
    return;
1604
1605
0
  for (i = info->input_bfds; i; i = i->link.next)
1606
0
    {
1607
0
      bfd_signed_vma *local_got_refcounts;
1608
0
      bfd_size_type j, cnt;
1609
0
      Elf_Internal_Shdr *symtab_hdr;
1610
1611
0
      local_got_refcounts = elf_local_got_refcounts (i);
1612
0
      if (!local_got_refcounts)
1613
0
  continue;
1614
1615
0
      symtab_hdr = &elf_symtab_hdr (i);
1616
0
      cnt = symtab_hdr->sh_info;
1617
1618
0
      for (j = 0; j < cnt; ++j)
1619
0
  {
1620
    /* If we saw any use of an IE model for this symbol, we can
1621
       then optimize away GOT entries for any TLSDESC_FN relocs.  */
1622
0
    if ((elf_xtensa_local_got_tls_type (i) [j] & GOT_TLS_IE) != 0)
1623
0
      {
1624
0
        bfd_signed_vma *tlsfunc_refcount
1625
0
    = &elf_xtensa_local_tlsfunc_refcounts (i) [j];
1626
0
        BFD_ASSERT (local_got_refcounts[j] >= *tlsfunc_refcount);
1627
0
        local_got_refcounts[j] -= *tlsfunc_refcount;
1628
0
      }
1629
1630
0
    if (local_got_refcounts[j] > 0)
1631
0
      htab->elf.srelgot->size += (local_got_refcounts[j]
1632
0
          * sizeof (Elf32_External_Rela));
1633
0
  }
1634
0
    }
1635
0
}
1636
1637
1638
/* Set the sizes of the dynamic sections.  */
1639
1640
static bool
1641
elf_xtensa_late_size_sections (struct bfd_link_info *info)
1642
0
{
1643
0
  struct elf_xtensa_link_hash_table *htab;
1644
0
  bfd *dynobj, *abfd;
1645
0
  asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1646
0
  bool relplt, relgot;
1647
0
  int plt_entries, plt_chunks, chunk;
1648
1649
0
  plt_entries = 0;
1650
0
  plt_chunks = 0;
1651
1652
0
  htab = elf_xtensa_hash_table (info);
1653
0
  if (htab == NULL)
1654
0
    return false;
1655
1656
0
  dynobj = elf_hash_table (info)->dynobj;
1657
0
  if (dynobj == NULL)
1658
0
    return true;
1659
0
  srelgot = htab->elf.srelgot;
1660
0
  srelplt = htab->elf.srelplt;
1661
1662
0
  if (elf_hash_table (info)->dynamic_sections_created)
1663
0
    {
1664
0
      BFD_ASSERT (htab->elf.srelgot != NULL
1665
0
      && htab->elf.srelplt != NULL
1666
0
      && htab->elf.sgot != NULL
1667
0
      && htab->spltlittbl != NULL
1668
0
      && htab->sgotloc != NULL);
1669
1670
      /* Set the contents of the .interp section to the interpreter.  */
1671
0
      if (bfd_link_executable (info) && !info->nointerp)
1672
0
  {
1673
0
    s = elf_hash_table (info)->interp;
1674
0
    if (s == NULL)
1675
0
      abort ();
1676
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1677
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1678
0
    s->alloced = 1;
1679
0
  }
1680
1681
      /* Allocate room for one word in ".got".  */
1682
0
      htab->elf.sgot->size = 4;
1683
1684
      /* Allocate space in ".rela.got" for literals that reference global
1685
   symbols and space in ".rela.plt" for literals that have PLT
1686
   entries.  */
1687
0
      elf_link_hash_traverse (elf_hash_table (info),
1688
0
            elf_xtensa_allocate_dynrelocs,
1689
0
            (void *) info);
1690
1691
      /* If we are generating a shared object, we also need space in
1692
   ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1693
   reference local symbols.  */
1694
0
      if (bfd_link_pic (info))
1695
0
  elf_xtensa_allocate_local_got_size (info);
1696
1697
      /* Allocate space in ".plt" to match the size of ".rela.plt".  For
1698
   each PLT entry, we need the PLT code plus a 4-byte literal.
1699
   For each chunk of ".plt", we also need two more 4-byte
1700
   literals, two corresponding entries in ".rela.got", and an
1701
   8-byte entry in ".xt.lit.plt".  */
1702
0
      spltlittbl = htab->spltlittbl;
1703
0
      plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
1704
0
      plt_chunks =
1705
0
  (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1706
1707
      /* Iterate over all the PLT chunks, including any extra sections
1708
   created earlier because the initial count of PLT relocations
1709
   was an overestimate.  */
1710
0
      for (chunk = 0;
1711
0
     (splt = elf_xtensa_get_plt_section (info, chunk)) != NULL;
1712
0
     chunk++)
1713
0
  {
1714
0
    int chunk_entries;
1715
1716
0
    sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
1717
0
    BFD_ASSERT (sgotplt != NULL);
1718
1719
0
    if (chunk < plt_chunks - 1)
1720
0
      chunk_entries = PLT_ENTRIES_PER_CHUNK;
1721
0
    else if (chunk == plt_chunks - 1)
1722
0
      chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1723
0
    else
1724
0
      chunk_entries = 0;
1725
1726
0
    if (chunk_entries != 0)
1727
0
      {
1728
0
        sgotplt->size = 4 * (chunk_entries + 2);
1729
0
        splt->size = PLT_ENTRY_SIZE * chunk_entries;
1730
0
        srelgot->size += 2 * sizeof (Elf32_External_Rela);
1731
0
        spltlittbl->size += 8;
1732
0
      }
1733
0
    else
1734
0
      {
1735
0
        sgotplt->size = 0;
1736
0
        splt->size = 0;
1737
0
      }
1738
0
  }
1739
1740
      /* Allocate space in ".got.loc" to match the total size of all the
1741
   literal tables.  */
1742
0
      sgotloc = htab->sgotloc;
1743
0
      sgotloc->size = spltlittbl->size;
1744
0
      for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
1745
0
  {
1746
0
    if (abfd->flags & DYNAMIC)
1747
0
      continue;
1748
0
    for (s = abfd->sections; s != NULL; s = s->next)
1749
0
      {
1750
0
        if (! discarded_section (s)
1751
0
      && xtensa_is_littable_section (s)
1752
0
      && s != spltlittbl)
1753
0
    sgotloc->size += s->size;
1754
0
      }
1755
0
  }
1756
0
    }
1757
1758
  /* Allocate memory for dynamic sections.  */
1759
0
  relplt = false;
1760
0
  relgot = false;
1761
0
  for (s = dynobj->sections; s != NULL; s = s->next)
1762
0
    {
1763
0
      const char *name;
1764
1765
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
1766
0
  continue;
1767
1768
      /* It's OK to base decisions on the section name, because none
1769
   of the dynobj section names depend upon the input files.  */
1770
0
      name = bfd_section_name (s);
1771
1772
0
      if (startswith (name, ".rela"))
1773
0
  {
1774
0
    if (s->size != 0)
1775
0
      {
1776
0
        if (strcmp (name, ".rela.plt") == 0)
1777
0
    relplt = true;
1778
0
        else if (strcmp (name, ".rela.got") == 0)
1779
0
    relgot = true;
1780
1781
        /* We use the reloc_count field as a counter if we need
1782
     to copy relocs into the output file.  */
1783
0
        s->reloc_count = 0;
1784
0
      }
1785
0
  }
1786
0
      else if (! startswith (name, ".plt.")
1787
0
         && ! startswith (name, ".got.plt.")
1788
0
         && strcmp (name, ".got") != 0
1789
0
         && strcmp (name, ".plt") != 0
1790
0
         && strcmp (name, ".got.plt") != 0
1791
0
         && strcmp (name, ".xt.lit.plt") != 0
1792
0
         && strcmp (name, ".got.loc") != 0)
1793
0
  {
1794
    /* It's not one of our sections, so don't allocate space.  */
1795
0
    continue;
1796
0
  }
1797
1798
0
      if (s->size == 0)
1799
0
  {
1800
    /* If we don't need this section, strip it from the output
1801
       file.  We must create the ".plt*" and ".got.plt*"
1802
       sections in create_dynamic_sections and/or check_relocs
1803
       based on a conservative estimate of the PLT relocation
1804
       count, because the sections must be created before the
1805
       linker maps input sections to output sections.  The
1806
       linker does that before size_dynamic_sections, where we
1807
       compute the exact size of the PLT, so there may be more
1808
       of these sections than are actually needed.  */
1809
0
    s->flags |= SEC_EXCLUDE;
1810
0
  }
1811
0
      else if ((s->flags & SEC_HAS_CONTENTS) != 0)
1812
0
  {
1813
    /* Allocate memory for the section contents.  */
1814
0
    s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1815
0
    if (s->contents == NULL)
1816
0
      return false;
1817
0
    s->alloced = 1;
1818
0
  }
1819
0
    }
1820
1821
0
  if (elf_hash_table (info)->dynamic_sections_created)
1822
0
    {
1823
      /* Add the special XTENSA_RTLD relocations now.  The offsets won't be
1824
   known until finish_dynamic_sections, but we need to get the relocs
1825
   in place before they are sorted.  */
1826
0
      for (chunk = 0; chunk < plt_chunks; chunk++)
1827
0
  {
1828
0
    Elf_Internal_Rela irela;
1829
0
    bfd_byte *loc;
1830
1831
0
    irela.r_offset = 0;
1832
0
    irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1833
0
    irela.r_addend = 0;
1834
1835
0
    loc = (srelgot->contents
1836
0
     + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1837
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &irela, loc);
1838
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &irela,
1839
0
             loc + sizeof (Elf32_External_Rela));
1840
0
    srelgot->reloc_count += 2;
1841
0
  }
1842
1843
      /* Add some entries to the .dynamic section.  We fill in the
1844
   values later, in elf_xtensa_finish_dynamic_sections, but we
1845
   must add the entries now so that we get the correct size for
1846
   the .dynamic section.  The DT_DEBUG entry is filled in by the
1847
   dynamic linker and used by the debugger.  */
1848
0
#define add_dynamic_entry(TAG, VAL) \
1849
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1850
1851
0
      if (!_bfd_elf_add_dynamic_tags (info, relplt || relgot))
1852
0
  return false;
1853
1854
0
      if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1855
0
    || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1856
0
  return false;
1857
0
    }
1858
0
#undef add_dynamic_entry
1859
1860
0
  return true;
1861
0
}
1862
1863
static bool
1864
elf_xtensa_early_size_sections (struct bfd_link_info *info)
1865
0
{
1866
0
  struct elf_xtensa_link_hash_table *htab;
1867
0
  asection *tls_sec;
1868
1869
0
  htab = elf_xtensa_hash_table (info);
1870
0
  if (htab == NULL)
1871
0
    return false;
1872
1873
0
  tls_sec = htab->elf.tls_sec;
1874
1875
0
  if (tls_sec && (htab->tlsbase->tls_type & GOT_TLS_ANY) != 0)
1876
0
    {
1877
0
      struct elf_link_hash_entry *tlsbase = &htab->tlsbase->elf;
1878
0
      struct bfd_link_hash_entry *bh = &tlsbase->root;
1879
0
      elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
1880
1881
0
      tlsbase->type = STT_TLS;
1882
0
      if (!(_bfd_generic_link_add_one_symbol
1883
0
      (info, info->output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1884
0
       tls_sec, 0, NULL, false,
1885
0
       bed->collect, &bh)))
1886
0
  return false;
1887
0
      tlsbase->def_regular = 1;
1888
0
      tlsbase->other = STV_HIDDEN;
1889
0
      (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
1890
0
    }
1891
1892
0
  return true;
1893
0
}
1894
1895

1896
/* Return the base VMA address which should be subtracted from real addresses
1897
   when resolving @dtpoff relocation.
1898
   This is PT_TLS segment p_vaddr.  */
1899
1900
static bfd_vma
1901
dtpoff_base (struct bfd_link_info *info)
1902
0
{
1903
  /* If tls_sec is NULL, we should have signalled an error already.  */
1904
0
  if (elf_hash_table (info)->tls_sec == NULL)
1905
0
    return 0;
1906
0
  return elf_hash_table (info)->tls_sec->vma;
1907
0
}
1908
1909
/* Return the relocation value for @tpoff relocation
1910
   if STT_TLS virtual address is ADDRESS.  */
1911
1912
static bfd_vma
1913
tpoff (struct bfd_link_info *info, bfd_vma address)
1914
0
{
1915
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
1916
0
  bfd_vma base;
1917
1918
  /* If tls_sec is NULL, we should have signalled an error already.  */
1919
0
  if (htab->tls_sec == NULL)
1920
0
    return 0;
1921
0
  base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
1922
0
  return address - htab->tls_sec->vma + base;
1923
0
}
1924
1925
/* Perform the specified relocation.  The instruction at (contents + address)
1926
   is modified to set one operand to represent the value in "relocation".  The
1927
   operand position is determined by the relocation type recorded in the
1928
   howto.  */
1929
1930
100
#define CALL_SEGMENT_BITS (30)
1931
#define CALL_SEGMENT_SIZE (1 << CALL_SEGMENT_BITS)
1932
1933
static bfd_reloc_status_type
1934
elf_xtensa_do_reloc (reloc_howto_type *howto,
1935
         bfd *abfd,
1936
         asection *input_section,
1937
         bfd_vma relocation,
1938
         bfd_byte *contents,
1939
         bfd_vma address,
1940
         bool is_weak_undef,
1941
         char **error_message)
1942
7.49k
{
1943
7.49k
  xtensa_format fmt;
1944
7.49k
  xtensa_opcode opcode;
1945
7.49k
  xtensa_isa isa = xtensa_default_isa;
1946
7.49k
  static xtensa_insnbuf ibuff = NULL;
1947
7.49k
  static xtensa_insnbuf sbuff = NULL;
1948
7.49k
  bfd_vma self_address;
1949
7.49k
  bfd_size_type input_size;
1950
7.49k
  int opnd, slot;
1951
7.49k
  uint32 newval;
1952
1953
7.49k
  if (!ibuff)
1954
1
    {
1955
1
      ibuff = xtensa_insnbuf_alloc (isa);
1956
1
      sbuff = xtensa_insnbuf_alloc (isa);
1957
1
    }
1958
1959
7.49k
  input_size = bfd_get_section_limit (abfd, input_section);
1960
1961
  /* Calculate the PC address for this instruction.  */
1962
7.49k
  self_address = (input_section->output_section->vma
1963
7.49k
      + input_section->output_offset
1964
7.49k
      + address);
1965
1966
7.49k
  switch (howto->type)
1967
7.49k
    {
1968
2.16k
    case R_XTENSA_NONE:
1969
2.17k
    case R_XTENSA_DIFF8:
1970
2.17k
    case R_XTENSA_DIFF16:
1971
2.17k
    case R_XTENSA_DIFF32:
1972
2.27k
    case R_XTENSA_PDIFF8:
1973
2.29k
    case R_XTENSA_PDIFF16:
1974
2.29k
    case R_XTENSA_PDIFF32:
1975
2.29k
    case R_XTENSA_NDIFF8:
1976
2.30k
    case R_XTENSA_NDIFF16:
1977
2.30k
    case R_XTENSA_NDIFF32:
1978
2.30k
    case R_XTENSA_PDIFF_ULEB128:
1979
2.33k
    case R_XTENSA_TLS_FUNC:
1980
2.35k
    case R_XTENSA_TLS_ARG:
1981
2.65k
    case R_XTENSA_TLS_CALL:
1982
2.65k
      return bfd_reloc_ok;
1983
1984
4
    case R_XTENSA_ASM_EXPAND:
1985
4
      if (!is_weak_undef)
1986
4
  {
1987
    /* Check for windowed CALL across a 1GB boundary.  */
1988
4
    opcode = get_expanded_call_opcode (contents + address,
1989
4
               input_size - address, 0);
1990
4
    if (is_windowed_call_opcode (opcode))
1991
0
      {
1992
0
        if ((self_address >> CALL_SEGMENT_BITS)
1993
0
      != (relocation >> CALL_SEGMENT_BITS))
1994
0
    {
1995
0
      *error_message = "windowed longcall crosses 1GB boundary; "
1996
0
        "return may fail";
1997
0
      return bfd_reloc_dangerous;
1998
0
    }
1999
0
      }
2000
4
  }
2001
4
      return bfd_reloc_ok;
2002
2003
71
    case R_XTENSA_ASM_SIMPLIFY:
2004
71
      {
2005
  /* Convert the L32R/CALLX to CALL.  */
2006
71
  bfd_reloc_status_type retval =
2007
71
    elf_xtensa_do_asm_simplify (contents, address, input_size,
2008
71
              error_message);
2009
71
  if (retval != bfd_reloc_ok)
2010
71
    return bfd_reloc_dangerous;
2011
2012
  /* The CALL needs to be relocated.  Continue below for that part.  */
2013
0
  address += 3;
2014
0
  self_address += 3;
2015
0
  howto = &elf_howto_table[(unsigned) R_XTENSA_SLOT0_OP ];
2016
0
      }
2017
0
      break;
2018
2019
726
    case R_XTENSA_32:
2020
726
      {
2021
726
  bfd_vma x;
2022
726
  x = bfd_get_32 (abfd, contents + address);
2023
726
  x = x + relocation;
2024
726
  bfd_put_32 (abfd, x, contents + address);
2025
726
      }
2026
726
      return bfd_reloc_ok;
2027
2028
1
    case R_XTENSA_32_PCREL:
2029
1
      bfd_put_32 (abfd, relocation - self_address, contents + address);
2030
1
      return bfd_reloc_ok;
2031
2032
0
    case R_XTENSA_32_ABS:
2033
3
    case R_XTENSA_PLT:
2034
35
    case R_XTENSA_TLSDESC_FN:
2035
45
    case R_XTENSA_TLSDESC_ARG:
2036
92
    case R_XTENSA_TLS_DTPOFF:
2037
109
    case R_XTENSA_TLS_TPOFF:
2038
109
      bfd_put_32 (abfd, relocation, contents + address);
2039
109
      return bfd_reloc_ok;
2040
7.49k
    }
2041
2042
  /* Only instruction slot-specific relocations handled below.... */
2043
3.93k
  slot = get_relocation_slot (howto->type);
2044
3.93k
  if (slot == XTENSA_UNDEFINED)
2045
0
    {
2046
0
      *error_message = "unexpected relocation";
2047
0
      return bfd_reloc_dangerous;
2048
0
    }
2049
2050
3.93k
  if (input_size <= address)
2051
0
    return bfd_reloc_outofrange;
2052
  /* Read the instruction into a buffer and decode the opcode.  */
2053
3.93k
  if (xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
2054
3.93k
         input_size - address) < 0
2055
3.93k
      || (fmt = xtensa_format_decode (isa, ibuff)) == XTENSA_UNDEFINED)
2056
28
    {
2057
28
      *error_message = "cannot decode instruction format";
2058
28
      return bfd_reloc_dangerous;
2059
28
    }
2060
2061
3.90k
  xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
2062
2063
3.90k
  opcode = xtensa_opcode_decode (isa, fmt, slot, sbuff);
2064
3.90k
  if (opcode == XTENSA_UNDEFINED)
2065
850
    {
2066
850
      *error_message = "cannot decode instruction opcode";
2067
850
      return bfd_reloc_dangerous;
2068
850
    }
2069
2070
  /* Check for opcode-specific "alternate" relocations.  */
2071
3.05k
  if (is_alt_relocation (howto->type))
2072
38
    {
2073
38
      if (opcode == get_l32r_opcode ())
2074
0
  {
2075
    /* Handle the special-case of non-PC-relative L32R instructions.  */
2076
0
    bfd *output_bfd = input_section->output_section->owner;
2077
0
    asection *lit4_sec = bfd_get_section_by_name (output_bfd, ".lit4");
2078
0
    if (!lit4_sec)
2079
0
      {
2080
0
        *error_message = "relocation references missing .lit4 section";
2081
0
        return bfd_reloc_dangerous;
2082
0
      }
2083
0
    self_address = ((lit4_sec->vma & ~0xfff)
2084
0
        + 0x40000 - 3); /* -3 to compensate for do_reloc */
2085
0
    newval = relocation;
2086
0
    opnd = 1;
2087
0
  }
2088
38
      else if (opcode == get_const16_opcode ())
2089
0
  {
2090
    /* ALT used for high 16 bits.
2091
       Ignore 32-bit overflow.  */
2092
0
    newval = (relocation >> 16) & 0xffff;
2093
0
    opnd = 1;
2094
0
  }
2095
38
      else
2096
38
  {
2097
    /* No other "alternate" relocations currently defined.  */
2098
38
    *error_message = "unexpected relocation";
2099
38
    return bfd_reloc_dangerous;
2100
38
  }
2101
38
    }
2102
3.01k
  else /* Not an "alternate" relocation.... */
2103
3.01k
    {
2104
3.01k
      if (opcode == get_const16_opcode ())
2105
0
  {
2106
0
    newval = relocation & 0xffff;
2107
0
    opnd = 1;
2108
0
  }
2109
3.01k
      else
2110
3.01k
  {
2111
    /* ...normal PC-relative relocation.... */
2112
2113
    /* Determine which operand is being relocated.  */
2114
3.01k
    opnd = get_relocation_opnd (opcode, howto->type);
2115
3.01k
    if (opnd == XTENSA_UNDEFINED)
2116
2.48k
      {
2117
2.48k
        *error_message = "unexpected relocation";
2118
2.48k
        return bfd_reloc_dangerous;
2119
2.48k
      }
2120
2121
535
    if (!howto->pc_relative)
2122
0
      {
2123
0
        *error_message = "expected PC-relative relocation";
2124
0
        return bfd_reloc_dangerous;
2125
0
      }
2126
2127
535
    newval = relocation;
2128
535
  }
2129
3.01k
    }
2130
2131
  /* Apply the relocation.  */
2132
535
  if (xtensa_operand_do_reloc (isa, opcode, opnd, &newval, self_address)
2133
535
      || xtensa_operand_encode (isa, opcode, opnd, &newval)
2134
361
      || xtensa_operand_set_field (isa, opcode, opnd, fmt, slot,
2135
361
           sbuff, newval))
2136
174
    {
2137
174
      const char *opname = xtensa_opcode_name (isa, opcode);
2138
174
      const char *msg;
2139
2140
174
      msg = "cannot encode";
2141
174
      if (is_direct_call_opcode (opcode))
2142
4
  {
2143
4
    if ((relocation & 0x3) != 0)
2144
1
      msg = "misaligned call target";
2145
3
    else
2146
3
      msg = "call target out of range";
2147
4
  }
2148
170
      else if (opcode == get_l32r_opcode ())
2149
30
  {
2150
30
    if ((relocation & 0x3) != 0)
2151
5
      msg = "misaligned literal target";
2152
25
    else if (is_alt_relocation (howto->type))
2153
0
      msg = "literal target out of range (too many literals)";
2154
25
    else if (self_address > relocation)
2155
0
      msg = "literal target out of range (try using text-section-literals)";
2156
25
    else
2157
25
      msg = "literal placed after use";
2158
30
  }
2159
2160
174
      *error_message = vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
2161
174
      return bfd_reloc_dangerous;
2162
174
    }
2163
2164
  /* Check for calls across 1GB boundaries.  */
2165
361
  if (is_direct_call_opcode (opcode)
2166
71
      && is_windowed_call_opcode (opcode))
2167
50
    {
2168
50
      if ((self_address >> CALL_SEGMENT_BITS)
2169
50
    != (relocation >> CALL_SEGMENT_BITS))
2170
0
  {
2171
0
    *error_message =
2172
0
      "windowed call crosses 1GB boundary; return may fail";
2173
0
    return bfd_reloc_dangerous;
2174
0
  }
2175
50
    }
2176
2177
  /* Write the modified instruction back out of the buffer.  */
2178
361
  xtensa_format_set_slot (isa, fmt, slot, ibuff, sbuff);
2179
361
  xtensa_insnbuf_to_chars (isa, ibuff, contents + address,
2180
361
         input_size - address);
2181
361
  return bfd_reloc_ok;
2182
361
}
2183
2184
2185
static char *
2186
vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
2187
3.81k
{
2188
  /* To reduce the size of the memory leak,
2189
     we only use a single message buffer.  */
2190
3.81k
  static bfd_size_type alloc_size = 0;
2191
3.81k
  static char *message = NULL;
2192
3.81k
  bfd_size_type orig_len, len = 0;
2193
3.81k
  bool is_append;
2194
3.81k
  va_list ap;
2195
2196
3.81k
  va_start (ap, arglen);
2197
2198
3.81k
  is_append = (origmsg == message);
2199
2200
3.81k
  orig_len = strlen (origmsg);
2201
3.81k
  len = orig_len + strlen (fmt) + arglen + 20;
2202
3.81k
  if (len > alloc_size)
2203
2
    {
2204
2
      message = (char *) bfd_realloc_or_free (message, len);
2205
2
      alloc_size = len;
2206
2
    }
2207
3.81k
  if (message != NULL)
2208
3.81k
    {
2209
3.81k
      if (!is_append)
2210
3.64k
  memcpy (message, origmsg, orig_len);
2211
3.81k
      vsprintf (message + orig_len, fmt, ap);
2212
3.81k
    }
2213
3.81k
  va_end (ap);
2214
3.81k
  return message;
2215
3.81k
}
2216
2217
2218
/* This function is registered as the "special_function" in the
2219
   Xtensa howto for handling simplify operations.
2220
   bfd_perform_relocation / bfd_install_relocation use it to
2221
   perform (install) the specified relocation.  Since this replaces the code
2222
   in bfd_perform_relocation, it is basically an Xtensa-specific,
2223
   stripped-down version of bfd_perform_relocation.  */
2224
2225
static bfd_reloc_status_type
2226
bfd_elf_xtensa_reloc (bfd *abfd,
2227
          arelent *reloc_entry,
2228
          asymbol *symbol,
2229
          void *data,
2230
          asection *input_section,
2231
          bfd *output_bfd,
2232
          char **error_message)
2233
8.20k
{
2234
8.20k
  bfd_vma relocation;
2235
8.20k
  bfd_reloc_status_type flag;
2236
8.20k
  bfd_size_type octets = (reloc_entry->address
2237
8.20k
        * OCTETS_PER_BYTE (abfd, input_section));
2238
8.20k
  bfd_vma output_base = 0;
2239
8.20k
  reloc_howto_type *howto = reloc_entry->howto;
2240
8.20k
  asection *reloc_target_output_section;
2241
8.20k
  bool is_weak_undef;
2242
2243
8.20k
  if (!xtensa_default_isa)
2244
1
    xtensa_default_isa = xtensa_isa_init (0, 0);
2245
2246
  /* ELF relocs are against symbols.  If we are producing relocatable
2247
     output, and the reloc is against an external symbol, the resulting
2248
     reloc will also be against the same symbol.  In such a case, we
2249
     don't want to change anything about the way the reloc is handled,
2250
     since it will all be done at final link time.  This test is similar
2251
     to what bfd_elf_generic_reloc does except that it lets relocs with
2252
     howto->partial_inplace go through even if the addend is non-zero.
2253
     (The real problem is that partial_inplace is set for XTENSA_32
2254
     relocs to begin with, but that's a long story and there's little we
2255
     can do about it now....)  */
2256
2257
8.20k
  if (output_bfd && (symbol->flags & BSF_SECTION_SYM) == 0)
2258
0
    {
2259
0
      reloc_entry->address += input_section->output_offset;
2260
0
      return bfd_reloc_ok;
2261
0
    }
2262
2263
  /* Is the address of the relocation really within the section?  */
2264
8.20k
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets))
2265
708
    return bfd_reloc_outofrange;
2266
2267
  /* Work out which section the relocation is targeted at and the
2268
     initial relocation command value.  */
2269
2270
  /* Get symbol value.  (Common symbols are special.)  */
2271
7.49k
  if (bfd_is_com_section (symbol->section))
2272
0
    relocation = 0;
2273
7.49k
  else
2274
7.49k
    relocation = symbol->value;
2275
2276
7.49k
  reloc_target_output_section = symbol->section->output_section;
2277
2278
  /* Convert input-section-relative symbol value to absolute.  */
2279
7.49k
  if ((output_bfd && !howto->partial_inplace)
2280
7.49k
      || reloc_target_output_section == NULL)
2281
0
    output_base = 0;
2282
7.49k
  else
2283
7.49k
    output_base = reloc_target_output_section->vma;
2284
2285
7.49k
  relocation += output_base + symbol->section->output_offset;
2286
2287
  /* Add in supplied addend.  */
2288
7.49k
  relocation += reloc_entry->addend;
2289
2290
  /* Here the variable relocation holds the final address of the
2291
     symbol we are relocating against, plus any addend.  */
2292
7.49k
  if (output_bfd)
2293
0
    {
2294
0
      if (!howto->partial_inplace)
2295
0
  {
2296
    /* This is a partial relocation, and we want to apply the relocation
2297
       to the reloc entry rather than the raw data.  Everything except
2298
       relocations against section symbols has already been handled
2299
       above.  */
2300
2301
0
    BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
2302
0
    reloc_entry->addend = relocation;
2303
0
    reloc_entry->address += input_section->output_offset;
2304
0
    return bfd_reloc_ok;
2305
0
  }
2306
0
      else
2307
0
  {
2308
0
    reloc_entry->address += input_section->output_offset;
2309
0
    reloc_entry->addend = 0;
2310
0
  }
2311
0
    }
2312
2313
7.49k
  is_weak_undef = (bfd_is_und_section (symbol->section)
2314
1.08k
       && (symbol->flags & BSF_WEAK) != 0);
2315
7.49k
  flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
2316
7.49k
            (bfd_byte *) data, (bfd_vma) octets,
2317
7.49k
            is_weak_undef, error_message);
2318
2319
7.49k
  if (flag == bfd_reloc_dangerous)
2320
3.64k
    {
2321
      /* Add the symbol name to the error message.  */
2322
3.64k
      if (! *error_message)
2323
0
  *error_message = "";
2324
3.64k
      *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
2325
3.64k
            strlen (symbol->name) + 17,
2326
3.64k
            symbol->name,
2327
3.64k
            (unsigned long) reloc_entry->addend);
2328
3.64k
    }
2329
2330
7.49k
  return flag;
2331
7.49k
}
2332
2333
int xtensa_abi_choice (void)
2334
0
{
2335
0
  if (elf32xtensa_abi == XTHAL_ABI_UNDEFINED)
2336
0
    return XSHAL_ABI;
2337
0
  else
2338
0
    return elf32xtensa_abi;
2339
0
}
2340
2341
/* Set up an entry in the procedure linkage table.  */
2342
2343
static bfd_vma
2344
elf_xtensa_create_plt_entry (struct bfd_link_info *info,
2345
           bfd *output_bfd,
2346
           unsigned reloc_index)
2347
0
{
2348
0
  asection *splt, *sgotplt;
2349
0
  bfd_vma plt_base, got_base;
2350
0
  bfd_vma code_offset, lit_offset, abi_offset;
2351
0
  int chunk;
2352
0
  int abi = xtensa_abi_choice ();
2353
2354
0
  chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
2355
0
  splt = elf_xtensa_get_plt_section (info, chunk);
2356
0
  sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
2357
0
  BFD_ASSERT (splt != NULL && sgotplt != NULL);
2358
2359
0
  plt_base = splt->output_section->vma + splt->output_offset;
2360
0
  got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2361
2362
0
  lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
2363
0
  code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
2364
2365
  /* Fill in the literal entry.  This is the offset of the dynamic
2366
     relocation entry.  */
2367
0
  bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
2368
0
        sgotplt->contents + lit_offset);
2369
2370
  /* Fill in the entry in the procedure linkage table.  */
2371
0
  memcpy (splt->contents + code_offset,
2372
0
    (bfd_big_endian (output_bfd)
2373
0
     ? elf_xtensa_be_plt_entry[abi != XTHAL_ABI_WINDOWED]
2374
0
     : elf_xtensa_le_plt_entry[abi != XTHAL_ABI_WINDOWED]),
2375
0
    PLT_ENTRY_SIZE);
2376
0
  abi_offset = abi == XTHAL_ABI_WINDOWED ? 3 : 0;
2377
0
  bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
2378
0
               plt_base + code_offset + abi_offset),
2379
0
        splt->contents + code_offset + abi_offset + 1);
2380
0
  bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
2381
0
               plt_base + code_offset + abi_offset + 3),
2382
0
        splt->contents + code_offset + abi_offset + 4);
2383
0
  bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
2384
0
               plt_base + code_offset + abi_offset + 6),
2385
0
        splt->contents + code_offset + abi_offset + 7);
2386
2387
0
  return plt_base + code_offset;
2388
0
}
2389
2390
2391
static bool get_indirect_call_dest_reg (xtensa_opcode, unsigned *);
2392
2393
static bool
2394
replace_tls_insn (Elf_Internal_Rela *rel,
2395
      bfd *abfd,
2396
      asection *input_section,
2397
      bfd_byte *contents,
2398
      bool is_ld_model,
2399
      char **error_message)
2400
0
{
2401
0
  static xtensa_insnbuf ibuff = NULL;
2402
0
  static xtensa_insnbuf sbuff = NULL;
2403
0
  xtensa_isa isa = xtensa_default_isa;
2404
0
  xtensa_format fmt;
2405
0
  xtensa_opcode old_op, new_op;
2406
0
  bfd_size_type input_size;
2407
0
  int r_type;
2408
0
  unsigned dest_reg, src_reg;
2409
2410
0
  if (ibuff == NULL)
2411
0
    {
2412
0
      ibuff = xtensa_insnbuf_alloc (isa);
2413
0
      sbuff = xtensa_insnbuf_alloc (isa);
2414
0
    }
2415
2416
0
  input_size = bfd_get_section_limit (abfd, input_section);
2417
2418
  /* Read the instruction into a buffer and decode the opcode.  */
2419
0
  if (xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
2420
0
         input_size - rel->r_offset) < 0
2421
0
      || (fmt = xtensa_format_decode (isa, ibuff)) == XTENSA_UNDEFINED)
2422
0
    {
2423
0
      *error_message = "cannot decode instruction format";
2424
0
      return false;
2425
0
    }
2426
2427
0
  BFD_ASSERT (xtensa_format_num_slots (isa, fmt) == 1);
2428
0
  xtensa_format_get_slot (isa, fmt, 0, ibuff, sbuff);
2429
2430
0
  old_op = xtensa_opcode_decode (isa, fmt, 0, sbuff);
2431
0
  if (old_op == XTENSA_UNDEFINED)
2432
0
    {
2433
0
      *error_message = "cannot decode instruction opcode";
2434
0
      return false;
2435
0
    }
2436
2437
0
  r_type = ELF32_R_TYPE (rel->r_info);
2438
0
  switch (r_type)
2439
0
    {
2440
0
    case R_XTENSA_TLS_FUNC:
2441
0
    case R_XTENSA_TLS_ARG:
2442
0
      if (old_op != get_l32r_opcode ()
2443
0
    || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2444
0
               sbuff, &dest_reg) != 0)
2445
0
  {
2446
0
    *error_message = "cannot extract L32R destination for TLS access";
2447
0
    return false;
2448
0
  }
2449
0
      break;
2450
2451
0
    case R_XTENSA_TLS_CALL:
2452
0
      if (! get_indirect_call_dest_reg (old_op, &dest_reg)
2453
0
    || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2454
0
               sbuff, &src_reg) != 0)
2455
0
  {
2456
0
    *error_message = "cannot extract CALLXn operands for TLS access";
2457
0
    return false;
2458
0
  }
2459
0
      break;
2460
2461
0
    default:
2462
0
      abort ();
2463
0
    }
2464
2465
0
  if (is_ld_model)
2466
0
    {
2467
0
      switch (r_type)
2468
0
  {
2469
0
  case R_XTENSA_TLS_FUNC:
2470
0
  case R_XTENSA_TLS_ARG:
2471
    /* Change the instruction to a NOP (or "OR a1, a1, a1" for older
2472
       versions of Xtensa).  */
2473
0
    new_op = xtensa_opcode_lookup (isa, "nop");
2474
0
    if (new_op == XTENSA_UNDEFINED)
2475
0
      {
2476
0
        new_op = xtensa_opcode_lookup (isa, "or");
2477
0
        if (new_op == XTENSA_UNDEFINED
2478
0
      || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2479
0
      || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2480
0
                 sbuff, 1) != 0
2481
0
      || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2482
0
                 sbuff, 1) != 0
2483
0
      || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2484
0
                 sbuff, 1) != 0)
2485
0
    {
2486
0
      *error_message = "cannot encode OR for TLS access";
2487
0
      return false;
2488
0
    }
2489
0
      }
2490
0
    else
2491
0
      {
2492
0
        if (xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0)
2493
0
    {
2494
0
      *error_message = "cannot encode NOP for TLS access";
2495
0
      return false;
2496
0
    }
2497
0
      }
2498
0
    break;
2499
2500
0
  case R_XTENSA_TLS_CALL:
2501
    /* Read THREADPTR into the CALLX's return value register.  */
2502
0
    new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2503
0
    if (new_op == XTENSA_UNDEFINED
2504
0
        || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2505
0
        || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2506
0
             sbuff, dest_reg + 2) != 0)
2507
0
      {
2508
0
        *error_message = "cannot encode RUR.THREADPTR for TLS access";
2509
0
        return false;
2510
0
      }
2511
0
    break;
2512
0
  }
2513
0
    }
2514
0
  else
2515
0
    {
2516
0
      switch (r_type)
2517
0
  {
2518
0
  case R_XTENSA_TLS_FUNC:
2519
0
    new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2520
0
    if (new_op == XTENSA_UNDEFINED
2521
0
        || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2522
0
        || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2523
0
             sbuff, dest_reg) != 0)
2524
0
      {
2525
0
        *error_message = "cannot encode RUR.THREADPTR for TLS access";
2526
0
        return false;
2527
0
      }
2528
0
    break;
2529
2530
0
  case R_XTENSA_TLS_ARG:
2531
    /* Nothing to do.  Keep the original L32R instruction.  */
2532
0
    return true;
2533
2534
0
  case R_XTENSA_TLS_CALL:
2535
    /* Add the CALLX's src register (holding the THREADPTR value)
2536
       to the first argument register (holding the offset) and put
2537
       the result in the CALLX's return value register.  */
2538
0
    new_op = xtensa_opcode_lookup (isa, "add");
2539
0
    if (new_op == XTENSA_UNDEFINED
2540
0
        || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2541
0
        || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2542
0
             sbuff, dest_reg + 2) != 0
2543
0
        || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2544
0
             sbuff, dest_reg + 2) != 0
2545
0
        || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2546
0
             sbuff, src_reg) != 0)
2547
0
      {
2548
0
        *error_message = "cannot encode ADD for TLS access";
2549
0
        return false;
2550
0
      }
2551
0
    break;
2552
0
  }
2553
0
    }
2554
2555
0
  xtensa_format_set_slot (isa, fmt, 0, ibuff, sbuff);
2556
0
  xtensa_insnbuf_to_chars (isa, ibuff, contents + rel->r_offset,
2557
0
         input_size - rel->r_offset);
2558
2559
0
  return true;
2560
0
}
2561
2562
2563
#define IS_XTENSA_TLS_RELOC(R_TYPE) \
2564
0
  ((R_TYPE) == R_XTENSA_TLSDESC_FN \
2565
0
   || (R_TYPE) == R_XTENSA_TLSDESC_ARG \
2566
0
   || (R_TYPE) == R_XTENSA_TLS_DTPOFF \
2567
0
   || (R_TYPE) == R_XTENSA_TLS_TPOFF \
2568
0
   || (R_TYPE) == R_XTENSA_TLS_FUNC \
2569
0
   || (R_TYPE) == R_XTENSA_TLS_ARG \
2570
0
   || (R_TYPE) == R_XTENSA_TLS_CALL)
2571
2572
/* Relocate an Xtensa ELF section.  This is invoked by the linker for
2573
   both relocatable and final links.  */
2574
2575
static int
2576
elf_xtensa_relocate_section (struct bfd_link_info *info,
2577
           bfd *input_bfd,
2578
           asection *input_section,
2579
           bfd_byte *contents,
2580
           Elf_Internal_Rela *relocs,
2581
           Elf_Internal_Sym *local_syms,
2582
           asection **local_sections)
2583
0
{
2584
0
  struct elf_xtensa_link_hash_table *htab;
2585
0
  Elf_Internal_Shdr *symtab_hdr;
2586
0
  Elf_Internal_Rela *rel;
2587
0
  Elf_Internal_Rela *relend;
2588
0
  struct elf_link_hash_entry **sym_hashes;
2589
0
  property_table_entry *lit_table = 0;
2590
0
  int ltblsize = 0;
2591
0
  char *local_got_tls_types;
2592
0
  char *error_message = NULL;
2593
0
  bfd_size_type input_size;
2594
0
  int tls_type;
2595
2596
0
  if (!xtensa_default_isa)
2597
0
    xtensa_default_isa = xtensa_isa_init (0, 0);
2598
2599
0
  if (!is_xtensa_elf (input_bfd))
2600
0
    {
2601
0
      bfd_set_error (bfd_error_wrong_format);
2602
0
      return false;
2603
0
    }
2604
2605
0
  htab = elf_xtensa_hash_table (info);
2606
0
  if (htab == NULL)
2607
0
    return false;
2608
2609
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2610
0
  sym_hashes = elf_sym_hashes (input_bfd);
2611
0
  local_got_tls_types = elf_xtensa_local_got_tls_type (input_bfd);
2612
2613
0
  if (elf_hash_table (info)->dynamic_sections_created)
2614
0
    {
2615
0
      ltblsize = xtensa_read_table_entries (input_bfd, input_section,
2616
0
              &lit_table, XTENSA_LIT_SEC_NAME,
2617
0
              true);
2618
0
      if (ltblsize < 0)
2619
0
  return false;
2620
0
    }
2621
2622
0
  input_size = bfd_get_section_limit (input_bfd, input_section);
2623
2624
0
  rel = relocs;
2625
0
  relend = relocs + input_section->reloc_count;
2626
0
  for (; rel < relend; rel++)
2627
0
    {
2628
0
      int r_type;
2629
0
      reloc_howto_type *howto;
2630
0
      unsigned long r_symndx;
2631
0
      struct elf_link_hash_entry *h;
2632
0
      const Elf_Internal_Sym *sym;
2633
0
      char sym_type;
2634
0
      const char *name;
2635
0
      asection *sec;
2636
0
      bfd_vma relocation;
2637
0
      bfd_reloc_status_type r;
2638
0
      bool is_weak_undef;
2639
0
      bool unresolved_reloc;
2640
0
      bool warned;
2641
0
      bool dynamic_symbol;
2642
2643
0
      r_type = ELF32_R_TYPE (rel->r_info);
2644
0
      if (r_type == (int) R_XTENSA_GNU_VTINHERIT
2645
0
    || r_type == (int) R_XTENSA_GNU_VTENTRY)
2646
0
  continue;
2647
2648
0
      if (r_type < 0 || r_type >= (int) R_XTENSA_max)
2649
0
  {
2650
0
    bfd_set_error (bfd_error_bad_value);
2651
0
    return false;
2652
0
  }
2653
0
      howto = &elf_howto_table[r_type];
2654
2655
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2656
2657
0
      h = NULL;
2658
0
      sym = NULL;
2659
0
      sec = NULL;
2660
0
      is_weak_undef = false;
2661
0
      unresolved_reloc = false;
2662
0
      warned = false;
2663
2664
0
      if (howto->partial_inplace && !bfd_link_relocatable (info))
2665
0
  {
2666
    /* Because R_XTENSA_32 was made partial_inplace to fix some
2667
       problems with DWARF info in partial links, there may be
2668
       an addend stored in the contents.  Take it out of there
2669
       and move it back into the addend field of the reloc.  */
2670
0
    rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
2671
0
    bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
2672
0
  }
2673
2674
0
      if (r_symndx < symtab_hdr->sh_info)
2675
0
  {
2676
0
    sym = local_syms + r_symndx;
2677
0
    sym_type = ELF32_ST_TYPE (sym->st_info);
2678
0
    sec = local_sections[r_symndx];
2679
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
2680
0
            sym, &sec, rel);
2681
0
  }
2682
0
      else
2683
0
  {
2684
0
    bool ignored;
2685
2686
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2687
0
           r_symndx, symtab_hdr, sym_hashes,
2688
0
           h, sec, relocation,
2689
0
           unresolved_reloc, warned, ignored);
2690
2691
0
    if (relocation == 0
2692
0
        && !unresolved_reloc
2693
0
        && h->root.type == bfd_link_hash_undefweak)
2694
0
      is_weak_undef = true;
2695
2696
0
    sym_type = h->type;
2697
0
  }
2698
2699
0
      if (sec != NULL && discarded_section (sec))
2700
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2701
0
           rel, 1, relend, R_XTENSA_NONE,
2702
0
           howto, 0, contents);
2703
2704
0
      if (bfd_link_relocatable (info))
2705
0
  {
2706
0
    bfd_vma dest_addr;
2707
0
    asection * sym_sec = get_elf_r_symndx_section (input_bfd, r_symndx);
2708
2709
    /* This is a relocatable link.
2710
       1) If the reloc is against a section symbol, adjust
2711
       according to the output section.
2712
       2) If there is a new target for this relocation,
2713
       the new target will be in the same output section.
2714
       We adjust the relocation by the output section
2715
       difference.  */
2716
2717
0
    if (relaxing_section)
2718
0
      {
2719
        /* Check if this references a section in another input file.  */
2720
0
        if (!do_fix_for_relocatable_link (rel, input_bfd, input_section,
2721
0
            contents))
2722
0
    return false;
2723
0
      }
2724
2725
0
    dest_addr = sym_sec->output_section->vma + sym_sec->output_offset
2726
0
      + get_elf_r_symndx_offset (input_bfd, r_symndx) + rel->r_addend;
2727
2728
0
    if (r_type == R_XTENSA_ASM_SIMPLIFY)
2729
0
      {
2730
0
        error_message = NULL;
2731
        /* Convert ASM_SIMPLIFY into the simpler relocation
2732
     so that they never escape a relaxing link.  */
2733
0
        r = contract_asm_expansion (contents, input_size, rel,
2734
0
            &error_message);
2735
0
        if (r != bfd_reloc_ok)
2736
0
    (*info->callbacks->reloc_dangerous)
2737
0
      (info, error_message,
2738
0
       input_bfd, input_section, rel->r_offset);
2739
2740
0
        r_type = ELF32_R_TYPE (rel->r_info);
2741
0
      }
2742
2743
    /* This is a relocatable link, so we don't have to change
2744
       anything unless the reloc is against a section symbol,
2745
       in which case we have to adjust according to where the
2746
       section symbol winds up in the output section.  */
2747
0
    if (r_symndx < symtab_hdr->sh_info)
2748
0
      {
2749
0
        sym = local_syms + r_symndx;
2750
0
        if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2751
0
    {
2752
0
      sec = local_sections[r_symndx];
2753
0
      rel->r_addend += sec->output_offset + sym->st_value;
2754
0
    }
2755
0
      }
2756
2757
    /* If there is an addend with a partial_inplace howto,
2758
       then move the addend to the contents.  This is a hack
2759
       to work around problems with DWARF in relocatable links
2760
       with some previous version of BFD.  Now we can't easily get
2761
       rid of the hack without breaking backward compatibility.... */
2762
0
    r = bfd_reloc_ok;
2763
0
    howto = &elf_howto_table[r_type];
2764
0
    if (howto->partial_inplace && rel->r_addend)
2765
0
      {
2766
0
        r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2767
0
               rel->r_addend, contents,
2768
0
               rel->r_offset, false,
2769
0
               &error_message);
2770
0
        rel->r_addend = 0;
2771
0
      }
2772
0
    else
2773
0
      {
2774
        /* Put the correct bits in the target instruction, even
2775
     though the relocation will still be present in the output
2776
     file.  This makes disassembly clearer, as well as
2777
     allowing loadable kernel modules to work without needing
2778
     relocations on anything other than calls and l32r's.  */
2779
2780
        /* If it is not in the same section, there is nothing we can do.  */
2781
0
        if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP &&
2782
0
      sym_sec->output_section == input_section->output_section)
2783
0
    {
2784
0
      r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2785
0
             dest_addr, contents,
2786
0
             rel->r_offset, false,
2787
0
             &error_message);
2788
0
    }
2789
0
      }
2790
0
    if (r != bfd_reloc_ok)
2791
0
      (*info->callbacks->reloc_dangerous)
2792
0
        (info, error_message,
2793
0
         input_bfd, input_section, rel->r_offset);
2794
2795
    /* Done with work for relocatable link; continue with next reloc.  */
2796
0
    continue;
2797
0
  }
2798
2799
      /* This is a final link.  */
2800
2801
0
      if (relaxing_section)
2802
0
  {
2803
    /* Check if this references a section in another input file.  */
2804
0
    do_fix_for_final_link (rel, input_bfd, input_section, contents,
2805
0
         &relocation);
2806
0
  }
2807
2808
      /* Sanity check the address.  */
2809
0
      if (rel->r_offset >= input_size
2810
0
    && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2811
0
  {
2812
0
    _bfd_error_handler
2813
      /* xgettext:c-format */
2814
0
      (_("%pB(%pA+%#" PRIx64 "): "
2815
0
         "relocation offset out of range (size=%#" PRIx64 ")"),
2816
0
       input_bfd, input_section, (uint64_t) rel->r_offset,
2817
0
       (uint64_t) input_size);
2818
0
    bfd_set_error (bfd_error_bad_value);
2819
0
    return false;
2820
0
  }
2821
2822
0
      if (h != NULL)
2823
0
  name = h->root.root.string;
2824
0
      else
2825
0
  {
2826
0
    name = (bfd_elf_string_from_elf_section
2827
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
2828
0
    if (name == NULL || *name == '\0')
2829
0
      name = bfd_section_name (sec);
2830
0
  }
2831
2832
0
      if (r_symndx != STN_UNDEF
2833
0
    && r_type != R_XTENSA_NONE
2834
0
    && (h == NULL
2835
0
        || h->root.type == bfd_link_hash_defined
2836
0
        || h->root.type == bfd_link_hash_defweak)
2837
0
    && IS_XTENSA_TLS_RELOC (r_type) != (sym_type == STT_TLS))
2838
0
  {
2839
0
    _bfd_error_handler
2840
0
      ((sym_type == STT_TLS
2841
        /* xgettext:c-format */
2842
0
        ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
2843
        /* xgettext:c-format */
2844
0
        : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
2845
0
       input_bfd,
2846
0
       input_section,
2847
0
       (uint64_t) rel->r_offset,
2848
0
       howto->name,
2849
0
       name);
2850
0
  }
2851
2852
0
      dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
2853
2854
0
      tls_type = GOT_UNKNOWN;
2855
0
      if (h)
2856
0
  tls_type = elf_xtensa_hash_entry (h)->tls_type;
2857
0
      else if (local_got_tls_types)
2858
0
  tls_type = local_got_tls_types [r_symndx];
2859
2860
0
      switch (r_type)
2861
0
  {
2862
0
  case R_XTENSA_32:
2863
0
  case R_XTENSA_32_ABS:
2864
0
  case R_XTENSA_PLT:
2865
0
    if (elf_hash_table (info)->dynamic_sections_created
2866
0
        && (input_section->flags & SEC_ALLOC) != 0
2867
0
        && (dynamic_symbol || bfd_link_pic (info)))
2868
0
      {
2869
0
        Elf_Internal_Rela outrel;
2870
0
        bfd_byte *loc;
2871
0
        asection *srel;
2872
2873
0
        if (dynamic_symbol && r_type == R_XTENSA_PLT)
2874
0
    srel = htab->elf.srelplt;
2875
0
        else
2876
0
    srel = htab->elf.srelgot;
2877
2878
0
        BFD_ASSERT (srel != NULL);
2879
2880
0
        outrel.r_offset =
2881
0
    _bfd_elf_section_offset (info->output_bfd, info,
2882
0
           input_section, rel->r_offset);
2883
2884
0
        if ((outrel.r_offset | 1) == (bfd_vma) -1)
2885
0
    memset (&outrel, 0, sizeof outrel);
2886
0
        else
2887
0
    {
2888
0
      outrel.r_offset += (input_section->output_section->vma
2889
0
              + input_section->output_offset);
2890
2891
      /* Complain if the relocation is in a read-only section
2892
         and not in a literal pool.  */
2893
0
      if ((input_section->flags & SEC_READONLY) != 0
2894
0
          && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
2895
0
                  outrel.r_offset))
2896
0
        {
2897
0
          error_message =
2898
0
      _("dynamic relocation in read-only section");
2899
0
          (*info->callbacks->reloc_dangerous)
2900
0
      (info, error_message,
2901
0
       input_bfd, input_section, rel->r_offset);
2902
0
        }
2903
2904
0
      if (dynamic_symbol)
2905
0
        {
2906
0
          outrel.r_addend = rel->r_addend;
2907
0
          rel->r_addend = 0;
2908
2909
0
          if (r_type == R_XTENSA_32 || r_type == R_XTENSA_32_ABS)
2910
0
      {
2911
0
        outrel.r_info =
2912
0
          ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2913
0
        relocation = 0;
2914
0
      }
2915
0
          else /* r_type == R_XTENSA_PLT */
2916
0
      {
2917
0
        outrel.r_info =
2918
0
          ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2919
2920
        /* Create the PLT entry and set the initial
2921
           contents of the literal entry to the address of
2922
           the PLT entry.  */
2923
0
        relocation =
2924
0
          elf_xtensa_create_plt_entry (info, info->output_bfd,
2925
0
               srel->reloc_count);
2926
0
      }
2927
0
          unresolved_reloc = false;
2928
0
        }
2929
0
      else if (!is_weak_undef)
2930
0
        {
2931
          /* Generate a RELATIVE relocation.  */
2932
0
          outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2933
0
          outrel.r_addend = 0;
2934
0
        }
2935
0
      else
2936
0
        {
2937
0
          continue;
2938
0
        }
2939
0
    }
2940
2941
0
        loc = (srel->contents
2942
0
         + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2943
0
        bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
2944
0
        BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2945
0
        <= srel->size);
2946
0
      }
2947
0
    else if (r_type == R_XTENSA_ASM_EXPAND && dynamic_symbol)
2948
0
      {
2949
        /* This should only happen for non-PIC code, which is not
2950
     supposed to be used on systems with dynamic linking.
2951
     Just ignore these relocations.  */
2952
0
        continue;
2953
0
      }
2954
0
    break;
2955
2956
0
  case R_XTENSA_TLS_TPOFF:
2957
    /* Switch to LE model for local symbols in an executable.  */
2958
0
    if (! bfd_link_dll (info) && ! dynamic_symbol)
2959
0
      {
2960
0
        relocation = tpoff (info, relocation);
2961
0
        break;
2962
0
      }
2963
    /* fall through */
2964
2965
0
  case R_XTENSA_TLSDESC_FN:
2966
0
  case R_XTENSA_TLSDESC_ARG:
2967
0
    {
2968
0
      if (r_type == R_XTENSA_TLSDESC_FN)
2969
0
        {
2970
0
    if (! bfd_link_dll (info) || (tls_type & GOT_TLS_IE) != 0)
2971
0
      r_type = R_XTENSA_NONE;
2972
0
        }
2973
0
      else if (r_type == R_XTENSA_TLSDESC_ARG)
2974
0
        {
2975
0
    if (bfd_link_dll (info))
2976
0
      {
2977
0
        if ((tls_type & GOT_TLS_IE) != 0)
2978
0
          r_type = R_XTENSA_TLS_TPOFF;
2979
0
      }
2980
0
    else
2981
0
      {
2982
0
        r_type = R_XTENSA_TLS_TPOFF;
2983
0
        if (! dynamic_symbol)
2984
0
          {
2985
0
      relocation = tpoff (info, relocation);
2986
0
      break;
2987
0
          }
2988
0
      }
2989
0
        }
2990
2991
0
      if (r_type == R_XTENSA_NONE)
2992
        /* Nothing to do here; skip to the next reloc.  */
2993
0
        continue;
2994
2995
0
      if (! elf_hash_table (info)->dynamic_sections_created)
2996
0
        {
2997
0
    error_message =
2998
0
      _("TLS relocation invalid without dynamic sections");
2999
0
    (*info->callbacks->reloc_dangerous)
3000
0
      (info, error_message,
3001
0
       input_bfd, input_section, rel->r_offset);
3002
0
        }
3003
0
      else
3004
0
        {
3005
0
    Elf_Internal_Rela outrel;
3006
0
    bfd_byte *loc;
3007
0
    asection *srel = htab->elf.srelgot;
3008
0
    int indx;
3009
3010
0
    outrel.r_offset = (input_section->output_section->vma
3011
0
           + input_section->output_offset
3012
0
           + rel->r_offset);
3013
3014
    /* Complain if the relocation is in a read-only section
3015
       and not in a literal pool.  */
3016
0
    if ((input_section->flags & SEC_READONLY) != 0
3017
0
        && ! elf_xtensa_in_literal_pool (lit_table, ltblsize,
3018
0
                 outrel.r_offset))
3019
0
      {
3020
0
        error_message =
3021
0
          _("dynamic relocation in read-only section");
3022
0
        (*info->callbacks->reloc_dangerous)
3023
0
          (info, error_message,
3024
0
           input_bfd, input_section, rel->r_offset);
3025
0
      }
3026
3027
0
    indx = h && h->dynindx != -1 ? h->dynindx : 0;
3028
0
    if (indx == 0)
3029
0
      outrel.r_addend = relocation - dtpoff_base (info);
3030
0
    else
3031
0
      outrel.r_addend = 0;
3032
0
    rel->r_addend = 0;
3033
3034
0
    outrel.r_info = ELF32_R_INFO (indx, r_type);
3035
0
    relocation = 0;
3036
0
    unresolved_reloc = false;
3037
3038
0
    BFD_ASSERT (srel);
3039
0
    loc = (srel->contents
3040
0
           + srel->reloc_count++ * sizeof (Elf32_External_Rela));
3041
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
3042
0
    BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
3043
0
          <= srel->size);
3044
0
        }
3045
0
    }
3046
0
    break;
3047
3048
0
  case R_XTENSA_TLS_DTPOFF:
3049
0
    if (! bfd_link_dll (info))
3050
      /* Switch from LD model to LE model.  */
3051
0
      relocation = tpoff (info, relocation);
3052
0
    else
3053
0
      relocation -= dtpoff_base (info);
3054
0
    break;
3055
3056
0
  case R_XTENSA_TLS_FUNC:
3057
0
  case R_XTENSA_TLS_ARG:
3058
0
  case R_XTENSA_TLS_CALL:
3059
    /* Check if optimizing to IE or LE model.  */
3060
0
    if ((tls_type & GOT_TLS_IE) != 0)
3061
0
      {
3062
0
        bool is_ld_model =
3063
0
    (h && elf_xtensa_hash_entry (h) == htab->tlsbase);
3064
0
        if (! replace_tls_insn (rel, input_bfd, input_section, contents,
3065
0
              is_ld_model, &error_message))
3066
0
    (*info->callbacks->reloc_dangerous)
3067
0
      (info, error_message,
3068
0
       input_bfd, input_section, rel->r_offset);
3069
3070
0
        if (r_type != R_XTENSA_TLS_ARG || is_ld_model)
3071
0
    {
3072
      /* Skip subsequent relocations on the same instruction.  */
3073
0
      while (rel + 1 < relend && rel[1].r_offset == rel->r_offset)
3074
0
        rel++;
3075
0
    }
3076
0
      }
3077
0
    continue;
3078
3079
0
  default:
3080
0
    if (elf_hash_table (info)->dynamic_sections_created
3081
0
        && dynamic_symbol && (is_operand_relocation (r_type)
3082
0
            || r_type == R_XTENSA_32_PCREL))
3083
0
      {
3084
0
        error_message =
3085
0
    vsprint_msg ("invalid relocation for dynamic symbol", ": %s",
3086
0
           strlen (name) + 2, name);
3087
0
        (*info->callbacks->reloc_dangerous)
3088
0
    (info, error_message, input_bfd, input_section, rel->r_offset);
3089
0
        continue;
3090
0
      }
3091
0
    break;
3092
0
  }
3093
3094
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3095
   because such sections are not SEC_ALLOC and thus ld.so will
3096
   not process them.  */
3097
0
      if (unresolved_reloc
3098
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
3099
0
         && h->def_dynamic)
3100
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
3101
0
              rel->r_offset) != (bfd_vma) -1)
3102
0
  {
3103
0
    _bfd_error_handler
3104
      /* xgettext:c-format */
3105
0
      (_("%pB(%pA+%#" PRIx64 "): "
3106
0
         "unresolvable %s relocation against symbol `%s'"),
3107
0
       input_bfd,
3108
0
       input_section,
3109
0
       (uint64_t) rel->r_offset,
3110
0
       howto->name,
3111
0
       name);
3112
0
    return false;
3113
0
  }
3114
3115
      /* TLS optimizations may have changed r_type; update "howto".  */
3116
0
      howto = &elf_howto_table[r_type];
3117
3118
      /* There's no point in calling bfd_perform_relocation here.
3119
   Just go directly to our "special function".  */
3120
0
      r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
3121
0
             relocation + rel->r_addend,
3122
0
             contents, rel->r_offset, is_weak_undef,
3123
0
             &error_message);
3124
3125
0
      if (r != bfd_reloc_ok && !warned)
3126
0
  {
3127
0
    BFD_ASSERT (r == bfd_reloc_dangerous || r == bfd_reloc_other);
3128
0
    BFD_ASSERT (error_message != NULL);
3129
3130
0
    if (rel->r_addend == 0)
3131
0
      error_message = vsprint_msg (error_message, ": %s",
3132
0
           strlen (name) + 2, name);
3133
0
    else
3134
0
      error_message = vsprint_msg (error_message, ": (%s+0x%x)",
3135
0
           strlen (name) + 22,
3136
0
           name, (int) rel->r_addend);
3137
3138
0
    (*info->callbacks->reloc_dangerous)
3139
0
      (info, error_message, input_bfd, input_section, rel->r_offset);
3140
0
  }
3141
0
    }
3142
3143
0
  free (lit_table);
3144
0
  input_section->reloc_done = true;
3145
3146
0
  return true;
3147
0
}
3148
3149
3150
/* Finish up dynamic symbol handling.  There's not much to do here since
3151
   the PLT and GOT entries are all set up by relocate_section.  */
3152
3153
static bool
3154
elf_xtensa_finish_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3155
          struct elf_link_hash_entry *h,
3156
          Elf_Internal_Sym *sym)
3157
0
{
3158
0
  if (h->needs_plt && !h->def_regular)
3159
0
    {
3160
      /* Mark the symbol as undefined, rather than as defined in
3161
   the .plt section.  Leave the value alone.  */
3162
0
      sym->st_shndx = SHN_UNDEF;
3163
      /* If the symbol is weak, we do need to clear the value.
3164
   Otherwise, the PLT entry would provide a definition for
3165
   the symbol even if the symbol wasn't defined anywhere,
3166
   and so the symbol would never be NULL.  */
3167
0
      if (!h->ref_regular_nonweak)
3168
0
  sym->st_value = 0;
3169
0
    }
3170
3171
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3172
0
  if (h == elf_hash_table (info)->hdynamic
3173
0
      || h == elf_hash_table (info)->hgot)
3174
0
    sym->st_shndx = SHN_ABS;
3175
3176
0
  return true;
3177
0
}
3178
3179
3180
/* Combine adjacent literal table entries in the output.  Adjacent
3181
   entries within each input section may have been removed during
3182
   relaxation, but we repeat the process here, even though it's too late
3183
   to shrink the output section, because it's important to minimize the
3184
   number of literal table entries to reduce the start-up work for the
3185
   runtime linker.  Returns the number of remaining table entries or -1
3186
   on error.  */
3187
3188
static int
3189
elf_xtensa_combine_prop_entries (bfd *output_bfd,
3190
         asection *sxtlit,
3191
         asection *sgotloc)
3192
0
{
3193
0
  bfd_byte *contents;
3194
0
  property_table_entry *table;
3195
0
  bfd_size_type section_size, sgotloc_size;
3196
0
  bfd_vma offset;
3197
0
  int n, m, num;
3198
3199
0
  section_size = sxtlit->size;
3200
0
  if (section_size == 0)
3201
0
    return 0;
3202
3203
0
  BFD_ASSERT (section_size % 8 == 0);
3204
0
  num = section_size / 8;
3205
3206
0
  sgotloc_size = sgotloc->size;
3207
0
  if (sgotloc_size != section_size)
3208
0
    {
3209
0
      _bfd_error_handler
3210
0
  (_("internal inconsistency in size of .got.loc section"));
3211
0
      return -1;
3212
0
    }
3213
3214
0
  table = bfd_malloc (num * sizeof (property_table_entry));
3215
0
  if (table == 0)
3216
0
    return -1;
3217
3218
  /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
3219
     propagates to the output section, where it doesn't really apply and
3220
     where it breaks the following call to bfd_malloc_and_get_section.  */
3221
0
  sxtlit->flags &= ~SEC_IN_MEMORY;
3222
3223
0
  if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
3224
0
    {
3225
0
      free (contents);
3226
0
      free (table);
3227
0
      return -1;
3228
0
    }
3229
3230
  /* There should never be any relocations left at this point, so this
3231
     is quite a bit easier than what is done during relaxation.  */
3232
3233
  /* Copy the raw contents into a property table array and sort it.  */
3234
0
  offset = 0;
3235
0
  for (n = 0; n < num; n++)
3236
0
    {
3237
0
      table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
3238
0
      table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
3239
0
      offset += 8;
3240
0
    }
3241
0
  qsort (table, num, sizeof (property_table_entry), property_table_compare);
3242
3243
0
  for (n = 0; n < num; n++)
3244
0
    {
3245
0
      bool remove_entry = false;
3246
3247
0
      if (table[n].size == 0)
3248
0
  remove_entry = true;
3249
0
      else if (n > 0
3250
0
         && (table[n-1].address + table[n-1].size == table[n].address))
3251
0
  {
3252
0
    table[n-1].size += table[n].size;
3253
0
    remove_entry = true;
3254
0
  }
3255
3256
0
      if (remove_entry)
3257
0
  {
3258
0
    for (m = n; m < num - 1; m++)
3259
0
      {
3260
0
        table[m].address = table[m+1].address;
3261
0
        table[m].size = table[m+1].size;
3262
0
      }
3263
3264
0
    n--;
3265
0
    num--;
3266
0
  }
3267
0
    }
3268
3269
  /* Copy the data back to the raw contents.  */
3270
0
  offset = 0;
3271
0
  for (n = 0; n < num; n++)
3272
0
    {
3273
0
      bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
3274
0
      bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
3275
0
      offset += 8;
3276
0
    }
3277
3278
  /* Clear the removed bytes.  */
3279
0
  if ((bfd_size_type) (num * 8) < section_size)
3280
0
    memset (&contents[num * 8], 0, section_size - num * 8);
3281
3282
0
  if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
3283
0
          section_size))
3284
0
    return -1;
3285
3286
  /* Copy the contents to ".got.loc".  */
3287
0
  memcpy (sgotloc->contents, contents, section_size);
3288
3289
0
  free (contents);
3290
0
  free (table);
3291
0
  return num;
3292
0
}
3293
3294
3295
/* Finish up the dynamic sections.  */
3296
3297
static bool
3298
elf_xtensa_finish_dynamic_sections (struct bfd_link_info *info,
3299
            bfd_byte *buf ATTRIBUTE_UNUSED)
3300
0
{
3301
0
  struct elf_xtensa_link_hash_table *htab;
3302
0
  bfd *dynobj;
3303
0
  asection *sdyn, *srelplt, *srelgot, *sgot, *sxtlit, *sgotloc;
3304
0
  Elf32_External_Dyn *dyncon, *dynconend;
3305
0
  int num_xtlit_entries = 0;
3306
3307
0
  if (! elf_hash_table (info)->dynamic_sections_created)
3308
0
    return true;
3309
3310
0
  htab = elf_xtensa_hash_table (info);
3311
0
  if (htab == NULL)
3312
0
    return false;
3313
3314
0
  dynobj = elf_hash_table (info)->dynobj;
3315
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3316
0
  BFD_ASSERT (sdyn != NULL);
3317
3318
  /* Set the first entry in the global offset table to the address of
3319
     the dynamic section.  */
3320
0
  sgot = htab->elf.sgot;
3321
0
  if (sgot)
3322
0
    {
3323
0
      BFD_ASSERT (sgot->size == 4);
3324
0
      if (sdyn == NULL)
3325
0
  bfd_put_32 (info->output_bfd, 0, sgot->contents);
3326
0
      else
3327
0
  bfd_put_32 (info->output_bfd,
3328
0
        sdyn->output_section->vma + sdyn->output_offset,
3329
0
        sgot->contents);
3330
0
    }
3331
3332
0
  srelplt = htab->elf.srelplt;
3333
0
  srelgot = htab->elf.srelgot;
3334
0
  if (srelplt && srelplt->size != 0)
3335
0
    {
3336
0
      asection *sgotplt, *spltlittbl;
3337
0
      int chunk, plt_chunks, plt_entries;
3338
0
      Elf_Internal_Rela irela;
3339
0
      bfd_byte *loc;
3340
0
      unsigned rtld_reloc;
3341
3342
0
      spltlittbl = htab->spltlittbl;
3343
0
      BFD_ASSERT (srelgot != NULL && spltlittbl != NULL);
3344
3345
      /* Find the first XTENSA_RTLD relocation.  Presumably the rest
3346
   of them follow immediately after....  */
3347
0
      for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
3348
0
  {
3349
0
    loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3350
0
    bfd_elf32_swap_reloca_in (info->output_bfd, loc, &irela);
3351
0
    if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
3352
0
      break;
3353
0
  }
3354
0
      BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
3355
3356
0
      plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
3357
0
      plt_chunks =
3358
0
  (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
3359
3360
0
      for (chunk = 0; chunk < plt_chunks; chunk++)
3361
0
  {
3362
0
    int chunk_entries = 0;
3363
3364
0
    sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
3365
0
    BFD_ASSERT (sgotplt != NULL);
3366
3367
    /* Emit special RTLD relocations for the first two entries in
3368
       each chunk of the .got.plt section.  */
3369
3370
0
    loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3371
0
    bfd_elf32_swap_reloca_in (info->output_bfd, loc, &irela);
3372
0
    BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3373
0
    irela.r_offset = (sgotplt->output_section->vma
3374
0
          + sgotplt->output_offset);
3375
0
    irela.r_addend = 1; /* tell rtld to set value to resolver function */
3376
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &irela, loc);
3377
0
    rtld_reloc += 1;
3378
0
    BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3379
3380
    /* Next literal immediately follows the first.  */
3381
0
    loc += sizeof (Elf32_External_Rela);
3382
0
    bfd_elf32_swap_reloca_in (info->output_bfd, loc, &irela);
3383
0
    BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3384
0
    irela.r_offset = (sgotplt->output_section->vma
3385
0
          + sgotplt->output_offset + 4);
3386
    /* Tell rtld to set value to object's link map.  */
3387
0
    irela.r_addend = 2;
3388
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &irela, loc);
3389
0
    rtld_reloc += 1;
3390
0
    BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3391
3392
    /* Fill in the literal table.  */
3393
0
    if (chunk < plt_chunks - 1)
3394
0
      chunk_entries = PLT_ENTRIES_PER_CHUNK;
3395
0
    else
3396
0
      chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
3397
3398
0
    BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->size);
3399
0
    bfd_put_32 (info->output_bfd,
3400
0
          sgotplt->output_section->vma + sgotplt->output_offset,
3401
0
          spltlittbl->contents + (chunk * 8) + 0);
3402
0
    bfd_put_32 (info->output_bfd,
3403
0
          8 + (chunk_entries * 4),
3404
0
          spltlittbl->contents + (chunk * 8) + 4);
3405
0
  }
3406
3407
     /* The .xt.lit.plt section has just been modified.  This must
3408
  happen before the code below which combines adjacent literal
3409
  table entries, and the .xt.lit.plt contents have to be forced to
3410
  the output here.  */
3411
0
      if (! bfd_set_section_contents (info->output_bfd,
3412
0
              spltlittbl->output_section,
3413
0
              spltlittbl->contents,
3414
0
              spltlittbl->output_offset,
3415
0
              spltlittbl->size))
3416
0
  return false;
3417
      /* Clear SEC_HAS_CONTENTS so the contents won't be output again.  */
3418
0
      spltlittbl->flags &= ~SEC_HAS_CONTENTS;
3419
0
    }
3420
3421
  /* All the dynamic relocations have been emitted at this point.
3422
     Make sure the relocation sections are the correct size.  */
3423
0
  if ((srelgot && srelgot->size != (sizeof (Elf32_External_Rela)
3424
0
            * srelgot->reloc_count))
3425
0
      || (srelplt && srelplt->size != (sizeof (Elf32_External_Rela)
3426
0
               * srelplt->reloc_count)))
3427
0
    abort ();
3428
3429
  /* Combine adjacent literal table entries.  */
3430
0
  BFD_ASSERT (! bfd_link_relocatable (info));
3431
0
  sxtlit = bfd_get_section_by_name (info->output_bfd, ".xt.lit");
3432
0
  sgotloc = htab->sgotloc;
3433
0
  BFD_ASSERT (sgotloc);
3434
0
  if (sxtlit)
3435
0
    {
3436
0
      num_xtlit_entries =
3437
0
  elf_xtensa_combine_prop_entries (info->output_bfd, sxtlit, sgotloc);
3438
0
      if (num_xtlit_entries < 0)
3439
0
  return false;
3440
0
    }
3441
3442
0
  dyncon = (Elf32_External_Dyn *) sdyn->contents;
3443
0
  dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3444
0
  for (; dyncon < dynconend; dyncon++)
3445
0
    {
3446
0
      Elf_Internal_Dyn dyn;
3447
3448
0
      bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3449
3450
0
      switch (dyn.d_tag)
3451
0
  {
3452
0
  default:
3453
0
    break;
3454
3455
0
  case DT_XTENSA_GOT_LOC_SZ:
3456
0
    dyn.d_un.d_val = num_xtlit_entries;
3457
0
    break;
3458
3459
0
  case DT_XTENSA_GOT_LOC_OFF:
3460
0
    dyn.d_un.d_ptr = (htab->sgotloc->output_section->vma
3461
0
          + htab->sgotloc->output_offset);
3462
0
    break;
3463
3464
0
  case DT_PLTGOT:
3465
0
    dyn.d_un.d_ptr = (htab->elf.sgot->output_section->vma
3466
0
          + htab->elf.sgot->output_offset);
3467
0
    break;
3468
3469
0
  case DT_JMPREL:
3470
0
    dyn.d_un.d_ptr = (htab->elf.srelplt->output_section->vma
3471
0
          + htab->elf.srelplt->output_offset);
3472
0
    break;
3473
3474
0
  case DT_PLTRELSZ:
3475
0
    dyn.d_un.d_val = htab->elf.srelplt->size;
3476
0
    break;
3477
0
  }
3478
3479
0
      bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
3480
0
    }
3481
3482
0
  return true;
3483
0
}
3484
3485

3486
/* Functions for dealing with the e_flags field.  */
3487
3488
/* Merge backend specific data from an object file to the output
3489
   object file when linking.  */
3490
3491
static bool
3492
elf_xtensa_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3493
0
{
3494
0
  bfd *obfd = info->output_bfd;
3495
0
  unsigned out_mach, in_mach;
3496
0
  flagword out_flag, in_flag;
3497
3498
  /* Check if we have the same endianness.  */
3499
0
  if (!_bfd_generic_verify_endian_match (ibfd, info))
3500
0
    return false;
3501
3502
  /* Don't even pretend to support mixed-format linking.  */
3503
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3504
0
    return false;
3505
3506
0
  out_flag = elf_elfheader (obfd)->e_flags;
3507
0
  in_flag = elf_elfheader (ibfd)->e_flags;
3508
3509
0
  out_mach = out_flag & EF_XTENSA_MACH;
3510
0
  in_mach = in_flag & EF_XTENSA_MACH;
3511
0
  if (out_mach != in_mach)
3512
0
    {
3513
0
      _bfd_error_handler
3514
  /* xgettext:c-format */
3515
0
  (_("%pB: incompatible machine type; output is 0x%x; input is 0x%x"),
3516
0
   ibfd, out_mach, in_mach);
3517
0
      bfd_set_error (bfd_error_wrong_format);
3518
0
      return false;
3519
0
    }
3520
3521
0
  if (! elf_flags_init (obfd))
3522
0
    {
3523
0
      elf_flags_init (obfd) = true;
3524
0
      elf_elfheader (obfd)->e_flags = in_flag;
3525
3526
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3527
0
    && bfd_get_arch_info (obfd)->the_default)
3528
0
  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3529
0
          bfd_get_mach (ibfd));
3530
3531
0
      return true;
3532
0
    }
3533
3534
0
  if ((out_flag & EF_XTENSA_XT_INSN) != (in_flag & EF_XTENSA_XT_INSN))
3535
0
    elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
3536
3537
0
  if ((out_flag & EF_XTENSA_XT_LIT) != (in_flag & EF_XTENSA_XT_LIT))
3538
0
    elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
3539
3540
0
  return true;
3541
0
}
3542
3543
3544
static bool
3545
elf_xtensa_set_private_flags (bfd *abfd, flagword flags)
3546
0
{
3547
0
  BFD_ASSERT (!elf_flags_init (abfd)
3548
0
        || elf_elfheader (abfd)->e_flags == flags);
3549
3550
0
  elf_elfheader (abfd)->e_flags |= flags;
3551
0
  elf_flags_init (abfd) = true;
3552
3553
0
  return true;
3554
0
}
3555
3556
3557
static bool
3558
elf_xtensa_print_private_bfd_data (bfd *abfd, void *farg)
3559
1.64k
{
3560
1.64k
  FILE *f = (FILE *) farg;
3561
1.64k
  flagword e_flags = elf_elfheader (abfd)->e_flags;
3562
3563
1.64k
  fprintf (f, "\nXtensa header:\n");
3564
1.64k
  if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
3565
1.64k
    fprintf (f, "\nMachine     = Base\n");
3566
0
  else
3567
0
    fprintf (f, "\nMachine Id  = 0x%x\n", e_flags & EF_XTENSA_MACH);
3568
3569
1.64k
  fprintf (f, "Insn tables = %s\n",
3570
1.64k
     (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
3571
3572
1.64k
  fprintf (f, "Literal tables = %s\n",
3573
1.64k
     (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
3574
3575
1.64k
  return _bfd_elf_print_private_bfd_data (abfd, farg);
3576
1.64k
}
3577
3578
3579
/* Set the right machine number for an Xtensa ELF file.  */
3580
3581
static bool
3582
elf_xtensa_object_p (bfd *abfd)
3583
4.94k
{
3584
4.94k
  int mach;
3585
4.94k
  unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
3586
3587
4.94k
  switch (arch)
3588
4.94k
    {
3589
4.93k
    case E_XTENSA_MACH:
3590
4.93k
      mach = bfd_mach_xtensa;
3591
4.93k
      break;
3592
10
    default:
3593
10
      return false;
3594
4.94k
    }
3595
3596
4.93k
  (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
3597
4.93k
  return true;
3598
4.94k
}
3599
3600
3601
/* The final processing done just before writing out an Xtensa ELF object
3602
   file.  This gets the Xtensa architecture right based on the machine
3603
   number.  */
3604
3605
static bool
3606
elf_xtensa_final_write_processing (bfd *abfd)
3607
5
{
3608
5
  int mach;
3609
5
  unsigned long val = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
3610
3611
5
  switch (mach = bfd_get_mach (abfd))
3612
5
    {
3613
5
    case bfd_mach_xtensa:
3614
5
      val = E_XTENSA_MACH;
3615
5
      break;
3616
0
    default:
3617
0
      break;
3618
5
    }
3619
3620
5
  elf_elfheader (abfd)->e_flags &= ~EF_XTENSA_MACH;
3621
5
  elf_elfheader (abfd)->e_flags |= val;
3622
5
  return _bfd_elf_final_write_processing (abfd);
3623
5
}
3624
3625
3626
static enum elf_reloc_type_class
3627
elf_xtensa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3628
           const asection *rel_sec ATTRIBUTE_UNUSED,
3629
           const Elf_Internal_Rela *rela)
3630
0
{
3631
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
3632
0
    {
3633
0
    case R_XTENSA_RELATIVE:
3634
0
      return reloc_class_relative;
3635
0
    case R_XTENSA_JMP_SLOT:
3636
0
      return reloc_class_plt;
3637
0
    default:
3638
0
      return reloc_class_normal;
3639
0
    }
3640
0
}
3641
3642

3643
static bool
3644
elf_xtensa_discard_info_for_section (bfd *abfd,
3645
             struct elf_reloc_cookie *cookie,
3646
             struct bfd_link_info *info,
3647
             asection *sec)
3648
0
{
3649
0
  bfd_byte *contents;
3650
0
  bfd_vma offset, actual_offset;
3651
0
  bfd_size_type removed_bytes = 0;
3652
0
  bfd_size_type entry_size;
3653
3654
0
  if (sec->output_section
3655
0
      && bfd_is_abs_section (sec->output_section))
3656
0
    return false;
3657
3658
0
  if (xtensa_is_proptable_section (sec))
3659
0
    entry_size = 12;
3660
0
  else
3661
0
    entry_size = 8;
3662
3663
0
  if (sec->size == 0 || sec->size % entry_size != 0)
3664
0
    return false;
3665
3666
0
  contents = retrieve_contents (abfd, sec, info->keep_memory);
3667
0
  if (!contents)
3668
0
    return false;
3669
3670
0
  cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
3671
0
  if (!cookie->rels)
3672
0
    {
3673
0
      release_contents (sec, contents);
3674
0
      return false;
3675
0
    }
3676
3677
  /* Sort the relocations.  They should already be in order when
3678
     relaxation is enabled, but it might not be.  */
3679
0
  qsort (cookie->rels, sec->reloc_count, sizeof (Elf_Internal_Rela),
3680
0
   internal_reloc_compare);
3681
3682
0
  cookie->rel = cookie->rels;
3683
0
  cookie->relend = cookie->rels + sec->reloc_count;
3684
3685
0
  for (offset = 0; offset < sec->size; offset += entry_size)
3686
0
    {
3687
0
      actual_offset = offset - removed_bytes;
3688
3689
      /* The ...symbol_deleted_p function will skip over relocs but it
3690
   won't adjust their offsets, so do that here.  */
3691
0
      while (cookie->rel < cookie->relend
3692
0
       && cookie->rel->r_offset < offset)
3693
0
  {
3694
0
    cookie->rel->r_offset -= removed_bytes;
3695
0
    cookie->rel++;
3696
0
  }
3697
3698
0
      while (cookie->rel < cookie->relend
3699
0
       && cookie->rel->r_offset == offset)
3700
0
  {
3701
0
    if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
3702
0
      {
3703
        /* Remove the table entry.  (If the reloc type is NONE, then
3704
     the entry has already been merged with another and deleted
3705
     during relaxation.)  */
3706
0
        if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
3707
0
    {
3708
      /* Shift the contents up.  */
3709
0
      if (offset + entry_size < sec->size)
3710
0
        memmove (&contents[actual_offset],
3711
0
           &contents[actual_offset + entry_size],
3712
0
           sec->size - offset - entry_size);
3713
0
      removed_bytes += entry_size;
3714
0
    }
3715
3716
        /* Remove this relocation.  */
3717
0
        cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
3718
0
      }
3719
3720
    /* Adjust the relocation offset for previous removals.  This
3721
       should not be done before calling ...symbol_deleted_p
3722
       because it might mess up the offset comparisons there.
3723
       Make sure the offset doesn't underflow in the case where
3724
       the first entry is removed.  */
3725
0
    if (cookie->rel->r_offset >= removed_bytes)
3726
0
      cookie->rel->r_offset -= removed_bytes;
3727
0
    else
3728
0
      cookie->rel->r_offset = 0;
3729
3730
0
    cookie->rel++;
3731
0
  }
3732
0
    }
3733
3734
0
  if (removed_bytes != 0)
3735
0
    {
3736
      /* Adjust any remaining relocs (shouldn't be any).  */
3737
0
      for (; cookie->rel < cookie->relend; cookie->rel++)
3738
0
  {
3739
0
    if (cookie->rel->r_offset >= removed_bytes)
3740
0
      cookie->rel->r_offset -= removed_bytes;
3741
0
    else
3742
0
      cookie->rel->r_offset = 0;
3743
0
  }
3744
3745
      /* Clear the removed bytes.  */
3746
0
      memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
3747
3748
0
      pin_contents (sec, contents);
3749
0
      pin_internal_relocs (sec, cookie->rels);
3750
3751
      /* Shrink size.  */
3752
0
      if (sec->rawsize == 0)
3753
0
  sec->rawsize = sec->size;
3754
0
      sec->size -= removed_bytes;
3755
3756
0
      if (xtensa_is_littable_section (sec))
3757
0
  {
3758
0
    asection *sgotloc = elf_xtensa_hash_table (info)->sgotloc;
3759
0
    if (sgotloc)
3760
0
      sgotloc->size -= removed_bytes;
3761
0
  }
3762
0
    }
3763
0
  else
3764
0
    {
3765
0
      release_contents (sec, contents);
3766
0
      release_internal_relocs (sec, cookie->rels);
3767
0
    }
3768
3769
0
  return (removed_bytes != 0);
3770
0
}
3771
3772
3773
static bool
3774
elf_xtensa_discard_info (bfd *abfd,
3775
       struct elf_reloc_cookie *cookie,
3776
       struct bfd_link_info *info)
3777
0
{
3778
0
  asection *sec;
3779
0
  bool changed = false;
3780
3781
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
3782
0
    {
3783
0
      if (xtensa_is_property_section (sec))
3784
0
  {
3785
0
    if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
3786
0
      changed = true;
3787
0
  }
3788
0
    }
3789
3790
0
  return changed;
3791
0
}
3792
3793
3794
static bool
3795
elf_xtensa_ignore_discarded_relocs (asection *sec)
3796
0
{
3797
0
  return xtensa_is_property_section (sec);
3798
0
}
3799
3800
3801
static unsigned int
3802
elf_xtensa_action_discarded (asection *sec)
3803
0
{
3804
0
  if (strcmp (".xt_except_table", sec->name) == 0)
3805
0
    return 0;
3806
3807
0
  if (strcmp (".xt_except_desc", sec->name) == 0)
3808
0
    return 0;
3809
3810
0
  return _bfd_elf_default_action_discarded (sec);
3811
0
}
3812
3813

3814
/* Support for core dump NOTE sections.  */
3815
3816
static bool
3817
elf_xtensa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3818
3
{
3819
3
  int offset;
3820
3
  unsigned int size;
3821
3822
3
  if (elf_tdata (abfd) == NULL
3823
3
      || elf_tdata (abfd)->core == NULL)
3824
0
    return false;
3825
3826
  /* The size for Xtensa is variable, so don't try to recognize the format
3827
     based on the size.  Just assume this is GNU/Linux.  */
3828
3
  if (note == NULL || note->descsz < 28)
3829
3
    return false;
3830
3831
  /* pr_cursig */
3832
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3833
3834
  /* pr_pid */
3835
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
3836
3837
  /* pr_reg */
3838
0
  offset = 72;
3839
0
  size = note->descsz - offset - 4;
3840
3841
  /* Make a ".reg/999" section.  */
3842
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3843
0
            size, note->descpos + offset);
3844
3
}
3845
3846
static bool
3847
elf_xtensa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3848
2
{
3849
2
  switch (note->descsz)
3850
2
    {
3851
2
      default:
3852
2
  return false;
3853
3854
0
      case 128:   /* GNU/Linux elf_prpsinfo */
3855
0
  elf_tdata (abfd)->core->program
3856
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3857
0
  elf_tdata (abfd)->core->command
3858
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3859
2
    }
3860
3861
  /* Note that for some reason, a spurious space is tacked
3862
     onto the end of the args in some (at least one anyway)
3863
     implementations, so strip it off if it exists.  */
3864
3865
0
  {
3866
0
    char *command = elf_tdata (abfd)->core->command;
3867
0
    int n = strlen (command);
3868
3869
0
    if (0 < n && command[n - 1] == ' ')
3870
0
      command[n - 1] = '\0';
3871
0
  }
3872
3873
0
  return true;
3874
2
}
3875
3876

3877
/* Generic Xtensa configurability stuff.  */
3878
3879
static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
3880
static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
3881
static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
3882
static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
3883
static xtensa_opcode call0_op = XTENSA_UNDEFINED;
3884
static xtensa_opcode call4_op = XTENSA_UNDEFINED;
3885
static xtensa_opcode call8_op = XTENSA_UNDEFINED;
3886
static xtensa_opcode call12_op = XTENSA_UNDEFINED;
3887
3888
static void
3889
init_call_opcodes (void)
3890
691
{
3891
691
  if (callx0_op == XTENSA_UNDEFINED)
3892
1
    {
3893
1
      callx0_op  = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
3894
1
      callx4_op  = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
3895
1
      callx8_op  = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
3896
1
      callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
3897
1
      call0_op   = xtensa_opcode_lookup (xtensa_default_isa, "call0");
3898
1
      call4_op   = xtensa_opcode_lookup (xtensa_default_isa, "call4");
3899
1
      call8_op   = xtensa_opcode_lookup (xtensa_default_isa, "call8");
3900
1
      call12_op  = xtensa_opcode_lookup (xtensa_default_isa, "call12");
3901
1
    }
3902
691
}
3903
3904
3905
static bool
3906
is_indirect_call_opcode (xtensa_opcode opcode)
3907
10
{
3908
10
  init_call_opcodes ();
3909
10
  return (opcode == callx0_op
3910
10
    || opcode == callx4_op
3911
10
    || opcode == callx8_op
3912
10
    || opcode == callx12_op);
3913
10
}
3914
3915
3916
static bool
3917
is_direct_call_opcode (xtensa_opcode opcode)
3918
535
{
3919
535
  init_call_opcodes ();
3920
535
  return (opcode == call0_op
3921
513
    || opcode == call4_op
3922
500
    || opcode == call8_op
3923
471
    || opcode == call12_op);
3924
535
}
3925
3926
3927
static bool
3928
is_windowed_call_opcode (xtensa_opcode opcode)
3929
75
{
3930
75
  init_call_opcodes ();
3931
75
  return (opcode == call4_op
3932
64
    || opcode == call8_op
3933
36
    || opcode == call12_op
3934
25
    || opcode == callx4_op
3935
25
    || opcode == callx8_op
3936
25
    || opcode == callx12_op);
3937
75
}
3938
3939
3940
static bool
3941
get_indirect_call_dest_reg (xtensa_opcode opcode, unsigned *pdst)
3942
0
{
3943
0
  unsigned dst = (unsigned) -1;
3944
3945
0
  init_call_opcodes ();
3946
0
  if (opcode == callx0_op)
3947
0
    dst = 0;
3948
0
  else if (opcode == callx4_op)
3949
0
    dst = 4;
3950
0
  else if (opcode == callx8_op)
3951
0
    dst = 8;
3952
0
  else if (opcode == callx12_op)
3953
0
    dst = 12;
3954
3955
0
  if (dst == (unsigned) -1)
3956
0
    return false;
3957
3958
0
  *pdst = dst;
3959
0
  return true;
3960
0
}
3961
3962
3963
static xtensa_opcode
3964
get_const16_opcode (void)
3965
3.11k
{
3966
3.11k
  static bool done_lookup = false;
3967
3.11k
  static xtensa_opcode const16_opcode = XTENSA_UNDEFINED;
3968
3.11k
  if (!done_lookup)
3969
1
    {
3970
1
      const16_opcode = xtensa_opcode_lookup (xtensa_default_isa, "const16");
3971
1
      done_lookup = true;
3972
1
    }
3973
3.11k
  return const16_opcode;
3974
3.11k
}
3975
3976
3977
static xtensa_opcode
3978
get_l32r_opcode (void)
3979
280
{
3980
280
  static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
3981
280
  static bool done_lookup = false;
3982
3983
280
  if (!done_lookup)
3984
1
    {
3985
1
      l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
3986
1
      done_lookup = true;
3987
1
    }
3988
280
  return l32r_opcode;
3989
280
}
3990
3991
3992
static bfd_vma
3993
l32r_offset (bfd_vma addr, bfd_vma pc)
3994
0
{
3995
0
  bfd_vma offset;
3996
3997
0
  offset = addr - ((pc+3) & -4);
3998
0
  BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
3999
0
  offset = (signed int) offset >> 2;
4000
0
  BFD_ASSERT ((signed int) offset >> 16 == -1);
4001
0
  return offset;
4002
0
}
4003
4004
4005
static xtensa_opcode
4006
get_rsr_lend_opcode (void)
4007
0
{
4008
0
  static xtensa_opcode rsr_lend_opcode = XTENSA_UNDEFINED;
4009
0
  static bool done_lookup = false;
4010
0
  if (!done_lookup)
4011
0
    {
4012
0
      rsr_lend_opcode = xtensa_opcode_lookup (xtensa_default_isa, "rsr.lend");
4013
0
      done_lookup = true;
4014
0
    }
4015
0
  return rsr_lend_opcode;
4016
0
}
4017
4018
static xtensa_opcode
4019
get_wsr_lbeg_opcode (void)
4020
0
{
4021
0
  static xtensa_opcode wsr_lbeg_opcode = XTENSA_UNDEFINED;
4022
0
  static bool done_lookup = false;
4023
0
  if (!done_lookup)
4024
0
    {
4025
0
      wsr_lbeg_opcode = xtensa_opcode_lookup (xtensa_default_isa, "wsr.lbeg");
4026
0
      done_lookup = true;
4027
0
    }
4028
0
  return wsr_lbeg_opcode;
4029
0
}
4030
4031
4032
static int
4033
get_relocation_opnd (xtensa_opcode opcode, int r_type)
4034
3.01k
{
4035
3.01k
  xtensa_isa isa = xtensa_default_isa;
4036
3.01k
  int last_immed, last_opnd, opi;
4037
4038
3.01k
  if (opcode == XTENSA_UNDEFINED)
4039
0
    return XTENSA_UNDEFINED;
4040
4041
  /* Find the last visible PC-relative immediate operand for the opcode.
4042
     If there are no PC-relative immediates, then choose the last visible
4043
     immediate; otherwise, fail and return XTENSA_UNDEFINED.  */
4044
3.01k
  last_immed = XTENSA_UNDEFINED;
4045
3.01k
  last_opnd = xtensa_opcode_num_operands (isa, opcode);
4046
6.39k
  for (opi = last_opnd - 1; opi >= 0; opi--)
4047
3.71k
    {
4048
3.71k
      if (xtensa_operand_is_visible (isa, opcode, opi) == 0)
4049
118
  continue;
4050
3.59k
      if (xtensa_operand_is_PCrelative (isa, opcode, opi) == 1)
4051
333
  {
4052
333
    last_immed = opi;
4053
333
    break;
4054
333
  }
4055
3.26k
      if (last_immed == XTENSA_UNDEFINED
4056
2.08k
    && xtensa_operand_is_register (isa, opcode, opi) == 0)
4057
665
  last_immed = opi;
4058
3.26k
    }
4059
3.01k
  if (last_immed < 0)
4060
2.02k
    return XTENSA_UNDEFINED;
4061
4062
  /* If the operand number was specified in an old-style relocation,
4063
     check for consistency with the operand computed above.  */
4064
998
  if (r_type >= R_XTENSA_OP0 && r_type <= R_XTENSA_OP2)
4065
543
    {
4066
543
      int reloc_opnd = r_type - R_XTENSA_OP0;
4067
543
      if (reloc_opnd != last_immed)
4068
463
  return XTENSA_UNDEFINED;
4069
543
    }
4070
4071
535
  return last_immed;
4072
998
}
4073
4074
4075
int
4076
get_relocation_slot (int r_type)
4077
3.93k
{
4078
3.93k
  switch (r_type)
4079
3.93k
    {
4080
1.76k
    case R_XTENSA_OP0:
4081
1.81k
    case R_XTENSA_OP1:
4082
1.83k
    case R_XTENSA_OP2:
4083
1.83k
      return 0;
4084
4085
2.10k
    default:
4086
2.10k
      if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4087
1.62k
  return r_type - R_XTENSA_SLOT0_OP;
4088
484
      if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4089
484
  return r_type - R_XTENSA_SLOT0_ALT;
4090
0
      break;
4091
3.93k
    }
4092
4093
0
  return XTENSA_UNDEFINED;
4094
3.93k
}
4095
4096
4097
/* Get the opcode for a relocation.  */
4098
4099
static xtensa_opcode
4100
get_relocation_opcode (bfd *abfd,
4101
           asection *sec,
4102
           bfd_byte *contents,
4103
           Elf_Internal_Rela *irel)
4104
0
{
4105
0
  static xtensa_insnbuf ibuff = NULL;
4106
0
  static xtensa_insnbuf sbuff = NULL;
4107
0
  xtensa_isa isa = xtensa_default_isa;
4108
0
  xtensa_format fmt;
4109
0
  int slot;
4110
4111
0
  if (contents == NULL)
4112
0
    return XTENSA_UNDEFINED;
4113
4114
0
  if (bfd_get_section_limit (abfd, sec) <= irel->r_offset)
4115
0
    return XTENSA_UNDEFINED;
4116
4117
0
  if (ibuff == NULL)
4118
0
    {
4119
0
      ibuff = xtensa_insnbuf_alloc (isa);
4120
0
      sbuff = xtensa_insnbuf_alloc (isa);
4121
0
    }
4122
4123
  /* Decode the instruction.  */
4124
0
  if (xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
4125
0
         sec->size - irel->r_offset) < 0)
4126
0
    return XTENSA_UNDEFINED;
4127
0
  fmt = xtensa_format_decode (isa, ibuff);
4128
0
  slot = get_relocation_slot (ELF32_R_TYPE (irel->r_info));
4129
0
  if (slot == XTENSA_UNDEFINED)
4130
0
    return XTENSA_UNDEFINED;
4131
0
  xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
4132
0
  return xtensa_opcode_decode (isa, fmt, slot, sbuff);
4133
0
}
4134
4135
4136
bool
4137
is_l32r_relocation (bfd *abfd,
4138
        asection *sec,
4139
        bfd_byte *contents,
4140
        Elf_Internal_Rela *irel)
4141
0
{
4142
0
  xtensa_opcode opcode;
4143
0
  if (!is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
4144
0
    return false;
4145
0
  opcode = get_relocation_opcode (abfd, sec, contents, irel);
4146
0
  return (opcode == get_l32r_opcode ());
4147
0
}
4148
4149
4150
static bfd_size_type
4151
get_asm_simplify_size (bfd_byte *contents,
4152
           bfd_size_type content_len,
4153
           bfd_size_type offset)
4154
0
{
4155
0
  bfd_size_type insnlen, size = 0;
4156
4157
  /* Decode the size of the next two instructions.  */
4158
0
  insnlen = insn_decode_len (contents, content_len, offset);
4159
0
  if (insnlen == 0)
4160
0
    return 0;
4161
4162
0
  size += insnlen;
4163
4164
0
  insnlen = insn_decode_len (contents, content_len, offset + size);
4165
0
  if (insnlen == 0)
4166
0
    return 0;
4167
4168
0
  size += insnlen;
4169
0
  return size;
4170
0
}
4171
4172
4173
bool
4174
is_alt_relocation (int r_type)
4175
3.08k
{
4176
3.08k
  return (r_type >= R_XTENSA_SLOT0_ALT
4177
38
    && r_type <= R_XTENSA_SLOT14_ALT);
4178
3.08k
}
4179
4180
4181
bool
4182
is_operand_relocation (int r_type)
4183
0
{
4184
0
  switch (r_type)
4185
0
    {
4186
0
    case R_XTENSA_OP0:
4187
0
    case R_XTENSA_OP1:
4188
0
    case R_XTENSA_OP2:
4189
0
      return true;
4190
4191
0
    default:
4192
0
      if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4193
0
  return true;
4194
0
      if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4195
0
  return true;
4196
0
      break;
4197
0
    }
4198
4199
0
  return false;
4200
0
}
4201
4202
4203
0
#define MIN_INSN_LENGTH 2
4204
4205
/* Return 0 if it fails to decode.  */
4206
4207
bfd_size_type
4208
insn_decode_len (bfd_byte *contents,
4209
     bfd_size_type content_len,
4210
     bfd_size_type offset)
4211
0
{
4212
0
  int insn_len;
4213
0
  xtensa_isa isa = xtensa_default_isa;
4214
0
  xtensa_format fmt;
4215
0
  static xtensa_insnbuf ibuff = NULL;
4216
4217
0
  if (offset + MIN_INSN_LENGTH > content_len)
4218
0
    return 0;
4219
4220
0
  if (ibuff == NULL)
4221
0
    ibuff = xtensa_insnbuf_alloc (isa);
4222
0
  if (xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
4223
0
         content_len - offset) < 0)
4224
0
    return 0;
4225
0
  fmt = xtensa_format_decode (isa, ibuff);
4226
0
  if (fmt == XTENSA_UNDEFINED)
4227
0
    return 0;
4228
0
  insn_len = xtensa_format_length (isa, fmt);
4229
0
  if (insn_len ==  XTENSA_UNDEFINED)
4230
0
    return 0;
4231
0
  return insn_len;
4232
0
}
4233
4234
int
4235
insn_num_slots (bfd_byte *contents,
4236
    bfd_size_type content_len,
4237
    bfd_size_type offset)
4238
0
{
4239
0
  xtensa_isa isa = xtensa_default_isa;
4240
0
  xtensa_format fmt;
4241
0
  static xtensa_insnbuf ibuff = NULL;
4242
4243
0
  if (offset + MIN_INSN_LENGTH > content_len)
4244
0
    return XTENSA_UNDEFINED;
4245
4246
0
  if (ibuff == NULL)
4247
0
    ibuff = xtensa_insnbuf_alloc (isa);
4248
0
  if (xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
4249
0
         content_len - offset) < 0)
4250
0
    return XTENSA_UNDEFINED;
4251
0
  fmt = xtensa_format_decode (isa, ibuff);
4252
0
  if (fmt == XTENSA_UNDEFINED)
4253
0
    return XTENSA_UNDEFINED;
4254
0
  return xtensa_format_num_slots (isa, fmt);
4255
0
}
4256
4257
4258
/* Decode the opcode for a single slot instruction.
4259
   Return 0 if it fails to decode or the instruction is multi-slot.  */
4260
4261
xtensa_opcode
4262
insn_decode_opcode (bfd_byte *contents,
4263
        bfd_size_type content_len,
4264
        bfd_size_type offset,
4265
        int slot)
4266
0
{
4267
0
  xtensa_isa isa = xtensa_default_isa;
4268
0
  xtensa_format fmt;
4269
0
  static xtensa_insnbuf insnbuf = NULL;
4270
0
  static xtensa_insnbuf slotbuf = NULL;
4271
4272
0
  if (offset + MIN_INSN_LENGTH > content_len)
4273
0
    return XTENSA_UNDEFINED;
4274
4275
0
  if (insnbuf == NULL)
4276
0
    {
4277
0
      insnbuf = xtensa_insnbuf_alloc (isa);
4278
0
      slotbuf = xtensa_insnbuf_alloc (isa);
4279
0
    }
4280
4281
0
  if (xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4282
0
         content_len - offset) < 0)
4283
0
    return XTENSA_UNDEFINED;
4284
0
  fmt = xtensa_format_decode (isa, insnbuf);
4285
0
  if (fmt == XTENSA_UNDEFINED)
4286
0
    return XTENSA_UNDEFINED;
4287
4288
0
  if (slot >= xtensa_format_num_slots (isa, fmt))
4289
0
    return XTENSA_UNDEFINED;
4290
4291
0
  xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4292
0
  return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4293
0
}
4294
4295
4296
/* The offset is the offset in the contents.
4297
   The address is the address of that offset.  */
4298
4299
static bool
4300
check_branch_target_aligned (bfd_byte *contents,
4301
           bfd_size_type content_length,
4302
           bfd_vma offset,
4303
           bfd_vma address)
4304
0
{
4305
0
  bfd_size_type insn_len = insn_decode_len (contents, content_length, offset);
4306
0
  if (insn_len == 0)
4307
0
    return false;
4308
0
  return check_branch_target_aligned_address (address, insn_len);
4309
0
}
4310
4311
4312
static bool
4313
check_loop_aligned (bfd_byte *contents,
4314
        bfd_size_type content_length,
4315
        bfd_vma offset,
4316
        bfd_vma address)
4317
0
{
4318
0
  bfd_size_type loop_len, insn_len;
4319
0
  xtensa_opcode opcode;
4320
4321
0
  opcode = insn_decode_opcode (contents, content_length, offset, 0);
4322
0
  if (opcode == XTENSA_UNDEFINED
4323
0
      || xtensa_opcode_is_loop (xtensa_default_isa, opcode) != 1)
4324
0
    {
4325
0
      BFD_ASSERT (false);
4326
0
      return false;
4327
0
    }
4328
4329
0
  loop_len = insn_decode_len (contents, content_length, offset);
4330
0
  insn_len = insn_decode_len (contents, content_length, offset + loop_len);
4331
0
  if (loop_len == 0 || insn_len == 0)
4332
0
    {
4333
0
      BFD_ASSERT (false);
4334
0
      return false;
4335
0
    }
4336
4337
  /* If this is relaxed loop, analyze first instruction of the actual loop
4338
     body.  It must be at offset 27 from the loop instruction address.  */
4339
0
  if (insn_len == 3
4340
0
      && insn_num_slots (contents, content_length, offset + loop_len) == 1
4341
0
      && insn_decode_opcode (contents, content_length,
4342
0
           offset + loop_len, 0) == get_rsr_lend_opcode()
4343
0
      && insn_decode_len (contents, content_length, offset + loop_len + 3) == 3
4344
0
      && insn_num_slots (contents, content_length, offset + loop_len + 3) == 1
4345
0
      && insn_decode_opcode (contents, content_length,
4346
0
           offset + loop_len + 3, 0) == get_wsr_lbeg_opcode())
4347
0
    {
4348
0
      loop_len = 27;
4349
0
      insn_len = insn_decode_len (contents, content_length, offset + loop_len);
4350
0
    }
4351
0
  return check_branch_target_aligned_address (address + loop_len, insn_len);
4352
0
}
4353
4354
4355
static bool
4356
check_branch_target_aligned_address (bfd_vma addr, int len)
4357
0
{
4358
0
  if (len == 8)
4359
0
    return (addr % 8 == 0);
4360
0
  return ((addr >> 2) == ((addr + len - 1) >> 2));
4361
0
}
4362
4363

4364
/* Instruction widening and narrowing.  */
4365
4366
/* When FLIX is available we need to access certain instructions only
4367
   when they are 16-bit or 24-bit instructions.  This table caches
4368
   information about such instructions by walking through all the
4369
   opcodes and finding the smallest single-slot format into which each
4370
   can be encoded.  */
4371
4372
static xtensa_format *op_single_fmt_table = NULL;
4373
4374
4375
static void
4376
init_op_single_format_table (void)
4377
0
{
4378
0
  xtensa_isa isa = xtensa_default_isa;
4379
0
  xtensa_insnbuf ibuf;
4380
0
  xtensa_opcode opcode;
4381
0
  xtensa_format fmt;
4382
0
  int num_opcodes;
4383
4384
0
  if (op_single_fmt_table)
4385
0
    return;
4386
4387
0
  ibuf = xtensa_insnbuf_alloc (isa);
4388
0
  num_opcodes = xtensa_isa_num_opcodes (isa);
4389
4390
0
  op_single_fmt_table = (xtensa_format *)
4391
0
    bfd_malloc (sizeof (xtensa_format) * num_opcodes);
4392
0
  for (opcode = 0; opcode < num_opcodes; opcode++)
4393
0
    {
4394
0
      op_single_fmt_table[opcode] = XTENSA_UNDEFINED;
4395
0
      for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
4396
0
  {
4397
0
    if (xtensa_format_num_slots (isa, fmt) == 1
4398
0
        && xtensa_opcode_encode (isa, fmt, 0, ibuf, opcode) == 0)
4399
0
      {
4400
0
        xtensa_opcode old_fmt = op_single_fmt_table[opcode];
4401
0
        int fmt_length = xtensa_format_length (isa, fmt);
4402
0
        if (old_fmt == XTENSA_UNDEFINED
4403
0
      || fmt_length < xtensa_format_length (isa, old_fmt))
4404
0
    op_single_fmt_table[opcode] = fmt;
4405
0
      }
4406
0
  }
4407
0
    }
4408
0
  xtensa_insnbuf_free (isa, ibuf);
4409
0
}
4410
4411
4412
static xtensa_format
4413
get_single_format (xtensa_opcode opcode)
4414
0
{
4415
0
  init_op_single_format_table ();
4416
0
  return op_single_fmt_table[opcode];
4417
0
}
4418
4419
4420
/* For the set of narrowable instructions we do NOT include the
4421
   narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
4422
   involved during linker relaxation that may require these to
4423
   re-expand in some conditions.  Also, the narrowing "or" -> mov.n
4424
   requires special case code to ensure it only works when op1 == op2.  */
4425
4426
struct string_pair
4427
{
4428
  const char *wide;
4429
  const char *narrow;
4430
};
4431
4432
const struct string_pair narrowable[] =
4433
{
4434
  { "add", "add.n" },
4435
  { "addi", "addi.n" },
4436
  { "addmi", "addi.n" },
4437
  { "l32i", "l32i.n" },
4438
  { "movi", "movi.n" },
4439
  { "ret", "ret.n" },
4440
  { "retw", "retw.n" },
4441
  { "s32i", "s32i.n" },
4442
  { "or", "mov.n" } /* special case only when op1 == op2 */
4443
};
4444
4445
const struct string_pair widenable[] =
4446
{
4447
  { "add", "add.n" },
4448
  { "addi", "addi.n" },
4449
  { "addmi", "addi.n" },
4450
  { "beqz", "beqz.n" },
4451
  { "bnez", "bnez.n" },
4452
  { "l32i", "l32i.n" },
4453
  { "movi", "movi.n" },
4454
  { "ret", "ret.n" },
4455
  { "retw", "retw.n" },
4456
  { "s32i", "s32i.n" },
4457
  { "or", "mov.n" } /* special case only when op1 == op2 */
4458
};
4459
4460
4461
/* Check if an instruction can be "narrowed", i.e., changed from a standard
4462
   3-byte instruction to a 2-byte "density" instruction.  If it is valid,
4463
   return the instruction buffer holding the narrow instruction.  Otherwise,
4464
   return 0.  The set of valid narrowing are specified by a string table
4465
   but require some special case operand checks in some cases.  */
4466
4467
static xtensa_insnbuf
4468
can_narrow_instruction (xtensa_insnbuf slotbuf,
4469
      xtensa_format fmt,
4470
      xtensa_opcode opcode)
4471
0
{
4472
0
  xtensa_isa isa = xtensa_default_isa;
4473
0
  xtensa_format o_fmt;
4474
0
  unsigned opi;
4475
4476
0
  static xtensa_insnbuf o_insnbuf = NULL;
4477
0
  static xtensa_insnbuf o_slotbuf = NULL;
4478
4479
0
  if (o_insnbuf == NULL)
4480
0
    {
4481
0
      o_insnbuf = xtensa_insnbuf_alloc (isa);
4482
0
      o_slotbuf = xtensa_insnbuf_alloc (isa);
4483
0
    }
4484
4485
0
  for (opi = 0; opi < (sizeof (narrowable)/sizeof (struct string_pair)); opi++)
4486
0
    {
4487
0
      bool is_or = (strcmp ("or", narrowable[opi].wide) == 0);
4488
4489
0
      if (opcode == xtensa_opcode_lookup (isa, narrowable[opi].wide))
4490
0
  {
4491
0
    uint32 value, newval;
4492
0
    int i, operand_count, o_operand_count;
4493
0
    xtensa_opcode o_opcode;
4494
4495
    /* Address does not matter in this case.  We might need to
4496
       fix it to handle branches/jumps.  */
4497
0
    bfd_vma self_address = 0;
4498
4499
0
    o_opcode = xtensa_opcode_lookup (isa, narrowable[opi].narrow);
4500
0
    if (o_opcode == XTENSA_UNDEFINED)
4501
0
      return 0;
4502
0
    o_fmt = get_single_format (o_opcode);
4503
0
    if (o_fmt == XTENSA_UNDEFINED)
4504
0
      return 0;
4505
4506
0
    if (xtensa_format_length (isa, fmt) != 3
4507
0
        || xtensa_format_length (isa, o_fmt) != 2)
4508
0
      return 0;
4509
4510
0
    xtensa_format_encode (isa, o_fmt, o_insnbuf);
4511
0
    operand_count = xtensa_opcode_num_operands (isa, opcode);
4512
0
    o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4513
4514
0
    if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4515
0
      return 0;
4516
4517
0
    if (!is_or)
4518
0
      {
4519
0
        if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4520
0
    return 0;
4521
0
      }
4522
0
    else
4523
0
      {
4524
0
        uint32 rawval0, rawval1, rawval2;
4525
4526
0
        if (o_operand_count + 1 != operand_count
4527
0
      || xtensa_operand_get_field (isa, opcode, 0,
4528
0
                 fmt, 0, slotbuf, &rawval0) != 0
4529
0
      || xtensa_operand_get_field (isa, opcode, 1,
4530
0
                 fmt, 0, slotbuf, &rawval1) != 0
4531
0
      || xtensa_operand_get_field (isa, opcode, 2,
4532
0
                 fmt, 0, slotbuf, &rawval2) != 0
4533
0
      || rawval1 != rawval2
4534
0
      || rawval0 == rawval1 /* it is a nop */)
4535
0
    return 0;
4536
0
      }
4537
4538
0
    for (i = 0; i < o_operand_count; ++i)
4539
0
      {
4540
0
        if (xtensa_operand_get_field (isa, opcode, i, fmt, 0,
4541
0
              slotbuf, &value)
4542
0
      || xtensa_operand_decode (isa, opcode, i, &value))
4543
0
    return 0;
4544
4545
        /* PC-relative branches need adjustment, but
4546
     the PC-rel operand will always have a relocation.  */
4547
0
        newval = value;
4548
0
        if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4549
0
             self_address)
4550
0
      || xtensa_operand_encode (isa, o_opcode, i, &newval)
4551
0
      || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4552
0
                 o_slotbuf, newval))
4553
0
    return 0;
4554
0
      }
4555
4556
0
    if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4557
0
      return 0;
4558
4559
0
    return o_insnbuf;
4560
0
  }
4561
0
    }
4562
0
  return 0;
4563
0
}
4564
4565
4566
/* Attempt to narrow an instruction.  If the narrowing is valid, perform
4567
   the action in-place directly into the contents and return TRUE.  Otherwise,
4568
   the return value is FALSE and the contents are not modified.  */
4569
4570
static bool
4571
narrow_instruction (bfd_byte *contents,
4572
        bfd_size_type content_length,
4573
        bfd_size_type offset)
4574
0
{
4575
0
  xtensa_opcode opcode;
4576
0
  bfd_size_type insn_len;
4577
0
  xtensa_isa isa = xtensa_default_isa;
4578
0
  xtensa_format fmt;
4579
0
  xtensa_insnbuf o_insnbuf;
4580
4581
0
  static xtensa_insnbuf insnbuf = NULL;
4582
0
  static xtensa_insnbuf slotbuf = NULL;
4583
4584
0
  if (insnbuf == NULL)
4585
0
    {
4586
0
      insnbuf = xtensa_insnbuf_alloc (isa);
4587
0
      slotbuf = xtensa_insnbuf_alloc (isa);
4588
0
    }
4589
4590
0
  BFD_ASSERT (offset < content_length);
4591
4592
0
  if (content_length < 2)
4593
0
    return false;
4594
4595
  /* We will hand-code a few of these for a little while.
4596
     These have all been specified in the assembler aleady.  */
4597
0
  if (xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4598
0
         content_length - offset) < 0)
4599
0
    return false;
4600
0
  fmt = xtensa_format_decode (isa, insnbuf);
4601
0
  if (xtensa_format_num_slots (isa, fmt) != 1)
4602
0
    return false;
4603
4604
0
  if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4605
0
    return false;
4606
4607
0
  opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4608
0
  if (opcode == XTENSA_UNDEFINED)
4609
0
    return false;
4610
0
  insn_len = xtensa_format_length (isa, fmt);
4611
0
  if (insn_len > content_length)
4612
0
    return false;
4613
4614
0
  o_insnbuf = can_narrow_instruction (slotbuf, fmt, opcode);
4615
0
  if (o_insnbuf)
4616
0
    {
4617
0
      xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4618
0
             content_length - offset);
4619
0
      return true;
4620
0
    }
4621
4622
0
  return false;
4623
0
}
4624
4625
4626
/* Check if an instruction can be "widened", i.e., changed from a 2-byte
4627
   "density" instruction to a standard 3-byte instruction.  If it is valid,
4628
   return the instruction buffer holding the wide instruction.  Otherwise,
4629
   return 0.  The set of valid widenings are specified by a string table
4630
   but require some special case operand checks in some cases.  */
4631
4632
static xtensa_insnbuf
4633
can_widen_instruction (xtensa_insnbuf slotbuf,
4634
           xtensa_format fmt,
4635
           xtensa_opcode opcode)
4636
0
{
4637
0
  xtensa_isa isa = xtensa_default_isa;
4638
0
  xtensa_format o_fmt;
4639
0
  unsigned opi;
4640
4641
0
  static xtensa_insnbuf o_insnbuf = NULL;
4642
0
  static xtensa_insnbuf o_slotbuf = NULL;
4643
4644
0
  if (o_insnbuf == NULL)
4645
0
    {
4646
0
      o_insnbuf = xtensa_insnbuf_alloc (isa);
4647
0
      o_slotbuf = xtensa_insnbuf_alloc (isa);
4648
0
    }
4649
4650
0
  for (opi = 0; opi < (sizeof (widenable)/sizeof (struct string_pair)); opi++)
4651
0
    {
4652
0
      bool is_or = (strcmp ("or", widenable[opi].wide) == 0);
4653
0
      bool is_branch = (strcmp ("beqz", widenable[opi].wide) == 0
4654
0
      || strcmp ("bnez", widenable[opi].wide) == 0);
4655
4656
0
      if (opcode == xtensa_opcode_lookup (isa, widenable[opi].narrow))
4657
0
  {
4658
0
    uint32 value, newval;
4659
0
    int i, operand_count, o_operand_count, check_operand_count;
4660
0
    xtensa_opcode o_opcode;
4661
4662
    /* Address does not matter in this case.  We might need to fix it
4663
       to handle branches/jumps.  */
4664
0
    bfd_vma self_address = 0;
4665
4666
0
    o_opcode = xtensa_opcode_lookup (isa, widenable[opi].wide);
4667
0
    if (o_opcode == XTENSA_UNDEFINED)
4668
0
      return 0;
4669
0
    o_fmt = get_single_format (o_opcode);
4670
0
    if (o_fmt == XTENSA_UNDEFINED)
4671
0
      return 0;
4672
4673
0
    if (xtensa_format_length (isa, fmt) != 2
4674
0
        || xtensa_format_length (isa, o_fmt) != 3)
4675
0
      return 0;
4676
4677
0
    xtensa_format_encode (isa, o_fmt, o_insnbuf);
4678
0
    operand_count = xtensa_opcode_num_operands (isa, opcode);
4679
0
    o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4680
0
    check_operand_count = o_operand_count;
4681
4682
0
    if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4683
0
      return 0;
4684
4685
0
    if (!is_or)
4686
0
      {
4687
0
        if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4688
0
    return 0;
4689
0
      }
4690
0
    else
4691
0
      {
4692
0
        uint32 rawval0, rawval1;
4693
4694
0
        if (o_operand_count != operand_count + 1
4695
0
      || xtensa_operand_get_field (isa, opcode, 0,
4696
0
                 fmt, 0, slotbuf, &rawval0) != 0
4697
0
      || xtensa_operand_get_field (isa, opcode, 1,
4698
0
                 fmt, 0, slotbuf, &rawval1) != 0
4699
0
      || rawval0 == rawval1 /* it is a nop */)
4700
0
    return 0;
4701
0
      }
4702
0
    if (is_branch)
4703
0
      check_operand_count--;
4704
4705
0
    for (i = 0; i < check_operand_count; i++)
4706
0
      {
4707
0
        int new_i = i;
4708
0
        if (is_or && i == o_operand_count - 1)
4709
0
    new_i = i - 1;
4710
0
        if (xtensa_operand_get_field (isa, opcode, new_i, fmt, 0,
4711
0
              slotbuf, &value)
4712
0
      || xtensa_operand_decode (isa, opcode, new_i, &value))
4713
0
    return 0;
4714
4715
        /* PC-relative branches need adjustment, but
4716
     the PC-rel operand will always have a relocation.  */
4717
0
        newval = value;
4718
0
        if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4719
0
             self_address)
4720
0
      || xtensa_operand_encode (isa, o_opcode, i, &newval)
4721
0
      || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4722
0
                 o_slotbuf, newval))
4723
0
    return 0;
4724
0
      }
4725
4726
0
    if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4727
0
      return 0;
4728
4729
0
    return o_insnbuf;
4730
0
  }
4731
0
    }
4732
0
  return 0;
4733
0
}
4734
4735
4736
/* Attempt to widen an instruction.  If the widening is valid, perform
4737
   the action in-place directly into the contents and return TRUE.  Otherwise,
4738
   the return value is FALSE and the contents are not modified.  */
4739
4740
static bool
4741
widen_instruction (bfd_byte *contents,
4742
       bfd_size_type content_length,
4743
       bfd_size_type offset)
4744
0
{
4745
0
  xtensa_opcode opcode;
4746
0
  bfd_size_type insn_len;
4747
0
  xtensa_isa isa = xtensa_default_isa;
4748
0
  xtensa_format fmt;
4749
0
  xtensa_insnbuf o_insnbuf;
4750
4751
0
  static xtensa_insnbuf insnbuf = NULL;
4752
0
  static xtensa_insnbuf slotbuf = NULL;
4753
4754
0
  if (insnbuf == NULL)
4755
0
    {
4756
0
      insnbuf = xtensa_insnbuf_alloc (isa);
4757
0
      slotbuf = xtensa_insnbuf_alloc (isa);
4758
0
    }
4759
4760
0
  BFD_ASSERT (offset < content_length);
4761
4762
0
  if (content_length < 2)
4763
0
    return false;
4764
4765
  /* We will hand-code a few of these for a little while.
4766
     These have all been specified in the assembler aleady.  */
4767
0
  if (xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4768
0
         content_length - offset) < 0)
4769
0
    return false;
4770
0
  fmt = xtensa_format_decode (isa, insnbuf);
4771
0
  if (xtensa_format_num_slots (isa, fmt) != 1)
4772
0
    return false;
4773
4774
0
  if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4775
0
    return false;
4776
4777
0
  opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4778
0
  if (opcode == XTENSA_UNDEFINED)
4779
0
    return false;
4780
0
  insn_len = xtensa_format_length (isa, fmt);
4781
0
  if (insn_len > content_length)
4782
0
    return false;
4783
4784
0
  o_insnbuf = can_widen_instruction (slotbuf, fmt, opcode);
4785
0
  if (o_insnbuf)
4786
0
    {
4787
0
      xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4788
0
             content_length - offset);
4789
0
      return true;
4790
0
    }
4791
0
  return false;
4792
0
}
4793
4794

4795
/* Code for transforming CALLs at link-time.  */
4796
4797
static bfd_reloc_status_type
4798
elf_xtensa_do_asm_simplify (bfd_byte *contents,
4799
          bfd_vma address,
4800
          bfd_vma content_length,
4801
          char **error_message)
4802
71
{
4803
71
  static xtensa_insnbuf insnbuf = NULL;
4804
71
  static xtensa_insnbuf slotbuf = NULL;
4805
71
  xtensa_format core_format = XTENSA_UNDEFINED;
4806
71
  xtensa_opcode opcode;
4807
71
  xtensa_opcode direct_call_opcode;
4808
71
  xtensa_isa isa = xtensa_default_isa;
4809
71
  bfd_byte *chbuf = contents + address;
4810
71
  int opn;
4811
4812
71
  if (insnbuf == NULL)
4813
1
    {
4814
1
      insnbuf = xtensa_insnbuf_alloc (isa);
4815
1
      slotbuf = xtensa_insnbuf_alloc (isa);
4816
1
    }
4817
4818
71
  if (content_length < address)
4819
0
    {
4820
0
      *error_message = _("attempt to convert L32R/CALLX to CALL failed");
4821
0
      return bfd_reloc_other;
4822
0
    }
4823
4824
71
  opcode = get_expanded_call_opcode (chbuf, content_length - address, 0);
4825
71
  direct_call_opcode = swap_callx_for_call_opcode (opcode);
4826
71
  if (direct_call_opcode == XTENSA_UNDEFINED)
4827
71
    {
4828
71
      *error_message = _("attempt to convert L32R/CALLX to CALL failed");
4829
71
      return bfd_reloc_other;
4830
71
    }
4831
4832
  /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset.  */
4833
0
  core_format = xtensa_format_lookup (isa, "x24");
4834
0
  opcode = xtensa_opcode_lookup (isa, "or");
4835
0
  xtensa_opcode_encode (isa, core_format, 0, slotbuf, opcode);
4836
0
  for (opn = 0; opn < 3; opn++)
4837
0
    {
4838
0
      uint32 regno = 1;
4839
0
      xtensa_operand_encode (isa, opcode, opn, &regno);
4840
0
      xtensa_operand_set_field (isa, opcode, opn, core_format, 0,
4841
0
        slotbuf, regno);
4842
0
    }
4843
0
  xtensa_format_encode (isa, core_format, insnbuf);
4844
0
  xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4845
0
  xtensa_insnbuf_to_chars (isa, insnbuf, chbuf, content_length - address);
4846
4847
  /* Assemble a CALL ("callN 0") into the 3 byte offset.  */
4848
0
  xtensa_opcode_encode (isa, core_format, 0, slotbuf, direct_call_opcode);
4849
0
  xtensa_operand_set_field (isa, opcode, 0, core_format, 0, slotbuf, 0);
4850
4851
0
  xtensa_format_encode (isa, core_format, insnbuf);
4852
0
  xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4853
0
  xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3,
4854
0
         content_length - address - 3);
4855
4856
0
  return bfd_reloc_ok;
4857
71
}
4858
4859
4860
static bfd_reloc_status_type
4861
contract_asm_expansion (bfd_byte *contents,
4862
      bfd_vma content_length,
4863
      Elf_Internal_Rela *irel,
4864
      char **error_message)
4865
0
{
4866
0
  bfd_reloc_status_type retval =
4867
0
    elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length,
4868
0
        error_message);
4869
4870
0
  if (retval != bfd_reloc_ok)
4871
0
    return bfd_reloc_dangerous;
4872
4873
  /* Update the irel->r_offset field so that the right immediate and
4874
     the right instruction are modified during the relocation.  */
4875
0
  irel->r_offset += 3;
4876
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_SLOT0_OP);
4877
0
  return bfd_reloc_ok;
4878
0
}
4879
4880
4881
static xtensa_opcode
4882
swap_callx_for_call_opcode (xtensa_opcode opcode)
4883
71
{
4884
71
  init_call_opcodes ();
4885
4886
71
  if (opcode == callx0_op) return call0_op;
4887
71
  if (opcode == callx4_op) return call4_op;
4888
71
  if (opcode == callx8_op) return call8_op;
4889
71
  if (opcode == callx12_op) return call12_op;
4890
4891
  /* Return XTENSA_UNDEFINED if the opcode is not an indirect call.  */
4892
71
  return XTENSA_UNDEFINED;
4893
71
}
4894
4895
4896
/* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
4897
   CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
4898
   If not, return XTENSA_UNDEFINED.  */
4899
4900
20
#define L32R_TARGET_REG_OPERAND 0
4901
0
#define CONST16_TARGET_REG_OPERAND 0
4902
0
#define CALLN_SOURCE_OPERAND 0
4903
4904
static xtensa_opcode
4905
get_expanded_call_opcode (bfd_byte *buf, int bufsize, bool *p_uses_l32r)
4906
75
{
4907
75
  static xtensa_insnbuf insnbuf = NULL;
4908
75
  static xtensa_insnbuf slotbuf = NULL;
4909
75
  xtensa_format fmt;
4910
75
  xtensa_opcode opcode;
4911
75
  xtensa_isa isa = xtensa_default_isa;
4912
75
  uint32 regno, const16_regno, call_regno;
4913
75
  int offset = 0;
4914
4915
75
  if (insnbuf == NULL)
4916
1
    {
4917
1
      insnbuf = xtensa_insnbuf_alloc (isa);
4918
1
      slotbuf = xtensa_insnbuf_alloc (isa);
4919
1
    }
4920
4921
75
  if (xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize) < 0)
4922
2
    return XTENSA_UNDEFINED;
4923
73
  fmt = xtensa_format_decode (isa, insnbuf);
4924
73
  if (fmt == XTENSA_UNDEFINED
4925
72
      || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4926
1
    return XTENSA_UNDEFINED;
4927
4928
72
  opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4929
72
  if (opcode == XTENSA_UNDEFINED)
4930
0
    return XTENSA_UNDEFINED;
4931
4932
72
  if (opcode == get_l32r_opcode ())
4933
10
    {
4934
10
      if (p_uses_l32r)
4935
0
  *p_uses_l32r = true;
4936
10
      if (xtensa_operand_get_field (isa, opcode, L32R_TARGET_REG_OPERAND,
4937
10
            fmt, 0, slotbuf, &regno)
4938
10
    || xtensa_operand_decode (isa, opcode, L32R_TARGET_REG_OPERAND,
4939
10
            &regno))
4940
0
  return XTENSA_UNDEFINED;
4941
10
    }
4942
62
  else if (opcode == get_const16_opcode ())
4943
0
    {
4944
0
      if (p_uses_l32r)
4945
0
  *p_uses_l32r = false;
4946
0
      if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4947
0
            fmt, 0, slotbuf, &regno)
4948
0
    || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4949
0
            &regno))
4950
0
  return XTENSA_UNDEFINED;
4951
4952
      /* Check that the next instruction is also CONST16.  */
4953
0
      offset += xtensa_format_length (isa, fmt);
4954
0
      if (xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset,
4955
0
             bufsize - offset) < 0)
4956
0
  return XTENSA_UNDEFINED;
4957
0
      fmt = xtensa_format_decode (isa, insnbuf);
4958
0
      if (fmt == XTENSA_UNDEFINED
4959
0
    || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4960
0
  return XTENSA_UNDEFINED;
4961
0
      opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4962
0
      if (opcode != get_const16_opcode ())
4963
0
  return XTENSA_UNDEFINED;
4964
4965
0
      if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4966
0
            fmt, 0, slotbuf, &const16_regno)
4967
0
    || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4968
0
            &const16_regno)
4969
0
    || const16_regno != regno)
4970
0
  return XTENSA_UNDEFINED;
4971
0
    }
4972
62
  else
4973
62
    return XTENSA_UNDEFINED;
4974
4975
  /* Next instruction should be an CALLXn with operand 0 == regno.  */
4976
10
  offset += xtensa_format_length (isa, fmt);
4977
10
  if (xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset,
4978
10
         bufsize - offset) < 0)
4979
0
    return XTENSA_UNDEFINED;
4980
10
  fmt = xtensa_format_decode (isa, insnbuf);
4981
10
  if (fmt == XTENSA_UNDEFINED
4982
10
      || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4983
0
    return XTENSA_UNDEFINED;
4984
10
  opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4985
10
  if (opcode == XTENSA_UNDEFINED
4986
10
      || !is_indirect_call_opcode (opcode))
4987
10
    return XTENSA_UNDEFINED;
4988
4989
0
  if (xtensa_operand_get_field (isa, opcode, CALLN_SOURCE_OPERAND,
4990
0
        fmt, 0, slotbuf, &call_regno)
4991
0
      || xtensa_operand_decode (isa, opcode, CALLN_SOURCE_OPERAND,
4992
0
        &call_regno))
4993
0
    return XTENSA_UNDEFINED;
4994
4995
0
  if (call_regno != regno)
4996
0
    return XTENSA_UNDEFINED;
4997
4998
0
  return opcode;
4999
0
}
5000
5001

5002
/* Data structures used during relaxation.  */
5003
5004
/* r_reloc: relocation values.  */
5005
5006
/* Through the relaxation process, we need to keep track of the values
5007
   that will result from evaluating relocations.  The standard ELF
5008
   relocation structure is not sufficient for this purpose because we're
5009
   operating on multiple input files at once, so we need to know which
5010
   input file a relocation refers to.  The r_reloc structure thus
5011
   records both the input file (bfd) and ELF relocation.
5012
5013
   For efficiency, an r_reloc also contains a "target_offset" field to
5014
   cache the target-section-relative offset value that is represented by
5015
   the relocation.
5016
5017
   The r_reloc also contains a virtual offset that allows multiple
5018
   inserted literals to be placed at the same "address" with
5019
   different offsets.  */
5020
5021
typedef struct r_reloc_struct r_reloc;
5022
5023
struct r_reloc_struct
5024
{
5025
  bfd *abfd;
5026
  Elf_Internal_Rela rela;
5027
  bfd_vma target_offset;
5028
  bfd_vma virtual_offset;
5029
};
5030
5031
5032
/* The r_reloc structure is included by value in literal_value, but not
5033
   every literal_value has an associated relocation -- some are simple
5034
   constants.  In such cases, we set all the fields in the r_reloc
5035
   struct to zero.  The r_reloc_is_const function should be used to
5036
   detect this case.  */
5037
5038
static bool
5039
r_reloc_is_const (const r_reloc *r_rel)
5040
0
{
5041
0
  return (r_rel->abfd == NULL);
5042
0
}
5043
5044
5045
static bfd_vma
5046
r_reloc_get_target_offset (const r_reloc *r_rel)
5047
0
{
5048
0
  bfd_vma target_offset;
5049
0
  unsigned long r_symndx;
5050
5051
0
  BFD_ASSERT (!r_reloc_is_const (r_rel));
5052
0
  r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
5053
0
  target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
5054
0
  return (target_offset + r_rel->rela.r_addend);
5055
0
}
5056
5057
5058
static struct elf_link_hash_entry *
5059
r_reloc_get_hash_entry (const r_reloc *r_rel)
5060
0
{
5061
0
  unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
5062
0
  return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
5063
0
}
5064
5065
5066
static asection *
5067
r_reloc_get_section (const r_reloc *r_rel)
5068
0
{
5069
0
  unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
5070
0
  return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
5071
0
}
5072
5073
5074
static bool
5075
r_reloc_is_defined (const r_reloc *r_rel)
5076
0
{
5077
0
  asection *sec;
5078
0
  if (r_rel == NULL)
5079
0
    return false;
5080
5081
0
  sec = r_reloc_get_section (r_rel);
5082
0
  if (sec == bfd_abs_section_ptr
5083
0
      || sec == bfd_com_section_ptr
5084
0
      || sec == bfd_und_section_ptr)
5085
0
    return false;
5086
0
  return true;
5087
0
}
5088
5089
5090
static void
5091
r_reloc_init (r_reloc *r_rel,
5092
        bfd *abfd,
5093
        Elf_Internal_Rela *irel,
5094
        bfd_byte *contents,
5095
        bfd_size_type content_length)
5096
0
{
5097
0
  int r_type;
5098
0
  reloc_howto_type *howto;
5099
5100
0
  if (irel)
5101
0
    {
5102
0
      r_rel->rela = *irel;
5103
0
      r_rel->abfd = abfd;
5104
0
      r_rel->target_offset = r_reloc_get_target_offset (r_rel);
5105
0
      r_rel->virtual_offset = 0;
5106
0
      r_type = ELF32_R_TYPE (r_rel->rela.r_info);
5107
0
      howto = &elf_howto_table[r_type];
5108
0
      if (howto->partial_inplace)
5109
0
  {
5110
0
    bfd_vma inplace_val;
5111
0
    BFD_ASSERT (r_rel->rela.r_offset < content_length);
5112
5113
0
    inplace_val = bfd_get_32 (abfd, &contents[r_rel->rela.r_offset]);
5114
0
    r_rel->target_offset += inplace_val;
5115
0
  }
5116
0
    }
5117
0
  else
5118
0
    memset (r_rel, 0, sizeof (r_reloc));
5119
0
}
5120
5121
5122
#if DEBUG
5123
5124
static void
5125
print_r_reloc (FILE *fp, const r_reloc *r_rel)
5126
{
5127
  if (r_reloc_is_defined (r_rel))
5128
    {
5129
      asection *sec = r_reloc_get_section (r_rel);
5130
      fprintf (fp, " %s(%s + ", sec->owner->filename, sec->name);
5131
    }
5132
  else if (r_reloc_get_hash_entry (r_rel))
5133
    fprintf (fp, " %s + ", r_reloc_get_hash_entry (r_rel)->root.root.string);
5134
  else
5135
    fprintf (fp, " ?? + ");
5136
5137
  fprintf (fp, "%" PRIx64, (uint64_t) r_rel->target_offset);
5138
  if (r_rel->virtual_offset)
5139
    fprintf (fp, " + %" PRIx64, (uint64_t) r_rel->virtual_offset);
5140
5141
  fprintf (fp, ")");
5142
}
5143
5144
#endif /* DEBUG */
5145
5146

5147
/* source_reloc: relocations that reference literals.  */
5148
5149
/* To determine whether literals can be coalesced, we need to first
5150
   record all the relocations that reference the literals.  The
5151
   source_reloc structure below is used for this purpose.  The
5152
   source_reloc entries are kept in a per-literal-section array, sorted
5153
   by offset within the literal section (i.e., target offset).
5154
5155
   The source_sec and r_rel.rela.r_offset fields identify the source of
5156
   the relocation.  The r_rel field records the relocation value, i.e.,
5157
   the offset of the literal being referenced.  The opnd field is needed
5158
   to determine the range of the immediate field to which the relocation
5159
   applies, so we can determine whether another literal with the same
5160
   value is within range.  The is_null field is true when the relocation
5161
   is being removed (e.g., when an L32R is being removed due to a CALLX
5162
   that is converted to a direct CALL).  */
5163
5164
typedef struct source_reloc_struct source_reloc;
5165
5166
struct source_reloc_struct
5167
{
5168
  asection *source_sec;
5169
  r_reloc r_rel;
5170
  xtensa_opcode opcode;
5171
  int opnd;
5172
  bool is_null;
5173
  bool is_abs_literal;
5174
};
5175
5176
5177
static void
5178
init_source_reloc (source_reloc *reloc,
5179
       asection *source_sec,
5180
       const r_reloc *r_rel,
5181
       xtensa_opcode opcode,
5182
       int opnd,
5183
       bool is_abs_literal)
5184
0
{
5185
0
  reloc->source_sec = source_sec;
5186
0
  reloc->r_rel = *r_rel;
5187
0
  reloc->opcode = opcode;
5188
0
  reloc->opnd = opnd;
5189
0
  reloc->is_null = false;
5190
0
  reloc->is_abs_literal = is_abs_literal;
5191
0
}
5192
5193
5194
/* Find the source_reloc for a particular source offset and relocation
5195
   type.  Note that the array is sorted by _target_ offset, so this is
5196
   just a linear search.  */
5197
5198
static source_reloc *
5199
find_source_reloc (source_reloc *src_relocs,
5200
       int src_count,
5201
       asection *sec,
5202
       Elf_Internal_Rela *irel)
5203
0
{
5204
0
  int i;
5205
5206
0
  for (i = 0; i < src_count; i++)
5207
0
    {
5208
0
      if (src_relocs[i].source_sec == sec
5209
0
    && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
5210
0
    && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
5211
0
        == ELF32_R_TYPE (irel->r_info)))
5212
0
  return &src_relocs[i];
5213
0
    }
5214
5215
0
  return NULL;
5216
0
}
5217
5218
5219
static int
5220
source_reloc_compare (const void *ap, const void *bp)
5221
0
{
5222
0
  const source_reloc *a = (const source_reloc *) ap;
5223
0
  const source_reloc *b = (const source_reloc *) bp;
5224
5225
0
  if (a->r_rel.target_offset != b->r_rel.target_offset)
5226
0
    return (a->r_rel.target_offset - b->r_rel.target_offset);
5227
5228
  /* We don't need to sort on these criteria for correctness,
5229
     but enforcing a more strict ordering prevents unstable qsort
5230
     from behaving differently with different implementations.
5231
     Without the code below we get correct but different results
5232
     on Solaris 2.7 and 2.8.  We would like to always produce the
5233
     same results no matter the host. */
5234
5235
0
  if ((!a->is_null) - (!b->is_null))
5236
0
    return ((!a->is_null) - (!b->is_null));
5237
0
  return internal_reloc_compare (&a->r_rel.rela, &b->r_rel.rela);
5238
0
}
5239
5240

5241
/* Literal values and value hash tables.  */
5242
5243
/* Literals with the same value can be coalesced.  The literal_value
5244
   structure records the value of a literal: the "r_rel" field holds the
5245
   information from the relocation on the literal (if there is one) and
5246
   the "value" field holds the contents of the literal word itself.
5247
5248
   The value_map structure records a literal value along with the
5249
   location of a literal holding that value.  The value_map hash table
5250
   is indexed by the literal value, so that we can quickly check if a
5251
   particular literal value has been seen before and is thus a candidate
5252
   for coalescing.  */
5253
5254
typedef struct literal_value_struct literal_value;
5255
typedef struct value_map_struct value_map;
5256
typedef struct value_map_hash_table_struct value_map_hash_table;
5257
5258
struct literal_value_struct
5259
{
5260
  r_reloc r_rel;
5261
  unsigned long value;
5262
  bool is_abs_literal;
5263
};
5264
5265
struct value_map_struct
5266
{
5267
  literal_value val;      /* The literal value.  */
5268
  r_reloc loc;        /* Location of the literal.  */
5269
  value_map *next;
5270
};
5271
5272
struct value_map_hash_table_struct
5273
{
5274
  unsigned bucket_count;
5275
  value_map **buckets;
5276
  unsigned count;
5277
  bool has_last_loc;
5278
  r_reloc last_loc;
5279
};
5280
5281
5282
static void
5283
init_literal_value (literal_value *lit,
5284
        const r_reloc *r_rel,
5285
        unsigned long value,
5286
        bool is_abs_literal)
5287
0
{
5288
0
  lit->r_rel = *r_rel;
5289
0
  lit->value = value;
5290
0
  lit->is_abs_literal = is_abs_literal;
5291
0
}
5292
5293
5294
/* R_XTENSA_32 and R_XTENSA_32_ABS literals with the same target_offset hold
5295
   the same address, because r_reloc_init folds the in-place addend of
5296
   R_XTENSA_32 into target_offset.  */
5297
5298
static unsigned
5299
literal_reloc_type (const r_reloc *r_rel)
5300
0
{
5301
0
  unsigned r_type = ELF32_R_TYPE (r_rel->rela.r_info);
5302
5303
0
  return r_type == R_XTENSA_32_ABS ? R_XTENSA_32 : r_type;
5304
0
}
5305
5306
5307
/* Return the offset of R_REL's target in a SEC_MERGE section after
5308
   merging, and set *PENTITY to the input offset of the entity that
5309
   merging moves.  Targets in one input section all map into the same
5310
   representative section, so their offsets compare directly.
5311
5312
   Gas reduces a zero addend against a local symbol to a relocation
5313
   against the section symbol, whose addend selects the entity.  Any
5314
   other "symbol + addend" is relative to the symbol: merging moves the
5315
   symbol and the addend is applied afterwards.  */
5316
5317
static bfd_vma
5318
r_reloc_merged_offset (const r_reloc *r_rel, bfd_vma *pentity)
5319
0
{
5320
0
  unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
5321
0
  asection *sec = r_reloc_get_section (r_rel);
5322
0
  bfd_vma symval = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
5323
0
  bfd_vma addend = r_rel->target_offset - symval;
5324
0
  unsigned int symtype = STT_NOTYPE;
5325
5326
0
  if (r_symndx < elf_symtab_hdr (r_rel->abfd).sh_info)
5327
0
    {
5328
0
      Elf_Internal_Sym *isymbuf = retrieve_local_syms (r_rel->abfd);
5329
5330
0
      symtype = ELF_ST_TYPE (isymbuf[r_symndx].st_info);
5331
0
    }
5332
5333
0
  if (symtype == STT_SECTION)
5334
0
    symval += addend;
5335
5336
0
  *pentity = symval;
5337
0
  if (sec->sec_info_type == SEC_INFO_TYPE_MERGE)
5338
0
    symval = _bfd_merged_section_offset (r_rel->abfd, &sec, symval);
5339
5340
0
  if (symtype != STT_SECTION)
5341
0
    symval += addend;
5342
5343
0
  return symval;
5344
0
}
5345
5346
5347
static bool
5348
literal_value_equal (const literal_value *src1,
5349
         const literal_value *src2,
5350
         bool final_static_link)
5351
0
{
5352
0
  struct elf_link_hash_entry *h1, *h2;
5353
5354
0
  if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
5355
0
    return false;
5356
5357
0
  if (r_reloc_is_const (&src1->r_rel))
5358
0
    return (src1->value == src2->value);
5359
5360
0
  if (literal_reloc_type (&src1->r_rel) != literal_reloc_type (&src2->r_rel))
5361
0
    return false;
5362
5363
0
  if (src1->r_rel.target_offset != src2->r_rel.target_offset)
5364
0
    return false;
5365
5366
0
  if (src1->r_rel.virtual_offset != src2->r_rel.virtual_offset)
5367
0
    return false;
5368
5369
0
  if (src1->value != src2->value)
5370
0
    return false;
5371
5372
  /* Now check for the same section (if defined) or the same elf_hash
5373
     (if undefined or weak).  */
5374
0
  h1 = r_reloc_get_hash_entry (&src1->r_rel);
5375
0
  h2 = r_reloc_get_hash_entry (&src2->r_rel);
5376
5377
  /* Keep start_stop literals always unique to avoid dropping it due to them
5378
     having late initialization.
5379
     Now they are equal because initialized with zeroed values.  */
5380
0
  if (h2 && h2->start_stop)
5381
0
      return false;
5382
5383
0
  if (r_reloc_is_defined (&src1->r_rel)
5384
0
      && (final_static_link
5385
0
    || ((!h1 || h1->root.type != bfd_link_hash_defweak)
5386
0
        && (!h2 || h2->root.type != bfd_link_hash_defweak))))
5387
0
    {
5388
0
      asection *sec = r_reloc_get_section (&src1->r_rel);
5389
5390
0
      if (sec != r_reloc_get_section (&src2->r_rel))
5391
0
  return false;
5392
5393
      /* Same input offset is not the same value once SEC_MERGE runs.  */
5394
0
      if ((sec->flags & SEC_MERGE) != 0)
5395
0
  {
5396
0
    bfd_vma entity1, entity2;
5397
5398
0
    if (r_reloc_merged_offset (&src1->r_rel, &entity1)
5399
0
        != r_reloc_merged_offset (&src2->r_rel, &entity2))
5400
0
      return false;
5401
5402
    /* A relocatable link has not merged the section yet, and the
5403
       final link may still move different entities apart.  */
5404
0
    if (sec->sec_info_type != SEC_INFO_TYPE_MERGE
5405
0
        && entity1 != entity2)
5406
0
      return false;
5407
0
  }
5408
0
    }
5409
0
  else
5410
0
    {
5411
      /* Require that the hash entries (i.e., symbols) be identical.  */
5412
0
      if (h1 != h2 || h1 == 0)
5413
0
  return false;
5414
0
    }
5415
5416
0
  if (src1->is_abs_literal != src2->is_abs_literal)
5417
0
    return false;
5418
5419
0
  return true;
5420
0
}
5421
5422
5423
/* Must be power of 2.  */
5424
0
#define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
5425
5426
static value_map_hash_table *
5427
value_map_hash_table_init (void)
5428
0
{
5429
0
  value_map_hash_table *values;
5430
5431
0
  values = (value_map_hash_table *)
5432
0
    bfd_zmalloc (sizeof (value_map_hash_table));
5433
0
  values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
5434
0
  values->count = 0;
5435
0
  values->buckets = (value_map **)
5436
0
    bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
5437
0
  if (values->buckets == NULL)
5438
0
    {
5439
0
      free (values);
5440
0
      return NULL;
5441
0
    }
5442
0
  values->has_last_loc = false;
5443
5444
0
  return values;
5445
0
}
5446
5447
5448
static void
5449
value_map_hash_table_delete (value_map_hash_table *table)
5450
0
{
5451
0
  free (table->buckets);
5452
0
  free (table);
5453
0
}
5454
5455
5456
static unsigned
5457
hash_bfd_vma (bfd_vma val)
5458
0
{
5459
0
  return (val >> 2) + (val >> 10);
5460
0
}
5461
5462
5463
static unsigned
5464
literal_value_hash (const literal_value *src)
5465
0
{
5466
0
  unsigned hash_val;
5467
5468
0
  hash_val = hash_bfd_vma (src->value);
5469
0
  if (!r_reloc_is_const (&src->r_rel))
5470
0
    {
5471
0
      void *sec_or_hash;
5472
5473
0
      hash_val += hash_bfd_vma (src->is_abs_literal * 1000);
5474
0
      hash_val += hash_bfd_vma (src->r_rel.target_offset);
5475
0
      hash_val += hash_bfd_vma (src->r_rel.virtual_offset);
5476
5477
      /* Now check for the same section and the same elf_hash.  */
5478
0
      if (r_reloc_is_defined (&src->r_rel))
5479
0
  sec_or_hash = r_reloc_get_section (&src->r_rel);
5480
0
      else
5481
0
  sec_or_hash = r_reloc_get_hash_entry (&src->r_rel);
5482
0
      hash_val += hash_bfd_vma ((bfd_vma) (size_t) sec_or_hash);
5483
0
    }
5484
0
  return hash_val;
5485
0
}
5486
5487
5488
/* Check if the specified literal_value has been seen before.  */
5489
5490
static value_map *
5491
value_map_get_cached_value (value_map_hash_table *map,
5492
          const literal_value *val,
5493
          bool final_static_link)
5494
0
{
5495
0
  value_map *map_e;
5496
0
  value_map *bucket;
5497
0
  unsigned idx;
5498
5499
0
  idx = literal_value_hash (val);
5500
0
  idx = idx & (map->bucket_count - 1);
5501
0
  bucket = map->buckets[idx];
5502
0
  for (map_e = bucket; map_e; map_e = map_e->next)
5503
0
    {
5504
0
      if (literal_value_equal (&map_e->val, val, final_static_link))
5505
0
  return map_e;
5506
0
    }
5507
0
  return NULL;
5508
0
}
5509
5510
5511
/* Record a new literal value.  It is illegal to call this if VALUE
5512
   already has an entry here.  */
5513
5514
static value_map *
5515
add_value_map (value_map_hash_table *map,
5516
         const literal_value *val,
5517
         const r_reloc *loc,
5518
         bool final_static_link)
5519
0
{
5520
0
  value_map **bucket_p;
5521
0
  unsigned idx;
5522
5523
0
  value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
5524
0
  if (val_e == NULL)
5525
0
    {
5526
0
      bfd_set_error (bfd_error_no_memory);
5527
0
      return NULL;
5528
0
    }
5529
5530
0
  BFD_ASSERT (!value_map_get_cached_value (map, val, final_static_link));
5531
0
  val_e->val = *val;
5532
0
  val_e->loc = *loc;
5533
5534
0
  idx = literal_value_hash (val);
5535
0
  idx = idx & (map->bucket_count - 1);
5536
0
  bucket_p = &map->buckets[idx];
5537
5538
0
  val_e->next = *bucket_p;
5539
0
  *bucket_p = val_e;
5540
0
  map->count++;
5541
  /* FIXME: Consider resizing the hash table if we get too many entries.  */
5542
5543
0
  return val_e;
5544
0
}
5545
5546

5547
/* Lists of text actions (ta_) for narrowing, widening, longcall
5548
   conversion, space fill, code & literal removal, etc.  */
5549
5550
/* The following text actions are generated:
5551
5552
   "ta_remove_insn"     remove an instruction or instructions
5553
   "ta_remove_longcall"     convert longcall to call
5554
   "ta_convert_longcall"    convert longcall to nop/call
5555
   "ta_narrow_insn"     narrow a wide instruction
5556
   "ta_widen"       widen a narrow instruction
5557
   "ta_fill"        add fill or remove fill
5558
      removed < 0 is a fill; branches to the fill address will be
5559
  changed to address + fill size (e.g., address - removed)
5560
      removed >= 0 branches to the fill address will stay unchanged
5561
   "ta_remove_literal"      remove a literal; this action is
5562
          indicated when a literal is removed
5563
          or replaced.
5564
   "ta_add_literal"     insert a new literal; this action is
5565
          indicated when a literal has been moved.
5566
          It may use a virtual_offset because
5567
          multiple literals can be placed at the
5568
          same location.
5569
5570
   For each of these text actions, we also record the number of bytes
5571
   removed by performing the text action.  In the case of a "ta_widen"
5572
   or a "ta_fill" that adds space, the removed_bytes will be negative.  */
5573
5574
typedef struct text_action_struct text_action;
5575
typedef struct text_action_list_struct text_action_list;
5576
typedef enum text_action_enum_t text_action_t;
5577
5578
enum text_action_enum_t
5579
{
5580
  ta_none,
5581
  ta_remove_insn,  /* removed = -size */
5582
  ta_remove_longcall,  /* removed = -size */
5583
  ta_convert_longcall,   /* removed = 0 */
5584
  ta_narrow_insn,  /* removed = -1 */
5585
  ta_widen_insn,   /* removed = +1 */
5586
  ta_fill,     /* removed = +size */
5587
  ta_remove_literal,
5588
  ta_add_literal
5589
};
5590
5591
5592
/* Structure for a text action record.  */
5593
struct text_action_struct
5594
{
5595
  text_action_t action;
5596
  asection *sec;  /* Optional */
5597
  bfd_vma offset;
5598
  bfd_vma virtual_offset;  /* Zero except for adding literals.  */
5599
  int removed_bytes;
5600
  literal_value value;  /* Only valid when adding literals.  */
5601
};
5602
5603
struct removal_by_action_entry_struct
5604
{
5605
  bfd_vma offset;
5606
  int removed;
5607
  int eq_removed;
5608
  int eq_removed_before_fill;
5609
};
5610
typedef struct removal_by_action_entry_struct removal_by_action_entry;
5611
5612
struct removal_by_action_map_struct
5613
{
5614
  unsigned n_entries;
5615
  removal_by_action_entry *entry;
5616
};
5617
typedef struct removal_by_action_map_struct removal_by_action_map;
5618
5619
5620
/* List of all of the actions taken on a text section.  */
5621
struct text_action_list_struct
5622
{
5623
  unsigned count;
5624
  splay_tree tree;
5625
  removal_by_action_map map;
5626
};
5627
5628
5629
static text_action *
5630
find_fill_action (text_action_list *l, asection *sec, bfd_vma offset)
5631
0
{
5632
0
  text_action a;
5633
5634
  /* It is not necessary to fill at the end of a section.  */
5635
0
  if (sec->size == offset)
5636
0
    return NULL;
5637
5638
0
  a.offset = offset;
5639
0
  a.action = ta_fill;
5640
5641
0
  splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5642
0
  if (node)
5643
0
    return (text_action *)node->value;
5644
0
  return NULL;
5645
0
}
5646
5647
5648
static int
5649
compute_removed_action_diff (const text_action *ta,
5650
           asection *sec,
5651
           bfd_vma offset,
5652
           int removed,
5653
           int removable_space)
5654
0
{
5655
0
  int new_removed;
5656
0
  int current_removed = 0;
5657
5658
0
  if (ta)
5659
0
    current_removed = ta->removed_bytes;
5660
5661
0
  BFD_ASSERT (ta == NULL || ta->offset == offset);
5662
0
  BFD_ASSERT (ta == NULL || ta->action == ta_fill);
5663
5664
  /* It is not necessary to fill at the end of a section.  Clean this up.  */
5665
0
  if (sec->size == offset)
5666
0
    new_removed = removable_space - 0;
5667
0
  else
5668
0
    {
5669
0
      int space;
5670
0
      int added = -removed - current_removed;
5671
      /* Ignore multiples of the section alignment.  */
5672
0
      added = ((1 << sec->alignment_power) - 1) & added;
5673
0
      new_removed = (-added);
5674
5675
      /* Modify for removable.  */
5676
0
      space = removable_space - new_removed;
5677
0
      new_removed = (removable_space
5678
0
         - (((1 << sec->alignment_power) - 1) & space));
5679
0
    }
5680
0
  return (new_removed - current_removed);
5681
0
}
5682
5683
5684
static void
5685
adjust_fill_action (text_action *ta, int fill_diff)
5686
0
{
5687
0
  ta->removed_bytes += fill_diff;
5688
0
}
5689
5690
5691
static int
5692
text_action_compare (splay_tree_key a, splay_tree_key b)
5693
0
{
5694
0
  text_action *pa = (text_action *)a;
5695
0
  text_action *pb = (text_action *)b;
5696
0
  static const int action_priority[] =
5697
0
    {
5698
0
      [ta_fill] = 0,
5699
0
      [ta_none] = 1,
5700
0
      [ta_convert_longcall] = 2,
5701
0
      [ta_narrow_insn] = 3,
5702
0
      [ta_remove_insn] = 4,
5703
0
      [ta_remove_longcall] = 5,
5704
0
      [ta_remove_literal] = 6,
5705
0
      [ta_widen_insn] = 7,
5706
0
      [ta_add_literal] = 8,
5707
0
    };
5708
5709
0
  if (pa->offset == pb->offset)
5710
0
    {
5711
0
      if (pa->action == pb->action)
5712
0
    return 0;
5713
0
      return action_priority[pa->action] - action_priority[pb->action];
5714
0
    }
5715
0
  else
5716
0
    return pa->offset < pb->offset ? -1 : 1;
5717
0
}
5718
5719
static text_action *
5720
action_first (text_action_list *action_list)
5721
0
{
5722
0
  splay_tree_node node = splay_tree_min (action_list->tree);
5723
0
  return node ? (text_action *)node->value : NULL;
5724
0
}
5725
5726
static text_action *
5727
action_next (text_action_list *action_list, text_action *action)
5728
0
{
5729
0
  splay_tree_node node = splay_tree_successor (action_list->tree,
5730
0
                 (splay_tree_key)action);
5731
0
  return node ? (text_action *)node->value : NULL;
5732
0
}
5733
5734
/* Add a modification action to the text.  For the case of adding or
5735
   removing space, modify any current fill and assume that
5736
   "unreachable_space" bytes can be freely contracted.  Note that a
5737
   negative removed value is a fill.  */
5738
5739
static void
5740
text_action_add (text_action_list *l,
5741
     text_action_t action,
5742
     asection *sec,
5743
     bfd_vma offset,
5744
     int removed)
5745
0
{
5746
0
  text_action *ta;
5747
0
  text_action a;
5748
5749
  /* It is not necessary to fill at the end of a section.  */
5750
0
  if (action == ta_fill && sec->size == offset)
5751
0
    return;
5752
5753
  /* It is not necessary to fill 0 bytes.  */
5754
0
  if (action == ta_fill && removed == 0)
5755
0
    return;
5756
5757
0
  a.action = action;
5758
0
  a.offset = offset;
5759
5760
0
  if (action == ta_fill)
5761
0
    {
5762
0
      splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5763
5764
0
      if (node)
5765
0
  {
5766
0
    ta = (text_action *)node->value;
5767
0
    ta->removed_bytes += removed;
5768
0
    return;
5769
0
  }
5770
0
    }
5771
0
  else
5772
0
    BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)&a) == NULL);
5773
5774
0
  ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5775
0
  ta->action = action;
5776
0
  ta->sec = sec;
5777
0
  ta->offset = offset;
5778
0
  ta->removed_bytes = removed;
5779
0
  splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5780
0
  ++l->count;
5781
0
}
5782
5783
5784
static void
5785
text_action_add_literal (text_action_list *l,
5786
       text_action_t action,
5787
       const r_reloc *loc,
5788
       const literal_value *value,
5789
       int removed)
5790
0
{
5791
0
  text_action *ta;
5792
0
  asection *sec = r_reloc_get_section (loc);
5793
0
  bfd_vma offset = loc->target_offset;
5794
0
  bfd_vma virtual_offset = loc->virtual_offset;
5795
5796
0
  BFD_ASSERT (action == ta_add_literal);
5797
5798
  /* Create a new record and fill it up.  */
5799
0
  ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5800
0
  ta->action = action;
5801
0
  ta->sec = sec;
5802
0
  ta->offset = offset;
5803
0
  ta->virtual_offset = virtual_offset;
5804
0
  ta->value = *value;
5805
0
  ta->removed_bytes = removed;
5806
5807
0
  BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)ta) == NULL);
5808
0
  splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5809
0
  ++l->count;
5810
0
}
5811
5812
5813
/* Find the total offset adjustment for the relaxations specified by
5814
   text_actions, beginning from a particular starting action.  This is
5815
   typically used from offset_with_removed_text to search an entire list of
5816
   actions, but it may also be called directly when adjusting adjacent offsets
5817
   so that each search may begin where the previous one left off.  */
5818
5819
static int
5820
removed_by_actions (text_action_list *action_list,
5821
        text_action **p_start_action,
5822
        bfd_vma offset,
5823
        bool before_fill)
5824
0
{
5825
0
  text_action *r;
5826
0
  int removed = 0;
5827
5828
0
  r = *p_start_action;
5829
0
  if (r)
5830
0
    {
5831
0
      splay_tree_node node = splay_tree_lookup (action_list->tree,
5832
0
            (splay_tree_key)r);
5833
0
      BFD_ASSERT (node != NULL && r == (text_action *)node->value);
5834
0
    }
5835
5836
0
  while (r)
5837
0
    {
5838
0
      if (r->offset > offset)
5839
0
  break;
5840
5841
0
      if (r->offset == offset
5842
0
    && (before_fill || r->action != ta_fill || r->removed_bytes >= 0))
5843
0
  break;
5844
5845
0
      removed += r->removed_bytes;
5846
5847
0
      r = action_next (action_list, r);
5848
0
    }
5849
5850
0
  *p_start_action = r;
5851
0
  return removed;
5852
0
}
5853
5854
5855
static bfd_vma
5856
offset_with_removed_text (text_action_list *action_list, bfd_vma offset)
5857
0
{
5858
0
  text_action *r = action_first (action_list);
5859
5860
0
  return offset - removed_by_actions (action_list, &r, offset, false);
5861
0
}
5862
5863
5864
static unsigned
5865
action_list_count (text_action_list *action_list)
5866
0
{
5867
0
  return action_list->count;
5868
0
}
5869
5870
typedef struct map_action_fn_context_struct map_action_fn_context;
5871
struct map_action_fn_context_struct
5872
{
5873
  int removed;
5874
  removal_by_action_map map;
5875
  bool eq_complete;
5876
};
5877
5878
static int
5879
map_action_fn (splay_tree_node node, void *p)
5880
0
{
5881
0
  map_action_fn_context *ctx = p;
5882
0
  text_action *r = (text_action *)node->value;
5883
0
  removal_by_action_entry *ientry = ctx->map.entry + ctx->map.n_entries;
5884
5885
0
  if (ctx->map.n_entries && (ientry - 1)->offset == r->offset)
5886
0
    {
5887
0
      --ientry;
5888
0
    }
5889
0
  else
5890
0
    {
5891
0
      ++ctx->map.n_entries;
5892
0
      ctx->eq_complete = false;
5893
0
      ientry->offset = r->offset;
5894
0
      ientry->eq_removed_before_fill = ctx->removed;
5895
0
    }
5896
5897
0
  if (!ctx->eq_complete)
5898
0
    {
5899
0
      if (r->action != ta_fill || r->removed_bytes >= 0)
5900
0
  {
5901
0
    ientry->eq_removed = ctx->removed;
5902
0
    ctx->eq_complete = true;
5903
0
  }
5904
0
      else
5905
0
  ientry->eq_removed = ctx->removed + r->removed_bytes;
5906
0
    }
5907
5908
0
  ctx->removed += r->removed_bytes;
5909
0
  ientry->removed = ctx->removed;
5910
0
  return 0;
5911
0
}
5912
5913
static void
5914
map_removal_by_action (text_action_list *action_list)
5915
0
{
5916
0
  map_action_fn_context ctx;
5917
5918
0
  ctx.removed = 0;
5919
0
  ctx.map.n_entries = 0;
5920
0
  ctx.map.entry = bfd_malloc (action_list_count (action_list) *
5921
0
            sizeof (removal_by_action_entry));
5922
0
  ctx.eq_complete = false;
5923
5924
0
  splay_tree_foreach (action_list->tree, map_action_fn, &ctx);
5925
0
  action_list->map = ctx.map;
5926
0
}
5927
5928
static int
5929
removed_by_actions_map (text_action_list *action_list, bfd_vma offset,
5930
      bool before_fill)
5931
0
{
5932
0
  unsigned a, b;
5933
5934
0
  if (!action_list->map.entry)
5935
0
    map_removal_by_action (action_list);
5936
5937
0
  if (!action_list->map.n_entries)
5938
0
    return 0;
5939
5940
0
  a = 0;
5941
0
  b = action_list->map.n_entries;
5942
5943
0
  while (b - a > 1)
5944
0
    {
5945
0
      unsigned c = (a + b) / 2;
5946
5947
0
      if (action_list->map.entry[c].offset <= offset)
5948
0
  a = c;
5949
0
      else
5950
0
  b = c;
5951
0
    }
5952
5953
0
  if (action_list->map.entry[a].offset < offset)
5954
0
    {
5955
0
      return action_list->map.entry[a].removed;
5956
0
    }
5957
0
  else if (action_list->map.entry[a].offset == offset)
5958
0
    {
5959
0
      return before_fill ?
5960
0
  action_list->map.entry[a].eq_removed_before_fill :
5961
0
  action_list->map.entry[a].eq_removed;
5962
0
    }
5963
0
  else
5964
0
    {
5965
0
      return 0;
5966
0
    }
5967
0
}
5968
5969
static bfd_vma
5970
offset_with_removed_text_map (text_action_list *action_list, bfd_vma offset)
5971
0
{
5972
0
  int removed = removed_by_actions_map (action_list, offset, false);
5973
0
  return offset - removed;
5974
0
}
5975
5976
5977
/* The find_insn_action routine will only find non-fill actions.  */
5978
5979
static text_action *
5980
find_insn_action (text_action_list *action_list, bfd_vma offset)
5981
0
{
5982
0
  static const text_action_t action[] =
5983
0
    {
5984
0
      ta_convert_longcall,
5985
0
      ta_remove_longcall,
5986
0
      ta_widen_insn,
5987
0
      ta_narrow_insn,
5988
0
      ta_remove_insn,
5989
0
    };
5990
0
  text_action a;
5991
0
  unsigned i;
5992
5993
0
  a.offset = offset;
5994
0
  for (i = 0; i < sizeof (action) / sizeof (*action); ++i)
5995
0
    {
5996
0
      splay_tree_node node;
5997
5998
0
      a.action = action[i];
5999
0
      node = splay_tree_lookup (action_list->tree, (splay_tree_key)&a);
6000
0
      if (node)
6001
0
  return (text_action *)node->value;
6002
0
    }
6003
0
  return NULL;
6004
0
}
6005
6006
6007
#if DEBUG
6008
6009
static void
6010
print_action (FILE *fp, text_action *r)
6011
{
6012
  const char *t = "unknown";
6013
  switch (r->action)
6014
    {
6015
    case ta_remove_insn:
6016
      t = "remove_insn"; break;
6017
    case ta_remove_longcall:
6018
      t = "remove_longcall"; break;
6019
    case ta_convert_longcall:
6020
      t = "convert_longcall"; break;
6021
    case ta_narrow_insn:
6022
      t = "narrow_insn"; break;
6023
    case ta_widen_insn:
6024
      t = "widen_insn"; break;
6025
    case ta_fill:
6026
      t = "fill"; break;
6027
    case ta_none:
6028
      t = "none"; break;
6029
    case ta_remove_literal:
6030
      t = "remove_literal"; break;
6031
    case ta_add_literal:
6032
      t = "add_literal"; break;
6033
    }
6034
6035
  fprintf (fp, "%s: %s[0x%lx] \"%s\" %d\n",
6036
     r->sec->owner->filename,
6037
     r->sec->name, (unsigned long) r->offset, t, r->removed_bytes);
6038
}
6039
6040
static int
6041
print_action_list_fn (splay_tree_node node, void *p)
6042
{
6043
  text_action *r = (text_action *)node->value;
6044
6045
  print_action (p, r);
6046
  return 0;
6047
}
6048
6049
static void
6050
print_action_list (FILE *fp, text_action_list *action_list)
6051
{
6052
  fprintf (fp, "Text Action\n");
6053
  splay_tree_foreach (action_list->tree, print_action_list_fn, fp);
6054
}
6055
6056
#endif /* DEBUG */
6057
6058

6059
/* Lists of literals being coalesced or removed.  */
6060
6061
/* In the usual case, the literal identified by "from" is being
6062
   coalesced with another literal identified by "to".  If the literal is
6063
   unused and is being removed altogether, "to.abfd" will be NULL.
6064
   The removed_literal entries are kept on a per-section list, sorted
6065
   by the "from" offset field.  */
6066
6067
typedef struct removed_literal_struct removed_literal;
6068
typedef struct removed_literal_map_entry_struct removed_literal_map_entry;
6069
typedef struct removed_literal_list_struct removed_literal_list;
6070
6071
struct removed_literal_struct
6072
{
6073
  r_reloc from;
6074
  r_reloc to;
6075
  removed_literal *next;
6076
};
6077
6078
struct removed_literal_map_entry_struct
6079
{
6080
  bfd_vma addr;
6081
  removed_literal *literal;
6082
};
6083
6084
struct removed_literal_list_struct
6085
{
6086
  removed_literal *head;
6087
  removed_literal *tail;
6088
6089
  unsigned n_map;
6090
  removed_literal_map_entry *map;
6091
};
6092
6093
6094
/* Record that the literal at "from" is being removed.  If "to" is not
6095
   NULL, the "from" literal is being coalesced with the "to" literal.  */
6096
6097
static void
6098
add_removed_literal (removed_literal_list *removed_list,
6099
         const r_reloc *from,
6100
         const r_reloc *to)
6101
0
{
6102
0
  removed_literal *r, *new_r, *next_r;
6103
6104
0
  new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
6105
6106
0
  new_r->from = *from;
6107
0
  if (to)
6108
0
    new_r->to = *to;
6109
0
  else
6110
0
    new_r->to.abfd = NULL;
6111
0
  new_r->next = NULL;
6112
6113
0
  r = removed_list->head;
6114
0
  if (r == NULL)
6115
0
    {
6116
0
      removed_list->head = new_r;
6117
0
      removed_list->tail = new_r;
6118
0
    }
6119
  /* Special check for common case of append.  */
6120
0
  else if (removed_list->tail->from.target_offset < from->target_offset)
6121
0
    {
6122
0
      removed_list->tail->next = new_r;
6123
0
      removed_list->tail = new_r;
6124
0
    }
6125
0
  else
6126
0
    {
6127
0
      while (r->from.target_offset < from->target_offset && r->next)
6128
0
  {
6129
0
    r = r->next;
6130
0
  }
6131
0
      next_r = r->next;
6132
0
      r->next = new_r;
6133
0
      new_r->next = next_r;
6134
0
      if (next_r == NULL)
6135
0
  removed_list->tail = new_r;
6136
0
    }
6137
0
}
6138
6139
static void
6140
map_removed_literal (removed_literal_list *removed_list)
6141
0
{
6142
0
  unsigned n_map = 0;
6143
0
  unsigned i;
6144
0
  removed_literal_map_entry *map = NULL;
6145
0
  removed_literal *r = removed_list->head;
6146
6147
0
  for (i = 0; r; ++i, r = r->next)
6148
0
    {
6149
0
      if (i == n_map)
6150
0
  {
6151
0
    n_map = (n_map * 2) + 2;
6152
0
    map = bfd_realloc (map, n_map * sizeof (*map));
6153
0
  }
6154
0
      map[i].addr = r->from.target_offset;
6155
0
      map[i].literal = r;
6156
0
    }
6157
0
  removed_list->map = map;
6158
0
  removed_list->n_map = i;
6159
0
}
6160
6161
static int
6162
removed_literal_compare (const void *a, const void *b)
6163
0
{
6164
0
  const bfd_vma *key = a;
6165
0
  const removed_literal_map_entry *memb = b;
6166
6167
0
  if (*key == memb->addr)
6168
0
    return 0;
6169
0
  else
6170
0
    return *key < memb->addr ? -1 : 1;
6171
0
}
6172
6173
/* Check if the list of removed literals contains an entry for the
6174
   given address.  Return the entry if found.  */
6175
6176
static removed_literal *
6177
find_removed_literal (removed_literal_list *removed_list, bfd_vma addr)
6178
0
{
6179
0
  removed_literal_map_entry *p;
6180
0
  removed_literal *r = NULL;
6181
6182
0
  if (removed_list->map == NULL)
6183
0
    map_removed_literal (removed_list);
6184
6185
0
  if (removed_list->map != NULL)
6186
0
    {
6187
0
      p = bsearch (&addr, removed_list->map, removed_list->n_map,
6188
0
       sizeof (*removed_list->map), removed_literal_compare);
6189
0
      if (p)
6190
0
  {
6191
0
    while (p != removed_list->map && (p - 1)->addr == addr)
6192
0
      --p;
6193
0
    r = p->literal;
6194
0
  }
6195
0
    }
6196
0
  return r;
6197
0
}
6198
6199
6200
#if DEBUG
6201
6202
static void
6203
print_removed_literals (FILE *fp, removed_literal_list *removed_list)
6204
{
6205
  removed_literal *r;
6206
  r = removed_list->head;
6207
  if (r)
6208
    fprintf (fp, "Removed Literals\n");
6209
  for (; r != NULL; r = r->next)
6210
    {
6211
      print_r_reloc (fp, &r->from);
6212
      fprintf (fp, " => ");
6213
      if (r->to.abfd == NULL)
6214
  fprintf (fp, "REMOVED");
6215
      else
6216
  print_r_reloc (fp, &r->to);
6217
      fprintf (fp, "\n");
6218
    }
6219
}
6220
6221
#endif /* DEBUG */
6222
6223

6224
/* Per-section data for relaxation.  */
6225
6226
typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
6227
6228
struct xtensa_relax_info_struct
6229
{
6230
  bool is_relaxable_literal_section;
6231
  bool is_relaxable_asm_section;
6232
  int visited;        /* Number of times visited.  */
6233
6234
  source_reloc *src_relocs;   /* Array[src_count].  */
6235
  int src_count;
6236
  int src_next;       /* Next src_relocs entry to assign.  */
6237
6238
  removed_literal_list removed_list;
6239
  text_action_list action_list;
6240
6241
  reloc_bfd_fix *fix_list;
6242
  reloc_bfd_fix *fix_array;
6243
  unsigned fix_array_count;
6244
6245
  /* Support for expanding the reloc array that is stored
6246
     in the section structure.  If the relocations have been
6247
     reallocated, the newly allocated relocations will be referenced
6248
     here along with the actual size allocated.  The relocation
6249
     count will always be found in the section structure.  */
6250
  Elf_Internal_Rela *allocated_relocs;
6251
  unsigned relocs_count;
6252
  unsigned allocated_relocs_count;
6253
};
6254
6255
struct elf_xtensa_section_data
6256
{
6257
  struct bfd_elf_section_data elf;
6258
  xtensa_relax_info relax_info;
6259
};
6260
6261
6262
static bool
6263
elf_xtensa_new_section_hook (bfd *abfd, asection *sec)
6264
82.8k
{
6265
82.8k
  struct elf_xtensa_section_data *sdata;
6266
6267
82.8k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
6268
82.8k
  if (sdata == NULL)
6269
0
    return false;
6270
82.8k
  sec->used_by_bfd = sdata;
6271
6272
82.8k
  return _bfd_elf_new_section_hook (abfd, sec);
6273
82.8k
}
6274
6275
6276
static xtensa_relax_info *
6277
get_xtensa_relax_info (asection *sec)
6278
0
{
6279
0
  struct elf_xtensa_section_data *section_data;
6280
6281
  /* No info available if no section or if it is an output section.  */
6282
0
  if (!sec || sec == sec->output_section)
6283
0
    return NULL;
6284
6285
0
  section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
6286
0
  return &section_data->relax_info;
6287
0
}
6288
6289
6290
static void
6291
init_xtensa_relax_info (asection *sec)
6292
0
{
6293
0
  xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6294
6295
0
  relax_info->is_relaxable_literal_section = false;
6296
0
  relax_info->is_relaxable_asm_section = false;
6297
0
  relax_info->visited = 0;
6298
6299
0
  relax_info->src_relocs = NULL;
6300
0
  relax_info->src_count = 0;
6301
0
  relax_info->src_next = 0;
6302
6303
0
  relax_info->removed_list.head = NULL;
6304
0
  relax_info->removed_list.tail = NULL;
6305
6306
0
  relax_info->action_list.tree = splay_tree_new (text_action_compare,
6307
0
             NULL, NULL);
6308
0
  relax_info->action_list.map.n_entries = 0;
6309
0
  relax_info->action_list.map.entry = NULL;
6310
6311
0
  relax_info->fix_list = NULL;
6312
0
  relax_info->fix_array = NULL;
6313
0
  relax_info->fix_array_count = 0;
6314
6315
0
  relax_info->allocated_relocs = NULL;
6316
0
  relax_info->relocs_count = 0;
6317
0
  relax_info->allocated_relocs_count = 0;
6318
0
}
6319
6320

6321
/* Coalescing literals may require a relocation to refer to a section in
6322
   a different input file, but the standard relocation information
6323
   cannot express that.  Instead, the reloc_bfd_fix structures are used
6324
   to "fix" the relocations that refer to sections in other input files.
6325
   These structures are kept on per-section lists.  The "src_type" field
6326
   records the relocation type in case there are multiple relocations on
6327
   the same location.  FIXME: This is ugly; an alternative might be to
6328
   add new symbols with the "owner" field to some other input file.  */
6329
6330
struct reloc_bfd_fix_struct
6331
{
6332
  asection *src_sec;
6333
  bfd_vma src_offset;
6334
  unsigned src_type;      /* Relocation type.  */
6335
6336
  asection *target_sec;
6337
  bfd_vma target_offset;
6338
  bool translated;
6339
6340
  reloc_bfd_fix *next;
6341
};
6342
6343
6344
static reloc_bfd_fix *
6345
reloc_bfd_fix_init (asection *src_sec,
6346
        bfd_vma src_offset,
6347
        unsigned src_type,
6348
        asection *target_sec,
6349
        bfd_vma target_offset,
6350
        bool translated)
6351
0
{
6352
0
  reloc_bfd_fix *fix;
6353
6354
0
  fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
6355
0
  fix->src_sec = src_sec;
6356
0
  fix->src_offset = src_offset;
6357
0
  fix->src_type = src_type;
6358
0
  fix->target_sec = target_sec;
6359
0
  fix->target_offset = target_offset;
6360
0
  fix->translated = translated;
6361
6362
0
  return fix;
6363
0
}
6364
6365
6366
static void
6367
add_fix (asection *src_sec, reloc_bfd_fix *fix)
6368
0
{
6369
0
  xtensa_relax_info *relax_info;
6370
6371
0
  relax_info = get_xtensa_relax_info (src_sec);
6372
0
  fix->next = relax_info->fix_list;
6373
0
  relax_info->fix_list = fix;
6374
0
}
6375
6376
6377
static int
6378
fix_compare (const void *ap, const void *bp)
6379
0
{
6380
0
  const reloc_bfd_fix *a = (const reloc_bfd_fix *) ap;
6381
0
  const reloc_bfd_fix *b = (const reloc_bfd_fix *) bp;
6382
6383
0
  if (a->src_offset != b->src_offset)
6384
0
    return (a->src_offset - b->src_offset);
6385
0
  return (a->src_type - b->src_type);
6386
0
}
6387
6388
6389
static void
6390
cache_fix_array (asection *sec)
6391
0
{
6392
0
  unsigned i, count = 0;
6393
0
  reloc_bfd_fix *r;
6394
0
  xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6395
6396
0
  if (relax_info == NULL)
6397
0
    return;
6398
0
  if (relax_info->fix_list == NULL)
6399
0
    return;
6400
6401
0
  for (r = relax_info->fix_list; r != NULL; r = r->next)
6402
0
    count++;
6403
6404
0
  relax_info->fix_array =
6405
0
    (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix) * count);
6406
0
  relax_info->fix_array_count = count;
6407
6408
0
  r = relax_info->fix_list;
6409
0
  for (i = 0; i < count; i++, r = r->next)
6410
0
    {
6411
0
      relax_info->fix_array[count - 1 - i] = *r;
6412
0
      relax_info->fix_array[count - 1 - i].next = NULL;
6413
0
    }
6414
6415
0
  qsort (relax_info->fix_array, relax_info->fix_array_count,
6416
0
   sizeof (reloc_bfd_fix), fix_compare);
6417
0
}
6418
6419
6420
static reloc_bfd_fix *
6421
get_bfd_fix (asection *sec, bfd_vma offset, unsigned type)
6422
0
{
6423
0
  xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6424
0
  reloc_bfd_fix *rv;
6425
0
  reloc_bfd_fix key;
6426
6427
0
  if (relax_info == NULL)
6428
0
    return NULL;
6429
0
  if (relax_info->fix_list == NULL)
6430
0
    return NULL;
6431
6432
0
  if (relax_info->fix_array == NULL)
6433
0
    cache_fix_array (sec);
6434
6435
0
  key.src_offset = offset;
6436
0
  key.src_type = type;
6437
0
  rv = bsearch (&key, relax_info->fix_array,  relax_info->fix_array_count,
6438
0
    sizeof (reloc_bfd_fix), fix_compare);
6439
0
  return rv;
6440
0
}
6441
6442

6443
/* Section caching.  */
6444
6445
typedef struct section_cache_struct section_cache_t;
6446
6447
struct section_cache_struct
6448
{
6449
  asection *sec;
6450
6451
  bfd_byte *contents;   /* Cache of the section contents.  */
6452
  bfd_size_type content_length;
6453
6454
  property_table_entry *ptbl; /* Cache of the section property table.  */
6455
  unsigned pte_count;
6456
6457
  Elf_Internal_Rela *relocs;  /* Cache of the section relocations.  */
6458
  unsigned reloc_count;
6459
};
6460
6461
6462
static void
6463
init_section_cache (section_cache_t *sec_cache)
6464
0
{
6465
0
  memset (sec_cache, 0, sizeof (*sec_cache));
6466
0
}
6467
6468
6469
static void
6470
free_section_cache (section_cache_t *sec_cache)
6471
0
{
6472
0
  if (sec_cache->sec)
6473
0
    {
6474
0
      release_contents (sec_cache->sec, sec_cache->contents);
6475
0
      release_internal_relocs (sec_cache->sec, sec_cache->relocs);
6476
0
      free (sec_cache->ptbl);
6477
0
    }
6478
0
}
6479
6480
6481
static bool
6482
section_cache_section (section_cache_t *sec_cache,
6483
           asection *sec,
6484
           struct bfd_link_info *link_info)
6485
0
{
6486
0
  bfd *abfd;
6487
0
  property_table_entry *prop_table = NULL;
6488
0
  int ptblsize = 0;
6489
0
  bfd_byte *contents = NULL;
6490
0
  Elf_Internal_Rela *internal_relocs = NULL;
6491
0
  bfd_size_type sec_size;
6492
6493
0
  if (sec == NULL)
6494
0
    return false;
6495
0
  if (sec == sec_cache->sec)
6496
0
    return true;
6497
6498
0
  abfd = sec->owner;
6499
0
  sec_size = bfd_get_section_limit (abfd, sec);
6500
6501
  /* Get the contents.  */
6502
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6503
0
  if (contents == NULL && sec_size != 0)
6504
0
    goto err;
6505
6506
  /* Get the relocations.  */
6507
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
6508
0
                link_info->keep_memory);
6509
6510
  /* Get the entry table.  */
6511
0
  ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
6512
0
          XTENSA_PROP_SEC_NAME, false);
6513
0
  if (ptblsize < 0)
6514
0
    goto err;
6515
6516
  /* Fill in the new section cache.  */
6517
0
  free_section_cache (sec_cache);
6518
0
  init_section_cache (sec_cache);
6519
6520
0
  sec_cache->sec = sec;
6521
0
  sec_cache->contents = contents;
6522
0
  sec_cache->content_length = sec_size;
6523
0
  sec_cache->relocs = internal_relocs;
6524
0
  sec_cache->reloc_count = sec->reloc_count;
6525
0
  sec_cache->pte_count = ptblsize;
6526
0
  sec_cache->ptbl = prop_table;
6527
6528
0
  return true;
6529
6530
0
 err:
6531
0
  release_contents (sec, contents);
6532
0
  release_internal_relocs (sec, internal_relocs);
6533
0
  free (prop_table);
6534
0
  return false;
6535
0
}
6536
6537

6538
/* Extended basic blocks.  */
6539
6540
/* An ebb_struct represents an Extended Basic Block.  Within this
6541
   range, we guarantee that all instructions are decodable, the
6542
   property table entries are contiguous, and no property table
6543
   specifies a segment that cannot have instructions moved.  This
6544
   structure contains caches of the contents, property table and
6545
   relocations for the specified section for easy use.  The range is
6546
   specified by ranges of indices for the byte offset, property table
6547
   offsets and relocation offsets.  These must be consistent.  */
6548
6549
typedef struct ebb_struct ebb_t;
6550
6551
struct ebb_struct
6552
{
6553
  asection *sec;
6554
6555
  bfd_byte *contents;   /* Cache of the section contents.  */
6556
  bfd_size_type content_length;
6557
6558
  property_table_entry *ptbl; /* Cache of the section property table.  */
6559
  unsigned pte_count;
6560
6561
  Elf_Internal_Rela *relocs;  /* Cache of the section relocations.  */
6562
  unsigned reloc_count;
6563
6564
  bfd_vma start_offset;   /* Offset in section.  */
6565
  unsigned start_ptbl_idx;  /* Offset in the property table.  */
6566
  unsigned start_reloc_idx; /* Offset in the relocations.  */
6567
6568
  bfd_vma end_offset;
6569
  unsigned end_ptbl_idx;
6570
  unsigned end_reloc_idx;
6571
6572
  bool ends_section;  /* Is this the last ebb in a section?  */
6573
6574
  /* The unreachable property table at the end of this set of blocks;
6575
     NULL if the end is not an unreachable block.  */
6576
  property_table_entry *ends_unreachable;
6577
};
6578
6579
6580
enum ebb_target_enum
6581
{
6582
  EBB_NO_ALIGN = 0,
6583
  EBB_DESIRE_TGT_ALIGN,
6584
  EBB_REQUIRE_TGT_ALIGN,
6585
  EBB_REQUIRE_LOOP_ALIGN,
6586
  EBB_REQUIRE_ALIGN
6587
};
6588
6589
6590
/* proposed_action_struct is similar to the text_action_struct except
6591
   that is represents a potential transformation, not one that will
6592
   occur.  We build a list of these for an extended basic block
6593
   and use them to compute the actual actions desired.  We must be
6594
   careful that the entire set of actual actions we perform do not
6595
   break any relocations that would fit if the actions were not
6596
   performed.  */
6597
6598
typedef struct proposed_action_struct proposed_action;
6599
6600
struct proposed_action_struct
6601
{
6602
  enum ebb_target_enum align_type; /* for the target alignment */
6603
  bfd_vma alignment_pow;
6604
  text_action_t action;
6605
  bfd_vma offset;
6606
  int removed_bytes;
6607
  bool do_action; /* If false, then we will not perform the action.  */
6608
};
6609
6610
6611
/* The ebb_constraint_struct keeps a set of proposed actions for an
6612
   extended basic block.   */
6613
6614
typedef struct ebb_constraint_struct ebb_constraint;
6615
6616
struct ebb_constraint_struct
6617
{
6618
  ebb_t ebb;
6619
  bool start_movable;
6620
6621
  /* Bytes of extra space at the beginning if movable.  */
6622
  int start_extra_space;
6623
6624
  enum ebb_target_enum start_align;
6625
6626
  bool end_movable;
6627
6628
  /* Bytes of extra space at the end if movable.  */
6629
  int end_extra_space;
6630
6631
  unsigned action_count;
6632
  unsigned action_allocated;
6633
6634
  /* Array of proposed actions.  */
6635
  proposed_action *actions;
6636
6637
  /* Action alignments -- one for each proposed action.  */
6638
  enum ebb_target_enum *action_aligns;
6639
};
6640
6641
6642
static void
6643
init_ebb_constraint (ebb_constraint *c)
6644
0
{
6645
0
  memset (c, 0, sizeof (ebb_constraint));
6646
0
}
6647
6648
6649
static void
6650
free_ebb_constraint (ebb_constraint *c)
6651
0
{
6652
0
  free (c->actions);
6653
0
}
6654
6655
6656
static void
6657
init_ebb (ebb_t *ebb,
6658
    asection *sec,
6659
    bfd_byte *contents,
6660
    bfd_size_type content_length,
6661
    property_table_entry *prop_table,
6662
    unsigned ptblsize,
6663
    Elf_Internal_Rela *internal_relocs,
6664
    unsigned reloc_count)
6665
0
{
6666
0
  memset (ebb, 0, sizeof (ebb_t));
6667
0
  ebb->sec = sec;
6668
0
  ebb->contents = contents;
6669
0
  ebb->content_length = content_length;
6670
0
  ebb->ptbl = prop_table;
6671
0
  ebb->pte_count = ptblsize;
6672
0
  ebb->relocs = internal_relocs;
6673
0
  ebb->reloc_count = reloc_count;
6674
0
  ebb->start_offset = 0;
6675
0
  ebb->end_offset = ebb->content_length - 1;
6676
0
  ebb->start_ptbl_idx = 0;
6677
0
  ebb->end_ptbl_idx = ptblsize;
6678
0
  ebb->start_reloc_idx = 0;
6679
0
  ebb->end_reloc_idx = reloc_count;
6680
0
}
6681
6682
6683
/* Extend the ebb to all decodable contiguous sections.  The algorithm
6684
   for building a basic block around an instruction is to push it
6685
   forward until we hit the end of a section, an unreachable block or
6686
   a block that cannot be transformed.  Then we push it backwards
6687
   searching for similar conditions.  */
6688
6689
static bool extend_ebb_bounds_forward (ebb_t *);
6690
static bool extend_ebb_bounds_backward (ebb_t *);
6691
static bfd_size_type insn_block_decodable_len
6692
  (bfd_byte *, bfd_size_type, bfd_vma, bfd_size_type);
6693
6694
static bool
6695
extend_ebb_bounds (ebb_t *ebb)
6696
0
{
6697
0
  if (!extend_ebb_bounds_forward (ebb))
6698
0
    return false;
6699
0
  if (!extend_ebb_bounds_backward (ebb))
6700
0
    return false;
6701
0
  return true;
6702
0
}
6703
6704
6705
static bool
6706
extend_ebb_bounds_forward (ebb_t *ebb)
6707
0
{
6708
0
  property_table_entry *the_entry, *new_entry;
6709
6710
0
  the_entry = &ebb->ptbl[ebb->end_ptbl_idx];
6711
6712
  /* Stop when (1) we cannot decode an instruction, (2) we are at
6713
     the end of the property tables, (3) we hit a non-contiguous property
6714
     table entry, (4) we hit a NO_TRANSFORM region.  */
6715
6716
0
  while (1)
6717
0
    {
6718
0
      bfd_vma entry_end;
6719
0
      bfd_size_type insn_block_len;
6720
6721
0
      entry_end = the_entry->address - ebb->sec->vma + the_entry->size;
6722
0
      insn_block_len =
6723
0
  insn_block_decodable_len (ebb->contents, ebb->content_length,
6724
0
          ebb->end_offset,
6725
0
          entry_end - ebb->end_offset);
6726
0
      if (insn_block_len != (entry_end - ebb->end_offset))
6727
0
  {
6728
0
    _bfd_error_handler
6729
      /* xgettext:c-format */
6730
0
      (_("%pB(%pA+%#" PRIx64 "): could not decode instruction; "
6731
0
         "possible configuration mismatch"),
6732
0
       ebb->sec->owner, ebb->sec,
6733
0
       (uint64_t) (ebb->end_offset + insn_block_len));
6734
0
    return false;
6735
0
  }
6736
0
      ebb->end_offset += insn_block_len;
6737
6738
0
      if (ebb->end_offset == ebb->sec->size)
6739
0
  ebb->ends_section = true;
6740
6741
      /* Update the reloc counter.  */
6742
0
      while (ebb->end_reloc_idx + 1 < ebb->reloc_count
6743
0
       && (ebb->relocs[ebb->end_reloc_idx + 1].r_offset
6744
0
     < ebb->end_offset))
6745
0
  {
6746
0
    ebb->end_reloc_idx++;
6747
0
  }
6748
6749
0
      if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6750
0
  return true;
6751
6752
0
      new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6753
0
      if (((new_entry->flags & XTENSA_PROP_INSN) == 0)
6754
0
    || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6755
0
    || ((the_entry->flags & XTENSA_PROP_ALIGN) != 0))
6756
0
  break;
6757
6758
0
      if (the_entry->address + the_entry->size != new_entry->address)
6759
0
  break;
6760
6761
0
      the_entry = new_entry;
6762
0
      ebb->end_ptbl_idx++;
6763
0
    }
6764
6765
  /* Quick check for an unreachable or end of file just at the end.  */
6766
0
  if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6767
0
    {
6768
0
      if (ebb->end_offset == ebb->content_length)
6769
0
  ebb->ends_section = true;
6770
0
    }
6771
0
  else
6772
0
    {
6773
0
      new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6774
0
      if ((new_entry->flags & XTENSA_PROP_UNREACHABLE) != 0
6775
0
    && the_entry->address + the_entry->size == new_entry->address)
6776
0
  ebb->ends_unreachable = new_entry;
6777
0
    }
6778
6779
  /* Any other ending requires exact alignment.  */
6780
0
  return true;
6781
0
}
6782
6783
6784
static bool
6785
extend_ebb_bounds_backward (ebb_t *ebb)
6786
0
{
6787
0
  property_table_entry *the_entry, *new_entry;
6788
6789
0
  the_entry = &ebb->ptbl[ebb->start_ptbl_idx];
6790
6791
  /* Stop when (1) we cannot decode the instructions in the current entry.
6792
     (2) we are at the beginning of the property tables, (3) we hit a
6793
     non-contiguous property table entry, (4) we hit a NO_TRANSFORM region.  */
6794
6795
0
  while (1)
6796
0
    {
6797
0
      bfd_vma block_begin;
6798
0
      bfd_size_type insn_block_len;
6799
6800
0
      block_begin = the_entry->address - ebb->sec->vma;
6801
0
      insn_block_len =
6802
0
  insn_block_decodable_len (ebb->contents, ebb->content_length,
6803
0
          block_begin,
6804
0
          ebb->start_offset - block_begin);
6805
0
      if (insn_block_len != ebb->start_offset - block_begin)
6806
0
  {
6807
0
    _bfd_error_handler
6808
      /* xgettext:c-format */
6809
0
      (_("%pB(%pA+%#" PRIx64 "): could not decode instruction; "
6810
0
         "possible configuration mismatch"),
6811
0
       ebb->sec->owner, ebb->sec,
6812
0
       (uint64_t) (ebb->end_offset + insn_block_len));
6813
0
    return false;
6814
0
  }
6815
0
      ebb->start_offset -= insn_block_len;
6816
6817
      /* Update the reloc counter.  */
6818
0
      while (ebb->start_reloc_idx > 0
6819
0
       && (ebb->relocs[ebb->start_reloc_idx - 1].r_offset
6820
0
     >= ebb->start_offset))
6821
0
  {
6822
0
    ebb->start_reloc_idx--;
6823
0
  }
6824
6825
0
      if (ebb->start_ptbl_idx == 0)
6826
0
  return true;
6827
6828
0
      new_entry = &ebb->ptbl[ebb->start_ptbl_idx - 1];
6829
0
      if ((new_entry->flags & XTENSA_PROP_INSN) == 0
6830
0
    || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6831
0
    || ((new_entry->flags & XTENSA_PROP_ALIGN) != 0))
6832
0
  return true;
6833
0
      if (new_entry->address + new_entry->size != the_entry->address)
6834
0
  return true;
6835
6836
0
      the_entry = new_entry;
6837
0
      ebb->start_ptbl_idx--;
6838
0
    }
6839
0
  return true;
6840
0
}
6841
6842
6843
static bfd_size_type
6844
insn_block_decodable_len (bfd_byte *contents,
6845
        bfd_size_type content_len,
6846
        bfd_vma block_offset,
6847
        bfd_size_type block_len)
6848
0
{
6849
0
  bfd_vma offset = block_offset;
6850
6851
0
  while (offset < block_offset + block_len)
6852
0
    {
6853
0
      bfd_size_type insn_len = 0;
6854
6855
0
      insn_len = insn_decode_len (contents, content_len, offset);
6856
0
      if (insn_len == 0)
6857
0
  return (offset - block_offset);
6858
0
      offset += insn_len;
6859
0
    }
6860
0
  return (offset - block_offset);
6861
0
}
6862
6863
6864
static void
6865
ebb_propose_action (ebb_constraint *c,
6866
        enum ebb_target_enum align_type,
6867
        bfd_vma alignment_pow,
6868
        text_action_t action,
6869
        bfd_vma offset,
6870
        int removed_bytes,
6871
        bool do_action)
6872
0
{
6873
0
  proposed_action *act;
6874
6875
0
  if (c->action_allocated <= c->action_count)
6876
0
    {
6877
0
      unsigned new_allocated, i;
6878
0
      proposed_action *new_actions;
6879
6880
0
      new_allocated = (c->action_count + 2) * 2;
6881
0
      new_actions = (proposed_action *)
6882
0
  bfd_zmalloc (sizeof (proposed_action) * new_allocated);
6883
6884
0
      for (i = 0; i < c->action_count; i++)
6885
0
  new_actions[i] = c->actions[i];
6886
0
      free (c->actions);
6887
0
      c->actions = new_actions;
6888
0
      c->action_allocated = new_allocated;
6889
0
    }
6890
6891
0
  act = &c->actions[c->action_count];
6892
0
  act->align_type = align_type;
6893
0
  act->alignment_pow = alignment_pow;
6894
0
  act->action = action;
6895
0
  act->offset = offset;
6896
0
  act->removed_bytes = removed_bytes;
6897
0
  act->do_action = do_action;
6898
6899
0
  c->action_count++;
6900
0
}
6901
6902

6903
/* Access to internal relocations, section contents and symbols.  */
6904
6905
/* During relaxation, we need to modify relocations, section contents,
6906
   and symbol definitions, and we need to keep the original values from
6907
   being reloaded from the input files, i.e., we need to "pin" the
6908
   modified values in memory.  We also want to continue to observe the
6909
   setting of the "keep-memory" flag.  The following functions wrap the
6910
   standard BFD functions to take care of this for us.  */
6911
6912
static Elf_Internal_Rela *
6913
retrieve_internal_relocs (bfd *abfd, asection *sec, bool keep_memory)
6914
0
{
6915
0
  Elf_Internal_Rela *internal_relocs;
6916
6917
0
  if ((sec->flags & SEC_LINKER_CREATED) != 0)
6918
0
    return NULL;
6919
6920
0
  internal_relocs = elf_section_data (sec)->relocs;
6921
0
  if (internal_relocs == NULL)
6922
0
    internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
6923
0
             keep_memory);
6924
0
  return internal_relocs;
6925
0
}
6926
6927
6928
static void
6929
pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6930
0
{
6931
0
  elf_section_data (sec)->relocs = internal_relocs;
6932
0
}
6933
6934
6935
static void
6936
release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6937
0
{
6938
0
  if (elf_section_data (sec)->relocs != internal_relocs)
6939
0
    free (internal_relocs);
6940
0
}
6941
6942
6943
static bfd_byte *
6944
retrieve_contents (bfd *abfd, asection *sec, bool keep_memory)
6945
0
{
6946
0
  bfd_byte *contents;
6947
0
  bfd_size_type sec_size;
6948
6949
0
  sec_size = bfd_get_section_limit (abfd, sec);
6950
0
  contents = elf_section_data (sec)->this_hdr.contents;
6951
6952
0
  if (contents == NULL && sec_size != 0)
6953
0
    {
6954
0
      if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6955
0
  {
6956
0
    free (contents);
6957
0
    return NULL;
6958
0
  }
6959
0
      if (keep_memory)
6960
0
  elf_section_data (sec)->this_hdr.contents = contents;
6961
0
    }
6962
0
  return contents;
6963
0
}
6964
6965
6966
static void
6967
pin_contents (asection *sec, bfd_byte *contents)
6968
0
{
6969
0
  elf_section_data (sec)->this_hdr.contents = contents;
6970
0
}
6971
6972
6973
static void
6974
release_contents (asection *sec, bfd_byte *contents)
6975
0
{
6976
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
6977
0
    free (contents);
6978
0
}
6979
6980
6981
static Elf_Internal_Sym *
6982
retrieve_local_syms (bfd *input_bfd)
6983
0
{
6984
0
  Elf_Internal_Shdr *symtab_hdr;
6985
0
  Elf_Internal_Sym *isymbuf;
6986
0
  size_t locsymcount;
6987
6988
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
6989
0
  locsymcount = symtab_hdr->sh_info;
6990
6991
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6992
0
  if (isymbuf == NULL && locsymcount != 0)
6993
0
    isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6994
0
            NULL, NULL, NULL);
6995
6996
  /* Save the symbols for this input file so they won't be read again.  */
6997
0
  if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
6998
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
6999
7000
0
  return isymbuf;
7001
0
}
7002
7003

7004
/* Code for link-time relaxation.  */
7005
7006
/* Initialization for relaxation: */
7007
static bool analyze_relocations (struct bfd_link_info *);
7008
static bool find_relaxable_sections
7009
  (bfd *, asection *, struct bfd_link_info *, bool *);
7010
static bool collect_source_relocs
7011
  (bfd *, asection *, struct bfd_link_info *);
7012
static bool is_resolvable_asm_expansion
7013
  (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, struct bfd_link_info *,
7014
   bool *);
7015
static Elf_Internal_Rela *find_associated_l32r_irel
7016
  (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Rela *);
7017
static bool compute_text_actions
7018
  (bfd *, asection *, struct bfd_link_info *);
7019
static bool compute_ebb_proposed_actions (ebb_constraint *);
7020
static bool compute_ebb_actions (ebb_constraint *);
7021
typedef struct reloc_range_list_struct reloc_range_list;
7022
static bool check_section_ebb_pcrels_fit
7023
  (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
7024
   reloc_range_list *, const ebb_constraint *,
7025
   const xtensa_opcode *);
7026
static bool check_section_ebb_reduces (const ebb_constraint *);
7027
static void text_action_add_proposed
7028
  (text_action_list *, const ebb_constraint *, asection *);
7029
7030
/* First pass: */
7031
static bool compute_removed_literals
7032
  (bfd *, asection *, struct bfd_link_info *, value_map_hash_table *);
7033
static Elf_Internal_Rela *get_irel_at_offset
7034
  (asection *, Elf_Internal_Rela *, bfd_vma);
7035
static bool is_removable_literal
7036
  (const source_reloc *, int, const source_reloc *, int, asection *,
7037
   property_table_entry *, int);
7038
static bool remove_dead_literal
7039
  (bfd *, asection *, struct bfd_link_info *, Elf_Internal_Rela *,
7040
   Elf_Internal_Rela *, source_reloc *, property_table_entry *, int);
7041
static bool identify_literal_placement
7042
  (bfd *, asection *, bfd_byte *, struct bfd_link_info *,
7043
   value_map_hash_table *, bool *, Elf_Internal_Rela *, int,
7044
   source_reloc *, property_table_entry *, int, section_cache_t *,
7045
   bool);
7046
static bool relocations_reach (source_reloc *, int, const r_reloc *);
7047
static bool coalesce_shared_literal
7048
  (asection *, source_reloc *, property_table_entry *, int, value_map *);
7049
static bool move_shared_literal
7050
  (asection *, struct bfd_link_info *, source_reloc *, property_table_entry *,
7051
   int, const r_reloc *, const literal_value *, section_cache_t *);
7052
7053
/* Second pass: */
7054
static bool relax_section (bfd *, asection *, struct bfd_link_info *);
7055
static bool translate_section_fixes (asection *);
7056
static bool translate_reloc_bfd_fix (reloc_bfd_fix *);
7057
static asection *translate_reloc (const r_reloc *, r_reloc *, asection *);
7058
static void shrink_dynamic_reloc_sections
7059
  (struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *);
7060
static bool move_literal
7061
  (bfd *, struct bfd_link_info *, asection *, bfd_vma, bfd_byte *,
7062
   xtensa_relax_info *, Elf_Internal_Rela **, const literal_value *);
7063
static bool relax_property_section
7064
  (bfd *, asection *, struct bfd_link_info *);
7065
7066
/* Third pass: */
7067
static bool relax_section_symbols (bfd *, asection *);
7068
7069
7070
static bool
7071
elf_xtensa_relax_section (bfd *abfd,
7072
        asection *sec,
7073
        struct bfd_link_info *link_info,
7074
        bool *again)
7075
0
{
7076
0
  static value_map_hash_table *values = NULL;
7077
0
  static bool relocations_analyzed = false;
7078
0
  xtensa_relax_info *relax_info;
7079
7080
0
  if (!relocations_analyzed)
7081
0
    {
7082
      /* Do some overall initialization for relaxation.  */
7083
0
      values = value_map_hash_table_init ();
7084
0
      if (values == NULL)
7085
0
  return false;
7086
0
      relaxing_section = true;
7087
0
      if (!analyze_relocations (link_info))
7088
0
  return false;
7089
0
      relocations_analyzed = true;
7090
0
    }
7091
0
  *again = false;
7092
7093
  /* Don't mess with linker-created sections.  */
7094
0
  if ((sec->flags & SEC_LINKER_CREATED) != 0)
7095
0
    return true;
7096
7097
0
  relax_info = get_xtensa_relax_info (sec);
7098
0
  BFD_ASSERT (relax_info != NULL);
7099
7100
0
  switch (relax_info->visited)
7101
0
    {
7102
0
    case 0:
7103
      /* Note: It would be nice to fold this pass into
7104
   analyze_relocations, but it is important for this step that the
7105
   sections be examined in link order.  */
7106
0
      if (!compute_removed_literals (abfd, sec, link_info, values))
7107
0
  return false;
7108
0
      *again = true;
7109
0
      break;
7110
7111
0
    case 1:
7112
0
      if (values)
7113
0
  value_map_hash_table_delete (values);
7114
0
      values = NULL;
7115
0
      if (!relax_section (abfd, sec, link_info))
7116
0
  return false;
7117
0
      *again = true;
7118
0
      break;
7119
7120
0
    case 2:
7121
0
      if (!relax_section_symbols (abfd, sec))
7122
0
  return false;
7123
0
      break;
7124
0
    }
7125
7126
0
  relax_info->visited++;
7127
0
  return true;
7128
0
}
7129
7130

7131
/* Initialization for relaxation.  */
7132
7133
/* This function is called once at the start of relaxation.  It scans
7134
   all the input sections and marks the ones that are relaxable (i.e.,
7135
   literal sections with L32R relocations against them), and then
7136
   collects source_reloc information for all the relocations against
7137
   those relaxable sections.  During this process, it also detects
7138
   longcalls, i.e., calls relaxed by the assembler into indirect
7139
   calls, that can be optimized back into direct calls.  Within each
7140
   extended basic block (ebb) containing an optimized longcall, it
7141
   computes a set of "text actions" that can be performed to remove
7142
   the L32R associated with the longcall while optionally preserving
7143
   branch target alignments.  */
7144
7145
static bool
7146
analyze_relocations (struct bfd_link_info *link_info)
7147
0
{
7148
0
  bfd *abfd;
7149
0
  asection *sec;
7150
0
  bool is_relaxable = false;
7151
7152
  /* Initialize the per-section relaxation info.  */
7153
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7154
0
    for (sec = abfd->sections; sec != NULL; sec = sec->next)
7155
0
      {
7156
0
  init_xtensa_relax_info (sec);
7157
0
      }
7158
7159
  /* Mark relaxable sections (and count relocations against each one).  */
7160
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7161
0
    for (sec = abfd->sections; sec != NULL; sec = sec->next)
7162
0
      {
7163
0
  if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
7164
0
    return false;
7165
0
      }
7166
7167
  /* Bail out if there are no relaxable sections.  */
7168
0
  if (!is_relaxable)
7169
0
    return true;
7170
7171
  /* Allocate space for source_relocs.  */
7172
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7173
0
    for (sec = abfd->sections; sec != NULL; sec = sec->next)
7174
0
      {
7175
0
  xtensa_relax_info *relax_info;
7176
7177
0
  relax_info = get_xtensa_relax_info (sec);
7178
0
  if (relax_info->is_relaxable_literal_section
7179
0
      || relax_info->is_relaxable_asm_section)
7180
0
    {
7181
0
      relax_info->src_relocs = (source_reloc *)
7182
0
        bfd_malloc (relax_info->src_count * sizeof (source_reloc));
7183
0
    }
7184
0
  else
7185
0
    relax_info->src_count = 0;
7186
0
      }
7187
7188
  /* Collect info on relocations against each relaxable section.  */
7189
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7190
0
    for (sec = abfd->sections; sec != NULL; sec = sec->next)
7191
0
      {
7192
0
  if (!collect_source_relocs (abfd, sec, link_info))
7193
0
    return false;
7194
0
      }
7195
7196
  /* Compute the text actions.  */
7197
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7198
0
    for (sec = abfd->sections; sec != NULL; sec = sec->next)
7199
0
      {
7200
0
  if (!compute_text_actions (abfd, sec, link_info))
7201
0
    return false;
7202
0
      }
7203
7204
0
  return true;
7205
0
}
7206
7207
7208
/* Find all the sections that might be relaxed.  The motivation for
7209
   this pass is that collect_source_relocs() needs to record _all_ the
7210
   relocations that target each relaxable section.  That is expensive
7211
   and unnecessary unless the target section is actually going to be
7212
   relaxed.  This pass identifies all such sections by checking if
7213
   they have L32Rs pointing to them.  In the process, the total number
7214
   of relocations targeting each section is also counted so that we
7215
   know how much space to allocate for source_relocs against each
7216
   relaxable literal section.  */
7217
7218
static bool
7219
find_relaxable_sections (bfd *abfd,
7220
       asection *sec,
7221
       struct bfd_link_info *link_info,
7222
       bool *is_relaxable_p)
7223
0
{
7224
0
  Elf_Internal_Rela *internal_relocs;
7225
0
  bfd_byte *contents;
7226
0
  bool ok = true;
7227
0
  unsigned i;
7228
0
  xtensa_relax_info *source_relax_info;
7229
0
  bool is_l32r_reloc;
7230
7231
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
7232
0
                link_info->keep_memory);
7233
0
  if (internal_relocs == NULL)
7234
0
    return ok;
7235
7236
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7237
0
  if (contents == NULL && sec->size != 0)
7238
0
    {
7239
0
      ok = false;
7240
0
      goto error_return;
7241
0
    }
7242
7243
0
  source_relax_info = get_xtensa_relax_info (sec);
7244
0
  for (i = 0; i < sec->reloc_count; i++)
7245
0
    {
7246
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
7247
0
      r_reloc r_rel;
7248
0
      asection *target_sec;
7249
0
      xtensa_relax_info *target_relax_info;
7250
7251
      /* If this section has not already been marked as "relaxable", and
7252
   if it contains any ASM_EXPAND relocations (marking expanded
7253
   longcalls) that can be optimized into direct calls, then mark
7254
   the section as "relaxable".  */
7255
0
      if (source_relax_info
7256
0
    && !source_relax_info->is_relaxable_asm_section
7257
0
    && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_EXPAND)
7258
0
  {
7259
0
    bool is_reachable = false;
7260
0
    if (is_resolvable_asm_expansion (abfd, sec, contents, irel,
7261
0
             link_info, &is_reachable)
7262
0
        && is_reachable)
7263
0
      {
7264
0
        source_relax_info->is_relaxable_asm_section = true;
7265
0
        *is_relaxable_p = true;
7266
0
      }
7267
0
  }
7268
7269
0
      r_reloc_init (&r_rel, abfd, irel, contents,
7270
0
        bfd_get_section_limit (abfd, sec));
7271
7272
0
      target_sec = r_reloc_get_section (&r_rel);
7273
0
      target_relax_info = get_xtensa_relax_info (target_sec);
7274
0
      if (!target_relax_info)
7275
0
  continue;
7276
7277
      /* Count PC-relative operand relocations against the target section.
7278
   Note: The conditions tested here must match the conditions under
7279
   which init_source_reloc is called in collect_source_relocs().  */
7280
0
      is_l32r_reloc = false;
7281
0
      if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7282
0
  {
7283
0
    xtensa_opcode opcode =
7284
0
      get_relocation_opcode (abfd, sec, contents, irel);
7285
0
    if (opcode != XTENSA_UNDEFINED)
7286
0
      {
7287
0
        is_l32r_reloc = (opcode == get_l32r_opcode ());
7288
0
        if (!is_alt_relocation (ELF32_R_TYPE (irel->r_info))
7289
0
      || is_l32r_reloc)
7290
0
    target_relax_info->src_count++;
7291
0
      }
7292
0
  }
7293
7294
0
      if (is_l32r_reloc && r_reloc_is_defined (&r_rel))
7295
0
  {
7296
    /* Mark the target section as relaxable.  */
7297
0
    target_relax_info->is_relaxable_literal_section = true;
7298
0
    *is_relaxable_p = true;
7299
0
  }
7300
0
    }
7301
7302
0
 error_return:
7303
0
  release_contents (sec, contents);
7304
0
  release_internal_relocs (sec, internal_relocs);
7305
0
  return ok;
7306
0
}
7307
7308
7309
/* Record _all_ the relocations that point to relaxable sections, and
7310
   get rid of ASM_EXPAND relocs by either converting them to
7311
   ASM_SIMPLIFY or by removing them.  */
7312
7313
static bool
7314
collect_source_relocs (bfd *abfd,
7315
           asection *sec,
7316
           struct bfd_link_info *link_info)
7317
0
{
7318
0
  Elf_Internal_Rela *internal_relocs;
7319
0
  bfd_byte *contents;
7320
0
  bool ok = true;
7321
0
  unsigned i;
7322
0
  bfd_size_type sec_size;
7323
7324
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
7325
0
                link_info->keep_memory);
7326
0
  if (internal_relocs == NULL)
7327
0
    return ok;
7328
7329
0
  sec_size = bfd_get_section_limit (abfd, sec);
7330
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7331
0
  if (contents == NULL && sec_size != 0)
7332
0
    {
7333
0
      ok = false;
7334
0
      goto error_return;
7335
0
    }
7336
7337
  /* Record relocations against relaxable literal sections.  */
7338
0
  for (i = 0; i < sec->reloc_count; i++)
7339
0
    {
7340
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
7341
0
      r_reloc r_rel;
7342
0
      asection *target_sec;
7343
0
      xtensa_relax_info *target_relax_info;
7344
7345
0
      r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7346
7347
0
      target_sec = r_reloc_get_section (&r_rel);
7348
0
      target_relax_info = get_xtensa_relax_info (target_sec);
7349
7350
0
      if (target_relax_info
7351
0
    && (target_relax_info->is_relaxable_literal_section
7352
0
        || target_relax_info->is_relaxable_asm_section))
7353
0
  {
7354
0
    xtensa_opcode opcode = XTENSA_UNDEFINED;
7355
0
    int opnd = -1;
7356
0
    bool is_abs_literal = false;
7357
7358
0
    if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7359
0
      {
7360
        /* None of the current alternate relocs are PC-relative,
7361
     and only PC-relative relocs matter here.  However, we
7362
     still need to record the opcode for literal
7363
     coalescing.  */
7364
0
        opcode = get_relocation_opcode (abfd, sec, contents, irel);
7365
0
        if (opcode == get_l32r_opcode ())
7366
0
    {
7367
0
      is_abs_literal = true;
7368
0
      opnd = 1;
7369
0
    }
7370
0
        else
7371
0
    opcode = XTENSA_UNDEFINED;
7372
0
      }
7373
0
    else if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7374
0
      {
7375
0
        opcode = get_relocation_opcode (abfd, sec, contents, irel);
7376
0
        opnd = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7377
0
      }
7378
7379
0
    if (opcode != XTENSA_UNDEFINED)
7380
0
      {
7381
0
        int src_next = target_relax_info->src_next++;
7382
0
        source_reloc *s_reloc = &target_relax_info->src_relocs[src_next];
7383
7384
0
        init_source_reloc (s_reloc, sec, &r_rel, opcode, opnd,
7385
0
         is_abs_literal);
7386
0
      }
7387
0
  }
7388
0
    }
7389
7390
  /* Now get rid of ASM_EXPAND relocations.  At this point, the
7391
     src_relocs array for the target literal section may still be
7392
     incomplete, but it must at least contain the entries for the L32R
7393
     relocations associated with ASM_EXPANDs because they were just
7394
     added in the preceding loop over the relocations.  */
7395
7396
0
  for (i = 0; i < sec->reloc_count; i++)
7397
0
    {
7398
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
7399
0
      bool is_reachable;
7400
7401
0
      if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
7402
0
          &is_reachable))
7403
0
  continue;
7404
7405
0
      if (is_reachable)
7406
0
  {
7407
0
    Elf_Internal_Rela *l32r_irel;
7408
0
    r_reloc r_rel;
7409
0
    asection *target_sec;
7410
0
    xtensa_relax_info *target_relax_info;
7411
7412
    /* Mark the source_reloc for the L32R so that it will be
7413
       removed in compute_removed_literals(), along with the
7414
       associated literal.  */
7415
0
    l32r_irel = find_associated_l32r_irel (abfd, sec, contents,
7416
0
             irel, internal_relocs);
7417
0
    if (l32r_irel == NULL)
7418
0
      continue;
7419
7420
0
    r_reloc_init (&r_rel, abfd, l32r_irel, contents, sec_size);
7421
7422
0
    target_sec = r_reloc_get_section (&r_rel);
7423
0
    target_relax_info = get_xtensa_relax_info (target_sec);
7424
7425
0
    if (target_relax_info
7426
0
        && (target_relax_info->is_relaxable_literal_section
7427
0
      || target_relax_info->is_relaxable_asm_section))
7428
0
      {
7429
0
        source_reloc *s_reloc;
7430
7431
        /* Search the source_relocs for the entry corresponding to
7432
     the l32r_irel.  Note: The src_relocs array is not yet
7433
     sorted, but it wouldn't matter anyway because we're
7434
     searching by source offset instead of target offset.  */
7435
0
        s_reloc = find_source_reloc (target_relax_info->src_relocs,
7436
0
             target_relax_info->src_next,
7437
0
             sec, l32r_irel);
7438
0
        BFD_ASSERT (s_reloc);
7439
0
        s_reloc->is_null = true;
7440
0
      }
7441
7442
    /* Convert this reloc to ASM_SIMPLIFY.  */
7443
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
7444
0
               R_XTENSA_ASM_SIMPLIFY);
7445
0
    l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7446
7447
0
    pin_internal_relocs (sec, internal_relocs);
7448
0
  }
7449
0
      else
7450
0
  {
7451
    /* It is resolvable but doesn't reach.  We resolve now
7452
       by eliminating the relocation -- the call will remain
7453
       expanded into L32R/CALLX.  */
7454
0
    irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7455
0
    pin_internal_relocs (sec, internal_relocs);
7456
0
  }
7457
0
    }
7458
7459
0
 error_return:
7460
0
  release_contents (sec, contents);
7461
0
  release_internal_relocs (sec, internal_relocs);
7462
0
  return ok;
7463
0
}
7464
7465
7466
/* Return TRUE if the asm expansion can be resolved.  Generally it can
7467
   be resolved on a final link or when a partial link locates it in the
7468
   same section as the target.  Set "is_reachable" flag if the target of
7469
   the call is within the range of a direct call, given the current VMA
7470
   for this section and the target section.  */
7471
7472
bool
7473
is_resolvable_asm_expansion (bfd *abfd,
7474
           asection *sec,
7475
           bfd_byte *contents,
7476
           Elf_Internal_Rela *irel,
7477
           struct bfd_link_info *link_info,
7478
           bool *is_reachable_p)
7479
0
{
7480
0
  asection *target_sec;
7481
0
  asection *s;
7482
0
  bfd_vma first_vma;
7483
0
  bfd_vma last_vma;
7484
0
  unsigned int first_align;
7485
0
  unsigned int adjust;
7486
0
  bfd_vma target_offset;
7487
0
  r_reloc r_rel;
7488
0
  xtensa_opcode opcode, direct_call_opcode;
7489
0
  bfd_vma self_address;
7490
0
  bfd_vma dest_address;
7491
0
  bool uses_l32r;
7492
0
  bfd_size_type sec_size;
7493
7494
0
  *is_reachable_p = false;
7495
7496
0
  if (contents == NULL)
7497
0
    return false;
7498
7499
0
  if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
7500
0
    return false;
7501
7502
0
  sec_size = bfd_get_section_limit (abfd, sec);
7503
0
  opcode = get_expanded_call_opcode (contents + irel->r_offset,
7504
0
             sec_size - irel->r_offset, &uses_l32r);
7505
  /* Optimization of longcalls that use CONST16 is not yet implemented.  */
7506
0
  if (!uses_l32r)
7507
0
    return false;
7508
7509
0
  direct_call_opcode = swap_callx_for_call_opcode (opcode);
7510
0
  if (direct_call_opcode == XTENSA_UNDEFINED)
7511
0
    return false;
7512
7513
  /* Check and see that the target resolves.  */
7514
0
  r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7515
0
  if (!r_reloc_is_defined (&r_rel))
7516
0
    return false;
7517
7518
0
  target_sec = r_reloc_get_section (&r_rel);
7519
0
  target_offset = r_rel.target_offset;
7520
7521
  /* If the target is in a shared library, then it doesn't reach.  This
7522
     isn't supposed to come up because the compiler should never generate
7523
     non-PIC calls on systems that use shared libraries, but the linker
7524
     shouldn't crash regardless.  */
7525
0
  if (!target_sec->output_section)
7526
0
    return false;
7527
7528
  /* For relocatable sections, we can only simplify when the output
7529
     section of the target is the same as the output section of the
7530
     source.  */
7531
0
  if (bfd_link_relocatable (link_info)
7532
0
      && (target_sec->output_section != sec->output_section
7533
0
    || is_reloc_sym_weak (abfd, irel)))
7534
0
    return false;
7535
7536
0
  if (target_sec->output_section != sec->output_section)
7537
0
    {
7538
      /* If the two sections are sufficiently far away that relaxation
7539
   might take the call out of range, we can't simplify.  For
7540
   example, a positive displacement call into another memory
7541
   could get moved to a lower address due to literal removal,
7542
   but the destination won't move, and so the displacment might
7543
   get larger.
7544
7545
   If the displacement is negative, assume the destination could
7546
   move as far back as the start of the output section.  The
7547
   self_address will be at least as far into the output section
7548
   as it is prior to relaxation.
7549
7550
   If the displacement is postive, assume the destination will be in
7551
   it's pre-relaxed location (because relaxation only makes sections
7552
   smaller).  The self_address could go all the way to the beginning
7553
   of the output section.  */
7554
7555
0
      dest_address = target_sec->output_section->vma;
7556
0
      self_address = sec->output_section->vma;
7557
7558
0
      if (sec->output_section->vma > target_sec->output_section->vma)
7559
0
  self_address += sec->output_offset + irel->r_offset + 3;
7560
0
      else
7561
0
  dest_address += bfd_get_section_limit (abfd, target_sec->output_section);
7562
      /* Call targets should be four-byte aligned.  */
7563
0
      dest_address = (dest_address + 3) & ~3;
7564
0
    }
7565
0
  else
7566
0
    {
7567
7568
0
      self_address = (sec->output_section->vma
7569
0
          + sec->output_offset + irel->r_offset + 3);
7570
0
      dest_address = (target_sec->output_section->vma
7571
0
          + target_sec->output_offset + target_offset);
7572
0
    }
7573
7574
  /* Adjust addresses with alignments for the worst case to see if call insn
7575
     can fit.  Don't relax l32r + callx to call if the target can be out of
7576
     range due to alignment.
7577
     Caller and target addresses are highest and lowest address.
7578
     Search all sections between caller and target, looking for max alignment.
7579
     The adjustment is max alignment bytes.  If the alignment at the lowest
7580
     address is less than the adjustment, apply the adjustment to highest
7581
     address.  */
7582
7583
  /* Start from lowest address.
7584
     Lowest address aligmnet is from input section.
7585
     Initial alignment (adjust) is from input section.  */
7586
0
  if (dest_address > self_address)
7587
0
    {
7588
0
      s = sec->output_section;
7589
0
      last_vma = dest_address;
7590
0
      first_align = sec->alignment_power;
7591
0
      adjust = target_sec->alignment_power;
7592
0
    }
7593
0
  else
7594
0
    {
7595
0
      s = target_sec->output_section;
7596
0
      last_vma = self_address;
7597
0
      first_align = target_sec->alignment_power;
7598
0
      adjust = sec->alignment_power;
7599
0
    }
7600
7601
0
  first_vma = s->vma;
7602
7603
  /* Find the largest alignment in output section list.  */
7604
0
  for (; s && s->vma >= first_vma && s->vma <= last_vma ; s = s->next)
7605
0
    {
7606
0
      if (s->alignment_power > adjust)
7607
0
  adjust = s->alignment_power;
7608
0
    }
7609
7610
0
  if (adjust > first_align)
7611
0
    {
7612
      /* Alignment may enlarge the range, adjust highest address.  */
7613
0
      adjust = 1 << adjust;
7614
0
      if (dest_address > self_address)
7615
0
  {
7616
0
    dest_address += adjust;
7617
0
  }
7618
0
      else
7619
0
  {
7620
0
    self_address += adjust;
7621
0
  }
7622
0
    }
7623
7624
0
  *is_reachable_p = pcrel_reloc_fits (direct_call_opcode, 0,
7625
0
              self_address, dest_address);
7626
7627
0
  if ((self_address >> CALL_SEGMENT_BITS) !=
7628
0
      (dest_address >> CALL_SEGMENT_BITS))
7629
0
    return false;
7630
7631
0
  return true;
7632
0
}
7633
7634
7635
static Elf_Internal_Rela *
7636
find_associated_l32r_irel (bfd *abfd,
7637
         asection *sec,
7638
         bfd_byte *contents,
7639
         Elf_Internal_Rela *other_irel,
7640
         Elf_Internal_Rela *internal_relocs)
7641
0
{
7642
0
  unsigned i;
7643
7644
0
  for (i = 0; i < sec->reloc_count; i++)
7645
0
    {
7646
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
7647
7648
0
      if (irel == other_irel)
7649
0
  continue;
7650
0
      if (irel->r_offset != other_irel->r_offset)
7651
0
  continue;
7652
0
      if (is_l32r_relocation (abfd, sec, contents, irel))
7653
0
  return irel;
7654
0
    }
7655
7656
0
  return NULL;
7657
0
}
7658
7659
7660
static xtensa_opcode *
7661
build_reloc_opcodes (bfd *abfd,
7662
         asection *sec,
7663
         bfd_byte *contents,
7664
         Elf_Internal_Rela *internal_relocs)
7665
0
{
7666
0
  unsigned i;
7667
0
  xtensa_opcode *reloc_opcodes =
7668
0
    (xtensa_opcode *) bfd_malloc (sizeof (xtensa_opcode) * sec->reloc_count);
7669
0
  for (i = 0; i < sec->reloc_count; i++)
7670
0
    {
7671
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
7672
0
      reloc_opcodes[i] = get_relocation_opcode (abfd, sec, contents, irel);
7673
0
    }
7674
0
  return reloc_opcodes;
7675
0
}
7676
7677
struct reloc_range_struct
7678
{
7679
  bfd_vma addr;
7680
  bool add; /* TRUE if start of a range, FALSE otherwise.  */
7681
  /* Original irel index in the array of relocations for a section.  */
7682
  unsigned irel_index;
7683
};
7684
typedef struct reloc_range_struct reloc_range;
7685
7686
typedef struct reloc_range_list_entry_struct reloc_range_list_entry;
7687
struct reloc_range_list_entry_struct
7688
{
7689
  reloc_range_list_entry *next;
7690
  reloc_range_list_entry *prev;
7691
  Elf_Internal_Rela *irel;
7692
  xtensa_opcode opcode;
7693
  int opnum;
7694
};
7695
7696
struct reloc_range_list_struct
7697
{
7698
  /* The rest of the structure is only meaningful when ok is TRUE.  */
7699
  bool ok;
7700
7701
  unsigned n_range; /* Number of range markers.  */
7702
  reloc_range *range; /* Sorted range markers.  */
7703
7704
  unsigned first; /* Index of a first range element in the list.  */
7705
  unsigned last; /* One past index of a last range element in the list.  */
7706
7707
  unsigned n_list; /* Number of list elements.  */
7708
  reloc_range_list_entry *reloc; /*  */
7709
  reloc_range_list_entry list_root;
7710
};
7711
7712
static int
7713
reloc_range_compare (const void *a, const void *b)
7714
0
{
7715
0
  const reloc_range *ra = a;
7716
0
  const reloc_range *rb = b;
7717
7718
0
  if (ra->addr != rb->addr)
7719
0
    return ra->addr < rb->addr ? -1 : 1;
7720
0
  if (ra->add != rb->add)
7721
0
    return ra->add ? -1 : 1;
7722
0
  return 0;
7723
0
}
7724
7725
static void
7726
build_reloc_ranges (bfd *abfd, asection *sec,
7727
        bfd_byte *contents,
7728
        Elf_Internal_Rela *internal_relocs,
7729
        xtensa_opcode *reloc_opcodes,
7730
        reloc_range_list *list)
7731
0
{
7732
0
  unsigned i;
7733
0
  size_t n = 0;
7734
0
  size_t max_n = 0;
7735
0
  reloc_range *ranges = NULL;
7736
0
  reloc_range_list_entry *reloc =
7737
0
    bfd_malloc (sec->reloc_count * sizeof (*reloc));
7738
7739
0
  memset (list, 0, sizeof (*list));
7740
0
  list->ok = true;
7741
7742
0
  for (i = 0; i < sec->reloc_count; i++)
7743
0
    {
7744
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
7745
0
      int r_type = ELF32_R_TYPE (irel->r_info);
7746
0
      reloc_howto_type *howto = &elf_howto_table[r_type];
7747
0
      r_reloc r_rel;
7748
7749
0
      if (r_type == R_XTENSA_ASM_SIMPLIFY
7750
0
    || r_type == R_XTENSA_32_PCREL
7751
0
    || !howto->pc_relative)
7752
0
  continue;
7753
7754
0
      r_reloc_init (&r_rel, abfd, irel, contents,
7755
0
        bfd_get_section_limit (abfd, sec));
7756
7757
0
      if (r_reloc_get_section (&r_rel) != sec)
7758
0
  continue;
7759
7760
0
      if (n + 2 > max_n)
7761
0
  {
7762
0
    max_n = (max_n + 2) * 2;
7763
0
    ranges = bfd_realloc (ranges, max_n * sizeof (*ranges));
7764
0
  }
7765
7766
0
      ranges[n].addr = irel->r_offset;
7767
0
      ranges[n + 1].addr = r_rel.target_offset;
7768
7769
0
      ranges[n].add = ranges[n].addr < ranges[n + 1].addr;
7770
0
      ranges[n + 1].add = !ranges[n].add;
7771
7772
0
      ranges[n].irel_index = i;
7773
0
      ranges[n + 1].irel_index = i;
7774
7775
0
      n += 2;
7776
7777
0
      reloc[i].irel = irel;
7778
7779
      /* Every relocation won't possibly be checked in the optimized version of
7780
   check_section_ebb_pcrels_fit, so this needs to be done here.  */
7781
0
      if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7782
0
  {
7783
    /* None of the current alternate relocs are PC-relative,
7784
       and only PC-relative relocs matter here.  */
7785
0
  }
7786
0
      else
7787
0
  {
7788
0
    xtensa_opcode opcode;
7789
0
    int opnum;
7790
7791
0
    if (reloc_opcodes)
7792
0
      opcode = reloc_opcodes[i];
7793
0
    else
7794
0
      opcode = get_relocation_opcode (abfd, sec, contents, irel);
7795
7796
0
    if (opcode == XTENSA_UNDEFINED)
7797
0
      {
7798
0
        list->ok = false;
7799
0
        break;
7800
0
      }
7801
7802
0
    opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7803
0
    if (opnum == XTENSA_UNDEFINED)
7804
0
      {
7805
0
        list->ok = false;
7806
0
        break;
7807
0
      }
7808
7809
    /* Record relocation opcode and opnum as we've calculated them
7810
       anyway and they won't change.  */
7811
0
    reloc[i].opcode = opcode;
7812
0
    reloc[i].opnum = opnum;
7813
0
  }
7814
0
    }
7815
7816
0
  if (list->ok)
7817
0
    {
7818
0
      ranges = bfd_realloc (ranges, n * sizeof (*ranges));
7819
0
      qsort (ranges, n, sizeof (*ranges), reloc_range_compare);
7820
7821
0
      list->n_range = n;
7822
0
      list->range = ranges;
7823
0
      list->reloc = reloc;
7824
0
      list->list_root.prev = &list->list_root;
7825
0
      list->list_root.next = &list->list_root;
7826
0
    }
7827
0
  else
7828
0
    {
7829
0
      free (ranges);
7830
0
      free (reloc);
7831
0
    }
7832
0
}
7833
7834
static void reloc_range_list_append (reloc_range_list *list,
7835
             unsigned irel_index)
7836
0
{
7837
0
  reloc_range_list_entry *entry = list->reloc + irel_index;
7838
7839
0
  entry->prev = list->list_root.prev;
7840
0
  entry->next = &list->list_root;
7841
0
  entry->prev->next = entry;
7842
0
  entry->next->prev = entry;
7843
0
  ++list->n_list;
7844
0
}
7845
7846
static void reloc_range_list_remove (reloc_range_list *list,
7847
             unsigned irel_index)
7848
0
{
7849
0
  reloc_range_list_entry *entry = list->reloc + irel_index;
7850
7851
0
  entry->next->prev = entry->prev;
7852
0
  entry->prev->next = entry->next;
7853
0
  --list->n_list;
7854
0
}
7855
7856
/* Update relocation list object so that it lists all relocations that cross
7857
   [first; last] range.  Range bounds should not decrease with successive
7858
   invocations.  */
7859
static void reloc_range_list_update_range (reloc_range_list *list,
7860
             bfd_vma first, bfd_vma last)
7861
0
{
7862
  /* This should not happen: EBBs are iterated from lower addresses to higher.
7863
     But even if that happens there's no need to break: just flush current list
7864
     and start from scratch.  */
7865
0
  if ((list->last > 0 && list->range[list->last - 1].addr > last) ||
7866
0
      (list->first > 0 && list->range[list->first - 1].addr >= first))
7867
0
    {
7868
0
      list->first = 0;
7869
0
      list->last = 0;
7870
0
      list->n_list = 0;
7871
0
      list->list_root.next = &list->list_root;
7872
0
      list->list_root.prev = &list->list_root;
7873
0
      fprintf (stderr, "%s: move backwards requested\n", __func__);
7874
0
    }
7875
7876
0
  for (; list->last < list->n_range &&
7877
0
       list->range[list->last].addr <= last; ++list->last)
7878
0
    if (list->range[list->last].add)
7879
0
      reloc_range_list_append (list, list->range[list->last].irel_index);
7880
7881
0
  for (; list->first < list->n_range &&
7882
0
       list->range[list->first].addr < first; ++list->first)
7883
0
    if (!list->range[list->first].add)
7884
0
      reloc_range_list_remove (list, list->range[list->first].irel_index);
7885
0
}
7886
7887
static void free_reloc_range_list (reloc_range_list *list)
7888
0
{
7889
0
  free (list->range);
7890
0
  free (list->reloc);
7891
0
}
7892
7893
/* The compute_text_actions function will build a list of potential
7894
   transformation actions for code in the extended basic block of each
7895
   longcall that is optimized to a direct call.  From this list we
7896
   generate a set of actions to actually perform that optimizes for
7897
   space and, if not using size_opt, maintains branch target
7898
   alignments.
7899
7900
   These actions to be performed are placed on a per-section list.
7901
   The actual changes are performed by relax_section() in the second
7902
   pass.  */
7903
7904
bool
7905
compute_text_actions (bfd *abfd,
7906
          asection *sec,
7907
          struct bfd_link_info *link_info)
7908
0
{
7909
0
  xtensa_opcode *reloc_opcodes = NULL;
7910
0
  xtensa_relax_info *relax_info;
7911
0
  bfd_byte *contents;
7912
0
  Elf_Internal_Rela *internal_relocs;
7913
0
  bool ok = true;
7914
0
  unsigned i;
7915
0
  property_table_entry *prop_table = 0;
7916
0
  int ptblsize = 0;
7917
0
  bfd_size_type sec_size;
7918
0
  reloc_range_list relevant_relocs;
7919
7920
0
  relax_info = get_xtensa_relax_info (sec);
7921
0
  BFD_ASSERT (relax_info);
7922
0
  BFD_ASSERT (relax_info->src_next == relax_info->src_count);
7923
7924
  /* Do nothing if the section contains no optimized longcalls.  */
7925
0
  if (!relax_info->is_relaxable_asm_section)
7926
0
    return ok;
7927
7928
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
7929
0
                link_info->keep_memory);
7930
7931
0
  if (internal_relocs)
7932
0
    qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
7933
0
     internal_reloc_compare);
7934
7935
0
  sec_size = bfd_get_section_limit (abfd, sec);
7936
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7937
0
  if (contents == NULL && sec_size != 0)
7938
0
    {
7939
0
      ok = false;
7940
0
      goto error_return;
7941
0
    }
7942
7943
0
  ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
7944
0
          XTENSA_PROP_SEC_NAME, false);
7945
0
  if (ptblsize < 0)
7946
0
    {
7947
0
      ok = false;
7948
0
      goto error_return;
7949
0
    }
7950
7951
  /* Precompute the opcode for each relocation.  */
7952
0
  reloc_opcodes = build_reloc_opcodes (abfd, sec, contents, internal_relocs);
7953
7954
0
  build_reloc_ranges (abfd, sec, contents, internal_relocs, reloc_opcodes,
7955
0
          &relevant_relocs);
7956
7957
0
  for (i = 0; i < sec->reloc_count; i++)
7958
0
    {
7959
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
7960
0
      bfd_vma r_offset;
7961
0
      property_table_entry *the_entry;
7962
0
      int ptbl_idx;
7963
0
      ebb_t *ebb;
7964
0
      ebb_constraint ebb_table;
7965
0
      bfd_size_type simplify_size;
7966
7967
0
      if (irel && ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_SIMPLIFY)
7968
0
  continue;
7969
0
      r_offset = irel->r_offset;
7970
7971
0
      simplify_size = get_asm_simplify_size (contents, sec_size, r_offset);
7972
0
      if (simplify_size == 0)
7973
0
  {
7974
0
    _bfd_error_handler
7975
      /* xgettext:c-format */
7976
0
      (_("%pB(%pA+%#" PRIx64 "): could not decode instruction for "
7977
0
         "XTENSA_ASM_SIMPLIFY relocation; "
7978
0
         "possible configuration mismatch"),
7979
0
       sec->owner, sec, (uint64_t) r_offset);
7980
0
    continue;
7981
0
  }
7982
7983
      /* If the instruction table is not around, then don't do this
7984
   relaxation.  */
7985
0
      the_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
7986
0
              sec->vma + irel->r_offset);
7987
0
      if (the_entry == NULL || XTENSA_NO_NOP_REMOVAL)
7988
0
  {
7989
0
    text_action_add (&relax_info->action_list,
7990
0
         ta_convert_longcall, sec, r_offset,
7991
0
         0);
7992
0
    continue;
7993
0
  }
7994
7995
      /* If the next longcall happens to be at the same address as an
7996
   unreachable section of size 0, then skip forward.  */
7997
0
      ptbl_idx = the_entry - prop_table;
7998
0
      while ((the_entry->flags & XTENSA_PROP_UNREACHABLE)
7999
0
       && the_entry->size == 0
8000
0
       && ptbl_idx + 1 < ptblsize
8001
0
       && (prop_table[ptbl_idx + 1].address
8002
0
     == prop_table[ptbl_idx].address))
8003
0
  {
8004
0
    ptbl_idx++;
8005
0
    the_entry++;
8006
0
  }
8007
8008
0
      if (the_entry->flags & XTENSA_PROP_NO_TRANSFORM)
8009
    /* NO_REORDER is OK */
8010
0
  continue;
8011
8012
0
      init_ebb_constraint (&ebb_table);
8013
0
      ebb = &ebb_table.ebb;
8014
0
      init_ebb (ebb, sec, contents, sec_size, prop_table, ptblsize,
8015
0
    internal_relocs, sec->reloc_count);
8016
0
      ebb->start_offset = r_offset + simplify_size;
8017
0
      ebb->end_offset = r_offset + simplify_size;
8018
0
      ebb->start_ptbl_idx = ptbl_idx;
8019
0
      ebb->end_ptbl_idx = ptbl_idx;
8020
0
      ebb->start_reloc_idx = i;
8021
0
      ebb->end_reloc_idx = i;
8022
8023
0
      if (!extend_ebb_bounds (ebb)
8024
0
    || !compute_ebb_proposed_actions (&ebb_table)
8025
0
    || !compute_ebb_actions (&ebb_table)
8026
0
    || !check_section_ebb_pcrels_fit (abfd, sec, contents,
8027
0
              internal_relocs,
8028
0
              &relevant_relocs,
8029
0
              &ebb_table, reloc_opcodes)
8030
0
    || !check_section_ebb_reduces (&ebb_table))
8031
0
  {
8032
    /* If anything goes wrong or we get unlucky and something does
8033
       not fit, with our plan because of expansion between
8034
       critical branches, just convert to a NOP.  */
8035
8036
0
    text_action_add (&relax_info->action_list,
8037
0
         ta_convert_longcall, sec, r_offset, 0);
8038
0
    i = ebb_table.ebb.end_reloc_idx;
8039
0
    free_ebb_constraint (&ebb_table);
8040
0
    continue;
8041
0
  }
8042
8043
0
      text_action_add_proposed (&relax_info->action_list, &ebb_table, sec);
8044
8045
      /* Update the index so we do not go looking at the relocations
8046
   we have already processed.  */
8047
0
      i = ebb_table.ebb.end_reloc_idx;
8048
0
      free_ebb_constraint (&ebb_table);
8049
0
    }
8050
8051
0
  free_reloc_range_list (&relevant_relocs);
8052
8053
#if DEBUG
8054
  if (action_list_count (&relax_info->action_list))
8055
    print_action_list (stderr, &relax_info->action_list);
8056
#endif
8057
8058
0
 error_return:
8059
0
  release_contents (sec, contents);
8060
0
  release_internal_relocs (sec, internal_relocs);
8061
0
  free (prop_table);
8062
0
  free (reloc_opcodes);
8063
8064
0
  return ok;
8065
0
}
8066
8067
8068
/* Do not widen an instruction if it is preceeded by a
8069
   loop opcode.  It might cause misalignment.  */
8070
8071
static bool
8072
prev_instr_is_a_loop (bfd_byte *contents,
8073
          bfd_size_type content_length,
8074
          bfd_size_type offset)
8075
0
{
8076
0
  xtensa_opcode prev_opcode;
8077
8078
0
  if (offset < 3)
8079
0
    return false;
8080
0
  prev_opcode = insn_decode_opcode (contents, content_length, offset-3, 0);
8081
0
  return (xtensa_opcode_is_loop (xtensa_default_isa, prev_opcode) == 1);
8082
0
}
8083
8084
8085
/* Find all of the possible actions for an extended basic block.  */
8086
8087
bool
8088
compute_ebb_proposed_actions (ebb_constraint *ebb_table)
8089
0
{
8090
0
  const ebb_t *ebb = &ebb_table->ebb;
8091
0
  unsigned rel_idx = ebb->start_reloc_idx;
8092
0
  property_table_entry *entry, *start_entry, *end_entry;
8093
0
  bfd_vma offset = 0;
8094
0
  xtensa_isa isa = xtensa_default_isa;
8095
0
  xtensa_format fmt;
8096
0
  static xtensa_insnbuf insnbuf = NULL;
8097
0
  static xtensa_insnbuf slotbuf = NULL;
8098
8099
0
  if (insnbuf == NULL)
8100
0
    {
8101
0
      insnbuf = xtensa_insnbuf_alloc (isa);
8102
0
      slotbuf = xtensa_insnbuf_alloc (isa);
8103
0
    }
8104
8105
0
  start_entry = &ebb->ptbl[ebb->start_ptbl_idx];
8106
0
  end_entry = &ebb->ptbl[ebb->end_ptbl_idx];
8107
8108
0
  for (entry = start_entry; entry <= end_entry; entry++)
8109
0
    {
8110
0
      bfd_vma start_offset, end_offset;
8111
0
      bfd_size_type insn_len;
8112
8113
0
      start_offset = entry->address - ebb->sec->vma;
8114
0
      end_offset = entry->address + entry->size - ebb->sec->vma;
8115
8116
0
      if (entry == start_entry)
8117
0
  start_offset = ebb->start_offset;
8118
0
      if (entry == end_entry)
8119
0
  end_offset = ebb->end_offset;
8120
0
      offset = start_offset;
8121
8122
0
      if (offset == entry->address - ebb->sec->vma
8123
0
    && (entry->flags & XTENSA_PROP_INSN_BRANCH_TARGET) != 0)
8124
0
  {
8125
0
    enum ebb_target_enum align_type = EBB_DESIRE_TGT_ALIGN;
8126
0
    BFD_ASSERT (offset != end_offset);
8127
0
    if (offset == end_offset)
8128
0
      return false;
8129
8130
0
    insn_len = insn_decode_len (ebb->contents, ebb->content_length,
8131
0
              offset);
8132
0
    if (insn_len == 0)
8133
0
      goto decode_error;
8134
8135
0
    if (check_branch_target_aligned_address (offset, insn_len))
8136
0
      align_type = EBB_REQUIRE_TGT_ALIGN;
8137
8138
0
    ebb_propose_action (ebb_table, align_type, 0,
8139
0
            ta_none, offset, 0, true);
8140
0
  }
8141
8142
0
      while (offset != end_offset)
8143
0
  {
8144
0
    Elf_Internal_Rela *irel;
8145
0
    xtensa_opcode opcode;
8146
8147
0
    while (rel_idx < ebb->end_reloc_idx
8148
0
     && (ebb->relocs[rel_idx].r_offset < offset
8149
0
         || (ebb->relocs[rel_idx].r_offset == offset
8150
0
       && (ELF32_R_TYPE (ebb->relocs[rel_idx].r_info)
8151
0
           != R_XTENSA_ASM_SIMPLIFY))))
8152
0
      rel_idx++;
8153
8154
    /* Check for longcall.  */
8155
0
    irel = &ebb->relocs[rel_idx];
8156
0
    if (irel->r_offset == offset
8157
0
        && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_SIMPLIFY)
8158
0
      {
8159
0
        bfd_size_type simplify_size;
8160
8161
0
        simplify_size = get_asm_simplify_size (ebb->contents,
8162
0
                 ebb->content_length,
8163
0
                 irel->r_offset);
8164
0
        if (simplify_size == 0)
8165
0
    goto decode_error;
8166
8167
0
        ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
8168
0
          ta_convert_longcall, offset, 0, true);
8169
8170
0
        offset += simplify_size;
8171
0
        continue;
8172
0
      }
8173
8174
0
    if (offset + MIN_INSN_LENGTH > ebb->content_length)
8175
0
      goto decode_error;
8176
0
    if (xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
8177
0
           ebb->content_length - offset) < 0)
8178
0
      goto decode_error;
8179
0
    fmt = xtensa_format_decode (isa, insnbuf);
8180
0
    if (fmt == XTENSA_UNDEFINED)
8181
0
      goto decode_error;
8182
0
    insn_len = xtensa_format_length (isa, fmt);
8183
0
    if (insn_len == (bfd_size_type) XTENSA_UNDEFINED)
8184
0
      goto decode_error;
8185
8186
0
    if (xtensa_format_num_slots (isa, fmt) != 1)
8187
0
      {
8188
0
        offset += insn_len;
8189
0
        continue;
8190
0
      }
8191
8192
0
    xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
8193
0
    opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
8194
0
    if (opcode == XTENSA_UNDEFINED)
8195
0
      goto decode_error;
8196
8197
0
    if ((entry->flags & XTENSA_PROP_INSN_NO_DENSITY) == 0
8198
0
        && (entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
8199
0
        && can_narrow_instruction (slotbuf, fmt, opcode) != 0)
8200
0
      {
8201
        /* Add an instruction narrow action.  */
8202
0
        ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
8203
0
          ta_narrow_insn, offset, 0, false);
8204
0
      }
8205
0
    else if ((entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
8206
0
       && can_widen_instruction (slotbuf, fmt, opcode) != 0
8207
0
       && ! prev_instr_is_a_loop (ebb->contents,
8208
0
                ebb->content_length, offset))
8209
0
      {
8210
        /* Add an instruction widen action.  */
8211
0
        ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
8212
0
          ta_widen_insn, offset, 0, false);
8213
0
      }
8214
0
    else if (xtensa_opcode_is_loop (xtensa_default_isa, opcode) == 1)
8215
0
      {
8216
        /* Check for branch targets.  */
8217
0
        ebb_propose_action (ebb_table, EBB_REQUIRE_LOOP_ALIGN, 0,
8218
0
          ta_none, offset, 0, true);
8219
0
      }
8220
8221
0
    offset += insn_len;
8222
0
  }
8223
0
    }
8224
8225
0
  if (ebb->ends_unreachable)
8226
0
    {
8227
0
      ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
8228
0
        ta_fill, ebb->end_offset, 0, true);
8229
0
    }
8230
8231
0
  return true;
8232
8233
0
 decode_error:
8234
0
  _bfd_error_handler
8235
    /* xgettext:c-format */
8236
0
    (_("%pB(%pA+%#" PRIx64 "): could not decode instruction; "
8237
0
       "possible configuration mismatch"),
8238
0
     ebb->sec->owner, ebb->sec, (uint64_t) offset);
8239
0
  return false;
8240
0
}
8241
8242
8243
/* After all of the information has collected about the
8244
   transformations possible in an EBB, compute the appropriate actions
8245
   here in compute_ebb_actions.  We still must check later to make
8246
   sure that the actions do not break any relocations.  The algorithm
8247
   used here is pretty greedy.  Basically, it removes as many no-ops
8248
   as possible so that the end of the EBB has the same alignment
8249
   characteristics as the original.  First, it uses narrowing, then
8250
   fill space at the end of the EBB, and finally widenings.  If that
8251
   does not work, it tries again with one fewer no-op removed.  The
8252
   optimization will only be performed if all of the branch targets
8253
   that were aligned before transformation are also aligned after the
8254
   transformation.
8255
8256
   When the size_opt flag is set, ignore the branch target alignments,
8257
   narrow all wide instructions, and remove all no-ops unless the end
8258
   of the EBB prevents it.  */
8259
8260
bool
8261
compute_ebb_actions (ebb_constraint *ebb_table)
8262
0
{
8263
0
  unsigned i = 0;
8264
0
  unsigned j;
8265
0
  int removed_bytes = 0;
8266
0
  ebb_t *ebb = &ebb_table->ebb;
8267
0
  unsigned seg_idx_start = 0;
8268
0
  unsigned seg_idx_end = 0;
8269
8270
  /* We perform this like the assembler relaxation algorithm: Start by
8271
     assuming all instructions are narrow and all no-ops removed; then
8272
     walk through....  */
8273
8274
  /* For each segment of this that has a solid constraint, check to
8275
     see if there are any combinations that will keep the constraint.
8276
     If so, use it.  */
8277
0
  for (seg_idx_end = 0; seg_idx_end < ebb_table->action_count; seg_idx_end++)
8278
0
    {
8279
0
      bool requires_text_end_align = false;
8280
0
      unsigned longcall_count = 0;
8281
0
      unsigned longcall_convert_count = 0;
8282
0
      unsigned narrowable_count = 0;
8283
0
      unsigned narrowable_convert_count = 0;
8284
0
      unsigned widenable_count = 0;
8285
0
      unsigned widenable_convert_count = 0;
8286
8287
0
      proposed_action *action = NULL;
8288
0
      int align = (1 << ebb_table->ebb.sec->alignment_power);
8289
8290
0
      seg_idx_start = seg_idx_end;
8291
8292
0
      for (i = seg_idx_start; i < ebb_table->action_count; i++)
8293
0
  {
8294
0
    action = &ebb_table->actions[i];
8295
0
    if (action->action == ta_convert_longcall)
8296
0
      longcall_count++;
8297
0
    if (action->action == ta_narrow_insn)
8298
0
      narrowable_count++;
8299
0
    if (action->action == ta_widen_insn)
8300
0
      widenable_count++;
8301
0
    if (action->action == ta_fill)
8302
0
      break;
8303
0
    if (action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8304
0
      break;
8305
0
    if (action->align_type == EBB_REQUIRE_TGT_ALIGN
8306
0
        && !elf32xtensa_size_opt)
8307
0
      break;
8308
0
  }
8309
0
      seg_idx_end = i;
8310
8311
0
      if (seg_idx_end == ebb_table->action_count && !ebb->ends_unreachable)
8312
0
  requires_text_end_align = true;
8313
8314
0
      if (elf32xtensa_size_opt && !requires_text_end_align
8315
0
    && action->align_type != EBB_REQUIRE_LOOP_ALIGN
8316
0
    && action->align_type != EBB_REQUIRE_TGT_ALIGN)
8317
0
  {
8318
0
    longcall_convert_count = longcall_count;
8319
0
    narrowable_convert_count = narrowable_count;
8320
0
    widenable_convert_count = 0;
8321
0
  }
8322
0
      else
8323
0
  {
8324
    /* There is a constraint.  Convert the max number of longcalls.  */
8325
0
    narrowable_convert_count = 0;
8326
0
    longcall_convert_count = 0;
8327
0
    widenable_convert_count = 0;
8328
8329
0
    for (j = 0; j < longcall_count; j++)
8330
0
      {
8331
0
        int removed = (longcall_count - j) * 3 & (align - 1);
8332
0
        unsigned desire_narrow = (align - removed) & (align - 1);
8333
0
        unsigned desire_widen = removed;
8334
0
        if (desire_narrow <= narrowable_count)
8335
0
    {
8336
0
      narrowable_convert_count = desire_narrow;
8337
0
      narrowable_convert_count +=
8338
0
        (align * ((narrowable_count - narrowable_convert_count)
8339
0
            / align));
8340
0
      longcall_convert_count = (longcall_count - j);
8341
0
      widenable_convert_count = 0;
8342
0
      break;
8343
0
    }
8344
0
        if (desire_widen <= widenable_count && !elf32xtensa_size_opt)
8345
0
    {
8346
0
      narrowable_convert_count = 0;
8347
0
      longcall_convert_count = longcall_count - j;
8348
0
      widenable_convert_count = desire_widen;
8349
0
      break;
8350
0
    }
8351
0
      }
8352
0
  }
8353
8354
      /* Now the number of conversions are saved.  Do them.  */
8355
0
      for (i = seg_idx_start; i < seg_idx_end; i++)
8356
0
  {
8357
0
    action = &ebb_table->actions[i];
8358
0
    switch (action->action)
8359
0
      {
8360
0
      case ta_convert_longcall:
8361
0
        if (longcall_convert_count != 0)
8362
0
    {
8363
0
      action->action = ta_remove_longcall;
8364
0
      action->do_action = true;
8365
0
      action->removed_bytes += 3;
8366
0
      longcall_convert_count--;
8367
0
    }
8368
0
        break;
8369
0
      case ta_narrow_insn:
8370
0
        if (narrowable_convert_count != 0)
8371
0
    {
8372
0
      action->do_action = true;
8373
0
      action->removed_bytes += 1;
8374
0
      narrowable_convert_count--;
8375
0
    }
8376
0
        break;
8377
0
      case ta_widen_insn:
8378
0
        if (widenable_convert_count != 0)
8379
0
    {
8380
0
      action->do_action = true;
8381
0
      action->removed_bytes -= 1;
8382
0
      widenable_convert_count--;
8383
0
    }
8384
0
        break;
8385
0
      default:
8386
0
        break;
8387
0
      }
8388
0
  }
8389
0
    }
8390
8391
  /* Now we move on to some local opts.  Try to remove each of the
8392
     remaining longcalls.  */
8393
8394
0
  if (ebb_table->ebb.ends_section || ebb_table->ebb.ends_unreachable)
8395
0
    {
8396
0
      removed_bytes = 0;
8397
0
      for (i = 0; i < ebb_table->action_count; i++)
8398
0
  {
8399
0
    int old_removed_bytes = removed_bytes;
8400
0
    proposed_action *action = &ebb_table->actions[i];
8401
8402
0
    if (action->do_action && action->action == ta_convert_longcall)
8403
0
      {
8404
0
        bool bad_alignment = false;
8405
0
        removed_bytes += 3;
8406
0
        for (j = i + 1; j < ebb_table->action_count; j++)
8407
0
    {
8408
0
      proposed_action *new_action = &ebb_table->actions[j];
8409
0
      bfd_vma offset = new_action->offset;
8410
0
      if (new_action->align_type == EBB_REQUIRE_TGT_ALIGN)
8411
0
        {
8412
0
          if (!check_branch_target_aligned
8413
0
        (ebb_table->ebb.contents,
8414
0
         ebb_table->ebb.content_length,
8415
0
         offset, offset - removed_bytes))
8416
0
      {
8417
0
        bad_alignment = true;
8418
0
        break;
8419
0
      }
8420
0
        }
8421
0
      if (new_action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8422
0
        {
8423
0
          if (!check_loop_aligned (ebb_table->ebb.contents,
8424
0
                 ebb_table->ebb.content_length,
8425
0
                 offset,
8426
0
                 offset - removed_bytes))
8427
0
      {
8428
0
        bad_alignment = true;
8429
0
        break;
8430
0
      }
8431
0
        }
8432
0
      if (new_action->action == ta_narrow_insn
8433
0
          && !new_action->do_action
8434
0
          && ebb_table->ebb.sec->alignment_power == 2)
8435
0
        {
8436
          /* Narrow an instruction and we are done.  */
8437
0
          new_action->do_action = true;
8438
0
          new_action->removed_bytes += 1;
8439
0
          bad_alignment = false;
8440
0
          break;
8441
0
        }
8442
0
      if (new_action->action == ta_widen_insn
8443
0
          && new_action->do_action
8444
0
          && ebb_table->ebb.sec->alignment_power == 2)
8445
0
        {
8446
          /* Narrow an instruction and we are done.  */
8447
0
          new_action->do_action = false;
8448
0
          new_action->removed_bytes += 1;
8449
0
          bad_alignment = false;
8450
0
          break;
8451
0
        }
8452
0
      if (new_action->do_action)
8453
0
        removed_bytes += new_action->removed_bytes;
8454
0
    }
8455
0
        if (!bad_alignment)
8456
0
    {
8457
0
      action->removed_bytes += 3;
8458
0
      action->action = ta_remove_longcall;
8459
0
      action->do_action = true;
8460
0
    }
8461
0
      }
8462
0
    removed_bytes = old_removed_bytes;
8463
0
    if (action->do_action)
8464
0
      removed_bytes += action->removed_bytes;
8465
0
  }
8466
0
    }
8467
8468
0
  removed_bytes = 0;
8469
0
  for (i = 0; i < ebb_table->action_count; ++i)
8470
0
    {
8471
0
      proposed_action *action = &ebb_table->actions[i];
8472
0
      if (action->do_action)
8473
0
  removed_bytes += action->removed_bytes;
8474
0
    }
8475
8476
0
  if ((removed_bytes % (1 << ebb_table->ebb.sec->alignment_power)) != 0
8477
0
      && ebb->ends_unreachable)
8478
0
    {
8479
0
      proposed_action *action;
8480
0
      int br;
8481
0
      int extra_space;
8482
8483
0
      BFD_ASSERT (ebb_table->action_count != 0);
8484
0
      action = &ebb_table->actions[ebb_table->action_count - 1];
8485
0
      BFD_ASSERT (action->action == ta_fill);
8486
0
      BFD_ASSERT (ebb->ends_unreachable->flags & XTENSA_PROP_UNREACHABLE);
8487
8488
0
      extra_space = xtensa_compute_fill_extra_space (ebb->ends_unreachable);
8489
0
      br = action->removed_bytes + removed_bytes + extra_space;
8490
0
      br = br & ((1 << ebb->sec->alignment_power ) - 1);
8491
8492
0
      action->removed_bytes = extra_space - br;
8493
0
    }
8494
0
  return true;
8495
0
}
8496
8497
8498
/* The xlate_map is a sorted array of address mappings designed to
8499
   answer the offset_with_removed_text() query with a binary search instead
8500
   of a linear search through the section's action_list.  */
8501
8502
typedef struct xlate_map_entry xlate_map_entry_t;
8503
typedef struct xlate_map xlate_map_t;
8504
8505
struct xlate_map_entry
8506
{
8507
  bfd_vma orig_address;
8508
  bfd_vma new_address;
8509
  unsigned size;
8510
};
8511
8512
struct xlate_map
8513
{
8514
  unsigned entry_count;
8515
  xlate_map_entry_t *entry;
8516
};
8517
8518
8519
static int
8520
xlate_compare (const void *a_v, const void *b_v)
8521
0
{
8522
0
  const xlate_map_entry_t *a = (const xlate_map_entry_t *) a_v;
8523
0
  const xlate_map_entry_t *b = (const xlate_map_entry_t *) b_v;
8524
0
  if (a->orig_address < b->orig_address)
8525
0
    return -1;
8526
0
  if (a->orig_address > (b->orig_address + b->size - 1))
8527
0
    return 1;
8528
0
  return 0;
8529
0
}
8530
8531
8532
static bfd_vma
8533
xlate_offset_with_removed_text (const xlate_map_t *map,
8534
        text_action_list *action_list,
8535
        bfd_vma offset)
8536
0
{
8537
0
  void *r;
8538
0
  xlate_map_entry_t *e;
8539
0
  struct xlate_map_entry se;
8540
8541
0
  if (map == NULL)
8542
0
    return offset_with_removed_text (action_list, offset);
8543
8544
0
  if (map->entry_count == 0)
8545
0
    return offset;
8546
8547
0
  se.orig_address = offset;
8548
0
  r = bsearch (&se, map->entry, map->entry_count,
8549
0
         sizeof (xlate_map_entry_t), &xlate_compare);
8550
0
  e = (xlate_map_entry_t *) r;
8551
8552
  /* There could be a jump past the end of the section,
8553
     allow it using the last xlate map entry to translate its address.  */
8554
0
  if (e == NULL)
8555
0
    {
8556
0
      e = map->entry + map->entry_count - 1;
8557
0
      if (xlate_compare (&se, e) <= 0)
8558
0
  e = NULL;
8559
0
    }
8560
0
  BFD_ASSERT (e != NULL);
8561
0
  if (e == NULL)
8562
0
    return offset;
8563
0
  return e->new_address - e->orig_address + offset;
8564
0
}
8565
8566
typedef struct xlate_map_context_struct xlate_map_context;
8567
struct xlate_map_context_struct
8568
{
8569
  xlate_map_t *map;
8570
  xlate_map_entry_t *current_entry;
8571
  int removed;
8572
};
8573
8574
static int
8575
xlate_map_fn (splay_tree_node node, void *p)
8576
0
{
8577
0
  text_action *r = (text_action *)node->value;
8578
0
  xlate_map_context *ctx = p;
8579
0
  unsigned orig_size = 0;
8580
8581
0
  switch (r->action)
8582
0
    {
8583
0
    case ta_none:
8584
0
    case ta_remove_insn:
8585
0
    case ta_convert_longcall:
8586
0
    case ta_remove_literal:
8587
0
    case ta_add_literal:
8588
0
      break;
8589
0
    case ta_remove_longcall:
8590
0
      orig_size = 6;
8591
0
      break;
8592
0
    case ta_narrow_insn:
8593
0
      orig_size = 3;
8594
0
      break;
8595
0
    case ta_widen_insn:
8596
0
      orig_size = 2;
8597
0
      break;
8598
0
    case ta_fill:
8599
0
      break;
8600
0
    }
8601
0
  ctx->current_entry->size =
8602
0
    r->offset + orig_size - ctx->current_entry->orig_address;
8603
0
  if (ctx->current_entry->size != 0)
8604
0
    {
8605
0
      ctx->current_entry++;
8606
0
      ctx->map->entry_count++;
8607
0
    }
8608
0
  ctx->current_entry->orig_address = r->offset + orig_size;
8609
0
  ctx->removed += r->removed_bytes;
8610
0
  ctx->current_entry->new_address = r->offset + orig_size - ctx->removed;
8611
0
  ctx->current_entry->size = 0;
8612
0
  return 0;
8613
0
}
8614
8615
/* Build a binary searchable offset translation map from a section's
8616
   action list.  */
8617
8618
static xlate_map_t *
8619
build_xlate_map (asection *sec, xtensa_relax_info *relax_info)
8620
0
{
8621
0
  text_action_list *action_list = &relax_info->action_list;
8622
0
  unsigned num_actions = 0;
8623
0
  xlate_map_context ctx;
8624
8625
0
  ctx.map = (xlate_map_t *) bfd_malloc (sizeof (xlate_map_t));
8626
8627
0
  if (ctx.map == NULL)
8628
0
    return NULL;
8629
8630
0
  num_actions = action_list_count (action_list);
8631
0
  ctx.map->entry = (xlate_map_entry_t *)
8632
0
    bfd_malloc (sizeof (xlate_map_entry_t) * (num_actions + 1));
8633
0
  if (ctx.map->entry == NULL)
8634
0
    {
8635
0
      free (ctx.map);
8636
0
      return NULL;
8637
0
    }
8638
0
  ctx.map->entry_count = 0;
8639
8640
0
  ctx.removed = 0;
8641
0
  ctx.current_entry = &ctx.map->entry[0];
8642
8643
0
  ctx.current_entry->orig_address = 0;
8644
0
  ctx.current_entry->new_address = 0;
8645
0
  ctx.current_entry->size = 0;
8646
8647
0
  splay_tree_foreach (action_list->tree, xlate_map_fn, &ctx);
8648
8649
0
  ctx.current_entry->size = (bfd_get_section_limit (sec->owner, sec)
8650
0
           - ctx.current_entry->orig_address);
8651
0
  if (ctx.current_entry->size != 0)
8652
0
    ctx.map->entry_count++;
8653
8654
0
  return ctx.map;
8655
0
}
8656
8657
8658
/* Free an offset translation map.  */
8659
8660
static void
8661
free_xlate_map (xlate_map_t *map)
8662
0
{
8663
0
  if (map)
8664
0
    {
8665
0
      free (map->entry);
8666
0
      free (map);
8667
0
    }
8668
0
}
8669
8670
8671
/* Use check_section_ebb_pcrels_fit to make sure that all of the
8672
   relocations in a section will fit if a proposed set of actions
8673
   are performed.  */
8674
8675
static bool
8676
check_section_ebb_pcrels_fit (bfd *abfd,
8677
            asection *sec,
8678
            bfd_byte *contents,
8679
            Elf_Internal_Rela *internal_relocs,
8680
            reloc_range_list *relevant_relocs,
8681
            const ebb_constraint *constraint,
8682
            const xtensa_opcode *reloc_opcodes)
8683
0
{
8684
0
  unsigned i, j;
8685
0
  unsigned n = sec->reloc_count;
8686
0
  Elf_Internal_Rela *irel;
8687
0
  xlate_map_t *xmap = NULL;
8688
0
  bool ok = true;
8689
0
  xtensa_relax_info *relax_info;
8690
0
  reloc_range_list_entry *entry = NULL;
8691
8692
0
  relax_info = get_xtensa_relax_info (sec);
8693
8694
0
  if (relax_info && sec->reloc_count > 100)
8695
0
    {
8696
0
      xmap = build_xlate_map (sec, relax_info);
8697
      /* NULL indicates out of memory, but the slow version
8698
   can still be used.  */
8699
0
    }
8700
8701
0
  if (relevant_relocs && constraint->action_count)
8702
0
    {
8703
0
      if (!relevant_relocs->ok)
8704
0
  {
8705
0
    ok = false;
8706
0
    n = 0;
8707
0
  }
8708
0
      else
8709
0
  {
8710
0
    bfd_vma min_offset, max_offset;
8711
0
    min_offset = max_offset = constraint->actions[0].offset;
8712
8713
0
    for (i = 1; i < constraint->action_count; ++i)
8714
0
      {
8715
0
        proposed_action *action = &constraint->actions[i];
8716
0
        bfd_vma offset = action->offset;
8717
8718
0
        if (offset < min_offset)
8719
0
    min_offset = offset;
8720
0
        if (offset > max_offset)
8721
0
    max_offset = offset;
8722
0
      }
8723
0
    reloc_range_list_update_range (relevant_relocs, min_offset,
8724
0
           max_offset);
8725
0
    n = relevant_relocs->n_list;
8726
0
    entry = &relevant_relocs->list_root;
8727
0
  }
8728
0
    }
8729
0
  else
8730
0
    {
8731
0
      relevant_relocs = NULL;
8732
0
    }
8733
8734
0
  for (i = 0; i < n; i++)
8735
0
    {
8736
0
      r_reloc r_rel;
8737
0
      bfd_vma orig_self_offset, orig_target_offset;
8738
0
      bfd_vma self_offset, target_offset;
8739
0
      int r_type;
8740
0
      reloc_howto_type *howto;
8741
0
      int self_removed_bytes, target_removed_bytes;
8742
8743
0
      if (relevant_relocs)
8744
0
  {
8745
0
    entry = entry->next;
8746
0
    irel = entry->irel;
8747
0
  }
8748
0
      else
8749
0
  {
8750
0
    irel = internal_relocs + i;
8751
0
  }
8752
0
      r_type = ELF32_R_TYPE (irel->r_info);
8753
8754
0
      howto = &elf_howto_table[r_type];
8755
      /* We maintain the required invariant: PC-relative relocations
8756
   that fit before linking must fit after linking.  Thus we only
8757
   need to deal with relocations to the same section that are
8758
   PC-relative.  */
8759
0
      if (r_type == R_XTENSA_ASM_SIMPLIFY
8760
0
    || r_type == R_XTENSA_32_PCREL
8761
0
    || !howto->pc_relative)
8762
0
  continue;
8763
8764
0
      r_reloc_init (&r_rel, abfd, irel, contents,
8765
0
        bfd_get_section_limit (abfd, sec));
8766
8767
0
      if (r_reloc_get_section (&r_rel) != sec)
8768
0
  continue;
8769
8770
0
      orig_self_offset = irel->r_offset;
8771
0
      orig_target_offset = r_rel.target_offset;
8772
8773
0
      self_offset = orig_self_offset;
8774
0
      target_offset = orig_target_offset;
8775
8776
0
      if (relax_info)
8777
0
  {
8778
0
    self_offset =
8779
0
      xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8780
0
              orig_self_offset);
8781
0
    target_offset =
8782
0
      xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8783
0
              orig_target_offset);
8784
0
  }
8785
8786
0
      self_removed_bytes = 0;
8787
0
      target_removed_bytes = 0;
8788
8789
0
      for (j = 0; j < constraint->action_count; ++j)
8790
0
  {
8791
0
    proposed_action *action = &constraint->actions[j];
8792
0
    bfd_vma offset = action->offset;
8793
0
    int removed_bytes = action->removed_bytes;
8794
0
    if (offset < orig_self_offset
8795
0
        || (offset == orig_self_offset && action->action == ta_fill
8796
0
      && action->removed_bytes < 0))
8797
0
      self_removed_bytes += removed_bytes;
8798
0
    if (offset < orig_target_offset
8799
0
        || (offset == orig_target_offset && action->action == ta_fill
8800
0
      && action->removed_bytes < 0))
8801
0
      target_removed_bytes += removed_bytes;
8802
0
  }
8803
0
      self_offset -= self_removed_bytes;
8804
0
      target_offset -= target_removed_bytes;
8805
8806
      /* Try to encode it.  Get the operand and check.  */
8807
0
      if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
8808
0
  {
8809
    /* None of the current alternate relocs are PC-relative,
8810
       and only PC-relative relocs matter here.  */
8811
0
  }
8812
0
      else
8813
0
  {
8814
0
    xtensa_opcode opcode;
8815
0
    int opnum;
8816
8817
0
    if (relevant_relocs)
8818
0
      {
8819
0
        opcode = entry->opcode;
8820
0
        opnum = entry->opnum;
8821
0
      }
8822
0
    else
8823
0
      {
8824
0
        if (reloc_opcodes)
8825
0
    opcode = reloc_opcodes[relevant_relocs ?
8826
0
      (unsigned)(entry - relevant_relocs->reloc) : i];
8827
0
        else
8828
0
    opcode = get_relocation_opcode (abfd, sec, contents, irel);
8829
0
        if (opcode == XTENSA_UNDEFINED)
8830
0
    {
8831
0
      ok = false;
8832
0
      break;
8833
0
    }
8834
8835
0
        opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
8836
0
        if (opnum == XTENSA_UNDEFINED)
8837
0
    {
8838
0
      ok = false;
8839
0
      break;
8840
0
    }
8841
0
      }
8842
8843
0
    if (!pcrel_reloc_fits (opcode, opnum, self_offset, target_offset))
8844
0
      {
8845
0
        ok = false;
8846
0
        break;
8847
0
      }
8848
0
  }
8849
0
    }
8850
8851
0
  free_xlate_map (xmap);
8852
8853
0
  return ok;
8854
0
}
8855
8856
8857
static bool
8858
check_section_ebb_reduces (const ebb_constraint *constraint)
8859
0
{
8860
0
  int removed = 0;
8861
0
  unsigned i;
8862
8863
0
  for (i = 0; i < constraint->action_count; i++)
8864
0
    {
8865
0
      const proposed_action *action = &constraint->actions[i];
8866
0
      if (action->do_action)
8867
0
  removed += action->removed_bytes;
8868
0
    }
8869
0
  if (removed < 0)
8870
0
    return false;
8871
8872
0
  return true;
8873
0
}
8874
8875
8876
void
8877
text_action_add_proposed (text_action_list *l,
8878
        const ebb_constraint *ebb_table,
8879
        asection *sec)
8880
0
{
8881
0
  unsigned i;
8882
8883
0
  for (i = 0; i < ebb_table->action_count; i++)
8884
0
    {
8885
0
      proposed_action *action = &ebb_table->actions[i];
8886
8887
0
      if (!action->do_action)
8888
0
  continue;
8889
0
      switch (action->action)
8890
0
  {
8891
0
  case ta_remove_insn:
8892
0
  case ta_remove_longcall:
8893
0
  case ta_convert_longcall:
8894
0
  case ta_narrow_insn:
8895
0
  case ta_widen_insn:
8896
0
  case ta_fill:
8897
0
  case ta_remove_literal:
8898
0
    text_action_add (l, action->action, sec, action->offset,
8899
0
         action->removed_bytes);
8900
0
    break;
8901
0
  case ta_none:
8902
0
    break;
8903
0
  default:
8904
0
    BFD_ASSERT (0);
8905
0
    break;
8906
0
  }
8907
0
    }
8908
0
}
8909
8910
8911
int
8912
xtensa_compute_fill_extra_space (property_table_entry *entry)
8913
0
{
8914
0
  int fill_extra_space;
8915
8916
0
  if (!entry)
8917
0
    return 0;
8918
8919
0
  if ((entry->flags & XTENSA_PROP_UNREACHABLE) == 0)
8920
0
    return 0;
8921
8922
0
  fill_extra_space = entry->size;
8923
0
  if ((entry->flags & XTENSA_PROP_ALIGN) != 0)
8924
0
    {
8925
      /* Fill bytes for alignment:
8926
   (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
8927
0
      int pow = GET_XTENSA_PROP_ALIGNMENT (entry->flags);
8928
0
      int nsm = (1 << pow) - 1;
8929
0
      bfd_vma addr = entry->address + entry->size;
8930
0
      bfd_vma align_fill = nsm - ((addr + nsm) & nsm);
8931
0
      fill_extra_space += align_fill;
8932
0
    }
8933
0
  return fill_extra_space;
8934
0
}
8935
8936

8937
/* First relaxation pass.  */
8938
8939
/* If the section contains relaxable literals, check each literal to
8940
   see if it has the same value as another literal that has already
8941
   been seen, either in the current section or a previous one.  If so,
8942
   add an entry to the per-section list of removed literals.  The
8943
   actual changes are deferred until the next pass.  */
8944
8945
static bool
8946
compute_removed_literals (bfd *abfd,
8947
        asection *sec,
8948
        struct bfd_link_info *link_info,
8949
        value_map_hash_table *values)
8950
0
{
8951
0
  xtensa_relax_info *relax_info;
8952
0
  bfd_byte *contents;
8953
0
  Elf_Internal_Rela *internal_relocs;
8954
0
  source_reloc *src_relocs, *rel;
8955
0
  bool ok = true;
8956
0
  property_table_entry *prop_table = NULL;
8957
0
  int ptblsize;
8958
0
  int i, prev_i;
8959
0
  bool last_loc_is_prev = false;
8960
0
  bfd_vma last_target_offset = 0;
8961
0
  section_cache_t target_sec_cache;
8962
0
  bfd_size_type sec_size;
8963
8964
0
  init_section_cache (&target_sec_cache);
8965
8966
  /* Do nothing if it is not a relaxable literal section.  */
8967
0
  relax_info = get_xtensa_relax_info (sec);
8968
0
  BFD_ASSERT (relax_info);
8969
0
  if (!relax_info->is_relaxable_literal_section)
8970
0
    return ok;
8971
8972
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
8973
0
                link_info->keep_memory);
8974
8975
0
  sec_size = bfd_get_section_limit (abfd, sec);
8976
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
8977
0
  if (contents == NULL && sec_size != 0)
8978
0
    {
8979
0
      ok = false;
8980
0
      goto error_return;
8981
0
    }
8982
8983
  /* Sort the source_relocs by target offset.  */
8984
0
  src_relocs = relax_info->src_relocs;
8985
0
  qsort (src_relocs, relax_info->src_count,
8986
0
   sizeof (source_reloc), source_reloc_compare);
8987
0
  qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
8988
0
   internal_reloc_compare);
8989
8990
0
  ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
8991
0
          XTENSA_PROP_SEC_NAME, false);
8992
0
  if (ptblsize < 0)
8993
0
    {
8994
0
      ok = false;
8995
0
      goto error_return;
8996
0
    }
8997
8998
0
  prev_i = -1;
8999
0
  for (i = 0; i < relax_info->src_count; i++)
9000
0
    {
9001
0
      Elf_Internal_Rela *irel = NULL;
9002
9003
0
      rel = &src_relocs[i];
9004
0
      if (get_l32r_opcode () != rel->opcode)
9005
0
  continue;
9006
0
      irel = get_irel_at_offset (sec, internal_relocs,
9007
0
         rel->r_rel.target_offset);
9008
9009
      /* If the relocation on this is not a simple R_XTENSA_32,
9010
   R_XTENSA_32_ABS or R_XTENSA_PLT then do not consider it.  This may
9011
   happen when the difference of two symbols is used in a literal.  */
9012
0
      if (irel && (ELF32_R_TYPE (irel->r_info) != R_XTENSA_32
9013
0
       && ELF32_R_TYPE (irel->r_info) != R_XTENSA_32_ABS
9014
0
       && ELF32_R_TYPE (irel->r_info) != R_XTENSA_PLT))
9015
0
  continue;
9016
9017
      /* If the target_offset for this relocation is the same as the
9018
   previous relocation, then we've already considered whether the
9019
   literal can be coalesced.  Skip to the next one....  */
9020
0
      if (i != 0 && prev_i != -1
9021
0
    && src_relocs[i-1].r_rel.target_offset == rel->r_rel.target_offset)
9022
0
  continue;
9023
0
      prev_i = i;
9024
9025
0
      if (last_loc_is_prev &&
9026
0
    last_target_offset + 4 != rel->r_rel.target_offset)
9027
0
  last_loc_is_prev = false;
9028
9029
      /* Check if the relocation was from an L32R that is being removed
9030
   because a CALLX was converted to a direct CALL, and check if
9031
   there are no other relocations to the literal.  */
9032
0
      if (is_removable_literal (rel, i, src_relocs, relax_info->src_count,
9033
0
        sec, prop_table, ptblsize))
9034
0
  {
9035
0
    if (!remove_dead_literal (abfd, sec, link_info, internal_relocs,
9036
0
            irel, rel, prop_table, ptblsize))
9037
0
      {
9038
0
        ok = false;
9039
0
        goto error_return;
9040
0
      }
9041
0
    last_target_offset = rel->r_rel.target_offset;
9042
0
    continue;
9043
0
  }
9044
9045
0
      if (!identify_literal_placement (abfd, sec, contents, link_info,
9046
0
               values,
9047
0
               &last_loc_is_prev, irel,
9048
0
               relax_info->src_count - i, rel,
9049
0
               prop_table, ptblsize,
9050
0
               &target_sec_cache, rel->is_abs_literal))
9051
0
  {
9052
0
    ok = false;
9053
0
    goto error_return;
9054
0
  }
9055
0
      last_target_offset = rel->r_rel.target_offset;
9056
0
    }
9057
9058
#if DEBUG
9059
  print_removed_literals (stderr, &relax_info->removed_list);
9060
  print_action_list (stderr, &relax_info->action_list);
9061
#endif /* DEBUG */
9062
9063
0
 error_return:
9064
0
  free (prop_table);
9065
0
  free_section_cache (&target_sec_cache);
9066
9067
0
  release_contents (sec, contents);
9068
0
  release_internal_relocs (sec, internal_relocs);
9069
0
  return ok;
9070
0
}
9071
9072
9073
static Elf_Internal_Rela *
9074
get_irel_at_offset (asection *sec,
9075
        Elf_Internal_Rela *internal_relocs,
9076
        bfd_vma offset)
9077
0
{
9078
0
  unsigned i;
9079
0
  Elf_Internal_Rela *irel;
9080
0
  unsigned r_type;
9081
0
  Elf_Internal_Rela key;
9082
9083
0
  if (!internal_relocs)
9084
0
    return NULL;
9085
9086
0
  key.r_offset = offset;
9087
0
  irel = bsearch (&key, internal_relocs, sec->reloc_count,
9088
0
      sizeof (Elf_Internal_Rela), internal_reloc_matches);
9089
0
  if (!irel)
9090
0
    return NULL;
9091
9092
  /* bsearch does not guarantee which will be returned if there are
9093
     multiple matches.  We need the first that is not an alignment.  */
9094
0
  i = irel - internal_relocs;
9095
0
  while (i > 0)
9096
0
    {
9097
0
      if (internal_relocs[i-1].r_offset != offset)
9098
0
  break;
9099
0
      i--;
9100
0
    }
9101
0
  for ( ; i < sec->reloc_count; i++)
9102
0
    {
9103
0
      irel = &internal_relocs[i];
9104
0
      r_type = ELF32_R_TYPE (irel->r_info);
9105
0
      if (irel->r_offset == offset && r_type != R_XTENSA_NONE)
9106
0
  return irel;
9107
0
    }
9108
9109
0
  return NULL;
9110
0
}
9111
9112
9113
bool
9114
is_removable_literal (const source_reloc *rel,
9115
          int i,
9116
          const source_reloc *src_relocs,
9117
          int src_count,
9118
          asection *sec,
9119
          property_table_entry *prop_table,
9120
          int ptblsize)
9121
0
{
9122
0
  const source_reloc *curr_rel;
9123
0
  property_table_entry *entry;
9124
9125
0
  if (!rel->is_null)
9126
0
    return false;
9127
9128
0
  entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9129
0
            sec->vma + rel->r_rel.target_offset);
9130
0
  if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
9131
0
    return false;
9132
9133
0
  for (++i; i < src_count; ++i)
9134
0
    {
9135
0
      curr_rel = &src_relocs[i];
9136
      /* If all others have the same target offset....  */
9137
0
      if (curr_rel->r_rel.target_offset != rel->r_rel.target_offset)
9138
0
  return true;
9139
9140
0
      if (!curr_rel->is_null
9141
0
    && !xtensa_is_property_section (curr_rel->source_sec)
9142
0
    && !(curr_rel->source_sec->flags & SEC_DEBUGGING))
9143
0
  return false;
9144
0
    }
9145
0
  return true;
9146
0
}
9147
9148
9149
bool
9150
remove_dead_literal (bfd *abfd,
9151
         asection *sec,
9152
         struct bfd_link_info *link_info,
9153
         Elf_Internal_Rela *internal_relocs,
9154
         Elf_Internal_Rela *irel,
9155
         source_reloc *rel,
9156
         property_table_entry *prop_table,
9157
         int ptblsize)
9158
0
{
9159
0
  property_table_entry *entry;
9160
0
  xtensa_relax_info *relax_info;
9161
9162
0
  relax_info = get_xtensa_relax_info (sec);
9163
0
  if (!relax_info)
9164
0
    return false;
9165
9166
0
  entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9167
0
            sec->vma + rel->r_rel.target_offset);
9168
9169
  /* Mark the unused literal so that it will be removed.  */
9170
0
  add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
9171
9172
0
  text_action_add (&relax_info->action_list,
9173
0
       ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9174
9175
  /* If the section is 4-byte aligned, do not add fill.  */
9176
0
  if (sec->alignment_power > 2)
9177
0
    {
9178
0
      int fill_extra_space;
9179
0
      bfd_vma entry_sec_offset;
9180
0
      text_action *fa;
9181
0
      property_table_entry *the_add_entry;
9182
0
      int removed_diff;
9183
9184
0
      if (entry)
9185
0
  entry_sec_offset = entry->address - sec->vma + entry->size;
9186
0
      else
9187
0
  entry_sec_offset = rel->r_rel.target_offset + 4;
9188
9189
      /* If the literal range is at the end of the section,
9190
   do not add fill.  */
9191
0
      the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9192
0
                  entry_sec_offset);
9193
0
      fill_extra_space = xtensa_compute_fill_extra_space (the_add_entry);
9194
9195
0
      fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9196
0
      removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9197
0
              -4, fill_extra_space);
9198
0
      if (fa)
9199
0
  adjust_fill_action (fa, removed_diff);
9200
0
      else
9201
0
  text_action_add (&relax_info->action_list,
9202
0
       ta_fill, sec, entry_sec_offset, removed_diff);
9203
0
    }
9204
9205
  /* Zero out the relocation on this literal location.  */
9206
0
  if (irel)
9207
0
    {
9208
0
      if (elf_hash_table (link_info)->dynamic_sections_created)
9209
0
  shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
9210
9211
0
      irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
9212
0
      pin_internal_relocs (sec, internal_relocs);
9213
0
    }
9214
9215
  /* Do not modify "last_loc_is_prev".  */
9216
0
  return true;
9217
0
}
9218
9219
9220
bool
9221
identify_literal_placement (bfd *abfd,
9222
          asection *sec,
9223
          bfd_byte *contents,
9224
          struct bfd_link_info *link_info,
9225
          value_map_hash_table *values,
9226
          bool *last_loc_is_prev_p,
9227
          Elf_Internal_Rela *irel,
9228
          int remaining_src_rels,
9229
          source_reloc *rel,
9230
          property_table_entry *prop_table,
9231
          int ptblsize,
9232
          section_cache_t *target_sec_cache,
9233
          bool is_abs_literal)
9234
0
{
9235
0
  literal_value val;
9236
0
  value_map *val_map;
9237
0
  xtensa_relax_info *relax_info;
9238
0
  bool literal_placed = false;
9239
0
  r_reloc r_rel;
9240
0
  unsigned long value;
9241
0
  bool final_static_link;
9242
0
  bfd_size_type sec_size;
9243
9244
0
  relax_info = get_xtensa_relax_info (sec);
9245
0
  if (!relax_info)
9246
0
    return false;
9247
9248
0
  sec_size = bfd_get_section_limit (abfd, sec);
9249
9250
0
  final_static_link =
9251
0
    (!bfd_link_relocatable (link_info)
9252
0
     && !elf_hash_table (link_info)->dynamic_sections_created);
9253
9254
  /* The placement algorithm first checks to see if the literal is
9255
     already in the value map.  If so and the value map is reachable
9256
     from all uses, then the literal is moved to that location.  If
9257
     not, then we identify the last location where a fresh literal was
9258
     placed.  If the literal can be safely moved there, then we do so.
9259
     If not, then we assume that the literal is not to move and leave
9260
     the literal where it is, marking it as the last literal
9261
     location.  */
9262
9263
  /* Find the literal value.  */
9264
0
  value = 0;
9265
0
  r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9266
0
  if (!irel)
9267
0
    {
9268
0
      BFD_ASSERT (rel->r_rel.target_offset < sec_size);
9269
0
      value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
9270
0
    }
9271
0
  init_literal_value (&val, &r_rel, value, is_abs_literal);
9272
9273
  /* Check if we've seen another literal with the same value that
9274
     is in the same output section.  */
9275
0
  val_map = value_map_get_cached_value (values, &val, final_static_link);
9276
9277
0
  if (val_map
9278
0
      && (r_reloc_get_section (&val_map->loc)->output_section
9279
0
    == sec->output_section)
9280
0
      && relocations_reach (rel, remaining_src_rels, &val_map->loc)
9281
0
      && coalesce_shared_literal (sec, rel, prop_table, ptblsize, val_map))
9282
0
    {
9283
      /* No change to last_loc_is_prev.  */
9284
0
      literal_placed = true;
9285
0
    }
9286
9287
  /* For relocatable links, do not try to move literals.  To do it
9288
     correctly might increase the number of relocations in an input
9289
     section making the default relocatable linking fail.  */
9290
0
  if (!bfd_link_relocatable (link_info) && !literal_placed
9291
0
      && values->has_last_loc && !(*last_loc_is_prev_p))
9292
0
    {
9293
0
      asection *target_sec = r_reloc_get_section (&values->last_loc);
9294
0
      if (target_sec && target_sec->output_section == sec->output_section)
9295
0
  {
9296
    /* Increment the virtual offset.  */
9297
0
    r_reloc try_loc = values->last_loc;
9298
0
    try_loc.virtual_offset += 4;
9299
9300
    /* There is a last loc that was in the same output section.  */
9301
0
    if (relocations_reach (rel, remaining_src_rels, &try_loc)
9302
0
        && move_shared_literal (sec, link_info, rel,
9303
0
              prop_table, ptblsize,
9304
0
              &try_loc, &val, target_sec_cache))
9305
0
      {
9306
0
        values->last_loc.virtual_offset += 4;
9307
0
        literal_placed = true;
9308
0
        if (!val_map)
9309
0
    val_map = add_value_map (values, &val, &try_loc,
9310
0
           final_static_link);
9311
0
        else
9312
0
    val_map->loc = try_loc;
9313
0
      }
9314
0
  }
9315
0
    }
9316
9317
0
  if (!literal_placed)
9318
0
    {
9319
      /* Nothing worked, leave the literal alone but update the last loc.  */
9320
0
      values->has_last_loc = true;
9321
0
      values->last_loc = rel->r_rel;
9322
0
      if (!val_map)
9323
0
  val_map = add_value_map (values, &val, &rel->r_rel, final_static_link);
9324
0
      else
9325
0
  val_map->loc = rel->r_rel;
9326
0
      *last_loc_is_prev_p = true;
9327
0
    }
9328
9329
0
  return true;
9330
0
}
9331
9332
9333
/* Check if the original relocations (presumably on L32R instructions)
9334
   identified by reloc[0..N] can be changed to reference the literal
9335
   identified by r_rel.  If r_rel is out of range for any of the
9336
   original relocations, then we don't want to coalesce the original
9337
   literal with the one at r_rel.  We only check reloc[0..N], where the
9338
   offsets are all the same as for reloc[0] (i.e., they're all
9339
   referencing the same literal) and where N is also bounded by the
9340
   number of remaining entries in the "reloc" array.  The "reloc" array
9341
   is sorted by target offset so we know all the entries for the same
9342
   literal will be contiguous.  */
9343
9344
static bool
9345
relocations_reach (source_reloc *reloc,
9346
       int remaining_relocs,
9347
       const r_reloc *r_rel)
9348
0
{
9349
0
  bfd_vma from_offset, source_address, dest_address;
9350
0
  asection *sec;
9351
0
  int i;
9352
9353
0
  if (!r_reloc_is_defined (r_rel))
9354
0
    return false;
9355
9356
0
  sec = r_reloc_get_section (r_rel);
9357
0
  from_offset = reloc[0].r_rel.target_offset;
9358
9359
0
  for (i = 0; i < remaining_relocs; i++)
9360
0
    {
9361
0
      if (reloc[i].r_rel.target_offset != from_offset)
9362
0
  break;
9363
9364
      /* Ignore relocations that have been removed.  */
9365
0
      if (reloc[i].is_null)
9366
0
  continue;
9367
9368
      /* The original and new output section for these must be the same
9369
   in order to coalesce.  */
9370
0
      if (r_reloc_get_section (&reloc[i].r_rel)->output_section
9371
0
    != sec->output_section)
9372
0
  return false;
9373
9374
      /* Absolute literals in the same output section can always be
9375
   combined.  */
9376
0
      if (reloc[i].is_abs_literal)
9377
0
  continue;
9378
9379
      /* A literal with no PC-relative relocations can be moved anywhere.  */
9380
0
      if (reloc[i].opnd != -1)
9381
0
  {
9382
    /* Otherwise, check to see that it fits.  */
9383
0
    source_address = (reloc[i].source_sec->output_section->vma
9384
0
          + reloc[i].source_sec->output_offset
9385
0
          + reloc[i].r_rel.rela.r_offset);
9386
0
    dest_address = (sec->output_section->vma
9387
0
        + sec->output_offset
9388
0
        + r_rel->target_offset);
9389
9390
0
    if (!pcrel_reloc_fits (reloc[i].opcode, reloc[i].opnd,
9391
0
         source_address, dest_address))
9392
0
      return false;
9393
0
  }
9394
0
    }
9395
9396
0
  return true;
9397
0
}
9398
9399
9400
/* Move a literal to another literal location because it is
9401
   the same as the other literal value.  */
9402
9403
static bool
9404
coalesce_shared_literal (asection *sec,
9405
       source_reloc *rel,
9406
       property_table_entry *prop_table,
9407
       int ptblsize,
9408
       value_map *val_map)
9409
0
{
9410
0
  property_table_entry *entry;
9411
0
  text_action *fa;
9412
0
  property_table_entry *the_add_entry;
9413
0
  int removed_diff;
9414
0
  xtensa_relax_info *relax_info;
9415
9416
0
  relax_info = get_xtensa_relax_info (sec);
9417
0
  if (!relax_info)
9418
0
    return false;
9419
9420
0
  entry = elf_xtensa_find_property_entry
9421
0
    (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9422
0
  if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
9423
0
    return true;
9424
9425
  /* Mark that the literal will be coalesced.  */
9426
0
  add_removed_literal (&relax_info->removed_list, &rel->r_rel, &val_map->loc);
9427
9428
0
  text_action_add (&relax_info->action_list,
9429
0
       ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9430
9431
  /* If the section is 4-byte aligned, do not add fill.  */
9432
0
  if (sec->alignment_power > 2)
9433
0
    {
9434
0
      int fill_extra_space;
9435
0
      bfd_vma entry_sec_offset;
9436
9437
0
      if (entry)
9438
0
  entry_sec_offset = entry->address - sec->vma + entry->size;
9439
0
      else
9440
0
  entry_sec_offset = rel->r_rel.target_offset + 4;
9441
9442
      /* If the literal range is at the end of the section,
9443
   do not add fill.  */
9444
0
      fill_extra_space = 0;
9445
0
      the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9446
0
                  entry_sec_offset);
9447
0
      if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9448
0
  fill_extra_space = the_add_entry->size;
9449
9450
0
      fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9451
0
      removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9452
0
              -4, fill_extra_space);
9453
0
      if (fa)
9454
0
  adjust_fill_action (fa, removed_diff);
9455
0
      else
9456
0
  text_action_add (&relax_info->action_list,
9457
0
       ta_fill, sec, entry_sec_offset, removed_diff);
9458
0
    }
9459
9460
0
  return true;
9461
0
}
9462
9463
9464
/* Move a literal to another location.  This may actually increase the
9465
   total amount of space used because of alignments so we need to do
9466
   this carefully.  Also, it may make a branch go out of range.  */
9467
9468
static bool
9469
move_shared_literal (asection *sec,
9470
         struct bfd_link_info *link_info,
9471
         source_reloc *rel,
9472
         property_table_entry *prop_table,
9473
         int ptblsize,
9474
         const r_reloc *target_loc,
9475
         const literal_value *lit_value,
9476
         section_cache_t *target_sec_cache)
9477
0
{
9478
0
  property_table_entry *the_add_entry, *src_entry, *target_entry = NULL;
9479
0
  text_action *fa, *target_fa;
9480
0
  int removed_diff;
9481
0
  xtensa_relax_info *relax_info, *target_relax_info;
9482
0
  asection *target_sec;
9483
0
  ebb_t *ebb;
9484
0
  ebb_constraint ebb_table;
9485
0
  bool relocs_fit;
9486
9487
  /* If this routine always returns FALSE, the literals that cannot be
9488
     coalesced will not be moved.  */
9489
0
  if (elf32xtensa_no_literal_movement)
9490
0
    return false;
9491
9492
0
  relax_info = get_xtensa_relax_info (sec);
9493
0
  if (!relax_info)
9494
0
    return false;
9495
9496
0
  target_sec = r_reloc_get_section (target_loc);
9497
0
  target_relax_info = get_xtensa_relax_info (target_sec);
9498
9499
  /* Literals to undefined sections may not be moved because they
9500
     must report an error.  */
9501
0
  if (bfd_is_und_section (target_sec))
9502
0
    return false;
9503
9504
0
  src_entry = elf_xtensa_find_property_entry
9505
0
    (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9506
9507
0
  if (!section_cache_section (target_sec_cache, target_sec, link_info))
9508
0
    return false;
9509
9510
0
  target_entry = elf_xtensa_find_property_entry
9511
0
    (target_sec_cache->ptbl, target_sec_cache->pte_count,
9512
0
     target_sec->vma + target_loc->target_offset);
9513
9514
0
  if (!target_entry)
9515
0
    return false;
9516
9517
  /* Make sure that we have not broken any branches.  */
9518
0
  relocs_fit = false;
9519
9520
0
  init_ebb_constraint (&ebb_table);
9521
0
  ebb = &ebb_table.ebb;
9522
0
  init_ebb (ebb, target_sec_cache->sec, target_sec_cache->contents,
9523
0
      target_sec_cache->content_length,
9524
0
      target_sec_cache->ptbl, target_sec_cache->pte_count,
9525
0
      target_sec_cache->relocs, target_sec_cache->reloc_count);
9526
9527
  /* Propose to add 4 bytes + worst-case alignment size increase to
9528
     destination.  */
9529
0
  ebb_propose_action (&ebb_table, EBB_NO_ALIGN, 0,
9530
0
          ta_fill, target_loc->target_offset,
9531
0
          -4 - (1 << target_sec->alignment_power), true);
9532
9533
  /* Check all of the PC-relative relocations to make sure they still fit.  */
9534
0
  relocs_fit = check_section_ebb_pcrels_fit (target_sec->owner, target_sec,
9535
0
               target_sec_cache->contents,
9536
0
               target_sec_cache->relocs, NULL,
9537
0
               &ebb_table, NULL);
9538
9539
0
  if (!relocs_fit)
9540
0
    return false;
9541
9542
0
  text_action_add_literal (&target_relax_info->action_list,
9543
0
         ta_add_literal, target_loc, lit_value, -4);
9544
9545
0
  if (target_sec->alignment_power > 2 && target_entry != src_entry)
9546
0
    {
9547
      /* May need to add or remove some fill to maintain alignment.  */
9548
0
      int fill_extra_space;
9549
0
      bfd_vma entry_sec_offset;
9550
9551
0
      entry_sec_offset =
9552
0
  target_entry->address - target_sec->vma + target_entry->size;
9553
9554
      /* If the literal range is at the end of the section,
9555
   do not add fill.  */
9556
0
      fill_extra_space = 0;
9557
0
      the_add_entry =
9558
0
  elf_xtensa_find_property_entry (target_sec_cache->ptbl,
9559
0
          target_sec_cache->pte_count,
9560
0
          entry_sec_offset);
9561
0
      if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9562
0
  fill_extra_space = the_add_entry->size;
9563
9564
0
      target_fa = find_fill_action (&target_relax_info->action_list,
9565
0
            target_sec, entry_sec_offset);
9566
0
      removed_diff = compute_removed_action_diff (target_fa, target_sec,
9567
0
              entry_sec_offset, 4,
9568
0
              fill_extra_space);
9569
0
      if (target_fa)
9570
0
  adjust_fill_action (target_fa, removed_diff);
9571
0
      else
9572
0
  text_action_add (&target_relax_info->action_list,
9573
0
       ta_fill, target_sec, entry_sec_offset, removed_diff);
9574
0
    }
9575
9576
  /* Mark that the literal will be moved to the new location.  */
9577
0
  add_removed_literal (&relax_info->removed_list, &rel->r_rel, target_loc);
9578
9579
  /* Remove the literal.  */
9580
0
  text_action_add (&relax_info->action_list,
9581
0
       ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9582
9583
  /* If the section is 4-byte aligned, do not add fill.  */
9584
0
  if (sec->alignment_power > 2 && target_entry != src_entry)
9585
0
    {
9586
0
      int fill_extra_space;
9587
0
      bfd_vma entry_sec_offset;
9588
9589
0
      if (src_entry)
9590
0
  entry_sec_offset = src_entry->address - sec->vma + src_entry->size;
9591
0
      else
9592
0
  entry_sec_offset = rel->r_rel.target_offset+4;
9593
9594
      /* If the literal range is at the end of the section,
9595
   do not add fill.  */
9596
0
      fill_extra_space = 0;
9597
0
      the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9598
0
                  entry_sec_offset);
9599
0
      if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9600
0
  fill_extra_space = the_add_entry->size;
9601
9602
0
      fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9603
0
      removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9604
0
              -4, fill_extra_space);
9605
0
      if (fa)
9606
0
  adjust_fill_action (fa, removed_diff);
9607
0
      else
9608
0
  text_action_add (&relax_info->action_list,
9609
0
       ta_fill, sec, entry_sec_offset, removed_diff);
9610
0
    }
9611
9612
0
  return true;
9613
0
}
9614
9615

9616
/* Second relaxation pass.  */
9617
9618
static int
9619
action_remove_bytes_fn (splay_tree_node node, void *p)
9620
0
{
9621
0
  bfd_size_type *final_size = p;
9622
0
  text_action *action = (text_action *)node->value;
9623
9624
0
  *final_size -= action->removed_bytes;
9625
0
  return 0;
9626
0
}
9627
9628
/* Modify all of the relocations to point to the right spot, and if this
9629
   is a relaxable section, delete the unwanted literals and fix the
9630
   section size.  */
9631
9632
bool
9633
relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
9634
0
{
9635
0
  Elf_Internal_Rela *internal_relocs;
9636
0
  xtensa_relax_info *relax_info;
9637
0
  bfd_byte *contents;
9638
0
  bool ok = true;
9639
0
  unsigned i;
9640
0
  bool rv = false;
9641
0
  bool virtual_action;
9642
0
  bfd_size_type sec_size;
9643
9644
0
  sec_size = bfd_get_section_limit (abfd, sec);
9645
0
  relax_info = get_xtensa_relax_info (sec);
9646
0
  BFD_ASSERT (relax_info);
9647
9648
  /* First translate any of the fixes that have been added already.  */
9649
0
  translate_section_fixes (sec);
9650
9651
  /* Handle property sections (e.g., literal tables) specially.  */
9652
0
  if (xtensa_is_property_section (sec))
9653
0
    {
9654
0
      BFD_ASSERT (!relax_info->is_relaxable_literal_section);
9655
0
      return relax_property_section (abfd, sec, link_info);
9656
0
    }
9657
9658
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
9659
0
                link_info->keep_memory);
9660
0
  if (!internal_relocs && !action_list_count (&relax_info->action_list))
9661
0
    return true;
9662
9663
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
9664
0
  if (contents == NULL && sec_size != 0)
9665
0
    {
9666
0
      ok = false;
9667
0
      goto error_return;
9668
0
    }
9669
9670
0
  if (internal_relocs)
9671
0
    {
9672
0
      for (i = 0; i < sec->reloc_count; i++)
9673
0
  {
9674
0
    Elf_Internal_Rela *irel;
9675
0
    xtensa_relax_info *target_relax_info;
9676
0
    bfd_vma source_offset, old_source_offset;
9677
0
    r_reloc r_rel;
9678
0
    unsigned r_type;
9679
0
    asection *target_sec;
9680
9681
    /* Locally change the source address.
9682
       Translate the target to the new target address.
9683
       If it points to this section and has been removed,
9684
       NULLify it.
9685
       Write it back.  */
9686
9687
0
    irel = &internal_relocs[i];
9688
0
    source_offset = irel->r_offset;
9689
0
    old_source_offset = source_offset;
9690
9691
0
    r_type = ELF32_R_TYPE (irel->r_info);
9692
0
    r_reloc_init (&r_rel, abfd, irel, contents,
9693
0
      bfd_get_section_limit (abfd, sec));
9694
9695
    /* If this section could have changed then we may need to
9696
       change the relocation's offset.  */
9697
9698
0
    if (relax_info->is_relaxable_literal_section
9699
0
        || relax_info->is_relaxable_asm_section)
9700
0
      {
9701
0
        pin_internal_relocs (sec, internal_relocs);
9702
9703
0
        if (r_type != R_XTENSA_NONE
9704
0
      && find_removed_literal (&relax_info->removed_list,
9705
0
             irel->r_offset))
9706
0
    {
9707
      /* Remove this relocation.  */
9708
0
      if (elf_hash_table (link_info)->dynamic_sections_created)
9709
0
        shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
9710
0
      irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
9711
0
      irel->r_offset = offset_with_removed_text_map
9712
0
        (&relax_info->action_list, irel->r_offset);
9713
0
      continue;
9714
0
    }
9715
9716
0
        if (r_type == R_XTENSA_ASM_SIMPLIFY)
9717
0
    {
9718
0
      text_action *action =
9719
0
        find_insn_action (&relax_info->action_list,
9720
0
              irel->r_offset);
9721
0
      if (action && (action->action == ta_convert_longcall
9722
0
         || action->action == ta_remove_longcall))
9723
0
        {
9724
0
          bfd_reloc_status_type retval;
9725
0
          char *error_message = NULL;
9726
9727
0
          retval = contract_asm_expansion (contents, sec_size,
9728
0
                   irel, &error_message);
9729
0
          if (retval != bfd_reloc_ok)
9730
0
      {
9731
0
        (*link_info->callbacks->reloc_dangerous)
9732
0
          (link_info, error_message, abfd, sec,
9733
0
           irel->r_offset);
9734
0
        goto error_return;
9735
0
      }
9736
          /* Update the action so that the code that moves
9737
       the contents will do the right thing.  */
9738
          /* ta_remove_longcall and ta_remove_insn actions are
9739
       grouped together in the tree as well as
9740
       ta_convert_longcall and ta_none, so that changes below
9741
       can be done w/o removing and reinserting action into
9742
       the tree.  */
9743
9744
0
          if (action->action == ta_remove_longcall)
9745
0
      action->action = ta_remove_insn;
9746
0
          else
9747
0
      action->action = ta_none;
9748
          /* Refresh the info in the r_rel.  */
9749
0
          r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9750
0
          r_type = ELF32_R_TYPE (irel->r_info);
9751
0
        }
9752
0
    }
9753
9754
0
        source_offset = offset_with_removed_text_map
9755
0
    (&relax_info->action_list, irel->r_offset);
9756
0
        irel->r_offset = source_offset;
9757
0
      }
9758
9759
    /* If the target section could have changed then
9760
       we may need to change the relocation's target offset.  */
9761
9762
0
    target_sec = r_reloc_get_section (&r_rel);
9763
9764
    /* For a reference to a discarded section from a DWARF section,
9765
       i.e., where action_discarded is PRETEND, the symbol will
9766
       eventually be modified to refer to the kept section (at least if
9767
       the kept and discarded sections are the same size).  Anticipate
9768
       that here and adjust things accordingly.  */
9769
0
    if (! elf_xtensa_ignore_discarded_relocs (sec)
9770
0
        && elf_xtensa_action_discarded (sec) == PRETEND
9771
0
        && sec->sec_info_type != SEC_INFO_TYPE_STABS
9772
0
        && target_sec != NULL
9773
0
        && discarded_section (target_sec))
9774
0
      {
9775
        /* It would be natural to call _bfd_elf_check_kept_section
9776
     here, but it's not exported from elflink.c.  It's also a
9777
     fairly expensive check.  Adjusting the relocations to the
9778
     discarded section is fairly harmless; it will only adjust
9779
     some addends and difference values.  If it turns out that
9780
     _bfd_elf_check_kept_section fails later, it won't matter,
9781
     so just compare the section names to find the right group
9782
     member.  */
9783
0
        asection *kept = target_sec->kept_section;
9784
0
        if (kept != NULL)
9785
0
    {
9786
0
      if ((kept->flags & SEC_GROUP) != 0)
9787
0
        {
9788
0
          asection *first = elf_next_in_group (kept);
9789
0
          asection *s = first;
9790
9791
0
          kept = NULL;
9792
0
          while (s != NULL)
9793
0
      {
9794
0
        if (strcmp (s->name, target_sec->name) == 0)
9795
0
          {
9796
0
            kept = s;
9797
0
            break;
9798
0
          }
9799
0
        s = elf_next_in_group (s);
9800
0
        if (s == first)
9801
0
          break;
9802
0
      }
9803
0
        }
9804
0
    }
9805
0
        if (kept != NULL
9806
0
      && ((target_sec->rawsize != 0
9807
0
           ? target_sec->rawsize : target_sec->size)
9808
0
          == (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9809
0
    target_sec = kept;
9810
0
      }
9811
9812
0
    target_relax_info = get_xtensa_relax_info (target_sec);
9813
0
    if (target_relax_info
9814
0
        && (target_relax_info->is_relaxable_literal_section
9815
0
      || target_relax_info->is_relaxable_asm_section))
9816
0
      {
9817
0
        r_reloc new_reloc;
9818
0
        target_sec = translate_reloc (&r_rel, &new_reloc, target_sec);
9819
9820
0
        if (r_type == R_XTENSA_PDIFF_ULEB128)
9821
0
    {
9822
0
      bfd_vma diff_value, new_end_offset;
9823
0
      unsigned int len, new_len = 0;
9824
0
      bfd_byte *p, *q, *endp;
9825
9826
0
      if (sec_size <= old_source_offset)
9827
0
        {
9828
0
          (*link_info->callbacks->reloc_dangerous)
9829
0
      (link_info, _("invalid relocation address"),
9830
0
       abfd, sec, old_source_offset);
9831
0
          goto error_return;
9832
0
        }
9833
9834
      /* The value is read back from the section contents, so
9835
         it must not be trusted to be a well formed uleb128
9836
         that ends before the end of the section.  */
9837
0
      p = q = &contents[old_source_offset];
9838
0
      diff_value = _bfd_safe_read_leb128 (abfd, &q, false,
9839
0
                  contents + sec_size);
9840
0
      len = q - p;
9841
0
      if (len == 0 || (q[-1] & 0x80) != 0)
9842
0
        {
9843
0
          (*link_info->callbacks->reloc_dangerous)
9844
0
      (link_info, _("invalid uleb128 difference"),
9845
0
       abfd, sec, old_source_offset);
9846
0
          goto error_return;
9847
0
        }
9848
9849
0
      new_end_offset = offset_with_removed_text_map
9850
0
        (&target_relax_info->action_list,
9851
0
         r_rel.target_offset + diff_value);
9852
0
      if (new_end_offset < new_reloc.target_offset)
9853
0
        {
9854
0
          (*link_info->callbacks->reloc_dangerous)
9855
0
      (link_info, _("uleb128 difference is negative after "
9856
0
              "relaxation"),
9857
0
       abfd, sec, old_source_offset);
9858
0
          goto error_return;
9859
0
        }
9860
0
      diff_value = new_end_offset - new_reloc.target_offset;
9861
9862
      /* Byte length of the minimal uleb128 encoding of
9863
         DIFF_VALUE.  */
9864
0
      for (bfd_vma v = diff_value; new_len++, v >>= 7; )
9865
0
        ;
9866
      /* The assembler reserves one spare byte for these
9867
         differences, but reject growth that still needs more
9868
         bytes than were emitted.  */
9869
0
      if (new_len > len)
9870
0
        {
9871
0
          (*link_info->callbacks->reloc_dangerous)
9872
0
      (link_info, _("uleb128 difference overflows after "
9873
0
              "relaxation"),
9874
0
       abfd, sec, old_source_offset);
9875
0
          goto error_return;
9876
0
        }
9877
9878
      /* Rewrite the difference in place, always using the
9879
         number of bytes the assembler emitted so that the size
9880
         of this section does not change.  A value that now
9881
         needs fewer bytes is padded with uleb128 zeroes.  */
9882
0
      endp = p + len - 1;
9883
0
      memset (p, 0x80, len - 1);
9884
0
      *endp = 0;
9885
0
      p = _bfd_write_unsigned_leb128 (p, endp, diff_value);
9886
0
      BFD_ASSERT (p);
9887
0
      if (--p < endp)
9888
0
        *p |= 0x80;
9889
9890
0
      pin_contents (sec, contents);
9891
0
    }
9892
9893
0
        if (r_type == R_XTENSA_DIFF8
9894
0
      || r_type == R_XTENSA_DIFF16
9895
0
      || r_type == R_XTENSA_DIFF32
9896
0
      || r_type == R_XTENSA_PDIFF8
9897
0
      || r_type == R_XTENSA_PDIFF16
9898
0
      || r_type == R_XTENSA_PDIFF32
9899
0
      || r_type == R_XTENSA_NDIFF8
9900
0
      || r_type == R_XTENSA_NDIFF16
9901
0
      || r_type == R_XTENSA_NDIFF32)
9902
0
    {
9903
0
      bfd_signed_vma diff_value = 0;
9904
0
      bfd_vma new_end_offset, diff_mask = 0;
9905
9906
0
      if (bfd_get_section_limit (abfd, sec) < old_source_offset)
9907
0
        {
9908
0
          (*link_info->callbacks->reloc_dangerous)
9909
0
      (link_info, _("invalid relocation address"),
9910
0
       abfd, sec, old_source_offset);
9911
0
          goto error_return;
9912
0
        }
9913
9914
0
      switch (r_type)
9915
0
        {
9916
0
        case R_XTENSA_DIFF8:
9917
0
          diff_mask = 0x7f;
9918
0
          diff_value =
9919
0
      bfd_get_signed_8 (abfd, &contents[old_source_offset]);
9920
0
          break;
9921
0
        case R_XTENSA_DIFF16:
9922
0
          diff_mask = 0x7fff;
9923
0
          diff_value =
9924
0
      bfd_get_signed_16 (abfd, &contents[old_source_offset]);
9925
0
          break;
9926
0
        case R_XTENSA_DIFF32:
9927
0
          diff_mask = 0x7fffffff;
9928
0
          diff_value =
9929
0
      bfd_get_signed_32 (abfd, &contents[old_source_offset]);
9930
0
          break;
9931
0
        case R_XTENSA_PDIFF8:
9932
0
        case R_XTENSA_NDIFF8:
9933
0
          diff_mask = 0xff;
9934
0
          diff_value =
9935
0
      bfd_get_8 (abfd, &contents[old_source_offset]);
9936
0
          break;
9937
0
        case R_XTENSA_PDIFF16:
9938
0
        case R_XTENSA_NDIFF16:
9939
0
          diff_mask = 0xffff;
9940
0
          diff_value =
9941
0
      bfd_get_16 (abfd, &contents[old_source_offset]);
9942
0
          break;
9943
0
        case R_XTENSA_PDIFF32:
9944
0
        case R_XTENSA_NDIFF32:
9945
0
          diff_mask = 0xffffffff;
9946
0
          diff_value =
9947
0
      bfd_get_32 (abfd, &contents[old_source_offset]);
9948
0
          break;
9949
0
        }
9950
9951
0
      if (r_type >= R_XTENSA_NDIFF8
9952
0
          && r_type <= R_XTENSA_NDIFF32
9953
0
          && diff_value)
9954
0
        diff_value |= ~diff_mask;
9955
9956
0
      new_end_offset = offset_with_removed_text_map
9957
0
        (&target_relax_info->action_list,
9958
0
         r_rel.target_offset + diff_value);
9959
0
      diff_value = new_end_offset - new_reloc.target_offset;
9960
9961
0
      switch (r_type)
9962
0
        {
9963
0
        case R_XTENSA_DIFF8:
9964
0
          bfd_put_signed_8 (abfd, diff_value,
9965
0
         &contents[old_source_offset]);
9966
0
          break;
9967
0
        case R_XTENSA_DIFF16:
9968
0
          bfd_put_signed_16 (abfd, diff_value,
9969
0
          &contents[old_source_offset]);
9970
0
          break;
9971
0
        case R_XTENSA_DIFF32:
9972
0
          bfd_put_signed_32 (abfd, diff_value,
9973
0
          &contents[old_source_offset]);
9974
0
          break;
9975
0
        case R_XTENSA_PDIFF8:
9976
0
        case R_XTENSA_NDIFF8:
9977
0
          bfd_put_8 (abfd, diff_value,
9978
0
         &contents[old_source_offset]);
9979
0
          break;
9980
0
        case R_XTENSA_PDIFF16:
9981
0
        case R_XTENSA_NDIFF16:
9982
0
          bfd_put_16 (abfd, diff_value,
9983
0
          &contents[old_source_offset]);
9984
0
          break;
9985
0
        case R_XTENSA_PDIFF32:
9986
0
        case R_XTENSA_NDIFF32:
9987
0
          bfd_put_32 (abfd, diff_value,
9988
0
          &contents[old_source_offset]);
9989
0
          break;
9990
0
        }
9991
9992
      /* Check for overflow. Sign bits must be all zeroes or
9993
         all ones.  When sign bits are all ones diff_value
9994
         may not be zero.  */
9995
0
      if (((diff_value & ~diff_mask) != 0
9996
0
           && (diff_value & ~diff_mask) != ~diff_mask)
9997
0
          || (diff_value && (bfd_vma) diff_value == ~diff_mask))
9998
0
        {
9999
0
          (*link_info->callbacks->reloc_dangerous)
10000
0
      (link_info, _("overflow after relaxation"),
10001
0
       abfd, sec, old_source_offset);
10002
0
          goto error_return;
10003
0
        }
10004
10005
0
      pin_contents (sec, contents);
10006
0
    }
10007
10008
        /* If the relocation still references a section in the same
10009
     input file, modify the relocation directly instead of
10010
     adding a "fix" record.  */
10011
0
        if (target_sec->owner == abfd)
10012
0
    {
10013
0
      unsigned r_symndx = ELF32_R_SYM (new_reloc.rela.r_info);
10014
0
      irel->r_info = ELF32_R_INFO (r_symndx, r_type);
10015
0
      irel->r_addend = new_reloc.rela.r_addend;
10016
0
      pin_internal_relocs (sec, internal_relocs);
10017
0
    }
10018
0
        else
10019
0
    {
10020
0
      bfd_vma addend_displacement;
10021
0
      reloc_bfd_fix *fix;
10022
10023
0
      addend_displacement =
10024
0
        new_reloc.target_offset + new_reloc.virtual_offset;
10025
0
      fix = reloc_bfd_fix_init (sec, source_offset, r_type,
10026
0
              target_sec,
10027
0
              addend_displacement, true);
10028
0
      add_fix (sec, fix);
10029
0
    }
10030
0
      }
10031
0
  }
10032
0
    }
10033
10034
0
  if ((relax_info->is_relaxable_literal_section
10035
0
       || relax_info->is_relaxable_asm_section)
10036
0
      && action_list_count (&relax_info->action_list))
10037
0
    {
10038
      /* Walk through the planned actions and build up a table
10039
   of move, copy and fill records.  Use the move, copy and
10040
   fill records to perform the actions once.  */
10041
10042
0
      bfd_size_type final_size, copy_size, orig_insn_size;
10043
0
      bfd_byte *scratch = NULL;
10044
0
      bfd_byte *dup_contents = NULL;
10045
0
      bfd_size_type orig_size = sec->size;
10046
0
      bfd_vma orig_dot = 0;
10047
0
      bfd_vma orig_dot_copied = 0; /* Byte copied already from
10048
              orig dot in physical memory.  */
10049
0
      bfd_vma orig_dot_vo = 0; /* Virtual offset from orig_dot.  */
10050
0
      bfd_vma dup_dot = 0;
10051
10052
0
      text_action *action;
10053
10054
0
      final_size = sec->size;
10055
10056
0
      splay_tree_foreach (relax_info->action_list.tree,
10057
0
        action_remove_bytes_fn, &final_size);
10058
0
      scratch = (bfd_byte *) bfd_zmalloc (final_size);
10059
0
      dup_contents = (bfd_byte *) bfd_zmalloc (final_size);
10060
10061
      /* The dot is the current fill location.  */
10062
#if DEBUG
10063
      print_action_list (stderr, &relax_info->action_list);
10064
#endif
10065
10066
0
      for (action = action_first (&relax_info->action_list); action;
10067
0
     action = action_next (&relax_info->action_list, action))
10068
0
  {
10069
0
    virtual_action = false;
10070
0
    if (action->offset > orig_dot)
10071
0
      {
10072
0
        orig_dot += orig_dot_copied;
10073
0
        orig_dot_copied = 0;
10074
0
        orig_dot_vo = 0;
10075
        /* Out of the virtual world.  */
10076
0
      }
10077
10078
0
    if (action->offset > orig_dot)
10079
0
      {
10080
0
        copy_size = action->offset - orig_dot;
10081
0
        memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
10082
0
        orig_dot += copy_size;
10083
0
        dup_dot += copy_size;
10084
0
        BFD_ASSERT (action->offset == orig_dot);
10085
0
      }
10086
0
    else if (action->offset < orig_dot)
10087
0
      {
10088
0
        if (action->action == ta_fill
10089
0
      && action->offset - action->removed_bytes == orig_dot)
10090
0
    {
10091
      /* This is OK because the fill only effects the dup_dot.  */
10092
0
    }
10093
0
        else if (action->action == ta_add_literal)
10094
0
    {
10095
      /* TBD.  Might need to handle this.  */
10096
0
    }
10097
0
      }
10098
0
    if (action->offset == orig_dot)
10099
0
      {
10100
0
        if (action->virtual_offset > orig_dot_vo)
10101
0
    {
10102
0
      if (orig_dot_vo == 0)
10103
0
        {
10104
          /* Need to copy virtual_offset bytes.  Probably four.  */
10105
0
          copy_size = action->virtual_offset - orig_dot_vo;
10106
0
          memmove (&dup_contents[dup_dot],
10107
0
             &contents[orig_dot], copy_size);
10108
0
          orig_dot_copied = copy_size;
10109
0
          dup_dot += copy_size;
10110
0
        }
10111
0
      virtual_action = true;
10112
0
    }
10113
0
        else
10114
0
    BFD_ASSERT (action->virtual_offset <= orig_dot_vo);
10115
0
      }
10116
0
    switch (action->action)
10117
0
      {
10118
0
      case ta_remove_literal:
10119
0
      case ta_remove_insn:
10120
0
        BFD_ASSERT (action->removed_bytes >= 0);
10121
0
        orig_dot += action->removed_bytes;
10122
0
        break;
10123
10124
0
      case ta_narrow_insn:
10125
0
        orig_insn_size = 3;
10126
0
        copy_size = 2;
10127
0
        memmove (scratch, &contents[orig_dot], orig_insn_size);
10128
0
        BFD_ASSERT (action->removed_bytes == 1);
10129
0
        rv = narrow_instruction (scratch, final_size, 0);
10130
0
        BFD_ASSERT (rv);
10131
0
        memmove (&dup_contents[dup_dot], scratch, copy_size);
10132
0
        orig_dot += orig_insn_size;
10133
0
        dup_dot += copy_size;
10134
0
        break;
10135
10136
0
      case ta_fill:
10137
0
        if (action->removed_bytes >= 0)
10138
0
    orig_dot += action->removed_bytes;
10139
0
        else
10140
0
    {
10141
      /* Already zeroed in dup_contents.  Just bump the
10142
         counters.  */
10143
0
      dup_dot += (-action->removed_bytes);
10144
0
    }
10145
0
        break;
10146
10147
0
      case ta_none:
10148
0
        BFD_ASSERT (action->removed_bytes == 0);
10149
0
        break;
10150
10151
0
      case ta_convert_longcall:
10152
0
      case ta_remove_longcall:
10153
        /* These will be removed or converted before we get here.  */
10154
0
        BFD_ASSERT (0);
10155
0
        break;
10156
10157
0
      case ta_widen_insn:
10158
0
        orig_insn_size = 2;
10159
0
        copy_size = 3;
10160
0
        memmove (scratch, &contents[orig_dot], orig_insn_size);
10161
0
        BFD_ASSERT (action->removed_bytes == -1);
10162
0
        rv = widen_instruction (scratch, final_size, 0);
10163
0
        BFD_ASSERT (rv);
10164
0
        memmove (&dup_contents[dup_dot], scratch, copy_size);
10165
0
        orig_dot += orig_insn_size;
10166
0
        dup_dot += copy_size;
10167
0
        break;
10168
10169
0
      case ta_add_literal:
10170
0
        orig_insn_size = 0;
10171
0
        copy_size = 4;
10172
0
        BFD_ASSERT (action->removed_bytes == -4);
10173
        /* TBD -- place the literal value here and insert
10174
     into the table.  */
10175
0
        memset (&dup_contents[dup_dot], 0, 4);
10176
0
        pin_internal_relocs (sec, internal_relocs);
10177
0
        pin_contents (sec, contents);
10178
10179
0
        if (!move_literal (abfd, link_info, sec, dup_dot, dup_contents,
10180
0
         relax_info, &internal_relocs, &action->value))
10181
0
    goto error_return;
10182
10183
0
        if (virtual_action)
10184
0
    orig_dot_vo += copy_size;
10185
10186
0
        orig_dot += orig_insn_size;
10187
0
        dup_dot += copy_size;
10188
0
        break;
10189
10190
0
      default:
10191
        /* Not implemented yet.  */
10192
0
        BFD_ASSERT (0);
10193
0
        break;
10194
0
      }
10195
10196
0
    BFD_ASSERT (dup_dot <= final_size);
10197
0
    BFD_ASSERT (orig_dot <= orig_size);
10198
0
  }
10199
10200
0
      orig_dot += orig_dot_copied;
10201
0
      orig_dot_copied = 0;
10202
10203
0
      if (orig_dot != orig_size)
10204
0
  {
10205
0
    copy_size = orig_size - orig_dot;
10206
0
    BFD_ASSERT (orig_size > orig_dot);
10207
0
    BFD_ASSERT (dup_dot + copy_size == final_size);
10208
0
    memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
10209
0
    orig_dot += copy_size;
10210
0
    dup_dot += copy_size;
10211
0
  }
10212
0
      BFD_ASSERT (orig_size == orig_dot);
10213
0
      BFD_ASSERT (final_size == dup_dot);
10214
10215
      /* Move the dup_contents back.  */
10216
0
      if (final_size > orig_size)
10217
0
  {
10218
    /* Contents need to be reallocated.  Swap the dup_contents into
10219
       contents.  */
10220
0
    sec->contents = dup_contents;
10221
0
    free (contents);
10222
0
    contents = dup_contents;
10223
0
    pin_contents (sec, contents);
10224
0
  }
10225
0
      else
10226
0
  {
10227
0
    BFD_ASSERT (final_size <= orig_size);
10228
0
    memset (contents, 0, orig_size);
10229
0
    memcpy (contents, dup_contents, final_size);
10230
0
    free (dup_contents);
10231
0
  }
10232
0
      free (scratch);
10233
0
      pin_contents (sec, contents);
10234
10235
0
      if (sec->rawsize == 0)
10236
0
  sec->rawsize = sec->size;
10237
0
      sec->size = final_size;
10238
0
    }
10239
10240
0
 error_return:
10241
0
  release_internal_relocs (sec, internal_relocs);
10242
0
  release_contents (sec, contents);
10243
0
  return ok;
10244
0
}
10245
10246
10247
static bool
10248
translate_section_fixes (asection *sec)
10249
0
{
10250
0
  xtensa_relax_info *relax_info;
10251
0
  reloc_bfd_fix *r;
10252
10253
0
  relax_info = get_xtensa_relax_info (sec);
10254
0
  if (!relax_info)
10255
0
    return true;
10256
10257
0
  for (r = relax_info->fix_list; r != NULL; r = r->next)
10258
0
    if (!translate_reloc_bfd_fix (r))
10259
0
      return false;
10260
10261
0
  return true;
10262
0
}
10263
10264
10265
/* Translate a fix given the mapping in the relax info for the target
10266
   section.  If it has already been translated, no work is required.  */
10267
10268
static bool
10269
translate_reloc_bfd_fix (reloc_bfd_fix *fix)
10270
0
{
10271
0
  reloc_bfd_fix new_fix;
10272
0
  asection *sec;
10273
0
  xtensa_relax_info *relax_info;
10274
0
  removed_literal *removed;
10275
0
  bfd_vma new_offset, target_offset;
10276
10277
0
  if (fix->translated)
10278
0
    return true;
10279
10280
0
  sec = fix->target_sec;
10281
0
  target_offset = fix->target_offset;
10282
10283
0
  relax_info = get_xtensa_relax_info (sec);
10284
0
  if (!relax_info)
10285
0
    {
10286
0
      fix->translated = true;
10287
0
      return true;
10288
0
    }
10289
10290
0
  new_fix = *fix;
10291
10292
  /* The fix does not need to be translated if the section cannot change.  */
10293
0
  if (!relax_info->is_relaxable_literal_section
10294
0
      && !relax_info->is_relaxable_asm_section)
10295
0
    {
10296
0
      fix->translated = true;
10297
0
      return true;
10298
0
    }
10299
10300
  /* If the literal has been moved and this relocation was on an
10301
     opcode, then the relocation should move to the new literal
10302
     location.  Otherwise, the relocation should move within the
10303
     section.  */
10304
10305
0
  removed = NULL;
10306
0
  if (is_operand_relocation (fix->src_type))
10307
0
    {
10308
      /* Check if the original relocation is against a literal being
10309
   removed.  */
10310
0
      removed = find_removed_literal (&relax_info->removed_list,
10311
0
              target_offset);
10312
0
    }
10313
10314
0
  if (removed)
10315
0
    {
10316
0
      asection *new_sec;
10317
10318
      /* The fact that there is still a relocation to this literal indicates
10319
   that the literal is being coalesced, not simply removed.  */
10320
0
      BFD_ASSERT (removed->to.abfd != NULL);
10321
10322
      /* This was moved to some other address (possibly another section).  */
10323
0
      new_sec = r_reloc_get_section (&removed->to);
10324
0
      if (new_sec != sec)
10325
0
  {
10326
0
    sec = new_sec;
10327
0
    relax_info = get_xtensa_relax_info (sec);
10328
0
    if (!relax_info ||
10329
0
        (!relax_info->is_relaxable_literal_section
10330
0
         && !relax_info->is_relaxable_asm_section))
10331
0
      {
10332
0
        target_offset = removed->to.target_offset;
10333
0
        new_fix.target_sec = new_sec;
10334
0
        new_fix.target_offset = target_offset;
10335
0
        new_fix.translated = true;
10336
0
        *fix = new_fix;
10337
0
        return true;
10338
0
      }
10339
0
  }
10340
0
      target_offset = removed->to.target_offset;
10341
0
      new_fix.target_sec = new_sec;
10342
0
    }
10343
10344
  /* The target address may have been moved within its section.  */
10345
0
  new_offset = offset_with_removed_text (&relax_info->action_list,
10346
0
           target_offset);
10347
10348
0
  new_fix.target_offset = new_offset;
10349
0
  new_fix.target_offset = new_offset;
10350
0
  new_fix.translated = true;
10351
0
  *fix = new_fix;
10352
0
  return true;
10353
0
}
10354
10355
10356
/* Fix up a relocation to take account of removed literals.  */
10357
10358
static asection *
10359
translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
10360
0
{
10361
0
  xtensa_relax_info *relax_info;
10362
0
  removed_literal *removed;
10363
0
  bfd_vma target_offset, base_offset;
10364
10365
0
  *new_rel = *orig_rel;
10366
10367
0
  if (!r_reloc_is_defined (orig_rel))
10368
0
    return sec ;
10369
10370
0
  relax_info = get_xtensa_relax_info (sec);
10371
0
  BFD_ASSERT (relax_info && (relax_info->is_relaxable_literal_section
10372
0
           || relax_info->is_relaxable_asm_section));
10373
10374
0
  target_offset = orig_rel->target_offset;
10375
10376
0
  removed = NULL;
10377
0
  if (is_operand_relocation (ELF32_R_TYPE (orig_rel->rela.r_info)))
10378
0
    {
10379
      /* Check if the original relocation is against a literal being
10380
   removed.  */
10381
0
      removed = find_removed_literal (&relax_info->removed_list,
10382
0
              target_offset);
10383
0
    }
10384
0
  if (removed && removed->to.abfd)
10385
0
    {
10386
0
      asection *new_sec;
10387
10388
      /* The fact that there is still a relocation to this literal indicates
10389
   that the literal is being coalesced, not simply removed.  */
10390
0
      BFD_ASSERT (removed->to.abfd != NULL);
10391
10392
      /* This was moved to some other address
10393
   (possibly in another section).  */
10394
0
      *new_rel = removed->to;
10395
0
      new_sec = r_reloc_get_section (new_rel);
10396
0
      if (new_sec != sec)
10397
0
  {
10398
0
    sec = new_sec;
10399
0
    relax_info = get_xtensa_relax_info (sec);
10400
0
    if (!relax_info
10401
0
        || (!relax_info->is_relaxable_literal_section
10402
0
      && !relax_info->is_relaxable_asm_section))
10403
0
      return sec;
10404
0
  }
10405
0
      target_offset = new_rel->target_offset;
10406
0
    }
10407
10408
  /* Find the base offset of the reloc symbol, excluding any addend from the
10409
     reloc or from the section contents (for a partial_inplace reloc).  Then
10410
     find the adjusted values of the offsets due to relaxation.  The base
10411
     offset is needed to determine the change to the reloc's addend; the reloc
10412
     addend should not be adjusted due to relaxations located before the base
10413
     offset.  */
10414
10415
0
  base_offset = r_reloc_get_target_offset (new_rel) - new_rel->rela.r_addend;
10416
0
  if (base_offset <= target_offset)
10417
0
    {
10418
0
      int base_removed = removed_by_actions_map (&relax_info->action_list,
10419
0
             base_offset, false);
10420
0
      int addend_removed = removed_by_actions_map (&relax_info->action_list,
10421
0
               target_offset, false) -
10422
0
  base_removed;
10423
10424
0
      new_rel->target_offset = target_offset - base_removed - addend_removed;
10425
0
      new_rel->rela.r_addend -= addend_removed;
10426
0
    }
10427
0
  else
10428
0
    {
10429
      /* Handle a negative addend.  The base offset comes first.  */
10430
0
      int tgt_removed = removed_by_actions_map (&relax_info->action_list,
10431
0
            target_offset, false);
10432
0
      int addend_removed = removed_by_actions_map (&relax_info->action_list,
10433
0
               base_offset, false) -
10434
0
  tgt_removed;
10435
10436
0
      new_rel->target_offset = target_offset - tgt_removed;
10437
0
      new_rel->rela.r_addend += addend_removed;
10438
0
    }
10439
10440
0
  return sec;
10441
0
}
10442
10443
10444
/* For dynamic links, there may be a dynamic relocation for each
10445
   literal.  The number of dynamic relocations must be computed in
10446
   size_dynamic_sections, which occurs before relaxation.  When a
10447
   literal is removed, this function checks if there is a corresponding
10448
   dynamic relocation and shrinks the size of the appropriate dynamic
10449
   relocation section accordingly.  At this point, the contents of the
10450
   dynamic relocation sections have not yet been filled in, so there's
10451
   nothing else that needs to be done.  */
10452
10453
static void
10454
shrink_dynamic_reloc_sections (struct bfd_link_info *info,
10455
             bfd *abfd,
10456
             asection *input_section,
10457
             Elf_Internal_Rela *rel)
10458
0
{
10459
0
  struct elf_xtensa_link_hash_table *htab;
10460
0
  Elf_Internal_Shdr *symtab_hdr;
10461
0
  struct elf_link_hash_entry **sym_hashes;
10462
0
  unsigned long r_symndx;
10463
0
  int r_type;
10464
0
  struct elf_link_hash_entry *h;
10465
0
  bool dynamic_symbol;
10466
10467
0
  htab = elf_xtensa_hash_table (info);
10468
0
  if (htab == NULL)
10469
0
    return;
10470
10471
0
  symtab_hdr = &elf_symtab_hdr (abfd);
10472
0
  sym_hashes = elf_sym_hashes (abfd);
10473
10474
0
  r_type = ELF32_R_TYPE (rel->r_info);
10475
0
  r_symndx = ELF32_R_SYM (rel->r_info);
10476
10477
0
  if (r_symndx < symtab_hdr->sh_info)
10478
0
    h = NULL;
10479
0
  else
10480
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
10481
10482
0
  dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
10483
10484
0
  if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_32_ABS
10485
0
       || r_type == R_XTENSA_PLT)
10486
0
      && (input_section->flags & SEC_ALLOC) != 0
10487
0
      && (dynamic_symbol
10488
0
    || (bfd_link_pic (info)
10489
0
        && (!h || h->root.type != bfd_link_hash_undefweak))))
10490
0
    {
10491
0
      asection *srel;
10492
0
      bool is_plt = false;
10493
10494
0
      if (dynamic_symbol && r_type == R_XTENSA_PLT)
10495
0
  {
10496
0
    srel = htab->elf.srelplt;
10497
0
    is_plt = true;
10498
0
  }
10499
0
      else
10500
0
  srel = htab->elf.srelgot;
10501
10502
      /* Reduce size of the .rela.* section by one reloc.  */
10503
0
      BFD_ASSERT (srel != NULL);
10504
0
      BFD_ASSERT (srel->size >= sizeof (Elf32_External_Rela));
10505
0
      srel->size -= sizeof (Elf32_External_Rela);
10506
10507
0
      if (is_plt)
10508
0
  {
10509
0
    asection *splt, *sgotplt, *srelgot;
10510
0
    int reloc_index, chunk;
10511
10512
    /* Find the PLT reloc index of the entry being removed.  This
10513
       is computed from the size of ".rela.plt".  It is needed to
10514
       figure out which PLT chunk to resize.  Usually "last index
10515
       = size - 1" since the index starts at zero, but in this
10516
       context, the size has just been decremented so there's no
10517
       need to subtract one.  */
10518
0
    reloc_index = srel->size / sizeof (Elf32_External_Rela);
10519
10520
0
    chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
10521
0
    splt = elf_xtensa_get_plt_section (info, chunk);
10522
0
    sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
10523
0
    BFD_ASSERT (splt != NULL && sgotplt != NULL);
10524
10525
    /* Check if an entire PLT chunk has just been eliminated.  */
10526
0
    if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
10527
0
      {
10528
        /* The two magic GOT entries for that chunk can go away.  */
10529
0
        srelgot = htab->elf.srelgot;
10530
0
        BFD_ASSERT (srelgot != NULL);
10531
0
        srelgot->reloc_count -= 2;
10532
0
        srelgot->size -= 2 * sizeof (Elf32_External_Rela);
10533
0
        sgotplt->size -= 8;
10534
10535
        /* There should be only one entry left (and it will be
10536
     removed below).  */
10537
0
        BFD_ASSERT (sgotplt->size == 4);
10538
0
        BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
10539
0
      }
10540
10541
0
    BFD_ASSERT (sgotplt->size >= 4);
10542
0
    BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
10543
10544
0
    sgotplt->size -= 4;
10545
0
    splt->size -= PLT_ENTRY_SIZE;
10546
0
  }
10547
0
    }
10548
0
}
10549
10550
10551
/* Take an r_rel and move it to another section.  This usually
10552
   requires extending the interal_relocation array and pinning it.  If
10553
   the original r_rel is from the same BFD, we can complete this here.
10554
   Otherwise, we add a fix record to let the final link fix the
10555
   appropriate address.  Contents and internal relocations for the
10556
   section must be pinned after calling this routine.  */
10557
10558
static bool
10559
move_literal (bfd *abfd,
10560
        struct bfd_link_info *link_info,
10561
        asection *sec,
10562
        bfd_vma offset,
10563
        bfd_byte *contents,
10564
        xtensa_relax_info *relax_info,
10565
        Elf_Internal_Rela **internal_relocs_p,
10566
        const literal_value *lit)
10567
0
{
10568
0
  Elf_Internal_Rela *new_relocs = NULL;
10569
0
  size_t new_relocs_count = 0;
10570
0
  Elf_Internal_Rela this_rela;
10571
0
  const r_reloc *r_rel;
10572
10573
0
  r_rel = &lit->r_rel;
10574
0
  BFD_ASSERT (elf_section_data (sec)->relocs == *internal_relocs_p);
10575
10576
0
  if (r_reloc_is_const (r_rel))
10577
0
    bfd_put_32 (abfd, lit->value, contents + offset);
10578
0
  else
10579
0
    {
10580
0
      int r_type;
10581
0
      unsigned i;
10582
0
      reloc_bfd_fix *fix;
10583
0
      unsigned insert_at;
10584
10585
0
      r_type = ELF32_R_TYPE (r_rel->rela.r_info);
10586
10587
      /* This is the difficult case.  We have to create a fix up.  */
10588
0
      this_rela.r_offset = offset;
10589
0
      this_rela.r_info = ELF32_R_INFO (0, r_type);
10590
0
      this_rela.r_addend =
10591
0
  r_rel->target_offset - r_reloc_get_target_offset (r_rel);
10592
0
      bfd_put_32 (abfd, lit->value, contents + offset);
10593
10594
      /* Currently, we cannot move relocations during a relocatable link.  */
10595
0
      BFD_ASSERT (!bfd_link_relocatable (link_info));
10596
0
      fix = reloc_bfd_fix_init (sec, offset, r_type,
10597
0
        r_reloc_get_section (r_rel),
10598
0
        r_rel->target_offset + r_rel->virtual_offset,
10599
0
        false);
10600
      /* We also need to mark that relocations are needed here.  */
10601
0
      sec->flags |= SEC_RELOC;
10602
10603
0
      translate_reloc_bfd_fix (fix);
10604
      /* This fix has not yet been translated.  */
10605
0
      add_fix (sec, fix);
10606
10607
      /* Add the relocation.  If we have already allocated our own
10608
   space for the relocations and we have room for more, then use
10609
   it.  Otherwise, allocate new space and move the literals.  */
10610
0
      insert_at = sec->reloc_count;
10611
0
      for (i = 0; i < sec->reloc_count; ++i)
10612
0
  {
10613
0
    if (this_rela.r_offset < (*internal_relocs_p)[i].r_offset)
10614
0
      {
10615
0
        insert_at = i;
10616
0
        break;
10617
0
      }
10618
0
  }
10619
10620
0
      if (*internal_relocs_p != relax_info->allocated_relocs
10621
0
    || sec->reloc_count + 1 > relax_info->allocated_relocs_count)
10622
0
  {
10623
0
    BFD_ASSERT (relax_info->allocated_relocs == NULL
10624
0
          || sec->reloc_count == relax_info->relocs_count);
10625
10626
0
    if (relax_info->allocated_relocs_count == 0)
10627
0
      new_relocs_count = (sec->reloc_count + 2) * 2;
10628
0
    else
10629
0
      new_relocs_count = (relax_info->allocated_relocs_count + 2) * 2;
10630
10631
0
    new_relocs = (Elf_Internal_Rela *)
10632
0
      bfd_zmalloc (sizeof (Elf_Internal_Rela) * (new_relocs_count));
10633
0
    if (!new_relocs)
10634
0
      return false;
10635
10636
    /* We could handle this more quickly by finding the split point.  */
10637
0
    if (insert_at != 0)
10638
0
      memcpy (new_relocs, *internal_relocs_p,
10639
0
        insert_at * sizeof (Elf_Internal_Rela));
10640
10641
0
    new_relocs[insert_at] = this_rela;
10642
10643
0
    if (insert_at != sec->reloc_count)
10644
0
      memcpy (new_relocs + insert_at + 1,
10645
0
        (*internal_relocs_p) + insert_at,
10646
0
        (sec->reloc_count - insert_at)
10647
0
        * sizeof (Elf_Internal_Rela));
10648
10649
0
    if (*internal_relocs_p != relax_info->allocated_relocs)
10650
0
      {
10651
        /* The first time we re-allocate, we can only free the
10652
     old relocs if they were allocated with bfd_malloc.
10653
     This is not true when keep_memory is in effect.  */
10654
0
        if (!link_info->keep_memory)
10655
0
    free (*internal_relocs_p);
10656
0
      }
10657
0
    else
10658
0
      free (*internal_relocs_p);
10659
0
    relax_info->allocated_relocs = new_relocs;
10660
0
    relax_info->allocated_relocs_count = new_relocs_count;
10661
0
    elf_section_data (sec)->relocs = new_relocs;
10662
0
    sec->reloc_count++;
10663
0
    relax_info->relocs_count = sec->reloc_count;
10664
0
    *internal_relocs_p = new_relocs;
10665
0
  }
10666
0
      else
10667
0
  {
10668
0
    if (insert_at != sec->reloc_count)
10669
0
      {
10670
0
        unsigned idx;
10671
0
        for (idx = sec->reloc_count; idx > insert_at; idx--)
10672
0
    (*internal_relocs_p)[idx] = (*internal_relocs_p)[idx-1];
10673
0
      }
10674
0
    (*internal_relocs_p)[insert_at] = this_rela;
10675
0
    sec->reloc_count++;
10676
0
    if (relax_info->allocated_relocs)
10677
0
      relax_info->relocs_count = sec->reloc_count;
10678
0
  }
10679
0
    }
10680
0
  return true;
10681
0
}
10682
10683
10684
/* This is similar to relax_section except that when a target is moved,
10685
   we shift addresses up.  We also need to modify the size.  This
10686
   algorithm does NOT allow for relocations into the middle of the
10687
   property sections.  */
10688
10689
static bool
10690
relax_property_section (bfd *abfd,
10691
      asection *sec,
10692
      struct bfd_link_info *link_info)
10693
0
{
10694
0
  Elf_Internal_Rela *internal_relocs;
10695
0
  bfd_byte *contents;
10696
0
  unsigned i;
10697
0
  bool ok = true;
10698
0
  bool is_full_prop_section;
10699
0
  size_t last_zfill_target_offset = 0;
10700
0
  asection *last_zfill_target_sec = NULL;
10701
0
  bfd_size_type sec_size;
10702
0
  bfd_size_type entry_size;
10703
10704
0
  sec_size = bfd_get_section_limit (abfd, sec);
10705
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
10706
0
                link_info->keep_memory);
10707
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
10708
0
  if (contents == NULL && sec_size != 0)
10709
0
    {
10710
0
      ok = false;
10711
0
      goto error_return;
10712
0
    }
10713
10714
0
  is_full_prop_section = xtensa_is_proptable_section (sec);
10715
0
  if (is_full_prop_section)
10716
0
    entry_size = 12;
10717
0
  else
10718
0
    entry_size = 8;
10719
10720
0
  if (internal_relocs)
10721
0
    {
10722
0
      for (i = 0; i < sec->reloc_count; i++)
10723
0
  {
10724
0
    Elf_Internal_Rela *irel;
10725
0
    xtensa_relax_info *target_relax_info;
10726
0
    unsigned r_type;
10727
0
    asection *target_sec;
10728
0
    literal_value val;
10729
0
    bfd_byte *size_p, *flags_p;
10730
10731
    /* Locally change the source address.
10732
       Translate the target to the new target address.
10733
       If it points to this section and has been removed, MOVE IT.
10734
       Also, don't forget to modify the associated SIZE at
10735
       (offset + 4).  */
10736
10737
0
    irel = &internal_relocs[i];
10738
0
    r_type = ELF32_R_TYPE (irel->r_info);
10739
0
    if (r_type == R_XTENSA_NONE)
10740
0
      continue;
10741
10742
    /* Find the literal value.  */
10743
0
    r_reloc_init (&val.r_rel, abfd, irel, contents, sec_size);
10744
0
    size_p = &contents[irel->r_offset + 4];
10745
0
    flags_p = NULL;
10746
0
    if (is_full_prop_section)
10747
0
      flags_p = &contents[irel->r_offset + 8];
10748
0
    BFD_ASSERT (irel->r_offset + entry_size <= sec_size);
10749
10750
0
    target_sec = r_reloc_get_section (&val.r_rel);
10751
0
    target_relax_info = get_xtensa_relax_info (target_sec);
10752
10753
0
    if (target_relax_info
10754
0
        && (target_relax_info->is_relaxable_literal_section
10755
0
      || target_relax_info->is_relaxable_asm_section ))
10756
0
      {
10757
        /* Translate the relocation's destination.  */
10758
0
        bfd_vma old_offset = val.r_rel.target_offset;
10759
0
        bfd_vma new_offset;
10760
0
        long old_size, new_size;
10761
0
        int removed_by_old_offset =
10762
0
    removed_by_actions_map (&target_relax_info->action_list,
10763
0
          old_offset, false);
10764
0
        new_offset = old_offset - removed_by_old_offset;
10765
10766
        /* Assert that we are not out of bounds.  */
10767
0
        old_size = bfd_get_32 (abfd, size_p);
10768
0
        new_size = old_size;
10769
10770
0
        if (old_size == 0)
10771
0
    {
10772
      /* Only the first zero-sized unreachable entry is
10773
         allowed to expand.  In this case the new offset
10774
         should be the offset before the fill and the new
10775
         size is the expansion size.  For other zero-sized
10776
         entries the resulting size should be zero with an
10777
         offset before or after the fill address depending
10778
         on whether the expanding unreachable entry
10779
         preceeds it.  */
10780
0
      if (last_zfill_target_sec == 0
10781
0
          || last_zfill_target_sec != target_sec
10782
0
          || last_zfill_target_offset != old_offset)
10783
0
        {
10784
0
          bfd_vma new_end_offset = new_offset;
10785
10786
          /* Recompute the new_offset, but this time don't
10787
       include any fill inserted by relaxation.  */
10788
0
          removed_by_old_offset =
10789
0
      removed_by_actions_map (&target_relax_info->action_list,
10790
0
            old_offset, true);
10791
0
          new_offset = old_offset - removed_by_old_offset;
10792
10793
          /* If it is not unreachable and we have not yet
10794
       seen an unreachable at this address, place it
10795
       before the fill address.  */
10796
0
          if (flags_p && (bfd_get_32 (abfd, flags_p)
10797
0
              & XTENSA_PROP_UNREACHABLE) != 0)
10798
0
      {
10799
0
        new_size = new_end_offset - new_offset;
10800
10801
0
        last_zfill_target_sec = target_sec;
10802
0
        last_zfill_target_offset = old_offset;
10803
0
      }
10804
0
        }
10805
0
    }
10806
0
        else
10807
0
    {
10808
0
      int removed_by_old_offset_size =
10809
0
        removed_by_actions_map (&target_relax_info->action_list,
10810
0
              old_offset + old_size, true);
10811
0
      new_size -= removed_by_old_offset_size - removed_by_old_offset;
10812
0
    }
10813
10814
0
        if (new_size != old_size)
10815
0
    {
10816
0
      bfd_put_32 (abfd, new_size, size_p);
10817
0
      pin_contents (sec, contents);
10818
0
    }
10819
10820
0
        if (new_offset != old_offset)
10821
0
    {
10822
0
      bfd_vma diff = new_offset - old_offset;
10823
0
      irel->r_addend += diff;
10824
0
      pin_internal_relocs (sec, internal_relocs);
10825
0
    }
10826
0
      }
10827
0
  }
10828
0
    }
10829
10830
  /* Combine adjacent property table entries.  This is also done in
10831
     finish_dynamic_sections() but at that point it's too late to
10832
     reclaim the space in the output section, so we do this twice.  */
10833
10834
0
  if (internal_relocs && (!bfd_link_relocatable (link_info)
10835
0
        || xtensa_is_littable_section (sec)))
10836
0
    {
10837
0
      Elf_Internal_Rela *last_irel = NULL;
10838
0
      Elf_Internal_Rela *irel, *next_rel, *rel_end;
10839
0
      int removed_bytes = 0;
10840
0
      bfd_vma offset;
10841
0
      flagword predef_flags;
10842
10843
0
      predef_flags = xtensa_get_property_predef_flags (sec);
10844
10845
      /* Walk over memory and relocations at the same time.
10846
   This REQUIRES that the internal_relocs be sorted by offset.  */
10847
0
      qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
10848
0
       internal_reloc_compare);
10849
10850
0
      pin_internal_relocs (sec, internal_relocs);
10851
0
      pin_contents (sec, contents);
10852
10853
0
      next_rel = internal_relocs;
10854
0
      rel_end = internal_relocs + sec->reloc_count;
10855
10856
0
      BFD_ASSERT (sec->size % entry_size == 0);
10857
10858
0
      for (offset = 0; offset < sec->size; offset += entry_size)
10859
0
  {
10860
0
    Elf_Internal_Rela *offset_rel, *extra_rel;
10861
0
    bfd_vma bytes_to_remove, size, actual_offset;
10862
0
    bool remove_this_rel;
10863
0
    flagword flags;
10864
10865
    /* Find the first relocation for the entry at the current offset.
10866
       Adjust the offsets of any extra relocations for the previous
10867
       entry.  */
10868
0
    offset_rel = NULL;
10869
0
    if (next_rel)
10870
0
      {
10871
0
        for (irel = next_rel; irel < rel_end; irel++)
10872
0
    {
10873
0
      if ((irel->r_offset == offset
10874
0
           && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10875
0
          || irel->r_offset > offset)
10876
0
        {
10877
0
          offset_rel = irel;
10878
0
          break;
10879
0
        }
10880
0
      irel->r_offset -= removed_bytes;
10881
0
    }
10882
0
      }
10883
10884
    /* Find the next relocation (if there are any left).  */
10885
0
    extra_rel = NULL;
10886
0
    if (offset_rel)
10887
0
      {
10888
0
        for (irel = offset_rel + 1; irel < rel_end; irel++)
10889
0
    {
10890
0
      if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10891
0
        {
10892
0
          extra_rel = irel;
10893
0
          break;
10894
0
        }
10895
0
    }
10896
0
      }
10897
10898
    /* Check if there are relocations on the current entry.  There
10899
       should usually be a relocation on the offset field.  If there
10900
       are relocations on the size or flags, then we can't optimize
10901
       this entry.  Also, find the next relocation to examine on the
10902
       next iteration.  */
10903
0
    if (offset_rel)
10904
0
      {
10905
0
        if (offset_rel->r_offset >= offset + entry_size)
10906
0
    {
10907
0
      next_rel = offset_rel;
10908
      /* There are no relocations on the current entry, but we
10909
         might still be able to remove it if the size is zero.  */
10910
0
      offset_rel = NULL;
10911
0
    }
10912
0
        else if (offset_rel->r_offset > offset
10913
0
           || (extra_rel
10914
0
         && extra_rel->r_offset < offset + entry_size))
10915
0
    {
10916
      /* There is a relocation on the size or flags, so we can't
10917
         do anything with this entry.  Continue with the next.  */
10918
0
      next_rel = offset_rel;
10919
0
      continue;
10920
0
    }
10921
0
        else
10922
0
    {
10923
0
      BFD_ASSERT (offset_rel->r_offset == offset);
10924
0
      offset_rel->r_offset -= removed_bytes;
10925
0
      next_rel = offset_rel + 1;
10926
0
    }
10927
0
      }
10928
0
    else
10929
0
      next_rel = NULL;
10930
10931
0
    remove_this_rel = false;
10932
0
    bytes_to_remove = 0;
10933
0
    actual_offset = offset - removed_bytes;
10934
0
    size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
10935
10936
0
    if (is_full_prop_section)
10937
0
      flags = bfd_get_32 (abfd, &contents[actual_offset + 8]);
10938
0
    else
10939
0
      flags = predef_flags;
10940
10941
0
    if (size == 0
10942
0
        && (flags & XTENSA_PROP_ALIGN) == 0
10943
0
        && (flags & XTENSA_PROP_UNREACHABLE) == 0)
10944
0
      {
10945
        /* Always remove entries with zero size and no alignment.  */
10946
0
        bytes_to_remove = entry_size;
10947
0
        if (offset_rel)
10948
0
    remove_this_rel = true;
10949
0
      }
10950
0
    else if (offset_rel
10951
0
       && (ELF32_R_TYPE (offset_rel->r_info) == R_XTENSA_32
10952
0
           || ELF32_R_TYPE (offset_rel->r_info) == R_XTENSA_32_ABS))
10953
0
      {
10954
0
        if (last_irel)
10955
0
    {
10956
0
      flagword old_flags;
10957
0
      bfd_vma old_size =
10958
0
        bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
10959
0
      bfd_vma old_address = xtensa_reloc_addend (abfd, contents,
10960
0
                  last_irel);
10961
0
      bfd_vma new_address = xtensa_reloc_addend (abfd, contents,
10962
0
                  offset_rel);
10963
0
      if (is_full_prop_section)
10964
0
        old_flags = bfd_get_32
10965
0
          (abfd, &contents[last_irel->r_offset + 8]);
10966
0
      else
10967
0
        old_flags = predef_flags;
10968
10969
0
      if ((ELF32_R_SYM (offset_rel->r_info)
10970
0
           == ELF32_R_SYM (last_irel->r_info))
10971
0
          && old_address + old_size == new_address
10972
0
          && old_flags == flags
10973
0
          && (old_flags & XTENSA_PROP_INSN_BRANCH_TARGET) == 0
10974
0
          && (old_flags & XTENSA_PROP_INSN_LOOP_TARGET) == 0)
10975
0
        {
10976
          /* Fix the old size.  */
10977
0
          bfd_put_32 (abfd, old_size + size,
10978
0
          &contents[last_irel->r_offset + 4]);
10979
0
          bytes_to_remove = entry_size;
10980
0
          remove_this_rel = true;
10981
0
        }
10982
0
      else
10983
0
        last_irel = offset_rel;
10984
0
    }
10985
0
        else
10986
0
    last_irel = offset_rel;
10987
0
      }
10988
10989
0
    if (remove_this_rel)
10990
0
      {
10991
0
        offset_rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
10992
0
        offset_rel->r_offset = 0;
10993
0
      }
10994
10995
0
    if (bytes_to_remove != 0)
10996
0
      {
10997
0
        removed_bytes += bytes_to_remove;
10998
0
        if (offset + bytes_to_remove < sec->size)
10999
0
    memmove (&contents[actual_offset],
11000
0
       &contents[actual_offset + bytes_to_remove],
11001
0
       sec->size - offset - bytes_to_remove);
11002
0
      }
11003
0
  }
11004
11005
0
      if (removed_bytes)
11006
0
  {
11007
    /* Fix up any extra relocations on the last entry.  */
11008
0
    for (irel = next_rel; irel < rel_end; irel++)
11009
0
      irel->r_offset -= removed_bytes;
11010
11011
    /* Clear the removed bytes.  */
11012
0
    memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
11013
11014
0
    if (sec->rawsize == 0)
11015
0
      sec->rawsize = sec->size;
11016
0
    sec->size -= removed_bytes;
11017
11018
0
    if (xtensa_is_littable_section (sec))
11019
0
      {
11020
0
        asection *sgotloc = elf_xtensa_hash_table (link_info)->sgotloc;
11021
0
        if (sgotloc)
11022
0
    sgotloc->size -= removed_bytes;
11023
0
      }
11024
0
  }
11025
0
    }
11026
11027
0
 error_return:
11028
0
  release_internal_relocs (sec, internal_relocs);
11029
0
  release_contents (sec, contents);
11030
0
  return ok;
11031
0
}
11032
11033

11034
/* Third relaxation pass.  */
11035
11036
/* Change symbol values to account for removed literals.  */
11037
11038
bool
11039
relax_section_symbols (bfd *abfd, asection *sec)
11040
0
{
11041
0
  xtensa_relax_info *relax_info;
11042
0
  unsigned int sec_shndx;
11043
0
  Elf_Internal_Shdr *symtab_hdr;
11044
0
  Elf_Internal_Sym *isymbuf;
11045
0
  unsigned i, num_syms, num_locals;
11046
11047
0
  relax_info = get_xtensa_relax_info (sec);
11048
0
  BFD_ASSERT (relax_info);
11049
11050
0
  if (!relax_info->is_relaxable_literal_section
11051
0
      && !relax_info->is_relaxable_asm_section)
11052
0
    return true;
11053
11054
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
11055
11056
0
  symtab_hdr = &elf_symtab_hdr (abfd);
11057
0
  isymbuf = retrieve_local_syms (abfd);
11058
11059
0
  num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
11060
0
  num_locals = symtab_hdr->sh_info;
11061
11062
  /* Adjust the local symbols defined in this section.  */
11063
0
  for (i = 0; i < num_locals; i++)
11064
0
    {
11065
0
      Elf_Internal_Sym *isym = &isymbuf[i];
11066
11067
0
      if (isym->st_shndx == sec_shndx)
11068
0
  {
11069
0
    bfd_vma orig_addr = isym->st_value;
11070
0
    int removed = removed_by_actions_map (&relax_info->action_list,
11071
0
            orig_addr, false);
11072
11073
0
    isym->st_value -= removed;
11074
0
    if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
11075
0
      isym->st_size -=
11076
0
        removed_by_actions_map (&relax_info->action_list,
11077
0
              orig_addr + isym->st_size, false) -
11078
0
        removed;
11079
0
  }
11080
0
    }
11081
11082
  /* Now adjust the global symbols defined in this section.  */
11083
0
  for (i = 0; i < (num_syms - num_locals); i++)
11084
0
    {
11085
0
      struct elf_link_hash_entry *sym_hash;
11086
11087
0
      sym_hash = elf_sym_hashes (abfd)[i];
11088
11089
0
      if (sym_hash->root.type == bfd_link_hash_warning)
11090
0
  sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
11091
11092
0
      if ((sym_hash->root.type == bfd_link_hash_defined
11093
0
     || sym_hash->root.type == bfd_link_hash_defweak)
11094
0
    && sym_hash->root.u.def.section == sec)
11095
0
  {
11096
0
    bfd_vma orig_addr = sym_hash->root.u.def.value;
11097
0
    int removed = removed_by_actions_map (&relax_info->action_list,
11098
0
            orig_addr, false);
11099
11100
0
    sym_hash->root.u.def.value -= removed;
11101
11102
0
    if (sym_hash->type == STT_FUNC)
11103
0
      sym_hash->size -=
11104
0
        removed_by_actions_map (&relax_info->action_list,
11105
0
              orig_addr + sym_hash->size, false) -
11106
0
        removed;
11107
0
  }
11108
0
    }
11109
11110
0
  return true;
11111
0
}
11112
11113

11114
/* "Fix" handling functions, called while performing relocations.  */
11115
11116
static bool
11117
do_fix_for_relocatable_link (Elf_Internal_Rela *rel,
11118
           bfd *input_bfd,
11119
           asection *input_section,
11120
           bfd_byte *contents)
11121
0
{
11122
0
  r_reloc r_rel;
11123
0
  asection *sec, *old_sec;
11124
0
  bfd_vma old_offset;
11125
0
  int r_type = ELF32_R_TYPE (rel->r_info);
11126
0
  reloc_bfd_fix *fix;
11127
11128
0
  if (r_type == R_XTENSA_NONE)
11129
0
    return true;
11130
11131
0
  fix = get_bfd_fix (input_section, rel->r_offset, r_type);
11132
0
  if (!fix)
11133
0
    return true;
11134
11135
0
  r_reloc_init (&r_rel, input_bfd, rel, contents,
11136
0
    bfd_get_section_limit (input_bfd, input_section));
11137
0
  old_sec = r_reloc_get_section (&r_rel);
11138
0
  old_offset = r_rel.target_offset;
11139
11140
0
  if (!old_sec || !r_reloc_is_defined (&r_rel))
11141
0
    {
11142
0
      if (r_type != R_XTENSA_ASM_EXPAND)
11143
0
  {
11144
0
    _bfd_error_handler
11145
      /* xgettext:c-format */
11146
0
      (_("%pB(%pA+%#" PRIx64 "): unexpected fix for %s relocation"),
11147
0
       input_bfd, input_section, (uint64_t) rel->r_offset,
11148
0
       elf_howto_table[r_type].name);
11149
0
    return false;
11150
0
  }
11151
      /* Leave it be.  Resolution will happen in a later stage.  */
11152
0
    }
11153
0
  else
11154
0
    {
11155
0
      sec = fix->target_sec;
11156
0
      rel->r_addend += ((sec->output_offset + fix->target_offset)
11157
0
      - (old_sec->output_offset + old_offset));
11158
0
    }
11159
0
  return true;
11160
0
}
11161
11162
11163
static void
11164
do_fix_for_final_link (Elf_Internal_Rela *rel,
11165
           bfd *input_bfd,
11166
           asection *input_section,
11167
           bfd_byte *contents,
11168
           bfd_vma *relocationp)
11169
0
{
11170
0
  asection *sec;
11171
0
  int r_type = ELF32_R_TYPE (rel->r_info);
11172
0
  reloc_bfd_fix *fix;
11173
0
  bfd_vma fixup_diff;
11174
11175
0
  if (r_type == R_XTENSA_NONE)
11176
0
    return;
11177
11178
0
  fix = get_bfd_fix (input_section, rel->r_offset, r_type);
11179
0
  if (!fix)
11180
0
    return;
11181
11182
0
  sec = fix->target_sec;
11183
11184
0
  fixup_diff = rel->r_addend;
11185
0
  if (elf_howto_table[fix->src_type].partial_inplace)
11186
0
    {
11187
0
      bfd_vma inplace_val;
11188
0
      BFD_ASSERT (fix->src_offset
11189
0
      < bfd_get_section_limit (input_bfd, input_section));
11190
0
      inplace_val = bfd_get_32 (input_bfd, &contents[fix->src_offset]);
11191
0
      fixup_diff += inplace_val;
11192
0
    }
11193
11194
0
  *relocationp = (sec->output_section->vma
11195
0
      + sec->output_offset
11196
0
      + fix->target_offset - fixup_diff);
11197
0
}
11198
11199

11200
/* Miscellaneous utility functions....  */
11201
11202
static asection *
11203
elf_xtensa_get_plt_section (struct bfd_link_info *info, int chunk)
11204
0
{
11205
0
  bfd *dynobj;
11206
0
  char plt_name[17];
11207
11208
0
  if (chunk == 0)
11209
0
    return elf_hash_table (info)->splt;
11210
11211
0
  dynobj = elf_hash_table (info)->dynobj;
11212
0
  sprintf (plt_name, ".plt.%u", chunk);
11213
0
  return bfd_get_linker_section (dynobj, plt_name);
11214
0
}
11215
11216
11217
static asection *
11218
elf_xtensa_get_gotplt_section (struct bfd_link_info *info, int chunk)
11219
0
{
11220
0
  bfd *dynobj;
11221
0
  char got_name[21];
11222
11223
0
  if (chunk == 0)
11224
0
    return elf_hash_table (info)->sgotplt;
11225
11226
0
  dynobj = elf_hash_table (info)->dynobj;
11227
0
  sprintf (got_name, ".got.plt.%u", chunk);
11228
0
  return bfd_get_linker_section (dynobj, got_name);
11229
0
}
11230
11231
11232
/* Get the input section for a given symbol index.
11233
   If the symbol is:
11234
   . a section symbol, return the section;
11235
   . a common symbol, return the common section;
11236
   . an undefined symbol, return the undefined section;
11237
   . an indirect symbol, follow the links;
11238
   . an absolute value, return the absolute section.  */
11239
11240
static asection *
11241
get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
11242
0
{
11243
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
11244
0
  asection *target_sec = NULL;
11245
0
  if (r_symndx < symtab_hdr->sh_info)
11246
0
    {
11247
0
      Elf_Internal_Sym *isymbuf;
11248
0
      unsigned int section_index;
11249
11250
0
      isymbuf = retrieve_local_syms (abfd);
11251
0
      section_index = isymbuf[r_symndx].st_shndx;
11252
11253
0
      if (section_index == SHN_UNDEF)
11254
0
  target_sec = bfd_und_section_ptr;
11255
0
      else if (section_index == SHN_ABS)
11256
0
  target_sec = bfd_abs_section_ptr;
11257
0
      else if (section_index == SHN_COMMON)
11258
0
  target_sec = bfd_com_section_ptr;
11259
0
      else
11260
0
  target_sec = bfd_section_from_elf_index (abfd, section_index);
11261
0
    }
11262
0
  else
11263
0
    {
11264
0
      unsigned long indx = r_symndx - symtab_hdr->sh_info;
11265
0
      struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
11266
11267
0
      while (h->root.type == bfd_link_hash_indirect
11268
0
       || h->root.type == bfd_link_hash_warning)
11269
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
11270
11271
0
      switch (h->root.type)
11272
0
  {
11273
0
  case bfd_link_hash_defined:
11274
0
  case  bfd_link_hash_defweak:
11275
0
    target_sec = h->root.u.def.section;
11276
0
    break;
11277
0
  case bfd_link_hash_common:
11278
0
    target_sec = bfd_com_section_ptr;
11279
0
    break;
11280
0
  case bfd_link_hash_undefined:
11281
0
  case bfd_link_hash_undefweak:
11282
0
    target_sec = bfd_und_section_ptr;
11283
0
    break;
11284
0
  default: /* New indirect warning.  */
11285
0
    target_sec = bfd_und_section_ptr;
11286
0
    break;
11287
0
  }
11288
0
    }
11289
0
  return target_sec;
11290
0
}
11291
11292
11293
static struct elf_link_hash_entry *
11294
get_elf_r_symndx_hash_entry (bfd *abfd, unsigned long r_symndx)
11295
0
{
11296
0
  unsigned long indx;
11297
0
  struct elf_link_hash_entry *h;
11298
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
11299
11300
0
  if (r_symndx < symtab_hdr->sh_info)
11301
0
    return NULL;
11302
11303
0
  indx = r_symndx - symtab_hdr->sh_info;
11304
0
  h = elf_sym_hashes (abfd)[indx];
11305
0
  while (h->root.type == bfd_link_hash_indirect
11306
0
   || h->root.type == bfd_link_hash_warning)
11307
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
11308
0
  return h;
11309
0
}
11310
11311
11312
/* Get the section-relative offset for a symbol number.  */
11313
11314
static bfd_vma
11315
get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
11316
0
{
11317
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
11318
0
  bfd_vma offset = 0;
11319
11320
0
  if (r_symndx < symtab_hdr->sh_info)
11321
0
    {
11322
0
      Elf_Internal_Sym *isymbuf;
11323
0
      isymbuf = retrieve_local_syms (abfd);
11324
0
      offset = isymbuf[r_symndx].st_value;
11325
0
    }
11326
0
  else
11327
0
    {
11328
0
      unsigned long indx = r_symndx - symtab_hdr->sh_info;
11329
0
      struct elf_link_hash_entry *h =
11330
0
  elf_sym_hashes (abfd)[indx];
11331
11332
0
      while (h->root.type == bfd_link_hash_indirect
11333
0
       || h->root.type == bfd_link_hash_warning)
11334
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
11335
0
      if (h->root.type == bfd_link_hash_defined
11336
0
    || h->root.type == bfd_link_hash_defweak)
11337
0
  offset = h->root.u.def.value;
11338
0
    }
11339
0
  return offset;
11340
0
}
11341
11342
11343
static bool
11344
is_reloc_sym_weak (bfd *abfd, Elf_Internal_Rela *rel)
11345
0
{
11346
0
  unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
11347
0
  struct elf_link_hash_entry *h;
11348
11349
0
  h = get_elf_r_symndx_hash_entry (abfd, r_symndx);
11350
0
  if (h && h->root.type == bfd_link_hash_defweak)
11351
0
    return true;
11352
0
  return false;
11353
0
}
11354
11355
11356
static bool
11357
pcrel_reloc_fits (xtensa_opcode opc,
11358
      int opnd,
11359
      bfd_vma self_address,
11360
      bfd_vma dest_address)
11361
0
{
11362
0
  xtensa_isa isa = xtensa_default_isa;
11363
0
  uint32 valp = dest_address;
11364
0
  if (xtensa_operand_do_reloc (isa, opc, opnd, &valp, self_address)
11365
0
      || xtensa_operand_encode (isa, opc, opnd, &valp))
11366
0
    return false;
11367
0
  return true;
11368
0
}
11369
11370
11371
static bool
11372
xtensa_is_property_section (asection *sec)
11373
0
{
11374
0
  if (xtensa_is_insntable_section (sec)
11375
0
      || xtensa_is_littable_section (sec)
11376
0
      || xtensa_is_proptable_section (sec))
11377
0
    return true;
11378
11379
0
  return false;
11380
0
}
11381
11382
11383
static bool
11384
xtensa_is_insntable_section (asection *sec)
11385
0
{
11386
0
  if (startswith (sec->name, XTENSA_INSN_SEC_NAME)
11387
0
      || startswith (sec->name, ".gnu.linkonce.x."))
11388
0
    return true;
11389
11390
0
  return false;
11391
0
}
11392
11393
11394
static bool
11395
xtensa_is_littable_section (asection *sec)
11396
0
{
11397
0
  if (startswith (sec->name, XTENSA_LIT_SEC_NAME)
11398
0
      || startswith (sec->name, ".gnu.linkonce.p."))
11399
0
    return true;
11400
11401
0
  return false;
11402
0
}
11403
11404
11405
static bool
11406
xtensa_is_proptable_section (asection *sec)
11407
0
{
11408
0
  if (startswith (sec->name, XTENSA_PROP_SEC_NAME)
11409
0
      || startswith (sec->name, ".gnu.linkonce.prop."))
11410
0
    return true;
11411
11412
0
  return false;
11413
0
}
11414
11415
11416
static int
11417
internal_reloc_compare (const void *ap, const void *bp)
11418
0
{
11419
0
  const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11420
0
  const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11421
11422
0
  if (a->r_offset != b->r_offset)
11423
0
    return (a->r_offset - b->r_offset);
11424
11425
  /* We don't need to sort on these criteria for correctness,
11426
     but enforcing a more strict ordering prevents unstable qsort
11427
     from behaving differently with different implementations.
11428
     Without the code below we get correct but different results
11429
     on Solaris 2.7 and 2.8.  We would like to always produce the
11430
     same results no matter the host.  */
11431
11432
0
  if (a->r_info != b->r_info)
11433
0
    return (a->r_info - b->r_info);
11434
11435
0
  return (a->r_addend - b->r_addend);
11436
0
}
11437
11438
11439
static int
11440
internal_reloc_matches (const void *ap, const void *bp)
11441
0
{
11442
0
  const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11443
0
  const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11444
11445
  /* Check if one entry overlaps with the other; this shouldn't happen
11446
     except when searching for a match.  */
11447
0
  return (a->r_offset - b->r_offset);
11448
0
}
11449
11450
11451
/* Predicate function used to look up a section in a particular group.  */
11452
11453
static bool
11454
match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
11455
0
{
11456
0
  const char *gname = inf;
11457
0
  const char *group_name = elf_group_name (sec);
11458
11459
0
  return (group_name == gname
11460
0
    || (group_name != NULL
11461
0
        && gname != NULL
11462
0
        && strcmp (group_name, gname) == 0));
11463
0
}
11464
11465
11466
static char *
11467
xtensa_add_names (const char *base, const char *suffix)
11468
96
{
11469
96
  if (suffix)
11470
48
    {
11471
48
      size_t base_len = strlen (base);
11472
48
      size_t suffix_len = strlen (suffix);
11473
48
      char *str = bfd_malloc (base_len + suffix_len + 1);
11474
11475
48
      memcpy (str, base, base_len);
11476
48
      memcpy (str + base_len, suffix, suffix_len + 1);
11477
48
      return str;
11478
48
    }
11479
48
  else
11480
48
    {
11481
48
      return strdup (base);
11482
48
    }
11483
96
}
11484
11485
static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
11486
11487
char *
11488
xtensa_property_section_name (asection *sec, const char *base_name,
11489
            bool separate_sections)
11490
96
{
11491
96
  const char *suffix, *group_name;
11492
96
  char *prop_sec_name;
11493
11494
96
  group_name = elf_group_name (sec);
11495
96
  if (group_name)
11496
0
    {
11497
0
      suffix = strrchr (sec->name, '.');
11498
0
      if (suffix == sec->name)
11499
0
  suffix = 0;
11500
0
      prop_sec_name = xtensa_add_names (base_name, suffix);
11501
0
    }
11502
96
  else if (startswith (sec->name, ".gnu.linkonce."))
11503
0
    {
11504
0
      char *linkonce_kind = 0;
11505
11506
0
      if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
11507
0
  linkonce_kind = "x.";
11508
0
      else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
11509
0
  linkonce_kind = "p.";
11510
0
      else if (strcmp (base_name, XTENSA_PROP_SEC_NAME) == 0)
11511
0
  linkonce_kind = "prop.";
11512
0
      else
11513
0
  abort ();
11514
11515
0
      prop_sec_name = (char *) bfd_malloc (strlen (sec->name)
11516
0
             + strlen (linkonce_kind) + 1);
11517
0
      memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
11518
0
      strcpy (prop_sec_name + linkonce_len, linkonce_kind);
11519
11520
0
      suffix = sec->name + linkonce_len;
11521
      /* For backward compatibility, replace "t." instead of inserting
11522
   the new linkonce_kind (but not for "prop" sections).  */
11523
0
      if (startswith (suffix, "t.") && linkonce_kind[1] == '.')
11524
0
  suffix += 2;
11525
0
      strcat (prop_sec_name + linkonce_len, suffix);
11526
0
    }
11527
96
  else
11528
96
    {
11529
96
      prop_sec_name = xtensa_add_names (base_name,
11530
96
          separate_sections ? sec->name : NULL);
11531
96
    }
11532
11533
96
  return prop_sec_name;
11534
96
}
11535
11536
11537
static asection *
11538
xtensa_get_separate_property_section (asection *sec, const char *base_name,
11539
              bool separate_section)
11540
96
{
11541
96
  char *prop_sec_name;
11542
96
  asection *prop_sec;
11543
11544
96
  prop_sec_name = xtensa_property_section_name (sec, base_name,
11545
96
            separate_section);
11546
96
  prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
11547
96
           match_section_group,
11548
96
           (void *) elf_group_name (sec));
11549
96
  free (prop_sec_name);
11550
96
  return prop_sec;
11551
96
}
11552
11553
static asection *
11554
xtensa_get_property_section (asection *sec, const char *base_name)
11555
48
{
11556
48
  asection *prop_sec;
11557
11558
  /* Try individual property section first.  */
11559
48
  prop_sec = xtensa_get_separate_property_section (sec, base_name, true);
11560
11561
  /* Refer to a common property section if individual is not present.  */
11562
48
  if (!prop_sec)
11563
48
    prop_sec = xtensa_get_separate_property_section (sec, base_name, false);
11564
11565
48
  return prop_sec;
11566
48
}
11567
11568
11569
flagword
11570
xtensa_get_property_predef_flags (asection *sec)
11571
0
{
11572
0
  if (xtensa_is_insntable_section (sec))
11573
0
    return (XTENSA_PROP_INSN
11574
0
      | XTENSA_PROP_NO_TRANSFORM
11575
0
      | XTENSA_PROP_INSN_NO_REORDER);
11576
11577
0
  if (xtensa_is_littable_section (sec))
11578
0
    return (XTENSA_PROP_LITERAL
11579
0
      | XTENSA_PROP_NO_TRANSFORM
11580
0
      | XTENSA_PROP_INSN_NO_REORDER);
11581
11582
0
  return 0;
11583
0
}
11584
11585

11586
/* Other functions called directly by the linker.  */
11587
11588
bool
11589
xtensa_callback_required_dependence (bfd *abfd,
11590
             asection *sec,
11591
             struct bfd_link_info *link_info,
11592
             deps_callback_t callback,
11593
             void *closure)
11594
0
{
11595
0
  Elf_Internal_Rela *internal_relocs;
11596
0
  bfd_byte *contents;
11597
0
  unsigned i;
11598
0
  bool ok = true;
11599
0
  bfd_size_type sec_size;
11600
11601
0
  sec_size = bfd_get_section_limit (abfd, sec);
11602
11603
  /* ".plt*" sections have no explicit relocations but they contain L32R
11604
     instructions that reference the corresponding ".got.plt*" sections.  */
11605
0
  if ((sec->flags & SEC_LINKER_CREATED) != 0
11606
0
      && startswith (sec->name, ".plt"))
11607
0
    {
11608
0
      asection *sgotplt;
11609
11610
      /* Find the corresponding ".got.plt*" section.  */
11611
0
      if (sec->name[4] == '\0')
11612
0
  sgotplt = elf_hash_table (link_info)->sgotplt;
11613
0
      else
11614
0
  {
11615
0
    char got_name[14];
11616
0
    int chunk = 0;
11617
11618
0
    BFD_ASSERT (sec->name[4] == '.');
11619
0
    chunk = strtol (&sec->name[5], NULL, 10);
11620
11621
0
    sprintf (got_name, ".got.plt.%u", chunk);
11622
0
    sgotplt = bfd_get_linker_section (sec->owner, got_name);
11623
0
  }
11624
0
      BFD_ASSERT (sgotplt);
11625
11626
      /* Assume worst-case offsets: L32R at the very end of the ".plt"
11627
   section referencing a literal at the very beginning of
11628
   ".got.plt".  This is very close to the real dependence, anyway.  */
11629
0
      (*callback) (sec, sec_size, sgotplt, 0, closure);
11630
0
    }
11631
11632
  /* Only ELF files are supported for Xtensa.  Check here to avoid a segfault
11633
     when building uclibc, which runs "ld -b binary /dev/null".  */
11634
0
  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11635
0
    return ok;
11636
11637
0
  internal_relocs = retrieve_internal_relocs (abfd, sec,
11638
0
                link_info->keep_memory);
11639
0
  if (internal_relocs == NULL
11640
0
      || sec->reloc_count == 0)
11641
0
    return ok;
11642
11643
  /* Cache the contents for the duration of this scan.  */
11644
0
  contents = retrieve_contents (abfd, sec, link_info->keep_memory);
11645
0
  if (contents == NULL && sec_size != 0)
11646
0
    {
11647
0
      ok = false;
11648
0
      goto error_return;
11649
0
    }
11650
11651
0
  if (!xtensa_default_isa)
11652
0
    xtensa_default_isa = xtensa_isa_init (0, 0);
11653
11654
0
  for (i = 0; i < sec->reloc_count; i++)
11655
0
    {
11656
0
      Elf_Internal_Rela *irel = &internal_relocs[i];
11657
0
      if (is_l32r_relocation (abfd, sec, contents, irel))
11658
0
  {
11659
0
    r_reloc l32r_rel;
11660
0
    asection *target_sec;
11661
0
    bfd_vma target_offset;
11662
11663
0
    r_reloc_init (&l32r_rel, abfd, irel, contents, sec_size);
11664
0
    target_sec = NULL;
11665
0
    target_offset = 0;
11666
    /* L32Rs must be local to the input file.  */
11667
0
    if (r_reloc_is_defined (&l32r_rel))
11668
0
      {
11669
0
        target_sec = r_reloc_get_section (&l32r_rel);
11670
0
        target_offset = l32r_rel.target_offset;
11671
0
      }
11672
0
    (*callback) (sec, irel->r_offset, target_sec, target_offset,
11673
0
           closure);
11674
0
  }
11675
0
    }
11676
11677
0
 error_return:
11678
0
  release_internal_relocs (sec, internal_relocs);
11679
0
  release_contents (sec, contents);
11680
0
  return ok;
11681
0
}
11682
11683
/* The default literal sections should always be marked as "code" (i.e.,
11684
   SHF_EXECINSTR).  This is particularly important for the Linux kernel
11685
   module loader so that the literals are not placed after the text.  */
11686
static const struct bfd_elf_special_section elf_xtensa_special_sections[] =
11687
{
11688
  { STRING_COMMA_LEN (".fini.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11689
  { STRING_COMMA_LEN (".init.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11690
  { STRING_COMMA_LEN (".literal"),  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11691
  { STRING_COMMA_LEN (".xtensa.info"),  0, SHT_NOTE,   0 },
11692
  { NULL,     0,  0, 0,    0 }
11693
};
11694

11695
#define ELF_TARGET_ID     XTENSA_ELF_DATA
11696
#ifndef ELF_ARCH
11697
#define TARGET_LITTLE_SYM   xtensa_elf32_le_vec
11698
#define TARGET_LITTLE_NAME    "elf32-xtensa-le"
11699
#define TARGET_BIG_SYM      xtensa_elf32_be_vec
11700
#define TARGET_BIG_NAME     "elf32-xtensa-be"
11701
#define ELF_ARCH      bfd_arch_xtensa
11702
11703
#define ELF_MACHINE_CODE    EM_XTENSA
11704
#define ELF_MACHINE_ALT1    EM_XTENSA_OLD
11705
11706
#define ELF_MAXPAGESIZE     0x1000
11707
#endif /* ELF_ARCH */
11708
11709
#define elf_backend_can_gc_sections 1
11710
#define elf_backend_can_refcount  1
11711
#define elf_backend_plt_readonly  1
11712
#define elf_backend_got_header_size 4
11713
#define elf_backend_want_dynbss   0
11714
#define elf_backend_want_got_plt  1
11715
#define elf_backend_dtrel_excludes_plt  1
11716
11717
#define elf_info_to_howto        elf_xtensa_info_to_howto_rela
11718
11719
#define bfd_elf32_mkobject         elf_xtensa_mkobject
11720
11721
#define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
11722
#define bfd_elf32_new_section_hook       elf_xtensa_new_section_hook
11723
#define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
11724
#define bfd_elf32_bfd_relax_section      elf_xtensa_relax_section
11725
#define bfd_elf32_bfd_reloc_type_lookup      elf_xtensa_reloc_type_lookup
11726
#define bfd_elf32_bfd_reloc_name_lookup \
11727
  elf_xtensa_reloc_name_lookup
11728
#define bfd_elf32_bfd_set_private_flags      elf_xtensa_set_private_flags
11729
#define bfd_elf32_bfd_link_hash_table_create elf_xtensa_link_hash_table_create
11730
11731
#define elf_backend_adjust_dynamic_symbol    elf_xtensa_adjust_dynamic_symbol
11732
#define elf_backend_check_relocs       elf_xtensa_check_relocs
11733
#define elf_backend_create_dynamic_sections  elf_xtensa_create_dynamic_sections
11734
#define elf_backend_discard_info       elf_xtensa_discard_info
11735
#define elf_backend_ignore_discarded_relocs  elf_xtensa_ignore_discarded_relocs
11736
#define elf_backend_final_write_processing   elf_xtensa_final_write_processing
11737
#define elf_backend_finish_dynamic_sections  elf_xtensa_finish_dynamic_sections
11738
#define elf_backend_finish_dynamic_symbol    elf_xtensa_finish_dynamic_symbol
11739
#define elf_backend_gc_mark_hook       elf_xtensa_gc_mark_hook
11740
#define elf_backend_gc_mark_extra_sections   elf_xtensa_gc_mark_extra_sections
11741
#define elf_backend_grok_prstatus      elf_xtensa_grok_prstatus
11742
#define elf_backend_grok_psinfo        elf_xtensa_grok_psinfo
11743
#define elf_backend_hide_symbol        elf_xtensa_hide_symbol
11744
#define elf_backend_object_p         elf_xtensa_object_p
11745
#define elf_backend_reloc_type_class       elf_xtensa_reloc_type_class
11746
#define elf_backend_relocate_section       elf_xtensa_relocate_section
11747
#define elf_backend_late_size_sections       elf_xtensa_late_size_sections
11748
#define elf_backend_early_size_sections      elf_xtensa_early_size_sections
11749
#define elf_backend_omit_section_dynsym      _bfd_elf_omit_section_dynsym_all
11750
#define elf_backend_special_sections       elf_xtensa_special_sections
11751
#define elf_backend_action_discarded       elf_xtensa_action_discarded
11752
#define elf_backend_copy_indirect_symbol     elf_xtensa_copy_indirect_symbol
11753
11754
#include "elf32-target.h"