* read.c (cons_worker): Detect and reject unexpected string argument.
[binutils/dougsmingw.git] / bfd / elf32-xtensa.c
blob4d1b384f661184ef60df282f70e98e2f21295831
1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
25 #include <stdarg.h>
26 #include <strings.h>
28 #include "bfdlink.h"
29 #include "libbfd.h"
30 #include "elf-bfd.h"
31 #include "elf/xtensa.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 extern asection *xtensa_make_property_section (asection *, const char *);
114 static flagword xtensa_get_property_predef_flags (asection *);
116 /* Other functions called directly by the linker. */
118 typedef void (*deps_callback_t)
119 (asection *, bfd_vma, asection *, bfd_vma, void *);
120 extern bfd_boolean xtensa_callback_required_dependence
121 (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
124 /* Globally visible flag for choosing size optimization of NOP removal
125 instead of branch-target-aware minimization for NOP removal.
126 When nonzero, narrow all instructions and remove all NOPs possible
127 around longcall expansions. */
129 int elf32xtensa_size_opt;
132 /* The "new_section_hook" is used to set up a per-section
133 "xtensa_relax_info" data structure with additional information used
134 during relaxation. */
136 typedef struct xtensa_relax_info_struct xtensa_relax_info;
139 /* The GNU tools do not easily allow extending interfaces to pass around
140 the pointer to the Xtensa ISA information, so instead we add a global
141 variable here (in BFD) that can be used by any of the tools that need
142 this information. */
144 xtensa_isa xtensa_default_isa;
147 /* When this is true, relocations may have been modified to refer to
148 symbols from other input files. The per-section list of "fix"
149 records needs to be checked when resolving relocations. */
151 static bfd_boolean relaxing_section = FALSE;
153 /* When this is true, during final links, literals that cannot be
154 coalesced and their relocations may be moved to other sections. */
156 int elf32xtensa_no_literal_movement = 1;
158 /* Rename one of the generic section flags to better document how it
159 is used here. */
160 /* Whether relocations have been processed. */
161 #define reloc_done sec_flg0
163 static reloc_howto_type elf_howto_table[] =
165 HOWTO (R_XTENSA_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
166 bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
167 FALSE, 0, 0, FALSE),
168 HOWTO (R_XTENSA_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
169 bfd_elf_xtensa_reloc, "R_XTENSA_32",
170 TRUE, 0xffffffff, 0xffffffff, FALSE),
172 /* Replace a 32-bit value with a value from the runtime linker (only
173 used by linker-generated stub functions). The r_addend value is
174 special: 1 means to substitute a pointer to the runtime linker's
175 dynamic resolver function; 2 means to substitute the link map for
176 the shared object. */
177 HOWTO (R_XTENSA_RTLD, 0, 2, 32, FALSE, 0, complain_overflow_dont,
178 NULL, "R_XTENSA_RTLD", FALSE, 0, 0, FALSE),
180 HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
181 bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
182 FALSE, 0, 0xffffffff, FALSE),
183 HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
184 bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
185 FALSE, 0, 0xffffffff, FALSE),
186 HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
187 bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
188 FALSE, 0, 0xffffffff, FALSE),
189 HOWTO (R_XTENSA_PLT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
190 bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
191 FALSE, 0, 0xffffffff, FALSE),
193 EMPTY_HOWTO (7),
195 /* Old relocations for backward compatibility. */
196 HOWTO (R_XTENSA_OP0, 0, 0, 0, TRUE, 0, complain_overflow_dont,
197 bfd_elf_xtensa_reloc, "R_XTENSA_OP0", FALSE, 0, 0, TRUE),
198 HOWTO (R_XTENSA_OP1, 0, 0, 0, TRUE, 0, complain_overflow_dont,
199 bfd_elf_xtensa_reloc, "R_XTENSA_OP1", FALSE, 0, 0, TRUE),
200 HOWTO (R_XTENSA_OP2, 0, 0, 0, TRUE, 0, complain_overflow_dont,
201 bfd_elf_xtensa_reloc, "R_XTENSA_OP2", FALSE, 0, 0, TRUE),
203 /* Assembly auto-expansion. */
204 HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, TRUE, 0, complain_overflow_dont,
205 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND", FALSE, 0, 0, TRUE),
206 /* Relax assembly auto-expansion. */
207 HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, TRUE, 0, complain_overflow_dont,
208 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY", FALSE, 0, 0, TRUE),
210 EMPTY_HOWTO (13),
212 HOWTO (R_XTENSA_32_PCREL, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
213 bfd_elf_xtensa_reloc, "R_XTENSA_32_PCREL",
214 FALSE, 0, 0xffffffff, TRUE),
216 /* GNU extension to record C++ vtable hierarchy. */
217 HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, FALSE, 0, complain_overflow_dont,
218 NULL, "R_XTENSA_GNU_VTINHERIT",
219 FALSE, 0, 0, FALSE),
220 /* GNU extension to record C++ vtable member usage. */
221 HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, FALSE, 0, complain_overflow_dont,
222 _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
223 FALSE, 0, 0, FALSE),
225 /* Relocations for supporting difference of symbols. */
226 HOWTO (R_XTENSA_DIFF8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
227 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF8", FALSE, 0, 0xff, FALSE),
228 HOWTO (R_XTENSA_DIFF16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
229 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF16", FALSE, 0, 0xffff, FALSE),
230 HOWTO (R_XTENSA_DIFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
231 bfd_elf_xtensa_reloc, "R_XTENSA_DIFF32", FALSE, 0, 0xffffffff, FALSE),
233 /* General immediate operand relocations. */
234 HOWTO (R_XTENSA_SLOT0_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
235 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_OP", FALSE, 0, 0, TRUE),
236 HOWTO (R_XTENSA_SLOT1_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
237 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_OP", FALSE, 0, 0, TRUE),
238 HOWTO (R_XTENSA_SLOT2_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
239 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_OP", FALSE, 0, 0, TRUE),
240 HOWTO (R_XTENSA_SLOT3_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
241 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_OP", FALSE, 0, 0, TRUE),
242 HOWTO (R_XTENSA_SLOT4_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
243 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_OP", FALSE, 0, 0, TRUE),
244 HOWTO (R_XTENSA_SLOT5_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
245 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_OP", FALSE, 0, 0, TRUE),
246 HOWTO (R_XTENSA_SLOT6_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
247 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_OP", FALSE, 0, 0, TRUE),
248 HOWTO (R_XTENSA_SLOT7_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
249 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_OP", FALSE, 0, 0, TRUE),
250 HOWTO (R_XTENSA_SLOT8_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
251 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_OP", FALSE, 0, 0, TRUE),
252 HOWTO (R_XTENSA_SLOT9_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
253 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_OP", FALSE, 0, 0, TRUE),
254 HOWTO (R_XTENSA_SLOT10_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
255 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_OP", FALSE, 0, 0, TRUE),
256 HOWTO (R_XTENSA_SLOT11_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
257 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_OP", FALSE, 0, 0, TRUE),
258 HOWTO (R_XTENSA_SLOT12_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
259 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_OP", FALSE, 0, 0, TRUE),
260 HOWTO (R_XTENSA_SLOT13_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
261 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_OP", FALSE, 0, 0, TRUE),
262 HOWTO (R_XTENSA_SLOT14_OP, 0, 0, 0, TRUE, 0, complain_overflow_dont,
263 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_OP", FALSE, 0, 0, TRUE),
265 /* "Alternate" relocations. The meaning of these is opcode-specific. */
266 HOWTO (R_XTENSA_SLOT0_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
267 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT0_ALT", FALSE, 0, 0, TRUE),
268 HOWTO (R_XTENSA_SLOT1_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
269 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT1_ALT", FALSE, 0, 0, TRUE),
270 HOWTO (R_XTENSA_SLOT2_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
271 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT2_ALT", FALSE, 0, 0, TRUE),
272 HOWTO (R_XTENSA_SLOT3_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
273 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT3_ALT", FALSE, 0, 0, TRUE),
274 HOWTO (R_XTENSA_SLOT4_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
275 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT4_ALT", FALSE, 0, 0, TRUE),
276 HOWTO (R_XTENSA_SLOT5_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
277 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT5_ALT", FALSE, 0, 0, TRUE),
278 HOWTO (R_XTENSA_SLOT6_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
279 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT6_ALT", FALSE, 0, 0, TRUE),
280 HOWTO (R_XTENSA_SLOT7_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
281 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT7_ALT", FALSE, 0, 0, TRUE),
282 HOWTO (R_XTENSA_SLOT8_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
283 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT8_ALT", FALSE, 0, 0, TRUE),
284 HOWTO (R_XTENSA_SLOT9_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
285 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT9_ALT", FALSE, 0, 0, TRUE),
286 HOWTO (R_XTENSA_SLOT10_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
287 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT10_ALT", FALSE, 0, 0, TRUE),
288 HOWTO (R_XTENSA_SLOT11_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
289 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT11_ALT", FALSE, 0, 0, TRUE),
290 HOWTO (R_XTENSA_SLOT12_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
291 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT12_ALT", FALSE, 0, 0, TRUE),
292 HOWTO (R_XTENSA_SLOT13_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
293 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT13_ALT", FALSE, 0, 0, TRUE),
294 HOWTO (R_XTENSA_SLOT14_ALT, 0, 0, 0, TRUE, 0, complain_overflow_dont,
295 bfd_elf_xtensa_reloc, "R_XTENSA_SLOT14_ALT", FALSE, 0, 0, TRUE),
297 /* TLS relocations. */
298 HOWTO (R_XTENSA_TLSDESC_FN, 0, 2, 32, FALSE, 0, complain_overflow_dont,
299 bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_FN",
300 FALSE, 0, 0xffffffff, FALSE),
301 HOWTO (R_XTENSA_TLSDESC_ARG, 0, 2, 32, FALSE, 0, complain_overflow_dont,
302 bfd_elf_xtensa_reloc, "R_XTENSA_TLSDESC_ARG",
303 FALSE, 0, 0xffffffff, FALSE),
304 HOWTO (R_XTENSA_TLS_DTPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
305 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_DTPOFF",
306 FALSE, 0, 0xffffffff, FALSE),
307 HOWTO (R_XTENSA_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_dont,
308 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_TPOFF",
309 FALSE, 0, 0xffffffff, FALSE),
310 HOWTO (R_XTENSA_TLS_FUNC, 0, 0, 0, FALSE, 0, complain_overflow_dont,
311 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_FUNC",
312 FALSE, 0, 0, FALSE),
313 HOWTO (R_XTENSA_TLS_ARG, 0, 0, 0, FALSE, 0, complain_overflow_dont,
314 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_ARG",
315 FALSE, 0, 0, FALSE),
316 HOWTO (R_XTENSA_TLS_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
317 bfd_elf_xtensa_reloc, "R_XTENSA_TLS_CALL",
318 FALSE, 0, 0, FALSE),
321 #if DEBUG_GEN_RELOC
322 #define TRACE(str) \
323 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
324 #else
325 #define TRACE(str)
326 #endif
328 static reloc_howto_type *
329 elf_xtensa_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
330 bfd_reloc_code_real_type code)
332 switch (code)
334 case BFD_RELOC_NONE:
335 TRACE ("BFD_RELOC_NONE");
336 return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
338 case BFD_RELOC_32:
339 TRACE ("BFD_RELOC_32");
340 return &elf_howto_table[(unsigned) R_XTENSA_32 ];
342 case BFD_RELOC_32_PCREL:
343 TRACE ("BFD_RELOC_32_PCREL");
344 return &elf_howto_table[(unsigned) R_XTENSA_32_PCREL ];
346 case BFD_RELOC_XTENSA_DIFF8:
347 TRACE ("BFD_RELOC_XTENSA_DIFF8");
348 return &elf_howto_table[(unsigned) R_XTENSA_DIFF8 ];
350 case BFD_RELOC_XTENSA_DIFF16:
351 TRACE ("BFD_RELOC_XTENSA_DIFF16");
352 return &elf_howto_table[(unsigned) R_XTENSA_DIFF16 ];
354 case BFD_RELOC_XTENSA_DIFF32:
355 TRACE ("BFD_RELOC_XTENSA_DIFF32");
356 return &elf_howto_table[(unsigned) R_XTENSA_DIFF32 ];
358 case BFD_RELOC_XTENSA_RTLD:
359 TRACE ("BFD_RELOC_XTENSA_RTLD");
360 return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
362 case BFD_RELOC_XTENSA_GLOB_DAT:
363 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
364 return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
366 case BFD_RELOC_XTENSA_JMP_SLOT:
367 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
368 return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
370 case BFD_RELOC_XTENSA_RELATIVE:
371 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
372 return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
374 case BFD_RELOC_XTENSA_PLT:
375 TRACE ("BFD_RELOC_XTENSA_PLT");
376 return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
378 case BFD_RELOC_XTENSA_OP0:
379 TRACE ("BFD_RELOC_XTENSA_OP0");
380 return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
382 case BFD_RELOC_XTENSA_OP1:
383 TRACE ("BFD_RELOC_XTENSA_OP1");
384 return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
386 case BFD_RELOC_XTENSA_OP2:
387 TRACE ("BFD_RELOC_XTENSA_OP2");
388 return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
390 case BFD_RELOC_XTENSA_ASM_EXPAND:
391 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
392 return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
394 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
395 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
396 return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
398 case BFD_RELOC_VTABLE_INHERIT:
399 TRACE ("BFD_RELOC_VTABLE_INHERIT");
400 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
402 case BFD_RELOC_VTABLE_ENTRY:
403 TRACE ("BFD_RELOC_VTABLE_ENTRY");
404 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
406 case BFD_RELOC_XTENSA_TLSDESC_FN:
407 TRACE ("BFD_RELOC_XTENSA_TLSDESC_FN");
408 return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_FN ];
410 case BFD_RELOC_XTENSA_TLSDESC_ARG:
411 TRACE ("BFD_RELOC_XTENSA_TLSDESC_ARG");
412 return &elf_howto_table[(unsigned) R_XTENSA_TLSDESC_ARG ];
414 case BFD_RELOC_XTENSA_TLS_DTPOFF:
415 TRACE ("BFD_RELOC_XTENSA_TLS_DTPOFF");
416 return &elf_howto_table[(unsigned) R_XTENSA_TLS_DTPOFF ];
418 case BFD_RELOC_XTENSA_TLS_TPOFF:
419 TRACE ("BFD_RELOC_XTENSA_TLS_TPOFF");
420 return &elf_howto_table[(unsigned) R_XTENSA_TLS_TPOFF ];
422 case BFD_RELOC_XTENSA_TLS_FUNC:
423 TRACE ("BFD_RELOC_XTENSA_TLS_FUNC");
424 return &elf_howto_table[(unsigned) R_XTENSA_TLS_FUNC ];
426 case BFD_RELOC_XTENSA_TLS_ARG:
427 TRACE ("BFD_RELOC_XTENSA_TLS_ARG");
428 return &elf_howto_table[(unsigned) R_XTENSA_TLS_ARG ];
430 case BFD_RELOC_XTENSA_TLS_CALL:
431 TRACE ("BFD_RELOC_XTENSA_TLS_CALL");
432 return &elf_howto_table[(unsigned) R_XTENSA_TLS_CALL ];
434 default:
435 if (code >= BFD_RELOC_XTENSA_SLOT0_OP
436 && code <= BFD_RELOC_XTENSA_SLOT14_OP)
438 unsigned n = (R_XTENSA_SLOT0_OP +
439 (code - BFD_RELOC_XTENSA_SLOT0_OP));
440 return &elf_howto_table[n];
443 if (code >= BFD_RELOC_XTENSA_SLOT0_ALT
444 && code <= BFD_RELOC_XTENSA_SLOT14_ALT)
446 unsigned n = (R_XTENSA_SLOT0_ALT +
447 (code - BFD_RELOC_XTENSA_SLOT0_ALT));
448 return &elf_howto_table[n];
451 break;
454 TRACE ("Unknown");
455 return NULL;
458 static reloc_howto_type *
459 elf_xtensa_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
460 const char *r_name)
462 unsigned int i;
464 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
465 if (elf_howto_table[i].name != NULL
466 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
467 return &elf_howto_table[i];
469 return NULL;
473 /* Given an ELF "rela" relocation, find the corresponding howto and record
474 it in the BFD internal arelent representation of the relocation. */
476 static void
477 elf_xtensa_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
478 arelent *cache_ptr,
479 Elf_Internal_Rela *dst)
481 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
483 BFD_ASSERT (r_type < (unsigned int) R_XTENSA_max);
484 cache_ptr->howto = &elf_howto_table[r_type];
488 /* Functions for the Xtensa ELF linker. */
490 /* The name of the dynamic interpreter. This is put in the .interp
491 section. */
493 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
495 /* The size in bytes of an entry in the procedure linkage table.
496 (This does _not_ include the space for the literals associated with
497 the PLT entry.) */
499 #define PLT_ENTRY_SIZE 16
501 /* For _really_ large PLTs, we may need to alternate between literals
502 and code to keep the literals within the 256K range of the L32R
503 instructions in the code. It's unlikely that anyone would ever need
504 such a big PLT, but an arbitrary limit on the PLT size would be bad.
505 Thus, we split the PLT into chunks. Since there's very little
506 overhead (2 extra literals) for each chunk, the chunk size is kept
507 small so that the code for handling multiple chunks get used and
508 tested regularly. With 254 entries, there are 1K of literals for
509 each chunk, and that seems like a nice round number. */
511 #define PLT_ENTRIES_PER_CHUNK 254
513 /* PLT entries are actually used as stub functions for lazy symbol
514 resolution. Once the symbol is resolved, the stub function is never
515 invoked. Note: the 32-byte frame size used here cannot be changed
516 without a corresponding change in the runtime linker. */
518 static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
520 0x6c, 0x10, 0x04, /* entry sp, 32 */
521 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
522 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
523 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
524 0x0a, 0x80, 0x00, /* jx a8 */
525 0 /* unused */
528 static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
530 0x36, 0x41, 0x00, /* entry sp, 32 */
531 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
532 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
533 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
534 0xa0, 0x08, 0x00, /* jx a8 */
535 0 /* unused */
538 /* The size of the thread control block. */
539 #define TCB_SIZE 8
541 struct elf_xtensa_link_hash_entry
543 struct elf_link_hash_entry elf;
545 bfd_signed_vma tlsfunc_refcount;
547 #define GOT_UNKNOWN 0
548 #define GOT_NORMAL 1
549 #define GOT_TLS_GD 2 /* global or local dynamic */
550 #define GOT_TLS_IE 4 /* initial or local exec */
551 #define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
552 unsigned char tls_type;
555 #define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
557 struct elf_xtensa_obj_tdata
559 struct elf_obj_tdata root;
561 /* tls_type for each local got entry. */
562 char *local_got_tls_type;
564 bfd_signed_vma *local_tlsfunc_refcounts;
567 #define elf_xtensa_tdata(abfd) \
568 ((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
570 #define elf_xtensa_local_got_tls_type(abfd) \
571 (elf_xtensa_tdata (abfd)->local_got_tls_type)
573 #define elf_xtensa_local_tlsfunc_refcounts(abfd) \
574 (elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
576 #define is_xtensa_elf(bfd) \
577 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
578 && elf_tdata (bfd) != NULL \
579 && elf_object_id (bfd) == XTENSA_ELF_DATA)
581 static bfd_boolean
582 elf_xtensa_mkobject (bfd *abfd)
584 return bfd_elf_allocate_object (abfd, sizeof (struct elf_xtensa_obj_tdata),
585 XTENSA_ELF_DATA);
588 /* Xtensa ELF linker hash table. */
590 struct elf_xtensa_link_hash_table
592 struct elf_link_hash_table elf;
594 /* Short-cuts to get to dynamic linker sections. */
595 asection *sgot;
596 asection *sgotplt;
597 asection *srelgot;
598 asection *splt;
599 asection *srelplt;
600 asection *sgotloc;
601 asection *spltlittbl;
603 /* Total count of PLT relocations seen during check_relocs.
604 The actual PLT code must be split into multiple sections and all
605 the sections have to be created before size_dynamic_sections,
606 where we figure out the exact number of PLT entries that will be
607 needed. It is OK if this count is an overestimate, e.g., some
608 relocations may be removed by GC. */
609 int plt_reloc_count;
611 struct elf_xtensa_link_hash_entry *tlsbase;
614 /* Get the Xtensa ELF linker hash table from a link_info structure. */
616 #define elf_xtensa_hash_table(p) \
617 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
618 == XTENSA_ELF_DATA ? ((struct elf_xtensa_link_hash_table *) ((p)->hash)) : NULL)
620 /* Create an entry in an Xtensa ELF linker hash table. */
622 static struct bfd_hash_entry *
623 elf_xtensa_link_hash_newfunc (struct bfd_hash_entry *entry,
624 struct bfd_hash_table *table,
625 const char *string)
627 /* Allocate the structure if it has not already been allocated by a
628 subclass. */
629 if (entry == NULL)
631 entry = bfd_hash_allocate (table,
632 sizeof (struct elf_xtensa_link_hash_entry));
633 if (entry == NULL)
634 return entry;
637 /* Call the allocation method of the superclass. */
638 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
639 if (entry != NULL)
641 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (entry);
642 eh->tlsfunc_refcount = 0;
643 eh->tls_type = GOT_UNKNOWN;
646 return entry;
649 /* Create an Xtensa ELF linker hash table. */
651 static struct bfd_link_hash_table *
652 elf_xtensa_link_hash_table_create (bfd *abfd)
654 struct elf_link_hash_entry *tlsbase;
655 struct elf_xtensa_link_hash_table *ret;
656 bfd_size_type amt = sizeof (struct elf_xtensa_link_hash_table);
658 ret = bfd_malloc (amt);
659 if (ret == NULL)
660 return NULL;
662 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
663 elf_xtensa_link_hash_newfunc,
664 sizeof (struct elf_xtensa_link_hash_entry),
665 XTENSA_ELF_DATA))
667 free (ret);
668 return NULL;
671 ret->sgot = NULL;
672 ret->sgotplt = NULL;
673 ret->srelgot = NULL;
674 ret->splt = NULL;
675 ret->srelplt = NULL;
676 ret->sgotloc = NULL;
677 ret->spltlittbl = NULL;
679 ret->plt_reloc_count = 0;
681 /* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
682 for it later. */
683 tlsbase = elf_link_hash_lookup (&ret->elf, "_TLS_MODULE_BASE_",
684 TRUE, FALSE, FALSE);
685 tlsbase->root.type = bfd_link_hash_new;
686 tlsbase->root.u.undef.abfd = NULL;
687 tlsbase->non_elf = 0;
688 ret->tlsbase = elf_xtensa_hash_entry (tlsbase);
689 ret->tlsbase->tls_type = GOT_UNKNOWN;
691 return &ret->elf.root;
694 /* Copy the extra info we tack onto an elf_link_hash_entry. */
696 static void
697 elf_xtensa_copy_indirect_symbol (struct bfd_link_info *info,
698 struct elf_link_hash_entry *dir,
699 struct elf_link_hash_entry *ind)
701 struct elf_xtensa_link_hash_entry *edir, *eind;
703 edir = elf_xtensa_hash_entry (dir);
704 eind = elf_xtensa_hash_entry (ind);
706 if (ind->root.type == bfd_link_hash_indirect)
708 edir->tlsfunc_refcount += eind->tlsfunc_refcount;
709 eind->tlsfunc_refcount = 0;
711 if (dir->got.refcount <= 0)
713 edir->tls_type = eind->tls_type;
714 eind->tls_type = GOT_UNKNOWN;
718 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
721 static inline bfd_boolean
722 elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry *h,
723 struct bfd_link_info *info)
725 /* Check if we should do dynamic things to this symbol. The
726 "ignore_protected" argument need not be set, because Xtensa code
727 does not require special handling of STV_PROTECTED to make function
728 pointer comparisons work properly. The PLT addresses are never
729 used for function pointers. */
731 return _bfd_elf_dynamic_symbol_p (h, info, 0);
735 static int
736 property_table_compare (const void *ap, const void *bp)
738 const property_table_entry *a = (const property_table_entry *) ap;
739 const property_table_entry *b = (const property_table_entry *) bp;
741 if (a->address == b->address)
743 if (a->size != b->size)
744 return (a->size - b->size);
746 if ((a->flags & XTENSA_PROP_ALIGN) != (b->flags & XTENSA_PROP_ALIGN))
747 return ((b->flags & XTENSA_PROP_ALIGN)
748 - (a->flags & XTENSA_PROP_ALIGN));
750 if ((a->flags & XTENSA_PROP_ALIGN)
751 && (GET_XTENSA_PROP_ALIGNMENT (a->flags)
752 != GET_XTENSA_PROP_ALIGNMENT (b->flags)))
753 return (GET_XTENSA_PROP_ALIGNMENT (a->flags)
754 - GET_XTENSA_PROP_ALIGNMENT (b->flags));
756 if ((a->flags & XTENSA_PROP_UNREACHABLE)
757 != (b->flags & XTENSA_PROP_UNREACHABLE))
758 return ((b->flags & XTENSA_PROP_UNREACHABLE)
759 - (a->flags & XTENSA_PROP_UNREACHABLE));
761 return (a->flags - b->flags);
764 return (a->address - b->address);
768 static int
769 property_table_matches (const void *ap, const void *bp)
771 const property_table_entry *a = (const property_table_entry *) ap;
772 const property_table_entry *b = (const property_table_entry *) bp;
774 /* Check if one entry overlaps with the other. */
775 if ((b->address >= a->address && b->address < (a->address + a->size))
776 || (a->address >= b->address && a->address < (b->address + b->size)))
777 return 0;
779 return (a->address - b->address);
783 /* Get the literal table or property table entries for the given
784 section. Sets TABLE_P and returns the number of entries. On
785 error, returns a negative value. */
787 static int
788 xtensa_read_table_entries (bfd *abfd,
789 asection *section,
790 property_table_entry **table_p,
791 const char *sec_name,
792 bfd_boolean output_addr)
794 asection *table_section;
795 bfd_size_type table_size = 0;
796 bfd_byte *table_data;
797 property_table_entry *blocks;
798 int blk, block_count;
799 bfd_size_type num_records;
800 Elf_Internal_Rela *internal_relocs, *irel, *rel_end;
801 bfd_vma section_addr, off;
802 flagword predef_flags;
803 bfd_size_type table_entry_size, section_limit;
805 if (!section
806 || !(section->flags & SEC_ALLOC)
807 || (section->flags & SEC_DEBUGGING))
809 *table_p = NULL;
810 return 0;
813 table_section = xtensa_get_property_section (section, sec_name);
814 if (table_section)
815 table_size = table_section->size;
817 if (table_size == 0)
819 *table_p = NULL;
820 return 0;
823 predef_flags = xtensa_get_property_predef_flags (table_section);
824 table_entry_size = 12;
825 if (predef_flags)
826 table_entry_size -= 4;
828 num_records = table_size / table_entry_size;
829 table_data = retrieve_contents (abfd, table_section, TRUE);
830 blocks = (property_table_entry *)
831 bfd_malloc (num_records * sizeof (property_table_entry));
832 block_count = 0;
834 if (output_addr)
835 section_addr = section->output_section->vma + section->output_offset;
836 else
837 section_addr = section->vma;
839 internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
840 if (internal_relocs && !table_section->reloc_done)
842 qsort (internal_relocs, table_section->reloc_count,
843 sizeof (Elf_Internal_Rela), internal_reloc_compare);
844 irel = internal_relocs;
846 else
847 irel = NULL;
849 section_limit = bfd_get_section_limit (abfd, section);
850 rel_end = internal_relocs + table_section->reloc_count;
852 for (off = 0; off < table_size; off += table_entry_size)
854 bfd_vma address = bfd_get_32 (abfd, table_data + off);
856 /* Skip any relocations before the current offset. This should help
857 avoid confusion caused by unexpected relocations for the preceding
858 table entry. */
859 while (irel &&
860 (irel->r_offset < off
861 || (irel->r_offset == off
862 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_NONE)))
864 irel += 1;
865 if (irel >= rel_end)
866 irel = 0;
869 if (irel && irel->r_offset == off)
871 bfd_vma sym_off;
872 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
873 BFD_ASSERT (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32);
875 if (get_elf_r_symndx_section (abfd, r_symndx) != section)
876 continue;
878 sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
879 BFD_ASSERT (sym_off == 0);
880 address += (section_addr + sym_off + irel->r_addend);
882 else
884 if (address < section_addr
885 || address >= section_addr + section_limit)
886 continue;
889 blocks[block_count].address = address;
890 blocks[block_count].size = bfd_get_32 (abfd, table_data + off + 4);
891 if (predef_flags)
892 blocks[block_count].flags = predef_flags;
893 else
894 blocks[block_count].flags = bfd_get_32 (abfd, table_data + off + 8);
895 block_count++;
898 release_contents (table_section, table_data);
899 release_internal_relocs (table_section, internal_relocs);
901 if (block_count > 0)
903 /* Now sort them into address order for easy reference. */
904 qsort (blocks, block_count, sizeof (property_table_entry),
905 property_table_compare);
907 /* Check that the table contents are valid. Problems may occur,
908 for example, if an unrelocated object file is stripped. */
909 for (blk = 1; blk < block_count; blk++)
911 /* The only circumstance where two entries may legitimately
912 have the same address is when one of them is a zero-size
913 placeholder to mark a place where fill can be inserted.
914 The zero-size entry should come first. */
915 if (blocks[blk - 1].address == blocks[blk].address &&
916 blocks[blk - 1].size != 0)
918 (*_bfd_error_handler) (_("%B(%A): invalid property table"),
919 abfd, section);
920 bfd_set_error (bfd_error_bad_value);
921 free (blocks);
922 return -1;
927 *table_p = blocks;
928 return block_count;
932 static property_table_entry *
933 elf_xtensa_find_property_entry (property_table_entry *property_table,
934 int property_table_size,
935 bfd_vma addr)
937 property_table_entry entry;
938 property_table_entry *rv;
940 if (property_table_size == 0)
941 return NULL;
943 entry.address = addr;
944 entry.size = 1;
945 entry.flags = 0;
947 rv = bsearch (&entry, property_table, property_table_size,
948 sizeof (property_table_entry), property_table_matches);
949 return rv;
953 static bfd_boolean
954 elf_xtensa_in_literal_pool (property_table_entry *lit_table,
955 int lit_table_size,
956 bfd_vma addr)
958 if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
959 return TRUE;
961 return FALSE;
965 /* Look through the relocs for a section during the first phase, and
966 calculate needed space in the dynamic reloc sections. */
968 static bfd_boolean
969 elf_xtensa_check_relocs (bfd *abfd,
970 struct bfd_link_info *info,
971 asection *sec,
972 const Elf_Internal_Rela *relocs)
974 struct elf_xtensa_link_hash_table *htab;
975 Elf_Internal_Shdr *symtab_hdr;
976 struct elf_link_hash_entry **sym_hashes;
977 const Elf_Internal_Rela *rel;
978 const Elf_Internal_Rela *rel_end;
980 if (info->relocatable || (sec->flags & SEC_ALLOC) == 0)
981 return TRUE;
983 BFD_ASSERT (is_xtensa_elf (abfd));
985 htab = elf_xtensa_hash_table (info);
986 if (htab == NULL)
987 return FALSE;
989 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
990 sym_hashes = elf_sym_hashes (abfd);
992 rel_end = relocs + sec->reloc_count;
993 for (rel = relocs; rel < rel_end; rel++)
995 unsigned int r_type;
996 unsigned long r_symndx;
997 struct elf_link_hash_entry *h = NULL;
998 struct elf_xtensa_link_hash_entry *eh;
999 int tls_type, old_tls_type;
1000 bfd_boolean is_got = FALSE;
1001 bfd_boolean is_plt = FALSE;
1002 bfd_boolean is_tlsfunc = FALSE;
1004 r_symndx = ELF32_R_SYM (rel->r_info);
1005 r_type = ELF32_R_TYPE (rel->r_info);
1007 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1009 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1010 abfd, r_symndx);
1011 return FALSE;
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 eh = elf_xtensa_hash_entry (h);
1023 switch (r_type)
1025 case R_XTENSA_TLSDESC_FN:
1026 if (info->shared)
1028 tls_type = GOT_TLS_GD;
1029 is_got = TRUE;
1030 is_tlsfunc = TRUE;
1032 else
1033 tls_type = GOT_TLS_IE;
1034 break;
1036 case R_XTENSA_TLSDESC_ARG:
1037 if (info->shared)
1039 tls_type = GOT_TLS_GD;
1040 is_got = TRUE;
1042 else
1044 tls_type = GOT_TLS_IE;
1045 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1046 is_got = TRUE;
1048 break;
1050 case R_XTENSA_TLS_DTPOFF:
1051 if (info->shared)
1052 tls_type = GOT_TLS_GD;
1053 else
1054 tls_type = GOT_TLS_IE;
1055 break;
1057 case R_XTENSA_TLS_TPOFF:
1058 tls_type = GOT_TLS_IE;
1059 if (info->shared)
1060 info->flags |= DF_STATIC_TLS;
1061 if (info->shared || h)
1062 is_got = TRUE;
1063 break;
1065 case R_XTENSA_32:
1066 tls_type = GOT_NORMAL;
1067 is_got = TRUE;
1068 break;
1070 case R_XTENSA_PLT:
1071 tls_type = GOT_NORMAL;
1072 is_plt = TRUE;
1073 break;
1075 case R_XTENSA_GNU_VTINHERIT:
1076 /* This relocation describes the C++ object vtable hierarchy.
1077 Reconstruct it for later use during GC. */
1078 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1079 return FALSE;
1080 continue;
1082 case R_XTENSA_GNU_VTENTRY:
1083 /* This relocation describes which C++ vtable entries are actually
1084 used. Record for later use during GC. */
1085 BFD_ASSERT (h != NULL);
1086 if (h != NULL
1087 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1088 return FALSE;
1089 continue;
1091 default:
1092 /* Nothing to do for any other relocations. */
1093 continue;
1096 if (h)
1098 if (is_plt)
1100 if (h->plt.refcount <= 0)
1102 h->needs_plt = 1;
1103 h->plt.refcount = 1;
1105 else
1106 h->plt.refcount += 1;
1108 /* Keep track of the total PLT relocation count even if we
1109 don't yet know whether the dynamic sections will be
1110 created. */
1111 htab->plt_reloc_count += 1;
1113 if (elf_hash_table (info)->dynamic_sections_created)
1115 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1116 return FALSE;
1119 else if (is_got)
1121 if (h->got.refcount <= 0)
1122 h->got.refcount = 1;
1123 else
1124 h->got.refcount += 1;
1127 if (is_tlsfunc)
1128 eh->tlsfunc_refcount += 1;
1130 old_tls_type = eh->tls_type;
1132 else
1134 /* Allocate storage the first time. */
1135 if (elf_local_got_refcounts (abfd) == NULL)
1137 bfd_size_type size = symtab_hdr->sh_info;
1138 void *mem;
1140 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1141 if (mem == NULL)
1142 return FALSE;
1143 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) mem;
1145 mem = bfd_zalloc (abfd, size);
1146 if (mem == NULL)
1147 return FALSE;
1148 elf_xtensa_local_got_tls_type (abfd) = (char *) mem;
1150 mem = bfd_zalloc (abfd, size * sizeof (bfd_signed_vma));
1151 if (mem == NULL)
1152 return FALSE;
1153 elf_xtensa_local_tlsfunc_refcounts (abfd)
1154 = (bfd_signed_vma *) mem;
1157 /* This is a global offset table entry for a local symbol. */
1158 if (is_got || is_plt)
1159 elf_local_got_refcounts (abfd) [r_symndx] += 1;
1161 if (is_tlsfunc)
1162 elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx] += 1;
1164 old_tls_type = elf_xtensa_local_got_tls_type (abfd) [r_symndx];
1167 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1168 tls_type |= old_tls_type;
1169 /* If a TLS symbol is accessed using IE at least once,
1170 there is no point to use a dynamic model for it. */
1171 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1172 && ((old_tls_type & GOT_TLS_GD) == 0
1173 || (tls_type & GOT_TLS_IE) == 0))
1175 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_GD))
1176 tls_type = old_tls_type;
1177 else if ((old_tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_GD))
1178 tls_type |= old_tls_type;
1179 else
1181 (*_bfd_error_handler)
1182 (_("%B: `%s' accessed both as normal and thread local symbol"),
1183 abfd,
1184 h ? h->root.root.string : "<local>");
1185 return FALSE;
1189 if (old_tls_type != tls_type)
1191 if (eh)
1192 eh->tls_type = tls_type;
1193 else
1194 elf_xtensa_local_got_tls_type (abfd) [r_symndx] = tls_type;
1198 return TRUE;
1202 static void
1203 elf_xtensa_make_sym_local (struct bfd_link_info *info,
1204 struct elf_link_hash_entry *h)
1206 if (info->shared)
1208 if (h->plt.refcount > 0)
1210 /* For shared objects, there's no need for PLT entries for local
1211 symbols (use RELATIVE relocs instead of JMP_SLOT relocs). */
1212 if (h->got.refcount < 0)
1213 h->got.refcount = 0;
1214 h->got.refcount += h->plt.refcount;
1215 h->plt.refcount = 0;
1218 else
1220 /* Don't need any dynamic relocations at all. */
1221 h->plt.refcount = 0;
1222 h->got.refcount = 0;
1227 static void
1228 elf_xtensa_hide_symbol (struct bfd_link_info *info,
1229 struct elf_link_hash_entry *h,
1230 bfd_boolean force_local)
1232 /* For a shared link, move the plt refcount to the got refcount to leave
1233 space for RELATIVE relocs. */
1234 elf_xtensa_make_sym_local (info, h);
1236 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1240 /* Return the section that should be marked against GC for a given
1241 relocation. */
1243 static asection *
1244 elf_xtensa_gc_mark_hook (asection *sec,
1245 struct bfd_link_info *info,
1246 Elf_Internal_Rela *rel,
1247 struct elf_link_hash_entry *h,
1248 Elf_Internal_Sym *sym)
1250 /* Property sections are marked "KEEP" in the linker scripts, but they
1251 should not cause other sections to be marked. (This approach relies
1252 on elf_xtensa_discard_info to remove property table entries that
1253 describe discarded sections. Alternatively, it might be more
1254 efficient to avoid using "KEEP" in the linker scripts and instead use
1255 the gc_mark_extra_sections hook to mark only the property sections
1256 that describe marked sections. That alternative does not work well
1257 with the current property table sections, which do not correspond
1258 one-to-one with the sections they describe, but that should be fixed
1259 someday.) */
1260 if (xtensa_is_property_section (sec))
1261 return NULL;
1263 if (h != NULL)
1264 switch (ELF32_R_TYPE (rel->r_info))
1266 case R_XTENSA_GNU_VTINHERIT:
1267 case R_XTENSA_GNU_VTENTRY:
1268 return NULL;
1271 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1275 /* Update the GOT & PLT entry reference counts
1276 for the section being removed. */
1278 static bfd_boolean
1279 elf_xtensa_gc_sweep_hook (bfd *abfd,
1280 struct bfd_link_info *info,
1281 asection *sec,
1282 const Elf_Internal_Rela *relocs)
1284 Elf_Internal_Shdr *symtab_hdr;
1285 struct elf_link_hash_entry **sym_hashes;
1286 const Elf_Internal_Rela *rel, *relend;
1287 struct elf_xtensa_link_hash_table *htab;
1289 htab = elf_xtensa_hash_table (info);
1290 if (htab == NULL)
1291 return FALSE;
1293 if (info->relocatable)
1294 return TRUE;
1296 if ((sec->flags & SEC_ALLOC) == 0)
1297 return TRUE;
1299 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1300 sym_hashes = elf_sym_hashes (abfd);
1302 relend = relocs + sec->reloc_count;
1303 for (rel = relocs; rel < relend; rel++)
1305 unsigned long r_symndx;
1306 unsigned int r_type;
1307 struct elf_link_hash_entry *h = NULL;
1308 struct elf_xtensa_link_hash_entry *eh;
1309 bfd_boolean is_got = FALSE;
1310 bfd_boolean is_plt = FALSE;
1311 bfd_boolean is_tlsfunc = FALSE;
1313 r_symndx = ELF32_R_SYM (rel->r_info);
1314 if (r_symndx >= symtab_hdr->sh_info)
1316 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1317 while (h->root.type == bfd_link_hash_indirect
1318 || h->root.type == bfd_link_hash_warning)
1319 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1321 eh = elf_xtensa_hash_entry (h);
1323 r_type = ELF32_R_TYPE (rel->r_info);
1324 switch (r_type)
1326 case R_XTENSA_TLSDESC_FN:
1327 if (info->shared)
1329 is_got = TRUE;
1330 is_tlsfunc = TRUE;
1332 break;
1334 case R_XTENSA_TLSDESC_ARG:
1335 if (info->shared)
1336 is_got = TRUE;
1337 else
1339 if (h && elf_xtensa_hash_entry (h) != htab->tlsbase)
1340 is_got = TRUE;
1342 break;
1344 case R_XTENSA_TLS_TPOFF:
1345 if (info->shared || h)
1346 is_got = TRUE;
1347 break;
1349 case R_XTENSA_32:
1350 is_got = TRUE;
1351 break;
1353 case R_XTENSA_PLT:
1354 is_plt = TRUE;
1355 break;
1357 default:
1358 continue;
1361 if (h)
1363 if (is_plt)
1365 if (h->plt.refcount > 0)
1366 h->plt.refcount--;
1368 else if (is_got)
1370 if (h->got.refcount > 0)
1371 h->got.refcount--;
1373 if (is_tlsfunc)
1375 if (eh->tlsfunc_refcount > 0)
1376 eh->tlsfunc_refcount--;
1379 else
1381 if (is_got || is_plt)
1383 bfd_signed_vma *got_refcount
1384 = &elf_local_got_refcounts (abfd) [r_symndx];
1385 if (*got_refcount > 0)
1386 *got_refcount -= 1;
1388 if (is_tlsfunc)
1390 bfd_signed_vma *tlsfunc_refcount
1391 = &elf_xtensa_local_tlsfunc_refcounts (abfd) [r_symndx];
1392 if (*tlsfunc_refcount > 0)
1393 *tlsfunc_refcount -= 1;
1398 return TRUE;
1402 /* Create all the dynamic sections. */
1404 static bfd_boolean
1405 elf_xtensa_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1407 struct elf_xtensa_link_hash_table *htab;
1408 flagword flags, noalloc_flags;
1410 htab = elf_xtensa_hash_table (info);
1411 if (htab == NULL)
1412 return FALSE;
1414 /* First do all the standard stuff. */
1415 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
1416 return FALSE;
1417 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
1418 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1419 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1420 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1421 htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1423 /* Create any extra PLT sections in case check_relocs has already
1424 been called on all the non-dynamic input files. */
1425 if (! add_extra_plt_sections (info, htab->plt_reloc_count))
1426 return FALSE;
1428 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
1429 | SEC_LINKER_CREATED | SEC_READONLY);
1430 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
1432 /* Mark the ".got.plt" section READONLY. */
1433 if (htab->sgotplt == NULL
1434 || ! bfd_set_section_flags (dynobj, htab->sgotplt, flags))
1435 return FALSE;
1437 /* Create ".got.loc" (literal tables for use by dynamic linker). */
1438 htab->sgotloc = bfd_make_section_with_flags (dynobj, ".got.loc", flags);
1439 if (htab->sgotloc == NULL
1440 || ! bfd_set_section_alignment (dynobj, htab->sgotloc, 2))
1441 return FALSE;
1443 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
1444 htab->spltlittbl = bfd_make_section_with_flags (dynobj, ".xt.lit.plt",
1445 noalloc_flags);
1446 if (htab->spltlittbl == NULL
1447 || ! bfd_set_section_alignment (dynobj, htab->spltlittbl, 2))
1448 return FALSE;
1450 return TRUE;
1454 static bfd_boolean
1455 add_extra_plt_sections (struct bfd_link_info *info, int count)
1457 bfd *dynobj = elf_hash_table (info)->dynobj;
1458 int chunk;
1460 /* Iterate over all chunks except 0 which uses the standard ".plt" and
1461 ".got.plt" sections. */
1462 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
1464 char *sname;
1465 flagword flags;
1466 asection *s;
1468 /* Stop when we find a section has already been created. */
1469 if (elf_xtensa_get_plt_section (info, chunk))
1470 break;
1472 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1473 | SEC_LINKER_CREATED | SEC_READONLY);
1475 sname = (char *) bfd_malloc (10);
1476 sprintf (sname, ".plt.%u", chunk);
1477 s = bfd_make_section_with_flags (dynobj, sname, flags | SEC_CODE);
1478 if (s == NULL
1479 || ! bfd_set_section_alignment (dynobj, s, 2))
1480 return FALSE;
1482 sname = (char *) bfd_malloc (14);
1483 sprintf (sname, ".got.plt.%u", chunk);
1484 s = bfd_make_section_with_flags (dynobj, sname, flags);
1485 if (s == NULL
1486 || ! bfd_set_section_alignment (dynobj, s, 2))
1487 return FALSE;
1490 return TRUE;
1494 /* Adjust a symbol defined by a dynamic object and referenced by a
1495 regular object. The current definition is in some section of the
1496 dynamic object, but we're not including those sections. We have to
1497 change the definition to something the rest of the link can
1498 understand. */
1500 static bfd_boolean
1501 elf_xtensa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1502 struct elf_link_hash_entry *h)
1504 /* If this is a weak symbol, and there is a real definition, the
1505 processor independent code will have arranged for us to see the
1506 real definition first, and we can just use the same value. */
1507 if (h->u.weakdef)
1509 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1510 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1511 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1512 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1513 return TRUE;
1516 /* This is a reference to a symbol defined by a dynamic object. The
1517 reference must go through the GOT, so there's no need for COPY relocs,
1518 .dynbss, etc. */
1520 return TRUE;
1524 static bfd_boolean
1525 elf_xtensa_allocate_dynrelocs (struct elf_link_hash_entry *h, void *arg)
1527 struct bfd_link_info *info;
1528 struct elf_xtensa_link_hash_table *htab;
1529 struct elf_xtensa_link_hash_entry *eh = elf_xtensa_hash_entry (h);
1531 if (h->root.type == bfd_link_hash_indirect)
1532 return TRUE;
1534 if (h->root.type == bfd_link_hash_warning)
1535 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1537 info = (struct bfd_link_info *) arg;
1538 htab = elf_xtensa_hash_table (info);
1539 if (htab == NULL)
1540 return FALSE;
1542 /* If we saw any use of an IE model for this symbol, we can then optimize
1543 away GOT entries for any TLSDESC_FN relocs. */
1544 if ((eh->tls_type & GOT_TLS_IE) != 0)
1546 BFD_ASSERT (h->got.refcount >= eh->tlsfunc_refcount);
1547 h->got.refcount -= eh->tlsfunc_refcount;
1550 if (! elf_xtensa_dynamic_symbol_p (h, info))
1551 elf_xtensa_make_sym_local (info, h);
1553 if (h->plt.refcount > 0)
1554 htab->srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1556 if (h->got.refcount > 0)
1557 htab->srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
1559 return TRUE;
1563 static void
1564 elf_xtensa_allocate_local_got_size (struct bfd_link_info *info)
1566 struct elf_xtensa_link_hash_table *htab;
1567 bfd *i;
1569 htab = elf_xtensa_hash_table (info);
1570 if (htab == NULL)
1571 return;
1573 for (i = info->input_bfds; i; i = i->link_next)
1575 bfd_signed_vma *local_got_refcounts;
1576 bfd_size_type j, cnt;
1577 Elf_Internal_Shdr *symtab_hdr;
1579 local_got_refcounts = elf_local_got_refcounts (i);
1580 if (!local_got_refcounts)
1581 continue;
1583 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1584 cnt = symtab_hdr->sh_info;
1586 for (j = 0; j < cnt; ++j)
1588 /* If we saw any use of an IE model for this symbol, we can
1589 then optimize away GOT entries for any TLSDESC_FN relocs. */
1590 if ((elf_xtensa_local_got_tls_type (i) [j] & GOT_TLS_IE) != 0)
1592 bfd_signed_vma *tlsfunc_refcount
1593 = &elf_xtensa_local_tlsfunc_refcounts (i) [j];
1594 BFD_ASSERT (local_got_refcounts[j] >= *tlsfunc_refcount);
1595 local_got_refcounts[j] -= *tlsfunc_refcount;
1598 if (local_got_refcounts[j] > 0)
1599 htab->srelgot->size += (local_got_refcounts[j]
1600 * sizeof (Elf32_External_Rela));
1606 /* Set the sizes of the dynamic sections. */
1608 static bfd_boolean
1609 elf_xtensa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1610 struct bfd_link_info *info)
1612 struct elf_xtensa_link_hash_table *htab;
1613 bfd *dynobj, *abfd;
1614 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1615 bfd_boolean relplt, relgot;
1616 int plt_entries, plt_chunks, chunk;
1618 plt_entries = 0;
1619 plt_chunks = 0;
1621 htab = elf_xtensa_hash_table (info);
1622 if (htab == NULL)
1623 return FALSE;
1625 dynobj = elf_hash_table (info)->dynobj;
1626 if (dynobj == NULL)
1627 abort ();
1628 srelgot = htab->srelgot;
1629 srelplt = htab->srelplt;
1631 if (elf_hash_table (info)->dynamic_sections_created)
1633 BFD_ASSERT (htab->srelgot != NULL
1634 && htab->srelplt != NULL
1635 && htab->sgot != NULL
1636 && htab->spltlittbl != NULL
1637 && htab->sgotloc != NULL);
1639 /* Set the contents of the .interp section to the interpreter. */
1640 if (info->executable)
1642 s = bfd_get_section_by_name (dynobj, ".interp");
1643 if (s == NULL)
1644 abort ();
1645 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1646 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1649 /* Allocate room for one word in ".got". */
1650 htab->sgot->size = 4;
1652 /* Allocate space in ".rela.got" for literals that reference global
1653 symbols and space in ".rela.plt" for literals that have PLT
1654 entries. */
1655 elf_link_hash_traverse (elf_hash_table (info),
1656 elf_xtensa_allocate_dynrelocs,
1657 (void *) info);
1659 /* If we are generating a shared object, we also need space in
1660 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1661 reference local symbols. */
1662 if (info->shared)
1663 elf_xtensa_allocate_local_got_size (info);
1665 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1666 each PLT entry, we need the PLT code plus a 4-byte literal.
1667 For each chunk of ".plt", we also need two more 4-byte
1668 literals, two corresponding entries in ".rela.got", and an
1669 8-byte entry in ".xt.lit.plt". */
1670 spltlittbl = htab->spltlittbl;
1671 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
1672 plt_chunks =
1673 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1675 /* Iterate over all the PLT chunks, including any extra sections
1676 created earlier because the initial count of PLT relocations
1677 was an overestimate. */
1678 for (chunk = 0;
1679 (splt = elf_xtensa_get_plt_section (info, chunk)) != NULL;
1680 chunk++)
1682 int chunk_entries;
1684 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
1685 BFD_ASSERT (sgotplt != NULL);
1687 if (chunk < plt_chunks - 1)
1688 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1689 else if (chunk == plt_chunks - 1)
1690 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1691 else
1692 chunk_entries = 0;
1694 if (chunk_entries != 0)
1696 sgotplt->size = 4 * (chunk_entries + 2);
1697 splt->size = PLT_ENTRY_SIZE * chunk_entries;
1698 srelgot->size += 2 * sizeof (Elf32_External_Rela);
1699 spltlittbl->size += 8;
1701 else
1703 sgotplt->size = 0;
1704 splt->size = 0;
1708 /* Allocate space in ".got.loc" to match the total size of all the
1709 literal tables. */
1710 sgotloc = htab->sgotloc;
1711 sgotloc->size = spltlittbl->size;
1712 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1714 if (abfd->flags & DYNAMIC)
1715 continue;
1716 for (s = abfd->sections; s != NULL; s = s->next)
1718 if (! elf_discarded_section (s)
1719 && xtensa_is_littable_section (s)
1720 && s != spltlittbl)
1721 sgotloc->size += s->size;
1726 /* Allocate memory for dynamic sections. */
1727 relplt = FALSE;
1728 relgot = FALSE;
1729 for (s = dynobj->sections; s != NULL; s = s->next)
1731 const char *name;
1733 if ((s->flags & SEC_LINKER_CREATED) == 0)
1734 continue;
1736 /* It's OK to base decisions on the section name, because none
1737 of the dynobj section names depend upon the input files. */
1738 name = bfd_get_section_name (dynobj, s);
1740 if (CONST_STRNEQ (name, ".rela"))
1742 if (s->size != 0)
1744 if (strcmp (name, ".rela.plt") == 0)
1745 relplt = TRUE;
1746 else if (strcmp (name, ".rela.got") == 0)
1747 relgot = TRUE;
1749 /* We use the reloc_count field as a counter if we need
1750 to copy relocs into the output file. */
1751 s->reloc_count = 0;
1754 else if (! CONST_STRNEQ (name, ".plt.")
1755 && ! CONST_STRNEQ (name, ".got.plt.")
1756 && strcmp (name, ".got") != 0
1757 && strcmp (name, ".plt") != 0
1758 && strcmp (name, ".got.plt") != 0
1759 && strcmp (name, ".xt.lit.plt") != 0
1760 && strcmp (name, ".got.loc") != 0)
1762 /* It's not one of our sections, so don't allocate space. */
1763 continue;
1766 if (s->size == 0)
1768 /* If we don't need this section, strip it from the output
1769 file. We must create the ".plt*" and ".got.plt*"
1770 sections in create_dynamic_sections and/or check_relocs
1771 based on a conservative estimate of the PLT relocation
1772 count, because the sections must be created before the
1773 linker maps input sections to output sections. The
1774 linker does that before size_dynamic_sections, where we
1775 compute the exact size of the PLT, so there may be more
1776 of these sections than are actually needed. */
1777 s->flags |= SEC_EXCLUDE;
1779 else if ((s->flags & SEC_HAS_CONTENTS) != 0)
1781 /* Allocate memory for the section contents. */
1782 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1783 if (s->contents == NULL)
1784 return FALSE;
1788 if (elf_hash_table (info)->dynamic_sections_created)
1790 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1791 known until finish_dynamic_sections, but we need to get the relocs
1792 in place before they are sorted. */
1793 for (chunk = 0; chunk < plt_chunks; chunk++)
1795 Elf_Internal_Rela irela;
1796 bfd_byte *loc;
1798 irela.r_offset = 0;
1799 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1800 irela.r_addend = 0;
1802 loc = (srelgot->contents
1803 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1804 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1805 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1806 loc + sizeof (Elf32_External_Rela));
1807 srelgot->reloc_count += 2;
1810 /* Add some entries to the .dynamic section. We fill in the
1811 values later, in elf_xtensa_finish_dynamic_sections, but we
1812 must add the entries now so that we get the correct size for
1813 the .dynamic section. The DT_DEBUG entry is filled in by the
1814 dynamic linker and used by the debugger. */
1815 #define add_dynamic_entry(TAG, VAL) \
1816 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1818 if (info->executable)
1820 if (!add_dynamic_entry (DT_DEBUG, 0))
1821 return FALSE;
1824 if (relplt)
1826 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1827 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1828 || !add_dynamic_entry (DT_JMPREL, 0))
1829 return FALSE;
1832 if (relgot)
1834 if (!add_dynamic_entry (DT_RELA, 0)
1835 || !add_dynamic_entry (DT_RELASZ, 0)
1836 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1837 return FALSE;
1840 if (!add_dynamic_entry (DT_PLTGOT, 0)
1841 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1842 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1843 return FALSE;
1845 #undef add_dynamic_entry
1847 return TRUE;
1850 static bfd_boolean
1851 elf_xtensa_always_size_sections (bfd *output_bfd,
1852 struct bfd_link_info *info)
1854 struct elf_xtensa_link_hash_table *htab;
1855 asection *tls_sec;
1857 htab = elf_xtensa_hash_table (info);
1858 if (htab == NULL)
1859 return FALSE;
1861 tls_sec = htab->elf.tls_sec;
1863 if (tls_sec && (htab->tlsbase->tls_type & GOT_TLS_ANY) != 0)
1865 struct elf_link_hash_entry *tlsbase = &htab->tlsbase->elf;
1866 struct bfd_link_hash_entry *bh = &tlsbase->root;
1867 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
1869 tlsbase->type = STT_TLS;
1870 if (!(_bfd_generic_link_add_one_symbol
1871 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1872 tls_sec, 0, NULL, FALSE,
1873 bed->collect, &bh)))
1874 return FALSE;
1875 tlsbase->def_regular = 1;
1876 tlsbase->other = STV_HIDDEN;
1877 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1880 return TRUE;
1884 /* Return the base VMA address which should be subtracted from real addresses
1885 when resolving @dtpoff relocation.
1886 This is PT_TLS segment p_vaddr. */
1888 static bfd_vma
1889 dtpoff_base (struct bfd_link_info *info)
1891 /* If tls_sec is NULL, we should have signalled an error already. */
1892 if (elf_hash_table (info)->tls_sec == NULL)
1893 return 0;
1894 return elf_hash_table (info)->tls_sec->vma;
1897 /* Return the relocation value for @tpoff relocation
1898 if STT_TLS virtual address is ADDRESS. */
1900 static bfd_vma
1901 tpoff (struct bfd_link_info *info, bfd_vma address)
1903 struct elf_link_hash_table *htab = elf_hash_table (info);
1904 bfd_vma base;
1906 /* If tls_sec is NULL, we should have signalled an error already. */
1907 if (htab->tls_sec == NULL)
1908 return 0;
1909 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
1910 return address - htab->tls_sec->vma + base;
1913 /* Perform the specified relocation. The instruction at (contents + address)
1914 is modified to set one operand to represent the value in "relocation". The
1915 operand position is determined by the relocation type recorded in the
1916 howto. */
1918 #define CALL_SEGMENT_BITS (30)
1919 #define CALL_SEGMENT_SIZE (1 << CALL_SEGMENT_BITS)
1921 static bfd_reloc_status_type
1922 elf_xtensa_do_reloc (reloc_howto_type *howto,
1923 bfd *abfd,
1924 asection *input_section,
1925 bfd_vma relocation,
1926 bfd_byte *contents,
1927 bfd_vma address,
1928 bfd_boolean is_weak_undef,
1929 char **error_message)
1931 xtensa_format fmt;
1932 xtensa_opcode opcode;
1933 xtensa_isa isa = xtensa_default_isa;
1934 static xtensa_insnbuf ibuff = NULL;
1935 static xtensa_insnbuf sbuff = NULL;
1936 bfd_vma self_address;
1937 bfd_size_type input_size;
1938 int opnd, slot;
1939 uint32 newval;
1941 if (!ibuff)
1943 ibuff = xtensa_insnbuf_alloc (isa);
1944 sbuff = xtensa_insnbuf_alloc (isa);
1947 input_size = bfd_get_section_limit (abfd, input_section);
1949 /* Calculate the PC address for this instruction. */
1950 self_address = (input_section->output_section->vma
1951 + input_section->output_offset
1952 + address);
1954 switch (howto->type)
1956 case R_XTENSA_NONE:
1957 case R_XTENSA_DIFF8:
1958 case R_XTENSA_DIFF16:
1959 case R_XTENSA_DIFF32:
1960 case R_XTENSA_TLS_FUNC:
1961 case R_XTENSA_TLS_ARG:
1962 case R_XTENSA_TLS_CALL:
1963 return bfd_reloc_ok;
1965 case R_XTENSA_ASM_EXPAND:
1966 if (!is_weak_undef)
1968 /* Check for windowed CALL across a 1GB boundary. */
1969 opcode = get_expanded_call_opcode (contents + address,
1970 input_size - address, 0);
1971 if (is_windowed_call_opcode (opcode))
1973 if ((self_address >> CALL_SEGMENT_BITS)
1974 != (relocation >> CALL_SEGMENT_BITS))
1976 *error_message = "windowed longcall crosses 1GB boundary; "
1977 "return may fail";
1978 return bfd_reloc_dangerous;
1982 return bfd_reloc_ok;
1984 case R_XTENSA_ASM_SIMPLIFY:
1986 /* Convert the L32R/CALLX to CALL. */
1987 bfd_reloc_status_type retval =
1988 elf_xtensa_do_asm_simplify (contents, address, input_size,
1989 error_message);
1990 if (retval != bfd_reloc_ok)
1991 return bfd_reloc_dangerous;
1993 /* The CALL needs to be relocated. Continue below for that part. */
1994 address += 3;
1995 self_address += 3;
1996 howto = &elf_howto_table[(unsigned) R_XTENSA_SLOT0_OP ];
1998 break;
2000 case R_XTENSA_32:
2002 bfd_vma x;
2003 x = bfd_get_32 (abfd, contents + address);
2004 x = x + relocation;
2005 bfd_put_32 (abfd, x, contents + address);
2007 return bfd_reloc_ok;
2009 case R_XTENSA_32_PCREL:
2010 bfd_put_32 (abfd, relocation - self_address, contents + address);
2011 return bfd_reloc_ok;
2013 case R_XTENSA_PLT:
2014 case R_XTENSA_TLSDESC_FN:
2015 case R_XTENSA_TLSDESC_ARG:
2016 case R_XTENSA_TLS_DTPOFF:
2017 case R_XTENSA_TLS_TPOFF:
2018 bfd_put_32 (abfd, relocation, contents + address);
2019 return bfd_reloc_ok;
2022 /* Only instruction slot-specific relocations handled below.... */
2023 slot = get_relocation_slot (howto->type);
2024 if (slot == XTENSA_UNDEFINED)
2026 *error_message = "unexpected relocation";
2027 return bfd_reloc_dangerous;
2030 /* Read the instruction into a buffer and decode the opcode. */
2031 xtensa_insnbuf_from_chars (isa, ibuff, contents + address,
2032 input_size - address);
2033 fmt = xtensa_format_decode (isa, ibuff);
2034 if (fmt == XTENSA_UNDEFINED)
2036 *error_message = "cannot decode instruction format";
2037 return bfd_reloc_dangerous;
2040 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
2042 opcode = xtensa_opcode_decode (isa, fmt, slot, sbuff);
2043 if (opcode == XTENSA_UNDEFINED)
2045 *error_message = "cannot decode instruction opcode";
2046 return bfd_reloc_dangerous;
2049 /* Check for opcode-specific "alternate" relocations. */
2050 if (is_alt_relocation (howto->type))
2052 if (opcode == get_l32r_opcode ())
2054 /* Handle the special-case of non-PC-relative L32R instructions. */
2055 bfd *output_bfd = input_section->output_section->owner;
2056 asection *lit4_sec = bfd_get_section_by_name (output_bfd, ".lit4");
2057 if (!lit4_sec)
2059 *error_message = "relocation references missing .lit4 section";
2060 return bfd_reloc_dangerous;
2062 self_address = ((lit4_sec->vma & ~0xfff)
2063 + 0x40000 - 3); /* -3 to compensate for do_reloc */
2064 newval = relocation;
2065 opnd = 1;
2067 else if (opcode == get_const16_opcode ())
2069 /* ALT used for high 16 bits. */
2070 newval = relocation >> 16;
2071 opnd = 1;
2073 else
2075 /* No other "alternate" relocations currently defined. */
2076 *error_message = "unexpected relocation";
2077 return bfd_reloc_dangerous;
2080 else /* Not an "alternate" relocation.... */
2082 if (opcode == get_const16_opcode ())
2084 newval = relocation & 0xffff;
2085 opnd = 1;
2087 else
2089 /* ...normal PC-relative relocation.... */
2091 /* Determine which operand is being relocated. */
2092 opnd = get_relocation_opnd (opcode, howto->type);
2093 if (opnd == XTENSA_UNDEFINED)
2095 *error_message = "unexpected relocation";
2096 return bfd_reloc_dangerous;
2099 if (!howto->pc_relative)
2101 *error_message = "expected PC-relative relocation";
2102 return bfd_reloc_dangerous;
2105 newval = relocation;
2109 /* Apply the relocation. */
2110 if (xtensa_operand_do_reloc (isa, opcode, opnd, &newval, self_address)
2111 || xtensa_operand_encode (isa, opcode, opnd, &newval)
2112 || xtensa_operand_set_field (isa, opcode, opnd, fmt, slot,
2113 sbuff, newval))
2115 const char *opname = xtensa_opcode_name (isa, opcode);
2116 const char *msg;
2118 msg = "cannot encode";
2119 if (is_direct_call_opcode (opcode))
2121 if ((relocation & 0x3) != 0)
2122 msg = "misaligned call target";
2123 else
2124 msg = "call target out of range";
2126 else if (opcode == get_l32r_opcode ())
2128 if ((relocation & 0x3) != 0)
2129 msg = "misaligned literal target";
2130 else if (is_alt_relocation (howto->type))
2131 msg = "literal target out of range (too many literals)";
2132 else if (self_address > relocation)
2133 msg = "literal target out of range (try using text-section-literals)";
2134 else
2135 msg = "literal placed after use";
2138 *error_message = vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
2139 return bfd_reloc_dangerous;
2142 /* Check for calls across 1GB boundaries. */
2143 if (is_direct_call_opcode (opcode)
2144 && is_windowed_call_opcode (opcode))
2146 if ((self_address >> CALL_SEGMENT_BITS)
2147 != (relocation >> CALL_SEGMENT_BITS))
2149 *error_message =
2150 "windowed call crosses 1GB boundary; return may fail";
2151 return bfd_reloc_dangerous;
2155 /* Write the modified instruction back out of the buffer. */
2156 xtensa_format_set_slot (isa, fmt, slot, ibuff, sbuff);
2157 xtensa_insnbuf_to_chars (isa, ibuff, contents + address,
2158 input_size - address);
2159 return bfd_reloc_ok;
2163 static char *
2164 vsprint_msg (const char *origmsg, const char *fmt, int arglen, ...)
2166 /* To reduce the size of the memory leak,
2167 we only use a single message buffer. */
2168 static bfd_size_type alloc_size = 0;
2169 static char *message = NULL;
2170 bfd_size_type orig_len, len = 0;
2171 bfd_boolean is_append;
2173 VA_OPEN (ap, arglen);
2174 VA_FIXEDARG (ap, const char *, origmsg);
2176 is_append = (origmsg == message);
2178 orig_len = strlen (origmsg);
2179 len = orig_len + strlen (fmt) + arglen + 20;
2180 if (len > alloc_size)
2182 message = (char *) bfd_realloc_or_free (message, len);
2183 alloc_size = len;
2185 if (message != NULL)
2187 if (!is_append)
2188 memcpy (message, origmsg, orig_len);
2189 vsprintf (message + orig_len, fmt, ap);
2191 VA_CLOSE (ap);
2192 return message;
2196 /* This function is registered as the "special_function" in the
2197 Xtensa howto for handling simplify operations.
2198 bfd_perform_relocation / bfd_install_relocation use it to
2199 perform (install) the specified relocation. Since this replaces the code
2200 in bfd_perform_relocation, it is basically an Xtensa-specific,
2201 stripped-down version of bfd_perform_relocation. */
2203 static bfd_reloc_status_type
2204 bfd_elf_xtensa_reloc (bfd *abfd,
2205 arelent *reloc_entry,
2206 asymbol *symbol,
2207 void *data,
2208 asection *input_section,
2209 bfd *output_bfd,
2210 char **error_message)
2212 bfd_vma relocation;
2213 bfd_reloc_status_type flag;
2214 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2215 bfd_vma output_base = 0;
2216 reloc_howto_type *howto = reloc_entry->howto;
2217 asection *reloc_target_output_section;
2218 bfd_boolean is_weak_undef;
2220 if (!xtensa_default_isa)
2221 xtensa_default_isa = xtensa_isa_init (0, 0);
2223 /* ELF relocs are against symbols. If we are producing relocatable
2224 output, and the reloc is against an external symbol, the resulting
2225 reloc will also be against the same symbol. In such a case, we
2226 don't want to change anything about the way the reloc is handled,
2227 since it will all be done at final link time. This test is similar
2228 to what bfd_elf_generic_reloc does except that it lets relocs with
2229 howto->partial_inplace go through even if the addend is non-zero.
2230 (The real problem is that partial_inplace is set for XTENSA_32
2231 relocs to begin with, but that's a long story and there's little we
2232 can do about it now....) */
2234 if (output_bfd && (symbol->flags & BSF_SECTION_SYM) == 0)
2236 reloc_entry->address += input_section->output_offset;
2237 return bfd_reloc_ok;
2240 /* Is the address of the relocation really within the section? */
2241 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2242 return bfd_reloc_outofrange;
2244 /* Work out which section the relocation is targeted at and the
2245 initial relocation command value. */
2247 /* Get symbol value. (Common symbols are special.) */
2248 if (bfd_is_com_section (symbol->section))
2249 relocation = 0;
2250 else
2251 relocation = symbol->value;
2253 reloc_target_output_section = symbol->section->output_section;
2255 /* Convert input-section-relative symbol value to absolute. */
2256 if ((output_bfd && !howto->partial_inplace)
2257 || reloc_target_output_section == NULL)
2258 output_base = 0;
2259 else
2260 output_base = reloc_target_output_section->vma;
2262 relocation += output_base + symbol->section->output_offset;
2264 /* Add in supplied addend. */
2265 relocation += reloc_entry->addend;
2267 /* Here the variable relocation holds the final address of the
2268 symbol we are relocating against, plus any addend. */
2269 if (output_bfd)
2271 if (!howto->partial_inplace)
2273 /* This is a partial relocation, and we want to apply the relocation
2274 to the reloc entry rather than the raw data. Everything except
2275 relocations against section symbols has already been handled
2276 above. */
2278 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
2279 reloc_entry->addend = relocation;
2280 reloc_entry->address += input_section->output_offset;
2281 return bfd_reloc_ok;
2283 else
2285 reloc_entry->address += input_section->output_offset;
2286 reloc_entry->addend = 0;
2290 is_weak_undef = (bfd_is_und_section (symbol->section)
2291 && (symbol->flags & BSF_WEAK) != 0);
2292 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
2293 (bfd_byte *) data, (bfd_vma) octets,
2294 is_weak_undef, error_message);
2296 if (flag == bfd_reloc_dangerous)
2298 /* Add the symbol name to the error message. */
2299 if (! *error_message)
2300 *error_message = "";
2301 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
2302 strlen (symbol->name) + 17,
2303 symbol->name,
2304 (unsigned long) reloc_entry->addend);
2307 return flag;
2311 /* Set up an entry in the procedure linkage table. */
2313 static bfd_vma
2314 elf_xtensa_create_plt_entry (struct bfd_link_info *info,
2315 bfd *output_bfd,
2316 unsigned reloc_index)
2318 asection *splt, *sgotplt;
2319 bfd_vma plt_base, got_base;
2320 bfd_vma code_offset, lit_offset;
2321 int chunk;
2323 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
2324 splt = elf_xtensa_get_plt_section (info, chunk);
2325 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
2326 BFD_ASSERT (splt != NULL && sgotplt != NULL);
2328 plt_base = splt->output_section->vma + splt->output_offset;
2329 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2331 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
2332 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
2334 /* Fill in the literal entry. This is the offset of the dynamic
2335 relocation entry. */
2336 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
2337 sgotplt->contents + lit_offset);
2339 /* Fill in the entry in the procedure linkage table. */
2340 memcpy (splt->contents + code_offset,
2341 (bfd_big_endian (output_bfd)
2342 ? elf_xtensa_be_plt_entry
2343 : elf_xtensa_le_plt_entry),
2344 PLT_ENTRY_SIZE);
2345 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
2346 plt_base + code_offset + 3),
2347 splt->contents + code_offset + 4);
2348 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
2349 plt_base + code_offset + 6),
2350 splt->contents + code_offset + 7);
2351 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
2352 plt_base + code_offset + 9),
2353 splt->contents + code_offset + 10);
2355 return plt_base + code_offset;
2359 static bfd_boolean get_indirect_call_dest_reg (xtensa_opcode, unsigned *);
2361 static bfd_boolean
2362 replace_tls_insn (Elf_Internal_Rela *rel,
2363 bfd *abfd,
2364 asection *input_section,
2365 bfd_byte *contents,
2366 bfd_boolean is_ld_model,
2367 char **error_message)
2369 static xtensa_insnbuf ibuff = NULL;
2370 static xtensa_insnbuf sbuff = NULL;
2371 xtensa_isa isa = xtensa_default_isa;
2372 xtensa_format fmt;
2373 xtensa_opcode old_op, new_op;
2374 bfd_size_type input_size;
2375 int r_type;
2376 unsigned dest_reg, src_reg;
2378 if (ibuff == NULL)
2380 ibuff = xtensa_insnbuf_alloc (isa);
2381 sbuff = xtensa_insnbuf_alloc (isa);
2384 input_size = bfd_get_section_limit (abfd, input_section);
2386 /* Read the instruction into a buffer and decode the opcode. */
2387 xtensa_insnbuf_from_chars (isa, ibuff, contents + rel->r_offset,
2388 input_size - rel->r_offset);
2389 fmt = xtensa_format_decode (isa, ibuff);
2390 if (fmt == XTENSA_UNDEFINED)
2392 *error_message = "cannot decode instruction format";
2393 return FALSE;
2396 BFD_ASSERT (xtensa_format_num_slots (isa, fmt) == 1);
2397 xtensa_format_get_slot (isa, fmt, 0, ibuff, sbuff);
2399 old_op = xtensa_opcode_decode (isa, fmt, 0, sbuff);
2400 if (old_op == XTENSA_UNDEFINED)
2402 *error_message = "cannot decode instruction opcode";
2403 return FALSE;
2406 r_type = ELF32_R_TYPE (rel->r_info);
2407 switch (r_type)
2409 case R_XTENSA_TLS_FUNC:
2410 case R_XTENSA_TLS_ARG:
2411 if (old_op != get_l32r_opcode ()
2412 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2413 sbuff, &dest_reg) != 0)
2415 *error_message = "cannot extract L32R destination for TLS access";
2416 return FALSE;
2418 break;
2420 case R_XTENSA_TLS_CALL:
2421 if (! get_indirect_call_dest_reg (old_op, &dest_reg)
2422 || xtensa_operand_get_field (isa, old_op, 0, fmt, 0,
2423 sbuff, &src_reg) != 0)
2425 *error_message = "cannot extract CALLXn operands for TLS access";
2426 return FALSE;
2428 break;
2430 default:
2431 abort ();
2434 if (is_ld_model)
2436 switch (r_type)
2438 case R_XTENSA_TLS_FUNC:
2439 case R_XTENSA_TLS_ARG:
2440 /* Change the instruction to a NOP (or "OR a1, a1, a1" for older
2441 versions of Xtensa). */
2442 new_op = xtensa_opcode_lookup (isa, "nop");
2443 if (new_op == XTENSA_UNDEFINED)
2445 new_op = xtensa_opcode_lookup (isa, "or");
2446 if (new_op == XTENSA_UNDEFINED
2447 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2448 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2449 sbuff, 1) != 0
2450 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2451 sbuff, 1) != 0
2452 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2453 sbuff, 1) != 0)
2455 *error_message = "cannot encode OR for TLS access";
2456 return FALSE;
2459 else
2461 if (xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0)
2463 *error_message = "cannot encode NOP for TLS access";
2464 return FALSE;
2467 break;
2469 case R_XTENSA_TLS_CALL:
2470 /* Read THREADPTR into the CALLX's return value register. */
2471 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2472 if (new_op == XTENSA_UNDEFINED
2473 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2474 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2475 sbuff, dest_reg + 2) != 0)
2477 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2478 return FALSE;
2480 break;
2483 else
2485 switch (r_type)
2487 case R_XTENSA_TLS_FUNC:
2488 new_op = xtensa_opcode_lookup (isa, "rur.threadptr");
2489 if (new_op == XTENSA_UNDEFINED
2490 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2491 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2492 sbuff, dest_reg) != 0)
2494 *error_message = "cannot encode RUR.THREADPTR for TLS access";
2495 return FALSE;
2497 break;
2499 case R_XTENSA_TLS_ARG:
2500 /* Nothing to do. Keep the original L32R instruction. */
2501 return TRUE;
2503 case R_XTENSA_TLS_CALL:
2504 /* Add the CALLX's src register (holding the THREADPTR value)
2505 to the first argument register (holding the offset) and put
2506 the result in the CALLX's return value register. */
2507 new_op = xtensa_opcode_lookup (isa, "add");
2508 if (new_op == XTENSA_UNDEFINED
2509 || xtensa_opcode_encode (isa, fmt, 0, sbuff, new_op) != 0
2510 || xtensa_operand_set_field (isa, new_op, 0, fmt, 0,
2511 sbuff, dest_reg + 2) != 0
2512 || xtensa_operand_set_field (isa, new_op, 1, fmt, 0,
2513 sbuff, dest_reg + 2) != 0
2514 || xtensa_operand_set_field (isa, new_op, 2, fmt, 0,
2515 sbuff, src_reg) != 0)
2517 *error_message = "cannot encode ADD for TLS access";
2518 return FALSE;
2520 break;
2524 xtensa_format_set_slot (isa, fmt, 0, ibuff, sbuff);
2525 xtensa_insnbuf_to_chars (isa, ibuff, contents + rel->r_offset,
2526 input_size - rel->r_offset);
2528 return TRUE;
2532 #define IS_XTENSA_TLS_RELOC(R_TYPE) \
2533 ((R_TYPE) == R_XTENSA_TLSDESC_FN \
2534 || (R_TYPE) == R_XTENSA_TLSDESC_ARG \
2535 || (R_TYPE) == R_XTENSA_TLS_DTPOFF \
2536 || (R_TYPE) == R_XTENSA_TLS_TPOFF \
2537 || (R_TYPE) == R_XTENSA_TLS_FUNC \
2538 || (R_TYPE) == R_XTENSA_TLS_ARG \
2539 || (R_TYPE) == R_XTENSA_TLS_CALL)
2541 /* Relocate an Xtensa ELF section. This is invoked by the linker for
2542 both relocatable and final links. */
2544 static bfd_boolean
2545 elf_xtensa_relocate_section (bfd *output_bfd,
2546 struct bfd_link_info *info,
2547 bfd *input_bfd,
2548 asection *input_section,
2549 bfd_byte *contents,
2550 Elf_Internal_Rela *relocs,
2551 Elf_Internal_Sym *local_syms,
2552 asection **local_sections)
2554 struct elf_xtensa_link_hash_table *htab;
2555 Elf_Internal_Shdr *symtab_hdr;
2556 Elf_Internal_Rela *rel;
2557 Elf_Internal_Rela *relend;
2558 struct elf_link_hash_entry **sym_hashes;
2559 property_table_entry *lit_table = 0;
2560 int ltblsize = 0;
2561 char *local_got_tls_types;
2562 char *error_message = NULL;
2563 bfd_size_type input_size;
2564 int tls_type;
2566 if (!xtensa_default_isa)
2567 xtensa_default_isa = xtensa_isa_init (0, 0);
2569 BFD_ASSERT (is_xtensa_elf (input_bfd));
2571 htab = elf_xtensa_hash_table (info);
2572 if (htab == NULL)
2573 return FALSE;
2575 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2576 sym_hashes = elf_sym_hashes (input_bfd);
2577 local_got_tls_types = elf_xtensa_local_got_tls_type (input_bfd);
2579 if (elf_hash_table (info)->dynamic_sections_created)
2581 ltblsize = xtensa_read_table_entries (input_bfd, input_section,
2582 &lit_table, XTENSA_LIT_SEC_NAME,
2583 TRUE);
2584 if (ltblsize < 0)
2585 return FALSE;
2588 input_size = bfd_get_section_limit (input_bfd, input_section);
2590 rel = relocs;
2591 relend = relocs + input_section->reloc_count;
2592 for (; rel < relend; rel++)
2594 int r_type;
2595 reloc_howto_type *howto;
2596 unsigned long r_symndx;
2597 struct elf_link_hash_entry *h;
2598 Elf_Internal_Sym *sym;
2599 char sym_type;
2600 const char *name;
2601 asection *sec;
2602 bfd_vma relocation;
2603 bfd_reloc_status_type r;
2604 bfd_boolean is_weak_undef;
2605 bfd_boolean unresolved_reloc;
2606 bfd_boolean warned;
2607 bfd_boolean dynamic_symbol;
2609 r_type = ELF32_R_TYPE (rel->r_info);
2610 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
2611 || r_type == (int) R_XTENSA_GNU_VTENTRY)
2612 continue;
2614 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
2616 bfd_set_error (bfd_error_bad_value);
2617 return FALSE;
2619 howto = &elf_howto_table[r_type];
2621 r_symndx = ELF32_R_SYM (rel->r_info);
2623 h = NULL;
2624 sym = NULL;
2625 sec = NULL;
2626 is_weak_undef = FALSE;
2627 unresolved_reloc = FALSE;
2628 warned = FALSE;
2630 if (howto->partial_inplace && !info->relocatable)
2632 /* Because R_XTENSA_32 was made partial_inplace to fix some
2633 problems with DWARF info in partial links, there may be
2634 an addend stored in the contents. Take it out of there
2635 and move it back into the addend field of the reloc. */
2636 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
2637 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
2640 if (r_symndx < symtab_hdr->sh_info)
2642 sym = local_syms + r_symndx;
2643 sym_type = ELF32_ST_TYPE (sym->st_info);
2644 sec = local_sections[r_symndx];
2645 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2647 else
2649 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2650 r_symndx, symtab_hdr, sym_hashes,
2651 h, sec, relocation,
2652 unresolved_reloc, warned);
2654 if (relocation == 0
2655 && !unresolved_reloc
2656 && h->root.type == bfd_link_hash_undefweak)
2657 is_weak_undef = TRUE;
2659 sym_type = h->type;
2662 if (sec != NULL && elf_discarded_section (sec))
2664 /* For relocs against symbols from removed linkonce sections,
2665 or sections discarded by a linker script, we just want the
2666 section contents zeroed. Avoid any special processing. */
2667 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2668 rel->r_info = 0;
2669 rel->r_addend = 0;
2670 continue;
2673 if (info->relocatable)
2675 /* This is a relocatable link.
2676 1) If the reloc is against a section symbol, adjust
2677 according to the output section.
2678 2) If there is a new target for this relocation,
2679 the new target will be in the same output section.
2680 We adjust the relocation by the output section
2681 difference. */
2683 if (relaxing_section)
2685 /* Check if this references a section in another input file. */
2686 if (!do_fix_for_relocatable_link (rel, input_bfd, input_section,
2687 contents))
2688 return FALSE;
2691 if (r_type == R_XTENSA_ASM_SIMPLIFY)
2693 error_message = NULL;
2694 /* Convert ASM_SIMPLIFY into the simpler relocation
2695 so that they never escape a relaxing link. */
2696 r = contract_asm_expansion (contents, input_size, rel,
2697 &error_message);
2698 if (r != bfd_reloc_ok)
2700 if (!((*info->callbacks->reloc_dangerous)
2701 (info, error_message, input_bfd, input_section,
2702 rel->r_offset)))
2703 return FALSE;
2705 r_type = ELF32_R_TYPE (rel->r_info);
2708 /* This is a relocatable link, so we don't have to change
2709 anything unless the reloc is against a section symbol,
2710 in which case we have to adjust according to where the
2711 section symbol winds up in the output section. */
2712 if (r_symndx < symtab_hdr->sh_info)
2714 sym = local_syms + r_symndx;
2715 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2717 sec = local_sections[r_symndx];
2718 rel->r_addend += sec->output_offset + sym->st_value;
2722 /* If there is an addend with a partial_inplace howto,
2723 then move the addend to the contents. This is a hack
2724 to work around problems with DWARF in relocatable links
2725 with some previous version of BFD. Now we can't easily get
2726 rid of the hack without breaking backward compatibility.... */
2727 if (rel->r_addend)
2729 howto = &elf_howto_table[r_type];
2730 if (howto->partial_inplace)
2732 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2733 rel->r_addend, contents,
2734 rel->r_offset, FALSE,
2735 &error_message);
2736 if (r != bfd_reloc_ok)
2738 if (!((*info->callbacks->reloc_dangerous)
2739 (info, error_message, input_bfd, input_section,
2740 rel->r_offset)))
2741 return FALSE;
2743 rel->r_addend = 0;
2747 /* Done with work for relocatable link; continue with next reloc. */
2748 continue;
2751 /* This is a final link. */
2753 if (relaxing_section)
2755 /* Check if this references a section in another input file. */
2756 do_fix_for_final_link (rel, input_bfd, input_section, contents,
2757 &relocation);
2760 /* Sanity check the address. */
2761 if (rel->r_offset >= input_size
2762 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2764 (*_bfd_error_handler)
2765 (_("%B(%A+0x%lx): relocation offset out of range (size=0x%x)"),
2766 input_bfd, input_section, rel->r_offset, input_size);
2767 bfd_set_error (bfd_error_bad_value);
2768 return FALSE;
2771 if (h != NULL)
2772 name = h->root.root.string;
2773 else
2775 name = (bfd_elf_string_from_elf_section
2776 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2777 if (name == NULL || *name == '\0')
2778 name = bfd_section_name (input_bfd, sec);
2781 if (r_symndx != 0
2782 && r_type != R_XTENSA_NONE
2783 && (h == NULL
2784 || h->root.type == bfd_link_hash_defined
2785 || h->root.type == bfd_link_hash_defweak)
2786 && IS_XTENSA_TLS_RELOC (r_type) != (sym_type == STT_TLS))
2788 (*_bfd_error_handler)
2789 ((sym_type == STT_TLS
2790 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
2791 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
2792 input_bfd,
2793 input_section,
2794 (long) rel->r_offset,
2795 howto->name,
2796 name);
2799 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
2801 tls_type = GOT_UNKNOWN;
2802 if (h)
2803 tls_type = elf_xtensa_hash_entry (h)->tls_type;
2804 else if (local_got_tls_types)
2805 tls_type = local_got_tls_types [r_symndx];
2807 switch (r_type)
2809 case R_XTENSA_32:
2810 case R_XTENSA_PLT:
2811 if (elf_hash_table (info)->dynamic_sections_created
2812 && (input_section->flags & SEC_ALLOC) != 0
2813 && (dynamic_symbol || info->shared))
2815 Elf_Internal_Rela outrel;
2816 bfd_byte *loc;
2817 asection *srel;
2819 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2820 srel = htab->srelplt;
2821 else
2822 srel = htab->srelgot;
2824 BFD_ASSERT (srel != NULL);
2826 outrel.r_offset =
2827 _bfd_elf_section_offset (output_bfd, info,
2828 input_section, rel->r_offset);
2830 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2831 memset (&outrel, 0, sizeof outrel);
2832 else
2834 outrel.r_offset += (input_section->output_section->vma
2835 + input_section->output_offset);
2837 /* Complain if the relocation is in a read-only section
2838 and not in a literal pool. */
2839 if ((input_section->flags & SEC_READONLY) != 0
2840 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
2841 outrel.r_offset))
2843 error_message =
2844 _("dynamic relocation in read-only section");
2845 if (!((*info->callbacks->reloc_dangerous)
2846 (info, error_message, input_bfd, input_section,
2847 rel->r_offset)))
2848 return FALSE;
2851 if (dynamic_symbol)
2853 outrel.r_addend = rel->r_addend;
2854 rel->r_addend = 0;
2856 if (r_type == R_XTENSA_32)
2858 outrel.r_info =
2859 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2860 relocation = 0;
2862 else /* r_type == R_XTENSA_PLT */
2864 outrel.r_info =
2865 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2867 /* Create the PLT entry and set the initial
2868 contents of the literal entry to the address of
2869 the PLT entry. */
2870 relocation =
2871 elf_xtensa_create_plt_entry (info, output_bfd,
2872 srel->reloc_count);
2874 unresolved_reloc = FALSE;
2876 else
2878 /* Generate a RELATIVE relocation. */
2879 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2880 outrel.r_addend = 0;
2884 loc = (srel->contents
2885 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2886 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2887 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2888 <= srel->size);
2890 else if (r_type == R_XTENSA_ASM_EXPAND && dynamic_symbol)
2892 /* This should only happen for non-PIC code, which is not
2893 supposed to be used on systems with dynamic linking.
2894 Just ignore these relocations. */
2895 continue;
2897 break;
2899 case R_XTENSA_TLS_TPOFF:
2900 /* Switch to LE model for local symbols in an executable. */
2901 if (! info->shared && ! dynamic_symbol)
2903 relocation = tpoff (info, relocation);
2904 break;
2906 /* fall through */
2908 case R_XTENSA_TLSDESC_FN:
2909 case R_XTENSA_TLSDESC_ARG:
2911 if (r_type == R_XTENSA_TLSDESC_FN)
2913 if (! info->shared || (tls_type & GOT_TLS_IE) != 0)
2914 r_type = R_XTENSA_NONE;
2916 else if (r_type == R_XTENSA_TLSDESC_ARG)
2918 if (info->shared)
2920 if ((tls_type & GOT_TLS_IE) != 0)
2921 r_type = R_XTENSA_TLS_TPOFF;
2923 else
2925 r_type = R_XTENSA_TLS_TPOFF;
2926 if (! dynamic_symbol)
2928 relocation = tpoff (info, relocation);
2929 break;
2934 if (r_type == R_XTENSA_NONE)
2935 /* Nothing to do here; skip to the next reloc. */
2936 continue;
2938 if (! elf_hash_table (info)->dynamic_sections_created)
2940 error_message =
2941 _("TLS relocation invalid without dynamic sections");
2942 if (!((*info->callbacks->reloc_dangerous)
2943 (info, error_message, input_bfd, input_section,
2944 rel->r_offset)))
2945 return FALSE;
2947 else
2949 Elf_Internal_Rela outrel;
2950 bfd_byte *loc;
2951 asection *srel = htab->srelgot;
2952 int indx;
2954 outrel.r_offset = (input_section->output_section->vma
2955 + input_section->output_offset
2956 + rel->r_offset);
2958 /* Complain if the relocation is in a read-only section
2959 and not in a literal pool. */
2960 if ((input_section->flags & SEC_READONLY) != 0
2961 && ! elf_xtensa_in_literal_pool (lit_table, ltblsize,
2962 outrel.r_offset))
2964 error_message =
2965 _("dynamic relocation in read-only section");
2966 if (!((*info->callbacks->reloc_dangerous)
2967 (info, error_message, input_bfd, input_section,
2968 rel->r_offset)))
2969 return FALSE;
2972 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2973 if (indx == 0)
2974 outrel.r_addend = relocation - dtpoff_base (info);
2975 else
2976 outrel.r_addend = 0;
2977 rel->r_addend = 0;
2979 outrel.r_info = ELF32_R_INFO (indx, r_type);
2980 relocation = 0;
2981 unresolved_reloc = FALSE;
2983 BFD_ASSERT (srel);
2984 loc = (srel->contents
2985 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2986 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2987 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2988 <= srel->size);
2991 break;
2993 case R_XTENSA_TLS_DTPOFF:
2994 if (! info->shared)
2995 /* Switch from LD model to LE model. */
2996 relocation = tpoff (info, relocation);
2997 else
2998 relocation -= dtpoff_base (info);
2999 break;
3001 case R_XTENSA_TLS_FUNC:
3002 case R_XTENSA_TLS_ARG:
3003 case R_XTENSA_TLS_CALL:
3004 /* Check if optimizing to IE or LE model. */
3005 if ((tls_type & GOT_TLS_IE) != 0)
3007 bfd_boolean is_ld_model =
3008 (h && elf_xtensa_hash_entry (h) == htab->tlsbase);
3009 if (! replace_tls_insn (rel, input_bfd, input_section, contents,
3010 is_ld_model, &error_message))
3012 if (!((*info->callbacks->reloc_dangerous)
3013 (info, error_message, input_bfd, input_section,
3014 rel->r_offset)))
3015 return FALSE;
3018 if (r_type != R_XTENSA_TLS_ARG || is_ld_model)
3020 /* Skip subsequent relocations on the same instruction. */
3021 while (rel + 1 < relend && rel[1].r_offset == rel->r_offset)
3022 rel++;
3025 continue;
3027 default:
3028 if (elf_hash_table (info)->dynamic_sections_created
3029 && dynamic_symbol && (is_operand_relocation (r_type)
3030 || r_type == R_XTENSA_32_PCREL))
3032 error_message =
3033 vsprint_msg ("invalid relocation for dynamic symbol", ": %s",
3034 strlen (name) + 2, name);
3035 if (!((*info->callbacks->reloc_dangerous)
3036 (info, error_message, input_bfd, input_section,
3037 rel->r_offset)))
3038 return FALSE;
3039 continue;
3041 break;
3044 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3045 because such sections are not SEC_ALLOC and thus ld.so will
3046 not process them. */
3047 if (unresolved_reloc
3048 && !((input_section->flags & SEC_DEBUGGING) != 0
3049 && h->def_dynamic))
3051 (*_bfd_error_handler)
3052 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3053 input_bfd,
3054 input_section,
3055 (long) rel->r_offset,
3056 howto->name,
3057 name);
3058 return FALSE;
3061 /* TLS optimizations may have changed r_type; update "howto". */
3062 howto = &elf_howto_table[r_type];
3064 /* There's no point in calling bfd_perform_relocation here.
3065 Just go directly to our "special function". */
3066 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
3067 relocation + rel->r_addend,
3068 contents, rel->r_offset, is_weak_undef,
3069 &error_message);
3071 if (r != bfd_reloc_ok && !warned)
3073 BFD_ASSERT (r == bfd_reloc_dangerous || r == bfd_reloc_other);
3074 BFD_ASSERT (error_message != NULL);
3076 if (rel->r_addend == 0)
3077 error_message = vsprint_msg (error_message, ": %s",
3078 strlen (name) + 2, name);
3079 else
3080 error_message = vsprint_msg (error_message, ": (%s+0x%x)",
3081 strlen (name) + 22,
3082 name, (int) rel->r_addend);
3084 if (!((*info->callbacks->reloc_dangerous)
3085 (info, error_message, input_bfd, input_section,
3086 rel->r_offset)))
3087 return FALSE;
3091 if (lit_table)
3092 free (lit_table);
3094 input_section->reloc_done = TRUE;
3096 return TRUE;
3100 /* Finish up dynamic symbol handling. There's not much to do here since
3101 the PLT and GOT entries are all set up by relocate_section. */
3103 static bfd_boolean
3104 elf_xtensa_finish_dynamic_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
3105 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3106 struct elf_link_hash_entry *h,
3107 Elf_Internal_Sym *sym)
3109 if (h->needs_plt && !h->def_regular)
3111 /* Mark the symbol as undefined, rather than as defined in
3112 the .plt section. Leave the value alone. */
3113 sym->st_shndx = SHN_UNDEF;
3114 /* If the symbol is weak, we do need to clear the value.
3115 Otherwise, the PLT entry would provide a definition for
3116 the symbol even if the symbol wasn't defined anywhere,
3117 and so the symbol would never be NULL. */
3118 if (!h->ref_regular_nonweak)
3119 sym->st_value = 0;
3122 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3123 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3124 || h == elf_hash_table (info)->hgot)
3125 sym->st_shndx = SHN_ABS;
3127 return TRUE;
3131 /* Combine adjacent literal table entries in the output. Adjacent
3132 entries within each input section may have been removed during
3133 relaxation, but we repeat the process here, even though it's too late
3134 to shrink the output section, because it's important to minimize the
3135 number of literal table entries to reduce the start-up work for the
3136 runtime linker. Returns the number of remaining table entries or -1
3137 on error. */
3139 static int
3140 elf_xtensa_combine_prop_entries (bfd *output_bfd,
3141 asection *sxtlit,
3142 asection *sgotloc)
3144 bfd_byte *contents;
3145 property_table_entry *table;
3146 bfd_size_type section_size, sgotloc_size;
3147 bfd_vma offset;
3148 int n, m, num;
3150 section_size = sxtlit->size;
3151 BFD_ASSERT (section_size % 8 == 0);
3152 num = section_size / 8;
3154 sgotloc_size = sgotloc->size;
3155 if (sgotloc_size != section_size)
3157 (*_bfd_error_handler)
3158 (_("internal inconsistency in size of .got.loc section"));
3159 return -1;
3162 table = bfd_malloc (num * sizeof (property_table_entry));
3163 if (table == 0)
3164 return -1;
3166 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
3167 propagates to the output section, where it doesn't really apply and
3168 where it breaks the following call to bfd_malloc_and_get_section. */
3169 sxtlit->flags &= ~SEC_IN_MEMORY;
3171 if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
3173 if (contents != 0)
3174 free (contents);
3175 free (table);
3176 return -1;
3179 /* There should never be any relocations left at this point, so this
3180 is quite a bit easier than what is done during relaxation. */
3182 /* Copy the raw contents into a property table array and sort it. */
3183 offset = 0;
3184 for (n = 0; n < num; n++)
3186 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
3187 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
3188 offset += 8;
3190 qsort (table, num, sizeof (property_table_entry), property_table_compare);
3192 for (n = 0; n < num; n++)
3194 bfd_boolean remove_entry = FALSE;
3196 if (table[n].size == 0)
3197 remove_entry = TRUE;
3198 else if (n > 0
3199 && (table[n-1].address + table[n-1].size == table[n].address))
3201 table[n-1].size += table[n].size;
3202 remove_entry = TRUE;
3205 if (remove_entry)
3207 for (m = n; m < num - 1; m++)
3209 table[m].address = table[m+1].address;
3210 table[m].size = table[m+1].size;
3213 n--;
3214 num--;
3218 /* Copy the data back to the raw contents. */
3219 offset = 0;
3220 for (n = 0; n < num; n++)
3222 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
3223 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
3224 offset += 8;
3227 /* Clear the removed bytes. */
3228 if ((bfd_size_type) (num * 8) < section_size)
3229 memset (&contents[num * 8], 0, section_size - num * 8);
3231 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
3232 section_size))
3233 return -1;
3235 /* Copy the contents to ".got.loc". */
3236 memcpy (sgotloc->contents, contents, section_size);
3238 free (contents);
3239 free (table);
3240 return num;
3244 /* Finish up the dynamic sections. */
3246 static bfd_boolean
3247 elf_xtensa_finish_dynamic_sections (bfd *output_bfd,
3248 struct bfd_link_info *info)
3250 struct elf_xtensa_link_hash_table *htab;
3251 bfd *dynobj;
3252 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
3253 Elf32_External_Dyn *dyncon, *dynconend;
3254 int num_xtlit_entries = 0;
3256 if (! elf_hash_table (info)->dynamic_sections_created)
3257 return TRUE;
3259 htab = elf_xtensa_hash_table (info);
3260 if (htab == NULL)
3261 return FALSE;
3263 dynobj = elf_hash_table (info)->dynobj;
3264 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3265 BFD_ASSERT (sdyn != NULL);
3267 /* Set the first entry in the global offset table to the address of
3268 the dynamic section. */
3269 sgot = htab->sgot;
3270 if (sgot)
3272 BFD_ASSERT (sgot->size == 4);
3273 if (sdyn == NULL)
3274 bfd_put_32 (output_bfd, 0, sgot->contents);
3275 else
3276 bfd_put_32 (output_bfd,
3277 sdyn->output_section->vma + sdyn->output_offset,
3278 sgot->contents);
3281 srelplt = htab->srelplt;
3282 if (srelplt && srelplt->size != 0)
3284 asection *sgotplt, *srelgot, *spltlittbl;
3285 int chunk, plt_chunks, plt_entries;
3286 Elf_Internal_Rela irela;
3287 bfd_byte *loc;
3288 unsigned rtld_reloc;
3290 srelgot = htab->srelgot;
3291 spltlittbl = htab->spltlittbl;
3292 BFD_ASSERT (srelgot != NULL && spltlittbl != NULL);
3294 /* Find the first XTENSA_RTLD relocation. Presumably the rest
3295 of them follow immediately after.... */
3296 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
3298 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3299 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3300 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
3301 break;
3303 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
3305 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
3306 plt_chunks =
3307 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
3309 for (chunk = 0; chunk < plt_chunks; chunk++)
3311 int chunk_entries = 0;
3313 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
3314 BFD_ASSERT (sgotplt != NULL);
3316 /* Emit special RTLD relocations for the first two entries in
3317 each chunk of the .got.plt section. */
3319 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
3320 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3321 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3322 irela.r_offset = (sgotplt->output_section->vma
3323 + sgotplt->output_offset);
3324 irela.r_addend = 1; /* tell rtld to set value to resolver function */
3325 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3326 rtld_reloc += 1;
3327 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3329 /* Next literal immediately follows the first. */
3330 loc += sizeof (Elf32_External_Rela);
3331 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
3332 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
3333 irela.r_offset = (sgotplt->output_section->vma
3334 + sgotplt->output_offset + 4);
3335 /* Tell rtld to set value to object's link map. */
3336 irela.r_addend = 2;
3337 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
3338 rtld_reloc += 1;
3339 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
3341 /* Fill in the literal table. */
3342 if (chunk < plt_chunks - 1)
3343 chunk_entries = PLT_ENTRIES_PER_CHUNK;
3344 else
3345 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
3347 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->size);
3348 bfd_put_32 (output_bfd,
3349 sgotplt->output_section->vma + sgotplt->output_offset,
3350 spltlittbl->contents + (chunk * 8) + 0);
3351 bfd_put_32 (output_bfd,
3352 8 + (chunk_entries * 4),
3353 spltlittbl->contents + (chunk * 8) + 4);
3356 /* All the dynamic relocations have been emitted at this point.
3357 Make sure the relocation sections are the correct size. */
3358 if (srelgot->size != (sizeof (Elf32_External_Rela)
3359 * srelgot->reloc_count)
3360 || srelplt->size != (sizeof (Elf32_External_Rela)
3361 * srelplt->reloc_count))
3362 abort ();
3364 /* The .xt.lit.plt section has just been modified. This must
3365 happen before the code below which combines adjacent literal
3366 table entries, and the .xt.lit.plt contents have to be forced to
3367 the output here. */
3368 if (! bfd_set_section_contents (output_bfd,
3369 spltlittbl->output_section,
3370 spltlittbl->contents,
3371 spltlittbl->output_offset,
3372 spltlittbl->size))
3373 return FALSE;
3374 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
3375 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
3378 /* Combine adjacent literal table entries. */
3379 BFD_ASSERT (! info->relocatable);
3380 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
3381 sgotloc = htab->sgotloc;
3382 BFD_ASSERT (sgotloc);
3383 if (sxtlit)
3385 num_xtlit_entries =
3386 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
3387 if (num_xtlit_entries < 0)
3388 return FALSE;
3391 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3392 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3393 for (; dyncon < dynconend; dyncon++)
3395 Elf_Internal_Dyn dyn;
3397 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3399 switch (dyn.d_tag)
3401 default:
3402 break;
3404 case DT_XTENSA_GOT_LOC_SZ:
3405 dyn.d_un.d_val = num_xtlit_entries;
3406 break;
3408 case DT_XTENSA_GOT_LOC_OFF:
3409 dyn.d_un.d_ptr = htab->sgotloc->output_section->vma;
3410 break;
3412 case DT_PLTGOT:
3413 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
3414 break;
3416 case DT_JMPREL:
3417 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3418 break;
3420 case DT_PLTRELSZ:
3421 dyn.d_un.d_val = htab->srelplt->output_section->size;
3422 break;
3424 case DT_RELASZ:
3425 /* Adjust RELASZ to not include JMPREL. This matches what
3426 glibc expects and what is done for several other ELF
3427 targets (e.g., i386, alpha), but the "correct" behavior
3428 seems to be unresolved. Since the linker script arranges
3429 for .rela.plt to follow all other relocation sections, we
3430 don't have to worry about changing the DT_RELA entry. */
3431 if (htab->srelplt)
3432 dyn.d_un.d_val -= htab->srelplt->output_section->size;
3433 break;
3436 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3439 return TRUE;
3443 /* Functions for dealing with the e_flags field. */
3445 /* Merge backend specific data from an object file to the output
3446 object file when linking. */
3448 static bfd_boolean
3449 elf_xtensa_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3451 unsigned out_mach, in_mach;
3452 flagword out_flag, in_flag;
3454 /* Check if we have the same endianess. */
3455 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
3456 return FALSE;
3458 /* Don't even pretend to support mixed-format linking. */
3459 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3460 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3461 return FALSE;
3463 out_flag = elf_elfheader (obfd)->e_flags;
3464 in_flag = elf_elfheader (ibfd)->e_flags;
3466 out_mach = out_flag & EF_XTENSA_MACH;
3467 in_mach = in_flag & EF_XTENSA_MACH;
3468 if (out_mach != in_mach)
3470 (*_bfd_error_handler)
3471 (_("%B: incompatible machine type. Output is 0x%x. Input is 0x%x"),
3472 ibfd, out_mach, in_mach);
3473 bfd_set_error (bfd_error_wrong_format);
3474 return FALSE;
3477 if (! elf_flags_init (obfd))
3479 elf_flags_init (obfd) = TRUE;
3480 elf_elfheader (obfd)->e_flags = in_flag;
3482 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3483 && bfd_get_arch_info (obfd)->the_default)
3484 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3485 bfd_get_mach (ibfd));
3487 return TRUE;
3490 if ((out_flag & EF_XTENSA_XT_INSN) != (in_flag & EF_XTENSA_XT_INSN))
3491 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
3493 if ((out_flag & EF_XTENSA_XT_LIT) != (in_flag & EF_XTENSA_XT_LIT))
3494 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
3496 return TRUE;
3500 static bfd_boolean
3501 elf_xtensa_set_private_flags (bfd *abfd, flagword flags)
3503 BFD_ASSERT (!elf_flags_init (abfd)
3504 || elf_elfheader (abfd)->e_flags == flags);
3506 elf_elfheader (abfd)->e_flags |= flags;
3507 elf_flags_init (abfd) = TRUE;
3509 return TRUE;
3513 static bfd_boolean
3514 elf_xtensa_print_private_bfd_data (bfd *abfd, void *farg)
3516 FILE *f = (FILE *) farg;
3517 flagword e_flags = elf_elfheader (abfd)->e_flags;
3519 fprintf (f, "\nXtensa header:\n");
3520 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
3521 fprintf (f, "\nMachine = Base\n");
3522 else
3523 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
3525 fprintf (f, "Insn tables = %s\n",
3526 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
3528 fprintf (f, "Literal tables = %s\n",
3529 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
3531 return _bfd_elf_print_private_bfd_data (abfd, farg);
3535 /* Set the right machine number for an Xtensa ELF file. */
3537 static bfd_boolean
3538 elf_xtensa_object_p (bfd *abfd)
3540 int mach;
3541 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
3543 switch (arch)
3545 case E_XTENSA_MACH:
3546 mach = bfd_mach_xtensa;
3547 break;
3548 default:
3549 return FALSE;
3552 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
3553 return TRUE;
3557 /* The final processing done just before writing out an Xtensa ELF object
3558 file. This gets the Xtensa architecture right based on the machine
3559 number. */
3561 static void
3562 elf_xtensa_final_write_processing (bfd *abfd,
3563 bfd_boolean linker ATTRIBUTE_UNUSED)
3565 int mach;
3566 unsigned long val;
3568 switch (mach = bfd_get_mach (abfd))
3570 case bfd_mach_xtensa:
3571 val = E_XTENSA_MACH;
3572 break;
3573 default:
3574 return;
3577 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
3578 elf_elfheader (abfd)->e_flags |= val;
3582 static enum elf_reloc_type_class
3583 elf_xtensa_reloc_type_class (const Elf_Internal_Rela *rela)
3585 switch ((int) ELF32_R_TYPE (rela->r_info))
3587 case R_XTENSA_RELATIVE:
3588 return reloc_class_relative;
3589 case R_XTENSA_JMP_SLOT:
3590 return reloc_class_plt;
3591 default:
3592 return reloc_class_normal;
3597 static bfd_boolean
3598 elf_xtensa_discard_info_for_section (bfd *abfd,
3599 struct elf_reloc_cookie *cookie,
3600 struct bfd_link_info *info,
3601 asection *sec)
3603 bfd_byte *contents;
3604 bfd_vma offset, actual_offset;
3605 bfd_size_type removed_bytes = 0;
3606 bfd_size_type entry_size;
3608 if (sec->output_section
3609 && bfd_is_abs_section (sec->output_section))
3610 return FALSE;
3612 if (xtensa_is_proptable_section (sec))
3613 entry_size = 12;
3614 else
3615 entry_size = 8;
3617 if (sec->size == 0 || sec->size % entry_size != 0)
3618 return FALSE;
3620 contents = retrieve_contents (abfd, sec, info->keep_memory);
3621 if (!contents)
3622 return FALSE;
3624 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
3625 if (!cookie->rels)
3627 release_contents (sec, contents);
3628 return FALSE;
3631 /* Sort the relocations. They should already be in order when
3632 relaxation is enabled, but it might not be. */
3633 qsort (cookie->rels, sec->reloc_count, sizeof (Elf_Internal_Rela),
3634 internal_reloc_compare);
3636 cookie->rel = cookie->rels;
3637 cookie->relend = cookie->rels + sec->reloc_count;
3639 for (offset = 0; offset < sec->size; offset += entry_size)
3641 actual_offset = offset - removed_bytes;
3643 /* The ...symbol_deleted_p function will skip over relocs but it
3644 won't adjust their offsets, so do that here. */
3645 while (cookie->rel < cookie->relend
3646 && cookie->rel->r_offset < offset)
3648 cookie->rel->r_offset -= removed_bytes;
3649 cookie->rel++;
3652 while (cookie->rel < cookie->relend
3653 && cookie->rel->r_offset == offset)
3655 if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
3657 /* Remove the table entry. (If the reloc type is NONE, then
3658 the entry has already been merged with another and deleted
3659 during relaxation.) */
3660 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
3662 /* Shift the contents up. */
3663 if (offset + entry_size < sec->size)
3664 memmove (&contents[actual_offset],
3665 &contents[actual_offset + entry_size],
3666 sec->size - offset - entry_size);
3667 removed_bytes += entry_size;
3670 /* Remove this relocation. */
3671 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
3674 /* Adjust the relocation offset for previous removals. This
3675 should not be done before calling ...symbol_deleted_p
3676 because it might mess up the offset comparisons there.
3677 Make sure the offset doesn't underflow in the case where
3678 the first entry is removed. */
3679 if (cookie->rel->r_offset >= removed_bytes)
3680 cookie->rel->r_offset -= removed_bytes;
3681 else
3682 cookie->rel->r_offset = 0;
3684 cookie->rel++;
3688 if (removed_bytes != 0)
3690 /* Adjust any remaining relocs (shouldn't be any). */
3691 for (; cookie->rel < cookie->relend; cookie->rel++)
3693 if (cookie->rel->r_offset >= removed_bytes)
3694 cookie->rel->r_offset -= removed_bytes;
3695 else
3696 cookie->rel->r_offset = 0;
3699 /* Clear the removed bytes. */
3700 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
3702 pin_contents (sec, contents);
3703 pin_internal_relocs (sec, cookie->rels);
3705 /* Shrink size. */
3706 if (sec->rawsize == 0)
3707 sec->rawsize = sec->size;
3708 sec->size -= removed_bytes;
3710 if (xtensa_is_littable_section (sec))
3712 asection *sgotloc = elf_xtensa_hash_table (info)->sgotloc;
3713 if (sgotloc)
3714 sgotloc->size -= removed_bytes;
3717 else
3719 release_contents (sec, contents);
3720 release_internal_relocs (sec, cookie->rels);
3723 return (removed_bytes != 0);
3727 static bfd_boolean
3728 elf_xtensa_discard_info (bfd *abfd,
3729 struct elf_reloc_cookie *cookie,
3730 struct bfd_link_info *info)
3732 asection *sec;
3733 bfd_boolean changed = FALSE;
3735 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3737 if (xtensa_is_property_section (sec))
3739 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
3740 changed = TRUE;
3744 return changed;
3748 static bfd_boolean
3749 elf_xtensa_ignore_discarded_relocs (asection *sec)
3751 return xtensa_is_property_section (sec);
3755 static unsigned int
3756 elf_xtensa_action_discarded (asection *sec)
3758 if (strcmp (".xt_except_table", sec->name) == 0)
3759 return 0;
3761 if (strcmp (".xt_except_desc", sec->name) == 0)
3762 return 0;
3764 return _bfd_elf_default_action_discarded (sec);
3768 /* Support for core dump NOTE sections. */
3770 static bfd_boolean
3771 elf_xtensa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3773 int offset;
3774 unsigned int size;
3776 /* The size for Xtensa is variable, so don't try to recognize the format
3777 based on the size. Just assume this is GNU/Linux. */
3779 /* pr_cursig */
3780 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3782 /* pr_pid */
3783 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3785 /* pr_reg */
3786 offset = 72;
3787 size = note->descsz - offset - 4;
3789 /* Make a ".reg/999" section. */
3790 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3791 size, note->descpos + offset);
3795 static bfd_boolean
3796 elf_xtensa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3798 switch (note->descsz)
3800 default:
3801 return FALSE;
3803 case 128: /* GNU/Linux elf_prpsinfo */
3804 elf_tdata (abfd)->core_program
3805 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3806 elf_tdata (abfd)->core_command
3807 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3810 /* Note that for some reason, a spurious space is tacked
3811 onto the end of the args in some (at least one anyway)
3812 implementations, so strip it off if it exists. */
3815 char *command = elf_tdata (abfd)->core_command;
3816 int n = strlen (command);
3818 if (0 < n && command[n - 1] == ' ')
3819 command[n - 1] = '\0';
3822 return TRUE;
3826 /* Generic Xtensa configurability stuff. */
3828 static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
3829 static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
3830 static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
3831 static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
3832 static xtensa_opcode call0_op = XTENSA_UNDEFINED;
3833 static xtensa_opcode call4_op = XTENSA_UNDEFINED;
3834 static xtensa_opcode call8_op = XTENSA_UNDEFINED;
3835 static xtensa_opcode call12_op = XTENSA_UNDEFINED;
3837 static void
3838 init_call_opcodes (void)
3840 if (callx0_op == XTENSA_UNDEFINED)
3842 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
3843 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
3844 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
3845 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
3846 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
3847 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
3848 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
3849 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
3854 static bfd_boolean
3855 is_indirect_call_opcode (xtensa_opcode opcode)
3857 init_call_opcodes ();
3858 return (opcode == callx0_op
3859 || opcode == callx4_op
3860 || opcode == callx8_op
3861 || opcode == callx12_op);
3865 static bfd_boolean
3866 is_direct_call_opcode (xtensa_opcode opcode)
3868 init_call_opcodes ();
3869 return (opcode == call0_op
3870 || opcode == call4_op
3871 || opcode == call8_op
3872 || opcode == call12_op);
3876 static bfd_boolean
3877 is_windowed_call_opcode (xtensa_opcode opcode)
3879 init_call_opcodes ();
3880 return (opcode == call4_op
3881 || opcode == call8_op
3882 || opcode == call12_op
3883 || opcode == callx4_op
3884 || opcode == callx8_op
3885 || opcode == callx12_op);
3889 static bfd_boolean
3890 get_indirect_call_dest_reg (xtensa_opcode opcode, unsigned *pdst)
3892 unsigned dst = (unsigned) -1;
3894 init_call_opcodes ();
3895 if (opcode == callx0_op)
3896 dst = 0;
3897 else if (opcode == callx4_op)
3898 dst = 4;
3899 else if (opcode == callx8_op)
3900 dst = 8;
3901 else if (opcode == callx12_op)
3902 dst = 12;
3904 if (dst == (unsigned) -1)
3905 return FALSE;
3907 *pdst = dst;
3908 return TRUE;
3912 static xtensa_opcode
3913 get_const16_opcode (void)
3915 static bfd_boolean done_lookup = FALSE;
3916 static xtensa_opcode const16_opcode = XTENSA_UNDEFINED;
3917 if (!done_lookup)
3919 const16_opcode = xtensa_opcode_lookup (xtensa_default_isa, "const16");
3920 done_lookup = TRUE;
3922 return const16_opcode;
3926 static xtensa_opcode
3927 get_l32r_opcode (void)
3929 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
3930 static bfd_boolean done_lookup = FALSE;
3932 if (!done_lookup)
3934 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
3935 done_lookup = TRUE;
3937 return l32r_opcode;
3941 static bfd_vma
3942 l32r_offset (bfd_vma addr, bfd_vma pc)
3944 bfd_vma offset;
3946 offset = addr - ((pc+3) & -4);
3947 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
3948 offset = (signed int) offset >> 2;
3949 BFD_ASSERT ((signed int) offset >> 16 == -1);
3950 return offset;
3954 static int
3955 get_relocation_opnd (xtensa_opcode opcode, int r_type)
3957 xtensa_isa isa = xtensa_default_isa;
3958 int last_immed, last_opnd, opi;
3960 if (opcode == XTENSA_UNDEFINED)
3961 return XTENSA_UNDEFINED;
3963 /* Find the last visible PC-relative immediate operand for the opcode.
3964 If there are no PC-relative immediates, then choose the last visible
3965 immediate; otherwise, fail and return XTENSA_UNDEFINED. */
3966 last_immed = XTENSA_UNDEFINED;
3967 last_opnd = xtensa_opcode_num_operands (isa, opcode);
3968 for (opi = last_opnd - 1; opi >= 0; opi--)
3970 if (xtensa_operand_is_visible (isa, opcode, opi) == 0)
3971 continue;
3972 if (xtensa_operand_is_PCrelative (isa, opcode, opi) == 1)
3974 last_immed = opi;
3975 break;
3977 if (last_immed == XTENSA_UNDEFINED
3978 && xtensa_operand_is_register (isa, opcode, opi) == 0)
3979 last_immed = opi;
3981 if (last_immed < 0)
3982 return XTENSA_UNDEFINED;
3984 /* If the operand number was specified in an old-style relocation,
3985 check for consistency with the operand computed above. */
3986 if (r_type >= R_XTENSA_OP0 && r_type <= R_XTENSA_OP2)
3988 int reloc_opnd = r_type - R_XTENSA_OP0;
3989 if (reloc_opnd != last_immed)
3990 return XTENSA_UNDEFINED;
3993 return last_immed;
3998 get_relocation_slot (int r_type)
4000 switch (r_type)
4002 case R_XTENSA_OP0:
4003 case R_XTENSA_OP1:
4004 case R_XTENSA_OP2:
4005 return 0;
4007 default:
4008 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4009 return r_type - R_XTENSA_SLOT0_OP;
4010 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4011 return r_type - R_XTENSA_SLOT0_ALT;
4012 break;
4015 return XTENSA_UNDEFINED;
4019 /* Get the opcode for a relocation. */
4021 static xtensa_opcode
4022 get_relocation_opcode (bfd *abfd,
4023 asection *sec,
4024 bfd_byte *contents,
4025 Elf_Internal_Rela *irel)
4027 static xtensa_insnbuf ibuff = NULL;
4028 static xtensa_insnbuf sbuff = NULL;
4029 xtensa_isa isa = xtensa_default_isa;
4030 xtensa_format fmt;
4031 int slot;
4033 if (contents == NULL)
4034 return XTENSA_UNDEFINED;
4036 if (bfd_get_section_limit (abfd, sec) <= irel->r_offset)
4037 return XTENSA_UNDEFINED;
4039 if (ibuff == NULL)
4041 ibuff = xtensa_insnbuf_alloc (isa);
4042 sbuff = xtensa_insnbuf_alloc (isa);
4045 /* Decode the instruction. */
4046 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset],
4047 sec->size - irel->r_offset);
4048 fmt = xtensa_format_decode (isa, ibuff);
4049 slot = get_relocation_slot (ELF32_R_TYPE (irel->r_info));
4050 if (slot == XTENSA_UNDEFINED)
4051 return XTENSA_UNDEFINED;
4052 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
4053 return xtensa_opcode_decode (isa, fmt, slot, sbuff);
4057 bfd_boolean
4058 is_l32r_relocation (bfd *abfd,
4059 asection *sec,
4060 bfd_byte *contents,
4061 Elf_Internal_Rela *irel)
4063 xtensa_opcode opcode;
4064 if (!is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
4065 return FALSE;
4066 opcode = get_relocation_opcode (abfd, sec, contents, irel);
4067 return (opcode == get_l32r_opcode ());
4071 static bfd_size_type
4072 get_asm_simplify_size (bfd_byte *contents,
4073 bfd_size_type content_len,
4074 bfd_size_type offset)
4076 bfd_size_type insnlen, size = 0;
4078 /* Decode the size of the next two instructions. */
4079 insnlen = insn_decode_len (contents, content_len, offset);
4080 if (insnlen == 0)
4081 return 0;
4083 size += insnlen;
4085 insnlen = insn_decode_len (contents, content_len, offset + size);
4086 if (insnlen == 0)
4087 return 0;
4089 size += insnlen;
4090 return size;
4094 bfd_boolean
4095 is_alt_relocation (int r_type)
4097 return (r_type >= R_XTENSA_SLOT0_ALT
4098 && r_type <= R_XTENSA_SLOT14_ALT);
4102 bfd_boolean
4103 is_operand_relocation (int r_type)
4105 switch (r_type)
4107 case R_XTENSA_OP0:
4108 case R_XTENSA_OP1:
4109 case R_XTENSA_OP2:
4110 return TRUE;
4112 default:
4113 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
4114 return TRUE;
4115 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
4116 return TRUE;
4117 break;
4120 return FALSE;
4124 #define MIN_INSN_LENGTH 2
4126 /* Return 0 if it fails to decode. */
4128 bfd_size_type
4129 insn_decode_len (bfd_byte *contents,
4130 bfd_size_type content_len,
4131 bfd_size_type offset)
4133 int insn_len;
4134 xtensa_isa isa = xtensa_default_isa;
4135 xtensa_format fmt;
4136 static xtensa_insnbuf ibuff = NULL;
4138 if (offset + MIN_INSN_LENGTH > content_len)
4139 return 0;
4141 if (ibuff == NULL)
4142 ibuff = xtensa_insnbuf_alloc (isa);
4143 xtensa_insnbuf_from_chars (isa, ibuff, &contents[offset],
4144 content_len - offset);
4145 fmt = xtensa_format_decode (isa, ibuff);
4146 if (fmt == XTENSA_UNDEFINED)
4147 return 0;
4148 insn_len = xtensa_format_length (isa, fmt);
4149 if (insn_len == XTENSA_UNDEFINED)
4150 return 0;
4151 return insn_len;
4155 /* Decode the opcode for a single slot instruction.
4156 Return 0 if it fails to decode or the instruction is multi-slot. */
4158 xtensa_opcode
4159 insn_decode_opcode (bfd_byte *contents,
4160 bfd_size_type content_len,
4161 bfd_size_type offset,
4162 int slot)
4164 xtensa_isa isa = xtensa_default_isa;
4165 xtensa_format fmt;
4166 static xtensa_insnbuf insnbuf = NULL;
4167 static xtensa_insnbuf slotbuf = NULL;
4169 if (offset + MIN_INSN_LENGTH > content_len)
4170 return XTENSA_UNDEFINED;
4172 if (insnbuf == NULL)
4174 insnbuf = xtensa_insnbuf_alloc (isa);
4175 slotbuf = xtensa_insnbuf_alloc (isa);
4178 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4179 content_len - offset);
4180 fmt = xtensa_format_decode (isa, insnbuf);
4181 if (fmt == XTENSA_UNDEFINED)
4182 return XTENSA_UNDEFINED;
4184 if (slot >= xtensa_format_num_slots (isa, fmt))
4185 return XTENSA_UNDEFINED;
4187 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4188 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4192 /* The offset is the offset in the contents.
4193 The address is the address of that offset. */
4195 static bfd_boolean
4196 check_branch_target_aligned (bfd_byte *contents,
4197 bfd_size_type content_length,
4198 bfd_vma offset,
4199 bfd_vma address)
4201 bfd_size_type insn_len = insn_decode_len (contents, content_length, offset);
4202 if (insn_len == 0)
4203 return FALSE;
4204 return check_branch_target_aligned_address (address, insn_len);
4208 static bfd_boolean
4209 check_loop_aligned (bfd_byte *contents,
4210 bfd_size_type content_length,
4211 bfd_vma offset,
4212 bfd_vma address)
4214 bfd_size_type loop_len, insn_len;
4215 xtensa_opcode opcode;
4217 opcode = insn_decode_opcode (contents, content_length, offset, 0);
4218 if (opcode == XTENSA_UNDEFINED
4219 || xtensa_opcode_is_loop (xtensa_default_isa, opcode) != 1)
4221 BFD_ASSERT (FALSE);
4222 return FALSE;
4225 loop_len = insn_decode_len (contents, content_length, offset);
4226 insn_len = insn_decode_len (contents, content_length, offset + loop_len);
4227 if (loop_len == 0 || insn_len == 0)
4229 BFD_ASSERT (FALSE);
4230 return FALSE;
4233 return check_branch_target_aligned_address (address + loop_len, insn_len);
4237 static bfd_boolean
4238 check_branch_target_aligned_address (bfd_vma addr, int len)
4240 if (len == 8)
4241 return (addr % 8 == 0);
4242 return ((addr >> 2) == ((addr + len - 1) >> 2));
4246 /* Instruction widening and narrowing. */
4248 /* When FLIX is available we need to access certain instructions only
4249 when they are 16-bit or 24-bit instructions. This table caches
4250 information about such instructions by walking through all the
4251 opcodes and finding the smallest single-slot format into which each
4252 can be encoded. */
4254 static xtensa_format *op_single_fmt_table = NULL;
4257 static void
4258 init_op_single_format_table (void)
4260 xtensa_isa isa = xtensa_default_isa;
4261 xtensa_insnbuf ibuf;
4262 xtensa_opcode opcode;
4263 xtensa_format fmt;
4264 int num_opcodes;
4266 if (op_single_fmt_table)
4267 return;
4269 ibuf = xtensa_insnbuf_alloc (isa);
4270 num_opcodes = xtensa_isa_num_opcodes (isa);
4272 op_single_fmt_table = (xtensa_format *)
4273 bfd_malloc (sizeof (xtensa_format) * num_opcodes);
4274 for (opcode = 0; opcode < num_opcodes; opcode++)
4276 op_single_fmt_table[opcode] = XTENSA_UNDEFINED;
4277 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
4279 if (xtensa_format_num_slots (isa, fmt) == 1
4280 && xtensa_opcode_encode (isa, fmt, 0, ibuf, opcode) == 0)
4282 xtensa_opcode old_fmt = op_single_fmt_table[opcode];
4283 int fmt_length = xtensa_format_length (isa, fmt);
4284 if (old_fmt == XTENSA_UNDEFINED
4285 || fmt_length < xtensa_format_length (isa, old_fmt))
4286 op_single_fmt_table[opcode] = fmt;
4290 xtensa_insnbuf_free (isa, ibuf);
4294 static xtensa_format
4295 get_single_format (xtensa_opcode opcode)
4297 init_op_single_format_table ();
4298 return op_single_fmt_table[opcode];
4302 /* For the set of narrowable instructions we do NOT include the
4303 narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
4304 involved during linker relaxation that may require these to
4305 re-expand in some conditions. Also, the narrowing "or" -> mov.n
4306 requires special case code to ensure it only works when op1 == op2. */
4308 struct string_pair
4310 const char *wide;
4311 const char *narrow;
4314 struct string_pair narrowable[] =
4316 { "add", "add.n" },
4317 { "addi", "addi.n" },
4318 { "addmi", "addi.n" },
4319 { "l32i", "l32i.n" },
4320 { "movi", "movi.n" },
4321 { "ret", "ret.n" },
4322 { "retw", "retw.n" },
4323 { "s32i", "s32i.n" },
4324 { "or", "mov.n" } /* special case only when op1 == op2 */
4327 struct string_pair widenable[] =
4329 { "add", "add.n" },
4330 { "addi", "addi.n" },
4331 { "addmi", "addi.n" },
4332 { "beqz", "beqz.n" },
4333 { "bnez", "bnez.n" },
4334 { "l32i", "l32i.n" },
4335 { "movi", "movi.n" },
4336 { "ret", "ret.n" },
4337 { "retw", "retw.n" },
4338 { "s32i", "s32i.n" },
4339 { "or", "mov.n" } /* special case only when op1 == op2 */
4343 /* Check if an instruction can be "narrowed", i.e., changed from a standard
4344 3-byte instruction to a 2-byte "density" instruction. If it is valid,
4345 return the instruction buffer holding the narrow instruction. Otherwise,
4346 return 0. The set of valid narrowing are specified by a string table
4347 but require some special case operand checks in some cases. */
4349 static xtensa_insnbuf
4350 can_narrow_instruction (xtensa_insnbuf slotbuf,
4351 xtensa_format fmt,
4352 xtensa_opcode opcode)
4354 xtensa_isa isa = xtensa_default_isa;
4355 xtensa_format o_fmt;
4356 unsigned opi;
4358 static xtensa_insnbuf o_insnbuf = NULL;
4359 static xtensa_insnbuf o_slotbuf = NULL;
4361 if (o_insnbuf == NULL)
4363 o_insnbuf = xtensa_insnbuf_alloc (isa);
4364 o_slotbuf = xtensa_insnbuf_alloc (isa);
4367 for (opi = 0; opi < (sizeof (narrowable)/sizeof (struct string_pair)); opi++)
4369 bfd_boolean is_or = (strcmp ("or", narrowable[opi].wide) == 0);
4371 if (opcode == xtensa_opcode_lookup (isa, narrowable[opi].wide))
4373 uint32 value, newval;
4374 int i, operand_count, o_operand_count;
4375 xtensa_opcode o_opcode;
4377 /* Address does not matter in this case. We might need to
4378 fix it to handle branches/jumps. */
4379 bfd_vma self_address = 0;
4381 o_opcode = xtensa_opcode_lookup (isa, narrowable[opi].narrow);
4382 if (o_opcode == XTENSA_UNDEFINED)
4383 return 0;
4384 o_fmt = get_single_format (o_opcode);
4385 if (o_fmt == XTENSA_UNDEFINED)
4386 return 0;
4388 if (xtensa_format_length (isa, fmt) != 3
4389 || xtensa_format_length (isa, o_fmt) != 2)
4390 return 0;
4392 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4393 operand_count = xtensa_opcode_num_operands (isa, opcode);
4394 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4396 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4397 return 0;
4399 if (!is_or)
4401 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4402 return 0;
4404 else
4406 uint32 rawval0, rawval1, rawval2;
4408 if (o_operand_count + 1 != operand_count
4409 || xtensa_operand_get_field (isa, opcode, 0,
4410 fmt, 0, slotbuf, &rawval0) != 0
4411 || xtensa_operand_get_field (isa, opcode, 1,
4412 fmt, 0, slotbuf, &rawval1) != 0
4413 || xtensa_operand_get_field (isa, opcode, 2,
4414 fmt, 0, slotbuf, &rawval2) != 0
4415 || rawval1 != rawval2
4416 || rawval0 == rawval1 /* it is a nop */)
4417 return 0;
4420 for (i = 0; i < o_operand_count; ++i)
4422 if (xtensa_operand_get_field (isa, opcode, i, fmt, 0,
4423 slotbuf, &value)
4424 || xtensa_operand_decode (isa, opcode, i, &value))
4425 return 0;
4427 /* PC-relative branches need adjustment, but
4428 the PC-rel operand will always have a relocation. */
4429 newval = value;
4430 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4431 self_address)
4432 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4433 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4434 o_slotbuf, newval))
4435 return 0;
4438 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4439 return 0;
4441 return o_insnbuf;
4444 return 0;
4448 /* Attempt to narrow an instruction. If the narrowing is valid, perform
4449 the action in-place directly into the contents and return TRUE. Otherwise,
4450 the return value is FALSE and the contents are not modified. */
4452 static bfd_boolean
4453 narrow_instruction (bfd_byte *contents,
4454 bfd_size_type content_length,
4455 bfd_size_type offset)
4457 xtensa_opcode opcode;
4458 bfd_size_type insn_len;
4459 xtensa_isa isa = xtensa_default_isa;
4460 xtensa_format fmt;
4461 xtensa_insnbuf o_insnbuf;
4463 static xtensa_insnbuf insnbuf = NULL;
4464 static xtensa_insnbuf slotbuf = NULL;
4466 if (insnbuf == NULL)
4468 insnbuf = xtensa_insnbuf_alloc (isa);
4469 slotbuf = xtensa_insnbuf_alloc (isa);
4472 BFD_ASSERT (offset < content_length);
4474 if (content_length < 2)
4475 return FALSE;
4477 /* We will hand-code a few of these for a little while.
4478 These have all been specified in the assembler aleady. */
4479 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4480 content_length - offset);
4481 fmt = xtensa_format_decode (isa, insnbuf);
4482 if (xtensa_format_num_slots (isa, fmt) != 1)
4483 return FALSE;
4485 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4486 return FALSE;
4488 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4489 if (opcode == XTENSA_UNDEFINED)
4490 return FALSE;
4491 insn_len = xtensa_format_length (isa, fmt);
4492 if (insn_len > content_length)
4493 return FALSE;
4495 o_insnbuf = can_narrow_instruction (slotbuf, fmt, opcode);
4496 if (o_insnbuf)
4498 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4499 content_length - offset);
4500 return TRUE;
4503 return FALSE;
4507 /* Check if an instruction can be "widened", i.e., changed from a 2-byte
4508 "density" instruction to a standard 3-byte instruction. If it is valid,
4509 return the instruction buffer holding the wide instruction. Otherwise,
4510 return 0. The set of valid widenings are specified by a string table
4511 but require some special case operand checks in some cases. */
4513 static xtensa_insnbuf
4514 can_widen_instruction (xtensa_insnbuf slotbuf,
4515 xtensa_format fmt,
4516 xtensa_opcode opcode)
4518 xtensa_isa isa = xtensa_default_isa;
4519 xtensa_format o_fmt;
4520 unsigned opi;
4522 static xtensa_insnbuf o_insnbuf = NULL;
4523 static xtensa_insnbuf o_slotbuf = NULL;
4525 if (o_insnbuf == NULL)
4527 o_insnbuf = xtensa_insnbuf_alloc (isa);
4528 o_slotbuf = xtensa_insnbuf_alloc (isa);
4531 for (opi = 0; opi < (sizeof (widenable)/sizeof (struct string_pair)); opi++)
4533 bfd_boolean is_or = (strcmp ("or", widenable[opi].wide) == 0);
4534 bfd_boolean is_branch = (strcmp ("beqz", widenable[opi].wide) == 0
4535 || strcmp ("bnez", widenable[opi].wide) == 0);
4537 if (opcode == xtensa_opcode_lookup (isa, widenable[opi].narrow))
4539 uint32 value, newval;
4540 int i, operand_count, o_operand_count, check_operand_count;
4541 xtensa_opcode o_opcode;
4543 /* Address does not matter in this case. We might need to fix it
4544 to handle branches/jumps. */
4545 bfd_vma self_address = 0;
4547 o_opcode = xtensa_opcode_lookup (isa, widenable[opi].wide);
4548 if (o_opcode == XTENSA_UNDEFINED)
4549 return 0;
4550 o_fmt = get_single_format (o_opcode);
4551 if (o_fmt == XTENSA_UNDEFINED)
4552 return 0;
4554 if (xtensa_format_length (isa, fmt) != 2
4555 || xtensa_format_length (isa, o_fmt) != 3)
4556 return 0;
4558 xtensa_format_encode (isa, o_fmt, o_insnbuf);
4559 operand_count = xtensa_opcode_num_operands (isa, opcode);
4560 o_operand_count = xtensa_opcode_num_operands (isa, o_opcode);
4561 check_operand_count = o_operand_count;
4563 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
4564 return 0;
4566 if (!is_or)
4568 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
4569 return 0;
4571 else
4573 uint32 rawval0, rawval1;
4575 if (o_operand_count != operand_count + 1
4576 || xtensa_operand_get_field (isa, opcode, 0,
4577 fmt, 0, slotbuf, &rawval0) != 0
4578 || xtensa_operand_get_field (isa, opcode, 1,
4579 fmt, 0, slotbuf, &rawval1) != 0
4580 || rawval0 == rawval1 /* it is a nop */)
4581 return 0;
4583 if (is_branch)
4584 check_operand_count--;
4586 for (i = 0; i < check_operand_count; i++)
4588 int new_i = i;
4589 if (is_or && i == o_operand_count - 1)
4590 new_i = i - 1;
4591 if (xtensa_operand_get_field (isa, opcode, new_i, fmt, 0,
4592 slotbuf, &value)
4593 || xtensa_operand_decode (isa, opcode, new_i, &value))
4594 return 0;
4596 /* PC-relative branches need adjustment, but
4597 the PC-rel operand will always have a relocation. */
4598 newval = value;
4599 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4600 self_address)
4601 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4602 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4603 o_slotbuf, newval))
4604 return 0;
4607 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4608 return 0;
4610 return o_insnbuf;
4613 return 0;
4617 /* Attempt to widen an instruction. If the widening is valid, perform
4618 the action in-place directly into the contents and return TRUE. Otherwise,
4619 the return value is FALSE and the contents are not modified. */
4621 static bfd_boolean
4622 widen_instruction (bfd_byte *contents,
4623 bfd_size_type content_length,
4624 bfd_size_type offset)
4626 xtensa_opcode opcode;
4627 bfd_size_type insn_len;
4628 xtensa_isa isa = xtensa_default_isa;
4629 xtensa_format fmt;
4630 xtensa_insnbuf o_insnbuf;
4632 static xtensa_insnbuf insnbuf = NULL;
4633 static xtensa_insnbuf slotbuf = NULL;
4635 if (insnbuf == NULL)
4637 insnbuf = xtensa_insnbuf_alloc (isa);
4638 slotbuf = xtensa_insnbuf_alloc (isa);
4641 BFD_ASSERT (offset < content_length);
4643 if (content_length < 2)
4644 return FALSE;
4646 /* We will hand-code a few of these for a little while.
4647 These have all been specified in the assembler aleady. */
4648 xtensa_insnbuf_from_chars (isa, insnbuf, &contents[offset],
4649 content_length - offset);
4650 fmt = xtensa_format_decode (isa, insnbuf);
4651 if (xtensa_format_num_slots (isa, fmt) != 1)
4652 return FALSE;
4654 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
4655 return FALSE;
4657 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4658 if (opcode == XTENSA_UNDEFINED)
4659 return FALSE;
4660 insn_len = xtensa_format_length (isa, fmt);
4661 if (insn_len > content_length)
4662 return FALSE;
4664 o_insnbuf = can_widen_instruction (slotbuf, fmt, opcode);
4665 if (o_insnbuf)
4667 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4668 content_length - offset);
4669 return TRUE;
4671 return FALSE;
4675 /* Code for transforming CALLs at link-time. */
4677 static bfd_reloc_status_type
4678 elf_xtensa_do_asm_simplify (bfd_byte *contents,
4679 bfd_vma address,
4680 bfd_vma content_length,
4681 char **error_message)
4683 static xtensa_insnbuf insnbuf = NULL;
4684 static xtensa_insnbuf slotbuf = NULL;
4685 xtensa_format core_format = XTENSA_UNDEFINED;
4686 xtensa_opcode opcode;
4687 xtensa_opcode direct_call_opcode;
4688 xtensa_isa isa = xtensa_default_isa;
4689 bfd_byte *chbuf = contents + address;
4690 int opn;
4692 if (insnbuf == NULL)
4694 insnbuf = xtensa_insnbuf_alloc (isa);
4695 slotbuf = xtensa_insnbuf_alloc (isa);
4698 if (content_length < address)
4700 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4701 return bfd_reloc_other;
4704 opcode = get_expanded_call_opcode (chbuf, content_length - address, 0);
4705 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4706 if (direct_call_opcode == XTENSA_UNDEFINED)
4708 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4709 return bfd_reloc_other;
4712 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
4713 core_format = xtensa_format_lookup (isa, "x24");
4714 opcode = xtensa_opcode_lookup (isa, "or");
4715 xtensa_opcode_encode (isa, core_format, 0, slotbuf, opcode);
4716 for (opn = 0; opn < 3; opn++)
4718 uint32 regno = 1;
4719 xtensa_operand_encode (isa, opcode, opn, &regno);
4720 xtensa_operand_set_field (isa, opcode, opn, core_format, 0,
4721 slotbuf, regno);
4723 xtensa_format_encode (isa, core_format, insnbuf);
4724 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4725 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf, content_length - address);
4727 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
4728 xtensa_opcode_encode (isa, core_format, 0, slotbuf, direct_call_opcode);
4729 xtensa_operand_set_field (isa, opcode, 0, core_format, 0, slotbuf, 0);
4731 xtensa_format_encode (isa, core_format, insnbuf);
4732 xtensa_format_set_slot (isa, core_format, 0, insnbuf, slotbuf);
4733 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3,
4734 content_length - address - 3);
4736 return bfd_reloc_ok;
4740 static bfd_reloc_status_type
4741 contract_asm_expansion (bfd_byte *contents,
4742 bfd_vma content_length,
4743 Elf_Internal_Rela *irel,
4744 char **error_message)
4746 bfd_reloc_status_type retval =
4747 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length,
4748 error_message);
4750 if (retval != bfd_reloc_ok)
4751 return bfd_reloc_dangerous;
4753 /* Update the irel->r_offset field so that the right immediate and
4754 the right instruction are modified during the relocation. */
4755 irel->r_offset += 3;
4756 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_SLOT0_OP);
4757 return bfd_reloc_ok;
4761 static xtensa_opcode
4762 swap_callx_for_call_opcode (xtensa_opcode opcode)
4764 init_call_opcodes ();
4766 if (opcode == callx0_op) return call0_op;
4767 if (opcode == callx4_op) return call4_op;
4768 if (opcode == callx8_op) return call8_op;
4769 if (opcode == callx12_op) return call12_op;
4771 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
4772 return XTENSA_UNDEFINED;
4776 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
4777 CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
4778 If not, return XTENSA_UNDEFINED. */
4780 #define L32R_TARGET_REG_OPERAND 0
4781 #define CONST16_TARGET_REG_OPERAND 0
4782 #define CALLN_SOURCE_OPERAND 0
4784 static xtensa_opcode
4785 get_expanded_call_opcode (bfd_byte *buf, int bufsize, bfd_boolean *p_uses_l32r)
4787 static xtensa_insnbuf insnbuf = NULL;
4788 static xtensa_insnbuf slotbuf = NULL;
4789 xtensa_format fmt;
4790 xtensa_opcode opcode;
4791 xtensa_isa isa = xtensa_default_isa;
4792 uint32 regno, const16_regno, call_regno;
4793 int offset = 0;
4795 if (insnbuf == NULL)
4797 insnbuf = xtensa_insnbuf_alloc (isa);
4798 slotbuf = xtensa_insnbuf_alloc (isa);
4801 xtensa_insnbuf_from_chars (isa, insnbuf, buf, bufsize);
4802 fmt = xtensa_format_decode (isa, insnbuf);
4803 if (fmt == XTENSA_UNDEFINED
4804 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4805 return XTENSA_UNDEFINED;
4807 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4808 if (opcode == XTENSA_UNDEFINED)
4809 return XTENSA_UNDEFINED;
4811 if (opcode == get_l32r_opcode ())
4813 if (p_uses_l32r)
4814 *p_uses_l32r = TRUE;
4815 if (xtensa_operand_get_field (isa, opcode, L32R_TARGET_REG_OPERAND,
4816 fmt, 0, slotbuf, &regno)
4817 || xtensa_operand_decode (isa, opcode, L32R_TARGET_REG_OPERAND,
4818 &regno))
4819 return XTENSA_UNDEFINED;
4821 else if (opcode == get_const16_opcode ())
4823 if (p_uses_l32r)
4824 *p_uses_l32r = FALSE;
4825 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4826 fmt, 0, slotbuf, &regno)
4827 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4828 &regno))
4829 return XTENSA_UNDEFINED;
4831 /* Check that the next instruction is also CONST16. */
4832 offset += xtensa_format_length (isa, fmt);
4833 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4834 fmt = xtensa_format_decode (isa, insnbuf);
4835 if (fmt == XTENSA_UNDEFINED
4836 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4837 return XTENSA_UNDEFINED;
4838 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4839 if (opcode != get_const16_opcode ())
4840 return XTENSA_UNDEFINED;
4842 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4843 fmt, 0, slotbuf, &const16_regno)
4844 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4845 &const16_regno)
4846 || const16_regno != regno)
4847 return XTENSA_UNDEFINED;
4849 else
4850 return XTENSA_UNDEFINED;
4852 /* Next instruction should be an CALLXn with operand 0 == regno. */
4853 offset += xtensa_format_length (isa, fmt);
4854 xtensa_insnbuf_from_chars (isa, insnbuf, buf + offset, bufsize - offset);
4855 fmt = xtensa_format_decode (isa, insnbuf);
4856 if (fmt == XTENSA_UNDEFINED
4857 || xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf))
4858 return XTENSA_UNDEFINED;
4859 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4860 if (opcode == XTENSA_UNDEFINED
4861 || !is_indirect_call_opcode (opcode))
4862 return XTENSA_UNDEFINED;
4864 if (xtensa_operand_get_field (isa, opcode, CALLN_SOURCE_OPERAND,
4865 fmt, 0, slotbuf, &call_regno)
4866 || xtensa_operand_decode (isa, opcode, CALLN_SOURCE_OPERAND,
4867 &call_regno))
4868 return XTENSA_UNDEFINED;
4870 if (call_regno != regno)
4871 return XTENSA_UNDEFINED;
4873 return opcode;
4877 /* Data structures used during relaxation. */
4879 /* r_reloc: relocation values. */
4881 /* Through the relaxation process, we need to keep track of the values
4882 that will result from evaluating relocations. The standard ELF
4883 relocation structure is not sufficient for this purpose because we're
4884 operating on multiple input files at once, so we need to know which
4885 input file a relocation refers to. The r_reloc structure thus
4886 records both the input file (bfd) and ELF relocation.
4888 For efficiency, an r_reloc also contains a "target_offset" field to
4889 cache the target-section-relative offset value that is represented by
4890 the relocation.
4892 The r_reloc also contains a virtual offset that allows multiple
4893 inserted literals to be placed at the same "address" with
4894 different offsets. */
4896 typedef struct r_reloc_struct r_reloc;
4898 struct r_reloc_struct
4900 bfd *abfd;
4901 Elf_Internal_Rela rela;
4902 bfd_vma target_offset;
4903 bfd_vma virtual_offset;
4907 /* The r_reloc structure is included by value in literal_value, but not
4908 every literal_value has an associated relocation -- some are simple
4909 constants. In such cases, we set all the fields in the r_reloc
4910 struct to zero. The r_reloc_is_const function should be used to
4911 detect this case. */
4913 static bfd_boolean
4914 r_reloc_is_const (const r_reloc *r_rel)
4916 return (r_rel->abfd == NULL);
4920 static bfd_vma
4921 r_reloc_get_target_offset (const r_reloc *r_rel)
4923 bfd_vma target_offset;
4924 unsigned long r_symndx;
4926 BFD_ASSERT (!r_reloc_is_const (r_rel));
4927 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4928 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
4929 return (target_offset + r_rel->rela.r_addend);
4933 static struct elf_link_hash_entry *
4934 r_reloc_get_hash_entry (const r_reloc *r_rel)
4936 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4937 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
4941 static asection *
4942 r_reloc_get_section (const r_reloc *r_rel)
4944 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
4945 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
4949 static bfd_boolean
4950 r_reloc_is_defined (const r_reloc *r_rel)
4952 asection *sec;
4953 if (r_rel == NULL)
4954 return FALSE;
4956 sec = r_reloc_get_section (r_rel);
4957 if (sec == bfd_abs_section_ptr
4958 || sec == bfd_com_section_ptr
4959 || sec == bfd_und_section_ptr)
4960 return FALSE;
4961 return TRUE;
4965 static void
4966 r_reloc_init (r_reloc *r_rel,
4967 bfd *abfd,
4968 Elf_Internal_Rela *irel,
4969 bfd_byte *contents,
4970 bfd_size_type content_length)
4972 int r_type;
4973 reloc_howto_type *howto;
4975 if (irel)
4977 r_rel->rela = *irel;
4978 r_rel->abfd = abfd;
4979 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
4980 r_rel->virtual_offset = 0;
4981 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
4982 howto = &elf_howto_table[r_type];
4983 if (howto->partial_inplace)
4985 bfd_vma inplace_val;
4986 BFD_ASSERT (r_rel->rela.r_offset < content_length);
4988 inplace_val = bfd_get_32 (abfd, &contents[r_rel->rela.r_offset]);
4989 r_rel->target_offset += inplace_val;
4992 else
4993 memset (r_rel, 0, sizeof (r_reloc));
4997 #if DEBUG
4999 static void
5000 print_r_reloc (FILE *fp, const r_reloc *r_rel)
5002 if (r_reloc_is_defined (r_rel))
5004 asection *sec = r_reloc_get_section (r_rel);
5005 fprintf (fp, " %s(%s + ", sec->owner->filename, sec->name);
5007 else if (r_reloc_get_hash_entry (r_rel))
5008 fprintf (fp, " %s + ", r_reloc_get_hash_entry (r_rel)->root.root.string);
5009 else
5010 fprintf (fp, " ?? + ");
5012 fprintf_vma (fp, r_rel->target_offset);
5013 if (r_rel->virtual_offset)
5015 fprintf (fp, " + ");
5016 fprintf_vma (fp, r_rel->virtual_offset);
5019 fprintf (fp, ")");
5022 #endif /* DEBUG */
5025 /* source_reloc: relocations that reference literals. */
5027 /* To determine whether literals can be coalesced, we need to first
5028 record all the relocations that reference the literals. The
5029 source_reloc structure below is used for this purpose. The
5030 source_reloc entries are kept in a per-literal-section array, sorted
5031 by offset within the literal section (i.e., target offset).
5033 The source_sec and r_rel.rela.r_offset fields identify the source of
5034 the relocation. The r_rel field records the relocation value, i.e.,
5035 the offset of the literal being referenced. The opnd field is needed
5036 to determine the range of the immediate field to which the relocation
5037 applies, so we can determine whether another literal with the same
5038 value is within range. The is_null field is true when the relocation
5039 is being removed (e.g., when an L32R is being removed due to a CALLX
5040 that is converted to a direct CALL). */
5042 typedef struct source_reloc_struct source_reloc;
5044 struct source_reloc_struct
5046 asection *source_sec;
5047 r_reloc r_rel;
5048 xtensa_opcode opcode;
5049 int opnd;
5050 bfd_boolean is_null;
5051 bfd_boolean is_abs_literal;
5055 static void
5056 init_source_reloc (source_reloc *reloc,
5057 asection *source_sec,
5058 const r_reloc *r_rel,
5059 xtensa_opcode opcode,
5060 int opnd,
5061 bfd_boolean is_abs_literal)
5063 reloc->source_sec = source_sec;
5064 reloc->r_rel = *r_rel;
5065 reloc->opcode = opcode;
5066 reloc->opnd = opnd;
5067 reloc->is_null = FALSE;
5068 reloc->is_abs_literal = is_abs_literal;
5072 /* Find the source_reloc for a particular source offset and relocation
5073 type. Note that the array is sorted by _target_ offset, so this is
5074 just a linear search. */
5076 static source_reloc *
5077 find_source_reloc (source_reloc *src_relocs,
5078 int src_count,
5079 asection *sec,
5080 Elf_Internal_Rela *irel)
5082 int i;
5084 for (i = 0; i < src_count; i++)
5086 if (src_relocs[i].source_sec == sec
5087 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
5088 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
5089 == ELF32_R_TYPE (irel->r_info)))
5090 return &src_relocs[i];
5093 return NULL;
5097 static int
5098 source_reloc_compare (const void *ap, const void *bp)
5100 const source_reloc *a = (const source_reloc *) ap;
5101 const source_reloc *b = (const source_reloc *) bp;
5103 if (a->r_rel.target_offset != b->r_rel.target_offset)
5104 return (a->r_rel.target_offset - b->r_rel.target_offset);
5106 /* We don't need to sort on these criteria for correctness,
5107 but enforcing a more strict ordering prevents unstable qsort
5108 from behaving differently with different implementations.
5109 Without the code below we get correct but different results
5110 on Solaris 2.7 and 2.8. We would like to always produce the
5111 same results no matter the host. */
5113 if ((!a->is_null) - (!b->is_null))
5114 return ((!a->is_null) - (!b->is_null));
5115 return internal_reloc_compare (&a->r_rel.rela, &b->r_rel.rela);
5119 /* Literal values and value hash tables. */
5121 /* Literals with the same value can be coalesced. The literal_value
5122 structure records the value of a literal: the "r_rel" field holds the
5123 information from the relocation on the literal (if there is one) and
5124 the "value" field holds the contents of the literal word itself.
5126 The value_map structure records a literal value along with the
5127 location of a literal holding that value. The value_map hash table
5128 is indexed by the literal value, so that we can quickly check if a
5129 particular literal value has been seen before and is thus a candidate
5130 for coalescing. */
5132 typedef struct literal_value_struct literal_value;
5133 typedef struct value_map_struct value_map;
5134 typedef struct value_map_hash_table_struct value_map_hash_table;
5136 struct literal_value_struct
5138 r_reloc r_rel;
5139 unsigned long value;
5140 bfd_boolean is_abs_literal;
5143 struct value_map_struct
5145 literal_value val; /* The literal value. */
5146 r_reloc loc; /* Location of the literal. */
5147 value_map *next;
5150 struct value_map_hash_table_struct
5152 unsigned bucket_count;
5153 value_map **buckets;
5154 unsigned count;
5155 bfd_boolean has_last_loc;
5156 r_reloc last_loc;
5160 static void
5161 init_literal_value (literal_value *lit,
5162 const r_reloc *r_rel,
5163 unsigned long value,
5164 bfd_boolean is_abs_literal)
5166 lit->r_rel = *r_rel;
5167 lit->value = value;
5168 lit->is_abs_literal = is_abs_literal;
5172 static bfd_boolean
5173 literal_value_equal (const literal_value *src1,
5174 const literal_value *src2,
5175 bfd_boolean final_static_link)
5177 struct elf_link_hash_entry *h1, *h2;
5179 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
5180 return FALSE;
5182 if (r_reloc_is_const (&src1->r_rel))
5183 return (src1->value == src2->value);
5185 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
5186 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
5187 return FALSE;
5189 if (src1->r_rel.target_offset != src2->r_rel.target_offset)
5190 return FALSE;
5192 if (src1->r_rel.virtual_offset != src2->r_rel.virtual_offset)
5193 return FALSE;
5195 if (src1->value != src2->value)
5196 return FALSE;
5198 /* Now check for the same section (if defined) or the same elf_hash
5199 (if undefined or weak). */
5200 h1 = r_reloc_get_hash_entry (&src1->r_rel);
5201 h2 = r_reloc_get_hash_entry (&src2->r_rel);
5202 if (r_reloc_is_defined (&src1->r_rel)
5203 && (final_static_link
5204 || ((!h1 || h1->root.type != bfd_link_hash_defweak)
5205 && (!h2 || h2->root.type != bfd_link_hash_defweak))))
5207 if (r_reloc_get_section (&src1->r_rel)
5208 != r_reloc_get_section (&src2->r_rel))
5209 return FALSE;
5211 else
5213 /* Require that the hash entries (i.e., symbols) be identical. */
5214 if (h1 != h2 || h1 == 0)
5215 return FALSE;
5218 if (src1->is_abs_literal != src2->is_abs_literal)
5219 return FALSE;
5221 return TRUE;
5225 /* Must be power of 2. */
5226 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
5228 static value_map_hash_table *
5229 value_map_hash_table_init (void)
5231 value_map_hash_table *values;
5233 values = (value_map_hash_table *)
5234 bfd_zmalloc (sizeof (value_map_hash_table));
5235 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
5236 values->count = 0;
5237 values->buckets = (value_map **)
5238 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
5239 if (values->buckets == NULL)
5241 free (values);
5242 return NULL;
5244 values->has_last_loc = FALSE;
5246 return values;
5250 static void
5251 value_map_hash_table_delete (value_map_hash_table *table)
5253 free (table->buckets);
5254 free (table);
5258 static unsigned
5259 hash_bfd_vma (bfd_vma val)
5261 return (val >> 2) + (val >> 10);
5265 static unsigned
5266 literal_value_hash (const literal_value *src)
5268 unsigned hash_val;
5270 hash_val = hash_bfd_vma (src->value);
5271 if (!r_reloc_is_const (&src->r_rel))
5273 void *sec_or_hash;
5275 hash_val += hash_bfd_vma (src->is_abs_literal * 1000);
5276 hash_val += hash_bfd_vma (src->r_rel.target_offset);
5277 hash_val += hash_bfd_vma (src->r_rel.virtual_offset);
5279 /* Now check for the same section and the same elf_hash. */
5280 if (r_reloc_is_defined (&src->r_rel))
5281 sec_or_hash = r_reloc_get_section (&src->r_rel);
5282 else
5283 sec_or_hash = r_reloc_get_hash_entry (&src->r_rel);
5284 hash_val += hash_bfd_vma ((bfd_vma) (size_t) sec_or_hash);
5286 return hash_val;
5290 /* Check if the specified literal_value has been seen before. */
5292 static value_map *
5293 value_map_get_cached_value (value_map_hash_table *map,
5294 const literal_value *val,
5295 bfd_boolean final_static_link)
5297 value_map *map_e;
5298 value_map *bucket;
5299 unsigned idx;
5301 idx = literal_value_hash (val);
5302 idx = idx & (map->bucket_count - 1);
5303 bucket = map->buckets[idx];
5304 for (map_e = bucket; map_e; map_e = map_e->next)
5306 if (literal_value_equal (&map_e->val, val, final_static_link))
5307 return map_e;
5309 return NULL;
5313 /* Record a new literal value. It is illegal to call this if VALUE
5314 already has an entry here. */
5316 static value_map *
5317 add_value_map (value_map_hash_table *map,
5318 const literal_value *val,
5319 const r_reloc *loc,
5320 bfd_boolean final_static_link)
5322 value_map **bucket_p;
5323 unsigned idx;
5325 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
5326 if (val_e == NULL)
5328 bfd_set_error (bfd_error_no_memory);
5329 return NULL;
5332 BFD_ASSERT (!value_map_get_cached_value (map, val, final_static_link));
5333 val_e->val = *val;
5334 val_e->loc = *loc;
5336 idx = literal_value_hash (val);
5337 idx = idx & (map->bucket_count - 1);
5338 bucket_p = &map->buckets[idx];
5340 val_e->next = *bucket_p;
5341 *bucket_p = val_e;
5342 map->count++;
5343 /* FIXME: Consider resizing the hash table if we get too many entries. */
5345 return val_e;
5349 /* Lists of text actions (ta_) for narrowing, widening, longcall
5350 conversion, space fill, code & literal removal, etc. */
5352 /* The following text actions are generated:
5354 "ta_remove_insn" remove an instruction or instructions
5355 "ta_remove_longcall" convert longcall to call
5356 "ta_convert_longcall" convert longcall to nop/call
5357 "ta_narrow_insn" narrow a wide instruction
5358 "ta_widen" widen a narrow instruction
5359 "ta_fill" add fill or remove fill
5360 removed < 0 is a fill; branches to the fill address will be
5361 changed to address + fill size (e.g., address - removed)
5362 removed >= 0 branches to the fill address will stay unchanged
5363 "ta_remove_literal" remove a literal; this action is
5364 indicated when a literal is removed
5365 or replaced.
5366 "ta_add_literal" insert a new literal; this action is
5367 indicated when a literal has been moved.
5368 It may use a virtual_offset because
5369 multiple literals can be placed at the
5370 same location.
5372 For each of these text actions, we also record the number of bytes
5373 removed by performing the text action. In the case of a "ta_widen"
5374 or a "ta_fill" that adds space, the removed_bytes will be negative. */
5376 typedef struct text_action_struct text_action;
5377 typedef struct text_action_list_struct text_action_list;
5378 typedef enum text_action_enum_t text_action_t;
5380 enum text_action_enum_t
5382 ta_none,
5383 ta_remove_insn, /* removed = -size */
5384 ta_remove_longcall, /* removed = -size */
5385 ta_convert_longcall, /* removed = 0 */
5386 ta_narrow_insn, /* removed = -1 */
5387 ta_widen_insn, /* removed = +1 */
5388 ta_fill, /* removed = +size */
5389 ta_remove_literal,
5390 ta_add_literal
5394 /* Structure for a text action record. */
5395 struct text_action_struct
5397 text_action_t action;
5398 asection *sec; /* Optional */
5399 bfd_vma offset;
5400 bfd_vma virtual_offset; /* Zero except for adding literals. */
5401 int removed_bytes;
5402 literal_value value; /* Only valid when adding literals. */
5404 text_action *next;
5408 /* List of all of the actions taken on a text section. */
5409 struct text_action_list_struct
5411 text_action *head;
5415 static text_action *
5416 find_fill_action (text_action_list *l, asection *sec, bfd_vma offset)
5418 text_action **m_p;
5420 /* It is not necessary to fill at the end of a section. */
5421 if (sec->size == offset)
5422 return NULL;
5424 for (m_p = &l->head; *m_p && (*m_p)->offset <= offset; m_p = &(*m_p)->next)
5426 text_action *t = *m_p;
5427 /* When the action is another fill at the same address,
5428 just increase the size. */
5429 if (t->offset == offset && t->action == ta_fill)
5430 return t;
5432 return NULL;
5436 static int
5437 compute_removed_action_diff (const text_action *ta,
5438 asection *sec,
5439 bfd_vma offset,
5440 int removed,
5441 int removable_space)
5443 int new_removed;
5444 int current_removed = 0;
5446 if (ta)
5447 current_removed = ta->removed_bytes;
5449 BFD_ASSERT (ta == NULL || ta->offset == offset);
5450 BFD_ASSERT (ta == NULL || ta->action == ta_fill);
5452 /* It is not necessary to fill at the end of a section. Clean this up. */
5453 if (sec->size == offset)
5454 new_removed = removable_space - 0;
5455 else
5457 int space;
5458 int added = -removed - current_removed;
5459 /* Ignore multiples of the section alignment. */
5460 added = ((1 << sec->alignment_power) - 1) & added;
5461 new_removed = (-added);
5463 /* Modify for removable. */
5464 space = removable_space - new_removed;
5465 new_removed = (removable_space
5466 - (((1 << sec->alignment_power) - 1) & space));
5468 return (new_removed - current_removed);
5472 static void
5473 adjust_fill_action (text_action *ta, int fill_diff)
5475 ta->removed_bytes += fill_diff;
5479 /* Add a modification action to the text. For the case of adding or
5480 removing space, modify any current fill and assume that
5481 "unreachable_space" bytes can be freely contracted. Note that a
5482 negative removed value is a fill. */
5484 static void
5485 text_action_add (text_action_list *l,
5486 text_action_t action,
5487 asection *sec,
5488 bfd_vma offset,
5489 int removed)
5491 text_action **m_p;
5492 text_action *ta;
5494 /* It is not necessary to fill at the end of a section. */
5495 if (action == ta_fill && sec->size == offset)
5496 return;
5498 /* It is not necessary to fill 0 bytes. */
5499 if (action == ta_fill && removed == 0)
5500 return;
5502 for (m_p = &l->head; *m_p && (*m_p)->offset <= offset; m_p = &(*m_p)->next)
5504 text_action *t = *m_p;
5506 if (action == ta_fill)
5508 /* When the action is another fill at the same address,
5509 just increase the size. */
5510 if (t->offset == offset && t->action == ta_fill)
5512 t->removed_bytes += removed;
5513 return;
5515 /* Fills need to happen before widens so that we don't
5516 insert fill bytes into the instruction stream. */
5517 if (t->offset == offset && t->action == ta_widen_insn)
5518 break;
5522 /* Create a new record and fill it up. */
5523 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5524 ta->action = action;
5525 ta->sec = sec;
5526 ta->offset = offset;
5527 ta->removed_bytes = removed;
5528 ta->next = (*m_p);
5529 *m_p = ta;
5533 static void
5534 text_action_add_literal (text_action_list *l,
5535 text_action_t action,
5536 const r_reloc *loc,
5537 const literal_value *value,
5538 int removed)
5540 text_action **m_p;
5541 text_action *ta;
5542 asection *sec = r_reloc_get_section (loc);
5543 bfd_vma offset = loc->target_offset;
5544 bfd_vma virtual_offset = loc->virtual_offset;
5546 BFD_ASSERT (action == ta_add_literal);
5548 for (m_p = &l->head; *m_p != NULL; m_p = &(*m_p)->next)
5550 if ((*m_p)->offset > offset
5551 && ((*m_p)->offset != offset
5552 || (*m_p)->virtual_offset > virtual_offset))
5553 break;
5556 /* Create a new record and fill it up. */
5557 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5558 ta->action = action;
5559 ta->sec = sec;
5560 ta->offset = offset;
5561 ta->virtual_offset = virtual_offset;
5562 ta->value = *value;
5563 ta->removed_bytes = removed;
5564 ta->next = (*m_p);
5565 *m_p = ta;
5569 /* Find the total offset adjustment for the relaxations specified by
5570 text_actions, beginning from a particular starting action. This is
5571 typically used from offset_with_removed_text to search an entire list of
5572 actions, but it may also be called directly when adjusting adjacent offsets
5573 so that each search may begin where the previous one left off. */
5575 static int
5576 removed_by_actions (text_action **p_start_action,
5577 bfd_vma offset,
5578 bfd_boolean before_fill)
5580 text_action *r;
5581 int removed = 0;
5583 r = *p_start_action;
5584 while (r)
5586 if (r->offset > offset)
5587 break;
5589 if (r->offset == offset
5590 && (before_fill || r->action != ta_fill || r->removed_bytes >= 0))
5591 break;
5593 removed += r->removed_bytes;
5595 r = r->next;
5598 *p_start_action = r;
5599 return removed;
5603 static bfd_vma
5604 offset_with_removed_text (text_action_list *action_list, bfd_vma offset)
5606 text_action *r = action_list->head;
5607 return offset - removed_by_actions (&r, offset, FALSE);
5611 static unsigned
5612 action_list_count (text_action_list *action_list)
5614 text_action *r = action_list->head;
5615 unsigned count = 0;
5616 for (r = action_list->head; r != NULL; r = r->next)
5618 count++;
5620 return count;
5624 /* The find_insn_action routine will only find non-fill actions. */
5626 static text_action *
5627 find_insn_action (text_action_list *action_list, bfd_vma offset)
5629 text_action *t;
5630 for (t = action_list->head; t; t = t->next)
5632 if (t->offset == offset)
5634 switch (t->action)
5636 case ta_none:
5637 case ta_fill:
5638 break;
5639 case ta_remove_insn:
5640 case ta_remove_longcall:
5641 case ta_convert_longcall:
5642 case ta_narrow_insn:
5643 case ta_widen_insn:
5644 return t;
5645 case ta_remove_literal:
5646 case ta_add_literal:
5647 BFD_ASSERT (0);
5648 break;
5652 return NULL;
5656 #if DEBUG
5658 static void
5659 print_action_list (FILE *fp, text_action_list *action_list)
5661 text_action *r;
5663 fprintf (fp, "Text Action\n");
5664 for (r = action_list->head; r != NULL; r = r->next)
5666 const char *t = "unknown";
5667 switch (r->action)
5669 case ta_remove_insn:
5670 t = "remove_insn"; break;
5671 case ta_remove_longcall:
5672 t = "remove_longcall"; break;
5673 case ta_convert_longcall:
5674 t = "convert_longcall"; break;
5675 case ta_narrow_insn:
5676 t = "narrow_insn"; break;
5677 case ta_widen_insn:
5678 t = "widen_insn"; break;
5679 case ta_fill:
5680 t = "fill"; break;
5681 case ta_none:
5682 t = "none"; break;
5683 case ta_remove_literal:
5684 t = "remove_literal"; break;
5685 case ta_add_literal:
5686 t = "add_literal"; break;
5689 fprintf (fp, "%s: %s[0x%lx] \"%s\" %d\n",
5690 r->sec->owner->filename,
5691 r->sec->name, r->offset, t, r->removed_bytes);
5695 #endif /* DEBUG */
5698 /* Lists of literals being coalesced or removed. */
5700 /* In the usual case, the literal identified by "from" is being
5701 coalesced with another literal identified by "to". If the literal is
5702 unused and is being removed altogether, "to.abfd" will be NULL.
5703 The removed_literal entries are kept on a per-section list, sorted
5704 by the "from" offset field. */
5706 typedef struct removed_literal_struct removed_literal;
5707 typedef struct removed_literal_list_struct removed_literal_list;
5709 struct removed_literal_struct
5711 r_reloc from;
5712 r_reloc to;
5713 removed_literal *next;
5716 struct removed_literal_list_struct
5718 removed_literal *head;
5719 removed_literal *tail;
5723 /* Record that the literal at "from" is being removed. If "to" is not
5724 NULL, the "from" literal is being coalesced with the "to" literal. */
5726 static void
5727 add_removed_literal (removed_literal_list *removed_list,
5728 const r_reloc *from,
5729 const r_reloc *to)
5731 removed_literal *r, *new_r, *next_r;
5733 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
5735 new_r->from = *from;
5736 if (to)
5737 new_r->to = *to;
5738 else
5739 new_r->to.abfd = NULL;
5740 new_r->next = NULL;
5742 r = removed_list->head;
5743 if (r == NULL)
5745 removed_list->head = new_r;
5746 removed_list->tail = new_r;
5748 /* Special check for common case of append. */
5749 else if (removed_list->tail->from.target_offset < from->target_offset)
5751 removed_list->tail->next = new_r;
5752 removed_list->tail = new_r;
5754 else
5756 while (r->from.target_offset < from->target_offset && r->next)
5758 r = r->next;
5760 next_r = r->next;
5761 r->next = new_r;
5762 new_r->next = next_r;
5763 if (next_r == NULL)
5764 removed_list->tail = new_r;
5769 /* Check if the list of removed literals contains an entry for the
5770 given address. Return the entry if found. */
5772 static removed_literal *
5773 find_removed_literal (removed_literal_list *removed_list, bfd_vma addr)
5775 removed_literal *r = removed_list->head;
5776 while (r && r->from.target_offset < addr)
5777 r = r->next;
5778 if (r && r->from.target_offset == addr)
5779 return r;
5780 return NULL;
5784 #if DEBUG
5786 static void
5787 print_removed_literals (FILE *fp, removed_literal_list *removed_list)
5789 removed_literal *r;
5790 r = removed_list->head;
5791 if (r)
5792 fprintf (fp, "Removed Literals\n");
5793 for (; r != NULL; r = r->next)
5795 print_r_reloc (fp, &r->from);
5796 fprintf (fp, " => ");
5797 if (r->to.abfd == NULL)
5798 fprintf (fp, "REMOVED");
5799 else
5800 print_r_reloc (fp, &r->to);
5801 fprintf (fp, "\n");
5805 #endif /* DEBUG */
5808 /* Per-section data for relaxation. */
5810 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
5812 struct xtensa_relax_info_struct
5814 bfd_boolean is_relaxable_literal_section;
5815 bfd_boolean is_relaxable_asm_section;
5816 int visited; /* Number of times visited. */
5818 source_reloc *src_relocs; /* Array[src_count]. */
5819 int src_count;
5820 int src_next; /* Next src_relocs entry to assign. */
5822 removed_literal_list removed_list;
5823 text_action_list action_list;
5825 reloc_bfd_fix *fix_list;
5826 reloc_bfd_fix *fix_array;
5827 unsigned fix_array_count;
5829 /* Support for expanding the reloc array that is stored
5830 in the section structure. If the relocations have been
5831 reallocated, the newly allocated relocations will be referenced
5832 here along with the actual size allocated. The relocation
5833 count will always be found in the section structure. */
5834 Elf_Internal_Rela *allocated_relocs;
5835 unsigned relocs_count;
5836 unsigned allocated_relocs_count;
5839 struct elf_xtensa_section_data
5841 struct bfd_elf_section_data elf;
5842 xtensa_relax_info relax_info;
5846 static bfd_boolean
5847 elf_xtensa_new_section_hook (bfd *abfd, asection *sec)
5849 if (!sec->used_by_bfd)
5851 struct elf_xtensa_section_data *sdata;
5852 bfd_size_type amt = sizeof (*sdata);
5854 sdata = bfd_zalloc (abfd, amt);
5855 if (sdata == NULL)
5856 return FALSE;
5857 sec->used_by_bfd = sdata;
5860 return _bfd_elf_new_section_hook (abfd, sec);
5864 static xtensa_relax_info *
5865 get_xtensa_relax_info (asection *sec)
5867 struct elf_xtensa_section_data *section_data;
5869 /* No info available if no section or if it is an output section. */
5870 if (!sec || sec == sec->output_section)
5871 return NULL;
5873 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
5874 return &section_data->relax_info;
5878 static void
5879 init_xtensa_relax_info (asection *sec)
5881 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
5883 relax_info->is_relaxable_literal_section = FALSE;
5884 relax_info->is_relaxable_asm_section = FALSE;
5885 relax_info->visited = 0;
5887 relax_info->src_relocs = NULL;
5888 relax_info->src_count = 0;
5889 relax_info->src_next = 0;
5891 relax_info->removed_list.head = NULL;
5892 relax_info->removed_list.tail = NULL;
5894 relax_info->action_list.head = NULL;
5896 relax_info->fix_list = NULL;
5897 relax_info->fix_array = NULL;
5898 relax_info->fix_array_count = 0;
5900 relax_info->allocated_relocs = NULL;
5901 relax_info->relocs_count = 0;
5902 relax_info->allocated_relocs_count = 0;
5906 /* Coalescing literals may require a relocation to refer to a section in
5907 a different input file, but the standard relocation information
5908 cannot express that. Instead, the reloc_bfd_fix structures are used
5909 to "fix" the relocations that refer to sections in other input files.
5910 These structures are kept on per-section lists. The "src_type" field
5911 records the relocation type in case there are multiple relocations on
5912 the same location. FIXME: This is ugly; an alternative might be to
5913 add new symbols with the "owner" field to some other input file. */
5915 struct reloc_bfd_fix_struct
5917 asection *src_sec;
5918 bfd_vma src_offset;
5919 unsigned src_type; /* Relocation type. */
5921 asection *target_sec;
5922 bfd_vma target_offset;
5923 bfd_boolean translated;
5925 reloc_bfd_fix *next;
5929 static reloc_bfd_fix *
5930 reloc_bfd_fix_init (asection *src_sec,
5931 bfd_vma src_offset,
5932 unsigned src_type,
5933 asection *target_sec,
5934 bfd_vma target_offset,
5935 bfd_boolean translated)
5937 reloc_bfd_fix *fix;
5939 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
5940 fix->src_sec = src_sec;
5941 fix->src_offset = src_offset;
5942 fix->src_type = src_type;
5943 fix->target_sec = target_sec;
5944 fix->target_offset = target_offset;
5945 fix->translated = translated;
5947 return fix;
5951 static void
5952 add_fix (asection *src_sec, reloc_bfd_fix *fix)
5954 xtensa_relax_info *relax_info;
5956 relax_info = get_xtensa_relax_info (src_sec);
5957 fix->next = relax_info->fix_list;
5958 relax_info->fix_list = fix;
5962 static int
5963 fix_compare (const void *ap, const void *bp)
5965 const reloc_bfd_fix *a = (const reloc_bfd_fix *) ap;
5966 const reloc_bfd_fix *b = (const reloc_bfd_fix *) bp;
5968 if (a->src_offset != b->src_offset)
5969 return (a->src_offset - b->src_offset);
5970 return (a->src_type - b->src_type);
5974 static void
5975 cache_fix_array (asection *sec)
5977 unsigned i, count = 0;
5978 reloc_bfd_fix *r;
5979 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
5981 if (relax_info == NULL)
5982 return;
5983 if (relax_info->fix_list == NULL)
5984 return;
5986 for (r = relax_info->fix_list; r != NULL; r = r->next)
5987 count++;
5989 relax_info->fix_array =
5990 (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix) * count);
5991 relax_info->fix_array_count = count;
5993 r = relax_info->fix_list;
5994 for (i = 0; i < count; i++, r = r->next)
5996 relax_info->fix_array[count - 1 - i] = *r;
5997 relax_info->fix_array[count - 1 - i].next = NULL;
6000 qsort (relax_info->fix_array, relax_info->fix_array_count,
6001 sizeof (reloc_bfd_fix), fix_compare);
6005 static reloc_bfd_fix *
6006 get_bfd_fix (asection *sec, bfd_vma offset, unsigned type)
6008 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
6009 reloc_bfd_fix *rv;
6010 reloc_bfd_fix key;
6012 if (relax_info == NULL)
6013 return NULL;
6014 if (relax_info->fix_list == NULL)
6015 return NULL;
6017 if (relax_info->fix_array == NULL)
6018 cache_fix_array (sec);
6020 key.src_offset = offset;
6021 key.src_type = type;
6022 rv = bsearch (&key, relax_info->fix_array, relax_info->fix_array_count,
6023 sizeof (reloc_bfd_fix), fix_compare);
6024 return rv;
6028 /* Section caching. */
6030 typedef struct section_cache_struct section_cache_t;
6032 struct section_cache_struct
6034 asection *sec;
6036 bfd_byte *contents; /* Cache of the section contents. */
6037 bfd_size_type content_length;
6039 property_table_entry *ptbl; /* Cache of the section property table. */
6040 unsigned pte_count;
6042 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6043 unsigned reloc_count;
6047 static void
6048 init_section_cache (section_cache_t *sec_cache)
6050 memset (sec_cache, 0, sizeof (*sec_cache));
6054 static void
6055 clear_section_cache (section_cache_t *sec_cache)
6057 if (sec_cache->sec)
6059 release_contents (sec_cache->sec, sec_cache->contents);
6060 release_internal_relocs (sec_cache->sec, sec_cache->relocs);
6061 if (sec_cache->ptbl)
6062 free (sec_cache->ptbl);
6063 memset (sec_cache, 0, sizeof (sec_cache));
6068 static bfd_boolean
6069 section_cache_section (section_cache_t *sec_cache,
6070 asection *sec,
6071 struct bfd_link_info *link_info)
6073 bfd *abfd;
6074 property_table_entry *prop_table = NULL;
6075 int ptblsize = 0;
6076 bfd_byte *contents = NULL;
6077 Elf_Internal_Rela *internal_relocs = NULL;
6078 bfd_size_type sec_size;
6080 if (sec == NULL)
6081 return FALSE;
6082 if (sec == sec_cache->sec)
6083 return TRUE;
6085 abfd = sec->owner;
6086 sec_size = bfd_get_section_limit (abfd, sec);
6088 /* Get the contents. */
6089 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6090 if (contents == NULL && sec_size != 0)
6091 goto err;
6093 /* Get the relocations. */
6094 internal_relocs = retrieve_internal_relocs (abfd, sec,
6095 link_info->keep_memory);
6097 /* Get the entry table. */
6098 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
6099 XTENSA_PROP_SEC_NAME, FALSE);
6100 if (ptblsize < 0)
6101 goto err;
6103 /* Fill in the new section cache. */
6104 clear_section_cache (sec_cache);
6105 memset (sec_cache, 0, sizeof (sec_cache));
6107 sec_cache->sec = sec;
6108 sec_cache->contents = contents;
6109 sec_cache->content_length = sec_size;
6110 sec_cache->relocs = internal_relocs;
6111 sec_cache->reloc_count = sec->reloc_count;
6112 sec_cache->pte_count = ptblsize;
6113 sec_cache->ptbl = prop_table;
6115 return TRUE;
6117 err:
6118 release_contents (sec, contents);
6119 release_internal_relocs (sec, internal_relocs);
6120 if (prop_table)
6121 free (prop_table);
6122 return FALSE;
6126 /* Extended basic blocks. */
6128 /* An ebb_struct represents an Extended Basic Block. Within this
6129 range, we guarantee that all instructions are decodable, the
6130 property table entries are contiguous, and no property table
6131 specifies a segment that cannot have instructions moved. This
6132 structure contains caches of the contents, property table and
6133 relocations for the specified section for easy use. The range is
6134 specified by ranges of indices for the byte offset, property table
6135 offsets and relocation offsets. These must be consistent. */
6137 typedef struct ebb_struct ebb_t;
6139 struct ebb_struct
6141 asection *sec;
6143 bfd_byte *contents; /* Cache of the section contents. */
6144 bfd_size_type content_length;
6146 property_table_entry *ptbl; /* Cache of the section property table. */
6147 unsigned pte_count;
6149 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
6150 unsigned reloc_count;
6152 bfd_vma start_offset; /* Offset in section. */
6153 unsigned start_ptbl_idx; /* Offset in the property table. */
6154 unsigned start_reloc_idx; /* Offset in the relocations. */
6156 bfd_vma end_offset;
6157 unsigned end_ptbl_idx;
6158 unsigned end_reloc_idx;
6160 bfd_boolean ends_section; /* Is this the last ebb in a section? */
6162 /* The unreachable property table at the end of this set of blocks;
6163 NULL if the end is not an unreachable block. */
6164 property_table_entry *ends_unreachable;
6168 enum ebb_target_enum
6170 EBB_NO_ALIGN = 0,
6171 EBB_DESIRE_TGT_ALIGN,
6172 EBB_REQUIRE_TGT_ALIGN,
6173 EBB_REQUIRE_LOOP_ALIGN,
6174 EBB_REQUIRE_ALIGN
6178 /* proposed_action_struct is similar to the text_action_struct except
6179 that is represents a potential transformation, not one that will
6180 occur. We build a list of these for an extended basic block
6181 and use them to compute the actual actions desired. We must be
6182 careful that the entire set of actual actions we perform do not
6183 break any relocations that would fit if the actions were not
6184 performed. */
6186 typedef struct proposed_action_struct proposed_action;
6188 struct proposed_action_struct
6190 enum ebb_target_enum align_type; /* for the target alignment */
6191 bfd_vma alignment_pow;
6192 text_action_t action;
6193 bfd_vma offset;
6194 int removed_bytes;
6195 bfd_boolean do_action; /* If false, then we will not perform the action. */
6199 /* The ebb_constraint_struct keeps a set of proposed actions for an
6200 extended basic block. */
6202 typedef struct ebb_constraint_struct ebb_constraint;
6204 struct ebb_constraint_struct
6206 ebb_t ebb;
6207 bfd_boolean start_movable;
6209 /* Bytes of extra space at the beginning if movable. */
6210 int start_extra_space;
6212 enum ebb_target_enum start_align;
6214 bfd_boolean end_movable;
6216 /* Bytes of extra space at the end if movable. */
6217 int end_extra_space;
6219 unsigned action_count;
6220 unsigned action_allocated;
6222 /* Array of proposed actions. */
6223 proposed_action *actions;
6225 /* Action alignments -- one for each proposed action. */
6226 enum ebb_target_enum *action_aligns;
6230 static void
6231 init_ebb_constraint (ebb_constraint *c)
6233 memset (c, 0, sizeof (ebb_constraint));
6237 static void
6238 free_ebb_constraint (ebb_constraint *c)
6240 if (c->actions)
6241 free (c->actions);
6245 static void
6246 init_ebb (ebb_t *ebb,
6247 asection *sec,
6248 bfd_byte *contents,
6249 bfd_size_type content_length,
6250 property_table_entry *prop_table,
6251 unsigned ptblsize,
6252 Elf_Internal_Rela *internal_relocs,
6253 unsigned reloc_count)
6255 memset (ebb, 0, sizeof (ebb_t));
6256 ebb->sec = sec;
6257 ebb->contents = contents;
6258 ebb->content_length = content_length;
6259 ebb->ptbl = prop_table;
6260 ebb->pte_count = ptblsize;
6261 ebb->relocs = internal_relocs;
6262 ebb->reloc_count = reloc_count;
6263 ebb->start_offset = 0;
6264 ebb->end_offset = ebb->content_length - 1;
6265 ebb->start_ptbl_idx = 0;
6266 ebb->end_ptbl_idx = ptblsize;
6267 ebb->start_reloc_idx = 0;
6268 ebb->end_reloc_idx = reloc_count;
6272 /* Extend the ebb to all decodable contiguous sections. The algorithm
6273 for building a basic block around an instruction is to push it
6274 forward until we hit the end of a section, an unreachable block or
6275 a block that cannot be transformed. Then we push it backwards
6276 searching for similar conditions. */
6278 static bfd_boolean extend_ebb_bounds_forward (ebb_t *);
6279 static bfd_boolean extend_ebb_bounds_backward (ebb_t *);
6280 static bfd_size_type insn_block_decodable_len
6281 (bfd_byte *, bfd_size_type, bfd_vma, bfd_size_type);
6283 static bfd_boolean
6284 extend_ebb_bounds (ebb_t *ebb)
6286 if (!extend_ebb_bounds_forward (ebb))
6287 return FALSE;
6288 if (!extend_ebb_bounds_backward (ebb))
6289 return FALSE;
6290 return TRUE;
6294 static bfd_boolean
6295 extend_ebb_bounds_forward (ebb_t *ebb)
6297 property_table_entry *the_entry, *new_entry;
6299 the_entry = &ebb->ptbl[ebb->end_ptbl_idx];
6301 /* Stop when (1) we cannot decode an instruction, (2) we are at
6302 the end of the property tables, (3) we hit a non-contiguous property
6303 table entry, (4) we hit a NO_TRANSFORM region. */
6305 while (1)
6307 bfd_vma entry_end;
6308 bfd_size_type insn_block_len;
6310 entry_end = the_entry->address - ebb->sec->vma + the_entry->size;
6311 insn_block_len =
6312 insn_block_decodable_len (ebb->contents, ebb->content_length,
6313 ebb->end_offset,
6314 entry_end - ebb->end_offset);
6315 if (insn_block_len != (entry_end - ebb->end_offset))
6317 (*_bfd_error_handler)
6318 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6319 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6320 return FALSE;
6322 ebb->end_offset += insn_block_len;
6324 if (ebb->end_offset == ebb->sec->size)
6325 ebb->ends_section = TRUE;
6327 /* Update the reloc counter. */
6328 while (ebb->end_reloc_idx + 1 < ebb->reloc_count
6329 && (ebb->relocs[ebb->end_reloc_idx + 1].r_offset
6330 < ebb->end_offset))
6332 ebb->end_reloc_idx++;
6335 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6336 return TRUE;
6338 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6339 if (((new_entry->flags & XTENSA_PROP_INSN) == 0)
6340 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6341 || ((the_entry->flags & XTENSA_PROP_ALIGN) != 0))
6342 break;
6344 if (the_entry->address + the_entry->size != new_entry->address)
6345 break;
6347 the_entry = new_entry;
6348 ebb->end_ptbl_idx++;
6351 /* Quick check for an unreachable or end of file just at the end. */
6352 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
6354 if (ebb->end_offset == ebb->content_length)
6355 ebb->ends_section = TRUE;
6357 else
6359 new_entry = &ebb->ptbl[ebb->end_ptbl_idx + 1];
6360 if ((new_entry->flags & XTENSA_PROP_UNREACHABLE) != 0
6361 && the_entry->address + the_entry->size == new_entry->address)
6362 ebb->ends_unreachable = new_entry;
6365 /* Any other ending requires exact alignment. */
6366 return TRUE;
6370 static bfd_boolean
6371 extend_ebb_bounds_backward (ebb_t *ebb)
6373 property_table_entry *the_entry, *new_entry;
6375 the_entry = &ebb->ptbl[ebb->start_ptbl_idx];
6377 /* Stop when (1) we cannot decode the instructions in the current entry.
6378 (2) we are at the beginning of the property tables, (3) we hit a
6379 non-contiguous property table entry, (4) we hit a NO_TRANSFORM region. */
6381 while (1)
6383 bfd_vma block_begin;
6384 bfd_size_type insn_block_len;
6386 block_begin = the_entry->address - ebb->sec->vma;
6387 insn_block_len =
6388 insn_block_decodable_len (ebb->contents, ebb->content_length,
6389 block_begin,
6390 ebb->start_offset - block_begin);
6391 if (insn_block_len != ebb->start_offset - block_begin)
6393 (*_bfd_error_handler)
6394 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6395 ebb->sec->owner, ebb->sec, ebb->end_offset + insn_block_len);
6396 return FALSE;
6398 ebb->start_offset -= insn_block_len;
6400 /* Update the reloc counter. */
6401 while (ebb->start_reloc_idx > 0
6402 && (ebb->relocs[ebb->start_reloc_idx - 1].r_offset
6403 >= ebb->start_offset))
6405 ebb->start_reloc_idx--;
6408 if (ebb->start_ptbl_idx == 0)
6409 return TRUE;
6411 new_entry = &ebb->ptbl[ebb->start_ptbl_idx - 1];
6412 if ((new_entry->flags & XTENSA_PROP_INSN) == 0
6413 || ((new_entry->flags & XTENSA_PROP_NO_TRANSFORM) != 0)
6414 || ((new_entry->flags & XTENSA_PROP_ALIGN) != 0))
6415 return TRUE;
6416 if (new_entry->address + new_entry->size != the_entry->address)
6417 return TRUE;
6419 the_entry = new_entry;
6420 ebb->start_ptbl_idx--;
6422 return TRUE;
6426 static bfd_size_type
6427 insn_block_decodable_len (bfd_byte *contents,
6428 bfd_size_type content_len,
6429 bfd_vma block_offset,
6430 bfd_size_type block_len)
6432 bfd_vma offset = block_offset;
6434 while (offset < block_offset + block_len)
6436 bfd_size_type insn_len = 0;
6438 insn_len = insn_decode_len (contents, content_len, offset);
6439 if (insn_len == 0)
6440 return (offset - block_offset);
6441 offset += insn_len;
6443 return (offset - block_offset);
6447 static void
6448 ebb_propose_action (ebb_constraint *c,
6449 enum ebb_target_enum align_type,
6450 bfd_vma alignment_pow,
6451 text_action_t action,
6452 bfd_vma offset,
6453 int removed_bytes,
6454 bfd_boolean do_action)
6456 proposed_action *act;
6458 if (c->action_allocated <= c->action_count)
6460 unsigned new_allocated, i;
6461 proposed_action *new_actions;
6463 new_allocated = (c->action_count + 2) * 2;
6464 new_actions = (proposed_action *)
6465 bfd_zmalloc (sizeof (proposed_action) * new_allocated);
6467 for (i = 0; i < c->action_count; i++)
6468 new_actions[i] = c->actions[i];
6469 if (c->actions)
6470 free (c->actions);
6471 c->actions = new_actions;
6472 c->action_allocated = new_allocated;
6475 act = &c->actions[c->action_count];
6476 act->align_type = align_type;
6477 act->alignment_pow = alignment_pow;
6478 act->action = action;
6479 act->offset = offset;
6480 act->removed_bytes = removed_bytes;
6481 act->do_action = do_action;
6483 c->action_count++;
6487 /* Access to internal relocations, section contents and symbols. */
6489 /* During relaxation, we need to modify relocations, section contents,
6490 and symbol definitions, and we need to keep the original values from
6491 being reloaded from the input files, i.e., we need to "pin" the
6492 modified values in memory. We also want to continue to observe the
6493 setting of the "keep-memory" flag. The following functions wrap the
6494 standard BFD functions to take care of this for us. */
6496 static Elf_Internal_Rela *
6497 retrieve_internal_relocs (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6499 Elf_Internal_Rela *internal_relocs;
6501 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6502 return NULL;
6504 internal_relocs = elf_section_data (sec)->relocs;
6505 if (internal_relocs == NULL)
6506 internal_relocs = (_bfd_elf_link_read_relocs
6507 (abfd, sec, NULL, NULL, keep_memory));
6508 return internal_relocs;
6512 static void
6513 pin_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6515 elf_section_data (sec)->relocs = internal_relocs;
6519 static void
6520 release_internal_relocs (asection *sec, Elf_Internal_Rela *internal_relocs)
6522 if (internal_relocs
6523 && elf_section_data (sec)->relocs != internal_relocs)
6524 free (internal_relocs);
6528 static bfd_byte *
6529 retrieve_contents (bfd *abfd, asection *sec, bfd_boolean keep_memory)
6531 bfd_byte *contents;
6532 bfd_size_type sec_size;
6534 sec_size = bfd_get_section_limit (abfd, sec);
6535 contents = elf_section_data (sec)->this_hdr.contents;
6537 if (contents == NULL && sec_size != 0)
6539 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6541 if (contents)
6542 free (contents);
6543 return NULL;
6545 if (keep_memory)
6546 elf_section_data (sec)->this_hdr.contents = contents;
6548 return contents;
6552 static void
6553 pin_contents (asection *sec, bfd_byte *contents)
6555 elf_section_data (sec)->this_hdr.contents = contents;
6559 static void
6560 release_contents (asection *sec, bfd_byte *contents)
6562 if (contents && elf_section_data (sec)->this_hdr.contents != contents)
6563 free (contents);
6567 static Elf_Internal_Sym *
6568 retrieve_local_syms (bfd *input_bfd)
6570 Elf_Internal_Shdr *symtab_hdr;
6571 Elf_Internal_Sym *isymbuf;
6572 size_t locsymcount;
6574 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6575 locsymcount = symtab_hdr->sh_info;
6577 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6578 if (isymbuf == NULL && locsymcount != 0)
6579 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6580 NULL, NULL, NULL);
6582 /* Save the symbols for this input file so they won't be read again. */
6583 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
6584 symtab_hdr->contents = (unsigned char *) isymbuf;
6586 return isymbuf;
6590 /* Code for link-time relaxation. */
6592 /* Initialization for relaxation: */
6593 static bfd_boolean analyze_relocations (struct bfd_link_info *);
6594 static bfd_boolean find_relaxable_sections
6595 (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
6596 static bfd_boolean collect_source_relocs
6597 (bfd *, asection *, struct bfd_link_info *);
6598 static bfd_boolean is_resolvable_asm_expansion
6599 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, struct bfd_link_info *,
6600 bfd_boolean *);
6601 static Elf_Internal_Rela *find_associated_l32r_irel
6602 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Rela *);
6603 static bfd_boolean compute_text_actions
6604 (bfd *, asection *, struct bfd_link_info *);
6605 static bfd_boolean compute_ebb_proposed_actions (ebb_constraint *);
6606 static bfd_boolean compute_ebb_actions (ebb_constraint *);
6607 static bfd_boolean check_section_ebb_pcrels_fit
6608 (bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, const ebb_constraint *,
6609 const xtensa_opcode *);
6610 static bfd_boolean check_section_ebb_reduces (const ebb_constraint *);
6611 static void text_action_add_proposed
6612 (text_action_list *, const ebb_constraint *, asection *);
6613 static int compute_fill_extra_space (property_table_entry *);
6615 /* First pass: */
6616 static bfd_boolean compute_removed_literals
6617 (bfd *, asection *, struct bfd_link_info *, value_map_hash_table *);
6618 static Elf_Internal_Rela *get_irel_at_offset
6619 (asection *, Elf_Internal_Rela *, bfd_vma);
6620 static bfd_boolean is_removable_literal
6621 (const source_reloc *, int, const source_reloc *, int, asection *,
6622 property_table_entry *, int);
6623 static bfd_boolean remove_dead_literal
6624 (bfd *, asection *, struct bfd_link_info *, Elf_Internal_Rela *,
6625 Elf_Internal_Rela *, source_reloc *, property_table_entry *, int);
6626 static bfd_boolean identify_literal_placement
6627 (bfd *, asection *, bfd_byte *, struct bfd_link_info *,
6628 value_map_hash_table *, bfd_boolean *, Elf_Internal_Rela *, int,
6629 source_reloc *, property_table_entry *, int, section_cache_t *,
6630 bfd_boolean);
6631 static bfd_boolean relocations_reach (source_reloc *, int, const r_reloc *);
6632 static bfd_boolean coalesce_shared_literal
6633 (asection *, source_reloc *, property_table_entry *, int, value_map *);
6634 static bfd_boolean move_shared_literal
6635 (asection *, struct bfd_link_info *, source_reloc *, property_table_entry *,
6636 int, const r_reloc *, const literal_value *, section_cache_t *);
6638 /* Second pass: */
6639 static bfd_boolean relax_section (bfd *, asection *, struct bfd_link_info *);
6640 static bfd_boolean translate_section_fixes (asection *);
6641 static bfd_boolean translate_reloc_bfd_fix (reloc_bfd_fix *);
6642 static asection *translate_reloc (const r_reloc *, r_reloc *, asection *);
6643 static void shrink_dynamic_reloc_sections
6644 (struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *);
6645 static bfd_boolean move_literal
6646 (bfd *, struct bfd_link_info *, asection *, bfd_vma, bfd_byte *,
6647 xtensa_relax_info *, Elf_Internal_Rela **, const literal_value *);
6648 static bfd_boolean relax_property_section
6649 (bfd *, asection *, struct bfd_link_info *);
6651 /* Third pass: */
6652 static bfd_boolean relax_section_symbols (bfd *, asection *);
6655 static bfd_boolean
6656 elf_xtensa_relax_section (bfd *abfd,
6657 asection *sec,
6658 struct bfd_link_info *link_info,
6659 bfd_boolean *again)
6661 static value_map_hash_table *values = NULL;
6662 static bfd_boolean relocations_analyzed = FALSE;
6663 xtensa_relax_info *relax_info;
6665 if (!relocations_analyzed)
6667 /* Do some overall initialization for relaxation. */
6668 values = value_map_hash_table_init ();
6669 if (values == NULL)
6670 return FALSE;
6671 relaxing_section = TRUE;
6672 if (!analyze_relocations (link_info))
6673 return FALSE;
6674 relocations_analyzed = TRUE;
6676 *again = FALSE;
6678 /* Don't mess with linker-created sections. */
6679 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6680 return TRUE;
6682 relax_info = get_xtensa_relax_info (sec);
6683 BFD_ASSERT (relax_info != NULL);
6685 switch (relax_info->visited)
6687 case 0:
6688 /* Note: It would be nice to fold this pass into
6689 analyze_relocations, but it is important for this step that the
6690 sections be examined in link order. */
6691 if (!compute_removed_literals (abfd, sec, link_info, values))
6692 return FALSE;
6693 *again = TRUE;
6694 break;
6696 case 1:
6697 if (values)
6698 value_map_hash_table_delete (values);
6699 values = NULL;
6700 if (!relax_section (abfd, sec, link_info))
6701 return FALSE;
6702 *again = TRUE;
6703 break;
6705 case 2:
6706 if (!relax_section_symbols (abfd, sec))
6707 return FALSE;
6708 break;
6711 relax_info->visited++;
6712 return TRUE;
6716 /* Initialization for relaxation. */
6718 /* This function is called once at the start of relaxation. It scans
6719 all the input sections and marks the ones that are relaxable (i.e.,
6720 literal sections with L32R relocations against them), and then
6721 collects source_reloc information for all the relocations against
6722 those relaxable sections. During this process, it also detects
6723 longcalls, i.e., calls relaxed by the assembler into indirect
6724 calls, that can be optimized back into direct calls. Within each
6725 extended basic block (ebb) containing an optimized longcall, it
6726 computes a set of "text actions" that can be performed to remove
6727 the L32R associated with the longcall while optionally preserving
6728 branch target alignments. */
6730 static bfd_boolean
6731 analyze_relocations (struct bfd_link_info *link_info)
6733 bfd *abfd;
6734 asection *sec;
6735 bfd_boolean is_relaxable = FALSE;
6737 /* Initialize the per-section relaxation info. */
6738 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6739 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6741 init_xtensa_relax_info (sec);
6744 /* Mark relaxable sections (and count relocations against each one). */
6745 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6746 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6748 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
6749 return FALSE;
6752 /* Bail out if there are no relaxable sections. */
6753 if (!is_relaxable)
6754 return TRUE;
6756 /* Allocate space for source_relocs. */
6757 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6758 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6760 xtensa_relax_info *relax_info;
6762 relax_info = get_xtensa_relax_info (sec);
6763 if (relax_info->is_relaxable_literal_section
6764 || relax_info->is_relaxable_asm_section)
6766 relax_info->src_relocs = (source_reloc *)
6767 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
6769 else
6770 relax_info->src_count = 0;
6773 /* Collect info on relocations against each relaxable section. */
6774 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6775 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6777 if (!collect_source_relocs (abfd, sec, link_info))
6778 return FALSE;
6781 /* Compute the text actions. */
6782 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6783 for (sec = abfd->sections; sec != NULL; sec = sec->next)
6785 if (!compute_text_actions (abfd, sec, link_info))
6786 return FALSE;
6789 return TRUE;
6793 /* Find all the sections that might be relaxed. The motivation for
6794 this pass is that collect_source_relocs() needs to record _all_ the
6795 relocations that target each relaxable section. That is expensive
6796 and unnecessary unless the target section is actually going to be
6797 relaxed. This pass identifies all such sections by checking if
6798 they have L32Rs pointing to them. In the process, the total number
6799 of relocations targeting each section is also counted so that we
6800 know how much space to allocate for source_relocs against each
6801 relaxable literal section. */
6803 static bfd_boolean
6804 find_relaxable_sections (bfd *abfd,
6805 asection *sec,
6806 struct bfd_link_info *link_info,
6807 bfd_boolean *is_relaxable_p)
6809 Elf_Internal_Rela *internal_relocs;
6810 bfd_byte *contents;
6811 bfd_boolean ok = TRUE;
6812 unsigned i;
6813 xtensa_relax_info *source_relax_info;
6814 bfd_boolean is_l32r_reloc;
6816 internal_relocs = retrieve_internal_relocs (abfd, sec,
6817 link_info->keep_memory);
6818 if (internal_relocs == NULL)
6819 return ok;
6821 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6822 if (contents == NULL && sec->size != 0)
6824 ok = FALSE;
6825 goto error_return;
6828 source_relax_info = get_xtensa_relax_info (sec);
6829 for (i = 0; i < sec->reloc_count; i++)
6831 Elf_Internal_Rela *irel = &internal_relocs[i];
6832 r_reloc r_rel;
6833 asection *target_sec;
6834 xtensa_relax_info *target_relax_info;
6836 /* If this section has not already been marked as "relaxable", and
6837 if it contains any ASM_EXPAND relocations (marking expanded
6838 longcalls) that can be optimized into direct calls, then mark
6839 the section as "relaxable". */
6840 if (source_relax_info
6841 && !source_relax_info->is_relaxable_asm_section
6842 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_EXPAND)
6844 bfd_boolean is_reachable = FALSE;
6845 if (is_resolvable_asm_expansion (abfd, sec, contents, irel,
6846 link_info, &is_reachable)
6847 && is_reachable)
6849 source_relax_info->is_relaxable_asm_section = TRUE;
6850 *is_relaxable_p = TRUE;
6854 r_reloc_init (&r_rel, abfd, irel, contents,
6855 bfd_get_section_limit (abfd, sec));
6857 target_sec = r_reloc_get_section (&r_rel);
6858 target_relax_info = get_xtensa_relax_info (target_sec);
6859 if (!target_relax_info)
6860 continue;
6862 /* Count PC-relative operand relocations against the target section.
6863 Note: The conditions tested here must match the conditions under
6864 which init_source_reloc is called in collect_source_relocs(). */
6865 is_l32r_reloc = FALSE;
6866 if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
6868 xtensa_opcode opcode =
6869 get_relocation_opcode (abfd, sec, contents, irel);
6870 if (opcode != XTENSA_UNDEFINED)
6872 is_l32r_reloc = (opcode == get_l32r_opcode ());
6873 if (!is_alt_relocation (ELF32_R_TYPE (irel->r_info))
6874 || is_l32r_reloc)
6875 target_relax_info->src_count++;
6879 if (is_l32r_reloc && r_reloc_is_defined (&r_rel))
6881 /* Mark the target section as relaxable. */
6882 target_relax_info->is_relaxable_literal_section = TRUE;
6883 *is_relaxable_p = TRUE;
6887 error_return:
6888 release_contents (sec, contents);
6889 release_internal_relocs (sec, internal_relocs);
6890 return ok;
6894 /* Record _all_ the relocations that point to relaxable sections, and
6895 get rid of ASM_EXPAND relocs by either converting them to
6896 ASM_SIMPLIFY or by removing them. */
6898 static bfd_boolean
6899 collect_source_relocs (bfd *abfd,
6900 asection *sec,
6901 struct bfd_link_info *link_info)
6903 Elf_Internal_Rela *internal_relocs;
6904 bfd_byte *contents;
6905 bfd_boolean ok = TRUE;
6906 unsigned i;
6907 bfd_size_type sec_size;
6909 internal_relocs = retrieve_internal_relocs (abfd, sec,
6910 link_info->keep_memory);
6911 if (internal_relocs == NULL)
6912 return ok;
6914 sec_size = bfd_get_section_limit (abfd, sec);
6915 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6916 if (contents == NULL && sec_size != 0)
6918 ok = FALSE;
6919 goto error_return;
6922 /* Record relocations against relaxable literal sections. */
6923 for (i = 0; i < sec->reloc_count; i++)
6925 Elf_Internal_Rela *irel = &internal_relocs[i];
6926 r_reloc r_rel;
6927 asection *target_sec;
6928 xtensa_relax_info *target_relax_info;
6930 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
6932 target_sec = r_reloc_get_section (&r_rel);
6933 target_relax_info = get_xtensa_relax_info (target_sec);
6935 if (target_relax_info
6936 && (target_relax_info->is_relaxable_literal_section
6937 || target_relax_info->is_relaxable_asm_section))
6939 xtensa_opcode opcode = XTENSA_UNDEFINED;
6940 int opnd = -1;
6941 bfd_boolean is_abs_literal = FALSE;
6943 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
6945 /* None of the current alternate relocs are PC-relative,
6946 and only PC-relative relocs matter here. However, we
6947 still need to record the opcode for literal
6948 coalescing. */
6949 opcode = get_relocation_opcode (abfd, sec, contents, irel);
6950 if (opcode == get_l32r_opcode ())
6952 is_abs_literal = TRUE;
6953 opnd = 1;
6955 else
6956 opcode = XTENSA_UNDEFINED;
6958 else if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
6960 opcode = get_relocation_opcode (abfd, sec, contents, irel);
6961 opnd = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
6964 if (opcode != XTENSA_UNDEFINED)
6966 int src_next = target_relax_info->src_next++;
6967 source_reloc *s_reloc = &target_relax_info->src_relocs[src_next];
6969 init_source_reloc (s_reloc, sec, &r_rel, opcode, opnd,
6970 is_abs_literal);
6975 /* Now get rid of ASM_EXPAND relocations. At this point, the
6976 src_relocs array for the target literal section may still be
6977 incomplete, but it must at least contain the entries for the L32R
6978 relocations associated with ASM_EXPANDs because they were just
6979 added in the preceding loop over the relocations. */
6981 for (i = 0; i < sec->reloc_count; i++)
6983 Elf_Internal_Rela *irel = &internal_relocs[i];
6984 bfd_boolean is_reachable;
6986 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
6987 &is_reachable))
6988 continue;
6990 if (is_reachable)
6992 Elf_Internal_Rela *l32r_irel;
6993 r_reloc r_rel;
6994 asection *target_sec;
6995 xtensa_relax_info *target_relax_info;
6997 /* Mark the source_reloc for the L32R so that it will be
6998 removed in compute_removed_literals(), along with the
6999 associated literal. */
7000 l32r_irel = find_associated_l32r_irel (abfd, sec, contents,
7001 irel, internal_relocs);
7002 if (l32r_irel == NULL)
7003 continue;
7005 r_reloc_init (&r_rel, abfd, l32r_irel, contents, sec_size);
7007 target_sec = r_reloc_get_section (&r_rel);
7008 target_relax_info = get_xtensa_relax_info (target_sec);
7010 if (target_relax_info
7011 && (target_relax_info->is_relaxable_literal_section
7012 || target_relax_info->is_relaxable_asm_section))
7014 source_reloc *s_reloc;
7016 /* Search the source_relocs for the entry corresponding to
7017 the l32r_irel. Note: The src_relocs array is not yet
7018 sorted, but it wouldn't matter anyway because we're
7019 searching by source offset instead of target offset. */
7020 s_reloc = find_source_reloc (target_relax_info->src_relocs,
7021 target_relax_info->src_next,
7022 sec, l32r_irel);
7023 BFD_ASSERT (s_reloc);
7024 s_reloc->is_null = TRUE;
7027 /* Convert this reloc to ASM_SIMPLIFY. */
7028 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
7029 R_XTENSA_ASM_SIMPLIFY);
7030 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7032 pin_internal_relocs (sec, internal_relocs);
7034 else
7036 /* It is resolvable but doesn't reach. We resolve now
7037 by eliminating the relocation -- the call will remain
7038 expanded into L32R/CALLX. */
7039 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7040 pin_internal_relocs (sec, internal_relocs);
7044 error_return:
7045 release_contents (sec, contents);
7046 release_internal_relocs (sec, internal_relocs);
7047 return ok;
7051 /* Return TRUE if the asm expansion can be resolved. Generally it can
7052 be resolved on a final link or when a partial link locates it in the
7053 same section as the target. Set "is_reachable" flag if the target of
7054 the call is within the range of a direct call, given the current VMA
7055 for this section and the target section. */
7057 bfd_boolean
7058 is_resolvable_asm_expansion (bfd *abfd,
7059 asection *sec,
7060 bfd_byte *contents,
7061 Elf_Internal_Rela *irel,
7062 struct bfd_link_info *link_info,
7063 bfd_boolean *is_reachable_p)
7065 asection *target_sec;
7066 bfd_vma target_offset;
7067 r_reloc r_rel;
7068 xtensa_opcode opcode, direct_call_opcode;
7069 bfd_vma self_address;
7070 bfd_vma dest_address;
7071 bfd_boolean uses_l32r;
7072 bfd_size_type sec_size;
7074 *is_reachable_p = FALSE;
7076 if (contents == NULL)
7077 return FALSE;
7079 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
7080 return FALSE;
7082 sec_size = bfd_get_section_limit (abfd, sec);
7083 opcode = get_expanded_call_opcode (contents + irel->r_offset,
7084 sec_size - irel->r_offset, &uses_l32r);
7085 /* Optimization of longcalls that use CONST16 is not yet implemented. */
7086 if (!uses_l32r)
7087 return FALSE;
7089 direct_call_opcode = swap_callx_for_call_opcode (opcode);
7090 if (direct_call_opcode == XTENSA_UNDEFINED)
7091 return FALSE;
7093 /* Check and see that the target resolves. */
7094 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7095 if (!r_reloc_is_defined (&r_rel))
7096 return FALSE;
7098 target_sec = r_reloc_get_section (&r_rel);
7099 target_offset = r_rel.target_offset;
7101 /* If the target is in a shared library, then it doesn't reach. This
7102 isn't supposed to come up because the compiler should never generate
7103 non-PIC calls on systems that use shared libraries, but the linker
7104 shouldn't crash regardless. */
7105 if (!target_sec->output_section)
7106 return FALSE;
7108 /* For relocatable sections, we can only simplify when the output
7109 section of the target is the same as the output section of the
7110 source. */
7111 if (link_info->relocatable
7112 && (target_sec->output_section != sec->output_section
7113 || is_reloc_sym_weak (abfd, irel)))
7114 return FALSE;
7116 self_address = (sec->output_section->vma
7117 + sec->output_offset + irel->r_offset + 3);
7118 dest_address = (target_sec->output_section->vma
7119 + target_sec->output_offset + target_offset);
7121 *is_reachable_p = pcrel_reloc_fits (direct_call_opcode, 0,
7122 self_address, dest_address);
7124 if ((self_address >> CALL_SEGMENT_BITS) !=
7125 (dest_address >> CALL_SEGMENT_BITS))
7126 return FALSE;
7128 return TRUE;
7132 static Elf_Internal_Rela *
7133 find_associated_l32r_irel (bfd *abfd,
7134 asection *sec,
7135 bfd_byte *contents,
7136 Elf_Internal_Rela *other_irel,
7137 Elf_Internal_Rela *internal_relocs)
7139 unsigned i;
7141 for (i = 0; i < sec->reloc_count; i++)
7143 Elf_Internal_Rela *irel = &internal_relocs[i];
7145 if (irel == other_irel)
7146 continue;
7147 if (irel->r_offset != other_irel->r_offset)
7148 continue;
7149 if (is_l32r_relocation (abfd, sec, contents, irel))
7150 return irel;
7153 return NULL;
7157 static xtensa_opcode *
7158 build_reloc_opcodes (bfd *abfd,
7159 asection *sec,
7160 bfd_byte *contents,
7161 Elf_Internal_Rela *internal_relocs)
7163 unsigned i;
7164 xtensa_opcode *reloc_opcodes =
7165 (xtensa_opcode *) bfd_malloc (sizeof (xtensa_opcode) * sec->reloc_count);
7166 for (i = 0; i < sec->reloc_count; i++)
7168 Elf_Internal_Rela *irel = &internal_relocs[i];
7169 reloc_opcodes[i] = get_relocation_opcode (abfd, sec, contents, irel);
7171 return reloc_opcodes;
7175 /* The compute_text_actions function will build a list of potential
7176 transformation actions for code in the extended basic block of each
7177 longcall that is optimized to a direct call. From this list we
7178 generate a set of actions to actually perform that optimizes for
7179 space and, if not using size_opt, maintains branch target
7180 alignments.
7182 These actions to be performed are placed on a per-section list.
7183 The actual changes are performed by relax_section() in the second
7184 pass. */
7186 bfd_boolean
7187 compute_text_actions (bfd *abfd,
7188 asection *sec,
7189 struct bfd_link_info *link_info)
7191 xtensa_opcode *reloc_opcodes = NULL;
7192 xtensa_relax_info *relax_info;
7193 bfd_byte *contents;
7194 Elf_Internal_Rela *internal_relocs;
7195 bfd_boolean ok = TRUE;
7196 unsigned i;
7197 property_table_entry *prop_table = 0;
7198 int ptblsize = 0;
7199 bfd_size_type sec_size;
7201 relax_info = get_xtensa_relax_info (sec);
7202 BFD_ASSERT (relax_info);
7203 BFD_ASSERT (relax_info->src_next == relax_info->src_count);
7205 /* Do nothing if the section contains no optimized longcalls. */
7206 if (!relax_info->is_relaxable_asm_section)
7207 return ok;
7209 internal_relocs = retrieve_internal_relocs (abfd, sec,
7210 link_info->keep_memory);
7212 if (internal_relocs)
7213 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
7214 internal_reloc_compare);
7216 sec_size = bfd_get_section_limit (abfd, sec);
7217 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
7218 if (contents == NULL && sec_size != 0)
7220 ok = FALSE;
7221 goto error_return;
7224 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
7225 XTENSA_PROP_SEC_NAME, FALSE);
7226 if (ptblsize < 0)
7228 ok = FALSE;
7229 goto error_return;
7232 for (i = 0; i < sec->reloc_count; i++)
7234 Elf_Internal_Rela *irel = &internal_relocs[i];
7235 bfd_vma r_offset;
7236 property_table_entry *the_entry;
7237 int ptbl_idx;
7238 ebb_t *ebb;
7239 ebb_constraint ebb_table;
7240 bfd_size_type simplify_size;
7242 if (irel && ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_SIMPLIFY)
7243 continue;
7244 r_offset = irel->r_offset;
7246 simplify_size = get_asm_simplify_size (contents, sec_size, r_offset);
7247 if (simplify_size == 0)
7249 (*_bfd_error_handler)
7250 (_("%B(%A+0x%lx): could not decode instruction for XTENSA_ASM_SIMPLIFY relocation; possible configuration mismatch"),
7251 sec->owner, sec, r_offset);
7252 continue;
7255 /* If the instruction table is not around, then don't do this
7256 relaxation. */
7257 the_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
7258 sec->vma + irel->r_offset);
7259 if (the_entry == NULL || XTENSA_NO_NOP_REMOVAL)
7261 text_action_add (&relax_info->action_list,
7262 ta_convert_longcall, sec, r_offset,
7264 continue;
7267 /* If the next longcall happens to be at the same address as an
7268 unreachable section of size 0, then skip forward. */
7269 ptbl_idx = the_entry - prop_table;
7270 while ((the_entry->flags & XTENSA_PROP_UNREACHABLE)
7271 && the_entry->size == 0
7272 && ptbl_idx + 1 < ptblsize
7273 && (prop_table[ptbl_idx + 1].address
7274 == prop_table[ptbl_idx].address))
7276 ptbl_idx++;
7277 the_entry++;
7280 if (the_entry->flags & XTENSA_PROP_NO_TRANSFORM)
7281 /* NO_REORDER is OK */
7282 continue;
7284 init_ebb_constraint (&ebb_table);
7285 ebb = &ebb_table.ebb;
7286 init_ebb (ebb, sec, contents, sec_size, prop_table, ptblsize,
7287 internal_relocs, sec->reloc_count);
7288 ebb->start_offset = r_offset + simplify_size;
7289 ebb->end_offset = r_offset + simplify_size;
7290 ebb->start_ptbl_idx = ptbl_idx;
7291 ebb->end_ptbl_idx = ptbl_idx;
7292 ebb->start_reloc_idx = i;
7293 ebb->end_reloc_idx = i;
7295 /* Precompute the opcode for each relocation. */
7296 if (reloc_opcodes == NULL)
7297 reloc_opcodes = build_reloc_opcodes (abfd, sec, contents,
7298 internal_relocs);
7300 if (!extend_ebb_bounds (ebb)
7301 || !compute_ebb_proposed_actions (&ebb_table)
7302 || !compute_ebb_actions (&ebb_table)
7303 || !check_section_ebb_pcrels_fit (abfd, sec, contents,
7304 internal_relocs, &ebb_table,
7305 reloc_opcodes)
7306 || !check_section_ebb_reduces (&ebb_table))
7308 /* If anything goes wrong or we get unlucky and something does
7309 not fit, with our plan because of expansion between
7310 critical branches, just convert to a NOP. */
7312 text_action_add (&relax_info->action_list,
7313 ta_convert_longcall, sec, r_offset, 0);
7314 i = ebb_table.ebb.end_reloc_idx;
7315 free_ebb_constraint (&ebb_table);
7316 continue;
7319 text_action_add_proposed (&relax_info->action_list, &ebb_table, sec);
7321 /* Update the index so we do not go looking at the relocations
7322 we have already processed. */
7323 i = ebb_table.ebb.end_reloc_idx;
7324 free_ebb_constraint (&ebb_table);
7327 #if DEBUG
7328 if (relax_info->action_list.head)
7329 print_action_list (stderr, &relax_info->action_list);
7330 #endif
7332 error_return:
7333 release_contents (sec, contents);
7334 release_internal_relocs (sec, internal_relocs);
7335 if (prop_table)
7336 free (prop_table);
7337 if (reloc_opcodes)
7338 free (reloc_opcodes);
7340 return ok;
7344 /* Do not widen an instruction if it is preceeded by a
7345 loop opcode. It might cause misalignment. */
7347 static bfd_boolean
7348 prev_instr_is_a_loop (bfd_byte *contents,
7349 bfd_size_type content_length,
7350 bfd_size_type offset)
7352 xtensa_opcode prev_opcode;
7354 if (offset < 3)
7355 return FALSE;
7356 prev_opcode = insn_decode_opcode (contents, content_length, offset-3, 0);
7357 return (xtensa_opcode_is_loop (xtensa_default_isa, prev_opcode) == 1);
7361 /* Find all of the possible actions for an extended basic block. */
7363 bfd_boolean
7364 compute_ebb_proposed_actions (ebb_constraint *ebb_table)
7366 const ebb_t *ebb = &ebb_table->ebb;
7367 unsigned rel_idx = ebb->start_reloc_idx;
7368 property_table_entry *entry, *start_entry, *end_entry;
7369 bfd_vma offset = 0;
7370 xtensa_isa isa = xtensa_default_isa;
7371 xtensa_format fmt;
7372 static xtensa_insnbuf insnbuf = NULL;
7373 static xtensa_insnbuf slotbuf = NULL;
7375 if (insnbuf == NULL)
7377 insnbuf = xtensa_insnbuf_alloc (isa);
7378 slotbuf = xtensa_insnbuf_alloc (isa);
7381 start_entry = &ebb->ptbl[ebb->start_ptbl_idx];
7382 end_entry = &ebb->ptbl[ebb->end_ptbl_idx];
7384 for (entry = start_entry; entry <= end_entry; entry++)
7386 bfd_vma start_offset, end_offset;
7387 bfd_size_type insn_len;
7389 start_offset = entry->address - ebb->sec->vma;
7390 end_offset = entry->address + entry->size - ebb->sec->vma;
7392 if (entry == start_entry)
7393 start_offset = ebb->start_offset;
7394 if (entry == end_entry)
7395 end_offset = ebb->end_offset;
7396 offset = start_offset;
7398 if (offset == entry->address - ebb->sec->vma
7399 && (entry->flags & XTENSA_PROP_INSN_BRANCH_TARGET) != 0)
7401 enum ebb_target_enum align_type = EBB_DESIRE_TGT_ALIGN;
7402 BFD_ASSERT (offset != end_offset);
7403 if (offset == end_offset)
7404 return FALSE;
7406 insn_len = insn_decode_len (ebb->contents, ebb->content_length,
7407 offset);
7408 if (insn_len == 0)
7409 goto decode_error;
7411 if (check_branch_target_aligned_address (offset, insn_len))
7412 align_type = EBB_REQUIRE_TGT_ALIGN;
7414 ebb_propose_action (ebb_table, align_type, 0,
7415 ta_none, offset, 0, TRUE);
7418 while (offset != end_offset)
7420 Elf_Internal_Rela *irel;
7421 xtensa_opcode opcode;
7423 while (rel_idx < ebb->end_reloc_idx
7424 && (ebb->relocs[rel_idx].r_offset < offset
7425 || (ebb->relocs[rel_idx].r_offset == offset
7426 && (ELF32_R_TYPE (ebb->relocs[rel_idx].r_info)
7427 != R_XTENSA_ASM_SIMPLIFY))))
7428 rel_idx++;
7430 /* Check for longcall. */
7431 irel = &ebb->relocs[rel_idx];
7432 if (irel->r_offset == offset
7433 && ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_SIMPLIFY)
7435 bfd_size_type simplify_size;
7437 simplify_size = get_asm_simplify_size (ebb->contents,
7438 ebb->content_length,
7439 irel->r_offset);
7440 if (simplify_size == 0)
7441 goto decode_error;
7443 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7444 ta_convert_longcall, offset, 0, TRUE);
7446 offset += simplify_size;
7447 continue;
7450 if (offset + MIN_INSN_LENGTH > ebb->content_length)
7451 goto decode_error;
7452 xtensa_insnbuf_from_chars (isa, insnbuf, &ebb->contents[offset],
7453 ebb->content_length - offset);
7454 fmt = xtensa_format_decode (isa, insnbuf);
7455 if (fmt == XTENSA_UNDEFINED)
7456 goto decode_error;
7457 insn_len = xtensa_format_length (isa, fmt);
7458 if (insn_len == (bfd_size_type) XTENSA_UNDEFINED)
7459 goto decode_error;
7461 if (xtensa_format_num_slots (isa, fmt) != 1)
7463 offset += insn_len;
7464 continue;
7467 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7468 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7469 if (opcode == XTENSA_UNDEFINED)
7470 goto decode_error;
7472 if ((entry->flags & XTENSA_PROP_INSN_NO_DENSITY) == 0
7473 && (entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7474 && can_narrow_instruction (slotbuf, fmt, opcode) != 0)
7476 /* Add an instruction narrow action. */
7477 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7478 ta_narrow_insn, offset, 0, FALSE);
7480 else if ((entry->flags & XTENSA_PROP_NO_TRANSFORM) == 0
7481 && can_widen_instruction (slotbuf, fmt, opcode) != 0
7482 && ! prev_instr_is_a_loop (ebb->contents,
7483 ebb->content_length, offset))
7485 /* Add an instruction widen action. */
7486 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7487 ta_widen_insn, offset, 0, FALSE);
7489 else if (xtensa_opcode_is_loop (xtensa_default_isa, opcode) == 1)
7491 /* Check for branch targets. */
7492 ebb_propose_action (ebb_table, EBB_REQUIRE_LOOP_ALIGN, 0,
7493 ta_none, offset, 0, TRUE);
7496 offset += insn_len;
7500 if (ebb->ends_unreachable)
7502 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7503 ta_fill, ebb->end_offset, 0, TRUE);
7506 return TRUE;
7508 decode_error:
7509 (*_bfd_error_handler)
7510 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
7511 ebb->sec->owner, ebb->sec, offset);
7512 return FALSE;
7516 /* After all of the information has collected about the
7517 transformations possible in an EBB, compute the appropriate actions
7518 here in compute_ebb_actions. We still must check later to make
7519 sure that the actions do not break any relocations. The algorithm
7520 used here is pretty greedy. Basically, it removes as many no-ops
7521 as possible so that the end of the EBB has the same alignment
7522 characteristics as the original. First, it uses narrowing, then
7523 fill space at the end of the EBB, and finally widenings. If that
7524 does not work, it tries again with one fewer no-op removed. The
7525 optimization will only be performed if all of the branch targets
7526 that were aligned before transformation are also aligned after the
7527 transformation.
7529 When the size_opt flag is set, ignore the branch target alignments,
7530 narrow all wide instructions, and remove all no-ops unless the end
7531 of the EBB prevents it. */
7533 bfd_boolean
7534 compute_ebb_actions (ebb_constraint *ebb_table)
7536 unsigned i = 0;
7537 unsigned j;
7538 int removed_bytes = 0;
7539 ebb_t *ebb = &ebb_table->ebb;
7540 unsigned seg_idx_start = 0;
7541 unsigned seg_idx_end = 0;
7543 /* We perform this like the assembler relaxation algorithm: Start by
7544 assuming all instructions are narrow and all no-ops removed; then
7545 walk through.... */
7547 /* For each segment of this that has a solid constraint, check to
7548 see if there are any combinations that will keep the constraint.
7549 If so, use it. */
7550 for (seg_idx_end = 0; seg_idx_end < ebb_table->action_count; seg_idx_end++)
7552 bfd_boolean requires_text_end_align = FALSE;
7553 unsigned longcall_count = 0;
7554 unsigned longcall_convert_count = 0;
7555 unsigned narrowable_count = 0;
7556 unsigned narrowable_convert_count = 0;
7557 unsigned widenable_count = 0;
7558 unsigned widenable_convert_count = 0;
7560 proposed_action *action = NULL;
7561 int align = (1 << ebb_table->ebb.sec->alignment_power);
7563 seg_idx_start = seg_idx_end;
7565 for (i = seg_idx_start; i < ebb_table->action_count; i++)
7567 action = &ebb_table->actions[i];
7568 if (action->action == ta_convert_longcall)
7569 longcall_count++;
7570 if (action->action == ta_narrow_insn)
7571 narrowable_count++;
7572 if (action->action == ta_widen_insn)
7573 widenable_count++;
7574 if (action->action == ta_fill)
7575 break;
7576 if (action->align_type == EBB_REQUIRE_LOOP_ALIGN)
7577 break;
7578 if (action->align_type == EBB_REQUIRE_TGT_ALIGN
7579 && !elf32xtensa_size_opt)
7580 break;
7582 seg_idx_end = i;
7584 if (seg_idx_end == ebb_table->action_count && !ebb->ends_unreachable)
7585 requires_text_end_align = TRUE;
7587 if (elf32xtensa_size_opt && !requires_text_end_align
7588 && action->align_type != EBB_REQUIRE_LOOP_ALIGN
7589 && action->align_type != EBB_REQUIRE_TGT_ALIGN)
7591 longcall_convert_count = longcall_count;
7592 narrowable_convert_count = narrowable_count;
7593 widenable_convert_count = 0;
7595 else
7597 /* There is a constraint. Convert the max number of longcalls. */
7598 narrowable_convert_count = 0;
7599 longcall_convert_count = 0;
7600 widenable_convert_count = 0;
7602 for (j = 0; j < longcall_count; j++)
7604 int removed = (longcall_count - j) * 3 & (align - 1);
7605 unsigned desire_narrow = (align - removed) & (align - 1);
7606 unsigned desire_widen = removed;
7607 if (desire_narrow <= narrowable_count)
7609 narrowable_convert_count = desire_narrow;
7610 narrowable_convert_count +=
7611 (align * ((narrowable_count - narrowable_convert_count)
7612 / align));
7613 longcall_convert_count = (longcall_count - j);
7614 widenable_convert_count = 0;
7615 break;
7617 if (desire_widen <= widenable_count && !elf32xtensa_size_opt)
7619 narrowable_convert_count = 0;
7620 longcall_convert_count = longcall_count - j;
7621 widenable_convert_count = desire_widen;
7622 break;
7627 /* Now the number of conversions are saved. Do them. */
7628 for (i = seg_idx_start; i < seg_idx_end; i++)
7630 action = &ebb_table->actions[i];
7631 switch (action->action)
7633 case ta_convert_longcall:
7634 if (longcall_convert_count != 0)
7636 action->action = ta_remove_longcall;
7637 action->do_action = TRUE;
7638 action->removed_bytes += 3;
7639 longcall_convert_count--;
7641 break;
7642 case ta_narrow_insn:
7643 if (narrowable_convert_count != 0)
7645 action->do_action = TRUE;
7646 action->removed_bytes += 1;
7647 narrowable_convert_count--;
7649 break;
7650 case ta_widen_insn:
7651 if (widenable_convert_count != 0)
7653 action->do_action = TRUE;
7654 action->removed_bytes -= 1;
7655 widenable_convert_count--;
7657 break;
7658 default:
7659 break;
7664 /* Now we move on to some local opts. Try to remove each of the
7665 remaining longcalls. */
7667 if (ebb_table->ebb.ends_section || ebb_table->ebb.ends_unreachable)
7669 removed_bytes = 0;
7670 for (i = 0; i < ebb_table->action_count; i++)
7672 int old_removed_bytes = removed_bytes;
7673 proposed_action *action = &ebb_table->actions[i];
7675 if (action->do_action && action->action == ta_convert_longcall)
7677 bfd_boolean bad_alignment = FALSE;
7678 removed_bytes += 3;
7679 for (j = i + 1; j < ebb_table->action_count; j++)
7681 proposed_action *new_action = &ebb_table->actions[j];
7682 bfd_vma offset = new_action->offset;
7683 if (new_action->align_type == EBB_REQUIRE_TGT_ALIGN)
7685 if (!check_branch_target_aligned
7686 (ebb_table->ebb.contents,
7687 ebb_table->ebb.content_length,
7688 offset, offset - removed_bytes))
7690 bad_alignment = TRUE;
7691 break;
7694 if (new_action->align_type == EBB_REQUIRE_LOOP_ALIGN)
7696 if (!check_loop_aligned (ebb_table->ebb.contents,
7697 ebb_table->ebb.content_length,
7698 offset,
7699 offset - removed_bytes))
7701 bad_alignment = TRUE;
7702 break;
7705 if (new_action->action == ta_narrow_insn
7706 && !new_action->do_action
7707 && ebb_table->ebb.sec->alignment_power == 2)
7709 /* Narrow an instruction and we are done. */
7710 new_action->do_action = TRUE;
7711 new_action->removed_bytes += 1;
7712 bad_alignment = FALSE;
7713 break;
7715 if (new_action->action == ta_widen_insn
7716 && new_action->do_action
7717 && ebb_table->ebb.sec->alignment_power == 2)
7719 /* Narrow an instruction and we are done. */
7720 new_action->do_action = FALSE;
7721 new_action->removed_bytes += 1;
7722 bad_alignment = FALSE;
7723 break;
7725 if (new_action->do_action)
7726 removed_bytes += new_action->removed_bytes;
7728 if (!bad_alignment)
7730 action->removed_bytes += 3;
7731 action->action = ta_remove_longcall;
7732 action->do_action = TRUE;
7735 removed_bytes = old_removed_bytes;
7736 if (action->do_action)
7737 removed_bytes += action->removed_bytes;
7741 removed_bytes = 0;
7742 for (i = 0; i < ebb_table->action_count; ++i)
7744 proposed_action *action = &ebb_table->actions[i];
7745 if (action->do_action)
7746 removed_bytes += action->removed_bytes;
7749 if ((removed_bytes % (1 << ebb_table->ebb.sec->alignment_power)) != 0
7750 && ebb->ends_unreachable)
7752 proposed_action *action;
7753 int br;
7754 int extra_space;
7756 BFD_ASSERT (ebb_table->action_count != 0);
7757 action = &ebb_table->actions[ebb_table->action_count - 1];
7758 BFD_ASSERT (action->action == ta_fill);
7759 BFD_ASSERT (ebb->ends_unreachable->flags & XTENSA_PROP_UNREACHABLE);
7761 extra_space = compute_fill_extra_space (ebb->ends_unreachable);
7762 br = action->removed_bytes + removed_bytes + extra_space;
7763 br = br & ((1 << ebb->sec->alignment_power ) - 1);
7765 action->removed_bytes = extra_space - br;
7767 return TRUE;
7771 /* The xlate_map is a sorted array of address mappings designed to
7772 answer the offset_with_removed_text() query with a binary search instead
7773 of a linear search through the section's action_list. */
7775 typedef struct xlate_map_entry xlate_map_entry_t;
7776 typedef struct xlate_map xlate_map_t;
7778 struct xlate_map_entry
7780 unsigned orig_address;
7781 unsigned new_address;
7782 unsigned size;
7785 struct xlate_map
7787 unsigned entry_count;
7788 xlate_map_entry_t *entry;
7792 static int
7793 xlate_compare (const void *a_v, const void *b_v)
7795 const xlate_map_entry_t *a = (const xlate_map_entry_t *) a_v;
7796 const xlate_map_entry_t *b = (const xlate_map_entry_t *) b_v;
7797 if (a->orig_address < b->orig_address)
7798 return -1;
7799 if (a->orig_address > (b->orig_address + b->size - 1))
7800 return 1;
7801 return 0;
7805 static bfd_vma
7806 xlate_offset_with_removed_text (const xlate_map_t *map,
7807 text_action_list *action_list,
7808 bfd_vma offset)
7810 xlate_map_entry_t tmp;
7811 void *r;
7812 xlate_map_entry_t *e;
7814 if (map == NULL)
7815 return offset_with_removed_text (action_list, offset);
7817 if (map->entry_count == 0)
7818 return offset;
7820 tmp.orig_address = offset;
7821 tmp.new_address = offset;
7822 tmp.size = 1;
7824 r = bsearch (&offset, map->entry, map->entry_count,
7825 sizeof (xlate_map_entry_t), &xlate_compare);
7826 e = (xlate_map_entry_t *) r;
7828 BFD_ASSERT (e != NULL);
7829 if (e == NULL)
7830 return offset;
7831 return e->new_address - e->orig_address + offset;
7835 /* Build a binary searchable offset translation map from a section's
7836 action list. */
7838 static xlate_map_t *
7839 build_xlate_map (asection *sec, xtensa_relax_info *relax_info)
7841 xlate_map_t *map = (xlate_map_t *) bfd_malloc (sizeof (xlate_map_t));
7842 text_action_list *action_list = &relax_info->action_list;
7843 unsigned num_actions = 0;
7844 text_action *r;
7845 int removed;
7846 xlate_map_entry_t *current_entry;
7848 if (map == NULL)
7849 return NULL;
7851 num_actions = action_list_count (action_list);
7852 map->entry = (xlate_map_entry_t *)
7853 bfd_malloc (sizeof (xlate_map_entry_t) * (num_actions + 1));
7854 if (map->entry == NULL)
7856 free (map);
7857 return NULL;
7859 map->entry_count = 0;
7861 removed = 0;
7862 current_entry = &map->entry[0];
7864 current_entry->orig_address = 0;
7865 current_entry->new_address = 0;
7866 current_entry->size = 0;
7868 for (r = action_list->head; r != NULL; r = r->next)
7870 unsigned orig_size = 0;
7871 switch (r->action)
7873 case ta_none:
7874 case ta_remove_insn:
7875 case ta_convert_longcall:
7876 case ta_remove_literal:
7877 case ta_add_literal:
7878 break;
7879 case ta_remove_longcall:
7880 orig_size = 6;
7881 break;
7882 case ta_narrow_insn:
7883 orig_size = 3;
7884 break;
7885 case ta_widen_insn:
7886 orig_size = 2;
7887 break;
7888 case ta_fill:
7889 break;
7891 current_entry->size =
7892 r->offset + orig_size - current_entry->orig_address;
7893 if (current_entry->size != 0)
7895 current_entry++;
7896 map->entry_count++;
7898 current_entry->orig_address = r->offset + orig_size;
7899 removed += r->removed_bytes;
7900 current_entry->new_address = r->offset + orig_size - removed;
7901 current_entry->size = 0;
7904 current_entry->size = (bfd_get_section_limit (sec->owner, sec)
7905 - current_entry->orig_address);
7906 if (current_entry->size != 0)
7907 map->entry_count++;
7909 return map;
7913 /* Free an offset translation map. */
7915 static void
7916 free_xlate_map (xlate_map_t *map)
7918 if (map && map->entry)
7919 free (map->entry);
7920 if (map)
7921 free (map);
7925 /* Use check_section_ebb_pcrels_fit to make sure that all of the
7926 relocations in a section will fit if a proposed set of actions
7927 are performed. */
7929 static bfd_boolean
7930 check_section_ebb_pcrels_fit (bfd *abfd,
7931 asection *sec,
7932 bfd_byte *contents,
7933 Elf_Internal_Rela *internal_relocs,
7934 const ebb_constraint *constraint,
7935 const xtensa_opcode *reloc_opcodes)
7937 unsigned i, j;
7938 Elf_Internal_Rela *irel;
7939 xlate_map_t *xmap = NULL;
7940 bfd_boolean ok = TRUE;
7941 xtensa_relax_info *relax_info;
7943 relax_info = get_xtensa_relax_info (sec);
7945 if (relax_info && sec->reloc_count > 100)
7947 xmap = build_xlate_map (sec, relax_info);
7948 /* NULL indicates out of memory, but the slow version
7949 can still be used. */
7952 for (i = 0; i < sec->reloc_count; i++)
7954 r_reloc r_rel;
7955 bfd_vma orig_self_offset, orig_target_offset;
7956 bfd_vma self_offset, target_offset;
7957 int r_type;
7958 reloc_howto_type *howto;
7959 int self_removed_bytes, target_removed_bytes;
7961 irel = &internal_relocs[i];
7962 r_type = ELF32_R_TYPE (irel->r_info);
7964 howto = &elf_howto_table[r_type];
7965 /* We maintain the required invariant: PC-relative relocations
7966 that fit before linking must fit after linking. Thus we only
7967 need to deal with relocations to the same section that are
7968 PC-relative. */
7969 if (r_type == R_XTENSA_ASM_SIMPLIFY
7970 || r_type == R_XTENSA_32_PCREL
7971 || !howto->pc_relative)
7972 continue;
7974 r_reloc_init (&r_rel, abfd, irel, contents,
7975 bfd_get_section_limit (abfd, sec));
7977 if (r_reloc_get_section (&r_rel) != sec)
7978 continue;
7980 orig_self_offset = irel->r_offset;
7981 orig_target_offset = r_rel.target_offset;
7983 self_offset = orig_self_offset;
7984 target_offset = orig_target_offset;
7986 if (relax_info)
7988 self_offset =
7989 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
7990 orig_self_offset);
7991 target_offset =
7992 xlate_offset_with_removed_text (xmap, &relax_info->action_list,
7993 orig_target_offset);
7996 self_removed_bytes = 0;
7997 target_removed_bytes = 0;
7999 for (j = 0; j < constraint->action_count; ++j)
8001 proposed_action *action = &constraint->actions[j];
8002 bfd_vma offset = action->offset;
8003 int removed_bytes = action->removed_bytes;
8004 if (offset < orig_self_offset
8005 || (offset == orig_self_offset && action->action == ta_fill
8006 && action->removed_bytes < 0))
8007 self_removed_bytes += removed_bytes;
8008 if (offset < orig_target_offset
8009 || (offset == orig_target_offset && action->action == ta_fill
8010 && action->removed_bytes < 0))
8011 target_removed_bytes += removed_bytes;
8013 self_offset -= self_removed_bytes;
8014 target_offset -= target_removed_bytes;
8016 /* Try to encode it. Get the operand and check. */
8017 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
8019 /* None of the current alternate relocs are PC-relative,
8020 and only PC-relative relocs matter here. */
8022 else
8024 xtensa_opcode opcode;
8025 int opnum;
8027 if (reloc_opcodes)
8028 opcode = reloc_opcodes[i];
8029 else
8030 opcode = get_relocation_opcode (abfd, sec, contents, irel);
8031 if (opcode == XTENSA_UNDEFINED)
8033 ok = FALSE;
8034 break;
8037 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
8038 if (opnum == XTENSA_UNDEFINED)
8040 ok = FALSE;
8041 break;
8044 if (!pcrel_reloc_fits (opcode, opnum, self_offset, target_offset))
8046 ok = FALSE;
8047 break;
8052 if (xmap)
8053 free_xlate_map (xmap);
8055 return ok;
8059 static bfd_boolean
8060 check_section_ebb_reduces (const ebb_constraint *constraint)
8062 int removed = 0;
8063 unsigned i;
8065 for (i = 0; i < constraint->action_count; i++)
8067 const proposed_action *action = &constraint->actions[i];
8068 if (action->do_action)
8069 removed += action->removed_bytes;
8071 if (removed < 0)
8072 return FALSE;
8074 return TRUE;
8078 void
8079 text_action_add_proposed (text_action_list *l,
8080 const ebb_constraint *ebb_table,
8081 asection *sec)
8083 unsigned i;
8085 for (i = 0; i < ebb_table->action_count; i++)
8087 proposed_action *action = &ebb_table->actions[i];
8089 if (!action->do_action)
8090 continue;
8091 switch (action->action)
8093 case ta_remove_insn:
8094 case ta_remove_longcall:
8095 case ta_convert_longcall:
8096 case ta_narrow_insn:
8097 case ta_widen_insn:
8098 case ta_fill:
8099 case ta_remove_literal:
8100 text_action_add (l, action->action, sec, action->offset,
8101 action->removed_bytes);
8102 break;
8103 case ta_none:
8104 break;
8105 default:
8106 BFD_ASSERT (0);
8107 break;
8114 compute_fill_extra_space (property_table_entry *entry)
8116 int fill_extra_space;
8118 if (!entry)
8119 return 0;
8121 if ((entry->flags & XTENSA_PROP_UNREACHABLE) == 0)
8122 return 0;
8124 fill_extra_space = entry->size;
8125 if ((entry->flags & XTENSA_PROP_ALIGN) != 0)
8127 /* Fill bytes for alignment:
8128 (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
8129 int pow = GET_XTENSA_PROP_ALIGNMENT (entry->flags);
8130 int nsm = (1 << pow) - 1;
8131 bfd_vma addr = entry->address + entry->size;
8132 bfd_vma align_fill = nsm - ((addr + nsm) & nsm);
8133 fill_extra_space += align_fill;
8135 return fill_extra_space;
8139 /* First relaxation pass. */
8141 /* If the section contains relaxable literals, check each literal to
8142 see if it has the same value as another literal that has already
8143 been seen, either in the current section or a previous one. If so,
8144 add an entry to the per-section list of removed literals. The
8145 actual changes are deferred until the next pass. */
8147 static bfd_boolean
8148 compute_removed_literals (bfd *abfd,
8149 asection *sec,
8150 struct bfd_link_info *link_info,
8151 value_map_hash_table *values)
8153 xtensa_relax_info *relax_info;
8154 bfd_byte *contents;
8155 Elf_Internal_Rela *internal_relocs;
8156 source_reloc *src_relocs, *rel;
8157 bfd_boolean ok = TRUE;
8158 property_table_entry *prop_table = NULL;
8159 int ptblsize;
8160 int i, prev_i;
8161 bfd_boolean last_loc_is_prev = FALSE;
8162 bfd_vma last_target_offset = 0;
8163 section_cache_t target_sec_cache;
8164 bfd_size_type sec_size;
8166 init_section_cache (&target_sec_cache);
8168 /* Do nothing if it is not a relaxable literal section. */
8169 relax_info = get_xtensa_relax_info (sec);
8170 BFD_ASSERT (relax_info);
8171 if (!relax_info->is_relaxable_literal_section)
8172 return ok;
8174 internal_relocs = retrieve_internal_relocs (abfd, sec,
8175 link_info->keep_memory);
8177 sec_size = bfd_get_section_limit (abfd, sec);
8178 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
8179 if (contents == NULL && sec_size != 0)
8181 ok = FALSE;
8182 goto error_return;
8185 /* Sort the source_relocs by target offset. */
8186 src_relocs = relax_info->src_relocs;
8187 qsort (src_relocs, relax_info->src_count,
8188 sizeof (source_reloc), source_reloc_compare);
8189 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
8190 internal_reloc_compare);
8192 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
8193 XTENSA_PROP_SEC_NAME, FALSE);
8194 if (ptblsize < 0)
8196 ok = FALSE;
8197 goto error_return;
8200 prev_i = -1;
8201 for (i = 0; i < relax_info->src_count; i++)
8203 Elf_Internal_Rela *irel = NULL;
8205 rel = &src_relocs[i];
8206 if (get_l32r_opcode () != rel->opcode)
8207 continue;
8208 irel = get_irel_at_offset (sec, internal_relocs,
8209 rel->r_rel.target_offset);
8211 /* If the relocation on this is not a simple R_XTENSA_32 or
8212 R_XTENSA_PLT then do not consider it. This may happen when
8213 the difference of two symbols is used in a literal. */
8214 if (irel && (ELF32_R_TYPE (irel->r_info) != R_XTENSA_32
8215 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_PLT))
8216 continue;
8218 /* If the target_offset for this relocation is the same as the
8219 previous relocation, then we've already considered whether the
8220 literal can be coalesced. Skip to the next one.... */
8221 if (i != 0 && prev_i != -1
8222 && src_relocs[i-1].r_rel.target_offset == rel->r_rel.target_offset)
8223 continue;
8224 prev_i = i;
8226 if (last_loc_is_prev &&
8227 last_target_offset + 4 != rel->r_rel.target_offset)
8228 last_loc_is_prev = FALSE;
8230 /* Check if the relocation was from an L32R that is being removed
8231 because a CALLX was converted to a direct CALL, and check if
8232 there are no other relocations to the literal. */
8233 if (is_removable_literal (rel, i, src_relocs, relax_info->src_count,
8234 sec, prop_table, ptblsize))
8236 if (!remove_dead_literal (abfd, sec, link_info, internal_relocs,
8237 irel, rel, prop_table, ptblsize))
8239 ok = FALSE;
8240 goto error_return;
8242 last_target_offset = rel->r_rel.target_offset;
8243 continue;
8246 if (!identify_literal_placement (abfd, sec, contents, link_info,
8247 values,
8248 &last_loc_is_prev, irel,
8249 relax_info->src_count - i, rel,
8250 prop_table, ptblsize,
8251 &target_sec_cache, rel->is_abs_literal))
8253 ok = FALSE;
8254 goto error_return;
8256 last_target_offset = rel->r_rel.target_offset;
8259 #if DEBUG
8260 print_removed_literals (stderr, &relax_info->removed_list);
8261 print_action_list (stderr, &relax_info->action_list);
8262 #endif /* DEBUG */
8264 error_return:
8265 if (prop_table) free (prop_table);
8266 clear_section_cache (&target_sec_cache);
8268 release_contents (sec, contents);
8269 release_internal_relocs (sec, internal_relocs);
8270 return ok;
8274 static Elf_Internal_Rela *
8275 get_irel_at_offset (asection *sec,
8276 Elf_Internal_Rela *internal_relocs,
8277 bfd_vma offset)
8279 unsigned i;
8280 Elf_Internal_Rela *irel;
8281 unsigned r_type;
8282 Elf_Internal_Rela key;
8284 if (!internal_relocs)
8285 return NULL;
8287 key.r_offset = offset;
8288 irel = bsearch (&key, internal_relocs, sec->reloc_count,
8289 sizeof (Elf_Internal_Rela), internal_reloc_matches);
8290 if (!irel)
8291 return NULL;
8293 /* bsearch does not guarantee which will be returned if there are
8294 multiple matches. We need the first that is not an alignment. */
8295 i = irel - internal_relocs;
8296 while (i > 0)
8298 if (internal_relocs[i-1].r_offset != offset)
8299 break;
8300 i--;
8302 for ( ; i < sec->reloc_count; i++)
8304 irel = &internal_relocs[i];
8305 r_type = ELF32_R_TYPE (irel->r_info);
8306 if (irel->r_offset == offset && r_type != R_XTENSA_NONE)
8307 return irel;
8310 return NULL;
8314 bfd_boolean
8315 is_removable_literal (const source_reloc *rel,
8316 int i,
8317 const source_reloc *src_relocs,
8318 int src_count,
8319 asection *sec,
8320 property_table_entry *prop_table,
8321 int ptblsize)
8323 const source_reloc *curr_rel;
8324 property_table_entry *entry;
8326 if (!rel->is_null)
8327 return FALSE;
8329 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8330 sec->vma + rel->r_rel.target_offset);
8331 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
8332 return FALSE;
8334 for (++i; i < src_count; ++i)
8336 curr_rel = &src_relocs[i];
8337 /* If all others have the same target offset.... */
8338 if (curr_rel->r_rel.target_offset != rel->r_rel.target_offset)
8339 return TRUE;
8341 if (!curr_rel->is_null
8342 && !xtensa_is_property_section (curr_rel->source_sec)
8343 && !(curr_rel->source_sec->flags & SEC_DEBUGGING))
8344 return FALSE;
8346 return TRUE;
8350 bfd_boolean
8351 remove_dead_literal (bfd *abfd,
8352 asection *sec,
8353 struct bfd_link_info *link_info,
8354 Elf_Internal_Rela *internal_relocs,
8355 Elf_Internal_Rela *irel,
8356 source_reloc *rel,
8357 property_table_entry *prop_table,
8358 int ptblsize)
8360 property_table_entry *entry;
8361 xtensa_relax_info *relax_info;
8363 relax_info = get_xtensa_relax_info (sec);
8364 if (!relax_info)
8365 return FALSE;
8367 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8368 sec->vma + rel->r_rel.target_offset);
8370 /* Mark the unused literal so that it will be removed. */
8371 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
8373 text_action_add (&relax_info->action_list,
8374 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
8376 /* If the section is 4-byte aligned, do not add fill. */
8377 if (sec->alignment_power > 2)
8379 int fill_extra_space;
8380 bfd_vma entry_sec_offset;
8381 text_action *fa;
8382 property_table_entry *the_add_entry;
8383 int removed_diff;
8385 if (entry)
8386 entry_sec_offset = entry->address - sec->vma + entry->size;
8387 else
8388 entry_sec_offset = rel->r_rel.target_offset + 4;
8390 /* If the literal range is at the end of the section,
8391 do not add fill. */
8392 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8393 entry_sec_offset);
8394 fill_extra_space = compute_fill_extra_space (the_add_entry);
8396 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
8397 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
8398 -4, fill_extra_space);
8399 if (fa)
8400 adjust_fill_action (fa, removed_diff);
8401 else
8402 text_action_add (&relax_info->action_list,
8403 ta_fill, sec, entry_sec_offset, removed_diff);
8406 /* Zero out the relocation on this literal location. */
8407 if (irel)
8409 if (elf_hash_table (link_info)->dynamic_sections_created)
8410 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
8412 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
8413 pin_internal_relocs (sec, internal_relocs);
8416 /* Do not modify "last_loc_is_prev". */
8417 return TRUE;
8421 bfd_boolean
8422 identify_literal_placement (bfd *abfd,
8423 asection *sec,
8424 bfd_byte *contents,
8425 struct bfd_link_info *link_info,
8426 value_map_hash_table *values,
8427 bfd_boolean *last_loc_is_prev_p,
8428 Elf_Internal_Rela *irel,
8429 int remaining_src_rels,
8430 source_reloc *rel,
8431 property_table_entry *prop_table,
8432 int ptblsize,
8433 section_cache_t *target_sec_cache,
8434 bfd_boolean is_abs_literal)
8436 literal_value val;
8437 value_map *val_map;
8438 xtensa_relax_info *relax_info;
8439 bfd_boolean literal_placed = FALSE;
8440 r_reloc r_rel;
8441 unsigned long value;
8442 bfd_boolean final_static_link;
8443 bfd_size_type sec_size;
8445 relax_info = get_xtensa_relax_info (sec);
8446 if (!relax_info)
8447 return FALSE;
8449 sec_size = bfd_get_section_limit (abfd, sec);
8451 final_static_link =
8452 (!link_info->relocatable
8453 && !elf_hash_table (link_info)->dynamic_sections_created);
8455 /* The placement algorithm first checks to see if the literal is
8456 already in the value map. If so and the value map is reachable
8457 from all uses, then the literal is moved to that location. If
8458 not, then we identify the last location where a fresh literal was
8459 placed. If the literal can be safely moved there, then we do so.
8460 If not, then we assume that the literal is not to move and leave
8461 the literal where it is, marking it as the last literal
8462 location. */
8464 /* Find the literal value. */
8465 value = 0;
8466 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
8467 if (!irel)
8469 BFD_ASSERT (rel->r_rel.target_offset < sec_size);
8470 value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
8472 init_literal_value (&val, &r_rel, value, is_abs_literal);
8474 /* Check if we've seen another literal with the same value that
8475 is in the same output section. */
8476 val_map = value_map_get_cached_value (values, &val, final_static_link);
8478 if (val_map
8479 && (r_reloc_get_section (&val_map->loc)->output_section
8480 == sec->output_section)
8481 && relocations_reach (rel, remaining_src_rels, &val_map->loc)
8482 && coalesce_shared_literal (sec, rel, prop_table, ptblsize, val_map))
8484 /* No change to last_loc_is_prev. */
8485 literal_placed = TRUE;
8488 /* For relocatable links, do not try to move literals. To do it
8489 correctly might increase the number of relocations in an input
8490 section making the default relocatable linking fail. */
8491 if (!link_info->relocatable && !literal_placed
8492 && values->has_last_loc && !(*last_loc_is_prev_p))
8494 asection *target_sec = r_reloc_get_section (&values->last_loc);
8495 if (target_sec && target_sec->output_section == sec->output_section)
8497 /* Increment the virtual offset. */
8498 r_reloc try_loc = values->last_loc;
8499 try_loc.virtual_offset += 4;
8501 /* There is a last loc that was in the same output section. */
8502 if (relocations_reach (rel, remaining_src_rels, &try_loc)
8503 && move_shared_literal (sec, link_info, rel,
8504 prop_table, ptblsize,
8505 &try_loc, &val, target_sec_cache))
8507 values->last_loc.virtual_offset += 4;
8508 literal_placed = TRUE;
8509 if (!val_map)
8510 val_map = add_value_map (values, &val, &try_loc,
8511 final_static_link);
8512 else
8513 val_map->loc = try_loc;
8518 if (!literal_placed)
8520 /* Nothing worked, leave the literal alone but update the last loc. */
8521 values->has_last_loc = TRUE;
8522 values->last_loc = rel->r_rel;
8523 if (!val_map)
8524 val_map = add_value_map (values, &val, &rel->r_rel, final_static_link);
8525 else
8526 val_map->loc = rel->r_rel;
8527 *last_loc_is_prev_p = TRUE;
8530 return TRUE;
8534 /* Check if the original relocations (presumably on L32R instructions)
8535 identified by reloc[0..N] can be changed to reference the literal
8536 identified by r_rel. If r_rel is out of range for any of the
8537 original relocations, then we don't want to coalesce the original
8538 literal with the one at r_rel. We only check reloc[0..N], where the
8539 offsets are all the same as for reloc[0] (i.e., they're all
8540 referencing the same literal) and where N is also bounded by the
8541 number of remaining entries in the "reloc" array. The "reloc" array
8542 is sorted by target offset so we know all the entries for the same
8543 literal will be contiguous. */
8545 static bfd_boolean
8546 relocations_reach (source_reloc *reloc,
8547 int remaining_relocs,
8548 const r_reloc *r_rel)
8550 bfd_vma from_offset, source_address, dest_address;
8551 asection *sec;
8552 int i;
8554 if (!r_reloc_is_defined (r_rel))
8555 return FALSE;
8557 sec = r_reloc_get_section (r_rel);
8558 from_offset = reloc[0].r_rel.target_offset;
8560 for (i = 0; i < remaining_relocs; i++)
8562 if (reloc[i].r_rel.target_offset != from_offset)
8563 break;
8565 /* Ignore relocations that have been removed. */
8566 if (reloc[i].is_null)
8567 continue;
8569 /* The original and new output section for these must be the same
8570 in order to coalesce. */
8571 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
8572 != sec->output_section)
8573 return FALSE;
8575 /* Absolute literals in the same output section can always be
8576 combined. */
8577 if (reloc[i].is_abs_literal)
8578 continue;
8580 /* A literal with no PC-relative relocations can be moved anywhere. */
8581 if (reloc[i].opnd != -1)
8583 /* Otherwise, check to see that it fits. */
8584 source_address = (reloc[i].source_sec->output_section->vma
8585 + reloc[i].source_sec->output_offset
8586 + reloc[i].r_rel.rela.r_offset);
8587 dest_address = (sec->output_section->vma
8588 + sec->output_offset
8589 + r_rel->target_offset);
8591 if (!pcrel_reloc_fits (reloc[i].opcode, reloc[i].opnd,
8592 source_address, dest_address))
8593 return FALSE;
8597 return TRUE;
8601 /* Move a literal to another literal location because it is
8602 the same as the other literal value. */
8604 static bfd_boolean
8605 coalesce_shared_literal (asection *sec,
8606 source_reloc *rel,
8607 property_table_entry *prop_table,
8608 int ptblsize,
8609 value_map *val_map)
8611 property_table_entry *entry;
8612 text_action *fa;
8613 property_table_entry *the_add_entry;
8614 int removed_diff;
8615 xtensa_relax_info *relax_info;
8617 relax_info = get_xtensa_relax_info (sec);
8618 if (!relax_info)
8619 return FALSE;
8621 entry = elf_xtensa_find_property_entry
8622 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
8623 if (entry && (entry->flags & XTENSA_PROP_NO_TRANSFORM))
8624 return TRUE;
8626 /* Mark that the literal will be coalesced. */
8627 add_removed_literal (&relax_info->removed_list, &rel->r_rel, &val_map->loc);
8629 text_action_add (&relax_info->action_list,
8630 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
8632 /* If the section is 4-byte aligned, do not add fill. */
8633 if (sec->alignment_power > 2)
8635 int fill_extra_space;
8636 bfd_vma entry_sec_offset;
8638 if (entry)
8639 entry_sec_offset = entry->address - sec->vma + entry->size;
8640 else
8641 entry_sec_offset = rel->r_rel.target_offset + 4;
8643 /* If the literal range is at the end of the section,
8644 do not add fill. */
8645 fill_extra_space = 0;
8646 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8647 entry_sec_offset);
8648 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
8649 fill_extra_space = the_add_entry->size;
8651 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
8652 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
8653 -4, fill_extra_space);
8654 if (fa)
8655 adjust_fill_action (fa, removed_diff);
8656 else
8657 text_action_add (&relax_info->action_list,
8658 ta_fill, sec, entry_sec_offset, removed_diff);
8661 return TRUE;
8665 /* Move a literal to another location. This may actually increase the
8666 total amount of space used because of alignments so we need to do
8667 this carefully. Also, it may make a branch go out of range. */
8669 static bfd_boolean
8670 move_shared_literal (asection *sec,
8671 struct bfd_link_info *link_info,
8672 source_reloc *rel,
8673 property_table_entry *prop_table,
8674 int ptblsize,
8675 const r_reloc *target_loc,
8676 const literal_value *lit_value,
8677 section_cache_t *target_sec_cache)
8679 property_table_entry *the_add_entry, *src_entry, *target_entry = NULL;
8680 text_action *fa, *target_fa;
8681 int removed_diff;
8682 xtensa_relax_info *relax_info, *target_relax_info;
8683 asection *target_sec;
8684 ebb_t *ebb;
8685 ebb_constraint ebb_table;
8686 bfd_boolean relocs_fit;
8688 /* If this routine always returns FALSE, the literals that cannot be
8689 coalesced will not be moved. */
8690 if (elf32xtensa_no_literal_movement)
8691 return FALSE;
8693 relax_info = get_xtensa_relax_info (sec);
8694 if (!relax_info)
8695 return FALSE;
8697 target_sec = r_reloc_get_section (target_loc);
8698 target_relax_info = get_xtensa_relax_info (target_sec);
8700 /* Literals to undefined sections may not be moved because they
8701 must report an error. */
8702 if (bfd_is_und_section (target_sec))
8703 return FALSE;
8705 src_entry = elf_xtensa_find_property_entry
8706 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
8708 if (!section_cache_section (target_sec_cache, target_sec, link_info))
8709 return FALSE;
8711 target_entry = elf_xtensa_find_property_entry
8712 (target_sec_cache->ptbl, target_sec_cache->pte_count,
8713 target_sec->vma + target_loc->target_offset);
8715 if (!target_entry)
8716 return FALSE;
8718 /* Make sure that we have not broken any branches. */
8719 relocs_fit = FALSE;
8721 init_ebb_constraint (&ebb_table);
8722 ebb = &ebb_table.ebb;
8723 init_ebb (ebb, target_sec_cache->sec, target_sec_cache->contents,
8724 target_sec_cache->content_length,
8725 target_sec_cache->ptbl, target_sec_cache->pte_count,
8726 target_sec_cache->relocs, target_sec_cache->reloc_count);
8728 /* Propose to add 4 bytes + worst-case alignment size increase to
8729 destination. */
8730 ebb_propose_action (&ebb_table, EBB_NO_ALIGN, 0,
8731 ta_fill, target_loc->target_offset,
8732 -4 - (1 << target_sec->alignment_power), TRUE);
8734 /* Check all of the PC-relative relocations to make sure they still fit. */
8735 relocs_fit = check_section_ebb_pcrels_fit (target_sec->owner, target_sec,
8736 target_sec_cache->contents,
8737 target_sec_cache->relocs,
8738 &ebb_table, NULL);
8740 if (!relocs_fit)
8741 return FALSE;
8743 text_action_add_literal (&target_relax_info->action_list,
8744 ta_add_literal, target_loc, lit_value, -4);
8746 if (target_sec->alignment_power > 2 && target_entry != src_entry)
8748 /* May need to add or remove some fill to maintain alignment. */
8749 int fill_extra_space;
8750 bfd_vma entry_sec_offset;
8752 entry_sec_offset =
8753 target_entry->address - target_sec->vma + target_entry->size;
8755 /* If the literal range is at the end of the section,
8756 do not add fill. */
8757 fill_extra_space = 0;
8758 the_add_entry =
8759 elf_xtensa_find_property_entry (target_sec_cache->ptbl,
8760 target_sec_cache->pte_count,
8761 entry_sec_offset);
8762 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
8763 fill_extra_space = the_add_entry->size;
8765 target_fa = find_fill_action (&target_relax_info->action_list,
8766 target_sec, entry_sec_offset);
8767 removed_diff = compute_removed_action_diff (target_fa, target_sec,
8768 entry_sec_offset, 4,
8769 fill_extra_space);
8770 if (target_fa)
8771 adjust_fill_action (target_fa, removed_diff);
8772 else
8773 text_action_add (&target_relax_info->action_list,
8774 ta_fill, target_sec, entry_sec_offset, removed_diff);
8777 /* Mark that the literal will be moved to the new location. */
8778 add_removed_literal (&relax_info->removed_list, &rel->r_rel, target_loc);
8780 /* Remove the literal. */
8781 text_action_add (&relax_info->action_list,
8782 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
8784 /* If the section is 4-byte aligned, do not add fill. */
8785 if (sec->alignment_power > 2 && target_entry != src_entry)
8787 int fill_extra_space;
8788 bfd_vma entry_sec_offset;
8790 if (src_entry)
8791 entry_sec_offset = src_entry->address - sec->vma + src_entry->size;
8792 else
8793 entry_sec_offset = rel->r_rel.target_offset+4;
8795 /* If the literal range is at the end of the section,
8796 do not add fill. */
8797 fill_extra_space = 0;
8798 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8799 entry_sec_offset);
8800 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
8801 fill_extra_space = the_add_entry->size;
8803 fa = find_fill_action (&relax_info->action_list, sec, entry_sec_offset);
8804 removed_diff = compute_removed_action_diff (fa, sec, entry_sec_offset,
8805 -4, fill_extra_space);
8806 if (fa)
8807 adjust_fill_action (fa, removed_diff);
8808 else
8809 text_action_add (&relax_info->action_list,
8810 ta_fill, sec, entry_sec_offset, removed_diff);
8813 return TRUE;
8817 /* Second relaxation pass. */
8819 /* Modify all of the relocations to point to the right spot, and if this
8820 is a relaxable section, delete the unwanted literals and fix the
8821 section size. */
8823 bfd_boolean
8824 relax_section (bfd *abfd, asection *sec, struct bfd_link_info *link_info)
8826 Elf_Internal_Rela *internal_relocs;
8827 xtensa_relax_info *relax_info;
8828 bfd_byte *contents;
8829 bfd_boolean ok = TRUE;
8830 unsigned i;
8831 bfd_boolean rv = FALSE;
8832 bfd_boolean virtual_action;
8833 bfd_size_type sec_size;
8835 sec_size = bfd_get_section_limit (abfd, sec);
8836 relax_info = get_xtensa_relax_info (sec);
8837 BFD_ASSERT (relax_info);
8839 /* First translate any of the fixes that have been added already. */
8840 translate_section_fixes (sec);
8842 /* Handle property sections (e.g., literal tables) specially. */
8843 if (xtensa_is_property_section (sec))
8845 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
8846 return relax_property_section (abfd, sec, link_info);
8849 internal_relocs = retrieve_internal_relocs (abfd, sec,
8850 link_info->keep_memory);
8851 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
8852 if (contents == NULL && sec_size != 0)
8854 ok = FALSE;
8855 goto error_return;
8858 if (internal_relocs)
8860 for (i = 0; i < sec->reloc_count; i++)
8862 Elf_Internal_Rela *irel;
8863 xtensa_relax_info *target_relax_info;
8864 bfd_vma source_offset, old_source_offset;
8865 r_reloc r_rel;
8866 unsigned r_type;
8867 asection *target_sec;
8869 /* Locally change the source address.
8870 Translate the target to the new target address.
8871 If it points to this section and has been removed,
8872 NULLify it.
8873 Write it back. */
8875 irel = &internal_relocs[i];
8876 source_offset = irel->r_offset;
8877 old_source_offset = source_offset;
8879 r_type = ELF32_R_TYPE (irel->r_info);
8880 r_reloc_init (&r_rel, abfd, irel, contents,
8881 bfd_get_section_limit (abfd, sec));
8883 /* If this section could have changed then we may need to
8884 change the relocation's offset. */
8886 if (relax_info->is_relaxable_literal_section
8887 || relax_info->is_relaxable_asm_section)
8889 pin_internal_relocs (sec, internal_relocs);
8891 if (r_type != R_XTENSA_NONE
8892 && find_removed_literal (&relax_info->removed_list,
8893 irel->r_offset))
8895 /* Remove this relocation. */
8896 if (elf_hash_table (link_info)->dynamic_sections_created)
8897 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
8898 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
8899 irel->r_offset = offset_with_removed_text
8900 (&relax_info->action_list, irel->r_offset);
8901 continue;
8904 if (r_type == R_XTENSA_ASM_SIMPLIFY)
8906 text_action *action =
8907 find_insn_action (&relax_info->action_list,
8908 irel->r_offset);
8909 if (action && (action->action == ta_convert_longcall
8910 || action->action == ta_remove_longcall))
8912 bfd_reloc_status_type retval;
8913 char *error_message = NULL;
8915 retval = contract_asm_expansion (contents, sec_size,
8916 irel, &error_message);
8917 if (retval != bfd_reloc_ok)
8919 (*link_info->callbacks->reloc_dangerous)
8920 (link_info, error_message, abfd, sec,
8921 irel->r_offset);
8922 goto error_return;
8924 /* Update the action so that the code that moves
8925 the contents will do the right thing. */
8926 if (action->action == ta_remove_longcall)
8927 action->action = ta_remove_insn;
8928 else
8929 action->action = ta_none;
8930 /* Refresh the info in the r_rel. */
8931 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
8932 r_type = ELF32_R_TYPE (irel->r_info);
8936 source_offset = offset_with_removed_text
8937 (&relax_info->action_list, irel->r_offset);
8938 irel->r_offset = source_offset;
8941 /* If the target section could have changed then
8942 we may need to change the relocation's target offset. */
8944 target_sec = r_reloc_get_section (&r_rel);
8946 /* For a reference to a discarded section from a DWARF section,
8947 i.e., where action_discarded is PRETEND, the symbol will
8948 eventually be modified to refer to the kept section (at least if
8949 the kept and discarded sections are the same size). Anticipate
8950 that here and adjust things accordingly. */
8951 if (! elf_xtensa_ignore_discarded_relocs (sec)
8952 && elf_xtensa_action_discarded (sec) == PRETEND
8953 && sec->sec_info_type != ELF_INFO_TYPE_STABS
8954 && target_sec != NULL
8955 && elf_discarded_section (target_sec))
8957 /* It would be natural to call _bfd_elf_check_kept_section
8958 here, but it's not exported from elflink.c. It's also a
8959 fairly expensive check. Adjusting the relocations to the
8960 discarded section is fairly harmless; it will only adjust
8961 some addends and difference values. If it turns out that
8962 _bfd_elf_check_kept_section fails later, it won't matter,
8963 so just compare the section names to find the right group
8964 member. */
8965 asection *kept = target_sec->kept_section;
8966 if (kept != NULL)
8968 if ((kept->flags & SEC_GROUP) != 0)
8970 asection *first = elf_next_in_group (kept);
8971 asection *s = first;
8973 kept = NULL;
8974 while (s != NULL)
8976 if (strcmp (s->name, target_sec->name) == 0)
8978 kept = s;
8979 break;
8981 s = elf_next_in_group (s);
8982 if (s == first)
8983 break;
8987 if (kept != NULL
8988 && ((target_sec->rawsize != 0
8989 ? target_sec->rawsize : target_sec->size)
8990 == (kept->rawsize != 0 ? kept->rawsize : kept->size)))
8991 target_sec = kept;
8994 target_relax_info = get_xtensa_relax_info (target_sec);
8995 if (target_relax_info
8996 && (target_relax_info->is_relaxable_literal_section
8997 || target_relax_info->is_relaxable_asm_section))
8999 r_reloc new_reloc;
9000 target_sec = translate_reloc (&r_rel, &new_reloc, target_sec);
9002 if (r_type == R_XTENSA_DIFF8
9003 || r_type == R_XTENSA_DIFF16
9004 || r_type == R_XTENSA_DIFF32)
9006 bfd_vma diff_value = 0, new_end_offset, diff_mask = 0;
9008 if (bfd_get_section_limit (abfd, sec) < old_source_offset)
9010 (*link_info->callbacks->reloc_dangerous)
9011 (link_info, _("invalid relocation address"),
9012 abfd, sec, old_source_offset);
9013 goto error_return;
9016 switch (r_type)
9018 case R_XTENSA_DIFF8:
9019 diff_value =
9020 bfd_get_8 (abfd, &contents[old_source_offset]);
9021 break;
9022 case R_XTENSA_DIFF16:
9023 diff_value =
9024 bfd_get_16 (abfd, &contents[old_source_offset]);
9025 break;
9026 case R_XTENSA_DIFF32:
9027 diff_value =
9028 bfd_get_32 (abfd, &contents[old_source_offset]);
9029 break;
9032 new_end_offset = offset_with_removed_text
9033 (&target_relax_info->action_list,
9034 r_rel.target_offset + diff_value);
9035 diff_value = new_end_offset - new_reloc.target_offset;
9037 switch (r_type)
9039 case R_XTENSA_DIFF8:
9040 diff_mask = 0xff;
9041 bfd_put_8 (abfd, diff_value,
9042 &contents[old_source_offset]);
9043 break;
9044 case R_XTENSA_DIFF16:
9045 diff_mask = 0xffff;
9046 bfd_put_16 (abfd, diff_value,
9047 &contents[old_source_offset]);
9048 break;
9049 case R_XTENSA_DIFF32:
9050 diff_mask = 0xffffffff;
9051 bfd_put_32 (abfd, diff_value,
9052 &contents[old_source_offset]);
9053 break;
9056 /* Check for overflow. */
9057 if ((diff_value & ~diff_mask) != 0)
9059 (*link_info->callbacks->reloc_dangerous)
9060 (link_info, _("overflow after relaxation"),
9061 abfd, sec, old_source_offset);
9062 goto error_return;
9065 pin_contents (sec, contents);
9068 /* If the relocation still references a section in the same
9069 input file, modify the relocation directly instead of
9070 adding a "fix" record. */
9071 if (target_sec->owner == abfd)
9073 unsigned r_symndx = ELF32_R_SYM (new_reloc.rela.r_info);
9074 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
9075 irel->r_addend = new_reloc.rela.r_addend;
9076 pin_internal_relocs (sec, internal_relocs);
9078 else
9080 bfd_vma addend_displacement;
9081 reloc_bfd_fix *fix;
9083 addend_displacement =
9084 new_reloc.target_offset + new_reloc.virtual_offset;
9085 fix = reloc_bfd_fix_init (sec, source_offset, r_type,
9086 target_sec,
9087 addend_displacement, TRUE);
9088 add_fix (sec, fix);
9094 if ((relax_info->is_relaxable_literal_section
9095 || relax_info->is_relaxable_asm_section)
9096 && relax_info->action_list.head)
9098 /* Walk through the planned actions and build up a table
9099 of move, copy and fill records. Use the move, copy and
9100 fill records to perform the actions once. */
9102 int removed = 0;
9103 bfd_size_type final_size, copy_size, orig_insn_size;
9104 bfd_byte *scratch = NULL;
9105 bfd_byte *dup_contents = NULL;
9106 bfd_size_type orig_size = sec->size;
9107 bfd_vma orig_dot = 0;
9108 bfd_vma orig_dot_copied = 0; /* Byte copied already from
9109 orig dot in physical memory. */
9110 bfd_vma orig_dot_vo = 0; /* Virtual offset from orig_dot. */
9111 bfd_vma dup_dot = 0;
9113 text_action *action = relax_info->action_list.head;
9115 final_size = sec->size;
9116 for (action = relax_info->action_list.head; action;
9117 action = action->next)
9119 final_size -= action->removed_bytes;
9122 scratch = (bfd_byte *) bfd_zmalloc (final_size);
9123 dup_contents = (bfd_byte *) bfd_zmalloc (final_size);
9125 /* The dot is the current fill location. */
9126 #if DEBUG
9127 print_action_list (stderr, &relax_info->action_list);
9128 #endif
9130 for (action = relax_info->action_list.head; action;
9131 action = action->next)
9133 virtual_action = FALSE;
9134 if (action->offset > orig_dot)
9136 orig_dot += orig_dot_copied;
9137 orig_dot_copied = 0;
9138 orig_dot_vo = 0;
9139 /* Out of the virtual world. */
9142 if (action->offset > orig_dot)
9144 copy_size = action->offset - orig_dot;
9145 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9146 orig_dot += copy_size;
9147 dup_dot += copy_size;
9148 BFD_ASSERT (action->offset == orig_dot);
9150 else if (action->offset < orig_dot)
9152 if (action->action == ta_fill
9153 && action->offset - action->removed_bytes == orig_dot)
9155 /* This is OK because the fill only effects the dup_dot. */
9157 else if (action->action == ta_add_literal)
9159 /* TBD. Might need to handle this. */
9162 if (action->offset == orig_dot)
9164 if (action->virtual_offset > orig_dot_vo)
9166 if (orig_dot_vo == 0)
9168 /* Need to copy virtual_offset bytes. Probably four. */
9169 copy_size = action->virtual_offset - orig_dot_vo;
9170 memmove (&dup_contents[dup_dot],
9171 &contents[orig_dot], copy_size);
9172 orig_dot_copied = copy_size;
9173 dup_dot += copy_size;
9175 virtual_action = TRUE;
9177 else
9178 BFD_ASSERT (action->virtual_offset <= orig_dot_vo);
9180 switch (action->action)
9182 case ta_remove_literal:
9183 case ta_remove_insn:
9184 BFD_ASSERT (action->removed_bytes >= 0);
9185 orig_dot += action->removed_bytes;
9186 break;
9188 case ta_narrow_insn:
9189 orig_insn_size = 3;
9190 copy_size = 2;
9191 memmove (scratch, &contents[orig_dot], orig_insn_size);
9192 BFD_ASSERT (action->removed_bytes == 1);
9193 rv = narrow_instruction (scratch, final_size, 0);
9194 BFD_ASSERT (rv);
9195 memmove (&dup_contents[dup_dot], scratch, copy_size);
9196 orig_dot += orig_insn_size;
9197 dup_dot += copy_size;
9198 break;
9200 case ta_fill:
9201 if (action->removed_bytes >= 0)
9202 orig_dot += action->removed_bytes;
9203 else
9205 /* Already zeroed in dup_contents. Just bump the
9206 counters. */
9207 dup_dot += (-action->removed_bytes);
9209 break;
9211 case ta_none:
9212 BFD_ASSERT (action->removed_bytes == 0);
9213 break;
9215 case ta_convert_longcall:
9216 case ta_remove_longcall:
9217 /* These will be removed or converted before we get here. */
9218 BFD_ASSERT (0);
9219 break;
9221 case ta_widen_insn:
9222 orig_insn_size = 2;
9223 copy_size = 3;
9224 memmove (scratch, &contents[orig_dot], orig_insn_size);
9225 BFD_ASSERT (action->removed_bytes == -1);
9226 rv = widen_instruction (scratch, final_size, 0);
9227 BFD_ASSERT (rv);
9228 memmove (&dup_contents[dup_dot], scratch, copy_size);
9229 orig_dot += orig_insn_size;
9230 dup_dot += copy_size;
9231 break;
9233 case ta_add_literal:
9234 orig_insn_size = 0;
9235 copy_size = 4;
9236 BFD_ASSERT (action->removed_bytes == -4);
9237 /* TBD -- place the literal value here and insert
9238 into the table. */
9239 memset (&dup_contents[dup_dot], 0, 4);
9240 pin_internal_relocs (sec, internal_relocs);
9241 pin_contents (sec, contents);
9243 if (!move_literal (abfd, link_info, sec, dup_dot, dup_contents,
9244 relax_info, &internal_relocs, &action->value))
9245 goto error_return;
9247 if (virtual_action)
9248 orig_dot_vo += copy_size;
9250 orig_dot += orig_insn_size;
9251 dup_dot += copy_size;
9252 break;
9254 default:
9255 /* Not implemented yet. */
9256 BFD_ASSERT (0);
9257 break;
9260 removed += action->removed_bytes;
9261 BFD_ASSERT (dup_dot <= final_size);
9262 BFD_ASSERT (orig_dot <= orig_size);
9265 orig_dot += orig_dot_copied;
9266 orig_dot_copied = 0;
9268 if (orig_dot != orig_size)
9270 copy_size = orig_size - orig_dot;
9271 BFD_ASSERT (orig_size > orig_dot);
9272 BFD_ASSERT (dup_dot + copy_size == final_size);
9273 memmove (&dup_contents[dup_dot], &contents[orig_dot], copy_size);
9274 orig_dot += copy_size;
9275 dup_dot += copy_size;
9277 BFD_ASSERT (orig_size == orig_dot);
9278 BFD_ASSERT (final_size == dup_dot);
9280 /* Move the dup_contents back. */
9281 if (final_size > orig_size)
9283 /* Contents need to be reallocated. Swap the dup_contents into
9284 contents. */
9285 sec->contents = dup_contents;
9286 free (contents);
9287 contents = dup_contents;
9288 pin_contents (sec, contents);
9290 else
9292 BFD_ASSERT (final_size <= orig_size);
9293 memset (contents, 0, orig_size);
9294 memcpy (contents, dup_contents, final_size);
9295 free (dup_contents);
9297 free (scratch);
9298 pin_contents (sec, contents);
9300 if (sec->rawsize == 0)
9301 sec->rawsize = sec->size;
9302 sec->size = final_size;
9305 error_return:
9306 release_internal_relocs (sec, internal_relocs);
9307 release_contents (sec, contents);
9308 return ok;
9312 static bfd_boolean
9313 translate_section_fixes (asection *sec)
9315 xtensa_relax_info *relax_info;
9316 reloc_bfd_fix *r;
9318 relax_info = get_xtensa_relax_info (sec);
9319 if (!relax_info)
9320 return TRUE;
9322 for (r = relax_info->fix_list; r != NULL; r = r->next)
9323 if (!translate_reloc_bfd_fix (r))
9324 return FALSE;
9326 return TRUE;
9330 /* Translate a fix given the mapping in the relax info for the target
9331 section. If it has already been translated, no work is required. */
9333 static bfd_boolean
9334 translate_reloc_bfd_fix (reloc_bfd_fix *fix)
9336 reloc_bfd_fix new_fix;
9337 asection *sec;
9338 xtensa_relax_info *relax_info;
9339 removed_literal *removed;
9340 bfd_vma new_offset, target_offset;
9342 if (fix->translated)
9343 return TRUE;
9345 sec = fix->target_sec;
9346 target_offset = fix->target_offset;
9348 relax_info = get_xtensa_relax_info (sec);
9349 if (!relax_info)
9351 fix->translated = TRUE;
9352 return TRUE;
9355 new_fix = *fix;
9357 /* The fix does not need to be translated if the section cannot change. */
9358 if (!relax_info->is_relaxable_literal_section
9359 && !relax_info->is_relaxable_asm_section)
9361 fix->translated = TRUE;
9362 return TRUE;
9365 /* If the literal has been moved and this relocation was on an
9366 opcode, then the relocation should move to the new literal
9367 location. Otherwise, the relocation should move within the
9368 section. */
9370 removed = FALSE;
9371 if (is_operand_relocation (fix->src_type))
9373 /* Check if the original relocation is against a literal being
9374 removed. */
9375 removed = find_removed_literal (&relax_info->removed_list,
9376 target_offset);
9379 if (removed)
9381 asection *new_sec;
9383 /* The fact that there is still a relocation to this literal indicates
9384 that the literal is being coalesced, not simply removed. */
9385 BFD_ASSERT (removed->to.abfd != NULL);
9387 /* This was moved to some other address (possibly another section). */
9388 new_sec = r_reloc_get_section (&removed->to);
9389 if (new_sec != sec)
9391 sec = new_sec;
9392 relax_info = get_xtensa_relax_info (sec);
9393 if (!relax_info ||
9394 (!relax_info->is_relaxable_literal_section
9395 && !relax_info->is_relaxable_asm_section))
9397 target_offset = removed->to.target_offset;
9398 new_fix.target_sec = new_sec;
9399 new_fix.target_offset = target_offset;
9400 new_fix.translated = TRUE;
9401 *fix = new_fix;
9402 return TRUE;
9405 target_offset = removed->to.target_offset;
9406 new_fix.target_sec = new_sec;
9409 /* The target address may have been moved within its section. */
9410 new_offset = offset_with_removed_text (&relax_info->action_list,
9411 target_offset);
9413 new_fix.target_offset = new_offset;
9414 new_fix.target_offset = new_offset;
9415 new_fix.translated = TRUE;
9416 *fix = new_fix;
9417 return TRUE;
9421 /* Fix up a relocation to take account of removed literals. */
9423 static asection *
9424 translate_reloc (const r_reloc *orig_rel, r_reloc *new_rel, asection *sec)
9426 xtensa_relax_info *relax_info;
9427 removed_literal *removed;
9428 bfd_vma target_offset, base_offset;
9429 text_action *act;
9431 *new_rel = *orig_rel;
9433 if (!r_reloc_is_defined (orig_rel))
9434 return sec ;
9436 relax_info = get_xtensa_relax_info (sec);
9437 BFD_ASSERT (relax_info && (relax_info->is_relaxable_literal_section
9438 || relax_info->is_relaxable_asm_section));
9440 target_offset = orig_rel->target_offset;
9442 removed = FALSE;
9443 if (is_operand_relocation (ELF32_R_TYPE (orig_rel->rela.r_info)))
9445 /* Check if the original relocation is against a literal being
9446 removed. */
9447 removed = find_removed_literal (&relax_info->removed_list,
9448 target_offset);
9450 if (removed && removed->to.abfd)
9452 asection *new_sec;
9454 /* The fact that there is still a relocation to this literal indicates
9455 that the literal is being coalesced, not simply removed. */
9456 BFD_ASSERT (removed->to.abfd != NULL);
9458 /* This was moved to some other address
9459 (possibly in another section). */
9460 *new_rel = removed->to;
9461 new_sec = r_reloc_get_section (new_rel);
9462 if (new_sec != sec)
9464 sec = new_sec;
9465 relax_info = get_xtensa_relax_info (sec);
9466 if (!relax_info
9467 || (!relax_info->is_relaxable_literal_section
9468 && !relax_info->is_relaxable_asm_section))
9469 return sec;
9471 target_offset = new_rel->target_offset;
9474 /* Find the base offset of the reloc symbol, excluding any addend from the
9475 reloc or from the section contents (for a partial_inplace reloc). Then
9476 find the adjusted values of the offsets due to relaxation. The base
9477 offset is needed to determine the change to the reloc's addend; the reloc
9478 addend should not be adjusted due to relaxations located before the base
9479 offset. */
9481 base_offset = r_reloc_get_target_offset (new_rel) - new_rel->rela.r_addend;
9482 act = relax_info->action_list.head;
9483 if (base_offset <= target_offset)
9485 int base_removed = removed_by_actions (&act, base_offset, FALSE);
9486 int addend_removed = removed_by_actions (&act, target_offset, FALSE);
9487 new_rel->target_offset = target_offset - base_removed - addend_removed;
9488 new_rel->rela.r_addend -= addend_removed;
9490 else
9492 /* Handle a negative addend. The base offset comes first. */
9493 int tgt_removed = removed_by_actions (&act, target_offset, FALSE);
9494 int addend_removed = removed_by_actions (&act, base_offset, FALSE);
9495 new_rel->target_offset = target_offset - tgt_removed;
9496 new_rel->rela.r_addend += addend_removed;
9499 return sec;
9503 /* For dynamic links, there may be a dynamic relocation for each
9504 literal. The number of dynamic relocations must be computed in
9505 size_dynamic_sections, which occurs before relaxation. When a
9506 literal is removed, this function checks if there is a corresponding
9507 dynamic relocation and shrinks the size of the appropriate dynamic
9508 relocation section accordingly. At this point, the contents of the
9509 dynamic relocation sections have not yet been filled in, so there's
9510 nothing else that needs to be done. */
9512 static void
9513 shrink_dynamic_reloc_sections (struct bfd_link_info *info,
9514 bfd *abfd,
9515 asection *input_section,
9516 Elf_Internal_Rela *rel)
9518 struct elf_xtensa_link_hash_table *htab;
9519 Elf_Internal_Shdr *symtab_hdr;
9520 struct elf_link_hash_entry **sym_hashes;
9521 unsigned long r_symndx;
9522 int r_type;
9523 struct elf_link_hash_entry *h;
9524 bfd_boolean dynamic_symbol;
9526 htab = elf_xtensa_hash_table (info);
9527 if (htab == NULL)
9528 return;
9530 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9531 sym_hashes = elf_sym_hashes (abfd);
9533 r_type = ELF32_R_TYPE (rel->r_info);
9534 r_symndx = ELF32_R_SYM (rel->r_info);
9536 if (r_symndx < symtab_hdr->sh_info)
9537 h = NULL;
9538 else
9539 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
9541 dynamic_symbol = elf_xtensa_dynamic_symbol_p (h, info);
9543 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
9544 && (input_section->flags & SEC_ALLOC) != 0
9545 && (dynamic_symbol || info->shared))
9547 asection *srel;
9548 bfd_boolean is_plt = FALSE;
9550 if (dynamic_symbol && r_type == R_XTENSA_PLT)
9552 srel = htab->srelplt;
9553 is_plt = TRUE;
9555 else
9556 srel = htab->srelgot;
9558 /* Reduce size of the .rela.* section by one reloc. */
9559 BFD_ASSERT (srel != NULL);
9560 BFD_ASSERT (srel->size >= sizeof (Elf32_External_Rela));
9561 srel->size -= sizeof (Elf32_External_Rela);
9563 if (is_plt)
9565 asection *splt, *sgotplt, *srelgot;
9566 int reloc_index, chunk;
9568 /* Find the PLT reloc index of the entry being removed. This
9569 is computed from the size of ".rela.plt". It is needed to
9570 figure out which PLT chunk to resize. Usually "last index
9571 = size - 1" since the index starts at zero, but in this
9572 context, the size has just been decremented so there's no
9573 need to subtract one. */
9574 reloc_index = srel->size / sizeof (Elf32_External_Rela);
9576 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
9577 splt = elf_xtensa_get_plt_section (info, chunk);
9578 sgotplt = elf_xtensa_get_gotplt_section (info, chunk);
9579 BFD_ASSERT (splt != NULL && sgotplt != NULL);
9581 /* Check if an entire PLT chunk has just been eliminated. */
9582 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
9584 /* The two magic GOT entries for that chunk can go away. */
9585 srelgot = htab->srelgot;
9586 BFD_ASSERT (srelgot != NULL);
9587 srelgot->reloc_count -= 2;
9588 srelgot->size -= 2 * sizeof (Elf32_External_Rela);
9589 sgotplt->size -= 8;
9591 /* There should be only one entry left (and it will be
9592 removed below). */
9593 BFD_ASSERT (sgotplt->size == 4);
9594 BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
9597 BFD_ASSERT (sgotplt->size >= 4);
9598 BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
9600 sgotplt->size -= 4;
9601 splt->size -= PLT_ENTRY_SIZE;
9607 /* Take an r_rel and move it to another section. This usually
9608 requires extending the interal_relocation array and pinning it. If
9609 the original r_rel is from the same BFD, we can complete this here.
9610 Otherwise, we add a fix record to let the final link fix the
9611 appropriate address. Contents and internal relocations for the
9612 section must be pinned after calling this routine. */
9614 static bfd_boolean
9615 move_literal (bfd *abfd,
9616 struct bfd_link_info *link_info,
9617 asection *sec,
9618 bfd_vma offset,
9619 bfd_byte *contents,
9620 xtensa_relax_info *relax_info,
9621 Elf_Internal_Rela **internal_relocs_p,
9622 const literal_value *lit)
9624 Elf_Internal_Rela *new_relocs = NULL;
9625 size_t new_relocs_count = 0;
9626 Elf_Internal_Rela this_rela;
9627 const r_reloc *r_rel;
9629 r_rel = &lit->r_rel;
9630 BFD_ASSERT (elf_section_data (sec)->relocs == *internal_relocs_p);
9632 if (r_reloc_is_const (r_rel))
9633 bfd_put_32 (abfd, lit->value, contents + offset);
9634 else
9636 int r_type;
9637 unsigned i;
9638 asection *target_sec;
9639 reloc_bfd_fix *fix;
9640 unsigned insert_at;
9642 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
9643 target_sec = r_reloc_get_section (r_rel);
9645 /* This is the difficult case. We have to create a fix up. */
9646 this_rela.r_offset = offset;
9647 this_rela.r_info = ELF32_R_INFO (0, r_type);
9648 this_rela.r_addend =
9649 r_rel->target_offset - r_reloc_get_target_offset (r_rel);
9650 bfd_put_32 (abfd, lit->value, contents + offset);
9652 /* Currently, we cannot move relocations during a relocatable link. */
9653 BFD_ASSERT (!link_info->relocatable);
9654 fix = reloc_bfd_fix_init (sec, offset, r_type,
9655 r_reloc_get_section (r_rel),
9656 r_rel->target_offset + r_rel->virtual_offset,
9657 FALSE);
9658 /* We also need to mark that relocations are needed here. */
9659 sec->flags |= SEC_RELOC;
9661 translate_reloc_bfd_fix (fix);
9662 /* This fix has not yet been translated. */
9663 add_fix (sec, fix);
9665 /* Add the relocation. If we have already allocated our own
9666 space for the relocations and we have room for more, then use
9667 it. Otherwise, allocate new space and move the literals. */
9668 insert_at = sec->reloc_count;
9669 for (i = 0; i < sec->reloc_count; ++i)
9671 if (this_rela.r_offset < (*internal_relocs_p)[i].r_offset)
9673 insert_at = i;
9674 break;
9678 if (*internal_relocs_p != relax_info->allocated_relocs
9679 || sec->reloc_count + 1 > relax_info->allocated_relocs_count)
9681 BFD_ASSERT (relax_info->allocated_relocs == NULL
9682 || sec->reloc_count == relax_info->relocs_count);
9684 if (relax_info->allocated_relocs_count == 0)
9685 new_relocs_count = (sec->reloc_count + 2) * 2;
9686 else
9687 new_relocs_count = (relax_info->allocated_relocs_count + 2) * 2;
9689 new_relocs = (Elf_Internal_Rela *)
9690 bfd_zmalloc (sizeof (Elf_Internal_Rela) * (new_relocs_count));
9691 if (!new_relocs)
9692 return FALSE;
9694 /* We could handle this more quickly by finding the split point. */
9695 if (insert_at != 0)
9696 memcpy (new_relocs, *internal_relocs_p,
9697 insert_at * sizeof (Elf_Internal_Rela));
9699 new_relocs[insert_at] = this_rela;
9701 if (insert_at != sec->reloc_count)
9702 memcpy (new_relocs + insert_at + 1,
9703 (*internal_relocs_p) + insert_at,
9704 (sec->reloc_count - insert_at)
9705 * sizeof (Elf_Internal_Rela));
9707 if (*internal_relocs_p != relax_info->allocated_relocs)
9709 /* The first time we re-allocate, we can only free the
9710 old relocs if they were allocated with bfd_malloc.
9711 This is not true when keep_memory is in effect. */
9712 if (!link_info->keep_memory)
9713 free (*internal_relocs_p);
9715 else
9716 free (*internal_relocs_p);
9717 relax_info->allocated_relocs = new_relocs;
9718 relax_info->allocated_relocs_count = new_relocs_count;
9719 elf_section_data (sec)->relocs = new_relocs;
9720 sec->reloc_count++;
9721 relax_info->relocs_count = sec->reloc_count;
9722 *internal_relocs_p = new_relocs;
9724 else
9726 if (insert_at != sec->reloc_count)
9728 unsigned idx;
9729 for (idx = sec->reloc_count; idx > insert_at; idx--)
9730 (*internal_relocs_p)[idx] = (*internal_relocs_p)[idx-1];
9732 (*internal_relocs_p)[insert_at] = this_rela;
9733 sec->reloc_count++;
9734 if (relax_info->allocated_relocs)
9735 relax_info->relocs_count = sec->reloc_count;
9738 return TRUE;
9742 /* This is similar to relax_section except that when a target is moved,
9743 we shift addresses up. We also need to modify the size. This
9744 algorithm does NOT allow for relocations into the middle of the
9745 property sections. */
9747 static bfd_boolean
9748 relax_property_section (bfd *abfd,
9749 asection *sec,
9750 struct bfd_link_info *link_info)
9752 Elf_Internal_Rela *internal_relocs;
9753 bfd_byte *contents;
9754 unsigned i;
9755 bfd_boolean ok = TRUE;
9756 bfd_boolean is_full_prop_section;
9757 size_t last_zfill_target_offset = 0;
9758 asection *last_zfill_target_sec = NULL;
9759 bfd_size_type sec_size;
9760 bfd_size_type entry_size;
9762 sec_size = bfd_get_section_limit (abfd, sec);
9763 internal_relocs = retrieve_internal_relocs (abfd, sec,
9764 link_info->keep_memory);
9765 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
9766 if (contents == NULL && sec_size != 0)
9768 ok = FALSE;
9769 goto error_return;
9772 is_full_prop_section = xtensa_is_proptable_section (sec);
9773 if (is_full_prop_section)
9774 entry_size = 12;
9775 else
9776 entry_size = 8;
9778 if (internal_relocs)
9780 for (i = 0; i < sec->reloc_count; i++)
9782 Elf_Internal_Rela *irel;
9783 xtensa_relax_info *target_relax_info;
9784 unsigned r_type;
9785 asection *target_sec;
9786 literal_value val;
9787 bfd_byte *size_p, *flags_p;
9789 /* Locally change the source address.
9790 Translate the target to the new target address.
9791 If it points to this section and has been removed, MOVE IT.
9792 Also, don't forget to modify the associated SIZE at
9793 (offset + 4). */
9795 irel = &internal_relocs[i];
9796 r_type = ELF32_R_TYPE (irel->r_info);
9797 if (r_type == R_XTENSA_NONE)
9798 continue;
9800 /* Find the literal value. */
9801 r_reloc_init (&val.r_rel, abfd, irel, contents, sec_size);
9802 size_p = &contents[irel->r_offset + 4];
9803 flags_p = NULL;
9804 if (is_full_prop_section)
9805 flags_p = &contents[irel->r_offset + 8];
9806 BFD_ASSERT (irel->r_offset + entry_size <= sec_size);
9808 target_sec = r_reloc_get_section (&val.r_rel);
9809 target_relax_info = get_xtensa_relax_info (target_sec);
9811 if (target_relax_info
9812 && (target_relax_info->is_relaxable_literal_section
9813 || target_relax_info->is_relaxable_asm_section ))
9815 /* Translate the relocation's destination. */
9816 bfd_vma old_offset = val.r_rel.target_offset;
9817 bfd_vma new_offset;
9818 long old_size, new_size;
9819 text_action *act = target_relax_info->action_list.head;
9820 new_offset = old_offset -
9821 removed_by_actions (&act, old_offset, FALSE);
9823 /* Assert that we are not out of bounds. */
9824 old_size = bfd_get_32 (abfd, size_p);
9825 new_size = old_size;
9827 if (old_size == 0)
9829 /* Only the first zero-sized unreachable entry is
9830 allowed to expand. In this case the new offset
9831 should be the offset before the fill and the new
9832 size is the expansion size. For other zero-sized
9833 entries the resulting size should be zero with an
9834 offset before or after the fill address depending
9835 on whether the expanding unreachable entry
9836 preceeds it. */
9837 if (last_zfill_target_sec == 0
9838 || last_zfill_target_sec != target_sec
9839 || last_zfill_target_offset != old_offset)
9841 bfd_vma new_end_offset = new_offset;
9843 /* Recompute the new_offset, but this time don't
9844 include any fill inserted by relaxation. */
9845 act = target_relax_info->action_list.head;
9846 new_offset = old_offset -
9847 removed_by_actions (&act, old_offset, TRUE);
9849 /* If it is not unreachable and we have not yet
9850 seen an unreachable at this address, place it
9851 before the fill address. */
9852 if (flags_p && (bfd_get_32 (abfd, flags_p)
9853 & XTENSA_PROP_UNREACHABLE) != 0)
9855 new_size = new_end_offset - new_offset;
9857 last_zfill_target_sec = target_sec;
9858 last_zfill_target_offset = old_offset;
9862 else
9863 new_size -=
9864 removed_by_actions (&act, old_offset + old_size, TRUE);
9866 if (new_size != old_size)
9868 bfd_put_32 (abfd, new_size, size_p);
9869 pin_contents (sec, contents);
9872 if (new_offset != old_offset)
9874 bfd_vma diff = new_offset - old_offset;
9875 irel->r_addend += diff;
9876 pin_internal_relocs (sec, internal_relocs);
9882 /* Combine adjacent property table entries. This is also done in
9883 finish_dynamic_sections() but at that point it's too late to
9884 reclaim the space in the output section, so we do this twice. */
9886 if (internal_relocs && (!link_info->relocatable
9887 || xtensa_is_littable_section (sec)))
9889 Elf_Internal_Rela *last_irel = NULL;
9890 Elf_Internal_Rela *irel, *next_rel, *rel_end;
9891 int removed_bytes = 0;
9892 bfd_vma offset;
9893 flagword predef_flags;
9895 predef_flags = xtensa_get_property_predef_flags (sec);
9897 /* Walk over memory and relocations at the same time.
9898 This REQUIRES that the internal_relocs be sorted by offset. */
9899 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
9900 internal_reloc_compare);
9902 pin_internal_relocs (sec, internal_relocs);
9903 pin_contents (sec, contents);
9905 next_rel = internal_relocs;
9906 rel_end = internal_relocs + sec->reloc_count;
9908 BFD_ASSERT (sec->size % entry_size == 0);
9910 for (offset = 0; offset < sec->size; offset += entry_size)
9912 Elf_Internal_Rela *offset_rel, *extra_rel;
9913 bfd_vma bytes_to_remove, size, actual_offset;
9914 bfd_boolean remove_this_rel;
9915 flagword flags;
9917 /* Find the first relocation for the entry at the current offset.
9918 Adjust the offsets of any extra relocations for the previous
9919 entry. */
9920 offset_rel = NULL;
9921 if (next_rel)
9923 for (irel = next_rel; irel < rel_end; irel++)
9925 if ((irel->r_offset == offset
9926 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
9927 || irel->r_offset > offset)
9929 offset_rel = irel;
9930 break;
9932 irel->r_offset -= removed_bytes;
9936 /* Find the next relocation (if there are any left). */
9937 extra_rel = NULL;
9938 if (offset_rel)
9940 for (irel = offset_rel + 1; irel < rel_end; irel++)
9942 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
9944 extra_rel = irel;
9945 break;
9950 /* Check if there are relocations on the current entry. There
9951 should usually be a relocation on the offset field. If there
9952 are relocations on the size or flags, then we can't optimize
9953 this entry. Also, find the next relocation to examine on the
9954 next iteration. */
9955 if (offset_rel)
9957 if (offset_rel->r_offset >= offset + entry_size)
9959 next_rel = offset_rel;
9960 /* There are no relocations on the current entry, but we
9961 might still be able to remove it if the size is zero. */
9962 offset_rel = NULL;
9964 else if (offset_rel->r_offset > offset
9965 || (extra_rel
9966 && extra_rel->r_offset < offset + entry_size))
9968 /* There is a relocation on the size or flags, so we can't
9969 do anything with this entry. Continue with the next. */
9970 next_rel = offset_rel;
9971 continue;
9973 else
9975 BFD_ASSERT (offset_rel->r_offset == offset);
9976 offset_rel->r_offset -= removed_bytes;
9977 next_rel = offset_rel + 1;
9980 else
9981 next_rel = NULL;
9983 remove_this_rel = FALSE;
9984 bytes_to_remove = 0;
9985 actual_offset = offset - removed_bytes;
9986 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
9988 if (is_full_prop_section)
9989 flags = bfd_get_32 (abfd, &contents[actual_offset + 8]);
9990 else
9991 flags = predef_flags;
9993 if (size == 0
9994 && (flags & XTENSA_PROP_ALIGN) == 0
9995 && (flags & XTENSA_PROP_UNREACHABLE) == 0)
9997 /* Always remove entries with zero size and no alignment. */
9998 bytes_to_remove = entry_size;
9999 if (offset_rel)
10000 remove_this_rel = TRUE;
10002 else if (offset_rel
10003 && ELF32_R_TYPE (offset_rel->r_info) == R_XTENSA_32)
10005 if (last_irel)
10007 flagword old_flags;
10008 bfd_vma old_size =
10009 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
10010 bfd_vma old_address =
10011 (last_irel->r_addend
10012 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
10013 bfd_vma new_address =
10014 (offset_rel->r_addend
10015 + bfd_get_32 (abfd, &contents[actual_offset]));
10016 if (is_full_prop_section)
10017 old_flags = bfd_get_32
10018 (abfd, &contents[last_irel->r_offset + 8]);
10019 else
10020 old_flags = predef_flags;
10022 if ((ELF32_R_SYM (offset_rel->r_info)
10023 == ELF32_R_SYM (last_irel->r_info))
10024 && old_address + old_size == new_address
10025 && old_flags == flags
10026 && (old_flags & XTENSA_PROP_INSN_BRANCH_TARGET) == 0
10027 && (old_flags & XTENSA_PROP_INSN_LOOP_TARGET) == 0)
10029 /* Fix the old size. */
10030 bfd_put_32 (abfd, old_size + size,
10031 &contents[last_irel->r_offset + 4]);
10032 bytes_to_remove = entry_size;
10033 remove_this_rel = TRUE;
10035 else
10036 last_irel = offset_rel;
10038 else
10039 last_irel = offset_rel;
10042 if (remove_this_rel)
10044 offset_rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
10045 offset_rel->r_offset = 0;
10048 if (bytes_to_remove != 0)
10050 removed_bytes += bytes_to_remove;
10051 if (offset + bytes_to_remove < sec->size)
10052 memmove (&contents[actual_offset],
10053 &contents[actual_offset + bytes_to_remove],
10054 sec->size - offset - bytes_to_remove);
10058 if (removed_bytes)
10060 /* Fix up any extra relocations on the last entry. */
10061 for (irel = next_rel; irel < rel_end; irel++)
10062 irel->r_offset -= removed_bytes;
10064 /* Clear the removed bytes. */
10065 memset (&contents[sec->size - removed_bytes], 0, removed_bytes);
10067 if (sec->rawsize == 0)
10068 sec->rawsize = sec->size;
10069 sec->size -= removed_bytes;
10071 if (xtensa_is_littable_section (sec))
10073 asection *sgotloc = elf_xtensa_hash_table (link_info)->sgotloc;
10074 if (sgotloc)
10075 sgotloc->size -= removed_bytes;
10080 error_return:
10081 release_internal_relocs (sec, internal_relocs);
10082 release_contents (sec, contents);
10083 return ok;
10087 /* Third relaxation pass. */
10089 /* Change symbol values to account for removed literals. */
10091 bfd_boolean
10092 relax_section_symbols (bfd *abfd, asection *sec)
10094 xtensa_relax_info *relax_info;
10095 unsigned int sec_shndx;
10096 Elf_Internal_Shdr *symtab_hdr;
10097 Elf_Internal_Sym *isymbuf;
10098 unsigned i, num_syms, num_locals;
10100 relax_info = get_xtensa_relax_info (sec);
10101 BFD_ASSERT (relax_info);
10103 if (!relax_info->is_relaxable_literal_section
10104 && !relax_info->is_relaxable_asm_section)
10105 return TRUE;
10107 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
10109 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10110 isymbuf = retrieve_local_syms (abfd);
10112 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
10113 num_locals = symtab_hdr->sh_info;
10115 /* Adjust the local symbols defined in this section. */
10116 for (i = 0; i < num_locals; i++)
10118 Elf_Internal_Sym *isym = &isymbuf[i];
10120 if (isym->st_shndx == sec_shndx)
10122 text_action *act = relax_info->action_list.head;
10123 bfd_vma orig_addr = isym->st_value;
10125 isym->st_value -= removed_by_actions (&act, orig_addr, FALSE);
10127 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
10128 isym->st_size -=
10129 removed_by_actions (&act, orig_addr + isym->st_size, FALSE);
10133 /* Now adjust the global symbols defined in this section. */
10134 for (i = 0; i < (num_syms - num_locals); i++)
10136 struct elf_link_hash_entry *sym_hash;
10138 sym_hash = elf_sym_hashes (abfd)[i];
10140 if (sym_hash->root.type == bfd_link_hash_warning)
10141 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
10143 if ((sym_hash->root.type == bfd_link_hash_defined
10144 || sym_hash->root.type == bfd_link_hash_defweak)
10145 && sym_hash->root.u.def.section == sec)
10147 text_action *act = relax_info->action_list.head;
10148 bfd_vma orig_addr = sym_hash->root.u.def.value;
10150 sym_hash->root.u.def.value -=
10151 removed_by_actions (&act, orig_addr, FALSE);
10153 if (sym_hash->type == STT_FUNC)
10154 sym_hash->size -=
10155 removed_by_actions (&act, orig_addr + sym_hash->size, FALSE);
10159 return TRUE;
10163 /* "Fix" handling functions, called while performing relocations. */
10165 static bfd_boolean
10166 do_fix_for_relocatable_link (Elf_Internal_Rela *rel,
10167 bfd *input_bfd,
10168 asection *input_section,
10169 bfd_byte *contents)
10171 r_reloc r_rel;
10172 asection *sec, *old_sec;
10173 bfd_vma old_offset;
10174 int r_type = ELF32_R_TYPE (rel->r_info);
10175 reloc_bfd_fix *fix;
10177 if (r_type == R_XTENSA_NONE)
10178 return TRUE;
10180 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10181 if (!fix)
10182 return TRUE;
10184 r_reloc_init (&r_rel, input_bfd, rel, contents,
10185 bfd_get_section_limit (input_bfd, input_section));
10186 old_sec = r_reloc_get_section (&r_rel);
10187 old_offset = r_rel.target_offset;
10189 if (!old_sec || !r_reloc_is_defined (&r_rel))
10191 if (r_type != R_XTENSA_ASM_EXPAND)
10193 (*_bfd_error_handler)
10194 (_("%B(%A+0x%lx): unexpected fix for %s relocation"),
10195 input_bfd, input_section, rel->r_offset,
10196 elf_howto_table[r_type].name);
10197 return FALSE;
10199 /* Leave it be. Resolution will happen in a later stage. */
10201 else
10203 sec = fix->target_sec;
10204 rel->r_addend += ((sec->output_offset + fix->target_offset)
10205 - (old_sec->output_offset + old_offset));
10207 return TRUE;
10211 static void
10212 do_fix_for_final_link (Elf_Internal_Rela *rel,
10213 bfd *input_bfd,
10214 asection *input_section,
10215 bfd_byte *contents,
10216 bfd_vma *relocationp)
10218 asection *sec;
10219 int r_type = ELF32_R_TYPE (rel->r_info);
10220 reloc_bfd_fix *fix;
10221 bfd_vma fixup_diff;
10223 if (r_type == R_XTENSA_NONE)
10224 return;
10226 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
10227 if (!fix)
10228 return;
10230 sec = fix->target_sec;
10232 fixup_diff = rel->r_addend;
10233 if (elf_howto_table[fix->src_type].partial_inplace)
10235 bfd_vma inplace_val;
10236 BFD_ASSERT (fix->src_offset
10237 < bfd_get_section_limit (input_bfd, input_section));
10238 inplace_val = bfd_get_32 (input_bfd, &contents[fix->src_offset]);
10239 fixup_diff += inplace_val;
10242 *relocationp = (sec->output_section->vma
10243 + sec->output_offset
10244 + fix->target_offset - fixup_diff);
10248 /* Miscellaneous utility functions.... */
10250 static asection *
10251 elf_xtensa_get_plt_section (struct bfd_link_info *info, int chunk)
10253 struct elf_xtensa_link_hash_table *htab;
10254 bfd *dynobj;
10255 char plt_name[10];
10257 if (chunk == 0)
10259 htab = elf_xtensa_hash_table (info);
10260 if (htab == NULL)
10261 return NULL;
10263 return htab->splt;
10266 dynobj = elf_hash_table (info)->dynobj;
10267 sprintf (plt_name, ".plt.%u", chunk);
10268 return bfd_get_section_by_name (dynobj, plt_name);
10272 static asection *
10273 elf_xtensa_get_gotplt_section (struct bfd_link_info *info, int chunk)
10275 struct elf_xtensa_link_hash_table *htab;
10276 bfd *dynobj;
10277 char got_name[14];
10279 if (chunk == 0)
10281 htab = elf_xtensa_hash_table (info);
10282 if (htab == NULL)
10283 return NULL;
10284 return htab->sgotplt;
10287 dynobj = elf_hash_table (info)->dynobj;
10288 sprintf (got_name, ".got.plt.%u", chunk);
10289 return bfd_get_section_by_name (dynobj, got_name);
10293 /* Get the input section for a given symbol index.
10294 If the symbol is:
10295 . a section symbol, return the section;
10296 . a common symbol, return the common section;
10297 . an undefined symbol, return the undefined section;
10298 . an indirect symbol, follow the links;
10299 . an absolute value, return the absolute section. */
10301 static asection *
10302 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
10304 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10305 asection *target_sec = NULL;
10306 if (r_symndx < symtab_hdr->sh_info)
10308 Elf_Internal_Sym *isymbuf;
10309 unsigned int section_index;
10311 isymbuf = retrieve_local_syms (abfd);
10312 section_index = isymbuf[r_symndx].st_shndx;
10314 if (section_index == SHN_UNDEF)
10315 target_sec = bfd_und_section_ptr;
10316 else if (section_index == SHN_ABS)
10317 target_sec = bfd_abs_section_ptr;
10318 else if (section_index == SHN_COMMON)
10319 target_sec = bfd_com_section_ptr;
10320 else
10321 target_sec = bfd_section_from_elf_index (abfd, section_index);
10323 else
10325 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10326 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
10328 while (h->root.type == bfd_link_hash_indirect
10329 || h->root.type == bfd_link_hash_warning)
10330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10332 switch (h->root.type)
10334 case bfd_link_hash_defined:
10335 case bfd_link_hash_defweak:
10336 target_sec = h->root.u.def.section;
10337 break;
10338 case bfd_link_hash_common:
10339 target_sec = bfd_com_section_ptr;
10340 break;
10341 case bfd_link_hash_undefined:
10342 case bfd_link_hash_undefweak:
10343 target_sec = bfd_und_section_ptr;
10344 break;
10345 default: /* New indirect warning. */
10346 target_sec = bfd_und_section_ptr;
10347 break;
10350 return target_sec;
10354 static struct elf_link_hash_entry *
10355 get_elf_r_symndx_hash_entry (bfd *abfd, unsigned long r_symndx)
10357 unsigned long indx;
10358 struct elf_link_hash_entry *h;
10359 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10361 if (r_symndx < symtab_hdr->sh_info)
10362 return NULL;
10364 indx = r_symndx - symtab_hdr->sh_info;
10365 h = elf_sym_hashes (abfd)[indx];
10366 while (h->root.type == bfd_link_hash_indirect
10367 || h->root.type == bfd_link_hash_warning)
10368 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10369 return h;
10373 /* Get the section-relative offset for a symbol number. */
10375 static bfd_vma
10376 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
10378 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10379 bfd_vma offset = 0;
10381 if (r_symndx < symtab_hdr->sh_info)
10383 Elf_Internal_Sym *isymbuf;
10384 isymbuf = retrieve_local_syms (abfd);
10385 offset = isymbuf[r_symndx].st_value;
10387 else
10389 unsigned long indx = r_symndx - symtab_hdr->sh_info;
10390 struct elf_link_hash_entry *h =
10391 elf_sym_hashes (abfd)[indx];
10393 while (h->root.type == bfd_link_hash_indirect
10394 || h->root.type == bfd_link_hash_warning)
10395 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10396 if (h->root.type == bfd_link_hash_defined
10397 || h->root.type == bfd_link_hash_defweak)
10398 offset = h->root.u.def.value;
10400 return offset;
10404 static bfd_boolean
10405 is_reloc_sym_weak (bfd *abfd, Elf_Internal_Rela *rel)
10407 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
10408 struct elf_link_hash_entry *h;
10410 h = get_elf_r_symndx_hash_entry (abfd, r_symndx);
10411 if (h && h->root.type == bfd_link_hash_defweak)
10412 return TRUE;
10413 return FALSE;
10417 static bfd_boolean
10418 pcrel_reloc_fits (xtensa_opcode opc,
10419 int opnd,
10420 bfd_vma self_address,
10421 bfd_vma dest_address)
10423 xtensa_isa isa = xtensa_default_isa;
10424 uint32 valp = dest_address;
10425 if (xtensa_operand_do_reloc (isa, opc, opnd, &valp, self_address)
10426 || xtensa_operand_encode (isa, opc, opnd, &valp))
10427 return FALSE;
10428 return TRUE;
10432 static bfd_boolean
10433 xtensa_is_property_section (asection *sec)
10435 if (xtensa_is_insntable_section (sec)
10436 || xtensa_is_littable_section (sec)
10437 || xtensa_is_proptable_section (sec))
10438 return TRUE;
10440 return FALSE;
10444 static bfd_boolean
10445 xtensa_is_insntable_section (asection *sec)
10447 if (CONST_STRNEQ (sec->name, XTENSA_INSN_SEC_NAME)
10448 || CONST_STRNEQ (sec->name, ".gnu.linkonce.x."))
10449 return TRUE;
10451 return FALSE;
10455 static bfd_boolean
10456 xtensa_is_littable_section (asection *sec)
10458 if (CONST_STRNEQ (sec->name, XTENSA_LIT_SEC_NAME)
10459 || CONST_STRNEQ (sec->name, ".gnu.linkonce.p."))
10460 return TRUE;
10462 return FALSE;
10466 static bfd_boolean
10467 xtensa_is_proptable_section (asection *sec)
10469 if (CONST_STRNEQ (sec->name, XTENSA_PROP_SEC_NAME)
10470 || CONST_STRNEQ (sec->name, ".gnu.linkonce.prop."))
10471 return TRUE;
10473 return FALSE;
10477 static int
10478 internal_reloc_compare (const void *ap, const void *bp)
10480 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
10481 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
10483 if (a->r_offset != b->r_offset)
10484 return (a->r_offset - b->r_offset);
10486 /* We don't need to sort on these criteria for correctness,
10487 but enforcing a more strict ordering prevents unstable qsort
10488 from behaving differently with different implementations.
10489 Without the code below we get correct but different results
10490 on Solaris 2.7 and 2.8. We would like to always produce the
10491 same results no matter the host. */
10493 if (a->r_info != b->r_info)
10494 return (a->r_info - b->r_info);
10496 return (a->r_addend - b->r_addend);
10500 static int
10501 internal_reloc_matches (const void *ap, const void *bp)
10503 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
10504 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
10506 /* Check if one entry overlaps with the other; this shouldn't happen
10507 except when searching for a match. */
10508 return (a->r_offset - b->r_offset);
10512 /* Predicate function used to look up a section in a particular group. */
10514 static bfd_boolean
10515 match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
10517 const char *gname = inf;
10518 const char *group_name = elf_group_name (sec);
10520 return (group_name == gname
10521 || (group_name != NULL
10522 && gname != NULL
10523 && strcmp (group_name, gname) == 0));
10527 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
10529 static char *
10530 xtensa_property_section_name (asection *sec, const char *base_name)
10532 const char *suffix, *group_name;
10533 char *prop_sec_name;
10535 group_name = elf_group_name (sec);
10536 if (group_name)
10538 suffix = strrchr (sec->name, '.');
10539 if (suffix == sec->name)
10540 suffix = 0;
10541 prop_sec_name = (char *) bfd_malloc (strlen (base_name) + 1
10542 + (suffix ? strlen (suffix) : 0));
10543 strcpy (prop_sec_name, base_name);
10544 if (suffix)
10545 strcat (prop_sec_name, suffix);
10547 else if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
10549 char *linkonce_kind = 0;
10551 if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
10552 linkonce_kind = "x.";
10553 else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
10554 linkonce_kind = "p.";
10555 else if (strcmp (base_name, XTENSA_PROP_SEC_NAME) == 0)
10556 linkonce_kind = "prop.";
10557 else
10558 abort ();
10560 prop_sec_name = (char *) bfd_malloc (strlen (sec->name)
10561 + strlen (linkonce_kind) + 1);
10562 memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
10563 strcpy (prop_sec_name + linkonce_len, linkonce_kind);
10565 suffix = sec->name + linkonce_len;
10566 /* For backward compatibility, replace "t." instead of inserting
10567 the new linkonce_kind (but not for "prop" sections). */
10568 if (CONST_STRNEQ (suffix, "t.") && linkonce_kind[1] == '.')
10569 suffix += 2;
10570 strcat (prop_sec_name + linkonce_len, suffix);
10572 else
10573 prop_sec_name = strdup (base_name);
10575 return prop_sec_name;
10579 static asection *
10580 xtensa_get_property_section (asection *sec, const char *base_name)
10582 char *prop_sec_name;
10583 asection *prop_sec;
10585 prop_sec_name = xtensa_property_section_name (sec, base_name);
10586 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
10587 match_section_group,
10588 (void *) elf_group_name (sec));
10589 free (prop_sec_name);
10590 return prop_sec;
10594 asection *
10595 xtensa_make_property_section (asection *sec, const char *base_name)
10597 char *prop_sec_name;
10598 asection *prop_sec;
10600 /* Check if the section already exists. */
10601 prop_sec_name = xtensa_property_section_name (sec, base_name);
10602 prop_sec = bfd_get_section_by_name_if (sec->owner, prop_sec_name,
10603 match_section_group,
10604 (void *) elf_group_name (sec));
10605 /* If not, create it. */
10606 if (! prop_sec)
10608 flagword flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY);
10609 flags |= (bfd_get_section_flags (sec->owner, sec)
10610 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES));
10612 prop_sec = bfd_make_section_anyway_with_flags
10613 (sec->owner, strdup (prop_sec_name), flags);
10614 if (! prop_sec)
10615 return 0;
10617 elf_group_name (prop_sec) = elf_group_name (sec);
10620 free (prop_sec_name);
10621 return prop_sec;
10625 flagword
10626 xtensa_get_property_predef_flags (asection *sec)
10628 if (xtensa_is_insntable_section (sec))
10629 return (XTENSA_PROP_INSN
10630 | XTENSA_PROP_NO_TRANSFORM
10631 | XTENSA_PROP_INSN_NO_REORDER);
10633 if (xtensa_is_littable_section (sec))
10634 return (XTENSA_PROP_LITERAL
10635 | XTENSA_PROP_NO_TRANSFORM
10636 | XTENSA_PROP_INSN_NO_REORDER);
10638 return 0;
10642 /* Other functions called directly by the linker. */
10644 bfd_boolean
10645 xtensa_callback_required_dependence (bfd *abfd,
10646 asection *sec,
10647 struct bfd_link_info *link_info,
10648 deps_callback_t callback,
10649 void *closure)
10651 Elf_Internal_Rela *internal_relocs;
10652 bfd_byte *contents;
10653 unsigned i;
10654 bfd_boolean ok = TRUE;
10655 bfd_size_type sec_size;
10657 sec_size = bfd_get_section_limit (abfd, sec);
10659 /* ".plt*" sections have no explicit relocations but they contain L32R
10660 instructions that reference the corresponding ".got.plt*" sections. */
10661 if ((sec->flags & SEC_LINKER_CREATED) != 0
10662 && CONST_STRNEQ (sec->name, ".plt"))
10664 asection *sgotplt;
10666 /* Find the corresponding ".got.plt*" section. */
10667 if (sec->name[4] == '\0')
10668 sgotplt = bfd_get_section_by_name (sec->owner, ".got.plt");
10669 else
10671 char got_name[14];
10672 int chunk = 0;
10674 BFD_ASSERT (sec->name[4] == '.');
10675 chunk = strtol (&sec->name[5], NULL, 10);
10677 sprintf (got_name, ".got.plt.%u", chunk);
10678 sgotplt = bfd_get_section_by_name (sec->owner, got_name);
10680 BFD_ASSERT (sgotplt);
10682 /* Assume worst-case offsets: L32R at the very end of the ".plt"
10683 section referencing a literal at the very beginning of
10684 ".got.plt". This is very close to the real dependence, anyway. */
10685 (*callback) (sec, sec_size, sgotplt, 0, closure);
10688 /* Only ELF files are supported for Xtensa. Check here to avoid a segfault
10689 when building uclibc, which runs "ld -b binary /dev/null". */
10690 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
10691 return ok;
10693 internal_relocs = retrieve_internal_relocs (abfd, sec,
10694 link_info->keep_memory);
10695 if (internal_relocs == NULL
10696 || sec->reloc_count == 0)
10697 return ok;
10699 /* Cache the contents for the duration of this scan. */
10700 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
10701 if (contents == NULL && sec_size != 0)
10703 ok = FALSE;
10704 goto error_return;
10707 if (!xtensa_default_isa)
10708 xtensa_default_isa = xtensa_isa_init (0, 0);
10710 for (i = 0; i < sec->reloc_count; i++)
10712 Elf_Internal_Rela *irel = &internal_relocs[i];
10713 if (is_l32r_relocation (abfd, sec, contents, irel))
10715 r_reloc l32r_rel;
10716 asection *target_sec;
10717 bfd_vma target_offset;
10719 r_reloc_init (&l32r_rel, abfd, irel, contents, sec_size);
10720 target_sec = NULL;
10721 target_offset = 0;
10722 /* L32Rs must be local to the input file. */
10723 if (r_reloc_is_defined (&l32r_rel))
10725 target_sec = r_reloc_get_section (&l32r_rel);
10726 target_offset = l32r_rel.target_offset;
10728 (*callback) (sec, irel->r_offset, target_sec, target_offset,
10729 closure);
10733 error_return:
10734 release_internal_relocs (sec, internal_relocs);
10735 release_contents (sec, contents);
10736 return ok;
10739 /* The default literal sections should always be marked as "code" (i.e.,
10740 SHF_EXECINSTR). This is particularly important for the Linux kernel
10741 module loader so that the literals are not placed after the text. */
10742 static const struct bfd_elf_special_section elf_xtensa_special_sections[] =
10744 { STRING_COMMA_LEN (".fini.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
10745 { STRING_COMMA_LEN (".init.literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
10746 { STRING_COMMA_LEN (".literal"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
10747 { STRING_COMMA_LEN (".xtensa.info"), 0, SHT_NOTE, 0 },
10748 { NULL, 0, 0, 0, 0 }
10751 #ifndef ELF_ARCH
10752 #define TARGET_LITTLE_SYM bfd_elf32_xtensa_le_vec
10753 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
10754 #define TARGET_BIG_SYM bfd_elf32_xtensa_be_vec
10755 #define TARGET_BIG_NAME "elf32-xtensa-be"
10756 #define ELF_ARCH bfd_arch_xtensa
10758 #define ELF_MACHINE_CODE EM_XTENSA
10759 #define ELF_MACHINE_ALT1 EM_XTENSA_OLD
10761 #if XCHAL_HAVE_MMU
10762 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
10763 #else /* !XCHAL_HAVE_MMU */
10764 #define ELF_MAXPAGESIZE 1
10765 #endif /* !XCHAL_HAVE_MMU */
10766 #endif /* ELF_ARCH */
10768 #define elf_backend_can_gc_sections 1
10769 #define elf_backend_can_refcount 1
10770 #define elf_backend_plt_readonly 1
10771 #define elf_backend_got_header_size 4
10772 #define elf_backend_want_dynbss 0
10773 #define elf_backend_want_got_plt 1
10775 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
10777 #define bfd_elf32_mkobject elf_xtensa_mkobject
10779 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
10780 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
10781 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
10782 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
10783 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
10784 #define bfd_elf32_bfd_reloc_name_lookup \
10785 elf_xtensa_reloc_name_lookup
10786 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
10787 #define bfd_elf32_bfd_link_hash_table_create elf_xtensa_link_hash_table_create
10789 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
10790 #define elf_backend_check_relocs elf_xtensa_check_relocs
10791 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
10792 #define elf_backend_discard_info elf_xtensa_discard_info
10793 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
10794 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
10795 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
10796 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
10797 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
10798 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
10799 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
10800 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
10801 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
10802 #define elf_backend_object_p elf_xtensa_object_p
10803 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
10804 #define elf_backend_relocate_section elf_xtensa_relocate_section
10805 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
10806 #define elf_backend_always_size_sections elf_xtensa_always_size_sections
10807 #define elf_backend_omit_section_dynsym \
10808 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
10809 #define elf_backend_special_sections elf_xtensa_special_sections
10810 #define elf_backend_action_discarded elf_xtensa_action_discarded
10811 #define elf_backend_copy_indirect_symbol elf_xtensa_copy_indirect_symbol
10813 #include "elf32-target.h"