source: trunk/src/binutils/bfd/elf32-xtensa.c@ 1036

Last change on this file since 1036 was 607, checked in by bird, 22 years ago

Initial revision

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