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
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
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;
159 static reloc_howto_type elf_howto_table
[] =
161 HOWTO (R_XTENSA_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
162 bfd_elf_xtensa_reloc
, "R_XTENSA_NONE",
164 HOWTO (R_XTENSA_32
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
165 bfd_elf_xtensa_reloc
, "R_XTENSA_32",
166 TRUE
, 0xffffffff, 0xffffffff, FALSE
),
168 /* Replace a 32-bit value with a value from the runtime linker (only
169 used by linker-generated stub functions). The r_addend value is
170 special: 1 means to substitute a pointer to the runtime linker's
171 dynamic resolver function; 2 means to substitute the link map for
172 the shared object. */
173 HOWTO (R_XTENSA_RTLD
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
174 NULL
, "R_XTENSA_RTLD", FALSE
, 0, 0, FALSE
),
176 HOWTO (R_XTENSA_GLOB_DAT
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
177 bfd_elf_generic_reloc
, "R_XTENSA_GLOB_DAT",
178 FALSE
, 0, 0xffffffff, FALSE
),
179 HOWTO (R_XTENSA_JMP_SLOT
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
180 bfd_elf_generic_reloc
, "R_XTENSA_JMP_SLOT",
181 FALSE
, 0, 0xffffffff, FALSE
),
182 HOWTO (R_XTENSA_RELATIVE
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
183 bfd_elf_generic_reloc
, "R_XTENSA_RELATIVE",
184 FALSE
, 0, 0xffffffff, FALSE
),
185 HOWTO (R_XTENSA_PLT
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
186 bfd_elf_xtensa_reloc
, "R_XTENSA_PLT",
187 FALSE
, 0, 0xffffffff, FALSE
),
191 /* Old relocations for backward compatibility. */
192 HOWTO (R_XTENSA_OP0
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
193 bfd_elf_xtensa_reloc
, "R_XTENSA_OP0", FALSE
, 0, 0, TRUE
),
194 HOWTO (R_XTENSA_OP1
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
195 bfd_elf_xtensa_reloc
, "R_XTENSA_OP1", FALSE
, 0, 0, TRUE
),
196 HOWTO (R_XTENSA_OP2
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
197 bfd_elf_xtensa_reloc
, "R_XTENSA_OP2", FALSE
, 0, 0, TRUE
),
199 /* Assembly auto-expansion. */
200 HOWTO (R_XTENSA_ASM_EXPAND
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
201 bfd_elf_xtensa_reloc
, "R_XTENSA_ASM_EXPAND", FALSE
, 0, 0, TRUE
),
202 /* Relax assembly auto-expansion. */
203 HOWTO (R_XTENSA_ASM_SIMPLIFY
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
204 bfd_elf_xtensa_reloc
, "R_XTENSA_ASM_SIMPLIFY", FALSE
, 0, 0, TRUE
),
208 HOWTO (R_XTENSA_32_PCREL
, 0, 2, 32, TRUE
, 0, complain_overflow_bitfield
,
209 bfd_elf_xtensa_reloc
, "R_XTENSA_32_PCREL",
210 FALSE
, 0, 0xffffffff, TRUE
),
212 /* GNU extension to record C++ vtable hierarchy. */
213 HOWTO (R_XTENSA_GNU_VTINHERIT
, 0, 2, 0, FALSE
, 0, complain_overflow_dont
,
214 NULL
, "R_XTENSA_GNU_VTINHERIT",
216 /* GNU extension to record C++ vtable member usage. */
217 HOWTO (R_XTENSA_GNU_VTENTRY
, 0, 2, 0, FALSE
, 0, complain_overflow_dont
,
218 _bfd_elf_rel_vtable_reloc_fn
, "R_XTENSA_GNU_VTENTRY",
221 /* Relocations for supporting difference of symbols. */
222 HOWTO (R_XTENSA_DIFF8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
223 bfd_elf_xtensa_reloc
, "R_XTENSA_DIFF8", FALSE
, 0, 0xff, FALSE
),
224 HOWTO (R_XTENSA_DIFF16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
225 bfd_elf_xtensa_reloc
, "R_XTENSA_DIFF16", FALSE
, 0, 0xffff, FALSE
),
226 HOWTO (R_XTENSA_DIFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
227 bfd_elf_xtensa_reloc
, "R_XTENSA_DIFF32", FALSE
, 0, 0xffffffff, FALSE
),
229 /* General immediate operand relocations. */
230 HOWTO (R_XTENSA_SLOT0_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
231 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT0_OP", FALSE
, 0, 0, TRUE
),
232 HOWTO (R_XTENSA_SLOT1_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
233 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT1_OP", FALSE
, 0, 0, TRUE
),
234 HOWTO (R_XTENSA_SLOT2_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
235 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT2_OP", FALSE
, 0, 0, TRUE
),
236 HOWTO (R_XTENSA_SLOT3_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
237 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT3_OP", FALSE
, 0, 0, TRUE
),
238 HOWTO (R_XTENSA_SLOT4_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
239 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT4_OP", FALSE
, 0, 0, TRUE
),
240 HOWTO (R_XTENSA_SLOT5_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
241 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT5_OP", FALSE
, 0, 0, TRUE
),
242 HOWTO (R_XTENSA_SLOT6_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
243 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT6_OP", FALSE
, 0, 0, TRUE
),
244 HOWTO (R_XTENSA_SLOT7_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
245 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT7_OP", FALSE
, 0, 0, TRUE
),
246 HOWTO (R_XTENSA_SLOT8_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
247 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT8_OP", FALSE
, 0, 0, TRUE
),
248 HOWTO (R_XTENSA_SLOT9_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
249 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT9_OP", FALSE
, 0, 0, TRUE
),
250 HOWTO (R_XTENSA_SLOT10_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
251 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT10_OP", FALSE
, 0, 0, TRUE
),
252 HOWTO (R_XTENSA_SLOT11_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
253 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT11_OP", FALSE
, 0, 0, TRUE
),
254 HOWTO (R_XTENSA_SLOT12_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
255 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT12_OP", FALSE
, 0, 0, TRUE
),
256 HOWTO (R_XTENSA_SLOT13_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
257 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT13_OP", FALSE
, 0, 0, TRUE
),
258 HOWTO (R_XTENSA_SLOT14_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
259 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT14_OP", FALSE
, 0, 0, TRUE
),
261 /* "Alternate" relocations. The meaning of these is opcode-specific. */
262 HOWTO (R_XTENSA_SLOT0_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
263 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT0_ALT", FALSE
, 0, 0, TRUE
),
264 HOWTO (R_XTENSA_SLOT1_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
265 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT1_ALT", FALSE
, 0, 0, TRUE
),
266 HOWTO (R_XTENSA_SLOT2_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
267 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT2_ALT", FALSE
, 0, 0, TRUE
),
268 HOWTO (R_XTENSA_SLOT3_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
269 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT3_ALT", FALSE
, 0, 0, TRUE
),
270 HOWTO (R_XTENSA_SLOT4_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
271 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT4_ALT", FALSE
, 0, 0, TRUE
),
272 HOWTO (R_XTENSA_SLOT5_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
273 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT5_ALT", FALSE
, 0, 0, TRUE
),
274 HOWTO (R_XTENSA_SLOT6_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
275 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT6_ALT", FALSE
, 0, 0, TRUE
),
276 HOWTO (R_XTENSA_SLOT7_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
277 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT7_ALT", FALSE
, 0, 0, TRUE
),
278 HOWTO (R_XTENSA_SLOT8_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
279 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT8_ALT", FALSE
, 0, 0, TRUE
),
280 HOWTO (R_XTENSA_SLOT9_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
281 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT9_ALT", FALSE
, 0, 0, TRUE
),
282 HOWTO (R_XTENSA_SLOT10_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
283 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT10_ALT", FALSE
, 0, 0, TRUE
),
284 HOWTO (R_XTENSA_SLOT11_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
285 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT11_ALT", FALSE
, 0, 0, TRUE
),
286 HOWTO (R_XTENSA_SLOT12_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
287 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT12_ALT", FALSE
, 0, 0, TRUE
),
288 HOWTO (R_XTENSA_SLOT13_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
289 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT13_ALT", FALSE
, 0, 0, TRUE
),
290 HOWTO (R_XTENSA_SLOT14_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
291 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT14_ALT", FALSE
, 0, 0, TRUE
),
293 /* TLS relocations. */
294 HOWTO (R_XTENSA_TLSDESC_FN
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
295 bfd_elf_xtensa_reloc
, "R_XTENSA_TLSDESC_FN",
296 FALSE
, 0, 0xffffffff, FALSE
),
297 HOWTO (R_XTENSA_TLSDESC_ARG
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
298 bfd_elf_xtensa_reloc
, "R_XTENSA_TLSDESC_ARG",
299 FALSE
, 0, 0xffffffff, FALSE
),
300 HOWTO (R_XTENSA_TLS_DTPOFF
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
301 bfd_elf_xtensa_reloc
, "R_XTENSA_TLS_DTPOFF",
302 FALSE
, 0, 0xffffffff, FALSE
),
303 HOWTO (R_XTENSA_TLS_TPOFF
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
304 bfd_elf_xtensa_reloc
, "R_XTENSA_TLS_TPOFF",
305 FALSE
, 0, 0xffffffff, FALSE
),
306 HOWTO (R_XTENSA_TLS_FUNC
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
307 bfd_elf_xtensa_reloc
, "R_XTENSA_TLS_FUNC",
309 HOWTO (R_XTENSA_TLS_ARG
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
310 bfd_elf_xtensa_reloc
, "R_XTENSA_TLS_ARG",
312 HOWTO (R_XTENSA_TLS_CALL
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
313 bfd_elf_xtensa_reloc
, "R_XTENSA_TLS_CALL",
319 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
324 static reloc_howto_type
*
325 elf_xtensa_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
326 bfd_reloc_code_real_type code
)
331 TRACE ("BFD_RELOC_NONE");
332 return &elf_howto_table
[(unsigned) R_XTENSA_NONE
];
335 TRACE ("BFD_RELOC_32");
336 return &elf_howto_table
[(unsigned) R_XTENSA_32
];
338 case BFD_RELOC_32_PCREL
:
339 TRACE ("BFD_RELOC_32_PCREL");
340 return &elf_howto_table
[(unsigned) R_XTENSA_32_PCREL
];
342 case BFD_RELOC_XTENSA_DIFF8
:
343 TRACE ("BFD_RELOC_XTENSA_DIFF8");
344 return &elf_howto_table
[(unsigned) R_XTENSA_DIFF8
];
346 case BFD_RELOC_XTENSA_DIFF16
:
347 TRACE ("BFD_RELOC_XTENSA_DIFF16");
348 return &elf_howto_table
[(unsigned) R_XTENSA_DIFF16
];
350 case BFD_RELOC_XTENSA_DIFF32
:
351 TRACE ("BFD_RELOC_XTENSA_DIFF32");
352 return &elf_howto_table
[(unsigned) R_XTENSA_DIFF32
];
354 case BFD_RELOC_XTENSA_RTLD
:
355 TRACE ("BFD_RELOC_XTENSA_RTLD");
356 return &elf_howto_table
[(unsigned) R_XTENSA_RTLD
];
358 case BFD_RELOC_XTENSA_GLOB_DAT
:
359 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
360 return &elf_howto_table
[(unsigned) R_XTENSA_GLOB_DAT
];
362 case BFD_RELOC_XTENSA_JMP_SLOT
:
363 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
364 return &elf_howto_table
[(unsigned) R_XTENSA_JMP_SLOT
];
366 case BFD_RELOC_XTENSA_RELATIVE
:
367 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
368 return &elf_howto_table
[(unsigned) R_XTENSA_RELATIVE
];
370 case BFD_RELOC_XTENSA_PLT
:
371 TRACE ("BFD_RELOC_XTENSA_PLT");
372 return &elf_howto_table
[(unsigned) R_XTENSA_PLT
];
374 case BFD_RELOC_XTENSA_OP0
:
375 TRACE ("BFD_RELOC_XTENSA_OP0");
376 return &elf_howto_table
[(unsigned) R_XTENSA_OP0
];
378 case BFD_RELOC_XTENSA_OP1
:
379 TRACE ("BFD_RELOC_XTENSA_OP1");
380 return &elf_howto_table
[(unsigned) R_XTENSA_OP1
];
382 case BFD_RELOC_XTENSA_OP2
:
383 TRACE ("BFD_RELOC_XTENSA_OP2");
384 return &elf_howto_table
[(unsigned) R_XTENSA_OP2
];
386 case BFD_RELOC_XTENSA_ASM_EXPAND
:
387 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
388 return &elf_howto_table
[(unsigned) R_XTENSA_ASM_EXPAND
];
390 case BFD_RELOC_XTENSA_ASM_SIMPLIFY
:
391 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
392 return &elf_howto_table
[(unsigned) R_XTENSA_ASM_SIMPLIFY
];
394 case BFD_RELOC_VTABLE_INHERIT
:
395 TRACE ("BFD_RELOC_VTABLE_INHERIT");
396 return &elf_howto_table
[(unsigned) R_XTENSA_GNU_VTINHERIT
];
398 case BFD_RELOC_VTABLE_ENTRY
:
399 TRACE ("BFD_RELOC_VTABLE_ENTRY");
400 return &elf_howto_table
[(unsigned) R_XTENSA_GNU_VTENTRY
];
402 case BFD_RELOC_XTENSA_TLSDESC_FN
:
403 TRACE ("BFD_RELOC_XTENSA_TLSDESC_FN");
404 return &elf_howto_table
[(unsigned) R_XTENSA_TLSDESC_FN
];
406 case BFD_RELOC_XTENSA_TLSDESC_ARG
:
407 TRACE ("BFD_RELOC_XTENSA_TLSDESC_ARG");
408 return &elf_howto_table
[(unsigned) R_XTENSA_TLSDESC_ARG
];
410 case BFD_RELOC_XTENSA_TLS_DTPOFF
:
411 TRACE ("BFD_RELOC_XTENSA_TLS_DTPOFF");
412 return &elf_howto_table
[(unsigned) R_XTENSA_TLS_DTPOFF
];
414 case BFD_RELOC_XTENSA_TLS_TPOFF
:
415 TRACE ("BFD_RELOC_XTENSA_TLS_TPOFF");
416 return &elf_howto_table
[(unsigned) R_XTENSA_TLS_TPOFF
];
418 case BFD_RELOC_XTENSA_TLS_FUNC
:
419 TRACE ("BFD_RELOC_XTENSA_TLS_FUNC");
420 return &elf_howto_table
[(unsigned) R_XTENSA_TLS_FUNC
];
422 case BFD_RELOC_XTENSA_TLS_ARG
:
423 TRACE ("BFD_RELOC_XTENSA_TLS_ARG");
424 return &elf_howto_table
[(unsigned) R_XTENSA_TLS_ARG
];
426 case BFD_RELOC_XTENSA_TLS_CALL
:
427 TRACE ("BFD_RELOC_XTENSA_TLS_CALL");
428 return &elf_howto_table
[(unsigned) R_XTENSA_TLS_CALL
];
431 if (code
>= BFD_RELOC_XTENSA_SLOT0_OP
432 && code
<= BFD_RELOC_XTENSA_SLOT14_OP
)
434 unsigned n
= (R_XTENSA_SLOT0_OP
+
435 (code
- BFD_RELOC_XTENSA_SLOT0_OP
));
436 return &elf_howto_table
[n
];
439 if (code
>= BFD_RELOC_XTENSA_SLOT0_ALT
440 && code
<= BFD_RELOC_XTENSA_SLOT14_ALT
)
442 unsigned n
= (R_XTENSA_SLOT0_ALT
+
443 (code
- BFD_RELOC_XTENSA_SLOT0_ALT
));
444 return &elf_howto_table
[n
];
454 static reloc_howto_type
*
455 elf_xtensa_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
460 for (i
= 0; i
< sizeof (elf_howto_table
) / sizeof (elf_howto_table
[0]); i
++)
461 if (elf_howto_table
[i
].name
!= NULL
462 && strcasecmp (elf_howto_table
[i
].name
, r_name
) == 0)
463 return &elf_howto_table
[i
];
469 /* Given an ELF "rela" relocation, find the corresponding howto and record
470 it in the BFD internal arelent representation of the relocation. */
473 elf_xtensa_info_to_howto_rela (bfd
*abfd ATTRIBUTE_UNUSED
,
475 Elf_Internal_Rela
*dst
)
477 unsigned int r_type
= ELF32_R_TYPE (dst
->r_info
);
479 BFD_ASSERT (r_type
< (unsigned int) R_XTENSA_max
);
480 cache_ptr
->howto
= &elf_howto_table
[r_type
];
484 /* Functions for the Xtensa ELF linker. */
486 /* The name of the dynamic interpreter. This is put in the .interp
489 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
491 /* The size in bytes of an entry in the procedure linkage table.
492 (This does _not_ include the space for the literals associated with
495 #define PLT_ENTRY_SIZE 16
497 /* For _really_ large PLTs, we may need to alternate between literals
498 and code to keep the literals within the 256K range of the L32R
499 instructions in the code. It's unlikely that anyone would ever need
500 such a big PLT, but an arbitrary limit on the PLT size would be bad.
501 Thus, we split the PLT into chunks. Since there's very little
502 overhead (2 extra literals) for each chunk, the chunk size is kept
503 small so that the code for handling multiple chunks get used and
504 tested regularly. With 254 entries, there are 1K of literals for
505 each chunk, and that seems like a nice round number. */
507 #define PLT_ENTRIES_PER_CHUNK 254
509 /* PLT entries are actually used as stub functions for lazy symbol
510 resolution. Once the symbol is resolved, the stub function is never
511 invoked. Note: the 32-byte frame size used here cannot be changed
512 without a corresponding change in the runtime linker. */
514 static const bfd_byte elf_xtensa_be_plt_entry
[PLT_ENTRY_SIZE
] =
516 0x6c, 0x10, 0x04, /* entry sp, 32 */
517 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
518 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
519 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
520 0x0a, 0x80, 0x00, /* jx a8 */
524 static const bfd_byte elf_xtensa_le_plt_entry
[PLT_ENTRY_SIZE
] =
526 0x36, 0x41, 0x00, /* entry sp, 32 */
527 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
528 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
529 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
530 0xa0, 0x08, 0x00, /* jx a8 */
534 /* The size of the thread control block. */
537 struct elf_xtensa_link_hash_entry
539 struct elf_link_hash_entry elf
;
541 bfd_signed_vma tlsfunc_refcount
;
543 #define GOT_UNKNOWN 0
545 #define GOT_TLS_GD 2 /* global or local dynamic */
546 #define GOT_TLS_IE 4 /* initial or local exec */
547 #define GOT_TLS_ANY (GOT_TLS_GD | GOT_TLS_IE)
548 unsigned char tls_type
;
551 #define elf_xtensa_hash_entry(ent) ((struct elf_xtensa_link_hash_entry *)(ent))
553 struct elf_xtensa_obj_tdata
555 struct elf_obj_tdata root
;
557 /* tls_type for each local got entry. */
558 char *local_got_tls_type
;
560 bfd_signed_vma
*local_tlsfunc_refcounts
;
563 #define elf_xtensa_tdata(abfd) \
564 ((struct elf_xtensa_obj_tdata *) (abfd)->tdata.any)
566 #define elf_xtensa_local_got_tls_type(abfd) \
567 (elf_xtensa_tdata (abfd)->local_got_tls_type)
569 #define elf_xtensa_local_tlsfunc_refcounts(abfd) \
570 (elf_xtensa_tdata (abfd)->local_tlsfunc_refcounts)
572 #define is_xtensa_elf(bfd) \
573 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
574 && elf_tdata (bfd) != NULL \
575 && elf_object_id (bfd) == XTENSA_ELF_DATA)
578 elf_xtensa_mkobject (bfd
*abfd
)
580 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_xtensa_obj_tdata
),
584 /* Xtensa ELF linker hash table. */
586 struct elf_xtensa_link_hash_table
588 struct elf_link_hash_table elf
;
590 /* Short-cuts to get to dynamic linker sections. */
597 asection
*spltlittbl
;
599 /* Total count of PLT relocations seen during check_relocs.
600 The actual PLT code must be split into multiple sections and all
601 the sections have to be created before size_dynamic_sections,
602 where we figure out the exact number of PLT entries that will be
603 needed. It is OK if this count is an overestimate, e.g., some
604 relocations may be removed by GC. */
607 struct elf_xtensa_link_hash_entry
*tlsbase
;
610 /* Get the Xtensa ELF linker hash table from a link_info structure. */
612 #define elf_xtensa_hash_table(p) \
613 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
614 == XTENSA_ELF_DATA ? ((struct elf_xtensa_link_hash_table *) ((p)->hash)) : NULL)
616 /* Create an entry in an Xtensa ELF linker hash table. */
618 static struct bfd_hash_entry
*
619 elf_xtensa_link_hash_newfunc (struct bfd_hash_entry
*entry
,
620 struct bfd_hash_table
*table
,
623 /* Allocate the structure if it has not already been allocated by a
627 entry
= bfd_hash_allocate (table
,
628 sizeof (struct elf_xtensa_link_hash_entry
));
633 /* Call the allocation method of the superclass. */
634 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
637 struct elf_xtensa_link_hash_entry
*eh
= elf_xtensa_hash_entry (entry
);
638 eh
->tlsfunc_refcount
= 0;
639 eh
->tls_type
= GOT_UNKNOWN
;
645 /* Create an Xtensa ELF linker hash table. */
647 static struct bfd_link_hash_table
*
648 elf_xtensa_link_hash_table_create (bfd
*abfd
)
650 struct elf_link_hash_entry
*tlsbase
;
651 struct elf_xtensa_link_hash_table
*ret
;
652 bfd_size_type amt
= sizeof (struct elf_xtensa_link_hash_table
);
654 ret
= bfd_malloc (amt
);
658 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
,
659 elf_xtensa_link_hash_newfunc
,
660 sizeof (struct elf_xtensa_link_hash_entry
),
673 ret
->spltlittbl
= NULL
;
675 ret
->plt_reloc_count
= 0;
677 /* Create a hash entry for "_TLS_MODULE_BASE_" to speed up checking
679 tlsbase
= elf_link_hash_lookup (&ret
->elf
, "_TLS_MODULE_BASE_",
681 tlsbase
->root
.type
= bfd_link_hash_new
;
682 tlsbase
->root
.u
.undef
.abfd
= NULL
;
683 tlsbase
->non_elf
= 0;
684 ret
->tlsbase
= elf_xtensa_hash_entry (tlsbase
);
685 ret
->tlsbase
->tls_type
= GOT_UNKNOWN
;
687 return &ret
->elf
.root
;
690 /* Copy the extra info we tack onto an elf_link_hash_entry. */
693 elf_xtensa_copy_indirect_symbol (struct bfd_link_info
*info
,
694 struct elf_link_hash_entry
*dir
,
695 struct elf_link_hash_entry
*ind
)
697 struct elf_xtensa_link_hash_entry
*edir
, *eind
;
699 edir
= elf_xtensa_hash_entry (dir
);
700 eind
= elf_xtensa_hash_entry (ind
);
702 if (ind
->root
.type
== bfd_link_hash_indirect
)
704 edir
->tlsfunc_refcount
+= eind
->tlsfunc_refcount
;
705 eind
->tlsfunc_refcount
= 0;
707 if (dir
->got
.refcount
<= 0)
709 edir
->tls_type
= eind
->tls_type
;
710 eind
->tls_type
= GOT_UNKNOWN
;
714 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
717 static inline bfd_boolean
718 elf_xtensa_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
719 struct bfd_link_info
*info
)
721 /* Check if we should do dynamic things to this symbol. The
722 "ignore_protected" argument need not be set, because Xtensa code
723 does not require special handling of STV_PROTECTED to make function
724 pointer comparisons work properly. The PLT addresses are never
725 used for function pointers. */
727 return _bfd_elf_dynamic_symbol_p (h
, info
, 0);
732 property_table_compare (const void *ap
, const void *bp
)
734 const property_table_entry
*a
= (const property_table_entry
*) ap
;
735 const property_table_entry
*b
= (const property_table_entry
*) bp
;
737 if (a
->address
== b
->address
)
739 if (a
->size
!= b
->size
)
740 return (a
->size
- b
->size
);
742 if ((a
->flags
& XTENSA_PROP_ALIGN
) != (b
->flags
& XTENSA_PROP_ALIGN
))
743 return ((b
->flags
& XTENSA_PROP_ALIGN
)
744 - (a
->flags
& XTENSA_PROP_ALIGN
));
746 if ((a
->flags
& XTENSA_PROP_ALIGN
)
747 && (GET_XTENSA_PROP_ALIGNMENT (a
->flags
)
748 != GET_XTENSA_PROP_ALIGNMENT (b
->flags
)))
749 return (GET_XTENSA_PROP_ALIGNMENT (a
->flags
)
750 - GET_XTENSA_PROP_ALIGNMENT (b
->flags
));
752 if ((a
->flags
& XTENSA_PROP_UNREACHABLE
)
753 != (b
->flags
& XTENSA_PROP_UNREACHABLE
))
754 return ((b
->flags
& XTENSA_PROP_UNREACHABLE
)
755 - (a
->flags
& XTENSA_PROP_UNREACHABLE
));
757 return (a
->flags
- b
->flags
);
760 return (a
->address
- b
->address
);
765 property_table_matches (const void *ap
, const void *bp
)
767 const property_table_entry
*a
= (const property_table_entry
*) ap
;
768 const property_table_entry
*b
= (const property_table_entry
*) bp
;
770 /* Check if one entry overlaps with the other. */
771 if ((b
->address
>= a
->address
&& b
->address
< (a
->address
+ a
->size
))
772 || (a
->address
>= b
->address
&& a
->address
< (b
->address
+ b
->size
)))
775 return (a
->address
- b
->address
);
779 /* Get the literal table or property table entries for the given
780 section. Sets TABLE_P and returns the number of entries. On
781 error, returns a negative value. */
784 xtensa_read_table_entries (bfd
*abfd
,
786 property_table_entry
**table_p
,
787 const char *sec_name
,
788 bfd_boolean output_addr
)
790 asection
*table_section
;
791 bfd_size_type table_size
= 0;
792 bfd_byte
*table_data
;
793 property_table_entry
*blocks
;
794 int blk
, block_count
;
795 bfd_size_type num_records
;
796 Elf_Internal_Rela
*internal_relocs
, *irel
, *rel_end
;
797 bfd_vma section_addr
, off
;
798 flagword predef_flags
;
799 bfd_size_type table_entry_size
, section_limit
;
802 || !(section
->flags
& SEC_ALLOC
)
803 || (section
->flags
& SEC_DEBUGGING
))
809 table_section
= xtensa_get_property_section (section
, sec_name
);
811 table_size
= table_section
->size
;
819 predef_flags
= xtensa_get_property_predef_flags (table_section
);
820 table_entry_size
= 12;
822 table_entry_size
-= 4;
824 num_records
= table_size
/ table_entry_size
;
825 table_data
= retrieve_contents (abfd
, table_section
, TRUE
);
826 blocks
= (property_table_entry
*)
827 bfd_malloc (num_records
* sizeof (property_table_entry
));
831 section_addr
= section
->output_section
->vma
+ section
->output_offset
;
833 section_addr
= section
->vma
;
835 internal_relocs
= retrieve_internal_relocs (abfd
, table_section
, TRUE
);
836 if (internal_relocs
&& !table_section
->reloc_done
)
838 qsort (internal_relocs
, table_section
->reloc_count
,
839 sizeof (Elf_Internal_Rela
), internal_reloc_compare
);
840 irel
= internal_relocs
;
845 section_limit
= bfd_get_section_limit (abfd
, section
);
846 rel_end
= internal_relocs
+ table_section
->reloc_count
;
848 for (off
= 0; off
< table_size
; off
+= table_entry_size
)
850 bfd_vma address
= bfd_get_32 (abfd
, table_data
+ off
);
852 /* Skip any relocations before the current offset. This should help
853 avoid confusion caused by unexpected relocations for the preceding
856 (irel
->r_offset
< off
857 || (irel
->r_offset
== off
858 && ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_NONE
)))
865 if (irel
&& irel
->r_offset
== off
)
868 unsigned long r_symndx
= ELF32_R_SYM (irel
->r_info
);
869 BFD_ASSERT (ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_32
);
871 if (get_elf_r_symndx_section (abfd
, r_symndx
) != section
)
874 sym_off
= get_elf_r_symndx_offset (abfd
, r_symndx
);
875 BFD_ASSERT (sym_off
== 0);
876 address
+= (section_addr
+ sym_off
+ irel
->r_addend
);
880 if (address
< section_addr
881 || address
>= section_addr
+ section_limit
)
885 blocks
[block_count
].address
= address
;
886 blocks
[block_count
].size
= bfd_get_32 (abfd
, table_data
+ off
+ 4);
888 blocks
[block_count
].flags
= predef_flags
;
890 blocks
[block_count
].flags
= bfd_get_32 (abfd
, table_data
+ off
+ 8);
894 release_contents (table_section
, table_data
);
895 release_internal_relocs (table_section
, internal_relocs
);
899 /* Now sort them into address order for easy reference. */
900 qsort (blocks
, block_count
, sizeof (property_table_entry
),
901 property_table_compare
);
903 /* Check that the table contents are valid. Problems may occur,
904 for example, if an unrelocated object file is stripped. */
905 for (blk
= 1; blk
< block_count
; blk
++)
907 /* The only circumstance where two entries may legitimately
908 have the same address is when one of them is a zero-size
909 placeholder to mark a place where fill can be inserted.
910 The zero-size entry should come first. */
911 if (blocks
[blk
- 1].address
== blocks
[blk
].address
&&
912 blocks
[blk
- 1].size
!= 0)
914 (*_bfd_error_handler
) (_("%B(%A): invalid property table"),
916 bfd_set_error (bfd_error_bad_value
);
928 static property_table_entry
*
929 elf_xtensa_find_property_entry (property_table_entry
*property_table
,
930 int property_table_size
,
933 property_table_entry entry
;
934 property_table_entry
*rv
;
936 if (property_table_size
== 0)
939 entry
.address
= addr
;
943 rv
= bsearch (&entry
, property_table
, property_table_size
,
944 sizeof (property_table_entry
), property_table_matches
);
950 elf_xtensa_in_literal_pool (property_table_entry
*lit_table
,
954 if (elf_xtensa_find_property_entry (lit_table
, lit_table_size
, addr
))
961 /* Look through the relocs for a section during the first phase, and
962 calculate needed space in the dynamic reloc sections. */
965 elf_xtensa_check_relocs (bfd
*abfd
,
966 struct bfd_link_info
*info
,
968 const Elf_Internal_Rela
*relocs
)
970 struct elf_xtensa_link_hash_table
*htab
;
971 Elf_Internal_Shdr
*symtab_hdr
;
972 struct elf_link_hash_entry
**sym_hashes
;
973 const Elf_Internal_Rela
*rel
;
974 const Elf_Internal_Rela
*rel_end
;
976 if (info
->relocatable
|| (sec
->flags
& SEC_ALLOC
) == 0)
979 BFD_ASSERT (is_xtensa_elf (abfd
));
981 htab
= elf_xtensa_hash_table (info
);
985 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
986 sym_hashes
= elf_sym_hashes (abfd
);
988 rel_end
= relocs
+ sec
->reloc_count
;
989 for (rel
= relocs
; rel
< rel_end
; rel
++)
992 unsigned long r_symndx
;
993 struct elf_link_hash_entry
*h
= NULL
;
994 struct elf_xtensa_link_hash_entry
*eh
;
995 int tls_type
, old_tls_type
;
996 bfd_boolean is_got
= FALSE
;
997 bfd_boolean is_plt
= FALSE
;
998 bfd_boolean is_tlsfunc
= FALSE
;
1000 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1001 r_type
= ELF32_R_TYPE (rel
->r_info
);
1003 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
1005 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
1010 if (r_symndx
>= symtab_hdr
->sh_info
)
1012 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1013 while (h
->root
.type
== bfd_link_hash_indirect
1014 || h
->root
.type
== bfd_link_hash_warning
)
1015 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1017 eh
= elf_xtensa_hash_entry (h
);
1021 case R_XTENSA_TLSDESC_FN
:
1024 tls_type
= GOT_TLS_GD
;
1029 tls_type
= GOT_TLS_IE
;
1032 case R_XTENSA_TLSDESC_ARG
:
1035 tls_type
= GOT_TLS_GD
;
1040 tls_type
= GOT_TLS_IE
;
1041 if (h
&& elf_xtensa_hash_entry (h
) != htab
->tlsbase
)
1046 case R_XTENSA_TLS_DTPOFF
:
1048 tls_type
= GOT_TLS_GD
;
1050 tls_type
= GOT_TLS_IE
;
1053 case R_XTENSA_TLS_TPOFF
:
1054 tls_type
= GOT_TLS_IE
;
1056 info
->flags
|= DF_STATIC_TLS
;
1057 if (info
->shared
|| h
)
1062 tls_type
= GOT_NORMAL
;
1067 tls_type
= GOT_NORMAL
;
1071 case R_XTENSA_GNU_VTINHERIT
:
1072 /* This relocation describes the C++ object vtable hierarchy.
1073 Reconstruct it for later use during GC. */
1074 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1078 case R_XTENSA_GNU_VTENTRY
:
1079 /* This relocation describes which C++ vtable entries are actually
1080 used. Record for later use during GC. */
1081 BFD_ASSERT (h
!= NULL
);
1083 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1088 /* Nothing to do for any other relocations. */
1096 if (h
->plt
.refcount
<= 0)
1099 h
->plt
.refcount
= 1;
1102 h
->plt
.refcount
+= 1;
1104 /* Keep track of the total PLT relocation count even if we
1105 don't yet know whether the dynamic sections will be
1107 htab
->plt_reloc_count
+= 1;
1109 if (elf_hash_table (info
)->dynamic_sections_created
)
1111 if (! add_extra_plt_sections (info
, htab
->plt_reloc_count
))
1117 if (h
->got
.refcount
<= 0)
1118 h
->got
.refcount
= 1;
1120 h
->got
.refcount
+= 1;
1124 eh
->tlsfunc_refcount
+= 1;
1126 old_tls_type
= eh
->tls_type
;
1130 /* Allocate storage the first time. */
1131 if (elf_local_got_refcounts (abfd
) == NULL
)
1133 bfd_size_type size
= symtab_hdr
->sh_info
;
1136 mem
= bfd_zalloc (abfd
, size
* sizeof (bfd_signed_vma
));
1139 elf_local_got_refcounts (abfd
) = (bfd_signed_vma
*) mem
;
1141 mem
= bfd_zalloc (abfd
, size
);
1144 elf_xtensa_local_got_tls_type (abfd
) = (char *) mem
;
1146 mem
= bfd_zalloc (abfd
, size
* sizeof (bfd_signed_vma
));
1149 elf_xtensa_local_tlsfunc_refcounts (abfd
)
1150 = (bfd_signed_vma
*) mem
;
1153 /* This is a global offset table entry for a local symbol. */
1154 if (is_got
|| is_plt
)
1155 elf_local_got_refcounts (abfd
) [r_symndx
] += 1;
1158 elf_xtensa_local_tlsfunc_refcounts (abfd
) [r_symndx
] += 1;
1160 old_tls_type
= elf_xtensa_local_got_tls_type (abfd
) [r_symndx
];
1163 if ((old_tls_type
& GOT_TLS_IE
) && (tls_type
& GOT_TLS_IE
))
1164 tls_type
|= old_tls_type
;
1165 /* If a TLS symbol is accessed using IE at least once,
1166 there is no point to use a dynamic model for it. */
1167 else if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
1168 && ((old_tls_type
& GOT_TLS_GD
) == 0
1169 || (tls_type
& GOT_TLS_IE
) == 0))
1171 if ((old_tls_type
& GOT_TLS_IE
) && (tls_type
& GOT_TLS_GD
))
1172 tls_type
= old_tls_type
;
1173 else if ((old_tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_GD
))
1174 tls_type
|= old_tls_type
;
1177 (*_bfd_error_handler
)
1178 (_("%B: `%s' accessed both as normal and thread local symbol"),
1180 h
? h
->root
.root
.string
: "<local>");
1185 if (old_tls_type
!= tls_type
)
1188 eh
->tls_type
= tls_type
;
1190 elf_xtensa_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
1199 elf_xtensa_make_sym_local (struct bfd_link_info
*info
,
1200 struct elf_link_hash_entry
*h
)
1204 if (h
->plt
.refcount
> 0)
1206 /* For shared objects, there's no need for PLT entries for local
1207 symbols (use RELATIVE relocs instead of JMP_SLOT relocs). */
1208 if (h
->got
.refcount
< 0)
1209 h
->got
.refcount
= 0;
1210 h
->got
.refcount
+= h
->plt
.refcount
;
1211 h
->plt
.refcount
= 0;
1216 /* Don't need any dynamic relocations at all. */
1217 h
->plt
.refcount
= 0;
1218 h
->got
.refcount
= 0;
1224 elf_xtensa_hide_symbol (struct bfd_link_info
*info
,
1225 struct elf_link_hash_entry
*h
,
1226 bfd_boolean force_local
)
1228 /* For a shared link, move the plt refcount to the got refcount to leave
1229 space for RELATIVE relocs. */
1230 elf_xtensa_make_sym_local (info
, h
);
1232 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
);
1236 /* Return the section that should be marked against GC for a given
1240 elf_xtensa_gc_mark_hook (asection
*sec
,
1241 struct bfd_link_info
*info
,
1242 Elf_Internal_Rela
*rel
,
1243 struct elf_link_hash_entry
*h
,
1244 Elf_Internal_Sym
*sym
)
1246 /* Property sections are marked "KEEP" in the linker scripts, but they
1247 should not cause other sections to be marked. (This approach relies
1248 on elf_xtensa_discard_info to remove property table entries that
1249 describe discarded sections. Alternatively, it might be more
1250 efficient to avoid using "KEEP" in the linker scripts and instead use
1251 the gc_mark_extra_sections hook to mark only the property sections
1252 that describe marked sections. That alternative does not work well
1253 with the current property table sections, which do not correspond
1254 one-to-one with the sections they describe, but that should be fixed
1256 if (xtensa_is_property_section (sec
))
1260 switch (ELF32_R_TYPE (rel
->r_info
))
1262 case R_XTENSA_GNU_VTINHERIT
:
1263 case R_XTENSA_GNU_VTENTRY
:
1267 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
1271 /* Update the GOT & PLT entry reference counts
1272 for the section being removed. */
1275 elf_xtensa_gc_sweep_hook (bfd
*abfd
,
1276 struct bfd_link_info
*info
,
1278 const Elf_Internal_Rela
*relocs
)
1280 Elf_Internal_Shdr
*symtab_hdr
;
1281 struct elf_link_hash_entry
**sym_hashes
;
1282 const Elf_Internal_Rela
*rel
, *relend
;
1283 struct elf_xtensa_link_hash_table
*htab
;
1285 htab
= elf_xtensa_hash_table (info
);
1289 if (info
->relocatable
)
1292 if ((sec
->flags
& SEC_ALLOC
) == 0)
1295 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1296 sym_hashes
= elf_sym_hashes (abfd
);
1298 relend
= relocs
+ sec
->reloc_count
;
1299 for (rel
= relocs
; rel
< relend
; rel
++)
1301 unsigned long r_symndx
;
1302 unsigned int r_type
;
1303 struct elf_link_hash_entry
*h
= NULL
;
1304 struct elf_xtensa_link_hash_entry
*eh
;
1305 bfd_boolean is_got
= FALSE
;
1306 bfd_boolean is_plt
= FALSE
;
1307 bfd_boolean is_tlsfunc
= FALSE
;
1309 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1310 if (r_symndx
>= symtab_hdr
->sh_info
)
1312 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1313 while (h
->root
.type
== bfd_link_hash_indirect
1314 || h
->root
.type
== bfd_link_hash_warning
)
1315 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1317 eh
= elf_xtensa_hash_entry (h
);
1319 r_type
= ELF32_R_TYPE (rel
->r_info
);
1322 case R_XTENSA_TLSDESC_FN
:
1330 case R_XTENSA_TLSDESC_ARG
:
1335 if (h
&& elf_xtensa_hash_entry (h
) != htab
->tlsbase
)
1340 case R_XTENSA_TLS_TPOFF
:
1341 if (info
->shared
|| h
)
1361 if (h
->plt
.refcount
> 0)
1366 if (h
->got
.refcount
> 0)
1371 if (eh
->tlsfunc_refcount
> 0)
1372 eh
->tlsfunc_refcount
--;
1377 if (is_got
|| is_plt
)
1379 bfd_signed_vma
*got_refcount
1380 = &elf_local_got_refcounts (abfd
) [r_symndx
];
1381 if (*got_refcount
> 0)
1386 bfd_signed_vma
*tlsfunc_refcount
1387 = &elf_xtensa_local_tlsfunc_refcounts (abfd
) [r_symndx
];
1388 if (*tlsfunc_refcount
> 0)
1389 *tlsfunc_refcount
-= 1;
1398 /* Create all the dynamic sections. */
1401 elf_xtensa_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
1403 struct elf_xtensa_link_hash_table
*htab
;
1404 flagword flags
, noalloc_flags
;
1406 htab
= elf_xtensa_hash_table (info
);
1410 /* First do all the standard stuff. */
1411 if (! _bfd_elf_create_dynamic_sections (dynobj
, info
))
1413 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
1414 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
1415 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
1416 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
1417 htab
->srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
1419 /* Create any extra PLT sections in case check_relocs has already
1420 been called on all the non-dynamic input files. */
1421 if (! add_extra_plt_sections (info
, htab
->plt_reloc_count
))
1424 noalloc_flags
= (SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1425 | SEC_LINKER_CREATED
| SEC_READONLY
);
1426 flags
= noalloc_flags
| SEC_ALLOC
| SEC_LOAD
;
1428 /* Mark the ".got.plt" section READONLY. */
1429 if (htab
->sgotplt
== NULL
1430 || ! bfd_set_section_flags (dynobj
, htab
->sgotplt
, flags
))
1433 /* Create ".got.loc" (literal tables for use by dynamic linker). */
1434 htab
->sgotloc
= bfd_make_section_with_flags (dynobj
, ".got.loc", flags
);
1435 if (htab
->sgotloc
== NULL
1436 || ! bfd_set_section_alignment (dynobj
, htab
->sgotloc
, 2))
1439 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
1440 htab
->spltlittbl
= bfd_make_section_with_flags (dynobj
, ".xt.lit.plt",
1442 if (htab
->spltlittbl
== NULL
1443 || ! bfd_set_section_alignment (dynobj
, htab
->spltlittbl
, 2))
1451 add_extra_plt_sections (struct bfd_link_info
*info
, int count
)
1453 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
1456 /* Iterate over all chunks except 0 which uses the standard ".plt" and
1457 ".got.plt" sections. */
1458 for (chunk
= count
/ PLT_ENTRIES_PER_CHUNK
; chunk
> 0; chunk
--)
1464 /* Stop when we find a section has already been created. */
1465 if (elf_xtensa_get_plt_section (info
, chunk
))
1468 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1469 | SEC_LINKER_CREATED
| SEC_READONLY
);
1471 sname
= (char *) bfd_malloc (10);
1472 sprintf (sname
, ".plt.%u", chunk
);
1473 s
= bfd_make_section_with_flags (dynobj
, sname
, flags
| SEC_CODE
);
1475 || ! bfd_set_section_alignment (dynobj
, s
, 2))
1478 sname
= (char *) bfd_malloc (14);
1479 sprintf (sname
, ".got.plt.%u", chunk
);
1480 s
= bfd_make_section_with_flags (dynobj
, sname
, flags
);
1482 || ! bfd_set_section_alignment (dynobj
, s
, 2))
1490 /* Adjust a symbol defined by a dynamic object and referenced by a
1491 regular object. The current definition is in some section of the
1492 dynamic object, but we're not including those sections. We have to
1493 change the definition to something the rest of the link can
1497 elf_xtensa_adjust_dynamic_symbol (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1498 struct elf_link_hash_entry
*h
)
1500 /* If this is a weak symbol, and there is a real definition, the
1501 processor independent code will have arranged for us to see the
1502 real definition first, and we can just use the same value. */
1505 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
1506 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
1507 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
1508 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
1512 /* This is a reference to a symbol defined by a dynamic object. The
1513 reference must go through the GOT, so there's no need for COPY relocs,
1521 elf_xtensa_allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *arg
)
1523 struct bfd_link_info
*info
;
1524 struct elf_xtensa_link_hash_table
*htab
;
1525 struct elf_xtensa_link_hash_entry
*eh
= elf_xtensa_hash_entry (h
);
1527 if (h
->root
.type
== bfd_link_hash_indirect
)
1530 if (h
->root
.type
== bfd_link_hash_warning
)
1531 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1533 info
= (struct bfd_link_info
*) arg
;
1534 htab
= elf_xtensa_hash_table (info
);
1538 /* If we saw any use of an IE model for this symbol, we can then optimize
1539 away GOT entries for any TLSDESC_FN relocs. */
1540 if ((eh
->tls_type
& GOT_TLS_IE
) != 0)
1542 BFD_ASSERT (h
->got
.refcount
>= eh
->tlsfunc_refcount
);
1543 h
->got
.refcount
-= eh
->tlsfunc_refcount
;
1546 if (! elf_xtensa_dynamic_symbol_p (h
, info
))
1547 elf_xtensa_make_sym_local (info
, h
);
1549 if (h
->plt
.refcount
> 0)
1550 htab
->srelplt
->size
+= (h
->plt
.refcount
* sizeof (Elf32_External_Rela
));
1552 if (h
->got
.refcount
> 0)
1553 htab
->srelgot
->size
+= (h
->got
.refcount
* sizeof (Elf32_External_Rela
));
1560 elf_xtensa_allocate_local_got_size (struct bfd_link_info
*info
)
1562 struct elf_xtensa_link_hash_table
*htab
;
1565 htab
= elf_xtensa_hash_table (info
);
1569 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
1571 bfd_signed_vma
*local_got_refcounts
;
1572 bfd_size_type j
, cnt
;
1573 Elf_Internal_Shdr
*symtab_hdr
;
1575 local_got_refcounts
= elf_local_got_refcounts (i
);
1576 if (!local_got_refcounts
)
1579 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
1580 cnt
= symtab_hdr
->sh_info
;
1582 for (j
= 0; j
< cnt
; ++j
)
1584 /* If we saw any use of an IE model for this symbol, we can
1585 then optimize away GOT entries for any TLSDESC_FN relocs. */
1586 if ((elf_xtensa_local_got_tls_type (i
) [j
] & GOT_TLS_IE
) != 0)
1588 bfd_signed_vma
*tlsfunc_refcount
1589 = &elf_xtensa_local_tlsfunc_refcounts (i
) [j
];
1590 BFD_ASSERT (local_got_refcounts
[j
] >= *tlsfunc_refcount
);
1591 local_got_refcounts
[j
] -= *tlsfunc_refcount
;
1594 if (local_got_refcounts
[j
] > 0)
1595 htab
->srelgot
->size
+= (local_got_refcounts
[j
]
1596 * sizeof (Elf32_External_Rela
));
1602 /* Set the sizes of the dynamic sections. */
1605 elf_xtensa_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1606 struct bfd_link_info
*info
)
1608 struct elf_xtensa_link_hash_table
*htab
;
1610 asection
*s
, *srelplt
, *splt
, *sgotplt
, *srelgot
, *spltlittbl
, *sgotloc
;
1611 bfd_boolean relplt
, relgot
;
1612 int plt_entries
, plt_chunks
, chunk
;
1617 htab
= elf_xtensa_hash_table (info
);
1621 dynobj
= elf_hash_table (info
)->dynobj
;
1624 srelgot
= htab
->srelgot
;
1625 srelplt
= htab
->srelplt
;
1627 if (elf_hash_table (info
)->dynamic_sections_created
)
1629 BFD_ASSERT (htab
->srelgot
!= NULL
1630 && htab
->srelplt
!= NULL
1631 && htab
->sgot
!= NULL
1632 && htab
->spltlittbl
!= NULL
1633 && htab
->sgotloc
!= NULL
);
1635 /* Set the contents of the .interp section to the interpreter. */
1636 if (info
->executable
)
1638 s
= bfd_get_section_by_name (dynobj
, ".interp");
1641 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1642 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1645 /* Allocate room for one word in ".got". */
1646 htab
->sgot
->size
= 4;
1648 /* Allocate space in ".rela.got" for literals that reference global
1649 symbols and space in ".rela.plt" for literals that have PLT
1651 elf_link_hash_traverse (elf_hash_table (info
),
1652 elf_xtensa_allocate_dynrelocs
,
1655 /* If we are generating a shared object, we also need space in
1656 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1657 reference local symbols. */
1659 elf_xtensa_allocate_local_got_size (info
);
1661 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1662 each PLT entry, we need the PLT code plus a 4-byte literal.
1663 For each chunk of ".plt", we also need two more 4-byte
1664 literals, two corresponding entries in ".rela.got", and an
1665 8-byte entry in ".xt.lit.plt". */
1666 spltlittbl
= htab
->spltlittbl
;
1667 plt_entries
= srelplt
->size
/ sizeof (Elf32_External_Rela
);
1669 (plt_entries
+ PLT_ENTRIES_PER_CHUNK
- 1) / PLT_ENTRIES_PER_CHUNK
;
1671 /* Iterate over all the PLT chunks, including any extra sections
1672 created earlier because the initial count of PLT relocations
1673 was an overestimate. */
1675 (splt
= elf_xtensa_get_plt_section (info
, chunk
)) != NULL
;
1680 sgotplt
= elf_xtensa_get_gotplt_section (info
, chunk
);
1681 BFD_ASSERT (sgotplt
!= NULL
);
1683 if (chunk
< plt_chunks
- 1)
1684 chunk_entries
= PLT_ENTRIES_PER_CHUNK
;
1685 else if (chunk
== plt_chunks
- 1)
1686 chunk_entries
= plt_entries
- (chunk
* PLT_ENTRIES_PER_CHUNK
);
1690 if (chunk_entries
!= 0)
1692 sgotplt
->size
= 4 * (chunk_entries
+ 2);
1693 splt
->size
= PLT_ENTRY_SIZE
* chunk_entries
;
1694 srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
1695 spltlittbl
->size
+= 8;
1704 /* Allocate space in ".got.loc" to match the total size of all the
1706 sgotloc
= htab
->sgotloc
;
1707 sgotloc
->size
= spltlittbl
->size
;
1708 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
1710 if (abfd
->flags
& DYNAMIC
)
1712 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1714 if (! elf_discarded_section (s
)
1715 && xtensa_is_littable_section (s
)
1717 sgotloc
->size
+= s
->size
;
1722 /* Allocate memory for dynamic sections. */
1725 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1729 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1732 /* It's OK to base decisions on the section name, because none
1733 of the dynobj section names depend upon the input files. */
1734 name
= bfd_get_section_name (dynobj
, s
);
1736 if (CONST_STRNEQ (name
, ".rela"))
1740 if (strcmp (name
, ".rela.plt") == 0)
1742 else if (strcmp (name
, ".rela.got") == 0)
1745 /* We use the reloc_count field as a counter if we need
1746 to copy relocs into the output file. */
1750 else if (! CONST_STRNEQ (name
, ".plt.")
1751 && ! CONST_STRNEQ (name
, ".got.plt.")
1752 && strcmp (name
, ".got") != 0
1753 && strcmp (name
, ".plt") != 0
1754 && strcmp (name
, ".got.plt") != 0
1755 && strcmp (name
, ".xt.lit.plt") != 0
1756 && strcmp (name
, ".got.loc") != 0)
1758 /* It's not one of our sections, so don't allocate space. */
1764 /* If we don't need this section, strip it from the output
1765 file. We must create the ".plt*" and ".got.plt*"
1766 sections in create_dynamic_sections and/or check_relocs
1767 based on a conservative estimate of the PLT relocation
1768 count, because the sections must be created before the
1769 linker maps input sections to output sections. The
1770 linker does that before size_dynamic_sections, where we
1771 compute the exact size of the PLT, so there may be more
1772 of these sections than are actually needed. */
1773 s
->flags
|= SEC_EXCLUDE
;
1775 else if ((s
->flags
& SEC_HAS_CONTENTS
) != 0)
1777 /* Allocate memory for the section contents. */
1778 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1779 if (s
->contents
== NULL
)
1784 if (elf_hash_table (info
)->dynamic_sections_created
)
1786 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1787 known until finish_dynamic_sections, but we need to get the relocs
1788 in place before they are sorted. */
1789 for (chunk
= 0; chunk
< plt_chunks
; chunk
++)
1791 Elf_Internal_Rela irela
;
1795 irela
.r_info
= ELF32_R_INFO (0, R_XTENSA_RTLD
);
1798 loc
= (srelgot
->contents
1799 + srelgot
->reloc_count
* sizeof (Elf32_External_Rela
));
1800 bfd_elf32_swap_reloca_out (output_bfd
, &irela
, loc
);
1801 bfd_elf32_swap_reloca_out (output_bfd
, &irela
,
1802 loc
+ sizeof (Elf32_External_Rela
));
1803 srelgot
->reloc_count
+= 2;
1806 /* Add some entries to the .dynamic section. We fill in the
1807 values later, in elf_xtensa_finish_dynamic_sections, but we
1808 must add the entries now so that we get the correct size for
1809 the .dynamic section. The DT_DEBUG entry is filled in by the
1810 dynamic linker and used by the debugger. */
1811 #define add_dynamic_entry(TAG, VAL) \
1812 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1814 if (info
->executable
)
1816 if (!add_dynamic_entry (DT_DEBUG
, 0))
1822 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
1823 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1824 || !add_dynamic_entry (DT_JMPREL
, 0))
1830 if (!add_dynamic_entry (DT_RELA
, 0)
1831 || !add_dynamic_entry (DT_RELASZ
, 0)
1832 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
)))
1836 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1837 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF
, 0)
1838 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ
, 0))
1841 #undef add_dynamic_entry
1847 elf_xtensa_always_size_sections (bfd
*output_bfd
,
1848 struct bfd_link_info
*info
)
1850 struct elf_xtensa_link_hash_table
*htab
;
1853 htab
= elf_xtensa_hash_table (info
);
1857 tls_sec
= htab
->elf
.tls_sec
;
1859 if (tls_sec
&& (htab
->tlsbase
->tls_type
& GOT_TLS_ANY
) != 0)
1861 struct elf_link_hash_entry
*tlsbase
= &htab
->tlsbase
->elf
;
1862 struct bfd_link_hash_entry
*bh
= &tlsbase
->root
;
1863 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
1865 tlsbase
->type
= STT_TLS
;
1866 if (!(_bfd_generic_link_add_one_symbol
1867 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
1868 tls_sec
, 0, NULL
, FALSE
,
1869 bed
->collect
, &bh
)))
1871 tlsbase
->def_regular
= 1;
1872 tlsbase
->other
= STV_HIDDEN
;
1873 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
1880 /* Return the base VMA address which should be subtracted from real addresses
1881 when resolving @dtpoff relocation.
1882 This is PT_TLS segment p_vaddr. */
1885 dtpoff_base (struct bfd_link_info
*info
)
1887 /* If tls_sec is NULL, we should have signalled an error already. */
1888 if (elf_hash_table (info
)->tls_sec
== NULL
)
1890 return elf_hash_table (info
)->tls_sec
->vma
;
1893 /* Return the relocation value for @tpoff relocation
1894 if STT_TLS virtual address is ADDRESS. */
1897 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1899 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1902 /* If tls_sec is NULL, we should have signalled an error already. */
1903 if (htab
->tls_sec
== NULL
)
1905 base
= align_power ((bfd_vma
) TCB_SIZE
, htab
->tls_sec
->alignment_power
);
1906 return address
- htab
->tls_sec
->vma
+ base
;
1909 /* Perform the specified relocation. The instruction at (contents + address)
1910 is modified to set one operand to represent the value in "relocation". The
1911 operand position is determined by the relocation type recorded in the
1914 #define CALL_SEGMENT_BITS (30)
1915 #define CALL_SEGMENT_SIZE (1 << CALL_SEGMENT_BITS)
1917 static bfd_reloc_status_type
1918 elf_xtensa_do_reloc (reloc_howto_type
*howto
,
1920 asection
*input_section
,
1924 bfd_boolean is_weak_undef
,
1925 char **error_message
)
1928 xtensa_opcode opcode
;
1929 xtensa_isa isa
= xtensa_default_isa
;
1930 static xtensa_insnbuf ibuff
= NULL
;
1931 static xtensa_insnbuf sbuff
= NULL
;
1932 bfd_vma self_address
;
1933 bfd_size_type input_size
;
1939 ibuff
= xtensa_insnbuf_alloc (isa
);
1940 sbuff
= xtensa_insnbuf_alloc (isa
);
1943 input_size
= bfd_get_section_limit (abfd
, input_section
);
1945 /* Calculate the PC address for this instruction. */
1946 self_address
= (input_section
->output_section
->vma
1947 + input_section
->output_offset
1950 switch (howto
->type
)
1953 case R_XTENSA_DIFF8
:
1954 case R_XTENSA_DIFF16
:
1955 case R_XTENSA_DIFF32
:
1956 case R_XTENSA_TLS_FUNC
:
1957 case R_XTENSA_TLS_ARG
:
1958 case R_XTENSA_TLS_CALL
:
1959 return bfd_reloc_ok
;
1961 case R_XTENSA_ASM_EXPAND
:
1964 /* Check for windowed CALL across a 1GB boundary. */
1965 opcode
= get_expanded_call_opcode (contents
+ address
,
1966 input_size
- address
, 0);
1967 if (is_windowed_call_opcode (opcode
))
1969 if ((self_address
>> CALL_SEGMENT_BITS
)
1970 != (relocation
>> CALL_SEGMENT_BITS
))
1972 *error_message
= "windowed longcall crosses 1GB boundary; "
1974 return bfd_reloc_dangerous
;
1978 return bfd_reloc_ok
;
1980 case R_XTENSA_ASM_SIMPLIFY
:
1982 /* Convert the L32R/CALLX to CALL. */
1983 bfd_reloc_status_type retval
=
1984 elf_xtensa_do_asm_simplify (contents
, address
, input_size
,
1986 if (retval
!= bfd_reloc_ok
)
1987 return bfd_reloc_dangerous
;
1989 /* The CALL needs to be relocated. Continue below for that part. */
1992 howto
= &elf_howto_table
[(unsigned) R_XTENSA_SLOT0_OP
];
1999 x
= bfd_get_32 (abfd
, contents
+ address
);
2001 bfd_put_32 (abfd
, x
, contents
+ address
);
2003 return bfd_reloc_ok
;
2005 case R_XTENSA_32_PCREL
:
2006 bfd_put_32 (abfd
, relocation
- self_address
, contents
+ address
);
2007 return bfd_reloc_ok
;
2010 case R_XTENSA_TLSDESC_FN
:
2011 case R_XTENSA_TLSDESC_ARG
:
2012 case R_XTENSA_TLS_DTPOFF
:
2013 case R_XTENSA_TLS_TPOFF
:
2014 bfd_put_32 (abfd
, relocation
, contents
+ address
);
2015 return bfd_reloc_ok
;
2018 /* Only instruction slot-specific relocations handled below.... */
2019 slot
= get_relocation_slot (howto
->type
);
2020 if (slot
== XTENSA_UNDEFINED
)
2022 *error_message
= "unexpected relocation";
2023 return bfd_reloc_dangerous
;
2026 /* Read the instruction into a buffer and decode the opcode. */
2027 xtensa_insnbuf_from_chars (isa
, ibuff
, contents
+ address
,
2028 input_size
- address
);
2029 fmt
= xtensa_format_decode (isa
, ibuff
);
2030 if (fmt
== XTENSA_UNDEFINED
)
2032 *error_message
= "cannot decode instruction format";
2033 return bfd_reloc_dangerous
;
2036 xtensa_format_get_slot (isa
, fmt
, slot
, ibuff
, sbuff
);
2038 opcode
= xtensa_opcode_decode (isa
, fmt
, slot
, sbuff
);
2039 if (opcode
== XTENSA_UNDEFINED
)
2041 *error_message
= "cannot decode instruction opcode";
2042 return bfd_reloc_dangerous
;
2045 /* Check for opcode-specific "alternate" relocations. */
2046 if (is_alt_relocation (howto
->type
))
2048 if (opcode
== get_l32r_opcode ())
2050 /* Handle the special-case of non-PC-relative L32R instructions. */
2051 bfd
*output_bfd
= input_section
->output_section
->owner
;
2052 asection
*lit4_sec
= bfd_get_section_by_name (output_bfd
, ".lit4");
2055 *error_message
= "relocation references missing .lit4 section";
2056 return bfd_reloc_dangerous
;
2058 self_address
= ((lit4_sec
->vma
& ~0xfff)
2059 + 0x40000 - 3); /* -3 to compensate for do_reloc */
2060 newval
= relocation
;
2063 else if (opcode
== get_const16_opcode ())
2065 /* ALT used for high 16 bits. */
2066 newval
= relocation
>> 16;
2071 /* No other "alternate" relocations currently defined. */
2072 *error_message
= "unexpected relocation";
2073 return bfd_reloc_dangerous
;
2076 else /* Not an "alternate" relocation.... */
2078 if (opcode
== get_const16_opcode ())
2080 newval
= relocation
& 0xffff;
2085 /* ...normal PC-relative relocation.... */
2087 /* Determine which operand is being relocated. */
2088 opnd
= get_relocation_opnd (opcode
, howto
->type
);
2089 if (opnd
== XTENSA_UNDEFINED
)
2091 *error_message
= "unexpected relocation";
2092 return bfd_reloc_dangerous
;
2095 if (!howto
->pc_relative
)
2097 *error_message
= "expected PC-relative relocation";
2098 return bfd_reloc_dangerous
;
2101 newval
= relocation
;
2105 /* Apply the relocation. */
2106 if (xtensa_operand_do_reloc (isa
, opcode
, opnd
, &newval
, self_address
)
2107 || xtensa_operand_encode (isa
, opcode
, opnd
, &newval
)
2108 || xtensa_operand_set_field (isa
, opcode
, opnd
, fmt
, slot
,
2111 const char *opname
= xtensa_opcode_name (isa
, opcode
);
2114 msg
= "cannot encode";
2115 if (is_direct_call_opcode (opcode
))
2117 if ((relocation
& 0x3) != 0)
2118 msg
= "misaligned call target";
2120 msg
= "call target out of range";
2122 else if (opcode
== get_l32r_opcode ())
2124 if ((relocation
& 0x3) != 0)
2125 msg
= "misaligned literal target";
2126 else if (is_alt_relocation (howto
->type
))
2127 msg
= "literal target out of range (too many literals)";
2128 else if (self_address
> relocation
)
2129 msg
= "literal target out of range (try using text-section-literals)";
2131 msg
= "literal placed after use";
2134 *error_message
= vsprint_msg (opname
, ": %s", strlen (msg
) + 2, msg
);
2135 return bfd_reloc_dangerous
;
2138 /* Check for calls across 1GB boundaries. */
2139 if (is_direct_call_opcode (opcode
)
2140 && is_windowed_call_opcode (opcode
))
2142 if ((self_address
>> CALL_SEGMENT_BITS
)
2143 != (relocation
>> CALL_SEGMENT_BITS
))
2146 "windowed call crosses 1GB boundary; return may fail";
2147 return bfd_reloc_dangerous
;
2151 /* Write the modified instruction back out of the buffer. */
2152 xtensa_format_set_slot (isa
, fmt
, slot
, ibuff
, sbuff
);
2153 xtensa_insnbuf_to_chars (isa
, ibuff
, contents
+ address
,
2154 input_size
- address
);
2155 return bfd_reloc_ok
;
2160 vsprint_msg (const char *origmsg
, const char *fmt
, int arglen
, ...)
2162 /* To reduce the size of the memory leak,
2163 we only use a single message buffer. */
2164 static bfd_size_type alloc_size
= 0;
2165 static char *message
= NULL
;
2166 bfd_size_type orig_len
, len
= 0;
2167 bfd_boolean is_append
;
2169 VA_OPEN (ap
, arglen
);
2170 VA_FIXEDARG (ap
, const char *, origmsg
);
2172 is_append
= (origmsg
== message
);
2174 orig_len
= strlen (origmsg
);
2175 len
= orig_len
+ strlen (fmt
) + arglen
+ 20;
2176 if (len
> alloc_size
)
2178 message
= (char *) bfd_realloc_or_free (message
, len
);
2181 if (message
!= NULL
)
2184 memcpy (message
, origmsg
, orig_len
);
2185 vsprintf (message
+ orig_len
, fmt
, ap
);
2192 /* This function is registered as the "special_function" in the
2193 Xtensa howto for handling simplify operations.
2194 bfd_perform_relocation / bfd_install_relocation use it to
2195 perform (install) the specified relocation. Since this replaces the code
2196 in bfd_perform_relocation, it is basically an Xtensa-specific,
2197 stripped-down version of bfd_perform_relocation. */
2199 static bfd_reloc_status_type
2200 bfd_elf_xtensa_reloc (bfd
*abfd
,
2201 arelent
*reloc_entry
,
2204 asection
*input_section
,
2206 char **error_message
)
2209 bfd_reloc_status_type flag
;
2210 bfd_size_type octets
= reloc_entry
->address
* bfd_octets_per_byte (abfd
);
2211 bfd_vma output_base
= 0;
2212 reloc_howto_type
*howto
= reloc_entry
->howto
;
2213 asection
*reloc_target_output_section
;
2214 bfd_boolean is_weak_undef
;
2216 if (!xtensa_default_isa
)
2217 xtensa_default_isa
= xtensa_isa_init (0, 0);
2219 /* ELF relocs are against symbols. If we are producing relocatable
2220 output, and the reloc is against an external symbol, the resulting
2221 reloc will also be against the same symbol. In such a case, we
2222 don't want to change anything about the way the reloc is handled,
2223 since it will all be done at final link time. This test is similar
2224 to what bfd_elf_generic_reloc does except that it lets relocs with
2225 howto->partial_inplace go through even if the addend is non-zero.
2226 (The real problem is that partial_inplace is set for XTENSA_32
2227 relocs to begin with, but that's a long story and there's little we
2228 can do about it now....) */
2230 if (output_bfd
&& (symbol
->flags
& BSF_SECTION_SYM
) == 0)
2232 reloc_entry
->address
+= input_section
->output_offset
;
2233 return bfd_reloc_ok
;
2236 /* Is the address of the relocation really within the section? */
2237 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
2238 return bfd_reloc_outofrange
;
2240 /* Work out which section the relocation is targeted at and the
2241 initial relocation command value. */
2243 /* Get symbol value. (Common symbols are special.) */
2244 if (bfd_is_com_section (symbol
->section
))
2247 relocation
= symbol
->value
;
2249 reloc_target_output_section
= symbol
->section
->output_section
;
2251 /* Convert input-section-relative symbol value to absolute. */
2252 if ((output_bfd
&& !howto
->partial_inplace
)
2253 || reloc_target_output_section
== NULL
)
2256 output_base
= reloc_target_output_section
->vma
;
2258 relocation
+= output_base
+ symbol
->section
->output_offset
;
2260 /* Add in supplied addend. */
2261 relocation
+= reloc_entry
->addend
;
2263 /* Here the variable relocation holds the final address of the
2264 symbol we are relocating against, plus any addend. */
2267 if (!howto
->partial_inplace
)
2269 /* This is a partial relocation, and we want to apply the relocation
2270 to the reloc entry rather than the raw data. Everything except
2271 relocations against section symbols has already been handled
2274 BFD_ASSERT (symbol
->flags
& BSF_SECTION_SYM
);
2275 reloc_entry
->addend
= relocation
;
2276 reloc_entry
->address
+= input_section
->output_offset
;
2277 return bfd_reloc_ok
;
2281 reloc_entry
->address
+= input_section
->output_offset
;
2282 reloc_entry
->addend
= 0;
2286 is_weak_undef
= (bfd_is_und_section (symbol
->section
)
2287 && (symbol
->flags
& BSF_WEAK
) != 0);
2288 flag
= elf_xtensa_do_reloc (howto
, abfd
, input_section
, relocation
,
2289 (bfd_byte
*) data
, (bfd_vma
) octets
,
2290 is_weak_undef
, error_message
);
2292 if (flag
== bfd_reloc_dangerous
)
2294 /* Add the symbol name to the error message. */
2295 if (! *error_message
)
2296 *error_message
= "";
2297 *error_message
= vsprint_msg (*error_message
, ": (%s + 0x%lx)",
2298 strlen (symbol
->name
) + 17,
2300 (unsigned long) reloc_entry
->addend
);
2307 /* Set up an entry in the procedure linkage table. */
2310 elf_xtensa_create_plt_entry (struct bfd_link_info
*info
,
2312 unsigned reloc_index
)
2314 asection
*splt
, *sgotplt
;
2315 bfd_vma plt_base
, got_base
;
2316 bfd_vma code_offset
, lit_offset
;
2319 chunk
= reloc_index
/ PLT_ENTRIES_PER_CHUNK
;
2320 splt
= elf_xtensa_get_plt_section (info
, chunk
);
2321 sgotplt
= elf_xtensa_get_gotplt_section (info
, chunk
);
2322 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
);
2324 plt_base
= splt
->output_section
->vma
+ splt
->output_offset
;
2325 got_base
= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
;
2327 lit_offset
= 8 + (reloc_index
% PLT_ENTRIES_PER_CHUNK
) * 4;
2328 code_offset
= (reloc_index
% PLT_ENTRIES_PER_CHUNK
) * PLT_ENTRY_SIZE
;
2330 /* Fill in the literal entry. This is the offset of the dynamic
2331 relocation entry. */
2332 bfd_put_32 (output_bfd
, reloc_index
* sizeof (Elf32_External_Rela
),
2333 sgotplt
->contents
+ lit_offset
);
2335 /* Fill in the entry in the procedure linkage table. */
2336 memcpy (splt
->contents
+ code_offset
,
2337 (bfd_big_endian (output_bfd
)
2338 ? elf_xtensa_be_plt_entry
2339 : elf_xtensa_le_plt_entry
),
2341 bfd_put_16 (output_bfd
, l32r_offset (got_base
+ 0,
2342 plt_base
+ code_offset
+ 3),
2343 splt
->contents
+ code_offset
+ 4);
2344 bfd_put_16 (output_bfd
, l32r_offset (got_base
+ 4,
2345 plt_base
+ code_offset
+ 6),
2346 splt
->contents
+ code_offset
+ 7);
2347 bfd_put_16 (output_bfd
, l32r_offset (got_base
+ lit_offset
,
2348 plt_base
+ code_offset
+ 9),
2349 splt
->contents
+ code_offset
+ 10);
2351 return plt_base
+ code_offset
;
2355 static bfd_boolean
get_indirect_call_dest_reg (xtensa_opcode
, unsigned *);
2358 replace_tls_insn (Elf_Internal_Rela
*rel
,
2360 asection
*input_section
,
2362 bfd_boolean is_ld_model
,
2363 char **error_message
)
2365 static xtensa_insnbuf ibuff
= NULL
;
2366 static xtensa_insnbuf sbuff
= NULL
;
2367 xtensa_isa isa
= xtensa_default_isa
;
2369 xtensa_opcode old_op
, new_op
;
2370 bfd_size_type input_size
;
2372 unsigned dest_reg
, src_reg
;
2376 ibuff
= xtensa_insnbuf_alloc (isa
);
2377 sbuff
= xtensa_insnbuf_alloc (isa
);
2380 input_size
= bfd_get_section_limit (abfd
, input_section
);
2382 /* Read the instruction into a buffer and decode the opcode. */
2383 xtensa_insnbuf_from_chars (isa
, ibuff
, contents
+ rel
->r_offset
,
2384 input_size
- rel
->r_offset
);
2385 fmt
= xtensa_format_decode (isa
, ibuff
);
2386 if (fmt
== XTENSA_UNDEFINED
)
2388 *error_message
= "cannot decode instruction format";
2392 BFD_ASSERT (xtensa_format_num_slots (isa
, fmt
) == 1);
2393 xtensa_format_get_slot (isa
, fmt
, 0, ibuff
, sbuff
);
2395 old_op
= xtensa_opcode_decode (isa
, fmt
, 0, sbuff
);
2396 if (old_op
== XTENSA_UNDEFINED
)
2398 *error_message
= "cannot decode instruction opcode";
2402 r_type
= ELF32_R_TYPE (rel
->r_info
);
2405 case R_XTENSA_TLS_FUNC
:
2406 case R_XTENSA_TLS_ARG
:
2407 if (old_op
!= get_l32r_opcode ()
2408 || xtensa_operand_get_field (isa
, old_op
, 0, fmt
, 0,
2409 sbuff
, &dest_reg
) != 0)
2411 *error_message
= "cannot extract L32R destination for TLS access";
2416 case R_XTENSA_TLS_CALL
:
2417 if (! get_indirect_call_dest_reg (old_op
, &dest_reg
)
2418 || xtensa_operand_get_field (isa
, old_op
, 0, fmt
, 0,
2419 sbuff
, &src_reg
) != 0)
2421 *error_message
= "cannot extract CALLXn operands for TLS access";
2434 case R_XTENSA_TLS_FUNC
:
2435 case R_XTENSA_TLS_ARG
:
2436 /* Change the instruction to a NOP (or "OR a1, a1, a1" for older
2437 versions of Xtensa). */
2438 new_op
= xtensa_opcode_lookup (isa
, "nop");
2439 if (new_op
== XTENSA_UNDEFINED
)
2441 new_op
= xtensa_opcode_lookup (isa
, "or");
2442 if (new_op
== XTENSA_UNDEFINED
2443 || xtensa_opcode_encode (isa
, fmt
, 0, sbuff
, new_op
) != 0
2444 || xtensa_operand_set_field (isa
, new_op
, 0, fmt
, 0,
2446 || xtensa_operand_set_field (isa
, new_op
, 1, fmt
, 0,
2448 || xtensa_operand_set_field (isa
, new_op
, 2, fmt
, 0,
2451 *error_message
= "cannot encode OR for TLS access";
2457 if (xtensa_opcode_encode (isa
, fmt
, 0, sbuff
, new_op
) != 0)
2459 *error_message
= "cannot encode NOP for TLS access";
2465 case R_XTENSA_TLS_CALL
:
2466 /* Read THREADPTR into the CALLX's return value register. */
2467 new_op
= xtensa_opcode_lookup (isa
, "rur.threadptr");
2468 if (new_op
== XTENSA_UNDEFINED
2469 || xtensa_opcode_encode (isa
, fmt
, 0, sbuff
, new_op
) != 0
2470 || xtensa_operand_set_field (isa
, new_op
, 0, fmt
, 0,
2471 sbuff
, dest_reg
+ 2) != 0)
2473 *error_message
= "cannot encode RUR.THREADPTR for TLS access";
2483 case R_XTENSA_TLS_FUNC
:
2484 new_op
= xtensa_opcode_lookup (isa
, "rur.threadptr");
2485 if (new_op
== XTENSA_UNDEFINED
2486 || xtensa_opcode_encode (isa
, fmt
, 0, sbuff
, new_op
) != 0
2487 || xtensa_operand_set_field (isa
, new_op
, 0, fmt
, 0,
2488 sbuff
, dest_reg
) != 0)
2490 *error_message
= "cannot encode RUR.THREADPTR for TLS access";
2495 case R_XTENSA_TLS_ARG
:
2496 /* Nothing to do. Keep the original L32R instruction. */
2499 case R_XTENSA_TLS_CALL
:
2500 /* Add the CALLX's src register (holding the THREADPTR value)
2501 to the first argument register (holding the offset) and put
2502 the result in the CALLX's return value register. */
2503 new_op
= xtensa_opcode_lookup (isa
, "add");
2504 if (new_op
== XTENSA_UNDEFINED
2505 || xtensa_opcode_encode (isa
, fmt
, 0, sbuff
, new_op
) != 0
2506 || xtensa_operand_set_field (isa
, new_op
, 0, fmt
, 0,
2507 sbuff
, dest_reg
+ 2) != 0
2508 || xtensa_operand_set_field (isa
, new_op
, 1, fmt
, 0,
2509 sbuff
, dest_reg
+ 2) != 0
2510 || xtensa_operand_set_field (isa
, new_op
, 2, fmt
, 0,
2511 sbuff
, src_reg
) != 0)
2513 *error_message
= "cannot encode ADD for TLS access";
2520 xtensa_format_set_slot (isa
, fmt
, 0, ibuff
, sbuff
);
2521 xtensa_insnbuf_to_chars (isa
, ibuff
, contents
+ rel
->r_offset
,
2522 input_size
- rel
->r_offset
);
2528 #define IS_XTENSA_TLS_RELOC(R_TYPE) \
2529 ((R_TYPE) == R_XTENSA_TLSDESC_FN \
2530 || (R_TYPE) == R_XTENSA_TLSDESC_ARG \
2531 || (R_TYPE) == R_XTENSA_TLS_DTPOFF \
2532 || (R_TYPE) == R_XTENSA_TLS_TPOFF \
2533 || (R_TYPE) == R_XTENSA_TLS_FUNC \
2534 || (R_TYPE) == R_XTENSA_TLS_ARG \
2535 || (R_TYPE) == R_XTENSA_TLS_CALL)
2537 /* Relocate an Xtensa ELF section. This is invoked by the linker for
2538 both relocatable and final links. */
2541 elf_xtensa_relocate_section (bfd
*output_bfd
,
2542 struct bfd_link_info
*info
,
2544 asection
*input_section
,
2546 Elf_Internal_Rela
*relocs
,
2547 Elf_Internal_Sym
*local_syms
,
2548 asection
**local_sections
)
2550 struct elf_xtensa_link_hash_table
*htab
;
2551 Elf_Internal_Shdr
*symtab_hdr
;
2552 Elf_Internal_Rela
*rel
;
2553 Elf_Internal_Rela
*relend
;
2554 struct elf_link_hash_entry
**sym_hashes
;
2555 property_table_entry
*lit_table
= 0;
2557 char *local_got_tls_types
;
2558 char *error_message
= NULL
;
2559 bfd_size_type input_size
;
2562 if (!xtensa_default_isa
)
2563 xtensa_default_isa
= xtensa_isa_init (0, 0);
2565 BFD_ASSERT (is_xtensa_elf (input_bfd
));
2567 htab
= elf_xtensa_hash_table (info
);
2571 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2572 sym_hashes
= elf_sym_hashes (input_bfd
);
2573 local_got_tls_types
= elf_xtensa_local_got_tls_type (input_bfd
);
2575 if (elf_hash_table (info
)->dynamic_sections_created
)
2577 ltblsize
= xtensa_read_table_entries (input_bfd
, input_section
,
2578 &lit_table
, XTENSA_LIT_SEC_NAME
,
2584 input_size
= bfd_get_section_limit (input_bfd
, input_section
);
2587 relend
= relocs
+ input_section
->reloc_count
;
2588 for (; rel
< relend
; rel
++)
2591 reloc_howto_type
*howto
;
2592 unsigned long r_symndx
;
2593 struct elf_link_hash_entry
*h
;
2594 Elf_Internal_Sym
*sym
;
2599 bfd_reloc_status_type r
;
2600 bfd_boolean is_weak_undef
;
2601 bfd_boolean unresolved_reloc
;
2603 bfd_boolean dynamic_symbol
;
2605 r_type
= ELF32_R_TYPE (rel
->r_info
);
2606 if (r_type
== (int) R_XTENSA_GNU_VTINHERIT
2607 || r_type
== (int) R_XTENSA_GNU_VTENTRY
)
2610 if (r_type
< 0 || r_type
>= (int) R_XTENSA_max
)
2612 bfd_set_error (bfd_error_bad_value
);
2615 howto
= &elf_howto_table
[r_type
];
2617 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2622 is_weak_undef
= FALSE
;
2623 unresolved_reloc
= FALSE
;
2626 if (howto
->partial_inplace
&& !info
->relocatable
)
2628 /* Because R_XTENSA_32 was made partial_inplace to fix some
2629 problems with DWARF info in partial links, there may be
2630 an addend stored in the contents. Take it out of there
2631 and move it back into the addend field of the reloc. */
2632 rel
->r_addend
+= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2633 bfd_put_32 (input_bfd
, 0, contents
+ rel
->r_offset
);
2636 if (r_symndx
< symtab_hdr
->sh_info
)
2638 sym
= local_syms
+ r_symndx
;
2639 sym_type
= ELF32_ST_TYPE (sym
->st_info
);
2640 sec
= local_sections
[r_symndx
];
2641 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2645 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2646 r_symndx
, symtab_hdr
, sym_hashes
,
2648 unresolved_reloc
, warned
);
2651 && !unresolved_reloc
2652 && h
->root
.type
== bfd_link_hash_undefweak
)
2653 is_weak_undef
= TRUE
;
2658 if (sec
!= NULL
&& elf_discarded_section (sec
))
2660 /* For relocs against symbols from removed linkonce sections,
2661 or sections discarded by a linker script, we just want the
2662 section contents zeroed. Avoid any special processing. */
2663 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
2669 if (info
->relocatable
)
2671 /* This is a relocatable link.
2672 1) If the reloc is against a section symbol, adjust
2673 according to the output section.
2674 2) If there is a new target for this relocation,
2675 the new target will be in the same output section.
2676 We adjust the relocation by the output section
2679 if (relaxing_section
)
2681 /* Check if this references a section in another input file. */
2682 if (!do_fix_for_relocatable_link (rel
, input_bfd
, input_section
,
2687 if (r_type
== R_XTENSA_ASM_SIMPLIFY
)
2689 error_message
= NULL
;
2690 /* Convert ASM_SIMPLIFY into the simpler relocation
2691 so that they never escape a relaxing link. */
2692 r
= contract_asm_expansion (contents
, input_size
, rel
,
2694 if (r
!= bfd_reloc_ok
)
2696 if (!((*info
->callbacks
->reloc_dangerous
)
2697 (info
, error_message
, input_bfd
, input_section
,
2701 r_type
= ELF32_R_TYPE (rel
->r_info
);
2704 /* This is a relocatable link, so we don't have to change
2705 anything unless the reloc is against a section symbol,
2706 in which case we have to adjust according to where the
2707 section symbol winds up in the output section. */
2708 if (r_symndx
< symtab_hdr
->sh_info
)
2710 sym
= local_syms
+ r_symndx
;
2711 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
2713 sec
= local_sections
[r_symndx
];
2714 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
2718 /* If there is an addend with a partial_inplace howto,
2719 then move the addend to the contents. This is a hack
2720 to work around problems with DWARF in relocatable links
2721 with some previous version of BFD. Now we can't easily get
2722 rid of the hack without breaking backward compatibility.... */
2725 howto
= &elf_howto_table
[r_type
];
2726 if (howto
->partial_inplace
)
2728 r
= elf_xtensa_do_reloc (howto
, input_bfd
, input_section
,
2729 rel
->r_addend
, contents
,
2730 rel
->r_offset
, FALSE
,
2732 if (r
!= bfd_reloc_ok
)
2734 if (!((*info
->callbacks
->reloc_dangerous
)
2735 (info
, error_message
, input_bfd
, input_section
,
2743 /* Done with work for relocatable link; continue with next reloc. */
2747 /* This is a final link. */
2749 if (relaxing_section
)
2751 /* Check if this references a section in another input file. */
2752 do_fix_for_final_link (rel
, input_bfd
, input_section
, contents
,
2756 /* Sanity check the address. */
2757 if (rel
->r_offset
>= input_size
2758 && ELF32_R_TYPE (rel
->r_info
) != R_XTENSA_NONE
)
2760 (*_bfd_error_handler
)
2761 (_("%B(%A+0x%lx): relocation offset out of range (size=0x%x)"),
2762 input_bfd
, input_section
, rel
->r_offset
, input_size
);
2763 bfd_set_error (bfd_error_bad_value
);
2768 name
= h
->root
.root
.string
;
2771 name
= (bfd_elf_string_from_elf_section
2772 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2773 if (name
== NULL
|| *name
== '\0')
2774 name
= bfd_section_name (input_bfd
, sec
);
2778 && r_type
!= R_XTENSA_NONE
2780 || h
->root
.type
== bfd_link_hash_defined
2781 || h
->root
.type
== bfd_link_hash_defweak
)
2782 && IS_XTENSA_TLS_RELOC (r_type
) != (sym_type
== STT_TLS
))
2784 (*_bfd_error_handler
)
2785 ((sym_type
== STT_TLS
2786 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
2787 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
2790 (long) rel
->r_offset
,
2795 dynamic_symbol
= elf_xtensa_dynamic_symbol_p (h
, info
);
2797 tls_type
= GOT_UNKNOWN
;
2799 tls_type
= elf_xtensa_hash_entry (h
)->tls_type
;
2800 else if (local_got_tls_types
)
2801 tls_type
= local_got_tls_types
[r_symndx
];
2807 if (elf_hash_table (info
)->dynamic_sections_created
2808 && (input_section
->flags
& SEC_ALLOC
) != 0
2809 && (dynamic_symbol
|| info
->shared
))
2811 Elf_Internal_Rela outrel
;
2815 if (dynamic_symbol
&& r_type
== R_XTENSA_PLT
)
2816 srel
= htab
->srelplt
;
2818 srel
= htab
->srelgot
;
2820 BFD_ASSERT (srel
!= NULL
);
2823 _bfd_elf_section_offset (output_bfd
, info
,
2824 input_section
, rel
->r_offset
);
2826 if ((outrel
.r_offset
| 1) == (bfd_vma
) -1)
2827 memset (&outrel
, 0, sizeof outrel
);
2830 outrel
.r_offset
+= (input_section
->output_section
->vma
2831 + input_section
->output_offset
);
2833 /* Complain if the relocation is in a read-only section
2834 and not in a literal pool. */
2835 if ((input_section
->flags
& SEC_READONLY
) != 0
2836 && !elf_xtensa_in_literal_pool (lit_table
, ltblsize
,
2840 _("dynamic relocation in read-only section");
2841 if (!((*info
->callbacks
->reloc_dangerous
)
2842 (info
, error_message
, input_bfd
, input_section
,
2849 outrel
.r_addend
= rel
->r_addend
;
2852 if (r_type
== R_XTENSA_32
)
2855 ELF32_R_INFO (h
->dynindx
, R_XTENSA_GLOB_DAT
);
2858 else /* r_type == R_XTENSA_PLT */
2861 ELF32_R_INFO (h
->dynindx
, R_XTENSA_JMP_SLOT
);
2863 /* Create the PLT entry and set the initial
2864 contents of the literal entry to the address of
2867 elf_xtensa_create_plt_entry (info
, output_bfd
,
2870 unresolved_reloc
= FALSE
;
2874 /* Generate a RELATIVE relocation. */
2875 outrel
.r_info
= ELF32_R_INFO (0, R_XTENSA_RELATIVE
);
2876 outrel
.r_addend
= 0;
2880 loc
= (srel
->contents
2881 + srel
->reloc_count
++ * sizeof (Elf32_External_Rela
));
2882 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
2883 BFD_ASSERT (sizeof (Elf32_External_Rela
) * srel
->reloc_count
2886 else if (r_type
== R_XTENSA_ASM_EXPAND
&& dynamic_symbol
)
2888 /* This should only happen for non-PIC code, which is not
2889 supposed to be used on systems with dynamic linking.
2890 Just ignore these relocations. */
2895 case R_XTENSA_TLS_TPOFF
:
2896 /* Switch to LE model for local symbols in an executable. */
2897 if (! info
->shared
&& ! dynamic_symbol
)
2899 relocation
= tpoff (info
, relocation
);
2904 case R_XTENSA_TLSDESC_FN
:
2905 case R_XTENSA_TLSDESC_ARG
:
2907 if (r_type
== R_XTENSA_TLSDESC_FN
)
2909 if (! info
->shared
|| (tls_type
& GOT_TLS_IE
) != 0)
2910 r_type
= R_XTENSA_NONE
;
2912 else if (r_type
== R_XTENSA_TLSDESC_ARG
)
2916 if ((tls_type
& GOT_TLS_IE
) != 0)
2917 r_type
= R_XTENSA_TLS_TPOFF
;
2921 r_type
= R_XTENSA_TLS_TPOFF
;
2922 if (! dynamic_symbol
)
2924 relocation
= tpoff (info
, relocation
);
2930 if (r_type
== R_XTENSA_NONE
)
2931 /* Nothing to do here; skip to the next reloc. */
2934 if (! elf_hash_table (info
)->dynamic_sections_created
)
2937 _("TLS relocation invalid without dynamic sections");
2938 if (!((*info
->callbacks
->reloc_dangerous
)
2939 (info
, error_message
, input_bfd
, input_section
,
2945 Elf_Internal_Rela outrel
;
2947 asection
*srel
= htab
->srelgot
;
2950 outrel
.r_offset
= (input_section
->output_section
->vma
2951 + input_section
->output_offset
2954 /* Complain if the relocation is in a read-only section
2955 and not in a literal pool. */
2956 if ((input_section
->flags
& SEC_READONLY
) != 0
2957 && ! elf_xtensa_in_literal_pool (lit_table
, ltblsize
,
2961 _("dynamic relocation in read-only section");
2962 if (!((*info
->callbacks
->reloc_dangerous
)
2963 (info
, error_message
, input_bfd
, input_section
,
2968 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
2970 outrel
.r_addend
= relocation
- dtpoff_base (info
);
2972 outrel
.r_addend
= 0;
2975 outrel
.r_info
= ELF32_R_INFO (indx
, r_type
);
2977 unresolved_reloc
= FALSE
;
2980 loc
= (srel
->contents
2981 + srel
->reloc_count
++ * sizeof (Elf32_External_Rela
));
2982 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
2983 BFD_ASSERT (sizeof (Elf32_External_Rela
) * srel
->reloc_count
2989 case R_XTENSA_TLS_DTPOFF
:
2991 /* Switch from LD model to LE model. */
2992 relocation
= tpoff (info
, relocation
);
2994 relocation
-= dtpoff_base (info
);
2997 case R_XTENSA_TLS_FUNC
:
2998 case R_XTENSA_TLS_ARG
:
2999 case R_XTENSA_TLS_CALL
:
3000 /* Check if optimizing to IE or LE model. */
3001 if ((tls_type
& GOT_TLS_IE
) != 0)
3003 bfd_boolean is_ld_model
=
3004 (h
&& elf_xtensa_hash_entry (h
) == htab
->tlsbase
);
3005 if (! replace_tls_insn (rel
, input_bfd
, input_section
, contents
,
3006 is_ld_model
, &error_message
))
3008 if (!((*info
->callbacks
->reloc_dangerous
)
3009 (info
, error_message
, input_bfd
, input_section
,
3014 if (r_type
!= R_XTENSA_TLS_ARG
|| is_ld_model
)
3016 /* Skip subsequent relocations on the same instruction. */
3017 while (rel
+ 1 < relend
&& rel
[1].r_offset
== rel
->r_offset
)
3024 if (elf_hash_table (info
)->dynamic_sections_created
3025 && dynamic_symbol
&& (is_operand_relocation (r_type
)
3026 || r_type
== R_XTENSA_32_PCREL
))
3029 vsprint_msg ("invalid relocation for dynamic symbol", ": %s",
3030 strlen (name
) + 2, name
);
3031 if (!((*info
->callbacks
->reloc_dangerous
)
3032 (info
, error_message
, input_bfd
, input_section
,
3040 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3041 because such sections are not SEC_ALLOC and thus ld.so will
3042 not process them. */
3043 if (unresolved_reloc
3044 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3047 (*_bfd_error_handler
)
3048 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3051 (long) rel
->r_offset
,
3057 /* TLS optimizations may have changed r_type; update "howto". */
3058 howto
= &elf_howto_table
[r_type
];
3060 /* There's no point in calling bfd_perform_relocation here.
3061 Just go directly to our "special function". */
3062 r
= elf_xtensa_do_reloc (howto
, input_bfd
, input_section
,
3063 relocation
+ rel
->r_addend
,
3064 contents
, rel
->r_offset
, is_weak_undef
,
3067 if (r
!= bfd_reloc_ok
&& !warned
)
3069 BFD_ASSERT (r
== bfd_reloc_dangerous
|| r
== bfd_reloc_other
);
3070 BFD_ASSERT (error_message
!= NULL
);
3072 if (rel
->r_addend
== 0)
3073 error_message
= vsprint_msg (error_message
, ": %s",
3074 strlen (name
) + 2, name
);
3076 error_message
= vsprint_msg (error_message
, ": (%s+0x%x)",
3078 name
, (int) rel
->r_addend
);
3080 if (!((*info
->callbacks
->reloc_dangerous
)
3081 (info
, error_message
, input_bfd
, input_section
,
3090 input_section
->reloc_done
= TRUE
;
3096 /* Finish up dynamic symbol handling. There's not much to do here since
3097 the PLT and GOT entries are all set up by relocate_section. */
3100 elf_xtensa_finish_dynamic_symbol (bfd
*output_bfd ATTRIBUTE_UNUSED
,
3101 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3102 struct elf_link_hash_entry
*h
,
3103 Elf_Internal_Sym
*sym
)
3105 if (h
->needs_plt
&& !h
->def_regular
)
3107 /* Mark the symbol as undefined, rather than as defined in
3108 the .plt section. Leave the value alone. */
3109 sym
->st_shndx
= SHN_UNDEF
;
3110 /* If the symbol is weak, we do need to clear the value.
3111 Otherwise, the PLT entry would provide a definition for
3112 the symbol even if the symbol wasn't defined anywhere,
3113 and so the symbol would never be NULL. */
3114 if (!h
->ref_regular_nonweak
)
3118 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3119 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
3120 || h
== elf_hash_table (info
)->hgot
)
3121 sym
->st_shndx
= SHN_ABS
;
3127 /* Combine adjacent literal table entries in the output. Adjacent
3128 entries within each input section may have been removed during
3129 relaxation, but we repeat the process here, even though it's too late
3130 to shrink the output section, because it's important to minimize the
3131 number of literal table entries to reduce the start-up work for the
3132 runtime linker. Returns the number of remaining table entries or -1
3136 elf_xtensa_combine_prop_entries (bfd
*output_bfd
,
3141 property_table_entry
*table
;
3142 bfd_size_type section_size
, sgotloc_size
;
3146 section_size
= sxtlit
->size
;
3147 BFD_ASSERT (section_size
% 8 == 0);
3148 num
= section_size
/ 8;
3150 sgotloc_size
= sgotloc
->size
;
3151 if (sgotloc_size
!= section_size
)
3153 (*_bfd_error_handler
)
3154 (_("internal inconsistency in size of .got.loc section"));
3158 table
= bfd_malloc (num
* sizeof (property_table_entry
));
3162 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
3163 propagates to the output section, where it doesn't really apply and
3164 where it breaks the following call to bfd_malloc_and_get_section. */
3165 sxtlit
->flags
&= ~SEC_IN_MEMORY
;
3167 if (!bfd_malloc_and_get_section (output_bfd
, sxtlit
, &contents
))
3175 /* There should never be any relocations left at this point, so this
3176 is quite a bit easier than what is done during relaxation. */
3178 /* Copy the raw contents into a property table array and sort it. */
3180 for (n
= 0; n
< num
; n
++)
3182 table
[n
].address
= bfd_get_32 (output_bfd
, &contents
[offset
]);
3183 table
[n
].size
= bfd_get_32 (output_bfd
, &contents
[offset
+ 4]);
3186 qsort (table
, num
, sizeof (property_table_entry
), property_table_compare
);
3188 for (n
= 0; n
< num
; n
++)
3190 bfd_boolean remove_entry
= FALSE
;
3192 if (table
[n
].size
== 0)
3193 remove_entry
= TRUE
;
3195 && (table
[n
-1].address
+ table
[n
-1].size
== table
[n
].address
))
3197 table
[n
-1].size
+= table
[n
].size
;
3198 remove_entry
= TRUE
;
3203 for (m
= n
; m
< num
- 1; m
++)
3205 table
[m
].address
= table
[m
+1].address
;
3206 table
[m
].size
= table
[m
+1].size
;
3214 /* Copy the data back to the raw contents. */
3216 for (n
= 0; n
< num
; n
++)
3218 bfd_put_32 (output_bfd
, table
[n
].address
, &contents
[offset
]);
3219 bfd_put_32 (output_bfd
, table
[n
].size
, &contents
[offset
+ 4]);
3223 /* Clear the removed bytes. */
3224 if ((bfd_size_type
) (num
* 8) < section_size
)
3225 memset (&contents
[num
* 8], 0, section_size
- num
* 8);
3227 if (! bfd_set_section_contents (output_bfd
, sxtlit
, contents
, 0,
3231 /* Copy the contents to ".got.loc". */
3232 memcpy (sgotloc
->contents
, contents
, section_size
);
3240 /* Finish up the dynamic sections. */
3243 elf_xtensa_finish_dynamic_sections (bfd
*output_bfd
,
3244 struct bfd_link_info
*info
)
3246 struct elf_xtensa_link_hash_table
*htab
;
3248 asection
*sdyn
, *srelplt
, *sgot
, *sxtlit
, *sgotloc
;
3249 Elf32_External_Dyn
*dyncon
, *dynconend
;
3250 int num_xtlit_entries
= 0;
3252 if (! elf_hash_table (info
)->dynamic_sections_created
)
3255 htab
= elf_xtensa_hash_table (info
);
3259 dynobj
= elf_hash_table (info
)->dynobj
;
3260 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3261 BFD_ASSERT (sdyn
!= NULL
);
3263 /* Set the first entry in the global offset table to the address of
3264 the dynamic section. */
3268 BFD_ASSERT (sgot
->size
== 4);
3270 bfd_put_32 (output_bfd
, 0, sgot
->contents
);
3272 bfd_put_32 (output_bfd
,
3273 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
3277 srelplt
= htab
->srelplt
;
3278 if (srelplt
&& srelplt
->size
!= 0)
3280 asection
*sgotplt
, *srelgot
, *spltlittbl
;
3281 int chunk
, plt_chunks
, plt_entries
;
3282 Elf_Internal_Rela irela
;
3284 unsigned rtld_reloc
;
3286 srelgot
= htab
->srelgot
;
3287 spltlittbl
= htab
->spltlittbl
;
3288 BFD_ASSERT (srelgot
!= NULL
&& spltlittbl
!= NULL
);
3290 /* Find the first XTENSA_RTLD relocation. Presumably the rest
3291 of them follow immediately after.... */
3292 for (rtld_reloc
= 0; rtld_reloc
< srelgot
->reloc_count
; rtld_reloc
++)
3294 loc
= srelgot
->contents
+ rtld_reloc
* sizeof (Elf32_External_Rela
);
3295 bfd_elf32_swap_reloca_in (output_bfd
, loc
, &irela
);
3296 if (ELF32_R_TYPE (irela
.r_info
) == R_XTENSA_RTLD
)
3299 BFD_ASSERT (rtld_reloc
< srelgot
->reloc_count
);
3301 plt_entries
= srelplt
->size
/ sizeof (Elf32_External_Rela
);
3303 (plt_entries
+ PLT_ENTRIES_PER_CHUNK
- 1) / PLT_ENTRIES_PER_CHUNK
;
3305 for (chunk
= 0; chunk
< plt_chunks
; chunk
++)
3307 int chunk_entries
= 0;
3309 sgotplt
= elf_xtensa_get_gotplt_section (info
, chunk
);
3310 BFD_ASSERT (sgotplt
!= NULL
);
3312 /* Emit special RTLD relocations for the first two entries in
3313 each chunk of the .got.plt section. */
3315 loc
= srelgot
->contents
+ rtld_reloc
* sizeof (Elf32_External_Rela
);
3316 bfd_elf32_swap_reloca_in (output_bfd
, loc
, &irela
);
3317 BFD_ASSERT (ELF32_R_TYPE (irela
.r_info
) == R_XTENSA_RTLD
);
3318 irela
.r_offset
= (sgotplt
->output_section
->vma
3319 + sgotplt
->output_offset
);
3320 irela
.r_addend
= 1; /* tell rtld to set value to resolver function */
3321 bfd_elf32_swap_reloca_out (output_bfd
, &irela
, loc
);
3323 BFD_ASSERT (rtld_reloc
<= srelgot
->reloc_count
);
3325 /* Next literal immediately follows the first. */
3326 loc
+= sizeof (Elf32_External_Rela
);
3327 bfd_elf32_swap_reloca_in (output_bfd
, loc
, &irela
);
3328 BFD_ASSERT (ELF32_R_TYPE (irela
.r_info
) == R_XTENSA_RTLD
);
3329 irela
.r_offset
= (sgotplt
->output_section
->vma
3330 + sgotplt
->output_offset
+ 4);
3331 /* Tell rtld to set value to object's link map. */
3333 bfd_elf32_swap_reloca_out (output_bfd
, &irela
, loc
);
3335 BFD_ASSERT (rtld_reloc
<= srelgot
->reloc_count
);
3337 /* Fill in the literal table. */
3338 if (chunk
< plt_chunks
- 1)
3339 chunk_entries
= PLT_ENTRIES_PER_CHUNK
;
3341 chunk_entries
= plt_entries
- (chunk
* PLT_ENTRIES_PER_CHUNK
);
3343 BFD_ASSERT ((unsigned) (chunk
+ 1) * 8 <= spltlittbl
->size
);
3344 bfd_put_32 (output_bfd
,
3345 sgotplt
->output_section
->vma
+ sgotplt
->output_offset
,
3346 spltlittbl
->contents
+ (chunk
* 8) + 0);
3347 bfd_put_32 (output_bfd
,
3348 8 + (chunk_entries
* 4),
3349 spltlittbl
->contents
+ (chunk
* 8) + 4);
3352 /* All the dynamic relocations have been emitted at this point.
3353 Make sure the relocation sections are the correct size. */
3354 if (srelgot
->size
!= (sizeof (Elf32_External_Rela
)
3355 * srelgot
->reloc_count
)
3356 || srelplt
->size
!= (sizeof (Elf32_External_Rela
)
3357 * srelplt
->reloc_count
))
3360 /* The .xt.lit.plt section has just been modified. This must
3361 happen before the code below which combines adjacent literal
3362 table entries, and the .xt.lit.plt contents have to be forced to
3364 if (! bfd_set_section_contents (output_bfd
,
3365 spltlittbl
->output_section
,
3366 spltlittbl
->contents
,
3367 spltlittbl
->output_offset
,
3370 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
3371 spltlittbl
->flags
&= ~SEC_HAS_CONTENTS
;
3374 /* Combine adjacent literal table entries. */
3375 BFD_ASSERT (! info
->relocatable
);
3376 sxtlit
= bfd_get_section_by_name (output_bfd
, ".xt.lit");
3377 sgotloc
= htab
->sgotloc
;
3378 BFD_ASSERT (sgotloc
);
3382 elf_xtensa_combine_prop_entries (output_bfd
, sxtlit
, sgotloc
);
3383 if (num_xtlit_entries
< 0)
3387 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
3388 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3389 for (; dyncon
< dynconend
; dyncon
++)
3391 Elf_Internal_Dyn dyn
;
3393 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3400 case DT_XTENSA_GOT_LOC_SZ
:
3401 dyn
.d_un
.d_val
= num_xtlit_entries
;
3404 case DT_XTENSA_GOT_LOC_OFF
:
3405 dyn
.d_un
.d_ptr
= htab
->sgotloc
->output_section
->vma
;
3409 dyn
.d_un
.d_ptr
= htab
->sgot
->output_section
->vma
;
3413 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
3417 dyn
.d_un
.d_val
= htab
->srelplt
->output_section
->size
;
3421 /* Adjust RELASZ to not include JMPREL. This matches what
3422 glibc expects and what is done for several other ELF
3423 targets (e.g., i386, alpha), but the "correct" behavior
3424 seems to be unresolved. Since the linker script arranges
3425 for .rela.plt to follow all other relocation sections, we
3426 don't have to worry about changing the DT_RELA entry. */
3428 dyn
.d_un
.d_val
-= htab
->srelplt
->output_section
->size
;
3432 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3439 /* Functions for dealing with the e_flags field. */
3441 /* Merge backend specific data from an object file to the output
3442 object file when linking. */
3445 elf_xtensa_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
3447 unsigned out_mach
, in_mach
;
3448 flagword out_flag
, in_flag
;
3450 /* Check if we have the same endianess. */
3451 if (!_bfd_generic_verify_endian_match (ibfd
, obfd
))
3454 /* Don't even pretend to support mixed-format linking. */
3455 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3456 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3459 out_flag
= elf_elfheader (obfd
)->e_flags
;
3460 in_flag
= elf_elfheader (ibfd
)->e_flags
;
3462 out_mach
= out_flag
& EF_XTENSA_MACH
;
3463 in_mach
= in_flag
& EF_XTENSA_MACH
;
3464 if (out_mach
!= in_mach
)
3466 (*_bfd_error_handler
)
3467 (_("%B: incompatible machine type. Output is 0x%x. Input is 0x%x"),
3468 ibfd
, out_mach
, in_mach
);
3469 bfd_set_error (bfd_error_wrong_format
);
3473 if (! elf_flags_init (obfd
))
3475 elf_flags_init (obfd
) = TRUE
;
3476 elf_elfheader (obfd
)->e_flags
= in_flag
;
3478 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
3479 && bfd_get_arch_info (obfd
)->the_default
)
3480 return bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
3481 bfd_get_mach (ibfd
));
3486 if ((out_flag
& EF_XTENSA_XT_INSN
) != (in_flag
& EF_XTENSA_XT_INSN
))
3487 elf_elfheader (obfd
)->e_flags
&= (~ EF_XTENSA_XT_INSN
);
3489 if ((out_flag
& EF_XTENSA_XT_LIT
) != (in_flag
& EF_XTENSA_XT_LIT
))
3490 elf_elfheader (obfd
)->e_flags
&= (~ EF_XTENSA_XT_LIT
);
3497 elf_xtensa_set_private_flags (bfd
*abfd
, flagword flags
)
3499 BFD_ASSERT (!elf_flags_init (abfd
)
3500 || elf_elfheader (abfd
)->e_flags
== flags
);
3502 elf_elfheader (abfd
)->e_flags
|= flags
;
3503 elf_flags_init (abfd
) = TRUE
;
3510 elf_xtensa_print_private_bfd_data (bfd
*abfd
, void *farg
)
3512 FILE *f
= (FILE *) farg
;
3513 flagword e_flags
= elf_elfheader (abfd
)->e_flags
;
3515 fprintf (f
, "\nXtensa header:\n");
3516 if ((e_flags
& EF_XTENSA_MACH
) == E_XTENSA_MACH
)
3517 fprintf (f
, "\nMachine = Base\n");
3519 fprintf (f
, "\nMachine Id = 0x%x\n", e_flags
& EF_XTENSA_MACH
);
3521 fprintf (f
, "Insn tables = %s\n",
3522 (e_flags
& EF_XTENSA_XT_INSN
) ? "true" : "false");
3524 fprintf (f
, "Literal tables = %s\n",
3525 (e_flags
& EF_XTENSA_XT_LIT
) ? "true" : "false");
3527 return _bfd_elf_print_private_bfd_data (abfd
, farg
);
3531 /* Set the right machine number for an Xtensa ELF file. */
3534 elf_xtensa_object_p (bfd
*abfd
)
3537 unsigned long arch
= elf_elfheader (abfd
)->e_flags
& EF_XTENSA_MACH
;
3542 mach
= bfd_mach_xtensa
;
3548 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_xtensa
, mach
);
3553 /* The final processing done just before writing out an Xtensa ELF object
3554 file. This gets the Xtensa architecture right based on the machine
3558 elf_xtensa_final_write_processing (bfd
*abfd
,
3559 bfd_boolean linker ATTRIBUTE_UNUSED
)
3564 switch (mach
= bfd_get_mach (abfd
))
3566 case bfd_mach_xtensa
:
3567 val
= E_XTENSA_MACH
;
3573 elf_elfheader (abfd
)->e_flags
&= (~ EF_XTENSA_MACH
);
3574 elf_elfheader (abfd
)->e_flags
|= val
;
3578 static enum elf_reloc_type_class
3579 elf_xtensa_reloc_type_class (const Elf_Internal_Rela
*rela
)
3581 switch ((int) ELF32_R_TYPE (rela
->r_info
))
3583 case R_XTENSA_RELATIVE
:
3584 return reloc_class_relative
;
3585 case R_XTENSA_JMP_SLOT
:
3586 return reloc_class_plt
;
3588 return reloc_class_normal
;
3594 elf_xtensa_discard_info_for_section (bfd
*abfd
,
3595 struct elf_reloc_cookie
*cookie
,
3596 struct bfd_link_info
*info
,
3600 bfd_vma offset
, actual_offset
;
3601 bfd_size_type removed_bytes
= 0;
3602 bfd_size_type entry_size
;
3604 if (sec
->output_section
3605 && bfd_is_abs_section (sec
->output_section
))
3608 if (xtensa_is_proptable_section (sec
))
3613 if (sec
->size
== 0 || sec
->size
% entry_size
!= 0)
3616 contents
= retrieve_contents (abfd
, sec
, info
->keep_memory
);
3620 cookie
->rels
= retrieve_internal_relocs (abfd
, sec
, info
->keep_memory
);
3623 release_contents (sec
, contents
);
3627 /* Sort the relocations. They should already be in order when
3628 relaxation is enabled, but it might not be. */
3629 qsort (cookie
->rels
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
3630 internal_reloc_compare
);
3632 cookie
->rel
= cookie
->rels
;
3633 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
3635 for (offset
= 0; offset
< sec
->size
; offset
+= entry_size
)
3637 actual_offset
= offset
- removed_bytes
;
3639 /* The ...symbol_deleted_p function will skip over relocs but it
3640 won't adjust their offsets, so do that here. */
3641 while (cookie
->rel
< cookie
->relend
3642 && cookie
->rel
->r_offset
< offset
)
3644 cookie
->rel
->r_offset
-= removed_bytes
;
3648 while (cookie
->rel
< cookie
->relend
3649 && cookie
->rel
->r_offset
== offset
)
3651 if (bfd_elf_reloc_symbol_deleted_p (offset
, cookie
))
3653 /* Remove the table entry. (If the reloc type is NONE, then
3654 the entry has already been merged with another and deleted
3655 during relaxation.) */
3656 if (ELF32_R_TYPE (cookie
->rel
->r_info
) != R_XTENSA_NONE
)
3658 /* Shift the contents up. */
3659 if (offset
+ entry_size
< sec
->size
)
3660 memmove (&contents
[actual_offset
],
3661 &contents
[actual_offset
+ entry_size
],
3662 sec
->size
- offset
- entry_size
);
3663 removed_bytes
+= entry_size
;
3666 /* Remove this relocation. */
3667 cookie
->rel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
3670 /* Adjust the relocation offset for previous removals. This
3671 should not be done before calling ...symbol_deleted_p
3672 because it might mess up the offset comparisons there.
3673 Make sure the offset doesn't underflow in the case where
3674 the first entry is removed. */
3675 if (cookie
->rel
->r_offset
>= removed_bytes
)
3676 cookie
->rel
->r_offset
-= removed_bytes
;
3678 cookie
->rel
->r_offset
= 0;
3684 if (removed_bytes
!= 0)
3686 /* Adjust any remaining relocs (shouldn't be any). */
3687 for (; cookie
->rel
< cookie
->relend
; cookie
->rel
++)
3689 if (cookie
->rel
->r_offset
>= removed_bytes
)
3690 cookie
->rel
->r_offset
-= removed_bytes
;
3692 cookie
->rel
->r_offset
= 0;
3695 /* Clear the removed bytes. */
3696 memset (&contents
[sec
->size
- removed_bytes
], 0, removed_bytes
);
3698 pin_contents (sec
, contents
);
3699 pin_internal_relocs (sec
, cookie
->rels
);
3702 if (sec
->rawsize
== 0)
3703 sec
->rawsize
= sec
->size
;
3704 sec
->size
-= removed_bytes
;
3706 if (xtensa_is_littable_section (sec
))
3708 asection
*sgotloc
= elf_xtensa_hash_table (info
)->sgotloc
;
3710 sgotloc
->size
-= removed_bytes
;
3715 release_contents (sec
, contents
);
3716 release_internal_relocs (sec
, cookie
->rels
);
3719 return (removed_bytes
!= 0);
3724 elf_xtensa_discard_info (bfd
*abfd
,
3725 struct elf_reloc_cookie
*cookie
,
3726 struct bfd_link_info
*info
)
3729 bfd_boolean changed
= FALSE
;
3731 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3733 if (xtensa_is_property_section (sec
))
3735 if (elf_xtensa_discard_info_for_section (abfd
, cookie
, info
, sec
))
3745 elf_xtensa_ignore_discarded_relocs (asection
*sec
)
3747 return xtensa_is_property_section (sec
);
3752 elf_xtensa_action_discarded (asection
*sec
)
3754 if (strcmp (".xt_except_table", sec
->name
) == 0)
3757 if (strcmp (".xt_except_desc", sec
->name
) == 0)
3760 return _bfd_elf_default_action_discarded (sec
);
3764 /* Support for core dump NOTE sections. */
3767 elf_xtensa_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
3772 /* The size for Xtensa is variable, so don't try to recognize the format
3773 based on the size. Just assume this is GNU/Linux. */
3776 elf_tdata (abfd
)->core_signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
3779 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
3783 size
= note
->descsz
- offset
- 4;
3785 /* Make a ".reg/999" section. */
3786 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
3787 size
, note
->descpos
+ offset
);
3792 elf_xtensa_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
3794 switch (note
->descsz
)
3799 case 128: /* GNU/Linux elf_prpsinfo */
3800 elf_tdata (abfd
)->core_program
3801 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 32, 16);
3802 elf_tdata (abfd
)->core_command
3803 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 48, 80);
3806 /* Note that for some reason, a spurious space is tacked
3807 onto the end of the args in some (at least one anyway)
3808 implementations, so strip it off if it exists. */
3811 char *command
= elf_tdata (abfd
)->core_command
;
3812 int n
= strlen (command
);
3814 if (0 < n
&& command
[n
- 1] == ' ')
3815 command
[n
- 1] = '\0';
3822 /* Generic Xtensa configurability stuff. */
3824 static xtensa_opcode callx0_op
= XTENSA_UNDEFINED
;
3825 static xtensa_opcode callx4_op
= XTENSA_UNDEFINED
;
3826 static xtensa_opcode callx8_op
= XTENSA_UNDEFINED
;
3827 static xtensa_opcode callx12_op
= XTENSA_UNDEFINED
;
3828 static xtensa_opcode call0_op
= XTENSA_UNDEFINED
;
3829 static xtensa_opcode call4_op
= XTENSA_UNDEFINED
;
3830 static xtensa_opcode call8_op
= XTENSA_UNDEFINED
;
3831 static xtensa_opcode call12_op
= XTENSA_UNDEFINED
;
3834 init_call_opcodes (void)
3836 if (callx0_op
== XTENSA_UNDEFINED
)
3838 callx0_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx0");
3839 callx4_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx4");
3840 callx8_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx8");
3841 callx12_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx12");
3842 call0_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call0");
3843 call4_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call4");
3844 call8_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call8");
3845 call12_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call12");
3851 is_indirect_call_opcode (xtensa_opcode opcode
)
3853 init_call_opcodes ();
3854 return (opcode
== callx0_op
3855 || opcode
== callx4_op
3856 || opcode
== callx8_op
3857 || opcode
== callx12_op
);
3862 is_direct_call_opcode (xtensa_opcode opcode
)
3864 init_call_opcodes ();
3865 return (opcode
== call0_op
3866 || opcode
== call4_op
3867 || opcode
== call8_op
3868 || opcode
== call12_op
);
3873 is_windowed_call_opcode (xtensa_opcode opcode
)
3875 init_call_opcodes ();
3876 return (opcode
== call4_op
3877 || opcode
== call8_op
3878 || opcode
== call12_op
3879 || opcode
== callx4_op
3880 || opcode
== callx8_op
3881 || opcode
== callx12_op
);
3886 get_indirect_call_dest_reg (xtensa_opcode opcode
, unsigned *pdst
)
3888 unsigned dst
= (unsigned) -1;
3890 init_call_opcodes ();
3891 if (opcode
== callx0_op
)
3893 else if (opcode
== callx4_op
)
3895 else if (opcode
== callx8_op
)
3897 else if (opcode
== callx12_op
)
3900 if (dst
== (unsigned) -1)
3908 static xtensa_opcode
3909 get_const16_opcode (void)
3911 static bfd_boolean done_lookup
= FALSE
;
3912 static xtensa_opcode const16_opcode
= XTENSA_UNDEFINED
;
3915 const16_opcode
= xtensa_opcode_lookup (xtensa_default_isa
, "const16");
3918 return const16_opcode
;
3922 static xtensa_opcode
3923 get_l32r_opcode (void)
3925 static xtensa_opcode l32r_opcode
= XTENSA_UNDEFINED
;
3926 static bfd_boolean done_lookup
= FALSE
;
3930 l32r_opcode
= xtensa_opcode_lookup (xtensa_default_isa
, "l32r");
3938 l32r_offset (bfd_vma addr
, bfd_vma pc
)
3942 offset
= addr
- ((pc
+3) & -4);
3943 BFD_ASSERT ((offset
& ((1 << 2) - 1)) == 0);
3944 offset
= (signed int) offset
>> 2;
3945 BFD_ASSERT ((signed int) offset
>> 16 == -1);
3951 get_relocation_opnd (xtensa_opcode opcode
, int r_type
)
3953 xtensa_isa isa
= xtensa_default_isa
;
3954 int last_immed
, last_opnd
, opi
;
3956 if (opcode
== XTENSA_UNDEFINED
)
3957 return XTENSA_UNDEFINED
;
3959 /* Find the last visible PC-relative immediate operand for the opcode.
3960 If there are no PC-relative immediates, then choose the last visible
3961 immediate; otherwise, fail and return XTENSA_UNDEFINED. */
3962 last_immed
= XTENSA_UNDEFINED
;
3963 last_opnd
= xtensa_opcode_num_operands (isa
, opcode
);
3964 for (opi
= last_opnd
- 1; opi
>= 0; opi
--)
3966 if (xtensa_operand_is_visible (isa
, opcode
, opi
) == 0)
3968 if (xtensa_operand_is_PCrelative (isa
, opcode
, opi
) == 1)
3973 if (last_immed
== XTENSA_UNDEFINED
3974 && xtensa_operand_is_register (isa
, opcode
, opi
) == 0)
3978 return XTENSA_UNDEFINED
;
3980 /* If the operand number was specified in an old-style relocation,
3981 check for consistency with the operand computed above. */
3982 if (r_type
>= R_XTENSA_OP0
&& r_type
<= R_XTENSA_OP2
)
3984 int reloc_opnd
= r_type
- R_XTENSA_OP0
;
3985 if (reloc_opnd
!= last_immed
)
3986 return XTENSA_UNDEFINED
;
3994 get_relocation_slot (int r_type
)
4004 if (r_type
>= R_XTENSA_SLOT0_OP
&& r_type
<= R_XTENSA_SLOT14_OP
)
4005 return r_type
- R_XTENSA_SLOT0_OP
;
4006 if (r_type
>= R_XTENSA_SLOT0_ALT
&& r_type
<= R_XTENSA_SLOT14_ALT
)
4007 return r_type
- R_XTENSA_SLOT0_ALT
;
4011 return XTENSA_UNDEFINED
;
4015 /* Get the opcode for a relocation. */
4017 static xtensa_opcode
4018 get_relocation_opcode (bfd
*abfd
,
4021 Elf_Internal_Rela
*irel
)
4023 static xtensa_insnbuf ibuff
= NULL
;
4024 static xtensa_insnbuf sbuff
= NULL
;
4025 xtensa_isa isa
= xtensa_default_isa
;
4029 if (contents
== NULL
)
4030 return XTENSA_UNDEFINED
;
4032 if (bfd_get_section_limit (abfd
, sec
) <= irel
->r_offset
)
4033 return XTENSA_UNDEFINED
;
4037 ibuff
= xtensa_insnbuf_alloc (isa
);
4038 sbuff
= xtensa_insnbuf_alloc (isa
);
4041 /* Decode the instruction. */
4042 xtensa_insnbuf_from_chars (isa
, ibuff
, &contents
[irel
->r_offset
],
4043 sec
->size
- irel
->r_offset
);
4044 fmt
= xtensa_format_decode (isa
, ibuff
);
4045 slot
= get_relocation_slot (ELF32_R_TYPE (irel
->r_info
));
4046 if (slot
== XTENSA_UNDEFINED
)
4047 return XTENSA_UNDEFINED
;
4048 xtensa_format_get_slot (isa
, fmt
, slot
, ibuff
, sbuff
);
4049 return xtensa_opcode_decode (isa
, fmt
, slot
, sbuff
);
4054 is_l32r_relocation (bfd
*abfd
,
4057 Elf_Internal_Rela
*irel
)
4059 xtensa_opcode opcode
;
4060 if (!is_operand_relocation (ELF32_R_TYPE (irel
->r_info
)))
4062 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
4063 return (opcode
== get_l32r_opcode ());
4067 static bfd_size_type
4068 get_asm_simplify_size (bfd_byte
*contents
,
4069 bfd_size_type content_len
,
4070 bfd_size_type offset
)
4072 bfd_size_type insnlen
, size
= 0;
4074 /* Decode the size of the next two instructions. */
4075 insnlen
= insn_decode_len (contents
, content_len
, offset
);
4081 insnlen
= insn_decode_len (contents
, content_len
, offset
+ size
);
4091 is_alt_relocation (int r_type
)
4093 return (r_type
>= R_XTENSA_SLOT0_ALT
4094 && r_type
<= R_XTENSA_SLOT14_ALT
);
4099 is_operand_relocation (int r_type
)
4109 if (r_type
>= R_XTENSA_SLOT0_OP
&& r_type
<= R_XTENSA_SLOT14_OP
)
4111 if (r_type
>= R_XTENSA_SLOT0_ALT
&& r_type
<= R_XTENSA_SLOT14_ALT
)
4120 #define MIN_INSN_LENGTH 2
4122 /* Return 0 if it fails to decode. */
4125 insn_decode_len (bfd_byte
*contents
,
4126 bfd_size_type content_len
,
4127 bfd_size_type offset
)
4130 xtensa_isa isa
= xtensa_default_isa
;
4132 static xtensa_insnbuf ibuff
= NULL
;
4134 if (offset
+ MIN_INSN_LENGTH
> content_len
)
4138 ibuff
= xtensa_insnbuf_alloc (isa
);
4139 xtensa_insnbuf_from_chars (isa
, ibuff
, &contents
[offset
],
4140 content_len
- offset
);
4141 fmt
= xtensa_format_decode (isa
, ibuff
);
4142 if (fmt
== XTENSA_UNDEFINED
)
4144 insn_len
= xtensa_format_length (isa
, fmt
);
4145 if (insn_len
== XTENSA_UNDEFINED
)
4151 /* Decode the opcode for a single slot instruction.
4152 Return 0 if it fails to decode or the instruction is multi-slot. */
4155 insn_decode_opcode (bfd_byte
*contents
,
4156 bfd_size_type content_len
,
4157 bfd_size_type offset
,
4160 xtensa_isa isa
= xtensa_default_isa
;
4162 static xtensa_insnbuf insnbuf
= NULL
;
4163 static xtensa_insnbuf slotbuf
= NULL
;
4165 if (offset
+ MIN_INSN_LENGTH
> content_len
)
4166 return XTENSA_UNDEFINED
;
4168 if (insnbuf
== NULL
)
4170 insnbuf
= xtensa_insnbuf_alloc (isa
);
4171 slotbuf
= xtensa_insnbuf_alloc (isa
);
4174 xtensa_insnbuf_from_chars (isa
, insnbuf
, &contents
[offset
],
4175 content_len
- offset
);
4176 fmt
= xtensa_format_decode (isa
, insnbuf
);
4177 if (fmt
== XTENSA_UNDEFINED
)
4178 return XTENSA_UNDEFINED
;
4180 if (slot
>= xtensa_format_num_slots (isa
, fmt
))
4181 return XTENSA_UNDEFINED
;
4183 xtensa_format_get_slot (isa
, fmt
, slot
, insnbuf
, slotbuf
);
4184 return xtensa_opcode_decode (isa
, fmt
, slot
, slotbuf
);
4188 /* The offset is the offset in the contents.
4189 The address is the address of that offset. */
4192 check_branch_target_aligned (bfd_byte
*contents
,
4193 bfd_size_type content_length
,
4197 bfd_size_type insn_len
= insn_decode_len (contents
, content_length
, offset
);
4200 return check_branch_target_aligned_address (address
, insn_len
);
4205 check_loop_aligned (bfd_byte
*contents
,
4206 bfd_size_type content_length
,
4210 bfd_size_type loop_len
, insn_len
;
4211 xtensa_opcode opcode
;
4213 opcode
= insn_decode_opcode (contents
, content_length
, offset
, 0);
4214 if (opcode
== XTENSA_UNDEFINED
4215 || xtensa_opcode_is_loop (xtensa_default_isa
, opcode
) != 1)
4221 loop_len
= insn_decode_len (contents
, content_length
, offset
);
4222 insn_len
= insn_decode_len (contents
, content_length
, offset
+ loop_len
);
4223 if (loop_len
== 0 || insn_len
== 0)
4229 return check_branch_target_aligned_address (address
+ loop_len
, insn_len
);
4234 check_branch_target_aligned_address (bfd_vma addr
, int len
)
4237 return (addr
% 8 == 0);
4238 return ((addr
>> 2) == ((addr
+ len
- 1) >> 2));
4242 /* Instruction widening and narrowing. */
4244 /* When FLIX is available we need to access certain instructions only
4245 when they are 16-bit or 24-bit instructions. This table caches
4246 information about such instructions by walking through all the
4247 opcodes and finding the smallest single-slot format into which each
4250 static xtensa_format
*op_single_fmt_table
= NULL
;
4254 init_op_single_format_table (void)
4256 xtensa_isa isa
= xtensa_default_isa
;
4257 xtensa_insnbuf ibuf
;
4258 xtensa_opcode opcode
;
4262 if (op_single_fmt_table
)
4265 ibuf
= xtensa_insnbuf_alloc (isa
);
4266 num_opcodes
= xtensa_isa_num_opcodes (isa
);
4268 op_single_fmt_table
= (xtensa_format
*)
4269 bfd_malloc (sizeof (xtensa_format
) * num_opcodes
);
4270 for (opcode
= 0; opcode
< num_opcodes
; opcode
++)
4272 op_single_fmt_table
[opcode
] = XTENSA_UNDEFINED
;
4273 for (fmt
= 0; fmt
< xtensa_isa_num_formats (isa
); fmt
++)
4275 if (xtensa_format_num_slots (isa
, fmt
) == 1
4276 && xtensa_opcode_encode (isa
, fmt
, 0, ibuf
, opcode
) == 0)
4278 xtensa_opcode old_fmt
= op_single_fmt_table
[opcode
];
4279 int fmt_length
= xtensa_format_length (isa
, fmt
);
4280 if (old_fmt
== XTENSA_UNDEFINED
4281 || fmt_length
< xtensa_format_length (isa
, old_fmt
))
4282 op_single_fmt_table
[opcode
] = fmt
;
4286 xtensa_insnbuf_free (isa
, ibuf
);
4290 static xtensa_format
4291 get_single_format (xtensa_opcode opcode
)
4293 init_op_single_format_table ();
4294 return op_single_fmt_table
[opcode
];
4298 /* For the set of narrowable instructions we do NOT include the
4299 narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
4300 involved during linker relaxation that may require these to
4301 re-expand in some conditions. Also, the narrowing "or" -> mov.n
4302 requires special case code to ensure it only works when op1 == op2. */
4310 struct string_pair narrowable
[] =
4313 { "addi", "addi.n" },
4314 { "addmi", "addi.n" },
4315 { "l32i", "l32i.n" },
4316 { "movi", "movi.n" },
4318 { "retw", "retw.n" },
4319 { "s32i", "s32i.n" },
4320 { "or", "mov.n" } /* special case only when op1 == op2 */
4323 struct string_pair widenable
[] =
4326 { "addi", "addi.n" },
4327 { "addmi", "addi.n" },
4328 { "beqz", "beqz.n" },
4329 { "bnez", "bnez.n" },
4330 { "l32i", "l32i.n" },
4331 { "movi", "movi.n" },
4333 { "retw", "retw.n" },
4334 { "s32i", "s32i.n" },
4335 { "or", "mov.n" } /* special case only when op1 == op2 */
4339 /* Check if an instruction can be "narrowed", i.e., changed from a standard
4340 3-byte instruction to a 2-byte "density" instruction. If it is valid,
4341 return the instruction buffer holding the narrow instruction. Otherwise,
4342 return 0. The set of valid narrowing are specified by a string table
4343 but require some special case operand checks in some cases. */
4345 static xtensa_insnbuf
4346 can_narrow_instruction (xtensa_insnbuf slotbuf
,
4348 xtensa_opcode opcode
)
4350 xtensa_isa isa
= xtensa_default_isa
;
4351 xtensa_format o_fmt
;
4354 static xtensa_insnbuf o_insnbuf
= NULL
;
4355 static xtensa_insnbuf o_slotbuf
= NULL
;
4357 if (o_insnbuf
== NULL
)
4359 o_insnbuf
= xtensa_insnbuf_alloc (isa
);
4360 o_slotbuf
= xtensa_insnbuf_alloc (isa
);
4363 for (opi
= 0; opi
< (sizeof (narrowable
)/sizeof (struct string_pair
)); opi
++)
4365 bfd_boolean is_or
= (strcmp ("or", narrowable
[opi
].wide
) == 0);
4367 if (opcode
== xtensa_opcode_lookup (isa
, narrowable
[opi
].wide
))
4369 uint32 value
, newval
;
4370 int i
, operand_count
, o_operand_count
;
4371 xtensa_opcode o_opcode
;
4373 /* Address does not matter in this case. We might need to
4374 fix it to handle branches/jumps. */
4375 bfd_vma self_address
= 0;
4377 o_opcode
= xtensa_opcode_lookup (isa
, narrowable
[opi
].narrow
);
4378 if (o_opcode
== XTENSA_UNDEFINED
)
4380 o_fmt
= get_single_format (o_opcode
);
4381 if (o_fmt
== XTENSA_UNDEFINED
)
4384 if (xtensa_format_length (isa
, fmt
) != 3
4385 || xtensa_format_length (isa
, o_fmt
) != 2)
4388 xtensa_format_encode (isa
, o_fmt
, o_insnbuf
);
4389 operand_count
= xtensa_opcode_num_operands (isa
, opcode
);
4390 o_operand_count
= xtensa_opcode_num_operands (isa
, o_opcode
);
4392 if (xtensa_opcode_encode (isa
, o_fmt
, 0, o_slotbuf
, o_opcode
) != 0)
4397 if (xtensa_opcode_num_operands (isa
, o_opcode
) != operand_count
)
4402 uint32 rawval0
, rawval1
, rawval2
;
4404 if (o_operand_count
+ 1 != operand_count
4405 || xtensa_operand_get_field (isa
, opcode
, 0,
4406 fmt
, 0, slotbuf
, &rawval0
) != 0
4407 || xtensa_operand_get_field (isa
, opcode
, 1,
4408 fmt
, 0, slotbuf
, &rawval1
) != 0
4409 || xtensa_operand_get_field (isa
, opcode
, 2,
4410 fmt
, 0, slotbuf
, &rawval2
) != 0
4411 || rawval1
!= rawval2
4412 || rawval0
== rawval1
/* it is a nop */)
4416 for (i
= 0; i
< o_operand_count
; ++i
)
4418 if (xtensa_operand_get_field (isa
, opcode
, i
, fmt
, 0,
4420 || xtensa_operand_decode (isa
, opcode
, i
, &value
))
4423 /* PC-relative branches need adjustment, but
4424 the PC-rel operand will always have a relocation. */
4426 if (xtensa_operand_do_reloc (isa
, o_opcode
, i
, &newval
,
4428 || xtensa_operand_encode (isa
, o_opcode
, i
, &newval
)
4429 || xtensa_operand_set_field (isa
, o_opcode
, i
, o_fmt
, 0,
4434 if (xtensa_format_set_slot (isa
, o_fmt
, 0, o_insnbuf
, o_slotbuf
))
4444 /* Attempt to narrow an instruction. If the narrowing is valid, perform
4445 the action in-place directly into the contents and return TRUE. Otherwise,
4446 the return value is FALSE and the contents are not modified. */
4449 narrow_instruction (bfd_byte
*contents
,
4450 bfd_size_type content_length
,
4451 bfd_size_type offset
)
4453 xtensa_opcode opcode
;
4454 bfd_size_type insn_len
;
4455 xtensa_isa isa
= xtensa_default_isa
;
4457 xtensa_insnbuf o_insnbuf
;
4459 static xtensa_insnbuf insnbuf
= NULL
;
4460 static xtensa_insnbuf slotbuf
= NULL
;
4462 if (insnbuf
== NULL
)
4464 insnbuf
= xtensa_insnbuf_alloc (isa
);
4465 slotbuf
= xtensa_insnbuf_alloc (isa
);
4468 BFD_ASSERT (offset
< content_length
);
4470 if (content_length
< 2)
4473 /* We will hand-code a few of these for a little while.
4474 These have all been specified in the assembler aleady. */
4475 xtensa_insnbuf_from_chars (isa
, insnbuf
, &contents
[offset
],
4476 content_length
- offset
);
4477 fmt
= xtensa_format_decode (isa
, insnbuf
);
4478 if (xtensa_format_num_slots (isa
, fmt
) != 1)
4481 if (xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
) != 0)
4484 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4485 if (opcode
== XTENSA_UNDEFINED
)
4487 insn_len
= xtensa_format_length (isa
, fmt
);
4488 if (insn_len
> content_length
)
4491 o_insnbuf
= can_narrow_instruction (slotbuf
, fmt
, opcode
);
4494 xtensa_insnbuf_to_chars (isa
, o_insnbuf
, contents
+ offset
,
4495 content_length
- offset
);
4503 /* Check if an instruction can be "widened", i.e., changed from a 2-byte
4504 "density" instruction to a standard 3-byte instruction. If it is valid,
4505 return the instruction buffer holding the wide instruction. Otherwise,
4506 return 0. The set of valid widenings are specified by a string table
4507 but require some special case operand checks in some cases. */
4509 static xtensa_insnbuf
4510 can_widen_instruction (xtensa_insnbuf slotbuf
,
4512 xtensa_opcode opcode
)
4514 xtensa_isa isa
= xtensa_default_isa
;
4515 xtensa_format o_fmt
;
4518 static xtensa_insnbuf o_insnbuf
= NULL
;
4519 static xtensa_insnbuf o_slotbuf
= NULL
;
4521 if (o_insnbuf
== NULL
)
4523 o_insnbuf
= xtensa_insnbuf_alloc (isa
);
4524 o_slotbuf
= xtensa_insnbuf_alloc (isa
);
4527 for (opi
= 0; opi
< (sizeof (widenable
)/sizeof (struct string_pair
)); opi
++)
4529 bfd_boolean is_or
= (strcmp ("or", widenable
[opi
].wide
) == 0);
4530 bfd_boolean is_branch
= (strcmp ("beqz", widenable
[opi
].wide
) == 0
4531 || strcmp ("bnez", widenable
[opi
].wide
) == 0);
4533 if (opcode
== xtensa_opcode_lookup (isa
, widenable
[opi
].narrow
))
4535 uint32 value
, newval
;
4536 int i
, operand_count
, o_operand_count
, check_operand_count
;
4537 xtensa_opcode o_opcode
;
4539 /* Address does not matter in this case. We might need to fix it
4540 to handle branches/jumps. */
4541 bfd_vma self_address
= 0;
4543 o_opcode
= xtensa_opcode_lookup (isa
, widenable
[opi
].wide
);
4544 if (o_opcode
== XTENSA_UNDEFINED
)
4546 o_fmt
= get_single_format (o_opcode
);
4547 if (o_fmt
== XTENSA_UNDEFINED
)
4550 if (xtensa_format_length (isa
, fmt
) != 2
4551 || xtensa_format_length (isa
, o_fmt
) != 3)
4554 xtensa_format_encode (isa
, o_fmt
, o_insnbuf
);
4555 operand_count
= xtensa_opcode_num_operands (isa
, opcode
);
4556 o_operand_count
= xtensa_opcode_num_operands (isa
, o_opcode
);
4557 check_operand_count
= o_operand_count
;
4559 if (xtensa_opcode_encode (isa
, o_fmt
, 0, o_slotbuf
, o_opcode
) != 0)
4564 if (xtensa_opcode_num_operands (isa
, o_opcode
) != operand_count
)
4569 uint32 rawval0
, rawval1
;
4571 if (o_operand_count
!= operand_count
+ 1
4572 || xtensa_operand_get_field (isa
, opcode
, 0,
4573 fmt
, 0, slotbuf
, &rawval0
) != 0
4574 || xtensa_operand_get_field (isa
, opcode
, 1,
4575 fmt
, 0, slotbuf
, &rawval1
) != 0
4576 || rawval0
== rawval1
/* it is a nop */)
4580 check_operand_count
--;
4582 for (i
= 0; i
< check_operand_count
; i
++)
4585 if (is_or
&& i
== o_operand_count
- 1)
4587 if (xtensa_operand_get_field (isa
, opcode
, new_i
, fmt
, 0,
4589 || xtensa_operand_decode (isa
, opcode
, new_i
, &value
))
4592 /* PC-relative branches need adjustment, but
4593 the PC-rel operand will always have a relocation. */
4595 if (xtensa_operand_do_reloc (isa
, o_opcode
, i
, &newval
,
4597 || xtensa_operand_encode (isa
, o_opcode
, i
, &newval
)
4598 || xtensa_operand_set_field (isa
, o_opcode
, i
, o_fmt
, 0,
4603 if (xtensa_format_set_slot (isa
, o_fmt
, 0, o_insnbuf
, o_slotbuf
))
4613 /* Attempt to widen an instruction. If the widening is valid, perform
4614 the action in-place directly into the contents and return TRUE. Otherwise,
4615 the return value is FALSE and the contents are not modified. */
4618 widen_instruction (bfd_byte
*contents
,
4619 bfd_size_type content_length
,
4620 bfd_size_type offset
)
4622 xtensa_opcode opcode
;
4623 bfd_size_type insn_len
;
4624 xtensa_isa isa
= xtensa_default_isa
;
4626 xtensa_insnbuf o_insnbuf
;
4628 static xtensa_insnbuf insnbuf
= NULL
;
4629 static xtensa_insnbuf slotbuf
= NULL
;
4631 if (insnbuf
== NULL
)
4633 insnbuf
= xtensa_insnbuf_alloc (isa
);
4634 slotbuf
= xtensa_insnbuf_alloc (isa
);
4637 BFD_ASSERT (offset
< content_length
);
4639 if (content_length
< 2)
4642 /* We will hand-code a few of these for a little while.
4643 These have all been specified in the assembler aleady. */
4644 xtensa_insnbuf_from_chars (isa
, insnbuf
, &contents
[offset
],
4645 content_length
- offset
);
4646 fmt
= xtensa_format_decode (isa
, insnbuf
);
4647 if (xtensa_format_num_slots (isa
, fmt
) != 1)
4650 if (xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
) != 0)
4653 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4654 if (opcode
== XTENSA_UNDEFINED
)
4656 insn_len
= xtensa_format_length (isa
, fmt
);
4657 if (insn_len
> content_length
)
4660 o_insnbuf
= can_widen_instruction (slotbuf
, fmt
, opcode
);
4663 xtensa_insnbuf_to_chars (isa
, o_insnbuf
, contents
+ offset
,
4664 content_length
- offset
);
4671 /* Code for transforming CALLs at link-time. */
4673 static bfd_reloc_status_type
4674 elf_xtensa_do_asm_simplify (bfd_byte
*contents
,
4676 bfd_vma content_length
,
4677 char **error_message
)
4679 static xtensa_insnbuf insnbuf
= NULL
;
4680 static xtensa_insnbuf slotbuf
= NULL
;
4681 xtensa_format core_format
= XTENSA_UNDEFINED
;
4682 xtensa_opcode opcode
;
4683 xtensa_opcode direct_call_opcode
;
4684 xtensa_isa isa
= xtensa_default_isa
;
4685 bfd_byte
*chbuf
= contents
+ address
;
4688 if (insnbuf
== NULL
)
4690 insnbuf
= xtensa_insnbuf_alloc (isa
);
4691 slotbuf
= xtensa_insnbuf_alloc (isa
);
4694 if (content_length
< address
)
4696 *error_message
= _("Attempt to convert L32R/CALLX to CALL failed");
4697 return bfd_reloc_other
;
4700 opcode
= get_expanded_call_opcode (chbuf
, content_length
- address
, 0);
4701 direct_call_opcode
= swap_callx_for_call_opcode (opcode
);
4702 if (direct_call_opcode
== XTENSA_UNDEFINED
)
4704 *error_message
= _("Attempt to convert L32R/CALLX to CALL failed");
4705 return bfd_reloc_other
;
4708 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
4709 core_format
= xtensa_format_lookup (isa
, "x24");
4710 opcode
= xtensa_opcode_lookup (isa
, "or");
4711 xtensa_opcode_encode (isa
, core_format
, 0, slotbuf
, opcode
);
4712 for (opn
= 0; opn
< 3; opn
++)
4715 xtensa_operand_encode (isa
, opcode
, opn
, ®no
);
4716 xtensa_operand_set_field (isa
, opcode
, opn
, core_format
, 0,
4719 xtensa_format_encode (isa
, core_format
, insnbuf
);
4720 xtensa_format_set_slot (isa
, core_format
, 0, insnbuf
, slotbuf
);
4721 xtensa_insnbuf_to_chars (isa
, insnbuf
, chbuf
, content_length
- address
);
4723 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
4724 xtensa_opcode_encode (isa
, core_format
, 0, slotbuf
, direct_call_opcode
);
4725 xtensa_operand_set_field (isa
, opcode
, 0, core_format
, 0, slotbuf
, 0);
4727 xtensa_format_encode (isa
, core_format
, insnbuf
);
4728 xtensa_format_set_slot (isa
, core_format
, 0, insnbuf
, slotbuf
);
4729 xtensa_insnbuf_to_chars (isa
, insnbuf
, chbuf
+ 3,
4730 content_length
- address
- 3);
4732 return bfd_reloc_ok
;
4736 static bfd_reloc_status_type
4737 contract_asm_expansion (bfd_byte
*contents
,
4738 bfd_vma content_length
,
4739 Elf_Internal_Rela
*irel
,
4740 char **error_message
)
4742 bfd_reloc_status_type retval
=
4743 elf_xtensa_do_asm_simplify (contents
, irel
->r_offset
, content_length
,
4746 if (retval
!= bfd_reloc_ok
)
4747 return bfd_reloc_dangerous
;
4749 /* Update the irel->r_offset field so that the right immediate and
4750 the right instruction are modified during the relocation. */
4751 irel
->r_offset
+= 3;
4752 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
), R_XTENSA_SLOT0_OP
);
4753 return bfd_reloc_ok
;
4757 static xtensa_opcode
4758 swap_callx_for_call_opcode (xtensa_opcode opcode
)
4760 init_call_opcodes ();
4762 if (opcode
== callx0_op
) return call0_op
;
4763 if (opcode
== callx4_op
) return call4_op
;
4764 if (opcode
== callx8_op
) return call8_op
;
4765 if (opcode
== callx12_op
) return call12_op
;
4767 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
4768 return XTENSA_UNDEFINED
;
4772 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
4773 CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
4774 If not, return XTENSA_UNDEFINED. */
4776 #define L32R_TARGET_REG_OPERAND 0
4777 #define CONST16_TARGET_REG_OPERAND 0
4778 #define CALLN_SOURCE_OPERAND 0
4780 static xtensa_opcode
4781 get_expanded_call_opcode (bfd_byte
*buf
, int bufsize
, bfd_boolean
*p_uses_l32r
)
4783 static xtensa_insnbuf insnbuf
= NULL
;
4784 static xtensa_insnbuf slotbuf
= NULL
;
4786 xtensa_opcode opcode
;
4787 xtensa_isa isa
= xtensa_default_isa
;
4788 uint32 regno
, const16_regno
, call_regno
;
4791 if (insnbuf
== NULL
)
4793 insnbuf
= xtensa_insnbuf_alloc (isa
);
4794 slotbuf
= xtensa_insnbuf_alloc (isa
);
4797 xtensa_insnbuf_from_chars (isa
, insnbuf
, buf
, bufsize
);
4798 fmt
= xtensa_format_decode (isa
, insnbuf
);
4799 if (fmt
== XTENSA_UNDEFINED
4800 || xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
))
4801 return XTENSA_UNDEFINED
;
4803 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4804 if (opcode
== XTENSA_UNDEFINED
)
4805 return XTENSA_UNDEFINED
;
4807 if (opcode
== get_l32r_opcode ())
4810 *p_uses_l32r
= TRUE
;
4811 if (xtensa_operand_get_field (isa
, opcode
, L32R_TARGET_REG_OPERAND
,
4812 fmt
, 0, slotbuf
, ®no
)
4813 || xtensa_operand_decode (isa
, opcode
, L32R_TARGET_REG_OPERAND
,
4815 return XTENSA_UNDEFINED
;
4817 else if (opcode
== get_const16_opcode ())
4820 *p_uses_l32r
= FALSE
;
4821 if (xtensa_operand_get_field (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4822 fmt
, 0, slotbuf
, ®no
)
4823 || xtensa_operand_decode (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4825 return XTENSA_UNDEFINED
;
4827 /* Check that the next instruction is also CONST16. */
4828 offset
+= xtensa_format_length (isa
, fmt
);
4829 xtensa_insnbuf_from_chars (isa
, insnbuf
, buf
+ offset
, bufsize
- offset
);
4830 fmt
= xtensa_format_decode (isa
, insnbuf
);
4831 if (fmt
== XTENSA_UNDEFINED
4832 || xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
))
4833 return XTENSA_UNDEFINED
;
4834 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4835 if (opcode
!= get_const16_opcode ())
4836 return XTENSA_UNDEFINED
;
4838 if (xtensa_operand_get_field (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4839 fmt
, 0, slotbuf
, &const16_regno
)
4840 || xtensa_operand_decode (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4842 || const16_regno
!= regno
)
4843 return XTENSA_UNDEFINED
;
4846 return XTENSA_UNDEFINED
;
4848 /* Next instruction should be an CALLXn with operand 0 == regno. */
4849 offset
+= xtensa_format_length (isa
, fmt
);
4850 xtensa_insnbuf_from_chars (isa
, insnbuf
, buf
+ offset
, bufsize
- offset
);
4851 fmt
= xtensa_format_decode (isa
, insnbuf
);
4852 if (fmt
== XTENSA_UNDEFINED
4853 || xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
))
4854 return XTENSA_UNDEFINED
;
4855 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4856 if (opcode
== XTENSA_UNDEFINED
4857 || !is_indirect_call_opcode (opcode
))
4858 return XTENSA_UNDEFINED
;
4860 if (xtensa_operand_get_field (isa
, opcode
, CALLN_SOURCE_OPERAND
,
4861 fmt
, 0, slotbuf
, &call_regno
)
4862 || xtensa_operand_decode (isa
, opcode
, CALLN_SOURCE_OPERAND
,
4864 return XTENSA_UNDEFINED
;
4866 if (call_regno
!= regno
)
4867 return XTENSA_UNDEFINED
;
4873 /* Data structures used during relaxation. */
4875 /* r_reloc: relocation values. */
4877 /* Through the relaxation process, we need to keep track of the values
4878 that will result from evaluating relocations. The standard ELF
4879 relocation structure is not sufficient for this purpose because we're
4880 operating on multiple input files at once, so we need to know which
4881 input file a relocation refers to. The r_reloc structure thus
4882 records both the input file (bfd) and ELF relocation.
4884 For efficiency, an r_reloc also contains a "target_offset" field to
4885 cache the target-section-relative offset value that is represented by
4888 The r_reloc also contains a virtual offset that allows multiple
4889 inserted literals to be placed at the same "address" with
4890 different offsets. */
4892 typedef struct r_reloc_struct r_reloc
;
4894 struct r_reloc_struct
4897 Elf_Internal_Rela rela
;
4898 bfd_vma target_offset
;
4899 bfd_vma virtual_offset
;
4903 /* The r_reloc structure is included by value in literal_value, but not
4904 every literal_value has an associated relocation -- some are simple
4905 constants. In such cases, we set all the fields in the r_reloc
4906 struct to zero. The r_reloc_is_const function should be used to
4907 detect this case. */
4910 r_reloc_is_const (const r_reloc
*r_rel
)
4912 return (r_rel
->abfd
== NULL
);
4917 r_reloc_get_target_offset (const r_reloc
*r_rel
)
4919 bfd_vma target_offset
;
4920 unsigned long r_symndx
;
4922 BFD_ASSERT (!r_reloc_is_const (r_rel
));
4923 r_symndx
= ELF32_R_SYM (r_rel
->rela
.r_info
);
4924 target_offset
= get_elf_r_symndx_offset (r_rel
->abfd
, r_symndx
);
4925 return (target_offset
+ r_rel
->rela
.r_addend
);
4929 static struct elf_link_hash_entry
*
4930 r_reloc_get_hash_entry (const r_reloc
*r_rel
)
4932 unsigned long r_symndx
= ELF32_R_SYM (r_rel
->rela
.r_info
);
4933 return get_elf_r_symndx_hash_entry (r_rel
->abfd
, r_symndx
);
4938 r_reloc_get_section (const r_reloc
*r_rel
)
4940 unsigned long r_symndx
= ELF32_R_SYM (r_rel
->rela
.r_info
);
4941 return get_elf_r_symndx_section (r_rel
->abfd
, r_symndx
);
4946 r_reloc_is_defined (const r_reloc
*r_rel
)
4952 sec
= r_reloc_get_section (r_rel
);
4953 if (sec
== bfd_abs_section_ptr
4954 || sec
== bfd_com_section_ptr
4955 || sec
== bfd_und_section_ptr
)
4962 r_reloc_init (r_reloc
*r_rel
,
4964 Elf_Internal_Rela
*irel
,
4966 bfd_size_type content_length
)
4969 reloc_howto_type
*howto
;
4973 r_rel
->rela
= *irel
;
4975 r_rel
->target_offset
= r_reloc_get_target_offset (r_rel
);
4976 r_rel
->virtual_offset
= 0;
4977 r_type
= ELF32_R_TYPE (r_rel
->rela
.r_info
);
4978 howto
= &elf_howto_table
[r_type
];
4979 if (howto
->partial_inplace
)
4981 bfd_vma inplace_val
;
4982 BFD_ASSERT (r_rel
->rela
.r_offset
< content_length
);
4984 inplace_val
= bfd_get_32 (abfd
, &contents
[r_rel
->rela
.r_offset
]);
4985 r_rel
->target_offset
+= inplace_val
;
4989 memset (r_rel
, 0, sizeof (r_reloc
));
4996 print_r_reloc (FILE *fp
, const r_reloc
*r_rel
)
4998 if (r_reloc_is_defined (r_rel
))
5000 asection
*sec
= r_reloc_get_section (r_rel
);
5001 fprintf (fp
, " %s(%s + ", sec
->owner
->filename
, sec
->name
);
5003 else if (r_reloc_get_hash_entry (r_rel
))
5004 fprintf (fp
, " %s + ", r_reloc_get_hash_entry (r_rel
)->root
.root
.string
);
5006 fprintf (fp
, " ?? + ");
5008 fprintf_vma (fp
, r_rel
->target_offset
);
5009 if (r_rel
->virtual_offset
)
5011 fprintf (fp
, " + ");
5012 fprintf_vma (fp
, r_rel
->virtual_offset
);
5021 /* source_reloc: relocations that reference literals. */
5023 /* To determine whether literals can be coalesced, we need to first
5024 record all the relocations that reference the literals. The
5025 source_reloc structure below is used for this purpose. The
5026 source_reloc entries are kept in a per-literal-section array, sorted
5027 by offset within the literal section (i.e., target offset).
5029 The source_sec and r_rel.rela.r_offset fields identify the source of
5030 the relocation. The r_rel field records the relocation value, i.e.,
5031 the offset of the literal being referenced. The opnd field is needed
5032 to determine the range of the immediate field to which the relocation
5033 applies, so we can determine whether another literal with the same
5034 value is within range. The is_null field is true when the relocation
5035 is being removed (e.g., when an L32R is being removed due to a CALLX
5036 that is converted to a direct CALL). */
5038 typedef struct source_reloc_struct source_reloc
;
5040 struct source_reloc_struct
5042 asection
*source_sec
;
5044 xtensa_opcode opcode
;
5046 bfd_boolean is_null
;
5047 bfd_boolean is_abs_literal
;
5052 init_source_reloc (source_reloc
*reloc
,
5053 asection
*source_sec
,
5054 const r_reloc
*r_rel
,
5055 xtensa_opcode opcode
,
5057 bfd_boolean is_abs_literal
)
5059 reloc
->source_sec
= source_sec
;
5060 reloc
->r_rel
= *r_rel
;
5061 reloc
->opcode
= opcode
;
5063 reloc
->is_null
= FALSE
;
5064 reloc
->is_abs_literal
= is_abs_literal
;
5068 /* Find the source_reloc for a particular source offset and relocation
5069 type. Note that the array is sorted by _target_ offset, so this is
5070 just a linear search. */
5072 static source_reloc
*
5073 find_source_reloc (source_reloc
*src_relocs
,
5076 Elf_Internal_Rela
*irel
)
5080 for (i
= 0; i
< src_count
; i
++)
5082 if (src_relocs
[i
].source_sec
== sec
5083 && src_relocs
[i
].r_rel
.rela
.r_offset
== irel
->r_offset
5084 && (ELF32_R_TYPE (src_relocs
[i
].r_rel
.rela
.r_info
)
5085 == ELF32_R_TYPE (irel
->r_info
)))
5086 return &src_relocs
[i
];
5094 source_reloc_compare (const void *ap
, const void *bp
)
5096 const source_reloc
*a
= (const source_reloc
*) ap
;
5097 const source_reloc
*b
= (const source_reloc
*) bp
;
5099 if (a
->r_rel
.target_offset
!= b
->r_rel
.target_offset
)
5100 return (a
->r_rel
.target_offset
- b
->r_rel
.target_offset
);
5102 /* We don't need to sort on these criteria for correctness,
5103 but enforcing a more strict ordering prevents unstable qsort
5104 from behaving differently with different implementations.
5105 Without the code below we get correct but different results
5106 on Solaris 2.7 and 2.8. We would like to always produce the
5107 same results no matter the host. */
5109 if ((!a
->is_null
) - (!b
->is_null
))
5110 return ((!a
->is_null
) - (!b
->is_null
));
5111 return internal_reloc_compare (&a
->r_rel
.rela
, &b
->r_rel
.rela
);
5115 /* Literal values and value hash tables. */
5117 /* Literals with the same value can be coalesced. The literal_value
5118 structure records the value of a literal: the "r_rel" field holds the
5119 information from the relocation on the literal (if there is one) and
5120 the "value" field holds the contents of the literal word itself.
5122 The value_map structure records a literal value along with the
5123 location of a literal holding that value. The value_map hash table
5124 is indexed by the literal value, so that we can quickly check if a
5125 particular literal value has been seen before and is thus a candidate
5128 typedef struct literal_value_struct literal_value
;
5129 typedef struct value_map_struct value_map
;
5130 typedef struct value_map_hash_table_struct value_map_hash_table
;
5132 struct literal_value_struct
5135 unsigned long value
;
5136 bfd_boolean is_abs_literal
;
5139 struct value_map_struct
5141 literal_value val
; /* The literal value. */
5142 r_reloc loc
; /* Location of the literal. */
5146 struct value_map_hash_table_struct
5148 unsigned bucket_count
;
5149 value_map
**buckets
;
5151 bfd_boolean has_last_loc
;
5157 init_literal_value (literal_value
*lit
,
5158 const r_reloc
*r_rel
,
5159 unsigned long value
,
5160 bfd_boolean is_abs_literal
)
5162 lit
->r_rel
= *r_rel
;
5164 lit
->is_abs_literal
= is_abs_literal
;
5169 literal_value_equal (const literal_value
*src1
,
5170 const literal_value
*src2
,
5171 bfd_boolean final_static_link
)
5173 struct elf_link_hash_entry
*h1
, *h2
;
5175 if (r_reloc_is_const (&src1
->r_rel
) != r_reloc_is_const (&src2
->r_rel
))
5178 if (r_reloc_is_const (&src1
->r_rel
))
5179 return (src1
->value
== src2
->value
);
5181 if (ELF32_R_TYPE (src1
->r_rel
.rela
.r_info
)
5182 != ELF32_R_TYPE (src2
->r_rel
.rela
.r_info
))
5185 if (src1
->r_rel
.target_offset
!= src2
->r_rel
.target_offset
)
5188 if (src1
->r_rel
.virtual_offset
!= src2
->r_rel
.virtual_offset
)
5191 if (src1
->value
!= src2
->value
)
5194 /* Now check for the same section (if defined) or the same elf_hash
5195 (if undefined or weak). */
5196 h1
= r_reloc_get_hash_entry (&src1
->r_rel
);
5197 h2
= r_reloc_get_hash_entry (&src2
->r_rel
);
5198 if (r_reloc_is_defined (&src1
->r_rel
)
5199 && (final_static_link
5200 || ((!h1
|| h1
->root
.type
!= bfd_link_hash_defweak
)
5201 && (!h2
|| h2
->root
.type
!= bfd_link_hash_defweak
))))
5203 if (r_reloc_get_section (&src1
->r_rel
)
5204 != r_reloc_get_section (&src2
->r_rel
))
5209 /* Require that the hash entries (i.e., symbols) be identical. */
5210 if (h1
!= h2
|| h1
== 0)
5214 if (src1
->is_abs_literal
!= src2
->is_abs_literal
)
5221 /* Must be power of 2. */
5222 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
5224 static value_map_hash_table
*
5225 value_map_hash_table_init (void)
5227 value_map_hash_table
*values
;
5229 values
= (value_map_hash_table
*)
5230 bfd_zmalloc (sizeof (value_map_hash_table
));
5231 values
->bucket_count
= INITIAL_HASH_RELOC_BUCKET_COUNT
;
5233 values
->buckets
= (value_map
**)
5234 bfd_zmalloc (sizeof (value_map
*) * values
->bucket_count
);
5235 if (values
->buckets
== NULL
)
5240 values
->has_last_loc
= FALSE
;
5247 value_map_hash_table_delete (value_map_hash_table
*table
)
5249 free (table
->buckets
);
5255 hash_bfd_vma (bfd_vma val
)
5257 return (val
>> 2) + (val
>> 10);
5262 literal_value_hash (const literal_value
*src
)
5266 hash_val
= hash_bfd_vma (src
->value
);
5267 if (!r_reloc_is_const (&src
->r_rel
))
5271 hash_val
+= hash_bfd_vma (src
->is_abs_literal
* 1000);
5272 hash_val
+= hash_bfd_vma (src
->r_rel
.target_offset
);
5273 hash_val
+= hash_bfd_vma (src
->r_rel
.virtual_offset
);
5275 /* Now check for the same section and the same elf_hash. */
5276 if (r_reloc_is_defined (&src
->r_rel
))
5277 sec_or_hash
= r_reloc_get_section (&src
->r_rel
);
5279 sec_or_hash
= r_reloc_get_hash_entry (&src
->r_rel
);
5280 hash_val
+= hash_bfd_vma ((bfd_vma
) (size_t) sec_or_hash
);
5286 /* Check if the specified literal_value has been seen before. */
5289 value_map_get_cached_value (value_map_hash_table
*map
,
5290 const literal_value
*val
,
5291 bfd_boolean final_static_link
)
5297 idx
= literal_value_hash (val
);
5298 idx
= idx
& (map
->bucket_count
- 1);
5299 bucket
= map
->buckets
[idx
];
5300 for (map_e
= bucket
; map_e
; map_e
= map_e
->next
)
5302 if (literal_value_equal (&map_e
->val
, val
, final_static_link
))
5309 /* Record a new literal value. It is illegal to call this if VALUE
5310 already has an entry here. */
5313 add_value_map (value_map_hash_table
*map
,
5314 const literal_value
*val
,
5316 bfd_boolean final_static_link
)
5318 value_map
**bucket_p
;
5321 value_map
*val_e
= (value_map
*) bfd_zmalloc (sizeof (value_map
));
5324 bfd_set_error (bfd_error_no_memory
);
5328 BFD_ASSERT (!value_map_get_cached_value (map
, val
, final_static_link
));
5332 idx
= literal_value_hash (val
);
5333 idx
= idx
& (map
->bucket_count
- 1);
5334 bucket_p
= &map
->buckets
[idx
];
5336 val_e
->next
= *bucket_p
;
5339 /* FIXME: Consider resizing the hash table if we get too many entries. */
5345 /* Lists of text actions (ta_) for narrowing, widening, longcall
5346 conversion, space fill, code & literal removal, etc. */
5348 /* The following text actions are generated:
5350 "ta_remove_insn" remove an instruction or instructions
5351 "ta_remove_longcall" convert longcall to call
5352 "ta_convert_longcall" convert longcall to nop/call
5353 "ta_narrow_insn" narrow a wide instruction
5354 "ta_widen" widen a narrow instruction
5355 "ta_fill" add fill or remove fill
5356 removed < 0 is a fill; branches to the fill address will be
5357 changed to address + fill size (e.g., address - removed)
5358 removed >= 0 branches to the fill address will stay unchanged
5359 "ta_remove_literal" remove a literal; this action is
5360 indicated when a literal is removed
5362 "ta_add_literal" insert a new literal; this action is
5363 indicated when a literal has been moved.
5364 It may use a virtual_offset because
5365 multiple literals can be placed at the
5368 For each of these text actions, we also record the number of bytes
5369 removed by performing the text action. In the case of a "ta_widen"
5370 or a "ta_fill" that adds space, the removed_bytes will be negative. */
5372 typedef struct text_action_struct text_action
;
5373 typedef struct text_action_list_struct text_action_list
;
5374 typedef enum text_action_enum_t text_action_t
;
5376 enum text_action_enum_t
5379 ta_remove_insn
, /* removed = -size */
5380 ta_remove_longcall
, /* removed = -size */
5381 ta_convert_longcall
, /* removed = 0 */
5382 ta_narrow_insn
, /* removed = -1 */
5383 ta_widen_insn
, /* removed = +1 */
5384 ta_fill
, /* removed = +size */
5390 /* Structure for a text action record. */
5391 struct text_action_struct
5393 text_action_t action
;
5394 asection
*sec
; /* Optional */
5396 bfd_vma virtual_offset
; /* Zero except for adding literals. */
5398 literal_value value
; /* Only valid when adding literals. */
5404 /* List of all of the actions taken on a text section. */
5405 struct text_action_list_struct
5411 static text_action
*
5412 find_fill_action (text_action_list
*l
, asection
*sec
, bfd_vma offset
)
5416 /* It is not necessary to fill at the end of a section. */
5417 if (sec
->size
== offset
)
5420 for (m_p
= &l
->head
; *m_p
&& (*m_p
)->offset
<= offset
; m_p
= &(*m_p
)->next
)
5422 text_action
*t
= *m_p
;
5423 /* When the action is another fill at the same address,
5424 just increase the size. */
5425 if (t
->offset
== offset
&& t
->action
== ta_fill
)
5433 compute_removed_action_diff (const text_action
*ta
,
5437 int removable_space
)
5440 int current_removed
= 0;
5443 current_removed
= ta
->removed_bytes
;
5445 BFD_ASSERT (ta
== NULL
|| ta
->offset
== offset
);
5446 BFD_ASSERT (ta
== NULL
|| ta
->action
== ta_fill
);
5448 /* It is not necessary to fill at the end of a section. Clean this up. */
5449 if (sec
->size
== offset
)
5450 new_removed
= removable_space
- 0;
5454 int added
= -removed
- current_removed
;
5455 /* Ignore multiples of the section alignment. */
5456 added
= ((1 << sec
->alignment_power
) - 1) & added
;
5457 new_removed
= (-added
);
5459 /* Modify for removable. */
5460 space
= removable_space
- new_removed
;
5461 new_removed
= (removable_space
5462 - (((1 << sec
->alignment_power
) - 1) & space
));
5464 return (new_removed
- current_removed
);
5469 adjust_fill_action (text_action
*ta
, int fill_diff
)
5471 ta
->removed_bytes
+= fill_diff
;
5475 /* Add a modification action to the text. For the case of adding or
5476 removing space, modify any current fill and assume that
5477 "unreachable_space" bytes can be freely contracted. Note that a
5478 negative removed value is a fill. */
5481 text_action_add (text_action_list
*l
,
5482 text_action_t action
,
5490 /* It is not necessary to fill at the end of a section. */
5491 if (action
== ta_fill
&& sec
->size
== offset
)
5494 /* It is not necessary to fill 0 bytes. */
5495 if (action
== ta_fill
&& removed
== 0)
5498 for (m_p
= &l
->head
; *m_p
&& (*m_p
)->offset
<= offset
; m_p
= &(*m_p
)->next
)
5500 text_action
*t
= *m_p
;
5502 if (action
== ta_fill
)
5504 /* When the action is another fill at the same address,
5505 just increase the size. */
5506 if (t
->offset
== offset
&& t
->action
== ta_fill
)
5508 t
->removed_bytes
+= removed
;
5511 /* Fills need to happen before widens so that we don't
5512 insert fill bytes into the instruction stream. */
5513 if (t
->offset
== offset
&& t
->action
== ta_widen_insn
)
5518 /* Create a new record and fill it up. */
5519 ta
= (text_action
*) bfd_zmalloc (sizeof (text_action
));
5520 ta
->action
= action
;
5522 ta
->offset
= offset
;
5523 ta
->removed_bytes
= removed
;
5530 text_action_add_literal (text_action_list
*l
,
5531 text_action_t action
,
5533 const literal_value
*value
,
5538 asection
*sec
= r_reloc_get_section (loc
);
5539 bfd_vma offset
= loc
->target_offset
;
5540 bfd_vma virtual_offset
= loc
->virtual_offset
;
5542 BFD_ASSERT (action
== ta_add_literal
);
5544 for (m_p
= &l
->head
; *m_p
!= NULL
; m_p
= &(*m_p
)->next
)
5546 if ((*m_p
)->offset
> offset
5547 && ((*m_p
)->offset
!= offset
5548 || (*m_p
)->virtual_offset
> virtual_offset
))
5552 /* Create a new record and fill it up. */
5553 ta
= (text_action
*) bfd_zmalloc (sizeof (text_action
));
5554 ta
->action
= action
;
5556 ta
->offset
= offset
;
5557 ta
->virtual_offset
= virtual_offset
;
5559 ta
->removed_bytes
= removed
;
5565 /* Find the total offset adjustment for the relaxations specified by
5566 text_actions, beginning from a particular starting action. This is
5567 typically used from offset_with_removed_text to search an entire list of
5568 actions, but it may also be called directly when adjusting adjacent offsets
5569 so that each search may begin where the previous one left off. */
5572 removed_by_actions (text_action
**p_start_action
,
5574 bfd_boolean before_fill
)
5579 r
= *p_start_action
;
5582 if (r
->offset
> offset
)
5585 if (r
->offset
== offset
5586 && (before_fill
|| r
->action
!= ta_fill
|| r
->removed_bytes
>= 0))
5589 removed
+= r
->removed_bytes
;
5594 *p_start_action
= r
;
5600 offset_with_removed_text (text_action_list
*action_list
, bfd_vma offset
)
5602 text_action
*r
= action_list
->head
;
5603 return offset
- removed_by_actions (&r
, offset
, FALSE
);
5608 action_list_count (text_action_list
*action_list
)
5610 text_action
*r
= action_list
->head
;
5612 for (r
= action_list
->head
; r
!= NULL
; r
= r
->next
)
5620 /* The find_insn_action routine will only find non-fill actions. */
5622 static text_action
*
5623 find_insn_action (text_action_list
*action_list
, bfd_vma offset
)
5626 for (t
= action_list
->head
; t
; t
= t
->next
)
5628 if (t
->offset
== offset
)
5635 case ta_remove_insn
:
5636 case ta_remove_longcall
:
5637 case ta_convert_longcall
:
5638 case ta_narrow_insn
:
5641 case ta_remove_literal
:
5642 case ta_add_literal
:
5655 print_action_list (FILE *fp
, text_action_list
*action_list
)
5659 fprintf (fp
, "Text Action\n");
5660 for (r
= action_list
->head
; r
!= NULL
; r
= r
->next
)
5662 const char *t
= "unknown";
5665 case ta_remove_insn
:
5666 t
= "remove_insn"; break;
5667 case ta_remove_longcall
:
5668 t
= "remove_longcall"; break;
5669 case ta_convert_longcall
:
5670 t
= "convert_longcall"; break;
5671 case ta_narrow_insn
:
5672 t
= "narrow_insn"; break;
5674 t
= "widen_insn"; break;
5679 case ta_remove_literal
:
5680 t
= "remove_literal"; break;
5681 case ta_add_literal
:
5682 t
= "add_literal"; break;
5685 fprintf (fp
, "%s: %s[0x%lx] \"%s\" %d\n",
5686 r
->sec
->owner
->filename
,
5687 r
->sec
->name
, r
->offset
, t
, r
->removed_bytes
);
5694 /* Lists of literals being coalesced or removed. */
5696 /* In the usual case, the literal identified by "from" is being
5697 coalesced with another literal identified by "to". If the literal is
5698 unused and is being removed altogether, "to.abfd" will be NULL.
5699 The removed_literal entries are kept on a per-section list, sorted
5700 by the "from" offset field. */
5702 typedef struct removed_literal_struct removed_literal
;
5703 typedef struct removed_literal_list_struct removed_literal_list
;
5705 struct removed_literal_struct
5709 removed_literal
*next
;
5712 struct removed_literal_list_struct
5714 removed_literal
*head
;
5715 removed_literal
*tail
;
5719 /* Record that the literal at "from" is being removed. If "to" is not
5720 NULL, the "from" literal is being coalesced with the "to" literal. */
5723 add_removed_literal (removed_literal_list
*removed_list
,
5724 const r_reloc
*from
,
5727 removed_literal
*r
, *new_r
, *next_r
;
5729 new_r
= (removed_literal
*) bfd_zmalloc (sizeof (removed_literal
));
5731 new_r
->from
= *from
;
5735 new_r
->to
.abfd
= NULL
;
5738 r
= removed_list
->head
;
5741 removed_list
->head
= new_r
;
5742 removed_list
->tail
= new_r
;
5744 /* Special check for common case of append. */
5745 else if (removed_list
->tail
->from
.target_offset
< from
->target_offset
)
5747 removed_list
->tail
->next
= new_r
;
5748 removed_list
->tail
= new_r
;
5752 while (r
->from
.target_offset
< from
->target_offset
&& r
->next
)
5758 new_r
->next
= next_r
;
5760 removed_list
->tail
= new_r
;
5765 /* Check if the list of removed literals contains an entry for the
5766 given address. Return the entry if found. */
5768 static removed_literal
*
5769 find_removed_literal (removed_literal_list
*removed_list
, bfd_vma addr
)
5771 removed_literal
*r
= removed_list
->head
;
5772 while (r
&& r
->from
.target_offset
< addr
)
5774 if (r
&& r
->from
.target_offset
== addr
)
5783 print_removed_literals (FILE *fp
, removed_literal_list
*removed_list
)
5786 r
= removed_list
->head
;
5788 fprintf (fp
, "Removed Literals\n");
5789 for (; r
!= NULL
; r
= r
->next
)
5791 print_r_reloc (fp
, &r
->from
);
5792 fprintf (fp
, " => ");
5793 if (r
->to
.abfd
== NULL
)
5794 fprintf (fp
, "REMOVED");
5796 print_r_reloc (fp
, &r
->to
);
5804 /* Per-section data for relaxation. */
5806 typedef struct reloc_bfd_fix_struct reloc_bfd_fix
;
5808 struct xtensa_relax_info_struct
5810 bfd_boolean is_relaxable_literal_section
;
5811 bfd_boolean is_relaxable_asm_section
;
5812 int visited
; /* Number of times visited. */
5814 source_reloc
*src_relocs
; /* Array[src_count]. */
5816 int src_next
; /* Next src_relocs entry to assign. */
5818 removed_literal_list removed_list
;
5819 text_action_list action_list
;
5821 reloc_bfd_fix
*fix_list
;
5822 reloc_bfd_fix
*fix_array
;
5823 unsigned fix_array_count
;
5825 /* Support for expanding the reloc array that is stored
5826 in the section structure. If the relocations have been
5827 reallocated, the newly allocated relocations will be referenced
5828 here along with the actual size allocated. The relocation
5829 count will always be found in the section structure. */
5830 Elf_Internal_Rela
*allocated_relocs
;
5831 unsigned relocs_count
;
5832 unsigned allocated_relocs_count
;
5835 struct elf_xtensa_section_data
5837 struct bfd_elf_section_data elf
;
5838 xtensa_relax_info relax_info
;
5843 elf_xtensa_new_section_hook (bfd
*abfd
, asection
*sec
)
5845 if (!sec
->used_by_bfd
)
5847 struct elf_xtensa_section_data
*sdata
;
5848 bfd_size_type amt
= sizeof (*sdata
);
5850 sdata
= bfd_zalloc (abfd
, amt
);
5853 sec
->used_by_bfd
= sdata
;
5856 return _bfd_elf_new_section_hook (abfd
, sec
);
5860 static xtensa_relax_info
*
5861 get_xtensa_relax_info (asection
*sec
)
5863 struct elf_xtensa_section_data
*section_data
;
5865 /* No info available if no section or if it is an output section. */
5866 if (!sec
|| sec
== sec
->output_section
)
5869 section_data
= (struct elf_xtensa_section_data
*) elf_section_data (sec
);
5870 return §ion_data
->relax_info
;
5875 init_xtensa_relax_info (asection
*sec
)
5877 xtensa_relax_info
*relax_info
= get_xtensa_relax_info (sec
);
5879 relax_info
->is_relaxable_literal_section
= FALSE
;
5880 relax_info
->is_relaxable_asm_section
= FALSE
;
5881 relax_info
->visited
= 0;
5883 relax_info
->src_relocs
= NULL
;
5884 relax_info
->src_count
= 0;
5885 relax_info
->src_next
= 0;
5887 relax_info
->removed_list
.head
= NULL
;
5888 relax_info
->removed_list
.tail
= NULL
;
5890 relax_info
->action_list
.head
= NULL
;
5892 relax_info
->fix_list
= NULL
;
5893 relax_info
->fix_array
= NULL
;
5894 relax_info
->fix_array_count
= 0;
5896 relax_info
->allocated_relocs
= NULL
;
5897 relax_info
->relocs_count
= 0;
5898 relax_info
->allocated_relocs_count
= 0;
5902 /* Coalescing literals may require a relocation to refer to a section in
5903 a different input file, but the standard relocation information
5904 cannot express that. Instead, the reloc_bfd_fix structures are used
5905 to "fix" the relocations that refer to sections in other input files.
5906 These structures are kept on per-section lists. The "src_type" field
5907 records the relocation type in case there are multiple relocations on
5908 the same location. FIXME: This is ugly; an alternative might be to
5909 add new symbols with the "owner" field to some other input file. */
5911 struct reloc_bfd_fix_struct
5915 unsigned src_type
; /* Relocation type. */
5917 asection
*target_sec
;
5918 bfd_vma target_offset
;
5919 bfd_boolean translated
;
5921 reloc_bfd_fix
*next
;
5925 static reloc_bfd_fix
*
5926 reloc_bfd_fix_init (asection
*src_sec
,
5929 asection
*target_sec
,
5930 bfd_vma target_offset
,
5931 bfd_boolean translated
)
5935 fix
= (reloc_bfd_fix
*) bfd_malloc (sizeof (reloc_bfd_fix
));
5936 fix
->src_sec
= src_sec
;
5937 fix
->src_offset
= src_offset
;
5938 fix
->src_type
= src_type
;
5939 fix
->target_sec
= target_sec
;
5940 fix
->target_offset
= target_offset
;
5941 fix
->translated
= translated
;
5948 add_fix (asection
*src_sec
, reloc_bfd_fix
*fix
)
5950 xtensa_relax_info
*relax_info
;
5952 relax_info
= get_xtensa_relax_info (src_sec
);
5953 fix
->next
= relax_info
->fix_list
;
5954 relax_info
->fix_list
= fix
;
5959 fix_compare (const void *ap
, const void *bp
)
5961 const reloc_bfd_fix
*a
= (const reloc_bfd_fix
*) ap
;
5962 const reloc_bfd_fix
*b
= (const reloc_bfd_fix
*) bp
;
5964 if (a
->src_offset
!= b
->src_offset
)
5965 return (a
->src_offset
- b
->src_offset
);
5966 return (a
->src_type
- b
->src_type
);
5971 cache_fix_array (asection
*sec
)
5973 unsigned i
, count
= 0;
5975 xtensa_relax_info
*relax_info
= get_xtensa_relax_info (sec
);
5977 if (relax_info
== NULL
)
5979 if (relax_info
->fix_list
== NULL
)
5982 for (r
= relax_info
->fix_list
; r
!= NULL
; r
= r
->next
)
5985 relax_info
->fix_array
=
5986 (reloc_bfd_fix
*) bfd_malloc (sizeof (reloc_bfd_fix
) * count
);
5987 relax_info
->fix_array_count
= count
;
5989 r
= relax_info
->fix_list
;
5990 for (i
= 0; i
< count
; i
++, r
= r
->next
)
5992 relax_info
->fix_array
[count
- 1 - i
] = *r
;
5993 relax_info
->fix_array
[count
- 1 - i
].next
= NULL
;
5996 qsort (relax_info
->fix_array
, relax_info
->fix_array_count
,
5997 sizeof (reloc_bfd_fix
), fix_compare
);
6001 static reloc_bfd_fix
*
6002 get_bfd_fix (asection
*sec
, bfd_vma offset
, unsigned type
)
6004 xtensa_relax_info
*relax_info
= get_xtensa_relax_info (sec
);
6008 if (relax_info
== NULL
)
6010 if (relax_info
->fix_list
== NULL
)
6013 if (relax_info
->fix_array
== NULL
)
6014 cache_fix_array (sec
);
6016 key
.src_offset
= offset
;
6017 key
.src_type
= type
;
6018 rv
= bsearch (&key
, relax_info
->fix_array
, relax_info
->fix_array_count
,
6019 sizeof (reloc_bfd_fix
), fix_compare
);
6024 /* Section caching. */
6026 typedef struct section_cache_struct section_cache_t
;
6028 struct section_cache_struct
6032 bfd_byte
*contents
; /* Cache of the section contents. */
6033 bfd_size_type content_length
;
6035 property_table_entry
*ptbl
; /* Cache of the section property table. */
6038 Elf_Internal_Rela
*relocs
; /* Cache of the section relocations. */
6039 unsigned reloc_count
;
6044 init_section_cache (section_cache_t
*sec_cache
)
6046 memset (sec_cache
, 0, sizeof (*sec_cache
));
6051 clear_section_cache (section_cache_t
*sec_cache
)
6055 release_contents (sec_cache
->sec
, sec_cache
->contents
);
6056 release_internal_relocs (sec_cache
->sec
, sec_cache
->relocs
);
6057 if (sec_cache
->ptbl
)
6058 free (sec_cache
->ptbl
);
6059 memset (sec_cache
, 0, sizeof (sec_cache
));
6065 section_cache_section (section_cache_t
*sec_cache
,
6067 struct bfd_link_info
*link_info
)
6070 property_table_entry
*prop_table
= NULL
;
6072 bfd_byte
*contents
= NULL
;
6073 Elf_Internal_Rela
*internal_relocs
= NULL
;
6074 bfd_size_type sec_size
;
6078 if (sec
== sec_cache
->sec
)
6082 sec_size
= bfd_get_section_limit (abfd
, sec
);
6084 /* Get the contents. */
6085 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
6086 if (contents
== NULL
&& sec_size
!= 0)
6089 /* Get the relocations. */
6090 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
6091 link_info
->keep_memory
);
6093 /* Get the entry table. */
6094 ptblsize
= xtensa_read_table_entries (abfd
, sec
, &prop_table
,
6095 XTENSA_PROP_SEC_NAME
, FALSE
);
6099 /* Fill in the new section cache. */
6100 clear_section_cache (sec_cache
);
6101 memset (sec_cache
, 0, sizeof (sec_cache
));
6103 sec_cache
->sec
= sec
;
6104 sec_cache
->contents
= contents
;
6105 sec_cache
->content_length
= sec_size
;
6106 sec_cache
->relocs
= internal_relocs
;
6107 sec_cache
->reloc_count
= sec
->reloc_count
;
6108 sec_cache
->pte_count
= ptblsize
;
6109 sec_cache
->ptbl
= prop_table
;
6114 release_contents (sec
, contents
);
6115 release_internal_relocs (sec
, internal_relocs
);
6122 /* Extended basic blocks. */
6124 /* An ebb_struct represents an Extended Basic Block. Within this
6125 range, we guarantee that all instructions are decodable, the
6126 property table entries are contiguous, and no property table
6127 specifies a segment that cannot have instructions moved. This
6128 structure contains caches of the contents, property table and
6129 relocations for the specified section for easy use. The range is
6130 specified by ranges of indices for the byte offset, property table
6131 offsets and relocation offsets. These must be consistent. */
6133 typedef struct ebb_struct ebb_t
;
6139 bfd_byte
*contents
; /* Cache of the section contents. */
6140 bfd_size_type content_length
;
6142 property_table_entry
*ptbl
; /* Cache of the section property table. */
6145 Elf_Internal_Rela
*relocs
; /* Cache of the section relocations. */
6146 unsigned reloc_count
;
6148 bfd_vma start_offset
; /* Offset in section. */
6149 unsigned start_ptbl_idx
; /* Offset in the property table. */
6150 unsigned start_reloc_idx
; /* Offset in the relocations. */
6153 unsigned end_ptbl_idx
;
6154 unsigned end_reloc_idx
;
6156 bfd_boolean ends_section
; /* Is this the last ebb in a section? */
6158 /* The unreachable property table at the end of this set of blocks;
6159 NULL if the end is not an unreachable block. */
6160 property_table_entry
*ends_unreachable
;
6164 enum ebb_target_enum
6167 EBB_DESIRE_TGT_ALIGN
,
6168 EBB_REQUIRE_TGT_ALIGN
,
6169 EBB_REQUIRE_LOOP_ALIGN
,
6174 /* proposed_action_struct is similar to the text_action_struct except
6175 that is represents a potential transformation, not one that will
6176 occur. We build a list of these for an extended basic block
6177 and use them to compute the actual actions desired. We must be
6178 careful that the entire set of actual actions we perform do not
6179 break any relocations that would fit if the actions were not
6182 typedef struct proposed_action_struct proposed_action
;
6184 struct proposed_action_struct
6186 enum ebb_target_enum align_type
; /* for the target alignment */
6187 bfd_vma alignment_pow
;
6188 text_action_t action
;
6191 bfd_boolean do_action
; /* If false, then we will not perform the action. */
6195 /* The ebb_constraint_struct keeps a set of proposed actions for an
6196 extended basic block. */
6198 typedef struct ebb_constraint_struct ebb_constraint
;
6200 struct ebb_constraint_struct
6203 bfd_boolean start_movable
;
6205 /* Bytes of extra space at the beginning if movable. */
6206 int start_extra_space
;
6208 enum ebb_target_enum start_align
;
6210 bfd_boolean end_movable
;
6212 /* Bytes of extra space at the end if movable. */
6213 int end_extra_space
;
6215 unsigned action_count
;
6216 unsigned action_allocated
;
6218 /* Array of proposed actions. */
6219 proposed_action
*actions
;
6221 /* Action alignments -- one for each proposed action. */
6222 enum ebb_target_enum
*action_aligns
;
6227 init_ebb_constraint (ebb_constraint
*c
)
6229 memset (c
, 0, sizeof (ebb_constraint
));
6234 free_ebb_constraint (ebb_constraint
*c
)
6242 init_ebb (ebb_t
*ebb
,
6245 bfd_size_type content_length
,
6246 property_table_entry
*prop_table
,
6248 Elf_Internal_Rela
*internal_relocs
,
6249 unsigned reloc_count
)
6251 memset (ebb
, 0, sizeof (ebb_t
));
6253 ebb
->contents
= contents
;
6254 ebb
->content_length
= content_length
;
6255 ebb
->ptbl
= prop_table
;
6256 ebb
->pte_count
= ptblsize
;
6257 ebb
->relocs
= internal_relocs
;
6258 ebb
->reloc_count
= reloc_count
;
6259 ebb
->start_offset
= 0;
6260 ebb
->end_offset
= ebb
->content_length
- 1;
6261 ebb
->start_ptbl_idx
= 0;
6262 ebb
->end_ptbl_idx
= ptblsize
;
6263 ebb
->start_reloc_idx
= 0;
6264 ebb
->end_reloc_idx
= reloc_count
;
6268 /* Extend the ebb to all decodable contiguous sections. The algorithm
6269 for building a basic block around an instruction is to push it
6270 forward until we hit the end of a section, an unreachable block or
6271 a block that cannot be transformed. Then we push it backwards
6272 searching for similar conditions. */
6274 static bfd_boolean
extend_ebb_bounds_forward (ebb_t
*);
6275 static bfd_boolean
extend_ebb_bounds_backward (ebb_t
*);
6276 static bfd_size_type insn_block_decodable_len
6277 (bfd_byte
*, bfd_size_type
, bfd_vma
, bfd_size_type
);
6280 extend_ebb_bounds (ebb_t
*ebb
)
6282 if (!extend_ebb_bounds_forward (ebb
))
6284 if (!extend_ebb_bounds_backward (ebb
))
6291 extend_ebb_bounds_forward (ebb_t
*ebb
)
6293 property_table_entry
*the_entry
, *new_entry
;
6295 the_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
];
6297 /* Stop when (1) we cannot decode an instruction, (2) we are at
6298 the end of the property tables, (3) we hit a non-contiguous property
6299 table entry, (4) we hit a NO_TRANSFORM region. */
6304 bfd_size_type insn_block_len
;
6306 entry_end
= the_entry
->address
- ebb
->sec
->vma
+ the_entry
->size
;
6308 insn_block_decodable_len (ebb
->contents
, ebb
->content_length
,
6310 entry_end
- ebb
->end_offset
);
6311 if (insn_block_len
!= (entry_end
- ebb
->end_offset
))
6313 (*_bfd_error_handler
)
6314 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6315 ebb
->sec
->owner
, ebb
->sec
, ebb
->end_offset
+ insn_block_len
);
6318 ebb
->end_offset
+= insn_block_len
;
6320 if (ebb
->end_offset
== ebb
->sec
->size
)
6321 ebb
->ends_section
= TRUE
;
6323 /* Update the reloc counter. */
6324 while (ebb
->end_reloc_idx
+ 1 < ebb
->reloc_count
6325 && (ebb
->relocs
[ebb
->end_reloc_idx
+ 1].r_offset
6328 ebb
->end_reloc_idx
++;
6331 if (ebb
->end_ptbl_idx
+ 1 == ebb
->pte_count
)
6334 new_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
+ 1];
6335 if (((new_entry
->flags
& XTENSA_PROP_INSN
) == 0)
6336 || ((new_entry
->flags
& XTENSA_PROP_NO_TRANSFORM
) != 0)
6337 || ((the_entry
->flags
& XTENSA_PROP_ALIGN
) != 0))
6340 if (the_entry
->address
+ the_entry
->size
!= new_entry
->address
)
6343 the_entry
= new_entry
;
6344 ebb
->end_ptbl_idx
++;
6347 /* Quick check for an unreachable or end of file just at the end. */
6348 if (ebb
->end_ptbl_idx
+ 1 == ebb
->pte_count
)
6350 if (ebb
->end_offset
== ebb
->content_length
)
6351 ebb
->ends_section
= TRUE
;
6355 new_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
+ 1];
6356 if ((new_entry
->flags
& XTENSA_PROP_UNREACHABLE
) != 0
6357 && the_entry
->address
+ the_entry
->size
== new_entry
->address
)
6358 ebb
->ends_unreachable
= new_entry
;
6361 /* Any other ending requires exact alignment. */
6367 extend_ebb_bounds_backward (ebb_t
*ebb
)
6369 property_table_entry
*the_entry
, *new_entry
;
6371 the_entry
= &ebb
->ptbl
[ebb
->start_ptbl_idx
];
6373 /* Stop when (1) we cannot decode the instructions in the current entry.
6374 (2) we are at the beginning of the property tables, (3) we hit a
6375 non-contiguous property table entry, (4) we hit a NO_TRANSFORM region. */
6379 bfd_vma block_begin
;
6380 bfd_size_type insn_block_len
;
6382 block_begin
= the_entry
->address
- ebb
->sec
->vma
;
6384 insn_block_decodable_len (ebb
->contents
, ebb
->content_length
,
6386 ebb
->start_offset
- block_begin
);
6387 if (insn_block_len
!= ebb
->start_offset
- block_begin
)
6389 (*_bfd_error_handler
)
6390 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6391 ebb
->sec
->owner
, ebb
->sec
, ebb
->end_offset
+ insn_block_len
);
6394 ebb
->start_offset
-= insn_block_len
;
6396 /* Update the reloc counter. */
6397 while (ebb
->start_reloc_idx
> 0
6398 && (ebb
->relocs
[ebb
->start_reloc_idx
- 1].r_offset
6399 >= ebb
->start_offset
))
6401 ebb
->start_reloc_idx
--;
6404 if (ebb
->start_ptbl_idx
== 0)
6407 new_entry
= &ebb
->ptbl
[ebb
->start_ptbl_idx
- 1];
6408 if ((new_entry
->flags
& XTENSA_PROP_INSN
) == 0
6409 || ((new_entry
->flags
& XTENSA_PROP_NO_TRANSFORM
) != 0)
6410 || ((new_entry
->flags
& XTENSA_PROP_ALIGN
) != 0))
6412 if (new_entry
->address
+ new_entry
->size
!= the_entry
->address
)
6415 the_entry
= new_entry
;
6416 ebb
->start_ptbl_idx
--;
6422 static bfd_size_type
6423 insn_block_decodable_len (bfd_byte
*contents
,
6424 bfd_size_type content_len
,
6425 bfd_vma block_offset
,
6426 bfd_size_type block_len
)
6428 bfd_vma offset
= block_offset
;
6430 while (offset
< block_offset
+ block_len
)
6432 bfd_size_type insn_len
= 0;
6434 insn_len
= insn_decode_len (contents
, content_len
, offset
);
6436 return (offset
- block_offset
);
6439 return (offset
- block_offset
);
6444 ebb_propose_action (ebb_constraint
*c
,
6445 enum ebb_target_enum align_type
,
6446 bfd_vma alignment_pow
,
6447 text_action_t action
,
6450 bfd_boolean do_action
)
6452 proposed_action
*act
;
6454 if (c
->action_allocated
<= c
->action_count
)
6456 unsigned new_allocated
, i
;
6457 proposed_action
*new_actions
;
6459 new_allocated
= (c
->action_count
+ 2) * 2;
6460 new_actions
= (proposed_action
*)
6461 bfd_zmalloc (sizeof (proposed_action
) * new_allocated
);
6463 for (i
= 0; i
< c
->action_count
; i
++)
6464 new_actions
[i
] = c
->actions
[i
];
6467 c
->actions
= new_actions
;
6468 c
->action_allocated
= new_allocated
;
6471 act
= &c
->actions
[c
->action_count
];
6472 act
->align_type
= align_type
;
6473 act
->alignment_pow
= alignment_pow
;
6474 act
->action
= action
;
6475 act
->offset
= offset
;
6476 act
->removed_bytes
= removed_bytes
;
6477 act
->do_action
= do_action
;
6483 /* Access to internal relocations, section contents and symbols. */
6485 /* During relaxation, we need to modify relocations, section contents,
6486 and symbol definitions, and we need to keep the original values from
6487 being reloaded from the input files, i.e., we need to "pin" the
6488 modified values in memory. We also want to continue to observe the
6489 setting of the "keep-memory" flag. The following functions wrap the
6490 standard BFD functions to take care of this for us. */
6492 static Elf_Internal_Rela
*
6493 retrieve_internal_relocs (bfd
*abfd
, asection
*sec
, bfd_boolean keep_memory
)
6495 Elf_Internal_Rela
*internal_relocs
;
6497 if ((sec
->flags
& SEC_LINKER_CREATED
) != 0)
6500 internal_relocs
= elf_section_data (sec
)->relocs
;
6501 if (internal_relocs
== NULL
)
6502 internal_relocs
= (_bfd_elf_link_read_relocs
6503 (abfd
, sec
, NULL
, NULL
, keep_memory
));
6504 return internal_relocs
;
6509 pin_internal_relocs (asection
*sec
, Elf_Internal_Rela
*internal_relocs
)
6511 elf_section_data (sec
)->relocs
= internal_relocs
;
6516 release_internal_relocs (asection
*sec
, Elf_Internal_Rela
*internal_relocs
)
6519 && elf_section_data (sec
)->relocs
!= internal_relocs
)
6520 free (internal_relocs
);
6525 retrieve_contents (bfd
*abfd
, asection
*sec
, bfd_boolean keep_memory
)
6528 bfd_size_type sec_size
;
6530 sec_size
= bfd_get_section_limit (abfd
, sec
);
6531 contents
= elf_section_data (sec
)->this_hdr
.contents
;
6533 if (contents
== NULL
&& sec_size
!= 0)
6535 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
6542 elf_section_data (sec
)->this_hdr
.contents
= contents
;
6549 pin_contents (asection
*sec
, bfd_byte
*contents
)
6551 elf_section_data (sec
)->this_hdr
.contents
= contents
;
6556 release_contents (asection
*sec
, bfd_byte
*contents
)
6558 if (contents
&& elf_section_data (sec
)->this_hdr
.contents
!= contents
)
6563 static Elf_Internal_Sym
*
6564 retrieve_local_syms (bfd
*input_bfd
)
6566 Elf_Internal_Shdr
*symtab_hdr
;
6567 Elf_Internal_Sym
*isymbuf
;
6570 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6571 locsymcount
= symtab_hdr
->sh_info
;
6573 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6574 if (isymbuf
== NULL
&& locsymcount
!= 0)
6575 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6578 /* Save the symbols for this input file so they won't be read again. */
6579 if (isymbuf
&& isymbuf
!= (Elf_Internal_Sym
*) symtab_hdr
->contents
)
6580 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
6586 /* Code for link-time relaxation. */
6588 /* Initialization for relaxation: */
6589 static bfd_boolean
analyze_relocations (struct bfd_link_info
*);
6590 static bfd_boolean find_relaxable_sections
6591 (bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*);
6592 static bfd_boolean collect_source_relocs
6593 (bfd
*, asection
*, struct bfd_link_info
*);
6594 static bfd_boolean is_resolvable_asm_expansion
6595 (bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*, struct bfd_link_info
*,
6597 static Elf_Internal_Rela
*find_associated_l32r_irel
6598 (bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*, Elf_Internal_Rela
*);
6599 static bfd_boolean compute_text_actions
6600 (bfd
*, asection
*, struct bfd_link_info
*);
6601 static bfd_boolean
compute_ebb_proposed_actions (ebb_constraint
*);
6602 static bfd_boolean
compute_ebb_actions (ebb_constraint
*);
6603 static bfd_boolean check_section_ebb_pcrels_fit
6604 (bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*, const ebb_constraint
*,
6605 const xtensa_opcode
*);
6606 static bfd_boolean
check_section_ebb_reduces (const ebb_constraint
*);
6607 static void text_action_add_proposed
6608 (text_action_list
*, const ebb_constraint
*, asection
*);
6609 static int compute_fill_extra_space (property_table_entry
*);
6612 static bfd_boolean compute_removed_literals
6613 (bfd
*, asection
*, struct bfd_link_info
*, value_map_hash_table
*);
6614 static Elf_Internal_Rela
*get_irel_at_offset
6615 (asection
*, Elf_Internal_Rela
*, bfd_vma
);
6616 static bfd_boolean is_removable_literal
6617 (const source_reloc
*, int, const source_reloc
*, int, asection
*,
6618 property_table_entry
*, int);
6619 static bfd_boolean remove_dead_literal
6620 (bfd
*, asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6621 Elf_Internal_Rela
*, source_reloc
*, property_table_entry
*, int);
6622 static bfd_boolean identify_literal_placement
6623 (bfd
*, asection
*, bfd_byte
*, struct bfd_link_info
*,
6624 value_map_hash_table
*, bfd_boolean
*, Elf_Internal_Rela
*, int,
6625 source_reloc
*, property_table_entry
*, int, section_cache_t
*,
6627 static bfd_boolean
relocations_reach (source_reloc
*, int, const r_reloc
*);
6628 static bfd_boolean coalesce_shared_literal
6629 (asection
*, source_reloc
*, property_table_entry
*, int, value_map
*);
6630 static bfd_boolean move_shared_literal
6631 (asection
*, struct bfd_link_info
*, source_reloc
*, property_table_entry
*,
6632 int, const r_reloc
*, const literal_value
*, section_cache_t
*);
6635 static bfd_boolean
relax_section (bfd
*, asection
*, struct bfd_link_info
*);
6636 static bfd_boolean
translate_section_fixes (asection
*);
6637 static bfd_boolean
translate_reloc_bfd_fix (reloc_bfd_fix
*);
6638 static asection
*translate_reloc (const r_reloc
*, r_reloc
*, asection
*);
6639 static void shrink_dynamic_reloc_sections
6640 (struct bfd_link_info
*, bfd
*, asection
*, Elf_Internal_Rela
*);
6641 static bfd_boolean move_literal
6642 (bfd
*, struct bfd_link_info
*, asection
*, bfd_vma
, bfd_byte
*,
6643 xtensa_relax_info
*, Elf_Internal_Rela
**, const literal_value
*);
6644 static bfd_boolean relax_property_section
6645 (bfd
*, asection
*, struct bfd_link_info
*);
6648 static bfd_boolean
relax_section_symbols (bfd
*, asection
*);
6652 elf_xtensa_relax_section (bfd
*abfd
,
6654 struct bfd_link_info
*link_info
,
6657 static value_map_hash_table
*values
= NULL
;
6658 static bfd_boolean relocations_analyzed
= FALSE
;
6659 xtensa_relax_info
*relax_info
;
6661 if (!relocations_analyzed
)
6663 /* Do some overall initialization for relaxation. */
6664 values
= value_map_hash_table_init ();
6667 relaxing_section
= TRUE
;
6668 if (!analyze_relocations (link_info
))
6670 relocations_analyzed
= TRUE
;
6674 /* Don't mess with linker-created sections. */
6675 if ((sec
->flags
& SEC_LINKER_CREATED
) != 0)
6678 relax_info
= get_xtensa_relax_info (sec
);
6679 BFD_ASSERT (relax_info
!= NULL
);
6681 switch (relax_info
->visited
)
6684 /* Note: It would be nice to fold this pass into
6685 analyze_relocations, but it is important for this step that the
6686 sections be examined in link order. */
6687 if (!compute_removed_literals (abfd
, sec
, link_info
, values
))
6694 value_map_hash_table_delete (values
);
6696 if (!relax_section (abfd
, sec
, link_info
))
6702 if (!relax_section_symbols (abfd
, sec
))
6707 relax_info
->visited
++;
6712 /* Initialization for relaxation. */
6714 /* This function is called once at the start of relaxation. It scans
6715 all the input sections and marks the ones that are relaxable (i.e.,
6716 literal sections with L32R relocations against them), and then
6717 collects source_reloc information for all the relocations against
6718 those relaxable sections. During this process, it also detects
6719 longcalls, i.e., calls relaxed by the assembler into indirect
6720 calls, that can be optimized back into direct calls. Within each
6721 extended basic block (ebb) containing an optimized longcall, it
6722 computes a set of "text actions" that can be performed to remove
6723 the L32R associated with the longcall while optionally preserving
6724 branch target alignments. */
6727 analyze_relocations (struct bfd_link_info
*link_info
)
6731 bfd_boolean is_relaxable
= FALSE
;
6733 /* Initialize the per-section relaxation info. */
6734 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6735 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6737 init_xtensa_relax_info (sec
);
6740 /* Mark relaxable sections (and count relocations against each one). */
6741 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6742 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6744 if (!find_relaxable_sections (abfd
, sec
, link_info
, &is_relaxable
))
6748 /* Bail out if there are no relaxable sections. */
6752 /* Allocate space for source_relocs. */
6753 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6754 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6756 xtensa_relax_info
*relax_info
;
6758 relax_info
= get_xtensa_relax_info (sec
);
6759 if (relax_info
->is_relaxable_literal_section
6760 || relax_info
->is_relaxable_asm_section
)
6762 relax_info
->src_relocs
= (source_reloc
*)
6763 bfd_malloc (relax_info
->src_count
* sizeof (source_reloc
));
6766 relax_info
->src_count
= 0;
6769 /* Collect info on relocations against each relaxable section. */
6770 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6771 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6773 if (!collect_source_relocs (abfd
, sec
, link_info
))
6777 /* Compute the text actions. */
6778 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6779 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6781 if (!compute_text_actions (abfd
, sec
, link_info
))
6789 /* Find all the sections that might be relaxed. The motivation for
6790 this pass is that collect_source_relocs() needs to record _all_ the
6791 relocations that target each relaxable section. That is expensive
6792 and unnecessary unless the target section is actually going to be
6793 relaxed. This pass identifies all such sections by checking if
6794 they have L32Rs pointing to them. In the process, the total number
6795 of relocations targeting each section is also counted so that we
6796 know how much space to allocate for source_relocs against each
6797 relaxable literal section. */
6800 find_relaxable_sections (bfd
*abfd
,
6802 struct bfd_link_info
*link_info
,
6803 bfd_boolean
*is_relaxable_p
)
6805 Elf_Internal_Rela
*internal_relocs
;
6807 bfd_boolean ok
= TRUE
;
6809 xtensa_relax_info
*source_relax_info
;
6810 bfd_boolean is_l32r_reloc
;
6812 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
6813 link_info
->keep_memory
);
6814 if (internal_relocs
== NULL
)
6817 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
6818 if (contents
== NULL
&& sec
->size
!= 0)
6824 source_relax_info
= get_xtensa_relax_info (sec
);
6825 for (i
= 0; i
< sec
->reloc_count
; i
++)
6827 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6829 asection
*target_sec
;
6830 xtensa_relax_info
*target_relax_info
;
6832 /* If this section has not already been marked as "relaxable", and
6833 if it contains any ASM_EXPAND relocations (marking expanded
6834 longcalls) that can be optimized into direct calls, then mark
6835 the section as "relaxable". */
6836 if (source_relax_info
6837 && !source_relax_info
->is_relaxable_asm_section
6838 && ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_ASM_EXPAND
)
6840 bfd_boolean is_reachable
= FALSE
;
6841 if (is_resolvable_asm_expansion (abfd
, sec
, contents
, irel
,
6842 link_info
, &is_reachable
)
6845 source_relax_info
->is_relaxable_asm_section
= TRUE
;
6846 *is_relaxable_p
= TRUE
;
6850 r_reloc_init (&r_rel
, abfd
, irel
, contents
,
6851 bfd_get_section_limit (abfd
, sec
));
6853 target_sec
= r_reloc_get_section (&r_rel
);
6854 target_relax_info
= get_xtensa_relax_info (target_sec
);
6855 if (!target_relax_info
)
6858 /* Count PC-relative operand relocations against the target section.
6859 Note: The conditions tested here must match the conditions under
6860 which init_source_reloc is called in collect_source_relocs(). */
6861 is_l32r_reloc
= FALSE
;
6862 if (is_operand_relocation (ELF32_R_TYPE (irel
->r_info
)))
6864 xtensa_opcode opcode
=
6865 get_relocation_opcode (abfd
, sec
, contents
, irel
);
6866 if (opcode
!= XTENSA_UNDEFINED
)
6868 is_l32r_reloc
= (opcode
== get_l32r_opcode ());
6869 if (!is_alt_relocation (ELF32_R_TYPE (irel
->r_info
))
6871 target_relax_info
->src_count
++;
6875 if (is_l32r_reloc
&& r_reloc_is_defined (&r_rel
))
6877 /* Mark the target section as relaxable. */
6878 target_relax_info
->is_relaxable_literal_section
= TRUE
;
6879 *is_relaxable_p
= TRUE
;
6884 release_contents (sec
, contents
);
6885 release_internal_relocs (sec
, internal_relocs
);
6890 /* Record _all_ the relocations that point to relaxable sections, and
6891 get rid of ASM_EXPAND relocs by either converting them to
6892 ASM_SIMPLIFY or by removing them. */
6895 collect_source_relocs (bfd
*abfd
,
6897 struct bfd_link_info
*link_info
)
6899 Elf_Internal_Rela
*internal_relocs
;
6901 bfd_boolean ok
= TRUE
;
6903 bfd_size_type sec_size
;
6905 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
6906 link_info
->keep_memory
);
6907 if (internal_relocs
== NULL
)
6910 sec_size
= bfd_get_section_limit (abfd
, sec
);
6911 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
6912 if (contents
== NULL
&& sec_size
!= 0)
6918 /* Record relocations against relaxable literal sections. */
6919 for (i
= 0; i
< sec
->reloc_count
; i
++)
6921 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6923 asection
*target_sec
;
6924 xtensa_relax_info
*target_relax_info
;
6926 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
6928 target_sec
= r_reloc_get_section (&r_rel
);
6929 target_relax_info
= get_xtensa_relax_info (target_sec
);
6931 if (target_relax_info
6932 && (target_relax_info
->is_relaxable_literal_section
6933 || target_relax_info
->is_relaxable_asm_section
))
6935 xtensa_opcode opcode
= XTENSA_UNDEFINED
;
6937 bfd_boolean is_abs_literal
= FALSE
;
6939 if (is_alt_relocation (ELF32_R_TYPE (irel
->r_info
)))
6941 /* None of the current alternate relocs are PC-relative,
6942 and only PC-relative relocs matter here. However, we
6943 still need to record the opcode for literal
6945 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
6946 if (opcode
== get_l32r_opcode ())
6948 is_abs_literal
= TRUE
;
6952 opcode
= XTENSA_UNDEFINED
;
6954 else if (is_operand_relocation (ELF32_R_TYPE (irel
->r_info
)))
6956 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
6957 opnd
= get_relocation_opnd (opcode
, ELF32_R_TYPE (irel
->r_info
));
6960 if (opcode
!= XTENSA_UNDEFINED
)
6962 int src_next
= target_relax_info
->src_next
++;
6963 source_reloc
*s_reloc
= &target_relax_info
->src_relocs
[src_next
];
6965 init_source_reloc (s_reloc
, sec
, &r_rel
, opcode
, opnd
,
6971 /* Now get rid of ASM_EXPAND relocations. At this point, the
6972 src_relocs array for the target literal section may still be
6973 incomplete, but it must at least contain the entries for the L32R
6974 relocations associated with ASM_EXPANDs because they were just
6975 added in the preceding loop over the relocations. */
6977 for (i
= 0; i
< sec
->reloc_count
; i
++)
6979 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6980 bfd_boolean is_reachable
;
6982 if (!is_resolvable_asm_expansion (abfd
, sec
, contents
, irel
, link_info
,
6988 Elf_Internal_Rela
*l32r_irel
;
6990 asection
*target_sec
;
6991 xtensa_relax_info
*target_relax_info
;
6993 /* Mark the source_reloc for the L32R so that it will be
6994 removed in compute_removed_literals(), along with the
6995 associated literal. */
6996 l32r_irel
= find_associated_l32r_irel (abfd
, sec
, contents
,
6997 irel
, internal_relocs
);
6998 if (l32r_irel
== NULL
)
7001 r_reloc_init (&r_rel
, abfd
, l32r_irel
, contents
, sec_size
);
7003 target_sec
= r_reloc_get_section (&r_rel
);
7004 target_relax_info
= get_xtensa_relax_info (target_sec
);
7006 if (target_relax_info
7007 && (target_relax_info
->is_relaxable_literal_section
7008 || target_relax_info
->is_relaxable_asm_section
))
7010 source_reloc
*s_reloc
;
7012 /* Search the source_relocs for the entry corresponding to
7013 the l32r_irel. Note: The src_relocs array is not yet
7014 sorted, but it wouldn't matter anyway because we're
7015 searching by source offset instead of target offset. */
7016 s_reloc
= find_source_reloc (target_relax_info
->src_relocs
,
7017 target_relax_info
->src_next
,
7019 BFD_ASSERT (s_reloc
);
7020 s_reloc
->is_null
= TRUE
;
7023 /* Convert this reloc to ASM_SIMPLIFY. */
7024 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
7025 R_XTENSA_ASM_SIMPLIFY
);
7026 l32r_irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
7028 pin_internal_relocs (sec
, internal_relocs
);
7032 /* It is resolvable but doesn't reach. We resolve now
7033 by eliminating the relocation -- the call will remain
7034 expanded into L32R/CALLX. */
7035 irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
7036 pin_internal_relocs (sec
, internal_relocs
);
7041 release_contents (sec
, contents
);
7042 release_internal_relocs (sec
, internal_relocs
);
7047 /* Return TRUE if the asm expansion can be resolved. Generally it can
7048 be resolved on a final link or when a partial link locates it in the
7049 same section as the target. Set "is_reachable" flag if the target of
7050 the call is within the range of a direct call, given the current VMA
7051 for this section and the target section. */
7054 is_resolvable_asm_expansion (bfd
*abfd
,
7057 Elf_Internal_Rela
*irel
,
7058 struct bfd_link_info
*link_info
,
7059 bfd_boolean
*is_reachable_p
)
7061 asection
*target_sec
;
7062 bfd_vma target_offset
;
7064 xtensa_opcode opcode
, direct_call_opcode
;
7065 bfd_vma self_address
;
7066 bfd_vma dest_address
;
7067 bfd_boolean uses_l32r
;
7068 bfd_size_type sec_size
;
7070 *is_reachable_p
= FALSE
;
7072 if (contents
== NULL
)
7075 if (ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_ASM_EXPAND
)
7078 sec_size
= bfd_get_section_limit (abfd
, sec
);
7079 opcode
= get_expanded_call_opcode (contents
+ irel
->r_offset
,
7080 sec_size
- irel
->r_offset
, &uses_l32r
);
7081 /* Optimization of longcalls that use CONST16 is not yet implemented. */
7085 direct_call_opcode
= swap_callx_for_call_opcode (opcode
);
7086 if (direct_call_opcode
== XTENSA_UNDEFINED
)
7089 /* Check and see that the target resolves. */
7090 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
7091 if (!r_reloc_is_defined (&r_rel
))
7094 target_sec
= r_reloc_get_section (&r_rel
);
7095 target_offset
= r_rel
.target_offset
;
7097 /* If the target is in a shared library, then it doesn't reach. This
7098 isn't supposed to come up because the compiler should never generate
7099 non-PIC calls on systems that use shared libraries, but the linker
7100 shouldn't crash regardless. */
7101 if (!target_sec
->output_section
)
7104 /* For relocatable sections, we can only simplify when the output
7105 section of the target is the same as the output section of the
7107 if (link_info
->relocatable
7108 && (target_sec
->output_section
!= sec
->output_section
7109 || is_reloc_sym_weak (abfd
, irel
)))
7112 self_address
= (sec
->output_section
->vma
7113 + sec
->output_offset
+ irel
->r_offset
+ 3);
7114 dest_address
= (target_sec
->output_section
->vma
7115 + target_sec
->output_offset
+ target_offset
);
7117 *is_reachable_p
= pcrel_reloc_fits (direct_call_opcode
, 0,
7118 self_address
, dest_address
);
7120 if ((self_address
>> CALL_SEGMENT_BITS
) !=
7121 (dest_address
>> CALL_SEGMENT_BITS
))
7128 static Elf_Internal_Rela
*
7129 find_associated_l32r_irel (bfd
*abfd
,
7132 Elf_Internal_Rela
*other_irel
,
7133 Elf_Internal_Rela
*internal_relocs
)
7137 for (i
= 0; i
< sec
->reloc_count
; i
++)
7139 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
7141 if (irel
== other_irel
)
7143 if (irel
->r_offset
!= other_irel
->r_offset
)
7145 if (is_l32r_relocation (abfd
, sec
, contents
, irel
))
7153 static xtensa_opcode
*
7154 build_reloc_opcodes (bfd
*abfd
,
7157 Elf_Internal_Rela
*internal_relocs
)
7160 xtensa_opcode
*reloc_opcodes
=
7161 (xtensa_opcode
*) bfd_malloc (sizeof (xtensa_opcode
) * sec
->reloc_count
);
7162 for (i
= 0; i
< sec
->reloc_count
; i
++)
7164 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
7165 reloc_opcodes
[i
] = get_relocation_opcode (abfd
, sec
, contents
, irel
);
7167 return reloc_opcodes
;
7171 /* The compute_text_actions function will build a list of potential
7172 transformation actions for code in the extended basic block of each
7173 longcall that is optimized to a direct call. From this list we
7174 generate a set of actions to actually perform that optimizes for
7175 space and, if not using size_opt, maintains branch target
7178 These actions to be performed are placed on a per-section list.
7179 The actual changes are performed by relax_section() in the second
7183 compute_text_actions (bfd
*abfd
,
7185 struct bfd_link_info
*link_info
)
7187 xtensa_opcode
*reloc_opcodes
= NULL
;
7188 xtensa_relax_info
*relax_info
;
7190 Elf_Internal_Rela
*internal_relocs
;
7191 bfd_boolean ok
= TRUE
;
7193 property_table_entry
*prop_table
= 0;
7195 bfd_size_type sec_size
;
7197 relax_info
= get_xtensa_relax_info (sec
);
7198 BFD_ASSERT (relax_info
);
7199 BFD_ASSERT (relax_info
->src_next
== relax_info
->src_count
);
7201 /* Do nothing if the section contains no optimized longcalls. */
7202 if (!relax_info
->is_relaxable_asm_section
)
7205 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
7206 link_info
->keep_memory
);
7208 if (internal_relocs
)
7209 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
7210 internal_reloc_compare
);
7212 sec_size
= bfd_get_section_limit (abfd
, sec
);
7213 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
7214 if (contents
== NULL
&& sec_size
!= 0)
7220 ptblsize
= xtensa_read_table_entries (abfd
, sec
, &prop_table
,
7221 XTENSA_PROP_SEC_NAME
, FALSE
);
7228 for (i
= 0; i
< sec
->reloc_count
; i
++)
7230 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
7232 property_table_entry
*the_entry
;
7235 ebb_constraint ebb_table
;
7236 bfd_size_type simplify_size
;
7238 if (irel
&& ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_ASM_SIMPLIFY
)
7240 r_offset
= irel
->r_offset
;
7242 simplify_size
= get_asm_simplify_size (contents
, sec_size
, r_offset
);
7243 if (simplify_size
== 0)
7245 (*_bfd_error_handler
)
7246 (_("%B(%A+0x%lx): could not decode instruction for XTENSA_ASM_SIMPLIFY relocation; possible configuration mismatch"),
7247 sec
->owner
, sec
, r_offset
);
7251 /* If the instruction table is not around, then don't do this
7253 the_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
7254 sec
->vma
+ irel
->r_offset
);
7255 if (the_entry
== NULL
|| XTENSA_NO_NOP_REMOVAL
)
7257 text_action_add (&relax_info
->action_list
,
7258 ta_convert_longcall
, sec
, r_offset
,
7263 /* If the next longcall happens to be at the same address as an
7264 unreachable section of size 0, then skip forward. */
7265 ptbl_idx
= the_entry
- prop_table
;
7266 while ((the_entry
->flags
& XTENSA_PROP_UNREACHABLE
)
7267 && the_entry
->size
== 0
7268 && ptbl_idx
+ 1 < ptblsize
7269 && (prop_table
[ptbl_idx
+ 1].address
7270 == prop_table
[ptbl_idx
].address
))
7276 if (the_entry
->flags
& XTENSA_PROP_NO_TRANSFORM
)
7277 /* NO_REORDER is OK */
7280 init_ebb_constraint (&ebb_table
);
7281 ebb
= &ebb_table
.ebb
;
7282 init_ebb (ebb
, sec
, contents
, sec_size
, prop_table
, ptblsize
,
7283 internal_relocs
, sec
->reloc_count
);
7284 ebb
->start_offset
= r_offset
+ simplify_size
;
7285 ebb
->end_offset
= r_offset
+ simplify_size
;
7286 ebb
->start_ptbl_idx
= ptbl_idx
;
7287 ebb
->end_ptbl_idx
= ptbl_idx
;
7288 ebb
->start_reloc_idx
= i
;
7289 ebb
->end_reloc_idx
= i
;
7291 /* Precompute the opcode for each relocation. */
7292 if (reloc_opcodes
== NULL
)
7293 reloc_opcodes
= build_reloc_opcodes (abfd
, sec
, contents
,
7296 if (!extend_ebb_bounds (ebb
)
7297 || !compute_ebb_proposed_actions (&ebb_table
)
7298 || !compute_ebb_actions (&ebb_table
)
7299 || !check_section_ebb_pcrels_fit (abfd
, sec
, contents
,
7300 internal_relocs
, &ebb_table
,
7302 || !check_section_ebb_reduces (&ebb_table
))
7304 /* If anything goes wrong or we get unlucky and something does
7305 not fit, with our plan because of expansion between
7306 critical branches, just convert to a NOP. */
7308 text_action_add (&relax_info
->action_list
,
7309 ta_convert_longcall
, sec
, r_offset
, 0);
7310 i
= ebb_table
.ebb
.end_reloc_idx
;
7311 free_ebb_constraint (&ebb_table
);
7315 text_action_add_proposed (&relax_info
->action_list
, &ebb_table
, sec
);
7317 /* Update the index so we do not go looking at the relocations
7318 we have already processed. */
7319 i
= ebb_table
.ebb
.end_reloc_idx
;
7320 free_ebb_constraint (&ebb_table
);
7324 if (relax_info
->action_list
.head
)
7325 print_action_list (stderr
, &relax_info
->action_list
);
7329 release_contents (sec
, contents
);
7330 release_internal_relocs (sec
, internal_relocs
);
7334 free (reloc_opcodes
);
7340 /* Do not widen an instruction if it is preceeded by a
7341 loop opcode. It might cause misalignment. */
7344 prev_instr_is_a_loop (bfd_byte
*contents
,
7345 bfd_size_type content_length
,
7346 bfd_size_type offset
)
7348 xtensa_opcode prev_opcode
;
7352 prev_opcode
= insn_decode_opcode (contents
, content_length
, offset
-3, 0);
7353 return (xtensa_opcode_is_loop (xtensa_default_isa
, prev_opcode
) == 1);
7357 /* Find all of the possible actions for an extended basic block. */
7360 compute_ebb_proposed_actions (ebb_constraint
*ebb_table
)
7362 const ebb_t
*ebb
= &ebb_table
->ebb
;
7363 unsigned rel_idx
= ebb
->start_reloc_idx
;
7364 property_table_entry
*entry
, *start_entry
, *end_entry
;
7366 xtensa_isa isa
= xtensa_default_isa
;
7368 static xtensa_insnbuf insnbuf
= NULL
;
7369 static xtensa_insnbuf slotbuf
= NULL
;
7371 if (insnbuf
== NULL
)
7373 insnbuf
= xtensa_insnbuf_alloc (isa
);
7374 slotbuf
= xtensa_insnbuf_alloc (isa
);
7377 start_entry
= &ebb
->ptbl
[ebb
->start_ptbl_idx
];
7378 end_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
];
7380 for (entry
= start_entry
; entry
<= end_entry
; entry
++)
7382 bfd_vma start_offset
, end_offset
;
7383 bfd_size_type insn_len
;
7385 start_offset
= entry
->address
- ebb
->sec
->vma
;
7386 end_offset
= entry
->address
+ entry
->size
- ebb
->sec
->vma
;
7388 if (entry
== start_entry
)
7389 start_offset
= ebb
->start_offset
;
7390 if (entry
== end_entry
)
7391 end_offset
= ebb
->end_offset
;
7392 offset
= start_offset
;
7394 if (offset
== entry
->address
- ebb
->sec
->vma
7395 && (entry
->flags
& XTENSA_PROP_INSN_BRANCH_TARGET
) != 0)
7397 enum ebb_target_enum align_type
= EBB_DESIRE_TGT_ALIGN
;
7398 BFD_ASSERT (offset
!= end_offset
);
7399 if (offset
== end_offset
)
7402 insn_len
= insn_decode_len (ebb
->contents
, ebb
->content_length
,
7407 if (check_branch_target_aligned_address (offset
, insn_len
))
7408 align_type
= EBB_REQUIRE_TGT_ALIGN
;
7410 ebb_propose_action (ebb_table
, align_type
, 0,
7411 ta_none
, offset
, 0, TRUE
);
7414 while (offset
!= end_offset
)
7416 Elf_Internal_Rela
*irel
;
7417 xtensa_opcode opcode
;
7419 while (rel_idx
< ebb
->end_reloc_idx
7420 && (ebb
->relocs
[rel_idx
].r_offset
< offset
7421 || (ebb
->relocs
[rel_idx
].r_offset
== offset
7422 && (ELF32_R_TYPE (ebb
->relocs
[rel_idx
].r_info
)
7423 != R_XTENSA_ASM_SIMPLIFY
))))
7426 /* Check for longcall. */
7427 irel
= &ebb
->relocs
[rel_idx
];
7428 if (irel
->r_offset
== offset
7429 && ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_ASM_SIMPLIFY
)
7431 bfd_size_type simplify_size
;
7433 simplify_size
= get_asm_simplify_size (ebb
->contents
,
7434 ebb
->content_length
,
7436 if (simplify_size
== 0)
7439 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7440 ta_convert_longcall
, offset
, 0, TRUE
);
7442 offset
+= simplify_size
;
7446 if (offset
+ MIN_INSN_LENGTH
> ebb
->content_length
)
7448 xtensa_insnbuf_from_chars (isa
, insnbuf
, &ebb
->contents
[offset
],
7449 ebb
->content_length
- offset
);
7450 fmt
= xtensa_format_decode (isa
, insnbuf
);
7451 if (fmt
== XTENSA_UNDEFINED
)
7453 insn_len
= xtensa_format_length (isa
, fmt
);
7454 if (insn_len
== (bfd_size_type
) XTENSA_UNDEFINED
)
7457 if (xtensa_format_num_slots (isa
, fmt
) != 1)
7463 xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
);
7464 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
7465 if (opcode
== XTENSA_UNDEFINED
)
7468 if ((entry
->flags
& XTENSA_PROP_INSN_NO_DENSITY
) == 0
7469 && (entry
->flags
& XTENSA_PROP_NO_TRANSFORM
) == 0
7470 && can_narrow_instruction (slotbuf
, fmt
, opcode
) != 0)
7472 /* Add an instruction narrow action. */
7473 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7474 ta_narrow_insn
, offset
, 0, FALSE
);
7476 else if ((entry
->flags
& XTENSA_PROP_NO_TRANSFORM
) == 0
7477 && can_widen_instruction (slotbuf
, fmt
, opcode
) != 0
7478 && ! prev_instr_is_a_loop (ebb
->contents
,
7479 ebb
->content_length
, offset
))
7481 /* Add an instruction widen action. */
7482 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7483 ta_widen_insn
, offset
, 0, FALSE
);
7485 else if (xtensa_opcode_is_loop (xtensa_default_isa
, opcode
) == 1)
7487 /* Check for branch targets. */
7488 ebb_propose_action (ebb_table
, EBB_REQUIRE_LOOP_ALIGN
, 0,
7489 ta_none
, offset
, 0, TRUE
);
7496 if (ebb
->ends_unreachable
)
7498 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7499 ta_fill
, ebb
->end_offset
, 0, TRUE
);
7505 (*_bfd_error_handler
)
7506 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
7507 ebb
->sec
->owner
, ebb
->sec
, offset
);
7512 /* After all of the information has collected about the
7513 transformations possible in an EBB, compute the appropriate actions
7514 here in compute_ebb_actions. We still must check later to make
7515 sure that the actions do not break any relocations. The algorithm
7516 used here is pretty greedy. Basically, it removes as many no-ops
7517 as possible so that the end of the EBB has the same alignment
7518 characteristics as the original. First, it uses narrowing, then
7519 fill space at the end of the EBB, and finally widenings. If that
7520 does not work, it tries again with one fewer no-op removed. The
7521 optimization will only be performed if all of the branch targets
7522 that were aligned before transformation are also aligned after the
7525 When the size_opt flag is set, ignore the branch target alignments,
7526 narrow all wide instructions, and remove all no-ops unless the end
7527 of the EBB prevents it. */
7530 compute_ebb_actions (ebb_constraint
*ebb_table
)
7534 int removed_bytes
= 0;
7535 ebb_t
*ebb
= &ebb_table
->ebb
;
7536 unsigned seg_idx_start
= 0;
7537 unsigned seg_idx_end
= 0;
7539 /* We perform this like the assembler relaxation algorithm: Start by
7540 assuming all instructions are narrow and all no-ops removed; then
7543 /* For each segment of this that has a solid constraint, check to
7544 see if there are any combinations that will keep the constraint.
7546 for (seg_idx_end
= 0; seg_idx_end
< ebb_table
->action_count
; seg_idx_end
++)
7548 bfd_boolean requires_text_end_align
= FALSE
;
7549 unsigned longcall_count
= 0;
7550 unsigned longcall_convert_count
= 0;
7551 unsigned narrowable_count
= 0;
7552 unsigned narrowable_convert_count
= 0;
7553 unsigned widenable_count
= 0;
7554 unsigned widenable_convert_count
= 0;
7556 proposed_action
*action
= NULL
;
7557 int align
= (1 << ebb_table
->ebb
.sec
->alignment_power
);
7559 seg_idx_start
= seg_idx_end
;
7561 for (i
= seg_idx_start
; i
< ebb_table
->action_count
; i
++)
7563 action
= &ebb_table
->actions
[i
];
7564 if (action
->action
== ta_convert_longcall
)
7566 if (action
->action
== ta_narrow_insn
)
7568 if (action
->action
== ta_widen_insn
)
7570 if (action
->action
== ta_fill
)
7572 if (action
->align_type
== EBB_REQUIRE_LOOP_ALIGN
)
7574 if (action
->align_type
== EBB_REQUIRE_TGT_ALIGN
7575 && !elf32xtensa_size_opt
)
7580 if (seg_idx_end
== ebb_table
->action_count
&& !ebb
->ends_unreachable
)
7581 requires_text_end_align
= TRUE
;
7583 if (elf32xtensa_size_opt
&& !requires_text_end_align
7584 && action
->align_type
!= EBB_REQUIRE_LOOP_ALIGN
7585 && action
->align_type
!= EBB_REQUIRE_TGT_ALIGN
)
7587 longcall_convert_count
= longcall_count
;
7588 narrowable_convert_count
= narrowable_count
;
7589 widenable_convert_count
= 0;
7593 /* There is a constraint. Convert the max number of longcalls. */
7594 narrowable_convert_count
= 0;
7595 longcall_convert_count
= 0;
7596 widenable_convert_count
= 0;
7598 for (j
= 0; j
< longcall_count
; j
++)
7600 int removed
= (longcall_count
- j
) * 3 & (align
- 1);
7601 unsigned desire_narrow
= (align
- removed
) & (align
- 1);
7602 unsigned desire_widen
= removed
;
7603 if (desire_narrow
<= narrowable_count
)
7605 narrowable_convert_count
= desire_narrow
;
7606 narrowable_convert_count
+=
7607 (align
* ((narrowable_count
- narrowable_convert_count
)
7609 longcall_convert_count
= (longcall_count
- j
);
7610 widenable_convert_count
= 0;
7613 if (desire_widen
<= widenable_count
&& !elf32xtensa_size_opt
)
7615 narrowable_convert_count
= 0;
7616 longcall_convert_count
= longcall_count
- j
;
7617 widenable_convert_count
= desire_widen
;
7623 /* Now the number of conversions are saved. Do them. */
7624 for (i
= seg_idx_start
; i
< seg_idx_end
; i
++)
7626 action
= &ebb_table
->actions
[i
];
7627 switch (action
->action
)
7629 case ta_convert_longcall
:
7630 if (longcall_convert_count
!= 0)
7632 action
->action
= ta_remove_longcall
;
7633 action
->do_action
= TRUE
;
7634 action
->removed_bytes
+= 3;
7635 longcall_convert_count
--;
7638 case ta_narrow_insn
:
7639 if (narrowable_convert_count
!= 0)
7641 action
->do_action
= TRUE
;
7642 action
->removed_bytes
+= 1;
7643 narrowable_convert_count
--;
7647 if (widenable_convert_count
!= 0)
7649 action
->do_action
= TRUE
;
7650 action
->removed_bytes
-= 1;
7651 widenable_convert_count
--;
7660 /* Now we move on to some local opts. Try to remove each of the
7661 remaining longcalls. */
7663 if (ebb_table
->ebb
.ends_section
|| ebb_table
->ebb
.ends_unreachable
)
7666 for (i
= 0; i
< ebb_table
->action_count
; i
++)
7668 int old_removed_bytes
= removed_bytes
;
7669 proposed_action
*action
= &ebb_table
->actions
[i
];
7671 if (action
->do_action
&& action
->action
== ta_convert_longcall
)
7673 bfd_boolean bad_alignment
= FALSE
;
7675 for (j
= i
+ 1; j
< ebb_table
->action_count
; j
++)
7677 proposed_action
*new_action
= &ebb_table
->actions
[j
];
7678 bfd_vma offset
= new_action
->offset
;
7679 if (new_action
->align_type
== EBB_REQUIRE_TGT_ALIGN
)
7681 if (!check_branch_target_aligned
7682 (ebb_table
->ebb
.contents
,
7683 ebb_table
->ebb
.content_length
,
7684 offset
, offset
- removed_bytes
))
7686 bad_alignment
= TRUE
;
7690 if (new_action
->align_type
== EBB_REQUIRE_LOOP_ALIGN
)
7692 if (!check_loop_aligned (ebb_table
->ebb
.contents
,
7693 ebb_table
->ebb
.content_length
,
7695 offset
- removed_bytes
))
7697 bad_alignment
= TRUE
;
7701 if (new_action
->action
== ta_narrow_insn
7702 && !new_action
->do_action
7703 && ebb_table
->ebb
.sec
->alignment_power
== 2)
7705 /* Narrow an instruction and we are done. */
7706 new_action
->do_action
= TRUE
;
7707 new_action
->removed_bytes
+= 1;
7708 bad_alignment
= FALSE
;
7711 if (new_action
->action
== ta_widen_insn
7712 && new_action
->do_action
7713 && ebb_table
->ebb
.sec
->alignment_power
== 2)
7715 /* Narrow an instruction and we are done. */
7716 new_action
->do_action
= FALSE
;
7717 new_action
->removed_bytes
+= 1;
7718 bad_alignment
= FALSE
;
7721 if (new_action
->do_action
)
7722 removed_bytes
+= new_action
->removed_bytes
;
7726 action
->removed_bytes
+= 3;
7727 action
->action
= ta_remove_longcall
;
7728 action
->do_action
= TRUE
;
7731 removed_bytes
= old_removed_bytes
;
7732 if (action
->do_action
)
7733 removed_bytes
+= action
->removed_bytes
;
7738 for (i
= 0; i
< ebb_table
->action_count
; ++i
)
7740 proposed_action
*action
= &ebb_table
->actions
[i
];
7741 if (action
->do_action
)
7742 removed_bytes
+= action
->removed_bytes
;
7745 if ((removed_bytes
% (1 << ebb_table
->ebb
.sec
->alignment_power
)) != 0
7746 && ebb
->ends_unreachable
)
7748 proposed_action
*action
;
7752 BFD_ASSERT (ebb_table
->action_count
!= 0);
7753 action
= &ebb_table
->actions
[ebb_table
->action_count
- 1];
7754 BFD_ASSERT (action
->action
== ta_fill
);
7755 BFD_ASSERT (ebb
->ends_unreachable
->flags
& XTENSA_PROP_UNREACHABLE
);
7757 extra_space
= compute_fill_extra_space (ebb
->ends_unreachable
);
7758 br
= action
->removed_bytes
+ removed_bytes
+ extra_space
;
7759 br
= br
& ((1 << ebb
->sec
->alignment_power
) - 1);
7761 action
->removed_bytes
= extra_space
- br
;
7767 /* The xlate_map is a sorted array of address mappings designed to
7768 answer the offset_with_removed_text() query with a binary search instead
7769 of a linear search through the section's action_list. */
7771 typedef struct xlate_map_entry xlate_map_entry_t
;
7772 typedef struct xlate_map xlate_map_t
;
7774 struct xlate_map_entry
7776 unsigned orig_address
;
7777 unsigned new_address
;
7783 unsigned entry_count
;
7784 xlate_map_entry_t
*entry
;
7789 xlate_compare (const void *a_v
, const void *b_v
)
7791 const xlate_map_entry_t
*a
= (const xlate_map_entry_t
*) a_v
;
7792 const xlate_map_entry_t
*b
= (const xlate_map_entry_t
*) b_v
;
7793 if (a
->orig_address
< b
->orig_address
)
7795 if (a
->orig_address
> (b
->orig_address
+ b
->size
- 1))
7802 xlate_offset_with_removed_text (const xlate_map_t
*map
,
7803 text_action_list
*action_list
,
7806 xlate_map_entry_t tmp
;
7808 xlate_map_entry_t
*e
;
7811 return offset_with_removed_text (action_list
, offset
);
7813 if (map
->entry_count
== 0)
7816 tmp
.orig_address
= offset
;
7817 tmp
.new_address
= offset
;
7820 r
= bsearch (&offset
, map
->entry
, map
->entry_count
,
7821 sizeof (xlate_map_entry_t
), &xlate_compare
);
7822 e
= (xlate_map_entry_t
*) r
;
7824 BFD_ASSERT (e
!= NULL
);
7827 return e
->new_address
- e
->orig_address
+ offset
;
7831 /* Build a binary searchable offset translation map from a section's
7834 static xlate_map_t
*
7835 build_xlate_map (asection
*sec
, xtensa_relax_info
*relax_info
)
7837 xlate_map_t
*map
= (xlate_map_t
*) bfd_malloc (sizeof (xlate_map_t
));
7838 text_action_list
*action_list
= &relax_info
->action_list
;
7839 unsigned num_actions
= 0;
7842 xlate_map_entry_t
*current_entry
;
7847 num_actions
= action_list_count (action_list
);
7848 map
->entry
= (xlate_map_entry_t
*)
7849 bfd_malloc (sizeof (xlate_map_entry_t
) * (num_actions
+ 1));
7850 if (map
->entry
== NULL
)
7855 map
->entry_count
= 0;
7858 current_entry
= &map
->entry
[0];
7860 current_entry
->orig_address
= 0;
7861 current_entry
->new_address
= 0;
7862 current_entry
->size
= 0;
7864 for (r
= action_list
->head
; r
!= NULL
; r
= r
->next
)
7866 unsigned orig_size
= 0;
7870 case ta_remove_insn
:
7871 case ta_convert_longcall
:
7872 case ta_remove_literal
:
7873 case ta_add_literal
:
7875 case ta_remove_longcall
:
7878 case ta_narrow_insn
:
7887 current_entry
->size
=
7888 r
->offset
+ orig_size
- current_entry
->orig_address
;
7889 if (current_entry
->size
!= 0)
7894 current_entry
->orig_address
= r
->offset
+ orig_size
;
7895 removed
+= r
->removed_bytes
;
7896 current_entry
->new_address
= r
->offset
+ orig_size
- removed
;
7897 current_entry
->size
= 0;
7900 current_entry
->size
= (bfd_get_section_limit (sec
->owner
, sec
)
7901 - current_entry
->orig_address
);
7902 if (current_entry
->size
!= 0)
7909 /* Free an offset translation map. */
7912 free_xlate_map (xlate_map_t
*map
)
7914 if (map
&& map
->entry
)
7921 /* Use check_section_ebb_pcrels_fit to make sure that all of the
7922 relocations in a section will fit if a proposed set of actions
7926 check_section_ebb_pcrels_fit (bfd
*abfd
,
7929 Elf_Internal_Rela
*internal_relocs
,
7930 const ebb_constraint
*constraint
,
7931 const xtensa_opcode
*reloc_opcodes
)
7934 Elf_Internal_Rela
*irel
;
7935 xlate_map_t
*xmap
= NULL
;
7936 bfd_boolean ok
= TRUE
;
7937 xtensa_relax_info
*relax_info
;
7939 relax_info
= get_xtensa_relax_info (sec
);
7941 if (relax_info
&& sec
->reloc_count
> 100)
7943 xmap
= build_xlate_map (sec
, relax_info
);
7944 /* NULL indicates out of memory, but the slow version
7945 can still be used. */
7948 for (i
= 0; i
< sec
->reloc_count
; i
++)
7951 bfd_vma orig_self_offset
, orig_target_offset
;
7952 bfd_vma self_offset
, target_offset
;
7954 reloc_howto_type
*howto
;
7955 int self_removed_bytes
, target_removed_bytes
;
7957 irel
= &internal_relocs
[i
];
7958 r_type
= ELF32_R_TYPE (irel
->r_info
);
7960 howto
= &elf_howto_table
[r_type
];
7961 /* We maintain the required invariant: PC-relative relocations
7962 that fit before linking must fit after linking. Thus we only
7963 need to deal with relocations to the same section that are
7965 if (r_type
== R_XTENSA_ASM_SIMPLIFY
7966 || r_type
== R_XTENSA_32_PCREL
7967 || !howto
->pc_relative
)
7970 r_reloc_init (&r_rel
, abfd
, irel
, contents
,
7971 bfd_get_section_limit (abfd
, sec
));
7973 if (r_reloc_get_section (&r_rel
) != sec
)
7976 orig_self_offset
= irel
->r_offset
;
7977 orig_target_offset
= r_rel
.target_offset
;
7979 self_offset
= orig_self_offset
;
7980 target_offset
= orig_target_offset
;
7985 xlate_offset_with_removed_text (xmap
, &relax_info
->action_list
,
7988 xlate_offset_with_removed_text (xmap
, &relax_info
->action_list
,
7989 orig_target_offset
);
7992 self_removed_bytes
= 0;
7993 target_removed_bytes
= 0;
7995 for (j
= 0; j
< constraint
->action_count
; ++j
)
7997 proposed_action
*action
= &constraint
->actions
[j
];
7998 bfd_vma offset
= action
->offset
;
7999 int removed_bytes
= action
->removed_bytes
;
8000 if (offset
< orig_self_offset
8001 || (offset
== orig_self_offset
&& action
->action
== ta_fill
8002 && action
->removed_bytes
< 0))
8003 self_removed_bytes
+= removed_bytes
;
8004 if (offset
< orig_target_offset
8005 || (offset
== orig_target_offset
&& action
->action
== ta_fill
8006 && action
->removed_bytes
< 0))
8007 target_removed_bytes
+= removed_bytes
;
8009 self_offset
-= self_removed_bytes
;
8010 target_offset
-= target_removed_bytes
;
8012 /* Try to encode it. Get the operand and check. */
8013 if (is_alt_relocation (ELF32_R_TYPE (irel
->r_info
)))
8015 /* None of the current alternate relocs are PC-relative,
8016 and only PC-relative relocs matter here. */
8020 xtensa_opcode opcode
;
8024 opcode
= reloc_opcodes
[i
];
8026 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
8027 if (opcode
== XTENSA_UNDEFINED
)
8033 opnum
= get_relocation_opnd (opcode
, ELF32_R_TYPE (irel
->r_info
));
8034 if (opnum
== XTENSA_UNDEFINED
)
8040 if (!pcrel_reloc_fits (opcode
, opnum
, self_offset
, target_offset
))
8049 free_xlate_map (xmap
);
8056 check_section_ebb_reduces (const ebb_constraint
*constraint
)
8061 for (i
= 0; i
< constraint
->action_count
; i
++)
8063 const proposed_action
*action
= &constraint
->actions
[i
];
8064 if (action
->do_action
)
8065 removed
+= action
->removed_bytes
;
8075 text_action_add_proposed (text_action_list
*l
,
8076 const ebb_constraint
*ebb_table
,
8081 for (i
= 0; i
< ebb_table
->action_count
; i
++)
8083 proposed_action
*action
= &ebb_table
->actions
[i
];
8085 if (!action
->do_action
)
8087 switch (action
->action
)
8089 case ta_remove_insn
:
8090 case ta_remove_longcall
:
8091 case ta_convert_longcall
:
8092 case ta_narrow_insn
:
8095 case ta_remove_literal
:
8096 text_action_add (l
, action
->action
, sec
, action
->offset
,
8097 action
->removed_bytes
);
8110 compute_fill_extra_space (property_table_entry
*entry
)
8112 int fill_extra_space
;
8117 if ((entry
->flags
& XTENSA_PROP_UNREACHABLE
) == 0)
8120 fill_extra_space
= entry
->size
;
8121 if ((entry
->flags
& XTENSA_PROP_ALIGN
) != 0)
8123 /* Fill bytes for alignment:
8124 (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
8125 int pow
= GET_XTENSA_PROP_ALIGNMENT (entry
->flags
);
8126 int nsm
= (1 << pow
) - 1;
8127 bfd_vma addr
= entry
->address
+ entry
->size
;
8128 bfd_vma align_fill
= nsm
- ((addr
+ nsm
) & nsm
);
8129 fill_extra_space
+= align_fill
;
8131 return fill_extra_space
;
8135 /* First relaxation pass. */
8137 /* If the section contains relaxable literals, check each literal to
8138 see if it has the same value as another literal that has already
8139 been seen, either in the current section or a previous one. If so,
8140 add an entry to the per-section list of removed literals. The
8141 actual changes are deferred until the next pass. */
8144 compute_removed_literals (bfd
*abfd
,
8146 struct bfd_link_info
*link_info
,
8147 value_map_hash_table
*values
)
8149 xtensa_relax_info
*relax_info
;
8151 Elf_Internal_Rela
*internal_relocs
;
8152 source_reloc
*src_relocs
, *rel
;
8153 bfd_boolean ok
= TRUE
;
8154 property_table_entry
*prop_table
= NULL
;
8157 bfd_boolean last_loc_is_prev
= FALSE
;
8158 bfd_vma last_target_offset
= 0;
8159 section_cache_t target_sec_cache
;
8160 bfd_size_type sec_size
;
8162 init_section_cache (&target_sec_cache
);
8164 /* Do nothing if it is not a relaxable literal section. */
8165 relax_info
= get_xtensa_relax_info (sec
);
8166 BFD_ASSERT (relax_info
);
8167 if (!relax_info
->is_relaxable_literal_section
)
8170 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
8171 link_info
->keep_memory
);
8173 sec_size
= bfd_get_section_limit (abfd
, sec
);
8174 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
8175 if (contents
== NULL
&& sec_size
!= 0)
8181 /* Sort the source_relocs by target offset. */
8182 src_relocs
= relax_info
->src_relocs
;
8183 qsort (src_relocs
, relax_info
->src_count
,
8184 sizeof (source_reloc
), source_reloc_compare
);
8185 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
8186 internal_reloc_compare
);
8188 ptblsize
= xtensa_read_table_entries (abfd
, sec
, &prop_table
,
8189 XTENSA_PROP_SEC_NAME
, FALSE
);
8197 for (i
= 0; i
< relax_info
->src_count
; i
++)
8199 Elf_Internal_Rela
*irel
= NULL
;
8201 rel
= &src_relocs
[i
];
8202 if (get_l32r_opcode () != rel
->opcode
)
8204 irel
= get_irel_at_offset (sec
, internal_relocs
,
8205 rel
->r_rel
.target_offset
);
8207 /* If the relocation on this is not a simple R_XTENSA_32 or
8208 R_XTENSA_PLT then do not consider it. This may happen when
8209 the difference of two symbols is used in a literal. */
8210 if (irel
&& (ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_32
8211 && ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_PLT
))
8214 /* If the target_offset for this relocation is the same as the
8215 previous relocation, then we've already considered whether the
8216 literal can be coalesced. Skip to the next one.... */
8217 if (i
!= 0 && prev_i
!= -1
8218 && src_relocs
[i
-1].r_rel
.target_offset
== rel
->r_rel
.target_offset
)
8222 if (last_loc_is_prev
&&
8223 last_target_offset
+ 4 != rel
->r_rel
.target_offset
)
8224 last_loc_is_prev
= FALSE
;
8226 /* Check if the relocation was from an L32R that is being removed
8227 because a CALLX was converted to a direct CALL, and check if
8228 there are no other relocations to the literal. */
8229 if (is_removable_literal (rel
, i
, src_relocs
, relax_info
->src_count
,
8230 sec
, prop_table
, ptblsize
))
8232 if (!remove_dead_literal (abfd
, sec
, link_info
, internal_relocs
,
8233 irel
, rel
, prop_table
, ptblsize
))
8238 last_target_offset
= rel
->r_rel
.target_offset
;
8242 if (!identify_literal_placement (abfd
, sec
, contents
, link_info
,
8244 &last_loc_is_prev
, irel
,
8245 relax_info
->src_count
- i
, rel
,
8246 prop_table
, ptblsize
,
8247 &target_sec_cache
, rel
->is_abs_literal
))
8252 last_target_offset
= rel
->r_rel
.target_offset
;
8256 print_removed_literals (stderr
, &relax_info
->removed_list
);
8257 print_action_list (stderr
, &relax_info
->action_list
);
8261 if (prop_table
) free (prop_table
);
8262 clear_section_cache (&target_sec_cache
);
8264 release_contents (sec
, contents
);
8265 release_internal_relocs (sec
, internal_relocs
);
8270 static Elf_Internal_Rela
*
8271 get_irel_at_offset (asection
*sec
,
8272 Elf_Internal_Rela
*internal_relocs
,
8276 Elf_Internal_Rela
*irel
;
8278 Elf_Internal_Rela key
;
8280 if (!internal_relocs
)
8283 key
.r_offset
= offset
;
8284 irel
= bsearch (&key
, internal_relocs
, sec
->reloc_count
,
8285 sizeof (Elf_Internal_Rela
), internal_reloc_matches
);
8289 /* bsearch does not guarantee which will be returned if there are
8290 multiple matches. We need the first that is not an alignment. */
8291 i
= irel
- internal_relocs
;
8294 if (internal_relocs
[i
-1].r_offset
!= offset
)
8298 for ( ; i
< sec
->reloc_count
; i
++)
8300 irel
= &internal_relocs
[i
];
8301 r_type
= ELF32_R_TYPE (irel
->r_info
);
8302 if (irel
->r_offset
== offset
&& r_type
!= R_XTENSA_NONE
)
8311 is_removable_literal (const source_reloc
*rel
,
8313 const source_reloc
*src_relocs
,
8316 property_table_entry
*prop_table
,
8319 const source_reloc
*curr_rel
;
8320 property_table_entry
*entry
;
8325 entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
8326 sec
->vma
+ rel
->r_rel
.target_offset
);
8327 if (entry
&& (entry
->flags
& XTENSA_PROP_NO_TRANSFORM
))
8330 for (++i
; i
< src_count
; ++i
)
8332 curr_rel
= &src_relocs
[i
];
8333 /* If all others have the same target offset.... */
8334 if (curr_rel
->r_rel
.target_offset
!= rel
->r_rel
.target_offset
)
8337 if (!curr_rel
->is_null
8338 && !xtensa_is_property_section (curr_rel
->source_sec
)
8339 && !(curr_rel
->source_sec
->flags
& SEC_DEBUGGING
))
8347 remove_dead_literal (bfd
*abfd
,
8349 struct bfd_link_info
*link_info
,
8350 Elf_Internal_Rela
*internal_relocs
,
8351 Elf_Internal_Rela
*irel
,
8353 property_table_entry
*prop_table
,
8356 property_table_entry
*entry
;
8357 xtensa_relax_info
*relax_info
;
8359 relax_info
= get_xtensa_relax_info (sec
);
8363 entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
8364 sec
->vma
+ rel
->r_rel
.target_offset
);
8366 /* Mark the unused literal so that it will be removed. */
8367 add_removed_literal (&relax_info
->removed_list
, &rel
->r_rel
, NULL
);
8369 text_action_add (&relax_info
->action_list
,
8370 ta_remove_literal
, sec
, rel
->r_rel
.target_offset
, 4);
8372 /* If the section is 4-byte aligned, do not add fill. */
8373 if (sec
->alignment_power
> 2)
8375 int fill_extra_space
;
8376 bfd_vma entry_sec_offset
;
8378 property_table_entry
*the_add_entry
;
8382 entry_sec_offset
= entry
->address
- sec
->vma
+ entry
->size
;
8384 entry_sec_offset
= rel
->r_rel
.target_offset
+ 4;
8386 /* If the literal range is at the end of the section,
8388 the_add_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
8390 fill_extra_space
= compute_fill_extra_space (the_add_entry
);
8392 fa
= find_fill_action (&relax_info
->action_list
, sec
, entry_sec_offset
);
8393 removed_diff
= compute_removed_action_diff (fa
, sec
, entry_sec_offset
,
8394 -4, fill_extra_space
);
8396 adjust_fill_action (fa
, removed_diff
);
8398 text_action_add (&relax_info
->action_list
,
8399 ta_fill
, sec
, entry_sec_offset
, removed_diff
);
8402 /* Zero out the relocation on this literal location. */
8405 if (elf_hash_table (link_info
)->dynamic_sections_created
)
8406 shrink_dynamic_reloc_sections (link_info
, abfd
, sec
, irel
);
8408 irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
8409 pin_internal_relocs (sec
, internal_relocs
);
8412 /* Do not modify "last_loc_is_prev". */
8418 identify_literal_placement (bfd
*abfd
,
8421 struct bfd_link_info
*link_info
,
8422 value_map_hash_table
*values
,
8423 bfd_boolean
*last_loc_is_prev_p
,
8424 Elf_Internal_Rela
*irel
,
8425 int remaining_src_rels
,
8427 property_table_entry
*prop_table
,
8429 section_cache_t
*target_sec_cache
,
8430 bfd_boolean is_abs_literal
)
8434 xtensa_relax_info
*relax_info
;
8435 bfd_boolean literal_placed
= FALSE
;
8437 unsigned long value
;
8438 bfd_boolean final_static_link
;
8439 bfd_size_type sec_size
;
8441 relax_info
= get_xtensa_relax_info (sec
);
8445 sec_size
= bfd_get_section_limit (abfd
, sec
);
8448 (!link_info
->relocatable
8449 && !elf_hash_table (link_info
)->dynamic_sections_created
);
8451 /* The placement algorithm first checks to see if the literal is
8452 already in the value map. If so and the value map is reachable
8453 from all uses, then the literal is moved to that location. If
8454 not, then we identify the last location where a fresh literal was
8455 placed. If the literal can be safely moved there, then we do so.
8456 If not, then we assume that the literal is not to move and leave
8457 the literal where it is, marking it as the last literal
8460 /* Find the literal value. */
8462 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
8465 BFD_ASSERT (rel
->r_rel
.target_offset
< sec_size
);
8466 value
= bfd_get_32 (abfd
, contents
+ rel
->r_rel
.target_offset
);
8468 init_literal_value (&val
, &r_rel
, value
, is_abs_literal
);
8470 /* Check if we've seen another literal with the same value that
8471 is in the same output section. */
8472 val_map
= value_map_get_cached_value (values
, &val
, final_static_link
);
8475 && (r_reloc_get_section (&val_map
->loc
)->output_section
8476 == sec
->output_section
)
8477 && relocations_reach (rel
, remaining_src_rels
, &val_map
->loc
)
8478 && coalesce_shared_literal (sec
, rel
, prop_table
, ptblsize
, val_map
))
8480 /* No change to last_loc_is_prev. */
8481 literal_placed
= TRUE
;
8484 /* For relocatable links, do not try to move literals. To do it
8485 correctly might increase the number of relocations in an input
8486 section making the default relocatable linking fail. */
8487 if (!link_info
->relocatable
&& !literal_placed
8488 && values
->has_last_loc
&& !(*last_loc_is_prev_p
))
8490 asection
*target_sec
= r_reloc_get_section (&values
->last_loc
);
8491 if (target_sec
&& target_sec
->output_section
== sec
->output_section
)
8493 /* Increment the virtual offset. */
8494 r_reloc try_loc
= values
->last_loc
;
8495 try_loc
.virtual_offset
+= 4;
8497 /* There is a last loc that was in the same output section. */
8498 if (relocations_reach (rel
, remaining_src_rels
, &try_loc
)
8499 && move_shared_literal (sec
, link_info
, rel
,
8500 prop_table
, ptblsize
,
8501 &try_loc
, &val
, target_sec_cache
))
8503 values
->last_loc
.virtual_offset
+= 4;
8504 literal_placed
= TRUE
;
8506 val_map
= add_value_map (values
, &val
, &try_loc
,
8509 val_map
->loc
= try_loc
;
8514 if (!literal_placed
)
8516 /* Nothing worked, leave the literal alone but update the last loc. */
8517 values
->has_last_loc
= TRUE
;
8518 values
->last_loc
= rel
->r_rel
;
8520 val_map
= add_value_map (values
, &val
, &rel
->r_rel
, final_static_link
);
8522 val_map
->loc
= rel
->r_rel
;
8523 *last_loc_is_prev_p
= TRUE
;
8530 /* Check if the original relocations (presumably on L32R instructions)
8531 identified by reloc[0..N] can be changed to reference the literal
8532 identified by r_rel. If r_rel is out of range for any of the
8533 original relocations, then we don't want to coalesce the original
8534 literal with the one at r_rel. We only check reloc[0..N], where the
8535 offsets are all the same as for reloc[0] (i.e., they're all
8536 referencing the same literal) and where N is also bounded by the
8537 number of remaining entries in the "reloc" array. The "reloc" array
8538 is sorted by target offset so we know all the entries for the same
8539 literal will be contiguous. */
8542 relocations_reach (source_reloc
*reloc
,
8543 int remaining_relocs
,
8544 const r_reloc
*r_rel
)
8546 bfd_vma from_offset
, source_address
, dest_address
;
8550 if (!r_reloc_is_defined (r_rel
))
8553 sec
= r_reloc_get_section (r_rel
);
8554 from_offset
= reloc
[0].r_rel
.target_offset
;
8556 for (i
= 0; i
< remaining_relocs
; i
++)
8558 if (reloc
[i
].r_rel
.target_offset
!= from_offset
)
8561 /* Ignore relocations that have been removed. */
8562 if (reloc
[i
].is_null
)
8565 /* The original and new output section for these must be the same
8566 in order to coalesce. */
8567 if (r_reloc_get_section (&reloc
[i
].r_rel
)->output_section
8568 != sec
->output_section
)
8571 /* Absolute literals in the same output section can always be
8573 if (reloc
[i
].is_abs_literal
)
8576 /* A literal with no PC-relative relocations can be moved anywhere. */
8577 if (reloc
[i
].opnd
!= -1)
8579 /* Otherwise, check to see that it fits. */
8580 source_address
= (reloc
[i
].source_sec
->output_section
->vma
8581 + reloc
[i
].source_sec
->output_offset
8582 + reloc
[i
].r_rel
.rela
.r_offset
);
8583 dest_address
= (sec
->output_section
->vma
8584 + sec
->output_offset
8585 + r_rel
->target_offset
);
8587 if (!pcrel_reloc_fits (reloc
[i
].opcode
, reloc
[i
].opnd
,
8588 source_address
, dest_address
))
8597 /* Move a literal to another literal location because it is
8598 the same as the other literal value. */
8601 coalesce_shared_literal (asection
*sec
,
8603 property_table_entry
*prop_table
,
8607 property_table_entry
*entry
;
8609 property_table_entry
*the_add_entry
;
8611 xtensa_relax_info
*relax_info
;
8613 relax_info
= get_xtensa_relax_info (sec
);
8617 entry
= elf_xtensa_find_property_entry
8618 (prop_table
, ptblsize
, sec
->vma
+ rel
->r_rel
.target_offset
);
8619 if (entry
&& (entry
->flags
& XTENSA_PROP_NO_TRANSFORM
))
8622 /* Mark that the literal will be coalesced. */
8623 add_removed_literal (&relax_info
->removed_list
, &rel
->r_rel
, &val_map
->loc
);
8625 text_action_add (&relax_info
->action_list
,
8626 ta_remove_literal
, sec
, rel
->r_rel
.target_offset
, 4);
8628 /* If the section is 4-byte aligned, do not add fill. */
8629 if (sec
->alignment_power
> 2)
8631 int fill_extra_space
;
8632 bfd_vma entry_sec_offset
;
8635 entry_sec_offset
= entry
->address
- sec
->vma
+ entry
->size
;
8637 entry_sec_offset
= rel
->r_rel
.target_offset
+ 4;
8639 /* If the literal range is at the end of the section,
8641 fill_extra_space
= 0;
8642 the_add_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
8644 if (the_add_entry
&& (the_add_entry
->flags
& XTENSA_PROP_UNREACHABLE
))
8645 fill_extra_space
= the_add_entry
->size
;
8647 fa
= find_fill_action (&relax_info
->action_list
, sec
, entry_sec_offset
);
8648 removed_diff
= compute_removed_action_diff (fa
, sec
, entry_sec_offset
,
8649 -4, fill_extra_space
);
8651 adjust_fill_action (fa
, removed_diff
);
8653 text_action_add (&relax_info
->action_list
,
8654 ta_fill
, sec
, entry_sec_offset
, removed_diff
);
8661 /* Move a literal to another location. This may actually increase the
8662 total amount of space used because of alignments so we need to do
8663 this carefully. Also, it may make a branch go out of range. */
8666 move_shared_literal (asection
*sec
,
8667 struct bfd_link_info
*link_info
,
8669 property_table_entry
*prop_table
,
8671 const r_reloc
*target_loc
,
8672 const literal_value
*lit_value
,
8673 section_cache_t
*target_sec_cache
)
8675 property_table_entry
*the_add_entry
, *src_entry
, *target_entry
= NULL
;
8676 text_action
*fa
, *target_fa
;
8678 xtensa_relax_info
*relax_info
, *target_relax_info
;
8679 asection
*target_sec
;
8681 ebb_constraint ebb_table
;
8682 bfd_boolean relocs_fit
;
8684 /* If this routine always returns FALSE, the literals that cannot be
8685 coalesced will not be moved. */
8686 if (elf32xtensa_no_literal_movement
)
8689 relax_info
= get_xtensa_relax_info (sec
);
8693 target_sec
= r_reloc_get_section (target_loc
);
8694 target_relax_info
= get_xtensa_relax_info (target_sec
);
8696 /* Literals to undefined sections may not be moved because they
8697 must report an error. */
8698 if (bfd_is_und_section (target_sec
))
8701 src_entry
= elf_xtensa_find_property_entry
8702 (prop_table
, ptblsize
, sec
->vma
+ rel
->r_rel
.target_offset
);
8704 if (!section_cache_section (target_sec_cache
, target_sec
, link_info
))
8707 target_entry
= elf_xtensa_find_property_entry
8708 (target_sec_cache
->ptbl
, target_sec_cache
->pte_count
,
8709 target_sec
->vma
+ target_loc
->target_offset
);
8714 /* Make sure that we have not broken any branches. */
8717 init_ebb_constraint (&ebb_table
);
8718 ebb
= &ebb_table
.ebb
;
8719 init_ebb (ebb
, target_sec_cache
->sec
, target_sec_cache
->contents
,
8720 target_sec_cache
->content_length
,
8721 target_sec_cache
->ptbl
, target_sec_cache
->pte_count
,
8722 target_sec_cache
->relocs
, target_sec_cache
->reloc_count
);
8724 /* Propose to add 4 bytes + worst-case alignment size increase to
8726 ebb_propose_action (&ebb_table
, EBB_NO_ALIGN
, 0,
8727 ta_fill
, target_loc
->target_offset
,
8728 -4 - (1 << target_sec
->alignment_power
), TRUE
);
8730 /* Check all of the PC-relative relocations to make sure they still fit. */
8731 relocs_fit
= check_section_ebb_pcrels_fit (target_sec
->owner
, target_sec
,
8732 target_sec_cache
->contents
,
8733 target_sec_cache
->relocs
,
8739 text_action_add_literal (&target_relax_info
->action_list
,
8740 ta_add_literal
, target_loc
, lit_value
, -4);
8742 if (target_sec
->alignment_power
> 2 && target_entry
!= src_entry
)
8744 /* May need to add or remove some fill to maintain alignment. */
8745 int fill_extra_space
;
8746 bfd_vma entry_sec_offset
;
8749 target_entry
->address
- target_sec
->vma
+ target_entry
->size
;
8751 /* If the literal range is at the end of the section,
8753 fill_extra_space
= 0;
8755 elf_xtensa_find_property_entry (target_sec_cache
->ptbl
,
8756 target_sec_cache
->pte_count
,
8758 if (the_add_entry
&& (the_add_entry
->flags
& XTENSA_PROP_UNREACHABLE
))
8759 fill_extra_space
= the_add_entry
->size
;
8761 target_fa
= find_fill_action (&target_relax_info
->action_list
,
8762 target_sec
, entry_sec_offset
);
8763 removed_diff
= compute_removed_action_diff (target_fa
, target_sec
,
8764 entry_sec_offset
, 4,
8767 adjust_fill_action (target_fa
, removed_diff
);
8769 text_action_add (&target_relax_info
->action_list
,
8770 ta_fill
, target_sec
, entry_sec_offset
, removed_diff
);
8773 /* Mark that the literal will be moved to the new location. */
8774 add_removed_literal (&relax_info
->removed_list
, &rel
->r_rel
, target_loc
);
8776 /* Remove the literal. */
8777 text_action_add (&relax_info
->action_list
,
8778 ta_remove_literal
, sec
, rel
->r_rel
.target_offset
, 4);
8780 /* If the section is 4-byte aligned, do not add fill. */
8781 if (sec
->alignment_power
> 2 && target_entry
!= src_entry
)
8783 int fill_extra_space
;
8784 bfd_vma entry_sec_offset
;
8787 entry_sec_offset
= src_entry
->address
- sec
->vma
+ src_entry
->size
;
8789 entry_sec_offset
= rel
->r_rel
.target_offset
+4;
8791 /* If the literal range is at the end of the section,
8793 fill_extra_space
= 0;
8794 the_add_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
8796 if (the_add_entry
&& (the_add_entry
->flags
& XTENSA_PROP_UNREACHABLE
))
8797 fill_extra_space
= the_add_entry
->size
;
8799 fa
= find_fill_action (&relax_info
->action_list
, sec
, entry_sec_offset
);
8800 removed_diff
= compute_removed_action_diff (fa
, sec
, entry_sec_offset
,
8801 -4, fill_extra_space
);
8803 adjust_fill_action (fa
, removed_diff
);
8805 text_action_add (&relax_info
->action_list
,
8806 ta_fill
, sec
, entry_sec_offset
, removed_diff
);
8813 /* Second relaxation pass. */
8815 /* Modify all of the relocations to point to the right spot, and if this
8816 is a relaxable section, delete the unwanted literals and fix the
8820 relax_section (bfd
*abfd
, asection
*sec
, struct bfd_link_info
*link_info
)
8822 Elf_Internal_Rela
*internal_relocs
;
8823 xtensa_relax_info
*relax_info
;
8825 bfd_boolean ok
= TRUE
;
8827 bfd_boolean rv
= FALSE
;
8828 bfd_boolean virtual_action
;
8829 bfd_size_type sec_size
;
8831 sec_size
= bfd_get_section_limit (abfd
, sec
);
8832 relax_info
= get_xtensa_relax_info (sec
);
8833 BFD_ASSERT (relax_info
);
8835 /* First translate any of the fixes that have been added already. */
8836 translate_section_fixes (sec
);
8838 /* Handle property sections (e.g., literal tables) specially. */
8839 if (xtensa_is_property_section (sec
))
8841 BFD_ASSERT (!relax_info
->is_relaxable_literal_section
);
8842 return relax_property_section (abfd
, sec
, link_info
);
8845 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
8846 link_info
->keep_memory
);
8847 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
8848 if (contents
== NULL
&& sec_size
!= 0)
8854 if (internal_relocs
)
8856 for (i
= 0; i
< sec
->reloc_count
; i
++)
8858 Elf_Internal_Rela
*irel
;
8859 xtensa_relax_info
*target_relax_info
;
8860 bfd_vma source_offset
, old_source_offset
;
8863 asection
*target_sec
;
8865 /* Locally change the source address.
8866 Translate the target to the new target address.
8867 If it points to this section and has been removed,
8871 irel
= &internal_relocs
[i
];
8872 source_offset
= irel
->r_offset
;
8873 old_source_offset
= source_offset
;
8875 r_type
= ELF32_R_TYPE (irel
->r_info
);
8876 r_reloc_init (&r_rel
, abfd
, irel
, contents
,
8877 bfd_get_section_limit (abfd
, sec
));
8879 /* If this section could have changed then we may need to
8880 change the relocation's offset. */
8882 if (relax_info
->is_relaxable_literal_section
8883 || relax_info
->is_relaxable_asm_section
)
8885 pin_internal_relocs (sec
, internal_relocs
);
8887 if (r_type
!= R_XTENSA_NONE
8888 && find_removed_literal (&relax_info
->removed_list
,
8891 /* Remove this relocation. */
8892 if (elf_hash_table (link_info
)->dynamic_sections_created
)
8893 shrink_dynamic_reloc_sections (link_info
, abfd
, sec
, irel
);
8894 irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
8895 irel
->r_offset
= offset_with_removed_text
8896 (&relax_info
->action_list
, irel
->r_offset
);
8900 if (r_type
== R_XTENSA_ASM_SIMPLIFY
)
8902 text_action
*action
=
8903 find_insn_action (&relax_info
->action_list
,
8905 if (action
&& (action
->action
== ta_convert_longcall
8906 || action
->action
== ta_remove_longcall
))
8908 bfd_reloc_status_type retval
;
8909 char *error_message
= NULL
;
8911 retval
= contract_asm_expansion (contents
, sec_size
,
8912 irel
, &error_message
);
8913 if (retval
!= bfd_reloc_ok
)
8915 (*link_info
->callbacks
->reloc_dangerous
)
8916 (link_info
, error_message
, abfd
, sec
,
8920 /* Update the action so that the code that moves
8921 the contents will do the right thing. */
8922 if (action
->action
== ta_remove_longcall
)
8923 action
->action
= ta_remove_insn
;
8925 action
->action
= ta_none
;
8926 /* Refresh the info in the r_rel. */
8927 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
8928 r_type
= ELF32_R_TYPE (irel
->r_info
);
8932 source_offset
= offset_with_removed_text
8933 (&relax_info
->action_list
, irel
->r_offset
);
8934 irel
->r_offset
= source_offset
;
8937 /* If the target section could have changed then
8938 we may need to change the relocation's target offset. */
8940 target_sec
= r_reloc_get_section (&r_rel
);
8942 /* For a reference to a discarded section from a DWARF section,
8943 i.e., where action_discarded is PRETEND, the symbol will
8944 eventually be modified to refer to the kept section (at least if
8945 the kept and discarded sections are the same size). Anticipate
8946 that here and adjust things accordingly. */
8947 if (! elf_xtensa_ignore_discarded_relocs (sec
)
8948 && elf_xtensa_action_discarded (sec
) == PRETEND
8949 && sec
->sec_info_type
!= ELF_INFO_TYPE_STABS
8950 && target_sec
!= NULL
8951 && elf_discarded_section (target_sec
))
8953 /* It would be natural to call _bfd_elf_check_kept_section
8954 here, but it's not exported from elflink.c. It's also a
8955 fairly expensive check. Adjusting the relocations to the
8956 discarded section is fairly harmless; it will only adjust
8957 some addends and difference values. If it turns out that
8958 _bfd_elf_check_kept_section fails later, it won't matter,
8959 so just compare the section names to find the right group
8961 asection
*kept
= target_sec
->kept_section
;
8964 if ((kept
->flags
& SEC_GROUP
) != 0)
8966 asection
*first
= elf_next_in_group (kept
);
8967 asection
*s
= first
;
8972 if (strcmp (s
->name
, target_sec
->name
) == 0)
8977 s
= elf_next_in_group (s
);
8984 && ((target_sec
->rawsize
!= 0
8985 ? target_sec
->rawsize
: target_sec
->size
)
8986 == (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
8990 target_relax_info
= get_xtensa_relax_info (target_sec
);
8991 if (target_relax_info
8992 && (target_relax_info
->is_relaxable_literal_section
8993 || target_relax_info
->is_relaxable_asm_section
))
8996 target_sec
= translate_reloc (&r_rel
, &new_reloc
, target_sec
);
8998 if (r_type
== R_XTENSA_DIFF8
8999 || r_type
== R_XTENSA_DIFF16
9000 || r_type
== R_XTENSA_DIFF32
)
9002 bfd_vma diff_value
= 0, new_end_offset
, diff_mask
= 0;
9004 if (bfd_get_section_limit (abfd
, sec
) < old_source_offset
)
9006 (*link_info
->callbacks
->reloc_dangerous
)
9007 (link_info
, _("invalid relocation address"),
9008 abfd
, sec
, old_source_offset
);
9014 case R_XTENSA_DIFF8
:
9016 bfd_get_8 (abfd
, &contents
[old_source_offset
]);
9018 case R_XTENSA_DIFF16
:
9020 bfd_get_16 (abfd
, &contents
[old_source_offset
]);
9022 case R_XTENSA_DIFF32
:
9024 bfd_get_32 (abfd
, &contents
[old_source_offset
]);
9028 new_end_offset
= offset_with_removed_text
9029 (&target_relax_info
->action_list
,
9030 r_rel
.target_offset
+ diff_value
);
9031 diff_value
= new_end_offset
- new_reloc
.target_offset
;
9035 case R_XTENSA_DIFF8
:
9037 bfd_put_8 (abfd
, diff_value
,
9038 &contents
[old_source_offset
]);
9040 case R_XTENSA_DIFF16
:
9042 bfd_put_16 (abfd
, diff_value
,
9043 &contents
[old_source_offset
]);
9045 case R_XTENSA_DIFF32
:
9046 diff_mask
= 0xffffffff;
9047 bfd_put_32 (abfd
, diff_value
,
9048 &contents
[old_source_offset
]);
9052 /* Check for overflow. */
9053 if ((diff_value
& ~diff_mask
) != 0)
9055 (*link_info
->callbacks
->reloc_dangerous
)
9056 (link_info
, _("overflow after relaxation"),
9057 abfd
, sec
, old_source_offset
);
9061 pin_contents (sec
, contents
);
9064 /* If the relocation still references a section in the same
9065 input file, modify the relocation directly instead of
9066 adding a "fix" record. */
9067 if (target_sec
->owner
== abfd
)
9069 unsigned r_symndx
= ELF32_R_SYM (new_reloc
.rela
.r_info
);
9070 irel
->r_info
= ELF32_R_INFO (r_symndx
, r_type
);
9071 irel
->r_addend
= new_reloc
.rela
.r_addend
;
9072 pin_internal_relocs (sec
, internal_relocs
);
9076 bfd_vma addend_displacement
;
9079 addend_displacement
=
9080 new_reloc
.target_offset
+ new_reloc
.virtual_offset
;
9081 fix
= reloc_bfd_fix_init (sec
, source_offset
, r_type
,
9083 addend_displacement
, TRUE
);
9090 if ((relax_info
->is_relaxable_literal_section
9091 || relax_info
->is_relaxable_asm_section
)
9092 && relax_info
->action_list
.head
)
9094 /* Walk through the planned actions and build up a table
9095 of move, copy and fill records. Use the move, copy and
9096 fill records to perform the actions once. */
9099 bfd_size_type final_size
, copy_size
, orig_insn_size
;
9100 bfd_byte
*scratch
= NULL
;
9101 bfd_byte
*dup_contents
= NULL
;
9102 bfd_size_type orig_size
= sec
->size
;
9103 bfd_vma orig_dot
= 0;
9104 bfd_vma orig_dot_copied
= 0; /* Byte copied already from
9105 orig dot in physical memory. */
9106 bfd_vma orig_dot_vo
= 0; /* Virtual offset from orig_dot. */
9107 bfd_vma dup_dot
= 0;
9109 text_action
*action
= relax_info
->action_list
.head
;
9111 final_size
= sec
->size
;
9112 for (action
= relax_info
->action_list
.head
; action
;
9113 action
= action
->next
)
9115 final_size
-= action
->removed_bytes
;
9118 scratch
= (bfd_byte
*) bfd_zmalloc (final_size
);
9119 dup_contents
= (bfd_byte
*) bfd_zmalloc (final_size
);
9121 /* The dot is the current fill location. */
9123 print_action_list (stderr
, &relax_info
->action_list
);
9126 for (action
= relax_info
->action_list
.head
; action
;
9127 action
= action
->next
)
9129 virtual_action
= FALSE
;
9130 if (action
->offset
> orig_dot
)
9132 orig_dot
+= orig_dot_copied
;
9133 orig_dot_copied
= 0;
9135 /* Out of the virtual world. */
9138 if (action
->offset
> orig_dot
)
9140 copy_size
= action
->offset
- orig_dot
;
9141 memmove (&dup_contents
[dup_dot
], &contents
[orig_dot
], copy_size
);
9142 orig_dot
+= copy_size
;
9143 dup_dot
+= copy_size
;
9144 BFD_ASSERT (action
->offset
== orig_dot
);
9146 else if (action
->offset
< orig_dot
)
9148 if (action
->action
== ta_fill
9149 && action
->offset
- action
->removed_bytes
== orig_dot
)
9151 /* This is OK because the fill only effects the dup_dot. */
9153 else if (action
->action
== ta_add_literal
)
9155 /* TBD. Might need to handle this. */
9158 if (action
->offset
== orig_dot
)
9160 if (action
->virtual_offset
> orig_dot_vo
)
9162 if (orig_dot_vo
== 0)
9164 /* Need to copy virtual_offset bytes. Probably four. */
9165 copy_size
= action
->virtual_offset
- orig_dot_vo
;
9166 memmove (&dup_contents
[dup_dot
],
9167 &contents
[orig_dot
], copy_size
);
9168 orig_dot_copied
= copy_size
;
9169 dup_dot
+= copy_size
;
9171 virtual_action
= TRUE
;
9174 BFD_ASSERT (action
->virtual_offset
<= orig_dot_vo
);
9176 switch (action
->action
)
9178 case ta_remove_literal
:
9179 case ta_remove_insn
:
9180 BFD_ASSERT (action
->removed_bytes
>= 0);
9181 orig_dot
+= action
->removed_bytes
;
9184 case ta_narrow_insn
:
9187 memmove (scratch
, &contents
[orig_dot
], orig_insn_size
);
9188 BFD_ASSERT (action
->removed_bytes
== 1);
9189 rv
= narrow_instruction (scratch
, final_size
, 0);
9191 memmove (&dup_contents
[dup_dot
], scratch
, copy_size
);
9192 orig_dot
+= orig_insn_size
;
9193 dup_dot
+= copy_size
;
9197 if (action
->removed_bytes
>= 0)
9198 orig_dot
+= action
->removed_bytes
;
9201 /* Already zeroed in dup_contents. Just bump the
9203 dup_dot
+= (-action
->removed_bytes
);
9208 BFD_ASSERT (action
->removed_bytes
== 0);
9211 case ta_convert_longcall
:
9212 case ta_remove_longcall
:
9213 /* These will be removed or converted before we get here. */
9220 memmove (scratch
, &contents
[orig_dot
], orig_insn_size
);
9221 BFD_ASSERT (action
->removed_bytes
== -1);
9222 rv
= widen_instruction (scratch
, final_size
, 0);
9224 memmove (&dup_contents
[dup_dot
], scratch
, copy_size
);
9225 orig_dot
+= orig_insn_size
;
9226 dup_dot
+= copy_size
;
9229 case ta_add_literal
:
9232 BFD_ASSERT (action
->removed_bytes
== -4);
9233 /* TBD -- place the literal value here and insert
9235 memset (&dup_contents
[dup_dot
], 0, 4);
9236 pin_internal_relocs (sec
, internal_relocs
);
9237 pin_contents (sec
, contents
);
9239 if (!move_literal (abfd
, link_info
, sec
, dup_dot
, dup_contents
,
9240 relax_info
, &internal_relocs
, &action
->value
))
9244 orig_dot_vo
+= copy_size
;
9246 orig_dot
+= orig_insn_size
;
9247 dup_dot
+= copy_size
;
9251 /* Not implemented yet. */
9256 removed
+= action
->removed_bytes
;
9257 BFD_ASSERT (dup_dot
<= final_size
);
9258 BFD_ASSERT (orig_dot
<= orig_size
);
9261 orig_dot
+= orig_dot_copied
;
9262 orig_dot_copied
= 0;
9264 if (orig_dot
!= orig_size
)
9266 copy_size
= orig_size
- orig_dot
;
9267 BFD_ASSERT (orig_size
> orig_dot
);
9268 BFD_ASSERT (dup_dot
+ copy_size
== final_size
);
9269 memmove (&dup_contents
[dup_dot
], &contents
[orig_dot
], copy_size
);
9270 orig_dot
+= copy_size
;
9271 dup_dot
+= copy_size
;
9273 BFD_ASSERT (orig_size
== orig_dot
);
9274 BFD_ASSERT (final_size
== dup_dot
);
9276 /* Move the dup_contents back. */
9277 if (final_size
> orig_size
)
9279 /* Contents need to be reallocated. Swap the dup_contents into
9281 sec
->contents
= dup_contents
;
9283 contents
= dup_contents
;
9284 pin_contents (sec
, contents
);
9288 BFD_ASSERT (final_size
<= orig_size
);
9289 memset (contents
, 0, orig_size
);
9290 memcpy (contents
, dup_contents
, final_size
);
9291 free (dup_contents
);
9294 pin_contents (sec
, contents
);
9296 if (sec
->rawsize
== 0)
9297 sec
->rawsize
= sec
->size
;
9298 sec
->size
= final_size
;
9302 release_internal_relocs (sec
, internal_relocs
);
9303 release_contents (sec
, contents
);
9309 translate_section_fixes (asection
*sec
)
9311 xtensa_relax_info
*relax_info
;
9314 relax_info
= get_xtensa_relax_info (sec
);
9318 for (r
= relax_info
->fix_list
; r
!= NULL
; r
= r
->next
)
9319 if (!translate_reloc_bfd_fix (r
))
9326 /* Translate a fix given the mapping in the relax info for the target
9327 section. If it has already been translated, no work is required. */
9330 translate_reloc_bfd_fix (reloc_bfd_fix
*fix
)
9332 reloc_bfd_fix new_fix
;
9334 xtensa_relax_info
*relax_info
;
9335 removed_literal
*removed
;
9336 bfd_vma new_offset
, target_offset
;
9338 if (fix
->translated
)
9341 sec
= fix
->target_sec
;
9342 target_offset
= fix
->target_offset
;
9344 relax_info
= get_xtensa_relax_info (sec
);
9347 fix
->translated
= TRUE
;
9353 /* The fix does not need to be translated if the section cannot change. */
9354 if (!relax_info
->is_relaxable_literal_section
9355 && !relax_info
->is_relaxable_asm_section
)
9357 fix
->translated
= TRUE
;
9361 /* If the literal has been moved and this relocation was on an
9362 opcode, then the relocation should move to the new literal
9363 location. Otherwise, the relocation should move within the
9367 if (is_operand_relocation (fix
->src_type
))
9369 /* Check if the original relocation is against a literal being
9371 removed
= find_removed_literal (&relax_info
->removed_list
,
9379 /* The fact that there is still a relocation to this literal indicates
9380 that the literal is being coalesced, not simply removed. */
9381 BFD_ASSERT (removed
->to
.abfd
!= NULL
);
9383 /* This was moved to some other address (possibly another section). */
9384 new_sec
= r_reloc_get_section (&removed
->to
);
9388 relax_info
= get_xtensa_relax_info (sec
);
9390 (!relax_info
->is_relaxable_literal_section
9391 && !relax_info
->is_relaxable_asm_section
))
9393 target_offset
= removed
->to
.target_offset
;
9394 new_fix
.target_sec
= new_sec
;
9395 new_fix
.target_offset
= target_offset
;
9396 new_fix
.translated
= TRUE
;
9401 target_offset
= removed
->to
.target_offset
;
9402 new_fix
.target_sec
= new_sec
;
9405 /* The target address may have been moved within its section. */
9406 new_offset
= offset_with_removed_text (&relax_info
->action_list
,
9409 new_fix
.target_offset
= new_offset
;
9410 new_fix
.target_offset
= new_offset
;
9411 new_fix
.translated
= TRUE
;
9417 /* Fix up a relocation to take account of removed literals. */
9420 translate_reloc (const r_reloc
*orig_rel
, r_reloc
*new_rel
, asection
*sec
)
9422 xtensa_relax_info
*relax_info
;
9423 removed_literal
*removed
;
9424 bfd_vma target_offset
, base_offset
;
9427 *new_rel
= *orig_rel
;
9429 if (!r_reloc_is_defined (orig_rel
))
9432 relax_info
= get_xtensa_relax_info (sec
);
9433 BFD_ASSERT (relax_info
&& (relax_info
->is_relaxable_literal_section
9434 || relax_info
->is_relaxable_asm_section
));
9436 target_offset
= orig_rel
->target_offset
;
9439 if (is_operand_relocation (ELF32_R_TYPE (orig_rel
->rela
.r_info
)))
9441 /* Check if the original relocation is against a literal being
9443 removed
= find_removed_literal (&relax_info
->removed_list
,
9446 if (removed
&& removed
->to
.abfd
)
9450 /* The fact that there is still a relocation to this literal indicates
9451 that the literal is being coalesced, not simply removed. */
9452 BFD_ASSERT (removed
->to
.abfd
!= NULL
);
9454 /* This was moved to some other address
9455 (possibly in another section). */
9456 *new_rel
= removed
->to
;
9457 new_sec
= r_reloc_get_section (new_rel
);
9461 relax_info
= get_xtensa_relax_info (sec
);
9463 || (!relax_info
->is_relaxable_literal_section
9464 && !relax_info
->is_relaxable_asm_section
))
9467 target_offset
= new_rel
->target_offset
;
9470 /* Find the base offset of the reloc symbol, excluding any addend from the
9471 reloc or from the section contents (for a partial_inplace reloc). Then
9472 find the adjusted values of the offsets due to relaxation. The base
9473 offset is needed to determine the change to the reloc's addend; the reloc
9474 addend should not be adjusted due to relaxations located before the base
9477 base_offset
= r_reloc_get_target_offset (new_rel
) - new_rel
->rela
.r_addend
;
9478 act
= relax_info
->action_list
.head
;
9479 if (base_offset
<= target_offset
)
9481 int base_removed
= removed_by_actions (&act
, base_offset
, FALSE
);
9482 int addend_removed
= removed_by_actions (&act
, target_offset
, FALSE
);
9483 new_rel
->target_offset
= target_offset
- base_removed
- addend_removed
;
9484 new_rel
->rela
.r_addend
-= addend_removed
;
9488 /* Handle a negative addend. The base offset comes first. */
9489 int tgt_removed
= removed_by_actions (&act
, target_offset
, FALSE
);
9490 int addend_removed
= removed_by_actions (&act
, base_offset
, FALSE
);
9491 new_rel
->target_offset
= target_offset
- tgt_removed
;
9492 new_rel
->rela
.r_addend
+= addend_removed
;
9499 /* For dynamic links, there may be a dynamic relocation for each
9500 literal. The number of dynamic relocations must be computed in
9501 size_dynamic_sections, which occurs before relaxation. When a
9502 literal is removed, this function checks if there is a corresponding
9503 dynamic relocation and shrinks the size of the appropriate dynamic
9504 relocation section accordingly. At this point, the contents of the
9505 dynamic relocation sections have not yet been filled in, so there's
9506 nothing else that needs to be done. */
9509 shrink_dynamic_reloc_sections (struct bfd_link_info
*info
,
9511 asection
*input_section
,
9512 Elf_Internal_Rela
*rel
)
9514 struct elf_xtensa_link_hash_table
*htab
;
9515 Elf_Internal_Shdr
*symtab_hdr
;
9516 struct elf_link_hash_entry
**sym_hashes
;
9517 unsigned long r_symndx
;
9519 struct elf_link_hash_entry
*h
;
9520 bfd_boolean dynamic_symbol
;
9522 htab
= elf_xtensa_hash_table (info
);
9526 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9527 sym_hashes
= elf_sym_hashes (abfd
);
9529 r_type
= ELF32_R_TYPE (rel
->r_info
);
9530 r_symndx
= ELF32_R_SYM (rel
->r_info
);
9532 if (r_symndx
< symtab_hdr
->sh_info
)
9535 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
9537 dynamic_symbol
= elf_xtensa_dynamic_symbol_p (h
, info
);
9539 if ((r_type
== R_XTENSA_32
|| r_type
== R_XTENSA_PLT
)
9540 && (input_section
->flags
& SEC_ALLOC
) != 0
9541 && (dynamic_symbol
|| info
->shared
))
9544 bfd_boolean is_plt
= FALSE
;
9546 if (dynamic_symbol
&& r_type
== R_XTENSA_PLT
)
9548 srel
= htab
->srelplt
;
9552 srel
= htab
->srelgot
;
9554 /* Reduce size of the .rela.* section by one reloc. */
9555 BFD_ASSERT (srel
!= NULL
);
9556 BFD_ASSERT (srel
->size
>= sizeof (Elf32_External_Rela
));
9557 srel
->size
-= sizeof (Elf32_External_Rela
);
9561 asection
*splt
, *sgotplt
, *srelgot
;
9562 int reloc_index
, chunk
;
9564 /* Find the PLT reloc index of the entry being removed. This
9565 is computed from the size of ".rela.plt". It is needed to
9566 figure out which PLT chunk to resize. Usually "last index
9567 = size - 1" since the index starts at zero, but in this
9568 context, the size has just been decremented so there's no
9569 need to subtract one. */
9570 reloc_index
= srel
->size
/ sizeof (Elf32_External_Rela
);
9572 chunk
= reloc_index
/ PLT_ENTRIES_PER_CHUNK
;
9573 splt
= elf_xtensa_get_plt_section (info
, chunk
);
9574 sgotplt
= elf_xtensa_get_gotplt_section (info
, chunk
);
9575 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
);
9577 /* Check if an entire PLT chunk has just been eliminated. */
9578 if (reloc_index
% PLT_ENTRIES_PER_CHUNK
== 0)
9580 /* The two magic GOT entries for that chunk can go away. */
9581 srelgot
= htab
->srelgot
;
9582 BFD_ASSERT (srelgot
!= NULL
);
9583 srelgot
->reloc_count
-= 2;
9584 srelgot
->size
-= 2 * sizeof (Elf32_External_Rela
);
9587 /* There should be only one entry left (and it will be
9589 BFD_ASSERT (sgotplt
->size
== 4);
9590 BFD_ASSERT (splt
->size
== PLT_ENTRY_SIZE
);
9593 BFD_ASSERT (sgotplt
->size
>= 4);
9594 BFD_ASSERT (splt
->size
>= PLT_ENTRY_SIZE
);
9597 splt
->size
-= PLT_ENTRY_SIZE
;
9603 /* Take an r_rel and move it to another section. This usually
9604 requires extending the interal_relocation array and pinning it. If
9605 the original r_rel is from the same BFD, we can complete this here.
9606 Otherwise, we add a fix record to let the final link fix the
9607 appropriate address. Contents and internal relocations for the
9608 section must be pinned after calling this routine. */
9611 move_literal (bfd
*abfd
,
9612 struct bfd_link_info
*link_info
,
9616 xtensa_relax_info
*relax_info
,
9617 Elf_Internal_Rela
**internal_relocs_p
,
9618 const literal_value
*lit
)
9620 Elf_Internal_Rela
*new_relocs
= NULL
;
9621 size_t new_relocs_count
= 0;
9622 Elf_Internal_Rela this_rela
;
9623 const r_reloc
*r_rel
;
9625 r_rel
= &lit
->r_rel
;
9626 BFD_ASSERT (elf_section_data (sec
)->relocs
== *internal_relocs_p
);
9628 if (r_reloc_is_const (r_rel
))
9629 bfd_put_32 (abfd
, lit
->value
, contents
+ offset
);
9634 asection
*target_sec
;
9638 r_type
= ELF32_R_TYPE (r_rel
->rela
.r_info
);
9639 target_sec
= r_reloc_get_section (r_rel
);
9641 /* This is the difficult case. We have to create a fix up. */
9642 this_rela
.r_offset
= offset
;
9643 this_rela
.r_info
= ELF32_R_INFO (0, r_type
);
9644 this_rela
.r_addend
=
9645 r_rel
->target_offset
- r_reloc_get_target_offset (r_rel
);
9646 bfd_put_32 (abfd
, lit
->value
, contents
+ offset
);
9648 /* Currently, we cannot move relocations during a relocatable link. */
9649 BFD_ASSERT (!link_info
->relocatable
);
9650 fix
= reloc_bfd_fix_init (sec
, offset
, r_type
,
9651 r_reloc_get_section (r_rel
),
9652 r_rel
->target_offset
+ r_rel
->virtual_offset
,
9654 /* We also need to mark that relocations are needed here. */
9655 sec
->flags
|= SEC_RELOC
;
9657 translate_reloc_bfd_fix (fix
);
9658 /* This fix has not yet been translated. */
9661 /* Add the relocation. If we have already allocated our own
9662 space for the relocations and we have room for more, then use
9663 it. Otherwise, allocate new space and move the literals. */
9664 insert_at
= sec
->reloc_count
;
9665 for (i
= 0; i
< sec
->reloc_count
; ++i
)
9667 if (this_rela
.r_offset
< (*internal_relocs_p
)[i
].r_offset
)
9674 if (*internal_relocs_p
!= relax_info
->allocated_relocs
9675 || sec
->reloc_count
+ 1 > relax_info
->allocated_relocs_count
)
9677 BFD_ASSERT (relax_info
->allocated_relocs
== NULL
9678 || sec
->reloc_count
== relax_info
->relocs_count
);
9680 if (relax_info
->allocated_relocs_count
== 0)
9681 new_relocs_count
= (sec
->reloc_count
+ 2) * 2;
9683 new_relocs_count
= (relax_info
->allocated_relocs_count
+ 2) * 2;
9685 new_relocs
= (Elf_Internal_Rela
*)
9686 bfd_zmalloc (sizeof (Elf_Internal_Rela
) * (new_relocs_count
));
9690 /* We could handle this more quickly by finding the split point. */
9692 memcpy (new_relocs
, *internal_relocs_p
,
9693 insert_at
* sizeof (Elf_Internal_Rela
));
9695 new_relocs
[insert_at
] = this_rela
;
9697 if (insert_at
!= sec
->reloc_count
)
9698 memcpy (new_relocs
+ insert_at
+ 1,
9699 (*internal_relocs_p
) + insert_at
,
9700 (sec
->reloc_count
- insert_at
)
9701 * sizeof (Elf_Internal_Rela
));
9703 if (*internal_relocs_p
!= relax_info
->allocated_relocs
)
9705 /* The first time we re-allocate, we can only free the
9706 old relocs if they were allocated with bfd_malloc.
9707 This is not true when keep_memory is in effect. */
9708 if (!link_info
->keep_memory
)
9709 free (*internal_relocs_p
);
9712 free (*internal_relocs_p
);
9713 relax_info
->allocated_relocs
= new_relocs
;
9714 relax_info
->allocated_relocs_count
= new_relocs_count
;
9715 elf_section_data (sec
)->relocs
= new_relocs
;
9717 relax_info
->relocs_count
= sec
->reloc_count
;
9718 *internal_relocs_p
= new_relocs
;
9722 if (insert_at
!= sec
->reloc_count
)
9725 for (idx
= sec
->reloc_count
; idx
> insert_at
; idx
--)
9726 (*internal_relocs_p
)[idx
] = (*internal_relocs_p
)[idx
-1];
9728 (*internal_relocs_p
)[insert_at
] = this_rela
;
9730 if (relax_info
->allocated_relocs
)
9731 relax_info
->relocs_count
= sec
->reloc_count
;
9738 /* This is similar to relax_section except that when a target is moved,
9739 we shift addresses up. We also need to modify the size. This
9740 algorithm does NOT allow for relocations into the middle of the
9741 property sections. */
9744 relax_property_section (bfd
*abfd
,
9746 struct bfd_link_info
*link_info
)
9748 Elf_Internal_Rela
*internal_relocs
;
9751 bfd_boolean ok
= TRUE
;
9752 bfd_boolean is_full_prop_section
;
9753 size_t last_zfill_target_offset
= 0;
9754 asection
*last_zfill_target_sec
= NULL
;
9755 bfd_size_type sec_size
;
9756 bfd_size_type entry_size
;
9758 sec_size
= bfd_get_section_limit (abfd
, sec
);
9759 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
9760 link_info
->keep_memory
);
9761 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
9762 if (contents
== NULL
&& sec_size
!= 0)
9768 is_full_prop_section
= xtensa_is_proptable_section (sec
);
9769 if (is_full_prop_section
)
9774 if (internal_relocs
)
9776 for (i
= 0; i
< sec
->reloc_count
; i
++)
9778 Elf_Internal_Rela
*irel
;
9779 xtensa_relax_info
*target_relax_info
;
9781 asection
*target_sec
;
9783 bfd_byte
*size_p
, *flags_p
;
9785 /* Locally change the source address.
9786 Translate the target to the new target address.
9787 If it points to this section and has been removed, MOVE IT.
9788 Also, don't forget to modify the associated SIZE at
9791 irel
= &internal_relocs
[i
];
9792 r_type
= ELF32_R_TYPE (irel
->r_info
);
9793 if (r_type
== R_XTENSA_NONE
)
9796 /* Find the literal value. */
9797 r_reloc_init (&val
.r_rel
, abfd
, irel
, contents
, sec_size
);
9798 size_p
= &contents
[irel
->r_offset
+ 4];
9800 if (is_full_prop_section
)
9801 flags_p
= &contents
[irel
->r_offset
+ 8];
9802 BFD_ASSERT (irel
->r_offset
+ entry_size
<= sec_size
);
9804 target_sec
= r_reloc_get_section (&val
.r_rel
);
9805 target_relax_info
= get_xtensa_relax_info (target_sec
);
9807 if (target_relax_info
9808 && (target_relax_info
->is_relaxable_literal_section
9809 || target_relax_info
->is_relaxable_asm_section
))
9811 /* Translate the relocation's destination. */
9812 bfd_vma old_offset
= val
.r_rel
.target_offset
;
9814 long old_size
, new_size
;
9815 text_action
*act
= target_relax_info
->action_list
.head
;
9816 new_offset
= old_offset
-
9817 removed_by_actions (&act
, old_offset
, FALSE
);
9819 /* Assert that we are not out of bounds. */
9820 old_size
= bfd_get_32 (abfd
, size_p
);
9821 new_size
= old_size
;
9825 /* Only the first zero-sized unreachable entry is
9826 allowed to expand. In this case the new offset
9827 should be the offset before the fill and the new
9828 size is the expansion size. For other zero-sized
9829 entries the resulting size should be zero with an
9830 offset before or after the fill address depending
9831 on whether the expanding unreachable entry
9833 if (last_zfill_target_sec
== 0
9834 || last_zfill_target_sec
!= target_sec
9835 || last_zfill_target_offset
!= old_offset
)
9837 bfd_vma new_end_offset
= new_offset
;
9839 /* Recompute the new_offset, but this time don't
9840 include any fill inserted by relaxation. */
9841 act
= target_relax_info
->action_list
.head
;
9842 new_offset
= old_offset
-
9843 removed_by_actions (&act
, old_offset
, TRUE
);
9845 /* If it is not unreachable and we have not yet
9846 seen an unreachable at this address, place it
9847 before the fill address. */
9848 if (flags_p
&& (bfd_get_32 (abfd
, flags_p
)
9849 & XTENSA_PROP_UNREACHABLE
) != 0)
9851 new_size
= new_end_offset
- new_offset
;
9853 last_zfill_target_sec
= target_sec
;
9854 last_zfill_target_offset
= old_offset
;
9860 removed_by_actions (&act
, old_offset
+ old_size
, TRUE
);
9862 if (new_size
!= old_size
)
9864 bfd_put_32 (abfd
, new_size
, size_p
);
9865 pin_contents (sec
, contents
);
9868 if (new_offset
!= old_offset
)
9870 bfd_vma diff
= new_offset
- old_offset
;
9871 irel
->r_addend
+= diff
;
9872 pin_internal_relocs (sec
, internal_relocs
);
9878 /* Combine adjacent property table entries. This is also done in
9879 finish_dynamic_sections() but at that point it's too late to
9880 reclaim the space in the output section, so we do this twice. */
9882 if (internal_relocs
&& (!link_info
->relocatable
9883 || xtensa_is_littable_section (sec
)))
9885 Elf_Internal_Rela
*last_irel
= NULL
;
9886 Elf_Internal_Rela
*irel
, *next_rel
, *rel_end
;
9887 int removed_bytes
= 0;
9889 flagword predef_flags
;
9891 predef_flags
= xtensa_get_property_predef_flags (sec
);
9893 /* Walk over memory and relocations at the same time.
9894 This REQUIRES that the internal_relocs be sorted by offset. */
9895 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
9896 internal_reloc_compare
);
9898 pin_internal_relocs (sec
, internal_relocs
);
9899 pin_contents (sec
, contents
);
9901 next_rel
= internal_relocs
;
9902 rel_end
= internal_relocs
+ sec
->reloc_count
;
9904 BFD_ASSERT (sec
->size
% entry_size
== 0);
9906 for (offset
= 0; offset
< sec
->size
; offset
+= entry_size
)
9908 Elf_Internal_Rela
*offset_rel
, *extra_rel
;
9909 bfd_vma bytes_to_remove
, size
, actual_offset
;
9910 bfd_boolean remove_this_rel
;
9913 /* Find the first relocation for the entry at the current offset.
9914 Adjust the offsets of any extra relocations for the previous
9919 for (irel
= next_rel
; irel
< rel_end
; irel
++)
9921 if ((irel
->r_offset
== offset
9922 && ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_NONE
)
9923 || irel
->r_offset
> offset
)
9928 irel
->r_offset
-= removed_bytes
;
9932 /* Find the next relocation (if there are any left). */
9936 for (irel
= offset_rel
+ 1; irel
< rel_end
; irel
++)
9938 if (ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_NONE
)
9946 /* Check if there are relocations on the current entry. There
9947 should usually be a relocation on the offset field. If there
9948 are relocations on the size or flags, then we can't optimize
9949 this entry. Also, find the next relocation to examine on the
9953 if (offset_rel
->r_offset
>= offset
+ entry_size
)
9955 next_rel
= offset_rel
;
9956 /* There are no relocations on the current entry, but we
9957 might still be able to remove it if the size is zero. */
9960 else if (offset_rel
->r_offset
> offset
9962 && extra_rel
->r_offset
< offset
+ entry_size
))
9964 /* There is a relocation on the size or flags, so we can't
9965 do anything with this entry. Continue with the next. */
9966 next_rel
= offset_rel
;
9971 BFD_ASSERT (offset_rel
->r_offset
== offset
);
9972 offset_rel
->r_offset
-= removed_bytes
;
9973 next_rel
= offset_rel
+ 1;
9979 remove_this_rel
= FALSE
;
9980 bytes_to_remove
= 0;
9981 actual_offset
= offset
- removed_bytes
;
9982 size
= bfd_get_32 (abfd
, &contents
[actual_offset
+ 4]);
9984 if (is_full_prop_section
)
9985 flags
= bfd_get_32 (abfd
, &contents
[actual_offset
+ 8]);
9987 flags
= predef_flags
;
9990 && (flags
& XTENSA_PROP_ALIGN
) == 0
9991 && (flags
& XTENSA_PROP_UNREACHABLE
) == 0)
9993 /* Always remove entries with zero size and no alignment. */
9994 bytes_to_remove
= entry_size
;
9996 remove_this_rel
= TRUE
;
9999 && ELF32_R_TYPE (offset_rel
->r_info
) == R_XTENSA_32
)
10003 flagword old_flags
;
10005 bfd_get_32 (abfd
, &contents
[last_irel
->r_offset
+ 4]);
10006 bfd_vma old_address
=
10007 (last_irel
->r_addend
10008 + bfd_get_32 (abfd
, &contents
[last_irel
->r_offset
]));
10009 bfd_vma new_address
=
10010 (offset_rel
->r_addend
10011 + bfd_get_32 (abfd
, &contents
[actual_offset
]));
10012 if (is_full_prop_section
)
10013 old_flags
= bfd_get_32
10014 (abfd
, &contents
[last_irel
->r_offset
+ 8]);
10016 old_flags
= predef_flags
;
10018 if ((ELF32_R_SYM (offset_rel
->r_info
)
10019 == ELF32_R_SYM (last_irel
->r_info
))
10020 && old_address
+ old_size
== new_address
10021 && old_flags
== flags
10022 && (old_flags
& XTENSA_PROP_INSN_BRANCH_TARGET
) == 0
10023 && (old_flags
& XTENSA_PROP_INSN_LOOP_TARGET
) == 0)
10025 /* Fix the old size. */
10026 bfd_put_32 (abfd
, old_size
+ size
,
10027 &contents
[last_irel
->r_offset
+ 4]);
10028 bytes_to_remove
= entry_size
;
10029 remove_this_rel
= TRUE
;
10032 last_irel
= offset_rel
;
10035 last_irel
= offset_rel
;
10038 if (remove_this_rel
)
10040 offset_rel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
10041 offset_rel
->r_offset
= 0;
10044 if (bytes_to_remove
!= 0)
10046 removed_bytes
+= bytes_to_remove
;
10047 if (offset
+ bytes_to_remove
< sec
->size
)
10048 memmove (&contents
[actual_offset
],
10049 &contents
[actual_offset
+ bytes_to_remove
],
10050 sec
->size
- offset
- bytes_to_remove
);
10056 /* Fix up any extra relocations on the last entry. */
10057 for (irel
= next_rel
; irel
< rel_end
; irel
++)
10058 irel
->r_offset
-= removed_bytes
;
10060 /* Clear the removed bytes. */
10061 memset (&contents
[sec
->size
- removed_bytes
], 0, removed_bytes
);
10063 if (sec
->rawsize
== 0)
10064 sec
->rawsize
= sec
->size
;
10065 sec
->size
-= removed_bytes
;
10067 if (xtensa_is_littable_section (sec
))
10069 asection
*sgotloc
= elf_xtensa_hash_table (link_info
)->sgotloc
;
10071 sgotloc
->size
-= removed_bytes
;
10077 release_internal_relocs (sec
, internal_relocs
);
10078 release_contents (sec
, contents
);
10083 /* Third relaxation pass. */
10085 /* Change symbol values to account for removed literals. */
10088 relax_section_symbols (bfd
*abfd
, asection
*sec
)
10090 xtensa_relax_info
*relax_info
;
10091 unsigned int sec_shndx
;
10092 Elf_Internal_Shdr
*symtab_hdr
;
10093 Elf_Internal_Sym
*isymbuf
;
10094 unsigned i
, num_syms
, num_locals
;
10096 relax_info
= get_xtensa_relax_info (sec
);
10097 BFD_ASSERT (relax_info
);
10099 if (!relax_info
->is_relaxable_literal_section
10100 && !relax_info
->is_relaxable_asm_section
)
10103 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
10105 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10106 isymbuf
= retrieve_local_syms (abfd
);
10108 num_syms
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
10109 num_locals
= symtab_hdr
->sh_info
;
10111 /* Adjust the local symbols defined in this section. */
10112 for (i
= 0; i
< num_locals
; i
++)
10114 Elf_Internal_Sym
*isym
= &isymbuf
[i
];
10116 if (isym
->st_shndx
== sec_shndx
)
10118 text_action
*act
= relax_info
->action_list
.head
;
10119 bfd_vma orig_addr
= isym
->st_value
;
10121 isym
->st_value
-= removed_by_actions (&act
, orig_addr
, FALSE
);
10123 if (ELF32_ST_TYPE (isym
->st_info
) == STT_FUNC
)
10125 removed_by_actions (&act
, orig_addr
+ isym
->st_size
, FALSE
);
10129 /* Now adjust the global symbols defined in this section. */
10130 for (i
= 0; i
< (num_syms
- num_locals
); i
++)
10132 struct elf_link_hash_entry
*sym_hash
;
10134 sym_hash
= elf_sym_hashes (abfd
)[i
];
10136 if (sym_hash
->root
.type
== bfd_link_hash_warning
)
10137 sym_hash
= (struct elf_link_hash_entry
*) sym_hash
->root
.u
.i
.link
;
10139 if ((sym_hash
->root
.type
== bfd_link_hash_defined
10140 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
10141 && sym_hash
->root
.u
.def
.section
== sec
)
10143 text_action
*act
= relax_info
->action_list
.head
;
10144 bfd_vma orig_addr
= sym_hash
->root
.u
.def
.value
;
10146 sym_hash
->root
.u
.def
.value
-=
10147 removed_by_actions (&act
, orig_addr
, FALSE
);
10149 if (sym_hash
->type
== STT_FUNC
)
10151 removed_by_actions (&act
, orig_addr
+ sym_hash
->size
, FALSE
);
10159 /* "Fix" handling functions, called while performing relocations. */
10162 do_fix_for_relocatable_link (Elf_Internal_Rela
*rel
,
10164 asection
*input_section
,
10165 bfd_byte
*contents
)
10168 asection
*sec
, *old_sec
;
10169 bfd_vma old_offset
;
10170 int r_type
= ELF32_R_TYPE (rel
->r_info
);
10171 reloc_bfd_fix
*fix
;
10173 if (r_type
== R_XTENSA_NONE
)
10176 fix
= get_bfd_fix (input_section
, rel
->r_offset
, r_type
);
10180 r_reloc_init (&r_rel
, input_bfd
, rel
, contents
,
10181 bfd_get_section_limit (input_bfd
, input_section
));
10182 old_sec
= r_reloc_get_section (&r_rel
);
10183 old_offset
= r_rel
.target_offset
;
10185 if (!old_sec
|| !r_reloc_is_defined (&r_rel
))
10187 if (r_type
!= R_XTENSA_ASM_EXPAND
)
10189 (*_bfd_error_handler
)
10190 (_("%B(%A+0x%lx): unexpected fix for %s relocation"),
10191 input_bfd
, input_section
, rel
->r_offset
,
10192 elf_howto_table
[r_type
].name
);
10195 /* Leave it be. Resolution will happen in a later stage. */
10199 sec
= fix
->target_sec
;
10200 rel
->r_addend
+= ((sec
->output_offset
+ fix
->target_offset
)
10201 - (old_sec
->output_offset
+ old_offset
));
10208 do_fix_for_final_link (Elf_Internal_Rela
*rel
,
10210 asection
*input_section
,
10211 bfd_byte
*contents
,
10212 bfd_vma
*relocationp
)
10215 int r_type
= ELF32_R_TYPE (rel
->r_info
);
10216 reloc_bfd_fix
*fix
;
10217 bfd_vma fixup_diff
;
10219 if (r_type
== R_XTENSA_NONE
)
10222 fix
= get_bfd_fix (input_section
, rel
->r_offset
, r_type
);
10226 sec
= fix
->target_sec
;
10228 fixup_diff
= rel
->r_addend
;
10229 if (elf_howto_table
[fix
->src_type
].partial_inplace
)
10231 bfd_vma inplace_val
;
10232 BFD_ASSERT (fix
->src_offset
10233 < bfd_get_section_limit (input_bfd
, input_section
));
10234 inplace_val
= bfd_get_32 (input_bfd
, &contents
[fix
->src_offset
]);
10235 fixup_diff
+= inplace_val
;
10238 *relocationp
= (sec
->output_section
->vma
10239 + sec
->output_offset
10240 + fix
->target_offset
- fixup_diff
);
10244 /* Miscellaneous utility functions.... */
10247 elf_xtensa_get_plt_section (struct bfd_link_info
*info
, int chunk
)
10249 struct elf_xtensa_link_hash_table
*htab
;
10255 htab
= elf_xtensa_hash_table (info
);
10262 dynobj
= elf_hash_table (info
)->dynobj
;
10263 sprintf (plt_name
, ".plt.%u", chunk
);
10264 return bfd_get_section_by_name (dynobj
, plt_name
);
10269 elf_xtensa_get_gotplt_section (struct bfd_link_info
*info
, int chunk
)
10271 struct elf_xtensa_link_hash_table
*htab
;
10277 htab
= elf_xtensa_hash_table (info
);
10280 return htab
->sgotplt
;
10283 dynobj
= elf_hash_table (info
)->dynobj
;
10284 sprintf (got_name
, ".got.plt.%u", chunk
);
10285 return bfd_get_section_by_name (dynobj
, got_name
);
10289 /* Get the input section for a given symbol index.
10291 . a section symbol, return the section;
10292 . a common symbol, return the common section;
10293 . an undefined symbol, return the undefined section;
10294 . an indirect symbol, follow the links;
10295 . an absolute value, return the absolute section. */
10298 get_elf_r_symndx_section (bfd
*abfd
, unsigned long r_symndx
)
10300 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10301 asection
*target_sec
= NULL
;
10302 if (r_symndx
< symtab_hdr
->sh_info
)
10304 Elf_Internal_Sym
*isymbuf
;
10305 unsigned int section_index
;
10307 isymbuf
= retrieve_local_syms (abfd
);
10308 section_index
= isymbuf
[r_symndx
].st_shndx
;
10310 if (section_index
== SHN_UNDEF
)
10311 target_sec
= bfd_und_section_ptr
;
10312 else if (section_index
== SHN_ABS
)
10313 target_sec
= bfd_abs_section_ptr
;
10314 else if (section_index
== SHN_COMMON
)
10315 target_sec
= bfd_com_section_ptr
;
10317 target_sec
= bfd_section_from_elf_index (abfd
, section_index
);
10321 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
10322 struct elf_link_hash_entry
*h
= elf_sym_hashes (abfd
)[indx
];
10324 while (h
->root
.type
== bfd_link_hash_indirect
10325 || h
->root
.type
== bfd_link_hash_warning
)
10326 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10328 switch (h
->root
.type
)
10330 case bfd_link_hash_defined
:
10331 case bfd_link_hash_defweak
:
10332 target_sec
= h
->root
.u
.def
.section
;
10334 case bfd_link_hash_common
:
10335 target_sec
= bfd_com_section_ptr
;
10337 case bfd_link_hash_undefined
:
10338 case bfd_link_hash_undefweak
:
10339 target_sec
= bfd_und_section_ptr
;
10341 default: /* New indirect warning. */
10342 target_sec
= bfd_und_section_ptr
;
10350 static struct elf_link_hash_entry
*
10351 get_elf_r_symndx_hash_entry (bfd
*abfd
, unsigned long r_symndx
)
10353 unsigned long indx
;
10354 struct elf_link_hash_entry
*h
;
10355 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10357 if (r_symndx
< symtab_hdr
->sh_info
)
10360 indx
= r_symndx
- symtab_hdr
->sh_info
;
10361 h
= elf_sym_hashes (abfd
)[indx
];
10362 while (h
->root
.type
== bfd_link_hash_indirect
10363 || h
->root
.type
== bfd_link_hash_warning
)
10364 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10369 /* Get the section-relative offset for a symbol number. */
10372 get_elf_r_symndx_offset (bfd
*abfd
, unsigned long r_symndx
)
10374 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
10375 bfd_vma offset
= 0;
10377 if (r_symndx
< symtab_hdr
->sh_info
)
10379 Elf_Internal_Sym
*isymbuf
;
10380 isymbuf
= retrieve_local_syms (abfd
);
10381 offset
= isymbuf
[r_symndx
].st_value
;
10385 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
10386 struct elf_link_hash_entry
*h
=
10387 elf_sym_hashes (abfd
)[indx
];
10389 while (h
->root
.type
== bfd_link_hash_indirect
10390 || h
->root
.type
== bfd_link_hash_warning
)
10391 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10392 if (h
->root
.type
== bfd_link_hash_defined
10393 || h
->root
.type
== bfd_link_hash_defweak
)
10394 offset
= h
->root
.u
.def
.value
;
10401 is_reloc_sym_weak (bfd
*abfd
, Elf_Internal_Rela
*rel
)
10403 unsigned long r_symndx
= ELF32_R_SYM (rel
->r_info
);
10404 struct elf_link_hash_entry
*h
;
10406 h
= get_elf_r_symndx_hash_entry (abfd
, r_symndx
);
10407 if (h
&& h
->root
.type
== bfd_link_hash_defweak
)
10414 pcrel_reloc_fits (xtensa_opcode opc
,
10416 bfd_vma self_address
,
10417 bfd_vma dest_address
)
10419 xtensa_isa isa
= xtensa_default_isa
;
10420 uint32 valp
= dest_address
;
10421 if (xtensa_operand_do_reloc (isa
, opc
, opnd
, &valp
, self_address
)
10422 || xtensa_operand_encode (isa
, opc
, opnd
, &valp
))
10429 xtensa_is_property_section (asection
*sec
)
10431 if (xtensa_is_insntable_section (sec
)
10432 || xtensa_is_littable_section (sec
)
10433 || xtensa_is_proptable_section (sec
))
10441 xtensa_is_insntable_section (asection
*sec
)
10443 if (CONST_STRNEQ (sec
->name
, XTENSA_INSN_SEC_NAME
)
10444 || CONST_STRNEQ (sec
->name
, ".gnu.linkonce.x."))
10452 xtensa_is_littable_section (asection
*sec
)
10454 if (CONST_STRNEQ (sec
->name
, XTENSA_LIT_SEC_NAME
)
10455 || CONST_STRNEQ (sec
->name
, ".gnu.linkonce.p."))
10463 xtensa_is_proptable_section (asection
*sec
)
10465 if (CONST_STRNEQ (sec
->name
, XTENSA_PROP_SEC_NAME
)
10466 || CONST_STRNEQ (sec
->name
, ".gnu.linkonce.prop."))
10474 internal_reloc_compare (const void *ap
, const void *bp
)
10476 const Elf_Internal_Rela
*a
= (const Elf_Internal_Rela
*) ap
;
10477 const Elf_Internal_Rela
*b
= (const Elf_Internal_Rela
*) bp
;
10479 if (a
->r_offset
!= b
->r_offset
)
10480 return (a
->r_offset
- b
->r_offset
);
10482 /* We don't need to sort on these criteria for correctness,
10483 but enforcing a more strict ordering prevents unstable qsort
10484 from behaving differently with different implementations.
10485 Without the code below we get correct but different results
10486 on Solaris 2.7 and 2.8. We would like to always produce the
10487 same results no matter the host. */
10489 if (a
->r_info
!= b
->r_info
)
10490 return (a
->r_info
- b
->r_info
);
10492 return (a
->r_addend
- b
->r_addend
);
10497 internal_reloc_matches (const void *ap
, const void *bp
)
10499 const Elf_Internal_Rela
*a
= (const Elf_Internal_Rela
*) ap
;
10500 const Elf_Internal_Rela
*b
= (const Elf_Internal_Rela
*) bp
;
10502 /* Check if one entry overlaps with the other; this shouldn't happen
10503 except when searching for a match. */
10504 return (a
->r_offset
- b
->r_offset
);
10508 /* Predicate function used to look up a section in a particular group. */
10511 match_section_group (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *inf
)
10513 const char *gname
= inf
;
10514 const char *group_name
= elf_group_name (sec
);
10516 return (group_name
== gname
10517 || (group_name
!= NULL
10519 && strcmp (group_name
, gname
) == 0));
10523 static int linkonce_len
= sizeof (".gnu.linkonce.") - 1;
10526 xtensa_property_section_name (asection
*sec
, const char *base_name
)
10528 const char *suffix
, *group_name
;
10529 char *prop_sec_name
;
10531 group_name
= elf_group_name (sec
);
10534 suffix
= strrchr (sec
->name
, '.');
10535 if (suffix
== sec
->name
)
10537 prop_sec_name
= (char *) bfd_malloc (strlen (base_name
) + 1
10538 + (suffix
? strlen (suffix
) : 0));
10539 strcpy (prop_sec_name
, base_name
);
10541 strcat (prop_sec_name
, suffix
);
10543 else if (strncmp (sec
->name
, ".gnu.linkonce.", linkonce_len
) == 0)
10545 char *linkonce_kind
= 0;
10547 if (strcmp (base_name
, XTENSA_INSN_SEC_NAME
) == 0)
10548 linkonce_kind
= "x.";
10549 else if (strcmp (base_name
, XTENSA_LIT_SEC_NAME
) == 0)
10550 linkonce_kind
= "p.";
10551 else if (strcmp (base_name
, XTENSA_PROP_SEC_NAME
) == 0)
10552 linkonce_kind
= "prop.";
10556 prop_sec_name
= (char *) bfd_malloc (strlen (sec
->name
)
10557 + strlen (linkonce_kind
) + 1);
10558 memcpy (prop_sec_name
, ".gnu.linkonce.", linkonce_len
);
10559 strcpy (prop_sec_name
+ linkonce_len
, linkonce_kind
);
10561 suffix
= sec
->name
+ linkonce_len
;
10562 /* For backward compatibility, replace "t." instead of inserting
10563 the new linkonce_kind (but not for "prop" sections). */
10564 if (CONST_STRNEQ (suffix
, "t.") && linkonce_kind
[1] == '.')
10566 strcat (prop_sec_name
+ linkonce_len
, suffix
);
10569 prop_sec_name
= strdup (base_name
);
10571 return prop_sec_name
;
10576 xtensa_get_property_section (asection
*sec
, const char *base_name
)
10578 char *prop_sec_name
;
10579 asection
*prop_sec
;
10581 prop_sec_name
= xtensa_property_section_name (sec
, base_name
);
10582 prop_sec
= bfd_get_section_by_name_if (sec
->owner
, prop_sec_name
,
10583 match_section_group
,
10584 (void *) elf_group_name (sec
));
10585 free (prop_sec_name
);
10591 xtensa_make_property_section (asection
*sec
, const char *base_name
)
10593 char *prop_sec_name
;
10594 asection
*prop_sec
;
10596 /* Check if the section already exists. */
10597 prop_sec_name
= xtensa_property_section_name (sec
, base_name
);
10598 prop_sec
= bfd_get_section_by_name_if (sec
->owner
, prop_sec_name
,
10599 match_section_group
,
10600 (void *) elf_group_name (sec
));
10601 /* If not, create it. */
10604 flagword flags
= (SEC_RELOC
| SEC_HAS_CONTENTS
| SEC_READONLY
);
10605 flags
|= (bfd_get_section_flags (sec
->owner
, sec
)
10606 & (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
));
10608 prop_sec
= bfd_make_section_anyway_with_flags
10609 (sec
->owner
, strdup (prop_sec_name
), flags
);
10613 elf_group_name (prop_sec
) = elf_group_name (sec
);
10616 free (prop_sec_name
);
10622 xtensa_get_property_predef_flags (asection
*sec
)
10624 if (xtensa_is_insntable_section (sec
))
10625 return (XTENSA_PROP_INSN
10626 | XTENSA_PROP_NO_TRANSFORM
10627 | XTENSA_PROP_INSN_NO_REORDER
);
10629 if (xtensa_is_littable_section (sec
))
10630 return (XTENSA_PROP_LITERAL
10631 | XTENSA_PROP_NO_TRANSFORM
10632 | XTENSA_PROP_INSN_NO_REORDER
);
10638 /* Other functions called directly by the linker. */
10641 xtensa_callback_required_dependence (bfd
*abfd
,
10643 struct bfd_link_info
*link_info
,
10644 deps_callback_t callback
,
10647 Elf_Internal_Rela
*internal_relocs
;
10648 bfd_byte
*contents
;
10650 bfd_boolean ok
= TRUE
;
10651 bfd_size_type sec_size
;
10653 sec_size
= bfd_get_section_limit (abfd
, sec
);
10655 /* ".plt*" sections have no explicit relocations but they contain L32R
10656 instructions that reference the corresponding ".got.plt*" sections. */
10657 if ((sec
->flags
& SEC_LINKER_CREATED
) != 0
10658 && CONST_STRNEQ (sec
->name
, ".plt"))
10662 /* Find the corresponding ".got.plt*" section. */
10663 if (sec
->name
[4] == '\0')
10664 sgotplt
= bfd_get_section_by_name (sec
->owner
, ".got.plt");
10670 BFD_ASSERT (sec
->name
[4] == '.');
10671 chunk
= strtol (&sec
->name
[5], NULL
, 10);
10673 sprintf (got_name
, ".got.plt.%u", chunk
);
10674 sgotplt
= bfd_get_section_by_name (sec
->owner
, got_name
);
10676 BFD_ASSERT (sgotplt
);
10678 /* Assume worst-case offsets: L32R at the very end of the ".plt"
10679 section referencing a literal at the very beginning of
10680 ".got.plt". This is very close to the real dependence, anyway. */
10681 (*callback
) (sec
, sec_size
, sgotplt
, 0, closure
);
10684 /* Only ELF files are supported for Xtensa. Check here to avoid a segfault
10685 when building uclibc, which runs "ld -b binary /dev/null". */
10686 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
10689 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
10690 link_info
->keep_memory
);
10691 if (internal_relocs
== NULL
10692 || sec
->reloc_count
== 0)
10695 /* Cache the contents for the duration of this scan. */
10696 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
10697 if (contents
== NULL
&& sec_size
!= 0)
10703 if (!xtensa_default_isa
)
10704 xtensa_default_isa
= xtensa_isa_init (0, 0);
10706 for (i
= 0; i
< sec
->reloc_count
; i
++)
10708 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
10709 if (is_l32r_relocation (abfd
, sec
, contents
, irel
))
10712 asection
*target_sec
;
10713 bfd_vma target_offset
;
10715 r_reloc_init (&l32r_rel
, abfd
, irel
, contents
, sec_size
);
10718 /* L32Rs must be local to the input file. */
10719 if (r_reloc_is_defined (&l32r_rel
))
10721 target_sec
= r_reloc_get_section (&l32r_rel
);
10722 target_offset
= l32r_rel
.target_offset
;
10724 (*callback
) (sec
, irel
->r_offset
, target_sec
, target_offset
,
10730 release_internal_relocs (sec
, internal_relocs
);
10731 release_contents (sec
, contents
);
10735 /* The default literal sections should always be marked as "code" (i.e.,
10736 SHF_EXECINSTR). This is particularly important for the Linux kernel
10737 module loader so that the literals are not placed after the text. */
10738 static const struct bfd_elf_special_section elf_xtensa_special_sections
[] =
10740 { STRING_COMMA_LEN (".fini.literal"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
10741 { STRING_COMMA_LEN (".init.literal"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
10742 { STRING_COMMA_LEN (".literal"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
10743 { STRING_COMMA_LEN (".xtensa.info"), 0, SHT_NOTE
, 0 },
10744 { NULL
, 0, 0, 0, 0 }
10748 #define TARGET_LITTLE_SYM bfd_elf32_xtensa_le_vec
10749 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
10750 #define TARGET_BIG_SYM bfd_elf32_xtensa_be_vec
10751 #define TARGET_BIG_NAME "elf32-xtensa-be"
10752 #define ELF_ARCH bfd_arch_xtensa
10754 #define ELF_MACHINE_CODE EM_XTENSA
10755 #define ELF_MACHINE_ALT1 EM_XTENSA_OLD
10758 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
10759 #else /* !XCHAL_HAVE_MMU */
10760 #define ELF_MAXPAGESIZE 1
10761 #endif /* !XCHAL_HAVE_MMU */
10762 #endif /* ELF_ARCH */
10764 #define elf_backend_can_gc_sections 1
10765 #define elf_backend_can_refcount 1
10766 #define elf_backend_plt_readonly 1
10767 #define elf_backend_got_header_size 4
10768 #define elf_backend_want_dynbss 0
10769 #define elf_backend_want_got_plt 1
10771 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
10773 #define bfd_elf32_mkobject elf_xtensa_mkobject
10775 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
10776 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
10777 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
10778 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
10779 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
10780 #define bfd_elf32_bfd_reloc_name_lookup \
10781 elf_xtensa_reloc_name_lookup
10782 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
10783 #define bfd_elf32_bfd_link_hash_table_create elf_xtensa_link_hash_table_create
10785 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
10786 #define elf_backend_check_relocs elf_xtensa_check_relocs
10787 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
10788 #define elf_backend_discard_info elf_xtensa_discard_info
10789 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
10790 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
10791 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
10792 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
10793 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
10794 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
10795 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
10796 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
10797 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
10798 #define elf_backend_object_p elf_xtensa_object_p
10799 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
10800 #define elf_backend_relocate_section elf_xtensa_relocate_section
10801 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
10802 #define elf_backend_always_size_sections elf_xtensa_always_size_sections
10803 #define elf_backend_omit_section_dynsym \
10804 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
10805 #define elf_backend_special_sections elf_xtensa_special_sections
10806 #define elf_backend_action_discarded elf_xtensa_action_discarded
10807 #define elf_backend_copy_indirect_symbol elf_xtensa_copy_indirect_symbol
10809 #include "elf32-target.h"