daily update
[binutils.git] / bfd / elf32-xtensa.c
blob0755e09d5963fba5797da048a62ed39ca28d7051
1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright 2003, 2004 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
24 #ifdef ANSI_PROTOTYPES
25 #include <stdarg.h>
26 #else
27 #include <varargs.h>
28 #endif
29 #include <strings.h>
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"
38 /* Main interface functions. */
39 static void elf_xtensa_info_to_howto_rela
40 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
41 static reloc_howto_type *elf_xtensa_reloc_type_lookup
42 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
43 extern int xtensa_read_table_entries
44 PARAMS ((bfd *, asection *, property_table_entry **, const char *));
45 static bfd_boolean elf_xtensa_check_relocs
46 PARAMS ((bfd *, struct bfd_link_info *, asection *,
47 const Elf_Internal_Rela *));
48 static void elf_xtensa_hide_symbol
49 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
50 static asection *elf_xtensa_gc_mark_hook
51 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
52 struct elf_link_hash_entry *, Elf_Internal_Sym *));
53 static bfd_boolean elf_xtensa_gc_sweep_hook
54 PARAMS ((bfd *, struct bfd_link_info *, asection *,
55 const Elf_Internal_Rela *));
56 static bfd_boolean elf_xtensa_create_dynamic_sections
57 PARAMS ((bfd *, struct bfd_link_info *));
58 static bfd_boolean elf_xtensa_adjust_dynamic_symbol
59 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
60 static bfd_boolean elf_xtensa_size_dynamic_sections
61 PARAMS ((bfd *, struct bfd_link_info *));
62 static bfd_boolean elf_xtensa_modify_segment_map
63 PARAMS ((bfd *, struct bfd_link_info *));
64 static bfd_boolean elf_xtensa_relocate_section
65 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
66 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
67 static bfd_boolean elf_xtensa_relax_section
68 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *again));
69 static bfd_boolean elf_xtensa_finish_dynamic_symbol
70 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
71 Elf_Internal_Sym *));
72 static bfd_boolean elf_xtensa_finish_dynamic_sections
73 PARAMS ((bfd *, struct bfd_link_info *));
74 static bfd_boolean elf_xtensa_merge_private_bfd_data
75 PARAMS ((bfd *, bfd *));
76 static bfd_boolean elf_xtensa_set_private_flags
77 PARAMS ((bfd *, flagword));
78 extern flagword elf_xtensa_get_private_bfd_flags
79 PARAMS ((bfd *));
80 static bfd_boolean elf_xtensa_print_private_bfd_data
81 PARAMS ((bfd *, PTR));
82 static bfd_boolean elf_xtensa_object_p
83 PARAMS ((bfd *));
84 static void elf_xtensa_final_write_processing
85 PARAMS ((bfd *, bfd_boolean));
86 static enum elf_reloc_type_class elf_xtensa_reloc_type_class
87 PARAMS ((const Elf_Internal_Rela *));
88 static bfd_boolean elf_xtensa_discard_info
89 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *));
90 static bfd_boolean elf_xtensa_ignore_discarded_relocs
91 PARAMS ((asection *));
92 static bfd_boolean elf_xtensa_grok_prstatus
93 PARAMS ((bfd *, Elf_Internal_Note *));
94 static bfd_boolean elf_xtensa_grok_psinfo
95 PARAMS ((bfd *, Elf_Internal_Note *));
96 static bfd_boolean elf_xtensa_new_section_hook
97 PARAMS ((bfd *, asection *));
100 /* Local helper functions. */
102 static bfd_boolean xtensa_elf_dynamic_symbol_p
103 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
104 static int property_table_compare
105 PARAMS ((const PTR, const PTR));
106 static bfd_boolean elf_xtensa_in_literal_pool
107 PARAMS ((property_table_entry *, int, bfd_vma));
108 static void elf_xtensa_make_sym_local
109 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
110 static bfd_boolean add_extra_plt_sections
111 PARAMS ((bfd *, int));
112 static bfd_boolean elf_xtensa_fix_refcounts
113 PARAMS ((struct elf_link_hash_entry *, PTR));
114 static bfd_boolean elf_xtensa_allocate_plt_size
115 PARAMS ((struct elf_link_hash_entry *, PTR));
116 static bfd_boolean elf_xtensa_allocate_got_size
117 PARAMS ((struct elf_link_hash_entry *, PTR));
118 static void elf_xtensa_allocate_local_got_size
119 PARAMS ((struct bfd_link_info *, asection *));
120 static bfd_reloc_status_type elf_xtensa_do_reloc
121 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_vma, bfd_byte *,
122 bfd_vma, bfd_boolean, char **));
123 static char * vsprint_msg
124 VPARAMS ((const char *, const char *, int, ...));
125 static char *build_encoding_error_message
126 PARAMS ((xtensa_opcode, xtensa_encode_result));
127 static bfd_reloc_status_type bfd_elf_xtensa_reloc
128 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
129 static void do_fix_for_relocatable_link
130 PARAMS ((Elf_Internal_Rela *, bfd *, asection *));
131 static void do_fix_for_final_link
132 PARAMS ((Elf_Internal_Rela *, asection *, bfd_vma *));
133 static bfd_vma elf_xtensa_create_plt_entry
134 PARAMS ((bfd *, bfd *, unsigned));
135 static int elf_xtensa_combine_prop_entries
136 PARAMS ((bfd *, asection *, asection *));
137 static bfd_boolean elf_xtensa_discard_info_for_section
138 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *,
139 asection *));
141 /* Local functions to handle Xtensa configurability. */
143 static void init_call_opcodes
144 PARAMS ((void));
145 static bfd_boolean is_indirect_call_opcode
146 PARAMS ((xtensa_opcode));
147 static bfd_boolean is_direct_call_opcode
148 PARAMS ((xtensa_opcode));
149 static bfd_boolean is_windowed_call_opcode
150 PARAMS ((xtensa_opcode));
151 static xtensa_opcode get_l32r_opcode
152 PARAMS ((void));
153 static bfd_vma l32r_offset
154 PARAMS ((bfd_vma, bfd_vma));
155 static int get_relocation_opnd
156 PARAMS ((Elf_Internal_Rela *));
157 static xtensa_opcode get_relocation_opcode
158 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *));
159 static bfd_boolean is_l32r_relocation
160 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *));
162 /* Functions for link-time code simplifications. */
164 static bfd_reloc_status_type elf_xtensa_do_asm_simplify
165 PARAMS ((bfd_byte *, bfd_vma, bfd_vma));
166 static bfd_reloc_status_type contract_asm_expansion
167 PARAMS ((bfd_byte *, bfd_vma, Elf_Internal_Rela *));
168 static xtensa_opcode swap_callx_for_call_opcode
169 PARAMS ((xtensa_opcode));
170 static xtensa_opcode get_expanded_call_opcode
171 PARAMS ((bfd_byte *, int));
173 /* Access to internal relocations, section contents and symbols. */
175 static Elf_Internal_Rela *retrieve_internal_relocs
176 PARAMS ((bfd *, asection *, bfd_boolean));
177 static void pin_internal_relocs
178 PARAMS ((asection *, Elf_Internal_Rela *));
179 static void release_internal_relocs
180 PARAMS ((asection *, Elf_Internal_Rela *));
181 static bfd_byte *retrieve_contents
182 PARAMS ((bfd *, asection *, bfd_boolean));
183 static void pin_contents
184 PARAMS ((asection *, bfd_byte *));
185 static void release_contents
186 PARAMS ((asection *, bfd_byte *));
187 static Elf_Internal_Sym *retrieve_local_syms
188 PARAMS ((bfd *));
190 /* Miscellaneous utility functions. */
192 static asection *elf_xtensa_get_plt_section
193 PARAMS ((bfd *, int));
194 static asection *elf_xtensa_get_gotplt_section
195 PARAMS ((bfd *, int));
196 static asection *get_elf_r_symndx_section
197 PARAMS ((bfd *, unsigned long));
198 static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
199 PARAMS ((bfd *, unsigned long));
200 static bfd_vma get_elf_r_symndx_offset
201 PARAMS ((bfd *, unsigned long));
202 static bfd_boolean pcrel_reloc_fits
203 PARAMS ((xtensa_operand, bfd_vma, bfd_vma));
204 static bfd_boolean xtensa_is_property_section
205 PARAMS ((asection *));
206 static bfd_boolean xtensa_is_littable_section
207 PARAMS ((asection *));
208 static bfd_boolean is_literal_section
209 PARAMS ((asection *));
210 static int internal_reloc_compare
211 PARAMS ((const PTR, const PTR));
212 extern char *xtensa_get_property_section_name
213 PARAMS ((asection *, const char *));
215 /* Other functions called directly by the linker. */
217 typedef void (*deps_callback_t)
218 PARAMS ((asection *, bfd_vma, asection *, bfd_vma, PTR));
219 extern bfd_boolean xtensa_callback_required_dependence
220 PARAMS ((bfd *, asection *, struct bfd_link_info *,
221 deps_callback_t, PTR));
224 typedef struct xtensa_relax_info_struct xtensa_relax_info;
227 /* Total count of PLT relocations seen during check_relocs.
228 The actual PLT code must be split into multiple sections and all
229 the sections have to be created before size_dynamic_sections,
230 where we figure out the exact number of PLT entries that will be
231 needed. It is OK if this count is an overestimate, e.g., some
232 relocations may be removed by GC. */
234 static int plt_reloc_count = 0;
237 /* When this is true, relocations may have been modified to refer to
238 symbols from other input files. The per-section list of "fix"
239 records needs to be checked when resolving relocations. */
241 static bfd_boolean relaxing_section = FALSE;
244 static reloc_howto_type elf_howto_table[] =
246 HOWTO (R_XTENSA_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
247 bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
248 FALSE, 0x00000000, 0x00000000, FALSE),
249 HOWTO (R_XTENSA_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
250 bfd_elf_xtensa_reloc, "R_XTENSA_32",
251 TRUE, 0xffffffff, 0xffffffff, FALSE),
252 /* Replace a 32-bit value with a value from the runtime linker (only
253 used by linker-generated stub functions). The r_addend value is
254 special: 1 means to substitute a pointer to the runtime linker's
255 dynamic resolver function; 2 means to substitute the link map for
256 the shared object. */
257 HOWTO (R_XTENSA_RTLD, 0, 2, 32, FALSE, 0, complain_overflow_dont,
258 NULL, "R_XTENSA_RTLD",
259 FALSE, 0x00000000, 0x00000000, FALSE),
260 HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
261 bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
262 FALSE, 0xffffffff, 0xffffffff, FALSE),
263 HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
264 bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
265 FALSE, 0xffffffff, 0xffffffff, FALSE),
266 HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
267 bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
268 FALSE, 0xffffffff, 0xffffffff, FALSE),
269 HOWTO (R_XTENSA_PLT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
270 bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
271 FALSE, 0xffffffff, 0xffffffff, FALSE),
272 EMPTY_HOWTO (7),
273 HOWTO (R_XTENSA_OP0, 0, 0, 0, TRUE, 0, complain_overflow_dont,
274 bfd_elf_xtensa_reloc, "R_XTENSA_OP0",
275 FALSE, 0x00000000, 0x00000000, TRUE),
276 HOWTO (R_XTENSA_OP1, 0, 0, 0, TRUE, 0, complain_overflow_dont,
277 bfd_elf_xtensa_reloc, "R_XTENSA_OP1",
278 FALSE, 0x00000000, 0x00000000, TRUE),
279 HOWTO (R_XTENSA_OP2, 0, 0, 0, TRUE, 0, complain_overflow_dont,
280 bfd_elf_xtensa_reloc, "R_XTENSA_OP2",
281 FALSE, 0x00000000, 0x00000000, TRUE),
282 /* Assembly auto-expansion. */
283 HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, TRUE, 0, complain_overflow_dont,
284 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND",
285 FALSE, 0x00000000, 0x00000000, FALSE),
286 /* Relax assembly auto-expansion. */
287 HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, TRUE, 0, complain_overflow_dont,
288 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY",
289 FALSE, 0x00000000, 0x00000000, TRUE),
290 EMPTY_HOWTO (13),
291 EMPTY_HOWTO (14),
292 /* GNU extension to record C++ vtable hierarchy. */
293 HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, FALSE, 0, complain_overflow_dont,
294 NULL, "R_XTENSA_GNU_VTINHERIT",
295 FALSE, 0x00000000, 0x00000000, FALSE),
296 /* GNU extension to record C++ vtable member usage. */
297 HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, FALSE, 0, complain_overflow_dont,
298 _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
299 FALSE, 0x00000000, 0x00000000, FALSE)
302 #ifdef DEBUG_GEN_RELOC
303 #define TRACE(str) \
304 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
305 #else
306 #define TRACE(str)
307 #endif
309 static reloc_howto_type *
310 elf_xtensa_reloc_type_lookup (abfd, code)
311 bfd *abfd ATTRIBUTE_UNUSED;
312 bfd_reloc_code_real_type code;
314 switch (code)
316 case BFD_RELOC_NONE:
317 TRACE ("BFD_RELOC_NONE");
318 return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
320 case BFD_RELOC_32:
321 TRACE ("BFD_RELOC_32");
322 return &elf_howto_table[(unsigned) R_XTENSA_32 ];
324 case BFD_RELOC_XTENSA_RTLD:
325 TRACE ("BFD_RELOC_XTENSA_RTLD");
326 return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
328 case BFD_RELOC_XTENSA_GLOB_DAT:
329 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
330 return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
332 case BFD_RELOC_XTENSA_JMP_SLOT:
333 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
334 return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
336 case BFD_RELOC_XTENSA_RELATIVE:
337 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
338 return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
340 case BFD_RELOC_XTENSA_PLT:
341 TRACE ("BFD_RELOC_XTENSA_PLT");
342 return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
344 case BFD_RELOC_XTENSA_OP0:
345 TRACE ("BFD_RELOC_XTENSA_OP0");
346 return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
348 case BFD_RELOC_XTENSA_OP1:
349 TRACE ("BFD_RELOC_XTENSA_OP1");
350 return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
352 case BFD_RELOC_XTENSA_OP2:
353 TRACE ("BFD_RELOC_XTENSA_OP2");
354 return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
356 case BFD_RELOC_XTENSA_ASM_EXPAND:
357 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
358 return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
360 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
361 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
362 return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
364 case BFD_RELOC_VTABLE_INHERIT:
365 TRACE ("BFD_RELOC_VTABLE_INHERIT");
366 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
368 case BFD_RELOC_VTABLE_ENTRY:
369 TRACE ("BFD_RELOC_VTABLE_ENTRY");
370 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
372 default:
373 break;
376 TRACE ("Unknown");
377 return NULL;
381 /* Given an ELF "rela" relocation, find the corresponding howto and record
382 it in the BFD internal arelent representation of the relocation. */
384 static void
385 elf_xtensa_info_to_howto_rela (abfd, cache_ptr, dst)
386 bfd *abfd ATTRIBUTE_UNUSED;
387 arelent *cache_ptr;
388 Elf_Internal_Rela *dst;
390 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
392 BFD_ASSERT (r_type < (unsigned int) R_XTENSA_max);
393 cache_ptr->howto = &elf_howto_table[r_type];
397 /* Functions for the Xtensa ELF linker. */
399 /* The name of the dynamic interpreter. This is put in the .interp
400 section. */
402 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
404 /* The size in bytes of an entry in the procedure linkage table.
405 (This does _not_ include the space for the literals associated with
406 the PLT entry.) */
408 #define PLT_ENTRY_SIZE 16
410 /* For _really_ large PLTs, we may need to alternate between literals
411 and code to keep the literals within the 256K range of the L32R
412 instructions in the code. It's unlikely that anyone would ever need
413 such a big PLT, but an arbitrary limit on the PLT size would be bad.
414 Thus, we split the PLT into chunks. Since there's very little
415 overhead (2 extra literals) for each chunk, the chunk size is kept
416 small so that the code for handling multiple chunks get used and
417 tested regularly. With 254 entries, there are 1K of literals for
418 each chunk, and that seems like a nice round number. */
420 #define PLT_ENTRIES_PER_CHUNK 254
422 /* PLT entries are actually used as stub functions for lazy symbol
423 resolution. Once the symbol is resolved, the stub function is never
424 invoked. Note: the 32-byte frame size used here cannot be changed
425 without a corresponding change in the runtime linker. */
427 static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
429 0x6c, 0x10, 0x04, /* entry sp, 32 */
430 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
431 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
432 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
433 0x0a, 0x80, 0x00, /* jx a8 */
434 0 /* unused */
437 static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
439 0x36, 0x41, 0x00, /* entry sp, 32 */
440 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
441 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
442 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
443 0xa0, 0x08, 0x00, /* jx a8 */
444 0 /* unused */
448 static inline bfd_boolean
449 xtensa_elf_dynamic_symbol_p (h, info)
450 struct elf_link_hash_entry *h;
451 struct bfd_link_info *info;
453 /* Check if we should do dynamic things to this symbol. The
454 "ignore_protected" argument need not be set, because Xtensa code
455 does not require special handling of STV_PROTECTED to make function
456 pointer comparisons work properly. The PLT addresses are never
457 used for function pointers. */
459 return _bfd_elf_dynamic_symbol_p (h, info, 0);
463 static int
464 property_table_compare (ap, bp)
465 const PTR ap;
466 const PTR bp;
468 const property_table_entry *a = (const property_table_entry *) ap;
469 const property_table_entry *b = (const property_table_entry *) bp;
471 /* Check if one entry overlaps with the other; this shouldn't happen
472 except when searching for a match. */
473 if ((b->address >= a->address && b->address < (a->address + a->size))
474 || (a->address >= b->address && a->address < (b->address + b->size)))
475 return 0;
477 return (a->address - b->address);
481 /* Get the literal table or instruction table entries for the given
482 section. Sets TABLE_P and returns the number of entries. On error,
483 returns a negative value. */
486 xtensa_read_table_entries (abfd, section, table_p, sec_name)
487 bfd *abfd;
488 asection *section;
489 property_table_entry **table_p;
490 const char *sec_name;
492 asection *table_section;
493 char *table_section_name;
494 bfd_size_type table_size = 0;
495 bfd_byte *table_data;
496 property_table_entry *blocks;
497 int block_count;
498 bfd_size_type num_records;
499 Elf_Internal_Rela *internal_relocs;
500 bfd_vma section_addr;
502 table_section_name =
503 xtensa_get_property_section_name (section, sec_name);
504 table_section = bfd_get_section_by_name (abfd, table_section_name);
505 free (table_section_name);
506 if (table_section != NULL)
507 table_size = table_section->size;
509 if (table_size == 0)
511 *table_p = NULL;
512 return 0;
515 num_records = table_size / 8;
516 table_data = retrieve_contents (abfd, table_section, TRUE);
517 blocks = (property_table_entry *)
518 bfd_malloc (num_records * sizeof (property_table_entry));
519 block_count = 0;
521 section_addr = section->output_section->vma + section->output_offset;
523 /* If the file has not yet been relocated, process the relocations
524 and sort out the table entries that apply to the specified section. */
525 internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
526 if (internal_relocs && !table_section->reloc_done)
528 unsigned i;
530 for (i = 0; i < table_section->reloc_count; i++)
532 Elf_Internal_Rela *rel = &internal_relocs[i];
533 unsigned long r_symndx;
535 if (ELF32_R_TYPE (rel->r_info) == R_XTENSA_NONE)
536 continue;
538 BFD_ASSERT (ELF32_R_TYPE (rel->r_info) == R_XTENSA_32);
539 r_symndx = ELF32_R_SYM (rel->r_info);
541 if (get_elf_r_symndx_section (abfd, r_symndx) == section)
543 bfd_vma sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
544 blocks[block_count].address =
545 (section_addr + sym_off + rel->r_addend
546 + bfd_get_32 (abfd, table_data + rel->r_offset));
547 blocks[block_count].size =
548 bfd_get_32 (abfd, table_data + rel->r_offset + 4);
549 block_count++;
553 else
555 /* The file has already been relocated and the addresses are
556 already in the table. */
557 bfd_vma off;
559 for (off = 0; off < table_size; off += 8)
561 bfd_vma address = bfd_get_32 (abfd, table_data + off);
563 if (address >= section_addr
564 && address < section_addr + section->size)
566 blocks[block_count].address = address;
567 blocks[block_count].size =
568 bfd_get_32 (abfd, table_data + off + 4);
569 block_count++;
574 release_contents (table_section, table_data);
575 release_internal_relocs (table_section, internal_relocs);
577 if (block_count > 0)
579 /* Now sort them into address order for easy reference. */
580 qsort (blocks, block_count, sizeof (property_table_entry),
581 property_table_compare);
584 *table_p = blocks;
585 return block_count;
589 static bfd_boolean
590 elf_xtensa_in_literal_pool (lit_table, lit_table_size, addr)
591 property_table_entry *lit_table;
592 int lit_table_size;
593 bfd_vma addr;
595 property_table_entry entry;
597 if (lit_table_size == 0)
598 return FALSE;
600 entry.address = addr;
601 entry.size = 1;
603 if (bsearch (&entry, lit_table, lit_table_size,
604 sizeof (property_table_entry), property_table_compare))
605 return TRUE;
607 return FALSE;
611 /* Look through the relocs for a section during the first phase, and
612 calculate needed space in the dynamic reloc sections. */
614 static bfd_boolean
615 elf_xtensa_check_relocs (abfd, info, sec, relocs)
616 bfd *abfd;
617 struct bfd_link_info *info;
618 asection *sec;
619 const Elf_Internal_Rela *relocs;
621 Elf_Internal_Shdr *symtab_hdr;
622 struct elf_link_hash_entry **sym_hashes;
623 const Elf_Internal_Rela *rel;
624 const Elf_Internal_Rela *rel_end;
626 if (info->relocatable)
627 return TRUE;
629 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
630 sym_hashes = elf_sym_hashes (abfd);
632 rel_end = relocs + sec->reloc_count;
633 for (rel = relocs; rel < rel_end; rel++)
635 unsigned int r_type;
636 unsigned long r_symndx;
637 struct elf_link_hash_entry *h;
639 r_symndx = ELF32_R_SYM (rel->r_info);
640 r_type = ELF32_R_TYPE (rel->r_info);
642 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
644 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
645 abfd, r_symndx);
646 return FALSE;
649 if (r_symndx < symtab_hdr->sh_info)
650 h = NULL;
651 else
653 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
654 while (h->root.type == bfd_link_hash_indirect
655 || h->root.type == bfd_link_hash_warning)
656 h = (struct elf_link_hash_entry *) h->root.u.i.link;
659 switch (r_type)
661 case R_XTENSA_32:
662 if (h == NULL)
663 goto local_literal;
665 if ((sec->flags & SEC_ALLOC) != 0)
667 if (h->got.refcount <= 0)
668 h->got.refcount = 1;
669 else
670 h->got.refcount += 1;
672 break;
674 case R_XTENSA_PLT:
675 /* If this relocation is against a local symbol, then it's
676 exactly the same as a normal local GOT entry. */
677 if (h == NULL)
678 goto local_literal;
680 if ((sec->flags & SEC_ALLOC) != 0)
682 if (h->plt.refcount <= 0)
684 h->needs_plt = 1;
685 h->plt.refcount = 1;
687 else
688 h->plt.refcount += 1;
690 /* Keep track of the total PLT relocation count even if we
691 don't yet know whether the dynamic sections will be
692 created. */
693 plt_reloc_count += 1;
695 if (elf_hash_table (info)->dynamic_sections_created)
697 if (!add_extra_plt_sections (elf_hash_table (info)->dynobj,
698 plt_reloc_count))
699 return FALSE;
702 break;
704 local_literal:
705 if ((sec->flags & SEC_ALLOC) != 0)
707 bfd_signed_vma *local_got_refcounts;
709 /* This is a global offset table entry for a local symbol. */
710 local_got_refcounts = elf_local_got_refcounts (abfd);
711 if (local_got_refcounts == NULL)
713 bfd_size_type size;
715 size = symtab_hdr->sh_info;
716 size *= sizeof (bfd_signed_vma);
717 local_got_refcounts = ((bfd_signed_vma *)
718 bfd_zalloc (abfd, size));
719 if (local_got_refcounts == NULL)
720 return FALSE;
721 elf_local_got_refcounts (abfd) = local_got_refcounts;
723 local_got_refcounts[r_symndx] += 1;
725 break;
727 case R_XTENSA_OP0:
728 case R_XTENSA_OP1:
729 case R_XTENSA_OP2:
730 case R_XTENSA_ASM_EXPAND:
731 case R_XTENSA_ASM_SIMPLIFY:
732 /* Nothing to do for these. */
733 break;
735 case R_XTENSA_GNU_VTINHERIT:
736 /* This relocation describes the C++ object vtable hierarchy.
737 Reconstruct it for later use during GC. */
738 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
739 return FALSE;
740 break;
742 case R_XTENSA_GNU_VTENTRY:
743 /* This relocation describes which C++ vtable entries are actually
744 used. Record for later use during GC. */
745 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
746 return FALSE;
747 break;
749 default:
750 break;
754 return TRUE;
758 static void
759 elf_xtensa_hide_symbol (info, h, force_local)
760 struct bfd_link_info *info;
761 struct elf_link_hash_entry *h;
762 bfd_boolean force_local;
764 /* For a shared link, move the plt refcount to the got refcount to leave
765 space for RELATIVE relocs. */
766 elf_xtensa_make_sym_local (info, h);
768 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
772 /* Return the section that should be marked against GC for a given
773 relocation. */
775 static asection *
776 elf_xtensa_gc_mark_hook (sec, info, rel, h, sym)
777 asection *sec;
778 struct bfd_link_info *info ATTRIBUTE_UNUSED;
779 Elf_Internal_Rela *rel;
780 struct elf_link_hash_entry *h;
781 Elf_Internal_Sym *sym;
783 if (h != NULL)
785 switch (ELF32_R_TYPE (rel->r_info))
787 case R_XTENSA_GNU_VTINHERIT:
788 case R_XTENSA_GNU_VTENTRY:
789 break;
791 default:
792 switch (h->root.type)
794 case bfd_link_hash_defined:
795 case bfd_link_hash_defweak:
796 return h->root.u.def.section;
798 case bfd_link_hash_common:
799 return h->root.u.c.p->section;
801 default:
802 break;
806 else
807 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
809 return NULL;
812 /* Update the GOT & PLT entry reference counts
813 for the section being removed. */
815 static bfd_boolean
816 elf_xtensa_gc_sweep_hook (abfd, info, sec, relocs)
817 bfd *abfd;
818 struct bfd_link_info *info ATTRIBUTE_UNUSED;
819 asection *sec;
820 const Elf_Internal_Rela *relocs;
822 Elf_Internal_Shdr *symtab_hdr;
823 struct elf_link_hash_entry **sym_hashes;
824 bfd_signed_vma *local_got_refcounts;
825 const Elf_Internal_Rela *rel, *relend;
827 if ((sec->flags & SEC_ALLOC) == 0)
828 return TRUE;
830 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
831 sym_hashes = elf_sym_hashes (abfd);
832 local_got_refcounts = elf_local_got_refcounts (abfd);
834 relend = relocs + sec->reloc_count;
835 for (rel = relocs; rel < relend; rel++)
837 unsigned long r_symndx;
838 unsigned int r_type;
839 struct elf_link_hash_entry *h = NULL;
841 r_symndx = ELF32_R_SYM (rel->r_info);
842 if (r_symndx >= symtab_hdr->sh_info)
843 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
845 r_type = ELF32_R_TYPE (rel->r_info);
846 switch (r_type)
848 case R_XTENSA_32:
849 if (h == NULL)
850 goto local_literal;
851 if (h->got.refcount > 0)
852 h->got.refcount--;
853 break;
855 case R_XTENSA_PLT:
856 if (h == NULL)
857 goto local_literal;
858 if (h->plt.refcount > 0)
859 h->plt.refcount--;
860 break;
862 local_literal:
863 if (local_got_refcounts[r_symndx] > 0)
864 local_got_refcounts[r_symndx] -= 1;
865 break;
867 default:
868 break;
872 return TRUE;
876 /* Create all the dynamic sections. */
878 static bfd_boolean
879 elf_xtensa_create_dynamic_sections (dynobj, info)
880 bfd *dynobj;
881 struct bfd_link_info *info;
883 flagword flags, noalloc_flags;
884 asection *s;
886 /* First do all the standard stuff. */
887 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
888 return FALSE;
890 /* Create any extra PLT sections in case check_relocs has already
891 been called on all the non-dynamic input files. */
892 if (!add_extra_plt_sections (dynobj, plt_reloc_count))
893 return FALSE;
895 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
896 | SEC_LINKER_CREATED | SEC_READONLY);
897 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
899 /* Mark the ".got.plt" section READONLY. */
900 s = bfd_get_section_by_name (dynobj, ".got.plt");
901 if (s == NULL
902 || ! bfd_set_section_flags (dynobj, s, flags))
903 return FALSE;
905 /* Create ".rela.got". */
906 s = bfd_make_section (dynobj, ".rela.got");
907 if (s == NULL
908 || ! bfd_set_section_flags (dynobj, s, flags)
909 || ! bfd_set_section_alignment (dynobj, s, 2))
910 return FALSE;
912 /* Create ".got.loc" (literal tables for use by dynamic linker). */
913 s = bfd_make_section (dynobj, ".got.loc");
914 if (s == NULL
915 || ! bfd_set_section_flags (dynobj, s, flags)
916 || ! bfd_set_section_alignment (dynobj, s, 2))
917 return FALSE;
919 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
920 s = bfd_make_section (dynobj, ".xt.lit.plt");
921 if (s == NULL
922 || ! bfd_set_section_flags (dynobj, s, noalloc_flags)
923 || ! bfd_set_section_alignment (dynobj, s, 2))
924 return FALSE;
926 return TRUE;
930 static bfd_boolean
931 add_extra_plt_sections (dynobj, count)
932 bfd *dynobj;
933 int count;
935 int chunk;
937 /* Iterate over all chunks except 0 which uses the standard ".plt" and
938 ".got.plt" sections. */
939 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
941 char *sname;
942 flagword flags;
943 asection *s;
945 /* Stop when we find a section has already been created. */
946 if (elf_xtensa_get_plt_section (dynobj, chunk))
947 break;
949 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
950 | SEC_LINKER_CREATED | SEC_READONLY);
952 sname = (char *) bfd_malloc (10);
953 sprintf (sname, ".plt.%u", chunk);
954 s = bfd_make_section (dynobj, sname);
955 if (s == NULL
956 || ! bfd_set_section_flags (dynobj, s, flags | SEC_CODE)
957 || ! bfd_set_section_alignment (dynobj, s, 2))
958 return FALSE;
960 sname = (char *) bfd_malloc (14);
961 sprintf (sname, ".got.plt.%u", chunk);
962 s = bfd_make_section (dynobj, sname);
963 if (s == NULL
964 || ! bfd_set_section_flags (dynobj, s, flags)
965 || ! bfd_set_section_alignment (dynobj, s, 2))
966 return FALSE;
969 return TRUE;
973 /* Adjust a symbol defined by a dynamic object and referenced by a
974 regular object. The current definition is in some section of the
975 dynamic object, but we're not including those sections. We have to
976 change the definition to something the rest of the link can
977 understand. */
979 static bfd_boolean
980 elf_xtensa_adjust_dynamic_symbol (info, h)
981 struct bfd_link_info *info ATTRIBUTE_UNUSED;
982 struct elf_link_hash_entry *h;
984 /* If this is a weak symbol, and there is a real definition, the
985 processor independent code will have arranged for us to see the
986 real definition first, and we can just use the same value. */
987 if (h->u.weakdef != NULL)
989 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
990 || h->u.weakdef->root.type == bfd_link_hash_defweak);
991 h->root.u.def.section = h->u.weakdef->root.u.def.section;
992 h->root.u.def.value = h->u.weakdef->root.u.def.value;
993 return TRUE;
996 /* This is a reference to a symbol defined by a dynamic object. The
997 reference must go through the GOT, so there's no need for COPY relocs,
998 .dynbss, etc. */
1000 return TRUE;
1004 static void
1005 elf_xtensa_make_sym_local (info, h)
1006 struct bfd_link_info *info;
1007 struct elf_link_hash_entry *h;
1009 if (info->shared)
1011 if (h->plt.refcount > 0)
1013 /* Will use RELATIVE relocs instead of JMP_SLOT relocs. */
1014 if (h->got.refcount < 0)
1015 h->got.refcount = 0;
1016 h->got.refcount += h->plt.refcount;
1017 h->plt.refcount = 0;
1020 else
1022 /* Don't need any dynamic relocations at all. */
1023 h->plt.refcount = 0;
1024 h->got.refcount = 0;
1029 static bfd_boolean
1030 elf_xtensa_fix_refcounts (h, arg)
1031 struct elf_link_hash_entry *h;
1032 PTR arg;
1034 struct bfd_link_info *info = (struct bfd_link_info *) arg;
1036 if (h->root.type == bfd_link_hash_warning)
1037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1039 if (! xtensa_elf_dynamic_symbol_p (h, info))
1040 elf_xtensa_make_sym_local (info, h);
1042 return TRUE;
1046 static bfd_boolean
1047 elf_xtensa_allocate_plt_size (h, arg)
1048 struct elf_link_hash_entry *h;
1049 PTR arg;
1051 asection *srelplt = (asection *) arg;
1053 if (h->root.type == bfd_link_hash_warning)
1054 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1056 if (h->plt.refcount > 0)
1057 srelplt->size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1059 return TRUE;
1063 static bfd_boolean
1064 elf_xtensa_allocate_got_size (h, arg)
1065 struct elf_link_hash_entry *h;
1066 PTR arg;
1068 asection *srelgot = (asection *) arg;
1070 if (h->root.type == bfd_link_hash_warning)
1071 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1073 if (h->got.refcount > 0)
1074 srelgot->size += (h->got.refcount * sizeof (Elf32_External_Rela));
1076 return TRUE;
1080 static void
1081 elf_xtensa_allocate_local_got_size (info, srelgot)
1082 struct bfd_link_info *info;
1083 asection *srelgot;
1085 bfd *i;
1087 for (i = info->input_bfds; i; i = i->link_next)
1089 bfd_signed_vma *local_got_refcounts;
1090 bfd_size_type j, cnt;
1091 Elf_Internal_Shdr *symtab_hdr;
1093 local_got_refcounts = elf_local_got_refcounts (i);
1094 if (!local_got_refcounts)
1095 continue;
1097 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1098 cnt = symtab_hdr->sh_info;
1100 for (j = 0; j < cnt; ++j)
1102 if (local_got_refcounts[j] > 0)
1103 srelgot->size += (local_got_refcounts[j]
1104 * sizeof (Elf32_External_Rela));
1110 /* Set the sizes of the dynamic sections. */
1112 static bfd_boolean
1113 elf_xtensa_size_dynamic_sections (output_bfd, info)
1114 bfd *output_bfd ATTRIBUTE_UNUSED;
1115 struct bfd_link_info *info;
1117 bfd *dynobj, *abfd;
1118 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1119 bfd_boolean relplt, relgot;
1120 int plt_entries, plt_chunks, chunk;
1122 plt_entries = 0;
1123 plt_chunks = 0;
1124 srelgot = 0;
1126 dynobj = elf_hash_table (info)->dynobj;
1127 if (dynobj == NULL)
1128 abort ();
1130 if (elf_hash_table (info)->dynamic_sections_created)
1132 /* Set the contents of the .interp section to the interpreter. */
1133 if (info->executable)
1135 s = bfd_get_section_by_name (dynobj, ".interp");
1136 if (s == NULL)
1137 abort ();
1138 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1139 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1142 /* Allocate room for one word in ".got". */
1143 s = bfd_get_section_by_name (dynobj, ".got");
1144 if (s == NULL)
1145 abort ();
1146 s->size = 4;
1148 /* Adjust refcounts for symbols that we now know are not "dynamic". */
1149 elf_link_hash_traverse (elf_hash_table (info),
1150 elf_xtensa_fix_refcounts,
1151 (PTR) info);
1153 /* Allocate space in ".rela.got" for literals that reference
1154 global symbols. */
1155 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1156 if (srelgot == NULL)
1157 abort ();
1158 elf_link_hash_traverse (elf_hash_table (info),
1159 elf_xtensa_allocate_got_size,
1160 (PTR) srelgot);
1162 /* If we are generating a shared object, we also need space in
1163 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1164 reference local symbols. */
1165 if (info->shared)
1166 elf_xtensa_allocate_local_got_size (info, srelgot);
1168 /* Allocate space in ".rela.plt" for literals that have PLT entries. */
1169 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1170 if (srelplt == NULL)
1171 abort ();
1172 elf_link_hash_traverse (elf_hash_table (info),
1173 elf_xtensa_allocate_plt_size,
1174 (PTR) srelplt);
1176 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1177 each PLT entry, we need the PLT code plus a 4-byte literal.
1178 For each chunk of ".plt", we also need two more 4-byte
1179 literals, two corresponding entries in ".rela.got", and an
1180 8-byte entry in ".xt.lit.plt". */
1181 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
1182 if (spltlittbl == NULL)
1183 abort ();
1185 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
1186 plt_chunks =
1187 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1189 /* Iterate over all the PLT chunks, including any extra sections
1190 created earlier because the initial count of PLT relocations
1191 was an overestimate. */
1192 for (chunk = 0;
1193 (splt = elf_xtensa_get_plt_section (dynobj, chunk)) != NULL;
1194 chunk++)
1196 int chunk_entries;
1198 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1199 if (sgotplt == NULL)
1200 abort ();
1202 if (chunk < plt_chunks - 1)
1203 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1204 else if (chunk == plt_chunks - 1)
1205 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1206 else
1207 chunk_entries = 0;
1209 if (chunk_entries != 0)
1211 sgotplt->size = 4 * (chunk_entries + 2);
1212 splt->size = PLT_ENTRY_SIZE * chunk_entries;
1213 srelgot->size += 2 * sizeof (Elf32_External_Rela);
1214 spltlittbl->size += 8;
1216 else
1218 sgotplt->size = 0;
1219 splt->size = 0;
1223 /* Allocate space in ".got.loc" to match the total size of all the
1224 literal tables. */
1225 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
1226 if (sgotloc == NULL)
1227 abort ();
1228 sgotloc->size = spltlittbl->size;
1229 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1231 if (abfd->flags & DYNAMIC)
1232 continue;
1233 for (s = abfd->sections; s != NULL; s = s->next)
1235 if (! elf_discarded_section (s)
1236 && xtensa_is_littable_section (s)
1237 && s != spltlittbl)
1238 sgotloc->size += s->size;
1243 /* Allocate memory for dynamic sections. */
1244 relplt = FALSE;
1245 relgot = FALSE;
1246 for (s = dynobj->sections; s != NULL; s = s->next)
1248 const char *name;
1249 bfd_boolean strip;
1251 if ((s->flags & SEC_LINKER_CREATED) == 0)
1252 continue;
1254 /* It's OK to base decisions on the section name, because none
1255 of the dynobj section names depend upon the input files. */
1256 name = bfd_get_section_name (dynobj, s);
1258 strip = FALSE;
1260 if (strncmp (name, ".rela", 5) == 0)
1262 if (strcmp (name, ".rela.plt") == 0)
1263 relplt = TRUE;
1264 else if (strcmp (name, ".rela.got") == 0)
1265 relgot = TRUE;
1267 /* We use the reloc_count field as a counter if we need
1268 to copy relocs into the output file. */
1269 s->reloc_count = 0;
1271 else if (strncmp (name, ".plt.", 5) == 0
1272 || strncmp (name, ".got.plt.", 9) == 0)
1274 if (s->size == 0)
1276 /* If we don't need this section, strip it from the output
1277 file. We must create the ".plt*" and ".got.plt*"
1278 sections in create_dynamic_sections and/or check_relocs
1279 based on a conservative estimate of the PLT relocation
1280 count, because the sections must be created before the
1281 linker maps input sections to output sections. The
1282 linker does that before size_dynamic_sections, where we
1283 compute the exact size of the PLT, so there may be more
1284 of these sections than are actually needed. */
1285 strip = TRUE;
1288 else if (strcmp (name, ".got") != 0
1289 && strcmp (name, ".plt") != 0
1290 && strcmp (name, ".got.plt") != 0
1291 && strcmp (name, ".xt.lit.plt") != 0
1292 && strcmp (name, ".got.loc") != 0)
1294 /* It's not one of our sections, so don't allocate space. */
1295 continue;
1298 if (strip)
1299 _bfd_strip_section_from_output (info, s);
1300 else
1302 /* Allocate memory for the section contents. */
1303 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1304 if (s->contents == NULL && s->size != 0)
1305 return FALSE;
1309 if (elf_hash_table (info)->dynamic_sections_created)
1311 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1312 known until finish_dynamic_sections, but we need to get the relocs
1313 in place before they are sorted. */
1314 if (srelgot == NULL)
1315 abort ();
1316 for (chunk = 0; chunk < plt_chunks; chunk++)
1318 Elf_Internal_Rela irela;
1319 bfd_byte *loc;
1321 irela.r_offset = 0;
1322 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1323 irela.r_addend = 0;
1325 loc = (srelgot->contents
1326 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1327 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1328 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1329 loc + sizeof (Elf32_External_Rela));
1330 srelgot->reloc_count += 2;
1333 /* Add some entries to the .dynamic section. We fill in the
1334 values later, in elf_xtensa_finish_dynamic_sections, but we
1335 must add the entries now so that we get the correct size for
1336 the .dynamic section. The DT_DEBUG entry is filled in by the
1337 dynamic linker and used by the debugger. */
1338 #define add_dynamic_entry(TAG, VAL) \
1339 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1341 if (! info->shared)
1343 if (!add_dynamic_entry (DT_DEBUG, 0))
1344 return FALSE;
1347 if (relplt)
1349 if (!add_dynamic_entry (DT_PLTGOT, 0)
1350 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1351 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1352 || !add_dynamic_entry (DT_JMPREL, 0))
1353 return FALSE;
1356 if (relgot)
1358 if (!add_dynamic_entry (DT_RELA, 0)
1359 || !add_dynamic_entry (DT_RELASZ, 0)
1360 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1361 return FALSE;
1364 if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1365 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1366 return FALSE;
1368 #undef add_dynamic_entry
1370 return TRUE;
1374 /* Remove any PT_LOAD segments with no allocated sections. Prior to
1375 binutils 2.13, this function used to remove the non-SEC_ALLOC
1376 sections from PT_LOAD segments, but that task has now been moved
1377 into elf.c. We still need this function to remove any empty
1378 segments that result, but there's nothing Xtensa-specific about
1379 this and it probably ought to be moved into elf.c as well. */
1381 static bfd_boolean
1382 elf_xtensa_modify_segment_map (abfd, info)
1383 bfd *abfd;
1384 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1386 struct elf_segment_map **m_p;
1388 m_p = &elf_tdata (abfd)->segment_map;
1389 while (*m_p != NULL)
1391 if ((*m_p)->p_type == PT_LOAD && (*m_p)->count == 0)
1392 *m_p = (*m_p)->next;
1393 else
1394 m_p = &(*m_p)->next;
1396 return TRUE;
1400 /* Perform the specified relocation. The instruction at (contents + address)
1401 is modified to set one operand to represent the value in "relocation". The
1402 operand position is determined by the relocation type recorded in the
1403 howto. */
1405 #define CALL_SEGMENT_BITS (30)
1406 #define CALL_SEGMENT_SIZE (1<<CALL_SEGMENT_BITS)
1408 static bfd_reloc_status_type
1409 elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1410 contents, address, is_weak_undef, error_message)
1411 reloc_howto_type *howto;
1412 bfd *abfd;
1413 asection *input_section;
1414 bfd_vma relocation;
1415 bfd_byte *contents;
1416 bfd_vma address;
1417 bfd_boolean is_weak_undef;
1418 char **error_message;
1420 xtensa_opcode opcode;
1421 xtensa_operand operand;
1422 xtensa_encode_result encode_result;
1423 xtensa_isa isa = xtensa_default_isa;
1424 xtensa_insnbuf ibuff;
1425 bfd_vma self_address;
1426 int opnd;
1427 uint32 newval;
1429 switch (howto->type)
1431 case R_XTENSA_NONE:
1432 return bfd_reloc_ok;
1434 case R_XTENSA_ASM_EXPAND:
1435 if (!is_weak_undef)
1437 /* Check for windowed CALL across a 1GB boundary. */
1438 xtensa_opcode opcode =
1439 get_expanded_call_opcode (contents + address,
1440 input_section->size - address);
1441 if (is_windowed_call_opcode (opcode))
1443 self_address = (input_section->output_section->vma
1444 + input_section->output_offset
1445 + address);
1446 if ((self_address >> CALL_SEGMENT_BITS) !=
1447 (relocation >> CALL_SEGMENT_BITS))
1449 *error_message = "windowed longcall crosses 1GB boundary; "
1450 "return may fail";
1451 return bfd_reloc_dangerous;
1455 return bfd_reloc_ok;
1457 case R_XTENSA_ASM_SIMPLIFY:
1459 /* Convert the L32R/CALLX to CALL. */
1460 bfd_reloc_status_type retval =
1461 elf_xtensa_do_asm_simplify (contents, address, input_section->size);
1462 if (retval != bfd_reloc_ok)
1463 return retval;
1465 /* The CALL needs to be relocated. Continue below for that part. */
1466 address += 3;
1467 howto = &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
1469 break;
1471 case R_XTENSA_32:
1472 case R_XTENSA_PLT:
1474 bfd_vma x;
1475 x = bfd_get_32 (abfd, contents + address);
1476 x = x + relocation;
1477 bfd_put_32 (abfd, x, contents + address);
1479 return bfd_reloc_ok;
1482 /* Read the instruction into a buffer and decode the opcode. */
1483 ibuff = xtensa_insnbuf_alloc (isa);
1484 xtensa_insnbuf_from_chars (isa, ibuff, contents + address);
1485 opcode = xtensa_decode_insn (isa, ibuff);
1487 /* Determine which operand is being relocated. */
1488 if (opcode == XTENSA_UNDEFINED)
1490 *error_message = "cannot decode instruction";
1491 return bfd_reloc_dangerous;
1494 if (howto->type < R_XTENSA_OP0 || howto->type > R_XTENSA_OP2)
1496 *error_message = "unexpected relocation";
1497 return bfd_reloc_dangerous;
1500 opnd = howto->type - R_XTENSA_OP0;
1502 /* Calculate the PC address for this instruction. */
1503 if (!howto->pc_relative)
1505 *error_message = "expected PC-relative relocation";
1506 return bfd_reloc_dangerous;
1509 self_address = (input_section->output_section->vma
1510 + input_section->output_offset
1511 + address);
1513 /* Apply the relocation. */
1514 operand = xtensa_get_operand (isa, opcode, opnd);
1515 newval = xtensa_operand_do_reloc (operand, relocation, self_address);
1516 encode_result = xtensa_operand_encode (operand, &newval);
1517 xtensa_operand_set_field (operand, ibuff, newval);
1519 /* Write the modified instruction back out of the buffer. */
1520 xtensa_insnbuf_to_chars (isa, ibuff, contents + address);
1521 free (ibuff);
1523 if (encode_result != xtensa_encode_result_ok)
1525 char *message = build_encoding_error_message (opcode, encode_result);
1526 *error_message = message;
1527 return bfd_reloc_dangerous;
1530 /* Final check for call. */
1531 if (is_direct_call_opcode (opcode)
1532 && is_windowed_call_opcode (opcode))
1534 if ((self_address >> CALL_SEGMENT_BITS) !=
1535 (relocation >> CALL_SEGMENT_BITS))
1537 *error_message = "windowed call crosses 1GB boundary; "
1538 "return may fail";
1539 return bfd_reloc_dangerous;
1543 return bfd_reloc_ok;
1547 static char *
1548 vsprint_msg VPARAMS ((const char *origmsg, const char *fmt, int arglen, ...))
1550 /* To reduce the size of the memory leak,
1551 we only use a single message buffer. */
1552 static bfd_size_type alloc_size = 0;
1553 static char *message = NULL;
1554 bfd_size_type orig_len, len = 0;
1555 bfd_boolean is_append;
1557 VA_OPEN (ap, arglen);
1558 VA_FIXEDARG (ap, const char *, origmsg);
1560 is_append = (origmsg == message);
1562 orig_len = strlen (origmsg);
1563 len = orig_len + strlen (fmt) + arglen + 20;
1564 if (len > alloc_size)
1566 message = (char *) bfd_realloc (message, len);
1567 alloc_size = len;
1569 if (!is_append)
1570 memcpy (message, origmsg, orig_len);
1571 vsprintf (message + orig_len, fmt, ap);
1572 VA_CLOSE (ap);
1573 return message;
1577 static char *
1578 build_encoding_error_message (opcode, encode_result)
1579 xtensa_opcode opcode;
1580 xtensa_encode_result encode_result;
1582 const char *opname = xtensa_opcode_name (xtensa_default_isa, opcode);
1583 const char *msg = NULL;
1585 switch (encode_result)
1587 case xtensa_encode_result_ok:
1588 msg = "unexpected valid encoding";
1589 break;
1590 case xtensa_encode_result_align:
1591 msg = "misaligned encoding";
1592 break;
1593 case xtensa_encode_result_not_in_table:
1594 msg = "encoding not in lookup table";
1595 break;
1596 case xtensa_encode_result_too_low:
1597 msg = "encoding out of range: too low";
1598 break;
1599 case xtensa_encode_result_too_high:
1600 msg = "encoding out of range: too high";
1601 break;
1602 case xtensa_encode_result_not_ok:
1603 default:
1604 msg = "could not encode";
1605 break;
1608 if (is_direct_call_opcode (opcode)
1609 && (encode_result == xtensa_encode_result_too_low
1610 || encode_result == xtensa_encode_result_too_high))
1612 msg = "direct call out of range";
1614 else if (opcode == get_l32r_opcode ())
1616 /* L32Rs have the strange interaction with encoding in that they
1617 have an unsigned immediate field, so libisa returns "too high"
1618 when the absolute value is out of range and never returns "too
1619 low", but I leave the "too low" message in case anything
1620 changes. */
1621 if (encode_result == xtensa_encode_result_too_low)
1622 msg = "literal out of range";
1623 else if (encode_result == xtensa_encode_result_too_high)
1624 msg = "literal placed after use";
1627 return vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
1631 /* This function is registered as the "special_function" in the
1632 Xtensa howto for handling simplify operations.
1633 bfd_perform_relocation / bfd_install_relocation use it to
1634 perform (install) the specified relocation. Since this replaces the code
1635 in bfd_perform_relocation, it is basically an Xtensa-specific,
1636 stripped-down version of bfd_perform_relocation. */
1638 static bfd_reloc_status_type
1639 bfd_elf_xtensa_reloc (abfd, reloc_entry, symbol, data, input_section,
1640 output_bfd, error_message)
1641 bfd *abfd;
1642 arelent *reloc_entry;
1643 asymbol *symbol;
1644 PTR data;
1645 asection *input_section;
1646 bfd *output_bfd;
1647 char **error_message;
1649 bfd_vma relocation;
1650 bfd_reloc_status_type flag;
1651 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1652 bfd_vma output_base = 0;
1653 reloc_howto_type *howto = reloc_entry->howto;
1654 asection *reloc_target_output_section;
1655 bfd_boolean is_weak_undef;
1657 /* ELF relocs are against symbols. If we are producing relocatable
1658 output, and the reloc is against an external symbol, the resulting
1659 reloc will also be against the same symbol. In such a case, we
1660 don't want to change anything about the way the reloc is handled,
1661 since it will all be done at final link time. This test is similar
1662 to what bfd_elf_generic_reloc does except that it lets relocs with
1663 howto->partial_inplace go through even if the addend is non-zero.
1664 (The real problem is that partial_inplace is set for XTENSA_32
1665 relocs to begin with, but that's a long story and there's little we
1666 can do about it now....) */
1668 if (output_bfd != (bfd *) NULL
1669 && (symbol->flags & BSF_SECTION_SYM) == 0)
1671 reloc_entry->address += input_section->output_offset;
1672 return bfd_reloc_ok;
1675 /* Is the address of the relocation really within the section? */
1676 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1677 return bfd_reloc_outofrange;
1679 /* Work out which section the relocation is targeted at and the
1680 initial relocation command value. */
1682 /* Get symbol value. (Common symbols are special.) */
1683 if (bfd_is_com_section (symbol->section))
1684 relocation = 0;
1685 else
1686 relocation = symbol->value;
1688 reloc_target_output_section = symbol->section->output_section;
1690 /* Convert input-section-relative symbol value to absolute. */
1691 if ((output_bfd && !howto->partial_inplace)
1692 || reloc_target_output_section == NULL)
1693 output_base = 0;
1694 else
1695 output_base = reloc_target_output_section->vma;
1697 relocation += output_base + symbol->section->output_offset;
1699 /* Add in supplied addend. */
1700 relocation += reloc_entry->addend;
1702 /* Here the variable relocation holds the final address of the
1703 symbol we are relocating against, plus any addend. */
1704 if (output_bfd)
1706 if (!howto->partial_inplace)
1708 /* This is a partial relocation, and we want to apply the relocation
1709 to the reloc entry rather than the raw data. Everything except
1710 relocations against section symbols has already been handled
1711 above. */
1713 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
1714 reloc_entry->addend = relocation;
1715 reloc_entry->address += input_section->output_offset;
1716 return bfd_reloc_ok;
1718 else
1720 reloc_entry->address += input_section->output_offset;
1721 reloc_entry->addend = 0;
1725 is_weak_undef = (bfd_is_und_section (symbol->section)
1726 && (symbol->flags & BSF_WEAK) != 0);
1727 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1728 (bfd_byte *) data, (bfd_vma) octets,
1729 is_weak_undef, error_message);
1731 if (flag == bfd_reloc_dangerous)
1733 /* Add the symbol name to the error message. */
1734 if (! *error_message)
1735 *error_message = "";
1736 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
1737 strlen (symbol->name) + 17,
1738 symbol->name, reloc_entry->addend);
1741 return flag;
1745 /* Set up an entry in the procedure linkage table. */
1747 static bfd_vma
1748 elf_xtensa_create_plt_entry (dynobj, output_bfd, reloc_index)
1749 bfd *dynobj;
1750 bfd *output_bfd;
1751 unsigned reloc_index;
1753 asection *splt, *sgotplt;
1754 bfd_vma plt_base, got_base;
1755 bfd_vma code_offset, lit_offset;
1756 int chunk;
1758 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
1759 splt = elf_xtensa_get_plt_section (dynobj, chunk);
1760 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1761 BFD_ASSERT (splt != NULL && sgotplt != NULL);
1763 plt_base = splt->output_section->vma + splt->output_offset;
1764 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1766 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
1767 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
1769 /* Fill in the literal entry. This is the offset of the dynamic
1770 relocation entry. */
1771 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
1772 sgotplt->contents + lit_offset);
1774 /* Fill in the entry in the procedure linkage table. */
1775 memcpy (splt->contents + code_offset,
1776 (bfd_big_endian (output_bfd)
1777 ? elf_xtensa_be_plt_entry
1778 : elf_xtensa_le_plt_entry),
1779 PLT_ENTRY_SIZE);
1780 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
1781 plt_base + code_offset + 3),
1782 splt->contents + code_offset + 4);
1783 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
1784 plt_base + code_offset + 6),
1785 splt->contents + code_offset + 7);
1786 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
1787 plt_base + code_offset + 9),
1788 splt->contents + code_offset + 10);
1790 return plt_base + code_offset;
1794 /* Relocate an Xtensa ELF section. This is invoked by the linker for
1795 both relocatable and final links. */
1797 static bfd_boolean
1798 elf_xtensa_relocate_section (output_bfd, info, input_bfd,
1799 input_section, contents, relocs,
1800 local_syms, local_sections)
1801 bfd *output_bfd;
1802 struct bfd_link_info *info;
1803 bfd *input_bfd;
1804 asection *input_section;
1805 bfd_byte *contents;
1806 Elf_Internal_Rela *relocs;
1807 Elf_Internal_Sym *local_syms;
1808 asection **local_sections;
1810 Elf_Internal_Shdr *symtab_hdr;
1811 Elf_Internal_Rela *rel;
1812 Elf_Internal_Rela *relend;
1813 struct elf_link_hash_entry **sym_hashes;
1814 asection *srelgot, *srelplt;
1815 bfd *dynobj;
1816 property_table_entry *lit_table = 0;
1817 int ltblsize = 0;
1818 char *error_message = NULL;
1820 if (xtensa_default_isa == NULL)
1821 xtensa_isa_init ();
1823 dynobj = elf_hash_table (info)->dynobj;
1824 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1825 sym_hashes = elf_sym_hashes (input_bfd);
1827 srelgot = NULL;
1828 srelplt = NULL;
1829 if (dynobj != NULL)
1831 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
1832 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1835 if (elf_hash_table (info)->dynamic_sections_created)
1837 ltblsize = xtensa_read_table_entries (input_bfd, input_section,
1838 &lit_table, XTENSA_LIT_SEC_NAME);
1839 if (ltblsize < 0)
1840 return FALSE;
1843 rel = relocs;
1844 relend = relocs + input_section->reloc_count;
1845 for (; rel < relend; rel++)
1847 int r_type;
1848 reloc_howto_type *howto;
1849 unsigned long r_symndx;
1850 struct elf_link_hash_entry *h;
1851 Elf_Internal_Sym *sym;
1852 asection *sec;
1853 bfd_vma relocation;
1854 bfd_reloc_status_type r;
1855 bfd_boolean is_weak_undef;
1856 bfd_boolean unresolved_reloc;
1857 bfd_boolean warned;
1859 r_type = ELF32_R_TYPE (rel->r_info);
1860 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
1861 || r_type == (int) R_XTENSA_GNU_VTENTRY)
1862 continue;
1864 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
1866 bfd_set_error (bfd_error_bad_value);
1867 return FALSE;
1869 howto = &elf_howto_table[r_type];
1871 r_symndx = ELF32_R_SYM (rel->r_info);
1873 if (info->relocatable)
1875 /* This is a relocatable link.
1876 1) If the reloc is against a section symbol, adjust
1877 according to the output section.
1878 2) If there is a new target for this relocation,
1879 the new target will be in the same output section.
1880 We adjust the relocation by the output section
1881 difference. */
1883 if (relaxing_section)
1885 /* Check if this references a section in another input file. */
1886 do_fix_for_relocatable_link (rel, input_bfd, input_section);
1887 r_type = ELF32_R_TYPE (rel->r_info);
1890 if (r_type == R_XTENSA_ASM_SIMPLIFY)
1892 /* Convert ASM_SIMPLIFY into the simpler relocation
1893 so that they never escape a relaxing link. */
1894 contract_asm_expansion (contents, input_section->size, rel);
1895 r_type = ELF32_R_TYPE (rel->r_info);
1898 /* This is a relocatable link, so we don't have to change
1899 anything unless the reloc is against a section symbol,
1900 in which case we have to adjust according to where the
1901 section symbol winds up in the output section. */
1902 if (r_symndx < symtab_hdr->sh_info)
1904 sym = local_syms + r_symndx;
1905 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1907 sec = local_sections[r_symndx];
1908 rel->r_addend += sec->output_offset + sym->st_value;
1912 /* If there is an addend with a partial_inplace howto,
1913 then move the addend to the contents. This is a hack
1914 to work around problems with DWARF in relocatable links
1915 with some previous version of BFD. Now we can't easily get
1916 rid of the hack without breaking backward compatibility.... */
1917 if (rel->r_addend)
1919 howto = &elf_howto_table[r_type];
1920 if (howto->partial_inplace)
1922 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
1923 rel->r_addend, contents,
1924 rel->r_offset, FALSE,
1925 &error_message);
1926 if (r != bfd_reloc_ok)
1928 if (!((*info->callbacks->reloc_dangerous)
1929 (info, error_message, input_bfd, input_section,
1930 rel->r_offset)))
1931 return FALSE;
1933 rel->r_addend = 0;
1937 /* Done with work for relocatable link; continue with next reloc. */
1938 continue;
1941 /* This is a final link. */
1943 h = NULL;
1944 sym = NULL;
1945 sec = NULL;
1946 is_weak_undef = FALSE;
1947 unresolved_reloc = FALSE;
1948 warned = FALSE;
1950 if (howto->partial_inplace)
1952 /* Because R_XTENSA_32 was made partial_inplace to fix some
1953 problems with DWARF info in partial links, there may be
1954 an addend stored in the contents. Take it out of there
1955 and move it back into the addend field of the reloc. */
1956 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
1957 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
1960 if (r_symndx < symtab_hdr->sh_info)
1962 sym = local_syms + r_symndx;
1963 sec = local_sections[r_symndx];
1964 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1966 else
1968 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1969 r_symndx, symtab_hdr, sym_hashes,
1970 h, sec, relocation,
1971 unresolved_reloc, warned);
1973 if (relocation == 0
1974 && !unresolved_reloc
1975 && h->root.type == bfd_link_hash_undefweak)
1976 is_weak_undef = TRUE;
1979 if (relaxing_section)
1981 /* Check if this references a section in another input file. */
1982 do_fix_for_final_link (rel, input_section, &relocation);
1984 /* Update some already cached values. */
1985 r_type = ELF32_R_TYPE (rel->r_info);
1986 howto = &elf_howto_table[r_type];
1989 /* Sanity check the address. */
1990 if (rel->r_offset >= bfd_get_section_limit (input_bfd, input_section)
1991 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
1993 bfd_set_error (bfd_error_bad_value);
1994 return FALSE;
1997 /* Generate dynamic relocations. */
1998 if (elf_hash_table (info)->dynamic_sections_created)
2000 bfd_boolean dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
2002 if (dynamic_symbol && (r_type == R_XTENSA_OP0
2003 || r_type == R_XTENSA_OP1
2004 || r_type == R_XTENSA_OP2))
2006 /* This is an error. The symbol's real value won't be known
2007 until runtime and it's likely to be out of range anyway. */
2008 const char *name = h->root.root.string;
2009 error_message = vsprint_msg ("invalid relocation for dynamic "
2010 "symbol", ": %s",
2011 strlen (name) + 2, name);
2012 if (!((*info->callbacks->reloc_dangerous)
2013 (info, error_message, input_bfd, input_section,
2014 rel->r_offset)))
2015 return FALSE;
2017 else if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
2018 && (input_section->flags & SEC_ALLOC) != 0
2019 && (dynamic_symbol || info->shared))
2021 Elf_Internal_Rela outrel;
2022 bfd_byte *loc;
2023 asection *srel;
2025 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2026 srel = srelplt;
2027 else
2028 srel = srelgot;
2030 BFD_ASSERT (srel != NULL);
2032 outrel.r_offset =
2033 _bfd_elf_section_offset (output_bfd, info,
2034 input_section, rel->r_offset);
2036 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2037 memset (&outrel, 0, sizeof outrel);
2038 else
2040 outrel.r_offset += (input_section->output_section->vma
2041 + input_section->output_offset);
2043 /* Complain if the relocation is in a read-only section
2044 and not in a literal pool. */
2045 if ((input_section->flags & SEC_READONLY) != 0
2046 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
2047 outrel.r_offset))
2049 error_message =
2050 _("dynamic relocation in read-only section");
2051 if (!((*info->callbacks->reloc_dangerous)
2052 (info, error_message, input_bfd, input_section,
2053 rel->r_offset)))
2054 return FALSE;
2057 if (dynamic_symbol)
2059 outrel.r_addend = rel->r_addend;
2060 rel->r_addend = 0;
2062 if (r_type == R_XTENSA_32)
2064 outrel.r_info =
2065 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2066 relocation = 0;
2068 else /* r_type == R_XTENSA_PLT */
2070 outrel.r_info =
2071 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2073 /* Create the PLT entry and set the initial
2074 contents of the literal entry to the address of
2075 the PLT entry. */
2076 relocation =
2077 elf_xtensa_create_plt_entry (dynobj, output_bfd,
2078 srel->reloc_count);
2080 unresolved_reloc = FALSE;
2082 else
2084 /* Generate a RELATIVE relocation. */
2085 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2086 outrel.r_addend = 0;
2090 loc = (srel->contents
2091 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2092 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2093 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2094 <= srel->size);
2098 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2099 because such sections are not SEC_ALLOC and thus ld.so will
2100 not process them. */
2101 if (unresolved_reloc
2102 && !((input_section->flags & SEC_DEBUGGING) != 0
2103 && h->def_dynamic))
2104 (*_bfd_error_handler)
2105 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2106 input_bfd,
2107 input_section,
2108 (long) rel->r_offset,
2109 h->root.root.string);
2111 /* There's no point in calling bfd_perform_relocation here.
2112 Just go directly to our "special function". */
2113 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2114 relocation + rel->r_addend,
2115 contents, rel->r_offset, is_weak_undef,
2116 &error_message);
2118 if (r != bfd_reloc_ok && !warned)
2120 const char *name;
2122 BFD_ASSERT (r == bfd_reloc_dangerous);
2123 BFD_ASSERT (error_message != (char *) NULL);
2125 if (h != NULL)
2126 name = h->root.root.string;
2127 else
2129 name = bfd_elf_string_from_elf_section
2130 (input_bfd, symtab_hdr->sh_link, sym->st_name);
2131 if (name && *name == '\0')
2132 name = bfd_section_name (input_bfd, sec);
2134 if (name)
2135 error_message = vsprint_msg (error_message, ": %s",
2136 strlen (name), name);
2137 if (!((*info->callbacks->reloc_dangerous)
2138 (info, error_message, input_bfd, input_section,
2139 rel->r_offset)))
2140 return FALSE;
2144 if (lit_table)
2145 free (lit_table);
2147 input_section->reloc_done = TRUE;
2149 return TRUE;
2153 /* Finish up dynamic symbol handling. There's not much to do here since
2154 the PLT and GOT entries are all set up by relocate_section. */
2156 static bfd_boolean
2157 elf_xtensa_finish_dynamic_symbol (output_bfd, info, h, sym)
2158 bfd *output_bfd ATTRIBUTE_UNUSED;
2159 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2160 struct elf_link_hash_entry *h;
2161 Elf_Internal_Sym *sym;
2163 if (h->needs_plt
2164 && !h->def_regular)
2166 /* Mark the symbol as undefined, rather than as defined in
2167 the .plt section. Leave the value alone. */
2168 sym->st_shndx = SHN_UNDEF;
2171 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2172 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2173 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2174 sym->st_shndx = SHN_ABS;
2176 return TRUE;
2180 /* Combine adjacent literal table entries in the output. Adjacent
2181 entries within each input section may have been removed during
2182 relaxation, but we repeat the process here, even though it's too late
2183 to shrink the output section, because it's important to minimize the
2184 number of literal table entries to reduce the start-up work for the
2185 runtime linker. Returns the number of remaining table entries or -1
2186 on error. */
2188 static int
2189 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc)
2190 bfd *output_bfd;
2191 asection *sxtlit;
2192 asection *sgotloc;
2194 bfd_byte *contents;
2195 property_table_entry *table;
2196 bfd_size_type section_size, sgotloc_size;
2197 bfd_vma offset;
2198 int n, m, num;
2200 section_size = sxtlit->size;
2201 BFD_ASSERT (section_size % 8 == 0);
2202 num = section_size / 8;
2204 sgotloc_size = sgotloc->size;
2205 if (sgotloc_size != section_size)
2207 (*_bfd_error_handler)
2208 ("internal inconsistency in size of .got.loc section");
2209 return -1;
2212 table = bfd_malloc (num * sizeof (property_table_entry));
2213 if (table == 0)
2214 return -1;
2216 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
2217 propagates to the output section, where it doesn't really apply and
2218 where it breaks the following call to bfd_malloc_and_get_section. */
2219 sxtlit->flags &= ~SEC_IN_MEMORY;
2221 if (!bfd_malloc_and_get_section (output_bfd, sxtlit, &contents))
2223 if (contents != 0)
2224 free (contents);
2225 free (table);
2226 return -1;
2229 /* There should never be any relocations left at this point, so this
2230 is quite a bit easier than what is done during relaxation. */
2232 /* Copy the raw contents into a property table array and sort it. */
2233 offset = 0;
2234 for (n = 0; n < num; n++)
2236 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
2237 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
2238 offset += 8;
2240 qsort (table, num, sizeof (property_table_entry), property_table_compare);
2242 for (n = 0; n < num; n++)
2244 bfd_boolean remove = FALSE;
2246 if (table[n].size == 0)
2247 remove = TRUE;
2248 else if (n > 0 &&
2249 (table[n-1].address + table[n-1].size == table[n].address))
2251 table[n-1].size += table[n].size;
2252 remove = TRUE;
2255 if (remove)
2257 for (m = n; m < num - 1; m++)
2259 table[m].address = table[m+1].address;
2260 table[m].size = table[m+1].size;
2263 n--;
2264 num--;
2268 /* Copy the data back to the raw contents. */
2269 offset = 0;
2270 for (n = 0; n < num; n++)
2272 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
2273 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
2274 offset += 8;
2277 /* Clear the removed bytes. */
2278 if ((bfd_size_type) (num * 8) < section_size)
2279 memset (&contents[num * 8], 0, section_size - num * 8);
2281 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
2282 section_size))
2283 return -1;
2285 /* Copy the contents to ".got.loc". */
2286 memcpy (sgotloc->contents, contents, section_size);
2288 free (contents);
2289 free (table);
2290 return num;
2294 /* Finish up the dynamic sections. */
2296 static bfd_boolean
2297 elf_xtensa_finish_dynamic_sections (output_bfd, info)
2298 bfd *output_bfd;
2299 struct bfd_link_info *info;
2301 bfd *dynobj;
2302 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
2303 Elf32_External_Dyn *dyncon, *dynconend;
2304 int num_xtlit_entries;
2306 if (! elf_hash_table (info)->dynamic_sections_created)
2307 return TRUE;
2309 dynobj = elf_hash_table (info)->dynobj;
2310 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2311 BFD_ASSERT (sdyn != NULL);
2313 /* Set the first entry in the global offset table to the address of
2314 the dynamic section. */
2315 sgot = bfd_get_section_by_name (dynobj, ".got");
2316 if (sgot)
2318 BFD_ASSERT (sgot->size == 4);
2319 if (sdyn == NULL)
2320 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2321 else
2322 bfd_put_32 (output_bfd,
2323 sdyn->output_section->vma + sdyn->output_offset,
2324 sgot->contents);
2327 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2328 if (srelplt != NULL && srelplt->size != 0)
2330 asection *sgotplt, *srelgot, *spltlittbl;
2331 int chunk, plt_chunks, plt_entries;
2332 Elf_Internal_Rela irela;
2333 bfd_byte *loc;
2334 unsigned rtld_reloc;
2336 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
2337 BFD_ASSERT (srelgot != NULL);
2339 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
2340 BFD_ASSERT (spltlittbl != NULL);
2342 /* Find the first XTENSA_RTLD relocation. Presumably the rest
2343 of them follow immediately after.... */
2344 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
2346 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2347 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2348 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
2349 break;
2351 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
2353 plt_entries = srelplt->size / sizeof (Elf32_External_Rela);
2354 plt_chunks =
2355 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
2357 for (chunk = 0; chunk < plt_chunks; chunk++)
2359 int chunk_entries = 0;
2361 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
2362 BFD_ASSERT (sgotplt != NULL);
2364 /* Emit special RTLD relocations for the first two entries in
2365 each chunk of the .got.plt section. */
2367 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2368 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2369 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2370 irela.r_offset = (sgotplt->output_section->vma
2371 + sgotplt->output_offset);
2372 irela.r_addend = 1; /* tell rtld to set value to resolver function */
2373 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2374 rtld_reloc += 1;
2375 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2377 /* Next literal immediately follows the first. */
2378 loc += sizeof (Elf32_External_Rela);
2379 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2380 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2381 irela.r_offset = (sgotplt->output_section->vma
2382 + sgotplt->output_offset + 4);
2383 /* Tell rtld to set value to object's link map. */
2384 irela.r_addend = 2;
2385 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2386 rtld_reloc += 1;
2387 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2389 /* Fill in the literal table. */
2390 if (chunk < plt_chunks - 1)
2391 chunk_entries = PLT_ENTRIES_PER_CHUNK;
2392 else
2393 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
2395 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->size);
2396 bfd_put_32 (output_bfd,
2397 sgotplt->output_section->vma + sgotplt->output_offset,
2398 spltlittbl->contents + (chunk * 8) + 0);
2399 bfd_put_32 (output_bfd,
2400 8 + (chunk_entries * 4),
2401 spltlittbl->contents + (chunk * 8) + 4);
2404 /* All the dynamic relocations have been emitted at this point.
2405 Make sure the relocation sections are the correct size. */
2406 if (srelgot->size != (sizeof (Elf32_External_Rela)
2407 * srelgot->reloc_count)
2408 || srelplt->size != (sizeof (Elf32_External_Rela)
2409 * srelplt->reloc_count))
2410 abort ();
2412 /* The .xt.lit.plt section has just been modified. This must
2413 happen before the code below which combines adjacent literal
2414 table entries, and the .xt.lit.plt contents have to be forced to
2415 the output here. */
2416 if (! bfd_set_section_contents (output_bfd,
2417 spltlittbl->output_section,
2418 spltlittbl->contents,
2419 spltlittbl->output_offset,
2420 spltlittbl->size))
2421 return FALSE;
2422 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
2423 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
2426 /* Combine adjacent literal table entries. */
2427 BFD_ASSERT (! info->relocatable);
2428 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
2429 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
2430 BFD_ASSERT (sxtlit && sgotloc);
2431 num_xtlit_entries =
2432 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
2433 if (num_xtlit_entries < 0)
2434 return FALSE;
2436 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2437 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2438 for (; dyncon < dynconend; dyncon++)
2440 Elf_Internal_Dyn dyn;
2441 const char *name;
2442 asection *s;
2444 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2446 switch (dyn.d_tag)
2448 default:
2449 break;
2451 case DT_XTENSA_GOT_LOC_SZ:
2452 dyn.d_un.d_val = num_xtlit_entries;
2453 break;
2455 case DT_XTENSA_GOT_LOC_OFF:
2456 name = ".got.loc";
2457 goto get_vma;
2458 case DT_PLTGOT:
2459 name = ".got";
2460 goto get_vma;
2461 case DT_JMPREL:
2462 name = ".rela.plt";
2463 get_vma:
2464 s = bfd_get_section_by_name (output_bfd, name);
2465 BFD_ASSERT (s);
2466 dyn.d_un.d_ptr = s->vma;
2467 break;
2469 case DT_PLTRELSZ:
2470 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2471 BFD_ASSERT (s);
2472 dyn.d_un.d_val = s->size;
2473 break;
2475 case DT_RELASZ:
2476 /* Adjust RELASZ to not include JMPREL. This matches what
2477 glibc expects and what is done for several other ELF
2478 targets (e.g., i386, alpha), but the "correct" behavior
2479 seems to be unresolved. Since the linker script arranges
2480 for .rela.plt to follow all other relocation sections, we
2481 don't have to worry about changing the DT_RELA entry. */
2482 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2483 if (s)
2484 dyn.d_un.d_val -= s->size;
2485 break;
2488 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2491 return TRUE;
2495 /* Functions for dealing with the e_flags field. */
2497 /* Merge backend specific data from an object file to the output
2498 object file when linking. */
2500 static bfd_boolean
2501 elf_xtensa_merge_private_bfd_data (ibfd, obfd)
2502 bfd *ibfd;
2503 bfd *obfd;
2505 unsigned out_mach, in_mach;
2506 flagword out_flag, in_flag;
2508 /* Check if we have the same endianess. */
2509 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
2510 return FALSE;
2512 /* Don't even pretend to support mixed-format linking. */
2513 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2514 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2515 return FALSE;
2517 out_flag = elf_elfheader (obfd)->e_flags;
2518 in_flag = elf_elfheader (ibfd)->e_flags;
2520 out_mach = out_flag & EF_XTENSA_MACH;
2521 in_mach = in_flag & EF_XTENSA_MACH;
2522 if (out_mach != in_mach)
2524 (*_bfd_error_handler)
2525 ("%B: incompatible machine type. Output is 0x%x. Input is 0x%x",
2526 ibfd, out_mach, in_mach);
2527 bfd_set_error (bfd_error_wrong_format);
2528 return FALSE;
2531 if (! elf_flags_init (obfd))
2533 elf_flags_init (obfd) = TRUE;
2534 elf_elfheader (obfd)->e_flags = in_flag;
2536 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2537 && bfd_get_arch_info (obfd)->the_default)
2538 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2539 bfd_get_mach (ibfd));
2541 return TRUE;
2544 if ((out_flag & EF_XTENSA_XT_INSN) !=
2545 (in_flag & EF_XTENSA_XT_INSN))
2546 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
2548 if ((out_flag & EF_XTENSA_XT_LIT) !=
2549 (in_flag & EF_XTENSA_XT_LIT))
2550 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
2552 return TRUE;
2556 static bfd_boolean
2557 elf_xtensa_set_private_flags (abfd, flags)
2558 bfd *abfd;
2559 flagword flags;
2561 BFD_ASSERT (!elf_flags_init (abfd)
2562 || elf_elfheader (abfd)->e_flags == flags);
2564 elf_elfheader (abfd)->e_flags |= flags;
2565 elf_flags_init (abfd) = TRUE;
2567 return TRUE;
2571 extern flagword
2572 elf_xtensa_get_private_bfd_flags (abfd)
2573 bfd *abfd;
2575 return elf_elfheader (abfd)->e_flags;
2579 static bfd_boolean
2580 elf_xtensa_print_private_bfd_data (abfd, farg)
2581 bfd *abfd;
2582 PTR farg;
2584 FILE *f = (FILE *) farg;
2585 flagword e_flags = elf_elfheader (abfd)->e_flags;
2587 fprintf (f, "\nXtensa header:\n");
2588 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
2589 fprintf (f, "\nMachine = Base\n");
2590 else
2591 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
2593 fprintf (f, "Insn tables = %s\n",
2594 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
2596 fprintf (f, "Literal tables = %s\n",
2597 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
2599 return _bfd_elf_print_private_bfd_data (abfd, farg);
2603 /* Set the right machine number for an Xtensa ELF file. */
2605 static bfd_boolean
2606 elf_xtensa_object_p (abfd)
2607 bfd *abfd;
2609 int mach;
2610 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
2612 switch (arch)
2614 case E_XTENSA_MACH:
2615 mach = bfd_mach_xtensa;
2616 break;
2617 default:
2618 return FALSE;
2621 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
2622 return TRUE;
2626 /* The final processing done just before writing out an Xtensa ELF object
2627 file. This gets the Xtensa architecture right based on the machine
2628 number. */
2630 static void
2631 elf_xtensa_final_write_processing (abfd, linker)
2632 bfd *abfd;
2633 bfd_boolean linker ATTRIBUTE_UNUSED;
2635 int mach;
2636 unsigned long val;
2638 switch (mach = bfd_get_mach (abfd))
2640 case bfd_mach_xtensa:
2641 val = E_XTENSA_MACH;
2642 break;
2643 default:
2644 return;
2647 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
2648 elf_elfheader (abfd)->e_flags |= val;
2652 static enum elf_reloc_type_class
2653 elf_xtensa_reloc_type_class (rela)
2654 const Elf_Internal_Rela *rela;
2656 switch ((int) ELF32_R_TYPE (rela->r_info))
2658 case R_XTENSA_RELATIVE:
2659 return reloc_class_relative;
2660 case R_XTENSA_JMP_SLOT:
2661 return reloc_class_plt;
2662 default:
2663 return reloc_class_normal;
2668 static bfd_boolean
2669 elf_xtensa_discard_info_for_section (abfd, cookie, info, sec)
2670 bfd *abfd;
2671 struct elf_reloc_cookie *cookie;
2672 struct bfd_link_info *info;
2673 asection *sec;
2675 bfd_byte *contents;
2676 bfd_vma section_size;
2677 bfd_vma offset, actual_offset;
2678 size_t removed_bytes = 0;
2680 section_size = sec->size;
2681 if (section_size == 0 || section_size % 8 != 0)
2682 return FALSE;
2684 if (sec->output_section
2685 && bfd_is_abs_section (sec->output_section))
2686 return FALSE;
2688 contents = retrieve_contents (abfd, sec, info->keep_memory);
2689 if (!contents)
2690 return FALSE;
2692 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
2693 if (!cookie->rels)
2695 release_contents (sec, contents);
2696 return FALSE;
2699 cookie->rel = cookie->rels;
2700 cookie->relend = cookie->rels + sec->reloc_count;
2702 for (offset = 0; offset < section_size; offset += 8)
2704 actual_offset = offset - removed_bytes;
2706 /* The ...symbol_deleted_p function will skip over relocs but it
2707 won't adjust their offsets, so do that here. */
2708 while (cookie->rel < cookie->relend
2709 && cookie->rel->r_offset < offset)
2711 cookie->rel->r_offset -= removed_bytes;
2712 cookie->rel++;
2715 while (cookie->rel < cookie->relend
2716 && cookie->rel->r_offset == offset)
2718 if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
2720 /* Remove the table entry. (If the reloc type is NONE, then
2721 the entry has already been merged with another and deleted
2722 during relaxation.) */
2723 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
2725 /* Shift the contents up. */
2726 if (offset + 8 < section_size)
2727 memmove (&contents[actual_offset],
2728 &contents[actual_offset+8],
2729 section_size - offset - 8);
2730 removed_bytes += 8;
2733 /* Remove this relocation. */
2734 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
2737 /* Adjust the relocation offset for previous removals. This
2738 should not be done before calling ...symbol_deleted_p
2739 because it might mess up the offset comparisons there.
2740 Make sure the offset doesn't underflow in the case where
2741 the first entry is removed. */
2742 if (cookie->rel->r_offset >= removed_bytes)
2743 cookie->rel->r_offset -= removed_bytes;
2744 else
2745 cookie->rel->r_offset = 0;
2747 cookie->rel++;
2751 if (removed_bytes != 0)
2753 /* Adjust any remaining relocs (shouldn't be any). */
2754 for (; cookie->rel < cookie->relend; cookie->rel++)
2756 if (cookie->rel->r_offset >= removed_bytes)
2757 cookie->rel->r_offset -= removed_bytes;
2758 else
2759 cookie->rel->r_offset = 0;
2762 /* Clear the removed bytes. */
2763 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
2765 pin_contents (sec, contents);
2766 pin_internal_relocs (sec, cookie->rels);
2768 /* Shrink size. */
2769 sec->size = section_size - removed_bytes;
2771 if (xtensa_is_littable_section (sec))
2773 bfd *dynobj = elf_hash_table (info)->dynobj;
2774 if (dynobj)
2776 asection *sgotloc =
2777 bfd_get_section_by_name (dynobj, ".got.loc");
2778 if (sgotloc)
2779 sgotloc->size -= removed_bytes;
2783 else
2785 release_contents (sec, contents);
2786 release_internal_relocs (sec, cookie->rels);
2789 return (removed_bytes != 0);
2793 static bfd_boolean
2794 elf_xtensa_discard_info (abfd, cookie, info)
2795 bfd *abfd;
2796 struct elf_reloc_cookie *cookie;
2797 struct bfd_link_info *info;
2799 asection *sec;
2800 bfd_boolean changed = FALSE;
2802 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2804 if (xtensa_is_property_section (sec))
2806 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
2807 changed = TRUE;
2811 return changed;
2815 static bfd_boolean
2816 elf_xtensa_ignore_discarded_relocs (sec)
2817 asection *sec;
2819 return xtensa_is_property_section (sec);
2823 /* Support for core dump NOTE sections. */
2825 static bfd_boolean
2826 elf_xtensa_grok_prstatus (abfd, note)
2827 bfd *abfd;
2828 Elf_Internal_Note *note;
2830 int offset;
2831 unsigned int size;
2833 /* The size for Xtensa is variable, so don't try to recognize the format
2834 based on the size. Just assume this is GNU/Linux. */
2836 /* pr_cursig */
2837 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2839 /* pr_pid */
2840 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
2842 /* pr_reg */
2843 offset = 72;
2844 size = note->descsz - offset - 4;
2846 /* Make a ".reg/999" section. */
2847 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2848 size, note->descpos + offset);
2852 static bfd_boolean
2853 elf_xtensa_grok_psinfo (abfd, note)
2854 bfd *abfd;
2855 Elf_Internal_Note *note;
2857 switch (note->descsz)
2859 default:
2860 return FALSE;
2862 case 128: /* GNU/Linux elf_prpsinfo */
2863 elf_tdata (abfd)->core_program
2864 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
2865 elf_tdata (abfd)->core_command
2866 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
2869 /* Note that for some reason, a spurious space is tacked
2870 onto the end of the args in some (at least one anyway)
2871 implementations, so strip it off if it exists. */
2874 char *command = elf_tdata (abfd)->core_command;
2875 int n = strlen (command);
2877 if (0 < n && command[n - 1] == ' ')
2878 command[n - 1] = '\0';
2881 return TRUE;
2885 /* Generic Xtensa configurability stuff. */
2887 static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
2888 static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
2889 static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
2890 static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
2891 static xtensa_opcode call0_op = XTENSA_UNDEFINED;
2892 static xtensa_opcode call4_op = XTENSA_UNDEFINED;
2893 static xtensa_opcode call8_op = XTENSA_UNDEFINED;
2894 static xtensa_opcode call12_op = XTENSA_UNDEFINED;
2896 static void
2897 init_call_opcodes ()
2899 if (callx0_op == XTENSA_UNDEFINED)
2901 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
2902 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
2903 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
2904 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
2905 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
2906 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
2907 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
2908 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
2913 static bfd_boolean
2914 is_indirect_call_opcode (opcode)
2915 xtensa_opcode opcode;
2917 init_call_opcodes ();
2918 return (opcode == callx0_op
2919 || opcode == callx4_op
2920 || opcode == callx8_op
2921 || opcode == callx12_op);
2925 static bfd_boolean
2926 is_direct_call_opcode (opcode)
2927 xtensa_opcode opcode;
2929 init_call_opcodes ();
2930 return (opcode == call0_op
2931 || opcode == call4_op
2932 || opcode == call8_op
2933 || opcode == call12_op);
2937 static bfd_boolean
2938 is_windowed_call_opcode (opcode)
2939 xtensa_opcode opcode;
2941 init_call_opcodes ();
2942 return (opcode == call4_op
2943 || opcode == call8_op
2944 || opcode == call12_op
2945 || opcode == callx4_op
2946 || opcode == callx8_op
2947 || opcode == callx12_op);
2951 static xtensa_opcode
2952 get_l32r_opcode (void)
2954 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
2955 if (l32r_opcode == XTENSA_UNDEFINED)
2957 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
2958 BFD_ASSERT (l32r_opcode != XTENSA_UNDEFINED);
2960 return l32r_opcode;
2964 static bfd_vma
2965 l32r_offset (addr, pc)
2966 bfd_vma addr;
2967 bfd_vma pc;
2969 bfd_vma offset;
2971 offset = addr - ((pc+3) & -4);
2972 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
2973 offset = (signed int) offset >> 2;
2974 BFD_ASSERT ((signed int) offset >> 16 == -1);
2975 return offset;
2979 /* Get the operand number for a PC-relative relocation.
2980 If the relocation is not a PC-relative one, return (-1). */
2982 static int
2983 get_relocation_opnd (irel)
2984 Elf_Internal_Rela *irel;
2986 if (ELF32_R_TYPE (irel->r_info) < R_XTENSA_OP0
2987 || ELF32_R_TYPE (irel->r_info) >= R_XTENSA_max)
2988 return -1;
2989 return ELF32_R_TYPE (irel->r_info) - R_XTENSA_OP0;
2993 /* Get the opcode for a relocation. */
2995 static xtensa_opcode
2996 get_relocation_opcode (sec, contents, irel)
2997 asection *sec;
2998 bfd_byte *contents;
2999 Elf_Internal_Rela *irel;
3001 static xtensa_insnbuf ibuff = NULL;
3002 xtensa_isa isa = xtensa_default_isa;
3004 if (get_relocation_opnd (irel) == -1)
3005 return XTENSA_UNDEFINED;
3007 if (contents == NULL)
3008 return XTENSA_UNDEFINED;
3010 if (sec->size <= irel->r_offset)
3011 return XTENSA_UNDEFINED;
3013 if (ibuff == NULL)
3014 ibuff = xtensa_insnbuf_alloc (isa);
3016 /* Decode the instruction. */
3017 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset]);
3018 return xtensa_decode_insn (isa, ibuff);
3022 bfd_boolean
3023 is_l32r_relocation (sec, contents, irel)
3024 asection *sec;
3025 bfd_byte *contents;
3026 Elf_Internal_Rela *irel;
3028 xtensa_opcode opcode;
3030 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_OP1)
3031 return FALSE;
3033 opcode = get_relocation_opcode (sec, contents, irel);
3034 return (opcode == get_l32r_opcode ());
3038 /* Code for transforming CALLs at link-time. */
3040 static bfd_reloc_status_type
3041 elf_xtensa_do_asm_simplify (contents, address, content_length)
3042 bfd_byte *contents;
3043 bfd_vma address;
3044 bfd_vma content_length;
3046 static xtensa_insnbuf insnbuf = NULL;
3047 xtensa_opcode opcode;
3048 xtensa_operand operand;
3049 xtensa_opcode direct_call_opcode;
3050 xtensa_isa isa = xtensa_default_isa;
3051 bfd_byte *chbuf = contents + address;
3052 int opn;
3054 if (insnbuf == NULL)
3055 insnbuf = xtensa_insnbuf_alloc (isa);
3057 if (content_length < address)
3059 (*_bfd_error_handler)
3060 ("Attempt to convert L32R/CALLX to CALL failed");
3061 return bfd_reloc_other;
3064 opcode = get_expanded_call_opcode (chbuf, content_length - address);
3065 direct_call_opcode = swap_callx_for_call_opcode (opcode);
3066 if (direct_call_opcode == XTENSA_UNDEFINED)
3068 (*_bfd_error_handler)
3069 ("Attempt to convert L32R/CALLX to CALL failed");
3070 return bfd_reloc_other;
3073 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
3074 opcode = xtensa_opcode_lookup (isa, "or");
3075 xtensa_encode_insn (isa, opcode, insnbuf);
3076 for (opn = 0; opn < 3; opn++)
3078 operand = xtensa_get_operand (isa, opcode, opn);
3079 xtensa_operand_set_field (operand, insnbuf, 1);
3081 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf);
3083 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
3084 xtensa_encode_insn (isa, direct_call_opcode, insnbuf);
3085 operand = xtensa_get_operand (isa, opcode, 0);
3086 xtensa_operand_set_field (operand, insnbuf, 0);
3087 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3);
3089 return bfd_reloc_ok;
3093 static bfd_reloc_status_type
3094 contract_asm_expansion (contents, content_length, irel)
3095 bfd_byte *contents;
3096 bfd_vma content_length;
3097 Elf_Internal_Rela *irel;
3099 bfd_reloc_status_type retval =
3100 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length);
3102 if (retval != bfd_reloc_ok)
3103 return retval;
3105 /* Update the irel->r_offset field so that the right immediate and
3106 the right instruction are modified during the relocation. */
3107 irel->r_offset += 3;
3108 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_OP0);
3109 return bfd_reloc_ok;
3113 static xtensa_opcode
3114 swap_callx_for_call_opcode (opcode)
3115 xtensa_opcode opcode;
3117 init_call_opcodes ();
3119 if (opcode == callx0_op) return call0_op;
3120 if (opcode == callx4_op) return call4_op;
3121 if (opcode == callx8_op) return call8_op;
3122 if (opcode == callx12_op) return call12_op;
3124 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
3125 return XTENSA_UNDEFINED;
3129 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" sequence, and
3130 if so, return the CALLX opcode. If not, return XTENSA_UNDEFINED. */
3132 #define L32R_TARGET_REG_OPERAND 0
3133 #define CALLN_SOURCE_OPERAND 0
3135 static xtensa_opcode
3136 get_expanded_call_opcode (buf, bufsize)
3137 bfd_byte *buf;
3138 int bufsize;
3140 static xtensa_insnbuf insnbuf = NULL;
3141 xtensa_opcode opcode;
3142 xtensa_operand operand;
3143 xtensa_isa isa = xtensa_default_isa;
3144 uint32 regno, call_regno;
3146 /* Buffer must be at least 6 bytes. */
3147 if (bufsize < 6)
3148 return XTENSA_UNDEFINED;
3150 if (insnbuf == NULL)
3151 insnbuf = xtensa_insnbuf_alloc (isa);
3153 xtensa_insnbuf_from_chars (isa, insnbuf, buf);
3154 opcode = xtensa_decode_insn (isa, insnbuf);
3156 if (opcode != get_l32r_opcode ())
3157 return XTENSA_UNDEFINED;
3159 operand = xtensa_get_operand (isa, opcode, L32R_TARGET_REG_OPERAND);
3160 regno = xtensa_operand_decode
3161 (operand, xtensa_operand_get_field (operand, insnbuf));
3163 /* Next instruction should be an CALLXn with operand 0 == regno. */
3164 xtensa_insnbuf_from_chars (isa, insnbuf,
3165 buf + xtensa_insn_length (isa, opcode));
3166 opcode = xtensa_decode_insn (isa, insnbuf);
3168 if (!is_indirect_call_opcode (opcode))
3169 return XTENSA_UNDEFINED;
3171 operand = xtensa_get_operand (isa, opcode, CALLN_SOURCE_OPERAND);
3172 call_regno = xtensa_operand_decode
3173 (operand, xtensa_operand_get_field (operand, insnbuf));
3174 if (call_regno != regno)
3175 return XTENSA_UNDEFINED;
3177 return opcode;
3181 /* Data structures used during relaxation. */
3183 /* r_reloc: relocation values. */
3185 /* Through the relaxation process, we need to keep track of the values
3186 that will result from evaluating relocations. The standard ELF
3187 relocation structure is not sufficient for this purpose because we're
3188 operating on multiple input files at once, so we need to know which
3189 input file a relocation refers to. The r_reloc structure thus
3190 records both the input file (bfd) and ELF relocation.
3192 For efficiency, an r_reloc also contains a "target_offset" field to
3193 cache the target-section-relative offset value that is represented by
3194 the relocation. */
3196 typedef struct r_reloc_struct r_reloc;
3198 struct r_reloc_struct
3200 bfd *abfd;
3201 Elf_Internal_Rela rela;
3202 bfd_vma target_offset;
3205 static bfd_boolean r_reloc_is_const
3206 PARAMS ((const r_reloc *));
3207 static void r_reloc_init
3208 PARAMS ((r_reloc *, bfd *, Elf_Internal_Rela *));
3209 static bfd_vma r_reloc_get_target_offset
3210 PARAMS ((const r_reloc *));
3211 static asection *r_reloc_get_section
3212 PARAMS ((const r_reloc *));
3213 static bfd_boolean r_reloc_is_defined
3214 PARAMS ((const r_reloc *));
3215 static struct elf_link_hash_entry *r_reloc_get_hash_entry
3216 PARAMS ((const r_reloc *));
3219 /* The r_reloc structure is included by value in literal_value, but not
3220 every literal_value has an associated relocation -- some are simple
3221 constants. In such cases, we set all the fields in the r_reloc
3222 struct to zero. The r_reloc_is_const function should be used to
3223 detect this case. */
3225 static bfd_boolean
3226 r_reloc_is_const (r_rel)
3227 const r_reloc *r_rel;
3229 return (r_rel->abfd == NULL);
3233 static void
3234 r_reloc_init (r_rel, abfd, irel)
3235 r_reloc *r_rel;
3236 bfd *abfd;
3237 Elf_Internal_Rela *irel;
3239 if (irel != NULL)
3241 r_rel->rela = *irel;
3242 r_rel->abfd = abfd;
3243 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
3245 else
3246 memset (r_rel, 0, sizeof (r_reloc));
3250 static bfd_vma
3251 r_reloc_get_target_offset (r_rel)
3252 const r_reloc *r_rel;
3254 bfd_vma target_offset;
3255 unsigned long r_symndx;
3257 BFD_ASSERT (!r_reloc_is_const (r_rel));
3258 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3259 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
3260 return (target_offset + r_rel->rela.r_addend);
3264 static struct elf_link_hash_entry *
3265 r_reloc_get_hash_entry (r_rel)
3266 const r_reloc *r_rel;
3268 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3269 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
3273 static asection *
3274 r_reloc_get_section (r_rel)
3275 const r_reloc *r_rel;
3277 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3278 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
3282 static bfd_boolean
3283 r_reloc_is_defined (r_rel)
3284 const r_reloc *r_rel;
3286 asection *sec = r_reloc_get_section (r_rel);
3287 if (sec == bfd_abs_section_ptr
3288 || sec == bfd_com_section_ptr
3289 || sec == bfd_und_section_ptr)
3290 return FALSE;
3291 return TRUE;
3295 /* source_reloc: relocations that reference literal sections. */
3297 /* To determine whether literals can be coalesced, we need to first
3298 record all the relocations that reference the literals. The
3299 source_reloc structure below is used for this purpose. The
3300 source_reloc entries are kept in a per-literal-section array, sorted
3301 by offset within the literal section (i.e., target offset).
3303 The source_sec and r_rel.rela.r_offset fields identify the source of
3304 the relocation. The r_rel field records the relocation value, i.e.,
3305 the offset of the literal being referenced. The opnd field is needed
3306 to determine the range of the immediate field to which the relocation
3307 applies, so we can determine whether another literal with the same
3308 value is within range. The is_null field is true when the relocation
3309 is being removed (e.g., when an L32R is being removed due to a CALLX
3310 that is converted to a direct CALL). */
3312 typedef struct source_reloc_struct source_reloc;
3314 struct source_reloc_struct
3316 asection *source_sec;
3317 r_reloc r_rel;
3318 xtensa_operand opnd;
3319 bfd_boolean is_null;
3323 static void init_source_reloc
3324 PARAMS ((source_reloc *, asection *, const r_reloc *, xtensa_operand));
3325 static source_reloc *find_source_reloc
3326 PARAMS ((source_reloc *, int, asection *, Elf_Internal_Rela *));
3327 static int source_reloc_compare
3328 PARAMS ((const PTR, const PTR));
3331 static void
3332 init_source_reloc (reloc, source_sec, r_rel, opnd)
3333 source_reloc *reloc;
3334 asection *source_sec;
3335 const r_reloc *r_rel;
3336 xtensa_operand opnd;
3338 reloc->source_sec = source_sec;
3339 reloc->r_rel = *r_rel;
3340 reloc->opnd = opnd;
3341 reloc->is_null = FALSE;
3345 /* Find the source_reloc for a particular source offset and relocation
3346 type. Note that the array is sorted by _target_ offset, so this is
3347 just a linear search. */
3349 static source_reloc *
3350 find_source_reloc (src_relocs, src_count, sec, irel)
3351 source_reloc *src_relocs;
3352 int src_count;
3353 asection *sec;
3354 Elf_Internal_Rela *irel;
3356 int i;
3358 for (i = 0; i < src_count; i++)
3360 if (src_relocs[i].source_sec == sec
3361 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
3362 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
3363 == ELF32_R_TYPE (irel->r_info)))
3364 return &src_relocs[i];
3367 return NULL;
3371 static int
3372 source_reloc_compare (ap, bp)
3373 const PTR ap;
3374 const PTR bp;
3376 const source_reloc *a = (const source_reloc *) ap;
3377 const source_reloc *b = (const source_reloc *) bp;
3379 return (a->r_rel.target_offset - b->r_rel.target_offset);
3383 /* Literal values and value hash tables. */
3385 /* Literals with the same value can be coalesced. The literal_value
3386 structure records the value of a literal: the "r_rel" field holds the
3387 information from the relocation on the literal (if there is one) and
3388 the "value" field holds the contents of the literal word itself.
3390 The value_map structure records a literal value along with the
3391 location of a literal holding that value. The value_map hash table
3392 is indexed by the literal value, so that we can quickly check if a
3393 particular literal value has been seen before and is thus a candidate
3394 for coalescing. */
3396 typedef struct literal_value_struct literal_value;
3397 typedef struct value_map_struct value_map;
3398 typedef struct value_map_hash_table_struct value_map_hash_table;
3400 struct literal_value_struct
3402 r_reloc r_rel;
3403 unsigned long value;
3406 struct value_map_struct
3408 literal_value val; /* The literal value. */
3409 r_reloc loc; /* Location of the literal. */
3410 value_map *next;
3413 struct value_map_hash_table_struct
3415 unsigned bucket_count;
3416 value_map **buckets;
3417 unsigned count;
3421 static bfd_boolean is_same_value
3422 PARAMS ((const literal_value *, const literal_value *, bfd_boolean));
3423 static value_map_hash_table *value_map_hash_table_init
3424 PARAMS ((void));
3425 static unsigned hash_literal_value
3426 PARAMS ((const literal_value *));
3427 static unsigned hash_bfd_vma
3428 PARAMS ((bfd_vma));
3429 static value_map *get_cached_value
3430 PARAMS ((value_map_hash_table *, const literal_value *, bfd_boolean));
3431 static value_map *add_value_map
3432 PARAMS ((value_map_hash_table *, const literal_value *, const r_reloc *,
3433 bfd_boolean));
3436 static bfd_boolean
3437 is_same_value (src1, src2, final_static_link)
3438 const literal_value *src1;
3439 const literal_value *src2;
3440 bfd_boolean final_static_link;
3442 struct elf_link_hash_entry *h1, *h2;
3444 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
3445 return FALSE;
3447 if (r_reloc_is_const (&src1->r_rel))
3448 return (src1->value == src2->value);
3450 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
3451 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
3452 return FALSE;
3454 if (r_reloc_get_target_offset (&src1->r_rel)
3455 != r_reloc_get_target_offset (&src2->r_rel))
3456 return FALSE;
3458 if (src1->value != src2->value)
3459 return FALSE;
3461 /* Now check for the same section (if defined) or the same elf_hash
3462 (if undefined or weak). */
3463 h1 = r_reloc_get_hash_entry (&src1->r_rel);
3464 h2 = r_reloc_get_hash_entry (&src2->r_rel);
3465 if (r_reloc_is_defined (&src1->r_rel)
3466 && (final_static_link
3467 || ((!h1 || h1->root.type != bfd_link_hash_defweak)
3468 && (!h2 || h2->root.type != bfd_link_hash_defweak))))
3470 if (r_reloc_get_section (&src1->r_rel)
3471 != r_reloc_get_section (&src2->r_rel))
3472 return FALSE;
3474 else
3476 /* Require that the hash entries (i.e., symbols) be identical. */
3477 if (h1 != h2 || h1 == 0)
3478 return FALSE;
3481 return TRUE;
3485 /* Must be power of 2. */
3486 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
3488 static value_map_hash_table *
3489 value_map_hash_table_init ()
3491 value_map_hash_table *values;
3493 values = (value_map_hash_table *)
3494 bfd_malloc (sizeof (value_map_hash_table));
3496 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
3497 values->count = 0;
3498 values->buckets = (value_map **)
3499 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
3501 return values;
3505 static unsigned
3506 hash_bfd_vma (val)
3507 bfd_vma val;
3509 return (val >> 2) + (val >> 10);
3513 static unsigned
3514 hash_literal_value (src)
3515 const literal_value *src;
3517 unsigned hash_val;
3519 if (r_reloc_is_const (&src->r_rel))
3520 return hash_bfd_vma (src->value);
3522 hash_val = (hash_bfd_vma (r_reloc_get_target_offset (&src->r_rel))
3523 + hash_bfd_vma (src->value));
3525 /* Now check for the same section and the same elf_hash. */
3526 if (r_reloc_is_defined (&src->r_rel))
3527 hash_val += hash_bfd_vma ((bfd_vma) (unsigned) r_reloc_get_section (&src->r_rel));
3528 else
3529 hash_val += hash_bfd_vma ((bfd_vma) (unsigned) r_reloc_get_hash_entry (&src->r_rel));
3531 return hash_val;
3535 /* Check if the specified literal_value has been seen before. */
3537 static value_map *
3538 get_cached_value (map, val, final_static_link)
3539 value_map_hash_table *map;
3540 const literal_value *val;
3541 bfd_boolean final_static_link;
3543 value_map *map_e;
3544 value_map *bucket;
3545 unsigned idx;
3547 idx = hash_literal_value (val);
3548 idx = idx & (map->bucket_count - 1);
3549 bucket = map->buckets[idx];
3550 for (map_e = bucket; map_e; map_e = map_e->next)
3552 if (is_same_value (&map_e->val, val, final_static_link))
3553 return map_e;
3555 return NULL;
3559 /* Record a new literal value. It is illegal to call this if VALUE
3560 already has an entry here. */
3562 static value_map *
3563 add_value_map (map, val, loc, final_static_link)
3564 value_map_hash_table *map;
3565 const literal_value *val;
3566 const r_reloc *loc;
3567 bfd_boolean final_static_link;
3569 value_map **bucket_p;
3570 unsigned idx;
3572 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
3574 BFD_ASSERT (get_cached_value (map, val, final_static_link) == NULL);
3575 val_e->val = *val;
3576 val_e->loc = *loc;
3578 idx = hash_literal_value (val);
3579 idx = idx & (map->bucket_count - 1);
3580 bucket_p = &map->buckets[idx];
3582 val_e->next = *bucket_p;
3583 *bucket_p = val_e;
3584 map->count++;
3585 /* FIXME: consider resizing the hash table if we get too many entries */
3587 return val_e;
3591 /* Lists of literals being coalesced or removed. */
3593 /* In the usual case, the literal identified by "from" is being
3594 coalesced with another literal identified by "to". If the literal is
3595 unused and is being removed altogether, "to.abfd" will be NULL.
3596 The removed_literal entries are kept on a per-section list, sorted
3597 by the "from" offset field. */
3599 typedef struct removed_literal_struct removed_literal;
3600 typedef struct removed_literal_list_struct removed_literal_list;
3602 struct removed_literal_struct
3604 r_reloc from;
3605 r_reloc to;
3606 removed_literal *next;
3609 struct removed_literal_list_struct
3611 removed_literal *head;
3612 removed_literal *tail;
3616 static void add_removed_literal
3617 PARAMS ((removed_literal_list *, const r_reloc *, const r_reloc *));
3618 static removed_literal *find_removed_literal
3619 PARAMS ((removed_literal_list *, bfd_vma));
3620 static bfd_vma offset_with_removed_literals
3621 PARAMS ((removed_literal_list *, bfd_vma));
3624 /* Record that the literal at "from" is being removed. If "to" is not
3625 NULL, the "from" literal is being coalesced with the "to" literal. */
3627 static void
3628 add_removed_literal (removed_list, from, to)
3629 removed_literal_list *removed_list;
3630 const r_reloc *from;
3631 const r_reloc *to;
3633 removed_literal *r, *new_r, *next_r;
3635 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
3637 new_r->from = *from;
3638 if (to)
3639 new_r->to = *to;
3640 else
3641 new_r->to.abfd = NULL;
3642 new_r->next = NULL;
3644 r = removed_list->head;
3645 if (r == NULL)
3647 removed_list->head = new_r;
3648 removed_list->tail = new_r;
3650 /* Special check for common case of append. */
3651 else if (removed_list->tail->from.target_offset < from->target_offset)
3653 removed_list->tail->next = new_r;
3654 removed_list->tail = new_r;
3656 else
3658 while (r->from.target_offset < from->target_offset
3659 && r->next != NULL)
3661 r = r->next;
3663 next_r = r->next;
3664 r->next = new_r;
3665 new_r->next = next_r;
3666 if (next_r == NULL)
3667 removed_list->tail = new_r;
3672 /* Check if the list of removed literals contains an entry for the
3673 given address. Return the entry if found. */
3675 static removed_literal *
3676 find_removed_literal (removed_list, addr)
3677 removed_literal_list *removed_list;
3678 bfd_vma addr;
3680 removed_literal *r = removed_list->head;
3681 while (r && r->from.target_offset < addr)
3682 r = r->next;
3683 if (r && r->from.target_offset == addr)
3684 return r;
3685 return NULL;
3689 /* Adjust an offset in a section to compensate for literals that are
3690 being removed. Search the list of removed literals and subtract
3691 4 bytes for every removed literal prior to the given address. */
3693 static bfd_vma
3694 offset_with_removed_literals (removed_list, addr)
3695 removed_literal_list *removed_list;
3696 bfd_vma addr;
3698 removed_literal *r = removed_list->head;
3699 unsigned num_bytes = 0;
3701 if (r == NULL)
3702 return addr;
3704 while (r && r->from.target_offset <= addr)
3706 num_bytes += 4;
3707 r = r->next;
3709 if (num_bytes > addr)
3710 return 0;
3711 return (addr - num_bytes);
3715 /* Coalescing literals may require a relocation to refer to a section in
3716 a different input file, but the standard relocation information
3717 cannot express that. Instead, the reloc_bfd_fix structures are used
3718 to "fix" the relocations that refer to sections in other input files.
3719 These structures are kept on per-section lists. The "src_type" field
3720 records the relocation type in case there are multiple relocations on
3721 the same location. FIXME: This is ugly; an alternative might be to
3722 add new symbols with the "owner" field to some other input file. */
3724 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
3726 struct reloc_bfd_fix_struct
3728 asection *src_sec;
3729 bfd_vma src_offset;
3730 unsigned src_type; /* Relocation type. */
3732 bfd *target_abfd;
3733 asection *target_sec;
3734 bfd_vma target_offset;
3736 reloc_bfd_fix *next;
3740 static reloc_bfd_fix *reloc_bfd_fix_init
3741 PARAMS ((asection *, bfd_vma, unsigned, bfd *, asection *, bfd_vma));
3742 static reloc_bfd_fix *get_bfd_fix
3743 PARAMS ((reloc_bfd_fix *, asection *, bfd_vma, unsigned));
3746 static reloc_bfd_fix *
3747 reloc_bfd_fix_init (src_sec, src_offset, src_type,
3748 target_abfd, target_sec, target_offset)
3749 asection *src_sec;
3750 bfd_vma src_offset;
3751 unsigned src_type;
3752 bfd *target_abfd;
3753 asection *target_sec;
3754 bfd_vma target_offset;
3756 reloc_bfd_fix *fix;
3758 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
3759 fix->src_sec = src_sec;
3760 fix->src_offset = src_offset;
3761 fix->src_type = src_type;
3762 fix->target_abfd = target_abfd;
3763 fix->target_sec = target_sec;
3764 fix->target_offset = target_offset;
3766 return fix;
3770 static reloc_bfd_fix *
3771 get_bfd_fix (fix_list, sec, offset, type)
3772 reloc_bfd_fix *fix_list;
3773 asection *sec;
3774 bfd_vma offset;
3775 unsigned type;
3777 reloc_bfd_fix *r;
3779 for (r = fix_list; r != NULL; r = r->next)
3781 if (r->src_sec == sec
3782 && r->src_offset == offset
3783 && r->src_type == type)
3784 return r;
3786 return NULL;
3790 /* Per-section data for relaxation. */
3792 struct xtensa_relax_info_struct
3794 bfd_boolean is_relaxable_literal_section;
3795 int visited; /* Number of times visited. */
3797 source_reloc *src_relocs; /* Array[src_count]. */
3798 int src_count;
3799 int src_next; /* Next src_relocs entry to assign. */
3801 removed_literal_list removed_list;
3803 reloc_bfd_fix *fix_list;
3806 struct elf_xtensa_section_data
3808 struct bfd_elf_section_data elf;
3809 xtensa_relax_info relax_info;
3812 static void init_xtensa_relax_info
3813 PARAMS ((asection *));
3814 static xtensa_relax_info *get_xtensa_relax_info
3815 PARAMS ((asection *));
3816 static void add_fix
3817 PARAMS ((asection *, reloc_bfd_fix *));
3820 static bfd_boolean
3821 elf_xtensa_new_section_hook (abfd, sec)
3822 bfd *abfd;
3823 asection *sec;
3825 struct elf_xtensa_section_data *sdata;
3826 bfd_size_type amt = sizeof (*sdata);
3828 sdata = (struct elf_xtensa_section_data *) bfd_zalloc (abfd, amt);
3829 if (sdata == NULL)
3830 return FALSE;
3831 sec->used_by_bfd = (PTR) sdata;
3833 return _bfd_elf_new_section_hook (abfd, sec);
3837 static void
3838 init_xtensa_relax_info (sec)
3839 asection *sec;
3841 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
3843 relax_info->is_relaxable_literal_section = FALSE;
3844 relax_info->visited = 0;
3846 relax_info->src_relocs = NULL;
3847 relax_info->src_count = 0;
3848 relax_info->src_next = 0;
3850 relax_info->removed_list.head = NULL;
3851 relax_info->removed_list.tail = NULL;
3853 relax_info->fix_list = NULL;
3857 static xtensa_relax_info *
3858 get_xtensa_relax_info (sec)
3859 asection *sec;
3861 struct elf_xtensa_section_data *section_data;
3863 /* No info available if no section or if it is an output section. */
3864 if (!sec || sec == sec->output_section)
3865 return NULL;
3867 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
3868 return &section_data->relax_info;
3872 static void
3873 add_fix (src_sec, fix)
3874 asection *src_sec;
3875 reloc_bfd_fix *fix;
3877 xtensa_relax_info *relax_info;
3879 relax_info = get_xtensa_relax_info (src_sec);
3880 fix->next = relax_info->fix_list;
3881 relax_info->fix_list = fix;
3885 /* Access to internal relocations, section contents and symbols. */
3887 /* During relaxation, we need to modify relocations, section contents,
3888 and symbol definitions, and we need to keep the original values from
3889 being reloaded from the input files, i.e., we need to "pin" the
3890 modified values in memory. We also want to continue to observe the
3891 setting of the "keep-memory" flag. The following functions wrap the
3892 standard BFD functions to take care of this for us. */
3894 static Elf_Internal_Rela *
3895 retrieve_internal_relocs (abfd, sec, keep_memory)
3896 bfd *abfd;
3897 asection *sec;
3898 bfd_boolean keep_memory;
3900 Elf_Internal_Rela *internal_relocs;
3902 if ((sec->flags & SEC_LINKER_CREATED) != 0)
3903 return NULL;
3905 internal_relocs = elf_section_data (sec)->relocs;
3906 if (internal_relocs == NULL)
3907 internal_relocs = (_bfd_elf_link_read_relocs
3908 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3909 keep_memory));
3910 return internal_relocs;
3914 static void
3915 pin_internal_relocs (sec, internal_relocs)
3916 asection *sec;
3917 Elf_Internal_Rela *internal_relocs;
3919 elf_section_data (sec)->relocs = internal_relocs;
3923 static void
3924 release_internal_relocs (sec, internal_relocs)
3925 asection *sec;
3926 Elf_Internal_Rela *internal_relocs;
3928 if (internal_relocs
3929 && elf_section_data (sec)->relocs != internal_relocs)
3930 free (internal_relocs);
3934 static bfd_byte *
3935 retrieve_contents (abfd, sec, keep_memory)
3936 bfd *abfd;
3937 asection *sec;
3938 bfd_boolean keep_memory;
3940 bfd_byte *contents;
3942 contents = elf_section_data (sec)->this_hdr.contents;
3944 if (contents == NULL && sec->size != 0)
3946 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3948 if (contents != NULL)
3949 free (contents);
3950 return NULL;
3952 if (keep_memory)
3953 elf_section_data (sec)->this_hdr.contents = contents;
3955 return contents;
3959 static void
3960 pin_contents (sec, contents)
3961 asection *sec;
3962 bfd_byte *contents;
3964 elf_section_data (sec)->this_hdr.contents = contents;
3968 static void
3969 release_contents (sec, contents)
3970 asection *sec;
3971 bfd_byte *contents;
3973 if (contents &&
3974 elf_section_data (sec)->this_hdr.contents != contents)
3975 free (contents);
3979 static Elf_Internal_Sym *
3980 retrieve_local_syms (input_bfd)
3981 bfd *input_bfd;
3983 Elf_Internal_Shdr *symtab_hdr;
3984 Elf_Internal_Sym *isymbuf;
3985 size_t locsymcount;
3987 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3988 locsymcount = symtab_hdr->sh_info;
3990 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3991 if (isymbuf == NULL && locsymcount != 0)
3992 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
3993 NULL, NULL, NULL);
3995 /* Save the symbols for this input file so they won't be read again. */
3996 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
3997 symtab_hdr->contents = (unsigned char *) isymbuf;
3999 return isymbuf;
4003 /* Code for link-time relaxation. */
4005 /* Local helper functions. */
4006 static bfd_boolean analyze_relocations
4007 PARAMS ((struct bfd_link_info *));
4008 static bfd_boolean find_relaxable_sections
4009 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
4010 static bfd_boolean collect_source_relocs
4011 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4012 static bfd_boolean is_resolvable_asm_expansion
4013 PARAMS ((bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
4014 struct bfd_link_info *, bfd_boolean *));
4015 static bfd_boolean remove_literals
4016 PARAMS ((bfd *, asection *, struct bfd_link_info *, value_map_hash_table *));
4017 static bfd_boolean relax_section
4018 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4019 static bfd_boolean relax_property_section
4020 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4021 static bfd_boolean relax_section_symbols
4022 PARAMS ((bfd *, asection *));
4023 static bfd_boolean relocations_reach
4024 PARAMS ((source_reloc *, int, const r_reloc *));
4025 static void translate_reloc
4026 PARAMS ((const r_reloc *, r_reloc *));
4027 static Elf_Internal_Rela *get_irel_at_offset
4028 PARAMS ((asection *, Elf_Internal_Rela *, bfd_vma));
4029 static Elf_Internal_Rela *find_associated_l32r_irel
4030 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *,
4031 Elf_Internal_Rela *));
4032 static void shrink_dynamic_reloc_sections
4033 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *));
4036 static bfd_boolean
4037 elf_xtensa_relax_section (abfd, sec, link_info, again)
4038 bfd *abfd;
4039 asection *sec;
4040 struct bfd_link_info *link_info;
4041 bfd_boolean *again;
4043 static value_map_hash_table *values = NULL;
4044 xtensa_relax_info *relax_info;
4046 if (!values)
4048 /* Do some overall initialization for relaxation. */
4049 values = value_map_hash_table_init ();
4050 relaxing_section = TRUE;
4051 if (!analyze_relocations (link_info))
4052 return FALSE;
4054 *again = FALSE;
4056 /* Don't mess with linker-created sections. */
4057 if ((sec->flags & SEC_LINKER_CREATED) != 0)
4058 return TRUE;
4060 relax_info = get_xtensa_relax_info (sec);
4061 BFD_ASSERT (relax_info != NULL);
4063 switch (relax_info->visited)
4065 case 0:
4066 /* Note: It would be nice to fold this pass into
4067 analyze_relocations, but it is important for this step that the
4068 sections be examined in link order. */
4069 if (!remove_literals (abfd, sec, link_info, values))
4070 return FALSE;
4071 *again = TRUE;
4072 break;
4074 case 1:
4075 if (!relax_section (abfd, sec, link_info))
4076 return FALSE;
4077 *again = TRUE;
4078 break;
4080 case 2:
4081 if (!relax_section_symbols (abfd, sec))
4082 return FALSE;
4083 break;
4086 relax_info->visited++;
4087 return TRUE;
4090 /* Initialization for relaxation. */
4092 /* This function is called once at the start of relaxation. It scans
4093 all the input sections and marks the ones that are relaxable (i.e.,
4094 literal sections with L32R relocations against them). It then
4095 collect source_reloc information for all the relocations against
4096 those relaxable sections. */
4098 static bfd_boolean
4099 analyze_relocations (link_info)
4100 struct bfd_link_info *link_info;
4102 bfd *abfd;
4103 asection *sec;
4104 bfd_boolean is_relaxable = FALSE;
4106 /* Initialize the per-section relaxation info. */
4107 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4108 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4110 init_xtensa_relax_info (sec);
4113 /* Mark relaxable sections (and count relocations against each one). */
4114 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4115 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4117 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
4118 return FALSE;
4121 /* Bail out if there are no relaxable sections. */
4122 if (!is_relaxable)
4123 return TRUE;
4125 /* Allocate space for source_relocs. */
4126 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4127 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4129 xtensa_relax_info *relax_info;
4131 relax_info = get_xtensa_relax_info (sec);
4132 if (relax_info->is_relaxable_literal_section)
4134 relax_info->src_relocs = (source_reloc *)
4135 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
4139 /* Collect info on relocations against each relaxable section. */
4140 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4141 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4143 if (!collect_source_relocs (abfd, sec, link_info))
4144 return FALSE;
4147 return TRUE;
4151 /* Find all the literal sections that might be relaxed. The motivation
4152 for this pass is that collect_source_relocs() needs to record _all_
4153 the relocations that target each relaxable section. That is
4154 expensive and unnecessary unless the target section is actually going
4155 to be relaxed. This pass identifies all such sections by checking if
4156 they have L32Rs pointing to them. In the process, the total number
4157 of relocations targeting each section is also counted so that we
4158 know how much space to allocate for source_relocs against each
4159 relaxable literal section. */
4161 static bfd_boolean
4162 find_relaxable_sections (abfd, sec, link_info, is_relaxable_p)
4163 bfd *abfd;
4164 asection *sec;
4165 struct bfd_link_info *link_info;
4166 bfd_boolean *is_relaxable_p;
4168 Elf_Internal_Rela *internal_relocs;
4169 bfd_byte *contents;
4170 bfd_boolean ok = TRUE;
4171 unsigned i;
4173 internal_relocs = retrieve_internal_relocs (abfd, sec,
4174 link_info->keep_memory);
4175 if (internal_relocs == NULL)
4176 return ok;
4178 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4179 if (contents == NULL && sec->size != 0)
4181 ok = FALSE;
4182 goto error_return;
4185 for (i = 0; i < sec->reloc_count; i++)
4187 Elf_Internal_Rela *irel = &internal_relocs[i];
4188 r_reloc r_rel;
4189 asection *target_sec;
4190 xtensa_relax_info *target_relax_info;
4192 r_reloc_init (&r_rel, abfd, irel);
4194 target_sec = r_reloc_get_section (&r_rel);
4195 target_relax_info = get_xtensa_relax_info (target_sec);
4196 if (!target_relax_info)
4197 continue;
4199 /* Count relocations against the target section. */
4200 target_relax_info->src_count++;
4202 if (is_literal_section (target_sec)
4203 && is_l32r_relocation (sec, contents, irel)
4204 && r_reloc_is_defined (&r_rel))
4206 /* Mark the target section as relaxable. */
4207 target_relax_info->is_relaxable_literal_section = TRUE;
4208 *is_relaxable_p = TRUE;
4212 error_return:
4213 release_contents (sec, contents);
4214 release_internal_relocs (sec, internal_relocs);
4215 return ok;
4219 /* Record _all_ the relocations that point to relaxable literal
4220 sections, and get rid of ASM_EXPAND relocs by either converting them
4221 to ASM_SIMPLIFY or by removing them. */
4223 static bfd_boolean
4224 collect_source_relocs (abfd, sec, link_info)
4225 bfd *abfd;
4226 asection *sec;
4227 struct bfd_link_info *link_info;
4229 Elf_Internal_Rela *internal_relocs;
4230 bfd_byte *contents;
4231 bfd_boolean ok = TRUE;
4232 unsigned i;
4234 internal_relocs = retrieve_internal_relocs (abfd, sec,
4235 link_info->keep_memory);
4236 if (internal_relocs == NULL)
4237 return ok;
4239 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4240 if (contents == NULL && sec->size != 0)
4242 ok = FALSE;
4243 goto error_return;
4246 /* Record relocations against relaxable literal sections. */
4247 for (i = 0; i < sec->reloc_count; i++)
4249 Elf_Internal_Rela *irel = &internal_relocs[i];
4250 r_reloc r_rel;
4251 asection *target_sec;
4252 xtensa_relax_info *target_relax_info;
4254 r_reloc_init (&r_rel, abfd, irel);
4256 target_sec = r_reloc_get_section (&r_rel);
4257 target_relax_info = get_xtensa_relax_info (target_sec);
4259 if (target_relax_info
4260 && target_relax_info->is_relaxable_literal_section)
4262 xtensa_opcode opcode;
4263 xtensa_operand opnd;
4264 source_reloc *s_reloc;
4265 int src_next;
4267 src_next = target_relax_info->src_next++;
4268 s_reloc = &target_relax_info->src_relocs[src_next];
4270 opcode = get_relocation_opcode (sec, contents, irel);
4271 if (opcode == XTENSA_UNDEFINED)
4272 opnd = NULL;
4273 else
4274 opnd = xtensa_get_operand (xtensa_default_isa, opcode,
4275 get_relocation_opnd (irel));
4277 init_source_reloc (s_reloc, sec, &r_rel, opnd);
4281 /* Now get rid of ASM_EXPAND relocations. At this point, the
4282 src_relocs array for the target literal section may still be
4283 incomplete, but it must at least contain the entries for the L32R
4284 relocations associated with ASM_EXPANDs because they were just
4285 added in the preceding loop over the relocations. */
4287 for (i = 0; i < sec->reloc_count; i++)
4289 Elf_Internal_Rela *irel = &internal_relocs[i];
4290 bfd_boolean is_reachable;
4292 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4293 &is_reachable))
4294 continue;
4296 if (is_reachable)
4298 Elf_Internal_Rela *l32r_irel;
4299 r_reloc r_rel;
4300 asection *target_sec;
4301 xtensa_relax_info *target_relax_info;
4303 /* Mark the source_reloc for the L32R so that it will be
4304 removed in remove_literals(), along with the associated
4305 literal. */
4306 l32r_irel = find_associated_l32r_irel (sec, contents,
4307 irel, internal_relocs);
4308 if (l32r_irel == NULL)
4309 continue;
4311 r_reloc_init (&r_rel, abfd, l32r_irel);
4313 target_sec = r_reloc_get_section (&r_rel);
4314 target_relax_info = get_xtensa_relax_info (target_sec);
4316 if (target_relax_info
4317 && target_relax_info->is_relaxable_literal_section)
4319 source_reloc *s_reloc;
4321 /* Search the source_relocs for the entry corresponding to
4322 the l32r_irel. Note: The src_relocs array is not yet
4323 sorted, but it wouldn't matter anyway because we're
4324 searching by source offset instead of target offset. */
4325 s_reloc = find_source_reloc (target_relax_info->src_relocs,
4326 target_relax_info->src_next,
4327 sec, l32r_irel);
4328 BFD_ASSERT (s_reloc);
4329 s_reloc->is_null = TRUE;
4332 /* Convert this reloc to ASM_SIMPLIFY. */
4333 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4334 R_XTENSA_ASM_SIMPLIFY);
4335 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4337 pin_internal_relocs (sec, internal_relocs);
4339 else
4341 /* It is resolvable but doesn't reach. We resolve now
4342 by eliminating the relocation -- the call will remain
4343 expanded into L32R/CALLX. */
4344 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4345 pin_internal_relocs (sec, internal_relocs);
4349 error_return:
4350 release_contents (sec, contents);
4351 release_internal_relocs (sec, internal_relocs);
4352 return ok;
4356 /* Return TRUE if the asm expansion can be resolved. Generally it can
4357 be resolved on a final link or when a partial link locates it in the
4358 same section as the target. Set "is_reachable" flag if the target of
4359 the call is within the range of a direct call, given the current VMA
4360 for this section and the target section. */
4362 bfd_boolean
4363 is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4364 is_reachable_p)
4365 bfd *abfd;
4366 asection *sec;
4367 bfd_byte *contents;
4368 Elf_Internal_Rela *irel;
4369 struct bfd_link_info *link_info;
4370 bfd_boolean *is_reachable_p;
4372 asection *target_sec;
4373 bfd_vma target_offset;
4374 r_reloc r_rel;
4375 xtensa_opcode opcode, direct_call_opcode;
4376 bfd_vma self_address;
4377 bfd_vma dest_address;
4379 *is_reachable_p = FALSE;
4381 if (contents == NULL)
4382 return FALSE;
4384 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
4385 return FALSE;
4387 opcode = get_expanded_call_opcode (contents + irel->r_offset,
4388 sec->size - irel->r_offset);
4390 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4391 if (direct_call_opcode == XTENSA_UNDEFINED)
4392 return FALSE;
4394 /* Check and see that the target resolves. */
4395 r_reloc_init (&r_rel, abfd, irel);
4396 if (!r_reloc_is_defined (&r_rel))
4397 return FALSE;
4399 target_sec = r_reloc_get_section (&r_rel);
4400 target_offset = r_reloc_get_target_offset (&r_rel);
4402 /* If the target is in a shared library, then it doesn't reach. This
4403 isn't supposed to come up because the compiler should never generate
4404 non-PIC calls on systems that use shared libraries, but the linker
4405 shouldn't crash regardless. */
4406 if (!target_sec->output_section)
4407 return FALSE;
4409 /* For relocatable sections, we can only simplify when the output
4410 section of the target is the same as the output section of the
4411 source. */
4412 if (link_info->relocatable
4413 && (target_sec->output_section != sec->output_section))
4414 return FALSE;
4416 self_address = (sec->output_section->vma
4417 + sec->output_offset + irel->r_offset + 3);
4418 dest_address = (target_sec->output_section->vma
4419 + target_sec->output_offset + target_offset);
4421 *is_reachable_p = pcrel_reloc_fits
4422 (xtensa_get_operand (xtensa_default_isa, direct_call_opcode, 0),
4423 self_address, dest_address);
4425 if ((self_address >> CALL_SEGMENT_BITS) !=
4426 (dest_address >> CALL_SEGMENT_BITS))
4427 return FALSE;
4429 return TRUE;
4433 static Elf_Internal_Rela *
4434 find_associated_l32r_irel (sec, contents, other_irel, internal_relocs)
4435 asection *sec;
4436 bfd_byte *contents;
4437 Elf_Internal_Rela *other_irel;
4438 Elf_Internal_Rela *internal_relocs;
4440 unsigned i;
4442 for (i = 0; i < sec->reloc_count; i++)
4444 Elf_Internal_Rela *irel = &internal_relocs[i];
4446 if (irel == other_irel)
4447 continue;
4448 if (irel->r_offset != other_irel->r_offset)
4449 continue;
4450 if (is_l32r_relocation (sec, contents, irel))
4451 return irel;
4454 return NULL;
4457 /* First relaxation pass. */
4459 /* If the section is relaxable (i.e., a literal section), check each
4460 literal to see if it has the same value as another literal that has
4461 already been seen, either in the current section or a previous one.
4462 If so, add an entry to the per-section list of removed literals. The
4463 actual changes are deferred until the next pass. */
4465 static bfd_boolean
4466 remove_literals (abfd, sec, link_info, values)
4467 bfd *abfd;
4468 asection *sec;
4469 struct bfd_link_info *link_info;
4470 value_map_hash_table *values;
4472 xtensa_relax_info *relax_info;
4473 bfd_byte *contents;
4474 Elf_Internal_Rela *internal_relocs;
4475 source_reloc *src_relocs;
4476 bfd_boolean final_static_link;
4477 bfd_boolean ok = TRUE;
4478 int i;
4480 /* Do nothing if it is not a relaxable literal section. */
4481 relax_info = get_xtensa_relax_info (sec);
4482 BFD_ASSERT (relax_info);
4484 if (!relax_info->is_relaxable_literal_section)
4485 return ok;
4487 internal_relocs = retrieve_internal_relocs (abfd, sec,
4488 link_info->keep_memory);
4490 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4491 if (contents == NULL && sec->size != 0)
4493 ok = FALSE;
4494 goto error_return;
4497 final_static_link =
4498 (!link_info->relocatable
4499 && !elf_hash_table (link_info)->dynamic_sections_created);
4501 /* Sort the source_relocs by target offset. */
4502 src_relocs = relax_info->src_relocs;
4503 qsort (src_relocs, relax_info->src_count,
4504 sizeof (source_reloc), source_reloc_compare);
4506 for (i = 0; i < relax_info->src_count; i++)
4508 source_reloc *rel;
4509 Elf_Internal_Rela *irel = NULL;
4510 literal_value val;
4511 value_map *val_map;
4513 rel = &src_relocs[i];
4514 irel = get_irel_at_offset (sec, internal_relocs,
4515 rel->r_rel.target_offset);
4517 /* If the target_offset for this relocation is the same as the
4518 previous relocation, then we've already considered whether the
4519 literal can be coalesced. Skip to the next one.... */
4520 if (i != 0 && (src_relocs[i-1].r_rel.target_offset
4521 == rel->r_rel.target_offset))
4522 continue;
4524 /* Check if the relocation was from an L32R that is being removed
4525 because a CALLX was converted to a direct CALL, and check if
4526 there are no other relocations to the literal. */
4527 if (rel->is_null
4528 && (i == relax_info->src_count - 1
4529 || (src_relocs[i+1].r_rel.target_offset
4530 != rel->r_rel.target_offset)))
4532 /* Mark the unused literal so that it will be removed. */
4533 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
4535 /* Zero out the relocation on this literal location. */
4536 if (irel)
4538 if (elf_hash_table (link_info)->dynamic_sections_created)
4539 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4541 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4544 continue;
4547 /* Find the literal value. */
4548 r_reloc_init (&val.r_rel, abfd, irel);
4549 BFD_ASSERT (rel->r_rel.target_offset < sec->size);
4550 val.value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
4552 /* Check if we've seen another literal with the same value. */
4553 val_map = get_cached_value (values, &val, final_static_link);
4554 if (val_map != NULL)
4556 /* First check that THIS and all the other relocs to this
4557 literal will FIT if we move them to the new address. */
4559 if (relocations_reach (rel, relax_info->src_count - i,
4560 &val_map->loc))
4562 /* Mark that the literal will be coalesced. */
4563 add_removed_literal (&relax_info->removed_list,
4564 &rel->r_rel, &val_map->loc);
4566 else
4568 /* Relocations do not reach -- do not remove this literal. */
4569 val_map->loc = rel->r_rel;
4572 else
4574 /* This is the first time we've seen this literal value. */
4575 BFD_ASSERT (sec == r_reloc_get_section (&rel->r_rel));
4576 add_value_map (values, &val, &rel->r_rel, final_static_link);
4580 error_return:
4581 release_contents (sec, contents);
4582 release_internal_relocs (sec, internal_relocs);
4583 return ok;
4587 /* Check if the original relocations (presumably on L32R instructions)
4588 identified by reloc[0..N] can be changed to reference the literal
4589 identified by r_rel. If r_rel is out of range for any of the
4590 original relocations, then we don't want to coalesce the original
4591 literal with the one at r_rel. We only check reloc[0..N], where the
4592 offsets are all the same as for reloc[0] (i.e., they're all
4593 referencing the same literal) and where N is also bounded by the
4594 number of remaining entries in the "reloc" array. The "reloc" array
4595 is sorted by target offset so we know all the entries for the same
4596 literal will be contiguous. */
4598 static bfd_boolean
4599 relocations_reach (reloc, remaining_relocs, r_rel)
4600 source_reloc *reloc;
4601 int remaining_relocs;
4602 const r_reloc *r_rel;
4604 bfd_vma from_offset, source_address, dest_address;
4605 asection *sec;
4606 int i;
4608 if (!r_reloc_is_defined (r_rel))
4609 return FALSE;
4611 sec = r_reloc_get_section (r_rel);
4612 from_offset = reloc[0].r_rel.target_offset;
4614 for (i = 0; i < remaining_relocs; i++)
4616 if (reloc[i].r_rel.target_offset != from_offset)
4617 break;
4619 /* Ignore relocations that have been removed. */
4620 if (reloc[i].is_null)
4621 continue;
4623 /* The original and new output section for these must be the same
4624 in order to coalesce. */
4625 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
4626 != sec->output_section)
4627 return FALSE;
4629 /* A NULL operand means it is not a PC-relative relocation, so
4630 the literal can be moved anywhere. */
4631 if (reloc[i].opnd)
4633 /* Otherwise, check to see that it fits. */
4634 source_address = (reloc[i].source_sec->output_section->vma
4635 + reloc[i].source_sec->output_offset
4636 + reloc[i].r_rel.rela.r_offset);
4637 dest_address = (sec->output_section->vma
4638 + sec->output_offset
4639 + r_rel->target_offset);
4641 if (!pcrel_reloc_fits (reloc[i].opnd, source_address, dest_address))
4642 return FALSE;
4646 return TRUE;
4650 /* WARNING: linear search here. If the relocation are in order by
4651 address, we can use a faster binary search. ALSO, we assume that
4652 there is only 1 non-NONE relocation per address. */
4654 static Elf_Internal_Rela *
4655 get_irel_at_offset (sec, internal_relocs, offset)
4656 asection *sec;
4657 Elf_Internal_Rela *internal_relocs;
4658 bfd_vma offset;
4660 unsigned i;
4661 if (!internal_relocs)
4662 return NULL;
4663 for (i = 0; i < sec->reloc_count; i++)
4665 Elf_Internal_Rela *irel = &internal_relocs[i];
4666 if (irel->r_offset == offset
4667 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
4668 return irel;
4670 return NULL;
4674 /* Second relaxation pass. */
4676 /* Modify all of the relocations to point to the right spot, and if this
4677 is a relaxable section, delete the unwanted literals and fix the
4678 cooked_size. */
4680 bfd_boolean
4681 relax_section (abfd, sec, link_info)
4682 bfd *abfd;
4683 asection *sec;
4684 struct bfd_link_info *link_info;
4686 Elf_Internal_Rela *internal_relocs;
4687 xtensa_relax_info *relax_info;
4688 bfd_byte *contents;
4689 bfd_boolean ok = TRUE;
4690 unsigned i;
4692 relax_info = get_xtensa_relax_info (sec);
4693 BFD_ASSERT (relax_info);
4695 /* Handle property sections (e.g., literal tables) specially. */
4696 if (xtensa_is_property_section (sec))
4698 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
4699 return relax_property_section (abfd, sec, link_info);
4702 internal_relocs = retrieve_internal_relocs (abfd, sec,
4703 link_info->keep_memory);
4704 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4705 if (contents == NULL && sec->size != 0)
4707 ok = FALSE;
4708 goto error_return;
4711 if (internal_relocs)
4713 for (i = 0; i < sec->reloc_count; i++)
4715 Elf_Internal_Rela *irel;
4716 xtensa_relax_info *target_relax_info;
4717 bfd_vma source_offset;
4718 r_reloc r_rel;
4719 unsigned r_type;
4720 asection *target_sec;
4722 /* Locally change the source address.
4723 Translate the target to the new target address.
4724 If it points to this section and has been removed,
4725 NULLify it.
4726 Write it back. */
4728 irel = &internal_relocs[i];
4729 source_offset = irel->r_offset;
4731 r_type = ELF32_R_TYPE (irel->r_info);
4732 r_reloc_init (&r_rel, abfd, irel);
4734 if (relax_info->is_relaxable_literal_section)
4736 if (r_type != R_XTENSA_NONE
4737 && find_removed_literal (&relax_info->removed_list,
4738 irel->r_offset))
4740 /* Remove this relocation. */
4741 if (elf_hash_table (link_info)->dynamic_sections_created)
4742 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4743 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4744 irel->r_offset = offset_with_removed_literals
4745 (&relax_info->removed_list, irel->r_offset);
4746 continue;
4748 source_offset =
4749 offset_with_removed_literals (&relax_info->removed_list,
4750 irel->r_offset);
4751 irel->r_offset = source_offset;
4754 target_sec = r_reloc_get_section (&r_rel);
4755 target_relax_info = get_xtensa_relax_info (target_sec);
4757 if (target_relax_info
4758 && target_relax_info->is_relaxable_literal_section)
4760 r_reloc new_rel;
4761 reloc_bfd_fix *fix;
4763 translate_reloc (&r_rel, &new_rel);
4765 /* FIXME: If the relocation still references a section in
4766 the same input file, the relocation should be modified
4767 directly instead of adding a "fix" record. */
4769 fix = reloc_bfd_fix_init (sec, source_offset, r_type, 0,
4770 r_reloc_get_section (&new_rel),
4771 new_rel.target_offset);
4772 add_fix (sec, fix);
4775 pin_internal_relocs (sec, internal_relocs);
4779 if (relax_info->is_relaxable_literal_section)
4781 /* Walk through the contents and delete literals that are not needed
4782 anymore. */
4784 unsigned long size = sec->size;
4785 unsigned long removed = 0;
4787 removed_literal *reloc = relax_info->removed_list.head;
4788 for (; reloc; reloc = reloc->next)
4790 unsigned long upper = sec->size;
4791 bfd_vma start = reloc->from.target_offset + 4;
4792 if (reloc->next)
4793 upper = reloc->next->from.target_offset;
4794 if (upper - start != 0)
4796 BFD_ASSERT (start <= upper);
4797 memmove (contents + start - removed - 4,
4798 contents + start,
4799 upper - start );
4800 pin_contents (sec, contents);
4802 removed += 4;
4803 size -= 4;
4806 /* Change the section size. */
4807 sec->size = size;
4810 error_return:
4811 release_internal_relocs (sec, internal_relocs);
4812 release_contents (sec, contents);
4813 return ok;
4817 /* Fix up a relocation to take account of removed literals. */
4819 static void
4820 translate_reloc (orig_rel, new_rel)
4821 const r_reloc *orig_rel;
4822 r_reloc *new_rel;
4824 asection *sec;
4825 xtensa_relax_info *relax_info;
4826 removed_literal *removed;
4827 unsigned long new_offset;
4829 *new_rel = *orig_rel;
4831 if (!r_reloc_is_defined (orig_rel))
4832 return;
4833 sec = r_reloc_get_section (orig_rel);
4835 relax_info = get_xtensa_relax_info (sec);
4836 BFD_ASSERT (relax_info);
4838 if (!relax_info->is_relaxable_literal_section)
4839 return;
4841 /* Check if the original relocation is against a literal being removed. */
4842 removed = find_removed_literal (&relax_info->removed_list,
4843 orig_rel->target_offset);
4844 if (removed)
4846 asection *new_sec;
4848 /* The fact that there is still a relocation to this literal indicates
4849 that the literal is being coalesced, not simply removed. */
4850 BFD_ASSERT (removed->to.abfd != NULL);
4852 /* This was moved to some other address (possibly in another section). */
4853 *new_rel = removed->to;
4854 new_sec = r_reloc_get_section (new_rel);
4855 if (new_sec != sec)
4857 sec = new_sec;
4858 relax_info = get_xtensa_relax_info (sec);
4859 if (!relax_info || !relax_info->is_relaxable_literal_section)
4860 return;
4864 /* ...and the target address may have been moved within its section. */
4865 new_offset = offset_with_removed_literals (&relax_info->removed_list,
4866 new_rel->target_offset);
4868 /* Modify the offset and addend. */
4869 new_rel->target_offset = new_offset;
4870 new_rel->rela.r_addend += (new_offset - new_rel->target_offset);
4874 /* For dynamic links, there may be a dynamic relocation for each
4875 literal. The number of dynamic relocations must be computed in
4876 size_dynamic_sections, which occurs before relaxation. When a
4877 literal is removed, this function checks if there is a corresponding
4878 dynamic relocation and shrinks the size of the appropriate dynamic
4879 relocation section accordingly. At this point, the contents of the
4880 dynamic relocation sections have not yet been filled in, so there's
4881 nothing else that needs to be done. */
4883 static void
4884 shrink_dynamic_reloc_sections (info, abfd, input_section, rel)
4885 struct bfd_link_info *info;
4886 bfd *abfd;
4887 asection *input_section;
4888 Elf_Internal_Rela *rel;
4890 Elf_Internal_Shdr *symtab_hdr;
4891 struct elf_link_hash_entry **sym_hashes;
4892 unsigned long r_symndx;
4893 int r_type;
4894 struct elf_link_hash_entry *h;
4895 bfd_boolean dynamic_symbol;
4897 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4898 sym_hashes = elf_sym_hashes (abfd);
4900 r_type = ELF32_R_TYPE (rel->r_info);
4901 r_symndx = ELF32_R_SYM (rel->r_info);
4903 if (r_symndx < symtab_hdr->sh_info)
4904 h = NULL;
4905 else
4906 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4908 dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
4910 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
4911 && (input_section->flags & SEC_ALLOC) != 0
4912 && (dynamic_symbol || info->shared))
4914 bfd *dynobj;
4915 const char *srel_name;
4916 asection *srel;
4917 bfd_boolean is_plt = FALSE;
4919 dynobj = elf_hash_table (info)->dynobj;
4920 BFD_ASSERT (dynobj != NULL);
4922 if (dynamic_symbol && r_type == R_XTENSA_PLT)
4924 srel_name = ".rela.plt";
4925 is_plt = TRUE;
4927 else
4928 srel_name = ".rela.got";
4930 /* Reduce size of the .rela.* section by one reloc. */
4931 srel = bfd_get_section_by_name (dynobj, srel_name);
4932 BFD_ASSERT (srel != NULL);
4933 BFD_ASSERT (srel->size >= sizeof (Elf32_External_Rela));
4934 srel->size -= sizeof (Elf32_External_Rela);
4936 if (is_plt)
4938 asection *splt, *sgotplt, *srelgot;
4939 int reloc_index, chunk;
4941 /* Find the PLT reloc index of the entry being removed. This
4942 is computed from the size of ".rela.plt". It is needed to
4943 figure out which PLT chunk to resize. Usually "last index
4944 = size - 1" since the index starts at zero, but in this
4945 context, the size has just been decremented so there's no
4946 need to subtract one. */
4947 reloc_index = srel->size / sizeof (Elf32_External_Rela);
4949 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
4950 splt = elf_xtensa_get_plt_section (dynobj, chunk);
4951 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
4952 BFD_ASSERT (splt != NULL && sgotplt != NULL);
4954 /* Check if an entire PLT chunk has just been eliminated. */
4955 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
4957 /* The two magic GOT entries for that chunk can go away. */
4958 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4959 BFD_ASSERT (srelgot != NULL);
4960 srelgot->reloc_count -= 2;
4961 srelgot->size -= 2 * sizeof (Elf32_External_Rela);
4962 sgotplt->size -= 8;
4964 /* There should be only one entry left (and it will be
4965 removed below). */
4966 BFD_ASSERT (sgotplt->size == 4);
4967 BFD_ASSERT (splt->size == PLT_ENTRY_SIZE);
4970 BFD_ASSERT (sgotplt->size >= 4);
4971 BFD_ASSERT (splt->size >= PLT_ENTRY_SIZE);
4973 sgotplt->size -= 4;
4974 splt->size -= PLT_ENTRY_SIZE;
4980 /* This is similar to relax_section except that when a target is moved,
4981 we shift addresses up. We also need to modify the size. This
4982 algorithm does NOT allow for relocations into the middle of the
4983 property sections. */
4985 static bfd_boolean
4986 relax_property_section (abfd, sec, link_info)
4987 bfd *abfd;
4988 asection *sec;
4989 struct bfd_link_info *link_info;
4991 Elf_Internal_Rela *internal_relocs;
4992 bfd_byte *contents;
4993 unsigned i, nexti;
4994 bfd_boolean ok = TRUE;
4996 internal_relocs = retrieve_internal_relocs (abfd, sec,
4997 link_info->keep_memory);
4998 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4999 if (contents == NULL && sec->size != 0)
5001 ok = FALSE;
5002 goto error_return;
5005 if (internal_relocs)
5007 for (i = 0; i < sec->reloc_count; i++)
5009 Elf_Internal_Rela *irel;
5010 xtensa_relax_info *target_relax_info;
5011 r_reloc r_rel;
5012 unsigned r_type;
5013 asection *target_sec;
5015 /* Locally change the source address.
5016 Translate the target to the new target address.
5017 If it points to this section and has been removed, MOVE IT.
5018 Also, don't forget to modify the associated SIZE at
5019 (offset + 4). */
5021 irel = &internal_relocs[i];
5022 r_type = ELF32_R_TYPE (irel->r_info);
5023 if (r_type == R_XTENSA_NONE)
5024 continue;
5026 r_reloc_init (&r_rel, abfd, irel);
5028 target_sec = r_reloc_get_section (&r_rel);
5029 target_relax_info = get_xtensa_relax_info (target_sec);
5031 if (target_relax_info
5032 && target_relax_info->is_relaxable_literal_section)
5034 /* Translate the relocation's destination. */
5035 bfd_vma new_offset;
5036 bfd_vma new_end_offset;
5037 bfd_byte *size_p;
5038 long old_size, new_size;
5040 new_offset =
5041 offset_with_removed_literals (&target_relax_info->removed_list,
5042 r_rel.target_offset);
5044 /* Assert that we are not out of bounds. */
5045 size_p = &contents[irel->r_offset + 4];
5046 old_size = bfd_get_32 (abfd, &contents[irel->r_offset + 4]);
5048 new_end_offset =
5049 offset_with_removed_literals (&target_relax_info->removed_list,
5050 r_rel.target_offset + old_size);
5052 new_size = new_end_offset - new_offset;
5053 if (new_size != old_size)
5055 bfd_put_32 (abfd, new_size, size_p);
5056 pin_contents (sec, contents);
5059 if (new_offset != r_rel.target_offset)
5061 bfd_vma diff = new_offset - r_rel.target_offset;
5062 irel->r_addend += diff;
5063 pin_internal_relocs (sec, internal_relocs);
5069 /* Combine adjacent property table entries. This is also done in
5070 finish_dynamic_sections() but at that point it's too late to
5071 reclaim the space in the output section, so we do this twice. */
5073 if (internal_relocs)
5075 Elf_Internal_Rela *last_irel = NULL;
5076 int removed_bytes = 0;
5077 bfd_vma offset, last_irel_offset;
5078 bfd_vma section_size;
5080 /* Walk over memory and irels at the same time.
5081 This REQUIRES that the internal_relocs be sorted by offset. */
5082 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
5083 internal_reloc_compare);
5084 nexti = 0; /* Index into internal_relocs. */
5086 pin_internal_relocs (sec, internal_relocs);
5087 pin_contents (sec, contents);
5089 last_irel_offset = (bfd_vma) -1;
5090 section_size = sec->size;
5091 BFD_ASSERT (section_size % 8 == 0);
5093 for (offset = 0; offset < section_size; offset += 8)
5095 Elf_Internal_Rela *irel, *next_irel;
5096 bfd_vma bytes_to_remove, size, actual_offset;
5097 bfd_boolean remove_this_irel;
5099 irel = NULL;
5100 next_irel = NULL;
5102 /* Find the next two relocations (if there are that many left),
5103 skipping over any R_XTENSA_NONE relocs. On entry, "nexti" is
5104 the starting reloc index. After these two loops, "i"
5105 is the index of the first non-NONE reloc past that starting
5106 index, and "nexti" is the index for the next non-NONE reloc
5107 after "i". */
5109 for (i = nexti; i < sec->reloc_count; i++)
5111 if (ELF32_R_TYPE (internal_relocs[i].r_info) != R_XTENSA_NONE)
5113 irel = &internal_relocs[i];
5114 break;
5116 internal_relocs[i].r_offset -= removed_bytes;
5119 for (nexti = i + 1; nexti < sec->reloc_count; nexti++)
5121 if (ELF32_R_TYPE (internal_relocs[nexti].r_info)
5122 != R_XTENSA_NONE)
5124 next_irel = &internal_relocs[nexti];
5125 break;
5127 internal_relocs[nexti].r_offset -= removed_bytes;
5130 remove_this_irel = FALSE;
5131 bytes_to_remove = 0;
5132 actual_offset = offset - removed_bytes;
5133 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
5135 /* Check that the irels are sorted by offset,
5136 with only one per address. */
5137 BFD_ASSERT (!irel || (int) irel->r_offset > (int) last_irel_offset);
5138 BFD_ASSERT (!next_irel || next_irel->r_offset > irel->r_offset);
5140 /* Make sure there isn't a reloc on the size field. */
5141 if (irel && irel->r_offset == offset + 4)
5143 irel->r_offset -= removed_bytes;
5144 last_irel_offset = irel->r_offset;
5146 else if (next_irel && next_irel->r_offset == offset + 4)
5148 nexti += 1;
5149 irel->r_offset -= removed_bytes;
5150 next_irel->r_offset -= removed_bytes;
5151 last_irel_offset = next_irel->r_offset;
5153 else if (size == 0)
5155 /* Always remove entries with zero size. */
5156 bytes_to_remove = 8;
5157 if (irel && irel->r_offset == offset)
5159 remove_this_irel = TRUE;
5161 irel->r_offset -= removed_bytes;
5162 last_irel_offset = irel->r_offset;
5165 else if (irel && irel->r_offset == offset)
5167 if (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32)
5169 if (last_irel)
5171 bfd_vma old_size =
5172 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
5173 bfd_vma old_address =
5174 (last_irel->r_addend
5175 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
5176 bfd_vma new_address =
5177 (irel->r_addend
5178 + bfd_get_32 (abfd, &contents[actual_offset]));
5180 if ((ELF32_R_SYM (irel->r_info) ==
5181 ELF32_R_SYM (last_irel->r_info))
5182 && (old_address + old_size == new_address))
5184 /* fix the old size */
5185 bfd_put_32 (abfd, old_size + size,
5186 &contents[last_irel->r_offset + 4]);
5187 bytes_to_remove = 8;
5188 remove_this_irel = TRUE;
5190 else
5191 last_irel = irel;
5193 else
5194 last_irel = irel;
5197 irel->r_offset -= removed_bytes;
5198 last_irel_offset = irel->r_offset;
5201 if (remove_this_irel)
5203 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
5204 irel->r_offset -= bytes_to_remove;
5207 if (bytes_to_remove != 0)
5209 removed_bytes += bytes_to_remove;
5210 if (offset + 8 < section_size)
5211 memmove (&contents[actual_offset],
5212 &contents[actual_offset+8],
5213 section_size - offset - 8);
5217 if (removed_bytes)
5219 /* Clear the removed bytes. */
5220 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
5222 sec->size = section_size - removed_bytes;
5224 if (xtensa_is_littable_section (sec))
5226 bfd *dynobj = elf_hash_table (link_info)->dynobj;
5227 if (dynobj)
5229 asection *sgotloc =
5230 bfd_get_section_by_name (dynobj, ".got.loc");
5231 if (sgotloc)
5232 sgotloc->size -= removed_bytes;
5238 error_return:
5239 release_internal_relocs (sec, internal_relocs);
5240 release_contents (sec, contents);
5241 return ok;
5245 /* Third relaxation pass. */
5247 /* Change symbol values to account for removed literals. */
5249 bfd_boolean
5250 relax_section_symbols (abfd, sec)
5251 bfd *abfd;
5252 asection *sec;
5254 xtensa_relax_info *relax_info;
5255 unsigned int sec_shndx;
5256 Elf_Internal_Shdr *symtab_hdr;
5257 Elf_Internal_Sym *isymbuf;
5258 unsigned i, num_syms, num_locals;
5260 relax_info = get_xtensa_relax_info (sec);
5261 BFD_ASSERT (relax_info);
5263 if (!relax_info->is_relaxable_literal_section)
5264 return TRUE;
5266 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5268 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5269 isymbuf = retrieve_local_syms (abfd);
5271 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5272 num_locals = symtab_hdr->sh_info;
5274 /* Adjust the local symbols defined in this section. */
5275 for (i = 0; i < num_locals; i++)
5277 Elf_Internal_Sym *isym = &isymbuf[i];
5279 if (isym->st_shndx == sec_shndx)
5281 bfd_vma new_address = offset_with_removed_literals
5282 (&relax_info->removed_list, isym->st_value);
5283 if (new_address != isym->st_value)
5284 isym->st_value = new_address;
5288 /* Now adjust the global symbols defined in this section. */
5289 for (i = 0; i < (num_syms - num_locals); i++)
5291 struct elf_link_hash_entry *sym_hash;
5293 sym_hash = elf_sym_hashes (abfd)[i];
5295 if (sym_hash->root.type == bfd_link_hash_warning)
5296 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
5298 if ((sym_hash->root.type == bfd_link_hash_defined
5299 || sym_hash->root.type == bfd_link_hash_defweak)
5300 && sym_hash->root.u.def.section == sec)
5302 bfd_vma new_address = offset_with_removed_literals
5303 (&relax_info->removed_list, sym_hash->root.u.def.value);
5304 if (new_address != sym_hash->root.u.def.value)
5305 sym_hash->root.u.def.value = new_address;
5309 return TRUE;
5313 /* "Fix" handling functions, called while performing relocations. */
5315 static void
5316 do_fix_for_relocatable_link (rel, input_bfd, input_section)
5317 Elf_Internal_Rela *rel;
5318 bfd *input_bfd;
5319 asection *input_section;
5321 r_reloc r_rel;
5322 asection *sec, *old_sec;
5323 bfd_vma old_offset;
5324 int r_type = ELF32_R_TYPE (rel->r_info);
5325 reloc_bfd_fix *fix_list;
5326 reloc_bfd_fix *fix;
5328 if (r_type == R_XTENSA_NONE)
5329 return;
5331 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5332 if (fix_list == NULL)
5333 return;
5335 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5336 if (fix == NULL)
5337 return;
5339 r_reloc_init (&r_rel, input_bfd, rel);
5340 old_sec = r_reloc_get_section (&r_rel);
5341 old_offset = r_reloc_get_target_offset (&r_rel);
5343 if (old_sec == NULL || !r_reloc_is_defined (&r_rel))
5345 BFD_ASSERT (r_type == R_XTENSA_ASM_EXPAND);
5346 /* Leave it be. Resolution will happen in a later stage. */
5348 else
5350 sec = fix->target_sec;
5351 rel->r_addend += ((sec->output_offset + fix->target_offset)
5352 - (old_sec->output_offset + old_offset));
5357 static void
5358 do_fix_for_final_link (rel, input_section, relocationp)
5359 Elf_Internal_Rela *rel;
5360 asection *input_section;
5361 bfd_vma *relocationp;
5363 asection *sec;
5364 int r_type = ELF32_R_TYPE (rel->r_info);
5365 reloc_bfd_fix *fix_list;
5366 reloc_bfd_fix *fix;
5368 if (r_type == R_XTENSA_NONE)
5369 return;
5371 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5372 if (fix_list == NULL)
5373 return;
5375 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5376 if (fix == NULL)
5377 return;
5379 sec = fix->target_sec;
5380 *relocationp = (sec->output_section->vma
5381 + sec->output_offset
5382 + fix->target_offset - rel->r_addend);
5386 /* Miscellaneous utility functions.... */
5388 static asection *
5389 elf_xtensa_get_plt_section (dynobj, chunk)
5390 bfd *dynobj;
5391 int chunk;
5393 char plt_name[10];
5395 if (chunk == 0)
5396 return bfd_get_section_by_name (dynobj, ".plt");
5398 sprintf (plt_name, ".plt.%u", chunk);
5399 return bfd_get_section_by_name (dynobj, plt_name);
5403 static asection *
5404 elf_xtensa_get_gotplt_section (dynobj, chunk)
5405 bfd *dynobj;
5406 int chunk;
5408 char got_name[14];
5410 if (chunk == 0)
5411 return bfd_get_section_by_name (dynobj, ".got.plt");
5413 sprintf (got_name, ".got.plt.%u", chunk);
5414 return bfd_get_section_by_name (dynobj, got_name);
5418 /* Get the input section for a given symbol index.
5419 If the symbol is:
5420 . a section symbol, return the section;
5421 . a common symbol, return the common section;
5422 . an undefined symbol, return the undefined section;
5423 . an indirect symbol, follow the links;
5424 . an absolute value, return the absolute section. */
5426 static asection *
5427 get_elf_r_symndx_section (abfd, r_symndx)
5428 bfd *abfd;
5429 unsigned long r_symndx;
5431 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5432 asection *target_sec = NULL;
5433 if (r_symndx < symtab_hdr->sh_info)
5435 Elf_Internal_Sym *isymbuf;
5436 unsigned int section_index;
5438 isymbuf = retrieve_local_syms (abfd);
5439 section_index = isymbuf[r_symndx].st_shndx;
5441 if (section_index == SHN_UNDEF)
5442 target_sec = bfd_und_section_ptr;
5443 else if (section_index > 0 && section_index < SHN_LORESERVE)
5444 target_sec = bfd_section_from_elf_index (abfd, section_index);
5445 else if (section_index == SHN_ABS)
5446 target_sec = bfd_abs_section_ptr;
5447 else if (section_index == SHN_COMMON)
5448 target_sec = bfd_com_section_ptr;
5449 else
5450 /* Who knows? */
5451 target_sec = NULL;
5453 else
5455 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5456 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
5458 while (h->root.type == bfd_link_hash_indirect
5459 || h->root.type == bfd_link_hash_warning)
5460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5462 switch (h->root.type)
5464 case bfd_link_hash_defined:
5465 case bfd_link_hash_defweak:
5466 target_sec = h->root.u.def.section;
5467 break;
5468 case bfd_link_hash_common:
5469 target_sec = bfd_com_section_ptr;
5470 break;
5471 case bfd_link_hash_undefined:
5472 case bfd_link_hash_undefweak:
5473 target_sec = bfd_und_section_ptr;
5474 break;
5475 default: /* New indirect warning. */
5476 target_sec = bfd_und_section_ptr;
5477 break;
5480 return target_sec;
5484 static struct elf_link_hash_entry *
5485 get_elf_r_symndx_hash_entry (abfd, r_symndx)
5486 bfd *abfd;
5487 unsigned long r_symndx;
5489 unsigned long indx;
5490 struct elf_link_hash_entry *h;
5491 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5493 if (r_symndx < symtab_hdr->sh_info)
5494 return NULL;
5496 indx = r_symndx - symtab_hdr->sh_info;
5497 h = elf_sym_hashes (abfd)[indx];
5498 while (h->root.type == bfd_link_hash_indirect
5499 || h->root.type == bfd_link_hash_warning)
5500 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5501 return h;
5505 /* Get the section-relative offset for a symbol number. */
5507 static bfd_vma
5508 get_elf_r_symndx_offset (abfd, r_symndx)
5509 bfd *abfd;
5510 unsigned long r_symndx;
5512 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5513 bfd_vma offset = 0;
5515 if (r_symndx < symtab_hdr->sh_info)
5517 Elf_Internal_Sym *isymbuf;
5518 isymbuf = retrieve_local_syms (abfd);
5519 offset = isymbuf[r_symndx].st_value;
5521 else
5523 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5524 struct elf_link_hash_entry *h =
5525 elf_sym_hashes (abfd)[indx];
5527 while (h->root.type == bfd_link_hash_indirect
5528 || h->root.type == bfd_link_hash_warning)
5529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5530 if (h->root.type == bfd_link_hash_defined
5531 || h->root.type == bfd_link_hash_defweak)
5532 offset = h->root.u.def.value;
5534 return offset;
5538 static bfd_boolean
5539 pcrel_reloc_fits (opnd, self_address, dest_address)
5540 xtensa_operand opnd;
5541 bfd_vma self_address;
5542 bfd_vma dest_address;
5544 uint32 new_address =
5545 xtensa_operand_do_reloc (opnd, dest_address, self_address);
5546 return (xtensa_operand_encode (opnd, &new_address)
5547 == xtensa_encode_result_ok);
5551 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
5552 static int insn_sec_len = sizeof (XTENSA_INSN_SEC_NAME) - 1;
5553 static int lit_sec_len = sizeof (XTENSA_LIT_SEC_NAME) - 1;
5556 static bfd_boolean
5557 xtensa_is_property_section (sec)
5558 asection *sec;
5560 if (strncmp (XTENSA_INSN_SEC_NAME, sec->name, insn_sec_len) == 0
5561 || strncmp (XTENSA_LIT_SEC_NAME, sec->name, lit_sec_len) == 0)
5562 return TRUE;
5564 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0
5565 && (sec->name[linkonce_len] == 'x'
5566 || sec->name[linkonce_len] == 'p')
5567 && sec->name[linkonce_len + 1] == '.')
5568 return TRUE;
5570 return FALSE;
5574 static bfd_boolean
5575 xtensa_is_littable_section (sec)
5576 asection *sec;
5578 if (strncmp (XTENSA_LIT_SEC_NAME, sec->name, lit_sec_len) == 0)
5579 return TRUE;
5581 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0
5582 && sec->name[linkonce_len] == 'p'
5583 && sec->name[linkonce_len + 1] == '.')
5584 return TRUE;
5586 return FALSE;
5590 static bfd_boolean
5591 is_literal_section (sec)
5592 asection *sec;
5594 /* FIXME: the current definition of this leaves a lot to be desired.... */
5595 if (sec == NULL || sec->name == NULL)
5596 return FALSE;
5597 return (strstr (sec->name, "literal") != NULL);
5601 static int
5602 internal_reloc_compare (ap, bp)
5603 const PTR ap;
5604 const PTR bp;
5606 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
5607 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
5609 return (a->r_offset - b->r_offset);
5613 char *
5614 xtensa_get_property_section_name (sec, base_name)
5615 asection *sec;
5616 const char *base_name;
5618 if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
5620 char *prop_sec_name;
5621 const char *suffix;
5622 char linkonce_kind = 0;
5624 if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
5625 linkonce_kind = 'x';
5626 else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
5627 linkonce_kind = 'p';
5628 else
5629 abort ();
5631 prop_sec_name = (char *) bfd_malloc (strlen (sec->name) + 1);
5632 memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
5633 prop_sec_name[linkonce_len] = linkonce_kind;
5634 prop_sec_name[linkonce_len + 1] = '.';
5636 suffix = sec->name + linkonce_len;
5637 /* For backward compatibility, replace "t." instead of inserting
5638 the new linkonce_kind. */
5639 if (strncmp (suffix, "t.", 2) == 0)
5640 suffix += 2;
5641 strcpy (prop_sec_name + linkonce_len + 2, suffix);
5643 return prop_sec_name;
5646 return strdup (base_name);
5650 /* Other functions called directly by the linker. */
5652 bfd_boolean
5653 xtensa_callback_required_dependence (abfd, sec, link_info, callback, closure)
5654 bfd *abfd;
5655 asection *sec;
5656 struct bfd_link_info *link_info;
5657 deps_callback_t callback;
5658 PTR closure;
5660 Elf_Internal_Rela *internal_relocs;
5661 bfd_byte *contents;
5662 unsigned i;
5663 bfd_boolean ok = TRUE;
5665 /* ".plt*" sections have no explicit relocations but they contain L32R
5666 instructions that reference the corresponding ".got.plt*" sections. */
5667 if ((sec->flags & SEC_LINKER_CREATED) != 0
5668 && strncmp (sec->name, ".plt", 4) == 0)
5670 asection *sgotplt;
5672 /* Find the corresponding ".got.plt*" section. */
5673 if (sec->name[4] == '\0')
5674 sgotplt = bfd_get_section_by_name (sec->owner, ".got.plt");
5675 else
5677 char got_name[14];
5678 int chunk = 0;
5680 BFD_ASSERT (sec->name[4] == '.');
5681 chunk = strtol (&sec->name[5], NULL, 10);
5683 sprintf (got_name, ".got.plt.%u", chunk);
5684 sgotplt = bfd_get_section_by_name (sec->owner, got_name);
5686 BFD_ASSERT (sgotplt);
5688 /* Assume worst-case offsets: L32R at the very end of the ".plt"
5689 section referencing a literal at the very beginning of
5690 ".got.plt". This is very close to the real dependence, anyway. */
5691 (*callback) (sec, sec->size, sgotplt, 0, closure);
5694 internal_relocs = retrieve_internal_relocs (abfd, sec,
5695 link_info->keep_memory);
5696 if (internal_relocs == NULL
5697 || sec->reloc_count == 0)
5698 return ok;
5700 /* Cache the contents for the duration of this scan. */
5701 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5702 if (contents == NULL && sec->size != 0)
5704 ok = FALSE;
5705 goto error_return;
5708 if (xtensa_default_isa == NULL)
5709 xtensa_isa_init ();
5711 for (i = 0; i < sec->reloc_count; i++)
5713 Elf_Internal_Rela *irel = &internal_relocs[i];
5714 if (is_l32r_relocation (sec, contents, irel))
5716 r_reloc l32r_rel;
5717 asection *target_sec;
5718 bfd_vma target_offset;
5720 r_reloc_init (&l32r_rel, abfd, irel);
5721 target_sec = NULL;
5722 target_offset = 0;
5723 /* L32Rs must be local to the input file. */
5724 if (r_reloc_is_defined (&l32r_rel))
5726 target_sec = r_reloc_get_section (&l32r_rel);
5727 target_offset = r_reloc_get_target_offset (&l32r_rel);
5729 (*callback) (sec, irel->r_offset, target_sec, target_offset,
5730 closure);
5734 error_return:
5735 release_internal_relocs (sec, internal_relocs);
5736 release_contents (sec, contents);
5737 return ok;
5740 /* The default literal sections should always be marked as "code" (i.e.,
5741 SHF_EXECINSTR). This is particularly important for the Linux kernel
5742 module loader so that the literals are not placed after the text. */
5743 static struct bfd_elf_special_section const elf_xtensa_special_sections[]=
5745 { ".literal", 8, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5746 { ".init.literal", 13, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5747 { ".fini.literal", 13, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5748 { NULL, 0, 0, 0, 0 }
5752 #ifndef ELF_ARCH
5753 #define TARGET_LITTLE_SYM bfd_elf32_xtensa_le_vec
5754 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
5755 #define TARGET_BIG_SYM bfd_elf32_xtensa_be_vec
5756 #define TARGET_BIG_NAME "elf32-xtensa-be"
5757 #define ELF_ARCH bfd_arch_xtensa
5759 /* The new EM_XTENSA value will be recognized beginning in the Xtensa T1040
5760 release. However, we still have to generate files with the EM_XTENSA_OLD
5761 value so that pre-T1040 tools can read the files. As soon as we stop
5762 caring about pre-T1040 tools, the following two values should be
5763 swapped. At the same time, any other code that uses EM_XTENSA_OLD
5764 (e.g., prep_headers() in elf.c) should be changed to use EM_XTENSA. */
5765 #define ELF_MACHINE_CODE EM_XTENSA_OLD
5766 #define ELF_MACHINE_ALT1 EM_XTENSA
5768 #if XCHAL_HAVE_MMU
5769 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
5770 #else /* !XCHAL_HAVE_MMU */
5771 #define ELF_MAXPAGESIZE 1
5772 #endif /* !XCHAL_HAVE_MMU */
5773 #endif /* ELF_ARCH */
5775 #define elf_backend_can_gc_sections 1
5776 #define elf_backend_can_refcount 1
5777 #define elf_backend_plt_readonly 1
5778 #define elf_backend_got_header_size 4
5779 #define elf_backend_want_dynbss 0
5780 #define elf_backend_want_got_plt 1
5782 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
5784 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
5785 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
5786 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
5787 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
5788 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
5789 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
5791 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
5792 #define elf_backend_check_relocs elf_xtensa_check_relocs
5793 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
5794 #define elf_backend_discard_info elf_xtensa_discard_info
5795 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
5796 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
5797 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
5798 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
5799 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
5800 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
5801 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
5802 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
5803 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
5804 #define elf_backend_modify_segment_map elf_xtensa_modify_segment_map
5805 #define elf_backend_object_p elf_xtensa_object_p
5806 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
5807 #define elf_backend_relocate_section elf_xtensa_relocate_section
5808 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
5809 #define elf_backend_special_sections elf_xtensa_special_sections
5811 #include "elf32-target.h"