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