1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright 2003, 2004 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #ifdef ANSI_PROTOTYPES
34 #include "elf/xtensa.h"
35 #include "xtensa-isa.h"
36 #include "xtensa-config.h"
38 #define XTENSA_NO_NOP_REMOVAL 0
40 /* Main interface functions. */
41 static void elf_xtensa_info_to_howto_rela
42 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
43 static reloc_howto_type
*elf_xtensa_reloc_type_lookup
44 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
45 extern int xtensa_read_table_entries
46 PARAMS ((bfd
*, asection
*, property_table_entry
**, const char *,
48 static bfd_boolean elf_xtensa_check_relocs
49 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
50 const Elf_Internal_Rela
*));
51 static void elf_xtensa_hide_symbol
52 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*, bfd_boolean
));
53 static asection
*elf_xtensa_gc_mark_hook
54 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
55 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
56 static bfd_boolean elf_xtensa_gc_sweep_hook
57 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
58 const Elf_Internal_Rela
*));
59 static bfd_boolean elf_xtensa_create_dynamic_sections
60 PARAMS ((bfd
*, struct bfd_link_info
*));
61 static bfd_boolean elf_xtensa_adjust_dynamic_symbol
62 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
63 static bfd_boolean elf_xtensa_size_dynamic_sections
64 PARAMS ((bfd
*, struct bfd_link_info
*));
65 static bfd_boolean elf_xtensa_modify_segment_map
66 PARAMS ((bfd
*, struct bfd_link_info
*));
67 static bfd_boolean elf_xtensa_relocate_section
68 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
69 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
70 static bfd_boolean elf_xtensa_relax_section
71 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*again
));
72 static bfd_boolean elf_xtensa_finish_dynamic_symbol
73 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
75 static bfd_boolean elf_xtensa_finish_dynamic_sections
76 PARAMS ((bfd
*, struct bfd_link_info
*));
77 static bfd_boolean elf_xtensa_merge_private_bfd_data
78 PARAMS ((bfd
*, bfd
*));
79 static bfd_boolean elf_xtensa_set_private_flags
80 PARAMS ((bfd
*, flagword
));
81 extern flagword elf_xtensa_get_private_bfd_flags
83 static bfd_boolean elf_xtensa_print_private_bfd_data
84 PARAMS ((bfd
*, PTR
));
85 static bfd_boolean elf_xtensa_object_p
87 static void elf_xtensa_final_write_processing
88 PARAMS ((bfd
*, bfd_boolean
));
89 static enum elf_reloc_type_class elf_xtensa_reloc_type_class
90 PARAMS ((const Elf_Internal_Rela
*));
91 static bfd_boolean elf_xtensa_discard_info
92 PARAMS ((bfd
*, struct elf_reloc_cookie
*, struct bfd_link_info
*));
93 static bfd_boolean elf_xtensa_ignore_discarded_relocs
94 PARAMS ((asection
*));
95 static bfd_boolean elf_xtensa_grok_prstatus
96 PARAMS ((bfd
*, Elf_Internal_Note
*));
97 static bfd_boolean elf_xtensa_grok_psinfo
98 PARAMS ((bfd
*, Elf_Internal_Note
*));
99 static bfd_boolean elf_xtensa_new_section_hook
100 PARAMS ((bfd
*, asection
*));
103 /* Local helper functions. */
105 static bfd_boolean xtensa_elf_dynamic_symbol_p
106 PARAMS ((struct elf_link_hash_entry
*, struct bfd_link_info
*));
107 static int property_table_compare
108 PARAMS ((const PTR
, const PTR
));
109 static int property_table_matches
110 PARAMS ((const PTR
, const PTR
));
111 static property_table_entry
*elf_xtensa_find_property_entry
112 PARAMS ((property_table_entry
*, int, bfd_vma
));
113 static bfd_boolean elf_xtensa_in_literal_pool
114 PARAMS ((property_table_entry
*, int, bfd_vma
));
115 static void elf_xtensa_make_sym_local
116 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
117 static bfd_boolean add_extra_plt_sections
118 PARAMS ((bfd
*, int));
119 static bfd_boolean elf_xtensa_fix_refcounts
120 PARAMS ((struct elf_link_hash_entry
*, PTR
));
121 static bfd_boolean elf_xtensa_allocate_plt_size
122 PARAMS ((struct elf_link_hash_entry
*, PTR
));
123 static bfd_boolean elf_xtensa_allocate_got_size
124 PARAMS ((struct elf_link_hash_entry
*, PTR
));
125 static void elf_xtensa_allocate_local_got_size
126 PARAMS ((struct bfd_link_info
*, asection
*));
127 static bfd_reloc_status_type elf_xtensa_do_reloc
128 PARAMS ((reloc_howto_type
*, bfd
*, asection
*, bfd_vma
, bfd_byte
*,
129 bfd_vma
, bfd_boolean
, char **));
130 static char * vsprint_msg
131 VPARAMS ((const char *, const char *, int, ...));
132 static char *build_encoding_error_message
133 PARAMS ((xtensa_opcode
, bfd_vma
));
134 static bfd_reloc_status_type bfd_elf_xtensa_reloc
135 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
136 static bfd_boolean do_fix_for_relocatable_link
137 PARAMS ((Elf_Internal_Rela
*, bfd
*, asection
*, bfd_byte
*));
138 static void do_fix_for_final_link
139 PARAMS ((Elf_Internal_Rela
*, bfd
*, asection
*, bfd_byte
*, bfd_vma
*));
140 static bfd_vma elf_xtensa_create_plt_entry
141 PARAMS ((bfd
*, bfd
*, unsigned));
142 static int elf_xtensa_combine_prop_entries
143 PARAMS ((bfd
*, asection
*, asection
*));
144 static bfd_boolean elf_xtensa_discard_info_for_section
145 PARAMS ((bfd
*, struct elf_reloc_cookie
*, struct bfd_link_info
*,
148 /* Local functions to handle Xtensa configurability. */
150 static void init_call_opcodes
152 static bfd_boolean is_indirect_call_opcode
153 PARAMS ((xtensa_opcode
));
154 static bfd_boolean is_direct_call_opcode
155 PARAMS ((xtensa_opcode
));
156 static bfd_boolean is_windowed_call_opcode
157 PARAMS ((xtensa_opcode
));
158 static xtensa_opcode get_const16_opcode
160 static xtensa_opcode get_l32r_opcode
162 static bfd_vma l32r_offset
163 PARAMS ((bfd_vma
, bfd_vma
));
164 static int get_relocation_opnd
165 PARAMS ((xtensa_opcode
, int));
166 static int get_relocation_slot
168 static xtensa_opcode get_relocation_opcode
169 PARAMS ((bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*));
170 static bfd_boolean is_l32r_relocation
171 PARAMS ((bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*));
172 static bfd_boolean is_alt_relocation
174 static bfd_boolean is_operand_relocation
176 static bfd_size_type insn_decode_len
177 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_size_type
));
178 static xtensa_opcode insn_decode_opcode
179 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_size_type
, int));
180 static bfd_boolean check_branch_target_aligned
181 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_vma
, bfd_vma
));
182 static bfd_boolean check_loop_aligned
183 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_vma
, bfd_vma
));
184 static bfd_boolean check_branch_target_aligned_address
185 PARAMS ((bfd_vma
, int));
186 static bfd_size_type get_asm_simplify_size
187 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_size_type
));
189 /* Functions for link-time code simplifications. */
191 static bfd_reloc_status_type elf_xtensa_do_asm_simplify
192 PARAMS ((bfd_byte
*, bfd_vma
, bfd_vma
, char **));
193 static bfd_reloc_status_type contract_asm_expansion
194 PARAMS ((bfd_byte
*, bfd_vma
, Elf_Internal_Rela
*, char **));
195 static xtensa_opcode swap_callx_for_call_opcode
196 PARAMS ((xtensa_opcode
));
197 static xtensa_opcode get_expanded_call_opcode
198 PARAMS ((bfd_byte
*, int, bfd_boolean
*));
200 /* Access to internal relocations, section contents and symbols. */
202 static Elf_Internal_Rela
*retrieve_internal_relocs
203 PARAMS ((bfd
*, asection
*, bfd_boolean
));
204 static void pin_internal_relocs
205 PARAMS ((asection
*, Elf_Internal_Rela
*));
206 static void release_internal_relocs
207 PARAMS ((asection
*, Elf_Internal_Rela
*));
208 static bfd_byte
*retrieve_contents
209 PARAMS ((bfd
*, asection
*, bfd_boolean
));
210 static void pin_contents
211 PARAMS ((asection
*, bfd_byte
*));
212 static void release_contents
213 PARAMS ((asection
*, bfd_byte
*));
214 static Elf_Internal_Sym
*retrieve_local_syms
217 /* Miscellaneous utility functions. */
219 static asection
*elf_xtensa_get_plt_section
220 PARAMS ((bfd
*, int));
221 static asection
*elf_xtensa_get_gotplt_section
222 PARAMS ((bfd
*, int));
223 static asection
*get_elf_r_symndx_section
224 PARAMS ((bfd
*, unsigned long));
225 static struct elf_link_hash_entry
*get_elf_r_symndx_hash_entry
226 PARAMS ((bfd
*, unsigned long));
227 static bfd_vma get_elf_r_symndx_offset
228 PARAMS ((bfd
*, unsigned long));
229 static bfd_boolean is_reloc_sym_weak
230 PARAMS ((bfd
*, Elf_Internal_Rela
*));
231 static bfd_boolean pcrel_reloc_fits
232 PARAMS ((xtensa_opcode
, int, bfd_vma
, bfd_vma
));
233 static bfd_boolean xtensa_is_property_section
234 PARAMS ((asection
*));
235 static bfd_boolean xtensa_is_littable_section
236 PARAMS ((asection
*));
237 static int internal_reloc_compare
238 PARAMS ((const PTR
, const PTR
));
239 static int internal_reloc_matches
240 PARAMS ((const PTR
, const PTR
));
241 extern char *xtensa_get_property_section_name
242 PARAMS ((asection
*, const char *));
243 static flagword xtensa_get_property_predef_flags
244 PARAMS ((asection
*));
246 /* Other functions called directly by the linker. */
248 typedef void (*deps_callback_t
)
249 PARAMS ((asection
*, bfd_vma
, asection
*, bfd_vma
, PTR
));
250 extern bfd_boolean xtensa_callback_required_dependence
251 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*,
252 deps_callback_t
, PTR
));
255 /* Globally visible flag for choosing size optimization of NOP removal
256 instead of branch-target-aware minimization for NOP removal.
257 When nonzero, narrow all instructions and remove all NOPs possible
258 around longcall expansions. */
259 int elf32xtensa_size_opt
;
262 /* The "new_section_hook" is used to set up a per-section
263 "xtensa_relax_info" data structure with additional information used
264 during relaxation. */
265 typedef struct xtensa_relax_info_struct xtensa_relax_info
;
269 /* Total count of PLT relocations seen during check_relocs.
270 The actual PLT code must be split into multiple sections and all
271 the sections have to be created before size_dynamic_sections,
272 where we figure out the exact number of PLT entries that will be
273 needed. It is OK if this count is an overestimate, e.g., some
274 relocations may be removed by GC. */
276 static int plt_reloc_count
= 0;
279 /* The GNU tools do not easily allow extending interfaces to pass around
280 the pointer to the Xtensa ISA information, so instead we add a global
281 variable here (in BFD) that can be used by any of the tools that need
284 xtensa_isa xtensa_default_isa
;
287 /* When this is true, relocations may have been modified to refer to
288 symbols from other input files. The per-section list of "fix"
289 records needs to be checked when resolving relocations. */
291 static bfd_boolean relaxing_section
= FALSE
;
293 /* When this is true, during final links, literals that cannot be
294 coalesced and their relocations may be moved to other sections. */
296 int elf32xtensa_no_literal_movement
= 1;
299 static reloc_howto_type elf_howto_table
[] =
301 HOWTO (R_XTENSA_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
302 bfd_elf_xtensa_reloc
, "R_XTENSA_NONE",
303 FALSE
, 0x00000000, 0x00000000, FALSE
),
304 HOWTO (R_XTENSA_32
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
305 bfd_elf_xtensa_reloc
, "R_XTENSA_32",
306 TRUE
, 0xffffffff, 0xffffffff, FALSE
),
307 /* Replace a 32-bit value with a value from the runtime linker (only
308 used by linker-generated stub functions). The r_addend value is
309 special: 1 means to substitute a pointer to the runtime linker's
310 dynamic resolver function; 2 means to substitute the link map for
311 the shared object. */
312 HOWTO (R_XTENSA_RTLD
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
313 NULL
, "R_XTENSA_RTLD",
314 FALSE
, 0x00000000, 0x00000000, FALSE
),
315 HOWTO (R_XTENSA_GLOB_DAT
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
316 bfd_elf_generic_reloc
, "R_XTENSA_GLOB_DAT",
317 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
318 HOWTO (R_XTENSA_JMP_SLOT
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
319 bfd_elf_generic_reloc
, "R_XTENSA_JMP_SLOT",
320 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
321 HOWTO (R_XTENSA_RELATIVE
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
322 bfd_elf_generic_reloc
, "R_XTENSA_RELATIVE",
323 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
324 HOWTO (R_XTENSA_PLT
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
325 bfd_elf_xtensa_reloc
, "R_XTENSA_PLT",
326 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
328 HOWTO (R_XTENSA_OP0
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
329 bfd_elf_xtensa_reloc
, "R_XTENSA_OP0",
330 FALSE
, 0x00000000, 0x00000000, TRUE
),
331 HOWTO (R_XTENSA_OP1
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
332 bfd_elf_xtensa_reloc
, "R_XTENSA_OP1",
333 FALSE
, 0x00000000, 0x00000000, TRUE
),
334 HOWTO (R_XTENSA_OP2
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
335 bfd_elf_xtensa_reloc
, "R_XTENSA_OP2",
336 FALSE
, 0x00000000, 0x00000000, TRUE
),
337 /* Assembly auto-expansion. */
338 HOWTO (R_XTENSA_ASM_EXPAND
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
339 bfd_elf_xtensa_reloc
, "R_XTENSA_ASM_EXPAND",
340 FALSE
, 0x00000000, 0x00000000, FALSE
),
341 /* Relax assembly auto-expansion. */
342 HOWTO (R_XTENSA_ASM_SIMPLIFY
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
343 bfd_elf_xtensa_reloc
, "R_XTENSA_ASM_SIMPLIFY",
344 FALSE
, 0x00000000, 0x00000000, TRUE
),
347 /* GNU extension to record C++ vtable hierarchy. */
348 HOWTO (R_XTENSA_GNU_VTINHERIT
, 0, 2, 0, FALSE
, 0, complain_overflow_dont
,
349 NULL
, "R_XTENSA_GNU_VTINHERIT",
350 FALSE
, 0x00000000, 0x00000000, FALSE
),
351 /* GNU extension to record C++ vtable member usage. */
352 HOWTO (R_XTENSA_GNU_VTENTRY
, 0, 2, 0, FALSE
, 0, complain_overflow_dont
,
353 _bfd_elf_rel_vtable_reloc_fn
, "R_XTENSA_GNU_VTENTRY",
354 FALSE
, 0x00000000, 0x00000000, FALSE
),
356 /* Relocations for supporting difference of symbols. */
357 HOWTO (R_XTENSA_DIFF8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
358 bfd_elf_xtensa_reloc
, "R_XTENSA_DIFF8",
359 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
360 HOWTO (R_XTENSA_DIFF16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
361 bfd_elf_xtensa_reloc
, "R_XTENSA_DIFF16",
362 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
363 HOWTO (R_XTENSA_DIFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
364 bfd_elf_xtensa_reloc
, "R_XTENSA_DIFF32",
365 FALSE
, 0xffffffff, 0xffffffff, FALSE
),
367 /* General immediate operand relocations. */
368 HOWTO (R_XTENSA_SLOT0_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
369 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT0_OP",
370 FALSE
, 0x00000000, 0x00000000, TRUE
),
371 HOWTO (R_XTENSA_SLOT1_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
372 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT1_OP",
373 FALSE
, 0x00000000, 0x00000000, TRUE
),
374 HOWTO (R_XTENSA_SLOT2_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
375 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT2_OP",
376 FALSE
, 0x00000000, 0x00000000, TRUE
),
377 HOWTO (R_XTENSA_SLOT3_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
378 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT3_OP",
379 FALSE
, 0x00000000, 0x00000000, TRUE
),
380 HOWTO (R_XTENSA_SLOT4_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
381 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT4_OP",
382 FALSE
, 0x00000000, 0x00000000, TRUE
),
383 HOWTO (R_XTENSA_SLOT5_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
384 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT5_OP",
385 FALSE
, 0x00000000, 0x00000000, TRUE
),
386 HOWTO (R_XTENSA_SLOT6_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
387 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT6_OP",
388 FALSE
, 0x00000000, 0x00000000, TRUE
),
389 HOWTO (R_XTENSA_SLOT7_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
390 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT7_OP",
391 FALSE
, 0x00000000, 0x00000000, TRUE
),
392 HOWTO (R_XTENSA_SLOT8_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
393 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT8_OP",
394 FALSE
, 0x00000000, 0x00000000, TRUE
),
395 HOWTO (R_XTENSA_SLOT9_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
396 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT9_OP",
397 FALSE
, 0x00000000, 0x00000000, TRUE
),
398 HOWTO (R_XTENSA_SLOT10_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
399 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT10_OP",
400 FALSE
, 0x00000000, 0x00000000, TRUE
),
401 HOWTO (R_XTENSA_SLOT11_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
402 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT11_OP",
403 FALSE
, 0x00000000, 0x00000000, TRUE
),
404 HOWTO (R_XTENSA_SLOT12_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
405 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT12_OP",
406 FALSE
, 0x00000000, 0x00000000, TRUE
),
407 HOWTO (R_XTENSA_SLOT13_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
408 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT13_OP",
409 FALSE
, 0x00000000, 0x00000000, TRUE
),
410 HOWTO (R_XTENSA_SLOT14_OP
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
411 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT14_OP",
412 FALSE
, 0x00000000, 0x00000000, TRUE
),
414 /* "Alternate" relocations. The meaning of these is opcode-specific. */
415 HOWTO (R_XTENSA_SLOT0_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
416 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT0_ALT",
417 FALSE
, 0x00000000, 0x00000000, TRUE
),
418 HOWTO (R_XTENSA_SLOT1_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
419 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT1_ALT",
420 FALSE
, 0x00000000, 0x00000000, TRUE
),
421 HOWTO (R_XTENSA_SLOT2_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
422 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT2_ALT",
423 FALSE
, 0x00000000, 0x00000000, TRUE
),
424 HOWTO (R_XTENSA_SLOT3_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
425 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT3_ALT",
426 FALSE
, 0x00000000, 0x00000000, TRUE
),
427 HOWTO (R_XTENSA_SLOT4_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
428 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT4_ALT",
429 FALSE
, 0x00000000, 0x00000000, TRUE
),
430 HOWTO (R_XTENSA_SLOT5_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
431 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT5_ALT",
432 FALSE
, 0x00000000, 0x00000000, TRUE
),
433 HOWTO (R_XTENSA_SLOT6_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
434 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT6_ALT",
435 FALSE
, 0x00000000, 0x00000000, TRUE
),
436 HOWTO (R_XTENSA_SLOT7_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
437 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT7_ALT",
438 FALSE
, 0x00000000, 0x00000000, TRUE
),
439 HOWTO (R_XTENSA_SLOT8_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
440 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT8_ALT",
441 FALSE
, 0x00000000, 0x00000000, TRUE
),
442 HOWTO (R_XTENSA_SLOT9_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
443 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT9_ALT",
444 FALSE
, 0x00000000, 0x00000000, TRUE
),
445 HOWTO (R_XTENSA_SLOT10_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
446 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT10_ALT",
447 FALSE
, 0x00000000, 0x00000000, TRUE
),
448 HOWTO (R_XTENSA_SLOT11_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
449 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT11_ALT",
450 FALSE
, 0x00000000, 0x00000000, TRUE
),
451 HOWTO (R_XTENSA_SLOT12_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
452 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT12_ALT",
453 FALSE
, 0x00000000, 0x00000000, TRUE
),
454 HOWTO (R_XTENSA_SLOT13_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
455 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT13_ALT",
456 FALSE
, 0x00000000, 0x00000000, TRUE
),
457 HOWTO (R_XTENSA_SLOT14_ALT
, 0, 0, 0, TRUE
, 0, complain_overflow_dont
,
458 bfd_elf_xtensa_reloc
, "R_XTENSA_SLOT14_ALT",
459 FALSE
, 0x00000000, 0x00000000, TRUE
)
464 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
469 static reloc_howto_type
*
470 elf_xtensa_reloc_type_lookup (abfd
, code
)
471 bfd
*abfd ATTRIBUTE_UNUSED
;
472 bfd_reloc_code_real_type code
;
477 TRACE ("BFD_RELOC_NONE");
478 return &elf_howto_table
[(unsigned) R_XTENSA_NONE
];
481 TRACE ("BFD_RELOC_32");
482 return &elf_howto_table
[(unsigned) R_XTENSA_32
];
484 case BFD_RELOC_XTENSA_DIFF8
:
485 TRACE ("BFD_RELOC_XTENSA_DIFF8");
486 return &elf_howto_table
[(unsigned) R_XTENSA_DIFF8
];
488 case BFD_RELOC_XTENSA_DIFF16
:
489 TRACE ("BFD_RELOC_XTENSA_DIFF16");
490 return &elf_howto_table
[(unsigned) R_XTENSA_DIFF16
];
492 case BFD_RELOC_XTENSA_DIFF32
:
493 TRACE ("BFD_RELOC_XTENSA_DIFF32");
494 return &elf_howto_table
[(unsigned) R_XTENSA_DIFF32
];
496 case BFD_RELOC_XTENSA_RTLD
:
497 TRACE ("BFD_RELOC_XTENSA_RTLD");
498 return &elf_howto_table
[(unsigned) R_XTENSA_RTLD
];
500 case BFD_RELOC_XTENSA_GLOB_DAT
:
501 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
502 return &elf_howto_table
[(unsigned) R_XTENSA_GLOB_DAT
];
504 case BFD_RELOC_XTENSA_JMP_SLOT
:
505 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
506 return &elf_howto_table
[(unsigned) R_XTENSA_JMP_SLOT
];
508 case BFD_RELOC_XTENSA_RELATIVE
:
509 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
510 return &elf_howto_table
[(unsigned) R_XTENSA_RELATIVE
];
512 case BFD_RELOC_XTENSA_PLT
:
513 TRACE ("BFD_RELOC_XTENSA_PLT");
514 return &elf_howto_table
[(unsigned) R_XTENSA_PLT
];
516 case BFD_RELOC_XTENSA_OP0
:
517 TRACE ("BFD_RELOC_XTENSA_OP0");
518 return &elf_howto_table
[(unsigned) R_XTENSA_OP0
];
520 case BFD_RELOC_XTENSA_OP1
:
521 TRACE ("BFD_RELOC_XTENSA_OP1");
522 return &elf_howto_table
[(unsigned) R_XTENSA_OP1
];
524 case BFD_RELOC_XTENSA_OP2
:
525 TRACE ("BFD_RELOC_XTENSA_OP2");
526 return &elf_howto_table
[(unsigned) R_XTENSA_OP2
];
528 case BFD_RELOC_XTENSA_ASM_EXPAND
:
529 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
530 return &elf_howto_table
[(unsigned) R_XTENSA_ASM_EXPAND
];
532 case BFD_RELOC_XTENSA_ASM_SIMPLIFY
:
533 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
534 return &elf_howto_table
[(unsigned) R_XTENSA_ASM_SIMPLIFY
];
536 case BFD_RELOC_VTABLE_INHERIT
:
537 TRACE ("BFD_RELOC_VTABLE_INHERIT");
538 return &elf_howto_table
[(unsigned) R_XTENSA_GNU_VTINHERIT
];
540 case BFD_RELOC_VTABLE_ENTRY
:
541 TRACE ("BFD_RELOC_VTABLE_ENTRY");
542 return &elf_howto_table
[(unsigned) R_XTENSA_GNU_VTENTRY
];
545 if (code
>= BFD_RELOC_XTENSA_SLOT0_OP
546 && code
<= BFD_RELOC_XTENSA_SLOT14_OP
)
548 unsigned n
= (R_XTENSA_SLOT0_OP
+
549 (code
- BFD_RELOC_XTENSA_SLOT0_OP
));
550 return &elf_howto_table
[n
];
553 if (code
>= BFD_RELOC_XTENSA_SLOT0_ALT
554 && code
<= BFD_RELOC_XTENSA_SLOT14_ALT
)
556 unsigned n
= (R_XTENSA_SLOT0_ALT
+
557 (code
- BFD_RELOC_XTENSA_SLOT0_ALT
));
558 return &elf_howto_table
[n
];
569 /* Given an ELF "rela" relocation, find the corresponding howto and record
570 it in the BFD internal arelent representation of the relocation. */
573 elf_xtensa_info_to_howto_rela (abfd
, cache_ptr
, dst
)
574 bfd
*abfd ATTRIBUTE_UNUSED
;
576 Elf_Internal_Rela
*dst
;
578 unsigned int r_type
= ELF32_R_TYPE (dst
->r_info
);
580 BFD_ASSERT (r_type
< (unsigned int) R_XTENSA_max
);
581 cache_ptr
->howto
= &elf_howto_table
[r_type
];
585 /* Functions for the Xtensa ELF linker. */
587 /* The name of the dynamic interpreter. This is put in the .interp
590 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
592 /* The size in bytes of an entry in the procedure linkage table.
593 (This does _not_ include the space for the literals associated with
596 #define PLT_ENTRY_SIZE 16
598 /* For _really_ large PLTs, we may need to alternate between literals
599 and code to keep the literals within the 256K range of the L32R
600 instructions in the code. It's unlikely that anyone would ever need
601 such a big PLT, but an arbitrary limit on the PLT size would be bad.
602 Thus, we split the PLT into chunks. Since there's very little
603 overhead (2 extra literals) for each chunk, the chunk size is kept
604 small so that the code for handling multiple chunks get used and
605 tested regularly. With 254 entries, there are 1K of literals for
606 each chunk, and that seems like a nice round number. */
608 #define PLT_ENTRIES_PER_CHUNK 254
610 /* PLT entries are actually used as stub functions for lazy symbol
611 resolution. Once the symbol is resolved, the stub function is never
612 invoked. Note: the 32-byte frame size used here cannot be changed
613 without a corresponding change in the runtime linker. */
615 static const bfd_byte elf_xtensa_be_plt_entry
[PLT_ENTRY_SIZE
] =
617 0x6c, 0x10, 0x04, /* entry sp, 32 */
618 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
619 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
620 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
621 0x0a, 0x80, 0x00, /* jx a8 */
625 static const bfd_byte elf_xtensa_le_plt_entry
[PLT_ENTRY_SIZE
] =
627 0x36, 0x41, 0x00, /* entry sp, 32 */
628 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
629 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
630 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
631 0xa0, 0x08, 0x00, /* jx a8 */
636 static inline bfd_boolean
637 xtensa_elf_dynamic_symbol_p (h
, info
)
638 struct elf_link_hash_entry
*h
;
639 struct bfd_link_info
*info
;
641 /* Check if we should do dynamic things to this symbol. The
642 "ignore_protected" argument need not be set, because Xtensa code
643 does not require special handling of STV_PROTECTED to make function
644 pointer comparisons work properly. The PLT addresses are never
645 used for function pointers. */
647 return _bfd_elf_dynamic_symbol_p (h
, info
, 0);
652 property_table_compare (ap
, bp
)
656 const property_table_entry
*a
= (const property_table_entry
*) ap
;
657 const property_table_entry
*b
= (const property_table_entry
*) bp
;
659 if (a
->address
== b
->address
)
661 /* The only circumstance where two entries may legitimately have the
662 same address is when one of them is a zero-size placeholder to
663 mark a place where fill can be inserted. The zero-size entry should
665 BFD_ASSERT ((a
->size
== 0 || b
->size
== 0));
667 if (a
->size
!= b
->size
)
668 return (a
->size
- b
->size
);
670 if ((a
->flags
& XTENSA_PROP_ALIGN
) != (b
->flags
& XTENSA_PROP_ALIGN
))
671 return ((b
->flags
& XTENSA_PROP_ALIGN
)
672 - (a
->flags
& XTENSA_PROP_ALIGN
));
674 if ((a
->flags
& XTENSA_PROP_ALIGN
)
675 && (GET_XTENSA_PROP_ALIGNMENT (a
->flags
)
676 != GET_XTENSA_PROP_ALIGNMENT (b
->flags
)))
677 return (GET_XTENSA_PROP_ALIGNMENT (a
->flags
)
678 - GET_XTENSA_PROP_ALIGNMENT (b
->flags
));
680 if ((a
->flags
& XTENSA_PROP_UNREACHABLE
)
681 != (b
->flags
& XTENSA_PROP_UNREACHABLE
))
682 return ((b
->flags
& XTENSA_PROP_UNREACHABLE
)
683 - (a
->flags
& XTENSA_PROP_UNREACHABLE
));
685 return (a
->flags
- b
->flags
);
688 return (a
->address
- b
->address
);
693 property_table_matches (ap
, bp
)
697 const property_table_entry
*a
= (const property_table_entry
*) ap
;
698 const property_table_entry
*b
= (const property_table_entry
*) bp
;
700 /* Check if one entry overlaps with the other. */
701 if ((b
->address
>= a
->address
&& b
->address
< (a
->address
+ a
->size
))
702 || (a
->address
>= b
->address
&& a
->address
< (b
->address
+ b
->size
)))
705 return (a
->address
- b
->address
);
709 /* Get the literal table or property table entries for the given
710 section. Sets TABLE_P and returns the number of entries. On
711 error, returns a negative value. */
714 xtensa_read_table_entries (abfd
, section
, table_p
, sec_name
, output_addr
)
717 property_table_entry
**table_p
;
718 const char *sec_name
;
719 bfd_boolean output_addr
;
721 asection
*table_section
;
722 char *table_section_name
;
723 bfd_size_type table_size
= 0;
724 bfd_byte
*table_data
;
725 property_table_entry
*blocks
;
727 bfd_size_type num_records
;
728 Elf_Internal_Rela
*internal_relocs
;
729 bfd_vma section_addr
;
730 flagword predef_flags
;
731 bfd_size_type table_entry_size
;
734 || !(section
->flags
& SEC_ALLOC
)
735 || (section
->flags
& SEC_DEBUGGING
))
741 table_section_name
= xtensa_get_property_section_name (section
, sec_name
);
742 table_section
= bfd_get_section_by_name (abfd
, table_section_name
);
743 free (table_section_name
);
745 table_size
= table_section
->size
;
753 predef_flags
= xtensa_get_property_predef_flags (table_section
);
754 table_entry_size
= 12;
756 table_entry_size
-= 4;
758 num_records
= table_size
/ table_entry_size
;
759 table_data
= retrieve_contents (abfd
, table_section
, TRUE
);
760 blocks
= (property_table_entry
*)
761 bfd_malloc (num_records
* sizeof (property_table_entry
));
765 section_addr
= section
->output_section
->vma
+ section
->output_offset
;
767 section_addr
= section
->vma
;
769 /* If the file has not yet been relocated, process the relocations
770 and sort out the table entries that apply to the specified section. */
771 internal_relocs
= retrieve_internal_relocs (abfd
, table_section
, TRUE
);
772 if (internal_relocs
&& !table_section
->reloc_done
)
776 for (i
= 0; i
< table_section
->reloc_count
; i
++)
778 Elf_Internal_Rela
*rel
= &internal_relocs
[i
];
779 unsigned long r_symndx
;
781 if (ELF32_R_TYPE (rel
->r_info
) == R_XTENSA_NONE
)
784 BFD_ASSERT (ELF32_R_TYPE (rel
->r_info
) == R_XTENSA_32
);
785 r_symndx
= ELF32_R_SYM (rel
->r_info
);
787 if (get_elf_r_symndx_section (abfd
, r_symndx
) == section
)
789 bfd_vma sym_off
= get_elf_r_symndx_offset (abfd
, r_symndx
);
790 BFD_ASSERT (sym_off
== 0);
791 BFD_ASSERT (rel
->r_addend
== 0);
792 blocks
[block_count
].address
=
793 (section_addr
+ sym_off
+ rel
->r_addend
794 + bfd_get_32 (abfd
, table_data
+ rel
->r_offset
));
795 blocks
[block_count
].size
=
796 bfd_get_32 (abfd
, table_data
+ rel
->r_offset
+ 4);
798 blocks
[block_count
].flags
= predef_flags
;
800 blocks
[block_count
].flags
=
801 bfd_get_32 (abfd
, table_data
+ rel
->r_offset
+ 8);
808 /* The file has already been relocated and the addresses are
809 already in the table. */
811 bfd_size_type section_limit
= bfd_get_section_limit (abfd
, section
);
813 for (off
= 0; off
< table_size
; off
+= table_entry_size
)
815 bfd_vma address
= bfd_get_32 (abfd
, table_data
+ off
);
817 if (address
>= section_addr
818 && address
< section_addr
+ section_limit
)
820 blocks
[block_count
].address
= address
;
821 blocks
[block_count
].size
=
822 bfd_get_32 (abfd
, table_data
+ off
+ 4);
824 blocks
[block_count
].flags
= predef_flags
;
826 blocks
[block_count
].flags
=
827 bfd_get_32 (abfd
, table_data
+ off
+ 8);
833 release_contents (table_section
, table_data
);
834 release_internal_relocs (table_section
, internal_relocs
);
838 /* Now sort them into address order for easy reference. */
839 qsort (blocks
, block_count
, sizeof (property_table_entry
),
840 property_table_compare
);
848 property_table_entry
*
849 elf_xtensa_find_property_entry (property_table
, property_table_size
, addr
)
850 property_table_entry
*property_table
;
851 int property_table_size
;
854 property_table_entry entry
;
855 property_table_entry
*rv
;
857 if (property_table_size
== 0)
860 entry
.address
= addr
;
864 rv
= bsearch (&entry
, property_table
, property_table_size
,
865 sizeof (property_table_entry
), property_table_matches
);
871 elf_xtensa_in_literal_pool (lit_table
, lit_table_size
, addr
)
872 property_table_entry
*lit_table
;
876 if (elf_xtensa_find_property_entry (lit_table
, lit_table_size
, addr
))
883 /* Look through the relocs for a section during the first phase, and
884 calculate needed space in the dynamic reloc sections. */
887 elf_xtensa_check_relocs (abfd
, info
, sec
, relocs
)
889 struct bfd_link_info
*info
;
891 const Elf_Internal_Rela
*relocs
;
893 Elf_Internal_Shdr
*symtab_hdr
;
894 struct elf_link_hash_entry
**sym_hashes
;
895 const Elf_Internal_Rela
*rel
;
896 const Elf_Internal_Rela
*rel_end
;
898 if (info
->relocatable
)
901 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
902 sym_hashes
= elf_sym_hashes (abfd
);
904 rel_end
= relocs
+ sec
->reloc_count
;
905 for (rel
= relocs
; rel
< rel_end
; rel
++)
908 unsigned long r_symndx
;
909 struct elf_link_hash_entry
*h
;
911 r_symndx
= ELF32_R_SYM (rel
->r_info
);
912 r_type
= ELF32_R_TYPE (rel
->r_info
);
914 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
916 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
921 if (r_symndx
< symtab_hdr
->sh_info
)
925 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
926 while (h
->root
.type
== bfd_link_hash_indirect
927 || h
->root
.type
== bfd_link_hash_warning
)
928 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
937 if ((sec
->flags
& SEC_ALLOC
) != 0)
939 if (h
->got
.refcount
<= 0)
942 h
->got
.refcount
+= 1;
947 /* If this relocation is against a local symbol, then it's
948 exactly the same as a normal local GOT entry. */
952 if ((sec
->flags
& SEC_ALLOC
) != 0)
954 if (h
->plt
.refcount
<= 0)
960 h
->plt
.refcount
+= 1;
962 /* Keep track of the total PLT relocation count even if we
963 don't yet know whether the dynamic sections will be
965 plt_reloc_count
+= 1;
967 if (elf_hash_table (info
)->dynamic_sections_created
)
969 if (!add_extra_plt_sections (elf_hash_table (info
)->dynobj
,
977 if ((sec
->flags
& SEC_ALLOC
) != 0)
979 bfd_signed_vma
*local_got_refcounts
;
981 /* This is a global offset table entry for a local symbol. */
982 local_got_refcounts
= elf_local_got_refcounts (abfd
);
983 if (local_got_refcounts
== NULL
)
987 size
= symtab_hdr
->sh_info
;
988 size
*= sizeof (bfd_signed_vma
);
989 local_got_refcounts
=
990 (bfd_signed_vma
*) bfd_zalloc (abfd
, size
);
991 if (local_got_refcounts
== NULL
)
993 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
995 local_got_refcounts
[r_symndx
] += 1;
1002 case R_XTENSA_SLOT0_OP
:
1003 case R_XTENSA_SLOT1_OP
:
1004 case R_XTENSA_SLOT2_OP
:
1005 case R_XTENSA_SLOT3_OP
:
1006 case R_XTENSA_SLOT4_OP
:
1007 case R_XTENSA_SLOT5_OP
:
1008 case R_XTENSA_SLOT6_OP
:
1009 case R_XTENSA_SLOT7_OP
:
1010 case R_XTENSA_SLOT8_OP
:
1011 case R_XTENSA_SLOT9_OP
:
1012 case R_XTENSA_SLOT10_OP
:
1013 case R_XTENSA_SLOT11_OP
:
1014 case R_XTENSA_SLOT12_OP
:
1015 case R_XTENSA_SLOT13_OP
:
1016 case R_XTENSA_SLOT14_OP
:
1017 case R_XTENSA_SLOT0_ALT
:
1018 case R_XTENSA_SLOT1_ALT
:
1019 case R_XTENSA_SLOT2_ALT
:
1020 case R_XTENSA_SLOT3_ALT
:
1021 case R_XTENSA_SLOT4_ALT
:
1022 case R_XTENSA_SLOT5_ALT
:
1023 case R_XTENSA_SLOT6_ALT
:
1024 case R_XTENSA_SLOT7_ALT
:
1025 case R_XTENSA_SLOT8_ALT
:
1026 case R_XTENSA_SLOT9_ALT
:
1027 case R_XTENSA_SLOT10_ALT
:
1028 case R_XTENSA_SLOT11_ALT
:
1029 case R_XTENSA_SLOT12_ALT
:
1030 case R_XTENSA_SLOT13_ALT
:
1031 case R_XTENSA_SLOT14_ALT
:
1032 case R_XTENSA_ASM_EXPAND
:
1033 case R_XTENSA_ASM_SIMPLIFY
:
1034 case R_XTENSA_DIFF8
:
1035 case R_XTENSA_DIFF16
:
1036 case R_XTENSA_DIFF32
:
1037 /* Nothing to do for these. */
1040 case R_XTENSA_GNU_VTINHERIT
:
1041 /* This relocation describes the C++ object vtable hierarchy.
1042 Reconstruct it for later use during GC. */
1043 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1047 case R_XTENSA_GNU_VTENTRY
:
1048 /* This relocation describes which C++ vtable entries are actually
1049 used. Record for later use during GC. */
1050 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1064 elf_xtensa_hide_symbol (info
, h
, force_local
)
1065 struct bfd_link_info
*info
;
1066 struct elf_link_hash_entry
*h
;
1067 bfd_boolean force_local
;
1069 /* For a shared link, move the plt refcount to the got refcount to leave
1070 space for RELATIVE relocs. */
1071 elf_xtensa_make_sym_local (info
, h
);
1073 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
);
1077 /* Return the section that should be marked against GC for a given
1081 elf_xtensa_gc_mark_hook (sec
, info
, rel
, h
, sym
)
1083 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1084 Elf_Internal_Rela
*rel
;
1085 struct elf_link_hash_entry
*h
;
1086 Elf_Internal_Sym
*sym
;
1090 switch (ELF32_R_TYPE (rel
->r_info
))
1092 case R_XTENSA_GNU_VTINHERIT
:
1093 case R_XTENSA_GNU_VTENTRY
:
1097 switch (h
->root
.type
)
1099 case bfd_link_hash_defined
:
1100 case bfd_link_hash_defweak
:
1101 return h
->root
.u
.def
.section
;
1103 case bfd_link_hash_common
:
1104 return h
->root
.u
.c
.p
->section
;
1112 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
1117 /* Update the GOT & PLT entry reference counts
1118 for the section being removed. */
1121 elf_xtensa_gc_sweep_hook (abfd
, info
, sec
, relocs
)
1123 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1125 const Elf_Internal_Rela
*relocs
;
1127 Elf_Internal_Shdr
*symtab_hdr
;
1128 struct elf_link_hash_entry
**sym_hashes
;
1129 bfd_signed_vma
*local_got_refcounts
;
1130 const Elf_Internal_Rela
*rel
, *relend
;
1132 if ((sec
->flags
& SEC_ALLOC
) == 0)
1135 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1136 sym_hashes
= elf_sym_hashes (abfd
);
1137 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1139 relend
= relocs
+ sec
->reloc_count
;
1140 for (rel
= relocs
; rel
< relend
; rel
++)
1142 unsigned long r_symndx
;
1143 unsigned int r_type
;
1144 struct elf_link_hash_entry
*h
= NULL
;
1146 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1147 if (r_symndx
>= symtab_hdr
->sh_info
)
1148 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1150 r_type
= ELF32_R_TYPE (rel
->r_info
);
1156 if (h
->got
.refcount
> 0)
1163 if (h
->plt
.refcount
> 0)
1168 if (local_got_refcounts
[r_symndx
] > 0)
1169 local_got_refcounts
[r_symndx
] -= 1;
1181 /* Create all the dynamic sections. */
1184 elf_xtensa_create_dynamic_sections (dynobj
, info
)
1186 struct bfd_link_info
*info
;
1188 flagword flags
, noalloc_flags
;
1191 /* First do all the standard stuff. */
1192 if (! _bfd_elf_create_dynamic_sections (dynobj
, info
))
1195 /* Create any extra PLT sections in case check_relocs has already
1196 been called on all the non-dynamic input files. */
1197 if (!add_extra_plt_sections (dynobj
, plt_reloc_count
))
1200 noalloc_flags
= (SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1201 | SEC_LINKER_CREATED
| SEC_READONLY
);
1202 flags
= noalloc_flags
| SEC_ALLOC
| SEC_LOAD
;
1204 /* Mark the ".got.plt" section READONLY. */
1205 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
1207 || ! bfd_set_section_flags (dynobj
, s
, flags
))
1210 /* Create ".rela.got". */
1211 s
= bfd_make_section (dynobj
, ".rela.got");
1213 || ! bfd_set_section_flags (dynobj
, s
, flags
)
1214 || ! bfd_set_section_alignment (dynobj
, s
, 2))
1217 /* Create ".got.loc" (literal tables for use by dynamic linker). */
1218 s
= bfd_make_section (dynobj
, ".got.loc");
1220 || ! bfd_set_section_flags (dynobj
, s
, flags
)
1221 || ! bfd_set_section_alignment (dynobj
, s
, 2))
1224 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
1225 s
= bfd_make_section (dynobj
, ".xt.lit.plt");
1227 || ! bfd_set_section_flags (dynobj
, s
, noalloc_flags
)
1228 || ! bfd_set_section_alignment (dynobj
, s
, 2))
1236 add_extra_plt_sections (dynobj
, count
)
1242 /* Iterate over all chunks except 0 which uses the standard ".plt" and
1243 ".got.plt" sections. */
1244 for (chunk
= count
/ PLT_ENTRIES_PER_CHUNK
; chunk
> 0; chunk
--)
1250 /* Stop when we find a section has already been created. */
1251 if (elf_xtensa_get_plt_section (dynobj
, chunk
))
1254 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1255 | SEC_LINKER_CREATED
| SEC_READONLY
);
1257 sname
= (char *) bfd_malloc (10);
1258 sprintf (sname
, ".plt.%u", chunk
);
1259 s
= bfd_make_section (dynobj
, sname
);
1261 || ! bfd_set_section_flags (dynobj
, s
, flags
| SEC_CODE
)
1262 || ! bfd_set_section_alignment (dynobj
, s
, 2))
1265 sname
= (char *) bfd_malloc (14);
1266 sprintf (sname
, ".got.plt.%u", chunk
);
1267 s
= bfd_make_section (dynobj
, sname
);
1269 || ! bfd_set_section_flags (dynobj
, s
, flags
)
1270 || ! bfd_set_section_alignment (dynobj
, s
, 2))
1278 /* Adjust a symbol defined by a dynamic object and referenced by a
1279 regular object. The current definition is in some section of the
1280 dynamic object, but we're not including those sections. We have to
1281 change the definition to something the rest of the link can
1285 elf_xtensa_adjust_dynamic_symbol (info
, h
)
1286 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1287 struct elf_link_hash_entry
*h
;
1289 /* If this is a weak symbol, and there is a real definition, the
1290 processor independent code will have arranged for us to see the
1291 real definition first, and we can just use the same value. */
1292 if (h
->u
.weakdef
!= NULL
)
1294 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
1295 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
1296 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
1297 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
1301 /* This is a reference to a symbol defined by a dynamic object. The
1302 reference must go through the GOT, so there's no need for COPY relocs,
1310 elf_xtensa_make_sym_local (info
, h
)
1311 struct bfd_link_info
*info
;
1312 struct elf_link_hash_entry
*h
;
1316 if (h
->plt
.refcount
> 0)
1318 /* Will use RELATIVE relocs instead of JMP_SLOT relocs. */
1319 if (h
->got
.refcount
< 0)
1320 h
->got
.refcount
= 0;
1321 h
->got
.refcount
+= h
->plt
.refcount
;
1322 h
->plt
.refcount
= 0;
1327 /* Don't need any dynamic relocations at all. */
1328 h
->plt
.refcount
= 0;
1329 h
->got
.refcount
= 0;
1335 elf_xtensa_fix_refcounts (h
, arg
)
1336 struct elf_link_hash_entry
*h
;
1339 struct bfd_link_info
*info
= (struct bfd_link_info
*) arg
;
1341 if (h
->root
.type
== bfd_link_hash_warning
)
1342 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1344 if (! xtensa_elf_dynamic_symbol_p (h
, info
))
1345 elf_xtensa_make_sym_local (info
, h
);
1352 elf_xtensa_allocate_plt_size (h
, arg
)
1353 struct elf_link_hash_entry
*h
;
1356 asection
*srelplt
= (asection
*) arg
;
1358 if (h
->root
.type
== bfd_link_hash_warning
)
1359 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1361 if (h
->plt
.refcount
> 0)
1362 srelplt
->size
+= (h
->plt
.refcount
* sizeof (Elf32_External_Rela
));
1369 elf_xtensa_allocate_got_size (h
, arg
)
1370 struct elf_link_hash_entry
*h
;
1373 asection
*srelgot
= (asection
*) arg
;
1375 if (h
->root
.type
== bfd_link_hash_warning
)
1376 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1378 if (h
->got
.refcount
> 0)
1379 srelgot
->size
+= (h
->got
.refcount
* sizeof (Elf32_External_Rela
));
1386 elf_xtensa_allocate_local_got_size (info
, srelgot
)
1387 struct bfd_link_info
*info
;
1392 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
1394 bfd_signed_vma
*local_got_refcounts
;
1395 bfd_size_type j
, cnt
;
1396 Elf_Internal_Shdr
*symtab_hdr
;
1398 local_got_refcounts
= elf_local_got_refcounts (i
);
1399 if (!local_got_refcounts
)
1402 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
1403 cnt
= symtab_hdr
->sh_info
;
1405 for (j
= 0; j
< cnt
; ++j
)
1407 if (local_got_refcounts
[j
] > 0)
1408 srelgot
->size
+= (local_got_refcounts
[j
]
1409 * sizeof (Elf32_External_Rela
));
1415 /* Set the sizes of the dynamic sections. */
1418 elf_xtensa_size_dynamic_sections (output_bfd
, info
)
1419 bfd
*output_bfd ATTRIBUTE_UNUSED
;
1420 struct bfd_link_info
*info
;
1423 asection
*s
, *srelplt
, *splt
, *sgotplt
, *srelgot
, *spltlittbl
, *sgotloc
;
1424 bfd_boolean relplt
, relgot
;
1425 int plt_entries
, plt_chunks
, chunk
;
1431 dynobj
= elf_hash_table (info
)->dynobj
;
1435 if (elf_hash_table (info
)->dynamic_sections_created
)
1437 /* Set the contents of the .interp section to the interpreter. */
1438 if (info
->executable
)
1440 s
= bfd_get_section_by_name (dynobj
, ".interp");
1443 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1444 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1447 /* Allocate room for one word in ".got". */
1448 s
= bfd_get_section_by_name (dynobj
, ".got");
1453 /* Adjust refcounts for symbols that we now know are not "dynamic". */
1454 elf_link_hash_traverse (elf_hash_table (info
),
1455 elf_xtensa_fix_refcounts
,
1458 /* Allocate space in ".rela.got" for literals that reference
1460 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
1461 if (srelgot
== NULL
)
1463 elf_link_hash_traverse (elf_hash_table (info
),
1464 elf_xtensa_allocate_got_size
,
1467 /* If we are generating a shared object, we also need space in
1468 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1469 reference local symbols. */
1471 elf_xtensa_allocate_local_got_size (info
, srelgot
);
1473 /* Allocate space in ".rela.plt" for literals that have PLT entries. */
1474 srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
1475 if (srelplt
== NULL
)
1477 elf_link_hash_traverse (elf_hash_table (info
),
1478 elf_xtensa_allocate_plt_size
,
1481 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1482 each PLT entry, we need the PLT code plus a 4-byte literal.
1483 For each chunk of ".plt", we also need two more 4-byte
1484 literals, two corresponding entries in ".rela.got", and an
1485 8-byte entry in ".xt.lit.plt". */
1486 spltlittbl
= bfd_get_section_by_name (dynobj
, ".xt.lit.plt");
1487 if (spltlittbl
== NULL
)
1490 plt_entries
= srelplt
->size
/ sizeof (Elf32_External_Rela
);
1492 (plt_entries
+ PLT_ENTRIES_PER_CHUNK
- 1) / PLT_ENTRIES_PER_CHUNK
;
1494 /* Iterate over all the PLT chunks, including any extra sections
1495 created earlier because the initial count of PLT relocations
1496 was an overestimate. */
1498 (splt
= elf_xtensa_get_plt_section (dynobj
, chunk
)) != NULL
;
1503 sgotplt
= elf_xtensa_get_gotplt_section (dynobj
, chunk
);
1504 if (sgotplt
== NULL
)
1507 if (chunk
< plt_chunks
- 1)
1508 chunk_entries
= PLT_ENTRIES_PER_CHUNK
;
1509 else if (chunk
== plt_chunks
- 1)
1510 chunk_entries
= plt_entries
- (chunk
* PLT_ENTRIES_PER_CHUNK
);
1514 if (chunk_entries
!= 0)
1516 sgotplt
->size
= 4 * (chunk_entries
+ 2);
1517 splt
->size
= PLT_ENTRY_SIZE
* chunk_entries
;
1518 srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
1519 spltlittbl
->size
+= 8;
1528 /* Allocate space in ".got.loc" to match the total size of all the
1530 sgotloc
= bfd_get_section_by_name (dynobj
, ".got.loc");
1531 if (sgotloc
== NULL
)
1533 sgotloc
->size
= spltlittbl
->size
;
1534 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
1536 if (abfd
->flags
& DYNAMIC
)
1538 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1540 if (! elf_discarded_section (s
)
1541 && xtensa_is_littable_section (s
)
1543 sgotloc
->size
+= s
->size
;
1548 /* Allocate memory for dynamic sections. */
1551 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1556 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1559 /* It's OK to base decisions on the section name, because none
1560 of the dynobj section names depend upon the input files. */
1561 name
= bfd_get_section_name (dynobj
, s
);
1565 if (strncmp (name
, ".rela", 5) == 0)
1567 if (strcmp (name
, ".rela.plt") == 0)
1569 else if (strcmp (name
, ".rela.got") == 0)
1572 /* We use the reloc_count field as a counter if we need
1573 to copy relocs into the output file. */
1576 else if (strncmp (name
, ".plt.", 5) == 0
1577 || strncmp (name
, ".got.plt.", 9) == 0)
1581 /* If we don't need this section, strip it from the output
1582 file. We must create the ".plt*" and ".got.plt*"
1583 sections in create_dynamic_sections and/or check_relocs
1584 based on a conservative estimate of the PLT relocation
1585 count, because the sections must be created before the
1586 linker maps input sections to output sections. The
1587 linker does that before size_dynamic_sections, where we
1588 compute the exact size of the PLT, so there may be more
1589 of these sections than are actually needed. */
1593 else if (strcmp (name
, ".got") != 0
1594 && strcmp (name
, ".plt") != 0
1595 && strcmp (name
, ".got.plt") != 0
1596 && strcmp (name
, ".xt.lit.plt") != 0
1597 && strcmp (name
, ".got.loc") != 0)
1599 /* It's not one of our sections, so don't allocate space. */
1604 _bfd_strip_section_from_output (info
, s
);
1607 /* Allocate memory for the section contents. */
1608 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1609 if (s
->contents
== NULL
&& s
->size
!= 0)
1614 if (elf_hash_table (info
)->dynamic_sections_created
)
1616 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1617 known until finish_dynamic_sections, but we need to get the relocs
1618 in place before they are sorted. */
1619 if (srelgot
== NULL
)
1621 for (chunk
= 0; chunk
< plt_chunks
; chunk
++)
1623 Elf_Internal_Rela irela
;
1627 irela
.r_info
= ELF32_R_INFO (0, R_XTENSA_RTLD
);
1630 loc
= (srelgot
->contents
1631 + srelgot
->reloc_count
* sizeof (Elf32_External_Rela
));
1632 bfd_elf32_swap_reloca_out (output_bfd
, &irela
, loc
);
1633 bfd_elf32_swap_reloca_out (output_bfd
, &irela
,
1634 loc
+ sizeof (Elf32_External_Rela
));
1635 srelgot
->reloc_count
+= 2;
1638 /* Add some entries to the .dynamic section. We fill in the
1639 values later, in elf_xtensa_finish_dynamic_sections, but we
1640 must add the entries now so that we get the correct size for
1641 the .dynamic section. The DT_DEBUG entry is filled in by the
1642 dynamic linker and used by the debugger. */
1643 #define add_dynamic_entry(TAG, VAL) \
1644 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1648 if (!add_dynamic_entry (DT_DEBUG
, 0))
1654 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1655 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1656 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1657 || !add_dynamic_entry (DT_JMPREL
, 0))
1663 if (!add_dynamic_entry (DT_RELA
, 0)
1664 || !add_dynamic_entry (DT_RELASZ
, 0)
1665 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
)))
1669 if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF
, 0)
1670 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ
, 0))
1673 #undef add_dynamic_entry
1679 /* Remove any PT_LOAD segments with no allocated sections. Prior to
1680 binutils 2.13, this function used to remove the non-SEC_ALLOC
1681 sections from PT_LOAD segments, but that task has now been moved
1682 into elf.c. We still need this function to remove any empty
1683 segments that result, but there's nothing Xtensa-specific about
1684 this and it probably ought to be moved into elf.c as well. */
1687 elf_xtensa_modify_segment_map (abfd
, info
)
1689 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1691 struct elf_segment_map
**m_p
;
1693 m_p
= &elf_tdata (abfd
)->segment_map
;
1694 while (*m_p
!= NULL
)
1696 if ((*m_p
)->p_type
== PT_LOAD
&& (*m_p
)->count
== 0)
1697 *m_p
= (*m_p
)->next
;
1699 m_p
= &(*m_p
)->next
;
1705 /* Perform the specified relocation. The instruction at (contents + address)
1706 is modified to set one operand to represent the value in "relocation". The
1707 operand position is determined by the relocation type recorded in the
1710 #define CALL_SEGMENT_BITS (30)
1711 #define CALL_SEGMENT_SIZE (1<<CALL_SEGMENT_BITS)
1713 static bfd_reloc_status_type
1714 elf_xtensa_do_reloc (howto
, abfd
, input_section
, relocation
,
1715 contents
, address
, is_weak_undef
, error_message
)
1716 reloc_howto_type
*howto
;
1718 asection
*input_section
;
1722 bfd_boolean is_weak_undef
;
1723 char **error_message
;
1726 xtensa_opcode opcode
;
1727 xtensa_isa isa
= xtensa_default_isa
;
1728 static xtensa_insnbuf ibuff
= NULL
;
1729 static xtensa_insnbuf sbuff
= NULL
;
1730 bfd_vma self_address
= 0;
1731 bfd_size_type input_size
;
1737 ibuff
= xtensa_insnbuf_alloc (isa
);
1738 sbuff
= xtensa_insnbuf_alloc (isa
);
1741 input_size
= bfd_get_section_limit (abfd
, input_section
);
1743 switch (howto
->type
)
1746 case R_XTENSA_DIFF8
:
1747 case R_XTENSA_DIFF16
:
1748 case R_XTENSA_DIFF32
:
1749 return bfd_reloc_ok
;
1751 case R_XTENSA_ASM_EXPAND
:
1754 /* Check for windowed CALL across a 1GB boundary. */
1755 xtensa_opcode opcode
=
1756 get_expanded_call_opcode (contents
+ address
,
1757 input_size
- address
, 0);
1758 if (is_windowed_call_opcode (opcode
))
1760 self_address
= (input_section
->output_section
->vma
1761 + input_section
->output_offset
1763 if ((self_address
>> CALL_SEGMENT_BITS
)
1764 != (relocation
>> CALL_SEGMENT_BITS
))
1766 *error_message
= "windowed longcall crosses 1GB boundary; "
1768 return bfd_reloc_dangerous
;
1772 return bfd_reloc_ok
;
1774 case R_XTENSA_ASM_SIMPLIFY
:
1776 /* Convert the L32R/CALLX to CALL. */
1777 bfd_reloc_status_type retval
=
1778 elf_xtensa_do_asm_simplify (contents
, address
, input_size
,
1780 if (retval
!= bfd_reloc_ok
)
1781 return bfd_reloc_dangerous
;
1783 /* The CALL needs to be relocated. Continue below for that part. */
1785 howto
= &elf_howto_table
[(unsigned) R_XTENSA_SLOT0_OP
];
1793 x
= bfd_get_32 (abfd
, contents
+ address
);
1795 bfd_put_32 (abfd
, x
, contents
+ address
);
1797 return bfd_reloc_ok
;
1800 /* Only instruction slot-specific relocations handled below.... */
1801 slot
= get_relocation_slot (howto
->type
);
1802 if (slot
== XTENSA_UNDEFINED
)
1804 *error_message
= "unexpected relocation";
1805 return bfd_reloc_dangerous
;
1808 /* Read the instruction into a buffer and decode the opcode. */
1809 xtensa_insnbuf_from_chars (isa
, ibuff
, contents
+ address
,
1810 input_size
- address
);
1811 fmt
= xtensa_format_decode (isa
, ibuff
);
1812 if (fmt
== XTENSA_UNDEFINED
)
1814 *error_message
= "cannot decode instruction format";
1815 return bfd_reloc_dangerous
;
1818 xtensa_format_get_slot (isa
, fmt
, slot
, ibuff
, sbuff
);
1820 opcode
= xtensa_opcode_decode (isa
, fmt
, slot
, sbuff
);
1821 if (opcode
== XTENSA_UNDEFINED
)
1823 *error_message
= "cannot decode instruction opcode";
1824 return bfd_reloc_dangerous
;
1827 /* Check for opcode-specific "alternate" relocations. */
1828 if (is_alt_relocation (howto
->type
))
1830 if (opcode
== get_l32r_opcode ())
1832 /* Handle the special-case of non-PC-relative L32R instructions. */
1833 bfd
*output_bfd
= input_section
->output_section
->owner
;
1834 asection
*lit4_sec
= bfd_get_section_by_name (output_bfd
, ".lit4");
1837 *error_message
= "relocation references missing .lit4 section";
1838 return bfd_reloc_dangerous
;
1840 self_address
= ((lit4_sec
->vma
& ~0xfff)
1841 + 0x40000 - 3); /* -3 to compensate for do_reloc */
1842 newval
= relocation
;
1845 else if (opcode
== get_const16_opcode ())
1847 /* ALT used for high 16 bits. */
1848 newval
= relocation
>> 16;
1853 /* No other "alternate" relocations currently defined. */
1854 *error_message
= "unexpected relocation";
1855 return bfd_reloc_dangerous
;
1858 else /* Not an "alternate" relocation.... */
1860 if (opcode
== get_const16_opcode ())
1862 newval
= relocation
& 0xffff;
1867 /* ...normal PC-relative relocation.... */
1869 /* Determine which operand is being relocated. */
1870 opnd
= get_relocation_opnd (opcode
, howto
->type
);
1871 if (opnd
== XTENSA_UNDEFINED
)
1873 *error_message
= "unexpected relocation";
1874 return bfd_reloc_dangerous
;
1877 if (!howto
->pc_relative
)
1879 *error_message
= "expected PC-relative relocation";
1880 return bfd_reloc_dangerous
;
1883 /* Calculate the PC address for this instruction. */
1884 self_address
= (input_section
->output_section
->vma
1885 + input_section
->output_offset
1888 newval
= relocation
;
1892 /* Apply the relocation. */
1893 if (xtensa_operand_do_reloc (isa
, opcode
, opnd
, &newval
, self_address
)
1894 || xtensa_operand_encode (isa
, opcode
, opnd
, &newval
)
1895 || xtensa_operand_set_field (isa
, opcode
, opnd
, fmt
, slot
,
1898 *error_message
= build_encoding_error_message (opcode
, relocation
);
1899 return bfd_reloc_dangerous
;
1902 /* Check for calls across 1GB boundaries. */
1903 if (is_direct_call_opcode (opcode
)
1904 && is_windowed_call_opcode (opcode
))
1906 if ((self_address
>> CALL_SEGMENT_BITS
)
1907 != (relocation
>> CALL_SEGMENT_BITS
))
1910 "windowed call crosses 1GB boundary; return may fail";
1911 return bfd_reloc_dangerous
;
1915 /* Write the modified instruction back out of the buffer. */
1916 xtensa_format_set_slot (isa
, fmt
, slot
, ibuff
, sbuff
);
1917 xtensa_insnbuf_to_chars (isa
, ibuff
, contents
+ address
,
1918 input_size
- address
);
1919 return bfd_reloc_ok
;
1924 vsprint_msg
VPARAMS ((const char *origmsg
, const char *fmt
, int arglen
, ...))
1926 /* To reduce the size of the memory leak,
1927 we only use a single message buffer. */
1928 static bfd_size_type alloc_size
= 0;
1929 static char *message
= NULL
;
1930 bfd_size_type orig_len
, len
= 0;
1931 bfd_boolean is_append
;
1933 VA_OPEN (ap
, arglen
);
1934 VA_FIXEDARG (ap
, const char *, origmsg
);
1936 is_append
= (origmsg
== message
);
1938 orig_len
= strlen (origmsg
);
1939 len
= orig_len
+ strlen (fmt
) + arglen
+ 20;
1940 if (len
> alloc_size
)
1942 message
= (char *) bfd_realloc (message
, len
);
1946 memcpy (message
, origmsg
, orig_len
);
1947 vsprintf (message
+ orig_len
, fmt
, ap
);
1954 build_encoding_error_message (opcode
, target_address
)
1955 xtensa_opcode opcode
;
1956 bfd_vma target_address
;
1958 const char *opname
= xtensa_opcode_name (xtensa_default_isa
, opcode
);
1961 msg
= "cannot encode";
1962 if (is_direct_call_opcode (opcode
))
1964 if ((target_address
& 0x3) != 0)
1965 msg
= "misaligned call target";
1967 msg
= "call target out of range";
1969 else if (opcode
== get_l32r_opcode ())
1971 if ((target_address
& 0x3) != 0)
1972 msg
= "misaligned literal target";
1974 msg
= "literal target out of range";
1977 return vsprint_msg (opname
, ": %s", strlen (msg
) + 2, msg
);
1981 /* This function is registered as the "special_function" in the
1982 Xtensa howto for handling simplify operations.
1983 bfd_perform_relocation / bfd_install_relocation use it to
1984 perform (install) the specified relocation. Since this replaces the code
1985 in bfd_perform_relocation, it is basically an Xtensa-specific,
1986 stripped-down version of bfd_perform_relocation. */
1988 static bfd_reloc_status_type
1989 bfd_elf_xtensa_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
,
1990 output_bfd
, error_message
)
1992 arelent
*reloc_entry
;
1995 asection
*input_section
;
1997 char **error_message
;
2000 bfd_reloc_status_type flag
;
2001 bfd_size_type octets
= reloc_entry
->address
* bfd_octets_per_byte (abfd
);
2002 bfd_vma output_base
= 0;
2003 reloc_howto_type
*howto
= reloc_entry
->howto
;
2004 asection
*reloc_target_output_section
;
2005 bfd_boolean is_weak_undef
;
2007 /* ELF relocs are against symbols. If we are producing relocatable
2008 output, and the reloc is against an external symbol, the resulting
2009 reloc will also be against the same symbol. In such a case, we
2010 don't want to change anything about the way the reloc is handled,
2011 since it will all be done at final link time. This test is similar
2012 to what bfd_elf_generic_reloc does except that it lets relocs with
2013 howto->partial_inplace go through even if the addend is non-zero.
2014 (The real problem is that partial_inplace is set for XTENSA_32
2015 relocs to begin with, but that's a long story and there's little we
2016 can do about it now....) */
2018 if (output_bfd
!= (bfd
*) NULL
2019 && (symbol
->flags
& BSF_SECTION_SYM
) == 0)
2021 reloc_entry
->address
+= input_section
->output_offset
;
2022 return bfd_reloc_ok
;
2025 /* Is the address of the relocation really within the section? */
2026 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
2027 return bfd_reloc_outofrange
;
2029 /* Work out which section the relocation is targeted at and the
2030 initial relocation command value. */
2032 /* Get symbol value. (Common symbols are special.) */
2033 if (bfd_is_com_section (symbol
->section
))
2036 relocation
= symbol
->value
;
2038 reloc_target_output_section
= symbol
->section
->output_section
;
2040 /* Convert input-section-relative symbol value to absolute. */
2041 if ((output_bfd
&& !howto
->partial_inplace
)
2042 || reloc_target_output_section
== NULL
)
2045 output_base
= reloc_target_output_section
->vma
;
2047 relocation
+= output_base
+ symbol
->section
->output_offset
;
2049 /* Add in supplied addend. */
2050 relocation
+= reloc_entry
->addend
;
2052 /* Here the variable relocation holds the final address of the
2053 symbol we are relocating against, plus any addend. */
2056 if (!howto
->partial_inplace
)
2058 /* This is a partial relocation, and we want to apply the relocation
2059 to the reloc entry rather than the raw data. Everything except
2060 relocations against section symbols has already been handled
2063 BFD_ASSERT (symbol
->flags
& BSF_SECTION_SYM
);
2064 reloc_entry
->addend
= relocation
;
2065 reloc_entry
->address
+= input_section
->output_offset
;
2066 return bfd_reloc_ok
;
2070 reloc_entry
->address
+= input_section
->output_offset
;
2071 reloc_entry
->addend
= 0;
2075 is_weak_undef
= (bfd_is_und_section (symbol
->section
)
2076 && (symbol
->flags
& BSF_WEAK
) != 0);
2077 flag
= elf_xtensa_do_reloc (howto
, abfd
, input_section
, relocation
,
2078 (bfd_byte
*) data
, (bfd_vma
) octets
,
2079 is_weak_undef
, error_message
);
2081 if (flag
== bfd_reloc_dangerous
)
2083 /* Add the symbol name to the error message. */
2084 if (! *error_message
)
2085 *error_message
= "";
2086 *error_message
= vsprint_msg (*error_message
, ": (%s + 0x%lx)",
2087 strlen (symbol
->name
) + 17,
2088 symbol
->name
, reloc_entry
->addend
);
2095 /* Set up an entry in the procedure linkage table. */
2098 elf_xtensa_create_plt_entry (dynobj
, output_bfd
, reloc_index
)
2101 unsigned reloc_index
;
2103 asection
*splt
, *sgotplt
;
2104 bfd_vma plt_base
, got_base
;
2105 bfd_vma code_offset
, lit_offset
;
2108 chunk
= reloc_index
/ PLT_ENTRIES_PER_CHUNK
;
2109 splt
= elf_xtensa_get_plt_section (dynobj
, chunk
);
2110 sgotplt
= elf_xtensa_get_gotplt_section (dynobj
, chunk
);
2111 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
);
2113 plt_base
= splt
->output_section
->vma
+ splt
->output_offset
;
2114 got_base
= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
;
2116 lit_offset
= 8 + (reloc_index
% PLT_ENTRIES_PER_CHUNK
) * 4;
2117 code_offset
= (reloc_index
% PLT_ENTRIES_PER_CHUNK
) * PLT_ENTRY_SIZE
;
2119 /* Fill in the literal entry. This is the offset of the dynamic
2120 relocation entry. */
2121 bfd_put_32 (output_bfd
, reloc_index
* sizeof (Elf32_External_Rela
),
2122 sgotplt
->contents
+ lit_offset
);
2124 /* Fill in the entry in the procedure linkage table. */
2125 memcpy (splt
->contents
+ code_offset
,
2126 (bfd_big_endian (output_bfd
)
2127 ? elf_xtensa_be_plt_entry
2128 : elf_xtensa_le_plt_entry
),
2130 bfd_put_16 (output_bfd
, l32r_offset (got_base
+ 0,
2131 plt_base
+ code_offset
+ 3),
2132 splt
->contents
+ code_offset
+ 4);
2133 bfd_put_16 (output_bfd
, l32r_offset (got_base
+ 4,
2134 plt_base
+ code_offset
+ 6),
2135 splt
->contents
+ code_offset
+ 7);
2136 bfd_put_16 (output_bfd
, l32r_offset (got_base
+ lit_offset
,
2137 plt_base
+ code_offset
+ 9),
2138 splt
->contents
+ code_offset
+ 10);
2140 return plt_base
+ code_offset
;
2144 /* Relocate an Xtensa ELF section. This is invoked by the linker for
2145 both relocatable and final links. */
2148 elf_xtensa_relocate_section (output_bfd
, info
, input_bfd
,
2149 input_section
, contents
, relocs
,
2150 local_syms
, local_sections
)
2152 struct bfd_link_info
*info
;
2154 asection
*input_section
;
2156 Elf_Internal_Rela
*relocs
;
2157 Elf_Internal_Sym
*local_syms
;
2158 asection
**local_sections
;
2160 Elf_Internal_Shdr
*symtab_hdr
;
2161 Elf_Internal_Rela
*rel
;
2162 Elf_Internal_Rela
*relend
;
2163 struct elf_link_hash_entry
**sym_hashes
;
2164 asection
*srelgot
, *srelplt
;
2166 property_table_entry
*lit_table
= 0;
2168 char *error_message
= NULL
;
2169 bfd_size_type input_size
;
2171 if (!xtensa_default_isa
)
2172 xtensa_default_isa
= xtensa_isa_init (0, 0);
2174 dynobj
= elf_hash_table (info
)->dynobj
;
2175 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2176 sym_hashes
= elf_sym_hashes (input_bfd
);
2182 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");;
2183 srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
2186 if (elf_hash_table (info
)->dynamic_sections_created
)
2188 ltblsize
= xtensa_read_table_entries (input_bfd
, input_section
,
2189 &lit_table
, XTENSA_LIT_SEC_NAME
,
2195 input_size
= bfd_get_section_limit (input_bfd
, input_section
);
2198 relend
= relocs
+ input_section
->reloc_count
;
2199 for (; rel
< relend
; rel
++)
2202 reloc_howto_type
*howto
;
2203 unsigned long r_symndx
;
2204 struct elf_link_hash_entry
*h
;
2205 Elf_Internal_Sym
*sym
;
2208 bfd_reloc_status_type r
;
2209 bfd_boolean is_weak_undef
;
2210 bfd_boolean unresolved_reloc
;
2213 r_type
= ELF32_R_TYPE (rel
->r_info
);
2214 if (r_type
== (int) R_XTENSA_GNU_VTINHERIT
2215 || r_type
== (int) R_XTENSA_GNU_VTENTRY
)
2218 if (r_type
< 0 || r_type
>= (int) R_XTENSA_max
)
2220 bfd_set_error (bfd_error_bad_value
);
2223 howto
= &elf_howto_table
[r_type
];
2225 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2227 if (info
->relocatable
)
2229 /* This is a relocatable link.
2230 1) If the reloc is against a section symbol, adjust
2231 according to the output section.
2232 2) If there is a new target for this relocation,
2233 the new target will be in the same output section.
2234 We adjust the relocation by the output section
2237 if (relaxing_section
)
2239 /* Check if this references a section in another input file. */
2240 if (!do_fix_for_relocatable_link (rel
, input_bfd
, input_section
,
2243 r_type
= ELF32_R_TYPE (rel
->r_info
);
2246 if (r_type
== R_XTENSA_ASM_SIMPLIFY
)
2248 char *error_message
= NULL
;
2249 /* Convert ASM_SIMPLIFY into the simpler relocation
2250 so that they never escape a relaxing link. */
2251 r
= contract_asm_expansion (contents
, input_size
, rel
,
2253 if (r
!= bfd_reloc_ok
)
2255 if (!((*info
->callbacks
->reloc_dangerous
)
2256 (info
, error_message
, input_bfd
, input_section
,
2260 r_type
= ELF32_R_TYPE (rel
->r_info
);
2263 /* This is a relocatable link, so we don't have to change
2264 anything unless the reloc is against a section symbol,
2265 in which case we have to adjust according to where the
2266 section symbol winds up in the output section. */
2267 if (r_symndx
< symtab_hdr
->sh_info
)
2269 sym
= local_syms
+ r_symndx
;
2270 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
2272 sec
= local_sections
[r_symndx
];
2273 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
2277 /* If there is an addend with a partial_inplace howto,
2278 then move the addend to the contents. This is a hack
2279 to work around problems with DWARF in relocatable links
2280 with some previous version of BFD. Now we can't easily get
2281 rid of the hack without breaking backward compatibility.... */
2284 howto
= &elf_howto_table
[r_type
];
2285 if (howto
->partial_inplace
)
2287 r
= elf_xtensa_do_reloc (howto
, input_bfd
, input_section
,
2288 rel
->r_addend
, contents
,
2289 rel
->r_offset
, FALSE
,
2291 if (r
!= bfd_reloc_ok
)
2293 if (!((*info
->callbacks
->reloc_dangerous
)
2294 (info
, error_message
, input_bfd
, input_section
,
2302 /* Done with work for relocatable link; continue with next reloc. */
2306 /* This is a final link. */
2311 is_weak_undef
= FALSE
;
2312 unresolved_reloc
= FALSE
;
2315 if (howto
->partial_inplace
)
2317 /* Because R_XTENSA_32 was made partial_inplace to fix some
2318 problems with DWARF info in partial links, there may be
2319 an addend stored in the contents. Take it out of there
2320 and move it back into the addend field of the reloc. */
2321 rel
->r_addend
+= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2322 bfd_put_32 (input_bfd
, 0, contents
+ rel
->r_offset
);
2325 if (r_symndx
< symtab_hdr
->sh_info
)
2327 sym
= local_syms
+ r_symndx
;
2328 sec
= local_sections
[r_symndx
];
2329 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2333 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2334 r_symndx
, symtab_hdr
, sym_hashes
,
2336 unresolved_reloc
, warned
);
2339 && !unresolved_reloc
2340 && h
->root
.type
== bfd_link_hash_undefweak
)
2341 is_weak_undef
= TRUE
;
2344 if (relaxing_section
)
2346 /* Check if this references a section in another input file. */
2347 do_fix_for_final_link (rel
, input_bfd
, input_section
, contents
,
2350 /* Update some already cached values. */
2351 r_type
= ELF32_R_TYPE (rel
->r_info
);
2352 howto
= &elf_howto_table
[r_type
];
2355 /* Sanity check the address. */
2356 if (rel
->r_offset
>= input_size
2357 && ELF32_R_TYPE (rel
->r_info
) != R_XTENSA_NONE
)
2359 (*_bfd_error_handler
)
2360 (_("%B(%A+0x%lx): relocation offset out of range (size=0x%x)"),
2361 input_bfd
, input_section
, rel
->r_offset
, input_size
);
2362 bfd_set_error (bfd_error_bad_value
);
2366 /* Generate dynamic relocations. */
2367 if (elf_hash_table (info
)->dynamic_sections_created
)
2369 bfd_boolean dynamic_symbol
= xtensa_elf_dynamic_symbol_p (h
, info
);
2371 if (dynamic_symbol
&& is_operand_relocation (r_type
))
2373 /* This is an error. The symbol's real value won't be known
2374 until runtime and it's likely to be out of range anyway. */
2375 const char *name
= h
->root
.root
.string
;
2376 error_message
= vsprint_msg ("invalid relocation for dynamic "
2378 strlen (name
) + 2, name
);
2379 if (!((*info
->callbacks
->reloc_dangerous
)
2380 (info
, error_message
, input_bfd
, input_section
,
2384 else if ((r_type
== R_XTENSA_32
|| r_type
== R_XTENSA_PLT
)
2385 && (input_section
->flags
& SEC_ALLOC
) != 0
2386 && (dynamic_symbol
|| info
->shared
))
2388 Elf_Internal_Rela outrel
;
2392 if (dynamic_symbol
&& r_type
== R_XTENSA_PLT
)
2397 BFD_ASSERT (srel
!= NULL
);
2400 _bfd_elf_section_offset (output_bfd
, info
,
2401 input_section
, rel
->r_offset
);
2403 if ((outrel
.r_offset
| 1) == (bfd_vma
) -1)
2404 memset (&outrel
, 0, sizeof outrel
);
2407 outrel
.r_offset
+= (input_section
->output_section
->vma
2408 + input_section
->output_offset
);
2410 /* Complain if the relocation is in a read-only section
2411 and not in a literal pool. */
2412 if ((input_section
->flags
& SEC_READONLY
) != 0
2413 && !elf_xtensa_in_literal_pool (lit_table
, ltblsize
,
2417 _("dynamic relocation in read-only section");
2418 if (!((*info
->callbacks
->reloc_dangerous
)
2419 (info
, error_message
, input_bfd
, input_section
,
2426 outrel
.r_addend
= rel
->r_addend
;
2429 if (r_type
== R_XTENSA_32
)
2432 ELF32_R_INFO (h
->dynindx
, R_XTENSA_GLOB_DAT
);
2435 else /* r_type == R_XTENSA_PLT */
2438 ELF32_R_INFO (h
->dynindx
, R_XTENSA_JMP_SLOT
);
2440 /* Create the PLT entry and set the initial
2441 contents of the literal entry to the address of
2444 elf_xtensa_create_plt_entry (dynobj
, output_bfd
,
2447 unresolved_reloc
= FALSE
;
2451 /* Generate a RELATIVE relocation. */
2452 outrel
.r_info
= ELF32_R_INFO (0, R_XTENSA_RELATIVE
);
2453 outrel
.r_addend
= 0;
2457 loc
= (srel
->contents
2458 + srel
->reloc_count
++ * sizeof (Elf32_External_Rela
));
2459 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
2460 BFD_ASSERT (sizeof (Elf32_External_Rela
) * srel
->reloc_count
2465 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2466 because such sections are not SEC_ALLOC and thus ld.so will
2467 not process them. */
2468 if (unresolved_reloc
2469 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2471 (*_bfd_error_handler
)
2472 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2475 (long) rel
->r_offset
,
2476 h
->root
.root
.string
);
2478 /* There's no point in calling bfd_perform_relocation here.
2479 Just go directly to our "special function". */
2480 r
= elf_xtensa_do_reloc (howto
, input_bfd
, input_section
,
2481 relocation
+ rel
->r_addend
,
2482 contents
, rel
->r_offset
, is_weak_undef
,
2485 if (r
!= bfd_reloc_ok
&& !warned
)
2489 BFD_ASSERT (r
== bfd_reloc_dangerous
|| r
== bfd_reloc_other
);
2490 BFD_ASSERT (error_message
!= (char *) NULL
);
2493 name
= h
->root
.root
.string
;
2496 name
= bfd_elf_string_from_elf_section
2497 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
2498 if (name
&& *name
== '\0')
2499 name
= bfd_section_name (input_bfd
, sec
);
2503 if (rel
->r_addend
== 0)
2504 error_message
= vsprint_msg (error_message
, ": %s",
2505 strlen (name
) + 2, name
);
2507 error_message
= vsprint_msg (error_message
, ": (%s+0x%x)",
2509 name
, rel
->r_addend
);
2512 if (!((*info
->callbacks
->reloc_dangerous
)
2513 (info
, error_message
, input_bfd
, input_section
,
2522 input_section
->reloc_done
= TRUE
;
2528 /* Finish up dynamic symbol handling. There's not much to do here since
2529 the PLT and GOT entries are all set up by relocate_section. */
2532 elf_xtensa_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
2533 bfd
*output_bfd ATTRIBUTE_UNUSED
;
2534 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
2535 struct elf_link_hash_entry
*h
;
2536 Elf_Internal_Sym
*sym
;
2541 /* Mark the symbol as undefined, rather than as defined in
2542 the .plt section. Leave the value alone. */
2543 sym
->st_shndx
= SHN_UNDEF
;
2546 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2547 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
2548 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
2549 sym
->st_shndx
= SHN_ABS
;
2555 /* Combine adjacent literal table entries in the output. Adjacent
2556 entries within each input section may have been removed during
2557 relaxation, but we repeat the process here, even though it's too late
2558 to shrink the output section, because it's important to minimize the
2559 number of literal table entries to reduce the start-up work for the
2560 runtime linker. Returns the number of remaining table entries or -1
2564 elf_xtensa_combine_prop_entries (output_bfd
, sxtlit
, sgotloc
)
2570 property_table_entry
*table
;
2571 bfd_size_type section_size
, sgotloc_size
;
2575 section_size
= sxtlit
->size
;
2576 BFD_ASSERT (section_size
% 8 == 0);
2577 num
= section_size
/ 8;
2579 sgotloc_size
= sgotloc
->size
;
2580 if (sgotloc_size
!= section_size
)
2582 (*_bfd_error_handler
)
2583 (_("internal inconsistency in size of .got.loc section"));
2587 table
= bfd_malloc (num
* sizeof (property_table_entry
));
2591 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
2592 propagates to the output section, where it doesn't really apply and
2593 where it breaks the following call to bfd_malloc_and_get_section. */
2594 sxtlit
->flags
&= ~SEC_IN_MEMORY
;
2596 if (!bfd_malloc_and_get_section (output_bfd
, sxtlit
, &contents
))
2604 /* There should never be any relocations left at this point, so this
2605 is quite a bit easier than what is done during relaxation. */
2607 /* Copy the raw contents into a property table array and sort it. */
2609 for (n
= 0; n
< num
; n
++)
2611 table
[n
].address
= bfd_get_32 (output_bfd
, &contents
[offset
]);
2612 table
[n
].size
= bfd_get_32 (output_bfd
, &contents
[offset
+ 4]);
2615 qsort (table
, num
, sizeof (property_table_entry
), property_table_compare
);
2617 for (n
= 0; n
< num
; n
++)
2619 bfd_boolean remove
= FALSE
;
2621 if (table
[n
].size
== 0)
2624 (table
[n
-1].address
+ table
[n
-1].size
== table
[n
].address
))
2626 table
[n
-1].size
+= table
[n
].size
;
2632 for (m
= n
; m
< num
- 1; m
++)
2634 table
[m
].address
= table
[m
+1].address
;
2635 table
[m
].size
= table
[m
+1].size
;
2643 /* Copy the data back to the raw contents. */
2645 for (n
= 0; n
< num
; n
++)
2647 bfd_put_32 (output_bfd
, table
[n
].address
, &contents
[offset
]);
2648 bfd_put_32 (output_bfd
, table
[n
].size
, &contents
[offset
+ 4]);
2652 /* Clear the removed bytes. */
2653 if ((bfd_size_type
) (num
* 8) < section_size
)
2654 memset (&contents
[num
* 8], 0, section_size
- num
* 8);
2656 if (! bfd_set_section_contents (output_bfd
, sxtlit
, contents
, 0,
2660 /* Copy the contents to ".got.loc". */
2661 memcpy (sgotloc
->contents
, contents
, section_size
);
2669 /* Finish up the dynamic sections. */
2672 elf_xtensa_finish_dynamic_sections (output_bfd
, info
)
2674 struct bfd_link_info
*info
;
2677 asection
*sdyn
, *srelplt
, *sgot
, *sxtlit
, *sgotloc
;
2678 Elf32_External_Dyn
*dyncon
, *dynconend
;
2679 int num_xtlit_entries
;
2681 if (! elf_hash_table (info
)->dynamic_sections_created
)
2684 dynobj
= elf_hash_table (info
)->dynobj
;
2685 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2686 BFD_ASSERT (sdyn
!= NULL
);
2688 /* Set the first entry in the global offset table to the address of
2689 the dynamic section. */
2690 sgot
= bfd_get_section_by_name (dynobj
, ".got");
2693 BFD_ASSERT (sgot
->size
== 4);
2695 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
2697 bfd_put_32 (output_bfd
,
2698 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
2702 srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
2703 if (srelplt
!= NULL
&& srelplt
->size
!= 0)
2705 asection
*sgotplt
, *srelgot
, *spltlittbl
;
2706 int chunk
, plt_chunks
, plt_entries
;
2707 Elf_Internal_Rela irela
;
2709 unsigned rtld_reloc
;
2711 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");;
2712 BFD_ASSERT (srelgot
!= NULL
);
2714 spltlittbl
= bfd_get_section_by_name (dynobj
, ".xt.lit.plt");
2715 BFD_ASSERT (spltlittbl
!= NULL
);
2717 /* Find the first XTENSA_RTLD relocation. Presumably the rest
2718 of them follow immediately after.... */
2719 for (rtld_reloc
= 0; rtld_reloc
< srelgot
->reloc_count
; rtld_reloc
++)
2721 loc
= srelgot
->contents
+ rtld_reloc
* sizeof (Elf32_External_Rela
);
2722 bfd_elf32_swap_reloca_in (output_bfd
, loc
, &irela
);
2723 if (ELF32_R_TYPE (irela
.r_info
) == R_XTENSA_RTLD
)
2726 BFD_ASSERT (rtld_reloc
< srelgot
->reloc_count
);
2728 plt_entries
= srelplt
->size
/ sizeof (Elf32_External_Rela
);
2730 (plt_entries
+ PLT_ENTRIES_PER_CHUNK
- 1) / PLT_ENTRIES_PER_CHUNK
;
2732 for (chunk
= 0; chunk
< plt_chunks
; chunk
++)
2734 int chunk_entries
= 0;
2736 sgotplt
= elf_xtensa_get_gotplt_section (dynobj
, chunk
);
2737 BFD_ASSERT (sgotplt
!= NULL
);
2739 /* Emit special RTLD relocations for the first two entries in
2740 each chunk of the .got.plt section. */
2742 loc
= srelgot
->contents
+ rtld_reloc
* sizeof (Elf32_External_Rela
);
2743 bfd_elf32_swap_reloca_in (output_bfd
, loc
, &irela
);
2744 BFD_ASSERT (ELF32_R_TYPE (irela
.r_info
) == R_XTENSA_RTLD
);
2745 irela
.r_offset
= (sgotplt
->output_section
->vma
2746 + sgotplt
->output_offset
);
2747 irela
.r_addend
= 1; /* tell rtld to set value to resolver function */
2748 bfd_elf32_swap_reloca_out (output_bfd
, &irela
, loc
);
2750 BFD_ASSERT (rtld_reloc
<= srelgot
->reloc_count
);
2752 /* Next literal immediately follows the first. */
2753 loc
+= sizeof (Elf32_External_Rela
);
2754 bfd_elf32_swap_reloca_in (output_bfd
, loc
, &irela
);
2755 BFD_ASSERT (ELF32_R_TYPE (irela
.r_info
) == R_XTENSA_RTLD
);
2756 irela
.r_offset
= (sgotplt
->output_section
->vma
2757 + sgotplt
->output_offset
+ 4);
2758 /* Tell rtld to set value to object's link map. */
2760 bfd_elf32_swap_reloca_out (output_bfd
, &irela
, loc
);
2762 BFD_ASSERT (rtld_reloc
<= srelgot
->reloc_count
);
2764 /* Fill in the literal table. */
2765 if (chunk
< plt_chunks
- 1)
2766 chunk_entries
= PLT_ENTRIES_PER_CHUNK
;
2768 chunk_entries
= plt_entries
- (chunk
* PLT_ENTRIES_PER_CHUNK
);
2770 BFD_ASSERT ((unsigned) (chunk
+ 1) * 8 <= spltlittbl
->size
);
2771 bfd_put_32 (output_bfd
,
2772 sgotplt
->output_section
->vma
+ sgotplt
->output_offset
,
2773 spltlittbl
->contents
+ (chunk
* 8) + 0);
2774 bfd_put_32 (output_bfd
,
2775 8 + (chunk_entries
* 4),
2776 spltlittbl
->contents
+ (chunk
* 8) + 4);
2779 /* All the dynamic relocations have been emitted at this point.
2780 Make sure the relocation sections are the correct size. */
2781 if (srelgot
->size
!= (sizeof (Elf32_External_Rela
)
2782 * srelgot
->reloc_count
)
2783 || srelplt
->size
!= (sizeof (Elf32_External_Rela
)
2784 * srelplt
->reloc_count
))
2787 /* The .xt.lit.plt section has just been modified. This must
2788 happen before the code below which combines adjacent literal
2789 table entries, and the .xt.lit.plt contents have to be forced to
2791 if (! bfd_set_section_contents (output_bfd
,
2792 spltlittbl
->output_section
,
2793 spltlittbl
->contents
,
2794 spltlittbl
->output_offset
,
2797 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
2798 spltlittbl
->flags
&= ~SEC_HAS_CONTENTS
;
2801 /* Combine adjacent literal table entries. */
2802 BFD_ASSERT (! info
->relocatable
);
2803 sxtlit
= bfd_get_section_by_name (output_bfd
, ".xt.lit");
2804 sgotloc
= bfd_get_section_by_name (dynobj
, ".got.loc");
2805 BFD_ASSERT (sxtlit
&& sgotloc
);
2807 elf_xtensa_combine_prop_entries (output_bfd
, sxtlit
, sgotloc
);
2808 if (num_xtlit_entries
< 0)
2811 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
2812 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
2813 for (; dyncon
< dynconend
; dyncon
++)
2815 Elf_Internal_Dyn dyn
;
2819 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
2826 case DT_XTENSA_GOT_LOC_SZ
:
2827 dyn
.d_un
.d_val
= num_xtlit_entries
;
2830 case DT_XTENSA_GOT_LOC_OFF
:
2839 s
= bfd_get_section_by_name (output_bfd
, name
);
2841 dyn
.d_un
.d_ptr
= s
->vma
;
2845 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
2847 dyn
.d_un
.d_val
= s
->size
;
2851 /* Adjust RELASZ to not include JMPREL. This matches what
2852 glibc expects and what is done for several other ELF
2853 targets (e.g., i386, alpha), but the "correct" behavior
2854 seems to be unresolved. Since the linker script arranges
2855 for .rela.plt to follow all other relocation sections, we
2856 don't have to worry about changing the DT_RELA entry. */
2857 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
2859 dyn
.d_un
.d_val
-= s
->size
;
2863 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2870 /* Functions for dealing with the e_flags field. */
2872 /* Merge backend specific data from an object file to the output
2873 object file when linking. */
2876 elf_xtensa_merge_private_bfd_data (ibfd
, obfd
)
2880 unsigned out_mach
, in_mach
;
2881 flagword out_flag
, in_flag
;
2883 /* Check if we have the same endianess. */
2884 if (!_bfd_generic_verify_endian_match (ibfd
, obfd
))
2887 /* Don't even pretend to support mixed-format linking. */
2888 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2889 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
2892 out_flag
= elf_elfheader (obfd
)->e_flags
;
2893 in_flag
= elf_elfheader (ibfd
)->e_flags
;
2895 out_mach
= out_flag
& EF_XTENSA_MACH
;
2896 in_mach
= in_flag
& EF_XTENSA_MACH
;
2897 if (out_mach
!= in_mach
)
2899 (*_bfd_error_handler
)
2900 (_("%B: incompatible machine type. Output is 0x%x. Input is 0x%x"),
2901 ibfd
, out_mach
, in_mach
);
2902 bfd_set_error (bfd_error_wrong_format
);
2906 if (! elf_flags_init (obfd
))
2908 elf_flags_init (obfd
) = TRUE
;
2909 elf_elfheader (obfd
)->e_flags
= in_flag
;
2911 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
2912 && bfd_get_arch_info (obfd
)->the_default
)
2913 return bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
2914 bfd_get_mach (ibfd
));
2919 if ((out_flag
& EF_XTENSA_XT_INSN
) != (in_flag
& EF_XTENSA_XT_INSN
))
2920 elf_elfheader (obfd
)->e_flags
&= (~ EF_XTENSA_XT_INSN
);
2922 if ((out_flag
& EF_XTENSA_XT_LIT
) != (in_flag
& EF_XTENSA_XT_LIT
))
2923 elf_elfheader (obfd
)->e_flags
&= (~ EF_XTENSA_XT_LIT
);
2930 elf_xtensa_set_private_flags (abfd
, flags
)
2934 BFD_ASSERT (!elf_flags_init (abfd
)
2935 || elf_elfheader (abfd
)->e_flags
== flags
);
2937 elf_elfheader (abfd
)->e_flags
|= flags
;
2938 elf_flags_init (abfd
) = TRUE
;
2945 elf_xtensa_get_private_bfd_flags (abfd
)
2948 return elf_elfheader (abfd
)->e_flags
;
2953 elf_xtensa_print_private_bfd_data (abfd
, farg
)
2957 FILE *f
= (FILE *) farg
;
2958 flagword e_flags
= elf_elfheader (abfd
)->e_flags
;
2960 fprintf (f
, "\nXtensa header:\n");
2961 if ((e_flags
& EF_XTENSA_MACH
) == E_XTENSA_MACH
)
2962 fprintf (f
, "\nMachine = Base\n");
2964 fprintf (f
, "\nMachine Id = 0x%x\n", e_flags
& EF_XTENSA_MACH
);
2966 fprintf (f
, "Insn tables = %s\n",
2967 (e_flags
& EF_XTENSA_XT_INSN
) ? "true" : "false");
2969 fprintf (f
, "Literal tables = %s\n",
2970 (e_flags
& EF_XTENSA_XT_LIT
) ? "true" : "false");
2972 return _bfd_elf_print_private_bfd_data (abfd
, farg
);
2976 /* Set the right machine number for an Xtensa ELF file. */
2979 elf_xtensa_object_p (abfd
)
2983 unsigned long arch
= elf_elfheader (abfd
)->e_flags
& EF_XTENSA_MACH
;
2988 mach
= bfd_mach_xtensa
;
2994 (void) bfd_default_set_arch_mach (abfd
, bfd_arch_xtensa
, mach
);
2999 /* The final processing done just before writing out an Xtensa ELF object
3000 file. This gets the Xtensa architecture right based on the machine
3004 elf_xtensa_final_write_processing (abfd
, linker
)
3006 bfd_boolean linker ATTRIBUTE_UNUSED
;
3011 switch (mach
= bfd_get_mach (abfd
))
3013 case bfd_mach_xtensa
:
3014 val
= E_XTENSA_MACH
;
3020 elf_elfheader (abfd
)->e_flags
&= (~ EF_XTENSA_MACH
);
3021 elf_elfheader (abfd
)->e_flags
|= val
;
3025 static enum elf_reloc_type_class
3026 elf_xtensa_reloc_type_class (rela
)
3027 const Elf_Internal_Rela
*rela
;
3029 switch ((int) ELF32_R_TYPE (rela
->r_info
))
3031 case R_XTENSA_RELATIVE
:
3032 return reloc_class_relative
;
3033 case R_XTENSA_JMP_SLOT
:
3034 return reloc_class_plt
;
3036 return reloc_class_normal
;
3042 elf_xtensa_discard_info_for_section (abfd
, cookie
, info
, sec
)
3044 struct elf_reloc_cookie
*cookie
;
3045 struct bfd_link_info
*info
;
3049 bfd_vma section_size
;
3050 bfd_vma offset
, actual_offset
;
3051 size_t removed_bytes
= 0;
3053 section_size
= sec
->size
;
3054 if (section_size
== 0 || section_size
% 8 != 0)
3057 if (sec
->output_section
3058 && bfd_is_abs_section (sec
->output_section
))
3061 contents
= retrieve_contents (abfd
, sec
, info
->keep_memory
);
3065 cookie
->rels
= retrieve_internal_relocs (abfd
, sec
, info
->keep_memory
);
3068 release_contents (sec
, contents
);
3072 cookie
->rel
= cookie
->rels
;
3073 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
3075 for (offset
= 0; offset
< section_size
; offset
+= 8)
3077 actual_offset
= offset
- removed_bytes
;
3079 /* The ...symbol_deleted_p function will skip over relocs but it
3080 won't adjust their offsets, so do that here. */
3081 while (cookie
->rel
< cookie
->relend
3082 && cookie
->rel
->r_offset
< offset
)
3084 cookie
->rel
->r_offset
-= removed_bytes
;
3088 while (cookie
->rel
< cookie
->relend
3089 && cookie
->rel
->r_offset
== offset
)
3091 if (bfd_elf_reloc_symbol_deleted_p (offset
, cookie
))
3093 /* Remove the table entry. (If the reloc type is NONE, then
3094 the entry has already been merged with another and deleted
3095 during relaxation.) */
3096 if (ELF32_R_TYPE (cookie
->rel
->r_info
) != R_XTENSA_NONE
)
3098 /* Shift the contents up. */
3099 if (offset
+ 8 < section_size
)
3100 memmove (&contents
[actual_offset
],
3101 &contents
[actual_offset
+8],
3102 section_size
- offset
- 8);
3106 /* Remove this relocation. */
3107 cookie
->rel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
3110 /* Adjust the relocation offset for previous removals. This
3111 should not be done before calling ...symbol_deleted_p
3112 because it might mess up the offset comparisons there.
3113 Make sure the offset doesn't underflow in the case where
3114 the first entry is removed. */
3115 if (cookie
->rel
->r_offset
>= removed_bytes
)
3116 cookie
->rel
->r_offset
-= removed_bytes
;
3118 cookie
->rel
->r_offset
= 0;
3124 if (removed_bytes
!= 0)
3126 /* Adjust any remaining relocs (shouldn't be any). */
3127 for (; cookie
->rel
< cookie
->relend
; cookie
->rel
++)
3129 if (cookie
->rel
->r_offset
>= removed_bytes
)
3130 cookie
->rel
->r_offset
-= removed_bytes
;
3132 cookie
->rel
->r_offset
= 0;
3135 /* Clear the removed bytes. */
3136 memset (&contents
[section_size
- removed_bytes
], 0, removed_bytes
);
3138 pin_contents (sec
, contents
);
3139 pin_internal_relocs (sec
, cookie
->rels
);
3142 sec
->size
= section_size
- removed_bytes
;
3144 if (xtensa_is_littable_section (sec
))
3146 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
3150 bfd_get_section_by_name (dynobj
, ".got.loc");
3152 sgotloc
->size
-= removed_bytes
;
3158 release_contents (sec
, contents
);
3159 release_internal_relocs (sec
, cookie
->rels
);
3162 return (removed_bytes
!= 0);
3167 elf_xtensa_discard_info (abfd
, cookie
, info
)
3169 struct elf_reloc_cookie
*cookie
;
3170 struct bfd_link_info
*info
;
3173 bfd_boolean changed
= FALSE
;
3175 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3177 if (xtensa_is_property_section (sec
))
3179 if (elf_xtensa_discard_info_for_section (abfd
, cookie
, info
, sec
))
3189 elf_xtensa_ignore_discarded_relocs (sec
)
3192 return xtensa_is_property_section (sec
);
3196 /* Support for core dump NOTE sections. */
3199 elf_xtensa_grok_prstatus (abfd
, note
)
3201 Elf_Internal_Note
*note
;
3206 /* The size for Xtensa is variable, so don't try to recognize the format
3207 based on the size. Just assume this is GNU/Linux. */
3210 elf_tdata (abfd
)->core_signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
3213 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
3217 size
= note
->descsz
- offset
- 4;
3219 /* Make a ".reg/999" section. */
3220 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
3221 size
, note
->descpos
+ offset
);
3226 elf_xtensa_grok_psinfo (abfd
, note
)
3228 Elf_Internal_Note
*note
;
3230 switch (note
->descsz
)
3235 case 128: /* GNU/Linux elf_prpsinfo */
3236 elf_tdata (abfd
)->core_program
3237 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 32, 16);
3238 elf_tdata (abfd
)->core_command
3239 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 48, 80);
3242 /* Note that for some reason, a spurious space is tacked
3243 onto the end of the args in some (at least one anyway)
3244 implementations, so strip it off if it exists. */
3247 char *command
= elf_tdata (abfd
)->core_command
;
3248 int n
= strlen (command
);
3250 if (0 < n
&& command
[n
- 1] == ' ')
3251 command
[n
- 1] = '\0';
3258 /* Generic Xtensa configurability stuff. */
3260 static xtensa_opcode callx0_op
= XTENSA_UNDEFINED
;
3261 static xtensa_opcode callx4_op
= XTENSA_UNDEFINED
;
3262 static xtensa_opcode callx8_op
= XTENSA_UNDEFINED
;
3263 static xtensa_opcode callx12_op
= XTENSA_UNDEFINED
;
3264 static xtensa_opcode call0_op
= XTENSA_UNDEFINED
;
3265 static xtensa_opcode call4_op
= XTENSA_UNDEFINED
;
3266 static xtensa_opcode call8_op
= XTENSA_UNDEFINED
;
3267 static xtensa_opcode call12_op
= XTENSA_UNDEFINED
;
3270 init_call_opcodes ()
3272 if (callx0_op
== XTENSA_UNDEFINED
)
3274 callx0_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx0");
3275 callx4_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx4");
3276 callx8_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx8");
3277 callx12_op
= xtensa_opcode_lookup (xtensa_default_isa
, "callx12");
3278 call0_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call0");
3279 call4_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call4");
3280 call8_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call8");
3281 call12_op
= xtensa_opcode_lookup (xtensa_default_isa
, "call12");
3287 is_indirect_call_opcode (opcode
)
3288 xtensa_opcode opcode
;
3290 init_call_opcodes ();
3291 return (opcode
== callx0_op
3292 || opcode
== callx4_op
3293 || opcode
== callx8_op
3294 || opcode
== callx12_op
);
3299 is_direct_call_opcode (opcode
)
3300 xtensa_opcode opcode
;
3302 init_call_opcodes ();
3303 return (opcode
== call0_op
3304 || opcode
== call4_op
3305 || opcode
== call8_op
3306 || opcode
== call12_op
);
3311 is_windowed_call_opcode (opcode
)
3312 xtensa_opcode opcode
;
3314 init_call_opcodes ();
3315 return (opcode
== call4_op
3316 || opcode
== call8_op
3317 || opcode
== call12_op
3318 || opcode
== callx4_op
3319 || opcode
== callx8_op
3320 || opcode
== callx12_op
);
3324 static xtensa_opcode
3325 get_const16_opcode (void)
3327 static bfd_boolean done_lookup
= FALSE
;
3328 static xtensa_opcode const16_opcode
= XTENSA_UNDEFINED
;
3331 const16_opcode
= xtensa_opcode_lookup (xtensa_default_isa
, "const16");
3334 return const16_opcode
;
3338 static xtensa_opcode
3339 get_l32r_opcode (void)
3341 static xtensa_opcode l32r_opcode
= XTENSA_UNDEFINED
;
3342 static bfd_boolean done_lookup
= FALSE
;
3346 l32r_opcode
= xtensa_opcode_lookup (xtensa_default_isa
, "l32r");
3354 l32r_offset (addr
, pc
)
3360 offset
= addr
- ((pc
+3) & -4);
3361 BFD_ASSERT ((offset
& ((1 << 2) - 1)) == 0);
3362 offset
= (signed int) offset
>> 2;
3363 BFD_ASSERT ((signed int) offset
>> 16 == -1);
3369 get_relocation_opnd (opcode
, r_type
)
3370 xtensa_opcode opcode
;
3373 xtensa_isa isa
= xtensa_default_isa
;
3374 int last_immed
, last_opnd
, opi
;
3376 if (opcode
== XTENSA_UNDEFINED
)
3377 return XTENSA_UNDEFINED
;
3379 /* Find the last visible PC-relative immediate operand for the opcode.
3380 If there are no PC-relative immediates, then choose the last visible
3381 immediate; otherwise, fail and return XTENSA_UNDEFINED. */
3382 last_immed
= XTENSA_UNDEFINED
;
3383 last_opnd
= xtensa_opcode_num_operands (isa
, opcode
);
3384 for (opi
= last_opnd
- 1; opi
>= 0; opi
--)
3386 if (xtensa_operand_is_visible (isa
, opcode
, opi
) == 0)
3388 if (xtensa_operand_is_PCrelative (isa
, opcode
, opi
) == 1)
3393 if (last_immed
== XTENSA_UNDEFINED
3394 && xtensa_operand_is_register (isa
, opcode
, opi
) == 0)
3398 return XTENSA_UNDEFINED
;
3400 /* If the operand number was specified in an old-style relocation,
3401 check for consistency with the operand computed above. */
3402 if (r_type
>= R_XTENSA_OP0
&& r_type
<= R_XTENSA_OP2
)
3404 int reloc_opnd
= r_type
- R_XTENSA_OP0
;
3405 if (reloc_opnd
!= last_immed
)
3406 return XTENSA_UNDEFINED
;
3414 get_relocation_slot (r_type
)
3425 if (r_type
>= R_XTENSA_SLOT0_OP
&& r_type
<= R_XTENSA_SLOT14_OP
)
3426 return r_type
- R_XTENSA_SLOT0_OP
;
3427 if (r_type
>= R_XTENSA_SLOT0_ALT
&& r_type
<= R_XTENSA_SLOT14_ALT
)
3428 return r_type
- R_XTENSA_SLOT0_ALT
;
3432 return XTENSA_UNDEFINED
;
3436 /* Get the opcode for a relocation. */
3438 static xtensa_opcode
3439 get_relocation_opcode (abfd
, sec
, contents
, irel
)
3443 Elf_Internal_Rela
*irel
;
3445 static xtensa_insnbuf ibuff
= NULL
;
3446 static xtensa_insnbuf sbuff
= NULL
;
3447 xtensa_isa isa
= xtensa_default_isa
;
3451 if (contents
== NULL
)
3452 return XTENSA_UNDEFINED
;
3454 if (bfd_get_section_limit (abfd
, sec
) <= irel
->r_offset
)
3455 return XTENSA_UNDEFINED
;
3459 ibuff
= xtensa_insnbuf_alloc (isa
);
3460 sbuff
= xtensa_insnbuf_alloc (isa
);
3463 /* Decode the instruction. */
3464 xtensa_insnbuf_from_chars (isa
, ibuff
, &contents
[irel
->r_offset
],
3465 sec
->size
- irel
->r_offset
);
3466 fmt
= xtensa_format_decode (isa
, ibuff
);
3467 slot
= get_relocation_slot (ELF32_R_TYPE (irel
->r_info
));
3468 if (slot
== XTENSA_UNDEFINED
)
3469 return XTENSA_UNDEFINED
;
3470 xtensa_format_get_slot (isa
, fmt
, slot
, ibuff
, sbuff
);
3471 return xtensa_opcode_decode (isa
, fmt
, slot
, sbuff
);
3476 is_l32r_relocation (abfd
, sec
, contents
, irel
)
3480 Elf_Internal_Rela
*irel
;
3482 xtensa_opcode opcode
;
3483 if (!is_operand_relocation (ELF32_R_TYPE (irel
->r_info
)))
3485 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
3486 return (opcode
== get_l32r_opcode ());
3490 static bfd_size_type
3491 get_asm_simplify_size (contents
, content_len
, offset
)
3493 bfd_size_type content_len
;
3494 bfd_size_type offset
;
3496 bfd_size_type insnlen
, size
= 0;
3498 /* Decode the size of the next two instructions. */
3499 insnlen
= insn_decode_len (contents
, content_len
, offset
);
3505 insnlen
= insn_decode_len (contents
, content_len
, offset
+ size
);
3515 is_alt_relocation (r_type
)
3518 return (r_type
>= R_XTENSA_SLOT0_ALT
3519 && r_type
<= R_XTENSA_SLOT14_ALT
);
3524 is_operand_relocation (r_type
)
3535 if (r_type
>= R_XTENSA_SLOT0_OP
&& r_type
<= R_XTENSA_SLOT14_OP
)
3537 if (r_type
>= R_XTENSA_SLOT0_ALT
&& r_type
<= R_XTENSA_SLOT14_ALT
)
3546 #define MIN_INSN_LENGTH 2
3548 /* Return 0 if it fails to decode. */
3551 insn_decode_len (contents
, content_len
, offset
)
3553 bfd_size_type content_len
;
3554 bfd_size_type offset
;
3557 xtensa_isa isa
= xtensa_default_isa
;
3559 static xtensa_insnbuf ibuff
= NULL
;
3561 if (offset
+ MIN_INSN_LENGTH
> content_len
)
3565 ibuff
= xtensa_insnbuf_alloc (isa
);
3566 xtensa_insnbuf_from_chars (isa
, ibuff
, &contents
[offset
],
3567 content_len
- offset
);
3568 fmt
= xtensa_format_decode (isa
, ibuff
);
3569 if (fmt
== XTENSA_UNDEFINED
)
3571 insn_len
= xtensa_format_length (isa
, fmt
);
3572 if (insn_len
== XTENSA_UNDEFINED
)
3578 /* Decode the opcode for a single slot instruction.
3579 Return 0 if it fails to decode or the instruction is multi-slot. */
3582 insn_decode_opcode (contents
, content_len
, offset
, slot
)
3584 bfd_size_type content_len
;
3585 bfd_size_type offset
;
3588 xtensa_isa isa
= xtensa_default_isa
;
3590 static xtensa_insnbuf insnbuf
= NULL
;
3591 static xtensa_insnbuf slotbuf
= NULL
;
3593 if (offset
+ MIN_INSN_LENGTH
> content_len
)
3594 return XTENSA_UNDEFINED
;
3596 if (insnbuf
== NULL
)
3598 insnbuf
= xtensa_insnbuf_alloc (isa
);
3599 slotbuf
= xtensa_insnbuf_alloc (isa
);
3602 xtensa_insnbuf_from_chars (isa
, insnbuf
, &contents
[offset
],
3603 content_len
- offset
);
3604 fmt
= xtensa_format_decode (isa
, insnbuf
);
3605 if (fmt
== XTENSA_UNDEFINED
)
3606 return XTENSA_UNDEFINED
;
3608 if (slot
>= xtensa_format_num_slots (isa
, fmt
))
3609 return XTENSA_UNDEFINED
;
3611 xtensa_format_get_slot (isa
, fmt
, slot
, insnbuf
, slotbuf
);
3612 return xtensa_opcode_decode (isa
, fmt
, slot
, slotbuf
);
3616 /* The offset is the offset in the contents.
3617 The address is the address of that offset. */
3620 check_branch_target_aligned (contents
, content_length
, offset
, address
)
3622 bfd_size_type content_length
;
3626 bfd_size_type insn_len
= insn_decode_len (contents
, content_length
, offset
);
3629 return check_branch_target_aligned_address (address
, insn_len
);
3634 check_loop_aligned (contents
, content_length
, offset
, address
)
3636 bfd_size_type content_length
;
3640 bfd_size_type loop_len
, insn_len
;
3641 xtensa_opcode opcode
=
3642 insn_decode_opcode (contents
, content_length
, offset
, 0);
3643 BFD_ASSERT (opcode
!= XTENSA_UNDEFINED
);
3644 if (opcode
!= XTENSA_UNDEFINED
)
3646 BFD_ASSERT (xtensa_opcode_is_loop (xtensa_default_isa
, opcode
));
3647 if (!xtensa_opcode_is_loop (xtensa_default_isa
, opcode
))
3650 loop_len
= insn_decode_len (contents
, content_length
, offset
);
3651 BFD_ASSERT (loop_len
!= 0);
3655 insn_len
= insn_decode_len (contents
, content_length
, offset
+ loop_len
);
3656 BFD_ASSERT (insn_len
!= 0);
3660 return check_branch_target_aligned_address (address
+ loop_len
, insn_len
);
3665 check_branch_target_aligned_address (addr
, len
)
3670 return (addr
% 8 == 0);
3671 return ((addr
>> 2) == ((addr
+ len
- 1) >> 2));
3675 /* Instruction widening and narrowing. */
3677 static bfd_boolean narrow_instruction
3678 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_size_type
, bfd_boolean
));
3679 static bfd_boolean widen_instruction
3680 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_size_type
, bfd_boolean
));
3681 static xtensa_format get_single_format
3682 PARAMS ((xtensa_opcode
));
3683 static void init_op_single_format_table
3694 /* For the set of narrowable instructions we do NOT include the
3695 narrowings beqz -> beqz.n or bnez -> bnez.n because of complexities
3696 involved during linker relaxation that may require these to
3697 re-expand in some conditions. Also, the narrowing "or" -> mov.n
3698 requires special case code to ensure it only works when op1 == op2. */
3700 struct string_pair narrowable
[] =
3703 { "addi", "addi.n" },
3704 { "addmi", "addi.n" },
3705 { "l32i", "l32i.n" },
3706 { "movi", "movi.n" },
3708 { "retw", "retw.n" },
3709 { "s32i", "s32i.n" },
3710 { "or", "mov.n" } /* special case only when op1 == op2 */
3713 struct string_pair widenable
[] =
3716 { "addi", "addi.n" },
3717 { "addmi", "addi.n" },
3718 { "beqz", "beqz.n" },
3719 { "bnez", "bnez.n" },
3720 { "l32i", "l32i.n" },
3721 { "movi", "movi.n" },
3723 { "retw", "retw.n" },
3724 { "s32i", "s32i.n" },
3725 { "or", "mov.n" } /* special case only when op1 == op2 */
3729 /* Attempt to narrow an instruction. Return true if the narrowing is
3730 valid. If the do_it parameter is non-zero, then perform the action
3731 in-place directly into the contents. Otherwise, do not modify the
3732 contents. The set of valid narrowing are specified by a string table
3733 but require some special case operand checks in some cases. */
3736 narrow_instruction (contents
, content_length
, offset
, do_it
)
3738 bfd_size_type content_length
;
3739 bfd_size_type offset
;
3742 xtensa_opcode opcode
;
3743 bfd_size_type insn_len
, opi
;
3744 xtensa_isa isa
= xtensa_default_isa
;
3745 xtensa_format fmt
, o_fmt
;
3747 static xtensa_insnbuf insnbuf
= NULL
;
3748 static xtensa_insnbuf slotbuf
= NULL
;
3749 static xtensa_insnbuf o_insnbuf
= NULL
;
3750 static xtensa_insnbuf o_slotbuf
= NULL
;
3752 if (insnbuf
== NULL
)
3754 insnbuf
= xtensa_insnbuf_alloc (isa
);
3755 slotbuf
= xtensa_insnbuf_alloc (isa
);
3756 o_insnbuf
= xtensa_insnbuf_alloc (isa
);
3757 o_slotbuf
= xtensa_insnbuf_alloc (isa
);
3760 BFD_ASSERT (offset
< content_length
);
3762 if (content_length
< 2)
3765 /* We will hand-code a few of these for a little while.
3766 These have all been specified in the assembler aleady. */
3767 xtensa_insnbuf_from_chars (isa
, insnbuf
, &contents
[offset
],
3768 content_length
- offset
);
3769 fmt
= xtensa_format_decode (isa
, insnbuf
);
3770 if (xtensa_format_num_slots (isa
, fmt
) != 1)
3773 if (xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
) != 0)
3776 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
3777 if (opcode
== XTENSA_UNDEFINED
)
3779 insn_len
= xtensa_format_length (isa
, fmt
);
3780 if (insn_len
> content_length
)
3783 for (opi
= 0; opi
< (sizeof (narrowable
)/sizeof (struct string_pair
)); ++opi
)
3785 bfd_boolean is_or
= (strcmp ("or", narrowable
[opi
].wide
) == 0);
3787 if (opcode
== xtensa_opcode_lookup (isa
, narrowable
[opi
].wide
))
3789 uint32 value
, newval
;
3790 int i
, operand_count
, o_operand_count
;
3791 xtensa_opcode o_opcode
;
3793 /* Address does not matter in this case. We might need to
3794 fix it to handle branches/jumps. */
3795 bfd_vma self_address
= 0;
3797 o_opcode
= xtensa_opcode_lookup (isa
, narrowable
[opi
].narrow
);
3798 if (o_opcode
== XTENSA_UNDEFINED
)
3800 o_fmt
= get_single_format (o_opcode
);
3801 if (o_fmt
== XTENSA_UNDEFINED
)
3804 if (xtensa_format_length (isa
, fmt
) != 3
3805 || xtensa_format_length (isa
, o_fmt
) != 2)
3808 xtensa_format_encode (isa
, o_fmt
, o_slotbuf
);
3809 xtensa_format_encode (isa
, o_fmt
, o_insnbuf
);
3810 operand_count
= xtensa_opcode_num_operands (isa
, opcode
);
3811 o_operand_count
= xtensa_opcode_num_operands (isa
, o_opcode
);
3813 if (xtensa_opcode_encode (isa
, o_fmt
, 0, o_slotbuf
, o_opcode
) != 0)
3818 if (xtensa_opcode_num_operands (isa
, o_opcode
) != operand_count
)
3823 uint32 rawval0
, rawval1
, rawval2
;
3825 if (o_operand_count
+ 1 != operand_count
)
3827 if (xtensa_operand_get_field (isa
, opcode
, 0,
3828 fmt
, 0, slotbuf
, &rawval0
) != 0)
3830 if (xtensa_operand_get_field (isa
, opcode
, 1,
3831 fmt
, 0, slotbuf
, &rawval1
) != 0)
3833 if (xtensa_operand_get_field (isa
, opcode
, 2,
3834 fmt
, 0, slotbuf
, &rawval2
) != 0)
3837 if (rawval1
!= rawval2
)
3839 if (rawval0
== rawval1
) /* it is a nop */
3843 for (i
= 0; i
< o_operand_count
; ++i
)
3845 if (xtensa_operand_get_field (isa
, opcode
, i
, fmt
, 0,
3847 || xtensa_operand_decode (isa
, opcode
, i
, &value
))
3850 /* PC-relative branches need adjustment, but
3851 the PC-rel operand will always have a relocation. */
3853 if (xtensa_operand_do_reloc (isa
, o_opcode
, i
, &newval
,
3855 || xtensa_operand_encode (isa
, o_opcode
, i
, &newval
)
3856 || xtensa_operand_set_field (isa
, o_opcode
, i
, o_fmt
, 0,
3861 if (xtensa_format_set_slot (isa
, o_fmt
, 0,
3862 o_insnbuf
, o_slotbuf
) != 0)
3866 xtensa_insnbuf_to_chars (isa
, o_insnbuf
, contents
+ offset
,
3867 content_length
- offset
);
3875 /* Attempt to widen an instruction. Return true if the widening is
3876 valid. If the do_it parameter is non-zero, then the action should
3877 be performed inplace into the contents. Otherwise, do not modify
3878 the contents. The set of valid widenings are specified by a string
3879 table but require some special case operand checks in some
3883 widen_instruction (contents
, content_length
, offset
, do_it
)
3885 bfd_size_type content_length
;
3886 bfd_size_type offset
;
3889 xtensa_opcode opcode
;
3890 bfd_size_type insn_len
, opi
;
3891 xtensa_isa isa
= xtensa_default_isa
;
3892 xtensa_format fmt
, o_fmt
;
3894 static xtensa_insnbuf insnbuf
= NULL
;
3895 static xtensa_insnbuf slotbuf
= NULL
;
3896 static xtensa_insnbuf o_insnbuf
= NULL
;
3897 static xtensa_insnbuf o_slotbuf
= NULL
;
3899 if (insnbuf
== NULL
)
3901 insnbuf
= xtensa_insnbuf_alloc (isa
);
3902 slotbuf
= xtensa_insnbuf_alloc (isa
);
3903 o_insnbuf
= xtensa_insnbuf_alloc (isa
);
3904 o_slotbuf
= xtensa_insnbuf_alloc (isa
);
3907 BFD_ASSERT (offset
< content_length
);
3909 if (content_length
< 2)
3912 /* We will hand code a few of these for a little while.
3913 These have all been specified in the assembler aleady. */
3914 xtensa_insnbuf_from_chars (isa
, insnbuf
, &contents
[offset
],
3915 content_length
- offset
);
3916 fmt
= xtensa_format_decode (isa
, insnbuf
);
3917 if (xtensa_format_num_slots (isa
, fmt
) != 1)
3920 if (xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
) != 0)
3923 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
3924 if (opcode
== XTENSA_UNDEFINED
)
3926 insn_len
= xtensa_format_length (isa
, fmt
);
3927 if (insn_len
> content_length
)
3930 for (opi
= 0; opi
< (sizeof (widenable
)/sizeof (struct string_pair
)); ++opi
)
3932 bfd_boolean is_or
= (strcmp ("or", widenable
[opi
].wide
) == 0);
3933 bfd_boolean is_branch
= (strcmp ("beqz", widenable
[opi
].wide
) == 0
3934 || strcmp ("bnez", widenable
[opi
].wide
) == 0);
3936 if (opcode
== xtensa_opcode_lookup (isa
, widenable
[opi
].narrow
))
3938 uint32 value
, newval
;
3939 int i
, operand_count
, o_operand_count
, check_operand_count
;
3940 xtensa_opcode o_opcode
;
3942 /* Address does not matter in this case. We might need to fix it
3943 to handle branches/jumps. */
3944 bfd_vma self_address
= 0;
3946 o_opcode
= xtensa_opcode_lookup (isa
, widenable
[opi
].wide
);
3947 if (o_opcode
== XTENSA_UNDEFINED
)
3949 o_fmt
= get_single_format (o_opcode
);
3950 if (o_fmt
== XTENSA_UNDEFINED
)
3953 if (xtensa_format_length (isa
, fmt
) != 2
3954 || xtensa_format_length (isa
, o_fmt
) != 3)
3957 xtensa_format_encode (isa
, o_fmt
, o_slotbuf
);
3958 xtensa_format_encode (isa
, o_fmt
, o_insnbuf
);
3959 operand_count
= xtensa_opcode_num_operands (isa
, opcode
);
3960 o_operand_count
= xtensa_opcode_num_operands (isa
, o_opcode
);
3961 check_operand_count
= o_operand_count
;
3963 if (xtensa_opcode_encode (isa
, o_fmt
, 0, o_slotbuf
, o_opcode
) != 0)
3968 if (xtensa_opcode_num_operands (isa
, o_opcode
) != operand_count
)
3973 uint32 rawval0
, rawval1
;
3975 if (o_operand_count
!= operand_count
+ 1)
3977 if (xtensa_operand_get_field (isa
, opcode
, 0,
3978 fmt
, 0, slotbuf
, &rawval0
) != 0)
3980 if (xtensa_operand_get_field (isa
, opcode
, 1,
3981 fmt
, 0, slotbuf
, &rawval1
) != 0)
3983 if (rawval0
== rawval1
) /* it is a nop */
3987 check_operand_count
--;
3989 for (i
= 0; i
< check_operand_count
; ++i
)
3992 if (is_or
&& i
== o_operand_count
- 1)
3994 if (xtensa_operand_get_field (isa
, opcode
, new_i
, fmt
, 0,
3996 || xtensa_operand_decode (isa
, opcode
, new_i
, &value
))
3999 /* PC-relative branches need adjustment, but
4000 the PC-rel operand will always have a relocation. */
4002 if (xtensa_operand_do_reloc (isa
, o_opcode
, i
, &newval
,
4004 || xtensa_operand_encode (isa
, o_opcode
, i
, &newval
)
4005 || xtensa_operand_set_field (isa
, o_opcode
, i
, o_fmt
, 0,
4010 if (xtensa_format_set_slot (isa
, o_fmt
, 0, o_insnbuf
, o_slotbuf
))
4014 xtensa_insnbuf_to_chars (isa
, o_insnbuf
, contents
+ offset
,
4015 content_length
- offset
);
4023 /* When FLIX is available we need to access certain instructions only
4024 when they are 16-bit or 24-bit instructions. This table caches
4025 information about such instructions by walking through all the
4026 opcodes and finding the smallest single-slot format into which each
4029 static xtensa_format
*op_single_fmt_table
= NULL
;
4032 static xtensa_format
4033 get_single_format (opcode
)
4034 xtensa_opcode opcode
;
4036 init_op_single_format_table ();
4037 return op_single_fmt_table
[opcode
];
4042 init_op_single_format_table ()
4044 xtensa_isa isa
= xtensa_default_isa
;
4045 xtensa_insnbuf ibuf
;
4046 xtensa_opcode opcode
;
4050 if (op_single_fmt_table
!= NULL
)
4053 ibuf
= xtensa_insnbuf_alloc (isa
);
4054 num_opcodes
= xtensa_isa_num_opcodes (isa
);
4056 op_single_fmt_table
= (xtensa_format
*)
4057 bfd_malloc (sizeof (xtensa_format
) * num_opcodes
);
4058 for (opcode
= 0; opcode
< num_opcodes
; opcode
++)
4060 op_single_fmt_table
[opcode
] = XTENSA_UNDEFINED
;
4061 for (fmt
= 0; fmt
< xtensa_isa_num_formats (isa
); fmt
++)
4063 if (xtensa_format_num_slots (isa
, fmt
) == 1
4064 && xtensa_opcode_encode (isa
, fmt
, 0, ibuf
, opcode
) == 0)
4066 xtensa_opcode old_fmt
= op_single_fmt_table
[opcode
];
4067 int fmt_length
= xtensa_format_length (isa
, fmt
);
4068 if (old_fmt
== XTENSA_UNDEFINED
4069 || fmt_length
< xtensa_format_length (isa
, old_fmt
))
4070 op_single_fmt_table
[opcode
] = fmt
;
4074 xtensa_insnbuf_free (isa
, ibuf
);
4078 /* Code for transforming CALLs at link-time. */
4080 static bfd_reloc_status_type
4081 elf_xtensa_do_asm_simplify (contents
, address
, content_length
, error_message
)
4084 bfd_vma content_length
;
4085 char **error_message
;
4087 static xtensa_insnbuf insnbuf
= NULL
;
4088 static xtensa_insnbuf slotbuf
= NULL
;
4089 xtensa_format core_format
= XTENSA_UNDEFINED
;
4090 xtensa_opcode opcode
;
4091 xtensa_opcode direct_call_opcode
;
4092 xtensa_isa isa
= xtensa_default_isa
;
4093 bfd_byte
*chbuf
= contents
+ address
;
4096 if (insnbuf
== NULL
)
4098 insnbuf
= xtensa_insnbuf_alloc (isa
);
4099 slotbuf
= xtensa_insnbuf_alloc (isa
);
4102 if (content_length
< address
)
4104 *error_message
= _("Attempt to convert L32R/CALLX to CALL failed");
4105 return bfd_reloc_other
;
4108 opcode
= get_expanded_call_opcode (chbuf
, content_length
- address
, 0);
4109 direct_call_opcode
= swap_callx_for_call_opcode (opcode
);
4110 if (direct_call_opcode
== XTENSA_UNDEFINED
)
4112 *error_message
= _("Attempt to convert L32R/CALLX to CALL failed");
4113 return bfd_reloc_other
;
4116 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
4117 core_format
= xtensa_format_lookup (isa
, "x24");
4118 opcode
= xtensa_opcode_lookup (isa
, "or");
4119 xtensa_opcode_encode (isa
, core_format
, 0, slotbuf
, opcode
);
4120 for (opn
= 0; opn
< 3; opn
++)
4123 xtensa_operand_encode (isa
, opcode
, opn
, ®no
);
4124 xtensa_operand_set_field (isa
, opcode
, opn
, core_format
, 0,
4127 xtensa_format_encode (isa
, core_format
, insnbuf
);
4128 xtensa_format_set_slot (isa
, core_format
, 0, insnbuf
, slotbuf
);
4129 xtensa_insnbuf_to_chars (isa
, insnbuf
, chbuf
, content_length
- address
);
4131 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
4132 xtensa_opcode_encode (isa
, core_format
, 0, slotbuf
, direct_call_opcode
);
4133 xtensa_operand_set_field (isa
, opcode
, 0, core_format
, 0, slotbuf
, 0);
4135 xtensa_format_encode (isa
, core_format
, insnbuf
);
4136 xtensa_format_set_slot (isa
, core_format
, 0, insnbuf
, slotbuf
);
4137 xtensa_insnbuf_to_chars (isa
, insnbuf
, chbuf
+ 3,
4138 content_length
- address
- 3);
4140 return bfd_reloc_ok
;
4144 static bfd_reloc_status_type
4145 contract_asm_expansion (contents
, content_length
, irel
, error_message
)
4147 bfd_vma content_length
;
4148 Elf_Internal_Rela
*irel
;
4149 char **error_message
;
4151 bfd_reloc_status_type retval
=
4152 elf_xtensa_do_asm_simplify (contents
, irel
->r_offset
, content_length
,
4155 if (retval
!= bfd_reloc_ok
)
4156 return bfd_reloc_dangerous
;
4158 /* Update the irel->r_offset field so that the right immediate and
4159 the right instruction are modified during the relocation. */
4160 irel
->r_offset
+= 3;
4161 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
), R_XTENSA_SLOT0_OP
);
4162 return bfd_reloc_ok
;
4166 static xtensa_opcode
4167 swap_callx_for_call_opcode (opcode
)
4168 xtensa_opcode opcode
;
4170 init_call_opcodes ();
4172 if (opcode
== callx0_op
) return call0_op
;
4173 if (opcode
== callx4_op
) return call4_op
;
4174 if (opcode
== callx8_op
) return call8_op
;
4175 if (opcode
== callx12_op
) return call12_op
;
4177 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
4178 return XTENSA_UNDEFINED
;
4182 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" or "CONST16 aN;
4183 CONST16 aN; CALLX aN" sequence, and if so, return the CALLX opcode.
4184 If not, return XTENSA_UNDEFINED. */
4186 #define L32R_TARGET_REG_OPERAND 0
4187 #define CONST16_TARGET_REG_OPERAND 0
4188 #define CALLN_SOURCE_OPERAND 0
4190 static xtensa_opcode
4191 get_expanded_call_opcode (buf
, bufsize
, p_uses_l32r
)
4194 bfd_boolean
*p_uses_l32r
;
4196 static xtensa_insnbuf insnbuf
= NULL
;
4197 static xtensa_insnbuf slotbuf
= NULL
;
4199 xtensa_opcode opcode
;
4200 xtensa_isa isa
= xtensa_default_isa
;
4201 uint32 regno
, const16_regno
, call_regno
;
4204 if (insnbuf
== NULL
)
4206 insnbuf
= xtensa_insnbuf_alloc (isa
);
4207 slotbuf
= xtensa_insnbuf_alloc (isa
);
4210 xtensa_insnbuf_from_chars (isa
, insnbuf
, buf
, bufsize
);
4211 fmt
= xtensa_format_decode (isa
, insnbuf
);
4212 if (fmt
== XTENSA_UNDEFINED
4213 || xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
))
4214 return XTENSA_UNDEFINED
;
4216 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4217 if (opcode
== XTENSA_UNDEFINED
)
4218 return XTENSA_UNDEFINED
;
4220 if (opcode
== get_l32r_opcode ())
4223 *p_uses_l32r
= TRUE
;
4224 if (xtensa_operand_get_field (isa
, opcode
, L32R_TARGET_REG_OPERAND
,
4225 fmt
, 0, slotbuf
, ®no
)
4226 || xtensa_operand_decode (isa
, opcode
, L32R_TARGET_REG_OPERAND
,
4228 return XTENSA_UNDEFINED
;
4230 else if (opcode
== get_const16_opcode ())
4233 *p_uses_l32r
= FALSE
;
4234 if (xtensa_operand_get_field (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4235 fmt
, 0, slotbuf
, ®no
)
4236 || xtensa_operand_decode (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4238 return XTENSA_UNDEFINED
;
4240 /* Check that the next instruction is also CONST16. */
4241 offset
+= xtensa_format_length (isa
, fmt
);
4242 xtensa_insnbuf_from_chars (isa
, insnbuf
, buf
+ offset
, bufsize
- offset
);
4243 fmt
= xtensa_format_decode (isa
, insnbuf
);
4244 if (fmt
== XTENSA_UNDEFINED
4245 || xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
))
4246 return XTENSA_UNDEFINED
;
4247 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4248 if (opcode
!= get_const16_opcode ())
4249 return XTENSA_UNDEFINED
;
4251 if (xtensa_operand_get_field (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4252 fmt
, 0, slotbuf
, &const16_regno
)
4253 || xtensa_operand_decode (isa
, opcode
, CONST16_TARGET_REG_OPERAND
,
4255 || const16_regno
!= regno
)
4256 return XTENSA_UNDEFINED
;
4259 return XTENSA_UNDEFINED
;
4261 /* Next instruction should be an CALLXn with operand 0 == regno. */
4262 offset
+= xtensa_format_length (isa
, fmt
);
4263 xtensa_insnbuf_from_chars (isa
, insnbuf
, buf
+ offset
, bufsize
- offset
);
4264 fmt
= xtensa_format_decode (isa
, insnbuf
);
4265 if (fmt
== XTENSA_UNDEFINED
4266 || xtensa_format_get_slot (isa
, fmt
, 0, insnbuf
, slotbuf
))
4267 return XTENSA_UNDEFINED
;
4268 opcode
= xtensa_opcode_decode (isa
, fmt
, 0, slotbuf
);
4269 if (opcode
== XTENSA_UNDEFINED
4270 || !is_indirect_call_opcode (opcode
))
4271 return XTENSA_UNDEFINED
;
4273 if (xtensa_operand_get_field (isa
, opcode
, CALLN_SOURCE_OPERAND
,
4274 fmt
, 0, slotbuf
, &call_regno
)
4275 || xtensa_operand_decode (isa
, opcode
, CALLN_SOURCE_OPERAND
,
4277 return XTENSA_UNDEFINED
;
4279 if (call_regno
!= regno
)
4280 return XTENSA_UNDEFINED
;
4286 /* Data structures used during relaxation. */
4288 /* r_reloc: relocation values. */
4290 /* Through the relaxation process, we need to keep track of the values
4291 that will result from evaluating relocations. The standard ELF
4292 relocation structure is not sufficient for this purpose because we're
4293 operating on multiple input files at once, so we need to know which
4294 input file a relocation refers to. The r_reloc structure thus
4295 records both the input file (bfd) and ELF relocation.
4297 For efficiency, an r_reloc also contains a "target_offset" field to
4298 cache the target-section-relative offset value that is represented by
4301 The r_reloc also contains a virtual offset that allows multiple
4302 inserted literals to be placed at the same "address" with
4303 different offsets. */
4305 typedef struct r_reloc_struct r_reloc
;
4307 struct r_reloc_struct
4310 Elf_Internal_Rela rela
;
4311 bfd_vma target_offset
;
4312 bfd_vma virtual_offset
;
4315 static bfd_boolean r_reloc_is_const
4316 PARAMS ((const r_reloc
*));
4317 static void r_reloc_init
4318 PARAMS ((r_reloc
*, bfd
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_size_type
));
4319 static bfd_vma r_reloc_get_target_offset
4320 PARAMS ((const r_reloc
*));
4321 static asection
*r_reloc_get_section
4322 PARAMS ((const r_reloc
*));
4323 static bfd_boolean r_reloc_is_defined
4324 PARAMS ((const r_reloc
*));
4325 static struct elf_link_hash_entry
*r_reloc_get_hash_entry
4326 PARAMS ((const r_reloc
*));
4328 static void print_r_reloc
4329 PARAMS ((FILE *fp
, const r_reloc
*r
));
4333 /* The r_reloc structure is included by value in literal_value, but not
4334 every literal_value has an associated relocation -- some are simple
4335 constants. In such cases, we set all the fields in the r_reloc
4336 struct to zero. The r_reloc_is_const function should be used to
4337 detect this case. */
4340 r_reloc_is_const (r_rel
)
4341 const r_reloc
*r_rel
;
4343 return (r_rel
->abfd
== NULL
);
4348 r_reloc_init (r_rel
, abfd
, irel
, contents
, content_length
)
4351 Elf_Internal_Rela
*irel
;
4353 bfd_size_type content_length
;
4356 reloc_howto_type
*howto
;
4360 r_rel
->rela
= *irel
;
4362 r_rel
->target_offset
= r_reloc_get_target_offset (r_rel
);
4363 r_rel
->virtual_offset
= 0;
4364 r_type
= ELF32_R_TYPE (r_rel
->rela
.r_info
);
4365 howto
= &elf_howto_table
[r_type
];
4366 if (howto
->partial_inplace
)
4368 bfd_vma inplace_val
;
4369 BFD_ASSERT (r_rel
->rela
.r_offset
< content_length
);
4371 inplace_val
= bfd_get_32 (abfd
, &contents
[r_rel
->rela
.r_offset
]);
4372 r_rel
->target_offset
+= inplace_val
;
4376 memset (r_rel
, 0, sizeof (r_reloc
));
4381 r_reloc_get_target_offset (r_rel
)
4382 const r_reloc
*r_rel
;
4384 bfd_vma target_offset
;
4385 unsigned long r_symndx
;
4387 BFD_ASSERT (!r_reloc_is_const (r_rel
));
4388 r_symndx
= ELF32_R_SYM (r_rel
->rela
.r_info
);
4389 target_offset
= get_elf_r_symndx_offset (r_rel
->abfd
, r_symndx
);
4390 return (target_offset
+ r_rel
->rela
.r_addend
);
4394 static struct elf_link_hash_entry
*
4395 r_reloc_get_hash_entry (r_rel
)
4396 const r_reloc
*r_rel
;
4398 unsigned long r_symndx
= ELF32_R_SYM (r_rel
->rela
.r_info
);
4399 return get_elf_r_symndx_hash_entry (r_rel
->abfd
, r_symndx
);
4404 r_reloc_get_section (r_rel
)
4405 const r_reloc
*r_rel
;
4407 unsigned long r_symndx
= ELF32_R_SYM (r_rel
->rela
.r_info
);
4408 return get_elf_r_symndx_section (r_rel
->abfd
, r_symndx
);
4413 r_reloc_is_defined (r_rel
)
4414 const r_reloc
*r_rel
;
4420 sec
= r_reloc_get_section (r_rel
);
4421 if (sec
== bfd_abs_section_ptr
4422 || sec
== bfd_com_section_ptr
4423 || sec
== bfd_und_section_ptr
)
4432 print_r_reloc (fp
, r_rel
)
4434 const r_reloc
*r_rel
;
4436 if (r_reloc_is_defined (r_rel
))
4438 asection
*sec
= r_reloc_get_section (r_rel
);
4439 fprintf (fp
, " %s(%s + ", sec
->owner
->filename
, sec
->name
);
4441 else if (r_reloc_get_hash_entry (r_rel
))
4442 fprintf (fp
, " %s + ", r_reloc_get_hash_entry (r_rel
)->root
.root
.string
);
4444 fprintf (fp
, " ?? + ");
4446 fprintf_vma (fp
, r_rel
->target_offset
);
4447 if (r_rel
->virtual_offset
)
4449 fprintf (fp
, " + ");
4450 fprintf_vma (fp
, r_rel
->virtual_offset
);
4459 /* source_reloc: relocations that reference literals. */
4461 /* To determine whether literals can be coalesced, we need to first
4462 record all the relocations that reference the literals. The
4463 source_reloc structure below is used for this purpose. The
4464 source_reloc entries are kept in a per-literal-section array, sorted
4465 by offset within the literal section (i.e., target offset).
4467 The source_sec and r_rel.rela.r_offset fields identify the source of
4468 the relocation. The r_rel field records the relocation value, i.e.,
4469 the offset of the literal being referenced. The opnd field is needed
4470 to determine the range of the immediate field to which the relocation
4471 applies, so we can determine whether another literal with the same
4472 value is within range. The is_null field is true when the relocation
4473 is being removed (e.g., when an L32R is being removed due to a CALLX
4474 that is converted to a direct CALL). */
4476 typedef struct source_reloc_struct source_reloc
;
4478 struct source_reloc_struct
4480 asection
*source_sec
;
4482 xtensa_opcode opcode
;
4484 bfd_boolean is_null
;
4485 bfd_boolean is_abs_literal
;
4489 static void init_source_reloc
4490 PARAMS ((source_reloc
*, asection
*, const r_reloc
*,
4491 xtensa_opcode
, int, bfd_boolean
));
4492 static source_reloc
*find_source_reloc
4493 PARAMS ((source_reloc
*, int, asection
*, Elf_Internal_Rela
*));
4494 static int source_reloc_compare
4495 PARAMS ((const PTR
, const PTR
));
4499 init_source_reloc (reloc
, source_sec
, r_rel
, opcode
, opnd
, is_abs_literal
)
4500 source_reloc
*reloc
;
4501 asection
*source_sec
;
4502 const r_reloc
*r_rel
;
4503 xtensa_opcode opcode
;
4505 bfd_boolean is_abs_literal
;
4507 reloc
->source_sec
= source_sec
;
4508 reloc
->r_rel
= *r_rel
;
4509 reloc
->opcode
= opcode
;
4511 reloc
->is_null
= FALSE
;
4512 reloc
->is_abs_literal
= is_abs_literal
;
4516 /* Find the source_reloc for a particular source offset and relocation
4517 type. Note that the array is sorted by _target_ offset, so this is
4518 just a linear search. */
4520 static source_reloc
*
4521 find_source_reloc (src_relocs
, src_count
, sec
, irel
)
4522 source_reloc
*src_relocs
;
4525 Elf_Internal_Rela
*irel
;
4529 for (i
= 0; i
< src_count
; i
++)
4531 if (src_relocs
[i
].source_sec
== sec
4532 && src_relocs
[i
].r_rel
.rela
.r_offset
== irel
->r_offset
4533 && (ELF32_R_TYPE (src_relocs
[i
].r_rel
.rela
.r_info
)
4534 == ELF32_R_TYPE (irel
->r_info
)))
4535 return &src_relocs
[i
];
4543 source_reloc_compare (ap
, bp
)
4547 const source_reloc
*a
= (const source_reloc
*) ap
;
4548 const source_reloc
*b
= (const source_reloc
*) bp
;
4550 if (a
->r_rel
.target_offset
!= b
->r_rel
.target_offset
)
4551 return (a
->r_rel
.target_offset
- b
->r_rel
.target_offset
);
4553 /* We don't need to sort on these criteria for correctness,
4554 but enforcing a more strict ordering prevents unstable qsort
4555 from behaving differently with different implementations.
4556 Without the code below we get correct but different results
4557 on Solaris 2.7 and 2.8. We would like to always produce the
4558 same results no matter the host. */
4560 if ((!a
->is_null
) - (!b
->is_null
))
4561 return ((!a
->is_null
) - (!b
->is_null
));
4562 return internal_reloc_compare (&a
->r_rel
.rela
, &b
->r_rel
.rela
);
4566 /* Literal values and value hash tables. */
4568 /* Literals with the same value can be coalesced. The literal_value
4569 structure records the value of a literal: the "r_rel" field holds the
4570 information from the relocation on the literal (if there is one) and
4571 the "value" field holds the contents of the literal word itself.
4573 The value_map structure records a literal value along with the
4574 location of a literal holding that value. The value_map hash table
4575 is indexed by the literal value, so that we can quickly check if a
4576 particular literal value has been seen before and is thus a candidate
4579 typedef struct literal_value_struct literal_value
;
4580 typedef struct value_map_struct value_map
;
4581 typedef struct value_map_hash_table_struct value_map_hash_table
;
4583 struct literal_value_struct
4586 unsigned long value
;
4587 bfd_boolean is_abs_literal
;
4590 struct value_map_struct
4592 literal_value val
; /* The literal value. */
4593 r_reloc loc
; /* Location of the literal. */
4597 struct value_map_hash_table_struct
4599 unsigned bucket_count
;
4600 value_map
**buckets
;
4602 bfd_boolean has_last_loc
;
4607 static void init_literal_value
4608 PARAMS ((literal_value
*, const r_reloc
*, unsigned long, bfd_boolean
));
4609 static bfd_boolean literal_value_equal
4610 PARAMS ((const literal_value
*, const literal_value
*, bfd_boolean
));
4611 static value_map_hash_table
*value_map_hash_table_init
4613 static void value_map_hash_table_delete
4614 PARAMS ((value_map_hash_table
*));
4615 static unsigned literal_value_hash
4616 PARAMS ((const literal_value
*));
4617 static unsigned hash_bfd_vma
4619 static value_map
*value_map_get_cached_value
4620 PARAMS ((value_map_hash_table
*, const literal_value
*, bfd_boolean
));
4621 static value_map
*add_value_map
4622 PARAMS ((value_map_hash_table
*, const literal_value
*, const r_reloc
*,
4627 init_literal_value (lit
, r_rel
, value
, is_abs_literal
)
4629 const r_reloc
*r_rel
;
4630 unsigned long value
;
4631 bfd_boolean is_abs_literal
;
4633 lit
->r_rel
= *r_rel
;
4635 lit
->is_abs_literal
= is_abs_literal
;
4640 literal_value_equal (src1
, src2
, final_static_link
)
4641 const literal_value
*src1
;
4642 const literal_value
*src2
;
4643 bfd_boolean final_static_link
;
4645 struct elf_link_hash_entry
*h1
, *h2
;
4647 if (r_reloc_is_const (&src1
->r_rel
) != r_reloc_is_const (&src2
->r_rel
))
4650 if (r_reloc_is_const (&src1
->r_rel
))
4651 return (src1
->value
== src2
->value
);
4653 if (ELF32_R_TYPE (src1
->r_rel
.rela
.r_info
)
4654 != ELF32_R_TYPE (src2
->r_rel
.rela
.r_info
))
4657 if (src1
->r_rel
.target_offset
!= src2
->r_rel
.target_offset
)
4660 if (src1
->r_rel
.virtual_offset
!= src2
->r_rel
.virtual_offset
)
4663 if (src1
->value
!= src2
->value
)
4666 /* Now check for the same section (if defined) or the same elf_hash
4667 (if undefined or weak). */
4668 h1
= r_reloc_get_hash_entry (&src1
->r_rel
);
4669 h2
= r_reloc_get_hash_entry (&src2
->r_rel
);
4670 if (r_reloc_is_defined (&src1
->r_rel
)
4671 && (final_static_link
4672 || ((!h1
|| h1
->root
.type
!= bfd_link_hash_defweak
)
4673 && (!h2
|| h2
->root
.type
!= bfd_link_hash_defweak
))))
4675 if (r_reloc_get_section (&src1
->r_rel
)
4676 != r_reloc_get_section (&src2
->r_rel
))
4681 /* Require that the hash entries (i.e., symbols) be identical. */
4682 if (h1
!= h2
|| h1
== 0)
4686 if (src1
->is_abs_literal
!= src2
->is_abs_literal
)
4693 /* Must be power of 2. */
4694 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
4696 static value_map_hash_table
*
4697 value_map_hash_table_init ()
4699 value_map_hash_table
*values
;
4701 values
= (value_map_hash_table
*)
4702 bfd_zmalloc (sizeof (value_map_hash_table
));
4703 values
->bucket_count
= INITIAL_HASH_RELOC_BUCKET_COUNT
;
4705 values
->buckets
= (value_map
**)
4706 bfd_zmalloc (sizeof (value_map
*) * values
->bucket_count
);
4707 if (values
->buckets
== NULL
)
4712 values
->has_last_loc
= FALSE
;
4719 value_map_hash_table_delete (table
)
4720 value_map_hash_table
*table
;
4722 free (table
->buckets
);
4731 return (val
>> 2) + (val
>> 10);
4736 literal_value_hash (src
)
4737 const literal_value
*src
;
4741 hash_val
= hash_bfd_vma (src
->value
);
4742 if (!r_reloc_is_const (&src
->r_rel
))
4746 hash_val
+= hash_bfd_vma (src
->is_abs_literal
* 1000);
4747 hash_val
+= hash_bfd_vma (src
->r_rel
.target_offset
);
4748 hash_val
+= hash_bfd_vma (src
->r_rel
.virtual_offset
);
4750 /* Now check for the same section and the same elf_hash. */
4751 if (r_reloc_is_defined (&src
->r_rel
))
4752 sec_or_hash
= r_reloc_get_section (&src
->r_rel
);
4754 sec_or_hash
= r_reloc_get_hash_entry (&src
->r_rel
);
4755 hash_val
+= hash_bfd_vma ((bfd_vma
) (unsigned) sec_or_hash
);
4761 /* Check if the specified literal_value has been seen before. */
4764 value_map_get_cached_value (map
, val
, final_static_link
)
4765 value_map_hash_table
*map
;
4766 const literal_value
*val
;
4767 bfd_boolean final_static_link
;
4773 idx
= literal_value_hash (val
);
4774 idx
= idx
& (map
->bucket_count
- 1);
4775 bucket
= map
->buckets
[idx
];
4776 for (map_e
= bucket
; map_e
; map_e
= map_e
->next
)
4778 if (literal_value_equal (&map_e
->val
, val
, final_static_link
))
4785 /* Record a new literal value. It is illegal to call this if VALUE
4786 already has an entry here. */
4789 add_value_map (map
, val
, loc
, final_static_link
)
4790 value_map_hash_table
*map
;
4791 const literal_value
*val
;
4793 bfd_boolean final_static_link
;
4795 value_map
**bucket_p
;
4798 value_map
*val_e
= (value_map
*) bfd_zmalloc (sizeof (value_map
));
4801 bfd_set_error (bfd_error_no_memory
);
4805 BFD_ASSERT (!value_map_get_cached_value (map
, val
, final_static_link
));
4809 idx
= literal_value_hash (val
);
4810 idx
= idx
& (map
->bucket_count
- 1);
4811 bucket_p
= &map
->buckets
[idx
];
4813 val_e
->next
= *bucket_p
;
4816 /* FIXME: Consider resizing the hash table if we get too many entries. */
4822 /* Lists of text actions (ta_) for narrowing, widening, longcall
4823 conversion, space fill, code & literal removal, etc. */
4825 /* The following text actions are generated:
4827 "ta_remove_insn" remove an instruction or instructions
4828 "ta_remove_longcall" convert longcall to call
4829 "ta_convert_longcall" convert longcall to nop/call
4830 "ta_narrow_insn" narrow a wide instruction
4831 "ta_widen" widen a narrow instruction
4832 "ta_fill" add fill or remove fill
4833 removed < 0 is a fill; branches to the fill address will be
4834 changed to address + fill size (e.g., address - removed)
4835 removed >= 0 branches to the fill address will stay unchanged
4836 "ta_remove_literal" remove a literal; this action is
4837 indicated when a literal is removed
4839 "ta_add_literal" insert a new literal; this action is
4840 indicated when a literal has been moved.
4841 It may use a virtual_offset because
4842 multiple literals can be placed at the
4845 For each of these text actions, we also record the number of bytes
4846 removed by performing the text action. In the case of a "ta_widen"
4847 or a "ta_fill" that adds space, the removed_bytes will be negative. */
4849 typedef struct text_action_struct text_action
;
4850 typedef struct text_action_list_struct text_action_list
;
4851 typedef enum text_action_enum_t text_action_t
;
4853 enum text_action_enum_t
4856 ta_remove_insn
, /* removed = -size */
4857 ta_remove_longcall
, /* removed = -size */
4858 ta_convert_longcall
, /* removed = 0 */
4859 ta_narrow_insn
, /* removed = -1 */
4860 ta_widen_insn
, /* removed = +1 */
4861 ta_fill
, /* removed = +size */
4867 /* Structure for a text action record. */
4868 struct text_action_struct
4870 text_action_t action
;
4871 asection
*sec
; /* Optional */
4873 bfd_vma virtual_offset
; /* Zero except for adding literals. */
4875 literal_value value
; /* Only valid when adding literals. */
4881 /* List of all of the actions taken on a text section. */
4882 struct text_action_list_struct
4888 static text_action
*find_fill_action
4889 PARAMS ((text_action_list
*, asection
*, bfd_vma
));
4890 static int compute_removed_action_diff
4891 PARAMS ((const text_action
*, asection
*, bfd_vma
, int, int));
4892 static void adjust_fill_action
4893 PARAMS ((text_action
*, int));
4894 static void text_action_add
4895 PARAMS ((text_action_list
*, text_action_t
, asection
*, bfd_vma
, int));
4896 static void text_action_add_literal
4897 PARAMS ((text_action_list
*, text_action_t
, const r_reloc
*,
4898 const literal_value
*, int));
4899 static bfd_vma offset_with_removed_text
4900 PARAMS ((text_action_list
*, bfd_vma
));
4901 static bfd_vma offset_with_removed_text_before_fill
4902 PARAMS ((text_action_list
*, bfd_vma
));
4903 static text_action
*find_insn_action
4904 PARAMS ((text_action_list
*, bfd_vma
));
4906 static void print_action_list
4907 PARAMS ((FILE *, text_action_list
*));
4912 find_fill_action (l
, sec
, offset
)
4913 text_action_list
*l
;
4919 /* It is not necessary to fill at the end of a section. */
4920 if (sec
->size
== offset
)
4923 for (m_p
= &l
->head
;
4924 *m_p
!= NULL
&& (*m_p
)->offset
<= offset
;
4925 m_p
= &(*m_p
)->next
)
4927 text_action
*t
= *m_p
;
4928 /* When the action is another fill at the same address,
4929 just increase the size. */
4930 if (t
->offset
== offset
&& t
->action
== ta_fill
)
4938 compute_removed_action_diff (ta
, sec
, offset
, removed
, removable_space
)
4939 const text_action
*ta
;
4943 int removable_space
;
4946 int current_removed
= 0;
4949 current_removed
= ta
->removed_bytes
;
4951 BFD_ASSERT (ta
== NULL
|| ta
->offset
== offset
);
4952 BFD_ASSERT (ta
== NULL
|| ta
->action
== ta_fill
);
4954 /* It is not necessary to fill at the end of a section. Clean this up. */
4955 if (sec
->size
== offset
)
4956 new_removed
= removable_space
- 0;
4960 int added
= -removed
- current_removed
;
4961 /* Ignore multiples of the section alignment. */
4962 added
= ((1 << sec
->alignment_power
) - 1) & added
;
4963 new_removed
= (-added
);
4965 /* Modify for removable. */
4966 space
= removable_space
- new_removed
;
4967 new_removed
= (removable_space
4968 - (((1 << sec
->alignment_power
) - 1) & space
));
4970 return (new_removed
- current_removed
);
4975 adjust_fill_action (ta
, fill_diff
)
4979 ta
->removed_bytes
+= fill_diff
;
4983 /* Add a modification action to the text. For the case of adding or
4984 removing space, modify any current fill and assume that
4985 "unreachable_space" bytes can be freely contracted. Note that a
4986 negative removed value is a fill. */
4989 text_action_add (l
, action
, sec
, offset
, removed
)
4990 text_action_list
*l
;
4991 text_action_t action
;
4999 /* It is not necessary to fill at the end of a section. */
5000 if (action
== ta_fill
&& sec
->size
== offset
)
5003 /* It is not necessary to fill 0 bytes. */
5004 if (action
== ta_fill
&& removed
== 0)
5007 for (m_p
= &l
->head
;
5008 *m_p
!= NULL
&& (*m_p
)->offset
<= offset
;
5009 m_p
= &(*m_p
)->next
)
5011 text_action
*t
= *m_p
;
5012 /* When the action is another fill at the same address,
5013 just increase the size. */
5014 if (t
->offset
== offset
&& t
->action
== ta_fill
&& action
== ta_fill
)
5016 t
->removed_bytes
+= removed
;
5021 /* Create a new record and fill it up. */
5022 ta
= (text_action
*) bfd_zmalloc (sizeof (text_action
));
5023 ta
->action
= action
;
5025 ta
->offset
= offset
;
5026 ta
->removed_bytes
= removed
;
5033 text_action_add_literal (l
, action
, loc
, value
, removed
)
5034 text_action_list
*l
;
5035 text_action_t action
;
5037 const literal_value
*value
;
5042 asection
*sec
= r_reloc_get_section (loc
);
5043 bfd_vma offset
= loc
->target_offset
;
5044 bfd_vma virtual_offset
= loc
->virtual_offset
;
5046 BFD_ASSERT (action
== ta_add_literal
);
5048 for (m_p
= &l
->head
; *m_p
!= NULL
; m_p
= &(*m_p
)->next
)
5050 if ((*m_p
)->offset
> offset
5051 && ((*m_p
)->offset
!= offset
5052 || (*m_p
)->virtual_offset
> virtual_offset
))
5056 /* Create a new record and fill it up. */
5057 ta
= (text_action
*) bfd_zmalloc (sizeof (text_action
));
5058 ta
->action
= action
;
5060 ta
->offset
= offset
;
5061 ta
->virtual_offset
= virtual_offset
;
5063 ta
->removed_bytes
= removed
;
5070 offset_with_removed_text (action_list
, offset
)
5071 text_action_list
*action_list
;
5077 for (r
= action_list
->head
; r
&& r
->offset
<= offset
; r
= r
->next
)
5079 if (r
->offset
< offset
5080 || (r
->action
== ta_fill
&& r
->removed_bytes
< 0))
5081 removed
+= r
->removed_bytes
;
5084 return (offset
- removed
);
5089 offset_with_removed_text_before_fill (action_list
, offset
)
5090 text_action_list
*action_list
;
5096 for (r
= action_list
->head
; r
&& r
->offset
< offset
; r
= r
->next
)
5097 removed
+= r
->removed_bytes
;
5099 return (offset
- removed
);
5103 /* The find_insn_action routine will only find non-fill actions. */
5106 find_insn_action (action_list
, offset
)
5107 text_action_list
*action_list
;
5111 for (t
= action_list
->head
; t
; t
= t
->next
)
5113 if (t
->offset
== offset
)
5120 case ta_remove_insn
:
5121 case ta_remove_longcall
:
5122 case ta_convert_longcall
:
5123 case ta_narrow_insn
:
5126 case ta_remove_literal
:
5127 case ta_add_literal
:
5140 print_action_list (fp
, action_list
)
5142 text_action_list
*action_list
;
5146 fprintf (fp
, "Text Action\n");
5147 for (r
= action_list
->head
; r
!= NULL
; r
= r
->next
)
5149 const char *t
= "unknown";
5152 case ta_remove_insn
:
5153 t
= "remove_insn"; break;
5154 case ta_remove_longcall
:
5155 t
= "remove_longcall"; break;
5156 case ta_convert_longcall
:
5157 t
= "remove_longcall"; break;
5158 case ta_narrow_insn
:
5159 t
= "narrow_insn"; break;
5161 t
= "widen_insn"; break;
5166 case ta_remove_literal
:
5167 t
= "remove_literal"; break;
5168 case ta_add_literal
:
5169 t
= "add_literal"; break;
5172 fprintf (fp
, "%s: %s[0x%lx] \"%s\" %d\n",
5173 r
->sec
->owner
->filename
,
5174 r
->sec
->name
, r
->offset
, t
, r
->removed_bytes
);
5181 /* Lists of literals being coalesced or removed. */
5183 /* In the usual case, the literal identified by "from" is being
5184 coalesced with another literal identified by "to". If the literal is
5185 unused and is being removed altogether, "to.abfd" will be NULL.
5186 The removed_literal entries are kept on a per-section list, sorted
5187 by the "from" offset field. */
5189 typedef struct removed_literal_struct removed_literal
;
5190 typedef struct removed_literal_list_struct removed_literal_list
;
5192 struct removed_literal_struct
5196 removed_literal
*next
;
5199 struct removed_literal_list_struct
5201 removed_literal
*head
;
5202 removed_literal
*tail
;
5206 static void add_removed_literal
5207 PARAMS ((removed_literal_list
*, const r_reloc
*, const r_reloc
*));
5208 static removed_literal
*find_removed_literal
5209 PARAMS ((removed_literal_list
*, bfd_vma
));
5211 static void print_removed_literals
5212 PARAMS ((FILE *, removed_literal_list
*));
5216 /* Record that the literal at "from" is being removed. If "to" is not
5217 NULL, the "from" literal is being coalesced with the "to" literal. */
5220 add_removed_literal (removed_list
, from
, to
)
5221 removed_literal_list
*removed_list
;
5222 const r_reloc
*from
;
5225 removed_literal
*r
, *new_r
, *next_r
;
5227 new_r
= (removed_literal
*) bfd_zmalloc (sizeof (removed_literal
));
5229 new_r
->from
= *from
;
5233 new_r
->to
.abfd
= NULL
;
5236 r
= removed_list
->head
;
5239 removed_list
->head
= new_r
;
5240 removed_list
->tail
= new_r
;
5242 /* Special check for common case of append. */
5243 else if (removed_list
->tail
->from
.target_offset
< from
->target_offset
)
5245 removed_list
->tail
->next
= new_r
;
5246 removed_list
->tail
= new_r
;
5250 while (r
->from
.target_offset
< from
->target_offset
5257 new_r
->next
= next_r
;
5259 removed_list
->tail
= new_r
;
5264 /* Check if the list of removed literals contains an entry for the
5265 given address. Return the entry if found. */
5267 static removed_literal
*
5268 find_removed_literal (removed_list
, addr
)
5269 removed_literal_list
*removed_list
;
5272 removed_literal
*r
= removed_list
->head
;
5273 while (r
&& r
->from
.target_offset
< addr
)
5275 if (r
&& r
->from
.target_offset
== addr
)
5284 print_removed_literals (fp
, removed_list
)
5286 removed_literal_list
*removed_list
;
5289 r
= removed_list
->head
;
5291 fprintf (fp
, "Removed Literals\n");
5292 for (; r
!= NULL
; r
= r
->next
)
5294 print_r_reloc (fp
, &r
->from
);
5295 fprintf (fp
, " => ");
5296 if (r
->to
.abfd
== NULL
)
5297 fprintf (fp
, "REMOVED");
5299 print_r_reloc (fp
, &r
->to
);
5307 /* Per-section data for relaxation. */
5309 typedef struct reloc_bfd_fix_struct reloc_bfd_fix
;
5311 struct xtensa_relax_info_struct
5313 bfd_boolean is_relaxable_literal_section
;
5314 bfd_boolean is_relaxable_asm_section
;
5315 int visited
; /* Number of times visited. */
5317 source_reloc
*src_relocs
; /* Array[src_count]. */
5319 int src_next
; /* Next src_relocs entry to assign. */
5321 removed_literal_list removed_list
;
5322 text_action_list action_list
;
5324 reloc_bfd_fix
*fix_list
;
5325 reloc_bfd_fix
*fix_array
;
5326 unsigned fix_array_count
;
5328 /* Support for expanding the reloc array that is stored
5329 in the section structure. If the relocations have been
5330 reallocated, the newly allocated relocations will be referenced
5331 here along with the actual size allocated. The relocation
5332 count will always be found in the section structure. */
5333 Elf_Internal_Rela
*allocated_relocs
;
5334 unsigned relocs_count
;
5335 unsigned allocated_relocs_count
;
5338 struct elf_xtensa_section_data
5340 struct bfd_elf_section_data elf
;
5341 xtensa_relax_info relax_info
;
5344 static void init_xtensa_relax_info
5345 PARAMS ((asection
*));
5346 static xtensa_relax_info
*get_xtensa_relax_info
5347 PARAMS ((asection
*));
5351 elf_xtensa_new_section_hook (abfd
, sec
)
5355 struct elf_xtensa_section_data
*sdata
;
5356 bfd_size_type amt
= sizeof (*sdata
);
5358 sdata
= (struct elf_xtensa_section_data
*) bfd_zalloc (abfd
, amt
);
5361 sec
->used_by_bfd
= (PTR
) sdata
;
5363 return _bfd_elf_new_section_hook (abfd
, sec
);
5368 init_xtensa_relax_info (sec
)
5371 xtensa_relax_info
*relax_info
= get_xtensa_relax_info (sec
);
5373 relax_info
->is_relaxable_literal_section
= FALSE
;
5374 relax_info
->is_relaxable_asm_section
= FALSE
;
5375 relax_info
->visited
= 0;
5377 relax_info
->src_relocs
= NULL
;
5378 relax_info
->src_count
= 0;
5379 relax_info
->src_next
= 0;
5381 relax_info
->removed_list
.head
= NULL
;
5382 relax_info
->removed_list
.tail
= NULL
;
5384 relax_info
->action_list
.head
= NULL
;
5386 relax_info
->fix_list
= NULL
;
5387 relax_info
->fix_array
= NULL
;
5388 relax_info
->fix_array_count
= 0;
5390 relax_info
->allocated_relocs
= NULL
;
5391 relax_info
->relocs_count
= 0;
5392 relax_info
->allocated_relocs_count
= 0;
5396 static xtensa_relax_info
*
5397 get_xtensa_relax_info (sec
)
5400 struct elf_xtensa_section_data
*section_data
;
5402 /* No info available if no section or if it is an output section. */
5403 if (!sec
|| sec
== sec
->output_section
)
5406 section_data
= (struct elf_xtensa_section_data
*) elf_section_data (sec
);
5407 return §ion_data
->relax_info
;
5411 /* Coalescing literals may require a relocation to refer to a section in
5412 a different input file, but the standard relocation information
5413 cannot express that. Instead, the reloc_bfd_fix structures are used
5414 to "fix" the relocations that refer to sections in other input files.
5415 These structures are kept on per-section lists. The "src_type" field
5416 records the relocation type in case there are multiple relocations on
5417 the same location. FIXME: This is ugly; an alternative might be to
5418 add new symbols with the "owner" field to some other input file. */
5420 struct reloc_bfd_fix_struct
5424 unsigned src_type
; /* Relocation type. */
5427 asection
*target_sec
;
5428 bfd_vma target_offset
;
5429 bfd_boolean translated
;
5431 reloc_bfd_fix
*next
;
5435 static reloc_bfd_fix
*reloc_bfd_fix_init
5436 PARAMS ((asection
*, bfd_vma
, unsigned, bfd
*, asection
*, bfd_vma
,
5439 PARAMS ((asection
*, reloc_bfd_fix
*));
5440 static int fix_compare
5441 PARAMS ((const PTR
, const PTR
));
5442 static void cache_fix_array
5443 PARAMS ((asection
*));
5444 static reloc_bfd_fix
*get_bfd_fix
5445 PARAMS ((asection
*, bfd_vma
, unsigned));
5448 static reloc_bfd_fix
*
5449 reloc_bfd_fix_init (src_sec
, src_offset
, src_type
,
5450 target_abfd
, target_sec
, target_offset
, translated
)
5455 asection
*target_sec
;
5456 bfd_vma target_offset
;
5457 bfd_boolean translated
;
5461 fix
= (reloc_bfd_fix
*) bfd_malloc (sizeof (reloc_bfd_fix
));
5462 fix
->src_sec
= src_sec
;
5463 fix
->src_offset
= src_offset
;
5464 fix
->src_type
= src_type
;
5465 fix
->target_abfd
= target_abfd
;
5466 fix
->target_sec
= target_sec
;
5467 fix
->target_offset
= target_offset
;
5468 fix
->translated
= translated
;
5475 add_fix (src_sec
, fix
)
5479 xtensa_relax_info
*relax_info
;
5481 relax_info
= get_xtensa_relax_info (src_sec
);
5482 fix
->next
= relax_info
->fix_list
;
5483 relax_info
->fix_list
= fix
;
5488 fix_compare (ap
, bp
)
5492 const reloc_bfd_fix
*a
= (const reloc_bfd_fix
*) ap
;
5493 const reloc_bfd_fix
*b
= (const reloc_bfd_fix
*) bp
;
5495 if (a
->src_offset
!= b
->src_offset
)
5496 return (a
->src_offset
- b
->src_offset
);
5497 return (a
->src_type
- b
->src_type
);
5502 cache_fix_array (sec
)
5505 unsigned i
, count
= 0;
5507 xtensa_relax_info
*relax_info
= get_xtensa_relax_info (sec
);
5509 if (relax_info
== NULL
)
5511 if (relax_info
->fix_list
== NULL
)
5514 for (r
= relax_info
->fix_list
; r
!= NULL
; r
= r
->next
)
5517 relax_info
->fix_array
=
5518 (reloc_bfd_fix
*) bfd_malloc (sizeof (reloc_bfd_fix
) * count
);
5519 relax_info
->fix_array_count
= count
;
5521 r
= relax_info
->fix_list
;
5522 for (i
= 0; i
< count
; i
++, r
= r
->next
)
5524 relax_info
->fix_array
[count
- 1 - i
] = *r
;
5525 relax_info
->fix_array
[count
- 1 - i
].next
= NULL
;
5528 qsort (relax_info
->fix_array
, relax_info
->fix_array_count
,
5529 sizeof (reloc_bfd_fix
), fix_compare
);
5533 static reloc_bfd_fix
*
5534 get_bfd_fix (sec
, offset
, type
)
5539 xtensa_relax_info
*relax_info
= get_xtensa_relax_info (sec
);
5543 if (relax_info
== NULL
)
5545 if (relax_info
->fix_list
== NULL
)
5548 if (relax_info
->fix_array
== NULL
)
5549 cache_fix_array (sec
);
5551 key
.src_offset
= offset
;
5552 key
.src_type
= type
;
5553 rv
= bsearch (&key
, relax_info
->fix_array
, relax_info
->fix_array_count
,
5554 sizeof (reloc_bfd_fix
), fix_compare
);
5559 /* Section caching. */
5561 typedef struct section_cache_struct section_cache_t
;
5563 struct section_cache_struct
5567 bfd_byte
*contents
; /* Cache of the section contents. */
5568 bfd_size_type content_length
;
5570 property_table_entry
*ptbl
; /* Cache of the section property table. */
5573 Elf_Internal_Rela
*relocs
; /* Cache of the section relocations. */
5574 unsigned reloc_count
;
5578 static void init_section_cache
5579 PARAMS ((section_cache_t
*));
5580 static bfd_boolean section_cache_section
5581 PARAMS ((section_cache_t
*, asection
*, struct bfd_link_info
*));
5582 static void clear_section_cache
5583 PARAMS ((section_cache_t
*));
5587 init_section_cache (sec_cache
)
5588 section_cache_t
*sec_cache
;
5590 memset (sec_cache
, 0, sizeof (*sec_cache
));
5595 section_cache_section (sec_cache
, sec
, link_info
)
5596 section_cache_t
*sec_cache
;
5598 struct bfd_link_info
*link_info
;
5601 property_table_entry
*prop_table
= NULL
;
5603 bfd_byte
*contents
= NULL
;
5604 Elf_Internal_Rela
*internal_relocs
= NULL
;
5605 bfd_size_type sec_size
;
5609 if (sec
== sec_cache
->sec
)
5613 sec_size
= bfd_get_section_limit (abfd
, sec
);
5615 /* Get the contents. */
5616 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
5617 if (contents
== NULL
&& sec_size
!= 0)
5620 /* Get the relocations. */
5621 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
5622 link_info
->keep_memory
);
5624 /* Get the entry table. */
5625 ptblsize
= xtensa_read_table_entries (abfd
, sec
, &prop_table
,
5626 XTENSA_PROP_SEC_NAME
, FALSE
);
5630 /* Fill in the new section cache. */
5631 clear_section_cache (sec_cache
);
5632 memset (sec_cache
, 0, sizeof (sec_cache
));
5634 sec_cache
->sec
= sec
;
5635 sec_cache
->contents
= contents
;
5636 sec_cache
->content_length
= sec_size
;
5637 sec_cache
->relocs
= internal_relocs
;
5638 sec_cache
->reloc_count
= sec
->reloc_count
;
5639 sec_cache
->pte_count
= ptblsize
;
5640 sec_cache
->ptbl
= prop_table
;
5645 release_contents (sec
, contents
);
5646 release_internal_relocs (sec
, internal_relocs
);
5654 clear_section_cache (sec_cache
)
5655 section_cache_t
*sec_cache
;
5659 release_contents (sec_cache
->sec
, sec_cache
->contents
);
5660 release_internal_relocs (sec_cache
->sec
, sec_cache
->relocs
);
5661 if (sec_cache
->ptbl
)
5662 free (sec_cache
->ptbl
);
5663 memset (sec_cache
, 0, sizeof (sec_cache
));
5668 /* Extended basic blocks. */
5670 /* An ebb_struct represents an Extended Basic Block. Within this
5671 range, we guarantee that all instructions are decodable, the
5672 property table entries are contiguous, and no property table
5673 specifies a segment that cannot have instructions moved. This
5674 structure contains caches of the contents, property table and
5675 relocations for the specified section for easy use. The range is
5676 specified by ranges of indices for the byte offset, property table
5677 offsets and relocation offsets. These must be consistent. */
5679 typedef struct ebb_struct ebb_t
;
5685 bfd_byte
*contents
; /* Cache of the section contents. */
5686 bfd_size_type content_length
;
5688 property_table_entry
*ptbl
; /* Cache of the section property table. */
5691 Elf_Internal_Rela
*relocs
; /* Cache of the section relocations. */
5692 unsigned reloc_count
;
5694 bfd_vma start_offset
; /* Offset in section. */
5695 unsigned start_ptbl_idx
; /* Offset in the property table. */
5696 unsigned start_reloc_idx
; /* Offset in the relocations. */
5699 unsigned end_ptbl_idx
;
5700 unsigned end_reloc_idx
;
5702 bfd_boolean ends_section
; /* Is this the last ebb in a section? */
5704 /* The unreachable property table at the end of this set of blocks;
5705 NULL if the end is not an unreachable block. */
5706 property_table_entry
*ends_unreachable
;
5710 enum ebb_target_enum
5713 EBB_DESIRE_TGT_ALIGN
,
5714 EBB_REQUIRE_TGT_ALIGN
,
5715 EBB_REQUIRE_LOOP_ALIGN
,
5720 /* proposed_action_struct is similar to the text_action_struct except
5721 that is represents a potential transformation, not one that will
5722 occur. We build a list of these for an extended basic block
5723 and use them to compute the actual actions desired. We must be
5724 careful that the entire set of actual actions we perform do not
5725 break any relocations that would fit if the actions were not
5728 typedef struct proposed_action_struct proposed_action
;
5730 struct proposed_action_struct
5732 enum ebb_target_enum align_type
; /* for the target alignment */
5733 bfd_vma alignment_pow
;
5734 text_action_t action
;
5737 bfd_boolean do_action
; /* If false, then we will not perform the action. */
5741 /* The ebb_constraint_struct keeps a set of proposed actions for an
5742 extended basic block. */
5744 typedef struct ebb_constraint_struct ebb_constraint
;
5746 struct ebb_constraint_struct
5749 bfd_boolean start_movable
;
5751 /* Bytes of extra space at the beginning if movable. */
5752 int start_extra_space
;
5754 enum ebb_target_enum start_align
;
5756 bfd_boolean end_movable
;
5758 /* Bytes of extra space at the end if movable. */
5759 int end_extra_space
;
5761 unsigned action_count
;
5762 unsigned action_allocated
;
5764 /* Array of proposed actions. */
5765 proposed_action
*actions
;
5767 /* Action alignments -- one for each proposed action. */
5768 enum ebb_target_enum
*action_aligns
;
5772 static void init_ebb_constraint
5773 PARAMS ((ebb_constraint
*));
5774 static void free_ebb_constraint
5775 PARAMS ((ebb_constraint
*));
5776 static void init_ebb
5777 PARAMS ((ebb_t
*, asection
*, bfd_byte
*, bfd_size_type
,
5778 property_table_entry
*, unsigned, Elf_Internal_Rela
*, unsigned));
5779 static bfd_boolean extend_ebb_bounds
5781 static bfd_boolean extend_ebb_bounds_forward
5783 static bfd_boolean extend_ebb_bounds_backward
5785 static bfd_size_type insn_block_decodable_len
5786 PARAMS ((bfd_byte
*, bfd_size_type
, bfd_vma
, bfd_size_type
));
5787 static void ebb_propose_action
5788 PARAMS ((ebb_constraint
*, enum ebb_target_enum
, bfd_vma
, text_action_t
,
5789 bfd_vma
, int, bfd_boolean
));
5790 static void ebb_add_proposed_action
5791 PARAMS ((ebb_constraint
*, proposed_action
*));
5795 init_ebb_constraint (c
)
5798 memset (c
, 0, sizeof (ebb_constraint
));
5803 free_ebb_constraint (c
)
5806 if (c
->actions
!= NULL
)
5812 init_ebb (ebb
, sec
, contents
, content_length
, prop_table
, ptblsize
,
5813 internal_relocs
, reloc_count
)
5817 bfd_size_type content_length
;
5818 property_table_entry
*prop_table
;
5820 Elf_Internal_Rela
*internal_relocs
;
5821 unsigned reloc_count
;
5823 memset (ebb
, 0, sizeof (ebb_t
));
5825 ebb
->contents
= contents
;
5826 ebb
->content_length
= content_length
;
5827 ebb
->ptbl
= prop_table
;
5828 ebb
->pte_count
= ptblsize
;
5829 ebb
->relocs
= internal_relocs
;
5830 ebb
->reloc_count
= reloc_count
;
5831 ebb
->start_offset
= 0;
5832 ebb
->end_offset
= ebb
->content_length
- 1;
5833 ebb
->start_ptbl_idx
= 0;
5834 ebb
->end_ptbl_idx
= ptblsize
;
5835 ebb
->start_reloc_idx
= 0;
5836 ebb
->end_reloc_idx
= reloc_count
;
5840 /* Extend the ebb to all decodable contiguous sections. The algorithm
5841 for building a basic block around an instruction is to push it
5842 forward until we hit the end of a section, an unreachable block or
5843 a block that cannot be transformed. Then we push it backwards
5844 searching for similar conditions. */
5847 extend_ebb_bounds (ebb
)
5850 if (!extend_ebb_bounds_forward (ebb
))
5852 if (!extend_ebb_bounds_backward (ebb
))
5859 extend_ebb_bounds_forward (ebb
)
5862 property_table_entry
*the_entry
, *new_entry
;
5864 the_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
];
5866 /* Stop when (1) we cannot decode an instruction, (2) we are at
5867 the end of the property tables, (3) we hit a non-contiguous property
5868 table entry, (4) we hit a NO_TRANSFORM region. */
5873 bfd_size_type insn_block_len
;
5875 entry_end
= the_entry
->address
- ebb
->sec
->vma
+ the_entry
->size
;
5877 insn_block_decodable_len (ebb
->contents
, ebb
->content_length
,
5879 entry_end
- ebb
->end_offset
);
5880 if (insn_block_len
!= (entry_end
- ebb
->end_offset
))
5882 (*_bfd_error_handler
)
5883 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
5884 ebb
->sec
->owner
, ebb
->sec
, ebb
->end_offset
+ insn_block_len
);
5887 ebb
->end_offset
+= insn_block_len
;
5889 if (ebb
->end_offset
== ebb
->sec
->size
)
5890 ebb
->ends_section
= TRUE
;
5892 /* Update the reloc counter. */
5893 while (ebb
->end_reloc_idx
+ 1 < ebb
->reloc_count
5894 && (ebb
->relocs
[ebb
->end_reloc_idx
+ 1].r_offset
5897 ebb
->end_reloc_idx
++;
5900 if (ebb
->end_ptbl_idx
+ 1 == ebb
->pte_count
)
5903 new_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
+ 1];
5904 if (((new_entry
->flags
& XTENSA_PROP_INSN
) == 0)
5905 || ((new_entry
->flags
& XTENSA_PROP_INSN_NO_TRANSFORM
) != 0)
5906 || ((the_entry
->flags
& XTENSA_PROP_ALIGN
) != 0))
5909 if (the_entry
->address
+ the_entry
->size
!= new_entry
->address
)
5912 the_entry
= new_entry
;
5913 ebb
->end_ptbl_idx
++;
5916 /* Quick check for an unreachable or end of file just at the end. */
5917 if (ebb
->end_ptbl_idx
+ 1 == ebb
->pte_count
)
5919 if (ebb
->end_offset
== ebb
->content_length
)
5920 ebb
->ends_section
= TRUE
;
5924 new_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
+ 1];
5925 if ((new_entry
->flags
& XTENSA_PROP_UNREACHABLE
) != 0
5926 && the_entry
->address
+ the_entry
->size
== new_entry
->address
)
5927 ebb
->ends_unreachable
= new_entry
;
5930 /* Any other ending requires exact alignment. */
5936 extend_ebb_bounds_backward (ebb
)
5939 property_table_entry
*the_entry
, *new_entry
;
5941 the_entry
= &ebb
->ptbl
[ebb
->start_ptbl_idx
];
5943 /* Stop when (1) we cannot decode the instructions in the current entry.
5944 (2) we are at the beginning of the property tables, (3) we hit a
5945 non-contiguous property table entry, (4) we hit a NO_TRANSFORM region. */
5949 bfd_vma block_begin
;
5950 bfd_size_type insn_block_len
;
5952 block_begin
= the_entry
->address
- ebb
->sec
->vma
;
5954 insn_block_decodable_len (ebb
->contents
, ebb
->content_length
,
5956 ebb
->start_offset
- block_begin
);
5957 if (insn_block_len
!= ebb
->start_offset
- block_begin
)
5959 (*_bfd_error_handler
)
5960 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
5961 ebb
->sec
->owner
, ebb
->sec
, ebb
->end_offset
+ insn_block_len
);
5964 ebb
->start_offset
-= insn_block_len
;
5966 /* Update the reloc counter. */
5967 while (ebb
->start_reloc_idx
> 0
5968 && (ebb
->relocs
[ebb
->start_reloc_idx
- 1].r_offset
5969 >= ebb
->start_offset
))
5971 ebb
->start_reloc_idx
--;
5974 if (ebb
->start_ptbl_idx
== 0)
5977 new_entry
= &ebb
->ptbl
[ebb
->start_ptbl_idx
- 1];
5978 if ((new_entry
->flags
& XTENSA_PROP_INSN
) == 0
5979 || ((new_entry
->flags
& XTENSA_PROP_INSN_NO_TRANSFORM
) != 0)
5980 || ((new_entry
->flags
& XTENSA_PROP_ALIGN
) != 0))
5982 if (new_entry
->address
+ new_entry
->size
!= the_entry
->address
)
5985 the_entry
= new_entry
;
5986 ebb
->start_ptbl_idx
--;
5992 static bfd_size_type
5993 insn_block_decodable_len (contents
, content_len
, block_offset
, block_len
)
5995 bfd_size_type content_len
;
5996 bfd_vma block_offset
;
5997 bfd_size_type block_len
;
5999 bfd_vma offset
= block_offset
;
6001 while (offset
< block_offset
+ block_len
)
6003 bfd_size_type insn_len
= 0;
6005 insn_len
= insn_decode_len (contents
, content_len
, offset
);
6007 return (offset
- block_offset
);
6010 return (offset
- block_offset
);
6015 ebb_propose_action (c
, align_type
, alignment_pow
, action
, offset
,
6016 removed_bytes
, do_action
)
6018 bfd_vma alignment_pow
;
6019 enum ebb_target_enum align_type
;
6020 text_action_t action
;
6023 bfd_boolean do_action
;
6025 proposed_action paction
;
6026 paction
.align_type
= align_type
;
6027 paction
.alignment_pow
= alignment_pow
;
6028 paction
.action
= action
;
6029 paction
.offset
= offset
;
6030 paction
.removed_bytes
= removed_bytes
;
6031 paction
.do_action
= do_action
;
6032 ebb_add_proposed_action (c
, &paction
);
6037 ebb_add_proposed_action (c
, action
)
6039 proposed_action
*action
;
6042 if (c
->action_allocated
<= c
->action_count
)
6044 unsigned new_allocated
= (c
->action_count
+ 2) * 2;
6045 proposed_action
*new_actions
= (proposed_action
*)
6046 bfd_zmalloc (sizeof (proposed_action
) * new_allocated
);
6048 for (i
= 0; i
< c
->action_count
; i
++)
6049 new_actions
[i
] = c
->actions
[i
];
6050 if (c
->actions
!= NULL
)
6052 c
->actions
= new_actions
;
6053 c
->action_allocated
= new_allocated
;
6055 c
->actions
[c
->action_count
] = *action
;
6060 /* Access to internal relocations, section contents and symbols. */
6062 /* During relaxation, we need to modify relocations, section contents,
6063 and symbol definitions, and we need to keep the original values from
6064 being reloaded from the input files, i.e., we need to "pin" the
6065 modified values in memory. We also want to continue to observe the
6066 setting of the "keep-memory" flag. The following functions wrap the
6067 standard BFD functions to take care of this for us. */
6069 static Elf_Internal_Rela
*
6070 retrieve_internal_relocs (abfd
, sec
, keep_memory
)
6073 bfd_boolean keep_memory
;
6075 Elf_Internal_Rela
*internal_relocs
;
6077 if ((sec
->flags
& SEC_LINKER_CREATED
) != 0)
6080 internal_relocs
= elf_section_data (sec
)->relocs
;
6081 if (internal_relocs
== NULL
)
6082 internal_relocs
= (_bfd_elf_link_read_relocs
6083 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
6085 return internal_relocs
;
6090 pin_internal_relocs (sec
, internal_relocs
)
6092 Elf_Internal_Rela
*internal_relocs
;
6094 elf_section_data (sec
)->relocs
= internal_relocs
;
6099 release_internal_relocs (sec
, internal_relocs
)
6101 Elf_Internal_Rela
*internal_relocs
;
6104 && elf_section_data (sec
)->relocs
!= internal_relocs
)
6105 free (internal_relocs
);
6110 retrieve_contents (abfd
, sec
, keep_memory
)
6113 bfd_boolean keep_memory
;
6116 bfd_size_type sec_size
;
6118 sec_size
= bfd_get_section_limit (abfd
, sec
);
6119 contents
= elf_section_data (sec
)->this_hdr
.contents
;
6121 if (contents
== NULL
&& sec_size
!= 0)
6123 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
6125 if (contents
!= NULL
)
6130 elf_section_data (sec
)->this_hdr
.contents
= contents
;
6137 pin_contents (sec
, contents
)
6141 elf_section_data (sec
)->this_hdr
.contents
= contents
;
6146 release_contents (sec
, contents
)
6150 if (contents
&& elf_section_data (sec
)->this_hdr
.contents
!= contents
)
6155 static Elf_Internal_Sym
*
6156 retrieve_local_syms (input_bfd
)
6159 Elf_Internal_Shdr
*symtab_hdr
;
6160 Elf_Internal_Sym
*isymbuf
;
6163 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6164 locsymcount
= symtab_hdr
->sh_info
;
6166 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6167 if (isymbuf
== NULL
&& locsymcount
!= 0)
6168 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6171 /* Save the symbols for this input file so they won't be read again. */
6172 if (isymbuf
&& isymbuf
!= (Elf_Internal_Sym
*) symtab_hdr
->contents
)
6173 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
6179 /* Code for link-time relaxation. */
6181 /* Initialization for relaxation: */
6182 static bfd_boolean analyze_relocations
6183 PARAMS ((struct bfd_link_info
*));
6184 static bfd_boolean find_relaxable_sections
6185 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*));
6186 static bfd_boolean collect_source_relocs
6187 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*));
6188 static bfd_boolean is_resolvable_asm_expansion
6189 PARAMS ((bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*,
6190 struct bfd_link_info
*, bfd_boolean
*));
6191 static Elf_Internal_Rela
*find_associated_l32r_irel
6192 PARAMS ((bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*,
6193 Elf_Internal_Rela
*));
6194 static bfd_boolean compute_text_actions
6195 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*));
6196 static bfd_boolean compute_ebb_proposed_actions
6197 PARAMS ((ebb_constraint
*));
6198 static bfd_boolean compute_ebb_actions
6199 PARAMS ((ebb_constraint
*));
6200 static bfd_boolean check_section_ebb_pcrels_fit
6201 PARAMS ((bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*,
6202 const ebb_constraint
*));
6203 static bfd_boolean check_section_ebb_reduces
6204 PARAMS ((const ebb_constraint
*));
6205 static void text_action_add_proposed
6206 PARAMS ((text_action_list
*, const ebb_constraint
*, asection
*));
6207 static int compute_fill_extra_space
6208 PARAMS ((property_table_entry
*));
6211 static bfd_boolean compute_removed_literals
6212 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, value_map_hash_table
*));
6213 static Elf_Internal_Rela
*get_irel_at_offset
6214 PARAMS ((asection
*, Elf_Internal_Rela
*, bfd_vma
));
6215 static bfd_boolean is_removable_literal
6216 PARAMS ((const source_reloc
*, int, const source_reloc
*, int));
6217 static bfd_boolean remove_dead_literal
6218 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
6219 Elf_Internal_Rela
*, source_reloc
*, property_table_entry
*, int));
6220 static bfd_boolean identify_literal_placement
6221 PARAMS ((bfd
*, asection
*, bfd_byte
*, struct bfd_link_info
*,
6222 value_map_hash_table
*, bfd_boolean
*, Elf_Internal_Rela
*, int,
6223 source_reloc
*, property_table_entry
*, int, section_cache_t
*,
6225 static bfd_boolean relocations_reach
6226 PARAMS ((source_reloc
*, int, const r_reloc
*));
6227 static bfd_boolean coalesce_shared_literal
6228 PARAMS ((asection
*, source_reloc
*, property_table_entry
*, int,
6230 static bfd_boolean move_shared_literal
6231 PARAMS ((asection
*, struct bfd_link_info
*, source_reloc
*,
6232 property_table_entry
*, int, const r_reloc
*,
6233 const literal_value
*, section_cache_t
*));
6236 static bfd_boolean relax_section
6237 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*));
6238 static bfd_boolean translate_section_fixes
6239 PARAMS ((asection
*));
6240 static bfd_boolean translate_reloc_bfd_fix
6241 PARAMS ((reloc_bfd_fix
*));
6242 static void translate_reloc
6243 PARAMS ((const r_reloc
*, r_reloc
*));
6244 static void shrink_dynamic_reloc_sections
6245 PARAMS ((struct bfd_link_info
*, bfd
*, asection
*, Elf_Internal_Rela
*));
6246 static bfd_boolean move_literal
6247 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*, bfd_vma
, bfd_byte
*,
6248 xtensa_relax_info
*, Elf_Internal_Rela
**, const literal_value
*));
6249 static bfd_boolean relax_property_section
6250 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*));
6253 static bfd_boolean relax_section_symbols
6254 PARAMS ((bfd
*, asection
*));
6258 elf_xtensa_relax_section (abfd
, sec
, link_info
, again
)
6261 struct bfd_link_info
*link_info
;
6264 static value_map_hash_table
*values
= NULL
;
6265 static bfd_boolean relocations_analyzed
= FALSE
;
6266 xtensa_relax_info
*relax_info
;
6268 if (!relocations_analyzed
)
6270 /* Do some overall initialization for relaxation. */
6271 values
= value_map_hash_table_init ();
6274 relaxing_section
= TRUE
;
6275 if (!analyze_relocations (link_info
))
6277 relocations_analyzed
= TRUE
;
6281 /* Don't mess with linker-created sections. */
6282 if ((sec
->flags
& SEC_LINKER_CREATED
) != 0)
6285 relax_info
= get_xtensa_relax_info (sec
);
6286 BFD_ASSERT (relax_info
!= NULL
);
6288 switch (relax_info
->visited
)
6291 /* Note: It would be nice to fold this pass into
6292 analyze_relocations, but it is important for this step that the
6293 sections be examined in link order. */
6294 if (!compute_removed_literals (abfd
, sec
, link_info
, values
))
6301 value_map_hash_table_delete (values
);
6303 if (!relax_section (abfd
, sec
, link_info
))
6309 if (!relax_section_symbols (abfd
, sec
))
6314 relax_info
->visited
++;
6319 /* Initialization for relaxation. */
6321 /* This function is called once at the start of relaxation. It scans
6322 all the input sections and marks the ones that are relaxable (i.e.,
6323 literal sections with L32R relocations against them), and then
6324 collects source_reloc information for all the relocations against
6325 those relaxable sections. During this process, it also detects
6326 longcalls, i.e., calls relaxed by the assembler into indirect
6327 calls, that can be optimized back into direct calls. Within each
6328 extended basic block (ebb) containing an optimized longcall, it
6329 computes a set of "text actions" that can be performed to remove
6330 the L32R associated with the longcall while optionally preserving
6331 branch target alignments. */
6334 analyze_relocations (link_info
)
6335 struct bfd_link_info
*link_info
;
6339 bfd_boolean is_relaxable
= FALSE
;
6341 /* Initialize the per-section relaxation info. */
6342 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6343 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6345 init_xtensa_relax_info (sec
);
6348 /* Mark relaxable sections (and count relocations against each one). */
6349 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6350 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6352 if (!find_relaxable_sections (abfd
, sec
, link_info
, &is_relaxable
))
6356 /* Bail out if there are no relaxable sections. */
6360 /* Allocate space for source_relocs. */
6361 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6362 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6364 xtensa_relax_info
*relax_info
;
6366 relax_info
= get_xtensa_relax_info (sec
);
6367 if (relax_info
->is_relaxable_literal_section
6368 || relax_info
->is_relaxable_asm_section
)
6370 relax_info
->src_relocs
= (source_reloc
*)
6371 bfd_malloc (relax_info
->src_count
* sizeof (source_reloc
));
6375 /* Collect info on relocations against each relaxable section. */
6376 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6377 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6379 if (!collect_source_relocs (abfd
, sec
, link_info
))
6383 /* Compute the text actions. */
6384 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
6385 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6387 if (!compute_text_actions (abfd
, sec
, link_info
))
6395 /* Find all the sections that might be relaxed. The motivation for
6396 this pass is that collect_source_relocs() needs to record _all_ the
6397 relocations that target each relaxable section. That is expensive
6398 and unnecessary unless the target section is actually going to be
6399 relaxed. This pass identifies all such sections by checking if
6400 they have L32Rs pointing to them. In the process, the total number
6401 of relocations targeting each section is also counted so that we
6402 know how much space to allocate for source_relocs against each
6403 relaxable literal section. */
6406 find_relaxable_sections (abfd
, sec
, link_info
, is_relaxable_p
)
6409 struct bfd_link_info
*link_info
;
6410 bfd_boolean
*is_relaxable_p
;
6412 Elf_Internal_Rela
*internal_relocs
;
6414 bfd_boolean ok
= TRUE
;
6416 xtensa_relax_info
*source_relax_info
;
6418 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
6419 link_info
->keep_memory
);
6420 if (internal_relocs
== NULL
)
6423 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
6424 if (contents
== NULL
&& sec
->size
!= 0)
6430 source_relax_info
= get_xtensa_relax_info (sec
);
6431 for (i
= 0; i
< sec
->reloc_count
; i
++)
6433 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6435 asection
*target_sec
;
6436 xtensa_relax_info
*target_relax_info
;
6438 /* If this section has not already been marked as "relaxable", and
6439 if it contains any ASM_EXPAND relocations (marking expanded
6440 longcalls) that can be optimized into direct calls, then mark
6441 the section as "relaxable". */
6442 if (source_relax_info
6443 && !source_relax_info
->is_relaxable_asm_section
6444 && ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_ASM_EXPAND
)
6446 bfd_boolean is_reachable
= FALSE
;
6447 if (is_resolvable_asm_expansion (abfd
, sec
, contents
, irel
,
6448 link_info
, &is_reachable
)
6451 source_relax_info
->is_relaxable_asm_section
= TRUE
;
6452 *is_relaxable_p
= TRUE
;
6456 r_reloc_init (&r_rel
, abfd
, irel
, contents
,
6457 bfd_get_section_limit (abfd
, sec
));
6459 target_sec
= r_reloc_get_section (&r_rel
);
6460 target_relax_info
= get_xtensa_relax_info (target_sec
);
6461 if (!target_relax_info
)
6464 /* Count PC-relative operand relocations against the target section.
6465 Note: The conditions tested here must match the conditions under
6466 which init_source_reloc is called in collect_source_relocs(). */
6467 if (is_operand_relocation (ELF32_R_TYPE (irel
->r_info
))
6468 && (!is_alt_relocation (ELF32_R_TYPE (irel
->r_info
))
6469 || is_l32r_relocation (abfd
, sec
, contents
, irel
)))
6470 target_relax_info
->src_count
++;
6472 if (is_l32r_relocation (abfd
, sec
, contents
, irel
)
6473 && r_reloc_is_defined (&r_rel
))
6475 /* Mark the target section as relaxable. */
6476 target_relax_info
->is_relaxable_literal_section
= TRUE
;
6477 *is_relaxable_p
= TRUE
;
6482 release_contents (sec
, contents
);
6483 release_internal_relocs (sec
, internal_relocs
);
6488 /* Record _all_ the relocations that point to relaxable sections, and
6489 get rid of ASM_EXPAND relocs by either converting them to
6490 ASM_SIMPLIFY or by removing them. */
6493 collect_source_relocs (abfd
, sec
, link_info
)
6496 struct bfd_link_info
*link_info
;
6498 Elf_Internal_Rela
*internal_relocs
;
6500 bfd_boolean ok
= TRUE
;
6502 bfd_size_type sec_size
;
6504 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
6505 link_info
->keep_memory
);
6506 if (internal_relocs
== NULL
)
6509 sec_size
= bfd_get_section_limit (abfd
, sec
);
6510 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
6511 if (contents
== NULL
&& sec_size
!= 0)
6517 /* Record relocations against relaxable literal sections. */
6518 for (i
= 0; i
< sec
->reloc_count
; i
++)
6520 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6522 asection
*target_sec
;
6523 xtensa_relax_info
*target_relax_info
;
6525 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
6527 target_sec
= r_reloc_get_section (&r_rel
);
6528 target_relax_info
= get_xtensa_relax_info (target_sec
);
6530 if (target_relax_info
6531 && (target_relax_info
->is_relaxable_literal_section
6532 || target_relax_info
->is_relaxable_asm_section
))
6534 xtensa_opcode opcode
= XTENSA_UNDEFINED
;
6536 bfd_boolean is_abs_literal
= FALSE
;
6538 if (is_alt_relocation (ELF32_R_TYPE (irel
->r_info
)))
6540 /* None of the current alternate relocs are PC-relative,
6541 and only PC-relative relocs matter here. However, we
6542 still need to record the opcode for literal
6544 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
6545 if (opcode
== get_l32r_opcode ())
6547 is_abs_literal
= TRUE
;
6551 opcode
= XTENSA_UNDEFINED
;
6553 else if (is_operand_relocation (ELF32_R_TYPE (irel
->r_info
)))
6555 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
6556 opnd
= get_relocation_opnd (opcode
, ELF32_R_TYPE (irel
->r_info
));
6559 if (opcode
!= XTENSA_UNDEFINED
)
6561 int src_next
= target_relax_info
->src_next
++;
6562 source_reloc
*s_reloc
= &target_relax_info
->src_relocs
[src_next
];
6564 init_source_reloc (s_reloc
, sec
, &r_rel
, opcode
, opnd
,
6570 /* Now get rid of ASM_EXPAND relocations. At this point, the
6571 src_relocs array for the target literal section may still be
6572 incomplete, but it must at least contain the entries for the L32R
6573 relocations associated with ASM_EXPANDs because they were just
6574 added in the preceding loop over the relocations. */
6576 for (i
= 0; i
< sec
->reloc_count
; i
++)
6578 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6579 bfd_boolean is_reachable
;
6581 if (!is_resolvable_asm_expansion (abfd
, sec
, contents
, irel
, link_info
,
6587 Elf_Internal_Rela
*l32r_irel
;
6589 asection
*target_sec
;
6590 xtensa_relax_info
*target_relax_info
;
6592 /* Mark the source_reloc for the L32R so that it will be
6593 removed in compute_removed_literals(), along with the
6594 associated literal. */
6595 l32r_irel
= find_associated_l32r_irel (abfd
, sec
, contents
,
6596 irel
, internal_relocs
);
6597 if (l32r_irel
== NULL
)
6600 r_reloc_init (&r_rel
, abfd
, l32r_irel
, contents
, sec_size
);
6602 target_sec
= r_reloc_get_section (&r_rel
);
6603 target_relax_info
= get_xtensa_relax_info (target_sec
);
6605 if (target_relax_info
6606 && (target_relax_info
->is_relaxable_literal_section
6607 || target_relax_info
->is_relaxable_asm_section
))
6609 source_reloc
*s_reloc
;
6611 /* Search the source_relocs for the entry corresponding to
6612 the l32r_irel. Note: The src_relocs array is not yet
6613 sorted, but it wouldn't matter anyway because we're
6614 searching by source offset instead of target offset. */
6615 s_reloc
= find_source_reloc (target_relax_info
->src_relocs
,
6616 target_relax_info
->src_next
,
6618 BFD_ASSERT (s_reloc
);
6619 s_reloc
->is_null
= TRUE
;
6622 /* Convert this reloc to ASM_SIMPLIFY. */
6623 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
6624 R_XTENSA_ASM_SIMPLIFY
);
6625 l32r_irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
6627 pin_internal_relocs (sec
, internal_relocs
);
6631 /* It is resolvable but doesn't reach. We resolve now
6632 by eliminating the relocation -- the call will remain
6633 expanded into L32R/CALLX. */
6634 irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
6635 pin_internal_relocs (sec
, internal_relocs
);
6640 release_contents (sec
, contents
);
6641 release_internal_relocs (sec
, internal_relocs
);
6646 /* Return TRUE if the asm expansion can be resolved. Generally it can
6647 be resolved on a final link or when a partial link locates it in the
6648 same section as the target. Set "is_reachable" flag if the target of
6649 the call is within the range of a direct call, given the current VMA
6650 for this section and the target section. */
6653 is_resolvable_asm_expansion (abfd
, sec
, contents
, irel
, link_info
,
6658 Elf_Internal_Rela
*irel
;
6659 struct bfd_link_info
*link_info
;
6660 bfd_boolean
*is_reachable_p
;
6662 asection
*target_sec
;
6663 bfd_vma target_offset
;
6665 xtensa_opcode opcode
, direct_call_opcode
;
6666 bfd_vma self_address
;
6667 bfd_vma dest_address
;
6668 bfd_boolean uses_l32r
;
6669 bfd_size_type sec_size
;
6671 *is_reachable_p
= FALSE
;
6673 if (contents
== NULL
)
6676 if (ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_ASM_EXPAND
)
6679 sec_size
= bfd_get_section_limit (abfd
, sec
);
6680 opcode
= get_expanded_call_opcode (contents
+ irel
->r_offset
,
6681 sec_size
- irel
->r_offset
, &uses_l32r
);
6682 /* Optimization of longcalls that use CONST16 is not yet implemented. */
6686 direct_call_opcode
= swap_callx_for_call_opcode (opcode
);
6687 if (direct_call_opcode
== XTENSA_UNDEFINED
)
6690 /* Check and see that the target resolves. */
6691 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
6692 if (!r_reloc_is_defined (&r_rel
))
6695 target_sec
= r_reloc_get_section (&r_rel
);
6696 target_offset
= r_rel
.target_offset
;
6698 /* If the target is in a shared library, then it doesn't reach. This
6699 isn't supposed to come up because the compiler should never generate
6700 non-PIC calls on systems that use shared libraries, but the linker
6701 shouldn't crash regardless. */
6702 if (!target_sec
->output_section
)
6705 /* For relocatable sections, we can only simplify when the output
6706 section of the target is the same as the output section of the
6708 if (link_info
->relocatable
6709 && (target_sec
->output_section
!= sec
->output_section
6710 || is_reloc_sym_weak (abfd
, irel
)))
6713 self_address
= (sec
->output_section
->vma
6714 + sec
->output_offset
+ irel
->r_offset
+ 3);
6715 dest_address
= (target_sec
->output_section
->vma
6716 + target_sec
->output_offset
+ target_offset
);
6718 *is_reachable_p
= pcrel_reloc_fits (direct_call_opcode
, 0,
6719 self_address
, dest_address
);
6721 if ((self_address
>> CALL_SEGMENT_BITS
) !=
6722 (dest_address
>> CALL_SEGMENT_BITS
))
6729 static Elf_Internal_Rela
*
6730 find_associated_l32r_irel (abfd
, sec
, contents
, other_irel
, internal_relocs
)
6734 Elf_Internal_Rela
*other_irel
;
6735 Elf_Internal_Rela
*internal_relocs
;
6739 for (i
= 0; i
< sec
->reloc_count
; i
++)
6741 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6743 if (irel
== other_irel
)
6745 if (irel
->r_offset
!= other_irel
->r_offset
)
6747 if (is_l32r_relocation (abfd
, sec
, contents
, irel
))
6755 /* The compute_text_actions function will build a list of potential
6756 transformation actions for code in the extended basic block of each
6757 longcall that is optimized to a direct call. From this list we
6758 generate a set of actions to actually perform that optimizes for
6759 space and, if not using size_opt, maintains branch target
6762 These actions to be performed are placed on a per-section list.
6763 The actual changes are performed by relax_section() in the second
6767 compute_text_actions (abfd
, sec
, link_info
)
6770 struct bfd_link_info
*link_info
;
6772 xtensa_relax_info
*relax_info
;
6774 Elf_Internal_Rela
*internal_relocs
;
6775 bfd_boolean ok
= TRUE
;
6777 property_table_entry
*prop_table
= 0;
6779 bfd_size_type sec_size
;
6780 static bfd_boolean no_insn_move
= FALSE
;
6785 /* Do nothing if the section contains no optimized longcalls. */
6786 relax_info
= get_xtensa_relax_info (sec
);
6787 BFD_ASSERT (relax_info
);
6788 if (!relax_info
->is_relaxable_asm_section
)
6791 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
6792 link_info
->keep_memory
);
6794 if (internal_relocs
)
6795 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
6796 internal_reloc_compare
);
6798 sec_size
= bfd_get_section_limit (abfd
, sec
);
6799 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
6800 if (contents
== NULL
&& sec_size
!= 0)
6806 ptblsize
= xtensa_read_table_entries (abfd
, sec
, &prop_table
,
6807 XTENSA_PROP_SEC_NAME
, FALSE
);
6814 for (i
= 0; i
< sec
->reloc_count
; i
++)
6816 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
6818 property_table_entry
*the_entry
;
6821 ebb_constraint ebb_table
;
6822 bfd_size_type simplify_size
;
6824 if (irel
&& ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_ASM_SIMPLIFY
)
6826 r_offset
= irel
->r_offset
;
6828 simplify_size
= get_asm_simplify_size (contents
, sec_size
, r_offset
);
6829 if (simplify_size
== 0)
6831 (*_bfd_error_handler
)
6832 (_("%B(%A+0x%lx): could not decode instruction for XTENSA_ASM_SIMPLIFY relocation; possible configuration mismatch"),
6833 sec
->owner
, sec
, r_offset
);
6837 /* If the instruction table is not around, then don't do this
6839 the_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
6840 sec
->vma
+ irel
->r_offset
);
6841 if (the_entry
== NULL
|| XTENSA_NO_NOP_REMOVAL
)
6843 text_action_add (&relax_info
->action_list
,
6844 ta_convert_longcall
, sec
, r_offset
,
6849 /* If the next longcall happens to be at the same address as an
6850 unreachable section of size 0, then skip forward. */
6851 ptbl_idx
= the_entry
- prop_table
;
6852 while ((the_entry
->flags
& XTENSA_PROP_UNREACHABLE
)
6853 && the_entry
->size
== 0
6854 && ptbl_idx
+ 1 < ptblsize
6855 && (prop_table
[ptbl_idx
+ 1].address
6856 == prop_table
[ptbl_idx
].address
))
6862 if (the_entry
->flags
& XTENSA_PROP_INSN_NO_TRANSFORM
)
6863 /* NO_REORDER is OK */
6866 init_ebb_constraint (&ebb_table
);
6867 ebb
= &ebb_table
.ebb
;
6868 init_ebb (ebb
, sec
, contents
, sec_size
, prop_table
, ptblsize
,
6869 internal_relocs
, sec
->reloc_count
);
6870 ebb
->start_offset
= r_offset
+ simplify_size
;
6871 ebb
->end_offset
= r_offset
+ simplify_size
;
6872 ebb
->start_ptbl_idx
= ptbl_idx
;
6873 ebb
->end_ptbl_idx
= ptbl_idx
;
6874 ebb
->start_reloc_idx
= i
;
6875 ebb
->end_reloc_idx
= i
;
6877 if (!extend_ebb_bounds (ebb
)
6878 || !compute_ebb_proposed_actions (&ebb_table
)
6879 || !compute_ebb_actions (&ebb_table
)
6880 || !check_section_ebb_pcrels_fit (abfd
, sec
, contents
,
6881 internal_relocs
, &ebb_table
)
6882 || !check_section_ebb_reduces (&ebb_table
))
6884 /* If anything goes wrong or we get unlucky and something does
6885 not fit, with our plan because of expansion between
6886 critical branches, just convert to a NOP. */
6888 text_action_add (&relax_info
->action_list
,
6889 ta_convert_longcall
, sec
, r_offset
, 0);
6890 i
= ebb_table
.ebb
.end_reloc_idx
;
6891 free_ebb_constraint (&ebb_table
);
6895 text_action_add_proposed (&relax_info
->action_list
, &ebb_table
, sec
);
6897 /* Update the index so we do not go looking at the relocations
6898 we have already processed. */
6899 i
= ebb_table
.ebb
.end_reloc_idx
;
6900 free_ebb_constraint (&ebb_table
);
6904 if (relax_info
->action_list
.head
!= NULL
)
6905 print_action_list (stderr
, &relax_info
->action_list
);
6909 release_contents (sec
, contents
);
6910 release_internal_relocs (sec
, internal_relocs
);
6918 /* Find all of the possible actions for an extended basic block. */
6921 compute_ebb_proposed_actions (ebb_table
)
6922 ebb_constraint
*ebb_table
;
6924 const ebb_t
*ebb
= &ebb_table
->ebb
;
6925 unsigned rel_idx
= ebb
->start_reloc_idx
;
6926 property_table_entry
*entry
, *start_entry
, *end_entry
;
6928 start_entry
= &ebb
->ptbl
[ebb
->start_ptbl_idx
];
6929 end_entry
= &ebb
->ptbl
[ebb
->end_ptbl_idx
];
6931 for (entry
= start_entry
; entry
<= end_entry
; entry
++)
6933 bfd_vma offset
, start_offset
, end_offset
;
6934 bfd_size_type insn_len
;
6936 start_offset
= entry
->address
- ebb
->sec
->vma
;
6937 end_offset
= entry
->address
+ entry
->size
- ebb
->sec
->vma
;
6939 if (entry
== start_entry
)
6940 start_offset
= ebb
->start_offset
;
6941 if (entry
== end_entry
)
6942 end_offset
= ebb
->end_offset
;
6943 offset
= start_offset
;
6945 if (offset
== entry
->address
- ebb
->sec
->vma
6946 && (entry
->flags
& XTENSA_PROP_INSN_BRANCH_TARGET
) != 0)
6948 enum ebb_target_enum align_type
= EBB_DESIRE_TGT_ALIGN
;
6949 BFD_ASSERT (offset
!= end_offset
);
6950 if (offset
== end_offset
)
6953 insn_len
= insn_decode_len (ebb
->contents
, ebb
->content_length
,
6956 /* Propose no actions for a section with an undecodable offset. */
6959 (*_bfd_error_handler
)
6960 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6961 ebb
->sec
->owner
, ebb
->sec
, offset
);
6964 if (check_branch_target_aligned_address (offset
, insn_len
))
6965 align_type
= EBB_REQUIRE_TGT_ALIGN
;
6967 ebb_propose_action (ebb_table
, align_type
, 0,
6968 ta_none
, offset
, 0, TRUE
);
6971 while (offset
!= end_offset
)
6973 Elf_Internal_Rela
*irel
;
6974 xtensa_opcode opcode
;
6976 while (rel_idx
< ebb
->end_reloc_idx
6977 && (ebb
->relocs
[rel_idx
].r_offset
< offset
6978 || (ebb
->relocs
[rel_idx
].r_offset
== offset
6979 && (ELF32_R_TYPE (ebb
->relocs
[rel_idx
].r_info
)
6980 != R_XTENSA_ASM_SIMPLIFY
))))
6983 /* Check for longcall. */
6984 irel
= &ebb
->relocs
[rel_idx
];
6985 if (irel
->r_offset
== offset
6986 && ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_ASM_SIMPLIFY
)
6988 bfd_size_type simplify_size
;
6990 simplify_size
= get_asm_simplify_size (ebb
->contents
,
6991 ebb
->content_length
,
6993 if (simplify_size
== 0)
6995 (*_bfd_error_handler
)
6996 (_("%B(%A+0x%lx): could not decode instruction for XTENSA_ASM_SIMPLIFY relocation; possible configuration mismatch"),
6997 ebb
->sec
->owner
, ebb
->sec
, offset
);
7001 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7002 ta_convert_longcall
, offset
, 0, TRUE
);
7004 offset
+= simplify_size
;
7008 insn_len
= insn_decode_len (ebb
->contents
, ebb
->content_length
,
7010 /* If the instruction is undecodable, then report an error. */
7013 (*_bfd_error_handler
)
7014 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
7015 ebb
->sec
->owner
, ebb
->sec
, offset
);
7019 if ((entry
->flags
& XTENSA_PROP_INSN_NO_DENSITY
) == 0
7020 && (entry
->flags
& XTENSA_PROP_INSN_NO_TRANSFORM
) == 0
7021 && narrow_instruction (ebb
->contents
, ebb
->content_length
,
7024 /* Add an instruction narrow action. */
7025 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7026 ta_narrow_insn
, offset
, 0, FALSE
);
7030 if ((entry
->flags
& XTENSA_PROP_INSN_NO_TRANSFORM
) == 0
7031 && widen_instruction (ebb
->contents
, ebb
->content_length
,
7034 /* Add an instruction widen action. */
7035 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7036 ta_widen_insn
, offset
, 0, FALSE
);
7040 opcode
= insn_decode_opcode (ebb
->contents
, ebb
->content_length
,
7042 if (xtensa_opcode_is_loop (xtensa_default_isa
, opcode
))
7044 /* Check for branch targets. */
7045 ebb_propose_action (ebb_table
, EBB_REQUIRE_LOOP_ALIGN
, 0,
7046 ta_none
, offset
, 0, TRUE
);
7055 if (ebb
->ends_unreachable
)
7057 ebb_propose_action (ebb_table
, EBB_NO_ALIGN
, 0,
7058 ta_fill
, ebb
->end_offset
, 0, TRUE
);
7065 /* After all of the information has collected about the
7066 transformations possible in an EBB, compute the appropriate actions
7067 here in compute_ebb_actions. We still must check later to make
7068 sure that the actions do not break any relocations. The algorithm
7069 used here is pretty greedy. Basically, it removes as many no-ops
7070 as possible so that the end of the EBB has the same alignment
7071 characteristics as the original. First, it uses narrowing, then
7072 fill space at the end of the EBB, and finally widenings. If that
7073 does not work, it tries again with one fewer no-op removed. The
7074 optimization will only be performed if all of the branch targets
7075 that were aligned before transformation are also aligned after the
7078 When the size_opt flag is set, ignore the branch target alignments,
7079 narrow all wide instructions, and remove all no-ops unless the end
7080 of the EBB prevents it. */
7083 compute_ebb_actions (ebb_table
)
7084 ebb_constraint
*ebb_table
;
7088 int removed_bytes
= 0;
7089 ebb_t
*ebb
= &ebb_table
->ebb
;
7090 unsigned seg_idx_start
= 0;
7091 unsigned seg_idx_end
= 0;
7093 /* We perform this like the assembler relaxation algorithm: Start by
7094 assuming all instructions are narrow and all no-ops removed; then
7097 /* For each segment of this that has a solid constraint, check to
7098 see if there are any combinations that will keep the constraint.
7100 for (seg_idx_end
= 0; seg_idx_end
< ebb_table
->action_count
; seg_idx_end
++)
7102 bfd_boolean requires_text_end_align
= FALSE
;
7103 unsigned longcall_count
= 0;
7104 unsigned longcall_convert_count
= 0;
7105 unsigned narrowable_count
= 0;
7106 unsigned narrowable_convert_count
= 0;
7107 unsigned widenable_count
= 0;
7108 unsigned widenable_convert_count
= 0;
7110 proposed_action
*action
= NULL
;
7111 int align
= (1 << ebb_table
->ebb
.sec
->alignment_power
);
7113 seg_idx_start
= seg_idx_end
;
7115 for (i
= seg_idx_start
; i
< ebb_table
->action_count
; i
++)
7117 action
= &ebb_table
->actions
[i
];
7118 if (action
->action
== ta_convert_longcall
)
7120 if (action
->action
== ta_narrow_insn
)
7122 if (action
->action
== ta_widen_insn
)
7124 if (action
->action
== ta_fill
)
7126 if (action
->align_type
== EBB_REQUIRE_LOOP_ALIGN
)
7128 if (action
->align_type
== EBB_REQUIRE_TGT_ALIGN
7129 && !elf32xtensa_size_opt
)
7134 if (seg_idx_end
== ebb_table
->action_count
&& !ebb
->ends_unreachable
)
7135 requires_text_end_align
= TRUE
;
7137 if (elf32xtensa_size_opt
&& !requires_text_end_align
7138 && action
->align_type
!= EBB_REQUIRE_LOOP_ALIGN
7139 && action
->align_type
!= EBB_REQUIRE_TGT_ALIGN
)
7141 longcall_convert_count
= longcall_count
;
7142 narrowable_convert_count
= narrowable_count
;
7143 widenable_convert_count
= 0;
7147 /* There is a constraint. Convert the max number of longcalls. */
7148 narrowable_convert_count
= 0;
7149 longcall_convert_count
= 0;
7150 widenable_convert_count
= 0;
7152 for (j
= 0; j
< longcall_count
; j
++)
7154 int removed
= (longcall_count
- j
) * 3 & (align
- 1);
7155 unsigned desire_narrow
= (align
- removed
) & (align
- 1);
7156 unsigned desire_widen
= removed
;
7157 if (desire_narrow
<= narrowable_count
)
7159 narrowable_convert_count
= desire_narrow
;
7160 narrowable_convert_count
+=
7161 (align
* ((narrowable_count
- narrowable_convert_count
)
7163 longcall_convert_count
= (longcall_count
- j
);
7164 widenable_convert_count
= 0;
7167 if (desire_widen
<= widenable_count
&& !elf32xtensa_size_opt
)
7169 narrowable_convert_count
= 0;
7170 longcall_convert_count
= longcall_count
- j
;
7171 widenable_convert_count
= desire_widen
;
7177 /* Now the number of conversions are saved. Do them. */
7178 for (i
= seg_idx_start
; i
< seg_idx_end
; i
++)
7180 action
= &ebb_table
->actions
[i
];
7181 switch (action
->action
)
7183 case ta_convert_longcall
:
7184 if (longcall_convert_count
!= 0)
7186 action
->action
= ta_remove_longcall
;
7187 action
->do_action
= TRUE
;
7188 action
->removed_bytes
+= 3;
7189 longcall_convert_count
--;
7192 case ta_narrow_insn
:
7193 if (narrowable_convert_count
!= 0)
7195 action
->do_action
= TRUE
;
7196 action
->removed_bytes
+= 1;
7197 narrowable_convert_count
--;
7201 if (widenable_convert_count
!= 0)
7203 action
->do_action
= TRUE
;
7204 action
->removed_bytes
-= 1;
7205 widenable_convert_count
--;
7214 /* Now we move on to some local opts. Try to remove each of the
7215 remaining longcalls. */
7217 if (ebb_table
->ebb
.ends_section
|| ebb_table
->ebb
.ends_unreachable
)
7220 for (i
= 0; i
< ebb_table
->action_count
; i
++)
7222 int old_removed_bytes
= removed_bytes
;
7223 proposed_action
*action
= &ebb_table
->actions
[i
];
7225 if (action
->do_action
&& action
->action
== ta_convert_longcall
)
7227 bfd_boolean bad_alignment
= FALSE
;
7229 for (j
= i
+ 1; j
< ebb_table
->action_count
; j
++)
7231 proposed_action
*new_action
= &ebb_table
->actions
[j
];
7232 bfd_vma offset
= new_action
->offset
;
7233 if (new_action
->align_type
== EBB_REQUIRE_TGT_ALIGN
)
7235 if (!check_branch_target_aligned
7236 (ebb_table
->ebb
.contents
,
7237 ebb_table
->ebb
.content_length
,
7238 offset
, offset
- removed_bytes
))
7240 bad_alignment
= TRUE
;
7244 if (new_action
->align_type
== EBB_REQUIRE_LOOP_ALIGN
)
7246 if (!check_loop_aligned (ebb_table
->ebb
.contents
,
7247 ebb_table
->ebb
.content_length
,
7249 offset
- removed_bytes
))
7251 bad_alignment
= TRUE
;
7255 if (new_action
->action
== ta_narrow_insn
7256 && !new_action
->do_action
7257 && ebb_table
->ebb
.sec
->alignment_power
== 2)
7259 /* Narrow an instruction and we are done. */
7260 new_action
->do_action
= TRUE
;
7261 new_action
->removed_bytes
+= 1;
7262 bad_alignment
= FALSE
;
7265 if (new_action
->action
== ta_widen_insn
7266 && new_action
->do_action
7267 && ebb_table
->ebb
.sec
->alignment_power
== 2)
7269 /* Narrow an instruction and we are done. */
7270 new_action
->do_action
= FALSE
;
7271 new_action
->removed_bytes
+= 1;
7272 bad_alignment
= FALSE
;
7278 action
->removed_bytes
+= 3;
7279 action
->action
= ta_remove_longcall
;
7280 action
->do_action
= TRUE
;
7283 removed_bytes
= old_removed_bytes
;
7284 if (action
->do_action
)
7285 removed_bytes
+= action
->removed_bytes
;
7290 for (i
= 0; i
< ebb_table
->action_count
; ++i
)
7292 proposed_action
*action
= &ebb_table
->actions
[i
];
7293 if (action
->do_action
)
7294 removed_bytes
+= action
->removed_bytes
;
7297 if ((removed_bytes
% (1 << ebb_table
->ebb
.sec
->alignment_power
)) != 0
7298 && ebb
->ends_unreachable
)
7300 proposed_action
*action
;
7304 BFD_ASSERT (ebb_table
->action_count
!= 0);
7305 action
= &ebb_table
->actions
[ebb_table
->action_count
- 1];
7306 BFD_ASSERT (action
->action
== ta_fill
);
7307 BFD_ASSERT (ebb
->ends_unreachable
->flags
& XTENSA_PROP_UNREACHABLE
);
7309 extra_space
= compute_fill_extra_space (ebb
->ends_unreachable
);
7310 br
= action
->removed_bytes
+ removed_bytes
+ extra_space
;
7311 br
= br
& ((1 << ebb
->sec
->alignment_power
) - 1);
7313 action
->removed_bytes
= extra_space
- br
;
7319 /* Use check_section_ebb_pcrels_fit to make sure that all of the
7320 relocations in a section will fit if a proposed set of actions
7324 check_section_ebb_pcrels_fit (abfd
, sec
, contents
, internal_relocs
, constraint
)
7328 Elf_Internal_Rela
*internal_relocs
;
7329 const ebb_constraint
*constraint
;
7332 Elf_Internal_Rela
*irel
;
7333 xtensa_relax_info
*relax_info
;
7335 relax_info
= get_xtensa_relax_info (sec
);
7337 for (i
= 0; i
< sec
->reloc_count
; i
++)
7340 bfd_vma orig_self_offset
, orig_target_offset
;
7341 bfd_vma self_offset
, target_offset
;
7343 reloc_howto_type
*howto
;
7344 int self_removed_bytes
, target_removed_bytes
;
7346 irel
= &internal_relocs
[i
];
7347 r_type
= ELF32_R_TYPE (irel
->r_info
);
7349 howto
= &elf_howto_table
[r_type
];
7350 /* We maintain the required invariant: PC-relative relocations
7351 that fit before linking must fit after linking. Thus we only
7352 need to deal with relocations to the same section that are
7354 if (ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_ASM_SIMPLIFY
7355 || !howto
->pc_relative
)
7358 r_reloc_init (&r_rel
, abfd
, irel
, contents
,
7359 bfd_get_section_limit (abfd
, sec
));
7361 if (r_reloc_get_section (&r_rel
) != sec
)
7364 orig_self_offset
= irel
->r_offset
;
7365 orig_target_offset
= r_rel
.target_offset
;
7367 self_offset
= orig_self_offset
;
7368 target_offset
= orig_target_offset
;
7372 self_offset
= offset_with_removed_text (&relax_info
->action_list
,
7374 target_offset
= offset_with_removed_text (&relax_info
->action_list
,
7375 orig_target_offset
);
7378 self_removed_bytes
= 0;
7379 target_removed_bytes
= 0;
7381 for (j
= 0; j
< constraint
->action_count
; ++j
)
7383 proposed_action
*action
= &constraint
->actions
[j
];
7384 bfd_vma offset
= action
->offset
;
7385 int removed_bytes
= action
->removed_bytes
;
7386 if (offset
< orig_self_offset
7387 || (offset
== orig_self_offset
&& action
->action
== ta_fill
7388 && action
->removed_bytes
< 0))
7389 self_removed_bytes
+= removed_bytes
;
7390 if (offset
< orig_target_offset
7391 || (offset
== orig_target_offset
&& action
->action
== ta_fill
7392 && action
->removed_bytes
< 0))
7393 target_removed_bytes
+= removed_bytes
;
7395 self_offset
-= self_removed_bytes
;
7396 target_offset
-= target_removed_bytes
;
7398 /* Try to encode it. Get the operand and check. */
7399 if (is_alt_relocation (ELF32_R_TYPE (irel
->r_info
)))
7401 /* None of the current alternate relocs are PC-relative,
7402 and only PC-relative relocs matter here. */
7406 xtensa_opcode opcode
;
7409 opcode
= get_relocation_opcode (abfd
, sec
, contents
, irel
);
7410 if (opcode
== XTENSA_UNDEFINED
)
7413 opnum
= get_relocation_opnd (opcode
, ELF32_R_TYPE (irel
->r_info
));
7414 if (opnum
== XTENSA_UNDEFINED
)
7417 if (!pcrel_reloc_fits (opcode
, opnum
, self_offset
, target_offset
))
7427 check_section_ebb_reduces (constraint
)
7428 const ebb_constraint
*constraint
;
7433 for (i
= 0; i
< constraint
->action_count
; i
++)
7435 const proposed_action
*action
= &constraint
->actions
[i
];
7436 if (action
->do_action
)
7437 removed
+= action
->removed_bytes
;
7447 text_action_add_proposed (l
, ebb_table
, sec
)
7448 text_action_list
*l
;
7449 const ebb_constraint
*ebb_table
;
7454 for (i
= 0; i
< ebb_table
->action_count
; i
++)
7456 proposed_action
*action
= &ebb_table
->actions
[i
];
7458 if (!action
->do_action
)
7460 switch (action
->action
)
7462 case ta_remove_insn
:
7463 case ta_remove_longcall
:
7464 case ta_convert_longcall
:
7465 case ta_narrow_insn
:
7468 case ta_remove_literal
:
7469 text_action_add (l
, action
->action
, sec
, action
->offset
,
7470 action
->removed_bytes
);
7483 compute_fill_extra_space (entry
)
7484 property_table_entry
*entry
;
7486 int fill_extra_space
;
7491 if ((entry
->flags
& XTENSA_PROP_UNREACHABLE
) == 0)
7494 fill_extra_space
= entry
->size
;
7495 if ((entry
->flags
& XTENSA_PROP_ALIGN
) != 0)
7497 /* Fill bytes for alignment:
7498 (2**n)-1 - (addr + (2**n)-1) & (2**n -1) */
7499 int pow
= GET_XTENSA_PROP_ALIGNMENT (entry
->flags
);
7500 int nsm
= (1 << pow
) - 1;
7501 bfd_vma addr
= entry
->address
+ entry
->size
;
7502 bfd_vma align_fill
= nsm
- ((addr
+ nsm
) & nsm
);
7503 fill_extra_space
+= align_fill
;
7505 return fill_extra_space
;
7509 /* First relaxation pass. */
7511 /* If the section contains relaxable literals, check each literal to
7512 see if it has the same value as another literal that has already
7513 been seen, either in the current section or a previous one. If so,
7514 add an entry to the per-section list of removed literals. The
7515 actual changes are deferred until the next pass. */
7518 compute_removed_literals (abfd
, sec
, link_info
, values
)
7521 struct bfd_link_info
*link_info
;
7522 value_map_hash_table
*values
;
7524 xtensa_relax_info
*relax_info
;
7526 Elf_Internal_Rela
*internal_relocs
;
7527 source_reloc
*src_relocs
, *rel
;
7528 bfd_boolean ok
= TRUE
;
7529 property_table_entry
*prop_table
= NULL
;
7532 bfd_boolean last_loc_is_prev
= FALSE
;
7533 bfd_vma last_target_offset
= 0;
7534 section_cache_t target_sec_cache
;
7535 bfd_size_type sec_size
;
7537 init_section_cache (&target_sec_cache
);
7539 /* Do nothing if it is not a relaxable literal section. */
7540 relax_info
= get_xtensa_relax_info (sec
);
7541 BFD_ASSERT (relax_info
);
7542 if (!relax_info
->is_relaxable_literal_section
)
7545 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
7546 link_info
->keep_memory
);
7548 sec_size
= bfd_get_section_limit (abfd
, sec
);
7549 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
7550 if (contents
== NULL
&& sec_size
!= 0)
7556 /* Sort the source_relocs by target offset. */
7557 src_relocs
= relax_info
->src_relocs
;
7558 qsort (src_relocs
, relax_info
->src_count
,
7559 sizeof (source_reloc
), source_reloc_compare
);
7560 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
7561 internal_reloc_compare
);
7563 ptblsize
= xtensa_read_table_entries (abfd
, sec
, &prop_table
,
7564 XTENSA_PROP_SEC_NAME
, FALSE
);
7572 for (i
= 0; i
< relax_info
->src_count
; i
++)
7574 Elf_Internal_Rela
*irel
= NULL
;
7576 rel
= &src_relocs
[i
];
7577 if (get_l32r_opcode () != rel
->opcode
)
7579 irel
= get_irel_at_offset (sec
, internal_relocs
,
7580 rel
->r_rel
.target_offset
);
7582 /* If the relocation on this is not a simple R_XTENSA_32 or
7583 R_XTENSA_PLT then do not consider it. This may happen when
7584 the difference of two symbols is used in a literal. */
7585 if (irel
&& (ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_32
7586 && ELF32_R_TYPE (irel
->r_info
) != R_XTENSA_PLT
))
7589 /* If the target_offset for this relocation is the same as the
7590 previous relocation, then we've already considered whether the
7591 literal can be coalesced. Skip to the next one.... */
7592 if (i
!= 0 && prev_i
!= -1
7593 && src_relocs
[i
-1].r_rel
.target_offset
== rel
->r_rel
.target_offset
)
7597 if (last_loc_is_prev
&&
7598 last_target_offset
+ 4 != rel
->r_rel
.target_offset
)
7599 last_loc_is_prev
= FALSE
;
7601 /* Check if the relocation was from an L32R that is being removed
7602 because a CALLX was converted to a direct CALL, and check if
7603 there are no other relocations to the literal. */
7604 if (is_removable_literal (rel
, i
, src_relocs
, relax_info
->src_count
))
7606 if (!remove_dead_literal (abfd
, sec
, link_info
, internal_relocs
,
7607 irel
, rel
, prop_table
, ptblsize
))
7612 last_target_offset
= rel
->r_rel
.target_offset
;
7616 if (!identify_literal_placement (abfd
, sec
, contents
, link_info
,
7618 &last_loc_is_prev
, irel
,
7619 relax_info
->src_count
- i
, rel
,
7620 prop_table
, ptblsize
,
7621 &target_sec_cache
, rel
->is_abs_literal
))
7626 last_target_offset
= rel
->r_rel
.target_offset
;
7630 print_removed_literals (stderr
, &relax_info
->removed_list
);
7631 print_action_list (stderr
, &relax_info
->action_list
);
7635 if (prop_table
) free (prop_table
);
7636 clear_section_cache (&target_sec_cache
);
7638 release_contents (sec
, contents
);
7639 release_internal_relocs (sec
, internal_relocs
);
7644 static Elf_Internal_Rela
*
7645 get_irel_at_offset (sec
, internal_relocs
, offset
)
7647 Elf_Internal_Rela
*internal_relocs
;
7651 Elf_Internal_Rela
*irel
;
7653 Elf_Internal_Rela key
;
7655 if (!internal_relocs
)
7658 key
.r_offset
= offset
;
7659 irel
= bsearch (&key
, internal_relocs
, sec
->reloc_count
,
7660 sizeof (Elf_Internal_Rela
), internal_reloc_matches
);
7664 /* bsearch does not guarantee which will be returned if there are
7665 multiple matches. We need the first that is not an alignment. */
7666 i
= irel
- internal_relocs
;
7669 if (internal_relocs
[i
-1].r_offset
!= offset
)
7673 for ( ; i
< sec
->reloc_count
; i
++)
7675 irel
= &internal_relocs
[i
];
7676 r_type
= ELF32_R_TYPE (irel
->r_info
);
7677 if (irel
->r_offset
== offset
&& r_type
!= R_XTENSA_NONE
)
7686 is_removable_literal (rel
, i
, src_relocs
, src_count
)
7687 const source_reloc
*rel
;
7689 const source_reloc
*src_relocs
;
7692 const source_reloc
*curr_rel
;
7696 for (++i
; i
< src_count
; ++i
)
7698 curr_rel
= &src_relocs
[i
];
7699 /* If all others have the same target offset.... */
7700 if (curr_rel
->r_rel
.target_offset
!= rel
->r_rel
.target_offset
)
7703 if (!curr_rel
->is_null
7704 && !xtensa_is_property_section (curr_rel
->source_sec
)
7705 && !(curr_rel
->source_sec
->flags
& SEC_DEBUGGING
))
7713 remove_dead_literal (abfd
, sec
, link_info
, internal_relocs
,
7714 irel
, rel
, prop_table
, ptblsize
)
7717 struct bfd_link_info
*link_info
;
7718 Elf_Internal_Rela
*internal_relocs
;
7719 Elf_Internal_Rela
*irel
;
7721 property_table_entry
*prop_table
;
7724 property_table_entry
*entry
;
7725 xtensa_relax_info
*relax_info
;
7727 relax_info
= get_xtensa_relax_info (sec
);
7731 entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
7732 sec
->vma
+ rel
->r_rel
.target_offset
);
7734 /* Mark the unused literal so that it will be removed. */
7735 add_removed_literal (&relax_info
->removed_list
, &rel
->r_rel
, NULL
);
7737 text_action_add (&relax_info
->action_list
,
7738 ta_remove_literal
, sec
, rel
->r_rel
.target_offset
, 4);
7740 /* If the section is 4-byte aligned, do not add fill. */
7741 if (sec
->alignment_power
> 2)
7743 int fill_extra_space
;
7744 bfd_vma entry_sec_offset
;
7746 property_table_entry
*the_add_entry
;
7750 entry_sec_offset
= entry
->address
- sec
->vma
+ entry
->size
;
7752 entry_sec_offset
= rel
->r_rel
.target_offset
+ 4;
7754 /* If the literal range is at the end of the section,
7756 the_add_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
7758 fill_extra_space
= compute_fill_extra_space (the_add_entry
);
7760 fa
= find_fill_action (&relax_info
->action_list
, sec
, entry_sec_offset
);
7761 removed_diff
= compute_removed_action_diff (fa
, sec
, entry_sec_offset
,
7762 -4, fill_extra_space
);
7764 adjust_fill_action (fa
, removed_diff
);
7766 text_action_add (&relax_info
->action_list
,
7767 ta_fill
, sec
, entry_sec_offset
, removed_diff
);
7770 /* Zero out the relocation on this literal location. */
7773 if (elf_hash_table (link_info
)->dynamic_sections_created
)
7774 shrink_dynamic_reloc_sections (link_info
, abfd
, sec
, irel
);
7776 irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
7777 pin_internal_relocs (sec
, internal_relocs
);
7780 /* Do not modify "last_loc_is_prev". */
7786 identify_literal_placement (abfd
, sec
, contents
, link_info
, values
,
7787 last_loc_is_prev_p
, irel
, remaining_src_rels
,
7788 rel
, prop_table
, ptblsize
, target_sec_cache
,
7793 struct bfd_link_info
*link_info
;
7794 value_map_hash_table
*values
;
7795 bfd_boolean
*last_loc_is_prev_p
;
7796 Elf_Internal_Rela
*irel
;
7797 int remaining_src_rels
;
7799 property_table_entry
*prop_table
;
7801 section_cache_t
*target_sec_cache
;
7802 bfd_boolean is_abs_literal
;
7806 xtensa_relax_info
*relax_info
;
7807 bfd_boolean literal_placed
= FALSE
;
7809 unsigned long value
;
7810 bfd_boolean final_static_link
;
7811 bfd_size_type sec_size
;
7813 relax_info
= get_xtensa_relax_info (sec
);
7817 sec_size
= bfd_get_section_limit (abfd
, sec
);
7820 (!link_info
->relocatable
7821 && !elf_hash_table (link_info
)->dynamic_sections_created
);
7823 /* The placement algorithm first checks to see if the literal is
7824 already in the value map. If so and the value map is reachable
7825 from all uses, then the literal is moved to that location. If
7826 not, then we identify the last location where a fresh literal was
7827 placed. If the literal can be safely moved there, then we do so.
7828 If not, then we assume that the literal is not to move and leave
7829 the literal where it is, marking it as the last literal
7832 /* Find the literal value. */
7834 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
7837 BFD_ASSERT (rel
->r_rel
.target_offset
< sec_size
);
7838 value
= bfd_get_32 (abfd
, contents
+ rel
->r_rel
.target_offset
);
7840 init_literal_value (&val
, &r_rel
, value
, is_abs_literal
);
7842 /* Check if we've seen another literal with the same value that
7843 is in the same output section. */
7844 val_map
= value_map_get_cached_value (values
, &val
, final_static_link
);
7847 && (r_reloc_get_section (&val_map
->loc
)->output_section
7848 == sec
->output_section
)
7849 && relocations_reach (rel
, remaining_src_rels
, &val_map
->loc
)
7850 && coalesce_shared_literal (sec
, rel
, prop_table
, ptblsize
, val_map
))
7852 /* No change to last_loc_is_prev. */
7853 literal_placed
= TRUE
;
7856 /* For relocatable links, do not try to move literals. To do it
7857 correctly might increase the number of relocations in an input
7858 section making the default relocatable linking fail. */
7859 if (!link_info
->relocatable
&& !literal_placed
7860 && values
->has_last_loc
&& !(*last_loc_is_prev_p
))
7862 asection
*target_sec
= r_reloc_get_section (&values
->last_loc
);
7863 if (target_sec
&& target_sec
->output_section
== sec
->output_section
)
7865 /* Increment the virtual offset. */
7866 r_reloc try_loc
= values
->last_loc
;
7867 try_loc
.virtual_offset
+= 4;
7869 /* There is a last loc that was in the same output section. */
7870 if (relocations_reach (rel
, remaining_src_rels
, &try_loc
)
7871 && move_shared_literal (sec
, link_info
, rel
,
7872 prop_table
, ptblsize
,
7873 &try_loc
, &val
, target_sec_cache
))
7875 values
->last_loc
.virtual_offset
+= 4;
7876 literal_placed
= TRUE
;
7878 val_map
= add_value_map (values
, &val
, &try_loc
,
7881 val_map
->loc
= try_loc
;
7886 if (!literal_placed
)
7888 /* Nothing worked, leave the literal alone but update the last loc. */
7889 values
->has_last_loc
= TRUE
;
7890 values
->last_loc
= rel
->r_rel
;
7892 val_map
= add_value_map (values
, &val
, &rel
->r_rel
, final_static_link
);
7894 val_map
->loc
= rel
->r_rel
;
7895 *last_loc_is_prev_p
= TRUE
;
7902 /* Check if the original relocations (presumably on L32R instructions)
7903 identified by reloc[0..N] can be changed to reference the literal
7904 identified by r_rel. If r_rel is out of range for any of the
7905 original relocations, then we don't want to coalesce the original
7906 literal with the one at r_rel. We only check reloc[0..N], where the
7907 offsets are all the same as for reloc[0] (i.e., they're all
7908 referencing the same literal) and where N is also bounded by the
7909 number of remaining entries in the "reloc" array. The "reloc" array
7910 is sorted by target offset so we know all the entries for the same
7911 literal will be contiguous. */
7914 relocations_reach (reloc
, remaining_relocs
, r_rel
)
7915 source_reloc
*reloc
;
7916 int remaining_relocs
;
7917 const r_reloc
*r_rel
;
7919 bfd_vma from_offset
, source_address
, dest_address
;
7923 if (!r_reloc_is_defined (r_rel
))
7926 sec
= r_reloc_get_section (r_rel
);
7927 from_offset
= reloc
[0].r_rel
.target_offset
;
7929 for (i
= 0; i
< remaining_relocs
; i
++)
7931 if (reloc
[i
].r_rel
.target_offset
!= from_offset
)
7934 /* Ignore relocations that have been removed. */
7935 if (reloc
[i
].is_null
)
7938 /* The original and new output section for these must be the same
7939 in order to coalesce. */
7940 if (r_reloc_get_section (&reloc
[i
].r_rel
)->output_section
7941 != sec
->output_section
)
7944 /* A literal with no PC-relative relocations can be moved anywhere. */
7945 if (reloc
[i
].opnd
!= -1)
7947 /* Otherwise, check to see that it fits. */
7948 source_address
= (reloc
[i
].source_sec
->output_section
->vma
7949 + reloc
[i
].source_sec
->output_offset
7950 + reloc
[i
].r_rel
.rela
.r_offset
);
7951 dest_address
= (sec
->output_section
->vma
7952 + sec
->output_offset
7953 + r_rel
->target_offset
);
7955 if (!pcrel_reloc_fits (reloc
[i
].opcode
, reloc
[i
].opnd
,
7956 source_address
, dest_address
))
7965 /* Move a literal to another literal location because it is
7966 the same as the other literal value. */
7969 coalesce_shared_literal (sec
, rel
, prop_table
, ptblsize
, val_map
)
7972 property_table_entry
*prop_table
;
7976 property_table_entry
*entry
;
7978 property_table_entry
*the_add_entry
;
7980 xtensa_relax_info
*relax_info
;
7982 relax_info
= get_xtensa_relax_info (sec
);
7986 entry
= elf_xtensa_find_property_entry
7987 (prop_table
, ptblsize
, sec
->vma
+ rel
->r_rel
.target_offset
);
7988 if (entry
&& (entry
->flags
& XTENSA_PROP_INSN_NO_TRANSFORM
))
7991 /* Mark that the literal will be coalesced. */
7992 add_removed_literal (&relax_info
->removed_list
, &rel
->r_rel
, &val_map
->loc
);
7994 text_action_add (&relax_info
->action_list
,
7995 ta_remove_literal
, sec
, rel
->r_rel
.target_offset
, 4);
7997 /* If the section is 4-byte aligned, do not add fill. */
7998 if (sec
->alignment_power
> 2)
8000 int fill_extra_space
;
8001 bfd_vma entry_sec_offset
;
8004 entry_sec_offset
= entry
->address
- sec
->vma
+ entry
->size
;
8006 entry_sec_offset
= rel
->r_rel
.target_offset
+ 4;
8008 /* If the literal range is at the end of the section,
8010 fill_extra_space
= 0;
8011 the_add_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
8013 if (the_add_entry
&& (the_add_entry
->flags
& XTENSA_PROP_UNREACHABLE
))
8014 fill_extra_space
= the_add_entry
->size
;
8016 fa
= find_fill_action (&relax_info
->action_list
, sec
, entry_sec_offset
);
8017 removed_diff
= compute_removed_action_diff (fa
, sec
, entry_sec_offset
,
8018 -4, fill_extra_space
);
8020 adjust_fill_action (fa
, removed_diff
);
8022 text_action_add (&relax_info
->action_list
,
8023 ta_fill
, sec
, entry_sec_offset
, removed_diff
);
8030 /* Move a literal to another location. This may actually increase the
8031 total amount of space used because of alignments so we need to do
8032 this carefully. Also, it may make a branch go out of range. */
8035 move_shared_literal (sec
, link_info
, rel
, prop_table
, ptblsize
,
8036 target_loc
, lit_value
, target_sec_cache
)
8038 struct bfd_link_info
*link_info
;
8040 property_table_entry
*prop_table
;
8042 const r_reloc
*target_loc
;
8043 const literal_value
*lit_value
;
8044 section_cache_t
*target_sec_cache
;
8046 property_table_entry
*the_add_entry
, *src_entry
, *target_entry
= NULL
;
8047 text_action
*fa
, *target_fa
;
8049 xtensa_relax_info
*relax_info
, *target_relax_info
;
8050 asection
*target_sec
;
8052 ebb_constraint ebb_table
;
8053 bfd_boolean relocs_fit
;
8055 /* If this routine always returns FALSE, the literals that cannot be
8056 coalesced will not be moved. */
8057 if (elf32xtensa_no_literal_movement
)
8060 relax_info
= get_xtensa_relax_info (sec
);
8064 target_sec
= r_reloc_get_section (target_loc
);
8065 target_relax_info
= get_xtensa_relax_info (target_sec
);
8067 /* Literals to undefined sections may not be moved because they
8068 must report an error. */
8069 if (bfd_is_und_section (target_sec
))
8072 src_entry
= elf_xtensa_find_property_entry
8073 (prop_table
, ptblsize
, sec
->vma
+ rel
->r_rel
.target_offset
);
8075 if (!section_cache_section (target_sec_cache
, target_sec
, link_info
))
8078 target_entry
= elf_xtensa_find_property_entry
8079 (target_sec_cache
->ptbl
, target_sec_cache
->pte_count
,
8080 target_sec
->vma
+ target_loc
->target_offset
);
8085 /* Make sure that we have not broken any branches. */
8088 init_ebb_constraint (&ebb_table
);
8089 ebb
= &ebb_table
.ebb
;
8090 init_ebb (ebb
, target_sec_cache
->sec
, target_sec_cache
->contents
,
8091 target_sec_cache
->content_length
,
8092 target_sec_cache
->ptbl
, target_sec_cache
->pte_count
,
8093 target_sec_cache
->relocs
, target_sec_cache
->reloc_count
);
8095 /* Propose to add 4 bytes + worst-case alignment size increase to
8097 ebb_propose_action (&ebb_table
, EBB_NO_ALIGN
, 0,
8098 ta_fill
, target_loc
->target_offset
,
8099 -4 - (1 << target_sec
->alignment_power
), TRUE
);
8101 /* Check all of the PC-relative relocations to make sure they still fit. */
8102 relocs_fit
= check_section_ebb_pcrels_fit (target_sec
->owner
, target_sec
,
8103 target_sec_cache
->contents
,
8104 target_sec_cache
->relocs
,
8110 text_action_add_literal (&target_relax_info
->action_list
,
8111 ta_add_literal
, target_loc
, lit_value
, -4);
8113 if (target_sec
->alignment_power
> 2 && target_entry
!= src_entry
)
8115 /* May need to add or remove some fill to maintain alignment. */
8116 int fill_extra_space
;
8117 bfd_vma entry_sec_offset
;
8120 target_entry
->address
- target_sec
->vma
+ target_entry
->size
;
8122 /* If the literal range is at the end of the section,
8124 fill_extra_space
= 0;
8126 elf_xtensa_find_property_entry (target_sec_cache
->ptbl
,
8127 target_sec_cache
->pte_count
,
8129 if (the_add_entry
&& (the_add_entry
->flags
& XTENSA_PROP_UNREACHABLE
))
8130 fill_extra_space
= the_add_entry
->size
;
8132 target_fa
= find_fill_action (&target_relax_info
->action_list
,
8133 target_sec
, entry_sec_offset
);
8134 removed_diff
= compute_removed_action_diff (target_fa
, target_sec
,
8135 entry_sec_offset
, 4,
8138 adjust_fill_action (target_fa
, removed_diff
);
8140 text_action_add (&target_relax_info
->action_list
,
8141 ta_fill
, target_sec
, entry_sec_offset
, removed_diff
);
8144 /* Mark that the literal will be moved to the new location. */
8145 add_removed_literal (&relax_info
->removed_list
, &rel
->r_rel
, target_loc
);
8147 /* Remove the literal. */
8148 text_action_add (&relax_info
->action_list
,
8149 ta_remove_literal
, sec
, rel
->r_rel
.target_offset
, 4);
8151 /* If the section is 4-byte aligned, do not add fill. */
8152 if (sec
->alignment_power
> 2 && target_entry
!= src_entry
)
8154 int fill_extra_space
;
8155 bfd_vma entry_sec_offset
;
8158 entry_sec_offset
= src_entry
->address
- sec
->vma
+ src_entry
->size
;
8160 entry_sec_offset
= rel
->r_rel
.target_offset
+4;
8162 /* If the literal range is at the end of the section,
8164 fill_extra_space
= 0;
8165 the_add_entry
= elf_xtensa_find_property_entry (prop_table
, ptblsize
,
8167 if (the_add_entry
&& (the_add_entry
->flags
& XTENSA_PROP_UNREACHABLE
))
8168 fill_extra_space
= the_add_entry
->size
;
8170 fa
= find_fill_action (&relax_info
->action_list
, sec
, entry_sec_offset
);
8171 removed_diff
= compute_removed_action_diff (fa
, sec
, entry_sec_offset
,
8172 -4, fill_extra_space
);
8174 adjust_fill_action (fa
, removed_diff
);
8176 text_action_add (&relax_info
->action_list
,
8177 ta_fill
, sec
, entry_sec_offset
, removed_diff
);
8184 /* Second relaxation pass. */
8186 /* Modify all of the relocations to point to the right spot, and if this
8187 is a relaxable section, delete the unwanted literals and fix the
8191 relax_section (abfd
, sec
, link_info
)
8194 struct bfd_link_info
*link_info
;
8196 Elf_Internal_Rela
*internal_relocs
;
8197 xtensa_relax_info
*relax_info
;
8199 bfd_boolean ok
= TRUE
;
8201 bfd_boolean rv
= FALSE
;
8202 bfd_boolean virtual_action
;
8203 bfd_size_type sec_size
;
8205 sec_size
= bfd_get_section_limit (abfd
, sec
);
8206 relax_info
= get_xtensa_relax_info (sec
);
8207 BFD_ASSERT (relax_info
);
8209 /* First translate any of the fixes that have been added already. */
8210 translate_section_fixes (sec
);
8212 /* Handle property sections (e.g., literal tables) specially. */
8213 if (xtensa_is_property_section (sec
))
8215 BFD_ASSERT (!relax_info
->is_relaxable_literal_section
);
8216 return relax_property_section (abfd
, sec
, link_info
);
8219 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
8220 link_info
->keep_memory
);
8221 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
8222 if (contents
== NULL
&& sec_size
!= 0)
8228 if (internal_relocs
)
8230 for (i
= 0; i
< sec
->reloc_count
; i
++)
8232 Elf_Internal_Rela
*irel
;
8233 xtensa_relax_info
*target_relax_info
;
8234 bfd_vma source_offset
, old_source_offset
;
8237 asection
*target_sec
;
8239 /* Locally change the source address.
8240 Translate the target to the new target address.
8241 If it points to this section and has been removed,
8245 irel
= &internal_relocs
[i
];
8246 source_offset
= irel
->r_offset
;
8247 old_source_offset
= source_offset
;
8249 r_type
= ELF32_R_TYPE (irel
->r_info
);
8250 r_reloc_init (&r_rel
, abfd
, irel
, contents
,
8251 bfd_get_section_limit (abfd
, sec
));
8253 /* If this section could have changed then we may need to
8254 change the relocation's offset. */
8256 if (relax_info
->is_relaxable_literal_section
8257 || relax_info
->is_relaxable_asm_section
)
8259 if (r_type
!= R_XTENSA_NONE
8260 && find_removed_literal (&relax_info
->removed_list
,
8263 /* Remove this relocation. */
8264 if (elf_hash_table (link_info
)->dynamic_sections_created
)
8265 shrink_dynamic_reloc_sections (link_info
, abfd
, sec
, irel
);
8266 irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
8267 irel
->r_offset
= offset_with_removed_text
8268 (&relax_info
->action_list
, irel
->r_offset
);
8269 pin_internal_relocs (sec
, internal_relocs
);
8273 if (r_type
== R_XTENSA_ASM_SIMPLIFY
)
8275 text_action
*action
=
8276 find_insn_action (&relax_info
->action_list
,
8278 if (action
&& (action
->action
== ta_convert_longcall
8279 || action
->action
== ta_remove_longcall
))
8281 bfd_reloc_status_type retval
;
8282 char *error_message
= NULL
;
8284 retval
= contract_asm_expansion (contents
, sec_size
,
8285 irel
, &error_message
);
8286 if (retval
!= bfd_reloc_ok
)
8288 (*link_info
->callbacks
->reloc_dangerous
)
8289 (link_info
, error_message
, abfd
, sec
,
8293 /* Update the action so that the code that moves
8294 the contents will do the right thing. */
8295 if (action
->action
== ta_remove_longcall
)
8296 action
->action
= ta_remove_insn
;
8298 action
->action
= ta_none
;
8299 /* Refresh the info in the r_rel. */
8300 r_reloc_init (&r_rel
, abfd
, irel
, contents
, sec_size
);
8301 r_type
= ELF32_R_TYPE (irel
->r_info
);
8305 source_offset
= offset_with_removed_text
8306 (&relax_info
->action_list
, irel
->r_offset
);
8307 irel
->r_offset
= source_offset
;
8310 /* If the target section could have changed then
8311 we may need to change the relocation's target offset. */
8313 target_sec
= r_reloc_get_section (&r_rel
);
8314 target_relax_info
= get_xtensa_relax_info (target_sec
);
8316 if (target_relax_info
8317 && (target_relax_info
->is_relaxable_literal_section
8318 || target_relax_info
->is_relaxable_asm_section
))
8322 bfd_vma addend_displacement
;
8324 translate_reloc (&r_rel
, &new_reloc
);
8326 if (r_type
== R_XTENSA_DIFF8
8327 || r_type
== R_XTENSA_DIFF16
8328 || r_type
== R_XTENSA_DIFF32
)
8330 bfd_vma diff_value
= 0, new_end_offset
, diff_mask
= 0;
8332 if (bfd_get_section_limit (abfd
, sec
) < old_source_offset
)
8334 (*link_info
->callbacks
->reloc_dangerous
)
8335 (link_info
, _("invalid relocation address"),
8336 abfd
, sec
, old_source_offset
);
8342 case R_XTENSA_DIFF8
:
8344 bfd_get_8 (abfd
, &contents
[old_source_offset
]);
8346 case R_XTENSA_DIFF16
:
8348 bfd_get_16 (abfd
, &contents
[old_source_offset
]);
8350 case R_XTENSA_DIFF32
:
8352 bfd_get_32 (abfd
, &contents
[old_source_offset
]);
8356 new_end_offset
= offset_with_removed_text
8357 (&target_relax_info
->action_list
,
8358 r_rel
.target_offset
+ diff_value
);
8359 diff_value
= new_end_offset
- new_reloc
.target_offset
;
8363 case R_XTENSA_DIFF8
:
8365 bfd_put_8 (abfd
, diff_value
,
8366 &contents
[old_source_offset
]);
8368 case R_XTENSA_DIFF16
:
8370 bfd_put_16 (abfd
, diff_value
,
8371 &contents
[old_source_offset
]);
8373 case R_XTENSA_DIFF32
:
8374 diff_mask
= 0xffffffff;
8375 bfd_put_32 (abfd
, diff_value
,
8376 &contents
[old_source_offset
]);
8380 /* Check for overflow. */
8381 if ((diff_value
& ~diff_mask
) != 0)
8383 (*link_info
->callbacks
->reloc_dangerous
)
8384 (link_info
, _("overflow after relaxation"),
8385 abfd
, sec
, old_source_offset
);
8389 pin_contents (sec
, contents
);
8392 /* FIXME: If the relocation still references a section in
8393 the same input file, the relocation should be modified
8394 directly instead of adding a "fix" record. */
8396 addend_displacement
=
8397 new_reloc
.target_offset
+ new_reloc
.virtual_offset
;
8399 fix
= reloc_bfd_fix_init (sec
, source_offset
, r_type
, 0,
8400 r_reloc_get_section (&new_reloc
),
8401 addend_displacement
, TRUE
);
8405 pin_internal_relocs (sec
, internal_relocs
);
8409 if ((relax_info
->is_relaxable_literal_section
8410 || relax_info
->is_relaxable_asm_section
)
8411 && relax_info
->action_list
.head
)
8413 /* Walk through the planned actions and build up a table
8414 of move, copy and fill records. Use the move, copy and
8415 fill records to perform the actions once. */
8417 bfd_size_type size
= sec
->size
;
8419 bfd_size_type final_size
, copy_size
, orig_insn_size
;
8420 bfd_byte
*scratch
= NULL
;
8421 bfd_byte
*dup_contents
= NULL
;
8422 bfd_size_type orig_size
= size
;
8423 bfd_vma orig_dot
= 0;
8424 bfd_vma orig_dot_copied
= 0; /* Byte copied already from
8425 orig dot in physical memory. */
8426 bfd_vma orig_dot_vo
= 0; /* Virtual offset from orig_dot. */
8427 bfd_vma dup_dot
= 0;
8429 text_action
*action
= relax_info
->action_list
.head
;
8431 final_size
= sec
->size
;
8432 for (action
= relax_info
->action_list
.head
; action
;
8433 action
= action
->next
)
8435 final_size
-= action
->removed_bytes
;
8438 scratch
= (bfd_byte
*) bfd_zmalloc (final_size
);
8439 dup_contents
= (bfd_byte
*) bfd_zmalloc (final_size
);
8441 /* The dot is the current fill location. */
8443 print_action_list (stderr
, &relax_info
->action_list
);
8446 for (action
= relax_info
->action_list
.head
; action
;
8447 action
= action
->next
)
8449 virtual_action
= FALSE
;
8450 if (action
->offset
> orig_dot
)
8452 orig_dot
+= orig_dot_copied
;
8453 orig_dot_copied
= 0;
8455 /* Out of the virtual world. */
8458 if (action
->offset
> orig_dot
)
8460 copy_size
= action
->offset
- orig_dot
;
8461 memmove (&dup_contents
[dup_dot
], &contents
[orig_dot
], copy_size
);
8462 orig_dot
+= copy_size
;
8463 dup_dot
+= copy_size
;
8464 BFD_ASSERT (action
->offset
== orig_dot
);
8466 else if (action
->offset
< orig_dot
)
8468 if (action
->action
== ta_fill
8469 && action
->offset
- action
->removed_bytes
== orig_dot
)
8471 /* This is OK because the fill only effects the dup_dot. */
8473 else if (action
->action
== ta_add_literal
)
8475 /* TBD. Might need to handle this. */
8478 if (action
->offset
== orig_dot
)
8480 if (action
->virtual_offset
> orig_dot_vo
)
8482 if (orig_dot_vo
== 0)
8484 /* Need to copy virtual_offset bytes. Probably four. */
8485 copy_size
= action
->virtual_offset
- orig_dot_vo
;
8486 memmove (&dup_contents
[dup_dot
],
8487 &contents
[orig_dot
], copy_size
);
8488 orig_dot_copied
= copy_size
;
8489 dup_dot
+= copy_size
;
8491 virtual_action
= TRUE
;
8494 BFD_ASSERT (action
->virtual_offset
<= orig_dot_vo
);
8496 switch (action
->action
)
8498 case ta_remove_literal
:
8499 case ta_remove_insn
:
8500 BFD_ASSERT (action
->removed_bytes
>= 0);
8501 orig_dot
+= action
->removed_bytes
;
8504 case ta_narrow_insn
:
8507 memmove (scratch
, &contents
[orig_dot
], orig_insn_size
);
8508 BFD_ASSERT (action
->removed_bytes
== 1);
8509 rv
= narrow_instruction (scratch
, final_size
, 0, TRUE
);
8511 memmove (&dup_contents
[dup_dot
], scratch
, copy_size
);
8512 orig_dot
+= orig_insn_size
;
8513 dup_dot
+= copy_size
;
8517 if (action
->removed_bytes
>= 0)
8518 orig_dot
+= action
->removed_bytes
;
8521 /* Already zeroed in dup_contents. Just bump the
8523 dup_dot
+= (-action
->removed_bytes
);
8528 BFD_ASSERT (action
->removed_bytes
== 0);
8531 case ta_convert_longcall
:
8532 case ta_remove_longcall
:
8533 /* These will be removed or converted before we get here. */
8540 memmove (scratch
, &contents
[orig_dot
], orig_insn_size
);
8541 BFD_ASSERT (action
->removed_bytes
== -1);
8542 rv
= widen_instruction (scratch
, final_size
, 0, TRUE
);
8544 memmove (&dup_contents
[dup_dot
], scratch
, copy_size
);
8545 orig_dot
+= orig_insn_size
;
8546 dup_dot
+= copy_size
;
8549 case ta_add_literal
:
8552 BFD_ASSERT (action
->removed_bytes
== -4);
8553 /* TBD -- place the literal value here and insert
8555 memset (&dup_contents
[dup_dot
], 0, 4);
8556 pin_internal_relocs (sec
, internal_relocs
);
8557 pin_contents (sec
, contents
);
8559 if (!move_literal (abfd
, link_info
, sec
, dup_dot
, dup_contents
,
8560 relax_info
, &internal_relocs
, &action
->value
))
8564 orig_dot_vo
+= copy_size
;
8566 orig_dot
+= orig_insn_size
;
8567 dup_dot
+= copy_size
;
8571 /* Not implemented yet. */
8576 size
-= action
->removed_bytes
;
8577 removed
+= action
->removed_bytes
;
8578 BFD_ASSERT (dup_dot
<= final_size
);
8579 BFD_ASSERT (orig_dot
<= orig_size
);
8582 orig_dot
+= orig_dot_copied
;
8583 orig_dot_copied
= 0;
8585 if (orig_dot
!= orig_size
)
8587 copy_size
= orig_size
- orig_dot
;
8588 BFD_ASSERT (orig_size
> orig_dot
);
8589 BFD_ASSERT (dup_dot
+ copy_size
== final_size
);
8590 memmove (&dup_contents
[dup_dot
], &contents
[orig_dot
], copy_size
);
8591 orig_dot
+= copy_size
;
8592 dup_dot
+= copy_size
;
8594 BFD_ASSERT (orig_size
== orig_dot
);
8595 BFD_ASSERT (final_size
== dup_dot
);
8597 /* Move the dup_contents back. */
8598 if (final_size
> orig_size
)
8600 /* Contents need to be reallocated. Swap the dup_contents into
8602 sec
->contents
= dup_contents
;
8604 contents
= dup_contents
;
8605 pin_contents (sec
, contents
);
8609 BFD_ASSERT (final_size
<= orig_size
);
8610 memset (contents
, 0, orig_size
);
8611 memcpy (contents
, dup_contents
, final_size
);
8612 free (dup_contents
);
8615 pin_contents (sec
, contents
);
8617 sec
->size
= final_size
;
8621 release_internal_relocs (sec
, internal_relocs
);
8622 release_contents (sec
, contents
);
8628 translate_section_fixes (sec
)
8631 xtensa_relax_info
*relax_info
;
8634 relax_info
= get_xtensa_relax_info (sec
);
8638 for (r
= relax_info
->fix_list
; r
!= NULL
; r
= r
->next
)
8639 if (!translate_reloc_bfd_fix (r
))
8646 /* Translate a fix given the mapping in the relax info for the target
8647 section. If it has already been translated, no work is required. */
8650 translate_reloc_bfd_fix (fix
)
8653 reloc_bfd_fix new_fix
;
8655 xtensa_relax_info
*relax_info
;
8656 removed_literal
*removed
;
8657 bfd_vma new_offset
, target_offset
;
8659 if (fix
->translated
)
8662 sec
= fix
->target_sec
;
8663 target_offset
= fix
->target_offset
;
8665 relax_info
= get_xtensa_relax_info (sec
);
8668 fix
->translated
= TRUE
;
8674 /* The fix does not need to be translated if the section cannot change. */
8675 if (!relax_info
->is_relaxable_literal_section
8676 && !relax_info
->is_relaxable_asm_section
)
8678 fix
->translated
= TRUE
;
8682 /* If the literal has been moved and this relocation was on an
8683 opcode, then the relocation should move to the new literal
8684 location. Otherwise, the relocation should move within the
8688 if (is_operand_relocation (fix
->src_type
))
8690 /* Check if the original relocation is against a literal being
8692 removed
= find_removed_literal (&relax_info
->removed_list
,
8700 /* The fact that there is still a relocation to this literal indicates
8701 that the literal is being coalesced, not simply removed. */
8702 BFD_ASSERT (removed
->to
.abfd
!= NULL
);
8704 /* This was moved to some other address (possibly another section). */
8705 new_sec
= r_reloc_get_section (&removed
->to
);
8709 relax_info
= get_xtensa_relax_info (sec
);
8711 (!relax_info
->is_relaxable_literal_section
8712 && !relax_info
->is_relaxable_asm_section
))
8714 target_offset
= removed
->to
.target_offset
;
8715 new_fix
.target_sec
= new_sec
;
8716 new_fix
.target_offset
= target_offset
;
8717 new_fix
.translated
= TRUE
;
8722 target_offset
= removed
->to
.target_offset
;
8723 new_fix
.target_sec
= new_sec
;
8726 /* The target address may have been moved within its section. */
8727 new_offset
= offset_with_removed_text (&relax_info
->action_list
,
8730 new_fix
.target_offset
= new_offset
;
8731 new_fix
.target_offset
= new_offset
;
8732 new_fix
.translated
= TRUE
;
8738 /* Fix up a relocation to take account of removed literals. */
8741 translate_reloc (orig_rel
, new_rel
)
8742 const r_reloc
*orig_rel
;
8746 xtensa_relax_info
*relax_info
;
8747 removed_literal
*removed
;
8748 bfd_vma new_offset
, target_offset
, removed_bytes
;
8750 *new_rel
= *orig_rel
;
8752 if (!r_reloc_is_defined (orig_rel
))
8754 sec
= r_reloc_get_section (orig_rel
);
8756 relax_info
= get_xtensa_relax_info (sec
);
8757 BFD_ASSERT (relax_info
);
8759 if (!relax_info
->is_relaxable_literal_section
8760 && !relax_info
->is_relaxable_asm_section
)
8763 target_offset
= orig_rel
->target_offset
;
8766 if (is_operand_relocation (ELF32_R_TYPE (orig_rel
->rela
.r_info
)))
8768 /* Check if the original relocation is against a literal being
8770 removed
= find_removed_literal (&relax_info
->removed_list
,
8773 if (removed
&& removed
->to
.abfd
)
8777 /* The fact that there is still a relocation to this literal indicates
8778 that the literal is being coalesced, not simply removed. */
8779 BFD_ASSERT (removed
->to
.abfd
!= NULL
);
8781 /* This was moved to some other address
8782 (possibly in another section). */
8783 *new_rel
= removed
->to
;
8784 new_sec
= r_reloc_get_section (new_rel
);
8788 relax_info
= get_xtensa_relax_info (sec
);
8790 || (!relax_info
->is_relaxable_literal_section
8791 && !relax_info
->is_relaxable_asm_section
))
8794 target_offset
= new_rel
->target_offset
;
8797 /* ...and the target address may have been moved within its section. */
8798 new_offset
= offset_with_removed_text (&relax_info
->action_list
,
8801 /* Modify the offset and addend. */
8802 removed_bytes
= target_offset
- new_offset
;
8803 new_rel
->target_offset
= new_offset
;
8804 new_rel
->rela
.r_addend
-= removed_bytes
;
8808 /* For dynamic links, there may be a dynamic relocation for each
8809 literal. The number of dynamic relocations must be computed in
8810 size_dynamic_sections, which occurs before relaxation. When a
8811 literal is removed, this function checks if there is a corresponding
8812 dynamic relocation and shrinks the size of the appropriate dynamic
8813 relocation section accordingly. At this point, the contents of the
8814 dynamic relocation sections have not yet been filled in, so there's
8815 nothing else that needs to be done. */
8818 shrink_dynamic_reloc_sections (info
, abfd
, input_section
, rel
)
8819 struct bfd_link_info
*info
;
8821 asection
*input_section
;
8822 Elf_Internal_Rela
*rel
;
8824 Elf_Internal_Shdr
*symtab_hdr
;
8825 struct elf_link_hash_entry
**sym_hashes
;
8826 unsigned long r_symndx
;
8828 struct elf_link_hash_entry
*h
;
8829 bfd_boolean dynamic_symbol
;
8831 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8832 sym_hashes
= elf_sym_hashes (abfd
);
8834 r_type
= ELF32_R_TYPE (rel
->r_info
);
8835 r_symndx
= ELF32_R_SYM (rel
->r_info
);
8837 if (r_symndx
< symtab_hdr
->sh_info
)
8840 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
8842 dynamic_symbol
= xtensa_elf_dynamic_symbol_p (h
, info
);
8844 if ((r_type
== R_XTENSA_32
|| r_type
== R_XTENSA_PLT
)
8845 && (input_section
->flags
& SEC_ALLOC
) != 0
8846 && (dynamic_symbol
|| info
->shared
))
8849 const char *srel_name
;
8851 bfd_boolean is_plt
= FALSE
;
8853 dynobj
= elf_hash_table (info
)->dynobj
;
8854 BFD_ASSERT (dynobj
!= NULL
);
8856 if (dynamic_symbol
&& r_type
== R_XTENSA_PLT
)
8858 srel_name
= ".rela.plt";
8862 srel_name
= ".rela.got";
8864 /* Reduce size of the .rela.* section by one reloc. */
8865 srel
= bfd_get_section_by_name (dynobj
, srel_name
);
8866 BFD_ASSERT (srel
!= NULL
);
8867 BFD_ASSERT (srel
->size
>= sizeof (Elf32_External_Rela
));
8868 srel
->size
-= sizeof (Elf32_External_Rela
);
8872 asection
*splt
, *sgotplt
, *srelgot
;
8873 int reloc_index
, chunk
;
8875 /* Find the PLT reloc index of the entry being removed. This
8876 is computed from the size of ".rela.plt". It is needed to
8877 figure out which PLT chunk to resize. Usually "last index
8878 = size - 1" since the index starts at zero, but in this
8879 context, the size has just been decremented so there's no
8880 need to subtract one. */
8881 reloc_index
= srel
->size
/ sizeof (Elf32_External_Rela
);
8883 chunk
= reloc_index
/ PLT_ENTRIES_PER_CHUNK
;
8884 splt
= elf_xtensa_get_plt_section (dynobj
, chunk
);
8885 sgotplt
= elf_xtensa_get_gotplt_section (dynobj
, chunk
);
8886 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
);
8888 /* Check if an entire PLT chunk has just been eliminated. */
8889 if (reloc_index
% PLT_ENTRIES_PER_CHUNK
== 0)
8891 /* The two magic GOT entries for that chunk can go away. */
8892 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
8893 BFD_ASSERT (srelgot
!= NULL
);
8894 srelgot
->reloc_count
-= 2;
8895 srelgot
->size
-= 2 * sizeof (Elf32_External_Rela
);
8898 /* There should be only one entry left (and it will be
8900 BFD_ASSERT (sgotplt
->size
== 4);
8901 BFD_ASSERT (splt
->size
== PLT_ENTRY_SIZE
);
8904 BFD_ASSERT (sgotplt
->size
>= 4);
8905 BFD_ASSERT (splt
->size
>= PLT_ENTRY_SIZE
);
8908 splt
->size
-= PLT_ENTRY_SIZE
;
8914 /* Take an r_rel and move it to another section. This usually
8915 requires extending the interal_relocation array and pinning it. If
8916 the original r_rel is from the same BFD, we can complete this here.
8917 Otherwise, we add a fix record to let the final link fix the
8918 appropriate address. Contents and internal relocations for the
8919 section must be pinned after calling this routine. */
8922 move_literal (abfd
, link_info
, sec
, offset
, contents
, relax_info
,
8923 internal_relocs_p
, lit
)
8925 struct bfd_link_info
*link_info
;
8929 xtensa_relax_info
*relax_info
;
8930 Elf_Internal_Rela
**internal_relocs_p
;
8931 const literal_value
*lit
;
8933 Elf_Internal_Rela
*new_relocs
= NULL
;
8934 size_t new_relocs_count
= 0;
8935 Elf_Internal_Rela this_rela
;
8936 const r_reloc
*r_rel
;
8938 r_rel
= &lit
->r_rel
;
8939 BFD_ASSERT (elf_section_data (sec
)->relocs
== *internal_relocs_p
);
8941 if (r_reloc_is_const (r_rel
))
8942 bfd_put_32 (abfd
, lit
->value
, contents
+ offset
);
8947 asection
*target_sec
;
8951 r_type
= ELF32_R_TYPE (r_rel
->rela
.r_info
);
8952 target_sec
= r_reloc_get_section (r_rel
);
8954 /* This is the difficult case. We have to create a fix up. */
8955 this_rela
.r_offset
= offset
;
8956 this_rela
.r_info
= ELF32_R_INFO (0, r_type
);
8957 this_rela
.r_addend
=
8958 r_rel
->target_offset
- r_reloc_get_target_offset (r_rel
);
8959 bfd_put_32 (abfd
, lit
->value
, contents
+ offset
);
8961 /* Currently, we cannot move relocations during a relocatable link. */
8962 BFD_ASSERT (!link_info
->relocatable
);
8963 fix
= reloc_bfd_fix_init (sec
, offset
, r_type
, r_rel
->abfd
,
8964 r_reloc_get_section (r_rel
),
8965 r_rel
->target_offset
+ r_rel
->virtual_offset
,
8967 /* We also need to mark that relocations are needed here. */
8968 sec
->flags
|= SEC_RELOC
;
8970 translate_reloc_bfd_fix (fix
);
8971 /* This fix has not yet been translated. */
8974 /* Add the relocation. If we have already allocated our own
8975 space for the relocations and we have room for more, then use
8976 it. Otherwise, allocate new space and move the literals. */
8977 insert_at
= sec
->reloc_count
;
8978 for (i
= 0; i
< sec
->reloc_count
; ++i
)
8980 if (this_rela
.r_offset
< (*internal_relocs_p
)[i
].r_offset
)
8987 if (*internal_relocs_p
!= relax_info
->allocated_relocs
8988 || sec
->reloc_count
+ 1 > relax_info
->allocated_relocs_count
)
8990 BFD_ASSERT (relax_info
->allocated_relocs
== NULL
8991 || sec
->reloc_count
== relax_info
->relocs_count
);
8993 if (relax_info
->allocated_relocs_count
== 0)
8994 new_relocs_count
= (sec
->reloc_count
+ 2) * 2;
8996 new_relocs_count
= (relax_info
->allocated_relocs_count
+ 2) * 2;
8998 new_relocs
= (Elf_Internal_Rela
*)
8999 bfd_zmalloc (sizeof (Elf_Internal_Rela
) * (new_relocs_count
));
9003 /* We could handle this more quickly by finding the split point. */
9005 memcpy (new_relocs
, *internal_relocs_p
,
9006 insert_at
* sizeof (Elf_Internal_Rela
));
9008 new_relocs
[insert_at
] = this_rela
;
9010 if (insert_at
!= sec
->reloc_count
)
9011 memcpy (new_relocs
+ insert_at
+ 1,
9012 (*internal_relocs_p
) + insert_at
,
9013 (sec
->reloc_count
- insert_at
)
9014 * sizeof (Elf_Internal_Rela
));
9016 if (*internal_relocs_p
!= relax_info
->allocated_relocs
)
9018 /* The first time we re-allocate, we can only free the
9019 old relocs if they were allocated with bfd_malloc.
9020 This is not true when keep_memory is in effect. */
9021 if (!link_info
->keep_memory
)
9022 free (*internal_relocs_p
);
9025 free (*internal_relocs_p
);
9026 relax_info
->allocated_relocs
= new_relocs
;
9027 relax_info
->allocated_relocs_count
= new_relocs_count
;
9028 elf_section_data (sec
)->relocs
= new_relocs
;
9030 relax_info
->relocs_count
= sec
->reloc_count
;
9031 *internal_relocs_p
= new_relocs
;
9035 if (insert_at
!= sec
->reloc_count
)
9038 for (idx
= sec
->reloc_count
; idx
> insert_at
; idx
--)
9039 (*internal_relocs_p
)[idx
] = (*internal_relocs_p
)[idx
-1];
9041 (*internal_relocs_p
)[insert_at
] = this_rela
;
9043 if (relax_info
->allocated_relocs
)
9044 relax_info
->relocs_count
= sec
->reloc_count
;
9051 /* This is similar to relax_section except that when a target is moved,
9052 we shift addresses up. We also need to modify the size. This
9053 algorithm does NOT allow for relocations into the middle of the
9054 property sections. */
9057 relax_property_section (abfd
, sec
, link_info
)
9060 struct bfd_link_info
*link_info
;
9062 Elf_Internal_Rela
*internal_relocs
;
9065 bfd_boolean ok
= TRUE
;
9066 bfd_boolean is_full_prop_section
;
9067 size_t last_zfill_target_offset
= 0;
9068 asection
*last_zfill_target_sec
= NULL
;
9069 bfd_size_type sec_size
;
9071 sec_size
= bfd_get_section_limit (abfd
, sec
);
9072 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
9073 link_info
->keep_memory
);
9074 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
9075 if (contents
== NULL
&& sec_size
!= 0)
9081 is_full_prop_section
=
9082 ((strcmp (sec
->name
, XTENSA_PROP_SEC_NAME
) == 0)
9083 || (strncmp (sec
->name
, ".gnu.linkonce.prop.",
9084 sizeof ".gnu.linkonce.prop." - 1) == 0));
9086 if (internal_relocs
)
9088 for (i
= 0; i
< sec
->reloc_count
; i
++)
9090 Elf_Internal_Rela
*irel
;
9091 xtensa_relax_info
*target_relax_info
;
9093 asection
*target_sec
;
9095 bfd_byte
*size_p
, *flags_p
;
9097 /* Locally change the source address.
9098 Translate the target to the new target address.
9099 If it points to this section and has been removed, MOVE IT.
9100 Also, don't forget to modify the associated SIZE at
9103 irel
= &internal_relocs
[i
];
9104 r_type
= ELF32_R_TYPE (irel
->r_info
);
9105 if (r_type
== R_XTENSA_NONE
)
9108 /* Find the literal value. */
9109 r_reloc_init (&val
.r_rel
, abfd
, irel
, contents
, sec_size
);
9110 size_p
= &contents
[irel
->r_offset
+ 4];
9112 if (is_full_prop_section
)
9114 flags_p
= &contents
[irel
->r_offset
+ 8];
9115 BFD_ASSERT (irel
->r_offset
+ 12 <= sec_size
);
9118 BFD_ASSERT (irel
->r_offset
+ 8 <= sec_size
);
9120 target_sec
= r_reloc_get_section (&val
.r_rel
);
9121 target_relax_info
= get_xtensa_relax_info (target_sec
);
9123 if (target_relax_info
9124 && (target_relax_info
->is_relaxable_literal_section
9125 || target_relax_info
->is_relaxable_asm_section
))
9127 /* Translate the relocation's destination. */
9128 bfd_vma new_offset
, new_end_offset
;
9129 long old_size
, new_size
;
9131 new_offset
= offset_with_removed_text
9132 (&target_relax_info
->action_list
, val
.r_rel
.target_offset
);
9134 /* Assert that we are not out of bounds. */
9135 old_size
= bfd_get_32 (abfd
, size_p
);
9139 /* Only the first zero-sized unreachable entry is
9140 allowed to expand. In this case the new offset
9141 should be the offset before the fill and the new
9142 size is the expansion size. For other zero-sized
9143 entries the resulting size should be zero with an
9144 offset before or after the fill address depending
9145 on whether the expanding unreachable entry
9147 if (last_zfill_target_sec
9148 && last_zfill_target_sec
== target_sec
9149 && last_zfill_target_offset
== val
.r_rel
.target_offset
)
9150 new_end_offset
= new_offset
;
9153 new_end_offset
= new_offset
;
9154 new_offset
= offset_with_removed_text_before_fill
9155 (&target_relax_info
->action_list
,
9156 val
.r_rel
.target_offset
);
9158 /* If it is not unreachable and we have not yet
9159 seen an unreachable at this address, place it
9160 before the fill address. */
9162 || (bfd_get_32 (abfd
, flags_p
)
9163 & XTENSA_PROP_UNREACHABLE
) == 0)
9164 new_end_offset
= new_offset
;
9167 last_zfill_target_sec
= target_sec
;
9168 last_zfill_target_offset
= val
.r_rel
.target_offset
;
9174 new_end_offset
= offset_with_removed_text_before_fill
9175 (&target_relax_info
->action_list
,
9176 val
.r_rel
.target_offset
+ old_size
);
9179 new_size
= new_end_offset
- new_offset
;
9181 if (new_size
!= old_size
)
9183 bfd_put_32 (abfd
, new_size
, size_p
);
9184 pin_contents (sec
, contents
);
9187 if (new_offset
!= val
.r_rel
.target_offset
)
9189 bfd_vma diff
= new_offset
- val
.r_rel
.target_offset
;
9190 irel
->r_addend
+= diff
;
9191 pin_internal_relocs (sec
, internal_relocs
);
9197 /* Combine adjacent property table entries. This is also done in
9198 finish_dynamic_sections() but at that point it's too late to
9199 reclaim the space in the output section, so we do this twice. */
9201 if (internal_relocs
&& (!link_info
->relocatable
9202 || strcmp (sec
->name
, XTENSA_LIT_SEC_NAME
) == 0))
9204 Elf_Internal_Rela
*last_irel
= NULL
;
9205 int removed_bytes
= 0;
9206 bfd_vma offset
, last_irel_offset
;
9207 bfd_vma section_size
;
9208 bfd_size_type entry_size
;
9209 flagword predef_flags
;
9211 if (is_full_prop_section
)
9216 predef_flags
= xtensa_get_property_predef_flags (sec
);
9218 /* Walk over memory and irels at the same time.
9219 This REQUIRES that the internal_relocs be sorted by offset. */
9220 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
9221 internal_reloc_compare
);
9222 nexti
= 0; /* Index into internal_relocs. */
9224 pin_internal_relocs (sec
, internal_relocs
);
9225 pin_contents (sec
, contents
);
9227 last_irel_offset
= (bfd_vma
) -1;
9228 section_size
= sec
->size
;
9229 BFD_ASSERT (section_size
% entry_size
== 0);
9231 for (offset
= 0; offset
< section_size
; offset
+= entry_size
)
9233 Elf_Internal_Rela
*irel
, *next_irel
;
9234 bfd_vma bytes_to_remove
, size
, actual_offset
;
9235 bfd_boolean remove_this_irel
;
9241 /* Find the next two relocations (if there are that many left),
9242 skipping over any R_XTENSA_NONE relocs. On entry, "nexti" is
9243 the starting reloc index. After these two loops, "i"
9244 is the index of the first non-NONE reloc past that starting
9245 index, and "nexti" is the index for the next non-NONE reloc
9248 for (i
= nexti
; i
< sec
->reloc_count
; i
++)
9250 if (ELF32_R_TYPE (internal_relocs
[i
].r_info
) != R_XTENSA_NONE
)
9252 irel
= &internal_relocs
[i
];
9255 internal_relocs
[i
].r_offset
-= removed_bytes
;
9258 for (nexti
= i
+ 1; nexti
< sec
->reloc_count
; nexti
++)
9260 if (ELF32_R_TYPE (internal_relocs
[nexti
].r_info
)
9263 next_irel
= &internal_relocs
[nexti
];
9266 internal_relocs
[nexti
].r_offset
-= removed_bytes
;
9269 remove_this_irel
= FALSE
;
9270 bytes_to_remove
= 0;
9271 actual_offset
= offset
- removed_bytes
;
9272 size
= bfd_get_32 (abfd
, &contents
[actual_offset
+ 4]);
9274 if (is_full_prop_section
)
9275 flags
= bfd_get_32 (abfd
, &contents
[actual_offset
+ 8]);
9277 flags
= predef_flags
;
9279 /* Check that the irels are sorted by offset,
9280 with only one per address. */
9281 BFD_ASSERT (!irel
|| (int) irel
->r_offset
> (int) last_irel_offset
);
9282 BFD_ASSERT (!next_irel
|| next_irel
->r_offset
> irel
->r_offset
);
9284 /* Make sure there aren't relocs on the size or flag fields. */
9285 if ((irel
&& irel
->r_offset
== offset
+ 4)
9286 || (is_full_prop_section
9287 && irel
&& irel
->r_offset
== offset
+ 8))
9289 irel
->r_offset
-= removed_bytes
;
9290 last_irel_offset
= irel
->r_offset
;
9292 else if (next_irel
&& (next_irel
->r_offset
== offset
+ 4
9293 || (is_full_prop_section
9294 && next_irel
->r_offset
== offset
+ 8)))
9297 irel
->r_offset
-= removed_bytes
;
9298 next_irel
->r_offset
-= removed_bytes
;
9299 last_irel_offset
= next_irel
->r_offset
;
9301 else if (size
== 0 && (flags
& XTENSA_PROP_ALIGN
) == 0
9302 && (flags
& XTENSA_PROP_UNREACHABLE
) == 0)
9304 /* Always remove entries with zero size and no alignment. */
9305 bytes_to_remove
= entry_size
;
9306 if (irel
&& irel
->r_offset
== offset
)
9308 remove_this_irel
= TRUE
;
9310 irel
->r_offset
-= removed_bytes
;
9311 last_irel_offset
= irel
->r_offset
;
9314 else if (irel
&& irel
->r_offset
== offset
)
9316 if (ELF32_R_TYPE (irel
->r_info
) == R_XTENSA_32
)
9322 bfd_get_32 (abfd
, &contents
[last_irel
->r_offset
+ 4]);
9323 bfd_vma old_address
=
9324 (last_irel
->r_addend
9325 + bfd_get_32 (abfd
, &contents
[last_irel
->r_offset
]));
9326 bfd_vma new_address
=
9328 + bfd_get_32 (abfd
, &contents
[actual_offset
]));
9329 if (is_full_prop_section
)
9330 old_flags
= bfd_get_32
9331 (abfd
, &contents
[last_irel
->r_offset
+ 8]);
9333 old_flags
= predef_flags
;
9335 if ((ELF32_R_SYM (irel
->r_info
)
9336 == ELF32_R_SYM (last_irel
->r_info
))
9337 && old_address
+ old_size
== new_address
9338 && old_flags
== flags
9339 && (old_flags
& XTENSA_PROP_INSN_BRANCH_TARGET
) == 0
9340 && (old_flags
& XTENSA_PROP_INSN_LOOP_TARGET
) == 0)
9342 /* Fix the old size. */
9343 bfd_put_32 (abfd
, old_size
+ size
,
9344 &contents
[last_irel
->r_offset
+ 4]);
9345 bytes_to_remove
= entry_size
;
9346 remove_this_irel
= TRUE
;
9355 irel
->r_offset
-= removed_bytes
;
9356 last_irel_offset
= irel
->r_offset
;
9359 if (remove_this_irel
)
9361 irel
->r_info
= ELF32_R_INFO (0, R_XTENSA_NONE
);
9362 irel
->r_offset
-= bytes_to_remove
;
9365 if (bytes_to_remove
!= 0)
9367 removed_bytes
+= bytes_to_remove
;
9368 if (offset
+ bytes_to_remove
< section_size
)
9369 memmove (&contents
[actual_offset
],
9370 &contents
[actual_offset
+ bytes_to_remove
],
9371 section_size
- offset
- bytes_to_remove
);
9377 /* Clear the removed bytes. */
9378 memset (&contents
[section_size
- removed_bytes
], 0, removed_bytes
);
9380 sec
->size
= section_size
- removed_bytes
;
9382 if (xtensa_is_littable_section (sec
))
9384 bfd
*dynobj
= elf_hash_table (link_info
)->dynobj
;
9388 bfd_get_section_by_name (dynobj
, ".got.loc");
9390 sgotloc
->size
-= removed_bytes
;
9397 release_internal_relocs (sec
, internal_relocs
);
9398 release_contents (sec
, contents
);
9403 /* Third relaxation pass. */
9405 /* Change symbol values to account for removed literals. */
9408 relax_section_symbols (abfd
, sec
)
9412 xtensa_relax_info
*relax_info
;
9413 unsigned int sec_shndx
;
9414 Elf_Internal_Shdr
*symtab_hdr
;
9415 Elf_Internal_Sym
*isymbuf
;
9416 unsigned i
, num_syms
, num_locals
;
9418 relax_info
= get_xtensa_relax_info (sec
);
9419 BFD_ASSERT (relax_info
);
9421 if (!relax_info
->is_relaxable_literal_section
9422 && !relax_info
->is_relaxable_asm_section
)
9425 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
9427 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9428 isymbuf
= retrieve_local_syms (abfd
);
9430 num_syms
= symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
9431 num_locals
= symtab_hdr
->sh_info
;
9433 /* Adjust the local symbols defined in this section. */
9434 for (i
= 0; i
< num_locals
; i
++)
9436 Elf_Internal_Sym
*isym
= &isymbuf
[i
];
9438 if (isym
->st_shndx
== sec_shndx
)
9440 bfd_vma new_address
= offset_with_removed_text
9441 (&relax_info
->action_list
, isym
->st_value
);
9442 bfd_vma new_size
= isym
->st_size
;
9444 if (ELF32_ST_TYPE (isym
->st_info
) == STT_FUNC
)
9446 bfd_vma new_end
= offset_with_removed_text
9447 (&relax_info
->action_list
, isym
->st_value
+ isym
->st_size
);
9448 new_size
= new_end
- new_address
;
9451 isym
->st_value
= new_address
;
9452 isym
->st_size
= new_size
;
9456 /* Now adjust the global symbols defined in this section. */
9457 for (i
= 0; i
< (num_syms
- num_locals
); i
++)
9459 struct elf_link_hash_entry
*sym_hash
;
9461 sym_hash
= elf_sym_hashes (abfd
)[i
];
9463 if (sym_hash
->root
.type
== bfd_link_hash_warning
)
9464 sym_hash
= (struct elf_link_hash_entry
*) sym_hash
->root
.u
.i
.link
;
9466 if ((sym_hash
->root
.type
== bfd_link_hash_defined
9467 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
9468 && sym_hash
->root
.u
.def
.section
== sec
)
9470 bfd_vma new_address
= offset_with_removed_text
9471 (&relax_info
->action_list
, sym_hash
->root
.u
.def
.value
);
9472 bfd_vma new_size
= sym_hash
->size
;
9474 if (sym_hash
->type
== STT_FUNC
)
9476 bfd_vma new_end
= offset_with_removed_text
9477 (&relax_info
->action_list
,
9478 sym_hash
->root
.u
.def
.value
+ sym_hash
->size
);
9479 new_size
= new_end
- new_address
;
9482 sym_hash
->root
.u
.def
.value
= new_address
;
9483 sym_hash
->size
= new_size
;
9491 /* "Fix" handling functions, called while performing relocations. */
9494 do_fix_for_relocatable_link (rel
, input_bfd
, input_section
, contents
)
9495 Elf_Internal_Rela
*rel
;
9497 asection
*input_section
;
9501 asection
*sec
, *old_sec
;
9503 int r_type
= ELF32_R_TYPE (rel
->r_info
);
9506 if (r_type
== R_XTENSA_NONE
)
9509 fix
= get_bfd_fix (input_section
, rel
->r_offset
, r_type
);
9513 r_reloc_init (&r_rel
, input_bfd
, rel
, contents
,
9514 bfd_get_section_limit (input_bfd
, input_section
));
9515 old_sec
= r_reloc_get_section (&r_rel
);
9516 old_offset
= r_rel
.target_offset
;
9518 if (!old_sec
|| !r_reloc_is_defined (&r_rel
))
9520 if (r_type
!= R_XTENSA_ASM_EXPAND
)
9522 (*_bfd_error_handler
)
9523 (_("%B(%A+0x%lx): unexpected fix for %s relocation"),
9524 input_bfd
, input_section
, rel
->r_offset
,
9525 elf_howto_table
[r_type
].name
);
9528 /* Leave it be. Resolution will happen in a later stage. */
9532 sec
= fix
->target_sec
;
9533 rel
->r_addend
+= ((sec
->output_offset
+ fix
->target_offset
)
9534 - (old_sec
->output_offset
+ old_offset
));
9541 do_fix_for_final_link (rel
, input_bfd
, input_section
, contents
, relocationp
)
9542 Elf_Internal_Rela
*rel
;
9544 asection
*input_section
;
9546 bfd_vma
*relocationp
;
9549 int r_type
= ELF32_R_TYPE (rel
->r_info
);
9553 if (r_type
== R_XTENSA_NONE
)
9556 fix
= get_bfd_fix (input_section
, rel
->r_offset
, r_type
);
9560 sec
= fix
->target_sec
;
9562 fixup_diff
= rel
->r_addend
;
9563 if (elf_howto_table
[fix
->src_type
].partial_inplace
)
9565 bfd_vma inplace_val
;
9566 BFD_ASSERT (fix
->src_offset
9567 < bfd_get_section_limit (input_bfd
, input_section
));
9568 inplace_val
= bfd_get_32 (input_bfd
, &contents
[fix
->src_offset
]);
9569 fixup_diff
+= inplace_val
;
9572 *relocationp
= (sec
->output_section
->vma
9573 + sec
->output_offset
9574 + fix
->target_offset
- fixup_diff
);
9578 /* Miscellaneous utility functions.... */
9581 elf_xtensa_get_plt_section (dynobj
, chunk
)
9588 return bfd_get_section_by_name (dynobj
, ".plt");
9590 sprintf (plt_name
, ".plt.%u", chunk
);
9591 return bfd_get_section_by_name (dynobj
, plt_name
);
9596 elf_xtensa_get_gotplt_section (dynobj
, chunk
)
9603 return bfd_get_section_by_name (dynobj
, ".got.plt");
9605 sprintf (got_name
, ".got.plt.%u", chunk
);
9606 return bfd_get_section_by_name (dynobj
, got_name
);
9610 /* Get the input section for a given symbol index.
9612 . a section symbol, return the section;
9613 . a common symbol, return the common section;
9614 . an undefined symbol, return the undefined section;
9615 . an indirect symbol, follow the links;
9616 . an absolute value, return the absolute section. */
9619 get_elf_r_symndx_section (abfd
, r_symndx
)
9621 unsigned long r_symndx
;
9623 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9624 asection
*target_sec
= NULL
;
9625 if (r_symndx
< symtab_hdr
->sh_info
)
9627 Elf_Internal_Sym
*isymbuf
;
9628 unsigned int section_index
;
9630 isymbuf
= retrieve_local_syms (abfd
);
9631 section_index
= isymbuf
[r_symndx
].st_shndx
;
9633 if (section_index
== SHN_UNDEF
)
9634 target_sec
= bfd_und_section_ptr
;
9635 else if (section_index
> 0 && section_index
< SHN_LORESERVE
)
9636 target_sec
= bfd_section_from_elf_index (abfd
, section_index
);
9637 else if (section_index
== SHN_ABS
)
9638 target_sec
= bfd_abs_section_ptr
;
9639 else if (section_index
== SHN_COMMON
)
9640 target_sec
= bfd_com_section_ptr
;
9647 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
9648 struct elf_link_hash_entry
*h
= elf_sym_hashes (abfd
)[indx
];
9650 while (h
->root
.type
== bfd_link_hash_indirect
9651 || h
->root
.type
== bfd_link_hash_warning
)
9652 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9654 switch (h
->root
.type
)
9656 case bfd_link_hash_defined
:
9657 case bfd_link_hash_defweak
:
9658 target_sec
= h
->root
.u
.def
.section
;
9660 case bfd_link_hash_common
:
9661 target_sec
= bfd_com_section_ptr
;
9663 case bfd_link_hash_undefined
:
9664 case bfd_link_hash_undefweak
:
9665 target_sec
= bfd_und_section_ptr
;
9667 default: /* New indirect warning. */
9668 target_sec
= bfd_und_section_ptr
;
9676 static struct elf_link_hash_entry
*
9677 get_elf_r_symndx_hash_entry (abfd
, r_symndx
)
9679 unsigned long r_symndx
;
9682 struct elf_link_hash_entry
*h
;
9683 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9685 if (r_symndx
< symtab_hdr
->sh_info
)
9688 indx
= r_symndx
- symtab_hdr
->sh_info
;
9689 h
= elf_sym_hashes (abfd
)[indx
];
9690 while (h
->root
.type
== bfd_link_hash_indirect
9691 || h
->root
.type
== bfd_link_hash_warning
)
9692 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9697 /* Get the section-relative offset for a symbol number. */
9700 get_elf_r_symndx_offset (abfd
, r_symndx
)
9702 unsigned long r_symndx
;
9704 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9707 if (r_symndx
< symtab_hdr
->sh_info
)
9709 Elf_Internal_Sym
*isymbuf
;
9710 isymbuf
= retrieve_local_syms (abfd
);
9711 offset
= isymbuf
[r_symndx
].st_value
;
9715 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
9716 struct elf_link_hash_entry
*h
=
9717 elf_sym_hashes (abfd
)[indx
];
9719 while (h
->root
.type
== bfd_link_hash_indirect
9720 || h
->root
.type
== bfd_link_hash_warning
)
9721 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9722 if (h
->root
.type
== bfd_link_hash_defined
9723 || h
->root
.type
== bfd_link_hash_defweak
)
9724 offset
= h
->root
.u
.def
.value
;
9731 is_reloc_sym_weak (abfd
, rel
)
9733 Elf_Internal_Rela
*rel
;
9735 unsigned long r_symndx
= ELF32_R_SYM (rel
->r_info
);
9736 struct elf_link_hash_entry
*h
;
9738 h
= get_elf_r_symndx_hash_entry (abfd
, r_symndx
);
9739 if (h
&& h
->root
.type
== bfd_link_hash_defweak
)
9746 pcrel_reloc_fits (opc
, opnd
, self_address
, dest_address
)
9749 bfd_vma self_address
;
9750 bfd_vma dest_address
;
9752 xtensa_isa isa
= xtensa_default_isa
;
9753 uint32 valp
= dest_address
;
9754 if (xtensa_operand_do_reloc (isa
, opc
, opnd
, &valp
, self_address
)
9755 || xtensa_operand_encode (isa
, opc
, opnd
, &valp
))
9761 static int linkonce_len
= sizeof (".gnu.linkonce.") - 1;
9762 static int insn_sec_len
= sizeof (XTENSA_INSN_SEC_NAME
) - 1;
9763 static int lit_sec_len
= sizeof (XTENSA_LIT_SEC_NAME
) - 1;
9764 static int prop_sec_len
= sizeof (XTENSA_PROP_SEC_NAME
) - 1;
9768 xtensa_is_property_section (sec
)
9771 if (strncmp (XTENSA_INSN_SEC_NAME
, sec
->name
, insn_sec_len
) == 0
9772 || strncmp (XTENSA_LIT_SEC_NAME
, sec
->name
, lit_sec_len
) == 0
9773 || strncmp (XTENSA_PROP_SEC_NAME
, sec
->name
, prop_sec_len
) == 0)
9776 if (strncmp (".gnu.linkonce.", sec
->name
, linkonce_len
) == 0
9777 && (strncmp (&sec
->name
[linkonce_len
], "x.", 2) == 0
9778 || strncmp (&sec
->name
[linkonce_len
], "p.", 2) == 0
9779 || strncmp (&sec
->name
[linkonce_len
], "prop.", 5) == 0))
9787 xtensa_is_littable_section (sec
)
9790 if (strncmp (XTENSA_LIT_SEC_NAME
, sec
->name
, lit_sec_len
) == 0)
9793 if (strncmp (".gnu.linkonce.", sec
->name
, linkonce_len
) == 0
9794 && sec
->name
[linkonce_len
] == 'p'
9795 && sec
->name
[linkonce_len
+ 1] == '.')
9803 internal_reloc_compare (ap
, bp
)
9807 const Elf_Internal_Rela
*a
= (const Elf_Internal_Rela
*) ap
;
9808 const Elf_Internal_Rela
*b
= (const Elf_Internal_Rela
*) bp
;
9810 if (a
->r_offset
!= b
->r_offset
)
9811 return (a
->r_offset
- b
->r_offset
);
9813 /* We don't need to sort on these criteria for correctness,
9814 but enforcing a more strict ordering prevents unstable qsort
9815 from behaving differently with different implementations.
9816 Without the code below we get correct but different results
9817 on Solaris 2.7 and 2.8. We would like to always produce the
9818 same results no matter the host. */
9820 if (a
->r_info
!= b
->r_info
)
9821 return (a
->r_info
- b
->r_info
);
9823 return (a
->r_addend
- b
->r_addend
);
9828 internal_reloc_matches (ap
, bp
)
9832 const Elf_Internal_Rela
*a
= (const Elf_Internal_Rela
*) ap
;
9833 const Elf_Internal_Rela
*b
= (const Elf_Internal_Rela
*) bp
;
9835 /* Check if one entry overlaps with the other; this shouldn't happen
9836 except when searching for a match. */
9837 return (a
->r_offset
- b
->r_offset
);
9842 xtensa_get_property_section_name (sec
, base_name
)
9844 const char *base_name
;
9846 if (strncmp (sec
->name
, ".gnu.linkonce.", linkonce_len
) == 0)
9848 char *prop_sec_name
;
9850 char *linkonce_kind
= 0;
9852 if (strcmp (base_name
, XTENSA_INSN_SEC_NAME
) == 0)
9853 linkonce_kind
= "x";
9854 else if (strcmp (base_name
, XTENSA_LIT_SEC_NAME
) == 0)
9855 linkonce_kind
= "p";
9856 else if (strcmp (base_name
, XTENSA_PROP_SEC_NAME
) == 0)
9857 linkonce_kind
= "prop.";
9861 prop_sec_name
= (char *) bfd_malloc (strlen (sec
->name
)
9862 + strlen (linkonce_kind
) + 1);
9863 memcpy (prop_sec_name
, ".gnu.linkonce.", linkonce_len
);
9864 strcpy (prop_sec_name
+ linkonce_len
, linkonce_kind
);
9866 suffix
= sec
->name
+ linkonce_len
;
9867 /* For backward compatibility, replace "t." instead of inserting
9868 the new linkonce_kind (but not for "prop" sections). */
9869 if (strncmp (suffix
, "t.", 2) == 0 && linkonce_kind
[1] == '.')
9871 strcat (prop_sec_name
+ linkonce_len
, suffix
);
9873 return prop_sec_name
;
9876 return strdup (base_name
);
9881 xtensa_get_property_predef_flags (sec
)
9884 if (strcmp (sec
->name
, XTENSA_INSN_SEC_NAME
) == 0
9885 || strncmp (sec
->name
, ".gnu.linkonce.x.",
9886 sizeof ".gnu.linkonce.x." - 1) == 0)
9887 return (XTENSA_PROP_INSN
9888 | XTENSA_PROP_INSN_NO_TRANSFORM
9889 | XTENSA_PROP_INSN_NO_REORDER
);
9891 if (xtensa_is_littable_section (sec
))
9892 return (XTENSA_PROP_LITERAL
9893 | XTENSA_PROP_INSN_NO_TRANSFORM
9894 | XTENSA_PROP_INSN_NO_REORDER
);
9900 /* Other functions called directly by the linker. */
9903 xtensa_callback_required_dependence (abfd
, sec
, link_info
, callback
, closure
)
9906 struct bfd_link_info
*link_info
;
9907 deps_callback_t callback
;
9910 Elf_Internal_Rela
*internal_relocs
;
9913 bfd_boolean ok
= TRUE
;
9914 bfd_size_type sec_size
;
9916 sec_size
= bfd_get_section_limit (abfd
, sec
);
9918 /* ".plt*" sections have no explicit relocations but they contain L32R
9919 instructions that reference the corresponding ".got.plt*" sections. */
9920 if ((sec
->flags
& SEC_LINKER_CREATED
) != 0
9921 && strncmp (sec
->name
, ".plt", 4) == 0)
9925 /* Find the corresponding ".got.plt*" section. */
9926 if (sec
->name
[4] == '\0')
9927 sgotplt
= bfd_get_section_by_name (sec
->owner
, ".got.plt");
9933 BFD_ASSERT (sec
->name
[4] == '.');
9934 chunk
= strtol (&sec
->name
[5], NULL
, 10);
9936 sprintf (got_name
, ".got.plt.%u", chunk
);
9937 sgotplt
= bfd_get_section_by_name (sec
->owner
, got_name
);
9939 BFD_ASSERT (sgotplt
);
9941 /* Assume worst-case offsets: L32R at the very end of the ".plt"
9942 section referencing a literal at the very beginning of
9943 ".got.plt". This is very close to the real dependence, anyway. */
9944 (*callback
) (sec
, sec_size
, sgotplt
, 0, closure
);
9947 internal_relocs
= retrieve_internal_relocs (abfd
, sec
,
9948 link_info
->keep_memory
);
9949 if (internal_relocs
== NULL
9950 || sec
->reloc_count
== 0)
9953 /* Cache the contents for the duration of this scan. */
9954 contents
= retrieve_contents (abfd
, sec
, link_info
->keep_memory
);
9955 if (contents
== NULL
&& sec_size
!= 0)
9961 if (!xtensa_default_isa
)
9962 xtensa_default_isa
= xtensa_isa_init (0, 0);
9964 for (i
= 0; i
< sec
->reloc_count
; i
++)
9966 Elf_Internal_Rela
*irel
= &internal_relocs
[i
];
9967 if (is_l32r_relocation (abfd
, sec
, contents
, irel
))
9970 asection
*target_sec
;
9971 bfd_vma target_offset
;
9973 r_reloc_init (&l32r_rel
, abfd
, irel
, contents
, sec_size
);
9976 /* L32Rs must be local to the input file. */
9977 if (r_reloc_is_defined (&l32r_rel
))
9979 target_sec
= r_reloc_get_section (&l32r_rel
);
9980 target_offset
= l32r_rel
.target_offset
;
9982 (*callback
) (sec
, irel
->r_offset
, target_sec
, target_offset
,
9988 release_internal_relocs (sec
, internal_relocs
);
9989 release_contents (sec
, contents
);
9993 /* The default literal sections should always be marked as "code" (i.e.,
9994 SHF_EXECINSTR). This is particularly important for the Linux kernel
9995 module loader so that the literals are not placed after the text. */
9996 static struct bfd_elf_special_section
const elf_xtensa_special_sections
[]=
9998 { ".literal", 8, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
9999 { ".init.literal", 13, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
10000 { ".fini.literal", 13, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
10001 { NULL
, 0, 0, 0, 0 }
10006 #define TARGET_LITTLE_SYM bfd_elf32_xtensa_le_vec
10007 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
10008 #define TARGET_BIG_SYM bfd_elf32_xtensa_be_vec
10009 #define TARGET_BIG_NAME "elf32-xtensa-be"
10010 #define ELF_ARCH bfd_arch_xtensa
10012 /* The new EM_XTENSA value will be recognized beginning in the Xtensa T1040
10013 release. However, we still have to generate files with the EM_XTENSA_OLD
10014 value so that pre-T1040 tools can read the files. As soon as we stop
10015 caring about pre-T1040 tools, the following two values should be
10016 swapped. At the same time, any other code that uses EM_XTENSA_OLD
10017 should be changed to use EM_XTENSA. */
10018 #define ELF_MACHINE_CODE EM_XTENSA_OLD
10019 #define ELF_MACHINE_ALT1 EM_XTENSA
10022 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
10023 #else /* !XCHAL_HAVE_MMU */
10024 #define ELF_MAXPAGESIZE 1
10025 #endif /* !XCHAL_HAVE_MMU */
10026 #endif /* ELF_ARCH */
10028 #define elf_backend_can_gc_sections 1
10029 #define elf_backend_can_refcount 1
10030 #define elf_backend_plt_readonly 1
10031 #define elf_backend_got_header_size 4
10032 #define elf_backend_want_dynbss 0
10033 #define elf_backend_want_got_plt 1
10035 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
10037 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
10038 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
10039 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
10040 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
10041 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
10042 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
10044 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
10045 #define elf_backend_check_relocs elf_xtensa_check_relocs
10046 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
10047 #define elf_backend_discard_info elf_xtensa_discard_info
10048 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
10049 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
10050 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
10051 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
10052 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
10053 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
10054 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
10055 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
10056 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
10057 #define elf_backend_modify_segment_map elf_xtensa_modify_segment_map
10058 #define elf_backend_object_p elf_xtensa_object_p
10059 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
10060 #define elf_backend_relocate_section elf_xtensa_relocate_section
10061 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
10062 #define elf_backend_special_sections elf_xtensa_special_sections
10064 #include "elf32-target.h"