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