]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf32-xtensa.c
bfd ChangeLog
[thirdparty/binutils-gdb.git] / bfd / elf32-xtensa.c
1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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
19 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23
24 #ifdef ANSI_PROTOTYPES
25 #include <stdarg.h>
26 #else
27 #include <varargs.h>
28 #endif
29 #include <strings.h>
30
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/xtensa.h"
35 #include "xtensa-isa.h"
36 #include "xtensa-config.h"
37
38 #define XTENSA_NO_NOP_REMOVAL 0
39
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 *,
47 bfd_boolean));
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 *,
74 Elf_Internal_Sym *));
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
82 PARAMS ((bfd *));
83 static bfd_boolean elf_xtensa_print_private_bfd_data
84 PARAMS ((bfd *, PTR));
85 static bfd_boolean elf_xtensa_object_p
86 PARAMS ((bfd *));
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 *));
101
102
103 /* Local helper functions. */
104
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 *,
146 asection *));
147
148 /* Local functions to handle Xtensa configurability. */
149
150 static void init_call_opcodes
151 PARAMS ((void));
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
159 PARAMS ((void));
160 static xtensa_opcode get_l32r_opcode
161 PARAMS ((void));
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
167 PARAMS ((int));
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
173 PARAMS ((int));
174 static bfd_boolean is_operand_relocation
175 PARAMS ((int));
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));
188
189 /* Functions for link-time code simplifications. */
190
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 *));
199
200 /* Access to internal relocations, section contents and symbols. */
201
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
215 PARAMS ((bfd *));
216
217 /* Miscellaneous utility functions. */
218
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 *));
245
246 /* Other functions called directly by the linker. */
247
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));
253
254
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;
260
261
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;
266
267
268
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. */
275
276 static int plt_reloc_count = 0;
277
278
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
282 this information. */
283
284 xtensa_isa xtensa_default_isa;
285
286
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. */
290
291 static bfd_boolean relaxing_section = FALSE;
292
293 /* When this is true, during final links, literals that cannot be
294 coalesced and their relocations may be moved to other sections. */
295
296 int elf32xtensa_no_literal_movement = 1;
297
298 \f
299 static reloc_howto_type elf_howto_table[] =
300 {
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),
327 EMPTY_HOWTO (7),
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),
345 EMPTY_HOWTO (13),
346 EMPTY_HOWTO (14),
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),
355
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),
366
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),
413
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)
460 };
461
462 #if DEBUG_GEN_RELOC
463 #define TRACE(str) \
464 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
465 #else
466 #define TRACE(str)
467 #endif
468
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;
473 {
474 switch (code)
475 {
476 case BFD_RELOC_NONE:
477 TRACE ("BFD_RELOC_NONE");
478 return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
479
480 case BFD_RELOC_32:
481 TRACE ("BFD_RELOC_32");
482 return &elf_howto_table[(unsigned) R_XTENSA_32 ];
483
484 case BFD_RELOC_XTENSA_DIFF8:
485 TRACE ("BFD_RELOC_XTENSA_DIFF8");
486 return &elf_howto_table[(unsigned) R_XTENSA_DIFF8 ];
487
488 case BFD_RELOC_XTENSA_DIFF16:
489 TRACE ("BFD_RELOC_XTENSA_DIFF16");
490 return &elf_howto_table[(unsigned) R_XTENSA_DIFF16 ];
491
492 case BFD_RELOC_XTENSA_DIFF32:
493 TRACE ("BFD_RELOC_XTENSA_DIFF32");
494 return &elf_howto_table[(unsigned) R_XTENSA_DIFF32 ];
495
496 case BFD_RELOC_XTENSA_RTLD:
497 TRACE ("BFD_RELOC_XTENSA_RTLD");
498 return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
499
500 case BFD_RELOC_XTENSA_GLOB_DAT:
501 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
502 return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
503
504 case BFD_RELOC_XTENSA_JMP_SLOT:
505 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
506 return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
507
508 case BFD_RELOC_XTENSA_RELATIVE:
509 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
510 return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
511
512 case BFD_RELOC_XTENSA_PLT:
513 TRACE ("BFD_RELOC_XTENSA_PLT");
514 return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
515
516 case BFD_RELOC_XTENSA_OP0:
517 TRACE ("BFD_RELOC_XTENSA_OP0");
518 return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
519
520 case BFD_RELOC_XTENSA_OP1:
521 TRACE ("BFD_RELOC_XTENSA_OP1");
522 return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
523
524 case BFD_RELOC_XTENSA_OP2:
525 TRACE ("BFD_RELOC_XTENSA_OP2");
526 return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
527
528 case BFD_RELOC_XTENSA_ASM_EXPAND:
529 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
530 return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
531
532 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
533 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
534 return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
535
536 case BFD_RELOC_VTABLE_INHERIT:
537 TRACE ("BFD_RELOC_VTABLE_INHERIT");
538 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
539
540 case BFD_RELOC_VTABLE_ENTRY:
541 TRACE ("BFD_RELOC_VTABLE_ENTRY");
542 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
543
544 default:
545 if (code >= BFD_RELOC_XTENSA_SLOT0_OP
546 && code <= BFD_RELOC_XTENSA_SLOT14_OP)
547 {
548 unsigned n = (R_XTENSA_SLOT0_OP +
549 (code - BFD_RELOC_XTENSA_SLOT0_OP));
550 return &elf_howto_table[n];
551 }
552
553 if (code >= BFD_RELOC_XTENSA_SLOT0_ALT
554 && code <= BFD_RELOC_XTENSA_SLOT14_ALT)
555 {
556 unsigned n = (R_XTENSA_SLOT0_ALT +
557 (code - BFD_RELOC_XTENSA_SLOT0_ALT));
558 return &elf_howto_table[n];
559 }
560
561 break;
562 }
563
564 TRACE ("Unknown");
565 return NULL;
566 }
567
568
569 /* Given an ELF "rela" relocation, find the corresponding howto and record
570 it in the BFD internal arelent representation of the relocation. */
571
572 static void
573 elf_xtensa_info_to_howto_rela (abfd, cache_ptr, dst)
574 bfd *abfd ATTRIBUTE_UNUSED;
575 arelent *cache_ptr;
576 Elf_Internal_Rela *dst;
577 {
578 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
579
580 BFD_ASSERT (r_type < (unsigned int) R_XTENSA_max);
581 cache_ptr->howto = &elf_howto_table[r_type];
582 }
583
584 \f
585 /* Functions for the Xtensa ELF linker. */
586
587 /* The name of the dynamic interpreter. This is put in the .interp
588 section. */
589
590 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
591
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
594 the PLT entry.) */
595
596 #define PLT_ENTRY_SIZE 16
597
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. */
607
608 #define PLT_ENTRIES_PER_CHUNK 254
609
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. */
614
615 static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
616 {
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 */
622 0 /* unused */
623 };
624
625 static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
626 {
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 */
632 0 /* unused */
633 };
634
635
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;
640 {
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. */
646
647 return _bfd_elf_dynamic_symbol_p (h, info, 0);
648 }
649
650 \f
651 static int
652 property_table_compare (ap, bp)
653 const PTR ap;
654 const PTR bp;
655 {
656 const property_table_entry *a = (const property_table_entry *) ap;
657 const property_table_entry *b = (const property_table_entry *) bp;
658
659 if (a->address == b->address)
660 {
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
664 come first. */
665 BFD_ASSERT ((a->size == 0 || b->size == 0));
666
667 if (a->size != b->size)
668 return (a->size - b->size);
669
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));
673
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));
679
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));
684
685 return (a->flags - b->flags);
686 }
687
688 return (a->address - b->address);
689 }
690
691
692 static int
693 property_table_matches (ap, bp)
694 const PTR ap;
695 const PTR bp;
696 {
697 const property_table_entry *a = (const property_table_entry *) ap;
698 const property_table_entry *b = (const property_table_entry *) bp;
699
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)))
703 return 0;
704
705 return (a->address - b->address);
706 }
707
708
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. */
712
713 int
714 xtensa_read_table_entries (abfd, section, table_p, sec_name, output_addr)
715 bfd *abfd;
716 asection *section;
717 property_table_entry **table_p;
718 const char *sec_name;
719 bfd_boolean output_addr;
720 {
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;
726 int block_count;
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;
732
733 if (!section
734 || !(section->flags & SEC_ALLOC)
735 || (section->flags & SEC_DEBUGGING))
736 {
737 *table_p = NULL;
738 return 0;
739 }
740
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);
744 if (table_section)
745 table_size = table_section->size;
746
747 if (table_size == 0)
748 {
749 *table_p = NULL;
750 return 0;
751 }
752
753 predef_flags = xtensa_get_property_predef_flags (table_section);
754 table_entry_size = 12;
755 if (predef_flags)
756 table_entry_size -= 4;
757
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));
762 block_count = 0;
763
764 if (output_addr)
765 section_addr = section->output_section->vma + section->output_offset;
766 else
767 section_addr = section->vma;
768
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)
773 {
774 unsigned i;
775
776 for (i = 0; i < table_section->reloc_count; i++)
777 {
778 Elf_Internal_Rela *rel = &internal_relocs[i];
779 unsigned long r_symndx;
780
781 if (ELF32_R_TYPE (rel->r_info) == R_XTENSA_NONE)
782 continue;
783
784 BFD_ASSERT (ELF32_R_TYPE (rel->r_info) == R_XTENSA_32);
785 r_symndx = ELF32_R_SYM (rel->r_info);
786
787 if (get_elf_r_symndx_section (abfd, r_symndx) == section)
788 {
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);
797 if (predef_flags)
798 blocks[block_count].flags = predef_flags;
799 else
800 blocks[block_count].flags =
801 bfd_get_32 (abfd, table_data + rel->r_offset + 8);
802 block_count++;
803 }
804 }
805 }
806 else
807 {
808 /* The file has already been relocated and the addresses are
809 already in the table. */
810 bfd_vma off;
811 bfd_size_type section_limit = bfd_get_section_limit (abfd, section);
812
813 for (off = 0; off < table_size; off += table_entry_size)
814 {
815 bfd_vma address = bfd_get_32 (abfd, table_data + off);
816
817 if (address >= section_addr
818 && address < section_addr + section_limit)
819 {
820 blocks[block_count].address = address;
821 blocks[block_count].size =
822 bfd_get_32 (abfd, table_data + off + 4);
823 if (predef_flags)
824 blocks[block_count].flags = predef_flags;
825 else
826 blocks[block_count].flags =
827 bfd_get_32 (abfd, table_data + off + 8);
828 block_count++;
829 }
830 }
831 }
832
833 release_contents (table_section, table_data);
834 release_internal_relocs (table_section, internal_relocs);
835
836 if (block_count > 0)
837 {
838 /* Now sort them into address order for easy reference. */
839 qsort (blocks, block_count, sizeof (property_table_entry),
840 property_table_compare);
841 }
842
843 *table_p = blocks;
844 return block_count;
845 }
846
847
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;
852 bfd_vma addr;
853 {
854 property_table_entry entry;
855 property_table_entry *rv;
856
857 if (property_table_size == 0)
858 return NULL;
859
860 entry.address = addr;
861 entry.size = 1;
862 entry.flags = 0;
863
864 rv = bsearch (&entry, property_table, property_table_size,
865 sizeof (property_table_entry), property_table_matches);
866 return rv;
867 }
868
869
870 static bfd_boolean
871 elf_xtensa_in_literal_pool (lit_table, lit_table_size, addr)
872 property_table_entry *lit_table;
873 int lit_table_size;
874 bfd_vma addr;
875 {
876 if (elf_xtensa_find_property_entry (lit_table, lit_table_size, addr))
877 return TRUE;
878
879 return FALSE;
880 }
881
882 \f
883 /* Look through the relocs for a section during the first phase, and
884 calculate needed space in the dynamic reloc sections. */
885
886 static bfd_boolean
887 elf_xtensa_check_relocs (abfd, info, sec, relocs)
888 bfd *abfd;
889 struct bfd_link_info *info;
890 asection *sec;
891 const Elf_Internal_Rela *relocs;
892 {
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;
897
898 if (info->relocatable)
899 return TRUE;
900
901 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
902 sym_hashes = elf_sym_hashes (abfd);
903
904 rel_end = relocs + sec->reloc_count;
905 for (rel = relocs; rel < rel_end; rel++)
906 {
907 unsigned int r_type;
908 unsigned long r_symndx;
909 struct elf_link_hash_entry *h;
910
911 r_symndx = ELF32_R_SYM (rel->r_info);
912 r_type = ELF32_R_TYPE (rel->r_info);
913
914 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
915 {
916 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
917 abfd, r_symndx);
918 return FALSE;
919 }
920
921 if (r_symndx < symtab_hdr->sh_info)
922 h = NULL;
923 else
924 {
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;
929 }
930
931 switch (r_type)
932 {
933 case R_XTENSA_32:
934 if (h == NULL)
935 goto local_literal;
936
937 if ((sec->flags & SEC_ALLOC) != 0)
938 {
939 if (h->got.refcount <= 0)
940 h->got.refcount = 1;
941 else
942 h->got.refcount += 1;
943 }
944 break;
945
946 case R_XTENSA_PLT:
947 /* If this relocation is against a local symbol, then it's
948 exactly the same as a normal local GOT entry. */
949 if (h == NULL)
950 goto local_literal;
951
952 if ((sec->flags & SEC_ALLOC) != 0)
953 {
954 if (h->plt.refcount <= 0)
955 {
956 h->needs_plt = 1;
957 h->plt.refcount = 1;
958 }
959 else
960 h->plt.refcount += 1;
961
962 /* Keep track of the total PLT relocation count even if we
963 don't yet know whether the dynamic sections will be
964 created. */
965 plt_reloc_count += 1;
966
967 if (elf_hash_table (info)->dynamic_sections_created)
968 {
969 if (!add_extra_plt_sections (elf_hash_table (info)->dynobj,
970 plt_reloc_count))
971 return FALSE;
972 }
973 }
974 break;
975
976 local_literal:
977 if ((sec->flags & SEC_ALLOC) != 0)
978 {
979 bfd_signed_vma *local_got_refcounts;
980
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)
984 {
985 bfd_size_type size;
986
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)
992 return FALSE;
993 elf_local_got_refcounts (abfd) = local_got_refcounts;
994 }
995 local_got_refcounts[r_symndx] += 1;
996 }
997 break;
998
999 case R_XTENSA_OP0:
1000 case R_XTENSA_OP1:
1001 case R_XTENSA_OP2:
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. */
1038 break;
1039
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))
1044 return FALSE;
1045 break;
1046
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))
1051 return FALSE;
1052 break;
1053
1054 default:
1055 break;
1056 }
1057 }
1058
1059 return TRUE;
1060 }
1061
1062
1063 static void
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;
1068 {
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);
1072
1073 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1074 }
1075
1076
1077 /* Return the section that should be marked against GC for a given
1078 relocation. */
1079
1080 static asection *
1081 elf_xtensa_gc_mark_hook (sec, info, rel, h, sym)
1082 asection *sec;
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;
1087 {
1088 if (h != NULL)
1089 {
1090 switch (ELF32_R_TYPE (rel->r_info))
1091 {
1092 case R_XTENSA_GNU_VTINHERIT:
1093 case R_XTENSA_GNU_VTENTRY:
1094 break;
1095
1096 default:
1097 switch (h->root.type)
1098 {
1099 case bfd_link_hash_defined:
1100 case bfd_link_hash_defweak:
1101 return h->root.u.def.section;
1102
1103 case bfd_link_hash_common:
1104 return h->root.u.c.p->section;
1105
1106 default:
1107 break;
1108 }
1109 }
1110 }
1111 else
1112 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1113
1114 return NULL;
1115 }
1116
1117 /* Update the GOT & PLT entry reference counts
1118 for the section being removed. */
1119
1120 static bfd_boolean
1121 elf_xtensa_gc_sweep_hook (abfd, info, sec, relocs)
1122 bfd *abfd;
1123 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1124 asection *sec;
1125 const Elf_Internal_Rela *relocs;
1126 {
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;
1131
1132 if ((sec->flags & SEC_ALLOC) == 0)
1133 return TRUE;
1134
1135 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1136 sym_hashes = elf_sym_hashes (abfd);
1137 local_got_refcounts = elf_local_got_refcounts (abfd);
1138
1139 relend = relocs + sec->reloc_count;
1140 for (rel = relocs; rel < relend; rel++)
1141 {
1142 unsigned long r_symndx;
1143 unsigned int r_type;
1144 struct elf_link_hash_entry *h = NULL;
1145
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];
1149
1150 r_type = ELF32_R_TYPE (rel->r_info);
1151 switch (r_type)
1152 {
1153 case R_XTENSA_32:
1154 if (h == NULL)
1155 goto local_literal;
1156 if (h->got.refcount > 0)
1157 h->got.refcount--;
1158 break;
1159
1160 case R_XTENSA_PLT:
1161 if (h == NULL)
1162 goto local_literal;
1163 if (h->plt.refcount > 0)
1164 h->plt.refcount--;
1165 break;
1166
1167 local_literal:
1168 if (local_got_refcounts[r_symndx] > 0)
1169 local_got_refcounts[r_symndx] -= 1;
1170 break;
1171
1172 default:
1173 break;
1174 }
1175 }
1176
1177 return TRUE;
1178 }
1179
1180
1181 /* Create all the dynamic sections. */
1182
1183 static bfd_boolean
1184 elf_xtensa_create_dynamic_sections (dynobj, info)
1185 bfd *dynobj;
1186 struct bfd_link_info *info;
1187 {
1188 flagword flags, noalloc_flags;
1189 asection *s;
1190
1191 /* First do all the standard stuff. */
1192 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
1193 return FALSE;
1194
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))
1198 return FALSE;
1199
1200 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
1201 | SEC_LINKER_CREATED | SEC_READONLY);
1202 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
1203
1204 /* Mark the ".got.plt" section READONLY. */
1205 s = bfd_get_section_by_name (dynobj, ".got.plt");
1206 if (s == NULL
1207 || ! bfd_set_section_flags (dynobj, s, flags))
1208 return FALSE;
1209
1210 /* Create ".rela.got". */
1211 s = bfd_make_section (dynobj, ".rela.got");
1212 if (s == NULL
1213 || ! bfd_set_section_flags (dynobj, s, flags)
1214 || ! bfd_set_section_alignment (dynobj, s, 2))
1215 return FALSE;
1216
1217 /* Create ".got.loc" (literal tables for use by dynamic linker). */
1218 s = bfd_make_section (dynobj, ".got.loc");
1219 if (s == NULL
1220 || ! bfd_set_section_flags (dynobj, s, flags)
1221 || ! bfd_set_section_alignment (dynobj, s, 2))
1222 return FALSE;
1223
1224 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
1225 s = bfd_make_section (dynobj, ".xt.lit.plt");
1226 if (s == NULL
1227 || ! bfd_set_section_flags (dynobj, s, noalloc_flags)
1228 || ! bfd_set_section_alignment (dynobj, s, 2))
1229 return FALSE;
1230
1231 return TRUE;
1232 }
1233
1234
1235 static bfd_boolean
1236 add_extra_plt_sections (dynobj, count)
1237 bfd *dynobj;
1238 int count;
1239 {
1240 int chunk;
1241
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--)
1245 {
1246 char *sname;
1247 flagword flags;
1248 asection *s;
1249
1250 /* Stop when we find a section has already been created. */
1251 if (elf_xtensa_get_plt_section (dynobj, chunk))
1252 break;
1253
1254 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1255 | SEC_LINKER_CREATED | SEC_READONLY);
1256
1257 sname = (char *) bfd_malloc (10);
1258 sprintf (sname, ".plt.%u", chunk);
1259 s = bfd_make_section (dynobj, sname);
1260 if (s == NULL
1261 || ! bfd_set_section_flags (dynobj, s, flags | SEC_CODE)
1262 || ! bfd_set_section_alignment (dynobj, s, 2))
1263 return FALSE;
1264
1265 sname = (char *) bfd_malloc (14);
1266 sprintf (sname, ".got.plt.%u", chunk);
1267 s = bfd_make_section (dynobj, sname);
1268 if (s == NULL
1269 || ! bfd_set_section_flags (dynobj, s, flags)
1270 || ! bfd_set_section_alignment (dynobj, s, 2))
1271 return FALSE;
1272 }
1273
1274 return TRUE;
1275 }
1276
1277
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
1282 understand. */
1283
1284 static bfd_boolean
1285 elf_xtensa_adjust_dynamic_symbol (info, h)
1286 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1287 struct elf_link_hash_entry *h;
1288 {
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)
1293 {
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;
1298 return TRUE;
1299 }
1300
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,
1303 .dynbss, etc. */
1304
1305 return TRUE;
1306 }
1307
1308
1309 static void
1310 elf_xtensa_make_sym_local (info, h)
1311 struct bfd_link_info *info;
1312 struct elf_link_hash_entry *h;
1313 {
1314 if (info->shared)
1315 {
1316 if (h->plt.refcount > 0)
1317 {
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;
1323 }
1324 }
1325 else
1326 {
1327 /* Don't need any dynamic relocations at all. */
1328 h->plt.refcount = 0;
1329 h->got.refcount = 0;
1330 }
1331 }
1332
1333
1334 static bfd_boolean
1335 elf_xtensa_fix_refcounts (h, arg)
1336 struct elf_link_hash_entry *h;
1337 PTR arg;
1338 {
1339 struct bfd_link_info *info = (struct bfd_link_info *) arg;
1340
1341 if (h->root.type == bfd_link_hash_warning)
1342 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1343
1344 if (! xtensa_elf_dynamic_symbol_p (h, info))
1345 elf_xtensa_make_sym_local (info, h);
1346
1347 return TRUE;
1348 }
1349
1350
1351 static bfd_boolean
1352 elf_xtensa_allocate_plt_size (h, arg)
1353 struct elf_link_hash_entry *h;
1354 PTR arg;
1355 {
1356 asection *srelplt = (asection *) arg;
1357
1358 if (h->root.type == bfd_link_hash_warning)
1359 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1360
1361 if (h->plt.refcount > 0)
1362 srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1363
1364 return TRUE;
1365 }
1366
1367
1368 static bfd_boolean
1369 elf_xtensa_allocate_got_size (h, arg)
1370 struct elf_link_hash_entry *h;
1371 PTR arg;
1372 {
1373 asection *srelgot = (asection *) arg;
1374
1375 if (h->root.type == bfd_link_hash_warning)
1376 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1377
1378 if (h->got.refcount > 0)
1379 srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
1380
1381 return TRUE;
1382 }
1383
1384
1385 static void
1386 elf_xtensa_allocate_local_got_size (info, srelgot)
1387 struct bfd_link_info *info;
1388 asection *srelgot;
1389 {
1390 bfd *i;
1391
1392 for (i = info->input_bfds; i; i = i->link_next)
1393 {
1394 bfd_signed_vma *local_got_refcounts;
1395 bfd_size_type j, cnt;
1396 Elf_Internal_Shdr *symtab_hdr;
1397
1398 local_got_refcounts = elf_local_got_refcounts (i);
1399 if (!local_got_refcounts)
1400 continue;
1401
1402 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1403 cnt = symtab_hdr->sh_info;
1404
1405 for (j = 0; j < cnt; ++j)
1406 {
1407 if (local_got_refcounts[j] > 0)
1408 srelgot->size += (local_got_refcounts[j]
1409 * sizeof (Elf32_External_Rela));
1410 }
1411 }
1412 }
1413
1414
1415 /* Set the sizes of the dynamic sections. */
1416
1417 static bfd_boolean
1418 elf_xtensa_size_dynamic_sections (output_bfd, info)
1419 bfd *output_bfd ATTRIBUTE_UNUSED;
1420 struct bfd_link_info *info;
1421 {
1422 bfd *dynobj, *abfd;
1423 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1424 bfd_boolean relplt, relgot;
1425 int plt_entries, plt_chunks, chunk;
1426
1427 plt_entries = 0;
1428 plt_chunks = 0;
1429 srelgot = 0;
1430
1431 dynobj = elf_hash_table (info)->dynobj;
1432 if (dynobj == NULL)
1433 abort ();
1434
1435 if (elf_hash_table (info)->dynamic_sections_created)
1436 {
1437 /* Set the contents of the .interp section to the interpreter. */
1438 if (info->executable)
1439 {
1440 s = bfd_get_section_by_name (dynobj, ".interp");
1441 if (s == NULL)
1442 abort ();
1443 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1444 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1445 }
1446
1447 /* Allocate room for one word in ".got". */
1448 s = bfd_get_section_by_name (dynobj, ".got");
1449 if (s == NULL)
1450 abort ();
1451 s->size = 4;
1452
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,
1456 (PTR) info);
1457
1458 /* Allocate space in ".rela.got" for literals that reference
1459 global symbols. */
1460 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1461 if (srelgot == NULL)
1462 abort ();
1463 elf_link_hash_traverse (elf_hash_table (info),
1464 elf_xtensa_allocate_got_size,
1465 (PTR) srelgot);
1466
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. */
1470 if (info->shared)
1471 elf_xtensa_allocate_local_got_size (info, srelgot);
1472
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)
1476 abort ();
1477 elf_link_hash_traverse (elf_hash_table (info),
1478 elf_xtensa_allocate_plt_size,
1479 (PTR) srelplt);
1480
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)
1488 abort ();
1489
1490 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
1491 plt_chunks =
1492 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1493
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. */
1497 for (chunk = 0;
1498 (splt = elf_xtensa_get_plt_section (dynobj, chunk)) != NULL;
1499 chunk++)
1500 {
1501 int chunk_entries;
1502
1503 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1504 if (sgotplt == NULL)
1505 abort ();
1506
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);
1511 else
1512 chunk_entries = 0;
1513
1514 if (chunk_entries != 0)
1515 {
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;
1520 }
1521 else
1522 {
1523 sgotplt->size = 0;
1524 splt->size = 0;
1525 }
1526 }
1527
1528 /* Allocate space in ".got.loc" to match the total size of all the
1529 literal tables. */
1530 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
1531 if (sgotloc == NULL)
1532 abort ();
1533 sgotloc->size = spltlittbl->size;
1534 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1535 {
1536 if (abfd->flags & DYNAMIC)
1537 continue;
1538 for (s = abfd->sections; s != NULL; s = s->next)
1539 {
1540 if (! elf_discarded_section (s)
1541 && xtensa_is_littable_section (s)
1542 && s != spltlittbl)
1543 sgotloc->size += s->size;
1544 }
1545 }
1546 }
1547
1548 /* Allocate memory for dynamic sections. */
1549 relplt = FALSE;
1550 relgot = FALSE;
1551 for (s = dynobj->sections; s != NULL; s = s->next)
1552 {
1553 const char *name;
1554 bfd_boolean strip;
1555
1556 if ((s->flags & SEC_LINKER_CREATED) == 0)
1557 continue;
1558
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);
1562
1563 strip = FALSE;
1564
1565 if (strncmp (name, ".rela", 5) == 0)
1566 {
1567 if (strcmp (name, ".rela.plt") == 0)
1568 relplt = TRUE;
1569 else if (strcmp (name, ".rela.got") == 0)
1570 relgot = TRUE;
1571
1572 /* We use the reloc_count field as a counter if we need
1573 to copy relocs into the output file. */
1574 s->reloc_count = 0;
1575 }
1576 else if (strncmp (name, ".plt.", 5) == 0
1577 || strncmp (name, ".got.plt.", 9) == 0)
1578 {
1579 if (s->size == 0)
1580 {
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. */
1590 strip = TRUE;
1591 }
1592 }
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)
1598 {
1599 /* It's not one of our sections, so don't allocate space. */
1600 continue;
1601 }
1602
1603 if (strip)
1604 _bfd_strip_section_from_output (info, s);
1605 else
1606 {
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)
1610 return FALSE;
1611 }
1612 }
1613
1614 if (elf_hash_table (info)->dynamic_sections_created)
1615 {
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)
1620 abort ();
1621 for (chunk = 0; chunk < plt_chunks; chunk++)
1622 {
1623 Elf_Internal_Rela irela;
1624 bfd_byte *loc;
1625
1626 irela.r_offset = 0;
1627 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1628 irela.r_addend = 0;
1629
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;
1636 }
1637
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)
1645
1646 if (! info->shared)
1647 {
1648 if (!add_dynamic_entry (DT_DEBUG, 0))
1649 return FALSE;
1650 }
1651
1652 if (relplt)
1653 {
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))
1658 return FALSE;
1659 }
1660
1661 if (relgot)
1662 {
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)))
1666 return FALSE;
1667 }
1668
1669 if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1670 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1671 return FALSE;
1672 }
1673 #undef add_dynamic_entry
1674
1675 return TRUE;
1676 }
1677
1678 \f
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. */
1685
1686 static bfd_boolean
1687 elf_xtensa_modify_segment_map (abfd, info)
1688 bfd *abfd;
1689 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1690 {
1691 struct elf_segment_map **m_p;
1692
1693 m_p = &elf_tdata (abfd)->segment_map;
1694 while (*m_p != NULL)
1695 {
1696 if ((*m_p)->p_type == PT_LOAD && (*m_p)->count == 0)
1697 *m_p = (*m_p)->next;
1698 else
1699 m_p = &(*m_p)->next;
1700 }
1701 return TRUE;
1702 }
1703
1704 \f
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
1708 howto. */
1709
1710 #define CALL_SEGMENT_BITS (30)
1711 #define CALL_SEGMENT_SIZE (1<<CALL_SEGMENT_BITS)
1712
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;
1717 bfd *abfd;
1718 asection *input_section;
1719 bfd_vma relocation;
1720 bfd_byte *contents;
1721 bfd_vma address;
1722 bfd_boolean is_weak_undef;
1723 char **error_message;
1724 {
1725 xtensa_format fmt;
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;
1732 int opnd, slot;
1733 uint32 newval;
1734
1735 if (!ibuff)
1736 {
1737 ibuff = xtensa_insnbuf_alloc (isa);
1738 sbuff = xtensa_insnbuf_alloc (isa);
1739 }
1740
1741 input_size = bfd_get_section_limit (abfd, input_section);
1742
1743 switch (howto->type)
1744 {
1745 case R_XTENSA_NONE:
1746 case R_XTENSA_DIFF8:
1747 case R_XTENSA_DIFF16:
1748 case R_XTENSA_DIFF32:
1749 return bfd_reloc_ok;
1750
1751 case R_XTENSA_ASM_EXPAND:
1752 if (!is_weak_undef)
1753 {
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))
1759 {
1760 self_address = (input_section->output_section->vma
1761 + input_section->output_offset
1762 + address);
1763 if ((self_address >> CALL_SEGMENT_BITS)
1764 != (relocation >> CALL_SEGMENT_BITS))
1765 {
1766 *error_message = "windowed longcall crosses 1GB boundary; "
1767 "return may fail";
1768 return bfd_reloc_dangerous;
1769 }
1770 }
1771 }
1772 return bfd_reloc_ok;
1773
1774 case R_XTENSA_ASM_SIMPLIFY:
1775 {
1776 /* Convert the L32R/CALLX to CALL. */
1777 bfd_reloc_status_type retval =
1778 elf_xtensa_do_asm_simplify (contents, address, input_size,
1779 error_message);
1780 if (retval != bfd_reloc_ok)
1781 return bfd_reloc_dangerous;
1782
1783 /* The CALL needs to be relocated. Continue below for that part. */
1784 address += 3;
1785 howto = &elf_howto_table[(unsigned) R_XTENSA_SLOT0_OP ];
1786 }
1787 break;
1788
1789 case R_XTENSA_32:
1790 case R_XTENSA_PLT:
1791 {
1792 bfd_vma x;
1793 x = bfd_get_32 (abfd, contents + address);
1794 x = x + relocation;
1795 bfd_put_32 (abfd, x, contents + address);
1796 }
1797 return bfd_reloc_ok;
1798 }
1799
1800 /* Only instruction slot-specific relocations handled below.... */
1801 slot = get_relocation_slot (howto->type);
1802 if (slot == XTENSA_UNDEFINED)
1803 {
1804 *error_message = "unexpected relocation";
1805 return bfd_reloc_dangerous;
1806 }
1807
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)
1813 {
1814 *error_message = "cannot decode instruction format";
1815 return bfd_reloc_dangerous;
1816 }
1817
1818 xtensa_format_get_slot (isa, fmt, slot, ibuff, sbuff);
1819
1820 opcode = xtensa_opcode_decode (isa, fmt, slot, sbuff);
1821 if (opcode == XTENSA_UNDEFINED)
1822 {
1823 *error_message = "cannot decode instruction opcode";
1824 return bfd_reloc_dangerous;
1825 }
1826
1827 /* Check for opcode-specific "alternate" relocations. */
1828 if (is_alt_relocation (howto->type))
1829 {
1830 if (opcode == get_l32r_opcode ())
1831 {
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");
1835 if (!lit4_sec)
1836 {
1837 *error_message = "relocation references missing .lit4 section";
1838 return bfd_reloc_dangerous;
1839 }
1840 self_address = ((lit4_sec->vma & ~0xfff)
1841 + 0x40000 - 3); /* -3 to compensate for do_reloc */
1842 newval = relocation;
1843 opnd = 1;
1844 }
1845 else if (opcode == get_const16_opcode ())
1846 {
1847 /* ALT used for high 16 bits. */
1848 newval = relocation >> 16;
1849 opnd = 1;
1850 }
1851 else
1852 {
1853 /* No other "alternate" relocations currently defined. */
1854 *error_message = "unexpected relocation";
1855 return bfd_reloc_dangerous;
1856 }
1857 }
1858 else /* Not an "alternate" relocation.... */
1859 {
1860 if (opcode == get_const16_opcode ())
1861 {
1862 newval = relocation & 0xffff;
1863 opnd = 1;
1864 }
1865 else
1866 {
1867 /* ...normal PC-relative relocation.... */
1868
1869 /* Determine which operand is being relocated. */
1870 opnd = get_relocation_opnd (opcode, howto->type);
1871 if (opnd == XTENSA_UNDEFINED)
1872 {
1873 *error_message = "unexpected relocation";
1874 return bfd_reloc_dangerous;
1875 }
1876
1877 if (!howto->pc_relative)
1878 {
1879 *error_message = "expected PC-relative relocation";
1880 return bfd_reloc_dangerous;
1881 }
1882
1883 /* Calculate the PC address for this instruction. */
1884 self_address = (input_section->output_section->vma
1885 + input_section->output_offset
1886 + address);
1887
1888 newval = relocation;
1889 }
1890 }
1891
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,
1896 sbuff, newval))
1897 {
1898 *error_message = build_encoding_error_message (opcode, relocation);
1899 return bfd_reloc_dangerous;
1900 }
1901
1902 /* Check for calls across 1GB boundaries. */
1903 if (is_direct_call_opcode (opcode)
1904 && is_windowed_call_opcode (opcode))
1905 {
1906 if ((self_address >> CALL_SEGMENT_BITS)
1907 != (relocation >> CALL_SEGMENT_BITS))
1908 {
1909 *error_message =
1910 "windowed call crosses 1GB boundary; return may fail";
1911 return bfd_reloc_dangerous;
1912 }
1913 }
1914
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;
1920 }
1921
1922
1923 static char *
1924 vsprint_msg VPARAMS ((const char *origmsg, const char *fmt, int arglen, ...))
1925 {
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;
1932
1933 VA_OPEN (ap, arglen);
1934 VA_FIXEDARG (ap, const char *, origmsg);
1935
1936 is_append = (origmsg == message);
1937
1938 orig_len = strlen (origmsg);
1939 len = orig_len + strlen (fmt) + arglen + 20;
1940 if (len > alloc_size)
1941 {
1942 message = (char *) bfd_realloc (message, len);
1943 alloc_size = len;
1944 }
1945 if (!is_append)
1946 memcpy (message, origmsg, orig_len);
1947 vsprintf (message + orig_len, fmt, ap);
1948 VA_CLOSE (ap);
1949 return message;
1950 }
1951
1952
1953 static char *
1954 build_encoding_error_message (opcode, target_address)
1955 xtensa_opcode opcode;
1956 bfd_vma target_address;
1957 {
1958 const char *opname = xtensa_opcode_name (xtensa_default_isa, opcode);
1959 const char *msg;
1960
1961 msg = "cannot encode";
1962 if (is_direct_call_opcode (opcode))
1963 {
1964 if ((target_address & 0x3) != 0)
1965 msg = "misaligned call target";
1966 else
1967 msg = "call target out of range";
1968 }
1969 else if (opcode == get_l32r_opcode ())
1970 {
1971 if ((target_address & 0x3) != 0)
1972 msg = "misaligned literal target";
1973 else
1974 msg = "literal target out of range";
1975 }
1976
1977 return vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
1978 }
1979
1980
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. */
1987
1988 static bfd_reloc_status_type
1989 bfd_elf_xtensa_reloc (abfd, reloc_entry, symbol, data, input_section,
1990 output_bfd, error_message)
1991 bfd *abfd;
1992 arelent *reloc_entry;
1993 asymbol *symbol;
1994 PTR data;
1995 asection *input_section;
1996 bfd *output_bfd;
1997 char **error_message;
1998 {
1999 bfd_vma relocation;
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;
2006
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....) */
2017
2018 if (output_bfd != (bfd *) NULL
2019 && (symbol->flags & BSF_SECTION_SYM) == 0)
2020 {
2021 reloc_entry->address += input_section->output_offset;
2022 return bfd_reloc_ok;
2023 }
2024
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;
2028
2029 /* Work out which section the relocation is targeted at and the
2030 initial relocation command value. */
2031
2032 /* Get symbol value. (Common symbols are special.) */
2033 if (bfd_is_com_section (symbol->section))
2034 relocation = 0;
2035 else
2036 relocation = symbol->value;
2037
2038 reloc_target_output_section = symbol->section->output_section;
2039
2040 /* Convert input-section-relative symbol value to absolute. */
2041 if ((output_bfd && !howto->partial_inplace)
2042 || reloc_target_output_section == NULL)
2043 output_base = 0;
2044 else
2045 output_base = reloc_target_output_section->vma;
2046
2047 relocation += output_base + symbol->section->output_offset;
2048
2049 /* Add in supplied addend. */
2050 relocation += reloc_entry->addend;
2051
2052 /* Here the variable relocation holds the final address of the
2053 symbol we are relocating against, plus any addend. */
2054 if (output_bfd)
2055 {
2056 if (!howto->partial_inplace)
2057 {
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
2061 above. */
2062
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;
2067 }
2068 else
2069 {
2070 reloc_entry->address += input_section->output_offset;
2071 reloc_entry->addend = 0;
2072 }
2073 }
2074
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);
2080
2081 if (flag == bfd_reloc_dangerous)
2082 {
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);
2089 }
2090
2091 return flag;
2092 }
2093
2094
2095 /* Set up an entry in the procedure linkage table. */
2096
2097 static bfd_vma
2098 elf_xtensa_create_plt_entry (dynobj, output_bfd, reloc_index)
2099 bfd *dynobj;
2100 bfd *output_bfd;
2101 unsigned reloc_index;
2102 {
2103 asection *splt, *sgotplt;
2104 bfd_vma plt_base, got_base;
2105 bfd_vma code_offset, lit_offset;
2106 int chunk;
2107
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);
2112
2113 plt_base = splt->output_section->vma + splt->output_offset;
2114 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2115
2116 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
2117 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
2118
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);
2123
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),
2129 PLT_ENTRY_SIZE);
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);
2139
2140 return plt_base + code_offset;
2141 }
2142
2143
2144 /* Relocate an Xtensa ELF section. This is invoked by the linker for
2145 both relocatable and final links. */
2146
2147 static bfd_boolean
2148 elf_xtensa_relocate_section (output_bfd, info, input_bfd,
2149 input_section, contents, relocs,
2150 local_syms, local_sections)
2151 bfd *output_bfd;
2152 struct bfd_link_info *info;
2153 bfd *input_bfd;
2154 asection *input_section;
2155 bfd_byte *contents;
2156 Elf_Internal_Rela *relocs;
2157 Elf_Internal_Sym *local_syms;
2158 asection **local_sections;
2159 {
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;
2165 bfd *dynobj;
2166 property_table_entry *lit_table = 0;
2167 int ltblsize = 0;
2168 char *error_message = NULL;
2169 bfd_size_type input_size;
2170
2171 if (!xtensa_default_isa)
2172 xtensa_default_isa = xtensa_isa_init (0, 0);
2173
2174 dynobj = elf_hash_table (info)->dynobj;
2175 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2176 sym_hashes = elf_sym_hashes (input_bfd);
2177
2178 srelgot = NULL;
2179 srelplt = NULL;
2180 if (dynobj != NULL)
2181 {
2182 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
2183 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2184 }
2185
2186 if (elf_hash_table (info)->dynamic_sections_created)
2187 {
2188 ltblsize = xtensa_read_table_entries (input_bfd, input_section,
2189 &lit_table, XTENSA_LIT_SEC_NAME,
2190 TRUE);
2191 if (ltblsize < 0)
2192 return FALSE;
2193 }
2194
2195 input_size = bfd_get_section_limit (input_bfd, input_section);
2196
2197 rel = relocs;
2198 relend = relocs + input_section->reloc_count;
2199 for (; rel < relend; rel++)
2200 {
2201 int r_type;
2202 reloc_howto_type *howto;
2203 unsigned long r_symndx;
2204 struct elf_link_hash_entry *h;
2205 Elf_Internal_Sym *sym;
2206 asection *sec;
2207 bfd_vma relocation;
2208 bfd_reloc_status_type r;
2209 bfd_boolean is_weak_undef;
2210 bfd_boolean unresolved_reloc;
2211 bfd_boolean warned;
2212
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)
2216 continue;
2217
2218 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
2219 {
2220 bfd_set_error (bfd_error_bad_value);
2221 return FALSE;
2222 }
2223 howto = &elf_howto_table[r_type];
2224
2225 r_symndx = ELF32_R_SYM (rel->r_info);
2226
2227 if (info->relocatable)
2228 {
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
2235 difference. */
2236
2237 if (relaxing_section)
2238 {
2239 /* Check if this references a section in another input file. */
2240 if (!do_fix_for_relocatable_link (rel, input_bfd, input_section,
2241 contents))
2242 return FALSE;
2243 r_type = ELF32_R_TYPE (rel->r_info);
2244 }
2245
2246 if (r_type == R_XTENSA_ASM_SIMPLIFY)
2247 {
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,
2252 &error_message);
2253 if (r != bfd_reloc_ok)
2254 {
2255 if (!((*info->callbacks->reloc_dangerous)
2256 (info, error_message, input_bfd, input_section,
2257 rel->r_offset)))
2258 return FALSE;
2259 }
2260 r_type = ELF32_R_TYPE (rel->r_info);
2261 }
2262
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)
2268 {
2269 sym = local_syms + r_symndx;
2270 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2271 {
2272 sec = local_sections[r_symndx];
2273 rel->r_addend += sec->output_offset + sym->st_value;
2274 }
2275 }
2276
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.... */
2282 if (rel->r_addend)
2283 {
2284 howto = &elf_howto_table[r_type];
2285 if (howto->partial_inplace)
2286 {
2287 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2288 rel->r_addend, contents,
2289 rel->r_offset, FALSE,
2290 &error_message);
2291 if (r != bfd_reloc_ok)
2292 {
2293 if (!((*info->callbacks->reloc_dangerous)
2294 (info, error_message, input_bfd, input_section,
2295 rel->r_offset)))
2296 return FALSE;
2297 }
2298 rel->r_addend = 0;
2299 }
2300 }
2301
2302 /* Done with work for relocatable link; continue with next reloc. */
2303 continue;
2304 }
2305
2306 /* This is a final link. */
2307
2308 h = NULL;
2309 sym = NULL;
2310 sec = NULL;
2311 is_weak_undef = FALSE;
2312 unresolved_reloc = FALSE;
2313 warned = FALSE;
2314
2315 if (howto->partial_inplace)
2316 {
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);
2323 }
2324
2325 if (r_symndx < symtab_hdr->sh_info)
2326 {
2327 sym = local_syms + r_symndx;
2328 sec = local_sections[r_symndx];
2329 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2330 }
2331 else
2332 {
2333 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2334 r_symndx, symtab_hdr, sym_hashes,
2335 h, sec, relocation,
2336 unresolved_reloc, warned);
2337
2338 if (relocation == 0
2339 && !unresolved_reloc
2340 && h->root.type == bfd_link_hash_undefweak)
2341 is_weak_undef = TRUE;
2342 }
2343
2344 if (relaxing_section)
2345 {
2346 /* Check if this references a section in another input file. */
2347 do_fix_for_final_link (rel, input_bfd, input_section, contents,
2348 &relocation);
2349
2350 /* Update some already cached values. */
2351 r_type = ELF32_R_TYPE (rel->r_info);
2352 howto = &elf_howto_table[r_type];
2353 }
2354
2355 /* Sanity check the address. */
2356 if (rel->r_offset >= input_size
2357 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2358 {
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);
2363 return FALSE;
2364 }
2365
2366 /* Generate dynamic relocations. */
2367 if (elf_hash_table (info)->dynamic_sections_created)
2368 {
2369 bfd_boolean dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
2370
2371 if (dynamic_symbol && is_operand_relocation (r_type))
2372 {
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 "
2377 "symbol", ": %s",
2378 strlen (name) + 2, name);
2379 if (!((*info->callbacks->reloc_dangerous)
2380 (info, error_message, input_bfd, input_section,
2381 rel->r_offset)))
2382 return FALSE;
2383 }
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))
2387 {
2388 Elf_Internal_Rela outrel;
2389 bfd_byte *loc;
2390 asection *srel;
2391
2392 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2393 srel = srelplt;
2394 else
2395 srel = srelgot;
2396
2397 BFD_ASSERT (srel != NULL);
2398
2399 outrel.r_offset =
2400 _bfd_elf_section_offset (output_bfd, info,
2401 input_section, rel->r_offset);
2402
2403 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2404 memset (&outrel, 0, sizeof outrel);
2405 else
2406 {
2407 outrel.r_offset += (input_section->output_section->vma
2408 + input_section->output_offset);
2409
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,
2414 outrel.r_offset))
2415 {
2416 error_message =
2417 _("dynamic relocation in read-only section");
2418 if (!((*info->callbacks->reloc_dangerous)
2419 (info, error_message, input_bfd, input_section,
2420 rel->r_offset)))
2421 return FALSE;
2422 }
2423
2424 if (dynamic_symbol)
2425 {
2426 outrel.r_addend = rel->r_addend;
2427 rel->r_addend = 0;
2428
2429 if (r_type == R_XTENSA_32)
2430 {
2431 outrel.r_info =
2432 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2433 relocation = 0;
2434 }
2435 else /* r_type == R_XTENSA_PLT */
2436 {
2437 outrel.r_info =
2438 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2439
2440 /* Create the PLT entry and set the initial
2441 contents of the literal entry to the address of
2442 the PLT entry. */
2443 relocation =
2444 elf_xtensa_create_plt_entry (dynobj, output_bfd,
2445 srel->reloc_count);
2446 }
2447 unresolved_reloc = FALSE;
2448 }
2449 else
2450 {
2451 /* Generate a RELATIVE relocation. */
2452 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2453 outrel.r_addend = 0;
2454 }
2455 }
2456
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
2461 <= srel->size);
2462 }
2463 }
2464
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
2470 && h->def_dynamic))
2471 (*_bfd_error_handler)
2472 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2473 input_bfd,
2474 input_section,
2475 (long) rel->r_offset,
2476 h->root.root.string);
2477
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,
2483 &error_message);
2484
2485 if (r != bfd_reloc_ok && !warned)
2486 {
2487 const char *name;
2488
2489 BFD_ASSERT (r == bfd_reloc_dangerous || r == bfd_reloc_other);
2490 BFD_ASSERT (error_message != (char *) NULL);
2491
2492 if (h != NULL)
2493 name = h->root.root.string;
2494 else
2495 {
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);
2500 }
2501 if (name)
2502 {
2503 if (rel->r_addend == 0)
2504 error_message = vsprint_msg (error_message, ": %s",
2505 strlen (name) + 2, name);
2506 else
2507 error_message = vsprint_msg (error_message, ": (%s+0x%x)",
2508 strlen (name) + 22,
2509 name, rel->r_addend);
2510 }
2511
2512 if (!((*info->callbacks->reloc_dangerous)
2513 (info, error_message, input_bfd, input_section,
2514 rel->r_offset)))
2515 return FALSE;
2516 }
2517 }
2518
2519 if (lit_table)
2520 free (lit_table);
2521
2522 input_section->reloc_done = TRUE;
2523
2524 return TRUE;
2525 }
2526
2527
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. */
2530
2531 static bfd_boolean
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;
2537 {
2538 if (h->needs_plt
2539 && !h->def_regular)
2540 {
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;
2544 }
2545
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;
2550
2551 return TRUE;
2552 }
2553
2554
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
2561 on error. */
2562
2563 static int
2564 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc)
2565 bfd *output_bfd;
2566 asection *sxtlit;
2567 asection *sgotloc;
2568 {
2569 bfd_byte *contents;
2570 property_table_entry *table;
2571 bfd_size_type section_size, sgotloc_size;
2572 bfd_vma offset;
2573 int n, m, num;
2574
2575 section_size = sxtlit->size;
2576 BFD_ASSERT (section_size % 8 == 0);
2577 num = section_size / 8;
2578
2579 sgotloc_size = sgotloc->size;
2580 if (sgotloc_size != section_size)
2581 {
2582 (*_bfd_error_handler)
2583 (_("internal inconsistency in size of .got.loc section"));
2584 return -1;
2585 }
2586
2587 table = bfd_malloc (num * sizeof (property_table_entry));
2588 if (table == 0)
2589 return -1;
2590
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;
2595
2596 if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
2597 {
2598 if (contents != 0)
2599 free (contents);
2600 free (table);
2601 return -1;
2602 }
2603
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. */
2606
2607 /* Copy the raw contents into a property table array and sort it. */
2608 offset = 0;
2609 for (n = 0; n < num; n++)
2610 {
2611 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
2612 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
2613 offset += 8;
2614 }
2615 qsort (table, num, sizeof (property_table_entry), property_table_compare);
2616
2617 for (n = 0; n < num; n++)
2618 {
2619 bfd_boolean remove = FALSE;
2620
2621 if (table[n].size == 0)
2622 remove = TRUE;
2623 else if (n > 0 &&
2624 (table[n-1].address + table[n-1].size == table[n].address))
2625 {
2626 table[n-1].size += table[n].size;
2627 remove = TRUE;
2628 }
2629
2630 if (remove)
2631 {
2632 for (m = n; m < num - 1; m++)
2633 {
2634 table[m].address = table[m+1].address;
2635 table[m].size = table[m+1].size;
2636 }
2637
2638 n--;
2639 num--;
2640 }
2641 }
2642
2643 /* Copy the data back to the raw contents. */
2644 offset = 0;
2645 for (n = 0; n < num; n++)
2646 {
2647 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
2648 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
2649 offset += 8;
2650 }
2651
2652 /* Clear the removed bytes. */
2653 if ((bfd_size_type) (num * 8) < section_size)
2654 memset (&contents[num * 8], 0, section_size - num * 8);
2655
2656 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
2657 section_size))
2658 return -1;
2659
2660 /* Copy the contents to ".got.loc". */
2661 memcpy (sgotloc->contents, contents, section_size);
2662
2663 free (contents);
2664 free (table);
2665 return num;
2666 }
2667
2668
2669 /* Finish up the dynamic sections. */
2670
2671 static bfd_boolean
2672 elf_xtensa_finish_dynamic_sections (output_bfd, info)
2673 bfd *output_bfd;
2674 struct bfd_link_info *info;
2675 {
2676 bfd *dynobj;
2677 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
2678 Elf32_External_Dyn *dyncon, *dynconend;
2679 int num_xtlit_entries;
2680
2681 if (! elf_hash_table (info)->dynamic_sections_created)
2682 return TRUE;
2683
2684 dynobj = elf_hash_table (info)->dynobj;
2685 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2686 BFD_ASSERT (sdyn != NULL);
2687
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");
2691 if (sgot)
2692 {
2693 BFD_ASSERT (sgot->size == 4);
2694 if (sdyn == NULL)
2695 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2696 else
2697 bfd_put_32 (output_bfd,
2698 sdyn->output_section->vma + sdyn->output_offset,
2699 sgot->contents);
2700 }
2701
2702 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2703 if (srelplt != NULL && srelplt->size != 0)
2704 {
2705 asection *sgotplt, *srelgot, *spltlittbl;
2706 int chunk, plt_chunks, plt_entries;
2707 Elf_Internal_Rela irela;
2708 bfd_byte *loc;
2709 unsigned rtld_reloc;
2710
2711 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
2712 BFD_ASSERT (srelgot != NULL);
2713
2714 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
2715 BFD_ASSERT (spltlittbl != NULL);
2716
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++)
2720 {
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)
2724 break;
2725 }
2726 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
2727
2728 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
2729 plt_chunks =
2730 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
2731
2732 for (chunk = 0; chunk < plt_chunks; chunk++)
2733 {
2734 int chunk_entries = 0;
2735
2736 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
2737 BFD_ASSERT (sgotplt != NULL);
2738
2739 /* Emit special RTLD relocations for the first two entries in
2740 each chunk of the .got.plt section. */
2741
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);
2749 rtld_reloc += 1;
2750 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2751
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. */
2759 irela.r_addend = 2;
2760 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2761 rtld_reloc += 1;
2762 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2763
2764 /* Fill in the literal table. */
2765 if (chunk < plt_chunks - 1)
2766 chunk_entries = PLT_ENTRIES_PER_CHUNK;
2767 else
2768 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
2769
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);
2777 }
2778
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))
2785 abort ();
2786
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
2790 the output here. */
2791 if (! bfd_set_section_contents (output_bfd,
2792 spltlittbl->output_section,
2793 spltlittbl->contents,
2794 spltlittbl->output_offset,
2795 spltlittbl->size))
2796 return FALSE;
2797 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
2798 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
2799 }
2800
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);
2806 num_xtlit_entries =
2807 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
2808 if (num_xtlit_entries < 0)
2809 return FALSE;
2810
2811 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2812 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2813 for (; dyncon < dynconend; dyncon++)
2814 {
2815 Elf_Internal_Dyn dyn;
2816 const char *name;
2817 asection *s;
2818
2819 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2820
2821 switch (dyn.d_tag)
2822 {
2823 default:
2824 break;
2825
2826 case DT_XTENSA_GOT_LOC_SZ:
2827 dyn.d_un.d_val = num_xtlit_entries;
2828 break;
2829
2830 case DT_XTENSA_GOT_LOC_OFF:
2831 name = ".got.loc";
2832 goto get_vma;
2833 case DT_PLTGOT:
2834 name = ".got";
2835 goto get_vma;
2836 case DT_JMPREL:
2837 name = ".rela.plt";
2838 get_vma:
2839 s = bfd_get_section_by_name (output_bfd, name);
2840 BFD_ASSERT (s);
2841 dyn.d_un.d_ptr = s->vma;
2842 break;
2843
2844 case DT_PLTRELSZ:
2845 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2846 BFD_ASSERT (s);
2847 dyn.d_un.d_val = s->size;
2848 break;
2849
2850 case DT_RELASZ:
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");
2858 if (s)
2859 dyn.d_un.d_val -= s->size;
2860 break;
2861 }
2862
2863 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2864 }
2865
2866 return TRUE;
2867 }
2868
2869 \f
2870 /* Functions for dealing with the e_flags field. */
2871
2872 /* Merge backend specific data from an object file to the output
2873 object file when linking. */
2874
2875 static bfd_boolean
2876 elf_xtensa_merge_private_bfd_data (ibfd, obfd)
2877 bfd *ibfd;
2878 bfd *obfd;
2879 {
2880 unsigned out_mach, in_mach;
2881 flagword out_flag, in_flag;
2882
2883 /* Check if we have the same endianess. */
2884 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
2885 return FALSE;
2886
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)
2890 return FALSE;
2891
2892 out_flag = elf_elfheader (obfd)->e_flags;
2893 in_flag = elf_elfheader (ibfd)->e_flags;
2894
2895 out_mach = out_flag & EF_XTENSA_MACH;
2896 in_mach = in_flag & EF_XTENSA_MACH;
2897 if (out_mach != in_mach)
2898 {
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);
2903 return FALSE;
2904 }
2905
2906 if (! elf_flags_init (obfd))
2907 {
2908 elf_flags_init (obfd) = TRUE;
2909 elf_elfheader (obfd)->e_flags = in_flag;
2910
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));
2915
2916 return TRUE;
2917 }
2918
2919 if ((out_flag & EF_XTENSA_XT_INSN) != (in_flag & EF_XTENSA_XT_INSN))
2920 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
2921
2922 if ((out_flag & EF_XTENSA_XT_LIT) != (in_flag & EF_XTENSA_XT_LIT))
2923 elf_elfheader (obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
2924
2925 return TRUE;
2926 }
2927
2928
2929 static bfd_boolean
2930 elf_xtensa_set_private_flags (abfd, flags)
2931 bfd *abfd;
2932 flagword flags;
2933 {
2934 BFD_ASSERT (!elf_flags_init (abfd)
2935 || elf_elfheader (abfd)->e_flags == flags);
2936
2937 elf_elfheader (abfd)->e_flags |= flags;
2938 elf_flags_init (abfd) = TRUE;
2939
2940 return TRUE;
2941 }
2942
2943
2944 extern flagword
2945 elf_xtensa_get_private_bfd_flags (abfd)
2946 bfd *abfd;
2947 {
2948 return elf_elfheader (abfd)->e_flags;
2949 }
2950
2951
2952 static bfd_boolean
2953 elf_xtensa_print_private_bfd_data (abfd, farg)
2954 bfd *abfd;
2955 PTR farg;
2956 {
2957 FILE *f = (FILE *) farg;
2958 flagword e_flags = elf_elfheader (abfd)->e_flags;
2959
2960 fprintf (f, "\nXtensa header:\n");
2961 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
2962 fprintf (f, "\nMachine = Base\n");
2963 else
2964 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
2965
2966 fprintf (f, "Insn tables = %s\n",
2967 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
2968
2969 fprintf (f, "Literal tables = %s\n",
2970 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
2971
2972 return _bfd_elf_print_private_bfd_data (abfd, farg);
2973 }
2974
2975
2976 /* Set the right machine number for an Xtensa ELF file. */
2977
2978 static bfd_boolean
2979 elf_xtensa_object_p (abfd)
2980 bfd *abfd;
2981 {
2982 int mach;
2983 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
2984
2985 switch (arch)
2986 {
2987 case E_XTENSA_MACH:
2988 mach = bfd_mach_xtensa;
2989 break;
2990 default:
2991 return FALSE;
2992 }
2993
2994 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
2995 return TRUE;
2996 }
2997
2998
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
3001 number. */
3002
3003 static void
3004 elf_xtensa_final_write_processing (abfd, linker)
3005 bfd *abfd;
3006 bfd_boolean linker ATTRIBUTE_UNUSED;
3007 {
3008 int mach;
3009 unsigned long val;
3010
3011 switch (mach = bfd_get_mach (abfd))
3012 {
3013 case bfd_mach_xtensa:
3014 val = E_XTENSA_MACH;
3015 break;
3016 default:
3017 return;
3018 }
3019
3020 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
3021 elf_elfheader (abfd)->e_flags |= val;
3022 }
3023
3024
3025 static enum elf_reloc_type_class
3026 elf_xtensa_reloc_type_class (rela)
3027 const Elf_Internal_Rela *rela;
3028 {
3029 switch ((int) ELF32_R_TYPE (rela->r_info))
3030 {
3031 case R_XTENSA_RELATIVE:
3032 return reloc_class_relative;
3033 case R_XTENSA_JMP_SLOT:
3034 return reloc_class_plt;
3035 default:
3036 return reloc_class_normal;
3037 }
3038 }
3039
3040 \f
3041 static bfd_boolean
3042 elf_xtensa_discard_info_for_section (abfd, cookie, info, sec)
3043 bfd *abfd;
3044 struct elf_reloc_cookie *cookie;
3045 struct bfd_link_info *info;
3046 asection *sec;
3047 {
3048 bfd_byte *contents;
3049 bfd_vma section_size;
3050 bfd_vma offset, actual_offset;
3051 size_t removed_bytes = 0;
3052
3053 section_size = sec->size;
3054 if (section_size == 0 || section_size % 8 != 0)
3055 return FALSE;
3056
3057 if (sec->output_section
3058 && bfd_is_abs_section (sec->output_section))
3059 return FALSE;
3060
3061 contents = retrieve_contents (abfd, sec, info->keep_memory);
3062 if (!contents)
3063 return FALSE;
3064
3065 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
3066 if (!cookie->rels)
3067 {
3068 release_contents (sec, contents);
3069 return FALSE;
3070 }
3071
3072 cookie->rel = cookie->rels;
3073 cookie->relend = cookie->rels + sec->reloc_count;
3074
3075 for (offset = 0; offset < section_size; offset += 8)
3076 {
3077 actual_offset = offset - removed_bytes;
3078
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)
3083 {
3084 cookie->rel->r_offset -= removed_bytes;
3085 cookie->rel++;
3086 }
3087
3088 while (cookie->rel < cookie->relend
3089 && cookie->rel->r_offset == offset)
3090 {
3091 if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
3092 {
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)
3097 {
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);
3103 removed_bytes += 8;
3104 }
3105
3106 /* Remove this relocation. */
3107 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
3108 }
3109
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;
3117 else
3118 cookie->rel->r_offset = 0;
3119
3120 cookie->rel++;
3121 }
3122 }
3123
3124 if (removed_bytes != 0)
3125 {
3126 /* Adjust any remaining relocs (shouldn't be any). */
3127 for (; cookie->rel < cookie->relend; cookie->rel++)
3128 {
3129 if (cookie->rel->r_offset >= removed_bytes)
3130 cookie->rel->r_offset -= removed_bytes;
3131 else
3132 cookie->rel->r_offset = 0;
3133 }
3134
3135 /* Clear the removed bytes. */
3136 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
3137
3138 pin_contents (sec, contents);
3139 pin_internal_relocs (sec, cookie->rels);
3140
3141 /* Shrink size. */
3142 sec->size = section_size - removed_bytes;
3143
3144 if (xtensa_is_littable_section (sec))
3145 {
3146 bfd *dynobj = elf_hash_table (info)->dynobj;
3147 if (dynobj)
3148 {
3149 asection *sgotloc =
3150 bfd_get_section_by_name (dynobj, ".got.loc");
3151 if (sgotloc)
3152 sgotloc->size -= removed_bytes;
3153 }
3154 }
3155 }
3156 else
3157 {
3158 release_contents (sec, contents);
3159 release_internal_relocs (sec, cookie->rels);
3160 }
3161
3162 return (removed_bytes != 0);
3163 }
3164
3165
3166 static bfd_boolean
3167 elf_xtensa_discard_info (abfd, cookie, info)
3168 bfd *abfd;
3169 struct elf_reloc_cookie *cookie;
3170 struct bfd_link_info *info;
3171 {
3172 asection *sec;
3173 bfd_boolean changed = FALSE;
3174
3175 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3176 {
3177 if (xtensa_is_property_section (sec))
3178 {
3179 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
3180 changed = TRUE;
3181 }
3182 }
3183
3184 return changed;
3185 }
3186
3187
3188 static bfd_boolean
3189 elf_xtensa_ignore_discarded_relocs (sec)
3190 asection *sec;
3191 {
3192 return xtensa_is_property_section (sec);
3193 }
3194
3195 \f
3196 /* Support for core dump NOTE sections. */
3197
3198 static bfd_boolean
3199 elf_xtensa_grok_prstatus (abfd, note)
3200 bfd *abfd;
3201 Elf_Internal_Note *note;
3202 {
3203 int offset;
3204 unsigned int size;
3205
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. */
3208
3209 /* pr_cursig */
3210 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3211
3212 /* pr_pid */
3213 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3214
3215 /* pr_reg */
3216 offset = 72;
3217 size = note->descsz - offset - 4;
3218
3219 /* Make a ".reg/999" section. */
3220 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3221 size, note->descpos + offset);
3222 }
3223
3224
3225 static bfd_boolean
3226 elf_xtensa_grok_psinfo (abfd, note)
3227 bfd *abfd;
3228 Elf_Internal_Note *note;
3229 {
3230 switch (note->descsz)
3231 {
3232 default:
3233 return FALSE;
3234
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);
3240 }
3241
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. */
3245
3246 {
3247 char *command = elf_tdata (abfd)->core_command;
3248 int n = strlen (command);
3249
3250 if (0 < n && command[n - 1] == ' ')
3251 command[n - 1] = '\0';
3252 }
3253
3254 return TRUE;
3255 }
3256
3257 \f
3258 /* Generic Xtensa configurability stuff. */
3259
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;
3268
3269 static void
3270 init_call_opcodes ()
3271 {
3272 if (callx0_op == XTENSA_UNDEFINED)
3273 {
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");
3282 }
3283 }
3284
3285
3286 static bfd_boolean
3287 is_indirect_call_opcode (opcode)
3288 xtensa_opcode opcode;
3289 {
3290 init_call_opcodes ();
3291 return (opcode == callx0_op
3292 || opcode == callx4_op
3293 || opcode == callx8_op
3294 || opcode == callx12_op);
3295 }
3296
3297
3298 static bfd_boolean
3299 is_direct_call_opcode (opcode)
3300 xtensa_opcode opcode;
3301 {
3302 init_call_opcodes ();
3303 return (opcode == call0_op
3304 || opcode == call4_op
3305 || opcode == call8_op
3306 || opcode == call12_op);
3307 }
3308
3309
3310 static bfd_boolean
3311 is_windowed_call_opcode (opcode)
3312 xtensa_opcode opcode;
3313 {
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);
3321 }
3322
3323
3324 static xtensa_opcode
3325 get_const16_opcode (void)
3326 {
3327 static bfd_boolean done_lookup = FALSE;
3328 static xtensa_opcode const16_opcode = XTENSA_UNDEFINED;
3329 if (!done_lookup)
3330 {
3331 const16_opcode = xtensa_opcode_lookup (xtensa_default_isa, "const16");
3332 done_lookup = TRUE;
3333 }
3334 return const16_opcode;
3335 }
3336
3337
3338 static xtensa_opcode
3339 get_l32r_opcode (void)
3340 {
3341 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
3342 static bfd_boolean done_lookup = FALSE;
3343
3344 if (!done_lookup)
3345 {
3346 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
3347 done_lookup = TRUE;
3348 }
3349 return l32r_opcode;
3350 }
3351
3352
3353 static bfd_vma
3354 l32r_offset (addr, pc)
3355 bfd_vma addr;
3356 bfd_vma pc;
3357 {
3358 bfd_vma offset;
3359
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);
3364 return offset;
3365 }
3366
3367
3368 static int
3369 get_relocation_opnd (opcode, r_type)
3370 xtensa_opcode opcode;
3371 int r_type;
3372 {
3373 xtensa_isa isa = xtensa_default_isa;
3374 int last_immed, last_opnd, opi;
3375
3376 if (opcode == XTENSA_UNDEFINED)
3377 return XTENSA_UNDEFINED;
3378
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--)
3385 {
3386 if (xtensa_operand_is_visible (isa, opcode, opi) == 0)
3387 continue;
3388 if (xtensa_operand_is_PCrelative (isa, opcode, opi) == 1)
3389 {
3390 last_immed = opi;
3391 break;
3392 }
3393 if (last_immed == XTENSA_UNDEFINED
3394 && xtensa_operand_is_register (isa, opcode, opi) == 0)
3395 last_immed = opi;
3396 }
3397 if (last_immed < 0)
3398 return XTENSA_UNDEFINED;
3399
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)
3403 {
3404 int reloc_opnd = r_type - R_XTENSA_OP0;
3405 if (reloc_opnd != last_immed)
3406 return XTENSA_UNDEFINED;
3407 }
3408
3409 return last_immed;
3410 }
3411
3412
3413 int
3414 get_relocation_slot (r_type)
3415 int r_type;
3416 {
3417 switch (r_type)
3418 {
3419 case R_XTENSA_OP0:
3420 case R_XTENSA_OP1:
3421 case R_XTENSA_OP2:
3422 return 0;
3423
3424 default:
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;
3429 break;
3430 }
3431
3432 return XTENSA_UNDEFINED;
3433 }
3434
3435
3436 /* Get the opcode for a relocation. */
3437
3438 static xtensa_opcode
3439 get_relocation_opcode (abfd, sec, contents, irel)
3440 bfd *abfd;
3441 asection *sec;
3442 bfd_byte *contents;
3443 Elf_Internal_Rela *irel;
3444 {
3445 static xtensa_insnbuf ibuff = NULL;
3446 static xtensa_insnbuf sbuff = NULL;
3447 xtensa_isa isa = xtensa_default_isa;
3448 xtensa_format fmt;
3449 int slot;
3450
3451 if (contents == NULL)
3452 return XTENSA_UNDEFINED;
3453
3454 if (bfd_get_section_limit (abfd, sec) <= irel->r_offset)
3455 return XTENSA_UNDEFINED;
3456
3457 if (ibuff == NULL)
3458 {
3459 ibuff = xtensa_insnbuf_alloc (isa);
3460 sbuff = xtensa_insnbuf_alloc (isa);
3461 }
3462
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);
3472 }
3473
3474
3475 bfd_boolean
3476 is_l32r_relocation (abfd, sec, contents, irel)
3477 bfd *abfd;
3478 asection *sec;
3479 bfd_byte *contents;
3480 Elf_Internal_Rela *irel;
3481 {
3482 xtensa_opcode opcode;
3483 if (!is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
3484 return FALSE;
3485 opcode = get_relocation_opcode (abfd, sec, contents, irel);
3486 return (opcode == get_l32r_opcode ());
3487 }
3488
3489
3490 static bfd_size_type
3491 get_asm_simplify_size (contents, content_len, offset)
3492 bfd_byte *contents;
3493 bfd_size_type content_len;
3494 bfd_size_type offset;
3495 {
3496 bfd_size_type insnlen, size = 0;
3497
3498 /* Decode the size of the next two instructions. */
3499 insnlen = insn_decode_len (contents, content_len, offset);
3500 if (insnlen == 0)
3501 return 0;
3502
3503 size += insnlen;
3504
3505 insnlen = insn_decode_len (contents, content_len, offset + size);
3506 if (insnlen == 0)
3507 return 0;
3508
3509 size += insnlen;
3510 return size;
3511 }
3512
3513
3514 bfd_boolean
3515 is_alt_relocation (r_type)
3516 int r_type;
3517 {
3518 return (r_type >= R_XTENSA_SLOT0_ALT
3519 && r_type <= R_XTENSA_SLOT14_ALT);
3520 }
3521
3522
3523 bfd_boolean
3524 is_operand_relocation (r_type)
3525 int r_type;
3526 {
3527 switch (r_type)
3528 {
3529 case R_XTENSA_OP0:
3530 case R_XTENSA_OP1:
3531 case R_XTENSA_OP2:
3532 return TRUE;
3533
3534 default:
3535 if (r_type >= R_XTENSA_SLOT0_OP && r_type <= R_XTENSA_SLOT14_OP)
3536 return TRUE;
3537 if (r_type >= R_XTENSA_SLOT0_ALT && r_type <= R_XTENSA_SLOT14_ALT)
3538 return TRUE;
3539 break;
3540 }
3541
3542 return FALSE;
3543 }
3544
3545
3546 #define MIN_INSN_LENGTH 2
3547
3548 /* Return 0 if it fails to decode. */
3549
3550 bfd_size_type
3551 insn_decode_len (contents, content_len, offset)
3552 bfd_byte *contents;
3553 bfd_size_type content_len;
3554 bfd_size_type offset;
3555 {
3556 int insn_len;
3557 xtensa_isa isa = xtensa_default_isa;
3558 xtensa_format fmt;
3559 static xtensa_insnbuf ibuff = NULL;
3560
3561 if (offset + MIN_INSN_LENGTH > content_len)
3562 return 0;
3563
3564 if (ibuff == NULL)
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)
3570 return 0;
3571 insn_len = xtensa_format_length (isa, fmt);
3572 if (insn_len == XTENSA_UNDEFINED)
3573 return 0;
3574 return insn_len;
3575 }
3576
3577
3578 /* Decode the opcode for a single slot instruction.
3579 Return 0 if it fails to decode or the instruction is multi-slot. */
3580
3581 xtensa_opcode
3582 insn_decode_opcode (contents, content_len, offset, slot)
3583 bfd_byte *contents;
3584 bfd_size_type content_len;
3585 bfd_size_type offset;
3586 int slot;
3587 {
3588 xtensa_isa isa = xtensa_default_isa;
3589 xtensa_format fmt;
3590 static xtensa_insnbuf insnbuf = NULL;
3591 static xtensa_insnbuf slotbuf = NULL;
3592
3593 if (offset + MIN_INSN_LENGTH > content_len)
3594 return XTENSA_UNDEFINED;
3595
3596 if (insnbuf == NULL)
3597 {
3598 insnbuf = xtensa_insnbuf_alloc (isa);
3599 slotbuf = xtensa_insnbuf_alloc (isa);
3600 }
3601
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;
3607
3608 if (slot >= xtensa_format_num_slots (isa, fmt))
3609 return XTENSA_UNDEFINED;
3610
3611 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
3612 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
3613 }
3614
3615
3616 /* The offset is the offset in the contents.
3617 The address is the address of that offset. */
3618
3619 static bfd_boolean
3620 check_branch_target_aligned (contents, content_length, offset, address)
3621 bfd_byte *contents;
3622 bfd_size_type content_length;
3623 bfd_vma offset;
3624 bfd_vma address;
3625 {
3626 bfd_size_type insn_len = insn_decode_len (contents, content_length, offset);
3627 if (insn_len == 0)
3628 return FALSE;
3629 return check_branch_target_aligned_address (address, insn_len);
3630 }
3631
3632
3633 static bfd_boolean
3634 check_loop_aligned (contents, content_length, offset, address)
3635 bfd_byte *contents;
3636 bfd_size_type content_length;
3637 bfd_vma offset;
3638 bfd_vma address;
3639 {
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)
3645 return FALSE;
3646 BFD_ASSERT (xtensa_opcode_is_loop (xtensa_default_isa, opcode));
3647 if (!xtensa_opcode_is_loop (xtensa_default_isa, opcode))
3648 return FALSE;
3649
3650 loop_len = insn_decode_len (contents, content_length, offset);
3651 BFD_ASSERT (loop_len != 0);
3652 if (loop_len == 0)
3653 return FALSE;
3654
3655 insn_len = insn_decode_len (contents, content_length, offset + loop_len);
3656 BFD_ASSERT (insn_len != 0);
3657 if (insn_len == 0)
3658 return FALSE;
3659
3660 return check_branch_target_aligned_address (address + loop_len, insn_len);
3661 }
3662
3663
3664 static bfd_boolean
3665 check_branch_target_aligned_address (addr, len)
3666 bfd_vma addr;
3667 int len;
3668 {
3669 if (len == 8)
3670 return (addr % 8 == 0);
3671 return ((addr >> 2) == ((addr + len - 1) >> 2));
3672 }
3673
3674 \f
3675 /* Instruction widening and narrowing. */
3676
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
3684 PARAMS ((void));
3685
3686
3687 struct string_pair
3688 {
3689 const char *wide;
3690 const char *narrow;
3691 };
3692
3693
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. */
3699
3700 struct string_pair narrowable[] =
3701 {
3702 { "add", "add.n" },
3703 { "addi", "addi.n" },
3704 { "addmi", "addi.n" },
3705 { "l32i", "l32i.n" },
3706 { "movi", "movi.n" },
3707 { "ret", "ret.n" },
3708 { "retw", "retw.n" },
3709 { "s32i", "s32i.n" },
3710 { "or", "mov.n" } /* special case only when op1 == op2 */
3711 };
3712
3713 struct string_pair widenable[] =
3714 {
3715 { "add", "add.n" },
3716 { "addi", "addi.n" },
3717 { "addmi", "addi.n" },
3718 { "beqz", "beqz.n" },
3719 { "bnez", "bnez.n" },
3720 { "l32i", "l32i.n" },
3721 { "movi", "movi.n" },
3722 { "ret", "ret.n" },
3723 { "retw", "retw.n" },
3724 { "s32i", "s32i.n" },
3725 { "or", "mov.n" } /* special case only when op1 == op2 */
3726 };
3727
3728
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. */
3734
3735 static bfd_boolean
3736 narrow_instruction (contents, content_length, offset, do_it)
3737 bfd_byte *contents;
3738 bfd_size_type content_length;
3739 bfd_size_type offset;
3740 bfd_boolean do_it;
3741 {
3742 xtensa_opcode opcode;
3743 bfd_size_type insn_len, opi;
3744 xtensa_isa isa = xtensa_default_isa;
3745 xtensa_format fmt, o_fmt;
3746
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;
3751
3752 if (insnbuf == NULL)
3753 {
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);
3758 }
3759
3760 BFD_ASSERT (offset < content_length);
3761
3762 if (content_length < 2)
3763 return FALSE;
3764
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)
3771 return FALSE;
3772
3773 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
3774 return FALSE;
3775
3776 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
3777 if (opcode == XTENSA_UNDEFINED)
3778 return FALSE;
3779 insn_len = xtensa_format_length (isa, fmt);
3780 if (insn_len > content_length)
3781 return FALSE;
3782
3783 for (opi = 0; opi < (sizeof (narrowable)/sizeof (struct string_pair)); ++opi)
3784 {
3785 bfd_boolean is_or = (strcmp ("or", narrowable[opi].wide) == 0);
3786
3787 if (opcode == xtensa_opcode_lookup (isa, narrowable[opi].wide))
3788 {
3789 uint32 value, newval;
3790 int i, operand_count, o_operand_count;
3791 xtensa_opcode o_opcode;
3792
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;
3796
3797 o_opcode = xtensa_opcode_lookup (isa, narrowable[opi].narrow);
3798 if (o_opcode == XTENSA_UNDEFINED)
3799 return FALSE;
3800 o_fmt = get_single_format (o_opcode);
3801 if (o_fmt == XTENSA_UNDEFINED)
3802 return FALSE;
3803
3804 if (xtensa_format_length (isa, fmt) != 3
3805 || xtensa_format_length (isa, o_fmt) != 2)
3806 return FALSE;
3807
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);
3812
3813 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
3814 return FALSE;
3815
3816 if (!is_or)
3817 {
3818 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
3819 return FALSE;
3820 }
3821 else
3822 {
3823 uint32 rawval0, rawval1, rawval2;
3824
3825 if (o_operand_count + 1 != operand_count)
3826 return FALSE;
3827 if (xtensa_operand_get_field (isa, opcode, 0,
3828 fmt, 0, slotbuf, &rawval0) != 0)
3829 return FALSE;
3830 if (xtensa_operand_get_field (isa, opcode, 1,
3831 fmt, 0, slotbuf, &rawval1) != 0)
3832 return FALSE;
3833 if (xtensa_operand_get_field (isa, opcode, 2,
3834 fmt, 0, slotbuf, &rawval2) != 0)
3835 return FALSE;
3836
3837 if (rawval1 != rawval2)
3838 return FALSE;
3839 if (rawval0 == rawval1) /* it is a nop */
3840 return FALSE;
3841 }
3842
3843 for (i = 0; i < o_operand_count; ++i)
3844 {
3845 if (xtensa_operand_get_field (isa, opcode, i, fmt, 0,
3846 slotbuf, &value)
3847 || xtensa_operand_decode (isa, opcode, i, &value))
3848 return FALSE;
3849
3850 /* PC-relative branches need adjustment, but
3851 the PC-rel operand will always have a relocation. */
3852 newval = value;
3853 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
3854 self_address)
3855 || xtensa_operand_encode (isa, o_opcode, i, &newval)
3856 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
3857 o_slotbuf, newval))
3858 return FALSE;
3859 }
3860
3861 if (xtensa_format_set_slot (isa, o_fmt, 0,
3862 o_insnbuf, o_slotbuf) != 0)
3863 return FALSE;
3864
3865 if (do_it)
3866 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
3867 content_length - offset);
3868 return TRUE;
3869 }
3870 }
3871 return FALSE;
3872 }
3873
3874
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
3880 cases. */
3881
3882 static bfd_boolean
3883 widen_instruction (contents, content_length, offset, do_it)
3884 bfd_byte *contents;
3885 bfd_size_type content_length;
3886 bfd_size_type offset;
3887 bfd_boolean do_it;
3888 {
3889 xtensa_opcode opcode;
3890 bfd_size_type insn_len, opi;
3891 xtensa_isa isa = xtensa_default_isa;
3892 xtensa_format fmt, o_fmt;
3893
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;
3898
3899 if (insnbuf == NULL)
3900 {
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);
3905 }
3906
3907 BFD_ASSERT (offset < content_length);
3908
3909 if (content_length < 2)
3910 return FALSE;
3911
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)
3918 return FALSE;
3919
3920 if (xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf) != 0)
3921 return FALSE;
3922
3923 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
3924 if (opcode == XTENSA_UNDEFINED)
3925 return FALSE;
3926 insn_len = xtensa_format_length (isa, fmt);
3927 if (insn_len > content_length)
3928 return FALSE;
3929
3930 for (opi = 0; opi < (sizeof (widenable)/sizeof (struct string_pair)); ++opi)
3931 {
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);
3935
3936 if (opcode == xtensa_opcode_lookup (isa, widenable[opi].narrow))
3937 {
3938 uint32 value, newval;
3939 int i, operand_count, o_operand_count, check_operand_count;
3940 xtensa_opcode o_opcode;
3941
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;
3945
3946 o_opcode = xtensa_opcode_lookup (isa, widenable[opi].wide);
3947 if (o_opcode == XTENSA_UNDEFINED)
3948 return FALSE;
3949 o_fmt = get_single_format (o_opcode);
3950 if (o_fmt == XTENSA_UNDEFINED)
3951 return FALSE;
3952
3953 if (xtensa_format_length (isa, fmt) != 2
3954 || xtensa_format_length (isa, o_fmt) != 3)
3955 return FALSE;
3956
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;
3962
3963 if (xtensa_opcode_encode (isa, o_fmt, 0, o_slotbuf, o_opcode) != 0)
3964 return FALSE;
3965
3966 if (!is_or)
3967 {
3968 if (xtensa_opcode_num_operands (isa, o_opcode) != operand_count)
3969 return FALSE;
3970 }
3971 else
3972 {
3973 uint32 rawval0, rawval1;
3974
3975 if (o_operand_count != operand_count + 1)
3976 return FALSE;
3977 if (xtensa_operand_get_field (isa, opcode, 0,
3978 fmt, 0, slotbuf, &rawval0) != 0)
3979 return FALSE;
3980 if (xtensa_operand_get_field (isa, opcode, 1,
3981 fmt, 0, slotbuf, &rawval1) != 0)
3982 return FALSE;
3983 if (rawval0 == rawval1) /* it is a nop */
3984 return FALSE;
3985 }
3986 if (is_branch)
3987 check_operand_count--;
3988
3989 for (i = 0; i < check_operand_count; ++i)
3990 {
3991 int new_i = i;
3992 if (is_or && i == o_operand_count - 1)
3993 new_i = i - 1;
3994 if (xtensa_operand_get_field (isa, opcode, new_i, fmt, 0,
3995 slotbuf, &value)
3996 || xtensa_operand_decode (isa, opcode, new_i, &value))
3997 return FALSE;
3998
3999 /* PC-relative branches need adjustment, but
4000 the PC-rel operand will always have a relocation. */
4001 newval = value;
4002 if (xtensa_operand_do_reloc (isa, o_opcode, i, &newval,
4003 self_address)
4004 || xtensa_operand_encode (isa, o_opcode, i, &newval)
4005 || xtensa_operand_set_field (isa, o_opcode, i, o_fmt, 0,
4006 o_slotbuf, newval))
4007 return FALSE;
4008 }
4009
4010 if (xtensa_format_set_slot (isa, o_fmt, 0, o_insnbuf, o_slotbuf))
4011 return FALSE;
4012
4013 if (do_it)
4014 xtensa_insnbuf_to_chars (isa, o_insnbuf, contents + offset,
4015 content_length - offset);
4016 return TRUE;
4017 }
4018 }
4019 return FALSE;
4020 }
4021
4022
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
4027 can be encoded. */
4028
4029 static xtensa_format *op_single_fmt_table = NULL;
4030
4031
4032 static xtensa_format
4033 get_single_format (opcode)
4034 xtensa_opcode opcode;
4035 {
4036 init_op_single_format_table ();
4037 return op_single_fmt_table[opcode];
4038 }
4039
4040
4041 static void
4042 init_op_single_format_table ()
4043 {
4044 xtensa_isa isa = xtensa_default_isa;
4045 xtensa_insnbuf ibuf;
4046 xtensa_opcode opcode;
4047 xtensa_format fmt;
4048 int num_opcodes;
4049
4050 if (op_single_fmt_table != NULL)
4051 return;
4052
4053 ibuf = xtensa_insnbuf_alloc (isa);
4054 num_opcodes = xtensa_isa_num_opcodes (isa);
4055
4056 op_single_fmt_table = (xtensa_format *)
4057 bfd_malloc (sizeof (xtensa_format) * num_opcodes);
4058 for (opcode = 0; opcode < num_opcodes; opcode++)
4059 {
4060 op_single_fmt_table[opcode] = XTENSA_UNDEFINED;
4061 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
4062 {
4063 if (xtensa_format_num_slots (isa, fmt) == 1
4064 && xtensa_opcode_encode (isa, fmt, 0, ibuf, opcode) == 0)
4065 {
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;
4071 }
4072 }
4073 }
4074 xtensa_insnbuf_free (isa, ibuf);
4075 }
4076
4077 \f
4078 /* Code for transforming CALLs at link-time. */
4079
4080 static bfd_reloc_status_type
4081 elf_xtensa_do_asm_simplify (contents, address, content_length, error_message)
4082 bfd_byte *contents;
4083 bfd_vma address;
4084 bfd_vma content_length;
4085 char **error_message;
4086 {
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;
4094 int opn;
4095
4096 if (insnbuf == NULL)
4097 {
4098 insnbuf = xtensa_insnbuf_alloc (isa);
4099 slotbuf = xtensa_insnbuf_alloc (isa);
4100 }
4101
4102 if (content_length < address)
4103 {
4104 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4105 return bfd_reloc_other;
4106 }
4107
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)
4111 {
4112 *error_message = _("Attempt to convert L32R/CALLX to CALL failed");
4113 return bfd_reloc_other;
4114 }
4115
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++)
4121 {
4122 uint32 regno = 1;
4123 xtensa_operand_encode (isa, opcode, opn, &regno);
4124 xtensa_operand_set_field (isa, opcode, opn, core_format, 0,
4125 slotbuf, regno);
4126 }
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);
4130
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);
4134
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);
4139
4140 return bfd_reloc_ok;
4141 }
4142
4143
4144 static bfd_reloc_status_type
4145 contract_asm_expansion (contents, content_length, irel, error_message)
4146 bfd_byte *contents;
4147 bfd_vma content_length;
4148 Elf_Internal_Rela *irel;
4149 char **error_message;
4150 {
4151 bfd_reloc_status_type retval =
4152 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length,
4153 error_message);
4154
4155 if (retval != bfd_reloc_ok)
4156 return bfd_reloc_dangerous;
4157
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;
4163 }
4164
4165
4166 static xtensa_opcode
4167 swap_callx_for_call_opcode (opcode)
4168 xtensa_opcode opcode;
4169 {
4170 init_call_opcodes ();
4171
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;
4176
4177 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
4178 return XTENSA_UNDEFINED;
4179 }
4180
4181
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. */
4185
4186 #define L32R_TARGET_REG_OPERAND 0
4187 #define CONST16_TARGET_REG_OPERAND 0
4188 #define CALLN_SOURCE_OPERAND 0
4189
4190 static xtensa_opcode
4191 get_expanded_call_opcode (buf, bufsize, p_uses_l32r)
4192 bfd_byte *buf;
4193 int bufsize;
4194 bfd_boolean *p_uses_l32r;
4195 {
4196 static xtensa_insnbuf insnbuf = NULL;
4197 static xtensa_insnbuf slotbuf = NULL;
4198 xtensa_format fmt;
4199 xtensa_opcode opcode;
4200 xtensa_isa isa = xtensa_default_isa;
4201 uint32 regno, const16_regno, call_regno;
4202 int offset = 0;
4203
4204 if (insnbuf == NULL)
4205 {
4206 insnbuf = xtensa_insnbuf_alloc (isa);
4207 slotbuf = xtensa_insnbuf_alloc (isa);
4208 }
4209
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;
4215
4216 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
4217 if (opcode == XTENSA_UNDEFINED)
4218 return XTENSA_UNDEFINED;
4219
4220 if (opcode == get_l32r_opcode ())
4221 {
4222 if (p_uses_l32r)
4223 *p_uses_l32r = TRUE;
4224 if (xtensa_operand_get_field (isa, opcode, L32R_TARGET_REG_OPERAND,
4225 fmt, 0, slotbuf, &regno)
4226 || xtensa_operand_decode (isa, opcode, L32R_TARGET_REG_OPERAND,
4227 &regno))
4228 return XTENSA_UNDEFINED;
4229 }
4230 else if (opcode == get_const16_opcode ())
4231 {
4232 if (p_uses_l32r)
4233 *p_uses_l32r = FALSE;
4234 if (xtensa_operand_get_field (isa, opcode, CONST16_TARGET_REG_OPERAND,
4235 fmt, 0, slotbuf, &regno)
4236 || xtensa_operand_decode (isa, opcode, CONST16_TARGET_REG_OPERAND,
4237 &regno))
4238 return XTENSA_UNDEFINED;
4239
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;
4250
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,
4254 &const16_regno)
4255 || const16_regno != regno)
4256 return XTENSA_UNDEFINED;
4257 }
4258 else
4259 return XTENSA_UNDEFINED;
4260
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;
4272
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,
4276 &call_regno))
4277 return XTENSA_UNDEFINED;
4278
4279 if (call_regno != regno)
4280 return XTENSA_UNDEFINED;
4281
4282 return opcode;
4283 }
4284
4285 \f
4286 /* Data structures used during relaxation. */
4287
4288 /* r_reloc: relocation values. */
4289
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.
4296
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
4299 the relocation.
4300
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. */
4304
4305 typedef struct r_reloc_struct r_reloc;
4306
4307 struct r_reloc_struct
4308 {
4309 bfd *abfd;
4310 Elf_Internal_Rela rela;
4311 bfd_vma target_offset;
4312 bfd_vma virtual_offset;
4313 };
4314
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 *));
4327 #if DEBUG
4328 static void print_r_reloc
4329 PARAMS ((FILE *fp, const r_reloc *r));
4330 #endif /* DEBUG */
4331
4332
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. */
4338
4339 static bfd_boolean
4340 r_reloc_is_const (r_rel)
4341 const r_reloc *r_rel;
4342 {
4343 return (r_rel->abfd == NULL);
4344 }
4345
4346
4347 static void
4348 r_reloc_init (r_rel, abfd, irel, contents, content_length)
4349 r_reloc *r_rel;
4350 bfd *abfd;
4351 Elf_Internal_Rela *irel;
4352 bfd_byte *contents;
4353 bfd_size_type content_length;
4354 {
4355 int r_type;
4356 reloc_howto_type *howto;
4357
4358 if (irel != NULL)
4359 {
4360 r_rel->rela = *irel;
4361 r_rel->abfd = abfd;
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)
4367 {
4368 bfd_vma inplace_val;
4369 BFD_ASSERT (r_rel->rela.r_offset < content_length);
4370
4371 inplace_val = bfd_get_32 (abfd, &contents[r_rel->rela.r_offset]);
4372 r_rel->target_offset += inplace_val;
4373 }
4374 }
4375 else
4376 memset (r_rel, 0, sizeof (r_reloc));
4377 }
4378
4379
4380 static bfd_vma
4381 r_reloc_get_target_offset (r_rel)
4382 const r_reloc *r_rel;
4383 {
4384 bfd_vma target_offset;
4385 unsigned long r_symndx;
4386
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);
4391 }
4392
4393
4394 static struct elf_link_hash_entry *
4395 r_reloc_get_hash_entry (r_rel)
4396 const r_reloc *r_rel;
4397 {
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);
4400 }
4401
4402
4403 static asection *
4404 r_reloc_get_section (r_rel)
4405 const r_reloc *r_rel;
4406 {
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);
4409 }
4410
4411
4412 static bfd_boolean
4413 r_reloc_is_defined (r_rel)
4414 const r_reloc *r_rel;
4415 {
4416 asection *sec;
4417 if (r_rel == NULL)
4418 return FALSE;
4419
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)
4424 return FALSE;
4425 return TRUE;
4426 }
4427
4428
4429 #if DEBUG
4430
4431 static void
4432 print_r_reloc (fp, r_rel)
4433 FILE *fp;
4434 const r_reloc *r_rel;
4435 {
4436 if (r_reloc_is_defined (r_rel))
4437 {
4438 asection *sec = r_reloc_get_section (r_rel);
4439 fprintf (fp, " %s(%s + ", sec->owner->filename, sec->name);
4440 }
4441 else if (r_reloc_get_hash_entry (r_rel))
4442 fprintf (fp, " %s + ", r_reloc_get_hash_entry (r_rel)->root.root.string);
4443 else
4444 fprintf (fp, " ?? + ");
4445
4446 fprintf_vma (fp, r_rel->target_offset);
4447 if (r_rel->virtual_offset)
4448 {
4449 fprintf (fp, " + ");
4450 fprintf_vma (fp, r_rel->virtual_offset);
4451 }
4452
4453 fprintf (fp, ")");
4454 }
4455
4456 #endif /* DEBUG */
4457
4458 \f
4459 /* source_reloc: relocations that reference literals. */
4460
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).
4466
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). */
4475
4476 typedef struct source_reloc_struct source_reloc;
4477
4478 struct source_reloc_struct
4479 {
4480 asection *source_sec;
4481 r_reloc r_rel;
4482 xtensa_opcode opcode;
4483 int opnd;
4484 bfd_boolean is_null;
4485 bfd_boolean is_abs_literal;
4486 };
4487
4488
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));
4496
4497
4498 static void
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;
4504 int opnd;
4505 bfd_boolean is_abs_literal;
4506 {
4507 reloc->source_sec = source_sec;
4508 reloc->r_rel = *r_rel;
4509 reloc->opcode = opcode;
4510 reloc->opnd = opnd;
4511 reloc->is_null = FALSE;
4512 reloc->is_abs_literal = is_abs_literal;
4513 }
4514
4515
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. */
4519
4520 static source_reloc *
4521 find_source_reloc (src_relocs, src_count, sec, irel)
4522 source_reloc *src_relocs;
4523 int src_count;
4524 asection *sec;
4525 Elf_Internal_Rela *irel;
4526 {
4527 int i;
4528
4529 for (i = 0; i < src_count; i++)
4530 {
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];
4536 }
4537
4538 return NULL;
4539 }
4540
4541
4542 static int
4543 source_reloc_compare (ap, bp)
4544 const PTR ap;
4545 const PTR bp;
4546 {
4547 const source_reloc *a = (const source_reloc *) ap;
4548 const source_reloc *b = (const source_reloc *) bp;
4549
4550 if (a->r_rel.target_offset != b->r_rel.target_offset)
4551 return (a->r_rel.target_offset - b->r_rel.target_offset);
4552
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. */
4559
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);
4563 }
4564
4565 \f
4566 /* Literal values and value hash tables. */
4567
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.
4572
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
4577 for coalescing. */
4578
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;
4582
4583 struct literal_value_struct
4584 {
4585 r_reloc r_rel;
4586 unsigned long value;
4587 bfd_boolean is_abs_literal;
4588 };
4589
4590 struct value_map_struct
4591 {
4592 literal_value val; /* The literal value. */
4593 r_reloc loc; /* Location of the literal. */
4594 value_map *next;
4595 };
4596
4597 struct value_map_hash_table_struct
4598 {
4599 unsigned bucket_count;
4600 value_map **buckets;
4601 unsigned count;
4602 bfd_boolean has_last_loc;
4603 r_reloc last_loc;
4604 };
4605
4606
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
4612 PARAMS ((void));
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
4618 PARAMS ((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 *,
4623 bfd_boolean));
4624
4625
4626 static void
4627 init_literal_value (lit, r_rel, value, is_abs_literal)
4628 literal_value *lit;
4629 const r_reloc *r_rel;
4630 unsigned long value;
4631 bfd_boolean is_abs_literal;
4632 {
4633 lit->r_rel = *r_rel;
4634 lit->value = value;
4635 lit->is_abs_literal = is_abs_literal;
4636 }
4637
4638
4639 static bfd_boolean
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;
4644 {
4645 struct elf_link_hash_entry *h1, *h2;
4646
4647 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
4648 return FALSE;
4649
4650 if (r_reloc_is_const (&src1->r_rel))
4651 return (src1->value == src2->value);
4652
4653 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
4654 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
4655 return FALSE;
4656
4657 if (src1->r_rel.target_offset != src2->r_rel.target_offset)
4658 return FALSE;
4659
4660 if (src1->r_rel.virtual_offset != src2->r_rel.virtual_offset)
4661 return FALSE;
4662
4663 if (src1->value != src2->value)
4664 return FALSE;
4665
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))))
4674 {
4675 if (r_reloc_get_section (&src1->r_rel)
4676 != r_reloc_get_section (&src2->r_rel))
4677 return FALSE;
4678 }
4679 else
4680 {
4681 /* Require that the hash entries (i.e., symbols) be identical. */
4682 if (h1 != h2 || h1 == 0)
4683 return FALSE;
4684 }
4685
4686 if (src1->is_abs_literal != src2->is_abs_literal)
4687 return FALSE;
4688
4689 return TRUE;
4690 }
4691
4692
4693 /* Must be power of 2. */
4694 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
4695
4696 static value_map_hash_table *
4697 value_map_hash_table_init ()
4698 {
4699 value_map_hash_table *values;
4700
4701 values = (value_map_hash_table *)
4702 bfd_zmalloc (sizeof (value_map_hash_table));
4703 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
4704 values->count = 0;
4705 values->buckets = (value_map **)
4706 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
4707 if (values->buckets == NULL)
4708 {
4709 free (values);
4710 return NULL;
4711 }
4712 values->has_last_loc = FALSE;
4713
4714 return values;
4715 }
4716
4717
4718 static void
4719 value_map_hash_table_delete (table)
4720 value_map_hash_table *table;
4721 {
4722 free (table->buckets);
4723 free (table);
4724 }
4725
4726
4727 static unsigned
4728 hash_bfd_vma (val)
4729 bfd_vma val;
4730 {
4731 return (val >> 2) + (val >> 10);
4732 }
4733
4734
4735 static unsigned
4736 literal_value_hash (src)
4737 const literal_value *src;
4738 {
4739 unsigned hash_val;
4740
4741 hash_val = hash_bfd_vma (src->value);
4742 if (!r_reloc_is_const (&src->r_rel))
4743 {
4744 void *sec_or_hash;
4745
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);
4749
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);
4753 else
4754 sec_or_hash = r_reloc_get_hash_entry (&src->r_rel);
4755 hash_val += hash_bfd_vma ((bfd_vma) (unsigned) sec_or_hash);
4756 }
4757 return hash_val;
4758 }
4759
4760
4761 /* Check if the specified literal_value has been seen before. */
4762
4763 static value_map *
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;
4768 {
4769 value_map *map_e;
4770 value_map *bucket;
4771 unsigned idx;
4772
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)
4777 {
4778 if (literal_value_equal (&map_e->val, val, final_static_link))
4779 return map_e;
4780 }
4781 return NULL;
4782 }
4783
4784
4785 /* Record a new literal value. It is illegal to call this if VALUE
4786 already has an entry here. */
4787
4788 static value_map *
4789 add_value_map (map, val, loc, final_static_link)
4790 value_map_hash_table *map;
4791 const literal_value *val;
4792 const r_reloc *loc;
4793 bfd_boolean final_static_link;
4794 {
4795 value_map **bucket_p;
4796 unsigned idx;
4797
4798 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
4799 if (val_e == NULL)
4800 {
4801 bfd_set_error (bfd_error_no_memory);
4802 return NULL;
4803 }
4804
4805 BFD_ASSERT (!value_map_get_cached_value (map, val, final_static_link));
4806 val_e->val = *val;
4807 val_e->loc = *loc;
4808
4809 idx = literal_value_hash (val);
4810 idx = idx & (map->bucket_count - 1);
4811 bucket_p = &map->buckets[idx];
4812
4813 val_e->next = *bucket_p;
4814 *bucket_p = val_e;
4815 map->count++;
4816 /* FIXME: Consider resizing the hash table if we get too many entries. */
4817
4818 return val_e;
4819 }
4820
4821 \f
4822 /* Lists of text actions (ta_) for narrowing, widening, longcall
4823 conversion, space fill, code & literal removal, etc. */
4824
4825 /* The following text actions are generated:
4826
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
4838 or replaced.
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
4843 same location.
4844
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. */
4848
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;
4852
4853 enum text_action_enum_t
4854 {
4855 ta_none,
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 */
4862 ta_remove_literal,
4863 ta_add_literal
4864 };
4865
4866
4867 /* Structure for a text action record. */
4868 struct text_action_struct
4869 {
4870 text_action_t action;
4871 asection *sec; /* Optional */
4872 bfd_vma offset;
4873 bfd_vma virtual_offset; /* Zero except for adding literals. */
4874 int removed_bytes;
4875 literal_value value; /* Only valid when adding literals. */
4876
4877 text_action *next;
4878 };
4879
4880
4881 /* List of all of the actions taken on a text section. */
4882 struct text_action_list_struct
4883 {
4884 text_action *head;
4885 };
4886
4887
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));
4905 #if DEBUG
4906 static void print_action_list
4907 PARAMS ((FILE *, text_action_list *));
4908 #endif
4909
4910
4911 text_action *
4912 find_fill_action (l, sec, offset)
4913 text_action_list *l;
4914 asection *sec;
4915 bfd_vma offset;
4916 {
4917 text_action **m_p;
4918
4919 /* It is not necessary to fill at the end of a section. */
4920 if (sec->size == offset)
4921 return NULL;
4922
4923 for (m_p = &l->head;
4924 *m_p != NULL && (*m_p)->offset <= offset;
4925 m_p = &(*m_p)->next)
4926 {
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)
4931 return t;
4932 }
4933 return NULL;
4934 }
4935
4936
4937 static int
4938 compute_removed_action_diff (ta, sec, offset, removed, removable_space)
4939 const text_action *ta;
4940 asection *sec;
4941 bfd_vma offset;
4942 int removed;
4943 int removable_space;
4944 {
4945 int new_removed;
4946 int current_removed = 0;
4947
4948 if (ta != NULL)
4949 current_removed = ta->removed_bytes;
4950
4951 BFD_ASSERT (ta == NULL || ta->offset == offset);
4952 BFD_ASSERT (ta == NULL || ta->action == ta_fill);
4953
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;
4957 else
4958 {
4959 int space;
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);
4964
4965 /* Modify for removable. */
4966 space = removable_space - new_removed;
4967 new_removed = (removable_space
4968 - (((1 << sec->alignment_power) - 1) & space));
4969 }
4970 return (new_removed - current_removed);
4971 }
4972
4973
4974 void
4975 adjust_fill_action (ta, fill_diff)
4976 text_action *ta;
4977 int fill_diff;
4978 {
4979 ta->removed_bytes += fill_diff;
4980 }
4981
4982
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. */
4987
4988 static void
4989 text_action_add (l, action, sec, offset, removed)
4990 text_action_list *l;
4991 text_action_t action;
4992 asection *sec;
4993 bfd_vma offset;
4994 int removed;
4995 {
4996 text_action **m_p;
4997 text_action *ta;
4998
4999 /* It is not necessary to fill at the end of a section. */
5000 if (action == ta_fill && sec->size == offset)
5001 return;
5002
5003 /* It is not necessary to fill 0 bytes. */
5004 if (action == ta_fill && removed == 0)
5005 return;
5006
5007 for (m_p = &l->head;
5008 *m_p != NULL && (*m_p)->offset <= offset;
5009 m_p = &(*m_p)->next)
5010 {
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)
5015 {
5016 t->removed_bytes += removed;
5017 return;
5018 }
5019 }
5020
5021 /* Create a new record and fill it up. */
5022 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5023 ta->action = action;
5024 ta->sec = sec;
5025 ta->offset = offset;
5026 ta->removed_bytes = removed;
5027 ta->next = (*m_p);
5028 *m_p = ta;
5029 }
5030
5031
5032 static void
5033 text_action_add_literal (l, action, loc, value, removed)
5034 text_action_list *l;
5035 text_action_t action;
5036 const r_reloc *loc;
5037 const literal_value *value;
5038 int removed;
5039 {
5040 text_action **m_p;
5041 text_action *ta;
5042 asection *sec = r_reloc_get_section (loc);
5043 bfd_vma offset = loc->target_offset;
5044 bfd_vma virtual_offset = loc->virtual_offset;
5045
5046 BFD_ASSERT (action == ta_add_literal);
5047
5048 for (m_p = &l->head; *m_p != NULL; m_p = &(*m_p)->next)
5049 {
5050 if ((*m_p)->offset > offset
5051 && ((*m_p)->offset != offset
5052 || (*m_p)->virtual_offset > virtual_offset))
5053 break;
5054 }
5055
5056 /* Create a new record and fill it up. */
5057 ta = (text_action *) bfd_zmalloc (sizeof (text_action));
5058 ta->action = action;
5059 ta->sec = sec;
5060 ta->offset = offset;
5061 ta->virtual_offset = virtual_offset;
5062 ta->value = *value;
5063 ta->removed_bytes = removed;
5064 ta->next = (*m_p);
5065 *m_p = ta;
5066 }
5067
5068
5069 bfd_vma
5070 offset_with_removed_text (action_list, offset)
5071 text_action_list *action_list;
5072 bfd_vma offset;
5073 {
5074 text_action *r;
5075 int removed = 0;
5076
5077 for (r = action_list->head; r && r->offset <= offset; r = r->next)
5078 {
5079 if (r->offset < offset
5080 || (r->action == ta_fill && r->removed_bytes < 0))
5081 removed += r->removed_bytes;
5082 }
5083
5084 return (offset - removed);
5085 }
5086
5087
5088 bfd_vma
5089 offset_with_removed_text_before_fill (action_list, offset)
5090 text_action_list *action_list;
5091 bfd_vma offset;
5092 {
5093 text_action *r;
5094 int removed = 0;
5095
5096 for (r = action_list->head; r && r->offset < offset; r = r->next)
5097 removed += r->removed_bytes;
5098
5099 return (offset - removed);
5100 }
5101
5102
5103 /* The find_insn_action routine will only find non-fill actions. */
5104
5105 text_action *
5106 find_insn_action (action_list, offset)
5107 text_action_list *action_list;
5108 bfd_vma offset;
5109 {
5110 text_action *t;
5111 for (t = action_list->head; t; t = t->next)
5112 {
5113 if (t->offset == offset)
5114 {
5115 switch (t->action)
5116 {
5117 case ta_none:
5118 case ta_fill:
5119 break;
5120 case ta_remove_insn:
5121 case ta_remove_longcall:
5122 case ta_convert_longcall:
5123 case ta_narrow_insn:
5124 case ta_widen_insn:
5125 return t;
5126 case ta_remove_literal:
5127 case ta_add_literal:
5128 BFD_ASSERT (0);
5129 break;
5130 }
5131 }
5132 }
5133 return NULL;
5134 }
5135
5136
5137 #if DEBUG
5138
5139 static void
5140 print_action_list (fp, action_list)
5141 FILE *fp;
5142 text_action_list *action_list;
5143 {
5144 text_action *r;
5145
5146 fprintf (fp, "Text Action\n");
5147 for (r = action_list->head; r != NULL; r = r->next)
5148 {
5149 const char *t = "unknown";
5150 switch (r->action)
5151 {
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;
5160 case ta_widen_insn:
5161 t = "widen_insn"; break;
5162 case ta_fill:
5163 t = "fill"; break;
5164 case ta_none:
5165 t = "none"; break;
5166 case ta_remove_literal:
5167 t = "remove_literal"; break;
5168 case ta_add_literal:
5169 t = "add_literal"; break;
5170 }
5171
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);
5175 }
5176 }
5177
5178 #endif /* DEBUG */
5179
5180 \f
5181 /* Lists of literals being coalesced or removed. */
5182
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. */
5188
5189 typedef struct removed_literal_struct removed_literal;
5190 typedef struct removed_literal_list_struct removed_literal_list;
5191
5192 struct removed_literal_struct
5193 {
5194 r_reloc from;
5195 r_reloc to;
5196 removed_literal *next;
5197 };
5198
5199 struct removed_literal_list_struct
5200 {
5201 removed_literal *head;
5202 removed_literal *tail;
5203 };
5204
5205
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));
5210 #if DEBUG
5211 static void print_removed_literals
5212 PARAMS ((FILE *, removed_literal_list *));
5213 #endif /* DEBUG */
5214
5215
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. */
5218
5219 static void
5220 add_removed_literal (removed_list, from, to)
5221 removed_literal_list *removed_list;
5222 const r_reloc *from;
5223 const r_reloc *to;
5224 {
5225 removed_literal *r, *new_r, *next_r;
5226
5227 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
5228
5229 new_r->from = *from;
5230 if (to)
5231 new_r->to = *to;
5232 else
5233 new_r->to.abfd = NULL;
5234 new_r->next = NULL;
5235
5236 r = removed_list->head;
5237 if (r == NULL)
5238 {
5239 removed_list->head = new_r;
5240 removed_list->tail = new_r;
5241 }
5242 /* Special check for common case of append. */
5243 else if (removed_list->tail->from.target_offset < from->target_offset)
5244 {
5245 removed_list->tail->next = new_r;
5246 removed_list->tail = new_r;
5247 }
5248 else
5249 {
5250 while (r->from.target_offset < from->target_offset
5251 && r->next != NULL)
5252 {
5253 r = r->next;
5254 }
5255 next_r = r->next;
5256 r->next = new_r;
5257 new_r->next = next_r;
5258 if (next_r == NULL)
5259 removed_list->tail = new_r;
5260 }
5261 }
5262
5263
5264 /* Check if the list of removed literals contains an entry for the
5265 given address. Return the entry if found. */
5266
5267 static removed_literal *
5268 find_removed_literal (removed_list, addr)
5269 removed_literal_list *removed_list;
5270 bfd_vma addr;
5271 {
5272 removed_literal *r = removed_list->head;
5273 while (r && r->from.target_offset < addr)
5274 r = r->next;
5275 if (r && r->from.target_offset == addr)
5276 return r;
5277 return NULL;
5278 }
5279
5280
5281 #if DEBUG
5282
5283 static void
5284 print_removed_literals (fp, removed_list)
5285 FILE *fp;
5286 removed_literal_list *removed_list;
5287 {
5288 removed_literal *r;
5289 r = removed_list->head;
5290 if (r)
5291 fprintf (fp, "Removed Literals\n");
5292 for (; r != NULL; r = r->next)
5293 {
5294 print_r_reloc (fp, &r->from);
5295 fprintf (fp, " => ");
5296 if (r->to.abfd == NULL)
5297 fprintf (fp, "REMOVED");
5298 else
5299 print_r_reloc (fp, &r->to);
5300 fprintf (fp, "\n");
5301 }
5302 }
5303
5304 #endif /* DEBUG */
5305
5306 \f
5307 /* Per-section data for relaxation. */
5308
5309 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
5310
5311 struct xtensa_relax_info_struct
5312 {
5313 bfd_boolean is_relaxable_literal_section;
5314 bfd_boolean is_relaxable_asm_section;
5315 int visited; /* Number of times visited. */
5316
5317 source_reloc *src_relocs; /* Array[src_count]. */
5318 int src_count;
5319 int src_next; /* Next src_relocs entry to assign. */
5320
5321 removed_literal_list removed_list;
5322 text_action_list action_list;
5323
5324 reloc_bfd_fix *fix_list;
5325 reloc_bfd_fix *fix_array;
5326 unsigned fix_array_count;
5327
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;
5336 };
5337
5338 struct elf_xtensa_section_data
5339 {
5340 struct bfd_elf_section_data elf;
5341 xtensa_relax_info relax_info;
5342 };
5343
5344 static void init_xtensa_relax_info
5345 PARAMS ((asection *));
5346 static xtensa_relax_info *get_xtensa_relax_info
5347 PARAMS ((asection *));
5348
5349
5350 static bfd_boolean
5351 elf_xtensa_new_section_hook (abfd, sec)
5352 bfd *abfd;
5353 asection *sec;
5354 {
5355 struct elf_xtensa_section_data *sdata;
5356 bfd_size_type amt = sizeof (*sdata);
5357
5358 sdata = (struct elf_xtensa_section_data *) bfd_zalloc (abfd, amt);
5359 if (sdata == NULL)
5360 return FALSE;
5361 sec->used_by_bfd = (PTR) sdata;
5362
5363 return _bfd_elf_new_section_hook (abfd, sec);
5364 }
5365
5366
5367 static void
5368 init_xtensa_relax_info (sec)
5369 asection *sec;
5370 {
5371 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
5372
5373 relax_info->is_relaxable_literal_section = FALSE;
5374 relax_info->is_relaxable_asm_section = FALSE;
5375 relax_info->visited = 0;
5376
5377 relax_info->src_relocs = NULL;
5378 relax_info->src_count = 0;
5379 relax_info->src_next = 0;
5380
5381 relax_info->removed_list.head = NULL;
5382 relax_info->removed_list.tail = NULL;
5383
5384 relax_info->action_list.head = NULL;
5385
5386 relax_info->fix_list = NULL;
5387 relax_info->fix_array = NULL;
5388 relax_info->fix_array_count = 0;
5389
5390 relax_info->allocated_relocs = NULL;
5391 relax_info->relocs_count = 0;
5392 relax_info->allocated_relocs_count = 0;
5393 }
5394
5395
5396 static xtensa_relax_info *
5397 get_xtensa_relax_info (sec)
5398 asection *sec;
5399 {
5400 struct elf_xtensa_section_data *section_data;
5401
5402 /* No info available if no section or if it is an output section. */
5403 if (!sec || sec == sec->output_section)
5404 return NULL;
5405
5406 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
5407 return &section_data->relax_info;
5408 }
5409
5410 \f
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. */
5419
5420 struct reloc_bfd_fix_struct
5421 {
5422 asection *src_sec;
5423 bfd_vma src_offset;
5424 unsigned src_type; /* Relocation type. */
5425
5426 bfd *target_abfd;
5427 asection *target_sec;
5428 bfd_vma target_offset;
5429 bfd_boolean translated;
5430
5431 reloc_bfd_fix *next;
5432 };
5433
5434
5435 static reloc_bfd_fix *reloc_bfd_fix_init
5436 PARAMS ((asection *, bfd_vma, unsigned, bfd *, asection *, bfd_vma,
5437 bfd_boolean));
5438 static void add_fix
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));
5446
5447
5448 static reloc_bfd_fix *
5449 reloc_bfd_fix_init (src_sec, src_offset, src_type,
5450 target_abfd, target_sec, target_offset, translated)
5451 asection *src_sec;
5452 bfd_vma src_offset;
5453 unsigned src_type;
5454 bfd *target_abfd;
5455 asection *target_sec;
5456 bfd_vma target_offset;
5457 bfd_boolean translated;
5458 {
5459 reloc_bfd_fix *fix;
5460
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;
5469
5470 return fix;
5471 }
5472
5473
5474 static void
5475 add_fix (src_sec, fix)
5476 asection *src_sec;
5477 reloc_bfd_fix *fix;
5478 {
5479 xtensa_relax_info *relax_info;
5480
5481 relax_info = get_xtensa_relax_info (src_sec);
5482 fix->next = relax_info->fix_list;
5483 relax_info->fix_list = fix;
5484 }
5485
5486
5487 static int
5488 fix_compare (ap, bp)
5489 const PTR ap;
5490 const PTR bp;
5491 {
5492 const reloc_bfd_fix *a = (const reloc_bfd_fix *) ap;
5493 const reloc_bfd_fix *b = (const reloc_bfd_fix *) bp;
5494
5495 if (a->src_offset != b->src_offset)
5496 return (a->src_offset - b->src_offset);
5497 return (a->src_type - b->src_type);
5498 }
5499
5500
5501 static void
5502 cache_fix_array (sec)
5503 asection *sec;
5504 {
5505 unsigned i, count = 0;
5506 reloc_bfd_fix *r;
5507 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
5508
5509 if (relax_info == NULL)
5510 return;
5511 if (relax_info->fix_list == NULL)
5512 return;
5513
5514 for (r = relax_info->fix_list; r != NULL; r = r->next)
5515 count++;
5516
5517 relax_info->fix_array =
5518 (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix) * count);
5519 relax_info->fix_array_count = count;
5520
5521 r = relax_info->fix_list;
5522 for (i = 0; i < count; i++, r = r->next)
5523 {
5524 relax_info->fix_array[count - 1 - i] = *r;
5525 relax_info->fix_array[count - 1 - i].next = NULL;
5526 }
5527
5528 qsort (relax_info->fix_array, relax_info->fix_array_count,
5529 sizeof (reloc_bfd_fix), fix_compare);
5530 }
5531
5532
5533 static reloc_bfd_fix *
5534 get_bfd_fix (sec, offset, type)
5535 asection *sec;
5536 bfd_vma offset;
5537 unsigned type;
5538 {
5539 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
5540 reloc_bfd_fix *rv;
5541 reloc_bfd_fix key;
5542
5543 if (relax_info == NULL)
5544 return NULL;
5545 if (relax_info->fix_list == NULL)
5546 return NULL;
5547
5548 if (relax_info->fix_array == NULL)
5549 cache_fix_array (sec);
5550
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);
5555 return rv;
5556 }
5557
5558 \f
5559 /* Section caching. */
5560
5561 typedef struct section_cache_struct section_cache_t;
5562
5563 struct section_cache_struct
5564 {
5565 asection *sec;
5566
5567 bfd_byte *contents; /* Cache of the section contents. */
5568 bfd_size_type content_length;
5569
5570 property_table_entry *ptbl; /* Cache of the section property table. */
5571 unsigned pte_count;
5572
5573 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
5574 unsigned reloc_count;
5575 };
5576
5577
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 *));
5584
5585
5586 static void
5587 init_section_cache (sec_cache)
5588 section_cache_t *sec_cache;
5589 {
5590 memset (sec_cache, 0, sizeof (*sec_cache));
5591 }
5592
5593
5594 static bfd_boolean
5595 section_cache_section (sec_cache, sec, link_info)
5596 section_cache_t *sec_cache;
5597 asection *sec;
5598 struct bfd_link_info *link_info;
5599 {
5600 bfd *abfd;
5601 property_table_entry *prop_table = NULL;
5602 int ptblsize = 0;
5603 bfd_byte *contents = NULL;
5604 Elf_Internal_Rela *internal_relocs = NULL;
5605 bfd_size_type sec_size;
5606
5607 if (sec == NULL)
5608 return FALSE;
5609 if (sec == sec_cache->sec)
5610 return TRUE;
5611
5612 abfd = sec->owner;
5613 sec_size = bfd_get_section_limit (abfd, sec);
5614
5615 /* Get the contents. */
5616 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5617 if (contents == NULL && sec_size != 0)
5618 goto err;
5619
5620 /* Get the relocations. */
5621 internal_relocs = retrieve_internal_relocs (abfd, sec,
5622 link_info->keep_memory);
5623
5624 /* Get the entry table. */
5625 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
5626 XTENSA_PROP_SEC_NAME, FALSE);
5627 if (ptblsize < 0)
5628 goto err;
5629
5630 /* Fill in the new section cache. */
5631 clear_section_cache (sec_cache);
5632 memset (sec_cache, 0, sizeof (sec_cache));
5633
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;
5641
5642 return TRUE;
5643
5644 err:
5645 release_contents (sec, contents);
5646 release_internal_relocs (sec, internal_relocs);
5647 if (prop_table)
5648 free (prop_table);
5649 return FALSE;
5650 }
5651
5652
5653 static void
5654 clear_section_cache (sec_cache)
5655 section_cache_t *sec_cache;
5656 {
5657 if (sec_cache->sec)
5658 {
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));
5664 }
5665 }
5666
5667 \f
5668 /* Extended basic blocks. */
5669
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. */
5678
5679 typedef struct ebb_struct ebb_t;
5680
5681 struct ebb_struct
5682 {
5683 asection *sec;
5684
5685 bfd_byte *contents; /* Cache of the section contents. */
5686 bfd_size_type content_length;
5687
5688 property_table_entry *ptbl; /* Cache of the section property table. */
5689 unsigned pte_count;
5690
5691 Elf_Internal_Rela *relocs; /* Cache of the section relocations. */
5692 unsigned reloc_count;
5693
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. */
5697
5698 bfd_vma end_offset;
5699 unsigned end_ptbl_idx;
5700 unsigned end_reloc_idx;
5701
5702 bfd_boolean ends_section; /* Is this the last ebb in a section? */
5703
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;
5707 };
5708
5709
5710 enum ebb_target_enum
5711 {
5712 EBB_NO_ALIGN = 0,
5713 EBB_DESIRE_TGT_ALIGN,
5714 EBB_REQUIRE_TGT_ALIGN,
5715 EBB_REQUIRE_LOOP_ALIGN,
5716 EBB_REQUIRE_ALIGN
5717 };
5718
5719
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
5726 performed. */
5727
5728 typedef struct proposed_action_struct proposed_action;
5729
5730 struct proposed_action_struct
5731 {
5732 enum ebb_target_enum align_type; /* for the target alignment */
5733 bfd_vma alignment_pow;
5734 text_action_t action;
5735 bfd_vma offset;
5736 int removed_bytes;
5737 bfd_boolean do_action; /* If false, then we will not perform the action. */
5738 };
5739
5740
5741 /* The ebb_constraint_struct keeps a set of proposed actions for an
5742 extended basic block. */
5743
5744 typedef struct ebb_constraint_struct ebb_constraint;
5745
5746 struct ebb_constraint_struct
5747 {
5748 ebb_t ebb;
5749 bfd_boolean start_movable;
5750
5751 /* Bytes of extra space at the beginning if movable. */
5752 int start_extra_space;
5753
5754 enum ebb_target_enum start_align;
5755
5756 bfd_boolean end_movable;
5757
5758 /* Bytes of extra space at the end if movable. */
5759 int end_extra_space;
5760
5761 unsigned action_count;
5762 unsigned action_allocated;
5763
5764 /* Array of proposed actions. */
5765 proposed_action *actions;
5766
5767 /* Action alignments -- one for each proposed action. */
5768 enum ebb_target_enum *action_aligns;
5769 };
5770
5771
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
5780 PARAMS ((ebb_t *));
5781 static bfd_boolean extend_ebb_bounds_forward
5782 PARAMS ((ebb_t *));
5783 static bfd_boolean extend_ebb_bounds_backward
5784 PARAMS ((ebb_t *));
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 *));
5792
5793
5794 static void
5795 init_ebb_constraint (c)
5796 ebb_constraint *c;
5797 {
5798 memset (c, 0, sizeof (ebb_constraint));
5799 }
5800
5801
5802 static void
5803 free_ebb_constraint (c)
5804 ebb_constraint *c;
5805 {
5806 if (c->actions != NULL)
5807 free (c->actions);
5808 }
5809
5810
5811 static void
5812 init_ebb (ebb, sec, contents, content_length, prop_table, ptblsize,
5813 internal_relocs, reloc_count)
5814 ebb_t *ebb;
5815 asection *sec;
5816 bfd_byte *contents;
5817 bfd_size_type content_length;
5818 property_table_entry *prop_table;
5819 unsigned ptblsize;
5820 Elf_Internal_Rela *internal_relocs;
5821 unsigned reloc_count;
5822 {
5823 memset (ebb, 0, sizeof (ebb_t));
5824 ebb->sec = sec;
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;
5837 }
5838
5839
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. */
5845
5846 static bfd_boolean
5847 extend_ebb_bounds (ebb)
5848 ebb_t *ebb;
5849 {
5850 if (!extend_ebb_bounds_forward (ebb))
5851 return FALSE;
5852 if (!extend_ebb_bounds_backward (ebb))
5853 return FALSE;
5854 return TRUE;
5855 }
5856
5857
5858 static bfd_boolean
5859 extend_ebb_bounds_forward (ebb)
5860 ebb_t *ebb;
5861 {
5862 property_table_entry *the_entry, *new_entry;
5863
5864 the_entry = &ebb->ptbl[ebb->end_ptbl_idx];
5865
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. */
5869
5870 while (1)
5871 {
5872 bfd_vma entry_end;
5873 bfd_size_type insn_block_len;
5874
5875 entry_end = the_entry->address - ebb->sec->vma + the_entry->size;
5876 insn_block_len =
5877 insn_block_decodable_len (ebb->contents, ebb->content_length,
5878 ebb->end_offset,
5879 entry_end - ebb->end_offset);
5880 if (insn_block_len != (entry_end - ebb->end_offset))
5881 {
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);
5885 return FALSE;
5886 }
5887 ebb->end_offset += insn_block_len;
5888
5889 if (ebb->end_offset == ebb->sec->size)
5890 ebb->ends_section = TRUE;
5891
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
5895 < ebb->end_offset))
5896 {
5897 ebb->end_reloc_idx++;
5898 }
5899
5900 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
5901 return TRUE;
5902
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))
5907 break;
5908
5909 if (the_entry->address + the_entry->size != new_entry->address)
5910 break;
5911
5912 the_entry = new_entry;
5913 ebb->end_ptbl_idx++;
5914 }
5915
5916 /* Quick check for an unreachable or end of file just at the end. */
5917 if (ebb->end_ptbl_idx + 1 == ebb->pte_count)
5918 {
5919 if (ebb->end_offset == ebb->content_length)
5920 ebb->ends_section = TRUE;
5921 }
5922 else
5923 {
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;
5928 }
5929
5930 /* Any other ending requires exact alignment. */
5931 return TRUE;
5932 }
5933
5934
5935 static bfd_boolean
5936 extend_ebb_bounds_backward (ebb)
5937 ebb_t *ebb;
5938 {
5939 property_table_entry *the_entry, *new_entry;
5940
5941 the_entry = &ebb->ptbl[ebb->start_ptbl_idx];
5942
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. */
5946
5947 while (1)
5948 {
5949 bfd_vma block_begin;
5950 bfd_size_type insn_block_len;
5951
5952 block_begin = the_entry->address - ebb->sec->vma;
5953 insn_block_len =
5954 insn_block_decodable_len (ebb->contents, ebb->content_length,
5955 block_begin,
5956 ebb->start_offset - block_begin);
5957 if (insn_block_len != ebb->start_offset - block_begin)
5958 {
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);
5962 return FALSE;
5963 }
5964 ebb->start_offset -= insn_block_len;
5965
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))
5970 {
5971 ebb->start_reloc_idx--;
5972 }
5973
5974 if (ebb->start_ptbl_idx == 0)
5975 return TRUE;
5976
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))
5981 return TRUE;
5982 if (new_entry->address + new_entry->size != the_entry->address)
5983 return TRUE;
5984
5985 the_entry = new_entry;
5986 ebb->start_ptbl_idx--;
5987 }
5988 return TRUE;
5989 }
5990
5991
5992 static bfd_size_type
5993 insn_block_decodable_len (contents, content_len, block_offset, block_len)
5994 bfd_byte *contents;
5995 bfd_size_type content_len;
5996 bfd_vma block_offset;
5997 bfd_size_type block_len;
5998 {
5999 bfd_vma offset = block_offset;
6000
6001 while (offset < block_offset + block_len)
6002 {
6003 bfd_size_type insn_len = 0;
6004
6005 insn_len = insn_decode_len (contents, content_len, offset);
6006 if (insn_len == 0)
6007 return (offset - block_offset);
6008 offset += insn_len;
6009 }
6010 return (offset - block_offset);
6011 }
6012
6013
6014 static void
6015 ebb_propose_action (c, align_type, alignment_pow, action, offset,
6016 removed_bytes, do_action)
6017 ebb_constraint *c;
6018 bfd_vma alignment_pow;
6019 enum ebb_target_enum align_type;
6020 text_action_t action;
6021 bfd_vma offset;
6022 int removed_bytes;
6023 bfd_boolean do_action;
6024 {
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);
6033 }
6034
6035
6036 static void
6037 ebb_add_proposed_action (c, action)
6038 ebb_constraint *c;
6039 proposed_action *action;
6040 {
6041 unsigned i;
6042 if (c->action_allocated <= c->action_count)
6043 {
6044 unsigned new_allocated = (c->action_count + 2) * 2;
6045 proposed_action *new_actions = (proposed_action *)
6046 bfd_zmalloc (sizeof (proposed_action) * new_allocated);
6047
6048 for (i = 0; i < c->action_count; i++)
6049 new_actions[i] = c->actions[i];
6050 if (c->actions != NULL)
6051 free (c->actions);
6052 c->actions = new_actions;
6053 c->action_allocated = new_allocated;
6054 }
6055 c->actions[c->action_count] = *action;
6056 c->action_count++;
6057 }
6058
6059 \f
6060 /* Access to internal relocations, section contents and symbols. */
6061
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. */
6068
6069 static Elf_Internal_Rela *
6070 retrieve_internal_relocs (abfd, sec, keep_memory)
6071 bfd *abfd;
6072 asection *sec;
6073 bfd_boolean keep_memory;
6074 {
6075 Elf_Internal_Rela *internal_relocs;
6076
6077 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6078 return NULL;
6079
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,
6084 keep_memory));
6085 return internal_relocs;
6086 }
6087
6088
6089 static void
6090 pin_internal_relocs (sec, internal_relocs)
6091 asection *sec;
6092 Elf_Internal_Rela *internal_relocs;
6093 {
6094 elf_section_data (sec)->relocs = internal_relocs;
6095 }
6096
6097
6098 static void
6099 release_internal_relocs (sec, internal_relocs)
6100 asection *sec;
6101 Elf_Internal_Rela *internal_relocs;
6102 {
6103 if (internal_relocs
6104 && elf_section_data (sec)->relocs != internal_relocs)
6105 free (internal_relocs);
6106 }
6107
6108
6109 static bfd_byte *
6110 retrieve_contents (abfd, sec, keep_memory)
6111 bfd *abfd;
6112 asection *sec;
6113 bfd_boolean keep_memory;
6114 {
6115 bfd_byte *contents;
6116 bfd_size_type sec_size;
6117
6118 sec_size = bfd_get_section_limit (abfd, sec);
6119 contents = elf_section_data (sec)->this_hdr.contents;
6120
6121 if (contents == NULL && sec_size != 0)
6122 {
6123 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
6124 {
6125 if (contents != NULL)
6126 free (contents);
6127 return NULL;
6128 }
6129 if (keep_memory)
6130 elf_section_data (sec)->this_hdr.contents = contents;
6131 }
6132 return contents;
6133 }
6134
6135
6136 static void
6137 pin_contents (sec, contents)
6138 asection *sec;
6139 bfd_byte *contents;
6140 {
6141 elf_section_data (sec)->this_hdr.contents = contents;
6142 }
6143
6144
6145 static void
6146 release_contents (sec, contents)
6147 asection *sec;
6148 bfd_byte *contents;
6149 {
6150 if (contents && elf_section_data (sec)->this_hdr.contents != contents)
6151 free (contents);
6152 }
6153
6154
6155 static Elf_Internal_Sym *
6156 retrieve_local_syms (input_bfd)
6157 bfd *input_bfd;
6158 {
6159 Elf_Internal_Shdr *symtab_hdr;
6160 Elf_Internal_Sym *isymbuf;
6161 size_t locsymcount;
6162
6163 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6164 locsymcount = symtab_hdr->sh_info;
6165
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,
6169 NULL, NULL, NULL);
6170
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;
6174
6175 return isymbuf;
6176 }
6177
6178 \f
6179 /* Code for link-time relaxation. */
6180
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 *));
6209
6210 /* First pass: */
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 *,
6224 bfd_boolean));
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,
6229 value_map *));
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 *));
6234
6235 /* Second pass: */
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 *));
6251
6252 /* Third pass: */
6253 static bfd_boolean relax_section_symbols
6254 PARAMS ((bfd *, asection *));
6255
6256
6257 static bfd_boolean
6258 elf_xtensa_relax_section (abfd, sec, link_info, again)
6259 bfd *abfd;
6260 asection *sec;
6261 struct bfd_link_info *link_info;
6262 bfd_boolean *again;
6263 {
6264 static value_map_hash_table *values = NULL;
6265 static bfd_boolean relocations_analyzed = FALSE;
6266 xtensa_relax_info *relax_info;
6267
6268 if (!relocations_analyzed)
6269 {
6270 /* Do some overall initialization for relaxation. */
6271 values = value_map_hash_table_init ();
6272 if (values == NULL)
6273 return FALSE;
6274 relaxing_section = TRUE;
6275 if (!analyze_relocations (link_info))
6276 return FALSE;
6277 relocations_analyzed = TRUE;
6278 }
6279 *again = FALSE;
6280
6281 /* Don't mess with linker-created sections. */
6282 if ((sec->flags & SEC_LINKER_CREATED) != 0)
6283 return TRUE;
6284
6285 relax_info = get_xtensa_relax_info (sec);
6286 BFD_ASSERT (relax_info != NULL);
6287
6288 switch (relax_info->visited)
6289 {
6290 case 0:
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))
6295 return FALSE;
6296 *again = TRUE;
6297 break;
6298
6299 case 1:
6300 if (values)
6301 value_map_hash_table_delete (values);
6302 values = NULL;
6303 if (!relax_section (abfd, sec, link_info))
6304 return FALSE;
6305 *again = TRUE;
6306 break;
6307
6308 case 2:
6309 if (!relax_section_symbols (abfd, sec))
6310 return FALSE;
6311 break;
6312 }
6313
6314 relax_info->visited++;
6315 return TRUE;
6316 }
6317
6318 \f
6319 /* Initialization for relaxation. */
6320
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. */
6332
6333 static bfd_boolean
6334 analyze_relocations (link_info)
6335 struct bfd_link_info *link_info;
6336 {
6337 bfd *abfd;
6338 asection *sec;
6339 bfd_boolean is_relaxable = FALSE;
6340
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)
6344 {
6345 init_xtensa_relax_info (sec);
6346 }
6347
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)
6351 {
6352 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
6353 return FALSE;
6354 }
6355
6356 /* Bail out if there are no relaxable sections. */
6357 if (!is_relaxable)
6358 return TRUE;
6359
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)
6363 {
6364 xtensa_relax_info *relax_info;
6365
6366 relax_info = get_xtensa_relax_info (sec);
6367 if (relax_info->is_relaxable_literal_section
6368 || relax_info->is_relaxable_asm_section)
6369 {
6370 relax_info->src_relocs = (source_reloc *)
6371 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
6372 }
6373 }
6374
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)
6378 {
6379 if (!collect_source_relocs (abfd, sec, link_info))
6380 return FALSE;
6381 }
6382
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)
6386 {
6387 if (!compute_text_actions (abfd, sec, link_info))
6388 return FALSE;
6389 }
6390
6391 return TRUE;
6392 }
6393
6394
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. */
6404
6405 static bfd_boolean
6406 find_relaxable_sections (abfd, sec, link_info, is_relaxable_p)
6407 bfd *abfd;
6408 asection *sec;
6409 struct bfd_link_info *link_info;
6410 bfd_boolean *is_relaxable_p;
6411 {
6412 Elf_Internal_Rela *internal_relocs;
6413 bfd_byte *contents;
6414 bfd_boolean ok = TRUE;
6415 unsigned i;
6416 xtensa_relax_info *source_relax_info;
6417
6418 internal_relocs = retrieve_internal_relocs (abfd, sec,
6419 link_info->keep_memory);
6420 if (internal_relocs == NULL)
6421 return ok;
6422
6423 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
6424 if (contents == NULL && sec->size != 0)
6425 {
6426 ok = FALSE;
6427 goto error_return;
6428 }
6429
6430 source_relax_info = get_xtensa_relax_info (sec);
6431 for (i = 0; i < sec->reloc_count; i++)
6432 {
6433 Elf_Internal_Rela *irel = &internal_relocs[i];
6434 r_reloc r_rel;
6435 asection *target_sec;
6436 xtensa_relax_info *target_relax_info;
6437
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)
6445 {
6446 bfd_boolean is_reachable = FALSE;
6447 if (is_resolvable_asm_expansion (abfd, sec, contents, irel,
6448 link_info, &is_reachable)
6449 && is_reachable)
6450 {
6451 source_relax_info->is_relaxable_asm_section = TRUE;
6452 *is_relaxable_p = TRUE;
6453 }
6454 }
6455
6456 r_reloc_init (&r_rel, abfd, irel, contents,
6457 bfd_get_section_limit (abfd, sec));
6458
6459 target_sec = r_reloc_get_section (&r_rel);
6460 target_relax_info = get_xtensa_relax_info (target_sec);
6461 if (!target_relax_info)
6462 continue;
6463
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++;
6471
6472 if (is_l32r_relocation (abfd, sec, contents, irel)
6473 && r_reloc_is_defined (&r_rel))
6474 {
6475 /* Mark the target section as relaxable. */
6476 target_relax_info->is_relaxable_literal_section = TRUE;
6477 *is_relaxable_p = TRUE;
6478 }
6479 }
6480
6481 error_return:
6482 release_contents (sec, contents);
6483 release_internal_relocs (sec, internal_relocs);
6484 return ok;
6485 }
6486
6487
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. */
6491
6492 static bfd_boolean
6493 collect_source_relocs (abfd, sec, link_info)
6494 bfd *abfd;
6495 asection *sec;
6496 struct bfd_link_info *link_info;
6497 {
6498 Elf_Internal_Rela *internal_relocs;
6499 bfd_byte *contents;
6500 bfd_boolean ok = TRUE;
6501 unsigned i;
6502 bfd_size_type sec_size;
6503
6504 internal_relocs = retrieve_internal_relocs (abfd, sec,
6505 link_info->keep_memory);
6506 if (internal_relocs == NULL)
6507 return ok;
6508
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)
6512 {
6513 ok = FALSE;
6514 goto error_return;
6515 }
6516
6517 /* Record relocations against relaxable literal sections. */
6518 for (i = 0; i < sec->reloc_count; i++)
6519 {
6520 Elf_Internal_Rela *irel = &internal_relocs[i];
6521 r_reloc r_rel;
6522 asection *target_sec;
6523 xtensa_relax_info *target_relax_info;
6524
6525 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
6526
6527 target_sec = r_reloc_get_section (&r_rel);
6528 target_relax_info = get_xtensa_relax_info (target_sec);
6529
6530 if (target_relax_info
6531 && (target_relax_info->is_relaxable_literal_section
6532 || target_relax_info->is_relaxable_asm_section))
6533 {
6534 xtensa_opcode opcode = XTENSA_UNDEFINED;
6535 int opnd = -1;
6536 bfd_boolean is_abs_literal = FALSE;
6537
6538 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
6539 {
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
6543 coalescing. */
6544 opcode = get_relocation_opcode (abfd, sec, contents, irel);
6545 if (opcode == get_l32r_opcode ())
6546 {
6547 is_abs_literal = TRUE;
6548 opnd = 1;
6549 }
6550 else
6551 opcode = XTENSA_UNDEFINED;
6552 }
6553 else if (is_operand_relocation (ELF32_R_TYPE (irel->r_info)))
6554 {
6555 opcode = get_relocation_opcode (abfd, sec, contents, irel);
6556 opnd = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
6557 }
6558
6559 if (opcode != XTENSA_UNDEFINED)
6560 {
6561 int src_next = target_relax_info->src_next++;
6562 source_reloc *s_reloc = &target_relax_info->src_relocs[src_next];
6563
6564 init_source_reloc (s_reloc, sec, &r_rel, opcode, opnd,
6565 is_abs_literal);
6566 }
6567 }
6568 }
6569
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. */
6575
6576 for (i = 0; i < sec->reloc_count; i++)
6577 {
6578 Elf_Internal_Rela *irel = &internal_relocs[i];
6579 bfd_boolean is_reachable;
6580
6581 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
6582 &is_reachable))
6583 continue;
6584
6585 if (is_reachable)
6586 {
6587 Elf_Internal_Rela *l32r_irel;
6588 r_reloc r_rel;
6589 asection *target_sec;
6590 xtensa_relax_info *target_relax_info;
6591
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)
6598 continue;
6599
6600 r_reloc_init (&r_rel, abfd, l32r_irel, contents, sec_size);
6601
6602 target_sec = r_reloc_get_section (&r_rel);
6603 target_relax_info = get_xtensa_relax_info (target_sec);
6604
6605 if (target_relax_info
6606 && (target_relax_info->is_relaxable_literal_section
6607 || target_relax_info->is_relaxable_asm_section))
6608 {
6609 source_reloc *s_reloc;
6610
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,
6617 sec, l32r_irel);
6618 BFD_ASSERT (s_reloc);
6619 s_reloc->is_null = TRUE;
6620 }
6621
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);
6626
6627 pin_internal_relocs (sec, internal_relocs);
6628 }
6629 else
6630 {
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);
6636 }
6637 }
6638
6639 error_return:
6640 release_contents (sec, contents);
6641 release_internal_relocs (sec, internal_relocs);
6642 return ok;
6643 }
6644
6645
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. */
6651
6652 bfd_boolean
6653 is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
6654 is_reachable_p)
6655 bfd *abfd;
6656 asection *sec;
6657 bfd_byte *contents;
6658 Elf_Internal_Rela *irel;
6659 struct bfd_link_info *link_info;
6660 bfd_boolean *is_reachable_p;
6661 {
6662 asection *target_sec;
6663 bfd_vma target_offset;
6664 r_reloc r_rel;
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;
6670
6671 *is_reachable_p = FALSE;
6672
6673 if (contents == NULL)
6674 return FALSE;
6675
6676 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
6677 return FALSE;
6678
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. */
6683 if (!uses_l32r)
6684 return FALSE;
6685
6686 direct_call_opcode = swap_callx_for_call_opcode (opcode);
6687 if (direct_call_opcode == XTENSA_UNDEFINED)
6688 return FALSE;
6689
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))
6693 return FALSE;
6694
6695 target_sec = r_reloc_get_section (&r_rel);
6696 target_offset = r_rel.target_offset;
6697
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)
6703 return FALSE;
6704
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
6707 source. */
6708 if (link_info->relocatable
6709 && (target_sec->output_section != sec->output_section
6710 || is_reloc_sym_weak (abfd, irel)))
6711 return FALSE;
6712
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);
6717
6718 *is_reachable_p = pcrel_reloc_fits (direct_call_opcode, 0,
6719 self_address, dest_address);
6720
6721 if ((self_address >> CALL_SEGMENT_BITS) !=
6722 (dest_address >> CALL_SEGMENT_BITS))
6723 return FALSE;
6724
6725 return TRUE;
6726 }
6727
6728
6729 static Elf_Internal_Rela *
6730 find_associated_l32r_irel (abfd, sec, contents, other_irel, internal_relocs)
6731 bfd *abfd;
6732 asection *sec;
6733 bfd_byte *contents;
6734 Elf_Internal_Rela *other_irel;
6735 Elf_Internal_Rela *internal_relocs;
6736 {
6737 unsigned i;
6738
6739 for (i = 0; i < sec->reloc_count; i++)
6740 {
6741 Elf_Internal_Rela *irel = &internal_relocs[i];
6742
6743 if (irel == other_irel)
6744 continue;
6745 if (irel->r_offset != other_irel->r_offset)
6746 continue;
6747 if (is_l32r_relocation (abfd, sec, contents, irel))
6748 return irel;
6749 }
6750
6751 return NULL;
6752 }
6753
6754
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
6760 alignments.
6761
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
6764 pass. */
6765
6766 bfd_boolean
6767 compute_text_actions (abfd, sec, link_info)
6768 bfd *abfd;
6769 asection *sec;
6770 struct bfd_link_info *link_info;
6771 {
6772 xtensa_relax_info *relax_info;
6773 bfd_byte *contents;
6774 Elf_Internal_Rela *internal_relocs;
6775 bfd_boolean ok = TRUE;
6776 unsigned i;
6777 property_table_entry *prop_table = 0;
6778 int ptblsize = 0;
6779 bfd_size_type sec_size;
6780 static bfd_boolean no_insn_move = FALSE;
6781
6782 if (no_insn_move)
6783 return ok;
6784
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)
6789 return ok;
6790
6791 internal_relocs = retrieve_internal_relocs (abfd, sec,
6792 link_info->keep_memory);
6793
6794 if (internal_relocs)
6795 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
6796 internal_reloc_compare);
6797
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)
6801 {
6802 ok = FALSE;
6803 goto error_return;
6804 }
6805
6806 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
6807 XTENSA_PROP_SEC_NAME, FALSE);
6808 if (ptblsize < 0)
6809 {
6810 ok = FALSE;
6811 goto error_return;
6812 }
6813
6814 for (i = 0; i < sec->reloc_count; i++)
6815 {
6816 Elf_Internal_Rela *irel = &internal_relocs[i];
6817 bfd_vma r_offset;
6818 property_table_entry *the_entry;
6819 int ptbl_idx;
6820 ebb_t *ebb;
6821 ebb_constraint ebb_table;
6822 bfd_size_type simplify_size;
6823
6824 if (irel && ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_SIMPLIFY)
6825 continue;
6826 r_offset = irel->r_offset;
6827
6828 simplify_size = get_asm_simplify_size (contents, sec_size, r_offset);
6829 if (simplify_size == 0)
6830 {
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);
6834 continue;
6835 }
6836
6837 /* If the instruction table is not around, then don't do this
6838 relaxation. */
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)
6842 {
6843 text_action_add (&relax_info->action_list,
6844 ta_convert_longcall, sec, r_offset,
6845 0);
6846 continue;
6847 }
6848
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))
6857 {
6858 ptbl_idx++;
6859 the_entry++;
6860 }
6861
6862 if (the_entry->flags & XTENSA_PROP_INSN_NO_TRANSFORM)
6863 /* NO_REORDER is OK */
6864 continue;
6865
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;
6876
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))
6883 {
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. */
6887
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);
6892 continue;
6893 }
6894
6895 text_action_add_proposed (&relax_info->action_list, &ebb_table, sec);
6896
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);
6901 }
6902
6903 #if DEBUG
6904 if (relax_info->action_list.head != NULL)
6905 print_action_list (stderr, &relax_info->action_list);
6906 #endif
6907
6908 error_return:
6909 release_contents (sec, contents);
6910 release_internal_relocs (sec, internal_relocs);
6911 if (prop_table)
6912 free (prop_table);
6913
6914 return ok;
6915 }
6916
6917
6918 /* Find all of the possible actions for an extended basic block. */
6919
6920 bfd_boolean
6921 compute_ebb_proposed_actions (ebb_table)
6922 ebb_constraint *ebb_table;
6923 {
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;
6927
6928 start_entry = &ebb->ptbl[ebb->start_ptbl_idx];
6929 end_entry = &ebb->ptbl[ebb->end_ptbl_idx];
6930
6931 for (entry = start_entry; entry <= end_entry; entry++)
6932 {
6933 bfd_vma offset, start_offset, end_offset;
6934 bfd_size_type insn_len;
6935
6936 start_offset = entry->address - ebb->sec->vma;
6937 end_offset = entry->address + entry->size - ebb->sec->vma;
6938
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;
6944
6945 if (offset == entry->address - ebb->sec->vma
6946 && (entry->flags & XTENSA_PROP_INSN_BRANCH_TARGET) != 0)
6947 {
6948 enum ebb_target_enum align_type = EBB_DESIRE_TGT_ALIGN;
6949 BFD_ASSERT (offset != end_offset);
6950 if (offset == end_offset)
6951 return FALSE;
6952
6953 insn_len = insn_decode_len (ebb->contents, ebb->content_length,
6954 offset);
6955
6956 /* Propose no actions for a section with an undecodable offset. */
6957 if (insn_len == 0)
6958 {
6959 (*_bfd_error_handler)
6960 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
6961 ebb->sec->owner, ebb->sec, offset);
6962 return FALSE;
6963 }
6964 if (check_branch_target_aligned_address (offset, insn_len))
6965 align_type = EBB_REQUIRE_TGT_ALIGN;
6966
6967 ebb_propose_action (ebb_table, align_type, 0,
6968 ta_none, offset, 0, TRUE);
6969 }
6970
6971 while (offset != end_offset)
6972 {
6973 Elf_Internal_Rela *irel;
6974 xtensa_opcode opcode;
6975
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))))
6981 rel_idx++;
6982
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)
6987 {
6988 bfd_size_type simplify_size;
6989
6990 simplify_size = get_asm_simplify_size (ebb->contents,
6991 ebb->content_length,
6992 irel->r_offset);
6993 if (simplify_size == 0)
6994 {
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);
6998 return FALSE;
6999 }
7000
7001 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7002 ta_convert_longcall, offset, 0, TRUE);
7003
7004 offset += simplify_size;
7005 continue;
7006 }
7007
7008 insn_len = insn_decode_len (ebb->contents, ebb->content_length,
7009 offset);
7010 /* If the instruction is undecodable, then report an error. */
7011 if (insn_len == 0)
7012 {
7013 (*_bfd_error_handler)
7014 (_("%B(%A+0x%lx): could not decode instruction; possible configuration mismatch"),
7015 ebb->sec->owner, ebb->sec, offset);
7016 return FALSE;
7017 }
7018
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,
7022 offset, FALSE))
7023 {
7024 /* Add an instruction narrow action. */
7025 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7026 ta_narrow_insn, offset, 0, FALSE);
7027 offset += insn_len;
7028 continue;
7029 }
7030 if ((entry->flags & XTENSA_PROP_INSN_NO_TRANSFORM) == 0
7031 && widen_instruction (ebb->contents, ebb->content_length,
7032 offset, FALSE))
7033 {
7034 /* Add an instruction widen action. */
7035 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7036 ta_widen_insn, offset, 0, FALSE);
7037 offset += insn_len;
7038 continue;
7039 }
7040 opcode = insn_decode_opcode (ebb->contents, ebb->content_length,
7041 offset, 0);
7042 if (xtensa_opcode_is_loop (xtensa_default_isa, opcode))
7043 {
7044 /* Check for branch targets. */
7045 ebb_propose_action (ebb_table, EBB_REQUIRE_LOOP_ALIGN, 0,
7046 ta_none, offset, 0, TRUE);
7047 offset += insn_len;
7048 continue;
7049 }
7050
7051 offset += insn_len;
7052 }
7053 }
7054
7055 if (ebb->ends_unreachable)
7056 {
7057 ebb_propose_action (ebb_table, EBB_NO_ALIGN, 0,
7058 ta_fill, ebb->end_offset, 0, TRUE);
7059 }
7060
7061 return TRUE;
7062 }
7063
7064
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
7076 transformation.
7077
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. */
7081
7082 bfd_boolean
7083 compute_ebb_actions (ebb_table)
7084 ebb_constraint *ebb_table;
7085 {
7086 unsigned i = 0;
7087 unsigned j;
7088 int removed_bytes = 0;
7089 ebb_t *ebb = &ebb_table->ebb;
7090 unsigned seg_idx_start = 0;
7091 unsigned seg_idx_end = 0;
7092
7093 /* We perform this like the assembler relaxation algorithm: Start by
7094 assuming all instructions are narrow and all no-ops removed; then
7095 walk through.... */
7096
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.
7099 If so, use it. */
7100 for (seg_idx_end = 0; seg_idx_end < ebb_table->action_count; seg_idx_end++)
7101 {
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;
7109
7110 proposed_action *action = NULL;
7111 int align = (1 << ebb_table->ebb.sec->alignment_power);
7112
7113 seg_idx_start = seg_idx_end;
7114
7115 for (i = seg_idx_start; i < ebb_table->action_count; i++)
7116 {
7117 action = &ebb_table->actions[i];
7118 if (action->action == ta_convert_longcall)
7119 longcall_count++;
7120 if (action->action == ta_narrow_insn)
7121 narrowable_count++;
7122 if (action->action == ta_widen_insn)
7123 widenable_count++;
7124 if (action->action == ta_fill)
7125 break;
7126 if (action->align_type == EBB_REQUIRE_LOOP_ALIGN)
7127 break;
7128 if (action->align_type == EBB_REQUIRE_TGT_ALIGN
7129 && !elf32xtensa_size_opt)
7130 break;
7131 }
7132 seg_idx_end = i;
7133
7134 if (seg_idx_end == ebb_table->action_count && !ebb->ends_unreachable)
7135 requires_text_end_align = TRUE;
7136
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)
7140 {
7141 longcall_convert_count = longcall_count;
7142 narrowable_convert_count = narrowable_count;
7143 widenable_convert_count = 0;
7144 }
7145 else
7146 {
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;
7151
7152 for (j = 0; j < longcall_count; j++)
7153 {
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)
7158 {
7159 narrowable_convert_count = desire_narrow;
7160 narrowable_convert_count +=
7161 (align * ((narrowable_count - narrowable_convert_count)
7162 / align));
7163 longcall_convert_count = (longcall_count - j);
7164 widenable_convert_count = 0;
7165 break;
7166 }
7167 if (desire_widen <= widenable_count && !elf32xtensa_size_opt)
7168 {
7169 narrowable_convert_count = 0;
7170 longcall_convert_count = longcall_count - j;
7171 widenable_convert_count = desire_widen;
7172 break;
7173 }
7174 }
7175 }
7176
7177 /* Now the number of conversions are saved. Do them. */
7178 for (i = seg_idx_start; i < seg_idx_end; i++)
7179 {
7180 action = &ebb_table->actions[i];
7181 switch (action->action)
7182 {
7183 case ta_convert_longcall:
7184 if (longcall_convert_count != 0)
7185 {
7186 action->action = ta_remove_longcall;
7187 action->do_action = TRUE;
7188 action->removed_bytes += 3;
7189 longcall_convert_count--;
7190 }
7191 break;
7192 case ta_narrow_insn:
7193 if (narrowable_convert_count != 0)
7194 {
7195 action->do_action = TRUE;
7196 action->removed_bytes += 1;
7197 narrowable_convert_count--;
7198 }
7199 break;
7200 case ta_widen_insn:
7201 if (widenable_convert_count != 0)
7202 {
7203 action->do_action = TRUE;
7204 action->removed_bytes -= 1;
7205 widenable_convert_count--;
7206 }
7207 break;
7208 default:
7209 break;
7210 }
7211 }
7212 }
7213
7214 /* Now we move on to some local opts. Try to remove each of the
7215 remaining longcalls. */
7216
7217 if (ebb_table->ebb.ends_section || ebb_table->ebb.ends_unreachable)
7218 {
7219 removed_bytes = 0;
7220 for (i = 0; i < ebb_table->action_count; i++)
7221 {
7222 int old_removed_bytes = removed_bytes;
7223 proposed_action *action = &ebb_table->actions[i];
7224
7225 if (action->do_action && action->action == ta_convert_longcall)
7226 {
7227 bfd_boolean bad_alignment = FALSE;
7228 removed_bytes += 3;
7229 for (j = i + 1; j < ebb_table->action_count; j++)
7230 {
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)
7234 {
7235 if (!check_branch_target_aligned
7236 (ebb_table->ebb.contents,
7237 ebb_table->ebb.content_length,
7238 offset, offset - removed_bytes))
7239 {
7240 bad_alignment = TRUE;
7241 break;
7242 }
7243 }
7244 if (new_action->align_type == EBB_REQUIRE_LOOP_ALIGN)
7245 {
7246 if (!check_loop_aligned (ebb_table->ebb.contents,
7247 ebb_table->ebb.content_length,
7248 offset,
7249 offset - removed_bytes))
7250 {
7251 bad_alignment = TRUE;
7252 break;
7253 }
7254 }
7255 if (new_action->action == ta_narrow_insn
7256 && !new_action->do_action
7257 && ebb_table->ebb.sec->alignment_power == 2)
7258 {
7259 /* Narrow an instruction and we are done. */
7260 new_action->do_action = TRUE;
7261 new_action->removed_bytes += 1;
7262 bad_alignment = FALSE;
7263 break;
7264 }
7265 if (new_action->action == ta_widen_insn
7266 && new_action->do_action
7267 && ebb_table->ebb.sec->alignment_power == 2)
7268 {
7269 /* Narrow an instruction and we are done. */
7270 new_action->do_action = FALSE;
7271 new_action->removed_bytes += 1;
7272 bad_alignment = FALSE;
7273 break;
7274 }
7275 }
7276 if (!bad_alignment)
7277 {
7278 action->removed_bytes += 3;
7279 action->action = ta_remove_longcall;
7280 action->do_action = TRUE;
7281 }
7282 }
7283 removed_bytes = old_removed_bytes;
7284 if (action->do_action)
7285 removed_bytes += action->removed_bytes;
7286 }
7287 }
7288
7289 removed_bytes = 0;
7290 for (i = 0; i < ebb_table->action_count; ++i)
7291 {
7292 proposed_action *action = &ebb_table->actions[i];
7293 if (action->do_action)
7294 removed_bytes += action->removed_bytes;
7295 }
7296
7297 if ((removed_bytes % (1 << ebb_table->ebb.sec->alignment_power)) != 0
7298 && ebb->ends_unreachable)
7299 {
7300 proposed_action *action;
7301 int br;
7302 int extra_space;
7303
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);
7308
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);
7312
7313 action->removed_bytes = extra_space - br;
7314 }
7315 return TRUE;
7316 }
7317
7318
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
7321 are performed. */
7322
7323 static bfd_boolean
7324 check_section_ebb_pcrels_fit (abfd, sec, contents, internal_relocs, constraint)
7325 bfd *abfd;
7326 asection *sec;
7327 bfd_byte *contents;
7328 Elf_Internal_Rela *internal_relocs;
7329 const ebb_constraint *constraint;
7330 {
7331 unsigned i, j;
7332 Elf_Internal_Rela *irel;
7333 xtensa_relax_info *relax_info;
7334
7335 relax_info = get_xtensa_relax_info (sec);
7336
7337 for (i = 0; i < sec->reloc_count; i++)
7338 {
7339 r_reloc r_rel;
7340 bfd_vma orig_self_offset, orig_target_offset;
7341 bfd_vma self_offset, target_offset;
7342 int r_type;
7343 reloc_howto_type *howto;
7344 int self_removed_bytes, target_removed_bytes;
7345
7346 irel = &internal_relocs[i];
7347 r_type = ELF32_R_TYPE (irel->r_info);
7348
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
7353 PC-relative. */
7354 if (ELF32_R_TYPE (irel->r_info) == R_XTENSA_ASM_SIMPLIFY
7355 || !howto->pc_relative)
7356 continue;
7357
7358 r_reloc_init (&r_rel, abfd, irel, contents,
7359 bfd_get_section_limit (abfd, sec));
7360
7361 if (r_reloc_get_section (&r_rel) != sec)
7362 continue;
7363
7364 orig_self_offset = irel->r_offset;
7365 orig_target_offset = r_rel.target_offset;
7366
7367 self_offset = orig_self_offset;
7368 target_offset = orig_target_offset;
7369
7370 if (relax_info)
7371 {
7372 self_offset = offset_with_removed_text (&relax_info->action_list,
7373 orig_self_offset);
7374 target_offset = offset_with_removed_text (&relax_info->action_list,
7375 orig_target_offset);
7376 }
7377
7378 self_removed_bytes = 0;
7379 target_removed_bytes = 0;
7380
7381 for (j = 0; j < constraint->action_count; ++j)
7382 {
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;
7394 }
7395 self_offset -= self_removed_bytes;
7396 target_offset -= target_removed_bytes;
7397
7398 /* Try to encode it. Get the operand and check. */
7399 if (is_alt_relocation (ELF32_R_TYPE (irel->r_info)))
7400 {
7401 /* None of the current alternate relocs are PC-relative,
7402 and only PC-relative relocs matter here. */
7403 }
7404 else
7405 {
7406 xtensa_opcode opcode;
7407 int opnum;
7408
7409 opcode = get_relocation_opcode (abfd, sec, contents, irel);
7410 if (opcode == XTENSA_UNDEFINED)
7411 return FALSE;
7412
7413 opnum = get_relocation_opnd (opcode, ELF32_R_TYPE (irel->r_info));
7414 if (opnum == XTENSA_UNDEFINED)
7415 return FALSE;
7416
7417 if (!pcrel_reloc_fits (opcode, opnum, self_offset, target_offset))
7418 return FALSE;
7419 }
7420 }
7421
7422 return TRUE;
7423 }
7424
7425
7426 static bfd_boolean
7427 check_section_ebb_reduces (constraint)
7428 const ebb_constraint *constraint;
7429 {
7430 int removed = 0;
7431 unsigned i;
7432
7433 for (i = 0; i < constraint->action_count; i++)
7434 {
7435 const proposed_action *action = &constraint->actions[i];
7436 if (action->do_action)
7437 removed += action->removed_bytes;
7438 }
7439 if (removed < 0)
7440 return FALSE;
7441
7442 return TRUE;
7443 }
7444
7445
7446 void
7447 text_action_add_proposed (l, ebb_table, sec)
7448 text_action_list *l;
7449 const ebb_constraint *ebb_table;
7450 asection *sec;
7451 {
7452 unsigned i;
7453
7454 for (i = 0; i < ebb_table->action_count; i++)
7455 {
7456 proposed_action *action = &ebb_table->actions[i];
7457
7458 if (!action->do_action)
7459 continue;
7460 switch (action->action)
7461 {
7462 case ta_remove_insn:
7463 case ta_remove_longcall:
7464 case ta_convert_longcall:
7465 case ta_narrow_insn:
7466 case ta_widen_insn:
7467 case ta_fill:
7468 case ta_remove_literal:
7469 text_action_add (l, action->action, sec, action->offset,
7470 action->removed_bytes);
7471 break;
7472 case ta_none:
7473 break;
7474 default:
7475 BFD_ASSERT (0);
7476 break;
7477 }
7478 }
7479 }
7480
7481
7482 int
7483 compute_fill_extra_space (entry)
7484 property_table_entry *entry;
7485 {
7486 int fill_extra_space;
7487
7488 if (!entry)
7489 return 0;
7490
7491 if ((entry->flags & XTENSA_PROP_UNREACHABLE) == 0)
7492 return 0;
7493
7494 fill_extra_space = entry->size;
7495 if ((entry->flags & XTENSA_PROP_ALIGN) != 0)
7496 {
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;
7504 }
7505 return fill_extra_space;
7506 }
7507
7508 \f
7509 /* First relaxation pass. */
7510
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. */
7516
7517 static bfd_boolean
7518 compute_removed_literals (abfd, sec, link_info, values)
7519 bfd *abfd;
7520 asection *sec;
7521 struct bfd_link_info *link_info;
7522 value_map_hash_table *values;
7523 {
7524 xtensa_relax_info *relax_info;
7525 bfd_byte *contents;
7526 Elf_Internal_Rela *internal_relocs;
7527 source_reloc *src_relocs, *rel;
7528 bfd_boolean ok = TRUE;
7529 property_table_entry *prop_table = NULL;
7530 int ptblsize;
7531 int i, prev_i;
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;
7536
7537 init_section_cache (&target_sec_cache);
7538
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)
7543 return ok;
7544
7545 internal_relocs = retrieve_internal_relocs (abfd, sec,
7546 link_info->keep_memory);
7547
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)
7551 {
7552 ok = FALSE;
7553 goto error_return;
7554 }
7555
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);
7562
7563 ptblsize = xtensa_read_table_entries (abfd, sec, &prop_table,
7564 XTENSA_PROP_SEC_NAME, FALSE);
7565 if (ptblsize < 0)
7566 {
7567 ok = FALSE;
7568 goto error_return;
7569 }
7570
7571 prev_i = -1;
7572 for (i = 0; i < relax_info->src_count; i++)
7573 {
7574 Elf_Internal_Rela *irel = NULL;
7575
7576 rel = &src_relocs[i];
7577 if (get_l32r_opcode () != rel->opcode)
7578 continue;
7579 irel = get_irel_at_offset (sec, internal_relocs,
7580 rel->r_rel.target_offset);
7581
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))
7587 continue;
7588
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)
7594 continue;
7595 prev_i = i;
7596
7597 if (last_loc_is_prev &&
7598 last_target_offset + 4 != rel->r_rel.target_offset)
7599 last_loc_is_prev = FALSE;
7600
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))
7605 {
7606 if (!remove_dead_literal (abfd, sec, link_info, internal_relocs,
7607 irel, rel, prop_table, ptblsize))
7608 {
7609 ok = FALSE;
7610 goto error_return;
7611 }
7612 last_target_offset = rel->r_rel.target_offset;
7613 continue;
7614 }
7615
7616 if (!identify_literal_placement (abfd, sec, contents, link_info,
7617 values,
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))
7622 {
7623 ok = FALSE;
7624 goto error_return;
7625 }
7626 last_target_offset = rel->r_rel.target_offset;
7627 }
7628
7629 #if DEBUG
7630 print_removed_literals (stderr, &relax_info->removed_list);
7631 print_action_list (stderr, &relax_info->action_list);
7632 #endif /* DEBUG */
7633
7634 error_return:
7635 if (prop_table) free (prop_table);
7636 clear_section_cache (&target_sec_cache);
7637
7638 release_contents (sec, contents);
7639 release_internal_relocs (sec, internal_relocs);
7640 return ok;
7641 }
7642
7643
7644 static Elf_Internal_Rela *
7645 get_irel_at_offset (sec, internal_relocs, offset)
7646 asection *sec;
7647 Elf_Internal_Rela *internal_relocs;
7648 bfd_vma offset;
7649 {
7650 unsigned i;
7651 Elf_Internal_Rela *irel;
7652 unsigned r_type;
7653 Elf_Internal_Rela key;
7654
7655 if (!internal_relocs)
7656 return NULL;
7657
7658 key.r_offset = offset;
7659 irel = bsearch (&key, internal_relocs, sec->reloc_count,
7660 sizeof (Elf_Internal_Rela), internal_reloc_matches);
7661 if (!irel)
7662 return NULL;
7663
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;
7667 while (i > 0)
7668 {
7669 if (internal_relocs[i-1].r_offset != offset)
7670 break;
7671 i--;
7672 }
7673 for ( ; i < sec->reloc_count; i++)
7674 {
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)
7678 return irel;
7679 }
7680
7681 return NULL;
7682 }
7683
7684
7685 bfd_boolean
7686 is_removable_literal (rel, i, src_relocs, src_count)
7687 const source_reloc *rel;
7688 int i;
7689 const source_reloc *src_relocs;
7690 int src_count;
7691 {
7692 const source_reloc *curr_rel;
7693 if (!rel->is_null)
7694 return FALSE;
7695
7696 for (++i; i < src_count; ++i)
7697 {
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)
7701 return TRUE;
7702
7703 if (!curr_rel->is_null
7704 && !xtensa_is_property_section (curr_rel->source_sec)
7705 && !(curr_rel->source_sec->flags & SEC_DEBUGGING))
7706 return FALSE;
7707 }
7708 return TRUE;
7709 }
7710
7711
7712 bfd_boolean
7713 remove_dead_literal (abfd, sec, link_info, internal_relocs,
7714 irel, rel, prop_table, ptblsize)
7715 bfd *abfd;
7716 asection *sec;
7717 struct bfd_link_info *link_info;
7718 Elf_Internal_Rela *internal_relocs;
7719 Elf_Internal_Rela *irel;
7720 source_reloc *rel;
7721 property_table_entry *prop_table;
7722 int ptblsize;
7723 {
7724 property_table_entry *entry;
7725 xtensa_relax_info *relax_info;
7726
7727 relax_info = get_xtensa_relax_info (sec);
7728 if (!relax_info)
7729 return FALSE;
7730
7731 entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
7732 sec->vma + rel->r_rel.target_offset);
7733
7734 /* Mark the unused literal so that it will be removed. */
7735 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
7736
7737 text_action_add (&relax_info->action_list,
7738 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
7739
7740 /* If the section is 4-byte aligned, do not add fill. */
7741 if (sec->alignment_power > 2)
7742 {
7743 int fill_extra_space;
7744 bfd_vma entry_sec_offset;
7745 text_action *fa;
7746 property_table_entry *the_add_entry;
7747 int removed_diff;
7748
7749 if (entry)
7750 entry_sec_offset = entry->address - sec->vma + entry->size;
7751 else
7752 entry_sec_offset = rel->r_rel.target_offset + 4;
7753
7754 /* If the literal range is at the end of the section,
7755 do not add fill. */
7756 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
7757 entry_sec_offset);
7758 fill_extra_space = compute_fill_extra_space (the_add_entry);
7759
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);
7763 if (fa)
7764 adjust_fill_action (fa, removed_diff);
7765 else
7766 text_action_add (&relax_info->action_list,
7767 ta_fill, sec, entry_sec_offset, removed_diff);
7768 }
7769
7770 /* Zero out the relocation on this literal location. */
7771 if (irel)
7772 {
7773 if (elf_hash_table (link_info)->dynamic_sections_created)
7774 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
7775
7776 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
7777 pin_internal_relocs (sec, internal_relocs);
7778 }
7779
7780 /* Do not modify "last_loc_is_prev". */
7781 return TRUE;
7782 }
7783
7784
7785 bfd_boolean
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,
7789 is_abs_literal)
7790 bfd *abfd;
7791 asection *sec;
7792 bfd_byte *contents;
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;
7798 source_reloc *rel;
7799 property_table_entry *prop_table;
7800 int ptblsize;
7801 section_cache_t *target_sec_cache;
7802 bfd_boolean is_abs_literal;
7803 {
7804 literal_value val;
7805 value_map *val_map;
7806 xtensa_relax_info *relax_info;
7807 bfd_boolean literal_placed = FALSE;
7808 r_reloc r_rel;
7809 unsigned long value;
7810 bfd_boolean final_static_link;
7811 bfd_size_type sec_size;
7812
7813 relax_info = get_xtensa_relax_info (sec);
7814 if (!relax_info)
7815 return FALSE;
7816
7817 sec_size = bfd_get_section_limit (abfd, sec);
7818
7819 final_static_link =
7820 (!link_info->relocatable
7821 && !elf_hash_table (link_info)->dynamic_sections_created);
7822
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
7830 location. */
7831
7832 /* Find the literal value. */
7833 value = 0;
7834 r_reloc_init (&r_rel, abfd, irel, contents, sec_size);
7835 if (!irel)
7836 {
7837 BFD_ASSERT (rel->r_rel.target_offset < sec_size);
7838 value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
7839 }
7840 init_literal_value (&val, &r_rel, value, is_abs_literal);
7841
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);
7845
7846 if (val_map
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))
7851 {
7852 /* No change to last_loc_is_prev. */
7853 literal_placed = TRUE;
7854 }
7855
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))
7861 {
7862 asection *target_sec = r_reloc_get_section (&values->last_loc);
7863 if (target_sec && target_sec->output_section == sec->output_section)
7864 {
7865 /* Increment the virtual offset. */
7866 r_reloc try_loc = values->last_loc;
7867 try_loc.virtual_offset += 4;
7868
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))
7874 {
7875 values->last_loc.virtual_offset += 4;
7876 literal_placed = TRUE;
7877 if (!val_map)
7878 val_map = add_value_map (values, &val, &try_loc,
7879 final_static_link);
7880 else
7881 val_map->loc = try_loc;
7882 }
7883 }
7884 }
7885
7886 if (!literal_placed)
7887 {
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;
7891 if (!val_map)
7892 val_map = add_value_map (values, &val, &rel->r_rel, final_static_link);
7893 else
7894 val_map->loc = rel->r_rel;
7895 *last_loc_is_prev_p = TRUE;
7896 }
7897
7898 return TRUE;
7899 }
7900
7901
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. */
7912
7913 static bfd_boolean
7914 relocations_reach (reloc, remaining_relocs, r_rel)
7915 source_reloc *reloc;
7916 int remaining_relocs;
7917 const r_reloc *r_rel;
7918 {
7919 bfd_vma from_offset, source_address, dest_address;
7920 asection *sec;
7921 int i;
7922
7923 if (!r_reloc_is_defined (r_rel))
7924 return FALSE;
7925
7926 sec = r_reloc_get_section (r_rel);
7927 from_offset = reloc[0].r_rel.target_offset;
7928
7929 for (i = 0; i < remaining_relocs; i++)
7930 {
7931 if (reloc[i].r_rel.target_offset != from_offset)
7932 break;
7933
7934 /* Ignore relocations that have been removed. */
7935 if (reloc[i].is_null)
7936 continue;
7937
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)
7942 return FALSE;
7943
7944 /* A literal with no PC-relative relocations can be moved anywhere. */
7945 if (reloc[i].opnd != -1)
7946 {
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);
7954
7955 if (!pcrel_reloc_fits (reloc[i].opcode, reloc[i].opnd,
7956 source_address, dest_address))
7957 return FALSE;
7958 }
7959 }
7960
7961 return TRUE;
7962 }
7963
7964
7965 /* Move a literal to another literal location because it is
7966 the same as the other literal value. */
7967
7968 static bfd_boolean
7969 coalesce_shared_literal (sec, rel, prop_table, ptblsize, val_map)
7970 asection *sec;
7971 source_reloc *rel;
7972 property_table_entry *prop_table;
7973 int ptblsize;
7974 value_map *val_map;
7975 {
7976 property_table_entry *entry;
7977 text_action *fa;
7978 property_table_entry *the_add_entry;
7979 int removed_diff;
7980 xtensa_relax_info *relax_info;
7981
7982 relax_info = get_xtensa_relax_info (sec);
7983 if (!relax_info)
7984 return FALSE;
7985
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))
7989 return TRUE;
7990
7991 /* Mark that the literal will be coalesced. */
7992 add_removed_literal (&relax_info->removed_list, &rel->r_rel, &val_map->loc);
7993
7994 text_action_add (&relax_info->action_list,
7995 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
7996
7997 /* If the section is 4-byte aligned, do not add fill. */
7998 if (sec->alignment_power > 2)
7999 {
8000 int fill_extra_space;
8001 bfd_vma entry_sec_offset;
8002
8003 if (entry)
8004 entry_sec_offset = entry->address - sec->vma + entry->size;
8005 else
8006 entry_sec_offset = rel->r_rel.target_offset + 4;
8007
8008 /* If the literal range is at the end of the section,
8009 do not add fill. */
8010 fill_extra_space = 0;
8011 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8012 entry_sec_offset);
8013 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
8014 fill_extra_space = the_add_entry->size;
8015
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);
8019 if (fa)
8020 adjust_fill_action (fa, removed_diff);
8021 else
8022 text_action_add (&relax_info->action_list,
8023 ta_fill, sec, entry_sec_offset, removed_diff);
8024 }
8025
8026 return TRUE;
8027 }
8028
8029
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. */
8033
8034 static bfd_boolean
8035 move_shared_literal (sec, link_info, rel, prop_table, ptblsize,
8036 target_loc, lit_value, target_sec_cache)
8037 asection *sec;
8038 struct bfd_link_info *link_info;
8039 source_reloc *rel;
8040 property_table_entry *prop_table;
8041 int ptblsize;
8042 const r_reloc *target_loc;
8043 const literal_value *lit_value;
8044 section_cache_t *target_sec_cache;
8045 {
8046 property_table_entry *the_add_entry, *src_entry, *target_entry = NULL;
8047 text_action *fa, *target_fa;
8048 int removed_diff;
8049 xtensa_relax_info *relax_info, *target_relax_info;
8050 asection *target_sec;
8051 ebb_t *ebb;
8052 ebb_constraint ebb_table;
8053 bfd_boolean relocs_fit;
8054
8055 /* If this routine always returns FALSE, the literals that cannot be
8056 coalesced will not be moved. */
8057 if (elf32xtensa_no_literal_movement)
8058 return FALSE;
8059
8060 relax_info = get_xtensa_relax_info (sec);
8061 if (!relax_info)
8062 return FALSE;
8063
8064 target_sec = r_reloc_get_section (target_loc);
8065 target_relax_info = get_xtensa_relax_info (target_sec);
8066
8067 /* Literals to undefined sections may not be moved because they
8068 must report an error. */
8069 if (bfd_is_und_section (target_sec))
8070 return FALSE;
8071
8072 src_entry = elf_xtensa_find_property_entry
8073 (prop_table, ptblsize, sec->vma + rel->r_rel.target_offset);
8074
8075 if (!section_cache_section (target_sec_cache, target_sec, link_info))
8076 return FALSE;
8077
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);
8081
8082 if (!target_entry)
8083 return FALSE;
8084
8085 /* Make sure that we have not broken any branches. */
8086 relocs_fit = FALSE;
8087
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);
8094
8095 /* Propose to add 4 bytes + worst-case alignment size increase to
8096 destination. */
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);
8100
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,
8105 &ebb_table);
8106
8107 if (!relocs_fit)
8108 return FALSE;
8109
8110 text_action_add_literal (&target_relax_info->action_list,
8111 ta_add_literal, target_loc, lit_value, -4);
8112
8113 if (target_sec->alignment_power > 2 && target_entry != src_entry)
8114 {
8115 /* May need to add or remove some fill to maintain alignment. */
8116 int fill_extra_space;
8117 bfd_vma entry_sec_offset;
8118
8119 entry_sec_offset =
8120 target_entry->address - target_sec->vma + target_entry->size;
8121
8122 /* If the literal range is at the end of the section,
8123 do not add fill. */
8124 fill_extra_space = 0;
8125 the_add_entry =
8126 elf_xtensa_find_property_entry (target_sec_cache->ptbl,
8127 target_sec_cache->pte_count,
8128 entry_sec_offset);
8129 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
8130 fill_extra_space = the_add_entry->size;
8131
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,
8136 fill_extra_space);
8137 if (target_fa)
8138 adjust_fill_action (target_fa, removed_diff);
8139 else
8140 text_action_add (&target_relax_info->action_list,
8141 ta_fill, target_sec, entry_sec_offset, removed_diff);
8142 }
8143
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);
8146
8147 /* Remove the literal. */
8148 text_action_add (&relax_info->action_list,
8149 ta_remove_literal, sec, rel->r_rel.target_offset, 4);
8150
8151 /* If the section is 4-byte aligned, do not add fill. */
8152 if (sec->alignment_power > 2 && target_entry != src_entry)
8153 {
8154 int fill_extra_space;
8155 bfd_vma entry_sec_offset;
8156
8157 if (src_entry)
8158 entry_sec_offset = src_entry->address - sec->vma + src_entry->size;
8159 else
8160 entry_sec_offset = rel->r_rel.target_offset+4;
8161
8162 /* If the literal range is at the end of the section,
8163 do not add fill. */
8164 fill_extra_space = 0;
8165 the_add_entry = elf_xtensa_find_property_entry (prop_table, ptblsize,
8166 entry_sec_offset);
8167 if (the_add_entry && (the_add_entry->flags & XTENSA_PROP_UNREACHABLE))
8168 fill_extra_space = the_add_entry->size;
8169
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);
8173 if (fa)
8174 adjust_fill_action (fa, removed_diff);
8175 else
8176 text_action_add (&relax_info->action_list,
8177 ta_fill, sec, entry_sec_offset, removed_diff);
8178 }
8179
8180 return TRUE;
8181 }
8182
8183 \f
8184 /* Second relaxation pass. */
8185
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
8188 section size. */
8189
8190 bfd_boolean
8191 relax_section (abfd, sec, link_info)
8192 bfd *abfd;
8193 asection *sec;
8194 struct bfd_link_info *link_info;
8195 {
8196 Elf_Internal_Rela *internal_relocs;
8197 xtensa_relax_info *relax_info;
8198 bfd_byte *contents;
8199 bfd_boolean ok = TRUE;
8200 unsigned i;
8201 bfd_boolean rv = FALSE;
8202 bfd_boolean virtual_action;
8203 bfd_size_type sec_size;
8204
8205 sec_size = bfd_get_section_limit (abfd, sec);
8206 relax_info = get_xtensa_relax_info (sec);
8207 BFD_ASSERT (relax_info);
8208
8209 /* First translate any of the fixes that have been added already. */
8210 translate_section_fixes (sec);
8211
8212 /* Handle property sections (e.g., literal tables) specially. */
8213 if (xtensa_is_property_section (sec))
8214 {
8215 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
8216 return relax_property_section (abfd, sec, link_info);
8217 }
8218
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)
8223 {
8224 ok = FALSE;
8225 goto error_return;
8226 }
8227
8228 if (internal_relocs)
8229 {
8230 for (i = 0; i < sec->reloc_count; i++)
8231 {
8232 Elf_Internal_Rela *irel;
8233 xtensa_relax_info *target_relax_info;
8234 bfd_vma source_offset, old_source_offset;
8235 r_reloc r_rel;
8236 unsigned r_type;
8237 asection *target_sec;
8238
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,
8242 NULLify it.
8243 Write it back. */
8244
8245 irel = &internal_relocs[i];
8246 source_offset = irel->r_offset;
8247 old_source_offset = source_offset;
8248
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));
8252
8253 /* If this section could have changed then we may need to
8254 change the relocation's offset. */
8255
8256 if (relax_info->is_relaxable_literal_section
8257 || relax_info->is_relaxable_asm_section)
8258 {
8259 if (r_type != R_XTENSA_NONE
8260 && find_removed_literal (&relax_info->removed_list,
8261 irel->r_offset))
8262 {
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);
8270 continue;
8271 }
8272
8273 if (r_type == R_XTENSA_ASM_SIMPLIFY)
8274 {
8275 text_action *action =
8276 find_insn_action (&relax_info->action_list,
8277 irel->r_offset);
8278 if (action && (action->action == ta_convert_longcall
8279 || action->action == ta_remove_longcall))
8280 {
8281 bfd_reloc_status_type retval;
8282 char *error_message = NULL;
8283
8284 retval = contract_asm_expansion (contents, sec_size,
8285 irel, &error_message);
8286 if (retval != bfd_reloc_ok)
8287 {
8288 (*link_info->callbacks->reloc_dangerous)
8289 (link_info, error_message, abfd, sec,
8290 irel->r_offset);
8291 goto error_return;
8292 }
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;
8297 else
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);
8302 }
8303 }
8304
8305 source_offset = offset_with_removed_text
8306 (&relax_info->action_list, irel->r_offset);
8307 irel->r_offset = source_offset;
8308 }
8309
8310 /* If the target section could have changed then
8311 we may need to change the relocation's target offset. */
8312
8313 target_sec = r_reloc_get_section (&r_rel);
8314 target_relax_info = get_xtensa_relax_info (target_sec);
8315
8316 if (target_relax_info
8317 && (target_relax_info->is_relaxable_literal_section
8318 || target_relax_info->is_relaxable_asm_section))
8319 {
8320 r_reloc new_reloc;
8321 reloc_bfd_fix *fix;
8322 bfd_vma addend_displacement;
8323
8324 translate_reloc (&r_rel, &new_reloc);
8325
8326 if (r_type == R_XTENSA_DIFF8
8327 || r_type == R_XTENSA_DIFF16
8328 || r_type == R_XTENSA_DIFF32)
8329 {
8330 bfd_vma diff_value = 0, new_end_offset, diff_mask = 0;
8331
8332 if (bfd_get_section_limit (abfd, sec) < old_source_offset)
8333 {
8334 (*link_info->callbacks->reloc_dangerous)
8335 (link_info, _("invalid relocation address"),
8336 abfd, sec, old_source_offset);
8337 goto error_return;
8338 }
8339
8340 switch (r_type)
8341 {
8342 case R_XTENSA_DIFF8:
8343 diff_value =
8344 bfd_get_8 (abfd, &contents[old_source_offset]);
8345 break;
8346 case R_XTENSA_DIFF16:
8347 diff_value =
8348 bfd_get_16 (abfd, &contents[old_source_offset]);
8349 break;
8350 case R_XTENSA_DIFF32:
8351 diff_value =
8352 bfd_get_32 (abfd, &contents[old_source_offset]);
8353 break;
8354 }
8355
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;
8360
8361 switch (r_type)
8362 {
8363 case R_XTENSA_DIFF8:
8364 diff_mask = 0xff;
8365 bfd_put_8 (abfd, diff_value,
8366 &contents[old_source_offset]);
8367 break;
8368 case R_XTENSA_DIFF16:
8369 diff_mask = 0xffff;
8370 bfd_put_16 (abfd, diff_value,
8371 &contents[old_source_offset]);
8372 break;
8373 case R_XTENSA_DIFF32:
8374 diff_mask = 0xffffffff;
8375 bfd_put_32 (abfd, diff_value,
8376 &contents[old_source_offset]);
8377 break;
8378 }
8379
8380 /* Check for overflow. */
8381 if ((diff_value & ~diff_mask) != 0)
8382 {
8383 (*link_info->callbacks->reloc_dangerous)
8384 (link_info, _("overflow after relaxation"),
8385 abfd, sec, old_source_offset);
8386 goto error_return;
8387 }
8388
8389 pin_contents (sec, contents);
8390 }
8391
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. */
8395
8396 addend_displacement =
8397 new_reloc.target_offset + new_reloc.virtual_offset;
8398
8399 fix = reloc_bfd_fix_init (sec, source_offset, r_type, 0,
8400 r_reloc_get_section (&new_reloc),
8401 addend_displacement, TRUE);
8402 add_fix (sec, fix);
8403 }
8404
8405 pin_internal_relocs (sec, internal_relocs);
8406 }
8407 }
8408
8409 if ((relax_info->is_relaxable_literal_section
8410 || relax_info->is_relaxable_asm_section)
8411 && relax_info->action_list.head)
8412 {
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. */
8416
8417 bfd_size_type size = sec->size;
8418 int removed = 0;
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;
8428
8429 text_action *action = relax_info->action_list.head;
8430
8431 final_size = sec->size;
8432 for (action = relax_info->action_list.head; action;
8433 action = action->next)
8434 {
8435 final_size -= action->removed_bytes;
8436 }
8437
8438 scratch = (bfd_byte *) bfd_zmalloc (final_size);
8439 dup_contents = (bfd_byte *) bfd_zmalloc (final_size);
8440
8441 /* The dot is the current fill location. */
8442 #if DEBUG
8443 print_action_list (stderr, &relax_info->action_list);
8444 #endif
8445
8446 for (action = relax_info->action_list.head; action;
8447 action = action->next)
8448 {
8449 virtual_action = FALSE;
8450 if (action->offset > orig_dot)
8451 {
8452 orig_dot += orig_dot_copied;
8453 orig_dot_copied = 0;
8454 orig_dot_vo = 0;
8455 /* Out of the virtual world. */
8456 }
8457
8458 if (action->offset > orig_dot)
8459 {
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);
8465 }
8466 else if (action->offset < orig_dot)
8467 {
8468 if (action->action == ta_fill
8469 && action->offset - action->removed_bytes == orig_dot)
8470 {
8471 /* This is OK because the fill only effects the dup_dot. */
8472 }
8473 else if (action->action == ta_add_literal)
8474 {
8475 /* TBD. Might need to handle this. */
8476 }
8477 }
8478 if (action->offset == orig_dot)
8479 {
8480 if (action->virtual_offset > orig_dot_vo)
8481 {
8482 if (orig_dot_vo == 0)
8483 {
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;
8490 }
8491 virtual_action = TRUE;
8492 }
8493 else
8494 BFD_ASSERT (action->virtual_offset <= orig_dot_vo);
8495 }
8496 switch (action->action)
8497 {
8498 case ta_remove_literal:
8499 case ta_remove_insn:
8500 BFD_ASSERT (action->removed_bytes >= 0);
8501 orig_dot += action->removed_bytes;
8502 break;
8503
8504 case ta_narrow_insn:
8505 orig_insn_size = 3;
8506 copy_size = 2;
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);
8510 BFD_ASSERT (rv);
8511 memmove (&dup_contents[dup_dot], scratch, copy_size);
8512 orig_dot += orig_insn_size;
8513 dup_dot += copy_size;
8514 break;
8515
8516 case ta_fill:
8517 if (action->removed_bytes >= 0)
8518 orig_dot += action->removed_bytes;
8519 else
8520 {
8521 /* Already zeroed in dup_contents. Just bump the
8522 counters. */
8523 dup_dot += (-action->removed_bytes);
8524 }
8525 break;
8526
8527 case ta_none:
8528 BFD_ASSERT (action->removed_bytes == 0);
8529 break;
8530
8531 case ta_convert_longcall:
8532 case ta_remove_longcall:
8533 /* These will be removed or converted before we get here. */
8534 BFD_ASSERT (0);
8535 break;
8536
8537 case ta_widen_insn:
8538 orig_insn_size = 2;
8539 copy_size = 3;
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);
8543 BFD_ASSERT (rv);
8544 memmove (&dup_contents[dup_dot], scratch, copy_size);
8545 orig_dot += orig_insn_size;
8546 dup_dot += copy_size;
8547 break;
8548
8549 case ta_add_literal:
8550 orig_insn_size = 0;
8551 copy_size = 4;
8552 BFD_ASSERT (action->removed_bytes == -4);
8553 /* TBD -- place the literal value here and insert
8554 into the table. */
8555 memset (&dup_contents[dup_dot], 0, 4);
8556 pin_internal_relocs (sec, internal_relocs);
8557 pin_contents (sec, contents);
8558
8559 if (!move_literal (abfd, link_info, sec, dup_dot, dup_contents,
8560 relax_info, &internal_relocs, &action->value))
8561 goto error_return;
8562
8563 if (virtual_action)
8564 orig_dot_vo += copy_size;
8565
8566 orig_dot += orig_insn_size;
8567 dup_dot += copy_size;
8568 break;
8569
8570 default:
8571 /* Not implemented yet. */
8572 BFD_ASSERT (0);
8573 break;
8574 }
8575
8576 size -= action->removed_bytes;
8577 removed += action->removed_bytes;
8578 BFD_ASSERT (dup_dot <= final_size);
8579 BFD_ASSERT (orig_dot <= orig_size);
8580 }
8581
8582 orig_dot += orig_dot_copied;
8583 orig_dot_copied = 0;
8584
8585 if (orig_dot != orig_size)
8586 {
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;
8593 }
8594 BFD_ASSERT (orig_size == orig_dot);
8595 BFD_ASSERT (final_size == dup_dot);
8596
8597 /* Move the dup_contents back. */
8598 if (final_size > orig_size)
8599 {
8600 /* Contents need to be reallocated. Swap the dup_contents into
8601 contents. */
8602 sec->contents = dup_contents;
8603 free (contents);
8604 contents = dup_contents;
8605 pin_contents (sec, contents);
8606 }
8607 else
8608 {
8609 BFD_ASSERT (final_size <= orig_size);
8610 memset (contents, 0, orig_size);
8611 memcpy (contents, dup_contents, final_size);
8612 free (dup_contents);
8613 }
8614 free (scratch);
8615 pin_contents (sec, contents);
8616
8617 sec->size = final_size;
8618 }
8619
8620 error_return:
8621 release_internal_relocs (sec, internal_relocs);
8622 release_contents (sec, contents);
8623 return ok;
8624 }
8625
8626
8627 static bfd_boolean
8628 translate_section_fixes (sec)
8629 asection *sec;
8630 {
8631 xtensa_relax_info *relax_info;
8632 reloc_bfd_fix *r;
8633
8634 relax_info = get_xtensa_relax_info (sec);
8635 if (!relax_info)
8636 return TRUE;
8637
8638 for (r = relax_info->fix_list; r != NULL; r = r->next)
8639 if (!translate_reloc_bfd_fix (r))
8640 return FALSE;
8641
8642 return TRUE;
8643 }
8644
8645
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. */
8648
8649 static bfd_boolean
8650 translate_reloc_bfd_fix (fix)
8651 reloc_bfd_fix *fix;
8652 {
8653 reloc_bfd_fix new_fix;
8654 asection *sec;
8655 xtensa_relax_info *relax_info;
8656 removed_literal *removed;
8657 bfd_vma new_offset, target_offset;
8658
8659 if (fix->translated)
8660 return TRUE;
8661
8662 sec = fix->target_sec;
8663 target_offset = fix->target_offset;
8664
8665 relax_info = get_xtensa_relax_info (sec);
8666 if (!relax_info)
8667 {
8668 fix->translated = TRUE;
8669 return TRUE;
8670 }
8671
8672 new_fix = *fix;
8673
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)
8677 {
8678 fix->translated = TRUE;
8679 return TRUE;
8680 }
8681
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
8685 section. */
8686
8687 removed = FALSE;
8688 if (is_operand_relocation (fix->src_type))
8689 {
8690 /* Check if the original relocation is against a literal being
8691 removed. */
8692 removed = find_removed_literal (&relax_info->removed_list,
8693 target_offset);
8694 }
8695
8696 if (removed)
8697 {
8698 asection *new_sec;
8699
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);
8703
8704 /* This was moved to some other address (possibly another section). */
8705 new_sec = r_reloc_get_section (&removed->to);
8706 if (new_sec != sec)
8707 {
8708 sec = new_sec;
8709 relax_info = get_xtensa_relax_info (sec);
8710 if (!relax_info ||
8711 (!relax_info->is_relaxable_literal_section
8712 && !relax_info->is_relaxable_asm_section))
8713 {
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;
8718 *fix = new_fix;
8719 return TRUE;
8720 }
8721 }
8722 target_offset = removed->to.target_offset;
8723 new_fix.target_sec = new_sec;
8724 }
8725
8726 /* The target address may have been moved within its section. */
8727 new_offset = offset_with_removed_text (&relax_info->action_list,
8728 target_offset);
8729
8730 new_fix.target_offset = new_offset;
8731 new_fix.target_offset = new_offset;
8732 new_fix.translated = TRUE;
8733 *fix = new_fix;
8734 return TRUE;
8735 }
8736
8737
8738 /* Fix up a relocation to take account of removed literals. */
8739
8740 static void
8741 translate_reloc (orig_rel, new_rel)
8742 const r_reloc *orig_rel;
8743 r_reloc *new_rel;
8744 {
8745 asection *sec;
8746 xtensa_relax_info *relax_info;
8747 removed_literal *removed;
8748 bfd_vma new_offset, target_offset, removed_bytes;
8749
8750 *new_rel = *orig_rel;
8751
8752 if (!r_reloc_is_defined (orig_rel))
8753 return;
8754 sec = r_reloc_get_section (orig_rel);
8755
8756 relax_info = get_xtensa_relax_info (sec);
8757 BFD_ASSERT (relax_info);
8758
8759 if (!relax_info->is_relaxable_literal_section
8760 && !relax_info->is_relaxable_asm_section)
8761 return;
8762
8763 target_offset = orig_rel->target_offset;
8764
8765 removed = FALSE;
8766 if (is_operand_relocation (ELF32_R_TYPE (orig_rel->rela.r_info)))
8767 {
8768 /* Check if the original relocation is against a literal being
8769 removed. */
8770 removed = find_removed_literal (&relax_info->removed_list,
8771 target_offset);
8772 }
8773 if (removed && removed->to.abfd)
8774 {
8775 asection *new_sec;
8776
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);
8780
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);
8785 if (new_sec != sec)
8786 {
8787 sec = new_sec;
8788 relax_info = get_xtensa_relax_info (sec);
8789 if (!relax_info
8790 || (!relax_info->is_relaxable_literal_section
8791 && !relax_info->is_relaxable_asm_section))
8792 return;
8793 }
8794 target_offset = new_rel->target_offset;
8795 }
8796
8797 /* ...and the target address may have been moved within its section. */
8798 new_offset = offset_with_removed_text (&relax_info->action_list,
8799 target_offset);
8800
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;
8805 }
8806
8807
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. */
8816
8817 static void
8818 shrink_dynamic_reloc_sections (info, abfd, input_section, rel)
8819 struct bfd_link_info *info;
8820 bfd *abfd;
8821 asection *input_section;
8822 Elf_Internal_Rela *rel;
8823 {
8824 Elf_Internal_Shdr *symtab_hdr;
8825 struct elf_link_hash_entry **sym_hashes;
8826 unsigned long r_symndx;
8827 int r_type;
8828 struct elf_link_hash_entry *h;
8829 bfd_boolean dynamic_symbol;
8830
8831 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8832 sym_hashes = elf_sym_hashes (abfd);
8833
8834 r_type = ELF32_R_TYPE (rel->r_info);
8835 r_symndx = ELF32_R_SYM (rel->r_info);
8836
8837 if (r_symndx < symtab_hdr->sh_info)
8838 h = NULL;
8839 else
8840 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8841
8842 dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
8843
8844 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
8845 && (input_section->flags & SEC_ALLOC) != 0
8846 && (dynamic_symbol || info->shared))
8847 {
8848 bfd *dynobj;
8849 const char *srel_name;
8850 asection *srel;
8851 bfd_boolean is_plt = FALSE;
8852
8853 dynobj = elf_hash_table (info)->dynobj;
8854 BFD_ASSERT (dynobj != NULL);
8855
8856 if (dynamic_symbol && r_type == R_XTENSA_PLT)
8857 {
8858 srel_name = ".rela.plt";
8859 is_plt = TRUE;
8860 }
8861 else
8862 srel_name = ".rela.got";
8863
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);
8869
8870 if (is_plt)
8871 {
8872 asection *splt, *sgotplt, *srelgot;
8873 int reloc_index, chunk;
8874
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);
8882
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);
8887
8888 /* Check if an entire PLT chunk has just been eliminated. */
8889 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
8890 {
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);
8896 sgotplt->size -= 8;
8897
8898 /* There should be only one entry left (and it will be
8899 removed below). */
8900 BFD_ASSERT (sgotplt->size == 4);
8901 BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
8902 }
8903
8904 BFD_ASSERT (sgotplt->size >= 4);
8905 BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
8906
8907 sgotplt->size -= 4;
8908 splt->size -= PLT_ENTRY_SIZE;
8909 }
8910 }
8911 }
8912
8913
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. */
8920
8921 static bfd_boolean
8922 move_literal (abfd, link_info, sec, offset, contents, relax_info,
8923 internal_relocs_p, lit)
8924 bfd *abfd;
8925 struct bfd_link_info *link_info;
8926 asection *sec;
8927 bfd_vma offset;
8928 bfd_byte *contents;
8929 xtensa_relax_info *relax_info;
8930 Elf_Internal_Rela **internal_relocs_p;
8931 const literal_value *lit;
8932 {
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;
8937
8938 r_rel = &lit->r_rel;
8939 BFD_ASSERT (elf_section_data (sec)->relocs == *internal_relocs_p);
8940
8941 if (r_reloc_is_const (r_rel))
8942 bfd_put_32 (abfd, lit->value, contents + offset);
8943 else
8944 {
8945 int r_type;
8946 unsigned i;
8947 asection *target_sec;
8948 reloc_bfd_fix *fix;
8949 unsigned insert_at;
8950
8951 r_type = ELF32_R_TYPE (r_rel->rela.r_info);
8952 target_sec = r_reloc_get_section (r_rel);
8953
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);
8960
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,
8966 FALSE);
8967 /* We also need to mark that relocations are needed here. */
8968 sec->flags |= SEC_RELOC;
8969
8970 translate_reloc_bfd_fix (fix);
8971 /* This fix has not yet been translated. */
8972 add_fix (sec, fix);
8973
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)
8979 {
8980 if (this_rela.r_offset < (*internal_relocs_p)[i].r_offset)
8981 {
8982 insert_at = i;
8983 break;
8984 }
8985 }
8986
8987 if (*internal_relocs_p != relax_info->allocated_relocs
8988 || sec->reloc_count + 1 > relax_info->allocated_relocs_count)
8989 {
8990 BFD_ASSERT (relax_info->allocated_relocs == NULL
8991 || sec->reloc_count == relax_info->relocs_count);
8992
8993 if (relax_info->allocated_relocs_count == 0)
8994 new_relocs_count = (sec->reloc_count + 2) * 2;
8995 else
8996 new_relocs_count = (relax_info->allocated_relocs_count + 2) * 2;
8997
8998 new_relocs = (Elf_Internal_Rela *)
8999 bfd_zmalloc (sizeof (Elf_Internal_Rela) * (new_relocs_count));
9000 if (!new_relocs)
9001 return FALSE;
9002
9003 /* We could handle this more quickly by finding the split point. */
9004 if (insert_at != 0)
9005 memcpy (new_relocs, *internal_relocs_p,
9006 insert_at * sizeof (Elf_Internal_Rela));
9007
9008 new_relocs[insert_at] = this_rela;
9009
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));
9015
9016 if (*internal_relocs_p != relax_info->allocated_relocs)
9017 {
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);
9023 }
9024 else
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;
9029 sec->reloc_count++;
9030 relax_info->relocs_count = sec->reloc_count;
9031 *internal_relocs_p = new_relocs;
9032 }
9033 else
9034 {
9035 if (insert_at != sec->reloc_count)
9036 {
9037 unsigned idx;
9038 for (idx = sec->reloc_count; idx > insert_at; idx--)
9039 (*internal_relocs_p)[idx] = (*internal_relocs_p)[idx-1];
9040 }
9041 (*internal_relocs_p)[insert_at] = this_rela;
9042 sec->reloc_count++;
9043 if (relax_info->allocated_relocs)
9044 relax_info->relocs_count = sec->reloc_count;
9045 }
9046 }
9047 return TRUE;
9048 }
9049
9050
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. */
9055
9056 static bfd_boolean
9057 relax_property_section (abfd, sec, link_info)
9058 bfd *abfd;
9059 asection *sec;
9060 struct bfd_link_info *link_info;
9061 {
9062 Elf_Internal_Rela *internal_relocs;
9063 bfd_byte *contents;
9064 unsigned i, nexti;
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;
9070
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)
9076 {
9077 ok = FALSE;
9078 goto error_return;
9079 }
9080
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));
9085
9086 if (internal_relocs)
9087 {
9088 for (i = 0; i < sec->reloc_count; i++)
9089 {
9090 Elf_Internal_Rela *irel;
9091 xtensa_relax_info *target_relax_info;
9092 unsigned r_type;
9093 asection *target_sec;
9094 literal_value val;
9095 bfd_byte *size_p, *flags_p;
9096
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
9101 (offset + 4). */
9102
9103 irel = &internal_relocs[i];
9104 r_type = ELF32_R_TYPE (irel->r_info);
9105 if (r_type == R_XTENSA_NONE)
9106 continue;
9107
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];
9111 flags_p = NULL;
9112 if (is_full_prop_section)
9113 {
9114 flags_p = &contents[irel->r_offset + 8];
9115 BFD_ASSERT (irel->r_offset + 12 <= sec_size);
9116 }
9117 else
9118 BFD_ASSERT (irel->r_offset + 8 <= sec_size);
9119
9120 target_sec = r_reloc_get_section (&val.r_rel);
9121 target_relax_info = get_xtensa_relax_info (target_sec);
9122
9123 if (target_relax_info
9124 && (target_relax_info->is_relaxable_literal_section
9125 || target_relax_info->is_relaxable_asm_section ))
9126 {
9127 /* Translate the relocation's destination. */
9128 bfd_vma new_offset, new_end_offset;
9129 long old_size, new_size;
9130
9131 new_offset = offset_with_removed_text
9132 (&target_relax_info->action_list, val.r_rel.target_offset);
9133
9134 /* Assert that we are not out of bounds. */
9135 old_size = bfd_get_32 (abfd, size_p);
9136
9137 if (old_size == 0)
9138 {
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
9146 preceeds it. */
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;
9151 else
9152 {
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);
9157
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. */
9161 if (!flags_p
9162 || (bfd_get_32 (abfd, flags_p)
9163 & XTENSA_PROP_UNREACHABLE) == 0)
9164 new_end_offset = new_offset;
9165 else
9166 {
9167 last_zfill_target_sec = target_sec;
9168 last_zfill_target_offset = val.r_rel.target_offset;
9169 }
9170 }
9171 }
9172 else
9173 {
9174 new_end_offset = offset_with_removed_text_before_fill
9175 (&target_relax_info->action_list,
9176 val.r_rel.target_offset + old_size);
9177 }
9178
9179 new_size = new_end_offset - new_offset;
9180
9181 if (new_size != old_size)
9182 {
9183 bfd_put_32 (abfd, new_size, size_p);
9184 pin_contents (sec, contents);
9185 }
9186
9187 if (new_offset != val.r_rel.target_offset)
9188 {
9189 bfd_vma diff = new_offset - val.r_rel.target_offset;
9190 irel->r_addend += diff;
9191 pin_internal_relocs (sec, internal_relocs);
9192 }
9193 }
9194 }
9195 }
9196
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. */
9200
9201 if (internal_relocs && (!link_info->relocatable
9202 || strcmp (sec->name, XTENSA_LIT_SEC_NAME) == 0))
9203 {
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;
9210
9211 if (is_full_prop_section)
9212 entry_size = 12;
9213 else
9214 entry_size = 8;
9215
9216 predef_flags = xtensa_get_property_predef_flags (sec);
9217
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. */
9223
9224 pin_internal_relocs (sec, internal_relocs);
9225 pin_contents (sec, contents);
9226
9227 last_irel_offset = (bfd_vma) -1;
9228 section_size = sec->size;
9229 BFD_ASSERT (section_size % entry_size == 0);
9230
9231 for (offset = 0; offset < section_size; offset += entry_size)
9232 {
9233 Elf_Internal_Rela *irel, *next_irel;
9234 bfd_vma bytes_to_remove, size, actual_offset;
9235 bfd_boolean remove_this_irel;
9236 flagword flags;
9237
9238 irel = NULL;
9239 next_irel = NULL;
9240
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
9246 after "i". */
9247
9248 for (i = nexti; i < sec->reloc_count; i++)
9249 {
9250 if (ELF32_R_TYPE (internal_relocs[i].r_info) != R_XTENSA_NONE)
9251 {
9252 irel = &internal_relocs[i];
9253 break;
9254 }
9255 internal_relocs[i].r_offset -= removed_bytes;
9256 }
9257
9258 for (nexti = i + 1; nexti < sec->reloc_count; nexti++)
9259 {
9260 if (ELF32_R_TYPE (internal_relocs[nexti].r_info)
9261 != R_XTENSA_NONE)
9262 {
9263 next_irel = &internal_relocs[nexti];
9264 break;
9265 }
9266 internal_relocs[nexti].r_offset -= removed_bytes;
9267 }
9268
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]);
9273
9274 if (is_full_prop_section)
9275 flags = bfd_get_32 (abfd, &contents[actual_offset + 8]);
9276 else
9277 flags = predef_flags;
9278
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);
9283
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))
9288 {
9289 irel->r_offset -= removed_bytes;
9290 last_irel_offset = irel->r_offset;
9291 }
9292 else if (next_irel && (next_irel->r_offset == offset + 4
9293 || (is_full_prop_section
9294 && next_irel->r_offset == offset + 8)))
9295 {
9296 nexti += 1;
9297 irel->r_offset -= removed_bytes;
9298 next_irel->r_offset -= removed_bytes;
9299 last_irel_offset = next_irel->r_offset;
9300 }
9301 else if (size == 0 && (flags & XTENSA_PROP_ALIGN) == 0
9302 && (flags & XTENSA_PROP_UNREACHABLE) == 0)
9303 {
9304 /* Always remove entries with zero size and no alignment. */
9305 bytes_to_remove = entry_size;
9306 if (irel && irel->r_offset == offset)
9307 {
9308 remove_this_irel = TRUE;
9309
9310 irel->r_offset -= removed_bytes;
9311 last_irel_offset = irel->r_offset;
9312 }
9313 }
9314 else if (irel && irel->r_offset == offset)
9315 {
9316 if (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32)
9317 {
9318 if (last_irel)
9319 {
9320 flagword old_flags;
9321 bfd_vma old_size =
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 =
9327 (irel->r_addend
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]);
9332 else
9333 old_flags = predef_flags;
9334
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)
9341 {
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;
9347 }
9348 else
9349 last_irel = irel;
9350 }
9351 else
9352 last_irel = irel;
9353 }
9354
9355 irel->r_offset -= removed_bytes;
9356 last_irel_offset = irel->r_offset;
9357 }
9358
9359 if (remove_this_irel)
9360 {
9361 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
9362 irel->r_offset -= bytes_to_remove;
9363 }
9364
9365 if (bytes_to_remove != 0)
9366 {
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);
9372 }
9373 }
9374
9375 if (removed_bytes)
9376 {
9377 /* Clear the removed bytes. */
9378 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
9379
9380 sec->size = section_size - removed_bytes;
9381
9382 if (xtensa_is_littable_section (sec))
9383 {
9384 bfd *dynobj = elf_hash_table (link_info)->dynobj;
9385 if (dynobj)
9386 {
9387 asection *sgotloc =
9388 bfd_get_section_by_name (dynobj, ".got.loc");
9389 if (sgotloc)
9390 sgotloc->size -= removed_bytes;
9391 }
9392 }
9393 }
9394 }
9395
9396 error_return:
9397 release_internal_relocs (sec, internal_relocs);
9398 release_contents (sec, contents);
9399 return ok;
9400 }
9401
9402 \f
9403 /* Third relaxation pass. */
9404
9405 /* Change symbol values to account for removed literals. */
9406
9407 bfd_boolean
9408 relax_section_symbols (abfd, sec)
9409 bfd *abfd;
9410 asection *sec;
9411 {
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;
9417
9418 relax_info = get_xtensa_relax_info (sec);
9419 BFD_ASSERT (relax_info);
9420
9421 if (!relax_info->is_relaxable_literal_section
9422 && !relax_info->is_relaxable_asm_section)
9423 return TRUE;
9424
9425 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9426
9427 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9428 isymbuf = retrieve_local_syms (abfd);
9429
9430 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
9431 num_locals = symtab_hdr->sh_info;
9432
9433 /* Adjust the local symbols defined in this section. */
9434 for (i = 0; i < num_locals; i++)
9435 {
9436 Elf_Internal_Sym *isym = &isymbuf[i];
9437
9438 if (isym->st_shndx == sec_shndx)
9439 {
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;
9443
9444 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC)
9445 {
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;
9449 }
9450
9451 isym->st_value = new_address;
9452 isym->st_size = new_size;
9453 }
9454 }
9455
9456 /* Now adjust the global symbols defined in this section. */
9457 for (i = 0; i < (num_syms - num_locals); i++)
9458 {
9459 struct elf_link_hash_entry *sym_hash;
9460
9461 sym_hash = elf_sym_hashes (abfd)[i];
9462
9463 if (sym_hash->root.type == bfd_link_hash_warning)
9464 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
9465
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)
9469 {
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;
9473
9474 if (sym_hash->type == STT_FUNC)
9475 {
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;
9480 }
9481
9482 sym_hash->root.u.def.value = new_address;
9483 sym_hash->size = new_size;
9484 }
9485 }
9486
9487 return TRUE;
9488 }
9489
9490 \f
9491 /* "Fix" handling functions, called while performing relocations. */
9492
9493 static bfd_boolean
9494 do_fix_for_relocatable_link (rel, input_bfd, input_section, contents)
9495 Elf_Internal_Rela *rel;
9496 bfd *input_bfd;
9497 asection *input_section;
9498 bfd_byte *contents;
9499 {
9500 r_reloc r_rel;
9501 asection *sec, *old_sec;
9502 bfd_vma old_offset;
9503 int r_type = ELF32_R_TYPE (rel->r_info);
9504 reloc_bfd_fix *fix;
9505
9506 if (r_type == R_XTENSA_NONE)
9507 return TRUE;
9508
9509 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
9510 if (!fix)
9511 return TRUE;
9512
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;
9517
9518 if (!old_sec || !r_reloc_is_defined (&r_rel))
9519 {
9520 if (r_type != R_XTENSA_ASM_EXPAND)
9521 {
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);
9526 return FALSE;
9527 }
9528 /* Leave it be. Resolution will happen in a later stage. */
9529 }
9530 else
9531 {
9532 sec = fix->target_sec;
9533 rel->r_addend += ((sec->output_offset + fix->target_offset)
9534 - (old_sec->output_offset + old_offset));
9535 }
9536 return TRUE;
9537 }
9538
9539
9540 static void
9541 do_fix_for_final_link (rel, input_bfd, input_section, contents, relocationp)
9542 Elf_Internal_Rela *rel;
9543 bfd *input_bfd;
9544 asection *input_section;
9545 bfd_byte *contents;
9546 bfd_vma *relocationp;
9547 {
9548 asection *sec;
9549 int r_type = ELF32_R_TYPE (rel->r_info);
9550 reloc_bfd_fix *fix;
9551 bfd_vma fixup_diff;
9552
9553 if (r_type == R_XTENSA_NONE)
9554 return;
9555
9556 fix = get_bfd_fix (input_section, rel->r_offset, r_type);
9557 if (!fix)
9558 return;
9559
9560 sec = fix->target_sec;
9561
9562 fixup_diff = rel->r_addend;
9563 if (elf_howto_table[fix->src_type].partial_inplace)
9564 {
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;
9570 }
9571
9572 *relocationp = (sec->output_section->vma
9573 + sec->output_offset
9574 + fix->target_offset - fixup_diff);
9575 }
9576
9577 \f
9578 /* Miscellaneous utility functions.... */
9579
9580 static asection *
9581 elf_xtensa_get_plt_section (dynobj, chunk)
9582 bfd *dynobj;
9583 int chunk;
9584 {
9585 char plt_name[10];
9586
9587 if (chunk == 0)
9588 return bfd_get_section_by_name (dynobj, ".plt");
9589
9590 sprintf (plt_name, ".plt.%u", chunk);
9591 return bfd_get_section_by_name (dynobj, plt_name);
9592 }
9593
9594
9595 static asection *
9596 elf_xtensa_get_gotplt_section (dynobj, chunk)
9597 bfd *dynobj;
9598 int chunk;
9599 {
9600 char got_name[14];
9601
9602 if (chunk == 0)
9603 return bfd_get_section_by_name (dynobj, ".got.plt");
9604
9605 sprintf (got_name, ".got.plt.%u", chunk);
9606 return bfd_get_section_by_name (dynobj, got_name);
9607 }
9608
9609
9610 /* Get the input section for a given symbol index.
9611 If the symbol is:
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. */
9617
9618 static asection *
9619 get_elf_r_symndx_section (abfd, r_symndx)
9620 bfd *abfd;
9621 unsigned long r_symndx;
9622 {
9623 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9624 asection *target_sec = NULL;
9625 if (r_symndx < symtab_hdr->sh_info)
9626 {
9627 Elf_Internal_Sym *isymbuf;
9628 unsigned int section_index;
9629
9630 isymbuf = retrieve_local_syms (abfd);
9631 section_index = isymbuf[r_symndx].st_shndx;
9632
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;
9641 else
9642 /* Who knows? */
9643 target_sec = NULL;
9644 }
9645 else
9646 {
9647 unsigned long indx = r_symndx - symtab_hdr->sh_info;
9648 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
9649
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;
9653
9654 switch (h->root.type)
9655 {
9656 case bfd_link_hash_defined:
9657 case bfd_link_hash_defweak:
9658 target_sec = h->root.u.def.section;
9659 break;
9660 case bfd_link_hash_common:
9661 target_sec = bfd_com_section_ptr;
9662 break;
9663 case bfd_link_hash_undefined:
9664 case bfd_link_hash_undefweak:
9665 target_sec = bfd_und_section_ptr;
9666 break;
9667 default: /* New indirect warning. */
9668 target_sec = bfd_und_section_ptr;
9669 break;
9670 }
9671 }
9672 return target_sec;
9673 }
9674
9675
9676 static struct elf_link_hash_entry *
9677 get_elf_r_symndx_hash_entry (abfd, r_symndx)
9678 bfd *abfd;
9679 unsigned long r_symndx;
9680 {
9681 unsigned long indx;
9682 struct elf_link_hash_entry *h;
9683 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9684
9685 if (r_symndx < symtab_hdr->sh_info)
9686 return NULL;
9687
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;
9693 return h;
9694 }
9695
9696
9697 /* Get the section-relative offset for a symbol number. */
9698
9699 static bfd_vma
9700 get_elf_r_symndx_offset (abfd, r_symndx)
9701 bfd *abfd;
9702 unsigned long r_symndx;
9703 {
9704 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9705 bfd_vma offset = 0;
9706
9707 if (r_symndx < symtab_hdr->sh_info)
9708 {
9709 Elf_Internal_Sym *isymbuf;
9710 isymbuf = retrieve_local_syms (abfd);
9711 offset = isymbuf[r_symndx].st_value;
9712 }
9713 else
9714 {
9715 unsigned long indx = r_symndx - symtab_hdr->sh_info;
9716 struct elf_link_hash_entry *h =
9717 elf_sym_hashes (abfd)[indx];
9718
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;
9725 }
9726 return offset;
9727 }
9728
9729
9730 static bfd_boolean
9731 is_reloc_sym_weak (abfd, rel)
9732 bfd *abfd;
9733 Elf_Internal_Rela *rel;
9734 {
9735 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
9736 struct elf_link_hash_entry *h;
9737
9738 h = get_elf_r_symndx_hash_entry (abfd, r_symndx);
9739 if (h && h->root.type == bfd_link_hash_defweak)
9740 return TRUE;
9741 return FALSE;
9742 }
9743
9744
9745 static bfd_boolean
9746 pcrel_reloc_fits (opc, opnd, self_address, dest_address)
9747 xtensa_opcode opc;
9748 int opnd;
9749 bfd_vma self_address;
9750 bfd_vma dest_address;
9751 {
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))
9756 return FALSE;
9757 return TRUE;
9758 }
9759
9760
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;
9765
9766
9767 static bfd_boolean
9768 xtensa_is_property_section (sec)
9769 asection *sec;
9770 {
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)
9774 return TRUE;
9775
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))
9780 return TRUE;
9781
9782 return FALSE;
9783 }
9784
9785
9786 static bfd_boolean
9787 xtensa_is_littable_section (sec)
9788 asection *sec;
9789 {
9790 if (strncmp (XTENSA_LIT_SEC_NAME, sec->name, lit_sec_len) == 0)
9791 return TRUE;
9792
9793 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0
9794 && sec->name[linkonce_len] == 'p'
9795 && sec->name[linkonce_len + 1] == '.')
9796 return TRUE;
9797
9798 return FALSE;
9799 }
9800
9801
9802 static int
9803 internal_reloc_compare (ap, bp)
9804 const PTR ap;
9805 const PTR bp;
9806 {
9807 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
9808 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
9809
9810 if (a->r_offset != b->r_offset)
9811 return (a->r_offset - b->r_offset);
9812
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. */
9819
9820 if (a->r_info != b->r_info)
9821 return (a->r_info - b->r_info);
9822
9823 return (a->r_addend - b->r_addend);
9824 }
9825
9826
9827 static int
9828 internal_reloc_matches (ap, bp)
9829 const PTR ap;
9830 const PTR bp;
9831 {
9832 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
9833 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
9834
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);
9838 }
9839
9840
9841 char *
9842 xtensa_get_property_section_name (sec, base_name)
9843 asection *sec;
9844 const char *base_name;
9845 {
9846 if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
9847 {
9848 char *prop_sec_name;
9849 const char *suffix;
9850 char *linkonce_kind = 0;
9851
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.";
9858 else
9859 abort ();
9860
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);
9865
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] == '.')
9870 suffix += 2;
9871 strcat (prop_sec_name + linkonce_len, suffix);
9872
9873 return prop_sec_name;
9874 }
9875
9876 return strdup (base_name);
9877 }
9878
9879
9880 flagword
9881 xtensa_get_property_predef_flags (sec)
9882 asection *sec;
9883 {
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);
9890
9891 if (xtensa_is_littable_section (sec))
9892 return (XTENSA_PROP_LITERAL
9893 | XTENSA_PROP_INSN_NO_TRANSFORM
9894 | XTENSA_PROP_INSN_NO_REORDER);
9895
9896 return 0;
9897 }
9898
9899 \f
9900 /* Other functions called directly by the linker. */
9901
9902 bfd_boolean
9903 xtensa_callback_required_dependence (abfd, sec, link_info, callback, closure)
9904 bfd *abfd;
9905 asection *sec;
9906 struct bfd_link_info *link_info;
9907 deps_callback_t callback;
9908 PTR closure;
9909 {
9910 Elf_Internal_Rela *internal_relocs;
9911 bfd_byte *contents;
9912 unsigned i;
9913 bfd_boolean ok = TRUE;
9914 bfd_size_type sec_size;
9915
9916 sec_size = bfd_get_section_limit (abfd, sec);
9917
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)
9922 {
9923 asection *sgotplt;
9924
9925 /* Find the corresponding ".got.plt*" section. */
9926 if (sec->name[4] == '\0')
9927 sgotplt = bfd_get_section_by_name (sec->owner, ".got.plt");
9928 else
9929 {
9930 char got_name[14];
9931 int chunk = 0;
9932
9933 BFD_ASSERT (sec->name[4] == '.');
9934 chunk = strtol (&sec->name[5], NULL, 10);
9935
9936 sprintf (got_name, ".got.plt.%u", chunk);
9937 sgotplt = bfd_get_section_by_name (sec->owner, got_name);
9938 }
9939 BFD_ASSERT (sgotplt);
9940
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);
9945 }
9946
9947 internal_relocs = retrieve_internal_relocs (abfd, sec,
9948 link_info->keep_memory);
9949 if (internal_relocs == NULL
9950 || sec->reloc_count == 0)
9951 return ok;
9952
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)
9956 {
9957 ok = FALSE;
9958 goto error_return;
9959 }
9960
9961 if (!xtensa_default_isa)
9962 xtensa_default_isa = xtensa_isa_init (0, 0);
9963
9964 for (i = 0; i < sec->reloc_count; i++)
9965 {
9966 Elf_Internal_Rela *irel = &internal_relocs[i];
9967 if (is_l32r_relocation (abfd, sec, contents, irel))
9968 {
9969 r_reloc l32r_rel;
9970 asection *target_sec;
9971 bfd_vma target_offset;
9972
9973 r_reloc_init (&l32r_rel, abfd, irel, contents, sec_size);
9974 target_sec = NULL;
9975 target_offset = 0;
9976 /* L32Rs must be local to the input file. */
9977 if (r_reloc_is_defined (&l32r_rel))
9978 {
9979 target_sec = r_reloc_get_section (&l32r_rel);
9980 target_offset = l32r_rel.target_offset;
9981 }
9982 (*callback) (sec, irel->r_offset, target_sec, target_offset,
9983 closure);
9984 }
9985 }
9986
9987 error_return:
9988 release_internal_relocs (sec, internal_relocs);
9989 release_contents (sec, contents);
9990 return ok;
9991 }
9992
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[]=
9997 {
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 }
10002 };
10003
10004 \f
10005 #ifndef ELF_ARCH
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
10011
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
10020
10021 #if XCHAL_HAVE_MMU
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 */
10027
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
10034
10035 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
10036
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
10043
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
10063
10064 #include "elf32-target.h"