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