2004-05-07 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / bfd / elf32-xtensa.c
blob710a1a6c2810d1fadced70fcbb100169e1822f76
1 /* Xtensa-specific support for 32-bit ELF.
2 Copyright 2003, 2004 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
24 #ifdef ANSI_PROTOTYPES
25 #include <stdarg.h>
26 #else
27 #include <varargs.h>
28 #endif
29 #include <strings.h>
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/xtensa.h"
35 #include "xtensa-isa.h"
36 #include "xtensa-config.h"
38 /* Main interface functions. */
39 static void elf_xtensa_info_to_howto_rela
40 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
41 static reloc_howto_type *elf_xtensa_reloc_type_lookup
42 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
43 extern int xtensa_read_table_entries
44 PARAMS ((bfd *, asection *, property_table_entry **, const char *));
45 static bfd_boolean elf_xtensa_check_relocs
46 PARAMS ((bfd *, struct bfd_link_info *, asection *,
47 const Elf_Internal_Rela *));
48 static void elf_xtensa_hide_symbol
49 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
50 static asection *elf_xtensa_gc_mark_hook
51 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
52 struct elf_link_hash_entry *, Elf_Internal_Sym *));
53 static bfd_boolean elf_xtensa_gc_sweep_hook
54 PARAMS ((bfd *, struct bfd_link_info *, asection *,
55 const Elf_Internal_Rela *));
56 static bfd_boolean elf_xtensa_create_dynamic_sections
57 PARAMS ((bfd *, struct bfd_link_info *));
58 static bfd_boolean elf_xtensa_adjust_dynamic_symbol
59 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
60 static bfd_boolean elf_xtensa_size_dynamic_sections
61 PARAMS ((bfd *, struct bfd_link_info *));
62 static bfd_boolean elf_xtensa_modify_segment_map
63 PARAMS ((bfd *, struct bfd_link_info *));
64 static bfd_boolean elf_xtensa_relocate_section
65 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
66 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
67 static bfd_boolean elf_xtensa_relax_section
68 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *again));
69 static bfd_boolean elf_xtensa_finish_dynamic_symbol
70 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
71 Elf_Internal_Sym *));
72 static bfd_boolean elf_xtensa_finish_dynamic_sections
73 PARAMS ((bfd *, struct bfd_link_info *));
74 static bfd_boolean elf_xtensa_merge_private_bfd_data
75 PARAMS ((bfd *, bfd *));
76 static bfd_boolean elf_xtensa_set_private_flags
77 PARAMS ((bfd *, flagword));
78 extern flagword elf_xtensa_get_private_bfd_flags
79 PARAMS ((bfd *));
80 static bfd_boolean elf_xtensa_print_private_bfd_data
81 PARAMS ((bfd *, PTR));
82 static bfd_boolean elf_xtensa_object_p
83 PARAMS ((bfd *));
84 static void elf_xtensa_final_write_processing
85 PARAMS ((bfd *, bfd_boolean));
86 static enum elf_reloc_type_class elf_xtensa_reloc_type_class
87 PARAMS ((const Elf_Internal_Rela *));
88 static bfd_boolean elf_xtensa_discard_info
89 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *));
90 static bfd_boolean elf_xtensa_ignore_discarded_relocs
91 PARAMS ((asection *));
92 static bfd_boolean elf_xtensa_grok_prstatus
93 PARAMS ((bfd *, Elf_Internal_Note *));
94 static bfd_boolean elf_xtensa_grok_psinfo
95 PARAMS ((bfd *, Elf_Internal_Note *));
96 static bfd_boolean elf_xtensa_new_section_hook
97 PARAMS ((bfd *, asection *));
100 /* Local helper functions. */
102 static bfd_boolean xtensa_elf_dynamic_symbol_p
103 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
104 static int property_table_compare
105 PARAMS ((const PTR, const PTR));
106 static bfd_boolean elf_xtensa_in_literal_pool
107 PARAMS ((property_table_entry *, int, bfd_vma));
108 static void elf_xtensa_make_sym_local
109 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
110 static bfd_boolean add_extra_plt_sections
111 PARAMS ((bfd *, int));
112 static bfd_boolean elf_xtensa_fix_refcounts
113 PARAMS ((struct elf_link_hash_entry *, PTR));
114 static bfd_boolean elf_xtensa_allocate_plt_size
115 PARAMS ((struct elf_link_hash_entry *, PTR));
116 static bfd_boolean elf_xtensa_allocate_got_size
117 PARAMS ((struct elf_link_hash_entry *, PTR));
118 static void elf_xtensa_allocate_local_got_size
119 PARAMS ((struct bfd_link_info *, asection *));
120 static bfd_reloc_status_type elf_xtensa_do_reloc
121 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_vma, bfd_byte *,
122 bfd_vma, bfd_boolean, char **));
123 static char * vsprint_msg
124 VPARAMS ((const char *, const char *, int, ...));
125 static char *build_encoding_error_message
126 PARAMS ((xtensa_opcode, xtensa_encode_result));
127 static bfd_reloc_status_type bfd_elf_xtensa_reloc
128 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
129 static void do_fix_for_relocatable_link
130 PARAMS ((Elf_Internal_Rela *, bfd *, asection *));
131 static void do_fix_for_final_link
132 PARAMS ((Elf_Internal_Rela *, asection *, bfd_vma *));
133 static bfd_vma elf_xtensa_create_plt_entry
134 PARAMS ((bfd *, bfd *, unsigned));
135 static int elf_xtensa_combine_prop_entries
136 PARAMS ((bfd *, asection *, asection *));
137 static bfd_boolean elf_xtensa_discard_info_for_section
138 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *,
139 asection *));
141 /* Local functions to handle Xtensa configurability. */
143 static void init_call_opcodes
144 PARAMS ((void));
145 static bfd_boolean is_indirect_call_opcode
146 PARAMS ((xtensa_opcode));
147 static bfd_boolean is_direct_call_opcode
148 PARAMS ((xtensa_opcode));
149 static bfd_boolean is_windowed_call_opcode
150 PARAMS ((xtensa_opcode));
151 static xtensa_opcode get_l32r_opcode
152 PARAMS ((void));
153 static bfd_vma l32r_offset
154 PARAMS ((bfd_vma, bfd_vma));
155 static int get_relocation_opnd
156 PARAMS ((Elf_Internal_Rela *));
157 static xtensa_opcode get_relocation_opcode
158 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *));
159 static bfd_boolean is_l32r_relocation
160 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *));
162 /* Functions for link-time code simplifications. */
164 static bfd_reloc_status_type elf_xtensa_do_asm_simplify
165 PARAMS ((bfd_byte *, bfd_vma, bfd_vma));
166 static bfd_reloc_status_type contract_asm_expansion
167 PARAMS ((bfd_byte *, bfd_vma, Elf_Internal_Rela *));
168 static xtensa_opcode swap_callx_for_call_opcode
169 PARAMS ((xtensa_opcode));
170 static xtensa_opcode get_expanded_call_opcode
171 PARAMS ((bfd_byte *, int));
173 /* Access to internal relocations, section contents and symbols. */
175 static Elf_Internal_Rela *retrieve_internal_relocs
176 PARAMS ((bfd *, asection *, bfd_boolean));
177 static void pin_internal_relocs
178 PARAMS ((asection *, Elf_Internal_Rela *));
179 static void release_internal_relocs
180 PARAMS ((asection *, Elf_Internal_Rela *));
181 static bfd_byte *retrieve_contents
182 PARAMS ((bfd *, asection *, bfd_boolean));
183 static void pin_contents
184 PARAMS ((asection *, bfd_byte *));
185 static void release_contents
186 PARAMS ((asection *, bfd_byte *));
187 static Elf_Internal_Sym *retrieve_local_syms
188 PARAMS ((bfd *));
190 /* Miscellaneous utility functions. */
192 static asection *elf_xtensa_get_plt_section
193 PARAMS ((bfd *, int));
194 static asection *elf_xtensa_get_gotplt_section
195 PARAMS ((bfd *, int));
196 static asection *get_elf_r_symndx_section
197 PARAMS ((bfd *, unsigned long));
198 static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
199 PARAMS ((bfd *, unsigned long));
200 static bfd_vma get_elf_r_symndx_offset
201 PARAMS ((bfd *, unsigned long));
202 static bfd_boolean pcrel_reloc_fits
203 PARAMS ((xtensa_operand, bfd_vma, bfd_vma));
204 static bfd_boolean xtensa_is_property_section
205 PARAMS ((asection *));
206 static bfd_boolean xtensa_is_littable_section
207 PARAMS ((asection *));
208 static bfd_boolean is_literal_section
209 PARAMS ((asection *));
210 static int internal_reloc_compare
211 PARAMS ((const PTR, const PTR));
212 extern char *xtensa_get_property_section_name
213 PARAMS ((asection *, const char *));
215 /* Other functions called directly by the linker. */
217 typedef void (*deps_callback_t)
218 PARAMS ((asection *, bfd_vma, asection *, bfd_vma, PTR));
219 extern bfd_boolean xtensa_callback_required_dependence
220 PARAMS ((bfd *, asection *, struct bfd_link_info *,
221 deps_callback_t, PTR));
224 typedef struct xtensa_relax_info_struct xtensa_relax_info;
227 /* Total count of PLT relocations seen during check_relocs.
228 The actual PLT code must be split into multiple sections and all
229 the sections have to be created before size_dynamic_sections,
230 where we figure out the exact number of PLT entries that will be
231 needed. It is OK if this count is an overestimate, e.g., some
232 relocations may be removed by GC. */
234 static int plt_reloc_count = 0;
237 /* When this is true, relocations may have been modified to refer to
238 symbols from other input files. The per-section list of "fix"
239 records needs to be checked when resolving relocations. */
241 static bfd_boolean relaxing_section = FALSE;
244 static reloc_howto_type elf_howto_table[] =
246 HOWTO (R_XTENSA_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
247 bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
248 FALSE, 0x00000000, 0x00000000, FALSE),
249 HOWTO (R_XTENSA_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
250 bfd_elf_xtensa_reloc, "R_XTENSA_32",
251 TRUE, 0xffffffff, 0xffffffff, FALSE),
252 /* Replace a 32-bit value with a value from the runtime linker (only
253 used by linker-generated stub functions). The r_addend value is
254 special: 1 means to substitute a pointer to the runtime linker's
255 dynamic resolver function; 2 means to substitute the link map for
256 the shared object. */
257 HOWTO (R_XTENSA_RTLD, 0, 2, 32, FALSE, 0, complain_overflow_dont,
258 NULL, "R_XTENSA_RTLD",
259 FALSE, 0x00000000, 0x00000000, FALSE),
260 HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
261 bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
262 FALSE, 0xffffffff, 0xffffffff, FALSE),
263 HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
264 bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
265 FALSE, 0xffffffff, 0xffffffff, FALSE),
266 HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
267 bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
268 FALSE, 0xffffffff, 0xffffffff, FALSE),
269 HOWTO (R_XTENSA_PLT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
270 bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
271 FALSE, 0xffffffff, 0xffffffff, FALSE),
272 EMPTY_HOWTO (7),
273 HOWTO (R_XTENSA_OP0, 0, 0, 0, TRUE, 0, complain_overflow_dont,
274 bfd_elf_xtensa_reloc, "R_XTENSA_OP0",
275 FALSE, 0x00000000, 0x00000000, TRUE),
276 HOWTO (R_XTENSA_OP1, 0, 0, 0, TRUE, 0, complain_overflow_dont,
277 bfd_elf_xtensa_reloc, "R_XTENSA_OP1",
278 FALSE, 0x00000000, 0x00000000, TRUE),
279 HOWTO (R_XTENSA_OP2, 0, 0, 0, TRUE, 0, complain_overflow_dont,
280 bfd_elf_xtensa_reloc, "R_XTENSA_OP2",
281 FALSE, 0x00000000, 0x00000000, TRUE),
282 /* Assembly auto-expansion. */
283 HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, TRUE, 0, complain_overflow_dont,
284 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND",
285 FALSE, 0x00000000, 0x00000000, FALSE),
286 /* Relax assembly auto-expansion. */
287 HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, TRUE, 0, complain_overflow_dont,
288 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY",
289 FALSE, 0x00000000, 0x00000000, TRUE),
290 EMPTY_HOWTO (13),
291 EMPTY_HOWTO (14),
292 /* GNU extension to record C++ vtable hierarchy. */
293 HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, FALSE, 0, complain_overflow_dont,
294 NULL, "R_XTENSA_GNU_VTINHERIT",
295 FALSE, 0x00000000, 0x00000000, FALSE),
296 /* GNU extension to record C++ vtable member usage. */
297 HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, FALSE, 0, complain_overflow_dont,
298 _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
299 FALSE, 0x00000000, 0x00000000, FALSE)
302 #ifdef DEBUG_GEN_RELOC
303 #define TRACE(str) \
304 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
305 #else
306 #define TRACE(str)
307 #endif
309 static reloc_howto_type *
310 elf_xtensa_reloc_type_lookup (abfd, code)
311 bfd *abfd ATTRIBUTE_UNUSED;
312 bfd_reloc_code_real_type code;
314 switch (code)
316 case BFD_RELOC_NONE:
317 TRACE ("BFD_RELOC_NONE");
318 return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
320 case BFD_RELOC_32:
321 TRACE ("BFD_RELOC_32");
322 return &elf_howto_table[(unsigned) R_XTENSA_32 ];
324 case BFD_RELOC_XTENSA_RTLD:
325 TRACE ("BFD_RELOC_XTENSA_RTLD");
326 return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
328 case BFD_RELOC_XTENSA_GLOB_DAT:
329 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
330 return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
332 case BFD_RELOC_XTENSA_JMP_SLOT:
333 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
334 return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
336 case BFD_RELOC_XTENSA_RELATIVE:
337 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
338 return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
340 case BFD_RELOC_XTENSA_PLT:
341 TRACE ("BFD_RELOC_XTENSA_PLT");
342 return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
344 case BFD_RELOC_XTENSA_OP0:
345 TRACE ("BFD_RELOC_XTENSA_OP0");
346 return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
348 case BFD_RELOC_XTENSA_OP1:
349 TRACE ("BFD_RELOC_XTENSA_OP1");
350 return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
352 case BFD_RELOC_XTENSA_OP2:
353 TRACE ("BFD_RELOC_XTENSA_OP2");
354 return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
356 case BFD_RELOC_XTENSA_ASM_EXPAND:
357 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
358 return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
360 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
361 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
362 return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
364 case BFD_RELOC_VTABLE_INHERIT:
365 TRACE ("BFD_RELOC_VTABLE_INHERIT");
366 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
368 case BFD_RELOC_VTABLE_ENTRY:
369 TRACE ("BFD_RELOC_VTABLE_ENTRY");
370 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
372 default:
373 break;
376 TRACE ("Unknown");
377 return NULL;
381 /* Given an ELF "rela" relocation, find the corresponding howto and record
382 it in the BFD internal arelent representation of the relocation. */
384 static void
385 elf_xtensa_info_to_howto_rela (abfd, cache_ptr, dst)
386 bfd *abfd ATTRIBUTE_UNUSED;
387 arelent *cache_ptr;
388 Elf_Internal_Rela *dst;
390 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
392 BFD_ASSERT (r_type < (unsigned int) R_XTENSA_max);
393 cache_ptr->howto = &elf_howto_table[r_type];
397 /* Functions for the Xtensa ELF linker. */
399 /* The name of the dynamic interpreter. This is put in the .interp
400 section. */
402 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
404 /* The size in bytes of an entry in the procedure linkage table.
405 (This does _not_ include the space for the literals associated with
406 the PLT entry.) */
408 #define PLT_ENTRY_SIZE 16
410 /* For _really_ large PLTs, we may need to alternate between literals
411 and code to keep the literals within the 256K range of the L32R
412 instructions in the code. It's unlikely that anyone would ever need
413 such a big PLT, but an arbitrary limit on the PLT size would be bad.
414 Thus, we split the PLT into chunks. Since there's very little
415 overhead (2 extra literals) for each chunk, the chunk size is kept
416 small so that the code for handling multiple chunks get used and
417 tested regularly. With 254 entries, there are 1K of literals for
418 each chunk, and that seems like a nice round number. */
420 #define PLT_ENTRIES_PER_CHUNK 254
422 /* PLT entries are actually used as stub functions for lazy symbol
423 resolution. Once the symbol is resolved, the stub function is never
424 invoked. Note: the 32-byte frame size used here cannot be changed
425 without a corresponding change in the runtime linker. */
427 static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
429 0x6c, 0x10, 0x04, /* entry sp, 32 */
430 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
431 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
432 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
433 0x0a, 0x80, 0x00, /* jx a8 */
434 0 /* unused */
437 static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
439 0x36, 0x41, 0x00, /* entry sp, 32 */
440 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
441 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
442 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
443 0xa0, 0x08, 0x00, /* jx a8 */
444 0 /* unused */
448 static inline bfd_boolean
449 xtensa_elf_dynamic_symbol_p (h, info)
450 struct elf_link_hash_entry *h;
451 struct bfd_link_info *info;
453 /* Check if we should do dynamic things to this symbol. The
454 "ignore_protected" argument need not be set, because Xtensa code
455 does not require special handling of STV_PROTECTED to make function
456 pointer comparisons work properly. The PLT addresses are never
457 used for function pointers. */
459 return _bfd_elf_dynamic_symbol_p (h, info, 0);
463 static int
464 property_table_compare (ap, bp)
465 const PTR ap;
466 const PTR bp;
468 const property_table_entry *a = (const property_table_entry *) ap;
469 const property_table_entry *b = (const property_table_entry *) bp;
471 /* Check if one entry overlaps with the other; this shouldn't happen
472 except when searching for a match. */
473 if ((b->address >= a->address && b->address < (a->address + a->size))
474 || (a->address >= b->address && a->address < (b->address + b->size)))
475 return 0;
477 return (a->address - b->address);
481 /* Get the literal table or instruction table entries for the given
482 section. Sets TABLE_P and returns the number of entries. On error,
483 returns a negative value. */
486 xtensa_read_table_entries (abfd, section, table_p, sec_name)
487 bfd *abfd;
488 asection *section;
489 property_table_entry **table_p;
490 const char *sec_name;
492 asection *table_section;
493 char *table_section_name;
494 bfd_size_type table_size = 0;
495 bfd_byte *table_data;
496 property_table_entry *blocks;
497 int block_count;
498 bfd_size_type num_records;
499 Elf_Internal_Rela *internal_relocs;
500 bfd_vma section_addr;
502 table_section_name =
503 xtensa_get_property_section_name (section, sec_name);
504 table_section = bfd_get_section_by_name (abfd, table_section_name);
505 free (table_section_name);
506 if (table_section != NULL)
507 table_size = (table_section->_cooked_size
508 ? table_section->_cooked_size : table_section->_raw_size);
510 if (table_size == 0)
512 *table_p = NULL;
513 return 0;
516 num_records = table_size / 8;
517 table_data = retrieve_contents (abfd, table_section, TRUE);
518 blocks = (property_table_entry *)
519 bfd_malloc (num_records * sizeof (property_table_entry));
520 block_count = 0;
522 section_addr = section->output_section->vma + section->output_offset;
524 /* If the file has not yet been relocated, process the relocations
525 and sort out the table entries that apply to the specified section. */
526 internal_relocs = retrieve_internal_relocs (abfd, table_section, TRUE);
527 if (internal_relocs && !table_section->reloc_done)
529 unsigned i;
531 for (i = 0; i < table_section->reloc_count; i++)
533 Elf_Internal_Rela *rel = &internal_relocs[i];
534 unsigned long r_symndx;
536 if (ELF32_R_TYPE (rel->r_info) == R_XTENSA_NONE)
537 continue;
539 BFD_ASSERT (ELF32_R_TYPE (rel->r_info) == R_XTENSA_32);
540 r_symndx = ELF32_R_SYM (rel->r_info);
542 if (get_elf_r_symndx_section (abfd, r_symndx) == section)
544 bfd_vma sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
545 blocks[block_count].address =
546 (section_addr + sym_off + rel->r_addend
547 + bfd_get_32 (abfd, table_data + rel->r_offset));
548 blocks[block_count].size =
549 bfd_get_32 (abfd, table_data + rel->r_offset + 4);
550 block_count++;
554 else
556 /* The file has already been relocated and the addresses are
557 already in the table. */
558 bfd_vma off;
560 for (off = 0; off < table_size; off += 8)
562 bfd_vma address = bfd_get_32 (abfd, table_data + off);
564 if (address >= section_addr
565 && address < ( section_addr + section->_raw_size))
567 blocks[block_count].address = address;
568 blocks[block_count].size =
569 bfd_get_32 (abfd, table_data + off + 4);
570 block_count++;
575 release_contents (table_section, table_data);
576 release_internal_relocs (table_section, internal_relocs);
578 if (block_count > 0)
580 /* Now sort them into address order for easy reference. */
581 qsort (blocks, block_count, sizeof (property_table_entry),
582 property_table_compare);
585 *table_p = blocks;
586 return block_count;
590 static bfd_boolean
591 elf_xtensa_in_literal_pool (lit_table, lit_table_size, addr)
592 property_table_entry *lit_table;
593 int lit_table_size;
594 bfd_vma addr;
596 property_table_entry entry;
598 if (lit_table_size == 0)
599 return FALSE;
601 entry.address = addr;
602 entry.size = 1;
604 if (bsearch (&entry, lit_table, lit_table_size,
605 sizeof (property_table_entry), property_table_compare))
606 return TRUE;
608 return FALSE;
612 /* Look through the relocs for a section during the first phase, and
613 calculate needed space in the dynamic reloc sections. */
615 static bfd_boolean
616 elf_xtensa_check_relocs (abfd, info, sec, relocs)
617 bfd *abfd;
618 struct bfd_link_info *info;
619 asection *sec;
620 const Elf_Internal_Rela *relocs;
622 Elf_Internal_Shdr *symtab_hdr;
623 struct elf_link_hash_entry **sym_hashes;
624 const Elf_Internal_Rela *rel;
625 const Elf_Internal_Rela *rel_end;
627 if (info->relocatable)
628 return TRUE;
630 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
631 sym_hashes = elf_sym_hashes (abfd);
633 rel_end = relocs + sec->reloc_count;
634 for (rel = relocs; rel < rel_end; rel++)
636 unsigned int r_type;
637 unsigned long r_symndx;
638 struct elf_link_hash_entry *h;
640 r_symndx = ELF32_R_SYM (rel->r_info);
641 r_type = ELF32_R_TYPE (rel->r_info);
643 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
645 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
646 bfd_archive_filename (abfd),
647 r_symndx);
648 return FALSE;
651 if (r_symndx < symtab_hdr->sh_info)
652 h = NULL;
653 else
655 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
656 while (h->root.type == bfd_link_hash_indirect
657 || h->root.type == bfd_link_hash_warning)
658 h = (struct elf_link_hash_entry *) h->root.u.i.link;
661 switch (r_type)
663 case R_XTENSA_32:
664 if (h == NULL)
665 goto local_literal;
667 if ((sec->flags & SEC_ALLOC) != 0)
669 if (h->got.refcount <= 0)
670 h->got.refcount = 1;
671 else
672 h->got.refcount += 1;
674 break;
676 case R_XTENSA_PLT:
677 /* If this relocation is against a local symbol, then it's
678 exactly the same as a normal local GOT entry. */
679 if (h == NULL)
680 goto local_literal;
682 if ((sec->flags & SEC_ALLOC) != 0)
684 if (h->plt.refcount <= 0)
686 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
687 h->plt.refcount = 1;
689 else
690 h->plt.refcount += 1;
692 /* Keep track of the total PLT relocation count even if we
693 don't yet know whether the dynamic sections will be
694 created. */
695 plt_reloc_count += 1;
697 if (elf_hash_table (info)->dynamic_sections_created)
699 if (!add_extra_plt_sections (elf_hash_table (info)->dynobj,
700 plt_reloc_count))
701 return FALSE;
704 break;
706 local_literal:
707 if ((sec->flags & SEC_ALLOC) != 0)
709 bfd_signed_vma *local_got_refcounts;
711 /* This is a global offset table entry for a local symbol. */
712 local_got_refcounts = elf_local_got_refcounts (abfd);
713 if (local_got_refcounts == NULL)
715 bfd_size_type size;
717 size = symtab_hdr->sh_info;
718 size *= sizeof (bfd_signed_vma);
719 local_got_refcounts = ((bfd_signed_vma *)
720 bfd_zalloc (abfd, size));
721 if (local_got_refcounts == NULL)
722 return FALSE;
723 elf_local_got_refcounts (abfd) = local_got_refcounts;
725 local_got_refcounts[r_symndx] += 1;
727 break;
729 case R_XTENSA_OP0:
730 case R_XTENSA_OP1:
731 case R_XTENSA_OP2:
732 case R_XTENSA_ASM_EXPAND:
733 case R_XTENSA_ASM_SIMPLIFY:
734 /* Nothing to do for these. */
735 break;
737 case R_XTENSA_GNU_VTINHERIT:
738 /* This relocation describes the C++ object vtable hierarchy.
739 Reconstruct it for later use during GC. */
740 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
741 return FALSE;
742 break;
744 case R_XTENSA_GNU_VTENTRY:
745 /* This relocation describes which C++ vtable entries are actually
746 used. Record for later use during GC. */
747 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
748 return FALSE;
749 break;
751 default:
752 break;
756 return TRUE;
760 static void
761 elf_xtensa_hide_symbol (info, h, force_local)
762 struct bfd_link_info *info;
763 struct elf_link_hash_entry *h;
764 bfd_boolean force_local;
766 /* For a shared link, move the plt refcount to the got refcount to leave
767 space for RELATIVE relocs. */
768 elf_xtensa_make_sym_local (info, h);
770 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
774 /* Return the section that should be marked against GC for a given
775 relocation. */
777 static asection *
778 elf_xtensa_gc_mark_hook (sec, info, rel, h, sym)
779 asection *sec;
780 struct bfd_link_info *info ATTRIBUTE_UNUSED;
781 Elf_Internal_Rela *rel;
782 struct elf_link_hash_entry *h;
783 Elf_Internal_Sym *sym;
785 if (h != NULL)
787 switch (ELF32_R_TYPE (rel->r_info))
789 case R_XTENSA_GNU_VTINHERIT:
790 case R_XTENSA_GNU_VTENTRY:
791 break;
793 default:
794 switch (h->root.type)
796 case bfd_link_hash_defined:
797 case bfd_link_hash_defweak:
798 return h->root.u.def.section;
800 case bfd_link_hash_common:
801 return h->root.u.c.p->section;
803 default:
804 break;
808 else
809 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
811 return NULL;
814 /* Update the GOT & PLT entry reference counts
815 for the section being removed. */
817 static bfd_boolean
818 elf_xtensa_gc_sweep_hook (abfd, info, sec, relocs)
819 bfd *abfd;
820 struct bfd_link_info *info ATTRIBUTE_UNUSED;
821 asection *sec;
822 const Elf_Internal_Rela *relocs;
824 Elf_Internal_Shdr *symtab_hdr;
825 struct elf_link_hash_entry **sym_hashes;
826 bfd_signed_vma *local_got_refcounts;
827 const Elf_Internal_Rela *rel, *relend;
829 if ((sec->flags & SEC_ALLOC) == 0)
830 return TRUE;
832 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
833 sym_hashes = elf_sym_hashes (abfd);
834 local_got_refcounts = elf_local_got_refcounts (abfd);
836 relend = relocs + sec->reloc_count;
837 for (rel = relocs; rel < relend; rel++)
839 unsigned long r_symndx;
840 unsigned int r_type;
841 struct elf_link_hash_entry *h = NULL;
843 r_symndx = ELF32_R_SYM (rel->r_info);
844 if (r_symndx >= symtab_hdr->sh_info)
845 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
847 r_type = ELF32_R_TYPE (rel->r_info);
848 switch (r_type)
850 case R_XTENSA_32:
851 if (h == NULL)
852 goto local_literal;
853 if (h->got.refcount > 0)
854 h->got.refcount--;
855 break;
857 case R_XTENSA_PLT:
858 if (h == NULL)
859 goto local_literal;
860 if (h->plt.refcount > 0)
861 h->plt.refcount--;
862 break;
864 local_literal:
865 if (local_got_refcounts[r_symndx] > 0)
866 local_got_refcounts[r_symndx] -= 1;
867 break;
869 default:
870 break;
874 return TRUE;
878 /* Create all the dynamic sections. */
880 static bfd_boolean
881 elf_xtensa_create_dynamic_sections (dynobj, info)
882 bfd *dynobj;
883 struct bfd_link_info *info;
885 flagword flags, noalloc_flags;
886 asection *s;
888 /* First do all the standard stuff. */
889 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
890 return FALSE;
892 /* Create any extra PLT sections in case check_relocs has already
893 been called on all the non-dynamic input files. */
894 if (!add_extra_plt_sections (dynobj, plt_reloc_count))
895 return FALSE;
897 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
898 | SEC_LINKER_CREATED | SEC_READONLY);
899 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
901 /* Mark the ".got.plt" section READONLY. */
902 s = bfd_get_section_by_name (dynobj, ".got.plt");
903 if (s == NULL
904 || ! bfd_set_section_flags (dynobj, s, flags))
905 return FALSE;
907 /* Create ".rela.got". */
908 s = bfd_make_section (dynobj, ".rela.got");
909 if (s == NULL
910 || ! bfd_set_section_flags (dynobj, s, flags)
911 || ! bfd_set_section_alignment (dynobj, s, 2))
912 return FALSE;
914 /* Create ".got.loc" (literal tables for use by dynamic linker). */
915 s = bfd_make_section (dynobj, ".got.loc");
916 if (s == NULL
917 || ! bfd_set_section_flags (dynobj, s, flags)
918 || ! bfd_set_section_alignment (dynobj, s, 2))
919 return FALSE;
921 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
922 s = bfd_make_section (dynobj, ".xt.lit.plt");
923 if (s == NULL
924 || ! bfd_set_section_flags (dynobj, s, noalloc_flags)
925 || ! bfd_set_section_alignment (dynobj, s, 2))
926 return FALSE;
928 return TRUE;
932 static bfd_boolean
933 add_extra_plt_sections (dynobj, count)
934 bfd *dynobj;
935 int count;
937 int chunk;
939 /* Iterate over all chunks except 0 which uses the standard ".plt" and
940 ".got.plt" sections. */
941 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
943 char *sname;
944 flagword flags;
945 asection *s;
947 /* Stop when we find a section has already been created. */
948 if (elf_xtensa_get_plt_section (dynobj, chunk))
949 break;
951 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
952 | SEC_LINKER_CREATED | SEC_READONLY);
954 sname = (char *) bfd_malloc (10);
955 sprintf (sname, ".plt.%u", chunk);
956 s = bfd_make_section (dynobj, sname);
957 if (s == NULL
958 || ! bfd_set_section_flags (dynobj, s, flags | SEC_CODE)
959 || ! bfd_set_section_alignment (dynobj, s, 2))
960 return FALSE;
962 sname = (char *) bfd_malloc (14);
963 sprintf (sname, ".got.plt.%u", chunk);
964 s = bfd_make_section (dynobj, sname);
965 if (s == NULL
966 || ! bfd_set_section_flags (dynobj, s, flags)
967 || ! bfd_set_section_alignment (dynobj, s, 2))
968 return FALSE;
971 return TRUE;
975 /* Adjust a symbol defined by a dynamic object and referenced by a
976 regular object. The current definition is in some section of the
977 dynamic object, but we're not including those sections. We have to
978 change the definition to something the rest of the link can
979 understand. */
981 static bfd_boolean
982 elf_xtensa_adjust_dynamic_symbol (info, h)
983 struct bfd_link_info *info ATTRIBUTE_UNUSED;
984 struct elf_link_hash_entry *h;
986 /* If this is a weak symbol, and there is a real definition, the
987 processor independent code will have arranged for us to see the
988 real definition first, and we can just use the same value. */
989 if (h->weakdef != NULL)
991 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
992 || h->weakdef->root.type == bfd_link_hash_defweak);
993 h->root.u.def.section = h->weakdef->root.u.def.section;
994 h->root.u.def.value = h->weakdef->root.u.def.value;
995 return TRUE;
998 /* This is a reference to a symbol defined by a dynamic object. The
999 reference must go through the GOT, so there's no need for COPY relocs,
1000 .dynbss, etc. */
1002 return TRUE;
1006 static void
1007 elf_xtensa_make_sym_local (info, h)
1008 struct bfd_link_info *info;
1009 struct elf_link_hash_entry *h;
1011 if (info->shared)
1013 if (h->plt.refcount > 0)
1015 /* Will use RELATIVE relocs instead of JMP_SLOT relocs. */
1016 if (h->got.refcount < 0)
1017 h->got.refcount = 0;
1018 h->got.refcount += h->plt.refcount;
1019 h->plt.refcount = 0;
1022 else
1024 /* Don't need any dynamic relocations at all. */
1025 h->plt.refcount = 0;
1026 h->got.refcount = 0;
1031 static bfd_boolean
1032 elf_xtensa_fix_refcounts (h, arg)
1033 struct elf_link_hash_entry *h;
1034 PTR arg;
1036 struct bfd_link_info *info = (struct bfd_link_info *) arg;
1038 if (h->root.type == bfd_link_hash_warning)
1039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1041 if (! xtensa_elf_dynamic_symbol_p (h, info))
1042 elf_xtensa_make_sym_local (info, h);
1044 return TRUE;
1048 static bfd_boolean
1049 elf_xtensa_allocate_plt_size (h, arg)
1050 struct elf_link_hash_entry *h;
1051 PTR arg;
1053 asection *srelplt = (asection *) arg;
1055 if (h->root.type == bfd_link_hash_warning)
1056 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1058 if (h->plt.refcount > 0)
1059 srelplt->_raw_size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1061 return TRUE;
1065 static bfd_boolean
1066 elf_xtensa_allocate_got_size (h, arg)
1067 struct elf_link_hash_entry *h;
1068 PTR arg;
1070 asection *srelgot = (asection *) arg;
1072 if (h->root.type == bfd_link_hash_warning)
1073 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1075 if (h->got.refcount > 0)
1076 srelgot->_raw_size += (h->got.refcount * sizeof (Elf32_External_Rela));
1078 return TRUE;
1082 static void
1083 elf_xtensa_allocate_local_got_size (info, srelgot)
1084 struct bfd_link_info *info;
1085 asection *srelgot;
1087 bfd *i;
1089 for (i = info->input_bfds; i; i = i->link_next)
1091 bfd_signed_vma *local_got_refcounts;
1092 bfd_size_type j, cnt;
1093 Elf_Internal_Shdr *symtab_hdr;
1095 local_got_refcounts = elf_local_got_refcounts (i);
1096 if (!local_got_refcounts)
1097 continue;
1099 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1100 cnt = symtab_hdr->sh_info;
1102 for (j = 0; j < cnt; ++j)
1104 if (local_got_refcounts[j] > 0)
1105 srelgot->_raw_size += (local_got_refcounts[j]
1106 * sizeof (Elf32_External_Rela));
1112 /* Set the sizes of the dynamic sections. */
1114 static bfd_boolean
1115 elf_xtensa_size_dynamic_sections (output_bfd, info)
1116 bfd *output_bfd ATTRIBUTE_UNUSED;
1117 struct bfd_link_info *info;
1119 bfd *dynobj, *abfd;
1120 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
1121 bfd_boolean relplt, relgot;
1122 int plt_entries, plt_chunks, chunk;
1124 plt_entries = 0;
1125 plt_chunks = 0;
1126 srelgot = 0;
1128 dynobj = elf_hash_table (info)->dynobj;
1129 if (dynobj == NULL)
1130 abort ();
1132 if (elf_hash_table (info)->dynamic_sections_created)
1134 /* Set the contents of the .interp section to the interpreter. */
1135 if (info->executable)
1137 s = bfd_get_section_by_name (dynobj, ".interp");
1138 if (s == NULL)
1139 abort ();
1140 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1141 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1144 /* Allocate room for one word in ".got". */
1145 s = bfd_get_section_by_name (dynobj, ".got");
1146 if (s == NULL)
1147 abort ();
1148 s->_raw_size = 4;
1150 /* Adjust refcounts for symbols that we now know are not "dynamic". */
1151 elf_link_hash_traverse (elf_hash_table (info),
1152 elf_xtensa_fix_refcounts,
1153 (PTR) info);
1155 /* Allocate space in ".rela.got" for literals that reference
1156 global symbols. */
1157 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1158 if (srelgot == NULL)
1159 abort ();
1160 elf_link_hash_traverse (elf_hash_table (info),
1161 elf_xtensa_allocate_got_size,
1162 (PTR) srelgot);
1164 /* If we are generating a shared object, we also need space in
1165 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1166 reference local symbols. */
1167 if (info->shared)
1168 elf_xtensa_allocate_local_got_size (info, srelgot);
1170 /* Allocate space in ".rela.plt" for literals that have PLT entries. */
1171 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1172 if (srelplt == NULL)
1173 abort ();
1174 elf_link_hash_traverse (elf_hash_table (info),
1175 elf_xtensa_allocate_plt_size,
1176 (PTR) srelplt);
1178 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1179 each PLT entry, we need the PLT code plus a 4-byte literal.
1180 For each chunk of ".plt", we also need two more 4-byte
1181 literals, two corresponding entries in ".rela.got", and an
1182 8-byte entry in ".xt.lit.plt". */
1183 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
1184 if (spltlittbl == NULL)
1185 abort ();
1187 plt_entries = srelplt->_raw_size / sizeof (Elf32_External_Rela);
1188 plt_chunks =
1189 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1191 /* Iterate over all the PLT chunks, including any extra sections
1192 created earlier because the initial count of PLT relocations
1193 was an overestimate. */
1194 for (chunk = 0;
1195 (splt = elf_xtensa_get_plt_section (dynobj, chunk)) != NULL;
1196 chunk++)
1198 int chunk_entries;
1200 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1201 if (sgotplt == NULL)
1202 abort ();
1204 if (chunk < plt_chunks - 1)
1205 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1206 else if (chunk == plt_chunks - 1)
1207 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1208 else
1209 chunk_entries = 0;
1211 if (chunk_entries != 0)
1213 sgotplt->_raw_size = 4 * (chunk_entries + 2);
1214 splt->_raw_size = PLT_ENTRY_SIZE * chunk_entries;
1215 srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
1216 spltlittbl->_raw_size += 8;
1218 else
1220 sgotplt->_raw_size = 0;
1221 splt->_raw_size = 0;
1225 /* Allocate space in ".got.loc" to match the total size of all the
1226 literal tables. */
1227 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
1228 if (sgotloc == NULL)
1229 abort ();
1230 sgotloc->_raw_size = spltlittbl->_raw_size;
1231 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1233 if (abfd->flags & DYNAMIC)
1234 continue;
1235 for (s = abfd->sections; s != NULL; s = s->next)
1237 if (! elf_discarded_section (s)
1238 && xtensa_is_littable_section (s)
1239 && s != spltlittbl)
1240 sgotloc->_raw_size += s->_raw_size;
1245 /* Allocate memory for dynamic sections. */
1246 relplt = FALSE;
1247 relgot = FALSE;
1248 for (s = dynobj->sections; s != NULL; s = s->next)
1250 const char *name;
1251 bfd_boolean strip;
1253 if ((s->flags & SEC_LINKER_CREATED) == 0)
1254 continue;
1256 /* It's OK to base decisions on the section name, because none
1257 of the dynobj section names depend upon the input files. */
1258 name = bfd_get_section_name (dynobj, s);
1260 strip = FALSE;
1262 if (strncmp (name, ".rela", 5) == 0)
1264 if (strcmp (name, ".rela.plt") == 0)
1265 relplt = TRUE;
1266 else if (strcmp (name, ".rela.got") == 0)
1267 relgot = TRUE;
1269 /* We use the reloc_count field as a counter if we need
1270 to copy relocs into the output file. */
1271 s->reloc_count = 0;
1273 else if (strncmp (name, ".plt.", 5) == 0
1274 || strncmp (name, ".got.plt.", 9) == 0)
1276 if (s->_raw_size == 0)
1278 /* If we don't need this section, strip it from the output
1279 file. We must create the ".plt*" and ".got.plt*"
1280 sections in create_dynamic_sections and/or check_relocs
1281 based on a conservative estimate of the PLT relocation
1282 count, because the sections must be created before the
1283 linker maps input sections to output sections. The
1284 linker does that before size_dynamic_sections, where we
1285 compute the exact size of the PLT, so there may be more
1286 of these sections than are actually needed. */
1287 strip = TRUE;
1290 else if (strcmp (name, ".got") != 0
1291 && strcmp (name, ".plt") != 0
1292 && strcmp (name, ".got.plt") != 0
1293 && strcmp (name, ".xt.lit.plt") != 0
1294 && strcmp (name, ".got.loc") != 0)
1296 /* It's not one of our sections, so don't allocate space. */
1297 continue;
1300 if (strip)
1301 _bfd_strip_section_from_output (info, s);
1302 else
1304 /* Allocate memory for the section contents. */
1305 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1306 if (s->contents == NULL && s->_raw_size != 0)
1307 return FALSE;
1311 if (elf_hash_table (info)->dynamic_sections_created)
1313 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1314 known until finish_dynamic_sections, but we need to get the relocs
1315 in place before they are sorted. */
1316 if (srelgot == NULL)
1317 abort ();
1318 for (chunk = 0; chunk < plt_chunks; chunk++)
1320 Elf_Internal_Rela irela;
1321 bfd_byte *loc;
1323 irela.r_offset = 0;
1324 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1325 irela.r_addend = 0;
1327 loc = (srelgot->contents
1328 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1329 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1330 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1331 loc + sizeof (Elf32_External_Rela));
1332 srelgot->reloc_count += 2;
1335 /* Add some entries to the .dynamic section. We fill in the
1336 values later, in elf_xtensa_finish_dynamic_sections, but we
1337 must add the entries now so that we get the correct size for
1338 the .dynamic section. The DT_DEBUG entry is filled in by the
1339 dynamic linker and used by the debugger. */
1340 #define add_dynamic_entry(TAG, VAL) \
1341 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1343 if (! info->shared)
1345 if (!add_dynamic_entry (DT_DEBUG, 0))
1346 return FALSE;
1349 if (relplt)
1351 if (!add_dynamic_entry (DT_PLTGOT, 0)
1352 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1353 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1354 || !add_dynamic_entry (DT_JMPREL, 0))
1355 return FALSE;
1358 if (relgot)
1360 if (!add_dynamic_entry (DT_RELA, 0)
1361 || !add_dynamic_entry (DT_RELASZ, 0)
1362 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1363 return FALSE;
1366 if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1367 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1368 return FALSE;
1370 #undef add_dynamic_entry
1372 return TRUE;
1376 /* Remove any PT_LOAD segments with no allocated sections. Prior to
1377 binutils 2.13, this function used to remove the non-SEC_ALLOC
1378 sections from PT_LOAD segments, but that task has now been moved
1379 into elf.c. We still need this function to remove any empty
1380 segments that result, but there's nothing Xtensa-specific about
1381 this and it probably ought to be moved into elf.c as well. */
1383 static bfd_boolean
1384 elf_xtensa_modify_segment_map (abfd, info)
1385 bfd *abfd;
1386 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1388 struct elf_segment_map **m_p;
1390 m_p = &elf_tdata (abfd)->segment_map;
1391 while (*m_p != NULL)
1393 if ((*m_p)->p_type == PT_LOAD && (*m_p)->count == 0)
1394 *m_p = (*m_p)->next;
1395 else
1396 m_p = &(*m_p)->next;
1398 return TRUE;
1402 /* Perform the specified relocation. The instruction at (contents + address)
1403 is modified to set one operand to represent the value in "relocation". The
1404 operand position is determined by the relocation type recorded in the
1405 howto. */
1407 #define CALL_SEGMENT_BITS (30)
1408 #define CALL_SEGMENT_SIZE (1<<CALL_SEGMENT_BITS)
1410 static bfd_reloc_status_type
1411 elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1412 contents, address, is_weak_undef, error_message)
1413 reloc_howto_type *howto;
1414 bfd *abfd;
1415 asection *input_section;
1416 bfd_vma relocation;
1417 bfd_byte *contents;
1418 bfd_vma address;
1419 bfd_boolean is_weak_undef;
1420 char **error_message;
1422 xtensa_opcode opcode;
1423 xtensa_operand operand;
1424 xtensa_encode_result encode_result;
1425 xtensa_isa isa = xtensa_default_isa;
1426 xtensa_insnbuf ibuff;
1427 bfd_vma self_address;
1428 int opnd;
1429 uint32 newval;
1431 switch (howto->type)
1433 case R_XTENSA_NONE:
1434 return bfd_reloc_ok;
1436 case R_XTENSA_ASM_EXPAND:
1437 if (!is_weak_undef)
1439 /* Check for windowed CALL across a 1GB boundary. */
1440 xtensa_opcode opcode =
1441 get_expanded_call_opcode (contents + address,
1442 input_section->_raw_size - address);
1443 if (is_windowed_call_opcode (opcode))
1445 self_address = (input_section->output_section->vma
1446 + input_section->output_offset
1447 + address);
1448 if ((self_address >> CALL_SEGMENT_BITS) !=
1449 (relocation >> CALL_SEGMENT_BITS))
1451 *error_message = "windowed longcall crosses 1GB boundary; "
1452 "return may fail";
1453 return bfd_reloc_dangerous;
1457 return bfd_reloc_ok;
1459 case R_XTENSA_ASM_SIMPLIFY:
1461 /* Convert the L32R/CALLX to CALL. */
1462 bfd_reloc_status_type retval =
1463 elf_xtensa_do_asm_simplify (contents, address,
1464 input_section->_raw_size);
1465 if (retval != bfd_reloc_ok)
1466 return retval;
1468 /* The CALL needs to be relocated. Continue below for that part. */
1469 address += 3;
1470 howto = &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
1472 break;
1474 case R_XTENSA_32:
1475 case R_XTENSA_PLT:
1477 bfd_vma x;
1478 x = bfd_get_32 (abfd, contents + address);
1479 x = x + relocation;
1480 bfd_put_32 (abfd, x, contents + address);
1482 return bfd_reloc_ok;
1485 /* Read the instruction into a buffer and decode the opcode. */
1486 ibuff = xtensa_insnbuf_alloc (isa);
1487 xtensa_insnbuf_from_chars (isa, ibuff, contents + address);
1488 opcode = xtensa_decode_insn (isa, ibuff);
1490 /* Determine which operand is being relocated. */
1491 if (opcode == XTENSA_UNDEFINED)
1493 *error_message = "cannot decode instruction";
1494 return bfd_reloc_dangerous;
1497 if (howto->type < R_XTENSA_OP0 || howto->type > R_XTENSA_OP2)
1499 *error_message = "unexpected relocation";
1500 return bfd_reloc_dangerous;
1503 opnd = howto->type - R_XTENSA_OP0;
1505 /* Calculate the PC address for this instruction. */
1506 if (!howto->pc_relative)
1508 *error_message = "expected PC-relative relocation";
1509 return bfd_reloc_dangerous;
1512 self_address = (input_section->output_section->vma
1513 + input_section->output_offset
1514 + address);
1516 /* Apply the relocation. */
1517 operand = xtensa_get_operand (isa, opcode, opnd);
1518 newval = xtensa_operand_do_reloc (operand, relocation, self_address);
1519 encode_result = xtensa_operand_encode (operand, &newval);
1520 xtensa_operand_set_field (operand, ibuff, newval);
1522 /* Write the modified instruction back out of the buffer. */
1523 xtensa_insnbuf_to_chars (isa, ibuff, contents + address);
1524 free (ibuff);
1526 if (encode_result != xtensa_encode_result_ok)
1528 char *message = build_encoding_error_message (opcode, encode_result);
1529 *error_message = message;
1530 return bfd_reloc_dangerous;
1533 /* Final check for call. */
1534 if (is_direct_call_opcode (opcode)
1535 && is_windowed_call_opcode (opcode))
1537 if ((self_address >> CALL_SEGMENT_BITS) !=
1538 (relocation >> CALL_SEGMENT_BITS))
1540 *error_message = "windowed call crosses 1GB boundary; "
1541 "return may fail";
1542 return bfd_reloc_dangerous;
1546 return bfd_reloc_ok;
1550 static char *
1551 vsprint_msg VPARAMS ((const char *origmsg, const char *fmt, int arglen, ...))
1553 /* To reduce the size of the memory leak,
1554 we only use a single message buffer. */
1555 static bfd_size_type alloc_size = 0;
1556 static char *message = NULL;
1557 bfd_size_type orig_len, len = 0;
1558 bfd_boolean is_append;
1560 VA_OPEN (ap, arglen);
1561 VA_FIXEDARG (ap, const char *, origmsg);
1563 is_append = (origmsg == message);
1565 orig_len = strlen (origmsg);
1566 len = orig_len + strlen (fmt) + arglen + 20;
1567 if (len > alloc_size)
1569 message = (char *) bfd_realloc (message, len);
1570 alloc_size = len;
1572 if (!is_append)
1573 memcpy (message, origmsg, orig_len);
1574 vsprintf (message + orig_len, fmt, ap);
1575 VA_CLOSE (ap);
1576 return message;
1580 static char *
1581 build_encoding_error_message (opcode, encode_result)
1582 xtensa_opcode opcode;
1583 xtensa_encode_result encode_result;
1585 const char *opname = xtensa_opcode_name (xtensa_default_isa, opcode);
1586 const char *msg = NULL;
1588 switch (encode_result)
1590 case xtensa_encode_result_ok:
1591 msg = "unexpected valid encoding";
1592 break;
1593 case xtensa_encode_result_align:
1594 msg = "misaligned encoding";
1595 break;
1596 case xtensa_encode_result_not_in_table:
1597 msg = "encoding not in lookup table";
1598 break;
1599 case xtensa_encode_result_too_low:
1600 msg = "encoding out of range: too low";
1601 break;
1602 case xtensa_encode_result_too_high:
1603 msg = "encoding out of range: too high";
1604 break;
1605 case xtensa_encode_result_not_ok:
1606 default:
1607 msg = "could not encode";
1608 break;
1611 if (is_direct_call_opcode (opcode)
1612 && (encode_result == xtensa_encode_result_too_low
1613 || encode_result == xtensa_encode_result_too_high))
1615 msg = "direct call out of range";
1617 else if (opcode == get_l32r_opcode ())
1619 /* L32Rs have the strange interaction with encoding in that they
1620 have an unsigned immediate field, so libisa returns "too high"
1621 when the absolute value is out of range and never returns "too
1622 low", but I leave the "too low" message in case anything
1623 changes. */
1624 if (encode_result == xtensa_encode_result_too_low)
1625 msg = "literal out of range";
1626 else if (encode_result == xtensa_encode_result_too_high)
1627 msg = "literal placed after use";
1630 return vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
1634 /* This function is registered as the "special_function" in the
1635 Xtensa howto for handling simplify operations.
1636 bfd_perform_relocation / bfd_install_relocation use it to
1637 perform (install) the specified relocation. Since this replaces the code
1638 in bfd_perform_relocation, it is basically an Xtensa-specific,
1639 stripped-down version of bfd_perform_relocation. */
1641 static bfd_reloc_status_type
1642 bfd_elf_xtensa_reloc (abfd, reloc_entry, symbol, data, input_section,
1643 output_bfd, error_message)
1644 bfd *abfd;
1645 arelent *reloc_entry;
1646 asymbol *symbol;
1647 PTR data;
1648 asection *input_section;
1649 bfd *output_bfd;
1650 char **error_message;
1652 bfd_vma relocation;
1653 bfd_reloc_status_type flag;
1654 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1655 bfd_vma output_base = 0;
1656 reloc_howto_type *howto = reloc_entry->howto;
1657 asection *reloc_target_output_section;
1658 bfd_boolean is_weak_undef;
1660 /* ELF relocs are against symbols. If we are producing relocatable
1661 output, and the reloc is against an external symbol, the resulting
1662 reloc will also be against the same symbol. In such a case, we
1663 don't want to change anything about the way the reloc is handled,
1664 since it will all be done at final link time. This test is similar
1665 to what bfd_elf_generic_reloc does except that it lets relocs with
1666 howto->partial_inplace go through even if the addend is non-zero.
1667 (The real problem is that partial_inplace is set for XTENSA_32
1668 relocs to begin with, but that's a long story and there's little we
1669 can do about it now....) */
1671 if (output_bfd != (bfd *) NULL
1672 && (symbol->flags & BSF_SECTION_SYM) == 0)
1674 reloc_entry->address += input_section->output_offset;
1675 return bfd_reloc_ok;
1678 /* Is the address of the relocation really within the section? */
1679 if (reloc_entry->address > (input_section->_cooked_size
1680 / bfd_octets_per_byte (abfd)))
1681 return bfd_reloc_outofrange;
1683 /* Work out which section the relocation is targeted at and the
1684 initial relocation command value. */
1686 /* Get symbol value. (Common symbols are special.) */
1687 if (bfd_is_com_section (symbol->section))
1688 relocation = 0;
1689 else
1690 relocation = symbol->value;
1692 reloc_target_output_section = symbol->section->output_section;
1694 /* Convert input-section-relative symbol value to absolute. */
1695 if ((output_bfd && !howto->partial_inplace)
1696 || reloc_target_output_section == NULL)
1697 output_base = 0;
1698 else
1699 output_base = reloc_target_output_section->vma;
1701 relocation += output_base + symbol->section->output_offset;
1703 /* Add in supplied addend. */
1704 relocation += reloc_entry->addend;
1706 /* Here the variable relocation holds the final address of the
1707 symbol we are relocating against, plus any addend. */
1708 if (output_bfd)
1710 if (!howto->partial_inplace)
1712 /* This is a partial relocation, and we want to apply the relocation
1713 to the reloc entry rather than the raw data. Everything except
1714 relocations against section symbols has already been handled
1715 above. */
1717 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
1718 reloc_entry->addend = relocation;
1719 reloc_entry->address += input_section->output_offset;
1720 return bfd_reloc_ok;
1722 else
1724 reloc_entry->address += input_section->output_offset;
1725 reloc_entry->addend = 0;
1729 is_weak_undef = (bfd_is_und_section (symbol->section)
1730 && (symbol->flags & BSF_WEAK) != 0);
1731 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1732 (bfd_byte *) data, (bfd_vma) octets,
1733 is_weak_undef, error_message);
1735 if (flag == bfd_reloc_dangerous)
1737 /* Add the symbol name to the error message. */
1738 if (! *error_message)
1739 *error_message = "";
1740 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
1741 strlen (symbol->name) + 17,
1742 symbol->name, reloc_entry->addend);
1745 return flag;
1749 /* Set up an entry in the procedure linkage table. */
1751 static bfd_vma
1752 elf_xtensa_create_plt_entry (dynobj, output_bfd, reloc_index)
1753 bfd *dynobj;
1754 bfd *output_bfd;
1755 unsigned reloc_index;
1757 asection *splt, *sgotplt;
1758 bfd_vma plt_base, got_base;
1759 bfd_vma code_offset, lit_offset;
1760 int chunk;
1762 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
1763 splt = elf_xtensa_get_plt_section (dynobj, chunk);
1764 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1765 BFD_ASSERT (splt != NULL && sgotplt != NULL);
1767 plt_base = splt->output_section->vma + splt->output_offset;
1768 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1770 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
1771 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
1773 /* Fill in the literal entry. This is the offset of the dynamic
1774 relocation entry. */
1775 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
1776 sgotplt->contents + lit_offset);
1778 /* Fill in the entry in the procedure linkage table. */
1779 memcpy (splt->contents + code_offset,
1780 (bfd_big_endian (output_bfd)
1781 ? elf_xtensa_be_plt_entry
1782 : elf_xtensa_le_plt_entry),
1783 PLT_ENTRY_SIZE);
1784 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
1785 plt_base + code_offset + 3),
1786 splt->contents + code_offset + 4);
1787 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
1788 plt_base + code_offset + 6),
1789 splt->contents + code_offset + 7);
1790 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
1791 plt_base + code_offset + 9),
1792 splt->contents + code_offset + 10);
1794 return plt_base + code_offset;
1798 /* Relocate an Xtensa ELF section. This is invoked by the linker for
1799 both relocatable and final links. */
1801 static bfd_boolean
1802 elf_xtensa_relocate_section (output_bfd, info, input_bfd,
1803 input_section, contents, relocs,
1804 local_syms, local_sections)
1805 bfd *output_bfd;
1806 struct bfd_link_info *info;
1807 bfd *input_bfd;
1808 asection *input_section;
1809 bfd_byte *contents;
1810 Elf_Internal_Rela *relocs;
1811 Elf_Internal_Sym *local_syms;
1812 asection **local_sections;
1814 Elf_Internal_Shdr *symtab_hdr;
1815 Elf_Internal_Rela *rel;
1816 Elf_Internal_Rela *relend;
1817 struct elf_link_hash_entry **sym_hashes;
1818 asection *srelgot, *srelplt;
1819 bfd *dynobj;
1820 property_table_entry *lit_table = 0;
1821 int ltblsize = 0;
1822 char *error_message = NULL;
1824 if (xtensa_default_isa == NULL)
1825 xtensa_isa_init ();
1827 dynobj = elf_hash_table (info)->dynobj;
1828 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1829 sym_hashes = elf_sym_hashes (input_bfd);
1831 srelgot = NULL;
1832 srelplt = NULL;
1833 if (dynobj != NULL)
1835 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
1836 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1839 if (elf_hash_table (info)->dynamic_sections_created)
1841 ltblsize = xtensa_read_table_entries (input_bfd, input_section,
1842 &lit_table, XTENSA_LIT_SEC_NAME);
1843 if (ltblsize < 0)
1844 return FALSE;
1847 rel = relocs;
1848 relend = relocs + input_section->reloc_count;
1849 for (; rel < relend; rel++)
1851 int r_type;
1852 reloc_howto_type *howto;
1853 unsigned long r_symndx;
1854 struct elf_link_hash_entry *h;
1855 Elf_Internal_Sym *sym;
1856 asection *sec;
1857 bfd_vma relocation;
1858 bfd_reloc_status_type r;
1859 bfd_boolean is_weak_undef;
1860 bfd_boolean unresolved_reloc;
1861 bfd_boolean warned;
1863 r_type = ELF32_R_TYPE (rel->r_info);
1864 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
1865 || r_type == (int) R_XTENSA_GNU_VTENTRY)
1866 continue;
1868 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
1870 bfd_set_error (bfd_error_bad_value);
1871 return FALSE;
1873 howto = &elf_howto_table[r_type];
1875 r_symndx = ELF32_R_SYM (rel->r_info);
1877 if (info->relocatable)
1879 /* This is a relocatable link.
1880 1) If the reloc is against a section symbol, adjust
1881 according to the output section.
1882 2) If there is a new target for this relocation,
1883 the new target will be in the same output section.
1884 We adjust the relocation by the output section
1885 difference. */
1887 if (relaxing_section)
1889 /* Check if this references a section in another input file. */
1890 do_fix_for_relocatable_link (rel, input_bfd, input_section);
1891 r_type = ELF32_R_TYPE (rel->r_info);
1894 if (r_type == R_XTENSA_ASM_SIMPLIFY)
1896 /* Convert ASM_SIMPLIFY into the simpler relocation
1897 so that they never escape a relaxing link. */
1898 contract_asm_expansion (contents, input_section->_raw_size, rel);
1899 r_type = ELF32_R_TYPE (rel->r_info);
1902 /* This is a relocatable link, so we don't have to change
1903 anything unless the reloc is against a section symbol,
1904 in which case we have to adjust according to where the
1905 section symbol winds up in the output section. */
1906 if (r_symndx < symtab_hdr->sh_info)
1908 sym = local_syms + r_symndx;
1909 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1911 sec = local_sections[r_symndx];
1912 rel->r_addend += sec->output_offset + sym->st_value;
1916 /* If there is an addend with a partial_inplace howto,
1917 then move the addend to the contents. This is a hack
1918 to work around problems with DWARF in relocatable links
1919 with some previous version of BFD. Now we can't easily get
1920 rid of the hack without breaking backward compatibility.... */
1921 if (rel->r_addend)
1923 howto = &elf_howto_table[r_type];
1924 if (howto->partial_inplace)
1926 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
1927 rel->r_addend, contents,
1928 rel->r_offset, FALSE,
1929 &error_message);
1930 if (r != bfd_reloc_ok)
1932 if (!((*info->callbacks->reloc_dangerous)
1933 (info, error_message, input_bfd, input_section,
1934 rel->r_offset)))
1935 return FALSE;
1937 rel->r_addend = 0;
1941 /* Done with work for relocatable link; continue with next reloc. */
1942 continue;
1945 /* This is a final link. */
1947 h = NULL;
1948 sym = NULL;
1949 sec = NULL;
1950 is_weak_undef = FALSE;
1951 unresolved_reloc = FALSE;
1952 warned = FALSE;
1954 if (howto->partial_inplace)
1956 /* Because R_XTENSA_32 was made partial_inplace to fix some
1957 problems with DWARF info in partial links, there may be
1958 an addend stored in the contents. Take it out of there
1959 and move it back into the addend field of the reloc. */
1960 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
1961 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
1964 if (r_symndx < symtab_hdr->sh_info)
1966 sym = local_syms + r_symndx;
1967 sec = local_sections[r_symndx];
1968 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1970 else
1972 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1973 r_symndx, symtab_hdr, sym_hashes,
1974 h, sec, relocation,
1975 unresolved_reloc, warned);
1977 if (relocation == 0
1978 && !unresolved_reloc
1979 && h->root.type == bfd_link_hash_undefweak)
1980 is_weak_undef = TRUE;
1983 if (relaxing_section)
1985 /* Check if this references a section in another input file. */
1986 do_fix_for_final_link (rel, input_section, &relocation);
1988 /* Update some already cached values. */
1989 r_type = ELF32_R_TYPE (rel->r_info);
1990 howto = &elf_howto_table[r_type];
1993 /* Sanity check the address. */
1994 if (rel->r_offset >= input_section->_raw_size
1995 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
1997 bfd_set_error (bfd_error_bad_value);
1998 return FALSE;
2001 /* Generate dynamic relocations. */
2002 if (elf_hash_table (info)->dynamic_sections_created)
2004 bfd_boolean dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
2006 if (dynamic_symbol && (r_type == R_XTENSA_OP0
2007 || r_type == R_XTENSA_OP1
2008 || r_type == R_XTENSA_OP2))
2010 /* This is an error. The symbol's real value won't be known
2011 until runtime and it's likely to be out of range anyway. */
2012 const char *name = h->root.root.string;
2013 error_message = vsprint_msg ("invalid relocation for dynamic "
2014 "symbol", ": %s",
2015 strlen (name) + 2, name);
2016 if (!((*info->callbacks->reloc_dangerous)
2017 (info, error_message, input_bfd, input_section,
2018 rel->r_offset)))
2019 return FALSE;
2021 else if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
2022 && (input_section->flags & SEC_ALLOC) != 0
2023 && (dynamic_symbol || info->shared))
2025 Elf_Internal_Rela outrel;
2026 bfd_byte *loc;
2027 asection *srel;
2029 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2030 srel = srelplt;
2031 else
2032 srel = srelgot;
2034 BFD_ASSERT (srel != NULL);
2036 outrel.r_offset =
2037 _bfd_elf_section_offset (output_bfd, info,
2038 input_section, rel->r_offset);
2040 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2041 memset (&outrel, 0, sizeof outrel);
2042 else
2044 outrel.r_offset += (input_section->output_section->vma
2045 + input_section->output_offset);
2047 /* Complain if the relocation is in a read-only section
2048 and not in a literal pool. */
2049 if ((input_section->flags & SEC_READONLY) != 0
2050 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
2051 outrel.r_offset))
2053 error_message =
2054 _("dynamic relocation in read-only section");
2055 if (!((*info->callbacks->reloc_dangerous)
2056 (info, error_message, input_bfd, input_section,
2057 rel->r_offset)))
2058 return FALSE;
2061 if (dynamic_symbol)
2063 outrel.r_addend = rel->r_addend;
2064 rel->r_addend = 0;
2066 if (r_type == R_XTENSA_32)
2068 outrel.r_info =
2069 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2070 relocation = 0;
2072 else /* r_type == R_XTENSA_PLT */
2074 outrel.r_info =
2075 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2077 /* Create the PLT entry and set the initial
2078 contents of the literal entry to the address of
2079 the PLT entry. */
2080 relocation =
2081 elf_xtensa_create_plt_entry (dynobj, output_bfd,
2082 srel->reloc_count);
2084 unresolved_reloc = FALSE;
2086 else
2088 /* Generate a RELATIVE relocation. */
2089 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2090 outrel.r_addend = 0;
2094 loc = (srel->contents
2095 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2096 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2097 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2098 <= srel->_cooked_size);
2102 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2103 because such sections are not SEC_ALLOC and thus ld.so will
2104 not process them. */
2105 if (unresolved_reloc
2106 && !((input_section->flags & SEC_DEBUGGING) != 0
2107 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2108 (*_bfd_error_handler)
2109 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2110 bfd_archive_filename (input_bfd),
2111 bfd_get_section_name (input_bfd, input_section),
2112 (long) rel->r_offset,
2113 h->root.root.string);
2115 /* There's no point in calling bfd_perform_relocation here.
2116 Just go directly to our "special function". */
2117 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2118 relocation + rel->r_addend,
2119 contents, rel->r_offset, is_weak_undef,
2120 &error_message);
2122 if (r != bfd_reloc_ok && !warned)
2124 const char *name;
2126 BFD_ASSERT (r == bfd_reloc_dangerous);
2127 BFD_ASSERT (error_message != (char *) NULL);
2129 if (h != NULL)
2130 name = h->root.root.string;
2131 else
2133 name = bfd_elf_string_from_elf_section
2134 (input_bfd, symtab_hdr->sh_link, sym->st_name);
2135 if (name && *name == '\0')
2136 name = bfd_section_name (input_bfd, sec);
2138 if (name)
2139 error_message = vsprint_msg (error_message, ": %s",
2140 strlen (name), name);
2141 if (!((*info->callbacks->reloc_dangerous)
2142 (info, error_message, input_bfd, input_section,
2143 rel->r_offset)))
2144 return FALSE;
2148 if (lit_table)
2149 free (lit_table);
2151 input_section->reloc_done = TRUE;
2153 return TRUE;
2157 /* Finish up dynamic symbol handling. There's not much to do here since
2158 the PLT and GOT entries are all set up by relocate_section. */
2160 static bfd_boolean
2161 elf_xtensa_finish_dynamic_symbol (output_bfd, info, h, sym)
2162 bfd *output_bfd ATTRIBUTE_UNUSED;
2163 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2164 struct elf_link_hash_entry *h;
2165 Elf_Internal_Sym *sym;
2167 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2168 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2170 /* Mark the symbol as undefined, rather than as defined in
2171 the .plt section. Leave the value alone. */
2172 sym->st_shndx = SHN_UNDEF;
2175 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2176 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2177 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2178 sym->st_shndx = SHN_ABS;
2180 return TRUE;
2184 /* Combine adjacent literal table entries in the output. Adjacent
2185 entries within each input section may have been removed during
2186 relaxation, but we repeat the process here, even though it's too late
2187 to shrink the output section, because it's important to minimize the
2188 number of literal table entries to reduce the start-up work for the
2189 runtime linker. Returns the number of remaining table entries or -1
2190 on error. */
2192 static int
2193 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc)
2194 bfd *output_bfd;
2195 asection *sxtlit;
2196 asection *sgotloc;
2198 bfd_byte *contents;
2199 property_table_entry *table;
2200 bfd_size_type section_size, sgotloc_size;
2201 bfd_vma offset;
2202 int n, m, num;
2204 section_size = (sxtlit->_cooked_size != 0
2205 ? sxtlit->_cooked_size : sxtlit->_raw_size);
2206 BFD_ASSERT (section_size % 8 == 0);
2207 num = section_size / 8;
2209 sgotloc_size = (sgotloc->_cooked_size != 0
2210 ? sgotloc->_cooked_size : sgotloc->_raw_size);
2211 if (sgotloc_size != section_size)
2213 (*_bfd_error_handler)
2214 ("internal inconsistency in size of .got.loc section");
2215 return -1;
2218 contents = (bfd_byte *) bfd_malloc (section_size);
2219 table = (property_table_entry *)
2220 bfd_malloc (num * sizeof (property_table_entry));
2221 if (contents == 0 || table == 0)
2222 return -1;
2224 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
2225 propagates to the output section, where it doesn't really apply and
2226 where it breaks the following call to bfd_get_section_contents. */
2227 sxtlit->flags &= ~SEC_IN_MEMORY;
2229 if (! bfd_get_section_contents (output_bfd, sxtlit, contents, 0,
2230 section_size))
2231 return -1;
2233 /* There should never be any relocations left at this point, so this
2234 is quite a bit easier than what is done during relaxation. */
2236 /* Copy the raw contents into a property table array and sort it. */
2237 offset = 0;
2238 for (n = 0; n < num; n++)
2240 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
2241 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
2242 offset += 8;
2244 qsort (table, num, sizeof (property_table_entry), property_table_compare);
2246 for (n = 0; n < num; n++)
2248 bfd_boolean remove = FALSE;
2250 if (table[n].size == 0)
2251 remove = TRUE;
2252 else if (n > 0 &&
2253 (table[n-1].address + table[n-1].size == table[n].address))
2255 table[n-1].size += table[n].size;
2256 remove = TRUE;
2259 if (remove)
2261 for (m = n; m < num - 1; m++)
2263 table[m].address = table[m+1].address;
2264 table[m].size = table[m+1].size;
2267 n--;
2268 num--;
2272 /* Copy the data back to the raw contents. */
2273 offset = 0;
2274 for (n = 0; n < num; n++)
2276 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
2277 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
2278 offset += 8;
2281 /* Clear the removed bytes. */
2282 if ((bfd_size_type) (num * 8) < section_size)
2284 memset (&contents[num * 8], 0, section_size - num * 8);
2285 sxtlit->_cooked_size = num * 8;
2288 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
2289 section_size))
2290 return -1;
2292 /* Copy the contents to ".got.loc". */
2293 memcpy (sgotloc->contents, contents, section_size);
2295 free (contents);
2296 free (table);
2297 return num;
2301 /* Finish up the dynamic sections. */
2303 static bfd_boolean
2304 elf_xtensa_finish_dynamic_sections (output_bfd, info)
2305 bfd *output_bfd;
2306 struct bfd_link_info *info;
2308 bfd *dynobj;
2309 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
2310 Elf32_External_Dyn *dyncon, *dynconend;
2311 int num_xtlit_entries;
2313 if (! elf_hash_table (info)->dynamic_sections_created)
2314 return TRUE;
2316 dynobj = elf_hash_table (info)->dynobj;
2317 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2318 BFD_ASSERT (sdyn != NULL);
2320 /* Set the first entry in the global offset table to the address of
2321 the dynamic section. */
2322 sgot = bfd_get_section_by_name (dynobj, ".got");
2323 if (sgot)
2325 BFD_ASSERT (sgot->_raw_size == 4);
2326 if (sdyn == NULL)
2327 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2328 else
2329 bfd_put_32 (output_bfd,
2330 sdyn->output_section->vma + sdyn->output_offset,
2331 sgot->contents);
2334 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2335 if (srelplt != NULL && srelplt->_raw_size != 0)
2337 asection *sgotplt, *srelgot, *spltlittbl;
2338 int chunk, plt_chunks, plt_entries;
2339 Elf_Internal_Rela irela;
2340 bfd_byte *loc;
2341 unsigned rtld_reloc;
2343 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
2344 BFD_ASSERT (srelgot != NULL);
2346 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
2347 BFD_ASSERT (spltlittbl != NULL);
2349 /* Find the first XTENSA_RTLD relocation. Presumably the rest
2350 of them follow immediately after.... */
2351 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
2353 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2354 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2355 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
2356 break;
2358 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
2360 plt_entries = (srelplt->_raw_size / sizeof (Elf32_External_Rela));
2361 plt_chunks =
2362 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
2364 for (chunk = 0; chunk < plt_chunks; chunk++)
2366 int chunk_entries = 0;
2368 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
2369 BFD_ASSERT (sgotplt != NULL);
2371 /* Emit special RTLD relocations for the first two entries in
2372 each chunk of the .got.plt section. */
2374 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2375 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2376 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2377 irela.r_offset = (sgotplt->output_section->vma
2378 + sgotplt->output_offset);
2379 irela.r_addend = 1; /* tell rtld to set value to resolver function */
2380 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2381 rtld_reloc += 1;
2382 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2384 /* Next literal immediately follows the first. */
2385 loc += sizeof (Elf32_External_Rela);
2386 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2387 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2388 irela.r_offset = (sgotplt->output_section->vma
2389 + sgotplt->output_offset + 4);
2390 /* Tell rtld to set value to object's link map. */
2391 irela.r_addend = 2;
2392 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2393 rtld_reloc += 1;
2394 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2396 /* Fill in the literal table. */
2397 if (chunk < plt_chunks - 1)
2398 chunk_entries = PLT_ENTRIES_PER_CHUNK;
2399 else
2400 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
2402 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->_cooked_size);
2403 bfd_put_32 (output_bfd,
2404 sgotplt->output_section->vma + sgotplt->output_offset,
2405 spltlittbl->contents + (chunk * 8) + 0);
2406 bfd_put_32 (output_bfd,
2407 8 + (chunk_entries * 4),
2408 spltlittbl->contents + (chunk * 8) + 4);
2411 /* All the dynamic relocations have been emitted at this point.
2412 Make sure the relocation sections are the correct size. */
2413 if (srelgot->_cooked_size != (sizeof (Elf32_External_Rela)
2414 * srelgot->reloc_count)
2415 || srelplt->_cooked_size != (sizeof (Elf32_External_Rela)
2416 * srelplt->reloc_count))
2417 abort ();
2419 /* The .xt.lit.plt section has just been modified. This must
2420 happen before the code below which combines adjacent literal
2421 table entries, and the .xt.lit.plt contents have to be forced to
2422 the output here. */
2423 if (! bfd_set_section_contents (output_bfd,
2424 spltlittbl->output_section,
2425 spltlittbl->contents,
2426 spltlittbl->output_offset,
2427 spltlittbl->_raw_size))
2428 return FALSE;
2429 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
2430 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
2433 /* Combine adjacent literal table entries. */
2434 BFD_ASSERT (! info->relocatable);
2435 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
2436 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
2437 BFD_ASSERT (sxtlit && sgotloc);
2438 num_xtlit_entries =
2439 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
2440 if (num_xtlit_entries < 0)
2441 return FALSE;
2443 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2444 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2445 for (; dyncon < dynconend; dyncon++)
2447 Elf_Internal_Dyn dyn;
2448 const char *name;
2449 asection *s;
2451 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2453 switch (dyn.d_tag)
2455 default:
2456 break;
2458 case DT_XTENSA_GOT_LOC_SZ:
2459 dyn.d_un.d_val = num_xtlit_entries;
2460 break;
2462 case DT_XTENSA_GOT_LOC_OFF:
2463 name = ".got.loc";
2464 goto get_vma;
2465 case DT_PLTGOT:
2466 name = ".got";
2467 goto get_vma;
2468 case DT_JMPREL:
2469 name = ".rela.plt";
2470 get_vma:
2471 s = bfd_get_section_by_name (output_bfd, name);
2472 BFD_ASSERT (s);
2473 dyn.d_un.d_ptr = s->vma;
2474 break;
2476 case DT_PLTRELSZ:
2477 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2478 BFD_ASSERT (s);
2479 dyn.d_un.d_val = (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2480 break;
2482 case DT_RELASZ:
2483 /* Adjust RELASZ to not include JMPREL. This matches what
2484 glibc expects and what is done for several other ELF
2485 targets (e.g., i386, alpha), but the "correct" behavior
2486 seems to be unresolved. Since the linker script arranges
2487 for .rela.plt to follow all other relocation sections, we
2488 don't have to worry about changing the DT_RELA entry. */
2489 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2490 if (s)
2492 dyn.d_un.d_val -=
2493 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2495 break;
2498 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2501 return TRUE;
2505 /* Functions for dealing with the e_flags field. */
2507 /* Merge backend specific data from an object file to the output
2508 object file when linking. */
2510 static bfd_boolean
2511 elf_xtensa_merge_private_bfd_data (ibfd, obfd)
2512 bfd *ibfd;
2513 bfd *obfd;
2515 unsigned out_mach, in_mach;
2516 flagword out_flag, in_flag;
2518 /* Check if we have the same endianess. */
2519 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
2520 return FALSE;
2522 /* Don't even pretend to support mixed-format linking. */
2523 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2524 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2525 return FALSE;
2527 out_flag = elf_elfheader (obfd)->e_flags;
2528 in_flag = elf_elfheader (ibfd)->e_flags;
2530 out_mach = out_flag & EF_XTENSA_MACH;
2531 in_mach = in_flag & EF_XTENSA_MACH;
2532 if (out_mach != in_mach)
2534 (*_bfd_error_handler)
2535 ("%s: incompatible machine type. Output is 0x%x. Input is 0x%x",
2536 bfd_archive_filename (ibfd), out_mach, in_mach);
2537 bfd_set_error (bfd_error_wrong_format);
2538 return FALSE;
2541 if (! elf_flags_init (obfd))
2543 elf_flags_init (obfd) = TRUE;
2544 elf_elfheader (obfd)->e_flags = in_flag;
2546 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2547 && bfd_get_arch_info (obfd)->the_default)
2548 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2549 bfd_get_mach (ibfd));
2551 return TRUE;
2554 if ((out_flag & EF_XTENSA_XT_INSN) !=
2555 (in_flag & EF_XTENSA_XT_INSN))
2556 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
2558 if ((out_flag & EF_XTENSA_XT_LIT) !=
2559 (in_flag & EF_XTENSA_XT_LIT))
2560 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
2562 return TRUE;
2566 static bfd_boolean
2567 elf_xtensa_set_private_flags (abfd, flags)
2568 bfd *abfd;
2569 flagword flags;
2571 BFD_ASSERT (!elf_flags_init (abfd)
2572 || elf_elfheader (abfd)->e_flags == flags);
2574 elf_elfheader (abfd)->e_flags |= flags;
2575 elf_flags_init (abfd) = TRUE;
2577 return TRUE;
2581 extern flagword
2582 elf_xtensa_get_private_bfd_flags (abfd)
2583 bfd *abfd;
2585 return elf_elfheader (abfd)->e_flags;
2589 static bfd_boolean
2590 elf_xtensa_print_private_bfd_data (abfd, farg)
2591 bfd *abfd;
2592 PTR farg;
2594 FILE *f = (FILE *) farg;
2595 flagword e_flags = elf_elfheader (abfd)->e_flags;
2597 fprintf (f, "\nXtensa header:\n");
2598 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
2599 fprintf (f, "\nMachine = Base\n");
2600 else
2601 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
2603 fprintf (f, "Insn tables = %s\n",
2604 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
2606 fprintf (f, "Literal tables = %s\n",
2607 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
2609 return _bfd_elf_print_private_bfd_data (abfd, farg);
2613 /* Set the right machine number for an Xtensa ELF file. */
2615 static bfd_boolean
2616 elf_xtensa_object_p (abfd)
2617 bfd *abfd;
2619 int mach;
2620 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
2622 switch (arch)
2624 case E_XTENSA_MACH:
2625 mach = bfd_mach_xtensa;
2626 break;
2627 default:
2628 return FALSE;
2631 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
2632 return TRUE;
2636 /* The final processing done just before writing out an Xtensa ELF object
2637 file. This gets the Xtensa architecture right based on the machine
2638 number. */
2640 static void
2641 elf_xtensa_final_write_processing (abfd, linker)
2642 bfd *abfd;
2643 bfd_boolean linker ATTRIBUTE_UNUSED;
2645 int mach;
2646 unsigned long val;
2648 switch (mach = bfd_get_mach (abfd))
2650 case bfd_mach_xtensa:
2651 val = E_XTENSA_MACH;
2652 break;
2653 default:
2654 return;
2657 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
2658 elf_elfheader (abfd)->e_flags |= val;
2662 static enum elf_reloc_type_class
2663 elf_xtensa_reloc_type_class (rela)
2664 const Elf_Internal_Rela *rela;
2666 switch ((int) ELF32_R_TYPE (rela->r_info))
2668 case R_XTENSA_RELATIVE:
2669 return reloc_class_relative;
2670 case R_XTENSA_JMP_SLOT:
2671 return reloc_class_plt;
2672 default:
2673 return reloc_class_normal;
2678 static bfd_boolean
2679 elf_xtensa_discard_info_for_section (abfd, cookie, info, sec)
2680 bfd *abfd;
2681 struct elf_reloc_cookie *cookie;
2682 struct bfd_link_info *info;
2683 asection *sec;
2685 bfd_byte *contents;
2686 bfd_vma section_size;
2687 bfd_vma offset, actual_offset;
2688 size_t removed_bytes = 0;
2690 section_size = (sec->_cooked_size ? sec->_cooked_size : sec->_raw_size);
2691 if (section_size == 0 || section_size % 8 != 0)
2692 return FALSE;
2694 if (sec->output_section
2695 && bfd_is_abs_section (sec->output_section))
2696 return FALSE;
2698 contents = retrieve_contents (abfd, sec, info->keep_memory);
2699 if (!contents)
2700 return FALSE;
2702 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
2703 if (!cookie->rels)
2705 release_contents (sec, contents);
2706 return FALSE;
2709 cookie->rel = cookie->rels;
2710 cookie->relend = cookie->rels + sec->reloc_count;
2712 for (offset = 0; offset < section_size; offset += 8)
2714 actual_offset = offset - removed_bytes;
2716 /* The ...symbol_deleted_p function will skip over relocs but it
2717 won't adjust their offsets, so do that here. */
2718 while (cookie->rel < cookie->relend
2719 && cookie->rel->r_offset < offset)
2721 cookie->rel->r_offset -= removed_bytes;
2722 cookie->rel++;
2725 while (cookie->rel < cookie->relend
2726 && cookie->rel->r_offset == offset)
2728 if (bfd_elf_reloc_symbol_deleted_p (offset, cookie))
2730 /* Remove the table entry. (If the reloc type is NONE, then
2731 the entry has already been merged with another and deleted
2732 during relaxation.) */
2733 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
2735 /* Shift the contents up. */
2736 if (offset + 8 < section_size)
2737 memmove (&contents[actual_offset],
2738 &contents[actual_offset+8],
2739 section_size - offset - 8);
2740 removed_bytes += 8;
2743 /* Remove this relocation. */
2744 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
2747 /* Adjust the relocation offset for previous removals. This
2748 should not be done before calling ...symbol_deleted_p
2749 because it might mess up the offset comparisons there.
2750 Make sure the offset doesn't underflow in the case where
2751 the first entry is removed. */
2752 if (cookie->rel->r_offset >= removed_bytes)
2753 cookie->rel->r_offset -= removed_bytes;
2754 else
2755 cookie->rel->r_offset = 0;
2757 cookie->rel++;
2761 if (removed_bytes != 0)
2763 /* Adjust any remaining relocs (shouldn't be any). */
2764 for (; cookie->rel < cookie->relend; cookie->rel++)
2766 if (cookie->rel->r_offset >= removed_bytes)
2767 cookie->rel->r_offset -= removed_bytes;
2768 else
2769 cookie->rel->r_offset = 0;
2772 /* Clear the removed bytes. */
2773 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
2775 pin_contents (sec, contents);
2776 pin_internal_relocs (sec, cookie->rels);
2778 sec->_cooked_size = section_size - removed_bytes;
2779 /* Also shrink _raw_size. See comments in relax_property_section. */
2780 sec->_raw_size = sec->_cooked_size;
2782 if (xtensa_is_littable_section (sec))
2784 bfd *dynobj = elf_hash_table (info)->dynobj;
2785 if (dynobj)
2787 asection *sgotloc =
2788 bfd_get_section_by_name (dynobj, ".got.loc");
2789 if (sgotloc)
2791 bfd_size_type sgotloc_size =
2792 (sgotloc->_cooked_size ? sgotloc->_cooked_size
2793 : sgotloc->_raw_size);
2794 sgotloc->_cooked_size = sgotloc_size - removed_bytes;
2795 sgotloc->_raw_size = sgotloc_size - removed_bytes;
2800 else
2802 release_contents (sec, contents);
2803 release_internal_relocs (sec, cookie->rels);
2806 return (removed_bytes != 0);
2810 static bfd_boolean
2811 elf_xtensa_discard_info (abfd, cookie, info)
2812 bfd *abfd;
2813 struct elf_reloc_cookie *cookie;
2814 struct bfd_link_info *info;
2816 asection *sec;
2817 bfd_boolean changed = FALSE;
2819 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2821 if (xtensa_is_property_section (sec))
2823 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
2824 changed = TRUE;
2828 return changed;
2832 static bfd_boolean
2833 elf_xtensa_ignore_discarded_relocs (sec)
2834 asection *sec;
2836 return xtensa_is_property_section (sec);
2840 /* Support for core dump NOTE sections. */
2842 static bfd_boolean
2843 elf_xtensa_grok_prstatus (abfd, note)
2844 bfd *abfd;
2845 Elf_Internal_Note *note;
2847 int offset;
2848 unsigned int raw_size;
2850 /* The size for Xtensa is variable, so don't try to recognize the format
2851 based on the size. Just assume this is GNU/Linux. */
2853 /* pr_cursig */
2854 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2856 /* pr_pid */
2857 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
2859 /* pr_reg */
2860 offset = 72;
2861 raw_size = note->descsz - offset - 4;
2863 /* Make a ".reg/999" section. */
2864 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2865 raw_size, note->descpos + offset);
2869 static bfd_boolean
2870 elf_xtensa_grok_psinfo (abfd, note)
2871 bfd *abfd;
2872 Elf_Internal_Note *note;
2874 switch (note->descsz)
2876 default:
2877 return FALSE;
2879 case 128: /* GNU/Linux elf_prpsinfo */
2880 elf_tdata (abfd)->core_program
2881 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
2882 elf_tdata (abfd)->core_command
2883 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
2886 /* Note that for some reason, a spurious space is tacked
2887 onto the end of the args in some (at least one anyway)
2888 implementations, so strip it off if it exists. */
2891 char *command = elf_tdata (abfd)->core_command;
2892 int n = strlen (command);
2894 if (0 < n && command[n - 1] == ' ')
2895 command[n - 1] = '\0';
2898 return TRUE;
2902 /* Generic Xtensa configurability stuff. */
2904 static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
2905 static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
2906 static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
2907 static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
2908 static xtensa_opcode call0_op = XTENSA_UNDEFINED;
2909 static xtensa_opcode call4_op = XTENSA_UNDEFINED;
2910 static xtensa_opcode call8_op = XTENSA_UNDEFINED;
2911 static xtensa_opcode call12_op = XTENSA_UNDEFINED;
2913 static void
2914 init_call_opcodes ()
2916 if (callx0_op == XTENSA_UNDEFINED)
2918 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
2919 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
2920 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
2921 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
2922 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
2923 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
2924 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
2925 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
2930 static bfd_boolean
2931 is_indirect_call_opcode (opcode)
2932 xtensa_opcode opcode;
2934 init_call_opcodes ();
2935 return (opcode == callx0_op
2936 || opcode == callx4_op
2937 || opcode == callx8_op
2938 || opcode == callx12_op);
2942 static bfd_boolean
2943 is_direct_call_opcode (opcode)
2944 xtensa_opcode opcode;
2946 init_call_opcodes ();
2947 return (opcode == call0_op
2948 || opcode == call4_op
2949 || opcode == call8_op
2950 || opcode == call12_op);
2954 static bfd_boolean
2955 is_windowed_call_opcode (opcode)
2956 xtensa_opcode opcode;
2958 init_call_opcodes ();
2959 return (opcode == call4_op
2960 || opcode == call8_op
2961 || opcode == call12_op
2962 || opcode == callx4_op
2963 || opcode == callx8_op
2964 || opcode == callx12_op);
2968 static xtensa_opcode
2969 get_l32r_opcode (void)
2971 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
2972 if (l32r_opcode == XTENSA_UNDEFINED)
2974 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
2975 BFD_ASSERT (l32r_opcode != XTENSA_UNDEFINED);
2977 return l32r_opcode;
2981 static bfd_vma
2982 l32r_offset (addr, pc)
2983 bfd_vma addr;
2984 bfd_vma pc;
2986 bfd_vma offset;
2988 offset = addr - ((pc+3) & -4);
2989 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
2990 offset = (signed int) offset >> 2;
2991 BFD_ASSERT ((signed int) offset >> 16 == -1);
2992 return offset;
2996 /* Get the operand number for a PC-relative relocation.
2997 If the relocation is not a PC-relative one, return (-1). */
2999 static int
3000 get_relocation_opnd (irel)
3001 Elf_Internal_Rela *irel;
3003 if (ELF32_R_TYPE (irel->r_info) < R_XTENSA_OP0
3004 || ELF32_R_TYPE (irel->r_info) >= R_XTENSA_max)
3005 return -1;
3006 return ELF32_R_TYPE (irel->r_info) - R_XTENSA_OP0;
3010 /* Get the opcode for a relocation. */
3012 static xtensa_opcode
3013 get_relocation_opcode (sec, contents, irel)
3014 asection *sec;
3015 bfd_byte *contents;
3016 Elf_Internal_Rela *irel;
3018 static xtensa_insnbuf ibuff = NULL;
3019 xtensa_isa isa = xtensa_default_isa;
3021 if (get_relocation_opnd (irel) == -1)
3022 return XTENSA_UNDEFINED;
3024 if (contents == NULL)
3025 return XTENSA_UNDEFINED;
3027 if (sec->_raw_size <= irel->r_offset)
3028 return XTENSA_UNDEFINED;
3030 if (ibuff == NULL)
3031 ibuff = xtensa_insnbuf_alloc (isa);
3033 /* Decode the instruction. */
3034 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset]);
3035 return xtensa_decode_insn (isa, ibuff);
3039 bfd_boolean
3040 is_l32r_relocation (sec, contents, irel)
3041 asection *sec;
3042 bfd_byte *contents;
3043 Elf_Internal_Rela *irel;
3045 xtensa_opcode opcode;
3047 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_OP1)
3048 return FALSE;
3050 opcode = get_relocation_opcode (sec, contents, irel);
3051 return (opcode == get_l32r_opcode ());
3055 /* Code for transforming CALLs at link-time. */
3057 static bfd_reloc_status_type
3058 elf_xtensa_do_asm_simplify (contents, address, content_length)
3059 bfd_byte *contents;
3060 bfd_vma address;
3061 bfd_vma content_length;
3063 static xtensa_insnbuf insnbuf = NULL;
3064 xtensa_opcode opcode;
3065 xtensa_operand operand;
3066 xtensa_opcode direct_call_opcode;
3067 xtensa_isa isa = xtensa_default_isa;
3068 bfd_byte *chbuf = contents + address;
3069 int opn;
3071 if (insnbuf == NULL)
3072 insnbuf = xtensa_insnbuf_alloc (isa);
3074 if (content_length < address)
3076 (*_bfd_error_handler)
3077 ("Attempt to convert L32R/CALLX to CALL failed");
3078 return bfd_reloc_other;
3081 opcode = get_expanded_call_opcode (chbuf, content_length - address);
3082 direct_call_opcode = swap_callx_for_call_opcode (opcode);
3083 if (direct_call_opcode == XTENSA_UNDEFINED)
3085 (*_bfd_error_handler)
3086 ("Attempt to convert L32R/CALLX to CALL failed");
3087 return bfd_reloc_other;
3090 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
3091 opcode = xtensa_opcode_lookup (isa, "or");
3092 xtensa_encode_insn (isa, opcode, insnbuf);
3093 for (opn = 0; opn < 3; opn++)
3095 operand = xtensa_get_operand (isa, opcode, opn);
3096 xtensa_operand_set_field (operand, insnbuf, 1);
3098 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf);
3100 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
3101 xtensa_encode_insn (isa, direct_call_opcode, insnbuf);
3102 operand = xtensa_get_operand (isa, opcode, 0);
3103 xtensa_operand_set_field (operand, insnbuf, 0);
3104 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3);
3106 return bfd_reloc_ok;
3110 static bfd_reloc_status_type
3111 contract_asm_expansion (contents, content_length, irel)
3112 bfd_byte *contents;
3113 bfd_vma content_length;
3114 Elf_Internal_Rela *irel;
3116 bfd_reloc_status_type retval =
3117 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length);
3119 if (retval != bfd_reloc_ok)
3120 return retval;
3122 /* Update the irel->r_offset field so that the right immediate and
3123 the right instruction are modified during the relocation. */
3124 irel->r_offset += 3;
3125 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_OP0);
3126 return bfd_reloc_ok;
3130 static xtensa_opcode
3131 swap_callx_for_call_opcode (opcode)
3132 xtensa_opcode opcode;
3134 init_call_opcodes ();
3136 if (opcode == callx0_op) return call0_op;
3137 if (opcode == callx4_op) return call4_op;
3138 if (opcode == callx8_op) return call8_op;
3139 if (opcode == callx12_op) return call12_op;
3141 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
3142 return XTENSA_UNDEFINED;
3146 /* Check if "buf" is pointing to a "L32R aN; CALLX aN" sequence, and
3147 if so, return the CALLX opcode. If not, return XTENSA_UNDEFINED. */
3149 #define L32R_TARGET_REG_OPERAND 0
3150 #define CALLN_SOURCE_OPERAND 0
3152 static xtensa_opcode
3153 get_expanded_call_opcode (buf, bufsize)
3154 bfd_byte *buf;
3155 int bufsize;
3157 static xtensa_insnbuf insnbuf = NULL;
3158 xtensa_opcode opcode;
3159 xtensa_operand operand;
3160 xtensa_isa isa = xtensa_default_isa;
3161 uint32 regno, call_regno;
3163 /* Buffer must be at least 6 bytes. */
3164 if (bufsize < 6)
3165 return XTENSA_UNDEFINED;
3167 if (insnbuf == NULL)
3168 insnbuf = xtensa_insnbuf_alloc (isa);
3170 xtensa_insnbuf_from_chars (isa, insnbuf, buf);
3171 opcode = xtensa_decode_insn (isa, insnbuf);
3173 if (opcode != get_l32r_opcode ())
3174 return XTENSA_UNDEFINED;
3176 operand = xtensa_get_operand (isa, opcode, L32R_TARGET_REG_OPERAND);
3177 regno = xtensa_operand_decode
3178 (operand, xtensa_operand_get_field (operand, insnbuf));
3180 /* Next instruction should be an CALLXn with operand 0 == regno. */
3181 xtensa_insnbuf_from_chars (isa, insnbuf,
3182 buf + xtensa_insn_length (isa, opcode));
3183 opcode = xtensa_decode_insn (isa, insnbuf);
3185 if (!is_indirect_call_opcode (opcode))
3186 return XTENSA_UNDEFINED;
3188 operand = xtensa_get_operand (isa, opcode, CALLN_SOURCE_OPERAND);
3189 call_regno = xtensa_operand_decode
3190 (operand, xtensa_operand_get_field (operand, insnbuf));
3191 if (call_regno != regno)
3192 return XTENSA_UNDEFINED;
3194 return opcode;
3198 /* Data structures used during relaxation. */
3200 /* r_reloc: relocation values. */
3202 /* Through the relaxation process, we need to keep track of the values
3203 that will result from evaluating relocations. The standard ELF
3204 relocation structure is not sufficient for this purpose because we're
3205 operating on multiple input files at once, so we need to know which
3206 input file a relocation refers to. The r_reloc structure thus
3207 records both the input file (bfd) and ELF relocation.
3209 For efficiency, an r_reloc also contains a "target_offset" field to
3210 cache the target-section-relative offset value that is represented by
3211 the relocation. */
3213 typedef struct r_reloc_struct r_reloc;
3215 struct r_reloc_struct
3217 bfd *abfd;
3218 Elf_Internal_Rela rela;
3219 bfd_vma target_offset;
3222 static bfd_boolean r_reloc_is_const
3223 PARAMS ((const r_reloc *));
3224 static void r_reloc_init
3225 PARAMS ((r_reloc *, bfd *, Elf_Internal_Rela *));
3226 static bfd_vma r_reloc_get_target_offset
3227 PARAMS ((const r_reloc *));
3228 static asection *r_reloc_get_section
3229 PARAMS ((const r_reloc *));
3230 static bfd_boolean r_reloc_is_defined
3231 PARAMS ((const r_reloc *));
3232 static struct elf_link_hash_entry *r_reloc_get_hash_entry
3233 PARAMS ((const r_reloc *));
3236 /* The r_reloc structure is included by value in literal_value, but not
3237 every literal_value has an associated relocation -- some are simple
3238 constants. In such cases, we set all the fields in the r_reloc
3239 struct to zero. The r_reloc_is_const function should be used to
3240 detect this case. */
3242 static bfd_boolean
3243 r_reloc_is_const (r_rel)
3244 const r_reloc *r_rel;
3246 return (r_rel->abfd == NULL);
3250 static void
3251 r_reloc_init (r_rel, abfd, irel)
3252 r_reloc *r_rel;
3253 bfd *abfd;
3254 Elf_Internal_Rela *irel;
3256 if (irel != NULL)
3258 r_rel->rela = *irel;
3259 r_rel->abfd = abfd;
3260 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
3262 else
3263 memset (r_rel, 0, sizeof (r_reloc));
3267 static bfd_vma
3268 r_reloc_get_target_offset (r_rel)
3269 const r_reloc *r_rel;
3271 bfd_vma target_offset;
3272 unsigned long r_symndx;
3274 BFD_ASSERT (!r_reloc_is_const (r_rel));
3275 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3276 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
3277 return (target_offset + r_rel->rela.r_addend);
3281 static struct elf_link_hash_entry *
3282 r_reloc_get_hash_entry (r_rel)
3283 const r_reloc *r_rel;
3285 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3286 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
3290 static asection *
3291 r_reloc_get_section (r_rel)
3292 const r_reloc *r_rel;
3294 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3295 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
3299 static bfd_boolean
3300 r_reloc_is_defined (r_rel)
3301 const r_reloc *r_rel;
3303 asection *sec = r_reloc_get_section (r_rel);
3304 if (sec == bfd_abs_section_ptr
3305 || sec == bfd_com_section_ptr
3306 || sec == bfd_und_section_ptr)
3307 return FALSE;
3308 return TRUE;
3312 /* source_reloc: relocations that reference literal sections. */
3314 /* To determine whether literals can be coalesced, we need to first
3315 record all the relocations that reference the literals. The
3316 source_reloc structure below is used for this purpose. The
3317 source_reloc entries are kept in a per-literal-section array, sorted
3318 by offset within the literal section (i.e., target offset).
3320 The source_sec and r_rel.rela.r_offset fields identify the source of
3321 the relocation. The r_rel field records the relocation value, i.e.,
3322 the offset of the literal being referenced. The opnd field is needed
3323 to determine the range of the immediate field to which the relocation
3324 applies, so we can determine whether another literal with the same
3325 value is within range. The is_null field is true when the relocation
3326 is being removed (e.g., when an L32R is being removed due to a CALLX
3327 that is converted to a direct CALL). */
3329 typedef struct source_reloc_struct source_reloc;
3331 struct source_reloc_struct
3333 asection *source_sec;
3334 r_reloc r_rel;
3335 xtensa_operand opnd;
3336 bfd_boolean is_null;
3340 static void init_source_reloc
3341 PARAMS ((source_reloc *, asection *, const r_reloc *, xtensa_operand));
3342 static source_reloc *find_source_reloc
3343 PARAMS ((source_reloc *, int, asection *, Elf_Internal_Rela *));
3344 static int source_reloc_compare
3345 PARAMS ((const PTR, const PTR));
3348 static void
3349 init_source_reloc (reloc, source_sec, r_rel, opnd)
3350 source_reloc *reloc;
3351 asection *source_sec;
3352 const r_reloc *r_rel;
3353 xtensa_operand opnd;
3355 reloc->source_sec = source_sec;
3356 reloc->r_rel = *r_rel;
3357 reloc->opnd = opnd;
3358 reloc->is_null = FALSE;
3362 /* Find the source_reloc for a particular source offset and relocation
3363 type. Note that the array is sorted by _target_ offset, so this is
3364 just a linear search. */
3366 static source_reloc *
3367 find_source_reloc (src_relocs, src_count, sec, irel)
3368 source_reloc *src_relocs;
3369 int src_count;
3370 asection *sec;
3371 Elf_Internal_Rela *irel;
3373 int i;
3375 for (i = 0; i < src_count; i++)
3377 if (src_relocs[i].source_sec == sec
3378 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
3379 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
3380 == ELF32_R_TYPE (irel->r_info)))
3381 return &src_relocs[i];
3384 return NULL;
3388 static int
3389 source_reloc_compare (ap, bp)
3390 const PTR ap;
3391 const PTR bp;
3393 const source_reloc *a = (const source_reloc *) ap;
3394 const source_reloc *b = (const source_reloc *) bp;
3396 return (a->r_rel.target_offset - b->r_rel.target_offset);
3400 /* Literal values and value hash tables. */
3402 /* Literals with the same value can be coalesced. The literal_value
3403 structure records the value of a literal: the "r_rel" field holds the
3404 information from the relocation on the literal (if there is one) and
3405 the "value" field holds the contents of the literal word itself.
3407 The value_map structure records a literal value along with the
3408 location of a literal holding that value. The value_map hash table
3409 is indexed by the literal value, so that we can quickly check if a
3410 particular literal value has been seen before and is thus a candidate
3411 for coalescing. */
3413 typedef struct literal_value_struct literal_value;
3414 typedef struct value_map_struct value_map;
3415 typedef struct value_map_hash_table_struct value_map_hash_table;
3417 struct literal_value_struct
3419 r_reloc r_rel;
3420 unsigned long value;
3423 struct value_map_struct
3425 literal_value val; /* The literal value. */
3426 r_reloc loc; /* Location of the literal. */
3427 value_map *next;
3430 struct value_map_hash_table_struct
3432 unsigned bucket_count;
3433 value_map **buckets;
3434 unsigned count;
3438 static bfd_boolean is_same_value
3439 PARAMS ((const literal_value *, const literal_value *, bfd_boolean));
3440 static value_map_hash_table *value_map_hash_table_init
3441 PARAMS ((void));
3442 static unsigned hash_literal_value
3443 PARAMS ((const literal_value *));
3444 static unsigned hash_bfd_vma
3445 PARAMS ((bfd_vma));
3446 static value_map *get_cached_value
3447 PARAMS ((value_map_hash_table *, const literal_value *, bfd_boolean));
3448 static value_map *add_value_map
3449 PARAMS ((value_map_hash_table *, const literal_value *, const r_reloc *,
3450 bfd_boolean));
3453 static bfd_boolean
3454 is_same_value (src1, src2, final_static_link)
3455 const literal_value *src1;
3456 const literal_value *src2;
3457 bfd_boolean final_static_link;
3459 struct elf_link_hash_entry *h1, *h2;
3461 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
3462 return FALSE;
3464 if (r_reloc_is_const (&src1->r_rel))
3465 return (src1->value == src2->value);
3467 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
3468 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
3469 return FALSE;
3471 if (r_reloc_get_target_offset (&src1->r_rel)
3472 != r_reloc_get_target_offset (&src2->r_rel))
3473 return FALSE;
3475 if (src1->value != src2->value)
3476 return FALSE;
3478 /* Now check for the same section (if defined) or the same elf_hash
3479 (if undefined or weak). */
3480 h1 = r_reloc_get_hash_entry (&src1->r_rel);
3481 h2 = r_reloc_get_hash_entry (&src2->r_rel);
3482 if (r_reloc_is_defined (&src1->r_rel)
3483 && (final_static_link
3484 || ((!h1 || h1->root.type != bfd_link_hash_defweak)
3485 && (!h2 || h2->root.type != bfd_link_hash_defweak))))
3487 if (r_reloc_get_section (&src1->r_rel)
3488 != r_reloc_get_section (&src2->r_rel))
3489 return FALSE;
3491 else
3493 /* Require that the hash entries (i.e., symbols) be identical. */
3494 if (h1 != h2 || h1 == 0)
3495 return FALSE;
3498 return TRUE;
3502 /* Must be power of 2. */
3503 #define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
3505 static value_map_hash_table *
3506 value_map_hash_table_init ()
3508 value_map_hash_table *values;
3510 values = (value_map_hash_table *)
3511 bfd_malloc (sizeof (value_map_hash_table));
3513 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
3514 values->count = 0;
3515 values->buckets = (value_map **)
3516 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
3518 return values;
3522 static unsigned
3523 hash_bfd_vma (val)
3524 bfd_vma val;
3526 return (val >> 2) + (val >> 10);
3530 static unsigned
3531 hash_literal_value (src)
3532 const literal_value *src;
3534 unsigned hash_val;
3536 if (r_reloc_is_const (&src->r_rel))
3537 return hash_bfd_vma (src->value);
3539 hash_val = (hash_bfd_vma (r_reloc_get_target_offset (&src->r_rel))
3540 + hash_bfd_vma (src->value));
3542 /* Now check for the same section and the same elf_hash. */
3543 if (r_reloc_is_defined (&src->r_rel))
3544 hash_val += hash_bfd_vma ((bfd_vma) (unsigned) r_reloc_get_section (&src->r_rel));
3545 else
3546 hash_val += hash_bfd_vma ((bfd_vma) (unsigned) r_reloc_get_hash_entry (&src->r_rel));
3548 return hash_val;
3552 /* Check if the specified literal_value has been seen before. */
3554 static value_map *
3555 get_cached_value (map, val, final_static_link)
3556 value_map_hash_table *map;
3557 const literal_value *val;
3558 bfd_boolean final_static_link;
3560 value_map *map_e;
3561 value_map *bucket;
3562 unsigned idx;
3564 idx = hash_literal_value (val);
3565 idx = idx & (map->bucket_count - 1);
3566 bucket = map->buckets[idx];
3567 for (map_e = bucket; map_e; map_e = map_e->next)
3569 if (is_same_value (&map_e->val, val, final_static_link))
3570 return map_e;
3572 return NULL;
3576 /* Record a new literal value. It is illegal to call this if VALUE
3577 already has an entry here. */
3579 static value_map *
3580 add_value_map (map, val, loc, final_static_link)
3581 value_map_hash_table *map;
3582 const literal_value *val;
3583 const r_reloc *loc;
3584 bfd_boolean final_static_link;
3586 value_map **bucket_p;
3587 unsigned idx;
3589 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
3591 BFD_ASSERT (get_cached_value (map, val, final_static_link) == NULL);
3592 val_e->val = *val;
3593 val_e->loc = *loc;
3595 idx = hash_literal_value (val);
3596 idx = idx & (map->bucket_count - 1);
3597 bucket_p = &map->buckets[idx];
3599 val_e->next = *bucket_p;
3600 *bucket_p = val_e;
3601 map->count++;
3602 /* FIXME: consider resizing the hash table if we get too many entries */
3604 return val_e;
3608 /* Lists of literals being coalesced or removed. */
3610 /* In the usual case, the literal identified by "from" is being
3611 coalesced with another literal identified by "to". If the literal is
3612 unused and is being removed altogether, "to.abfd" will be NULL.
3613 The removed_literal entries are kept on a per-section list, sorted
3614 by the "from" offset field. */
3616 typedef struct removed_literal_struct removed_literal;
3617 typedef struct removed_literal_list_struct removed_literal_list;
3619 struct removed_literal_struct
3621 r_reloc from;
3622 r_reloc to;
3623 removed_literal *next;
3626 struct removed_literal_list_struct
3628 removed_literal *head;
3629 removed_literal *tail;
3633 static void add_removed_literal
3634 PARAMS ((removed_literal_list *, const r_reloc *, const r_reloc *));
3635 static removed_literal *find_removed_literal
3636 PARAMS ((removed_literal_list *, bfd_vma));
3637 static bfd_vma offset_with_removed_literals
3638 PARAMS ((removed_literal_list *, bfd_vma));
3641 /* Record that the literal at "from" is being removed. If "to" is not
3642 NULL, the "from" literal is being coalesced with the "to" literal. */
3644 static void
3645 add_removed_literal (removed_list, from, to)
3646 removed_literal_list *removed_list;
3647 const r_reloc *from;
3648 const r_reloc *to;
3650 removed_literal *r, *new_r, *next_r;
3652 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
3654 new_r->from = *from;
3655 if (to)
3656 new_r->to = *to;
3657 else
3658 new_r->to.abfd = NULL;
3659 new_r->next = NULL;
3661 r = removed_list->head;
3662 if (r == NULL)
3664 removed_list->head = new_r;
3665 removed_list->tail = new_r;
3667 /* Special check for common case of append. */
3668 else if (removed_list->tail->from.target_offset < from->target_offset)
3670 removed_list->tail->next = new_r;
3671 removed_list->tail = new_r;
3673 else
3675 while (r->from.target_offset < from->target_offset
3676 && r->next != NULL)
3678 r = r->next;
3680 next_r = r->next;
3681 r->next = new_r;
3682 new_r->next = next_r;
3683 if (next_r == NULL)
3684 removed_list->tail = new_r;
3689 /* Check if the list of removed literals contains an entry for the
3690 given address. Return the entry if found. */
3692 static removed_literal *
3693 find_removed_literal (removed_list, addr)
3694 removed_literal_list *removed_list;
3695 bfd_vma addr;
3697 removed_literal *r = removed_list->head;
3698 while (r && r->from.target_offset < addr)
3699 r = r->next;
3700 if (r && r->from.target_offset == addr)
3701 return r;
3702 return NULL;
3706 /* Adjust an offset in a section to compensate for literals that are
3707 being removed. Search the list of removed literals and subtract
3708 4 bytes for every removed literal prior to the given address. */
3710 static bfd_vma
3711 offset_with_removed_literals (removed_list, addr)
3712 removed_literal_list *removed_list;
3713 bfd_vma addr;
3715 removed_literal *r = removed_list->head;
3716 unsigned num_bytes = 0;
3718 if (r == NULL)
3719 return addr;
3721 while (r && r->from.target_offset <= addr)
3723 num_bytes += 4;
3724 r = r->next;
3726 if (num_bytes > addr)
3727 return 0;
3728 return (addr - num_bytes);
3732 /* Coalescing literals may require a relocation to refer to a section in
3733 a different input file, but the standard relocation information
3734 cannot express that. Instead, the reloc_bfd_fix structures are used
3735 to "fix" the relocations that refer to sections in other input files.
3736 These structures are kept on per-section lists. The "src_type" field
3737 records the relocation type in case there are multiple relocations on
3738 the same location. FIXME: This is ugly; an alternative might be to
3739 add new symbols with the "owner" field to some other input file. */
3741 typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
3743 struct reloc_bfd_fix_struct
3745 asection *src_sec;
3746 bfd_vma src_offset;
3747 unsigned src_type; /* Relocation type. */
3749 bfd *target_abfd;
3750 asection *target_sec;
3751 bfd_vma target_offset;
3753 reloc_bfd_fix *next;
3757 static reloc_bfd_fix *reloc_bfd_fix_init
3758 PARAMS ((asection *, bfd_vma, unsigned, bfd *, asection *, bfd_vma));
3759 static reloc_bfd_fix *get_bfd_fix
3760 PARAMS ((reloc_bfd_fix *, asection *, bfd_vma, unsigned));
3763 static reloc_bfd_fix *
3764 reloc_bfd_fix_init (src_sec, src_offset, src_type,
3765 target_abfd, target_sec, target_offset)
3766 asection *src_sec;
3767 bfd_vma src_offset;
3768 unsigned src_type;
3769 bfd *target_abfd;
3770 asection *target_sec;
3771 bfd_vma target_offset;
3773 reloc_bfd_fix *fix;
3775 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
3776 fix->src_sec = src_sec;
3777 fix->src_offset = src_offset;
3778 fix->src_type = src_type;
3779 fix->target_abfd = target_abfd;
3780 fix->target_sec = target_sec;
3781 fix->target_offset = target_offset;
3783 return fix;
3787 static reloc_bfd_fix *
3788 get_bfd_fix (fix_list, sec, offset, type)
3789 reloc_bfd_fix *fix_list;
3790 asection *sec;
3791 bfd_vma offset;
3792 unsigned type;
3794 reloc_bfd_fix *r;
3796 for (r = fix_list; r != NULL; r = r->next)
3798 if (r->src_sec == sec
3799 && r->src_offset == offset
3800 && r->src_type == type)
3801 return r;
3803 return NULL;
3807 /* Per-section data for relaxation. */
3809 struct xtensa_relax_info_struct
3811 bfd_boolean is_relaxable_literal_section;
3812 int visited; /* Number of times visited. */
3814 source_reloc *src_relocs; /* Array[src_count]. */
3815 int src_count;
3816 int src_next; /* Next src_relocs entry to assign. */
3818 removed_literal_list removed_list;
3820 reloc_bfd_fix *fix_list;
3823 struct elf_xtensa_section_data
3825 struct bfd_elf_section_data elf;
3826 xtensa_relax_info relax_info;
3829 static void init_xtensa_relax_info
3830 PARAMS ((asection *));
3831 static xtensa_relax_info *get_xtensa_relax_info
3832 PARAMS ((asection *));
3833 static void add_fix
3834 PARAMS ((asection *, reloc_bfd_fix *));
3837 static bfd_boolean
3838 elf_xtensa_new_section_hook (abfd, sec)
3839 bfd *abfd;
3840 asection *sec;
3842 struct elf_xtensa_section_data *sdata;
3843 bfd_size_type amt = sizeof (*sdata);
3845 sdata = (struct elf_xtensa_section_data *) bfd_zalloc (abfd, amt);
3846 if (sdata == NULL)
3847 return FALSE;
3848 sec->used_by_bfd = (PTR) sdata;
3850 return _bfd_elf_new_section_hook (abfd, sec);
3854 static void
3855 init_xtensa_relax_info (sec)
3856 asection *sec;
3858 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
3860 relax_info->is_relaxable_literal_section = FALSE;
3861 relax_info->visited = 0;
3863 relax_info->src_relocs = NULL;
3864 relax_info->src_count = 0;
3865 relax_info->src_next = 0;
3867 relax_info->removed_list.head = NULL;
3868 relax_info->removed_list.tail = NULL;
3870 relax_info->fix_list = NULL;
3874 static xtensa_relax_info *
3875 get_xtensa_relax_info (sec)
3876 asection *sec;
3878 struct elf_xtensa_section_data *section_data;
3880 /* No info available if no section or if it is an output section. */
3881 if (!sec || sec == sec->output_section)
3882 return NULL;
3884 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
3885 return &section_data->relax_info;
3889 static void
3890 add_fix (src_sec, fix)
3891 asection *src_sec;
3892 reloc_bfd_fix *fix;
3894 xtensa_relax_info *relax_info;
3896 relax_info = get_xtensa_relax_info (src_sec);
3897 fix->next = relax_info->fix_list;
3898 relax_info->fix_list = fix;
3902 /* Access to internal relocations, section contents and symbols. */
3904 /* During relaxation, we need to modify relocations, section contents,
3905 and symbol definitions, and we need to keep the original values from
3906 being reloaded from the input files, i.e., we need to "pin" the
3907 modified values in memory. We also want to continue to observe the
3908 setting of the "keep-memory" flag. The following functions wrap the
3909 standard BFD functions to take care of this for us. */
3911 static Elf_Internal_Rela *
3912 retrieve_internal_relocs (abfd, sec, keep_memory)
3913 bfd *abfd;
3914 asection *sec;
3915 bfd_boolean keep_memory;
3917 Elf_Internal_Rela *internal_relocs;
3919 if ((sec->flags & SEC_LINKER_CREATED) != 0)
3920 return NULL;
3922 internal_relocs = elf_section_data (sec)->relocs;
3923 if (internal_relocs == NULL)
3924 internal_relocs = (_bfd_elf_link_read_relocs
3925 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3926 keep_memory));
3927 return internal_relocs;
3931 static void
3932 pin_internal_relocs (sec, internal_relocs)
3933 asection *sec;
3934 Elf_Internal_Rela *internal_relocs;
3936 elf_section_data (sec)->relocs = internal_relocs;
3940 static void
3941 release_internal_relocs (sec, internal_relocs)
3942 asection *sec;
3943 Elf_Internal_Rela *internal_relocs;
3945 if (internal_relocs
3946 && elf_section_data (sec)->relocs != internal_relocs)
3947 free (internal_relocs);
3951 static bfd_byte *
3952 retrieve_contents (abfd, sec, keep_memory)
3953 bfd *abfd;
3954 asection *sec;
3955 bfd_boolean keep_memory;
3957 bfd_byte *contents;
3959 contents = elf_section_data (sec)->this_hdr.contents;
3961 if (contents == NULL && sec->_raw_size != 0)
3963 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
3964 if (contents != NULL)
3966 if (! bfd_get_section_contents (abfd, sec, contents,
3967 (file_ptr) 0, sec->_raw_size))
3969 free (contents);
3970 return NULL;
3972 if (keep_memory)
3973 elf_section_data (sec)->this_hdr.contents = contents;
3976 return contents;
3980 static void
3981 pin_contents (sec, contents)
3982 asection *sec;
3983 bfd_byte *contents;
3985 elf_section_data (sec)->this_hdr.contents = contents;
3989 static void
3990 release_contents (sec, contents)
3991 asection *sec;
3992 bfd_byte *contents;
3994 if (contents &&
3995 elf_section_data (sec)->this_hdr.contents != contents)
3996 free (contents);
4000 static Elf_Internal_Sym *
4001 retrieve_local_syms (input_bfd)
4002 bfd *input_bfd;
4004 Elf_Internal_Shdr *symtab_hdr;
4005 Elf_Internal_Sym *isymbuf;
4006 size_t locsymcount;
4008 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4009 locsymcount = symtab_hdr->sh_info;
4011 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4012 if (isymbuf == NULL && locsymcount != 0)
4013 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4014 NULL, NULL, NULL);
4016 /* Save the symbols for this input file so they won't be read again. */
4017 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
4018 symtab_hdr->contents = (unsigned char *) isymbuf;
4020 return isymbuf;
4024 /* Code for link-time relaxation. */
4026 /* Local helper functions. */
4027 static bfd_boolean analyze_relocations
4028 PARAMS ((struct bfd_link_info *));
4029 static bfd_boolean find_relaxable_sections
4030 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
4031 static bfd_boolean collect_source_relocs
4032 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4033 static bfd_boolean is_resolvable_asm_expansion
4034 PARAMS ((bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
4035 struct bfd_link_info *, bfd_boolean *));
4036 static bfd_boolean remove_literals
4037 PARAMS ((bfd *, asection *, struct bfd_link_info *, value_map_hash_table *));
4038 static bfd_boolean relax_section
4039 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4040 static bfd_boolean relax_property_section
4041 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4042 static bfd_boolean relax_section_symbols
4043 PARAMS ((bfd *, asection *));
4044 static bfd_boolean relocations_reach
4045 PARAMS ((source_reloc *, int, const r_reloc *));
4046 static void translate_reloc
4047 PARAMS ((const r_reloc *, r_reloc *));
4048 static Elf_Internal_Rela *get_irel_at_offset
4049 PARAMS ((asection *, Elf_Internal_Rela *, bfd_vma));
4050 static Elf_Internal_Rela *find_associated_l32r_irel
4051 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *,
4052 Elf_Internal_Rela *));
4053 static void shrink_dynamic_reloc_sections
4054 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *));
4057 static bfd_boolean
4058 elf_xtensa_relax_section (abfd, sec, link_info, again)
4059 bfd *abfd;
4060 asection *sec;
4061 struct bfd_link_info *link_info;
4062 bfd_boolean *again;
4064 static value_map_hash_table *values = NULL;
4065 xtensa_relax_info *relax_info;
4067 if (!values)
4069 /* Do some overall initialization for relaxation. */
4070 values = value_map_hash_table_init ();
4071 relaxing_section = TRUE;
4072 if (!analyze_relocations (link_info))
4073 return FALSE;
4075 *again = FALSE;
4077 /* Don't mess with linker-created sections. */
4078 if ((sec->flags & SEC_LINKER_CREATED) != 0)
4079 return TRUE;
4081 relax_info = get_xtensa_relax_info (sec);
4082 BFD_ASSERT (relax_info != NULL);
4084 switch (relax_info->visited)
4086 case 0:
4087 /* Note: It would be nice to fold this pass into
4088 analyze_relocations, but it is important for this step that the
4089 sections be examined in link order. */
4090 if (!remove_literals (abfd, sec, link_info, values))
4091 return FALSE;
4092 *again = TRUE;
4093 break;
4095 case 1:
4096 if (!relax_section (abfd, sec, link_info))
4097 return FALSE;
4098 *again = TRUE;
4099 break;
4101 case 2:
4102 if (!relax_section_symbols (abfd, sec))
4103 return FALSE;
4104 break;
4107 relax_info->visited++;
4108 return TRUE;
4111 /* Initialization for relaxation. */
4113 /* This function is called once at the start of relaxation. It scans
4114 all the input sections and marks the ones that are relaxable (i.e.,
4115 literal sections with L32R relocations against them). It then
4116 collect source_reloc information for all the relocations against
4117 those relaxable sections. */
4119 static bfd_boolean
4120 analyze_relocations (link_info)
4121 struct bfd_link_info *link_info;
4123 bfd *abfd;
4124 asection *sec;
4125 bfd_boolean is_relaxable = FALSE;
4127 /* Initialize the per-section relaxation info. */
4128 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4129 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4131 init_xtensa_relax_info (sec);
4134 /* Mark relaxable sections (and count relocations against each one). */
4135 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4136 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4138 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
4139 return FALSE;
4142 /* Bail out if there are no relaxable sections. */
4143 if (!is_relaxable)
4144 return TRUE;
4146 /* Allocate space for source_relocs. */
4147 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4148 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4150 xtensa_relax_info *relax_info;
4152 relax_info = get_xtensa_relax_info (sec);
4153 if (relax_info->is_relaxable_literal_section)
4155 relax_info->src_relocs = (source_reloc *)
4156 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
4160 /* Collect info on relocations against each relaxable section. */
4161 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4162 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4164 if (!collect_source_relocs (abfd, sec, link_info))
4165 return FALSE;
4168 return TRUE;
4172 /* Find all the literal sections that might be relaxed. The motivation
4173 for this pass is that collect_source_relocs() needs to record _all_
4174 the relocations that target each relaxable section. That is
4175 expensive and unnecessary unless the target section is actually going
4176 to be relaxed. This pass identifies all such sections by checking if
4177 they have L32Rs pointing to them. In the process, the total number
4178 of relocations targeting each section is also counted so that we
4179 know how much space to allocate for source_relocs against each
4180 relaxable literal section. */
4182 static bfd_boolean
4183 find_relaxable_sections (abfd, sec, link_info, is_relaxable_p)
4184 bfd *abfd;
4185 asection *sec;
4186 struct bfd_link_info *link_info;
4187 bfd_boolean *is_relaxable_p;
4189 Elf_Internal_Rela *internal_relocs;
4190 bfd_byte *contents;
4191 bfd_boolean ok = TRUE;
4192 unsigned i;
4194 internal_relocs = retrieve_internal_relocs (abfd, sec,
4195 link_info->keep_memory);
4196 if (internal_relocs == NULL)
4197 return ok;
4199 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4200 if (contents == NULL && sec->_raw_size != 0)
4202 ok = FALSE;
4203 goto error_return;
4206 for (i = 0; i < sec->reloc_count; i++)
4208 Elf_Internal_Rela *irel = &internal_relocs[i];
4209 r_reloc r_rel;
4210 asection *target_sec;
4211 xtensa_relax_info *target_relax_info;
4213 r_reloc_init (&r_rel, abfd, irel);
4215 target_sec = r_reloc_get_section (&r_rel);
4216 target_relax_info = get_xtensa_relax_info (target_sec);
4217 if (!target_relax_info)
4218 continue;
4220 /* Count relocations against the target section. */
4221 target_relax_info->src_count++;
4223 if (is_literal_section (target_sec)
4224 && is_l32r_relocation (sec, contents, irel)
4225 && r_reloc_is_defined (&r_rel))
4227 /* Mark the target section as relaxable. */
4228 target_relax_info->is_relaxable_literal_section = TRUE;
4229 *is_relaxable_p = TRUE;
4233 error_return:
4234 release_contents (sec, contents);
4235 release_internal_relocs (sec, internal_relocs);
4236 return ok;
4240 /* Record _all_ the relocations that point to relaxable literal
4241 sections, and get rid of ASM_EXPAND relocs by either converting them
4242 to ASM_SIMPLIFY or by removing them. */
4244 static bfd_boolean
4245 collect_source_relocs (abfd, sec, link_info)
4246 bfd *abfd;
4247 asection *sec;
4248 struct bfd_link_info *link_info;
4250 Elf_Internal_Rela *internal_relocs;
4251 bfd_byte *contents;
4252 bfd_boolean ok = TRUE;
4253 unsigned i;
4255 internal_relocs = retrieve_internal_relocs (abfd, sec,
4256 link_info->keep_memory);
4257 if (internal_relocs == NULL)
4258 return ok;
4260 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4261 if (contents == NULL && sec->_raw_size != 0)
4263 ok = FALSE;
4264 goto error_return;
4267 /* Record relocations against relaxable literal sections. */
4268 for (i = 0; i < sec->reloc_count; i++)
4270 Elf_Internal_Rela *irel = &internal_relocs[i];
4271 r_reloc r_rel;
4272 asection *target_sec;
4273 xtensa_relax_info *target_relax_info;
4275 r_reloc_init (&r_rel, abfd, irel);
4277 target_sec = r_reloc_get_section (&r_rel);
4278 target_relax_info = get_xtensa_relax_info (target_sec);
4280 if (target_relax_info
4281 && target_relax_info->is_relaxable_literal_section)
4283 xtensa_opcode opcode;
4284 xtensa_operand opnd;
4285 source_reloc *s_reloc;
4286 int src_next;
4288 src_next = target_relax_info->src_next++;
4289 s_reloc = &target_relax_info->src_relocs[src_next];
4291 opcode = get_relocation_opcode (sec, contents, irel);
4292 if (opcode == XTENSA_UNDEFINED)
4293 opnd = NULL;
4294 else
4295 opnd = xtensa_get_operand (xtensa_default_isa, opcode,
4296 get_relocation_opnd (irel));
4298 init_source_reloc (s_reloc, sec, &r_rel, opnd);
4302 /* Now get rid of ASM_EXPAND relocations. At this point, the
4303 src_relocs array for the target literal section may still be
4304 incomplete, but it must at least contain the entries for the L32R
4305 relocations associated with ASM_EXPANDs because they were just
4306 added in the preceding loop over the relocations. */
4308 for (i = 0; i < sec->reloc_count; i++)
4310 Elf_Internal_Rela *irel = &internal_relocs[i];
4311 bfd_boolean is_reachable;
4313 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4314 &is_reachable))
4315 continue;
4317 if (is_reachable)
4319 Elf_Internal_Rela *l32r_irel;
4320 r_reloc r_rel;
4321 asection *target_sec;
4322 xtensa_relax_info *target_relax_info;
4324 /* Mark the source_reloc for the L32R so that it will be
4325 removed in remove_literals(), along with the associated
4326 literal. */
4327 l32r_irel = find_associated_l32r_irel (sec, contents,
4328 irel, internal_relocs);
4329 if (l32r_irel == NULL)
4330 continue;
4332 r_reloc_init (&r_rel, abfd, l32r_irel);
4334 target_sec = r_reloc_get_section (&r_rel);
4335 target_relax_info = get_xtensa_relax_info (target_sec);
4337 if (target_relax_info
4338 && target_relax_info->is_relaxable_literal_section)
4340 source_reloc *s_reloc;
4342 /* Search the source_relocs for the entry corresponding to
4343 the l32r_irel. Note: The src_relocs array is not yet
4344 sorted, but it wouldn't matter anyway because we're
4345 searching by source offset instead of target offset. */
4346 s_reloc = find_source_reloc (target_relax_info->src_relocs,
4347 target_relax_info->src_next,
4348 sec, l32r_irel);
4349 BFD_ASSERT (s_reloc);
4350 s_reloc->is_null = TRUE;
4353 /* Convert this reloc to ASM_SIMPLIFY. */
4354 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4355 R_XTENSA_ASM_SIMPLIFY);
4356 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4358 pin_internal_relocs (sec, internal_relocs);
4360 else
4362 /* It is resolvable but doesn't reach. We resolve now
4363 by eliminating the relocation -- the call will remain
4364 expanded into L32R/CALLX. */
4365 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4366 pin_internal_relocs (sec, internal_relocs);
4370 error_return:
4371 release_contents (sec, contents);
4372 release_internal_relocs (sec, internal_relocs);
4373 return ok;
4377 /* Return TRUE if the asm expansion can be resolved. Generally it can
4378 be resolved on a final link or when a partial link locates it in the
4379 same section as the target. Set "is_reachable" flag if the target of
4380 the call is within the range of a direct call, given the current VMA
4381 for this section and the target section. */
4383 bfd_boolean
4384 is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4385 is_reachable_p)
4386 bfd *abfd;
4387 asection *sec;
4388 bfd_byte *contents;
4389 Elf_Internal_Rela *irel;
4390 struct bfd_link_info *link_info;
4391 bfd_boolean *is_reachable_p;
4393 asection *target_sec;
4394 bfd_vma target_offset;
4395 r_reloc r_rel;
4396 xtensa_opcode opcode, direct_call_opcode;
4397 bfd_vma self_address;
4398 bfd_vma dest_address;
4400 *is_reachable_p = FALSE;
4402 if (contents == NULL)
4403 return FALSE;
4405 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
4406 return FALSE;
4408 opcode = get_expanded_call_opcode (contents + irel->r_offset,
4409 sec->_raw_size - irel->r_offset);
4411 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4412 if (direct_call_opcode == XTENSA_UNDEFINED)
4413 return FALSE;
4415 /* Check and see that the target resolves. */
4416 r_reloc_init (&r_rel, abfd, irel);
4417 if (!r_reloc_is_defined (&r_rel))
4418 return FALSE;
4420 target_sec = r_reloc_get_section (&r_rel);
4421 target_offset = r_reloc_get_target_offset (&r_rel);
4423 /* If the target is in a shared library, then it doesn't reach. This
4424 isn't supposed to come up because the compiler should never generate
4425 non-PIC calls on systems that use shared libraries, but the linker
4426 shouldn't crash regardless. */
4427 if (!target_sec->output_section)
4428 return FALSE;
4430 /* For relocatable sections, we can only simplify when the output
4431 section of the target is the same as the output section of the
4432 source. */
4433 if (link_info->relocatable
4434 && (target_sec->output_section != sec->output_section))
4435 return FALSE;
4437 self_address = (sec->output_section->vma
4438 + sec->output_offset + irel->r_offset + 3);
4439 dest_address = (target_sec->output_section->vma
4440 + target_sec->output_offset + target_offset);
4442 *is_reachable_p = pcrel_reloc_fits
4443 (xtensa_get_operand (xtensa_default_isa, direct_call_opcode, 0),
4444 self_address, dest_address);
4446 if ((self_address >> CALL_SEGMENT_BITS) !=
4447 (dest_address >> CALL_SEGMENT_BITS))
4448 return FALSE;
4450 return TRUE;
4454 static Elf_Internal_Rela *
4455 find_associated_l32r_irel (sec, contents, other_irel, internal_relocs)
4456 asection *sec;
4457 bfd_byte *contents;
4458 Elf_Internal_Rela *other_irel;
4459 Elf_Internal_Rela *internal_relocs;
4461 unsigned i;
4463 for (i = 0; i < sec->reloc_count; i++)
4465 Elf_Internal_Rela *irel = &internal_relocs[i];
4467 if (irel == other_irel)
4468 continue;
4469 if (irel->r_offset != other_irel->r_offset)
4470 continue;
4471 if (is_l32r_relocation (sec, contents, irel))
4472 return irel;
4475 return NULL;
4478 /* First relaxation pass. */
4480 /* If the section is relaxable (i.e., a literal section), check each
4481 literal to see if it has the same value as another literal that has
4482 already been seen, either in the current section or a previous one.
4483 If so, add an entry to the per-section list of removed literals. The
4484 actual changes are deferred until the next pass. */
4486 static bfd_boolean
4487 remove_literals (abfd, sec, link_info, values)
4488 bfd *abfd;
4489 asection *sec;
4490 struct bfd_link_info *link_info;
4491 value_map_hash_table *values;
4493 xtensa_relax_info *relax_info;
4494 bfd_byte *contents;
4495 Elf_Internal_Rela *internal_relocs;
4496 source_reloc *src_relocs;
4497 bfd_boolean final_static_link;
4498 bfd_boolean ok = TRUE;
4499 int i;
4501 /* Do nothing if it is not a relaxable literal section. */
4502 relax_info = get_xtensa_relax_info (sec);
4503 BFD_ASSERT (relax_info);
4505 if (!relax_info->is_relaxable_literal_section)
4506 return ok;
4508 internal_relocs = retrieve_internal_relocs (abfd, sec,
4509 link_info->keep_memory);
4511 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4512 if (contents == NULL && sec->_raw_size != 0)
4514 ok = FALSE;
4515 goto error_return;
4518 final_static_link =
4519 (!link_info->relocatable
4520 && !elf_hash_table (link_info)->dynamic_sections_created);
4522 /* Sort the source_relocs by target offset. */
4523 src_relocs = relax_info->src_relocs;
4524 qsort (src_relocs, relax_info->src_count,
4525 sizeof (source_reloc), source_reloc_compare);
4527 for (i = 0; i < relax_info->src_count; i++)
4529 source_reloc *rel;
4530 Elf_Internal_Rela *irel = NULL;
4531 literal_value val;
4532 value_map *val_map;
4534 rel = &src_relocs[i];
4535 irel = get_irel_at_offset (sec, internal_relocs,
4536 rel->r_rel.target_offset);
4538 /* If the target_offset for this relocation is the same as the
4539 previous relocation, then we've already considered whether the
4540 literal can be coalesced. Skip to the next one.... */
4541 if (i != 0 && (src_relocs[i-1].r_rel.target_offset
4542 == rel->r_rel.target_offset))
4543 continue;
4545 /* Check if the relocation was from an L32R that is being removed
4546 because a CALLX was converted to a direct CALL, and check if
4547 there are no other relocations to the literal. */
4548 if (rel->is_null
4549 && (i == relax_info->src_count - 1
4550 || (src_relocs[i+1].r_rel.target_offset
4551 != rel->r_rel.target_offset)))
4553 /* Mark the unused literal so that it will be removed. */
4554 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
4556 /* Zero out the relocation on this literal location. */
4557 if (irel)
4559 if (elf_hash_table (link_info)->dynamic_sections_created)
4560 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4562 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4565 continue;
4568 /* Find the literal value. */
4569 r_reloc_init (&val.r_rel, abfd, irel);
4570 BFD_ASSERT (rel->r_rel.target_offset < sec->_raw_size);
4571 val.value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
4573 /* Check if we've seen another literal with the same value. */
4574 val_map = get_cached_value (values, &val, final_static_link);
4575 if (val_map != NULL)
4577 /* First check that THIS and all the other relocs to this
4578 literal will FIT if we move them to the new address. */
4580 if (relocations_reach (rel, relax_info->src_count - i,
4581 &val_map->loc))
4583 /* Mark that the literal will be coalesced. */
4584 add_removed_literal (&relax_info->removed_list,
4585 &rel->r_rel, &val_map->loc);
4587 else
4589 /* Relocations do not reach -- do not remove this literal. */
4590 val_map->loc = rel->r_rel;
4593 else
4595 /* This is the first time we've seen this literal value. */
4596 BFD_ASSERT (sec == r_reloc_get_section (&rel->r_rel));
4597 add_value_map (values, &val, &rel->r_rel, final_static_link);
4601 error_return:
4602 release_contents (sec, contents);
4603 release_internal_relocs (sec, internal_relocs);
4604 return ok;
4608 /* Check if the original relocations (presumably on L32R instructions)
4609 identified by reloc[0..N] can be changed to reference the literal
4610 identified by r_rel. If r_rel is out of range for any of the
4611 original relocations, then we don't want to coalesce the original
4612 literal with the one at r_rel. We only check reloc[0..N], where the
4613 offsets are all the same as for reloc[0] (i.e., they're all
4614 referencing the same literal) and where N is also bounded by the
4615 number of remaining entries in the "reloc" array. The "reloc" array
4616 is sorted by target offset so we know all the entries for the same
4617 literal will be contiguous. */
4619 static bfd_boolean
4620 relocations_reach (reloc, remaining_relocs, r_rel)
4621 source_reloc *reloc;
4622 int remaining_relocs;
4623 const r_reloc *r_rel;
4625 bfd_vma from_offset, source_address, dest_address;
4626 asection *sec;
4627 int i;
4629 if (!r_reloc_is_defined (r_rel))
4630 return FALSE;
4632 sec = r_reloc_get_section (r_rel);
4633 from_offset = reloc[0].r_rel.target_offset;
4635 for (i = 0; i < remaining_relocs; i++)
4637 if (reloc[i].r_rel.target_offset != from_offset)
4638 break;
4640 /* Ignore relocations that have been removed. */
4641 if (reloc[i].is_null)
4642 continue;
4644 /* The original and new output section for these must be the same
4645 in order to coalesce. */
4646 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
4647 != sec->output_section)
4648 return FALSE;
4650 /* A NULL operand means it is not a PC-relative relocation, so
4651 the literal can be moved anywhere. */
4652 if (reloc[i].opnd)
4654 /* Otherwise, check to see that it fits. */
4655 source_address = (reloc[i].source_sec->output_section->vma
4656 + reloc[i].source_sec->output_offset
4657 + reloc[i].r_rel.rela.r_offset);
4658 dest_address = (sec->output_section->vma
4659 + sec->output_offset
4660 + r_rel->target_offset);
4662 if (!pcrel_reloc_fits (reloc[i].opnd, source_address, dest_address))
4663 return FALSE;
4667 return TRUE;
4671 /* WARNING: linear search here. If the relocation are in order by
4672 address, we can use a faster binary search. ALSO, we assume that
4673 there is only 1 non-NONE relocation per address. */
4675 static Elf_Internal_Rela *
4676 get_irel_at_offset (sec, internal_relocs, offset)
4677 asection *sec;
4678 Elf_Internal_Rela *internal_relocs;
4679 bfd_vma offset;
4681 unsigned i;
4682 if (!internal_relocs)
4683 return NULL;
4684 for (i = 0; i < sec->reloc_count; i++)
4686 Elf_Internal_Rela *irel = &internal_relocs[i];
4687 if (irel->r_offset == offset
4688 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
4689 return irel;
4691 return NULL;
4695 /* Second relaxation pass. */
4697 /* Modify all of the relocations to point to the right spot, and if this
4698 is a relaxable section, delete the unwanted literals and fix the
4699 cooked_size. */
4701 bfd_boolean
4702 relax_section (abfd, sec, link_info)
4703 bfd *abfd;
4704 asection *sec;
4705 struct bfd_link_info *link_info;
4707 Elf_Internal_Rela *internal_relocs;
4708 xtensa_relax_info *relax_info;
4709 bfd_byte *contents;
4710 bfd_boolean ok = TRUE;
4711 unsigned i;
4713 relax_info = get_xtensa_relax_info (sec);
4714 BFD_ASSERT (relax_info);
4716 /* Handle property sections (e.g., literal tables) specially. */
4717 if (xtensa_is_property_section (sec))
4719 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
4720 return relax_property_section (abfd, sec, link_info);
4723 internal_relocs = retrieve_internal_relocs (abfd, sec,
4724 link_info->keep_memory);
4725 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4726 if (contents == NULL && sec->_raw_size != 0)
4728 ok = FALSE;
4729 goto error_return;
4732 if (internal_relocs)
4734 for (i = 0; i < sec->reloc_count; i++)
4736 Elf_Internal_Rela *irel;
4737 xtensa_relax_info *target_relax_info;
4738 bfd_vma source_offset;
4739 r_reloc r_rel;
4740 unsigned r_type;
4741 asection *target_sec;
4743 /* Locally change the source address.
4744 Translate the target to the new target address.
4745 If it points to this section and has been removed,
4746 NULLify it.
4747 Write it back. */
4749 irel = &internal_relocs[i];
4750 source_offset = irel->r_offset;
4752 r_type = ELF32_R_TYPE (irel->r_info);
4753 r_reloc_init (&r_rel, abfd, irel);
4755 if (relax_info->is_relaxable_literal_section)
4757 if (r_type != R_XTENSA_NONE
4758 && find_removed_literal (&relax_info->removed_list,
4759 irel->r_offset))
4761 /* Remove this relocation. */
4762 if (elf_hash_table (link_info)->dynamic_sections_created)
4763 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4764 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4765 irel->r_offset = offset_with_removed_literals
4766 (&relax_info->removed_list, irel->r_offset);
4767 continue;
4769 source_offset =
4770 offset_with_removed_literals (&relax_info->removed_list,
4771 irel->r_offset);
4772 irel->r_offset = source_offset;
4775 target_sec = r_reloc_get_section (&r_rel);
4776 target_relax_info = get_xtensa_relax_info (target_sec);
4778 if (target_relax_info
4779 && target_relax_info->is_relaxable_literal_section)
4781 r_reloc new_rel;
4782 reloc_bfd_fix *fix;
4784 translate_reloc (&r_rel, &new_rel);
4786 /* FIXME: If the relocation still references a section in
4787 the same input file, the relocation should be modified
4788 directly instead of adding a "fix" record. */
4790 fix = reloc_bfd_fix_init (sec, source_offset, r_type, 0,
4791 r_reloc_get_section (&new_rel),
4792 new_rel.target_offset);
4793 add_fix (sec, fix);
4796 pin_internal_relocs (sec, internal_relocs);
4800 if (relax_info->is_relaxable_literal_section)
4802 /* Walk through the contents and delete literals that are not needed
4803 anymore. */
4805 unsigned long size = sec->_cooked_size;
4806 unsigned long removed = 0;
4808 removed_literal *reloc = relax_info->removed_list.head;
4809 for (; reloc; reloc = reloc->next)
4811 unsigned long upper = sec->_raw_size;
4812 bfd_vma start = reloc->from.target_offset + 4;
4813 if (reloc->next)
4814 upper = reloc->next->from.target_offset;
4815 if (upper - start != 0)
4817 BFD_ASSERT (start <= upper);
4818 memmove (contents + start - removed - 4,
4819 contents + start,
4820 upper - start );
4821 pin_contents (sec, contents);
4823 removed += 4;
4824 size -= 4;
4827 /* Change the section size. */
4828 sec->_cooked_size = size;
4829 /* Also shrink _raw_size. (The code in relocate_section that
4830 checks that relocations are within the section must use
4831 _raw_size because of the way the stabs sections are relaxed;
4832 shrinking _raw_size means that these checks will not be
4833 unnecessarily lax.) */
4834 sec->_raw_size = size;
4837 error_return:
4838 release_internal_relocs (sec, internal_relocs);
4839 release_contents (sec, contents);
4840 return ok;
4844 /* Fix up a relocation to take account of removed literals. */
4846 static void
4847 translate_reloc (orig_rel, new_rel)
4848 const r_reloc *orig_rel;
4849 r_reloc *new_rel;
4851 asection *sec;
4852 xtensa_relax_info *relax_info;
4853 removed_literal *removed;
4854 unsigned long new_offset;
4856 *new_rel = *orig_rel;
4858 if (!r_reloc_is_defined (orig_rel))
4859 return;
4860 sec = r_reloc_get_section (orig_rel);
4862 relax_info = get_xtensa_relax_info (sec);
4863 BFD_ASSERT (relax_info);
4865 if (!relax_info->is_relaxable_literal_section)
4866 return;
4868 /* Check if the original relocation is against a literal being removed. */
4869 removed = find_removed_literal (&relax_info->removed_list,
4870 orig_rel->target_offset);
4871 if (removed)
4873 asection *new_sec;
4875 /* The fact that there is still a relocation to this literal indicates
4876 that the literal is being coalesced, not simply removed. */
4877 BFD_ASSERT (removed->to.abfd != NULL);
4879 /* This was moved to some other address (possibly in another section). */
4880 *new_rel = removed->to;
4881 new_sec = r_reloc_get_section (new_rel);
4882 if (new_sec != sec)
4884 sec = new_sec;
4885 relax_info = get_xtensa_relax_info (sec);
4886 if (!relax_info || !relax_info->is_relaxable_literal_section)
4887 return;
4891 /* ...and the target address may have been moved within its section. */
4892 new_offset = offset_with_removed_literals (&relax_info->removed_list,
4893 new_rel->target_offset);
4895 /* Modify the offset and addend. */
4896 new_rel->target_offset = new_offset;
4897 new_rel->rela.r_addend += (new_offset - new_rel->target_offset);
4901 /* For dynamic links, there may be a dynamic relocation for each
4902 literal. The number of dynamic relocations must be computed in
4903 size_dynamic_sections, which occurs before relaxation. When a
4904 literal is removed, this function checks if there is a corresponding
4905 dynamic relocation and shrinks the size of the appropriate dynamic
4906 relocation section accordingly. At this point, the contents of the
4907 dynamic relocation sections have not yet been filled in, so there's
4908 nothing else that needs to be done. */
4910 static void
4911 shrink_dynamic_reloc_sections (info, abfd, input_section, rel)
4912 struct bfd_link_info *info;
4913 bfd *abfd;
4914 asection *input_section;
4915 Elf_Internal_Rela *rel;
4917 Elf_Internal_Shdr *symtab_hdr;
4918 struct elf_link_hash_entry **sym_hashes;
4919 unsigned long r_symndx;
4920 int r_type;
4921 struct elf_link_hash_entry *h;
4922 bfd_boolean dynamic_symbol;
4924 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4925 sym_hashes = elf_sym_hashes (abfd);
4927 r_type = ELF32_R_TYPE (rel->r_info);
4928 r_symndx = ELF32_R_SYM (rel->r_info);
4930 if (r_symndx < symtab_hdr->sh_info)
4931 h = NULL;
4932 else
4933 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4935 dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
4937 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
4938 && (input_section->flags & SEC_ALLOC) != 0
4939 && (dynamic_symbol || info->shared))
4941 bfd *dynobj;
4942 const char *srel_name;
4943 asection *srel;
4944 bfd_boolean is_plt = FALSE;
4946 dynobj = elf_hash_table (info)->dynobj;
4947 BFD_ASSERT (dynobj != NULL);
4949 if (dynamic_symbol && r_type == R_XTENSA_PLT)
4951 srel_name = ".rela.plt";
4952 is_plt = TRUE;
4954 else
4955 srel_name = ".rela.got";
4957 /* Reduce size of the .rela.* section by one reloc. */
4958 srel = bfd_get_section_by_name (dynobj, srel_name);
4959 BFD_ASSERT (srel != NULL);
4960 BFD_ASSERT (srel->_cooked_size >= sizeof (Elf32_External_Rela));
4961 srel->_cooked_size -= sizeof (Elf32_External_Rela);
4963 /* Also shrink _raw_size. (This seems wrong but other bfd code seems
4964 to assume that linker-created sections will never be relaxed and
4965 hence _raw_size must always equal _cooked_size.) */
4966 srel->_raw_size = srel->_cooked_size;
4968 if (is_plt)
4970 asection *splt, *sgotplt, *srelgot;
4971 int reloc_index, chunk;
4973 /* Find the PLT reloc index of the entry being removed. This
4974 is computed from the size of ".rela.plt". It is needed to
4975 figure out which PLT chunk to resize. Usually "last index
4976 = size - 1" since the index starts at zero, but in this
4977 context, the size has just been decremented so there's no
4978 need to subtract one. */
4979 reloc_index = srel->_cooked_size / sizeof (Elf32_External_Rela);
4981 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
4982 splt = elf_xtensa_get_plt_section (dynobj, chunk);
4983 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
4984 BFD_ASSERT (splt != NULL && sgotplt != NULL);
4986 /* Check if an entire PLT chunk has just been eliminated. */
4987 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
4989 /* The two magic GOT entries for that chunk can go away. */
4990 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4991 BFD_ASSERT (srelgot != NULL);
4992 srelgot->reloc_count -= 2;
4993 srelgot->_cooked_size -= 2 * sizeof (Elf32_External_Rela);
4994 /* Shrink _raw_size (see comment above). */
4995 srelgot->_raw_size = srelgot->_cooked_size;
4997 sgotplt->_cooked_size -= 8;
4999 /* There should be only one entry left (and it will be
5000 removed below). */
5001 BFD_ASSERT (sgotplt->_cooked_size == 4);
5002 BFD_ASSERT (splt->_cooked_size == PLT_ENTRY_SIZE);
5005 BFD_ASSERT (sgotplt->_cooked_size >= 4);
5006 BFD_ASSERT (splt->_cooked_size >= PLT_ENTRY_SIZE);
5008 sgotplt->_cooked_size -= 4;
5009 splt->_cooked_size -= PLT_ENTRY_SIZE;
5011 /* Shrink _raw_sizes (see comment above). */
5012 sgotplt->_raw_size = sgotplt->_cooked_size;
5013 splt->_raw_size = splt->_cooked_size;
5019 /* This is similar to relax_section except that when a target is moved,
5020 we shift addresses up. We also need to modify the size. This
5021 algorithm does NOT allow for relocations into the middle of the
5022 property sections. */
5024 static bfd_boolean
5025 relax_property_section (abfd, sec, link_info)
5026 bfd *abfd;
5027 asection *sec;
5028 struct bfd_link_info *link_info;
5030 Elf_Internal_Rela *internal_relocs;
5031 bfd_byte *contents;
5032 unsigned i, nexti;
5033 bfd_boolean ok = TRUE;
5035 internal_relocs = retrieve_internal_relocs (abfd, sec,
5036 link_info->keep_memory);
5037 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5038 if (contents == NULL && sec->_raw_size != 0)
5040 ok = FALSE;
5041 goto error_return;
5044 if (internal_relocs)
5046 for (i = 0; i < sec->reloc_count; i++)
5048 Elf_Internal_Rela *irel;
5049 xtensa_relax_info *target_relax_info;
5050 r_reloc r_rel;
5051 unsigned r_type;
5052 asection *target_sec;
5054 /* Locally change the source address.
5055 Translate the target to the new target address.
5056 If it points to this section and has been removed, MOVE IT.
5057 Also, don't forget to modify the associated SIZE at
5058 (offset + 4). */
5060 irel = &internal_relocs[i];
5061 r_type = ELF32_R_TYPE (irel->r_info);
5062 if (r_type == R_XTENSA_NONE)
5063 continue;
5065 r_reloc_init (&r_rel, abfd, irel);
5067 target_sec = r_reloc_get_section (&r_rel);
5068 target_relax_info = get_xtensa_relax_info (target_sec);
5070 if (target_relax_info
5071 && target_relax_info->is_relaxable_literal_section)
5073 /* Translate the relocation's destination. */
5074 bfd_vma new_offset;
5075 bfd_vma new_end_offset;
5076 bfd_byte *size_p;
5077 long old_size, new_size;
5079 new_offset =
5080 offset_with_removed_literals (&target_relax_info->removed_list,
5081 r_rel.target_offset);
5083 /* Assert that we are not out of bounds. */
5084 size_p = &contents[irel->r_offset + 4];
5085 old_size = bfd_get_32 (abfd, &contents[irel->r_offset + 4]);
5087 new_end_offset =
5088 offset_with_removed_literals (&target_relax_info->removed_list,
5089 r_rel.target_offset + old_size);
5091 new_size = new_end_offset - new_offset;
5092 if (new_size != old_size)
5094 bfd_put_32 (abfd, new_size, size_p);
5095 pin_contents (sec, contents);
5098 if (new_offset != r_rel.target_offset)
5100 bfd_vma diff = new_offset - r_rel.target_offset;
5101 irel->r_addend += diff;
5102 pin_internal_relocs (sec, internal_relocs);
5108 /* Combine adjacent property table entries. This is also done in
5109 finish_dynamic_sections() but at that point it's too late to
5110 reclaim the space in the output section, so we do this twice. */
5112 if (internal_relocs)
5114 Elf_Internal_Rela *last_irel = NULL;
5115 int removed_bytes = 0;
5116 bfd_vma offset, last_irel_offset;
5117 bfd_vma section_size;
5119 /* Walk over memory and irels at the same time.
5120 This REQUIRES that the internal_relocs be sorted by offset. */
5121 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
5122 internal_reloc_compare);
5123 nexti = 0; /* Index into internal_relocs. */
5125 pin_internal_relocs (sec, internal_relocs);
5126 pin_contents (sec, contents);
5128 last_irel_offset = (bfd_vma) -1;
5129 section_size = (sec->_cooked_size ? sec->_cooked_size : sec->_raw_size);
5130 BFD_ASSERT (section_size % 8 == 0);
5132 for (offset = 0; offset < section_size; offset += 8)
5134 Elf_Internal_Rela *irel, *next_irel;
5135 bfd_vma bytes_to_remove, size, actual_offset;
5136 bfd_boolean remove_this_irel;
5138 irel = NULL;
5139 next_irel = NULL;
5141 /* Find the next two relocations (if there are that many left),
5142 skipping over any R_XTENSA_NONE relocs. On entry, "nexti" is
5143 the starting reloc index. After these two loops, "i"
5144 is the index of the first non-NONE reloc past that starting
5145 index, and "nexti" is the index for the next non-NONE reloc
5146 after "i". */
5148 for (i = nexti; i < sec->reloc_count; i++)
5150 if (ELF32_R_TYPE (internal_relocs[i].r_info) != R_XTENSA_NONE)
5152 irel = &internal_relocs[i];
5153 break;
5155 internal_relocs[i].r_offset -= removed_bytes;
5158 for (nexti = i + 1; nexti < sec->reloc_count; nexti++)
5160 if (ELF32_R_TYPE (internal_relocs[nexti].r_info)
5161 != R_XTENSA_NONE)
5163 next_irel = &internal_relocs[nexti];
5164 break;
5166 internal_relocs[nexti].r_offset -= removed_bytes;
5169 remove_this_irel = FALSE;
5170 bytes_to_remove = 0;
5171 actual_offset = offset - removed_bytes;
5172 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
5174 /* Check that the irels are sorted by offset,
5175 with only one per address. */
5176 BFD_ASSERT (!irel || (int) irel->r_offset > (int) last_irel_offset);
5177 BFD_ASSERT (!next_irel || next_irel->r_offset > irel->r_offset);
5179 /* Make sure there isn't a reloc on the size field. */
5180 if (irel && irel->r_offset == offset + 4)
5182 irel->r_offset -= removed_bytes;
5183 last_irel_offset = irel->r_offset;
5185 else if (next_irel && next_irel->r_offset == offset + 4)
5187 nexti += 1;
5188 irel->r_offset -= removed_bytes;
5189 next_irel->r_offset -= removed_bytes;
5190 last_irel_offset = next_irel->r_offset;
5192 else if (size == 0)
5194 /* Always remove entries with zero size. */
5195 bytes_to_remove = 8;
5196 if (irel && irel->r_offset == offset)
5198 remove_this_irel = TRUE;
5200 irel->r_offset -= removed_bytes;
5201 last_irel_offset = irel->r_offset;
5204 else if (irel && irel->r_offset == offset)
5206 if (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32)
5208 if (last_irel)
5210 bfd_vma old_size =
5211 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
5212 bfd_vma old_address =
5213 (last_irel->r_addend
5214 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
5215 bfd_vma new_address =
5216 (irel->r_addend
5217 + bfd_get_32 (abfd, &contents[actual_offset]));
5219 if ((ELF32_R_SYM (irel->r_info) ==
5220 ELF32_R_SYM (last_irel->r_info))
5221 && (old_address + old_size == new_address))
5223 /* fix the old size */
5224 bfd_put_32 (abfd, old_size + size,
5225 &contents[last_irel->r_offset + 4]);
5226 bytes_to_remove = 8;
5227 remove_this_irel = TRUE;
5229 else
5230 last_irel = irel;
5232 else
5233 last_irel = irel;
5236 irel->r_offset -= removed_bytes;
5237 last_irel_offset = irel->r_offset;
5240 if (remove_this_irel)
5242 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
5243 irel->r_offset -= bytes_to_remove;
5246 if (bytes_to_remove != 0)
5248 removed_bytes += bytes_to_remove;
5249 if (offset + 8 < section_size)
5250 memmove (&contents[actual_offset],
5251 &contents[actual_offset+8],
5252 section_size - offset - 8);
5256 if (removed_bytes)
5258 /* Clear the removed bytes. */
5259 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
5261 sec->_cooked_size = section_size - removed_bytes;
5262 /* Also shrink _raw_size. (The code in relocate_section that
5263 checks that relocations are within the section must use
5264 _raw_size because of the way the stabs sections are
5265 relaxed; shrinking _raw_size means that these checks will
5266 not be unnecessarily lax.) */
5267 sec->_raw_size = sec->_cooked_size;
5269 if (xtensa_is_littable_section (sec))
5271 bfd *dynobj = elf_hash_table (link_info)->dynobj;
5272 if (dynobj)
5274 asection *sgotloc =
5275 bfd_get_section_by_name (dynobj, ".got.loc");
5276 if (sgotloc)
5278 bfd_size_type sgotloc_size =
5279 (sgotloc->_cooked_size ? sgotloc->_cooked_size
5280 : sgotloc->_raw_size);
5281 sgotloc->_cooked_size = sgotloc_size - removed_bytes;
5282 sgotloc->_raw_size = sgotloc_size - removed_bytes;
5289 error_return:
5290 release_internal_relocs (sec, internal_relocs);
5291 release_contents (sec, contents);
5292 return ok;
5296 /* Third relaxation pass. */
5298 /* Change symbol values to account for removed literals. */
5300 bfd_boolean
5301 relax_section_symbols (abfd, sec)
5302 bfd *abfd;
5303 asection *sec;
5305 xtensa_relax_info *relax_info;
5306 unsigned int sec_shndx;
5307 Elf_Internal_Shdr *symtab_hdr;
5308 Elf_Internal_Sym *isymbuf;
5309 unsigned i, num_syms, num_locals;
5311 relax_info = get_xtensa_relax_info (sec);
5312 BFD_ASSERT (relax_info);
5314 if (!relax_info->is_relaxable_literal_section)
5315 return TRUE;
5317 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5319 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5320 isymbuf = retrieve_local_syms (abfd);
5322 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5323 num_locals = symtab_hdr->sh_info;
5325 /* Adjust the local symbols defined in this section. */
5326 for (i = 0; i < num_locals; i++)
5328 Elf_Internal_Sym *isym = &isymbuf[i];
5330 if (isym->st_shndx == sec_shndx)
5332 bfd_vma new_address = offset_with_removed_literals
5333 (&relax_info->removed_list, isym->st_value);
5334 if (new_address != isym->st_value)
5335 isym->st_value = new_address;
5339 /* Now adjust the global symbols defined in this section. */
5340 for (i = 0; i < (num_syms - num_locals); i++)
5342 struct elf_link_hash_entry *sym_hash;
5344 sym_hash = elf_sym_hashes (abfd)[i];
5346 if (sym_hash->root.type == bfd_link_hash_warning)
5347 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
5349 if ((sym_hash->root.type == bfd_link_hash_defined
5350 || sym_hash->root.type == bfd_link_hash_defweak)
5351 && sym_hash->root.u.def.section == sec)
5353 bfd_vma new_address = offset_with_removed_literals
5354 (&relax_info->removed_list, sym_hash->root.u.def.value);
5355 if (new_address != sym_hash->root.u.def.value)
5356 sym_hash->root.u.def.value = new_address;
5360 return TRUE;
5364 /* "Fix" handling functions, called while performing relocations. */
5366 static void
5367 do_fix_for_relocatable_link (rel, input_bfd, input_section)
5368 Elf_Internal_Rela *rel;
5369 bfd *input_bfd;
5370 asection *input_section;
5372 r_reloc r_rel;
5373 asection *sec, *old_sec;
5374 bfd_vma old_offset;
5375 int r_type = ELF32_R_TYPE (rel->r_info);
5376 reloc_bfd_fix *fix_list;
5377 reloc_bfd_fix *fix;
5379 if (r_type == R_XTENSA_NONE)
5380 return;
5382 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5383 if (fix_list == NULL)
5384 return;
5386 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5387 if (fix == NULL)
5388 return;
5390 r_reloc_init (&r_rel, input_bfd, rel);
5391 old_sec = r_reloc_get_section (&r_rel);
5392 old_offset = r_reloc_get_target_offset (&r_rel);
5394 if (old_sec == NULL || !r_reloc_is_defined (&r_rel))
5396 BFD_ASSERT (r_type == R_XTENSA_ASM_EXPAND);
5397 /* Leave it be. Resolution will happen in a later stage. */
5399 else
5401 sec = fix->target_sec;
5402 rel->r_addend += ((sec->output_offset + fix->target_offset)
5403 - (old_sec->output_offset + old_offset));
5408 static void
5409 do_fix_for_final_link (rel, input_section, relocationp)
5410 Elf_Internal_Rela *rel;
5411 asection *input_section;
5412 bfd_vma *relocationp;
5414 asection *sec;
5415 int r_type = ELF32_R_TYPE (rel->r_info);
5416 reloc_bfd_fix *fix_list;
5417 reloc_bfd_fix *fix;
5419 if (r_type == R_XTENSA_NONE)
5420 return;
5422 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5423 if (fix_list == NULL)
5424 return;
5426 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5427 if (fix == NULL)
5428 return;
5430 sec = fix->target_sec;
5431 *relocationp = (sec->output_section->vma
5432 + sec->output_offset
5433 + fix->target_offset - rel->r_addend);
5437 /* Miscellaneous utility functions.... */
5439 static asection *
5440 elf_xtensa_get_plt_section (dynobj, chunk)
5441 bfd *dynobj;
5442 int chunk;
5444 char plt_name[10];
5446 if (chunk == 0)
5447 return bfd_get_section_by_name (dynobj, ".plt");
5449 sprintf (plt_name, ".plt.%u", chunk);
5450 return bfd_get_section_by_name (dynobj, plt_name);
5454 static asection *
5455 elf_xtensa_get_gotplt_section (dynobj, chunk)
5456 bfd *dynobj;
5457 int chunk;
5459 char got_name[14];
5461 if (chunk == 0)
5462 return bfd_get_section_by_name (dynobj, ".got.plt");
5464 sprintf (got_name, ".got.plt.%u", chunk);
5465 return bfd_get_section_by_name (dynobj, got_name);
5469 /* Get the input section for a given symbol index.
5470 If the symbol is:
5471 . a section symbol, return the section;
5472 . a common symbol, return the common section;
5473 . an undefined symbol, return the undefined section;
5474 . an indirect symbol, follow the links;
5475 . an absolute value, return the absolute section. */
5477 static asection *
5478 get_elf_r_symndx_section (abfd, r_symndx)
5479 bfd *abfd;
5480 unsigned long r_symndx;
5482 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5483 asection *target_sec = NULL;
5484 if (r_symndx < symtab_hdr->sh_info)
5486 Elf_Internal_Sym *isymbuf;
5487 unsigned int section_index;
5489 isymbuf = retrieve_local_syms (abfd);
5490 section_index = isymbuf[r_symndx].st_shndx;
5492 if (section_index == SHN_UNDEF)
5493 target_sec = bfd_und_section_ptr;
5494 else if (section_index > 0 && section_index < SHN_LORESERVE)
5495 target_sec = bfd_section_from_elf_index (abfd, section_index);
5496 else if (section_index == SHN_ABS)
5497 target_sec = bfd_abs_section_ptr;
5498 else if (section_index == SHN_COMMON)
5499 target_sec = bfd_com_section_ptr;
5500 else
5501 /* Who knows? */
5502 target_sec = NULL;
5504 else
5506 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5507 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
5509 while (h->root.type == bfd_link_hash_indirect
5510 || h->root.type == bfd_link_hash_warning)
5511 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5513 switch (h->root.type)
5515 case bfd_link_hash_defined:
5516 case bfd_link_hash_defweak:
5517 target_sec = h->root.u.def.section;
5518 break;
5519 case bfd_link_hash_common:
5520 target_sec = bfd_com_section_ptr;
5521 break;
5522 case bfd_link_hash_undefined:
5523 case bfd_link_hash_undefweak:
5524 target_sec = bfd_und_section_ptr;
5525 break;
5526 default: /* New indirect warning. */
5527 target_sec = bfd_und_section_ptr;
5528 break;
5531 return target_sec;
5535 static struct elf_link_hash_entry *
5536 get_elf_r_symndx_hash_entry (abfd, r_symndx)
5537 bfd *abfd;
5538 unsigned long r_symndx;
5540 unsigned long indx;
5541 struct elf_link_hash_entry *h;
5542 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5544 if (r_symndx < symtab_hdr->sh_info)
5545 return NULL;
5547 indx = r_symndx - symtab_hdr->sh_info;
5548 h = elf_sym_hashes (abfd)[indx];
5549 while (h->root.type == bfd_link_hash_indirect
5550 || h->root.type == bfd_link_hash_warning)
5551 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5552 return h;
5556 /* Get the section-relative offset for a symbol number. */
5558 static bfd_vma
5559 get_elf_r_symndx_offset (abfd, r_symndx)
5560 bfd *abfd;
5561 unsigned long r_symndx;
5563 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5564 bfd_vma offset = 0;
5566 if (r_symndx < symtab_hdr->sh_info)
5568 Elf_Internal_Sym *isymbuf;
5569 isymbuf = retrieve_local_syms (abfd);
5570 offset = isymbuf[r_symndx].st_value;
5572 else
5574 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5575 struct elf_link_hash_entry *h =
5576 elf_sym_hashes (abfd)[indx];
5578 while (h->root.type == bfd_link_hash_indirect
5579 || h->root.type == bfd_link_hash_warning)
5580 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5581 if (h->root.type == bfd_link_hash_defined
5582 || h->root.type == bfd_link_hash_defweak)
5583 offset = h->root.u.def.value;
5585 return offset;
5589 static bfd_boolean
5590 pcrel_reloc_fits (opnd, self_address, dest_address)
5591 xtensa_operand opnd;
5592 bfd_vma self_address;
5593 bfd_vma dest_address;
5595 uint32 new_address =
5596 xtensa_operand_do_reloc (opnd, dest_address, self_address);
5597 return (xtensa_operand_encode (opnd, &new_address)
5598 == xtensa_encode_result_ok);
5602 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
5603 static int insn_sec_len = sizeof (XTENSA_INSN_SEC_NAME) - 1;
5604 static int lit_sec_len = sizeof (XTENSA_LIT_SEC_NAME) - 1;
5607 static bfd_boolean
5608 xtensa_is_property_section (sec)
5609 asection *sec;
5611 if (strncmp (XTENSA_INSN_SEC_NAME, sec->name, insn_sec_len) == 0
5612 || strncmp (XTENSA_LIT_SEC_NAME, sec->name, lit_sec_len) == 0)
5613 return TRUE;
5615 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0
5616 && (sec->name[linkonce_len] == 'x'
5617 || sec->name[linkonce_len] == 'p')
5618 && sec->name[linkonce_len + 1] == '.')
5619 return TRUE;
5621 return FALSE;
5625 static bfd_boolean
5626 xtensa_is_littable_section (sec)
5627 asection *sec;
5629 if (strncmp (XTENSA_LIT_SEC_NAME, sec->name, lit_sec_len) == 0)
5630 return TRUE;
5632 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0
5633 && sec->name[linkonce_len] == 'p'
5634 && sec->name[linkonce_len + 1] == '.')
5635 return TRUE;
5637 return FALSE;
5641 static bfd_boolean
5642 is_literal_section (sec)
5643 asection *sec;
5645 /* FIXME: the current definition of this leaves a lot to be desired.... */
5646 if (sec == NULL || sec->name == NULL)
5647 return FALSE;
5648 return (strstr (sec->name, "literal") != NULL);
5652 static int
5653 internal_reloc_compare (ap, bp)
5654 const PTR ap;
5655 const PTR bp;
5657 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
5658 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
5660 return (a->r_offset - b->r_offset);
5664 char *
5665 xtensa_get_property_section_name (sec, base_name)
5666 asection *sec;
5667 const char *base_name;
5669 if (strncmp (sec->name, ".gnu.linkonce.", linkonce_len) == 0)
5671 char *prop_sec_name;
5672 const char *suffix;
5673 char linkonce_kind = 0;
5675 if (strcmp (base_name, XTENSA_INSN_SEC_NAME) == 0)
5676 linkonce_kind = 'x';
5677 else if (strcmp (base_name, XTENSA_LIT_SEC_NAME) == 0)
5678 linkonce_kind = 'p';
5679 else
5680 abort ();
5682 prop_sec_name = (char *) bfd_malloc (strlen (sec->name) + 1);
5683 memcpy (prop_sec_name, ".gnu.linkonce.", linkonce_len);
5684 prop_sec_name[linkonce_len] = linkonce_kind;
5685 prop_sec_name[linkonce_len + 1] = '.';
5687 suffix = sec->name + linkonce_len;
5688 while (*suffix)
5690 suffix += 1;
5691 if (suffix[-1] == '.')
5692 break;
5694 strcpy (prop_sec_name + linkonce_len + 2, suffix);
5696 return prop_sec_name;
5699 return strdup (base_name);
5703 /* Other functions called directly by the linker. */
5705 bfd_boolean
5706 xtensa_callback_required_dependence (abfd, sec, link_info, callback, closure)
5707 bfd *abfd;
5708 asection *sec;
5709 struct bfd_link_info *link_info;
5710 deps_callback_t callback;
5711 PTR closure;
5713 Elf_Internal_Rela *internal_relocs;
5714 bfd_byte *contents;
5715 unsigned i;
5716 bfd_boolean ok = TRUE;
5718 /* ".plt*" sections have no explicit relocations but they contain L32R
5719 instructions that reference the corresponding ".got.plt*" sections. */
5720 if ((sec->flags & SEC_LINKER_CREATED) != 0
5721 && strncmp (sec->name, ".plt", 4) == 0)
5723 asection *sgotplt;
5725 /* Find the corresponding ".got.plt*" section. */
5726 if (sec->name[4] == '\0')
5727 sgotplt = bfd_get_section_by_name (sec->owner, ".got.plt");
5728 else
5730 char got_name[14];
5731 int chunk = 0;
5733 BFD_ASSERT (sec->name[4] == '.');
5734 chunk = strtol (&sec->name[5], NULL, 10);
5736 sprintf (got_name, ".got.plt.%u", chunk);
5737 sgotplt = bfd_get_section_by_name (sec->owner, got_name);
5739 BFD_ASSERT (sgotplt);
5741 /* Assume worst-case offsets: L32R at the very end of the ".plt"
5742 section referencing a literal at the very beginning of
5743 ".got.plt". This is very close to the real dependence, anyway. */
5744 (*callback) (sec, sec->_raw_size, sgotplt, 0, closure);
5747 internal_relocs = retrieve_internal_relocs (abfd, sec,
5748 link_info->keep_memory);
5749 if (internal_relocs == NULL
5750 || sec->reloc_count == 0)
5751 return ok;
5753 /* Cache the contents for the duration of this scan. */
5754 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5755 if (contents == NULL && sec->_raw_size != 0)
5757 ok = FALSE;
5758 goto error_return;
5761 if (xtensa_default_isa == NULL)
5762 xtensa_isa_init ();
5764 for (i = 0; i < sec->reloc_count; i++)
5766 Elf_Internal_Rela *irel = &internal_relocs[i];
5767 if (is_l32r_relocation (sec, contents, irel))
5769 r_reloc l32r_rel;
5770 asection *target_sec;
5771 bfd_vma target_offset;
5773 r_reloc_init (&l32r_rel, abfd, irel);
5774 target_sec = NULL;
5775 target_offset = 0;
5776 /* L32Rs must be local to the input file. */
5777 if (r_reloc_is_defined (&l32r_rel))
5779 target_sec = r_reloc_get_section (&l32r_rel);
5780 target_offset = r_reloc_get_target_offset (&l32r_rel);
5782 (*callback) (sec, irel->r_offset, target_sec, target_offset,
5783 closure);
5787 error_return:
5788 release_internal_relocs (sec, internal_relocs);
5789 release_contents (sec, contents);
5790 return ok;
5793 /* The default literal sections should always be marked as "code" (i.e.,
5794 SHF_EXECINSTR). This is particularly important for the Linux kernel
5795 module loader so that the literals are not placed after the text. */
5796 static struct bfd_elf_special_section const elf_xtensa_special_sections[]=
5798 { ".literal", 8, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5799 { ".init.literal", 13, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5800 { ".fini.literal", 13, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5801 { NULL, 0, 0, 0, 0 }
5805 #ifndef ELF_ARCH
5806 #define TARGET_LITTLE_SYM bfd_elf32_xtensa_le_vec
5807 #define TARGET_LITTLE_NAME "elf32-xtensa-le"
5808 #define TARGET_BIG_SYM bfd_elf32_xtensa_be_vec
5809 #define TARGET_BIG_NAME "elf32-xtensa-be"
5810 #define ELF_ARCH bfd_arch_xtensa
5812 /* The new EM_XTENSA value will be recognized beginning in the Xtensa T1040
5813 release. However, we still have to generate files with the EM_XTENSA_OLD
5814 value so that pre-T1040 tools can read the files. As soon as we stop
5815 caring about pre-T1040 tools, the following two values should be
5816 swapped. At the same time, any other code that uses EM_XTENSA_OLD
5817 (e.g., prep_headers() in elf.c) should be changed to use EM_XTENSA. */
5818 #define ELF_MACHINE_CODE EM_XTENSA_OLD
5819 #define ELF_MACHINE_ALT1 EM_XTENSA
5821 #if XCHAL_HAVE_MMU
5822 #define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
5823 #else /* !XCHAL_HAVE_MMU */
5824 #define ELF_MAXPAGESIZE 1
5825 #endif /* !XCHAL_HAVE_MMU */
5826 #endif /* ELF_ARCH */
5828 #define elf_backend_can_gc_sections 1
5829 #define elf_backend_can_refcount 1
5830 #define elf_backend_plt_readonly 1
5831 #define elf_backend_got_header_size 4
5832 #define elf_backend_want_dynbss 0
5833 #define elf_backend_want_got_plt 1
5835 #define elf_info_to_howto elf_xtensa_info_to_howto_rela
5837 #define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
5838 #define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
5839 #define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
5840 #define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
5841 #define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
5842 #define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
5844 #define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
5845 #define elf_backend_check_relocs elf_xtensa_check_relocs
5846 #define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
5847 #define elf_backend_discard_info elf_xtensa_discard_info
5848 #define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
5849 #define elf_backend_final_write_processing elf_xtensa_final_write_processing
5850 #define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
5851 #define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
5852 #define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
5853 #define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
5854 #define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
5855 #define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
5856 #define elf_backend_hide_symbol elf_xtensa_hide_symbol
5857 #define elf_backend_modify_segment_map elf_xtensa_modify_segment_map
5858 #define elf_backend_object_p elf_xtensa_object_p
5859 #define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
5860 #define elf_backend_relocate_section elf_xtensa_relocate_section
5861 #define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
5862 #define elf_backend_special_sections elf_xtensa_special_sections
5864 #include "elf32-target.h"