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