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