93baaf1ba1b0a47ed0fd36b6c280923e57c8bff1
[external/binutils.git] / bfd / elfxx-ia64.c
1 /* IA-64 support for 64-bit ELF
2    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3    2008, 2009, 2010  Free Software Foundation, Inc.
4    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "opcode/ia64.h"
28 #include "elf/ia64.h"
29 #include "objalloc.h"
30 #include "hashtab.h"
31
32 #define ARCH_SIZE       NN
33
34 #if ARCH_SIZE == 64
35 #define LOG_SECTION_ALIGN       3
36 #endif
37
38 #if ARCH_SIZE == 32
39 #define LOG_SECTION_ALIGN       2
40 #endif
41
42 /* THE RULES for all the stuff the linker creates --
43
44   GOT           Entries created in response to LTOFF or LTOFF_FPTR
45                 relocations.  Dynamic relocs created for dynamic
46                 symbols in an application; REL relocs for locals
47                 in a shared library.
48
49   FPTR          The canonical function descriptor.  Created for local
50                 symbols in applications.  Descriptors for dynamic symbols
51                 and local symbols in shared libraries are created by
52                 ld.so.  Thus there are no dynamic relocs against these
53                 objects.  The FPTR relocs for such _are_ passed through
54                 to the dynamic relocation tables.
55
56   FULL_PLT      Created for a PCREL21B relocation against a dynamic symbol.
57                 Requires the creation of a PLTOFF entry.  This does not
58                 require any dynamic relocations.
59
60   PLTOFF        Created by PLTOFF relocations.  For local symbols, this
61                 is an alternate function descriptor, and in shared libraries
62                 requires two REL relocations.  Note that this cannot be
63                 transformed into an FPTR relocation, since it must be in
64                 range of the GP.  For dynamic symbols, this is a function
65                 descriptor for a MIN_PLT entry, and requires one IPLT reloc.
66
67   MIN_PLT       Created by PLTOFF entries against dynamic symbols.  This
68                 does not require dynamic relocations.  */
69
70 /* Only add code for vms when the vms target is enabled.  This is required
71    because it depends on vms-lib.c for its archive format and we don't want
72    to compile that code if it is not used.  */
73 #if ARCH_SIZE == 64 && \
74   (defined (HAVE_bfd_elf64_ia64_vms_vec) || defined (HAVE_all_vecs))
75 #define INCLUDE_IA64_VMS
76 #endif
77
78
79 #define NELEMS(a)       ((int) (sizeof (a) / sizeof ((a)[0])))
80
81 typedef struct bfd_hash_entry *(*new_hash_entry_func)
82   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
83
84 /* In dynamically (linker-) created sections, we generally need to keep track
85    of the place a symbol or expression got allocated to. This is done via hash
86    tables that store entries of the following type.  */
87
88 struct elfNN_ia64_dyn_sym_info
89 {
90   /* The addend for which this entry is relevant.  */
91   bfd_vma addend;
92
93   bfd_vma got_offset;
94   bfd_vma fptr_offset;
95   bfd_vma pltoff_offset;
96   bfd_vma plt_offset;
97   bfd_vma plt2_offset;
98   bfd_vma tprel_offset;
99   bfd_vma dtpmod_offset;
100   bfd_vma dtprel_offset;
101
102   /* The symbol table entry, if any, that this was derived from.  */
103   struct elf_link_hash_entry *h;
104
105   /* Used to count non-got, non-plt relocations for delayed sizing
106      of relocation sections.  */
107   struct elfNN_ia64_dyn_reloc_entry
108   {
109     struct elfNN_ia64_dyn_reloc_entry *next;
110     asection *srel;
111     int type;
112     int count;
113
114     /* Is this reloc against readonly section? */
115     bfd_boolean reltext;
116   } *reloc_entries;
117
118   /* TRUE when the section contents have been updated.  */
119   unsigned got_done : 1;
120   unsigned fptr_done : 1;
121   unsigned pltoff_done : 1;
122   unsigned tprel_done : 1;
123   unsigned dtpmod_done : 1;
124   unsigned dtprel_done : 1;
125
126   /* TRUE for the different kinds of linker data we want created.  */
127   unsigned want_got : 1;
128   unsigned want_gotx : 1;
129   unsigned want_fptr : 1;
130   unsigned want_ltoff_fptr : 1;
131   unsigned want_plt : 1;
132   unsigned want_plt2 : 1;
133   unsigned want_pltoff : 1;
134   unsigned want_tprel : 1;
135   unsigned want_dtpmod : 1;
136   unsigned want_dtprel : 1;
137 };
138
139 struct elfNN_ia64_local_hash_entry
140 {
141   int id;
142   unsigned int r_sym;
143   /* The number of elements in elfNN_ia64_dyn_sym_info array.  */
144   unsigned int count;
145   /* The number of sorted elements in elfNN_ia64_dyn_sym_info array.  */
146   unsigned int sorted_count;
147   /* The size of elfNN_ia64_dyn_sym_info array.  */
148   unsigned int size;
149   /* The array of elfNN_ia64_dyn_sym_info.  */
150   struct elfNN_ia64_dyn_sym_info *info;
151
152   /* TRUE if this hash entry's addends was translated for
153      SHF_MERGE optimization.  */
154   unsigned sec_merge_done : 1;
155 };
156
157 struct elfNN_ia64_link_hash_entry
158 {
159   struct elf_link_hash_entry root;
160   /* The number of elements in elfNN_ia64_dyn_sym_info array.  */
161   unsigned int count;
162   /* The number of sorted elements in elfNN_ia64_dyn_sym_info array.  */
163   unsigned int sorted_count;
164   /* The size of elfNN_ia64_dyn_sym_info array.  */
165   unsigned int size;
166   /* The array of elfNN_ia64_dyn_sym_info.  */
167   struct elfNN_ia64_dyn_sym_info *info;
168 };
169
170 struct elfNN_ia64_link_hash_table
171 {
172   /* The main hash table.  */
173   struct elf_link_hash_table root;
174
175   asection *fptr_sec;           /* Function descriptor table (or NULL).  */
176   asection *rel_fptr_sec;       /* Dynamic relocation section for same.  */
177   asection *pltoff_sec;         /* Private descriptors for plt (or NULL).  */
178   asection *rel_pltoff_sec;     /* Dynamic relocation section for same.  */
179
180   bfd_size_type minplt_entries; /* Number of minplt entries.  */
181   unsigned reltext : 1;         /* Are there relocs against readonly sections?  */
182   unsigned self_dtpmod_done : 1;/* Has self DTPMOD entry been finished?  */
183   bfd_vma self_dtpmod_offset;   /* .got offset to self DTPMOD entry.  */
184   /* There are maybe R_IA64_GPREL22 relocations, including those
185      optimized from R_IA64_LTOFF22X, against non-SHF_IA_64_SHORT
186      sections.  We need to record those sections so that we can choose
187      a proper GP to cover all R_IA64_GPREL22 relocations.  */
188   asection *max_short_sec;      /* Maximum short output section.  */
189   bfd_vma max_short_offset;     /* Maximum short offset.  */
190   asection *min_short_sec;      /* Minimum short output section.  */
191   bfd_vma min_short_offset;     /* Minimum short offset.  */
192
193   htab_t loc_hash_table;
194   void *loc_hash_memory;
195 };
196
197 struct elfNN_ia64_allocate_data
198 {
199   struct bfd_link_info *info;
200   bfd_size_type ofs;
201   bfd_boolean only_got;
202 };
203
204 #define elfNN_ia64_hash_table(p) \
205   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
206   == IA64_ELF_DATA ? ((struct elfNN_ia64_link_hash_table *) ((p)->hash)) : NULL)
207
208 static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
209   (struct elfNN_ia64_link_hash_table *ia64_info,
210    struct elf_link_hash_entry *h,
211    bfd *abfd, const Elf_Internal_Rela *rel, bfd_boolean create);
212 static bfd_boolean elfNN_ia64_dynamic_symbol_p
213   (struct elf_link_hash_entry *h, struct bfd_link_info *info, int);
214 static bfd_reloc_status_type elfNN_ia64_install_value
215   (bfd_byte *hit_addr, bfd_vma val, unsigned int r_type);
216 static bfd_boolean elfNN_ia64_choose_gp
217   (bfd *abfd, struct bfd_link_info *info);
218 static void elfNN_ia64_relax_ldxmov
219   (bfd_byte *contents, bfd_vma off);
220 static void elfNN_ia64_dyn_sym_traverse
221   (struct elfNN_ia64_link_hash_table *ia64_info,
222    bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR),
223    PTR info);
224 static bfd_boolean allocate_global_data_got
225   (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
226 static bfd_boolean allocate_global_fptr_got
227   (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
228 static bfd_boolean allocate_local_got
229   (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
230 static bfd_boolean elfNN_ia64_hpux_vec
231   (const bfd_target *vec);
232 static bfd_boolean allocate_dynrel_entries
233   (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
234 static asection *get_pltoff
235   (bfd *abfd, struct bfd_link_info *info,
236    struct elfNN_ia64_link_hash_table *ia64_info);
237 \f
238 /* ia64-specific relocation.  */
239
240 /* Perform a relocation.  Not much to do here as all the hard work is
241    done in elfNN_ia64_final_link_relocate.  */
242 static bfd_reloc_status_type
243 elfNN_ia64_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
244                   asymbol *sym ATTRIBUTE_UNUSED,
245                   PTR data ATTRIBUTE_UNUSED, asection *input_section,
246                   bfd *output_bfd, char **error_message)
247 {
248   if (output_bfd)
249     {
250       reloc->address += input_section->output_offset;
251       return bfd_reloc_ok;
252     }
253
254   if (input_section->flags & SEC_DEBUGGING)
255     return bfd_reloc_continue;
256
257   *error_message = "Unsupported call to elfNN_ia64_reloc";
258   return bfd_reloc_notsupported;
259 }
260
261 #define IA64_HOWTO(TYPE, NAME, SIZE, PCREL, IN)                 \
262   HOWTO (TYPE, 0, SIZE, 0, PCREL, 0, complain_overflow_signed,  \
263          elfNN_ia64_reloc, NAME, FALSE, 0, -1, IN)
264
265 /* This table has to be sorted according to increasing number of the
266    TYPE field.  */
267 static reloc_howto_type ia64_howto_table[] =
268   {
269     IA64_HOWTO (R_IA64_NONE,        "NONE",        0, FALSE, TRUE),
270
271     IA64_HOWTO (R_IA64_IMM14,       "IMM14",       0, FALSE, TRUE),
272     IA64_HOWTO (R_IA64_IMM22,       "IMM22",       0, FALSE, TRUE),
273     IA64_HOWTO (R_IA64_IMM64,       "IMM64",       0, FALSE, TRUE),
274     IA64_HOWTO (R_IA64_DIR32MSB,    "DIR32MSB",    2, FALSE, TRUE),
275     IA64_HOWTO (R_IA64_DIR32LSB,    "DIR32LSB",    2, FALSE, TRUE),
276     IA64_HOWTO (R_IA64_DIR64MSB,    "DIR64MSB",    4, FALSE, TRUE),
277     IA64_HOWTO (R_IA64_DIR64LSB,    "DIR64LSB",    4, FALSE, TRUE),
278
279     IA64_HOWTO (R_IA64_GPREL22,     "GPREL22",     0, FALSE, TRUE),
280     IA64_HOWTO (R_IA64_GPREL64I,    "GPREL64I",    0, FALSE, TRUE),
281     IA64_HOWTO (R_IA64_GPREL32MSB,  "GPREL32MSB",  2, FALSE, TRUE),
282     IA64_HOWTO (R_IA64_GPREL32LSB,  "GPREL32LSB",  2, FALSE, TRUE),
283     IA64_HOWTO (R_IA64_GPREL64MSB,  "GPREL64MSB",  4, FALSE, TRUE),
284     IA64_HOWTO (R_IA64_GPREL64LSB,  "GPREL64LSB",  4, FALSE, TRUE),
285
286     IA64_HOWTO (R_IA64_LTOFF22,     "LTOFF22",     0, FALSE, TRUE),
287     IA64_HOWTO (R_IA64_LTOFF64I,    "LTOFF64I",    0, FALSE, TRUE),
288
289     IA64_HOWTO (R_IA64_PLTOFF22,    "PLTOFF22",    0, FALSE, TRUE),
290     IA64_HOWTO (R_IA64_PLTOFF64I,   "PLTOFF64I",   0, FALSE, TRUE),
291     IA64_HOWTO (R_IA64_PLTOFF64MSB, "PLTOFF64MSB", 4, FALSE, TRUE),
292     IA64_HOWTO (R_IA64_PLTOFF64LSB, "PLTOFF64LSB", 4, FALSE, TRUE),
293
294     IA64_HOWTO (R_IA64_FPTR64I,     "FPTR64I",     0, FALSE, TRUE),
295     IA64_HOWTO (R_IA64_FPTR32MSB,   "FPTR32MSB",   2, FALSE, TRUE),
296     IA64_HOWTO (R_IA64_FPTR32LSB,   "FPTR32LSB",   2, FALSE, TRUE),
297     IA64_HOWTO (R_IA64_FPTR64MSB,   "FPTR64MSB",   4, FALSE, TRUE),
298     IA64_HOWTO (R_IA64_FPTR64LSB,   "FPTR64LSB",   4, FALSE, TRUE),
299
300     IA64_HOWTO (R_IA64_PCREL60B,    "PCREL60B",    0, TRUE, TRUE),
301     IA64_HOWTO (R_IA64_PCREL21B,    "PCREL21B",    0, TRUE, TRUE),
302     IA64_HOWTO (R_IA64_PCREL21M,    "PCREL21M",    0, TRUE, TRUE),
303     IA64_HOWTO (R_IA64_PCREL21F,    "PCREL21F",    0, TRUE, TRUE),
304     IA64_HOWTO (R_IA64_PCREL32MSB,  "PCREL32MSB",  2, TRUE, TRUE),
305     IA64_HOWTO (R_IA64_PCREL32LSB,  "PCREL32LSB",  2, TRUE, TRUE),
306     IA64_HOWTO (R_IA64_PCREL64MSB,  "PCREL64MSB",  4, TRUE, TRUE),
307     IA64_HOWTO (R_IA64_PCREL64LSB,  "PCREL64LSB",  4, TRUE, TRUE),
308
309     IA64_HOWTO (R_IA64_LTOFF_FPTR22, "LTOFF_FPTR22", 0, FALSE, TRUE),
310     IA64_HOWTO (R_IA64_LTOFF_FPTR64I, "LTOFF_FPTR64I", 0, FALSE, TRUE),
311     IA64_HOWTO (R_IA64_LTOFF_FPTR32MSB, "LTOFF_FPTR32MSB", 2, FALSE, TRUE),
312     IA64_HOWTO (R_IA64_LTOFF_FPTR32LSB, "LTOFF_FPTR32LSB", 2, FALSE, TRUE),
313     IA64_HOWTO (R_IA64_LTOFF_FPTR64MSB, "LTOFF_FPTR64MSB", 4, FALSE, TRUE),
314     IA64_HOWTO (R_IA64_LTOFF_FPTR64LSB, "LTOFF_FPTR64LSB", 4, FALSE, TRUE),
315
316     IA64_HOWTO (R_IA64_SEGREL32MSB, "SEGREL32MSB", 2, FALSE, TRUE),
317     IA64_HOWTO (R_IA64_SEGREL32LSB, "SEGREL32LSB", 2, FALSE, TRUE),
318     IA64_HOWTO (R_IA64_SEGREL64MSB, "SEGREL64MSB", 4, FALSE, TRUE),
319     IA64_HOWTO (R_IA64_SEGREL64LSB, "SEGREL64LSB", 4, FALSE, TRUE),
320
321     IA64_HOWTO (R_IA64_SECREL32MSB, "SECREL32MSB", 2, FALSE, TRUE),
322     IA64_HOWTO (R_IA64_SECREL32LSB, "SECREL32LSB", 2, FALSE, TRUE),
323     IA64_HOWTO (R_IA64_SECREL64MSB, "SECREL64MSB", 4, FALSE, TRUE),
324     IA64_HOWTO (R_IA64_SECREL64LSB, "SECREL64LSB", 4, FALSE, TRUE),
325
326     IA64_HOWTO (R_IA64_REL32MSB,    "REL32MSB",    2, FALSE, TRUE),
327     IA64_HOWTO (R_IA64_REL32LSB,    "REL32LSB",    2, FALSE, TRUE),
328     IA64_HOWTO (R_IA64_REL64MSB,    "REL64MSB",    4, FALSE, TRUE),
329     IA64_HOWTO (R_IA64_REL64LSB,    "REL64LSB",    4, FALSE, TRUE),
330
331     IA64_HOWTO (R_IA64_LTV32MSB,    "LTV32MSB",    2, FALSE, TRUE),
332     IA64_HOWTO (R_IA64_LTV32LSB,    "LTV32LSB",    2, FALSE, TRUE),
333     IA64_HOWTO (R_IA64_LTV64MSB,    "LTV64MSB",    4, FALSE, TRUE),
334     IA64_HOWTO (R_IA64_LTV64LSB,    "LTV64LSB",    4, FALSE, TRUE),
335
336     IA64_HOWTO (R_IA64_PCREL21BI,   "PCREL21BI",   0, TRUE, TRUE),
337     IA64_HOWTO (R_IA64_PCREL22,     "PCREL22",     0, TRUE, TRUE),
338     IA64_HOWTO (R_IA64_PCREL64I,    "PCREL64I",    0, TRUE, TRUE),
339
340     IA64_HOWTO (R_IA64_IPLTMSB,     "IPLTMSB",     4, FALSE, TRUE),
341     IA64_HOWTO (R_IA64_IPLTLSB,     "IPLTLSB",     4, FALSE, TRUE),
342     IA64_HOWTO (R_IA64_COPY,        "COPY",        4, FALSE, TRUE),
343     IA64_HOWTO (R_IA64_LTOFF22X,    "LTOFF22X",    0, FALSE, TRUE),
344     IA64_HOWTO (R_IA64_LDXMOV,      "LDXMOV",      0, FALSE, TRUE),
345
346     IA64_HOWTO (R_IA64_TPREL14,     "TPREL14",     0, FALSE, FALSE),
347     IA64_HOWTO (R_IA64_TPREL22,     "TPREL22",     0, FALSE, FALSE),
348     IA64_HOWTO (R_IA64_TPREL64I,    "TPREL64I",    0, FALSE, FALSE),
349     IA64_HOWTO (R_IA64_TPREL64MSB,  "TPREL64MSB",  4, FALSE, FALSE),
350     IA64_HOWTO (R_IA64_TPREL64LSB,  "TPREL64LSB",  4, FALSE, FALSE),
351     IA64_HOWTO (R_IA64_LTOFF_TPREL22, "LTOFF_TPREL22",  0, FALSE, FALSE),
352
353     IA64_HOWTO (R_IA64_DTPMOD64MSB, "DTPMOD64MSB",  4, FALSE, FALSE),
354     IA64_HOWTO (R_IA64_DTPMOD64LSB, "DTPMOD64LSB",  4, FALSE, FALSE),
355     IA64_HOWTO (R_IA64_LTOFF_DTPMOD22, "LTOFF_DTPMOD22", 0, FALSE, FALSE),
356
357     IA64_HOWTO (R_IA64_DTPREL14,    "DTPREL14",    0, FALSE, FALSE),
358     IA64_HOWTO (R_IA64_DTPREL22,    "DTPREL22",    0, FALSE, FALSE),
359     IA64_HOWTO (R_IA64_DTPREL64I,   "DTPREL64I",   0, FALSE, FALSE),
360     IA64_HOWTO (R_IA64_DTPREL32MSB, "DTPREL32MSB", 2, FALSE, FALSE),
361     IA64_HOWTO (R_IA64_DTPREL32LSB, "DTPREL32LSB", 2, FALSE, FALSE),
362     IA64_HOWTO (R_IA64_DTPREL64MSB, "DTPREL64MSB", 4, FALSE, FALSE),
363     IA64_HOWTO (R_IA64_DTPREL64LSB, "DTPREL64LSB", 4, FALSE, FALSE),
364     IA64_HOWTO (R_IA64_LTOFF_DTPREL22, "LTOFF_DTPREL22", 0, FALSE, FALSE),
365   };
366
367 static unsigned char elf_code_to_howto_index[R_IA64_MAX_RELOC_CODE + 1];
368
369 /* Given a BFD reloc type, return the matching HOWTO structure.  */
370
371 static reloc_howto_type *
372 lookup_howto (unsigned int rtype)
373 {
374   static int inited = 0;
375   int i;
376
377   if (!inited)
378     {
379       inited = 1;
380
381       memset (elf_code_to_howto_index, 0xff, sizeof (elf_code_to_howto_index));
382       for (i = 0; i < NELEMS (ia64_howto_table); ++i)
383         elf_code_to_howto_index[ia64_howto_table[i].type] = i;
384     }
385
386   if (rtype > R_IA64_MAX_RELOC_CODE)
387     return 0;
388   i = elf_code_to_howto_index[rtype];
389   if (i >= NELEMS (ia64_howto_table))
390     return 0;
391   return ia64_howto_table + i;
392 }
393
394 static reloc_howto_type*
395 elfNN_ia64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
396                               bfd_reloc_code_real_type bfd_code)
397 {
398   unsigned int rtype;
399
400   switch (bfd_code)
401     {
402     case BFD_RELOC_NONE:                rtype = R_IA64_NONE; break;
403
404     case BFD_RELOC_IA64_IMM14:          rtype = R_IA64_IMM14; break;
405     case BFD_RELOC_IA64_IMM22:          rtype = R_IA64_IMM22; break;
406     case BFD_RELOC_IA64_IMM64:          rtype = R_IA64_IMM64; break;
407
408     case BFD_RELOC_IA64_DIR32MSB:       rtype = R_IA64_DIR32MSB; break;
409     case BFD_RELOC_IA64_DIR32LSB:       rtype = R_IA64_DIR32LSB; break;
410     case BFD_RELOC_IA64_DIR64MSB:       rtype = R_IA64_DIR64MSB; break;
411     case BFD_RELOC_IA64_DIR64LSB:       rtype = R_IA64_DIR64LSB; break;
412
413     case BFD_RELOC_IA64_GPREL22:        rtype = R_IA64_GPREL22; break;
414     case BFD_RELOC_IA64_GPREL64I:       rtype = R_IA64_GPREL64I; break;
415     case BFD_RELOC_IA64_GPREL32MSB:     rtype = R_IA64_GPREL32MSB; break;
416     case BFD_RELOC_IA64_GPREL32LSB:     rtype = R_IA64_GPREL32LSB; break;
417     case BFD_RELOC_IA64_GPREL64MSB:     rtype = R_IA64_GPREL64MSB; break;
418     case BFD_RELOC_IA64_GPREL64LSB:     rtype = R_IA64_GPREL64LSB; break;
419
420     case BFD_RELOC_IA64_LTOFF22:        rtype = R_IA64_LTOFF22; break;
421     case BFD_RELOC_IA64_LTOFF64I:       rtype = R_IA64_LTOFF64I; break;
422
423     case BFD_RELOC_IA64_PLTOFF22:       rtype = R_IA64_PLTOFF22; break;
424     case BFD_RELOC_IA64_PLTOFF64I:      rtype = R_IA64_PLTOFF64I; break;
425     case BFD_RELOC_IA64_PLTOFF64MSB:    rtype = R_IA64_PLTOFF64MSB; break;
426     case BFD_RELOC_IA64_PLTOFF64LSB:    rtype = R_IA64_PLTOFF64LSB; break;
427     case BFD_RELOC_IA64_FPTR64I:        rtype = R_IA64_FPTR64I; break;
428     case BFD_RELOC_IA64_FPTR32MSB:      rtype = R_IA64_FPTR32MSB; break;
429     case BFD_RELOC_IA64_FPTR32LSB:      rtype = R_IA64_FPTR32LSB; break;
430     case BFD_RELOC_IA64_FPTR64MSB:      rtype = R_IA64_FPTR64MSB; break;
431     case BFD_RELOC_IA64_FPTR64LSB:      rtype = R_IA64_FPTR64LSB; break;
432
433     case BFD_RELOC_IA64_PCREL21B:       rtype = R_IA64_PCREL21B; break;
434     case BFD_RELOC_IA64_PCREL21BI:      rtype = R_IA64_PCREL21BI; break;
435     case BFD_RELOC_IA64_PCREL21M:       rtype = R_IA64_PCREL21M; break;
436     case BFD_RELOC_IA64_PCREL21F:       rtype = R_IA64_PCREL21F; break;
437     case BFD_RELOC_IA64_PCREL22:        rtype = R_IA64_PCREL22; break;
438     case BFD_RELOC_IA64_PCREL60B:       rtype = R_IA64_PCREL60B; break;
439     case BFD_RELOC_IA64_PCREL64I:       rtype = R_IA64_PCREL64I; break;
440     case BFD_RELOC_IA64_PCREL32MSB:     rtype = R_IA64_PCREL32MSB; break;
441     case BFD_RELOC_IA64_PCREL32LSB:     rtype = R_IA64_PCREL32LSB; break;
442     case BFD_RELOC_IA64_PCREL64MSB:     rtype = R_IA64_PCREL64MSB; break;
443     case BFD_RELOC_IA64_PCREL64LSB:     rtype = R_IA64_PCREL64LSB; break;
444
445     case BFD_RELOC_IA64_LTOFF_FPTR22:   rtype = R_IA64_LTOFF_FPTR22; break;
446     case BFD_RELOC_IA64_LTOFF_FPTR64I:  rtype = R_IA64_LTOFF_FPTR64I; break;
447     case BFD_RELOC_IA64_LTOFF_FPTR32MSB: rtype = R_IA64_LTOFF_FPTR32MSB; break;
448     case BFD_RELOC_IA64_LTOFF_FPTR32LSB: rtype = R_IA64_LTOFF_FPTR32LSB; break;
449     case BFD_RELOC_IA64_LTOFF_FPTR64MSB: rtype = R_IA64_LTOFF_FPTR64MSB; break;
450     case BFD_RELOC_IA64_LTOFF_FPTR64LSB: rtype = R_IA64_LTOFF_FPTR64LSB; break;
451
452     case BFD_RELOC_IA64_SEGREL32MSB:    rtype = R_IA64_SEGREL32MSB; break;
453     case BFD_RELOC_IA64_SEGREL32LSB:    rtype = R_IA64_SEGREL32LSB; break;
454     case BFD_RELOC_IA64_SEGREL64MSB:    rtype = R_IA64_SEGREL64MSB; break;
455     case BFD_RELOC_IA64_SEGREL64LSB:    rtype = R_IA64_SEGREL64LSB; break;
456
457     case BFD_RELOC_IA64_SECREL32MSB:    rtype = R_IA64_SECREL32MSB; break;
458     case BFD_RELOC_IA64_SECREL32LSB:    rtype = R_IA64_SECREL32LSB; break;
459     case BFD_RELOC_IA64_SECREL64MSB:    rtype = R_IA64_SECREL64MSB; break;
460     case BFD_RELOC_IA64_SECREL64LSB:    rtype = R_IA64_SECREL64LSB; break;
461
462     case BFD_RELOC_IA64_REL32MSB:       rtype = R_IA64_REL32MSB; break;
463     case BFD_RELOC_IA64_REL32LSB:       rtype = R_IA64_REL32LSB; break;
464     case BFD_RELOC_IA64_REL64MSB:       rtype = R_IA64_REL64MSB; break;
465     case BFD_RELOC_IA64_REL64LSB:       rtype = R_IA64_REL64LSB; break;
466
467     case BFD_RELOC_IA64_LTV32MSB:       rtype = R_IA64_LTV32MSB; break;
468     case BFD_RELOC_IA64_LTV32LSB:       rtype = R_IA64_LTV32LSB; break;
469     case BFD_RELOC_IA64_LTV64MSB:       rtype = R_IA64_LTV64MSB; break;
470     case BFD_RELOC_IA64_LTV64LSB:       rtype = R_IA64_LTV64LSB; break;
471
472     case BFD_RELOC_IA64_IPLTMSB:        rtype = R_IA64_IPLTMSB; break;
473     case BFD_RELOC_IA64_IPLTLSB:        rtype = R_IA64_IPLTLSB; break;
474     case BFD_RELOC_IA64_COPY:           rtype = R_IA64_COPY; break;
475     case BFD_RELOC_IA64_LTOFF22X:       rtype = R_IA64_LTOFF22X; break;
476     case BFD_RELOC_IA64_LDXMOV:         rtype = R_IA64_LDXMOV; break;
477
478     case BFD_RELOC_IA64_TPREL14:        rtype = R_IA64_TPREL14; break;
479     case BFD_RELOC_IA64_TPREL22:        rtype = R_IA64_TPREL22; break;
480     case BFD_RELOC_IA64_TPREL64I:       rtype = R_IA64_TPREL64I; break;
481     case BFD_RELOC_IA64_TPREL64MSB:     rtype = R_IA64_TPREL64MSB; break;
482     case BFD_RELOC_IA64_TPREL64LSB:     rtype = R_IA64_TPREL64LSB; break;
483     case BFD_RELOC_IA64_LTOFF_TPREL22:  rtype = R_IA64_LTOFF_TPREL22; break;
484
485     case BFD_RELOC_IA64_DTPMOD64MSB:    rtype = R_IA64_DTPMOD64MSB; break;
486     case BFD_RELOC_IA64_DTPMOD64LSB:    rtype = R_IA64_DTPMOD64LSB; break;
487     case BFD_RELOC_IA64_LTOFF_DTPMOD22: rtype = R_IA64_LTOFF_DTPMOD22; break;
488
489     case BFD_RELOC_IA64_DTPREL14:       rtype = R_IA64_DTPREL14; break;
490     case BFD_RELOC_IA64_DTPREL22:       rtype = R_IA64_DTPREL22; break;
491     case BFD_RELOC_IA64_DTPREL64I:      rtype = R_IA64_DTPREL64I; break;
492     case BFD_RELOC_IA64_DTPREL32MSB:    rtype = R_IA64_DTPREL32MSB; break;
493     case BFD_RELOC_IA64_DTPREL32LSB:    rtype = R_IA64_DTPREL32LSB; break;
494     case BFD_RELOC_IA64_DTPREL64MSB:    rtype = R_IA64_DTPREL64MSB; break;
495     case BFD_RELOC_IA64_DTPREL64LSB:    rtype = R_IA64_DTPREL64LSB; break;
496     case BFD_RELOC_IA64_LTOFF_DTPREL22: rtype = R_IA64_LTOFF_DTPREL22; break;
497
498     default: return 0;
499     }
500   return lookup_howto (rtype);
501 }
502
503 static reloc_howto_type *
504 elfNN_ia64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
505                               const char *r_name)
506 {
507   unsigned int i;
508
509   for (i = 0;
510        i < sizeof (ia64_howto_table) / sizeof (ia64_howto_table[0]);
511        i++)
512     if (ia64_howto_table[i].name != NULL
513         && strcasecmp (ia64_howto_table[i].name, r_name) == 0)
514       return &ia64_howto_table[i];
515
516   return NULL;
517 }
518
519 /* Given a ELF reloc, return the matching HOWTO structure.  */
520
521 static void
522 elfNN_ia64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
523                           arelent *bfd_reloc,
524                           Elf_Internal_Rela *elf_reloc)
525 {
526   bfd_reloc->howto
527     = lookup_howto ((unsigned int) ELFNN_R_TYPE (elf_reloc->r_info));
528 }
529 \f
530 #define PLT_HEADER_SIZE         (3 * 16)
531 #define PLT_MIN_ENTRY_SIZE      (1 * 16)
532 #define PLT_FULL_ENTRY_SIZE     (2 * 16)
533 #define PLT_RESERVED_WORDS      3
534
535 static const bfd_byte plt_header[PLT_HEADER_SIZE] =
536 {
537   0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21,  /*   [MMI]       mov r2=r14;;       */
538   0xe0, 0x00, 0x08, 0x00, 0x48, 0x00,  /*               addl r14=0,r2      */
539   0x00, 0x00, 0x04, 0x00,              /*               nop.i 0x0;;        */
540   0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14,  /*   [MMI]       ld8 r16=[r14],8;;  */
541   0x10, 0x41, 0x38, 0x30, 0x28, 0x00,  /*               ld8 r17=[r14],8    */
542   0x00, 0x00, 0x04, 0x00,              /*               nop.i 0x0;;        */
543   0x11, 0x08, 0x00, 0x1c, 0x18, 0x10,  /*   [MIB]       ld8 r1=[r14]       */
544   0x60, 0x88, 0x04, 0x80, 0x03, 0x00,  /*               mov b6=r17         */
545   0x60, 0x00, 0x80, 0x00               /*               br.few b6;;        */
546 };
547
548 static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
549 {
550   0x11, 0x78, 0x00, 0x00, 0x00, 0x24,  /*   [MIB]       mov r15=0          */
551   0x00, 0x00, 0x00, 0x02, 0x00, 0x00,  /*               nop.i 0x0          */
552   0x00, 0x00, 0x00, 0x40               /*               br.few 0 <PLT0>;;  */
553 };
554
555 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
556 {
557   0x0b, 0x78, 0x00, 0x02, 0x00, 0x24,  /*   [MMI]       addl r15=0,r1;;    */
558   0x00, 0x41, 0x3c, 0x70, 0x29, 0xc0,  /*               ld8.acq r16=[r15],8*/
559   0x01, 0x08, 0x00, 0x84,              /*               mov r14=r1;;       */
560   0x11, 0x08, 0x00, 0x1e, 0x18, 0x10,  /*   [MIB]       ld8 r1=[r15]       */
561   0x60, 0x80, 0x04, 0x80, 0x03, 0x00,  /*               mov b6=r16         */
562   0x60, 0x00, 0x80, 0x00               /*               br.few b6;;        */
563 };
564
565 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
566
567 static const bfd_byte oor_brl[16] =
568 {
569   0x05, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MLX]        nop.m 0            */
570   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /*               brl.sptk.few tgt;; */
571   0x00, 0x00, 0x00, 0xc0
572 };
573
574 static const bfd_byte oor_ip[48] =
575 {
576   0x04, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MLX]        nop.m 0            */
577   0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,  /*               movl r15=0         */
578   0x01, 0x00, 0x00, 0x60,
579   0x03, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MII]        nop.m 0            */
580   0x00, 0x01, 0x00, 0x60, 0x00, 0x00,  /*               mov r16=ip;;       */
581   0xf2, 0x80, 0x00, 0x80,              /*               add r16=r15,r16;;  */
582   0x11, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MIB]        nop.m 0            */
583   0x60, 0x80, 0x04, 0x80, 0x03, 0x00,  /*               mov b6=r16         */
584   0x60, 0x00, 0x80, 0x00               /*               br b6;;            */
585 };
586
587 static size_t oor_branch_size = sizeof (oor_brl);
588
589 void
590 bfd_elfNN_ia64_after_parse (int itanium)
591 {
592   oor_branch_size = itanium ? sizeof (oor_ip) : sizeof (oor_brl);
593 }
594
595 #define BTYPE_SHIFT     6
596 #define Y_SHIFT         26
597 #define X6_SHIFT        27
598 #define X4_SHIFT        27
599 #define X3_SHIFT        33
600 #define X2_SHIFT        31
601 #define X_SHIFT         33
602 #define OPCODE_SHIFT    37
603
604 #define OPCODE_BITS     (0xfLL << OPCODE_SHIFT)
605 #define X6_BITS         (0x3fLL << X6_SHIFT)
606 #define X4_BITS         (0xfLL << X4_SHIFT)
607 #define X3_BITS         (0x7LL << X3_SHIFT)
608 #define X2_BITS         (0x3LL << X2_SHIFT)
609 #define X_BITS          (0x1LL << X_SHIFT)
610 #define Y_BITS          (0x1LL << Y_SHIFT)
611 #define BTYPE_BITS      (0x7LL << BTYPE_SHIFT)
612 #define PREDICATE_BITS  (0x3fLL)
613
614 #define IS_NOP_B(i) \
615   (((i) & (OPCODE_BITS | X6_BITS)) == (2LL << OPCODE_SHIFT))
616 #define IS_NOP_F(i) \
617   (((i) & (OPCODE_BITS | X_BITS | X6_BITS | Y_BITS)) \
618    == (0x1LL << X6_SHIFT))
619 #define IS_NOP_I(i) \
620   (((i) & (OPCODE_BITS | X3_BITS | X6_BITS | Y_BITS)) \
621    == (0x1LL << X6_SHIFT))
622 #define IS_NOP_M(i) \
623   (((i) & (OPCODE_BITS | X3_BITS | X2_BITS | X4_BITS | Y_BITS)) \
624    == (0x1LL << X4_SHIFT))
625 #define IS_BR_COND(i) \
626   (((i) & (OPCODE_BITS | BTYPE_BITS)) == (0x4LL << OPCODE_SHIFT))
627 #define IS_BR_CALL(i) \
628   (((i) & OPCODE_BITS) == (0x5LL << OPCODE_SHIFT))
629
630 static bfd_boolean
631 elfNN_ia64_relax_br (bfd_byte *contents, bfd_vma off)
632 {
633   unsigned int template_val, mlx;
634   bfd_vma t0, t1, s0, s1, s2, br_code;
635   long br_slot;
636   bfd_byte *hit_addr;
637
638   hit_addr = (bfd_byte *) (contents + off);
639   br_slot = (long) hit_addr & 0x3;
640   hit_addr -= br_slot;
641   t0 = bfd_getl64 (hit_addr + 0);
642   t1 = bfd_getl64 (hit_addr + 8);
643
644   /* Check if we can turn br into brl.  A label is always at the start
645      of the bundle.  Even if there are predicates on NOPs, we still
646      perform this optimization.  */
647   template_val = t0 & 0x1e;
648   s0 = (t0 >> 5) & 0x1ffffffffffLL;
649   s1 = ((t0 >> 46) | (t1 << 18)) & 0x1ffffffffffLL;
650   s2 = (t1 >> 23) & 0x1ffffffffffLL;
651   switch (br_slot)
652     {
653     case 0:
654       /* Check if slot 1 and slot 2 are NOPs. Possible template is
655          BBB.  We only need to check nop.b.  */
656       if (!(IS_NOP_B (s1) && IS_NOP_B (s2)))
657         return FALSE;
658       br_code = s0;
659       break;
660     case 1:
661       /* Check if slot 2 is NOP. Possible templates are MBB and BBB.
662          For BBB, slot 0 also has to be nop.b.  */
663       if (!((template_val == 0x12                               /* MBB */
664              && IS_NOP_B (s2))
665             || (template_val == 0x16                    /* BBB */
666                 && IS_NOP_B (s0)
667                 && IS_NOP_B (s2))))
668         return FALSE;
669       br_code = s1;
670       break;
671     case 2:
672       /* Check if slot 1 is NOP. Possible templates are MIB, MBB, BBB,
673          MMB and MFB. For BBB, slot 0 also has to be nop.b.  */
674       if (!((template_val == 0x10                               /* MIB */
675              && IS_NOP_I (s1))
676             || (template_val == 0x12                    /* MBB */
677                 && IS_NOP_B (s1))
678             || (template_val == 0x16                    /* BBB */
679                 && IS_NOP_B (s0)
680                 && IS_NOP_B (s1))
681             || (template_val == 0x18                    /* MMB */
682                 && IS_NOP_M (s1))
683             || (template_val == 0x1c                    /* MFB */
684                 && IS_NOP_F (s1))))
685         return FALSE;
686       br_code = s2;
687       break;
688     default:
689       /* It should never happen.  */
690       abort ();
691     }
692
693   /* We can turn br.cond/br.call into brl.cond/brl.call.  */
694   if (!(IS_BR_COND (br_code) || IS_BR_CALL (br_code)))
695     return FALSE;
696
697   /* Turn br into brl by setting bit 40.  */
698   br_code |= 0x1LL << 40;
699
700   /* Turn the old bundle into a MLX bundle with the same stop-bit
701      variety.  */
702   if (t0 & 0x1)
703     mlx = 0x5;
704   else
705     mlx = 0x4;
706
707   if (template_val == 0x16)
708     {
709       /* For BBB, we need to put nop.m in slot 0.  We keep the original
710          predicate only if slot 0 isn't br.  */
711       if (br_slot == 0)
712         t0 = 0LL;
713       else
714         t0 &= PREDICATE_BITS << 5;
715       t0 |= 0x1LL << (X4_SHIFT + 5);
716     }
717   else
718     {
719       /* Keep the original instruction in slot 0.  */
720       t0 &= 0x1ffffffffffLL << 5;
721     }
722
723   t0 |= mlx;
724
725   /* Put brl in slot 1.  */
726   t1 = br_code << 23;
727
728   bfd_putl64 (t0, hit_addr);
729   bfd_putl64 (t1, hit_addr + 8);
730   return TRUE;
731 }
732
733 static void
734 elfNN_ia64_relax_brl (bfd_byte *contents, bfd_vma off)
735 {
736   int template_val;
737   bfd_byte *hit_addr;
738   bfd_vma t0, t1, i0, i1, i2;
739
740   hit_addr = (bfd_byte *) (contents + off);
741   hit_addr -= (long) hit_addr & 0x3;
742   t0 = bfd_getl64 (hit_addr);
743   t1 = bfd_getl64 (hit_addr + 8);
744
745   /* Keep the instruction in slot 0. */
746   i0 = (t0 >> 5) & 0x1ffffffffffLL;
747   /* Use nop.b for slot 1. */
748   i1 = 0x4000000000LL;
749   /* For slot 2, turn brl into br by masking out bit 40.  */
750   i2 = (t1 >> 23) & 0x0ffffffffffLL;
751
752   /* Turn a MLX bundle into a MBB bundle with the same stop-bit
753      variety.  */
754   if (t0 & 0x1)
755     template_val = 0x13;
756   else
757     template_val = 0x12;
758   t0 = (i1 << 46) | (i0 << 5) | template_val;
759   t1 = (i2 << 23) | (i1 >> 18);
760
761   bfd_putl64 (t0, hit_addr);
762   bfd_putl64 (t1, hit_addr + 8);
763 }
764
765 /* Rename some of the generic section flags to better document how they
766    are used here.  */
767 #define skip_relax_pass_0 sec_flg0
768 #define skip_relax_pass_1 sec_flg1
769
770 \f
771 /* These functions do relaxation for IA-64 ELF.  */
772
773 static void
774 elfNN_ia64_update_short_info (asection *sec, bfd_vma offset,
775                               struct elfNN_ia64_link_hash_table *ia64_info)
776 {
777   /* Skip ABS and SHF_IA_64_SHORT sections.  */
778   if (sec == bfd_abs_section_ptr
779       || (sec->flags & SEC_SMALL_DATA) != 0)
780     return;
781
782   if (!ia64_info->min_short_sec)
783     {
784       ia64_info->max_short_sec = sec;
785       ia64_info->max_short_offset = offset;
786       ia64_info->min_short_sec = sec;
787       ia64_info->min_short_offset = offset;
788     }
789   else if (sec == ia64_info->max_short_sec
790            && offset > ia64_info->max_short_offset)
791     ia64_info->max_short_offset = offset;
792   else if (sec == ia64_info->min_short_sec
793            && offset < ia64_info->min_short_offset)
794     ia64_info->min_short_offset = offset;
795   else if (sec->output_section->vma
796            > ia64_info->max_short_sec->vma)
797     {
798       ia64_info->max_short_sec = sec;
799       ia64_info->max_short_offset = offset;
800     }
801   else if (sec->output_section->vma
802            < ia64_info->min_short_sec->vma)
803     {
804       ia64_info->min_short_sec = sec;
805       ia64_info->min_short_offset = offset;
806     }
807 }
808
809 static bfd_boolean
810 elfNN_ia64_relax_section (bfd *abfd, asection *sec,
811                           struct bfd_link_info *link_info,
812                           bfd_boolean *again)
813 {
814   struct one_fixup
815     {
816       struct one_fixup *next;
817       asection *tsec;
818       bfd_vma toff;
819       bfd_vma trampoff;
820     };
821
822   Elf_Internal_Shdr *symtab_hdr;
823   Elf_Internal_Rela *internal_relocs;
824   Elf_Internal_Rela *irel, *irelend;
825   bfd_byte *contents;
826   Elf_Internal_Sym *isymbuf = NULL;
827   struct elfNN_ia64_link_hash_table *ia64_info;
828   struct one_fixup *fixups = NULL;
829   bfd_boolean changed_contents = FALSE;
830   bfd_boolean changed_relocs = FALSE;
831   bfd_boolean changed_got = FALSE;
832   bfd_boolean skip_relax_pass_0 = TRUE;
833   bfd_boolean skip_relax_pass_1 = TRUE;
834   bfd_vma gp = 0;
835
836   /* Assume we're not going to change any sizes, and we'll only need
837      one pass.  */
838   *again = FALSE;
839
840   if (link_info->relocatable)
841     (*link_info->callbacks->einfo)
842       (_("%P%F: --relax and -r may not be used together\n"));
843
844   /* Don't even try to relax for non-ELF outputs.  */
845   if (!is_elf_hash_table (link_info->hash))
846     return FALSE;
847
848   /* Nothing to do if there are no relocations or there is no need for
849      the current pass.  */
850   if ((sec->flags & SEC_RELOC) == 0
851       || sec->reloc_count == 0
852       || (link_info->relax_pass == 0 && sec->skip_relax_pass_0)
853       || (link_info->relax_pass == 1 && sec->skip_relax_pass_1))
854     return TRUE;
855
856   ia64_info = elfNN_ia64_hash_table (link_info);
857   if (ia64_info == NULL)
858     return FALSE;
859
860   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
861
862   /* Load the relocations for this section.  */
863   internal_relocs = (_bfd_elf_link_read_relocs
864                      (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
865                       link_info->keep_memory));
866   if (internal_relocs == NULL)
867     return FALSE;
868
869   irelend = internal_relocs + sec->reloc_count;
870
871   /* Get the section contents.  */
872   if (elf_section_data (sec)->this_hdr.contents != NULL)
873     contents = elf_section_data (sec)->this_hdr.contents;
874   else
875     {
876       if (!bfd_malloc_and_get_section (abfd, sec, &contents))
877         goto error_return;
878     }
879
880   for (irel = internal_relocs; irel < irelend; irel++)
881     {
882       unsigned long r_type = ELFNN_R_TYPE (irel->r_info);
883       bfd_vma symaddr, reladdr, trampoff, toff, roff;
884       asection *tsec;
885       struct one_fixup *f;
886       bfd_size_type amt;
887       bfd_boolean is_branch;
888       struct elfNN_ia64_dyn_sym_info *dyn_i;
889       char symtype;
890
891       switch (r_type)
892         {
893         case R_IA64_PCREL21B:
894         case R_IA64_PCREL21BI:
895         case R_IA64_PCREL21M:
896         case R_IA64_PCREL21F:
897           /* In pass 1, all br relaxations are done. We can skip it. */
898           if (link_info->relax_pass == 1)
899             continue;
900           skip_relax_pass_0 = FALSE;
901           is_branch = TRUE;
902           break;
903
904         case R_IA64_PCREL60B:
905           /* We can't optimize brl to br in pass 0 since br relaxations
906              will increase the code size. Defer it to pass 1.  */
907           if (link_info->relax_pass == 0)
908             {
909               skip_relax_pass_1 = FALSE;
910               continue;
911             }
912           is_branch = TRUE;
913           break;
914
915         case R_IA64_GPREL22:
916           /* Update max_short_sec/min_short_sec.  */
917
918         case R_IA64_LTOFF22X:
919         case R_IA64_LDXMOV:
920           /* We can't relax ldx/mov in pass 0 since br relaxations will
921              increase the code size. Defer it to pass 1.  */
922           if (link_info->relax_pass == 0)
923             {
924               skip_relax_pass_1 = FALSE;
925               continue;
926             }
927           is_branch = FALSE;
928           break;
929
930         default:
931           continue;
932         }
933
934       /* Get the value of the symbol referred to by the reloc.  */
935       if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
936         {
937           /* A local symbol.  */
938           Elf_Internal_Sym *isym;
939
940           /* Read this BFD's local symbols.  */
941           if (isymbuf == NULL)
942             {
943               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
944               if (isymbuf == NULL)
945                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
946                                                 symtab_hdr->sh_info, 0,
947                                                 NULL, NULL, NULL);
948               if (isymbuf == 0)
949                 goto error_return;
950             }
951
952           isym = isymbuf + ELFNN_R_SYM (irel->r_info);
953           if (isym->st_shndx == SHN_UNDEF)
954             continue;   /* We can't do anything with undefined symbols.  */
955           else if (isym->st_shndx == SHN_ABS)
956             tsec = bfd_abs_section_ptr;
957           else if (isym->st_shndx == SHN_COMMON)
958             tsec = bfd_com_section_ptr;
959           else if (isym->st_shndx == SHN_IA_64_ANSI_COMMON)
960             tsec = bfd_com_section_ptr;
961           else
962             tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
963
964           toff = isym->st_value;
965           dyn_i = get_dyn_sym_info (ia64_info, NULL, abfd, irel, FALSE);
966           symtype = ELF_ST_TYPE (isym->st_info);
967         }
968       else
969         {
970           unsigned long indx;
971           struct elf_link_hash_entry *h;
972
973           indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
974           h = elf_sym_hashes (abfd)[indx];
975           BFD_ASSERT (h != NULL);
976
977           while (h->root.type == bfd_link_hash_indirect
978                  || h->root.type == bfd_link_hash_warning)
979             h = (struct elf_link_hash_entry *) h->root.u.i.link;
980
981           dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, FALSE);
982
983           /* For branches to dynamic symbols, we're interested instead
984              in a branch to the PLT entry.  */
985           if (is_branch && dyn_i && dyn_i->want_plt2)
986             {
987               /* Internal branches shouldn't be sent to the PLT.
988                  Leave this for now and we'll give an error later.  */
989               if (r_type != R_IA64_PCREL21B)
990                 continue;
991
992               tsec = ia64_info->root.splt;
993               toff = dyn_i->plt2_offset;
994               BFD_ASSERT (irel->r_addend == 0);
995             }
996
997           /* Can't do anything else with dynamic symbols.  */
998           else if (elfNN_ia64_dynamic_symbol_p (h, link_info, r_type))
999             continue;
1000
1001           else
1002             {
1003               /* We can't do anything with undefined symbols.  */
1004               if (h->root.type == bfd_link_hash_undefined
1005                   || h->root.type == bfd_link_hash_undefweak)
1006                 continue;
1007
1008               tsec = h->root.u.def.section;
1009               toff = h->root.u.def.value;
1010             }
1011
1012           symtype = h->type;
1013         }
1014
1015       if (tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
1016         {
1017           /* At this stage in linking, no SEC_MERGE symbol has been
1018              adjusted, so all references to such symbols need to be
1019              passed through _bfd_merged_section_offset.  (Later, in
1020              relocate_section, all SEC_MERGE symbols *except* for
1021              section symbols have been adjusted.)
1022
1023              gas may reduce relocations against symbols in SEC_MERGE
1024              sections to a relocation against the section symbol when
1025              the original addend was zero.  When the reloc is against
1026              a section symbol we should include the addend in the
1027              offset passed to _bfd_merged_section_offset, since the
1028              location of interest is the original symbol.  On the
1029              other hand, an access to "sym+addend" where "sym" is not
1030              a section symbol should not include the addend;  Such an
1031              access is presumed to be an offset from "sym";  The
1032              location of interest is just "sym".  */
1033            if (symtype == STT_SECTION)
1034              toff += irel->r_addend;
1035
1036            toff = _bfd_merged_section_offset (abfd, &tsec,
1037                                               elf_section_data (tsec)->sec_info,
1038                                               toff);
1039
1040            if (symtype != STT_SECTION)
1041              toff += irel->r_addend;
1042         }
1043       else
1044         toff += irel->r_addend;
1045
1046       symaddr = tsec->output_section->vma + tsec->output_offset + toff;
1047
1048       roff = irel->r_offset;
1049
1050       if (is_branch)
1051         {
1052           bfd_signed_vma offset;
1053
1054           reladdr = (sec->output_section->vma
1055                      + sec->output_offset
1056                      + roff) & (bfd_vma) -4;
1057
1058           /* The .plt section is aligned at 32byte and the .text section
1059              is aligned at 64byte. The .text section is right after the
1060              .plt section.  After the first relaxation pass, linker may
1061              increase the gap between the .plt and .text sections up
1062              to 32byte.  We assume linker will always insert 32byte
1063              between the .plt and .text sections after the the first
1064              relaxation pass.  */
1065           if (tsec == ia64_info->root.splt)
1066             offset = -0x1000000 + 32;
1067           else
1068             offset = -0x1000000;
1069
1070           /* If the branch is in range, no need to do anything.  */
1071           if ((bfd_signed_vma) (symaddr - reladdr) >= offset 
1072               && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
1073             {
1074               /* If the 60-bit branch is in 21-bit range, optimize it. */
1075               if (r_type == R_IA64_PCREL60B)
1076                 {
1077                   elfNN_ia64_relax_brl (contents, roff);
1078
1079                   irel->r_info
1080                     = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1081                                     R_IA64_PCREL21B);
1082
1083                   /* If the original relocation offset points to slot
1084                      1, change it to slot 2.  */
1085                   if ((irel->r_offset & 3) == 1)
1086                     irel->r_offset += 1;
1087                 }
1088
1089               continue;
1090             }
1091           else if (r_type == R_IA64_PCREL60B)
1092             continue;
1093           else if (elfNN_ia64_relax_br (contents, roff))
1094             {
1095               irel->r_info
1096                 = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1097                                 R_IA64_PCREL60B);
1098
1099               /* Make the relocation offset point to slot 1.  */
1100               irel->r_offset = (irel->r_offset & ~((bfd_vma) 0x3)) + 1;
1101               continue;
1102             }
1103
1104           /* We can't put a trampoline in a .init/.fini section. Issue
1105              an error.  */
1106           if (strcmp (sec->output_section->name, ".init") == 0
1107               || strcmp (sec->output_section->name, ".fini") == 0)
1108             {
1109               (*_bfd_error_handler)
1110                 (_("%B: Can't relax br at 0x%lx in section `%A'. Please use brl or indirect branch."),
1111                  sec->owner, sec, (unsigned long) roff);
1112               bfd_set_error (bfd_error_bad_value);
1113               goto error_return;
1114             }
1115
1116           /* If the branch and target are in the same section, you've
1117              got one honking big section and we can't help you unless
1118              you are branching backwards.  You'll get an error message
1119              later.  */
1120           if (tsec == sec && toff > roff)
1121             continue;
1122
1123           /* Look for an existing fixup to this address.  */
1124           for (f = fixups; f ; f = f->next)
1125             if (f->tsec == tsec && f->toff == toff)
1126               break;
1127
1128           if (f == NULL)
1129             {
1130               /* Two alternatives: If it's a branch to a PLT entry, we can
1131                  make a copy of the FULL_PLT entry.  Otherwise, we'll have
1132                  to use a `brl' insn to get where we're going.  */
1133
1134               size_t size;
1135
1136               if (tsec == ia64_info->root.splt)
1137                 size = sizeof (plt_full_entry);
1138               else
1139                 size = oor_branch_size;
1140
1141               /* Resize the current section to make room for the new branch. */
1142               trampoff = (sec->size + 15) & (bfd_vma) -16;
1143
1144               /* If trampoline is out of range, there is nothing we
1145                  can do.  */
1146               offset = trampoff - (roff & (bfd_vma) -4);
1147               if (offset < -0x1000000 || offset > 0x0FFFFF0)
1148                 continue;
1149
1150               amt = trampoff + size;
1151               contents = (bfd_byte *) bfd_realloc (contents, amt);
1152               if (contents == NULL)
1153                 goto error_return;
1154               sec->size = amt;
1155
1156               if (tsec == ia64_info->root.splt)
1157                 {
1158                   memcpy (contents + trampoff, plt_full_entry, size);
1159
1160                   /* Hijack the old relocation for use as the PLTOFF reloc.  */
1161                   irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1162                                                R_IA64_PLTOFF22);
1163                   irel->r_offset = trampoff;
1164                 }
1165               else
1166                 {
1167                   if (size == sizeof (oor_ip))
1168                     {
1169                       memcpy (contents + trampoff, oor_ip, size);
1170                       irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1171                                                    R_IA64_PCREL64I);
1172                       irel->r_addend -= 16;
1173                       irel->r_offset = trampoff + 2;
1174                     }
1175                   else
1176                     {
1177                       memcpy (contents + trampoff, oor_brl, size);
1178                       irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1179                                                    R_IA64_PCREL60B);
1180                       irel->r_offset = trampoff + 2;
1181                     }
1182
1183                 }
1184
1185               /* Record the fixup so we don't do it again this section.  */
1186               f = (struct one_fixup *)
1187                 bfd_malloc ((bfd_size_type) sizeof (*f));
1188               f->next = fixups;
1189               f->tsec = tsec;
1190               f->toff = toff;
1191               f->trampoff = trampoff;
1192               fixups = f;
1193             }
1194           else
1195             {
1196               /* If trampoline is out of range, there is nothing we
1197                  can do.  */
1198               offset = f->trampoff - (roff & (bfd_vma) -4);
1199               if (offset < -0x1000000 || offset > 0x0FFFFF0)
1200                 continue;
1201
1202               /* Nop out the reloc, since we're finalizing things here.  */
1203               irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
1204             }
1205
1206           /* Fix up the existing branch to hit the trampoline.  */
1207           if (elfNN_ia64_install_value (contents + roff, offset, r_type)
1208               != bfd_reloc_ok)
1209             goto error_return;
1210
1211           changed_contents = TRUE;
1212           changed_relocs = TRUE;
1213         }
1214       else
1215         {
1216           /* Fetch the gp.  */
1217           if (gp == 0)
1218             {
1219               bfd *obfd = sec->output_section->owner;
1220               gp = _bfd_get_gp_value (obfd);
1221               if (gp == 0)
1222                 {
1223                   if (!elfNN_ia64_choose_gp (obfd, link_info))
1224                     goto error_return;
1225                   gp = _bfd_get_gp_value (obfd);
1226                 }
1227             }
1228
1229           /* If the data is out of range, do nothing.  */
1230           if ((bfd_signed_vma) (symaddr - gp) >= 0x200000
1231               ||(bfd_signed_vma) (symaddr - gp) < -0x200000)
1232             continue;
1233
1234           if (r_type == R_IA64_GPREL22)
1235             elfNN_ia64_update_short_info (tsec->output_section,
1236                                           tsec->output_offset + toff,
1237                                           ia64_info);
1238           else if (r_type == R_IA64_LTOFF22X)
1239             {
1240               irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1241                                            R_IA64_GPREL22);
1242               changed_relocs = TRUE;
1243               if (dyn_i->want_gotx)
1244                 {
1245                   dyn_i->want_gotx = 0;
1246                   changed_got |= !dyn_i->want_got;
1247                 }
1248
1249               elfNN_ia64_update_short_info (tsec->output_section,
1250                                             tsec->output_offset + toff,
1251                                             ia64_info);
1252             }
1253           else
1254             {
1255               elfNN_ia64_relax_ldxmov (contents, roff);
1256               irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
1257               changed_contents = TRUE;
1258               changed_relocs = TRUE;
1259             }
1260         }
1261     }
1262
1263   /* ??? If we created fixups, this may push the code segment large
1264      enough that the data segment moves, which will change the GP.
1265      Reset the GP so that we re-calculate next round.  We need to
1266      do this at the _beginning_ of the next round; now will not do.  */
1267
1268   /* Clean up and go home.  */
1269   while (fixups)
1270     {
1271       struct one_fixup *f = fixups;
1272       fixups = fixups->next;
1273       free (f);
1274     }
1275
1276   if (isymbuf != NULL
1277       && symtab_hdr->contents != (unsigned char *) isymbuf)
1278     {
1279       if (! link_info->keep_memory)
1280         free (isymbuf);
1281       else
1282         {
1283           /* Cache the symbols for elf_link_input_bfd.  */
1284           symtab_hdr->contents = (unsigned char *) isymbuf;
1285         }
1286     }
1287
1288   if (contents != NULL
1289       && elf_section_data (sec)->this_hdr.contents != contents)
1290     {
1291       if (!changed_contents && !link_info->keep_memory)
1292         free (contents);
1293       else
1294         {
1295           /* Cache the section contents for elf_link_input_bfd.  */
1296           elf_section_data (sec)->this_hdr.contents = contents;
1297         }
1298     }
1299
1300   if (elf_section_data (sec)->relocs != internal_relocs)
1301     {
1302       if (!changed_relocs)
1303         free (internal_relocs);
1304       else
1305         elf_section_data (sec)->relocs = internal_relocs;
1306     }
1307
1308   if (changed_got)
1309     {
1310       struct elfNN_ia64_allocate_data data;
1311       data.info = link_info;
1312       data.ofs = 0;
1313       ia64_info->self_dtpmod_offset = (bfd_vma) -1;
1314
1315       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
1316       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
1317       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
1318       ia64_info->root.sgot->size = data.ofs;
1319
1320       if (ia64_info->root.dynamic_sections_created
1321           && ia64_info->root.srelgot != NULL)
1322         {
1323           /* Resize .rela.got.  */
1324           ia64_info->root.srelgot->size = 0;
1325           if (link_info->shared
1326               && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
1327             ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
1328           data.only_got = TRUE;
1329           elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries,
1330                                        &data);
1331         }
1332     }
1333
1334   if (link_info->relax_pass == 0)
1335     {
1336       /* Pass 0 is only needed to relax br.  */
1337       sec->skip_relax_pass_0 = skip_relax_pass_0;
1338       sec->skip_relax_pass_1 = skip_relax_pass_1;
1339     }
1340
1341   *again = changed_contents || changed_relocs;
1342   return TRUE;
1343
1344  error_return:
1345   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
1346     free (isymbuf);
1347   if (contents != NULL
1348       && elf_section_data (sec)->this_hdr.contents != contents)
1349     free (contents);
1350   if (internal_relocs != NULL
1351       && elf_section_data (sec)->relocs != internal_relocs)
1352     free (internal_relocs);
1353   return FALSE;
1354 }
1355 #undef skip_relax_pass_0
1356 #undef skip_relax_pass_1
1357
1358 static void
1359 elfNN_ia64_relax_ldxmov (bfd_byte *contents, bfd_vma off)
1360 {
1361   int shift, r1, r3;
1362   bfd_vma dword, insn;
1363
1364   switch ((int)off & 0x3)
1365     {
1366     case 0: shift =  5; break;
1367     case 1: shift = 14; off += 3; break;
1368     case 2: shift = 23; off += 6; break;
1369     default:
1370       abort ();
1371     }
1372
1373   dword = bfd_getl64 (contents + off);
1374   insn = (dword >> shift) & 0x1ffffffffffLL;
1375
1376   r1 = (insn >> 6) & 127;
1377   r3 = (insn >> 20) & 127;
1378   if (r1 == r3)
1379     insn = 0x8000000;                              /* nop */
1380   else
1381     insn = (insn & 0x7f01fff) | 0x10800000000LL;   /* (qp) mov r1 = r3 */
1382
1383   dword &= ~(0x1ffffffffffLL << shift);
1384   dword |= (insn << shift);
1385   bfd_putl64 (dword, contents + off);
1386 }
1387 \f
1388 /* Return TRUE if NAME is an unwind table section name.  */
1389
1390 static inline bfd_boolean
1391 is_unwind_section_name (bfd *abfd, const char *name)
1392 {
1393   if (elfNN_ia64_hpux_vec (abfd->xvec)
1394       && !strcmp (name, ELF_STRING_ia64_unwind_hdr))
1395     return FALSE;
1396
1397   return ((CONST_STRNEQ (name, ELF_STRING_ia64_unwind)
1398            && ! CONST_STRNEQ (name, ELF_STRING_ia64_unwind_info))
1399           || CONST_STRNEQ (name, ELF_STRING_ia64_unwind_once));
1400 }
1401
1402 /* Handle an IA-64 specific section when reading an object file.  This
1403    is called when bfd_section_from_shdr finds a section with an unknown
1404    type.  */
1405
1406 static bfd_boolean
1407 elfNN_ia64_section_from_shdr (bfd *abfd,
1408                               Elf_Internal_Shdr *hdr,
1409                               const char *name,
1410                               int shindex)
1411 {
1412   asection *newsect;
1413
1414   /* There ought to be a place to keep ELF backend specific flags, but
1415      at the moment there isn't one.  We just keep track of the
1416      sections by their name, instead.  Fortunately, the ABI gives
1417      suggested names for all the MIPS specific sections, so we will
1418      probably get away with this.  */
1419   switch (hdr->sh_type)
1420     {
1421     case SHT_IA_64_UNWIND:
1422     case SHT_IA_64_HP_OPT_ANOT:
1423       break;
1424
1425     case SHT_IA_64_EXT:
1426       if (strcmp (name, ELF_STRING_ia64_archext) != 0)
1427         return FALSE;
1428       break;
1429
1430     default:
1431       return FALSE;
1432     }
1433
1434   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1435     return FALSE;
1436   newsect = hdr->bfd_section;
1437
1438   return TRUE;
1439 }
1440
1441 /* Convert IA-64 specific section flags to bfd internal section flags.  */
1442
1443 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
1444    flag.  */
1445
1446 static bfd_boolean
1447 elfNN_ia64_section_flags (flagword *flags,
1448                           const Elf_Internal_Shdr *hdr)
1449 {
1450   if (hdr->sh_flags & SHF_IA_64_SHORT)
1451     *flags |= SEC_SMALL_DATA;
1452
1453   return TRUE;
1454 }
1455
1456 /* Set the correct type for an IA-64 ELF section.  We do this by the
1457    section name, which is a hack, but ought to work.  */
1458
1459 static bfd_boolean
1460 elfNN_ia64_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr,
1461                           asection *sec)
1462 {
1463   const char *name;
1464
1465   name = bfd_get_section_name (abfd, sec);
1466
1467   if (is_unwind_section_name (abfd, name))
1468     {
1469       /* We don't have the sections numbered at this point, so sh_info
1470          is set later, in elfNN_ia64_final_write_processing.  */
1471       hdr->sh_type = SHT_IA_64_UNWIND;
1472       hdr->sh_flags |= SHF_LINK_ORDER;
1473     }
1474   else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
1475     hdr->sh_type = SHT_IA_64_EXT;
1476   else if (strcmp (name, ".HP.opt_annot") == 0)
1477     hdr->sh_type = SHT_IA_64_HP_OPT_ANOT;
1478   else if (strcmp (name, ".reloc") == 0)
1479     /* This is an ugly, but unfortunately necessary hack that is
1480        needed when producing EFI binaries on IA-64. It tells
1481        elf.c:elf_fake_sections() not to consider ".reloc" as a section
1482        containing ELF relocation info.  We need this hack in order to
1483        be able to generate ELF binaries that can be translated into
1484        EFI applications (which are essentially COFF objects).  Those
1485        files contain a COFF ".reloc" section inside an ELFNN object,
1486        which would normally cause BFD to segfault because it would
1487        attempt to interpret this section as containing relocation
1488        entries for section "oc".  With this hack enabled, ".reloc"
1489        will be treated as a normal data section, which will avoid the
1490        segfault.  However, you won't be able to create an ELFNN binary
1491        with a section named "oc" that needs relocations, but that's
1492        the kind of ugly side-effects you get when detecting section
1493        types based on their names...  In practice, this limitation is
1494        unlikely to bite.  */
1495     hdr->sh_type = SHT_PROGBITS;
1496
1497   if (sec->flags & SEC_SMALL_DATA)
1498     hdr->sh_flags |= SHF_IA_64_SHORT;
1499
1500   /* Some HP linkers look for the SHF_IA_64_HP_TLS flag instead of SHF_TLS. */
1501
1502   if (elfNN_ia64_hpux_vec (abfd->xvec) && (sec->flags & SHF_TLS))
1503     hdr->sh_flags |= SHF_IA_64_HP_TLS;
1504
1505   return TRUE;
1506 }
1507
1508 /* The final processing done just before writing out an IA-64 ELF
1509    object file.  */
1510
1511 static void
1512 elfNN_ia64_final_write_processing (bfd *abfd,
1513                                    bfd_boolean linker ATTRIBUTE_UNUSED)
1514 {
1515   Elf_Internal_Shdr *hdr;
1516   asection *s;
1517
1518   for (s = abfd->sections; s; s = s->next)
1519     {
1520       hdr = &elf_section_data (s)->this_hdr;
1521       switch (hdr->sh_type)
1522         {
1523         case SHT_IA_64_UNWIND:
1524           /* The IA-64 processor-specific ABI requires setting sh_link
1525              to the unwind section, whereas HP-UX requires sh_info to
1526              do so.  For maximum compatibility, we'll set both for
1527              now... */
1528           hdr->sh_info = hdr->sh_link;
1529           break;
1530         }
1531     }
1532
1533   if (! elf_flags_init (abfd))
1534     {
1535       unsigned long flags = 0;
1536
1537       if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
1538         flags |= EF_IA_64_BE;
1539       if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
1540         flags |= EF_IA_64_ABI64;
1541
1542       elf_elfheader(abfd)->e_flags = flags;
1543       elf_flags_init (abfd) = TRUE;
1544     }
1545 }
1546
1547 /* Hook called by the linker routine which adds symbols from an object
1548    file.  We use it to put .comm items in .sbss, and not .bss.  */
1549
1550 static bfd_boolean
1551 elfNN_ia64_add_symbol_hook (bfd *abfd,
1552                             struct bfd_link_info *info,
1553                             Elf_Internal_Sym *sym,
1554                             const char **namep ATTRIBUTE_UNUSED,
1555                             flagword *flagsp ATTRIBUTE_UNUSED,
1556                             asection **secp,
1557                             bfd_vma *valp)
1558 {
1559   if (sym->st_shndx == SHN_COMMON
1560       && !info->relocatable
1561       && sym->st_size <= elf_gp_size (abfd))
1562     {
1563       /* Common symbols less than or equal to -G nn bytes are
1564          automatically put into .sbss.  */
1565
1566       asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1567
1568       if (scomm == NULL)
1569         {
1570           scomm = bfd_make_section_with_flags (abfd, ".scommon",
1571                                                (SEC_ALLOC
1572                                                 | SEC_IS_COMMON
1573                                                 | SEC_LINKER_CREATED));
1574           if (scomm == NULL)
1575             return FALSE;
1576         }
1577
1578       *secp = scomm;
1579       *valp = sym->st_size;
1580     }
1581
1582   return TRUE;
1583 }
1584
1585 /* Return the number of additional phdrs we will need.  */
1586
1587 static int
1588 elfNN_ia64_additional_program_headers (bfd *abfd,
1589                                        struct bfd_link_info *info ATTRIBUTE_UNUSED)
1590 {
1591   asection *s;
1592   int ret = 0;
1593
1594   /* See if we need a PT_IA_64_ARCHEXT segment.  */
1595   s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1596   if (s && (s->flags & SEC_LOAD))
1597     ++ret;
1598
1599   /* Count how many PT_IA_64_UNWIND segments we need.  */
1600   for (s = abfd->sections; s; s = s->next)
1601     if (is_unwind_section_name (abfd, s->name) && (s->flags & SEC_LOAD))
1602       ++ret;
1603
1604   return ret;
1605 }
1606
1607 static bfd_boolean
1608 elfNN_ia64_modify_segment_map (bfd *abfd,
1609                                struct bfd_link_info *info ATTRIBUTE_UNUSED)
1610 {
1611   struct elf_segment_map *m, **pm;
1612   Elf_Internal_Shdr *hdr;
1613   asection *s;
1614
1615   /* If we need a PT_IA_64_ARCHEXT segment, it must come before
1616      all PT_LOAD segments.  */
1617   s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1618   if (s && (s->flags & SEC_LOAD))
1619     {
1620       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1621         if (m->p_type == PT_IA_64_ARCHEXT)
1622           break;
1623       if (m == NULL)
1624         {
1625           m = ((struct elf_segment_map *)
1626                bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1627           if (m == NULL)
1628             return FALSE;
1629
1630           m->p_type = PT_IA_64_ARCHEXT;
1631           m->count = 1;
1632           m->sections[0] = s;
1633
1634           /* We want to put it after the PHDR and INTERP segments.  */
1635           pm = &elf_tdata (abfd)->segment_map;
1636           while (*pm != NULL
1637                  && ((*pm)->p_type == PT_PHDR
1638                      || (*pm)->p_type == PT_INTERP))
1639             pm = &(*pm)->next;
1640
1641           m->next = *pm;
1642           *pm = m;
1643         }
1644     }
1645
1646   /* Install PT_IA_64_UNWIND segments, if needed.  */
1647   for (s = abfd->sections; s; s = s->next)
1648     {
1649       hdr = &elf_section_data (s)->this_hdr;
1650       if (hdr->sh_type != SHT_IA_64_UNWIND)
1651         continue;
1652
1653       if (s && (s->flags & SEC_LOAD))
1654         {
1655           for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1656             if (m->p_type == PT_IA_64_UNWIND)
1657               {
1658                 int i;
1659
1660                 /* Look through all sections in the unwind segment
1661                    for a match since there may be multiple sections
1662                    to a segment.  */
1663                 for (i = m->count - 1; i >= 0; --i)
1664                   if (m->sections[i] == s)
1665                     break;
1666
1667                 if (i >= 0)
1668                   break;
1669               }
1670
1671           if (m == NULL)
1672             {
1673               m = ((struct elf_segment_map *)
1674                    bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1675               if (m == NULL)
1676                 return FALSE;
1677
1678               m->p_type = PT_IA_64_UNWIND;
1679               m->count = 1;
1680               m->sections[0] = s;
1681               m->next = NULL;
1682
1683               /* We want to put it last.  */
1684               pm = &elf_tdata (abfd)->segment_map;
1685               while (*pm != NULL)
1686                 pm = &(*pm)->next;
1687               *pm = m;
1688             }
1689         }
1690     }
1691
1692   return TRUE;
1693 }
1694
1695 /* Turn on PF_IA_64_NORECOV if needed.  This involves traversing all of
1696    the input sections for each output section in the segment and testing
1697    for SHF_IA_64_NORECOV on each.  */
1698
1699 static bfd_boolean
1700 elfNN_ia64_modify_program_headers (bfd *abfd,
1701                                    struct bfd_link_info *info ATTRIBUTE_UNUSED)
1702 {
1703   struct elf_obj_tdata *tdata = elf_tdata (abfd);
1704   struct elf_segment_map *m;
1705   Elf_Internal_Phdr *p;
1706
1707   for (p = tdata->phdr, m = tdata->segment_map; m != NULL; m = m->next, p++)
1708     if (m->p_type == PT_LOAD)
1709       {
1710         int i;
1711         for (i = m->count - 1; i >= 0; --i)
1712           {
1713             struct bfd_link_order *order = m->sections[i]->map_head.link_order;
1714
1715             while (order != NULL)
1716               {
1717                 if (order->type == bfd_indirect_link_order)
1718                   {
1719                     asection *is = order->u.indirect.section;
1720                     bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
1721                     if (flags & SHF_IA_64_NORECOV)
1722                       {
1723                         p->p_flags |= PF_IA_64_NORECOV;
1724                         goto found;
1725                       }
1726                   }
1727                 order = order->next;
1728               }
1729           }
1730       found:;
1731       }
1732
1733   return TRUE;
1734 }
1735
1736 /* According to the Tahoe assembler spec, all labels starting with a
1737    '.' are local.  */
1738
1739 static bfd_boolean
1740 elfNN_ia64_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
1741                                 const char *name)
1742 {
1743   return name[0] == '.';
1744 }
1745
1746 /* Should we do dynamic things to this symbol?  */
1747
1748 static bfd_boolean
1749 elfNN_ia64_dynamic_symbol_p (struct elf_link_hash_entry *h,
1750                              struct bfd_link_info *info, int r_type)
1751 {
1752   bfd_boolean ignore_protected
1753     = ((r_type & 0xf8) == 0x40          /* FPTR relocs */
1754        || (r_type & 0xf8) == 0x50);     /* LTOFF_FPTR relocs */
1755
1756   return _bfd_elf_dynamic_symbol_p (h, info, ignore_protected);
1757 }
1758 \f
1759 static struct bfd_hash_entry*
1760 elfNN_ia64_new_elf_hash_entry (struct bfd_hash_entry *entry,
1761                                struct bfd_hash_table *table,
1762                                const char *string)
1763 {
1764   struct elfNN_ia64_link_hash_entry *ret;
1765   ret = (struct elfNN_ia64_link_hash_entry *) entry;
1766
1767   /* Allocate the structure if it has not already been allocated by a
1768      subclass.  */
1769   if (!ret)
1770     ret = bfd_hash_allocate (table, sizeof (*ret));
1771
1772   if (!ret)
1773     return 0;
1774
1775   /* Call the allocation method of the superclass.  */
1776   ret = ((struct elfNN_ia64_link_hash_entry *)
1777          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1778                                      table, string));
1779
1780   ret->info = NULL;
1781   ret->count = 0;
1782   ret->sorted_count = 0;
1783   ret->size = 0;
1784   return (struct bfd_hash_entry *) ret;
1785 }
1786
1787 static void
1788 elfNN_ia64_hash_copy_indirect (struct bfd_link_info *info,
1789                                struct elf_link_hash_entry *xdir,
1790                                struct elf_link_hash_entry *xind)
1791 {
1792   struct elfNN_ia64_link_hash_entry *dir, *ind;
1793
1794   dir = (struct elfNN_ia64_link_hash_entry *) xdir;
1795   ind = (struct elfNN_ia64_link_hash_entry *) xind;
1796
1797   /* Copy down any references that we may have already seen to the
1798      symbol which just became indirect.  */
1799
1800   dir->root.ref_dynamic |= ind->root.ref_dynamic;
1801   dir->root.ref_regular |= ind->root.ref_regular;
1802   dir->root.ref_regular_nonweak |= ind->root.ref_regular_nonweak;
1803   dir->root.needs_plt |= ind->root.needs_plt;
1804
1805   if (ind->root.root.type != bfd_link_hash_indirect)
1806     return;
1807
1808   /* Copy over the got and plt data.  This would have been done
1809      by check_relocs.  */
1810
1811   if (ind->info != NULL)
1812     {
1813       struct elfNN_ia64_dyn_sym_info *dyn_i;
1814       unsigned int count;
1815
1816       if (dir->info)
1817         free (dir->info);
1818
1819       dir->info = ind->info;
1820       dir->count = ind->count;
1821       dir->sorted_count = ind->sorted_count;
1822       dir->size = ind->size;
1823
1824       ind->info = NULL;
1825       ind->count = 0;
1826       ind->sorted_count = 0;
1827       ind->size = 0;
1828
1829       /* Fix up the dyn_sym_info pointers to the global symbol.  */
1830       for (count = dir->count, dyn_i = dir->info;
1831            count != 0;
1832            count--, dyn_i++)
1833         dyn_i->h = &dir->root;
1834     }
1835
1836   /* Copy over the dynindx.  */
1837
1838   if (ind->root.dynindx != -1)
1839     {
1840       if (dir->root.dynindx != -1)
1841         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1842                                 dir->root.dynstr_index);
1843       dir->root.dynindx = ind->root.dynindx;
1844       dir->root.dynstr_index = ind->root.dynstr_index;
1845       ind->root.dynindx = -1;
1846       ind->root.dynstr_index = 0;
1847     }
1848 }
1849
1850 static void
1851 elfNN_ia64_hash_hide_symbol (struct bfd_link_info *info,
1852                              struct elf_link_hash_entry *xh,
1853                              bfd_boolean force_local)
1854 {
1855   struct elfNN_ia64_link_hash_entry *h;
1856   struct elfNN_ia64_dyn_sym_info *dyn_i;
1857   unsigned int count;
1858
1859   h = (struct elfNN_ia64_link_hash_entry *)xh;
1860
1861   _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
1862
1863   for (count = h->count, dyn_i = h->info;
1864        count != 0;
1865        count--, dyn_i++)
1866     {
1867       dyn_i->want_plt2 = 0;
1868       dyn_i->want_plt = 0;
1869     }
1870 }
1871
1872 /* Compute a hash of a local hash entry.  */
1873
1874 static hashval_t
1875 elfNN_ia64_local_htab_hash (const void *ptr)
1876 {
1877   struct elfNN_ia64_local_hash_entry *entry
1878     = (struct elfNN_ia64_local_hash_entry *) ptr;
1879
1880   return ELF_LOCAL_SYMBOL_HASH (entry->id, entry->r_sym);
1881 }
1882
1883 /* Compare local hash entries.  */
1884
1885 static int
1886 elfNN_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
1887 {
1888   struct elfNN_ia64_local_hash_entry *entry1
1889     = (struct elfNN_ia64_local_hash_entry *) ptr1;
1890   struct elfNN_ia64_local_hash_entry *entry2
1891     = (struct elfNN_ia64_local_hash_entry *) ptr2;
1892
1893   return entry1->id == entry2->id && entry1->r_sym == entry2->r_sym;
1894 }
1895
1896 /* Create the derived linker hash table.  The IA-64 ELF port uses this
1897    derived hash table to keep information specific to the IA-64 ElF
1898    linker (without using static variables).  */
1899
1900 static struct bfd_link_hash_table *
1901 elfNN_ia64_hash_table_create (bfd *abfd)
1902 {
1903   struct elfNN_ia64_link_hash_table *ret;
1904
1905   ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
1906   if (!ret)
1907     return NULL;
1908
1909   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
1910                                       elfNN_ia64_new_elf_hash_entry,
1911                                       sizeof (struct elfNN_ia64_link_hash_entry),
1912                                       IA64_ELF_DATA))
1913     {
1914       free (ret);
1915       return NULL;
1916     }
1917
1918   ret->loc_hash_table = htab_try_create (1024, elfNN_ia64_local_htab_hash,
1919                                          elfNN_ia64_local_htab_eq, NULL);
1920   ret->loc_hash_memory = objalloc_create ();
1921   if (!ret->loc_hash_table || !ret->loc_hash_memory)
1922     {
1923       free (ret);
1924       return NULL;
1925     }
1926
1927   return &ret->root.root;
1928 }
1929
1930 /* Free the global elfNN_ia64_dyn_sym_info array.  */
1931
1932 static bfd_boolean
1933 elfNN_ia64_global_dyn_info_free (void **xentry,
1934                                 PTR unused ATTRIBUTE_UNUSED)
1935 {
1936   struct elfNN_ia64_link_hash_entry *entry
1937     = (struct elfNN_ia64_link_hash_entry *) xentry;
1938
1939   if (entry->root.root.type == bfd_link_hash_warning)
1940     entry = (struct elfNN_ia64_link_hash_entry *) entry->root.root.u.i.link;
1941
1942   if (entry->info)
1943     {
1944       free (entry->info);
1945       entry->info = NULL;
1946       entry->count = 0;
1947       entry->sorted_count = 0;
1948       entry->size = 0;
1949     }
1950
1951   return TRUE;
1952 }
1953
1954 /* Free the local elfNN_ia64_dyn_sym_info array.  */
1955
1956 static bfd_boolean
1957 elfNN_ia64_local_dyn_info_free (void **slot,
1958                                 PTR unused ATTRIBUTE_UNUSED)
1959 {
1960   struct elfNN_ia64_local_hash_entry *entry
1961     = (struct elfNN_ia64_local_hash_entry *) *slot;
1962
1963   if (entry->info)
1964     {
1965       free (entry->info);
1966       entry->info = NULL;
1967       entry->count = 0;
1968       entry->sorted_count = 0;
1969       entry->size = 0;
1970     }
1971
1972   return TRUE;
1973 }
1974
1975 /* Destroy IA-64 linker hash table.  */
1976
1977 static void
1978 elfNN_ia64_hash_table_free (struct bfd_link_hash_table *hash)
1979 {
1980   struct elfNN_ia64_link_hash_table *ia64_info
1981     = (struct elfNN_ia64_link_hash_table *) hash;
1982   if (ia64_info->loc_hash_table)
1983     {
1984       htab_traverse (ia64_info->loc_hash_table,
1985                      elfNN_ia64_local_dyn_info_free, NULL);
1986       htab_delete (ia64_info->loc_hash_table);
1987     }
1988   if (ia64_info->loc_hash_memory)
1989     objalloc_free ((struct objalloc *) ia64_info->loc_hash_memory);
1990   elf_link_hash_traverse (&ia64_info->root,
1991                           elfNN_ia64_global_dyn_info_free, NULL);
1992   _bfd_generic_link_hash_table_free (hash);
1993 }
1994
1995 /* Traverse both local and global hash tables.  */
1996
1997 struct elfNN_ia64_dyn_sym_traverse_data
1998 {
1999   bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR);
2000   PTR data;
2001 };
2002
2003 static bfd_boolean
2004 elfNN_ia64_global_dyn_sym_thunk (struct bfd_hash_entry *xentry,
2005                                  PTR xdata)
2006 {
2007   struct elfNN_ia64_link_hash_entry *entry
2008     = (struct elfNN_ia64_link_hash_entry *) xentry;
2009   struct elfNN_ia64_dyn_sym_traverse_data *data
2010     = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
2011   struct elfNN_ia64_dyn_sym_info *dyn_i;
2012   unsigned int count;
2013
2014   if (entry->root.root.type == bfd_link_hash_warning)
2015     entry = (struct elfNN_ia64_link_hash_entry *) entry->root.root.u.i.link;
2016
2017   for (count = entry->count, dyn_i = entry->info;
2018        count != 0;
2019        count--, dyn_i++)
2020     if (! (*data->func) (dyn_i, data->data))
2021       return FALSE;
2022   return TRUE;
2023 }
2024
2025 static bfd_boolean
2026 elfNN_ia64_local_dyn_sym_thunk (void **slot, PTR xdata)
2027 {
2028   struct elfNN_ia64_local_hash_entry *entry
2029     = (struct elfNN_ia64_local_hash_entry *) *slot;
2030   struct elfNN_ia64_dyn_sym_traverse_data *data
2031     = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
2032   struct elfNN_ia64_dyn_sym_info *dyn_i;
2033   unsigned int count;
2034
2035   for (count = entry->count, dyn_i = entry->info;
2036        count != 0;
2037        count--, dyn_i++)
2038     if (! (*data->func) (dyn_i, data->data))
2039       return FALSE;
2040   return TRUE;
2041 }
2042
2043 static void
2044 elfNN_ia64_dyn_sym_traverse (struct elfNN_ia64_link_hash_table *ia64_info,
2045                              bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR),
2046                              PTR data)
2047 {
2048   struct elfNN_ia64_dyn_sym_traverse_data xdata;
2049
2050   xdata.func = func;
2051   xdata.data = data;
2052
2053   elf_link_hash_traverse (&ia64_info->root,
2054                           elfNN_ia64_global_dyn_sym_thunk, &xdata);
2055   htab_traverse (ia64_info->loc_hash_table,
2056                  elfNN_ia64_local_dyn_sym_thunk, &xdata);
2057 }
2058 \f
2059 static bfd_boolean
2060 elfNN_ia64_create_dynamic_sections (bfd *abfd,
2061                                     struct bfd_link_info *info)
2062 {
2063   struct elfNN_ia64_link_hash_table *ia64_info;
2064   asection *s;
2065
2066   if (! _bfd_elf_create_dynamic_sections (abfd, info))
2067     return FALSE;
2068
2069   ia64_info = elfNN_ia64_hash_table (info);
2070   if (ia64_info == NULL)
2071     return FALSE;
2072
2073   {
2074     flagword flags = bfd_get_section_flags (abfd, ia64_info->root.sgot);
2075     bfd_set_section_flags (abfd, ia64_info->root.sgot,
2076                            SEC_SMALL_DATA | flags);
2077     /* The .got section is always aligned at 8 bytes.  */
2078     bfd_set_section_alignment (abfd, ia64_info->root.sgot, 3);
2079   }
2080
2081   if (!get_pltoff (abfd, info, ia64_info))
2082     return FALSE;
2083
2084   s = bfd_make_section_with_flags (abfd, ".rela.IA_64.pltoff",
2085                                    (SEC_ALLOC | SEC_LOAD
2086                                     | SEC_HAS_CONTENTS
2087                                     | SEC_IN_MEMORY
2088                                     | SEC_LINKER_CREATED
2089                                     | SEC_READONLY));
2090   if (s == NULL
2091       || !bfd_set_section_alignment (abfd, s, LOG_SECTION_ALIGN))
2092     return FALSE;
2093   ia64_info->rel_pltoff_sec = s;
2094
2095   return TRUE;
2096 }
2097
2098 /* Find and/or create a hash entry for local symbol.  */
2099 static struct elfNN_ia64_local_hash_entry *
2100 get_local_sym_hash (struct elfNN_ia64_link_hash_table *ia64_info,
2101                     bfd *abfd, const Elf_Internal_Rela *rel,
2102                     bfd_boolean create)
2103 {
2104   struct elfNN_ia64_local_hash_entry e, *ret;
2105   asection *sec = abfd->sections;
2106   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
2107                                        ELFNN_R_SYM (rel->r_info));
2108   void **slot;
2109
2110   e.id = sec->id;
2111   e.r_sym = ELFNN_R_SYM (rel->r_info);
2112   slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
2113                                    create ? INSERT : NO_INSERT);
2114
2115   if (!slot)
2116     return NULL;
2117
2118   if (*slot)
2119     return (struct elfNN_ia64_local_hash_entry *) *slot;
2120
2121   ret = (struct elfNN_ia64_local_hash_entry *)
2122         objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
2123                         sizeof (struct elfNN_ia64_local_hash_entry));
2124   if (ret)
2125     {
2126       memset (ret, 0, sizeof (*ret));
2127       ret->id = sec->id;
2128       ret->r_sym = ELFNN_R_SYM (rel->r_info);
2129       *slot = ret;
2130     }
2131   return ret;
2132 }
2133
2134 /* Used to sort elfNN_ia64_dyn_sym_info array.  */
2135
2136 static int
2137 addend_compare (const void *xp, const void *yp)
2138 {
2139   const struct elfNN_ia64_dyn_sym_info *x
2140     = (const struct elfNN_ia64_dyn_sym_info *) xp;
2141   const struct elfNN_ia64_dyn_sym_info *y
2142     = (const struct elfNN_ia64_dyn_sym_info *) yp;
2143
2144   return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
2145 }
2146
2147 /* Sort elfNN_ia64_dyn_sym_info array and remove duplicates.  */
2148
2149 static unsigned int
2150 sort_dyn_sym_info (struct elfNN_ia64_dyn_sym_info *info,
2151                    unsigned int count)
2152 {
2153   bfd_vma curr, prev, got_offset;
2154   unsigned int i, kept, dupes, diff, dest, src, len;
2155
2156   qsort (info, count, sizeof (*info), addend_compare);
2157
2158   /* Find the first duplicate.  */
2159   prev = info [0].addend;
2160   got_offset = info [0].got_offset;
2161   for (i = 1; i < count; i++)
2162     {
2163       curr = info [i].addend;
2164       if (curr == prev)
2165         {
2166           /* For duplicates, make sure that GOT_OFFSET is valid.  */
2167           if (got_offset == (bfd_vma) -1)
2168             got_offset = info [i].got_offset;
2169           break;
2170         }
2171       got_offset = info [i].got_offset;
2172       prev = curr;
2173     }
2174
2175   /* We may move a block of elements to here.  */
2176   dest = i++;
2177
2178   /* Remove duplicates.  */
2179   if (i < count)
2180     {
2181       while (i < count)
2182         {
2183           /* For duplicates, make sure that the kept one has a valid
2184              got_offset.  */
2185           kept = dest - 1;
2186           if (got_offset != (bfd_vma) -1)
2187             info [kept].got_offset = got_offset;
2188
2189           curr = info [i].addend;
2190           got_offset = info [i].got_offset;
2191
2192           /* Move a block of elements whose first one is different from
2193              the previous.  */
2194           if (curr == prev)
2195             {
2196               for (src = i + 1; src < count; src++)
2197                 {
2198                   if (info [src].addend != curr)
2199                     break;
2200                   /* For duplicates, make sure that GOT_OFFSET is
2201                      valid.  */
2202                   if (got_offset == (bfd_vma) -1)
2203                     got_offset = info [src].got_offset;
2204                 }
2205
2206               /* Make sure that the kept one has a valid got_offset.  */
2207               if (got_offset != (bfd_vma) -1)
2208                 info [kept].got_offset = got_offset;
2209             }
2210           else
2211             src = i;
2212
2213           if (src >= count)
2214             break;
2215
2216           /* Find the next duplicate.  SRC will be kept.  */
2217           prev = info [src].addend;
2218           got_offset = info [src].got_offset;
2219           for (dupes = src + 1; dupes < count; dupes ++)
2220             {
2221               curr = info [dupes].addend;
2222               if (curr == prev)
2223                 {
2224                   /* Make sure that got_offset is valid.  */
2225                   if (got_offset == (bfd_vma) -1)
2226                     got_offset = info [dupes].got_offset;
2227
2228                   /* For duplicates, make sure that the kept one has
2229                      a valid got_offset.  */
2230                   if (got_offset != (bfd_vma) -1)
2231                     info [dupes - 1].got_offset = got_offset;
2232                   break;
2233                 }
2234               got_offset = info [dupes].got_offset;
2235               prev = curr;
2236             }
2237
2238           /* How much to move.  */
2239           len = dupes - src;
2240           i = dupes + 1;
2241
2242           if (len == 1 && dupes < count)
2243             {
2244               /* If we only move 1 element, we combine it with the next
2245                  one.  There must be at least a duplicate.  Find the
2246                  next different one.  */
2247               for (diff = dupes + 1, src++; diff < count; diff++, src++)
2248                 {
2249                   if (info [diff].addend != curr)
2250                     break;
2251                   /* Make sure that got_offset is valid.  */
2252                   if (got_offset == (bfd_vma) -1)
2253                     got_offset = info [diff].got_offset;
2254                 }
2255
2256               /* Makre sure that the last duplicated one has an valid
2257                  offset.  */
2258               BFD_ASSERT (curr == prev);
2259               if (got_offset != (bfd_vma) -1)
2260                 info [diff - 1].got_offset = got_offset;
2261
2262               if (diff < count)
2263                 {
2264                   /* Find the next duplicate.  Track the current valid
2265                      offset.  */
2266                   prev = info [diff].addend;
2267                   got_offset = info [diff].got_offset;
2268                   for (dupes = diff + 1; dupes < count; dupes ++)
2269                     {
2270                       curr = info [dupes].addend;
2271                       if (curr == prev)
2272                         {
2273                           /* For duplicates, make sure that GOT_OFFSET
2274                              is valid.  */
2275                           if (got_offset == (bfd_vma) -1)
2276                             got_offset = info [dupes].got_offset;
2277                           break;
2278                         }
2279                       got_offset = info [dupes].got_offset;
2280                       prev = curr;
2281                       diff++;
2282                     }
2283
2284                   len = diff - src + 1;
2285                   i = diff + 1;
2286                 }
2287             }
2288
2289           memmove (&info [dest], &info [src], len * sizeof (*info));
2290
2291           dest += len;
2292         }
2293
2294       count = dest;
2295     }
2296   else
2297     {
2298       /* When we get here, either there is no duplicate at all or
2299          the only duplicate is the last element.  */
2300       if (dest < count)
2301         {
2302           /* If the last element is a duplicate, make sure that the
2303              kept one has a valid got_offset.  We also update count.  */
2304           if (got_offset != (bfd_vma) -1)
2305             info [dest - 1].got_offset = got_offset;
2306           count = dest;
2307         }
2308     }
2309
2310   return count;
2311 }
2312
2313 /* Find and/or create a descriptor for dynamic symbol info.  This will
2314    vary based on global or local symbol, and the addend to the reloc.
2315
2316    We don't sort when inserting.  Also, we sort and eliminate
2317    duplicates if there is an unsorted section.  Typically, this will
2318    only happen once, because we do all insertions before lookups.  We
2319    then use bsearch to do a lookup.  This also allows lookups to be
2320    fast.  So we have fast insertion (O(log N) due to duplicate check),
2321    fast lookup (O(log N)) and one sort (O(N log N) expected time).
2322    Previously, all lookups were O(N) because of the use of the linked
2323    list and also all insertions were O(N) because of the check for
2324    duplicates.  There are some complications here because the array
2325    size grows occasionally, which may add an O(N) factor, but this
2326    should be rare.  Also,  we free the excess array allocation, which
2327    requires a copy which is O(N), but this only happens once.  */
2328
2329 static struct elfNN_ia64_dyn_sym_info *
2330 get_dyn_sym_info (struct elfNN_ia64_link_hash_table *ia64_info,
2331                   struct elf_link_hash_entry *h, bfd *abfd,
2332                   const Elf_Internal_Rela *rel, bfd_boolean create)
2333 {
2334   struct elfNN_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
2335   unsigned int *count_p, *sorted_count_p, *size_p;
2336   unsigned int count, sorted_count, size;
2337   bfd_vma addend = rel ? rel->r_addend : 0;
2338   bfd_size_type amt;
2339
2340   if (h)
2341     {
2342       struct elfNN_ia64_link_hash_entry *global_h;
2343
2344       global_h = (struct elfNN_ia64_link_hash_entry *) h;
2345       info_p = &global_h->info;
2346       count_p = &global_h->count;
2347       sorted_count_p = &global_h->sorted_count;
2348       size_p = &global_h->size;
2349     }
2350   else
2351     {
2352       struct elfNN_ia64_local_hash_entry *loc_h;
2353
2354       loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
2355       if (!loc_h)
2356         {
2357           BFD_ASSERT (!create);
2358           return NULL;
2359         }
2360
2361       info_p = &loc_h->info;
2362       count_p = &loc_h->count;
2363       sorted_count_p = &loc_h->sorted_count;
2364       size_p = &loc_h->size;
2365     }
2366
2367   count = *count_p;
2368   sorted_count = *sorted_count_p;
2369   size = *size_p;
2370   info = *info_p;
2371   if (create)
2372     {
2373       /* When we create the array, we don't check for duplicates,
2374          except in the previously sorted section if one exists, and
2375          against the last inserted entry.  This allows insertions to
2376          be fast.  */
2377       if (info)
2378         {
2379           if (sorted_count)
2380             {
2381               /* Try bsearch first on the sorted section.  */
2382               key.addend = addend;
2383               dyn_i = bsearch (&key, info, sorted_count,
2384                                sizeof (*info), addend_compare);
2385
2386               if (dyn_i)
2387                 {
2388                   return dyn_i;
2389                 }
2390             }
2391
2392           /* Do a quick check for the last inserted entry.  */
2393           dyn_i = info + count - 1;
2394           if (dyn_i->addend == addend)
2395             {
2396               return dyn_i;
2397             }
2398         }
2399
2400       if (size == 0)
2401         {
2402           /* It is the very first element. We create the array of size
2403              1.  */
2404           size = 1;
2405           amt = size * sizeof (*info);
2406           info = bfd_malloc (amt);
2407         }
2408       else if (size <= count)
2409         {
2410           /* We double the array size every time when we reach the
2411              size limit.  */
2412           size += size;
2413           amt = size * sizeof (*info);
2414           info = bfd_realloc (info, amt);
2415         }
2416       else
2417         goto has_space;
2418
2419       if (info == NULL)
2420         return NULL;
2421       *size_p = size;
2422       *info_p = info;
2423
2424 has_space:
2425       /* Append the new one to the array.  */
2426       dyn_i = info + count;
2427       memset (dyn_i, 0, sizeof (*dyn_i));
2428       dyn_i->got_offset = (bfd_vma) -1;
2429       dyn_i->addend = addend;
2430
2431       /* We increment count only since the new ones are unsorted and
2432          may have duplicate.  */
2433       (*count_p)++;
2434     }
2435   else
2436     {
2437       /* It is a lookup without insertion.  Sort array if part of the
2438          array isn't sorted.  */
2439       if (count != sorted_count)
2440         {
2441           count = sort_dyn_sym_info (info, count);
2442           *count_p = count;
2443           *sorted_count_p = count;
2444         }
2445
2446       /* Free unused memory.  */
2447       if (size != count)
2448         {
2449           amt = count * sizeof (*info);
2450           info = bfd_malloc (amt);
2451           if (info != NULL)
2452             {
2453               memcpy (info, *info_p, amt);
2454               free (*info_p);
2455               *size_p = count;
2456               *info_p = info;
2457             }
2458         }
2459
2460       key.addend = addend;
2461       dyn_i = bsearch (&key, info, count,
2462                        sizeof (*info), addend_compare);
2463     }
2464
2465   return dyn_i;
2466 }
2467
2468 static asection *
2469 get_got (bfd *abfd, struct bfd_link_info *info,
2470          struct elfNN_ia64_link_hash_table *ia64_info)
2471 {
2472   asection *got;
2473   bfd *dynobj;
2474
2475   got = ia64_info->root.sgot;
2476   if (!got)
2477     {
2478       flagword flags;
2479
2480       dynobj = ia64_info->root.dynobj;
2481       if (!dynobj)
2482         ia64_info->root.dynobj = dynobj = abfd;
2483       if (!_bfd_elf_create_got_section (dynobj, info))
2484         return 0;
2485
2486       got = ia64_info->root.sgot;
2487
2488       /* The .got section is always aligned at 8 bytes.  */
2489       if (!bfd_set_section_alignment (abfd, got, 3))
2490         return 0;
2491
2492       flags = bfd_get_section_flags (abfd, got);
2493       bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags);
2494     }
2495
2496   return got;
2497 }
2498
2499 /* Create function descriptor section (.opd).  This section is called .opd
2500    because it contains "official procedure descriptors".  The "official"
2501    refers to the fact that these descriptors are used when taking the address
2502    of a procedure, thus ensuring a unique address for each procedure.  */
2503
2504 static asection *
2505 get_fptr (bfd *abfd, struct bfd_link_info *info,
2506           struct elfNN_ia64_link_hash_table *ia64_info)
2507 {
2508   asection *fptr;
2509   bfd *dynobj;
2510
2511   fptr = ia64_info->fptr_sec;
2512   if (!fptr)
2513     {
2514       dynobj = ia64_info->root.dynobj;
2515       if (!dynobj)
2516         ia64_info->root.dynobj = dynobj = abfd;
2517
2518       fptr = bfd_make_section_with_flags (dynobj, ".opd",
2519                                           (SEC_ALLOC
2520                                            | SEC_LOAD
2521                                            | SEC_HAS_CONTENTS
2522                                            | SEC_IN_MEMORY
2523                                            | (info->pie ? 0 : SEC_READONLY)
2524                                            | SEC_LINKER_CREATED));
2525       if (!fptr
2526           || !bfd_set_section_alignment (abfd, fptr, 4))
2527         {
2528           BFD_ASSERT (0);
2529           return NULL;
2530         }
2531
2532       ia64_info->fptr_sec = fptr;
2533
2534       if (info->pie)
2535         {
2536           asection *fptr_rel;
2537           fptr_rel = bfd_make_section_with_flags (dynobj, ".rela.opd",
2538                                                   (SEC_ALLOC | SEC_LOAD
2539                                                    | SEC_HAS_CONTENTS
2540                                                    | SEC_IN_MEMORY
2541                                                    | SEC_LINKER_CREATED
2542                                                    | SEC_READONLY));
2543           if (fptr_rel == NULL
2544               || !bfd_set_section_alignment (abfd, fptr_rel,
2545                                              LOG_SECTION_ALIGN))
2546             {
2547               BFD_ASSERT (0);
2548               return NULL;
2549             }
2550
2551           ia64_info->rel_fptr_sec = fptr_rel;
2552         }
2553     }
2554
2555   return fptr;
2556 }
2557
2558 static asection *
2559 get_pltoff (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED,
2560             struct elfNN_ia64_link_hash_table *ia64_info)
2561 {
2562   asection *pltoff;
2563   bfd *dynobj;
2564
2565   pltoff = ia64_info->pltoff_sec;
2566   if (!pltoff)
2567     {
2568       dynobj = ia64_info->root.dynobj;
2569       if (!dynobj)
2570         ia64_info->root.dynobj = dynobj = abfd;
2571
2572       pltoff = bfd_make_section_with_flags (dynobj,
2573                                             ELF_STRING_ia64_pltoff,
2574                                             (SEC_ALLOC
2575                                              | SEC_LOAD
2576                                              | SEC_HAS_CONTENTS
2577                                              | SEC_IN_MEMORY
2578                                              | SEC_SMALL_DATA
2579                                              | SEC_LINKER_CREATED));
2580       if (!pltoff
2581           || !bfd_set_section_alignment (abfd, pltoff, 4))
2582         {
2583           BFD_ASSERT (0);
2584           return NULL;
2585         }
2586
2587       ia64_info->pltoff_sec = pltoff;
2588     }
2589
2590   return pltoff;
2591 }
2592
2593 static asection *
2594 get_reloc_section (bfd *abfd,
2595                    struct elfNN_ia64_link_hash_table *ia64_info,
2596                    asection *sec, bfd_boolean create)
2597 {
2598   const char *srel_name;
2599   asection *srel;
2600   bfd *dynobj;
2601
2602   srel_name = (bfd_elf_string_from_elf_section
2603                (abfd, elf_elfheader(abfd)->e_shstrndx,
2604                 elf_section_data(sec)->rel_hdr.sh_name));
2605   if (srel_name == NULL)
2606     return NULL;
2607
2608   BFD_ASSERT ((CONST_STRNEQ (srel_name, ".rela")
2609                && strcmp (bfd_get_section_name (abfd, sec),
2610                           srel_name+5) == 0)
2611               || (CONST_STRNEQ (srel_name, ".rel")
2612                   && strcmp (bfd_get_section_name (abfd, sec),
2613                              srel_name+4) == 0));
2614
2615   dynobj = ia64_info->root.dynobj;
2616   if (!dynobj)
2617     ia64_info->root.dynobj = dynobj = abfd;
2618
2619   srel = bfd_get_section_by_name (dynobj, srel_name);
2620   if (srel == NULL && create)
2621     {
2622       srel = bfd_make_section_with_flags (dynobj, srel_name,
2623                                           (SEC_ALLOC | SEC_LOAD
2624                                            | SEC_HAS_CONTENTS
2625                                            | SEC_IN_MEMORY
2626                                            | SEC_LINKER_CREATED
2627                                            | SEC_READONLY));
2628       if (srel == NULL
2629           || !bfd_set_section_alignment (dynobj, srel,
2630                                          LOG_SECTION_ALIGN))
2631         return NULL;
2632     }
2633
2634   return srel;
2635 }
2636
2637 static bfd_boolean
2638 count_dyn_reloc (bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
2639                  asection *srel, int type, bfd_boolean reltext)
2640 {
2641   struct elfNN_ia64_dyn_reloc_entry *rent;
2642
2643   for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2644     if (rent->srel == srel && rent->type == type)
2645       break;
2646
2647   if (!rent)
2648     {
2649       rent = ((struct elfNN_ia64_dyn_reloc_entry *)
2650               bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
2651       if (!rent)
2652         return FALSE;
2653
2654       rent->next = dyn_i->reloc_entries;
2655       rent->srel = srel;
2656       rent->type = type;
2657       rent->count = 0;
2658       dyn_i->reloc_entries = rent;
2659     }
2660   rent->reltext = reltext;
2661   rent->count++;
2662
2663   return TRUE;
2664 }
2665
2666 static bfd_boolean
2667 elfNN_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2668                          asection *sec,
2669                          const Elf_Internal_Rela *relocs)
2670 {
2671   struct elfNN_ia64_link_hash_table *ia64_info;
2672   const Elf_Internal_Rela *relend;
2673   Elf_Internal_Shdr *symtab_hdr;
2674   const Elf_Internal_Rela *rel;
2675   asection *got, *fptr, *srel, *pltoff;
2676   enum {
2677     NEED_GOT = 1,
2678     NEED_GOTX = 2,
2679     NEED_FPTR = 4,
2680     NEED_PLTOFF = 8,
2681     NEED_MIN_PLT = 16,
2682     NEED_FULL_PLT = 32,
2683     NEED_DYNREL = 64,
2684     NEED_LTOFF_FPTR = 128,
2685     NEED_TPREL = 256,
2686     NEED_DTPMOD = 512,
2687     NEED_DTPREL = 1024
2688   };
2689   int need_entry;
2690   struct elf_link_hash_entry *h;
2691   unsigned long r_symndx;
2692   bfd_boolean maybe_dynamic;
2693
2694   if (info->relocatable)
2695     return TRUE;
2696
2697   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2698   ia64_info = elfNN_ia64_hash_table (info);
2699   if (ia64_info == NULL)
2700     return FALSE;
2701
2702   got = fptr = srel = pltoff = NULL;
2703
2704   relend = relocs + sec->reloc_count;
2705
2706   /* We scan relocations first to create dynamic relocation arrays.  We
2707      modified get_dyn_sym_info to allow fast insertion and support fast
2708      lookup in the next loop.  */
2709   for (rel = relocs; rel < relend; ++rel)
2710     {
2711       r_symndx = ELFNN_R_SYM (rel->r_info);
2712       if (r_symndx >= symtab_hdr->sh_info)
2713         {
2714           long indx = r_symndx - symtab_hdr->sh_info;
2715           h = elf_sym_hashes (abfd)[indx];
2716           while (h->root.type == bfd_link_hash_indirect
2717                  || h->root.type == bfd_link_hash_warning)
2718             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2719         }
2720       else
2721         h = NULL;
2722
2723       /* We can only get preliminary data on whether a symbol is
2724          locally or externally defined, as not all of the input files
2725          have yet been processed.  Do something with what we know, as
2726          this may help reduce memory usage and processing time later.  */
2727       maybe_dynamic = (h && ((!info->executable
2728                               && (!SYMBOLIC_BIND (info, h)
2729                                   || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2730                              || !h->def_regular
2731                              || h->root.type == bfd_link_hash_defweak));
2732
2733       need_entry = 0;
2734       switch (ELFNN_R_TYPE (rel->r_info))
2735         {
2736         case R_IA64_TPREL64MSB:
2737         case R_IA64_TPREL64LSB:
2738           if (info->shared || maybe_dynamic)
2739             need_entry = NEED_DYNREL;
2740           break;
2741
2742         case R_IA64_LTOFF_TPREL22:
2743           need_entry = NEED_TPREL;
2744           if (info->shared)
2745             info->flags |= DF_STATIC_TLS;
2746           break;
2747
2748         case R_IA64_DTPREL32MSB:
2749         case R_IA64_DTPREL32LSB:
2750         case R_IA64_DTPREL64MSB:
2751         case R_IA64_DTPREL64LSB:
2752           if (info->shared || maybe_dynamic)
2753             need_entry = NEED_DYNREL;
2754           break;
2755
2756         case R_IA64_LTOFF_DTPREL22:
2757           need_entry = NEED_DTPREL;
2758           break;
2759
2760         case R_IA64_DTPMOD64MSB:
2761         case R_IA64_DTPMOD64LSB:
2762           if (info->shared || maybe_dynamic)
2763             need_entry = NEED_DYNREL;
2764           break;
2765
2766         case R_IA64_LTOFF_DTPMOD22:
2767           need_entry = NEED_DTPMOD;
2768           break;
2769
2770         case R_IA64_LTOFF_FPTR22:
2771         case R_IA64_LTOFF_FPTR64I:
2772         case R_IA64_LTOFF_FPTR32MSB:
2773         case R_IA64_LTOFF_FPTR32LSB:
2774         case R_IA64_LTOFF_FPTR64MSB:
2775         case R_IA64_LTOFF_FPTR64LSB:
2776           need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2777           break;
2778
2779         case R_IA64_FPTR64I:
2780         case R_IA64_FPTR32MSB:
2781         case R_IA64_FPTR32LSB:
2782         case R_IA64_FPTR64MSB:
2783         case R_IA64_FPTR64LSB:
2784           if (info->shared || h)
2785             need_entry = NEED_FPTR | NEED_DYNREL;
2786           else
2787             need_entry = NEED_FPTR;
2788           break;
2789
2790         case R_IA64_LTOFF22:
2791         case R_IA64_LTOFF64I:
2792           need_entry = NEED_GOT;
2793           break;
2794
2795         case R_IA64_LTOFF22X:
2796           need_entry = NEED_GOTX;
2797           break;
2798
2799         case R_IA64_PLTOFF22:
2800         case R_IA64_PLTOFF64I:
2801         case R_IA64_PLTOFF64MSB:
2802         case R_IA64_PLTOFF64LSB:
2803           need_entry = NEED_PLTOFF;
2804           if (h)
2805             {
2806               if (maybe_dynamic)
2807                 need_entry |= NEED_MIN_PLT;
2808             }
2809           else
2810             {
2811               (*info->callbacks->warning)
2812                 (info, _("@pltoff reloc against local symbol"), 0,
2813                  abfd, 0, (bfd_vma) 0);
2814             }
2815           break;
2816
2817         case R_IA64_PCREL21B:
2818         case R_IA64_PCREL60B:
2819           /* Depending on where this symbol is defined, we may or may not
2820              need a full plt entry.  Only skip if we know we'll not need
2821              the entry -- static or symbolic, and the symbol definition
2822              has already been seen.  */
2823           if (maybe_dynamic && rel->r_addend == 0)
2824             need_entry = NEED_FULL_PLT;
2825           break;
2826
2827         case R_IA64_IMM14:
2828         case R_IA64_IMM22:
2829         case R_IA64_IMM64:
2830         case R_IA64_DIR32MSB:
2831         case R_IA64_DIR32LSB:
2832         case R_IA64_DIR64MSB:
2833         case R_IA64_DIR64LSB:
2834           /* Shared objects will always need at least a REL relocation.  */
2835           if (info->shared || maybe_dynamic)
2836             need_entry = NEED_DYNREL;
2837           break;
2838
2839         case R_IA64_IPLTMSB:
2840         case R_IA64_IPLTLSB:
2841           /* Shared objects will always need at least a REL relocation.  */
2842           if (info->shared || maybe_dynamic)
2843             need_entry = NEED_DYNREL;
2844           break;
2845
2846         case R_IA64_PCREL22:
2847         case R_IA64_PCREL64I:
2848         case R_IA64_PCREL32MSB:
2849         case R_IA64_PCREL32LSB:
2850         case R_IA64_PCREL64MSB:
2851         case R_IA64_PCREL64LSB:
2852           if (maybe_dynamic)
2853             need_entry = NEED_DYNREL;
2854           break;
2855         }
2856
2857       if (!need_entry)
2858         continue;
2859
2860       if ((need_entry & NEED_FPTR) != 0
2861           && rel->r_addend)
2862         {
2863           (*info->callbacks->warning)
2864             (info, _("non-zero addend in @fptr reloc"), 0,
2865              abfd, 0, (bfd_vma) 0);
2866         }
2867
2868       if (get_dyn_sym_info (ia64_info, h, abfd, rel, TRUE) == NULL)
2869         return FALSE;
2870     }
2871
2872   /* Now, we only do lookup without insertion, which is very fast
2873      with the modified get_dyn_sym_info.  */
2874   for (rel = relocs; rel < relend; ++rel)
2875     {
2876       struct elfNN_ia64_dyn_sym_info *dyn_i;
2877       int dynrel_type = R_IA64_NONE;
2878
2879       r_symndx = ELFNN_R_SYM (rel->r_info);
2880       if (r_symndx >= symtab_hdr->sh_info)
2881         {
2882           /* We're dealing with a global symbol -- find its hash entry
2883              and mark it as being referenced.  */
2884           long indx = r_symndx - symtab_hdr->sh_info;
2885           h = elf_sym_hashes (abfd)[indx];
2886           while (h->root.type == bfd_link_hash_indirect
2887                  || h->root.type == bfd_link_hash_warning)
2888             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2889
2890           h->ref_regular = 1;
2891         }
2892       else
2893         h = NULL;
2894
2895       /* We can only get preliminary data on whether a symbol is
2896          locally or externally defined, as not all of the input files
2897          have yet been processed.  Do something with what we know, as
2898          this may help reduce memory usage and processing time later.  */
2899       maybe_dynamic = (h && ((!info->executable
2900                               && (!SYMBOLIC_BIND (info, h)
2901                                   || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2902                              || !h->def_regular
2903                              || h->root.type == bfd_link_hash_defweak));
2904
2905       need_entry = 0;
2906       switch (ELFNN_R_TYPE (rel->r_info))
2907         {
2908         case R_IA64_TPREL64MSB:
2909         case R_IA64_TPREL64LSB:
2910           if (info->shared || maybe_dynamic)
2911             need_entry = NEED_DYNREL;
2912           dynrel_type = R_IA64_TPREL64LSB;
2913           if (info->shared)
2914             info->flags |= DF_STATIC_TLS;
2915           break;
2916
2917         case R_IA64_LTOFF_TPREL22:
2918           need_entry = NEED_TPREL;
2919           if (info->shared)
2920             info->flags |= DF_STATIC_TLS;
2921           break;
2922
2923         case R_IA64_DTPREL32MSB:
2924         case R_IA64_DTPREL32LSB:
2925         case R_IA64_DTPREL64MSB:
2926         case R_IA64_DTPREL64LSB:
2927           if (info->shared || maybe_dynamic)
2928             need_entry = NEED_DYNREL;
2929           dynrel_type = R_IA64_DTPRELNNLSB;
2930           break;
2931
2932         case R_IA64_LTOFF_DTPREL22:
2933           need_entry = NEED_DTPREL;
2934           break;
2935
2936         case R_IA64_DTPMOD64MSB:
2937         case R_IA64_DTPMOD64LSB:
2938           if (info->shared || maybe_dynamic)
2939             need_entry = NEED_DYNREL;
2940           dynrel_type = R_IA64_DTPMOD64LSB;
2941           break;
2942
2943         case R_IA64_LTOFF_DTPMOD22:
2944           need_entry = NEED_DTPMOD;
2945           break;
2946
2947         case R_IA64_LTOFF_FPTR22:
2948         case R_IA64_LTOFF_FPTR64I:
2949         case R_IA64_LTOFF_FPTR32MSB:
2950         case R_IA64_LTOFF_FPTR32LSB:
2951         case R_IA64_LTOFF_FPTR64MSB:
2952         case R_IA64_LTOFF_FPTR64LSB:
2953           need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2954           break;
2955
2956         case R_IA64_FPTR64I:
2957         case R_IA64_FPTR32MSB:
2958         case R_IA64_FPTR32LSB:
2959         case R_IA64_FPTR64MSB:
2960         case R_IA64_FPTR64LSB:
2961           if (info->shared || h)
2962             need_entry = NEED_FPTR | NEED_DYNREL;
2963           else
2964             need_entry = NEED_FPTR;
2965           dynrel_type = R_IA64_FPTRNNLSB;
2966           break;
2967
2968         case R_IA64_LTOFF22:
2969         case R_IA64_LTOFF64I:
2970           need_entry = NEED_GOT;
2971           break;
2972
2973         case R_IA64_LTOFF22X:
2974           need_entry = NEED_GOTX;
2975           break;
2976
2977         case R_IA64_PLTOFF22:
2978         case R_IA64_PLTOFF64I:
2979         case R_IA64_PLTOFF64MSB:
2980         case R_IA64_PLTOFF64LSB:
2981           need_entry = NEED_PLTOFF;
2982           if (h)
2983             {
2984               if (maybe_dynamic)
2985                 need_entry |= NEED_MIN_PLT;
2986             }
2987           break;
2988
2989         case R_IA64_PCREL21B:
2990         case R_IA64_PCREL60B:
2991           /* Depending on where this symbol is defined, we may or may not
2992              need a full plt entry.  Only skip if we know we'll not need
2993              the entry -- static or symbolic, and the symbol definition
2994              has already been seen.  */
2995           if (maybe_dynamic && rel->r_addend == 0)
2996             need_entry = NEED_FULL_PLT;
2997           break;
2998
2999         case R_IA64_IMM14:
3000         case R_IA64_IMM22:
3001         case R_IA64_IMM64:
3002         case R_IA64_DIR32MSB:
3003         case R_IA64_DIR32LSB:
3004         case R_IA64_DIR64MSB:
3005         case R_IA64_DIR64LSB:
3006           /* Shared objects will always need at least a REL relocation.  */
3007           if (info->shared || maybe_dynamic)
3008             need_entry = NEED_DYNREL;
3009           dynrel_type = R_IA64_DIRNNLSB;
3010           break;
3011
3012         case R_IA64_IPLTMSB:
3013         case R_IA64_IPLTLSB:
3014           /* Shared objects will always need at least a REL relocation.  */
3015           if (info->shared || maybe_dynamic)
3016             need_entry = NEED_DYNREL;
3017           dynrel_type = R_IA64_IPLTLSB;
3018           break;
3019
3020         case R_IA64_PCREL22:
3021         case R_IA64_PCREL64I:
3022         case R_IA64_PCREL32MSB:
3023         case R_IA64_PCREL32LSB:
3024         case R_IA64_PCREL64MSB:
3025         case R_IA64_PCREL64LSB:
3026           if (maybe_dynamic)
3027             need_entry = NEED_DYNREL;
3028           dynrel_type = R_IA64_PCRELNNLSB;
3029           break;
3030         }
3031
3032       if (!need_entry)
3033         continue;
3034
3035       dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, FALSE);
3036
3037       /* Record whether or not this is a local symbol.  */
3038       dyn_i->h = h;
3039
3040       /* Create what's needed.  */
3041       if (need_entry & (NEED_GOT | NEED_GOTX | NEED_TPREL
3042                         | NEED_DTPMOD | NEED_DTPREL))
3043         {
3044           if (!got)
3045             {
3046               got = get_got (abfd, info, ia64_info);
3047               if (!got)
3048                 return FALSE;
3049             }
3050           if (need_entry & NEED_GOT)
3051             dyn_i->want_got = 1;
3052           if (need_entry & NEED_GOTX)
3053             dyn_i->want_gotx = 1;
3054           if (need_entry & NEED_TPREL)
3055             dyn_i->want_tprel = 1;
3056           if (need_entry & NEED_DTPMOD)
3057             dyn_i->want_dtpmod = 1;
3058           if (need_entry & NEED_DTPREL)
3059             dyn_i->want_dtprel = 1;
3060         }
3061       if (need_entry & NEED_FPTR)
3062         {
3063           if (!fptr)
3064             {
3065               fptr = get_fptr (abfd, info, ia64_info);
3066               if (!fptr)
3067                 return FALSE;
3068             }
3069
3070           /* FPTRs for shared libraries are allocated by the dynamic
3071              linker.  Make sure this local symbol will appear in the
3072              dynamic symbol table.  */
3073           if (!h && info->shared)
3074             {
3075               if (! (bfd_elf_link_record_local_dynamic_symbol
3076                      (info, abfd, (long) r_symndx)))
3077                 return FALSE;
3078             }
3079
3080           dyn_i->want_fptr = 1;
3081         }
3082       if (need_entry & NEED_LTOFF_FPTR)
3083         dyn_i->want_ltoff_fptr = 1;
3084       if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
3085         {
3086           if (!ia64_info->root.dynobj)
3087             ia64_info->root.dynobj = abfd;
3088           h->needs_plt = 1;
3089           dyn_i->want_plt = 1;
3090         }
3091       if (need_entry & NEED_FULL_PLT)
3092         dyn_i->want_plt2 = 1;
3093       if (need_entry & NEED_PLTOFF)
3094         {
3095           /* This is needed here, in case @pltoff is used in a non-shared
3096              link.  */
3097           if (!pltoff)
3098             {
3099               pltoff = get_pltoff (abfd, info, ia64_info);
3100               if (!pltoff)
3101                 return FALSE;
3102             }
3103
3104           dyn_i->want_pltoff = 1;
3105         }
3106       if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
3107         {
3108           if (!srel)
3109             {
3110               srel = get_reloc_section (abfd, ia64_info, sec, TRUE);
3111               if (!srel)
3112                 return FALSE;
3113             }
3114           if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type,
3115                                 (sec->flags & SEC_READONLY) != 0))
3116             return FALSE;
3117         }
3118     }
3119
3120   return TRUE;
3121 }
3122
3123 /* For cleanliness, and potentially faster dynamic loading, allocate
3124    external GOT entries first.  */
3125
3126 static bfd_boolean
3127 allocate_global_data_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
3128                           void * data)
3129 {
3130   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3131
3132   if ((dyn_i->want_got || dyn_i->want_gotx)
3133       && ! dyn_i->want_fptr
3134       && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
3135      {
3136        dyn_i->got_offset = x->ofs;
3137        x->ofs += 8;
3138      }
3139   if (dyn_i->want_tprel)
3140     {
3141       dyn_i->tprel_offset = x->ofs;
3142       x->ofs += 8;
3143     }
3144   if (dyn_i->want_dtpmod)
3145     {
3146       if (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
3147         {
3148           dyn_i->dtpmod_offset = x->ofs;
3149           x->ofs += 8;
3150         }
3151       else
3152         {
3153           struct elfNN_ia64_link_hash_table *ia64_info;
3154
3155           ia64_info = elfNN_ia64_hash_table (x->info);
3156           if (ia64_info == NULL)
3157             return FALSE;
3158
3159           if (ia64_info->self_dtpmod_offset == (bfd_vma) -1)
3160             {
3161               ia64_info->self_dtpmod_offset = x->ofs;
3162               x->ofs += 8;
3163             }
3164           dyn_i->dtpmod_offset = ia64_info->self_dtpmod_offset;
3165         }
3166     }
3167   if (dyn_i->want_dtprel)
3168     {
3169       dyn_i->dtprel_offset = x->ofs;
3170       x->ofs += 8;
3171     }
3172   return TRUE;
3173 }
3174
3175 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs.  */
3176
3177 static bfd_boolean
3178 allocate_global_fptr_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
3179                           void * data)
3180 {
3181   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3182
3183   if (dyn_i->want_got
3184       && dyn_i->want_fptr
3185       && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, R_IA64_FPTRNNLSB))
3186     {
3187       dyn_i->got_offset = x->ofs;
3188       x->ofs += 8;
3189     }
3190   return TRUE;
3191 }
3192
3193 /* Lastly, allocate all the GOT entries for local data.  */
3194
3195 static bfd_boolean
3196 allocate_local_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
3197                     PTR data)
3198 {
3199   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3200
3201   if ((dyn_i->want_got || dyn_i->want_gotx)
3202       && !elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
3203     {
3204       dyn_i->got_offset = x->ofs;
3205       x->ofs += 8;
3206     }
3207   return TRUE;
3208 }
3209
3210 /* Search for the index of a global symbol in it's defining object file.  */
3211
3212 static long
3213 global_sym_index (struct elf_link_hash_entry *h)
3214 {
3215   struct elf_link_hash_entry **p;
3216   bfd *obj;
3217
3218   BFD_ASSERT (h->root.type == bfd_link_hash_defined
3219               || h->root.type == bfd_link_hash_defweak);
3220
3221   obj = h->root.u.def.section->owner;
3222   for (p = elf_sym_hashes (obj); *p != h; ++p)
3223     continue;
3224
3225   return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
3226 }
3227
3228 /* Allocate function descriptors.  We can do these for every function
3229    in a main executable that is not exported.  */
3230
3231 static bfd_boolean
3232 allocate_fptr (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data)
3233 {
3234   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3235
3236   if (dyn_i->want_fptr)
3237     {
3238       struct elf_link_hash_entry *h = dyn_i->h;
3239
3240       if (h)
3241         while (h->root.type == bfd_link_hash_indirect
3242                || h->root.type == bfd_link_hash_warning)
3243           h = (struct elf_link_hash_entry *) h->root.u.i.link;
3244
3245       if (!x->info->executable
3246           && (!h
3247               || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3248               || (h->root.type != bfd_link_hash_undefweak
3249                   && h->root.type != bfd_link_hash_undefined)))
3250         {
3251           if (h && h->dynindx == -1)
3252             {
3253               BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
3254                           || (h->root.type == bfd_link_hash_defweak));
3255
3256               if (!bfd_elf_link_record_local_dynamic_symbol
3257                     (x->info, h->root.u.def.section->owner,
3258                      global_sym_index (h)))
3259                 return FALSE;
3260             }
3261
3262           dyn_i->want_fptr = 0;
3263         }
3264       else if (h == NULL || h->dynindx == -1)
3265         {
3266           dyn_i->fptr_offset = x->ofs;
3267           x->ofs += 16;
3268         }
3269       else
3270         dyn_i->want_fptr = 0;
3271     }
3272   return TRUE;
3273 }
3274
3275 /* Allocate all the minimal PLT entries.  */
3276
3277 static bfd_boolean
3278 allocate_plt_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3279                       PTR data)
3280 {
3281   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3282
3283   if (dyn_i->want_plt)
3284     {
3285       struct elf_link_hash_entry *h = dyn_i->h;
3286
3287       if (h)
3288         while (h->root.type == bfd_link_hash_indirect
3289                || h->root.type == bfd_link_hash_warning)
3290           h = (struct elf_link_hash_entry *) h->root.u.i.link;
3291
3292       /* ??? Versioned symbols seem to lose NEEDS_PLT.  */
3293       if (elfNN_ia64_dynamic_symbol_p (h, x->info, 0))
3294         {
3295           bfd_size_type offset = x->ofs;
3296           if (offset == 0)
3297             offset = PLT_HEADER_SIZE;
3298           dyn_i->plt_offset = offset;
3299           x->ofs = offset + PLT_MIN_ENTRY_SIZE;
3300
3301           dyn_i->want_pltoff = 1;
3302         }
3303       else
3304         {
3305           dyn_i->want_plt = 0;
3306           dyn_i->want_plt2 = 0;
3307         }
3308     }
3309   return TRUE;
3310 }
3311
3312 /* Allocate all the full PLT entries.  */
3313
3314 static bfd_boolean
3315 allocate_plt2_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3316                        PTR data)
3317 {
3318   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3319
3320   if (dyn_i->want_plt2)
3321     {
3322       struct elf_link_hash_entry *h = dyn_i->h;
3323       bfd_size_type ofs = x->ofs;
3324
3325       dyn_i->plt2_offset = ofs;
3326       x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
3327
3328       while (h->root.type == bfd_link_hash_indirect
3329              || h->root.type == bfd_link_hash_warning)
3330         h = (struct elf_link_hash_entry *) h->root.u.i.link;
3331       dyn_i->h->plt.offset = ofs;
3332     }
3333   return TRUE;
3334 }
3335
3336 /* Allocate all the PLTOFF entries requested by relocations and
3337    plt entries.  We can't share space with allocated FPTR entries,
3338    because the latter are not necessarily addressable by the GP.
3339    ??? Relaxation might be able to determine that they are.  */
3340
3341 static bfd_boolean
3342 allocate_pltoff_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3343                          PTR data)
3344 {
3345   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3346
3347   if (dyn_i->want_pltoff)
3348     {
3349       dyn_i->pltoff_offset = x->ofs;
3350       x->ofs += 16;
3351     }
3352   return TRUE;
3353 }
3354
3355 /* Allocate dynamic relocations for those symbols that turned out
3356    to be dynamic.  */
3357
3358 static bfd_boolean
3359 allocate_dynrel_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3360                          PTR data)
3361 {
3362   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3363   struct elfNN_ia64_link_hash_table *ia64_info;
3364   struct elfNN_ia64_dyn_reloc_entry *rent;
3365   bfd_boolean dynamic_symbol, shared, resolved_zero;
3366
3367   ia64_info = elfNN_ia64_hash_table (x->info);
3368   if (ia64_info == NULL)
3369     return FALSE;
3370
3371   /* Note that this can't be used in relation to FPTR relocs below.  */
3372   dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0);
3373
3374   shared = x->info->shared;
3375   resolved_zero = (dyn_i->h
3376                    && ELF_ST_VISIBILITY (dyn_i->h->other)
3377                    && dyn_i->h->root.type == bfd_link_hash_undefweak);
3378
3379   /* Take care of the GOT and PLT relocations.  */
3380
3381   if ((!resolved_zero
3382        && (dynamic_symbol || shared)
3383        && (dyn_i->want_got || dyn_i->want_gotx))
3384       || (dyn_i->want_ltoff_fptr
3385           && dyn_i->h
3386           && dyn_i->h->dynindx != -1))
3387     {
3388       if (!dyn_i->want_ltoff_fptr
3389           || !x->info->pie
3390           || dyn_i->h == NULL
3391           || dyn_i->h->root.type != bfd_link_hash_undefweak)
3392         ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
3393     }
3394   if ((dynamic_symbol || shared) && dyn_i->want_tprel)
3395     ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
3396   if (dynamic_symbol && dyn_i->want_dtpmod)
3397     ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
3398   if (dynamic_symbol && dyn_i->want_dtprel)
3399     ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
3400
3401   if (x->only_got)
3402     return TRUE;
3403
3404   if (ia64_info->rel_fptr_sec && dyn_i->want_fptr)
3405     {
3406       if (dyn_i->h == NULL || dyn_i->h->root.type != bfd_link_hash_undefweak)
3407         ia64_info->rel_fptr_sec->size += sizeof (ElfNN_External_Rela);
3408     }
3409
3410   if (!resolved_zero && dyn_i->want_pltoff)
3411     {
3412       bfd_size_type t = 0;
3413
3414       /* Dynamic symbols get one IPLT relocation.  Local symbols in
3415          shared libraries get two REL relocations.  Local symbols in
3416          main applications get nothing.  */
3417       if (dynamic_symbol)
3418         t = sizeof (ElfNN_External_Rela);
3419       else if (shared)
3420         t = 2 * sizeof (ElfNN_External_Rela);
3421
3422       ia64_info->rel_pltoff_sec->size += t;
3423     }
3424
3425   /* Take care of the normal data relocations.  */
3426
3427   for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
3428     {
3429       int count = rent->count;
3430
3431       switch (rent->type)
3432         {
3433         case R_IA64_FPTR32LSB:
3434         case R_IA64_FPTR64LSB:
3435           /* Allocate one iff !want_fptr and not PIE, which by this point
3436              will be true only if we're actually allocating one statically
3437              in the main executable.  Position independent executables
3438              need a relative reloc.  */
3439           if (dyn_i->want_fptr && !x->info->pie)
3440             continue;
3441           break;
3442         case R_IA64_PCREL32LSB:
3443         case R_IA64_PCREL64LSB:
3444           if (!dynamic_symbol)
3445             continue;
3446           break;
3447         case R_IA64_DIR32LSB:
3448         case R_IA64_DIR64LSB:
3449           if (!dynamic_symbol && !shared)
3450             continue;
3451           break;
3452         case R_IA64_IPLTLSB:
3453           if (!dynamic_symbol && !shared)
3454             continue;
3455           /* Use two REL relocations for IPLT relocations
3456              against local symbols.  */
3457           if (!dynamic_symbol)
3458             count *= 2;
3459           break;
3460         case R_IA64_DTPREL32LSB:
3461         case R_IA64_TPREL64LSB:
3462         case R_IA64_DTPREL64LSB:
3463         case R_IA64_DTPMOD64LSB:
3464           break;
3465         default:
3466           abort ();
3467         }
3468       if (rent->reltext)
3469         ia64_info->reltext = 1;
3470       rent->srel->size += sizeof (ElfNN_External_Rela) * count;
3471     }
3472
3473   return TRUE;
3474 }
3475
3476 static bfd_boolean
3477 elfNN_ia64_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3478                                   struct elf_link_hash_entry *h)
3479 {
3480   /* ??? Undefined symbols with PLT entries should be re-defined
3481      to be the PLT entry.  */
3482
3483   /* If this is a weak symbol, and there is a real definition, the
3484      processor independent code will have arranged for us to see the
3485      real definition first, and we can just use the same value.  */
3486   if (h->u.weakdef != NULL)
3487     {
3488       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3489                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
3490       h->root.u.def.section = h->u.weakdef->root.u.def.section;
3491       h->root.u.def.value = h->u.weakdef->root.u.def.value;
3492       return TRUE;
3493     }
3494
3495   /* If this is a reference to a symbol defined by a dynamic object which
3496      is not a function, we might allocate the symbol in our .dynbss section
3497      and allocate a COPY dynamic relocation.
3498
3499      But IA-64 code is canonically PIC, so as a rule we can avoid this sort
3500      of hackery.  */
3501
3502   return TRUE;
3503 }
3504
3505 static bfd_boolean
3506 elfNN_ia64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3507                                   struct bfd_link_info *info)
3508 {
3509   struct elfNN_ia64_allocate_data data;
3510   struct elfNN_ia64_link_hash_table *ia64_info;
3511   asection *sec;
3512   bfd *dynobj;
3513   bfd_boolean relplt = FALSE;
3514
3515   dynobj = elf_hash_table(info)->dynobj;
3516   ia64_info = elfNN_ia64_hash_table (info);
3517   if (ia64_info == NULL)
3518     return FALSE;
3519   ia64_info->self_dtpmod_offset = (bfd_vma) -1;
3520   BFD_ASSERT(dynobj != NULL);
3521   data.info = info;
3522
3523   /* Set the contents of the .interp section to the interpreter.  */
3524   if (ia64_info->root.dynamic_sections_created
3525       && info->executable)
3526     {
3527       sec = bfd_get_section_by_name (dynobj, ".interp");
3528       BFD_ASSERT (sec != NULL);
3529       sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
3530       sec->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
3531     }
3532
3533   /* Allocate the GOT entries.  */
3534
3535   if (ia64_info->root.sgot)
3536     {
3537       data.ofs = 0;
3538       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
3539       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
3540       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
3541       ia64_info->root.sgot->size = data.ofs;
3542     }
3543
3544   /* Allocate the FPTR entries.  */
3545
3546   if (ia64_info->fptr_sec)
3547     {
3548       data.ofs = 0;
3549       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
3550       ia64_info->fptr_sec->size = data.ofs;
3551     }
3552
3553   /* Now that we've seen all of the input files, we can decide which
3554      symbols need plt entries.  Allocate the minimal PLT entries first.
3555      We do this even though dynamic_sections_created may be FALSE, because
3556      this has the side-effect of clearing want_plt and want_plt2.  */
3557
3558   data.ofs = 0;
3559   elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
3560
3561   ia64_info->minplt_entries = 0;
3562   if (data.ofs)
3563     {
3564       ia64_info->minplt_entries
3565         = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
3566     }
3567
3568   /* Align the pointer for the plt2 entries.  */
3569   data.ofs = (data.ofs + 31) & (bfd_vma) -32;
3570
3571   elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
3572   if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
3573     {
3574       /* FIXME: we always reserve the memory for dynamic linker even if
3575          there are no PLT entries since dynamic linker may assume the
3576          reserved memory always exists.  */
3577
3578       BFD_ASSERT (ia64_info->root.dynamic_sections_created);
3579
3580       ia64_info->root.splt->size = data.ofs;
3581
3582       /* If we've got a .plt, we need some extra memory for the dynamic
3583          linker.  We stuff these in .got.plt.  */
3584       sec = bfd_get_section_by_name (dynobj, ".got.plt");
3585       sec->size = 8 * PLT_RESERVED_WORDS;
3586     }
3587
3588   /* Allocate the PLTOFF entries.  */
3589
3590   if (ia64_info->pltoff_sec)
3591     {
3592       data.ofs = 0;
3593       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
3594       ia64_info->pltoff_sec->size = data.ofs;
3595     }
3596
3597   if (ia64_info->root.dynamic_sections_created)
3598     {
3599       /* Allocate space for the dynamic relocations that turned out to be
3600          required.  */
3601
3602       if (info->shared && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
3603         ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
3604       data.only_got = FALSE;
3605       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
3606     }
3607
3608   /* We have now determined the sizes of the various dynamic sections.
3609      Allocate memory for them.  */
3610   for (sec = dynobj->sections; sec != NULL; sec = sec->next)
3611     {
3612       bfd_boolean strip;
3613
3614       if (!(sec->flags & SEC_LINKER_CREATED))
3615         continue;
3616
3617       /* If we don't need this section, strip it from the output file.
3618          There were several sections primarily related to dynamic
3619          linking that must be create before the linker maps input
3620          sections to output sections.  The linker does that before
3621          bfd_elf_size_dynamic_sections is called, and it is that
3622          function which decides whether anything needs to go into
3623          these sections.  */
3624
3625       strip = (sec->size == 0);
3626
3627       if (sec == ia64_info->root.sgot)
3628         strip = FALSE;
3629       else if (sec == ia64_info->root.srelgot)
3630         {
3631           if (strip)
3632             ia64_info->root.srelgot = NULL;
3633           else
3634             /* We use the reloc_count field as a counter if we need to
3635                copy relocs into the output file.  */
3636             sec->reloc_count = 0;
3637         }
3638       else if (sec == ia64_info->fptr_sec)
3639         {
3640           if (strip)
3641             ia64_info->fptr_sec = NULL;
3642         }
3643       else if (sec == ia64_info->rel_fptr_sec)
3644         {
3645           if (strip)
3646             ia64_info->rel_fptr_sec = NULL;
3647           else
3648             /* We use the reloc_count field as a counter if we need to
3649                copy relocs into the output file.  */
3650             sec->reloc_count = 0;
3651         }
3652       else if (sec == ia64_info->root.splt)
3653         {
3654           if (strip)
3655             ia64_info->root.splt = NULL;
3656         }
3657       else if (sec == ia64_info->pltoff_sec)
3658         {
3659           if (strip)
3660             ia64_info->pltoff_sec = NULL;
3661         }
3662       else if (sec == ia64_info->rel_pltoff_sec)
3663         {
3664           if (strip)
3665             ia64_info->rel_pltoff_sec = NULL;
3666           else
3667             {
3668               relplt = TRUE;
3669               /* We use the reloc_count field as a counter if we need to
3670                  copy relocs into the output file.  */
3671               sec->reloc_count = 0;
3672             }
3673         }
3674       else
3675         {
3676           const char *name;
3677
3678           /* It's OK to base decisions on the section name, because none
3679              of the dynobj section names depend upon the input files.  */
3680           name = bfd_get_section_name (dynobj, sec);
3681
3682           if (strcmp (name, ".got.plt") == 0)
3683             strip = FALSE;
3684           else if (CONST_STRNEQ (name, ".rel"))
3685             {
3686               if (!strip)
3687                 {
3688                   /* We use the reloc_count field as a counter if we need to
3689                      copy relocs into the output file.  */
3690                   sec->reloc_count = 0;
3691                 }
3692             }
3693           else
3694             continue;
3695         }
3696
3697       if (strip)
3698         sec->flags |= SEC_EXCLUDE;
3699       else
3700         {
3701           /* Allocate memory for the section contents.  */
3702           sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
3703           if (sec->contents == NULL && sec->size != 0)
3704             return FALSE;
3705         }
3706     }
3707
3708   if (elf_hash_table (info)->dynamic_sections_created)
3709     {
3710       /* Add some entries to the .dynamic section.  We fill in the values
3711          later (in finish_dynamic_sections) but we must add the entries now
3712          so that we get the correct size for the .dynamic section.  */
3713
3714       if (info->executable)
3715         {
3716           /* The DT_DEBUG entry is filled in by the dynamic linker and used
3717              by the debugger.  */
3718 #define add_dynamic_entry(TAG, VAL) \
3719   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3720
3721           if (!add_dynamic_entry (DT_DEBUG, 0))
3722             return FALSE;
3723         }
3724
3725       if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
3726         return FALSE;
3727       if (!add_dynamic_entry (DT_PLTGOT, 0))
3728         return FALSE;
3729
3730       if (relplt)
3731         {
3732           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3733               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3734               || !add_dynamic_entry (DT_JMPREL, 0))
3735             return FALSE;
3736         }
3737
3738       if (!add_dynamic_entry (DT_RELA, 0)
3739           || !add_dynamic_entry (DT_RELASZ, 0)
3740           || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
3741         return FALSE;
3742
3743       if (ia64_info->reltext)
3744         {
3745           if (!add_dynamic_entry (DT_TEXTREL, 0))
3746             return FALSE;
3747           info->flags |= DF_TEXTREL;
3748         }
3749     }
3750
3751   /* ??? Perhaps force __gp local.  */
3752
3753   return TRUE;
3754 }
3755
3756 static bfd_reloc_status_type
3757 elfNN_ia64_install_value (bfd_byte *hit_addr, bfd_vma v,
3758                           unsigned int r_type)
3759 {
3760   const struct ia64_operand *op;
3761   int bigendian = 0, shift = 0;
3762   bfd_vma t0, t1, dword;
3763   ia64_insn insn;
3764   enum ia64_opnd opnd;
3765   const char *err;
3766   size_t size = 8;
3767 #ifdef BFD_HOST_U_64_BIT
3768   BFD_HOST_U_64_BIT val = (BFD_HOST_U_64_BIT) v;
3769 #else
3770   bfd_vma val = v;
3771 #endif
3772
3773   opnd = IA64_OPND_NIL;
3774   switch (r_type)
3775     {
3776     case R_IA64_NONE:
3777     case R_IA64_LDXMOV:
3778       return bfd_reloc_ok;
3779
3780       /* Instruction relocations.  */
3781
3782     case R_IA64_IMM14:
3783     case R_IA64_TPREL14:
3784     case R_IA64_DTPREL14:
3785       opnd = IA64_OPND_IMM14;
3786       break;
3787
3788     case R_IA64_PCREL21F:       opnd = IA64_OPND_TGT25; break;
3789     case R_IA64_PCREL21M:       opnd = IA64_OPND_TGT25b; break;
3790     case R_IA64_PCREL60B:       opnd = IA64_OPND_TGT64; break;
3791     case R_IA64_PCREL21B:
3792     case R_IA64_PCREL21BI:
3793       opnd = IA64_OPND_TGT25c;
3794       break;
3795
3796     case R_IA64_IMM22:
3797     case R_IA64_GPREL22:
3798     case R_IA64_LTOFF22:
3799     case R_IA64_LTOFF22X:
3800     case R_IA64_PLTOFF22:
3801     case R_IA64_PCREL22:
3802     case R_IA64_LTOFF_FPTR22:
3803     case R_IA64_TPREL22:
3804     case R_IA64_DTPREL22:
3805     case R_IA64_LTOFF_TPREL22:
3806     case R_IA64_LTOFF_DTPMOD22:
3807     case R_IA64_LTOFF_DTPREL22:
3808       opnd = IA64_OPND_IMM22;
3809       break;
3810
3811     case R_IA64_IMM64:
3812     case R_IA64_GPREL64I:
3813     case R_IA64_LTOFF64I:
3814     case R_IA64_PLTOFF64I:
3815     case R_IA64_PCREL64I:
3816     case R_IA64_FPTR64I:
3817     case R_IA64_LTOFF_FPTR64I:
3818     case R_IA64_TPREL64I:
3819     case R_IA64_DTPREL64I:
3820       opnd = IA64_OPND_IMMU64;
3821       break;
3822
3823       /* Data relocations.  */
3824
3825     case R_IA64_DIR32MSB:
3826     case R_IA64_GPREL32MSB:
3827     case R_IA64_FPTR32MSB:
3828     case R_IA64_PCREL32MSB:
3829     case R_IA64_LTOFF_FPTR32MSB:
3830     case R_IA64_SEGREL32MSB:
3831     case R_IA64_SECREL32MSB:
3832     case R_IA64_LTV32MSB:
3833     case R_IA64_DTPREL32MSB:
3834       size = 4; bigendian = 1;
3835       break;
3836
3837     case R_IA64_DIR32LSB:
3838     case R_IA64_GPREL32LSB:
3839     case R_IA64_FPTR32LSB:
3840     case R_IA64_PCREL32LSB:
3841     case R_IA64_LTOFF_FPTR32LSB:
3842     case R_IA64_SEGREL32LSB:
3843     case R_IA64_SECREL32LSB:
3844     case R_IA64_LTV32LSB:
3845     case R_IA64_DTPREL32LSB:
3846       size = 4; bigendian = 0;
3847       break;
3848
3849     case R_IA64_DIR64MSB:
3850     case R_IA64_GPREL64MSB:
3851     case R_IA64_PLTOFF64MSB:
3852     case R_IA64_FPTR64MSB:
3853     case R_IA64_PCREL64MSB:
3854     case R_IA64_LTOFF_FPTR64MSB:
3855     case R_IA64_SEGREL64MSB:
3856     case R_IA64_SECREL64MSB:
3857     case R_IA64_LTV64MSB:
3858     case R_IA64_TPREL64MSB:
3859     case R_IA64_DTPMOD64MSB:
3860     case R_IA64_DTPREL64MSB:
3861       size = 8; bigendian = 1;
3862       break;
3863
3864     case R_IA64_DIR64LSB:
3865     case R_IA64_GPREL64LSB:
3866     case R_IA64_PLTOFF64LSB:
3867     case R_IA64_FPTR64LSB:
3868     case R_IA64_PCREL64LSB:
3869     case R_IA64_LTOFF_FPTR64LSB:
3870     case R_IA64_SEGREL64LSB:
3871     case R_IA64_SECREL64LSB:
3872     case R_IA64_LTV64LSB:
3873     case R_IA64_TPREL64LSB:
3874     case R_IA64_DTPMOD64LSB:
3875     case R_IA64_DTPREL64LSB:
3876       size = 8; bigendian = 0;
3877       break;
3878
3879       /* Unsupported / Dynamic relocations.  */
3880     default:
3881       return bfd_reloc_notsupported;
3882     }
3883
3884   switch (opnd)
3885     {
3886     case IA64_OPND_IMMU64:
3887       hit_addr -= (long) hit_addr & 0x3;
3888       t0 = bfd_getl64 (hit_addr);
3889       t1 = bfd_getl64 (hit_addr + 8);
3890
3891       /* tmpl/s: bits  0.. 5 in t0
3892          slot 0: bits  5..45 in t0
3893          slot 1: bits 46..63 in t0, bits 0..22 in t1
3894          slot 2: bits 23..63 in t1 */
3895
3896       /* First, clear the bits that form the 64 bit constant.  */
3897       t0 &= ~(0x3ffffLL << 46);
3898       t1 &= ~(0x7fffffLL
3899               | ((  (0x07fLL << 13) | (0x1ffLL << 27)
3900                     | (0x01fLL << 22) | (0x001LL << 21)
3901                     | (0x001LL << 36)) << 23));
3902
3903       t0 |= ((val >> 22) & 0x03ffffLL) << 46;           /* 18 lsbs of imm41 */
3904       t1 |= ((val >> 40) & 0x7fffffLL) <<  0;           /* 23 msbs of imm41 */
3905       t1 |= (  (((val >>  0) & 0x07f) << 13)            /* imm7b */
3906                | (((val >>  7) & 0x1ff) << 27)          /* imm9d */
3907                | (((val >> 16) & 0x01f) << 22)          /* imm5c */
3908                | (((val >> 21) & 0x001) << 21)          /* ic */
3909                | (((val >> 63) & 0x001) << 36)) << 23;  /* i */
3910
3911       bfd_putl64 (t0, hit_addr);
3912       bfd_putl64 (t1, hit_addr + 8);
3913       break;
3914
3915     case IA64_OPND_TGT64:
3916       hit_addr -= (long) hit_addr & 0x3;
3917       t0 = bfd_getl64 (hit_addr);
3918       t1 = bfd_getl64 (hit_addr + 8);
3919
3920       /* tmpl/s: bits  0.. 5 in t0
3921          slot 0: bits  5..45 in t0
3922          slot 1: bits 46..63 in t0, bits 0..22 in t1
3923          slot 2: bits 23..63 in t1 */
3924
3925       /* First, clear the bits that form the 64 bit constant.  */
3926       t0 &= ~(0x3ffffLL << 46);
3927       t1 &= ~(0x7fffffLL
3928               | ((1LL << 36 | 0xfffffLL << 13) << 23));
3929
3930       val >>= 4;
3931       t0 |= ((val >> 20) & 0xffffLL) << 2 << 46;        /* 16 lsbs of imm39 */
3932       t1 |= ((val >> 36) & 0x7fffffLL) << 0;            /* 23 msbs of imm39 */
3933       t1 |= ((((val >> 0) & 0xfffffLL) << 13)           /* imm20b */
3934               | (((val >> 59) & 0x1LL) << 36)) << 23;   /* i */
3935
3936       bfd_putl64 (t0, hit_addr);
3937       bfd_putl64 (t1, hit_addr + 8);
3938       break;
3939
3940     default:
3941       switch ((long) hit_addr & 0x3)
3942         {
3943         case 0: shift =  5; break;
3944         case 1: shift = 14; hit_addr += 3; break;
3945         case 2: shift = 23; hit_addr += 6; break;
3946         case 3: return bfd_reloc_notsupported; /* shouldn't happen...  */
3947         }
3948       dword = bfd_getl64 (hit_addr);
3949       insn = (dword >> shift) & 0x1ffffffffffLL;
3950
3951       op = elf64_ia64_operands + opnd;
3952       err = (*op->insert) (op, val, &insn);
3953       if (err)
3954         return bfd_reloc_overflow;
3955
3956       dword &= ~(0x1ffffffffffLL << shift);
3957       dword |= (insn << shift);
3958       bfd_putl64 (dword, hit_addr);
3959       break;
3960
3961     case IA64_OPND_NIL:
3962       /* A data relocation.  */
3963       if (bigendian)
3964         if (size == 4)
3965           bfd_putb32 (val, hit_addr);
3966         else
3967           bfd_putb64 (val, hit_addr);
3968       else
3969         if (size == 4)
3970           bfd_putl32 (val, hit_addr);
3971         else
3972           bfd_putl64 (val, hit_addr);
3973       break;
3974     }
3975
3976   return bfd_reloc_ok;
3977 }
3978
3979 static void
3980 elfNN_ia64_install_dyn_reloc (bfd *abfd, struct bfd_link_info *info,
3981                               asection *sec, asection *srel,
3982                               bfd_vma offset, unsigned int type,
3983                               long dynindx, bfd_vma addend)
3984 {
3985   Elf_Internal_Rela outrel;
3986   bfd_byte *loc;
3987
3988   BFD_ASSERT (dynindx != -1);
3989   outrel.r_info = ELFNN_R_INFO (dynindx, type);
3990   outrel.r_addend = addend;
3991   outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
3992   if (outrel.r_offset >= (bfd_vma) -2)
3993     {
3994       /* Run for the hills.  We shouldn't be outputting a relocation
3995          for this.  So do what everyone else does and output a no-op.  */
3996       outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
3997       outrel.r_addend = 0;
3998       outrel.r_offset = 0;
3999     }
4000   else
4001     outrel.r_offset += sec->output_section->vma + sec->output_offset;
4002
4003   loc = srel->contents;
4004   loc += srel->reloc_count++ * sizeof (ElfNN_External_Rela);
4005   bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
4006   BFD_ASSERT (sizeof (ElfNN_External_Rela) * srel->reloc_count <= srel->size);
4007 }
4008
4009 /* Store an entry for target address TARGET_ADDR in the linkage table
4010    and return the gp-relative address of the linkage table entry.  */
4011
4012 static bfd_vma
4013 set_got_entry (bfd *abfd, struct bfd_link_info *info,
4014                struct elfNN_ia64_dyn_sym_info *dyn_i,
4015                long dynindx, bfd_vma addend, bfd_vma value,
4016                unsigned int dyn_r_type)
4017 {
4018   struct elfNN_ia64_link_hash_table *ia64_info;
4019   asection *got_sec;
4020   bfd_boolean done;
4021   bfd_vma got_offset;
4022
4023   ia64_info = elfNN_ia64_hash_table (info);
4024   if (ia64_info == NULL)
4025     return 0;
4026
4027   got_sec = ia64_info->root.sgot;
4028
4029   switch (dyn_r_type)
4030     {
4031     case R_IA64_TPREL64LSB:
4032       done = dyn_i->tprel_done;
4033       dyn_i->tprel_done = TRUE;
4034       got_offset = dyn_i->tprel_offset;
4035       break;
4036     case R_IA64_DTPMOD64LSB:
4037       if (dyn_i->dtpmod_offset != ia64_info->self_dtpmod_offset)
4038         {
4039           done = dyn_i->dtpmod_done;
4040           dyn_i->dtpmod_done = TRUE;
4041         }
4042       else
4043         {
4044           done = ia64_info->self_dtpmod_done;
4045           ia64_info->self_dtpmod_done = TRUE;
4046           dynindx = 0;
4047         }
4048       got_offset = dyn_i->dtpmod_offset;
4049       break;
4050     case R_IA64_DTPREL32LSB:
4051     case R_IA64_DTPREL64LSB:
4052       done = dyn_i->dtprel_done;
4053       dyn_i->dtprel_done = TRUE;
4054       got_offset = dyn_i->dtprel_offset;
4055       break;
4056     default:
4057       done = dyn_i->got_done;
4058       dyn_i->got_done = TRUE;
4059       got_offset = dyn_i->got_offset;
4060       break;
4061     }
4062
4063   BFD_ASSERT ((got_offset & 7) == 0);
4064
4065   if (! done)
4066     {
4067       /* Store the target address in the linkage table entry.  */
4068       bfd_put_64 (abfd, value, got_sec->contents + got_offset);
4069
4070       /* Install a dynamic relocation if needed.  */
4071       if (((info->shared
4072             && (!dyn_i->h
4073                 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
4074                 || dyn_i->h->root.type != bfd_link_hash_undefweak)
4075             && dyn_r_type != R_IA64_DTPREL32LSB
4076             && dyn_r_type != R_IA64_DTPREL64LSB)
4077            || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info, dyn_r_type)
4078            || (dynindx != -1
4079                && (dyn_r_type == R_IA64_FPTR32LSB
4080                    || dyn_r_type == R_IA64_FPTR64LSB)))
4081           && (!dyn_i->want_ltoff_fptr
4082               || !info->pie
4083               || !dyn_i->h
4084               || dyn_i->h->root.type != bfd_link_hash_undefweak))
4085         {
4086           if (dynindx == -1
4087               && dyn_r_type != R_IA64_TPREL64LSB
4088               && dyn_r_type != R_IA64_DTPMOD64LSB
4089               && dyn_r_type != R_IA64_DTPREL32LSB
4090               && dyn_r_type != R_IA64_DTPREL64LSB)
4091             {
4092               dyn_r_type = R_IA64_RELNNLSB;
4093               dynindx = 0;
4094               addend = value;
4095             }
4096
4097           if (bfd_big_endian (abfd))
4098             {
4099               switch (dyn_r_type)
4100                 {
4101                 case R_IA64_REL32LSB:
4102                   dyn_r_type = R_IA64_REL32MSB;
4103                   break;
4104                 case R_IA64_DIR32LSB:
4105                   dyn_r_type = R_IA64_DIR32MSB;
4106                   break;
4107                 case R_IA64_FPTR32LSB:
4108                   dyn_r_type = R_IA64_FPTR32MSB;
4109                   break;
4110                 case R_IA64_DTPREL32LSB:
4111                   dyn_r_type = R_IA64_DTPREL32MSB;
4112                   break;
4113                 case R_IA64_REL64LSB:
4114                   dyn_r_type = R_IA64_REL64MSB;
4115                   break;
4116                 case R_IA64_DIR64LSB:
4117                   dyn_r_type = R_IA64_DIR64MSB;
4118                   break;
4119                 case R_IA64_FPTR64LSB:
4120                   dyn_r_type = R_IA64_FPTR64MSB;
4121                   break;
4122                 case R_IA64_TPREL64LSB:
4123                   dyn_r_type = R_IA64_TPREL64MSB;
4124                   break;
4125                 case R_IA64_DTPMOD64LSB:
4126                   dyn_r_type = R_IA64_DTPMOD64MSB;
4127                   break;
4128                 case R_IA64_DTPREL64LSB:
4129                   dyn_r_type = R_IA64_DTPREL64MSB;
4130                   break;
4131                 default:
4132                   BFD_ASSERT (FALSE);
4133                   break;
4134                 }
4135             }
4136
4137           elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
4138                                         ia64_info->root.srelgot,
4139                                         got_offset, dyn_r_type,
4140                                         dynindx, addend);
4141         }
4142     }
4143
4144   /* Return the address of the linkage table entry.  */
4145   value = (got_sec->output_section->vma
4146            + got_sec->output_offset
4147            + got_offset);
4148
4149   return value;
4150 }
4151
4152 /* Fill in a function descriptor consisting of the function's code
4153    address and its global pointer.  Return the descriptor's address.  */
4154
4155 static bfd_vma
4156 set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
4157                 struct elfNN_ia64_dyn_sym_info *dyn_i,
4158                 bfd_vma value)
4159 {
4160   struct elfNN_ia64_link_hash_table *ia64_info;
4161   asection *fptr_sec;
4162
4163   ia64_info = elfNN_ia64_hash_table (info);
4164   if (ia64_info == NULL)
4165     return 0;
4166
4167   fptr_sec = ia64_info->fptr_sec;
4168
4169   if (!dyn_i->fptr_done)
4170     {
4171       dyn_i->fptr_done = 1;
4172
4173       /* Fill in the function descriptor.  */
4174       bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
4175       bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
4176                   fptr_sec->contents + dyn_i->fptr_offset + 8);
4177       if (ia64_info->rel_fptr_sec)
4178         {
4179           Elf_Internal_Rela outrel;
4180           bfd_byte *loc;
4181
4182           if (bfd_little_endian (abfd))
4183             outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTLSB);
4184           else
4185             outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTMSB);
4186           outrel.r_addend = value;
4187           outrel.r_offset = (fptr_sec->output_section->vma
4188                              + fptr_sec->output_offset
4189                              + dyn_i->fptr_offset);
4190           loc = ia64_info->rel_fptr_sec->contents;
4191           loc += ia64_info->rel_fptr_sec->reloc_count++
4192                  * sizeof (ElfNN_External_Rela);
4193           bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
4194         }
4195     }
4196
4197   /* Return the descriptor's address.  */
4198   value = (fptr_sec->output_section->vma
4199            + fptr_sec->output_offset
4200            + dyn_i->fptr_offset);
4201
4202   return value;
4203 }
4204
4205 /* Fill in a PLTOFF entry consisting of the function's code address
4206    and its global pointer.  Return the descriptor's address.  */
4207
4208 static bfd_vma
4209 set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
4210                   struct elfNN_ia64_dyn_sym_info *dyn_i,
4211                   bfd_vma value, bfd_boolean is_plt)
4212 {
4213   struct elfNN_ia64_link_hash_table *ia64_info;
4214   asection *pltoff_sec;
4215
4216   ia64_info = elfNN_ia64_hash_table (info);
4217   if (ia64_info == NULL)
4218     return 0;
4219
4220   pltoff_sec = ia64_info->pltoff_sec;
4221
4222   /* Don't do anything if this symbol uses a real PLT entry.  In
4223      that case, we'll fill this in during finish_dynamic_symbol.  */
4224   if ((! dyn_i->want_plt || is_plt)
4225       && !dyn_i->pltoff_done)
4226     {
4227       bfd_vma gp = _bfd_get_gp_value (abfd);
4228
4229       /* Fill in the function descriptor.  */
4230       bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
4231       bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
4232
4233       /* Install dynamic relocations if needed.  */
4234       if (!is_plt
4235           && info->shared
4236           && (!dyn_i->h
4237               || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
4238               || dyn_i->h->root.type != bfd_link_hash_undefweak))
4239         {
4240           unsigned int dyn_r_type;
4241
4242           if (bfd_big_endian (abfd))
4243             dyn_r_type = R_IA64_RELNNMSB;
4244           else
4245             dyn_r_type = R_IA64_RELNNLSB;
4246
4247           elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
4248                                         ia64_info->rel_pltoff_sec,
4249                                         dyn_i->pltoff_offset,
4250                                         dyn_r_type, 0, value);
4251           elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
4252                                         ia64_info->rel_pltoff_sec,
4253                                         dyn_i->pltoff_offset + ARCH_SIZE / 8,
4254                                         dyn_r_type, 0, gp);
4255         }
4256
4257       dyn_i->pltoff_done = 1;
4258     }
4259
4260   /* Return the descriptor's address.  */
4261   value = (pltoff_sec->output_section->vma
4262            + pltoff_sec->output_offset
4263            + dyn_i->pltoff_offset);
4264
4265   return value;
4266 }
4267
4268 /* Return the base VMA address which should be subtracted from real addresses
4269    when resolving @tprel() relocation.
4270    Main program TLS (whose template starts at PT_TLS p_vaddr)
4271    is assigned offset round(2 * size of pointer, PT_TLS p_align).  */
4272
4273 static bfd_vma
4274 elfNN_ia64_tprel_base (struct bfd_link_info *info)
4275 {
4276   asection *tls_sec = elf_hash_table (info)->tls_sec;
4277   return tls_sec->vma - align_power ((bfd_vma) ARCH_SIZE / 4,
4278                                      tls_sec->alignment_power);
4279 }
4280
4281 /* Return the base VMA address which should be subtracted from real addresses
4282    when resolving @dtprel() relocation.
4283    This is PT_TLS segment p_vaddr.  */
4284
4285 static bfd_vma
4286 elfNN_ia64_dtprel_base (struct bfd_link_info *info)
4287 {
4288   return elf_hash_table (info)->tls_sec->vma;
4289 }
4290
4291 /* Called through qsort to sort the .IA_64.unwind section during a
4292    non-relocatable link.  Set elfNN_ia64_unwind_entry_compare_bfd
4293    to the output bfd so we can do proper endianness frobbing.  */
4294
4295 static bfd *elfNN_ia64_unwind_entry_compare_bfd;
4296
4297 static int
4298 elfNN_ia64_unwind_entry_compare (const PTR a, const PTR b)
4299 {
4300   bfd_vma av, bv;
4301
4302   av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
4303   bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
4304
4305   return (av < bv ? -1 : av > bv ? 1 : 0);
4306 }
4307
4308 /* Make sure we've got ourselves a nice fat __gp value.  */
4309 static bfd_boolean
4310 elfNN_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info)
4311 {
4312   bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
4313   bfd_vma min_short_vma = min_vma, max_short_vma = 0;
4314   struct elf_link_hash_entry *gp;
4315   bfd_vma gp_val;
4316   asection *os;
4317   struct elfNN_ia64_link_hash_table *ia64_info;
4318
4319   ia64_info = elfNN_ia64_hash_table (info);
4320   if (ia64_info == NULL)
4321     return FALSE;
4322
4323   /* Find the min and max vma of all sections marked short.  Also collect
4324      min and max vma of any type, for use in selecting a nice gp.  */
4325   for (os = abfd->sections; os ; os = os->next)
4326     {
4327       bfd_vma lo, hi;
4328
4329       if ((os->flags & SEC_ALLOC) == 0)
4330         continue;
4331
4332       lo = os->vma;
4333       hi = os->vma + (os->rawsize ? os->rawsize : os->size);
4334       if (hi < lo)
4335         hi = (bfd_vma) -1;
4336
4337       if (min_vma > lo)
4338         min_vma = lo;
4339       if (max_vma < hi)
4340         max_vma = hi;
4341       if (os->flags & SEC_SMALL_DATA)
4342         {
4343           if (min_short_vma > lo)
4344             min_short_vma = lo;
4345           if (max_short_vma < hi)
4346             max_short_vma = hi;
4347         }
4348     }
4349
4350   if (ia64_info->min_short_sec)
4351     {
4352       if (min_short_vma 
4353           > (ia64_info->min_short_sec->vma
4354              + ia64_info->min_short_offset))
4355         min_short_vma = (ia64_info->min_short_sec->vma
4356                          + ia64_info->min_short_offset);
4357       if (max_short_vma
4358           < (ia64_info->max_short_sec->vma
4359              + ia64_info->max_short_offset))
4360         max_short_vma = (ia64_info->max_short_sec->vma
4361                          + ia64_info->max_short_offset);
4362     }
4363
4364   /* See if the user wants to force a value.  */
4365   gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
4366                              FALSE, FALSE);
4367
4368   if (gp
4369       && (gp->root.type == bfd_link_hash_defined
4370           || gp->root.type == bfd_link_hash_defweak))
4371     {
4372       asection *gp_sec = gp->root.u.def.section;
4373       gp_val = (gp->root.u.def.value
4374                 + gp_sec->output_section->vma
4375                 + gp_sec->output_offset);
4376     }
4377   else
4378     {
4379       /* Pick a sensible value.  */
4380
4381       if (ia64_info->min_short_sec)
4382         {
4383           bfd_vma short_range = max_short_vma - min_short_vma;
4384
4385           /* If min_short_sec is set, pick one in the middle bewteen
4386              min_short_vma and max_short_vma.  */
4387           if (short_range >= 0x400000)
4388             goto overflow;
4389           gp_val = min_short_vma + short_range / 2;
4390         }
4391       else
4392         {
4393           asection *got_sec = ia64_info->root.sgot;
4394
4395           /* Start with just the address of the .got.  */
4396           if (got_sec)
4397             gp_val = got_sec->output_section->vma;
4398           else if (max_short_vma != 0)
4399             gp_val = min_short_vma;
4400           else if (max_vma - min_vma < 0x200000)
4401             gp_val = min_vma;
4402           else
4403             gp_val = max_vma - 0x200000 + 8;
4404         }
4405
4406       /* If it is possible to address the entire image, but we
4407          don't with the choice above, adjust.  */
4408       if (max_vma - min_vma < 0x400000
4409           && (max_vma - gp_val >= 0x200000
4410               || gp_val - min_vma > 0x200000))
4411         gp_val = min_vma + 0x200000;
4412       else if (max_short_vma != 0)
4413         {
4414           /* If we don't cover all the short data, adjust.  */
4415           if (max_short_vma - gp_val >= 0x200000)
4416             gp_val = min_short_vma + 0x200000;
4417
4418           /* If we're addressing stuff past the end, adjust back.  */
4419           if (gp_val > max_vma)
4420             gp_val = max_vma - 0x200000 + 8;
4421         }
4422     }
4423
4424   /* Validate whether all SHF_IA_64_SHORT sections are within
4425      range of the chosen GP.  */
4426
4427   if (max_short_vma != 0)
4428     {
4429       if (max_short_vma - min_short_vma >= 0x400000)
4430         {
4431 overflow:
4432           (*_bfd_error_handler)
4433             (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
4434              bfd_get_filename (abfd),
4435              (unsigned long) (max_short_vma - min_short_vma));
4436           return FALSE;
4437         }
4438       else if ((gp_val > min_short_vma
4439                 && gp_val - min_short_vma > 0x200000)
4440                || (gp_val < max_short_vma
4441                    && max_short_vma - gp_val >= 0x200000))
4442         {
4443           (*_bfd_error_handler)
4444             (_("%s: __gp does not cover short data segment"),
4445              bfd_get_filename (abfd));
4446           return FALSE;
4447         }
4448     }
4449
4450   _bfd_set_gp_value (abfd, gp_val);
4451
4452   return TRUE;
4453 }
4454
4455 static bfd_boolean
4456 elfNN_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
4457 {
4458   struct elfNN_ia64_link_hash_table *ia64_info;
4459   asection *unwind_output_sec;
4460
4461   ia64_info = elfNN_ia64_hash_table (info);
4462   if (ia64_info == NULL)
4463     return FALSE;
4464
4465   /* Make sure we've got ourselves a nice fat __gp value.  */
4466   if (!info->relocatable)
4467     {
4468       bfd_vma gp_val;
4469       struct elf_link_hash_entry *gp;
4470
4471       /* We assume after gp is set, section size will only decrease. We
4472          need to adjust gp for it.  */
4473       _bfd_set_gp_value (abfd, 0);
4474       if (! elfNN_ia64_choose_gp (abfd, info))
4475         return FALSE;
4476       gp_val = _bfd_get_gp_value (abfd);
4477
4478       gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
4479                                  FALSE, FALSE);
4480       if (gp)
4481         {
4482           gp->root.type = bfd_link_hash_defined;
4483           gp->root.u.def.value = gp_val;
4484           gp->root.u.def.section = bfd_abs_section_ptr;
4485         }
4486     }
4487
4488   /* If we're producing a final executable, we need to sort the contents
4489      of the .IA_64.unwind section.  Force this section to be relocated
4490      into memory rather than written immediately to the output file.  */
4491   unwind_output_sec = NULL;
4492   if (!info->relocatable)
4493     {
4494       asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
4495       if (s)
4496         {
4497           unwind_output_sec = s->output_section;
4498           unwind_output_sec->contents
4499             = bfd_malloc (unwind_output_sec->size);
4500           if (unwind_output_sec->contents == NULL)
4501             return FALSE;
4502         }
4503     }
4504
4505   /* Invoke the regular ELF backend linker to do all the work.  */
4506   if (!bfd_elf_final_link (abfd, info))
4507     return FALSE;
4508
4509   if (unwind_output_sec)
4510     {
4511       elfNN_ia64_unwind_entry_compare_bfd = abfd;
4512       qsort (unwind_output_sec->contents,
4513              (size_t) (unwind_output_sec->size / 24),
4514              24,
4515              elfNN_ia64_unwind_entry_compare);
4516
4517       if (! bfd_set_section_contents (abfd, unwind_output_sec,
4518                                       unwind_output_sec->contents, (bfd_vma) 0,
4519                                       unwind_output_sec->size))
4520         return FALSE;
4521     }
4522
4523   return TRUE;
4524 }
4525
4526 static bfd_boolean
4527 elfNN_ia64_relocate_section (bfd *output_bfd,
4528                              struct bfd_link_info *info,
4529                              bfd *input_bfd,
4530                              asection *input_section,
4531                              bfd_byte *contents,
4532                              Elf_Internal_Rela *relocs,
4533                              Elf_Internal_Sym *local_syms,
4534                              asection **local_sections)
4535 {
4536   struct elfNN_ia64_link_hash_table *ia64_info;
4537   Elf_Internal_Shdr *symtab_hdr;
4538   Elf_Internal_Rela *rel;
4539   Elf_Internal_Rela *relend;
4540   asection *srel;
4541   bfd_boolean ret_val = TRUE;   /* for non-fatal errors */
4542   bfd_vma gp_val;
4543
4544   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4545   ia64_info = elfNN_ia64_hash_table (info);
4546   if (ia64_info == NULL)
4547     return FALSE;
4548
4549   /* Infect various flags from the input section to the output section.  */
4550   if (info->relocatable)
4551     {
4552       bfd_vma flags;
4553
4554       flags = elf_section_data(input_section)->this_hdr.sh_flags;
4555       flags &= SHF_IA_64_NORECOV;
4556
4557       elf_section_data(input_section->output_section)
4558         ->this_hdr.sh_flags |= flags;
4559     }
4560
4561   gp_val = _bfd_get_gp_value (output_bfd);
4562   srel = get_reloc_section (input_bfd, ia64_info, input_section, FALSE);
4563
4564   rel = relocs;
4565   relend = relocs + input_section->reloc_count;
4566   for (; rel < relend; ++rel)
4567     {
4568       struct elf_link_hash_entry *h;
4569       struct elfNN_ia64_dyn_sym_info *dyn_i;
4570       bfd_reloc_status_type r;
4571       reloc_howto_type *howto;
4572       unsigned long r_symndx;
4573       Elf_Internal_Sym *sym;
4574       unsigned int r_type;
4575       bfd_vma value;
4576       asection *sym_sec;
4577       bfd_byte *hit_addr;
4578       bfd_boolean dynamic_symbol_p;
4579       bfd_boolean undef_weak_ref;
4580
4581       r_type = ELFNN_R_TYPE (rel->r_info);
4582       if (r_type > R_IA64_MAX_RELOC_CODE)
4583         {
4584           (*_bfd_error_handler)
4585             (_("%B: unknown relocation type %d"),
4586              input_bfd, (int) r_type);
4587           bfd_set_error (bfd_error_bad_value);
4588           ret_val = FALSE;
4589           continue;
4590         }
4591
4592       howto = lookup_howto (r_type);
4593       r_symndx = ELFNN_R_SYM (rel->r_info);
4594       h = NULL;
4595       sym = NULL;
4596       sym_sec = NULL;
4597       undef_weak_ref = FALSE;
4598
4599       if (r_symndx < symtab_hdr->sh_info)
4600         {
4601           /* Reloc against local symbol.  */
4602           asection *msec;
4603           sym = local_syms + r_symndx;
4604           sym_sec = local_sections[r_symndx];
4605           msec = sym_sec;
4606           value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
4607           if (!info->relocatable
4608               && (sym_sec->flags & SEC_MERGE) != 0
4609               && ELF_ST_TYPE (sym->st_info) == STT_SECTION
4610               && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
4611             {
4612               struct elfNN_ia64_local_hash_entry *loc_h;
4613
4614               loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, FALSE);
4615               if (loc_h && ! loc_h->sec_merge_done)
4616                 {
4617                   struct elfNN_ia64_dyn_sym_info *dynent;
4618                   unsigned int count;
4619
4620                   for (count = loc_h->count, dynent = loc_h->info;
4621                        count != 0;
4622                        count--, dynent++)
4623                     {
4624                       msec = sym_sec;
4625                       dynent->addend =
4626                         _bfd_merged_section_offset (output_bfd, &msec,
4627                                                     elf_section_data (msec)->
4628                                                     sec_info,
4629                                                     sym->st_value
4630                                                     + dynent->addend);
4631                       dynent->addend -= sym->st_value;
4632                       dynent->addend += msec->output_section->vma
4633                                         + msec->output_offset
4634                                         - sym_sec->output_section->vma
4635                                         - sym_sec->output_offset;
4636                     }
4637
4638                   /* We may have introduced duplicated entries. We need
4639                      to remove them properly.  */
4640                   count = sort_dyn_sym_info (loc_h->info, loc_h->count);
4641                   if (count != loc_h->count)
4642                     {
4643                       loc_h->count = count;
4644                       loc_h->sorted_count = count;
4645                     }
4646
4647                   loc_h->sec_merge_done = 1;
4648                 }
4649             }
4650         }
4651       else
4652         {
4653           bfd_boolean unresolved_reloc;
4654           bfd_boolean warned;
4655           struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
4656
4657           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4658                                    r_symndx, symtab_hdr, sym_hashes,
4659                                    h, sym_sec, value,
4660                                    unresolved_reloc, warned);
4661
4662           if (h->root.type == bfd_link_hash_undefweak)
4663             undef_weak_ref = TRUE;
4664           else if (warned)
4665             continue;
4666         }
4667
4668       /* For relocs against symbols from removed linkonce sections,
4669          or sections discarded by a linker script, we just want the
4670          section contents zeroed.  Avoid any special processing.  */
4671       if (sym_sec != NULL && elf_discarded_section (sym_sec))
4672         {
4673           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
4674           rel->r_info = 0;
4675           rel->r_addend = 0;
4676           continue;
4677         }
4678
4679       if (info->relocatable)
4680         continue;
4681
4682       hit_addr = contents + rel->r_offset;
4683       value += rel->r_addend;
4684       dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info, r_type);
4685
4686       switch (r_type)
4687         {
4688         case R_IA64_NONE:
4689         case R_IA64_LDXMOV:
4690           continue;
4691
4692         case R_IA64_IMM14:
4693         case R_IA64_IMM22:
4694         case R_IA64_IMM64:
4695         case R_IA64_DIR32MSB:
4696         case R_IA64_DIR32LSB:
4697         case R_IA64_DIR64MSB:
4698         case R_IA64_DIR64LSB:
4699           /* Install a dynamic relocation for this reloc.  */
4700           if ((dynamic_symbol_p || info->shared)
4701               && r_symndx != 0
4702               && (input_section->flags & SEC_ALLOC) != 0)
4703             {
4704               unsigned int dyn_r_type;
4705               long dynindx;
4706               bfd_vma addend;
4707
4708               BFD_ASSERT (srel != NULL);
4709
4710               switch (r_type)
4711                 {
4712                 case R_IA64_IMM14:
4713                 case R_IA64_IMM22:
4714                 case R_IA64_IMM64:
4715                   /* ??? People shouldn't be doing non-pic code in
4716                      shared libraries nor dynamic executables.  */
4717                   (*_bfd_error_handler)
4718                     (_("%B: non-pic code with imm relocation against dynamic symbol `%s'"),
4719                      input_bfd,
4720                      h ? h->root.root.string
4721                        : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4722                                            sym_sec));
4723                   ret_val = FALSE;
4724                   continue;
4725
4726                 default:
4727                   break;
4728                 }
4729
4730               /* If we don't need dynamic symbol lookup, find a
4731                  matching RELATIVE relocation.  */
4732               dyn_r_type = r_type;
4733               if (dynamic_symbol_p)
4734                 {
4735                   dynindx = h->dynindx;
4736                   addend = rel->r_addend;
4737                   value = 0;
4738                 }
4739               else
4740                 {
4741                   switch (r_type)
4742                     {
4743                     case R_IA64_DIR32MSB:
4744                       dyn_r_type = R_IA64_REL32MSB;
4745                       break;
4746                     case R_IA64_DIR32LSB:
4747                       dyn_r_type = R_IA64_REL32LSB;
4748                       break;
4749                     case R_IA64_DIR64MSB:
4750                       dyn_r_type = R_IA64_REL64MSB;
4751                       break;
4752                     case R_IA64_DIR64LSB:
4753                       dyn_r_type = R_IA64_REL64LSB;
4754                       break;
4755
4756                     default:
4757                       break;
4758                     }
4759                   dynindx = 0;
4760                   addend = value;
4761                 }
4762
4763               elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
4764                                             srel, rel->r_offset, dyn_r_type,
4765                                             dynindx, addend);
4766             }
4767           /* Fall through.  */
4768
4769         case R_IA64_LTV32MSB:
4770         case R_IA64_LTV32LSB:
4771         case R_IA64_LTV64MSB:
4772         case R_IA64_LTV64LSB:
4773           r = elfNN_ia64_install_value (hit_addr, value, r_type);
4774           break;
4775
4776         case R_IA64_GPREL22:
4777         case R_IA64_GPREL64I:
4778         case R_IA64_GPREL32MSB:
4779         case R_IA64_GPREL32LSB:
4780         case R_IA64_GPREL64MSB:
4781         case R_IA64_GPREL64LSB:
4782           if (dynamic_symbol_p)
4783             {
4784               (*_bfd_error_handler)
4785                 (_("%B: @gprel relocation against dynamic symbol %s"),
4786                  input_bfd,
4787                  h ? h->root.root.string
4788                    : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4789                                        sym_sec));
4790               ret_val = FALSE;
4791               continue;
4792             }
4793           value -= gp_val;
4794           r = elfNN_ia64_install_value (hit_addr, value, r_type);
4795           break;
4796
4797         case R_IA64_LTOFF22:
4798         case R_IA64_LTOFF22X:
4799         case R_IA64_LTOFF64I:
4800           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
4801           value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
4802                                  rel->r_addend, value, R_IA64_DIRNNLSB);
4803           value -= gp_val;
4804           r = elfNN_ia64_install_value (hit_addr, value, r_type);
4805           break;
4806
4807         case R_IA64_PLTOFF22:
4808         case R_IA64_PLTOFF64I:
4809         case R_IA64_PLTOFF64MSB:
4810         case R_IA64_PLTOFF64LSB:
4811           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
4812           value = set_pltoff_entry (output_bfd, info, dyn_i, value, FALSE);
4813           value -= gp_val;
4814           r = elfNN_ia64_install_value (hit_addr, value, r_type);
4815           break;
4816
4817         case R_IA64_FPTR64I:
4818         case R_IA64_FPTR32MSB:
4819         case R_IA64_FPTR32LSB:
4820         case R_IA64_FPTR64MSB:
4821         case R_IA64_FPTR64LSB:
4822           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
4823           if (dyn_i->want_fptr)
4824             {
4825               if (!undef_weak_ref)
4826                 value = set_fptr_entry (output_bfd, info, dyn_i, value);
4827             }
4828           if (!dyn_i->want_fptr || info->pie)
4829             {
4830               long dynindx;
4831               unsigned int dyn_r_type = r_type;
4832               bfd_vma addend = rel->r_addend;
4833
4834               /* Otherwise, we expect the dynamic linker to create
4835                  the entry.  */
4836
4837               if (dyn_i->want_fptr)
4838                 {
4839                   if (r_type == R_IA64_FPTR64I)
4840                     {
4841                       /* We can't represent this without a dynamic symbol.
4842                          Adjust the relocation to be against an output
4843                          section symbol, which are always present in the
4844                          dynamic symbol table.  */
4845                       /* ??? People shouldn't be doing non-pic code in
4846                          shared libraries.  Hork.  */
4847                       (*_bfd_error_handler)
4848                         (_("%B: linking non-pic code in a position independent executable"),
4849                          input_bfd);
4850                       ret_val = FALSE;
4851                       continue;
4852                     }
4853                   dynindx = 0;
4854                   addend = value;
4855                   dyn_r_type = r_type + R_IA64_RELNNLSB - R_IA64_FPTRNNLSB;
4856                 }
4857               else if (h)
4858                 {
4859                   if (h->dynindx != -1)
4860                     dynindx = h->dynindx;
4861                   else
4862                     dynindx = (_bfd_elf_link_lookup_local_dynindx
4863                                (info, h->root.u.def.section->owner,
4864                                 global_sym_index (h)));
4865                   value = 0;
4866                 }
4867               else
4868                 {
4869                   dynindx = (_bfd_elf_link_lookup_local_dynindx
4870                              (info, input_bfd, (long) r_symndx));
4871                   value = 0;
4872                 }
4873
4874               elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
4875                                             srel, rel->r_offset, dyn_r_type,
4876                                             dynindx, addend);
4877             }
4878
4879           r = elfNN_ia64_install_value (hit_addr, value, r_type);
4880           break;
4881
4882         case R_IA64_LTOFF_FPTR22:
4883         case R_IA64_LTOFF_FPTR64I:
4884         case R_IA64_LTOFF_FPTR32MSB:
4885         case R_IA64_LTOFF_FPTR32LSB:
4886         case R_IA64_LTOFF_FPTR64MSB:
4887         case R_IA64_LTOFF_FPTR64LSB:
4888           {
4889             long dynindx;
4890
4891             dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
4892             if (dyn_i->want_fptr)
4893               {
4894                 BFD_ASSERT (h == NULL || h->dynindx == -1);
4895                 if (!undef_weak_ref)
4896                   value = set_fptr_entry (output_bfd, info, dyn_i, value);
4897                 dynindx = -1;
4898               }
4899             else
4900               {
4901                 /* Otherwise, we expect the dynamic linker to create
4902                    the entry.  */
4903                 if (h)
4904                   {
4905                     if (h->dynindx != -1)
4906                       dynindx = h->dynindx;
4907                     else
4908                       dynindx = (_bfd_elf_link_lookup_local_dynindx
4909                                  (info, h->root.u.def.section->owner,
4910                                   global_sym_index (h)));
4911                   }
4912                 else
4913                   dynindx = (_bfd_elf_link_lookup_local_dynindx
4914                              (info, input_bfd, (long) r_symndx));
4915                 value = 0;
4916               }
4917
4918             value = set_got_entry (output_bfd, info, dyn_i, dynindx,
4919                                    rel->r_addend, value, R_IA64_FPTRNNLSB);
4920             value -= gp_val;
4921             r = elfNN_ia64_install_value (hit_addr, value, r_type);
4922           }
4923           break;
4924
4925         case R_IA64_PCREL32MSB:
4926         case R_IA64_PCREL32LSB:
4927         case R_IA64_PCREL64MSB:
4928         case R_IA64_PCREL64LSB:
4929           /* Install a dynamic relocation for this reloc.  */
4930           if (dynamic_symbol_p && r_symndx != 0)
4931             {
4932               BFD_ASSERT (srel != NULL);
4933
4934               elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
4935                                             srel, rel->r_offset, r_type,
4936                                             h->dynindx, rel->r_addend);
4937             }
4938           goto finish_pcrel;
4939
4940         case R_IA64_PCREL21B:
4941         case R_IA64_PCREL60B:
4942           /* We should have created a PLT entry for any dynamic symbol.  */
4943           dyn_i = NULL;
4944           if (h)
4945             dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
4946
4947           if (dyn_i && dyn_i->want_plt2)
4948             {
4949               /* Should have caught this earlier.  */
4950               BFD_ASSERT (rel->r_addend == 0);
4951
4952               value = (ia64_info->root.splt->output_section->vma
4953                        + ia64_info->root.splt->output_offset
4954                        + dyn_i->plt2_offset);
4955             }
4956           else
4957             {
4958               /* Since there's no PLT entry, Validate that this is
4959                  locally defined.  */
4960               BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
4961
4962               /* If the symbol is undef_weak, we shouldn't be trying
4963                  to call it.  There's every chance that we'd wind up
4964                  with an out-of-range fixup here.  Don't bother setting
4965                  any value at all.  */
4966               if (undef_weak_ref)
4967                 continue;
4968             }
4969           goto finish_pcrel;
4970
4971         case R_IA64_PCREL21BI:
4972         case R_IA64_PCREL21F:
4973         case R_IA64_PCREL21M:
4974         case R_IA64_PCREL22:
4975         case R_IA64_PCREL64I:
4976           /* The PCREL21BI reloc is specifically not intended for use with
4977              dynamic relocs.  PCREL21F and PCREL21M are used for speculation
4978              fixup code, and thus probably ought not be dynamic.  The
4979              PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs.  */
4980           if (dynamic_symbol_p)
4981             {
4982               const char *msg;
4983
4984               if (r_type == R_IA64_PCREL21BI)
4985                 msg = _("%B: @internal branch to dynamic symbol %s");
4986               else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
4987                 msg = _("%B: speculation fixup to dynamic symbol %s");
4988               else
4989                 msg = _("%B: @pcrel relocation against dynamic symbol %s");
4990               (*_bfd_error_handler) (msg, input_bfd,
4991                                      h ? h->root.root.string
4992                                        : bfd_elf_sym_name (input_bfd,
4993                                                            symtab_hdr,
4994                                                            sym,
4995                                                            sym_sec));
4996               ret_val = FALSE;
4997               continue;
4998             }
4999           goto finish_pcrel;
5000
5001         finish_pcrel:
5002           /* Make pc-relative.  */
5003           value -= (input_section->output_section->vma
5004                     + input_section->output_offset
5005                     + rel->r_offset) & ~ (bfd_vma) 0x3;
5006           r = elfNN_ia64_install_value (hit_addr, value, r_type);
5007           break;
5008
5009         case R_IA64_SEGREL32MSB:
5010         case R_IA64_SEGREL32LSB:
5011         case R_IA64_SEGREL64MSB:
5012         case R_IA64_SEGREL64LSB:
5013             {
5014               /* Find the segment that contains the output_section.  */
5015               Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
5016                 (output_bfd, input_section->output_section);
5017
5018               if (p == NULL)
5019                 {
5020                   r = bfd_reloc_notsupported;
5021                 }
5022               else
5023                 {
5024                   /* The VMA of the segment is the vaddr of the associated
5025                      program header.  */
5026                   if (value > p->p_vaddr)
5027                     value -= p->p_vaddr;
5028                   else
5029                     value = 0;
5030                   r = elfNN_ia64_install_value (hit_addr, value, r_type);
5031                 }
5032               break;
5033             }
5034
5035         case R_IA64_SECREL32MSB:
5036         case R_IA64_SECREL32LSB:
5037         case R_IA64_SECREL64MSB:
5038         case R_IA64_SECREL64LSB:
5039           /* Make output-section relative to section where the symbol
5040              is defined. PR 475  */
5041           if (sym_sec)
5042             value -= sym_sec->output_section->vma;
5043           r = elfNN_ia64_install_value (hit_addr, value, r_type);
5044           break;
5045
5046         case R_IA64_IPLTMSB:
5047         case R_IA64_IPLTLSB:
5048           /* Install a dynamic relocation for this reloc.  */
5049           if ((dynamic_symbol_p || info->shared)
5050               && (input_section->flags & SEC_ALLOC) != 0)
5051             {
5052               BFD_ASSERT (srel != NULL);
5053
5054               /* If we don't need dynamic symbol lookup, install two
5055                  RELATIVE relocations.  */
5056               if (!dynamic_symbol_p)
5057                 {
5058                   unsigned int dyn_r_type;
5059
5060                   if (r_type == R_IA64_IPLTMSB)
5061                     dyn_r_type = R_IA64_REL64MSB;
5062                   else
5063                     dyn_r_type = R_IA64_REL64LSB;
5064
5065                   elfNN_ia64_install_dyn_reloc (output_bfd, info,
5066                                                 input_section,
5067                                                 srel, rel->r_offset,
5068                                                 dyn_r_type, 0, value);
5069                   elfNN_ia64_install_dyn_reloc (output_bfd, info,
5070                                                 input_section,
5071                                                 srel, rel->r_offset + 8,
5072                                                 dyn_r_type, 0, gp_val);
5073                 }
5074               else
5075                 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
5076                                               srel, rel->r_offset, r_type,
5077                                               h->dynindx, rel->r_addend);
5078             }
5079
5080           if (r_type == R_IA64_IPLTMSB)
5081             r_type = R_IA64_DIR64MSB;
5082           else
5083             r_type = R_IA64_DIR64LSB;
5084           elfNN_ia64_install_value (hit_addr, value, r_type);
5085           r = elfNN_ia64_install_value (hit_addr + 8, gp_val, r_type);
5086           break;
5087
5088         case R_IA64_TPREL14:
5089         case R_IA64_TPREL22:
5090         case R_IA64_TPREL64I:
5091           if (elf_hash_table (info)->tls_sec == NULL)
5092             goto missing_tls_sec;
5093           value -= elfNN_ia64_tprel_base (info);
5094           r = elfNN_ia64_install_value (hit_addr, value, r_type);
5095           break;
5096
5097         case R_IA64_DTPREL14:
5098         case R_IA64_DTPREL22:
5099         case R_IA64_DTPREL64I:
5100         case R_IA64_DTPREL32LSB:
5101         case R_IA64_DTPREL32MSB:
5102         case R_IA64_DTPREL64LSB:
5103         case R_IA64_DTPREL64MSB:
5104           if (elf_hash_table (info)->tls_sec == NULL)
5105             goto missing_tls_sec;
5106           value -= elfNN_ia64_dtprel_base (info);
5107           r = elfNN_ia64_install_value (hit_addr, value, r_type);
5108           break;
5109
5110         case R_IA64_LTOFF_TPREL22:
5111         case R_IA64_LTOFF_DTPMOD22:
5112         case R_IA64_LTOFF_DTPREL22:
5113           {
5114             int got_r_type;
5115             long dynindx = h ? h->dynindx : -1;
5116             bfd_vma r_addend = rel->r_addend;
5117
5118             switch (r_type)
5119               {
5120               default:
5121               case R_IA64_LTOFF_TPREL22:
5122                 if (!dynamic_symbol_p)
5123                   {
5124                     if (elf_hash_table (info)->tls_sec == NULL)
5125                       goto missing_tls_sec;
5126                     if (!info->shared)
5127                       value -= elfNN_ia64_tprel_base (info);
5128                     else
5129                       {
5130                         r_addend += value - elfNN_ia64_dtprel_base (info);
5131                         dynindx = 0;
5132                       }
5133                   }
5134                 got_r_type = R_IA64_TPREL64LSB;
5135                 break;
5136               case R_IA64_LTOFF_DTPMOD22:
5137                 if (!dynamic_symbol_p && !info->shared)
5138                   value = 1;
5139                 got_r_type = R_IA64_DTPMOD64LSB;
5140                 break;
5141               case R_IA64_LTOFF_DTPREL22:
5142                 if (!dynamic_symbol_p)
5143                   {
5144                     if (elf_hash_table (info)->tls_sec == NULL)
5145                       goto missing_tls_sec;
5146                     value -= elfNN_ia64_dtprel_base (info);
5147                   }
5148                 got_r_type = R_IA64_DTPRELNNLSB;
5149                 break;
5150               }
5151             dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
5152             value = set_got_entry (input_bfd, info, dyn_i, dynindx, r_addend,
5153                                    value, got_r_type);
5154             value -= gp_val;
5155             r = elfNN_ia64_install_value (hit_addr, value, r_type);
5156           }
5157           break;
5158
5159         default:
5160           r = bfd_reloc_notsupported;
5161           break;
5162         }
5163
5164       switch (r)
5165         {
5166         case bfd_reloc_ok:
5167           break;
5168
5169         case bfd_reloc_undefined:
5170           /* This can happen for global table relative relocs if
5171              __gp is undefined.  This is a panic situation so we
5172              don't try to continue.  */
5173           (*info->callbacks->undefined_symbol)
5174             (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
5175           return FALSE;
5176
5177         case bfd_reloc_notsupported:
5178           {
5179             const char *name;
5180
5181             if (h)
5182               name = h->root.root.string;
5183             else
5184               name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
5185                                        sym_sec);
5186             if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
5187                                               name, input_bfd,
5188                                               input_section, rel->r_offset))
5189               return FALSE;
5190             ret_val = FALSE;
5191           }
5192           break;
5193
5194         case bfd_reloc_dangerous:
5195         case bfd_reloc_outofrange:
5196         case bfd_reloc_overflow:
5197         default:
5198 missing_tls_sec:
5199           {
5200             const char *name;
5201
5202             if (h)
5203               name = h->root.root.string;
5204             else
5205               name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
5206                                        sym_sec);
5207
5208             switch (r_type)
5209               {
5210               case R_IA64_TPREL14:
5211               case R_IA64_TPREL22:
5212               case R_IA64_TPREL64I:
5213               case R_IA64_DTPREL14:
5214               case R_IA64_DTPREL22:
5215               case R_IA64_DTPREL64I:
5216               case R_IA64_DTPREL32LSB:
5217               case R_IA64_DTPREL32MSB:
5218               case R_IA64_DTPREL64LSB:
5219               case R_IA64_DTPREL64MSB:
5220               case R_IA64_LTOFF_TPREL22:
5221               case R_IA64_LTOFF_DTPMOD22:
5222               case R_IA64_LTOFF_DTPREL22:
5223                 (*_bfd_error_handler)
5224                   (_("%B: missing TLS section for relocation %s against `%s' at 0x%lx in section `%A'."),
5225                    input_bfd, input_section, howto->name, name,
5226                    rel->r_offset);
5227                 break;
5228
5229               case R_IA64_PCREL21B:
5230               case R_IA64_PCREL21BI:
5231               case R_IA64_PCREL21M:
5232               case R_IA64_PCREL21F:
5233                 if (is_elf_hash_table (info->hash))
5234                   {
5235                     /* Relaxtion is always performed for ELF output.
5236                        Overflow failures for those relocations mean
5237                        that the section is too big to relax.  */
5238                     (*_bfd_error_handler)
5239                       (_("%B: Can't relax br (%s) to `%s' at 0x%lx in section `%A' with size 0x%lx (> 0x1000000)."),
5240                        input_bfd, input_section, howto->name, name,
5241                        rel->r_offset, input_section->size);
5242                     break;
5243                   }
5244               default:
5245                 if (!(*info->callbacks->reloc_overflow) (info,
5246                                                          &h->root,
5247                                                          name,
5248                                                          howto->name,
5249                                                          (bfd_vma) 0,
5250                                                          input_bfd,
5251                                                          input_section,
5252                                                          rel->r_offset))
5253                   return FALSE;
5254                 break;
5255               }
5256
5257             ret_val = FALSE;
5258           }
5259           break;
5260         }
5261     }
5262
5263   return ret_val;
5264 }
5265
5266 static bfd_boolean
5267 elfNN_ia64_finish_dynamic_symbol (bfd *output_bfd,
5268                                   struct bfd_link_info *info,
5269                                   struct elf_link_hash_entry *h,
5270                                   Elf_Internal_Sym *sym)
5271 {
5272   struct elfNN_ia64_link_hash_table *ia64_info;
5273   struct elfNN_ia64_dyn_sym_info *dyn_i;
5274
5275   ia64_info = elfNN_ia64_hash_table (info);
5276   if (ia64_info == NULL)
5277     return FALSE;
5278
5279   dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
5280
5281   /* Fill in the PLT data, if required.  */
5282   if (dyn_i && dyn_i->want_plt)
5283     {
5284       Elf_Internal_Rela outrel;
5285       bfd_byte *loc;
5286       asection *plt_sec;
5287       bfd_vma plt_addr, pltoff_addr, gp_val, plt_index;
5288
5289       gp_val = _bfd_get_gp_value (output_bfd);
5290
5291       /* Initialize the minimal PLT entry.  */
5292
5293       plt_index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
5294       plt_sec = ia64_info->root.splt;
5295       loc = plt_sec->contents + dyn_i->plt_offset;
5296
5297       memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
5298       elfNN_ia64_install_value (loc, plt_index, R_IA64_IMM22);
5299       elfNN_ia64_install_value (loc+2, -dyn_i->plt_offset, R_IA64_PCREL21B);
5300
5301       plt_addr = (plt_sec->output_section->vma
5302                   + plt_sec->output_offset
5303                   + dyn_i->plt_offset);
5304       pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, TRUE);
5305
5306       /* Initialize the FULL PLT entry, if needed.  */
5307       if (dyn_i->want_plt2)
5308         {
5309           loc = plt_sec->contents + dyn_i->plt2_offset;
5310
5311           memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
5312           elfNN_ia64_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
5313
5314           /* Mark the symbol as undefined, rather than as defined in the
5315              plt section.  Leave the value alone.  */
5316           /* ??? We didn't redefine it in adjust_dynamic_symbol in the
5317              first place.  But perhaps elflink.c did some for us.  */
5318           if (!h->def_regular)
5319             sym->st_shndx = SHN_UNDEF;
5320         }
5321
5322       /* Create the dynamic relocation.  */
5323       outrel.r_offset = pltoff_addr;
5324       if (bfd_little_endian (output_bfd))
5325         outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
5326       else
5327         outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
5328       outrel.r_addend = 0;
5329
5330       /* This is fun.  In the .IA_64.pltoff section, we've got entries
5331          that correspond both to real PLT entries, and those that
5332          happened to resolve to local symbols but need to be created
5333          to satisfy @pltoff relocations.  The .rela.IA_64.pltoff
5334          relocations for the real PLT should come at the end of the
5335          section, so that they can be indexed by plt entry at runtime.
5336
5337          We emitted all of the relocations for the non-PLT @pltoff
5338          entries during relocate_section.  So we can consider the
5339          existing sec->reloc_count to be the base of the array of
5340          PLT relocations.  */
5341
5342       loc = ia64_info->rel_pltoff_sec->contents;
5343       loc += ((ia64_info->rel_pltoff_sec->reloc_count + plt_index)
5344               * sizeof (ElfNN_External_Rela));
5345       bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
5346     }
5347
5348   /* Mark some specially defined symbols as absolute.  */
5349   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5350       || h == ia64_info->root.hgot
5351       || h == ia64_info->root.hplt)
5352     sym->st_shndx = SHN_ABS;
5353
5354   return TRUE;
5355 }
5356
5357 static bfd_boolean
5358 elfNN_ia64_finish_dynamic_sections (bfd *abfd,
5359                                     struct bfd_link_info *info)
5360 {
5361   struct elfNN_ia64_link_hash_table *ia64_info;
5362   bfd *dynobj;
5363
5364   ia64_info = elfNN_ia64_hash_table (info);
5365   if (ia64_info == NULL)
5366     return FALSE;
5367
5368   dynobj = ia64_info->root.dynobj;
5369
5370   if (elf_hash_table (info)->dynamic_sections_created)
5371     {
5372       ElfNN_External_Dyn *dyncon, *dynconend;
5373       asection *sdyn, *sgotplt;
5374       bfd_vma gp_val;
5375
5376       sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5377       sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
5378       BFD_ASSERT (sdyn != NULL);
5379       dyncon = (ElfNN_External_Dyn *) sdyn->contents;
5380       dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
5381
5382       gp_val = _bfd_get_gp_value (abfd);
5383
5384       for (; dyncon < dynconend; dyncon++)
5385         {
5386           Elf_Internal_Dyn dyn;
5387
5388           bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
5389
5390           switch (dyn.d_tag)
5391             {
5392             case DT_PLTGOT:
5393               dyn.d_un.d_ptr = gp_val;
5394               break;
5395
5396             case DT_PLTRELSZ:
5397               dyn.d_un.d_val = (ia64_info->minplt_entries
5398                                 * sizeof (ElfNN_External_Rela));
5399               break;
5400
5401             case DT_JMPREL:
5402               /* See the comment above in finish_dynamic_symbol.  */
5403               dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
5404                                 + ia64_info->rel_pltoff_sec->output_offset
5405                                 + (ia64_info->rel_pltoff_sec->reloc_count
5406                                    * sizeof (ElfNN_External_Rela)));
5407               break;
5408
5409             case DT_IA_64_PLT_RESERVE:
5410               dyn.d_un.d_ptr = (sgotplt->output_section->vma
5411                                 + sgotplt->output_offset);
5412               break;
5413
5414             case DT_RELASZ:
5415               /* Do not have RELASZ include JMPREL.  This makes things
5416                  easier on ld.so.  This is not what the rest of BFD set up.  */
5417               dyn.d_un.d_val -= (ia64_info->minplt_entries
5418                                  * sizeof (ElfNN_External_Rela));
5419               break;
5420             }
5421
5422           bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
5423         }
5424
5425       /* Initialize the PLT0 entry.  */
5426       if (ia64_info->root.splt)
5427         {
5428           bfd_byte *loc = ia64_info->root.splt->contents;
5429           bfd_vma pltres;
5430
5431           memcpy (loc, plt_header, PLT_HEADER_SIZE);
5432
5433           pltres = (sgotplt->output_section->vma
5434                     + sgotplt->output_offset
5435                     - gp_val);
5436
5437           elfNN_ia64_install_value (loc+1, pltres, R_IA64_GPREL22);
5438         }
5439     }
5440
5441   return TRUE;
5442 }
5443 \f
5444 /* ELF file flag handling:  */
5445
5446 /* Function to keep IA-64 specific file flags.  */
5447 static bfd_boolean
5448 elfNN_ia64_set_private_flags (bfd *abfd, flagword flags)
5449 {
5450   BFD_ASSERT (!elf_flags_init (abfd)
5451               || elf_elfheader (abfd)->e_flags == flags);
5452
5453   elf_elfheader (abfd)->e_flags = flags;
5454   elf_flags_init (abfd) = TRUE;
5455   return TRUE;
5456 }
5457
5458 /* Merge backend specific data from an object file to the output
5459    object file when linking.  */
5460 static bfd_boolean
5461 elfNN_ia64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5462 {
5463   flagword out_flags;
5464   flagword in_flags;
5465   bfd_boolean ok = TRUE;
5466
5467   /* Don't even pretend to support mixed-format linking.  */
5468   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5469       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5470     return FALSE;
5471
5472   in_flags  = elf_elfheader (ibfd)->e_flags;
5473   out_flags = elf_elfheader (obfd)->e_flags;
5474
5475   if (! elf_flags_init (obfd))
5476     {
5477       elf_flags_init (obfd) = TRUE;
5478       elf_elfheader (obfd)->e_flags = in_flags;
5479
5480       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
5481           && bfd_get_arch_info (obfd)->the_default)
5482         {
5483           return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
5484                                     bfd_get_mach (ibfd));
5485         }
5486
5487       return TRUE;
5488     }
5489
5490   /* Check flag compatibility.  */
5491   if (in_flags == out_flags)
5492     return TRUE;
5493
5494   /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set.  */
5495   if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
5496     elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
5497
5498   if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
5499     {
5500       (*_bfd_error_handler)
5501         (_("%B: linking trap-on-NULL-dereference with non-trapping files"),
5502          ibfd);
5503
5504       bfd_set_error (bfd_error_bad_value);
5505       ok = FALSE;
5506     }
5507   if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
5508     {
5509       (*_bfd_error_handler)
5510         (_("%B: linking big-endian files with little-endian files"),
5511          ibfd);
5512
5513       bfd_set_error (bfd_error_bad_value);
5514       ok = FALSE;
5515     }
5516   if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
5517     {
5518       (*_bfd_error_handler)
5519         (_("%B: linking 64-bit files with 32-bit files"),
5520          ibfd);
5521
5522       bfd_set_error (bfd_error_bad_value);
5523       ok = FALSE;
5524     }
5525   if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
5526     {
5527       (*_bfd_error_handler)
5528         (_("%B: linking constant-gp files with non-constant-gp files"),
5529          ibfd);
5530
5531       bfd_set_error (bfd_error_bad_value);
5532       ok = FALSE;
5533     }
5534   if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
5535       != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
5536     {
5537       (*_bfd_error_handler)
5538         (_("%B: linking auto-pic files with non-auto-pic files"),
5539          ibfd);
5540
5541       bfd_set_error (bfd_error_bad_value);
5542       ok = FALSE;
5543     }
5544
5545   return ok;
5546 }
5547
5548 static bfd_boolean
5549 elfNN_ia64_print_private_bfd_data (bfd *abfd, PTR ptr)
5550 {
5551   FILE *file = (FILE *) ptr;
5552   flagword flags = elf_elfheader (abfd)->e_flags;
5553
5554   BFD_ASSERT (abfd != NULL && ptr != NULL);
5555
5556   fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
5557            (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
5558            (flags & EF_IA_64_EXT) ? "EXT, " : "",
5559            (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
5560            (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
5561            (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
5562            (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
5563            (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
5564            (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
5565
5566   _bfd_elf_print_private_bfd_data (abfd, ptr);
5567   return TRUE;
5568 }
5569
5570 static enum elf_reloc_type_class
5571 elfNN_ia64_reloc_type_class (const Elf_Internal_Rela *rela)
5572 {
5573   switch ((int) ELFNN_R_TYPE (rela->r_info))
5574     {
5575     case R_IA64_REL32MSB:
5576     case R_IA64_REL32LSB:
5577     case R_IA64_REL64MSB:
5578     case R_IA64_REL64LSB:
5579       return reloc_class_relative;
5580     case R_IA64_IPLTMSB:
5581     case R_IA64_IPLTLSB:
5582       return reloc_class_plt;
5583     case R_IA64_COPY:
5584       return reloc_class_copy;
5585     default:
5586       return reloc_class_normal;
5587     }
5588 }
5589
5590 static const struct bfd_elf_special_section elfNN_ia64_special_sections[] =
5591 {
5592   { STRING_COMMA_LEN (".sbss"),  -1, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
5593   { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
5594   { NULL,                    0,   0, 0,            0 }
5595 };
5596
5597 static bfd_boolean
5598 elfNN_ia64_object_p (bfd *abfd)
5599 {
5600   asection *sec;
5601   asection *group, *unwi, *unw;
5602   flagword flags;
5603   const char *name;
5604   char *unwi_name, *unw_name;
5605   bfd_size_type amt;
5606
5607   if (abfd->flags & DYNAMIC)
5608     return TRUE;
5609
5610   /* Flags for fake group section.  */
5611   flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
5612            | SEC_EXCLUDE);
5613
5614   /* We add a fake section group for each .gnu.linkonce.t.* section,
5615      which isn't in a section group, and its unwind sections.  */
5616   for (sec = abfd->sections; sec != NULL; sec = sec->next)
5617     {
5618       if (elf_sec_group (sec) == NULL
5619           && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
5620               == (SEC_LINK_ONCE | SEC_CODE))
5621           && CONST_STRNEQ (sec->name, ".gnu.linkonce.t."))
5622         {
5623           name = sec->name + 16;
5624
5625           amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
5626           unwi_name = bfd_alloc (abfd, amt);
5627           if (!unwi_name)
5628             return FALSE;
5629
5630           strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
5631           unwi = bfd_get_section_by_name (abfd, unwi_name);
5632
5633           amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
5634           unw_name = bfd_alloc (abfd, amt);
5635           if (!unw_name)
5636             return FALSE;
5637
5638           strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
5639           unw = bfd_get_section_by_name (abfd, unw_name);
5640
5641           /* We need to create a fake group section for it and its
5642              unwind sections.  */
5643           group = bfd_make_section_anyway_with_flags (abfd, name,
5644                                                       flags);
5645           if (group == NULL)
5646             return FALSE;
5647
5648           /* Move the fake group section to the beginning.  */
5649           bfd_section_list_remove (abfd, group);
5650           bfd_section_list_prepend (abfd, group);
5651
5652           elf_next_in_group (group) = sec;
5653
5654           elf_group_name (sec) = name;
5655           elf_next_in_group (sec) = sec;
5656           elf_sec_group (sec) = group;
5657
5658           if (unwi)
5659             {
5660               elf_group_name (unwi) = name;
5661               elf_next_in_group (unwi) = sec;
5662               elf_next_in_group (sec) = unwi;
5663               elf_sec_group (unwi) = group;
5664             }
5665
5666            if (unw)
5667              {
5668                elf_group_name (unw) = name;
5669                if (unwi)
5670                  {
5671                    elf_next_in_group (unw) = elf_next_in_group (unwi);
5672                    elf_next_in_group (unwi) = unw;
5673                  }
5674                else
5675                  {
5676                    elf_next_in_group (unw) = sec;
5677                    elf_next_in_group (sec) = unw;
5678                  }
5679                elf_sec_group (unw) = group;
5680              }
5681
5682            /* Fake SHT_GROUP section header.  */
5683           elf_section_data (group)->this_hdr.bfd_section = group;
5684           elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
5685         }
5686     }
5687   return TRUE;
5688 }
5689
5690 static bfd_boolean
5691 elfNN_ia64_hpux_vec (const bfd_target *vec)
5692 {
5693   extern const bfd_target bfd_elfNN_ia64_hpux_big_vec;
5694   return (vec == & bfd_elfNN_ia64_hpux_big_vec);
5695 }
5696
5697 static void
5698 elfNN_hpux_post_process_headers (bfd *abfd,
5699                                  struct bfd_link_info *info ATTRIBUTE_UNUSED)
5700 {
5701   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5702
5703   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
5704   i_ehdrp->e_ident[EI_ABIVERSION] = 1;
5705 }
5706
5707 static bfd_boolean
5708 elfNN_hpux_backend_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5709                                              asection *sec, int *retval)
5710 {
5711   if (bfd_is_com_section (sec))
5712     {
5713       *retval = SHN_IA_64_ANSI_COMMON;
5714       return TRUE;
5715     }
5716   return FALSE;
5717 }
5718
5719 static void
5720 elfNN_hpux_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5721                                       asymbol *asym)
5722 {
5723   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5724
5725   switch (elfsym->internal_elf_sym.st_shndx)
5726     {
5727     case SHN_IA_64_ANSI_COMMON:
5728       asym->section = bfd_com_section_ptr;
5729       asym->value = elfsym->internal_elf_sym.st_size;
5730       asym->flags &= ~BSF_GLOBAL;
5731       break;
5732     }
5733 }
5734
5735 #ifdef INCLUDE_IA64_VMS
5736
5737 static bfd_boolean
5738 elfNN_vms_section_from_shdr (bfd *abfd,
5739                              Elf_Internal_Shdr *hdr,
5740                              const char *name,
5741                              int shindex)
5742 {
5743   asection *newsect;
5744
5745   switch (hdr->sh_type)
5746     {
5747     case SHT_IA_64_VMS_TRACE:
5748     case SHT_IA_64_VMS_DEBUG:
5749     case SHT_IA_64_VMS_DEBUG_STR:
5750       break;
5751
5752     default:
5753       return elfNN_ia64_section_from_shdr (abfd, hdr, name, shindex);
5754     }
5755
5756   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5757     return FALSE;
5758   newsect = hdr->bfd_section;
5759
5760   return TRUE;
5761 }
5762
5763 static bfd_boolean
5764 elfNN_vms_object_p (bfd *abfd)
5765 {
5766   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5767   Elf_Internal_Phdr *i_phdr = elf_tdata (abfd)->phdr;
5768   unsigned int i;
5769   unsigned int num_text = 0;
5770   unsigned int num_data = 0;
5771   unsigned int num_rodata = 0;
5772   char name[16];
5773
5774   if (!elfNN_ia64_object_p (abfd))
5775     return FALSE;
5776
5777   for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
5778     {
5779       /* Is there a section for this segment?  */
5780       bfd_vma base_vma = i_phdr->p_vaddr;
5781       bfd_vma limit_vma = base_vma + i_phdr->p_filesz;
5782
5783       if (i_phdr->p_type != PT_LOAD)
5784         continue;
5785
5786     again:
5787       while (base_vma < limit_vma)
5788         {
5789           bfd_vma next_vma = limit_vma;
5790           asection *nsec;
5791           asection *sec;
5792           flagword flags;
5793           char *nname = NULL;
5794
5795           /* Find a section covering base_vma.  */
5796           for (sec = abfd->sections; sec != NULL; sec = sec->next)
5797             {
5798               if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
5799                 continue;
5800               if (sec->vma <= base_vma && sec->vma + sec->size > base_vma)
5801                 {
5802                   base_vma = sec->vma + sec->size;
5803                   goto again;
5804                 }
5805               if (sec->vma < next_vma && sec->vma + sec->size >= base_vma)
5806                 next_vma = sec->vma;
5807             }
5808
5809           /* No section covering [base_vma; next_vma).  Create a fake one.  */
5810           flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
5811           if (i_phdr->p_flags & PF_X)
5812             {
5813               flags |= SEC_CODE;
5814               if (num_text++ == 0)
5815                 nname = ".text";
5816               else
5817                 sprintf (name, ".text$%u", num_text);
5818             }
5819           else if ((i_phdr->p_flags & (PF_R | PF_W)) == PF_R)
5820             {
5821               flags |= SEC_READONLY;
5822               sprintf (name, ".rodata$%u", num_rodata++);
5823             }
5824           else
5825             {
5826               flags |= SEC_DATA;
5827               sprintf (name, ".data$%u", num_data++);
5828             }
5829
5830           /* Allocate name.  */
5831           if (nname == NULL)
5832             {
5833               size_t name_len = strlen (name) + 1;
5834               nname = bfd_alloc (abfd, name_len);
5835               if (nname == NULL)
5836                 return FALSE;
5837               memcpy (nname, name, name_len);
5838             }
5839
5840           /* Create and fill new section.  */
5841           nsec = bfd_make_section_anyway_with_flags (abfd, nname, flags);
5842           if (nsec == NULL)
5843             return FALSE;
5844           nsec->vma = base_vma;
5845           nsec->size = next_vma - base_vma;
5846           nsec->filepos = i_phdr->p_offset + (base_vma - i_phdr->p_vaddr);
5847           
5848           base_vma = next_vma;
5849         }
5850     }
5851   return TRUE;
5852 }
5853
5854 static void
5855 elfNN_vms_post_process_headers (bfd *abfd,
5856                                 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5857 {
5858   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5859
5860   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_OPENVMS;
5861   i_ehdrp->e_ident[EI_ABIVERSION] = 2;
5862 }
5863
5864 static bfd_boolean
5865 elfNN_vms_section_processing (bfd *abfd ATTRIBUTE_UNUSED,
5866                               Elf_Internal_Shdr *hdr)
5867 {
5868   if (hdr->bfd_section != NULL)
5869     {
5870       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5871
5872       if (strcmp (name, ".text") == 0)
5873         hdr->sh_flags |= SHF_IA_64_VMS_SHARED;
5874       else if ((strcmp (name, ".debug") == 0)
5875             || (strcmp (name, ".debug_abbrev") == 0)
5876             || (strcmp (name, ".debug_aranges") == 0)
5877             || (strcmp (name, ".debug_frame") == 0)
5878             || (strcmp (name, ".debug_info") == 0)
5879             || (strcmp (name, ".debug_loc") == 0)
5880             || (strcmp (name, ".debug_macinfo") == 0)
5881             || (strcmp (name, ".debug_pubnames") == 0)
5882             || (strcmp (name, ".debug_pubtypes") == 0))
5883         hdr->sh_type = SHT_IA_64_VMS_DEBUG;
5884       else if ((strcmp (name, ".debug_line") == 0)
5885             || (strcmp (name, ".debug_ranges") == 0))
5886         hdr->sh_type = SHT_IA_64_VMS_TRACE;
5887       else if (strcmp (name, ".debug_str") == 0)
5888         hdr->sh_type = SHT_IA_64_VMS_DEBUG_STR;
5889       else if (strcmp (name, ".vms_display_name_info") == 0)
5890         {
5891           int idx, symcount;
5892           asymbol **syms;
5893           struct elf_obj_tdata *t = elf_tdata (abfd);
5894           int buf[2];
5895           int demangler_sym_idx = -1;
5896
5897           symcount = bfd_get_symcount (abfd);
5898           syms = bfd_get_outsymbols (abfd);
5899           for (idx = 0; idx < symcount; idx++)
5900             {
5901               asymbol *sym;
5902               sym = syms[idx];
5903               if ((sym->flags & (BSF_DEBUGGING | BSF_DYNAMIC))
5904                   && strchr (sym->name, '@')
5905                   && (strcmp (sym->section->name, BFD_ABS_SECTION_NAME) == 0))
5906                 {
5907                   demangler_sym_idx = sym->udata.i;
5908                   break;
5909                 }
5910             }
5911
5912           hdr->sh_type = SHT_IA_64_VMS_DISPLAY_NAME_INFO;
5913           hdr->sh_entsize = 4;
5914           hdr->sh_addralign = 0;
5915           hdr->sh_link = t->symtab_section;
5916
5917           /* Find symtab index of demangler routine and stuff it in
5918              the second long word of section data.  */
5919
5920           if (demangler_sym_idx > -1)
5921             {
5922               bfd_seek (abfd, hdr->sh_offset, SEEK_SET);
5923               bfd_bread (buf, hdr->sh_size, abfd);
5924               buf [1] = demangler_sym_idx;
5925               bfd_seek (abfd, hdr->sh_offset, SEEK_SET);
5926               bfd_bwrite (buf, hdr->sh_size, abfd);
5927             }
5928         }
5929     }
5930
5931   return TRUE;
5932 }
5933
5934 /* The final processing done just before writing out a VMS IA-64 ELF
5935    object file.  */
5936
5937 static void
5938 elfNN_vms_final_write_processing (bfd *abfd,
5939                                   bfd_boolean linker ATTRIBUTE_UNUSED)
5940 {
5941   Elf_Internal_Shdr *hdr;
5942   asection *s;
5943   int unwind_info_sect_idx = 0;
5944
5945   for (s = abfd->sections; s; s = s->next)
5946     {
5947       hdr = &elf_section_data (s)->this_hdr;
5948
5949       if (strcmp (bfd_get_section_name (abfd, hdr->bfd_section),
5950                   ".IA_64.unwind_info") == 0)
5951         unwind_info_sect_idx = elf_section_data (s)->this_idx;
5952
5953       switch (hdr->sh_type)
5954         {
5955         case SHT_IA_64_UNWIND:
5956           /* VMS requires sh_info to point to the unwind info section.  */
5957           hdr->sh_info = unwind_info_sect_idx;
5958           break;
5959         }
5960     }
5961
5962   if (! elf_flags_init (abfd))
5963     {
5964       unsigned long flags = 0;
5965
5966       if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
5967         flags |= EF_IA_64_BE;
5968       if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
5969         flags |= EF_IA_64_ABI64;
5970
5971       elf_elfheader(abfd)->e_flags = flags;
5972       elf_flags_init (abfd) = TRUE;
5973     }
5974 }
5975
5976 static bfd_boolean
5977 elfNN_vms_close_and_cleanup (bfd *abfd)
5978 {
5979   if (bfd_get_format (abfd) == bfd_object)
5980     {
5981       long isize, irsize;
5982
5983       if (elf_shstrtab (abfd) != NULL)
5984         _bfd_elf_strtab_free (elf_shstrtab (abfd));
5985
5986       /* Pad to 8 byte boundary for IPF/VMS.  */
5987       isize = bfd_get_size (abfd);
5988       if ((irsize = isize/8*8) < isize)
5989         {
5990           int ishort = (irsize + 8) - isize;
5991           bfd_seek (abfd, isize, SEEK_SET);
5992           bfd_bwrite (bfd_zmalloc (ishort), ishort, abfd);
5993         }
5994     }
5995
5996   return _bfd_generic_close_and_cleanup (abfd);
5997 }
5998 #endif /* INCLUDE_IA64_VMS */
5999 \f
6000 #define TARGET_LITTLE_SYM               bfd_elfNN_ia64_little_vec
6001 #define TARGET_LITTLE_NAME              "elfNN-ia64-little"
6002 #define TARGET_BIG_SYM                  bfd_elfNN_ia64_big_vec
6003 #define TARGET_BIG_NAME                 "elfNN-ia64-big"
6004 #define ELF_ARCH                        bfd_arch_ia64
6005 #define ELF_MACHINE_CODE                EM_IA_64
6006 #define ELF_MACHINE_ALT1                1999    /* EAS2.3 */
6007 #define ELF_MACHINE_ALT2                1998    /* EAS2.2 */
6008 #define ELF_MAXPAGESIZE                 0x10000 /* 64KB */
6009 #define ELF_COMMONPAGESIZE              0x4000  /* 16KB */
6010
6011 #define elf_backend_section_from_shdr \
6012         elfNN_ia64_section_from_shdr
6013 #define elf_backend_section_flags \
6014         elfNN_ia64_section_flags
6015 #define elf_backend_fake_sections \
6016         elfNN_ia64_fake_sections
6017 #define elf_backend_final_write_processing \
6018         elfNN_ia64_final_write_processing
6019 #define elf_backend_add_symbol_hook \
6020         elfNN_ia64_add_symbol_hook
6021 #define elf_backend_additional_program_headers \
6022         elfNN_ia64_additional_program_headers
6023 #define elf_backend_modify_segment_map \
6024         elfNN_ia64_modify_segment_map
6025 #define elf_backend_modify_program_headers \
6026         elfNN_ia64_modify_program_headers
6027 #define elf_info_to_howto \
6028         elfNN_ia64_info_to_howto
6029
6030 #define bfd_elfNN_bfd_reloc_type_lookup \
6031         elfNN_ia64_reloc_type_lookup
6032 #define bfd_elfNN_bfd_reloc_name_lookup \
6033         elfNN_ia64_reloc_name_lookup
6034 #define bfd_elfNN_bfd_is_local_label_name \
6035         elfNN_ia64_is_local_label_name
6036 #define bfd_elfNN_bfd_relax_section \
6037         elfNN_ia64_relax_section
6038
6039 #define elf_backend_object_p \
6040         elfNN_ia64_object_p
6041
6042 /* Stuff for the BFD linker: */
6043 #define bfd_elfNN_bfd_link_hash_table_create \
6044         elfNN_ia64_hash_table_create
6045 #define bfd_elfNN_bfd_link_hash_table_free \
6046         elfNN_ia64_hash_table_free
6047 #define elf_backend_create_dynamic_sections \
6048         elfNN_ia64_create_dynamic_sections
6049 #define elf_backend_check_relocs \
6050         elfNN_ia64_check_relocs
6051 #define elf_backend_adjust_dynamic_symbol \
6052         elfNN_ia64_adjust_dynamic_symbol
6053 #define elf_backend_size_dynamic_sections \
6054         elfNN_ia64_size_dynamic_sections
6055 #define elf_backend_omit_section_dynsym \
6056   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6057 #define elf_backend_relocate_section \
6058         elfNN_ia64_relocate_section
6059 #define elf_backend_finish_dynamic_symbol \
6060         elfNN_ia64_finish_dynamic_symbol
6061 #define elf_backend_finish_dynamic_sections \
6062         elfNN_ia64_finish_dynamic_sections
6063 #define bfd_elfNN_bfd_final_link \
6064         elfNN_ia64_final_link
6065
6066 #define bfd_elfNN_bfd_merge_private_bfd_data \
6067         elfNN_ia64_merge_private_bfd_data
6068 #define bfd_elfNN_bfd_set_private_flags \
6069         elfNN_ia64_set_private_flags
6070 #define bfd_elfNN_bfd_print_private_bfd_data \
6071         elfNN_ia64_print_private_bfd_data
6072
6073 #define elf_backend_plt_readonly        1
6074 #define elf_backend_want_plt_sym        0
6075 #define elf_backend_plt_alignment       5
6076 #define elf_backend_got_header_size     0
6077 #define elf_backend_want_got_plt        1
6078 #define elf_backend_may_use_rel_p       1
6079 #define elf_backend_may_use_rela_p      1
6080 #define elf_backend_default_use_rela_p  1
6081 #define elf_backend_want_dynbss         0
6082 #define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
6083 #define elf_backend_hide_symbol         elfNN_ia64_hash_hide_symbol
6084 #define elf_backend_fixup_symbol        _bfd_elf_link_hash_fixup_symbol
6085 #define elf_backend_reloc_type_class    elfNN_ia64_reloc_type_class
6086 #define elf_backend_rela_normal         1
6087 #define elf_backend_special_sections    elfNN_ia64_special_sections
6088 #define elf_backend_default_execstack   0
6089
6090 /* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
6091    SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
6092    We don't want to flood users with so many error messages. We turn
6093    off the warning for now. It will be turned on later when the Intel
6094    compiler is fixed.   */
6095 #define elf_backend_link_order_error_handler NULL
6096
6097 #include "elfNN-target.h"
6098
6099 /* HPUX-specific vectors.  */
6100
6101 #undef  TARGET_LITTLE_SYM
6102 #undef  TARGET_LITTLE_NAME
6103 #undef  TARGET_BIG_SYM
6104 #define TARGET_BIG_SYM                  bfd_elfNN_ia64_hpux_big_vec
6105 #undef  TARGET_BIG_NAME
6106 #define TARGET_BIG_NAME                 "elfNN-ia64-hpux-big"
6107
6108 /* These are HP-UX specific functions.  */
6109
6110 #undef  elf_backend_post_process_headers
6111 #define elf_backend_post_process_headers elfNN_hpux_post_process_headers
6112
6113 #undef  elf_backend_section_from_bfd_section
6114 #define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
6115
6116 #undef elf_backend_symbol_processing
6117 #define elf_backend_symbol_processing elfNN_hpux_backend_symbol_processing
6118
6119 #undef  elf_backend_want_p_paddr_set_to_zero
6120 #define elf_backend_want_p_paddr_set_to_zero 1
6121
6122 #undef ELF_COMMONPAGESIZE
6123 #undef ELF_OSABI
6124 #define ELF_OSABI                       ELFOSABI_HPUX
6125
6126 #undef  elfNN_bed
6127 #define elfNN_bed elfNN_ia64_hpux_bed
6128
6129 #include "elfNN-target.h"
6130
6131 /* VMS-specific vectors.  */
6132 #ifdef INCLUDE_IA64_VMS
6133
6134 #undef  TARGET_LITTLE_SYM
6135 #define TARGET_LITTLE_SYM               bfd_elfNN_ia64_vms_vec
6136 #undef  TARGET_LITTLE_NAME
6137 #define TARGET_LITTLE_NAME              "elfNN-ia64-vms"
6138 #undef  TARGET_BIG_SYM
6139 #undef  TARGET_BIG_NAME
6140
6141 /* These are VMS specific functions.  */
6142
6143 #undef  elf_backend_object_p
6144 #define elf_backend_object_p elfNN_vms_object_p
6145
6146 #undef  elf_backend_section_from_shdr
6147 #define elf_backend_section_from_shdr elfNN_vms_section_from_shdr
6148
6149 #undef  elf_backend_post_process_headers
6150 #define elf_backend_post_process_headers elfNN_vms_post_process_headers
6151
6152 #undef  elf_backend_section_processing
6153 #define elf_backend_section_processing elfNN_vms_section_processing
6154
6155 #undef  elf_backend_final_write_processing
6156 #define elf_backend_final_write_processing elfNN_vms_final_write_processing
6157
6158 #undef  bfd_elfNN_close_and_cleanup
6159 #define bfd_elfNN_close_and_cleanup elfNN_vms_close_and_cleanup
6160
6161 #undef  elf_backend_section_from_bfd_section
6162
6163 #undef  elf_backend_symbol_processing
6164
6165 #undef  elf_backend_want_p_paddr_set_to_zero
6166
6167 #undef ELF_OSABI
6168 #define ELF_OSABI                       ELFOSABI_OPENVMS
6169
6170 #undef  ELF_MAXPAGESIZE
6171 #define ELF_MAXPAGESIZE                 0x10000 /* 64KB */
6172
6173 #undef  elfNN_bed
6174 #define elfNN_bed elfNN_ia64_vms_bed
6175
6176 /* Use VMS-style archives (in particular, don't use the standard coff
6177    archive format).  */
6178 #define bfd_elfNN_archive_functions
6179
6180 #undef bfd_elfNN_archive_p
6181 #define bfd_elfNN_archive_p _bfd_vms_lib_ia64_archive_p
6182
6183 #define bfd_elfNN_archive_slurp_armap \
6184   _bfd_vms_lib_slurp_armap
6185 #define bfd_elfNN_archive_slurp_extended_name_table \
6186   _bfd_vms_lib_slurp_extended_name_table
6187 #define bfd_elfNN_archive_construct_extended_name_table \
6188   _bfd_vms_lib_construct_extended_name_table
6189 #define bfd_elfNN_archive_truncate_arname \
6190   _bfd_vms_lib_truncate_arname
6191 #define bfd_elfNN_archive_write_armap \
6192   _bfd_vms_lib_write_armap
6193 #define bfd_elfNN_archive_read_ar_hdr \
6194   _bfd_vms_lib_read_ar_hdr
6195 #define bfd_elfNN_archive_write_ar_hdr \
6196   _bfd_vms_lib_write_ar_hdr
6197 #define bfd_elfNN_archive_openr_next_archived_file \
6198   _bfd_vms_lib_openr_next_archived_file
6199 #define bfd_elfNN_archive_get_elt_at_index \
6200   _bfd_vms_lib_get_elt_at_index
6201 #define bfd_elfNN_archive_generic_stat_arch_elt \
6202   _bfd_vms_lib_generic_stat_arch_elt
6203 #define bfd_elfNN_archive_update_armap_timestamp \
6204   _bfd_vms_lib_update_armap_timestamp
6205
6206 #include "elfNN-target.h"
6207
6208 #endif /* INCLUDE_IA64_VMS */