Add no-c-format comment to prevent confusion when translating string.
[deliverable/binutils-gdb.git] / bfd / elfxx-ia64.c
CommitLineData
800eeca4 1/* IA-64 support for 64-bit ELF
81545d45 2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
800eeca4
JW
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, 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
62typedef 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
bbe66d08 69struct elfNN_ia64_dyn_sym_info
800eeca4
JW
70{
71 /* The addend for which this entry is relevant. */
72 bfd_vma addend;
73
74 /* Next addend in the list. */
bbe66d08 75 struct elfNN_ia64_dyn_sym_info *next;
800eeca4
JW
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;
3e932841 85
800eeca4
JW
86 /* Used to count non-got, non-plt relocations for delayed sizing
87 of relocation sections. */
bbe66d08 88 struct elfNN_ia64_dyn_reloc_entry
800eeca4 89 {
bbe66d08 90 struct elfNN_ia64_dyn_reloc_entry *next;
800eeca4
JW
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
bbe66d08 110struct elfNN_ia64_local_hash_entry
800eeca4
JW
111{
112 struct bfd_hash_entry root;
bbe66d08 113 struct elfNN_ia64_dyn_sym_info *info;
f7460f5f
JJ
114
115 /* True if this hash entry's addends was translated for
116 SHF_MERGE optimization. */
117 unsigned sec_merge_done : 1;
800eeca4
JW
118};
119
bbe66d08 120struct elfNN_ia64_local_hash_table
800eeca4
JW
121{
122 struct bfd_hash_table root;
123 /* No additional fields for now. */
124};
125
bbe66d08 126struct elfNN_ia64_link_hash_entry
800eeca4
JW
127{
128 struct elf_link_hash_entry root;
bbe66d08 129 struct elfNN_ia64_dyn_sym_info *info;
800eeca4
JW
130};
131
bbe66d08 132struct elfNN_ia64_link_hash_table
800eeca4
JW
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 */
db6751f2 145 unsigned reltext : 1; /* are there relocs against readonly sections? */
800eeca4 146
bbe66d08 147 struct elfNN_ia64_local_hash_table loc_hash_table;
800eeca4
JW
148};
149
bbe66d08
JW
150#define elfNN_ia64_hash_table(p) \
151 ((struct elfNN_ia64_link_hash_table *) ((p)->hash))
800eeca4 152
bbe66d08 153static bfd_reloc_status_type elfNN_ia64_reloc
800eeca4
JW
154 PARAMS ((bfd *abfd, arelent *reloc, asymbol *sym, PTR data,
155 asection *input_section, bfd *output_bfd, char **error_message));
156static reloc_howto_type * lookup_howto
157 PARAMS ((unsigned int rtype));
bbe66d08 158static reloc_howto_type *elfNN_ia64_reloc_type_lookup
800eeca4 159 PARAMS ((bfd *abfd, bfd_reloc_code_real_type bfd_code));
bbe66d08
JW
160static void elfNN_ia64_info_to_howto
161 PARAMS ((bfd *abfd, arelent *bfd_reloc, ElfNN_Internal_Rela *elf_reloc));
162static boolean elfNN_ia64_relax_section
748abff6
RH
163 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
164 boolean *again));
81545d45 165static boolean is_unwind_section_name
d9cf1b54 166 PARAMS ((bfd *abfd, const char *));
bbe66d08
JW
167static boolean elfNN_ia64_section_from_shdr
168 PARAMS ((bfd *, ElfNN_Internal_Shdr *, char *));
cea4409c
AM
169static boolean elfNN_ia64_section_flags
170 PARAMS ((flagword *, ElfNN_Internal_Shdr *));
bbe66d08
JW
171static boolean elfNN_ia64_fake_sections
172 PARAMS ((bfd *abfd, ElfNN_Internal_Shdr *hdr, asection *sec));
81545d45
RH
173static void elfNN_ia64_final_write_processing
174 PARAMS ((bfd *abfd, boolean linker));
bbe66d08 175static boolean elfNN_ia64_add_symbol_hook
800eeca4
JW
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));
dc810e39 179static boolean elfNN_ia64_aix_vec
7b6dab7f
TW
180 PARAMS ((const bfd_target *vec));
181static 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));
185static boolean elfNN_ia64_aix_link_add_symbols
186 PARAMS ((bfd *abfd, struct bfd_link_info *info));
bbe66d08 187static int elfNN_ia64_additional_program_headers
800eeca4 188 PARAMS ((bfd *abfd));
cea4409c
AM
189static boolean elfNN_ia64_modify_segment_map
190 PARAMS ((bfd *));
bbe66d08 191static boolean elfNN_ia64_is_local_label_name
800eeca4 192 PARAMS ((bfd *abfd, const char *name));
bbe66d08 193static boolean elfNN_ia64_dynamic_symbol_p
800eeca4 194 PARAMS ((struct elf_link_hash_entry *h, struct bfd_link_info *info));
bbe66d08
JW
195static boolean elfNN_ia64_local_hash_table_init
196 PARAMS ((struct elfNN_ia64_local_hash_table *ht, bfd *abfd,
800eeca4 197 new_hash_entry_func new));
bbe66d08 198static struct bfd_hash_entry *elfNN_ia64_new_loc_hash_entry
800eeca4
JW
199 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
200 const char *string));
bbe66d08 201static struct bfd_hash_entry *elfNN_ia64_new_elf_hash_entry
800eeca4
JW
202 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
203 const char *string));
cea4409c
AM
204static void elfNN_ia64_hash_copy_indirect
205 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
206static void elfNN_ia64_hash_hide_symbol
207 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
bbe66d08 208static struct bfd_link_hash_table *elfNN_ia64_hash_table_create
800eeca4 209 PARAMS ((bfd *abfd));
bbe66d08
JW
210static struct elfNN_ia64_local_hash_entry *elfNN_ia64_local_hash_lookup
211 PARAMS ((struct elfNN_ia64_local_hash_table *table, const char *string,
800eeca4 212 boolean create, boolean copy));
cea4409c
AM
213static boolean elfNN_ia64_global_dyn_sym_thunk
214 PARAMS ((struct bfd_hash_entry *, PTR));
215static boolean elfNN_ia64_local_dyn_sym_thunk
216 PARAMS ((struct bfd_hash_entry *, PTR));
bbe66d08
JW
217static void elfNN_ia64_dyn_sym_traverse
218 PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
3e932841 219 boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR),
800eeca4 220 PTR info));
bbe66d08 221static boolean elfNN_ia64_create_dynamic_sections
800eeca4 222 PARAMS ((bfd *abfd, struct bfd_link_info *info));
f7460f5f
JJ
223static 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));
bbe66d08
JW
226static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
227 PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
800eeca4
JW
228 struct elf_link_hash_entry *h,
229 bfd *abfd, const Elf_Internal_Rela *rel, boolean create));
230static asection *get_got
231 PARAMS ((bfd *abfd, struct bfd_link_info *info,
bbe66d08 232 struct elfNN_ia64_link_hash_table *ia64_info));
800eeca4
JW
233static asection *get_fptr
234 PARAMS ((bfd *abfd, struct bfd_link_info *info,
bbe66d08 235 struct elfNN_ia64_link_hash_table *ia64_info));
800eeca4
JW
236static asection *get_pltoff
237 PARAMS ((bfd *abfd, struct bfd_link_info *info,
bbe66d08 238 struct elfNN_ia64_link_hash_table *ia64_info));
800eeca4 239static asection *get_reloc_section
bbe66d08 240 PARAMS ((bfd *abfd, struct elfNN_ia64_link_hash_table *ia64_info,
800eeca4
JW
241 asection *sec, boolean create));
242static boolean count_dyn_reloc
bbe66d08 243 PARAMS ((bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
800eeca4 244 asection *srel, int type));
bbe66d08 245static boolean elfNN_ia64_check_relocs
800eeca4
JW
246 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
247 const Elf_Internal_Rela *relocs));
bbe66d08 248static boolean elfNN_ia64_adjust_dynamic_symbol
800eeca4 249 PARAMS ((struct bfd_link_info *info, struct elf_link_hash_entry *h));
dc810e39 250static long global_sym_index
800eeca4
JW
251 PARAMS ((struct elf_link_hash_entry *h));
252static boolean allocate_fptr
bbe66d08 253 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
800eeca4 254static boolean allocate_global_data_got
bbe66d08 255 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
800eeca4 256static boolean allocate_global_fptr_got
bbe66d08 257 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
800eeca4 258static boolean allocate_local_got
bbe66d08 259 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
800eeca4 260static boolean allocate_pltoff_entries
bbe66d08 261 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
800eeca4 262static boolean allocate_plt_entries
bbe66d08 263 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
800eeca4 264static boolean allocate_plt2_entries
bbe66d08 265 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
800eeca4 266static boolean allocate_dynrel_entries
bbe66d08
JW
267 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
268static boolean elfNN_ia64_size_dynamic_sections
800eeca4 269 PARAMS ((bfd *output_bfd, struct bfd_link_info *info));
bbe66d08 270static bfd_reloc_status_type elfNN_ia64_install_value
800eeca4 271 PARAMS ((bfd *abfd, bfd_byte *hit_addr, bfd_vma val, unsigned int r_type));
bbe66d08 272static void elfNN_ia64_install_dyn_reloc
800eeca4
JW
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));
276static bfd_vma set_got_entry
277 PARAMS ((bfd *abfd, struct bfd_link_info *info,
bbe66d08 278 struct elfNN_ia64_dyn_sym_info *dyn_i, long dynindx,
800eeca4
JW
279 bfd_vma addend, bfd_vma value, unsigned int dyn_r_type));
280static bfd_vma set_fptr_entry
281 PARAMS ((bfd *abfd, struct bfd_link_info *info,
bbe66d08 282 struct elfNN_ia64_dyn_sym_info *dyn_i,
800eeca4
JW
283 bfd_vma value));
284static bfd_vma set_pltoff_entry
285 PARAMS ((bfd *abfd, struct bfd_link_info *info,
bbe66d08 286 struct elfNN_ia64_dyn_sym_info *dyn_i,
800eeca4 287 bfd_vma value, boolean));
cea4409c
AM
288static int elfNN_ia64_unwind_entry_compare
289 PARAMS ((const PTR, const PTR));
bbe66d08 290static boolean elfNN_ia64_final_link
800eeca4 291 PARAMS ((bfd *abfd, struct bfd_link_info *info));
bbe66d08 292static boolean elfNN_ia64_relocate_section
800eeca4
JW
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));
bbe66d08 297static boolean elfNN_ia64_finish_dynamic_symbol
800eeca4
JW
298 PARAMS ((bfd *output_bfd, struct bfd_link_info *info,
299 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
bbe66d08 300static boolean elfNN_ia64_finish_dynamic_sections
800eeca4 301 PARAMS ((bfd *abfd, struct bfd_link_info *info));
bbe66d08 302static boolean elfNN_ia64_set_private_flags
800eeca4 303 PARAMS ((bfd *abfd, flagword flags));
bbe66d08 304static boolean elfNN_ia64_merge_private_bfd_data
800eeca4 305 PARAMS ((bfd *ibfd, bfd *obfd));
bbe66d08 306static boolean elfNN_ia64_print_private_bfd_data
800eeca4 307 PARAMS ((bfd *abfd, PTR ptr));
db6751f2 308static enum elf_reloc_type_class elfNN_ia64_reloc_type_class
f51e552e 309 PARAMS ((const Elf_Internal_Rela *));
d9cf1b54
AM
310static boolean elfNN_ia64_hpux_vec
311 PARAMS ((const bfd_target *vec));
fcf12726
AM
312static void elfNN_hpux_post_process_headers
313 PARAMS ((bfd *abfd, struct bfd_link_info *info));
d9cf1b54
AM
314boolean elfNN_hpux_backend_section_from_bfd_section
315 PARAMS ((bfd *abfd, ElfNN_Internal_Shdr *hdr, asection *sec, int *retval));
800eeca4
JW
316\f
317/* ia64-specific relocation */
318
319/* Perform a relocation. Not much to do here as all the hard work is
bbe66d08 320 done in elfNN_ia64_final_link_relocate. */
800eeca4 321static bfd_reloc_status_type
bbe66d08 322elfNN_ia64_reloc (abfd, reloc, sym, data, input_section,
800eeca4 323 output_bfd, error_message)
64bf6ae6 324 bfd *abfd ATTRIBUTE_UNUSED;
800eeca4 325 arelent *reloc;
64bf6ae6
JW
326 asymbol *sym ATTRIBUTE_UNUSED;
327 PTR data ATTRIBUTE_UNUSED;
800eeca4
JW
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 }
bbe66d08 337 *error_message = "Unsupported call to elfNN_ia64_reloc";
800eeca4
JW
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, \
bbe66d08 343 elfNN_ia64_reloc, NAME, false, 0, 0, IN)
800eeca4
JW
344
345/* This table has to be sorted according to increasing number of the
346 TYPE field. */
347static 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
748abff6 374 IA64_HOWTO (R_IA64_FPTR64I, "FPTR64I", 0, false, true),
800eeca4
JW
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
748abff6 380 IA64_HOWTO (R_IA64_PCREL60B, "PCREL60B", 0, true, true),
800eeca4
JW
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
748abff6
RH
389 IA64_HOWTO (R_IA64_LTOFF_FPTR22, "LTOFF_FPTR22", 0, false, true),
390 IA64_HOWTO (R_IA64_LTOFF_FPTR64I, "LTOFF_FPTR64I", 0, false, true),
a4bd8390
JW
391 IA64_HOWTO (R_IA64_LTOFF_FPTR32MSB, "LTOFF_FPTR32MSB", 2, false, true),
392 IA64_HOWTO (R_IA64_LTOFF_FPTR32LSB, "LTOFF_FPTR32LSB", 2, false, true),
800eeca4
JW
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
800eeca4
JW
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
748abff6
RH
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
800eeca4
JW
420 IA64_HOWTO (R_IA64_IPLTMSB, "IPLTMSB", 4, false, true),
421 IA64_HOWTO (R_IA64_IPLTLSB, "IPLTLSB", 4, false, true),
800eeca4
JW
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
748abff6 426 IA64_HOWTO (R_IA64_TPREL22, "TPREL22", 0, false, false),
800eeca4
JW
427 IA64_HOWTO (R_IA64_TPREL64MSB, "TPREL64MSB", 8, false, false),
428 IA64_HOWTO (R_IA64_TPREL64LSB, "TPREL64LSB", 8, false, false),
748abff6 429 IA64_HOWTO (R_IA64_LTOFF_TP22, "LTOFF_TP22", 0, false, false),
800eeca4
JW
430 };
431
432static 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
436static reloc_howto_type*
437lookup_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
459static reloc_howto_type*
bbe66d08 460elfNN_ia64_reloc_type_lookup (abfd, bfd_code)
64bf6ae6 461 bfd *abfd ATTRIBUTE_UNUSED;
800eeca4
JW
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;
748abff6 500 case BFD_RELOC_IA64_PCREL21BI: rtype = R_IA64_PCREL21BI; break;
800eeca4
JW
501 case BFD_RELOC_IA64_PCREL21M: rtype = R_IA64_PCREL21M; break;
502 case BFD_RELOC_IA64_PCREL21F: rtype = R_IA64_PCREL21F; break;
748abff6
RH
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;
800eeca4
JW
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;
a4bd8390
JW
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;
800eeca4
JW
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
800eeca4
JW
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;
800eeca4
JW
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
556static void
bbe66d08 557elfNN_ia64_info_to_howto (abfd, bfd_reloc, elf_reloc)
64bf6ae6 558 bfd *abfd ATTRIBUTE_UNUSED;
800eeca4 559 arelent *bfd_reloc;
bbe66d08 560 ElfNN_Internal_Rela *elf_reloc;
800eeca4 561{
dc810e39
AM
562 bfd_reloc->howto
563 = lookup_howto ((unsigned int) ELFNN_R_TYPE (elf_reloc->r_info));
800eeca4
JW
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
571static 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
584static 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
591static 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"
7b6dab7f
TW
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)
748abff6
RH
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
610static 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
617static 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
635static boolean
bbe66d08 636elfNN_ia64_relax_section (abfd, sec, link_info, again)
748abff6
RH
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;
9ad5cbcf 651 Elf_Internal_Shdr *shndx_hdr;
748abff6 652 Elf_Internal_Rela *internal_relocs;
64bf6ae6 653 Elf_Internal_Rela *free_relocs = NULL;
748abff6
RH
654 Elf_Internal_Rela *irel, *irelend;
655 bfd_byte *contents;
64bf6ae6 656 bfd_byte *free_contents = NULL;
bbe66d08 657 ElfNN_External_Sym *extsyms;
64bf6ae6 658 ElfNN_External_Sym *free_extsyms = NULL;
9ad5cbcf 659 Elf_External_Sym_Shndx *shndx_buf = NULL;
bbe66d08 660 struct elfNN_ia64_link_hash_table *ia64_info;
748abff6
RH
661 struct one_fixup *fixups = NULL;
662 boolean changed_contents = false;
663 boolean changed_relocs = false;
664
46f5aac8
KH
665 /* Assume we're not going to change any sizes, and we'll only need
666 one pass. */
748abff6
RH
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. */
bbe66d08 682 internal_relocs = (_bfd_elfNN_link_read_relocs
748abff6
RH
683 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
684 link_info->keep_memory));
685 if (internal_relocs == NULL)
686 goto error_return;
64bf6ae6 687
748abff6
RH
688 if (! link_info->keep_memory)
689 free_relocs = internal_relocs;
690
bbe66d08 691 ia64_info = elfNN_ia64_hash_table (link_info);
748abff6
RH
692 irelend = internal_relocs + sec->reloc_count;
693
694 for (irel = internal_relocs; irel < irelend; irel++)
bbe66d08 695 if (ELFNN_R_TYPE (irel->r_info) == (int) R_IA64_PCREL21B)
748abff6
RH
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. */
748abff6
RH
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
9ad5cbcf 721 /* Read this BFD's local symbols. */
748abff6 722 if (symtab_hdr->contents != NULL)
bbe66d08 723 extsyms = (ElfNN_External_Sym *) symtab_hdr->contents;
748abff6
RH
724 else
725 {
9ad5cbcf
AM
726 bfd_size_type amt;
727
728 amt = symtab_hdr->sh_info * sizeof (ElfNN_External_Sym);
729 extsyms = (ElfNN_External_Sym *) bfd_malloc (amt);
748abff6
RH
730 if (extsyms == NULL)
731 goto error_return;
732 free_extsyms = extsyms;
733 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
9ad5cbcf
AM
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)
748abff6
RH
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;
dc810e39 758 bfd_size_type amt;
748abff6 759
bbe66d08 760 if (ELFNN_R_TYPE (irel->r_info) != (int) R_IA64_PCREL21B)
748abff6
RH
761 continue;
762
763 /* Get the value of the symbol referred to by the reloc. */
bbe66d08 764 if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
748abff6 765 {
9ad5cbcf
AM
766 ElfNN_External_Sym *esym;
767 Elf_External_Sym_Shndx *shndx;
768
748abff6 769 /* A local symbol. */
9ad5cbcf
AM
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);
748abff6
RH
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;
d9cf1b54
AM
779 else if (isym.st_shndx == SHN_IA_64_ANSI_COMMON)
780 tsec = bfd_com_section_ptr;
3e932841 781 else
9ad5cbcf 782 tsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
748abff6
RH
783
784 toff = isym.st_value;
785 }
786 else
787 {
788 unsigned long indx;
789 struct elf_link_hash_entry *h;
bbe66d08 790 struct elfNN_ia64_dyn_sym_info *dyn_i;
748abff6 791
bbe66d08 792 indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
748abff6
RH
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
dc810e39 829 + roff) & (bfd_vma) -4;
748abff6
RH
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
dc810e39 853 size_t size;
748abff6
RH
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. */
dc810e39
AM
867 trampoff = (sec->_cooked_size + 15) & (bfd_vma) -16;
868 amt = trampoff + size;
869 contents = (bfd_byte *) bfd_realloc (contents, amt);
748abff6
RH
870 if (contents == NULL)
871 goto error_return;
dc810e39 872 sec->_cooked_size = amt;
748abff6
RH
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. */
bbe66d08 879 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
748abff6
RH
880 R_IA64_PLTOFF22);
881 irel->r_offset = trampoff;
882 }
883 else
884 {
885#ifdef USE_BRL
886 memcpy (contents + trampoff, oor_brl, size);
bbe66d08 887 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
748abff6
RH
888 R_IA64_PCREL60B);
889 irel->r_offset = trampoff + 2;
890#else
891 memcpy (contents + trampoff, oor_ip, size);
bbe66d08 892 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
748abff6
RH
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. */
dc810e39 900 f = (struct one_fixup *) bfd_malloc ((bfd_size_type) sizeof (*f));
748abff6
RH
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. */
bbe66d08 910 irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
748abff6
RH
911 }
912
913 /* Fix up the existing branch to hit the trampoline. Hope like
914 hell this doesn't overflow too. */
bbe66d08 915 if (elfNN_ia64_install_value (abfd, contents + roff,
dc810e39 916 f->trampoff - (roff & (bfd_vma) -4),
748abff6
RH
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
9ad5cbcf
AM
950 if (shndx_buf != NULL)
951 free (shndx_buf);
952
748abff6
RH
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. */
973ffd63 960 symtab_hdr->contents = (unsigned char *) extsyms;
748abff6
RH
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);
9ad5cbcf
AM
972 if (shndx_buf != NULL)
973 free (shndx_buf);
748abff6
RH
974 if (free_extsyms != NULL)
975 free (free_extsyms);
976 return false;
977}
800eeca4 978\f
81545d45
RH
979/* Return true if NAME is an unwind table section name. */
980
981static inline boolean
d9cf1b54
AM
982is_unwind_section_name (abfd, name)
983 bfd *abfd;
81545d45
RH
984 const char *name;
985{
579f31ac 986 size_t len1, len2, len3;
81545d45 987
d9cf1b54
AM
988 if (elfNN_ia64_hpux_vec (abfd->xvec)
989 && !strcmp (name, ELF_STRING_ia64_unwind_hdr))
990 return false;
991
81545d45
RH
992 len1 = sizeof (ELF_STRING_ia64_unwind) - 1;
993 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
579f31ac
JJ
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);
81545d45
RH
998}
999
800eeca4
JW
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
1003static boolean
bbe66d08 1004elfNN_ia64_section_from_shdr (abfd, hdr, name)
800eeca4 1005 bfd *abfd;
bbe66d08 1006 ElfNN_Internal_Shdr *hdr;
800eeca4
JW
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:
d9cf1b54
AM
1019 case SHT_INIT_ARRAY:
1020 case SHT_FINI_ARRAY:
1021 case SHT_PREINIT_ARRAY:
1022 case SHT_IA_64_HP_OPT_ANOT:
800eeca4
JW
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
fa152c49
JW
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
1046static boolean
bbe66d08 1047elfNN_ia64_section_flags (flags, hdr)
fa152c49 1048 flagword *flags;
bbe66d08 1049 ElfNN_Internal_Shdr *hdr;
fa152c49 1050{
800eeca4 1051 if (hdr->sh_flags & SHF_IA_64_SHORT)
fa152c49 1052 *flags |= SEC_SMALL_DATA;
800eeca4
JW
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
1060static boolean
bbe66d08 1061elfNN_ia64_fake_sections (abfd, hdr, sec)
64bf6ae6 1062 bfd *abfd ATTRIBUTE_UNUSED;
bbe66d08 1063 ElfNN_Internal_Shdr *hdr;
800eeca4
JW
1064 asection *sec;
1065{
1066 register const char *name;
1067
1068 name = bfd_get_section_name (abfd, sec);
1069
d9cf1b54 1070 if (is_unwind_section_name (abfd, name))
81545d45
RH
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 }
800eeca4
JW
1077 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
1078 hdr->sh_type = SHT_IA_64_EXT;
d9cf1b54
AM
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;
800eeca4
JW
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
bbe66d08 1095 * files contain a COFF ".reloc" section inside an ELFNN object,
800eeca4
JW
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
bbe66d08 1100 * segfault. However, you won't be able to create an ELFNN binary
800eeca4
JW
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
81545d45
RH
1114/* The final processing done just before writing out an IA-64 ELF
1115 object file. */
1116
1117static void
1118elfNN_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 }
579f31ac
JJ
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;
fcf12726 1154 char *once_name = bfd_malloc (len2 + strlen (sname + len) + 1);
579f31ac 1155
fcf12726
AM
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 }
579f31ac 1177 }
81545d45
RH
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
800eeca4
JW
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
1199static boolean
bbe66d08 1200elfNN_ia64_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
800eeca4
JW
1201 bfd *abfd;
1202 struct bfd_link_info *info;
1203 const Elf_Internal_Sym *sym;
64bf6ae6
JW
1204 const char **namep ATTRIBUTE_UNUSED;
1205 flagword *flagsp ATTRIBUTE_UNUSED;
800eeca4
JW
1206 asection **secp;
1207 bfd_vma *valp;
1208{
1209 if (sym->st_shndx == SHN_COMMON
1210 && !info->relocateable
c0846b23 1211 && sym->st_size <= elf_gp_size (abfd))
800eeca4
JW
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
7b6dab7f
TW
1235static boolean
1236elfNN_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
dc810e39 1241 return (/**/vec == & bfd_elfNN_ia64_aix_little_vec
7b6dab7f
TW
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
1248static boolean
1249elfNN_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 {
64e9ece0
TW
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. */
7b6dab7f
TW
1263 struct elf_link_hash_entry *h;
1264
0a991dfe
AM
1265 h = elf_link_hash_lookup (elf_hash_table (info), *namep,
1266 false, false, false);
dc810e39 1267 if (h == NULL)
7b6dab7f
TW
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);
dc810e39 1274
7b6dab7f 1275 if (!(_bfd_generic_link_add_one_symbol
dc810e39 1276 (info, abfd, *namep, BSF_GLOBAL,
64e9ece0 1277 bfd_get_section_by_name (abfd, ".bss"),
7b6dab7f
TW
1278 bed->got_symbol_offset, (const char *) NULL, false,
1279 bed->collect, (struct bfd_link_hash_entry **) &h)))
1280 return false;
dc810e39 1281
7b6dab7f
TW
1282 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1283 h->type = STT_OBJECT;
dc810e39 1284
64e9ece0 1285 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
7b6dab7f
TW
1286 return false;
1287 }
1288
1289 return true;
1290 }
1291 else if (sym->st_shndx == SHN_LOOS)
1292 {
9ad5cbcf 1293 unsigned int i;
dc810e39 1294
10d1e03a
TW
1295 /* SHN_AIX_SYSCALL: Treat this as any other symbol. The special symbol
1296 is only relevant when compiling code for extended system calls.
dc810e39 1297 Replace the "special" section with .text, if possible.
64e9ece0 1298 Note that these symbols are always assumed to be in .text. */
9ad5cbcf 1299 for (i = 1; i < elf_numsections (abfd); i++)
7b6dab7f 1300 {
9ad5cbcf 1301 asection * sec = bfd_section_from_elf_index (abfd, i);
dc810e39 1302
7b6dab7f
TW
1303 if (sec && strcmp (sec->name, ".text") == 0)
1304 {
1305 *secp = sec;
1306 break;
1307 }
1308 }
1309
7b6dab7f
TW
1310 if (*secp == NULL)
1311 *secp = bfd_abs_section_ptr;
dc810e39 1312
7b6dab7f 1313 *valp = sym->st_size;
dc810e39 1314
7b6dab7f
TW
1315 return true;
1316 }
dc810e39 1317 else
7b6dab7f 1318 {
dc810e39 1319 return elfNN_ia64_add_symbol_hook (abfd, info, sym,
7b6dab7f
TW
1320 namep, flagsp, secp, valp);
1321 }
1322}
1323
1324boolean
1325elfNN_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
800eeca4
JW
1341/* Return the number of additional phdrs we will need. */
1342
1343static int
bbe66d08 1344elfNN_ia64_additional_program_headers (abfd)
800eeca4
JW
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
81545d45
RH
1355 /* Count how many PT_IA_64_UNWIND segments we need. */
1356 for (s = abfd->sections; s; s = s->next)
d9cf1b54 1357 if (is_unwind_section_name (abfd, s->name) && (s->flags & SEC_LOAD))
81545d45 1358 ++ret;
800eeca4
JW
1359
1360 return ret;
1361}
1362
1363static boolean
bbe66d08 1364elfNN_ia64_modify_segment_map (abfd)
800eeca4
JW
1365 bfd *abfd;
1366{
1367 struct elf_segment_map *m, **pm;
81545d45 1368 Elf_Internal_Shdr *hdr;
800eeca4 1369 asection *s;
d9cf1b54
AM
1370 boolean unwind_found;
1371 asection *unwind_sec;
800eeca4
JW
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 {
dc810e39
AM
1383 m = ((struct elf_segment_map *)
1384 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
800eeca4
JW
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
81545d45
RH
1404 /* Install PT_IA_64_UNWIND segments, if needed. */
1405 for (s = abfd->sections; s; s = s->next)
800eeca4 1406 {
81545d45
RH
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))
800eeca4 1412 {
81545d45 1413 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
d9cf1b54
AM
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 }
81545d45 1432
800eeca4 1433 if (m == NULL)
81545d45 1434 {
dc810e39
AM
1435 m = ((struct elf_segment_map *)
1436 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
81545d45
RH
1437 if (m == NULL)
1438 return false;
800eeca4 1439
81545d45
RH
1440 m->p_type = PT_IA_64_UNWIND;
1441 m->count = 1;
1442 m->sections[0] = s;
1443 m->next = NULL;
800eeca4 1444
81545d45
RH
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 }
800eeca4
JW
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
800eeca4
JW
1485/* According to the Tahoe assembler spec, all labels starting with a
1486 '.' are local. */
1487
1488static boolean
bbe66d08 1489elfNN_ia64_is_local_label_name (abfd, name)
64bf6ae6 1490 bfd *abfd ATTRIBUTE_UNUSED;
800eeca4
JW
1491 const char *name;
1492{
1493 return name[0] == '.';
1494}
1495
1496/* Should we do dynamic things to this symbol? */
1497
1498static boolean
bbe66d08 1499elfNN_ia64_dynamic_symbol_p (h, info)
800eeca4
JW
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;
2719f880
L
1512 switch (ELF_ST_VISIBILITY (h->other))
1513 {
1514 case STV_INTERNAL:
1515 case STV_HIDDEN:
1516 return false;
1517 }
800eeca4
JW
1518
1519 if (h->root.type == bfd_link_hash_undefweak
1520 || h->root.type == bfd_link_hash_defweak)
1521 return true;
1522
671bae9c 1523 if ((info->shared && (!info->symbolic || info->allow_shlib_undefined))
800eeca4
JW
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
1532static boolean
bbe66d08
JW
1533elfNN_ia64_local_hash_table_init (ht, abfd, new)
1534 struct elfNN_ia64_local_hash_table *ht;
64bf6ae6 1535 bfd *abfd ATTRIBUTE_UNUSED;
800eeca4
JW
1536 new_hash_entry_func new;
1537{
3e932841 1538 memset (ht, 0, sizeof (*ht));
800eeca4
JW
1539 return bfd_hash_table_init (&ht->root, new);
1540}
1541
1542static struct bfd_hash_entry*
bbe66d08 1543elfNN_ia64_new_loc_hash_entry (entry, table, string)
800eeca4
JW
1544 struct bfd_hash_entry *entry;
1545 struct bfd_hash_table *table;
1546 const char *string;
1547{
bbe66d08
JW
1548 struct elfNN_ia64_local_hash_entry *ret;
1549 ret = (struct elfNN_ia64_local_hash_entry *) entry;
800eeca4
JW
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. */
3e932841 1561 memset (ret, 0, sizeof (*ret));
800eeca4
JW
1562
1563 /* Call the allocation method of the superclass. */
bbe66d08 1564 ret = ((struct elfNN_ia64_local_hash_entry *)
800eeca4
JW
1565 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
1566
1567 return (struct bfd_hash_entry *) ret;
1568}
1569
1570static struct bfd_hash_entry*
bbe66d08 1571elfNN_ia64_new_elf_hash_entry (entry, table, string)
800eeca4
JW
1572 struct bfd_hash_entry *entry;
1573 struct bfd_hash_table *table;
1574 const char *string;
1575{
bbe66d08
JW
1576 struct elfNN_ia64_link_hash_entry *ret;
1577 ret = (struct elfNN_ia64_link_hash_entry *) entry;
800eeca4
JW
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. */
3e932841 1589 memset (ret, 0, sizeof (*ret));
800eeca4
JW
1590
1591 /* Call the allocation method of the superclass. */
bbe66d08 1592 ret = ((struct elfNN_ia64_link_hash_entry *)
800eeca4
JW
1593 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1594 table, string));
1595
1596 return (struct bfd_hash_entry *) ret;
1597}
1598
1599static void
bbe66d08 1600elfNN_ia64_hash_copy_indirect (xdir, xind)
800eeca4
JW
1601 struct elf_link_hash_entry *xdir, *xind;
1602{
bbe66d08 1603 struct elfNN_ia64_link_hash_entry *dir, *ind;
800eeca4 1604
57c7194e
AM
1605 dir = (struct elfNN_ia64_link_hash_entry *) xdir;
1606 ind = (struct elfNN_ia64_link_hash_entry *) xind;
800eeca4 1607
3e932841 1608 /* Copy down any references that we may have already seen to the
800eeca4
JW
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
1e370bd2 1617 if (ind->root.root.type != bfd_link_hash_indirect)
0a991dfe
AM
1618 return;
1619
800eeca4
JW
1620 /* Copy over the got and plt data. This would have been done
1621 by check_relocs. */
1622
1623 if (dir->info == NULL)
1624 {
bbe66d08 1625 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
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
1648static void
bbe66d08 1649elfNN_ia64_hash_hide_symbol (info, xh)
f41cbf03 1650 struct bfd_link_info *info ATTRIBUTE_UNUSED;
800eeca4
JW
1651 struct elf_link_hash_entry *xh;
1652{
bbe66d08
JW
1653 struct elfNN_ia64_link_hash_entry *h;
1654 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4 1655
bbe66d08 1656 h = (struct elfNN_ia64_link_hash_entry *)xh;
800eeca4
JW
1657
1658 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5fba655a
L
1659 if ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1660 h->root.dynindx = -1;
800eeca4
JW
1661
1662 for (dyn_i = h->info; dyn_i; dyn_i = dyn_i->next)
1663 dyn_i->want_plt2 = 0;
1664}
1665
1666/* Create the derived linker hash table. The IA-64 ELF port uses this
1667 derived hash table to keep information specific to the IA-64 ElF
1668 linker (without using static variables). */
1669
1670static struct bfd_link_hash_table*
bbe66d08 1671elfNN_ia64_hash_table_create (abfd)
800eeca4
JW
1672 bfd *abfd;
1673{
bbe66d08 1674 struct elfNN_ia64_link_hash_table *ret;
800eeca4 1675
dc810e39 1676 ret = bfd_zalloc (abfd, (bfd_size_type) sizeof (*ret));
800eeca4
JW
1677 if (!ret)
1678 return 0;
1679 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
bbe66d08 1680 elfNN_ia64_new_elf_hash_entry))
800eeca4
JW
1681 {
1682 bfd_release (abfd, ret);
1683 return 0;
1684 }
1685
bbe66d08
JW
1686 if (!elfNN_ia64_local_hash_table_init (&ret->loc_hash_table, abfd,
1687 elfNN_ia64_new_loc_hash_entry))
800eeca4
JW
1688 return 0;
1689 return &ret->root.root;
1690}
1691
1692/* Look up an entry in a Alpha ELF linker hash table. */
1693
bbe66d08
JW
1694static INLINE struct elfNN_ia64_local_hash_entry *
1695elfNN_ia64_local_hash_lookup(table, string, create, copy)
1696 struct elfNN_ia64_local_hash_table *table;
800eeca4
JW
1697 const char *string;
1698 boolean create, copy;
1699{
bbe66d08 1700 return ((struct elfNN_ia64_local_hash_entry *)
800eeca4
JW
1701 bfd_hash_lookup (&table->root, string, create, copy));
1702}
1703
1704/* Traverse both local and global hash tables. */
1705
bbe66d08 1706struct elfNN_ia64_dyn_sym_traverse_data
800eeca4 1707{
bbe66d08 1708 boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
800eeca4
JW
1709 PTR data;
1710};
1711
1712static boolean
bbe66d08 1713elfNN_ia64_global_dyn_sym_thunk (xentry, xdata)
800eeca4
JW
1714 struct bfd_hash_entry *xentry;
1715 PTR xdata;
1716{
bbe66d08
JW
1717 struct elfNN_ia64_link_hash_entry *entry
1718 = (struct elfNN_ia64_link_hash_entry *) xentry;
1719 struct elfNN_ia64_dyn_sym_traverse_data *data
1720 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1721 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
1722
1723 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1724 if (! (*data->func) (dyn_i, data->data))
1725 return false;
1726 return true;
1727}
1728
1729static boolean
bbe66d08 1730elfNN_ia64_local_dyn_sym_thunk (xentry, xdata)
800eeca4
JW
1731 struct bfd_hash_entry *xentry;
1732 PTR xdata;
1733{
bbe66d08
JW
1734 struct elfNN_ia64_local_hash_entry *entry
1735 = (struct elfNN_ia64_local_hash_entry *) xentry;
1736 struct elfNN_ia64_dyn_sym_traverse_data *data
1737 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1738 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
1739
1740 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1741 if (! (*data->func) (dyn_i, data->data))
1742 return false;
1743 return true;
1744}
1745
1746static void
bbe66d08
JW
1747elfNN_ia64_dyn_sym_traverse (ia64_info, func, data)
1748 struct elfNN_ia64_link_hash_table *ia64_info;
1749 boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
800eeca4
JW
1750 PTR data;
1751{
bbe66d08 1752 struct elfNN_ia64_dyn_sym_traverse_data xdata;
800eeca4
JW
1753
1754 xdata.func = func;
1755 xdata.data = data;
1756
1757 elf_link_hash_traverse (&ia64_info->root,
bbe66d08 1758 elfNN_ia64_global_dyn_sym_thunk, &xdata);
800eeca4 1759 bfd_hash_traverse (&ia64_info->loc_hash_table.root,
bbe66d08 1760 elfNN_ia64_local_dyn_sym_thunk, &xdata);
800eeca4
JW
1761}
1762\f
1763static boolean
bbe66d08 1764elfNN_ia64_create_dynamic_sections (abfd, info)
800eeca4
JW
1765 bfd *abfd;
1766 struct bfd_link_info *info;
1767{
bbe66d08 1768 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
1769 asection *s;
1770
1771 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1772 return false;
1773
bbe66d08 1774 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
1775
1776 ia64_info->plt_sec = bfd_get_section_by_name (abfd, ".plt");
1777 ia64_info->got_sec = bfd_get_section_by_name (abfd, ".got");
1778
1779 {
1780 flagword flags = bfd_get_section_flags (abfd, ia64_info->got_sec);
1781 bfd_set_section_flags (abfd, ia64_info->got_sec, SEC_SMALL_DATA | flags);
1782 }
1783
1784 if (!get_pltoff (abfd, info, ia64_info))
1785 return false;
1786
1787 s = bfd_make_section(abfd, ".rela.IA_64.pltoff");
1788 if (s == NULL
1789 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1790 | SEC_HAS_CONTENTS
1791 | SEC_IN_MEMORY
1792 | SEC_LINKER_CREATED
1793 | SEC_READONLY))
1794 || !bfd_set_section_alignment (abfd, s, 3))
1795 return false;
1796 ia64_info->rel_pltoff_sec = s;
1797
1798 s = bfd_make_section(abfd, ".rela.got");
1799 if (s == NULL
1800 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1801 | SEC_HAS_CONTENTS
1802 | SEC_IN_MEMORY
1803 | SEC_LINKER_CREATED
1804 | SEC_READONLY))
1805 || !bfd_set_section_alignment (abfd, s, 3))
1806 return false;
1807 ia64_info->rel_got_sec = s;
1808
1809 return true;
1810}
1811
f7460f5f
JJ
1812/* Find and/or create a hash entry for local symbol. */
1813static struct elfNN_ia64_local_hash_entry *
1814get_local_sym_hash (ia64_info, abfd, rel, create)
1815 struct elfNN_ia64_link_hash_table *ia64_info;
1816 bfd *abfd;
1817 const Elf_Internal_Rela *rel;
1818 boolean create;
1819{
1820 char *addr_name;
1821 size_t len;
fcf12726 1822 struct elfNN_ia64_local_hash_entry *ret;
f7460f5f
JJ
1823
1824 /* Construct a string for use in the elfNN_ia64_local_hash_table.
1825 name describes what was once anonymous memory. */
1826
1827 len = sizeof (void*)*2 + 1 + sizeof (bfd_vma)*4 + 1 + 1;
1828 len += 10; /* %p slop */
1829
fcf12726
AM
1830 addr_name = bfd_malloc (len);
1831 if (addr_name == NULL)
1832 return 0;
f7460f5f
JJ
1833 sprintf (addr_name, "%p:%lx",
1834 (void *) abfd, (unsigned long) ELFNN_R_SYM (rel->r_info));
1835
1836 /* Collect the canonical entry data for this address. */
fcf12726
AM
1837 ret = elfNN_ia64_local_hash_lookup (&ia64_info->loc_hash_table,
1838 addr_name, create, create);
1839 free (addr_name);
1840 return ret;
f7460f5f
JJ
1841}
1842
800eeca4
JW
1843/* Find and/or create a descriptor for dynamic symbol info. This will
1844 vary based on global or local symbol, and the addend to the reloc. */
1845
bbe66d08 1846static struct elfNN_ia64_dyn_sym_info *
800eeca4 1847get_dyn_sym_info (ia64_info, h, abfd, rel, create)
bbe66d08 1848 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
1849 struct elf_link_hash_entry *h;
1850 bfd *abfd;
1851 const Elf_Internal_Rela *rel;
1852 boolean create;
1853{
bbe66d08
JW
1854 struct elfNN_ia64_dyn_sym_info **pp;
1855 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4 1856 bfd_vma addend = rel ? rel->r_addend : 0;
3e932841 1857
800eeca4 1858 if (h)
bbe66d08 1859 pp = &((struct elfNN_ia64_link_hash_entry *)h)->info;
800eeca4
JW
1860 else
1861 {
bbe66d08 1862 struct elfNN_ia64_local_hash_entry *loc_h;
800eeca4 1863
f7460f5f 1864 loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
800eeca4
JW
1865 BFD_ASSERT (loc_h);
1866
1867 pp = &loc_h->info;
3e932841 1868 }
800eeca4
JW
1869
1870 for (dyn_i = *pp; dyn_i && dyn_i->addend != addend; dyn_i = *pp)
1871 pp = &dyn_i->next;
1872
1873 if (dyn_i == NULL && create)
1874 {
dc810e39
AM
1875 dyn_i = ((struct elfNN_ia64_dyn_sym_info *)
1876 bfd_zalloc (abfd, (bfd_size_type) sizeof *dyn_i));
800eeca4
JW
1877 *pp = dyn_i;
1878 dyn_i->addend = addend;
1879 }
1880
1881 return dyn_i;
1882}
1883
1884static asection *
1885get_got (abfd, info, ia64_info)
1886 bfd *abfd;
1887 struct bfd_link_info *info;
bbe66d08 1888 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4 1889{
64bf6ae6 1890 asection *got;
800eeca4
JW
1891 bfd *dynobj;
1892
1893 got = ia64_info->got_sec;
1894 if (!got)
1895 {
1896 flagword flags;
1897
1898 dynobj = ia64_info->root.dynobj;
1899 if (!dynobj)
1900 ia64_info->root.dynobj = dynobj = abfd;
1901 if (!_bfd_elf_create_got_section (dynobj, info))
1902 return 0;
1903
1904 got = bfd_get_section_by_name (dynobj, ".got");
1905 BFD_ASSERT (got);
1906 ia64_info->got_sec = got;
1907
1908 flags = bfd_get_section_flags (abfd, got);
1909 bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags);
1910 }
1911
1912 return got;
1913}
1914
1915/* Create function descriptor section (.opd). This section is called .opd
1916 because it contains "official prodecure descriptors". The "official"
1917 refers to the fact that these descriptors are used when taking the address
1918 of a procedure, thus ensuring a unique address for each procedure. */
1919
1920static asection *
1921get_fptr (abfd, info, ia64_info)
1922 bfd *abfd;
64bf6ae6 1923 struct bfd_link_info *info ATTRIBUTE_UNUSED;
bbe66d08 1924 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
1925{
1926 asection *fptr;
1927 bfd *dynobj;
1928
1929 fptr = ia64_info->fptr_sec;
1930 if (!fptr)
1931 {
1932 dynobj = ia64_info->root.dynobj;
1933 if (!dynobj)
1934 ia64_info->root.dynobj = dynobj = abfd;
1935
1936 fptr = bfd_make_section (dynobj, ".opd");
1937 if (!fptr
1938 || !bfd_set_section_flags (dynobj, fptr,
1939 (SEC_ALLOC
1940 | SEC_LOAD
1941 | SEC_HAS_CONTENTS
1942 | SEC_IN_MEMORY
1943 | SEC_READONLY
1944 | SEC_LINKER_CREATED))
1945 || !bfd_set_section_alignment (abfd, fptr, 4))
1946 {
1947 BFD_ASSERT (0);
1948 return NULL;
1949 }
1950
1951 ia64_info->fptr_sec = fptr;
1952 }
1953
1954 return fptr;
1955}
1956
1957static asection *
1958get_pltoff (abfd, info, ia64_info)
1959 bfd *abfd;
64bf6ae6 1960 struct bfd_link_info *info ATTRIBUTE_UNUSED;
bbe66d08 1961 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
1962{
1963 asection *pltoff;
1964 bfd *dynobj;
1965
1966 pltoff = ia64_info->pltoff_sec;
1967 if (!pltoff)
1968 {
1969 dynobj = ia64_info->root.dynobj;
1970 if (!dynobj)
1971 ia64_info->root.dynobj = dynobj = abfd;
1972
1973 pltoff = bfd_make_section (dynobj, ELF_STRING_ia64_pltoff);
1974 if (!pltoff
1975 || !bfd_set_section_flags (dynobj, pltoff,
1976 (SEC_ALLOC
1977 | SEC_LOAD
1978 | SEC_HAS_CONTENTS
1979 | SEC_IN_MEMORY
1980 | SEC_SMALL_DATA
1981 | SEC_LINKER_CREATED))
1982 || !bfd_set_section_alignment (abfd, pltoff, 4))
1983 {
1984 BFD_ASSERT (0);
1985 return NULL;
1986 }
1987
1988 ia64_info->pltoff_sec = pltoff;
1989 }
1990
1991 return pltoff;
1992}
1993
1994static asection *
1995get_reloc_section (abfd, ia64_info, sec, create)
1996 bfd *abfd;
bbe66d08 1997 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
1998 asection *sec;
1999 boolean create;
2000{
2001 const char *srel_name;
2002 asection *srel;
2003 bfd *dynobj;
2004
2005 srel_name = (bfd_elf_string_from_elf_section
2006 (abfd, elf_elfheader(abfd)->e_shstrndx,
2007 elf_section_data(sec)->rel_hdr.sh_name));
2008 if (srel_name == NULL)
2009 return NULL;
2010
2011 BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
2012 && strcmp (bfd_get_section_name (abfd, sec),
2013 srel_name+5) == 0)
2014 || (strncmp (srel_name, ".rel", 4) == 0
2015 && strcmp (bfd_get_section_name (abfd, sec),
2016 srel_name+4) == 0));
2017
2018 dynobj = ia64_info->root.dynobj;
2019 if (!dynobj)
2020 ia64_info->root.dynobj = dynobj = abfd;
2021
2022 srel = bfd_get_section_by_name (dynobj, srel_name);
2023 if (srel == NULL && create)
2024 {
2025 srel = bfd_make_section (dynobj, srel_name);
2026 if (srel == NULL
2027 || !bfd_set_section_flags (dynobj, srel,
2028 (SEC_ALLOC
2029 | SEC_LOAD
2030 | SEC_HAS_CONTENTS
2031 | SEC_IN_MEMORY
2032 | SEC_LINKER_CREATED
2033 | SEC_READONLY))
2034 || !bfd_set_section_alignment (dynobj, srel, 3))
2035 return NULL;
2036 }
2037
db6751f2
JJ
2038 if (sec->flags & SEC_READONLY)
2039 ia64_info->reltext = 1;
2040
800eeca4
JW
2041 return srel;
2042}
2043
2044static boolean
2045count_dyn_reloc (abfd, dyn_i, srel, type)
2046 bfd *abfd;
bbe66d08 2047 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2048 asection *srel;
2049 int type;
2050{
bbe66d08 2051 struct elfNN_ia64_dyn_reloc_entry *rent;
800eeca4
JW
2052
2053 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2054 if (rent->srel == srel && rent->type == type)
2055 break;
2056
2057 if (!rent)
2058 {
dc810e39
AM
2059 rent = ((struct elfNN_ia64_dyn_reloc_entry *)
2060 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
800eeca4
JW
2061 if (!rent)
2062 return false;
2063
2064 rent->next = dyn_i->reloc_entries;
2065 rent->srel = srel;
2066 rent->type = type;
2067 rent->count = 0;
2068 dyn_i->reloc_entries = rent;
2069 }
2070 rent->count++;
2071
2072 return true;
2073}
2074
2075static boolean
bbe66d08 2076elfNN_ia64_check_relocs (abfd, info, sec, relocs)
800eeca4
JW
2077 bfd *abfd;
2078 struct bfd_link_info *info;
2079 asection *sec;
2080 const Elf_Internal_Rela *relocs;
2081{
bbe66d08 2082 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
2083 const Elf_Internal_Rela *relend;
2084 Elf_Internal_Shdr *symtab_hdr;
2085 const Elf_Internal_Rela *rel;
2086 asection *got, *fptr, *srel;
2087
2088 if (info->relocateable)
2089 return true;
2090
2091 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
bbe66d08 2092 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
2093
2094 got = fptr = srel = NULL;
2095
2096 relend = relocs + sec->reloc_count;
2097 for (rel = relocs; rel < relend; ++rel)
2098 {
2099 enum {
2100 NEED_GOT = 1,
2101 NEED_FPTR = 2,
2102 NEED_PLTOFF = 4,
2103 NEED_MIN_PLT = 8,
2104 NEED_FULL_PLT = 16,
2105 NEED_DYNREL = 32,
2106 NEED_LTOFF_FPTR = 64,
2107 };
2108
2109 struct elf_link_hash_entry *h = NULL;
bbe66d08
JW
2110 unsigned long r_symndx = ELFNN_R_SYM (rel->r_info);
2111 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2112 int need_entry;
2113 boolean maybe_dynamic;
64bf6ae6 2114 int dynrel_type = R_IA64_NONE;
800eeca4
JW
2115
2116 if (r_symndx >= symtab_hdr->sh_info)
2117 {
2118 /* We're dealing with a global symbol -- find its hash entry
2119 and mark it as being referenced. */
2120 long indx = r_symndx - symtab_hdr->sh_info;
2121 h = elf_sym_hashes (abfd)[indx];
2122 while (h->root.type == bfd_link_hash_indirect
2123 || h->root.type == bfd_link_hash_warning)
2124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2125
2126 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2127 }
2128
2129 /* We can only get preliminary data on whether a symbol is
2130 locally or externally defined, as not all of the input files
2131 have yet been processed. Do something with what we know, as
2132 this may help reduce memory usage and processing time later. */
2133 maybe_dynamic = false;
671bae9c
NC
2134 if (h && ((info->shared
2135 && (!info->symbolic || info->allow_shlib_undefined))
800eeca4 2136 || ! (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
7b6dab7f
TW
2137 || h->root.type == bfd_link_hash_defweak
2138 || elfNN_ia64_aix_vec (abfd->xvec)))
800eeca4
JW
2139 maybe_dynamic = true;
2140
2141 need_entry = 0;
bbe66d08 2142 switch (ELFNN_R_TYPE (rel->r_info))
800eeca4
JW
2143 {
2144 case R_IA64_TPREL22:
2145 case R_IA64_TPREL64MSB:
2146 case R_IA64_TPREL64LSB:
2147 case R_IA64_LTOFF_TP22:
2148 return false;
2149
2150 case R_IA64_LTOFF_FPTR22:
2151 case R_IA64_LTOFF_FPTR64I:
a4bd8390
JW
2152 case R_IA64_LTOFF_FPTR32MSB:
2153 case R_IA64_LTOFF_FPTR32LSB:
800eeca4
JW
2154 case R_IA64_LTOFF_FPTR64MSB:
2155 case R_IA64_LTOFF_FPTR64LSB:
2156 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2157 break;
2158
2159 case R_IA64_FPTR64I:
2160 case R_IA64_FPTR32MSB:
2161 case R_IA64_FPTR32LSB:
2162 case R_IA64_FPTR64MSB:
2163 case R_IA64_FPTR64LSB:
64e9ece0 2164 if (info->shared || h || elfNN_ia64_aix_vec (abfd->xvec))
800eeca4
JW
2165 need_entry = NEED_FPTR | NEED_DYNREL;
2166 else
2167 need_entry = NEED_FPTR;
2168 dynrel_type = R_IA64_FPTR64LSB;
2169 break;
2170
2171 case R_IA64_LTOFF22:
2172 case R_IA64_LTOFF22X:
2173 case R_IA64_LTOFF64I:
2174 need_entry = NEED_GOT;
2175 break;
2176
2177 case R_IA64_PLTOFF22:
2178 case R_IA64_PLTOFF64I:
2179 case R_IA64_PLTOFF64MSB:
2180 case R_IA64_PLTOFF64LSB:
2181 need_entry = NEED_PLTOFF;
2182 if (h)
2183 {
2184 if (maybe_dynamic)
2185 need_entry |= NEED_MIN_PLT;
2186 }
2187 else
2188 {
2189 (*info->callbacks->warning)
2190 (info, _("@pltoff reloc against local symbol"), 0,
dc810e39 2191 abfd, 0, (bfd_vma) 0);
800eeca4
JW
2192 }
2193 break;
2194
2195 case R_IA64_PCREL21B:
748abff6 2196 case R_IA64_PCREL60B:
800eeca4
JW
2197 /* Depending on where this symbol is defined, we may or may not
2198 need a full plt entry. Only skip if we know we'll not need
2199 the entry -- static or symbolic, and the symbol definition
2200 has already been seen. */
2201 if (maybe_dynamic && rel->r_addend == 0)
2202 need_entry = NEED_FULL_PLT;
2203 break;
2204
2205 case R_IA64_IMM14:
2206 case R_IA64_IMM22:
2207 case R_IA64_IMM64:
2208 case R_IA64_DIR32MSB:
2209 case R_IA64_DIR32LSB:
2210 case R_IA64_DIR64MSB:
2211 case R_IA64_DIR64LSB:
2212 /* Shared objects will always need at least a REL relocation. */
7b6dab7f 2213 if (info->shared || maybe_dynamic
7b6dab7f 2214 || (elfNN_ia64_aix_vec (abfd->xvec)
64e9ece0 2215 && (!h || strcmp (h->root.root.string,
7b6dab7f 2216 "__GLOB_DATA_PTR") != 0)))
800eeca4
JW
2217 need_entry = NEED_DYNREL;
2218 dynrel_type = R_IA64_DIR64LSB;
2219 break;
2220
18b27f17
RH
2221 case R_IA64_IPLTMSB:
2222 case R_IA64_IPLTLSB:
2223 /* Shared objects will always need at least a REL relocation. */
2224 if (info->shared || maybe_dynamic)
2225 need_entry = NEED_DYNREL;
2226 dynrel_type = R_IA64_IPLTLSB;
2227 break;
2228
748abff6
RH
2229 case R_IA64_PCREL22:
2230 case R_IA64_PCREL64I:
800eeca4
JW
2231 case R_IA64_PCREL32MSB:
2232 case R_IA64_PCREL32LSB:
2233 case R_IA64_PCREL64MSB:
2234 case R_IA64_PCREL64LSB:
2235 if (maybe_dynamic)
2236 need_entry = NEED_DYNREL;
2237 dynrel_type = R_IA64_PCREL64LSB;
2238 break;
2239 }
2240
2241 if (!need_entry)
2242 continue;
2243
2244 if ((need_entry & NEED_FPTR) != 0
2245 && rel->r_addend)
2246 {
2247 (*info->callbacks->warning)
2248 (info, _("non-zero addend in @fptr reloc"), 0,
dc810e39 2249 abfd, 0, (bfd_vma) 0);
800eeca4
JW
2250 }
2251
2252 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, true);
2253
2254 /* Record whether or not this is a local symbol. */
2255 dyn_i->h = h;
2256
2257 /* Create what's needed. */
2258 if (need_entry & NEED_GOT)
2259 {
2260 if (!got)
2261 {
2262 got = get_got (abfd, info, ia64_info);
2263 if (!got)
2264 return false;
2265 }
2266 dyn_i->want_got = 1;
2267 }
2268 if (need_entry & NEED_FPTR)
2269 {
2270 if (!fptr)
2271 {
2272 fptr = get_fptr (abfd, info, ia64_info);
2273 if (!fptr)
2274 return false;
2275 }
2276
2277 /* FPTRs for shared libraries are allocated by the dynamic
2278 linker. Make sure this local symbol will appear in the
2279 dynamic symbol table. */
7b6dab7f
TW
2280 if (!h && (info->shared
2281 /* AIX also needs one */
2282 || elfNN_ia64_aix_vec (abfd->xvec)))
800eeca4 2283 {
bbe66d08 2284 if (! (_bfd_elfNN_link_record_local_dynamic_symbol
dc810e39 2285 (info, abfd, (long) r_symndx)))
800eeca4
JW
2286 return false;
2287 }
2288
2289 dyn_i->want_fptr = 1;
2290 }
2291 if (need_entry & NEED_LTOFF_FPTR)
2292 dyn_i->want_ltoff_fptr = 1;
2293 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
2294 {
2295 if (!ia64_info->root.dynobj)
2296 ia64_info->root.dynobj = abfd;
2297 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2298 dyn_i->want_plt = 1;
2299 }
2300 if (need_entry & NEED_FULL_PLT)
2301 dyn_i->want_plt2 = 1;
2302 if (need_entry & NEED_PLTOFF)
2303 dyn_i->want_pltoff = 1;
2304 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
2305 {
2306 if (!srel)
2307 {
2308 srel = get_reloc_section (abfd, ia64_info, sec, true);
2309 if (!srel)
2310 return false;
2311 }
2312 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
2313 return false;
2314 }
2315 }
2316
2317 return true;
2318}
2319
bbe66d08 2320struct elfNN_ia64_allocate_data
800eeca4
JW
2321{
2322 struct bfd_link_info *info;
2323 bfd_size_type ofs;
2324};
2325
2326/* For cleanliness, and potentially faster dynamic loading, allocate
2327 external GOT entries first. */
2328
2329static boolean
2330allocate_global_data_got (dyn_i, data)
bbe66d08 2331 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2332 PTR data;
2333{
bbe66d08 2334 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
2335
2336 if (dyn_i->want_got
2337 && ! dyn_i->want_fptr
7b6dab7f 2338 && (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
64e9ece0
TW
2339 || (elfNN_ia64_aix_vec (x->info->hash->creator)
2340 && (!dyn_i->h || strcmp (dyn_i->h->root.root.string,
2341 "__GLOB_DATA_PTR") != 0))))
800eeca4
JW
2342 {
2343 dyn_i->got_offset = x->ofs;
2344 x->ofs += 8;
2345 }
2346 return true;
2347}
2348
2349/* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
2350
2351static boolean
2352allocate_global_fptr_got (dyn_i, data)
bbe66d08 2353 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2354 PTR data;
2355{
bbe66d08 2356 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
2357
2358 if (dyn_i->want_got
2359 && dyn_i->want_fptr
7b6dab7f
TW
2360 && (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2361 || elfNN_ia64_aix_vec (x->info->hash->creator)))
800eeca4
JW
2362 {
2363 dyn_i->got_offset = x->ofs;
2364 x->ofs += 8;
2365 }
2366 return true;
2367}
2368
2369/* Lastly, allocate all the GOT entries for local data. */
2370
2371static boolean
2372allocate_local_got (dyn_i, data)
bbe66d08 2373 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2374 PTR data;
2375{
bbe66d08 2376 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
2377
2378 if (dyn_i->want_got
7b6dab7f
TW
2379 && ! (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2380 || elfNN_ia64_aix_vec (x->info->hash->creator)))
800eeca4
JW
2381 {
2382 dyn_i->got_offset = x->ofs;
2383 x->ofs += 8;
2384 }
2385 return true;
2386}
2387
2388/* Search for the index of a global symbol in it's defining object file. */
2389
dc810e39 2390static long
800eeca4
JW
2391global_sym_index (h)
2392 struct elf_link_hash_entry *h;
2393{
2394 struct elf_link_hash_entry **p;
2395 bfd *obj;
2396
2397 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2398 || h->root.type == bfd_link_hash_defweak);
2399
2400 obj = h->root.u.def.section->owner;
2401 for (p = elf_sym_hashes (obj); *p != h; ++p)
2402 continue;
2403
2404 return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
2405}
2406
2407/* Allocate function descriptors. We can do these for every function
2408 in a main executable that is not exported. */
2409
2410static boolean
2411allocate_fptr (dyn_i, data)
bbe66d08 2412 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2413 PTR data;
2414{
bbe66d08 2415 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
2416
2417 if (dyn_i->want_fptr)
2418 {
2419 struct elf_link_hash_entry *h = dyn_i->h;
3e932841 2420
800eeca4
JW
2421 if (h)
2422 while (h->root.type == bfd_link_hash_indirect
2423 || h->root.type == bfd_link_hash_warning)
2424 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2425
7b6dab7f
TW
2426 if (x->info->shared
2427 /* AIX needs an FPTR in this case. */
2428 || (elfNN_ia64_aix_vec (x->info->hash->creator)
2429 && (!h
2430 || h->root.type == bfd_link_hash_defined
2431 || h->root.type == bfd_link_hash_defweak)))
800eeca4
JW
2432 {
2433 if (h && h->dynindx == -1)
2434 {
2435 BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
2436 || (h->root.type == bfd_link_hash_defweak));
2437
bbe66d08 2438 if (!_bfd_elfNN_link_record_local_dynamic_symbol
800eeca4
JW
2439 (x->info, h->root.u.def.section->owner,
2440 global_sym_index (h)))
2441 return false;
2442 }
2443
2444 dyn_i->want_fptr = 0;
2445 }
2446 else if (h == NULL || h->dynindx == -1)
2447 {
2448 dyn_i->fptr_offset = x->ofs;
2449 x->ofs += 16;
2450 }
2451 else
2452 dyn_i->want_fptr = 0;
2453 }
2454 return true;
2455}
2456
2457/* Allocate all the minimal PLT entries. */
2458
2459static boolean
2460allocate_plt_entries (dyn_i, data)
bbe66d08 2461 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2462 PTR data;
2463{
bbe66d08 2464 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
2465
2466 if (dyn_i->want_plt)
2467 {
2468 struct elf_link_hash_entry *h = dyn_i->h;
2469
2470 if (h)
2471 while (h->root.type == bfd_link_hash_indirect
2472 || h->root.type == bfd_link_hash_warning)
2473 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2474
2475 /* ??? Versioned symbols seem to lose ELF_LINK_HASH_NEEDS_PLT. */
bbe66d08 2476 if (elfNN_ia64_dynamic_symbol_p (h, x->info))
800eeca4
JW
2477 {
2478 bfd_size_type offset = x->ofs;
2479 if (offset == 0)
2480 offset = PLT_HEADER_SIZE;
2481 dyn_i->plt_offset = offset;
2482 x->ofs = offset + PLT_MIN_ENTRY_SIZE;
2483
2484 dyn_i->want_pltoff = 1;
2485 }
2486 else
2487 {
2488 dyn_i->want_plt = 0;
2489 dyn_i->want_plt2 = 0;
2490 }
2491 }
2492 return true;
2493}
2494
2495/* Allocate all the full PLT entries. */
2496
2497static boolean
2498allocate_plt2_entries (dyn_i, data)
bbe66d08 2499 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2500 PTR data;
2501{
bbe66d08 2502 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
2503
2504 if (dyn_i->want_plt2)
2505 {
2506 struct elf_link_hash_entry *h = dyn_i->h;
2507 bfd_size_type ofs = x->ofs;
2508
2509 dyn_i->plt2_offset = ofs;
2510 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2511
2512 while (h->root.type == bfd_link_hash_indirect
2513 || h->root.type == bfd_link_hash_warning)
2514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2515 dyn_i->h->plt.offset = ofs;
2516 }
2517 return true;
2518}
2519
2520/* Allocate all the PLTOFF entries requested by relocations and
2521 plt entries. We can't share space with allocated FPTR entries,
2522 because the latter are not necessarily addressable by the GP.
2523 ??? Relaxation might be able to determine that they are. */
2524
2525static boolean
2526allocate_pltoff_entries (dyn_i, data)
bbe66d08 2527 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2528 PTR data;
2529{
bbe66d08 2530 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
2531
2532 if (dyn_i->want_pltoff)
2533 {
2534 dyn_i->pltoff_offset = x->ofs;
2535 x->ofs += 16;
2536 }
2537 return true;
2538}
2539
2540/* Allocate dynamic relocations for those symbols that turned out
2541 to be dynamic. */
2542
2543static boolean
2544allocate_dynrel_entries (dyn_i, data)
bbe66d08 2545 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
2546 PTR data;
2547{
bbe66d08
JW
2548 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2549 struct elfNN_ia64_link_hash_table *ia64_info;
2550 struct elfNN_ia64_dyn_reloc_entry *rent;
800eeca4
JW
2551 boolean dynamic_symbol, shared;
2552
bbe66d08 2553 ia64_info = elfNN_ia64_hash_table (x->info);
7b6dab7f 2554 dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
64e9ece0
TW
2555 || (elfNN_ia64_aix_vec (x->info->hash->creator)
2556 /* Don't allocate an entry for __GLOB_DATA_PTR */
2557 && (!dyn_i->h || strcmp (dyn_i->h->root.root.string,
2558 "__GLOB_DATA_PTR") != 0));
800eeca4
JW
2559 shared = x->info->shared;
2560
2561 /* Take care of the normal data relocations. */
2562
2563 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2564 {
18b27f17
RH
2565 int count = rent->count;
2566
800eeca4
JW
2567 switch (rent->type)
2568 {
2569 case R_IA64_FPTR64LSB:
2570 /* Allocate one iff !want_fptr, which by this point will
2571 be true only if we're actually allocating one statically
2572 in the main executable. */
2573 if (dyn_i->want_fptr)
2574 continue;
2575 break;
2576 case R_IA64_PCREL64LSB:
2577 if (!dynamic_symbol)
2578 continue;
2579 break;
2580 case R_IA64_DIR64LSB:
2581 if (!dynamic_symbol && !shared)
2582 continue;
2583 break;
18b27f17
RH
2584 case R_IA64_IPLTLSB:
2585 if (!dynamic_symbol && !shared)
2586 continue;
2587 /* Use two REL relocations for IPLT relocations
2588 against local symbols. */
2589 if (!dynamic_symbol)
2590 count *= 2;
2591 break;
2592 default:
2593 abort ();
800eeca4 2594 }
18b27f17 2595 rent->srel->_raw_size += sizeof (ElfNN_External_Rela) * count;
800eeca4
JW
2596 }
2597
2598 /* Take care of the GOT and PLT relocations. */
2599
2600 if (((dynamic_symbol || shared) && dyn_i->want_got)
2601 || (dyn_i->want_ltoff_fptr && dyn_i->h && dyn_i->h->dynindx != -1))
bbe66d08 2602 ia64_info->rel_got_sec->_raw_size += sizeof (ElfNN_External_Rela);
800eeca4
JW
2603
2604 if (dyn_i->want_pltoff)
2605 {
2606 bfd_size_type t = 0;
2607
2608 /* Dynamic symbols get one IPLT relocation. Local symbols in
2609 shared libraries get two REL relocations. Local symbols in
2610 main applications get nothing. */
2611 if (dynamic_symbol)
bbe66d08 2612 t = sizeof (ElfNN_External_Rela);
800eeca4 2613 else if (shared)
bbe66d08 2614 t = 2 * sizeof (ElfNN_External_Rela);
800eeca4
JW
2615
2616 ia64_info->rel_pltoff_sec->_raw_size += t;
2617 }
2618
2619 return true;
2620}
2621
2622static boolean
bbe66d08 2623elfNN_ia64_adjust_dynamic_symbol (info, h)
64bf6ae6 2624 struct bfd_link_info *info ATTRIBUTE_UNUSED;
800eeca4
JW
2625 struct elf_link_hash_entry *h;
2626{
2627 /* ??? Undefined symbols with PLT entries should be re-defined
2628 to be the PLT entry. */
2629
2630 /* If this is a weak symbol, and there is a real definition, the
2631 processor independent code will have arranged for us to see the
2632 real definition first, and we can just use the same value. */
2633 if (h->weakdef != NULL)
2634 {
2635 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2636 || h->weakdef->root.type == bfd_link_hash_defweak);
2637 h->root.u.def.section = h->weakdef->root.u.def.section;
2638 h->root.u.def.value = h->weakdef->root.u.def.value;
2639 return true;
2640 }
2641
2642 /* If this is a reference to a symbol defined by a dynamic object which
2643 is not a function, we might allocate the symbol in our .dynbss section
2644 and allocate a COPY dynamic relocation.
2645
2646 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2647 of hackery. */
2648
2649 return true;
2650}
2651
2652static boolean
bbe66d08 2653elfNN_ia64_size_dynamic_sections (output_bfd, info)
800eeca4
JW
2654 bfd *output_bfd;
2655 struct bfd_link_info *info;
2656{
bbe66d08
JW
2657 struct elfNN_ia64_allocate_data data;
2658 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
2659 asection *sec;
2660 bfd *dynobj;
800eeca4
JW
2661 boolean relplt = false;
2662
2663 dynobj = elf_hash_table(info)->dynobj;
bbe66d08 2664 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
2665 BFD_ASSERT(dynobj != NULL);
2666 data.info = info;
2667
2668 /* Set the contents of the .interp section to the interpreter. */
2669 if (ia64_info->root.dynamic_sections_created
2670 && !info->shared)
2671 {
2672 sec = bfd_get_section_by_name (dynobj, ".interp");
2673 BFD_ASSERT (sec != NULL);
7b6dab7f
TW
2674 sec->contents = (bfd_byte *) DYNAMIC_INTERPRETER (output_bfd);
2675 sec->_raw_size = strlen (DYNAMIC_INTERPRETER (output_bfd)) + 1;
800eeca4
JW
2676 }
2677
800eeca4
JW
2678 /* Allocate the GOT entries. */
2679
2680 if (ia64_info->got_sec)
2681 {
2682 data.ofs = 0;
bbe66d08
JW
2683 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2684 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2685 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
800eeca4
JW
2686 ia64_info->got_sec->_raw_size = data.ofs;
2687 }
2688
2689 /* Allocate the FPTR entries. */
2690
2691 if (ia64_info->fptr_sec)
2692 {
2693 data.ofs = 0;
bbe66d08 2694 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
800eeca4
JW
2695 ia64_info->fptr_sec->_raw_size = data.ofs;
2696 }
2697
2698 /* Now that we've seen all of the input files, we can decide which
2699 symbols need plt entries. Allocate the minimal PLT entries first.
2700 We do this even though dynamic_sections_created may be false, because
2701 this has the side-effect of clearing want_plt and want_plt2. */
2702
2703 data.ofs = 0;
bbe66d08 2704 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
800eeca4
JW
2705
2706 ia64_info->minplt_entries = 0;
2707 if (data.ofs)
2708 {
2709 ia64_info->minplt_entries
2710 = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
2711 }
2712
2713 /* Align the pointer for the plt2 entries. */
dc810e39 2714 data.ofs = (data.ofs + 31) & (bfd_vma) -32;
800eeca4 2715
bbe66d08 2716 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
800eeca4
JW
2717 if (data.ofs != 0)
2718 {
2719 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2720
2721 ia64_info->plt_sec->_raw_size = data.ofs;
2722
2723 /* If we've got a .plt, we need some extra memory for the dynamic
2724 linker. We stuff these in .got.plt. */
2725 sec = bfd_get_section_by_name (dynobj, ".got.plt");
2726 sec->_raw_size = 8 * PLT_RESERVED_WORDS;
2727 }
2728
2729 /* Allocate the PLTOFF entries. */
2730
2731 if (ia64_info->pltoff_sec)
2732 {
2733 data.ofs = 0;
bbe66d08 2734 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
800eeca4
JW
2735 ia64_info->pltoff_sec->_raw_size = data.ofs;
2736 }
2737
2738 if (ia64_info->root.dynamic_sections_created)
2739 {
2740 /* Allocate space for the dynamic relocations that turned out to be
2741 required. */
2742
bbe66d08 2743 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
800eeca4
JW
2744 }
2745
2746 /* We have now determined the sizes of the various dynamic sections.
2747 Allocate memory for them. */
2748 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2749 {
2750 boolean strip;
2751
2752 if (!(sec->flags & SEC_LINKER_CREATED))
2753 continue;
2754
2755 /* If we don't need this section, strip it from the output file.
2756 There were several sections primarily related to dynamic
2757 linking that must be create before the linker maps input
2758 sections to output sections. The linker does that before
2759 bfd_elf_size_dynamic_sections is called, and it is that
2760 function which decides whether anything needs to go into
2761 these sections. */
2762
2763 strip = (sec->_raw_size == 0);
2764
2765 if (sec == ia64_info->got_sec)
2766 strip = false;
2767 else if (sec == ia64_info->rel_got_sec)
2768 {
2769 if (strip)
2770 ia64_info->rel_got_sec = NULL;
2771 else
2772 /* We use the reloc_count field as a counter if we need to
2773 copy relocs into the output file. */
2774 sec->reloc_count = 0;
2775 }
2776 else if (sec == ia64_info->fptr_sec)
2777 {
2778 if (strip)
2779 ia64_info->fptr_sec = NULL;
2780 }
2781 else if (sec == ia64_info->plt_sec)
2782 {
2783 if (strip)
2784 ia64_info->plt_sec = NULL;
2785 }
2786 else if (sec == ia64_info->pltoff_sec)
2787 {
2788 if (strip)
2789 ia64_info->pltoff_sec = NULL;
2790 }
2791 else if (sec == ia64_info->rel_pltoff_sec)
2792 {
2793 if (strip)
2794 ia64_info->rel_pltoff_sec = NULL;
2795 else
2796 {
2797 relplt = true;
2798 /* We use the reloc_count field as a counter if we need to
2799 copy relocs into the output file. */
2800 sec->reloc_count = 0;
2801 }
2802 }
2803 else
2804 {
2805 const char *name;
2806
2807 /* It's OK to base decisions on the section name, because none
2808 of the dynobj section names depend upon the input files. */
2809 name = bfd_get_section_name (dynobj, sec);
2810
2811 if (strcmp (name, ".got.plt") == 0)
2812 strip = false;
2813 else if (strncmp (name, ".rel", 4) == 0)
2814 {
2815 if (!strip)
2816 {
800eeca4
JW
2817 /* We use the reloc_count field as a counter if we need to
2818 copy relocs into the output file. */
2819 sec->reloc_count = 0;
2820 }
2821 }
2822 else
2823 continue;
2824 }
2825
2826 if (strip)
2827 _bfd_strip_section_from_output (info, sec);
2828 else
2829 {
2830 /* Allocate memory for the section contents. */
dc810e39 2831 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->_raw_size);
800eeca4
JW
2832 if (sec->contents == NULL && sec->_raw_size != 0)
2833 return false;
2834 }
2835 }
2836
2837 if (elf_hash_table (info)->dynamic_sections_created)
2838 {
2839 /* Add some entries to the .dynamic section. We fill in the values
2840 later (in finish_dynamic_sections) but we must add the entries now
2841 so that we get the correct size for the .dynamic section. */
2842
2843 if (!info->shared)
2844 {
2845 /* The DT_DEBUG entry is filled in by the dynamic linker and used
2846 by the debugger. */
dc810e39
AM
2847#define add_dynamic_entry(TAG, VAL) \
2848 bfd_elfNN_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2849
2850 if (!add_dynamic_entry (DT_DEBUG, 0))
800eeca4
JW
2851 return false;
2852 }
2853
dc810e39 2854 if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
800eeca4 2855 return false;
dc810e39 2856 if (!add_dynamic_entry (DT_PLTGOT, 0))
800eeca4
JW
2857 return false;
2858
2859 if (relplt)
2860 {
dc810e39
AM
2861 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2862 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2863 || !add_dynamic_entry (DT_JMPREL, 0))
800eeca4
JW
2864 return false;
2865 }
2866
dc810e39
AM
2867 if (!add_dynamic_entry (DT_RELA, 0)
2868 || !add_dynamic_entry (DT_RELASZ, 0)
2869 || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
800eeca4
JW
2870 return false;
2871
db6751f2 2872 if (ia64_info->reltext)
800eeca4 2873 {
dc810e39 2874 if (!add_dynamic_entry (DT_TEXTREL, 0))
800eeca4 2875 return false;
d6cf2879 2876 info->flags |= DF_TEXTREL;
800eeca4
JW
2877 }
2878 }
2879
2880 /* ??? Perhaps force __gp local. */
2881
2882 return true;
2883}
2884
2885static bfd_reloc_status_type
1e738b87 2886elfNN_ia64_install_value (abfd, hit_addr, v, r_type)
800eeca4
JW
2887 bfd *abfd;
2888 bfd_byte *hit_addr;
1e738b87 2889 bfd_vma v;
800eeca4
JW
2890 unsigned int r_type;
2891{
2892 const struct ia64_operand *op;
2893 int bigendian = 0, shift = 0;
2894 bfd_vma t0, t1, insn, dword;
2895 enum ia64_opnd opnd;
2896 const char *err;
2897 size_t size = 8;
1e738b87
NC
2898#ifdef BFD_HOST_U_64_BIT
2899 BFD_HOST_U_64_BIT val = (BFD_HOST_U_64_BIT) v;
2900#else
2901 bfd_vma val = v;
2902#endif
800eeca4
JW
2903
2904 opnd = IA64_OPND_NIL;
2905 switch (r_type)
2906 {
2907 case R_IA64_NONE:
2908 case R_IA64_LDXMOV:
2909 return bfd_reloc_ok;
2910
3e932841 2911 /* Instruction relocations. */
800eeca4
JW
2912
2913 case R_IA64_IMM14: opnd = IA64_OPND_IMM14; break;
748abff6 2914
800eeca4
JW
2915 case R_IA64_PCREL21F: opnd = IA64_OPND_TGT25; break;
2916 case R_IA64_PCREL21M: opnd = IA64_OPND_TGT25b; break;
748abff6
RH
2917 case R_IA64_PCREL60B: opnd = IA64_OPND_TGT64; break;
2918 case R_IA64_PCREL21B:
2919 case R_IA64_PCREL21BI:
2920 opnd = IA64_OPND_TGT25c;
2921 break;
800eeca4
JW
2922
2923 case R_IA64_IMM22:
2924 case R_IA64_GPREL22:
2925 case R_IA64_LTOFF22:
2926 case R_IA64_LTOFF22X:
2927 case R_IA64_PLTOFF22:
748abff6 2928 case R_IA64_PCREL22:
800eeca4
JW
2929 case R_IA64_LTOFF_FPTR22:
2930 opnd = IA64_OPND_IMM22;
2931 break;
2932
2933 case R_IA64_IMM64:
2934 case R_IA64_GPREL64I:
2935 case R_IA64_LTOFF64I:
2936 case R_IA64_PLTOFF64I:
748abff6 2937 case R_IA64_PCREL64I:
800eeca4
JW
2938 case R_IA64_FPTR64I:
2939 case R_IA64_LTOFF_FPTR64I:
2940 opnd = IA64_OPND_IMMU64;
2941 break;
2942
2943 /* Data relocations. */
2944
2945 case R_IA64_DIR32MSB:
2946 case R_IA64_GPREL32MSB:
2947 case R_IA64_FPTR32MSB:
2948 case R_IA64_PCREL32MSB:
a4bd8390 2949 case R_IA64_LTOFF_FPTR32MSB:
800eeca4
JW
2950 case R_IA64_SEGREL32MSB:
2951 case R_IA64_SECREL32MSB:
2952 case R_IA64_LTV32MSB:
2953 size = 4; bigendian = 1;
2954 break;
2955
2956 case R_IA64_DIR32LSB:
2957 case R_IA64_GPREL32LSB:
2958 case R_IA64_FPTR32LSB:
2959 case R_IA64_PCREL32LSB:
a4bd8390 2960 case R_IA64_LTOFF_FPTR32LSB:
800eeca4
JW
2961 case R_IA64_SEGREL32LSB:
2962 case R_IA64_SECREL32LSB:
2963 case R_IA64_LTV32LSB:
2964 size = 4; bigendian = 0;
2965 break;
2966
2967 case R_IA64_DIR64MSB:
2968 case R_IA64_GPREL64MSB:
2969 case R_IA64_PLTOFF64MSB:
2970 case R_IA64_FPTR64MSB:
2971 case R_IA64_PCREL64MSB:
2972 case R_IA64_LTOFF_FPTR64MSB:
2973 case R_IA64_SEGREL64MSB:
2974 case R_IA64_SECREL64MSB:
2975 case R_IA64_LTV64MSB:
2976 size = 8; bigendian = 1;
2977 break;
2978
2979 case R_IA64_DIR64LSB:
2980 case R_IA64_GPREL64LSB:
2981 case R_IA64_PLTOFF64LSB:
2982 case R_IA64_FPTR64LSB:
2983 case R_IA64_PCREL64LSB:
2984 case R_IA64_LTOFF_FPTR64LSB:
2985 case R_IA64_SEGREL64LSB:
2986 case R_IA64_SECREL64LSB:
2987 case R_IA64_LTV64LSB:
2988 size = 8; bigendian = 0;
2989 break;
2990
2991 /* Unsupported / Dynamic relocations. */
800eeca4
JW
2992 default:
2993 return bfd_reloc_notsupported;
2994 }
2995
2996 switch (opnd)
2997 {
2998 case IA64_OPND_IMMU64:
2999 hit_addr -= (long) hit_addr & 0x3;
3000 t0 = bfd_get_64 (abfd, hit_addr);
3001 t1 = bfd_get_64 (abfd, hit_addr + 8);
3002
3003 /* tmpl/s: bits 0.. 5 in t0
3004 slot 0: bits 5..45 in t0
3005 slot 1: bits 46..63 in t0, bits 0..22 in t1
3006 slot 2: bits 23..63 in t1 */
3007
3008 /* First, clear the bits that form the 64 bit constant. */
3009 t0 &= ~(0x3ffffLL << 46);
3010 t1 &= ~(0x7fffffLL
3011 | (( (0x07fLL << 13) | (0x1ffLL << 27)
3012 | (0x01fLL << 22) | (0x001LL << 21)
3013 | (0x001LL << 36)) << 23));
3014
3015 t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */
3016 t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */
3017 t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */
3018 | (((val >> 7) & 0x1ff) << 27) /* imm9d */
3019 | (((val >> 16) & 0x01f) << 22) /* imm5c */
3020 | (((val >> 21) & 0x001) << 21) /* ic */
3021 | (((val >> 63) & 0x001) << 36)) << 23; /* i */
3022
3023 bfd_put_64 (abfd, t0, hit_addr);
3024 bfd_put_64 (abfd, t1, hit_addr + 8);
3025 break;
3026
748abff6
RH
3027 case IA64_OPND_TGT64:
3028 hit_addr -= (long) hit_addr & 0x3;
3029 t0 = bfd_get_64 (abfd, hit_addr);
3030 t1 = bfd_get_64 (abfd, hit_addr + 8);
3031
3032 /* tmpl/s: bits 0.. 5 in t0
3033 slot 0: bits 5..45 in t0
3034 slot 1: bits 46..63 in t0, bits 0..22 in t1
3035 slot 2: bits 23..63 in t1 */
3036
3037 /* First, clear the bits that form the 64 bit constant. */
3038 t0 &= ~(0x3ffffLL << 46);
3039 t1 &= ~(0x7fffffLL
3040 | ((1LL << 36 | 0xfffffLL << 13) << 23));
3041
3042 val >>= 4;
3043 t0 |= ((val >> 20) & 0xffffLL) << 2 << 46; /* 16 lsbs of imm39 */
3044 t1 |= ((val >> 36) & 0x7fffffLL) << 0; /* 23 msbs of imm39 */
3045 t1 |= ((((val >> 0) & 0xfffffLL) << 13) /* imm20b */
3046 | (((val >> 59) & 0x1LL) << 36)) << 23; /* i */
3047
3048 bfd_put_64 (abfd, t0, hit_addr);
3049 bfd_put_64 (abfd, t1, hit_addr + 8);
3050 break;
3051
800eeca4
JW
3052 default:
3053 switch ((long) hit_addr & 0x3)
3054 {
3055 case 0: shift = 5; break;
3056 case 1: shift = 14; hit_addr += 3; break;
3057 case 2: shift = 23; hit_addr += 6; break;
3e932841 3058 case 3: return bfd_reloc_notsupported; /* shouldn't happen... */
800eeca4
JW
3059 }
3060 dword = bfd_get_64 (abfd, hit_addr);
3061 insn = (dword >> shift) & 0x1ffffffffffLL;
3062
3063 op = elf64_ia64_operands + opnd;
1e738b87 3064 err = (*op->insert) (op, val, (ia64_insn *)& insn);
800eeca4
JW
3065 if (err)
3066 return bfd_reloc_overflow;
3067
3068 dword &= ~(0x1ffffffffffLL << shift);
3069 dword |= (insn << shift);
3070 bfd_put_64 (abfd, dword, hit_addr);
3071 break;
3072
3073 case IA64_OPND_NIL:
3074 /* A data relocation. */
3075 if (bigendian)
3076 if (size == 4)
3077 bfd_putb32 (val, hit_addr);
3078 else
3079 bfd_putb64 (val, hit_addr);
3080 else
3081 if (size == 4)
3082 bfd_putl32 (val, hit_addr);
3083 else
3084 bfd_putl64 (val, hit_addr);
3085 break;
3086 }
3087
3088 return bfd_reloc_ok;
3089}
3090
3091static void
bbe66d08 3092elfNN_ia64_install_dyn_reloc (abfd, info, sec, srel, offset, type,
800eeca4
JW
3093 dynindx, addend)
3094 bfd *abfd;
3095 struct bfd_link_info *info;
3096 asection *sec;
3097 asection *srel;
3098 bfd_vma offset;
3099 unsigned int type;
3100 long dynindx;
3101 bfd_vma addend;
3102{
3103 Elf_Internal_Rela outrel;
3104
c629eae0 3105 offset += sec->output_section->vma + sec->output_offset;
800eeca4
JW
3106
3107 BFD_ASSERT (dynindx != -1);
bbe66d08 3108 outrel.r_info = ELFNN_R_INFO (dynindx, type);
800eeca4 3109 outrel.r_addend = addend;
c629eae0
JJ
3110 outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
3111 if (outrel.r_offset == (bfd_vma) -1)
800eeca4 3112 {
c629eae0
JJ
3113 /* Run for the hills. We shouldn't be outputting a relocation
3114 for this. So do what everyone else does and output a no-op. */
3115 outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
3116 outrel.r_addend = 0;
3117 outrel.r_offset = 0;
800eeca4
JW
3118 }
3119
bbe66d08
JW
3120 bfd_elfNN_swap_reloca_out (abfd, &outrel,
3121 ((ElfNN_External_Rela *) srel->contents
800eeca4 3122 + srel->reloc_count++));
3e932841 3123 BFD_ASSERT (sizeof (ElfNN_External_Rela) * srel->reloc_count
800eeca4
JW
3124 <= srel->_cooked_size);
3125}
3126
3127/* Store an entry for target address TARGET_ADDR in the linkage table
3128 and return the gp-relative address of the linkage table entry. */
3129
3130static bfd_vma
3131set_got_entry (abfd, info, dyn_i, dynindx, addend, value, dyn_r_type)
3132 bfd *abfd;
3133 struct bfd_link_info *info;
bbe66d08 3134 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
3135 long dynindx;
3136 bfd_vma addend;
3137 bfd_vma value;
3138 unsigned int dyn_r_type;
3139{
bbe66d08 3140 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
3141 asection *got_sec;
3142
bbe66d08 3143 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
3144 got_sec = ia64_info->got_sec;
3145
3146 BFD_ASSERT ((dyn_i->got_offset & 7) == 0);
3147
3148 if (! dyn_i->got_done)
3149 {
3150 dyn_i->got_done = true;
3151
3152 /* Store the target address in the linkage table entry. */
3153 bfd_put_64 (abfd, value, got_sec->contents + dyn_i->got_offset);
3154
3155 /* Install a dynamic relocation if needed. */
3156 if (info->shared
bbe66d08 3157 || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info)
7b6dab7f 3158 || elfNN_ia64_aix_vec (abfd->xvec)
800eeca4
JW
3159 || (dynindx != -1 && dyn_r_type == R_IA64_FPTR64LSB))
3160 {
3161 if (dynindx == -1)
3162 {
3163 dyn_r_type = R_IA64_REL64LSB;
3164 dynindx = 0;
3165 addend = value;
3166 }
3167
3168 if (bfd_big_endian (abfd))
3169 {
3170 switch (dyn_r_type)
3171 {
3172 case R_IA64_REL64LSB:
3173 dyn_r_type = R_IA64_REL64MSB;
3174 break;
3175 case R_IA64_DIR64LSB:
3176 dyn_r_type = R_IA64_DIR64MSB;
3177 break;
3178 case R_IA64_FPTR64LSB:
3179 dyn_r_type = R_IA64_FPTR64MSB;
3180 break;
3181 default:
3182 BFD_ASSERT (false);
3183 break;
3184 }
3185 }
3186
bbe66d08 3187 elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
800eeca4
JW
3188 ia64_info->rel_got_sec,
3189 dyn_i->got_offset, dyn_r_type,
3190 dynindx, addend);
3191 }
3192 }
3193
3194 /* Return the address of the linkage table entry. */
3195 value = (got_sec->output_section->vma
3196 + got_sec->output_offset
3197 + dyn_i->got_offset);
3198
3199 return value;
3200}
3201
3202/* Fill in a function descriptor consisting of the function's code
3203 address and its global pointer. Return the descriptor's address. */
3204
3205static bfd_vma
3206set_fptr_entry (abfd, info, dyn_i, value)
3207 bfd *abfd;
3208 struct bfd_link_info *info;
bbe66d08 3209 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
3210 bfd_vma value;
3211{
bbe66d08 3212 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
3213 asection *fptr_sec;
3214
bbe66d08 3215 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
3216 fptr_sec = ia64_info->fptr_sec;
3217
3218 if (!dyn_i->fptr_done)
3219 {
3220 dyn_i->fptr_done = 1;
3221
3222 /* Fill in the function descriptor. */
3223 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
3224 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
3225 fptr_sec->contents + dyn_i->fptr_offset + 8);
3226 }
3227
3228 /* Return the descriptor's address. */
3229 value = (fptr_sec->output_section->vma
3230 + fptr_sec->output_offset
3231 + dyn_i->fptr_offset);
3232
3233 return value;
3234}
3235
3236/* Fill in a PLTOFF entry consisting of the function's code address
3237 and its global pointer. Return the descriptor's address. */
3238
3239static bfd_vma
3240set_pltoff_entry (abfd, info, dyn_i, value, is_plt)
3241 bfd *abfd;
3242 struct bfd_link_info *info;
bbe66d08 3243 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
3244 bfd_vma value;
3245 boolean is_plt;
3246{
bbe66d08 3247 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
3248 asection *pltoff_sec;
3249
bbe66d08 3250 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
3251 pltoff_sec = ia64_info->pltoff_sec;
3252
3253 /* Don't do anything if this symbol uses a real PLT entry. In
3254 that case, we'll fill this in during finish_dynamic_symbol. */
3255 if ((! dyn_i->want_plt || is_plt)
3256 && !dyn_i->pltoff_done)
3257 {
18b27f17
RH
3258 bfd_vma gp = _bfd_get_gp_value (abfd);
3259
800eeca4
JW
3260 /* Fill in the function descriptor. */
3261 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
18b27f17 3262 bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
800eeca4
JW
3263
3264 /* Install dynamic relocations if needed. */
3265 if (!is_plt && info->shared)
3266 {
3267 unsigned int dyn_r_type;
3268
3269 if (bfd_big_endian (abfd))
3270 dyn_r_type = R_IA64_REL64MSB;
3271 else
3272 dyn_r_type = R_IA64_REL64LSB;
3273
bbe66d08 3274 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
800eeca4
JW
3275 ia64_info->rel_pltoff_sec,
3276 dyn_i->pltoff_offset,
18b27f17 3277 dyn_r_type, 0, value);
bbe66d08 3278 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
800eeca4
JW
3279 ia64_info->rel_pltoff_sec,
3280 dyn_i->pltoff_offset + 8,
18b27f17 3281 dyn_r_type, 0, gp);
800eeca4
JW
3282 }
3283
3284 dyn_i->pltoff_done = 1;
3285 }
3286
3287 /* Return the descriptor's address. */
3288 value = (pltoff_sec->output_section->vma
3289 + pltoff_sec->output_offset
3290 + dyn_i->pltoff_offset);
3291
3292 return value;
3293}
3294
f3b6f7c3 3295/* Called through qsort to sort the .IA_64.unwind section during a
bbe66d08 3296 non-relocatable link. Set elfNN_ia64_unwind_entry_compare_bfd
f3b6f7c3
RH
3297 to the output bfd so we can do proper endianness frobbing. */
3298
bbe66d08 3299static bfd *elfNN_ia64_unwind_entry_compare_bfd;
f3b6f7c3
RH
3300
3301static int
bbe66d08 3302elfNN_ia64_unwind_entry_compare (a, b)
cea4409c
AM
3303 const PTR a;
3304 const PTR b;
f3b6f7c3
RH
3305{
3306 bfd_vma av, bv;
3307
bbe66d08
JW
3308 av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
3309 bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
f3b6f7c3
RH
3310
3311 return (av < bv ? -1 : av > bv ? 1 : 0);
3312}
3313
800eeca4 3314static boolean
bbe66d08 3315elfNN_ia64_final_link (abfd, info)
800eeca4
JW
3316 bfd *abfd;
3317 struct bfd_link_info *info;
3318{
bbe66d08 3319 struct elfNN_ia64_link_hash_table *ia64_info;
9a951beb
RH
3320 asection *unwind_output_sec;
3321
bbe66d08 3322 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
3323
3324 /* Make sure we've got ourselves a nice fat __gp value. */
3325 if (!info->relocateable)
3326 {
3327 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3328 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3329 struct elf_link_hash_entry *gp;
3330 bfd_vma gp_val;
3331 asection *os;
3332
3333 /* Find the min and max vma of all sections marked short. Also
3334 collect min and max vma of any type, for use in selecting a
3335 nice gp. */
3336 for (os = abfd->sections; os ; os = os->next)
3337 {
3338 bfd_vma lo, hi;
3339
3340 if ((os->flags & SEC_ALLOC) == 0)
3341 continue;
3342
3343 lo = os->vma;
3344 hi = os->vma + os->_raw_size;
3345 if (hi < lo)
3346 hi = (bfd_vma) -1;
3347
3348 if (min_vma > lo)
3349 min_vma = lo;
3350 if (max_vma < hi)
3351 max_vma = hi;
3352 if (os->flags & SEC_SMALL_DATA)
3353 {
3354 if (min_short_vma > lo)
3355 min_short_vma = lo;
3356 if (max_short_vma < hi)
3357 max_short_vma = hi;
3358 }
3359 }
3360
3361 /* See if the user wants to force a value. */
3362 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3363 false, false);
3364
3365 if (gp
3366 && (gp->root.type == bfd_link_hash_defined
3367 || gp->root.type == bfd_link_hash_defweak))
3368 {
3369 asection *gp_sec = gp->root.u.def.section;
3370 gp_val = (gp->root.u.def.value
3371 + gp_sec->output_section->vma
3372 + gp_sec->output_offset);
3373 }
3374 else
3375 {
3376 /* Pick a sensible value. */
3377
3378 asection *got_sec = ia64_info->got_sec;
3379
3380 /* Start with just the address of the .got. */
3381 if (got_sec)
3382 gp_val = got_sec->output_section->vma;
3383 else if (max_short_vma != 0)
3384 gp_val = min_short_vma;
3385 else
3386 gp_val = min_vma;
3387
3388 /* If it is possible to address the entire image, but we
3389 don't with the choice above, adjust. */
3390 if (max_vma - min_vma < 0x400000
3391 && max_vma - gp_val <= 0x200000
3392 && gp_val - min_vma > 0x200000)
3393 gp_val = min_vma + 0x200000;
3394 else if (max_short_vma != 0)
3395 {
3396 /* If we don't cover all the short data, adjust. */
3397 if (max_short_vma - gp_val >= 0x200000)
3398 gp_val = min_short_vma + 0x200000;
3399
3400 /* If we're addressing stuff past the end, adjust back. */
3401 if (gp_val > max_vma)
3402 gp_val = max_vma - 0x200000 + 8;
3403 }
3404 }
3405
3406 /* Validate whether all SHF_IA_64_SHORT sections are within
3407 range of the chosen GP. */
3408
3409 if (max_short_vma != 0)
3410 {
3411 if (max_short_vma - min_short_vma >= 0x400000)
3412 {
3413 (*_bfd_error_handler)
3414 (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
3415 bfd_get_filename (abfd),
3e932841 3416 (unsigned long) (max_short_vma - min_short_vma));
800eeca4
JW
3417 return false;
3418 }
3419 else if ((gp_val > min_short_vma
3420 && gp_val - min_short_vma > 0x200000)
3421 || (gp_val < max_short_vma
3422 && max_short_vma - gp_val >= 0x200000))
3423 {
3424 (*_bfd_error_handler)
3425 (_("%s: __gp does not cover short data segment"),
3426 bfd_get_filename (abfd));
3427 return false;
3428 }
3429 }
3430
3431 _bfd_set_gp_value (abfd, gp_val);
b4adccfd
RH
3432
3433 if (gp)
3434 {
3435 gp->root.type = bfd_link_hash_defined;
3436 gp->root.u.def.value = gp_val;
3437 gp->root.u.def.section = bfd_abs_section_ptr;
3438 }
800eeca4
JW
3439 }
3440
f3b6f7c3 3441 /* If we're producing a final executable, we need to sort the contents
9a951beb
RH
3442 of the .IA_64.unwind section. Force this section to be relocated
3443 into memory rather than written immediately to the output file. */
3444 unwind_output_sec = NULL;
f3b6f7c3
RH
3445 if (!info->relocateable)
3446 {
3447 asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3448 if (s)
3449 {
9a951beb
RH
3450 unwind_output_sec = s->output_section;
3451 unwind_output_sec->contents
3452 = bfd_malloc (unwind_output_sec->_raw_size);
3453 if (unwind_output_sec->contents == NULL)
f3b6f7c3 3454 return false;
9a951beb
RH
3455 }
3456 }
f3b6f7c3 3457
9a951beb
RH
3458 /* Invoke the regular ELF backend linker to do all the work. */
3459 if (!bfd_elfNN_bfd_final_link (abfd, info))
3460 return false;
f3b6f7c3 3461
9a951beb
RH
3462 if (unwind_output_sec)
3463 {
3464 elfNN_ia64_unwind_entry_compare_bfd = abfd;
dc810e39
AM
3465 qsort (unwind_output_sec->contents,
3466 (size_t) (unwind_output_sec->_raw_size / 24),
3467 24,
3468 elfNN_ia64_unwind_entry_compare);
9a951beb
RH
3469
3470 if (! bfd_set_section_contents (abfd, unwind_output_sec,
dc810e39 3471 unwind_output_sec->contents, (bfd_vma) 0,
9a951beb
RH
3472 unwind_output_sec->_raw_size))
3473 return false;
f3b6f7c3
RH
3474 }
3475
3476 return true;
800eeca4
JW
3477}
3478
3479static boolean
bbe66d08 3480elfNN_ia64_relocate_section (output_bfd, info, input_bfd, input_section,
800eeca4
JW
3481 contents, relocs, local_syms, local_sections)
3482 bfd *output_bfd;
3483 struct bfd_link_info *info;
3484 bfd *input_bfd;
3485 asection *input_section;
3486 bfd_byte *contents;
3487 Elf_Internal_Rela *relocs;
3488 Elf_Internal_Sym *local_syms;
3489 asection **local_sections;
3490{
bbe66d08 3491 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
3492 Elf_Internal_Shdr *symtab_hdr;
3493 Elf_Internal_Rela *rel;
3494 Elf_Internal_Rela *relend;
3495 asection *srel;
3496 boolean ret_val = true; /* for non-fatal errors */
3497 bfd_vma gp_val;
3498
3499 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
bbe66d08 3500 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
3501
3502 /* Infect various flags from the input section to the output section. */
3503 if (info->relocateable)
3504 {
3505 bfd_vma flags;
3506
3507 flags = elf_section_data(input_section)->this_hdr.sh_flags;
3508 flags &= SHF_IA_64_NORECOV;
3509
3510 elf_section_data(input_section->output_section)
3511 ->this_hdr.sh_flags |= flags;
3512 }
3513
3514 gp_val = _bfd_get_gp_value (output_bfd);
3515 srel = get_reloc_section (input_bfd, ia64_info, input_section, false);
3516
3517 rel = relocs;
3518 relend = relocs + input_section->reloc_count;
3519 for (; rel < relend; ++rel)
3520 {
3521 struct elf_link_hash_entry *h;
bbe66d08 3522 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
3523 bfd_reloc_status_type r;
3524 reloc_howto_type *howto;
3525 unsigned long r_symndx;
3526 Elf_Internal_Sym *sym;
3527 unsigned int r_type;
3528 bfd_vma value;
3529 asection *sym_sec;
3530 bfd_byte *hit_addr;
3531 boolean dynamic_symbol_p;
3532 boolean undef_weak_ref;
3533
bbe66d08 3534 r_type = ELFNN_R_TYPE (rel->r_info);
800eeca4
JW
3535 if (r_type > R_IA64_MAX_RELOC_CODE)
3536 {
3537 (*_bfd_error_handler)
3538 (_("%s: unknown relocation type %d"),
8f615d07 3539 bfd_archive_filename (input_bfd), (int)r_type);
800eeca4
JW
3540 bfd_set_error (bfd_error_bad_value);
3541 ret_val = false;
3542 continue;
3543 }
3544 howto = lookup_howto (r_type);
bbe66d08 3545 r_symndx = ELFNN_R_SYM (rel->r_info);
800eeca4
JW
3546
3547 if (info->relocateable)
3548 {
3549 /* This is a relocateable link. We don't have to change
3550 anything, unless the reloc is against a section symbol,
3551 in which case we have to adjust according to where the
3552 section symbol winds up in the output section. */
3553 if (r_symndx < symtab_hdr->sh_info)
3554 {
3555 sym = local_syms + r_symndx;
3556 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3557 {
3558 sym_sec = local_sections[r_symndx];
3559 rel->r_addend += sym_sec->output_offset;
3560 }
3561 }
3562 continue;
3563 }
3564
3565 /* This is a final link. */
3566
3567 h = NULL;
3568 sym = NULL;
3569 sym_sec = NULL;
3570 undef_weak_ref = false;
3571
3572 if (r_symndx < symtab_hdr->sh_info)
3573 {
3574 /* Reloc against local symbol. */
3575 sym = local_syms + r_symndx;
3576 sym_sec = local_sections[r_symndx];
f8df10f4 3577 value = _bfd_elf_rela_local_sym (output_bfd, sym, sym_sec, rel);
f7460f5f
JJ
3578 if ((sym_sec->flags & SEC_MERGE)
3579 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
65765700
JJ
3580 && (elf_section_data (sym_sec)->sec_info_type
3581 == ELF_INFO_TYPE_MERGE))
f7460f5f
JJ
3582 {
3583 struct elfNN_ia64_local_hash_entry *loc_h;
3584
3585 loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, false);
3586 if (loc_h && ! loc_h->sec_merge_done)
3587 {
3588 struct elfNN_ia64_dyn_sym_info *dynent;
3589 asection *msec;
3590
3591 for (dynent = loc_h->info; dynent; dynent = dynent->next)
3592 {
3593 msec = sym_sec;
3594 dynent->addend =
3595 _bfd_merged_section_offset (output_bfd, &msec,
3596 elf_section_data (msec)->
65765700 3597 sec_info,
f7460f5f
JJ
3598 sym->st_value
3599 + dynent->addend,
3600 (bfd_vma) 0);
3601 dynent->addend -= sym->st_value;
3602 dynent->addend += msec->output_section->vma
3603 + msec->output_offset
3604 - sym_sec->output_section->vma
3605 - sym_sec->output_offset;
3606 }
3607 loc_h->sec_merge_done = 1;
3608 }
3609 }
800eeca4
JW
3610 }
3611 else
3612 {
3613 long indx;
3614
3615 /* Reloc against global symbol. */
3616 indx = r_symndx - symtab_hdr->sh_info;
3617 h = elf_sym_hashes (input_bfd)[indx];
3618 while (h->root.type == bfd_link_hash_indirect
3619 || h->root.type == bfd_link_hash_warning)
3620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3621
3622 value = 0;
3623 if (h->root.type == bfd_link_hash_defined
3624 || h->root.type == bfd_link_hash_defweak)
3625 {
3626 sym_sec = h->root.u.def.section;
3627
3628 /* Detect the cases that sym_sec->output_section is
3629 expected to be NULL -- all cases in which the symbol
3630 is defined in another shared module. This includes
3631 PLT relocs for which we've created a PLT entry and
3632 other relocs for which we're prepared to create
3633 dynamic relocations. */
3634 /* ??? Just accept it NULL and continue. */
3635
3636 if (sym_sec->output_section != NULL)
3637 {
3638 value = (h->root.u.def.value
3639 + sym_sec->output_section->vma
3640 + sym_sec->output_offset);
3641 }
3642 }
3643 else if (h->root.type == bfd_link_hash_undefweak)
3644 undef_weak_ref = true;
671bae9c
NC
3645 else if (info->shared
3646 && (!info->symbolic || info->allow_shlib_undefined)
3a27a730
L
3647 && !info->no_undefined
3648 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
800eeca4
JW
3649 ;
3650 else
3651 {
3652 if (! ((*info->callbacks->undefined_symbol)
3653 (info, h->root.root.string, input_bfd,
3654 input_section, rel->r_offset,
3a27a730
L
3655 (!info->shared || info->no_undefined
3656 || ELF_ST_VISIBILITY (h->other)))))
800eeca4
JW
3657 return false;
3658 ret_val = false;
3659 continue;
3660 }
3661 }
3662
3663 hit_addr = contents + rel->r_offset;
3664 value += rel->r_addend;
bbe66d08 3665 dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info);
800eeca4
JW
3666
3667 switch (r_type)
3668 {
3669 case R_IA64_NONE:
3670 case R_IA64_LDXMOV:
3671 continue;
3672
3673 case R_IA64_IMM14:
3674 case R_IA64_IMM22:
3675 case R_IA64_IMM64:
3676 case R_IA64_DIR32MSB:
3677 case R_IA64_DIR32LSB:
3678 case R_IA64_DIR64MSB:
3679 case R_IA64_DIR64LSB:
3680 /* Install a dynamic relocation for this reloc. */
7b6dab7f
TW
3681 if ((dynamic_symbol_p || info->shared
3682 || (elfNN_ia64_aix_vec (info->hash->creator)
64e9ece0 3683 /* Don't emit relocs for __GLOB_DATA_PTR on AIX. */
dc810e39 3684 && (!h || strcmp (h->root.root.string,
64e9ece0 3685 "__GLOB_DATA_PTR") != 0)))
ec338859 3686 && r_symndx != 0
800eeca4
JW
3687 && (input_section->flags & SEC_ALLOC) != 0)
3688 {
3689 unsigned int dyn_r_type;
3690 long dynindx;
18b27f17 3691 bfd_vma addend;
800eeca4
JW
3692
3693 BFD_ASSERT (srel != NULL);
3694
3695 /* If we don't need dynamic symbol lookup, find a
3696 matching RELATIVE relocation. */
3697 dyn_r_type = r_type;
3698 if (dynamic_symbol_p)
18b27f17
RH
3699 {
3700 dynindx = h->dynindx;
3701 addend = rel->r_addend;
3702 value = 0;
3703 }
800eeca4
JW
3704 else
3705 {
3706 switch (r_type)
3707 {
3708 case R_IA64_DIR32MSB:
3709 dyn_r_type = R_IA64_REL32MSB;
3710 break;
3711 case R_IA64_DIR32LSB:
3712 dyn_r_type = R_IA64_REL32LSB;
3713 break;
3714 case R_IA64_DIR64MSB:
3715 dyn_r_type = R_IA64_REL64MSB;
3716 break;
3717 case R_IA64_DIR64LSB:
3718 dyn_r_type = R_IA64_REL64LSB;
3719 break;
3720
3721 default:
3722 /* We can't represent this without a dynamic symbol.
3723 Adjust the relocation to be against an output
3724 section symbol, which are always present in the
3725 dynamic symbol table. */
3726 /* ??? People shouldn't be doing non-pic code in
3727 shared libraries. Hork. */
3728 (*_bfd_error_handler)
3729 (_("%s: linking non-pic code in a shared library"),
8f615d07 3730 bfd_archive_filename (input_bfd));
800eeca4
JW
3731 ret_val = false;
3732 continue;
3733 }
3734 dynindx = 0;
18b27f17 3735 addend = value;
800eeca4
JW
3736 }
3737
7b6dab7f
TW
3738 if (elfNN_ia64_aix_vec (info->hash->creator))
3739 rel->r_addend = value;
bbe66d08 3740 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
800eeca4 3741 srel, rel->r_offset, dyn_r_type,
18b27f17 3742 dynindx, addend);
800eeca4
JW
3743 }
3744 /* FALLTHRU */
3745
3746 case R_IA64_LTV32MSB:
3747 case R_IA64_LTV32LSB:
3748 case R_IA64_LTV64MSB:
3749 case R_IA64_LTV64LSB:
bbe66d08 3750 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
3751 break;
3752
3753 case R_IA64_GPREL22:
3754 case R_IA64_GPREL64I:
3755 case R_IA64_GPREL32MSB:
3756 case R_IA64_GPREL32LSB:
3757 case R_IA64_GPREL64MSB:
3758 case R_IA64_GPREL64LSB:
3759 if (dynamic_symbol_p)
3760 {
3761 (*_bfd_error_handler)
3762 (_("%s: @gprel relocation against dynamic symbol %s"),
8f615d07 3763 bfd_archive_filename (input_bfd), h->root.root.string);
800eeca4
JW
3764 ret_val = false;
3765 continue;
3766 }
3767 value -= gp_val;
bbe66d08 3768 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
3769 break;
3770
3771 case R_IA64_LTOFF22:
3772 case R_IA64_LTOFF22X:
3773 case R_IA64_LTOFF64I:
3774 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3775 value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
3776 rel->r_addend, value, R_IA64_DIR64LSB);
3777 value -= gp_val;
bbe66d08 3778 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
3779 break;
3780
3781 case R_IA64_PLTOFF22:
3782 case R_IA64_PLTOFF64I:
3783 case R_IA64_PLTOFF64MSB:
3784 case R_IA64_PLTOFF64LSB:
3785 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3786 value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
3787 value -= gp_val;
bbe66d08 3788 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
3789 break;
3790
3791 case R_IA64_FPTR64I:
3792 case R_IA64_FPTR32MSB:
3793 case R_IA64_FPTR32LSB:
3794 case R_IA64_FPTR64MSB:
3795 case R_IA64_FPTR64LSB:
3796 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3797 if (dyn_i->want_fptr)
3798 {
3799 if (!undef_weak_ref)
3800 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3801 }
3802 else
3803 {
3804 long dynindx;
3805
3806 /* Otherwise, we expect the dynamic linker to create
3807 the entry. */
3808
3809 if (h)
3810 {
3811 if (h->dynindx != -1)
3812 dynindx = h->dynindx;
3813 else
3814 dynindx = (_bfd_elf_link_lookup_local_dynindx
3815 (info, h->root.u.def.section->owner,
3816 global_sym_index (h)));
3817 }
3818 else
3819 {
3820 dynindx = (_bfd_elf_link_lookup_local_dynindx
dc810e39 3821 (info, input_bfd, (long) r_symndx));
800eeca4
JW
3822 }
3823
bbe66d08 3824 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
800eeca4
JW
3825 srel, rel->r_offset, r_type,
3826 dynindx, rel->r_addend);
3827 value = 0;
3828 }
3829
bbe66d08 3830 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
3831 break;
3832
3833 case R_IA64_LTOFF_FPTR22:
3834 case R_IA64_LTOFF_FPTR64I:
a4bd8390
JW
3835 case R_IA64_LTOFF_FPTR32MSB:
3836 case R_IA64_LTOFF_FPTR32LSB:
800eeca4
JW
3837 case R_IA64_LTOFF_FPTR64MSB:
3838 case R_IA64_LTOFF_FPTR64LSB:
3839 {
3840 long dynindx;
3841
3842 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3843 if (dyn_i->want_fptr)
3844 {
3845 BFD_ASSERT (h == NULL || h->dynindx == -1)
3846 if (!undef_weak_ref)
3847 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3848 dynindx = -1;
3849 }
3850 else
3851 {
3852 /* Otherwise, we expect the dynamic linker to create
3853 the entry. */
3854 if (h)
3855 {
3856 if (h->dynindx != -1)
3857 dynindx = h->dynindx;
3858 else
3859 dynindx = (_bfd_elf_link_lookup_local_dynindx
3860 (info, h->root.u.def.section->owner,
3861 global_sym_index (h)));
3862 }
3863 else
3864 dynindx = (_bfd_elf_link_lookup_local_dynindx
dc810e39 3865 (info, input_bfd, (long) r_symndx));
800eeca4
JW
3866 value = 0;
3867 }
3868
3869 value = set_got_entry (output_bfd, info, dyn_i, dynindx,
3870 rel->r_addend, value, R_IA64_FPTR64LSB);
3871 value -= gp_val;
bbe66d08 3872 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
3873 }
3874 break;
3875
3876 case R_IA64_PCREL32MSB:
3877 case R_IA64_PCREL32LSB:
3878 case R_IA64_PCREL64MSB:
3879 case R_IA64_PCREL64LSB:
3880 /* Install a dynamic relocation for this reloc. */
ec338859
AM
3881 if ((dynamic_symbol_p
3882 || elfNN_ia64_aix_vec (info->hash->creator))
3883 && r_symndx != 0)
800eeca4
JW
3884 {
3885 BFD_ASSERT (srel != NULL);
3886
bbe66d08 3887 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
800eeca4
JW
3888 srel, rel->r_offset, r_type,
3889 h->dynindx, rel->r_addend);
3890 }
3891 goto finish_pcrel;
3892
748abff6 3893 case R_IA64_PCREL21BI:
800eeca4
JW
3894 case R_IA64_PCREL21F:
3895 case R_IA64_PCREL21M:
3896 /* ??? These two are only used for speculation fixup code.
3897 They should never be dynamic. */
3898 if (dynamic_symbol_p)
3899 {
3900 (*_bfd_error_handler)
3901 (_("%s: dynamic relocation against speculation fixup"),
8f615d07 3902 bfd_archive_filename (input_bfd));
800eeca4
JW
3903 ret_val = false;
3904 continue;
3905 }
3906 if (undef_weak_ref)
3907 {
3908 (*_bfd_error_handler)
3909 (_("%s: speculation fixup against undefined weak symbol"),
8f615d07 3910 bfd_archive_filename (input_bfd));
800eeca4
JW
3911 ret_val = false;
3912 continue;
3913 }
3914 goto finish_pcrel;
3915
3916 case R_IA64_PCREL21B:
748abff6 3917 case R_IA64_PCREL60B:
800eeca4 3918 /* We should have created a PLT entry for any dynamic symbol. */
800eeca4
JW
3919 dyn_i = NULL;
3920 if (h)
3921 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3922
3923 if (dyn_i && dyn_i->want_plt2)
3924 {
3925 /* Should have caught this earlier. */
3926 BFD_ASSERT (rel->r_addend == 0);
3927
3928 value = (ia64_info->plt_sec->output_section->vma
3929 + ia64_info->plt_sec->output_offset
3930 + dyn_i->plt2_offset);
3931 }
3932 else
3933 {
3934 /* Since there's no PLT entry, Validate that this is
3935 locally defined. */
3936 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
3937
3938 /* If the symbol is undef_weak, we shouldn't be trying
3939 to call it. There's every chance that we'd wind up
3940 with an out-of-range fixup here. Don't bother setting
3941 any value at all. */
3942 if (undef_weak_ref)
3943 continue;
3944 }
3945 goto finish_pcrel;
3946
748abff6
RH
3947 case R_IA64_PCREL22:
3948 case R_IA64_PCREL64I:
800eeca4
JW
3949 finish_pcrel:
3950 /* Make pc-relative. */
3951 value -= (input_section->output_section->vma
3952 + input_section->output_offset
3953 + rel->r_offset) & ~ (bfd_vma) 0x3;
bbe66d08 3954 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
3955 break;
3956
3957 case R_IA64_SEGREL32MSB:
3958 case R_IA64_SEGREL32LSB:
3959 case R_IA64_SEGREL64MSB:
3960 case R_IA64_SEGREL64LSB:
d7458677
AM
3961 if (r_symndx == 0)
3962 {
3963 /* If the input section was discarded from the output, then
3964 do nothing. */
3965 r = bfd_reloc_ok;
3966 }
3967 else
3968 {
3969 struct elf_segment_map *m;
3970 Elf_Internal_Phdr *p;
3971
3972 /* Find the segment that contains the output_section. */
3973 for (m = elf_tdata (output_bfd)->segment_map,
3974 p = elf_tdata (output_bfd)->phdr;
3975 m != NULL;
3976 m = m->next, p++)
3977 {
3978 int i;
3979 for (i = m->count - 1; i >= 0; i--)
3980 if (m->sections[i] == sym_sec->output_section)
3981 break;
3982 if (i >= 0)
800eeca4 3983 break;
d7458677 3984 }
800eeca4 3985
d7458677
AM
3986 if (m == NULL)
3987 {
800eeca4 3988 r = bfd_reloc_notsupported;
d7458677
AM
3989 }
3990 else
3991 {
3992 /* The VMA of the segment is the vaddr of the associated
3993 program header. */
3994 if (value > p->p_vaddr)
3995 value -= p->p_vaddr;
3996 else
3997 value = 0;
3998 r = elfNN_ia64_install_value (output_bfd, hit_addr, value,
3999 r_type);
4000 }
4001 break;
4002 }
800eeca4
JW
4003
4004 case R_IA64_SECREL32MSB:
4005 case R_IA64_SECREL32LSB:
4006 case R_IA64_SECREL64MSB:
4007 case R_IA64_SECREL64LSB:
4008 /* Make output-section relative. */
4009 if (value > input_section->output_section->vma)
4010 value -= input_section->output_section->vma;
4011 else
4012 value = 0;
bbe66d08 4013 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
800eeca4
JW
4014 break;
4015
800eeca4
JW
4016 case R_IA64_IPLTMSB:
4017 case R_IA64_IPLTLSB:
18b27f17
RH
4018 /* Install a dynamic relocation for this reloc. */
4019 if ((dynamic_symbol_p || info->shared)
4020 && (input_section->flags & SEC_ALLOC) != 0)
4021 {
18b27f17
RH
4022 BFD_ASSERT (srel != NULL);
4023
4024 /* If we don't need dynamic symbol lookup, install two
4025 RELATIVE relocations. */
4026 if (! dynamic_symbol_p)
4027 {
4028 unsigned int dyn_r_type;
3e932841 4029
18b27f17
RH
4030 if (r_type == R_IA64_IPLTMSB)
4031 dyn_r_type = R_IA64_REL64MSB;
4032 else
4033 dyn_r_type = R_IA64_REL64LSB;
4034
4035 elfNN_ia64_install_dyn_reloc (output_bfd, info,
4036 input_section,
4037 srel, rel->r_offset,
4038 dyn_r_type, 0, value);
4039 elfNN_ia64_install_dyn_reloc (output_bfd, info,
4040 input_section,
4041 srel, rel->r_offset + 8,
4042 dyn_r_type, 0, gp_val);
4043 }
4044 else
4045 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
4046 srel, rel->r_offset, r_type,
4047 h->dynindx, rel->r_addend);
4048 }
4049
4050 if (r_type == R_IA64_IPLTMSB)
4051 r_type = R_IA64_DIR64MSB;
4052 else
4053 r_type = R_IA64_DIR64LSB;
4054 elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4055 r = elfNN_ia64_install_value (output_bfd, hit_addr + 8, gp_val,
4056 r_type);
4057 break;
800eeca4 4058
800eeca4
JW
4059 default:
4060 r = bfd_reloc_notsupported;
4061 break;
4062 }
4063
4064 switch (r)
4065 {
4066 case bfd_reloc_ok:
4067 break;
4068
4069 case bfd_reloc_undefined:
4070 /* This can happen for global table relative relocs if
4071 __gp is undefined. This is a panic situation so we
4072 don't try to continue. */
4073 (*info->callbacks->undefined_symbol)
4074 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
4075 return false;
4076
4077 case bfd_reloc_notsupported:
4078 {
4079 const char *name;
4080
4081 if (h)
4082 name = h->root.root.string;
4083 else
4084 {
4085 name = bfd_elf_string_from_elf_section (input_bfd,
4086 symtab_hdr->sh_link,
4087 sym->st_name);
4088 if (name == NULL)
4089 return false;
4090 if (*name == '\0')
4091 name = bfd_section_name (input_bfd, input_section);
4092 }
4093 if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
4094 name, input_bfd,
4095 input_section, rel->r_offset))
4096 return false;
4097 ret_val = false;
4098 }
4099 break;
4100
4101 case bfd_reloc_dangerous:
4102 case bfd_reloc_outofrange:
4103 case bfd_reloc_overflow:
4104 default:
4105 {
4106 const char *name;
4107
4108 if (h)
4109 name = h->root.root.string;
4110 else
4111 {
4112 name = bfd_elf_string_from_elf_section (input_bfd,
4113 symtab_hdr->sh_link,
4114 sym->st_name);
4115 if (name == NULL)
4116 return false;
4117 if (*name == '\0')
4118 name = bfd_section_name (input_bfd, input_section);
4119 }
4120 if (!(*info->callbacks->reloc_overflow) (info, name,
dc810e39
AM
4121 howto->name,
4122 (bfd_vma) 0,
800eeca4
JW
4123 input_bfd,
4124 input_section,
4125 rel->r_offset))
4126 return false;
4127 ret_val = false;
4128 }
4129 break;
4130 }
4131 }
4132
4133 return ret_val;
4134}
4135
4136static boolean
bbe66d08 4137elfNN_ia64_finish_dynamic_symbol (output_bfd, info, h, sym)
800eeca4
JW
4138 bfd *output_bfd;
4139 struct bfd_link_info *info;
4140 struct elf_link_hash_entry *h;
4141 Elf_Internal_Sym *sym;
4142{
bbe66d08
JW
4143 struct elfNN_ia64_link_hash_table *ia64_info;
4144 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4 4145
bbe66d08 4146 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
4147 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
4148
4149 /* Fill in the PLT data, if required. */
4150 if (dyn_i && dyn_i->want_plt)
4151 {
4152 Elf_Internal_Rela outrel;
4153 bfd_byte *loc;
4154 asection *plt_sec;
4155 bfd_vma plt_addr, pltoff_addr, gp_val, index;
bbe66d08 4156 ElfNN_External_Rela *rel;
800eeca4
JW
4157
4158 gp_val = _bfd_get_gp_value (output_bfd);
4159
4160 /* Initialize the minimal PLT entry. */
4161
4162 index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
4163 plt_sec = ia64_info->plt_sec;
4164 loc = plt_sec->contents + dyn_i->plt_offset;
4165
4166 memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
bbe66d08
JW
4167 elfNN_ia64_install_value (output_bfd, loc, index, R_IA64_IMM22);
4168 elfNN_ia64_install_value (output_bfd, loc+2, -dyn_i->plt_offset,
800eeca4
JW
4169 R_IA64_PCREL21B);
4170
4171 plt_addr = (plt_sec->output_section->vma
4172 + plt_sec->output_offset
4173 + dyn_i->plt_offset);
4174 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
4175
4176 /* Initialize the FULL PLT entry, if needed. */
4177 if (dyn_i->want_plt2)
4178 {
4179 loc = plt_sec->contents + dyn_i->plt2_offset;
4180
4181 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
bbe66d08 4182 elfNN_ia64_install_value (output_bfd, loc, pltoff_addr - gp_val,
800eeca4
JW
4183 R_IA64_IMM22);
4184
4185 /* Mark the symbol as undefined, rather than as defined in the
4186 plt section. Leave the value alone. */
4187 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
4188 first place. But perhaps elflink.h did some for us. */
4189 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4190 sym->st_shndx = SHN_UNDEF;
4191 }
4192
4193 /* Create the dynamic relocation. */
4194 outrel.r_offset = pltoff_addr;
4195 if (bfd_little_endian (output_bfd))
bbe66d08 4196 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
800eeca4 4197 else
bbe66d08 4198 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
800eeca4
JW
4199 outrel.r_addend = 0;
4200
4201 /* This is fun. In the .IA_64.pltoff section, we've got entries
4202 that correspond both to real PLT entries, and those that
4203 happened to resolve to local symbols but need to be created
4204 to satisfy @pltoff relocations. The .rela.IA_64.pltoff
4205 relocations for the real PLT should come at the end of the
4206 section, so that they can be indexed by plt entry at runtime.
4207
4208 We emitted all of the relocations for the non-PLT @pltoff
4209 entries during relocate_section. So we can consider the
4210 existing sec->reloc_count to be the base of the array of
4211 PLT relocations. */
4212
bbe66d08 4213 rel = (ElfNN_External_Rela *)ia64_info->rel_pltoff_sec->contents;
800eeca4
JW
4214 rel += ia64_info->rel_pltoff_sec->reloc_count;
4215
bbe66d08 4216 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, rel + index);
800eeca4
JW
4217 }
4218
4219 /* Mark some specially defined symbols as absolute. */
4220 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4221 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4222 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4223 sym->st_shndx = SHN_ABS;
4224
4225 return true;
4226}
4227
4228static boolean
bbe66d08 4229elfNN_ia64_finish_dynamic_sections (abfd, info)
800eeca4
JW
4230 bfd *abfd;
4231 struct bfd_link_info *info;
4232{
bbe66d08 4233 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
4234 bfd *dynobj;
4235
bbe66d08 4236 ia64_info = elfNN_ia64_hash_table (info);
800eeca4
JW
4237 dynobj = ia64_info->root.dynobj;
4238
4239 if (elf_hash_table (info)->dynamic_sections_created)
4240 {
bbe66d08 4241 ElfNN_External_Dyn *dyncon, *dynconend;
800eeca4
JW
4242 asection *sdyn, *sgotplt;
4243 bfd_vma gp_val;
4244
4245 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4246 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
4247 BFD_ASSERT (sdyn != NULL);
bbe66d08
JW
4248 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
4249 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
800eeca4
JW
4250
4251 gp_val = _bfd_get_gp_value (abfd);
4252
4253 for (; dyncon < dynconend; dyncon++)
4254 {
4255 Elf_Internal_Dyn dyn;
800eeca4 4256
bbe66d08 4257 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
800eeca4
JW
4258
4259 switch (dyn.d_tag)
4260 {
4261 case DT_PLTGOT:
4262 dyn.d_un.d_ptr = gp_val;
4263 break;
4264
4265 case DT_PLTRELSZ:
4266 dyn.d_un.d_val = (ia64_info->minplt_entries
bbe66d08 4267 * sizeof (ElfNN_External_Rela));
800eeca4
JW
4268 break;
4269
4270 case DT_JMPREL:
4271 /* See the comment above in finish_dynamic_symbol. */
4272 dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
4273 + ia64_info->rel_pltoff_sec->output_offset
4274 + (ia64_info->rel_pltoff_sec->reloc_count
bbe66d08 4275 * sizeof (ElfNN_External_Rela)));
800eeca4
JW
4276 break;
4277
4278 case DT_IA_64_PLT_RESERVE:
4279 dyn.d_un.d_ptr = (sgotplt->output_section->vma
4280 + sgotplt->output_offset);
4281 break;
4282
4283 case DT_RELASZ:
4284 /* Do not have RELASZ include JMPREL. This makes things
3e932841 4285 easier on ld.so. This is not what the rest of BFD set up. */
800eeca4 4286 dyn.d_un.d_val -= (ia64_info->minplt_entries
bbe66d08 4287 * sizeof (ElfNN_External_Rela));
800eeca4 4288 break;
800eeca4
JW
4289 }
4290
bbe66d08 4291 bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
800eeca4
JW
4292 }
4293
4294 /* Initialize the PLT0 entry */
4295 if (ia64_info->plt_sec)
4296 {
4297 bfd_byte *loc = ia64_info->plt_sec->contents;
4298 bfd_vma pltres;
4299
4300 memcpy (loc, plt_header, PLT_HEADER_SIZE);
4301
4302 pltres = (sgotplt->output_section->vma
4303 + sgotplt->output_offset
4304 - gp_val);
4305
bbe66d08 4306 elfNN_ia64_install_value (abfd, loc+1, pltres, R_IA64_GPREL22);
800eeca4
JW
4307 }
4308 }
4309
4310 return true;
4311}
4312\f
4313/* ELF file flag handling: */
4314
3e932841 4315/* Function to keep IA-64 specific file flags. */
800eeca4 4316static boolean
bbe66d08 4317elfNN_ia64_set_private_flags (abfd, flags)
800eeca4
JW
4318 bfd *abfd;
4319 flagword flags;
4320{
4321 BFD_ASSERT (!elf_flags_init (abfd)
4322 || elf_elfheader (abfd)->e_flags == flags);
4323
4324 elf_elfheader (abfd)->e_flags = flags;
4325 elf_flags_init (abfd) = true;
4326 return true;
4327}
4328
800eeca4
JW
4329/* Merge backend specific data from an object file to the output
4330 object file when linking. */
4331static boolean
bbe66d08 4332elfNN_ia64_merge_private_bfd_data (ibfd, obfd)
800eeca4
JW
4333 bfd *ibfd, *obfd;
4334{
4335 flagword out_flags;
4336 flagword in_flags;
4337 boolean ok = true;
4338
4339 /* Don't even pretend to support mixed-format linking. */
4340 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4341 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4342 return false;
4343
4344 in_flags = elf_elfheader (ibfd)->e_flags;
4345 out_flags = elf_elfheader (obfd)->e_flags;
4346
4347 if (! elf_flags_init (obfd))
4348 {
4349 elf_flags_init (obfd) = true;
4350 elf_elfheader (obfd)->e_flags = in_flags;
4351
4352 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4353 && bfd_get_arch_info (obfd)->the_default)
4354 {
4355 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4356 bfd_get_mach (ibfd));
4357 }
4358
4359 return true;
4360 }
4361
4362 /* Check flag compatibility. */
4363 if (in_flags == out_flags)
4364 return true;
4365
c43c2cc5
JW
4366 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
4367 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4368 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4369
800eeca4
JW
4370 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4371 {
4372 (*_bfd_error_handler)
4373 (_("%s: linking trap-on-NULL-dereference with non-trapping files"),
8f615d07 4374 bfd_archive_filename (ibfd));
800eeca4
JW
4375
4376 bfd_set_error (bfd_error_bad_value);
4377 ok = false;
4378 }
4379 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4380 {
4381 (*_bfd_error_handler)
4382 (_("%s: linking big-endian files with little-endian files"),
8f615d07 4383 bfd_archive_filename (ibfd));
800eeca4
JW
4384
4385 bfd_set_error (bfd_error_bad_value);
4386 ok = false;
4387 }
4388 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4389 {
4390 (*_bfd_error_handler)
4391 (_("%s: linking 64-bit files with 32-bit files"),
8f615d07 4392 bfd_archive_filename (ibfd));
800eeca4
JW
4393
4394 bfd_set_error (bfd_error_bad_value);
4395 ok = false;
4396 }
c43c2cc5
JW
4397 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4398 {
4399 (*_bfd_error_handler)
4400 (_("%s: linking constant-gp files with non-constant-gp files"),
8f615d07 4401 bfd_archive_filename (ibfd));
c43c2cc5
JW
4402
4403 bfd_set_error (bfd_error_bad_value);
4404 ok = false;
4405 }
4406 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4407 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4408 {
4409 (*_bfd_error_handler)
4410 (_("%s: linking auto-pic files with non-auto-pic files"),
8f615d07 4411 bfd_archive_filename (ibfd));
c43c2cc5
JW
4412
4413 bfd_set_error (bfd_error_bad_value);
4414 ok = false;
4415 }
800eeca4
JW
4416
4417 return ok;
4418}
4419
4420static boolean
bbe66d08 4421elfNN_ia64_print_private_bfd_data (abfd, ptr)
800eeca4
JW
4422 bfd *abfd;
4423 PTR ptr;
4424{
4425 FILE *file = (FILE *) ptr;
4426 flagword flags = elf_elfheader (abfd)->e_flags;
4427
4428 BFD_ASSERT (abfd != NULL && ptr != NULL);
4429
c43c2cc5 4430 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
800eeca4
JW
4431 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4432 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4433 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
c43c2cc5
JW
4434 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4435 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4436 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4437 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
800eeca4 4438 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
3e932841 4439
800eeca4
JW
4440 _bfd_elf_print_private_bfd_data (abfd, ptr);
4441 return true;
4442}
db6751f2
JJ
4443
4444static enum elf_reloc_type_class
f51e552e
AM
4445elfNN_ia64_reloc_type_class (rela)
4446 const Elf_Internal_Rela *rela;
db6751f2 4447{
f51e552e 4448 switch ((int) ELFNN_R_TYPE (rela->r_info))
db6751f2
JJ
4449 {
4450 case R_IA64_REL32MSB:
4451 case R_IA64_REL32LSB:
4452 case R_IA64_REL64MSB:
4453 case R_IA64_REL64LSB:
4454 return reloc_class_relative;
4455 case R_IA64_IPLTMSB:
4456 case R_IA64_IPLTLSB:
4457 return reloc_class_plt;
4458 case R_IA64_COPY:
4459 return reloc_class_copy;
4460 default:
4461 return reloc_class_normal;
4462 }
4463}
fcf12726 4464
d9cf1b54
AM
4465static boolean
4466elfNN_ia64_hpux_vec (const bfd_target *vec)
4467{
4468 extern const bfd_target bfd_elfNN_ia64_hpux_big_vec;
4469 return (vec == & bfd_elfNN_ia64_hpux_big_vec);
4470}
4471
fcf12726
AM
4472static void
4473elfNN_hpux_post_process_headers (abfd, info)
4474 bfd *abfd;
4475 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4476{
4477 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4478
4479 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4480 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
4481}
d9cf1b54
AM
4482
4483boolean
4484elfNN_hpux_backend_section_from_bfd_section (abfd, hdr, sec, retval)
4485 bfd *abfd ATTRIBUTE_UNUSED;
4486 Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4487 asection *sec;
4488 int *retval;
4489{
4490 if (bfd_is_com_section (sec))
4491 {
4492 *retval = SHN_IA_64_ANSI_COMMON;
4493 return true;
4494 }
4495 return false;
4496}
800eeca4 4497\f
bbe66d08
JW
4498#define TARGET_LITTLE_SYM bfd_elfNN_ia64_little_vec
4499#define TARGET_LITTLE_NAME "elfNN-ia64-little"
4500#define TARGET_BIG_SYM bfd_elfNN_ia64_big_vec
4501#define TARGET_BIG_NAME "elfNN-ia64-big"
800eeca4
JW
4502#define ELF_ARCH bfd_arch_ia64
4503#define ELF_MACHINE_CODE EM_IA_64
4504#define ELF_MACHINE_ALT1 1999 /* EAS2.3 */
4505#define ELF_MACHINE_ALT2 1998 /* EAS2.2 */
4506#define ELF_MAXPAGESIZE 0x10000 /* 64KB */
4507
4508#define elf_backend_section_from_shdr \
bbe66d08 4509 elfNN_ia64_section_from_shdr
fa152c49 4510#define elf_backend_section_flags \
bbe66d08 4511 elfNN_ia64_section_flags
800eeca4 4512#define elf_backend_fake_sections \
bbe66d08 4513 elfNN_ia64_fake_sections
81545d45
RH
4514#define elf_backend_final_write_processing \
4515 elfNN_ia64_final_write_processing
800eeca4 4516#define elf_backend_add_symbol_hook \
bbe66d08 4517 elfNN_ia64_add_symbol_hook
800eeca4 4518#define elf_backend_additional_program_headers \
bbe66d08 4519 elfNN_ia64_additional_program_headers
800eeca4 4520#define elf_backend_modify_segment_map \
bbe66d08 4521 elfNN_ia64_modify_segment_map
800eeca4 4522#define elf_info_to_howto \
bbe66d08 4523 elfNN_ia64_info_to_howto
800eeca4 4524
bbe66d08
JW
4525#define bfd_elfNN_bfd_reloc_type_lookup \
4526 elfNN_ia64_reloc_type_lookup
4527#define bfd_elfNN_bfd_is_local_label_name \
4528 elfNN_ia64_is_local_label_name
4529#define bfd_elfNN_bfd_relax_section \
4530 elfNN_ia64_relax_section
800eeca4
JW
4531
4532/* Stuff for the BFD linker: */
bbe66d08
JW
4533#define bfd_elfNN_bfd_link_hash_table_create \
4534 elfNN_ia64_hash_table_create
800eeca4 4535#define elf_backend_create_dynamic_sections \
bbe66d08 4536 elfNN_ia64_create_dynamic_sections
800eeca4 4537#define elf_backend_check_relocs \
bbe66d08 4538 elfNN_ia64_check_relocs
800eeca4 4539#define elf_backend_adjust_dynamic_symbol \
bbe66d08 4540 elfNN_ia64_adjust_dynamic_symbol
800eeca4 4541#define elf_backend_size_dynamic_sections \
bbe66d08 4542 elfNN_ia64_size_dynamic_sections
800eeca4 4543#define elf_backend_relocate_section \
bbe66d08 4544 elfNN_ia64_relocate_section
800eeca4 4545#define elf_backend_finish_dynamic_symbol \
bbe66d08 4546 elfNN_ia64_finish_dynamic_symbol
800eeca4 4547#define elf_backend_finish_dynamic_sections \
bbe66d08
JW
4548 elfNN_ia64_finish_dynamic_sections
4549#define bfd_elfNN_bfd_final_link \
4550 elfNN_ia64_final_link
4551
bbe66d08
JW
4552#define bfd_elfNN_bfd_merge_private_bfd_data \
4553 elfNN_ia64_merge_private_bfd_data
4554#define bfd_elfNN_bfd_set_private_flags \
4555 elfNN_ia64_set_private_flags
4556#define bfd_elfNN_bfd_print_private_bfd_data \
4557 elfNN_ia64_print_private_bfd_data
800eeca4
JW
4558
4559#define elf_backend_plt_readonly 1
4560#define elf_backend_want_plt_sym 0
4561#define elf_backend_plt_alignment 5
4562#define elf_backend_got_header_size 0
4563#define elf_backend_plt_header_size PLT_HEADER_SIZE
4564#define elf_backend_want_got_plt 1
4565#define elf_backend_may_use_rel_p 1
4566#define elf_backend_may_use_rela_p 1
4567#define elf_backend_default_use_rela_p 1
4568#define elf_backend_want_dynbss 0
bbe66d08
JW
4569#define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
4570#define elf_backend_hide_symbol elfNN_ia64_hash_hide_symbol
db6751f2 4571#define elf_backend_reloc_type_class elfNN_ia64_reloc_type_class
800eeca4 4572
bbe66d08 4573#include "elfNN-target.h"
7b6dab7f
TW
4574
4575/* AIX-specific vectors. */
4576
4577#undef TARGET_LITTLE_SYM
4578#define TARGET_LITTLE_SYM bfd_elfNN_ia64_aix_little_vec
4579#undef TARGET_LITTLE_NAME
4580#define TARGET_LITTLE_NAME "elfNN-ia64-aix-little"
4581#undef TARGET_BIG_SYM
4582#define TARGET_BIG_SYM bfd_elfNN_ia64_aix_big_vec
4583#undef TARGET_BIG_NAME
4584#define TARGET_BIG_NAME "elfNN-ia64-aix-big"
4585
4586#undef elf_backend_add_symbol_hook
4587#define elf_backend_add_symbol_hook elfNN_ia64_aix_add_symbol_hook
4588
4589#undef bfd_elfNN_bfd_link_add_symbols
4590#define bfd_elfNN_bfd_link_add_symbols elfNN_ia64_aix_link_add_symbols
4591
4592#define elfNN_bed elfNN_ia64_aix_bed
4593
4594#include "elfNN-target.h"
fcf12726
AM
4595
4596/* HPUX-specific vectors. */
4597
4598#undef TARGET_LITTLE_SYM
4599#undef TARGET_LITTLE_NAME
4600#undef TARGET_BIG_SYM
4601#define TARGET_BIG_SYM bfd_elfNN_ia64_hpux_big_vec
4602#undef TARGET_BIG_NAME
4603#define TARGET_BIG_NAME "elfNN-ia64-hpux-big"
4604
4605#undef elf_backend_post_process_headers
4606#define elf_backend_post_process_headers elfNN_hpux_post_process_headers
4607
d9cf1b54
AM
4608#undef elf_backend_section_from_bfd_section
4609#define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
4610
fcf12726
AM
4611#undef ELF_MAXPAGESIZE
4612#define ELF_MAXPAGESIZE 0x1000 /* 1K */
4613
4614#undef elfNN_bed
4615#define elfNN_bed elfNN_ia64_hpux_bed
4616
4617#include "elfNN-target.h"
This page took 0.30665 seconds and 4 git commands to generate.