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 /* xgettext:c-format */
485 _bfd_error_handler (_("%B: invalid XTENSA reloc number: %d"), abfd, r_type);
488 cache_ptr->howto = &elf_howto_table[r_type];
492 /* Functions for the Xtensa ELF linker. */
494 /* The name of the dynamic interpreter. This is put in the .interp
497 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
499 /* The size in bytes of an entry in the procedure linkage table.
500 (This does _not_ include the space for the literals associated with
503 #define PLT_ENTRY_SIZE 16
505 /* For _really_ large PLTs, we may need to alternate between literals
506 and code to keep the literals within the 256K range of the L32R
507 instructions in the code. It's unlikely that anyone would ever need
508 such a big PLT, but an arbitrary limit on the PLT size would be bad.
509 Thus, we split the PLT into chunks. Since there's very little
510 overhead (2 extra literals) for each chunk, the chunk size is kept
511 small so that the code for handling multiple chunks get used and
512 tested regularly. With 254 entries, there are 1K of literals for
513 each chunk, and that seems like a nice round number. */
515 #define PLT_ENTRIES_PER_CHUNK 254
517 /* PLT entries are actually used as stub functions for lazy symbol
518 resolution. Once the symbol is resolved, the stub function is never
519 invoked. Note: the 32-byte frame size used here cannot be changed
520 without a corresponding change in the runtime linker. */
522 static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
524 #if XSHAL_ABI == XTHAL_ABI_WINDOWED
525 0x6c, 0x10, 0x04, /* entry sp, 32 */
527 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
528 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
529 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
530 0x0a, 0x80, 0x00, /* jx a8 */
534 static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
536 #if XSHAL_ABI == XTHAL_ABI_WINDOWED
537 0x36, 0x41, 0x00, /* entry sp, 32 */
539 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
540 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
541 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
542 0xa0, 0x08, 0x00, /* jx a8 */
546 /* The size of the thread control block. */
549 struct elf_xtensa_link_hash_entry
551 struct elf_link_hash_entry elf;
553 bfd_signed_vma tlsfunc_refcount;
555 #define GOT_UNKNOWN 0
557 #define GOT_TLS_GD 2 /* global or local dynamic */
558 #define GOT_TLS_IE 4 /* initial or local exec */
559 #define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
560 unsigned char tls_type;
563 #define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
565 struct elf_xtensa_obj_tdata
567 struct elf_obj_tdata root;
569 /* tls_type for each local got entry. */
570 char *local_got_tls_type;
572 bfd_signed_vma *local_tlsfunc_refcounts;
575 #define elf_xtensa_tdata(abfd) \
576 ((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
578 #define elf_xtensa_local_got_tls_type(abfd) \
579 (elf_xtensa_tdata (abfd)->local_got_tls_type)
581 #define elf_xtensa_local_tlsfunc_refcounts(abfd) \
582 (elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
584 #define is_xtensa_elf(bfd) \
585 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
586 && elf_tdata (bfd) != NULL \
587 && elf_object_id (bfd) == XTENSA_ELF_DATA)
590 elf_xtensa_mkobject (bfd *abfd)
592 return bfd_elf_allocate_object (abfd, sizeof (struct elf_xtensa_obj_tdata),
596 /* Xtensa ELF linker hash table. */
598 struct elf_xtensa_link_hash_table
600 struct elf_link_hash_table elf;
602 /* Short-cuts to get to dynamic linker sections. */
609 asection *spltlittbl;
611 /* Total count of PLT relocations seen during check_relocs.
612 The actual PLT code must be split into multiple sections and all
613 the sections have to be created before size_dynamic_sections,
614 where we figure out the exact number of PLT entries that will be
615 needed. It is OK if this count is an overestimate, e.g., some
616 relocations may be removed by GC. */
619 struct elf_xtensa_link_hash_entry *tlsbase;
622 /* Get the Xtensa ELF linker hash table from a link_info structure. */
624 #define elf_xtensa_hash_table(p) \
625 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
626 == XTENSA_ELF_DATA ? ((struct elf_xtensa_link_hash_table *) ((p)->hash)) : NULL)
628 /* Create an entry in an Xtensa ELF linker hash table. */
630 static struct bfd_hash_entry *
631 elf_xtensa_link_hash_newfunc (struct bfd_hash_entry *entry,
632 struct bfd_hash_table *table,
635 /* Allocate the structure if it has not already been allocated by a
639 entry = bfd_hash_allocate (table,
640 sizeof (struct elf_xtensa_link_hash_entry));
645 /* Call the allocation method of the superclass. */
646 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
649 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (entry);
650 eh->tlsfunc_refcount = 0;
651 eh->tls_type = GOT_UNKNOWN;
657 /* Create an Xtensa ELF linker hash table. */
659 static struct bfd_link_hash_table *
660 elf_xtensa_link_hash_table_create (bfd *abfd)
662 struct elf_link_hash_entry *tlsbase;
663 struct elf_xtensa_link_hash_table *ret;
664 bfd_size_type amt = sizeof (struct elf_xtensa_link_hash_table);
666 ret = bfd_zmalloc (amt);
670 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
671 elf_xtensa_link_hash_newfunc,
672 sizeof (struct elf_xtensa_link_hash_entry),
679 /* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
681 tlsbase = elf_link_hash_lookup (&ret->elf, "_TLS_MODULE_BASE_",
683 tlsbase->root.type = bfd_link_hash_new;
684 tlsbase->root.u.undef.abfd = NULL;
685 tlsbase->non_elf = 0;
686 ret->tlsbase = elf_xtensa_hash_entry (tlsbase);
687 ret->tlsbase->tls_type = GOT_UNKNOWN;
689 return &ret->elf.root;
692 /* Copy the extra info we tack onto an elf_link_hash_entry. */
695 elf_xtensa_copy_indirect_symbol (struct bfd_link_info *info,
696 struct elf_link_hash_entry *dir,
697 struct elf_link_hash_entry *ind)
699 struct elf_xtensa_link_hash_entry *edir, *eind;
701 edir = elf_xtensa_hash_entry (dir);
702 eind = elf_xtensa_hash_entry (ind);
704 if (ind->root.type == bfd_link_hash_indirect)
706 edir->tlsfunc_refcount += eind->tlsfunc_refcount;
707 eind->tlsfunc_refcount = 0;
709 if (dir->got.refcount <= 0)
711 edir->tls_type = eind->tls_type;
712 eind->tls_type = GOT_UNKNOWN;
716 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
719 static inline bfd_boolean
720 elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry *h,
721 struct bfd_link_info *info)
723 /* Check if we should do dynamic things to this symbol. The
724 "ignore_protected" argument need not be set, because Xtensa code
725 does not require special handling of STV_PROTECTED to make function
726 pointer comparisons work properly. The PLT addresses are never
727 used for function pointers. */
729 return _bfd_elf_dynamic_symbol_p (h, info, 0);
734 property_table_compare (const void *ap, const void *bp)
736 const property_table_entry *a = (const property_table_entry *) ap;
737 const property_table_entry *b = (const property_table_entry *) bp;
739 if (a->address == b->address)
741 if (a->size != b->size)
742 return (a->size - b->size);
744 if ((a->flags & XTENSA_PROP_ALIGN) != (b->flags & XTENSA_PROP_ALIGN))
745 return ((b->flags & XTENSA_PROP_ALIGN)
746 - (a->flags & XTENSA_PROP_ALIGN));
748 if ((a->flags & XTENSA_PROP_ALIGN)
749 && (GET_XTENSA_PROP_ALIGNMENT (a->flags)
750 != GET_XTENSA_PROP_ALIGNMENT (b->flags)))
751 return (GET_XTENSA_PROP_ALIGNMENT (a->flags)
752 - GET_XTENSA_PROP_ALIGNMENT (b->flags));
754 if ((a->flags & XTENSA_PROP_UNREACHABLE)
755 != (b->flags & XTENSA_PROP_UNREACHABLE))
756 return ((b->flags & XTENSA_PROP_UNREACHABLE)
757 - (a->flags & XTENSA_PROP_UNREACHABLE));
759 return (a->flags - b->flags);
762 return (a->address - b->address);
767 property_table_matches (const void *ap, const void *bp)
769 const property_table_entry *a = (const property_table_entry *) ap;
770 const property_table_entry *b = (const property_table_entry *) bp;
772 /* Check if one entry overlaps with the other. */
773 if ((b->address >= a->address && b->address < (a->address + a->size))
774 || (a->address >= b->address && a->address < (b->address + b->size)))
777 return (a->address - b->address);
781 /* Get the literal table or property table entries for the given
782 section. Sets TABLE_P and returns the number of entries. On
783 error, returns a negative value. */
786 xtensa_read_table_entries (bfd *abfd,
788 property_table_entry **table_p,
789 const char *sec_name,
790 bfd_boolean output_addr)
792 asection *table_section;
793 bfd_size_type table_size = 0;
794 bfd_byte *table_data;
795 property_table_entry *blocks;
796 int blk, block_count;
797 bfd_size_type num_records;
798 Elf_Internal_Rela *internal_relocs, *irel, *rel_end;
799 bfd_vma section_addr, off;
800 flagword predef_flags;
801 bfd_size_type table_entry_size, section_limit;
804 || !(section->flags & SEC_ALLOC)
805 || (section->flags & SEC_DEBUGGING))
811 table_section = xtensa_get_property_section (section, sec_name);
813 table_size = table_section->size;
821 predef_flags = xtensa_get_property_predef_flags (table_section);
822 table_entry_size = 12;
824 table_entry_size -= 4;
826 num_records = table_size / table_entry_size;
827 table_data = retrieve_contents (abfd, table_section, TRUE);
828 blocks = (property_table_entry *)
829 bfd_malloc (num_records * sizeof (property_table_entry));
833 section_addr = section->output_section->vma + section->output_offset;
835 section_addr = section->vma;
837 internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
838 if (internal_relocs && !table_section->reloc_done)
840 qsort (internal_relocs, table_section->reloc_count,
841 sizeof (Elf_Internal_Rela), internal_reloc_compare);
842 irel = internal_relocs;
847 section_limit = bfd_get_section_limit (abfd, section);
848 rel_end = internal_relocs + table_section->reloc_count;
850 for (off = 0; off < table_size; off += table_entry_size)
852 bfd_vma address = bfd_get_32 (abfd, table_data + off);
854 /* Skip any relocations before the current offset. This should help
855 avoid confusion caused by unexpected relocations for the preceding
858 (irel->r_offset < off
859 || (irel->r_offset == off
860 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_NONE)))
867 if (irel && irel->r_offset == off)
870 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
871 BFD_ASSERT (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32);
873 if (get_elf_r_symndx_section (abfd, r_symndx) != section)
876 sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
877 BFD_ASSERT (sym_off == 0);
878 address += (section_addr + sym_off + irel->r_addend);
882 if (address < section_addr
883 || address >= section_addr + section_limit)
887 blocks[block_count].address = address;
888 blocks[block_count].size = bfd_get_32 (abfd, table_data + off + 4);
890 blocks[block_count].flags = predef_flags;
892 blocks[block_count].flags = bfd_get_32 (abfd, table_data + off + 8);
896 release_contents (table_section, table_data);
897 release_internal_relocs (table_section, internal_relocs);
901 /* Now sort them into address order for easy reference. */
902 qsort (blocks, block_count, sizeof (property_table_entry),
903 property_table_compare);
905 /* Check that the table contents are valid. Problems may occur,
906 for example, if an unrelocated object file is stripped. */
907 for (blk = 1; blk < block_count; blk++)
909 /* The only circumstance where two entries may legitimately
910 have the same address is when one of them is a zero-size
911 placeholder to mark a place where fill can be inserted.
912 The zero-size entry should come first. */
913 if (blocks[blk - 1].address == blocks[blk].address &&
914 blocks[blk - 1].size != 0)
916 /* xgettext:c-format */
917 _bfd_error_handler (_("%B(%A): invalid property table"),
919 bfd_set_error (bfd_error_bad_value);
931 static property_table_entry *
932 elf_xtensa_find_property_entry (property_table_entry *property_table,
933 int property_table_size,
936 property_table_entry entry;
937 property_table_entry *rv;
939 if (property_table_size == 0)
942 entry.address = addr;
946 rv = bsearch (&entry, property_table, property_table_size,
947 sizeof (property_table_entry), property_table_matches);
953 elf_xtensa_in_literal_pool (property_table_entry *lit_table,
957 if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
964 /* Look through the relocs for a section during the first phase, and
965 calculate needed space in the dynamic reloc sections. */
968 elf_xtensa_check_relocs (bfd *abfd,
969 struct bfd_link_info *info,
971 const Elf_Internal_Rela *relocs)
973 struct elf_xtensa_link_hash_table *htab;
974 Elf_Internal_Shdr *symtab_hdr;
975 struct elf_link_hash_entry **sym_hashes;
976 const Elf_Internal_Rela *rel;
977 const Elf_Internal_Rela *rel_end;
979 if (bfd_link_relocatable (info) || (sec->flags & SEC_ALLOC) == 0)
982 BFD_ASSERT (is_xtensa_elf (abfd));
984 htab = elf_xtensa_hash_table (info);
988 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
989 sym_hashes = elf_sym_hashes (abfd);
991 rel_end = relocs + sec->reloc_count;
992 for (rel = relocs; rel < rel_end; rel++)
995 unsigned long r_symndx;
996 struct elf_link_hash_entry *h = NULL;
997 struct elf_xtensa_link_hash_entry *eh;
998 int tls_type, old_tls_type;
999 bfd_boolean is_got = FALSE;
1000 bfd_boolean is_plt = FALSE;
1001 bfd_boolean is_tlsfunc = FALSE;
1003 r_symndx = ELF32_R_SYM (rel->r_info);
1004 r_type = ELF32_R_TYPE (rel->r_info);
1006 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1008 /* xgettext:c-format */
1009 _bfd_error_handler (_("%B: bad symbol index: %d"),
1014 if (r_symndx >= symtab_hdr->sh_info)
1016 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1017 while (h->root.type == bfd_link_hash_indirect
1018 || h->root.type == bfd_link_hash_warning)
1019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1021 /* PR15323, ref flags aren't set for references in the same
1023 h->root.non_ir_ref = 1;
1025 eh = elf_xtensa_hash_entry (h);
1029 case R_XTENSA_TLSDESC_FN:
1030 if (bfd_link_pic (info))
1032 tls_type = GOT_TLS_GD;
1037 tls_type = GOT_TLS_IE;
1040 case R_XTENSA_TLSDESC_ARG:
1041 if (bfd_link_pic (info))
1043 tls_type = GOT_TLS_GD;
1048 tls_type = GOT_TLS_IE;
1049 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1054 case R_XTENSA_TLS_DTPOFF:
1055 if (bfd_link_pic (info))
1056 tls_type = GOT_TLS_GD;
1058 tls_type = GOT_TLS_IE;
1061 case R_XTENSA_TLS_TPOFF:
1062 tls_type = GOT_TLS_IE;
1063 if (bfd_link_pic (info))
1064 info->flags |= DF_STATIC_TLS;
1065 if (bfd_link_pic (info) || h)
1070 tls_type = GOT_NORMAL;
1075 tls_type = GOT_NORMAL;
1079 case R_XTENSA_GNU_VTINHERIT:
1080 /* This relocation describes the C++ object vtable hierarchy.
1081 Reconstruct it for later use during GC. */
1082 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1086 case R_XTENSA_GNU_VTENTRY:
1087 /* This relocation describes which C++ vtable entries are actually
1088 used. Record for later use during GC. */
1089 BFD_ASSERT (h != NULL);
1091 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1096 /* Nothing to do for any other relocations. */
1104 if (h->plt.refcount <= 0)
1107 h->plt.refcount = 1;
1110 h->plt.refcount += 1;
1112 /* Keep track of the total PLT relocation count even if we
1113 don't yet know whether the dynamic sections will be
1115 htab->plt_reloc_count += 1;
1117 if (elf_hash_table (info)->dynamic_sections_created)
1119 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1125 if (h->got.refcount <= 0)
1126 h->got.refcount = 1;
1128 h->got.refcount += 1;
1132 eh->tlsfunc_refcount += 1;
1134 old_tls_type = eh->tls_type;
1138 /* Allocate storage the first time. */
1139 if (elf_local_got_refcounts (abfd) == NULL)
1141 bfd_size_type size = symtab_hdr->sh_info;
1144 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1147 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) mem;
1149 mem = bfd_zalloc (abfd, size);
1152 elf_xtensa_local_got_tls_type (abfd) = (char *) mem;
1154 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1157 elf_xtensa_local_tlsfunc_refcounts (abfd)
1158 = (bfd_signed_vma *) mem;
1161 /* This is a global offset table entry for a local symbol. */
1162 if (is_got || is_plt)
1163 elf_local_got_refcounts (abfd) [r_symndx] += 1;
1166 elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx] += 1;
1168 old_tls_type = elf_xtensa_local_got_tls_type (abfd) [r_symndx];
1171 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1172 tls_type |= old_tls_type;
1173 /* If a TLS symbol is accessed using IE at least once,
1174 there is no point to use a dynamic model for it. */
1175 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1176 && ((old_tls_type & GOT_TLS_GD) == 0
1177 || (tls_type & GOT_TLS_IE) == 0))
1179 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GD))
1180 tls_type = old_tls_type;
1181 else if ((old_tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_GD))
1182 tls_type |= old_tls_type;
1186 /* xgettext:c-format */
1187 (_("%B: `%s' accessed both as normal and thread local symbol"),
1189 h ? h->root.root.string : "<local>");
1194 if (old_tls_type != tls_type)
1197 eh->tls_type = tls_type;
1199 elf_xtensa_local_got_tls_type (abfd) [r_symndx] = tls_type;
1208 elf_xtensa_make_sym_local (struct bfd_link_info *info,
1209 struct elf_link_hash_entry *h)
1211 if (bfd_link_pic (info))
1213 if (h->plt.refcount > 0)
1215 /* For shared objects, there's no need for PLT entries for local
1216 symbols (use RELATIVE relocs instead of JMP_SLOT relocs). */
1217 if (h->got.refcount < 0)
1218 h->got.refcount = 0;
1219 h->got.refcount += h->plt.refcount;
1220 h->plt.refcount = 0;
1225 /* Don't need any dynamic relocations at all. */
1226 h->plt.refcount = 0;
1227 h->got.refcount = 0;
1233 elf_xtensa_hide_symbol (struct bfd_link_info *info,
1234 struct elf_link_hash_entry *h,
1235 bfd_boolean force_local)
1237 /* For a shared link, move the plt refcount to the got refcount to leave
1238 space for RELATIVE relocs. */
1239 elf_xtensa_make_sym_local (info, h);
1241 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1245 /* Return the section that should be marked against GC for a given
1249 elf_xtensa_gc_mark_hook (asection *sec,
1250 struct bfd_link_info *info,
1251 Elf_Internal_Rela *rel,
1252 struct elf_link_hash_entry *h,
1253 Elf_Internal_Sym *sym)
1255 /* Property sections are marked "KEEP" in the linker scripts, but they
1256 should not cause other sections to be marked. (This approach relies
1257 on elf_xtensa_discard_info to remove property table entries that
1258 describe discarded sections. Alternatively, it might be more
1259 efficient to avoid using "KEEP" in the linker scripts and instead use
1260 the gc_mark_extra_sections hook to mark only the property sections
1261 that describe marked sections. That alternative does not work well
1262 with the current property table sections, which do not correspond
1263 one-to-one with the sections they describe, but that should be fixed
1265 if (xtensa_is_property_section (sec))
1269 switch (ELF32_R_TYPE (rel->r_info))
1271 case R_XTENSA_GNU_VTINHERIT:
1272 case R_XTENSA_GNU_VTENTRY:
1276 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1280 /* Update the GOT & PLT entry reference counts
1281 for the section being removed. */
1284 elf_xtensa_gc_sweep_hook (bfd *abfd,
1285 struct bfd_link_info *info,
1287 const Elf_Internal_Rela *relocs)
1289 Elf_Internal_Shdr *symtab_hdr;
1290 struct elf_link_hash_entry **sym_hashes;
1291 const Elf_Internal_Rela *rel, *relend;
1292 struct elf_xtensa_link_hash_table *htab;
1294 htab = elf_xtensa_hash_table (info);
1298 if (bfd_link_relocatable (info))
1301 if ((sec->flags & SEC_ALLOC) == 0)
1304 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1305 sym_hashes = elf_sym_hashes (abfd);
1307 relend = relocs + sec->reloc_count;
1308 for (rel = relocs; rel < relend; rel++)
1310 unsigned long r_symndx;
1311 unsigned int r_type;
1312 struct elf_link_hash_entry *h = NULL;
1313 struct elf_xtensa_link_hash_entry *eh;
1314 bfd_boolean is_got = FALSE;
1315 bfd_boolean is_plt = FALSE;
1316 bfd_boolean is_tlsfunc = FALSE;
1318 r_symndx = ELF32_R_SYM (rel->r_info);
1319 if (r_symndx >= symtab_hdr->sh_info)
1321 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1322 while (h->root.type == bfd_link_hash_indirect
1323 || h->root.type == bfd_link_hash_warning)
1324 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1326 eh = elf_xtensa_hash_entry (h);
1328 r_type = ELF32_R_TYPE (rel->r_info);
1331 case R_XTENSA_TLSDESC_FN:
1332 if (bfd_link_pic (info))
1339 case R_XTENSA_TLSDESC_ARG:
1340 if (bfd_link_pic (info))
1344 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1349 case R_XTENSA_TLS_TPOFF:
1350 if (bfd_link_pic (info) || h)
1370 /* If the symbol has been localized its plt.refcount got moved
1371 to got.refcount. Handle it as GOT. */
1372 if (h->plt.refcount > 0)
1379 if (h->got.refcount > 0)
1384 if (eh->tlsfunc_refcount > 0)
1385 eh->tlsfunc_refcount--;
1390 if (is_got || is_plt)
1392 bfd_signed_vma *got_refcount
1393 = &elf_local_got_refcounts (abfd) [r_symndx];
1394 if (*got_refcount > 0)
1399 bfd_signed_vma *tlsfunc_refcount
1400 = &elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx];
1401 if (*tlsfunc_refcount > 0)
1402 *tlsfunc_refcount -= 1;
1411 /* Create all the dynamic sections. */
1414 elf_xtensa_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1416 struct elf_xtensa_link_hash_table *htab;
1417 flagword flags, noalloc_flags;
1419 htab = elf_xtensa_hash_table (info);
1423 /* First do all the standard stuff. */
1424 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
1426 htab->splt = bfd_get_linker_section (dynobj, ".plt");
1427 htab->srelplt = bfd_get_linker_section (dynobj, ".rela.plt");
1428 htab->sgot = bfd_get_linker_section (dynobj, ".got");
1429 htab->sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
1430 htab->srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1432 /* Create any extra PLT sections in case check_relocs has already
1433 been called on all the non-dynamic input files. */
1434 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1437 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
1438 | SEC_LINKER_CREATED | SEC_READONLY);
1439 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
1441 /* Mark the ".got.plt" section READONLY. */
1442 if (htab->sgotplt == NULL
1443 || ! bfd_set_section_flags (dynobj, htab->sgotplt, flags))
1446 /* Create ".got.loc" (literal tables for use by dynamic linker). */
1447 htab->sgotloc = bfd_make_section_anyway_with_flags (dynobj, ".got.loc",
1449 if (htab->sgotloc == NULL
1450 || ! bfd_set_section_alignment (dynobj, htab->sgotloc, 2))
1453 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
1454 htab->spltlittbl = bfd_make_section_anyway_with_flags (dynobj, ".xt.lit.plt",
1456 if (htab->spltlittbl == NULL
1457 || ! bfd_set_section_alignment (dynobj, htab->spltlittbl, 2))
1465 add_extra_plt_sections (struct bfd_link_info *info, int count)
1467 bfd *dynobj = elf_hash_table (info)->dynobj;
1470 /* Iterate over all chunks except 0 which uses the standard ".plt" and
1471 ".got.plt" sections. */
1472 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
1478 /* Stop when we find a section has already been created. */
1479 if (elf_xtensa_get_plt_section (info, chunk))
1482 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1483 | SEC_LINKER_CREATED | SEC_READONLY);
1485 sname = (char *) bfd_malloc (10);
1486 sprintf (sname, ".plt.%u", chunk);
1487 s = bfd_make_section_anyway_with_flags (dynobj, sname, flags | SEC_CODE);
1489 || ! bfd_set_section_alignment (dynobj, s, 2))
1492 sname = (char *) bfd_malloc (14);
1493 sprintf (sname, ".got.plt.%u", chunk);
1494 s = bfd_make_section_anyway_with_flags (dynobj, sname, flags);
1496 || ! bfd_set_section_alignment (dynobj, s, 2))
1504 /* Adjust a symbol defined by a dynamic object and referenced by a
1505 regular object. The current definition is in some section of the
1506 dynamic object, but we're not including those sections. We have to
1507 change the definition to something the rest of the link can
1511 elf_xtensa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1512 struct elf_link_hash_entry *h)
1514 /* If this is a weak symbol, and there is a real definition, the
1515 processor independent code will have arranged for us to see the
1516 real definition first, and we can just use the same value. */
1519 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1520 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1521 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1522 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1526 /* This is a reference to a symbol defined by a dynamic object. The
1527 reference must go through the GOT, so there's no need for COPY relocs,
1535 elf_xtensa_allocate_dynrelocs (struct elf_link_hash_entry *h, void *arg)
1537 struct bfd_link_info *info;
1538 struct elf_xtensa_link_hash_table *htab;
1539 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (h);
1541 if (h->root.type == bfd_link_hash_indirect)
1544 info = (struct bfd_link_info *) arg;
1545 htab = elf_xtensa_hash_table (info);
1549 /* If we saw any use of an IE model for this symbol, we can then optimize
1550 away GOT entries for any TLSDESC_FN relocs. */
1551 if ((eh->tls_type & GOT_TLS_IE) != 0)
1553 BFD_ASSERT (h->got.refcount >= eh->tlsfunc_refcount);
1554 h->got.refcount -= eh->tlsfunc_refcount;
1557 if (! elf_xtensa_dynamic_symbol_p (h, info))
1558 elf_xtensa_make_sym_local (info, h);
1560 if (h->plt.refcount > 0)
1561 htab->srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1563 if (h->got.refcount > 0)
1564 htab->srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
1571 elf_xtensa_allocate_local_got_size (struct bfd_link_info *info)
1573 struct elf_xtensa_link_hash_table *htab;
1576 htab = elf_xtensa_hash_table (info);
1580 for (i = info->input_bfds; i; i = i->link.next)
1582 bfd_signed_vma *local_got_refcounts;
1583 bfd_size_type j, cnt;
1584 Elf_Internal_Shdr *symtab_hdr;
1586 local_got_refcounts = elf_local_got_refcounts (i);
1587 if (!local_got_refcounts)
1590 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1591 cnt = symtab_hdr->sh_info;
1593 for (j = 0; j < cnt; ++j)
1595 /* If we saw any use of an IE model for this symbol, we can
1596 then optimize away GOT entries for any TLSDESC_FN relocs. */
1597 if ((elf_xtensa_local_got_tls_type (i) [j] & GOT_TLS_IE) != 0)
1599 bfd_signed_vma *tlsfunc_refcount
1600 = &elf_xtensa_local_tlsfunc_refcounts (i) [j];
1601 BFD_ASSERT (local_got_refcounts[j] >= *tlsfunc_refcount);
1602 local_got_refcounts[j] -= *tlsfunc_refcount;
1605 if (local_got_refcounts[j] > 0)
1606 htab->srelgot->size += (local_got_refcounts[j]
1607 * sizeof (Elf32_External_Rela));
1613 /* Set the sizes of the dynamic sections. */
1616 elf_xtensa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1617 struct bfd_link_info *info)
1619 struct elf_xtensa_link_hash_table *htab;
1621 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1622 bfd_boolean relplt, relgot;
1623 int plt_entries, plt_chunks, chunk;
1628 htab = elf_xtensa_hash_table (info);
1632 dynobj = elf_hash_table (info)->dynobj;
1635 srelgot = htab->srelgot;
1636 srelplt = htab->srelplt;
1638 if (elf_hash_table (info)->dynamic_sections_created)
1640 BFD_ASSERT (htab->srelgot != NULL
1641 && htab->srelplt != NULL
1642 && htab->sgot != NULL
1643 && htab->spltlittbl != NULL
1644 && htab->sgotloc != NULL);
1646 /* Set the contents of the .interp section to the interpreter. */
1647 if (bfd_link_executable (info) && !info->nointerp)
1649 s = bfd_get_linker_section (dynobj, ".interp");
1652 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1653 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1656 /* Allocate room for one word in ".got". */
1657 htab->sgot->size = 4;
1659 /* Allocate space in ".rela.got" for literals that reference global
1660 symbols and space in ".rela.plt" for literals that have PLT
1662 elf_link_hash_traverse (elf_hash_table (info),
1663 elf_xtensa_allocate_dynrelocs,
1666 /* If we are generating a shared object, we also need space in
1667 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1668 reference local symbols. */
1669 if (bfd_link_pic (info))
1670 elf_xtensa_allocate_local_got_size (info);
1672 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1673 each PLT entry, we need the PLT code plus a 4-byte literal.
1674 For each chunk of ".plt", we also need two more 4-byte
1675 literals, two corresponding entries in ".rela.got", and an
1676 8-byte entry in ".xt.lit.plt". */
1677 spltlittbl = htab->spltlittbl;
1678 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
1680 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1682 /* Iterate over all the PLT chunks, including any extra sections
1683 created earlier because the initial count of PLT relocations
1684 was an overestimate. */
1686 (splt = elf_xtensa_get_plt_section (info, chunk)) != NULL;
1691 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
1692 BFD_ASSERT (sgotplt != NULL);
1694 if (chunk < plt_chunks - 1)
1695 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1696 else if (chunk == plt_chunks - 1)
1697 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1701 if (chunk_entries != 0)
1703 sgotplt->size = 4 * (chunk_entries + 2);
1704 splt->size = PLT_ENTRY_SIZE * chunk_entries;
1705 srelgot->size += 2 * sizeof (Elf32_External_Rela);
1706 spltlittbl->size += 8;
1715 /* Allocate space in ".got.loc" to match the total size of all the
1717 sgotloc = htab->sgotloc;
1718 sgotloc->size = spltlittbl->size;
1719 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
1721 if (abfd->flags & DYNAMIC)
1723 for (s = abfd->sections; s != NULL; s = s->next)
1725 if (! discarded_section (s)
1726 && xtensa_is_littable_section (s)
1728 sgotloc->size += s->size;
1733 /* Allocate memory for dynamic sections. */
1736 for (s = dynobj->sections; s != NULL; s = s->next)
1740 if ((s->flags & SEC_LINKER_CREATED) == 0)
1743 /* It's OK to base decisions on the section name, because none
1744 of the dynobj section names depend upon the input files. */
1745 name = bfd_get_section_name (dynobj, s);
1747 if (CONST_STRNEQ (name, ".rela"))
1751 if (strcmp (name, ".rela.plt") == 0)
1753 else if (strcmp (name, ".rela.got") == 0)
1756 /* We use the reloc_count field as a counter if we need
1757 to copy relocs into the output file. */
1761 else if (! CONST_STRNEQ (name, ".plt.")
1762 && ! CONST_STRNEQ (name, ".got.plt.")
1763 && strcmp (name, ".got") != 0
1764 && strcmp (name, ".plt") != 0
1765 && strcmp (name, ".got.plt") != 0
1766 && strcmp (name, ".xt.lit.plt") != 0
1767 && strcmp (name, ".got.loc") != 0)
1769 /* It's not one of our sections, so don't allocate space. */
1775 /* If we don't need this section, strip it from the output
1776 file. We must create the ".plt*" and ".got.plt*"
1777 sections in create_dynamic_sections and/or check_relocs
1778 based on a conservative estimate of the PLT relocation
1779 count, because the sections must be created before the
1780 linker maps input sections to output sections. The
1781 linker does that before size_dynamic_sections, where we
1782 compute the exact size of the PLT, so there may be more
1783 of these sections than are actually needed. */
1784 s->flags |= SEC_EXCLUDE;
1786 else if ((s->flags & SEC_HAS_CONTENTS) != 0)
1788 /* Allocate memory for the section contents. */
1789 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1790 if (s->contents == NULL)
1795 if (elf_hash_table (info)->dynamic_sections_created)
1797 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1798 known until finish_dynamic_sections, but we need to get the relocs
1799 in place before they are sorted. */
1800 for (chunk = 0; chunk < plt_chunks; chunk++)
1802 Elf_Internal_Rela irela;
1806 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1809 loc = (srelgot->contents
1810 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1811 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1812 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1813 loc + sizeof (Elf32_External_Rela));
1814 srelgot->reloc_count += 2;
1817 /* Add some entries to the .dynamic section. We fill in the
1818 values later, in elf_xtensa_finish_dynamic_sections, but we
1819 must add the entries now so that we get the correct size for
1820 the .dynamic section. The DT_DEBUG entry is filled in by the
1821 dynamic linker and used by the debugger. */
1822 #define add_dynamic_entry(TAG, VAL) \
1823 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1825 if (bfd_link_executable (info))
1827 if (!add_dynamic_entry (DT_DEBUG, 0))
1833 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1834 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1835 || !add_dynamic_entry (DT_JMPREL, 0))
1841 if (!add_dynamic_entry (DT_RELA, 0)
1842 || !add_dynamic_entry (DT_RELASZ, 0)
1843 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1847 if (!add_dynamic_entry (DT_PLTGOT, 0)
1848 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1849 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1852 #undef add_dynamic_entry
1858 elf_xtensa_always_size_sections (bfd *output_bfd,
1859 struct bfd_link_info *info)
1861 struct elf_xtensa_link_hash_table *htab;
1864 htab = elf_xtensa_hash_table (info);
1868 tls_sec = htab->elf.tls_sec;
1870 if (tls_sec && (htab->tlsbase->tls_type & GOT_TLS_ANY) != 0)
1872 struct elf_link_hash_entry *tlsbase = &htab->tlsbase->elf;
1873 struct bfd_link_hash_entry *bh = &tlsbase->root;
1874 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1876 tlsbase->type = STT_TLS;
1877 if (!(_bfd_generic_link_add_one_symbol
1878 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1879 tls_sec, 0, NULL, FALSE,
1880 bed->collect, &bh)))
1882 tlsbase->def_regular = 1;
1883 tlsbase->other = STV_HIDDEN;
1884 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1891 /* Return the base VMA address which should be subtracted from real addresses
1892 when resolving @dtpoff relocation.
1893 This is PT_TLS segment p_vaddr. */
1896 dtpoff_base (struct bfd_link_info *info)
1898 /* If tls_sec is NULL, we should have signalled an error already. */
1899 if (elf_hash_table (info)->tls_sec == NULL)
1901 return elf_hash_table (info)->tls_sec->vma;
1904 /* Return the relocation value for @tpoff relocation
1905 if STT_TLS virtual address is ADDRESS. */
1908 tpoff (struct bfd_link_info *info, bfd_vma address)
1910 struct elf_link_hash_table *htab = elf_hash_table (info);
1913 /* If tls_sec is NULL, we should have signalled an error already. */
1914 if (htab->tls_sec == NULL)
1916 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
1917 return address - htab->tls_sec->vma + base;
1920 /* Perform the specified relocation. The instruction at (contents + address)
1921 is modified to set one operand to represent the value in "relocation". The
1922 operand position is determined by the relocation type recorded in the
1925 #define CALL_SEGMENT_BITS (30)
1926 #define CALL_SEGMENT_SIZE (1 << CALL_SEGMENT_BITS)
1928 static bfd_reloc_status_type
1929 elf_xtensa_do_reloc (reloc_howto_type *howto,
1931 asection *input_section,
1935 bfd_boolean is_weak_undef,
1936 char **error_message)
1939 xtensa_opcode opcode;
1940 xtensa_isa isa = xtensa_default_isa;
1941 static xtensa_insnbuf ibuff = NULL;
1942 static xtensa_insnbuf sbuff = NULL;
1943 bfd_vma self_address;
1944 bfd_size_type input_size;
1950 ibuff = xtensa_insnbuf_alloc (isa);
1951 sbuff = xtensa_insnbuf_alloc (isa);
1954 input_size = bfd_get_section_limit (abfd, input_section);
1956 /* Calculate the PC address for this instruction. */
1957 self_address = (input_section->output_section->vma
1958 + input_section->output_offset
1961 switch (howto->type)
1964 case R_XTENSA_DIFF8:
1965 case R_XTENSA_DIFF16:
1966 case R_XTENSA_DIFF32:
1967 case R_XTENSA_TLS_FUNC:
1968 case R_XTENSA_TLS_ARG:
1969 case R_XTENSA_TLS_CALL:
1970 return bfd_reloc_ok;
1972 case R_XTENSA_ASM_EXPAND:
1975 /* Check for windowed CALL across a 1GB boundary. */
1976 opcode = get_expanded_call_opcode (contents + address,
1977 input_size - address, 0);
1978 if (is_windowed_call_opcode (opcode))
1980 if ((self_address >> CALL_SEGMENT_BITS)
1981 != (relocation >> CALL_SEGMENT_BITS))
1983 *error_message = "windowed longcall crosses 1GB boundary; "
1985 return bfd_reloc_dangerous;
1989 return bfd_reloc_ok;
1991 case R_XTENSA_ASM_SIMPLIFY:
1993 /* Convert the L32R/CALLX to CALL. */
1994 bfd_reloc_status_type retval =
1995 elf_xtensa_do_asm_simplify (contents, address, input_size,
1997 if (retval != bfd_reloc_ok)
1998 return bfd_reloc_dangerous;
2000 /* The CALL needs to be relocated. Continue below for that part. */
2003 howto = &elf_howto_table[(unsigned) R_XTENSA_SLOT0_OP ];
2010 x = bfd_get_32 (abfd, contents + address);
2012 bfd_put_32 (abfd, x, contents + address);
2014 return bfd_reloc_ok;
2016 case R_XTENSA_32_PCREL:
2017 bfd_put_32 (abfd, relocation - self_address, contents + address);
2018 return bfd_reloc_ok;
2021 case R_XTENSA_TLSDESC_FN:
2022 case R_XTENSA_TLSDESC_ARG:
2023 case R_XTENSA_TLS_DTPOFF:
2024 case R_XTENSA_TLS_TPOFF:
2025 bfd_put_32 (abfd, relocation, contents + address);
2026 return bfd_reloc_ok;
2029 /* Only instruction slot-specific relocations handled below.... */
2030 slot = get_relocation_slot (howto->type);
2031 if (slot == XTENSA_UNDEFINED)
2033 *error_message = "unexpected relocation";
2034 return bfd_reloc_dangerous;
2037 /* Read the instruction into a buffer and decode the opcode. */
2038 xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
2039 input_size - address);
2040 fmt = xtensa_format_decode (isa, ibuff);
2041 if (fmt == XTENSA_UNDEFINED)
2043 *error_message = "cannot decode instruction format";
2044 return bfd_reloc_dangerous;
2047 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
2049 opcode = xtensa_opcode_decode (isa, fmt, slot, sbuff);
2050 if (opcode == XTENSA_UNDEFINED)
2052 *error_message = "cannot decode instruction opcode";
2053 return bfd_reloc_dangerous;
2056 /* Check for opcode-specific "alternate" relocations. */
2057 if (is_alt_relocation (howto->type))
2059 if (opcode == get_l32r_opcode ())
2061 /* Handle the special-case of non-PC-relative L32R instructions. */
2062 bfd *output_bfd = input_section->output_section->owner;
2063 asection *lit4_sec = bfd_get_section_by_name (output_bfd, ".lit4");
2066 *error_message = "relocation references missing .lit4 section";
2067 return bfd_reloc_dangerous;
2069 self_address = ((lit4_sec->vma & ~0xfff)
2070 + 0x40000 - 3); /* -3 to compensate for do_reloc */
2071 newval = relocation;
2074 else if (opcode == get_const16_opcode ())
2076 /* ALT used for high 16 bits. */
2077 newval = relocation >> 16;
2082 /* No other "alternate" relocations currently defined. */
2083 *error_message = "unexpected relocation";
2084 return bfd_reloc_dangerous;
2087 else /* Not an "alternate" relocation.... */
2089 if (opcode == get_const16_opcode ())
2091 newval = relocation & 0xffff;
2096 /* ...normal PC-relative relocation.... */
2098 /* Determine which operand is being relocated. */
2099 opnd = get_relocation_opnd (opcode, howto->type);
2100 if (opnd == XTENSA_UNDEFINED)
2102 *error_message = "unexpected relocation";
2103 return bfd_reloc_dangerous;
2106 if (!howto->pc_relative)
2108 *error_message = "expected PC-relative relocation";
2109 return bfd_reloc_dangerous;
2112 newval = relocation;
2116 /* Apply the relocation. */
2117 if (xtensa_operand_do_reloc (isa, opcode, opnd, &newval, self_address)
2118 || xtensa_operand_encode (isa, opcode, opnd, &newval)
2119 || xtensa_operand_set_field (isa, opcode, opnd, fmt, slot,
2122 const char *opname = xtensa_opcode_name (isa, opcode);
2125 msg = "cannot encode";
2126 if (is_direct_call_opcode (opcode))
2128 if ((relocation & 0x3) != 0)
2129 msg = "misaligned call target";
2131 msg = "call target out of range";
2133 else if (opcode == get_l32r_opcode ())
2135 if ((relocation & 0x3) != 0)
2136 msg = "misaligned literal target";
2137 else if (is_alt_relocation (howto->type))
2138 msg = "literal target out of range (too many literals)";
2139 else if (self_address > relocation)
2140 msg = "literal target out of range (try using text-section-literals)";
2142 msg = "literal placed after use";
2145 *error_message = vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
2146 return bfd_reloc_dangerous;
2149 /* Check for calls across 1GB boundaries. */
2150 if (is_direct_call_opcode (opcode)
2151 && is_windowed_call_opcode (opcode))
2153 if ((self_address >> CALL_SEGMENT_BITS)
2154 != (relocation >> CALL_SEGMENT_BITS))
2157 "windowed call crosses 1GB boundary; return may fail";
2158 return bfd_reloc_dangerous;
2162 /* Write the modified instruction back out of the buffer. */
2163 xtensa_format_set_slot (isa, fmt, slot, ibuff, sbuff);
2164 xtensa_insnbuf_to_chars (isa, ibuff, contents + address,
2165 input_size - address);
2166 return bfd_reloc_ok;
2171 vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
2173 /* To reduce the size of the memory leak,
2174 we only use a single message buffer. */
2175 static bfd_size_type alloc_size = 0;
2176 static char *message = NULL;
2177 bfd_size_type orig_len, len = 0;
2178 bfd_boolean is_append;
2181 va_start (ap, arglen);
2183 is_append = (origmsg == message);
2185 orig_len = strlen (origmsg);
2186 len = orig_len + strlen (fmt) + arglen + 20;
2187 if (len > alloc_size)
2189 message = (char *) bfd_realloc_or_free (message, len);
2192 if (message != NULL)
2195 memcpy (message, origmsg, orig_len);
2196 vsprintf (message + orig_len, fmt, ap);
2203 /* This function is registered as the "special_function" in the
2204 Xtensa howto for handling simplify operations.
2205 bfd_perform_relocation / bfd_install_relocation use it to
2206 perform (install) the specified relocation. Since this replaces the code
2207 in bfd_perform_relocation, it is basically an Xtensa-specific,
2208 stripped-down version of bfd_perform_relocation. */
2210 static bfd_reloc_status_type
2211 bfd_elf_xtensa_reloc (bfd *abfd,
2212 arelent *reloc_entry,
2215 asection *input_section,
2217 char **error_message)
2220 bfd_reloc_status_type flag;
2221 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2222 bfd_vma output_base = 0;
2223 reloc_howto_type *howto = reloc_entry->howto;
2224 asection *reloc_target_output_section;
2225 bfd_boolean is_weak_undef;
2227 if (!xtensa_default_isa)
2228 xtensa_default_isa = xtensa_isa_init (0, 0);
2230 /* ELF relocs are against symbols. If we are producing relocatable
2231 output, and the reloc is against an external symbol, the resulting
2232 reloc will also be against the same symbol. In such a case, we
2233 don't want to change anything about the way the reloc is handled,
2234 since it will all be done at final link time. This test is similar
2235 to what bfd_elf_generic_reloc does except that it lets relocs with
2236 howto->partial_inplace go through even if the addend is non-zero.
2237 (The real problem is that partial_inplace is set for XTENSA_32
2238 relocs to begin with, but that's a long story and there's little we
2239 can do about it now....) */
2241 if (output_bfd && (symbol->flags & BSF_SECTION_SYM) == 0)
2243 reloc_entry->address += input_section->output_offset;
2244 return bfd_reloc_ok;
2247 /* Is the address of the relocation really within the section? */
2248 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2249 return bfd_reloc_outofrange;
2251 /* Work out which section the relocation is targeted at and the
2252 initial relocation command value. */
2254 /* Get symbol value. (Common symbols are special.) */
2255 if (bfd_is_com_section (symbol->section))
2258 relocation = symbol->value;
2260 reloc_target_output_section = symbol->section->output_section;
2262 /* Convert input-section-relative symbol value to absolute. */
2263 if ((output_bfd && !howto->partial_inplace)
2264 || reloc_target_output_section == NULL)
2267 output_base = reloc_target_output_section->vma;
2269 relocation += output_base + symbol->section->output_offset;
2271 /* Add in supplied addend. */
2272 relocation += reloc_entry->addend;
2274 /* Here the variable relocation holds the final address of the
2275 symbol we are relocating against, plus any addend. */
2278 if (!howto->partial_inplace)
2280 /* This is a partial relocation, and we want to apply the relocation
2281 to the reloc entry rather than the raw data. Everything except
2282 relocations against section symbols has already been handled
2285 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
2286 reloc_entry->addend = relocation;
2287 reloc_entry->address += input_section->output_offset;
2288 return bfd_reloc_ok;
2292 reloc_entry->address += input_section->output_offset;
2293 reloc_entry->addend = 0;
2297 is_weak_undef = (bfd_is_und_section (symbol->section)
2298 && (symbol->flags & BSF_WEAK) != 0);
2299 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
2300 (bfd_byte *) data, (bfd_vma) octets,
2301 is_weak_undef, error_message);
2303 if (flag == bfd_reloc_dangerous)
2305 /* Add the symbol name to the error message. */
2306 if (! *error_message)
2307 *error_message = "";
2308 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
2309 strlen (symbol->name) + 17,
2311 (unsigned long) reloc_entry->addend);
2318 /* Set up an entry in the procedure linkage table. */
2321 elf_xtensa_create_plt_entry (struct bfd_link_info *info,
2323 unsigned reloc_index)
2325 asection *splt, *sgotplt;
2326 bfd_vma plt_base, got_base;
2327 bfd_vma code_offset, lit_offset, abi_offset;
2330 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
2331 splt = elf_xtensa_get_plt_section (info, chunk);
2332 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
2333 BFD_ASSERT (splt != NULL && sgotplt != NULL);
2335 plt_base = splt->output_section->vma + splt->output_offset;
2336 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2338 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
2339 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
2341 /* Fill in the literal entry. This is the offset of the dynamic
2342 relocation entry. */
2343 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
2344 sgotplt->contents + lit_offset);
2346 /* Fill in the entry in the procedure linkage table. */
2347 memcpy (splt->contents + code_offset,
2348 (bfd_big_endian (output_bfd)
2349 ? elf_xtensa_be_plt_entry
2350 : elf_xtensa_le_plt_entry),
2352 abi_offset = XSHAL_ABI == XTHAL_ABI_WINDOWED ? 3 : 0;
2353 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
2354 plt_base + code_offset + abi_offset),
2355 splt->contents + code_offset + abi_offset + 1);
2356 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
2357 plt_base + code_offset + abi_offset + 3),
2358 splt->contents + code_offset + abi_offset + 4);
2359 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
2360 plt_base + code_offset + abi_offset + 6),
2361 splt->contents + code_offset + abi_offset + 7);
2363 return plt_base + code_offset;
2367 static bfd_boolean get_indirect_call_dest_reg (xtensa_opcode, unsigned *);
2370 replace_tls_insn (Elf_Internal_Rela *rel,
2372 asection *input_section,
2374 bfd_boolean is_ld_model,
2375 char **error_message)
2377 static xtensa_insnbuf ibuff = NULL;
2378 static xtensa_insnbuf sbuff = NULL;
2379 xtensa_isa isa = xtensa_default_isa;
2381 xtensa_opcode old_op, new_op;
2382 bfd_size_type input_size;
2384 unsigned dest_reg, src_reg;
2388 ibuff = xtensa_insnbuf_alloc (isa);
2389 sbuff = xtensa_insnbuf_alloc (isa);
2392 input_size = bfd_get_section_limit (abfd, input_section);
2394 /* Read the instruction into a buffer and decode the opcode. */
2395 xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
2396 input_size - rel->r_offset);
2397 fmt = xtensa_format_decode (isa, ibuff);
2398 if (fmt == XTENSA_UNDEFINED)
2400 *error_message = "cannot decode instruction format";
2404 BFD_ASSERT (xtensa_format_num_slots (isa, fmt) == 1);
2405 xtensa_format_get_slot (isa, fmt, 0, ibuff, sbuff);
2407 old_op = xtensa_opcode_decode (isa, fmt, 0, sbuff);
2408 if (old_op == XTENSA_UNDEFINED)
2410 *error_message = "cannot decode instruction opcode";
2414 r_type = ELF32_R_TYPE (rel->r_info);
2417 case R_XTENSA_TLS_FUNC:
2418 case R_XTENSA_TLS_ARG:
2419 if (old_op != get_l32r_opcode ()
2420 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2421 sbuff, &dest_reg) != 0)
2423 *error_message = "cannot extract L32R destination for TLS access";
2428 case R_XTENSA_TLS_CALL:
2429 if (! get_indirect_call_dest_reg (old_op, &dest_reg)
2430 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2431 sbuff, &src_reg) != 0)
2433 *error_message = "cannot extract CALLXn operands for TLS access";
2446 case R_XTENSA_TLS_FUNC:
2447 case R_XTENSA_TLS_ARG:
2448 /* Change the instruction to a NOP (or "OR a1, a1, a1" for older
2449 versions of Xtensa). */
2450 new_op = xtensa_opcode_lookup (isa, "nop");
2451 if (new_op == XTENSA_UNDEFINED)
2453 new_op = xtensa_opcode_lookup (isa, "or");
2454 if (new_op == XTENSA_UNDEFINED
2455 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2456 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2458 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2460 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2463 *error_message = "cannot encode OR for TLS access";
2469 if (xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0)
2471 *error_message = "cannot encode NOP for TLS access";
2477 case R_XTENSA_TLS_CALL:
2478 /* Read THREADPTR into the CALLX's return value register. */
2479 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2480 if (new_op == XTENSA_UNDEFINED
2481 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2482 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2483 sbuff, dest_reg + 2) != 0)
2485 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2495 case R_XTENSA_TLS_FUNC:
2496 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2497 if (new_op == XTENSA_UNDEFINED
2498 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2499 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2500 sbuff, dest_reg) != 0)
2502 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2507 case R_XTENSA_TLS_ARG:
2508 /* Nothing to do. Keep the original L32R instruction. */
2511 case R_XTENSA_TLS_CALL:
2512 /* Add the CALLX's src register (holding the THREADPTR value)
2513 to the first argument register (holding the offset) and put
2514 the result in the CALLX's return value register. */
2515 new_op = xtensa_opcode_lookup (isa, "add");
2516 if (new_op == XTENSA_UNDEFINED
2517 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2518 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2519 sbuff, dest_reg + 2) != 0
2520 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2521 sbuff, dest_reg + 2) != 0
2522 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2523 sbuff, src_reg) != 0)
2525 *error_message = "cannot encode ADD for TLS access";
2532 xtensa_format_set_slot (isa, fmt, 0, ibuff, sbuff);
2533 xtensa_insnbuf_to_chars (isa, ibuff, contents + rel->r_offset,
2534 input_size - rel->r_offset);
2540 #define IS_XTENSA_TLS_RELOC(R_TYPE) \
2541 ((R_TYPE) == R_XTENSA_TLSDESC_FN \
2542 || (R_TYPE) == R_XTENSA_TLSDESC_ARG \
2543 || (R_TYPE) == R_XTENSA_TLS_DTPOFF \
2544 || (R_TYPE) == R_XTENSA_TLS_TPOFF \
2545 || (R_TYPE) == R_XTENSA_TLS_FUNC \
2546 || (R_TYPE) == R_XTENSA_TLS_ARG \
2547 || (R_TYPE) == R_XTENSA_TLS_CALL)
2549 /* Relocate an Xtensa ELF section. This is invoked by the linker for
2550 both relocatable and final links. */
2553 elf_xtensa_relocate_section (bfd *output_bfd,
2554 struct bfd_link_info *info,
2556 asection *input_section,
2558 Elf_Internal_Rela *relocs,
2559 Elf_Internal_Sym *local_syms,
2560 asection **local_sections)
2562 struct elf_xtensa_link_hash_table *htab;
2563 Elf_Internal_Shdr *symtab_hdr;
2564 Elf_Internal_Rela *rel;
2565 Elf_Internal_Rela *relend;
2566 struct elf_link_hash_entry **sym_hashes;
2567 property_table_entry *lit_table = 0;
2569 char *local_got_tls_types;
2570 char *error_message = NULL;
2571 bfd_size_type input_size;
2574 if (!xtensa_default_isa)
2575 xtensa_default_isa = xtensa_isa_init (0, 0);
2577 BFD_ASSERT (is_xtensa_elf (input_bfd));
2579 htab = elf_xtensa_hash_table (info);
2583 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2584 sym_hashes = elf_sym_hashes (input_bfd);
2585 local_got_tls_types = elf_xtensa_local_got_tls_type (input_bfd);
2587 if (elf_hash_table (info)->dynamic_sections_created)
2589 ltblsize = xtensa_read_table_entries (input_bfd, input_section,
2590 &lit_table, XTENSA_LIT_SEC_NAME,
2596 input_size = bfd_get_section_limit (input_bfd, input_section);
2599 relend = relocs + input_section->reloc_count;
2600 for (; rel < relend; rel++)
2603 reloc_howto_type *howto;
2604 unsigned long r_symndx;
2605 struct elf_link_hash_entry *h;
2606 Elf_Internal_Sym *sym;
2611 bfd_reloc_status_type r;
2612 bfd_boolean is_weak_undef;
2613 bfd_boolean unresolved_reloc;
2615 bfd_boolean dynamic_symbol;
2617 r_type = ELF32_R_TYPE (rel->r_info);
2618 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
2619 || r_type == (int) R_XTENSA_GNU_VTENTRY)
2622 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
2624 bfd_set_error (bfd_error_bad_value);
2627 howto = &elf_howto_table[r_type];
2629 r_symndx = ELF32_R_SYM (rel->r_info);
2634 is_weak_undef = FALSE;
2635 unresolved_reloc = FALSE;
2638 if (howto->partial_inplace && !bfd_link_relocatable (info))
2640 /* Because R_XTENSA_32 was made partial_inplace to fix some
2641 problems with DWARF info in partial links, there may be
2642 an addend stored in the contents. Take it out of there
2643 and move it back into the addend field of the reloc. */
2644 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
2645 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
2648 if (r_symndx < symtab_hdr->sh_info)
2650 sym = local_syms + r_symndx;
2651 sym_type = ELF32_ST_TYPE (sym->st_info);
2652 sec = local_sections[r_symndx];
2653 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2657 bfd_boolean ignored;
2659 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2660 r_symndx, symtab_hdr, sym_hashes,
2662 unresolved_reloc, warned, ignored);
2665 && !unresolved_reloc
2666 && h->root.type == bfd_link_hash_undefweak)
2667 is_weak_undef = TRUE;
2672 if (sec != NULL && discarded_section (sec))
2673 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2674 rel, 1, relend, howto, 0, contents);
2676 if (bfd_link_relocatable (info))
2679 asection * sym_sec = get_elf_r_symndx_section (input_bfd, r_symndx);
2681 /* This is a relocatable link.
2682 1) If the reloc is against a section symbol, adjust
2683 according to the output section.
2684 2) If there is a new target for this relocation,
2685 the new target will be in the same output section.
2686 We adjust the relocation by the output section
2689 if (relaxing_section)
2691 /* Check if this references a section in another input file. */
2692 if (!do_fix_for_relocatable_link (rel, input_bfd, input_section,
2697 dest_addr = sym_sec->output_section->vma + sym_sec->output_offset
2698 + get_elf_r_symndx_offset (input_bfd, r_symndx) + rel->r_addend;
2700 if (r_type == R_XTENSA_ASM_SIMPLIFY)
2702 error_message = NULL;
2703 /* Convert ASM_SIMPLIFY into the simpler relocation
2704 so that they never escape a relaxing link. */
2705 r = contract_asm_expansion (contents, input_size, rel,
2707 if (r != bfd_reloc_ok)
2708 (*info->callbacks->reloc_dangerous)
2709 (info, error_message,
2710 input_bfd, input_section, rel->r_offset);
2712 r_type = ELF32_R_TYPE (rel->r_info);
2715 /* This is a relocatable link, so we don't have to change
2716 anything unless the reloc is against a section symbol,
2717 in which case we have to adjust according to where the
2718 section symbol winds up in the output section. */
2719 if (r_symndx < symtab_hdr->sh_info)
2721 sym = local_syms + r_symndx;
2722 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2724 sec = local_sections[r_symndx];
2725 rel->r_addend += sec->output_offset + sym->st_value;
2729 /* If there is an addend with a partial_inplace howto,
2730 then move the addend to the contents. This is a hack
2731 to work around problems with DWARF in relocatable links
2732 with some previous version of BFD. Now we can't easily get
2733 rid of the hack without breaking backward compatibility.... */
2735 howto = &elf_howto_table[r_type];
2736 if (howto->partial_inplace && rel->r_addend)
2738 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2739 rel->r_addend, contents,
2740 rel->r_offset, FALSE,
2746 /* Put the correct bits in the target instruction, even
2747 though the relocation will still be present in the output
2748 file. This makes disassembly clearer, as well as
2749 allowing loadable kernel modules to work without needing
2750 relocations on anything other than calls and l32r's. */
2752 /* If it is not in the same section, there is nothing we can do. */
2753 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP &&
2754 sym_sec->output_section == input_section->output_section)
2756 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2757 dest_addr, contents,
2758 rel->r_offset, FALSE,
2762 if (r != bfd_reloc_ok)
2763 (*info->callbacks->reloc_dangerous)
2764 (info, error_message,
2765 input_bfd, input_section, rel->r_offset);
2767 /* Done with work for relocatable link; continue with next reloc. */
2771 /* This is a final link. */
2773 if (relaxing_section)
2775 /* Check if this references a section in another input file. */
2776 do_fix_for_final_link (rel, input_bfd, input_section, contents,
2780 /* Sanity check the address. */
2781 if (rel->r_offset >= input_size
2782 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2785 /* xgettext:c-format */
2786 (_("%B(%A+0x%lx): relocation offset out of range (size=0x%x)"),
2787 input_bfd, input_section, rel->r_offset, input_size);
2788 bfd_set_error (bfd_error_bad_value);
2793 name = h->root.root.string;
2796 name = (bfd_elf_string_from_elf_section
2797 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2798 if (name == NULL || *name == '\0')
2799 name = bfd_section_name (input_bfd, sec);
2802 if (r_symndx != STN_UNDEF
2803 && r_type != R_XTENSA_NONE
2805 || h->root.type == bfd_link_hash_defined
2806 || h->root.type == bfd_link_hash_defweak)
2807 && IS_XTENSA_TLS_RELOC (r_type) != (sym_type == STT_TLS))
2810 ((sym_type == STT_TLS
2811 /* xgettext:c-format */
2812 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
2813 /* xgettext:c-format */
2814 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
2817 (long) rel->r_offset,
2822 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
2824 tls_type = GOT_UNKNOWN;
2826 tls_type = elf_xtensa_hash_entry (h)->tls_type;
2827 else if (local_got_tls_types)
2828 tls_type = local_got_tls_types [r_symndx];
2834 if (elf_hash_table (info)->dynamic_sections_created
2835 && (input_section->flags & SEC_ALLOC) != 0
2836 && (dynamic_symbol || bfd_link_pic (info)))
2838 Elf_Internal_Rela outrel;
2842 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2843 srel = htab->srelplt;
2845 srel = htab->srelgot;
2847 BFD_ASSERT (srel != NULL);
2850 _bfd_elf_section_offset (output_bfd, info,
2851 input_section, rel->r_offset);
2853 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2854 memset (&outrel, 0, sizeof outrel);
2857 outrel.r_offset += (input_section->output_section->vma
2858 + input_section->output_offset);
2860 /* Complain if the relocation is in a read-only section
2861 and not in a literal pool. */
2862 if ((input_section->flags & SEC_READONLY) != 0
2863 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
2867 _("dynamic relocation in read-only section");
2868 (*info->callbacks->reloc_dangerous)
2869 (info, error_message,
2870 input_bfd, input_section, rel->r_offset);
2875 outrel.r_addend = rel->r_addend;
2878 if (r_type == R_XTENSA_32)
2881 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2884 else /* r_type == R_XTENSA_PLT */
2887 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2889 /* Create the PLT entry and set the initial
2890 contents of the literal entry to the address of
2893 elf_xtensa_create_plt_entry (info, output_bfd,
2896 unresolved_reloc = FALSE;
2900 /* Generate a RELATIVE relocation. */
2901 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2902 outrel.r_addend = 0;
2906 loc = (srel->contents
2907 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2908 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2909 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2912 else if (r_type == R_XTENSA_ASM_EXPAND && dynamic_symbol)
2914 /* This should only happen for non-PIC code, which is not
2915 supposed to be used on systems with dynamic linking.
2916 Just ignore these relocations. */
2921 case R_XTENSA_TLS_TPOFF:
2922 /* Switch to LE model for local symbols in an executable. */
2923 if (! bfd_link_pic (info) && ! dynamic_symbol)
2925 relocation = tpoff (info, relocation);
2930 case R_XTENSA_TLSDESC_FN:
2931 case R_XTENSA_TLSDESC_ARG:
2933 if (r_type == R_XTENSA_TLSDESC_FN)
2935 if (! bfd_link_pic (info) || (tls_type & GOT_TLS_IE) != 0)
2936 r_type = R_XTENSA_NONE;
2938 else if (r_type == R_XTENSA_TLSDESC_ARG)
2940 if (bfd_link_pic (info))
2942 if ((tls_type & GOT_TLS_IE) != 0)
2943 r_type = R_XTENSA_TLS_TPOFF;
2947 r_type = R_XTENSA_TLS_TPOFF;
2948 if (! dynamic_symbol)
2950 relocation = tpoff (info, relocation);
2956 if (r_type == R_XTENSA_NONE)
2957 /* Nothing to do here; skip to the next reloc. */
2960 if (! elf_hash_table (info)->dynamic_sections_created)
2963 _("TLS relocation invalid without dynamic sections");
2964 (*info->callbacks->reloc_dangerous)
2965 (info, error_message,
2966 input_bfd, input_section, rel->r_offset);
2970 Elf_Internal_Rela outrel;
2972 asection *srel = htab->srelgot;
2975 outrel.r_offset = (input_section->output_section->vma
2976 + input_section->output_offset
2979 /* Complain if the relocation is in a read-only section
2980 and not in a literal pool. */
2981 if ((input_section->flags & SEC_READONLY) != 0
2982 && ! elf_xtensa_in_literal_pool (lit_table, ltblsize,
2986 _("dynamic relocation in read-only section");
2987 (*info->callbacks->reloc_dangerous)
2988 (info, error_message,
2989 input_bfd, input_section, rel->r_offset);
2992 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2994 outrel.r_addend = relocation - dtpoff_base (info);
2996 outrel.r_addend = 0;
2999 outrel.r_info = ELF32_R_INFO (indx, r_type);
3001 unresolved_reloc = FALSE;
3004 loc = (srel->contents
3005 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
3006 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3007 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
3013 case R_XTENSA_TLS_DTPOFF:
3014 if (! bfd_link_pic (info))
3015 /* Switch from LD model to LE model. */
3016 relocation = tpoff (info, relocation);
3018 relocation -= dtpoff_base (info);
3021 case R_XTENSA_TLS_FUNC:
3022 case R_XTENSA_TLS_ARG:
3023 case R_XTENSA_TLS_CALL:
3024 /* Check if optimizing to IE or LE model. */
3025 if ((tls_type & GOT_TLS_IE) != 0)
3027 bfd_boolean is_ld_model =
3028 (h && elf_xtensa_hash_entry (h) == htab->tlsbase);
3029 if (! replace_tls_insn (rel, input_bfd, input_section, contents,
3030 is_ld_model, &error_message))
3031 (*info->callbacks->reloc_dangerous)
3032 (info, error_message,
3033 input_bfd, input_section, rel->r_offset);
3035 if (r_type != R_XTENSA_TLS_ARG || is_ld_model)
3037 /* Skip subsequent relocations on the same instruction. */
3038 while (rel + 1 < relend && rel[1].r_offset == rel->r_offset)
3045 if (elf_hash_table (info)->dynamic_sections_created
3046 && dynamic_symbol && (is_operand_relocation (r_type)
3047 || r_type == R_XTENSA_32_PCREL))
3050 vsprint_msg ("invalid relocation for dynamic symbol", ": %s",
3051 strlen (name) + 2, name);
3052 (*info->callbacks->reloc_dangerous)
3053 (info, error_message, input_bfd, input_section, rel->r_offset);
3059 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3060 because such sections are not SEC_ALLOC and thus ld.so will
3061 not process them. */
3062 if (unresolved_reloc
3063 && !((input_section->flags & SEC_DEBUGGING) != 0
3065 && _bfd_elf_section_offset (output_bfd, info, input_section,
3066 rel->r_offset) != (bfd_vma) -1)
3069 /* xgettext:c-format */
3070 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3073 (long) rel->r_offset,
3079 /* TLS optimizations may have changed r_type; update "howto". */
3080 howto = &elf_howto_table[r_type];
3082 /* There's no point in calling bfd_perform_relocation here.
3083 Just go directly to our "special function". */
3084 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
3085 relocation + rel->r_addend,
3086 contents, rel->r_offset, is_weak_undef,
3089 if (r != bfd_reloc_ok && !warned)
3091 BFD_ASSERT (r == bfd_reloc_dangerous || r == bfd_reloc_other);
3092 BFD_ASSERT (error_message != NULL);
3094 if (rel->r_addend == 0)
3095 error_message = vsprint_msg (error_message, ": %s",
3096 strlen (name) + 2, name);
3098 error_message = vsprint_msg (error_message, ": (%s+0x%x)",
3100 name, (int) rel->r_addend);
3102 (*info->callbacks->reloc_dangerous)
3103 (info, error_message, input_bfd, input_section, rel->r_offset);
3110 input_section->reloc_done = TRUE;
3116 /* Finish up dynamic symbol handling. There's not much to do here since
3117 the PLT and GOT entries are all set up by relocate_section. */
3120 elf_xtensa_finish_dynamic_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
3121 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3122 struct elf_link_hash_entry *h,
3123 Elf_Internal_Sym *sym)
3125 if (h->needs_plt && !h->def_regular)
3127 /* Mark the symbol as undefined, rather than as defined in
3128 the .plt section. Leave the value alone. */
3129 sym->st_shndx = SHN_UNDEF;
3130 /* If the symbol is weak, we do need to clear the value.
3131 Otherwise, the PLT entry would provide a definition for
3132 the symbol even if the symbol wasn't defined anywhere,
3133 and so the symbol would never be NULL. */
3134 if (!h->ref_regular_nonweak)
3138 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3139 if (h == elf_hash_table (info)->hdynamic
3140 || h == elf_hash_table (info)->hgot)
3141 sym->st_shndx = SHN_ABS;
3147 /* Combine adjacent literal table entries in the output. Adjacent
3148 entries within each input section may have been removed during
3149 relaxation, but we repeat the process here, even though it's too late
3150 to shrink the output section, because it's important to minimize the
3151 number of literal table entries to reduce the start-up work for the
3152 runtime linker. Returns the number of remaining table entries or -1
3156 elf_xtensa_combine_prop_entries (bfd *output_bfd,
3161 property_table_entry *table;
3162 bfd_size_type section_size, sgotloc_size;
3166 section_size = sxtlit->size;
3167 BFD_ASSERT (section_size % 8 == 0);
3168 num = section_size / 8;
3170 sgotloc_size = sgotloc->size;
3171 if (sgotloc_size != section_size)
3174 (_("internal inconsistency in size of .got.loc section"));
3178 table = bfd_malloc (num * sizeof (property_table_entry));
3182 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
3183 propagates to the output section, where it doesn't really apply and
3184 where it breaks the following call to bfd_malloc_and_get_section. */
3185 sxtlit->flags &= ~SEC_IN_MEMORY;
3187 if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
3195 /* There should never be any relocations left at this point, so this
3196 is quite a bit easier than what is done during relaxation. */
3198 /* Copy the raw contents into a property table array and sort it. */
3200 for (n = 0; n < num; n++)
3202 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
3203 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
3206 qsort (table, num, sizeof (property_table_entry), property_table_compare);
3208 for (n = 0; n < num; n++)
3210 bfd_boolean remove_entry = FALSE;
3212 if (table[n].size == 0)
3213 remove_entry = TRUE;
3215 && (table[n-1].address + table[n-1].size == table[n].address))
3217 table[n-1].size += table[n].size;
3218 remove_entry = TRUE;
3223 for (m = n; m < num - 1; m++)
3225 table[m].address = table[m+1].address;
3226 table[m].size = table[m+1].size;
3234 /* Copy the data back to the raw contents. */
3236 for (n = 0; n < num; n++)
3238 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
3239 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
3243 /* Clear the removed bytes. */
3244 if ((bfd_size_type) (num * 8) < section_size)
3245 memset (&contents[num * 8], 0, section_size - num * 8);
3247 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
3251 /* Copy the contents to ".got.loc". */
3252 memcpy (sgotloc->contents, contents, section_size);
3260 /* Finish up the dynamic sections. */
3263 elf_xtensa_finish_dynamic_sections (bfd *output_bfd,
3264 struct bfd_link_info *info)
3266 struct elf_xtensa_link_hash_table *htab;
3268 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
3269 Elf32_External_Dyn *dyncon, *dynconend;
3270 int num_xtlit_entries = 0;
3272 if (! elf_hash_table (info)->dynamic_sections_created)
3275 htab = elf_xtensa_hash_table (info);
3279 dynobj = elf_hash_table (info)->dynobj;
3280 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3281 BFD_ASSERT (sdyn != NULL);
3283 /* Set the first entry in the global offset table to the address of
3284 the dynamic section. */
3288 BFD_ASSERT (sgot->size == 4);
3290 bfd_put_32 (output_bfd, 0, sgot->contents);
3292 bfd_put_32 (output_bfd,
3293 sdyn->output_section->vma + sdyn->output_offset,
3297 srelplt = htab->srelplt;
3298 if (srelplt && srelplt->size != 0)
3300 asection *sgotplt, *srelgot, *spltlittbl;
3301 int chunk, plt_chunks, plt_entries;
3302 Elf_Internal_Rela irela;
3304 unsigned rtld_reloc;
3306 srelgot = htab->srelgot;
3307 spltlittbl = htab->spltlittbl;
3308 BFD_ASSERT (srelgot != NULL && spltlittbl != NULL);
3310 /* Find the first XTENSA_RTLD relocation. Presumably the rest
3311 of them follow immediately after.... */
3312 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
3314 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3315 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3316 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
3319 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
3321 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
3323 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
3325 for (chunk = 0; chunk < plt_chunks; chunk++)
3327 int chunk_entries = 0;
3329 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
3330 BFD_ASSERT (sgotplt != NULL);
3332 /* Emit special RTLD relocations for the first two entries in
3333 each chunk of the .got.plt section. */
3335 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3336 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3337 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3338 irela.r_offset = (sgotplt->output_section->vma
3339 + sgotplt->output_offset);
3340 irela.r_addend = 1; /* tell rtld to set value to resolver function */
3341 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3343 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3345 /* Next literal immediately follows the first. */
3346 loc += sizeof (Elf32_External_Rela);
3347 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3348 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3349 irela.r_offset = (sgotplt->output_section->vma
3350 + sgotplt->output_offset + 4);
3351 /* Tell rtld to set value to object's link map. */
3353 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3355 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3357 /* Fill in the literal table. */
3358 if (chunk < plt_chunks - 1)
3359 chunk_entries = PLT_ENTRIES_PER_CHUNK;
3361 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
3363 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->size);
3364 bfd_put_32 (output_bfd,
3365 sgotplt->output_section->vma + sgotplt->output_offset,
3366 spltlittbl->contents + (chunk * 8) + 0);
3367 bfd_put_32 (output_bfd,
3368 8 + (chunk_entries * 4),
3369 spltlittbl->contents + (chunk * 8) + 4);
3372 /* All the dynamic relocations have been emitted at this point.
3373 Make sure the relocation sections are the correct size. */
3374 if (srelgot->size != (sizeof (Elf32_External_Rela)
3375 * srelgot->reloc_count)
3376 || srelplt->size != (sizeof (Elf32_External_Rela)
3377 * srelplt->reloc_count))
3380 /* The .xt.lit.plt section has just been modified. This must
3381 happen before the code below which combines adjacent literal
3382 table entries, and the .xt.lit.plt contents have to be forced to
3384 if (! bfd_set_section_contents (output_bfd,
3385 spltlittbl->output_section,
3386 spltlittbl->contents,
3387 spltlittbl->output_offset,
3390 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
3391 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
3394 /* Combine adjacent literal table entries. */
3395 BFD_ASSERT (! bfd_link_relocatable (info));
3396 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
3397 sgotloc = htab->sgotloc;
3398 BFD_ASSERT (sgotloc);
3402 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
3403 if (num_xtlit_entries < 0)
3407 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3408 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3409 for (; dyncon < dynconend; dyncon++)
3411 Elf_Internal_Dyn dyn;
3413 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3420 case DT_XTENSA_GOT_LOC_SZ:
3421 dyn.d_un.d_val = num_xtlit_entries;
3424 case DT_XTENSA_GOT_LOC_OFF:
3425 dyn.d_un.d_ptr = (htab->sgotloc->output_section->vma
3426 + htab->sgotloc->output_offset);
3430 dyn.d_un.d_ptr = (htab->sgot->output_section->vma
3431 + htab->sgot->output_offset);
3435 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
3436 + htab->srelplt->output_offset);
3440 dyn.d_un.d_val = htab->srelplt->size;
3444 /* Adjust RELASZ to not include JMPREL. This matches what
3445 glibc expects and what is done for several other ELF
3446 targets (e.g., i386, alpha), but the "correct" behavior
3447 seems to be unresolved. Since the linker script arranges
3448 for .rela.plt to follow all other relocation sections, we
3449 don't have to worry about changing the DT_RELA entry. */
3451 dyn.d_un.d_val -= htab->srelplt->size;
3455 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3462 /* Functions for dealing with the e_flags field. */
3464 /* Merge backend specific data from an object file to the output
3465 object file when linking. */
3468 elf_xtensa_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3470 bfd *obfd = info->output_bfd;
3471 unsigned out_mach, in_mach;
3472 flagword out_flag, in_flag;
3474 /* Check if we have the same endianness. */
3475 if (!_bfd_generic_verify_endian_match (ibfd, info))
3478 /* Don't even pretend to support mixed-format linking. */
3479 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3480 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3483 out_flag = elf_elfheader (obfd)->e_flags;
3484 in_flag = elf_elfheader (ibfd)->e_flags;
3486 out_mach = out_flag & EF_XTENSA_MACH;
3487 in_mach = in_flag & EF_XTENSA_MACH;
3488 if (out_mach != in_mach)
3491 /* xgettext:c-format */
3492 (_("%B: incompatible machine type. Output is 0x%x. Input is 0x%x"),
3493 ibfd, out_mach, in_mach);
3494 bfd_set_error (bfd_error_wrong_format);
3498 if (! elf_flags_init (obfd))
3500 elf_flags_init (obfd) = TRUE;
3501 elf_elfheader (obfd)->e_flags = in_flag;
3503 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3504 && bfd_get_arch_info (obfd)->the_default)
3505 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3506 bfd_get_mach (ibfd));
3511 if ((out_flag & EF_XTENSA_XT_INSN) != (in_flag & EF_XTENSA_XT_INSN))
3512 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
3514 if ((out_flag & EF_XTENSA_XT_LIT) != (in_flag & EF_XTENSA_XT_LIT))
3515 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
3522 elf_xtensa_set_private_flags (bfd *abfd, flagword flags)
3524 BFD_ASSERT (!elf_flags_init (abfd)
3525 || elf_elfheader (abfd)->e_flags == flags);
3527 elf_elfheader (abfd)->e_flags |= flags;
3528 elf_flags_init (abfd) = TRUE;
3535 elf_xtensa_print_private_bfd_data (bfd *abfd, void *farg)
3537 FILE *f = (FILE *) farg;
3538 flagword e_flags = elf_elfheader (abfd)->e_flags;
3540 fprintf (f, "\nXtensa header:\n");
3541 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
3542 fprintf (f, "\nMachine = Base\n");
3544 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
3546 fprintf (f, "Insn tables = %s\n",
3547 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
3549 fprintf (f, "Literal tables = %s\n",
3550 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
3552 return _bfd_elf_print_private_bfd_data (abfd, farg);
3556 /* Set the right machine number for an Xtensa ELF file. */
3559 elf_xtensa_object_p (bfd *abfd)
3562 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
3567 mach = bfd_mach_xtensa;
3573 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
3578 /* The final processing done just before writing out an Xtensa ELF object
3579 file. This gets the Xtensa architecture right based on the machine
3583 elf_xtensa_final_write_processing (bfd *abfd,
3584 bfd_boolean linker ATTRIBUTE_UNUSED)
3589 switch (mach = bfd_get_mach (abfd))
3591 case bfd_mach_xtensa:
3592 val = E_XTENSA_MACH;
3598 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
3599 elf_elfheader (abfd)->e_flags |= val;
3603 static enum elf_reloc_type_class
3604 elf_xtensa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3605 const asection *rel_sec ATTRIBUTE_UNUSED,
3606 const Elf_Internal_Rela *rela)
3608 switch ((int) ELF32_R_TYPE (rela->r_info))
3610 case R_XTENSA_RELATIVE:
3611 return reloc_class_relative;
3612 case R_XTENSA_JMP_SLOT:
3613 return reloc_class_plt;
3615 return reloc_class_normal;
3621 elf_xtensa_discard_info_for_section (bfd *abfd,
3622 struct elf_reloc_cookie *cookie,
3623 struct bfd_link_info *info,
3627 bfd_vma offset, actual_offset;
3628 bfd_size_type removed_bytes = 0;
3629 bfd_size_type entry_size;
3631 if (sec->output_section
3632 && bfd_is_abs_section (sec->output_section))
3635 if (xtensa_is_proptable_section (sec))
3640 if (sec->size == 0 || sec->size % entry_size != 0)
3643 contents = retrieve_contents (abfd, sec, info->keep_memory);
3647 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
3650 release_contents (sec, contents);
3654 /* Sort the relocations. They should already be in order when
3655 relaxation is enabled, but it might not be. */
3656 qsort (cookie->rels, sec->reloc_count, sizeof (Elf_Internal_Rela),
3657 internal_reloc_compare);
3659 cookie->rel = cookie->rels;
3660 cookie->relend = cookie->rels + sec->reloc_count;
3662 for (offset = 0; offset < sec->size; offset += entry_size)
3664 actual_offset = offset - removed_bytes;
3666 /* The ...symbol_deleted_p function will skip over relocs but it
3667 won't adjust their offsets, so do that here. */
3668 while (cookie->rel < cookie->relend
3669 && cookie->rel->r_offset < offset)
3671 cookie->rel->r_offset -= removed_bytes;
3675 while (cookie->rel < cookie->relend
3676 && cookie->rel->r_offset == offset)
3678 if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
3680 /* Remove the table entry. (If the reloc type is NONE, then
3681 the entry has already been merged with another and deleted
3682 during relaxation.) */
3683 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
3685 /* Shift the contents up. */
3686 if (offset + entry_size < sec->size)
3687 memmove (&contents[actual_offset],
3688 &contents[actual_offset + entry_size],
3689 sec->size - offset - entry_size);
3690 removed_bytes += entry_size;
3693 /* Remove this relocation. */
3694 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
3697 /* Adjust the relocation offset for previous removals. This
3698 should not be done before calling ...symbol_deleted_p
3699 because it might mess up the offset comparisons there.
3700 Make sure the offset doesn't underflow in the case where
3701 the first entry is removed. */
3702 if (cookie->rel->r_offset >= removed_bytes)
3703 cookie->rel->r_offset -= removed_bytes;
3705 cookie->rel->r_offset = 0;
3711 if (removed_bytes != 0)
3713 /* Adjust any remaining relocs (shouldn't be any). */
3714 for (; cookie->rel < cookie->relend; cookie->rel++)
3716 if (cookie->rel->r_offset >= removed_bytes)
3717 cookie->rel->r_offset -= removed_bytes;
3719 cookie->rel->r_offset = 0;
3722 /* Clear the removed bytes. */
3723 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
3725 pin_contents (sec, contents);
3726 pin_internal_relocs (sec, cookie->rels);
3729 if (sec->rawsize == 0)
3730 sec->rawsize = sec->size;
3731 sec->size -= removed_bytes;
3733 if (xtensa_is_littable_section (sec))
3735 asection *sgotloc = elf_xtensa_hash_table (info)->sgotloc;
3737 sgotloc->size -= removed_bytes;
3742 release_contents (sec, contents);
3743 release_internal_relocs (sec, cookie->rels);
3746 return (removed_bytes != 0);
3751 elf_xtensa_discard_info (bfd *abfd,
3752 struct elf_reloc_cookie *cookie,
3753 struct bfd_link_info *info)
3756 bfd_boolean changed = FALSE;
3758 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3760 if (xtensa_is_property_section (sec))
3762 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
3772 elf_xtensa_ignore_discarded_relocs (asection *sec)
3774 return xtensa_is_property_section (sec);
3779 elf_xtensa_action_discarded (asection *sec)
3781 if (strcmp (".xt_except_table", sec->name) == 0)
3784 if (strcmp (".xt_except_desc", sec->name) == 0)
3787 return _bfd_elf_default_action_discarded (sec);
3791 /* Support for core dump NOTE sections. */
3794 elf_xtensa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3799 /* The size for Xtensa is variable, so don't try to recognize the format
3800 based on the size. Just assume this is GNU/Linux. */
3803 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3806 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
3810 size = note->descsz - offset - 4;
3812 /* Make a ".reg/999" section. */
3813 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3814 size, note->descpos + offset);
3819 elf_xtensa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3821 switch (note->descsz)
3826 case 128: /* GNU/Linux elf_prpsinfo */
3827 elf_tdata (abfd)->core->program
3828 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3829 elf_tdata (abfd)->core->command
3830 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3833 /* Note that for some reason, a spurious space is tacked
3834 onto the end of the args in some (at least one anyway)
3835 implementations, so strip it off if it exists. */
3838 char *command = elf_tdata (abfd)->core->command;
3839 int n = strlen (command);
3841 if (0 < n && command[n - 1] == ' ')
3842 command[n - 1] = '\0';
3849 /* Generic Xtensa configurability stuff. */
3851 static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
3852 static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
3853 static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
3854 static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
3855 static xtensa_opcode call0_op = XTENSA_UNDEFINED;
3856 static xtensa_opcode call4_op = XTENSA_UNDEFINED;
3857 static xtensa_opcode call8_op = XTENSA_UNDEFINED;
3858 static xtensa_opcode call12_op = XTENSA_UNDEFINED;
3861 init_call_opcodes (void)
3863 if (callx0_op == XTENSA_UNDEFINED)
3865 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
3866 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
3867 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
3868 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
3869 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
3870 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
3871 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
3872 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
3878 is_indirect_call_opcode (xtensa_opcode opcode)
3880 init_call_opcodes ();
3881 return (opcode == callx0_op
3882 || opcode == callx4_op
3883 || opcode == callx8_op
3884 || opcode == callx12_op);
3889 is_direct_call_opcode (xtensa_opcode opcode)
3891 init_call_opcodes ();
3892 return (opcode == call0_op
3893 || opcode == call4_op
3894 || opcode == call8_op
3895 || opcode == call12_op);
3900 is_windowed_call_opcode (xtensa_opcode opcode)
3902 init_call_opcodes ();
3903 return (opcode == call4_op
3904 || opcode == call8_op
3905 || opcode == call12_op
3906 || opcode == callx4_op
3907 || opcode == callx8_op
3908 || opcode == callx12_op);
3913 get_indirect_call_dest_reg (xtensa_opcode opcode, unsigned *pdst)
3915 unsigned dst = (unsigned) -1;
3917 init_call_opcodes ();
3918 if (opcode == callx0_op)
3920 else if (opcode == callx4_op)
3922 else if (opcode == callx8_op)
3924 else if (opcode == callx12_op)
3927 if (dst == (unsigned) -1)
3935 static xtensa_opcode
3936 get_const16_opcode (void)
3938 static bfd_boolean done_lookup = FALSE;
3939 static xtensa_opcode const16_opcode = XTENSA_UNDEFINED;
3942 const16_opcode = xtensa_opcode_lookup (xtensa_default_isa, "const16");
3945 return const16_opcode;
3949 static xtensa_opcode
3950 get_l32r_opcode (void)
3952 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
3953 static bfd_boolean done_lookup = FALSE;
3957 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
3965 l32r_offset (bfd_vma addr, bfd_vma pc)
3969 offset = addr - ((pc+3) & -4);
3970 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
3971 offset = (signed int) offset >> 2;
3972 BFD_ASSERT ((signed int) offset >> 16 == -1);
3978 get_relocation_opnd (xtensa_opcode opcode, int r_type)
3980 xtensa_isa isa = xtensa_default_isa;
3981 int last_immed, last_opnd, opi;
3983 if (opcode == XTENSA_UNDEFINED)
3984 return XTENSA_UNDEFINED;
3986 /* Find the last visible PC-relative immediate operand for the opcode.
3987 If there are no PC-relative immediates, then choose the last visible
3988 immediate; otherwise, fail and return XTENSA_UNDEFINED. */
3989 last_immed = XTENSA_UNDEFINED;
3990 last_opnd = xtensa_opcode_num_operands (isa, opcode);
3991 for (opi = last_opnd - 1; opi >= 0; opi--)
3993 if (xtensa_operand_is_visible (isa, opcode, opi) == 0)
3995 if (xtensa_operand_is_PCrelative (isa, opcode, opi) == 1)
4000 if (last_immed == XTENSA_UNDEFINED
4001 && xtensa_operand_is_register (isa, opcode, opi) == 0)
4005 return XTENSA_UNDEFINED;
4007 /* If the operand number was specified in an old-style relocation,
4008 check for consistency with the operand computed above. */
4009 if (r_type >= R_XTENSA_OP0 && r_type <= R_XTENSA_OP2)
4011 int reloc_opnd = r_type - R_XTENSA_OP0;
4012 if (reloc_opnd != last_immed)
4013 return XTENSA_UNDEFINED;
4021 get_relocation_slot (int r_type)
4031 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4032 return r_type - R_XTENSA_SLOT0_OP;
4033 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4034 return r_type - R_XTENSA_SLOT0_ALT;
4038 return XTENSA_UNDEFINED;
4042 /* Get the opcode for a relocation. */
4044 static xtensa_opcode
4045 get_relocation_opcode (bfd *abfd,
4048 Elf_Internal_Rela *irel)
4050 static xtensa_insnbuf ibuff = NULL;
4051 static xtensa_insnbuf sbuff = NULL;
4052 xtensa_isa isa = xtensa_default_isa;
4056 if (contents == NULL)
4057 return XTENSA_UNDEFINED;
4059 if (bfd_get_section_limit (abfd, sec) <= irel->r_offset)
4060 return XTENSA_UNDEFINED;
4064 ibuff = xtensa_insnbuf_alloc (isa);
4065 sbuff = xtensa_insnbuf_alloc (isa);
4068 /* Decode the instruction. */
4069 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
4070 sec->size - irel->r_offset);
4071 fmt = xtensa_format_decode (isa, ibuff);
4072 slot = get_relocation_slot (ELF32_R_TYPE (irel->r_info));
4073 if (slot == XTENSA_UNDEFINED)
4074 return XTENSA_UNDEFINED;
4075 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
4076 return xtensa_opcode_decode (isa, fmt, slot, sbuff);
4081 is_l32r_relocation (bfd *abfd,
4084 Elf_Internal_Rela *irel)
4086 xtensa_opcode opcode;
4087 if (!is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
4089 opcode = get_relocation_opcode (abfd, sec, contents, irel);
4090 return (opcode == get_l32r_opcode ());
4094 static bfd_size_type
4095 get_asm_simplify_size (bfd_byte *contents,
4096 bfd_size_type content_len,
4097 bfd_size_type offset)
4099 bfd_size_type insnlen, size = 0;
4101 /* Decode the size of the next two instructions. */
4102 insnlen = insn_decode_len (contents, content_len, offset);
4108 insnlen = insn_decode_len (contents, content_len, offset + size);
4118 is_alt_relocation (int r_type)
4120 return (r_type >= R_XTENSA_SLOT0_ALT
4121 && r_type <= R_XTENSA_SLOT14_ALT);
4126 is_operand_relocation (int r_type)
4136 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4138 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4147 #define MIN_INSN_LENGTH 2
4149 /* Return 0 if it fails to decode. */
4152 insn_decode_len (bfd_byte *contents,
4153 bfd_size_type content_len,
4154 bfd_size_type offset)
4157 xtensa_isa isa = xtensa_default_isa;
4159 static xtensa_insnbuf ibuff = NULL;
4161 if (offset + MIN_INSN_LENGTH > content_len)
4165 ibuff = xtensa_insnbuf_alloc (isa);
4166 xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
4167 content_len - offset);
4168 fmt = xtensa_format_decode (isa, ibuff);
4169 if (fmt == XTENSA_UNDEFINED)
4171 insn_len = xtensa_format_length (isa, fmt);
4172 if (insn_len == XTENSA_UNDEFINED)
4178 /* Decode the opcode for a single slot instruction.
4179 Return 0 if it fails to decode or the instruction is multi-slot. */
4182 insn_decode_opcode (bfd_byte *contents,
4183 bfd_size_type content_len,
4184 bfd_size_type offset,
4187 xtensa_isa isa = xtensa_default_isa;
4189 static xtensa_insnbuf insnbuf = NULL;
4190 static xtensa_insnbuf slotbuf = NULL;
4192 if (offset + MIN_INSN_LENGTH > content_len)
4193 return XTENSA_UNDEFINED;
4195 if (insnbuf == NULL)
4197 insnbuf = xtensa_insnbuf_alloc (isa);
4198 slotbuf = xtensa_insnbuf_alloc (isa);
4201 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4202 content_len - offset);
4203 fmt = xtensa_format_decode (isa, insnbuf);
4204 if (fmt == XTENSA_UNDEFINED)
4205 return XTENSA_UNDEFINED;
4207 if (slot >= xtensa_format_num_slots (isa, fmt))
4208 return XTENSA_UNDEFINED;
4210 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4211 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4215 /* The offset is the offset in the contents.
4216 The address is the address of that offset. */
4219 check_branch_target_aligned (bfd_byte *contents,
4220 bfd_size_type content_length,
4224 bfd_size_type insn_len = insn_decode_len (contents, content_length, offset);
4227 return check_branch_target_aligned_address (address, insn_len);
4232 check_loop_aligned (bfd_byte *contents,
4233 bfd_size_type content_length,
4237 bfd_size_type loop_len, insn_len;
4238 xtensa_opcode opcode;
4240 opcode = insn_decode_opcode (contents, content_length, offset, 0);
4241 if (opcode == XTENSA_UNDEFINED
4242 || xtensa_opcode_is_loop (xtensa_default_isa, opcode) != 1)
4248 loop_len = insn_decode_len (contents, content_length, offset);
4249 insn_len = insn_decode_len (contents, content_length, offset + loop_len);
4250 if (loop_len == 0 || insn_len == 0)
4256 return check_branch_target_aligned_address (address + loop_len, insn_len);
4261 check_branch_target_aligned_address (bfd_vma addr, int len)
4264 return (addr % 8 == 0);
4265 return ((addr >> 2) == ((addr + len - 1) >> 2));
4269 /* Instruction widening and narrowing. */
4271 /* When FLIX is available we need to access certain instructions only
4272 when they are 16-bit or 24-bit instructions. This table caches
4273 information about such instructions by walking through all the
4274 opcodes and finding the smallest single-slot format into which each
4277 static xtensa_format *op_single_fmt_table = NULL;
4281 init_op_single_format_table (void)
4283 xtensa_isa isa = xtensa_default_isa;
4284 xtensa_insnbuf ibuf;
4285 xtensa_opcode opcode;
4289 if (op_single_fmt_table)
4292 ibuf = xtensa_insnbuf_alloc (isa);
4293 num_opcodes = xtensa_isa_num_opcodes (isa);
4295 op_single_fmt_table = (xtensa_format *)
4296 bfd_malloc (sizeof (xtensa_format) * num_opcodes);
4297 for (opcode = 0; opcode < num_opcodes; opcode++)
4299 op_single_fmt_table[opcode] = XTENSA_UNDEFINED;
4300 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
4302 if (xtensa_format_num_slots (isa, fmt) == 1
4303 && xtensa_opcode_encode (isa, fmt, 0, ibuf, opcode) == 0)
4305 xtensa_opcode old_fmt = op_single_fmt_table[opcode];
4306 int fmt_length = xtensa_format_length (isa, fmt);
4307 if (old_fmt == XTENSA_UNDEFINED
4308 || fmt_length < xtensa_format_length (isa, old_fmt))
4309 op_single_fmt_table[opcode] = fmt;
4313 xtensa_insnbuf_free (isa, ibuf);
4317 static xtensa_format
4318 get_single_format (xtensa_opcode opcode)
4320 init_op_single_format_table ();
4321 return op_single_fmt_table[opcode];
4325 /* For the set of narrowable instructions we do NOT include the
4326 narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
4327 involved during linker relaxation that may require these to
4328 re-expand in some conditions. Also, the narrowing "or" -> mov.n
4329 requires special case code to ensure it only works when op1 == op2. */
4337 struct string_pair narrowable[] =
4340 { "addi", "addi.n" },
4341 { "addmi", "addi.n" },
4342 { "l32i", "l32i.n" },
4343 { "movi", "movi.n" },
4345 { "retw", "retw.n" },
4346 { "s32i", "s32i.n" },
4347 { "or", "mov.n" } /* special case only when op1 == op2 */
4350 struct string_pair widenable[] =
4353 { "addi", "addi.n" },
4354 { "addmi", "addi.n" },
4355 { "beqz", "beqz.n" },
4356 { "bnez", "bnez.n" },
4357 { "l32i", "l32i.n" },
4358 { "movi", "movi.n" },
4360 { "retw", "retw.n" },
4361 { "s32i", "s32i.n" },
4362 { "or", "mov.n" } /* special case only when op1 == op2 */
4366 /* Check if an instruction can be "narrowed", i.e., changed from a standard
4367 3-byte instruction to a 2-byte "density" instruction. If it is valid,
4368 return the instruction buffer holding the narrow instruction. Otherwise,
4369 return 0. The set of valid narrowing are specified by a string table
4370 but require some special case operand checks in some cases. */
4372 static xtensa_insnbuf
4373 can_narrow_instruction (xtensa_insnbuf slotbuf,
4375 xtensa_opcode opcode)
4377 xtensa_isa isa = xtensa_default_isa;
4378 xtensa_format o_fmt;
4381 static xtensa_insnbuf o_insnbuf = NULL;
4382 static xtensa_insnbuf o_slotbuf = NULL;
4384 if (o_insnbuf == NULL)
4386 o_insnbuf = xtensa_insnbuf_alloc (isa);
4387 o_slotbuf = xtensa_insnbuf_alloc (isa);
4390 for (opi = 0; opi < (sizeof (narrowable)/sizeof (struct string_pair)); opi++)
4392 bfd_boolean is_or = (strcmp ("or", narrowable[opi].wide) == 0);
4394 if (opcode == xtensa_opcode_lookup (isa, narrowable[opi].wide))
4396 uint32 value, newval;
4397 int i, operand_count, o_operand_count;
4398 xtensa_opcode o_opcode;
4400 /* Address does not matter in this case. We might need to
4401 fix it to handle branches/jumps. */
4402 bfd_vma self_address = 0;
4404 o_opcode = xtensa_opcode_lookup (isa, narrowable[opi].narrow);
4405 if (o_opcode == XTENSA_UNDEFINED)
4407 o_fmt = get_single_format (o_opcode);
4408 if (o_fmt == XTENSA_UNDEFINED)
4411 if (xtensa_format_length (isa, fmt) != 3
4412 || xtensa_format_length (isa, o_fmt) != 2)
4415 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4416 operand_count = xtensa_opcode_num_operands (isa, opcode);
4417 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4419 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4424 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4429 uint32 rawval0, rawval1, rawval2;
4431 if (o_operand_count + 1 != operand_count
4432 || xtensa_operand_get_field (isa, opcode, 0,
4433 fmt, 0, slotbuf, &rawval0) != 0
4434 || xtensa_operand_get_field (isa, opcode, 1,
4435 fmt, 0, slotbuf, &rawval1) != 0
4436 || xtensa_operand_get_field (isa, opcode, 2,
4437 fmt, 0, slotbuf, &rawval2) != 0
4438 || rawval1 != rawval2
4439 || rawval0 == rawval1 /* it is a nop */)
4443 for (i = 0; i < o_operand_count; ++i)
4445 if (xtensa_operand_get_field (isa, opcode, i, fmt, 0,
4447 || xtensa_operand_decode (isa, opcode, i, &value))
4450 /* PC-relative branches need adjustment, but
4451 the PC-rel operand will always have a relocation. */
4453 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4455 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4456 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4461 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4471 /* Attempt to narrow an instruction. If the narrowing is valid, perform
4472 the action in-place directly into the contents and return TRUE. Otherwise,
4473 the return value is FALSE and the contents are not modified. */
4476 narrow_instruction (bfd_byte *contents,
4477 bfd_size_type content_length,
4478 bfd_size_type offset)
4480 xtensa_opcode opcode;
4481 bfd_size_type insn_len;
4482 xtensa_isa isa = xtensa_default_isa;
4484 xtensa_insnbuf o_insnbuf;
4486 static xtensa_insnbuf insnbuf = NULL;
4487 static xtensa_insnbuf slotbuf = NULL;
4489 if (insnbuf == NULL)
4491 insnbuf = xtensa_insnbuf_alloc (isa);
4492 slotbuf = xtensa_insnbuf_alloc (isa);
4495 BFD_ASSERT (offset < content_length);
4497 if (content_length < 2)
4500 /* We will hand-code a few of these for a little while.
4501 These have all been specified in the assembler aleady. */
4502 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4503 content_length - offset);
4504 fmt = xtensa_format_decode (isa, insnbuf);
4505 if (xtensa_format_num_slots (isa, fmt) != 1)
4508 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4511 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4512 if (opcode == XTENSA_UNDEFINED)
4514 insn_len = xtensa_format_length (isa, fmt);
4515 if (insn_len > content_length)
4518 o_insnbuf = can_narrow_instruction (slotbuf, fmt, opcode);
4521 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4522 content_length - offset);
4530 /* Check if an instruction can be "widened", i.e., changed from a 2-byte
4531 "density" instruction to a standard 3-byte instruction. If it is valid,
4532 return the instruction buffer holding the wide instruction. Otherwise,
4533 return 0. The set of valid widenings are specified by a string table
4534 but require some special case operand checks in some cases. */
4536 static xtensa_insnbuf
4537 can_widen_instruction (xtensa_insnbuf slotbuf,
4539 xtensa_opcode opcode)
4541 xtensa_isa isa = xtensa_default_isa;
4542 xtensa_format o_fmt;
4545 static xtensa_insnbuf o_insnbuf = NULL;
4546 static xtensa_insnbuf o_slotbuf = NULL;
4548 if (o_insnbuf == NULL)
4550 o_insnbuf = xtensa_insnbuf_alloc (isa);
4551 o_slotbuf = xtensa_insnbuf_alloc (isa);
4554 for (opi = 0; opi < (sizeof (widenable)/sizeof (struct string_pair)); opi++)
4556 bfd_boolean is_or = (strcmp ("or", widenable[opi].wide) == 0);
4557 bfd_boolean is_branch = (strcmp ("beqz", widenable[opi].wide) == 0
4558 || strcmp ("bnez", widenable[opi].wide) == 0);
4560 if (opcode == xtensa_opcode_lookup (isa, widenable[opi].narrow))
4562 uint32 value, newval;
4563 int i, operand_count, o_operand_count, check_operand_count;
4564 xtensa_opcode o_opcode;
4566 /* Address does not matter in this case. We might need to fix it
4567 to handle branches/jumps. */
4568 bfd_vma self_address = 0;
4570 o_opcode = xtensa_opcode_lookup (isa, widenable[opi].wide);
4571 if (o_opcode == XTENSA_UNDEFINED)
4573 o_fmt = get_single_format (o_opcode);
4574 if (o_fmt == XTENSA_UNDEFINED)
4577 if (xtensa_format_length (isa, fmt) != 2
4578 || xtensa_format_length (isa, o_fmt) != 3)
4581 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4582 operand_count = xtensa_opcode_num_operands (isa, opcode);
4583 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4584 check_operand_count = o_operand_count;
4586 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4591 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4596 uint32 rawval0, rawval1;
4598 if (o_operand_count != operand_count + 1
4599 || xtensa_operand_get_field (isa, opcode, 0,
4600 fmt, 0, slotbuf, &rawval0) != 0
4601 || xtensa_operand_get_field (isa, opcode, 1,
4602 fmt, 0, slotbuf, &rawval1) != 0
4603 || rawval0 == rawval1 /* it is a nop */)
4607 check_operand_count--;
4609 for (i = 0; i < check_operand_count; i++)
4612 if (is_or && i == o_operand_count - 1)
4614 if (xtensa_operand_get_field (isa, opcode, new_i, fmt, 0,
4616 || xtensa_operand_decode (isa, opcode, new_i, &value))
4619 /* PC-relative branches need adjustment, but
4620 the PC-rel operand will always have a relocation. */
4622 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4624 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4625 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4630 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4640 /* Attempt to widen an instruction. If the widening is valid, perform
4641 the action in-place directly into the contents and return TRUE. Otherwise,
4642 the return value is FALSE and the contents are not modified. */
4645 widen_instruction (bfd_byte *contents,
4646 bfd_size_type content_length,
4647 bfd_size_type offset)
4649 xtensa_opcode opcode;
4650 bfd_size_type insn_len;
4651 xtensa_isa isa = xtensa_default_isa;
4653 xtensa_insnbuf o_insnbuf;
4655 static xtensa_insnbuf insnbuf = NULL;
4656 static xtensa_insnbuf slotbuf = NULL;
4658 if (insnbuf == NULL)
4660 insnbuf = xtensa_insnbuf_alloc (isa);
4661 slotbuf = xtensa_insnbuf_alloc (isa);
4664 BFD_ASSERT (offset < content_length);
4666 if (content_length < 2)
4669 /* We will hand-code a few of these for a little while.
4670 These have all been specified in the assembler aleady. */
4671 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4672 content_length - offset);
4673 fmt = xtensa_format_decode (isa, insnbuf);
4674 if (xtensa_format_num_slots (isa, fmt) != 1)
4677 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4680 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4681 if (opcode == XTENSA_UNDEFINED)
4683 insn_len = xtensa_format_length (isa, fmt);
4684 if (insn_len > content_length)
4687 o_insnbuf = can_widen_instruction (slotbuf, fmt, opcode);
4690 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4691 content_length - offset);
4698 /* Code for transforming CALLs at link-time. */
4700 static bfd_reloc_status_type
4701 elf_xtensa_do_asm_simplify (bfd_byte *contents,
4703 bfd_vma content_length,
4704 char **error_message)
4706 static xtensa_insnbuf insnbuf = NULL;
4707 static xtensa_insnbuf slotbuf = NULL;
4708 xtensa_format core_format = XTENSA_UNDEFINED;
4709 xtensa_opcode opcode;
4710 xtensa_opcode direct_call_opcode;
4711 xtensa_isa isa = xtensa_default_isa;
4712 bfd_byte *chbuf = contents + address;
4715 if (insnbuf == NULL)
4717 insnbuf = xtensa_insnbuf_alloc (isa);
4718 slotbuf = xtensa_insnbuf_alloc (isa);
4721 if (content_length < address)
4723 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4724 return bfd_reloc_other;
4727 opcode = get_expanded_call_opcode (chbuf, content_length - address, 0);
4728 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4729 if (direct_call_opcode == XTENSA_UNDEFINED)
4731 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4732 return bfd_reloc_other;
4735 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
4736 core_format = xtensa_format_lookup (isa, "x24");
4737 opcode = xtensa_opcode_lookup (isa, "or");
4738 xtensa_opcode_encode (isa, core_format, 0, slotbuf, opcode);
4739 for (opn = 0; opn < 3; opn++)
4742 xtensa_operand_encode (isa, opcode, opn, ®no);
4743 xtensa_operand_set_field (isa, opcode, opn, core_format, 0,
4746 xtensa_format_encode (isa, core_format, insnbuf);
4747 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4748 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf, content_length - address);
4750 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
4751 xtensa_opcode_encode (isa, core_format, 0, slotbuf, direct_call_opcode);
4752 xtensa_operand_set_field (isa, opcode, 0, core_format, 0, slotbuf, 0);
4754 xtensa_format_encode (isa, core_format, insnbuf);
4755 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4756 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3,
4757 content_length - address - 3);
4759 return bfd_reloc_ok;
4763 static bfd_reloc_status_type
4764 contract_asm_expansion (bfd_byte *contents,
4765 bfd_vma content_length,
4766 Elf_Internal_Rela *irel,
4767 char **error_message)
4769 bfd_reloc_status_type retval =
4770 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length,
4773 if (retval != bfd_reloc_ok)
4774 return bfd_reloc_dangerous;
4776 /* Update the irel->r_offset field so that the right immediate and
4777 the right instruction are modified during the relocation. */
4778 irel->r_offset += 3;
4779 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_SLOT0_OP);
4780 return bfd_reloc_ok;
4784 static xtensa_opcode
4785 swap_callx_for_call_opcode (xtensa_opcode opcode)
4787 init_call_opcodes ();
4789 if (opcode == callx0_op) return call0_op;
4790 if (opcode == callx4_op) return call4_op;
4791 if (opcode == callx8_op) return call8_op;
4792 if (opcode == callx12_op) return call12_op;
4794 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
4795 return XTENSA_UNDEFINED;
4799 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
4800 CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
4801 If not, return XTENSA_UNDEFINED. */
4803 #define L32R_TARGET_REG_OPERAND 0
4804 #define CONST16_TARGET_REG_OPERAND 0
4805 #define CALLN_SOURCE_OPERAND 0
4807 static xtensa_opcode
4808 get_expanded_call_opcode (bfd_byte *buf, int bufsize, bfd_boolean *p_uses_l32r)
4810 static xtensa_insnbuf insnbuf = NULL;
4811 static xtensa_insnbuf slotbuf = NULL;
4813 xtensa_opcode opcode;
4814 xtensa_isa isa = xtensa_default_isa;
4815 uint32 regno, const16_regno, call_regno;
4818 if (insnbuf == NULL)
4820 insnbuf = xtensa_insnbuf_alloc (isa);
4821 slotbuf = xtensa_insnbuf_alloc (isa);
4824 xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize);
4825 fmt = xtensa_format_decode (isa, insnbuf);
4826 if (fmt == XTENSA_UNDEFINED
4827 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4828 return XTENSA_UNDEFINED;
4830 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4831 if (opcode == XTENSA_UNDEFINED)
4832 return XTENSA_UNDEFINED;
4834 if (opcode == get_l32r_opcode ())
4837 *p_uses_l32r = TRUE;
4838 if (xtensa_operand_get_field (isa, opcode, L32R_TARGET_REG_OPERAND,
4839 fmt, 0, slotbuf, ®no)
4840 || xtensa_operand_decode (isa, opcode, L32R_TARGET_REG_OPERAND,
4842 return XTENSA_UNDEFINED;
4844 else if (opcode == get_const16_opcode ())
4847 *p_uses_l32r = FALSE;
4848 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4849 fmt, 0, slotbuf, ®no)
4850 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4852 return XTENSA_UNDEFINED;
4854 /* Check that the next instruction is also CONST16. */
4855 offset += xtensa_format_length (isa, fmt);
4856 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4857 fmt = xtensa_format_decode (isa, insnbuf);
4858 if (fmt == XTENSA_UNDEFINED
4859 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4860 return XTENSA_UNDEFINED;
4861 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4862 if (opcode != get_const16_opcode ())
4863 return XTENSA_UNDEFINED;
4865 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4866 fmt, 0, slotbuf, &const16_regno)
4867 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4869 || const16_regno != regno)
4870 return XTENSA_UNDEFINED;
4873 return XTENSA_UNDEFINED;
4875 /* Next instruction should be an CALLXn with operand 0 == regno. */
4876 offset += xtensa_format_length (isa, fmt);
4877 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4878 fmt = xtensa_format_decode (isa, insnbuf);
4879 if (fmt == XTENSA_UNDEFINED
4880 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4881 return XTENSA_UNDEFINED;
4882 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4883 if (opcode == XTENSA_UNDEFINED
4884 || !is_indirect_call_opcode (opcode))
4885 return XTENSA_UNDEFINED;
4887 if (xtensa_operand_get_field (isa, opcode, CALLN_SOURCE_OPERAND,
4888 fmt, 0, slotbuf, &call_regno)
4889 || xtensa_operand_decode (isa, opcode, CALLN_SOURCE_OPERAND,
4891 return XTENSA_UNDEFINED;
4893 if (call_regno != regno)
4894 return XTENSA_UNDEFINED;
4900 /* Data structures used during relaxation. */
4902 /* r_reloc: relocation values. */
4904 /* Through the relaxation process, we need to keep track of the values
4905 that will result from evaluating relocations. The standard ELF
4906 relocation structure is not sufficient for this purpose because we're
4907 operating on multiple input files at once, so we need to know which
4908 input file a relocation refers to. The r_reloc structure thus
4909 records both the input file (bfd) and ELF relocation.
4911 For efficiency, an r_reloc also contains a "target_offset" field to
4912 cache the target-section-relative offset value that is represented by
4915 The r_reloc also contains a virtual offset that allows multiple
4916 inserted literals to be placed at the same "address" with
4917 different offsets. */
4919 typedef struct r_reloc_struct r_reloc;
4921 struct r_reloc_struct
4924 Elf_Internal_Rela rela;
4925 bfd_vma target_offset;
4926 bfd_vma virtual_offset;
4930 /* The r_reloc structure is included by value in literal_value, but not
4931 every literal_value has an associated relocation -- some are simple
4932 constants. In such cases, we set all the fields in the r_reloc
4933 struct to zero. The r_reloc_is_const function should be used to
4934 detect this case. */
4937 r_reloc_is_const (const r_reloc *r_rel)
4939 return (r_rel->abfd == NULL);
4944 r_reloc_get_target_offset (const r_reloc *r_rel)
4946 bfd_vma target_offset;
4947 unsigned long r_symndx;
4949 BFD_ASSERT (!r_reloc_is_const (r_rel));
4950 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4951 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
4952 return (target_offset + r_rel->rela.r_addend);
4956 static struct elf_link_hash_entry *
4957 r_reloc_get_hash_entry (const r_reloc *r_rel)
4959 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4960 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
4965 r_reloc_get_section (const r_reloc *r_rel)
4967 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4968 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
4973 r_reloc_is_defined (const r_reloc *r_rel)
4979 sec = r_reloc_get_section (r_rel);
4980 if (sec == bfd_abs_section_ptr
4981 || sec == bfd_com_section_ptr
4982 || sec == bfd_und_section_ptr)
4989 r_reloc_init (r_reloc *r_rel,
4991 Elf_Internal_Rela *irel,
4993 bfd_size_type content_length)
4996 reloc_howto_type *howto;
5000 r_rel->rela = *irel;
5002 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
5003 r_rel->virtual_offset = 0;
5004 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
5005 howto = &elf_howto_table[r_type];
5006 if (howto->partial_inplace)
5008 bfd_vma inplace_val;
5009 BFD_ASSERT (r_rel->rela.r_offset < content_length);
5011 inplace_val = bfd_get_32 (abfd, &contents[r_rel->rela.r_offset]);
5012 r_rel->target_offset += inplace_val;
5016 memset (r_rel, 0, sizeof (r_reloc));
5023 print_r_reloc (FILE *fp, const r_reloc *r_rel)
5025 if (r_reloc_is_defined (r_rel))
5027 asection *sec = r_reloc_get_section (r_rel);
5028 fprintf (fp, " %s(%s + ", sec->owner->filename, sec->name);
5030 else if (r_reloc_get_hash_entry (r_rel))
5031 fprintf (fp, " %s + ", r_reloc_get_hash_entry (r_rel)->root.root.string);
5033 fprintf (fp, " ?? + ");
5035 fprintf_vma (fp, r_rel->target_offset);
5036 if (r_rel->virtual_offset)
5038 fprintf (fp, " + ");
5039 fprintf_vma (fp, r_rel->virtual_offset);
5048 /* source_reloc: relocations that reference literals. */
5050 /* To determine whether literals can be coalesced, we need to first
5051 record all the relocations that reference the literals. The
5052 source_reloc structure below is used for this purpose. The
5053 source_reloc entries are kept in a per-literal-section array, sorted
5054 by offset within the literal section (i.e., target offset).
5056 The source_sec and r_rel.rela.r_offset fields identify the source of
5057 the relocation. The r_rel field records the relocation value, i.e.,
5058 the offset of the literal being referenced. The opnd field is needed
5059 to determine the range of the immediate field to which the relocation
5060 applies, so we can determine whether another literal with the same
5061 value is within range. The is_null field is true when the relocation
5062 is being removed (e.g., when an L32R is being removed due to a CALLX
5063 that is converted to a direct CALL). */
5065 typedef struct source_reloc_struct source_reloc;
5067 struct source_reloc_struct
5069 asection *source_sec;
5071 xtensa_opcode opcode;
5073 bfd_boolean is_null;
5074 bfd_boolean is_abs_literal;
5079 init_source_reloc (source_reloc *reloc,
5080 asection *source_sec,
5081 const r_reloc *r_rel,
5082 xtensa_opcode opcode,
5084 bfd_boolean is_abs_literal)
5086 reloc->source_sec = source_sec;
5087 reloc->r_rel = *r_rel;
5088 reloc->opcode = opcode;
5090 reloc->is_null = FALSE;
5091 reloc->is_abs_literal = is_abs_literal;
5095 /* Find the source_reloc for a particular source offset and relocation
5096 type. Note that the array is sorted by _target_ offset, so this is
5097 just a linear search. */
5099 static source_reloc *
5100 find_source_reloc (source_reloc *src_relocs,
5103 Elf_Internal_Rela *irel)
5107 for (i = 0; i < src_count; i++)
5109 if (src_relocs[i].source_sec == sec
5110 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
5111 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
5112 == ELF32_R_TYPE (irel->r_info)))
5113 return &src_relocs[i];
5121 source_reloc_compare (const void *ap, const void *bp)
5123 const source_reloc *a = (const source_reloc *) ap;
5124 const source_reloc *b = (const source_reloc *) bp;
5126 if (a->r_rel.target_offset != b->r_rel.target_offset)
5127 return (a->r_rel.target_offset - b->r_rel.target_offset);
5129 /* We don't need to sort on these criteria for correctness,
5130 but enforcing a more strict ordering prevents unstable qsort
5131 from behaving differently with different implementations.
5132 Without the code below we get correct but different results
5133 on Solaris 2.7 and 2.8. We would like to always produce the
5134 same results no matter the host. */
5136 if ((!a->is_null) - (!b->is_null))
5137 return ((!a->is_null) - (!b->is_null));
5138 return internal_reloc_compare (&a->r_rel.rela, &b->r_rel.rela);
5142 /* Literal values and value hash tables. */
5144 /* Literals with the same value can be coalesced. The literal_value
5145 structure records the value of a literal: the "r_rel" field holds the
5146 information from the relocation on the literal (if there is one) and
5147 the "value" field holds the contents of the literal word itself.
5149 The value_map structure records a literal value along with the
5150 location of a literal holding that value. The value_map hash table
5151 is indexed by the literal value, so that we can quickly check if a
5152 particular literal value has been seen before and is thus a candidate
5155 typedef struct literal_value_struct literal_value;
5156 typedef struct value_map_struct value_map;
5157 typedef struct value_map_hash_table_struct value_map_hash_table;
5159 struct literal_value_struct
5162 unsigned long value;
5163 bfd_boolean is_abs_literal;
5166 struct value_map_struct
5168 literal_value val; /* The literal value. */
5169 r_reloc loc; /* Location of the literal. */
5173 struct value_map_hash_table_struct
5175 unsigned bucket_count;
5176 value_map **buckets;
5178 bfd_boolean has_last_loc;
5184 init_literal_value (literal_value *lit,
5185 const r_reloc *r_rel,
5186 unsigned long value,
5187 bfd_boolean is_abs_literal)
5189 lit->r_rel = *r_rel;
5191 lit->is_abs_literal = is_abs_literal;
5196 literal_value_equal (const literal_value *src1,
5197 const literal_value *src2,
5198 bfd_boolean final_static_link)
5200 struct elf_link_hash_entry *h1, *h2;
5202 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
5205 if (r_reloc_is_const (&src1->r_rel))
5206 return (src1->value == src2->value);
5208 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
5209 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
5212 if (src1->r_rel.target_offset != src2->r_rel.target_offset)
5215 if (src1->r_rel.virtual_offset != src2->r_rel.virtual_offset)
5218 if (src1->value != src2->value)
5221 /* Now check for the same section (if defined) or the same elf_hash
5222 (if undefined or weak). */
5223 h1 = r_reloc_get_hash_entry (&src1->r_rel);
5224 h2 = r_reloc_get_hash_entry (&src2->r_rel);
5225 if (r_reloc_is_defined (&src1->r_rel)
5226 && (final_static_link
5227 || ((!h1 || h1->root.type != bfd_link_hash_defweak)
5228 && (!h2 || h2->root.type != bfd_link_hash_defweak))))
5230 if (r_reloc_get_section (&src1->r_rel)
5231 != r_reloc_get_section (&src2->r_rel))
5236 /* Require that the hash entries (i.e., symbols) be identical. */
5237 if (h1 != h2 || h1 == 0)
5241 if (src1->is_abs_literal != src2->is_abs_literal)
5248 /* Must be power of 2. */
5249 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
5251 static value_map_hash_table *
5252 value_map_hash_table_init (void)
5254 value_map_hash_table *values;
5256 values = (value_map_hash_table *)
5257 bfd_zmalloc (sizeof (value_map_hash_table));
5258 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
5260 values->buckets = (value_map **)
5261 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
5262 if (values->buckets == NULL)
5267 values->has_last_loc = FALSE;
5274 value_map_hash_table_delete (value_map_hash_table *table)
5276 free (table->buckets);
5282 hash_bfd_vma (bfd_vma val)
5284 return (val >> 2) + (val >> 10);
5289 literal_value_hash (const literal_value *src)
5293 hash_val = hash_bfd_vma (src->value);
5294 if (!r_reloc_is_const (&src->r_rel))
5298 hash_val += hash_bfd_vma (src->is_abs_literal * 1000);
5299 hash_val += hash_bfd_vma (src->r_rel.target_offset);
5300 hash_val += hash_bfd_vma (src->r_rel.virtual_offset);
5302 /* Now check for the same section and the same elf_hash. */
5303 if (r_reloc_is_defined (&src->r_rel))
5304 sec_or_hash = r_reloc_get_section (&src->r_rel);
5306 sec_or_hash = r_reloc_get_hash_entry (&src->r_rel);
5307 hash_val += hash_bfd_vma ((bfd_vma) (size_t) sec_or_hash);
5313 /* Check if the specified literal_value has been seen before. */
5316 value_map_get_cached_value (value_map_hash_table *map,
5317 const literal_value *val,
5318 bfd_boolean final_static_link)
5324 idx = literal_value_hash (val);
5325 idx = idx & (map->bucket_count - 1);
5326 bucket = map->buckets[idx];
5327 for (map_e = bucket; map_e; map_e = map_e->next)
5329 if (literal_value_equal (&map_e->val, val, final_static_link))
5336 /* Record a new literal value. It is illegal to call this if VALUE
5337 already has an entry here. */
5340 add_value_map (value_map_hash_table *map,
5341 const literal_value *val,
5343 bfd_boolean final_static_link)
5345 value_map **bucket_p;
5348 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
5351 bfd_set_error (bfd_error_no_memory);
5355 BFD_ASSERT (!value_map_get_cached_value (map, val, final_static_link));
5359 idx = literal_value_hash (val);
5360 idx = idx & (map->bucket_count - 1);
5361 bucket_p = &map->buckets[idx];
5363 val_e->next = *bucket_p;
5366 /* FIXME: Consider resizing the hash table if we get too many entries. */
5372 /* Lists of text actions (ta_) for narrowing, widening, longcall
5373 conversion, space fill, code & literal removal, etc. */
5375 /* The following text actions are generated:
5377 "ta_remove_insn" remove an instruction or instructions
5378 "ta_remove_longcall" convert longcall to call
5379 "ta_convert_longcall" convert longcall to nop/call
5380 "ta_narrow_insn" narrow a wide instruction
5381 "ta_widen" widen a narrow instruction
5382 "ta_fill" add fill or remove fill
5383 removed < 0 is a fill; branches to the fill address will be
5384 changed to address + fill size (e.g., address - removed)
5385 removed >= 0 branches to the fill address will stay unchanged
5386 "ta_remove_literal" remove a literal; this action is
5387 indicated when a literal is removed
5389 "ta_add_literal" insert a new literal; this action is
5390 indicated when a literal has been moved.
5391 It may use a virtual_offset because
5392 multiple literals can be placed at the
5395 For each of these text actions, we also record the number of bytes
5396 removed by performing the text action. In the case of a "ta_widen"
5397 or a "ta_fill" that adds space, the removed_bytes will be negative. */
5399 typedef struct text_action_struct text_action;
5400 typedef struct text_action_list_struct text_action_list;
5401 typedef enum text_action_enum_t text_action_t;
5403 enum text_action_enum_t
5406 ta_remove_insn, /* removed = -size */
5407 ta_remove_longcall, /* removed = -size */
5408 ta_convert_longcall, /* removed = 0 */
5409 ta_narrow_insn, /* removed = -1 */
5410 ta_widen_insn, /* removed = +1 */
5411 ta_fill, /* removed = +size */
5417 /* Structure for a text action record. */
5418 struct text_action_struct
5420 text_action_t action;
5421 asection *sec; /* Optional */
5423 bfd_vma virtual_offset; /* Zero except for adding literals. */
5425 literal_value value; /* Only valid when adding literals. */
5428 struct removal_by_action_entry_struct
5433 int eq_removed_before_fill;
5435 typedef struct removal_by_action_entry_struct removal_by_action_entry;
5437 struct removal_by_action_map_struct
5440 removal_by_action_entry *entry;
5442 typedef struct removal_by_action_map_struct removal_by_action_map;
5445 /* List of all of the actions taken on a text section. */
5446 struct text_action_list_struct
5450 removal_by_action_map map;
5454 static text_action *
5455 find_fill_action (text_action_list *l, asection *sec, bfd_vma offset)
5459 /* It is not necessary to fill at the end of a section. */
5460 if (sec->size == offset)
5466 splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5468 return (text_action *)node->value;
5474 compute_removed_action_diff (const text_action *ta,
5478 int removable_space)
5481 int current_removed = 0;
5484 current_removed = ta->removed_bytes;
5486 BFD_ASSERT (ta == NULL || ta->offset == offset);
5487 BFD_ASSERT (ta == NULL || ta->action == ta_fill);
5489 /* It is not necessary to fill at the end of a section. Clean this up. */
5490 if (sec->size == offset)
5491 new_removed = removable_space - 0;
5495 int added = -removed - current_removed;
5496 /* Ignore multiples of the section alignment. */
5497 added = ((1 << sec->alignment_power) - 1) & added;
5498 new_removed = (-added);
5500 /* Modify for removable. */
5501 space = removable_space - new_removed;
5502 new_removed = (removable_space
5503 - (((1 << sec->alignment_power) - 1) & space));
5505 return (new_removed - current_removed);
5510 adjust_fill_action (text_action *ta, int fill_diff)
5512 ta->removed_bytes += fill_diff;
5517 text_action_compare (splay_tree_key a, splay_tree_key b)
5519 text_action *pa = (text_action *)a;
5520 text_action *pb = (text_action *)b;
5521 static const int action_priority[] =
5525 [ta_convert_longcall] = 2,
5526 [ta_narrow_insn] = 3,
5527 [ta_remove_insn] = 4,
5528 [ta_remove_longcall] = 5,
5529 [ta_remove_literal] = 6,
5530 [ta_widen_insn] = 7,
5531 [ta_add_literal] = 8,
5534 if (pa->offset == pb->offset)
5536 if (pa->action == pb->action)
5538 return action_priority[pa->action] - action_priority[pb->action];
5541 return pa->offset < pb->offset ? -1 : 1;
5544 static text_action *
5545 action_first (text_action_list *action_list)
5547 splay_tree_node node = splay_tree_min (action_list->tree);
5548 return node ? (text_action *)node->value : NULL;
5551 static text_action *
5552 action_next (text_action_list *action_list, text_action *action)
5554 splay_tree_node node = splay_tree_successor (action_list->tree,
5555 (splay_tree_key)action);
5556 return node ? (text_action *)node->value : NULL;
5559 /* Add a modification action to the text. For the case of adding or
5560 removing space, modify any current fill and assume that
5561 "unreachable_space" bytes can be freely contracted. Note that a
5562 negative removed value is a fill. */
5565 text_action_add (text_action_list *l,
5566 text_action_t action,
5574 /* It is not necessary to fill at the end of a section. */
5575 if (action == ta_fill && sec->size == offset)
5578 /* It is not necessary to fill 0 bytes. */
5579 if (action == ta_fill && removed == 0)
5585 if (action == ta_fill)
5587 splay_tree_node node = splay_tree_lookup (l->tree, (splay_tree_key)&a);
5591 ta = (text_action *)node->value;
5592 ta->removed_bytes += removed;
5597 BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)&a) == NULL);
5599 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5600 ta->action = action;
5602 ta->offset = offset;
5603 ta->removed_bytes = removed;
5604 splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5610 text_action_add_literal (text_action_list *l,
5611 text_action_t action,
5613 const literal_value *value,
5617 asection *sec = r_reloc_get_section (loc);
5618 bfd_vma offset = loc->target_offset;
5619 bfd_vma virtual_offset = loc->virtual_offset;
5621 BFD_ASSERT (action == ta_add_literal);
5623 /* Create a new record and fill it up. */
5624 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5625 ta->action = action;
5627 ta->offset = offset;
5628 ta->virtual_offset = virtual_offset;
5630 ta->removed_bytes = removed;
5632 BFD_ASSERT (splay_tree_lookup (l->tree, (splay_tree_key)ta) == NULL);
5633 splay_tree_insert (l->tree, (splay_tree_key)ta, (splay_tree_value)ta);
5638 /* Find the total offset adjustment for the relaxations specified by
5639 text_actions, beginning from a particular starting action. This is
5640 typically used from offset_with_removed_text to search an entire list of
5641 actions, but it may also be called directly when adjusting adjacent offsets
5642 so that each search may begin where the previous one left off. */
5645 removed_by_actions (text_action_list *action_list,
5646 text_action **p_start_action,
5648 bfd_boolean before_fill)
5653 r = *p_start_action;
5656 splay_tree_node node = splay_tree_lookup (action_list->tree,
5658 BFD_ASSERT (node != NULL && r == (text_action *)node->value);
5663 if (r->offset > offset)
5666 if (r->offset == offset
5667 && (before_fill || r->action != ta_fill || r->removed_bytes >= 0))
5670 removed += r->removed_bytes;
5672 r = action_next (action_list, r);
5675 *p_start_action = r;
5681 offset_with_removed_text (text_action_list *action_list, bfd_vma offset)
5683 text_action *r = action_first (action_list);
5685 return offset - removed_by_actions (action_list, &r, offset, FALSE);
5690 action_list_count (text_action_list *action_list)
5692 return action_list->count;
5695 typedef struct map_action_fn_context_struct map_action_fn_context;
5696 struct map_action_fn_context_struct
5699 removal_by_action_map map;
5700 bfd_boolean eq_complete;
5704 map_action_fn (splay_tree_node node, void *p)
5706 map_action_fn_context *ctx = p;
5707 text_action *r = (text_action *)node->value;
5708 removal_by_action_entry *ientry = ctx->map.entry + ctx->map.n_entries;
5710 if (ctx->map.n_entries && (ientry - 1)->offset == r->offset)
5716 ++ctx->map.n_entries;
5717 ctx->eq_complete = FALSE;
5718 ientry->offset = r->offset;
5719 ientry->eq_removed_before_fill = ctx->removed;
5722 if (!ctx->eq_complete)
5724 if (r->action != ta_fill || r->removed_bytes >= 0)
5726 ientry->eq_removed = ctx->removed;
5727 ctx->eq_complete = TRUE;
5730 ientry->eq_removed = ctx->removed + r->removed_bytes;
5733 ctx->removed += r->removed_bytes;
5734 ientry->removed = ctx->removed;
5739 map_removal_by_action (text_action_list *action_list)
5741 map_action_fn_context ctx;
5744 ctx.map.n_entries = 0;
5745 ctx.map.entry = bfd_malloc (action_list_count (action_list) *
5746 sizeof (removal_by_action_entry));
5747 ctx.eq_complete = FALSE;
5749 splay_tree_foreach (action_list->tree, map_action_fn, &ctx);
5750 action_list->map = ctx.map;
5754 removed_by_actions_map (text_action_list *action_list, bfd_vma offset,
5755 bfd_boolean before_fill)
5759 if (!action_list->map.entry)
5760 map_removal_by_action (action_list);
5762 if (!action_list->map.n_entries)
5766 b = action_list->map.n_entries;
5770 unsigned c = (a + b) / 2;
5772 if (action_list->map.entry[c].offset <= offset)
5778 if (action_list->map.entry[a].offset < offset)
5780 return action_list->map.entry[a].removed;
5782 else if (action_list->map.entry[a].offset == offset)
5784 return before_fill ?
5785 action_list->map.entry[a].eq_removed_before_fill :
5786 action_list->map.entry[a].eq_removed;
5795 offset_with_removed_text_map (text_action_list *action_list, bfd_vma offset)
5797 int removed = removed_by_actions_map (action_list, offset, FALSE);
5798 return offset - removed;
5802 /* The find_insn_action routine will only find non-fill actions. */
5804 static text_action *
5805 find_insn_action (text_action_list *action_list, bfd_vma offset)
5807 static const text_action_t action[] =
5809 ta_convert_longcall,
5819 for (i = 0; i < sizeof (action) / sizeof (*action); ++i)
5821 splay_tree_node node;
5823 a.action = action[i];
5824 node = splay_tree_lookup (action_list->tree, (splay_tree_key)&a);
5826 return (text_action *)node->value;
5835 print_action (FILE *fp, text_action *r)
5837 const char *t = "unknown";
5840 case ta_remove_insn:
5841 t = "remove_insn"; break;
5842 case ta_remove_longcall:
5843 t = "remove_longcall"; break;
5844 case ta_convert_longcall:
5845 t = "convert_longcall"; break;
5846 case ta_narrow_insn:
5847 t = "narrow_insn"; break;
5849 t = "widen_insn"; break;
5854 case ta_remove_literal:
5855 t = "remove_literal"; break;
5856 case ta_add_literal:
5857 t = "add_literal"; break;
5860 fprintf (fp, "%s: %s[0x%lx] \"%s\" %d\n",
5861 r->sec->owner->filename,
5862 r->sec->name, (unsigned long) r->offset, t, r->removed_bytes);
5866 print_action_list_fn (splay_tree_node node, void *p)
5868 text_action *r = (text_action *)node->value;
5870 print_action (p, r);
5875 print_action_list (FILE *fp, text_action_list *action_list)
5877 fprintf (fp, "Text Action\n");
5878 splay_tree_foreach (action_list->tree, print_action_list_fn, fp);
5884 /* Lists of literals being coalesced or removed. */
5886 /* In the usual case, the literal identified by "from" is being
5887 coalesced with another literal identified by "to". If the literal is
5888 unused and is being removed altogether, "to.abfd" will be NULL.
5889 The removed_literal entries are kept on a per-section list, sorted
5890 by the "from" offset field. */
5892 typedef struct removed_literal_struct removed_literal;
5893 typedef struct removed_literal_map_entry_struct removed_literal_map_entry;
5894 typedef struct removed_literal_list_struct removed_literal_list;
5896 struct removed_literal_struct
5900 removed_literal *next;
5903 struct removed_literal_map_entry_struct
5906 removed_literal *literal;
5909 struct removed_literal_list_struct
5911 removed_literal *head;
5912 removed_literal *tail;
5915 removed_literal_map_entry *map;
5919 /* Record that the literal at "from" is being removed. If "to" is not
5920 NULL, the "from" literal is being coalesced with the "to" literal. */
5923 add_removed_literal (removed_literal_list *removed_list,
5924 const r_reloc *from,
5927 removed_literal *r, *new_r, *next_r;
5929 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
5931 new_r->from = *from;
5935 new_r->to.abfd = NULL;
5938 r = removed_list->head;
5941 removed_list->head = new_r;
5942 removed_list->tail = new_r;
5944 /* Special check for common case of append. */
5945 else if (removed_list->tail->from.target_offset < from->target_offset)
5947 removed_list->tail->next = new_r;
5948 removed_list->tail = new_r;
5952 while (r->from.target_offset < from->target_offset && r->next)
5958 new_r->next = next_r;
5960 removed_list->tail = new_r;
5965 map_removed_literal (removed_literal_list *removed_list)
5969 removed_literal_map_entry *map = NULL;
5970 removed_literal *r = removed_list->head;
5972 for (i = 0; r; ++i, r = r->next)
5976 n_map = (n_map * 2) + 2;
5977 map = bfd_realloc (map, n_map * sizeof (*map));
5979 map[i].addr = r->from.target_offset;
5982 removed_list->map = map;
5983 removed_list->n_map = i;
5987 removed_literal_compare (const void *a, const void *b)
5989 const removed_literal_map_entry *pa = a;
5990 const removed_literal_map_entry *pb = b;
5992 if (pa->addr == pb->addr)
5995 return pa->addr < pb->addr ? -1 : 1;
5998 /* Check if the list of removed literals contains an entry for the
5999 given address. Return the entry if found. */
6001 static removed_literal *
6002 find_removed_literal (removed_literal_list *removed_list, bfd_vma addr)
6004 removed_literal_map_entry *p;
6005 removed_literal *r = NULL;
6007 if (removed_list->map == NULL)
6008 map_removed_literal (removed_list);
6010 p = bsearch (&addr, removed_list->map, removed_list->n_map,
6011 sizeof (*removed_list->map), removed_literal_compare);
6014 while (p != removed_list->map && (p - 1)->addr == addr)
6025 print_removed_literals (FILE *fp, removed_literal_list *removed_list)
6028 r = removed_list->head;
6030 fprintf (fp, "Removed Literals\n");
6031 for (; r != NULL; r = r->next)
6033 print_r_reloc (fp, &r->from);
6034 fprintf (fp, " => ");
6035 if (r->to.abfd == NULL)
6036 fprintf (fp, "REMOVED");
6038 print_r_reloc (fp, &r->to);
6046 /* Per-section data for relaxation. */
6048 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
6050 struct xtensa_relax_info_struct
6052 bfd_boolean is_relaxable_literal_section;
6053 bfd_boolean is_relaxable_asm_section;
6054 int visited; /* Number of times visited. */
6056 source_reloc *src_relocs; /* Array[src_count]. */
6058 int src_next; /* Next src_relocs entry to assign. */
6060 removed_literal_list removed_list;
6061 text_action_list action_list;
6063 reloc_bfd_fix *fix_list;
6064 reloc_bfd_fix *fix_array;
6065 unsigned fix_array_count;
6067 /* Support for expanding the reloc array that is stored
6068 in the section structure. If the relocations have been
6069 reallocated, the newly allocated relocations will be referenced
6070 here along with the actual size allocated. The relocation
6071 count will always be found in the section structure. */
6072 Elf_Internal_Rela *allocated_relocs;
6073 unsigned relocs_count;
6074 unsigned allocated_relocs_count;
6077 struct elf_xtensa_section_data
6079 struct bfd_elf_section_data elf;
6080 xtensa_relax_info relax_info;
6085 elf_xtensa_new_section_hook (bfd *abfd, asection *sec)
6087 if (!sec->used_by_bfd)
6089 struct elf_xtensa_section_data *sdata;
6090 bfd_size_type amt = sizeof (*sdata);
6092 sdata = bfd_zalloc (abfd, amt);
6095 sec->used_by_bfd = sdata;
6098 return _bfd_elf_new_section_hook (abfd, sec);
6102 static xtensa_relax_info *
6103 get_xtensa_relax_info (asection *sec)
6105 struct elf_xtensa_section_data *section_data;
6107 /* No info available if no section or if it is an output section. */
6108 if (!sec || sec == sec->output_section)
6111 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
6112 return §ion_data->relax_info;
6117 init_xtensa_relax_info (asection *sec)
6119 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6121 relax_info->is_relaxable_literal_section = FALSE;
6122 relax_info->is_relaxable_asm_section = FALSE;
6123 relax_info->visited = 0;
6125 relax_info->src_relocs = NULL;
6126 relax_info->src_count = 0;
6127 relax_info->src_next = 0;
6129 relax_info->removed_list.head = NULL;
6130 relax_info->removed_list.tail = NULL;
6132 relax_info->action_list.tree = splay_tree_new (text_action_compare,
6134 relax_info->action_list.map.n_entries = 0;
6135 relax_info->action_list.map.entry = NULL;
6137 relax_info->fix_list = NULL;
6138 relax_info->fix_array = NULL;
6139 relax_info->fix_array_count = 0;
6141 relax_info->allocated_relocs = NULL;
6142 relax_info->relocs_count = 0;
6143 relax_info->allocated_relocs_count = 0;
6147 /* Coalescing literals may require a relocation to refer to a section in
6148 a different input file, but the standard relocation information
6149 cannot express that. Instead, the reloc_bfd_fix structures are used
6150 to "fix" the relocations that refer to sections in other input files.
6151 These structures are kept on per-section lists. The "src_type" field
6152 records the relocation type in case there are multiple relocations on
6153 the same location. FIXME: This is ugly; an alternative might be to
6154 add new symbols with the "owner" field to some other input file. */
6156 struct reloc_bfd_fix_struct
6160 unsigned src_type; /* Relocation type. */
6162 asection *target_sec;
6163 bfd_vma target_offset;
6164 bfd_boolean translated;
6166 reloc_bfd_fix *next;
6170 static reloc_bfd_fix *
6171 reloc_bfd_fix_init (asection *src_sec,
6174 asection *target_sec,
6175 bfd_vma target_offset,
6176 bfd_boolean translated)
6180 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
6181 fix->src_sec = src_sec;
6182 fix->src_offset = src_offset;
6183 fix->src_type = src_type;
6184 fix->target_sec = target_sec;
6185 fix->target_offset = target_offset;
6186 fix->translated = translated;
6193 add_fix (asection *src_sec, reloc_bfd_fix *fix)
6195 xtensa_relax_info *relax_info;
6197 relax_info = get_xtensa_relax_info (src_sec);
6198 fix->next = relax_info->fix_list;
6199 relax_info->fix_list = fix;
6204 fix_compare (const void *ap, const void *bp)
6206 const reloc_bfd_fix *a = (const reloc_bfd_fix *) ap;
6207 const reloc_bfd_fix *b = (const reloc_bfd_fix *) bp;
6209 if (a->src_offset != b->src_offset)
6210 return (a->src_offset - b->src_offset);
6211 return (a->src_type - b->src_type);
6216 cache_fix_array (asection *sec)
6218 unsigned i, count = 0;
6220 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6222 if (relax_info == NULL)
6224 if (relax_info->fix_list == NULL)
6227 for (r = relax_info->fix_list; r != NULL; r = r->next)
6230 relax_info->fix_array =
6231 (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix) * count);
6232 relax_info->fix_array_count = count;
6234 r = relax_info->fix_list;
6235 for (i = 0; i < count; i++, r = r->next)
6237 relax_info->fix_array[count - 1 - i] = *r;
6238 relax_info->fix_array[count - 1 - i].next = NULL;
6241 qsort (relax_info->fix_array, relax_info->fix_array_count,
6242 sizeof (reloc_bfd_fix), fix_compare);
6246 static reloc_bfd_fix *
6247 get_bfd_fix (asection *sec, bfd_vma offset, unsigned type)
6249 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6253 if (relax_info == NULL)
6255 if (relax_info->fix_list == NULL)
6258 if (relax_info->fix_array == NULL)
6259 cache_fix_array (sec);
6261 key.src_offset = offset;
6262 key.src_type = type;
6263 rv = bsearch (&key, relax_info->fix_array, relax_info->fix_array_count,
6264 sizeof (reloc_bfd_fix), fix_compare);
6269 /* Section caching. */
6271 typedef struct section_cache_struct section_cache_t;
6273 struct section_cache_struct
6277 bfd_byte *contents; /* Cache of the section contents. */
6278 bfd_size_type content_length;
6280 property_table_entry *ptbl; /* Cache of the section property table. */
6283 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6284 unsigned reloc_count;
6289 init_section_cache (section_cache_t *sec_cache)
6291 memset (sec_cache, 0, sizeof (*sec_cache));
6296 free_section_cache (section_cache_t *sec_cache)
6300 release_contents (sec_cache->sec, sec_cache->contents);
6301 release_internal_relocs (sec_cache->sec, sec_cache->relocs);
6302 if (sec_cache->ptbl)
6303 free (sec_cache->ptbl);
6309 section_cache_section (section_cache_t *sec_cache,
6311 struct bfd_link_info *link_info)
6314 property_table_entry *prop_table = NULL;
6316 bfd_byte *contents = NULL;
6317 Elf_Internal_Rela *internal_relocs = NULL;
6318 bfd_size_type sec_size;
6322 if (sec == sec_cache->sec)
6326 sec_size = bfd_get_section_limit (abfd, sec);
6328 /* Get the contents. */
6329 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6330 if (contents == NULL && sec_size != 0)
6333 /* Get the relocations. */
6334 internal_relocs = retrieve_internal_relocs (abfd, sec,
6335 link_info->keep_memory);
6337 /* Get the entry table. */
6338 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
6339 XTENSA_PROP_SEC_NAME, FALSE);
6343 /* Fill in the new section cache. */
6344 free_section_cache (sec_cache);
6345 init_section_cache (sec_cache);
6347 sec_cache->sec = sec;
6348 sec_cache->contents = contents;
6349 sec_cache->content_length = sec_size;
6350 sec_cache->relocs = internal_relocs;
6351 sec_cache->reloc_count = sec->reloc_count;
6352 sec_cache->pte_count = ptblsize;
6353 sec_cache->ptbl = prop_table;
6358 release_contents (sec, contents);
6359 release_internal_relocs (sec, internal_relocs);
6366 /* Extended basic blocks. */
6368 /* An ebb_struct represents an Extended Basic Block. Within this
6369 range, we guarantee that all instructions are decodable, the
6370 property table entries are contiguous, and no property table
6371 specifies a segment that cannot have instructions moved. This
6372 structure contains caches of the contents, property table and
6373 relocations for the specified section for easy use. The range is
6374 specified by ranges of indices for the byte offset, property table
6375 offsets and relocation offsets. These must be consistent. */
6377 typedef struct ebb_struct ebb_t;
6383 bfd_byte *contents; /* Cache of the section contents. */
6384 bfd_size_type content_length;
6386 property_table_entry *ptbl; /* Cache of the section property table. */
6389 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6390 unsigned reloc_count;
6392 bfd_vma start_offset; /* Offset in section. */
6393 unsigned start_ptbl_idx; /* Offset in the property table. */
6394 unsigned start_reloc_idx; /* Offset in the relocations. */
6397 unsigned end_ptbl_idx;
6398 unsigned end_reloc_idx;
6400 bfd_boolean ends_section; /* Is this the last ebb in a section? */
6402 /* The unreachable property table at the end of this set of blocks;
6403 NULL if the end is not an unreachable block. */
6404 property_table_entry *ends_unreachable;
6408 enum ebb_target_enum
6411 EBB_DESIRE_TGT_ALIGN,
6412 EBB_REQUIRE_TGT_ALIGN,
6413 EBB_REQUIRE_LOOP_ALIGN,
6418 /* proposed_action_struct is similar to the text_action_struct except
6419 that is represents a potential transformation, not one that will
6420 occur. We build a list of these for an extended basic block
6421 and use them to compute the actual actions desired. We must be
6422 careful that the entire set of actual actions we perform do not
6423 break any relocations that would fit if the actions were not
6426 typedef struct proposed_action_struct proposed_action;
6428 struct proposed_action_struct
6430 enum ebb_target_enum align_type; /* for the target alignment */
6431 bfd_vma alignment_pow;
6432 text_action_t action;
6435 bfd_boolean do_action; /* If false, then we will not perform the action. */
6439 /* The ebb_constraint_struct keeps a set of proposed actions for an
6440 extended basic block. */
6442 typedef struct ebb_constraint_struct ebb_constraint;
6444 struct ebb_constraint_struct
6447 bfd_boolean start_movable;
6449 /* Bytes of extra space at the beginning if movable. */
6450 int start_extra_space;
6452 enum ebb_target_enum start_align;
6454 bfd_boolean end_movable;
6456 /* Bytes of extra space at the end if movable. */
6457 int end_extra_space;
6459 unsigned action_count;
6460 unsigned action_allocated;
6462 /* Array of proposed actions. */
6463 proposed_action *actions;
6465 /* Action alignments -- one for each proposed action. */
6466 enum ebb_target_enum *action_aligns;
6471 init_ebb_constraint (ebb_constraint *c)
6473 memset (c, 0, sizeof (ebb_constraint));
6478 free_ebb_constraint (ebb_constraint *c)
6486 init_ebb (ebb_t *ebb,
6489 bfd_size_type content_length,
6490 property_table_entry *prop_table,
6492 Elf_Internal_Rela *internal_relocs,
6493 unsigned reloc_count)
6495 memset (ebb, 0, sizeof (ebb_t));
6497 ebb->contents = contents;
6498 ebb->content_length = content_length;
6499 ebb->ptbl = prop_table;
6500 ebb->pte_count = ptblsize;
6501 ebb->relocs = internal_relocs;
6502 ebb->reloc_count = reloc_count;
6503 ebb->start_offset = 0;
6504 ebb->end_offset = ebb->content_length - 1;
6505 ebb->start_ptbl_idx = 0;
6506 ebb->end_ptbl_idx = ptblsize;
6507 ebb->start_reloc_idx = 0;
6508 ebb->end_reloc_idx = reloc_count;
6512 /* Extend the ebb to all decodable contiguous sections. The algorithm
6513 for building a basic block around an instruction is to push it
6514 forward until we hit the end of a section, an unreachable block or
6515 a block that cannot be transformed. Then we push it backwards
6516 searching for similar conditions. */
6518 static bfd_boolean extend_ebb_bounds_forward (ebb_t *);
6519 static bfd_boolean extend_ebb_bounds_backward (ebb_t *);
6520 static bfd_size_type insn_block_decodable_len
6521 (bfd_byte *, bfd_size_type, bfd_vma, bfd_size_type);
6524 extend_ebb_bounds (ebb_t *ebb)
6526 if (!extend_ebb_bounds_forward (ebb))
6528 if (!extend_ebb_bounds_backward (ebb))
6535 extend_ebb_bounds_forward (ebb_t *ebb)
6537 property_table_entry *the_entry, *new_entry;
6539 the_entry = &ebb->ptbl[ebb->end_ptbl_idx];
6541 /* Stop when (1) we cannot decode an instruction, (2) we are at
6542 the end of the property tables, (3) we hit a non-contiguous property
6543 table entry, (4) we hit a NO_TRANSFORM region. */
6548 bfd_size_type insn_block_len;
6550 entry_end = the_entry->address - ebb->sec->vma + the_entry->size;
6552 insn_block_decodable_len (ebb->contents, ebb->content_length,
6554 entry_end - ebb->end_offset);
6555 if (insn_block_len != (entry_end - ebb->end_offset))
6558 /* xgettext:c-format */
6559 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6560 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6563 ebb->end_offset += insn_block_len;
6565 if (ebb->end_offset == ebb->sec->size)
6566 ebb->ends_section = TRUE;
6568 /* Update the reloc counter. */
6569 while (ebb->end_reloc_idx + 1 < ebb->reloc_count
6570 && (ebb->relocs[ebb->end_reloc_idx + 1].r_offset
6573 ebb->end_reloc_idx++;
6576 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6579 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6580 if (((new_entry->flags & XTENSA_PROP_INSN) == 0)
6581 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6582 || ((the_entry->flags & XTENSA_PROP_ALIGN) != 0))
6585 if (the_entry->address + the_entry->size != new_entry->address)
6588 the_entry = new_entry;
6589 ebb->end_ptbl_idx++;
6592 /* Quick check for an unreachable or end of file just at the end. */
6593 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6595 if (ebb->end_offset == ebb->content_length)
6596 ebb->ends_section = TRUE;
6600 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6601 if ((new_entry->flags & XTENSA_PROP_UNREACHABLE) != 0
6602 && the_entry->address + the_entry->size == new_entry->address)
6603 ebb->ends_unreachable = new_entry;
6606 /* Any other ending requires exact alignment. */
6612 extend_ebb_bounds_backward (ebb_t *ebb)
6614 property_table_entry *the_entry, *new_entry;
6616 the_entry = &ebb->ptbl[ebb->start_ptbl_idx];
6618 /* Stop when (1) we cannot decode the instructions in the current entry.
6619 (2) we are at the beginning of the property tables, (3) we hit a
6620 non-contiguous property table entry, (4) we hit a NO_TRANSFORM region. */
6624 bfd_vma block_begin;
6625 bfd_size_type insn_block_len;
6627 block_begin = the_entry->address - ebb->sec->vma;
6629 insn_block_decodable_len (ebb->contents, ebb->content_length,
6631 ebb->start_offset - block_begin);
6632 if (insn_block_len != ebb->start_offset - block_begin)
6635 /* xgettext:c-format */
6636 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6637 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6640 ebb->start_offset -= insn_block_len;
6642 /* Update the reloc counter. */
6643 while (ebb->start_reloc_idx > 0
6644 && (ebb->relocs[ebb->start_reloc_idx - 1].r_offset
6645 >= ebb->start_offset))
6647 ebb->start_reloc_idx--;
6650 if (ebb->start_ptbl_idx == 0)
6653 new_entry = &ebb->ptbl[ebb->start_ptbl_idx - 1];
6654 if ((new_entry->flags & XTENSA_PROP_INSN) == 0
6655 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6656 || ((new_entry->flags & XTENSA_PROP_ALIGN) != 0))
6658 if (new_entry->address + new_entry->size != the_entry->address)
6661 the_entry = new_entry;
6662 ebb->start_ptbl_idx--;
6668 static bfd_size_type
6669 insn_block_decodable_len (bfd_byte *contents,
6670 bfd_size_type content_len,
6671 bfd_vma block_offset,
6672 bfd_size_type block_len)
6674 bfd_vma offset = block_offset;
6676 while (offset < block_offset + block_len)
6678 bfd_size_type insn_len = 0;
6680 insn_len = insn_decode_len (contents, content_len, offset);
6682 return (offset - block_offset);
6685 return (offset - block_offset);
6690 ebb_propose_action (ebb_constraint *c,
6691 enum ebb_target_enum align_type,
6692 bfd_vma alignment_pow,
6693 text_action_t action,
6696 bfd_boolean do_action)
6698 proposed_action *act;
6700 if (c->action_allocated <= c->action_count)
6702 unsigned new_allocated, i;
6703 proposed_action *new_actions;
6705 new_allocated = (c->action_count + 2) * 2;
6706 new_actions = (proposed_action *)
6707 bfd_zmalloc (sizeof (proposed_action) * new_allocated);
6709 for (i = 0; i < c->action_count; i++)
6710 new_actions[i] = c->actions[i];
6713 c->actions = new_actions;
6714 c->action_allocated = new_allocated;
6717 act = &c->actions[c->action_count];
6718 act->align_type = align_type;
6719 act->alignment_pow = alignment_pow;
6720 act->action = action;
6721 act->offset = offset;
6722 act->removed_bytes = removed_bytes;
6723 act->do_action = do_action;
6729 /* Access to internal relocations, section contents and symbols. */
6731 /* During relaxation, we need to modify relocations, section contents,
6732 and symbol definitions, and we need to keep the original values from
6733 being reloaded from the input files, i.e., we need to "pin" the
6734 modified values in memory. We also want to continue to observe the
6735 setting of the "keep-memory" flag. The following functions wrap the
6736 standard BFD functions to take care of this for us. */
6738 static Elf_Internal_Rela *
6739 retrieve_internal_relocs (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6741 Elf_Internal_Rela *internal_relocs;
6743 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6746 internal_relocs = elf_section_data (sec)->relocs;
6747 if (internal_relocs == NULL)
6748 internal_relocs = (_bfd_elf_link_read_relocs
6749 (abfd, sec, NULL, NULL, keep_memory));
6750 return internal_relocs;
6755 pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6757 elf_section_data (sec)->relocs = internal_relocs;
6762 release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6765 && elf_section_data (sec)->relocs != internal_relocs)
6766 free (internal_relocs);
6771 retrieve_contents (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6774 bfd_size_type sec_size;
6776 sec_size = bfd_get_section_limit (abfd, sec);
6777 contents = elf_section_data (sec)->this_hdr.contents;
6779 if (contents == NULL && sec_size != 0)
6781 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6788 elf_section_data (sec)->this_hdr.contents = contents;
6795 pin_contents (asection *sec, bfd_byte *contents)
6797 elf_section_data (sec)->this_hdr.contents = contents;
6802 release_contents (asection *sec, bfd_byte *contents)
6804 if (contents && elf_section_data (sec)->this_hdr.contents != contents)
6809 static Elf_Internal_Sym *
6810 retrieve_local_syms (bfd *input_bfd)
6812 Elf_Internal_Shdr *symtab_hdr;
6813 Elf_Internal_Sym *isymbuf;
6816 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6817 locsymcount = symtab_hdr->sh_info;
6819 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6820 if (isymbuf == NULL && locsymcount != 0)
6821 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6824 /* Save the symbols for this input file so they won't be read again. */
6825 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
6826 symtab_hdr->contents = (unsigned char *) isymbuf;
6832 /* Code for link-time relaxation. */
6834 /* Initialization for relaxation: */
6835 static bfd_boolean analyze_relocations (struct bfd_link_info *);
6836 static bfd_boolean find_relaxable_sections
6837 (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
6838 static bfd_boolean collect_source_relocs
6839 (bfd *, asection *, struct bfd_link_info *);
6840 static bfd_boolean is_resolvable_asm_expansion
6841 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, struct bfd_link_info *,
6843 static Elf_Internal_Rela *find_associated_l32r_irel
6844 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Rela *);
6845 static bfd_boolean compute_text_actions
6846 (bfd *, asection *, struct bfd_link_info *);
6847 static bfd_boolean compute_ebb_proposed_actions (ebb_constraint *);
6848 static bfd_boolean compute_ebb_actions (ebb_constraint *);
6849 typedef struct reloc_range_list_struct reloc_range_list;
6850 static bfd_boolean check_section_ebb_pcrels_fit
6851 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
6852 reloc_range_list *, const ebb_constraint *,
6853 const xtensa_opcode *);
6854 static bfd_boolean check_section_ebb_reduces (const ebb_constraint *);
6855 static void text_action_add_proposed
6856 (text_action_list *, const ebb_constraint *, asection *);
6857 static int compute_fill_extra_space (property_table_entry *);
6860 static bfd_boolean compute_removed_literals
6861 (bfd *, asection *, struct bfd_link_info *, value_map_hash_table *);
6862 static Elf_Internal_Rela *get_irel_at_offset
6863 (asection *, Elf_Internal_Rela *, bfd_vma);
6864 static bfd_boolean is_removable_literal
6865 (const source_reloc *, int, const source_reloc *, int, asection *,
6866 property_table_entry *, int);
6867 static bfd_boolean remove_dead_literal
6868 (bfd *, asection *, struct bfd_link_info *, Elf_Internal_Rela *,
6869 Elf_Internal_Rela *, source_reloc *, property_table_entry *, int);
6870 static bfd_boolean identify_literal_placement
6871 (bfd *, asection *, bfd_byte *, struct bfd_link_info *,
6872 value_map_hash_table *, bfd_boolean *, Elf_Internal_Rela *, int,
6873 source_reloc *, property_table_entry *, int, section_cache_t *,
6875 static bfd_boolean relocations_reach (source_reloc *, int, const r_reloc *);
6876 static bfd_boolean coalesce_shared_literal
6877 (asection *, source_reloc *, property_table_entry *, int, value_map *);
6878 static bfd_boolean move_shared_literal
6879 (asection *, struct bfd_link_info *, source_reloc *, property_table_entry *,
6880 int, const r_reloc *, const literal_value *, section_cache_t *);
6883 static bfd_boolean relax_section (bfd *, asection *, struct bfd_link_info *);
6884 static bfd_boolean translate_section_fixes (asection *);
6885 static bfd_boolean translate_reloc_bfd_fix (reloc_bfd_fix *);
6886 static asection *translate_reloc (const r_reloc *, r_reloc *, asection *);
6887 static void shrink_dynamic_reloc_sections
6888 (struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *);
6889 static bfd_boolean move_literal
6890 (bfd *, struct bfd_link_info *, asection *, bfd_vma, bfd_byte *,
6891 xtensa_relax_info *, Elf_Internal_Rela **, const literal_value *);
6892 static bfd_boolean relax_property_section
6893 (bfd *, asection *, struct bfd_link_info *);
6896 static bfd_boolean relax_section_symbols (bfd *, asection *);
6900 elf_xtensa_relax_section (bfd *abfd,
6902 struct bfd_link_info *link_info,
6905 static value_map_hash_table *values = NULL;
6906 static bfd_boolean relocations_analyzed = FALSE;
6907 xtensa_relax_info *relax_info;
6909 if (!relocations_analyzed)
6911 /* Do some overall initialization for relaxation. */
6912 values = value_map_hash_table_init ();
6915 relaxing_section = TRUE;
6916 if (!analyze_relocations (link_info))
6918 relocations_analyzed = TRUE;
6922 /* Don't mess with linker-created sections. */
6923 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6926 relax_info = get_xtensa_relax_info (sec);
6927 BFD_ASSERT (relax_info != NULL);
6929 switch (relax_info->visited)
6932 /* Note: It would be nice to fold this pass into
6933 analyze_relocations, but it is important for this step that the
6934 sections be examined in link order. */
6935 if (!compute_removed_literals (abfd, sec, link_info, values))
6942 value_map_hash_table_delete (values);
6944 if (!relax_section (abfd, sec, link_info))
6950 if (!relax_section_symbols (abfd, sec))
6955 relax_info->visited++;
6960 /* Initialization for relaxation. */
6962 /* This function is called once at the start of relaxation. It scans
6963 all the input sections and marks the ones that are relaxable (i.e.,
6964 literal sections with L32R relocations against them), and then
6965 collects source_reloc information for all the relocations against
6966 those relaxable sections. During this process, it also detects
6967 longcalls, i.e., calls relaxed by the assembler into indirect
6968 calls, that can be optimized back into direct calls. Within each
6969 extended basic block (ebb) containing an optimized longcall, it
6970 computes a set of "text actions" that can be performed to remove
6971 the L32R associated with the longcall while optionally preserving
6972 branch target alignments. */
6975 analyze_relocations (struct bfd_link_info *link_info)
6979 bfd_boolean is_relaxable = FALSE;
6981 /* Initialize the per-section relaxation info. */
6982 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
6983 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6985 init_xtensa_relax_info (sec);
6988 /* Mark relaxable sections (and count relocations against each one). */
6989 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
6990 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6992 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
6996 /* Bail out if there are no relaxable sections. */
7000 /* Allocate space for source_relocs. */
7001 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7002 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7004 xtensa_relax_info *relax_info;
7006 relax_info = get_xtensa_relax_info (sec);
7007 if (relax_info->is_relaxable_literal_section
7008 || relax_info->is_relaxable_asm_section)
7010 relax_info->src_relocs = (source_reloc *)
7011 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
7014 relax_info->src_count = 0;
7017 /* Collect info on relocations against each relaxable section. */
7018 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7019 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7021 if (!collect_source_relocs (abfd, sec, link_info))
7025 /* Compute the text actions. */
7026 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
7027 for (sec = abfd->sections; sec != NULL; sec = sec->next)
7029 if (!compute_text_actions (abfd, sec, link_info))
7037 /* Find all the sections that might be relaxed. The motivation for
7038 this pass is that collect_source_relocs() needs to record _all_ the
7039 relocations that target each relaxable section. That is expensive
7040 and unnecessary unless the target section is actually going to be
7041 relaxed. This pass identifies all such sections by checking if
7042 they have L32Rs pointing to them. In the process, the total number
7043 of relocations targeting each section is also counted so that we
7044 know how much space to allocate for source_relocs against each
7045 relaxable literal section. */
7048 find_relaxable_sections (bfd *abfd,
7050 struct bfd_link_info *link_info,
7051 bfd_boolean *is_relaxable_p)
7053 Elf_Internal_Rela *internal_relocs;
7055 bfd_boolean ok = TRUE;
7057 xtensa_relax_info *source_relax_info;
7058 bfd_boolean is_l32r_reloc;
7060 internal_relocs = retrieve_internal_relocs (abfd, sec,
7061 link_info->keep_memory);
7062 if (internal_relocs == NULL)
7065 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7066 if (contents == NULL && sec->size != 0)
7072 source_relax_info = get_xtensa_relax_info (sec);
7073 for (i = 0; i < sec->reloc_count; i++)
7075 Elf_Internal_Rela *irel = &internal_relocs[i];
7077 asection *target_sec;
7078 xtensa_relax_info *target_relax_info;
7080 /* If this section has not already been marked as "relaxable", and
7081 if it contains any ASM_EXPAND relocations (marking expanded
7082 longcalls) that can be optimized into direct calls, then mark
7083 the section as "relaxable". */
7084 if (source_relax_info
7085 && !source_relax_info->is_relaxable_asm_section
7086 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_EXPAND)
7088 bfd_boolean is_reachable = FALSE;
7089 if (is_resolvable_asm_expansion (abfd, sec, contents, irel,
7090 link_info, &is_reachable)
7093 source_relax_info->is_relaxable_asm_section = TRUE;
7094 *is_relaxable_p = TRUE;
7098 r_reloc_init (&r_rel, abfd, irel, contents,
7099 bfd_get_section_limit (abfd, sec));
7101 target_sec = r_reloc_get_section (&r_rel);
7102 target_relax_info = get_xtensa_relax_info (target_sec);
7103 if (!target_relax_info)
7106 /* Count PC-relative operand relocations against the target section.
7107 Note: The conditions tested here must match the conditions under
7108 which init_source_reloc is called in collect_source_relocs(). */
7109 is_l32r_reloc = FALSE;
7110 if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7112 xtensa_opcode opcode =
7113 get_relocation_opcode (abfd, sec, contents, irel);
7114 if (opcode != XTENSA_UNDEFINED)
7116 is_l32r_reloc = (opcode == get_l32r_opcode ());
7117 if (!is_alt_relocation (ELF32_R_TYPE (irel->r_info))
7119 target_relax_info->src_count++;
7123 if (is_l32r_reloc && r_reloc_is_defined (&r_rel))
7125 /* Mark the target section as relaxable. */
7126 target_relax_info->is_relaxable_literal_section = TRUE;
7127 *is_relaxable_p = TRUE;
7132 release_contents (sec, contents);
7133 release_internal_relocs (sec, internal_relocs);
7138 /* Record _all_ the relocations that point to relaxable sections, and
7139 get rid of ASM_EXPAND relocs by either converting them to
7140 ASM_SIMPLIFY or by removing them. */
7143 collect_source_relocs (bfd *abfd,
7145 struct bfd_link_info *link_info)
7147 Elf_Internal_Rela *internal_relocs;
7149 bfd_boolean ok = TRUE;
7151 bfd_size_type sec_size;
7153 internal_relocs = retrieve_internal_relocs (abfd, sec,
7154 link_info->keep_memory);
7155 if (internal_relocs == NULL)
7158 sec_size = bfd_get_section_limit (abfd, sec);
7159 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7160 if (contents == NULL && sec_size != 0)
7166 /* Record relocations against relaxable literal sections. */
7167 for (i = 0; i < sec->reloc_count; i++)
7169 Elf_Internal_Rela *irel = &internal_relocs[i];
7171 asection *target_sec;
7172 xtensa_relax_info *target_relax_info;
7174 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7176 target_sec = r_reloc_get_section (&r_rel);
7177 target_relax_info = get_xtensa_relax_info (target_sec);
7179 if (target_relax_info
7180 && (target_relax_info->is_relaxable_literal_section
7181 || target_relax_info->is_relaxable_asm_section))
7183 xtensa_opcode opcode = XTENSA_UNDEFINED;
7185 bfd_boolean is_abs_literal = FALSE;
7187 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7189 /* None of the current alternate relocs are PC-relative,
7190 and only PC-relative relocs matter here. However, we
7191 still need to record the opcode for literal
7193 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7194 if (opcode == get_l32r_opcode ())
7196 is_abs_literal = TRUE;
7200 opcode = XTENSA_UNDEFINED;
7202 else if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
7204 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7205 opnd = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7208 if (opcode != XTENSA_UNDEFINED)
7210 int src_next = target_relax_info->src_next++;
7211 source_reloc *s_reloc = &target_relax_info->src_relocs[src_next];
7213 init_source_reloc (s_reloc, sec, &r_rel, opcode, opnd,
7219 /* Now get rid of ASM_EXPAND relocations. At this point, the
7220 src_relocs array for the target literal section may still be
7221 incomplete, but it must at least contain the entries for the L32R
7222 relocations associated with ASM_EXPANDs because they were just
7223 added in the preceding loop over the relocations. */
7225 for (i = 0; i < sec->reloc_count; i++)
7227 Elf_Internal_Rela *irel = &internal_relocs[i];
7228 bfd_boolean is_reachable;
7230 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
7236 Elf_Internal_Rela *l32r_irel;
7238 asection *target_sec;
7239 xtensa_relax_info *target_relax_info;
7241 /* Mark the source_reloc for the L32R so that it will be
7242 removed in compute_removed_literals(), along with the
7243 associated literal. */
7244 l32r_irel = find_associated_l32r_irel (abfd, sec, contents,
7245 irel, internal_relocs);
7246 if (l32r_irel == NULL)
7249 r_reloc_init (&r_rel, abfd, l32r_irel, contents, sec_size);
7251 target_sec = r_reloc_get_section (&r_rel);
7252 target_relax_info = get_xtensa_relax_info (target_sec);
7254 if (target_relax_info
7255 && (target_relax_info->is_relaxable_literal_section
7256 || target_relax_info->is_relaxable_asm_section))
7258 source_reloc *s_reloc;
7260 /* Search the source_relocs for the entry corresponding to
7261 the l32r_irel. Note: The src_relocs array is not yet
7262 sorted, but it wouldn't matter anyway because we're
7263 searching by source offset instead of target offset. */
7264 s_reloc = find_source_reloc (target_relax_info->src_relocs,
7265 target_relax_info->src_next,
7267 BFD_ASSERT (s_reloc);
7268 s_reloc->is_null = TRUE;
7271 /* Convert this reloc to ASM_SIMPLIFY. */
7272 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
7273 R_XTENSA_ASM_SIMPLIFY);
7274 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7276 pin_internal_relocs (sec, internal_relocs);
7280 /* It is resolvable but doesn't reach. We resolve now
7281 by eliminating the relocation -- the call will remain
7282 expanded into L32R/CALLX. */
7283 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7284 pin_internal_relocs (sec, internal_relocs);
7289 release_contents (sec, contents);
7290 release_internal_relocs (sec, internal_relocs);
7295 /* Return TRUE if the asm expansion can be resolved. Generally it can
7296 be resolved on a final link or when a partial link locates it in the
7297 same section as the target. Set "is_reachable" flag if the target of
7298 the call is within the range of a direct call, given the current VMA
7299 for this section and the target section. */
7302 is_resolvable_asm_expansion (bfd *abfd,
7305 Elf_Internal_Rela *irel,
7306 struct bfd_link_info *link_info,
7307 bfd_boolean *is_reachable_p)
7309 asection *target_sec;
7310 bfd_vma target_offset;
7312 xtensa_opcode opcode, direct_call_opcode;
7313 bfd_vma self_address;
7314 bfd_vma dest_address;
7315 bfd_boolean uses_l32r;
7316 bfd_size_type sec_size;
7318 *is_reachable_p = FALSE;
7320 if (contents == NULL)
7323 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
7326 sec_size = bfd_get_section_limit (abfd, sec);
7327 opcode = get_expanded_call_opcode (contents + irel->r_offset,
7328 sec_size - irel->r_offset, &uses_l32r);
7329 /* Optimization of longcalls that use CONST16 is not yet implemented. */
7333 direct_call_opcode = swap_callx_for_call_opcode (opcode);
7334 if (direct_call_opcode == XTENSA_UNDEFINED)
7337 /* Check and see that the target resolves. */
7338 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7339 if (!r_reloc_is_defined (&r_rel))
7342 target_sec = r_reloc_get_section (&r_rel);
7343 target_offset = r_rel.target_offset;
7345 /* If the target is in a shared library, then it doesn't reach. This
7346 isn't supposed to come up because the compiler should never generate
7347 non-PIC calls on systems that use shared libraries, but the linker
7348 shouldn't crash regardless. */
7349 if (!target_sec->output_section)
7352 /* For relocatable sections, we can only simplify when the output
7353 section of the target is the same as the output section of the
7355 if (bfd_link_relocatable (link_info)
7356 && (target_sec->output_section != sec->output_section
7357 || is_reloc_sym_weak (abfd, irel)))
7360 if (target_sec->output_section != sec->output_section)
7362 /* If the two sections are sufficiently far away that relaxation
7363 might take the call out of range, we can't simplify. For
7364 example, a positive displacement call into another memory
7365 could get moved to a lower address due to literal removal,
7366 but the destination won't move, and so the displacment might
7369 If the displacement is negative, assume the destination could
7370 move as far back as the start of the output section. The
7371 self_address will be at least as far into the output section
7372 as it is prior to relaxation.
7374 If the displacement is postive, assume the destination will be in
7375 it's pre-relaxed location (because relaxation only makes sections
7376 smaller). The self_address could go all the way to the beginning
7377 of the output section. */
7379 dest_address = target_sec->output_section->vma;
7380 self_address = sec->output_section->vma;
7382 if (sec->output_section->vma > target_sec->output_section->vma)
7383 self_address += sec->output_offset + irel->r_offset + 3;
7385 dest_address += bfd_get_section_limit (abfd, target_sec->output_section);
7386 /* Call targets should be four-byte aligned. */
7387 dest_address = (dest_address + 3) & ~3;
7392 self_address = (sec->output_section->vma
7393 + sec->output_offset + irel->r_offset + 3);
7394 dest_address = (target_sec->output_section->vma
7395 + target_sec->output_offset + target_offset);
7398 *is_reachable_p = pcrel_reloc_fits (direct_call_opcode, 0,
7399 self_address, dest_address);
7401 if ((self_address >> CALL_SEGMENT_BITS) !=
7402 (dest_address >> CALL_SEGMENT_BITS))
7409 static Elf_Internal_Rela *
7410 find_associated_l32r_irel (bfd *abfd,
7413 Elf_Internal_Rela *other_irel,
7414 Elf_Internal_Rela *internal_relocs)
7418 for (i = 0; i < sec->reloc_count; i++)
7420 Elf_Internal_Rela *irel = &internal_relocs[i];
7422 if (irel == other_irel)
7424 if (irel->r_offset != other_irel->r_offset)
7426 if (is_l32r_relocation (abfd, sec, contents, irel))
7434 static xtensa_opcode *
7435 build_reloc_opcodes (bfd *abfd,
7438 Elf_Internal_Rela *internal_relocs)
7441 xtensa_opcode *reloc_opcodes =
7442 (xtensa_opcode *) bfd_malloc (sizeof (xtensa_opcode) * sec->reloc_count);
7443 for (i = 0; i < sec->reloc_count; i++)
7445 Elf_Internal_Rela *irel = &internal_relocs[i];
7446 reloc_opcodes[i] = get_relocation_opcode (abfd, sec, contents, irel);
7448 return reloc_opcodes;
7451 struct reloc_range_struct
7454 bfd_boolean add; /* TRUE if start of a range, FALSE otherwise. */
7455 /* Original irel index in the array of relocations for a section. */
7456 unsigned irel_index;
7458 typedef struct reloc_range_struct reloc_range;
7460 typedef struct reloc_range_list_entry_struct reloc_range_list_entry;
7461 struct reloc_range_list_entry_struct
7463 reloc_range_list_entry *next;
7464 reloc_range_list_entry *prev;
7465 Elf_Internal_Rela *irel;
7466 xtensa_opcode opcode;
7470 struct reloc_range_list_struct
7472 /* The rest of the structure is only meaningful when ok is TRUE. */
7475 unsigned n_range; /* Number of range markers. */
7476 reloc_range *range; /* Sorted range markers. */
7478 unsigned first; /* Index of a first range element in the list. */
7479 unsigned last; /* One past index of a last range element in the list. */
7481 unsigned n_list; /* Number of list elements. */
7482 reloc_range_list_entry *reloc; /* */
7483 reloc_range_list_entry list_root;
7487 reloc_range_compare (const void *a, const void *b)
7489 const reloc_range *ra = a;
7490 const reloc_range *rb = b;
7492 if (ra->addr != rb->addr)
7493 return ra->addr < rb->addr ? -1 : 1;
7494 if (ra->add != rb->add)
7495 return ra->add ? -1 : 1;
7500 build_reloc_ranges (bfd *abfd, asection *sec,
7502 Elf_Internal_Rela *internal_relocs,
7503 xtensa_opcode *reloc_opcodes,
7504 reloc_range_list *list)
7509 reloc_range *ranges = NULL;
7510 reloc_range_list_entry *reloc =
7511 bfd_malloc (sec->reloc_count * sizeof (*reloc));
7513 memset (list, 0, sizeof (*list));
7516 for (i = 0; i < sec->reloc_count; i++)
7518 Elf_Internal_Rela *irel = &internal_relocs[i];
7519 int r_type = ELF32_R_TYPE (irel->r_info);
7520 reloc_howto_type *howto = &elf_howto_table[r_type];
7523 if (r_type == R_XTENSA_ASM_SIMPLIFY
7524 || r_type == R_XTENSA_32_PCREL
7525 || !howto->pc_relative)
7528 r_reloc_init (&r_rel, abfd, irel, contents,
7529 bfd_get_section_limit (abfd, sec));
7531 if (r_reloc_get_section (&r_rel) != sec)
7536 max_n = (max_n + 2) * 2;
7537 ranges = bfd_realloc (ranges, max_n * sizeof (*ranges));
7540 ranges[n].addr = irel->r_offset;
7541 ranges[n + 1].addr = r_rel.target_offset;
7543 ranges[n].add = ranges[n].addr < ranges[n + 1].addr;
7544 ranges[n + 1].add = !ranges[n].add;
7546 ranges[n].irel_index = i;
7547 ranges[n + 1].irel_index = i;
7551 reloc[i].irel = irel;
7553 /* Every relocation won't possibly be checked in the optimized version of
7554 check_section_ebb_pcrels_fit, so this needs to be done here. */
7555 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7557 /* None of the current alternate relocs are PC-relative,
7558 and only PC-relative relocs matter here. */
7562 xtensa_opcode opcode;
7566 opcode = reloc_opcodes[i];
7568 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7570 if (opcode == XTENSA_UNDEFINED)
7576 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7577 if (opnum == XTENSA_UNDEFINED)
7583 /* Record relocation opcode and opnum as we've calculated them
7584 anyway and they won't change. */
7585 reloc[i].opcode = opcode;
7586 reloc[i].opnum = opnum;
7592 ranges = bfd_realloc (ranges, n * sizeof (*ranges));
7593 qsort (ranges, n, sizeof (*ranges), reloc_range_compare);
7596 list->range = ranges;
7597 list->reloc = reloc;
7598 list->list_root.prev = &list->list_root;
7599 list->list_root.next = &list->list_root;
7608 static void reloc_range_list_append (reloc_range_list *list,
7609 unsigned irel_index)
7611 reloc_range_list_entry *entry = list->reloc + irel_index;
7613 entry->prev = list->list_root.prev;
7614 entry->next = &list->list_root;
7615 entry->prev->next = entry;
7616 entry->next->prev = entry;
7620 static void reloc_range_list_remove (reloc_range_list *list,
7621 unsigned irel_index)
7623 reloc_range_list_entry *entry = list->reloc + irel_index;
7625 entry->next->prev = entry->prev;
7626 entry->prev->next = entry->next;
7630 /* Update relocation list object so that it lists all relocations that cross
7631 [first; last] range. Range bounds should not decrease with successive
7633 static void reloc_range_list_update_range (reloc_range_list *list,
7634 bfd_vma first, bfd_vma last)
7636 /* This should not happen: EBBs are iterated from lower addresses to higher.
7637 But even if that happens there's no need to break: just flush current list
7638 and start from scratch. */
7639 if ((list->last > 0 && list->range[list->last - 1].addr > last) ||
7640 (list->first > 0 && list->range[list->first - 1].addr >= first))
7645 list->list_root.next = &list->list_root;
7646 list->list_root.prev = &list->list_root;
7647 fprintf (stderr, "%s: move backwards requested\n", __func__);
7650 for (; list->last < list->n_range &&
7651 list->range[list->last].addr <= last; ++list->last)
7652 if (list->range[list->last].add)
7653 reloc_range_list_append (list, list->range[list->last].irel_index);
7655 for (; list->first < list->n_range &&
7656 list->range[list->first].addr < first; ++list->first)
7657 if (!list->range[list->first].add)
7658 reloc_range_list_remove (list, list->range[list->first].irel_index);
7661 static void free_reloc_range_list (reloc_range_list *list)
7667 /* The compute_text_actions function will build a list of potential
7668 transformation actions for code in the extended basic block of each
7669 longcall that is optimized to a direct call. From this list we
7670 generate a set of actions to actually perform that optimizes for
7671 space and, if not using size_opt, maintains branch target
7674 These actions to be performed are placed on a per-section list.
7675 The actual changes are performed by relax_section() in the second
7679 compute_text_actions (bfd *abfd,
7681 struct bfd_link_info *link_info)
7683 xtensa_opcode *reloc_opcodes = NULL;
7684 xtensa_relax_info *relax_info;
7686 Elf_Internal_Rela *internal_relocs;
7687 bfd_boolean ok = TRUE;
7689 property_table_entry *prop_table = 0;
7691 bfd_size_type sec_size;
7692 reloc_range_list relevant_relocs;
7694 relax_info = get_xtensa_relax_info (sec);
7695 BFD_ASSERT (relax_info);
7696 BFD_ASSERT (relax_info->src_next == relax_info->src_count);
7698 /* Do nothing if the section contains no optimized longcalls. */
7699 if (!relax_info->is_relaxable_asm_section)
7702 internal_relocs = retrieve_internal_relocs (abfd, sec,
7703 link_info->keep_memory);
7705 if (internal_relocs)
7706 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
7707 internal_reloc_compare);
7709 sec_size = bfd_get_section_limit (abfd, sec);
7710 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7711 if (contents == NULL && sec_size != 0)
7717 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
7718 XTENSA_PROP_SEC_NAME, FALSE);
7725 /* Precompute the opcode for each relocation. */
7726 reloc_opcodes = build_reloc_opcodes (abfd, sec, contents, internal_relocs);
7728 build_reloc_ranges (abfd, sec, contents, internal_relocs, reloc_opcodes,
7731 for (i = 0; i < sec->reloc_count; i++)
7733 Elf_Internal_Rela *irel = &internal_relocs[i];
7735 property_table_entry *the_entry;
7738 ebb_constraint ebb_table;
7739 bfd_size_type simplify_size;
7741 if (irel && ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_SIMPLIFY)
7743 r_offset = irel->r_offset;
7745 simplify_size = get_asm_simplify_size (contents, sec_size, r_offset);
7746 if (simplify_size == 0)
7749 /* xgettext:c-format */
7750 (_("%B(%A+0x%lx): could not decode instruction for XTENSA_ASM_SIMPLIFY relocation; possible configuration mismatch"),
7751 sec->owner, sec, r_offset);
7755 /* If the instruction table is not around, then don't do this
7757 the_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
7758 sec->vma + irel->r_offset);
7759 if (the_entry == NULL || XTENSA_NO_NOP_REMOVAL)
7761 text_action_add (&relax_info->action_list,
7762 ta_convert_longcall, sec, r_offset,
7767 /* If the next longcall happens to be at the same address as an
7768 unreachable section of size 0, then skip forward. */
7769 ptbl_idx = the_entry - prop_table;
7770 while ((the_entry->flags & XTENSA_PROP_UNREACHABLE)
7771 && the_entry->size == 0
7772 && ptbl_idx + 1 < ptblsize
7773 && (prop_table[ptbl_idx + 1].address
7774 == prop_table[ptbl_idx].address))
7780 if (the_entry->flags & XTENSA_PROP_NO_TRANSFORM)
7781 /* NO_REORDER is OK */
7784 init_ebb_constraint (&ebb_table);
7785 ebb = &ebb_table.ebb;
7786 init_ebb (ebb, sec, contents, sec_size, prop_table, ptblsize,
7787 internal_relocs, sec->reloc_count);
7788 ebb->start_offset = r_offset + simplify_size;
7789 ebb->end_offset = r_offset + simplify_size;
7790 ebb->start_ptbl_idx = ptbl_idx;
7791 ebb->end_ptbl_idx = ptbl_idx;
7792 ebb->start_reloc_idx = i;
7793 ebb->end_reloc_idx = i;
7795 if (!extend_ebb_bounds (ebb)
7796 || !compute_ebb_proposed_actions (&ebb_table)
7797 || !compute_ebb_actions (&ebb_table)
7798 || !check_section_ebb_pcrels_fit (abfd, sec, contents,
7801 &ebb_table, reloc_opcodes)
7802 || !check_section_ebb_reduces (&ebb_table))
7804 /* If anything goes wrong or we get unlucky and something does
7805 not fit, with our plan because of expansion between
7806 critical branches, just convert to a NOP. */
7808 text_action_add (&relax_info->action_list,
7809 ta_convert_longcall, sec, r_offset, 0);
7810 i = ebb_table.ebb.end_reloc_idx;
7811 free_ebb_constraint (&ebb_table);
7815 text_action_add_proposed (&relax_info->action_list, &ebb_table, sec);
7817 /* Update the index so we do not go looking at the relocations
7818 we have already processed. */
7819 i = ebb_table.ebb.end_reloc_idx;
7820 free_ebb_constraint (&ebb_table);
7823 free_reloc_range_list (&relevant_relocs);
7826 if (action_list_count (&relax_info->action_list))
7827 print_action_list (stderr, &relax_info->action_list);
7831 release_contents (sec, contents);
7832 release_internal_relocs (sec, internal_relocs);
7836 free (reloc_opcodes);
7842 /* Do not widen an instruction if it is preceeded by a
7843 loop opcode. It might cause misalignment. */
7846 prev_instr_is_a_loop (bfd_byte *contents,
7847 bfd_size_type content_length,
7848 bfd_size_type offset)
7850 xtensa_opcode prev_opcode;
7854 prev_opcode = insn_decode_opcode (contents, content_length, offset-3, 0);
7855 return (xtensa_opcode_is_loop (xtensa_default_isa, prev_opcode) == 1);
7859 /* Find all of the possible actions for an extended basic block. */
7862 compute_ebb_proposed_actions (ebb_constraint *ebb_table)
7864 const ebb_t *ebb = &ebb_table->ebb;
7865 unsigned rel_idx = ebb->start_reloc_idx;
7866 property_table_entry *entry, *start_entry, *end_entry;
7868 xtensa_isa isa = xtensa_default_isa;
7870 static xtensa_insnbuf insnbuf = NULL;
7871 static xtensa_insnbuf slotbuf = NULL;
7873 if (insnbuf == NULL)
7875 insnbuf = xtensa_insnbuf_alloc (isa);
7876 slotbuf = xtensa_insnbuf_alloc (isa);
7879 start_entry = &ebb->ptbl[ebb->start_ptbl_idx];
7880 end_entry = &ebb->ptbl[ebb->end_ptbl_idx];
7882 for (entry = start_entry; entry <= end_entry; entry++)
7884 bfd_vma start_offset, end_offset;
7885 bfd_size_type insn_len;
7887 start_offset = entry->address - ebb->sec->vma;
7888 end_offset = entry->address + entry->size - ebb->sec->vma;
7890 if (entry == start_entry)
7891 start_offset = ebb->start_offset;
7892 if (entry == end_entry)
7893 end_offset = ebb->end_offset;
7894 offset = start_offset;
7896 if (offset == entry->address - ebb->sec->vma
7897 && (entry->flags & XTENSA_PROP_INSN_BRANCH_TARGET) != 0)
7899 enum ebb_target_enum align_type = EBB_DESIRE_TGT_ALIGN;
7900 BFD_ASSERT (offset != end_offset);
7901 if (offset == end_offset)
7904 insn_len = insn_decode_len (ebb->contents, ebb->content_length,
7909 if (check_branch_target_aligned_address (offset, insn_len))
7910 align_type = EBB_REQUIRE_TGT_ALIGN;
7912 ebb_propose_action (ebb_table, align_type, 0,
7913 ta_none, offset, 0, TRUE);
7916 while (offset != end_offset)
7918 Elf_Internal_Rela *irel;
7919 xtensa_opcode opcode;
7921 while (rel_idx < ebb->end_reloc_idx
7922 && (ebb->relocs[rel_idx].r_offset < offset
7923 || (ebb->relocs[rel_idx].r_offset == offset
7924 && (ELF32_R_TYPE (ebb->relocs[rel_idx].r_info)
7925 != R_XTENSA_ASM_SIMPLIFY))))
7928 /* Check for longcall. */
7929 irel = &ebb->relocs[rel_idx];
7930 if (irel->r_offset == offset
7931 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_SIMPLIFY)
7933 bfd_size_type simplify_size;
7935 simplify_size = get_asm_simplify_size (ebb->contents,
7936 ebb->content_length,
7938 if (simplify_size == 0)
7941 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7942 ta_convert_longcall, offset, 0, TRUE);
7944 offset += simplify_size;
7948 if (offset + MIN_INSN_LENGTH > ebb->content_length)
7950 xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
7951 ebb->content_length - offset);
7952 fmt = xtensa_format_decode (isa, insnbuf);
7953 if (fmt == XTENSA_UNDEFINED)
7955 insn_len = xtensa_format_length (isa, fmt);
7956 if (insn_len == (bfd_size_type) XTENSA_UNDEFINED)
7959 if (xtensa_format_num_slots (isa, fmt) != 1)
7965 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7966 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7967 if (opcode == XTENSA_UNDEFINED)
7970 if ((entry->flags & XTENSA_PROP_INSN_NO_DENSITY) == 0
7971 && (entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7972 && can_narrow_instruction (slotbuf, fmt, opcode) != 0)
7974 /* Add an instruction narrow action. */
7975 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7976 ta_narrow_insn, offset, 0, FALSE);
7978 else if ((entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7979 && can_widen_instruction (slotbuf, fmt, opcode) != 0
7980 && ! prev_instr_is_a_loop (ebb->contents,
7981 ebb->content_length, offset))
7983 /* Add an instruction widen action. */
7984 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7985 ta_widen_insn, offset, 0, FALSE);
7987 else if (xtensa_opcode_is_loop (xtensa_default_isa, opcode) == 1)
7989 /* Check for branch targets. */
7990 ebb_propose_action (ebb_table, EBB_REQUIRE_LOOP_ALIGN, 0,
7991 ta_none, offset, 0, TRUE);
7998 if (ebb->ends_unreachable)
8000 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
8001 ta_fill, ebb->end_offset, 0, TRUE);
8008 /* xgettext:c-format */
8009 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
8010 ebb->sec->owner, ebb->sec, offset);
8015 /* After all of the information has collected about the
8016 transformations possible in an EBB, compute the appropriate actions
8017 here in compute_ebb_actions. We still must check later to make
8018 sure that the actions do not break any relocations. The algorithm
8019 used here is pretty greedy. Basically, it removes as many no-ops
8020 as possible so that the end of the EBB has the same alignment
8021 characteristics as the original. First, it uses narrowing, then
8022 fill space at the end of the EBB, and finally widenings. If that
8023 does not work, it tries again with one fewer no-op removed. The
8024 optimization will only be performed if all of the branch targets
8025 that were aligned before transformation are also aligned after the
8028 When the size_opt flag is set, ignore the branch target alignments,
8029 narrow all wide instructions, and remove all no-ops unless the end
8030 of the EBB prevents it. */
8033 compute_ebb_actions (ebb_constraint *ebb_table)
8037 int removed_bytes = 0;
8038 ebb_t *ebb = &ebb_table->ebb;
8039 unsigned seg_idx_start = 0;
8040 unsigned seg_idx_end = 0;
8042 /* We perform this like the assembler relaxation algorithm: Start by
8043 assuming all instructions are narrow and all no-ops removed; then
8046 /* For each segment of this that has a solid constraint, check to
8047 see if there are any combinations that will keep the constraint.
8049 for (seg_idx_end = 0; seg_idx_end < ebb_table->action_count; seg_idx_end++)
8051 bfd_boolean requires_text_end_align = FALSE;
8052 unsigned longcall_count = 0;
8053 unsigned longcall_convert_count = 0;
8054 unsigned narrowable_count = 0;
8055 unsigned narrowable_convert_count = 0;
8056 unsigned widenable_count = 0;
8057 unsigned widenable_convert_count = 0;
8059 proposed_action *action = NULL;
8060 int align = (1 << ebb_table->ebb.sec->alignment_power);
8062 seg_idx_start = seg_idx_end;
8064 for (i = seg_idx_start; i < ebb_table->action_count; i++)
8066 action = &ebb_table->actions[i];
8067 if (action->action == ta_convert_longcall)
8069 if (action->action == ta_narrow_insn)
8071 if (action->action == ta_widen_insn)
8073 if (action->action == ta_fill)
8075 if (action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8077 if (action->align_type == EBB_REQUIRE_TGT_ALIGN
8078 && !elf32xtensa_size_opt)
8083 if (seg_idx_end == ebb_table->action_count && !ebb->ends_unreachable)
8084 requires_text_end_align = TRUE;
8086 if (elf32xtensa_size_opt && !requires_text_end_align
8087 && action->align_type != EBB_REQUIRE_LOOP_ALIGN
8088 && action->align_type != EBB_REQUIRE_TGT_ALIGN)
8090 longcall_convert_count = longcall_count;
8091 narrowable_convert_count = narrowable_count;
8092 widenable_convert_count = 0;
8096 /* There is a constraint. Convert the max number of longcalls. */
8097 narrowable_convert_count = 0;
8098 longcall_convert_count = 0;
8099 widenable_convert_count = 0;
8101 for (j = 0; j < longcall_count; j++)
8103 int removed = (longcall_count - j) * 3 & (align - 1);
8104 unsigned desire_narrow = (align - removed) & (align - 1);
8105 unsigned desire_widen = removed;
8106 if (desire_narrow <= narrowable_count)
8108 narrowable_convert_count = desire_narrow;
8109 narrowable_convert_count +=
8110 (align * ((narrowable_count - narrowable_convert_count)
8112 longcall_convert_count = (longcall_count - j);
8113 widenable_convert_count = 0;
8116 if (desire_widen <= widenable_count && !elf32xtensa_size_opt)
8118 narrowable_convert_count = 0;
8119 longcall_convert_count = longcall_count - j;
8120 widenable_convert_count = desire_widen;
8126 /* Now the number of conversions are saved. Do them. */
8127 for (i = seg_idx_start; i < seg_idx_end; i++)
8129 action = &ebb_table->actions[i];
8130 switch (action->action)
8132 case ta_convert_longcall:
8133 if (longcall_convert_count != 0)
8135 action->action = ta_remove_longcall;
8136 action->do_action = TRUE;
8137 action->removed_bytes += 3;
8138 longcall_convert_count--;
8141 case ta_narrow_insn:
8142 if (narrowable_convert_count != 0)
8144 action->do_action = TRUE;
8145 action->removed_bytes += 1;
8146 narrowable_convert_count--;
8150 if (widenable_convert_count != 0)
8152 action->do_action = TRUE;
8153 action->removed_bytes -= 1;
8154 widenable_convert_count--;
8163 /* Now we move on to some local opts. Try to remove each of the
8164 remaining longcalls. */
8166 if (ebb_table->ebb.ends_section || ebb_table->ebb.ends_unreachable)
8169 for (i = 0; i < ebb_table->action_count; i++)
8171 int old_removed_bytes = removed_bytes;
8172 proposed_action *action = &ebb_table->actions[i];
8174 if (action->do_action && action->action == ta_convert_longcall)
8176 bfd_boolean bad_alignment = FALSE;
8178 for (j = i + 1; j < ebb_table->action_count; j++)
8180 proposed_action *new_action = &ebb_table->actions[j];
8181 bfd_vma offset = new_action->offset;
8182 if (new_action->align_type == EBB_REQUIRE_TGT_ALIGN)
8184 if (!check_branch_target_aligned
8185 (ebb_table->ebb.contents,
8186 ebb_table->ebb.content_length,
8187 offset, offset - removed_bytes))
8189 bad_alignment = TRUE;
8193 if (new_action->align_type == EBB_REQUIRE_LOOP_ALIGN)
8195 if (!check_loop_aligned (ebb_table->ebb.contents,
8196 ebb_table->ebb.content_length,
8198 offset - removed_bytes))
8200 bad_alignment = TRUE;
8204 if (new_action->action == ta_narrow_insn
8205 && !new_action->do_action
8206 && ebb_table->ebb.sec->alignment_power == 2)
8208 /* Narrow an instruction and we are done. */
8209 new_action->do_action = TRUE;
8210 new_action->removed_bytes += 1;
8211 bad_alignment = FALSE;
8214 if (new_action->action == ta_widen_insn
8215 && new_action->do_action
8216 && ebb_table->ebb.sec->alignment_power == 2)
8218 /* Narrow an instruction and we are done. */
8219 new_action->do_action = FALSE;
8220 new_action->removed_bytes += 1;
8221 bad_alignment = FALSE;
8224 if (new_action->do_action)
8225 removed_bytes += new_action->removed_bytes;
8229 action->removed_bytes += 3;
8230 action->action = ta_remove_longcall;
8231 action->do_action = TRUE;
8234 removed_bytes = old_removed_bytes;
8235 if (action->do_action)
8236 removed_bytes += action->removed_bytes;
8241 for (i = 0; i < ebb_table->action_count; ++i)
8243 proposed_action *action = &ebb_table->actions[i];
8244 if (action->do_action)
8245 removed_bytes += action->removed_bytes;
8248 if ((removed_bytes % (1 << ebb_table->ebb.sec->alignment_power)) != 0
8249 && ebb->ends_unreachable)
8251 proposed_action *action;
8255 BFD_ASSERT (ebb_table->action_count != 0);
8256 action = &ebb_table->actions[ebb_table->action_count - 1];
8257 BFD_ASSERT (action->action == ta_fill);
8258 BFD_ASSERT (ebb->ends_unreachable->flags & XTENSA_PROP_UNREACHABLE);
8260 extra_space = compute_fill_extra_space (ebb->ends_unreachable);
8261 br = action->removed_bytes + removed_bytes + extra_space;
8262 br = br & ((1 << ebb->sec->alignment_power ) - 1);
8264 action->removed_bytes = extra_space - br;
8270 /* The xlate_map is a sorted array of address mappings designed to
8271 answer the offset_with_removed_text() query with a binary search instead
8272 of a linear search through the section's action_list. */
8274 typedef struct xlate_map_entry xlate_map_entry_t;
8275 typedef struct xlate_map xlate_map_t;
8277 struct xlate_map_entry
8279 unsigned orig_address;
8280 unsigned new_address;
8286 unsigned entry_count;
8287 xlate_map_entry_t *entry;
8292 xlate_compare (const void *a_v, const void *b_v)
8294 const xlate_map_entry_t *a = (const xlate_map_entry_t *) a_v;
8295 const xlate_map_entry_t *b = (const xlate_map_entry_t *) b_v;
8296 if (a->orig_address < b->orig_address)
8298 if (a->orig_address > (b->orig_address + b->size - 1))
8305 xlate_offset_with_removed_text (const xlate_map_t *map,
8306 text_action_list *action_list,
8310 xlate_map_entry_t *e;
8313 return offset_with_removed_text (action_list, offset);
8315 if (map->entry_count == 0)
8318 r = bsearch (&offset, map->entry, map->entry_count,
8319 sizeof (xlate_map_entry_t), &xlate_compare);
8320 e = (xlate_map_entry_t *) r;
8322 BFD_ASSERT (e != NULL);
8325 return e->new_address - e->orig_address + offset;
8328 typedef struct xlate_map_context_struct xlate_map_context;
8329 struct xlate_map_context_struct
8332 xlate_map_entry_t *current_entry;
8337 xlate_map_fn (splay_tree_node node, void *p)
8339 text_action *r = (text_action *)node->value;
8340 xlate_map_context *ctx = p;
8341 unsigned orig_size = 0;
8346 case ta_remove_insn:
8347 case ta_convert_longcall:
8348 case ta_remove_literal:
8349 case ta_add_literal:
8351 case ta_remove_longcall:
8354 case ta_narrow_insn:
8363 ctx->current_entry->size =
8364 r->offset + orig_size - ctx->current_entry->orig_address;
8365 if (ctx->current_entry->size != 0)
8367 ctx->current_entry++;
8368 ctx->map->entry_count++;
8370 ctx->current_entry->orig_address = r->offset + orig_size;
8371 ctx->removed += r->removed_bytes;
8372 ctx->current_entry->new_address = r->offset + orig_size - ctx->removed;
8373 ctx->current_entry->size = 0;
8377 /* Build a binary searchable offset translation map from a section's
8380 static xlate_map_t *
8381 build_xlate_map (asection *sec, xtensa_relax_info *relax_info)
8383 text_action_list *action_list = &relax_info->action_list;
8384 unsigned num_actions = 0;
8385 xlate_map_context ctx;
8387 ctx.map = (xlate_map_t *) bfd_malloc (sizeof (xlate_map_t));
8389 if (ctx.map == NULL)
8392 num_actions = action_list_count (action_list);
8393 ctx.map->entry = (xlate_map_entry_t *)
8394 bfd_malloc (sizeof (xlate_map_entry_t) * (num_actions + 1));
8395 if (ctx.map->entry == NULL)
8400 ctx.map->entry_count = 0;
8403 ctx.current_entry = &ctx.map->entry[0];
8405 ctx.current_entry->orig_address = 0;
8406 ctx.current_entry->new_address = 0;
8407 ctx.current_entry->size = 0;
8409 splay_tree_foreach (action_list->tree, xlate_map_fn, &ctx);
8411 ctx.current_entry->size = (bfd_get_section_limit (sec->owner, sec)
8412 - ctx.current_entry->orig_address);
8413 if (ctx.current_entry->size != 0)
8414 ctx.map->entry_count++;
8420 /* Free an offset translation map. */
8423 free_xlate_map (xlate_map_t *map)
8425 if (map && map->entry)
8432 /* Use check_section_ebb_pcrels_fit to make sure that all of the
8433 relocations in a section will fit if a proposed set of actions
8437 check_section_ebb_pcrels_fit (bfd *abfd,
8440 Elf_Internal_Rela *internal_relocs,
8441 reloc_range_list *relevant_relocs,
8442 const ebb_constraint *constraint,
8443 const xtensa_opcode *reloc_opcodes)
8446 unsigned n = sec->reloc_count;
8447 Elf_Internal_Rela *irel;
8448 xlate_map_t *xmap = NULL;
8449 bfd_boolean ok = TRUE;
8450 xtensa_relax_info *relax_info;
8451 reloc_range_list_entry *entry = NULL;
8453 relax_info = get_xtensa_relax_info (sec);
8455 if (relax_info && sec->reloc_count > 100)
8457 xmap = build_xlate_map (sec, relax_info);
8458 /* NULL indicates out of memory, but the slow version
8459 can still be used. */
8462 if (relevant_relocs && constraint->action_count)
8464 if (!relevant_relocs->ok)
8471 bfd_vma min_offset, max_offset;
8472 min_offset = max_offset = constraint->actions[0].offset;
8474 for (i = 1; i < constraint->action_count; ++i)
8476 proposed_action *action = &constraint->actions[i];
8477 bfd_vma offset = action->offset;
8479 if (offset < min_offset)
8480 min_offset = offset;
8481 if (offset > max_offset)
8482 max_offset = offset;
8484 reloc_range_list_update_range (relevant_relocs, min_offset,
8486 n = relevant_relocs->n_list;
8487 entry = &relevant_relocs->list_root;
8492 relevant_relocs = NULL;
8495 for (i = 0; i < n; i++)
8498 bfd_vma orig_self_offset, orig_target_offset;
8499 bfd_vma self_offset, target_offset;
8501 reloc_howto_type *howto;
8502 int self_removed_bytes, target_removed_bytes;
8504 if (relevant_relocs)
8506 entry = entry->next;
8511 irel = internal_relocs + i;
8513 r_type = ELF32_R_TYPE (irel->r_info);
8515 howto = &elf_howto_table[r_type];
8516 /* We maintain the required invariant: PC-relative relocations
8517 that fit before linking must fit after linking. Thus we only
8518 need to deal with relocations to the same section that are
8520 if (r_type == R_XTENSA_ASM_SIMPLIFY
8521 || r_type == R_XTENSA_32_PCREL
8522 || !howto->pc_relative)
8525 r_reloc_init (&r_rel, abfd, irel, contents,
8526 bfd_get_section_limit (abfd, sec));
8528 if (r_reloc_get_section (&r_rel) != sec)
8531 orig_self_offset = irel->r_offset;
8532 orig_target_offset = r_rel.target_offset;
8534 self_offset = orig_self_offset;
8535 target_offset = orig_target_offset;
8540 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8543 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
8544 orig_target_offset);
8547 self_removed_bytes = 0;
8548 target_removed_bytes = 0;
8550 for (j = 0; j < constraint->action_count; ++j)
8552 proposed_action *action = &constraint->actions[j];
8553 bfd_vma offset = action->offset;
8554 int removed_bytes = action->removed_bytes;
8555 if (offset < orig_self_offset
8556 || (offset == orig_self_offset && action->action == ta_fill
8557 && action->removed_bytes < 0))
8558 self_removed_bytes += removed_bytes;
8559 if (offset < orig_target_offset
8560 || (offset == orig_target_offset && action->action == ta_fill
8561 && action->removed_bytes < 0))
8562 target_removed_bytes += removed_bytes;
8564 self_offset -= self_removed_bytes;
8565 target_offset -= target_removed_bytes;
8567 /* Try to encode it. Get the operand and check. */
8568 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
8570 /* None of the current alternate relocs are PC-relative,
8571 and only PC-relative relocs matter here. */
8575 xtensa_opcode opcode;
8578 if (relevant_relocs)
8580 opcode = entry->opcode;
8581 opnum = entry->opnum;
8586 opcode = reloc_opcodes[relevant_relocs ?
8587 (unsigned)(entry - relevant_relocs->reloc) : i];
8589 opcode = get_relocation_opcode (abfd, sec, contents, irel);
8590 if (opcode == XTENSA_UNDEFINED)
8596 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
8597 if (opnum == XTENSA_UNDEFINED)
8604 if (!pcrel_reloc_fits (opcode, opnum, self_offset, target_offset))
8613 free_xlate_map (xmap);
8620 check_section_ebb_reduces (const ebb_constraint *constraint)
8625 for (i = 0; i < constraint->action_count; i++)
8627 const proposed_action *action = &constraint->actions[i];
8628 if (action->do_action)
8629 removed += action->removed_bytes;
8639 text_action_add_proposed (text_action_list *l,
8640 const ebb_constraint *ebb_table,
8645 for (i = 0; i < ebb_table->action_count; i++)
8647 proposed_action *action = &ebb_table->actions[i];
8649 if (!action->do_action)
8651 switch (action->action)
8653 case ta_remove_insn:
8654 case ta_remove_longcall:
8655 case ta_convert_longcall:
8656 case ta_narrow_insn:
8659 case ta_remove_literal:
8660 text_action_add (l, action->action, sec, action->offset,
8661 action->removed_bytes);
8674 compute_fill_extra_space (property_table_entry *entry)
8676 int fill_extra_space;
8681 if ((entry->flags & XTENSA_PROP_UNREACHABLE) == 0)
8684 fill_extra_space = entry->size;
8685 if ((entry->flags & XTENSA_PROP_ALIGN) != 0)
8687 /* Fill bytes for alignment:
8688 (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
8689 int pow = GET_XTENSA_PROP_ALIGNMENT (entry->flags);
8690 int nsm = (1 << pow) - 1;
8691 bfd_vma addr = entry->address + entry->size;
8692 bfd_vma align_fill = nsm - ((addr + nsm) & nsm);
8693 fill_extra_space += align_fill;
8695 return fill_extra_space;
8699 /* First relaxation pass. */
8701 /* If the section contains relaxable literals, check each literal to
8702 see if it has the same value as another literal that has already
8703 been seen, either in the current section or a previous one. If so,
8704 add an entry to the per-section list of removed literals. The
8705 actual changes are deferred until the next pass. */
8708 compute_removed_literals (bfd *abfd,
8710 struct bfd_link_info *link_info,
8711 value_map_hash_table *values)
8713 xtensa_relax_info *relax_info;
8715 Elf_Internal_Rela *internal_relocs;
8716 source_reloc *src_relocs, *rel;
8717 bfd_boolean ok = TRUE;
8718 property_table_entry *prop_table = NULL;
8721 bfd_boolean last_loc_is_prev = FALSE;
8722 bfd_vma last_target_offset = 0;
8723 section_cache_t target_sec_cache;
8724 bfd_size_type sec_size;
8726 init_section_cache (&target_sec_cache);
8728 /* Do nothing if it is not a relaxable literal section. */
8729 relax_info = get_xtensa_relax_info (sec);
8730 BFD_ASSERT (relax_info);
8731 if (!relax_info->is_relaxable_literal_section)
8734 internal_relocs = retrieve_internal_relocs (abfd, sec,
8735 link_info->keep_memory);
8737 sec_size = bfd_get_section_limit (abfd, sec);
8738 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
8739 if (contents == NULL && sec_size != 0)
8745 /* Sort the source_relocs by target offset. */
8746 src_relocs = relax_info->src_relocs;
8747 qsort (src_relocs, relax_info->src_count,
8748 sizeof (source_reloc), source_reloc_compare);
8749 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
8750 internal_reloc_compare);
8752 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
8753 XTENSA_PROP_SEC_NAME, FALSE);
8761 for (i = 0; i < relax_info->src_count; i++)
8763 Elf_Internal_Rela *irel = NULL;
8765 rel = &src_relocs[i];
8766 if (get_l32r_opcode () != rel->opcode)
8768 irel = get_irel_at_offset (sec, internal_relocs,
8769 rel->r_rel.target_offset);
8771 /* If the relocation on this is not a simple R_XTENSA_32 or
8772 R_XTENSA_PLT then do not consider it. This may happen when
8773 the difference of two symbols is used in a literal. */
8774 if (irel && (ELF32_R_TYPE (irel->r_info) != R_XTENSA_32
8775 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_PLT))
8778 /* If the target_offset for this relocation is the same as the
8779 previous relocation, then we've already considered whether the
8780 literal can be coalesced. Skip to the next one.... */
8781 if (i != 0 && prev_i != -1
8782 && src_relocs[i-1].r_rel.target_offset == rel->r_rel.target_offset)
8786 if (last_loc_is_prev &&
8787 last_target_offset + 4 != rel->r_rel.target_offset)
8788 last_loc_is_prev = FALSE;
8790 /* Check if the relocation was from an L32R that is being removed
8791 because a CALLX was converted to a direct CALL, and check if
8792 there are no other relocations to the literal. */
8793 if (is_removable_literal (rel, i, src_relocs, relax_info->src_count,
8794 sec, prop_table, ptblsize))
8796 if (!remove_dead_literal (abfd, sec, link_info, internal_relocs,
8797 irel, rel, prop_table, ptblsize))
8802 last_target_offset = rel->r_rel.target_offset;
8806 if (!identify_literal_placement (abfd, sec, contents, link_info,
8808 &last_loc_is_prev, irel,
8809 relax_info->src_count - i, rel,
8810 prop_table, ptblsize,
8811 &target_sec_cache, rel->is_abs_literal))
8816 last_target_offset = rel->r_rel.target_offset;
8820 print_removed_literals (stderr, &relax_info->removed_list);
8821 print_action_list (stderr, &relax_info->action_list);
8827 free_section_cache (&target_sec_cache);
8829 release_contents (sec, contents);
8830 release_internal_relocs (sec, internal_relocs);
8835 static Elf_Internal_Rela *
8836 get_irel_at_offset (asection *sec,
8837 Elf_Internal_Rela *internal_relocs,
8841 Elf_Internal_Rela *irel;
8843 Elf_Internal_Rela key;
8845 if (!internal_relocs)
8848 key.r_offset = offset;
8849 irel = bsearch (&key, internal_relocs, sec->reloc_count,
8850 sizeof (Elf_Internal_Rela), internal_reloc_matches);
8854 /* bsearch does not guarantee which will be returned if there are
8855 multiple matches. We need the first that is not an alignment. */
8856 i = irel - internal_relocs;
8859 if (internal_relocs[i-1].r_offset != offset)
8863 for ( ; i < sec->reloc_count; i++)
8865 irel = &internal_relocs[i];
8866 r_type = ELF32_R_TYPE (irel->r_info);
8867 if (irel->r_offset == offset && r_type != R_XTENSA_NONE)
8876 is_removable_literal (const source_reloc *rel,
8878 const source_reloc *src_relocs,
8881 property_table_entry *prop_table,
8884 const source_reloc *curr_rel;
8885 property_table_entry *entry;
8890 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8891 sec->vma + rel->r_rel.target_offset);
8892 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
8895 for (++i; i < src_count; ++i)
8897 curr_rel = &src_relocs[i];
8898 /* If all others have the same target offset.... */
8899 if (curr_rel->r_rel.target_offset != rel->r_rel.target_offset)
8902 if (!curr_rel->is_null
8903 && !xtensa_is_property_section (curr_rel->source_sec)
8904 && !(curr_rel->source_sec->flags & SEC_DEBUGGING))
8912 remove_dead_literal (bfd *abfd,
8914 struct bfd_link_info *link_info,
8915 Elf_Internal_Rela *internal_relocs,
8916 Elf_Internal_Rela *irel,
8918 property_table_entry *prop_table,
8921 property_table_entry *entry;
8922 xtensa_relax_info *relax_info;
8924 relax_info = get_xtensa_relax_info (sec);
8928 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8929 sec->vma + rel->r_rel.target_offset);
8931 /* Mark the unused literal so that it will be removed. */
8932 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
8934 text_action_add (&relax_info->action_list,
8935 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
8937 /* If the section is 4-byte aligned, do not add fill. */
8938 if (sec->alignment_power > 2)
8940 int fill_extra_space;
8941 bfd_vma entry_sec_offset;
8943 property_table_entry *the_add_entry;
8947 entry_sec_offset = entry->address - sec->vma + entry->size;
8949 entry_sec_offset = rel->r_rel.target_offset + 4;
8951 /* If the literal range is at the end of the section,
8953 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8955 fill_extra_space = compute_fill_extra_space (the_add_entry);
8957 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
8958 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
8959 -4, fill_extra_space);
8961 adjust_fill_action (fa, removed_diff);
8963 text_action_add (&relax_info->action_list,
8964 ta_fill, sec, entry_sec_offset, removed_diff);
8967 /* Zero out the relocation on this literal location. */
8970 if (elf_hash_table (link_info)->dynamic_sections_created)
8971 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
8973 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
8974 pin_internal_relocs (sec, internal_relocs);
8977 /* Do not modify "last_loc_is_prev". */
8983 identify_literal_placement (bfd *abfd,
8986 struct bfd_link_info *link_info,
8987 value_map_hash_table *values,
8988 bfd_boolean *last_loc_is_prev_p,
8989 Elf_Internal_Rela *irel,
8990 int remaining_src_rels,
8992 property_table_entry *prop_table,
8994 section_cache_t *target_sec_cache,
8995 bfd_boolean is_abs_literal)
8999 xtensa_relax_info *relax_info;
9000 bfd_boolean literal_placed = FALSE;
9002 unsigned long value;
9003 bfd_boolean final_static_link;
9004 bfd_size_type sec_size;
9006 relax_info = get_xtensa_relax_info (sec);
9010 sec_size = bfd_get_section_limit (abfd, sec);
9013 (!bfd_link_relocatable (link_info)
9014 && !elf_hash_table (link_info)->dynamic_sections_created);
9016 /* The placement algorithm first checks to see if the literal is
9017 already in the value map. If so and the value map is reachable
9018 from all uses, then the literal is moved to that location. If
9019 not, then we identify the last location where a fresh literal was
9020 placed. If the literal can be safely moved there, then we do so.
9021 If not, then we assume that the literal is not to move and leave
9022 the literal where it is, marking it as the last literal
9025 /* Find the literal value. */
9027 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9030 BFD_ASSERT (rel->r_rel.target_offset < sec_size);
9031 value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
9033 init_literal_value (&val, &r_rel, value, is_abs_literal);
9035 /* Check if we've seen another literal with the same value that
9036 is in the same output section. */
9037 val_map = value_map_get_cached_value (values, &val, final_static_link);
9040 && (r_reloc_get_section (&val_map->loc)->output_section
9041 == sec->output_section)
9042 && relocations_reach (rel, remaining_src_rels, &val_map->loc)
9043 && coalesce_shared_literal (sec, rel, prop_table, ptblsize, val_map))
9045 /* No change to last_loc_is_prev. */
9046 literal_placed = TRUE;
9049 /* For relocatable links, do not try to move literals. To do it
9050 correctly might increase the number of relocations in an input
9051 section making the default relocatable linking fail. */
9052 if (!bfd_link_relocatable (link_info) && !literal_placed
9053 && values->has_last_loc && !(*last_loc_is_prev_p))
9055 asection *target_sec = r_reloc_get_section (&values->last_loc);
9056 if (target_sec && target_sec->output_section == sec->output_section)
9058 /* Increment the virtual offset. */
9059 r_reloc try_loc = values->last_loc;
9060 try_loc.virtual_offset += 4;
9062 /* There is a last loc that was in the same output section. */
9063 if (relocations_reach (rel, remaining_src_rels, &try_loc)
9064 && move_shared_literal (sec, link_info, rel,
9065 prop_table, ptblsize,
9066 &try_loc, &val, target_sec_cache))
9068 values->last_loc.virtual_offset += 4;
9069 literal_placed = TRUE;
9071 val_map = add_value_map (values, &val, &try_loc,
9074 val_map->loc = try_loc;
9079 if (!literal_placed)
9081 /* Nothing worked, leave the literal alone but update the last loc. */
9082 values->has_last_loc = TRUE;
9083 values->last_loc = rel->r_rel;
9085 val_map = add_value_map (values, &val, &rel->r_rel, final_static_link);
9087 val_map->loc = rel->r_rel;
9088 *last_loc_is_prev_p = TRUE;
9095 /* Check if the original relocations (presumably on L32R instructions)
9096 identified by reloc[0..N] can be changed to reference the literal
9097 identified by r_rel. If r_rel is out of range for any of the
9098 original relocations, then we don't want to coalesce the original
9099 literal with the one at r_rel. We only check reloc[0..N], where the
9100 offsets are all the same as for reloc[0] (i.e., they're all
9101 referencing the same literal) and where N is also bounded by the
9102 number of remaining entries in the "reloc" array. The "reloc" array
9103 is sorted by target offset so we know all the entries for the same
9104 literal will be contiguous. */
9107 relocations_reach (source_reloc *reloc,
9108 int remaining_relocs,
9109 const r_reloc *r_rel)
9111 bfd_vma from_offset, source_address, dest_address;
9115 if (!r_reloc_is_defined (r_rel))
9118 sec = r_reloc_get_section (r_rel);
9119 from_offset = reloc[0].r_rel.target_offset;
9121 for (i = 0; i < remaining_relocs; i++)
9123 if (reloc[i].r_rel.target_offset != from_offset)
9126 /* Ignore relocations that have been removed. */
9127 if (reloc[i].is_null)
9130 /* The original and new output section for these must be the same
9131 in order to coalesce. */
9132 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
9133 != sec->output_section)
9136 /* Absolute literals in the same output section can always be
9138 if (reloc[i].is_abs_literal)
9141 /* A literal with no PC-relative relocations can be moved anywhere. */
9142 if (reloc[i].opnd != -1)
9144 /* Otherwise, check to see that it fits. */
9145 source_address = (reloc[i].source_sec->output_section->vma
9146 + reloc[i].source_sec->output_offset
9147 + reloc[i].r_rel.rela.r_offset);
9148 dest_address = (sec->output_section->vma
9149 + sec->output_offset
9150 + r_rel->target_offset);
9152 if (!pcrel_reloc_fits (reloc[i].opcode, reloc[i].opnd,
9153 source_address, dest_address))
9162 /* Move a literal to another literal location because it is
9163 the same as the other literal value. */
9166 coalesce_shared_literal (asection *sec,
9168 property_table_entry *prop_table,
9172 property_table_entry *entry;
9174 property_table_entry *the_add_entry;
9176 xtensa_relax_info *relax_info;
9178 relax_info = get_xtensa_relax_info (sec);
9182 entry = elf_xtensa_find_property_entry
9183 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9184 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
9187 /* Mark that the literal will be coalesced. */
9188 add_removed_literal (&relax_info->removed_list, &rel->r_rel, &val_map->loc);
9190 text_action_add (&relax_info->action_list,
9191 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9193 /* If the section is 4-byte aligned, do not add fill. */
9194 if (sec->alignment_power > 2)
9196 int fill_extra_space;
9197 bfd_vma entry_sec_offset;
9200 entry_sec_offset = entry->address - sec->vma + entry->size;
9202 entry_sec_offset = rel->r_rel.target_offset + 4;
9204 /* If the literal range is at the end of the section,
9206 fill_extra_space = 0;
9207 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9209 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9210 fill_extra_space = the_add_entry->size;
9212 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9213 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9214 -4, fill_extra_space);
9216 adjust_fill_action (fa, removed_diff);
9218 text_action_add (&relax_info->action_list,
9219 ta_fill, sec, entry_sec_offset, removed_diff);
9226 /* Move a literal to another location. This may actually increase the
9227 total amount of space used because of alignments so we need to do
9228 this carefully. Also, it may make a branch go out of range. */
9231 move_shared_literal (asection *sec,
9232 struct bfd_link_info *link_info,
9234 property_table_entry *prop_table,
9236 const r_reloc *target_loc,
9237 const literal_value *lit_value,
9238 section_cache_t *target_sec_cache)
9240 property_table_entry *the_add_entry, *src_entry, *target_entry = NULL;
9241 text_action *fa, *target_fa;
9243 xtensa_relax_info *relax_info, *target_relax_info;
9244 asection *target_sec;
9246 ebb_constraint ebb_table;
9247 bfd_boolean relocs_fit;
9249 /* If this routine always returns FALSE, the literals that cannot be
9250 coalesced will not be moved. */
9251 if (elf32xtensa_no_literal_movement)
9254 relax_info = get_xtensa_relax_info (sec);
9258 target_sec = r_reloc_get_section (target_loc);
9259 target_relax_info = get_xtensa_relax_info (target_sec);
9261 /* Literals to undefined sections may not be moved because they
9262 must report an error. */
9263 if (bfd_is_und_section (target_sec))
9266 src_entry = elf_xtensa_find_property_entry
9267 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
9269 if (!section_cache_section (target_sec_cache, target_sec, link_info))
9272 target_entry = elf_xtensa_find_property_entry
9273 (target_sec_cache->ptbl, target_sec_cache->pte_count,
9274 target_sec->vma + target_loc->target_offset);
9279 /* Make sure that we have not broken any branches. */
9282 init_ebb_constraint (&ebb_table);
9283 ebb = &ebb_table.ebb;
9284 init_ebb (ebb, target_sec_cache->sec, target_sec_cache->contents,
9285 target_sec_cache->content_length,
9286 target_sec_cache->ptbl, target_sec_cache->pte_count,
9287 target_sec_cache->relocs, target_sec_cache->reloc_count);
9289 /* Propose to add 4 bytes + worst-case alignment size increase to
9291 ebb_propose_action (&ebb_table, EBB_NO_ALIGN, 0,
9292 ta_fill, target_loc->target_offset,
9293 -4 - (1 << target_sec->alignment_power), TRUE);
9295 /* Check all of the PC-relative relocations to make sure they still fit. */
9296 relocs_fit = check_section_ebb_pcrels_fit (target_sec->owner, target_sec,
9297 target_sec_cache->contents,
9298 target_sec_cache->relocs, NULL,
9304 text_action_add_literal (&target_relax_info->action_list,
9305 ta_add_literal, target_loc, lit_value, -4);
9307 if (target_sec->alignment_power > 2 && target_entry != src_entry)
9309 /* May need to add or remove some fill to maintain alignment. */
9310 int fill_extra_space;
9311 bfd_vma entry_sec_offset;
9314 target_entry->address - target_sec->vma + target_entry->size;
9316 /* If the literal range is at the end of the section,
9318 fill_extra_space = 0;
9320 elf_xtensa_find_property_entry (target_sec_cache->ptbl,
9321 target_sec_cache->pte_count,
9323 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9324 fill_extra_space = the_add_entry->size;
9326 target_fa = find_fill_action (&target_relax_info->action_list,
9327 target_sec, entry_sec_offset);
9328 removed_diff = compute_removed_action_diff (target_fa, target_sec,
9329 entry_sec_offset, 4,
9332 adjust_fill_action (target_fa, removed_diff);
9334 text_action_add (&target_relax_info->action_list,
9335 ta_fill, target_sec, entry_sec_offset, removed_diff);
9338 /* Mark that the literal will be moved to the new location. */
9339 add_removed_literal (&relax_info->removed_list, &rel->r_rel, target_loc);
9341 /* Remove the literal. */
9342 text_action_add (&relax_info->action_list,
9343 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
9345 /* If the section is 4-byte aligned, do not add fill. */
9346 if (sec->alignment_power > 2 && target_entry != src_entry)
9348 int fill_extra_space;
9349 bfd_vma entry_sec_offset;
9352 entry_sec_offset = src_entry->address - sec->vma + src_entry->size;
9354 entry_sec_offset = rel->r_rel.target_offset+4;
9356 /* If the literal range is at the end of the section,
9358 fill_extra_space = 0;
9359 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
9361 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
9362 fill_extra_space = the_add_entry->size;
9364 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
9365 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
9366 -4, fill_extra_space);
9368 adjust_fill_action (fa, removed_diff);
9370 text_action_add (&relax_info->action_list,
9371 ta_fill, sec, entry_sec_offset, removed_diff);
9378 /* Second relaxation pass. */
9381 action_remove_bytes_fn (splay_tree_node node, void *p)
9383 bfd_size_type *final_size = p;
9384 text_action *action = (text_action *)node->value;
9386 *final_size -= action->removed_bytes;
9390 /* Modify all of the relocations to point to the right spot, and if this
9391 is a relaxable section, delete the unwanted literals and fix the
9395 relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
9397 Elf_Internal_Rela *internal_relocs;
9398 xtensa_relax_info *relax_info;
9400 bfd_boolean ok = TRUE;
9402 bfd_boolean rv = FALSE;
9403 bfd_boolean virtual_action;
9404 bfd_size_type sec_size;
9406 sec_size = bfd_get_section_limit (abfd, sec);
9407 relax_info = get_xtensa_relax_info (sec);
9408 BFD_ASSERT (relax_info);
9410 /* First translate any of the fixes that have been added already. */
9411 translate_section_fixes (sec);
9413 /* Handle property sections (e.g., literal tables) specially. */
9414 if (xtensa_is_property_section (sec))
9416 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
9417 return relax_property_section (abfd, sec, link_info);
9420 internal_relocs = retrieve_internal_relocs (abfd, sec,
9421 link_info->keep_memory);
9422 if (!internal_relocs && !action_list_count (&relax_info->action_list))
9425 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
9426 if (contents == NULL && sec_size != 0)
9432 if (internal_relocs)
9434 for (i = 0; i < sec->reloc_count; i++)
9436 Elf_Internal_Rela *irel;
9437 xtensa_relax_info *target_relax_info;
9438 bfd_vma source_offset, old_source_offset;
9441 asection *target_sec;
9443 /* Locally change the source address.
9444 Translate the target to the new target address.
9445 If it points to this section and has been removed,
9449 irel = &internal_relocs[i];
9450 source_offset = irel->r_offset;
9451 old_source_offset = source_offset;
9453 r_type = ELF32_R_TYPE (irel->r_info);
9454 r_reloc_init (&r_rel, abfd, irel, contents,
9455 bfd_get_section_limit (abfd, sec));
9457 /* If this section could have changed then we may need to
9458 change the relocation's offset. */
9460 if (relax_info->is_relaxable_literal_section
9461 || relax_info->is_relaxable_asm_section)
9463 pin_internal_relocs (sec, internal_relocs);
9465 if (r_type != R_XTENSA_NONE
9466 && find_removed_literal (&relax_info->removed_list,
9469 /* Remove this relocation. */
9470 if (elf_hash_table (link_info)->dynamic_sections_created)
9471 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
9472 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
9473 irel->r_offset = offset_with_removed_text_map
9474 (&relax_info->action_list, irel->r_offset);
9478 if (r_type == R_XTENSA_ASM_SIMPLIFY)
9480 text_action *action =
9481 find_insn_action (&relax_info->action_list,
9483 if (action && (action->action == ta_convert_longcall
9484 || action->action == ta_remove_longcall))
9486 bfd_reloc_status_type retval;
9487 char *error_message = NULL;
9489 retval = contract_asm_expansion (contents, sec_size,
9490 irel, &error_message);
9491 if (retval != bfd_reloc_ok)
9493 (*link_info->callbacks->reloc_dangerous)
9494 (link_info, error_message, abfd, sec,
9498 /* Update the action so that the code that moves
9499 the contents will do the right thing. */
9500 /* ta_remove_longcall and ta_remove_insn actions are
9501 grouped together in the tree as well as
9502 ta_convert_longcall and ta_none, so that changes below
9503 can be done w/o removing and reinserting action into
9506 if (action->action == ta_remove_longcall)
9507 action->action = ta_remove_insn;
9509 action->action = ta_none;
9510 /* Refresh the info in the r_rel. */
9511 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
9512 r_type = ELF32_R_TYPE (irel->r_info);
9516 source_offset = offset_with_removed_text_map
9517 (&relax_info->action_list, irel->r_offset);
9518 irel->r_offset = source_offset;
9521 /* If the target section could have changed then
9522 we may need to change the relocation's target offset. */
9524 target_sec = r_reloc_get_section (&r_rel);
9526 /* For a reference to a discarded section from a DWARF section,
9527 i.e., where action_discarded is PRETEND, the symbol will
9528 eventually be modified to refer to the kept section (at least if
9529 the kept and discarded sections are the same size). Anticipate
9530 that here and adjust things accordingly. */
9531 if (! elf_xtensa_ignore_discarded_relocs (sec)
9532 && elf_xtensa_action_discarded (sec) == PRETEND
9533 && sec->sec_info_type != SEC_INFO_TYPE_STABS
9534 && target_sec != NULL
9535 && discarded_section (target_sec))
9537 /* It would be natural to call _bfd_elf_check_kept_section
9538 here, but it's not exported from elflink.c. It's also a
9539 fairly expensive check. Adjusting the relocations to the
9540 discarded section is fairly harmless; it will only adjust
9541 some addends and difference values. If it turns out that
9542 _bfd_elf_check_kept_section fails later, it won't matter,
9543 so just compare the section names to find the right group
9545 asection *kept = target_sec->kept_section;
9548 if ((kept->flags & SEC_GROUP) != 0)
9550 asection *first = elf_next_in_group (kept);
9551 asection *s = first;
9556 if (strcmp (s->name, target_sec->name) == 0)
9561 s = elf_next_in_group (s);
9568 && ((target_sec->rawsize != 0
9569 ? target_sec->rawsize : target_sec->size)
9570 == (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9574 target_relax_info = get_xtensa_relax_info (target_sec);
9575 if (target_relax_info
9576 && (target_relax_info->is_relaxable_literal_section
9577 || target_relax_info->is_relaxable_asm_section))
9580 target_sec = translate_reloc (&r_rel, &new_reloc, target_sec);
9582 if (r_type == R_XTENSA_DIFF8
9583 || r_type == R_XTENSA_DIFF16
9584 || r_type == R_XTENSA_DIFF32)
9586 bfd_signed_vma diff_value = 0;
9587 bfd_vma new_end_offset, diff_mask = 0;
9589 if (bfd_get_section_limit (abfd, sec) < old_source_offset)
9591 (*link_info->callbacks->reloc_dangerous)
9592 (link_info, _("invalid relocation address"),
9593 abfd, sec, old_source_offset);
9599 case R_XTENSA_DIFF8:
9601 bfd_get_signed_8 (abfd, &contents[old_source_offset]);
9603 case R_XTENSA_DIFF16:
9605 bfd_get_signed_16 (abfd, &contents[old_source_offset]);
9607 case R_XTENSA_DIFF32:
9609 bfd_get_signed_32 (abfd, &contents[old_source_offset]);
9613 new_end_offset = offset_with_removed_text_map
9614 (&target_relax_info->action_list,
9615 r_rel.target_offset + diff_value);
9616 diff_value = new_end_offset - new_reloc.target_offset;
9620 case R_XTENSA_DIFF8:
9622 bfd_put_signed_8 (abfd, diff_value,
9623 &contents[old_source_offset]);
9625 case R_XTENSA_DIFF16:
9627 bfd_put_signed_16 (abfd, diff_value,
9628 &contents[old_source_offset]);
9630 case R_XTENSA_DIFF32:
9631 diff_mask = 0x7fffffff;
9632 bfd_put_signed_32 (abfd, diff_value,
9633 &contents[old_source_offset]);
9637 /* Check for overflow. Sign bits must be all zeroes or all ones */
9638 if ((diff_value & ~diff_mask) != 0 &&
9639 (diff_value & ~diff_mask) != (-1 & ~diff_mask))
9641 (*link_info->callbacks->reloc_dangerous)
9642 (link_info, _("overflow after relaxation"),
9643 abfd, sec, old_source_offset);
9647 pin_contents (sec, contents);
9650 /* If the relocation still references a section in the same
9651 input file, modify the relocation directly instead of
9652 adding a "fix" record. */
9653 if (target_sec->owner == abfd)
9655 unsigned r_symndx = ELF32_R_SYM (new_reloc.rela.r_info);
9656 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
9657 irel->r_addend = new_reloc.rela.r_addend;
9658 pin_internal_relocs (sec, internal_relocs);
9662 bfd_vma addend_displacement;
9665 addend_displacement =
9666 new_reloc.target_offset + new_reloc.virtual_offset;
9667 fix = reloc_bfd_fix_init (sec, source_offset, r_type,
9669 addend_displacement, TRUE);
9676 if ((relax_info->is_relaxable_literal_section
9677 || relax_info->is_relaxable_asm_section)
9678 && action_list_count (&relax_info->action_list))
9680 /* Walk through the planned actions and build up a table
9681 of move, copy and fill records. Use the move, copy and
9682 fill records to perform the actions once. */
9684 bfd_size_type final_size, copy_size, orig_insn_size;
9685 bfd_byte *scratch = NULL;
9686 bfd_byte *dup_contents = NULL;
9687 bfd_size_type orig_size = sec->size;
9688 bfd_vma orig_dot = 0;
9689 bfd_vma orig_dot_copied = 0; /* Byte copied already from
9690 orig dot in physical memory. */
9691 bfd_vma orig_dot_vo = 0; /* Virtual offset from orig_dot. */
9692 bfd_vma dup_dot = 0;
9694 text_action *action;
9696 final_size = sec->size;
9698 splay_tree_foreach (relax_info->action_list.tree,
9699 action_remove_bytes_fn, &final_size);
9700 scratch = (bfd_byte *) bfd_zmalloc (final_size);
9701 dup_contents = (bfd_byte *) bfd_zmalloc (final_size);
9703 /* The dot is the current fill location. */
9705 print_action_list (stderr, &relax_info->action_list);
9708 for (action = action_first (&relax_info->action_list); action;
9709 action = action_next (&relax_info->action_list, action))
9711 virtual_action = FALSE;
9712 if (action->offset > orig_dot)
9714 orig_dot += orig_dot_copied;
9715 orig_dot_copied = 0;
9717 /* Out of the virtual world. */
9720 if (action->offset > orig_dot)
9722 copy_size = action->offset - orig_dot;
9723 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9724 orig_dot += copy_size;
9725 dup_dot += copy_size;
9726 BFD_ASSERT (action->offset == orig_dot);
9728 else if (action->offset < orig_dot)
9730 if (action->action == ta_fill
9731 && action->offset - action->removed_bytes == orig_dot)
9733 /* This is OK because the fill only effects the dup_dot. */
9735 else if (action->action == ta_add_literal)
9737 /* TBD. Might need to handle this. */
9740 if (action->offset == orig_dot)
9742 if (action->virtual_offset > orig_dot_vo)
9744 if (orig_dot_vo == 0)
9746 /* Need to copy virtual_offset bytes. Probably four. */
9747 copy_size = action->virtual_offset - orig_dot_vo;
9748 memmove (&dup_contents[dup_dot],
9749 &contents[orig_dot], copy_size);
9750 orig_dot_copied = copy_size;
9751 dup_dot += copy_size;
9753 virtual_action = TRUE;
9756 BFD_ASSERT (action->virtual_offset <= orig_dot_vo);
9758 switch (action->action)
9760 case ta_remove_literal:
9761 case ta_remove_insn:
9762 BFD_ASSERT (action->removed_bytes >= 0);
9763 orig_dot += action->removed_bytes;
9766 case ta_narrow_insn:
9769 memmove (scratch, &contents[orig_dot], orig_insn_size);
9770 BFD_ASSERT (action->removed_bytes == 1);
9771 rv = narrow_instruction (scratch, final_size, 0);
9773 memmove (&dup_contents[dup_dot], scratch, copy_size);
9774 orig_dot += orig_insn_size;
9775 dup_dot += copy_size;
9779 if (action->removed_bytes >= 0)
9780 orig_dot += action->removed_bytes;
9783 /* Already zeroed in dup_contents. Just bump the
9785 dup_dot += (-action->removed_bytes);
9790 BFD_ASSERT (action->removed_bytes == 0);
9793 case ta_convert_longcall:
9794 case ta_remove_longcall:
9795 /* These will be removed or converted before we get here. */
9802 memmove (scratch, &contents[orig_dot], orig_insn_size);
9803 BFD_ASSERT (action->removed_bytes == -1);
9804 rv = widen_instruction (scratch, final_size, 0);
9806 memmove (&dup_contents[dup_dot], scratch, copy_size);
9807 orig_dot += orig_insn_size;
9808 dup_dot += copy_size;
9811 case ta_add_literal:
9814 BFD_ASSERT (action->removed_bytes == -4);
9815 /* TBD -- place the literal value here and insert
9817 memset (&dup_contents[dup_dot], 0, 4);
9818 pin_internal_relocs (sec, internal_relocs);
9819 pin_contents (sec, contents);
9821 if (!move_literal (abfd, link_info, sec, dup_dot, dup_contents,
9822 relax_info, &internal_relocs, &action->value))
9826 orig_dot_vo += copy_size;
9828 orig_dot += orig_insn_size;
9829 dup_dot += copy_size;
9833 /* Not implemented yet. */
9838 BFD_ASSERT (dup_dot <= final_size);
9839 BFD_ASSERT (orig_dot <= orig_size);
9842 orig_dot += orig_dot_copied;
9843 orig_dot_copied = 0;
9845 if (orig_dot != orig_size)
9847 copy_size = orig_size - orig_dot;
9848 BFD_ASSERT (orig_size > orig_dot);
9849 BFD_ASSERT (dup_dot + copy_size == final_size);
9850 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9851 orig_dot += copy_size;
9852 dup_dot += copy_size;
9854 BFD_ASSERT (orig_size == orig_dot);
9855 BFD_ASSERT (final_size == dup_dot);
9857 /* Move the dup_contents back. */
9858 if (final_size > orig_size)
9860 /* Contents need to be reallocated. Swap the dup_contents into
9862 sec->contents = dup_contents;
9864 contents = dup_contents;
9865 pin_contents (sec, contents);
9869 BFD_ASSERT (final_size <= orig_size);
9870 memset (contents, 0, orig_size);
9871 memcpy (contents, dup_contents, final_size);
9872 free (dup_contents);
9875 pin_contents (sec, contents);
9877 if (sec->rawsize == 0)
9878 sec->rawsize = sec->size;
9879 sec->size = final_size;
9883 release_internal_relocs (sec, internal_relocs);
9884 release_contents (sec, contents);
9890 translate_section_fixes (asection *sec)
9892 xtensa_relax_info *relax_info;
9895 relax_info = get_xtensa_relax_info (sec);
9899 for (r = relax_info->fix_list; r != NULL; r = r->next)
9900 if (!translate_reloc_bfd_fix (r))
9907 /* Translate a fix given the mapping in the relax info for the target
9908 section. If it has already been translated, no work is required. */
9911 translate_reloc_bfd_fix (reloc_bfd_fix *fix)
9913 reloc_bfd_fix new_fix;
9915 xtensa_relax_info *relax_info;
9916 removed_literal *removed;
9917 bfd_vma new_offset, target_offset;
9919 if (fix->translated)
9922 sec = fix->target_sec;
9923 target_offset = fix->target_offset;
9925 relax_info = get_xtensa_relax_info (sec);
9928 fix->translated = TRUE;
9934 /* The fix does not need to be translated if the section cannot change. */
9935 if (!relax_info->is_relaxable_literal_section
9936 && !relax_info->is_relaxable_asm_section)
9938 fix->translated = TRUE;
9942 /* If the literal has been moved and this relocation was on an
9943 opcode, then the relocation should move to the new literal
9944 location. Otherwise, the relocation should move within the
9948 if (is_operand_relocation (fix->src_type))
9950 /* Check if the original relocation is against a literal being
9952 removed = find_removed_literal (&relax_info->removed_list,
9960 /* The fact that there is still a relocation to this literal indicates
9961 that the literal is being coalesced, not simply removed. */
9962 BFD_ASSERT (removed->to.abfd != NULL);
9964 /* This was moved to some other address (possibly another section). */
9965 new_sec = r_reloc_get_section (&removed->to);
9969 relax_info = get_xtensa_relax_info (sec);
9971 (!relax_info->is_relaxable_literal_section
9972 && !relax_info->is_relaxable_asm_section))
9974 target_offset = removed->to.target_offset;
9975 new_fix.target_sec = new_sec;
9976 new_fix.target_offset = target_offset;
9977 new_fix.translated = TRUE;
9982 target_offset = removed->to.target_offset;
9983 new_fix.target_sec = new_sec;
9986 /* The target address may have been moved within its section. */
9987 new_offset = offset_with_removed_text (&relax_info->action_list,
9990 new_fix.target_offset = new_offset;
9991 new_fix.target_offset = new_offset;
9992 new_fix.translated = TRUE;
9998 /* Fix up a relocation to take account of removed literals. */
10001 translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
10003 xtensa_relax_info *relax_info;
10004 removed_literal *removed;
10005 bfd_vma target_offset, base_offset;
10007 *new_rel = *orig_rel;
10009 if (!r_reloc_is_defined (orig_rel))
10012 relax_info = get_xtensa_relax_info (sec);
10013 BFD_ASSERT (relax_info && (relax_info->is_relaxable_literal_section
10014 || relax_info->is_relaxable_asm_section));
10016 target_offset = orig_rel->target_offset;
10019 if (is_operand_relocation (ELF32_R_TYPE (orig_rel->rela.r_info)))
10021 /* Check if the original relocation is against a literal being
10023 removed = find_removed_literal (&relax_info->removed_list,
10026 if (removed && removed->to.abfd)
10030 /* The fact that there is still a relocation to this literal indicates
10031 that the literal is being coalesced, not simply removed. */
10032 BFD_ASSERT (removed->to.abfd != NULL);
10034 /* This was moved to some other address
10035 (possibly in another section). */
10036 *new_rel = removed->to;
10037 new_sec = r_reloc_get_section (new_rel);
10038 if (new_sec != sec)
10041 relax_info = get_xtensa_relax_info (sec);
10043 || (!relax_info->is_relaxable_literal_section
10044 && !relax_info->is_relaxable_asm_section))
10047 target_offset = new_rel->target_offset;
10050 /* Find the base offset of the reloc symbol, excluding any addend from the
10051 reloc or from the section contents (for a partial_inplace reloc). Then
10052 find the adjusted values of the offsets due to relaxation. The base
10053 offset is needed to determine the change to the reloc's addend; the reloc
10054 addend should not be adjusted due to relaxations located before the base
10057 base_offset = r_reloc_get_target_offset (new_rel) - new_rel->rela.r_addend;
10058 if (base_offset <= target_offset)
10060 int base_removed = removed_by_actions_map (&relax_info->action_list,
10061 base_offset, FALSE);
10062 int addend_removed = removed_by_actions_map (&relax_info->action_list,
10063 target_offset, FALSE) -
10066 new_rel->target_offset = target_offset - base_removed - addend_removed;
10067 new_rel->rela.r_addend -= addend_removed;
10071 /* Handle a negative addend. The base offset comes first. */
10072 int tgt_removed = removed_by_actions_map (&relax_info->action_list,
10073 target_offset, FALSE);
10074 int addend_removed = removed_by_actions_map (&relax_info->action_list,
10075 base_offset, FALSE) -
10078 new_rel->target_offset = target_offset - tgt_removed;
10079 new_rel->rela.r_addend += addend_removed;
10086 /* For dynamic links, there may be a dynamic relocation for each
10087 literal. The number of dynamic relocations must be computed in
10088 size_dynamic_sections, which occurs before relaxation. When a
10089 literal is removed, this function checks if there is a corresponding
10090 dynamic relocation and shrinks the size of the appropriate dynamic
10091 relocation section accordingly. At this point, the contents of the
10092 dynamic relocation sections have not yet been filled in, so there's
10093 nothing else that needs to be done. */
10096 shrink_dynamic_reloc_sections (struct bfd_link_info *info,
10098 asection *input_section,
10099 Elf_Internal_Rela *rel)
10101 struct elf_xtensa_link_hash_table *htab;
10102 Elf_Internal_Shdr *symtab_hdr;
10103 struct elf_link_hash_entry **sym_hashes;
10104 unsigned long r_symndx;
10106 struct elf_link_hash_entry *h;
10107 bfd_boolean dynamic_symbol;
10109 htab = elf_xtensa_hash_table (info);
10113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10114 sym_hashes = elf_sym_hashes (abfd);
10116 r_type = ELF32_R_TYPE (rel->r_info);
10117 r_symndx = ELF32_R_SYM (rel->r_info);
10119 if (r_symndx < symtab_hdr->sh_info)
10122 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
10124 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
10126 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
10127 && (input_section->flags & SEC_ALLOC) != 0
10128 && (dynamic_symbol || bfd_link_pic (info)))
10131 bfd_boolean is_plt = FALSE;
10133 if (dynamic_symbol && r_type == R_XTENSA_PLT)
10135 srel = htab->srelplt;
10139 srel = htab->srelgot;
10141 /* Reduce size of the .rela.* section by one reloc. */
10142 BFD_ASSERT (srel != NULL);
10143 BFD_ASSERT (srel->size >= sizeof (Elf32_External_Rela));
10144 srel->size -= sizeof (Elf32_External_Rela);
10148 asection *splt, *sgotplt, *srelgot;
10149 int reloc_index, chunk;
10151 /* Find the PLT reloc index of the entry being removed. This
10152 is computed from the size of ".rela.plt". It is needed to
10153 figure out which PLT chunk to resize. Usually "last index
10154 = size - 1" since the index starts at zero, but in this
10155 context, the size has just been decremented so there's no
10156 need to subtract one. */
10157 reloc_index = srel->size / sizeof (Elf32_External_Rela);
10159 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
10160 splt = elf_xtensa_get_plt_section (info, chunk);
10161 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
10162 BFD_ASSERT (splt != NULL && sgotplt != NULL);
10164 /* Check if an entire PLT chunk has just been eliminated. */
10165 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
10167 /* The two magic GOT entries for that chunk can go away. */
10168 srelgot = htab->srelgot;
10169 BFD_ASSERT (srelgot != NULL);
10170 srelgot->reloc_count -= 2;
10171 srelgot->size -= 2 * sizeof (Elf32_External_Rela);
10172 sgotplt->size -= 8;
10174 /* There should be only one entry left (and it will be
10176 BFD_ASSERT (sgotplt->size == 4);
10177 BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
10180 BFD_ASSERT (sgotplt->size >= 4);
10181 BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
10183 sgotplt->size -= 4;
10184 splt->size -= PLT_ENTRY_SIZE;
10190 /* Take an r_rel and move it to another section. This usually
10191 requires extending the interal_relocation array and pinning it. If
10192 the original r_rel is from the same BFD, we can complete this here.
10193 Otherwise, we add a fix record to let the final link fix the
10194 appropriate address. Contents and internal relocations for the
10195 section must be pinned after calling this routine. */
10198 move_literal (bfd *abfd,
10199 struct bfd_link_info *link_info,
10202 bfd_byte *contents,
10203 xtensa_relax_info *relax_info,
10204 Elf_Internal_Rela **internal_relocs_p,
10205 const literal_value *lit)
10207 Elf_Internal_Rela *new_relocs = NULL;
10208 size_t new_relocs_count = 0;
10209 Elf_Internal_Rela this_rela;
10210 const r_reloc *r_rel;
10212 r_rel = &lit->r_rel;
10213 BFD_ASSERT (elf_section_data (sec)->relocs == *internal_relocs_p);
10215 if (r_reloc_is_const (r_rel))
10216 bfd_put_32 (abfd, lit->value, contents + offset);
10221 reloc_bfd_fix *fix;
10222 unsigned insert_at;
10224 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
10226 /* This is the difficult case. We have to create a fix up. */
10227 this_rela.r_offset = offset;
10228 this_rela.r_info = ELF32_R_INFO (0, r_type);
10229 this_rela.r_addend =
10230 r_rel->target_offset - r_reloc_get_target_offset (r_rel);
10231 bfd_put_32 (abfd, lit->value, contents + offset);
10233 /* Currently, we cannot move relocations during a relocatable link. */
10234 BFD_ASSERT (!bfd_link_relocatable (link_info));
10235 fix = reloc_bfd_fix_init (sec, offset, r_type,
10236 r_reloc_get_section (r_rel),
10237 r_rel->target_offset + r_rel->virtual_offset,
10239 /* We also need to mark that relocations are needed here. */
10240 sec->flags |= SEC_RELOC;
10242 translate_reloc_bfd_fix (fix);
10243 /* This fix has not yet been translated. */
10244 add_fix (sec, fix);
10246 /* Add the relocation. If we have already allocated our own
10247 space for the relocations and we have room for more, then use
10248 it. Otherwise, allocate new space and move the literals. */
10249 insert_at = sec->reloc_count;
10250 for (i = 0; i < sec->reloc_count; ++i)
10252 if (this_rela.r_offset < (*internal_relocs_p)[i].r_offset)
10259 if (*internal_relocs_p != relax_info->allocated_relocs
10260 || sec->reloc_count + 1 > relax_info->allocated_relocs_count)
10262 BFD_ASSERT (relax_info->allocated_relocs == NULL
10263 || sec->reloc_count == relax_info->relocs_count);
10265 if (relax_info->allocated_relocs_count == 0)
10266 new_relocs_count = (sec->reloc_count + 2) * 2;
10268 new_relocs_count = (relax_info->allocated_relocs_count + 2) * 2;
10270 new_relocs = (Elf_Internal_Rela *)
10271 bfd_zmalloc (sizeof (Elf_Internal_Rela) * (new_relocs_count));
10275 /* We could handle this more quickly by finding the split point. */
10276 if (insert_at != 0)
10277 memcpy (new_relocs, *internal_relocs_p,
10278 insert_at * sizeof (Elf_Internal_Rela));
10280 new_relocs[insert_at] = this_rela;
10282 if (insert_at != sec->reloc_count)
10283 memcpy (new_relocs + insert_at + 1,
10284 (*internal_relocs_p) + insert_at,
10285 (sec->reloc_count - insert_at)
10286 * sizeof (Elf_Internal_Rela));
10288 if (*internal_relocs_p != relax_info->allocated_relocs)
10290 /* The first time we re-allocate, we can only free the
10291 old relocs if they were allocated with bfd_malloc.
10292 This is not true when keep_memory is in effect. */
10293 if (!link_info->keep_memory)
10294 free (*internal_relocs_p);
10297 free (*internal_relocs_p);
10298 relax_info->allocated_relocs = new_relocs;
10299 relax_info->allocated_relocs_count = new_relocs_count;
10300 elf_section_data (sec)->relocs = new_relocs;
10301 sec->reloc_count++;
10302 relax_info->relocs_count = sec->reloc_count;
10303 *internal_relocs_p = new_relocs;
10307 if (insert_at != sec->reloc_count)
10310 for (idx = sec->reloc_count; idx > insert_at; idx--)
10311 (*internal_relocs_p)[idx] = (*internal_relocs_p)[idx-1];
10313 (*internal_relocs_p)[insert_at] = this_rela;
10314 sec->reloc_count++;
10315 if (relax_info->allocated_relocs)
10316 relax_info->relocs_count = sec->reloc_count;
10323 /* This is similar to relax_section except that when a target is moved,
10324 we shift addresses up. We also need to modify the size. This
10325 algorithm does NOT allow for relocations into the middle of the
10326 property sections. */
10329 relax_property_section (bfd *abfd,
10331 struct bfd_link_info *link_info)
10333 Elf_Internal_Rela *internal_relocs;
10334 bfd_byte *contents;
10336 bfd_boolean ok = TRUE;
10337 bfd_boolean is_full_prop_section;
10338 size_t last_zfill_target_offset = 0;
10339 asection *last_zfill_target_sec = NULL;
10340 bfd_size_type sec_size;
10341 bfd_size_type entry_size;
10343 sec_size = bfd_get_section_limit (abfd, sec);
10344 internal_relocs = retrieve_internal_relocs (abfd, sec,
10345 link_info->keep_memory);
10346 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
10347 if (contents == NULL && sec_size != 0)
10353 is_full_prop_section = xtensa_is_proptable_section (sec);
10354 if (is_full_prop_section)
10359 if (internal_relocs)
10361 for (i = 0; i < sec->reloc_count; i++)
10363 Elf_Internal_Rela *irel;
10364 xtensa_relax_info *target_relax_info;
10366 asection *target_sec;
10368 bfd_byte *size_p, *flags_p;
10370 /* Locally change the source address.
10371 Translate the target to the new target address.
10372 If it points to this section and has been removed, MOVE IT.
10373 Also, don't forget to modify the associated SIZE at
10376 irel = &internal_relocs[i];
10377 r_type = ELF32_R_TYPE (irel->r_info);
10378 if (r_type == R_XTENSA_NONE)
10381 /* Find the literal value. */
10382 r_reloc_init (&val.r_rel, abfd, irel, contents, sec_size);
10383 size_p = &contents[irel->r_offset + 4];
10385 if (is_full_prop_section)
10386 flags_p = &contents[irel->r_offset + 8];
10387 BFD_ASSERT (irel->r_offset + entry_size <= sec_size);
10389 target_sec = r_reloc_get_section (&val.r_rel);
10390 target_relax_info = get_xtensa_relax_info (target_sec);
10392 if (target_relax_info
10393 && (target_relax_info->is_relaxable_literal_section
10394 || target_relax_info->is_relaxable_asm_section ))
10396 /* Translate the relocation's destination. */
10397 bfd_vma old_offset = val.r_rel.target_offset;
10398 bfd_vma new_offset;
10399 long old_size, new_size;
10400 int removed_by_old_offset =
10401 removed_by_actions_map (&target_relax_info->action_list,
10402 old_offset, FALSE);
10403 new_offset = old_offset - removed_by_old_offset;
10405 /* Assert that we are not out of bounds. */
10406 old_size = bfd_get_32 (abfd, size_p);
10407 new_size = old_size;
10411 /* Only the first zero-sized unreachable entry is
10412 allowed to expand. In this case the new offset
10413 should be the offset before the fill and the new
10414 size is the expansion size. For other zero-sized
10415 entries the resulting size should be zero with an
10416 offset before or after the fill address depending
10417 on whether the expanding unreachable entry
10419 if (last_zfill_target_sec == 0
10420 || last_zfill_target_sec != target_sec
10421 || last_zfill_target_offset != old_offset)
10423 bfd_vma new_end_offset = new_offset;
10425 /* Recompute the new_offset, but this time don't
10426 include any fill inserted by relaxation. */
10427 removed_by_old_offset =
10428 removed_by_actions_map (&target_relax_info->action_list,
10430 new_offset = old_offset - removed_by_old_offset;
10432 /* If it is not unreachable and we have not yet
10433 seen an unreachable at this address, place it
10434 before the fill address. */
10435 if (flags_p && (bfd_get_32 (abfd, flags_p)
10436 & XTENSA_PROP_UNREACHABLE) != 0)
10438 new_size = new_end_offset - new_offset;
10440 last_zfill_target_sec = target_sec;
10441 last_zfill_target_offset = old_offset;
10447 int removed_by_old_offset_size =
10448 removed_by_actions_map (&target_relax_info->action_list,
10449 old_offset + old_size, TRUE);
10450 new_size -= removed_by_old_offset_size - removed_by_old_offset;
10453 if (new_size != old_size)
10455 bfd_put_32 (abfd, new_size, size_p);
10456 pin_contents (sec, contents);
10459 if (new_offset != old_offset)
10461 bfd_vma diff = new_offset - old_offset;
10462 irel->r_addend += diff;
10463 pin_internal_relocs (sec, internal_relocs);
10469 /* Combine adjacent property table entries. This is also done in
10470 finish_dynamic_sections() but at that point it's too late to
10471 reclaim the space in the output section, so we do this twice. */
10473 if (internal_relocs && (!bfd_link_relocatable (link_info)
10474 || xtensa_is_littable_section (sec)))
10476 Elf_Internal_Rela *last_irel = NULL;
10477 Elf_Internal_Rela *irel, *next_rel, *rel_end;
10478 int removed_bytes = 0;
10480 flagword predef_flags;
10482 predef_flags = xtensa_get_property_predef_flags (sec);
10484 /* Walk over memory and relocations at the same time.
10485 This REQUIRES that the internal_relocs be sorted by offset. */
10486 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
10487 internal_reloc_compare);
10489 pin_internal_relocs (sec, internal_relocs);
10490 pin_contents (sec, contents);
10492 next_rel = internal_relocs;
10493 rel_end = internal_relocs + sec->reloc_count;
10495 BFD_ASSERT (sec->size % entry_size == 0);
10497 for (offset = 0; offset < sec->size; offset += entry_size)
10499 Elf_Internal_Rela *offset_rel, *extra_rel;
10500 bfd_vma bytes_to_remove, size, actual_offset;
10501 bfd_boolean remove_this_rel;
10504 /* Find the first relocation for the entry at the current offset.
10505 Adjust the offsets of any extra relocations for the previous
10510 for (irel = next_rel; irel < rel_end; irel++)
10512 if ((irel->r_offset == offset
10513 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10514 || irel->r_offset > offset)
10519 irel->r_offset -= removed_bytes;
10523 /* Find the next relocation (if there are any left). */
10527 for (irel = offset_rel + 1; irel < rel_end; irel++)
10529 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
10537 /* Check if there are relocations on the current entry. There
10538 should usually be a relocation on the offset field. If there
10539 are relocations on the size or flags, then we can't optimize
10540 this entry. Also, find the next relocation to examine on the
10544 if (offset_rel->r_offset >= offset + entry_size)
10546 next_rel = offset_rel;
10547 /* There are no relocations on the current entry, but we
10548 might still be able to remove it if the size is zero. */
10551 else if (offset_rel->r_offset > offset
10553 && extra_rel->r_offset < offset + entry_size))
10555 /* There is a relocation on the size or flags, so we can't
10556 do anything with this entry. Continue with the next. */
10557 next_rel = offset_rel;
10562 BFD_ASSERT (offset_rel->r_offset == offset);
10563 offset_rel->r_offset -= removed_bytes;
10564 next_rel = offset_rel + 1;
10570 remove_this_rel = FALSE;
10571 bytes_to_remove = 0;
10572 actual_offset = offset - removed_bytes;
10573 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
10575 if (is_full_prop_section)
10576 flags = bfd_get_32 (abfd, &contents[actual_offset + 8]);
10578 flags = predef_flags;
10581 && (flags & XTENSA_PROP_ALIGN) == 0
10582 && (flags & XTENSA_PROP_UNREACHABLE) == 0)
10584 /* Always remove entries with zero size and no alignment. */
10585 bytes_to_remove = entry_size;
10587 remove_this_rel = TRUE;
10589 else if (offset_rel
10590 && ELF32_R_TYPE (offset_rel->r_info) == R_XTENSA_32)
10594 flagword old_flags;
10596 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
10597 bfd_vma old_address =
10598 (last_irel->r_addend
10599 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
10600 bfd_vma new_address =
10601 (offset_rel->r_addend
10602 + bfd_get_32 (abfd, &contents[actual_offset]));
10603 if (is_full_prop_section)
10604 old_flags = bfd_get_32
10605 (abfd, &contents[last_irel->r_offset + 8]);
10607 old_flags = predef_flags;
10609 if ((ELF32_R_SYM (offset_rel->r_info)
10610 == ELF32_R_SYM (last_irel->r_info))
10611 && old_address + old_size == new_address
10612 && old_flags == flags
10613 && (old_flags & XTENSA_PROP_INSN_BRANCH_TARGET) == 0
10614 && (old_flags & XTENSA_PROP_INSN_LOOP_TARGET) == 0)
10616 /* Fix the old size. */
10617 bfd_put_32 (abfd, old_size + size,
10618 &contents[last_irel->r_offset + 4]);
10619 bytes_to_remove = entry_size;
10620 remove_this_rel = TRUE;
10623 last_irel = offset_rel;
10626 last_irel = offset_rel;
10629 if (remove_this_rel)
10631 offset_rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
10632 offset_rel->r_offset = 0;
10635 if (bytes_to_remove != 0)
10637 removed_bytes += bytes_to_remove;
10638 if (offset + bytes_to_remove < sec->size)
10639 memmove (&contents[actual_offset],
10640 &contents[actual_offset + bytes_to_remove],
10641 sec->size - offset - bytes_to_remove);
10647 /* Fix up any extra relocations on the last entry. */
10648 for (irel = next_rel; irel < rel_end; irel++)
10649 irel->r_offset -= removed_bytes;
10651 /* Clear the removed bytes. */
10652 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
10654 if (sec->rawsize == 0)
10655 sec->rawsize = sec->size;
10656 sec->size -= removed_bytes;
10658 if (xtensa_is_littable_section (sec))
10660 asection *sgotloc = elf_xtensa_hash_table (link_info)->sgotloc;
10662 sgotloc->size -= removed_bytes;
10668 release_internal_relocs (sec, internal_relocs);
10669 release_contents (sec, contents);
10674 /* Third relaxation pass. */
10676 /* Change symbol values to account for removed literals. */
10679 relax_section_symbols (bfd *abfd, asection *sec)
10681 xtensa_relax_info *relax_info;
10682 unsigned int sec_shndx;
10683 Elf_Internal_Shdr *symtab_hdr;
10684 Elf_Internal_Sym *isymbuf;
10685 unsigned i, num_syms, num_locals;
10687 relax_info = get_xtensa_relax_info (sec);
10688 BFD_ASSERT (relax_info);
10690 if (!relax_info->is_relaxable_literal_section
10691 && !relax_info->is_relaxable_asm_section)
10694 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
10696 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10697 isymbuf = retrieve_local_syms (abfd);
10699 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
10700 num_locals = symtab_hdr->sh_info;
10702 /* Adjust the local symbols defined in this section. */
10703 for (i = 0; i < num_locals; i++)
10705 Elf_Internal_Sym *isym = &isymbuf[i];
10707 if (isym->st_shndx == sec_shndx)
10709 bfd_vma orig_addr = isym->st_value;
10710 int removed = removed_by_actions_map (&relax_info->action_list,
10713 isym->st_value -= removed;
10714 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
10716 removed_by_actions_map (&relax_info->action_list,
10717 orig_addr + isym->st_size, FALSE) -
10722 /* Now adjust the global symbols defined in this section. */
10723 for (i = 0; i < (num_syms - num_locals); i++)
10725 struct elf_link_hash_entry *sym_hash;
10727 sym_hash = elf_sym_hashes (abfd)[i];
10729 if (sym_hash->root.type == bfd_link_hash_warning)
10730 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
10732 if ((sym_hash->root.type == bfd_link_hash_defined
10733 || sym_hash->root.type == bfd_link_hash_defweak)
10734 && sym_hash->root.u.def.section == sec)
10736 bfd_vma orig_addr = sym_hash->root.u.def.value;
10737 int removed = removed_by_actions_map (&relax_info->action_list,
10740 sym_hash->root.u.def.value -= removed;
10742 if (sym_hash->type == STT_FUNC)
10744 removed_by_actions_map (&relax_info->action_list,
10745 orig_addr + sym_hash->size, FALSE) -
10754 /* "Fix" handling functions, called while performing relocations. */
10757 do_fix_for_relocatable_link (Elf_Internal_Rela *rel,
10759 asection *input_section,
10760 bfd_byte *contents)
10763 asection *sec, *old_sec;
10764 bfd_vma old_offset;
10765 int r_type = ELF32_R_TYPE (rel->r_info);
10766 reloc_bfd_fix *fix;
10768 if (r_type == R_XTENSA_NONE)
10771 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10775 r_reloc_init (&r_rel, input_bfd, rel, contents,
10776 bfd_get_section_limit (input_bfd, input_section));
10777 old_sec = r_reloc_get_section (&r_rel);
10778 old_offset = r_rel.target_offset;
10780 if (!old_sec || !r_reloc_is_defined (&r_rel))
10782 if (r_type != R_XTENSA_ASM_EXPAND)
10785 /* xgettext:c-format */
10786 (_("%B(%A+0x%lx): unexpected fix for %s relocation"),
10787 input_bfd, input_section, rel->r_offset,
10788 elf_howto_table[r_type].name);
10791 /* Leave it be. Resolution will happen in a later stage. */
10795 sec = fix->target_sec;
10796 rel->r_addend += ((sec->output_offset + fix->target_offset)
10797 - (old_sec->output_offset + old_offset));
10804 do_fix_for_final_link (Elf_Internal_Rela *rel,
10806 asection *input_section,
10807 bfd_byte *contents,
10808 bfd_vma *relocationp)
10811 int r_type = ELF32_R_TYPE (rel->r_info);
10812 reloc_bfd_fix *fix;
10813 bfd_vma fixup_diff;
10815 if (r_type == R_XTENSA_NONE)
10818 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10822 sec = fix->target_sec;
10824 fixup_diff = rel->r_addend;
10825 if (elf_howto_table[fix->src_type].partial_inplace)
10827 bfd_vma inplace_val;
10828 BFD_ASSERT (fix->src_offset
10829 < bfd_get_section_limit (input_bfd, input_section));
10830 inplace_val = bfd_get_32 (input_bfd, &contents[fix->src_offset]);
10831 fixup_diff += inplace_val;
10834 *relocationp = (sec->output_section->vma
10835 + sec->output_offset
10836 + fix->target_offset - fixup_diff);
10840 /* Miscellaneous utility functions.... */
10843 elf_xtensa_get_plt_section (struct bfd_link_info *info, int chunk)
10845 struct elf_xtensa_link_hash_table *htab;
10851 htab = elf_xtensa_hash_table (info);
10858 dynobj = elf_hash_table (info)->dynobj;
10859 sprintf (plt_name, ".plt.%u", chunk);
10860 return bfd_get_linker_section (dynobj, plt_name);
10865 elf_xtensa_get_gotplt_section (struct bfd_link_info *info, int chunk)
10867 struct elf_xtensa_link_hash_table *htab;
10873 htab = elf_xtensa_hash_table (info);
10876 return htab->sgotplt;
10879 dynobj = elf_hash_table (info)->dynobj;
10880 sprintf (got_name, ".got.plt.%u", chunk);
10881 return bfd_get_linker_section (dynobj, got_name);
10885 /* Get the input section for a given symbol index.
10887 . a section symbol, return the section;
10888 . a common symbol, return the common section;
10889 . an undefined symbol, return the undefined section;
10890 . an indirect symbol, follow the links;
10891 . an absolute value, return the absolute section. */
10894 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
10896 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10897 asection *target_sec = NULL;
10898 if (r_symndx < symtab_hdr->sh_info)
10900 Elf_Internal_Sym *isymbuf;
10901 unsigned int section_index;
10903 isymbuf = retrieve_local_syms (abfd);
10904 section_index = isymbuf[r_symndx].st_shndx;
10906 if (section_index == SHN_UNDEF)
10907 target_sec = bfd_und_section_ptr;
10908 else if (section_index == SHN_ABS)
10909 target_sec = bfd_abs_section_ptr;
10910 else if (section_index == SHN_COMMON)
10911 target_sec = bfd_com_section_ptr;
10913 target_sec = bfd_section_from_elf_index (abfd, section_index);
10917 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10918 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
10920 while (h->root.type == bfd_link_hash_indirect
10921 || h->root.type == bfd_link_hash_warning)
10922 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10924 switch (h->root.type)
10926 case bfd_link_hash_defined:
10927 case bfd_link_hash_defweak:
10928 target_sec = h->root.u.def.section;
10930 case bfd_link_hash_common:
10931 target_sec = bfd_com_section_ptr;
10933 case bfd_link_hash_undefined:
10934 case bfd_link_hash_undefweak:
10935 target_sec = bfd_und_section_ptr;
10937 default: /* New indirect warning. */
10938 target_sec = bfd_und_section_ptr;
10946 static struct elf_link_hash_entry *
10947 get_elf_r_symndx_hash_entry (bfd *abfd, unsigned long r_symndx)
10949 unsigned long indx;
10950 struct elf_link_hash_entry *h;
10951 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10953 if (r_symndx < symtab_hdr->sh_info)
10956 indx = r_symndx - symtab_hdr->sh_info;
10957 h = elf_sym_hashes (abfd)[indx];
10958 while (h->root.type == bfd_link_hash_indirect
10959 || h->root.type == bfd_link_hash_warning)
10960 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10965 /* Get the section-relative offset for a symbol number. */
10968 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
10970 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10971 bfd_vma offset = 0;
10973 if (r_symndx < symtab_hdr->sh_info)
10975 Elf_Internal_Sym *isymbuf;
10976 isymbuf = retrieve_local_syms (abfd);
10977 offset = isymbuf[r_symndx].st_value;
10981 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10982 struct elf_link_hash_entry *h =
10983 elf_sym_hashes (abfd)[indx];
10985 while (h->root.type == bfd_link_hash_indirect
10986 || h->root.type == bfd_link_hash_warning)
10987 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10988 if (h->root.type == bfd_link_hash_defined
10989 || h->root.type == bfd_link_hash_defweak)
10990 offset = h->root.u.def.value;
10997 is_reloc_sym_weak (bfd *abfd, Elf_Internal_Rela *rel)
10999 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
11000 struct elf_link_hash_entry *h;
11002 h = get_elf_r_symndx_hash_entry (abfd, r_symndx);
11003 if (h && h->root.type == bfd_link_hash_defweak)
11010 pcrel_reloc_fits (xtensa_opcode opc,
11012 bfd_vma self_address,
11013 bfd_vma dest_address)
11015 xtensa_isa isa = xtensa_default_isa;
11016 uint32 valp = dest_address;
11017 if (xtensa_operand_do_reloc (isa, opc, opnd, &valp, self_address)
11018 || xtensa_operand_encode (isa, opc, opnd, &valp))
11025 xtensa_is_property_section (asection *sec)
11027 if (xtensa_is_insntable_section (sec)
11028 || xtensa_is_littable_section (sec)
11029 || xtensa_is_proptable_section (sec))
11037 xtensa_is_insntable_section (asection *sec)
11039 if (CONST_STRNEQ (sec->name, XTENSA_INSN_SEC_NAME)
11040 || CONST_STRNEQ (sec->name, ".gnu.linkonce.x."))
11048 xtensa_is_littable_section (asection *sec)
11050 if (CONST_STRNEQ (sec->name, XTENSA_LIT_SEC_NAME)
11051 || CONST_STRNEQ (sec->name, ".gnu.linkonce.p."))
11059 xtensa_is_proptable_section (asection *sec)
11061 if (CONST_STRNEQ (sec->name, XTENSA_PROP_SEC_NAME)
11062 || CONST_STRNEQ (sec->name, ".gnu.linkonce.prop."))
11070 internal_reloc_compare (const void *ap, const void *bp)
11072 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11073 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11075 if (a->r_offset != b->r_offset)
11076 return (a->r_offset - b->r_offset);
11078 /* We don't need to sort on these criteria for correctness,
11079 but enforcing a more strict ordering prevents unstable qsort
11080 from behaving differently with different implementations.
11081 Without the code below we get correct but different results
11082 on Solaris 2.7 and 2.8. We would like to always produce the
11083 same results no matter the host. */
11085 if (a->r_info != b->r_info)
11086 return (a->r_info - b->r_info);
11088 return (a->r_addend - b->r_addend);
11093 internal_reloc_matches (const void *ap, const void *bp)
11095 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
11096 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
11098 /* Check if one entry overlaps with the other; this shouldn't happen
11099 except when searching for a match. */
11100 return (a->r_offset - b->r_offset);
11104 /* Predicate function used to look up a section in a particular group. */
11107 match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
11109 const char *gname = inf;
11110 const char *group_name = elf_group_name (sec);
11112 return (group_name == gname
11113 || (group_name != NULL
11115 && strcmp (group_name, gname) == 0));
11119 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
11122 xtensa_property_section_name (asection *sec, const char *base_name)
11124 const char *suffix, *group_name;
11125 char *prop_sec_name;
11127 group_name = elf_group_name (sec);
11130 suffix = strrchr (sec->name, '.');
11131 if (suffix == sec->name)
11133 prop_sec_name = (char *) bfd_malloc (strlen (base_name) + 1
11134 + (suffix ? strlen (suffix) : 0));
11135 strcpy (prop_sec_name, base_name);
11137 strcat (prop_sec_name, suffix);
11139 else if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
11141 char *linkonce_kind = 0;
11143 if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
11144 linkonce_kind = "x.";
11145 else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
11146 linkonce_kind = "p.";
11147 else if (strcmp (base_name, XTENSA_PROP_SEC_NAME) == 0)
11148 linkonce_kind = "prop.";
11152 prop_sec_name = (char *) bfd_malloc (strlen (sec->name)
11153 + strlen (linkonce_kind) + 1);
11154 memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
11155 strcpy (prop_sec_name + linkonce_len, linkonce_kind);
11157 suffix = sec->name + linkonce_len;
11158 /* For backward compatibility, replace "t." instead of inserting
11159 the new linkonce_kind (but not for "prop" sections). */
11160 if (CONST_STRNEQ (suffix, "t.") && linkonce_kind[1] == '.')
11162 strcat (prop_sec_name + linkonce_len, suffix);
11165 prop_sec_name = strdup (base_name);
11167 return prop_sec_name;
11172 xtensa_get_property_section (asection *sec, const char *base_name)
11174 char *prop_sec_name;
11175 asection *prop_sec;
11177 prop_sec_name = xtensa_property_section_name (sec, base_name);
11178 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
11179 match_section_group,
11180 (void *) elf_group_name (sec));
11181 free (prop_sec_name);
11187 xtensa_make_property_section (asection *sec, const char *base_name)
11189 char *prop_sec_name;
11190 asection *prop_sec;
11192 /* Check if the section already exists. */
11193 prop_sec_name = xtensa_property_section_name (sec, base_name);
11194 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
11195 match_section_group,
11196 (void *) elf_group_name (sec));
11197 /* If not, create it. */
11200 flagword flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY);
11201 flags |= (bfd_get_section_flags (sec->owner, sec)
11202 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES));
11204 prop_sec = bfd_make_section_anyway_with_flags
11205 (sec->owner, strdup (prop_sec_name), flags);
11209 elf_group_name (prop_sec) = elf_group_name (sec);
11212 free (prop_sec_name);
11218 xtensa_get_property_predef_flags (asection *sec)
11220 if (xtensa_is_insntable_section (sec))
11221 return (XTENSA_PROP_INSN
11222 | XTENSA_PROP_NO_TRANSFORM
11223 | XTENSA_PROP_INSN_NO_REORDER);
11225 if (xtensa_is_littable_section (sec))
11226 return (XTENSA_PROP_LITERAL
11227 | XTENSA_PROP_NO_TRANSFORM
11228 | XTENSA_PROP_INSN_NO_REORDER);
11234 /* Other functions called directly by the linker. */
11237 xtensa_callback_required_dependence (bfd *abfd,
11239 struct bfd_link_info *link_info,
11240 deps_callback_t callback,
11243 Elf_Internal_Rela *internal_relocs;
11244 bfd_byte *contents;
11246 bfd_boolean ok = TRUE;
11247 bfd_size_type sec_size;
11249 sec_size = bfd_get_section_limit (abfd, sec);
11251 /* ".plt*" sections have no explicit relocations but they contain L32R
11252 instructions that reference the corresponding ".got.plt*" sections. */
11253 if ((sec->flags & SEC_LINKER_CREATED) != 0
11254 && CONST_STRNEQ (sec->name, ".plt"))
11258 /* Find the corresponding ".got.plt*" section. */
11259 if (sec->name[4] == '\0')
11260 sgotplt = bfd_get_linker_section (sec->owner, ".got.plt");
11266 BFD_ASSERT (sec->name[4] == '.');
11267 chunk = strtol (&sec->name[5], NULL, 10);
11269 sprintf (got_name, ".got.plt.%u", chunk);
11270 sgotplt = bfd_get_linker_section (sec->owner, got_name);
11272 BFD_ASSERT (sgotplt);
11274 /* Assume worst-case offsets: L32R at the very end of the ".plt"
11275 section referencing a literal at the very beginning of
11276 ".got.plt". This is very close to the real dependence, anyway. */
11277 (*callback) (sec, sec_size, sgotplt, 0, closure);
11280 /* Only ELF files are supported for Xtensa. Check here to avoid a segfault
11281 when building uclibc, which runs "ld -b binary /dev/null". */
11282 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11285 internal_relocs = retrieve_internal_relocs (abfd, sec,
11286 link_info->keep_memory);
11287 if (internal_relocs == NULL
11288 || sec->reloc_count == 0)
11291 /* Cache the contents for the duration of this scan. */
11292 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
11293 if (contents == NULL && sec_size != 0)
11299 if (!xtensa_default_isa)
11300 xtensa_default_isa = xtensa_isa_init (0, 0);
11302 for (i = 0; i < sec->reloc_count; i++)
11304 Elf_Internal_Rela *irel = &internal_relocs[i];
11305 if (is_l32r_relocation (abfd, sec, contents, irel))
11308 asection *target_sec;
11309 bfd_vma target_offset;
11311 r_reloc_init (&l32r_rel, abfd, irel, contents, sec_size);
11314 /* L32Rs must be local to the input file. */
11315 if (r_reloc_is_defined (&l32r_rel))
11317 target_sec = r_reloc_get_section (&l32r_rel);
11318 target_offset = l32r_rel.target_offset;
11320 (*callback) (sec, irel->r_offset, target_sec, target_offset,
11326 release_internal_relocs (sec, internal_relocs);
11327 release_contents (sec, contents);
11331 /* The default literal sections should always be marked as "code" (i.e.,
11332 SHF_EXECINSTR). This is particularly important for the Linux kernel
11333 module loader so that the literals are not placed after the text. */
11334 static const struct bfd_elf_special_section elf_xtensa_special_sections[] =
11336 { STRING_COMMA_LEN (".fini.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11337 { STRING_COMMA_LEN (".init.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11338 { STRING_COMMA_LEN (".literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
11339 { STRING_COMMA_LEN (".xtensa.info"), 0, SHT_NOTE, 0 },
11340 { NULL, 0, 0, 0, 0 }
11343 #define ELF_TARGET_ID XTENSA_ELF_DATA
11345 #define TARGET_LITTLE_SYM xtensa_elf32_le_vec
11346 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
11347 #define TARGET_BIG_SYM xtensa_elf32_be_vec
11348 #define TARGET_BIG_NAME "elf32-xtensa-be"
11349 #define ELF_ARCH bfd_arch_xtensa
11351 #define ELF_MACHINE_CODE EM_XTENSA
11352 #define ELF_MACHINE_ALT1 EM_XTENSA_OLD
11355 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
11356 #else /* !XCHAL_HAVE_MMU */
11357 #define ELF_MAXPAGESIZE 1
11358 #endif /* !XCHAL_HAVE_MMU */
11359 #endif /* ELF_ARCH */
11361 #define elf_backend_can_gc_sections 1
11362 #define elf_backend_can_refcount 1
11363 #define elf_backend_plt_readonly 1
11364 #define elf_backend_got_header_size 4
11365 #define elf_backend_want_dynbss 0
11366 #define elf_backend_want_got_plt 1
11368 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
11370 #define bfd_elf32_mkobject elf_xtensa_mkobject
11372 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
11373 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
11374 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
11375 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
11376 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
11377 #define bfd_elf32_bfd_reloc_name_lookup \
11378 elf_xtensa_reloc_name_lookup
11379 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
11380 #define bfd_elf32_bfd_link_hash_table_create elf_xtensa_link_hash_table_create
11382 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
11383 #define elf_backend_check_relocs elf_xtensa_check_relocs
11384 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
11385 #define elf_backend_discard_info elf_xtensa_discard_info
11386 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
11387 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
11388 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
11389 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
11390 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
11391 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
11392 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
11393 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
11394 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
11395 #define elf_backend_object_p elf_xtensa_object_p
11396 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
11397 #define elf_backend_relocate_section elf_xtensa_relocate_section
11398 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
11399 #define elf_backend_always_size_sections elf_xtensa_always_size_sections
11400 #define elf_backend_omit_section_dynsym \
11401 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
11402 #define elf_backend_special_sections elf_xtensa_special_sections
11403 #define elf_backend_action_discarded elf_xtensa_action_discarded
11404 #define elf_backend_copy_indirect_symbol elf_xtensa_copy_indirect_symbol
11406 #include "elf32-target.h"