* read.c: Remove unneeded prototypes.
[binutils.git] / bfd / elf32-xtensa.c
blob44450c52e5bbf4beb0eaa9ae925bac3f4509d3bb
1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright 2003 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;
501 table_section_name =
502 xtensa_get_property_section_name (section, sec_name);
503 table_section = bfd_get_section_by_name (abfd, table_section_name);
504 free (table_section_name);
505 if (table_section != NULL)
506 table_size = bfd_get_section_size_before_reloc (table_section);
508 if (table_size == 0)
510 *table_p = NULL;
511 return 0;
514 num_records = table_size / 8;
515 table_data = retrieve_contents (abfd, table_section, TRUE);
516 blocks = (property_table_entry *)
517 bfd_malloc (num_records * sizeof (property_table_entry));
518 block_count = 0;
520 /* If the file has not yet been relocated, process the relocations
521 and sort out the table entries that apply to the specified section. */
522 internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
523 if (internal_relocs)
525 unsigned i;
527 for (i = 0; i < table_section->reloc_count; i++)
529 Elf_Internal_Rela *rel = &internal_relocs[i];
530 unsigned long r_symndx;
532 if (ELF32_R_TYPE (rel->r_info) == R_XTENSA_NONE)
533 continue;
535 BFD_ASSERT (ELF32_R_TYPE (rel->r_info) == R_XTENSA_32);
536 r_symndx = ELF32_R_SYM (rel->r_info);
538 if (get_elf_r_symndx_section (abfd, r_symndx) == section)
540 bfd_vma sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
541 blocks[block_count].address =
542 (section->vma + sym_off + rel->r_addend
543 + bfd_get_32 (abfd, table_data + rel->r_offset));
544 blocks[block_count].size =
545 bfd_get_32 (abfd, table_data + rel->r_offset + 4);
546 block_count++;
550 else
552 /* No relocations. Presumably the file has been relocated
553 and the addresses are already in the table. */
554 bfd_vma off;
556 for (off = 0; off < table_size; off += 8)
558 bfd_vma address = bfd_get_32 (abfd, table_data + off);
560 if (address >= section->vma
561 && address < ( section->vma + section->_raw_size))
563 blocks[block_count].address = address;
564 blocks[block_count].size =
565 bfd_get_32 (abfd, table_data + off + 4);
566 block_count++;
571 release_contents (table_section, table_data);
572 release_internal_relocs (table_section, internal_relocs);
574 if (block_count > 0)
576 /* Now sort them into address order for easy reference. */
577 qsort (blocks, block_count, sizeof (property_table_entry),
578 property_table_compare);
581 *table_p = blocks;
582 return block_count;
586 static bfd_boolean
587 elf_xtensa_in_literal_pool (lit_table, lit_table_size, addr)
588 property_table_entry *lit_table;
589 int lit_table_size;
590 bfd_vma addr;
592 property_table_entry entry;
594 if (lit_table_size == 0)
595 return FALSE;
597 entry.address = addr;
598 entry.size = 1;
600 if (bsearch (&entry, lit_table, lit_table_size,
601 sizeof (property_table_entry), property_table_compare))
602 return TRUE;
604 return FALSE;
608 /* Look through the relocs for a section during the first phase, and
609 calculate needed space in the dynamic reloc sections. */
611 static bfd_boolean
612 elf_xtensa_check_relocs (abfd, info, sec, relocs)
613 bfd *abfd;
614 struct bfd_link_info *info;
615 asection *sec;
616 const Elf_Internal_Rela *relocs;
618 Elf_Internal_Shdr *symtab_hdr;
619 struct elf_link_hash_entry **sym_hashes;
620 const Elf_Internal_Rela *rel;
621 const Elf_Internal_Rela *rel_end;
622 property_table_entry *lit_table;
623 int ltblsize;
625 if (info->relocatable)
626 return TRUE;
628 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
629 sym_hashes = elf_sym_hashes (abfd);
631 ltblsize = xtensa_read_table_entries (abfd, sec, &lit_table,
632 XTENSA_LIT_SEC_NAME);
633 if (ltblsize < 0)
634 return FALSE;
636 rel_end = relocs + sec->reloc_count;
637 for (rel = relocs; rel < rel_end; rel++)
639 unsigned int r_type;
640 unsigned long r_symndx;
641 struct elf_link_hash_entry *h;
643 r_symndx = ELF32_R_SYM (rel->r_info);
644 r_type = ELF32_R_TYPE (rel->r_info);
646 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
648 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
649 bfd_archive_filename (abfd),
650 r_symndx);
651 return FALSE;
654 if (r_symndx < symtab_hdr->sh_info)
655 h = NULL;
656 else
658 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
659 while (h->root.type == bfd_link_hash_indirect
660 || h->root.type == bfd_link_hash_warning)
661 h = (struct elf_link_hash_entry *) h->root.u.i.link;
664 switch (r_type)
666 case R_XTENSA_32:
667 if (h == NULL)
668 goto local_literal;
670 if ((sec->flags & SEC_ALLOC) != 0)
672 if ((sec->flags & SEC_READONLY) != 0
673 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
674 sec->vma + rel->r_offset))
675 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
677 if (h->got.refcount <= 0)
678 h->got.refcount = 1;
679 else
680 h->got.refcount += 1;
682 break;
684 case R_XTENSA_PLT:
685 /* If this relocation is against a local symbol, then it's
686 exactly the same as a normal local GOT entry. */
687 if (h == NULL)
688 goto local_literal;
690 if ((sec->flags & SEC_ALLOC) != 0)
692 if ((sec->flags & SEC_READONLY) != 0
693 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
694 sec->vma + rel->r_offset))
695 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
697 if (h->plt.refcount <= 0)
699 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
700 h->plt.refcount = 1;
702 else
703 h->plt.refcount += 1;
705 /* Keep track of the total PLT relocation count even if we
706 don't yet know whether the dynamic sections will be
707 created. */
708 plt_reloc_count += 1;
710 if (elf_hash_table (info)->dynamic_sections_created)
712 if (!add_extra_plt_sections (elf_hash_table (info)->dynobj,
713 plt_reloc_count))
714 return FALSE;
717 break;
719 local_literal:
720 if ((sec->flags & SEC_ALLOC) != 0)
722 bfd_signed_vma *local_got_refcounts;
724 /* This is a global offset table entry for a local symbol. */
725 local_got_refcounts = elf_local_got_refcounts (abfd);
726 if (local_got_refcounts == NULL)
728 bfd_size_type size;
730 size = symtab_hdr->sh_info;
731 size *= sizeof (bfd_signed_vma);
732 local_got_refcounts = ((bfd_signed_vma *)
733 bfd_zalloc (abfd, size));
734 if (local_got_refcounts == NULL)
735 return FALSE;
736 elf_local_got_refcounts (abfd) = local_got_refcounts;
738 local_got_refcounts[r_symndx] += 1;
740 /* If the relocation is not inside the GOT, the DF_TEXTREL
741 flag needs to be set. */
742 if (info->shared
743 && (sec->flags & SEC_READONLY) != 0
744 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
745 sec->vma + rel->r_offset))
746 info->flags |= DF_TEXTREL;
748 break;
750 case R_XTENSA_OP0:
751 case R_XTENSA_OP1:
752 case R_XTENSA_OP2:
753 case R_XTENSA_ASM_EXPAND:
754 case R_XTENSA_ASM_SIMPLIFY:
755 /* Nothing to do for these. */
756 break;
758 case R_XTENSA_GNU_VTINHERIT:
759 /* This relocation describes the C++ object vtable hierarchy.
760 Reconstruct it for later use during GC. */
761 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
762 return FALSE;
763 break;
765 case R_XTENSA_GNU_VTENTRY:
766 /* This relocation describes which C++ vtable entries are actually
767 used. Record for later use during GC. */
768 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
769 return FALSE;
770 break;
772 default:
773 break;
777 free (lit_table);
778 return TRUE;
782 static void
783 elf_xtensa_hide_symbol (info, h, force_local)
784 struct bfd_link_info *info;
785 struct elf_link_hash_entry *h;
786 bfd_boolean force_local;
788 /* For a shared link, move the plt refcount to the got refcount to leave
789 space for RELATIVE relocs. */
790 elf_xtensa_make_sym_local (info, h);
792 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
796 /* Return the section that should be marked against GC for a given
797 relocation. */
799 static asection *
800 elf_xtensa_gc_mark_hook (sec, info, rel, h, sym)
801 asection *sec;
802 struct bfd_link_info *info ATTRIBUTE_UNUSED;
803 Elf_Internal_Rela *rel;
804 struct elf_link_hash_entry *h;
805 Elf_Internal_Sym *sym;
807 if (h != NULL)
809 switch (ELF32_R_TYPE (rel->r_info))
811 case R_XTENSA_GNU_VTINHERIT:
812 case R_XTENSA_GNU_VTENTRY:
813 break;
815 default:
816 switch (h->root.type)
818 case bfd_link_hash_defined:
819 case bfd_link_hash_defweak:
820 return h->root.u.def.section;
822 case bfd_link_hash_common:
823 return h->root.u.c.p->section;
825 default:
826 break;
830 else
831 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
833 return NULL;
836 /* Update the GOT & PLT entry reference counts
837 for the section being removed. */
839 static bfd_boolean
840 elf_xtensa_gc_sweep_hook (abfd, info, sec, relocs)
841 bfd *abfd;
842 struct bfd_link_info *info ATTRIBUTE_UNUSED;
843 asection *sec;
844 const Elf_Internal_Rela *relocs;
846 Elf_Internal_Shdr *symtab_hdr;
847 struct elf_link_hash_entry **sym_hashes;
848 bfd_signed_vma *local_got_refcounts;
849 const Elf_Internal_Rela *rel, *relend;
851 if ((sec->flags & SEC_ALLOC) == 0)
852 return TRUE;
854 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
855 sym_hashes = elf_sym_hashes (abfd);
856 local_got_refcounts = elf_local_got_refcounts (abfd);
858 relend = relocs + sec->reloc_count;
859 for (rel = relocs; rel < relend; rel++)
861 unsigned long r_symndx;
862 unsigned int r_type;
863 struct elf_link_hash_entry *h = NULL;
865 r_symndx = ELF32_R_SYM (rel->r_info);
866 if (r_symndx >= symtab_hdr->sh_info)
867 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
869 r_type = ELF32_R_TYPE (rel->r_info);
870 switch (r_type)
872 case R_XTENSA_32:
873 if (h == NULL)
874 goto local_literal;
875 if (h->got.refcount > 0)
876 h->got.refcount--;
877 break;
879 case R_XTENSA_PLT:
880 if (h == NULL)
881 goto local_literal;
882 if (h->plt.refcount > 0)
883 h->plt.refcount--;
884 break;
886 local_literal:
887 if (local_got_refcounts[r_symndx] > 0)
888 local_got_refcounts[r_symndx] -= 1;
889 break;
891 default:
892 break;
896 return TRUE;
900 /* Create all the dynamic sections. */
902 static bfd_boolean
903 elf_xtensa_create_dynamic_sections (dynobj, info)
904 bfd *dynobj;
905 struct bfd_link_info *info;
907 flagword flags, noalloc_flags;
908 asection *s;
910 /* First do all the standard stuff. */
911 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
912 return FALSE;
914 /* Create any extra PLT sections in case check_relocs has already
915 been called on all the non-dynamic input files. */
916 if (!add_extra_plt_sections (dynobj, plt_reloc_count))
917 return FALSE;
919 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
920 | SEC_LINKER_CREATED | SEC_READONLY);
921 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
923 /* Mark the ".got.plt" section READONLY. */
924 s = bfd_get_section_by_name (dynobj, ".got.plt");
925 if (s == NULL
926 || ! bfd_set_section_flags (dynobj, s, flags))
927 return FALSE;
929 /* Create ".rela.got". */
930 s = bfd_make_section (dynobj, ".rela.got");
931 if (s == NULL
932 || ! bfd_set_section_flags (dynobj, s, flags)
933 || ! bfd_set_section_alignment (dynobj, s, 2))
934 return FALSE;
936 /* Create ".got.loc" (literal tables for use by dynamic linker). */
937 s = bfd_make_section (dynobj, ".got.loc");
938 if (s == NULL
939 || ! bfd_set_section_flags (dynobj, s, flags)
940 || ! bfd_set_section_alignment (dynobj, s, 2))
941 return FALSE;
943 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
944 s = bfd_make_section (dynobj, ".xt.lit.plt");
945 if (s == NULL
946 || ! bfd_set_section_flags (dynobj, s, noalloc_flags)
947 || ! bfd_set_section_alignment (dynobj, s, 2))
948 return FALSE;
950 return TRUE;
954 static bfd_boolean
955 add_extra_plt_sections (dynobj, count)
956 bfd *dynobj;
957 int count;
959 int chunk;
961 /* Iterate over all chunks except 0 which uses the standard ".plt" and
962 ".got.plt" sections. */
963 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
965 char *sname;
966 flagword flags;
967 asection *s;
969 /* Stop when we find a section has already been created. */
970 if (elf_xtensa_get_plt_section (dynobj, chunk))
971 break;
973 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
974 | SEC_LINKER_CREATED | SEC_READONLY);
976 sname = (char *) bfd_malloc (10);
977 sprintf (sname, ".plt.%u", chunk);
978 s = bfd_make_section (dynobj, sname);
979 if (s == NULL
980 || ! bfd_set_section_flags (dynobj, s, flags | SEC_CODE)
981 || ! bfd_set_section_alignment (dynobj, s, 2))
982 return FALSE;
984 sname = (char *) bfd_malloc (14);
985 sprintf (sname, ".got.plt.%u", chunk);
986 s = bfd_make_section (dynobj, sname);
987 if (s == NULL
988 || ! bfd_set_section_flags (dynobj, s, flags)
989 || ! bfd_set_section_alignment (dynobj, s, 2))
990 return FALSE;
993 return TRUE;
997 /* Adjust a symbol defined by a dynamic object and referenced by a
998 regular object. The current definition is in some section of the
999 dynamic object, but we're not including those sections. We have to
1000 change the definition to something the rest of the link can
1001 understand. */
1003 static bfd_boolean
1004 elf_xtensa_adjust_dynamic_symbol (info, h)
1005 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1006 struct elf_link_hash_entry *h;
1008 /* If this is a weak symbol, and there is a real definition, the
1009 processor independent code will have arranged for us to see the
1010 real definition first, and we can just use the same value. */
1011 if (h->weakdef != NULL)
1013 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1014 || h->weakdef->root.type == bfd_link_hash_defweak);
1015 h->root.u.def.section = h->weakdef->root.u.def.section;
1016 h->root.u.def.value = h->weakdef->root.u.def.value;
1017 return TRUE;
1020 /* This is a reference to a symbol defined by a dynamic object. The
1021 reference must go through the GOT, so there's no need for COPY relocs,
1022 .dynbss, etc. */
1024 return TRUE;
1028 static void
1029 elf_xtensa_make_sym_local (info, h)
1030 struct bfd_link_info *info;
1031 struct elf_link_hash_entry *h;
1033 if (info->shared)
1035 if (h->plt.refcount > 0)
1037 /* Will use RELATIVE relocs instead of JMP_SLOT relocs. */
1038 if (h->got.refcount < 0)
1039 h->got.refcount = 0;
1040 h->got.refcount += h->plt.refcount;
1041 h->plt.refcount = 0;
1044 else
1046 /* Don't need any dynamic relocations at all. */
1047 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1048 h->plt.refcount = 0;
1049 h->got.refcount = 0;
1054 static bfd_boolean
1055 elf_xtensa_fix_refcounts (h, arg)
1056 struct elf_link_hash_entry *h;
1057 PTR arg;
1059 struct bfd_link_info *info = (struct bfd_link_info *) arg;
1061 if (h->root.type == bfd_link_hash_warning)
1062 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1064 if (! xtensa_elf_dynamic_symbol_p (h, info))
1065 elf_xtensa_make_sym_local (info, h);
1067 /* If the symbol has a relocation outside the GOT, set the
1068 DF_TEXTREL flag. */
1069 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) != 0)
1070 info->flags |= DF_TEXTREL;
1072 return TRUE;
1076 static bfd_boolean
1077 elf_xtensa_allocate_plt_size (h, arg)
1078 struct elf_link_hash_entry *h;
1079 PTR arg;
1081 asection *srelplt = (asection *) arg;
1083 if (h->root.type == bfd_link_hash_warning)
1084 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1086 if (h->plt.refcount > 0)
1087 srelplt->_raw_size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1089 return TRUE;
1093 static bfd_boolean
1094 elf_xtensa_allocate_got_size (h, arg)
1095 struct elf_link_hash_entry *h;
1096 PTR arg;
1098 asection *srelgot = (asection *) arg;
1100 if (h->root.type == bfd_link_hash_warning)
1101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1103 if (h->got.refcount > 0)
1104 srelgot->_raw_size += (h->got.refcount * sizeof (Elf32_External_Rela));
1106 return TRUE;
1110 static void
1111 elf_xtensa_allocate_local_got_size (info, srelgot)
1112 struct bfd_link_info *info;
1113 asection *srelgot;
1115 bfd *i;
1117 for (i = info->input_bfds; i; i = i->link_next)
1119 bfd_signed_vma *local_got_refcounts;
1120 bfd_size_type j, cnt;
1121 Elf_Internal_Shdr *symtab_hdr;
1123 local_got_refcounts = elf_local_got_refcounts (i);
1124 if (!local_got_refcounts)
1125 continue;
1127 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1128 cnt = symtab_hdr->sh_info;
1130 for (j = 0; j < cnt; ++j)
1132 if (local_got_refcounts[j] > 0)
1133 srelgot->_raw_size += (local_got_refcounts[j]
1134 * sizeof (Elf32_External_Rela));
1140 /* Set the sizes of the dynamic sections. */
1142 static bfd_boolean
1143 elf_xtensa_size_dynamic_sections (output_bfd, info)
1144 bfd *output_bfd ATTRIBUTE_UNUSED;
1145 struct bfd_link_info *info;
1147 bfd *dynobj, *abfd;
1148 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1149 bfd_boolean relplt, relgot;
1150 int plt_entries, plt_chunks, chunk;
1152 plt_entries = 0;
1153 plt_chunks = 0;
1154 srelgot = 0;
1156 dynobj = elf_hash_table (info)->dynobj;
1157 if (dynobj == NULL)
1158 abort ();
1160 if (elf_hash_table (info)->dynamic_sections_created)
1162 /* Set the contents of the .interp section to the interpreter. */
1163 if (info->executable)
1165 s = bfd_get_section_by_name (dynobj, ".interp");
1166 if (s == NULL)
1167 abort ();
1168 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1169 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1172 /* Allocate room for one word in ".got". */
1173 s = bfd_get_section_by_name (dynobj, ".got");
1174 if (s == NULL)
1175 abort ();
1176 s->_raw_size = 4;
1178 /* Adjust refcounts for symbols that we now know are not "dynamic". */
1179 elf_link_hash_traverse (elf_hash_table (info),
1180 elf_xtensa_fix_refcounts,
1181 (PTR) info);
1183 /* Allocate space in ".rela.got" for literals that reference
1184 global symbols. */
1185 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1186 if (srelgot == NULL)
1187 abort ();
1188 elf_link_hash_traverse (elf_hash_table (info),
1189 elf_xtensa_allocate_got_size,
1190 (PTR) srelgot);
1192 /* If we are generating a shared object, we also need space in
1193 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1194 reference local symbols. */
1195 if (info->shared)
1196 elf_xtensa_allocate_local_got_size (info, srelgot);
1198 /* Allocate space in ".rela.plt" for literals that have PLT entries. */
1199 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1200 if (srelplt == NULL)
1201 abort ();
1202 elf_link_hash_traverse (elf_hash_table (info),
1203 elf_xtensa_allocate_plt_size,
1204 (PTR) srelplt);
1206 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1207 each PLT entry, we need the PLT code plus a 4-byte literal.
1208 For each chunk of ".plt", we also need two more 4-byte
1209 literals, two corresponding entries in ".rela.got", and an
1210 8-byte entry in ".xt.lit.plt". */
1211 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
1212 if (spltlittbl == NULL)
1213 abort ();
1215 plt_entries = srelplt->_raw_size / sizeof (Elf32_External_Rela);
1216 plt_chunks =
1217 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1219 /* Iterate over all the PLT chunks, including any extra sections
1220 created earlier because the initial count of PLT relocations
1221 was an overestimate. */
1222 for (chunk = 0;
1223 (splt = elf_xtensa_get_plt_section (dynobj, chunk)) != NULL;
1224 chunk++)
1226 int chunk_entries;
1228 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1229 if (sgotplt == NULL)
1230 abort ();
1232 if (chunk < plt_chunks - 1)
1233 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1234 else if (chunk == plt_chunks - 1)
1235 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1236 else
1237 chunk_entries = 0;
1239 if (chunk_entries != 0)
1241 sgotplt->_raw_size = 4 * (chunk_entries + 2);
1242 splt->_raw_size = PLT_ENTRY_SIZE * chunk_entries;
1243 srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
1244 spltlittbl->_raw_size += 8;
1246 else
1248 sgotplt->_raw_size = 0;
1249 splt->_raw_size = 0;
1253 /* Allocate space in ".got.loc" to match the total size of all the
1254 literal tables. */
1255 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
1256 if (sgotloc == NULL)
1257 abort ();
1258 sgotloc->_raw_size = spltlittbl->_raw_size;
1259 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1261 if (abfd->flags & DYNAMIC)
1262 continue;
1263 for (s = abfd->sections; s != NULL; s = s->next)
1265 if (! elf_discarded_section (s)
1266 && xtensa_is_littable_section (s)
1267 && s != spltlittbl)
1268 sgotloc->_raw_size += s->_raw_size;
1273 /* Allocate memory for dynamic sections. */
1274 relplt = FALSE;
1275 relgot = FALSE;
1276 for (s = dynobj->sections; s != NULL; s = s->next)
1278 const char *name;
1279 bfd_boolean strip;
1281 if ((s->flags & SEC_LINKER_CREATED) == 0)
1282 continue;
1284 /* It's OK to base decisions on the section name, because none
1285 of the dynobj section names depend upon the input files. */
1286 name = bfd_get_section_name (dynobj, s);
1288 strip = FALSE;
1290 if (strncmp (name, ".rela", 5) == 0)
1292 if (strcmp (name, ".rela.plt") == 0)
1293 relplt = TRUE;
1294 else if (strcmp (name, ".rela.got") == 0)
1295 relgot = TRUE;
1297 /* We use the reloc_count field as a counter if we need
1298 to copy relocs into the output file. */
1299 s->reloc_count = 0;
1301 else if (strncmp (name, ".plt.", 5) == 0
1302 || strncmp (name, ".got.plt.", 9) == 0)
1304 if (s->_raw_size == 0)
1306 /* If we don't need this section, strip it from the output
1307 file. We must create the ".plt*" and ".got.plt*"
1308 sections in create_dynamic_sections and/or check_relocs
1309 based on a conservative estimate of the PLT relocation
1310 count, because the sections must be created before the
1311 linker maps input sections to output sections. The
1312 linker does that before size_dynamic_sections, where we
1313 compute the exact size of the PLT, so there may be more
1314 of these sections than are actually needed. */
1315 strip = TRUE;
1318 else if (strcmp (name, ".got") != 0
1319 && strcmp (name, ".plt") != 0
1320 && strcmp (name, ".got.plt") != 0
1321 && strcmp (name, ".xt.lit.plt") != 0
1322 && strcmp (name, ".got.loc") != 0)
1324 /* It's not one of our sections, so don't allocate space. */
1325 continue;
1328 if (strip)
1329 _bfd_strip_section_from_output (info, s);
1330 else
1332 /* Allocate memory for the section contents. */
1333 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1334 if (s->contents == NULL && s->_raw_size != 0)
1335 return FALSE;
1339 if (elf_hash_table (info)->dynamic_sections_created)
1341 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1342 known until finish_dynamic_sections, but we need to get the relocs
1343 in place before they are sorted. */
1344 if (srelgot == NULL)
1345 abort ();
1346 for (chunk = 0; chunk < plt_chunks; chunk++)
1348 Elf_Internal_Rela irela;
1349 bfd_byte *loc;
1351 irela.r_offset = 0;
1352 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1353 irela.r_addend = 0;
1355 loc = (srelgot->contents
1356 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1357 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1358 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1359 loc + sizeof (Elf32_External_Rela));
1360 srelgot->reloc_count += 2;
1363 /* Add some entries to the .dynamic section. We fill in the
1364 values later, in elf_xtensa_finish_dynamic_sections, but we
1365 must add the entries now so that we get the correct size for
1366 the .dynamic section. The DT_DEBUG entry is filled in by the
1367 dynamic linker and used by the debugger. */
1368 #define add_dynamic_entry(TAG, VAL) \
1369 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1371 if (! info->shared)
1373 if (!add_dynamic_entry (DT_DEBUG, 0))
1374 return FALSE;
1377 if (relplt)
1379 if (!add_dynamic_entry (DT_PLTGOT, 0)
1380 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1381 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1382 || !add_dynamic_entry (DT_JMPREL, 0))
1383 return FALSE;
1386 if (relgot)
1388 if (!add_dynamic_entry (DT_RELA, 0)
1389 || !add_dynamic_entry (DT_RELASZ, 0)
1390 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1391 return FALSE;
1394 if ((info->flags & DF_TEXTREL) != 0)
1396 if (!add_dynamic_entry (DT_TEXTREL, 0))
1397 return FALSE;
1400 if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1401 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1402 return FALSE;
1404 #undef add_dynamic_entry
1406 return TRUE;
1410 /* Remove any PT_LOAD segments with no allocated sections. Prior to
1411 binutils 2.13, this function used to remove the non-SEC_ALLOC
1412 sections from PT_LOAD segments, but that task has now been moved
1413 into elf.c. We still need this function to remove any empty
1414 segments that result, but there's nothing Xtensa-specific about
1415 this and it probably ought to be moved into elf.c as well. */
1417 static bfd_boolean
1418 elf_xtensa_modify_segment_map (abfd, info)
1419 bfd *abfd;
1420 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1422 struct elf_segment_map **m_p;
1424 m_p = &elf_tdata (abfd)->segment_map;
1425 while (*m_p != NULL)
1427 if ((*m_p)->p_type == PT_LOAD && (*m_p)->count == 0)
1428 *m_p = (*m_p)->next;
1429 else
1430 m_p = &(*m_p)->next;
1432 return TRUE;
1436 /* Perform the specified relocation. The instruction at (contents + address)
1437 is modified to set one operand to represent the value in "relocation". The
1438 operand position is determined by the relocation type recorded in the
1439 howto. */
1441 #define CALL_SEGMENT_BITS (30)
1442 #define CALL_SEGMENT_SIZE (1<<CALL_SEGMENT_BITS)
1444 static bfd_reloc_status_type
1445 elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1446 contents, address, is_weak_undef, error_message)
1447 reloc_howto_type *howto;
1448 bfd *abfd;
1449 asection *input_section;
1450 bfd_vma relocation;
1451 bfd_byte *contents;
1452 bfd_vma address;
1453 bfd_boolean is_weak_undef;
1454 char **error_message;
1456 xtensa_opcode opcode;
1457 xtensa_operand operand;
1458 xtensa_encode_result encode_result;
1459 xtensa_isa isa = xtensa_default_isa;
1460 xtensa_insnbuf ibuff;
1461 bfd_vma self_address;
1462 int opnd;
1463 uint32 newval;
1465 switch (howto->type)
1467 case R_XTENSA_NONE:
1468 return bfd_reloc_ok;
1470 case R_XTENSA_ASM_EXPAND:
1471 if (!is_weak_undef)
1473 /* Check for windowed CALL across a 1GB boundary. */
1474 xtensa_opcode opcode =
1475 get_expanded_call_opcode (contents + address,
1476 input_section->_raw_size - address);
1477 if (is_windowed_call_opcode (opcode))
1479 self_address = (input_section->output_section->vma
1480 + input_section->output_offset
1481 + address);
1482 if ((self_address >> CALL_SEGMENT_BITS) !=
1483 (relocation >> CALL_SEGMENT_BITS))
1485 *error_message = "windowed longcall crosses 1GB boundary; "
1486 "return may fail";
1487 return bfd_reloc_dangerous;
1491 return bfd_reloc_ok;
1493 case R_XTENSA_ASM_SIMPLIFY:
1495 /* Convert the L32R/CALLX to CALL. */
1496 bfd_reloc_status_type retval =
1497 elf_xtensa_do_asm_simplify (contents, address,
1498 input_section->_raw_size);
1499 if (retval != bfd_reloc_ok)
1500 return retval;
1502 /* The CALL needs to be relocated. Continue below for that part. */
1503 address += 3;
1504 howto = &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
1506 break;
1508 case R_XTENSA_32:
1509 case R_XTENSA_PLT:
1511 bfd_vma x;
1512 x = bfd_get_32 (abfd, contents + address);
1513 x = x + relocation;
1514 bfd_put_32 (abfd, x, contents + address);
1516 return bfd_reloc_ok;
1519 /* Read the instruction into a buffer and decode the opcode. */
1520 ibuff = xtensa_insnbuf_alloc (isa);
1521 xtensa_insnbuf_from_chars (isa, ibuff, contents + address);
1522 opcode = xtensa_decode_insn (isa, ibuff);
1524 /* Determine which operand is being relocated. */
1525 if (opcode == XTENSA_UNDEFINED)
1527 *error_message = "cannot decode instruction";
1528 return bfd_reloc_dangerous;
1531 if (howto->type < R_XTENSA_OP0 || howto->type > R_XTENSA_OP2)
1533 *error_message = "unexpected relocation";
1534 return bfd_reloc_dangerous;
1537 opnd = howto->type - R_XTENSA_OP0;
1539 /* Calculate the PC address for this instruction. */
1540 if (!howto->pc_relative)
1542 *error_message = "expected PC-relative relocation";
1543 return bfd_reloc_dangerous;
1546 self_address = (input_section->output_section->vma
1547 + input_section->output_offset
1548 + address);
1550 /* Apply the relocation. */
1551 operand = xtensa_get_operand (isa, opcode, opnd);
1552 newval = xtensa_operand_do_reloc (operand, relocation, self_address);
1553 encode_result = xtensa_operand_encode (operand, &newval);
1554 xtensa_operand_set_field (operand, ibuff, newval);
1556 /* Write the modified instruction back out of the buffer. */
1557 xtensa_insnbuf_to_chars (isa, ibuff, contents + address);
1558 free (ibuff);
1560 if (encode_result != xtensa_encode_result_ok)
1562 char *message = build_encoding_error_message (opcode, encode_result);
1563 *error_message = message;
1564 return bfd_reloc_dangerous;
1567 /* Final check for call. */
1568 if (is_direct_call_opcode (opcode)
1569 && is_windowed_call_opcode (opcode))
1571 if ((self_address >> CALL_SEGMENT_BITS) !=
1572 (relocation >> CALL_SEGMENT_BITS))
1574 *error_message = "windowed call crosses 1GB boundary; "
1575 "return may fail";
1576 return bfd_reloc_dangerous;
1580 return bfd_reloc_ok;
1584 static char *
1585 vsprint_msg VPARAMS ((const char *origmsg, const char *fmt, int arglen, ...))
1587 /* To reduce the size of the memory leak,
1588 we only use a single message buffer. */
1589 static bfd_size_type alloc_size = 0;
1590 static char *message = NULL;
1591 bfd_size_type orig_len, len = 0;
1592 bfd_boolean is_append;
1594 VA_OPEN (ap, arglen);
1595 VA_FIXEDARG (ap, const char *, origmsg);
1597 is_append = (origmsg == message);
1599 orig_len = strlen (origmsg);
1600 len = orig_len + strlen (fmt) + arglen + 20;
1601 if (len > alloc_size)
1603 message = (char *) bfd_realloc (message, len);
1604 alloc_size = len;
1606 if (!is_append)
1607 memcpy (message, origmsg, orig_len);
1608 vsprintf (message + orig_len, fmt, ap);
1609 VA_CLOSE (ap);
1610 return message;
1614 static char *
1615 build_encoding_error_message (opcode, encode_result)
1616 xtensa_opcode opcode;
1617 xtensa_encode_result encode_result;
1619 const char *opname = xtensa_opcode_name (xtensa_default_isa, opcode);
1620 const char *msg = NULL;
1622 switch (encode_result)
1624 case xtensa_encode_result_ok:
1625 msg = "unexpected valid encoding";
1626 break;
1627 case xtensa_encode_result_align:
1628 msg = "misaligned encoding";
1629 break;
1630 case xtensa_encode_result_not_in_table:
1631 msg = "encoding not in lookup table";
1632 break;
1633 case xtensa_encode_result_too_low:
1634 msg = "encoding out of range: too low";
1635 break;
1636 case xtensa_encode_result_too_high:
1637 msg = "encoding out of range: too high";
1638 break;
1639 case xtensa_encode_result_not_ok:
1640 default:
1641 msg = "could not encode";
1642 break;
1645 if (is_direct_call_opcode (opcode)
1646 && (encode_result == xtensa_encode_result_too_low
1647 || encode_result == xtensa_encode_result_too_high))
1649 msg = "direct call out of range";
1651 else if (opcode == get_l32r_opcode ())
1653 /* L32Rs have the strange interaction with encoding in that they
1654 have an unsigned immediate field, so libisa returns "too high"
1655 when the absolute value is out of range and never returns "too
1656 low", but I leave the "too low" message in case anything
1657 changes. */
1658 if (encode_result == xtensa_encode_result_too_low)
1659 msg = "literal out of range";
1660 else if (encode_result == xtensa_encode_result_too_high)
1661 msg = "literal placed after use";
1664 return vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
1668 /* This function is registered as the "special_function" in the
1669 Xtensa howto for handling simplify operations.
1670 bfd_perform_relocation / bfd_install_relocation use it to
1671 perform (install) the specified relocation. Since this replaces the code
1672 in bfd_perform_relocation, it is basically an Xtensa-specific,
1673 stripped-down version of bfd_perform_relocation. */
1675 static bfd_reloc_status_type
1676 bfd_elf_xtensa_reloc (abfd, reloc_entry, symbol, data, input_section,
1677 output_bfd, error_message)
1678 bfd *abfd;
1679 arelent *reloc_entry;
1680 asymbol *symbol;
1681 PTR data;
1682 asection *input_section;
1683 bfd *output_bfd;
1684 char **error_message;
1686 bfd_vma relocation;
1687 bfd_reloc_status_type flag;
1688 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1689 bfd_vma output_base = 0;
1690 reloc_howto_type *howto = reloc_entry->howto;
1691 asection *reloc_target_output_section;
1692 bfd_boolean is_weak_undef;
1694 /* ELF relocs are against symbols. If we are producing relocatable
1695 output, and the reloc is against an external symbol, the resulting
1696 reloc will also be against the same symbol. In such a case, we
1697 don't want to change anything about the way the reloc is handled,
1698 since it will all be done at final link time. This test is similar
1699 to what bfd_elf_generic_reloc does except that it lets relocs with
1700 howto->partial_inplace go through even if the addend is non-zero.
1701 (The real problem is that partial_inplace is set for XTENSA_32
1702 relocs to begin with, but that's a long story and there's little we
1703 can do about it now....) */
1705 if (output_bfd != (bfd *) NULL
1706 && (symbol->flags & BSF_SECTION_SYM) == 0)
1708 reloc_entry->address += input_section->output_offset;
1709 return bfd_reloc_ok;
1712 /* Is the address of the relocation really within the section? */
1713 if (reloc_entry->address > (input_section->_cooked_size
1714 / bfd_octets_per_byte (abfd)))
1715 return bfd_reloc_outofrange;
1717 /* Work out which section the relocation is targeted at and the
1718 initial relocation command value. */
1720 /* Get symbol value. (Common symbols are special.) */
1721 if (bfd_is_com_section (symbol->section))
1722 relocation = 0;
1723 else
1724 relocation = symbol->value;
1726 reloc_target_output_section = symbol->section->output_section;
1728 /* Convert input-section-relative symbol value to absolute. */
1729 if ((output_bfd && !howto->partial_inplace)
1730 || reloc_target_output_section == NULL)
1731 output_base = 0;
1732 else
1733 output_base = reloc_target_output_section->vma;
1735 relocation += output_base + symbol->section->output_offset;
1737 /* Add in supplied addend. */
1738 relocation += reloc_entry->addend;
1740 /* Here the variable relocation holds the final address of the
1741 symbol we are relocating against, plus any addend. */
1742 if (output_bfd)
1744 if (!howto->partial_inplace)
1746 /* This is a partial relocation, and we want to apply the relocation
1747 to the reloc entry rather than the raw data. Everything except
1748 relocations against section symbols has already been handled
1749 above. */
1751 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
1752 reloc_entry->addend = relocation;
1753 reloc_entry->address += input_section->output_offset;
1754 return bfd_reloc_ok;
1756 else
1758 reloc_entry->address += input_section->output_offset;
1759 reloc_entry->addend = 0;
1763 is_weak_undef = (bfd_is_und_section (symbol->section)
1764 && (symbol->flags & BSF_WEAK) != 0);
1765 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1766 (bfd_byte *) data, (bfd_vma) octets,
1767 is_weak_undef, error_message);
1769 if (flag == bfd_reloc_dangerous)
1771 /* Add the symbol name to the error message. */
1772 if (! *error_message)
1773 *error_message = "";
1774 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
1775 strlen (symbol->name) + 17,
1776 symbol->name, reloc_entry->addend);
1779 return flag;
1783 /* Set up an entry in the procedure linkage table. */
1785 static bfd_vma
1786 elf_xtensa_create_plt_entry (dynobj, output_bfd, reloc_index)
1787 bfd *dynobj;
1788 bfd *output_bfd;
1789 unsigned reloc_index;
1791 asection *splt, *sgotplt;
1792 bfd_vma plt_base, got_base;
1793 bfd_vma code_offset, lit_offset;
1794 int chunk;
1796 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
1797 splt = elf_xtensa_get_plt_section (dynobj, chunk);
1798 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1799 BFD_ASSERT (splt != NULL && sgotplt != NULL);
1801 plt_base = splt->output_section->vma + splt->output_offset;
1802 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1804 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
1805 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
1807 /* Fill in the literal entry. This is the offset of the dynamic
1808 relocation entry. */
1809 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
1810 sgotplt->contents + lit_offset);
1812 /* Fill in the entry in the procedure linkage table. */
1813 memcpy (splt->contents + code_offset,
1814 (bfd_big_endian (output_bfd)
1815 ? elf_xtensa_be_plt_entry
1816 : elf_xtensa_le_plt_entry),
1817 PLT_ENTRY_SIZE);
1818 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
1819 plt_base + code_offset + 3),
1820 splt->contents + code_offset + 4);
1821 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
1822 plt_base + code_offset + 6),
1823 splt->contents + code_offset + 7);
1824 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
1825 plt_base + code_offset + 9),
1826 splt->contents + code_offset + 10);
1828 return plt_base + code_offset;
1832 /* Relocate an Xtensa ELF section. This is invoked by the linker for
1833 both relocatable and final links. */
1835 static bfd_boolean
1836 elf_xtensa_relocate_section (output_bfd, info, input_bfd,
1837 input_section, contents, relocs,
1838 local_syms, local_sections)
1839 bfd *output_bfd;
1840 struct bfd_link_info *info;
1841 bfd *input_bfd;
1842 asection *input_section;
1843 bfd_byte *contents;
1844 Elf_Internal_Rela *relocs;
1845 Elf_Internal_Sym *local_syms;
1846 asection **local_sections;
1848 Elf_Internal_Shdr *symtab_hdr;
1849 Elf_Internal_Rela *rel;
1850 Elf_Internal_Rela *relend;
1851 struct elf_link_hash_entry **sym_hashes;
1852 asection *srelgot, *srelplt;
1853 bfd *dynobj;
1854 char *error_message = NULL;
1856 if (xtensa_default_isa == NULL)
1857 xtensa_isa_init ();
1859 dynobj = elf_hash_table (info)->dynobj;
1860 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1861 sym_hashes = elf_sym_hashes (input_bfd);
1863 srelgot = NULL;
1864 srelplt = NULL;
1865 if (dynobj != NULL)
1867 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
1868 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1871 rel = relocs;
1872 relend = relocs + input_section->reloc_count;
1873 for (; rel < relend; rel++)
1875 int r_type;
1876 reloc_howto_type *howto;
1877 unsigned long r_symndx;
1878 struct elf_link_hash_entry *h;
1879 Elf_Internal_Sym *sym;
1880 asection *sec;
1881 bfd_vma relocation;
1882 bfd_reloc_status_type r;
1883 bfd_boolean is_weak_undef;
1884 bfd_boolean unresolved_reloc;
1885 bfd_boolean warned;
1887 r_type = ELF32_R_TYPE (rel->r_info);
1888 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
1889 || r_type == (int) R_XTENSA_GNU_VTENTRY)
1890 continue;
1892 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
1894 bfd_set_error (bfd_error_bad_value);
1895 return FALSE;
1897 howto = &elf_howto_table[r_type];
1899 r_symndx = ELF32_R_SYM (rel->r_info);
1901 if (info->relocatable)
1903 /* This is a relocatable link.
1904 1) If the reloc is against a section symbol, adjust
1905 according to the output section.
1906 2) If there is a new target for this relocation,
1907 the new target will be in the same output section.
1908 We adjust the relocation by the output section
1909 difference. */
1911 if (relaxing_section)
1913 /* Check if this references a section in another input file. */
1914 do_fix_for_relocatable_link (rel, input_bfd, input_section);
1915 r_type = ELF32_R_TYPE (rel->r_info);
1918 if (r_type == R_XTENSA_ASM_SIMPLIFY)
1920 /* Convert ASM_SIMPLIFY into the simpler relocation
1921 so that they never escape a relaxing link. */
1922 contract_asm_expansion (contents, input_section->_raw_size, rel);
1923 r_type = ELF32_R_TYPE (rel->r_info);
1926 /* This is a relocatable link, so we don't have to change
1927 anything unless the reloc is against a section symbol,
1928 in which case we have to adjust according to where the
1929 section symbol winds up in the output section. */
1930 if (r_symndx < symtab_hdr->sh_info)
1932 sym = local_syms + r_symndx;
1933 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1935 sec = local_sections[r_symndx];
1936 rel->r_addend += sec->output_offset + sym->st_value;
1940 /* If there is an addend with a partial_inplace howto,
1941 then move the addend to the contents. This is a hack
1942 to work around problems with DWARF in relocatable links
1943 with some previous version of BFD. Now we can't easily get
1944 rid of the hack without breaking backward compatibility.... */
1945 if (rel->r_addend)
1947 howto = &elf_howto_table[r_type];
1948 if (howto->partial_inplace)
1950 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
1951 rel->r_addend, contents,
1952 rel->r_offset, FALSE,
1953 &error_message);
1954 if (r != bfd_reloc_ok)
1956 if (!((*info->callbacks->reloc_dangerous)
1957 (info, error_message, input_bfd, input_section,
1958 rel->r_offset)))
1959 return FALSE;
1961 rel->r_addend = 0;
1965 /* Done with work for relocatable link; continue with next reloc. */
1966 continue;
1969 /* This is a final link. */
1971 h = NULL;
1972 sym = NULL;
1973 sec = NULL;
1974 is_weak_undef = FALSE;
1975 unresolved_reloc = FALSE;
1976 warned = FALSE;
1978 if (howto->partial_inplace)
1980 /* Because R_XTENSA_32 was made partial_inplace to fix some
1981 problems with DWARF info in partial links, there may be
1982 an addend stored in the contents. Take it out of there
1983 and move it back into the addend field of the reloc. */
1984 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
1985 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
1988 if (r_symndx < symtab_hdr->sh_info)
1990 sym = local_syms + r_symndx;
1991 sec = local_sections[r_symndx];
1992 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1994 else
1996 RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx,
1997 symtab_hdr, relocation, sec,
1998 unresolved_reloc, info,
1999 warned);
2001 if (relocation == 0
2002 && !unresolved_reloc
2003 && h->root.type == bfd_link_hash_undefweak)
2004 is_weak_undef = TRUE;
2007 if (relaxing_section)
2009 /* Check if this references a section in another input file. */
2010 do_fix_for_final_link (rel, input_section, &relocation);
2012 /* Update some already cached values. */
2013 r_type = ELF32_R_TYPE (rel->r_info);
2014 howto = &elf_howto_table[r_type];
2017 /* Sanity check the address. */
2018 if (rel->r_offset >= input_section->_raw_size
2019 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2021 bfd_set_error (bfd_error_bad_value);
2022 return FALSE;
2025 /* Generate dynamic relocations. */
2026 if (elf_hash_table (info)->dynamic_sections_created)
2028 bfd_boolean dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
2030 if (dynamic_symbol && (r_type == R_XTENSA_OP0
2031 || r_type == R_XTENSA_OP1
2032 || r_type == R_XTENSA_OP2))
2034 /* This is an error. The symbol's real value won't be known
2035 until runtime and it's likely to be out of range anyway. */
2036 const char *name = h->root.root.string;
2037 error_message = vsprint_msg ("invalid relocation for dynamic "
2038 "symbol", ": %s",
2039 strlen (name) + 2, name);
2040 if (!((*info->callbacks->reloc_dangerous)
2041 (info, error_message, input_bfd, input_section,
2042 rel->r_offset)))
2043 return FALSE;
2045 else if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
2046 && (input_section->flags & SEC_ALLOC) != 0
2047 && (dynamic_symbol || info->shared))
2049 Elf_Internal_Rela outrel;
2050 bfd_byte *loc;
2051 asection *srel;
2053 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2054 srel = srelplt;
2055 else
2056 srel = srelgot;
2058 BFD_ASSERT (srel != NULL);
2060 outrel.r_offset =
2061 _bfd_elf_section_offset (output_bfd, info,
2062 input_section, rel->r_offset);
2064 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2065 memset (&outrel, 0, sizeof outrel);
2066 else
2068 outrel.r_offset += (input_section->output_section->vma
2069 + input_section->output_offset);
2071 if (dynamic_symbol)
2073 outrel.r_addend = rel->r_addend;
2074 rel->r_addend = 0;
2076 if (r_type == R_XTENSA_32)
2078 outrel.r_info =
2079 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2080 relocation = 0;
2082 else /* r_type == R_XTENSA_PLT */
2084 outrel.r_info =
2085 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2087 /* Create the PLT entry and set the initial
2088 contents of the literal entry to the address of
2089 the PLT entry. */
2090 relocation =
2091 elf_xtensa_create_plt_entry (dynobj, output_bfd,
2092 srel->reloc_count);
2094 unresolved_reloc = FALSE;
2096 else
2098 /* Generate a RELATIVE relocation. */
2099 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2100 outrel.r_addend = 0;
2104 loc = (srel->contents
2105 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2106 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2107 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2108 <= srel->_cooked_size);
2112 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2113 because such sections are not SEC_ALLOC and thus ld.so will
2114 not process them. */
2115 if (unresolved_reloc
2116 && !((input_section->flags & SEC_DEBUGGING) != 0
2117 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2118 (*_bfd_error_handler)
2119 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2120 bfd_archive_filename (input_bfd),
2121 bfd_get_section_name (input_bfd, input_section),
2122 (long) rel->r_offset,
2123 h->root.root.string);
2125 /* There's no point in calling bfd_perform_relocation here.
2126 Just go directly to our "special function". */
2127 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2128 relocation + rel->r_addend,
2129 contents, rel->r_offset, is_weak_undef,
2130 &error_message);
2132 if (r != bfd_reloc_ok && !warned)
2134 const char *name;
2136 BFD_ASSERT (r == bfd_reloc_dangerous);
2137 BFD_ASSERT (error_message != (char *) NULL);
2139 if (h != NULL)
2140 name = h->root.root.string;
2141 else
2143 name = bfd_elf_string_from_elf_section
2144 (input_bfd, symtab_hdr->sh_link, sym->st_name);
2145 if (name && *name == '\0')
2146 name = bfd_section_name (input_bfd, sec);
2148 if (name)
2149 error_message = vsprint_msg (error_message, ": %s",
2150 strlen (name), name);
2151 if (!((*info->callbacks->reloc_dangerous)
2152 (info, error_message, input_bfd, input_section,
2153 rel->r_offset)))
2154 return FALSE;
2158 return TRUE;
2162 /* Finish up dynamic symbol handling. There's not much to do here since
2163 the PLT and GOT entries are all set up by relocate_section. */
2165 static bfd_boolean
2166 elf_xtensa_finish_dynamic_symbol (output_bfd, info, h, sym)
2167 bfd *output_bfd ATTRIBUTE_UNUSED;
2168 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2169 struct elf_link_hash_entry *h;
2170 Elf_Internal_Sym *sym;
2172 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2173 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2175 /* Mark the symbol as undefined, rather than as defined in
2176 the .plt section. Leave the value alone. */
2177 sym->st_shndx = SHN_UNDEF;
2180 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2181 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2182 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2183 sym->st_shndx = SHN_ABS;
2185 return TRUE;
2189 /* Combine adjacent literal table entries in the output. Adjacent
2190 entries within each input section may have been removed during
2191 relaxation, but we repeat the process here, even though it's too late
2192 to shrink the output section, because it's important to minimize the
2193 number of literal table entries to reduce the start-up work for the
2194 runtime linker. Returns the number of remaining table entries or -1
2195 on error. */
2197 static int
2198 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc)
2199 bfd *output_bfd;
2200 asection *sxtlit;
2201 asection *sgotloc;
2203 bfd_byte *contents;
2204 property_table_entry *table;
2205 bfd_size_type section_size, sgotloc_size;
2206 bfd_vma offset;
2207 int n, m, num;
2209 section_size = (sxtlit->_cooked_size != 0
2210 ? sxtlit->_cooked_size : sxtlit->_raw_size);
2211 BFD_ASSERT (section_size % 8 == 0);
2212 num = section_size / 8;
2214 sgotloc_size = (sgotloc->_cooked_size != 0
2215 ? sgotloc->_cooked_size : sgotloc->_raw_size);
2216 if (sgotloc_size != section_size)
2218 (*_bfd_error_handler)
2219 ("internal inconsistency in size of .got.loc section");
2220 return -1;
2223 contents = (bfd_byte *) bfd_malloc (section_size);
2224 table = (property_table_entry *)
2225 bfd_malloc (num * sizeof (property_table_entry));
2226 if (contents == 0 || table == 0)
2227 return -1;
2229 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
2230 propagates to the output section, where it doesn't really apply and
2231 where it breaks the following call to bfd_get_section_contents. */
2232 sxtlit->flags &= ~SEC_IN_MEMORY;
2234 if (! bfd_get_section_contents (output_bfd, sxtlit, contents, 0,
2235 section_size))
2236 return -1;
2238 /* There should never be any relocations left at this point, so this
2239 is quite a bit easier than what is done during relaxation. */
2241 /* Copy the raw contents into a property table array and sort it. */
2242 offset = 0;
2243 for (n = 0; n < num; n++)
2245 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
2246 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
2247 offset += 8;
2249 qsort (table, num, sizeof (property_table_entry), property_table_compare);
2251 for (n = 0; n < num; n++)
2253 bfd_boolean remove = FALSE;
2255 if (table[n].size == 0)
2256 remove = TRUE;
2257 else if (n > 0 &&
2258 (table[n-1].address + table[n-1].size == table[n].address))
2260 table[n-1].size += table[n].size;
2261 remove = TRUE;
2264 if (remove)
2266 for (m = n; m < num - 1; m++)
2268 table[m].address = table[m+1].address;
2269 table[m].size = table[m+1].size;
2272 n--;
2273 num--;
2277 /* Copy the data back to the raw contents. */
2278 offset = 0;
2279 for (n = 0; n < num; n++)
2281 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
2282 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
2283 offset += 8;
2286 /* Clear the removed bytes. */
2287 if ((bfd_size_type) (num * 8) < section_size)
2289 memset (&contents[num * 8], 0, section_size - num * 8);
2290 sxtlit->_cooked_size = num * 8;
2293 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
2294 section_size))
2295 return -1;
2297 /* Copy the contents to ".got.loc". */
2298 memcpy (sgotloc->contents, contents, section_size);
2300 free (contents);
2301 free (table);
2302 return num;
2306 /* Finish up the dynamic sections. */
2308 static bfd_boolean
2309 elf_xtensa_finish_dynamic_sections (output_bfd, info)
2310 bfd *output_bfd;
2311 struct bfd_link_info *info;
2313 bfd *dynobj;
2314 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
2315 Elf32_External_Dyn *dyncon, *dynconend;
2316 int num_xtlit_entries;
2318 if (! elf_hash_table (info)->dynamic_sections_created)
2319 return TRUE;
2321 dynobj = elf_hash_table (info)->dynobj;
2322 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2323 BFD_ASSERT (sdyn != NULL);
2325 /* Set the first entry in the global offset table to the address of
2326 the dynamic section. */
2327 sgot = bfd_get_section_by_name (dynobj, ".got");
2328 if (sgot)
2330 BFD_ASSERT (sgot->_raw_size == 4);
2331 if (sdyn == NULL)
2332 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2333 else
2334 bfd_put_32 (output_bfd,
2335 sdyn->output_section->vma + sdyn->output_offset,
2336 sgot->contents);
2339 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2340 if (srelplt != NULL && srelplt->_raw_size != 0)
2342 asection *sgotplt, *srelgot, *spltlittbl;
2343 int chunk, plt_chunks, plt_entries;
2344 Elf_Internal_Rela irela;
2345 bfd_byte *loc;
2346 unsigned rtld_reloc;
2348 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
2349 BFD_ASSERT (srelgot != NULL);
2351 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
2352 BFD_ASSERT (spltlittbl != NULL);
2354 /* Find the first XTENSA_RTLD relocation. Presumably the rest
2355 of them follow immediately after.... */
2356 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
2358 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2359 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2360 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
2361 break;
2363 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
2365 plt_entries = (srelplt->_raw_size / sizeof (Elf32_External_Rela));
2366 plt_chunks =
2367 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
2369 for (chunk = 0; chunk < plt_chunks; chunk++)
2371 int chunk_entries = 0;
2373 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
2374 BFD_ASSERT (sgotplt != NULL);
2376 /* Emit special RTLD relocations for the first two entries in
2377 each chunk of the .got.plt section. */
2379 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2380 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2381 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2382 irela.r_offset = (sgotplt->output_section->vma
2383 + sgotplt->output_offset);
2384 irela.r_addend = 1; /* tell rtld to set value to resolver function */
2385 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2386 rtld_reloc += 1;
2387 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2389 /* Next literal immediately follows the first. */
2390 loc += sizeof (Elf32_External_Rela);
2391 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2392 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2393 irela.r_offset = (sgotplt->output_section->vma
2394 + sgotplt->output_offset + 4);
2395 /* Tell rtld to set value to object's link map. */
2396 irela.r_addend = 2;
2397 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2398 rtld_reloc += 1;
2399 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2401 /* Fill in the literal table. */
2402 if (chunk < plt_chunks - 1)
2403 chunk_entries = PLT_ENTRIES_PER_CHUNK;
2404 else
2405 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
2407 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->_cooked_size);
2408 bfd_put_32 (output_bfd,
2409 sgotplt->output_section->vma + sgotplt->output_offset,
2410 spltlittbl->contents + (chunk * 8) + 0);
2411 bfd_put_32 (output_bfd,
2412 8 + (chunk_entries * 4),
2413 spltlittbl->contents + (chunk * 8) + 4);
2416 /* All the dynamic relocations have been emitted at this point.
2417 Make sure the relocation sections are the correct size. */
2418 if (srelgot->_cooked_size != (sizeof (Elf32_External_Rela)
2419 * srelgot->reloc_count)
2420 || srelplt->_cooked_size != (sizeof (Elf32_External_Rela)
2421 * srelplt->reloc_count))
2422 abort ();
2424 /* The .xt.lit.plt section has just been modified. This must
2425 happen before the code below which combines adjacent literal
2426 table entries, and the .xt.lit.plt contents have to be forced to
2427 the output here. */
2428 if (! bfd_set_section_contents (output_bfd,
2429 spltlittbl->output_section,
2430 spltlittbl->contents,
2431 spltlittbl->output_offset,
2432 spltlittbl->_raw_size))
2433 return FALSE;
2434 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
2435 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
2438 /* Combine adjacent literal table entries. */
2439 BFD_ASSERT (! info->relocatable);
2440 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
2441 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
2442 BFD_ASSERT (sxtlit && sgotloc);
2443 num_xtlit_entries =
2444 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
2445 if (num_xtlit_entries < 0)
2446 return FALSE;
2448 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2449 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2450 for (; dyncon < dynconend; dyncon++)
2452 Elf_Internal_Dyn dyn;
2453 const char *name;
2454 asection *s;
2456 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2458 switch (dyn.d_tag)
2460 default:
2461 break;
2463 case DT_XTENSA_GOT_LOC_SZ:
2464 dyn.d_un.d_val = num_xtlit_entries;
2465 break;
2467 case DT_XTENSA_GOT_LOC_OFF:
2468 name = ".got.loc";
2469 goto get_vma;
2470 case DT_PLTGOT:
2471 name = ".got";
2472 goto get_vma;
2473 case DT_JMPREL:
2474 name = ".rela.plt";
2475 get_vma:
2476 s = bfd_get_section_by_name (output_bfd, name);
2477 BFD_ASSERT (s);
2478 dyn.d_un.d_ptr = s->vma;
2479 break;
2481 case DT_PLTRELSZ:
2482 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2483 BFD_ASSERT (s);
2484 dyn.d_un.d_val = (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2485 break;
2487 case DT_RELASZ:
2488 /* Adjust RELASZ to not include JMPREL. This matches what
2489 glibc expects and what is done for several other ELF
2490 targets (e.g., i386, alpha), but the "correct" behavior
2491 seems to be unresolved. Since the linker script arranges
2492 for .rela.plt to follow all other relocation sections, we
2493 don't have to worry about changing the DT_RELA entry. */
2494 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2495 if (s)
2497 dyn.d_un.d_val -=
2498 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2500 break;
2503 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2506 return TRUE;
2510 /* Functions for dealing with the e_flags field. */
2512 /* Merge backend specific data from an object file to the output
2513 object file when linking. */
2515 static bfd_boolean
2516 elf_xtensa_merge_private_bfd_data (ibfd, obfd)
2517 bfd *ibfd;
2518 bfd *obfd;
2520 unsigned out_mach, in_mach;
2521 flagword out_flag, in_flag;
2523 /* Check if we have the same endianess. */
2524 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
2525 return FALSE;
2527 /* Don't even pretend to support mixed-format linking. */
2528 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2529 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2530 return FALSE;
2532 out_flag = elf_elfheader (obfd)->e_flags;
2533 in_flag = elf_elfheader (ibfd)->e_flags;
2535 out_mach = out_flag & EF_XTENSA_MACH;
2536 in_mach = in_flag & EF_XTENSA_MACH;
2537 if (out_mach != in_mach)
2539 (*_bfd_error_handler)
2540 ("%s: incompatible machine type. Output is 0x%x. Input is 0x%x",
2541 bfd_archive_filename (ibfd), out_mach, in_mach);
2542 bfd_set_error (bfd_error_wrong_format);
2543 return FALSE;
2546 if (! elf_flags_init (obfd))
2548 elf_flags_init (obfd) = TRUE;
2549 elf_elfheader (obfd)->e_flags = in_flag;
2551 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2552 && bfd_get_arch_info (obfd)->the_default)
2553 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2554 bfd_get_mach (ibfd));
2556 return TRUE;
2559 if ((out_flag & EF_XTENSA_XT_INSN) !=
2560 (in_flag & EF_XTENSA_XT_INSN))
2561 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
2563 if ((out_flag & EF_XTENSA_XT_LIT) !=
2564 (in_flag & EF_XTENSA_XT_LIT))
2565 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
2567 return TRUE;
2571 static bfd_boolean
2572 elf_xtensa_set_private_flags (abfd, flags)
2573 bfd *abfd;
2574 flagword flags;
2576 BFD_ASSERT (!elf_flags_init (abfd)
2577 || elf_elfheader (abfd)->e_flags == flags);
2579 elf_elfheader (abfd)->e_flags |= flags;
2580 elf_flags_init (abfd) = TRUE;
2582 return TRUE;
2586 extern flagword
2587 elf_xtensa_get_private_bfd_flags (abfd)
2588 bfd *abfd;
2590 return elf_elfheader (abfd)->e_flags;
2594 static bfd_boolean
2595 elf_xtensa_print_private_bfd_data (abfd, farg)
2596 bfd *abfd;
2597 PTR farg;
2599 FILE *f = (FILE *) farg;
2600 flagword e_flags = elf_elfheader (abfd)->e_flags;
2602 fprintf (f, "\nXtensa header:\n");
2603 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
2604 fprintf (f, "\nMachine = Base\n");
2605 else
2606 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
2608 fprintf (f, "Insn tables = %s\n",
2609 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
2611 fprintf (f, "Literal tables = %s\n",
2612 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
2614 return _bfd_elf_print_private_bfd_data (abfd, farg);
2618 /* Set the right machine number for an Xtensa ELF file. */
2620 static bfd_boolean
2621 elf_xtensa_object_p (abfd)
2622 bfd *abfd;
2624 int mach;
2625 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
2627 switch (arch)
2629 case E_XTENSA_MACH:
2630 mach = bfd_mach_xtensa;
2631 break;
2632 default:
2633 return FALSE;
2636 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
2637 return TRUE;
2641 /* The final processing done just before writing out an Xtensa ELF object
2642 file. This gets the Xtensa architecture right based on the machine
2643 number. */
2645 static void
2646 elf_xtensa_final_write_processing (abfd, linker)
2647 bfd *abfd;
2648 bfd_boolean linker ATTRIBUTE_UNUSED;
2650 int mach;
2651 unsigned long val;
2653 switch (mach = bfd_get_mach (abfd))
2655 case bfd_mach_xtensa:
2656 val = E_XTENSA_MACH;
2657 break;
2658 default:
2659 return;
2662 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
2663 elf_elfheader (abfd)->e_flags |= val;
2667 static enum elf_reloc_type_class
2668 elf_xtensa_reloc_type_class (rela)
2669 const Elf_Internal_Rela *rela;
2671 switch ((int) ELF32_R_TYPE (rela->r_info))
2673 case R_XTENSA_RELATIVE:
2674 return reloc_class_relative;
2675 case R_XTENSA_JMP_SLOT:
2676 return reloc_class_plt;
2677 default:
2678 return reloc_class_normal;
2683 static bfd_boolean
2684 elf_xtensa_discard_info_for_section (abfd, cookie, info, sec)
2685 bfd *abfd;
2686 struct elf_reloc_cookie *cookie;
2687 struct bfd_link_info *info;
2688 asection *sec;
2690 bfd_byte *contents;
2691 bfd_vma section_size;
2692 bfd_vma offset, actual_offset;
2693 size_t removed_bytes = 0;
2695 section_size = (sec->_cooked_size ? sec->_cooked_size : sec->_raw_size);
2696 if (section_size == 0 || section_size % 8 != 0)
2697 return FALSE;
2699 if (sec->output_section
2700 && bfd_is_abs_section (sec->output_section))
2701 return FALSE;
2703 contents = retrieve_contents (abfd, sec, info->keep_memory);
2704 if (!contents)
2705 return FALSE;
2707 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
2708 if (!cookie->rels)
2710 release_contents (sec, contents);
2711 return FALSE;
2714 cookie->rel = cookie->rels;
2715 cookie->relend = cookie->rels + sec->reloc_count;
2717 for (offset = 0; offset < section_size; offset += 8)
2719 actual_offset = offset - removed_bytes;
2721 /* The ...symbol_deleted_p function will skip over relocs but it
2722 won't adjust their offsets, so do that here. */
2723 while (cookie->rel < cookie->relend
2724 && cookie->rel->r_offset < offset)
2726 cookie->rel->r_offset -= removed_bytes;
2727 cookie->rel++;
2730 while (cookie->rel < cookie->relend
2731 && cookie->rel->r_offset == offset)
2733 if (_bfd_elf32_reloc_symbol_deleted_p (offset, cookie))
2735 /* Remove the table entry. (If the reloc type is NONE, then
2736 the entry has already been merged with another and deleted
2737 during relaxation.) */
2738 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
2740 /* Shift the contents up. */
2741 if (offset + 8 < section_size)
2742 memmove (&contents[actual_offset],
2743 &contents[actual_offset+8],
2744 section_size - offset - 8);
2745 removed_bytes += 8;
2748 /* Remove this relocation. */
2749 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
2752 /* Adjust the relocation offset for previous removals. This
2753 should not be done before calling ...symbol_deleted_p
2754 because it might mess up the offset comparisons there.
2755 Make sure the offset doesn't underflow in the case where
2756 the first entry is removed. */
2757 if (cookie->rel->r_offset >= removed_bytes)
2758 cookie->rel->r_offset -= removed_bytes;
2759 else
2760 cookie->rel->r_offset = 0;
2762 cookie->rel++;
2766 if (removed_bytes != 0)
2768 /* Adjust any remaining relocs (shouldn't be any). */
2769 for (; cookie->rel < cookie->relend; cookie->rel++)
2771 if (cookie->rel->r_offset >= removed_bytes)
2772 cookie->rel->r_offset -= removed_bytes;
2773 else
2774 cookie->rel->r_offset = 0;
2777 /* Clear the removed bytes. */
2778 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
2780 pin_contents (sec, contents);
2781 pin_internal_relocs (sec, cookie->rels);
2783 sec->_cooked_size = section_size - removed_bytes;
2784 /* Also shrink _raw_size. See comments in relax_property_section. */
2785 sec->_raw_size = sec->_cooked_size;
2787 if (xtensa_is_littable_section (sec))
2789 bfd *dynobj = elf_hash_table (info)->dynobj;
2790 if (dynobj)
2792 asection *sgotloc =
2793 bfd_get_section_by_name (dynobj, ".got.loc");
2794 if (sgotloc)
2796 bfd_size_type sgotloc_size =
2797 (sgotloc->_cooked_size ? sgotloc->_cooked_size
2798 : sgotloc->_raw_size);
2799 sgotloc->_cooked_size = sgotloc_size - removed_bytes;
2800 sgotloc->_raw_size = sgotloc_size - removed_bytes;
2805 else
2807 release_contents (sec, contents);
2808 release_internal_relocs (sec, cookie->rels);
2811 return (removed_bytes != 0);
2815 static bfd_boolean
2816 elf_xtensa_discard_info (abfd, cookie, info)
2817 bfd *abfd;
2818 struct elf_reloc_cookie *cookie;
2819 struct bfd_link_info *info;
2821 asection *sec;
2822 bfd_boolean changed = FALSE;
2824 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2826 if (xtensa_is_property_section (sec))
2828 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
2829 changed = TRUE;
2833 return changed;
2837 static bfd_boolean
2838 elf_xtensa_ignore_discarded_relocs (sec)
2839 asection *sec;
2841 return xtensa_is_property_section (sec);
2845 /* Support for core dump NOTE sections. */
2847 static bfd_boolean
2848 elf_xtensa_grok_prstatus (abfd, note)
2849 bfd *abfd;
2850 Elf_Internal_Note *note;
2852 int offset;
2853 unsigned int raw_size;
2855 /* The size for Xtensa is variable, so don't try to recognize the format
2856 based on the size. Just assume this is GNU/Linux. */
2858 /* pr_cursig */
2859 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2861 /* pr_pid */
2862 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
2864 /* pr_reg */
2865 offset = 72;
2866 raw_size = note->descsz - offset - 4;
2868 /* Make a ".reg/999" section. */
2869 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2870 raw_size, note->descpos + offset);
2874 static bfd_boolean
2875 elf_xtensa_grok_psinfo (abfd, note)
2876 bfd *abfd;
2877 Elf_Internal_Note *note;
2879 switch (note->descsz)
2881 default:
2882 return FALSE;
2884 case 128: /* GNU/Linux elf_prpsinfo */
2885 elf_tdata (abfd)->core_program
2886 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
2887 elf_tdata (abfd)->core_command
2888 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
2891 /* Note that for some reason, a spurious space is tacked
2892 onto the end of the args in some (at least one anyway)
2893 implementations, so strip it off if it exists. */
2896 char *command = elf_tdata (abfd)->core_command;
2897 int n = strlen (command);
2899 if (0 < n && command[n - 1] == ' ')
2900 command[n - 1] = '\0';
2903 return TRUE;
2907 /* Generic Xtensa configurability stuff. */
2909 static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
2910 static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
2911 static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
2912 static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
2913 static xtensa_opcode call0_op = XTENSA_UNDEFINED;
2914 static xtensa_opcode call4_op = XTENSA_UNDEFINED;
2915 static xtensa_opcode call8_op = XTENSA_UNDEFINED;
2916 static xtensa_opcode call12_op = XTENSA_UNDEFINED;
2918 static void
2919 init_call_opcodes ()
2921 if (callx0_op == XTENSA_UNDEFINED)
2923 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
2924 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
2925 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
2926 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
2927 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
2928 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
2929 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
2930 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
2935 static bfd_boolean
2936 is_indirect_call_opcode (opcode)
2937 xtensa_opcode opcode;
2939 init_call_opcodes ();
2940 return (opcode == callx0_op
2941 || opcode == callx4_op
2942 || opcode == callx8_op
2943 || opcode == callx12_op);
2947 static bfd_boolean
2948 is_direct_call_opcode (opcode)
2949 xtensa_opcode opcode;
2951 init_call_opcodes ();
2952 return (opcode == call0_op
2953 || opcode == call4_op
2954 || opcode == call8_op
2955 || opcode == call12_op);
2959 static bfd_boolean
2960 is_windowed_call_opcode (opcode)
2961 xtensa_opcode opcode;
2963 init_call_opcodes ();
2964 return (opcode == call4_op
2965 || opcode == call8_op
2966 || opcode == call12_op
2967 || opcode == callx4_op
2968 || opcode == callx8_op
2969 || opcode == callx12_op);
2973 static xtensa_opcode
2974 get_l32r_opcode (void)
2976 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
2977 if (l32r_opcode == XTENSA_UNDEFINED)
2979 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
2980 BFD_ASSERT (l32r_opcode != XTENSA_UNDEFINED);
2982 return l32r_opcode;
2986 static bfd_vma
2987 l32r_offset (addr, pc)
2988 bfd_vma addr;
2989 bfd_vma pc;
2991 bfd_vma offset;
2993 offset = addr - ((pc+3) & -4);
2994 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
2995 offset = (signed int) offset >> 2;
2996 BFD_ASSERT ((signed int) offset >> 16 == -1);
2997 return offset;
3001 /* Get the operand number for a PC-relative relocation.
3002 If the relocation is not a PC-relative one, return (-1). */
3004 static int
3005 get_relocation_opnd (irel)
3006 Elf_Internal_Rela *irel;
3008 if (ELF32_R_TYPE (irel->r_info) < R_XTENSA_OP0
3009 || ELF32_R_TYPE (irel->r_info) >= R_XTENSA_max)
3010 return -1;
3011 return ELF32_R_TYPE (irel->r_info) - R_XTENSA_OP0;
3015 /* Get the opcode for a relocation. */
3017 static xtensa_opcode
3018 get_relocation_opcode (sec, contents, irel)
3019 asection *sec;
3020 bfd_byte *contents;
3021 Elf_Internal_Rela *irel;
3023 static xtensa_insnbuf ibuff = NULL;
3024 xtensa_isa isa = xtensa_default_isa;
3026 if (get_relocation_opnd (irel) == -1)
3027 return XTENSA_UNDEFINED;
3029 if (contents == NULL)
3030 return XTENSA_UNDEFINED;
3032 if (sec->_raw_size <= irel->r_offset)
3033 return XTENSA_UNDEFINED;
3035 if (ibuff == NULL)
3036 ibuff = xtensa_insnbuf_alloc (isa);
3038 /* Decode the instruction. */
3039 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset]);
3040 return xtensa_decode_insn (isa, ibuff);
3044 bfd_boolean
3045 is_l32r_relocation (sec, contents, irel)
3046 asection *sec;
3047 bfd_byte *contents;
3048 Elf_Internal_Rela *irel;
3050 xtensa_opcode opcode;
3052 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_OP1)
3053 return FALSE;
3055 opcode = get_relocation_opcode (sec, contents, irel);
3056 return (opcode == get_l32r_opcode ());
3060 /* Code for transforming CALLs at link-time. */
3062 static bfd_reloc_status_type
3063 elf_xtensa_do_asm_simplify (contents, address, content_length)
3064 bfd_byte *contents;
3065 bfd_vma address;
3066 bfd_vma content_length;
3068 static xtensa_insnbuf insnbuf = NULL;
3069 xtensa_opcode opcode;
3070 xtensa_operand operand;
3071 xtensa_opcode direct_call_opcode;
3072 xtensa_isa isa = xtensa_default_isa;
3073 bfd_byte *chbuf = contents + address;
3074 int opn;
3076 if (insnbuf == NULL)
3077 insnbuf = xtensa_insnbuf_alloc (isa);
3079 if (content_length < address)
3081 (*_bfd_error_handler)
3082 ("Attempt to convert L32R/CALLX to CALL failed");
3083 return bfd_reloc_other;
3086 opcode = get_expanded_call_opcode (chbuf, content_length - address);
3087 direct_call_opcode = swap_callx_for_call_opcode (opcode);
3088 if (direct_call_opcode == XTENSA_UNDEFINED)
3090 (*_bfd_error_handler)
3091 ("Attempt to convert L32R/CALLX to CALL failed");
3092 return bfd_reloc_other;
3095 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
3096 opcode = xtensa_opcode_lookup (isa, "or");
3097 xtensa_encode_insn (isa, opcode, insnbuf);
3098 for (opn = 0; opn < 3; opn++)
3100 operand = xtensa_get_operand (isa, opcode, opn);
3101 xtensa_operand_set_field (operand, insnbuf, 1);
3103 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf);
3105 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
3106 xtensa_encode_insn (isa, direct_call_opcode, insnbuf);
3107 operand = xtensa_get_operand (isa, opcode, 0);
3108 xtensa_operand_set_field (operand, insnbuf, 0);
3109 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3);
3111 return bfd_reloc_ok;
3115 static bfd_reloc_status_type
3116 contract_asm_expansion (contents, content_length, irel)
3117 bfd_byte *contents;
3118 bfd_vma content_length;
3119 Elf_Internal_Rela *irel;
3121 bfd_reloc_status_type retval =
3122 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length);
3124 if (retval != bfd_reloc_ok)
3125 return retval;
3127 /* Update the irel->r_offset field so that the right immediate and
3128 the right instruction are modified during the relocation. */
3129 irel->r_offset += 3;
3130 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_OP0);
3131 return bfd_reloc_ok;
3135 static xtensa_opcode
3136 swap_callx_for_call_opcode (opcode)
3137 xtensa_opcode opcode;
3139 init_call_opcodes ();
3141 if (opcode == callx0_op) return call0_op;
3142 if (opcode == callx4_op) return call4_op;
3143 if (opcode == callx8_op) return call8_op;
3144 if (opcode == callx12_op) return call12_op;
3146 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
3147 return XTENSA_UNDEFINED;
3151 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" sequence, and
3152 if so, return the CALLX opcode. If not, return XTENSA_UNDEFINED. */
3154 #define L32R_TARGET_REG_OPERAND 0
3155 #define CALLN_SOURCE_OPERAND 0
3157 static xtensa_opcode
3158 get_expanded_call_opcode (buf, bufsize)
3159 bfd_byte *buf;
3160 int bufsize;
3162 static xtensa_insnbuf insnbuf = NULL;
3163 xtensa_opcode opcode;
3164 xtensa_operand operand;
3165 xtensa_isa isa = xtensa_default_isa;
3166 uint32 regno, call_regno;
3168 /* Buffer must be at least 6 bytes. */
3169 if (bufsize < 6)
3170 return XTENSA_UNDEFINED;
3172 if (insnbuf == NULL)
3173 insnbuf = xtensa_insnbuf_alloc (isa);
3175 xtensa_insnbuf_from_chars (isa, insnbuf, buf);
3176 opcode = xtensa_decode_insn (isa, insnbuf);
3178 if (opcode != get_l32r_opcode ())
3179 return XTENSA_UNDEFINED;
3181 operand = xtensa_get_operand (isa, opcode, L32R_TARGET_REG_OPERAND);
3182 regno = xtensa_operand_decode
3183 (operand, xtensa_operand_get_field (operand, insnbuf));
3185 /* Next instruction should be an CALLXn with operand 0 == regno. */
3186 xtensa_insnbuf_from_chars (isa, insnbuf,
3187 buf + xtensa_insn_length (isa, opcode));
3188 opcode = xtensa_decode_insn (isa, insnbuf);
3190 if (!is_indirect_call_opcode (opcode))
3191 return XTENSA_UNDEFINED;
3193 operand = xtensa_get_operand (isa, opcode, CALLN_SOURCE_OPERAND);
3194 call_regno = xtensa_operand_decode
3195 (operand, xtensa_operand_get_field (operand, insnbuf));
3196 if (call_regno != regno)
3197 return XTENSA_UNDEFINED;
3199 return opcode;
3203 /* Data structures used during relaxation. */
3205 /* r_reloc: relocation values. */
3207 /* Through the relaxation process, we need to keep track of the values
3208 that will result from evaluating relocations. The standard ELF
3209 relocation structure is not sufficient for this purpose because we're
3210 operating on multiple input files at once, so we need to know which
3211 input file a relocation refers to. The r_reloc structure thus
3212 records both the input file (bfd) and ELF relocation.
3214 For efficiency, an r_reloc also contains a "target_offset" field to
3215 cache the target-section-relative offset value that is represented by
3216 the relocation. */
3218 typedef struct r_reloc_struct r_reloc;
3220 struct r_reloc_struct
3222 bfd *abfd;
3223 Elf_Internal_Rela rela;
3224 bfd_vma target_offset;
3227 static bfd_boolean r_reloc_is_const
3228 PARAMS ((const r_reloc *));
3229 static void r_reloc_init
3230 PARAMS ((r_reloc *, bfd *, Elf_Internal_Rela *));
3231 static bfd_vma r_reloc_get_target_offset
3232 PARAMS ((const r_reloc *));
3233 static asection *r_reloc_get_section
3234 PARAMS ((const r_reloc *));
3235 static bfd_boolean r_reloc_is_defined
3236 PARAMS ((const r_reloc *));
3237 static struct elf_link_hash_entry *r_reloc_get_hash_entry
3238 PARAMS ((const r_reloc *));
3241 /* The r_reloc structure is included by value in literal_value, but not
3242 every literal_value has an associated relocation -- some are simple
3243 constants. In such cases, we set all the fields in the r_reloc
3244 struct to zero. The r_reloc_is_const function should be used to
3245 detect this case. */
3247 static bfd_boolean
3248 r_reloc_is_const (r_rel)
3249 const r_reloc *r_rel;
3251 return (r_rel->abfd == NULL);
3255 static void
3256 r_reloc_init (r_rel, abfd, irel)
3257 r_reloc *r_rel;
3258 bfd *abfd;
3259 Elf_Internal_Rela *irel;
3261 if (irel != NULL)
3263 r_rel->rela = *irel;
3264 r_rel->abfd = abfd;
3265 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
3267 else
3268 memset (r_rel, 0, sizeof (r_reloc));
3272 static bfd_vma
3273 r_reloc_get_target_offset (r_rel)
3274 const r_reloc *r_rel;
3276 bfd_vma target_offset;
3277 unsigned long r_symndx;
3279 BFD_ASSERT (!r_reloc_is_const (r_rel));
3280 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3281 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
3282 return (target_offset + r_rel->rela.r_addend);
3286 static struct elf_link_hash_entry *
3287 r_reloc_get_hash_entry (r_rel)
3288 const r_reloc *r_rel;
3290 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3291 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
3295 static asection *
3296 r_reloc_get_section (r_rel)
3297 const r_reloc *r_rel;
3299 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3300 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
3304 static bfd_boolean
3305 r_reloc_is_defined (r_rel)
3306 const r_reloc *r_rel;
3308 asection *sec = r_reloc_get_section (r_rel);
3309 if (sec == bfd_abs_section_ptr
3310 || sec == bfd_com_section_ptr
3311 || sec == bfd_und_section_ptr)
3312 return FALSE;
3313 return TRUE;
3317 /* source_reloc: relocations that reference literal sections. */
3319 /* To determine whether literals can be coalesced, we need to first
3320 record all the relocations that reference the literals. The
3321 source_reloc structure below is used for this purpose. The
3322 source_reloc entries are kept in a per-literal-section array, sorted
3323 by offset within the literal section (i.e., target offset).
3325 The source_sec and r_rel.rela.r_offset fields identify the source of
3326 the relocation. The r_rel field records the relocation value, i.e.,
3327 the offset of the literal being referenced. The opnd field is needed
3328 to determine the range of the immediate field to which the relocation
3329 applies, so we can determine whether another literal with the same
3330 value is within range. The is_null field is true when the relocation
3331 is being removed (e.g., when an L32R is being removed due to a CALLX
3332 that is converted to a direct CALL). */
3334 typedef struct source_reloc_struct source_reloc;
3336 struct source_reloc_struct
3338 asection *source_sec;
3339 r_reloc r_rel;
3340 xtensa_operand opnd;
3341 bfd_boolean is_null;
3345 static void init_source_reloc
3346 PARAMS ((source_reloc *, asection *, const r_reloc *, xtensa_operand));
3347 static source_reloc *find_source_reloc
3348 PARAMS ((source_reloc *, int, asection *, Elf_Internal_Rela *));
3349 static int source_reloc_compare
3350 PARAMS ((const PTR, const PTR));
3353 static void
3354 init_source_reloc (reloc, source_sec, r_rel, opnd)
3355 source_reloc *reloc;
3356 asection *source_sec;
3357 const r_reloc *r_rel;
3358 xtensa_operand opnd;
3360 reloc->source_sec = source_sec;
3361 reloc->r_rel = *r_rel;
3362 reloc->opnd = opnd;
3363 reloc->is_null = FALSE;
3367 /* Find the source_reloc for a particular source offset and relocation
3368 type. Note that the array is sorted by _target_ offset, so this is
3369 just a linear search. */
3371 static source_reloc *
3372 find_source_reloc (src_relocs, src_count, sec, irel)
3373 source_reloc *src_relocs;
3374 int src_count;
3375 asection *sec;
3376 Elf_Internal_Rela *irel;
3378 int i;
3380 for (i = 0; i < src_count; i++)
3382 if (src_relocs[i].source_sec == sec
3383 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
3384 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
3385 == ELF32_R_TYPE (irel->r_info)))
3386 return &src_relocs[i];
3389 return NULL;
3393 static int
3394 source_reloc_compare (ap, bp)
3395 const PTR ap;
3396 const PTR bp;
3398 const source_reloc *a = (const source_reloc *) ap;
3399 const source_reloc *b = (const source_reloc *) bp;
3401 return (a->r_rel.target_offset - b->r_rel.target_offset);
3405 /* Literal values and value hash tables. */
3407 /* Literals with the same value can be coalesced. The literal_value
3408 structure records the value of a literal: the "r_rel" field holds the
3409 information from the relocation on the literal (if there is one) and
3410 the "value" field holds the contents of the literal word itself.
3412 The value_map structure records a literal value along with the
3413 location of a literal holding that value. The value_map hash table
3414 is indexed by the literal value, so that we can quickly check if a
3415 particular literal value has been seen before and is thus a candidate
3416 for coalescing. */
3418 typedef struct literal_value_struct literal_value;
3419 typedef struct value_map_struct value_map;
3420 typedef struct value_map_hash_table_struct value_map_hash_table;
3422 struct literal_value_struct
3424 r_reloc r_rel;
3425 unsigned long value;
3428 struct value_map_struct
3430 literal_value val; /* The literal value. */
3431 r_reloc loc; /* Location of the literal. */
3432 value_map *next;
3435 struct value_map_hash_table_struct
3437 unsigned bucket_count;
3438 value_map **buckets;
3439 unsigned count;
3443 static bfd_boolean is_same_value
3444 PARAMS ((const literal_value *, const literal_value *));
3445 static value_map_hash_table *value_map_hash_table_init
3446 PARAMS ((void));
3447 static unsigned hash_literal_value
3448 PARAMS ((const literal_value *));
3449 static unsigned hash_bfd_vma
3450 PARAMS ((bfd_vma));
3451 static value_map *get_cached_value
3452 PARAMS ((value_map_hash_table *, const literal_value *));
3453 static value_map *add_value_map
3454 PARAMS ((value_map_hash_table *, const literal_value *, const r_reloc *));
3457 static bfd_boolean
3458 is_same_value (src1, src2)
3459 const literal_value *src1;
3460 const literal_value *src2;
3462 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
3463 return FALSE;
3465 if (r_reloc_is_const (&src1->r_rel))
3466 return (src1->value == src2->value);
3468 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
3469 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
3470 return FALSE;
3472 if (r_reloc_get_target_offset (&src1->r_rel)
3473 != r_reloc_get_target_offset (&src2->r_rel))
3474 return FALSE;
3476 if (src1->value != src2->value)
3477 return FALSE;
3479 /* Now check for the same section and the same elf_hash. */
3480 if (r_reloc_is_defined (&src1->r_rel))
3482 if (r_reloc_get_section (&src1->r_rel)
3483 != r_reloc_get_section (&src2->r_rel))
3484 return FALSE;
3486 else
3488 if (r_reloc_get_hash_entry (&src1->r_rel)
3489 != r_reloc_get_hash_entry (&src2->r_rel))
3490 return FALSE;
3492 if (r_reloc_get_hash_entry (&src1->r_rel) == 0)
3493 return FALSE;
3496 return TRUE;
3500 /* Must be power of 2. */
3501 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
3503 static value_map_hash_table *
3504 value_map_hash_table_init ()
3506 value_map_hash_table *values;
3508 values = (value_map_hash_table *)
3509 bfd_malloc (sizeof (value_map_hash_table));
3511 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
3512 values->count = 0;
3513 values->buckets = (value_map **)
3514 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
3516 return values;
3520 static unsigned
3521 hash_bfd_vma (val)
3522 bfd_vma val;
3524 return (val >> 2) + (val >> 10);
3528 static unsigned
3529 hash_literal_value (src)
3530 const literal_value *src;
3532 unsigned hash_val;
3534 if (r_reloc_is_const (&src->r_rel))
3535 return hash_bfd_vma (src->value);
3537 hash_val = (hash_bfd_vma (r_reloc_get_target_offset (&src->r_rel))
3538 + hash_bfd_vma (src->value));
3540 /* Now check for the same section and the same elf_hash. */
3541 if (r_reloc_is_defined (&src->r_rel))
3542 hash_val += hash_bfd_vma ((bfd_vma) (unsigned) r_reloc_get_section (&src->r_rel));
3543 else
3544 hash_val += hash_bfd_vma ((bfd_vma) (unsigned) r_reloc_get_hash_entry (&src->r_rel));
3546 return hash_val;
3550 /* Check if the specified literal_value has been seen before. */
3552 static value_map *
3553 get_cached_value (map, val)
3554 value_map_hash_table *map;
3555 const literal_value *val;
3557 value_map *map_e;
3558 value_map *bucket;
3559 unsigned idx;
3561 idx = hash_literal_value (val);
3562 idx = idx & (map->bucket_count - 1);
3563 bucket = map->buckets[idx];
3564 for (map_e = bucket; map_e; map_e = map_e->next)
3566 if (is_same_value (&map_e->val, val))
3567 return map_e;
3569 return NULL;
3573 /* Record a new literal value. It is illegal to call this if VALUE
3574 already has an entry here. */
3576 static value_map *
3577 add_value_map (map, val, loc)
3578 value_map_hash_table *map;
3579 const literal_value *val;
3580 const r_reloc *loc;
3582 value_map **bucket_p;
3583 unsigned idx;
3585 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
3587 BFD_ASSERT (get_cached_value (map, val) == NULL);
3588 val_e->val = *val;
3589 val_e->loc = *loc;
3591 idx = hash_literal_value (val);
3592 idx = idx & (map->bucket_count - 1);
3593 bucket_p = &map->buckets[idx];
3595 val_e->next = *bucket_p;
3596 *bucket_p = val_e;
3597 map->count++;
3598 /* FIXME: consider resizing the hash table if we get too many entries */
3600 return val_e;
3604 /* Lists of literals being coalesced or removed. */
3606 /* In the usual case, the literal identified by "from" is being
3607 coalesced with another literal identified by "to". If the literal is
3608 unused and is being removed altogether, "to.abfd" will be NULL.
3609 The removed_literal entries are kept on a per-section list, sorted
3610 by the "from" offset field. */
3612 typedef struct removed_literal_struct removed_literal;
3613 typedef struct removed_literal_list_struct removed_literal_list;
3615 struct removed_literal_struct
3617 r_reloc from;
3618 r_reloc to;
3619 removed_literal *next;
3622 struct removed_literal_list_struct
3624 removed_literal *head;
3625 removed_literal *tail;
3629 static void add_removed_literal
3630 PARAMS ((removed_literal_list *, const r_reloc *, const r_reloc *));
3631 static removed_literal *find_removed_literal
3632 PARAMS ((removed_literal_list *, bfd_vma));
3633 static bfd_vma offset_with_removed_literals
3634 PARAMS ((removed_literal_list *, bfd_vma));
3637 /* Record that the literal at "from" is being removed. If "to" is not
3638 NULL, the "from" literal is being coalesced with the "to" literal. */
3640 static void
3641 add_removed_literal (removed_list, from, to)
3642 removed_literal_list *removed_list;
3643 const r_reloc *from;
3644 const r_reloc *to;
3646 removed_literal *r, *new_r, *next_r;
3648 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
3650 new_r->from = *from;
3651 if (to)
3652 new_r->to = *to;
3653 else
3654 new_r->to.abfd = NULL;
3655 new_r->next = NULL;
3657 r = removed_list->head;
3658 if (r == NULL)
3660 removed_list->head = new_r;
3661 removed_list->tail = new_r;
3663 /* Special check for common case of append. */
3664 else if (removed_list->tail->from.target_offset < from->target_offset)
3666 removed_list->tail->next = new_r;
3667 removed_list->tail = new_r;
3669 else
3671 while (r->from.target_offset < from->target_offset
3672 && r->next != NULL)
3674 r = r->next;
3676 next_r = r->next;
3677 r->next = new_r;
3678 new_r->next = next_r;
3679 if (next_r == NULL)
3680 removed_list->tail = new_r;
3685 /* Check if the list of removed literals contains an entry for the
3686 given address. Return the entry if found. */
3688 static removed_literal *
3689 find_removed_literal (removed_list, addr)
3690 removed_literal_list *removed_list;
3691 bfd_vma addr;
3693 removed_literal *r = removed_list->head;
3694 while (r && r->from.target_offset < addr)
3695 r = r->next;
3696 if (r && r->from.target_offset == addr)
3697 return r;
3698 return NULL;
3702 /* Adjust an offset in a section to compensate for literals that are
3703 being removed. Search the list of removed literals and subtract
3704 4 bytes for every removed literal prior to the given address. */
3706 static bfd_vma
3707 offset_with_removed_literals (removed_list, addr)
3708 removed_literal_list *removed_list;
3709 bfd_vma addr;
3711 removed_literal *r = removed_list->head;
3712 unsigned num_bytes = 0;
3714 if (r == NULL)
3715 return addr;
3717 while (r && r->from.target_offset <= addr)
3719 num_bytes += 4;
3720 r = r->next;
3722 if (num_bytes > addr)
3723 return 0;
3724 return (addr - num_bytes);
3728 /* Coalescing literals may require a relocation to refer to a section in
3729 a different input file, but the standard relocation information
3730 cannot express that. Instead, the reloc_bfd_fix structures are used
3731 to "fix" the relocations that refer to sections in other input files.
3732 These structures are kept on per-section lists. The "src_type" field
3733 records the relocation type in case there are multiple relocations on
3734 the same location. FIXME: This is ugly; an alternative might be to
3735 add new symbols with the "owner" field to some other input file. */
3737 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
3739 struct reloc_bfd_fix_struct
3741 asection *src_sec;
3742 bfd_vma src_offset;
3743 unsigned src_type; /* Relocation type. */
3745 bfd *target_abfd;
3746 asection *target_sec;
3747 bfd_vma target_offset;
3749 reloc_bfd_fix *next;
3753 static reloc_bfd_fix *reloc_bfd_fix_init
3754 PARAMS ((asection *, bfd_vma, unsigned, bfd *, asection *, bfd_vma));
3755 static reloc_bfd_fix *get_bfd_fix
3756 PARAMS ((reloc_bfd_fix *, asection *, bfd_vma, unsigned));
3759 static reloc_bfd_fix *
3760 reloc_bfd_fix_init (src_sec, src_offset, src_type,
3761 target_abfd, target_sec, target_offset)
3762 asection *src_sec;
3763 bfd_vma src_offset;
3764 unsigned src_type;
3765 bfd *target_abfd;
3766 asection *target_sec;
3767 bfd_vma target_offset;
3769 reloc_bfd_fix *fix;
3771 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
3772 fix->src_sec = src_sec;
3773 fix->src_offset = src_offset;
3774 fix->src_type = src_type;
3775 fix->target_abfd = target_abfd;
3776 fix->target_sec = target_sec;
3777 fix->target_offset = target_offset;
3779 return fix;
3783 static reloc_bfd_fix *
3784 get_bfd_fix (fix_list, sec, offset, type)
3785 reloc_bfd_fix *fix_list;
3786 asection *sec;
3787 bfd_vma offset;
3788 unsigned type;
3790 reloc_bfd_fix *r;
3792 for (r = fix_list; r != NULL; r = r->next)
3794 if (r->src_sec == sec
3795 && r->src_offset == offset
3796 && r->src_type == type)
3797 return r;
3799 return NULL;
3803 /* Per-section data for relaxation. */
3805 struct xtensa_relax_info_struct
3807 bfd_boolean is_relaxable_literal_section;
3808 int visited; /* Number of times visited. */
3810 source_reloc *src_relocs; /* Array[src_count]. */
3811 int src_count;
3812 int src_next; /* Next src_relocs entry to assign. */
3814 removed_literal_list removed_list;
3816 reloc_bfd_fix *fix_list;
3819 struct elf_xtensa_section_data
3821 struct bfd_elf_section_data elf;
3822 xtensa_relax_info relax_info;
3825 static void init_xtensa_relax_info
3826 PARAMS ((asection *));
3827 static xtensa_relax_info *get_xtensa_relax_info
3828 PARAMS ((asection *));
3829 static void add_fix
3830 PARAMS ((asection *, reloc_bfd_fix *));
3833 static bfd_boolean
3834 elf_xtensa_new_section_hook (abfd, sec)
3835 bfd *abfd;
3836 asection *sec;
3838 struct elf_xtensa_section_data *sdata;
3839 bfd_size_type amt = sizeof (*sdata);
3841 sdata = (struct elf_xtensa_section_data *) bfd_zalloc (abfd, amt);
3842 if (sdata == NULL)
3843 return FALSE;
3844 sec->used_by_bfd = (PTR) sdata;
3846 return _bfd_elf_new_section_hook (abfd, sec);
3850 static void
3851 init_xtensa_relax_info (sec)
3852 asection *sec;
3854 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
3856 relax_info->is_relaxable_literal_section = FALSE;
3857 relax_info->visited = 0;
3859 relax_info->src_relocs = NULL;
3860 relax_info->src_count = 0;
3861 relax_info->src_next = 0;
3863 relax_info->removed_list.head = NULL;
3864 relax_info->removed_list.tail = NULL;
3866 relax_info->fix_list = NULL;
3870 static xtensa_relax_info *
3871 get_xtensa_relax_info (sec)
3872 asection *sec;
3874 struct elf_xtensa_section_data *section_data;
3876 /* No info available if no section or if it is an output section. */
3877 if (!sec || sec == sec->output_section)
3878 return NULL;
3880 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
3881 return &section_data->relax_info;
3885 static void
3886 add_fix (src_sec, fix)
3887 asection *src_sec;
3888 reloc_bfd_fix *fix;
3890 xtensa_relax_info *relax_info;
3892 relax_info = get_xtensa_relax_info (src_sec);
3893 fix->next = relax_info->fix_list;
3894 relax_info->fix_list = fix;
3898 /* Access to internal relocations, section contents and symbols. */
3900 /* During relaxation, we need to modify relocations, section contents,
3901 and symbol definitions, and we need to keep the original values from
3902 being reloaded from the input files, i.e., we need to "pin" the
3903 modified values in memory. We also want to continue to observe the
3904 setting of the "keep-memory" flag. The following functions wrap the
3905 standard BFD functions to take care of this for us. */
3907 static Elf_Internal_Rela *
3908 retrieve_internal_relocs (abfd, sec, keep_memory)
3909 bfd *abfd;
3910 asection *sec;
3911 bfd_boolean keep_memory;
3913 Elf_Internal_Rela *internal_relocs;
3915 if ((sec->flags & SEC_LINKER_CREATED) != 0)
3916 return NULL;
3918 internal_relocs = elf_section_data (sec)->relocs;
3919 if (internal_relocs == NULL)
3920 internal_relocs = (_bfd_elf_link_read_relocs
3921 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3922 keep_memory));
3923 return internal_relocs;
3927 static void
3928 pin_internal_relocs (sec, internal_relocs)
3929 asection *sec;
3930 Elf_Internal_Rela *internal_relocs;
3932 elf_section_data (sec)->relocs = internal_relocs;
3936 static void
3937 release_internal_relocs (sec, internal_relocs)
3938 asection *sec;
3939 Elf_Internal_Rela *internal_relocs;
3941 if (internal_relocs
3942 && elf_section_data (sec)->relocs != internal_relocs)
3943 free (internal_relocs);
3947 static bfd_byte *
3948 retrieve_contents (abfd, sec, keep_memory)
3949 bfd *abfd;
3950 asection *sec;
3951 bfd_boolean keep_memory;
3953 bfd_byte *contents;
3955 contents = elf_section_data (sec)->this_hdr.contents;
3957 if (contents == NULL && sec->_raw_size != 0)
3959 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
3960 if (contents != NULL)
3962 if (! bfd_get_section_contents (abfd, sec, contents,
3963 (file_ptr) 0, sec->_raw_size))
3965 free (contents);
3966 return NULL;
3968 if (keep_memory)
3969 elf_section_data (sec)->this_hdr.contents = contents;
3972 return contents;
3976 static void
3977 pin_contents (sec, contents)
3978 asection *sec;
3979 bfd_byte *contents;
3981 elf_section_data (sec)->this_hdr.contents = contents;
3985 static void
3986 release_contents (sec, contents)
3987 asection *sec;
3988 bfd_byte *contents;
3990 if (contents &&
3991 elf_section_data (sec)->this_hdr.contents != contents)
3992 free (contents);
3996 static Elf_Internal_Sym *
3997 retrieve_local_syms (input_bfd)
3998 bfd *input_bfd;
4000 Elf_Internal_Shdr *symtab_hdr;
4001 Elf_Internal_Sym *isymbuf;
4002 size_t locsymcount;
4004 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4005 locsymcount = symtab_hdr->sh_info;
4007 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4008 if (isymbuf == NULL && locsymcount != 0)
4009 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4010 NULL, NULL, NULL);
4012 /* Save the symbols for this input file so they won't be read again. */
4013 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
4014 symtab_hdr->contents = (unsigned char *) isymbuf;
4016 return isymbuf;
4020 /* Code for link-time relaxation. */
4022 /* Local helper functions. */
4023 static bfd_boolean analyze_relocations
4024 PARAMS ((struct bfd_link_info *));
4025 static bfd_boolean find_relaxable_sections
4026 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
4027 static bfd_boolean collect_source_relocs
4028 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4029 static bfd_boolean is_resolvable_asm_expansion
4030 PARAMS ((bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
4031 struct bfd_link_info *, bfd_boolean *));
4032 static bfd_boolean remove_literals
4033 PARAMS ((bfd *, asection *, struct bfd_link_info *, value_map_hash_table *));
4034 static bfd_boolean relax_section
4035 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4036 static bfd_boolean relax_property_section
4037 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4038 static bfd_boolean relax_section_symbols
4039 PARAMS ((bfd *, asection *));
4040 static bfd_boolean relocations_reach
4041 PARAMS ((source_reloc *, int, const r_reloc *));
4042 static void translate_reloc
4043 PARAMS ((const r_reloc *, r_reloc *));
4044 static Elf_Internal_Rela *get_irel_at_offset
4045 PARAMS ((asection *, Elf_Internal_Rela *, bfd_vma));
4046 static Elf_Internal_Rela *find_associated_l32r_irel
4047 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *,
4048 Elf_Internal_Rela *));
4049 static void shrink_dynamic_reloc_sections
4050 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *));
4053 static bfd_boolean
4054 elf_xtensa_relax_section (abfd, sec, link_info, again)
4055 bfd *abfd;
4056 asection *sec;
4057 struct bfd_link_info *link_info;
4058 bfd_boolean *again;
4060 static value_map_hash_table *values = NULL;
4061 xtensa_relax_info *relax_info;
4063 if (!values)
4065 /* Do some overall initialization for relaxation. */
4066 values = value_map_hash_table_init ();
4067 relaxing_section = TRUE;
4068 if (!analyze_relocations (link_info))
4069 return FALSE;
4071 *again = FALSE;
4073 /* Don't mess with linker-created sections. */
4074 if ((sec->flags & SEC_LINKER_CREATED) != 0)
4075 return TRUE;
4077 relax_info = get_xtensa_relax_info (sec);
4078 BFD_ASSERT (relax_info != NULL);
4080 switch (relax_info->visited)
4082 case 0:
4083 /* Note: It would be nice to fold this pass into
4084 analyze_relocations, but it is important for this step that the
4085 sections be examined in link order. */
4086 if (!remove_literals (abfd, sec, link_info, values))
4087 return FALSE;
4088 *again = TRUE;
4089 break;
4091 case 1:
4092 if (!relax_section (abfd, sec, link_info))
4093 return FALSE;
4094 *again = TRUE;
4095 break;
4097 case 2:
4098 if (!relax_section_symbols (abfd, sec))
4099 return FALSE;
4100 break;
4103 relax_info->visited++;
4104 return TRUE;
4107 /* Initialization for relaxation. */
4109 /* This function is called once at the start of relaxation. It scans
4110 all the input sections and marks the ones that are relaxable (i.e.,
4111 literal sections with L32R relocations against them). It then
4112 collect source_reloc information for all the relocations against
4113 those relaxable sections. */
4115 static bfd_boolean
4116 analyze_relocations (link_info)
4117 struct bfd_link_info *link_info;
4119 bfd *abfd;
4120 asection *sec;
4121 bfd_boolean is_relaxable = FALSE;
4123 /* Initialize the per-section relaxation info. */
4124 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4125 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4127 init_xtensa_relax_info (sec);
4130 /* Mark relaxable sections (and count relocations against each one). */
4131 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4132 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4134 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
4135 return FALSE;
4138 /* Bail out if there are no relaxable sections. */
4139 if (!is_relaxable)
4140 return TRUE;
4142 /* Allocate space for source_relocs. */
4143 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4144 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4146 xtensa_relax_info *relax_info;
4148 relax_info = get_xtensa_relax_info (sec);
4149 if (relax_info->is_relaxable_literal_section)
4151 relax_info->src_relocs = (source_reloc *)
4152 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
4156 /* Collect info on relocations against each relaxable section. */
4157 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4158 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4160 if (!collect_source_relocs (abfd, sec, link_info))
4161 return FALSE;
4164 return TRUE;
4168 /* Find all the literal sections that might be relaxed. The motivation
4169 for this pass is that collect_source_relocs() needs to record _all_
4170 the relocations that target each relaxable section. That is
4171 expensive and unnecessary unless the target section is actually going
4172 to be relaxed. This pass identifies all such sections by checking if
4173 they have L32Rs pointing to them. In the process, the total number
4174 of relocations targeting each section is also counted so that we
4175 know how much space to allocate for source_relocs against each
4176 relaxable literal section. */
4178 static bfd_boolean
4179 find_relaxable_sections (abfd, sec, link_info, is_relaxable_p)
4180 bfd *abfd;
4181 asection *sec;
4182 struct bfd_link_info *link_info;
4183 bfd_boolean *is_relaxable_p;
4185 Elf_Internal_Rela *internal_relocs;
4186 bfd_byte *contents;
4187 bfd_boolean ok = TRUE;
4188 unsigned i;
4190 internal_relocs = retrieve_internal_relocs (abfd, sec,
4191 link_info->keep_memory);
4192 if (internal_relocs == NULL)
4193 return ok;
4195 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4196 if (contents == NULL && sec->_raw_size != 0)
4198 ok = FALSE;
4199 goto error_return;
4202 for (i = 0; i < sec->reloc_count; i++)
4204 Elf_Internal_Rela *irel = &internal_relocs[i];
4205 r_reloc r_rel;
4206 asection *target_sec;
4207 xtensa_relax_info *target_relax_info;
4209 r_reloc_init (&r_rel, abfd, irel);
4211 target_sec = r_reloc_get_section (&r_rel);
4212 target_relax_info = get_xtensa_relax_info (target_sec);
4213 if (!target_relax_info)
4214 continue;
4216 /* Count relocations against the target section. */
4217 target_relax_info->src_count++;
4219 if (is_literal_section (target_sec)
4220 && is_l32r_relocation (sec, contents, irel)
4221 && r_reloc_is_defined (&r_rel))
4223 /* Mark the target section as relaxable. */
4224 target_relax_info->is_relaxable_literal_section = TRUE;
4225 *is_relaxable_p = TRUE;
4229 error_return:
4230 release_contents (sec, contents);
4231 release_internal_relocs (sec, internal_relocs);
4232 return ok;
4236 /* Record _all_ the relocations that point to relaxable literal
4237 sections, and get rid of ASM_EXPAND relocs by either converting them
4238 to ASM_SIMPLIFY or by removing them. */
4240 static bfd_boolean
4241 collect_source_relocs (abfd, sec, link_info)
4242 bfd *abfd;
4243 asection *sec;
4244 struct bfd_link_info *link_info;
4246 Elf_Internal_Rela *internal_relocs;
4247 bfd_byte *contents;
4248 bfd_boolean ok = TRUE;
4249 unsigned i;
4251 internal_relocs = retrieve_internal_relocs (abfd, sec,
4252 link_info->keep_memory);
4253 if (internal_relocs == NULL)
4254 return ok;
4256 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4257 if (contents == NULL && sec->_raw_size != 0)
4259 ok = FALSE;
4260 goto error_return;
4263 /* Record relocations against relaxable literal sections. */
4264 for (i = 0; i < sec->reloc_count; i++)
4266 Elf_Internal_Rela *irel = &internal_relocs[i];
4267 r_reloc r_rel;
4268 asection *target_sec;
4269 xtensa_relax_info *target_relax_info;
4271 r_reloc_init (&r_rel, abfd, irel);
4273 target_sec = r_reloc_get_section (&r_rel);
4274 target_relax_info = get_xtensa_relax_info (target_sec);
4276 if (target_relax_info
4277 && target_relax_info->is_relaxable_literal_section)
4279 xtensa_opcode opcode;
4280 xtensa_operand opnd;
4281 source_reloc *s_reloc;
4282 int src_next;
4284 src_next = target_relax_info->src_next++;
4285 s_reloc = &target_relax_info->src_relocs[src_next];
4287 opcode = get_relocation_opcode (sec, contents, irel);
4288 if (opcode == XTENSA_UNDEFINED)
4289 opnd = NULL;
4290 else
4291 opnd = xtensa_get_operand (xtensa_default_isa, opcode,
4292 get_relocation_opnd (irel));
4294 init_source_reloc (s_reloc, sec, &r_rel, opnd);
4298 /* Now get rid of ASM_EXPAND relocations. At this point, the
4299 src_relocs array for the target literal section may still be
4300 incomplete, but it must at least contain the entries for the L32R
4301 relocations associated with ASM_EXPANDs because they were just
4302 added in the preceding loop over the relocations. */
4304 for (i = 0; i < sec->reloc_count; i++)
4306 Elf_Internal_Rela *irel = &internal_relocs[i];
4307 bfd_boolean is_reachable;
4309 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4310 &is_reachable))
4311 continue;
4313 if (is_reachable)
4315 Elf_Internal_Rela *l32r_irel;
4316 r_reloc r_rel;
4317 asection *target_sec;
4318 xtensa_relax_info *target_relax_info;
4320 /* Mark the source_reloc for the L32R so that it will be
4321 removed in remove_literals(), along with the associated
4322 literal. */
4323 l32r_irel = find_associated_l32r_irel (sec, contents,
4324 irel, internal_relocs);
4325 if (l32r_irel == NULL)
4326 continue;
4328 r_reloc_init (&r_rel, abfd, l32r_irel);
4330 target_sec = r_reloc_get_section (&r_rel);
4331 target_relax_info = get_xtensa_relax_info (target_sec);
4333 if (target_relax_info
4334 && target_relax_info->is_relaxable_literal_section)
4336 source_reloc *s_reloc;
4338 /* Search the source_relocs for the entry corresponding to
4339 the l32r_irel. Note: The src_relocs array is not yet
4340 sorted, but it wouldn't matter anyway because we're
4341 searching by source offset instead of target offset. */
4342 s_reloc = find_source_reloc (target_relax_info->src_relocs,
4343 target_relax_info->src_next,
4344 sec, l32r_irel);
4345 BFD_ASSERT (s_reloc);
4346 s_reloc->is_null = TRUE;
4349 /* Convert this reloc to ASM_SIMPLIFY. */
4350 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4351 R_XTENSA_ASM_SIMPLIFY);
4352 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4354 pin_internal_relocs (sec, internal_relocs);
4356 else
4358 /* It is resolvable but doesn't reach. We resolve now
4359 by eliminating the relocation -- the call will remain
4360 expanded into L32R/CALLX. */
4361 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4362 pin_internal_relocs (sec, internal_relocs);
4366 error_return:
4367 release_contents (sec, contents);
4368 release_internal_relocs (sec, internal_relocs);
4369 return ok;
4373 /* Return TRUE if the asm expansion can be resolved. Generally it can
4374 be resolved on a final link or when a partial link locates it in the
4375 same section as the target. Set "is_reachable" flag if the target of
4376 the call is within the range of a direct call, given the current VMA
4377 for this section and the target section. */
4379 bfd_boolean
4380 is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4381 is_reachable_p)
4382 bfd *abfd;
4383 asection *sec;
4384 bfd_byte *contents;
4385 Elf_Internal_Rela *irel;
4386 struct bfd_link_info *link_info;
4387 bfd_boolean *is_reachable_p;
4389 asection *target_sec;
4390 bfd_vma target_offset;
4391 r_reloc r_rel;
4392 xtensa_opcode opcode, direct_call_opcode;
4393 bfd_vma self_address;
4394 bfd_vma dest_address;
4396 *is_reachable_p = FALSE;
4398 if (contents == NULL)
4399 return FALSE;
4401 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
4402 return FALSE;
4404 opcode = get_expanded_call_opcode (contents + irel->r_offset,
4405 sec->_raw_size - irel->r_offset);
4407 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4408 if (direct_call_opcode == XTENSA_UNDEFINED)
4409 return FALSE;
4411 /* Check and see that the target resolves. */
4412 r_reloc_init (&r_rel, abfd, irel);
4413 if (!r_reloc_is_defined (&r_rel))
4414 return FALSE;
4416 target_sec = r_reloc_get_section (&r_rel);
4417 target_offset = r_reloc_get_target_offset (&r_rel);
4419 /* If the target is in a shared library, then it doesn't reach. This
4420 isn't supposed to come up because the compiler should never generate
4421 non-PIC calls on systems that use shared libraries, but the linker
4422 shouldn't crash regardless. */
4423 if (!target_sec->output_section)
4424 return FALSE;
4426 /* For relocatable sections, we can only simplify when the output
4427 section of the target is the same as the output section of the
4428 source. */
4429 if (link_info->relocatable
4430 && (target_sec->output_section != sec->output_section))
4431 return FALSE;
4433 self_address = (sec->output_section->vma
4434 + sec->output_offset + irel->r_offset + 3);
4435 dest_address = (target_sec->output_section->vma
4436 + target_sec->output_offset + target_offset);
4438 *is_reachable_p = pcrel_reloc_fits
4439 (xtensa_get_operand (xtensa_default_isa, direct_call_opcode, 0),
4440 self_address, dest_address);
4442 if ((self_address >> CALL_SEGMENT_BITS) !=
4443 (dest_address >> CALL_SEGMENT_BITS))
4444 return FALSE;
4446 return TRUE;
4450 static Elf_Internal_Rela *
4451 find_associated_l32r_irel (sec, contents, other_irel, internal_relocs)
4452 asection *sec;
4453 bfd_byte *contents;
4454 Elf_Internal_Rela *other_irel;
4455 Elf_Internal_Rela *internal_relocs;
4457 unsigned i;
4459 for (i = 0; i < sec->reloc_count; i++)
4461 Elf_Internal_Rela *irel = &internal_relocs[i];
4463 if (irel == other_irel)
4464 continue;
4465 if (irel->r_offset != other_irel->r_offset)
4466 continue;
4467 if (is_l32r_relocation (sec, contents, irel))
4468 return irel;
4471 return NULL;
4474 /* First relaxation pass. */
4476 /* If the section is relaxable (i.e., a literal section), check each
4477 literal to see if it has the same value as another literal that has
4478 already been seen, either in the current section or a previous one.
4479 If so, add an entry to the per-section list of removed literals. The
4480 actual changes are deferred until the next pass. */
4482 static bfd_boolean
4483 remove_literals (abfd, sec, link_info, values)
4484 bfd *abfd;
4485 asection *sec;
4486 struct bfd_link_info *link_info;
4487 value_map_hash_table *values;
4489 xtensa_relax_info *relax_info;
4490 bfd_byte *contents;
4491 Elf_Internal_Rela *internal_relocs;
4492 source_reloc *src_relocs;
4493 bfd_boolean ok = TRUE;
4494 int i;
4496 /* Do nothing if it is not a relaxable literal section. */
4497 relax_info = get_xtensa_relax_info (sec);
4498 BFD_ASSERT (relax_info);
4500 if (!relax_info->is_relaxable_literal_section)
4501 return ok;
4503 internal_relocs = retrieve_internal_relocs (abfd, sec,
4504 link_info->keep_memory);
4506 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4507 if (contents == NULL && sec->_raw_size != 0)
4509 ok = FALSE;
4510 goto error_return;
4513 /* Sort the source_relocs by target offset. */
4514 src_relocs = relax_info->src_relocs;
4515 qsort (src_relocs, relax_info->src_count,
4516 sizeof (source_reloc), source_reloc_compare);
4518 for (i = 0; i < relax_info->src_count; i++)
4520 source_reloc *rel;
4521 Elf_Internal_Rela *irel = NULL;
4522 literal_value val;
4523 value_map *val_map;
4525 rel = &src_relocs[i];
4526 irel = get_irel_at_offset (sec, internal_relocs,
4527 rel->r_rel.target_offset);
4529 /* If the target_offset for this relocation is the same as the
4530 previous relocation, then we've already considered whether the
4531 literal can be coalesced. Skip to the next one.... */
4532 if (i != 0 && (src_relocs[i-1].r_rel.target_offset
4533 == rel->r_rel.target_offset))
4534 continue;
4536 /* Check if the relocation was from an L32R that is being removed
4537 because a CALLX was converted to a direct CALL, and check if
4538 there are no other relocations to the literal. */
4539 if (rel->is_null
4540 && (i == relax_info->src_count - 1
4541 || (src_relocs[i+1].r_rel.target_offset
4542 != rel->r_rel.target_offset)))
4544 /* Mark the unused literal so that it will be removed. */
4545 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
4547 /* Zero out the relocation on this literal location. */
4548 if (irel)
4550 if (elf_hash_table (link_info)->dynamic_sections_created)
4551 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4553 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4556 continue;
4559 /* Find the literal value. */
4560 r_reloc_init (&val.r_rel, abfd, irel);
4561 BFD_ASSERT (rel->r_rel.target_offset < sec->_raw_size);
4562 val.value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
4564 /* Check if we've seen another literal with the same value. */
4565 val_map = get_cached_value (values, &val);
4566 if (val_map != NULL)
4568 /* First check that THIS and all the other relocs to this
4569 literal will FIT if we move them to the new address. */
4571 if (relocations_reach (rel, relax_info->src_count - i,
4572 &val_map->loc))
4574 /* Mark that the literal will be coalesced. */
4575 add_removed_literal (&relax_info->removed_list,
4576 &rel->r_rel, &val_map->loc);
4578 else
4580 /* Relocations do not reach -- do not remove this literal. */
4581 val_map->loc = rel->r_rel;
4584 else
4586 /* This is the first time we've seen this literal value. */
4587 BFD_ASSERT (sec == r_reloc_get_section (&rel->r_rel));
4588 add_value_map (values, &val, &rel->r_rel);
4592 error_return:
4593 release_contents (sec, contents);
4594 release_internal_relocs (sec, internal_relocs);
4595 return ok;
4599 /* Check if the original relocations (presumably on L32R instructions)
4600 identified by reloc[0..N] can be changed to reference the literal
4601 identified by r_rel. If r_rel is out of range for any of the
4602 original relocations, then we don't want to coalesce the original
4603 literal with the one at r_rel. We only check reloc[0..N], where the
4604 offsets are all the same as for reloc[0] (i.e., they're all
4605 referencing the same literal) and where N is also bounded by the
4606 number of remaining entries in the "reloc" array. The "reloc" array
4607 is sorted by target offset so we know all the entries for the same
4608 literal will be contiguous. */
4610 static bfd_boolean
4611 relocations_reach (reloc, remaining_relocs, r_rel)
4612 source_reloc *reloc;
4613 int remaining_relocs;
4614 const r_reloc *r_rel;
4616 bfd_vma from_offset, source_address, dest_address;
4617 asection *sec;
4618 int i;
4620 if (!r_reloc_is_defined (r_rel))
4621 return FALSE;
4623 sec = r_reloc_get_section (r_rel);
4624 from_offset = reloc[0].r_rel.target_offset;
4626 for (i = 0; i < remaining_relocs; i++)
4628 if (reloc[i].r_rel.target_offset != from_offset)
4629 break;
4631 /* Ignore relocations that have been removed. */
4632 if (reloc[i].is_null)
4633 continue;
4635 /* The original and new output section for these must be the same
4636 in order to coalesce. */
4637 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
4638 != sec->output_section)
4639 return FALSE;
4641 /* A NULL operand means it is not a PC-relative relocation, so
4642 the literal can be moved anywhere. */
4643 if (reloc[i].opnd)
4645 /* Otherwise, check to see that it fits. */
4646 source_address = (reloc[i].source_sec->output_section->vma
4647 + reloc[i].source_sec->output_offset
4648 + reloc[i].r_rel.rela.r_offset);
4649 dest_address = (sec->output_section->vma
4650 + sec->output_offset
4651 + r_rel->target_offset);
4653 if (!pcrel_reloc_fits (reloc[i].opnd, source_address, dest_address))
4654 return FALSE;
4658 return TRUE;
4662 /* WARNING: linear search here. If the relocation are in order by
4663 address, we can use a faster binary search. ALSO, we assume that
4664 there is only 1 non-NONE relocation per address. */
4666 static Elf_Internal_Rela *
4667 get_irel_at_offset (sec, internal_relocs, offset)
4668 asection *sec;
4669 Elf_Internal_Rela *internal_relocs;
4670 bfd_vma offset;
4672 unsigned i;
4673 if (!internal_relocs)
4674 return NULL;
4675 for (i = 0; i < sec->reloc_count; i++)
4677 Elf_Internal_Rela *irel = &internal_relocs[i];
4678 if (irel->r_offset == offset
4679 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
4680 return irel;
4682 return NULL;
4686 /* Second relaxation pass. */
4688 /* Modify all of the relocations to point to the right spot, and if this
4689 is a relaxable section, delete the unwanted literals and fix the
4690 cooked_size. */
4692 bfd_boolean
4693 relax_section (abfd, sec, link_info)
4694 bfd *abfd;
4695 asection *sec;
4696 struct bfd_link_info *link_info;
4698 Elf_Internal_Rela *internal_relocs;
4699 xtensa_relax_info *relax_info;
4700 bfd_byte *contents;
4701 bfd_boolean ok = TRUE;
4702 unsigned i;
4704 relax_info = get_xtensa_relax_info (sec);
4705 BFD_ASSERT (relax_info);
4707 /* Handle property sections (e.g., literal tables) specially. */
4708 if (xtensa_is_property_section (sec))
4710 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
4711 return relax_property_section (abfd, sec, link_info);
4714 internal_relocs = retrieve_internal_relocs (abfd, sec,
4715 link_info->keep_memory);
4716 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4717 if (contents == NULL && sec->_raw_size != 0)
4719 ok = FALSE;
4720 goto error_return;
4723 if (internal_relocs)
4725 for (i = 0; i < sec->reloc_count; i++)
4727 Elf_Internal_Rela *irel;
4728 xtensa_relax_info *target_relax_info;
4729 bfd_vma source_offset;
4730 r_reloc r_rel;
4731 unsigned r_type;
4732 asection *target_sec;
4734 /* Locally change the source address.
4735 Translate the target to the new target address.
4736 If it points to this section and has been removed,
4737 NULLify it.
4738 Write it back. */
4740 irel = &internal_relocs[i];
4741 source_offset = irel->r_offset;
4743 r_type = ELF32_R_TYPE (irel->r_info);
4744 r_reloc_init (&r_rel, abfd, irel);
4746 if (relax_info->is_relaxable_literal_section)
4748 if (r_type != R_XTENSA_NONE
4749 && find_removed_literal (&relax_info->removed_list,
4750 irel->r_offset))
4752 /* Remove this relocation. */
4753 if (elf_hash_table (link_info)->dynamic_sections_created)
4754 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4755 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4756 irel->r_offset = offset_with_removed_literals
4757 (&relax_info->removed_list, irel->r_offset);
4758 continue;
4760 source_offset =
4761 offset_with_removed_literals (&relax_info->removed_list,
4762 irel->r_offset);
4763 irel->r_offset = source_offset;
4766 target_sec = r_reloc_get_section (&r_rel);
4767 target_relax_info = get_xtensa_relax_info (target_sec);
4769 if (target_relax_info
4770 && target_relax_info->is_relaxable_literal_section)
4772 r_reloc new_rel;
4773 reloc_bfd_fix *fix;
4775 translate_reloc (&r_rel, &new_rel);
4777 /* FIXME: If the relocation still references a section in
4778 the same input file, the relocation should be modified
4779 directly instead of adding a "fix" record. */
4781 fix = reloc_bfd_fix_init (sec, source_offset, r_type, 0,
4782 r_reloc_get_section (&new_rel),
4783 new_rel.target_offset);
4784 add_fix (sec, fix);
4787 pin_internal_relocs (sec, internal_relocs);
4791 if (relax_info->is_relaxable_literal_section)
4793 /* Walk through the contents and delete literals that are not needed
4794 anymore. */
4796 unsigned long size = sec->_cooked_size;
4797 unsigned long removed = 0;
4799 removed_literal *reloc = relax_info->removed_list.head;
4800 for (; reloc; reloc = reloc->next)
4802 unsigned long upper = sec->_raw_size;
4803 bfd_vma start = reloc->from.target_offset + 4;
4804 if (reloc->next)
4805 upper = reloc->next->from.target_offset;
4806 if (upper - start != 0)
4808 BFD_ASSERT (start <= upper);
4809 memmove (contents + start - removed - 4,
4810 contents + start,
4811 upper - start );
4812 pin_contents (sec, contents);
4814 removed += 4;
4815 size -= 4;
4818 /* Change the section size. */
4819 sec->_cooked_size = size;
4820 /* Also shrink _raw_size. (The code in relocate_section that
4821 checks that relocations are within the section must use
4822 _raw_size because of the way the stabs sections are relaxed;
4823 shrinking _raw_size means that these checks will not be
4824 unnecessarily lax.) */
4825 sec->_raw_size = size;
4828 error_return:
4829 release_internal_relocs (sec, internal_relocs);
4830 release_contents (sec, contents);
4831 return ok;
4835 /* Fix up a relocation to take account of removed literals. */
4837 static void
4838 translate_reloc (orig_rel, new_rel)
4839 const r_reloc *orig_rel;
4840 r_reloc *new_rel;
4842 asection *sec;
4843 xtensa_relax_info *relax_info;
4844 removed_literal *removed;
4845 unsigned long new_offset;
4847 *new_rel = *orig_rel;
4849 if (!r_reloc_is_defined (orig_rel))
4850 return;
4851 sec = r_reloc_get_section (orig_rel);
4853 relax_info = get_xtensa_relax_info (sec);
4854 BFD_ASSERT (relax_info);
4856 if (!relax_info->is_relaxable_literal_section)
4857 return;
4859 /* Check if the original relocation is against a literal being removed. */
4860 removed = find_removed_literal (&relax_info->removed_list,
4861 orig_rel->target_offset);
4862 if (removed)
4864 asection *new_sec;
4866 /* The fact that there is still a relocation to this literal indicates
4867 that the literal is being coalesced, not simply removed. */
4868 BFD_ASSERT (removed->to.abfd != NULL);
4870 /* This was moved to some other address (possibly in another section). */
4871 *new_rel = removed->to;
4872 new_sec = r_reloc_get_section (new_rel);
4873 if (new_sec != sec)
4875 sec = new_sec;
4876 relax_info = get_xtensa_relax_info (sec);
4877 if (!relax_info || !relax_info->is_relaxable_literal_section)
4878 return;
4882 /* ...and the target address may have been moved within its section. */
4883 new_offset = offset_with_removed_literals (&relax_info->removed_list,
4884 new_rel->target_offset);
4886 /* Modify the offset and addend. */
4887 new_rel->target_offset = new_offset;
4888 new_rel->rela.r_addend += (new_offset - new_rel->target_offset);
4892 /* For dynamic links, there may be a dynamic relocation for each
4893 literal. The number of dynamic relocations must be computed in
4894 size_dynamic_sections, which occurs before relaxation. When a
4895 literal is removed, this function checks if there is a corresponding
4896 dynamic relocation and shrinks the size of the appropriate dynamic
4897 relocation section accordingly. At this point, the contents of the
4898 dynamic relocation sections have not yet been filled in, so there's
4899 nothing else that needs to be done. */
4901 static void
4902 shrink_dynamic_reloc_sections (info, abfd, input_section, rel)
4903 struct bfd_link_info *info;
4904 bfd *abfd;
4905 asection *input_section;
4906 Elf_Internal_Rela *rel;
4908 Elf_Internal_Shdr *symtab_hdr;
4909 struct elf_link_hash_entry **sym_hashes;
4910 unsigned long r_symndx;
4911 int r_type;
4912 struct elf_link_hash_entry *h;
4913 bfd_boolean dynamic_symbol;
4915 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4916 sym_hashes = elf_sym_hashes (abfd);
4918 r_type = ELF32_R_TYPE (rel->r_info);
4919 r_symndx = ELF32_R_SYM (rel->r_info);
4921 if (r_symndx < symtab_hdr->sh_info)
4922 h = NULL;
4923 else
4924 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4926 dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
4928 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
4929 && (input_section->flags & SEC_ALLOC) != 0
4930 && (dynamic_symbol || info->shared))
4932 bfd *dynobj;
4933 const char *srel_name;
4934 asection *srel;
4935 bfd_boolean is_plt = FALSE;
4937 dynobj = elf_hash_table (info)->dynobj;
4938 BFD_ASSERT (dynobj != NULL);
4940 if (dynamic_symbol && r_type == R_XTENSA_PLT)
4942 srel_name = ".rela.plt";
4943 is_plt = TRUE;
4945 else
4946 srel_name = ".rela.got";
4948 /* Reduce size of the .rela.* section by one reloc. */
4949 srel = bfd_get_section_by_name (dynobj, srel_name);
4950 BFD_ASSERT (srel != NULL);
4951 BFD_ASSERT (srel->_cooked_size >= sizeof (Elf32_External_Rela));
4952 srel->_cooked_size -= sizeof (Elf32_External_Rela);
4954 /* Also shrink _raw_size. (This seems wrong but other bfd code seems
4955 to assume that linker-created sections will never be relaxed and
4956 hence _raw_size must always equal _cooked_size.) */
4957 srel->_raw_size = srel->_cooked_size;
4959 if (is_plt)
4961 asection *splt, *sgotplt, *srelgot;
4962 int reloc_index, chunk;
4964 /* Find the PLT reloc index of the entry being removed. This
4965 is computed from the size of ".rela.plt". It is needed to
4966 figure out which PLT chunk to resize. Usually "last index
4967 = size - 1" since the index starts at zero, but in this
4968 context, the size has just been decremented so there's no
4969 need to subtract one. */
4970 reloc_index = srel->_cooked_size / sizeof (Elf32_External_Rela);
4972 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
4973 splt = elf_xtensa_get_plt_section (dynobj, chunk);
4974 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
4975 BFD_ASSERT (splt != NULL && sgotplt != NULL);
4977 /* Check if an entire PLT chunk has just been eliminated. */
4978 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
4980 /* The two magic GOT entries for that chunk can go away. */
4981 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4982 BFD_ASSERT (srelgot != NULL);
4983 srelgot->reloc_count -= 2;
4984 srelgot->_cooked_size -= 2 * sizeof (Elf32_External_Rela);
4985 /* Shrink _raw_size (see comment above). */
4986 srelgot->_raw_size = srelgot->_cooked_size;
4988 sgotplt->_cooked_size -= 8;
4990 /* There should be only one entry left (and it will be
4991 removed below). */
4992 BFD_ASSERT (sgotplt->_cooked_size == 4);
4993 BFD_ASSERT (splt->_cooked_size == PLT_ENTRY_SIZE);
4996 BFD_ASSERT (sgotplt->_cooked_size >= 4);
4997 BFD_ASSERT (splt->_cooked_size >= PLT_ENTRY_SIZE);
4999 sgotplt->_cooked_size -= 4;
5000 splt->_cooked_size -= PLT_ENTRY_SIZE;
5002 /* Shrink _raw_sizes (see comment above). */
5003 sgotplt->_raw_size = sgotplt->_cooked_size;
5004 splt->_raw_size = splt->_cooked_size;
5010 /* This is similar to relax_section except that when a target is moved,
5011 we shift addresses up. We also need to modify the size. This
5012 algorithm does NOT allow for relocations into the middle of the
5013 property sections. */
5015 static bfd_boolean
5016 relax_property_section (abfd, sec, link_info)
5017 bfd *abfd;
5018 asection *sec;
5019 struct bfd_link_info *link_info;
5021 Elf_Internal_Rela *internal_relocs;
5022 bfd_byte *contents;
5023 unsigned i, nexti;
5024 bfd_boolean ok = TRUE;
5026 internal_relocs = retrieve_internal_relocs (abfd, sec,
5027 link_info->keep_memory);
5028 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5029 if (contents == NULL && sec->_raw_size != 0)
5031 ok = FALSE;
5032 goto error_return;
5035 if (internal_relocs)
5037 for (i = 0; i < sec->reloc_count; i++)
5039 Elf_Internal_Rela *irel;
5040 xtensa_relax_info *target_relax_info;
5041 r_reloc r_rel;
5042 unsigned r_type;
5043 asection *target_sec;
5045 /* Locally change the source address.
5046 Translate the target to the new target address.
5047 If it points to this section and has been removed, MOVE IT.
5048 Also, don't forget to modify the associated SIZE at
5049 (offset + 4). */
5051 irel = &internal_relocs[i];
5052 r_type = ELF32_R_TYPE (irel->r_info);
5053 if (r_type == R_XTENSA_NONE)
5054 continue;
5056 r_reloc_init (&r_rel, abfd, irel);
5058 target_sec = r_reloc_get_section (&r_rel);
5059 target_relax_info = get_xtensa_relax_info (target_sec);
5061 if (target_relax_info
5062 && target_relax_info->is_relaxable_literal_section)
5064 /* Translate the relocation's destination. */
5065 bfd_vma new_offset;
5066 bfd_vma new_end_offset;
5067 bfd_byte *size_p;
5068 long old_size, new_size;
5070 new_offset =
5071 offset_with_removed_literals (&target_relax_info->removed_list,
5072 r_rel.target_offset);
5074 /* Assert that we are not out of bounds. */
5075 size_p = &contents[irel->r_offset + 4];
5076 old_size = bfd_get_32 (abfd, &contents[irel->r_offset + 4]);
5078 new_end_offset =
5079 offset_with_removed_literals (&target_relax_info->removed_list,
5080 r_rel.target_offset + old_size);
5082 new_size = new_end_offset - new_offset;
5083 if (new_size != old_size)
5085 bfd_put_32 (abfd, new_size, size_p);
5086 pin_contents (sec, contents);
5089 if (new_offset != r_rel.target_offset)
5091 bfd_vma diff = new_offset - r_rel.target_offset;
5092 irel->r_addend += diff;
5093 pin_internal_relocs (sec, internal_relocs);
5099 /* Combine adjacent property table entries. This is also done in
5100 finish_dynamic_sections() but at that point it's too late to
5101 reclaim the space in the output section, so we do this twice. */
5103 if (internal_relocs)
5105 Elf_Internal_Rela *last_irel = NULL;
5106 int removed_bytes = 0;
5107 bfd_vma offset, last_irel_offset;
5108 bfd_vma section_size;
5110 /* Walk over memory and irels at the same time.
5111 This REQUIRES that the internal_relocs be sorted by offset. */
5112 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
5113 internal_reloc_compare);
5114 nexti = 0; /* Index into internal_relocs. */
5116 pin_internal_relocs (sec, internal_relocs);
5117 pin_contents (sec, contents);
5119 last_irel_offset = (bfd_vma) -1;
5120 section_size = (sec->_cooked_size ? sec->_cooked_size : sec->_raw_size);
5121 BFD_ASSERT (section_size % 8 == 0);
5123 for (offset = 0; offset < section_size; offset += 8)
5125 Elf_Internal_Rela *irel, *next_irel;
5126 bfd_vma bytes_to_remove, size, actual_offset;
5127 bfd_boolean remove_this_irel;
5129 irel = NULL;
5130 next_irel = NULL;
5132 /* Find the next two relocations (if there are that many left),
5133 skipping over any R_XTENSA_NONE relocs. On entry, "nexti" is
5134 the starting reloc index. After these two loops, "i"
5135 is the index of the first non-NONE reloc past that starting
5136 index, and "nexti" is the index for the next non-NONE reloc
5137 after "i". */
5139 for (i = nexti; i < sec->reloc_count; i++)
5141 if (ELF32_R_TYPE (internal_relocs[i].r_info) != R_XTENSA_NONE)
5143 irel = &internal_relocs[i];
5144 break;
5146 internal_relocs[i].r_offset -= removed_bytes;
5149 for (nexti = i + 1; nexti < sec->reloc_count; nexti++)
5151 if (ELF32_R_TYPE (internal_relocs[nexti].r_info)
5152 != R_XTENSA_NONE)
5154 next_irel = &internal_relocs[nexti];
5155 break;
5157 internal_relocs[nexti].r_offset -= removed_bytes;
5160 remove_this_irel = FALSE;
5161 bytes_to_remove = 0;
5162 actual_offset = offset - removed_bytes;
5163 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
5165 /* Check that the irels are sorted by offset,
5166 with only one per address. */
5167 BFD_ASSERT (!irel || (int) irel->r_offset > (int) last_irel_offset);
5168 BFD_ASSERT (!next_irel || next_irel->r_offset > irel->r_offset);
5170 /* Make sure there isn't a reloc on the size field. */
5171 if (irel && irel->r_offset == offset + 4)
5173 irel->r_offset -= removed_bytes;
5174 last_irel_offset = irel->r_offset;
5176 else if (next_irel && next_irel->r_offset == offset + 4)
5178 nexti += 1;
5179 irel->r_offset -= removed_bytes;
5180 next_irel->r_offset -= removed_bytes;
5181 last_irel_offset = next_irel->r_offset;
5183 else if (size == 0)
5185 /* Always remove entries with zero size. */
5186 bytes_to_remove = 8;
5187 if (irel && irel->r_offset == offset)
5189 remove_this_irel = TRUE;
5191 irel->r_offset -= removed_bytes;
5192 last_irel_offset = irel->r_offset;
5195 else if (irel && irel->r_offset == offset)
5197 if (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32)
5199 if (last_irel)
5201 bfd_vma old_size =
5202 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
5203 bfd_vma old_address =
5204 (last_irel->r_addend
5205 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
5206 bfd_vma new_address =
5207 (irel->r_addend
5208 + bfd_get_32 (abfd, &contents[actual_offset]));
5210 if ((ELF32_R_SYM (irel->r_info) ==
5211 ELF32_R_SYM (last_irel->r_info))
5212 && (old_address + old_size == new_address))
5214 /* fix the old size */
5215 bfd_put_32 (abfd, old_size + size,
5216 &contents[last_irel->r_offset + 4]);
5217 bytes_to_remove = 8;
5218 remove_this_irel = TRUE;
5220 else
5221 last_irel = irel;
5223 else
5224 last_irel = irel;
5227 irel->r_offset -= removed_bytes;
5228 last_irel_offset = irel->r_offset;
5231 if (remove_this_irel)
5233 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
5234 irel->r_offset -= bytes_to_remove;
5237 if (bytes_to_remove != 0)
5239 removed_bytes += bytes_to_remove;
5240 if (offset + 8 < section_size)
5241 memmove (&contents[actual_offset],
5242 &contents[actual_offset+8],
5243 section_size - offset - 8);
5247 if (removed_bytes)
5249 /* Clear the removed bytes. */
5250 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
5252 sec->_cooked_size = section_size - removed_bytes;
5253 /* Also shrink _raw_size. (The code in relocate_section that
5254 checks that relocations are within the section must use
5255 _raw_size because of the way the stabs sections are
5256 relaxed; shrinking _raw_size means that these checks will
5257 not be unnecessarily lax.) */
5258 sec->_raw_size = sec->_cooked_size;
5260 if (xtensa_is_littable_section (sec))
5262 bfd *dynobj = elf_hash_table (link_info)->dynobj;
5263 if (dynobj)
5265 asection *sgotloc =
5266 bfd_get_section_by_name (dynobj, ".got.loc");
5267 if (sgotloc)
5269 bfd_size_type sgotloc_size =
5270 (sgotloc->_cooked_size ? sgotloc->_cooked_size
5271 : sgotloc->_raw_size);
5272 sgotloc->_cooked_size = sgotloc_size - removed_bytes;
5273 sgotloc->_raw_size = sgotloc_size - removed_bytes;
5280 error_return:
5281 release_internal_relocs (sec, internal_relocs);
5282 release_contents (sec, contents);
5283 return ok;
5287 /* Third relaxation pass. */
5289 /* Change symbol values to account for removed literals. */
5291 bfd_boolean
5292 relax_section_symbols (abfd, sec)
5293 bfd *abfd;
5294 asection *sec;
5296 xtensa_relax_info *relax_info;
5297 unsigned int sec_shndx;
5298 Elf_Internal_Shdr *symtab_hdr;
5299 Elf_Internal_Sym *isymbuf;
5300 unsigned i, num_syms, num_locals;
5302 relax_info = get_xtensa_relax_info (sec);
5303 BFD_ASSERT (relax_info);
5305 if (!relax_info->is_relaxable_literal_section)
5306 return TRUE;
5308 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5310 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5311 isymbuf = retrieve_local_syms (abfd);
5313 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5314 num_locals = symtab_hdr->sh_info;
5316 /* Adjust the local symbols defined in this section. */
5317 for (i = 0; i < num_locals; i++)
5319 Elf_Internal_Sym *isym = &isymbuf[i];
5321 if (isym->st_shndx == sec_shndx)
5323 bfd_vma new_address = offset_with_removed_literals
5324 (&relax_info->removed_list, isym->st_value);
5325 if (new_address != isym->st_value)
5326 isym->st_value = new_address;
5330 /* Now adjust the global symbols defined in this section. */
5331 for (i = 0; i < (num_syms - num_locals); i++)
5333 struct elf_link_hash_entry *sym_hash;
5335 sym_hash = elf_sym_hashes (abfd)[i];
5337 if (sym_hash->root.type == bfd_link_hash_warning)
5338 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
5340 if ((sym_hash->root.type == bfd_link_hash_defined
5341 || sym_hash->root.type == bfd_link_hash_defweak)
5342 && sym_hash->root.u.def.section == sec)
5344 bfd_vma new_address = offset_with_removed_literals
5345 (&relax_info->removed_list, sym_hash->root.u.def.value);
5346 if (new_address != sym_hash->root.u.def.value)
5347 sym_hash->root.u.def.value = new_address;
5351 return TRUE;
5355 /* "Fix" handling functions, called while performing relocations. */
5357 static void
5358 do_fix_for_relocatable_link (rel, input_bfd, input_section)
5359 Elf_Internal_Rela *rel;
5360 bfd *input_bfd;
5361 asection *input_section;
5363 r_reloc r_rel;
5364 asection *sec, *old_sec;
5365 bfd_vma old_offset;
5366 int r_type = ELF32_R_TYPE (rel->r_info);
5367 reloc_bfd_fix *fix_list;
5368 reloc_bfd_fix *fix;
5370 if (r_type == R_XTENSA_NONE)
5371 return;
5373 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5374 if (fix_list == NULL)
5375 return;
5377 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5378 if (fix == NULL)
5379 return;
5381 r_reloc_init (&r_rel, input_bfd, rel);
5382 old_sec = r_reloc_get_section (&r_rel);
5383 old_offset = r_reloc_get_target_offset (&r_rel);
5385 if (old_sec == NULL || !r_reloc_is_defined (&r_rel))
5387 BFD_ASSERT (r_type == R_XTENSA_ASM_EXPAND);
5388 /* Leave it be. Resolution will happen in a later stage. */
5390 else
5392 sec = fix->target_sec;
5393 rel->r_addend += ((sec->output_offset + fix->target_offset)
5394 - (old_sec->output_offset + old_offset));
5399 static void
5400 do_fix_for_final_link (rel, input_section, relocationp)
5401 Elf_Internal_Rela *rel;
5402 asection *input_section;
5403 bfd_vma *relocationp;
5405 asection *sec;
5406 int r_type = ELF32_R_TYPE (rel->r_info);
5407 reloc_bfd_fix *fix_list;
5408 reloc_bfd_fix *fix;
5410 if (r_type == R_XTENSA_NONE)
5411 return;
5413 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5414 if (fix_list == NULL)
5415 return;
5417 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5418 if (fix == NULL)
5419 return;
5421 sec = fix->target_sec;
5422 *relocationp = (sec->output_section->vma
5423 + sec->output_offset
5424 + fix->target_offset - rel->r_addend);
5428 /* Miscellaneous utility functions.... */
5430 static asection *
5431 elf_xtensa_get_plt_section (dynobj, chunk)
5432 bfd *dynobj;
5433 int chunk;
5435 char plt_name[10];
5437 if (chunk == 0)
5438 return bfd_get_section_by_name (dynobj, ".plt");
5440 sprintf (plt_name, ".plt.%u", chunk);
5441 return bfd_get_section_by_name (dynobj, plt_name);
5445 static asection *
5446 elf_xtensa_get_gotplt_section (dynobj, chunk)
5447 bfd *dynobj;
5448 int chunk;
5450 char got_name[14];
5452 if (chunk == 0)
5453 return bfd_get_section_by_name (dynobj, ".got.plt");
5455 sprintf (got_name, ".got.plt.%u", chunk);
5456 return bfd_get_section_by_name (dynobj, got_name);
5460 /* Get the input section for a given symbol index.
5461 If the symbol is:
5462 . a section symbol, return the section;
5463 . a common symbol, return the common section;
5464 . an undefined symbol, return the undefined section;
5465 . an indirect symbol, follow the links;
5466 . an absolute value, return the absolute section. */
5468 static asection *
5469 get_elf_r_symndx_section (abfd, r_symndx)
5470 bfd *abfd;
5471 unsigned long r_symndx;
5473 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5474 asection *target_sec = NULL;
5475 if (r_symndx < symtab_hdr->sh_info)
5477 Elf_Internal_Sym *isymbuf;
5478 unsigned int section_index;
5480 isymbuf = retrieve_local_syms (abfd);
5481 section_index = isymbuf[r_symndx].st_shndx;
5483 if (section_index == SHN_UNDEF)
5484 target_sec = bfd_und_section_ptr;
5485 else if (section_index > 0 && section_index < SHN_LORESERVE)
5486 target_sec = bfd_section_from_elf_index (abfd, section_index);
5487 else if (section_index == SHN_ABS)
5488 target_sec = bfd_abs_section_ptr;
5489 else if (section_index == SHN_COMMON)
5490 target_sec = bfd_com_section_ptr;
5491 else
5492 /* Who knows? */
5493 target_sec = NULL;
5495 else
5497 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5498 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
5500 while (h->root.type == bfd_link_hash_indirect
5501 || h->root.type == bfd_link_hash_warning)
5502 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5504 switch (h->root.type)
5506 case bfd_link_hash_defined:
5507 case bfd_link_hash_defweak:
5508 target_sec = h->root.u.def.section;
5509 break;
5510 case bfd_link_hash_common:
5511 target_sec = bfd_com_section_ptr;
5512 break;
5513 case bfd_link_hash_undefined:
5514 case bfd_link_hash_undefweak:
5515 target_sec = bfd_und_section_ptr;
5516 break;
5517 default: /* New indirect warning. */
5518 target_sec = bfd_und_section_ptr;
5519 break;
5522 return target_sec;
5526 static struct elf_link_hash_entry *
5527 get_elf_r_symndx_hash_entry (abfd, r_symndx)
5528 bfd *abfd;
5529 unsigned long r_symndx;
5531 unsigned long indx;
5532 struct elf_link_hash_entry *h;
5533 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5535 if (r_symndx < symtab_hdr->sh_info)
5536 return NULL;
5538 indx = r_symndx - symtab_hdr->sh_info;
5539 h = elf_sym_hashes (abfd)[indx];
5540 while (h->root.type == bfd_link_hash_indirect
5541 || h->root.type == bfd_link_hash_warning)
5542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5543 return h;
5547 /* Get the section-relative offset for a symbol number. */
5549 static bfd_vma
5550 get_elf_r_symndx_offset (abfd, r_symndx)
5551 bfd *abfd;
5552 unsigned long r_symndx;
5554 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5555 bfd_vma offset = 0;
5557 if (r_symndx < symtab_hdr->sh_info)
5559 Elf_Internal_Sym *isymbuf;
5560 isymbuf = retrieve_local_syms (abfd);
5561 offset = isymbuf[r_symndx].st_value;
5563 else
5565 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5566 struct elf_link_hash_entry *h =
5567 elf_sym_hashes (abfd)[indx];
5569 while (h->root.type == bfd_link_hash_indirect
5570 || h->root.type == bfd_link_hash_warning)
5571 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5572 if (h->root.type == bfd_link_hash_defined
5573 || h->root.type == bfd_link_hash_defweak)
5574 offset = h->root.u.def.value;
5576 return offset;
5580 static bfd_boolean
5581 pcrel_reloc_fits (opnd, self_address, dest_address)
5582 xtensa_operand opnd;
5583 bfd_vma self_address;
5584 bfd_vma dest_address;
5586 uint32 new_address =
5587 xtensa_operand_do_reloc (opnd, dest_address, self_address);
5588 return (xtensa_operand_encode (opnd, &new_address)
5589 == xtensa_encode_result_ok);
5593 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
5594 static int insn_sec_len = sizeof (XTENSA_INSN_SEC_NAME) - 1;
5595 static int lit_sec_len = sizeof (XTENSA_LIT_SEC_NAME) - 1;
5598 static bfd_boolean
5599 xtensa_is_property_section (sec)
5600 asection *sec;
5602 if (strncmp (XTENSA_INSN_SEC_NAME, sec->name, insn_sec_len) == 0
5603 || strncmp (XTENSA_LIT_SEC_NAME, sec->name, lit_sec_len) == 0)
5604 return TRUE;
5606 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0
5607 && (sec->name[linkonce_len] == 'x'
5608 || sec->name[linkonce_len] == 'p')
5609 && sec->name[linkonce_len + 1] == '.')
5610 return TRUE;
5612 return FALSE;
5616 static bfd_boolean
5617 xtensa_is_littable_section (sec)
5618 asection *sec;
5620 if (strncmp (XTENSA_LIT_SEC_NAME, sec->name, lit_sec_len) == 0)
5621 return TRUE;
5623 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0
5624 && sec->name[linkonce_len] == 'p'
5625 && sec->name[linkonce_len + 1] == '.')
5626 return TRUE;
5628 return FALSE;
5632 static bfd_boolean
5633 is_literal_section (sec)
5634 asection *sec;
5636 /* FIXME: the current definition of this leaves a lot to be desired.... */
5637 if (sec == NULL || sec->name == NULL)
5638 return FALSE;
5639 return (strstr (sec->name, "literal") != NULL);
5643 static int
5644 internal_reloc_compare (ap, bp)
5645 const PTR ap;
5646 const PTR bp;
5648 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
5649 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
5651 return (a->r_offset - b->r_offset);
5655 char *
5656 xtensa_get_property_section_name (sec, base_name)
5657 asection *sec;
5658 const char *base_name;
5660 if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
5662 char *prop_sec_name;
5663 const char *suffix;
5664 char linkonce_kind = 0;
5666 if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
5667 linkonce_kind = 'x';
5668 else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
5669 linkonce_kind = 'p';
5670 else
5671 abort ();
5673 prop_sec_name = (char *) bfd_malloc (strlen (sec->name) + 1);
5674 memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
5675 prop_sec_name[linkonce_len] = linkonce_kind;
5676 prop_sec_name[linkonce_len + 1] = '.';
5678 suffix = sec->name + linkonce_len;
5679 while (*suffix)
5681 suffix += 1;
5682 if (suffix[-1] == '.')
5683 break;
5685 strcpy (prop_sec_name + linkonce_len + 2, suffix);
5687 return prop_sec_name;
5690 return strdup (base_name);
5694 /* Other functions called directly by the linker. */
5696 bfd_boolean
5697 xtensa_callback_required_dependence (abfd, sec, link_info, callback, closure)
5698 bfd *abfd;
5699 asection *sec;
5700 struct bfd_link_info *link_info;
5701 deps_callback_t callback;
5702 PTR closure;
5704 Elf_Internal_Rela *internal_relocs;
5705 bfd_byte *contents;
5706 unsigned i;
5707 bfd_boolean ok = TRUE;
5709 /* ".plt*" sections have no explicit relocations but they contain L32R
5710 instructions that reference the corresponding ".got.plt*" sections. */
5711 if ((sec->flags & SEC_LINKER_CREATED) != 0
5712 && strncmp (sec->name, ".plt", 4) == 0)
5714 asection *sgotplt;
5716 /* Find the corresponding ".got.plt*" section. */
5717 if (sec->name[4] == '\0')
5718 sgotplt = bfd_get_section_by_name (sec->owner, ".got.plt");
5719 else
5721 char got_name[14];
5722 int chunk = 0;
5724 BFD_ASSERT (sec->name[4] == '.');
5725 chunk = strtol (&sec->name[5], NULL, 10);
5727 sprintf (got_name, ".got.plt.%u", chunk);
5728 sgotplt = bfd_get_section_by_name (sec->owner, got_name);
5730 BFD_ASSERT (sgotplt);
5732 /* Assume worst-case offsets: L32R at the very end of the ".plt"
5733 section referencing a literal at the very beginning of
5734 ".got.plt". This is very close to the real dependence, anyway. */
5735 (*callback) (sec, sec->_raw_size, sgotplt, 0, closure);
5738 internal_relocs = retrieve_internal_relocs (abfd, sec,
5739 link_info->keep_memory);
5740 if (internal_relocs == NULL
5741 || sec->reloc_count == 0)
5742 return ok;
5744 /* Cache the contents for the duration of this scan. */
5745 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5746 if (contents == NULL && sec->_raw_size != 0)
5748 ok = FALSE;
5749 goto error_return;
5752 if (xtensa_default_isa == NULL)
5753 xtensa_isa_init ();
5755 for (i = 0; i < sec->reloc_count; i++)
5757 Elf_Internal_Rela *irel = &internal_relocs[i];
5758 if (is_l32r_relocation (sec, contents, irel))
5760 r_reloc l32r_rel;
5761 asection *target_sec;
5762 bfd_vma target_offset;
5764 r_reloc_init (&l32r_rel, abfd, irel);
5765 target_sec = NULL;
5766 target_offset = 0;
5767 /* L32Rs must be local to the input file. */
5768 if (r_reloc_is_defined (&l32r_rel))
5770 target_sec = r_reloc_get_section (&l32r_rel);
5771 target_offset = r_reloc_get_target_offset (&l32r_rel);
5773 (*callback) (sec, irel->r_offset, target_sec, target_offset,
5774 closure);
5778 error_return:
5779 release_internal_relocs (sec, internal_relocs);
5780 release_contents (sec, contents);
5781 return ok;
5784 /* The default literal sections should always be marked as "code" (i.e.,
5785 SHF_EXECINSTR). This is particularly important for the Linux kernel
5786 module loader so that the literals are not placed after the text. */
5787 static struct bfd_elf_special_section const elf_xtensa_special_sections[]=
5789 { ".literal", 8, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5790 { ".init.literal", 13, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5791 { ".fini.literal", 13, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5792 { NULL, 0, 0, 0, 0 }
5796 #ifndef ELF_ARCH
5797 #define TARGET_LITTLE_SYM bfd_elf32_xtensa_le_vec
5798 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
5799 #define TARGET_BIG_SYM bfd_elf32_xtensa_be_vec
5800 #define TARGET_BIG_NAME "elf32-xtensa-be"
5801 #define ELF_ARCH bfd_arch_xtensa
5803 /* The new EM_XTENSA value will be recognized beginning in the Xtensa T1040
5804 release. However, we still have to generate files with the EM_XTENSA_OLD
5805 value so that pre-T1040 tools can read the files. As soon as we stop
5806 caring about pre-T1040 tools, the following two values should be
5807 swapped. At the same time, any other code that uses EM_XTENSA_OLD
5808 (e.g., prep_headers() in elf.c) should be changed to use EM_XTENSA. */
5809 #define ELF_MACHINE_CODE EM_XTENSA_OLD
5810 #define ELF_MACHINE_ALT1 EM_XTENSA
5812 #if XCHAL_HAVE_MMU
5813 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
5814 #else /* !XCHAL_HAVE_MMU */
5815 #define ELF_MAXPAGESIZE 1
5816 #endif /* !XCHAL_HAVE_MMU */
5817 #endif /* ELF_ARCH */
5819 #define elf_backend_can_gc_sections 1
5820 #define elf_backend_can_refcount 1
5821 #define elf_backend_plt_readonly 1
5822 #define elf_backend_got_header_size 4
5823 #define elf_backend_want_dynbss 0
5824 #define elf_backend_want_got_plt 1
5826 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
5828 #define bfd_elf32_bfd_final_link bfd_elf32_bfd_final_link
5829 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
5830 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
5831 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
5832 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
5833 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
5834 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
5836 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
5837 #define elf_backend_check_relocs elf_xtensa_check_relocs
5838 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
5839 #define elf_backend_discard_info elf_xtensa_discard_info
5840 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
5841 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
5842 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
5843 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
5844 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
5845 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
5846 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
5847 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
5848 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
5849 #define elf_backend_modify_segment_map elf_xtensa_modify_segment_map
5850 #define elf_backend_object_p elf_xtensa_object_p
5851 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
5852 #define elf_backend_relocate_section elf_xtensa_relocate_section
5853 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
5854 #define elf_backend_special_sections elf_xtensa_special_sections
5856 #include "elf32-target.h"