1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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
30 #include "elf/xtensa.h"
31 #include "splay-tree.h"
32 #include "xtensa-isa.h"
33 #include "xtensa-config.h"
35 #define XTENSA_NO_NOP_REMOVAL 0
37 /* Local helper functions. */
39 static bfd_boolean add_extra_plt_sections (struct bfd_link_info *, int);
40 static char *vsprint_msg (const char *, const char *, int, ...) ATTRIBUTE_PRINTF(2,4);
41 static bfd_reloc_status_type bfd_elf_xtensa_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_boolean do_fix_for_relocatable_link
44 (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *);
45 static void do_fix_for_final_link
46 (Elf_Internal_Rela *, bfd *, asection *, bfd_byte *, bfd_vma *);
48 /* Local functions to handle Xtensa configurability. */
50 static bfd_boolean is_indirect_call_opcode (xtensa_opcode);
51 static bfd_boolean is_direct_call_opcode (xtensa_opcode);
52 static bfd_boolean is_windowed_call_opcode (xtensa_opcode);
53 static xtensa_opcode get_const16_opcode (void);
54 static xtensa_opcode get_l32r_opcode (void);
55 static bfd_vma l32r_offset (bfd_vma, bfd_vma);
56 static int get_relocation_opnd (xtensa_opcode, int);
57 static int get_relocation_slot (int);
58 static xtensa_opcode get_relocation_opcode
59 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
60 static bfd_boolean is_l32r_relocation
61 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *);
62 static bfd_boolean is_alt_relocation (int);
63 static bfd_boolean is_operand_relocation (int);
64 static bfd_size_type insn_decode_len
65 (bfd_byte *, bfd_size_type, bfd_size_type);
66 static xtensa_opcode insn_decode_opcode
67 (bfd_byte *, bfd_size_type, bfd_size_type, int);
68 static bfd_boolean check_branch_target_aligned
69 (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
70 static bfd_boolean check_loop_aligned
71 (bfd_byte *, bfd_size_type, bfd_vma, bfd_vma);
72 static bfd_boolean check_branch_target_aligned_address (bfd_vma, int);
73 static bfd_size_type get_asm_simplify_size
74 (bfd_byte *, bfd_size_type, bfd_size_type);
76 /* Functions for link-time code simplifications. */
78 static bfd_reloc_status_type elf_xtensa_do_asm_simplify
79 (bfd_byte *, bfd_vma, bfd_vma, char **);
80 static bfd_reloc_status_type contract_asm_expansion
81 (bfd_byte *, bfd_vma, Elf_Internal_Rela *, char **);
82 static xtensa_opcode swap_callx_for_call_opcode (xtensa_opcode);
83 static xtensa_opcode get_expanded_call_opcode (bfd_byte *, int, bfd_boolean *);
85 /* Access to internal relocations, section contents and symbols. */
87 static Elf_Internal_Rela *retrieve_internal_relocs
88 (bfd *, asection *, bfd_boolean);
89 static void pin_internal_relocs (asection *, Elf_Internal_Rela *);
90 static void release_internal_relocs (asection *, Elf_Internal_Rela *);
91 static bfd_byte *retrieve_contents (bfd *, asection *, bfd_boolean);
92 static void pin_contents (asection *, bfd_byte *);
93 static void release_contents (asection *, bfd_byte *);
94 static Elf_Internal_Sym *retrieve_local_syms (bfd *);
96 /* Miscellaneous utility functions. */
98 static asection *elf_xtensa_get_plt_section (struct bfd_link_info *, int);
99 static asection *elf_xtensa_get_gotplt_section (struct bfd_link_info *, int);
100 static asection *get_elf_r_symndx_section (bfd *, unsigned long);
101 static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
102 (bfd *, unsigned long);
103 static bfd_vma get_elf_r_symndx_offset (bfd *, unsigned long);
104 static bfd_boolean is_reloc_sym_weak (bfd *, Elf_Internal_Rela *);
105 static bfd_boolean pcrel_reloc_fits (xtensa_opcode, int, bfd_vma, bfd_vma);
106 static bfd_boolean xtensa_is_property_section (asection *);
107 static bfd_boolean xtensa_is_insntable_section (asection *);
108 static bfd_boolean xtensa_is_littable_section (asection *);
109 static bfd_boolean xtensa_is_proptable_section (asection *);
110 static int internal_reloc_compare (const void *, const void *);
111 static int internal_reloc_matches (const void *, const void *);
112 static asection *xtensa_get_property_section (asection *, const char *);
113 static flagword xtensa_get_property_predef_flags (asection *);
115 /* Other functions called directly by the linker. */
117 typedef void (*deps_callback_t)
118 (asection *, bfd_vma, asection *, bfd_vma, void *);
119 extern bfd_boolean xtensa_callback_required_dependence
120 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
123 /* Globally visible flag for choosing size optimization of NOP removal
124 instead of branch-target-aware minimization for NOP removal.
125 When nonzero, narrow all instructions and remove all NOPs possible
126 around longcall expansions. */
128 int elf32xtensa_size_opt;
131 /* The "new_section_hook" is used to set up a per-section
132 "xtensa_relax_info" data structure with additional information used
133 during relaxation. */
135 typedef struct xtensa_relax_info_struct xtensa_relax_info;
138 /* The GNU tools do not easily allow extending interfaces to pass around
139 the pointer to the Xtensa ISA information, so instead we add a global
140 variable here (in BFD) that can be used by any of the tools that need
143 xtensa_isa xtensa_default_isa;
146 /* When this is true, relocations may have been modified to refer to
147 symbols from other input files. The per-section list of "fix"
148 records needs to be checked when resolving relocations. */
150 static bfd_boolean relaxing_section = FALSE;
152 /* When this is true, during final links, literals that cannot be
153 coalesced and their relocations may be moved to other sections. */
155 int elf32xtensa_no_literal_movement = 1;
157 /* Rename one of the generic section flags to better document how it
159 /* Whether relocations have been processed. */
160 #define reloc_done sec_flg0
162 static reloc_howto_type elf_howto_table[] =
164 HOWTO (R_XTENSA_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
165 bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
167 HOWTO (R_XTENSA_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
168 bfd_elf_xtensa_reloc, "R_XTENSA_32",
169 TRUE, 0xffffffff, 0xffffffff, FALSE),
171 /* Replace a 32-bit value with a value from the runtime linker (only
172 used by linker-generated stub functions). The r_addend value is
173 special: 1 means to substitute a pointer to the runtime linker's
174 dynamic resolver function; 2 means to substitute the link map for
175 the shared object. */
176 HOWTO (R_XTENSA_RTLD, 0, 2, 32, FALSE, 0, complain_overflow_dont,
177 NULL, "R_XTENSA_RTLD", FALSE, 0, 0, FALSE),
179 HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
180 bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
181 FALSE, 0, 0xffffffff, FALSE),
182 HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
183 bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
184 FALSE, 0, 0xffffffff, FALSE),
185 HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
186 bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
187 FALSE, 0, 0xffffffff, FALSE),
188 HOWTO (R_XTENSA_PLT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
189 bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
190 FALSE, 0, 0xffffffff, FALSE),
194 /* Old relocations for backward compatibility. */
195 HOWTO (R_XTENSA_OP0, 0, 0, 0, TRUE, 0, complain_overflow_dont,
196 bfd_elf_xtensa_reloc, "R_XTENSA_OP0", FALSE, 0, 0, TRUE),
197 HOWTO (R_XTENSA_OP1, 0, 0, 0, TRUE, 0, complain_overflow_dont,
198 bfd_elf_xtensa_reloc, "R_XTENSA_OP1", FALSE, 0, 0, TRUE),
199 HOWTO (R_XTENSA_OP2, 0, 0, 0, TRUE, 0, complain_overflow_dont,
200 bfd_elf_xtensa_reloc, "R_XTENSA_OP2", FALSE, 0, 0, TRUE),
202 /* Assembly auto-expansion. */
203 HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, TRUE, 0, complain_overflow_dont,
204 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND", FALSE, 0, 0, TRUE),
205 /* Relax assembly auto-expansion. */
206 HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, TRUE, 0, complain_overflow_dont,
207 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY", FALSE, 0, 0, TRUE),
211 HOWTO (R_XTENSA_32_PCREL, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
212 bfd_elf_xtensa_reloc, "R_XTENSA_32_PCREL",
213 FALSE, 0, 0xffffffff, TRUE),
215 /* GNU extension to record C++ vtable hierarchy. */
216 HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, FALSE, 0, complain_overflow_dont,
217 NULL, "R_XTENSA_GNU_VTINHERIT",
219 /* GNU extension to record C++ vtable member usage. */
220 HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, FALSE, 0, complain_overflow_dont,
221 _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
224 /* Relocations for supporting difference of symbols. */
225 HOWTO (R_XTENSA_DIFF8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
226 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF8", FALSE, 0, 0xff, FALSE),
227 HOWTO (R_XTENSA_DIFF16, 0, 1, 16, FALSE, 0, complain_overflow_signed,
228 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF16", FALSE, 0, 0xffff, FALSE),
229 HOWTO (R_XTENSA_DIFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
230 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF32", FALSE, 0, 0xffffffff, FALSE),
232 /* General immediate operand relocations. */
233 HOWTO (R_XTENSA_SLOT0_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
234 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_OP", FALSE, 0, 0, TRUE),
235 HOWTO (R_XTENSA_SLOT1_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
236 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_OP", FALSE, 0, 0, TRUE),
237 HOWTO (R_XTENSA_SLOT2_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
238 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_OP", FALSE, 0, 0, TRUE),
239 HOWTO (R_XTENSA_SLOT3_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
240 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_OP", FALSE, 0, 0, TRUE),
241 HOWTO (R_XTENSA_SLOT4_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
242 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_OP", FALSE, 0, 0, TRUE),
243 HOWTO (R_XTENSA_SLOT5_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
244 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_OP", FALSE, 0, 0, TRUE),
245 HOWTO (R_XTENSA_SLOT6_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
246 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_OP", FALSE, 0, 0, TRUE),
247 HOWTO (R_XTENSA_SLOT7_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
248 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_OP", FALSE, 0, 0, TRUE),
249 HOWTO (R_XTENSA_SLOT8_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
250 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_OP", FALSE, 0, 0, TRUE),
251 HOWTO (R_XTENSA_SLOT9_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
252 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_OP", FALSE, 0, 0, TRUE),
253 HOWTO (R_XTENSA_SLOT10_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
254 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_OP", FALSE, 0, 0, TRUE),
255 HOWTO (R_XTENSA_SLOT11_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
256 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_OP", FALSE, 0, 0, TRUE),
257 HOWTO (R_XTENSA_SLOT12_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
258 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_OP", FALSE, 0, 0, TRUE),
259 HOWTO (R_XTENSA_SLOT13_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
260 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_OP", FALSE, 0, 0, TRUE),
261 HOWTO (R_XTENSA_SLOT14_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
262 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_OP", FALSE, 0, 0, TRUE),
264 /* "Alternate" relocations. The meaning of these is opcode-specific. */
265 HOWTO (R_XTENSA_SLOT0_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
266 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_ALT", FALSE, 0, 0, TRUE),
267 HOWTO (R_XTENSA_SLOT1_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
268 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_ALT", FALSE, 0, 0, TRUE),
269 HOWTO (R_XTENSA_SLOT2_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
270 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_ALT", FALSE, 0, 0, TRUE),
271 HOWTO (R_XTENSA_SLOT3_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
272 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_ALT", FALSE, 0, 0, TRUE),
273 HOWTO (R_XTENSA_SLOT4_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
274 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_ALT", FALSE, 0, 0, TRUE),
275 HOWTO (R_XTENSA_SLOT5_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
276 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_ALT", FALSE, 0, 0, TRUE),
277 HOWTO (R_XTENSA_SLOT6_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
278 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_ALT", FALSE, 0, 0, TRUE),
279 HOWTO (R_XTENSA_SLOT7_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
280 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_ALT", FALSE, 0, 0, TRUE),
281 HOWTO (R_XTENSA_SLOT8_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
282 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_ALT", FALSE, 0, 0, TRUE),
283 HOWTO (R_XTENSA_SLOT9_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
284 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_ALT", FALSE, 0, 0, TRUE),
285 HOWTO (R_XTENSA_SLOT10_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
286 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_ALT", FALSE, 0, 0, TRUE),
287 HOWTO (R_XTENSA_SLOT11_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
288 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_ALT", FALSE, 0, 0, TRUE),
289 HOWTO (R_XTENSA_SLOT12_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
290 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_ALT", FALSE, 0, 0, TRUE),
291 HOWTO (R_XTENSA_SLOT13_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
292 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_ALT", FALSE, 0, 0, TRUE),
293 HOWTO (R_XTENSA_SLOT14_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
294 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_ALT", FALSE, 0, 0, TRUE),
296 /* TLS relocations. */
297 HOWTO (R_XTENSA_TLSDESC_FN, 0, 2, 32, FALSE, 0, complain_overflow_dont,
298 bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_FN",
299 FALSE, 0, 0xffffffff, FALSE),
300 HOWTO (R_XTENSA_TLSDESC_ARG, 0, 2, 32, FALSE, 0, complain_overflow_dont,
301 bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_ARG",
302 FALSE, 0, 0xffffffff, FALSE),
303 HOWTO (R_XTENSA_TLS_DTPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
304 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_DTPOFF",
305 FALSE, 0, 0xffffffff, FALSE),
306 HOWTO (R_XTENSA_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
307 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_TPOFF",
308 FALSE, 0, 0xffffffff, FALSE),
309 HOWTO (R_XTENSA_TLS_FUNC, 0, 0, 0, FALSE, 0, complain_overflow_dont,
310 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_FUNC",
312 HOWTO (R_XTENSA_TLS_ARG, 0, 0, 0, FALSE, 0, complain_overflow_dont,
313 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_ARG",
315 HOWTO (R_XTENSA_TLS_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
316 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_CALL",
322 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
327 static reloc_howto_type *
328 elf_xtensa_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
329 bfd_reloc_code_real_type code)
334 TRACE ("BFD_RELOC_NONE");
335 return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
338 TRACE ("BFD_RELOC_32");
339 return &elf_howto_table[(unsigned) R_XTENSA_32 ];
341 case BFD_RELOC_32_PCREL:
342 TRACE ("BFD_RELOC_32_PCREL");
343 return &elf_howto_table[(unsigned) R_XTENSA_32_PCREL ];
345 case BFD_RELOC_XTENSA_DIFF8:
346 TRACE ("BFD_RELOC_XTENSA_DIFF8");
347 return &elf_howto_table[(unsigned) R_XTENSA_DIFF8 ];
349 case BFD_RELOC_XTENSA_DIFF16:
350 TRACE ("BFD_RELOC_XTENSA_DIFF16");
351 return &elf_howto_table[(unsigned) R_XTENSA_DIFF16 ];
353 case BFD_RELOC_XTENSA_DIFF32:
354 TRACE ("BFD_RELOC_XTENSA_DIFF32");
355 return &elf_howto_table[(unsigned) R_XTENSA_DIFF32 ];
357 case BFD_RELOC_XTENSA_RTLD:
358 TRACE ("BFD_RELOC_XTENSA_RTLD");
359 return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
361 case BFD_RELOC_XTENSA_GLOB_DAT:
362 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
363 return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
365 case BFD_RELOC_XTENSA_JMP_SLOT:
366 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
367 return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
369 case BFD_RELOC_XTENSA_RELATIVE:
370 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
371 return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
373 case BFD_RELOC_XTENSA_PLT:
374 TRACE ("BFD_RELOC_XTENSA_PLT");
375 return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
377 case BFD_RELOC_XTENSA_OP0:
378 TRACE ("BFD_RELOC_XTENSA_OP0");
379 return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
381 case BFD_RELOC_XTENSA_OP1:
382 TRACE ("BFD_RELOC_XTENSA_OP1");
383 return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
385 case BFD_RELOC_XTENSA_OP2:
386 TRACE ("BFD_RELOC_XTENSA_OP2");
387 return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
389 case BFD_RELOC_XTENSA_ASM_EXPAND:
390 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
391 return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
393 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
394 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
395 return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
397 case BFD_RELOC_VTABLE_INHERIT:
398 TRACE ("BFD_RELOC_VTABLE_INHERIT");
399 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
401 case BFD_RELOC_VTABLE_ENTRY:
402 TRACE ("BFD_RELOC_VTABLE_ENTRY");
403 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
405 case BFD_RELOC_XTENSA_TLSDESC_FN:
406 TRACE ("BFD_RELOC_XTENSA_TLSDESC_FN");
407 return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_FN ];
409 case BFD_RELOC_XTENSA_TLSDESC_ARG:
410 TRACE ("BFD_RELOC_XTENSA_TLSDESC_ARG");
411 return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_ARG ];
413 case BFD_RELOC_XTENSA_TLS_DTPOFF:
414 TRACE ("BFD_RELOC_XTENSA_TLS_DTPOFF");
415 return &elf_howto_table[(unsigned) R_XTENSA_TLS_DTPOFF ];
417 case BFD_RELOC_XTENSA_TLS_TPOFF:
418 TRACE ("BFD_RELOC_XTENSA_TLS_TPOFF");
419 return &elf_howto_table[(unsigned) R_XTENSA_TLS_TPOFF ];
421 case BFD_RELOC_XTENSA_TLS_FUNC:
422 TRACE ("BFD_RELOC_XTENSA_TLS_FUNC");
423 return &elf_howto_table[(unsigned) R_XTENSA_TLS_FUNC ];
425 case BFD_RELOC_XTENSA_TLS_ARG:
426 TRACE ("BFD_RELOC_XTENSA_TLS_ARG");
427 return &elf_howto_table[(unsigned) R_XTENSA_TLS_ARG ];
429 case BFD_RELOC_XTENSA_TLS_CALL:
430 TRACE ("BFD_RELOC_XTENSA_TLS_CALL");
431 return &elf_howto_table[(unsigned) R_XTENSA_TLS_CALL ];
434 if (code >= BFD_RELOC_XTENSA_SLOT0_OP
435 && code <= BFD_RELOC_XTENSA_SLOT14_OP)
437 unsigned n = (R_XTENSA_SLOT0_OP +
438 (code - BFD_RELOC_XTENSA_SLOT0_OP));
439 return &elf_howto_table[n];
442 if (code >= BFD_RELOC_XTENSA_SLOT0_ALT
443 && code <= BFD_RELOC_XTENSA_SLOT14_ALT)
445 unsigned n = (R_XTENSA_SLOT0_ALT +
446 (code - BFD_RELOC_XTENSA_SLOT0_ALT));
447 return &elf_howto_table[n];
457 static reloc_howto_type *
458 elf_xtensa_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
463 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
464 if (elf_howto_table[i].name != NULL
465 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
466 return &elf_howto_table[i];
472 /* Given an ELF "rela" relocation, find the corresponding howto and record
473 it in the BFD internal arelent representation of the relocation. */
476 elf_xtensa_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
478 Elf_Internal_Rela *dst)
480 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
482 if (r_type >= (unsigned int) R_XTENSA_max)
484 _bfd_error_handler (_("%B: invalid XTENSA reloc number: %d"), abfd, r_type);
487 cache_ptr->howto = &elf_howto_table[r_type];
491 /* Functions for the Xtensa ELF linker. */
493 /* The name of the dynamic interpreter. This is put in the .interp
496 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
498 /* The size in bytes of an entry in the procedure linkage table.
499 (This does _not_ include the space for the literals associated with
502 #define PLT_ENTRY_SIZE 16
504 /* For _really_ large PLTs, we may need to alternate between literals
505 and code to keep the literals within the 256K range of the L32R
506 instructions in the code. It's unlikely that anyone would ever need
507 such a big PLT, but an arbitrary limit on the PLT size would be bad.
508 Thus, we split the PLT into chunks. Since there's very little
509 overhead (2 extra literals) for each chunk, the chunk size is kept
510 small so that the code for handling multiple chunks get used and
511 tested regularly. With 254 entries, there are 1K of literals for
512 each chunk, and that seems like a nice round number. */
514 #define PLT_ENTRIES_PER_CHUNK 254
516 /* PLT entries are actually used as stub functions for lazy symbol
517 resolution. Once the symbol is resolved, the stub function is never
518 invoked. Note: the 32-byte frame size used here cannot be changed
519 without a corresponding change in the runtime linker. */
521 static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
523 #if XSHAL_ABI == XTHAL_ABI_WINDOWED
524 0x6c, 0x10, 0x04, /* entry sp, 32 */
526 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
527 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
528 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
529 0x0a, 0x80, 0x00, /* jx a8 */
533 static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
535 #if XSHAL_ABI == XTHAL_ABI_WINDOWED
536 0x36, 0x41, 0x00, /* entry sp, 32 */
538 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
539 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
540 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
541 0xa0, 0x08, 0x00, /* jx a8 */
545 /* The size of the thread control block. */
548 struct elf_xtensa_link_hash_entry
550 struct elf_link_hash_entry elf;
552 bfd_signed_vma tlsfunc_refcount;
554 #define GOT_UNKNOWN 0
556 #define GOT_TLS_GD 2 /* global or local dynamic */
557 #define GOT_TLS_IE 4 /* initial or local exec */
558 #define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
559 unsigned char tls_type;
562 #define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
564 struct elf_xtensa_obj_tdata
566 struct elf_obj_tdata root;
568 /* tls_type for each local got entry. */
569 char *local_got_tls_type;
571 bfd_signed_vma *local_tlsfunc_refcounts;
574 #define elf_xtensa_tdata(abfd) \
575 ((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
577 #define elf_xtensa_local_got_tls_type(abfd) \
578 (elf_xtensa_tdata (abfd)->local_got_tls_type)
580 #define elf_xtensa_local_tlsfunc_refcounts(abfd) \
581 (elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
583 #define is_xtensa_elf(bfd) \
584 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
585 && elf_tdata (bfd) != NULL \
586 && elf_object_id (bfd) == XTENSA_ELF_DATA)
589 elf_xtensa_mkobject (bfd *abfd)
591 return bfd_elf_allocate_object (abfd, sizeof (struct elf_xtensa_obj_tdata),
595 /* Xtensa ELF linker hash table. */
597 struct elf_xtensa_link_hash_table
599 struct elf_link_hash_table elf;
601 /* Short-cuts to get to dynamic linker sections. */
608 asection *spltlittbl;
610 /* Total count of PLT relocations seen during check_relocs.
611 The actual PLT code must be split into multiple sections and all
612 the sections have to be created before size_dynamic_sections,
613 where we figure out the exact number of PLT entries that will be
614 needed. It is OK if this count is an overestimate, e.g., some
615 relocations may be removed by GC. */
618 struct elf_xtensa_link_hash_entry *tlsbase;
621 /* Get the Xtensa ELF linker hash table from a link_info structure. */
623 #define elf_xtensa_hash_table(p) \
624 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
625 == XTENSA_ELF_DATA ? ((struct elf_xtensa_link_hash_table *) ((p)->hash)) : NULL)
627 /* Create an entry in an Xtensa ELF linker hash table. */
629 static struct bfd_hash_entry *
630 elf_xtensa_link_hash_newfunc (struct bfd_hash_entry *entry,
631 struct bfd_hash_table *table,
634 /* Allocate the structure if it has not already been allocated by a
638 entry = bfd_hash_allocate (table,
639 sizeof (struct elf_xtensa_link_hash_entry));
644 /* Call the allocation method of the superclass. */
645 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
648 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (entry);
649 eh->tlsfunc_refcount = 0;
650 eh->tls_type = GOT_UNKNOWN;
656 /* Create an Xtensa ELF linker hash table. */
658 static struct bfd_link_hash_table *
659 elf_xtensa_link_hash_table_create (bfd *abfd)
661 struct elf_link_hash_entry *tlsbase;
662 struct elf_xtensa_link_hash_table *ret;
663 bfd_size_type amt = sizeof (struct elf_xtensa_link_hash_table);
665 ret = bfd_zmalloc (amt);
669 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
670 elf_xtensa_link_hash_newfunc,
671 sizeof (struct elf_xtensa_link_hash_entry),
678 /* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
680 tlsbase = elf_link_hash_lookup (&ret->elf, "_TLS_MODULE_BASE_",
682 tlsbase->root.type = bfd_link_hash_new;
683 tlsbase->root.u.undef.abfd = NULL;
684 tlsbase->non_elf = 0;
685 ret->tlsbase = elf_xtensa_hash_entry (tlsbase);
686 ret->tlsbase->tls_type = GOT_UNKNOWN;
688 return &ret->elf.root;
691 /* Copy the extra info we tack onto an elf_link_hash_entry. */
694 elf_xtensa_copy_indirect_symbol (struct bfd_link_info *info,
695 struct elf_link_hash_entry *dir,
696 struct elf_link_hash_entry *ind)
698 struct elf_xtensa_link_hash_entry *edir, *eind;
700 edir = elf_xtensa_hash_entry (dir);
701 eind = elf_xtensa_hash_entry (ind);
703 if (ind->root.type == bfd_link_hash_indirect)
705 edir->tlsfunc_refcount += eind->tlsfunc_refcount;
706 eind->tlsfunc_refcount = 0;
708 if (dir->got.refcount <= 0)
710 edir->tls_type = eind->tls_type;
711 eind->tls_type = GOT_UNKNOWN;
715 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
718 static inline bfd_boolean
719 elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry *h,
720 struct bfd_link_info *info)
722 /* Check if we should do dynamic things to this symbol. The
723 "ignore_protected" argument need not be set, because Xtensa code
724 does not require special handling of STV_PROTECTED to make function
725 pointer comparisons work properly. The PLT addresses are never
726 used for function pointers. */
728 return _bfd_elf_dynamic_symbol_p (h, info, 0);
733 property_table_compare (const void *ap, const void *bp)
735 const property_table_entry *a = (const property_table_entry *) ap;
736 const property_table_entry *b = (const property_table_entry *) bp;
738 if (a->address == b->address)
740 if (a->size != b->size)
741 return (a->size - b->size);
743 if ((a->flags & XTENSA_PROP_ALIGN) != (b->flags & XTENSA_PROP_ALIGN))
744 return ((b->flags & XTENSA_PROP_ALIGN)
745 - (a->flags & XTENSA_PROP_ALIGN));
747 if ((a->flags & XTENSA_PROP_ALIGN)
748 && (GET_XTENSA_PROP_ALIGNMENT (a->flags)
749 != GET_XTENSA_PROP_ALIGNMENT (b->flags)))
750 return (GET_XTENSA_PROP_ALIGNMENT (a->flags)
751 - GET_XTENSA_PROP_ALIGNMENT (b->flags));
753 if ((a->flags & XTENSA_PROP_UNREACHABLE)
754 != (b->flags & XTENSA_PROP_UNREACHABLE))
755 return ((b->flags & XTENSA_PROP_UNREACHABLE)
756 - (a->flags & XTENSA_PROP_UNREACHABLE));
758 return (a->flags - b->flags);
761 return (a->address - b->address);
766 property_table_matches (const void *ap, const void *bp)
768 const property_table_entry *a = (const property_table_entry *) ap;
769 const property_table_entry *b = (const property_table_entry *) bp;
771 /* Check if one entry overlaps with the other. */
772 if ((b->address >= a->address && b->address < (a->address + a->size))
773 || (a->address >= b->address && a->address < (b->address + b->size)))
776 return (a->address - b->address);
780 /* Get the literal table or property table entries for the given
781 section. Sets TABLE_P and returns the number of entries. On
782 error, returns a negative value. */
785 xtensa_read_table_entries (bfd *abfd,
787 property_table_entry **table_p,
788 const char *sec_name,
789 bfd_boolean output_addr)
791 asection *table_section;
792 bfd_size_type table_size = 0;
793 bfd_byte *table_data;
794 property_table_entry *blocks;
795 int blk, block_count;
796 bfd_size_type num_records;
797 Elf_Internal_Rela *internal_relocs, *irel, *rel_end;
798 bfd_vma section_addr, off;
799 flagword predef_flags;
800 bfd_size_type table_entry_size, section_limit;
803 || !(section->flags & SEC_ALLOC)
804 || (section->flags & SEC_DEBUGGING))
810 table_section = xtensa_get_property_section (section, sec_name);
812 table_size = table_section->size;
820 predef_flags = xtensa_get_property_predef_flags (table_section);
821 table_entry_size = 12;
823 table_entry_size -= 4;
825 num_records = table_size / table_entry_size;
826 table_data = retrieve_contents (abfd, table_section, TRUE);
827 blocks = (property_table_entry *)
828 bfd_malloc (num_records * sizeof (property_table_entry));
832 section_addr = section->output_section->vma + section->output_offset;
834 section_addr = section->vma;
836 internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
837 if (internal_relocs && !table_section->reloc_done)
839 qsort (internal_relocs, table_section->reloc_count,
840 sizeof (Elf_Internal_Rela), internal_reloc_compare);
841 irel = internal_relocs;
846 section_limit = bfd_get_section_limit (abfd, section);
847 rel_end = internal_relocs + table_section->reloc_count;
849 for (off = 0; off < table_size; off += table_entry_size)
851 bfd_vma address = bfd_get_32 (abfd, table_data + off);
853 /* Skip any relocations before the current offset. This should help
854 avoid confusion caused by unexpected relocations for the preceding
857 (irel->r_offset < off
858 || (irel->r_offset == off
859 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_NONE)))
866 if (irel && irel->r_offset == off)
869 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
870 BFD_ASSERT (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32);
872 if (get_elf_r_symndx_section (abfd, r_symndx) != section)
875 sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
876 BFD_ASSERT (sym_off == 0);
877 address += (section_addr + sym_off + irel->r_addend);
881 if (address < section_addr
882 || address >= section_addr + section_limit)
886 blocks[block_count].address = address;
887 blocks[block_count].size = bfd_get_32 (abfd, table_data + off + 4);
889 blocks[block_count].flags = predef_flags;
891 blocks[block_count].flags = bfd_get_32 (abfd, table_data + off + 8);
895 release_contents (table_section, table_data);
896 release_internal_relocs (table_section, internal_relocs);
900 /* Now sort them into address order for easy reference. */
901 qsort (blocks, block_count, sizeof (property_table_entry),
902 property_table_compare);
904 /* Check that the table contents are valid. Problems may occur,
905 for example, if an unrelocated object file is stripped. */
906 for (blk = 1; blk < block_count; blk++)
908 /* The only circumstance where two entries may legitimately
909 have the same address is when one of them is a zero-size
910 placeholder to mark a place where fill can be inserted.
911 The zero-size entry should come first. */
912 if (blocks[blk - 1].address == blocks[blk].address &&
913 blocks[blk - 1].size != 0)
915 (*_bfd_error_handler) (_("%B(%A): invalid property table"),
917 bfd_set_error (bfd_error_bad_value);
929 static property_table_entry *
930 elf_xtensa_find_property_entry (property_table_entry *property_table,
931 int property_table_size,
934 property_table_entry entry;
935 property_table_entry *rv;
937 if (property_table_size == 0)
940 entry.address = addr;
944 rv = bsearch (&entry, property_table, property_table_size,
945 sizeof (property_table_entry), property_table_matches);
951 elf_xtensa_in_literal_pool (property_table_entry *lit_table,
955 if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
962 /* Look through the relocs for a section during the first phase, and
963 calculate needed space in the dynamic reloc sections. */
966 elf_xtensa_check_relocs (bfd *abfd,
967 struct bfd_link_info *info,
969 const Elf_Internal_Rela *relocs)
971 struct elf_xtensa_link_hash_table *htab;
972 Elf_Internal_Shdr *symtab_hdr;
973 struct elf_link_hash_entry **sym_hashes;
974 const Elf_Internal_Rela *rel;
975 const Elf_Internal_Rela *rel_end;
977 if (bfd_link_relocatable (info) || (sec->flags & SEC_ALLOC) == 0)
980 BFD_ASSERT (is_xtensa_elf (abfd));
982 htab = elf_xtensa_hash_table (info);
986 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
987 sym_hashes = elf_sym_hashes (abfd);
989 rel_end = relocs + sec->reloc_count;
990 for (rel = relocs; rel < rel_end; rel++)
993 unsigned long r_symndx;
994 struct elf_link_hash_entry *h = NULL;
995 struct elf_xtensa_link_hash_entry *eh;
996 int tls_type, old_tls_type;
997 bfd_boolean is_got = FALSE;
998 bfd_boolean is_plt = FALSE;
999 bfd_boolean is_tlsfunc = FALSE;
1001 r_symndx = ELF32_R_SYM (rel->r_info);
1002 r_type = ELF32_R_TYPE (rel->r_info);
1004 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1006 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1011 if (r_symndx >= symtab_hdr->sh_info)
1013 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1014 while (h->root.type == bfd_link_hash_indirect
1015 || h->root.type == bfd_link_hash_warning)
1016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1018 /* PR15323, ref flags aren't set for references in the same
1020 h->root.non_ir_ref = 1;
1022 eh = elf_xtensa_hash_entry (h);
1026 case R_XTENSA_TLSDESC_FN:
1027 if (bfd_link_pic (info))
1029 tls_type = GOT_TLS_GD;
1034 tls_type = GOT_TLS_IE;
1037 case R_XTENSA_TLSDESC_ARG:
1038 if (bfd_link_pic (info))
1040 tls_type = GOT_TLS_GD;
1045 tls_type = GOT_TLS_IE;
1046 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1051 case R_XTENSA_TLS_DTPOFF:
1052 if (bfd_link_pic (info))
1053 tls_type = GOT_TLS_GD;
1055 tls_type = GOT_TLS_IE;
1058 case R_XTENSA_TLS_TPOFF:
1059 tls_type = GOT_TLS_IE;
1060 if (bfd_link_pic (info))
1061 info->flags |= DF_STATIC_TLS;
1062 if (bfd_link_pic (info) || h)
1067 tls_type = GOT_NORMAL;
1072 tls_type = GOT_NORMAL;
1076 case R_XTENSA_GNU_VTINHERIT:
1077 /* This relocation describes the C++ object vtable hierarchy.
1078 Reconstruct it for later use during GC. */
1079 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1083 case R_XTENSA_GNU_VTENTRY:
1084 /* This relocation describes which C++ vtable entries are actually
1085 used. Record for later use during GC. */
1086 BFD_ASSERT (h != NULL);
1088 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1093 /* Nothing to do for any other relocations. */
1101 if (h->plt.refcount <= 0)
1104 h->plt.refcount = 1;
1107 h->plt.refcount += 1;
1109 /* Keep track of the total PLT relocation count even if we
1110 don't yet know whether the dynamic sections will be
1112 htab->plt_reloc_count += 1;
1114 if (elf_hash_table (info)->dynamic_sections_created)
1116 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1122 if (h->got.refcount <= 0)
1123 h->got.refcount = 1;
1125 h->got.refcount += 1;
1129 eh->tlsfunc_refcount += 1;
1131 old_tls_type = eh->tls_type;
1135 /* Allocate storage the first time. */
1136 if (elf_local_got_refcounts (abfd) == NULL)
1138 bfd_size_type size = symtab_hdr->sh_info;
1141 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1144 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) mem;
1146 mem = bfd_zalloc (abfd, size);
1149 elf_xtensa_local_got_tls_type (abfd) = (char *) mem;
1151 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1154 elf_xtensa_local_tlsfunc_refcounts (abfd)
1155 = (bfd_signed_vma *) mem;
1158 /* This is a global offset table entry for a local symbol. */
1159 if (is_got || is_plt)
1160 elf_local_got_refcounts (abfd) [r_symndx] += 1;
1163 elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx] += 1;
1165 old_tls_type = elf_xtensa_local_got_tls_type (abfd) [r_symndx];
1168 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1169 tls_type |= old_tls_type;
1170 /* If a TLS symbol is accessed using IE at least once,
1171 there is no point to use a dynamic model for it. */
1172 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1173 && ((old_tls_type & GOT_TLS_GD) == 0
1174 || (tls_type & GOT_TLS_IE) == 0))
1176 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GD))
1177 tls_type = old_tls_type;
1178 else if ((old_tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_GD))
1179 tls_type |= old_tls_type;
1182 (*_bfd_error_handler)
1183 (_("%B: `%s' accessed both as normal and thread local symbol"),
1185 h ? h->root.root.string : "<local>");
1190 if (old_tls_type != tls_type)
1193 eh->tls_type = tls_type;
1195 elf_xtensa_local_got_tls_type (abfd) [r_symndx] = tls_type;
1204 elf_xtensa_make_sym_local (struct bfd_link_info *info,
1205 struct elf_link_hash_entry *h)
1207 if (bfd_link_pic (info))
1209 if (h->plt.refcount > 0)
1211 /* For shared objects, there's no need for PLT entries for local
1212 symbols (use RELATIVE relocs instead of JMP_SLOT relocs). */
1213 if (h->got.refcount < 0)
1214 h->got.refcount = 0;
1215 h->got.refcount += h->plt.refcount;
1216 h->plt.refcount = 0;
1221 /* Don't need any dynamic relocations at all. */
1222 h->plt.refcount = 0;
1223 h->got.refcount = 0;
1229 elf_xtensa_hide_symbol (struct bfd_link_info *info,
1230 struct elf_link_hash_entry *h,
1231 bfd_boolean force_local)
1233 /* For a shared link, move the plt refcount to the got refcount to leave
1234 space for RELATIVE relocs. */
1235 elf_xtensa_make_sym_local (info, h);
1237 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1241 /* Return the section that should be marked against GC for a given
1245 elf_xtensa_gc_mark_hook (asection *sec,
1246 struct bfd_link_info *info,
1247 Elf_Internal_Rela *rel,
1248 struct elf_link_hash_entry *h,
1249 Elf_Internal_Sym *sym)
1251 /* Property sections are marked "KEEP" in the linker scripts, but they
1252 should not cause other sections to be marked. (This approach relies
1253 on elf_xtensa_discard_info to remove property table entries that
1254 describe discarded sections. Alternatively, it might be more
1255 efficient to avoid using "KEEP" in the linker scripts and instead use
1256 the gc_mark_extra_sections hook to mark only the property sections
1257 that describe marked sections. That alternative does not work well
1258 with the current property table sections, which do not correspond
1259 one-to-one with the sections they describe, but that should be fixed
1261 if (xtensa_is_property_section (sec))
1265 switch (ELF32_R_TYPE (rel->r_info))
1267 case R_XTENSA_GNU_VTINHERIT:
1268 case R_XTENSA_GNU_VTENTRY:
1272 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1276 /* Update the GOT & PLT entry reference counts
1277 for the section being removed. */
1280 elf_xtensa_gc_sweep_hook (bfd *abfd,
1281 struct bfd_link_info *info,
1283 const Elf_Internal_Rela *relocs)
1285 Elf_Internal_Shdr *symtab_hdr;
1286 struct elf_link_hash_entry **sym_hashes;
1287 const Elf_Internal_Rela *rel, *relend;
1288 struct elf_xtensa_link_hash_table *htab;
1290 htab = elf_xtensa_hash_table (info);
1294 if (bfd_link_relocatable (info))
1297 if ((sec->flags & SEC_ALLOC) == 0)
1300 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1301 sym_hashes = elf_sym_hashes (abfd);
1303 relend = relocs + sec->reloc_count;
1304 for (rel = relocs; rel < relend; rel++)
1306 unsigned long r_symndx;
1307 unsigned int r_type;
1308 struct elf_link_hash_entry *h = NULL;
1309 struct elf_xtensa_link_hash_entry *eh;
1310 bfd_boolean is_got = FALSE;
1311 bfd_boolean is_plt = FALSE;
1312 bfd_boolean is_tlsfunc = FALSE;
1314 r_symndx = ELF32_R_SYM (rel->r_info);
1315 if (r_symndx >= symtab_hdr->sh_info)
1317 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1318 while (h->root.type == bfd_link_hash_indirect
1319 || h->root.type == bfd_link_hash_warning)
1320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1322 eh = elf_xtensa_hash_entry (h);
1324 r_type = ELF32_R_TYPE (rel->r_info);
1327 case R_XTENSA_TLSDESC_FN:
1328 if (bfd_link_pic (info))
1335 case R_XTENSA_TLSDESC_ARG:
1336 if (bfd_link_pic (info))
1340 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1345 case R_XTENSA_TLS_TPOFF:
1346 if (bfd_link_pic (info) || h)
1366 /* If the symbol has been localized its plt.refcount got moved
1367 to got.refcount. Handle it as GOT. */
1368 if (h->plt.refcount > 0)
1375 if (h->got.refcount > 0)
1380 if (eh->tlsfunc_refcount > 0)
1381 eh->tlsfunc_refcount--;
1386 if (is_got || is_plt)
1388 bfd_signed_vma *got_refcount
1389 = &elf_local_got_refcounts (abfd) [r_symndx];
1390 if (*got_refcount > 0)
1395 bfd_signed_vma *tlsfunc_refcount
1396 = &elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx];
1397 if (*tlsfunc_refcount > 0)
1398 *tlsfunc_refcount -= 1;
1407 /* Create all the dynamic sections. */
1410 elf_xtensa_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1412 struct elf_xtensa_link_hash_table *htab;
1413 flagword flags, noalloc_flags;
1415 htab = elf_xtensa_hash_table (info);
1419 /* First do all the standard stuff. */
1420 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
1422 htab->splt = bfd_get_linker_section (dynobj, ".plt");
1423 htab->srelplt = bfd_get_linker_section (dynobj, ".rela.plt");
1424 htab->sgot = bfd_get_linker_section (dynobj, ".got");
1425 htab->sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
1426 htab->srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1428 /* Create any extra PLT sections in case check_relocs has already
1429 been called on all the non-dynamic input files. */
1430 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1433 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
1434 | SEC_LINKER_CREATED | SEC_READONLY);
1435 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
1437 /* Mark the ".got.plt" section READONLY. */
1438 if (htab->sgotplt == NULL
1439 || ! bfd_set_section_flags (dynobj, htab->sgotplt, flags))
1442 /* Create ".got.loc" (literal tables for use by dynamic linker). */
1443 htab->sgotloc = bfd_make_section_anyway_with_flags (dynobj, ".got.loc",
1445 if (htab->sgotloc == NULL
1446 || ! bfd_set_section_alignment (dynobj, htab->sgotloc, 2))
1449 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
1450 htab->spltlittbl = bfd_make_section_anyway_with_flags (dynobj, ".xt.lit.plt",
1452 if (htab->spltlittbl == NULL
1453 || ! bfd_set_section_alignment (dynobj, htab->spltlittbl, 2))
1461 add_extra_plt_sections (struct bfd_link_info *info, int count)
1463 bfd *dynobj = elf_hash_table (info)->dynobj;
1466 /* Iterate over all chunks except 0 which uses the standard ".plt" and
1467 ".got.plt" sections. */
1468 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
1474 /* Stop when we find a section has already been created. */
1475 if (elf_xtensa_get_plt_section (info, chunk))
1478 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1479 | SEC_LINKER_CREATED | SEC_READONLY);
1481 sname = (char *) bfd_malloc (10);
1482 sprintf (sname, ".plt.%u", chunk);
1483 s = bfd_make_section_anyway_with_flags (dynobj, sname, flags | SEC_CODE);
1485 || ! bfd_set_section_alignment (dynobj, s, 2))
1488 sname = (char *) bfd_malloc (14);
1489 sprintf (sname, ".got.plt.%u", chunk);
1490 s = bfd_make_section_anyway_with_flags (dynobj, sname, flags);
1492 || ! bfd_set_section_alignment (dynobj, s, 2))
1500 /* Adjust a symbol defined by a dynamic object and referenced by a
1501 regular object. The current definition is in some section of the
1502 dynamic object, but we're not including those sections. We have to
1503 change the definition to something the rest of the link can
1507 elf_xtensa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1508 struct elf_link_hash_entry *h)
1510 /* If this is a weak symbol, and there is a real definition, the
1511 processor independent code will have arranged for us to see the
1512 real definition first, and we can just use the same value. */
1515 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1516 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1517 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1518 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1522 /* This is a reference to a symbol defined by a dynamic object. The
1523 reference must go through the GOT, so there's no need for COPY relocs,
1531 elf_xtensa_allocate_dynrelocs (struct elf_link_hash_entry *h, void *arg)
1533 struct bfd_link_info *info;
1534 struct elf_xtensa_link_hash_table *htab;
1535 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (h);
1537 if (h->root.type == bfd_link_hash_indirect)
1540 info = (struct bfd_link_info *) arg;
1541 htab = elf_xtensa_hash_table (info);
1545 /* If we saw any use of an IE model for this symbol, we can then optimize
1546 away GOT entries for any TLSDESC_FN relocs. */
1547 if ((eh->tls_type & GOT_TLS_IE) != 0)
1549 BFD_ASSERT (h->got.refcount >= eh->tlsfunc_refcount);
1550 h->got.refcount -= eh->tlsfunc_refcount;
1553 if (! elf_xtensa_dynamic_symbol_p (h, info))
1554 elf_xtensa_make_sym_local (info, h);
1556 if (h->plt.refcount > 0)
1557 htab->srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1559 if (h->got.refcount > 0)
1560 htab->srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
1567 elf_xtensa_allocate_local_got_size (struct bfd_link_info *info)
1569 struct elf_xtensa_link_hash_table *htab;
1572 htab = elf_xtensa_hash_table (info);
1576 for (i = info->input_bfds; i; i = i->link.next)
1578 bfd_signed_vma *local_got_refcounts;
1579 bfd_size_type j, cnt;
1580 Elf_Internal_Shdr *symtab_hdr;
1582 local_got_refcounts = elf_local_got_refcounts (i);
1583 if (!local_got_refcounts)
1586 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1587 cnt = symtab_hdr->sh_info;
1589 for (j = 0; j < cnt; ++j)
1591 /* If we saw any use of an IE model for this symbol, we can
1592 then optimize away GOT entries for any TLSDESC_FN relocs. */
1593 if ((elf_xtensa_local_got_tls_type (i) [j] & GOT_TLS_IE) != 0)
1595 bfd_signed_vma *tlsfunc_refcount
1596 = &elf_xtensa_local_tlsfunc_refcounts (i) [j];
1597 BFD_ASSERT (local_got_refcounts[j] >= *tlsfunc_refcount);
1598 local_got_refcounts[j] -= *tlsfunc_refcount;
1601 if (local_got_refcounts[j] > 0)
1602 htab->srelgot->size += (local_got_refcounts[j]
1603 * sizeof (Elf32_External_Rela));
1609 /* Set the sizes of the dynamic sections. */
1612 elf_xtensa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1613 struct bfd_link_info *info)
1615 struct elf_xtensa_link_hash_table *htab;
1617 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1618 bfd_boolean relplt, relgot;
1619 int plt_entries, plt_chunks, chunk;
1624 htab = elf_xtensa_hash_table (info);
1628 dynobj = elf_hash_table (info)->dynobj;
1631 srelgot = htab->srelgot;
1632 srelplt = htab->srelplt;
1634 if (elf_hash_table (info)->dynamic_sections_created)
1636 BFD_ASSERT (htab->srelgot != NULL
1637 && htab->srelplt != NULL
1638 && htab->sgot != NULL
1639 && htab->spltlittbl != NULL
1640 && htab->sgotloc != NULL);
1642 /* Set the contents of the .interp section to the interpreter. */
1643 if (bfd_link_executable (info) && !info->nointerp)
1645 s = bfd_get_linker_section (dynobj, ".interp");
1648 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1649 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1652 /* Allocate room for one word in ".got". */
1653 htab->sgot->size = 4;
1655 /* Allocate space in ".rela.got" for literals that reference global
1656 symbols and space in ".rela.plt" for literals that have PLT
1658 elf_link_hash_traverse (elf_hash_table (info),
1659 elf_xtensa_allocate_dynrelocs,
1662 /* If we are generating a shared object, we also need space in
1663 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1664 reference local symbols. */
1665 if (bfd_link_pic (info))
1666 elf_xtensa_allocate_local_got_size (info);
1668 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1669 each PLT entry, we need the PLT code plus a 4-byte literal.
1670 For each chunk of ".plt", we also need two more 4-byte
1671 literals, two corresponding entries in ".rela.got", and an
1672 8-byte entry in ".xt.lit.plt". */
1673 spltlittbl = htab->spltlittbl;
1674 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
1676 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1678 /* Iterate over all the PLT chunks, including any extra sections
1679 created earlier because the initial count of PLT relocations
1680 was an overestimate. */
1682 (splt = elf_xtensa_get_plt_section (info, chunk)) != NULL;
1687 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
1688 BFD_ASSERT (sgotplt != NULL);
1690 if (chunk < plt_chunks - 1)
1691 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1692 else if (chunk == plt_chunks - 1)
1693 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1697 if (chunk_entries != 0)
1699 sgotplt->size = 4 * (chunk_entries + 2);
1700 splt->size = PLT_ENTRY_SIZE * chunk_entries;
1701 srelgot->size += 2 * sizeof (Elf32_External_Rela);
1702 spltlittbl->size += 8;
1711 /* Allocate space in ".got.loc" to match the total size of all the
1713 sgotloc = htab->sgotloc;
1714 sgotloc->size = spltlittbl->size;
1715 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
1717 if (abfd->flags & DYNAMIC)
1719 for (s = abfd->sections; s != NULL; s = s->next)
1721 if (! discarded_section (s)
1722 && xtensa_is_littable_section (s)
1724 sgotloc->size += s->size;
1729 /* Allocate memory for dynamic sections. */
1732 for (s = dynobj->sections; s != NULL; s = s->next)
1736 if ((s->flags & SEC_LINKER_CREATED) == 0)
1739 /* It's OK to base decisions on the section name, because none
1740 of the dynobj section names depend upon the input files. */
1741 name = bfd_get_section_name (dynobj, s);
1743 if (CONST_STRNEQ (name, ".rela"))
1747 if (strcmp (name, ".rela.plt") == 0)
1749 else if (strcmp (name, ".rela.got") == 0)
1752 /* We use the reloc_count field as a counter if we need
1753 to copy relocs into the output file. */
1757 else if (! CONST_STRNEQ (name, ".plt.")
1758 && ! CONST_STRNEQ (name, ".got.plt.")
1759 && strcmp (name, ".got") != 0
1760 && strcmp (name, ".plt") != 0
1761 && strcmp (name, ".got.plt") != 0
1762 && strcmp (name, ".xt.lit.plt") != 0
1763 && strcmp (name, ".got.loc") != 0)
1765 /* It's not one of our sections, so don't allocate space. */
1771 /* If we don't need this section, strip it from the output
1772 file. We must create the ".plt*" and ".got.plt*"
1773 sections in create_dynamic_sections and/or check_relocs
1774 based on a conservative estimate of the PLT relocation
1775 count, because the sections must be created before the
1776 linker maps input sections to output sections. The
1777 linker does that before size_dynamic_sections, where we
1778 compute the exact size of the PLT, so there may be more
1779 of these sections than are actually needed. */
1780 s->flags |= SEC_EXCLUDE;
1782 else if ((s->flags & SEC_HAS_CONTENTS) != 0)
1784 /* Allocate memory for the section contents. */
1785 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1786 if (s->contents == NULL)
1791 if (elf_hash_table (info)->dynamic_sections_created)
1793 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1794 known until finish_dynamic_sections, but we need to get the relocs
1795 in place before they are sorted. */
1796 for (chunk = 0; chunk < plt_chunks; chunk++)
1798 Elf_Internal_Rela irela;
1802 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1805 loc = (srelgot->contents
1806 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1807 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1808 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1809 loc + sizeof (Elf32_External_Rela));
1810 srelgot->reloc_count += 2;
1813 /* Add some entries to the .dynamic section. We fill in the
1814 values later, in elf_xtensa_finish_dynamic_sections, but we
1815 must add the entries now so that we get the correct size for
1816 the .dynamic section. The DT_DEBUG entry is filled in by the
1817 dynamic linker and used by the debugger. */
1818 #define add_dynamic_entry(TAG, VAL) \
1819 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1821 if (bfd_link_executable (info))
1823 if (!add_dynamic_entry (DT_DEBUG, 0))
1829 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1830 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1831 || !add_dynamic_entry (DT_JMPREL, 0))
1837 if (!add_dynamic_entry (DT_RELA, 0)
1838 || !add_dynamic_entry (DT_RELASZ, 0)
1839 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1843 if (!add_dynamic_entry (DT_PLTGOT, 0)
1844 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1845 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1848 #undef add_dynamic_entry
1854 elf_xtensa_always_size_sections (bfd *output_bfd,
1855 struct bfd_link_info *info)
1857 struct elf_xtensa_link_hash_table *htab;
1860 htab = elf_xtensa_hash_table (info);
1864 tls_sec = htab->elf.tls_sec;
1866 if (tls_sec && (htab->tlsbase->tls_type & GOT_TLS_ANY) != 0)
1868 struct elf_link_hash_entry *tlsbase = &htab->tlsbase->elf;
1869 struct bfd_link_hash_entry *bh = &tlsbase->root;
1870 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1872 tlsbase->type = STT_TLS;
1873 if (!(_bfd_generic_link_add_one_symbol
1874 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1875 tls_sec, 0, NULL, FALSE,
1876 bed->collect, &bh)))
1878 tlsbase->def_regular = 1;
1879 tlsbase->other = STV_HIDDEN;
1880 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1887 /* Return the base VMA address which should be subtracted from real addresses
1888 when resolving @dtpoff relocation.
1889 This is PT_TLS segment p_vaddr. */
1892 dtpoff_base (struct bfd_link_info *info)
1894 /* If tls_sec is NULL, we should have signalled an error already. */
1895 if (elf_hash_table (info)->tls_sec == NULL)
1897 return elf_hash_table (info)->tls_sec->vma;
1900 /* Return the relocation value for @tpoff relocation
1901 if STT_TLS virtual address is ADDRESS. */
1904 tpoff (struct bfd_link_info *info, bfd_vma address)
1906 struct elf_link_hash_table *htab = elf_hash_table (info);
1909 /* If tls_sec is NULL, we should have signalled an error already. */
1910 if (htab->tls_sec == NULL)
1912 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
1913 return address - htab->tls_sec->vma + base;
1916 /* Perform the specified relocation. The instruction at (contents + address)
1917 is modified to set one operand to represent the value in "relocation". The
1918 operand position is determined by the relocation type recorded in the
1921 #define CALL_SEGMENT_BITS (30)
1922 #define CALL_SEGMENT_SIZE (1 << CALL_SEGMENT_BITS)
1924 static bfd_reloc_status_type
1925 elf_xtensa_do_reloc (reloc_howto_type *howto,
1927 asection *input_section,
1931 bfd_boolean is_weak_undef,
1932 char **error_message)
1935 xtensa_opcode opcode;
1936 xtensa_isa isa = xtensa_default_isa;
1937 static xtensa_insnbuf ibuff = NULL;
1938 static xtensa_insnbuf sbuff = NULL;
1939 bfd_vma self_address;
1940 bfd_size_type input_size;
1946 ibuff = xtensa_insnbuf_alloc (isa);
1947 sbuff = xtensa_insnbuf_alloc (isa);
1950 input_size = bfd_get_section_limit (abfd, input_section);
1952 /* Calculate the PC address for this instruction. */
1953 self_address = (input_section->output_section->vma
1954 + input_section->output_offset
1957 switch (howto->type)
1960 case R_XTENSA_DIFF8:
1961 case R_XTENSA_DIFF16:
1962 case R_XTENSA_DIFF32:
1963 case R_XTENSA_TLS_FUNC:
1964 case R_XTENSA_TLS_ARG:
1965 case R_XTENSA_TLS_CALL:
1966 return bfd_reloc_ok;
1968 case R_XTENSA_ASM_EXPAND:
1971 /* Check for windowed CALL across a 1GB boundary. */
1972 opcode = get_expanded_call_opcode (contents + address,
1973 input_size - address, 0);
1974 if (is_windowed_call_opcode (opcode))
1976 if ((self_address >> CALL_SEGMENT_BITS)
1977 != (relocation >> CALL_SEGMENT_BITS))
1979 *error_message = "windowed longcall crosses 1GB boundary; "
1981 return bfd_reloc_dangerous;
1985 return bfd_reloc_ok;
1987 case R_XTENSA_ASM_SIMPLIFY:
1989 /* Convert the L32R/CALLX to CALL. */
1990 bfd_reloc_status_type retval =
1991 elf_xtensa_do_asm_simplify (contents, address, input_size,
1993 if (retval != bfd_reloc_ok)
1994 return bfd_reloc_dangerous;
1996 /* The CALL needs to be relocated. Continue below for that part. */
1999 howto = &elf_howto_table[(unsigned) R_XTENSA_SLOT0_OP ];
2006 x = bfd_get_32 (abfd, contents + address);
2008 bfd_put_32 (abfd, x, contents + address);
2010 return bfd_reloc_ok;
2012 case R_XTENSA_32_PCREL:
2013 bfd_put_32 (abfd, relocation - self_address, contents + address);
2014 return bfd_reloc_ok;
2017 case R_XTENSA_TLSDESC_FN:
2018 case R_XTENSA_TLSDESC_ARG:
2019 case R_XTENSA_TLS_DTPOFF:
2020 case R_XTENSA_TLS_TPOFF:
2021 bfd_put_32 (abfd, relocation, contents + address);
2022 return bfd_reloc_ok;
2025 /* Only instruction slot-specific relocations handled below.... */
2026 slot = get_relocation_slot (howto->type);
2027 if (slot == XTENSA_UNDEFINED)
2029 *error_message = "unexpected relocation";
2030 return bfd_reloc_dangerous;
2033 /* Read the instruction into a buffer and decode the opcode. */
2034 xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
2035 input_size - address);
2036 fmt = xtensa_format_decode (isa, ibuff);
2037 if (fmt == XTENSA_UNDEFINED)
2039 *error_message = "cannot decode instruction format";
2040 return bfd_reloc_dangerous;
2043 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
2045 opcode = xtensa_opcode_decode (isa, fmt, slot, sbuff);
2046 if (opcode == XTENSA_UNDEFINED)
2048 *error_message = "cannot decode instruction opcode";
2049 return bfd_reloc_dangerous;
2052 /* Check for opcode-specific "alternate" relocations. */
2053 if (is_alt_relocation (howto->type))
2055 if (opcode == get_l32r_opcode ())
2057 /* Handle the special-case of non-PC-relative L32R instructions. */
2058 bfd *output_bfd = input_section->output_section->owner;
2059 asection *lit4_sec = bfd_get_section_by_name (output_bfd, ".lit4");
2062 *error_message = "relocation references missing .lit4 section";
2063 return bfd_reloc_dangerous;
2065 self_address = ((lit4_sec->vma & ~0xfff)
2066 + 0x40000 - 3); /* -3 to compensate for do_reloc */
2067 newval = relocation;
2070 else if (opcode == get_const16_opcode ())
2072 /* ALT used for high 16 bits. */
2073 newval = relocation >> 16;
2078 /* No other "alternate" relocations currently defined. */
2079 *error_message = "unexpected relocation";
2080 return bfd_reloc_dangerous;
2083 else /* Not an "alternate" relocation.... */
2085 if (opcode == get_const16_opcode ())
2087 newval = relocation & 0xffff;
2092 /* ...normal PC-relative relocation.... */
2094 /* Determine which operand is being relocated. */
2095 opnd = get_relocation_opnd (opcode, howto->type);
2096 if (opnd == XTENSA_UNDEFINED)
2098 *error_message = "unexpected relocation";
2099 return bfd_reloc_dangerous;
2102 if (!howto->pc_relative)
2104 *error_message = "expected PC-relative relocation";
2105 return bfd_reloc_dangerous;
2108 newval = relocation;
2112 /* Apply the relocation. */
2113 if (xtensa_operand_do_reloc (isa, opcode, opnd, &newval, self_address)
2114 || xtensa_operand_encode (isa, opcode, opnd, &newval)
2115 || xtensa_operand_set_field (isa, opcode, opnd, fmt, slot,
2118 const char *opname = xtensa_opcode_name (isa, opcode);
2121 msg = "cannot encode";
2122 if (is_direct_call_opcode (opcode))
2124 if ((relocation & 0x3) != 0)
2125 msg = "misaligned call target";
2127 msg = "call target out of range";
2129 else if (opcode == get_l32r_opcode ())
2131 if ((relocation & 0x3) != 0)
2132 msg = "misaligned literal target";
2133 else if (is_alt_relocation (howto->type))
2134 msg = "literal target out of range (too many literals)";
2135 else if (self_address > relocation)
2136 msg = "literal target out of range (try using text-section-literals)";
2138 msg = "literal placed after use";
2141 *error_message = vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
2142 return bfd_reloc_dangerous;
2145 /* Check for calls across 1GB boundaries. */
2146 if (is_direct_call_opcode (opcode)
2147 && is_windowed_call_opcode (opcode))
2149 if ((self_address >> CALL_SEGMENT_BITS)
2150 != (relocation >> CALL_SEGMENT_BITS))
2153 "windowed call crosses 1GB boundary; return may fail";
2154 return bfd_reloc_dangerous;
2158 /* Write the modified instruction back out of the buffer. */
2159 xtensa_format_set_slot (isa, fmt, slot, ibuff, sbuff);
2160 xtensa_insnbuf_to_chars (isa, ibuff, contents + address,
2161 input_size - address);
2162 return bfd_reloc_ok;
2167 vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
2169 /* To reduce the size of the memory leak,
2170 we only use a single message buffer. */
2171 static bfd_size_type alloc_size = 0;
2172 static char *message = NULL;
2173 bfd_size_type orig_len, len = 0;
2174 bfd_boolean is_append;
2177 va_start (ap, arglen);
2179 is_append = (origmsg == message);
2181 orig_len = strlen (origmsg);
2182 len = orig_len + strlen (fmt) + arglen + 20;
2183 if (len > alloc_size)
2185 message = (char *) bfd_realloc_or_free (message, len);
2188 if (message != NULL)
2191 memcpy (message, origmsg, orig_len);
2192 vsprintf (message + orig_len, fmt, ap);
2199 /* This function is registered as the "special_function" in the
2200 Xtensa howto for handling simplify operations.
2201 bfd_perform_relocation / bfd_install_relocation use it to
2202 perform (install) the specified relocation. Since this replaces the code
2203 in bfd_perform_relocation, it is basically an Xtensa-specific,
2204 stripped-down version of bfd_perform_relocation. */
2206 static bfd_reloc_status_type
2207 bfd_elf_xtensa_reloc (bfd *abfd,
2208 arelent *reloc_entry,
2211 asection *input_section,
2213 char **error_message)
2216 bfd_reloc_status_type flag;
2217 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2218 bfd_vma output_base = 0;
2219 reloc_howto_type *howto = reloc_entry->howto;
2220 asection *reloc_target_output_section;
2221 bfd_boolean is_weak_undef;
2223 if (!xtensa_default_isa)
2224 xtensa_default_isa = xtensa_isa_init (0, 0);
2226 /* ELF relocs are against symbols. If we are producing relocatable
2227 output, and the reloc is against an external symbol, the resulting
2228 reloc will also be against the same symbol. In such a case, we
2229 don't want to change anything about the way the reloc is handled,
2230 since it will all be done at final link time. This test is similar
2231 to what bfd_elf_generic_reloc does except that it lets relocs with
2232 howto->partial_inplace go through even if the addend is non-zero.
2233 (The real problem is that partial_inplace is set for XTENSA_32
2234 relocs to begin with, but that's a long story and there's little we
2235 can do about it now....) */
2237 if (output_bfd && (symbol->flags & BSF_SECTION_SYM) == 0)
2239 reloc_entry->address += input_section->output_offset;
2240 return bfd_reloc_ok;
2243 /* Is the address of the relocation really within the section? */
2244 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2245 return bfd_reloc_outofrange;
2247 /* Work out which section the relocation is targeted at and the
2248 initial relocation command value. */
2250 /* Get symbol value. (Common symbols are special.) */
2251 if (bfd_is_com_section (symbol->section))
2254 relocation = symbol->value;
2256 reloc_target_output_section = symbol->section->output_section;
2258 /* Convert input-section-relative symbol value to absolute. */
2259 if ((output_bfd && !howto->partial_inplace)
2260 || reloc_target_output_section == NULL)
2263 output_base = reloc_target_output_section->vma;
2265 relocation += output_base + symbol->section->output_offset;
2267 /* Add in supplied addend. */
2268 relocation += reloc_entry->addend;
2270 /* Here the variable relocation holds the final address of the
2271 symbol we are relocating against, plus any addend. */
2274 if (!howto->partial_inplace)
2276 /* This is a partial relocation, and we want to apply the relocation
2277 to the reloc entry rather than the raw data. Everything except
2278 relocations against section symbols has already been handled
2281 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
2282 reloc_entry->addend = relocation;
2283 reloc_entry->address += input_section->output_offset;
2284 return bfd_reloc_ok;
2288 reloc_entry->address += input_section->output_offset;
2289 reloc_entry->addend = 0;
2293 is_weak_undef = (bfd_is_und_section (symbol->section)
2294 && (symbol->flags & BSF_WEAK) != 0);
2295 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
2296 (bfd_byte *) data, (bfd_vma) octets,
2297 is_weak_undef, error_message);
2299 if (flag == bfd_reloc_dangerous)
2301 /* Add the symbol name to the error message. */
2302 if (! *error_message)
2303 *error_message = "";
2304 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
2305 strlen (symbol->name) + 17,
2307 (unsigned long) reloc_entry->addend);
2314 /* Set up an entry in the procedure linkage table. */
2317 elf_xtensa_create_plt_entry (struct bfd_link_info *info,
2319 unsigned reloc_index)
2321 asection *splt, *sgotplt;
2322 bfd_vma plt_base, got_base;
2323 bfd_vma code_offset, lit_offset, abi_offset;
2326 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
2327 splt = elf_xtensa_get_plt_section (info, chunk);
2328 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
2329 BFD_ASSERT (splt != NULL && sgotplt != NULL);
2331 plt_base = splt->output_section->vma + splt->output_offset;
2332 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2334 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
2335 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
2337 /* Fill in the literal entry. This is the offset of the dynamic
2338 relocation entry. */
2339 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
2340 sgotplt->contents + lit_offset);
2342 /* Fill in the entry in the procedure linkage table. */
2343 memcpy (splt->contents + code_offset,
2344 (bfd_big_endian (output_bfd)
2345 ? elf_xtensa_be_plt_entry
2346 : elf_xtensa_le_plt_entry),
2348 abi_offset = XSHAL_ABI == XTHAL_ABI_WINDOWED ? 3 : 0;
2349 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
2350 plt_base + code_offset + abi_offset),
2351 splt->contents + code_offset + abi_offset + 1);
2352 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
2353 plt_base + code_offset + abi_offset + 3),
2354 splt->contents + code_offset + abi_offset + 4);
2355 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
2356 plt_base + code_offset + abi_offset + 6),
2357 splt->contents + code_offset + abi_offset + 7);
2359 return plt_base + code_offset;
2363 static bfd_boolean get_indirect_call_dest_reg (xtensa_opcode, unsigned *);
2366 replace_tls_insn (Elf_Internal_Rela *rel,
2368 asection *input_section,
2370 bfd_boolean is_ld_model,
2371 char **error_message)
2373 static xtensa_insnbuf ibuff = NULL;
2374 static xtensa_insnbuf sbuff = NULL;
2375 xtensa_isa isa = xtensa_default_isa;
2377 xtensa_opcode old_op, new_op;
2378 bfd_size_type input_size;
2380 unsigned dest_reg, src_reg;
2384 ibuff = xtensa_insnbuf_alloc (isa);
2385 sbuff = xtensa_insnbuf_alloc (isa);
2388 input_size = bfd_get_section_limit (abfd, input_section);
2390 /* Read the instruction into a buffer and decode the opcode. */
2391 xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
2392 input_size - rel->r_offset);
2393 fmt = xtensa_format_decode (isa, ibuff);
2394 if (fmt == XTENSA_UNDEFINED)
2396 *error_message = "cannot decode instruction format";
2400 BFD_ASSERT (xtensa_format_num_slots (isa, fmt) == 1);
2401 xtensa_format_get_slot (isa, fmt, 0, ibuff, sbuff);
2403 old_op = xtensa_opcode_decode (isa, fmt, 0, sbuff);
2404 if (old_op == XTENSA_UNDEFINED)
2406 *error_message = "cannot decode instruction opcode";
2410 r_type = ELF32_R_TYPE (rel->r_info);
2413 case R_XTENSA_TLS_FUNC:
2414 case R_XTENSA_TLS_ARG:
2415 if (old_op != get_l32r_opcode ()
2416 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2417 sbuff, &dest_reg) != 0)
2419 *error_message = "cannot extract L32R destination for TLS access";
2424 case R_XTENSA_TLS_CALL:
2425 if (! get_indirect_call_dest_reg (old_op, &dest_reg)
2426 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2427 sbuff, &src_reg) != 0)
2429 *error_message = "cannot extract CALLXn operands for TLS access";
2442 case R_XTENSA_TLS_FUNC:
2443 case R_XTENSA_TLS_ARG:
2444 /* Change the instruction to a NOP (or "OR a1, a1, a1" for older
2445 versions of Xtensa). */
2446 new_op = xtensa_opcode_lookup (isa, "nop");
2447 if (new_op == XTENSA_UNDEFINED)
2449 new_op = xtensa_opcode_lookup (isa, "or");
2450 if (new_op == XTENSA_UNDEFINED
2451 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2452 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2454 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2456 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2459 *error_message = "cannot encode OR for TLS access";
2465 if (xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0)
2467 *error_message = "cannot encode NOP for TLS access";
2473 case R_XTENSA_TLS_CALL:
2474 /* Read THREADPTR into the CALLX's return value register. */
2475 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2476 if (new_op == XTENSA_UNDEFINED
2477 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2478 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2479 sbuff, dest_reg + 2) != 0)
2481 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2491 case R_XTENSA_TLS_FUNC:
2492 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2493 if (new_op == XTENSA_UNDEFINED
2494 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2495 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2496 sbuff, dest_reg) != 0)
2498 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2503 case R_XTENSA_TLS_ARG:
2504 /* Nothing to do. Keep the original L32R instruction. */
2507 case R_XTENSA_TLS_CALL:
2508 /* Add the CALLX's src register (holding the THREADPTR value)
2509 to the first argument register (holding the offset) and put
2510 the result in the CALLX's return value register. */
2511 new_op = xtensa_opcode_lookup (isa, "add");
2512 if (new_op == XTENSA_UNDEFINED
2513 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2514 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2515 sbuff, dest_reg + 2) != 0
2516 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2517 sbuff, dest_reg + 2) != 0
2518 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2519 sbuff, src_reg) != 0)
2521 *error_message = "cannot encode ADD for TLS access";
2528 xtensa_format_set_slot (isa, fmt, 0, ibuff, sbuff);
2529 xtensa_insnbuf_to_chars (isa, ibuff, contents + rel->r_offset,
2530 input_size - rel->r_offset);
2536 #define IS_XTENSA_TLS_RELOC(R_TYPE) \
2537 ((R_TYPE) == R_XTENSA_TLSDESC_FN \
2538 || (R_TYPE) == R_XTENSA_TLSDESC_ARG \
2539 || (R_TYPE) == R_XTENSA_TLS_DTPOFF \
2540 || (R_TYPE) == R_XTENSA_TLS_TPOFF \
2541 || (R_TYPE) == R_XTENSA_TLS_FUNC \
2542 || (R_TYPE) == R_XTENSA_TLS_ARG \
2543 || (R_TYPE) == R_XTENSA_TLS_CALL)
2545 /* Relocate an Xtensa ELF section. This is invoked by the linker for
2546 both relocatable and final links. */
2549 elf_xtensa_relocate_section (bfd *output_bfd,
2550 struct bfd_link_info *info,
2552 asection *input_section,
2554 Elf_Internal_Rela *relocs,
2555 Elf_Internal_Sym *local_syms,
2556 asection **local_sections)
2558 struct elf_xtensa_link_hash_table *htab;
2559 Elf_Internal_Shdr *symtab_hdr;
2560 Elf_Internal_Rela *rel;
2561 Elf_Internal_Rela *relend;
2562 struct elf_link_hash_entry **sym_hashes;
2563 property_table_entry *lit_table = 0;
2565 char *local_got_tls_types;
2566 char *error_message = NULL;
2567 bfd_size_type input_size;
2570 if (!xtensa_default_isa)
2571 xtensa_default_isa = xtensa_isa_init (0, 0);
2573 BFD_ASSERT (is_xtensa_elf (input_bfd));
2575 htab = elf_xtensa_hash_table (info);
2579 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2580 sym_hashes = elf_sym_hashes (input_bfd);
2581 local_got_tls_types = elf_xtensa_local_got_tls_type (input_bfd);
2583 if (elf_hash_table (info)->dynamic_sections_created)
2585 ltblsize = xtensa_read_table_entries (input_bfd, input_section,
2586 &lit_table, XTENSA_LIT_SEC_NAME,
2592 input_size = bfd_get_section_limit (input_bfd, input_section);
2595 relend = relocs + input_section->reloc_count;
2596 for (; rel < relend; rel++)
2599 reloc_howto_type *howto;
2600 unsigned long r_symndx;
2601 struct elf_link_hash_entry *h;
2602 Elf_Internal_Sym *sym;
2607 bfd_reloc_status_type r;
2608 bfd_boolean is_weak_undef;
2609 bfd_boolean unresolved_reloc;
2611 bfd_boolean dynamic_symbol;
2613 r_type = ELF32_R_TYPE (rel->r_info);
2614 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
2615 || r_type == (int) R_XTENSA_GNU_VTENTRY)
2618 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
2620 bfd_set_error (bfd_error_bad_value);
2623 howto = &elf_howto_table[r_type];
2625 r_symndx = ELF32_R_SYM (rel->r_info);
2630 is_weak_undef = FALSE;
2631 unresolved_reloc = FALSE;
2634 if (howto->partial_inplace && !bfd_link_relocatable (info))
2636 /* Because R_XTENSA_32 was made partial_inplace to fix some
2637 problems with DWARF info in partial links, there may be
2638 an addend stored in the contents. Take it out of there
2639 and move it back into the addend field of the reloc. */
2640 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
2641 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
2644 if (r_symndx < symtab_hdr->sh_info)
2646 sym = local_syms + r_symndx;
2647 sym_type = ELF32_ST_TYPE (sym->st_info);
2648 sec = local_sections[r_symndx];
2649 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2653 bfd_boolean ignored;
2655 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2656 r_symndx, symtab_hdr, sym_hashes,
2658 unresolved_reloc, warned, ignored);
2661 && !unresolved_reloc
2662 && h->root.type == bfd_link_hash_undefweak)
2663 is_weak_undef = TRUE;
2668 if (sec != NULL && discarded_section (sec))
2669 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2670 rel, 1, relend, howto, 0, contents);
2672 if (bfd_link_relocatable (info))
2675 asection * sym_sec = get_elf_r_symndx_section (input_bfd, r_symndx);
2677 /* This is a relocatable link.
2678 1) If the reloc is against a section symbol, adjust
2679 according to the output section.
2680 2) If there is a new target for this relocation,
2681 the new target will be in the same output section.
2682 We adjust the relocation by the output section
2685 if (relaxing_section)
2687 /* Check if this references a section in another input file. */
2688 if (!do_fix_for_relocatable_link (rel, input_bfd, input_section,
2693 dest_addr = sym_sec->output_section->vma + sym_sec->output_offset
2694 + get_elf_r_symndx_offset (input_bfd, r_symndx) + rel->r_addend;
2696 if (r_type == R_XTENSA_ASM_SIMPLIFY)
2698 error_message = NULL;
2699 /* Convert ASM_SIMPLIFY into the simpler relocation
2700 so that they never escape a relaxing link. */
2701 r = contract_asm_expansion (contents, input_size, rel,
2703 if (r != bfd_reloc_ok)
2704 (*info->callbacks->reloc_dangerous)
2705 (info, error_message,
2706 input_bfd, input_section, rel->r_offset);
2708 r_type = ELF32_R_TYPE (rel->r_info);
2711 /* This is a relocatable link, so we don't have to change
2712 anything unless the reloc is against a section symbol,
2713 in which case we have to adjust according to where the
2714 section symbol winds up in the output section. */
2715 if (r_symndx < symtab_hdr->sh_info)
2717 sym = local_syms + r_symndx;
2718 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2720 sec = local_sections[r_symndx];
2721 rel->r_addend += sec->output_offset + sym->st_value;
2725 /* If there is an addend with a partial_inplace howto,
2726 then move the addend to the contents. This is a hack
2727 to work around problems with DWARF in relocatable links
2728 with some previous version of BFD. Now we can't easily get
2729 rid of the hack without breaking backward compatibility.... */
2731 howto = &elf_howto_table[r_type];
2732 if (howto->partial_inplace && rel->r_addend)
2734 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2735 rel->r_addend, contents,
2736 rel->r_offset, FALSE,
2742 /* Put the correct bits in the target instruction, even
2743 though the relocation will still be present in the output
2744 file. This makes disassembly clearer, as well as
2745 allowing loadable kernel modules to work without needing
2746 relocations on anything other than calls and l32r's. */
2748 /* If it is not in the same section, there is nothing we can do. */
2749 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP &&
2750 sym_sec->output_section == input_section->output_section)
2752 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2753 dest_addr, contents,
2754 rel->r_offset, FALSE,
2758 if (r != bfd_reloc_ok)
2759 (*info->callbacks->reloc_dangerous)
2760 (info, error_message,
2761 input_bfd, input_section, rel->r_offset);
2763 /* Done with work for relocatable link; continue with next reloc. */
2767 /* This is a final link. */
2769 if (relaxing_section)
2771 /* Check if this references a section in another input file. */
2772 do_fix_for_final_link (rel, input_bfd, input_section, contents,
2776 /* Sanity check the address. */
2777 if (rel->r_offset >= input_size
2778 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2780 (*_bfd_error_handler)
2781 (_("%B(%A+0x%lx): relocation offset out of range (size=0x%x)"),
2782 input_bfd, input_section, rel->r_offset, input_size);
2783 bfd_set_error (bfd_error_bad_value);
2788 name = h->root.root.string;
2791 name = (bfd_elf_string_from_elf_section
2792 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2793 if (name == NULL || *name == '\0')
2794 name = bfd_section_name (input_bfd, sec);
2797 if (r_symndx != STN_UNDEF
2798 && r_type != R_XTENSA_NONE
2800 || h->root.type == bfd_link_hash_defined
2801 || h->root.type == bfd_link_hash_defweak)
2802 && IS_XTENSA_TLS_RELOC (r_type) != (sym_type == STT_TLS))
2804 (*_bfd_error_handler)
2805 ((sym_type == STT_TLS
2806 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
2807 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
2810 (long) rel->r_offset,
2815 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
2817 tls_type = GOT_UNKNOWN;
2819 tls_type = elf_xtensa_hash_entry (h)->tls_type;
2820 else if (local_got_tls_types)
2821 tls_type = local_got_tls_types [r_symndx];
2827 if (elf_hash_table (info)->dynamic_sections_created
2828 && (input_section->flags & SEC_ALLOC) != 0
2829 && (dynamic_symbol || bfd_link_pic (info)))
2831 Elf_Internal_Rela outrel;
2835 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2836 srel = htab->srelplt;
2838 srel = htab->srelgot;
2840 BFD_ASSERT (srel != NULL);
2843 _bfd_elf_section_offset (output_bfd, info,
2844 input_section, rel->r_offset);
2846 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2847 memset (&outrel, 0, sizeof outrel);
2850 outrel.r_offset += (input_section->output_section->vma
2851 + input_section->output_offset);
2853 /* Complain if the relocation is in a read-only section
2854 and not in a literal pool. */
2855 if ((input_section->flags & SEC_READONLY) != 0
2856 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
2860 _("dynamic relocation in read-only section");
2861 (*info->callbacks->reloc_dangerous)
2862 (info, error_message,
2863 input_bfd, input_section, rel->r_offset);
2868 outrel.r_addend = rel->r_addend;
2871 if (r_type == R_XTENSA_32)
2874 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2877 else /* r_type == R_XTENSA_PLT */
2880 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2882 /* Create the PLT entry and set the initial
2883 contents of the literal entry to the address of
2886 elf_xtensa_create_plt_entry (info, output_bfd,
2889 unresolved_reloc = FALSE;
2893 /* Generate a RELATIVE relocation. */
2894 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2895 outrel.r_addend = 0;
2899 loc = (srel->contents
2900 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2901 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2902 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2905 else if (r_type == R_XTENSA_ASM_EXPAND && dynamic_symbol)
2907 /* This should only happen for non-PIC code, which is not
2908 supposed to be used on systems with dynamic linking.
2909 Just ignore these relocations. */
2914 case R_XTENSA_TLS_TPOFF:
2915 /* Switch to LE model for local symbols in an executable. */
2916 if (! bfd_link_pic (info) && ! dynamic_symbol)
2918 relocation = tpoff (info, relocation);
2923 case R_XTENSA_TLSDESC_FN:
2924 case R_XTENSA_TLSDESC_ARG:
2926 if (r_type == R_XTENSA_TLSDESC_FN)
2928 if (! bfd_link_pic (info) || (tls_type & GOT_TLS_IE) != 0)
2929 r_type = R_XTENSA_NONE;
2931 else if (r_type == R_XTENSA_TLSDESC_ARG)
2933 if (bfd_link_pic (info))
2935 if ((tls_type & GOT_TLS_IE) != 0)
2936 r_type = R_XTENSA_TLS_TPOFF;
2940 r_type = R_XTENSA_TLS_TPOFF;
2941 if (! dynamic_symbol)
2943 relocation = tpoff (info, relocation);
2949 if (r_type == R_XTENSA_NONE)
2950 /* Nothing to do here; skip to the next reloc. */
2953 if (! elf_hash_table (info)->dynamic_sections_created)
2956 _("TLS relocation invalid without dynamic sections");
2957 (*info->callbacks->reloc_dangerous)
2958 (info, error_message,
2959 input_bfd, input_section, rel->r_offset);
2963 Elf_Internal_Rela outrel;
2965 asection *srel = htab->srelgot;
2968 outrel.r_offset = (input_section->output_section->vma
2969 + input_section->output_offset
2972 /* Complain if the relocation is in a read-only section
2973 and not in a literal pool. */
2974 if ((input_section->flags & SEC_READONLY) != 0
2975 && ! elf_xtensa_in_literal_pool (lit_table, ltblsize,
2979 _("dynamic relocation in read-only section");
2980 (*info->callbacks->reloc_dangerous)
2981 (info, error_message,
2982 input_bfd, input_section, rel->r_offset);
2985 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2987 outrel.r_addend = relocation - dtpoff_base (info);
2989 outrel.r_addend = 0;
2992 outrel.r_info = ELF32_R_INFO (indx, r_type);
2994 unresolved_reloc = FALSE;
2997 loc = (srel->contents
2998 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2999 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3000 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
3006 case R_XTENSA_TLS_DTPOFF:
3007 if (! bfd_link_pic (info))
3008 /* Switch from LD model to LE model. */
3009 relocation = tpoff (info, relocation);
3011 relocation -= dtpoff_base (info);
3014 case R_XTENSA_TLS_FUNC:
3015 case R_XTENSA_TLS_ARG:
3016 case R_XTENSA_TLS_CALL:
3017 /* Check if optimizing to IE or LE model. */
3018 if ((tls_type & GOT_TLS_IE) != 0)
3020 bfd_boolean is_ld_model =
3021 (h && elf_xtensa_hash_entry (h) == htab->tlsbase);
3022 if (! replace_tls_insn (rel, input_bfd, input_section, contents,
3023 is_ld_model, &error_message))
3024 (*info->callbacks->reloc_dangerous)
3025 (info, error_message,
3026 input_bfd, input_section, rel->r_offset);
3028 if (r_type != R_XTENSA_TLS_ARG || is_ld_model)
3030 /* Skip subsequent relocations on the same instruction. */
3031 while (rel + 1 < relend && rel[1].r_offset == rel->r_offset)
3038 if (elf_hash_table (info)->dynamic_sections_created
3039 && dynamic_symbol && (is_operand_relocation (r_type)
3040 || r_type == R_XTENSA_32_PCREL))
3043 vsprint_msg ("invalid relocation for dynamic symbol", ": %s",
3044 strlen (name) + 2, name);
3045 (*info->callbacks->reloc_dangerous)
3046 (info, error_message, input_bfd, input_section, rel->r_offset);
3052 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3053 because such sections are not SEC_ALLOC and thus ld.so will
3054 not process them. */
3055 if (unresolved_reloc
3056 && !((input_section->flags & SEC_DEBUGGING) != 0
3058 && _bfd_elf_section_offset (output_bfd, info, input_section,
3059 rel->r_offset) != (bfd_vma) -1)
3061 (*_bfd_error_handler)
3062 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3065 (long) rel->r_offset,
3071 /* TLS optimizations may have changed r_type; update "howto". */
3072 howto = &elf_howto_table[r_type];
3074 /* There's no point in calling bfd_perform_relocation here.
3075 Just go directly to our "special function". */
3076 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
3077 relocation + rel->r_addend,
3078 contents, rel->r_offset, is_weak_undef,
3081 if (r != bfd_reloc_ok && !warned)
3083 BFD_ASSERT (r == bfd_reloc_dangerous || r == bfd_reloc_other);
3084 BFD_ASSERT (error_message != NULL);
3086 if (rel->r_addend == 0)
3087 error_message = vsprint_msg (error_message, ": %s",
3088 strlen (name) + 2, name);
3090 error_message = vsprint_msg (error_message, ": (%s+0x%x)",
3092 name, (int) rel->r_addend);
3094 (*info->callbacks->reloc_dangerous)
3095 (info, error_message, input_bfd, input_section, rel->r_offset);
3102 input_section->reloc_done = TRUE;
3108 /* Finish up dynamic symbol handling. There's not much to do here since
3109 the PLT and GOT entries are all set up by relocate_section. */
3112 elf_xtensa_finish_dynamic_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
3113 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3114 struct elf_link_hash_entry *h,
3115 Elf_Internal_Sym *sym)
3117 if (h->needs_plt && !h->def_regular)
3119 /* Mark the symbol as undefined, rather than as defined in
3120 the .plt section. Leave the value alone. */
3121 sym->st_shndx = SHN_UNDEF;
3122 /* If the symbol is weak, we do need to clear the value.
3123 Otherwise, the PLT entry would provide a definition for
3124 the symbol even if the symbol wasn't defined anywhere,
3125 and so the symbol would never be NULL. */
3126 if (!h->ref_regular_nonweak)
3130 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3131 if (h == elf_hash_table (info)->hdynamic
3132 || h == elf_hash_table (info)->hgot)
3133 sym->st_shndx = SHN_ABS;
3139 /* Combine adjacent literal table entries in the output. Adjacent
3140 entries within each input section may have been removed during
3141 relaxation, but we repeat the process here, even though it's too late
3142 to shrink the output section, because it's important to minimize the
3143 number of literal table entries to reduce the start-up work for the
3144 runtime linker. Returns the number of remaining table entries or -1
3148 elf_xtensa_combine_prop_entries (bfd *output_bfd,
3153 property_table_entry *table;
3154 bfd_size_type section_size, sgotloc_size;
3158 section_size = sxtlit->size;
3159 BFD_ASSERT (section_size % 8 == 0);
3160 num = section_size / 8;
3162 sgotloc_size = sgotloc->size;
3163 if (sgotloc_size != section_size)
3165 (*_bfd_error_handler)
3166 (_("internal inconsistency in size of .got.loc section"));
3170 table = bfd_malloc (num * sizeof (property_table_entry));
3174 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
3175 propagates to the output section, where it doesn't really apply and
3176 where it breaks the following call to bfd_malloc_and_get_section. */
3177 sxtlit->flags &= ~SEC_IN_MEMORY;
3179 if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
3187 /* There should never be any relocations left at this point, so this
3188 is quite a bit easier than what is done during relaxation. */
3190 /* Copy the raw contents into a property table array and sort it. */
3192 for (n = 0; n < num; n++)
3194 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
3195 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
3198 qsort (table, num, sizeof (property_table_entry), property_table_compare);
3200 for (n = 0; n < num; n++)
3202 bfd_boolean remove_entry = FALSE;
3204 if (table[n].size == 0)
3205 remove_entry = TRUE;
3207 && (table[n-1].address + table[n-1].size == table[n].address))
3209 table[n-1].size += table[n].size;
3210 remove_entry = TRUE;
3215 for (m = n; m < num - 1; m++)
3217 table[m].address = table[m+1].address;
3218 table[m].size = table[m+1].size;
3226 /* Copy the data back to the raw contents. */
3228 for (n = 0; n < num; n++)
3230 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
3231 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
3235 /* Clear the removed bytes. */
3236 if ((bfd_size_type) (num * 8) < section_size)
3237 memset (&contents[num * 8], 0, section_size - num * 8);
3239 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
3243 /* Copy the contents to ".got.loc". */
3244 memcpy (sgotloc->contents, contents, section_size);
3252 /* Finish up the dynamic sections. */
3255 elf_xtensa_finish_dynamic_sections (bfd *output_bfd,
3256 struct bfd_link_info *info)
3258 struct elf_xtensa_link_hash_table *htab;
3260 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
3261 Elf32_External_Dyn *dyncon, *dynconend;
3262 int num_xtlit_entries = 0;
3264 if (! elf_hash_table (info)->dynamic_sections_created)
3267 htab = elf_xtensa_hash_table (info);
3271 dynobj = elf_hash_table (info)->dynobj;
3272 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3273 BFD_ASSERT (sdyn != NULL);
3275 /* Set the first entry in the global offset table to the address of
3276 the dynamic section. */
3280 BFD_ASSERT (sgot->size == 4);
3282 bfd_put_32 (output_bfd, 0, sgot->contents);
3284 bfd_put_32 (output_bfd,
3285 sdyn->output_section->vma + sdyn->output_offset,
3289 srelplt = htab->srelplt;
3290 if (srelplt && srelplt->size != 0)
3292 asection *sgotplt, *srelgot, *spltlittbl;
3293 int chunk, plt_chunks, plt_entries;
3294 Elf_Internal_Rela irela;
3296 unsigned rtld_reloc;
3298 srelgot = htab->srelgot;
3299 spltlittbl = htab->spltlittbl;
3300 BFD_ASSERT (srelgot != NULL && spltlittbl != NULL);
3302 /* Find the first XTENSA_RTLD relocation. Presumably the rest
3303 of them follow immediately after.... */
3304 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
3306 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3307 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3308 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
3311 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
3313 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
3315 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
3317 for (chunk = 0; chunk < plt_chunks; chunk++)
3319 int chunk_entries = 0;
3321 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
3322 BFD_ASSERT (sgotplt != NULL);
3324 /* Emit special RTLD relocations for the first two entries in
3325 each chunk of the .got.plt section. */
3327 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3328 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3329 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3330 irela.r_offset = (sgotplt->output_section->vma
3331 + sgotplt->output_offset);
3332 irela.r_addend = 1; /* tell rtld to set value to resolver function */
3333 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3335 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3337 /* Next literal immediately follows the first. */
3338 loc += sizeof (Elf32_External_Rela);
3339 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3340 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3341 irela.r_offset = (sgotplt->output_section->vma
3342 + sgotplt->output_offset + 4);
3343 /* Tell rtld to set value to object's link map. */
3345 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3347 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3349 /* Fill in the literal table. */
3350 if (chunk < plt_chunks - 1)
3351 chunk_entries = PLT_ENTRIES_PER_CHUNK;
3353 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
3355 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->size);
3356 bfd_put_32 (output_bfd,
3357 sgotplt->output_section->vma + sgotplt->output_offset,
3358 spltlittbl->contents + (chunk * 8) + 0);
3359 bfd_put_32 (output_bfd,
3360 8 + (chunk_entries * 4),
3361 spltlittbl->contents + (chunk * 8) + 4);
3364 /* All the dynamic relocations have been emitted at this point.
3365 Make sure the relocation sections are the correct size. */
3366 if (srelgot->size != (sizeof (Elf32_External_Rela)
3367 * srelgot->reloc_count)
3368 || srelplt->size != (sizeof (Elf32_External_Rela)
3369 * srelplt->reloc_count))
3372 /* The .xt.lit.plt section has just been modified. This must
3373 happen before the code below which combines adjacent literal
3374 table entries, and the .xt.lit.plt contents have to be forced to
3376 if (! bfd_set_section_contents (output_bfd,
3377 spltlittbl->output_section,
3378 spltlittbl->contents,
3379 spltlittbl->output_offset,
3382 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
3383 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
3386 /* Combine adjacent literal table entries. */
3387 BFD_ASSERT (! bfd_link_relocatable (info));
3388 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
3389 sgotloc = htab->sgotloc;
3390 BFD_ASSERT (sgotloc);
3394 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
3395 if (num_xtlit_entries < 0)
3399 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3400 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3401 for (; dyncon < dynconend; dyncon++)
3403 Elf_Internal_Dyn dyn;
3405 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3412 case DT_XTENSA_GOT_LOC_SZ:
3413 dyn.d_un.d_val = num_xtlit_entries;
3416 case DT_XTENSA_GOT_LOC_OFF:
3417 dyn.d_un.d_ptr = (htab->sgotloc->output_section->vma
3418 + htab->sgotloc->output_offset);
3422 dyn.d_un.d_ptr = (htab->sgot->output_section->vma
3423 + htab->sgot->output_offset);
3427 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
3428 + htab->srelplt->output_offset);
3432 dyn.d_un.d_val = htab->srelplt->size;
3436 /* Adjust RELASZ to not include JMPREL. This matches what
3437 glibc expects and what is done for several other ELF
3438 targets (e.g., i386, alpha), but the "correct" behavior
3439 seems to be unresolved. Since the linker script arranges
3440 for .rela.plt to follow all other relocation sections, we
3441 don't have to worry about changing the DT_RELA entry. */
3443 dyn.d_un.d_val -= htab->srelplt->size;
3447 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3454 /* Functions for dealing with the e_flags field. */
3456 /* Merge backend specific data from an object file to the output
3457 object file when linking. */
3460 elf_xtensa_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3462 unsigned out_mach, in_mach;
3463 flagword out_flag, in_flag;
3465 /* Check if we have the same endianness. */
3466 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
3469 /* Don't even pretend to support mixed-format linking. */
3470 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3471 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3474 out_flag = elf_elfheader (obfd)->e_flags;
3475 in_flag = elf_elfheader (ibfd)->e_flags;
3477 out_mach = out_flag & EF_XTENSA_MACH;
3478 in_mach = in_flag & EF_XTENSA_MACH;
3479 if (out_mach != in_mach)
3481 (*_bfd_error_handler)
3482 (_("%B: incompatible machine type. Output is 0x%x. Input is 0x%x"),
3483 ibfd, out_mach, in_mach);
3484 bfd_set_error (bfd_error_wrong_format);
3488 if (! elf_flags_init (obfd))
3490 elf_flags_init (obfd) = TRUE;
3491 elf_elfheader (obfd)->e_flags = in_flag;
3493 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3494 && bfd_get_arch_info (obfd)->the_default)
3495 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3496 bfd_get_mach (ibfd));
3501 if ((out_flag & EF_XTENSA_XT_INSN) != (in_flag & EF_XTENSA_XT_INSN))
3502 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
3504 if ((out_flag & EF_XTENSA_XT_LIT) != (in_flag & EF_XTENSA_XT_LIT))
3505 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
3512 elf_xtensa_set_private_flags (bfd *abfd, flagword flags)
3514 BFD_ASSERT (!elf_flags_init (abfd)
3515 || elf_elfheader (abfd)->e_flags == flags);
3517 elf_elfheader (abfd)->e_flags |= flags;
3518 elf_flags_init (abfd) = TRUE;
3525 elf_xtensa_print_private_bfd_data (bfd *abfd, void *farg)
3527 FILE *f = (FILE *) farg;
3528 flagword e_flags = elf_elfheader (abfd)->e_flags;
3530 fprintf (f, "\nXtensa header:\n");
3531 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
3532 fprintf (f, "\nMachine = Base\n");
3534 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
3536 fprintf (f, "Insn tables = %s\n",
3537 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
3539 fprintf (f, "Literal tables = %s\n",
3540 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
3542 return _bfd_elf_print_private_bfd_data (abfd, farg);
3546 /* Set the right machine number for an Xtensa ELF file. */
3549 elf_xtensa_object_p (bfd *abfd)
3552 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
3557 mach = bfd_mach_xtensa;
3563 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
3568 /* The final processing done just before writing out an Xtensa ELF object
3569 file. This gets the Xtensa architecture right based on the machine
3573 elf_xtensa_final_write_processing (bfd *abfd,
3574 bfd_boolean linker ATTRIBUTE_UNUSED)
3579 switch (mach = bfd_get_mach (abfd))
3581 case bfd_mach_xtensa:
3582 val = E_XTENSA_MACH;
3588 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
3589 elf_elfheader (abfd)->e_flags |= val;
3593 static enum elf_reloc_type_class
3594 elf_xtensa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3595 const asection *rel_sec ATTRIBUTE_UNUSED,
3596 const Elf_Internal_Rela *rela)
3598 switch ((int) ELF32_R_TYPE (rela->r_info))
3600 case R_XTENSA_RELATIVE:
3601 return reloc_class_relative;
3602 case R_XTENSA_JMP_SLOT:
3603 return reloc_class_plt;
3605 return reloc_class_normal;
3611 elf_xtensa_discard_info_for_section (bfd *abfd,
3612 struct elf_reloc_cookie *cookie,
3613 struct bfd_link_info *info,
3617 bfd_vma offset, actual_offset;
3618 bfd_size_type removed_bytes = 0;
3619 bfd_size_type entry_size;
3621 if (sec->output_section
3622 && bfd_is_abs_section (sec->output_section))
3625 if (xtensa_is_proptable_section (sec))
3630 if (sec->size == 0 || sec->size % entry_size != 0)
3633 contents = retrieve_contents (abfd, sec, info->keep_memory);
3637 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
3640 release_contents (sec, contents);
3644 /* Sort the relocations. They should already be in order when
3645 relaxation is enabled, but it might not be. */
3646 qsort (cookie->rels, sec->reloc_count, sizeof (Elf_Internal_Rela),
3647 internal_reloc_compare);
3649 cookie->rel = cookie->rels;
3650 cookie->relend = cookie->rels + sec->reloc_count;
3652 for (offset = 0; offset < sec->size; offset += entry_size)
3654 actual_offset = offset - removed_bytes;
3656 /* The ...symbol_deleted_p function will skip over relocs but it
3657 won't adjust their offsets, so do that here. */
3658 while (cookie->rel < cookie->relend
3659 && cookie->rel->r_offset < offset)
3661 cookie->rel->r_offset -= removed_bytes;
3665 while (cookie->rel < cookie->relend
3666 && cookie->rel->r_offset == offset)
3668 if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
3670 /* Remove the table entry. (If the reloc type is NONE, then
3671 the entry has already been merged with another and deleted
3672 during relaxation.) */
3673 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
3675 /* Shift the contents up. */
3676 if (offset + entry_size < sec->size)
3677 memmove (&contents[actual_offset],
3678 &contents[actual_offset + entry_size],
3679 sec->size - offset - entry_size);
3680 removed_bytes += entry_size;
3683 /* Remove this relocation. */
3684 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
3687 /* Adjust the relocation offset for previous removals. This
3688 should not be done before calling ...symbol_deleted_p
3689 because it might mess up the offset comparisons there.
3690 Make sure the offset doesn't underflow in the case where
3691 the first entry is removed. */
3692 if (cookie->rel->r_offset >= removed_bytes)
3693 cookie->rel->r_offset -= removed_bytes;
3695 cookie->rel->r_offset = 0;
3701 if (removed_bytes != 0)
3703 /* Adjust any remaining relocs (shouldn't be any). */
3704 for (; cookie->rel < cookie->relend; cookie->rel++)
3706 if (cookie->rel->r_offset >= removed_bytes)
3707 cookie->rel->r_offset -= removed_bytes;
3709 cookie->rel->r_offset = 0;
3712 /* Clear the removed bytes. */
3713 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
3715 pin_contents (sec, contents);
3716 pin_internal_relocs (sec, cookie->rels);
3719 if (sec->rawsize == 0)
3720 sec->rawsize = sec->size;
3721 sec->size -= removed_bytes;
3723 if (xtensa_is_littable_section (sec))
3725 asection *sgotloc = elf_xtensa_hash_table (info)->sgotloc;
3727 sgotloc->size -= removed_bytes;
3732 release_contents (sec, contents);
3733 release_internal_relocs (sec, cookie->rels);
3736 return (removed_bytes != 0);
3741 elf_xtensa_discard_info (bfd *abfd,
3742 struct elf_reloc_cookie *cookie,
3743 struct bfd_link_info *info)
3746 bfd_boolean changed = FALSE;
3748 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3750 if (xtensa_is_property_section (sec))
3752 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
3762 elf_xtensa_ignore_discarded_relocs (asection *sec)
3764 return xtensa_is_property_section (sec);
3769 elf_xtensa_action_discarded (asection *sec)
3771 if (strcmp (".xt_except_table", sec->name) == 0)
3774 if (strcmp (".xt_except_desc", sec->name) == 0)
3777 return _bfd_elf_default_action_discarded (sec);
3781 /* Support for core dump NOTE sections. */
3784 elf_xtensa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3789 /* The size for Xtensa is variable, so don't try to recognize the format
3790 based on the size. Just assume this is GNU/Linux. */
3793 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3796 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
3800 size = note->descsz - offset - 4;
3802 /* Make a ".reg/999" section. */
3803 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3804 size, note->descpos + offset);
3809 elf_xtensa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3811 switch (note->descsz)
3816 case 128: /* GNU/Linux elf_prpsinfo */
3817 elf_tdata (abfd)->core->program
3818 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3819 elf_tdata (abfd)->core->command
3820 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3823 /* Note that for some reason, a spurious space is tacked
3824 onto the end of the args in some (at least one anyway)
3825 implementations, so strip it off if it exists. */
3828 char *command = elf_tdata (abfd)->core->command;
3829 int n = strlen (command);
3831 if (0 < n && command[n - 1] == ' ')
3832 command[n - 1] = '\0';
3839 /* Generic Xtensa configurability stuff. */
3841 static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
3842 static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
3843 static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
3844 static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
3845 static xtensa_opcode call0_op = XTENSA_UNDEFINED;
3846 static xtensa_opcode call4_op = XTENSA_UNDEFINED;
3847 static xtensa_opcode call8_op = XTENSA_UNDEFINED;
3848 static xtensa_opcode call12_op = XTENSA_UNDEFINED;
3851 init_call_opcodes (void)
3853 if (callx0_op == XTENSA_UNDEFINED)
3855 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
3856 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
3857 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
3858 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
3859 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
3860 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
3861 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
3862 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
3868 is_indirect_call_opcode (xtensa_opcode opcode)
3870 init_call_opcodes ();
3871 return (opcode == callx0_op
3872 || opcode == callx4_op
3873 || opcode == callx8_op
3874 || opcode == callx12_op);
3879 is_direct_call_opcode (xtensa_opcode opcode)
3881 init_call_opcodes ();
3882 return (opcode == call0_op
3883 || opcode == call4_op
3884 || opcode == call8_op
3885 || opcode == call12_op);
3890 is_windowed_call_opcode (xtensa_opcode opcode)
3892 init_call_opcodes ();
3893 return (opcode == call4_op
3894 || opcode == call8_op
3895 || opcode == call12_op
3896 || opcode == callx4_op
3897 || opcode == callx8_op
3898 || opcode == callx12_op);
3903 get_indirect_call_dest_reg (xtensa_opcode opcode, unsigned *pdst)
3905 unsigned dst = (unsigned) -1;
3907 init_call_opcodes ();
3908 if (opcode == callx0_op)
3910 else if (opcode == callx4_op)
3912 else if (opcode == callx8_op)
3914 else if (opcode == callx12_op)
3917 if (dst == (unsigned) -1)
3925 static xtensa_opcode
3926 get_const16_opcode (void)
3928 static bfd_boolean done_lookup = FALSE;
3929 static xtensa_opcode const16_opcode = XTENSA_UNDEFINED;
3932 const16_opcode = xtensa_opcode_lookup (xtensa_default_isa, "const16");
3935 return const16_opcode;
3939 static xtensa_opcode
3940 get_l32r_opcode (void)
3942 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
3943 static bfd_boolean done_lookup = FALSE;
3947 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
3955 l32r_offset (bfd_vma addr, bfd_vma pc)
3959 offset = addr - ((pc+3) & -4);
3960 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
3961 offset = (signed int) offset >> 2;
3962 BFD_ASSERT ((signed int) offset >> 16 == -1);
3968 get_relocation_opnd (xtensa_opcode opcode, int r_type)
3970 xtensa_isa isa = xtensa_default_isa;
3971 int last_immed, last_opnd, opi;
3973 if (opcode == XTENSA_UNDEFINED)
3974 return XTENSA_UNDEFINED;
3976 /* Find the last visible PC-relative immediate operand for the opcode.
3977 If there are no PC-relative immediates, then choose the last visible
3978 immediate; otherwise, fail and return XTENSA_UNDEFINED. */
3979 last_immed = XTENSA_UNDEFINED;
3980 last_opnd = xtensa_opcode_num_operands (isa, opcode);
3981 for (opi = last_opnd - 1; opi >= 0; opi--)
3983 if (xtensa_operand_is_visible (isa, opcode, opi) == 0)
3985 if (xtensa_operand_is_PCrelative (isa, opcode, opi) == 1)
3990 if (last_immed == XTENSA_UNDEFINED
3991 && xtensa_operand_is_register (isa, opcode, opi) == 0)
3995 return XTENSA_UNDEFINED;
3997 /* If the operand number was specified in an old-style relocation,
3998 check for consistency with the operand computed above. */
3999 if (r_type >= R_XTENSA_OP0 && r_type <= R_XTENSA_OP2)
4001 int reloc_opnd = r_type - R_XTENSA_OP0;
4002 if (reloc_opnd != last_immed)
4003 return XTENSA_UNDEFINED;
4011 get_relocation_slot (int r_type)
4021 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4022 return r_type - R_XTENSA_SLOT0_OP;
4023 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4024 return r_type - R_XTENSA_SLOT0_ALT;
4028 return XTENSA_UNDEFINED;
4032 /* Get the opcode for a relocation. */
4034 static xtensa_opcode
4035 get_relocation_opcode (bfd *abfd,
4038 Elf_Internal_Rela *irel)
4040 static xtensa_insnbuf ibuff = NULL;
4041 static xtensa_insnbuf sbuff = NULL;
4042 xtensa_isa isa = xtensa_default_isa;
4046 if (contents == NULL)
4047 return XTENSA_UNDEFINED;
4049 if (bfd_get_section_limit (abfd, sec) <= irel->r_offset)
4050 return XTENSA_UNDEFINED;
4054 ibuff = xtensa_insnbuf_alloc (isa);
4055 sbuff = xtensa_insnbuf_alloc (isa);
4058 /* Decode the instruction. */
4059 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
4060 sec->size - irel->r_offset);
4061 fmt = xtensa_format_decode (isa, ibuff);
4062 slot = get_relocation_slot (ELF32_R_TYPE (irel->r_info));
4063 if (slot == XTENSA_UNDEFINED)
4064 return XTENSA_UNDEFINED;
4065 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
4066 return xtensa_opcode_decode (isa, fmt, slot, sbuff);
4071 is_l32r_relocation (bfd *abfd,
4074 Elf_Internal_Rela *irel)
4076 xtensa_opcode opcode;
4077 if (!is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
4079 opcode = get_relocation_opcode (abfd, sec, contents, irel);
4080 return (opcode == get_l32r_opcode ());
4084 static bfd_size_type
4085 get_asm_simplify_size (bfd_byte *contents,
4086 bfd_size_type content_len,
4087 bfd_size_type offset)
4089 bfd_size_type insnlen, size = 0;
4091 /* Decode the size of the next two instructions. */
4092 insnlen = insn_decode_len (contents, content_len, offset);
4098 insnlen = insn_decode_len (contents, content_len, offset + size);
4108 is_alt_relocation (int r_type)
4110 return (r_type >= R_XTENSA_SLOT0_ALT
4111 && r_type <= R_XTENSA_SLOT14_ALT);
4116 is_operand_relocation (int r_type)
4126 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4128 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4137 #define MIN_INSN_LENGTH 2
4139 /* Return 0 if it fails to decode. */
4142 insn_decode_len (bfd_byte *contents,
4143 bfd_size_type content_len,
4144 bfd_size_type offset)
4147 xtensa_isa isa = xtensa_default_isa;
4149 static xtensa_insnbuf ibuff = NULL;
4151 if (offset + MIN_INSN_LENGTH > content_len)
4155 ibuff = xtensa_insnbuf_alloc (isa);
4156 xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
4157 content_len - offset);
4158 fmt = xtensa_format_decode (isa, ibuff);
4159 if (fmt == XTENSA_UNDEFINED)
4161 insn_len = xtensa_format_length (isa, fmt);
4162 if (insn_len == XTENSA_UNDEFINED)
4168 /* Decode the opcode for a single slot instruction.
4169 Return 0 if it fails to decode or the instruction is multi-slot. */
4172 insn_decode_opcode (bfd_byte *contents,
4173 bfd_size_type content_len,
4174 bfd_size_type offset,
4177 xtensa_isa isa = xtensa_default_isa;
4179 static xtensa_insnbuf insnbuf = NULL;
4180 static xtensa_insnbuf slotbuf = NULL;
4182 if (offset + MIN_INSN_LENGTH > content_len)
4183 return XTENSA_UNDEFINED;
4185 if (insnbuf == NULL)
4187 insnbuf = xtensa_insnbuf_alloc (isa);
4188 slotbuf = xtensa_insnbuf_alloc (isa);
4191 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4192 content_len - offset);
4193 fmt = xtensa_format_decode (isa, insnbuf);
4194 if (fmt == XTENSA_UNDEFINED)
4195 return XTENSA_UNDEFINED;
4197 if (slot >= xtensa_format_num_slots (isa, fmt))
4198 return XTENSA_UNDEFINED;
4200 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4201 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4205 /* The offset is the offset in the contents.
4206 The address is the address of that offset. */
4209 check_branch_target_aligned (bfd_byte *contents,
4210 bfd_size_type content_length,
4214 bfd_size_type insn_len = insn_decode_len (contents, content_length, offset);
4217 return check_branch_target_aligned_address (address, insn_len);
4222 check_loop_aligned (bfd_byte *contents,
4223 bfd_size_type content_length,
4227 bfd_size_type loop_len, insn_len;
4228 xtensa_opcode opcode;
4230 opcode = insn_decode_opcode (contents, content_length, offset, 0);
4231 if (opcode == XTENSA_UNDEFINED
4232 || xtensa_opcode_is_loop (xtensa_default_isa, opcode) != 1)
4238 loop_len = insn_decode_len (contents, content_length, offset);
4239 insn_len = insn_decode_len (contents, content_length, offset + loop_len);
4240 if (loop_len == 0 || insn_len == 0)
4246 return check_branch_target_aligned_address (address + loop_len, insn_len);
4251 check_branch_target_aligned_address (bfd_vma addr, int len)
4254 return (addr % 8 == 0);
4255 return ((addr >> 2) == ((addr + len - 1) >> 2));
4259 /* Instruction widening and narrowing. */
4261 /* When FLIX is available we need to access certain instructions only
4262 when they are 16-bit or 24-bit instructions. This table caches
4263 information about such instructions by walking through all the
4264 opcodes and finding the smallest single-slot format into which each
4267 static xtensa_format *op_single_fmt_table = NULL;
4271 init_op_single_format_table (void)
4273 xtensa_isa isa = xtensa_default_isa;
4274 xtensa_insnbuf ibuf;
4275 xtensa_opcode opcode;
4279 if (op_single_fmt_table)
4282 ibuf = xtensa_insnbuf_alloc (isa);
4283 num_opcodes = xtensa_isa_num_opcodes (isa);
4285 op_single_fmt_table = (xtensa_format *)
4286 bfd_malloc (sizeof (xtensa_format) * num_opcodes);
4287 for (opcode = 0; opcode < num_opcodes; opcode++)
4289 op_single_fmt_table[opcode] = XTENSA_UNDEFINED;
4290 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
4292 if (xtensa_format_num_slots (isa, fmt) == 1
4293 && xtensa_opcode_encode (isa, fmt, 0, ibuf, opcode) == 0)
4295 xtensa_opcode old_fmt = op_single_fmt_table[opcode];
4296 int fmt_length = xtensa_format_length (isa, fmt);
4297 if (old_fmt == XTENSA_UNDEFINED
4298 || fmt_length < xtensa_format_length (isa, old_fmt))
4299 op_single_fmt_table[opcode] = fmt;
4303 xtensa_insnbuf_free (isa, ibuf);
4307 static xtensa_format
4308 get_single_format (xtensa_opcode opcode)
4310 init_op_single_format_table ();
4311 return op_single_fmt_table[opcode];
4315 /* For the set of narrowable instructions we do NOT include the
4316 narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
4317 involved during linker relaxation that may require these to
4318 re-expand in some conditions. Also, the narrowing "or" -> mov.n
4319 requires special case code to ensure it only works when op1 == op2. */
4327 struct string_pair narrowable[] =
4330 { "addi", "addi.n" },
4331 { "addmi", "addi.n" },
4332 { "l32i", "l32i.n" },
4333 { "movi", "movi.n" },
4335 { "retw", "retw.n" },
4336 { "s32i", "s32i.n" },
4337 { "or", "mov.n" } /* special case only when op1 == op2 */
4340 struct string_pair widenable[] =
4343 { "addi", "addi.n" },
4344 { "addmi", "addi.n" },
4345 { "beqz", "beqz.n" },
4346 { "bnez", "bnez.n" },
4347 { "l32i", "l32i.n" },
4348 { "movi", "movi.n" },
4350 { "retw", "retw.n" },
4351 { "s32i", "s32i.n" },
4352 { "or", "mov.n" } /* special case only when op1 == op2 */
4356 /* Check if an instruction can be "narrowed", i.e., changed from a standard
4357 3-byte instruction to a 2-byte "density" instruction. If it is valid,
4358 return the instruction buffer holding the narrow instruction. Otherwise,
4359 return 0. The set of valid narrowing are specified by a string table
4360 but require some special case operand checks in some cases. */
4362 static xtensa_insnbuf
4363 can_narrow_instruction (xtensa_insnbuf slotbuf,
4365 xtensa_opcode opcode)
4367 xtensa_isa isa = xtensa_default_isa;
4368 xtensa_format o_fmt;
4371 static xtensa_insnbuf o_insnbuf = NULL;
4372 static xtensa_insnbuf o_slotbuf = NULL;
4374 if (o_insnbuf == NULL)
4376 o_insnbuf = xtensa_insnbuf_alloc (isa);
4377 o_slotbuf = xtensa_insnbuf_alloc (isa);
4380 for (opi = 0; opi < (sizeof (narrowable)/sizeof (struct string_pair)); opi++)
4382 bfd_boolean is_or = (strcmp ("or", narrowable[opi].wide) == 0);
4384 if (opcode == xtensa_opcode_lookup (isa, narrowable[opi].wide))
4386 uint32 value, newval;
4387 int i, operand_count, o_operand_count;
4388 xtensa_opcode o_opcode;
4390 /* Address does not matter in this case. We might need to
4391 fix it to handle branches/jumps. */
4392 bfd_vma self_address = 0;
4394 o_opcode = xtensa_opcode_lookup (isa, narrowable[opi].narrow);
4395 if (o_opcode == XTENSA_UNDEFINED)
4397 o_fmt = get_single_format (o_opcode);
4398 if (o_fmt == XTENSA_UNDEFINED)
4401 if (xtensa_format_length (isa, fmt) != 3
4402 || xtensa_format_length (isa, o_fmt) != 2)
4405 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4406 operand_count = xtensa_opcode_num_operands (isa, opcode);
4407 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4409 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4414 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4419 uint32 rawval0, rawval1, rawval2;
4421 if (o_operand_count + 1 != operand_count
4422 || xtensa_operand_get_field (isa, opcode, 0,
4423 fmt, 0, slotbuf, &rawval0) != 0
4424 || xtensa_operand_get_field (isa, opcode, 1,
4425 fmt, 0, slotbuf, &rawval1) != 0
4426 || xtensa_operand_get_field (isa, opcode, 2,
4427 fmt, 0, slotbuf, &rawval2) != 0
4428 || rawval1 != rawval2
4429 || rawval0 == rawval1 /* it is a nop */)
4433 for (i = 0; i < o_operand_count; ++i)
4435 if (xtensa_operand_get_field (isa, opcode, i, fmt, 0,
4437 || xtensa_operand_decode (isa, opcode, i, &value))
4440 /* PC-relative branches need adjustment, but
4441 the PC-rel operand will always have a relocation. */
4443 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4445 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4446 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4451 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4461 /* Attempt to narrow an instruction. If the narrowing is valid, perform
4462 the action in-place directly into the contents and return TRUE. Otherwise,
4463 the return value is FALSE and the contents are not modified. */
4466 narrow_instruction (bfd_byte *contents,
4467 bfd_size_type content_length,
4468 bfd_size_type offset)
4470 xtensa_opcode opcode;
4471 bfd_size_type insn_len;
4472 xtensa_isa isa = xtensa_default_isa;
4474 xtensa_insnbuf o_insnbuf;
4476 static xtensa_insnbuf insnbuf = NULL;
4477 static xtensa_insnbuf slotbuf = NULL;
4479 if (insnbuf == NULL)
4481 insnbuf = xtensa_insnbuf_alloc (isa);
4482 slotbuf = xtensa_insnbuf_alloc (isa);
4485 BFD_ASSERT (offset < content_length);
4487 if (content_length < 2)
4490 /* We will hand-code a few of these for a little while.
4491 These have all been specified in the assembler aleady. */
4492 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4493 content_length - offset);
4494 fmt = xtensa_format_decode (isa, insnbuf);
4495 if (xtensa_format_num_slots (isa, fmt) != 1)
4498 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4501 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4502 if (opcode == XTENSA_UNDEFINED)
4504 insn_len = xtensa_format_length (isa, fmt);
4505 if (insn_len > content_length)
4508 o_insnbuf = can_narrow_instruction (slotbuf, fmt, opcode);
4511 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4512 content_length - offset);
4520 /* Check if an instruction can be "widened", i.e., changed from a 2-byte
4521 "density" instruction to a standard 3-byte instruction. If it is valid,
4522 return the instruction buffer holding the wide instruction. Otherwise,
4523 return 0. The set of valid widenings are specified by a string table
4524 but require some special case operand checks in some cases. */
4526 static xtensa_insnbuf
4527 can_widen_instruction (xtensa_insnbuf slotbuf,
4529 xtensa_opcode opcode)
4531 xtensa_isa isa = xtensa_default_isa;
4532 xtensa_format o_fmt;
4535 static xtensa_insnbuf o_insnbuf = NULL;
4536 static xtensa_insnbuf o_slotbuf = NULL;
4538 if (o_insnbuf == NULL)
4540 o_insnbuf = xtensa_insnbuf_alloc (isa);
4541 o_slotbuf = xtensa_insnbuf_alloc (isa);
4544 for (opi = 0; opi < (sizeof (widenable)/sizeof (struct string_pair)); opi++)
4546 bfd_boolean is_or = (strcmp ("or", widenable[opi].wide) == 0);
4547 bfd_boolean is_branch = (strcmp ("beqz", widenable[opi].wide) == 0
4548 || strcmp ("bnez", widenable[opi].wide) == 0);
4550 if (opcode == xtensa_opcode_lookup (isa, widenable[opi].narrow))
4552 uint32 value, newval;
4553 int i, operand_count, o_operand_count, check_operand_count;
4554 xtensa_opcode o_opcode;
4556 /* Address does not matter in this case. We might need to fix it
4557 to handle branches/jumps. */
4558 bfd_vma self_address = 0;
4560 o_opcode = xtensa_opcode_lookup (isa, widenable[opi].wide);
4561 if (o_opcode == XTENSA_UNDEFINED)
4563 o_fmt = get_single_format (o_opcode);
4564 if (o_fmt == XTENSA_UNDEFINED)
4567 if (xtensa_format_length (isa, fmt) != 2
4568 || xtensa_format_length (isa, o_fmt) != 3)
4571 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4572 operand_count = xtensa_opcode_num_operands (isa, opcode);
4573 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4574 check_operand_count = o_operand_count;
4576 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4581 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4586 uint32 rawval0, rawval1;
4588 if (o_operand_count != operand_count + 1
4589 || xtensa_operand_get_field (isa, opcode, 0,
4590 fmt, 0, slotbuf, &rawval0) != 0
4591 || xtensa_operand_get_field (isa, opcode, 1,
4592 fmt, 0, slotbuf, &rawval1) != 0
4593 || rawval0 == rawval1 /* it is a nop */)
4597 check_operand_count--;
4599 for (i = 0; i < check_operand_count; i++)
4602 if (is_or && i == o_operand_count - 1)
4604 if (xtensa_operand_get_field (isa, opcode, new_i, fmt, 0,
4606 || xtensa_operand_decode (isa, opcode, new_i, &value))
4609 /* PC-relative branches need adjustment, but
4610 the PC-rel operand will always have a relocation. */
4612 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4614 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4615 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4620 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4630 /* Attempt to widen an instruction. If the widening is valid, perform
4631 the action in-place directly into the contents and return TRUE. Otherwise,
4632 the return value is FALSE and the contents are not modified. */
4635 widen_instruction (bfd_byte *contents,
4636 bfd_size_type content_length,
4637 bfd_size_type offset)
4639 xtensa_opcode opcode;
4640 bfd_size_type insn_len;
4641 xtensa_isa isa = xtensa_default_isa;
4643 xtensa_insnbuf o_insnbuf;
4645 static xtensa_insnbuf insnbuf = NULL;
4646 static xtensa_insnbuf slotbuf = NULL;
4648 if (insnbuf == NULL)
4650 insnbuf = xtensa_insnbuf_alloc (isa);
4651 slotbuf = xtensa_insnbuf_alloc (isa);
4654 BFD_ASSERT (offset < content_length);
4656 if (content_length < 2)
4659 /* We will hand-code a few of these for a little while.
4660 These have all been specified in the assembler aleady. */
4661 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4662 content_length - offset);
4663 fmt = xtensa_format_decode (isa, insnbuf);
4664 if (xtensa_format_num_slots (isa, fmt) != 1)
4667 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4670 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4671 if (opcode == XTENSA_UNDEFINED)
4673 insn_len = xtensa_format_length (isa, fmt);
4674 if (insn_len > content_length)
4677 o_insnbuf = can_widen_instruction (slotbuf, fmt, opcode);
4680 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4681 content_length - offset);
4688 /* Code for transforming CALLs at link-time. */
4690 static bfd_reloc_status_type
4691 elf_xtensa_do_asm_simplify (bfd_byte *contents,
4693 bfd_vma content_length,
4694 char **error_message)
4696 static xtensa_insnbuf insnbuf = NULL;
4697 static xtensa_insnbuf slotbuf = NULL;
4698 xtensa_format core_format = XTENSA_UNDEFINED;
4699 xtensa_opcode opcode;
4700 xtensa_opcode direct_call_opcode;
4701 xtensa_isa isa = xtensa_default_isa;
4702 bfd_byte *chbuf = contents + address;
4705 if (insnbuf == NULL)
4707 insnbuf = xtensa_insnbuf_alloc (isa);
4708 slotbuf = xtensa_insnbuf_alloc (isa);
4711 if (content_length < address)
4713 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4714 return bfd_reloc_other;
4717 opcode = get_expanded_call_opcode (chbuf, content_length - address, 0);
4718 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4719 if (direct_call_opcode == XTENSA_UNDEFINED)
4721 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4722 return bfd_reloc_other;
4725 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
4726 core_format = xtensa_format_lookup (isa, "x24");
4727 opcode = xtensa_opcode_lookup (isa, "or");
4728 xtensa_opcode_encode (isa, core_format, 0, slotbuf, opcode);
4729 for (opn = 0; opn < 3; opn++)
4732 xtensa_operand_encode (isa, opcode, opn, ®no);
4733 xtensa_operand_set_field (isa, opcode, opn, core_format, 0,
4736 xtensa_format_encode (isa, core_format, insnbuf);
4737 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4738 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf, content_length - address);
4740 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
4741 xtensa_opcode_encode (isa, core_format, 0, slotbuf, direct_call_opcode);
4742 xtensa_operand_set_field (isa, opcode, 0, core_format, 0, slotbuf, 0);
4744 xtensa_format_encode (isa, core_format, insnbuf);
4745 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4746 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3,
4747 content_length - address - 3);
4749 return bfd_reloc_ok;
4753 static bfd_reloc_status_type
4754 contract_asm_expansion (bfd_byte *contents,
4755 bfd_vma content_length,
4756 Elf_Internal_Rela *irel,
4757 char **error_message)
4759 bfd_reloc_status_type retval =
4760 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length,
4763 if (retval != bfd_reloc_ok)
4764 return bfd_reloc_dangerous;
4766 /* Update the irel->r_offset field so that the right immediate and
4767 the right instruction are modified during the relocation. */
4768 irel->r_offset += 3;
4769 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_SLOT0_OP);
4770 return bfd_reloc_ok;
4774 static xtensa_opcode
4775 swap_callx_for_call_opcode (xtensa_opcode opcode)
4777 init_call_opcodes ();
4779 if (opcode == callx0_op) return call0_op;
4780 if (opcode == callx4_op) return call4_op;
4781 if (opcode == callx8_op) return call8_op;
4782 if (opcode == callx12_op) return call12_op;
4784 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
4785 return XTENSA_UNDEFINED;
4789 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
4790 CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
4791 If not, return XTENSA_UNDEFINED. */
4793 #define L32R_TARGET_REG_OPERAND 0
4794 #define CONST16_TARGET_REG_OPERAND 0
4795 #define CALLN_SOURCE_OPERAND 0
4797 static xtensa_opcode
4798 get_expanded_call_opcode (bfd_byte *buf, int bufsize, bfd_boolean *p_uses_l32r)
4800 static xtensa_insnbuf insnbuf = NULL;
4801 static xtensa_insnbuf slotbuf = NULL;
4803 xtensa_opcode opcode;
4804 xtensa_isa isa = xtensa_default_isa;
4805 uint32 regno, const16_regno, call_regno;
4808 if (insnbuf == NULL)
4810 insnbuf = xtensa_insnbuf_alloc (isa);
4811 slotbuf = xtensa_insnbuf_alloc (isa);
4814 xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize);
4815 fmt = xtensa_format_decode (isa, insnbuf);
4816 if (fmt == XTENSA_UNDEFINED
4817 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4818 return XTENSA_UNDEFINED;
4820 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4821 if (opcode == XTENSA_UNDEFINED)
4822 return XTENSA_UNDEFINED;
4824 if (opcode == get_l32r_opcode ())
4827 *p_uses_l32r = TRUE;
4828 if (xtensa_operand_get_field (isa, opcode, L32R_TARGET_REG_OPERAND,
4829 fmt, 0, slotbuf, ®no)
4830 || xtensa_operand_decode (isa, opcode, L32R_TARGET_REG_OPERAND,
4832 return XTENSA_UNDEFINED;
4834 else if (opcode == get_const16_opcode ())
4837 *p_uses_l32r = FALSE;
4838 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4839 fmt, 0, slotbuf, ®no)
4840 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4842 return XTENSA_UNDEFINED;
4844 /* Check that the next instruction is also CONST16. */
4845 offset += xtensa_format_length (isa, fmt);
4846 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4847 fmt = xtensa_format_decode (isa, insnbuf);
4848 if (fmt == XTENSA_UNDEFINED
4849 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4850 return XTENSA_UNDEFINED;
4851 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4852 if (opcode != get_const16_opcode ())
4853 return XTENSA_UNDEFINED;
4855 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4856 fmt, 0, slotbuf, &const16_regno)
4857 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4859 || const16_regno != regno)
4860 return XTENSA_UNDEFINED;
4863 return XTENSA_UNDEFINED;
4865 /* Next instruction should be an CALLXn with operand 0 == regno. */
4866 offset += xtensa_format_length (isa, fmt);
4867 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4868 fmt = xtensa_format_decode (isa, insnbuf);
4869 if (fmt == XTENSA_UNDEFINED
4870 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4871 return XTENSA_UNDEFINED;
4872 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4873 if (opcode == XTENSA_UNDEFINED
4874 || !is_indirect_call_opcode (opcode))
4875 return XTENSA_UNDEFINED;
4877 if (xtensa_operand_get_field (isa, opcode, CALLN_SOURCE_OPERAND,
4878 fmt, 0, slotbuf, &call_regno)
4879 || xtensa_operand_decode (isa, opcode, CALLN_SOURCE_OPERAND,
4881 return XTENSA_UNDEFINED;
4883 if (call_regno != regno)
4884 return XTENSA_UNDEFINED;
4890 /* Data structures used during relaxation. */
4892 /* r_reloc: relocation values. */
4894 /* Through the relaxation process, we need to keep track of the values
4895 that will result from evaluating relocations. The standard ELF
4896 relocation structure is not sufficient for this purpose because we're
4897 operating on multiple input files at once, so we need to know which
4898 input file a relocation refers to. The r_reloc structure thus
4899 records both the input file (bfd) and ELF relocation.
4901 For efficiency, an r_reloc also contains a "target_offset" field to
4902 cache the target-section-relative offset value that is represented by
4905 The r_reloc also contains a virtual offset that allows multiple
4906 inserted literals to be placed at the same "address" with
4907 different offsets. */
4909 typedef struct r_reloc_struct r_reloc;
4911 struct r_reloc_struct
4914 Elf_Internal_Rela rela;
4915 bfd_vma target_offset;
4916 bfd_vma virtual_offset;
4920 /* The r_reloc structure is included by value in literal_value, but not
4921 every literal_value has an associated relocation -- some are simple
4922 constants. In such cases, we set all the fields in the r_reloc
4923 struct to zero. The r_reloc_is_const function should be used to
4924 detect this case. */
4927 r_reloc_is_const (const r_reloc *r_rel)
4929 return (r_rel->abfd == NULL);
4934 r_reloc_get_target_offset (const r_reloc *r_rel)
4936 bfd_vma target_offset;
4937 unsigned long r_symndx;
4939 BFD_ASSERT (!r_reloc_is_const (r_rel));
4940 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4941 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
4942 return (target_offset + r_rel->rela.r_addend);
4946 static struct elf_link_hash_entry *
4947 r_reloc_get_hash_entry (const r_reloc *r_rel)
4949 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4950 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
4955 r_reloc_get_section (const r_reloc *r_rel)
4957 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4958 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
4963 r_reloc_is_defined (const r_reloc *r_rel)
4969 sec = r_reloc_get_section (r_rel);
4970 if (sec == bfd_abs_section_ptr
4971 || sec == bfd_com_section_ptr
4972 || sec == bfd_und_section_ptr)
4979 r_reloc_init (r_reloc *r_rel,
4981 Elf_Internal_Rela *irel,
4983 bfd_size_type content_length)
4986 reloc_howto_type *howto;
4990 r_rel->rela = *irel;
4992 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
4993 r_rel->virtual_offset = 0;
4994 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
4995 howto = &elf_howto_table[r_type];
4996 if (howto->partial_inplace)
4998 bfd_vma inplace_val;
4999 BFD_ASSERT (r_rel->rela.r_offset < content_length);
5001 inplace_val = bfd_get_32 (abfd, &contents[r_rel->rela.r_offset]);
5002 r_rel->target_offset += inplace_val;
5006 memset (r_rel, 0, sizeof (r_reloc));
5013 print_r_reloc (FILE *fp, const r_reloc *r_rel)
5015 if (r_reloc_is_defined (r_rel))
5017 asection *sec = r_reloc_get_section (r_rel);
5018 fprintf (fp, " %s(%s + ", sec->owner->filename, sec->name);
5020 else if (r_reloc_get_hash_entry (r_rel))
5021 fprintf (fp, " %s + ", r_reloc_get_hash_entry (r_rel)->root.root.string);
5023 fprintf (fp, " ?? + ");
5025 fprintf_vma (fp, r_rel->target_offset);
5026 if (r_rel->virtual_offset)
5028 fprintf (fp, " + ");
5029 fprintf_vma (fp, r_rel->virtual_offset);
5038 /* source_reloc: relocations that reference literals. */
5040 /* To determine whether literals can be coalesced, we need to first
5041 record all the relocations that reference the literals. The
5042 source_reloc structure below is used for this purpose. The
5043 source_reloc entries are kept in a per-literal-section array, sorted
5044 by offset within the literal section (i.e., target offset).
5046 The source_sec and r_rel.rela.r_offset fields identify the source of
5047 the relocation. The r_rel field records the relocation value, i.e.,
5048 the offset of the literal being referenced. The opnd field is needed
5049 to determine the range of the immediate field to which the relocation
5050 applies, so we can determine whether another literal with the same
5051 value is within range. The is_null field is true when the relocation
5052 is being removed (e.g., when an L32R is being removed due to a CALLX
5053 that is converted to a direct CALL). */
5055 typedef struct source_reloc_struct source_reloc;
5057 struct source_reloc_struct
5059 asection *source_sec;
5061 xtensa_opcode opcode;
5063 bfd_boolean is_null;
5064 bfd_boolean is_abs_literal;
5069 init_source_reloc (source_reloc *reloc,
5070 asection *source_sec,
5071 const r_reloc *r_rel,
5072 xtensa_opcode opcode,
5074 bfd_boolean is_abs_literal)
5076 reloc->source_sec = source_sec;
5077 reloc->r_rel = *r_rel;
5078 reloc->opcode = opcode;
5080 reloc->is_null = FALSE;
5081 reloc->is_abs_literal = is_abs_literal;
5085 /* Find the source_reloc for a particular source offset and relocation
5086 type. Note that the array is sorted by _target_ offset, so this is
5087 just a linear search. */
5089 static source_reloc *
5090 find_source_reloc (source_reloc *src_relocs,
5093 Elf_Internal_Rela *irel)
5097 for (i = 0; i < src_count; i++)
5099 if (src_relocs[i].source_sec == sec
5100 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
5101 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
5102 == ELF32_R_TYPE (irel->r_info)))
5103 return &src_relocs[i];
5111 source_reloc_compare (const void *ap, const void *bp)
5113 const source_reloc *a = (const source_reloc *) ap;
5114 const source_reloc *b = (const source_reloc *) bp;
5116 if (a->r_rel.target_offset != b->r_rel.target_offset)
5117 return (a->r_rel.target_offset - b->r_rel.target_offset);
5119 /* We don't need to sort on these criteria for correctness,
5120 but enforcing a more strict ordering prevents unstable qsort
5121 from behaving differently with different implementations.
5122 Without the code below we get correct but different results
5123 on Solaris 2.7 and 2.8. We would like to always produce the
5124 same results no matter the host. */
5126 if ((!a->is_null) - (!b->is_null))
5127 return ((!a->is_null) - (!b->is_null));
5128 return internal_reloc_compare (&a->r_rel.rela, &b->r_rel.rela);
5132 /* Literal values and value hash tables. */
5134 /* Literals with the same value can be coalesced. The literal_value
5135 structure records the value of a literal: the "r_rel" field holds the
5136 information from the relocation on the literal (if there is one) and
5137 the "value" field holds the contents of the literal word itself.
5139 The value_map structure records a literal value along with the
5140 location of a literal holding that value. The value_map hash table
5141 is indexed by the literal value, so that we can quickly check if a
5142 particular literal value has been seen before and is thus a candidate
5145 typedef struct literal_value_struct literal_value;
5146 typedef struct value_map_struct value_map;
5147 typedef struct value_map_hash_table_struct value_map_hash_table;
5149 struct literal_value_struct
5152 unsigned long value;
5153 bfd_boolean is_abs_literal;
5156 struct value_map_struct
5158 literal_value val; /* The literal value. */
5159 r_reloc loc; /* Location of the literal. */
5163 struct value_map_hash_table_struct
5165 unsigned bucket_count;
5166 value_map **buckets;
5168 bfd_boolean has_last_loc;
5174 init_literal_value (literal_value *lit,
5175 const r_reloc *r_rel,
5176 unsigned long value,
5177 bfd_boolean is_abs_literal)
5179 lit->r_rel = *r_rel;
5181 lit->is_abs_literal = is_abs_literal;
5186 literal_value_equal (const literal_value *src1,
5187 const literal_value *src2,
5188 bfd_boolean final_static_link)
5190 struct elf_link_hash_entry *h1, *h2;
5192 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
5195 if (r_reloc_is_const (&src1->r_rel))
5196 return (src1->value == src2->value);
5198 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
5199 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
5202 if (src1->r_rel.target_offset != src2->r_rel.target_offset)
5205 if (src1->r_rel.virtual_offset != src2->r_rel.virtual_offset)
5208 if (src1->value != src2->value)
5211 /* Now check for the same section (if defined) or the same elf_hash
5212 (if undefined or weak). */
5213 h1 = r_reloc_get_hash_entry (&src1->r_rel);
5214 h2 = r_reloc_get_hash_entry (&src2->r_rel);
5215 if (r_reloc_is_defined (&src1->r_rel)
5216 && (final_static_link
5217 || ((!h1 || h1->root.type != bfd_link_hash_defweak)
5218 && (!h2 || h2->root.type != bfd_link_hash_defweak))))
5220 if (r_reloc_get_section (&src1->r_rel)
5221 != r_reloc_get_section (&src2->r_rel))
5226 /* Require that the hash entries (i.e., symbols) be identical. */
5227 if (h1 != h2 || h1 == 0)
5231 if (src1->is_abs_literal != src2->is_abs_literal)
5238 /* Must be power of 2. */
5239 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
5241 static value_map_hash_table *
5242 value_map_hash_table_init (void)
5244 value_map_hash_table *values;
5246 values = (value_map_hash_table *)
5247 bfd_zmalloc (sizeof (value_map_hash_table));
5248 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
5250 values->buckets = (value_map **)
5251 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
5252 if (values->buckets == NULL)
5257 values->has_last_loc = FALSE;
5264 value_map_hash_table_delete (value_map_hash_table *table)
5266 free (table->buckets);
5272 hash_bfd_vma (bfd_vma val)
5274 return (val >> 2) + (val >> 10);
5279 literal_value_hash (const literal_value *src)
5283 hash_val = hash_bfd_vma (src->value);
5284 if (!r_reloc_is_const (&src->r_rel))
5288 hash_val += hash_bfd_vma (src->is_abs_literal * 1000);
5289 hash_val += hash_bfd_vma (src->r_rel.target_offset);
5290 hash_val += hash_bfd_vma (src->r_rel.virtual_offset);
5292 /* Now check for the same section and the same elf_hash. */
5293 if (r_reloc_is_defined (&src->r_rel))
5294 sec_or_hash = r_reloc_get_section (&src->r_rel);
5296 sec_or_hash = r_reloc_get_hash_entry (&src->r_rel);
5297 hash_val += hash_bfd_vma ((bfd_vma) (size_t) sec_or_hash);
5303 /* Check if the specified literal_value has been seen before. */
5306 value_map_get_cached_value (value_map_hash_table *map,
5307 const literal_value *val,
5308 bfd_boolean final_static_link)
5314 idx = literal_value_hash (val);
5315 idx = idx & (map->bucket_count - 1);
5316 bucket = map->buckets[idx];
5317 for (map_e = bucket; map_e; map_e = map_e->next)
5319 if (literal_value_equal (&map_e->val, val, final_static_link))
5326 /* Record a new literal value. It is illegal to call this if VALUE
5327 already has an entry here. */
5330 add_value_map (value_map_hash_table *map,
5331 const literal_value *val,
5333 bfd_boolean final_static_link)
5335 value_map **bucket_p;
5338 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
5341 bfd_set_error (bfd_error_no_memory);
5345 BFD_ASSERT (!value_map_get_cached_value (map, val, final_static_link));
5349 idx = literal_value_hash (val);
5350 idx = idx & (map->bucket_count - 1);
5351 bucket_p = &map->buckets[idx];
5353 val_e->next = *bucket_p;
5356 /* FIXME: Consider resizing the hash table if we get too many entries. */
5362 /* Lists of text actions (ta_) for narrowing, widening, longcall
5363 conversion, space fill, code & literal removal, etc. */
5365 /* The following text actions are generated:
5367 "ta_remove_insn" remove an instruction or instructions
5368 "ta_remove_longcall" convert longcall to call
5369 "ta_convert_longcall" convert longcall to nop/call
5370 "ta_narrow_insn" narrow a wide instruction
5371 "ta_widen" widen a narrow instruction
5372 "ta_fill" add fill or remove fill
5373 removed < 0 is a fill; branches to the fill address will be
5374 changed to address + fill size (e.g., address - removed)
5375 removed >= 0 branches to the fill address will stay unchanged
5376 "ta_remove_literal" remove a literal; this action is
5377 indicated when a literal is removed
5379 "ta_add_literal" insert a new literal; this action is
5380 indicated when a literal has been moved.
5381 It may use a virtual_offset because
5382 multiple literals can be placed at the
5385 For each of these text actions, we also record the number of bytes
5386 removed by performing the text action. In the case of a "ta_widen"
5387 or a "ta_fill" that adds space, the removed_bytes will be negative. */
5389 typedef struct text_action_struct text_action;
5390 typedef struct text_action_list_struct text_action_list;
5391 typedef enum text_action_enum_t text_action_t;
5393 enum text_action_enum_t
5396 ta_remove_insn, /* removed = -size */
5397 ta_remove_longcall, /* removed = -size */
5398 ta_convert_longcall, /* removed = 0 */
5399 ta_narrow_insn, /* removed = -1 */
5400 ta_widen_insn, /* removed = +1 */
5401 ta_fill, /* removed = +size */
5407 /* Structure for a text action record. */
5408 struct text_action_struct
5410 text_action_t action;
5411 asection *sec; /* Optional */
5413 bfd_vma virtual_offset; /* Zero except for adding literals. */
5415 literal_value value; /* Only valid when adding literals. */
5418 struct removal_by_action_entry_struct
5423 int eq_removed_before_fill;
5425 typedef struct removal_by_action_entry_struct removal_by_action_entry;
5427 struct removal_by_action_map_struct
5430 removal_by_action_entry *entry;
5432 typedef struct removal_by_action_map_struct removal_by_action_map;
5435 /* List of all of the actions taken on a text section. */
5436 struct text_action_list_struct
5440 removal_by_action_map map;
5444 static text_action *
5445 find_fill_action (text_action_list *l, asection *sec, bfd_vma offset)
5449 /* It is not necessary to fill at the end of a section. */
5450 if (sec->size == offset)
5456 splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5458 return (text_action *)node->value;
5464 compute_removed_action_diff (const text_action *ta,
5468 int removable_space)
5471 int current_removed = 0;
5474 current_removed = ta->removed_bytes;
5476 BFD_ASSERT (ta == NULL || ta->offset == offset);
5477 BFD_ASSERT (ta == NULL || ta->action == ta_fill);
5479 /* It is not necessary to fill at the end of a section. Clean this up. */
5480 if (sec->size == offset)
5481 new_removed = removable_space - 0;
5485 int added = -removed - current_removed;
5486 /* Ignore multiples of the section alignment. */
5487 added = ((1 << sec->alignment_power) - 1) & added;
5488 new_removed = (-added);
5490 /* Modify for removable. */
5491 space = removable_space - new_removed;
5492 new_removed = (removable_space
5493 - (((1 << sec->alignment_power) - 1) & space));
5495 return (new_removed - current_removed);
5500 adjust_fill_action (text_action *ta, int fill_diff)
5502 ta->removed_bytes += fill_diff;
5507 text_action_compare (splay_tree_key a, splay_tree_key b)
5509 text_action *pa = (text_action *)a;
5510 text_action *pb = (text_action *)b;
5511 static const int action_priority[] =
5515 [ta_convert_longcall] = 2,
5516 [ta_narrow_insn] = 3,
5517 [ta_remove_insn] = 4,
5518 [ta_remove_longcall] = 5,
5519 [ta_remove_literal] = 6,
5520 [ta_widen_insn] = 7,
5521 [ta_add_literal] = 8,
5524 if (pa->offset == pb->offset)
5526 if (pa->action == pb->action)
5528 return action_priority[pa->action] - action_priority[pb->action];
5531 return pa->offset < pb->offset ? -1 : 1;
5534 static text_action *
5535 action_first (text_action_list *action_list)
5537 splay_tree_node node = splay_tree_min (action_list->tree);
5538 return node ? (text_action *)node->value : NULL;
5541 static text_action *
5542 action_next (text_action_list *action_list, text_action *action)
5544 splay_tree_node node = splay_tree_successor (action_list->tree,
5545 (splay_tree_key)action);
5546 return node ? (text_action *)node->value : NULL;
5549 /* Add a modification action to the text. For the case of adding or
5550 removing space, modify any current fill and assume that
5551 "unreachable_space" bytes can be freely contracted. Note that a
5552 negative removed value is a fill. */
5555 text_action_add (text_action_list *l,
5556 text_action_t action,
5564 /* It is not necessary to fill at the end of a section. */
5565 if (action == ta_fill && sec->size == offset)
5568 /* It is not necessary to fill 0 bytes. */
5569 if (action == ta_fill && removed == 0)
5575 if (action == ta_fill)
5577 splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5581 ta = (text_action *)node->value;
5582 ta->removed_bytes += removed;
5587 BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)&a) == NULL);
5589 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5590 ta->action = action;
5592 ta->offset = offset;
5593 ta->removed_bytes = removed;
5594 splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5600 text_action_add_literal (text_action_list *l,
5601 text_action_t action,
5603 const literal_value *value,
5607 asection *sec = r_reloc_get_section (loc);
5608 bfd_vma offset = loc->target_offset;
5609 bfd_vma virtual_offset = loc->virtual_offset;
5611 BFD_ASSERT (action == ta_add_literal);
5613 /* Create a new record and fill it up. */
5614 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5615 ta->action = action;
5617 ta->offset = offset;
5618 ta->virtual_offset = virtual_offset;
5620 ta->removed_bytes = removed;
5622 BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)ta) == NULL);
5623 splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5628 /* Find the total offset adjustment for the relaxations specified by
5629 text_actions, beginning from a particular starting action. This is
5630 typically used from offset_with_removed_text to search an entire list of
5631 actions, but it may also be called directly when adjusting adjacent offsets
5632 so that each search may begin where the previous one left off. */
5635 removed_by_actions (text_action_list *action_list,
5636 text_action **p_start_action,
5638 bfd_boolean before_fill)
5643 r = *p_start_action;
5646 splay_tree_node node = splay_tree_lookup (action_list->tree,
5648 BFD_ASSERT (node != NULL && r == (text_action *)node->value);
5653 if (r->offset > offset)
5656 if (r->offset == offset
5657 && (before_fill || r->action != ta_fill || r->removed_bytes >= 0))
5660 removed += r->removed_bytes;
5662 r = action_next (action_list, r);
5665 *p_start_action = r;
5671 offset_with_removed_text (text_action_list *action_list, bfd_vma offset)
5673 text_action *r = action_first (action_list);
5675 return offset - removed_by_actions (action_list, &r, offset, FALSE);
5680 action_list_count (text_action_list *action_list)
5682 return action_list->count;
5685 typedef struct map_action_fn_context_struct map_action_fn_context;
5686 struct map_action_fn_context_struct
5689 removal_by_action_map map;
5690 bfd_boolean eq_complete;
5694 map_action_fn (splay_tree_node node, void *p)
5696 map_action_fn_context *ctx = p;
5697 text_action *r = (text_action *)node->value;
5698 removal_by_action_entry *ientry = ctx->map.entry + ctx->map.n_entries;
5700 if (ctx->map.n_entries && (ientry - 1)->offset == r->offset)
5706 ++ctx->map.n_entries;
5707 ctx->eq_complete = FALSE;
5708 ientry->offset = r->offset;
5709 ientry->eq_removed_before_fill = ctx->removed;
5712 if (!ctx->eq_complete)
5714 if (r->action != ta_fill || r->removed_bytes >= 0)
5716 ientry->eq_removed = ctx->removed;
5717 ctx->eq_complete = TRUE;
5720 ientry->eq_removed = ctx->removed + r->removed_bytes;
5723 ctx->removed += r->removed_bytes;
5724 ientry->removed = ctx->removed;
5729 map_removal_by_action (text_action_list *action_list)
5731 map_action_fn_context ctx;
5734 ctx.map.n_entries = 0;
5735 ctx.map.entry = bfd_malloc (action_list_count (action_list) *
5736 sizeof (removal_by_action_entry));
5737 ctx.eq_complete = FALSE;
5739 splay_tree_foreach (action_list->tree, map_action_fn, &ctx);
5740 action_list->map = ctx.map;
5744 removed_by_actions_map (text_action_list *action_list, bfd_vma offset,
5745 bfd_boolean before_fill)
5749 if (!action_list->map.entry)
5750 map_removal_by_action (action_list);
5752 if (!action_list->map.n_entries)
5756 b = action_list->map.n_entries;
5760 unsigned c = (a + b) / 2;
5762 if (action_list->map.entry[c].offset <= offset)
5768 if (action_list->map.entry[a].offset < offset)
5770 return action_list->map.entry[a].removed;
5772 else if (action_list->map.entry[a].offset == offset)
5774 return before_fill ?
5775 action_list->map.entry[a].eq_removed_before_fill :
5776 action_list->map.entry[a].eq_removed;
5785 offset_with_removed_text_map (text_action_list *action_list, bfd_vma offset)
5787 int removed = removed_by_actions_map (action_list, offset, FALSE);
5788 return offset - removed;
5792 /* The find_insn_action routine will only find non-fill actions. */
5794 static text_action *
5795 find_insn_action (text_action_list *action_list, bfd_vma offset)
5797 static const text_action_t action[] =
5799 ta_convert_longcall,
5809 for (i = 0; i < sizeof (action) / sizeof (*action); ++i)
5811 splay_tree_node node;
5813 a.action = action[i];
5814 node = splay_tree_lookup (action_list->tree, (splay_tree_key)&a);
5816 return (text_action *)node->value;
5825 print_action (FILE *fp, text_action *r)
5827 const char *t = "unknown";
5830 case ta_remove_insn:
5831 t = "remove_insn"; break;
5832 case ta_remove_longcall:
5833 t = "remove_longcall"; break;
5834 case ta_convert_longcall:
5835 t = "convert_longcall"; break;
5836 case ta_narrow_insn:
5837 t = "narrow_insn"; break;
5839 t = "widen_insn"; break;
5844 case ta_remove_literal:
5845 t = "remove_literal"; break;
5846 case ta_add_literal:
5847 t = "add_literal"; break;
5850 fprintf (fp, "%s: %s[0x%lx] \"%s\" %d\n",
5851 r->sec->owner->filename,
5852 r->sec->name, (unsigned long) r->offset, t, r->removed_bytes);
5856 print_action_list_fn (splay_tree_node node, void *p)
5858 text_action *r = (text_action *)node->value;
5860 print_action (p, r);
5865 print_action_list (FILE *fp, text_action_list *action_list)
5867 fprintf (fp, "Text Action\n");
5868 splay_tree_foreach (action_list->tree, print_action_list_fn, fp);
5874 /* Lists of literals being coalesced or removed. */
5876 /* In the usual case, the literal identified by "from" is being
5877 coalesced with another literal identified by "to". If the literal is
5878 unused and is being removed altogether, "to.abfd" will be NULL.
5879 The removed_literal entries are kept on a per-section list, sorted
5880 by the "from" offset field. */
5882 typedef struct removed_literal_struct removed_literal;
5883 typedef struct removed_literal_map_entry_struct removed_literal_map_entry;
5884 typedef struct removed_literal_list_struct removed_literal_list;
5886 struct removed_literal_struct
5890 removed_literal *next;
5893 struct removed_literal_map_entry_struct
5896 removed_literal *literal;
5899 struct removed_literal_list_struct
5901 removed_literal *head;
5902 removed_literal *tail;
5905 removed_literal_map_entry *map;
5909 /* Record that the literal at "from" is being removed. If "to" is not
5910 NULL, the "from" literal is being coalesced with the "to" literal. */
5913 add_removed_literal (removed_literal_list *removed_list,
5914 const r_reloc *from,
5917 removed_literal *r, *new_r, *next_r;
5919 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
5921 new_r->from = *from;
5925 new_r->to.abfd = NULL;
5928 r = removed_list->head;
5931 removed_list->head = new_r;
5932 removed_list->tail = new_r;
5934 /* Special check for common case of append. */
5935 else if (removed_list->tail->from.target_offset < from->target_offset)
5937 removed_list->tail->next = new_r;
5938 removed_list->tail = new_r;
5942 while (r->from.target_offset < from->target_offset && r->next)
5948 new_r->next = next_r;
5950 removed_list->tail = new_r;
5955 map_removed_literal (removed_literal_list *removed_list)
5959 removed_literal_map_entry *map = NULL;
5960 removed_literal *r = removed_list->head;
5962 for (i = 0; r; ++i, r = r->next)
5966 n_map = (n_map * 2) + 2;
5967 map = bfd_realloc (map, n_map * sizeof (*map));
5969 map[i].addr = r->from.target_offset;
5972 removed_list->map = map;
5973 removed_list->n_map = i;
5977 removed_literal_compare (const void *a, const void *b)
5979 const removed_literal_map_entry *pa = a;
5980 const removed_literal_map_entry *pb = b;
5982 if (pa->addr == pb->addr)
5985 return pa->addr < pb->addr ? -1 : 1;
5988 /* Check if the list of removed literals contains an entry for the
5989 given address. Return the entry if found. */
5991 static removed_literal *
5992 find_removed_literal (removed_literal_list *removed_list, bfd_vma addr)
5994 removed_literal_map_entry *p;
5995 removed_literal *r = NULL;
5997 if (removed_list->map == NULL)
5998 map_removed_literal (removed_list);
6000 p = bsearch (&addr, removed_list->map, removed_list->n_map,
6001 sizeof (*removed_list->map), removed_literal_compare);
6004 while (p != removed_list->map && (p - 1)->addr == addr)
6015 print_removed_literals (FILE *fp, removed_literal_list *removed_list)
6018 r = removed_list->head;
6020 fprintf (fp, "Removed Literals\n");
6021 for (; r != NULL; r = r->next)
6023 print_r_reloc (fp, &r->from);
6024 fprintf (fp, " => ");
6025 if (r->to.abfd == NULL)
6026 fprintf (fp, "REMOVED");
6028 print_r_reloc (fp, &r->to);
6036 /* Per-section data for relaxation. */
6038 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
6040 struct xtensa_relax_info_struct
6042 bfd_boolean is_relaxable_literal_section;
6043 bfd_boolean is_relaxable_asm_section;
6044 int visited; /* Number of times visited. */
6046 source_reloc *src_relocs; /* Array[src_count]. */
6048 int src_next; /* Next src_relocs entry to assign. */
6050 removed_literal_list removed_list;
6051 text_action_list action_list;
6053 reloc_bfd_fix *fix_list;
6054 reloc_bfd_fix *fix_array;
6055 unsigned fix_array_count;
6057 /* Support for expanding the reloc array that is stored
6058 in the section structure. If the relocations have been
6059 reallocated, the newly allocated relocations will be referenced
6060 here along with the actual size allocated. The relocation
6061 count will always be found in the section structure. */
6062 Elf_Internal_Rela *allocated_relocs;
6063 unsigned relocs_count;
6064 unsigned allocated_relocs_count;
6067 struct elf_xtensa_section_data
6069 struct bfd_elf_section_data elf;
6070 xtensa_relax_info relax_info;
6075 elf_xtensa_new_section_hook (bfd *abfd, asection *sec)
6077 if (!sec->used_by_bfd)
6079 struct elf_xtensa_section_data *sdata;
6080 bfd_size_type amt = sizeof (*sdata);
6082 sdata = bfd_zalloc (abfd, amt);
6085 sec->used_by_bfd = sdata;
6088 return _bfd_elf_new_section_hook (abfd, sec);
6092 static xtensa_relax_info *
6093 get_xtensa_relax_info (asection *sec)
6095 struct elf_xtensa_section_data *section_data;
6097 /* No info available if no section or if it is an output section. */
6098 if (!sec || sec == sec->output_section)
6101 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
6102 return §ion_data->relax_info;
6107 init_xtensa_relax_info (asection *sec)
6109 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6111 relax_info->is_relaxable_literal_section = FALSE;
6112 relax_info->is_relaxable_asm_section = FALSE;
6113 relax_info->visited = 0;
6115 relax_info->src_relocs = NULL;
6116 relax_info->src_count = 0;
6117 relax_info->src_next = 0;
6119 relax_info->removed_list.head = NULL;
6120 relax_info->removed_list.tail = NULL;
6122 relax_info->action_list.tree = splay_tree_new (text_action_compare,
6124 relax_info->action_list.map.n_entries = 0;
6125 relax_info->action_list.map.entry = NULL;
6127 relax_info->fix_list = NULL;
6128 relax_info->fix_array = NULL;
6129 relax_info->fix_array_count = 0;
6131 relax_info->allocated_relocs = NULL;
6132 relax_info->relocs_count = 0;
6133 relax_info->allocated_relocs_count = 0;
6137 /* Coalescing literals may require a relocation to refer to a section in
6138 a different input file, but the standard relocation information
6139 cannot express that. Instead, the reloc_bfd_fix structures are used
6140 to "fix" the relocations that refer to sections in other input files.
6141 These structures are kept on per-section lists. The "src_type" field
6142 records the relocation type in case there are multiple relocations on
6143 the same location. FIXME: This is ugly; an alternative might be to
6144 add new symbols with the "owner" field to some other input file. */
6146 struct reloc_bfd_fix_struct
6150 unsigned src_type; /* Relocation type. */
6152 asection *target_sec;
6153 bfd_vma target_offset;
6154 bfd_boolean translated;
6156 reloc_bfd_fix *next;
6160 static reloc_bfd_fix *
6161 reloc_bfd_fix_init (asection *src_sec,
6164 asection *target_sec,
6165 bfd_vma target_offset,
6166 bfd_boolean translated)
6170 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
6171 fix->src_sec = src_sec;
6172 fix->src_offset = src_offset;
6173 fix->src_type = src_type;
6174 fix->target_sec = target_sec;
6175 fix->target_offset = target_offset;
6176 fix->translated = translated;
6183 add_fix (asection *src_sec, reloc_bfd_fix *fix)
6185 xtensa_relax_info *relax_info;
6187 relax_info = get_xtensa_relax_info (src_sec);
6188 fix->next = relax_info->fix_list;
6189 relax_info->fix_list = fix;
6194 fix_compare (const void *ap, const void *bp)
6196 const reloc_bfd_fix *a = (const reloc_bfd_fix *) ap;
6197 const reloc_bfd_fix *b = (const reloc_bfd_fix *) bp;
6199 if (a->src_offset != b->src_offset)
6200 return (a->src_offset - b->src_offset);
6201 return (a->src_type - b->src_type);
6206 cache_fix_array (asection *sec)
6208 unsigned i, count = 0;
6210 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6212 if (relax_info == NULL)
6214 if (relax_info->fix_list == NULL)
6217 for (r = relax_info->fix_list; r != NULL; r = r->next)
6220 relax_info->fix_array =
6221 (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix) * count);
6222 relax_info->fix_array_count = count;
6224 r = relax_info->fix_list;
6225 for (i = 0; i < count; i++, r = r->next)
6227 relax_info->fix_array[count - 1 - i] = *r;
6228 relax_info->fix_array[count - 1 - i].next = NULL;
6231 qsort (relax_info->fix_array, relax_info->fix_array_count,
6232 sizeof (reloc_bfd_fix), fix_compare);
6236 static reloc_bfd_fix *
6237 get_bfd_fix (asection *sec, bfd_vma offset, unsigned type)
6239 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6243 if (relax_info == NULL)
6245 if (relax_info->fix_list == NULL)
6248 if (relax_info->fix_array == NULL)
6249 cache_fix_array (sec);
6251 key.src_offset = offset;
6252 key.src_type = type;
6253 rv = bsearch (&key, relax_info->fix_array, relax_info->fix_array_count,
6254 sizeof (reloc_bfd_fix), fix_compare);
6259 /* Section caching. */
6261 typedef struct section_cache_struct section_cache_t;
6263 struct section_cache_struct
6267 bfd_byte *contents; /* Cache of the section contents. */
6268 bfd_size_type content_length;
6270 property_table_entry *ptbl; /* Cache of the section property table. */
6273 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6274 unsigned reloc_count;
6279 init_section_cache (section_cache_t *sec_cache)
6281 memset (sec_cache, 0, sizeof (*sec_cache));
6286 free_section_cache (section_cache_t *sec_cache)
6290 release_contents (sec_cache->sec, sec_cache->contents);
6291 release_internal_relocs (sec_cache->sec, sec_cache->relocs);
6292 if (sec_cache->ptbl)
6293 free (sec_cache->ptbl);
6299 section_cache_section (section_cache_t *sec_cache,
6301 struct bfd_link_info *link_info)
6304 property_table_entry *prop_table = NULL;
6306 bfd_byte *contents = NULL;
6307 Elf_Internal_Rela *internal_relocs = NULL;
6308 bfd_size_type sec_size;
6312 if (sec == sec_cache->sec)
6316 sec_size = bfd_get_section_limit (abfd, sec);
6318 /* Get the contents. */
6319 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6320 if (contents == NULL && sec_size != 0)
6323 /* Get the relocations. */
6324 internal_relocs = retrieve_internal_relocs (abfd, sec,
6325 link_info->keep_memory);
6327 /* Get the entry table. */
6328 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
6329 XTENSA_PROP_SEC_NAME, FALSE);
6333 /* Fill in the new section cache. */
6334 free_section_cache (sec_cache);
6335 init_section_cache (sec_cache);
6337 sec_cache->sec = sec;
6338 sec_cache->contents = contents;
6339 sec_cache->content_length = sec_size;
6340 sec_cache->relocs = internal_relocs;
6341 sec_cache->reloc_count = sec->reloc_count;
6342 sec_cache->pte_count = ptblsize;
6343 sec_cache->ptbl = prop_table;
6348 release_contents (sec, contents);
6349 release_internal_relocs (sec, internal_relocs);
6356 /* Extended basic blocks. */
6358 /* An ebb_struct represents an Extended Basic Block. Within this
6359 range, we guarantee that all instructions are decodable, the
6360 property table entries are contiguous, and no property table
6361 specifies a segment that cannot have instructions moved. This
6362 structure contains caches of the contents, property table and
6363 relocations for the specified section for easy use. The range is
6364 specified by ranges of indices for the byte offset, property table
6365 offsets and relocation offsets. These must be consistent. */
6367 typedef struct ebb_struct ebb_t;
6373 bfd_byte *contents; /* Cache of the section contents. */
6374 bfd_size_type content_length;
6376 property_table_entry *ptbl; /* Cache of the section property table. */
6379 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6380 unsigned reloc_count;
6382 bfd_vma start_offset; /* Offset in section. */
6383 unsigned start_ptbl_idx; /* Offset in the property table. */
6384 unsigned start_reloc_idx; /* Offset in the relocations. */
6387 unsigned end_ptbl_idx;
6388 unsigned end_reloc_idx;
6390 bfd_boolean ends_section; /* Is this the last ebb in a section? */
6392 /* The unreachable property table at the end of this set of blocks;
6393 NULL if the end is not an unreachable block. */
6394 property_table_entry *ends_unreachable;
6398 enum ebb_target_enum
6401 EBB_DESIRE_TGT_ALIGN,
6402 EBB_REQUIRE_TGT_ALIGN,
6403 EBB_REQUIRE_LOOP_ALIGN,
6408 /* proposed_action_struct is similar to the text_action_struct except
6409 that is represents a potential transformation, not one that will
6410 occur. We build a list of these for an extended basic block
6411 and use them to compute the actual actions desired. We must be
6412 careful that the entire set of actual actions we perform do not
6413 break any relocations that would fit if the actions were not
6416 typedef struct proposed_action_struct proposed_action;
6418 struct proposed_action_struct
6420 enum ebb_target_enum align_type; /* for the target alignment */
6421 bfd_vma alignment_pow;
6422 text_action_t action;
6425 bfd_boolean do_action; /* If false, then we will not perform the action. */
6429 /* The ebb_constraint_struct keeps a set of proposed actions for an
6430 extended basic block. */
6432 typedef struct ebb_constraint_struct ebb_constraint;
6434 struct ebb_constraint_struct
6437 bfd_boolean start_movable;
6439 /* Bytes of extra space at the beginning if movable. */
6440 int start_extra_space;
6442 enum ebb_target_enum start_align;
6444 bfd_boolean end_movable;
6446 /* Bytes of extra space at the end if movable. */
6447 int end_extra_space;
6449 unsigned action_count;
6450 unsigned action_allocated;
6452 /* Array of proposed actions. */
6453 proposed_action *actions;
6455 /* Action alignments -- one for each proposed action. */
6456 enum ebb_target_enum *action_aligns;
6461 init_ebb_constraint (ebb_constraint *c)
6463 memset (c, 0, sizeof (ebb_constraint));
6468 free_ebb_constraint (ebb_constraint *c)
6476 init_ebb (ebb_t *ebb,
6479 bfd_size_type content_length,
6480 property_table_entry *prop_table,
6482 Elf_Internal_Rela *internal_relocs,
6483 unsigned reloc_count)
6485 memset (ebb, 0, sizeof (ebb_t));
6487 ebb->contents = contents;
6488 ebb->content_length = content_length;
6489 ebb->ptbl = prop_table;
6490 ebb->pte_count = ptblsize;
6491 ebb->relocs = internal_relocs;
6492 ebb->reloc_count = reloc_count;
6493 ebb->start_offset = 0;
6494 ebb->end_offset = ebb->content_length - 1;
6495 ebb->start_ptbl_idx = 0;
6496 ebb->end_ptbl_idx = ptblsize;
6497 ebb->start_reloc_idx = 0;
6498 ebb->end_reloc_idx = reloc_count;
6502 /* Extend the ebb to all decodable contiguous sections. The algorithm
6503 for building a basic block around an instruction is to push it
6504 forward until we hit the end of a section, an unreachable block or
6505 a block that cannot be transformed. Then we push it backwards
6506 searching for similar conditions. */
6508 static bfd_boolean extend_ebb_bounds_forward (ebb_t *);
6509 static bfd_boolean extend_ebb_bounds_backward (ebb_t *);
6510 static bfd_size_type insn_block_decodable_len
6511 (bfd_byte *, bfd_size_type, bfd_vma, bfd_size_type);
6514 extend_ebb_bounds (ebb_t *ebb)
6516 if (!extend_ebb_bounds_forward (ebb))
6518 if (!extend_ebb_bounds_backward (ebb))
6525 extend_ebb_bounds_forward (ebb_t *ebb)
6527 property_table_entry *the_entry, *new_entry;
6529 the_entry = &ebb->ptbl[ebb->end_ptbl_idx];
6531 /* Stop when (1) we cannot decode an instruction, (2) we are at
6532 the end of the property tables, (3) we hit a non-contiguous property
6533 table entry, (4) we hit a NO_TRANSFORM region. */
6538 bfd_size_type insn_block_len;
6540 entry_end = the_entry->address - ebb->sec->vma + the_entry->size;
6542 insn_block_decodable_len (ebb->contents, ebb->content_length,
6544 entry_end - ebb->end_offset);
6545 if (insn_block_len != (entry_end - ebb->end_offset))
6547 (*_bfd_error_handler)
6548 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6549 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6552 ebb->end_offset += insn_block_len;
6554 if (ebb->end_offset == ebb->sec->size)
6555 ebb->ends_section = TRUE;
6557 /* Update the reloc counter. */
6558 while (ebb->end_reloc_idx + 1 < ebb->reloc_count
6559 && (ebb->relocs[ebb->end_reloc_idx + 1].r_offset
6562 ebb->end_reloc_idx++;
6565 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6568 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6569 if (((new_entry->flags & XTENSA_PROP_INSN) == 0)
6570 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6571 || ((the_entry->flags & XTENSA_PROP_ALIGN) != 0))
6574 if (the_entry->address + the_entry->size != new_entry->address)
6577 the_entry = new_entry;
6578 ebb->end_ptbl_idx++;
6581 /* Quick check for an unreachable or end of file just at the end. */
6582 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6584 if (ebb->end_offset == ebb->content_length)
6585 ebb->ends_section = TRUE;
6589 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6590 if ((new_entry->flags & XTENSA_PROP_UNREACHABLE) != 0
6591 && the_entry->address + the_entry->size == new_entry->address)
6592 ebb->ends_unreachable = new_entry;
6595 /* Any other ending requires exact alignment. */
6601 extend_ebb_bounds_backward (ebb_t *ebb)
6603 property_table_entry *the_entry, *new_entry;
6605 the_entry = &ebb->ptbl[ebb->start_ptbl_idx];
6607 /* Stop when (1) we cannot decode the instructions in the current entry.
6608 (2) we are at the beginning of the property tables, (3) we hit a
6609 non-contiguous property table entry, (4) we hit a NO_TRANSFORM region. */
6613 bfd_vma block_begin;
6614 bfd_size_type insn_block_len;
6616 block_begin = the_entry->address - ebb->sec->vma;
6618 insn_block_decodable_len (ebb->contents, ebb->content_length,
6620 ebb->start_offset - block_begin);
6621 if (insn_block_len != ebb->start_offset - block_begin)
6623 (*_bfd_error_handler)
6624 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6625 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6628 ebb->start_offset -= insn_block_len;
6630 /* Update the reloc counter. */
6631 while (ebb->start_reloc_idx > 0
6632 && (ebb->relocs[ebb->start_reloc_idx - 1].r_offset
6633 >= ebb->start_offset))
6635 ebb->start_reloc_idx--;
6638 if (ebb->start_ptbl_idx == 0)
6641 new_entry = &ebb->ptbl[ebb->start_ptbl_idx - 1];
6642 if ((new_entry->flags & XTENSA_PROP_INSN) == 0
6643 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6644 || ((new_entry->flags & XTENSA_PROP_ALIGN) != 0))
6646 if (new_entry->address + new_entry->size != the_entry->address)
6649 the_entry = new_entry;
6650 ebb->start_ptbl_idx--;
6656 static bfd_size_type
6657 insn_block_decodable_len (bfd_byte *contents,
6658 bfd_size_type content_len,
6659 bfd_vma block_offset,
6660 bfd_size_type block_len)
6662 bfd_vma offset = block_offset;
6664 while (offset < block_offset + block_len)
6666 bfd_size_type insn_len = 0;
6668 insn_len = insn_decode_len (contents, content_len, offset);
6670 return (offset - block_offset);
6673 return (offset - block_offset);
6678 ebb_propose_action (ebb_constraint *c,
6679 enum ebb_target_enum align_type,
6680 bfd_vma alignment_pow,
6681 text_action_t action,
6684 bfd_boolean do_action)
6686 proposed_action *act;
6688 if (c->action_allocated <= c->action_count)
6690 unsigned new_allocated, i;
6691 proposed_action *new_actions;
6693 new_allocated = (c->action_count + 2) * 2;
6694 new_actions = (proposed_action *)
6695 bfd_zmalloc (sizeof (proposed_action) * new_allocated);
6697 for (i = 0; i < c->action_count; i++)
6698 new_actions[i] = c->actions[i];
6701 c->actions = new_actions;
6702 c->action_allocated = new_allocated;
6705 act = &c->actions[c->action_count];
6706 act->align_type = align_type;
6707 act->alignment_pow = alignment_pow;
6708 act->action = action;
6709 act->offset = offset;
6710 act->removed_bytes = removed_bytes;
6711 act->do_action = do_action;
6717 /* Access to internal relocations, section contents and symbols. */
6719 /* During relaxation, we need to modify relocations, section contents,
6720 and symbol definitions, and we need to keep the original values from
6721 being reloaded from the input files, i.e., we need to "pin" the
6722 modified values in memory. We also want to continue to observe the
6723 setting of the "keep-memory" flag. The following functions wrap the
6724 standard BFD functions to take care of this for us. */
6726 static Elf_Internal_Rela *
6727 retrieve_internal_relocs (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6729 Elf_Internal_Rela *internal_relocs;
6731 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6734 internal_relocs = elf_section_data (sec)->relocs;
6735 if (internal_relocs == NULL)
6736 internal_relocs = (_bfd_elf_link_read_relocs
6737 (abfd, sec, NULL, NULL, keep_memory));
6738 return internal_relocs;
6743 pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6745 elf_section_data (sec)->relocs = internal_relocs;
6750 release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6753 && elf_section_data (sec)->relocs != internal_relocs)
6754 free (internal_relocs);
6759 retrieve_contents (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6762 bfd_size_type sec_size;
6764 sec_size = bfd_get_section_limit (abfd, sec);
6765 contents = elf_section_data (sec)->this_hdr.contents;
6767 if (contents == NULL && sec_size != 0)
6769 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6776 elf_section_data (sec)->this_hdr.contents = contents;
6783 pin_contents (asection *sec, bfd_byte *contents)
6785 elf_section_data (sec)->this_hdr.contents = contents;
6790 release_contents (asection *sec, bfd_byte *contents)
6792 if (contents && elf_section_data (sec)->this_hdr.contents != contents)
6797 static Elf_Internal_Sym *
6798 retrieve_local_syms (bfd *input_bfd)
6800 Elf_Internal_Shdr *symtab_hdr;
6801 Elf_Internal_Sym *isymbuf;
6804 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6805 locsymcount = symtab_hdr->sh_info;
6807 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6808 if (isymbuf == NULL && locsymcount != 0)
6809 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6812 /* Save the symbols for this input file so they won't be read again. */
6813 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
6814 symtab_hdr->contents = (unsigned char *) isymbuf;
6820 /* Code for link-time relaxation. */
6822 /* Initialization for relaxation: */
6823 static bfd_boolean analyze_relocations (struct bfd_link_info *);
6824 static bfd_boolean find_relaxable_sections
6825 (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
6826 static bfd_boolean collect_source_relocs
6827 (bfd *, asection *, struct bfd_link_info *);
6828 static bfd_boolean is_resolvable_asm_expansion
6829 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, struct bfd_link_info *,
6831 static Elf_Internal_Rela *find_associated_l32r_irel
6832 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Rela *);
6833 static bfd_boolean compute_text_actions
6834 (bfd *, asection *, struct bfd_link_info *);
6835 static bfd_boolean compute_ebb_proposed_actions (ebb_constraint *);
6836 static bfd_boolean compute_ebb_actions (ebb_constraint *);
6837 typedef struct reloc_range_list_struct reloc_range_list;
6838 static bfd_boolean check_section_ebb_pcrels_fit
6839 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
6840 reloc_range_list *, const ebb_constraint *,
6841 const xtensa_opcode *);
6842 static bfd_boolean check_section_ebb_reduces (const ebb_constraint *);
6843 static void text_action_add_proposed
6844 (text_action_list *, const ebb_constraint *, asection *);
6845 static int compute_fill_extra_space (property_table_entry *);
6848 static bfd_boolean compute_removed_literals
6849 (bfd *, asection *, struct bfd_link_info *, value_map_hash_table *);
6850 static Elf_Internal_Rela *get_irel_at_offset
6851 (asection *, Elf_Internal_Rela *, bfd_vma);
6852 static bfd_boolean is_removable_literal
6853 (const source_reloc *, int, const source_reloc *, int, asection *,
6854 property_table_entry *, int);
6855 static bfd_boolean remove_dead_literal
6856 (bfd *, asection *, struct bfd_link_info *, Elf_Internal_Rela *,
6857 Elf_Internal_Rela *, source_reloc *, property_table_entry *, int);
6858 static bfd_boolean identify_literal_placement
6859 (bfd *, asection *, bfd_byte *, struct bfd_link_info *,
6860 value_map_hash_table *, bfd_boolean *, Elf_Internal_Rela *, int,
6861 source_reloc *, property_table_entry *, int, section_cache_t *,
6863 static bfd_boolean relocations_reach (source_reloc *, int, const r_reloc *);
6864 static bfd_boolean coalesce_shared_literal
6865 (asection *, source_reloc *, property_table_entry *, int, value_map *);
6866 static bfd_boolean move_shared_literal
6867 (asection *, struct bfd_link_info *, source_reloc *, property_table_entry *,
6868 int, const r_reloc *, const literal_value *, section_cache_t *);
6871 static bfd_boolean relax_section (bfd *, asection *, struct bfd_link_info *);
6872 static bfd_boolean translate_section_fixes (asection *);
6873 static bfd_boolean translate_reloc_bfd_fix (reloc_bfd_fix *);
6874 static asection *translate_reloc (const r_reloc *, r_reloc *, asection *);
6875 static void shrink_dynamic_reloc_sections
6876 (struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *);
6877 static bfd_boolean move_literal
6878 (bfd *, struct bfd_link_info *, asection *, bfd_vma, bfd_byte *,
6879 xtensa_relax_info *, Elf_Internal_Rela **, const literal_value *);
6880 static bfd_boolean relax_property_section
6881 (bfd *, asection *, struct bfd_link_info *);
6884 static bfd_boolean relax_section_symbols (bfd *, asection *);
6888 elf_xtensa_relax_section (bfd *abfd,
6890 struct bfd_link_info *link_info,
6893 static value_map_hash_table *values = NULL;
6894 static bfd_boolean relocations_analyzed = FALSE;
6895 xtensa_relax_info *relax_info;
6897 if (!relocations_analyzed)
6899 /* Do some overall initialization for relaxation. */
6900 values = value_map_hash_table_init ();
6903 relaxing_section = TRUE;
6904 if (!analyze_relocations (link_info))
6906 relocations_analyzed = TRUE;
6910 /* Don't mess with linker-created sections. */
6911 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6914 relax_info = get_xtensa_relax_info (sec);
6915 BFD_ASSERT (relax_info != NULL);
6917 switch (relax_info->visited)
6920 /* Note: It would be nice to fold this pass into
6921 analyze_relocations, but it is important for this step that the
6922 sections be examined in link order. */
6923 if (!compute_removed_literals (abfd, sec, link_info, values))
6930 value_map_hash_table_delete (values);
6932 if (!relax_section (abfd, sec, link_info))
6938 if (!relax_section_symbols (abfd, sec))
6943 relax_info->visited++;
6948 /* Initialization for relaxation. */
6950 /* This function is called once at the start of relaxation. It scans
6951 all the input sections and marks the ones that are relaxable (i.e.,
6952 literal sections with L32R relocations against them), and then
6953 collects source_reloc information for all the relocations against
6954 those relaxable sections. During this process, it also detects
6955 longcalls, i.e., calls relaxed by the assembler into indirect
6956 calls, that can be optimized back into direct calls. Within each
6957 extended basic block (ebb) containing an optimized longcall, it
6958 computes a set of "text actions" that can be performed to remove
6959 the L32R associated with the longcall while optionally preserving
6960 branch target alignments. */
6963 analyze_relocations (struct bfd_link_info *link_info)
6967 bfd_boolean is_relaxable = FALSE;
6969 /* Initialize the per-section relaxation info. */
6970 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
6971 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6973 init_xtensa_relax_info (sec);
6976 /* Mark relaxable sections (and count relocations against each one). */
6977 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
6978 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6980 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
6984 /* Bail out if there are no relaxable sections. */
6988 /* Allocate space for source_relocs. */
6989 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
6990 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6992 xtensa_relax_info *relax_info;
6994 relax_info = get_xtensa_relax_info (sec);
6995 if (relax_info->is_relaxable_literal_section
6996 || relax_info->is_relaxable_asm_section)
6998 relax_info->src_relocs = (source_reloc *)
6999 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
7002 relax_info->src_count = 0;
7005 /* Collect info on relocations against each relaxable section. */
7006 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7007 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7009 if (!collect_source_relocs (abfd, sec, link_info))
7013 /* Compute the text actions. */
7014 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7015 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7017 if (!compute_text_actions (abfd, sec, link_info))
7025 /* Find all the sections that might be relaxed. The motivation for
7026 this pass is that collect_source_relocs() needs to record _all_ the
7027 relocations that target each relaxable section. That is expensive
7028 and unnecessary unless the target section is actually going to be
7029 relaxed. This pass identifies all such sections by checking if
7030 they have L32Rs pointing to them. In the process, the total number
7031 of relocations targeting each section is also counted so that we
7032 know how much space to allocate for source_relocs against each
7033 relaxable literal section. */
7036 find_relaxable_sections (bfd *abfd,
7038 struct bfd_link_info *link_info,
7039 bfd_boolean *is_relaxable_p)
7041 Elf_Internal_Rela *internal_relocs;
7043 bfd_boolean ok = TRUE;
7045 xtensa_relax_info *source_relax_info;
7046 bfd_boolean is_l32r_reloc;
7048 internal_relocs = retrieve_internal_relocs (abfd, sec,
7049 link_info->keep_memory);
7050 if (internal_relocs == NULL)
7053 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7054 if (contents == NULL && sec->size != 0)
7060 source_relax_info = get_xtensa_relax_info (sec);
7061 for (i = 0; i < sec->reloc_count; i++)
7063 Elf_Internal_Rela *irel = &internal_relocs[i];
7065 asection *target_sec;
7066 xtensa_relax_info *target_relax_info;
7068 /* If this section has not already been marked as "relaxable", and
7069 if it contains any ASM_EXPAND relocations (marking expanded
7070 longcalls) that can be optimized into direct calls, then mark
7071 the section as "relaxable". */
7072 if (source_relax_info
7073 && !source_relax_info->is_relaxable_asm_section
7074 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_EXPAND)
7076 bfd_boolean is_reachable = FALSE;
7077 if (is_resolvable_asm_expansion (abfd, sec, contents, irel,
7078 link_info, &is_reachable)
7081 source_relax_info->is_relaxable_asm_section = TRUE;
7082 *is_relaxable_p = TRUE;
7086 r_reloc_init (&r_rel, abfd, irel, contents,
7087 bfd_get_section_limit (abfd, sec));
7089 target_sec = r_reloc_get_section (&r_rel);
7090 target_relax_info = get_xtensa_relax_info (target_sec);
7091 if (!target_relax_info)
7094 /* Count PC-relative operand relocations against the target section.
7095 Note: The conditions tested here must match the conditions under
7096 which init_source_reloc is called in collect_source_relocs(). */
7097 is_l32r_reloc = FALSE;
7098 if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7100 xtensa_opcode opcode =
7101 get_relocation_opcode (abfd, sec, contents, irel);
7102 if (opcode != XTENSA_UNDEFINED)
7104 is_l32r_reloc = (opcode == get_l32r_opcode ());
7105 if (!is_alt_relocation (ELF32_R_TYPE (irel->r_info))
7107 target_relax_info->src_count++;
7111 if (is_l32r_reloc && r_reloc_is_defined (&r_rel))
7113 /* Mark the target section as relaxable. */
7114 target_relax_info->is_relaxable_literal_section = TRUE;
7115 *is_relaxable_p = TRUE;
7120 release_contents (sec, contents);
7121 release_internal_relocs (sec, internal_relocs);
7126 /* Record _all_ the relocations that point to relaxable sections, and
7127 get rid of ASM_EXPAND relocs by either converting them to
7128 ASM_SIMPLIFY or by removing them. */
7131 collect_source_relocs (bfd *abfd,
7133 struct bfd_link_info *link_info)
7135 Elf_Internal_Rela *internal_relocs;
7137 bfd_boolean ok = TRUE;
7139 bfd_size_type sec_size;
7141 internal_relocs = retrieve_internal_relocs (abfd, sec,
7142 link_info->keep_memory);
7143 if (internal_relocs == NULL)
7146 sec_size = bfd_get_section_limit (abfd, sec);
7147 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7148 if (contents == NULL && sec_size != 0)
7154 /* Record relocations against relaxable literal sections. */
7155 for (i = 0; i < sec->reloc_count; i++)
7157 Elf_Internal_Rela *irel = &internal_relocs[i];
7159 asection *target_sec;
7160 xtensa_relax_info *target_relax_info;
7162 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7164 target_sec = r_reloc_get_section (&r_rel);
7165 target_relax_info = get_xtensa_relax_info (target_sec);
7167 if (target_relax_info
7168 && (target_relax_info->is_relaxable_literal_section
7169 || target_relax_info->is_relaxable_asm_section))
7171 xtensa_opcode opcode = XTENSA_UNDEFINED;
7173 bfd_boolean is_abs_literal = FALSE;
7175 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7177 /* None of the current alternate relocs are PC-relative,
7178 and only PC-relative relocs matter here. However, we
7179 still need to record the opcode for literal
7181 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7182 if (opcode == get_l32r_opcode ())
7184 is_abs_literal = TRUE;
7188 opcode = XTENSA_UNDEFINED;
7190 else if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7192 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7193 opnd = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7196 if (opcode != XTENSA_UNDEFINED)
7198 int src_next = target_relax_info->src_next++;
7199 source_reloc *s_reloc = &target_relax_info->src_relocs[src_next];
7201 init_source_reloc (s_reloc, sec, &r_rel, opcode, opnd,
7207 /* Now get rid of ASM_EXPAND relocations. At this point, the
7208 src_relocs array for the target literal section may still be
7209 incomplete, but it must at least contain the entries for the L32R
7210 relocations associated with ASM_EXPANDs because they were just
7211 added in the preceding loop over the relocations. */
7213 for (i = 0; i < sec->reloc_count; i++)
7215 Elf_Internal_Rela *irel = &internal_relocs[i];
7216 bfd_boolean is_reachable;
7218 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
7224 Elf_Internal_Rela *l32r_irel;
7226 asection *target_sec;
7227 xtensa_relax_info *target_relax_info;
7229 /* Mark the source_reloc for the L32R so that it will be
7230 removed in compute_removed_literals(), along with the
7231 associated literal. */
7232 l32r_irel = find_associated_l32r_irel (abfd, sec, contents,
7233 irel, internal_relocs);
7234 if (l32r_irel == NULL)
7237 r_reloc_init (&r_rel, abfd, l32r_irel, contents, sec_size);
7239 target_sec = r_reloc_get_section (&r_rel);
7240 target_relax_info = get_xtensa_relax_info (target_sec);
7242 if (target_relax_info
7243 && (target_relax_info->is_relaxable_literal_section
7244 || target_relax_info->is_relaxable_asm_section))
7246 source_reloc *s_reloc;
7248 /* Search the source_relocs for the entry corresponding to
7249 the l32r_irel. Note: The src_relocs array is not yet
7250 sorted, but it wouldn't matter anyway because we're
7251 searching by source offset instead of target offset. */
7252 s_reloc = find_source_reloc (target_relax_info->src_relocs,
7253 target_relax_info->src_next,
7255 BFD_ASSERT (s_reloc);
7256 s_reloc->is_null = TRUE;
7259 /* Convert this reloc to ASM_SIMPLIFY. */
7260 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
7261 R_XTENSA_ASM_SIMPLIFY);
7262 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7264 pin_internal_relocs (sec, internal_relocs);
7268 /* It is resolvable but doesn't reach. We resolve now
7269 by eliminating the relocation -- the call will remain
7270 expanded into L32R/CALLX. */
7271 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7272 pin_internal_relocs (sec, internal_relocs);
7277 release_contents (sec, contents);
7278 release_internal_relocs (sec, internal_relocs);
7283 /* Return TRUE if the asm expansion can be resolved. Generally it can
7284 be resolved on a final link or when a partial link locates it in the
7285 same section as the target. Set "is_reachable" flag if the target of
7286 the call is within the range of a direct call, given the current VMA
7287 for this section and the target section. */
7290 is_resolvable_asm_expansion (bfd *abfd,
7293 Elf_Internal_Rela *irel,
7294 struct bfd_link_info *link_info,
7295 bfd_boolean *is_reachable_p)
7297 asection *target_sec;
7298 bfd_vma target_offset;
7300 xtensa_opcode opcode, direct_call_opcode;
7301 bfd_vma self_address;
7302 bfd_vma dest_address;
7303 bfd_boolean uses_l32r;
7304 bfd_size_type sec_size;
7306 *is_reachable_p = FALSE;
7308 if (contents == NULL)
7311 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
7314 sec_size = bfd_get_section_limit (abfd, sec);
7315 opcode = get_expanded_call_opcode (contents + irel->r_offset,
7316 sec_size - irel->r_offset, &uses_l32r);
7317 /* Optimization of longcalls that use CONST16 is not yet implemented. */
7321 direct_call_opcode = swap_callx_for_call_opcode (opcode);
7322 if (direct_call_opcode == XTENSA_UNDEFINED)
7325 /* Check and see that the target resolves. */
7326 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7327 if (!r_reloc_is_defined (&r_rel))
7330 target_sec = r_reloc_get_section (&r_rel);
7331 target_offset = r_rel.target_offset;
7333 /* If the target is in a shared library, then it doesn't reach. This
7334 isn't supposed to come up because the compiler should never generate
7335 non-PIC calls on systems that use shared libraries, but the linker
7336 shouldn't crash regardless. */
7337 if (!target_sec->output_section)
7340 /* For relocatable sections, we can only simplify when the output
7341 section of the target is the same as the output section of the
7343 if (bfd_link_relocatable (link_info)
7344 && (target_sec->output_section != sec->output_section
7345 || is_reloc_sym_weak (abfd, irel)))
7348 if (target_sec->output_section != sec->output_section)
7350 /* If the two sections are sufficiently far away that relaxation
7351 might take the call out of range, we can't simplify. For
7352 example, a positive displacement call into another memory
7353 could get moved to a lower address due to literal removal,
7354 but the destination won't move, and so the displacment might
7357 If the displacement is negative, assume the destination could
7358 move as far back as the start of the output section. The
7359 self_address will be at least as far into the output section
7360 as it is prior to relaxation.
7362 If the displacement is postive, assume the destination will be in
7363 it's pre-relaxed location (because relaxation only makes sections
7364 smaller). The self_address could go all the way to the beginning
7365 of the output section. */
7367 dest_address = target_sec->output_section->vma;
7368 self_address = sec->output_section->vma;
7370 if (sec->output_section->vma > target_sec->output_section->vma)
7371 self_address += sec->output_offset + irel->r_offset + 3;
7373 dest_address += bfd_get_section_limit (abfd, target_sec->output_section);
7374 /* Call targets should be four-byte aligned. */
7375 dest_address = (dest_address + 3) & ~3;
7380 self_address = (sec->output_section->vma
7381 + sec->output_offset + irel->r_offset + 3);
7382 dest_address = (target_sec->output_section->vma
7383 + target_sec->output_offset + target_offset);
7386 *is_reachable_p = pcrel_reloc_fits (direct_call_opcode, 0,
7387 self_address, dest_address);
7389 if ((self_address >> CALL_SEGMENT_BITS) !=
7390 (dest_address >> CALL_SEGMENT_BITS))
7397 static Elf_Internal_Rela *
7398 find_associated_l32r_irel (bfd *abfd,
7401 Elf_Internal_Rela *other_irel,
7402 Elf_Internal_Rela *internal_relocs)
7406 for (i = 0; i < sec->reloc_count; i++)
7408 Elf_Internal_Rela *irel = &internal_relocs[i];
7410 if (irel == other_irel)
7412 if (irel->r_offset != other_irel->r_offset)
7414 if (is_l32r_relocation (abfd, sec, contents, irel))
7422 static xtensa_opcode *
7423 build_reloc_opcodes (bfd *abfd,
7426 Elf_Internal_Rela *internal_relocs)
7429 xtensa_opcode *reloc_opcodes =
7430 (xtensa_opcode *) bfd_malloc (sizeof (xtensa_opcode) * sec->reloc_count);
7431 for (i = 0; i < sec->reloc_count; i++)
7433 Elf_Internal_Rela *irel = &internal_relocs[i];
7434 reloc_opcodes[i] = get_relocation_opcode (abfd, sec, contents, irel);
7436 return reloc_opcodes;
7439 struct reloc_range_struct
7442 bfd_boolean add; /* TRUE if start of a range, FALSE otherwise. */
7443 /* Original irel index in the array of relocations for a section. */
7444 unsigned irel_index;
7446 typedef struct reloc_range_struct reloc_range;
7448 typedef struct reloc_range_list_entry_struct reloc_range_list_entry;
7449 struct reloc_range_list_entry_struct
7451 reloc_range_list_entry *next;
7452 reloc_range_list_entry *prev;
7453 Elf_Internal_Rela *irel;
7454 xtensa_opcode opcode;
7458 struct reloc_range_list_struct
7460 /* The rest of the structure is only meaningful when ok is TRUE. */
7463 unsigned n_range; /* Number of range markers. */
7464 reloc_range *range; /* Sorted range markers. */
7466 unsigned first; /* Index of a first range element in the list. */
7467 unsigned last; /* One past index of a last range element in the list. */
7469 unsigned n_list; /* Number of list elements. */
7470 reloc_range_list_entry *reloc; /* */
7471 reloc_range_list_entry list_root;
7475 reloc_range_compare (const void *a, const void *b)
7477 const reloc_range *ra = a;
7478 const reloc_range *rb = b;
7480 if (ra->addr != rb->addr)
7481 return ra->addr < rb->addr ? -1 : 1;
7482 if (ra->add != rb->add)
7483 return ra->add ? -1 : 1;
7488 build_reloc_ranges (bfd *abfd, asection *sec,
7490 Elf_Internal_Rela *internal_relocs,
7491 xtensa_opcode *reloc_opcodes,
7492 reloc_range_list *list)
7497 reloc_range *ranges = NULL;
7498 reloc_range_list_entry *reloc =
7499 bfd_malloc (sec->reloc_count * sizeof (*reloc));
7501 memset (list, 0, sizeof (*list));
7504 for (i = 0; i < sec->reloc_count; i++)
7506 Elf_Internal_Rela *irel = &internal_relocs[i];
7507 int r_type = ELF32_R_TYPE (irel->r_info);
7508 reloc_howto_type *howto = &elf_howto_table[r_type];
7511 if (r_type == R_XTENSA_ASM_SIMPLIFY
7512 || r_type == R_XTENSA_32_PCREL
7513 || !howto->pc_relative)
7516 r_reloc_init (&r_rel, abfd, irel, contents,
7517 bfd_get_section_limit (abfd, sec));
7519 if (r_reloc_get_section (&r_rel) != sec)
7524 max_n = (max_n + 2) * 2;
7525 ranges = bfd_realloc (ranges, max_n * sizeof (*ranges));
7528 ranges[n].addr = irel->r_offset;
7529 ranges[n + 1].addr = r_rel.target_offset;
7531 ranges[n].add = ranges[n].addr < ranges[n + 1].addr;
7532 ranges[n + 1].add = !ranges[n].add;
7534 ranges[n].irel_index = i;
7535 ranges[n + 1].irel_index = i;
7539 reloc[i].irel = irel;
7541 /* Every relocation won't possibly be checked in the optimized version of
7542 check_section_ebb_pcrels_fit, so this needs to be done here. */
7543 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7545 /* None of the current alternate relocs are PC-relative,
7546 and only PC-relative relocs matter here. */
7550 xtensa_opcode opcode;
7554 opcode = reloc_opcodes[i];
7556 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7558 if (opcode == XTENSA_UNDEFINED)
7564 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7565 if (opnum == XTENSA_UNDEFINED)
7571 /* Record relocation opcode and opnum as we've calculated them
7572 anyway and they won't change. */
7573 reloc[i].opcode = opcode;
7574 reloc[i].opnum = opnum;
7580 ranges = bfd_realloc (ranges, n * sizeof (*ranges));
7581 qsort (ranges, n, sizeof (*ranges), reloc_range_compare);
7584 list->range = ranges;
7585 list->reloc = reloc;
7586 list->list_root.prev = &list->list_root;
7587 list->list_root.next = &list->list_root;
7596 static void reloc_range_list_append (reloc_range_list *list,
7597 unsigned irel_index)
7599 reloc_range_list_entry *entry = list->reloc + irel_index;
7601 entry->prev = list->list_root.prev;
7602 entry->next = &list->list_root;
7603 entry->prev->next = entry;
7604 entry->next->prev = entry;
7608 static void reloc_range_list_remove (reloc_range_list *list,
7609 unsigned irel_index)
7611 reloc_range_list_entry *entry = list->reloc + irel_index;
7613 entry->next->prev = entry->prev;
7614 entry->prev->next = entry->next;
7618 /* Update relocation list object so that it lists all relocations that cross
7619 [first; last] range. Range bounds should not decrease with successive
7621 static void reloc_range_list_update_range (reloc_range_list *list,
7622 bfd_vma first, bfd_vma last)
7624 /* This should not happen: EBBs are iterated from lower addresses to higher.
7625 But even if that happens there's no need to break: just flush current list
7626 and start from scratch. */
7627 if ((list->last > 0 && list->range[list->last - 1].addr > last) ||
7628 (list->first > 0 && list->range[list->first - 1].addr >= first))
7633 list->list_root.next = &list->list_root;
7634 list->list_root.prev = &list->list_root;
7635 fprintf (stderr, "%s: move backwards requested\n", __func__);
7638 for (; list->last < list->n_range &&
7639 list->range[list->last].addr <= last; ++list->last)
7640 if (list->range[list->last].add)
7641 reloc_range_list_append (list, list->range[list->last].irel_index);
7643 for (; list->first < list->n_range &&
7644 list->range[list->first].addr < first; ++list->first)
7645 if (!list->range[list->first].add)
7646 reloc_range_list_remove (list, list->range[list->first].irel_index);
7649 static void free_reloc_range_list (reloc_range_list *list)
7655 /* The compute_text_actions function will build a list of potential
7656 transformation actions for code in the extended basic block of each
7657 longcall that is optimized to a direct call. From this list we
7658 generate a set of actions to actually perform that optimizes for
7659 space and, if not using size_opt, maintains branch target
7662 These actions to be performed are placed on a per-section list.
7663 The actual changes are performed by relax_section() in the second
7667 compute_text_actions (bfd *abfd,
7669 struct bfd_link_info *link_info)
7671 xtensa_opcode *reloc_opcodes = NULL;
7672 xtensa_relax_info *relax_info;
7674 Elf_Internal_Rela *internal_relocs;
7675 bfd_boolean ok = TRUE;
7677 property_table_entry *prop_table = 0;
7679 bfd_size_type sec_size;
7680 reloc_range_list relevant_relocs;
7682 relax_info = get_xtensa_relax_info (sec);
7683 BFD_ASSERT (relax_info);
7684 BFD_ASSERT (relax_info->src_next == relax_info->src_count);
7686 /* Do nothing if the section contains no optimized longcalls. */
7687 if (!relax_info->is_relaxable_asm_section)
7690 internal_relocs = retrieve_internal_relocs (abfd, sec,
7691 link_info->keep_memory);
7693 if (internal_relocs)
7694 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
7695 internal_reloc_compare);
7697 sec_size = bfd_get_section_limit (abfd, sec);
7698 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7699 if (contents == NULL && sec_size != 0)
7705 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
7706 XTENSA_PROP_SEC_NAME, FALSE);
7713 /* Precompute the opcode for each relocation. */
7714 reloc_opcodes = build_reloc_opcodes (abfd, sec, contents, internal_relocs);
7716 build_reloc_ranges (abfd, sec, contents, internal_relocs, reloc_opcodes,
7719 for (i = 0; i < sec->reloc_count; i++)
7721 Elf_Internal_Rela *irel = &internal_relocs[i];
7723 property_table_entry *the_entry;
7726 ebb_constraint ebb_table;
7727 bfd_size_type simplify_size;
7729 if (irel && ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_SIMPLIFY)
7731 r_offset = irel->r_offset;
7733 simplify_size = get_asm_simplify_size (contents, sec_size, r_offset);
7734 if (simplify_size == 0)
7736 (*_bfd_error_handler)
7737 (_("%B(%A+0x%lx): could not decode instruction for XTENSA_ASM_SIMPLIFY relocation; possible configuration mismatch"),
7738 sec->owner, sec, r_offset);
7742 /* If the instruction table is not around, then don't do this
7744 the_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
7745 sec->vma + irel->r_offset);
7746 if (the_entry == NULL || XTENSA_NO_NOP_REMOVAL)
7748 text_action_add (&relax_info->action_list,
7749 ta_convert_longcall, sec, r_offset,
7754 /* If the next longcall happens to be at the same address as an
7755 unreachable section of size 0, then skip forward. */
7756 ptbl_idx = the_entry - prop_table;
7757 while ((the_entry->flags & XTENSA_PROP_UNREACHABLE)
7758 && the_entry->size == 0
7759 && ptbl_idx + 1 < ptblsize
7760 && (prop_table[ptbl_idx + 1].address
7761 == prop_table[ptbl_idx].address))
7767 if (the_entry->flags & XTENSA_PROP_NO_TRANSFORM)
7768 /* NO_REORDER is OK */
7771 init_ebb_constraint (&ebb_table);
7772 ebb = &ebb_table.ebb;
7773 init_ebb (ebb, sec, contents, sec_size, prop_table, ptblsize,
7774 internal_relocs, sec->reloc_count);
7775 ebb->start_offset = r_offset + simplify_size;
7776 ebb->end_offset = r_offset + simplify_size;
7777 ebb->start_ptbl_idx = ptbl_idx;
7778 ebb->end_ptbl_idx = ptbl_idx;
7779 ebb->start_reloc_idx = i;
7780 ebb->end_reloc_idx = i;
7782 if (!extend_ebb_bounds (ebb)
7783 || !compute_ebb_proposed_actions (&ebb_table)
7784 || !compute_ebb_actions (&ebb_table)
7785 || !check_section_ebb_pcrels_fit (abfd, sec, contents,
7788 &ebb_table, reloc_opcodes)
7789 || !check_section_ebb_reduces (&ebb_table))
7791 /* If anything goes wrong or we get unlucky and something does
7792 not fit, with our plan because of expansion between
7793 critical branches, just convert to a NOP. */
7795 text_action_add (&relax_info->action_list,
7796 ta_convert_longcall, sec, r_offset, 0);
7797 i = ebb_table.ebb.end_reloc_idx;
7798 free_ebb_constraint (&ebb_table);
7802 text_action_add_proposed (&relax_info->action_list, &ebb_table, sec);
7804 /* Update the index so we do not go looking at the relocations
7805 we have already processed. */
7806 i = ebb_table.ebb.end_reloc_idx;
7807 free_ebb_constraint (&ebb_table);
7810 free_reloc_range_list (&relevant_relocs);
7813 if (action_list_count (&relax_info->action_list))
7814 print_action_list (stderr, &relax_info->action_list);
7818 release_contents (sec, contents);
7819 release_internal_relocs (sec, internal_relocs);
7823 free (reloc_opcodes);
7829 /* Do not widen an instruction if it is preceeded by a
7830 loop opcode. It might cause misalignment. */
7833 prev_instr_is_a_loop (bfd_byte *contents,
7834 bfd_size_type content_length,
7835 bfd_size_type offset)
7837 xtensa_opcode prev_opcode;
7841 prev_opcode = insn_decode_opcode (contents, content_length, offset-3, 0);
7842 return (xtensa_opcode_is_loop (xtensa_default_isa, prev_opcode) == 1);
7846 /* Find all of the possible actions for an extended basic block. */
7849 compute_ebb_proposed_actions (ebb_constraint *ebb_table)
7851 const ebb_t *ebb = &ebb_table->ebb;
7852 unsigned rel_idx = ebb->start_reloc_idx;
7853 property_table_entry *entry, *start_entry, *end_entry;
7855 xtensa_isa isa = xtensa_default_isa;
7857 static xtensa_insnbuf insnbuf = NULL;
7858 static xtensa_insnbuf slotbuf = NULL;
7860 if (insnbuf == NULL)
7862 insnbuf = xtensa_insnbuf_alloc (isa);
7863 slotbuf = xtensa_insnbuf_alloc (isa);
7866 start_entry = &ebb->ptbl[ebb->start_ptbl_idx];
7867 end_entry = &ebb->ptbl[ebb->end_ptbl_idx];
7869 for (entry = start_entry; entry <= end_entry; entry++)
7871 bfd_vma start_offset, end_offset;
7872 bfd_size_type insn_len;
7874 start_offset = entry->address - ebb->sec->vma;
7875 end_offset = entry->address + entry->size - ebb->sec->vma;
7877 if (entry == start_entry)
7878 start_offset = ebb->start_offset;
7879 if (entry == end_entry)
7880 end_offset = ebb->end_offset;
7881 offset = start_offset;
7883 if (offset == entry->address - ebb->sec->vma
7884 && (entry->flags & XTENSA_PROP_INSN_BRANCH_TARGET) != 0)
7886 enum ebb_target_enum align_type = EBB_DESIRE_TGT_ALIGN;
7887 BFD_ASSERT (offset != end_offset);
7888 if (offset == end_offset)
7891 insn_len = insn_decode_len (ebb->contents, ebb->content_length,
7896 if (check_branch_target_aligned_address (offset, insn_len))
7897 align_type = EBB_REQUIRE_TGT_ALIGN;
7899 ebb_propose_action (ebb_table, align_type, 0,
7900 ta_none, offset, 0, TRUE);
7903 while (offset != end_offset)
7905 Elf_Internal_Rela *irel;
7906 xtensa_opcode opcode;
7908 while (rel_idx < ebb->end_reloc_idx
7909 && (ebb->relocs[rel_idx].r_offset < offset
7910 || (ebb->relocs[rel_idx].r_offset == offset
7911 && (ELF32_R_TYPE (ebb->relocs[rel_idx].r_info)
7912 != R_XTENSA_ASM_SIMPLIFY))))
7915 /* Check for longcall. */
7916 irel = &ebb->relocs[rel_idx];
7917 if (irel->r_offset == offset
7918 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_SIMPLIFY)
7920 bfd_size_type simplify_size;
7922 simplify_size = get_asm_simplify_size (ebb->contents,
7923 ebb->content_length,
7925 if (simplify_size == 0)
7928 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7929 ta_convert_longcall, offset, 0, TRUE);
7931 offset += simplify_size;
7935 if (offset + MIN_INSN_LENGTH > ebb->content_length)
7937 xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
7938 ebb->content_length - offset);
7939 fmt = xtensa_format_decode (isa, insnbuf);
7940 if (fmt == XTENSA_UNDEFINED)
7942 insn_len = xtensa_format_length (isa, fmt);
7943 if (insn_len == (bfd_size_type) XTENSA_UNDEFINED)
7946 if (xtensa_format_num_slots (isa, fmt) != 1)
7952 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7953 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7954 if (opcode == XTENSA_UNDEFINED)
7957 if ((entry->flags & XTENSA_PROP_INSN_NO_DENSITY) == 0
7958 && (entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7959 && can_narrow_instruction (slotbuf, fmt, opcode) != 0)
7961 /* Add an instruction narrow action. */
7962 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7963 ta_narrow_insn, offset, 0, FALSE);
7965 else if ((entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7966 && can_widen_instruction (slotbuf, fmt, opcode) != 0
7967 && ! prev_instr_is_a_loop (ebb->contents,
7968 ebb->content_length, offset))
7970 /* Add an instruction widen action. */
7971 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7972 ta_widen_insn, offset, 0, FALSE);
7974 else if (xtensa_opcode_is_loop (xtensa_default_isa, opcode) == 1)
7976 /* Check for branch targets. */
7977 ebb_propose_action (ebb_table, EBB_REQUIRE_LOOP_ALIGN, 0,
7978 ta_none, offset, 0, TRUE);
7985 if (ebb->ends_unreachable)
7987 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7988 ta_fill, ebb->end_offset, 0, TRUE);
7994 (*_bfd_error_handler)
7995 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
7996 ebb->sec->owner, ebb->sec, offset);
8001 /* After all of the information has collected about the
8002 transformations possible in an EBB, compute the appropriate actions
8003 here in compute_ebb_actions. We still must check later to make
8004 sure that the actions do not break any relocations. The algorithm
8005 used here is pretty greedy. Basically, it removes as many no-ops
8006 as possible so that the end of the EBB has the same alignment
8007 characteristics as the original. First, it uses narrowing, then
8008 fill space at the end of the EBB, and finally widenings. If that
8009 does not work, it tries again with one fewer no-op removed. The
8010 optimization will only be performed if all of the branch targets
8011 that were aligned before transformation are also aligned after the
8014 When the size_opt flag is set, ignore the branch target alignments,
8015 narrow all wide instructions, and remove all no-ops unless the end
8016 of the EBB prevents it. */
8019 compute_ebb_actions (ebb_constraint *ebb_table)
8023 int removed_bytes = 0;
8024 ebb_t *ebb = &ebb_table->ebb;
8025 unsigned seg_idx_start = 0;
8026 unsigned seg_idx_end = 0;
8028 /* We perform this like the assembler relaxation algorithm: Start by
8029 assuming all instructions are narrow and all no-ops removed; then
8032 /* For each segment of this that has a solid constraint, check to
8033 see if there are any combinations that will keep the constraint.
8035 for (seg_idx_end = 0; seg_idx_end < ebb_table->action_count; seg_idx_end++)
8037 bfd_boolean requires_text_end_align = FALSE;
8038 unsigned longcall_count = 0;
8039 unsigned longcall_convert_count = 0;
8040 unsigned narrowable_count = 0;
8041 unsigned narrowable_convert_count = 0;
8042 unsigned widenable_count = 0;
8043 unsigned widenable_convert_count = 0;
8045 proposed_action *action = NULL;
8046 int align = (1 << ebb_table->ebb.sec->alignment_power);
8048 seg_idx_start = seg_idx_end;
8050 for (i = seg_idx_start; i < ebb_table->action_count; i++)
8052 action = &ebb_table->actions[i];
8053 if (action->action == ta_convert_longcall)
8055 if (action->action == ta_narrow_insn)
8057 if (action->action == ta_widen_insn)
8059 if (action->action == ta_fill)
8061 if (action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8063 if (action->align_type == EBB_REQUIRE_TGT_ALIGN
8064 && !elf32xtensa_size_opt)
8069 if (seg_idx_end == ebb_table->action_count && !ebb->ends_unreachable)
8070 requires_text_end_align = TRUE;
8072 if (elf32xtensa_size_opt && !requires_text_end_align
8073 && action->align_type != EBB_REQUIRE_LOOP_ALIGN
8074 && action->align_type != EBB_REQUIRE_TGT_ALIGN)
8076 longcall_convert_count = longcall_count;
8077 narrowable_convert_count = narrowable_count;
8078 widenable_convert_count = 0;
8082 /* There is a constraint. Convert the max number of longcalls. */
8083 narrowable_convert_count = 0;
8084 longcall_convert_count = 0;
8085 widenable_convert_count = 0;
8087 for (j = 0; j < longcall_count; j++)
8089 int removed = (longcall_count - j) * 3 & (align - 1);
8090 unsigned desire_narrow = (align - removed) & (align - 1);
8091 unsigned desire_widen = removed;
8092 if (desire_narrow <= narrowable_count)
8094 narrowable_convert_count = desire_narrow;
8095 narrowable_convert_count +=
8096 (align * ((narrowable_count - narrowable_convert_count)
8098 longcall_convert_count = (longcall_count - j);
8099 widenable_convert_count = 0;
8102 if (desire_widen <= widenable_count && !elf32xtensa_size_opt)
8104 narrowable_convert_count = 0;
8105 longcall_convert_count = longcall_count - j;
8106 widenable_convert_count = desire_widen;
8112 /* Now the number of conversions are saved. Do them. */
8113 for (i = seg_idx_start; i < seg_idx_end; i++)
8115 action = &ebb_table->actions[i];
8116 switch (action->action)
8118 case ta_convert_longcall:
8119 if (longcall_convert_count != 0)
8121 action->action = ta_remove_longcall;
8122 action->do_action = TRUE;
8123 action->removed_bytes += 3;
8124 longcall_convert_count--;
8127 case ta_narrow_insn:
8128 if (narrowable_convert_count != 0)
8130 action->do_action = TRUE;
8131 action->removed_bytes += 1;
8132 narrowable_convert_count--;
8136 if (widenable_convert_count != 0)
8138 action->do_action = TRUE;
8139 action->removed_bytes -= 1;
8140 widenable_convert_count--;
8149 /* Now we move on to some local opts. Try to remove each of the
8150 remaining longcalls. */
8152 if (ebb_table->ebb.ends_section || ebb_table->ebb.ends_unreachable)
8155 for (i = 0; i < ebb_table->action_count; i++)
8157 int old_removed_bytes = removed_bytes;
8158 proposed_action *action = &ebb_table->actions[i];
8160 if (action->do_action && action->action == ta_convert_longcall)
8162 bfd_boolean bad_alignment = FALSE;
8164 for (j = i + 1; j < ebb_table->action_count; j++)
8166 proposed_action *new_action = &ebb_table->actions[j];
8167 bfd_vma offset = new_action->offset;
8168 if (new_action->align_type == EBB_REQUIRE_TGT_ALIGN)
8170 if (!check_branch_target_aligned
8171 (ebb_table->ebb.contents,
8172 ebb_table->ebb.content_length,
8173 offset, offset - removed_bytes))
8175 bad_alignment = TRUE;
8179 if (new_action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8181 if (!check_loop_aligned (ebb_table->ebb.contents,
8182 ebb_table->ebb.content_length,
8184 offset - removed_bytes))
8186 bad_alignment = TRUE;
8190 if (new_action->action == ta_narrow_insn
8191 && !new_action->do_action
8192 && ebb_table->ebb.sec->alignment_power == 2)
8194 /* Narrow an instruction and we are done. */
8195 new_action->do_action = TRUE;
8196 new_action->removed_bytes += 1;
8197 bad_alignment = FALSE;
8200 if (new_action->action == ta_widen_insn
8201 && new_action->do_action
8202 && ebb_table->ebb.sec->alignment_power == 2)
8204 /* Narrow an instruction and we are done. */
8205 new_action->do_action = FALSE;
8206 new_action->removed_bytes += 1;
8207 bad_alignment = FALSE;
8210 if (new_action->do_action)
8211 removed_bytes += new_action->removed_bytes;
8215 action->removed_bytes += 3;
8216 action->action = ta_remove_longcall;
8217 action->do_action = TRUE;
8220 removed_bytes = old_removed_bytes;
8221 if (action->do_action)
8222 removed_bytes += action->removed_bytes;
8227 for (i = 0; i < ebb_table->action_count; ++i)
8229 proposed_action *action = &ebb_table->actions[i];
8230 if (action->do_action)
8231 removed_bytes += action->removed_bytes;
8234 if ((removed_bytes % (1 << ebb_table->ebb.sec->alignment_power)) != 0
8235 && ebb->ends_unreachable)
8237 proposed_action *action;
8241 BFD_ASSERT (ebb_table->action_count != 0);
8242 action = &ebb_table->actions[ebb_table->action_count - 1];
8243 BFD_ASSERT (action->action == ta_fill);
8244 BFD_ASSERT (ebb->ends_unreachable->flags & XTENSA_PROP_UNREACHABLE);
8246 extra_space = compute_fill_extra_space (ebb->ends_unreachable);
8247 br = action->removed_bytes + removed_bytes + extra_space;
8248 br = br & ((1 << ebb->sec->alignment_power ) - 1);
8250 action->removed_bytes = extra_space - br;
8256 /* The xlate_map is a sorted array of address mappings designed to
8257 answer the offset_with_removed_text() query with a binary search instead
8258 of a linear search through the section's action_list. */
8260 typedef struct xlate_map_entry xlate_map_entry_t;
8261 typedef struct xlate_map xlate_map_t;
8263 struct xlate_map_entry
8265 unsigned orig_address;
8266 unsigned new_address;
8272 unsigned entry_count;
8273 xlate_map_entry_t *entry;
8278 xlate_compare (const void *a_v, const void *b_v)
8280 const xlate_map_entry_t *a = (const xlate_map_entry_t *) a_v;
8281 const xlate_map_entry_t *b = (const xlate_map_entry_t *) b_v;
8282 if (a->orig_address < b->orig_address)
8284 if (a->orig_address > (b->orig_address + b->size - 1))
8291 xlate_offset_with_removed_text (const xlate_map_t *map,
8292 text_action_list *action_list,
8296 xlate_map_entry_t *e;
8299 return offset_with_removed_text (action_list, offset);
8301 if (map->entry_count == 0)
8304 r = bsearch (&offset, map->entry, map->entry_count,
8305 sizeof (xlate_map_entry_t), &xlate_compare);
8306 e = (xlate_map_entry_t *) r;
8308 BFD_ASSERT (e != NULL);
8311 return e->new_address - e->orig_address + offset;
8314 typedef struct xlate_map_context_struct xlate_map_context;
8315 struct xlate_map_context_struct
8318 xlate_map_entry_t *current_entry;
8323 xlate_map_fn (splay_tree_node node, void *p)
8325 text_action *r = (text_action *)node->value;
8326 xlate_map_context *ctx = p;
8327 unsigned orig_size = 0;
8332 case ta_remove_insn:
8333 case ta_convert_longcall:
8334 case ta_remove_literal:
8335 case ta_add_literal:
8337 case ta_remove_longcall:
8340 case ta_narrow_insn:
8349 ctx->current_entry->size =
8350 r->offset + orig_size - ctx->current_entry->orig_address;
8351 if (ctx->current_entry->size != 0)
8353 ctx->current_entry++;
8354 ctx->map->entry_count++;
8356 ctx->current_entry->orig_address = r->offset + orig_size;
8357 ctx->removed += r->removed_bytes;
8358 ctx->current_entry->new_address = r->offset + orig_size - ctx->removed;
8359 ctx->current_entry->size = 0;
8363 /* Build a binary searchable offset translation map from a section's
8366 static xlate_map_t *
8367 build_xlate_map (asection *sec, xtensa_relax_info *relax_info)
8369 text_action_list *action_list = &relax_info->action_list;
8370 unsigned num_actions = 0;
8371 xlate_map_context ctx;
8373 ctx.map = (xlate_map_t *) bfd_malloc (sizeof (xlate_map_t));
8375 if (ctx.map == NULL)
8378 num_actions = action_list_count (action_list);
8379 ctx.map->entry = (xlate_map_entry_t *)
8380 bfd_malloc (sizeof (xlate_map_entry_t) * (num_actions + 1));
8381 if (ctx.map->entry == NULL)
8386 ctx.map->entry_count = 0;
8389 ctx.current_entry = &ctx.map->entry[0];
8391 ctx.current_entry->orig_address = 0;
8392 ctx.current_entry->new_address = 0;
8393 ctx.current_entry->size = 0;
8395 splay_tree_foreach (action_list->tree, xlate_map_fn, &ctx);
8397 ctx.current_entry->size = (bfd_get_section_limit (sec->owner, sec)
8398 - ctx.current_entry->orig_address);
8399 if (ctx.current_entry->size != 0)
8400 ctx.map->entry_count++;
8406 /* Free an offset translation map. */
8409 free_xlate_map (xlate_map_t *map)
8411 if (map && map->entry)
8418 /* Use check_section_ebb_pcrels_fit to make sure that all of the
8419 relocations in a section will fit if a proposed set of actions
8423 check_section_ebb_pcrels_fit (bfd *abfd,
8426 Elf_Internal_Rela *internal_relocs,
8427 reloc_range_list *relevant_relocs,
8428 const ebb_constraint *constraint,
8429 const xtensa_opcode *reloc_opcodes)
8432 unsigned n = sec->reloc_count;
8433 Elf_Internal_Rela *irel;
8434 xlate_map_t *xmap = NULL;
8435 bfd_boolean ok = TRUE;
8436 xtensa_relax_info *relax_info;
8437 reloc_range_list_entry *entry = NULL;
8439 relax_info = get_xtensa_relax_info (sec);
8441 if (relax_info && sec->reloc_count > 100)
8443 xmap = build_xlate_map (sec, relax_info);
8444 /* NULL indicates out of memory, but the slow version
8445 can still be used. */
8448 if (relevant_relocs && constraint->action_count)
8450 if (!relevant_relocs->ok)
8457 bfd_vma min_offset, max_offset;
8458 min_offset = max_offset = constraint->actions[0].offset;
8460 for (i = 1; i < constraint->action_count; ++i)
8462 proposed_action *action = &constraint->actions[i];
8463 bfd_vma offset = action->offset;
8465 if (offset < min_offset)
8466 min_offset = offset;
8467 if (offset > max_offset)
8468 max_offset = offset;
8470 reloc_range_list_update_range (relevant_relocs, min_offset,
8472 n = relevant_relocs->n_list;
8473 entry = &relevant_relocs->list_root;
8478 relevant_relocs = NULL;
8481 for (i = 0; i < n; i++)
8484 bfd_vma orig_self_offset, orig_target_offset;
8485 bfd_vma self_offset, target_offset;
8487 reloc_howto_type *howto;
8488 int self_removed_bytes, target_removed_bytes;
8490 if (relevant_relocs)
8492 entry = entry->next;
8497 irel = internal_relocs + i;
8499 r_type = ELF32_R_TYPE (irel->r_info);
8501 howto = &elf_howto_table[r_type];
8502 /* We maintain the required invariant: PC-relative relocations
8503 that fit before linking must fit after linking. Thus we only
8504 need to deal with relocations to the same section that are
8506 if (r_type == R_XTENSA_ASM_SIMPLIFY
8507 || r_type == R_XTENSA_32_PCREL
8508 || !howto->pc_relative)
8511 r_reloc_init (&r_rel, abfd, irel, contents,
8512 bfd_get_section_limit (abfd, sec));
8514 if (r_reloc_get_section (&r_rel) != sec)
8517 orig_self_offset = irel->r_offset;
8518 orig_target_offset = r_rel.target_offset;
8520 self_offset = orig_self_offset;
8521 target_offset = orig_target_offset;
8526 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8529 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8530 orig_target_offset);
8533 self_removed_bytes = 0;
8534 target_removed_bytes = 0;
8536 for (j = 0; j < constraint->action_count; ++j)
8538 proposed_action *action = &constraint->actions[j];
8539 bfd_vma offset = action->offset;
8540 int removed_bytes = action->removed_bytes;
8541 if (offset < orig_self_offset
8542 || (offset == orig_self_offset && action->action == ta_fill
8543 && action->removed_bytes < 0))
8544 self_removed_bytes += removed_bytes;
8545 if (offset < orig_target_offset
8546 || (offset == orig_target_offset && action->action == ta_fill
8547 && action->removed_bytes < 0))
8548 target_removed_bytes += removed_bytes;
8550 self_offset -= self_removed_bytes;
8551 target_offset -= target_removed_bytes;
8553 /* Try to encode it. Get the operand and check. */
8554 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
8556 /* None of the current alternate relocs are PC-relative,
8557 and only PC-relative relocs matter here. */
8561 xtensa_opcode opcode;
8564 if (relevant_relocs)
8566 opcode = entry->opcode;
8567 opnum = entry->opnum;
8572 opcode = reloc_opcodes[relevant_relocs ?
8573 (unsigned)(entry - relevant_relocs->reloc) : i];
8575 opcode = get_relocation_opcode (abfd, sec, contents, irel);
8576 if (opcode == XTENSA_UNDEFINED)
8582 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
8583 if (opnum == XTENSA_UNDEFINED)
8590 if (!pcrel_reloc_fits (opcode, opnum, self_offset, target_offset))
8599 free_xlate_map (xmap);
8606 check_section_ebb_reduces (const ebb_constraint *constraint)
8611 for (i = 0; i < constraint->action_count; i++)
8613 const proposed_action *action = &constraint->actions[i];
8614 if (action->do_action)
8615 removed += action->removed_bytes;
8625 text_action_add_proposed (text_action_list *l,
8626 const ebb_constraint *ebb_table,
8631 for (i = 0; i < ebb_table->action_count; i++)
8633 proposed_action *action = &ebb_table->actions[i];
8635 if (!action->do_action)
8637 switch (action->action)
8639 case ta_remove_insn:
8640 case ta_remove_longcall:
8641 case ta_convert_longcall:
8642 case ta_narrow_insn:
8645 case ta_remove_literal:
8646 text_action_add (l, action->action, sec, action->offset,
8647 action->removed_bytes);
8660 compute_fill_extra_space (property_table_entry *entry)
8662 int fill_extra_space;
8667 if ((entry->flags & XTENSA_PROP_UNREACHABLE) == 0)
8670 fill_extra_space = entry->size;
8671 if ((entry->flags & XTENSA_PROP_ALIGN) != 0)
8673 /* Fill bytes for alignment:
8674 (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
8675 int pow = GET_XTENSA_PROP_ALIGNMENT (entry->flags);
8676 int nsm = (1 << pow) - 1;
8677 bfd_vma addr = entry->address + entry->size;
8678 bfd_vma align_fill = nsm - ((addr + nsm) & nsm);
8679 fill_extra_space += align_fill;
8681 return fill_extra_space;
8685 /* First relaxation pass. */
8687 /* If the section contains relaxable literals, check each literal to
8688 see if it has the same value as another literal that has already
8689 been seen, either in the current section or a previous one. If so,
8690 add an entry to the per-section list of removed literals. The
8691 actual changes are deferred until the next pass. */
8694 compute_removed_literals (bfd *abfd,
8696 struct bfd_link_info *link_info,
8697 value_map_hash_table *values)
8699 xtensa_relax_info *relax_info;
8701 Elf_Internal_Rela *internal_relocs;
8702 source_reloc *src_relocs, *rel;
8703 bfd_boolean ok = TRUE;
8704 property_table_entry *prop_table = NULL;
8707 bfd_boolean last_loc_is_prev = FALSE;
8708 bfd_vma last_target_offset = 0;
8709 section_cache_t target_sec_cache;
8710 bfd_size_type sec_size;
8712 init_section_cache (&target_sec_cache);
8714 /* Do nothing if it is not a relaxable literal section. */
8715 relax_info = get_xtensa_relax_info (sec);
8716 BFD_ASSERT (relax_info);
8717 if (!relax_info->is_relaxable_literal_section)
8720 internal_relocs = retrieve_internal_relocs (abfd, sec,
8721 link_info->keep_memory);
8723 sec_size = bfd_get_section_limit (abfd, sec);
8724 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
8725 if (contents == NULL && sec_size != 0)
8731 /* Sort the source_relocs by target offset. */
8732 src_relocs = relax_info->src_relocs;
8733 qsort (src_relocs, relax_info->src_count,
8734 sizeof (source_reloc), source_reloc_compare);
8735 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
8736 internal_reloc_compare);
8738 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
8739 XTENSA_PROP_SEC_NAME, FALSE);
8747 for (i = 0; i < relax_info->src_count; i++)
8749 Elf_Internal_Rela *irel = NULL;
8751 rel = &src_relocs[i];
8752 if (get_l32r_opcode () != rel->opcode)
8754 irel = get_irel_at_offset (sec, internal_relocs,
8755 rel->r_rel.target_offset);
8757 /* If the relocation on this is not a simple R_XTENSA_32 or
8758 R_XTENSA_PLT then do not consider it. This may happen when
8759 the difference of two symbols is used in a literal. */
8760 if (irel && (ELF32_R_TYPE (irel->r_info) != R_XTENSA_32
8761 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_PLT))
8764 /* If the target_offset for this relocation is the same as the
8765 previous relocation, then we've already considered whether the
8766 literal can be coalesced. Skip to the next one.... */
8767 if (i != 0 && prev_i != -1
8768 && src_relocs[i-1].r_rel.target_offset == rel->r_rel.target_offset)
8772 if (last_loc_is_prev &&
8773 last_target_offset + 4 != rel->r_rel.target_offset)
8774 last_loc_is_prev = FALSE;
8776 /* Check if the relocation was from an L32R that is being removed
8777 because a CALLX was converted to a direct CALL, and check if
8778 there are no other relocations to the literal. */
8779 if (is_removable_literal (rel, i, src_relocs, relax_info->src_count,
8780 sec, prop_table, ptblsize))
8782 if (!remove_dead_literal (abfd, sec, link_info, internal_relocs,
8783 irel, rel, prop_table, ptblsize))
8788 last_target_offset = rel->r_rel.target_offset;
8792 if (!identify_literal_placement (abfd, sec, contents, link_info,
8794 &last_loc_is_prev, irel,
8795 relax_info->src_count - i, rel,
8796 prop_table, ptblsize,
8797 &target_sec_cache, rel->is_abs_literal))
8802 last_target_offset = rel->r_rel.target_offset;
8806 print_removed_literals (stderr, &relax_info->removed_list);
8807 print_action_list (stderr, &relax_info->action_list);
8813 free_section_cache (&target_sec_cache);
8815 release_contents (sec, contents);
8816 release_internal_relocs (sec, internal_relocs);
8821 static Elf_Internal_Rela *
8822 get_irel_at_offset (asection *sec,
8823 Elf_Internal_Rela *internal_relocs,
8827 Elf_Internal_Rela *irel;
8829 Elf_Internal_Rela key;
8831 if (!internal_relocs)
8834 key.r_offset = offset;
8835 irel = bsearch (&key, internal_relocs, sec->reloc_count,
8836 sizeof (Elf_Internal_Rela), internal_reloc_matches);
8840 /* bsearch does not guarantee which will be returned if there are
8841 multiple matches. We need the first that is not an alignment. */
8842 i = irel - internal_relocs;
8845 if (internal_relocs[i-1].r_offset != offset)
8849 for ( ; i < sec->reloc_count; i++)
8851 irel = &internal_relocs[i];
8852 r_type = ELF32_R_TYPE (irel->r_info);
8853 if (irel->r_offset == offset && r_type != R_XTENSA_NONE)
8862 is_removable_literal (const source_reloc *rel,
8864 const source_reloc *src_relocs,
8867 property_table_entry *prop_table,
8870 const source_reloc *curr_rel;
8871 property_table_entry *entry;
8876 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8877 sec->vma + rel->r_rel.target_offset);
8878 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
8881 for (++i; i < src_count; ++i)
8883 curr_rel = &src_relocs[i];
8884 /* If all others have the same target offset.... */
8885 if (curr_rel->r_rel.target_offset != rel->r_rel.target_offset)
8888 if (!curr_rel->is_null
8889 && !xtensa_is_property_section (curr_rel->source_sec)
8890 && !(curr_rel->source_sec->flags & SEC_DEBUGGING))
8898 remove_dead_literal (bfd *abfd,
8900 struct bfd_link_info *link_info,
8901 Elf_Internal_Rela *internal_relocs,
8902 Elf_Internal_Rela *irel,
8904 property_table_entry *prop_table,
8907 property_table_entry *entry;
8908 xtensa_relax_info *relax_info;
8910 relax_info = get_xtensa_relax_info (sec);
8914 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8915 sec->vma + rel->r_rel.target_offset);
8917 /* Mark the unused literal so that it will be removed. */
8918 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
8920 text_action_add (&relax_info->action_list,
8921 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
8923 /* If the section is 4-byte aligned, do not add fill. */
8924 if (sec->alignment_power > 2)
8926 int fill_extra_space;
8927 bfd_vma entry_sec_offset;
8929 property_table_entry *the_add_entry;
8933 entry_sec_offset = entry->address - sec->vma + entry->size;
8935 entry_sec_offset = rel->r_rel.target_offset + 4;
8937 /* If the literal range is at the end of the section,
8939 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8941 fill_extra_space = compute_fill_extra_space (the_add_entry);
8943 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
8944 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
8945 -4, fill_extra_space);
8947 adjust_fill_action (fa, removed_diff);
8949 text_action_add (&relax_info->action_list,
8950 ta_fill, sec, entry_sec_offset, removed_diff);
8953 /* Zero out the relocation on this literal location. */
8956 if (elf_hash_table (link_info)->dynamic_sections_created)
8957 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
8959 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
8960 pin_internal_relocs (sec, internal_relocs);
8963 /* Do not modify "last_loc_is_prev". */
8969 identify_literal_placement (bfd *abfd,
8972 struct bfd_link_info *link_info,
8973 value_map_hash_table *values,
8974 bfd_boolean *last_loc_is_prev_p,
8975 Elf_Internal_Rela *irel,
8976 int remaining_src_rels,
8978 property_table_entry *prop_table,
8980 section_cache_t *target_sec_cache,
8981 bfd_boolean is_abs_literal)
8985 xtensa_relax_info *relax_info;
8986 bfd_boolean literal_placed = FALSE;
8988 unsigned long value;
8989 bfd_boolean final_static_link;
8990 bfd_size_type sec_size;
8992 relax_info = get_xtensa_relax_info (sec);
8996 sec_size = bfd_get_section_limit (abfd, sec);
8999 (!bfd_link_relocatable (link_info)
9000 && !elf_hash_table (link_info)->dynamic_sections_created);
9002 /* The placement algorithm first checks to see if the literal is
9003 already in the value map. If so and the value map is reachable
9004 from all uses, then the literal is moved to that location. If
9005 not, then we identify the last location where a fresh literal was
9006 placed. If the literal can be safely moved there, then we do so.
9007 If not, then we assume that the literal is not to move and leave
9008 the literal where it is, marking it as the last literal
9011 /* Find the literal value. */
9013 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9016 BFD_ASSERT (rel->r_rel.target_offset < sec_size);
9017 value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
9019 init_literal_value (&val, &r_rel, value, is_abs_literal);
9021 /* Check if we've seen another literal with the same value that
9022 is in the same output section. */
9023 val_map = value_map_get_cached_value (values, &val, final_static_link);
9026 && (r_reloc_get_section (&val_map->loc)->output_section
9027 == sec->output_section)
9028 && relocations_reach (rel, remaining_src_rels, &val_map->loc)
9029 && coalesce_shared_literal (sec, rel, prop_table, ptblsize, val_map))
9031 /* No change to last_loc_is_prev. */
9032 literal_placed = TRUE;
9035 /* For relocatable links, do not try to move literals. To do it
9036 correctly might increase the number of relocations in an input
9037 section making the default relocatable linking fail. */
9038 if (!bfd_link_relocatable (link_info) && !literal_placed
9039 && values->has_last_loc && !(*last_loc_is_prev_p))
9041 asection *target_sec = r_reloc_get_section (&values->last_loc);
9042 if (target_sec && target_sec->output_section == sec->output_section)
9044 /* Increment the virtual offset. */
9045 r_reloc try_loc = values->last_loc;
9046 try_loc.virtual_offset += 4;
9048 /* There is a last loc that was in the same output section. */
9049 if (relocations_reach (rel, remaining_src_rels, &try_loc)
9050 && move_shared_literal (sec, link_info, rel,
9051 prop_table, ptblsize,
9052 &try_loc, &val, target_sec_cache))
9054 values->last_loc.virtual_offset += 4;
9055 literal_placed = TRUE;
9057 val_map = add_value_map (values, &val, &try_loc,
9060 val_map->loc = try_loc;
9065 if (!literal_placed)
9067 /* Nothing worked, leave the literal alone but update the last loc. */
9068 values->has_last_loc = TRUE;
9069 values->last_loc = rel->r_rel;
9071 val_map = add_value_map (values, &val, &rel->r_rel, final_static_link);
9073 val_map->loc = rel->r_rel;
9074 *last_loc_is_prev_p = TRUE;
9081 /* Check if the original relocations (presumably on L32R instructions)
9082 identified by reloc[0..N] can be changed to reference the literal
9083 identified by r_rel. If r_rel is out of range for any of the
9084 original relocations, then we don't want to coalesce the original
9085 literal with the one at r_rel. We only check reloc[0..N], where the
9086 offsets are all the same as for reloc[0] (i.e., they're all
9087 referencing the same literal) and where N is also bounded by the
9088 number of remaining entries in the "reloc" array. The "reloc" array
9089 is sorted by target offset so we know all the entries for the same
9090 literal will be contiguous. */
9093 relocations_reach (source_reloc *reloc,
9094 int remaining_relocs,
9095 const r_reloc *r_rel)
9097 bfd_vma from_offset, source_address, dest_address;
9101 if (!r_reloc_is_defined (r_rel))
9104 sec = r_reloc_get_section (r_rel);
9105 from_offset = reloc[0].r_rel.target_offset;
9107 for (i = 0; i < remaining_relocs; i++)
9109 if (reloc[i].r_rel.target_offset != from_offset)
9112 /* Ignore relocations that have been removed. */
9113 if (reloc[i].is_null)
9116 /* The original and new output section for these must be the same
9117 in order to coalesce. */
9118 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
9119 != sec->output_section)
9122 /* Absolute literals in the same output section can always be
9124 if (reloc[i].is_abs_literal)
9127 /* A literal with no PC-relative relocations can be moved anywhere. */
9128 if (reloc[i].opnd != -1)
9130 /* Otherwise, check to see that it fits. */
9131 source_address = (reloc[i].source_sec->output_section->vma
9132 + reloc[i].source_sec->output_offset
9133 + reloc[i].r_rel.rela.r_offset);
9134 dest_address = (sec->output_section->vma
9135 + sec->output_offset
9136 + r_rel->target_offset);
9138 if (!pcrel_reloc_fits (reloc[i].opcode, reloc[i].opnd,
9139 source_address, dest_address))
9148 /* Move a literal to another literal location because it is
9149 the same as the other literal value. */
9152 coalesce_shared_literal (asection *sec,
9154 property_table_entry *prop_table,
9158 property_table_entry *entry;
9160 property_table_entry *the_add_entry;
9162 xtensa_relax_info *relax_info;
9164 relax_info = get_xtensa_relax_info (sec);
9168 entry = elf_xtensa_find_property_entry
9169 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9170 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
9173 /* Mark that the literal will be coalesced. */
9174 add_removed_literal (&relax_info->removed_list, &rel->r_rel, &val_map->loc);
9176 text_action_add (&relax_info->action_list,
9177 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9179 /* If the section is 4-byte aligned, do not add fill. */
9180 if (sec->alignment_power > 2)
9182 int fill_extra_space;
9183 bfd_vma entry_sec_offset;
9186 entry_sec_offset = entry->address - sec->vma + entry->size;
9188 entry_sec_offset = rel->r_rel.target_offset + 4;
9190 /* If the literal range is at the end of the section,
9192 fill_extra_space = 0;
9193 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9195 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9196 fill_extra_space = the_add_entry->size;
9198 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9199 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9200 -4, fill_extra_space);
9202 adjust_fill_action (fa, removed_diff);
9204 text_action_add (&relax_info->action_list,
9205 ta_fill, sec, entry_sec_offset, removed_diff);
9212 /* Move a literal to another location. This may actually increase the
9213 total amount of space used because of alignments so we need to do
9214 this carefully. Also, it may make a branch go out of range. */
9217 move_shared_literal (asection *sec,
9218 struct bfd_link_info *link_info,
9220 property_table_entry *prop_table,
9222 const r_reloc *target_loc,
9223 const literal_value *lit_value,
9224 section_cache_t *target_sec_cache)
9226 property_table_entry *the_add_entry, *src_entry, *target_entry = NULL;
9227 text_action *fa, *target_fa;
9229 xtensa_relax_info *relax_info, *target_relax_info;
9230 asection *target_sec;
9232 ebb_constraint ebb_table;
9233 bfd_boolean relocs_fit;
9235 /* If this routine always returns FALSE, the literals that cannot be
9236 coalesced will not be moved. */
9237 if (elf32xtensa_no_literal_movement)
9240 relax_info = get_xtensa_relax_info (sec);
9244 target_sec = r_reloc_get_section (target_loc);
9245 target_relax_info = get_xtensa_relax_info (target_sec);
9247 /* Literals to undefined sections may not be moved because they
9248 must report an error. */
9249 if (bfd_is_und_section (target_sec))
9252 src_entry = elf_xtensa_find_property_entry
9253 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9255 if (!section_cache_section (target_sec_cache, target_sec, link_info))
9258 target_entry = elf_xtensa_find_property_entry
9259 (target_sec_cache->ptbl, target_sec_cache->pte_count,
9260 target_sec->vma + target_loc->target_offset);
9265 /* Make sure that we have not broken any branches. */
9268 init_ebb_constraint (&ebb_table);
9269 ebb = &ebb_table.ebb;
9270 init_ebb (ebb, target_sec_cache->sec, target_sec_cache->contents,
9271 target_sec_cache->content_length,
9272 target_sec_cache->ptbl, target_sec_cache->pte_count,
9273 target_sec_cache->relocs, target_sec_cache->reloc_count);
9275 /* Propose to add 4 bytes + worst-case alignment size increase to
9277 ebb_propose_action (&ebb_table, EBB_NO_ALIGN, 0,
9278 ta_fill, target_loc->target_offset,
9279 -4 - (1 << target_sec->alignment_power), TRUE);
9281 /* Check all of the PC-relative relocations to make sure they still fit. */
9282 relocs_fit = check_section_ebb_pcrels_fit (target_sec->owner, target_sec,
9283 target_sec_cache->contents,
9284 target_sec_cache->relocs, NULL,
9290 text_action_add_literal (&target_relax_info->action_list,
9291 ta_add_literal, target_loc, lit_value, -4);
9293 if (target_sec->alignment_power > 2 && target_entry != src_entry)
9295 /* May need to add or remove some fill to maintain alignment. */
9296 int fill_extra_space;
9297 bfd_vma entry_sec_offset;
9300 target_entry->address - target_sec->vma + target_entry->size;
9302 /* If the literal range is at the end of the section,
9304 fill_extra_space = 0;
9306 elf_xtensa_find_property_entry (target_sec_cache->ptbl,
9307 target_sec_cache->pte_count,
9309 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9310 fill_extra_space = the_add_entry->size;
9312 target_fa = find_fill_action (&target_relax_info->action_list,
9313 target_sec, entry_sec_offset);
9314 removed_diff = compute_removed_action_diff (target_fa, target_sec,
9315 entry_sec_offset, 4,
9318 adjust_fill_action (target_fa, removed_diff);
9320 text_action_add (&target_relax_info->action_list,
9321 ta_fill, target_sec, entry_sec_offset, removed_diff);
9324 /* Mark that the literal will be moved to the new location. */
9325 add_removed_literal (&relax_info->removed_list, &rel->r_rel, target_loc);
9327 /* Remove the literal. */
9328 text_action_add (&relax_info->action_list,
9329 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9331 /* If the section is 4-byte aligned, do not add fill. */
9332 if (sec->alignment_power > 2 && target_entry != src_entry)
9334 int fill_extra_space;
9335 bfd_vma entry_sec_offset;
9338 entry_sec_offset = src_entry->address - sec->vma + src_entry->size;
9340 entry_sec_offset = rel->r_rel.target_offset+4;
9342 /* If the literal range is at the end of the section,
9344 fill_extra_space = 0;
9345 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9347 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9348 fill_extra_space = the_add_entry->size;
9350 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9351 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9352 -4, fill_extra_space);
9354 adjust_fill_action (fa, removed_diff);
9356 text_action_add (&relax_info->action_list,
9357 ta_fill, sec, entry_sec_offset, removed_diff);
9364 /* Second relaxation pass. */
9367 action_remove_bytes_fn (splay_tree_node node, void *p)
9369 bfd_size_type *final_size = p;
9370 text_action *action = (text_action *)node->value;
9372 *final_size -= action->removed_bytes;
9376 /* Modify all of the relocations to point to the right spot, and if this
9377 is a relaxable section, delete the unwanted literals and fix the
9381 relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
9383 Elf_Internal_Rela *internal_relocs;
9384 xtensa_relax_info *relax_info;
9386 bfd_boolean ok = TRUE;
9388 bfd_boolean rv = FALSE;
9389 bfd_boolean virtual_action;
9390 bfd_size_type sec_size;
9392 sec_size = bfd_get_section_limit (abfd, sec);
9393 relax_info = get_xtensa_relax_info (sec);
9394 BFD_ASSERT (relax_info);
9396 /* First translate any of the fixes that have been added already. */
9397 translate_section_fixes (sec);
9399 /* Handle property sections (e.g., literal tables) specially. */
9400 if (xtensa_is_property_section (sec))
9402 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
9403 return relax_property_section (abfd, sec, link_info);
9406 internal_relocs = retrieve_internal_relocs (abfd, sec,
9407 link_info->keep_memory);
9408 if (!internal_relocs && !action_list_count (&relax_info->action_list))
9411 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
9412 if (contents == NULL && sec_size != 0)
9418 if (internal_relocs)
9420 for (i = 0; i < sec->reloc_count; i++)
9422 Elf_Internal_Rela *irel;
9423 xtensa_relax_info *target_relax_info;
9424 bfd_vma source_offset, old_source_offset;
9427 asection *target_sec;
9429 /* Locally change the source address.
9430 Translate the target to the new target address.
9431 If it points to this section and has been removed,
9435 irel = &internal_relocs[i];
9436 source_offset = irel->r_offset;
9437 old_source_offset = source_offset;
9439 r_type = ELF32_R_TYPE (irel->r_info);
9440 r_reloc_init (&r_rel, abfd, irel, contents,
9441 bfd_get_section_limit (abfd, sec));
9443 /* If this section could have changed then we may need to
9444 change the relocation's offset. */
9446 if (relax_info->is_relaxable_literal_section
9447 || relax_info->is_relaxable_asm_section)
9449 pin_internal_relocs (sec, internal_relocs);
9451 if (r_type != R_XTENSA_NONE
9452 && find_removed_literal (&relax_info->removed_list,
9455 /* Remove this relocation. */
9456 if (elf_hash_table (link_info)->dynamic_sections_created)
9457 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
9458 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
9459 irel->r_offset = offset_with_removed_text_map
9460 (&relax_info->action_list, irel->r_offset);
9464 if (r_type == R_XTENSA_ASM_SIMPLIFY)
9466 text_action *action =
9467 find_insn_action (&relax_info->action_list,
9469 if (action && (action->action == ta_convert_longcall
9470 || action->action == ta_remove_longcall))
9472 bfd_reloc_status_type retval;
9473 char *error_message = NULL;
9475 retval = contract_asm_expansion (contents, sec_size,
9476 irel, &error_message);
9477 if (retval != bfd_reloc_ok)
9479 (*link_info->callbacks->reloc_dangerous)
9480 (link_info, error_message, abfd, sec,
9484 /* Update the action so that the code that moves
9485 the contents will do the right thing. */
9486 /* ta_remove_longcall and ta_remove_insn actions are
9487 grouped together in the tree as well as
9488 ta_convert_longcall and ta_none, so that changes below
9489 can be done w/o removing and reinserting action into
9492 if (action->action == ta_remove_longcall)
9493 action->action = ta_remove_insn;
9495 action->action = ta_none;
9496 /* Refresh the info in the r_rel. */
9497 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9498 r_type = ELF32_R_TYPE (irel->r_info);
9502 source_offset = offset_with_removed_text_map
9503 (&relax_info->action_list, irel->r_offset);
9504 irel->r_offset = source_offset;
9507 /* If the target section could have changed then
9508 we may need to change the relocation's target offset. */
9510 target_sec = r_reloc_get_section (&r_rel);
9512 /* For a reference to a discarded section from a DWARF section,
9513 i.e., where action_discarded is PRETEND, the symbol will
9514 eventually be modified to refer to the kept section (at least if
9515 the kept and discarded sections are the same size). Anticipate
9516 that here and adjust things accordingly. */
9517 if (! elf_xtensa_ignore_discarded_relocs (sec)
9518 && elf_xtensa_action_discarded (sec) == PRETEND
9519 && sec->sec_info_type != SEC_INFO_TYPE_STABS
9520 && target_sec != NULL
9521 && discarded_section (target_sec))
9523 /* It would be natural to call _bfd_elf_check_kept_section
9524 here, but it's not exported from elflink.c. It's also a
9525 fairly expensive check. Adjusting the relocations to the
9526 discarded section is fairly harmless; it will only adjust
9527 some addends and difference values. If it turns out that
9528 _bfd_elf_check_kept_section fails later, it won't matter,
9529 so just compare the section names to find the right group
9531 asection *kept = target_sec->kept_section;
9534 if ((kept->flags & SEC_GROUP) != 0)
9536 asection *first = elf_next_in_group (kept);
9537 asection *s = first;
9542 if (strcmp (s->name, target_sec->name) == 0)
9547 s = elf_next_in_group (s);
9554 && ((target_sec->rawsize != 0
9555 ? target_sec->rawsize : target_sec->size)
9556 == (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9560 target_relax_info = get_xtensa_relax_info (target_sec);
9561 if (target_relax_info
9562 && (target_relax_info->is_relaxable_literal_section
9563 || target_relax_info->is_relaxable_asm_section))
9566 target_sec = translate_reloc (&r_rel, &new_reloc, target_sec);
9568 if (r_type == R_XTENSA_DIFF8
9569 || r_type == R_XTENSA_DIFF16
9570 || r_type == R_XTENSA_DIFF32)
9572 bfd_signed_vma diff_value = 0;
9573 bfd_vma new_end_offset, diff_mask = 0;
9575 if (bfd_get_section_limit (abfd, sec) < old_source_offset)
9577 (*link_info->callbacks->reloc_dangerous)
9578 (link_info, _("invalid relocation address"),
9579 abfd, sec, old_source_offset);
9585 case R_XTENSA_DIFF8:
9587 bfd_get_signed_8 (abfd, &contents[old_source_offset]);
9589 case R_XTENSA_DIFF16:
9591 bfd_get_signed_16 (abfd, &contents[old_source_offset]);
9593 case R_XTENSA_DIFF32:
9595 bfd_get_signed_32 (abfd, &contents[old_source_offset]);
9599 new_end_offset = offset_with_removed_text_map
9600 (&target_relax_info->action_list,
9601 r_rel.target_offset + diff_value);
9602 diff_value = new_end_offset - new_reloc.target_offset;
9606 case R_XTENSA_DIFF8:
9608 bfd_put_signed_8 (abfd, diff_value,
9609 &contents[old_source_offset]);
9611 case R_XTENSA_DIFF16:
9613 bfd_put_signed_16 (abfd, diff_value,
9614 &contents[old_source_offset]);
9616 case R_XTENSA_DIFF32:
9617 diff_mask = 0x7fffffff;
9618 bfd_put_signed_32 (abfd, diff_value,
9619 &contents[old_source_offset]);
9623 /* Check for overflow. Sign bits must be all zeroes or all ones */
9624 if ((diff_value & ~diff_mask) != 0 &&
9625 (diff_value & ~diff_mask) != (-1 & ~diff_mask))
9627 (*link_info->callbacks->reloc_dangerous)
9628 (link_info, _("overflow after relaxation"),
9629 abfd, sec, old_source_offset);
9633 pin_contents (sec, contents);
9636 /* If the relocation still references a section in the same
9637 input file, modify the relocation directly instead of
9638 adding a "fix" record. */
9639 if (target_sec->owner == abfd)
9641 unsigned r_symndx = ELF32_R_SYM (new_reloc.rela.r_info);
9642 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
9643 irel->r_addend = new_reloc.rela.r_addend;
9644 pin_internal_relocs (sec, internal_relocs);
9648 bfd_vma addend_displacement;
9651 addend_displacement =
9652 new_reloc.target_offset + new_reloc.virtual_offset;
9653 fix = reloc_bfd_fix_init (sec, source_offset, r_type,
9655 addend_displacement, TRUE);
9662 if ((relax_info->is_relaxable_literal_section
9663 || relax_info->is_relaxable_asm_section)
9664 && action_list_count (&relax_info->action_list))
9666 /* Walk through the planned actions and build up a table
9667 of move, copy and fill records. Use the move, copy and
9668 fill records to perform the actions once. */
9670 bfd_size_type final_size, copy_size, orig_insn_size;
9671 bfd_byte *scratch = NULL;
9672 bfd_byte *dup_contents = NULL;
9673 bfd_size_type orig_size = sec->size;
9674 bfd_vma orig_dot = 0;
9675 bfd_vma orig_dot_copied = 0; /* Byte copied already from
9676 orig dot in physical memory. */
9677 bfd_vma orig_dot_vo = 0; /* Virtual offset from orig_dot. */
9678 bfd_vma dup_dot = 0;
9680 text_action *action;
9682 final_size = sec->size;
9684 splay_tree_foreach (relax_info->action_list.tree,
9685 action_remove_bytes_fn, &final_size);
9686 scratch = (bfd_byte *) bfd_zmalloc (final_size);
9687 dup_contents = (bfd_byte *) bfd_zmalloc (final_size);
9689 /* The dot is the current fill location. */
9691 print_action_list (stderr, &relax_info->action_list);
9694 for (action = action_first (&relax_info->action_list); action;
9695 action = action_next (&relax_info->action_list, action))
9697 virtual_action = FALSE;
9698 if (action->offset > orig_dot)
9700 orig_dot += orig_dot_copied;
9701 orig_dot_copied = 0;
9703 /* Out of the virtual world. */
9706 if (action->offset > orig_dot)
9708 copy_size = action->offset - orig_dot;
9709 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9710 orig_dot += copy_size;
9711 dup_dot += copy_size;
9712 BFD_ASSERT (action->offset == orig_dot);
9714 else if (action->offset < orig_dot)
9716 if (action->action == ta_fill
9717 && action->offset - action->removed_bytes == orig_dot)
9719 /* This is OK because the fill only effects the dup_dot. */
9721 else if (action->action == ta_add_literal)
9723 /* TBD. Might need to handle this. */
9726 if (action->offset == orig_dot)
9728 if (action->virtual_offset > orig_dot_vo)
9730 if (orig_dot_vo == 0)
9732 /* Need to copy virtual_offset bytes. Probably four. */
9733 copy_size = action->virtual_offset - orig_dot_vo;
9734 memmove (&dup_contents[dup_dot],
9735 &contents[orig_dot], copy_size);
9736 orig_dot_copied = copy_size;
9737 dup_dot += copy_size;
9739 virtual_action = TRUE;
9742 BFD_ASSERT (action->virtual_offset <= orig_dot_vo);
9744 switch (action->action)
9746 case ta_remove_literal:
9747 case ta_remove_insn:
9748 BFD_ASSERT (action->removed_bytes >= 0);
9749 orig_dot += action->removed_bytes;
9752 case ta_narrow_insn:
9755 memmove (scratch, &contents[orig_dot], orig_insn_size);
9756 BFD_ASSERT (action->removed_bytes == 1);
9757 rv = narrow_instruction (scratch, final_size, 0);
9759 memmove (&dup_contents[dup_dot], scratch, copy_size);
9760 orig_dot += orig_insn_size;
9761 dup_dot += copy_size;
9765 if (action->removed_bytes >= 0)
9766 orig_dot += action->removed_bytes;
9769 /* Already zeroed in dup_contents. Just bump the
9771 dup_dot += (-action->removed_bytes);
9776 BFD_ASSERT (action->removed_bytes == 0);
9779 case ta_convert_longcall:
9780 case ta_remove_longcall:
9781 /* These will be removed or converted before we get here. */
9788 memmove (scratch, &contents[orig_dot], orig_insn_size);
9789 BFD_ASSERT (action->removed_bytes == -1);
9790 rv = widen_instruction (scratch, final_size, 0);
9792 memmove (&dup_contents[dup_dot], scratch, copy_size);
9793 orig_dot += orig_insn_size;
9794 dup_dot += copy_size;
9797 case ta_add_literal:
9800 BFD_ASSERT (action->removed_bytes == -4);
9801 /* TBD -- place the literal value here and insert
9803 memset (&dup_contents[dup_dot], 0, 4);
9804 pin_internal_relocs (sec, internal_relocs);
9805 pin_contents (sec, contents);
9807 if (!move_literal (abfd, link_info, sec, dup_dot, dup_contents,
9808 relax_info, &internal_relocs, &action->value))
9812 orig_dot_vo += copy_size;
9814 orig_dot += orig_insn_size;
9815 dup_dot += copy_size;
9819 /* Not implemented yet. */
9824 BFD_ASSERT (dup_dot <= final_size);
9825 BFD_ASSERT (orig_dot <= orig_size);
9828 orig_dot += orig_dot_copied;
9829 orig_dot_copied = 0;
9831 if (orig_dot != orig_size)
9833 copy_size = orig_size - orig_dot;
9834 BFD_ASSERT (orig_size > orig_dot);
9835 BFD_ASSERT (dup_dot + copy_size == final_size);
9836 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9837 orig_dot += copy_size;
9838 dup_dot += copy_size;
9840 BFD_ASSERT (orig_size == orig_dot);
9841 BFD_ASSERT (final_size == dup_dot);
9843 /* Move the dup_contents back. */
9844 if (final_size > orig_size)
9846 /* Contents need to be reallocated. Swap the dup_contents into
9848 sec->contents = dup_contents;
9850 contents = dup_contents;
9851 pin_contents (sec, contents);
9855 BFD_ASSERT (final_size <= orig_size);
9856 memset (contents, 0, orig_size);
9857 memcpy (contents, dup_contents, final_size);
9858 free (dup_contents);
9861 pin_contents (sec, contents);
9863 if (sec->rawsize == 0)
9864 sec->rawsize = sec->size;
9865 sec->size = final_size;
9869 release_internal_relocs (sec, internal_relocs);
9870 release_contents (sec, contents);
9876 translate_section_fixes (asection *sec)
9878 xtensa_relax_info *relax_info;
9881 relax_info = get_xtensa_relax_info (sec);
9885 for (r = relax_info->fix_list; r != NULL; r = r->next)
9886 if (!translate_reloc_bfd_fix (r))
9893 /* Translate a fix given the mapping in the relax info for the target
9894 section. If it has already been translated, no work is required. */
9897 translate_reloc_bfd_fix (reloc_bfd_fix *fix)
9899 reloc_bfd_fix new_fix;
9901 xtensa_relax_info *relax_info;
9902 removed_literal *removed;
9903 bfd_vma new_offset, target_offset;
9905 if (fix->translated)
9908 sec = fix->target_sec;
9909 target_offset = fix->target_offset;
9911 relax_info = get_xtensa_relax_info (sec);
9914 fix->translated = TRUE;
9920 /* The fix does not need to be translated if the section cannot change. */
9921 if (!relax_info->is_relaxable_literal_section
9922 && !relax_info->is_relaxable_asm_section)
9924 fix->translated = TRUE;
9928 /* If the literal has been moved and this relocation was on an
9929 opcode, then the relocation should move to the new literal
9930 location. Otherwise, the relocation should move within the
9934 if (is_operand_relocation (fix->src_type))
9936 /* Check if the original relocation is against a literal being
9938 removed = find_removed_literal (&relax_info->removed_list,
9946 /* The fact that there is still a relocation to this literal indicates
9947 that the literal is being coalesced, not simply removed. */
9948 BFD_ASSERT (removed->to.abfd != NULL);
9950 /* This was moved to some other address (possibly another section). */
9951 new_sec = r_reloc_get_section (&removed->to);
9955 relax_info = get_xtensa_relax_info (sec);
9957 (!relax_info->is_relaxable_literal_section
9958 && !relax_info->is_relaxable_asm_section))
9960 target_offset = removed->to.target_offset;
9961 new_fix.target_sec = new_sec;
9962 new_fix.target_offset = target_offset;
9963 new_fix.translated = TRUE;
9968 target_offset = removed->to.target_offset;
9969 new_fix.target_sec = new_sec;
9972 /* The target address may have been moved within its section. */
9973 new_offset = offset_with_removed_text (&relax_info->action_list,
9976 new_fix.target_offset = new_offset;
9977 new_fix.target_offset = new_offset;
9978 new_fix.translated = TRUE;
9984 /* Fix up a relocation to take account of removed literals. */
9987 translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
9989 xtensa_relax_info *relax_info;
9990 removed_literal *removed;
9991 bfd_vma target_offset, base_offset;
9993 *new_rel = *orig_rel;
9995 if (!r_reloc_is_defined (orig_rel))
9998 relax_info = get_xtensa_relax_info (sec);
9999 BFD_ASSERT (relax_info && (relax_info->is_relaxable_literal_section
10000 || relax_info->is_relaxable_asm_section));
10002 target_offset = orig_rel->target_offset;
10005 if (is_operand_relocation (ELF32_R_TYPE (orig_rel->rela.r_info)))
10007 /* Check if the original relocation is against a literal being
10009 removed = find_removed_literal (&relax_info->removed_list,
10012 if (removed && removed->to.abfd)
10016 /* The fact that there is still a relocation to this literal indicates
10017 that the literal is being coalesced, not simply removed. */
10018 BFD_ASSERT (removed->to.abfd != NULL);
10020 /* This was moved to some other address
10021 (possibly in another section). */
10022 *new_rel = removed->to;
10023 new_sec = r_reloc_get_section (new_rel);
10024 if (new_sec != sec)
10027 relax_info = get_xtensa_relax_info (sec);
10029 || (!relax_info->is_relaxable_literal_section
10030 && !relax_info->is_relaxable_asm_section))
10033 target_offset = new_rel->target_offset;
10036 /* Find the base offset of the reloc symbol, excluding any addend from the
10037 reloc or from the section contents (for a partial_inplace reloc). Then
10038 find the adjusted values of the offsets due to relaxation. The base
10039 offset is needed to determine the change to the reloc's addend; the reloc
10040 addend should not be adjusted due to relaxations located before the base
10043 base_offset = r_reloc_get_target_offset (new_rel) - new_rel->rela.r_addend;
10044 if (base_offset <= target_offset)
10046 int base_removed = removed_by_actions_map (&relax_info->action_list,
10047 base_offset, FALSE);
10048 int addend_removed = removed_by_actions_map (&relax_info->action_list,
10049 target_offset, FALSE) -
10052 new_rel->target_offset = target_offset - base_removed - addend_removed;
10053 new_rel->rela.r_addend -= addend_removed;
10057 /* Handle a negative addend. The base offset comes first. */
10058 int tgt_removed = removed_by_actions_map (&relax_info->action_list,
10059 target_offset, FALSE);
10060 int addend_removed = removed_by_actions_map (&relax_info->action_list,
10061 base_offset, FALSE) -
10064 new_rel->target_offset = target_offset - tgt_removed;
10065 new_rel->rela.r_addend += addend_removed;
10072 /* For dynamic links, there may be a dynamic relocation for each
10073 literal. The number of dynamic relocations must be computed in
10074 size_dynamic_sections, which occurs before relaxation. When a
10075 literal is removed, this function checks if there is a corresponding
10076 dynamic relocation and shrinks the size of the appropriate dynamic
10077 relocation section accordingly. At this point, the contents of the
10078 dynamic relocation sections have not yet been filled in, so there's
10079 nothing else that needs to be done. */
10082 shrink_dynamic_reloc_sections (struct bfd_link_info *info,
10084 asection *input_section,
10085 Elf_Internal_Rela *rel)
10087 struct elf_xtensa_link_hash_table *htab;
10088 Elf_Internal_Shdr *symtab_hdr;
10089 struct elf_link_hash_entry **sym_hashes;
10090 unsigned long r_symndx;
10092 struct elf_link_hash_entry *h;
10093 bfd_boolean dynamic_symbol;
10095 htab = elf_xtensa_hash_table (info);
10099 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10100 sym_hashes = elf_sym_hashes (abfd);
10102 r_type = ELF32_R_TYPE (rel->r_info);
10103 r_symndx = ELF32_R_SYM (rel->r_info);
10105 if (r_symndx < symtab_hdr->sh_info)
10108 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
10110 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
10112 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
10113 && (input_section->flags & SEC_ALLOC) != 0
10114 && (dynamic_symbol || bfd_link_pic (info)))
10117 bfd_boolean is_plt = FALSE;
10119 if (dynamic_symbol && r_type == R_XTENSA_PLT)
10121 srel = htab->srelplt;
10125 srel = htab->srelgot;
10127 /* Reduce size of the .rela.* section by one reloc. */
10128 BFD_ASSERT (srel != NULL);
10129 BFD_ASSERT (srel->size >= sizeof (Elf32_External_Rela));
10130 srel->size -= sizeof (Elf32_External_Rela);
10134 asection *splt, *sgotplt, *srelgot;
10135 int reloc_index, chunk;
10137 /* Find the PLT reloc index of the entry being removed. This
10138 is computed from the size of ".rela.plt". It is needed to
10139 figure out which PLT chunk to resize. Usually "last index
10140 = size - 1" since the index starts at zero, but in this
10141 context, the size has just been decremented so there's no
10142 need to subtract one. */
10143 reloc_index = srel->size / sizeof (Elf32_External_Rela);
10145 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
10146 splt = elf_xtensa_get_plt_section (info, chunk);
10147 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
10148 BFD_ASSERT (splt != NULL && sgotplt != NULL);
10150 /* Check if an entire PLT chunk has just been eliminated. */
10151 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
10153 /* The two magic GOT entries for that chunk can go away. */
10154 srelgot = htab->srelgot;
10155 BFD_ASSERT (srelgot != NULL);
10156 srelgot->reloc_count -= 2;
10157 srelgot->size -= 2 * sizeof (Elf32_External_Rela);
10158 sgotplt->size -= 8;
10160 /* There should be only one entry left (and it will be
10162 BFD_ASSERT (sgotplt->size == 4);
10163 BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
10166 BFD_ASSERT (sgotplt->size >= 4);
10167 BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
10169 sgotplt->size -= 4;
10170 splt->size -= PLT_ENTRY_SIZE;
10176 /* Take an r_rel and move it to another section. This usually
10177 requires extending the interal_relocation array and pinning it. If
10178 the original r_rel is from the same BFD, we can complete this here.
10179 Otherwise, we add a fix record to let the final link fix the
10180 appropriate address. Contents and internal relocations for the
10181 section must be pinned after calling this routine. */
10184 move_literal (bfd *abfd,
10185 struct bfd_link_info *link_info,
10188 bfd_byte *contents,
10189 xtensa_relax_info *relax_info,
10190 Elf_Internal_Rela **internal_relocs_p,
10191 const literal_value *lit)
10193 Elf_Internal_Rela *new_relocs = NULL;
10194 size_t new_relocs_count = 0;
10195 Elf_Internal_Rela this_rela;
10196 const r_reloc *r_rel;
10198 r_rel = &lit->r_rel;
10199 BFD_ASSERT (elf_section_data (sec)->relocs == *internal_relocs_p);
10201 if (r_reloc_is_const (r_rel))
10202 bfd_put_32 (abfd, lit->value, contents + offset);
10207 reloc_bfd_fix *fix;
10208 unsigned insert_at;
10210 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
10212 /* This is the difficult case. We have to create a fix up. */
10213 this_rela.r_offset = offset;
10214 this_rela.r_info = ELF32_R_INFO (0, r_type);
10215 this_rela.r_addend =
10216 r_rel->target_offset - r_reloc_get_target_offset (r_rel);
10217 bfd_put_32 (abfd, lit->value, contents + offset);
10219 /* Currently, we cannot move relocations during a relocatable link. */
10220 BFD_ASSERT (!bfd_link_relocatable (link_info));
10221 fix = reloc_bfd_fix_init (sec, offset, r_type,
10222 r_reloc_get_section (r_rel),
10223 r_rel->target_offset + r_rel->virtual_offset,
10225 /* We also need to mark that relocations are needed here. */
10226 sec->flags |= SEC_RELOC;
10228 translate_reloc_bfd_fix (fix);
10229 /* This fix has not yet been translated. */
10230 add_fix (sec, fix);
10232 /* Add the relocation. If we have already allocated our own
10233 space for the relocations and we have room for more, then use
10234 it. Otherwise, allocate new space and move the literals. */
10235 insert_at = sec->reloc_count;
10236 for (i = 0; i < sec->reloc_count; ++i)
10238 if (this_rela.r_offset < (*internal_relocs_p)[i].r_offset)
10245 if (*internal_relocs_p != relax_info->allocated_relocs
10246 || sec->reloc_count + 1 > relax_info->allocated_relocs_count)
10248 BFD_ASSERT (relax_info->allocated_relocs == NULL
10249 || sec->reloc_count == relax_info->relocs_count);
10251 if (relax_info->allocated_relocs_count == 0)
10252 new_relocs_count = (sec->reloc_count + 2) * 2;
10254 new_relocs_count = (relax_info->allocated_relocs_count + 2) * 2;
10256 new_relocs = (Elf_Internal_Rela *)
10257 bfd_zmalloc (sizeof (Elf_Internal_Rela) * (new_relocs_count));
10261 /* We could handle this more quickly by finding the split point. */
10262 if (insert_at != 0)
10263 memcpy (new_relocs, *internal_relocs_p,
10264 insert_at * sizeof (Elf_Internal_Rela));
10266 new_relocs[insert_at] = this_rela;
10268 if (insert_at != sec->reloc_count)
10269 memcpy (new_relocs + insert_at + 1,
10270 (*internal_relocs_p) + insert_at,
10271 (sec->reloc_count - insert_at)
10272 * sizeof (Elf_Internal_Rela));
10274 if (*internal_relocs_p != relax_info->allocated_relocs)
10276 /* The first time we re-allocate, we can only free the
10277 old relocs if they were allocated with bfd_malloc.
10278 This is not true when keep_memory is in effect. */
10279 if (!link_info->keep_memory)
10280 free (*internal_relocs_p);
10283 free (*internal_relocs_p);
10284 relax_info->allocated_relocs = new_relocs;
10285 relax_info->allocated_relocs_count = new_relocs_count;
10286 elf_section_data (sec)->relocs = new_relocs;
10287 sec->reloc_count++;
10288 relax_info->relocs_count = sec->reloc_count;
10289 *internal_relocs_p = new_relocs;
10293 if (insert_at != sec->reloc_count)
10296 for (idx = sec->reloc_count; idx > insert_at; idx--)
10297 (*internal_relocs_p)[idx] = (*internal_relocs_p)[idx-1];
10299 (*internal_relocs_p)[insert_at] = this_rela;
10300 sec->reloc_count++;
10301 if (relax_info->allocated_relocs)
10302 relax_info->relocs_count = sec->reloc_count;
10309 /* This is similar to relax_section except that when a target is moved,
10310 we shift addresses up. We also need to modify the size. This
10311 algorithm does NOT allow for relocations into the middle of the
10312 property sections. */
10315 relax_property_section (bfd *abfd,
10317 struct bfd_link_info *link_info)
10319 Elf_Internal_Rela *internal_relocs;
10320 bfd_byte *contents;
10322 bfd_boolean ok = TRUE;
10323 bfd_boolean is_full_prop_section;
10324 size_t last_zfill_target_offset = 0;
10325 asection *last_zfill_target_sec = NULL;
10326 bfd_size_type sec_size;
10327 bfd_size_type entry_size;
10329 sec_size = bfd_get_section_limit (abfd, sec);
10330 internal_relocs = retrieve_internal_relocs (abfd, sec,
10331 link_info->keep_memory);
10332 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
10333 if (contents == NULL && sec_size != 0)
10339 is_full_prop_section = xtensa_is_proptable_section (sec);
10340 if (is_full_prop_section)
10345 if (internal_relocs)
10347 for (i = 0; i < sec->reloc_count; i++)
10349 Elf_Internal_Rela *irel;
10350 xtensa_relax_info *target_relax_info;
10352 asection *target_sec;
10354 bfd_byte *size_p, *flags_p;
10356 /* Locally change the source address.
10357 Translate the target to the new target address.
10358 If it points to this section and has been removed, MOVE IT.
10359 Also, don't forget to modify the associated SIZE at
10362 irel = &internal_relocs[i];
10363 r_type = ELF32_R_TYPE (irel->r_info);
10364 if (r_type == R_XTENSA_NONE)
10367 /* Find the literal value. */
10368 r_reloc_init (&val.r_rel, abfd, irel, contents, sec_size);
10369 size_p = &contents[irel->r_offset + 4];
10371 if (is_full_prop_section)
10372 flags_p = &contents[irel->r_offset + 8];
10373 BFD_ASSERT (irel->r_offset + entry_size <= sec_size);
10375 target_sec = r_reloc_get_section (&val.r_rel);
10376 target_relax_info = get_xtensa_relax_info (target_sec);
10378 if (target_relax_info
10379 && (target_relax_info->is_relaxable_literal_section
10380 || target_relax_info->is_relaxable_asm_section ))
10382 /* Translate the relocation's destination. */
10383 bfd_vma old_offset = val.r_rel.target_offset;
10384 bfd_vma new_offset;
10385 long old_size, new_size;
10386 int removed_by_old_offset =
10387 removed_by_actions_map (&target_relax_info->action_list,
10388 old_offset, FALSE);
10389 new_offset = old_offset - removed_by_old_offset;
10391 /* Assert that we are not out of bounds. */
10392 old_size = bfd_get_32 (abfd, size_p);
10393 new_size = old_size;
10397 /* Only the first zero-sized unreachable entry is
10398 allowed to expand. In this case the new offset
10399 should be the offset before the fill and the new
10400 size is the expansion size. For other zero-sized
10401 entries the resulting size should be zero with an
10402 offset before or after the fill address depending
10403 on whether the expanding unreachable entry
10405 if (last_zfill_target_sec == 0
10406 || last_zfill_target_sec != target_sec
10407 || last_zfill_target_offset != old_offset)
10409 bfd_vma new_end_offset = new_offset;
10411 /* Recompute the new_offset, but this time don't
10412 include any fill inserted by relaxation. */
10413 removed_by_old_offset =
10414 removed_by_actions_map (&target_relax_info->action_list,
10416 new_offset = old_offset - removed_by_old_offset;
10418 /* If it is not unreachable and we have not yet
10419 seen an unreachable at this address, place it
10420 before the fill address. */
10421 if (flags_p && (bfd_get_32 (abfd, flags_p)
10422 & XTENSA_PROP_UNREACHABLE) != 0)
10424 new_size = new_end_offset - new_offset;
10426 last_zfill_target_sec = target_sec;
10427 last_zfill_target_offset = old_offset;
10433 int removed_by_old_offset_size =
10434 removed_by_actions_map (&target_relax_info->action_list,
10435 old_offset + old_size, TRUE);
10436 new_size -= removed_by_old_offset_size - removed_by_old_offset;
10439 if (new_size != old_size)
10441 bfd_put_32 (abfd, new_size, size_p);
10442 pin_contents (sec, contents);
10445 if (new_offset != old_offset)
10447 bfd_vma diff = new_offset - old_offset;
10448 irel->r_addend += diff;
10449 pin_internal_relocs (sec, internal_relocs);
10455 /* Combine adjacent property table entries. This is also done in
10456 finish_dynamic_sections() but at that point it's too late to
10457 reclaim the space in the output section, so we do this twice. */
10459 if (internal_relocs && (!bfd_link_relocatable (link_info)
10460 || xtensa_is_littable_section (sec)))
10462 Elf_Internal_Rela *last_irel = NULL;
10463 Elf_Internal_Rela *irel, *next_rel, *rel_end;
10464 int removed_bytes = 0;
10466 flagword predef_flags;
10468 predef_flags = xtensa_get_property_predef_flags (sec);
10470 /* Walk over memory and relocations at the same time.
10471 This REQUIRES that the internal_relocs be sorted by offset. */
10472 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
10473 internal_reloc_compare);
10475 pin_internal_relocs (sec, internal_relocs);
10476 pin_contents (sec, contents);
10478 next_rel = internal_relocs;
10479 rel_end = internal_relocs + sec->reloc_count;
10481 BFD_ASSERT (sec->size % entry_size == 0);
10483 for (offset = 0; offset < sec->size; offset += entry_size)
10485 Elf_Internal_Rela *offset_rel, *extra_rel;
10486 bfd_vma bytes_to_remove, size, actual_offset;
10487 bfd_boolean remove_this_rel;
10490 /* Find the first relocation for the entry at the current offset.
10491 Adjust the offsets of any extra relocations for the previous
10496 for (irel = next_rel; irel < rel_end; irel++)
10498 if ((irel->r_offset == offset
10499 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10500 || irel->r_offset > offset)
10505 irel->r_offset -= removed_bytes;
10509 /* Find the next relocation (if there are any left). */
10513 for (irel = offset_rel + 1; irel < rel_end; irel++)
10515 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10523 /* Check if there are relocations on the current entry. There
10524 should usually be a relocation on the offset field. If there
10525 are relocations on the size or flags, then we can't optimize
10526 this entry. Also, find the next relocation to examine on the
10530 if (offset_rel->r_offset >= offset + entry_size)
10532 next_rel = offset_rel;
10533 /* There are no relocations on the current entry, but we
10534 might still be able to remove it if the size is zero. */
10537 else if (offset_rel->r_offset > offset
10539 && extra_rel->r_offset < offset + entry_size))
10541 /* There is a relocation on the size or flags, so we can't
10542 do anything with this entry. Continue with the next. */
10543 next_rel = offset_rel;
10548 BFD_ASSERT (offset_rel->r_offset == offset);
10549 offset_rel->r_offset -= removed_bytes;
10550 next_rel = offset_rel + 1;
10556 remove_this_rel = FALSE;
10557 bytes_to_remove = 0;
10558 actual_offset = offset - removed_bytes;
10559 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
10561 if (is_full_prop_section)
10562 flags = bfd_get_32 (abfd, &contents[actual_offset + 8]);
10564 flags = predef_flags;
10567 && (flags & XTENSA_PROP_ALIGN) == 0
10568 && (flags & XTENSA_PROP_UNREACHABLE) == 0)
10570 /* Always remove entries with zero size and no alignment. */
10571 bytes_to_remove = entry_size;
10573 remove_this_rel = TRUE;
10575 else if (offset_rel
10576 && ELF32_R_TYPE (offset_rel->r_info) == R_XTENSA_32)
10580 flagword old_flags;
10582 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
10583 bfd_vma old_address =
10584 (last_irel->r_addend
10585 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
10586 bfd_vma new_address =
10587 (offset_rel->r_addend
10588 + bfd_get_32 (abfd, &contents[actual_offset]));
10589 if (is_full_prop_section)
10590 old_flags = bfd_get_32
10591 (abfd, &contents[last_irel->r_offset + 8]);
10593 old_flags = predef_flags;
10595 if ((ELF32_R_SYM (offset_rel->r_info)
10596 == ELF32_R_SYM (last_irel->r_info))
10597 && old_address + old_size == new_address
10598 && old_flags == flags
10599 && (old_flags & XTENSA_PROP_INSN_BRANCH_TARGET) == 0
10600 && (old_flags & XTENSA_PROP_INSN_LOOP_TARGET) == 0)
10602 /* Fix the old size. */
10603 bfd_put_32 (abfd, old_size + size,
10604 &contents[last_irel->r_offset + 4]);
10605 bytes_to_remove = entry_size;
10606 remove_this_rel = TRUE;
10609 last_irel = offset_rel;
10612 last_irel = offset_rel;
10615 if (remove_this_rel)
10617 offset_rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
10618 offset_rel->r_offset = 0;
10621 if (bytes_to_remove != 0)
10623 removed_bytes += bytes_to_remove;
10624 if (offset + bytes_to_remove < sec->size)
10625 memmove (&contents[actual_offset],
10626 &contents[actual_offset + bytes_to_remove],
10627 sec->size - offset - bytes_to_remove);
10633 /* Fix up any extra relocations on the last entry. */
10634 for (irel = next_rel; irel < rel_end; irel++)
10635 irel->r_offset -= removed_bytes;
10637 /* Clear the removed bytes. */
10638 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
10640 if (sec->rawsize == 0)
10641 sec->rawsize = sec->size;
10642 sec->size -= removed_bytes;
10644 if (xtensa_is_littable_section (sec))
10646 asection *sgotloc = elf_xtensa_hash_table (link_info)->sgotloc;
10648 sgotloc->size -= removed_bytes;
10654 release_internal_relocs (sec, internal_relocs);
10655 release_contents (sec, contents);
10660 /* Third relaxation pass. */
10662 /* Change symbol values to account for removed literals. */
10665 relax_section_symbols (bfd *abfd, asection *sec)
10667 xtensa_relax_info *relax_info;
10668 unsigned int sec_shndx;
10669 Elf_Internal_Shdr *symtab_hdr;
10670 Elf_Internal_Sym *isymbuf;
10671 unsigned i, num_syms, num_locals;
10673 relax_info = get_xtensa_relax_info (sec);
10674 BFD_ASSERT (relax_info);
10676 if (!relax_info->is_relaxable_literal_section
10677 && !relax_info->is_relaxable_asm_section)
10680 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
10682 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10683 isymbuf = retrieve_local_syms (abfd);
10685 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
10686 num_locals = symtab_hdr->sh_info;
10688 /* Adjust the local symbols defined in this section. */
10689 for (i = 0; i < num_locals; i++)
10691 Elf_Internal_Sym *isym = &isymbuf[i];
10693 if (isym->st_shndx == sec_shndx)
10695 bfd_vma orig_addr = isym->st_value;
10696 int removed = removed_by_actions_map (&relax_info->action_list,
10699 isym->st_value -= removed;
10700 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
10702 removed_by_actions_map (&relax_info->action_list,
10703 orig_addr + isym->st_size, FALSE) -
10708 /* Now adjust the global symbols defined in this section. */
10709 for (i = 0; i < (num_syms - num_locals); i++)
10711 struct elf_link_hash_entry *sym_hash;
10713 sym_hash = elf_sym_hashes (abfd)[i];
10715 if (sym_hash->root.type == bfd_link_hash_warning)
10716 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
10718 if ((sym_hash->root.type == bfd_link_hash_defined
10719 || sym_hash->root.type == bfd_link_hash_defweak)
10720 && sym_hash->root.u.def.section == sec)
10722 bfd_vma orig_addr = sym_hash->root.u.def.value;
10723 int removed = removed_by_actions_map (&relax_info->action_list,
10726 sym_hash->root.u.def.value -= removed;
10728 if (sym_hash->type == STT_FUNC)
10730 removed_by_actions_map (&relax_info->action_list,
10731 orig_addr + sym_hash->size, FALSE) -
10740 /* "Fix" handling functions, called while performing relocations. */
10743 do_fix_for_relocatable_link (Elf_Internal_Rela *rel,
10745 asection *input_section,
10746 bfd_byte *contents)
10749 asection *sec, *old_sec;
10750 bfd_vma old_offset;
10751 int r_type = ELF32_R_TYPE (rel->r_info);
10752 reloc_bfd_fix *fix;
10754 if (r_type == R_XTENSA_NONE)
10757 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10761 r_reloc_init (&r_rel, input_bfd, rel, contents,
10762 bfd_get_section_limit (input_bfd, input_section));
10763 old_sec = r_reloc_get_section (&r_rel);
10764 old_offset = r_rel.target_offset;
10766 if (!old_sec || !r_reloc_is_defined (&r_rel))
10768 if (r_type != R_XTENSA_ASM_EXPAND)
10770 (*_bfd_error_handler)
10771 (_("%B(%A+0x%lx): unexpected fix for %s relocation"),
10772 input_bfd, input_section, rel->r_offset,
10773 elf_howto_table[r_type].name);
10776 /* Leave it be. Resolution will happen in a later stage. */
10780 sec = fix->target_sec;
10781 rel->r_addend += ((sec->output_offset + fix->target_offset)
10782 - (old_sec->output_offset + old_offset));
10789 do_fix_for_final_link (Elf_Internal_Rela *rel,
10791 asection *input_section,
10792 bfd_byte *contents,
10793 bfd_vma *relocationp)
10796 int r_type = ELF32_R_TYPE (rel->r_info);
10797 reloc_bfd_fix *fix;
10798 bfd_vma fixup_diff;
10800 if (r_type == R_XTENSA_NONE)
10803 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10807 sec = fix->target_sec;
10809 fixup_diff = rel->r_addend;
10810 if (elf_howto_table[fix->src_type].partial_inplace)
10812 bfd_vma inplace_val;
10813 BFD_ASSERT (fix->src_offset
10814 < bfd_get_section_limit (input_bfd, input_section));
10815 inplace_val = bfd_get_32 (input_bfd, &contents[fix->src_offset]);
10816 fixup_diff += inplace_val;
10819 *relocationp = (sec->output_section->vma
10820 + sec->output_offset
10821 + fix->target_offset - fixup_diff);
10825 /* Miscellaneous utility functions.... */
10828 elf_xtensa_get_plt_section (struct bfd_link_info *info, int chunk)
10830 struct elf_xtensa_link_hash_table *htab;
10836 htab = elf_xtensa_hash_table (info);
10843 dynobj = elf_hash_table (info)->dynobj;
10844 sprintf (plt_name, ".plt.%u", chunk);
10845 return bfd_get_linker_section (dynobj, plt_name);
10850 elf_xtensa_get_gotplt_section (struct bfd_link_info *info, int chunk)
10852 struct elf_xtensa_link_hash_table *htab;
10858 htab = elf_xtensa_hash_table (info);
10861 return htab->sgotplt;
10864 dynobj = elf_hash_table (info)->dynobj;
10865 sprintf (got_name, ".got.plt.%u", chunk);
10866 return bfd_get_linker_section (dynobj, got_name);
10870 /* Get the input section for a given symbol index.
10872 . a section symbol, return the section;
10873 . a common symbol, return the common section;
10874 . an undefined symbol, return the undefined section;
10875 . an indirect symbol, follow the links;
10876 . an absolute value, return the absolute section. */
10879 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
10881 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10882 asection *target_sec = NULL;
10883 if (r_symndx < symtab_hdr->sh_info)
10885 Elf_Internal_Sym *isymbuf;
10886 unsigned int section_index;
10888 isymbuf = retrieve_local_syms (abfd);
10889 section_index = isymbuf[r_symndx].st_shndx;
10891 if (section_index == SHN_UNDEF)
10892 target_sec = bfd_und_section_ptr;
10893 else if (section_index == SHN_ABS)
10894 target_sec = bfd_abs_section_ptr;
10895 else if (section_index == SHN_COMMON)
10896 target_sec = bfd_com_section_ptr;
10898 target_sec = bfd_section_from_elf_index (abfd, section_index);
10902 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10903 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
10905 while (h->root.type == bfd_link_hash_indirect
10906 || h->root.type == bfd_link_hash_warning)
10907 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10909 switch (h->root.type)
10911 case bfd_link_hash_defined:
10912 case bfd_link_hash_defweak:
10913 target_sec = h->root.u.def.section;
10915 case bfd_link_hash_common:
10916 target_sec = bfd_com_section_ptr;
10918 case bfd_link_hash_undefined:
10919 case bfd_link_hash_undefweak:
10920 target_sec = bfd_und_section_ptr;
10922 default: /* New indirect warning. */
10923 target_sec = bfd_und_section_ptr;
10931 static struct elf_link_hash_entry *
10932 get_elf_r_symndx_hash_entry (bfd *abfd, unsigned long r_symndx)
10934 unsigned long indx;
10935 struct elf_link_hash_entry *h;
10936 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10938 if (r_symndx < symtab_hdr->sh_info)
10941 indx = r_symndx - symtab_hdr->sh_info;
10942 h = elf_sym_hashes (abfd)[indx];
10943 while (h->root.type == bfd_link_hash_indirect
10944 || h->root.type == bfd_link_hash_warning)
10945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10950 /* Get the section-relative offset for a symbol number. */
10953 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
10955 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10956 bfd_vma offset = 0;
10958 if (r_symndx < symtab_hdr->sh_info)
10960 Elf_Internal_Sym *isymbuf;
10961 isymbuf = retrieve_local_syms (abfd);
10962 offset = isymbuf[r_symndx].st_value;
10966 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10967 struct elf_link_hash_entry *h =
10968 elf_sym_hashes (abfd)[indx];
10970 while (h->root.type == bfd_link_hash_indirect
10971 || h->root.type == bfd_link_hash_warning)
10972 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10973 if (h->root.type == bfd_link_hash_defined
10974 || h->root.type == bfd_link_hash_defweak)
10975 offset = h->root.u.def.value;
10982 is_reloc_sym_weak (bfd *abfd, Elf_Internal_Rela *rel)
10984 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
10985 struct elf_link_hash_entry *h;
10987 h = get_elf_r_symndx_hash_entry (abfd, r_symndx);
10988 if (h && h->root.type == bfd_link_hash_defweak)
10995 pcrel_reloc_fits (xtensa_opcode opc,
10997 bfd_vma self_address,
10998 bfd_vma dest_address)
11000 xtensa_isa isa = xtensa_default_isa;
11001 uint32 valp = dest_address;
11002 if (xtensa_operand_do_reloc (isa, opc, opnd, &valp, self_address)
11003 || xtensa_operand_encode (isa, opc, opnd, &valp))
11010 xtensa_is_property_section (asection *sec)
11012 if (xtensa_is_insntable_section (sec)
11013 || xtensa_is_littable_section (sec)
11014 || xtensa_is_proptable_section (sec))
11022 xtensa_is_insntable_section (asection *sec)
11024 if (CONST_STRNEQ (sec->name, XTENSA_INSN_SEC_NAME)
11025 || CONST_STRNEQ (sec->name, ".gnu.linkonce.x."))
11033 xtensa_is_littable_section (asection *sec)
11035 if (CONST_STRNEQ (sec->name, XTENSA_LIT_SEC_NAME)
11036 || CONST_STRNEQ (sec->name, ".gnu.linkonce.p."))
11044 xtensa_is_proptable_section (asection *sec)
11046 if (CONST_STRNEQ (sec->name, XTENSA_PROP_SEC_NAME)
11047 || CONST_STRNEQ (sec->name, ".gnu.linkonce.prop."))
11055 internal_reloc_compare (const void *ap, const void *bp)
11057 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11058 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11060 if (a->r_offset != b->r_offset)
11061 return (a->r_offset - b->r_offset);
11063 /* We don't need to sort on these criteria for correctness,
11064 but enforcing a more strict ordering prevents unstable qsort
11065 from behaving differently with different implementations.
11066 Without the code below we get correct but different results
11067 on Solaris 2.7 and 2.8. We would like to always produce the
11068 same results no matter the host. */
11070 if (a->r_info != b->r_info)
11071 return (a->r_info - b->r_info);
11073 return (a->r_addend - b->r_addend);
11078 internal_reloc_matches (const void *ap, const void *bp)
11080 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11081 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11083 /* Check if one entry overlaps with the other; this shouldn't happen
11084 except when searching for a match. */
11085 return (a->r_offset - b->r_offset);
11089 /* Predicate function used to look up a section in a particular group. */
11092 match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
11094 const char *gname = inf;
11095 const char *group_name = elf_group_name (sec);
11097 return (group_name == gname
11098 || (group_name != NULL
11100 && strcmp (group_name, gname) == 0));
11104 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
11107 xtensa_property_section_name (asection *sec, const char *base_name)
11109 const char *suffix, *group_name;
11110 char *prop_sec_name;
11112 group_name = elf_group_name (sec);
11115 suffix = strrchr (sec->name, '.');
11116 if (suffix == sec->name)
11118 prop_sec_name = (char *) bfd_malloc (strlen (base_name) + 1
11119 + (suffix ? strlen (suffix) : 0));
11120 strcpy (prop_sec_name, base_name);
11122 strcat (prop_sec_name, suffix);
11124 else if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
11126 char *linkonce_kind = 0;
11128 if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
11129 linkonce_kind = "x.";
11130 else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
11131 linkonce_kind = "p.";
11132 else if (strcmp (base_name, XTENSA_PROP_SEC_NAME) == 0)
11133 linkonce_kind = "prop.";
11137 prop_sec_name = (char *) bfd_malloc (strlen (sec->name)
11138 + strlen (linkonce_kind) + 1);
11139 memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
11140 strcpy (prop_sec_name + linkonce_len, linkonce_kind);
11142 suffix = sec->name + linkonce_len;
11143 /* For backward compatibility, replace "t." instead of inserting
11144 the new linkonce_kind (but not for "prop" sections). */
11145 if (CONST_STRNEQ (suffix, "t.") && linkonce_kind[1] == '.')
11147 strcat (prop_sec_name + linkonce_len, suffix);
11150 prop_sec_name = strdup (base_name);
11152 return prop_sec_name;
11157 xtensa_get_property_section (asection *sec, const char *base_name)
11159 char *prop_sec_name;
11160 asection *prop_sec;
11162 prop_sec_name = xtensa_property_section_name (sec, base_name);
11163 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
11164 match_section_group,
11165 (void *) elf_group_name (sec));
11166 free (prop_sec_name);
11172 xtensa_make_property_section (asection *sec, const char *base_name)
11174 char *prop_sec_name;
11175 asection *prop_sec;
11177 /* Check if the section already exists. */
11178 prop_sec_name = xtensa_property_section_name (sec, base_name);
11179 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
11180 match_section_group,
11181 (void *) elf_group_name (sec));
11182 /* If not, create it. */
11185 flagword flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY);
11186 flags |= (bfd_get_section_flags (sec->owner, sec)
11187 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES));
11189 prop_sec = bfd_make_section_anyway_with_flags
11190 (sec->owner, strdup (prop_sec_name), flags);
11194 elf_group_name (prop_sec) = elf_group_name (sec);
11197 free (prop_sec_name);
11203 xtensa_get_property_predef_flags (asection *sec)
11205 if (xtensa_is_insntable_section (sec))
11206 return (XTENSA_PROP_INSN
11207 | XTENSA_PROP_NO_TRANSFORM
11208 | XTENSA_PROP_INSN_NO_REORDER);
11210 if (xtensa_is_littable_section (sec))
11211 return (XTENSA_PROP_LITERAL
11212 | XTENSA_PROP_NO_TRANSFORM
11213 | XTENSA_PROP_INSN_NO_REORDER);
11219 /* Other functions called directly by the linker. */
11222 xtensa_callback_required_dependence (bfd *abfd,
11224 struct bfd_link_info *link_info,
11225 deps_callback_t callback,
11228 Elf_Internal_Rela *internal_relocs;
11229 bfd_byte *contents;
11231 bfd_boolean ok = TRUE;
11232 bfd_size_type sec_size;
11234 sec_size = bfd_get_section_limit (abfd, sec);
11236 /* ".plt*" sections have no explicit relocations but they contain L32R
11237 instructions that reference the corresponding ".got.plt*" sections. */
11238 if ((sec->flags & SEC_LINKER_CREATED) != 0
11239 && CONST_STRNEQ (sec->name, ".plt"))
11243 /* Find the corresponding ".got.plt*" section. */
11244 if (sec->name[4] == '\0')
11245 sgotplt = bfd_get_linker_section (sec->owner, ".got.plt");
11251 BFD_ASSERT (sec->name[4] == '.');
11252 chunk = strtol (&sec->name[5], NULL, 10);
11254 sprintf (got_name, ".got.plt.%u", chunk);
11255 sgotplt = bfd_get_linker_section (sec->owner, got_name);
11257 BFD_ASSERT (sgotplt);
11259 /* Assume worst-case offsets: L32R at the very end of the ".plt"
11260 section referencing a literal at the very beginning of
11261 ".got.plt". This is very close to the real dependence, anyway. */
11262 (*callback) (sec, sec_size, sgotplt, 0, closure);
11265 /* Only ELF files are supported for Xtensa. Check here to avoid a segfault
11266 when building uclibc, which runs "ld -b binary /dev/null". */
11267 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11270 internal_relocs = retrieve_internal_relocs (abfd, sec,
11271 link_info->keep_memory);
11272 if (internal_relocs == NULL
11273 || sec->reloc_count == 0)
11276 /* Cache the contents for the duration of this scan. */
11277 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
11278 if (contents == NULL && sec_size != 0)
11284 if (!xtensa_default_isa)
11285 xtensa_default_isa = xtensa_isa_init (0, 0);
11287 for (i = 0; i < sec->reloc_count; i++)
11289 Elf_Internal_Rela *irel = &internal_relocs[i];
11290 if (is_l32r_relocation (abfd, sec, contents, irel))
11293 asection *target_sec;
11294 bfd_vma target_offset;
11296 r_reloc_init (&l32r_rel, abfd, irel, contents, sec_size);
11299 /* L32Rs must be local to the input file. */
11300 if (r_reloc_is_defined (&l32r_rel))
11302 target_sec = r_reloc_get_section (&l32r_rel);
11303 target_offset = l32r_rel.target_offset;
11305 (*callback) (sec, irel->r_offset, target_sec, target_offset,
11311 release_internal_relocs (sec, internal_relocs);
11312 release_contents (sec, contents);
11316 /* The default literal sections should always be marked as "code" (i.e.,
11317 SHF_EXECINSTR). This is particularly important for the Linux kernel
11318 module loader so that the literals are not placed after the text. */
11319 static const struct bfd_elf_special_section elf_xtensa_special_sections[] =
11321 { STRING_COMMA_LEN (".fini.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11322 { STRING_COMMA_LEN (".init.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11323 { STRING_COMMA_LEN (".literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11324 { STRING_COMMA_LEN (".xtensa.info"), 0, SHT_NOTE, 0 },
11325 { NULL, 0, 0, 0, 0 }
11328 #define ELF_TARGET_ID XTENSA_ELF_DATA
11330 #define TARGET_LITTLE_SYM xtensa_elf32_le_vec
11331 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
11332 #define TARGET_BIG_SYM xtensa_elf32_be_vec
11333 #define TARGET_BIG_NAME "elf32-xtensa-be"
11334 #define ELF_ARCH bfd_arch_xtensa
11336 #define ELF_MACHINE_CODE EM_XTENSA
11337 #define ELF_MACHINE_ALT1 EM_XTENSA_OLD
11340 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
11341 #else /* !XCHAL_HAVE_MMU */
11342 #define ELF_MAXPAGESIZE 1
11343 #endif /* !XCHAL_HAVE_MMU */
11344 #endif /* ELF_ARCH */
11346 #define elf_backend_can_gc_sections 1
11347 #define elf_backend_can_refcount 1
11348 #define elf_backend_plt_readonly 1
11349 #define elf_backend_got_header_size 4
11350 #define elf_backend_want_dynbss 0
11351 #define elf_backend_want_got_plt 1
11353 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
11355 #define bfd_elf32_mkobject elf_xtensa_mkobject
11357 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
11358 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
11359 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
11360 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
11361 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
11362 #define bfd_elf32_bfd_reloc_name_lookup \
11363 elf_xtensa_reloc_name_lookup
11364 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
11365 #define bfd_elf32_bfd_link_hash_table_create elf_xtensa_link_hash_table_create
11367 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
11368 #define elf_backend_check_relocs elf_xtensa_check_relocs
11369 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
11370 #define elf_backend_discard_info elf_xtensa_discard_info
11371 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
11372 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
11373 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
11374 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
11375 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
11376 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
11377 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
11378 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
11379 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
11380 #define elf_backend_object_p elf_xtensa_object_p
11381 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
11382 #define elf_backend_relocate_section elf_xtensa_relocate_section
11383 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
11384 #define elf_backend_always_size_sections elf_xtensa_always_size_sections
11385 #define elf_backend_omit_section_dynsym \
11386 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
11387 #define elf_backend_special_sections elf_xtensa_special_sections
11388 #define elf_backend_action_discarded elf_xtensa_action_discarded
11389 #define elf_backend_copy_indirect_symbol elf_xtensa_copy_indirect_symbol
11391 #include "elf32-target.h"