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