7c4e1394ec3858f4da877cc9d5bc34d2dde25277
[external/binutils.git] / bfd / elfxx-ia64.c
1 /* IA-64 support for 64-bit ELF
2    Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "opcode/ia64.h"
26 #include "elf/ia64.h"
27
28 /*
29  * THE RULES for all the stuff the linker creates --
30  *
31  * GOT          Entries created in response to LTOFF or LTOFF_FPTR
32  *              relocations.  Dynamic relocs created for dynamic
33  *              symbols in an application; REL relocs for locals
34  *              in a shared library.
35  *
36  * FPTR         The canonical function descriptor.  Created for local
37  *              symbols in applications.  Descriptors for dynamic symbols
38  *              and local symbols in shared libraries are created by
39  *              ld.so.  Thus there are no dynamic relocs against these
40  *              objects.  The FPTR relocs for such _are_ passed through
41  *              to the dynamic relocation tables.
42  *
43  * FULL_PLT     Created for a PCREL21B relocation against a dynamic symbol.
44  *              Requires the creation of a PLTOFF entry.  This does not
45  *              require any dynamic relocations.
46  *
47  * PLTOFF       Created by PLTOFF relocations.  For local symbols, this
48  *              is an alternate function descriptor, and in shared libraries
49  *              requires two REL relocations.  Note that this cannot be
50  *              transformed into an FPTR relocation, since it must be in
51  *              range of the GP.  For dynamic symbols, this is a function
52  *              descriptor for a MIN_PLT entry, and requires one IPLT reloc.
53  *
54  * MIN_PLT      Created by PLTOFF entries against dynamic symbols.  This
55  *              does not reqire dynamic relocations.
56  */
57
58 #define USE_RELA                /* we want RELA relocs, not REL */
59
60 #define NELEMS(a)       ((int) (sizeof (a) / sizeof ((a)[0])))
61
62 typedef struct bfd_hash_entry *(*new_hash_entry_func)
63   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
64
65 /* In dynamically (linker-) created sections, we generally need to keep track
66    of the place a symbol or expression got allocated to. This is done via hash
67    tables that store entries of the following type.  */
68
69 struct elfNN_ia64_dyn_sym_info
70 {
71   /* The addend for which this entry is relevant.  */
72   bfd_vma addend;
73
74   /* Next addend in the list.  */
75   struct elfNN_ia64_dyn_sym_info *next;
76
77   bfd_vma got_offset;
78   bfd_vma fptr_offset;
79   bfd_vma pltoff_offset;
80   bfd_vma plt_offset;
81   bfd_vma plt2_offset;
82
83   /* The symbol table entry, if any, that this was derrived from.  */
84   struct elf_link_hash_entry *h;
85
86   /* Used to count non-got, non-plt relocations for delayed sizing
87      of relocation sections.  */
88   struct elfNN_ia64_dyn_reloc_entry
89   {
90     struct elfNN_ia64_dyn_reloc_entry *next;
91     asection *srel;
92     int type;
93     int count;
94   } *reloc_entries;
95
96   /* True when the section contents have been updated.  */
97   unsigned got_done : 1;
98   unsigned fptr_done : 1;
99   unsigned pltoff_done : 1;
100
101   /* True for the different kinds of linker data we want created.  */
102   unsigned want_got : 1;
103   unsigned want_fptr : 1;
104   unsigned want_ltoff_fptr : 1;
105   unsigned want_plt : 1;
106   unsigned want_plt2 : 1;
107   unsigned want_pltoff : 1;
108 };
109
110 struct elfNN_ia64_local_hash_entry
111 {
112   struct bfd_hash_entry root;
113   struct elfNN_ia64_dyn_sym_info *info;
114
115   /* True if this hash entry's addends was translated for
116      SHF_MERGE optimization.  */
117   unsigned sec_merge_done : 1;
118 };
119
120 struct elfNN_ia64_local_hash_table
121 {
122   struct bfd_hash_table root;
123   /* No additional fields for now.  */
124 };
125
126 struct elfNN_ia64_link_hash_entry
127 {
128   struct elf_link_hash_entry root;
129   struct elfNN_ia64_dyn_sym_info *info;
130 };
131
132 struct elfNN_ia64_link_hash_table
133 {
134   /* The main hash table */
135   struct elf_link_hash_table root;
136
137   asection *got_sec;            /* the linkage table section (or NULL) */
138   asection *rel_got_sec;        /* dynamic relocation section for same */
139   asection *fptr_sec;           /* function descriptor table (or NULL) */
140   asection *plt_sec;            /* the primary plt section (or NULL) */
141   asection *pltoff_sec;         /* private descriptors for plt (or NULL) */
142   asection *rel_pltoff_sec;     /* dynamic relocation section for same */
143
144   bfd_size_type minplt_entries; /* number of minplt entries */
145   unsigned reltext : 1;         /* are there relocs against readonly sections? */
146
147   struct elfNN_ia64_local_hash_table loc_hash_table;
148 };
149
150 #define elfNN_ia64_hash_table(p) \
151   ((struct elfNN_ia64_link_hash_table *) ((p)->hash))
152
153 static bfd_reloc_status_type elfNN_ia64_reloc
154   PARAMS ((bfd *abfd, arelent *reloc, asymbol *sym, PTR data,
155            asection *input_section, bfd *output_bfd, char **error_message));
156 static reloc_howto_type * lookup_howto
157   PARAMS ((unsigned int rtype));
158 static reloc_howto_type *elfNN_ia64_reloc_type_lookup
159   PARAMS ((bfd *abfd, bfd_reloc_code_real_type bfd_code));
160 static void elfNN_ia64_info_to_howto
161   PARAMS ((bfd *abfd, arelent *bfd_reloc, ElfNN_Internal_Rela *elf_reloc));
162 static boolean elfNN_ia64_relax_section
163   PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
164           boolean *again));
165 static boolean is_unwind_section_name
166   PARAMS ((bfd *abfd, const char *));
167 static boolean elfNN_ia64_section_from_shdr
168   PARAMS ((bfd *, ElfNN_Internal_Shdr *, char *));
169 static boolean elfNN_ia64_section_flags
170   PARAMS ((flagword *, ElfNN_Internal_Shdr *));
171 static boolean elfNN_ia64_fake_sections
172   PARAMS ((bfd *abfd, ElfNN_Internal_Shdr *hdr, asection *sec));
173 static void elfNN_ia64_final_write_processing
174   PARAMS ((bfd *abfd, boolean linker));
175 static boolean elfNN_ia64_add_symbol_hook
176   PARAMS ((bfd *abfd, struct bfd_link_info *info, const Elf_Internal_Sym *sym,
177            const char **namep, flagword *flagsp, asection **secp,
178            bfd_vma *valp));
179 static boolean elfNN_ia64_aix_vec
180   PARAMS ((const bfd_target *vec));
181 static boolean elfNN_ia64_aix_add_symbol_hook
182   PARAMS ((bfd *abfd, struct bfd_link_info *info, const Elf_Internal_Sym *sym,
183            const char **namep, flagword *flagsp, asection **secp,
184            bfd_vma *valp));
185 static boolean elfNN_ia64_aix_link_add_symbols
186   PARAMS ((bfd *abfd, struct bfd_link_info *info));
187 static int elfNN_ia64_additional_program_headers
188   PARAMS ((bfd *abfd));
189 static boolean elfNN_ia64_modify_segment_map
190   PARAMS ((bfd *));
191 static boolean elfNN_ia64_is_local_label_name
192   PARAMS ((bfd *abfd, const char *name));
193 static boolean elfNN_ia64_dynamic_symbol_p
194   PARAMS ((struct elf_link_hash_entry *h, struct bfd_link_info *info));
195 static boolean elfNN_ia64_local_hash_table_init
196   PARAMS ((struct elfNN_ia64_local_hash_table *ht, bfd *abfd,
197            new_hash_entry_func new));
198 static struct bfd_hash_entry *elfNN_ia64_new_loc_hash_entry
199   PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
200            const char *string));
201 static struct bfd_hash_entry *elfNN_ia64_new_elf_hash_entry
202   PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
203            const char *string));
204 static void elfNN_ia64_hash_copy_indirect
205   PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
206 static void elfNN_ia64_hash_hide_symbol
207   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, boolean));
208 static struct bfd_link_hash_table *elfNN_ia64_hash_table_create
209   PARAMS ((bfd *abfd));
210 static struct elfNN_ia64_local_hash_entry *elfNN_ia64_local_hash_lookup
211   PARAMS ((struct elfNN_ia64_local_hash_table *table, const char *string,
212            boolean create, boolean copy));
213 static boolean elfNN_ia64_global_dyn_sym_thunk
214   PARAMS ((struct bfd_hash_entry *, PTR));
215 static boolean elfNN_ia64_local_dyn_sym_thunk
216   PARAMS ((struct bfd_hash_entry *, PTR));
217 static void elfNN_ia64_dyn_sym_traverse
218   PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
219            boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR),
220            PTR info));
221 static boolean elfNN_ia64_create_dynamic_sections
222   PARAMS ((bfd *abfd, struct bfd_link_info *info));
223 static struct elfNN_ia64_local_hash_entry * get_local_sym_hash
224   PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
225            bfd *abfd, const Elf_Internal_Rela *rel, boolean create));
226 static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
227   PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
228            struct elf_link_hash_entry *h,
229            bfd *abfd, const Elf_Internal_Rela *rel, boolean create));
230 static asection *get_got
231   PARAMS ((bfd *abfd, struct bfd_link_info *info,
232            struct elfNN_ia64_link_hash_table *ia64_info));
233 static asection *get_fptr
234   PARAMS ((bfd *abfd, struct bfd_link_info *info,
235            struct elfNN_ia64_link_hash_table *ia64_info));
236 static asection *get_pltoff
237   PARAMS ((bfd *abfd, struct bfd_link_info *info,
238            struct elfNN_ia64_link_hash_table *ia64_info));
239 static asection *get_reloc_section
240   PARAMS ((bfd *abfd, struct elfNN_ia64_link_hash_table *ia64_info,
241            asection *sec, boolean create));
242 static boolean count_dyn_reloc
243   PARAMS ((bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
244            asection *srel, int type));
245 static boolean elfNN_ia64_check_relocs
246   PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
247            const Elf_Internal_Rela *relocs));
248 static boolean elfNN_ia64_adjust_dynamic_symbol
249   PARAMS ((struct bfd_link_info *info, struct elf_link_hash_entry *h));
250 static long global_sym_index
251   PARAMS ((struct elf_link_hash_entry *h));
252 static boolean allocate_fptr
253   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
254 static boolean allocate_global_data_got
255   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
256 static boolean allocate_global_fptr_got
257   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
258 static boolean allocate_local_got
259   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
260 static boolean allocate_pltoff_entries
261   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
262 static boolean allocate_plt_entries
263   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
264 static boolean allocate_plt2_entries
265   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
266 static boolean allocate_dynrel_entries
267   PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
268 static boolean elfNN_ia64_size_dynamic_sections
269   PARAMS ((bfd *output_bfd, struct bfd_link_info *info));
270 static bfd_reloc_status_type elfNN_ia64_install_value
271   PARAMS ((bfd *abfd, bfd_byte *hit_addr, bfd_vma val, unsigned int r_type));
272 static void elfNN_ia64_install_dyn_reloc
273   PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
274            asection *srel, bfd_vma offset, unsigned int type,
275            long dynindx, bfd_vma addend));
276 static bfd_vma set_got_entry
277   PARAMS ((bfd *abfd, struct bfd_link_info *info,
278            struct elfNN_ia64_dyn_sym_info *dyn_i, long dynindx,
279            bfd_vma addend, bfd_vma value, unsigned int dyn_r_type));
280 static bfd_vma set_fptr_entry
281   PARAMS ((bfd *abfd, struct bfd_link_info *info,
282            struct elfNN_ia64_dyn_sym_info *dyn_i,
283            bfd_vma value));
284 static bfd_vma set_pltoff_entry
285   PARAMS ((bfd *abfd, struct bfd_link_info *info,
286            struct elfNN_ia64_dyn_sym_info *dyn_i,
287            bfd_vma value, boolean));
288 static int elfNN_ia64_unwind_entry_compare
289   PARAMS ((const PTR, const PTR));
290 static boolean elfNN_ia64_final_link
291   PARAMS ((bfd *abfd, struct bfd_link_info *info));
292 static boolean elfNN_ia64_relocate_section
293   PARAMS ((bfd *output_bfd, struct bfd_link_info *info, bfd *input_bfd,
294            asection *input_section, bfd_byte *contents,
295            Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
296            asection **local_sections));
297 static boolean elfNN_ia64_finish_dynamic_symbol
298   PARAMS ((bfd *output_bfd, struct bfd_link_info *info,
299            struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
300 static boolean elfNN_ia64_finish_dynamic_sections
301   PARAMS ((bfd *abfd, struct bfd_link_info *info));
302 static boolean elfNN_ia64_set_private_flags
303   PARAMS ((bfd *abfd, flagword flags));
304 static boolean elfNN_ia64_merge_private_bfd_data
305   PARAMS ((bfd *ibfd, bfd *obfd));
306 static boolean elfNN_ia64_print_private_bfd_data
307   PARAMS ((bfd *abfd, PTR ptr));
308 static enum elf_reloc_type_class elfNN_ia64_reloc_type_class
309   PARAMS ((const Elf_Internal_Rela *));
310 static boolean elfNN_ia64_hpux_vec
311   PARAMS ((const bfd_target *vec));
312 static void elfNN_hpux_post_process_headers
313   PARAMS ((bfd *abfd, struct bfd_link_info *info));
314 boolean elfNN_hpux_backend_section_from_bfd_section
315   PARAMS ((bfd *abfd, asection *sec, int *retval));
316 \f
317 /* ia64-specific relocation */
318
319 /* Perform a relocation.  Not much to do here as all the hard work is
320    done in elfNN_ia64_final_link_relocate.  */
321 static bfd_reloc_status_type
322 elfNN_ia64_reloc (abfd, reloc, sym, data, input_section,
323                   output_bfd, error_message)
324      bfd *abfd ATTRIBUTE_UNUSED;
325      arelent *reloc;
326      asymbol *sym ATTRIBUTE_UNUSED;
327      PTR data ATTRIBUTE_UNUSED;
328      asection *input_section;
329      bfd *output_bfd;
330      char **error_message;
331 {
332   if (output_bfd)
333     {
334       reloc->address += input_section->output_offset;
335       return bfd_reloc_ok;
336     }
337   *error_message = "Unsupported call to elfNN_ia64_reloc";
338   return bfd_reloc_notsupported;
339 }
340
341 #define IA64_HOWTO(TYPE, NAME, SIZE, PCREL, IN)                 \
342   HOWTO (TYPE, 0, SIZE, 0, PCREL, 0, complain_overflow_signed,  \
343          elfNN_ia64_reloc, NAME, false, 0, 0, IN)
344
345 /* This table has to be sorted according to increasing number of the
346    TYPE field.  */
347 static reloc_howto_type ia64_howto_table[] =
348   {
349     IA64_HOWTO (R_IA64_NONE,        "NONE",        0, false, true),
350
351     IA64_HOWTO (R_IA64_IMM14,       "IMM14",       0, false, true),
352     IA64_HOWTO (R_IA64_IMM22,       "IMM22",       0, false, true),
353     IA64_HOWTO (R_IA64_IMM64,       "IMM64",       0, false, true),
354     IA64_HOWTO (R_IA64_DIR32MSB,    "DIR32MSB",    2, false, true),
355     IA64_HOWTO (R_IA64_DIR32LSB,    "DIR32LSB",    2, false, true),
356     IA64_HOWTO (R_IA64_DIR64MSB,    "DIR64MSB",    4, false, true),
357     IA64_HOWTO (R_IA64_DIR64LSB,    "DIR64LSB",    4, false, true),
358
359     IA64_HOWTO (R_IA64_GPREL22,     "GPREL22",     0, false, true),
360     IA64_HOWTO (R_IA64_GPREL64I,    "GPREL64I",    0, false, true),
361     IA64_HOWTO (R_IA64_GPREL32MSB,  "GPREL32MSB",  2, false, true),
362     IA64_HOWTO (R_IA64_GPREL32LSB,  "GPREL32LSB",  2, false, true),
363     IA64_HOWTO (R_IA64_GPREL64MSB,  "GPREL64MSB",  4, false, true),
364     IA64_HOWTO (R_IA64_GPREL64LSB,  "GPREL64LSB",  4, false, true),
365
366     IA64_HOWTO (R_IA64_LTOFF22,     "LTOFF22",     0, false, true),
367     IA64_HOWTO (R_IA64_LTOFF64I,    "LTOFF64I",    0, false, true),
368
369     IA64_HOWTO (R_IA64_PLTOFF22,    "PLTOFF22",    0, false, true),
370     IA64_HOWTO (R_IA64_PLTOFF64I,   "PLTOFF64I",   0, false, true),
371     IA64_HOWTO (R_IA64_PLTOFF64MSB, "PLTOFF64MSB", 4, false, true),
372     IA64_HOWTO (R_IA64_PLTOFF64LSB, "PLTOFF64LSB", 4, false, true),
373
374     IA64_HOWTO (R_IA64_FPTR64I,     "FPTR64I",     0, false, true),
375     IA64_HOWTO (R_IA64_FPTR32MSB,   "FPTR32MSB",   2, false, true),
376     IA64_HOWTO (R_IA64_FPTR32LSB,   "FPTR32LSB",   2, false, true),
377     IA64_HOWTO (R_IA64_FPTR64MSB,   "FPTR64MSB",   4, false, true),
378     IA64_HOWTO (R_IA64_FPTR64LSB,   "FPTR64LSB",   4, false, true),
379
380     IA64_HOWTO (R_IA64_PCREL60B,    "PCREL60B",    0, true, true),
381     IA64_HOWTO (R_IA64_PCREL21B,    "PCREL21B",    0, true, true),
382     IA64_HOWTO (R_IA64_PCREL21M,    "PCREL21M",    0, true, true),
383     IA64_HOWTO (R_IA64_PCREL21F,    "PCREL21F",    0, true, true),
384     IA64_HOWTO (R_IA64_PCREL32MSB,  "PCREL32MSB",  2, true, true),
385     IA64_HOWTO (R_IA64_PCREL32LSB,  "PCREL32LSB",  2, true, true),
386     IA64_HOWTO (R_IA64_PCREL64MSB,  "PCREL64MSB",  4, true, true),
387     IA64_HOWTO (R_IA64_PCREL64LSB,  "PCREL64LSB",  4, true, true),
388
389     IA64_HOWTO (R_IA64_LTOFF_FPTR22, "LTOFF_FPTR22", 0, false, true),
390     IA64_HOWTO (R_IA64_LTOFF_FPTR64I, "LTOFF_FPTR64I", 0, false, true),
391     IA64_HOWTO (R_IA64_LTOFF_FPTR32MSB, "LTOFF_FPTR32MSB", 2, false, true),
392     IA64_HOWTO (R_IA64_LTOFF_FPTR32LSB, "LTOFF_FPTR32LSB", 2, false, true),
393     IA64_HOWTO (R_IA64_LTOFF_FPTR64MSB, "LTOFF_FPTR64MSB", 4, false, true),
394     IA64_HOWTO (R_IA64_LTOFF_FPTR64LSB, "LTOFF_FPTR64LSB", 4, false, true),
395
396     IA64_HOWTO (R_IA64_SEGREL32MSB, "SEGREL32MSB", 2, false, true),
397     IA64_HOWTO (R_IA64_SEGREL32LSB, "SEGREL32LSB", 2, false, true),
398     IA64_HOWTO (R_IA64_SEGREL64MSB, "SEGREL64MSB", 4, false, true),
399     IA64_HOWTO (R_IA64_SEGREL64LSB, "SEGREL64LSB", 4, false, true),
400
401     IA64_HOWTO (R_IA64_SECREL32MSB, "SECREL32MSB", 2, false, true),
402     IA64_HOWTO (R_IA64_SECREL32LSB, "SECREL32LSB", 2, false, true),
403     IA64_HOWTO (R_IA64_SECREL64MSB, "SECREL64MSB", 4, false, true),
404     IA64_HOWTO (R_IA64_SECREL64LSB, "SECREL64LSB", 4, false, true),
405
406     IA64_HOWTO (R_IA64_REL32MSB,    "REL32MSB",    2, false, true),
407     IA64_HOWTO (R_IA64_REL32LSB,    "REL32LSB",    2, false, true),
408     IA64_HOWTO (R_IA64_REL64MSB,    "REL64MSB",    4, false, true),
409     IA64_HOWTO (R_IA64_REL64LSB,    "REL64LSB",    4, false, true),
410
411     IA64_HOWTO (R_IA64_LTV32MSB,    "LTV32MSB",    2, false, true),
412     IA64_HOWTO (R_IA64_LTV32LSB,    "LTV32LSB",    2, false, true),
413     IA64_HOWTO (R_IA64_LTV64MSB,    "LTV64MSB",    4, false, true),
414     IA64_HOWTO (R_IA64_LTV64LSB,    "LTV64LSB",    4, false, true),
415
416     IA64_HOWTO (R_IA64_PCREL21BI,   "PCREL21BI",   0, true, true),
417     IA64_HOWTO (R_IA64_PCREL22,     "PCREL22",     0, true, true),
418     IA64_HOWTO (R_IA64_PCREL64I,    "PCREL64I",    0, true, true),
419
420     IA64_HOWTO (R_IA64_IPLTMSB,     "IPLTMSB",     4, false, true),
421     IA64_HOWTO (R_IA64_IPLTLSB,     "IPLTLSB",     4, false, true),
422     IA64_HOWTO (R_IA64_COPY,        "COPY",        4, false, true),
423     IA64_HOWTO (R_IA64_LTOFF22X,    "LTOFF22X",    0, false, true),
424     IA64_HOWTO (R_IA64_LDXMOV,      "LDXMOV",      0, false, true),
425
426     IA64_HOWTO (R_IA64_TPREL22,     "TPREL22",     0, false, false),
427     IA64_HOWTO (R_IA64_TPREL64MSB,  "TPREL64MSB",  8, false, false),
428     IA64_HOWTO (R_IA64_TPREL64LSB,  "TPREL64LSB",  8, false, false),
429     IA64_HOWTO (R_IA64_LTOFF_TP22,  "LTOFF_TP22",  0, false, false),
430   };
431
432 static unsigned char elf_code_to_howto_index[R_IA64_MAX_RELOC_CODE + 1];
433
434 /* Given a BFD reloc type, return the matching HOWTO structure.  */
435
436 static reloc_howto_type*
437 lookup_howto (rtype)
438      unsigned int rtype;
439 {
440   static int inited = 0;
441   int i;
442
443   if (!inited)
444     {
445       inited = 1;
446
447       memset (elf_code_to_howto_index, 0xff, sizeof (elf_code_to_howto_index));
448       for (i = 0; i < NELEMS (ia64_howto_table); ++i)
449         elf_code_to_howto_index[ia64_howto_table[i].type] = i;
450     }
451
452   BFD_ASSERT (rtype <= R_IA64_MAX_RELOC_CODE);
453   i = elf_code_to_howto_index[rtype];
454   if (i >= NELEMS (ia64_howto_table))
455     return 0;
456   return ia64_howto_table + i;
457 }
458
459 static reloc_howto_type*
460 elfNN_ia64_reloc_type_lookup (abfd, bfd_code)
461      bfd *abfd ATTRIBUTE_UNUSED;
462      bfd_reloc_code_real_type bfd_code;
463 {
464   unsigned int rtype;
465
466   switch (bfd_code)
467     {
468     case BFD_RELOC_NONE:                rtype = R_IA64_NONE; break;
469
470     case BFD_RELOC_IA64_IMM14:          rtype = R_IA64_IMM14; break;
471     case BFD_RELOC_IA64_IMM22:          rtype = R_IA64_IMM22; break;
472     case BFD_RELOC_IA64_IMM64:          rtype = R_IA64_IMM64; break;
473
474     case BFD_RELOC_IA64_DIR32MSB:       rtype = R_IA64_DIR32MSB; break;
475     case BFD_RELOC_IA64_DIR32LSB:       rtype = R_IA64_DIR32LSB; break;
476     case BFD_RELOC_IA64_DIR64MSB:       rtype = R_IA64_DIR64MSB; break;
477     case BFD_RELOC_IA64_DIR64LSB:       rtype = R_IA64_DIR64LSB; break;
478
479     case BFD_RELOC_IA64_GPREL22:        rtype = R_IA64_GPREL22; break;
480     case BFD_RELOC_IA64_GPREL64I:       rtype = R_IA64_GPREL64I; break;
481     case BFD_RELOC_IA64_GPREL32MSB:     rtype = R_IA64_GPREL32MSB; break;
482     case BFD_RELOC_IA64_GPREL32LSB:     rtype = R_IA64_GPREL32LSB; break;
483     case BFD_RELOC_IA64_GPREL64MSB:     rtype = R_IA64_GPREL64MSB; break;
484     case BFD_RELOC_IA64_GPREL64LSB:     rtype = R_IA64_GPREL64LSB; break;
485
486     case BFD_RELOC_IA64_LTOFF22:        rtype = R_IA64_LTOFF22; break;
487     case BFD_RELOC_IA64_LTOFF64I:       rtype = R_IA64_LTOFF64I; break;
488
489     case BFD_RELOC_IA64_PLTOFF22:       rtype = R_IA64_PLTOFF22; break;
490     case BFD_RELOC_IA64_PLTOFF64I:      rtype = R_IA64_PLTOFF64I; break;
491     case BFD_RELOC_IA64_PLTOFF64MSB:    rtype = R_IA64_PLTOFF64MSB; break;
492     case BFD_RELOC_IA64_PLTOFF64LSB:    rtype = R_IA64_PLTOFF64LSB; break;
493     case BFD_RELOC_IA64_FPTR64I:        rtype = R_IA64_FPTR64I; break;
494     case BFD_RELOC_IA64_FPTR32MSB:      rtype = R_IA64_FPTR32MSB; break;
495     case BFD_RELOC_IA64_FPTR32LSB:      rtype = R_IA64_FPTR32LSB; break;
496     case BFD_RELOC_IA64_FPTR64MSB:      rtype = R_IA64_FPTR64MSB; break;
497     case BFD_RELOC_IA64_FPTR64LSB:      rtype = R_IA64_FPTR64LSB; break;
498
499     case BFD_RELOC_IA64_PCREL21B:       rtype = R_IA64_PCREL21B; break;
500     case BFD_RELOC_IA64_PCREL21BI:      rtype = R_IA64_PCREL21BI; break;
501     case BFD_RELOC_IA64_PCREL21M:       rtype = R_IA64_PCREL21M; break;
502     case BFD_RELOC_IA64_PCREL21F:       rtype = R_IA64_PCREL21F; break;
503     case BFD_RELOC_IA64_PCREL22:        rtype = R_IA64_PCREL22; break;
504     case BFD_RELOC_IA64_PCREL60B:       rtype = R_IA64_PCREL60B; break;
505     case BFD_RELOC_IA64_PCREL64I:       rtype = R_IA64_PCREL64I; break;
506     case BFD_RELOC_IA64_PCREL32MSB:     rtype = R_IA64_PCREL32MSB; break;
507     case BFD_RELOC_IA64_PCREL32LSB:     rtype = R_IA64_PCREL32LSB; break;
508     case BFD_RELOC_IA64_PCREL64MSB:     rtype = R_IA64_PCREL64MSB; break;
509     case BFD_RELOC_IA64_PCREL64LSB:     rtype = R_IA64_PCREL64LSB; break;
510
511     case BFD_RELOC_IA64_LTOFF_FPTR22:   rtype = R_IA64_LTOFF_FPTR22; break;
512     case BFD_RELOC_IA64_LTOFF_FPTR64I:  rtype = R_IA64_LTOFF_FPTR64I; break;
513     case BFD_RELOC_IA64_LTOFF_FPTR32MSB: rtype = R_IA64_LTOFF_FPTR32MSB; break;
514     case BFD_RELOC_IA64_LTOFF_FPTR32LSB: rtype = R_IA64_LTOFF_FPTR32LSB; break;
515     case BFD_RELOC_IA64_LTOFF_FPTR64MSB: rtype = R_IA64_LTOFF_FPTR64MSB; break;
516     case BFD_RELOC_IA64_LTOFF_FPTR64LSB: rtype = R_IA64_LTOFF_FPTR64LSB; break;
517
518     case BFD_RELOC_IA64_SEGREL32MSB:    rtype = R_IA64_SEGREL32MSB; break;
519     case BFD_RELOC_IA64_SEGREL32LSB:    rtype = R_IA64_SEGREL32LSB; break;
520     case BFD_RELOC_IA64_SEGREL64MSB:    rtype = R_IA64_SEGREL64MSB; break;
521     case BFD_RELOC_IA64_SEGREL64LSB:    rtype = R_IA64_SEGREL64LSB; break;
522
523     case BFD_RELOC_IA64_SECREL32MSB:    rtype = R_IA64_SECREL32MSB; break;
524     case BFD_RELOC_IA64_SECREL32LSB:    rtype = R_IA64_SECREL32LSB; break;
525     case BFD_RELOC_IA64_SECREL64MSB:    rtype = R_IA64_SECREL64MSB; break;
526     case BFD_RELOC_IA64_SECREL64LSB:    rtype = R_IA64_SECREL64LSB; break;
527
528     case BFD_RELOC_IA64_REL32MSB:       rtype = R_IA64_REL32MSB; break;
529     case BFD_RELOC_IA64_REL32LSB:       rtype = R_IA64_REL32LSB; break;
530     case BFD_RELOC_IA64_REL64MSB:       rtype = R_IA64_REL64MSB; break;
531     case BFD_RELOC_IA64_REL64LSB:       rtype = R_IA64_REL64LSB; break;
532
533     case BFD_RELOC_IA64_LTV32MSB:       rtype = R_IA64_LTV32MSB; break;
534     case BFD_RELOC_IA64_LTV32LSB:       rtype = R_IA64_LTV32LSB; break;
535     case BFD_RELOC_IA64_LTV64MSB:       rtype = R_IA64_LTV64MSB; break;
536     case BFD_RELOC_IA64_LTV64LSB:       rtype = R_IA64_LTV64LSB; break;
537
538     case BFD_RELOC_IA64_IPLTMSB:        rtype = R_IA64_IPLTMSB; break;
539     case BFD_RELOC_IA64_IPLTLSB:        rtype = R_IA64_IPLTLSB; break;
540     case BFD_RELOC_IA64_COPY:           rtype = R_IA64_COPY; break;
541     case BFD_RELOC_IA64_LTOFF22X:       rtype = R_IA64_LTOFF22X; break;
542     case BFD_RELOC_IA64_LDXMOV:         rtype = R_IA64_LDXMOV; break;
543
544     case BFD_RELOC_IA64_TPREL22:        rtype = R_IA64_TPREL22; break;
545     case BFD_RELOC_IA64_TPREL64MSB:     rtype = R_IA64_TPREL64MSB; break;
546     case BFD_RELOC_IA64_TPREL64LSB:     rtype = R_IA64_TPREL64LSB; break;
547     case BFD_RELOC_IA64_LTOFF_TP22:     rtype = R_IA64_LTOFF_TP22; break;
548
549     default: return 0;
550     }
551   return lookup_howto (rtype);
552 }
553
554 /* Given a ELF reloc, return the matching HOWTO structure.  */
555
556 static void
557 elfNN_ia64_info_to_howto (abfd, bfd_reloc, elf_reloc)
558      bfd *abfd ATTRIBUTE_UNUSED;
559      arelent *bfd_reloc;
560      ElfNN_Internal_Rela *elf_reloc;
561 {
562   bfd_reloc->howto
563     = lookup_howto ((unsigned int) ELFNN_R_TYPE (elf_reloc->r_info));
564 }
565 \f
566 #define PLT_HEADER_SIZE         (3 * 16)
567 #define PLT_MIN_ENTRY_SIZE      (1 * 16)
568 #define PLT_FULL_ENTRY_SIZE     (2 * 16)
569 #define PLT_RESERVED_WORDS      3
570
571 static const bfd_byte plt_header[PLT_HEADER_SIZE] =
572 {
573   0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21,  /*   [MMI]       mov r2=r14;;       */
574   0xe0, 0x00, 0x08, 0x00, 0x48, 0x00,  /*               addl r14=0,r2      */
575   0x00, 0x00, 0x04, 0x00,              /*               nop.i 0x0;;        */
576   0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14,  /*   [MMI]       ld8 r16=[r14],8;;  */
577   0x10, 0x41, 0x38, 0x30, 0x28, 0x00,  /*               ld8 r17=[r14],8    */
578   0x00, 0x00, 0x04, 0x00,              /*               nop.i 0x0;;        */
579   0x11, 0x08, 0x00, 0x1c, 0x18, 0x10,  /*   [MIB]       ld8 r1=[r14]       */
580   0x60, 0x88, 0x04, 0x80, 0x03, 0x00,  /*               mov b6=r17         */
581   0x60, 0x00, 0x80, 0x00               /*               br.few b6;;        */
582 };
583
584 static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
585 {
586   0x11, 0x78, 0x00, 0x00, 0x00, 0x24,  /*   [MIB]       mov r15=0          */
587   0x00, 0x00, 0x00, 0x02, 0x00, 0x00,  /*               nop.i 0x0          */
588   0x00, 0x00, 0x00, 0x40               /*               br.few 0 <PLT0>;;  */
589 };
590
591 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
592 {
593   0x0b, 0x78, 0x00, 0x02, 0x00, 0x24,  /*   [MMI]       addl r15=0,r1;;    */
594   0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0,  /*               ld8 r16=[r15],8    */
595   0x01, 0x08, 0x00, 0x84,              /*               mov r14=r1;;       */
596   0x11, 0x08, 0x00, 0x1e, 0x18, 0x10,  /*   [MIB]       ld8 r1=[r15]       */
597   0x60, 0x80, 0x04, 0x80, 0x03, 0x00,  /*               mov b6=r16         */
598   0x60, 0x00, 0x80, 0x00               /*               br.few b6;;        */
599 };
600
601 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
602 #define AIX_DYNAMIC_INTERPRETER "/usr/lib/ia64l64/libc.so.1"
603 #define DYNAMIC_INTERPRETER(abfd) \
604   (elfNN_ia64_aix_vec (abfd->xvec) ? AIX_DYNAMIC_INTERPRETER : ELF_DYNAMIC_INTERPRETER)
605
606 /* Select out of range branch fixup type.  Note that Itanium does
607    not support brl, and so it gets emulated by the kernel.  */
608 #undef USE_BRL
609
610 static const bfd_byte oor_brl[16] =
611 {
612   0x05, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MLX]        nop.m 0            */
613   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /*               brl.sptk.few tgt;; */
614   0x00, 0x00, 0x00, 0xc0
615 };
616
617 static const bfd_byte oor_ip[48] =
618 {
619   0x04, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MLX]        nop.m 0            */
620   0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,  /*               movl r15=0         */
621   0x01, 0x00, 0x00, 0x60,
622   0x03, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MII]        nop.m 0            */
623   0x00, 0x01, 0x00, 0x60, 0x00, 0x00,  /*               mov r16=ip;;       */
624   0xf2, 0x80, 0x00, 0x80,              /*               add r16=r15,r16;;  */
625   0x11, 0x00, 0x00, 0x00, 0x01, 0x00,  /*  [MIB]        nop.m 0            */
626   0x60, 0x80, 0x04, 0x80, 0x03, 0x00,  /*               mov b6=r16         */
627   0x60, 0x00, 0x80, 0x00               /*               br b6;;            */
628 };
629 \f
630 /* These functions do relaxation for IA-64 ELF.
631
632    This is primarily to support branches to targets out of range;
633    relaxation of R_IA64_LTOFF22X and R_IA64_LDXMOV not yet supported.  */
634
635 static boolean
636 elfNN_ia64_relax_section (abfd, sec, link_info, again)
637      bfd *abfd;
638      asection *sec;
639      struct bfd_link_info *link_info;
640      boolean *again;
641 {
642   struct one_fixup
643     {
644       struct one_fixup *next;
645       asection *tsec;
646       bfd_vma toff;
647       bfd_vma trampoff;
648     };
649
650   Elf_Internal_Shdr *symtab_hdr;
651   Elf_Internal_Shdr *shndx_hdr;
652   Elf_Internal_Rela *internal_relocs;
653   Elf_Internal_Rela *free_relocs = NULL;
654   Elf_Internal_Rela *irel, *irelend;
655   bfd_byte *contents;
656   bfd_byte *free_contents = NULL;
657   ElfNN_External_Sym *extsyms;
658   ElfNN_External_Sym *free_extsyms = NULL;
659   Elf_External_Sym_Shndx *shndx_buf = NULL;
660   struct elfNN_ia64_link_hash_table *ia64_info;
661   struct one_fixup *fixups = NULL;
662   boolean changed_contents = false;
663   boolean changed_relocs = false;
664
665   /* Assume we're not going to change any sizes, and we'll only need
666      one pass.  */
667   *again = false;
668
669   /* Nothing to do if there are no relocations.  */
670   if ((sec->flags & SEC_RELOC) == 0
671       || sec->reloc_count == 0)
672     return true;
673
674   /* If this is the first time we have been called for this section,
675      initialize the cooked size.  */
676   if (sec->_cooked_size == 0)
677     sec->_cooked_size = sec->_raw_size;
678
679   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
680
681   /* Load the relocations for this section.  */
682   internal_relocs = (_bfd_elfNN_link_read_relocs
683                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
684                       link_info->keep_memory));
685   if (internal_relocs == NULL)
686     goto error_return;
687
688   if (! link_info->keep_memory)
689     free_relocs = internal_relocs;
690
691   ia64_info = elfNN_ia64_hash_table (link_info);
692   irelend = internal_relocs + sec->reloc_count;
693
694   for (irel = internal_relocs; irel < irelend; irel++)
695     if (ELFNN_R_TYPE (irel->r_info) == (int) R_IA64_PCREL21B)
696       break;
697
698   /* No branch-type relocations.  */
699   if (irel == irelend)
700     {
701       if (free_relocs != NULL)
702         free (free_relocs);
703       return true;
704     }
705
706   /* Get the section contents.  */
707   if (elf_section_data (sec)->this_hdr.contents != NULL)
708     contents = elf_section_data (sec)->this_hdr.contents;
709   else
710     {
711       contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
712       if (contents == NULL)
713         goto error_return;
714       free_contents = contents;
715
716       if (! bfd_get_section_contents (abfd, sec, contents,
717                                       (file_ptr) 0, sec->_raw_size))
718         goto error_return;
719     }
720
721   /* Read this BFD's local symbols.  */
722   if (symtab_hdr->contents != NULL)
723     extsyms = (ElfNN_External_Sym *) symtab_hdr->contents;
724   else
725     {
726       bfd_size_type amt;
727
728       amt = symtab_hdr->sh_info * sizeof (ElfNN_External_Sym);
729       extsyms = (ElfNN_External_Sym *) bfd_malloc (amt);
730       if (extsyms == NULL)
731         goto error_return;
732       free_extsyms = extsyms;
733       if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
734           || bfd_bread (extsyms, amt, abfd) != amt)
735         goto error_return;
736     }
737
738   shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
739   if (shndx_hdr->sh_size != 0)
740     {
741       bfd_size_type amt;
742
743       amt = symtab_hdr->sh_info * sizeof (Elf_External_Sym_Shndx);
744       shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
745       if (shndx_buf == NULL)
746         goto error_return;
747       if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
748           || bfd_bread (shndx_buf, amt, abfd) != amt)
749         goto error_return;
750     }
751
752   for (; irel < irelend; irel++)
753     {
754       bfd_vma symaddr, reladdr, trampoff, toff, roff;
755       Elf_Internal_Sym isym;
756       asection *tsec;
757       struct one_fixup *f;
758       bfd_size_type amt;
759
760       if (ELFNN_R_TYPE (irel->r_info) != (int) R_IA64_PCREL21B)
761         continue;
762
763       /* Get the value of the symbol referred to by the reloc.  */
764       if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
765         {
766           ElfNN_External_Sym *esym;
767           Elf_External_Sym_Shndx *shndx;
768
769           /* A local symbol.  */
770           esym = extsyms + ELFNN_R_SYM (irel->r_info);
771           shndx = shndx_buf + (shndx_buf ? ELFNN_R_SYM (irel->r_info) : 0);
772           bfd_elfNN_swap_symbol_in (abfd, esym, shndx, &isym);
773           if (isym.st_shndx == SHN_UNDEF)
774             continue;   /* We can't do anthing with undefined symbols.  */
775           else if (isym.st_shndx == SHN_ABS)
776             tsec = bfd_abs_section_ptr;
777           else if (isym.st_shndx == SHN_COMMON)
778             tsec = bfd_com_section_ptr;
779           else if (isym.st_shndx == SHN_IA_64_ANSI_COMMON)
780             tsec = bfd_com_section_ptr;
781           else
782             tsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
783
784           toff = isym.st_value;
785         }
786       else
787         {
788           unsigned long indx;
789           struct elf_link_hash_entry *h;
790           struct elfNN_ia64_dyn_sym_info *dyn_i;
791
792           indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
793           h = elf_sym_hashes (abfd)[indx];
794           BFD_ASSERT (h != NULL);
795
796           while (h->root.type == bfd_link_hash_indirect
797                  || h->root.type == bfd_link_hash_warning)
798             h = (struct elf_link_hash_entry *) h->root.u.i.link;
799
800           dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, false);
801
802           /* For branches to dynamic symbols, we're interested instead
803              in a branch to the PLT entry.  */
804           if (dyn_i && dyn_i->want_plt2)
805             {
806               tsec = ia64_info->plt_sec;
807               toff = dyn_i->plt2_offset;
808             }
809           else
810             {
811               /* We can't do anthing with undefined symbols.  */
812               if (h->root.type == bfd_link_hash_undefined
813                   || h->root.type == bfd_link_hash_undefweak)
814                 continue;
815
816               tsec = h->root.u.def.section;
817               toff = h->root.u.def.value;
818             }
819         }
820
821       symaddr = (tsec->output_section->vma
822                  + tsec->output_offset
823                  + toff
824                  + irel->r_addend);
825
826       roff = irel->r_offset;
827       reladdr = (sec->output_section->vma
828                  + sec->output_offset
829                  + roff) & (bfd_vma) -4;
830
831       /* If the branch is in range, no need to do anything.  */
832       if ((bfd_signed_vma) (symaddr - reladdr) >= -0x1000000
833           && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
834         continue;
835
836       /* If the branch and target are in the same section, you've
837          got one honking big section and we can't help you.  You'll
838          get an error message later.  */
839       if (tsec == sec)
840         continue;
841
842       /* Look for an existing fixup to this address.  */
843       for (f = fixups; f ; f = f->next)
844         if (f->tsec == tsec && f->toff == toff)
845           break;
846
847       if (f == NULL)
848         {
849           /* Two alternatives: If it's a branch to a PLT entry, we can
850              make a copy of the FULL_PLT entry.  Otherwise, we'll have
851              to use a `brl' insn to get where we're going.  */
852
853           size_t size;
854
855           if (tsec == ia64_info->plt_sec)
856             size = sizeof (plt_full_entry);
857           else
858             {
859 #ifdef USE_BRL
860               size = sizeof (oor_brl);
861 #else
862               size = sizeof (oor_ip);
863 #endif
864             }
865
866           /* Resize the current section to make room for the new branch.  */
867           trampoff = (sec->_cooked_size + 15) & (bfd_vma) -16;
868           amt = trampoff + size;
869           contents = (bfd_byte *) bfd_realloc (contents, amt);
870           if (contents == NULL)
871             goto error_return;
872           sec->_cooked_size = amt;
873
874           if (tsec == ia64_info->plt_sec)
875             {
876               memcpy (contents + trampoff, plt_full_entry, size);
877
878               /* Hijack the old relocation for use as the PLTOFF reloc.  */
879               irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
880                                            R_IA64_PLTOFF22);
881               irel->r_offset = trampoff;
882             }
883           else
884             {
885 #ifdef USE_BRL
886               memcpy (contents + trampoff, oor_brl, size);
887               irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
888                                            R_IA64_PCREL60B);
889               irel->r_offset = trampoff + 2;
890 #else
891               memcpy (contents + trampoff, oor_ip, size);
892               irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
893                                            R_IA64_PCREL64I);
894               irel->r_addend -= 16;
895               irel->r_offset = trampoff + 2;
896 #endif
897             }
898
899           /* Record the fixup so we don't do it again this section.  */
900           f = (struct one_fixup *) bfd_malloc ((bfd_size_type) sizeof (*f));
901           f->next = fixups;
902           f->tsec = tsec;
903           f->toff = toff;
904           f->trampoff = trampoff;
905           fixups = f;
906         }
907       else
908         {
909           /* Nop out the reloc, since we're finalizing things here.  */
910           irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
911         }
912
913       /* Fix up the existing branch to hit the trampoline.  Hope like
914          hell this doesn't overflow too.  */
915       if (elfNN_ia64_install_value (abfd, contents + roff,
916                                     f->trampoff - (roff & (bfd_vma) -4),
917                                     R_IA64_PCREL21B) != bfd_reloc_ok)
918         goto error_return;
919
920       changed_contents = true;
921       changed_relocs = true;
922     }
923
924   /* Clean up and go home.  */
925   while (fixups)
926     {
927       struct one_fixup *f = fixups;
928       fixups = fixups->next;
929       free (f);
930     }
931
932   if (changed_relocs)
933     elf_section_data (sec)->relocs = internal_relocs;
934   else if (free_relocs != NULL)
935     free (free_relocs);
936
937   if (changed_contents)
938     elf_section_data (sec)->this_hdr.contents = contents;
939   else if (free_contents != NULL)
940     {
941       if (! link_info->keep_memory)
942         free (free_contents);
943       else
944         {
945           /* Cache the section contents for elf_link_input_bfd.  */
946           elf_section_data (sec)->this_hdr.contents = contents;
947         }
948     }
949
950   if (shndx_buf != NULL)
951     free (shndx_buf);
952
953   if (free_extsyms != NULL)
954     {
955       if (! link_info->keep_memory)
956         free (free_extsyms);
957       else
958         {
959           /* Cache the symbols for elf_link_input_bfd.  */
960           symtab_hdr->contents = (unsigned char *) extsyms;
961         }
962     }
963
964   *again = changed_contents || changed_relocs;
965   return true;
966
967  error_return:
968   if (free_relocs != NULL)
969     free (free_relocs);
970   if (free_contents != NULL)
971     free (free_contents);
972   if (shndx_buf != NULL)
973     free (shndx_buf);
974   if (free_extsyms != NULL)
975     free (free_extsyms);
976   return false;
977 }
978 \f
979 /* Return true if NAME is an unwind table section name.  */
980
981 static inline boolean
982 is_unwind_section_name (abfd, name)
983         bfd *abfd;
984         const char *name;
985 {
986   size_t len1, len2, len3;
987
988   if (elfNN_ia64_hpux_vec (abfd->xvec)
989       && !strcmp (name, ELF_STRING_ia64_unwind_hdr))
990     return false;
991
992   len1 = sizeof (ELF_STRING_ia64_unwind) - 1;
993   len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
994   len3 = sizeof (ELF_STRING_ia64_unwind_once) - 1;
995   return ((strncmp (name, ELF_STRING_ia64_unwind, len1) == 0
996            && strncmp (name, ELF_STRING_ia64_unwind_info, len2) != 0)
997           || strncmp (name, ELF_STRING_ia64_unwind_once, len3) == 0);
998 }
999
1000 /* Handle an IA-64 specific section when reading an object file.  This
1001    is called when elfcode.h finds a section with an unknown type.  */
1002
1003 static boolean
1004 elfNN_ia64_section_from_shdr (abfd, hdr, name)
1005      bfd *abfd;
1006      ElfNN_Internal_Shdr *hdr;
1007      char *name;
1008 {
1009   asection *newsect;
1010
1011   /* There ought to be a place to keep ELF backend specific flags, but
1012      at the moment there isn't one.  We just keep track of the
1013      sections by their name, instead.  Fortunately, the ABI gives
1014      suggested names for all the MIPS specific sections, so we will
1015      probably get away with this.  */
1016   switch (hdr->sh_type)
1017     {
1018     case SHT_IA_64_UNWIND:
1019     case SHT_INIT_ARRAY:
1020     case SHT_FINI_ARRAY:
1021     case SHT_PREINIT_ARRAY:
1022     case SHT_IA_64_HP_OPT_ANOT:
1023       break;
1024
1025     case SHT_IA_64_EXT:
1026       if (strcmp (name, ELF_STRING_ia64_archext) != 0)
1027         return false;
1028       break;
1029
1030     default:
1031       return false;
1032     }
1033
1034   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1035     return false;
1036   newsect = hdr->bfd_section;
1037
1038   return true;
1039 }
1040
1041 /* Convert IA-64 specific section flags to bfd internal section flags.  */
1042
1043 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
1044    flag.  */
1045
1046 static boolean
1047 elfNN_ia64_section_flags (flags, hdr)
1048      flagword *flags;
1049      ElfNN_Internal_Shdr *hdr;
1050 {
1051   if (hdr->sh_flags & SHF_IA_64_SHORT)
1052     *flags |= SEC_SMALL_DATA;
1053
1054   return true;
1055 }
1056
1057 /* Set the correct type for an IA-64 ELF section.  We do this by the
1058    section name, which is a hack, but ought to work.  */
1059
1060 static boolean
1061 elfNN_ia64_fake_sections (abfd, hdr, sec)
1062      bfd *abfd ATTRIBUTE_UNUSED;
1063      ElfNN_Internal_Shdr *hdr;
1064      asection *sec;
1065 {
1066   register const char *name;
1067
1068   name = bfd_get_section_name (abfd, sec);
1069
1070   if (is_unwind_section_name (abfd, name))
1071     {
1072       /* We don't have the sections numbered at this point, so sh_info
1073          is set later, in elfNN_ia64_final_write_processing.  */
1074       hdr->sh_type = SHT_IA_64_UNWIND;
1075       hdr->sh_flags |= SHF_LINK_ORDER;
1076     }
1077   else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
1078     hdr->sh_type = SHT_IA_64_EXT;
1079   else if (strcmp (name, ".init_array") == 0)
1080     hdr->sh_type = SHT_INIT_ARRAY;
1081   else if (strcmp (name, ".fini_array") == 0)
1082     hdr->sh_type = SHT_FINI_ARRAY;
1083   else if (strcmp (name, ".preinit_array") == 0)
1084     hdr->sh_type = SHT_PREINIT_ARRAY;
1085   else if (strcmp (name, ".HP.opt_annot") == 0)
1086     hdr->sh_type = SHT_IA_64_HP_OPT_ANOT;
1087   else if (strcmp (name, ".reloc") == 0)
1088     /*
1089      * This is an ugly, but unfortunately necessary hack that is
1090      * needed when producing EFI binaries on IA-64. It tells
1091      * elf.c:elf_fake_sections() not to consider ".reloc" as a section
1092      * containing ELF relocation info.  We need this hack in order to
1093      * be able to generate ELF binaries that can be translated into
1094      * EFI applications (which are essentially COFF objects).  Those
1095      * files contain a COFF ".reloc" section inside an ELFNN object,
1096      * which would normally cause BFD to segfault because it would
1097      * attempt to interpret this section as containing relocation
1098      * entries for section "oc".  With this hack enabled, ".reloc"
1099      * will be treated as a normal data section, which will avoid the
1100      * segfault.  However, you won't be able to create an ELFNN binary
1101      * with a section named "oc" that needs relocations, but that's
1102      * the kind of ugly side-effects you get when detecting section
1103      * types based on their names...  In practice, this limitation is
1104      * unlikely to bite.
1105      */
1106     hdr->sh_type = SHT_PROGBITS;
1107
1108   if (sec->flags & SEC_SMALL_DATA)
1109     hdr->sh_flags |= SHF_IA_64_SHORT;
1110
1111   return true;
1112 }
1113
1114 /* The final processing done just before writing out an IA-64 ELF
1115    object file.  */
1116
1117 static void
1118 elfNN_ia64_final_write_processing (abfd, linker)
1119      bfd *abfd;
1120      boolean linker ATTRIBUTE_UNUSED;
1121 {
1122   Elf_Internal_Shdr *hdr;
1123   const char *sname;
1124   asection *text_sect, *s;
1125   size_t len;
1126
1127   for (s = abfd->sections; s; s = s->next)
1128     {
1129       hdr = &elf_section_data (s)->this_hdr;
1130       switch (hdr->sh_type)
1131         {
1132         case SHT_IA_64_UNWIND:
1133           /* See comments in gas/config/tc-ia64.c:dot_endp on why we
1134              have to do this.  */
1135           sname = bfd_get_section_name (abfd, s);
1136           len = sizeof (ELF_STRING_ia64_unwind) - 1;
1137           if (sname && strncmp (sname, ELF_STRING_ia64_unwind, len) == 0)
1138             {
1139               sname += len;
1140
1141               if (sname[0] == '\0')
1142                 /* .IA_64.unwind -> .text */
1143                 text_sect = bfd_get_section_by_name (abfd, ".text");
1144               else
1145                 /* .IA_64.unwindFOO -> FOO */
1146                 text_sect = bfd_get_section_by_name (abfd, sname);
1147             }
1148           else if (sname
1149                    && (len = sizeof (ELF_STRING_ia64_unwind_once) - 1,
1150                        strncmp (sname, ELF_STRING_ia64_unwind_once, len)) == 0)
1151             {
1152               /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.t.FOO */
1153               size_t len2 = sizeof (".gnu.linkonce.t.") - 1;
1154               char *once_name = bfd_malloc (len2 + strlen (sname + len) + 1);
1155
1156               if (once_name != NULL)
1157                 {
1158                   memcpy (once_name, ".gnu.linkonce.t.", len2);
1159                   strcpy (once_name + len2, sname + len);
1160                   text_sect = bfd_get_section_by_name (abfd, once_name);
1161                   free (once_name);
1162                 }
1163               else
1164                 /* Should only happen if we run out of memory, in
1165                    which case we're probably toast anyway.  Try to
1166                    cope by finding the section the slow way.  */
1167                 for (text_sect = abfd->sections;
1168                      text_sect != NULL;
1169                      text_sect = text_sect->next)
1170                   {
1171                     if (strncmp (bfd_section_name (abfd, text_sect),
1172                                  ".gnu.linkonce.t.", len2) == 0
1173                         && strcmp (bfd_section_name (abfd, text_sect) + len2,
1174                                    sname + len) == 0)
1175                       break;
1176                   }
1177             }
1178           else
1179             /* last resort: fall back on .text */
1180             text_sect = bfd_get_section_by_name (abfd, ".text");
1181
1182           if (text_sect)
1183             {
1184               /* The IA-64 processor-specific ABI requires setting
1185                  sh_link to the unwind section, whereas HP-UX requires
1186                  sh_info to do so.  For maximum compatibility, we'll
1187                  set both for now... */
1188               hdr->sh_link = elf_section_data (text_sect)->this_idx;
1189               hdr->sh_info = elf_section_data (text_sect)->this_idx;
1190             }
1191           break;
1192         }
1193     }
1194 }
1195
1196 /* Hook called by the linker routine which adds symbols from an object
1197    file.  We use it to put .comm items in .sbss, and not .bss.  */
1198
1199 static boolean
1200 elfNN_ia64_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1201      bfd *abfd;
1202      struct bfd_link_info *info;
1203      const Elf_Internal_Sym *sym;
1204      const char **namep ATTRIBUTE_UNUSED;
1205      flagword *flagsp ATTRIBUTE_UNUSED;
1206      asection **secp;
1207      bfd_vma *valp;
1208 {
1209   if (sym->st_shndx == SHN_COMMON
1210       && !info->relocateable
1211       && sym->st_size <= elf_gp_size (abfd))
1212     {
1213       /* Common symbols less than or equal to -G nn bytes are
1214          automatically put into .sbss.  */
1215
1216       asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1217
1218       if (scomm == NULL)
1219         {
1220           scomm = bfd_make_section (abfd, ".scommon");
1221           if (scomm == NULL
1222               || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
1223                                                        | SEC_IS_COMMON
1224                                                        | SEC_LINKER_CREATED)))
1225             return false;
1226         }
1227
1228       *secp = scomm;
1229       *valp = sym->st_size;
1230     }
1231
1232   return true;
1233 }
1234
1235 static boolean
1236 elfNN_ia64_aix_vec (const bfd_target *vec)
1237 {
1238   extern const bfd_target bfd_elfNN_ia64_aix_little_vec;
1239   extern const bfd_target bfd_elfNN_ia64_aix_big_vec;
1240
1241   return (/**/vec == & bfd_elfNN_ia64_aix_little_vec
1242           ||  vec == & bfd_elfNN_ia64_aix_big_vec);
1243 }
1244
1245 /* Hook called by the linker routine which adds symbols from an object
1246    file.  We use it to handle OS-specific symbols.  */
1247
1248 static boolean
1249 elfNN_ia64_aix_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1250      bfd *abfd;
1251      struct bfd_link_info *info;
1252      const Elf_Internal_Sym *sym;
1253      const char **namep;
1254      flagword *flagsp;
1255      asection **secp;
1256      bfd_vma *valp;
1257 {
1258   if (strcmp (*namep, "__GLOB_DATA_PTR") == 0)
1259     {
1260       /* Define __GLOB_DATA_PTR when it is encountered.  This is expected to
1261          be a linker-defined symbol by the Aix C runtime startup code. IBM sez
1262          no one else should use it b/c it is undocumented.  */
1263       struct elf_link_hash_entry *h;
1264
1265       h = elf_link_hash_lookup (elf_hash_table (info), *namep,
1266                                 false, false, false);
1267       if (h == NULL)
1268         {
1269           struct elf_backend_data *bed;
1270           struct elfNN_ia64_link_hash_table *ia64_info;
1271
1272           bed = get_elf_backend_data (abfd);
1273           ia64_info = elfNN_ia64_hash_table (info);
1274
1275           if (!(_bfd_generic_link_add_one_symbol
1276                 (info, abfd, *namep, BSF_GLOBAL,
1277                  bfd_get_section_by_name (abfd, ".bss"),
1278                  bed->got_symbol_offset, (const char *) NULL, false,
1279                  bed->collect, (struct bfd_link_hash_entry **) &h)))
1280             return false;
1281
1282           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1283           h->type = STT_OBJECT;
1284
1285           if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1286             return false;
1287         }
1288
1289       return true;
1290     }
1291   else if (sym->st_shndx == SHN_LOOS)
1292     {
1293       unsigned int i;
1294
1295       /* SHN_AIX_SYSCALL: Treat this as any other symbol.  The special symbol
1296          is only relevant when compiling code for extended system calls.
1297          Replace the "special" section with .text, if possible.
1298          Note that these symbols are always assumed to be in .text. */
1299       for (i = 1; i < elf_numsections (abfd); i++)
1300         {
1301           asection * sec = bfd_section_from_elf_index (abfd, i);
1302
1303           if (sec && strcmp (sec->name, ".text") == 0)
1304             {
1305               *secp = sec;
1306               break;
1307             }
1308         }
1309
1310       if (*secp == NULL)
1311         *secp = bfd_abs_section_ptr;
1312
1313       *valp = sym->st_size;
1314
1315       return true;
1316     }
1317   else
1318     {
1319       return elfNN_ia64_add_symbol_hook (abfd, info, sym,
1320                                          namep, flagsp, secp, valp);
1321     }
1322 }
1323
1324 boolean
1325 elfNN_ia64_aix_link_add_symbols (abfd, info)
1326      bfd *abfd;
1327      struct bfd_link_info *info;
1328 {
1329   /* Make sure dynamic sections are always created.  */
1330   if (! elf_hash_table (info)->dynamic_sections_created
1331       && abfd->xvec == info->hash->creator)
1332     {
1333       if (! bfd_elfNN_link_create_dynamic_sections (abfd, info))
1334         return false;
1335     }
1336
1337   /* Now do the standard call.  */
1338   return bfd_elfNN_bfd_link_add_symbols (abfd, info);
1339 }
1340
1341 /* Return the number of additional phdrs we will need.  */
1342
1343 static int
1344 elfNN_ia64_additional_program_headers (abfd)
1345      bfd *abfd;
1346 {
1347   asection *s;
1348   int ret = 0;
1349
1350   /* See if we need a PT_IA_64_ARCHEXT segment.  */
1351   s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1352   if (s && (s->flags & SEC_LOAD))
1353     ++ret;
1354
1355   /* Count how many PT_IA_64_UNWIND segments we need.  */
1356   for (s = abfd->sections; s; s = s->next)
1357     if (is_unwind_section_name (abfd, s->name) && (s->flags & SEC_LOAD))
1358       ++ret;
1359
1360   return ret;
1361 }
1362
1363 static boolean
1364 elfNN_ia64_modify_segment_map (abfd)
1365      bfd *abfd;
1366 {
1367   struct elf_segment_map *m, **pm;
1368   Elf_Internal_Shdr *hdr;
1369   asection *s;
1370   boolean unwind_found;
1371   asection *unwind_sec;
1372
1373   /* If we need a PT_IA_64_ARCHEXT segment, it must come before
1374      all PT_LOAD segments.  */
1375   s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1376   if (s && (s->flags & SEC_LOAD))
1377     {
1378       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1379         if (m->p_type == PT_IA_64_ARCHEXT)
1380           break;
1381       if (m == NULL)
1382         {
1383           m = ((struct elf_segment_map *)
1384                bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1385           if (m == NULL)
1386             return false;
1387
1388           m->p_type = PT_IA_64_ARCHEXT;
1389           m->count = 1;
1390           m->sections[0] = s;
1391
1392           /* We want to put it after the PHDR and INTERP segments.  */
1393           pm = &elf_tdata (abfd)->segment_map;
1394           while (*pm != NULL
1395                  && ((*pm)->p_type == PT_PHDR
1396                      || (*pm)->p_type == PT_INTERP))
1397             pm = &(*pm)->next;
1398
1399           m->next = *pm;
1400           *pm = m;
1401         }
1402     }
1403
1404   /* Install PT_IA_64_UNWIND segments, if needed.  */
1405   for (s = abfd->sections; s; s = s->next)
1406     {
1407       hdr = &elf_section_data (s)->this_hdr;
1408       if (hdr->sh_type != SHT_IA_64_UNWIND)
1409         continue;
1410
1411       if (s && (s->flags & SEC_LOAD))
1412         {
1413           for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1414             if (m->p_type == PT_IA_64_UNWIND)
1415               {
1416                 /* Look through all sections in the unwind segment
1417                    for a match since there may be multiple sections
1418                    to a segment.  */
1419
1420                 unwind_sec = m->sections[0];
1421                 unwind_found = false;
1422                 while (unwind_sec != NULL && !unwind_found)
1423                   {
1424                     if (unwind_sec == s)
1425                       unwind_found = true;
1426                     else
1427                       unwind_sec = unwind_sec -> next;
1428                   }
1429                 if (unwind_found)
1430                   break;
1431               }
1432
1433           if (m == NULL)
1434             {
1435               m = ((struct elf_segment_map *)
1436                    bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1437               if (m == NULL)
1438                 return false;
1439
1440               m->p_type = PT_IA_64_UNWIND;
1441               m->count = 1;
1442               m->sections[0] = s;
1443               m->next = NULL;
1444
1445               /* We want to put it last.  */
1446               pm = &elf_tdata (abfd)->segment_map;
1447               while (*pm != NULL)
1448                 pm = &(*pm)->next;
1449               *pm = m;
1450             }
1451         }
1452     }
1453
1454   /* Turn on PF_IA_64_NORECOV if needed.  This involves traversing all of
1455      the input sections for each output section in the segment and testing
1456      for SHF_IA_64_NORECOV on each.  */
1457   for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1458     if (m->p_type == PT_LOAD)
1459       {
1460         int i;
1461         for (i = m->count - 1; i >= 0; --i)
1462           {
1463             struct bfd_link_order *order = m->sections[i]->link_order_head;
1464             while (order)
1465               {
1466                 if (order->type == bfd_indirect_link_order)
1467                   {
1468                     asection *is = order->u.indirect.section;
1469                     bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
1470                     if (flags & SHF_IA_64_NORECOV)
1471                       {
1472                         m->p_flags |= PF_IA_64_NORECOV;
1473                         goto found;
1474                       }
1475                   }
1476                 order = order->next;
1477               }
1478           }
1479       found:;
1480       }
1481
1482   return true;
1483 }
1484
1485 /* According to the Tahoe assembler spec, all labels starting with a
1486    '.' are local.  */
1487
1488 static boolean
1489 elfNN_ia64_is_local_label_name (abfd, name)
1490      bfd *abfd ATTRIBUTE_UNUSED;
1491      const char *name;
1492 {
1493   return name[0] == '.';
1494 }
1495
1496 /* Should we do dynamic things to this symbol?  */
1497
1498 static boolean
1499 elfNN_ia64_dynamic_symbol_p (h, info)
1500      struct elf_link_hash_entry *h;
1501      struct bfd_link_info *info;
1502 {
1503   if (h == NULL)
1504     return false;
1505
1506   while (h->root.type == bfd_link_hash_indirect
1507          || h->root.type == bfd_link_hash_warning)
1508     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1509
1510   if (h->dynindx == -1)
1511     return false;
1512   switch (ELF_ST_VISIBILITY (h->other))
1513     {
1514     case STV_INTERNAL:
1515     case STV_HIDDEN:
1516       return false;
1517     }
1518
1519   if (h->root.type == bfd_link_hash_undefweak
1520       || h->root.type == bfd_link_hash_defweak)
1521     return true;
1522
1523   if ((info->shared && (!info->symbolic || info->allow_shlib_undefined))
1524       || ((h->elf_link_hash_flags
1525            & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))
1526           == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
1527     return true;
1528
1529   return false;
1530 }
1531 \f
1532 static boolean
1533 elfNN_ia64_local_hash_table_init (ht, abfd, new)
1534      struct elfNN_ia64_local_hash_table *ht;
1535      bfd *abfd ATTRIBUTE_UNUSED;
1536      new_hash_entry_func new;
1537 {
1538   memset (ht, 0, sizeof (*ht));
1539   return bfd_hash_table_init (&ht->root, new);
1540 }
1541
1542 static struct bfd_hash_entry*
1543 elfNN_ia64_new_loc_hash_entry (entry, table, string)
1544      struct bfd_hash_entry *entry;
1545      struct bfd_hash_table *table;
1546      const char *string;
1547 {
1548   struct elfNN_ia64_local_hash_entry *ret;
1549   ret = (struct elfNN_ia64_local_hash_entry *) entry;
1550
1551   /* Allocate the structure if it has not already been allocated by a
1552      subclass.  */
1553   if (!ret)
1554     ret = bfd_hash_allocate (table, sizeof (*ret));
1555
1556   if (!ret)
1557     return 0;
1558
1559   /* Initialize our local data.  All zeros, and definitely easier
1560      than setting a handful of bit fields.  */
1561   memset (ret, 0, sizeof (*ret));
1562
1563   /* Call the allocation method of the superclass.  */
1564   ret = ((struct elfNN_ia64_local_hash_entry *)
1565          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
1566
1567   return (struct bfd_hash_entry *) ret;
1568 }
1569
1570 static struct bfd_hash_entry*
1571 elfNN_ia64_new_elf_hash_entry (entry, table, string)
1572      struct bfd_hash_entry *entry;
1573      struct bfd_hash_table *table;
1574      const char *string;
1575 {
1576   struct elfNN_ia64_link_hash_entry *ret;
1577   ret = (struct elfNN_ia64_link_hash_entry *) entry;
1578
1579   /* Allocate the structure if it has not already been allocated by a
1580      subclass.  */
1581   if (!ret)
1582     ret = bfd_hash_allocate (table, sizeof (*ret));
1583
1584   if (!ret)
1585     return 0;
1586
1587   /* Initialize our local data.  All zeros, and definitely easier
1588      than setting a handful of bit fields.  */
1589   memset (ret, 0, sizeof (*ret));
1590
1591   /* Call the allocation method of the superclass.  */
1592   ret = ((struct elfNN_ia64_link_hash_entry *)
1593          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1594                                      table, string));
1595
1596   return (struct bfd_hash_entry *) ret;
1597 }
1598
1599 static void
1600 elfNN_ia64_hash_copy_indirect (xdir, xind)
1601      struct elf_link_hash_entry *xdir, *xind;
1602 {
1603   struct elfNN_ia64_link_hash_entry *dir, *ind;
1604
1605   dir = (struct elfNN_ia64_link_hash_entry *) xdir;
1606   ind = (struct elfNN_ia64_link_hash_entry *) xind;
1607
1608   /* Copy down any references that we may have already seen to the
1609      symbol which just became indirect.  */
1610
1611   dir->root.elf_link_hash_flags |=
1612     (ind->root.elf_link_hash_flags
1613      & (ELF_LINK_HASH_REF_DYNAMIC
1614         | ELF_LINK_HASH_REF_REGULAR
1615         | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
1616
1617   if (ind->root.root.type != bfd_link_hash_indirect)
1618     return;
1619
1620   /* Copy over the got and plt data.  This would have been done
1621      by check_relocs.  */
1622
1623   if (dir->info == NULL)
1624     {
1625       struct elfNN_ia64_dyn_sym_info *dyn_i;
1626
1627       dir->info = dyn_i = ind->info;
1628       ind->info = NULL;
1629
1630       /* Fix up the dyn_sym_info pointers to the global symbol.  */
1631       for (; dyn_i; dyn_i = dyn_i->next)
1632         dyn_i->h = &dir->root;
1633     }
1634   BFD_ASSERT (ind->info == NULL);
1635
1636   /* Copy over the dynindx.  */
1637
1638   if (dir->root.dynindx == -1)
1639     {
1640       dir->root.dynindx = ind->root.dynindx;
1641       dir->root.dynstr_index = ind->root.dynstr_index;
1642       ind->root.dynindx = -1;
1643       ind->root.dynstr_index = 0;
1644     }
1645   BFD_ASSERT (ind->root.dynindx == -1);
1646 }
1647
1648 static void
1649 elfNN_ia64_hash_hide_symbol (info, xh, force_local)
1650      struct bfd_link_info *info;
1651      struct elf_link_hash_entry *xh;
1652      boolean force_local;
1653 {
1654   struct elfNN_ia64_link_hash_entry *h;
1655   struct elfNN_ia64_dyn_sym_info *dyn_i;
1656
1657   h = (struct elfNN_ia64_link_hash_entry *)xh;
1658
1659   _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
1660
1661   for (dyn_i = h->info; dyn_i; dyn_i = dyn_i->next)
1662     dyn_i->want_plt2 = 0;
1663 }
1664
1665 /* Create the derived linker hash table.  The IA-64 ELF port uses this
1666    derived hash table to keep information specific to the IA-64 ElF
1667    linker (without using static variables).  */
1668
1669 static struct bfd_link_hash_table*
1670 elfNN_ia64_hash_table_create (abfd)
1671      bfd *abfd;
1672 {
1673   struct elfNN_ia64_link_hash_table *ret;
1674
1675   ret = bfd_zalloc (abfd, (bfd_size_type) sizeof (*ret));
1676   if (!ret)
1677     return 0;
1678   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
1679                                       elfNN_ia64_new_elf_hash_entry))
1680     {
1681       bfd_release (abfd, ret);
1682       return 0;
1683     }
1684
1685   if (!elfNN_ia64_local_hash_table_init (&ret->loc_hash_table, abfd,
1686                                          elfNN_ia64_new_loc_hash_entry))
1687     return 0;
1688   return &ret->root.root;
1689 }
1690
1691 /* Look up an entry in a Alpha ELF linker hash table.  */
1692
1693 static INLINE struct elfNN_ia64_local_hash_entry *
1694 elfNN_ia64_local_hash_lookup(table, string, create, copy)
1695      struct elfNN_ia64_local_hash_table *table;
1696      const char *string;
1697      boolean create, copy;
1698 {
1699   return ((struct elfNN_ia64_local_hash_entry *)
1700           bfd_hash_lookup (&table->root, string, create, copy));
1701 }
1702
1703 /* Traverse both local and global hash tables.  */
1704
1705 struct elfNN_ia64_dyn_sym_traverse_data
1706 {
1707   boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
1708   PTR data;
1709 };
1710
1711 static boolean
1712 elfNN_ia64_global_dyn_sym_thunk (xentry, xdata)
1713      struct bfd_hash_entry *xentry;
1714      PTR xdata;
1715 {
1716   struct elfNN_ia64_link_hash_entry *entry
1717     = (struct elfNN_ia64_link_hash_entry *) xentry;
1718   struct elfNN_ia64_dyn_sym_traverse_data *data
1719     = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1720   struct elfNN_ia64_dyn_sym_info *dyn_i;
1721
1722   for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1723     if (! (*data->func) (dyn_i, data->data))
1724       return false;
1725   return true;
1726 }
1727
1728 static boolean
1729 elfNN_ia64_local_dyn_sym_thunk (xentry, xdata)
1730      struct bfd_hash_entry *xentry;
1731      PTR xdata;
1732 {
1733   struct elfNN_ia64_local_hash_entry *entry
1734     = (struct elfNN_ia64_local_hash_entry *) xentry;
1735   struct elfNN_ia64_dyn_sym_traverse_data *data
1736     = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1737   struct elfNN_ia64_dyn_sym_info *dyn_i;
1738
1739   for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1740     if (! (*data->func) (dyn_i, data->data))
1741       return false;
1742   return true;
1743 }
1744
1745 static void
1746 elfNN_ia64_dyn_sym_traverse (ia64_info, func, data)
1747      struct elfNN_ia64_link_hash_table *ia64_info;
1748      boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
1749      PTR data;
1750 {
1751   struct elfNN_ia64_dyn_sym_traverse_data xdata;
1752
1753   xdata.func = func;
1754   xdata.data = data;
1755
1756   elf_link_hash_traverse (&ia64_info->root,
1757                           elfNN_ia64_global_dyn_sym_thunk, &xdata);
1758   bfd_hash_traverse (&ia64_info->loc_hash_table.root,
1759                      elfNN_ia64_local_dyn_sym_thunk, &xdata);
1760 }
1761 \f
1762 static boolean
1763 elfNN_ia64_create_dynamic_sections (abfd, info)
1764      bfd *abfd;
1765      struct bfd_link_info *info;
1766 {
1767   struct elfNN_ia64_link_hash_table *ia64_info;
1768   asection *s;
1769
1770   if (! _bfd_elf_create_dynamic_sections (abfd, info))
1771     return false;
1772
1773   ia64_info = elfNN_ia64_hash_table (info);
1774
1775   ia64_info->plt_sec = bfd_get_section_by_name (abfd, ".plt");
1776   ia64_info->got_sec = bfd_get_section_by_name (abfd, ".got");
1777
1778   {
1779     flagword flags = bfd_get_section_flags (abfd, ia64_info->got_sec);
1780     bfd_set_section_flags (abfd, ia64_info->got_sec, SEC_SMALL_DATA | flags);
1781   }
1782
1783   if (!get_pltoff (abfd, info, ia64_info))
1784     return false;
1785
1786   s = bfd_make_section(abfd, ".rela.IA_64.pltoff");
1787   if (s == NULL
1788       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1789                                            | SEC_HAS_CONTENTS
1790                                            | SEC_IN_MEMORY
1791                                            | SEC_LINKER_CREATED
1792                                            | SEC_READONLY))
1793       || !bfd_set_section_alignment (abfd, s, 3))
1794     return false;
1795   ia64_info->rel_pltoff_sec = s;
1796
1797   s = bfd_make_section(abfd, ".rela.got");
1798   if (s == NULL
1799       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1800                                            | SEC_HAS_CONTENTS
1801                                            | SEC_IN_MEMORY
1802                                            | SEC_LINKER_CREATED
1803                                            | SEC_READONLY))
1804       || !bfd_set_section_alignment (abfd, s, 3))
1805     return false;
1806   ia64_info->rel_got_sec = s;
1807
1808   return true;
1809 }
1810
1811 /* Find and/or create a hash entry for local symbol.  */
1812 static struct elfNN_ia64_local_hash_entry *
1813 get_local_sym_hash (ia64_info, abfd, rel, create)
1814      struct elfNN_ia64_link_hash_table *ia64_info;
1815      bfd *abfd;
1816      const Elf_Internal_Rela *rel;
1817      boolean create;
1818 {
1819   char *addr_name;
1820   size_t len;
1821   struct elfNN_ia64_local_hash_entry *ret;
1822
1823   /* Construct a string for use in the elfNN_ia64_local_hash_table.
1824      name describes what was once anonymous memory.  */
1825
1826   len = sizeof (void*)*2 + 1 + sizeof (bfd_vma)*4 + 1 + 1;
1827   len += 10;    /* %p slop */
1828
1829   addr_name = bfd_malloc (len);
1830   if (addr_name == NULL)
1831     return 0;
1832   sprintf (addr_name, "%p:%lx",
1833            (void *) abfd, (unsigned long) ELFNN_R_SYM (rel->r_info));
1834
1835   /* Collect the canonical entry data for this address.  */
1836   ret = elfNN_ia64_local_hash_lookup (&ia64_info->loc_hash_table,
1837                                       addr_name, create, create);
1838   free (addr_name);
1839   return ret;
1840 }
1841
1842 /* Find and/or create a descriptor for dynamic symbol info.  This will
1843    vary based on global or local symbol, and the addend to the reloc.  */
1844
1845 static struct elfNN_ia64_dyn_sym_info *
1846 get_dyn_sym_info (ia64_info, h, abfd, rel, create)
1847      struct elfNN_ia64_link_hash_table *ia64_info;
1848      struct elf_link_hash_entry *h;
1849      bfd *abfd;
1850      const Elf_Internal_Rela *rel;
1851      boolean create;
1852 {
1853   struct elfNN_ia64_dyn_sym_info **pp;
1854   struct elfNN_ia64_dyn_sym_info *dyn_i;
1855   bfd_vma addend = rel ? rel->r_addend : 0;
1856
1857   if (h)
1858     pp = &((struct elfNN_ia64_link_hash_entry *)h)->info;
1859   else
1860     {
1861       struct elfNN_ia64_local_hash_entry *loc_h;
1862
1863       loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
1864       BFD_ASSERT (loc_h);
1865
1866       pp = &loc_h->info;
1867     }
1868
1869   for (dyn_i = *pp; dyn_i && dyn_i->addend != addend; dyn_i = *pp)
1870     pp = &dyn_i->next;
1871
1872   if (dyn_i == NULL && create)
1873     {
1874       dyn_i = ((struct elfNN_ia64_dyn_sym_info *)
1875                bfd_zalloc (abfd, (bfd_size_type) sizeof *dyn_i));
1876       *pp = dyn_i;
1877       dyn_i->addend = addend;
1878     }
1879
1880   return dyn_i;
1881 }
1882
1883 static asection *
1884 get_got (abfd, info, ia64_info)
1885      bfd *abfd;
1886      struct bfd_link_info *info;
1887      struct elfNN_ia64_link_hash_table *ia64_info;
1888 {
1889   asection *got;
1890   bfd *dynobj;
1891
1892   got = ia64_info->got_sec;
1893   if (!got)
1894     {
1895       flagword flags;
1896
1897       dynobj = ia64_info->root.dynobj;
1898       if (!dynobj)
1899         ia64_info->root.dynobj = dynobj = abfd;
1900       if (!_bfd_elf_create_got_section (dynobj, info))
1901         return 0;
1902
1903       got = bfd_get_section_by_name (dynobj, ".got");
1904       BFD_ASSERT (got);
1905       ia64_info->got_sec = got;
1906
1907       flags = bfd_get_section_flags (abfd, got);
1908       bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags);
1909     }
1910
1911   return got;
1912 }
1913
1914 /* Create function descriptor section (.opd).  This section is called .opd
1915    because it contains "official prodecure descriptors".  The "official"
1916    refers to the fact that these descriptors are used when taking the address
1917    of a procedure, thus ensuring a unique address for each procedure.  */
1918
1919 static asection *
1920 get_fptr (abfd, info, ia64_info)
1921      bfd *abfd;
1922      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1923      struct elfNN_ia64_link_hash_table *ia64_info;
1924 {
1925   asection *fptr;
1926   bfd *dynobj;
1927
1928   fptr = ia64_info->fptr_sec;
1929   if (!fptr)
1930     {
1931       dynobj = ia64_info->root.dynobj;
1932       if (!dynobj)
1933         ia64_info->root.dynobj = dynobj = abfd;
1934
1935       fptr = bfd_make_section (dynobj, ".opd");
1936       if (!fptr
1937           || !bfd_set_section_flags (dynobj, fptr,
1938                                      (SEC_ALLOC
1939                                       | SEC_LOAD
1940                                       | SEC_HAS_CONTENTS
1941                                       | SEC_IN_MEMORY
1942                                       | SEC_READONLY
1943                                       | SEC_LINKER_CREATED))
1944           || !bfd_set_section_alignment (abfd, fptr, 4))
1945         {
1946           BFD_ASSERT (0);
1947           return NULL;
1948         }
1949
1950       ia64_info->fptr_sec = fptr;
1951     }
1952
1953   return fptr;
1954 }
1955
1956 static asection *
1957 get_pltoff (abfd, info, ia64_info)
1958      bfd *abfd;
1959      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1960      struct elfNN_ia64_link_hash_table *ia64_info;
1961 {
1962   asection *pltoff;
1963   bfd *dynobj;
1964
1965   pltoff = ia64_info->pltoff_sec;
1966   if (!pltoff)
1967     {
1968       dynobj = ia64_info->root.dynobj;
1969       if (!dynobj)
1970         ia64_info->root.dynobj = dynobj = abfd;
1971
1972       pltoff = bfd_make_section (dynobj, ELF_STRING_ia64_pltoff);
1973       if (!pltoff
1974           || !bfd_set_section_flags (dynobj, pltoff,
1975                                      (SEC_ALLOC
1976                                       | SEC_LOAD
1977                                       | SEC_HAS_CONTENTS
1978                                       | SEC_IN_MEMORY
1979                                       | SEC_SMALL_DATA
1980                                       | SEC_LINKER_CREATED))
1981           || !bfd_set_section_alignment (abfd, pltoff, 4))
1982         {
1983           BFD_ASSERT (0);
1984           return NULL;
1985         }
1986
1987       ia64_info->pltoff_sec = pltoff;
1988     }
1989
1990   return pltoff;
1991 }
1992
1993 static asection *
1994 get_reloc_section (abfd, ia64_info, sec, create)
1995      bfd *abfd;
1996      struct elfNN_ia64_link_hash_table *ia64_info;
1997      asection *sec;
1998      boolean create;
1999 {
2000   const char *srel_name;
2001   asection *srel;
2002   bfd *dynobj;
2003
2004   srel_name = (bfd_elf_string_from_elf_section
2005                (abfd, elf_elfheader(abfd)->e_shstrndx,
2006                 elf_section_data(sec)->rel_hdr.sh_name));
2007   if (srel_name == NULL)
2008     return NULL;
2009
2010   BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
2011                && strcmp (bfd_get_section_name (abfd, sec),
2012                           srel_name+5) == 0)
2013               || (strncmp (srel_name, ".rel", 4) == 0
2014                   && strcmp (bfd_get_section_name (abfd, sec),
2015                              srel_name+4) == 0));
2016
2017   dynobj = ia64_info->root.dynobj;
2018   if (!dynobj)
2019     ia64_info->root.dynobj = dynobj = abfd;
2020
2021   srel = bfd_get_section_by_name (dynobj, srel_name);
2022   if (srel == NULL && create)
2023     {
2024       srel = bfd_make_section (dynobj, srel_name);
2025       if (srel == NULL
2026           || !bfd_set_section_flags (dynobj, srel,
2027                                      (SEC_ALLOC
2028                                       | SEC_LOAD
2029                                       | SEC_HAS_CONTENTS
2030                                       | SEC_IN_MEMORY
2031                                       | SEC_LINKER_CREATED
2032                                       | SEC_READONLY))
2033           || !bfd_set_section_alignment (dynobj, srel, 3))
2034         return NULL;
2035     }
2036
2037   if (sec->flags & SEC_READONLY)
2038     ia64_info->reltext = 1;
2039
2040   return srel;
2041 }
2042
2043 static boolean
2044 count_dyn_reloc (abfd, dyn_i, srel, type)
2045      bfd *abfd;
2046      struct elfNN_ia64_dyn_sym_info *dyn_i;
2047      asection *srel;
2048      int type;
2049 {
2050   struct elfNN_ia64_dyn_reloc_entry *rent;
2051
2052   for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2053     if (rent->srel == srel && rent->type == type)
2054       break;
2055
2056   if (!rent)
2057     {
2058       rent = ((struct elfNN_ia64_dyn_reloc_entry *)
2059               bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
2060       if (!rent)
2061         return false;
2062
2063       rent->next = dyn_i->reloc_entries;
2064       rent->srel = srel;
2065       rent->type = type;
2066       rent->count = 0;
2067       dyn_i->reloc_entries = rent;
2068     }
2069   rent->count++;
2070
2071   return true;
2072 }
2073
2074 static boolean
2075 elfNN_ia64_check_relocs (abfd, info, sec, relocs)
2076      bfd *abfd;
2077      struct bfd_link_info *info;
2078      asection *sec;
2079      const Elf_Internal_Rela *relocs;
2080 {
2081   struct elfNN_ia64_link_hash_table *ia64_info;
2082   const Elf_Internal_Rela *relend;
2083   Elf_Internal_Shdr *symtab_hdr;
2084   const Elf_Internal_Rela *rel;
2085   asection *got, *fptr, *srel;
2086
2087   if (info->relocateable)
2088     return true;
2089
2090   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2091   ia64_info = elfNN_ia64_hash_table (info);
2092
2093   got = fptr = srel = NULL;
2094
2095   relend = relocs + sec->reloc_count;
2096   for (rel = relocs; rel < relend; ++rel)
2097     {
2098       enum {
2099         NEED_GOT = 1,
2100         NEED_FPTR = 2,
2101         NEED_PLTOFF = 4,
2102         NEED_MIN_PLT = 8,
2103         NEED_FULL_PLT = 16,
2104         NEED_DYNREL = 32,
2105         NEED_LTOFF_FPTR = 64,
2106       };
2107
2108       struct elf_link_hash_entry *h = NULL;
2109       unsigned long r_symndx = ELFNN_R_SYM (rel->r_info);
2110       struct elfNN_ia64_dyn_sym_info *dyn_i;
2111       int need_entry;
2112       boolean maybe_dynamic;
2113       int dynrel_type = R_IA64_NONE;
2114
2115       if (r_symndx >= symtab_hdr->sh_info)
2116         {
2117           /* We're dealing with a global symbol -- find its hash entry
2118              and mark it as being referenced.  */
2119           long indx = r_symndx - symtab_hdr->sh_info;
2120           h = elf_sym_hashes (abfd)[indx];
2121           while (h->root.type == bfd_link_hash_indirect
2122                  || h->root.type == bfd_link_hash_warning)
2123             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2124
2125           h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2126         }
2127
2128       /* We can only get preliminary data on whether a symbol is
2129          locally or externally defined, as not all of the input files
2130          have yet been processed.  Do something with what we know, as
2131          this may help reduce memory usage and processing time later.  */
2132       maybe_dynamic = false;
2133       if (h && ((info->shared
2134                       && (!info->symbolic || info->allow_shlib_undefined))
2135                 || ! (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2136                 || h->root.type == bfd_link_hash_defweak
2137                 || elfNN_ia64_aix_vec (abfd->xvec)))
2138         maybe_dynamic = true;
2139
2140       need_entry = 0;
2141       switch (ELFNN_R_TYPE (rel->r_info))
2142         {
2143         case R_IA64_TPREL22:
2144         case R_IA64_TPREL64MSB:
2145         case R_IA64_TPREL64LSB:
2146         case R_IA64_LTOFF_TP22:
2147           return false;
2148
2149         case R_IA64_LTOFF_FPTR22:
2150         case R_IA64_LTOFF_FPTR64I:
2151         case R_IA64_LTOFF_FPTR32MSB:
2152         case R_IA64_LTOFF_FPTR32LSB:
2153         case R_IA64_LTOFF_FPTR64MSB:
2154         case R_IA64_LTOFF_FPTR64LSB:
2155           need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2156           break;
2157
2158         case R_IA64_FPTR64I:
2159         case R_IA64_FPTR32MSB:
2160         case R_IA64_FPTR32LSB:
2161         case R_IA64_FPTR64MSB:
2162         case R_IA64_FPTR64LSB:
2163           if (info->shared || h || elfNN_ia64_aix_vec (abfd->xvec))
2164             need_entry = NEED_FPTR | NEED_DYNREL;
2165           else
2166             need_entry = NEED_FPTR;
2167           dynrel_type = R_IA64_FPTR64LSB;
2168           break;
2169
2170         case R_IA64_LTOFF22:
2171         case R_IA64_LTOFF22X:
2172         case R_IA64_LTOFF64I:
2173           need_entry = NEED_GOT;
2174           break;
2175
2176         case R_IA64_PLTOFF22:
2177         case R_IA64_PLTOFF64I:
2178         case R_IA64_PLTOFF64MSB:
2179         case R_IA64_PLTOFF64LSB:
2180           need_entry = NEED_PLTOFF;
2181           if (h)
2182             {
2183               if (maybe_dynamic)
2184                 need_entry |= NEED_MIN_PLT;
2185             }
2186           else
2187             {
2188               (*info->callbacks->warning)
2189                 (info, _("@pltoff reloc against local symbol"), 0,
2190                  abfd, 0, (bfd_vma) 0);
2191             }
2192           break;
2193
2194         case R_IA64_PCREL21B:
2195         case R_IA64_PCREL60B:
2196           /* Depending on where this symbol is defined, we may or may not
2197              need a full plt entry.  Only skip if we know we'll not need
2198              the entry -- static or symbolic, and the symbol definition
2199              has already been seen.  */
2200           if (maybe_dynamic && rel->r_addend == 0)
2201             need_entry = NEED_FULL_PLT;
2202           break;
2203
2204         case R_IA64_IMM14:
2205         case R_IA64_IMM22:
2206         case R_IA64_IMM64:
2207         case R_IA64_DIR32MSB:
2208         case R_IA64_DIR32LSB:
2209         case R_IA64_DIR64MSB:
2210         case R_IA64_DIR64LSB:
2211           /* Shared objects will always need at least a REL relocation.  */
2212           if (info->shared || maybe_dynamic
2213               || (elfNN_ia64_aix_vec (abfd->xvec)
2214                   && (!h || strcmp (h->root.root.string,
2215                                     "__GLOB_DATA_PTR") != 0)))
2216             need_entry = NEED_DYNREL;
2217           dynrel_type = R_IA64_DIR64LSB;
2218           break;
2219
2220         case R_IA64_IPLTMSB:
2221         case R_IA64_IPLTLSB:
2222           /* Shared objects will always need at least a REL relocation.  */
2223           if (info->shared || maybe_dynamic)
2224             need_entry = NEED_DYNREL;
2225           dynrel_type = R_IA64_IPLTLSB;
2226           break;
2227
2228         case R_IA64_PCREL22:
2229         case R_IA64_PCREL64I:
2230         case R_IA64_PCREL32MSB:
2231         case R_IA64_PCREL32LSB:
2232         case R_IA64_PCREL64MSB:
2233         case R_IA64_PCREL64LSB:
2234           if (maybe_dynamic)
2235             need_entry = NEED_DYNREL;
2236           dynrel_type = R_IA64_PCREL64LSB;
2237           break;
2238         }
2239
2240       if (!need_entry)
2241         continue;
2242
2243       if ((need_entry & NEED_FPTR) != 0
2244           && rel->r_addend)
2245         {
2246           (*info->callbacks->warning)
2247             (info, _("non-zero addend in @fptr reloc"), 0,
2248              abfd, 0, (bfd_vma) 0);
2249         }
2250
2251       dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, true);
2252
2253       /* Record whether or not this is a local symbol.  */
2254       dyn_i->h = h;
2255
2256       /* Create what's needed.  */
2257       if (need_entry & NEED_GOT)
2258         {
2259           if (!got)
2260             {
2261               got = get_got (abfd, info, ia64_info);
2262               if (!got)
2263                 return false;
2264             }
2265           dyn_i->want_got = 1;
2266         }
2267       if (need_entry & NEED_FPTR)
2268         {
2269           if (!fptr)
2270             {
2271               fptr = get_fptr (abfd, info, ia64_info);
2272               if (!fptr)
2273                 return false;
2274             }
2275
2276           /* FPTRs for shared libraries are allocated by the dynamic
2277              linker.  Make sure this local symbol will appear in the
2278              dynamic symbol table.  */
2279           if (!h && (info->shared
2280                      /* AIX also needs one */
2281                      || elfNN_ia64_aix_vec (abfd->xvec)))
2282             {
2283               if (! (_bfd_elfNN_link_record_local_dynamic_symbol
2284                      (info, abfd, (long) r_symndx)))
2285                 return false;
2286             }
2287
2288           dyn_i->want_fptr = 1;
2289         }
2290       if (need_entry & NEED_LTOFF_FPTR)
2291         dyn_i->want_ltoff_fptr = 1;
2292       if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
2293         {
2294           if (!ia64_info->root.dynobj)
2295             ia64_info->root.dynobj = abfd;
2296           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2297           dyn_i->want_plt = 1;
2298         }
2299       if (need_entry & NEED_FULL_PLT)
2300         dyn_i->want_plt2 = 1;
2301       if (need_entry & NEED_PLTOFF)
2302         dyn_i->want_pltoff = 1;
2303       if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
2304         {
2305           if (!srel)
2306             {
2307               srel = get_reloc_section (abfd, ia64_info, sec, true);
2308               if (!srel)
2309                 return false;
2310             }
2311           if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
2312             return false;
2313         }
2314     }
2315
2316   return true;
2317 }
2318
2319 struct elfNN_ia64_allocate_data
2320 {
2321   struct bfd_link_info *info;
2322   bfd_size_type ofs;
2323 };
2324
2325 /* For cleanliness, and potentially faster dynamic loading, allocate
2326    external GOT entries first.  */
2327
2328 static boolean
2329 allocate_global_data_got (dyn_i, data)
2330      struct elfNN_ia64_dyn_sym_info *dyn_i;
2331      PTR data;
2332 {
2333   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2334
2335   if (dyn_i->want_got
2336       && ! dyn_i->want_fptr
2337       && (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2338           || (elfNN_ia64_aix_vec (x->info->hash->creator)
2339               && (!dyn_i->h || strcmp (dyn_i->h->root.root.string,
2340                                        "__GLOB_DATA_PTR") != 0))))
2341      {
2342        dyn_i->got_offset = x->ofs;
2343        x->ofs += 8;
2344      }
2345   return true;
2346 }
2347
2348 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs.  */
2349
2350 static boolean
2351 allocate_global_fptr_got (dyn_i, data)
2352      struct elfNN_ia64_dyn_sym_info *dyn_i;
2353      PTR data;
2354 {
2355   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2356
2357   if (dyn_i->want_got
2358       && dyn_i->want_fptr
2359       && (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2360           || elfNN_ia64_aix_vec (x->info->hash->creator)))
2361     {
2362       dyn_i->got_offset = x->ofs;
2363       x->ofs += 8;
2364     }
2365   return true;
2366 }
2367
2368 /* Lastly, allocate all the GOT entries for local data.  */
2369
2370 static boolean
2371 allocate_local_got (dyn_i, data)
2372      struct elfNN_ia64_dyn_sym_info *dyn_i;
2373      PTR data;
2374 {
2375   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2376
2377   if (dyn_i->want_got
2378       && ! (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2379             || elfNN_ia64_aix_vec (x->info->hash->creator)))
2380     {
2381       dyn_i->got_offset = x->ofs;
2382       x->ofs += 8;
2383     }
2384   return true;
2385 }
2386
2387 /* Search for the index of a global symbol in it's defining object file.  */
2388
2389 static long
2390 global_sym_index (h)
2391      struct elf_link_hash_entry *h;
2392 {
2393   struct elf_link_hash_entry **p;
2394   bfd *obj;
2395
2396   BFD_ASSERT (h->root.type == bfd_link_hash_defined
2397               || h->root.type == bfd_link_hash_defweak);
2398
2399   obj = h->root.u.def.section->owner;
2400   for (p = elf_sym_hashes (obj); *p != h; ++p)
2401     continue;
2402
2403   return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
2404 }
2405
2406 /* Allocate function descriptors.  We can do these for every function
2407    in a main executable that is not exported.  */
2408
2409 static boolean
2410 allocate_fptr (dyn_i, data)
2411      struct elfNN_ia64_dyn_sym_info *dyn_i;
2412      PTR data;
2413 {
2414   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2415
2416   if (dyn_i->want_fptr)
2417     {
2418       struct elf_link_hash_entry *h = dyn_i->h;
2419
2420       if (h)
2421         while (h->root.type == bfd_link_hash_indirect
2422                || h->root.type == bfd_link_hash_warning)
2423           h = (struct elf_link_hash_entry *) h->root.u.i.link;
2424
2425       if (x->info->shared
2426           /* AIX needs an FPTR in this case. */
2427           || (elfNN_ia64_aix_vec (x->info->hash->creator)
2428               && (!h
2429                   || h->root.type == bfd_link_hash_defined
2430                   || h->root.type == bfd_link_hash_defweak)))
2431         {
2432           if (h && h->dynindx == -1)
2433             {
2434               BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
2435                           || (h->root.type == bfd_link_hash_defweak));
2436
2437               if (!_bfd_elfNN_link_record_local_dynamic_symbol
2438                     (x->info, h->root.u.def.section->owner,
2439                      global_sym_index (h)))
2440                 return false;
2441             }
2442
2443           dyn_i->want_fptr = 0;
2444         }
2445       else if (h == NULL || h->dynindx == -1)
2446         {
2447           dyn_i->fptr_offset = x->ofs;
2448           x->ofs += 16;
2449         }
2450       else
2451         dyn_i->want_fptr = 0;
2452     }
2453   return true;
2454 }
2455
2456 /* Allocate all the minimal PLT entries.  */
2457
2458 static boolean
2459 allocate_plt_entries (dyn_i, data)
2460      struct elfNN_ia64_dyn_sym_info *dyn_i;
2461      PTR data;
2462 {
2463   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2464
2465   if (dyn_i->want_plt)
2466     {
2467       struct elf_link_hash_entry *h = dyn_i->h;
2468
2469       if (h)
2470         while (h->root.type == bfd_link_hash_indirect
2471                || h->root.type == bfd_link_hash_warning)
2472           h = (struct elf_link_hash_entry *) h->root.u.i.link;
2473
2474       /* ??? Versioned symbols seem to lose ELF_LINK_HASH_NEEDS_PLT.  */
2475       if (elfNN_ia64_dynamic_symbol_p (h, x->info))
2476         {
2477           bfd_size_type offset = x->ofs;
2478           if (offset == 0)
2479             offset = PLT_HEADER_SIZE;
2480           dyn_i->plt_offset = offset;
2481           x->ofs = offset + PLT_MIN_ENTRY_SIZE;
2482
2483           dyn_i->want_pltoff = 1;
2484         }
2485       else
2486         {
2487           dyn_i->want_plt = 0;
2488           dyn_i->want_plt2 = 0;
2489         }
2490     }
2491   return true;
2492 }
2493
2494 /* Allocate all the full PLT entries.  */
2495
2496 static boolean
2497 allocate_plt2_entries (dyn_i, data)
2498      struct elfNN_ia64_dyn_sym_info *dyn_i;
2499      PTR data;
2500 {
2501   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2502
2503   if (dyn_i->want_plt2)
2504     {
2505       struct elf_link_hash_entry *h = dyn_i->h;
2506       bfd_size_type ofs = x->ofs;
2507
2508       dyn_i->plt2_offset = ofs;
2509       x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2510
2511       while (h->root.type == bfd_link_hash_indirect
2512              || h->root.type == bfd_link_hash_warning)
2513         h = (struct elf_link_hash_entry *) h->root.u.i.link;
2514       dyn_i->h->plt.offset = ofs;
2515     }
2516   return true;
2517 }
2518
2519 /* Allocate all the PLTOFF entries requested by relocations and
2520    plt entries.  We can't share space with allocated FPTR entries,
2521    because the latter are not necessarily addressable by the GP.
2522    ??? Relaxation might be able to determine that they are.  */
2523
2524 static boolean
2525 allocate_pltoff_entries (dyn_i, data)
2526      struct elfNN_ia64_dyn_sym_info *dyn_i;
2527      PTR data;
2528 {
2529   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2530
2531   if (dyn_i->want_pltoff)
2532     {
2533       dyn_i->pltoff_offset = x->ofs;
2534       x->ofs += 16;
2535     }
2536   return true;
2537 }
2538
2539 /* Allocate dynamic relocations for those symbols that turned out
2540    to be dynamic.  */
2541
2542 static boolean
2543 allocate_dynrel_entries (dyn_i, data)
2544      struct elfNN_ia64_dyn_sym_info *dyn_i;
2545      PTR data;
2546 {
2547   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2548   struct elfNN_ia64_link_hash_table *ia64_info;
2549   struct elfNN_ia64_dyn_reloc_entry *rent;
2550   boolean dynamic_symbol, shared;
2551
2552   ia64_info = elfNN_ia64_hash_table (x->info);
2553   dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2554     || (elfNN_ia64_aix_vec (x->info->hash->creator)
2555         /* Don't allocate an entry for __GLOB_DATA_PTR */
2556         && (!dyn_i->h || strcmp (dyn_i->h->root.root.string,
2557           "__GLOB_DATA_PTR") != 0));
2558   shared = x->info->shared;
2559
2560   /* Take care of the normal data relocations.  */
2561
2562   for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2563     {
2564       int count = rent->count;
2565
2566       switch (rent->type)
2567         {
2568         case R_IA64_FPTR64LSB:
2569           /* Allocate one iff !want_fptr, which by this point will
2570              be true only if we're actually allocating one statically
2571              in the main executable.  */
2572           if (dyn_i->want_fptr)
2573             continue;
2574           break;
2575         case R_IA64_PCREL64LSB:
2576           if (!dynamic_symbol)
2577             continue;
2578           break;
2579         case R_IA64_DIR64LSB:
2580           if (!dynamic_symbol && !shared)
2581             continue;
2582           break;
2583         case R_IA64_IPLTLSB:
2584           if (!dynamic_symbol && !shared)
2585             continue;
2586           /* Use two REL relocations for IPLT relocations
2587              against local symbols.  */
2588           if (!dynamic_symbol)
2589             count *= 2;
2590           break;
2591         default:
2592           abort ();
2593         }
2594       rent->srel->_raw_size += sizeof (ElfNN_External_Rela) * count;
2595     }
2596
2597   /* Take care of the GOT and PLT relocations.  */
2598
2599   if (((dynamic_symbol || shared) && dyn_i->want_got)
2600       || (dyn_i->want_ltoff_fptr && dyn_i->h && dyn_i->h->dynindx != -1))
2601     ia64_info->rel_got_sec->_raw_size += sizeof (ElfNN_External_Rela);
2602
2603   if (dyn_i->want_pltoff)
2604     {
2605       bfd_size_type t = 0;
2606
2607       /* Dynamic symbols get one IPLT relocation.  Local symbols in
2608          shared libraries get two REL relocations.  Local symbols in
2609          main applications get nothing.  */
2610       if (dynamic_symbol)
2611         t = sizeof (ElfNN_External_Rela);
2612       else if (shared)
2613         t = 2 * sizeof (ElfNN_External_Rela);
2614
2615       ia64_info->rel_pltoff_sec->_raw_size += t;
2616     }
2617
2618   return true;
2619 }
2620
2621 static boolean
2622 elfNN_ia64_adjust_dynamic_symbol (info, h)
2623      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2624      struct elf_link_hash_entry *h;
2625 {
2626   /* ??? Undefined symbols with PLT entries should be re-defined
2627      to be the PLT entry.  */
2628
2629   /* If this is a weak symbol, and there is a real definition, the
2630      processor independent code will have arranged for us to see the
2631      real definition first, and we can just use the same value.  */
2632   if (h->weakdef != NULL)
2633     {
2634       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2635                   || h->weakdef->root.type == bfd_link_hash_defweak);
2636       h->root.u.def.section = h->weakdef->root.u.def.section;
2637       h->root.u.def.value = h->weakdef->root.u.def.value;
2638       return true;
2639     }
2640
2641   /* If this is a reference to a symbol defined by a dynamic object which
2642      is not a function, we might allocate the symbol in our .dynbss section
2643      and allocate a COPY dynamic relocation.
2644
2645      But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2646      of hackery.  */
2647
2648   return true;
2649 }
2650
2651 static boolean
2652 elfNN_ia64_size_dynamic_sections (output_bfd, info)
2653      bfd *output_bfd;
2654      struct bfd_link_info *info;
2655 {
2656   struct elfNN_ia64_allocate_data data;
2657   struct elfNN_ia64_link_hash_table *ia64_info;
2658   asection *sec;
2659   bfd *dynobj;
2660   boolean relplt = false;
2661
2662   dynobj = elf_hash_table(info)->dynobj;
2663   ia64_info = elfNN_ia64_hash_table (info);
2664   BFD_ASSERT(dynobj != NULL);
2665   data.info = info;
2666
2667   /* Set the contents of the .interp section to the interpreter.  */
2668   if (ia64_info->root.dynamic_sections_created
2669       && !info->shared)
2670     {
2671       sec = bfd_get_section_by_name (dynobj, ".interp");
2672       BFD_ASSERT (sec != NULL);
2673       sec->contents = (bfd_byte *) DYNAMIC_INTERPRETER (output_bfd);
2674       sec->_raw_size = strlen (DYNAMIC_INTERPRETER (output_bfd)) + 1;
2675     }
2676
2677   /* Allocate the GOT entries.  */
2678
2679   if (ia64_info->got_sec)
2680     {
2681       data.ofs = 0;
2682       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2683       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2684       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
2685       ia64_info->got_sec->_raw_size = data.ofs;
2686     }
2687
2688   /* Allocate the FPTR entries.  */
2689
2690   if (ia64_info->fptr_sec)
2691     {
2692       data.ofs = 0;
2693       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
2694       ia64_info->fptr_sec->_raw_size = data.ofs;
2695     }
2696
2697   /* Now that we've seen all of the input files, we can decide which
2698      symbols need plt entries.  Allocate the minimal PLT entries first.
2699      We do this even though dynamic_sections_created may be false, because
2700      this has the side-effect of clearing want_plt and want_plt2.  */
2701
2702   data.ofs = 0;
2703   elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
2704
2705   ia64_info->minplt_entries = 0;
2706   if (data.ofs)
2707     {
2708       ia64_info->minplt_entries
2709         = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
2710     }
2711
2712   /* Align the pointer for the plt2 entries.  */
2713   data.ofs = (data.ofs + 31) & (bfd_vma) -32;
2714
2715   elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
2716   if (data.ofs != 0)
2717     {
2718       BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2719
2720       ia64_info->plt_sec->_raw_size = data.ofs;
2721
2722       /* If we've got a .plt, we need some extra memory for the dynamic
2723          linker.  We stuff these in .got.plt.  */
2724       sec = bfd_get_section_by_name (dynobj, ".got.plt");
2725       sec->_raw_size = 8 * PLT_RESERVED_WORDS;
2726     }
2727
2728   /* Allocate the PLTOFF entries.  */
2729
2730   if (ia64_info->pltoff_sec)
2731     {
2732       data.ofs = 0;
2733       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
2734       ia64_info->pltoff_sec->_raw_size = data.ofs;
2735     }
2736
2737   if (ia64_info->root.dynamic_sections_created)
2738     {
2739       /* Allocate space for the dynamic relocations that turned out to be
2740          required.  */
2741
2742       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
2743     }
2744
2745   /* We have now determined the sizes of the various dynamic sections.
2746      Allocate memory for them.  */
2747   for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2748     {
2749       boolean strip;
2750
2751       if (!(sec->flags & SEC_LINKER_CREATED))
2752         continue;
2753
2754       /* If we don't need this section, strip it from the output file.
2755          There were several sections primarily related to dynamic
2756          linking that must be create before the linker maps input
2757          sections to output sections.  The linker does that before
2758          bfd_elf_size_dynamic_sections is called, and it is that
2759          function which decides whether anything needs to go into
2760          these sections.  */
2761
2762       strip = (sec->_raw_size == 0);
2763
2764       if (sec == ia64_info->got_sec)
2765         strip = false;
2766       else if (sec == ia64_info->rel_got_sec)
2767         {
2768           if (strip)
2769             ia64_info->rel_got_sec = NULL;
2770           else
2771             /* We use the reloc_count field as a counter if we need to
2772                copy relocs into the output file.  */
2773             sec->reloc_count = 0;
2774         }
2775       else if (sec == ia64_info->fptr_sec)
2776         {
2777           if (strip)
2778             ia64_info->fptr_sec = NULL;
2779         }
2780       else if (sec == ia64_info->plt_sec)
2781         {
2782           if (strip)
2783             ia64_info->plt_sec = NULL;
2784         }
2785       else if (sec == ia64_info->pltoff_sec)
2786         {
2787           if (strip)
2788             ia64_info->pltoff_sec = NULL;
2789         }
2790       else if (sec == ia64_info->rel_pltoff_sec)
2791         {
2792           if (strip)
2793             ia64_info->rel_pltoff_sec = NULL;
2794           else
2795             {
2796               relplt = true;
2797               /* We use the reloc_count field as a counter if we need to
2798                  copy relocs into the output file.  */
2799               sec->reloc_count = 0;
2800             }
2801         }
2802       else
2803         {
2804           const char *name;
2805
2806           /* It's OK to base decisions on the section name, because none
2807              of the dynobj section names depend upon the input files.  */
2808           name = bfd_get_section_name (dynobj, sec);
2809
2810           if (strcmp (name, ".got.plt") == 0)
2811             strip = false;
2812           else if (strncmp (name, ".rel", 4) == 0)
2813             {
2814               if (!strip)
2815                 {
2816                   /* We use the reloc_count field as a counter if we need to
2817                      copy relocs into the output file.  */
2818                   sec->reloc_count = 0;
2819                 }
2820             }
2821           else
2822             continue;
2823         }
2824
2825       if (strip)
2826         _bfd_strip_section_from_output (info, sec);
2827       else
2828         {
2829           /* Allocate memory for the section contents.  */
2830           sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->_raw_size);
2831           if (sec->contents == NULL && sec->_raw_size != 0)
2832             return false;
2833         }
2834     }
2835
2836   if (elf_hash_table (info)->dynamic_sections_created)
2837     {
2838       /* Add some entries to the .dynamic section.  We fill in the values
2839          later (in finish_dynamic_sections) but we must add the entries now
2840          so that we get the correct size for the .dynamic section.  */
2841
2842       if (!info->shared)
2843         {
2844           /* The DT_DEBUG entry is filled in by the dynamic linker and used
2845              by the debugger.  */
2846 #define add_dynamic_entry(TAG, VAL) \
2847   bfd_elfNN_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2848
2849           if (!add_dynamic_entry (DT_DEBUG, 0))
2850             return false;
2851         }
2852
2853       if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
2854         return false;
2855       if (!add_dynamic_entry (DT_PLTGOT, 0))
2856         return false;
2857
2858       if (relplt)
2859         {
2860           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2861               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2862               || !add_dynamic_entry (DT_JMPREL, 0))
2863             return false;
2864         }
2865
2866       if (!add_dynamic_entry (DT_RELA, 0)
2867           || !add_dynamic_entry (DT_RELASZ, 0)
2868           || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
2869         return false;
2870
2871       if (ia64_info->reltext)
2872         {
2873           if (!add_dynamic_entry (DT_TEXTREL, 0))
2874             return false;
2875           info->flags |= DF_TEXTREL;
2876         }
2877     }
2878
2879   /* ??? Perhaps force __gp local.  */
2880
2881   return true;
2882 }
2883
2884 static bfd_reloc_status_type
2885 elfNN_ia64_install_value (abfd, hit_addr, v, r_type)
2886      bfd *abfd;
2887      bfd_byte *hit_addr;
2888      bfd_vma v;
2889      unsigned int r_type;
2890 {
2891   const struct ia64_operand *op;
2892   int bigendian = 0, shift = 0;
2893   bfd_vma t0, t1, insn, dword;
2894   enum ia64_opnd opnd;
2895   const char *err;
2896   size_t size = 8;
2897 #ifdef BFD_HOST_U_64_BIT
2898   BFD_HOST_U_64_BIT val = (BFD_HOST_U_64_BIT) v;
2899 #else
2900   bfd_vma val = v;
2901 #endif
2902
2903   opnd = IA64_OPND_NIL;
2904   switch (r_type)
2905     {
2906     case R_IA64_NONE:
2907     case R_IA64_LDXMOV:
2908       return bfd_reloc_ok;
2909
2910       /* Instruction relocations.  */
2911
2912     case R_IA64_IMM14:          opnd = IA64_OPND_IMM14; break;
2913
2914     case R_IA64_PCREL21F:       opnd = IA64_OPND_TGT25; break;
2915     case R_IA64_PCREL21M:       opnd = IA64_OPND_TGT25b; break;
2916     case R_IA64_PCREL60B:       opnd = IA64_OPND_TGT64; break;
2917     case R_IA64_PCREL21B:
2918     case R_IA64_PCREL21BI:
2919       opnd = IA64_OPND_TGT25c;
2920       break;
2921
2922     case R_IA64_IMM22:
2923     case R_IA64_GPREL22:
2924     case R_IA64_LTOFF22:
2925     case R_IA64_LTOFF22X:
2926     case R_IA64_PLTOFF22:
2927     case R_IA64_PCREL22:
2928     case R_IA64_LTOFF_FPTR22:
2929       opnd = IA64_OPND_IMM22;
2930       break;
2931
2932     case R_IA64_IMM64:
2933     case R_IA64_GPREL64I:
2934     case R_IA64_LTOFF64I:
2935     case R_IA64_PLTOFF64I:
2936     case R_IA64_PCREL64I:
2937     case R_IA64_FPTR64I:
2938     case R_IA64_LTOFF_FPTR64I:
2939       opnd = IA64_OPND_IMMU64;
2940       break;
2941
2942       /* Data relocations.  */
2943
2944     case R_IA64_DIR32MSB:
2945     case R_IA64_GPREL32MSB:
2946     case R_IA64_FPTR32MSB:
2947     case R_IA64_PCREL32MSB:
2948     case R_IA64_LTOFF_FPTR32MSB:
2949     case R_IA64_SEGREL32MSB:
2950     case R_IA64_SECREL32MSB:
2951     case R_IA64_LTV32MSB:
2952       size = 4; bigendian = 1;
2953       break;
2954
2955     case R_IA64_DIR32LSB:
2956     case R_IA64_GPREL32LSB:
2957     case R_IA64_FPTR32LSB:
2958     case R_IA64_PCREL32LSB:
2959     case R_IA64_LTOFF_FPTR32LSB:
2960     case R_IA64_SEGREL32LSB:
2961     case R_IA64_SECREL32LSB:
2962     case R_IA64_LTV32LSB:
2963       size = 4; bigendian = 0;
2964       break;
2965
2966     case R_IA64_DIR64MSB:
2967     case R_IA64_GPREL64MSB:
2968     case R_IA64_PLTOFF64MSB:
2969     case R_IA64_FPTR64MSB:
2970     case R_IA64_PCREL64MSB:
2971     case R_IA64_LTOFF_FPTR64MSB:
2972     case R_IA64_SEGREL64MSB:
2973     case R_IA64_SECREL64MSB:
2974     case R_IA64_LTV64MSB:
2975       size = 8; bigendian = 1;
2976       break;
2977
2978     case R_IA64_DIR64LSB:
2979     case R_IA64_GPREL64LSB:
2980     case R_IA64_PLTOFF64LSB:
2981     case R_IA64_FPTR64LSB:
2982     case R_IA64_PCREL64LSB:
2983     case R_IA64_LTOFF_FPTR64LSB:
2984     case R_IA64_SEGREL64LSB:
2985     case R_IA64_SECREL64LSB:
2986     case R_IA64_LTV64LSB:
2987       size = 8; bigendian = 0;
2988       break;
2989
2990       /* Unsupported / Dynamic relocations.  */
2991     default:
2992       return bfd_reloc_notsupported;
2993     }
2994
2995   switch (opnd)
2996     {
2997     case IA64_OPND_IMMU64:
2998       hit_addr -= (long) hit_addr & 0x3;
2999       t0 = bfd_get_64 (abfd, hit_addr);
3000       t1 = bfd_get_64 (abfd, hit_addr + 8);
3001
3002       /* tmpl/s: bits  0.. 5 in t0
3003          slot 0: bits  5..45 in t0
3004          slot 1: bits 46..63 in t0, bits 0..22 in t1
3005          slot 2: bits 23..63 in t1 */
3006
3007       /* First, clear the bits that form the 64 bit constant.  */
3008       t0 &= ~(0x3ffffLL << 46);
3009       t1 &= ~(0x7fffffLL
3010               | ((  (0x07fLL << 13) | (0x1ffLL << 27)
3011                     | (0x01fLL << 22) | (0x001LL << 21)
3012                     | (0x001LL << 36)) << 23));
3013
3014       t0 |= ((val >> 22) & 0x03ffffLL) << 46;           /* 18 lsbs of imm41 */
3015       t1 |= ((val >> 40) & 0x7fffffLL) <<  0;           /* 23 msbs of imm41 */
3016       t1 |= (  (((val >>  0) & 0x07f) << 13)            /* imm7b */
3017                | (((val >>  7) & 0x1ff) << 27)          /* imm9d */
3018                | (((val >> 16) & 0x01f) << 22)          /* imm5c */
3019                | (((val >> 21) & 0x001) << 21)          /* ic */
3020                | (((val >> 63) & 0x001) << 36)) << 23;  /* i */
3021
3022       bfd_put_64 (abfd, t0, hit_addr);
3023       bfd_put_64 (abfd, t1, hit_addr + 8);
3024       break;
3025
3026     case IA64_OPND_TGT64:
3027       hit_addr -= (long) hit_addr & 0x3;
3028       t0 = bfd_get_64 (abfd, hit_addr);
3029       t1 = bfd_get_64 (abfd, hit_addr + 8);
3030
3031       /* tmpl/s: bits  0.. 5 in t0
3032          slot 0: bits  5..45 in t0
3033          slot 1: bits 46..63 in t0, bits 0..22 in t1
3034          slot 2: bits 23..63 in t1 */
3035
3036       /* First, clear the bits that form the 64 bit constant.  */
3037       t0 &= ~(0x3ffffLL << 46);
3038       t1 &= ~(0x7fffffLL
3039               | ((1LL << 36 | 0xfffffLL << 13) << 23));
3040
3041       val >>= 4;
3042       t0 |= ((val >> 20) & 0xffffLL) << 2 << 46;        /* 16 lsbs of imm39 */
3043       t1 |= ((val >> 36) & 0x7fffffLL) << 0;            /* 23 msbs of imm39 */
3044       t1 |= ((((val >> 0) & 0xfffffLL) << 13)           /* imm20b */
3045               | (((val >> 59) & 0x1LL) << 36)) << 23;   /* i */
3046
3047       bfd_put_64 (abfd, t0, hit_addr);
3048       bfd_put_64 (abfd, t1, hit_addr + 8);
3049       break;
3050
3051     default:
3052       switch ((long) hit_addr & 0x3)
3053         {
3054         case 0: shift =  5; break;
3055         case 1: shift = 14; hit_addr += 3; break;
3056         case 2: shift = 23; hit_addr += 6; break;
3057         case 3: return bfd_reloc_notsupported; /* shouldn't happen...  */
3058         }
3059       dword = bfd_get_64 (abfd, hit_addr);
3060       insn = (dword >> shift) & 0x1ffffffffffLL;
3061
3062       op = elf64_ia64_operands + opnd;
3063       err = (*op->insert) (op, val, (ia64_insn *)& insn);
3064       if (err)
3065         return bfd_reloc_overflow;
3066
3067       dword &= ~(0x1ffffffffffLL << shift);
3068       dword |= (insn << shift);
3069       bfd_put_64 (abfd, dword, hit_addr);
3070       break;
3071
3072     case IA64_OPND_NIL:
3073       /* A data relocation.  */
3074       if (bigendian)
3075         if (size == 4)
3076           bfd_putb32 (val, hit_addr);
3077         else
3078           bfd_putb64 (val, hit_addr);
3079       else
3080         if (size == 4)
3081           bfd_putl32 (val, hit_addr);
3082         else
3083           bfd_putl64 (val, hit_addr);
3084       break;
3085     }
3086
3087   return bfd_reloc_ok;
3088 }
3089
3090 static void
3091 elfNN_ia64_install_dyn_reloc (abfd, info, sec, srel, offset, type,
3092                               dynindx, addend)
3093      bfd *abfd;
3094      struct bfd_link_info *info;
3095      asection *sec;
3096      asection *srel;
3097      bfd_vma offset;
3098      unsigned int type;
3099      long dynindx;
3100      bfd_vma addend;
3101 {
3102   Elf_Internal_Rela outrel;
3103
3104   offset += sec->output_section->vma + sec->output_offset;
3105
3106   BFD_ASSERT (dynindx != -1);
3107   outrel.r_info = ELFNN_R_INFO (dynindx, type);
3108   outrel.r_addend = addend;
3109   outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
3110   if (outrel.r_offset == (bfd_vma) -1)
3111     {
3112       /* Run for the hills.  We shouldn't be outputting a relocation
3113          for this.  So do what everyone else does and output a no-op.  */
3114       outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
3115       outrel.r_addend = 0;
3116       outrel.r_offset = 0;
3117     }
3118
3119   bfd_elfNN_swap_reloca_out (abfd, &outrel,
3120                              ((ElfNN_External_Rela *) srel->contents
3121                               + srel->reloc_count++));
3122   BFD_ASSERT (sizeof (ElfNN_External_Rela) * srel->reloc_count
3123               <= srel->_cooked_size);
3124 }
3125
3126 /* Store an entry for target address TARGET_ADDR in the linkage table
3127    and return the gp-relative address of the linkage table entry.  */
3128
3129 static bfd_vma
3130 set_got_entry (abfd, info, dyn_i, dynindx, addend, value, dyn_r_type)
3131      bfd *abfd;
3132      struct bfd_link_info *info;
3133      struct elfNN_ia64_dyn_sym_info *dyn_i;
3134      long dynindx;
3135      bfd_vma addend;
3136      bfd_vma value;
3137      unsigned int dyn_r_type;
3138 {
3139   struct elfNN_ia64_link_hash_table *ia64_info;
3140   asection *got_sec;
3141
3142   ia64_info = elfNN_ia64_hash_table (info);
3143   got_sec = ia64_info->got_sec;
3144
3145   BFD_ASSERT ((dyn_i->got_offset & 7) == 0);
3146
3147   if (! dyn_i->got_done)
3148     {
3149       dyn_i->got_done = true;
3150
3151       /* Store the target address in the linkage table entry.  */
3152       bfd_put_64 (abfd, value, got_sec->contents + dyn_i->got_offset);
3153
3154       /* Install a dynamic relocation if needed.  */
3155       if (info->shared
3156           || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info)
3157           || elfNN_ia64_aix_vec (abfd->xvec)
3158           || (dynindx != -1 && dyn_r_type == R_IA64_FPTR64LSB))
3159         {
3160           if (dynindx == -1)
3161             {
3162               dyn_r_type = R_IA64_REL64LSB;
3163               dynindx = 0;
3164               addend = value;
3165             }
3166
3167           if (bfd_big_endian (abfd))
3168             {
3169               switch (dyn_r_type)
3170                 {
3171                 case R_IA64_REL64LSB:
3172                   dyn_r_type = R_IA64_REL64MSB;
3173                   break;
3174                 case R_IA64_DIR64LSB:
3175                   dyn_r_type = R_IA64_DIR64MSB;
3176                   break;
3177                 case R_IA64_FPTR64LSB:
3178                   dyn_r_type = R_IA64_FPTR64MSB;
3179                   break;
3180                 default:
3181                   BFD_ASSERT (false);
3182                   break;
3183                 }
3184             }
3185
3186           elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
3187                                         ia64_info->rel_got_sec,
3188                                         dyn_i->got_offset, dyn_r_type,
3189                                         dynindx, addend);
3190         }
3191     }
3192
3193   /* Return the address of the linkage table entry.  */
3194   value = (got_sec->output_section->vma
3195            + got_sec->output_offset
3196            + dyn_i->got_offset);
3197
3198   return value;
3199 }
3200
3201 /* Fill in a function descriptor consisting of the function's code
3202    address and its global pointer.  Return the descriptor's address.  */
3203
3204 static bfd_vma
3205 set_fptr_entry (abfd, info, dyn_i, value)
3206      bfd *abfd;
3207      struct bfd_link_info *info;
3208      struct elfNN_ia64_dyn_sym_info *dyn_i;
3209      bfd_vma value;
3210 {
3211   struct elfNN_ia64_link_hash_table *ia64_info;
3212   asection *fptr_sec;
3213
3214   ia64_info = elfNN_ia64_hash_table (info);
3215   fptr_sec = ia64_info->fptr_sec;
3216
3217   if (!dyn_i->fptr_done)
3218     {
3219       dyn_i->fptr_done = 1;
3220
3221       /* Fill in the function descriptor.  */
3222       bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
3223       bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
3224                   fptr_sec->contents + dyn_i->fptr_offset + 8);
3225     }
3226
3227   /* Return the descriptor's address.  */
3228   value = (fptr_sec->output_section->vma
3229            + fptr_sec->output_offset
3230            + dyn_i->fptr_offset);
3231
3232   return value;
3233 }
3234
3235 /* Fill in a PLTOFF entry consisting of the function's code address
3236    and its global pointer.  Return the descriptor's address.  */
3237
3238 static bfd_vma
3239 set_pltoff_entry (abfd, info, dyn_i, value, is_plt)
3240      bfd *abfd;
3241      struct bfd_link_info *info;
3242      struct elfNN_ia64_dyn_sym_info *dyn_i;
3243      bfd_vma value;
3244      boolean is_plt;
3245 {
3246   struct elfNN_ia64_link_hash_table *ia64_info;
3247   asection *pltoff_sec;
3248
3249   ia64_info = elfNN_ia64_hash_table (info);
3250   pltoff_sec = ia64_info->pltoff_sec;
3251
3252   /* Don't do anything if this symbol uses a real PLT entry.  In
3253      that case, we'll fill this in during finish_dynamic_symbol.  */
3254   if ((! dyn_i->want_plt || is_plt)
3255       && !dyn_i->pltoff_done)
3256     {
3257       bfd_vma gp = _bfd_get_gp_value (abfd);
3258
3259       /* Fill in the function descriptor.  */
3260       bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
3261       bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
3262
3263       /* Install dynamic relocations if needed.  */
3264       if (!is_plt && info->shared)
3265         {
3266           unsigned int dyn_r_type;
3267
3268           if (bfd_big_endian (abfd))
3269             dyn_r_type = R_IA64_REL64MSB;
3270           else
3271             dyn_r_type = R_IA64_REL64LSB;
3272
3273           elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
3274                                         ia64_info->rel_pltoff_sec,
3275                                         dyn_i->pltoff_offset,
3276                                         dyn_r_type, 0, value);
3277           elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
3278                                         ia64_info->rel_pltoff_sec,
3279                                         dyn_i->pltoff_offset + 8,
3280                                         dyn_r_type, 0, gp);
3281         }
3282
3283       dyn_i->pltoff_done = 1;
3284     }
3285
3286   /* Return the descriptor's address.  */
3287   value = (pltoff_sec->output_section->vma
3288            + pltoff_sec->output_offset
3289            + dyn_i->pltoff_offset);
3290
3291   return value;
3292 }
3293
3294 /* Called through qsort to sort the .IA_64.unwind section during a
3295    non-relocatable link.  Set elfNN_ia64_unwind_entry_compare_bfd
3296    to the output bfd so we can do proper endianness frobbing.  */
3297
3298 static bfd *elfNN_ia64_unwind_entry_compare_bfd;
3299
3300 static int
3301 elfNN_ia64_unwind_entry_compare (a, b)
3302      const PTR a;
3303      const PTR b;
3304 {
3305   bfd_vma av, bv;
3306
3307   av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
3308   bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
3309
3310   return (av < bv ? -1 : av > bv ? 1 : 0);
3311 }
3312
3313 static boolean
3314 elfNN_ia64_final_link (abfd, info)
3315      bfd *abfd;
3316      struct bfd_link_info *info;
3317 {
3318   struct elfNN_ia64_link_hash_table *ia64_info;
3319   asection *unwind_output_sec;
3320
3321   ia64_info = elfNN_ia64_hash_table (info);
3322
3323   /* Make sure we've got ourselves a nice fat __gp value.  */
3324   if (!info->relocateable)
3325     {
3326       bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3327       bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3328       struct elf_link_hash_entry *gp;
3329       bfd_vma gp_val;
3330       asection *os;
3331
3332       /* Find the min and max vma of all sections marked short.  Also
3333          collect min and max vma of any type, for use in selecting a
3334          nice gp.  */
3335       for (os = abfd->sections; os ; os = os->next)
3336         {
3337           bfd_vma lo, hi;
3338
3339           if ((os->flags & SEC_ALLOC) == 0)
3340             continue;
3341
3342           lo = os->vma;
3343           hi = os->vma + os->_raw_size;
3344           if (hi < lo)
3345             hi = (bfd_vma) -1;
3346
3347           if (min_vma > lo)
3348             min_vma = lo;
3349           if (max_vma < hi)
3350             max_vma = hi;
3351           if (os->flags & SEC_SMALL_DATA)
3352             {
3353               if (min_short_vma > lo)
3354                 min_short_vma = lo;
3355               if (max_short_vma < hi)
3356                 max_short_vma = hi;
3357             }
3358         }
3359
3360       /* See if the user wants to force a value.  */
3361       gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3362                                  false, false);
3363
3364       if (gp
3365           && (gp->root.type == bfd_link_hash_defined
3366               || gp->root.type == bfd_link_hash_defweak))
3367         {
3368           asection *gp_sec = gp->root.u.def.section;
3369           gp_val = (gp->root.u.def.value
3370                     + gp_sec->output_section->vma
3371                     + gp_sec->output_offset);
3372         }
3373       else
3374         {
3375           /* Pick a sensible value.  */
3376
3377           asection *got_sec = ia64_info->got_sec;
3378
3379           /* Start with just the address of the .got.  */
3380           if (got_sec)
3381             gp_val = got_sec->output_section->vma;
3382           else if (max_short_vma != 0)
3383             gp_val = min_short_vma;
3384           else
3385             gp_val = min_vma;
3386
3387           /* If it is possible to address the entire image, but we
3388              don't with the choice above, adjust.  */
3389           if (max_vma - min_vma < 0x400000
3390               && max_vma - gp_val <= 0x200000
3391               && gp_val - min_vma > 0x200000)
3392             gp_val = min_vma + 0x200000;
3393           else if (max_short_vma != 0)
3394             {
3395               /* If we don't cover all the short data, adjust.  */
3396               if (max_short_vma - gp_val >= 0x200000)
3397                 gp_val = min_short_vma + 0x200000;
3398
3399               /* If we're addressing stuff past the end, adjust back.  */
3400               if (gp_val > max_vma)
3401                 gp_val = max_vma - 0x200000 + 8;
3402             }
3403         }
3404
3405       /* Validate whether all SHF_IA_64_SHORT sections are within
3406          range of the chosen GP.  */
3407
3408       if (max_short_vma != 0)
3409         {
3410           if (max_short_vma - min_short_vma >= 0x400000)
3411             {
3412               (*_bfd_error_handler)
3413                 (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
3414                  bfd_get_filename (abfd),
3415                  (unsigned long) (max_short_vma - min_short_vma));
3416               return false;
3417             }
3418           else if ((gp_val > min_short_vma
3419                     && gp_val - min_short_vma > 0x200000)
3420                    || (gp_val < max_short_vma
3421                        && max_short_vma - gp_val >= 0x200000))
3422             {
3423               (*_bfd_error_handler)
3424                 (_("%s: __gp does not cover short data segment"),
3425                  bfd_get_filename (abfd));
3426               return false;
3427             }
3428         }
3429
3430       _bfd_set_gp_value (abfd, gp_val);
3431
3432       if (gp)
3433         {
3434           gp->root.type = bfd_link_hash_defined;
3435           gp->root.u.def.value = gp_val;
3436           gp->root.u.def.section = bfd_abs_section_ptr;
3437         }
3438     }
3439
3440   /* If we're producing a final executable, we need to sort the contents
3441      of the .IA_64.unwind section.  Force this section to be relocated
3442      into memory rather than written immediately to the output file.  */
3443   unwind_output_sec = NULL;
3444   if (!info->relocateable)
3445     {
3446       asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3447       if (s)
3448         {
3449           unwind_output_sec = s->output_section;
3450           unwind_output_sec->contents
3451             = bfd_malloc (unwind_output_sec->_raw_size);
3452           if (unwind_output_sec->contents == NULL)
3453             return false;
3454         }
3455     }
3456
3457   /* Invoke the regular ELF backend linker to do all the work.  */
3458   if (!bfd_elfNN_bfd_final_link (abfd, info))
3459     return false;
3460
3461   if (unwind_output_sec)
3462     {
3463       elfNN_ia64_unwind_entry_compare_bfd = abfd;
3464       qsort (unwind_output_sec->contents,
3465              (size_t) (unwind_output_sec->_raw_size / 24),
3466              24,
3467              elfNN_ia64_unwind_entry_compare);
3468
3469       if (! bfd_set_section_contents (abfd, unwind_output_sec,
3470                                       unwind_output_sec->contents, (bfd_vma) 0,
3471                                       unwind_output_sec->_raw_size))
3472         return false;
3473     }
3474
3475   return true;
3476 }
3477
3478 static boolean
3479 elfNN_ia64_relocate_section (output_bfd, info, input_bfd, input_section,
3480                              contents, relocs, local_syms, local_sections)
3481      bfd *output_bfd;
3482      struct bfd_link_info *info;
3483      bfd *input_bfd;
3484      asection *input_section;
3485      bfd_byte *contents;
3486      Elf_Internal_Rela *relocs;
3487      Elf_Internal_Sym *local_syms;
3488      asection **local_sections;
3489 {
3490   struct elfNN_ia64_link_hash_table *ia64_info;
3491   Elf_Internal_Shdr *symtab_hdr;
3492   Elf_Internal_Rela *rel;
3493   Elf_Internal_Rela *relend;
3494   asection *srel;
3495   boolean ret_val = true;       /* for non-fatal errors */
3496   bfd_vma gp_val;
3497
3498   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3499   ia64_info = elfNN_ia64_hash_table (info);
3500
3501   /* Infect various flags from the input section to the output section.  */
3502   if (info->relocateable)
3503     {
3504       bfd_vma flags;
3505
3506       flags = elf_section_data(input_section)->this_hdr.sh_flags;
3507       flags &= SHF_IA_64_NORECOV;
3508
3509       elf_section_data(input_section->output_section)
3510         ->this_hdr.sh_flags |= flags;
3511     }
3512
3513   gp_val = _bfd_get_gp_value (output_bfd);
3514   srel = get_reloc_section (input_bfd, ia64_info, input_section, false);
3515
3516   rel = relocs;
3517   relend = relocs + input_section->reloc_count;
3518   for (; rel < relend; ++rel)
3519     {
3520       struct elf_link_hash_entry *h;
3521       struct elfNN_ia64_dyn_sym_info *dyn_i;
3522       bfd_reloc_status_type r;
3523       reloc_howto_type *howto;
3524       unsigned long r_symndx;
3525       Elf_Internal_Sym *sym;
3526       unsigned int r_type;
3527       bfd_vma value;
3528       asection *sym_sec;
3529       bfd_byte *hit_addr;
3530       boolean dynamic_symbol_p;
3531       boolean undef_weak_ref;
3532
3533       r_type = ELFNN_R_TYPE (rel->r_info);
3534       if (r_type > R_IA64_MAX_RELOC_CODE)
3535         {
3536           (*_bfd_error_handler)
3537             (_("%s: unknown relocation type %d"),
3538              bfd_archive_filename (input_bfd), (int)r_type);
3539           bfd_set_error (bfd_error_bad_value);
3540           ret_val = false;
3541           continue;
3542         }
3543       howto = lookup_howto (r_type);
3544       r_symndx = ELFNN_R_SYM (rel->r_info);
3545
3546       if (info->relocateable)
3547         {
3548           /* This is a relocateable link.  We don't have to change
3549              anything, unless the reloc is against a section symbol,
3550              in which case we have to adjust according to where the
3551              section symbol winds up in the output section.  */
3552           if (r_symndx < symtab_hdr->sh_info)
3553             {
3554               sym = local_syms + r_symndx;
3555               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3556                 {
3557                   sym_sec = local_sections[r_symndx];
3558                   rel->r_addend += sym_sec->output_offset;
3559                 }
3560             }
3561           continue;
3562         }
3563
3564       /* This is a final link.  */
3565
3566       h = NULL;
3567       sym = NULL;
3568       sym_sec = NULL;
3569       undef_weak_ref = false;
3570
3571       if (r_symndx < symtab_hdr->sh_info)
3572         {
3573           /* Reloc against local symbol.  */
3574           sym = local_syms + r_symndx;
3575           sym_sec = local_sections[r_symndx];
3576           value = _bfd_elf_rela_local_sym (output_bfd, sym, sym_sec, rel);
3577           if ((sym_sec->flags & SEC_MERGE)
3578               && ELF_ST_TYPE (sym->st_info) == STT_SECTION
3579               && (elf_section_data (sym_sec)->sec_info_type
3580                   == ELF_INFO_TYPE_MERGE))
3581             {
3582               struct elfNN_ia64_local_hash_entry *loc_h;
3583       
3584               loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, false);
3585               if (loc_h && ! loc_h->sec_merge_done)
3586                 {
3587                   struct elfNN_ia64_dyn_sym_info *dynent;
3588                   asection *msec;
3589
3590                   for (dynent = loc_h->info; dynent; dynent = dynent->next)
3591                     {
3592                       msec = sym_sec;
3593                       dynent->addend =
3594                         _bfd_merged_section_offset (output_bfd, &msec,
3595                                                     elf_section_data (msec)->
3596                                                     sec_info,
3597                                                     sym->st_value
3598                                                     + dynent->addend,
3599                                                     (bfd_vma) 0);
3600                       dynent->addend -= sym->st_value;
3601                       dynent->addend += msec->output_section->vma
3602                                         + msec->output_offset
3603                                         - sym_sec->output_section->vma
3604                                         - sym_sec->output_offset;
3605                     }
3606                   loc_h->sec_merge_done = 1;
3607                 }
3608             }
3609         }
3610       else
3611         {
3612           long indx;
3613
3614           /* Reloc against global symbol.  */
3615           indx = r_symndx - symtab_hdr->sh_info;
3616           h = elf_sym_hashes (input_bfd)[indx];
3617           while (h->root.type == bfd_link_hash_indirect
3618                  || h->root.type == bfd_link_hash_warning)
3619             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3620
3621           value = 0;
3622           if (h->root.type == bfd_link_hash_defined
3623               || h->root.type == bfd_link_hash_defweak)
3624             {
3625               sym_sec = h->root.u.def.section;
3626
3627               /* Detect the cases that sym_sec->output_section is
3628                  expected to be NULL -- all cases in which the symbol
3629                  is defined in another shared module.  This includes
3630                  PLT relocs for which we've created a PLT entry and
3631                  other relocs for which we're prepared to create
3632                  dynamic relocations.  */
3633               /* ??? Just accept it NULL and continue.  */
3634
3635               if (sym_sec->output_section != NULL)
3636                 {
3637                   value = (h->root.u.def.value
3638                            + sym_sec->output_section->vma
3639                            + sym_sec->output_offset);
3640                 }
3641             }
3642           else if (h->root.type == bfd_link_hash_undefweak)
3643             undef_weak_ref = true;
3644           else if (info->shared
3645                    && (!info->symbolic || info->allow_shlib_undefined)
3646                    && !info->no_undefined
3647                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3648             ;
3649           else
3650             {
3651               if (! ((*info->callbacks->undefined_symbol)
3652                      (info, h->root.root.string, input_bfd,
3653                       input_section, rel->r_offset,
3654                       (!info->shared || info->no_undefined
3655                        || ELF_ST_VISIBILITY (h->other)))))
3656                 return false;
3657               ret_val = false;
3658               continue;
3659             }
3660         }
3661
3662       hit_addr = contents + rel->r_offset;
3663       value += rel->r_addend;
3664       dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info);
3665
3666       switch (r_type)
3667         {
3668         case R_IA64_NONE:
3669         case R_IA64_LDXMOV:
3670           continue;
3671
3672         case R_IA64_IMM14:
3673         case R_IA64_IMM22:
3674         case R_IA64_IMM64:
3675         case R_IA64_DIR32MSB:
3676         case R_IA64_DIR32LSB:
3677         case R_IA64_DIR64MSB:
3678         case R_IA64_DIR64LSB:
3679           /* Install a dynamic relocation for this reloc.  */
3680           if ((dynamic_symbol_p || info->shared
3681                || (elfNN_ia64_aix_vec (info->hash->creator)
3682                    /* Don't emit relocs for __GLOB_DATA_PTR on AIX. */
3683                    && (!h || strcmp (h->root.root.string,
3684                                      "__GLOB_DATA_PTR") != 0)))
3685               && r_symndx != 0
3686               && (input_section->flags & SEC_ALLOC) != 0)
3687             {
3688               unsigned int dyn_r_type;
3689               long dynindx;
3690               bfd_vma addend;
3691
3692               BFD_ASSERT (srel != NULL);
3693
3694               /* If we don't need dynamic symbol lookup, find a
3695                  matching RELATIVE relocation.  */
3696               dyn_r_type = r_type;
3697               if (dynamic_symbol_p)
3698                 {
3699                   dynindx = h->dynindx;
3700                   addend = rel->r_addend;
3701                   value = 0;
3702                 }
3703               else
3704                 {
3705                   switch (r_type)
3706                     {
3707                     case R_IA64_DIR32MSB:
3708                       dyn_r_type = R_IA64_REL32MSB;
3709                       break;
3710                     case R_IA64_DIR32LSB:
3711                       dyn_r_type = R_IA64_REL32LSB;
3712                       break;
3713                     case R_IA64_DIR64MSB:
3714                       dyn_r_type = R_IA64_REL64MSB;
3715                       break;
3716                     case R_IA64_DIR64LSB:
3717                       dyn_r_type = R_IA64_REL64LSB;
3718                       break;
3719
3720                     default:
3721                       /* We can't represent this without a dynamic symbol.
3722                          Adjust the relocation to be against an output
3723                          section symbol, which are always present in the
3724                          dynamic symbol table.  */
3725                       /* ??? People shouldn't be doing non-pic code in
3726                          shared libraries.  Hork.  */
3727                       (*_bfd_error_handler)
3728                         (_("%s: linking non-pic code in a shared library"),
3729                          bfd_archive_filename (input_bfd));
3730                       ret_val = false;
3731                       continue;
3732                     }
3733                   dynindx = 0;
3734                   addend = value;
3735                 }
3736
3737               if (elfNN_ia64_aix_vec (info->hash->creator))
3738                 rel->r_addend = value;
3739               elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3740                                             srel, rel->r_offset, dyn_r_type,
3741                                             dynindx, addend);
3742             }
3743           /* FALLTHRU */
3744
3745         case R_IA64_LTV32MSB:
3746         case R_IA64_LTV32LSB:
3747         case R_IA64_LTV64MSB:
3748         case R_IA64_LTV64LSB:
3749           r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3750           break;
3751
3752         case R_IA64_GPREL22:
3753         case R_IA64_GPREL64I:
3754         case R_IA64_GPREL32MSB:
3755         case R_IA64_GPREL32LSB:
3756         case R_IA64_GPREL64MSB:
3757         case R_IA64_GPREL64LSB:
3758           if (dynamic_symbol_p)
3759             {
3760               (*_bfd_error_handler)
3761                 (_("%s: @gprel relocation against dynamic symbol %s"),
3762                  bfd_archive_filename (input_bfd), h->root.root.string);
3763               ret_val = false;
3764               continue;
3765             }
3766           value -= gp_val;
3767           r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3768           break;
3769
3770         case R_IA64_LTOFF22:
3771         case R_IA64_LTOFF22X:
3772         case R_IA64_LTOFF64I:
3773           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3774           value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
3775                                  rel->r_addend, value, R_IA64_DIR64LSB);
3776           value -= gp_val;
3777           r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3778           break;
3779
3780         case R_IA64_PLTOFF22:
3781         case R_IA64_PLTOFF64I:
3782         case R_IA64_PLTOFF64MSB:
3783         case R_IA64_PLTOFF64LSB:
3784           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3785           value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
3786           value -= gp_val;
3787           r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3788           break;
3789
3790         case R_IA64_FPTR64I:
3791         case R_IA64_FPTR32MSB:
3792         case R_IA64_FPTR32LSB:
3793         case R_IA64_FPTR64MSB:
3794         case R_IA64_FPTR64LSB:
3795           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3796           if (dyn_i->want_fptr)
3797             {
3798               if (!undef_weak_ref)
3799                 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3800             }
3801           else
3802             {
3803               long dynindx;
3804
3805               /* Otherwise, we expect the dynamic linker to create
3806                  the entry.  */
3807
3808               if (h)
3809                 {
3810                   if (h->dynindx != -1)
3811                     dynindx = h->dynindx;
3812                   else
3813                     dynindx = (_bfd_elf_link_lookup_local_dynindx
3814                                (info, h->root.u.def.section->owner,
3815                                 global_sym_index (h)));
3816                 }
3817               else
3818                 {
3819                   dynindx = (_bfd_elf_link_lookup_local_dynindx
3820                              (info, input_bfd, (long) r_symndx));
3821                 }
3822
3823               elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3824                                             srel, rel->r_offset, r_type,
3825                                             dynindx, rel->r_addend);
3826               value = 0;
3827             }
3828
3829           r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3830           break;
3831
3832         case R_IA64_LTOFF_FPTR22:
3833         case R_IA64_LTOFF_FPTR64I:
3834         case R_IA64_LTOFF_FPTR32MSB:
3835         case R_IA64_LTOFF_FPTR32LSB:
3836         case R_IA64_LTOFF_FPTR64MSB:
3837         case R_IA64_LTOFF_FPTR64LSB:
3838           {
3839             long dynindx;
3840
3841             dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3842             if (dyn_i->want_fptr)
3843               {
3844                 BFD_ASSERT (h == NULL || h->dynindx == -1)
3845                 if (!undef_weak_ref)
3846                   value = set_fptr_entry (output_bfd, info, dyn_i, value);
3847                 dynindx = -1;
3848               }
3849             else
3850               {
3851                 /* Otherwise, we expect the dynamic linker to create
3852                    the entry.  */
3853                 if (h)
3854                   {
3855                     if (h->dynindx != -1)
3856                       dynindx = h->dynindx;
3857                     else
3858                       dynindx = (_bfd_elf_link_lookup_local_dynindx
3859                                  (info, h->root.u.def.section->owner,
3860                                   global_sym_index (h)));
3861                   }
3862                 else
3863                   dynindx = (_bfd_elf_link_lookup_local_dynindx
3864                              (info, input_bfd, (long) r_symndx));
3865                 value = 0;
3866               }
3867
3868             value = set_got_entry (output_bfd, info, dyn_i, dynindx,
3869                                    rel->r_addend, value, R_IA64_FPTR64LSB);
3870             value -= gp_val;
3871             r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3872           }
3873           break;
3874
3875         case R_IA64_PCREL32MSB:
3876         case R_IA64_PCREL32LSB:
3877         case R_IA64_PCREL64MSB:
3878         case R_IA64_PCREL64LSB:
3879           /* Install a dynamic relocation for this reloc.  */
3880           if ((dynamic_symbol_p
3881                || elfNN_ia64_aix_vec (info->hash->creator))
3882               && r_symndx != 0)
3883             {
3884               BFD_ASSERT (srel != NULL);
3885
3886               elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3887                                             srel, rel->r_offset, r_type,
3888                                             h->dynindx, rel->r_addend);
3889             }
3890           goto finish_pcrel;
3891
3892         case R_IA64_PCREL21BI:
3893         case R_IA64_PCREL21F:
3894         case R_IA64_PCREL21M:
3895           /* ??? These two are only used for speculation fixup code.
3896              They should never be dynamic.  */
3897           if (dynamic_symbol_p)
3898             {
3899               (*_bfd_error_handler)
3900                 (_("%s: dynamic relocation against speculation fixup"),
3901                  bfd_archive_filename (input_bfd));
3902               ret_val = false;
3903               continue;
3904             }
3905           if (undef_weak_ref)
3906             {
3907               (*_bfd_error_handler)
3908                 (_("%s: speculation fixup against undefined weak symbol"),
3909                  bfd_archive_filename (input_bfd));
3910               ret_val = false;
3911               continue;
3912             }
3913           goto finish_pcrel;
3914
3915         case R_IA64_PCREL21B:
3916         case R_IA64_PCREL60B:
3917           /* We should have created a PLT entry for any dynamic symbol.  */
3918           dyn_i = NULL;
3919           if (h)
3920             dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3921
3922           if (dyn_i && dyn_i->want_plt2)
3923             {
3924               /* Should have caught this earlier.  */
3925               BFD_ASSERT (rel->r_addend == 0);
3926
3927               value = (ia64_info->plt_sec->output_section->vma
3928                        + ia64_info->plt_sec->output_offset
3929                        + dyn_i->plt2_offset);
3930             }
3931           else
3932             {
3933               /* Since there's no PLT entry, Validate that this is
3934                  locally defined.  */
3935               BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
3936
3937               /* If the symbol is undef_weak, we shouldn't be trying
3938                  to call it.  There's every chance that we'd wind up
3939                  with an out-of-range fixup here.  Don't bother setting
3940                  any value at all.  */
3941               if (undef_weak_ref)
3942                 continue;
3943             }
3944           goto finish_pcrel;
3945
3946         case R_IA64_PCREL22:
3947         case R_IA64_PCREL64I:
3948         finish_pcrel:
3949           /* Make pc-relative.  */
3950           value -= (input_section->output_section->vma
3951                     + input_section->output_offset
3952                     + rel->r_offset) & ~ (bfd_vma) 0x3;
3953           r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3954           break;
3955
3956         case R_IA64_SEGREL32MSB:
3957         case R_IA64_SEGREL32LSB:
3958         case R_IA64_SEGREL64MSB:
3959         case R_IA64_SEGREL64LSB:
3960           if (r_symndx == 0)
3961             {
3962               /* If the input section was discarded from the output, then
3963                  do nothing.  */
3964               r = bfd_reloc_ok;
3965             }
3966           else
3967             {
3968               struct elf_segment_map *m;
3969               Elf_Internal_Phdr *p;
3970
3971               /* Find the segment that contains the output_section.  */
3972               for (m = elf_tdata (output_bfd)->segment_map,
3973                      p = elf_tdata (output_bfd)->phdr;
3974                    m != NULL;
3975                    m = m->next, p++)
3976                 {
3977                   int i;
3978                   for (i = m->count - 1; i >= 0; i--)
3979                     if (m->sections[i] == sym_sec->output_section)
3980                       break;
3981                   if (i >= 0)
3982                     break;
3983                 }
3984
3985               if (m == NULL)
3986                 {
3987                   r = bfd_reloc_notsupported;
3988                 }
3989               else
3990                 {
3991                   /* The VMA of the segment is the vaddr of the associated
3992                      program header.  */
3993                   if (value > p->p_vaddr)
3994                     value -= p->p_vaddr;
3995                   else
3996                     value = 0;
3997                   r = elfNN_ia64_install_value (output_bfd, hit_addr, value,
3998                                                 r_type);
3999                 }
4000               break;
4001             }
4002
4003         case R_IA64_SECREL32MSB:
4004         case R_IA64_SECREL32LSB:
4005         case R_IA64_SECREL64MSB:
4006         case R_IA64_SECREL64LSB:
4007           /* Make output-section relative.  */
4008           if (value > input_section->output_section->vma)
4009             value -= input_section->output_section->vma;
4010           else
4011             value = 0;
4012           r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4013           break;
4014
4015         case R_IA64_IPLTMSB:
4016         case R_IA64_IPLTLSB:
4017           /* Install a dynamic relocation for this reloc.  */
4018           if ((dynamic_symbol_p || info->shared)
4019               && (input_section->flags & SEC_ALLOC) != 0)
4020             {
4021               BFD_ASSERT (srel != NULL);
4022
4023               /* If we don't need dynamic symbol lookup, install two
4024                  RELATIVE relocations.  */
4025               if (! dynamic_symbol_p)
4026                 {
4027                   unsigned int dyn_r_type;
4028
4029                   if (r_type == R_IA64_IPLTMSB)
4030                     dyn_r_type = R_IA64_REL64MSB;
4031                   else
4032                     dyn_r_type = R_IA64_REL64LSB;
4033
4034                   elfNN_ia64_install_dyn_reloc (output_bfd, info,
4035                                                 input_section,
4036                                                 srel, rel->r_offset,
4037                                                 dyn_r_type, 0, value);
4038                   elfNN_ia64_install_dyn_reloc (output_bfd, info,
4039                                                 input_section,
4040                                                 srel, rel->r_offset + 8,
4041                                                 dyn_r_type, 0, gp_val);
4042                 }
4043               else
4044                 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
4045                                               srel, rel->r_offset, r_type,
4046                                               h->dynindx, rel->r_addend);
4047             }
4048
4049           if (r_type == R_IA64_IPLTMSB)
4050             r_type = R_IA64_DIR64MSB;
4051           else
4052             r_type = R_IA64_DIR64LSB;
4053           elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4054           r = elfNN_ia64_install_value (output_bfd, hit_addr + 8, gp_val,
4055                                         r_type);
4056           break;
4057
4058         default:
4059           r = bfd_reloc_notsupported;
4060           break;
4061         }
4062
4063       switch (r)
4064         {
4065         case bfd_reloc_ok:
4066           break;
4067
4068         case bfd_reloc_undefined:
4069           /* This can happen for global table relative relocs if
4070              __gp is undefined.  This is a panic situation so we
4071              don't try to continue.  */
4072           (*info->callbacks->undefined_symbol)
4073             (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
4074           return false;
4075
4076         case bfd_reloc_notsupported:
4077           {
4078             const char *name;
4079
4080             if (h)
4081               name = h->root.root.string;
4082             else
4083               {
4084                 name = bfd_elf_string_from_elf_section (input_bfd,
4085                                                         symtab_hdr->sh_link,
4086                                                         sym->st_name);
4087                 if (name == NULL)
4088                   return false;
4089                 if (*name == '\0')
4090                   name = bfd_section_name (input_bfd, input_section);
4091               }
4092             if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
4093                                               name, input_bfd,
4094                                               input_section, rel->r_offset))
4095               return false;
4096             ret_val = false;
4097           }
4098           break;
4099
4100         case bfd_reloc_dangerous:
4101         case bfd_reloc_outofrange:
4102         case bfd_reloc_overflow:
4103         default:
4104           {
4105             const char *name;
4106
4107             if (h)
4108               name = h->root.root.string;
4109             else
4110               {
4111                 name = bfd_elf_string_from_elf_section (input_bfd,
4112                                                         symtab_hdr->sh_link,
4113                                                         sym->st_name);
4114                 if (name == NULL)
4115                   return false;
4116                 if (*name == '\0')
4117                   name = bfd_section_name (input_bfd, input_section);
4118               }
4119             if (!(*info->callbacks->reloc_overflow) (info, name,
4120                                                      howto->name,
4121                                                      (bfd_vma) 0,
4122                                                      input_bfd,
4123                                                      input_section,
4124                                                      rel->r_offset))
4125               return false;
4126             ret_val = false;
4127           }
4128           break;
4129         }
4130     }
4131
4132   return ret_val;
4133 }
4134
4135 static boolean
4136 elfNN_ia64_finish_dynamic_symbol (output_bfd, info, h, sym)
4137      bfd *output_bfd;
4138      struct bfd_link_info *info;
4139      struct elf_link_hash_entry *h;
4140      Elf_Internal_Sym *sym;
4141 {
4142   struct elfNN_ia64_link_hash_table *ia64_info;
4143   struct elfNN_ia64_dyn_sym_info *dyn_i;
4144
4145   ia64_info = elfNN_ia64_hash_table (info);
4146   dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
4147
4148   /* Fill in the PLT data, if required.  */
4149   if (dyn_i && dyn_i->want_plt)
4150     {
4151       Elf_Internal_Rela outrel;
4152       bfd_byte *loc;
4153       asection *plt_sec;
4154       bfd_vma plt_addr, pltoff_addr, gp_val, index;
4155       ElfNN_External_Rela *rel;
4156
4157       gp_val = _bfd_get_gp_value (output_bfd);
4158
4159       /* Initialize the minimal PLT entry.  */
4160
4161       index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
4162       plt_sec = ia64_info->plt_sec;
4163       loc = plt_sec->contents + dyn_i->plt_offset;
4164
4165       memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
4166       elfNN_ia64_install_value (output_bfd, loc, index, R_IA64_IMM22);
4167       elfNN_ia64_install_value (output_bfd, loc+2, -dyn_i->plt_offset,
4168                                 R_IA64_PCREL21B);
4169
4170       plt_addr = (plt_sec->output_section->vma
4171                   + plt_sec->output_offset
4172                   + dyn_i->plt_offset);
4173       pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
4174
4175       /* Initialize the FULL PLT entry, if needed.  */
4176       if (dyn_i->want_plt2)
4177         {
4178           loc = plt_sec->contents + dyn_i->plt2_offset;
4179
4180           memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
4181           elfNN_ia64_install_value (output_bfd, loc, pltoff_addr - gp_val,
4182                                     R_IA64_IMM22);
4183
4184           /* Mark the symbol as undefined, rather than as defined in the
4185              plt section.  Leave the value alone.  */
4186           /* ??? We didn't redefine it in adjust_dynamic_symbol in the
4187              first place.  But perhaps elflink.h did some for us.  */
4188           if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4189             sym->st_shndx = SHN_UNDEF;
4190         }
4191
4192       /* Create the dynamic relocation.  */
4193       outrel.r_offset = pltoff_addr;
4194       if (bfd_little_endian (output_bfd))
4195         outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
4196       else
4197         outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
4198       outrel.r_addend = 0;
4199
4200       /* This is fun.  In the .IA_64.pltoff section, we've got entries
4201          that correspond both to real PLT entries, and those that
4202          happened to resolve to local symbols but need to be created
4203          to satisfy @pltoff relocations.  The .rela.IA_64.pltoff
4204          relocations for the real PLT should come at the end of the
4205          section, so that they can be indexed by plt entry at runtime.
4206
4207          We emitted all of the relocations for the non-PLT @pltoff
4208          entries during relocate_section.  So we can consider the
4209          existing sec->reloc_count to be the base of the array of
4210          PLT relocations.  */
4211
4212       rel = (ElfNN_External_Rela *)ia64_info->rel_pltoff_sec->contents;
4213       rel += ia64_info->rel_pltoff_sec->reloc_count;
4214
4215       bfd_elfNN_swap_reloca_out (output_bfd, &outrel, rel + index);
4216     }
4217
4218   /* Mark some specially defined symbols as absolute.  */
4219   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4220       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4221       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4222     sym->st_shndx = SHN_ABS;
4223
4224   return true;
4225 }
4226
4227 static boolean
4228 elfNN_ia64_finish_dynamic_sections (abfd, info)
4229      bfd *abfd;
4230      struct bfd_link_info *info;
4231 {
4232   struct elfNN_ia64_link_hash_table *ia64_info;
4233   bfd *dynobj;
4234
4235   ia64_info = elfNN_ia64_hash_table (info);
4236   dynobj = ia64_info->root.dynobj;
4237
4238   if (elf_hash_table (info)->dynamic_sections_created)
4239     {
4240       ElfNN_External_Dyn *dyncon, *dynconend;
4241       asection *sdyn, *sgotplt;
4242       bfd_vma gp_val;
4243
4244       sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4245       sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
4246       BFD_ASSERT (sdyn != NULL);
4247       dyncon = (ElfNN_External_Dyn *) sdyn->contents;
4248       dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4249
4250       gp_val = _bfd_get_gp_value (abfd);
4251
4252       for (; dyncon < dynconend; dyncon++)
4253         {
4254           Elf_Internal_Dyn dyn;
4255
4256           bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
4257
4258           switch (dyn.d_tag)
4259             {
4260             case DT_PLTGOT:
4261               dyn.d_un.d_ptr = gp_val;
4262               break;
4263
4264             case DT_PLTRELSZ:
4265               dyn.d_un.d_val = (ia64_info->minplt_entries
4266                                 * sizeof (ElfNN_External_Rela));
4267               break;
4268
4269             case DT_JMPREL:
4270               /* See the comment above in finish_dynamic_symbol.  */
4271               dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
4272                                 + ia64_info->rel_pltoff_sec->output_offset
4273                                 + (ia64_info->rel_pltoff_sec->reloc_count
4274                                    * sizeof (ElfNN_External_Rela)));
4275               break;
4276
4277             case DT_IA_64_PLT_RESERVE:
4278               dyn.d_un.d_ptr = (sgotplt->output_section->vma
4279                                 + sgotplt->output_offset);
4280               break;
4281
4282             case DT_RELASZ:
4283               /* Do not have RELASZ include JMPREL.  This makes things
4284                  easier on ld.so.  This is not what the rest of BFD set up.  */
4285               dyn.d_un.d_val -= (ia64_info->minplt_entries
4286                                  * sizeof (ElfNN_External_Rela));
4287               break;
4288             }
4289
4290           bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
4291         }
4292
4293       /* Initialize the PLT0 entry */
4294       if (ia64_info->plt_sec)
4295         {
4296           bfd_byte *loc = ia64_info->plt_sec->contents;
4297           bfd_vma pltres;
4298
4299           memcpy (loc, plt_header, PLT_HEADER_SIZE);
4300
4301           pltres = (sgotplt->output_section->vma
4302                     + sgotplt->output_offset
4303                     - gp_val);
4304
4305           elfNN_ia64_install_value (abfd, loc+1, pltres, R_IA64_GPREL22);
4306         }
4307     }
4308
4309   return true;
4310 }
4311 \f
4312 /* ELF file flag handling: */
4313
4314 /* Function to keep IA-64 specific file flags.  */
4315 static boolean
4316 elfNN_ia64_set_private_flags (abfd, flags)
4317      bfd *abfd;
4318      flagword flags;
4319 {
4320   BFD_ASSERT (!elf_flags_init (abfd)
4321               || elf_elfheader (abfd)->e_flags == flags);
4322
4323   elf_elfheader (abfd)->e_flags = flags;
4324   elf_flags_init (abfd) = true;
4325   return true;
4326 }
4327
4328 /* Merge backend specific data from an object file to the output
4329    object file when linking.  */
4330 static boolean
4331 elfNN_ia64_merge_private_bfd_data (ibfd, obfd)
4332      bfd *ibfd, *obfd;
4333 {
4334   flagword out_flags;
4335   flagword in_flags;
4336   boolean ok = true;
4337
4338   /* Don't even pretend to support mixed-format linking.  */
4339   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4340       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4341     return false;
4342
4343   in_flags  = elf_elfheader (ibfd)->e_flags;
4344   out_flags = elf_elfheader (obfd)->e_flags;
4345
4346   if (! elf_flags_init (obfd))
4347     {
4348       elf_flags_init (obfd) = true;
4349       elf_elfheader (obfd)->e_flags = in_flags;
4350
4351       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4352           && bfd_get_arch_info (obfd)->the_default)
4353         {
4354           return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4355                                     bfd_get_mach (ibfd));
4356         }
4357
4358       return true;
4359     }
4360
4361   /* Check flag compatibility.  */
4362   if (in_flags == out_flags)
4363     return true;
4364
4365   /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set.  */
4366   if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4367     elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4368
4369   if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4370     {
4371       (*_bfd_error_handler)
4372         (_("%s: linking trap-on-NULL-dereference with non-trapping files"),
4373          bfd_archive_filename (ibfd));
4374
4375       bfd_set_error (bfd_error_bad_value);
4376       ok = false;
4377     }
4378   if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4379     {
4380       (*_bfd_error_handler)
4381         (_("%s: linking big-endian files with little-endian files"),
4382          bfd_archive_filename (ibfd));
4383
4384       bfd_set_error (bfd_error_bad_value);
4385       ok = false;
4386     }
4387   if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4388     {
4389       (*_bfd_error_handler)
4390         (_("%s: linking 64-bit files with 32-bit files"),
4391          bfd_archive_filename (ibfd));
4392
4393       bfd_set_error (bfd_error_bad_value);
4394       ok = false;
4395     }
4396   if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4397     {
4398       (*_bfd_error_handler)
4399         (_("%s: linking constant-gp files with non-constant-gp files"),
4400          bfd_archive_filename (ibfd));
4401
4402       bfd_set_error (bfd_error_bad_value);
4403       ok = false;
4404     }
4405   if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4406       != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4407     {
4408       (*_bfd_error_handler)
4409         (_("%s: linking auto-pic files with non-auto-pic files"),
4410          bfd_archive_filename (ibfd));
4411
4412       bfd_set_error (bfd_error_bad_value);
4413       ok = false;
4414     }
4415
4416   return ok;
4417 }
4418
4419 static boolean
4420 elfNN_ia64_print_private_bfd_data (abfd, ptr)
4421      bfd *abfd;
4422      PTR ptr;
4423 {
4424   FILE *file = (FILE *) ptr;
4425   flagword flags = elf_elfheader (abfd)->e_flags;
4426
4427   BFD_ASSERT (abfd != NULL && ptr != NULL);
4428
4429   fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4430            (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4431            (flags & EF_IA_64_EXT) ? "EXT, " : "",
4432            (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4433            (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4434            (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4435            (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4436            (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4437            (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4438
4439   _bfd_elf_print_private_bfd_data (abfd, ptr);
4440   return true;
4441 }
4442
4443 static enum elf_reloc_type_class
4444 elfNN_ia64_reloc_type_class (rela)
4445      const Elf_Internal_Rela *rela;
4446 {
4447   switch ((int) ELFNN_R_TYPE (rela->r_info))
4448     {
4449     case R_IA64_REL32MSB:
4450     case R_IA64_REL32LSB:
4451     case R_IA64_REL64MSB:
4452     case R_IA64_REL64LSB:
4453       return reloc_class_relative;
4454     case R_IA64_IPLTMSB:
4455     case R_IA64_IPLTLSB:
4456       return reloc_class_plt;
4457     case R_IA64_COPY:
4458       return reloc_class_copy;
4459     default:
4460       return reloc_class_normal;
4461     }
4462 }
4463
4464 static boolean
4465 elfNN_ia64_hpux_vec (const bfd_target *vec)
4466 {
4467   extern const bfd_target bfd_elfNN_ia64_hpux_big_vec;
4468   return (vec == & bfd_elfNN_ia64_hpux_big_vec);
4469 }
4470
4471 static void
4472 elfNN_hpux_post_process_headers (abfd, info)
4473         bfd *abfd;
4474         struct bfd_link_info *info ATTRIBUTE_UNUSED;
4475 {
4476   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4477
4478   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4479   i_ehdrp->e_ident[EI_ABIVERSION] = 1;
4480 }
4481
4482 boolean
4483 elfNN_hpux_backend_section_from_bfd_section (abfd, sec, retval)
4484         bfd *abfd ATTRIBUTE_UNUSED;
4485         asection *sec;
4486         int *retval;
4487 {
4488   if (bfd_is_com_section (sec))
4489     {
4490       *retval = SHN_IA_64_ANSI_COMMON;
4491       return true;
4492     }
4493   return false;
4494 }
4495 \f
4496 #define TARGET_LITTLE_SYM               bfd_elfNN_ia64_little_vec
4497 #define TARGET_LITTLE_NAME              "elfNN-ia64-little"
4498 #define TARGET_BIG_SYM                  bfd_elfNN_ia64_big_vec
4499 #define TARGET_BIG_NAME                 "elfNN-ia64-big"
4500 #define ELF_ARCH                        bfd_arch_ia64
4501 #define ELF_MACHINE_CODE                EM_IA_64
4502 #define ELF_MACHINE_ALT1                1999    /* EAS2.3 */
4503 #define ELF_MACHINE_ALT2                1998    /* EAS2.2 */
4504 #define ELF_MAXPAGESIZE                 0x10000 /* 64KB */
4505
4506 #define elf_backend_section_from_shdr \
4507         elfNN_ia64_section_from_shdr
4508 #define elf_backend_section_flags \
4509         elfNN_ia64_section_flags
4510 #define elf_backend_fake_sections \
4511         elfNN_ia64_fake_sections
4512 #define elf_backend_final_write_processing \
4513         elfNN_ia64_final_write_processing
4514 #define elf_backend_add_symbol_hook \
4515         elfNN_ia64_add_symbol_hook
4516 #define elf_backend_additional_program_headers \
4517         elfNN_ia64_additional_program_headers
4518 #define elf_backend_modify_segment_map \
4519         elfNN_ia64_modify_segment_map
4520 #define elf_info_to_howto \
4521         elfNN_ia64_info_to_howto
4522
4523 #define bfd_elfNN_bfd_reloc_type_lookup \
4524         elfNN_ia64_reloc_type_lookup
4525 #define bfd_elfNN_bfd_is_local_label_name \
4526         elfNN_ia64_is_local_label_name
4527 #define bfd_elfNN_bfd_relax_section \
4528         elfNN_ia64_relax_section
4529
4530 /* Stuff for the BFD linker: */
4531 #define bfd_elfNN_bfd_link_hash_table_create \
4532         elfNN_ia64_hash_table_create
4533 #define elf_backend_create_dynamic_sections \
4534         elfNN_ia64_create_dynamic_sections
4535 #define elf_backend_check_relocs \
4536         elfNN_ia64_check_relocs
4537 #define elf_backend_adjust_dynamic_symbol \
4538         elfNN_ia64_adjust_dynamic_symbol
4539 #define elf_backend_size_dynamic_sections \
4540         elfNN_ia64_size_dynamic_sections
4541 #define elf_backend_relocate_section \
4542         elfNN_ia64_relocate_section
4543 #define elf_backend_finish_dynamic_symbol \
4544         elfNN_ia64_finish_dynamic_symbol
4545 #define elf_backend_finish_dynamic_sections \
4546         elfNN_ia64_finish_dynamic_sections
4547 #define bfd_elfNN_bfd_final_link \
4548         elfNN_ia64_final_link
4549
4550 #define bfd_elfNN_bfd_merge_private_bfd_data \
4551         elfNN_ia64_merge_private_bfd_data
4552 #define bfd_elfNN_bfd_set_private_flags \
4553         elfNN_ia64_set_private_flags
4554 #define bfd_elfNN_bfd_print_private_bfd_data \
4555         elfNN_ia64_print_private_bfd_data
4556
4557 #define elf_backend_plt_readonly        1
4558 #define elf_backend_want_plt_sym        0
4559 #define elf_backend_plt_alignment       5
4560 #define elf_backend_got_header_size     0
4561 #define elf_backend_plt_header_size     PLT_HEADER_SIZE
4562 #define elf_backend_want_got_plt        1
4563 #define elf_backend_may_use_rel_p       1
4564 #define elf_backend_may_use_rela_p      1
4565 #define elf_backend_default_use_rela_p  1
4566 #define elf_backend_want_dynbss         0
4567 #define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
4568 #define elf_backend_hide_symbol         elfNN_ia64_hash_hide_symbol
4569 #define elf_backend_reloc_type_class    elfNN_ia64_reloc_type_class
4570
4571 #include "elfNN-target.h"
4572
4573 /* AIX-specific vectors.  */
4574
4575 #undef  TARGET_LITTLE_SYM
4576 #define TARGET_LITTLE_SYM               bfd_elfNN_ia64_aix_little_vec
4577 #undef  TARGET_LITTLE_NAME
4578 #define TARGET_LITTLE_NAME              "elfNN-ia64-aix-little"
4579 #undef  TARGET_BIG_SYM
4580 #define TARGET_BIG_SYM                  bfd_elfNN_ia64_aix_big_vec
4581 #undef  TARGET_BIG_NAME
4582 #define TARGET_BIG_NAME                 "elfNN-ia64-aix-big"
4583
4584 #undef  elf_backend_add_symbol_hook
4585 #define elf_backend_add_symbol_hook     elfNN_ia64_aix_add_symbol_hook
4586
4587 #undef  bfd_elfNN_bfd_link_add_symbols
4588 #define bfd_elfNN_bfd_link_add_symbols  elfNN_ia64_aix_link_add_symbols
4589
4590 #define elfNN_bed elfNN_ia64_aix_bed
4591
4592 #include "elfNN-target.h"
4593
4594 /* HPUX-specific vectors.  */
4595
4596 #undef  TARGET_LITTLE_SYM
4597 #undef  TARGET_LITTLE_NAME
4598 #undef  TARGET_BIG_SYM
4599 #define TARGET_BIG_SYM                  bfd_elfNN_ia64_hpux_big_vec
4600 #undef  TARGET_BIG_NAME
4601 #define TARGET_BIG_NAME                 "elfNN-ia64-hpux-big"
4602
4603 /* We need to undo the AIX specific functions.  */
4604
4605 #undef  elf_backend_add_symbol_hook
4606 #define elf_backend_add_symbol_hook     elfNN_ia64_add_symbol_hook
4607
4608 #undef  bfd_elfNN_bfd_link_add_symbols
4609 #define bfd_elfNN_bfd_link_add_symbols  _bfd_generic_link_add_symbols
4610
4611 /* These are HP-UX specific functions.  */
4612
4613 #undef  elf_backend_post_process_headers
4614 #define elf_backend_post_process_headers elfNN_hpux_post_process_headers
4615
4616 #undef  elf_backend_section_from_bfd_section
4617 #define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
4618
4619 #undef  ELF_MAXPAGESIZE
4620 #define ELF_MAXPAGESIZE                 0x1000  /* 1K */
4621
4622 #undef  elfNN_bed
4623 #define elfNN_bed elfNN_ia64_hpux_bed
4624
4625 #include "elfNN-target.h"