* elfxx-target.h: Remove PTR cast.
[deliverable/binutils-gdb.git] / bfd / elf32-xtensa.c
CommitLineData
e0001a05
NC
1/* Xtensa-specific support for 32-bit ELF.
2 Copyright 2003 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23
24#ifdef ANSI_PROTOTYPES
25#include <stdarg.h>
26#else
27#include <varargs.h>
28#endif
29#include <strings.h>
30
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
34#include "elf/xtensa.h"
35#include "xtensa-isa.h"
36#include "xtensa-config.h"
37
38/* Main interface functions. */
39static void elf_xtensa_info_to_howto_rela
40 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
41static reloc_howto_type *elf_xtensa_reloc_type_lookup
42 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
43extern int xtensa_read_table_entries
44 PARAMS ((bfd *, asection *, property_table_entry **, const char *));
45static bfd_boolean elf_xtensa_check_relocs
46 PARAMS ((bfd *, struct bfd_link_info *, asection *,
47 const Elf_Internal_Rela *));
48static void elf_xtensa_hide_symbol
49 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
50static void elf_xtensa_copy_indirect_symbol
9c5bfbb7 51 PARAMS ((const struct elf_backend_data *, struct elf_link_hash_entry *,
e0001a05
NC
52 struct elf_link_hash_entry *));
53static asection *elf_xtensa_gc_mark_hook
54 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
55 struct elf_link_hash_entry *, Elf_Internal_Sym *));
56static bfd_boolean elf_xtensa_gc_sweep_hook
57 PARAMS ((bfd *, struct bfd_link_info *, asection *,
58 const Elf_Internal_Rela *));
59static bfd_boolean elf_xtensa_create_dynamic_sections
60 PARAMS ((bfd *, struct bfd_link_info *));
61static bfd_boolean elf_xtensa_adjust_dynamic_symbol
62 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
63static bfd_boolean elf_xtensa_size_dynamic_sections
64 PARAMS ((bfd *, struct bfd_link_info *));
65static bfd_boolean elf_xtensa_modify_segment_map
66 PARAMS ((bfd *));
67static bfd_boolean elf_xtensa_relocate_section
68 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
69 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
70static bfd_boolean elf_xtensa_relax_section
71 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *again));
72static bfd_boolean elf_xtensa_finish_dynamic_symbol
73 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
74 Elf_Internal_Sym *));
75static bfd_boolean elf_xtensa_finish_dynamic_sections
76 PARAMS ((bfd *, struct bfd_link_info *));
77static bfd_boolean elf_xtensa_merge_private_bfd_data
78 PARAMS ((bfd *, bfd *));
79static bfd_boolean elf_xtensa_set_private_flags
80 PARAMS ((bfd *, flagword));
81extern flagword elf_xtensa_get_private_bfd_flags
82 PARAMS ((bfd *));
83static bfd_boolean elf_xtensa_print_private_bfd_data
84 PARAMS ((bfd *, PTR));
85static bfd_boolean elf_xtensa_object_p
86 PARAMS ((bfd *));
87static void elf_xtensa_final_write_processing
88 PARAMS ((bfd *, bfd_boolean));
89static enum elf_reloc_type_class elf_xtensa_reloc_type_class
90 PARAMS ((const Elf_Internal_Rela *));
91static bfd_boolean elf_xtensa_discard_info
92 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *));
93static bfd_boolean elf_xtensa_ignore_discarded_relocs
94 PARAMS ((asection *));
95static bfd_boolean elf_xtensa_grok_prstatus
96 PARAMS ((bfd *, Elf_Internal_Note *));
97static bfd_boolean elf_xtensa_grok_psinfo
98 PARAMS ((bfd *, Elf_Internal_Note *));
99static bfd_boolean elf_xtensa_new_section_hook
100 PARAMS ((bfd *, asection *));
101
102
103/* Local helper functions. */
104
571b5725
BW
105static bfd_boolean xtensa_elf_dynamic_symbol_p
106 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
e0001a05
NC
107static int property_table_compare
108 PARAMS ((const PTR, const PTR));
109static bfd_boolean elf_xtensa_in_literal_pool
110 PARAMS ((property_table_entry *, int, bfd_vma));
111static void elf_xtensa_make_sym_local
112 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
113static bfd_boolean add_extra_plt_sections
114 PARAMS ((bfd *, int));
115static bfd_boolean elf_xtensa_fix_refcounts
116 PARAMS ((struct elf_link_hash_entry *, PTR));
117static bfd_boolean elf_xtensa_allocate_plt_size
118 PARAMS ((struct elf_link_hash_entry *, PTR));
119static bfd_boolean elf_xtensa_allocate_got_size
120 PARAMS ((struct elf_link_hash_entry *, PTR));
121static void elf_xtensa_allocate_local_got_size
122 PARAMS ((struct bfd_link_info *, asection *));
123static bfd_reloc_status_type elf_xtensa_do_reloc
124 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_vma, bfd_byte *,
125 bfd_vma, bfd_boolean, char **));
126static char * vsprint_msg
127 VPARAMS ((const char *, const char *, int, ...));
128static char *build_encoding_error_message
129 PARAMS ((xtensa_opcode, xtensa_encode_result));
130static bfd_reloc_status_type bfd_elf_xtensa_reloc
131 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
1049f94e 132static void do_fix_for_relocatable_link
e0001a05
NC
133 PARAMS ((Elf_Internal_Rela *, bfd *, asection *));
134static void do_fix_for_final_link
135 PARAMS ((Elf_Internal_Rela *, asection *, bfd_vma *));
e0001a05
NC
136static bfd_vma elf_xtensa_create_plt_entry
137 PARAMS ((bfd *, bfd *, unsigned));
138static int elf_xtensa_combine_prop_entries
e901de89 139 PARAMS ((bfd *, asection *, asection *));
e0001a05
NC
140static bfd_boolean elf_xtensa_discard_info_for_section
141 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *,
142 asection *));
143
144/* Local functions to handle Xtensa configurability. */
145
146static void init_call_opcodes
147 PARAMS ((void));
148static bfd_boolean is_indirect_call_opcode
149 PARAMS ((xtensa_opcode));
150static bfd_boolean is_direct_call_opcode
151 PARAMS ((xtensa_opcode));
152static bfd_boolean is_windowed_call_opcode
153 PARAMS ((xtensa_opcode));
154static xtensa_opcode get_l32r_opcode
155 PARAMS ((void));
156static bfd_vma l32r_offset
157 PARAMS ((bfd_vma, bfd_vma));
158static int get_relocation_opnd
159 PARAMS ((Elf_Internal_Rela *));
160static xtensa_opcode get_relocation_opcode
161 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *));
162static bfd_boolean is_l32r_relocation
163 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *));
164
165/* Functions for link-time code simplifications. */
166
167static bfd_reloc_status_type elf_xtensa_do_asm_simplify
168 PARAMS ((bfd_byte *, bfd_vma, bfd_vma));
169static bfd_reloc_status_type contract_asm_expansion
170 PARAMS ((bfd_byte *, bfd_vma, Elf_Internal_Rela *));
171static xtensa_opcode swap_callx_for_call_opcode
172 PARAMS ((xtensa_opcode));
173static xtensa_opcode get_expanded_call_opcode
174 PARAMS ((bfd_byte *, int));
175
176/* Access to internal relocations, section contents and symbols. */
177
178static Elf_Internal_Rela *retrieve_internal_relocs
179 PARAMS ((bfd *, asection *, bfd_boolean));
180static void pin_internal_relocs
181 PARAMS ((asection *, Elf_Internal_Rela *));
182static void release_internal_relocs
183 PARAMS ((asection *, Elf_Internal_Rela *));
184static bfd_byte *retrieve_contents
185 PARAMS ((bfd *, asection *, bfd_boolean));
186static void pin_contents
187 PARAMS ((asection *, bfd_byte *));
188static void release_contents
189 PARAMS ((asection *, bfd_byte *));
190static Elf_Internal_Sym *retrieve_local_syms
191 PARAMS ((bfd *));
192
193/* Miscellaneous utility functions. */
194
195static asection *elf_xtensa_get_plt_section
196 PARAMS ((bfd *, int));
197static asection *elf_xtensa_get_gotplt_section
198 PARAMS ((bfd *, int));
199static asection *get_elf_r_symndx_section
200 PARAMS ((bfd *, unsigned long));
201static struct elf_link_hash_entry *get_elf_r_symndx_hash_entry
202 PARAMS ((bfd *, unsigned long));
203static bfd_vma get_elf_r_symndx_offset
204 PARAMS ((bfd *, unsigned long));
205static bfd_boolean pcrel_reloc_fits
206 PARAMS ((xtensa_operand, bfd_vma, bfd_vma));
207static bfd_boolean xtensa_is_property_section
208 PARAMS ((asection *));
e901de89
BW
209static bfd_boolean xtensa_is_littable_section
210 PARAMS ((asection *));
e0001a05
NC
211static bfd_boolean is_literal_section
212 PARAMS ((asection *));
213static int internal_reloc_compare
214 PARAMS ((const PTR, const PTR));
215static bfd_boolean get_is_linkonce_section
216 PARAMS ((bfd *, asection *));
217extern char *xtensa_get_property_section_name
218 PARAMS ((bfd *, asection *, const char *));
219
220/* Other functions called directly by the linker. */
221
222typedef void (*deps_callback_t)
223 PARAMS ((asection *, bfd_vma, asection *, bfd_vma, PTR));
224extern bfd_boolean xtensa_callback_required_dependence
225 PARAMS ((bfd *, asection *, struct bfd_link_info *,
226 deps_callback_t, PTR));
227
228
229typedef struct xtensa_relax_info_struct xtensa_relax_info;
230
231
232/* Total count of PLT relocations seen during check_relocs.
233 The actual PLT code must be split into multiple sections and all
234 the sections have to be created before size_dynamic_sections,
235 where we figure out the exact number of PLT entries that will be
b536dc1e 236 needed. It is OK if this count is an overestimate, e.g., some
e0001a05
NC
237 relocations may be removed by GC. */
238
239static int plt_reloc_count = 0;
240
241
242/* When this is true, relocations may have been modified to refer to
243 symbols from other input files. The per-section list of "fix"
244 records needs to be checked when resolving relocations. */
245
246static bfd_boolean relaxing_section = FALSE;
247
248\f
249static reloc_howto_type elf_howto_table[] =
250{
251 HOWTO (R_XTENSA_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
252 bfd_elf_xtensa_reloc, "R_XTENSA_NONE",
253 FALSE, 0x00000000, 0x00000000, FALSE),
254 HOWTO (R_XTENSA_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
255 bfd_elf_xtensa_reloc, "R_XTENSA_32",
256 TRUE, 0xffffffff, 0xffffffff, FALSE),
257 /* Replace a 32-bit value with a value from the runtime linker (only
258 used by linker-generated stub functions). The r_addend value is
259 special: 1 means to substitute a pointer to the runtime linker's
260 dynamic resolver function; 2 means to substitute the link map for
261 the shared object. */
262 HOWTO (R_XTENSA_RTLD, 0, 2, 32, FALSE, 0, complain_overflow_dont,
263 NULL, "R_XTENSA_RTLD",
264 FALSE, 0x00000000, 0x00000000, FALSE),
265 HOWTO (R_XTENSA_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
266 bfd_elf_generic_reloc, "R_XTENSA_GLOB_DAT",
267 FALSE, 0xffffffff, 0xffffffff, FALSE),
268 HOWTO (R_XTENSA_JMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
269 bfd_elf_generic_reloc, "R_XTENSA_JMP_SLOT",
270 FALSE, 0xffffffff, 0xffffffff, FALSE),
271 HOWTO (R_XTENSA_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
272 bfd_elf_generic_reloc, "R_XTENSA_RELATIVE",
273 FALSE, 0xffffffff, 0xffffffff, FALSE),
274 HOWTO (R_XTENSA_PLT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
275 bfd_elf_xtensa_reloc, "R_XTENSA_PLT",
276 FALSE, 0xffffffff, 0xffffffff, FALSE),
277 EMPTY_HOWTO (7),
278 HOWTO (R_XTENSA_OP0, 0, 0, 0, TRUE, 0, complain_overflow_dont,
279 bfd_elf_xtensa_reloc, "R_XTENSA_OP0",
280 FALSE, 0x00000000, 0x00000000, TRUE),
281 HOWTO (R_XTENSA_OP1, 0, 0, 0, TRUE, 0, complain_overflow_dont,
282 bfd_elf_xtensa_reloc, "R_XTENSA_OP1",
283 FALSE, 0x00000000, 0x00000000, TRUE),
284 HOWTO (R_XTENSA_OP2, 0, 0, 0, TRUE, 0, complain_overflow_dont,
285 bfd_elf_xtensa_reloc, "R_XTENSA_OP2",
286 FALSE, 0x00000000, 0x00000000, TRUE),
287 /* Assembly auto-expansion. */
288 HOWTO (R_XTENSA_ASM_EXPAND, 0, 0, 0, TRUE, 0, complain_overflow_dont,
289 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_EXPAND",
290 FALSE, 0x00000000, 0x00000000, FALSE),
291 /* Relax assembly auto-expansion. */
292 HOWTO (R_XTENSA_ASM_SIMPLIFY, 0, 0, 0, TRUE, 0, complain_overflow_dont,
293 bfd_elf_xtensa_reloc, "R_XTENSA_ASM_SIMPLIFY",
294 FALSE, 0x00000000, 0x00000000, TRUE),
295 EMPTY_HOWTO (13),
296 EMPTY_HOWTO (14),
297 /* GNU extension to record C++ vtable hierarchy. */
298 HOWTO (R_XTENSA_GNU_VTINHERIT, 0, 2, 0, FALSE, 0, complain_overflow_dont,
299 NULL, "R_XTENSA_GNU_VTINHERIT",
300 FALSE, 0x00000000, 0x00000000, FALSE),
301 /* GNU extension to record C++ vtable member usage. */
302 HOWTO (R_XTENSA_GNU_VTENTRY, 0, 2, 0, FALSE, 0, complain_overflow_dont,
303 _bfd_elf_rel_vtable_reloc_fn, "R_XTENSA_GNU_VTENTRY",
304 FALSE, 0x00000000, 0x00000000, FALSE)
305};
306
307#ifdef DEBUG_GEN_RELOC
308#define TRACE(str) \
309 fprintf (stderr, "Xtensa bfd reloc lookup %d (%s)\n", code, str)
310#else
311#define TRACE(str)
312#endif
313
314static reloc_howto_type *
315elf_xtensa_reloc_type_lookup (abfd, code)
316 bfd *abfd ATTRIBUTE_UNUSED;
317 bfd_reloc_code_real_type code;
318{
319 switch (code)
320 {
321 case BFD_RELOC_NONE:
322 TRACE ("BFD_RELOC_NONE");
323 return &elf_howto_table[(unsigned) R_XTENSA_NONE ];
324
325 case BFD_RELOC_32:
326 TRACE ("BFD_RELOC_32");
327 return &elf_howto_table[(unsigned) R_XTENSA_32 ];
328
329 case BFD_RELOC_XTENSA_RTLD:
330 TRACE ("BFD_RELOC_XTENSA_RTLD");
331 return &elf_howto_table[(unsigned) R_XTENSA_RTLD ];
332
333 case BFD_RELOC_XTENSA_GLOB_DAT:
334 TRACE ("BFD_RELOC_XTENSA_GLOB_DAT");
335 return &elf_howto_table[(unsigned) R_XTENSA_GLOB_DAT ];
336
337 case BFD_RELOC_XTENSA_JMP_SLOT:
338 TRACE ("BFD_RELOC_XTENSA_JMP_SLOT");
339 return &elf_howto_table[(unsigned) R_XTENSA_JMP_SLOT ];
340
341 case BFD_RELOC_XTENSA_RELATIVE:
342 TRACE ("BFD_RELOC_XTENSA_RELATIVE");
343 return &elf_howto_table[(unsigned) R_XTENSA_RELATIVE ];
344
345 case BFD_RELOC_XTENSA_PLT:
346 TRACE ("BFD_RELOC_XTENSA_PLT");
347 return &elf_howto_table[(unsigned) R_XTENSA_PLT ];
348
349 case BFD_RELOC_XTENSA_OP0:
350 TRACE ("BFD_RELOC_XTENSA_OP0");
351 return &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
352
353 case BFD_RELOC_XTENSA_OP1:
354 TRACE ("BFD_RELOC_XTENSA_OP1");
355 return &elf_howto_table[(unsigned) R_XTENSA_OP1 ];
356
357 case BFD_RELOC_XTENSA_OP2:
358 TRACE ("BFD_RELOC_XTENSA_OP2");
359 return &elf_howto_table[(unsigned) R_XTENSA_OP2 ];
360
361 case BFD_RELOC_XTENSA_ASM_EXPAND:
362 TRACE ("BFD_RELOC_XTENSA_ASM_EXPAND");
363 return &elf_howto_table[(unsigned) R_XTENSA_ASM_EXPAND ];
364
365 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
366 TRACE ("BFD_RELOC_XTENSA_ASM_SIMPLIFY");
367 return &elf_howto_table[(unsigned) R_XTENSA_ASM_SIMPLIFY ];
368
369 case BFD_RELOC_VTABLE_INHERIT:
370 TRACE ("BFD_RELOC_VTABLE_INHERIT");
371 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTINHERIT ];
372
373 case BFD_RELOC_VTABLE_ENTRY:
374 TRACE ("BFD_RELOC_VTABLE_ENTRY");
375 return &elf_howto_table[(unsigned) R_XTENSA_GNU_VTENTRY ];
376
377 default:
378 break;
379 }
380
381 TRACE ("Unknown");
382 return NULL;
383}
384
385
386/* Given an ELF "rela" relocation, find the corresponding howto and record
387 it in the BFD internal arelent representation of the relocation. */
388
389static void
390elf_xtensa_info_to_howto_rela (abfd, cache_ptr, dst)
391 bfd *abfd ATTRIBUTE_UNUSED;
392 arelent *cache_ptr;
393 Elf_Internal_Rela *dst;
394{
395 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
396
397 BFD_ASSERT (r_type < (unsigned int) R_XTENSA_max);
398 cache_ptr->howto = &elf_howto_table[r_type];
399}
400
401\f
402/* Functions for the Xtensa ELF linker. */
403
404/* The name of the dynamic interpreter. This is put in the .interp
405 section. */
406
407#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so"
408
409/* The size in bytes of an entry in the procedure linkage table.
410 (This does _not_ include the space for the literals associated with
411 the PLT entry.) */
412
413#define PLT_ENTRY_SIZE 16
414
415/* For _really_ large PLTs, we may need to alternate between literals
416 and code to keep the literals within the 256K range of the L32R
417 instructions in the code. It's unlikely that anyone would ever need
418 such a big PLT, but an arbitrary limit on the PLT size would be bad.
419 Thus, we split the PLT into chunks. Since there's very little
420 overhead (2 extra literals) for each chunk, the chunk size is kept
421 small so that the code for handling multiple chunks get used and
422 tested regularly. With 254 entries, there are 1K of literals for
423 each chunk, and that seems like a nice round number. */
424
425#define PLT_ENTRIES_PER_CHUNK 254
426
427/* PLT entries are actually used as stub functions for lazy symbol
428 resolution. Once the symbol is resolved, the stub function is never
429 invoked. Note: the 32-byte frame size used here cannot be changed
430 without a corresponding change in the runtime linker. */
431
432static const bfd_byte elf_xtensa_be_plt_entry[PLT_ENTRY_SIZE] =
433{
434 0x6c, 0x10, 0x04, /* entry sp, 32 */
435 0x18, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
436 0x1a, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
437 0x1b, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
438 0x0a, 0x80, 0x00, /* jx a8 */
439 0 /* unused */
440};
441
442static const bfd_byte elf_xtensa_le_plt_entry[PLT_ENTRY_SIZE] =
443{
444 0x36, 0x41, 0x00, /* entry sp, 32 */
445 0x81, 0x00, 0x00, /* l32r a8, [got entry for rtld's resolver] */
446 0xa1, 0x00, 0x00, /* l32r a10, [got entry for rtld's link map] */
447 0xb1, 0x00, 0x00, /* l32r a11, [literal for reloc index] */
448 0xa0, 0x08, 0x00, /* jx a8 */
449 0 /* unused */
450};
451
571b5725
BW
452
453static inline bfd_boolean
454xtensa_elf_dynamic_symbol_p (h, info)
455 struct elf_link_hash_entry *h;
456 struct bfd_link_info *info;
457{
458 /* Check if we should do dynamic things to this symbol. The
459 "ignore_protected" argument need not be set, because Xtensa code
460 does not require special handling of STV_PROTECTED to make function
461 pointer comparisons work properly. The PLT addresses are never
462 used for function pointers. */
463
464 return _bfd_elf_dynamic_symbol_p (h, info, 0);
465}
466
e0001a05
NC
467\f
468static int
469property_table_compare (ap, bp)
470 const PTR ap;
471 const PTR bp;
472{
473 const property_table_entry *a = (const property_table_entry *) ap;
474 const property_table_entry *b = (const property_table_entry *) bp;
475
476 /* Check if one entry overlaps with the other; this shouldn't happen
477 except when searching for a match. */
478 if ((b->address >= a->address && b->address < (a->address + a->size))
479 || (a->address >= b->address && a->address < (b->address + b->size)))
480 return 0;
481
482 return (a->address - b->address);
483}
484
485
486/* Get the literal table or instruction table entries for the given
487 section. Sets TABLE_P and returns the number of entries. On error,
488 returns a negative value. */
489
490int
491xtensa_read_table_entries (abfd, section, table_p, sec_name)
492 bfd *abfd;
493 asection *section;
494 property_table_entry **table_p;
495 const char *sec_name;
496{
497 asection *table_section;
498 char *table_section_name;
499 bfd_size_type table_size = 0;
500 bfd_byte *table_data;
501 property_table_entry *blocks;
502 int block_count;
503 bfd_size_type num_records;
504 Elf_Internal_Rela *internal_relocs;
505
506 table_section_name =
507 xtensa_get_property_section_name (abfd, section, sec_name);
508 table_section = bfd_get_section_by_name (abfd, table_section_name);
509 if (table_section != NULL)
510 table_size = bfd_get_section_size_before_reloc (table_section);
511
512 if (table_size == 0)
513 {
514 *table_p = NULL;
515 return 0;
516 }
517
518 num_records = table_size / sizeof (property_table_entry);
519 table_data = retrieve_contents (abfd, table_section, TRUE);
520 blocks = (property_table_entry *)
521 bfd_malloc (num_records * sizeof (property_table_entry));
522 block_count = 0;
523
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)
528 {
529 unsigned i;
530
531 for (i = 0; i < table_section->reloc_count; i++)
532 {
533 Elf_Internal_Rela *rel = &internal_relocs[i];
534 unsigned long r_symndx;
535
536 if (ELF32_R_TYPE (rel->r_info) == R_XTENSA_NONE)
537 continue;
538
539 BFD_ASSERT (ELF32_R_TYPE (rel->r_info) == R_XTENSA_32);
540 r_symndx = ELF32_R_SYM (rel->r_info);
541
542 if (get_elf_r_symndx_section (abfd, r_symndx) == section)
543 {
544 bfd_vma sym_off = get_elf_r_symndx_offset (abfd, r_symndx);
545 blocks[block_count].address =
546 (section->vma + 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++;
551 }
552 }
553 }
554 else
555 {
556 /* No relocations. Presumably the file has been relocated
557 and the addresses are already in the table. */
558 bfd_vma off;
559
560 for (off = 0; off < table_size; off += sizeof (property_table_entry))
561 {
562 bfd_vma address = bfd_get_32 (abfd, table_data + off);
563
564 if (address >= section->vma
565 && address < ( section->vma + section->_raw_size))
566 {
567 blocks[block_count].address = address;
568 blocks[block_count].size =
569 bfd_get_32 (abfd, table_data + off + 4);
570 block_count++;
571 }
572 }
573 }
574
575 release_contents (table_section, table_data);
576 release_internal_relocs (table_section, internal_relocs);
577
578 if (block_count > 0)
579 {
580 /* Now sort them into address order for easy reference. */
581 qsort (blocks, block_count, sizeof (property_table_entry),
582 property_table_compare);
583 }
584
585 *table_p = blocks;
586 return block_count;
587}
588
589
590static bfd_boolean
591elf_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;
595{
596 property_table_entry entry;
597
598 if (lit_table_size == 0)
599 return FALSE;
600
601 entry.address = addr;
602 entry.size = 1;
603
604 if (bsearch (&entry, lit_table, lit_table_size,
605 sizeof (property_table_entry), property_table_compare))
606 return TRUE;
607
608 return FALSE;
609}
610
611\f
612/* Look through the relocs for a section during the first phase, and
613 calculate needed space in the dynamic reloc sections. */
614
615static bfd_boolean
616elf_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;
621{
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;
626 property_table_entry *lit_table;
627 int ltblsize;
628
1049f94e 629 if (info->relocatable)
e0001a05
NC
630 return TRUE;
631
632 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
633 sym_hashes = elf_sym_hashes (abfd);
634
635 ltblsize = xtensa_read_table_entries (abfd, sec, &lit_table,
636 XTENSA_LIT_SEC_NAME);
637 if (ltblsize < 0)
638 return FALSE;
639
640 rel_end = relocs + sec->reloc_count;
641 for (rel = relocs; rel < rel_end; rel++)
642 {
643 unsigned int r_type;
644 unsigned long r_symndx;
645 struct elf_link_hash_entry *h;
646
647 r_symndx = ELF32_R_SYM (rel->r_info);
648 r_type = ELF32_R_TYPE (rel->r_info);
649
650 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
651 {
652 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
653 bfd_archive_filename (abfd),
654 r_symndx);
655 return FALSE;
656 }
657
658 if (r_symndx < symtab_hdr->sh_info)
659 h = NULL;
660 else
661 {
662 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
663 while (h->root.type == bfd_link_hash_indirect
664 || h->root.type == bfd_link_hash_warning)
665 h = (struct elf_link_hash_entry *) h->root.u.i.link;
666 }
667
668 switch (r_type)
669 {
670 case R_XTENSA_32:
671 if (h == NULL)
672 goto local_literal;
673
674 if ((sec->flags & SEC_ALLOC) != 0)
675 {
676 if ((sec->flags & SEC_READONLY) != 0
677 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
678 sec->vma + rel->r_offset))
679 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
680
681 if (h->got.refcount <= 0)
682 h->got.refcount = 1;
683 else
684 h->got.refcount += 1;
685 }
686 break;
687
688 case R_XTENSA_PLT:
689 /* If this relocation is against a local symbol, then it's
690 exactly the same as a normal local GOT entry. */
691 if (h == NULL)
692 goto local_literal;
693
694 if ((sec->flags & SEC_ALLOC) != 0)
695 {
696 if ((sec->flags & SEC_READONLY) != 0
697 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
698 sec->vma + rel->r_offset))
699 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
700
701 if (h->plt.refcount <= 0)
702 {
703 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
704 h->plt.refcount = 1;
705 }
706 else
707 h->plt.refcount += 1;
708
709 /* Keep track of the total PLT relocation count even if we
710 don't yet know whether the dynamic sections will be
711 created. */
712 plt_reloc_count += 1;
713
714 if (elf_hash_table (info)->dynamic_sections_created)
715 {
716 if (!add_extra_plt_sections (elf_hash_table (info)->dynobj,
717 plt_reloc_count))
718 return FALSE;
719 }
720 }
721 break;
722
723 local_literal:
724 if ((sec->flags & SEC_ALLOC) != 0)
725 {
726 bfd_signed_vma *local_got_refcounts;
727
728 /* This is a global offset table entry for a local symbol. */
729 local_got_refcounts = elf_local_got_refcounts (abfd);
730 if (local_got_refcounts == NULL)
731 {
732 bfd_size_type size;
733
734 size = symtab_hdr->sh_info;
735 size *= sizeof (bfd_signed_vma);
736 local_got_refcounts = ((bfd_signed_vma *)
737 bfd_zalloc (abfd, size));
738 if (local_got_refcounts == NULL)
739 return FALSE;
740 elf_local_got_refcounts (abfd) = local_got_refcounts;
741 }
742 local_got_refcounts[r_symndx] += 1;
743
744 /* If the relocation is not inside the GOT, the DF_TEXTREL
745 flag needs to be set. */
746 if (info->shared
747 && (sec->flags & SEC_READONLY) != 0
748 && !elf_xtensa_in_literal_pool (lit_table, ltblsize,
749 sec->vma + rel->r_offset))
750 info->flags |= DF_TEXTREL;
751 }
752 break;
753
754 case R_XTENSA_OP0:
755 case R_XTENSA_OP1:
756 case R_XTENSA_OP2:
757 case R_XTENSA_ASM_EXPAND:
758 case R_XTENSA_ASM_SIMPLIFY:
759 /* Nothing to do for these. */
760 break;
761
762 case R_XTENSA_GNU_VTINHERIT:
763 /* This relocation describes the C++ object vtable hierarchy.
764 Reconstruct it for later use during GC. */
765 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
766 return FALSE;
767 break;
768
769 case R_XTENSA_GNU_VTENTRY:
770 /* This relocation describes which C++ vtable entries are actually
771 used. Record for later use during GC. */
772 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
773 return FALSE;
774 break;
775
776 default:
777 break;
778 }
779 }
780
781 free (lit_table);
782 return TRUE;
783}
784
785
786static void
787elf_xtensa_hide_symbol (info, h, force_local)
788 struct bfd_link_info *info;
789 struct elf_link_hash_entry *h;
790 bfd_boolean force_local;
791{
792 /* For a shared link, move the plt refcount to the got refcount to leave
793 space for RELATIVE relocs. */
794 elf_xtensa_make_sym_local (info, h);
795
796 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
797}
798
799
800static void
801elf_xtensa_copy_indirect_symbol (bed, dir, ind)
9c5bfbb7 802 const struct elf_backend_data *bed;
e0001a05
NC
803 struct elf_link_hash_entry *dir, *ind;
804{
805 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
806
807 /* The standard function doesn't copy the NEEDS_PLT flag. */
808 dir->elf_link_hash_flags |=
809 (ind->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT);
810}
811
812
813/* Return the section that should be marked against GC for a given
814 relocation. */
815
816static asection *
817elf_xtensa_gc_mark_hook (sec, info, rel, h, sym)
818 asection *sec;
819 struct bfd_link_info *info ATTRIBUTE_UNUSED;
820 Elf_Internal_Rela *rel;
821 struct elf_link_hash_entry *h;
822 Elf_Internal_Sym *sym;
823{
824 if (h != NULL)
825 {
826 switch (ELF32_R_TYPE (rel->r_info))
827 {
828 case R_XTENSA_GNU_VTINHERIT:
829 case R_XTENSA_GNU_VTENTRY:
830 break;
831
832 default:
833 switch (h->root.type)
834 {
835 case bfd_link_hash_defined:
836 case bfd_link_hash_defweak:
837 return h->root.u.def.section;
838
839 case bfd_link_hash_common:
840 return h->root.u.c.p->section;
841
842 default:
843 break;
844 }
845 }
846 }
847 else
848 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
849
850 return NULL;
851}
852
853/* Update the GOT & PLT entry reference counts
854 for the section being removed. */
855
856static bfd_boolean
857elf_xtensa_gc_sweep_hook (abfd, info, sec, relocs)
858 bfd *abfd;
859 struct bfd_link_info *info ATTRIBUTE_UNUSED;
860 asection *sec;
861 const Elf_Internal_Rela *relocs;
862{
863 Elf_Internal_Shdr *symtab_hdr;
864 struct elf_link_hash_entry **sym_hashes;
865 bfd_signed_vma *local_got_refcounts;
866 const Elf_Internal_Rela *rel, *relend;
867
868 if ((sec->flags & SEC_ALLOC) == 0)
869 return TRUE;
870
871 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
872 sym_hashes = elf_sym_hashes (abfd);
873 local_got_refcounts = elf_local_got_refcounts (abfd);
874
875 relend = relocs + sec->reloc_count;
876 for (rel = relocs; rel < relend; rel++)
877 {
878 unsigned long r_symndx;
879 unsigned int r_type;
880 struct elf_link_hash_entry *h = NULL;
881
882 r_symndx = ELF32_R_SYM (rel->r_info);
883 if (r_symndx >= symtab_hdr->sh_info)
884 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
885
886 r_type = ELF32_R_TYPE (rel->r_info);
887 switch (r_type)
888 {
889 case R_XTENSA_32:
890 if (h == NULL)
891 goto local_literal;
892 if (h->got.refcount > 0)
893 h->got.refcount--;
894 break;
895
896 case R_XTENSA_PLT:
897 if (h == NULL)
898 goto local_literal;
899 if (h->plt.refcount > 0)
900 h->plt.refcount--;
901 break;
902
903 local_literal:
904 if (local_got_refcounts[r_symndx] > 0)
905 local_got_refcounts[r_symndx] -= 1;
906 break;
907
908 default:
909 break;
910 }
911 }
912
913 return TRUE;
914}
915
916
917/* Create all the dynamic sections. */
918
919static bfd_boolean
920elf_xtensa_create_dynamic_sections (dynobj, info)
921 bfd *dynobj;
922 struct bfd_link_info *info;
923{
e901de89 924 flagword flags, noalloc_flags;
e0001a05
NC
925 asection *s;
926
927 /* First do all the standard stuff. */
928 if (! _bfd_elf_create_dynamic_sections (dynobj, info))
929 return FALSE;
930
931 /* Create any extra PLT sections in case check_relocs has already
932 been called on all the non-dynamic input files. */
933 if (!add_extra_plt_sections (dynobj, plt_reloc_count))
934 return FALSE;
935
e901de89
BW
936 noalloc_flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
937 | SEC_LINKER_CREATED | SEC_READONLY);
938 flags = noalloc_flags | SEC_ALLOC | SEC_LOAD;
e0001a05
NC
939
940 /* Mark the ".got.plt" section READONLY. */
941 s = bfd_get_section_by_name (dynobj, ".got.plt");
942 if (s == NULL
943 || ! bfd_set_section_flags (dynobj, s, flags))
944 return FALSE;
945
946 /* Create ".rela.got". */
947 s = bfd_make_section (dynobj, ".rela.got");
948 if (s == NULL
949 || ! bfd_set_section_flags (dynobj, s, flags)
950 || ! bfd_set_section_alignment (dynobj, s, 2))
951 return FALSE;
952
e901de89
BW
953 /* Create ".got.loc" (literal tables for use by dynamic linker). */
954 s = bfd_make_section (dynobj, ".got.loc");
955 if (s == NULL
956 || ! bfd_set_section_flags (dynobj, s, flags)
957 || ! bfd_set_section_alignment (dynobj, s, 2))
958 return FALSE;
959
e0001a05
NC
960 /* Create ".xt.lit.plt" (literal table for ".got.plt*"). */
961 s = bfd_make_section (dynobj, ".xt.lit.plt");
962 if (s == NULL
e901de89 963 || ! bfd_set_section_flags (dynobj, s, noalloc_flags)
e0001a05
NC
964 || ! bfd_set_section_alignment (dynobj, s, 2))
965 return FALSE;
966
967 return TRUE;
968}
969
970
971static bfd_boolean
972add_extra_plt_sections (dynobj, count)
973 bfd *dynobj;
974 int count;
975{
976 int chunk;
977
978 /* Iterate over all chunks except 0 which uses the standard ".plt" and
979 ".got.plt" sections. */
980 for (chunk = count / PLT_ENTRIES_PER_CHUNK; chunk > 0; chunk--)
981 {
982 char *sname;
983 flagword flags;
984 asection *s;
985
986 /* Stop when we find a section has already been created. */
987 if (elf_xtensa_get_plt_section (dynobj, chunk))
988 break;
989
990 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
991 | SEC_LINKER_CREATED | SEC_READONLY);
992
993 sname = (char *) bfd_malloc (10);
994 sprintf (sname, ".plt.%u", chunk);
995 s = bfd_make_section (dynobj, sname);
996 if (s == NULL
997 || ! bfd_set_section_flags (dynobj, s, flags | SEC_CODE)
998 || ! bfd_set_section_alignment (dynobj, s, 2))
999 return FALSE;
1000
1001 sname = (char *) bfd_malloc (14);
1002 sprintf (sname, ".got.plt.%u", chunk);
1003 s = bfd_make_section (dynobj, sname);
1004 if (s == NULL
1005 || ! bfd_set_section_flags (dynobj, s, flags)
1006 || ! bfd_set_section_alignment (dynobj, s, 2))
1007 return FALSE;
1008 }
1009
1010 return TRUE;
1011}
1012
1013
1014/* Adjust a symbol defined by a dynamic object and referenced by a
1015 regular object. The current definition is in some section of the
1016 dynamic object, but we're not including those sections. We have to
1017 change the definition to something the rest of the link can
1018 understand. */
1019
1020static bfd_boolean
1021elf_xtensa_adjust_dynamic_symbol (info, h)
1022 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1023 struct elf_link_hash_entry *h;
1024{
1025 /* If this is a weak symbol, and there is a real definition, the
1026 processor independent code will have arranged for us to see the
1027 real definition first, and we can just use the same value. */
1028 if (h->weakdef != NULL)
1029 {
1030 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1031 || h->weakdef->root.type == bfd_link_hash_defweak);
1032 h->root.u.def.section = h->weakdef->root.u.def.section;
1033 h->root.u.def.value = h->weakdef->root.u.def.value;
1034 return TRUE;
1035 }
1036
1037 /* This is a reference to a symbol defined by a dynamic object. The
1038 reference must go through the GOT, so there's no need for COPY relocs,
1039 .dynbss, etc. */
1040
1041 return TRUE;
1042}
1043
1044
1045static void
1046elf_xtensa_make_sym_local (info, h)
1047 struct bfd_link_info *info;
1048 struct elf_link_hash_entry *h;
1049{
1050 if (info->shared)
1051 {
1052 if (h->plt.refcount > 0)
1053 {
1054 /* Will use RELATIVE relocs instead of JMP_SLOT relocs. */
1055 if (h->got.refcount < 0)
1056 h->got.refcount = 0;
1057 h->got.refcount += h->plt.refcount;
1058 h->plt.refcount = 0;
1059 }
1060 }
1061 else
1062 {
1063 /* Don't need any dynamic relocations at all. */
1064 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1065 h->plt.refcount = 0;
1066 h->got.refcount = 0;
1067 }
1068}
1069
1070
1071static bfd_boolean
1072elf_xtensa_fix_refcounts (h, arg)
1073 struct elf_link_hash_entry *h;
1074 PTR arg;
1075{
1076 struct bfd_link_info *info = (struct bfd_link_info *) arg;
1077
1078 if (h->root.type == bfd_link_hash_warning)
1079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1080
571b5725 1081 if (! xtensa_elf_dynamic_symbol_p (h, info))
e0001a05
NC
1082 elf_xtensa_make_sym_local (info, h);
1083
1084 /* If the symbol has a relocation outside the GOT, set the
1085 DF_TEXTREL flag. */
1086 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) != 0)
1087 info->flags |= DF_TEXTREL;
1088
1089 return TRUE;
1090}
1091
1092
1093static bfd_boolean
1094elf_xtensa_allocate_plt_size (h, arg)
1095 struct elf_link_hash_entry *h;
1096 PTR arg;
1097{
1098 asection *srelplt = (asection *) arg;
1099
1100 if (h->root.type == bfd_link_hash_warning)
1101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1102
1103 if (h->plt.refcount > 0)
1104 srelplt->_raw_size += (h->plt.refcount * sizeof (Elf32_External_Rela));
1105
1106 return TRUE;
1107}
1108
1109
1110static bfd_boolean
1111elf_xtensa_allocate_got_size (h, arg)
1112 struct elf_link_hash_entry *h;
1113 PTR arg;
1114{
1115 asection *srelgot = (asection *) arg;
1116
1117 if (h->root.type == bfd_link_hash_warning)
1118 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1119
1120 if (h->got.refcount > 0)
1121 srelgot->_raw_size += (h->got.refcount * sizeof (Elf32_External_Rela));
1122
1123 return TRUE;
1124}
1125
1126
1127static void
1128elf_xtensa_allocate_local_got_size (info, srelgot)
1129 struct bfd_link_info *info;
1130 asection *srelgot;
1131{
1132 bfd *i;
1133
1134 for (i = info->input_bfds; i; i = i->link_next)
1135 {
1136 bfd_signed_vma *local_got_refcounts;
1137 bfd_size_type j, cnt;
1138 Elf_Internal_Shdr *symtab_hdr;
1139
1140 local_got_refcounts = elf_local_got_refcounts (i);
1141 if (!local_got_refcounts)
1142 continue;
1143
1144 symtab_hdr = &elf_tdata (i)->symtab_hdr;
1145 cnt = symtab_hdr->sh_info;
1146
1147 for (j = 0; j < cnt; ++j)
1148 {
1149 if (local_got_refcounts[j] > 0)
1150 srelgot->_raw_size += (local_got_refcounts[j]
1151 * sizeof (Elf32_External_Rela));
1152 }
1153 }
1154}
1155
1156
1157/* Set the sizes of the dynamic sections. */
1158
1159static bfd_boolean
1160elf_xtensa_size_dynamic_sections (output_bfd, info)
1161 bfd *output_bfd ATTRIBUTE_UNUSED;
1162 struct bfd_link_info *info;
1163{
e901de89
BW
1164 bfd *dynobj, *abfd;
1165 asection *s, *srelplt, *splt, *sgotplt, *srelgot, *spltlittbl, *sgotloc;
e0001a05
NC
1166 bfd_boolean relplt, relgot;
1167 int plt_entries, plt_chunks, chunk;
1168
1169 plt_entries = 0;
1170 plt_chunks = 0;
1171 srelgot = 0;
1172
1173 dynobj = elf_hash_table (info)->dynobj;
1174 if (dynobj == NULL)
1175 abort ();
1176
1177 if (elf_hash_table (info)->dynamic_sections_created)
1178 {
1179 /* Set the contents of the .interp section to the interpreter. */
1180 if (! info->shared)
1181 {
1182 s = bfd_get_section_by_name (dynobj, ".interp");
1183 if (s == NULL)
1184 abort ();
1185 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1186 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1187 }
1188
1189 /* Allocate room for one word in ".got". */
1190 s = bfd_get_section_by_name (dynobj, ".got");
1191 if (s == NULL)
1192 abort ();
1193 s->_raw_size = 4;
1194
1195 /* Adjust refcounts for symbols that we now know are not "dynamic". */
1196 elf_link_hash_traverse (elf_hash_table (info),
1197 elf_xtensa_fix_refcounts,
1198 (PTR) info);
1199
1200 /* Allocate space in ".rela.got" for literals that reference
1201 global symbols. */
1202 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1203 if (srelgot == NULL)
1204 abort ();
1205 elf_link_hash_traverse (elf_hash_table (info),
1206 elf_xtensa_allocate_got_size,
1207 (PTR) srelgot);
1208
1209 /* If we are generating a shared object, we also need space in
1210 ".rela.got" for R_XTENSA_RELATIVE relocs for literals that
1211 reference local symbols. */
1212 if (info->shared)
1213 elf_xtensa_allocate_local_got_size (info, srelgot);
1214
1215 /* Allocate space in ".rela.plt" for literals that have PLT entries. */
1216 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1217 if (srelplt == NULL)
1218 abort ();
1219 elf_link_hash_traverse (elf_hash_table (info),
1220 elf_xtensa_allocate_plt_size,
1221 (PTR) srelplt);
1222
1223 /* Allocate space in ".plt" to match the size of ".rela.plt". For
1224 each PLT entry, we need the PLT code plus a 4-byte literal.
1225 For each chunk of ".plt", we also need two more 4-byte
1226 literals, two corresponding entries in ".rela.got", and an
1227 8-byte entry in ".xt.lit.plt". */
1228 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
1229 if (spltlittbl == NULL)
1230 abort ();
1231
1232 plt_entries = srelplt->_raw_size / sizeof (Elf32_External_Rela);
1233 plt_chunks =
1234 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
1235
1236 /* Iterate over all the PLT chunks, including any extra sections
1237 created earlier because the initial count of PLT relocations
1238 was an overestimate. */
1239 for (chunk = 0;
1240 (splt = elf_xtensa_get_plt_section (dynobj, chunk)) != NULL;
1241 chunk++)
1242 {
1243 int chunk_entries;
1244
1245 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1246 if (sgotplt == NULL)
1247 abort ();
1248
1249 if (chunk < plt_chunks - 1)
1250 chunk_entries = PLT_ENTRIES_PER_CHUNK;
1251 else if (chunk == plt_chunks - 1)
1252 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
1253 else
1254 chunk_entries = 0;
1255
1256 if (chunk_entries != 0)
1257 {
1258 sgotplt->_raw_size = 4 * (chunk_entries + 2);
1259 splt->_raw_size = PLT_ENTRY_SIZE * chunk_entries;
1260 srelgot->_raw_size += 2 * sizeof (Elf32_External_Rela);
1261 spltlittbl->_raw_size += 8;
1262 }
1263 else
1264 {
1265 sgotplt->_raw_size = 0;
1266 splt->_raw_size = 0;
1267 }
1268 }
e901de89
BW
1269
1270 /* Allocate space in ".got.loc" to match the total size of all the
1271 literal tables. */
1272 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
1273 if (sgotloc == NULL)
1274 abort ();
1275 sgotloc->_raw_size = spltlittbl->_raw_size;
1276 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1277 {
1278 if (abfd->flags & DYNAMIC)
1279 continue;
1280 for (s = abfd->sections; s != NULL; s = s->next)
1281 {
b536dc1e
BW
1282 if (! elf_discarded_section (s)
1283 && xtensa_is_littable_section (s)
1284 && s != spltlittbl)
e901de89
BW
1285 sgotloc->_raw_size += s->_raw_size;
1286 }
1287 }
e0001a05
NC
1288 }
1289
1290 /* Allocate memory for dynamic sections. */
1291 relplt = FALSE;
1292 relgot = FALSE;
1293 for (s = dynobj->sections; s != NULL; s = s->next)
1294 {
1295 const char *name;
1296 bfd_boolean strip;
1297
1298 if ((s->flags & SEC_LINKER_CREATED) == 0)
1299 continue;
1300
1301 /* It's OK to base decisions on the section name, because none
1302 of the dynobj section names depend upon the input files. */
1303 name = bfd_get_section_name (dynobj, s);
1304
1305 strip = FALSE;
1306
1307 if (strncmp (name, ".rela", 5) == 0)
1308 {
1309 if (strcmp (name, ".rela.plt") == 0)
1310 relplt = TRUE;
1311 else if (strcmp (name, ".rela.got") == 0)
1312 relgot = TRUE;
1313
1314 /* We use the reloc_count field as a counter if we need
1315 to copy relocs into the output file. */
1316 s->reloc_count = 0;
1317 }
1318 else if (strncmp (name, ".plt.", 5) == 0
1319 || strncmp (name, ".got.plt.", 9) == 0)
1320 {
1321 if (s->_raw_size == 0)
1322 {
1323 /* If we don't need this section, strip it from the output
1324 file. We must create the ".plt*" and ".got.plt*"
1325 sections in create_dynamic_sections and/or check_relocs
1326 based on a conservative estimate of the PLT relocation
1327 count, because the sections must be created before the
1328 linker maps input sections to output sections. The
1329 linker does that before size_dynamic_sections, where we
1330 compute the exact size of the PLT, so there may be more
1331 of these sections than are actually needed. */
1332 strip = TRUE;
1333 }
1334 }
1335 else if (strcmp (name, ".got") != 0
1336 && strcmp (name, ".plt") != 0
1337 && strcmp (name, ".got.plt") != 0
e901de89
BW
1338 && strcmp (name, ".xt.lit.plt") != 0
1339 && strcmp (name, ".got.loc") != 0)
e0001a05
NC
1340 {
1341 /* It's not one of our sections, so don't allocate space. */
1342 continue;
1343 }
1344
1345 if (strip)
1346 _bfd_strip_section_from_output (info, s);
1347 else
1348 {
1349 /* Allocate memory for the section contents. */
1350 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1351 if (s->contents == NULL && s->_raw_size != 0)
1352 return FALSE;
1353 }
1354 }
1355
1356 if (elf_hash_table (info)->dynamic_sections_created)
1357 {
1358 /* Add the special XTENSA_RTLD relocations now. The offsets won't be
1359 known until finish_dynamic_sections, but we need to get the relocs
1360 in place before they are sorted. */
1361 if (srelgot == NULL)
1362 abort ();
1363 for (chunk = 0; chunk < plt_chunks; chunk++)
1364 {
1365 Elf_Internal_Rela irela;
1366 bfd_byte *loc;
1367
1368 irela.r_offset = 0;
1369 irela.r_info = ELF32_R_INFO (0, R_XTENSA_RTLD);
1370 irela.r_addend = 0;
1371
1372 loc = (srelgot->contents
1373 + srelgot->reloc_count * sizeof (Elf32_External_Rela));
1374 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
1375 bfd_elf32_swap_reloca_out (output_bfd, &irela,
1376 loc + sizeof (Elf32_External_Rela));
1377 srelgot->reloc_count += 2;
1378 }
1379
1380 /* Add some entries to the .dynamic section. We fill in the
1381 values later, in elf_xtensa_finish_dynamic_sections, but we
1382 must add the entries now so that we get the correct size for
1383 the .dynamic section. The DT_DEBUG entry is filled in by the
1384 dynamic linker and used by the debugger. */
1385#define add_dynamic_entry(TAG, VAL) \
1386 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1387
1388 if (! info->shared)
1389 {
1390 if (!add_dynamic_entry (DT_DEBUG, 0))
1391 return FALSE;
1392 }
1393
1394 if (relplt)
1395 {
1396 if (!add_dynamic_entry (DT_PLTGOT, 0)
1397 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1398 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1399 || !add_dynamic_entry (DT_JMPREL, 0))
1400 return FALSE;
1401 }
1402
1403 if (relgot)
1404 {
1405 if (!add_dynamic_entry (DT_RELA, 0)
1406 || !add_dynamic_entry (DT_RELASZ, 0)
1407 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1408 return FALSE;
1409 }
1410
1411 if ((info->flags & DF_TEXTREL) != 0)
1412 {
1413 if (!add_dynamic_entry (DT_TEXTREL, 0))
1414 return FALSE;
1415 }
1416
1417 if (!add_dynamic_entry (DT_XTENSA_GOT_LOC_OFF, 0)
1418 || !add_dynamic_entry (DT_XTENSA_GOT_LOC_SZ, 0))
1419 return FALSE;
1420 }
1421#undef add_dynamic_entry
1422
1423 return TRUE;
1424}
1425
1426\f
1427/* Remove any PT_LOAD segments with no allocated sections. Prior to
1428 binutils 2.13, this function used to remove the non-SEC_ALLOC
1429 sections from PT_LOAD segments, but that task has now been moved
1430 into elf.c. We still need this function to remove any empty
1431 segments that result, but there's nothing Xtensa-specific about
1432 this and it probably ought to be moved into elf.c as well. */
1433
1434static bfd_boolean
1435elf_xtensa_modify_segment_map (abfd)
1436 bfd *abfd;
1437{
1438 struct elf_segment_map **m_p;
1439
1440 m_p = &elf_tdata (abfd)->segment_map;
1441 while (*m_p != NULL)
1442 {
1443 if ((*m_p)->p_type == PT_LOAD && (*m_p)->count == 0)
1444 *m_p = (*m_p)->next;
1445 else
1446 m_p = &(*m_p)->next;
1447 }
1448 return TRUE;
1449}
1450
1451\f
1452/* Perform the specified relocation. The instruction at (contents + address)
1453 is modified to set one operand to represent the value in "relocation". The
1454 operand position is determined by the relocation type recorded in the
1455 howto. */
1456
1457#define CALL_SEGMENT_BITS (30)
1458#define CALL_SEGMENT_SIZE (1<<CALL_SEGMENT_BITS)
1459
1460static bfd_reloc_status_type
1461elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1462 contents, address, is_weak_undef, error_message)
1463 reloc_howto_type *howto;
1464 bfd *abfd;
1465 asection *input_section;
1466 bfd_vma relocation;
1467 bfd_byte *contents;
1468 bfd_vma address;
1469 bfd_boolean is_weak_undef;
1470 char **error_message;
1471{
1472 xtensa_opcode opcode;
1473 xtensa_operand operand;
1474 xtensa_encode_result encode_result;
1475 xtensa_isa isa = xtensa_default_isa;
1476 xtensa_insnbuf ibuff;
1477 bfd_vma self_address;
1478 int opnd;
1479 uint32 newval;
1480
1481 switch (howto->type)
1482 {
1483 case R_XTENSA_NONE:
1484 return bfd_reloc_ok;
1485
1486 case R_XTENSA_ASM_EXPAND:
1487 if (!is_weak_undef)
1488 {
1489 /* Check for windowed CALL across a 1GB boundary. */
1490 xtensa_opcode opcode =
1491 get_expanded_call_opcode (contents + address,
1492 input_section->_raw_size - address);
1493 if (is_windowed_call_opcode (opcode))
1494 {
1495 self_address = (input_section->output_section->vma
1496 + input_section->output_offset
1497 + address);
1498 if ((self_address >> CALL_SEGMENT_BITS) !=
1499 (relocation >> CALL_SEGMENT_BITS))
1500 {
1501 *error_message = "windowed longcall crosses 1GB boundary; "
1502 "return may fail";
1503 return bfd_reloc_dangerous;
1504 }
1505 }
1506 }
1507 return bfd_reloc_ok;
1508
1509 case R_XTENSA_ASM_SIMPLIFY:
1510 {
1511 /* Convert the L32R/CALLX to CALL. */
1512 bfd_reloc_status_type retval =
1513 elf_xtensa_do_asm_simplify (contents, address,
1514 input_section->_raw_size);
1515 if (retval != bfd_reloc_ok)
1516 return retval;
1517
1518 /* The CALL needs to be relocated. Continue below for that part. */
1519 address += 3;
1520 howto = &elf_howto_table[(unsigned) R_XTENSA_OP0 ];
1521 }
1522 break;
1523
1524 case R_XTENSA_32:
1525 case R_XTENSA_PLT:
1526 {
1527 bfd_vma x;
1528 x = bfd_get_32 (abfd, contents + address);
1529 x = x + relocation;
1530 bfd_put_32 (abfd, x, contents + address);
1531 }
1532 return bfd_reloc_ok;
1533 }
1534
1535 /* Read the instruction into a buffer and decode the opcode. */
1536 ibuff = xtensa_insnbuf_alloc (isa);
1537 xtensa_insnbuf_from_chars (isa, ibuff, contents + address);
1538 opcode = xtensa_decode_insn (isa, ibuff);
1539
1540 /* Determine which operand is being relocated. */
1541 if (opcode == XTENSA_UNDEFINED)
1542 {
1543 *error_message = "cannot decode instruction";
1544 return bfd_reloc_dangerous;
1545 }
1546
1547 if (howto->type < R_XTENSA_OP0 || howto->type > R_XTENSA_OP2)
1548 {
1549 *error_message = "unexpected relocation";
1550 return bfd_reloc_dangerous;
1551 }
1552
1553 opnd = howto->type - R_XTENSA_OP0;
1554
1555 /* Calculate the PC address for this instruction. */
1556 if (!howto->pc_relative)
1557 {
1558 *error_message = "expected PC-relative relocation";
1559 return bfd_reloc_dangerous;
1560 }
1561
1562 self_address = (input_section->output_section->vma
1563 + input_section->output_offset
1564 + address);
1565
1566 /* Apply the relocation. */
1567 operand = xtensa_get_operand (isa, opcode, opnd);
1568 newval = xtensa_operand_do_reloc (operand, relocation, self_address);
1569 encode_result = xtensa_operand_encode (operand, &newval);
1570 xtensa_operand_set_field (operand, ibuff, newval);
1571
1572 /* Write the modified instruction back out of the buffer. */
1573 xtensa_insnbuf_to_chars (isa, ibuff, contents + address);
1574 free (ibuff);
1575
1576 if (encode_result != xtensa_encode_result_ok)
1577 {
1578 char *message = build_encoding_error_message (opcode, encode_result);
1579 *error_message = message;
1580 return bfd_reloc_dangerous;
1581 }
1582
1583 /* Final check for call. */
1584 if (is_direct_call_opcode (opcode)
1585 && is_windowed_call_opcode (opcode))
1586 {
1587 if ((self_address >> CALL_SEGMENT_BITS) !=
1588 (relocation >> CALL_SEGMENT_BITS))
1589 {
1590 *error_message = "windowed call crosses 1GB boundary; "
1591 "return may fail";
1592 return bfd_reloc_dangerous;
1593 }
1594 }
1595
1596 return bfd_reloc_ok;
1597}
1598
1599
1600static char *
1601vsprint_msg VPARAMS ((const char *origmsg, const char *fmt, int arglen, ...))
1602{
1603 /* To reduce the size of the memory leak,
1604 we only use a single message buffer. */
1605 static bfd_size_type alloc_size = 0;
1606 static char *message = NULL;
1607 bfd_size_type orig_len, len = 0;
1608 bfd_boolean is_append;
1609
1610 VA_OPEN (ap, arglen);
1611 VA_FIXEDARG (ap, const char *, origmsg);
1612
1613 is_append = (origmsg == message);
1614
1615 orig_len = strlen (origmsg);
1616 len = orig_len + strlen (fmt) + arglen + 20;
1617 if (len > alloc_size)
1618 {
1619 message = (char *) bfd_realloc (message, len);
1620 alloc_size = len;
1621 }
1622 if (!is_append)
1623 memcpy (message, origmsg, orig_len);
1624 vsprintf (message + orig_len, fmt, ap);
1625 VA_CLOSE (ap);
1626 return message;
1627}
1628
1629
1630static char *
1631build_encoding_error_message (opcode, encode_result)
1632 xtensa_opcode opcode;
1633 xtensa_encode_result encode_result;
1634{
1635 const char *opname = xtensa_opcode_name (xtensa_default_isa, opcode);
1636 const char *msg = NULL;
1637
1638 switch (encode_result)
1639 {
1640 case xtensa_encode_result_ok:
1641 msg = "unexpected valid encoding";
1642 break;
1643 case xtensa_encode_result_align:
1644 msg = "misaligned encoding";
1645 break;
1646 case xtensa_encode_result_not_in_table:
1647 msg = "encoding not in lookup table";
1648 break;
1649 case xtensa_encode_result_too_low:
1650 msg = "encoding out of range: too low";
1651 break;
1652 case xtensa_encode_result_too_high:
1653 msg = "encoding out of range: too high";
1654 break;
1655 case xtensa_encode_result_not_ok:
1656 default:
1657 msg = "could not encode";
1658 break;
1659 }
1660
1661 if (is_direct_call_opcode (opcode)
1662 && (encode_result == xtensa_encode_result_too_low
1663 || encode_result == xtensa_encode_result_too_high))
1664
1665 msg = "direct call out of range";
1666
1667 else if (opcode == get_l32r_opcode ())
1668 {
1669 /* L32Rs have the strange interaction with encoding in that they
1670 have an unsigned immediate field, so libisa returns "too high"
1671 when the absolute value is out of range and never returns "too
1672 low", but I leave the "too low" message in case anything
1673 changes. */
1674 if (encode_result == xtensa_encode_result_too_low)
1675 msg = "literal out of range";
1676 else if (encode_result == xtensa_encode_result_too_high)
1677 msg = "literal placed after use";
1678 }
1679
1680 return vsprint_msg (opname, ": %s", strlen (msg) + 2, msg);
1681}
1682
1683
1684/* This function is registered as the "special_function" in the
1685 Xtensa howto for handling simplify operations.
1686 bfd_perform_relocation / bfd_install_relocation use it to
1687 perform (install) the specified relocation. Since this replaces the code
1688 in bfd_perform_relocation, it is basically an Xtensa-specific,
1689 stripped-down version of bfd_perform_relocation. */
1690
1691static bfd_reloc_status_type
1692bfd_elf_xtensa_reloc (abfd, reloc_entry, symbol, data, input_section,
1693 output_bfd, error_message)
1694 bfd *abfd;
1695 arelent *reloc_entry;
1696 asymbol *symbol;
1697 PTR data;
1698 asection *input_section;
1699 bfd *output_bfd;
1700 char **error_message;
1701{
1702 bfd_vma relocation;
1703 bfd_reloc_status_type flag;
1704 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1705 bfd_vma output_base = 0;
1706 reloc_howto_type *howto = reloc_entry->howto;
1707 asection *reloc_target_output_section;
1708 bfd_boolean is_weak_undef;
1709
1049f94e 1710 /* ELF relocs are against symbols. If we are producing relocatable
e0001a05
NC
1711 output, and the reloc is against an external symbol, the resulting
1712 reloc will also be against the same symbol. In such a case, we
1713 don't want to change anything about the way the reloc is handled,
1714 since it will all be done at final link time. This test is similar
1715 to what bfd_elf_generic_reloc does except that it lets relocs with
1716 howto->partial_inplace go through even if the addend is non-zero.
1717 (The real problem is that partial_inplace is set for XTENSA_32
1718 relocs to begin with, but that's a long story and there's little we
1719 can do about it now....) */
1720
1721 if (output_bfd != (bfd *) NULL
1722 && (symbol->flags & BSF_SECTION_SYM) == 0)
1723 {
1724 reloc_entry->address += input_section->output_offset;
1725 return bfd_reloc_ok;
1726 }
1727
1728 /* Is the address of the relocation really within the section? */
1729 if (reloc_entry->address > (input_section->_cooked_size
1730 / bfd_octets_per_byte (abfd)))
1731 return bfd_reloc_outofrange;
1732
1733 /* Work out which section the relocation is targetted at and the
1734 initial relocation command value. */
1735
1736 /* Get symbol value. (Common symbols are special.) */
1737 if (bfd_is_com_section (symbol->section))
1738 relocation = 0;
1739 else
1740 relocation = symbol->value;
1741
1742 reloc_target_output_section = symbol->section->output_section;
1743
1744 /* Convert input-section-relative symbol value to absolute. */
1745 if ((output_bfd && !howto->partial_inplace)
1746 || reloc_target_output_section == NULL)
1747 output_base = 0;
1748 else
1749 output_base = reloc_target_output_section->vma;
1750
1751 relocation += output_base + symbol->section->output_offset;
1752
1753 /* Add in supplied addend. */
1754 relocation += reloc_entry->addend;
1755
1756 /* Here the variable relocation holds the final address of the
1757 symbol we are relocating against, plus any addend. */
1758 if (output_bfd)
1759 {
1760 if (!howto->partial_inplace)
1761 {
1762 /* This is a partial relocation, and we want to apply the relocation
1763 to the reloc entry rather than the raw data. Everything except
1764 relocations against section symbols has already been handled
1765 above. */
1766
1767 BFD_ASSERT (symbol->flags & BSF_SECTION_SYM);
1768 reloc_entry->addend = relocation;
1769 reloc_entry->address += input_section->output_offset;
1770 return bfd_reloc_ok;
1771 }
1772 else
1773 {
1774 reloc_entry->address += input_section->output_offset;
1775 reloc_entry->addend = 0;
1776 }
1777 }
1778
1779 is_weak_undef = (bfd_is_und_section (symbol->section)
1780 && (symbol->flags & BSF_WEAK) != 0);
1781 flag = elf_xtensa_do_reloc (howto, abfd, input_section, relocation,
1782 (bfd_byte *) data, (bfd_vma) octets,
1783 is_weak_undef, error_message);
1784
1785 if (flag == bfd_reloc_dangerous)
1786 {
1787 /* Add the symbol name to the error message. */
1788 if (! *error_message)
1789 *error_message = "";
1790 *error_message = vsprint_msg (*error_message, ": (%s + 0x%lx)",
1791 strlen (symbol->name) + 17,
1792 symbol->name, reloc_entry->addend);
1793 }
1794
1795 return flag;
1796}
1797
1798
1799/* Set up an entry in the procedure linkage table. */
1800
1801static bfd_vma
1802elf_xtensa_create_plt_entry (dynobj, output_bfd, reloc_index)
1803 bfd *dynobj;
1804 bfd *output_bfd;
1805 unsigned reloc_index;
1806{
1807 asection *splt, *sgotplt;
1808 bfd_vma plt_base, got_base;
1809 bfd_vma code_offset, lit_offset;
1810 int chunk;
1811
1812 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
1813 splt = elf_xtensa_get_plt_section (dynobj, chunk);
1814 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
1815 BFD_ASSERT (splt != NULL && sgotplt != NULL);
1816
1817 plt_base = splt->output_section->vma + splt->output_offset;
1818 got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1819
1820 lit_offset = 8 + (reloc_index % PLT_ENTRIES_PER_CHUNK) * 4;
1821 code_offset = (reloc_index % PLT_ENTRIES_PER_CHUNK) * PLT_ENTRY_SIZE;
1822
1823 /* Fill in the literal entry. This is the offset of the dynamic
1824 relocation entry. */
1825 bfd_put_32 (output_bfd, reloc_index * sizeof (Elf32_External_Rela),
1826 sgotplt->contents + lit_offset);
1827
1828 /* Fill in the entry in the procedure linkage table. */
1829 memcpy (splt->contents + code_offset,
1830 (bfd_big_endian (output_bfd)
1831 ? elf_xtensa_be_plt_entry
1832 : elf_xtensa_le_plt_entry),
1833 PLT_ENTRY_SIZE);
1834 bfd_put_16 (output_bfd, l32r_offset (got_base + 0,
1835 plt_base + code_offset + 3),
1836 splt->contents + code_offset + 4);
1837 bfd_put_16 (output_bfd, l32r_offset (got_base + 4,
1838 plt_base + code_offset + 6),
1839 splt->contents + code_offset + 7);
1840 bfd_put_16 (output_bfd, l32r_offset (got_base + lit_offset,
1841 plt_base + code_offset + 9),
1842 splt->contents + code_offset + 10);
1843
1844 return plt_base + code_offset;
1845}
1846
1847
e0001a05 1848/* Relocate an Xtensa ELF section. This is invoked by the linker for
1049f94e 1849 both relocatable and final links. */
e0001a05
NC
1850
1851static bfd_boolean
1852elf_xtensa_relocate_section (output_bfd, info, input_bfd,
1853 input_section, contents, relocs,
1854 local_syms, local_sections)
1855 bfd *output_bfd;
1856 struct bfd_link_info *info;
1857 bfd *input_bfd;
1858 asection *input_section;
1859 bfd_byte *contents;
1860 Elf_Internal_Rela *relocs;
1861 Elf_Internal_Sym *local_syms;
1862 asection **local_sections;
1863{
1864 Elf_Internal_Shdr *symtab_hdr;
1865 Elf_Internal_Rela *rel;
1866 Elf_Internal_Rela *relend;
1867 struct elf_link_hash_entry **sym_hashes;
1868 asection *srelgot, *srelplt;
1869 bfd *dynobj;
1870 char *error_message = NULL;
1871
1872 if (xtensa_default_isa == NULL)
1873 xtensa_isa_init ();
1874
1875 dynobj = elf_hash_table (info)->dynobj;
1876 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1877 sym_hashes = elf_sym_hashes (input_bfd);
1878
1879 srelgot = NULL;
1880 srelplt = NULL;
1881 if (dynobj != NULL)
1882 {
1883 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
1884 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1885 }
1886
1887 rel = relocs;
1888 relend = relocs + input_section->reloc_count;
1889 for (; rel < relend; rel++)
1890 {
1891 int r_type;
1892 reloc_howto_type *howto;
1893 unsigned long r_symndx;
1894 struct elf_link_hash_entry *h;
1895 Elf_Internal_Sym *sym;
1896 asection *sec;
1897 bfd_vma relocation;
1898 bfd_reloc_status_type r;
1899 bfd_boolean is_weak_undef;
1900 bfd_boolean unresolved_reloc;
9b8c98a4 1901 bfd_boolean warned;
e0001a05
NC
1902
1903 r_type = ELF32_R_TYPE (rel->r_info);
1904 if (r_type == (int) R_XTENSA_GNU_VTINHERIT
1905 || r_type == (int) R_XTENSA_GNU_VTENTRY)
1906 continue;
1907
1908 if (r_type < 0 || r_type >= (int) R_XTENSA_max)
1909 {
1910 bfd_set_error (bfd_error_bad_value);
1911 return FALSE;
1912 }
1913 howto = &elf_howto_table[r_type];
1914
1915 r_symndx = ELF32_R_SYM (rel->r_info);
1916
1049f94e 1917 if (info->relocatable)
e0001a05 1918 {
1049f94e 1919 /* This is a relocatable link.
e0001a05
NC
1920 1) If the reloc is against a section symbol, adjust
1921 according to the output section.
1922 2) If there is a new target for this relocation,
1923 the new target will be in the same output section.
1924 We adjust the relocation by the output section
1925 difference. */
1926
1927 if (relaxing_section)
1928 {
1929 /* Check if this references a section in another input file. */
1049f94e 1930 do_fix_for_relocatable_link (rel, input_bfd, input_section);
e0001a05
NC
1931 r_type = ELF32_R_TYPE (rel->r_info);
1932 }
1933
1934 if (r_type == R_XTENSA_ASM_SIMPLIFY)
1935 {
1936 /* Convert ASM_SIMPLIFY into the simpler relocation
1937 so that they never escape a relaxing link. */
1938 contract_asm_expansion (contents, input_section->_raw_size, rel);
1939 r_type = ELF32_R_TYPE (rel->r_info);
1940 }
1941
1049f94e 1942 /* This is a relocatable link, so we don't have to change
e0001a05
NC
1943 anything unless the reloc is against a section symbol,
1944 in which case we have to adjust according to where the
1945 section symbol winds up in the output section. */
1946 if (r_symndx < symtab_hdr->sh_info)
1947 {
1948 sym = local_syms + r_symndx;
1949 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1950 {
1951 sec = local_sections[r_symndx];
1952 rel->r_addend += sec->output_offset + sym->st_value;
1953 }
1954 }
1955
1956 /* If there is an addend with a partial_inplace howto,
1957 then move the addend to the contents. This is a hack
1049f94e 1958 to work around problems with DWARF in relocatable links
e0001a05
NC
1959 with some previous version of BFD. Now we can't easily get
1960 rid of the hack without breaking backward compatibility.... */
1961 if (rel->r_addend)
1962 {
1963 howto = &elf_howto_table[r_type];
1964 if (howto->partial_inplace)
1965 {
1966 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
1967 rel->r_addend, contents,
1968 rel->r_offset, FALSE,
1969 &error_message);
1970 if (r != bfd_reloc_ok)
1971 {
1972 if (!((*info->callbacks->reloc_dangerous)
1973 (info, error_message, input_bfd, input_section,
1974 rel->r_offset)))
1975 return FALSE;
1976 }
1977 rel->r_addend = 0;
1978 }
1979 }
1980
1049f94e 1981 /* Done with work for relocatable link; continue with next reloc. */
e0001a05
NC
1982 continue;
1983 }
1984
1985 /* This is a final link. */
1986
1987 h = NULL;
1988 sym = NULL;
1989 sec = NULL;
1990 is_weak_undef = FALSE;
1991 unresolved_reloc = FALSE;
9b8c98a4 1992 warned = FALSE;
e0001a05
NC
1993
1994 if (howto->partial_inplace)
1995 {
1996 /* Because R_XTENSA_32 was made partial_inplace to fix some
1997 problems with DWARF info in partial links, there may be
1998 an addend stored in the contents. Take it out of there
1999 and move it back into the addend field of the reloc. */
2000 rel->r_addend += bfd_get_32 (input_bfd, contents + rel->r_offset);
2001 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
2002 }
2003
2004 if (r_symndx < symtab_hdr->sh_info)
2005 {
2006 sym = local_syms + r_symndx;
2007 sec = local_sections[r_symndx];
2008 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
2009 }
2010 else
2011 {
2012 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2013
2014 while (h->root.type == bfd_link_hash_indirect
2015 || h->root.type == bfd_link_hash_warning)
2016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2017
2018 relocation = 0;
2019 if (h->root.type == bfd_link_hash_defined
2020 || h->root.type == bfd_link_hash_defweak)
2021 {
2022 sec = h->root.u.def.section;
2023
2024 if (sec->output_section == NULL)
2025 /* Set a flag that will be cleared later if we find a
2026 relocation value for this symbol. output_section
2027 is typically NULL for symbols satisfied by a shared
2028 library. */
2029 unresolved_reloc = TRUE;
2030 else
2031 relocation = (h->root.u.def.value
2032 + sec->output_section->vma
2033 + sec->output_offset);
2034 }
2035 else if (h->root.type == bfd_link_hash_undefweak)
2036 is_weak_undef = TRUE;
2037 else if (info->shared
2038 && !info->no_undefined
2039 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2040 ;
2041 else
2042 {
2043 if (! ((*info->callbacks->undefined_symbol)
2044 (info, h->root.root.string, input_bfd,
2045 input_section, rel->r_offset,
2046 (!info->shared || info->no_undefined
2047 || ELF_ST_VISIBILITY (h->other)))))
2048 return FALSE;
9b8c98a4 2049 warned = TRUE;
e0001a05
NC
2050 }
2051 }
2052
2053 if (relaxing_section)
2054 {
2055 /* Check if this references a section in another input file. */
2056 do_fix_for_final_link (rel, input_section, &relocation);
2057
2058 /* Update some already cached values. */
2059 r_type = ELF32_R_TYPE (rel->r_info);
2060 howto = &elf_howto_table[r_type];
2061 }
2062
2063 /* Sanity check the address. */
2064 if (rel->r_offset >= input_section->_raw_size
2065 && ELF32_R_TYPE (rel->r_info) != R_XTENSA_NONE)
2066 {
2067 bfd_set_error (bfd_error_bad_value);
2068 return FALSE;
2069 }
2070
2071 /* Generate dynamic relocations. */
2072 if (elf_hash_table (info)->dynamic_sections_created)
2073 {
571b5725 2074 bfd_boolean dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
e0001a05
NC
2075
2076 if (dynamic_symbol && (r_type == R_XTENSA_OP0
2077 || r_type == R_XTENSA_OP1
2078 || r_type == R_XTENSA_OP2))
2079 {
2080 /* This is an error. The symbol's real value won't be known
2081 until runtime and it's likely to be out of range anyway. */
2082 const char *name = h->root.root.string;
2083 error_message = vsprint_msg ("invalid relocation for dynamic "
2084 "symbol", ": %s",
2085 strlen (name) + 2, name);
2086 if (!((*info->callbacks->reloc_dangerous)
2087 (info, error_message, input_bfd, input_section,
2088 rel->r_offset)))
2089 return FALSE;
2090 }
2091 else if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
2092 && (input_section->flags & SEC_ALLOC) != 0
2093 && (dynamic_symbol || info->shared))
2094 {
2095 Elf_Internal_Rela outrel;
2096 bfd_byte *loc;
2097 asection *srel;
2098
2099 if (dynamic_symbol && r_type == R_XTENSA_PLT)
2100 srel = srelplt;
2101 else
2102 srel = srelgot;
2103
2104 BFD_ASSERT (srel != NULL);
2105
2106 outrel.r_offset =
2107 _bfd_elf_section_offset (output_bfd, info,
2108 input_section, rel->r_offset);
2109
2110 if ((outrel.r_offset | 1) == (bfd_vma) -1)
2111 memset (&outrel, 0, sizeof outrel);
2112 else
2113 {
2114 outrel.r_offset = (input_section->output_section->vma
2115 + input_section->output_offset);
2116
2117 if (dynamic_symbol)
2118 {
2119 outrel.r_addend = rel->r_addend;
2120 rel->r_addend = 0;
2121
2122 if (r_type == R_XTENSA_32)
2123 {
2124 outrel.r_info =
2125 ELF32_R_INFO (h->dynindx, R_XTENSA_GLOB_DAT);
2126 relocation = 0;
2127 }
2128 else /* r_type == R_XTENSA_PLT */
2129 {
2130 outrel.r_info =
2131 ELF32_R_INFO (h->dynindx, R_XTENSA_JMP_SLOT);
2132
2133 /* Create the PLT entry and set the initial
2134 contents of the literal entry to the address of
2135 the PLT entry. */
2136 relocation =
2137 elf_xtensa_create_plt_entry (dynobj, output_bfd,
2138 srel->reloc_count);
2139 }
2140 unresolved_reloc = FALSE;
2141 }
2142 else
2143 {
2144 /* Generate a RELATIVE relocation. */
2145 outrel.r_info = ELF32_R_INFO (0, R_XTENSA_RELATIVE);
2146 outrel.r_addend = 0;
2147 }
2148 }
2149
2150 loc = (srel->contents
2151 + srel->reloc_count++ * sizeof (Elf32_External_Rela));
2152 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2153 BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count
2154 <= srel->_cooked_size);
2155 }
2156 }
2157
2158 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2159 because such sections are not SEC_ALLOC and thus ld.so will
2160 not process them. */
2161 if (unresolved_reloc
2162 && !((input_section->flags & SEC_DEBUGGING) != 0
2163 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2164 (*_bfd_error_handler)
2165 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2166 bfd_archive_filename (input_bfd),
2167 bfd_get_section_name (input_bfd, input_section),
2168 (long) rel->r_offset,
2169 h->root.root.string);
2170
2171 /* There's no point in calling bfd_perform_relocation here.
2172 Just go directly to our "special function". */
2173 r = elf_xtensa_do_reloc (howto, input_bfd, input_section,
2174 relocation + rel->r_addend,
2175 contents, rel->r_offset, is_weak_undef,
2176 &error_message);
2177
9b8c98a4 2178 if (r != bfd_reloc_ok && !warned)
e0001a05
NC
2179 {
2180 const char *name;
2181
2182 BFD_ASSERT (r == bfd_reloc_dangerous);
2183 BFD_ASSERT (error_message != (char *) NULL);
2184
2185 if (h != NULL)
2186 name = h->root.root.string;
2187 else
2188 {
2189 name = bfd_elf_string_from_elf_section
2190 (input_bfd, symtab_hdr->sh_link, sym->st_name);
2191 if (name && *name == '\0')
2192 name = bfd_section_name (input_bfd, sec);
2193 }
2194 if (name)
2195 error_message = vsprint_msg (error_message, ": %s",
2196 strlen (name), name);
2197 if (!((*info->callbacks->reloc_dangerous)
2198 (info, error_message, input_bfd, input_section,
2199 rel->r_offset)))
2200 return FALSE;
2201 }
2202 }
2203
2204 return TRUE;
2205}
2206
2207
2208/* Finish up dynamic symbol handling. There's not much to do here since
2209 the PLT and GOT entries are all set up by relocate_section. */
2210
2211static bfd_boolean
2212elf_xtensa_finish_dynamic_symbol (output_bfd, info, h, sym)
2213 bfd *output_bfd ATTRIBUTE_UNUSED;
2214 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2215 struct elf_link_hash_entry *h;
2216 Elf_Internal_Sym *sym;
2217{
2218 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2219 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2220 {
2221 /* Mark the symbol as undefined, rather than as defined in
2222 the .plt section. Leave the value alone. */
2223 sym->st_shndx = SHN_UNDEF;
2224 }
2225
2226 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2227 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2228 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2229 sym->st_shndx = SHN_ABS;
2230
2231 return TRUE;
2232}
2233
2234
2235/* Combine adjacent literal table entries in the output. Adjacent
2236 entries within each input section may have been removed during
2237 relaxation, but we repeat the process here, even though it's too late
2238 to shrink the output section, because it's important to minimize the
2239 number of literal table entries to reduce the start-up work for the
2240 runtime linker. Returns the number of remaining table entries or -1
2241 on error. */
2242
2243static int
e901de89 2244elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc)
e0001a05 2245 bfd *output_bfd;
e901de89
BW
2246 asection *sxtlit;
2247 asection *sgotloc;
e0001a05 2248{
e0001a05
NC
2249 bfd_byte *contents;
2250 property_table_entry *table;
e901de89 2251 bfd_size_type section_size, sgotloc_size;
e0001a05
NC
2252 bfd_vma offset;
2253 int n, m, num;
2254
e901de89
BW
2255 section_size = (sxtlit->_cooked_size != 0
2256 ? sxtlit->_cooked_size : sxtlit->_raw_size);
e0001a05
NC
2257 BFD_ASSERT (section_size % 8 == 0);
2258 num = section_size / 8;
2259
e901de89
BW
2260 sgotloc_size = (sgotloc->_cooked_size != 0
2261 ? sgotloc->_cooked_size : sgotloc->_raw_size);
2262 if (sgotloc_size != section_size)
b536dc1e
BW
2263 {
2264 (*_bfd_error_handler)
2265 ("internal inconsistency in size of .got.loc section");
2266 return -1;
2267 }
e901de89 2268
e0001a05
NC
2269 contents = (bfd_byte *) bfd_malloc (section_size);
2270 table = (property_table_entry *)
2271 bfd_malloc (num * sizeof (property_table_entry));
2272 if (contents == 0 || table == 0)
2273 return -1;
2274
2275 /* The ".xt.lit.plt" section has the SEC_IN_MEMORY flag set and this
2276 propagates to the output section, where it doesn't really apply and
2277 where it breaks the following call to bfd_get_section_contents. */
e901de89 2278 sxtlit->flags &= ~SEC_IN_MEMORY;
e0001a05 2279
e901de89
BW
2280 if (! bfd_get_section_contents (output_bfd, sxtlit, contents, 0,
2281 section_size))
e0001a05
NC
2282 return -1;
2283
2284 /* There should never be any relocations left at this point, so this
2285 is quite a bit easier than what is done during relaxation. */
2286
2287 /* Copy the raw contents into a property table array and sort it. */
2288 offset = 0;
2289 for (n = 0; n < num; n++)
2290 {
2291 table[n].address = bfd_get_32 (output_bfd, &contents[offset]);
2292 table[n].size = bfd_get_32 (output_bfd, &contents[offset + 4]);
2293 offset += 8;
2294 }
2295 qsort (table, num, sizeof (property_table_entry), property_table_compare);
2296
2297 for (n = 0; n < num; n++)
2298 {
2299 bfd_boolean remove = FALSE;
2300
2301 if (table[n].size == 0)
2302 remove = TRUE;
2303 else if (n > 0 &&
2304 (table[n-1].address + table[n-1].size == table[n].address))
2305 {
2306 table[n-1].size += table[n].size;
2307 remove = TRUE;
2308 }
2309
2310 if (remove)
2311 {
2312 for (m = n; m < num - 1; m++)
2313 {
2314 table[m].address = table[m+1].address;
2315 table[m].size = table[m+1].size;
2316 }
2317
2318 n--;
2319 num--;
2320 }
2321 }
2322
2323 /* Copy the data back to the raw contents. */
2324 offset = 0;
2325 for (n = 0; n < num; n++)
2326 {
2327 bfd_put_32 (output_bfd, table[n].address, &contents[offset]);
2328 bfd_put_32 (output_bfd, table[n].size, &contents[offset + 4]);
2329 offset += 8;
2330 }
2331
2332 /* Clear the removed bytes. */
2333 if ((bfd_size_type) (num * 8) < section_size)
2334 {
2335 memset (&contents[num * 8], 0, section_size - num * 8);
e901de89 2336 sxtlit->_cooked_size = num * 8;
e0001a05
NC
2337 }
2338
e901de89
BW
2339 if (! bfd_set_section_contents (output_bfd, sxtlit, contents, 0,
2340 section_size))
e0001a05
NC
2341 return -1;
2342
e901de89
BW
2343 /* Copy the contents to ".got.loc". */
2344 memcpy (sgotloc->contents, contents, section_size);
2345
e0001a05
NC
2346 free (contents);
2347 return num;
2348}
2349
2350
2351/* Finish up the dynamic sections. */
2352
2353static bfd_boolean
2354elf_xtensa_finish_dynamic_sections (output_bfd, info)
2355 bfd *output_bfd;
2356 struct bfd_link_info *info;
2357{
2358 bfd *dynobj;
e901de89 2359 asection *sdyn, *srelplt, *sgot, *sxtlit, *sgotloc;
e0001a05
NC
2360 Elf32_External_Dyn *dyncon, *dynconend;
2361 int num_xtlit_entries;
2362
2363 if (! elf_hash_table (info)->dynamic_sections_created)
2364 return TRUE;
2365
2366 dynobj = elf_hash_table (info)->dynobj;
2367 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2368 BFD_ASSERT (sdyn != NULL);
2369
2370 /* Set the first entry in the global offset table to the address of
2371 the dynamic section. */
2372 sgot = bfd_get_section_by_name (dynobj, ".got");
2373 if (sgot)
2374 {
2375 BFD_ASSERT (sgot->_raw_size == 4);
2376 if (sdyn == NULL)
2377 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2378 else
2379 bfd_put_32 (output_bfd,
2380 sdyn->output_section->vma + sdyn->output_offset,
2381 sgot->contents);
2382 }
2383
2384 srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2385 if (srelplt != NULL && srelplt->_raw_size != 0)
2386 {
2387 asection *sgotplt, *srelgot, *spltlittbl;
2388 int chunk, plt_chunks, plt_entries;
2389 Elf_Internal_Rela irela;
2390 bfd_byte *loc;
2391 unsigned rtld_reloc;
2392
2393 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");;
2394 BFD_ASSERT (srelgot != NULL);
2395
2396 spltlittbl = bfd_get_section_by_name (dynobj, ".xt.lit.plt");
2397 BFD_ASSERT (spltlittbl != NULL);
2398
2399 /* Find the first XTENSA_RTLD relocation. Presumably the rest
2400 of them follow immediately after.... */
2401 for (rtld_reloc = 0; rtld_reloc < srelgot->reloc_count; rtld_reloc++)
2402 {
2403 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2404 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2405 if (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD)
2406 break;
2407 }
2408 BFD_ASSERT (rtld_reloc < srelgot->reloc_count);
2409
2410 plt_entries = (srelplt->_raw_size / sizeof (Elf32_External_Rela));
2411 plt_chunks =
2412 (plt_entries + PLT_ENTRIES_PER_CHUNK - 1) / PLT_ENTRIES_PER_CHUNK;
2413
2414 for (chunk = 0; chunk < plt_chunks; chunk++)
2415 {
2416 int chunk_entries = 0;
2417
2418 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
2419 BFD_ASSERT (sgotplt != NULL);
2420
2421 /* Emit special RTLD relocations for the first two entries in
2422 each chunk of the .got.plt section. */
2423
2424 loc = srelgot->contents + rtld_reloc * sizeof (Elf32_External_Rela);
2425 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2426 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2427 irela.r_offset = (sgotplt->output_section->vma
2428 + sgotplt->output_offset);
2429 irela.r_addend = 1; /* tell rtld to set value to resolver function */
2430 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2431 rtld_reloc += 1;
2432 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2433
2434 /* Next literal immediately follows the first. */
2435 loc += sizeof (Elf32_External_Rela);
2436 bfd_elf32_swap_reloca_in (output_bfd, loc, &irela);
2437 BFD_ASSERT (ELF32_R_TYPE (irela.r_info) == R_XTENSA_RTLD);
2438 irela.r_offset = (sgotplt->output_section->vma
2439 + sgotplt->output_offset + 4);
2440 /* Tell rtld to set value to object's link map. */
2441 irela.r_addend = 2;
2442 bfd_elf32_swap_reloca_out (output_bfd, &irela, loc);
2443 rtld_reloc += 1;
2444 BFD_ASSERT (rtld_reloc <= srelgot->reloc_count);
2445
2446 /* Fill in the literal table. */
2447 if (chunk < plt_chunks - 1)
2448 chunk_entries = PLT_ENTRIES_PER_CHUNK;
2449 else
2450 chunk_entries = plt_entries - (chunk * PLT_ENTRIES_PER_CHUNK);
2451
2452 BFD_ASSERT ((unsigned) (chunk + 1) * 8 <= spltlittbl->_cooked_size);
2453 bfd_put_32 (output_bfd,
2454 sgotplt->output_section->vma + sgotplt->output_offset,
2455 spltlittbl->contents + (chunk * 8) + 0);
2456 bfd_put_32 (output_bfd,
2457 8 + (chunk_entries * 4),
2458 spltlittbl->contents + (chunk * 8) + 4);
2459 }
2460
2461 /* All the dynamic relocations have been emitted at this point.
2462 Make sure the relocation sections are the correct size. */
2463 if (srelgot->_cooked_size != (sizeof (Elf32_External_Rela)
2464 * srelgot->reloc_count)
2465 || srelplt->_cooked_size != (sizeof (Elf32_External_Rela)
2466 * srelplt->reloc_count))
2467 abort ();
2468
2469 /* The .xt.lit.plt section has just been modified. This must
2470 happen before the code below which combines adjacent literal
2471 table entries, and the .xt.lit.plt contents have to be forced to
2472 the output here. */
2473 if (! bfd_set_section_contents (output_bfd,
2474 spltlittbl->output_section,
2475 spltlittbl->contents,
2476 spltlittbl->output_offset,
2477 spltlittbl->_raw_size))
2478 return FALSE;
2479 /* Clear SEC_HAS_CONTENTS so the contents won't be output again. */
2480 spltlittbl->flags &= ~SEC_HAS_CONTENTS;
2481 }
2482
2483 /* Combine adjacent literal table entries. */
1049f94e 2484 BFD_ASSERT (! info->relocatable);
e901de89
BW
2485 sxtlit = bfd_get_section_by_name (output_bfd, ".xt.lit");
2486 sgotloc = bfd_get_section_by_name (dynobj, ".got.loc");
b536dc1e 2487 BFD_ASSERT (sxtlit && sgotloc);
e901de89
BW
2488 num_xtlit_entries =
2489 elf_xtensa_combine_prop_entries (output_bfd, sxtlit, sgotloc);
e0001a05
NC
2490 if (num_xtlit_entries < 0)
2491 return FALSE;
2492
2493 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2494 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2495 for (; dyncon < dynconend; dyncon++)
2496 {
2497 Elf_Internal_Dyn dyn;
2498 const char *name;
2499 asection *s;
2500
2501 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2502
2503 switch (dyn.d_tag)
2504 {
2505 default:
2506 break;
2507
2508 case DT_XTENSA_GOT_LOC_SZ:
e0001a05
NC
2509 dyn.d_un.d_val = num_xtlit_entries;
2510 break;
2511
2512 case DT_XTENSA_GOT_LOC_OFF:
e901de89 2513 name = ".got.loc";
e0001a05
NC
2514 goto get_vma;
2515 case DT_PLTGOT:
2516 name = ".got";
2517 goto get_vma;
2518 case DT_JMPREL:
2519 name = ".rela.plt";
2520 get_vma:
2521 s = bfd_get_section_by_name (output_bfd, name);
2522 BFD_ASSERT (s);
2523 dyn.d_un.d_ptr = s->vma;
2524 break;
2525
2526 case DT_PLTRELSZ:
2527 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2528 BFD_ASSERT (s);
2529 dyn.d_un.d_val = (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2530 break;
2531
2532 case DT_RELASZ:
2533 /* Adjust RELASZ to not include JMPREL. This matches what
2534 glibc expects and what is done for several other ELF
2535 targets (e.g., i386, alpha), but the "correct" behavior
2536 seems to be unresolved. Since the linker script arranges
2537 for .rela.plt to follow all other relocation sections, we
2538 don't have to worry about changing the DT_RELA entry. */
2539 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2540 if (s)
2541 {
2542 dyn.d_un.d_val -=
2543 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2544 }
2545 break;
2546 }
2547
2548 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2549 }
2550
2551 return TRUE;
2552}
2553
2554\f
2555/* Functions for dealing with the e_flags field. */
2556
2557/* Merge backend specific data from an object file to the output
2558 object file when linking. */
2559
2560static bfd_boolean
2561elf_xtensa_merge_private_bfd_data (ibfd, obfd)
2562 bfd *ibfd;
2563 bfd *obfd;
2564{
2565 unsigned out_mach, in_mach;
2566 flagword out_flag, in_flag;
2567
2568 /* Check if we have the same endianess. */
2569 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
2570 return FALSE;
2571
2572 /* Don't even pretend to support mixed-format linking. */
2573 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2574 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2575 return FALSE;
2576
2577 out_flag = elf_elfheader (obfd)->e_flags;
2578 in_flag = elf_elfheader (ibfd)->e_flags;
2579
2580 out_mach = out_flag & EF_XTENSA_MACH;
2581 in_mach = in_flag & EF_XTENSA_MACH;
2582 if (out_mach != in_mach)
2583 {
2584 (*_bfd_error_handler)
b536dc1e 2585 ("%s: incompatible machine type. Output is 0x%x. Input is 0x%x",
e0001a05
NC
2586 bfd_archive_filename (ibfd), out_mach, in_mach);
2587 bfd_set_error (bfd_error_wrong_format);
2588 return FALSE;
2589 }
2590
2591 if (! elf_flags_init (obfd))
2592 {
2593 elf_flags_init (obfd) = TRUE;
2594 elf_elfheader (obfd)->e_flags = in_flag;
2595
2596 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2597 && bfd_get_arch_info (obfd)->the_default)
2598 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2599 bfd_get_mach (ibfd));
2600
2601 return TRUE;
2602 }
2603
2604 if ((out_flag & EF_XTENSA_XT_INSN) !=
2605 (in_flag & EF_XTENSA_XT_INSN))
2606 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_INSN);
2607
2608 if ((out_flag & EF_XTENSA_XT_LIT) !=
2609 (in_flag & EF_XTENSA_XT_LIT))
2610 elf_elfheader(obfd)->e_flags &= (~ EF_XTENSA_XT_LIT);
2611
2612 return TRUE;
2613}
2614
2615
2616static bfd_boolean
2617elf_xtensa_set_private_flags (abfd, flags)
2618 bfd *abfd;
2619 flagword flags;
2620{
2621 BFD_ASSERT (!elf_flags_init (abfd)
2622 || elf_elfheader (abfd)->e_flags == flags);
2623
2624 elf_elfheader (abfd)->e_flags |= flags;
2625 elf_flags_init (abfd) = TRUE;
2626
2627 return TRUE;
2628}
2629
2630
2631extern flagword
2632elf_xtensa_get_private_bfd_flags (abfd)
2633 bfd *abfd;
2634{
2635 return elf_elfheader (abfd)->e_flags;
2636}
2637
2638
2639static bfd_boolean
2640elf_xtensa_print_private_bfd_data (abfd, farg)
2641 bfd *abfd;
2642 PTR farg;
2643{
2644 FILE *f = (FILE *) farg;
2645 flagword e_flags = elf_elfheader (abfd)->e_flags;
2646
2647 fprintf (f, "\nXtensa header:\n");
2648 if ((e_flags & EF_XTENSA_MACH) == E_XTENSA_MACH)
2649 fprintf (f, "\nMachine = Base\n");
2650 else
2651 fprintf (f, "\nMachine Id = 0x%x\n", e_flags & EF_XTENSA_MACH);
2652
2653 fprintf (f, "Insn tables = %s\n",
2654 (e_flags & EF_XTENSA_XT_INSN) ? "true" : "false");
2655
2656 fprintf (f, "Literal tables = %s\n",
2657 (e_flags & EF_XTENSA_XT_LIT) ? "true" : "false");
2658
2659 return _bfd_elf_print_private_bfd_data (abfd, farg);
2660}
2661
2662
2663/* Set the right machine number for an Xtensa ELF file. */
2664
2665static bfd_boolean
2666elf_xtensa_object_p (abfd)
2667 bfd *abfd;
2668{
2669 int mach;
2670 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_XTENSA_MACH;
2671
2672 switch (arch)
2673 {
2674 case E_XTENSA_MACH:
2675 mach = bfd_mach_xtensa;
2676 break;
2677 default:
2678 return FALSE;
2679 }
2680
2681 (void) bfd_default_set_arch_mach (abfd, bfd_arch_xtensa, mach);
2682 return TRUE;
2683}
2684
2685
2686/* The final processing done just before writing out an Xtensa ELF object
2687 file. This gets the Xtensa architecture right based on the machine
2688 number. */
2689
2690static void
2691elf_xtensa_final_write_processing (abfd, linker)
2692 bfd *abfd;
2693 bfd_boolean linker ATTRIBUTE_UNUSED;
2694{
2695 int mach;
2696 unsigned long val;
2697
2698 switch (mach = bfd_get_mach (abfd))
2699 {
2700 case bfd_mach_xtensa:
2701 val = E_XTENSA_MACH;
2702 break;
2703 default:
2704 return;
2705 }
2706
2707 elf_elfheader (abfd)->e_flags &= (~ EF_XTENSA_MACH);
2708 elf_elfheader (abfd)->e_flags |= val;
2709}
2710
2711
2712static enum elf_reloc_type_class
2713elf_xtensa_reloc_type_class (rela)
2714 const Elf_Internal_Rela *rela;
2715{
2716 switch ((int) ELF32_R_TYPE (rela->r_info))
2717 {
2718 case R_XTENSA_RELATIVE:
2719 return reloc_class_relative;
2720 case R_XTENSA_JMP_SLOT:
2721 return reloc_class_plt;
2722 default:
2723 return reloc_class_normal;
2724 }
2725}
2726
2727\f
2728static bfd_boolean
2729elf_xtensa_discard_info_for_section (abfd, cookie, info, sec)
2730 bfd *abfd;
2731 struct elf_reloc_cookie *cookie;
2732 struct bfd_link_info *info;
2733 asection *sec;
2734{
2735 bfd_byte *contents;
2736 bfd_vma section_size;
2737 bfd_vma offset, actual_offset;
2738 size_t removed_bytes = 0;
2739
2740 section_size = (sec->_cooked_size ? sec->_cooked_size : sec->_raw_size);
2741 if (section_size == 0 || section_size % 8 != 0)
2742 return FALSE;
2743
2744 if (sec->output_section
2745 && bfd_is_abs_section (sec->output_section))
2746 return FALSE;
2747
2748 contents = retrieve_contents (abfd, sec, info->keep_memory);
2749 if (!contents)
2750 return FALSE;
2751
2752 cookie->rels = retrieve_internal_relocs (abfd, sec, info->keep_memory);
2753 if (!cookie->rels)
2754 {
2755 release_contents (sec, contents);
2756 return FALSE;
2757 }
2758
2759 cookie->rel = cookie->rels;
2760 cookie->relend = cookie->rels + sec->reloc_count;
2761
2762 for (offset = 0; offset < section_size; offset += 8)
2763 {
2764 actual_offset = offset - removed_bytes;
2765
2766 /* The ...symbol_deleted_p function will skip over relocs but it
2767 won't adjust their offsets, so do that here. */
2768 while (cookie->rel < cookie->relend
2769 && cookie->rel->r_offset < offset)
2770 {
2771 cookie->rel->r_offset -= removed_bytes;
2772 cookie->rel++;
2773 }
2774
2775 while (cookie->rel < cookie->relend
2776 && cookie->rel->r_offset == offset)
2777 {
2778 if (_bfd_elf32_reloc_symbol_deleted_p (offset, cookie))
2779 {
2780 /* Remove the table entry. (If the reloc type is NONE, then
2781 the entry has already been merged with another and deleted
2782 during relaxation.) */
2783 if (ELF32_R_TYPE (cookie->rel->r_info) != R_XTENSA_NONE)
2784 {
2785 /* Shift the contents up. */
2786 if (offset + 8 < section_size)
2787 memmove (&contents[actual_offset],
2788 &contents[actual_offset+8],
2789 section_size - offset - 8);
2790 removed_bytes += 8;
2791 }
2792
2793 /* Remove this relocation. */
2794 cookie->rel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
2795 }
2796
2797 /* Adjust the relocation offset for previous removals. This
2798 should not be done before calling ...symbol_deleted_p
2799 because it might mess up the offset comparisons there.
2800 Make sure the offset doesn't underflow in the case where
2801 the first entry is removed. */
2802 if (cookie->rel->r_offset >= removed_bytes)
2803 cookie->rel->r_offset -= removed_bytes;
2804 else
2805 cookie->rel->r_offset = 0;
2806
2807 cookie->rel++;
2808 }
2809 }
2810
2811 if (removed_bytes != 0)
2812 {
2813 /* Adjust any remaining relocs (shouldn't be any). */
2814 for (; cookie->rel < cookie->relend; cookie->rel++)
2815 {
2816 if (cookie->rel->r_offset >= removed_bytes)
2817 cookie->rel->r_offset -= removed_bytes;
2818 else
2819 cookie->rel->r_offset = 0;
2820 }
2821
2822 /* Clear the removed bytes. */
2823 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
2824
2825 pin_contents (sec, contents);
2826 pin_internal_relocs (sec, cookie->rels);
2827
2828 sec->_cooked_size = section_size - removed_bytes;
2829 /* Also shrink _raw_size. See comments in relax_property_section. */
2830 sec->_raw_size = sec->_cooked_size;
b536dc1e
BW
2831
2832 if (xtensa_is_littable_section (sec))
2833 {
2834 bfd *dynobj = elf_hash_table (info)->dynobj;
2835 if (dynobj)
2836 {
2837 asection *sgotloc =
2838 bfd_get_section_by_name (dynobj, ".got.loc");
2839 if (sgotloc)
2840 {
2841 bfd_size_type sgotloc_size =
2842 (sgotloc->_cooked_size ? sgotloc->_cooked_size
2843 : sgotloc->_raw_size);
2844 sgotloc->_cooked_size = sgotloc_size - removed_bytes;
2845 sgotloc->_raw_size = sgotloc_size - removed_bytes;
2846 }
2847 }
2848 }
e0001a05
NC
2849 }
2850 else
2851 {
2852 release_contents (sec, contents);
2853 release_internal_relocs (sec, cookie->rels);
2854 }
2855
2856 return (removed_bytes != 0);
2857}
2858
2859
2860static bfd_boolean
2861elf_xtensa_discard_info (abfd, cookie, info)
2862 bfd *abfd;
2863 struct elf_reloc_cookie *cookie;
2864 struct bfd_link_info *info;
2865{
2866 asection *sec;
2867 bfd_boolean changed = FALSE;
2868
2869 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2870 {
2871 if (xtensa_is_property_section (sec))
2872 {
2873 if (elf_xtensa_discard_info_for_section (abfd, cookie, info, sec))
2874 changed = TRUE;
2875 }
2876 }
2877
2878 return changed;
2879}
2880
2881
2882static bfd_boolean
2883elf_xtensa_ignore_discarded_relocs (sec)
2884 asection *sec;
2885{
2886 return xtensa_is_property_section (sec);
2887}
2888
2889\f
2890/* Support for core dump NOTE sections. */
2891
2892static bfd_boolean
2893elf_xtensa_grok_prstatus (abfd, note)
2894 bfd *abfd;
2895 Elf_Internal_Note *note;
2896{
2897 int offset;
2898 unsigned int raw_size;
2899
2900 /* The size for Xtensa is variable, so don't try to recognize the format
2901 based on the size. Just assume this is GNU/Linux. */
2902
2903 /* pr_cursig */
2904 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2905
2906 /* pr_pid */
2907 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
2908
2909 /* pr_reg */
2910 offset = 72;
2911 raw_size = note->descsz - offset - 4;
2912
2913 /* Make a ".reg/999" section. */
2914 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2915 raw_size, note->descpos + offset);
2916}
2917
2918
2919static bfd_boolean
2920elf_xtensa_grok_psinfo (abfd, note)
2921 bfd *abfd;
2922 Elf_Internal_Note *note;
2923{
2924 switch (note->descsz)
2925 {
2926 default:
2927 return FALSE;
2928
2929 case 128: /* GNU/Linux elf_prpsinfo */
2930 elf_tdata (abfd)->core_program
2931 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
2932 elf_tdata (abfd)->core_command
2933 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
2934 }
2935
2936 /* Note that for some reason, a spurious space is tacked
2937 onto the end of the args in some (at least one anyway)
2938 implementations, so strip it off if it exists. */
2939
2940 {
2941 char *command = elf_tdata (abfd)->core_command;
2942 int n = strlen (command);
2943
2944 if (0 < n && command[n - 1] == ' ')
2945 command[n - 1] = '\0';
2946 }
2947
2948 return TRUE;
2949}
2950
2951\f
2952/* Generic Xtensa configurability stuff. */
2953
2954static xtensa_opcode callx0_op = XTENSA_UNDEFINED;
2955static xtensa_opcode callx4_op = XTENSA_UNDEFINED;
2956static xtensa_opcode callx8_op = XTENSA_UNDEFINED;
2957static xtensa_opcode callx12_op = XTENSA_UNDEFINED;
2958static xtensa_opcode call0_op = XTENSA_UNDEFINED;
2959static xtensa_opcode call4_op = XTENSA_UNDEFINED;
2960static xtensa_opcode call8_op = XTENSA_UNDEFINED;
2961static xtensa_opcode call12_op = XTENSA_UNDEFINED;
2962
2963static void
2964init_call_opcodes ()
2965{
2966 if (callx0_op == XTENSA_UNDEFINED)
2967 {
2968 callx0_op = xtensa_opcode_lookup (xtensa_default_isa, "callx0");
2969 callx4_op = xtensa_opcode_lookup (xtensa_default_isa, "callx4");
2970 callx8_op = xtensa_opcode_lookup (xtensa_default_isa, "callx8");
2971 callx12_op = xtensa_opcode_lookup (xtensa_default_isa, "callx12");
2972 call0_op = xtensa_opcode_lookup (xtensa_default_isa, "call0");
2973 call4_op = xtensa_opcode_lookup (xtensa_default_isa, "call4");
2974 call8_op = xtensa_opcode_lookup (xtensa_default_isa, "call8");
2975 call12_op = xtensa_opcode_lookup (xtensa_default_isa, "call12");
2976 }
2977}
2978
2979
2980static bfd_boolean
2981is_indirect_call_opcode (opcode)
2982 xtensa_opcode opcode;
2983{
2984 init_call_opcodes ();
2985 return (opcode == callx0_op
2986 || opcode == callx4_op
2987 || opcode == callx8_op
2988 || opcode == callx12_op);
2989}
2990
2991
2992static bfd_boolean
2993is_direct_call_opcode (opcode)
2994 xtensa_opcode opcode;
2995{
2996 init_call_opcodes ();
2997 return (opcode == call0_op
2998 || opcode == call4_op
2999 || opcode == call8_op
3000 || opcode == call12_op);
3001}
3002
3003
3004static bfd_boolean
3005is_windowed_call_opcode (opcode)
3006 xtensa_opcode opcode;
3007{
3008 init_call_opcodes ();
3009 return (opcode == call4_op
3010 || opcode == call8_op
3011 || opcode == call12_op
3012 || opcode == callx4_op
3013 || opcode == callx8_op
3014 || opcode == callx12_op);
3015}
3016
3017
3018static xtensa_opcode
3019get_l32r_opcode (void)
3020{
3021 static xtensa_opcode l32r_opcode = XTENSA_UNDEFINED;
3022 if (l32r_opcode == XTENSA_UNDEFINED)
3023 {
3024 l32r_opcode = xtensa_opcode_lookup (xtensa_default_isa, "l32r");
3025 BFD_ASSERT (l32r_opcode != XTENSA_UNDEFINED);
3026 }
3027 return l32r_opcode;
3028}
3029
3030
3031static bfd_vma
3032l32r_offset (addr, pc)
3033 bfd_vma addr;
3034 bfd_vma pc;
3035{
3036 bfd_vma offset;
3037
3038 offset = addr - ((pc+3) & -4);
3039 BFD_ASSERT ((offset & ((1 << 2) - 1)) == 0);
3040 offset = (signed int) offset >> 2;
3041 BFD_ASSERT ((signed int) offset >> 16 == -1);
3042 return offset;
3043}
3044
3045
3046/* Get the operand number for a PC-relative relocation.
3047 If the relocation is not a PC-relative one, return (-1). */
3048
3049static int
3050get_relocation_opnd (irel)
3051 Elf_Internal_Rela *irel;
3052{
3053 if (ELF32_R_TYPE (irel->r_info) < R_XTENSA_OP0
3054 || ELF32_R_TYPE (irel->r_info) >= R_XTENSA_max)
3055 return -1;
3056 return ELF32_R_TYPE (irel->r_info) - R_XTENSA_OP0;
3057}
3058
3059
3060/* Get the opcode for a relocation. */
3061
3062static xtensa_opcode
3063get_relocation_opcode (sec, contents, irel)
3064 asection *sec;
3065 bfd_byte *contents;
3066 Elf_Internal_Rela *irel;
3067{
3068 static xtensa_insnbuf ibuff = NULL;
3069 xtensa_isa isa = xtensa_default_isa;
3070
3071 if (get_relocation_opnd (irel) == -1)
3072 return XTENSA_UNDEFINED;
3073
3074 if (contents == NULL)
3075 return XTENSA_UNDEFINED;
3076
3077 if (sec->_raw_size <= irel->r_offset)
3078 return XTENSA_UNDEFINED;
3079
3080 if (ibuff == NULL)
3081 ibuff = xtensa_insnbuf_alloc (isa);
3082
3083 /* Decode the instruction. */
3084 xtensa_insnbuf_from_chars (isa, ibuff, &contents[irel->r_offset]);
3085 return xtensa_decode_insn (isa, ibuff);
3086}
3087
3088
3089bfd_boolean
3090is_l32r_relocation (sec, contents, irel)
3091 asection *sec;
3092 bfd_byte *contents;
3093 Elf_Internal_Rela *irel;
3094{
3095 xtensa_opcode opcode;
3096
3097 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_OP1)
3098 return FALSE;
3099
3100 opcode = get_relocation_opcode (sec, contents, irel);
3101 return (opcode == get_l32r_opcode ());
3102}
3103
3104\f
3105/* Code for transforming CALLs at link-time. */
3106
3107static bfd_reloc_status_type
3108elf_xtensa_do_asm_simplify (contents, address, content_length)
3109 bfd_byte *contents;
3110 bfd_vma address;
3111 bfd_vma content_length;
3112{
3113 static xtensa_insnbuf insnbuf = NULL;
3114 xtensa_opcode opcode;
3115 xtensa_operand operand;
3116 xtensa_opcode direct_call_opcode;
3117 xtensa_isa isa = xtensa_default_isa;
3118 bfd_byte *chbuf = contents + address;
3119 int opn;
3120
3121 if (insnbuf == NULL)
3122 insnbuf = xtensa_insnbuf_alloc (isa);
3123
3124 if (content_length < address)
3125 {
3126 (*_bfd_error_handler)
b536dc1e 3127 ("Attempt to convert L32R/CALLX to CALL failed");
e0001a05
NC
3128 return bfd_reloc_other;
3129 }
3130
3131 opcode = get_expanded_call_opcode (chbuf, content_length - address);
3132 direct_call_opcode = swap_callx_for_call_opcode (opcode);
3133 if (direct_call_opcode == XTENSA_UNDEFINED)
3134 {
3135 (*_bfd_error_handler)
b536dc1e 3136 ("Attempt to convert L32R/CALLX to CALL failed");
e0001a05
NC
3137 return bfd_reloc_other;
3138 }
3139
3140 /* Assemble a NOP ("or a1, a1, a1") into the 0 byte offset. */
3141 opcode = xtensa_opcode_lookup (isa, "or");
3142 xtensa_encode_insn (isa, opcode, insnbuf);
3143 for (opn = 0; opn < 3; opn++)
3144 {
3145 operand = xtensa_get_operand (isa, opcode, opn);
3146 xtensa_operand_set_field (operand, insnbuf, 1);
3147 }
3148 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf);
3149
3150 /* Assemble a CALL ("callN 0") into the 3 byte offset. */
3151 xtensa_encode_insn (isa, direct_call_opcode, insnbuf);
3152 operand = xtensa_get_operand (isa, opcode, 0);
3153 xtensa_operand_set_field (operand, insnbuf, 0);
3154 xtensa_insnbuf_to_chars (isa, insnbuf, chbuf + 3);
3155
3156 return bfd_reloc_ok;
3157}
3158
3159
3160static bfd_reloc_status_type
3161contract_asm_expansion (contents, content_length, irel)
3162 bfd_byte *contents;
3163 bfd_vma content_length;
3164 Elf_Internal_Rela *irel;
3165{
3166 bfd_reloc_status_type retval =
3167 elf_xtensa_do_asm_simplify (contents, irel->r_offset, content_length);
3168
3169 if (retval != bfd_reloc_ok)
3170 return retval;
3171
3172 /* Update the irel->r_offset field so that the right immediate and
3173 the right instruction are modified during the relocation. */
3174 irel->r_offset += 3;
3175 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_XTENSA_OP0);
3176 return bfd_reloc_ok;
3177}
3178
3179
3180static xtensa_opcode
3181swap_callx_for_call_opcode (opcode)
3182 xtensa_opcode opcode;
3183{
3184 init_call_opcodes ();
3185
3186 if (opcode == callx0_op) return call0_op;
3187 if (opcode == callx4_op) return call4_op;
3188 if (opcode == callx8_op) return call8_op;
3189 if (opcode == callx12_op) return call12_op;
3190
3191 /* Return XTENSA_UNDEFINED if the opcode is not an indirect call. */
3192 return XTENSA_UNDEFINED;
3193}
3194
3195
3196/* Check if "buf" is pointing to a "L32R aN; CALLX aN" sequence, and
3197 if so, return the CALLX opcode. If not, return XTENSA_UNDEFINED. */
3198
3199#define L32R_TARGET_REG_OPERAND 0
3200#define CALLN_SOURCE_OPERAND 0
3201
3202static xtensa_opcode
3203get_expanded_call_opcode (buf, bufsize)
3204 bfd_byte *buf;
3205 int bufsize;
3206{
3207 static xtensa_insnbuf insnbuf = NULL;
3208 xtensa_opcode opcode;
3209 xtensa_operand operand;
3210 xtensa_isa isa = xtensa_default_isa;
3211 uint32 regno, call_regno;
3212
3213 /* Buffer must be at least 6 bytes. */
3214 if (bufsize < 6)
3215 return XTENSA_UNDEFINED;
3216
3217 if (insnbuf == NULL)
3218 insnbuf = xtensa_insnbuf_alloc (isa);
3219
3220 xtensa_insnbuf_from_chars (isa, insnbuf, buf);
3221 opcode = xtensa_decode_insn (isa, insnbuf);
3222
3223 if (opcode != get_l32r_opcode ())
3224 return XTENSA_UNDEFINED;
3225
3226 operand = xtensa_get_operand (isa, opcode, L32R_TARGET_REG_OPERAND);
3227 regno = xtensa_operand_decode
3228 (operand, xtensa_operand_get_field (operand, insnbuf));
3229
3230 /* Next instruction should be an CALLXn with operand 0 == regno. */
3231 xtensa_insnbuf_from_chars (isa, insnbuf,
3232 buf + xtensa_insn_length (isa, opcode));
3233 opcode = xtensa_decode_insn (isa, insnbuf);
3234
3235 if (!is_indirect_call_opcode (opcode))
3236 return XTENSA_UNDEFINED;
3237
3238 operand = xtensa_get_operand (isa, opcode, CALLN_SOURCE_OPERAND);
3239 call_regno = xtensa_operand_decode
3240 (operand, xtensa_operand_get_field (operand, insnbuf));
3241 if (call_regno != regno)
3242 return XTENSA_UNDEFINED;
3243
3244 return opcode;
3245}
3246
3247\f
3248/* Data structures used during relaxation. */
3249
3250/* r_reloc: relocation values. */
3251
3252/* Through the relaxation process, we need to keep track of the values
3253 that will result from evaluating relocations. The standard ELF
3254 relocation structure is not sufficient for this purpose because we're
3255 operating on multiple input files at once, so we need to know which
3256 input file a relocation refers to. The r_reloc structure thus
3257 records both the input file (bfd) and ELF relocation.
3258
3259 For efficiency, an r_reloc also contains a "target_offset" field to
3260 cache the target-section-relative offset value that is represented by
3261 the relocation. */
3262
3263typedef struct r_reloc_struct r_reloc;
3264
3265struct r_reloc_struct
3266{
3267 bfd *abfd;
3268 Elf_Internal_Rela rela;
3269 bfd_vma target_offset;
3270};
3271
3272static bfd_boolean r_reloc_is_const
3273 PARAMS ((const r_reloc *));
3274static void r_reloc_init
3275 PARAMS ((r_reloc *, bfd *, Elf_Internal_Rela *));
3276static bfd_vma r_reloc_get_target_offset
3277 PARAMS ((const r_reloc *));
3278static asection *r_reloc_get_section
3279 PARAMS ((const r_reloc *));
3280static bfd_boolean r_reloc_is_defined
3281 PARAMS ((const r_reloc *));
3282static struct elf_link_hash_entry *r_reloc_get_hash_entry
3283 PARAMS ((const r_reloc *));
3284
3285
3286/* The r_reloc structure is included by value in literal_value, but not
3287 every literal_value has an associated relocation -- some are simple
3288 constants. In such cases, we set all the fields in the r_reloc
3289 struct to zero. The r_reloc_is_const function should be used to
3290 detect this case. */
3291
3292static bfd_boolean
3293r_reloc_is_const (r_rel)
3294 const r_reloc *r_rel;
3295{
3296 return (r_rel->abfd == NULL);
3297}
3298
3299
3300static void
3301r_reloc_init (r_rel, abfd, irel)
3302 r_reloc *r_rel;
3303 bfd *abfd;
3304 Elf_Internal_Rela *irel;
3305{
3306 if (irel != NULL)
3307 {
3308 r_rel->rela = *irel;
3309 r_rel->abfd = abfd;
3310 r_rel->target_offset = r_reloc_get_target_offset (r_rel);
3311 }
3312 else
3313 memset (r_rel, 0, sizeof (r_reloc));
3314}
3315
3316
3317static bfd_vma
3318r_reloc_get_target_offset (r_rel)
3319 const r_reloc *r_rel;
3320{
3321 bfd_vma target_offset;
3322 unsigned long r_symndx;
3323
3324 BFD_ASSERT (!r_reloc_is_const (r_rel));
3325 r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3326 target_offset = get_elf_r_symndx_offset (r_rel->abfd, r_symndx);
3327 return (target_offset + r_rel->rela.r_addend);
3328}
3329
3330
3331static struct elf_link_hash_entry *
3332r_reloc_get_hash_entry (r_rel)
3333 const r_reloc *r_rel;
3334{
3335 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3336 return get_elf_r_symndx_hash_entry (r_rel->abfd, r_symndx);
3337}
3338
3339
3340static asection *
3341r_reloc_get_section (r_rel)
3342 const r_reloc *r_rel;
3343{
3344 unsigned long r_symndx = ELF32_R_SYM (r_rel->rela.r_info);
3345 return get_elf_r_symndx_section (r_rel->abfd, r_symndx);
3346}
3347
3348
3349static bfd_boolean
3350r_reloc_is_defined (r_rel)
3351 const r_reloc *r_rel;
3352{
3353 asection *sec = r_reloc_get_section (r_rel);
3354 if (sec == bfd_abs_section_ptr
3355 || sec == bfd_com_section_ptr
3356 || sec == bfd_und_section_ptr)
3357 return FALSE;
3358 return TRUE;
3359}
3360
3361\f
3362/* source_reloc: relocations that reference literal sections. */
3363
3364/* To determine whether literals can be coalesced, we need to first
3365 record all the relocations that reference the literals. The
3366 source_reloc structure below is used for this purpose. The
3367 source_reloc entries are kept in a per-literal-section array, sorted
3368 by offset within the literal section (i.e., target offset).
3369
3370 The source_sec and r_rel.rela.r_offset fields identify the source of
3371 the relocation. The r_rel field records the relocation value, i.e.,
3372 the offset of the literal being referenced. The opnd field is needed
3373 to determine the range of the immediate field to which the relocation
3374 applies, so we can determine whether another literal with the same
3375 value is within range. The is_null field is true when the relocation
3376 is being removed (e.g., when an L32R is being removed due to a CALLX
3377 that is converted to a direct CALL). */
3378
3379typedef struct source_reloc_struct source_reloc;
3380
3381struct source_reloc_struct
3382{
3383 asection *source_sec;
3384 r_reloc r_rel;
3385 xtensa_operand opnd;
3386 bfd_boolean is_null;
3387};
3388
3389
3390static void init_source_reloc
3391 PARAMS ((source_reloc *, asection *, const r_reloc *, xtensa_operand));
3392static source_reloc *find_source_reloc
3393 PARAMS ((source_reloc *, int, asection *, Elf_Internal_Rela *));
3394static int source_reloc_compare
3395 PARAMS ((const PTR, const PTR));
3396
3397
3398static void
3399init_source_reloc (reloc, source_sec, r_rel, opnd)
3400 source_reloc *reloc;
3401 asection *source_sec;
3402 const r_reloc *r_rel;
3403 xtensa_operand opnd;
3404{
3405 reloc->source_sec = source_sec;
3406 reloc->r_rel = *r_rel;
3407 reloc->opnd = opnd;
3408 reloc->is_null = FALSE;
3409}
3410
3411
3412/* Find the source_reloc for a particular source offset and relocation
3413 type. Note that the array is sorted by _target_ offset, so this is
3414 just a linear search. */
3415
3416static source_reloc *
3417find_source_reloc (src_relocs, src_count, sec, irel)
3418 source_reloc *src_relocs;
3419 int src_count;
3420 asection *sec;
3421 Elf_Internal_Rela *irel;
3422{
3423 int i;
3424
3425 for (i = 0; i < src_count; i++)
3426 {
3427 if (src_relocs[i].source_sec == sec
3428 && src_relocs[i].r_rel.rela.r_offset == irel->r_offset
3429 && (ELF32_R_TYPE (src_relocs[i].r_rel.rela.r_info)
3430 == ELF32_R_TYPE (irel->r_info)))
3431 return &src_relocs[i];
3432 }
3433
3434 return NULL;
3435}
3436
3437
3438static int
3439source_reloc_compare (ap, bp)
3440 const PTR ap;
3441 const PTR bp;
3442{
3443 const source_reloc *a = (const source_reloc *) ap;
3444 const source_reloc *b = (const source_reloc *) bp;
3445
3446 return (a->r_rel.target_offset - b->r_rel.target_offset);
3447}
3448
3449\f
3450/* Literal values and value hash tables. */
3451
3452/* Literals with the same value can be coalesced. The literal_value
3453 structure records the value of a literal: the "r_rel" field holds the
3454 information from the relocation on the literal (if there is one) and
3455 the "value" field holds the contents of the literal word itself.
3456
3457 The value_map structure records a literal value along with the
3458 location of a literal holding that value. The value_map hash table
3459 is indexed by the literal value, so that we can quickly check if a
3460 particular literal value has been seen before and is thus a candidate
3461 for coalescing. */
3462
3463typedef struct literal_value_struct literal_value;
3464typedef struct value_map_struct value_map;
3465typedef struct value_map_hash_table_struct value_map_hash_table;
3466
3467struct literal_value_struct
3468{
3469 r_reloc r_rel;
3470 unsigned long value;
3471};
3472
3473struct value_map_struct
3474{
3475 literal_value val; /* The literal value. */
3476 r_reloc loc; /* Location of the literal. */
3477 value_map *next;
3478};
3479
3480struct value_map_hash_table_struct
3481{
3482 unsigned bucket_count;
3483 value_map **buckets;
3484 unsigned count;
3485};
3486
3487
3488static bfd_boolean is_same_value
3489 PARAMS ((const literal_value *, const literal_value *));
3490static value_map_hash_table *value_map_hash_table_init
3491 PARAMS ((void));
3492static unsigned hash_literal_value
3493 PARAMS ((const literal_value *));
3494static unsigned hash_bfd_vma
3495 PARAMS ((bfd_vma));
3496static value_map *get_cached_value
3497 PARAMS ((value_map_hash_table *, const literal_value *));
3498static value_map *add_value_map
3499 PARAMS ((value_map_hash_table *, const literal_value *, const r_reloc *));
3500
3501
3502static bfd_boolean
3503is_same_value (src1, src2)
3504 const literal_value *src1;
3505 const literal_value *src2;
3506{
3507 if (r_reloc_is_const (&src1->r_rel) != r_reloc_is_const (&src2->r_rel))
3508 return FALSE;
3509
3510 if (r_reloc_is_const (&src1->r_rel))
3511 return (src1->value == src2->value);
3512
3513 if (ELF32_R_TYPE (src1->r_rel.rela.r_info)
3514 != ELF32_R_TYPE (src2->r_rel.rela.r_info))
3515 return FALSE;
3516
3517 if (r_reloc_get_target_offset (&src1->r_rel)
3518 != r_reloc_get_target_offset (&src2->r_rel))
3519 return FALSE;
3520
3521 if (src1->value != src2->value)
3522 return FALSE;
3523
3524 /* Now check for the same section and the same elf_hash. */
3525 if (r_reloc_is_defined (&src1->r_rel))
3526 {
3527 if (r_reloc_get_section (&src1->r_rel)
3528 != r_reloc_get_section (&src2->r_rel))
3529 return FALSE;
3530 }
3531 else
3532 {
3533 if (r_reloc_get_hash_entry (&src1->r_rel)
3534 != r_reloc_get_hash_entry (&src2->r_rel))
3535 return FALSE;
3536
3537 if (r_reloc_get_hash_entry (&src1->r_rel) == 0)
3538 return FALSE;
3539 }
3540
3541 return TRUE;
3542}
3543
3544
3545/* Must be power of 2. */
3546#define INITIAL_HASH_RELOC_BUCKET_COUNT 1024
3547
3548static value_map_hash_table *
3549value_map_hash_table_init ()
3550{
3551 value_map_hash_table *values;
3552
3553 values = (value_map_hash_table *)
3554 bfd_malloc (sizeof (value_map_hash_table));
3555
3556 values->bucket_count = INITIAL_HASH_RELOC_BUCKET_COUNT;
3557 values->count = 0;
3558 values->buckets = (value_map **)
3559 bfd_zmalloc (sizeof (value_map *) * values->bucket_count);
3560
3561 return values;
3562}
3563
3564
3565static unsigned
3566hash_bfd_vma (val)
3567 bfd_vma val;
3568{
3569 return (val >> 2) + (val >> 10);
3570}
3571
3572
3573static unsigned
3574hash_literal_value (src)
3575 const literal_value *src;
3576{
3577 unsigned hash_val;
3578 if (r_reloc_is_const (&src->r_rel))
3579 return hash_bfd_vma (src->value);
3580
3581 hash_val = (hash_bfd_vma (r_reloc_get_target_offset (&src->r_rel))
3582 + hash_bfd_vma (src->value));
3583
3584 /* Now check for the same section and the same elf_hash. */
3585 if (r_reloc_is_defined (&src->r_rel))
3586 hash_val += hash_bfd_vma ((bfd_vma) r_reloc_get_section (&src->r_rel));
3587 else
3588 hash_val += hash_bfd_vma ((bfd_vma) r_reloc_get_hash_entry (&src->r_rel));
3589
3590 return hash_val;
3591}
3592
3593
3594/* Check if the specified literal_value has been seen before. */
3595
3596static value_map *
3597get_cached_value (map, val)
3598 value_map_hash_table *map;
3599 const literal_value *val;
3600{
3601 value_map *map_e;
3602 value_map *bucket;
3603 unsigned idx;
3604
3605 idx = hash_literal_value (val);
3606 idx = idx & (map->bucket_count - 1);
3607 bucket = map->buckets[idx];
3608 for (map_e = bucket; map_e; map_e = map_e->next)
3609 {
3610 if (is_same_value (&map_e->val, val))
3611 return map_e;
3612 }
3613 return NULL;
3614}
3615
3616
3617/* Record a new literal value. It is illegal to call this if VALUE
3618 already has an entry here. */
3619
3620static value_map *
3621add_value_map (map, val, loc)
3622 value_map_hash_table *map;
3623 const literal_value *val;
3624 const r_reloc *loc;
3625{
3626 value_map **bucket_p;
3627 unsigned idx;
3628
3629 value_map *val_e = (value_map *) bfd_zmalloc (sizeof (value_map));
3630
3631 BFD_ASSERT (get_cached_value (map, val) == NULL);
3632 val_e->val = *val;
3633 val_e->loc = *loc;
3634
3635 idx = hash_literal_value (val);
3636 idx = idx & (map->bucket_count - 1);
3637 bucket_p = &map->buckets[idx];
3638
3639 val_e->next = *bucket_p;
3640 *bucket_p = val_e;
3641 map->count++;
3642 /* FIXME: consider resizing the hash table if we get too many entries */
3643
3644 return val_e;
3645}
3646
3647\f
3648/* Lists of literals being coalesced or removed. */
3649
3650/* In the usual case, the literal identified by "from" is being
3651 coalesced with another literal identified by "to". If the literal is
3652 unused and is being removed altogether, "to.abfd" will be NULL.
3653 The removed_literal entries are kept on a per-section list, sorted
3654 by the "from" offset field. */
3655
3656typedef struct removed_literal_struct removed_literal;
3657typedef struct removed_literal_list_struct removed_literal_list;
3658
3659struct removed_literal_struct
3660{
3661 r_reloc from;
3662 r_reloc to;
3663 removed_literal *next;
3664};
3665
3666struct removed_literal_list_struct
3667{
3668 removed_literal *head;
3669 removed_literal *tail;
3670};
3671
3672
3673static void add_removed_literal
3674 PARAMS ((removed_literal_list *, const r_reloc *, const r_reloc *));
3675static removed_literal *find_removed_literal
3676 PARAMS ((removed_literal_list *, bfd_vma));
3677static bfd_vma offset_with_removed_literals
3678 PARAMS ((removed_literal_list *, bfd_vma));
3679
3680
3681/* Record that the literal at "from" is being removed. If "to" is not
3682 NULL, the "from" literal is being coalesced with the "to" literal. */
3683
3684static void
3685add_removed_literal (removed_list, from, to)
3686 removed_literal_list *removed_list;
3687 const r_reloc *from;
3688 const r_reloc *to;
3689{
3690 removed_literal *r, *new_r, *next_r;
3691
3692 new_r = (removed_literal *) bfd_zmalloc (sizeof (removed_literal));
3693
3694 new_r->from = *from;
3695 if (to)
3696 new_r->to = *to;
3697 else
3698 new_r->to.abfd = NULL;
3699 new_r->next = NULL;
3700
3701 r = removed_list->head;
3702 if (r == NULL)
3703 {
3704 removed_list->head = new_r;
3705 removed_list->tail = new_r;
3706 }
3707 /* Special check for common case of append. */
3708 else if (removed_list->tail->from.target_offset < from->target_offset)
3709 {
3710 removed_list->tail->next = new_r;
3711 removed_list->tail = new_r;
3712 }
3713 else
3714 {
3715 while (r->from.target_offset < from->target_offset
3716 && r->next != NULL)
3717 {
3718 r = r->next;
3719 }
3720 next_r = r->next;
3721 r->next = new_r;
3722 new_r->next = next_r;
3723 if (next_r == NULL)
3724 removed_list->tail = new_r;
3725 }
3726}
3727
3728
3729/* Check if the list of removed literals contains an entry for the
3730 given address. Return the entry if found. */
3731
3732static removed_literal *
3733find_removed_literal (removed_list, addr)
3734 removed_literal_list *removed_list;
3735 bfd_vma addr;
3736{
3737 removed_literal *r = removed_list->head;
3738 while (r && r->from.target_offset < addr)
3739 r = r->next;
3740 if (r && r->from.target_offset == addr)
3741 return r;
3742 return NULL;
3743}
3744
3745
3746/* Adjust an offset in a section to compensate for literals that are
3747 being removed. Search the list of removed literals and subtract
3748 4 bytes for every removed literal prior to the given address. */
3749
3750static bfd_vma
3751offset_with_removed_literals (removed_list, addr)
3752 removed_literal_list *removed_list;
3753 bfd_vma addr;
3754{
3755 removed_literal *r = removed_list->head;
3756 unsigned num_bytes = 0;
3757
3758 if (r == NULL)
3759 return addr;
3760
3761 while (r && r->from.target_offset <= addr)
3762 {
3763 num_bytes += 4;
3764 r = r->next;
3765 }
3766 if (num_bytes > addr)
3767 return 0;
3768 return (addr - num_bytes);
3769}
3770
3771\f
3772/* Coalescing literals may require a relocation to refer to a section in
3773 a different input file, but the standard relocation information
3774 cannot express that. Instead, the reloc_bfd_fix structures are used
3775 to "fix" the relocations that refer to sections in other input files.
3776 These structures are kept on per-section lists. The "src_type" field
3777 records the relocation type in case there are multiple relocations on
3778 the same location. FIXME: This is ugly; an alternative might be to
3779 add new symbols with the "owner" field to some other input file. */
3780
3781typedef struct reloc_bfd_fix_struct reloc_bfd_fix;
3782
3783struct reloc_bfd_fix_struct
3784{
3785 asection *src_sec;
3786 bfd_vma src_offset;
3787 unsigned src_type; /* Relocation type. */
3788
3789 bfd *target_abfd;
3790 asection *target_sec;
3791 bfd_vma target_offset;
3792
3793 reloc_bfd_fix *next;
3794};
3795
3796
3797static reloc_bfd_fix *reloc_bfd_fix_init
3798 PARAMS ((asection *, bfd_vma, unsigned, bfd *, asection *, bfd_vma));
3799static reloc_bfd_fix *get_bfd_fix
3800 PARAMS ((reloc_bfd_fix *, asection *, bfd_vma, unsigned));
3801
3802
3803static reloc_bfd_fix *
3804reloc_bfd_fix_init (src_sec, src_offset, src_type,
3805 target_abfd, target_sec, target_offset)
3806 asection *src_sec;
3807 bfd_vma src_offset;
3808 unsigned src_type;
3809 bfd *target_abfd;
3810 asection *target_sec;
3811 bfd_vma target_offset;
3812{
3813 reloc_bfd_fix *fix;
3814
3815 fix = (reloc_bfd_fix *) bfd_malloc (sizeof (reloc_bfd_fix));
3816 fix->src_sec = src_sec;
3817 fix->src_offset = src_offset;
3818 fix->src_type = src_type;
3819 fix->target_abfd = target_abfd;
3820 fix->target_sec = target_sec;
3821 fix->target_offset = target_offset;
3822
3823 return fix;
3824}
3825
3826
3827static reloc_bfd_fix *
3828get_bfd_fix (fix_list, sec, offset, type)
3829 reloc_bfd_fix *fix_list;
3830 asection *sec;
3831 bfd_vma offset;
3832 unsigned type;
3833{
3834 reloc_bfd_fix *r;
3835
3836 for (r = fix_list; r != NULL; r = r->next)
3837 {
3838 if (r->src_sec == sec
3839 && r->src_offset == offset
3840 && r->src_type == type)
3841 return r;
3842 }
3843 return NULL;
3844}
3845
3846\f
3847/* Per-section data for relaxation. */
3848
3849struct xtensa_relax_info_struct
3850{
3851 bfd_boolean is_relaxable_literal_section;
3852 int visited; /* Number of times visited. */
3853
3854 source_reloc *src_relocs; /* Array[src_count]. */
3855 int src_count;
3856 int src_next; /* Next src_relocs entry to assign. */
3857
3858 removed_literal_list removed_list;
3859
3860 reloc_bfd_fix *fix_list;
3861};
3862
3863struct elf_xtensa_section_data
3864{
3865 struct bfd_elf_section_data elf;
3866 xtensa_relax_info relax_info;
3867};
3868
3869static void init_xtensa_relax_info
3870 PARAMS ((asection *));
3871static xtensa_relax_info *get_xtensa_relax_info
3872 PARAMS ((asection *));
3873static void add_fix
3874 PARAMS ((asection *, reloc_bfd_fix *));
3875
3876
3877static bfd_boolean
3878elf_xtensa_new_section_hook (abfd, sec)
3879 bfd *abfd;
3880 asection *sec;
3881{
3882 struct elf_xtensa_section_data *sdata;
3883 bfd_size_type amt = sizeof (*sdata);
3884
3885 sdata = (struct elf_xtensa_section_data *) bfd_zalloc (abfd, amt);
3886 if (sdata == NULL)
3887 return FALSE;
3888 sec->used_by_bfd = (PTR) sdata;
3889
3890 return _bfd_elf_new_section_hook (abfd, sec);
3891}
3892
3893
3894static void
3895init_xtensa_relax_info (sec)
3896 asection *sec;
3897{
3898 xtensa_relax_info *relax_info = get_xtensa_relax_info (sec);
3899
3900 relax_info->is_relaxable_literal_section = FALSE;
3901 relax_info->visited = 0;
3902
3903 relax_info->src_relocs = NULL;
3904 relax_info->src_count = 0;
3905 relax_info->src_next = 0;
3906
3907 relax_info->removed_list.head = NULL;
3908 relax_info->removed_list.tail = NULL;
3909
3910 relax_info->fix_list = NULL;
3911}
3912
3913
3914static xtensa_relax_info *
3915get_xtensa_relax_info (sec)
3916 asection *sec;
3917{
3918 struct elf_xtensa_section_data *section_data;
3919
3920 /* No info available if no section or if it is an output section. */
3921 if (!sec || sec == sec->output_section)
3922 return NULL;
3923
3924 section_data = (struct elf_xtensa_section_data *) elf_section_data (sec);
3925 return &section_data->relax_info;
3926}
3927
3928
3929static void
3930add_fix (src_sec, fix)
3931 asection *src_sec;
3932 reloc_bfd_fix *fix;
3933{
3934 xtensa_relax_info *relax_info;
3935
3936 relax_info = get_xtensa_relax_info (src_sec);
3937 fix->next = relax_info->fix_list;
3938 relax_info->fix_list = fix;
3939}
3940
3941\f
3942/* Access to internal relocations, section contents and symbols. */
3943
3944/* During relaxation, we need to modify relocations, section contents,
3945 and symbol definitions, and we need to keep the original values from
3946 being reloaded from the input files, i.e., we need to "pin" the
3947 modified values in memory. We also want to continue to observe the
3948 setting of the "keep-memory" flag. The following functions wrap the
3949 standard BFD functions to take care of this for us. */
3950
3951static Elf_Internal_Rela *
3952retrieve_internal_relocs (abfd, sec, keep_memory)
3953 bfd *abfd;
3954 asection *sec;
3955 bfd_boolean keep_memory;
3956{
3957 Elf_Internal_Rela *internal_relocs;
3958
3959 if ((sec->flags & SEC_LINKER_CREATED) != 0)
3960 return NULL;
3961
3962 internal_relocs = elf_section_data (sec)->relocs;
3963 if (internal_relocs == NULL)
45d6a902 3964 internal_relocs = (_bfd_elf_link_read_relocs
e0001a05
NC
3965 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3966 keep_memory));
3967 return internal_relocs;
3968}
3969
3970
3971static void
3972pin_internal_relocs (sec, internal_relocs)
3973 asection *sec;
3974 Elf_Internal_Rela *internal_relocs;
3975{
3976 elf_section_data (sec)->relocs = internal_relocs;
3977}
3978
3979
3980static void
3981release_internal_relocs (sec, internal_relocs)
3982 asection *sec;
3983 Elf_Internal_Rela *internal_relocs;
3984{
3985 if (internal_relocs
3986 && elf_section_data (sec)->relocs != internal_relocs)
3987 free (internal_relocs);
3988}
3989
3990
3991static bfd_byte *
3992retrieve_contents (abfd, sec, keep_memory)
3993 bfd *abfd;
3994 asection *sec;
3995 bfd_boolean keep_memory;
3996{
3997 bfd_byte *contents;
3998
3999 contents = elf_section_data (sec)->this_hdr.contents;
4000
4001 if (contents == NULL && sec->_raw_size != 0)
4002 {
4003 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
4004 if (contents != NULL)
4005 {
4006 if (! bfd_get_section_contents (abfd, sec, contents,
4007 (file_ptr) 0, sec->_raw_size))
4008 {
4009 free (contents);
4010 return NULL;
4011 }
4012 if (keep_memory)
4013 elf_section_data (sec)->this_hdr.contents = contents;
4014 }
4015 }
4016 return contents;
4017}
4018
4019
4020static void
4021pin_contents (sec, contents)
4022 asection *sec;
4023 bfd_byte *contents;
4024{
4025 elf_section_data (sec)->this_hdr.contents = contents;
4026}
4027
4028
4029static void
4030release_contents (sec, contents)
4031 asection *sec;
4032 bfd_byte *contents;
4033{
4034 if (contents &&
4035 elf_section_data (sec)->this_hdr.contents != contents)
4036 free (contents);
4037}
4038
4039
4040static Elf_Internal_Sym *
4041retrieve_local_syms (input_bfd)
4042 bfd *input_bfd;
4043{
4044 Elf_Internal_Shdr *symtab_hdr;
4045 Elf_Internal_Sym *isymbuf;
4046 size_t locsymcount;
4047
4048 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4049 locsymcount = symtab_hdr->sh_info;
4050
4051 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4052 if (isymbuf == NULL && locsymcount != 0)
4053 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4054 NULL, NULL, NULL);
4055
4056 /* Save the symbols for this input file so they won't be read again. */
4057 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
4058 symtab_hdr->contents = (unsigned char *) isymbuf;
4059
4060 return isymbuf;
4061}
4062
4063\f
4064/* Code for link-time relaxation. */
4065
4066/* Local helper functions. */
4067static bfd_boolean analyze_relocations
4068 PARAMS ((struct bfd_link_info *));
4069static bfd_boolean find_relaxable_sections
4070 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
4071static bfd_boolean collect_source_relocs
4072 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4073static bfd_boolean is_resolvable_asm_expansion
4074 PARAMS ((bfd *, asection *, bfd_byte *, Elf_Internal_Rela *,
4075 struct bfd_link_info *, bfd_boolean *));
4076static bfd_boolean remove_literals
4077 PARAMS ((bfd *, asection *, struct bfd_link_info *, value_map_hash_table *));
4078static bfd_boolean relax_section
4079 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4080static bfd_boolean relax_property_section
4081 PARAMS ((bfd *, asection *, struct bfd_link_info *));
4082static bfd_boolean relax_section_symbols
4083 PARAMS ((bfd *, asection *));
4084static bfd_boolean relocations_reach
4085 PARAMS ((source_reloc *, int, const r_reloc *));
4086static void translate_reloc
4087 PARAMS ((const r_reloc *, r_reloc *));
4088static Elf_Internal_Rela *get_irel_at_offset
4089 PARAMS ((asection *, Elf_Internal_Rela *, bfd_vma));
4090static Elf_Internal_Rela *find_associated_l32r_irel
4091 PARAMS ((asection *, bfd_byte *, Elf_Internal_Rela *,
4092 Elf_Internal_Rela *));
4093static void shrink_dynamic_reloc_sections
4094 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *));
4095
4096
4097static bfd_boolean
4098elf_xtensa_relax_section (abfd, sec, link_info, again)
4099 bfd *abfd;
4100 asection *sec;
4101 struct bfd_link_info *link_info;
4102 bfd_boolean *again;
4103{
4104 static value_map_hash_table *values = NULL;
4105 xtensa_relax_info *relax_info;
4106
4107 if (!values)
4108 {
4109 /* Do some overall initialization for relaxation. */
4110 values = value_map_hash_table_init ();
4111 relaxing_section = TRUE;
4112 if (!analyze_relocations (link_info))
4113 return FALSE;
4114 }
4115 *again = FALSE;
4116
4117 /* Don't mess with linker-created sections. */
4118 if ((sec->flags & SEC_LINKER_CREATED) != 0)
4119 return TRUE;
4120
4121 relax_info = get_xtensa_relax_info (sec);
4122 BFD_ASSERT (relax_info != NULL);
4123
4124 switch (relax_info->visited)
4125 {
4126 case 0:
4127 /* Note: It would be nice to fold this pass into
4128 analyze_relocations, but it is important for this step that the
4129 sections be examined in link order. */
4130 if (!remove_literals (abfd, sec, link_info, values))
4131 return FALSE;
4132 *again = TRUE;
4133 break;
4134
4135 case 1:
4136 if (!relax_section (abfd, sec, link_info))
4137 return FALSE;
4138 *again = TRUE;
4139 break;
4140
4141 case 2:
4142 if (!relax_section_symbols (abfd, sec))
4143 return FALSE;
4144 break;
4145 }
4146
4147 relax_info->visited++;
4148 return TRUE;
4149}
4150
4151/* Initialization for relaxation. */
4152
4153/* This function is called once at the start of relaxation. It scans
4154 all the input sections and marks the ones that are relaxable (i.e.,
4155 literal sections with L32R relocations against them). It then
4156 collect source_reloc information for all the relocations against
4157 those relaxable sections. */
4158
4159static bfd_boolean
4160analyze_relocations (link_info)
4161 struct bfd_link_info *link_info;
4162{
4163 bfd *abfd;
4164 asection *sec;
4165 bfd_boolean is_relaxable = FALSE;
4166
4167 /* Initialize the per-section relaxation info. */
4168 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4169 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4170 {
4171 init_xtensa_relax_info (sec);
4172 }
4173
4174 /* Mark relaxable sections (and count relocations against each one). */
4175 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4176 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4177 {
4178 if (!find_relaxable_sections (abfd, sec, link_info, &is_relaxable))
4179 return FALSE;
4180 }
4181
4182 /* Bail out if there are no relaxable sections. */
4183 if (!is_relaxable)
4184 return TRUE;
4185
4186 /* Allocate space for source_relocs. */
4187 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4188 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4189 {
4190 xtensa_relax_info *relax_info;
4191
4192 relax_info = get_xtensa_relax_info (sec);
4193 if (relax_info->is_relaxable_literal_section)
4194 {
4195 relax_info->src_relocs = (source_reloc *)
4196 bfd_malloc (relax_info->src_count * sizeof (source_reloc));
4197 }
4198 }
4199
4200 /* Collect info on relocations against each relaxable section. */
4201 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link_next)
4202 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4203 {
4204 if (!collect_source_relocs (abfd, sec, link_info))
4205 return FALSE;
4206 }
4207
4208 return TRUE;
4209}
4210
4211
4212/* Find all the literal sections that might be relaxed. The motivation
4213 for this pass is that collect_source_relocs() needs to record _all_
4214 the relocations that target each relaxable section. That is
4215 expensive and unnecessary unless the target section is actually going
4216 to be relaxed. This pass identifies all such sections by checking if
4217 they have L32Rs pointing to them. In the process, the total number
4218 of relocations targetting each section is also counted so that we
4219 know how much space to allocate for source_relocs against each
4220 relaxable literal section. */
4221
4222static bfd_boolean
4223find_relaxable_sections (abfd, sec, link_info, is_relaxable_p)
4224 bfd *abfd;
4225 asection *sec;
4226 struct bfd_link_info *link_info;
4227 bfd_boolean *is_relaxable_p;
4228{
4229 Elf_Internal_Rela *internal_relocs;
4230 bfd_byte *contents;
4231 bfd_boolean ok = TRUE;
4232 unsigned i;
4233
4234 internal_relocs = retrieve_internal_relocs (abfd, sec,
4235 link_info->keep_memory);
4236 if (internal_relocs == NULL)
4237 return ok;
4238
4239 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4240 if (contents == NULL && sec->_raw_size != 0)
4241 {
4242 ok = FALSE;
4243 goto error_return;
4244 }
4245
4246 for (i = 0; i < sec->reloc_count; i++)
4247 {
4248 Elf_Internal_Rela *irel = &internal_relocs[i];
4249 r_reloc r_rel;
4250 asection *target_sec;
4251 xtensa_relax_info *target_relax_info;
4252
4253 r_reloc_init (&r_rel, abfd, irel);
4254
4255 target_sec = r_reloc_get_section (&r_rel);
4256 target_relax_info = get_xtensa_relax_info (target_sec);
4257 if (!target_relax_info)
4258 continue;
4259
4260 /* Count relocations against the target section. */
4261 target_relax_info->src_count++;
4262
4263 if (is_literal_section (target_sec)
4264 && is_l32r_relocation (sec, contents, irel)
4265 && r_reloc_is_defined (&r_rel))
4266 {
4267 /* Mark the target section as relaxable. */
4268 target_relax_info->is_relaxable_literal_section = TRUE;
4269 *is_relaxable_p = TRUE;
4270 }
4271 }
4272
4273 error_return:
4274 release_contents (sec, contents);
4275 release_internal_relocs (sec, internal_relocs);
4276 return ok;
4277}
4278
4279
4280/* Record _all_ the relocations that point to relaxable literal
4281 sections, and get rid of ASM_EXPAND relocs by either converting them
4282 to ASM_SIMPLIFY or by removing them. */
4283
4284static bfd_boolean
4285collect_source_relocs (abfd, sec, link_info)
4286 bfd *abfd;
4287 asection *sec;
4288 struct bfd_link_info *link_info;
4289{
4290 Elf_Internal_Rela *internal_relocs;
4291 bfd_byte *contents;
4292 bfd_boolean ok = TRUE;
4293 unsigned i;
4294
4295 internal_relocs = retrieve_internal_relocs (abfd, sec,
4296 link_info->keep_memory);
4297 if (internal_relocs == NULL)
4298 return ok;
4299
4300 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4301 if (contents == NULL && sec->_raw_size != 0)
4302 {
4303 ok = FALSE;
4304 goto error_return;
4305 }
4306
4307 /* Record relocations against relaxable literal sections. */
4308 for (i = 0; i < sec->reloc_count; i++)
4309 {
4310 Elf_Internal_Rela *irel = &internal_relocs[i];
4311 r_reloc r_rel;
4312 asection *target_sec;
4313 xtensa_relax_info *target_relax_info;
4314
4315 r_reloc_init (&r_rel, abfd, irel);
4316
4317 target_sec = r_reloc_get_section (&r_rel);
4318 target_relax_info = get_xtensa_relax_info (target_sec);
4319
4320 if (target_relax_info
4321 && target_relax_info->is_relaxable_literal_section)
4322 {
4323 xtensa_opcode opcode;
4324 xtensa_operand opnd;
4325 source_reloc *s_reloc;
4326 int src_next;
4327
4328 src_next = target_relax_info->src_next++;
4329 s_reloc = &target_relax_info->src_relocs[src_next];
4330
4331 opcode = get_relocation_opcode (sec, contents, irel);
4332 if (opcode == XTENSA_UNDEFINED)
4333 opnd = NULL;
4334 else
4335 opnd = xtensa_get_operand (xtensa_default_isa, opcode,
4336 get_relocation_opnd (irel));
4337
4338 init_source_reloc (s_reloc, sec, &r_rel, opnd);
4339 }
4340 }
4341
4342 /* Now get rid of ASM_EXPAND relocations. At this point, the
4343 src_relocs array for the target literal section may still be
4344 incomplete, but it must at least contain the entries for the L32R
4345 relocations associated with ASM_EXPANDs because they were just
4346 added in the preceding loop over the relocations. */
4347
4348 for (i = 0; i < sec->reloc_count; i++)
4349 {
4350 Elf_Internal_Rela *irel = &internal_relocs[i];
4351 bfd_boolean is_reachable;
4352
4353 if (!is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4354 &is_reachable))
4355 continue;
4356
4357 if (is_reachable)
4358 {
4359 Elf_Internal_Rela *l32r_irel;
4360 r_reloc r_rel;
4361 asection *target_sec;
4362 xtensa_relax_info *target_relax_info;
4363
4364 /* Mark the source_reloc for the L32R so that it will be
4365 removed in remove_literals(), along with the associated
4366 literal. */
4367 l32r_irel = find_associated_l32r_irel (sec, contents,
4368 irel, internal_relocs);
4369 if (l32r_irel == NULL)
4370 continue;
4371
4372 r_reloc_init (&r_rel, abfd, l32r_irel);
4373
4374 target_sec = r_reloc_get_section (&r_rel);
4375 target_relax_info = get_xtensa_relax_info (target_sec);
4376
4377 if (target_relax_info
4378 && target_relax_info->is_relaxable_literal_section)
4379 {
4380 source_reloc *s_reloc;
4381
4382 /* Search the source_relocs for the entry corresponding to
4383 the l32r_irel. Note: The src_relocs array is not yet
4384 sorted, but it wouldn't matter anyway because we're
4385 searching by source offset instead of target offset. */
4386 s_reloc = find_source_reloc (target_relax_info->src_relocs,
4387 target_relax_info->src_next,
4388 sec, l32r_irel);
4389 BFD_ASSERT (s_reloc);
4390 s_reloc->is_null = TRUE;
4391 }
4392
4393 /* Convert this reloc to ASM_SIMPLIFY. */
4394 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4395 R_XTENSA_ASM_SIMPLIFY);
4396 l32r_irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4397
4398 pin_internal_relocs (sec, internal_relocs);
4399 }
4400 else
4401 {
4402 /* It is resolvable but doesn't reach. We resolve now
4403 by eliminating the relocation -- the call will remain
4404 expanded into L32R/CALLX. */
4405 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4406 pin_internal_relocs (sec, internal_relocs);
4407 }
4408 }
4409
4410 error_return:
4411 release_contents (sec, contents);
4412 release_internal_relocs (sec, internal_relocs);
4413 return ok;
4414}
4415
4416
4417/* Return TRUE if the asm expansion can be resolved. Generally it can
4418 be resolved on a final link or when a partial link locates it in the
4419 same section as the target. Set "is_reachable" flag if the target of
4420 the call is within the range of a direct call, given the current VMA
4421 for this section and the target section. */
4422
4423bfd_boolean
4424is_resolvable_asm_expansion (abfd, sec, contents, irel, link_info,
4425 is_reachable_p)
4426 bfd *abfd;
4427 asection *sec;
4428 bfd_byte *contents;
4429 Elf_Internal_Rela *irel;
4430 struct bfd_link_info *link_info;
4431 bfd_boolean *is_reachable_p;
4432{
4433 asection *target_sec;
4434 bfd_vma target_offset;
4435 r_reloc r_rel;
4436 xtensa_opcode opcode, direct_call_opcode;
4437 bfd_vma self_address;
4438 bfd_vma dest_address;
4439
4440 *is_reachable_p = FALSE;
4441
4442 if (contents == NULL)
4443 return FALSE;
4444
4445 if (ELF32_R_TYPE (irel->r_info) != R_XTENSA_ASM_EXPAND)
4446 return FALSE;
4447
4448 opcode = get_expanded_call_opcode (contents + irel->r_offset,
4449 sec->_raw_size - irel->r_offset);
4450
4451 direct_call_opcode = swap_callx_for_call_opcode (opcode);
4452 if (direct_call_opcode == XTENSA_UNDEFINED)
4453 return FALSE;
4454
4455 /* Check and see that the target resolves. */
4456 r_reloc_init (&r_rel, abfd, irel);
4457 if (!r_reloc_is_defined (&r_rel))
4458 return FALSE;
4459
4460 target_sec = r_reloc_get_section (&r_rel);
4461 target_offset = r_reloc_get_target_offset (&r_rel);
4462
4463 /* If the target is in a shared library, then it doesn't reach. This
4464 isn't supposed to come up because the compiler should never generate
4465 non-PIC calls on systems that use shared libraries, but the linker
4466 shouldn't crash regardless. */
4467 if (!target_sec->output_section)
4468 return FALSE;
4469
1049f94e 4470 /* For relocatable sections, we can only simplify when the output
e0001a05
NC
4471 section of the target is the same as the output section of the
4472 source. */
1049f94e 4473 if (link_info->relocatable
e0001a05
NC
4474 && (target_sec->output_section != sec->output_section))
4475 return FALSE;
4476
4477 self_address = (sec->output_section->vma
4478 + sec->output_offset + irel->r_offset + 3);
4479 dest_address = (target_sec->output_section->vma
4480 + target_sec->output_offset + target_offset);
4481
4482 *is_reachable_p = pcrel_reloc_fits
4483 (xtensa_get_operand (xtensa_default_isa, direct_call_opcode, 0),
4484 self_address, dest_address);
4485
4486 if ((self_address >> CALL_SEGMENT_BITS) !=
4487 (dest_address >> CALL_SEGMENT_BITS))
4488 return FALSE;
4489
4490 return TRUE;
4491}
4492
4493
4494static Elf_Internal_Rela *
4495find_associated_l32r_irel (sec, contents, other_irel, internal_relocs)
4496 asection *sec;
4497 bfd_byte *contents;
4498 Elf_Internal_Rela *other_irel;
4499 Elf_Internal_Rela *internal_relocs;
4500{
4501 unsigned i;
4502
4503 for (i = 0; i < sec->reloc_count; i++)
4504 {
4505 Elf_Internal_Rela *irel = &internal_relocs[i];
4506
4507 if (irel == other_irel)
4508 continue;
4509 if (irel->r_offset != other_irel->r_offset)
4510 continue;
4511 if (is_l32r_relocation (sec, contents, irel))
4512 return irel;
4513 }
4514
4515 return NULL;
4516}
4517
4518/* First relaxation pass. */
4519
4520/* If the section is relaxable (i.e., a literal section), check each
4521 literal to see if it has the same value as another literal that has
4522 already been seen, either in the current section or a previous one.
4523 If so, add an entry to the per-section list of removed literals. The
4524 actual changes are deferred until the next pass. */
4525
4526static bfd_boolean
4527remove_literals (abfd, sec, link_info, values)
4528 bfd *abfd;
4529 asection *sec;
4530 struct bfd_link_info *link_info;
4531 value_map_hash_table *values;
4532{
4533 xtensa_relax_info *relax_info;
4534 bfd_byte *contents;
4535 Elf_Internal_Rela *internal_relocs;
4536 source_reloc *src_relocs;
4537 bfd_boolean ok = TRUE;
4538 int i;
4539
4540 /* Do nothing if it is not a relaxable literal section. */
4541 relax_info = get_xtensa_relax_info (sec);
4542 BFD_ASSERT (relax_info);
4543
4544 if (!relax_info->is_relaxable_literal_section)
4545 return ok;
4546
4547 internal_relocs = retrieve_internal_relocs (abfd, sec,
4548 link_info->keep_memory);
4549
4550 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4551 if (contents == NULL && sec->_raw_size != 0)
4552 {
4553 ok = FALSE;
4554 goto error_return;
4555 }
4556
4557 /* Sort the source_relocs by target offset. */
4558 src_relocs = relax_info->src_relocs;
4559 qsort (src_relocs, relax_info->src_count,
4560 sizeof (source_reloc), source_reloc_compare);
4561
4562 for (i = 0; i < relax_info->src_count; i++)
4563 {
4564 source_reloc *rel;
4565 Elf_Internal_Rela *irel = NULL;
4566 literal_value val;
4567 value_map *val_map;
4568
4569 rel = &src_relocs[i];
4570 irel = get_irel_at_offset (sec, internal_relocs,
4571 rel->r_rel.target_offset);
4572
4573 /* If the target_offset for this relocation is the same as the
4574 previous relocation, then we've already considered whether the
4575 literal can be coalesced. Skip to the next one.... */
4576 if (i != 0 && (src_relocs[i-1].r_rel.target_offset
4577 == rel->r_rel.target_offset))
4578 continue;
4579
4580 /* Check if the relocation was from an L32R that is being removed
4581 because a CALLX was converted to a direct CALL, and check if
4582 there are no other relocations to the literal. */
4583 if (rel->is_null
4584 && (i == relax_info->src_count - 1
4585 || (src_relocs[i+1].r_rel.target_offset
4586 != rel->r_rel.target_offset)))
4587 {
4588 /* Mark the unused literal so that it will be removed. */
4589 add_removed_literal (&relax_info->removed_list, &rel->r_rel, NULL);
4590
4591 /* Zero out the relocation on this literal location. */
4592 if (irel)
4593 {
4594 if (elf_hash_table (link_info)->dynamic_sections_created)
4595 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4596
4597 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4598 }
4599
4600 continue;
4601 }
4602
4603 /* Find the literal value. */
4604 r_reloc_init (&val.r_rel, abfd, irel);
4605 BFD_ASSERT (rel->r_rel.target_offset < sec->_raw_size);
4606 val.value = bfd_get_32 (abfd, contents + rel->r_rel.target_offset);
4607
4608 /* Check if we've seen another literal with the same value. */
4609 val_map = get_cached_value (values, &val);
4610 if (val_map != NULL)
4611 {
4612 /* First check that THIS and all the other relocs to this
4613 literal will FIT if we move them to the new address. */
4614
4615 if (relocations_reach (rel, relax_info->src_count - i,
4616 &val_map->loc))
4617 {
4618 /* Mark that the literal will be coalesced. */
4619 add_removed_literal (&relax_info->removed_list,
4620 &rel->r_rel, &val_map->loc);
4621 }
4622 else
4623 {
4624 /* Relocations do not reach -- do not remove this literal. */
4625 val_map->loc = rel->r_rel;
4626 }
4627 }
4628 else
4629 {
4630 /* This is the first time we've seen this literal value. */
4631 BFD_ASSERT (sec == r_reloc_get_section (&rel->r_rel));
4632 add_value_map (values, &val, &rel->r_rel);
4633 }
4634 }
4635
4636error_return:
4637 release_contents (sec, contents);
4638 release_internal_relocs (sec, internal_relocs);
4639 return ok;
4640}
4641
4642
4643/* Check if the original relocations (presumably on L32R instructions)
4644 identified by reloc[0..N] can be changed to reference the literal
4645 identified by r_rel. If r_rel is out of range for any of the
4646 original relocations, then we don't want to coalesce the original
4647 literal with the one at r_rel. We only check reloc[0..N], where the
4648 offsets are all the same as for reloc[0] (i.e., they're all
4649 referencing the same literal) and where N is also bounded by the
4650 number of remaining entries in the "reloc" array. The "reloc" array
4651 is sorted by target offset so we know all the entries for the same
4652 literal will be contiguous. */
4653
4654static bfd_boolean
4655relocations_reach (reloc, remaining_relocs, r_rel)
4656 source_reloc *reloc;
4657 int remaining_relocs;
4658 const r_reloc *r_rel;
4659{
4660 bfd_vma from_offset, source_address, dest_address;
4661 asection *sec;
4662 int i;
4663
4664 if (!r_reloc_is_defined (r_rel))
4665 return FALSE;
4666
4667 sec = r_reloc_get_section (r_rel);
4668 from_offset = reloc[0].r_rel.target_offset;
4669
4670 for (i = 0; i < remaining_relocs; i++)
4671 {
4672 if (reloc[i].r_rel.target_offset != from_offset)
4673 break;
4674
4675 /* Ignore relocations that have been removed. */
4676 if (reloc[i].is_null)
4677 continue;
4678
4679 /* The original and new output section for these must be the same
4680 in order to coalesce. */
4681 if (r_reloc_get_section (&reloc[i].r_rel)->output_section
4682 != sec->output_section)
4683 return FALSE;
4684
4685 /* A NULL operand means it is not a PC-relative relocation, so
4686 the literal can be moved anywhere. */
4687 if (reloc[i].opnd)
4688 {
4689 /* Otherwise, check to see that it fits. */
4690 source_address = (reloc[i].source_sec->output_section->vma
4691 + reloc[i].source_sec->output_offset
4692 + reloc[i].r_rel.rela.r_offset);
4693 dest_address = (sec->output_section->vma
4694 + sec->output_offset
4695 + r_rel->target_offset);
4696
4697 if (!pcrel_reloc_fits (reloc[i].opnd, source_address, dest_address))
4698 return FALSE;
4699 }
4700 }
4701
4702 return TRUE;
4703}
4704
4705
4706/* WARNING: linear search here. If the relocation are in order by
4707 address, we can use a faster binary search. ALSO, we assume that
4708 there is only 1 non-NONE relocation per address. */
4709
4710static Elf_Internal_Rela *
4711get_irel_at_offset (sec, internal_relocs, offset)
4712 asection *sec;
4713 Elf_Internal_Rela *internal_relocs;
4714 bfd_vma offset;
4715{
4716 unsigned i;
4717 if (!internal_relocs)
4718 return NULL;
4719 for (i = 0; i < sec->reloc_count; i++)
4720 {
4721 Elf_Internal_Rela *irel = &internal_relocs[i];
4722 if (irel->r_offset == offset
4723 && ELF32_R_TYPE (irel->r_info) != R_XTENSA_NONE)
4724 return irel;
4725 }
4726 return NULL;
4727}
4728
4729\f
4730/* Second relaxation pass. */
4731
4732/* Modify all of the relocations to point to the right spot, and if this
4733 is a relaxable section, delete the unwanted literals and fix the
4734 cooked_size. */
4735
4736bfd_boolean
4737relax_section (abfd, sec, link_info)
4738 bfd *abfd;
4739 asection *sec;
4740 struct bfd_link_info *link_info;
4741{
4742 Elf_Internal_Rela *internal_relocs;
4743 xtensa_relax_info *relax_info;
4744 bfd_byte *contents;
4745 bfd_boolean ok = TRUE;
4746 unsigned i;
4747
4748 relax_info = get_xtensa_relax_info (sec);
4749 BFD_ASSERT (relax_info);
4750
4751 /* Handle property sections (e.g., literal tables) specially. */
4752 if (xtensa_is_property_section (sec))
4753 {
4754 BFD_ASSERT (!relax_info->is_relaxable_literal_section);
4755 return relax_property_section (abfd, sec, link_info);
4756 }
4757
4758 internal_relocs = retrieve_internal_relocs (abfd, sec,
4759 link_info->keep_memory);
4760 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
4761 if (contents == NULL && sec->_raw_size != 0)
4762 {
4763 ok = FALSE;
4764 goto error_return;
4765 }
4766
4767 if (internal_relocs)
4768 {
4769 for (i = 0; i < sec->reloc_count; i++)
4770 {
4771 Elf_Internal_Rela *irel;
4772 xtensa_relax_info *target_relax_info;
4773 bfd_vma source_offset;
4774 r_reloc r_rel;
4775 unsigned r_type;
4776 asection *target_sec;
4777
4778 /* Locally change the source address.
4779 Translate the target to the new target address.
4780 If it points to this section and has been removed,
4781 NULLify it.
4782 Write it back. */
4783
4784 irel = &internal_relocs[i];
4785 source_offset = irel->r_offset;
4786
4787 r_type = ELF32_R_TYPE (irel->r_info);
4788 r_reloc_init (&r_rel, abfd, irel);
4789
4790 if (relax_info->is_relaxable_literal_section)
4791 {
4792 if (r_type != R_XTENSA_NONE
4793 && find_removed_literal (&relax_info->removed_list,
4794 irel->r_offset))
4795 {
4796 /* Remove this relocation. */
4797 if (elf_hash_table (link_info)->dynamic_sections_created)
4798 shrink_dynamic_reloc_sections (link_info, abfd, sec, irel);
4799 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
4800 irel->r_offset = offset_with_removed_literals
4801 (&relax_info->removed_list, irel->r_offset);
4802 continue;
4803 }
4804 source_offset =
4805 offset_with_removed_literals (&relax_info->removed_list,
4806 irel->r_offset);
4807 irel->r_offset = source_offset;
4808 }
4809
4810 target_sec = r_reloc_get_section (&r_rel);
4811 target_relax_info = get_xtensa_relax_info (target_sec);
4812
4813 if (target_relax_info
4814 && target_relax_info->is_relaxable_literal_section)
4815 {
4816 r_reloc new_rel;
4817 reloc_bfd_fix *fix;
4818
4819 translate_reloc (&r_rel, &new_rel);
4820
4821 /* FIXME: If the relocation still references a section in
4822 the same input file, the relocation should be modified
4823 directly instead of adding a "fix" record. */
4824
4825 fix = reloc_bfd_fix_init (sec, source_offset, r_type, 0,
4826 r_reloc_get_section (&new_rel),
4827 new_rel.target_offset);
4828 add_fix (sec, fix);
4829 }
4830
4831 pin_internal_relocs (sec, internal_relocs);
4832 }
4833 }
4834
4835 if (relax_info->is_relaxable_literal_section)
4836 {
4837 /* Walk through the contents and delete literals that are not needed
4838 anymore. */
4839
4840 unsigned long size = sec->_cooked_size;
4841 unsigned long removed = 0;
4842
4843 removed_literal *reloc = relax_info->removed_list.head;
4844 for (; reloc; reloc = reloc->next)
4845 {
4846 unsigned long upper = sec->_raw_size;
4847 bfd_vma start = reloc->from.target_offset + 4;
4848 if (reloc->next)
4849 upper = reloc->next->from.target_offset;
4850 if (upper - start != 0)
4851 {
4852 BFD_ASSERT (start <= upper);
4853 memmove (contents + start - removed - 4,
4854 contents + start,
4855 upper - start );
4856 pin_contents (sec, contents);
4857 }
4858 removed += 4;
4859 size -= 4;
4860 }
4861
4862 /* Change the section size. */
4863 sec->_cooked_size = size;
4864 /* Also shrink _raw_size. (The code in relocate_section that
4865 checks that relocations are within the section must use
4866 _raw_size because of the way the stabs sections are relaxed;
4867 shrinking _raw_size means that these checks will not be
4868 unnecessarily lax.) */
4869 sec->_raw_size = size;
4870 }
4871
4872 error_return:
4873 release_internal_relocs (sec, internal_relocs);
4874 release_contents (sec, contents);
4875 return ok;
4876}
4877
4878
4879/* Fix up a relocation to take account of removed literals. */
4880
4881static void
4882translate_reloc (orig_rel, new_rel)
4883 const r_reloc *orig_rel;
4884 r_reloc *new_rel;
4885{
4886 asection *sec;
4887 xtensa_relax_info *relax_info;
4888 removed_literal *removed;
4889 unsigned long new_offset;
4890
4891 *new_rel = *orig_rel;
4892
4893 if (!r_reloc_is_defined (orig_rel))
4894 return;
4895 sec = r_reloc_get_section (orig_rel);
4896
4897 relax_info = get_xtensa_relax_info (sec);
4898 BFD_ASSERT (relax_info);
4899
4900 if (!relax_info->is_relaxable_literal_section)
4901 return;
4902
4903 /* Check if the original relocation is against a literal being removed. */
4904 removed = find_removed_literal (&relax_info->removed_list,
4905 orig_rel->target_offset);
4906 if (removed)
4907 {
4908 asection *new_sec;
4909
4910 /* The fact that there is still a relocation to this literal indicates
4911 that the literal is being coalesced, not simply removed. */
4912 BFD_ASSERT (removed->to.abfd != NULL);
4913
4914 /* This was moved to some other address (possibly in another section). */
4915 *new_rel = removed->to;
4916 new_sec = r_reloc_get_section (new_rel);
4917 if (new_sec != sec)
4918 {
4919 sec = new_sec;
4920 relax_info = get_xtensa_relax_info (sec);
4921 if (!relax_info || !relax_info->is_relaxable_literal_section)
4922 return;
4923 }
4924 }
4925
4926 /* ...and the target address may have been moved within its section. */
4927 new_offset = offset_with_removed_literals (&relax_info->removed_list,
4928 new_rel->target_offset);
4929
4930 /* Modify the offset and addend. */
4931 new_rel->target_offset = new_offset;
4932 new_rel->rela.r_addend += (new_offset - new_rel->target_offset);
4933}
4934
4935
4936/* For dynamic links, there may be a dynamic relocation for each
4937 literal. The number of dynamic relocations must be computed in
4938 size_dynamic_sections, which occurs before relaxation. When a
4939 literal is removed, this function checks if there is a corresponding
4940 dynamic relocation and shrinks the size of the appropriate dynamic
4941 relocation section accordingly. At this point, the contents of the
4942 dynamic relocation sections have not yet been filled in, so there's
4943 nothing else that needs to be done. */
4944
4945static void
4946shrink_dynamic_reloc_sections (info, abfd, input_section, rel)
4947 struct bfd_link_info *info;
4948 bfd *abfd;
4949 asection *input_section;
4950 Elf_Internal_Rela *rel;
4951{
4952 Elf_Internal_Shdr *symtab_hdr;
4953 struct elf_link_hash_entry **sym_hashes;
4954 unsigned long r_symndx;
4955 int r_type;
4956 struct elf_link_hash_entry *h;
4957 bfd_boolean dynamic_symbol;
4958
4959 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4960 sym_hashes = elf_sym_hashes (abfd);
4961
4962 r_type = ELF32_R_TYPE (rel->r_info);
4963 r_symndx = ELF32_R_SYM (rel->r_info);
4964
4965 if (r_symndx < symtab_hdr->sh_info)
4966 h = NULL;
4967 else
4968 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4969
571b5725 4970 dynamic_symbol = xtensa_elf_dynamic_symbol_p (h, info);
e0001a05
NC
4971
4972 if ((r_type == R_XTENSA_32 || r_type == R_XTENSA_PLT)
4973 && (input_section->flags & SEC_ALLOC) != 0
4974 && (dynamic_symbol || info->shared))
4975 {
4976 bfd *dynobj;
4977 const char *srel_name;
4978 asection *srel;
4979 bfd_boolean is_plt = FALSE;
4980
4981 dynobj = elf_hash_table (info)->dynobj;
4982 BFD_ASSERT (dynobj != NULL);
4983
4984 if (dynamic_symbol && r_type == R_XTENSA_PLT)
4985 {
4986 srel_name = ".rela.plt";
4987 is_plt = TRUE;
4988 }
4989 else
4990 srel_name = ".rela.got";
4991
4992 /* Reduce size of the .rela.* section by one reloc. */
4993 srel = bfd_get_section_by_name (dynobj, srel_name);
4994 BFD_ASSERT (srel != NULL);
4995 BFD_ASSERT (srel->_cooked_size >= sizeof (Elf32_External_Rela));
4996 srel->_cooked_size -= sizeof (Elf32_External_Rela);
4997
4998 /* Also shrink _raw_size. (This seems wrong but other bfd code seems
4999 to assume that linker-created sections will never be relaxed and
5000 hence _raw_size must always equal _cooked_size.) */
5001 srel->_raw_size = srel->_cooked_size;
5002
5003 if (is_plt)
5004 {
5005 asection *splt, *sgotplt, *srelgot;
5006 int reloc_index, chunk;
5007
5008 /* Find the PLT reloc index of the entry being removed. This
5009 is computed from the size of ".rela.plt". It is needed to
5010 figure out which PLT chunk to resize. Usually "last index
5011 = size - 1" since the index starts at zero, but in this
5012 context, the size has just been decremented so there's no
5013 need to subtract one. */
5014 reloc_index = srel->_cooked_size / sizeof (Elf32_External_Rela);
5015
5016 chunk = reloc_index / PLT_ENTRIES_PER_CHUNK;
5017 splt = elf_xtensa_get_plt_section (dynobj, chunk);
5018 sgotplt = elf_xtensa_get_gotplt_section (dynobj, chunk);
5019 BFD_ASSERT (splt != NULL && sgotplt != NULL);
5020
5021 /* Check if an entire PLT chunk has just been eliminated. */
5022 if (reloc_index % PLT_ENTRIES_PER_CHUNK == 0)
5023 {
5024 /* The two magic GOT entries for that chunk can go away. */
5025 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5026 BFD_ASSERT (srelgot != NULL);
5027 srelgot->reloc_count -= 2;
5028 srelgot->_cooked_size -= 2 * sizeof (Elf32_External_Rela);
5029 /* Shrink _raw_size (see comment above). */
5030 srelgot->_raw_size = srelgot->_cooked_size;
5031
5032 sgotplt->_cooked_size -= 8;
5033
5034 /* There should be only one entry left (and it will be
5035 removed below). */
5036 BFD_ASSERT (sgotplt->_cooked_size == 4);
5037 BFD_ASSERT (splt->_cooked_size == PLT_ENTRY_SIZE);
5038 }
5039
5040 BFD_ASSERT (sgotplt->_cooked_size >= 4);
5041 BFD_ASSERT (splt->_cooked_size >= PLT_ENTRY_SIZE);
5042
5043 sgotplt->_cooked_size -= 4;
5044 splt->_cooked_size -= PLT_ENTRY_SIZE;
5045
5046 /* Shrink _raw_sizes (see comment above). */
5047 sgotplt->_raw_size = sgotplt->_cooked_size;
5048 splt->_raw_size = splt->_cooked_size;
5049 }
5050 }
5051}
5052
5053
5054/* This is similar to relax_section except that when a target is moved,
5055 we shift addresses up. We also need to modify the size. This
5056 algorithm does NOT allow for relocations into the middle of the
5057 property sections. */
5058
5059static bfd_boolean
5060relax_property_section (abfd, sec, link_info)
5061 bfd *abfd;
5062 asection *sec;
5063 struct bfd_link_info *link_info;
5064{
5065 Elf_Internal_Rela *internal_relocs;
5066 bfd_byte *contents;
5067 unsigned i, nexti;
5068 bfd_boolean ok = TRUE;
5069
5070 internal_relocs = retrieve_internal_relocs (abfd, sec,
5071 link_info->keep_memory);
5072 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5073 if (contents == NULL && sec->_raw_size != 0)
5074 {
5075 ok = FALSE;
5076 goto error_return;
5077 }
5078
5079 if (internal_relocs)
5080 {
5081 for (i = 0; i < sec->reloc_count; i++)
5082 {
5083 Elf_Internal_Rela *irel;
5084 xtensa_relax_info *target_relax_info;
5085 r_reloc r_rel;
5086 unsigned r_type;
5087 asection *target_sec;
5088
5089 /* Locally change the source address.
5090 Translate the target to the new target address.
5091 If it points to this section and has been removed, MOVE IT.
5092 Also, don't forget to modify the associated SIZE at
5093 (offset + 4). */
5094
5095 irel = &internal_relocs[i];
5096 r_type = ELF32_R_TYPE (irel->r_info);
5097 if (r_type == R_XTENSA_NONE)
5098 continue;
5099
5100 r_reloc_init (&r_rel, abfd, irel);
5101
5102 target_sec = r_reloc_get_section (&r_rel);
5103 target_relax_info = get_xtensa_relax_info (target_sec);
5104
5105 if (target_relax_info
5106 && target_relax_info->is_relaxable_literal_section)
5107 {
5108 /* Translate the relocation's destination. */
5109 bfd_vma new_offset;
5110 bfd_vma new_end_offset;
5111 bfd_byte *size_p;
5112 long old_size, new_size;
5113
5114 new_offset =
5115 offset_with_removed_literals (&target_relax_info->removed_list,
5116 r_rel.target_offset);
5117
5118 /* Assert that we are not out of bounds. */
5119 size_p = &contents[irel->r_offset + 4];
5120 old_size = bfd_get_32 (abfd, &contents[irel->r_offset + 4]);
5121
5122 new_end_offset =
5123 offset_with_removed_literals (&target_relax_info->removed_list,
5124 r_rel.target_offset + old_size);
5125
5126 new_size = new_end_offset - new_offset;
5127 if (new_size != old_size)
5128 {
5129 bfd_put_32 (abfd, new_size, size_p);
5130 pin_contents (sec, contents);
5131 }
5132
5133 if (new_offset != r_rel.target_offset)
5134 {
5135 bfd_vma diff = new_offset - r_rel.target_offset;
5136 irel->r_addend += diff;
5137 pin_internal_relocs (sec, internal_relocs);
5138 }
5139 }
5140 }
5141 }
5142
5143 /* Combine adjacent property table entries. This is also done in
5144 finish_dynamic_sections() but at that point it's too late to
5145 reclaim the space in the output section, so we do this twice. */
5146
5147 if (internal_relocs)
5148 {
5149 Elf_Internal_Rela *last_irel = NULL;
5150 int removed_bytes = 0;
5151 bfd_vma offset, last_irel_offset;
5152 bfd_vma section_size;
5153
5154 /* Walk over memory and irels at the same time.
5155 This REQUIRES that the internal_relocs be sorted by offset. */
5156 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
5157 internal_reloc_compare);
5158 nexti = 0; /* Index into internal_relocs. */
5159
5160 pin_internal_relocs (sec, internal_relocs);
5161 pin_contents (sec, contents);
5162
5163 last_irel_offset = (bfd_vma) -1;
5164 section_size = (sec->_cooked_size ? sec->_cooked_size : sec->_raw_size);
5165 BFD_ASSERT (section_size % 8 == 0);
5166
5167 for (offset = 0; offset < section_size; offset += 8)
5168 {
5169 Elf_Internal_Rela *irel, *next_irel;
5170 bfd_vma bytes_to_remove, size, actual_offset;
5171 bfd_boolean remove_this_irel;
5172
5173 irel = NULL;
5174 next_irel = NULL;
5175
5176 /* Find the next two relocations (if there are that many left),
5177 skipping over any R_XTENSA_NONE relocs. On entry, "nexti" is
5178 the starting reloc index. After these two loops, "i"
5179 is the index of the first non-NONE reloc past that starting
5180 index, and "nexti" is the index for the next non-NONE reloc
5181 after "i". */
5182
5183 for (i = nexti; i < sec->reloc_count; i++)
5184 {
5185 if (ELF32_R_TYPE (internal_relocs[i].r_info) != R_XTENSA_NONE)
5186 {
5187 irel = &internal_relocs[i];
5188 break;
5189 }
5190 internal_relocs[i].r_offset -= removed_bytes;
5191 }
5192
5193 for (nexti = i + 1; nexti < sec->reloc_count; nexti++)
5194 {
5195 if (ELF32_R_TYPE (internal_relocs[nexti].r_info)
5196 != R_XTENSA_NONE)
5197 {
5198 next_irel = &internal_relocs[nexti];
5199 break;
5200 }
5201 internal_relocs[nexti].r_offset -= removed_bytes;
5202 }
5203
5204 remove_this_irel = FALSE;
5205 bytes_to_remove = 0;
5206 actual_offset = offset - removed_bytes;
5207 size = bfd_get_32 (abfd, &contents[actual_offset + 4]);
5208
5209 /* Check that the irels are sorted by offset,
5210 with only one per address. */
5211 BFD_ASSERT (!irel || (int) irel->r_offset > (int) last_irel_offset);
5212 BFD_ASSERT (!next_irel || next_irel->r_offset > irel->r_offset);
5213
5214 /* Make sure there isn't a reloc on the size field. */
5215 if (irel && irel->r_offset == offset + 4)
5216 {
5217 irel->r_offset -= removed_bytes;
5218 last_irel_offset = irel->r_offset;
5219 }
5220 else if (next_irel && next_irel->r_offset == offset + 4)
5221 {
5222 nexti += 1;
5223 irel->r_offset -= removed_bytes;
5224 next_irel->r_offset -= removed_bytes;
5225 last_irel_offset = next_irel->r_offset;
5226 }
5227 else if (size == 0)
5228 {
5229 /* Always remove entries with zero size. */
5230 bytes_to_remove = 8;
5231 if (irel && irel->r_offset == offset)
5232 {
5233 remove_this_irel = TRUE;
5234
5235 irel->r_offset -= removed_bytes;
5236 last_irel_offset = irel->r_offset;
5237 }
5238 }
5239 else if (irel && irel->r_offset == offset)
5240 {
5241 if (ELF32_R_TYPE (irel->r_info) == R_XTENSA_32)
5242 {
5243 if (last_irel)
5244 {
5245 bfd_vma old_size =
5246 bfd_get_32 (abfd, &contents[last_irel->r_offset + 4]);
5247 bfd_vma old_address =
5248 (last_irel->r_addend
5249 + bfd_get_32 (abfd, &contents[last_irel->r_offset]));
5250 bfd_vma new_address =
5251 (irel->r_addend
5252 + bfd_get_32 (abfd, &contents[actual_offset]));
5253
5254 if ((ELF32_R_SYM (irel->r_info) ==
5255 ELF32_R_SYM (last_irel->r_info))
5256 && (old_address + old_size == new_address))
5257 {
5258 /* fix the old size */
5259 bfd_put_32 (abfd, old_size + size,
5260 &contents[last_irel->r_offset + 4]);
5261 bytes_to_remove = 8;
5262 remove_this_irel = TRUE;
5263 }
5264 else
5265 last_irel = irel;
5266 }
5267 else
5268 last_irel = irel;
5269 }
5270
5271 irel->r_offset -= removed_bytes;
5272 last_irel_offset = irel->r_offset;
5273 }
5274
5275 if (remove_this_irel)
5276 {
5277 irel->r_info = ELF32_R_INFO (0, R_XTENSA_NONE);
5278 irel->r_offset -= bytes_to_remove;
5279 }
5280
5281 if (bytes_to_remove != 0)
5282 {
5283 removed_bytes += bytes_to_remove;
5284 if (offset + 8 < section_size)
5285 memmove (&contents[actual_offset],
5286 &contents[actual_offset+8],
5287 section_size - offset - 8);
5288 }
5289 }
5290
5291 if (removed_bytes)
5292 {
5293 /* Clear the removed bytes. */
5294 memset (&contents[section_size - removed_bytes], 0, removed_bytes);
5295
5296 sec->_cooked_size = section_size - removed_bytes;
5297 /* Also shrink _raw_size. (The code in relocate_section that
5298 checks that relocations are within the section must use
5299 _raw_size because of the way the stabs sections are
5300 relaxed; shrinking _raw_size means that these checks will
5301 not be unnecessarily lax.) */
5302 sec->_raw_size = sec->_cooked_size;
e901de89
BW
5303
5304 if (xtensa_is_littable_section (sec))
5305 {
5306 bfd *dynobj = elf_hash_table (link_info)->dynobj;
5307 if (dynobj)
5308 {
5309 asection *sgotloc =
5310 bfd_get_section_by_name (dynobj, ".got.loc");
5311 if (sgotloc)
5312 {
5313 bfd_size_type sgotloc_size =
5314 (sgotloc->_cooked_size ? sgotloc->_cooked_size
5315 : sgotloc->_raw_size);
5316 sgotloc->_cooked_size = sgotloc_size - removed_bytes;
5317 sgotloc->_raw_size = sgotloc_size - removed_bytes;
5318 }
5319 }
5320 }
e0001a05
NC
5321 }
5322 }
e901de89 5323
e0001a05
NC
5324 error_return:
5325 release_internal_relocs (sec, internal_relocs);
5326 release_contents (sec, contents);
5327 return ok;
5328}
5329
5330\f
5331/* Third relaxation pass. */
5332
5333/* Change symbol values to account for removed literals. */
5334
5335bfd_boolean
5336relax_section_symbols (abfd, sec)
5337 bfd *abfd;
5338 asection *sec;
5339{
5340 xtensa_relax_info *relax_info;
5341 unsigned int sec_shndx;
5342 Elf_Internal_Shdr *symtab_hdr;
5343 Elf_Internal_Sym *isymbuf;
5344 unsigned i, num_syms, num_locals;
5345
5346 relax_info = get_xtensa_relax_info (sec);
5347 BFD_ASSERT (relax_info);
5348
5349 if (!relax_info->is_relaxable_literal_section)
5350 return TRUE;
5351
5352 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5353
5354 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5355 isymbuf = retrieve_local_syms (abfd);
5356
5357 num_syms = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5358 num_locals = symtab_hdr->sh_info;
5359
5360 /* Adjust the local symbols defined in this section. */
5361 for (i = 0; i < num_locals; i++)
5362 {
5363 Elf_Internal_Sym *isym = &isymbuf[i];
5364
5365 if (isym->st_shndx == sec_shndx)
5366 {
5367 bfd_vma new_address = offset_with_removed_literals
5368 (&relax_info->removed_list, isym->st_value);
5369 if (new_address != isym->st_value)
5370 isym->st_value = new_address;
5371 }
5372 }
5373
5374 /* Now adjust the global symbols defined in this section. */
5375 for (i = 0; i < (num_syms - num_locals); i++)
5376 {
5377 struct elf_link_hash_entry *sym_hash;
5378
5379 sym_hash = elf_sym_hashes (abfd)[i];
5380
5381 if (sym_hash->root.type == bfd_link_hash_warning)
5382 sym_hash = (struct elf_link_hash_entry *) sym_hash->root.u.i.link;
5383
5384 if ((sym_hash->root.type == bfd_link_hash_defined
5385 || sym_hash->root.type == bfd_link_hash_defweak)
5386 && sym_hash->root.u.def.section == sec)
5387 {
5388 bfd_vma new_address = offset_with_removed_literals
5389 (&relax_info->removed_list, sym_hash->root.u.def.value);
5390 if (new_address != sym_hash->root.u.def.value)
5391 sym_hash->root.u.def.value = new_address;
5392 }
5393 }
5394
5395 return TRUE;
5396}
5397
5398\f
5399/* "Fix" handling functions, called while performing relocations. */
5400
5401static void
1049f94e 5402do_fix_for_relocatable_link (rel, input_bfd, input_section)
e0001a05
NC
5403 Elf_Internal_Rela *rel;
5404 bfd *input_bfd;
5405 asection *input_section;
5406{
5407 r_reloc r_rel;
5408 asection *sec, *old_sec;
5409 bfd_vma old_offset;
5410 int r_type = ELF32_R_TYPE (rel->r_info);
5411 reloc_bfd_fix *fix_list;
5412 reloc_bfd_fix *fix;
5413
5414 if (r_type == R_XTENSA_NONE)
5415 return;
5416
5417 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5418 if (fix_list == NULL)
5419 return;
5420
5421 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5422 if (fix == NULL)
5423 return;
5424
5425 r_reloc_init (&r_rel, input_bfd, rel);
5426 old_sec = r_reloc_get_section (&r_rel);
5427 old_offset = r_reloc_get_target_offset (&r_rel);
5428
5429 if (old_sec == NULL || !r_reloc_is_defined (&r_rel))
5430 {
5431 BFD_ASSERT (r_type == R_XTENSA_ASM_EXPAND);
5432 /* Leave it be. Resolution will happen in a later stage. */
5433 }
5434 else
5435 {
5436 sec = fix->target_sec;
5437 rel->r_addend += ((sec->output_offset + fix->target_offset)
5438 - (old_sec->output_offset + old_offset));
5439 }
5440}
5441
5442
5443static void
5444do_fix_for_final_link (rel, input_section, relocationp)
5445 Elf_Internal_Rela *rel;
5446 asection *input_section;
5447 bfd_vma *relocationp;
5448{
5449 asection *sec;
5450 int r_type = ELF32_R_TYPE (rel->r_info);
5451 reloc_bfd_fix *fix_list;
5452 reloc_bfd_fix *fix;
5453
5454 if (r_type == R_XTENSA_NONE)
5455 return;
5456
5457 fix_list = (get_xtensa_relax_info (input_section))->fix_list;
5458 if (fix_list == NULL)
5459 return;
5460
5461 fix = get_bfd_fix (fix_list, input_section, rel->r_offset, r_type);
5462 if (fix == NULL)
5463 return;
5464
5465 sec = fix->target_sec;
5466 *relocationp = (sec->output_section->vma
5467 + sec->output_offset
5468 + fix->target_offset - rel->r_addend);
5469}
5470
5471\f
5472/* Miscellaneous utility functions.... */
5473
5474static asection *
5475elf_xtensa_get_plt_section (dynobj, chunk)
5476 bfd *dynobj;
5477 int chunk;
5478{
5479 char plt_name[10];
5480
5481 if (chunk == 0)
5482 return bfd_get_section_by_name (dynobj, ".plt");
5483
5484 sprintf (plt_name, ".plt.%u", chunk);
5485 return bfd_get_section_by_name (dynobj, plt_name);
5486}
5487
5488
5489static asection *
5490elf_xtensa_get_gotplt_section (dynobj, chunk)
5491 bfd *dynobj;
5492 int chunk;
5493{
5494 char got_name[14];
5495
5496 if (chunk == 0)
5497 return bfd_get_section_by_name (dynobj, ".got.plt");
5498
5499 sprintf (got_name, ".got.plt.%u", chunk);
5500 return bfd_get_section_by_name (dynobj, got_name);
5501}
5502
5503
5504/* Get the input section for a given symbol index.
5505 If the symbol is:
5506 . a section symbol, return the section;
5507 . a common symbol, return the common section;
5508 . an undefined symbol, return the undefined section;
5509 . an indirect symbol, follow the links;
5510 . an absolute value, return the absolute section. */
5511
5512static asection *
5513get_elf_r_symndx_section (abfd, r_symndx)
5514 bfd *abfd;
5515 unsigned long r_symndx;
5516{
5517 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5518 asection *target_sec = NULL;
5519 if (r_symndx < symtab_hdr->sh_info)
5520 {
5521 Elf_Internal_Sym *isymbuf;
5522 unsigned int section_index;
5523
5524 isymbuf = retrieve_local_syms (abfd);
5525 section_index = isymbuf[r_symndx].st_shndx;
5526
5527 if (section_index == SHN_UNDEF)
5528 target_sec = bfd_und_section_ptr;
5529 else if (section_index > 0 && section_index < SHN_LORESERVE)
5530 target_sec = bfd_section_from_elf_index (abfd, section_index);
5531 else if (section_index == SHN_ABS)
5532 target_sec = bfd_abs_section_ptr;
5533 else if (section_index == SHN_COMMON)
5534 target_sec = bfd_com_section_ptr;
5535 else
5536 /* Who knows? */
5537 target_sec = NULL;
5538 }
5539 else
5540 {
5541 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5542 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
5543
5544 while (h->root.type == bfd_link_hash_indirect
5545 || h->root.type == bfd_link_hash_warning)
5546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5547
5548 switch (h->root.type)
5549 {
5550 case bfd_link_hash_defined:
5551 case bfd_link_hash_defweak:
5552 target_sec = h->root.u.def.section;
5553 break;
5554 case bfd_link_hash_common:
5555 target_sec = bfd_com_section_ptr;
5556 break;
5557 case bfd_link_hash_undefined:
5558 case bfd_link_hash_undefweak:
5559 target_sec = bfd_und_section_ptr;
5560 break;
5561 default: /* New indirect warning. */
5562 target_sec = bfd_und_section_ptr;
5563 break;
5564 }
5565 }
5566 return target_sec;
5567}
5568
5569
5570static struct elf_link_hash_entry *
5571get_elf_r_symndx_hash_entry (abfd, r_symndx)
5572 bfd *abfd;
5573 unsigned long r_symndx;
5574{
5575 unsigned long indx;
5576 struct elf_link_hash_entry *h;
5577 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5578
5579 if (r_symndx < symtab_hdr->sh_info)
5580 return NULL;
5581
5582 indx = r_symndx - symtab_hdr->sh_info;
5583 h = elf_sym_hashes (abfd)[indx];
5584 while (h->root.type == bfd_link_hash_indirect
5585 || h->root.type == bfd_link_hash_warning)
5586 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5587 return h;
5588}
5589
5590
5591/* Get the section-relative offset for a symbol number. */
5592
5593static bfd_vma
5594get_elf_r_symndx_offset (abfd, r_symndx)
5595 bfd *abfd;
5596 unsigned long r_symndx;
5597{
5598 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5599 bfd_vma offset = 0;
5600
5601 if (r_symndx < symtab_hdr->sh_info)
5602 {
5603 Elf_Internal_Sym *isymbuf;
5604 isymbuf = retrieve_local_syms (abfd);
5605 offset = isymbuf[r_symndx].st_value;
5606 }
5607 else
5608 {
5609 unsigned long indx = r_symndx - symtab_hdr->sh_info;
5610 struct elf_link_hash_entry *h =
5611 elf_sym_hashes (abfd)[indx];
5612
5613 while (h->root.type == bfd_link_hash_indirect
5614 || h->root.type == bfd_link_hash_warning)
5615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5616 if (h->root.type == bfd_link_hash_defined
5617 || h->root.type == bfd_link_hash_defweak)
5618 offset = h->root.u.def.value;
5619 }
5620 return offset;
5621}
5622
5623
5624static bfd_boolean
5625pcrel_reloc_fits (opnd, self_address, dest_address)
5626 xtensa_operand opnd;
5627 bfd_vma self_address;
5628 bfd_vma dest_address;
5629{
5630 uint32 new_address =
5631 xtensa_operand_do_reloc (opnd, dest_address, self_address);
5632 return (xtensa_operand_encode (opnd, &new_address)
5633 == xtensa_encode_result_ok);
5634}
5635
5636
5637static bfd_boolean
5638xtensa_is_property_section (sec)
5639 asection *sec;
5640{
e901de89
BW
5641 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
5642
5643 if (strncmp (".xt.insn", sec->name, 8) == 0
5644 || strncmp (".xt.lit", sec->name, 7) == 0)
5645 return TRUE;
5646
5647 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0)
5648 {
5649 if (strncmp ("x.", sec->name + linkonce_len, 2) == 0
5650 || strncmp ("p.", sec->name + linkonce_len, 2) == 0)
5651 return TRUE;
5652 if (strstr (sec->name + linkonce_len, ".xt.insn") != NULL
5653 || strstr (sec->name + linkonce_len, ".xt.lit") != NULL)
5654 return TRUE;
5655 }
5656 return FALSE;
5657}
5658
5659
5660static bfd_boolean
5661xtensa_is_littable_section (sec)
5662 asection *sec;
5663{
5664 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
5665
5666 if (strncmp (".xt.lit", sec->name, 7) == 0)
5667 return TRUE;
e0001a05 5668
e901de89
BW
5669 if (strncmp (".gnu.linkonce.", sec->name, linkonce_len) == 0)
5670 {
5671 if (strncmp ("p.", sec->name + linkonce_len, 2) == 0)
5672 return TRUE;
5673 if (strstr (sec->name + linkonce_len, ".xt.lit") != NULL)
5674 return TRUE;
5675 }
5676 return FALSE;
e0001a05
NC
5677}
5678
5679
5680static bfd_boolean
5681is_literal_section (sec)
5682 asection *sec;
5683{
5684 /* FIXME: the current definition of this leaves a lot to be desired.... */
5685 if (sec == NULL || sec->name == NULL)
5686 return FALSE;
5687 return (strstr (sec->name, "literal") != NULL);
5688}
5689
5690
5691static int
5692internal_reloc_compare (ap, bp)
5693 const PTR ap;
5694 const PTR bp;
5695{
5696 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
5697 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
5698
5699 return (a->r_offset - b->r_offset);
5700}
5701
5702
5703static bfd_boolean
5704get_is_linkonce_section (abfd, sec)
5705 bfd *abfd ATTRIBUTE_UNUSED;
5706 asection *sec;
5707{
5708 flagword flags, link_once_flags;
5709 bfd_boolean is_linkonce = FALSE;;
5710
5711 flags = bfd_get_section_flags (abfd, sec);
5712 link_once_flags = (flags & SEC_LINK_ONCE);
5713 if (link_once_flags != 0)
5714 is_linkonce = TRUE;
5715
5716 /* In order for this to be useful to the assembler
5717 before the linkonce flag is set we need to
5718 check for the GNU extension name. */
5719 if (!is_linkonce &&
5720 strncmp (sec->name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
5721 is_linkonce = TRUE;
5722
5723 return is_linkonce;
5724}
5725
5726
5727char *
5728xtensa_get_property_section_name (abfd, sec, base_name)
5729 bfd *abfd;
5730 asection *sec;
5731 const char * base_name;
5732{
5733 char *table_sec_name = NULL;
5734 bfd_boolean is_linkonce;
5735
5736 is_linkonce = get_is_linkonce_section (abfd, sec);
5737
5738 if (!is_linkonce)
5739 {
5740 table_sec_name = strdup (base_name);
5741 }
5742 else
5743 {
5744 static size_t prefix_len = sizeof (".gnu.linkonce.t.") - 1;
5745 size_t len = strlen (sec->name) + 1;
5746 char repl_char = '\0';
5747 const char *segname = sec->name;
5748
5749 if (strncmp (segname, ".gnu.linkonce.t.", prefix_len) == 0)
5750 {
5751 if (strcmp (base_name, ".xt.insn") == 0)
5752 repl_char = 'x';
5753 else if (strcmp (base_name, ".xt.lit") == 0)
5754 repl_char = 'p';
5755 }
5756
5757 if (repl_char != '\0')
5758 {
5759 char *name = (char *) bfd_malloc (len);
5760 memcpy (name, sec->name, len);
5761 name[prefix_len - 2] = repl_char;
5762 table_sec_name = name;
5763 }
5764 else
5765 {
5766 size_t base_len = strlen (base_name) + 1;
5767 char *name = (char *) bfd_malloc (len + base_len);
5768 memcpy (name, sec->name, len - 1);
5769 memcpy (name + len - 1, base_name, base_len);
5770 table_sec_name = name;
5771 }
5772 }
5773
5774 return table_sec_name;
5775}
5776
5777\f
5778/* Other functions called directly by the linker. */
5779
5780bfd_boolean
5781xtensa_callback_required_dependence (abfd, sec, link_info, callback, closure)
5782 bfd *abfd;
5783 asection *sec;
5784 struct bfd_link_info *link_info;
5785 deps_callback_t callback;
5786 PTR closure;
5787{
5788 Elf_Internal_Rela *internal_relocs;
5789 bfd_byte *contents;
5790 unsigned i;
5791 bfd_boolean ok = TRUE;
5792
5793 /* ".plt*" sections have no explicit relocations but they contain L32R
5794 instructions that reference the corresponding ".got.plt*" sections. */
5795 if ((sec->flags & SEC_LINKER_CREATED) != 0
5796 && strncmp (sec->name, ".plt", 4) == 0)
5797 {
5798 asection *sgotplt;
5799
5800 /* Find the corresponding ".got.plt*" section. */
5801 if (sec->name[4] == '\0')
5802 sgotplt = bfd_get_section_by_name (sec->owner, ".got.plt");
5803 else
5804 {
5805 char got_name[14];
5806 int chunk = 0;
5807
5808 BFD_ASSERT (sec->name[4] == '.');
5809 chunk = strtol (&sec->name[5], NULL, 10);
5810
5811 sprintf (got_name, ".got.plt.%u", chunk);
5812 sgotplt = bfd_get_section_by_name (sec->owner, got_name);
5813 }
5814 BFD_ASSERT (sgotplt);
5815
5816 /* Assume worst-case offsets: L32R at the very end of the ".plt"
5817 section referencing a literal at the very beginning of
5818 ".got.plt". This is very close to the real dependence, anyway. */
5819 (*callback) (sec, sec->_raw_size, sgotplt, 0, closure);
5820 }
5821
5822 internal_relocs = retrieve_internal_relocs (abfd, sec,
5823 link_info->keep_memory);
5824 if (internal_relocs == NULL
5825 || sec->reloc_count == 0)
5826 return ok;
5827
5828 /* Cache the contents for the duration of this scan. */
5829 contents = retrieve_contents (abfd, sec, link_info->keep_memory);
5830 if (contents == NULL && sec->_raw_size != 0)
5831 {
5832 ok = FALSE;
5833 goto error_return;
5834 }
5835
5836 if (xtensa_default_isa == NULL)
5837 xtensa_isa_init ();
5838
5839 for (i = 0; i < sec->reloc_count; i++)
5840 {
5841 Elf_Internal_Rela *irel = &internal_relocs[i];
5842 if (is_l32r_relocation (sec, contents, irel))
5843 {
5844 r_reloc l32r_rel;
5845 asection *target_sec;
5846 bfd_vma target_offset;
5847
5848 r_reloc_init (&l32r_rel, abfd, irel);
5849 target_sec = NULL;
5850 target_offset = 0;
5851 /* L32Rs must be local to the input file. */
5852 if (r_reloc_is_defined (&l32r_rel))
5853 {
5854 target_sec = r_reloc_get_section (&l32r_rel);
5855 target_offset = r_reloc_get_target_offset (&l32r_rel);
5856 }
5857 (*callback) (sec, irel->r_offset, target_sec, target_offset,
5858 closure);
5859 }
5860 }
5861
5862 error_return:
5863 release_internal_relocs (sec, internal_relocs);
5864 release_contents (sec, contents);
5865 return ok;
5866}
5867
2f89ff8d
L
5868/* The default literal sections should always be marked as "code" (i.e.,
5869 SHF_EXECINSTR). This is particularly important for the Linux kernel
5870 module loader so that the literals are not placed after the text. */
5871static struct bfd_elf_special_section const elf_xtensa_special_sections[]=
5872{
5873 { ".literal", 0, NULL, 0,
5874 SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5875 { ".init.literal", 0, NULL, 0,
5876 SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5877 { ".fini.literal", 0, NULL, 0,
5878 SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5879 { NULL, 0, NULL, 0,
5880 0, 0 }
5881};
5882
e0001a05
NC
5883\f
5884#ifndef ELF_ARCH
5885#define TARGET_LITTLE_SYM bfd_elf32_xtensa_le_vec
5886#define TARGET_LITTLE_NAME "elf32-xtensa-le"
5887#define TARGET_BIG_SYM bfd_elf32_xtensa_be_vec
5888#define TARGET_BIG_NAME "elf32-xtensa-be"
5889#define ELF_ARCH bfd_arch_xtensa
5890
5891/* The new EM_XTENSA value will be recognized beginning in the Xtensa T1040
5892 release. However, we still have to generate files with the EM_XTENSA_OLD
5893 value so that pre-T1040 tools can read the files. As soon as we stop
5894 caring about pre-T1040 tools, the following two values should be
5895 swapped. At the same time, any other code that uses EM_XTENSA_OLD
5896 (e.g., prep_headers() in elf.c) should be changed to use EM_XTENSA. */
5897#define ELF_MACHINE_CODE EM_XTENSA_OLD
5898#define ELF_MACHINE_ALT1 EM_XTENSA
5899
5900#if XCHAL_HAVE_MMU
5901#define ELF_MAXPAGESIZE (1 << XCHAL_MMU_MIN_PTE_PAGE_SIZE)
5902#else /* !XCHAL_HAVE_MMU */
5903#define ELF_MAXPAGESIZE 1
5904#endif /* !XCHAL_HAVE_MMU */
5905#endif /* ELF_ARCH */
5906
5907#define elf_backend_can_gc_sections 1
5908#define elf_backend_can_refcount 1
5909#define elf_backend_plt_readonly 1
5910#define elf_backend_got_header_size 4
5911#define elf_backend_want_dynbss 0
5912#define elf_backend_want_got_plt 1
5913
5914#define elf_info_to_howto elf_xtensa_info_to_howto_rela
5915
5916#define bfd_elf32_bfd_final_link bfd_elf32_bfd_final_link
5917#define bfd_elf32_bfd_merge_private_bfd_data elf_xtensa_merge_private_bfd_data
5918#define bfd_elf32_new_section_hook elf_xtensa_new_section_hook
5919#define bfd_elf32_bfd_print_private_bfd_data elf_xtensa_print_private_bfd_data
5920#define bfd_elf32_bfd_relax_section elf_xtensa_relax_section
5921#define bfd_elf32_bfd_reloc_type_lookup elf_xtensa_reloc_type_lookup
5922#define bfd_elf32_bfd_set_private_flags elf_xtensa_set_private_flags
5923
5924#define elf_backend_adjust_dynamic_symbol elf_xtensa_adjust_dynamic_symbol
5925#define elf_backend_check_relocs elf_xtensa_check_relocs
5926#define elf_backend_copy_indirect_symbol elf_xtensa_copy_indirect_symbol
5927#define elf_backend_create_dynamic_sections elf_xtensa_create_dynamic_sections
5928#define elf_backend_discard_info elf_xtensa_discard_info
5929#define elf_backend_ignore_discarded_relocs elf_xtensa_ignore_discarded_relocs
5930#define elf_backend_final_write_processing elf_xtensa_final_write_processing
5931#define elf_backend_finish_dynamic_sections elf_xtensa_finish_dynamic_sections
5932#define elf_backend_finish_dynamic_symbol elf_xtensa_finish_dynamic_symbol
5933#define elf_backend_gc_mark_hook elf_xtensa_gc_mark_hook
5934#define elf_backend_gc_sweep_hook elf_xtensa_gc_sweep_hook
5935#define elf_backend_grok_prstatus elf_xtensa_grok_prstatus
5936#define elf_backend_grok_psinfo elf_xtensa_grok_psinfo
5937#define elf_backend_hide_symbol elf_xtensa_hide_symbol
5938#define elf_backend_modify_segment_map elf_xtensa_modify_segment_map
5939#define elf_backend_object_p elf_xtensa_object_p
5940#define elf_backend_reloc_type_class elf_xtensa_reloc_type_class
5941#define elf_backend_relocate_section elf_xtensa_relocate_section
5942#define elf_backend_size_dynamic_sections elf_xtensa_size_dynamic_sections
2f89ff8d 5943#define elf_backend_special_sections elf_xtensa_special_sections
e0001a05
NC
5944
5945#include "elf32-target.h"
This page took 0.356717 seconds and 4 git commands to generate.