Add field ``name'' to floatformat.
[binutils.git] / bfd / elf64-ia64.c
blob2fafeb18b29baa325a271e2f6a051a9153c6f89d
1 /* IA-64 support for 64-bit ELF
2 Copyright 1998, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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"
30 * THE RULES for all the stuff the linker creates --
32 * GOT Entries created in response to LTOFF or LTOFF_FPTR
33 * relocations. Dynamic relocs created for dynamic
34 * symbols in an application; REL relocs for locals
35 * in a shared library.
37 * FPTR The canonical function descriptor. Created for local
38 * symbols in applications. Descriptors for dynamic symbols
39 * and local symbols in shared libraries are created by
40 * ld.so. Thus there are no dynamic relocs against these
41 * objects. The FPTR relocs for such _are_ passed through
42 * to the dynamic relocation tables.
44 * FULL_PLT Created for a PCREL21B relocation against a dynamic symbol.
45 * Requires the creation of a PLTOFF entry. This does not
46 * require any dynamic relocations.
48 * PLTOFF Created by PLTOFF relocations. For local symbols, this
49 * is an alternate function descriptor, and in shared libraries
50 * requires two REL relocations. Note that this cannot be
51 * transformed into an FPTR relocation, since it must be in
52 * range of the GP. For dynamic symbols, this is a function
53 * descriptor for a MIN_PLT entry, and requires one IPLT reloc.
55 * MIN_PLT Created by PLTOFF entries against dynamic symbols. This
56 * does not reqire dynamic relocations.
59 #define USE_RELA /* we want RELA relocs, not REL */
61 #define NELEMS(a) ((int) (sizeof (a) / sizeof ((a)[0])))
63 typedef struct bfd_hash_entry *(*new_hash_entry_func)
64 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
66 /* In dynamically (linker-) created sections, we generally need to keep track
67 of the place a symbol or expression got allocated to. This is done via hash
68 tables that store entries of the following type. */
70 struct elf64_ia64_dyn_sym_info
72 /* The addend for which this entry is relevant. */
73 bfd_vma addend;
75 /* Next addend in the list. */
76 struct elf64_ia64_dyn_sym_info *next;
78 bfd_vma got_offset;
79 bfd_vma fptr_offset;
80 bfd_vma pltoff_offset;
81 bfd_vma plt_offset;
82 bfd_vma plt2_offset;
84 /* The symbol table entry, if any, that this was derrived from. */
85 struct elf_link_hash_entry *h;
87 /* Used to count non-got, non-plt relocations for delayed sizing
88 of relocation sections. */
89 struct elf64_ia64_dyn_reloc_entry
91 struct elf64_ia64_dyn_reloc_entry *next;
92 asection *srel;
93 int type;
94 int count;
95 } *reloc_entries;
97 /* True when the section contents have been updated. */
98 unsigned got_done : 1;
99 unsigned fptr_done : 1;
100 unsigned pltoff_done : 1;
102 /* True for the different kinds of linker data we want created. */
103 unsigned want_got : 1;
104 unsigned want_fptr : 1;
105 unsigned want_ltoff_fptr : 1;
106 unsigned want_plt : 1;
107 unsigned want_plt2 : 1;
108 unsigned want_pltoff : 1;
111 struct elf64_ia64_local_hash_entry
113 struct bfd_hash_entry root;
114 struct elf64_ia64_dyn_sym_info *info;
117 struct elf64_ia64_local_hash_table
119 struct bfd_hash_table root;
120 /* No additional fields for now. */
123 struct elf64_ia64_link_hash_entry
125 struct elf_link_hash_entry root;
126 struct elf64_ia64_dyn_sym_info *info;
129 struct elf64_ia64_link_hash_table
131 /* The main hash table */
132 struct elf_link_hash_table root;
134 asection *got_sec; /* the linkage table section (or NULL) */
135 asection *rel_got_sec; /* dynamic relocation section for same */
136 asection *fptr_sec; /* function descriptor table (or NULL) */
137 asection *plt_sec; /* the primary plt section (or NULL) */
138 asection *pltoff_sec; /* private descriptors for plt (or NULL) */
139 asection *rel_pltoff_sec; /* dynamic relocation section for same */
141 bfd_size_type minplt_entries; /* number of minplt entries */
143 struct elf64_ia64_local_hash_table loc_hash_table;
146 #define elf64_ia64_hash_table(p) \
147 ((struct elf64_ia64_link_hash_table *) ((p)->hash))
149 static bfd_reloc_status_type elf64_ia64_reloc
150 PARAMS ((bfd *abfd, arelent *reloc, asymbol *sym, PTR data,
151 asection *input_section, bfd *output_bfd, char **error_message));
152 static reloc_howto_type * lookup_howto
153 PARAMS ((unsigned int rtype));
154 static reloc_howto_type *elf64_ia64_reloc_type_lookup
155 PARAMS ((bfd *abfd, bfd_reloc_code_real_type bfd_code));
156 static void elf64_ia64_info_to_howto
157 PARAMS ((bfd *abfd, arelent *bfd_reloc, Elf64_Internal_Rela *elf_reloc));
158 static boolean elf64_ia64_relax_section
159 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
160 boolean *again));
161 static boolean elf64_ia64_section_from_shdr
162 PARAMS ((bfd *, Elf64_Internal_Shdr *, char *));
163 static boolean elf64_ia64_fake_sections
164 PARAMS ((bfd *abfd, Elf64_Internal_Shdr *hdr, asection *sec));
165 static boolean elf64_ia64_add_symbol_hook
166 PARAMS ((bfd *abfd, struct bfd_link_info *info, const Elf_Internal_Sym *sym,
167 const char **namep, flagword *flagsp, asection **secp,
168 bfd_vma *valp));
169 static int elf64_ia64_additional_program_headers
170 PARAMS ((bfd *abfd));
171 static boolean elf64_ia64_is_local_label_name
172 PARAMS ((bfd *abfd, const char *name));
173 static boolean elf64_ia64_dynamic_symbol_p
174 PARAMS ((struct elf_link_hash_entry *h, struct bfd_link_info *info));
175 static boolean elf64_ia64_local_hash_table_init
176 PARAMS ((struct elf64_ia64_local_hash_table *ht, bfd *abfd,
177 new_hash_entry_func new));
178 static struct bfd_hash_entry *elf64_ia64_new_loc_hash_entry
179 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
180 const char *string));
181 static struct bfd_hash_entry *elf64_ia64_new_elf_hash_entry
182 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
183 const char *string));
184 static struct bfd_link_hash_table *elf64_ia64_hash_table_create
185 PARAMS ((bfd *abfd));
186 static struct elf64_ia64_local_hash_entry *elf64_ia64_local_hash_lookup
187 PARAMS ((struct elf64_ia64_local_hash_table *table, const char *string,
188 boolean create, boolean copy));
189 static void elf64_ia64_dyn_sym_traverse
190 PARAMS ((struct elf64_ia64_link_hash_table *ia64_info,
191 boolean (*func)(struct elf64_ia64_dyn_sym_info *, PTR),
192 PTR info));
193 static boolean elf64_ia64_create_dynamic_sections
194 PARAMS ((bfd *abfd, struct bfd_link_info *info));
195 static struct elf64_ia64_dyn_sym_info * get_dyn_sym_info
196 PARAMS ((struct elf64_ia64_link_hash_table *ia64_info,
197 struct elf_link_hash_entry *h,
198 bfd *abfd, const Elf_Internal_Rela *rel, boolean create));
199 static asection *get_got
200 PARAMS ((bfd *abfd, struct bfd_link_info *info,
201 struct elf64_ia64_link_hash_table *ia64_info));
202 static asection *get_fptr
203 PARAMS ((bfd *abfd, struct bfd_link_info *info,
204 struct elf64_ia64_link_hash_table *ia64_info));
205 static asection *get_pltoff
206 PARAMS ((bfd *abfd, struct bfd_link_info *info,
207 struct elf64_ia64_link_hash_table *ia64_info));
208 static asection *get_reloc_section
209 PARAMS ((bfd *abfd, struct elf64_ia64_link_hash_table *ia64_info,
210 asection *sec, boolean create));
211 static boolean count_dyn_reloc
212 PARAMS ((bfd *abfd, struct elf64_ia64_dyn_sym_info *dyn_i,
213 asection *srel, int type));
214 static boolean elf64_ia64_check_relocs
215 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
216 const Elf_Internal_Rela *relocs));
217 static boolean elf64_ia64_adjust_dynamic_symbol
218 PARAMS ((struct bfd_link_info *info, struct elf_link_hash_entry *h));
219 static unsigned long global_sym_index
220 PARAMS ((struct elf_link_hash_entry *h));
221 static boolean allocate_fptr
222 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
223 static boolean allocate_global_data_got
224 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
225 static boolean allocate_global_fptr_got
226 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
227 static boolean allocate_local_got
228 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
229 static boolean allocate_pltoff_entries
230 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
231 static boolean allocate_plt_entries
232 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
233 static boolean allocate_plt2_entries
234 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
235 static boolean allocate_dynrel_entries
236 PARAMS ((struct elf64_ia64_dyn_sym_info *dyn_i, PTR data));
237 static boolean elf64_ia64_size_dynamic_sections
238 PARAMS ((bfd *output_bfd, struct bfd_link_info *info));
239 static bfd_reloc_status_type elf64_ia64_install_value
240 PARAMS ((bfd *abfd, bfd_byte *hit_addr, bfd_vma val, unsigned int r_type));
241 static void elf64_ia64_install_dyn_reloc
242 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
243 asection *srel, bfd_vma offset, unsigned int type,
244 long dynindx, bfd_vma addend));
245 static bfd_vma set_got_entry
246 PARAMS ((bfd *abfd, struct bfd_link_info *info,
247 struct elf64_ia64_dyn_sym_info *dyn_i, long dynindx,
248 bfd_vma addend, bfd_vma value, unsigned int dyn_r_type));
249 static bfd_vma set_fptr_entry
250 PARAMS ((bfd *abfd, struct bfd_link_info *info,
251 struct elf64_ia64_dyn_sym_info *dyn_i,
252 bfd_vma value));
253 static bfd_vma set_pltoff_entry
254 PARAMS ((bfd *abfd, struct bfd_link_info *info,
255 struct elf64_ia64_dyn_sym_info *dyn_i,
256 bfd_vma value, boolean));
257 static boolean elf64_ia64_final_link
258 PARAMS ((bfd *abfd, struct bfd_link_info *info));
259 static boolean elf64_ia64_relocate_section
260 PARAMS ((bfd *output_bfd, struct bfd_link_info *info, bfd *input_bfd,
261 asection *input_section, bfd_byte *contents,
262 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
263 asection **local_sections));
264 static boolean elf64_ia64_finish_dynamic_symbol
265 PARAMS ((bfd *output_bfd, struct bfd_link_info *info,
266 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
267 static boolean elf64_ia64_finish_dynamic_sections
268 PARAMS ((bfd *abfd, struct bfd_link_info *info));
269 static boolean elf64_ia64_set_private_flags
270 PARAMS ((bfd *abfd, flagword flags));
271 static boolean elf64_ia64_copy_private_bfd_data
272 PARAMS ((bfd *ibfd, bfd *obfd));
273 static boolean elf64_ia64_merge_private_bfd_data
274 PARAMS ((bfd *ibfd, bfd *obfd));
275 static boolean elf64_ia64_print_private_bfd_data
276 PARAMS ((bfd *abfd, PTR ptr));
279 /* ia64-specific relocation */
281 /* Perform a relocation. Not much to do here as all the hard work is
282 done in elf64_ia64_final_link_relocate. */
283 static bfd_reloc_status_type
284 elf64_ia64_reloc (abfd, reloc, sym, data, input_section,
285 output_bfd, error_message)
286 bfd *abfd;
287 arelent *reloc;
288 asymbol *sym;
289 PTR data;
290 asection *input_section;
291 bfd *output_bfd;
292 char **error_message;
294 if (output_bfd)
296 reloc->address += input_section->output_offset;
297 return bfd_reloc_ok;
299 *error_message = "Unsupported call to elf64_ia64_reloc";
300 return bfd_reloc_notsupported;
303 #define IA64_HOWTO(TYPE, NAME, SIZE, PCREL, IN) \
304 HOWTO (TYPE, 0, SIZE, 0, PCREL, 0, complain_overflow_signed, \
305 elf64_ia64_reloc, NAME, false, 0, 0, IN)
307 /* This table has to be sorted according to increasing number of the
308 TYPE field. */
309 static reloc_howto_type ia64_howto_table[] =
311 IA64_HOWTO (R_IA64_NONE, "NONE", 0, false, true),
313 IA64_HOWTO (R_IA64_IMM14, "IMM14", 0, false, true),
314 IA64_HOWTO (R_IA64_IMM22, "IMM22", 0, false, true),
315 IA64_HOWTO (R_IA64_IMM64, "IMM64", 0, false, true),
316 IA64_HOWTO (R_IA64_DIR32MSB, "DIR32MSB", 2, false, true),
317 IA64_HOWTO (R_IA64_DIR32LSB, "DIR32LSB", 2, false, true),
318 IA64_HOWTO (R_IA64_DIR64MSB, "DIR64MSB", 4, false, true),
319 IA64_HOWTO (R_IA64_DIR64LSB, "DIR64LSB", 4, false, true),
321 IA64_HOWTO (R_IA64_GPREL22, "GPREL22", 0, false, true),
322 IA64_HOWTO (R_IA64_GPREL64I, "GPREL64I", 0, false, true),
323 IA64_HOWTO (R_IA64_GPREL32MSB, "GPREL32MSB", 2, false, true),
324 IA64_HOWTO (R_IA64_GPREL32LSB, "GPREL32LSB", 2, false, true),
325 IA64_HOWTO (R_IA64_GPREL64MSB, "GPREL64MSB", 4, false, true),
326 IA64_HOWTO (R_IA64_GPREL64LSB, "GPREL64LSB", 4, false, true),
328 IA64_HOWTO (R_IA64_LTOFF22, "LTOFF22", 0, false, true),
329 IA64_HOWTO (R_IA64_LTOFF64I, "LTOFF64I", 0, false, true),
331 IA64_HOWTO (R_IA64_PLTOFF22, "PLTOFF22", 0, false, true),
332 IA64_HOWTO (R_IA64_PLTOFF64I, "PLTOFF64I", 0, false, true),
333 IA64_HOWTO (R_IA64_PLTOFF64MSB, "PLTOFF64MSB", 4, false, true),
334 IA64_HOWTO (R_IA64_PLTOFF64LSB, "PLTOFF64LSB", 4, false, true),
336 IA64_HOWTO (R_IA64_FPTR64I, "FPTR64I", 0, false, true),
337 IA64_HOWTO (R_IA64_FPTR32MSB, "FPTR32MSB", 2, false, true),
338 IA64_HOWTO (R_IA64_FPTR32LSB, "FPTR32LSB", 2, false, true),
339 IA64_HOWTO (R_IA64_FPTR64MSB, "FPTR64MSB", 4, false, true),
340 IA64_HOWTO (R_IA64_FPTR64LSB, "FPTR64LSB", 4, false, true),
342 IA64_HOWTO (R_IA64_PCREL60B, "PCREL60B", 0, true, true),
343 IA64_HOWTO (R_IA64_PCREL21B, "PCREL21B", 0, true, true),
344 IA64_HOWTO (R_IA64_PCREL21M, "PCREL21M", 0, true, true),
345 IA64_HOWTO (R_IA64_PCREL21F, "PCREL21F", 0, true, true),
346 IA64_HOWTO (R_IA64_PCREL32MSB, "PCREL32MSB", 2, true, true),
347 IA64_HOWTO (R_IA64_PCREL32LSB, "PCREL32LSB", 2, true, true),
348 IA64_HOWTO (R_IA64_PCREL64MSB, "PCREL64MSB", 4, true, true),
349 IA64_HOWTO (R_IA64_PCREL64LSB, "PCREL64LSB", 4, true, true),
351 IA64_HOWTO (R_IA64_LTOFF_FPTR22, "LTOFF_FPTR22", 0, false, true),
352 IA64_HOWTO (R_IA64_LTOFF_FPTR64I, "LTOFF_FPTR64I", 0, false, true),
353 IA64_HOWTO (R_IA64_LTOFF_FPTR64MSB, "LTOFF_FPTR64MSB", 4, false, true),
354 IA64_HOWTO (R_IA64_LTOFF_FPTR64LSB, "LTOFF_FPTR64LSB", 4, false, true),
356 IA64_HOWTO (R_IA64_SEGBASE, "SEGBASE", 4, false, true),
357 IA64_HOWTO (R_IA64_SEGREL32MSB, "SEGREL32MSB", 2, false, true),
358 IA64_HOWTO (R_IA64_SEGREL32LSB, "SEGREL32LSB", 2, false, true),
359 IA64_HOWTO (R_IA64_SEGREL64MSB, "SEGREL64MSB", 4, false, true),
360 IA64_HOWTO (R_IA64_SEGREL64LSB, "SEGREL64LSB", 4, false, true),
362 IA64_HOWTO (R_IA64_SECREL32MSB, "SECREL32MSB", 2, false, true),
363 IA64_HOWTO (R_IA64_SECREL32LSB, "SECREL32LSB", 2, false, true),
364 IA64_HOWTO (R_IA64_SECREL64MSB, "SECREL64MSB", 4, false, true),
365 IA64_HOWTO (R_IA64_SECREL64LSB, "SECREL64LSB", 4, false, true),
367 IA64_HOWTO (R_IA64_REL32MSB, "REL32MSB", 2, false, true),
368 IA64_HOWTO (R_IA64_REL32LSB, "REL32LSB", 2, false, true),
369 IA64_HOWTO (R_IA64_REL64MSB, "REL64MSB", 4, false, true),
370 IA64_HOWTO (R_IA64_REL64LSB, "REL64LSB", 4, false, true),
372 IA64_HOWTO (R_IA64_LTV32MSB, "LTV32MSB", 2, false, true),
373 IA64_HOWTO (R_IA64_LTV32LSB, "LTV32LSB", 2, false, true),
374 IA64_HOWTO (R_IA64_LTV64MSB, "LTV64MSB", 4, false, true),
375 IA64_HOWTO (R_IA64_LTV64LSB, "LTV64LSB", 4, false, true),
377 IA64_HOWTO (R_IA64_PCREL21BI, "PCREL21BI", 0, true, true),
378 IA64_HOWTO (R_IA64_PCREL22, "PCREL22", 0, true, true),
379 IA64_HOWTO (R_IA64_PCREL64I, "PCREL64I", 0, true, true),
381 IA64_HOWTO (R_IA64_IPLTMSB, "IPLTMSB", 4, false, true),
382 IA64_HOWTO (R_IA64_IPLTLSB, "IPLTLSB", 4, false, true),
383 IA64_HOWTO (R_IA64_EPLTMSB, "EPLTMSB", 4, false, true),
384 IA64_HOWTO (R_IA64_EPLTLSB, "EPLTLSB", 4, false, true),
385 IA64_HOWTO (R_IA64_COPY, "COPY", 4, false, true),
386 IA64_HOWTO (R_IA64_LTOFF22X, "LTOFF22X", 0, false, true),
387 IA64_HOWTO (R_IA64_LDXMOV, "LDXMOV", 0, false, true),
389 IA64_HOWTO (R_IA64_TPREL22, "TPREL22", 0, false, false),
390 IA64_HOWTO (R_IA64_TPREL64MSB, "TPREL64MSB", 8, false, false),
391 IA64_HOWTO (R_IA64_TPREL64LSB, "TPREL64LSB", 8, false, false),
392 IA64_HOWTO (R_IA64_LTOFF_TP22, "LTOFF_TP22", 0, false, false),
395 static unsigned char elf_code_to_howto_index[R_IA64_MAX_RELOC_CODE + 1];
397 /* Given a BFD reloc type, return the matching HOWTO structure. */
399 static reloc_howto_type*
400 lookup_howto (rtype)
401 unsigned int rtype;
403 static int inited = 0;
404 int i;
406 if (!inited)
408 inited = 1;
410 memset (elf_code_to_howto_index, 0xff, sizeof (elf_code_to_howto_index));
411 for (i = 0; i < NELEMS (ia64_howto_table); ++i)
412 elf_code_to_howto_index[ia64_howto_table[i].type] = i;
415 BFD_ASSERT (rtype <= R_IA64_MAX_RELOC_CODE);
416 i = elf_code_to_howto_index[rtype];
417 if (i >= NELEMS (ia64_howto_table))
418 return 0;
419 return ia64_howto_table + i;
422 static reloc_howto_type*
423 elf64_ia64_reloc_type_lookup (abfd, bfd_code)
424 bfd *abfd;
425 bfd_reloc_code_real_type bfd_code;
427 unsigned int rtype;
429 switch (bfd_code)
431 case BFD_RELOC_NONE: rtype = R_IA64_NONE; break;
433 case BFD_RELOC_IA64_IMM14: rtype = R_IA64_IMM14; break;
434 case BFD_RELOC_IA64_IMM22: rtype = R_IA64_IMM22; break;
435 case BFD_RELOC_IA64_IMM64: rtype = R_IA64_IMM64; break;
437 case BFD_RELOC_IA64_DIR32MSB: rtype = R_IA64_DIR32MSB; break;
438 case BFD_RELOC_IA64_DIR32LSB: rtype = R_IA64_DIR32LSB; break;
439 case BFD_RELOC_IA64_DIR64MSB: rtype = R_IA64_DIR64MSB; break;
440 case BFD_RELOC_IA64_DIR64LSB: rtype = R_IA64_DIR64LSB; break;
442 case BFD_RELOC_IA64_GPREL22: rtype = R_IA64_GPREL22; break;
443 case BFD_RELOC_IA64_GPREL64I: rtype = R_IA64_GPREL64I; break;
444 case BFD_RELOC_IA64_GPREL32MSB: rtype = R_IA64_GPREL32MSB; break;
445 case BFD_RELOC_IA64_GPREL32LSB: rtype = R_IA64_GPREL32LSB; break;
446 case BFD_RELOC_IA64_GPREL64MSB: rtype = R_IA64_GPREL64MSB; break;
447 case BFD_RELOC_IA64_GPREL64LSB: rtype = R_IA64_GPREL64LSB; break;
449 case BFD_RELOC_IA64_LTOFF22: rtype = R_IA64_LTOFF22; break;
450 case BFD_RELOC_IA64_LTOFF64I: rtype = R_IA64_LTOFF64I; break;
452 case BFD_RELOC_IA64_PLTOFF22: rtype = R_IA64_PLTOFF22; break;
453 case BFD_RELOC_IA64_PLTOFF64I: rtype = R_IA64_PLTOFF64I; break;
454 case BFD_RELOC_IA64_PLTOFF64MSB: rtype = R_IA64_PLTOFF64MSB; break;
455 case BFD_RELOC_IA64_PLTOFF64LSB: rtype = R_IA64_PLTOFF64LSB; break;
456 case BFD_RELOC_IA64_FPTR64I: rtype = R_IA64_FPTR64I; break;
457 case BFD_RELOC_IA64_FPTR32MSB: rtype = R_IA64_FPTR32MSB; break;
458 case BFD_RELOC_IA64_FPTR32LSB: rtype = R_IA64_FPTR32LSB; break;
459 case BFD_RELOC_IA64_FPTR64MSB: rtype = R_IA64_FPTR64MSB; break;
460 case BFD_RELOC_IA64_FPTR64LSB: rtype = R_IA64_FPTR64LSB; break;
462 case BFD_RELOC_IA64_PCREL21B: rtype = R_IA64_PCREL21B; break;
463 case BFD_RELOC_IA64_PCREL21BI: rtype = R_IA64_PCREL21BI; break;
464 case BFD_RELOC_IA64_PCREL21M: rtype = R_IA64_PCREL21M; break;
465 case BFD_RELOC_IA64_PCREL21F: rtype = R_IA64_PCREL21F; break;
466 case BFD_RELOC_IA64_PCREL22: rtype = R_IA64_PCREL22; break;
467 case BFD_RELOC_IA64_PCREL60B: rtype = R_IA64_PCREL60B; break;
468 case BFD_RELOC_IA64_PCREL64I: rtype = R_IA64_PCREL64I; break;
469 case BFD_RELOC_IA64_PCREL32MSB: rtype = R_IA64_PCREL32MSB; break;
470 case BFD_RELOC_IA64_PCREL32LSB: rtype = R_IA64_PCREL32LSB; break;
471 case BFD_RELOC_IA64_PCREL64MSB: rtype = R_IA64_PCREL64MSB; break;
472 case BFD_RELOC_IA64_PCREL64LSB: rtype = R_IA64_PCREL64LSB; break;
474 case BFD_RELOC_IA64_LTOFF_FPTR22: rtype = R_IA64_LTOFF_FPTR22; break;
475 case BFD_RELOC_IA64_LTOFF_FPTR64I: rtype = R_IA64_LTOFF_FPTR64I; break;
476 case BFD_RELOC_IA64_LTOFF_FPTR64MSB: rtype = R_IA64_LTOFF_FPTR64MSB; break;
477 case BFD_RELOC_IA64_LTOFF_FPTR64LSB: rtype = R_IA64_LTOFF_FPTR64LSB; break;
479 case BFD_RELOC_IA64_SEGBASE: rtype = R_IA64_SEGBASE; break;
480 case BFD_RELOC_IA64_SEGREL32MSB: rtype = R_IA64_SEGREL32MSB; break;
481 case BFD_RELOC_IA64_SEGREL32LSB: rtype = R_IA64_SEGREL32LSB; break;
482 case BFD_RELOC_IA64_SEGREL64MSB: rtype = R_IA64_SEGREL64MSB; break;
483 case BFD_RELOC_IA64_SEGREL64LSB: rtype = R_IA64_SEGREL64LSB; break;
485 case BFD_RELOC_IA64_SECREL32MSB: rtype = R_IA64_SECREL32MSB; break;
486 case BFD_RELOC_IA64_SECREL32LSB: rtype = R_IA64_SECREL32LSB; break;
487 case BFD_RELOC_IA64_SECREL64MSB: rtype = R_IA64_SECREL64MSB; break;
488 case BFD_RELOC_IA64_SECREL64LSB: rtype = R_IA64_SECREL64LSB; break;
490 case BFD_RELOC_IA64_REL32MSB: rtype = R_IA64_REL32MSB; break;
491 case BFD_RELOC_IA64_REL32LSB: rtype = R_IA64_REL32LSB; break;
492 case BFD_RELOC_IA64_REL64MSB: rtype = R_IA64_REL64MSB; break;
493 case BFD_RELOC_IA64_REL64LSB: rtype = R_IA64_REL64LSB; break;
495 case BFD_RELOC_IA64_LTV32MSB: rtype = R_IA64_LTV32MSB; break;
496 case BFD_RELOC_IA64_LTV32LSB: rtype = R_IA64_LTV32LSB; break;
497 case BFD_RELOC_IA64_LTV64MSB: rtype = R_IA64_LTV64MSB; break;
498 case BFD_RELOC_IA64_LTV64LSB: rtype = R_IA64_LTV64LSB; break;
500 case BFD_RELOC_IA64_IPLTMSB: rtype = R_IA64_IPLTMSB; break;
501 case BFD_RELOC_IA64_IPLTLSB: rtype = R_IA64_IPLTLSB; break;
502 case BFD_RELOC_IA64_EPLTMSB: rtype = R_IA64_EPLTMSB; break;
503 case BFD_RELOC_IA64_EPLTLSB: rtype = R_IA64_EPLTLSB; break;
504 case BFD_RELOC_IA64_COPY: rtype = R_IA64_COPY; break;
505 case BFD_RELOC_IA64_LTOFF22X: rtype = R_IA64_LTOFF22X; break;
506 case BFD_RELOC_IA64_LDXMOV: rtype = R_IA64_LDXMOV; break;
508 case BFD_RELOC_IA64_TPREL22: rtype = R_IA64_TPREL22; break;
509 case BFD_RELOC_IA64_TPREL64MSB: rtype = R_IA64_TPREL64MSB; break;
510 case BFD_RELOC_IA64_TPREL64LSB: rtype = R_IA64_TPREL64LSB; break;
511 case BFD_RELOC_IA64_LTOFF_TP22: rtype = R_IA64_LTOFF_TP22; break;
513 default: return 0;
515 return lookup_howto (rtype);
518 /* Given a ELF reloc, return the matching HOWTO structure. */
520 static void
521 elf64_ia64_info_to_howto (abfd, bfd_reloc, elf_reloc)
522 bfd *abfd;
523 arelent *bfd_reloc;
524 Elf64_Internal_Rela *elf_reloc;
526 bfd_reloc->howto = lookup_howto (ELF64_R_TYPE (elf_reloc->r_info));
529 #define PLT_HEADER_SIZE (3 * 16)
530 #define PLT_MIN_ENTRY_SIZE (1 * 16)
531 #define PLT_FULL_ENTRY_SIZE (2 * 16)
532 #define PLT_RESERVED_WORDS 3
534 static const bfd_byte plt_header[PLT_HEADER_SIZE] =
536 0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21, /* [MMI] mov r2=r14;; */
537 0xe0, 0x00, 0x08, 0x00, 0x48, 0x00, /* addl r14=0,r2 */
538 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
539 0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14, /* [MMI] ld8 r16=[r14],8;; */
540 0x10, 0x41, 0x38, 0x30, 0x28, 0x00, /* ld8 r17=[r14],8 */
541 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
542 0x11, 0x08, 0x00, 0x1c, 0x18, 0x10, /* [MIB] ld8 r1=[r14] */
543 0x60, 0x88, 0x04, 0x80, 0x03, 0x00, /* mov b6=r17 */
544 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
547 static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
549 0x11, 0x78, 0x00, 0x00, 0x00, 0x24, /* [MIB] mov r15=0 */
550 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, /* nop.i 0x0 */
551 0x00, 0x00, 0x00, 0x40 /* br.few 0 <PLT0>;; */
554 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
556 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
557 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
558 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
559 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
560 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
561 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
564 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
566 /* Select out of range branch fixup type. Note that Itanium does
567 not support brl, and so it gets emulated by the kernel. */
568 #undef USE_BRL
570 static const bfd_byte oor_brl[16] =
572 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
573 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.sptk.few tgt;; */
574 0x00, 0x00, 0x00, 0xc0
577 static const bfd_byte oor_ip[48] =
579 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
580 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, /* movl r15=0 */
581 0x01, 0x00, 0x00, 0x60,
582 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MII] nop.m 0 */
583 0x00, 0x01, 0x00, 0x60, 0x00, 0x00, /* mov r16=ip;; */
584 0xf2, 0x80, 0x00, 0x80, /* add r16=r15,r16;; */
585 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
586 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
587 0x60, 0x00, 0x80, 0x00 /* br b6;; */
590 /* These functions do relaxation for IA-64 ELF.
592 This is primarily to support branches to targets out of range;
593 relaxation of R_IA64_LTOFF22X and R_IA64_LDXMOV not yet supported. */
595 static boolean
596 elf64_ia64_relax_section (abfd, sec, link_info, again)
597 bfd *abfd;
598 asection *sec;
599 struct bfd_link_info *link_info;
600 boolean *again;
602 struct one_fixup
604 struct one_fixup *next;
605 asection *tsec;
606 bfd_vma toff;
607 bfd_vma trampoff;
610 Elf_Internal_Shdr *symtab_hdr;
611 Elf_Internal_Rela *internal_relocs;
612 Elf_Internal_Rela *free_relocs;
613 Elf_Internal_Rela *irel, *irelend;
614 bfd_byte *contents;
615 bfd_byte *free_contents;
616 Elf64_External_Sym *extsyms;
617 Elf64_External_Sym *free_extsyms;
618 struct elf64_ia64_link_hash_table *ia64_info;
619 struct one_fixup *fixups = NULL;
620 boolean changed_contents = false;
621 boolean changed_relocs = false;
623 /* Assume we're not going to change any sizes, and we we'll only
624 need one pass. */
625 *again = false;
627 /* Nothing to do if there are no relocations. */
628 if ((sec->flags & SEC_RELOC) == 0
629 || sec->reloc_count == 0)
630 return true;
632 /* If this is the first time we have been called for this section,
633 initialize the cooked size. */
634 if (sec->_cooked_size == 0)
635 sec->_cooked_size = sec->_raw_size;
637 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
639 /* Load the relocations for this section. */
640 internal_relocs = (_bfd_elf64_link_read_relocs
641 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
642 link_info->keep_memory));
643 if (internal_relocs == NULL)
644 goto error_return;
645 free_relocs = NULL;
646 if (! link_info->keep_memory)
647 free_relocs = internal_relocs;
649 ia64_info = elf64_ia64_hash_table (link_info);
650 irelend = internal_relocs + sec->reloc_count;
652 for (irel = internal_relocs; irel < irelend; irel++)
653 if (ELF64_R_TYPE (irel->r_info) == (int) R_IA64_PCREL21B)
654 break;
656 /* No branch-type relocations. */
657 if (irel == irelend)
659 if (free_relocs != NULL)
660 free (free_relocs);
661 return true;
664 /* Get the section contents. */
665 free_contents = NULL;
666 if (elf_section_data (sec)->this_hdr.contents != NULL)
667 contents = elf_section_data (sec)->this_hdr.contents;
668 else
670 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
671 if (contents == NULL)
672 goto error_return;
673 free_contents = contents;
675 if (! bfd_get_section_contents (abfd, sec, contents,
676 (file_ptr) 0, sec->_raw_size))
677 goto error_return;
680 /* Read this BFD's symbols. */
681 free_extsyms = NULL;
682 if (symtab_hdr->contents != NULL)
683 extsyms = (Elf64_External_Sym *) symtab_hdr->contents;
684 else
686 extsyms = (Elf64_External_Sym *) bfd_malloc (symtab_hdr->sh_size);
687 if (extsyms == NULL)
688 goto error_return;
689 free_extsyms = extsyms;
690 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
691 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
692 != symtab_hdr->sh_size))
693 goto error_return;
696 for (; irel < irelend; irel++)
698 bfd_vma symaddr, reladdr, trampoff, toff, roff;
699 Elf_Internal_Sym isym;
700 asection *tsec;
701 struct one_fixup *f;
703 if (ELF64_R_TYPE (irel->r_info) != (int) R_IA64_PCREL21B)
704 continue;
706 /* Get the value of the symbol referred to by the reloc. */
707 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
709 /* A local symbol. */
710 bfd_elf64_swap_symbol_in (abfd,
711 extsyms + ELF64_R_SYM (irel->r_info),
712 &isym);
713 if (isym.st_shndx == SHN_UNDEF)
714 continue; /* We can't do anthing with undefined symbols. */
715 else if (isym.st_shndx == SHN_ABS)
716 tsec = bfd_abs_section_ptr;
717 else if (isym.st_shndx == SHN_COMMON)
718 tsec = bfd_com_section_ptr;
719 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
720 tsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
721 else
722 continue; /* who knows. */
724 toff = isym.st_value;
726 else
728 unsigned long indx;
729 struct elf_link_hash_entry *h;
730 struct elf64_ia64_dyn_sym_info *dyn_i;
732 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
733 h = elf_sym_hashes (abfd)[indx];
734 BFD_ASSERT (h != NULL);
736 while (h->root.type == bfd_link_hash_indirect
737 || h->root.type == bfd_link_hash_warning)
738 h = (struct elf_link_hash_entry *) h->root.u.i.link;
740 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, false);
742 /* For branches to dynamic symbols, we're interested instead
743 in a branch to the PLT entry. */
744 if (dyn_i && dyn_i->want_plt2)
746 tsec = ia64_info->plt_sec;
747 toff = dyn_i->plt2_offset;
749 else
751 /* We can't do anthing with undefined symbols. */
752 if (h->root.type == bfd_link_hash_undefined
753 || h->root.type == bfd_link_hash_undefweak)
754 continue;
756 tsec = h->root.u.def.section;
757 toff = h->root.u.def.value;
761 symaddr = (tsec->output_section->vma
762 + tsec->output_offset
763 + toff
764 + irel->r_addend);
766 roff = irel->r_offset;
767 reladdr = (sec->output_section->vma
768 + sec->output_offset
769 + roff) & -4;
771 /* If the branch is in range, no need to do anything. */
772 if ((bfd_signed_vma) (symaddr - reladdr) >= -0x1000000
773 && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
774 continue;
776 /* If the branch and target are in the same section, you've
777 got one honking big section and we can't help you. You'll
778 get an error message later. */
779 if (tsec == sec)
780 continue;
782 /* Look for an existing fixup to this address. */
783 for (f = fixups; f ; f = f->next)
784 if (f->tsec == tsec && f->toff == toff)
785 break;
787 if (f == NULL)
789 /* Two alternatives: If it's a branch to a PLT entry, we can
790 make a copy of the FULL_PLT entry. Otherwise, we'll have
791 to use a `brl' insn to get where we're going. */
793 int size;
795 if (tsec == ia64_info->plt_sec)
796 size = sizeof (plt_full_entry);
797 else
799 #ifdef USE_BRL
800 size = sizeof (oor_brl);
801 #else
802 size = sizeof (oor_ip);
803 #endif
806 /* Resize the current section to make room for the new branch. */
807 trampoff = (sec->_cooked_size + 15) & -16;
808 contents = (bfd_byte *) bfd_realloc (contents, trampoff + size);
809 if (contents == NULL)
810 goto error_return;
811 sec->_cooked_size = trampoff + size;
813 if (tsec == ia64_info->plt_sec)
815 memcpy (contents + trampoff, plt_full_entry, size);
817 /* Hijack the old relocation for use as the PLTOFF reloc. */
818 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
819 R_IA64_PLTOFF22);
820 irel->r_offset = trampoff;
822 else
824 #ifdef USE_BRL
825 memcpy (contents + trampoff, oor_brl, size);
826 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
827 R_IA64_PCREL60B);
828 irel->r_offset = trampoff + 2;
829 #else
830 memcpy (contents + trampoff, oor_ip, size);
831 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
832 R_IA64_PCREL64I);
833 irel->r_addend -= 16;
834 irel->r_offset = trampoff + 2;
835 #endif
838 /* Record the fixup so we don't do it again this section. */
839 f = (struct one_fixup *) bfd_malloc (sizeof (*f));
840 f->next = fixups;
841 f->tsec = tsec;
842 f->toff = toff;
843 f->trampoff = trampoff;
844 fixups = f;
846 else
848 /* Nop out the reloc, since we're finalizing things here. */
849 irel->r_info = ELF64_R_INFO (0, R_IA64_NONE);
852 /* Fix up the existing branch to hit the trampoline. Hope like
853 hell this doesn't overflow too. */
854 if (elf64_ia64_install_value (abfd, contents + roff,
855 f->trampoff - (roff & -4),
856 R_IA64_PCREL21B) != bfd_reloc_ok)
857 goto error_return;
859 changed_contents = true;
860 changed_relocs = true;
863 /* Clean up and go home. */
864 while (fixups)
866 struct one_fixup *f = fixups;
867 fixups = fixups->next;
868 free (f);
871 if (changed_relocs)
872 elf_section_data (sec)->relocs = internal_relocs;
873 else if (free_relocs != NULL)
874 free (free_relocs);
876 if (changed_contents)
877 elf_section_data (sec)->this_hdr.contents = contents;
878 else if (free_contents != NULL)
880 if (! link_info->keep_memory)
881 free (free_contents);
882 else
884 /* Cache the section contents for elf_link_input_bfd. */
885 elf_section_data (sec)->this_hdr.contents = contents;
889 if (free_extsyms != NULL)
891 if (! link_info->keep_memory)
892 free (free_extsyms);
893 else
895 /* Cache the symbols for elf_link_input_bfd. */
896 symtab_hdr->contents = extsyms;
900 *again = changed_contents || changed_relocs;
901 return true;
903 error_return:
904 if (free_relocs != NULL)
905 free (free_relocs);
906 if (free_contents != NULL)
907 free (free_contents);
908 if (free_extsyms != NULL)
909 free (free_extsyms);
910 return false;
913 /* Handle an IA-64 specific section when reading an object file. This
914 is called when elfcode.h finds a section with an unknown type. */
916 static boolean
917 elf64_ia64_section_from_shdr (abfd, hdr, name)
918 bfd *abfd;
919 Elf64_Internal_Shdr *hdr;
920 char *name;
922 asection *newsect;
924 /* There ought to be a place to keep ELF backend specific flags, but
925 at the moment there isn't one. We just keep track of the
926 sections by their name, instead. Fortunately, the ABI gives
927 suggested names for all the MIPS specific sections, so we will
928 probably get away with this. */
929 switch (hdr->sh_type)
931 case SHT_IA_64_UNWIND:
932 if (strcmp (name, ELF_STRING_ia64_unwind) != 0)
933 return false;
934 break;
936 case SHT_IA_64_EXT:
937 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
938 return false;
939 break;
941 default:
942 return false;
945 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
946 return false;
947 newsect = hdr->bfd_section;
949 if (hdr->sh_flags & SHF_IA_64_SHORT)
950 newsect->flags |= SEC_SMALL_DATA;
952 return true;
955 /* Set the correct type for an IA-64 ELF section. We do this by the
956 section name, which is a hack, but ought to work. */
958 static boolean
959 elf64_ia64_fake_sections (abfd, hdr, sec)
960 bfd *abfd;
961 Elf64_Internal_Shdr *hdr;
962 asection *sec;
964 register const char *name;
966 name = bfd_get_section_name (abfd, sec);
968 if (strcmp (name, ELF_STRING_ia64_unwind) == 0)
969 hdr->sh_type = SHT_IA_64_UNWIND;
970 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
971 hdr->sh_type = SHT_IA_64_EXT;
972 else if (strcmp (name, ".reloc") == 0)
974 * This is an ugly, but unfortunately necessary hack that is
975 * needed when producing EFI binaries on IA-64. It tells
976 * elf.c:elf_fake_sections() not to consider ".reloc" as a section
977 * containing ELF relocation info. We need this hack in order to
978 * be able to generate ELF binaries that can be translated into
979 * EFI applications (which are essentially COFF objects). Those
980 * files contain a COFF ".reloc" section inside an ELF64 object,
981 * which would normally cause BFD to segfault because it would
982 * attempt to interpret this section as containing relocation
983 * entries for section "oc". With this hack enabled, ".reloc"
984 * will be treated as a normal data section, which will avoid the
985 * segfault. However, you won't be able to create an ELF64 binary
986 * with a section named "oc" that needs relocations, but that's
987 * the kind of ugly side-effects you get when detecting section
988 * types based on their names... In practice, this limitation is
989 * unlikely to bite.
991 hdr->sh_type = SHT_PROGBITS;
993 if (sec->flags & SEC_SMALL_DATA)
994 hdr->sh_flags |= SHF_IA_64_SHORT;
996 return true;
999 /* Hook called by the linker routine which adds symbols from an object
1000 file. We use it to put .comm items in .sbss, and not .bss. */
1002 static boolean
1003 elf64_ia64_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1004 bfd *abfd;
1005 struct bfd_link_info *info;
1006 const Elf_Internal_Sym *sym;
1007 const char **namep;
1008 flagword *flagsp;
1009 asection **secp;
1010 bfd_vma *valp;
1012 if (sym->st_shndx == SHN_COMMON
1013 && !info->relocateable
1014 && sym->st_size <= bfd_get_gp_size (abfd))
1016 /* Common symbols less than or equal to -G nn bytes are
1017 automatically put into .sbss. */
1019 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1021 if (scomm == NULL)
1023 scomm = bfd_make_section (abfd, ".scommon");
1024 if (scomm == NULL
1025 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
1026 | SEC_IS_COMMON
1027 | SEC_LINKER_CREATED)))
1028 return false;
1031 *secp = scomm;
1032 *valp = sym->st_size;
1035 return true;
1038 /* Return the number of additional phdrs we will need. */
1040 static int
1041 elf64_ia64_additional_program_headers (abfd)
1042 bfd *abfd;
1044 asection *s;
1045 int ret = 0;
1047 /* See if we need a PT_IA_64_ARCHEXT segment. */
1048 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1049 if (s && (s->flags & SEC_LOAD))
1050 ++ret;
1052 /* See if we need a PT_IA_64_UNWIND segment. */
1053 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
1054 if (s && (s->flags & SEC_LOAD))
1055 ++ret;
1057 return ret;
1060 static boolean
1061 elf64_ia64_modify_segment_map (abfd)
1062 bfd *abfd;
1064 struct elf_segment_map *m, **pm;
1065 asection *s;
1067 /* If we need a PT_IA_64_ARCHEXT segment, it must come before
1068 all PT_LOAD segments. */
1069 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1070 if (s && (s->flags & SEC_LOAD))
1072 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1073 if (m->p_type == PT_IA_64_ARCHEXT)
1074 break;
1075 if (m == NULL)
1077 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
1078 if (m == NULL)
1079 return false;
1081 m->p_type = PT_IA_64_ARCHEXT;
1082 m->count = 1;
1083 m->sections[0] = s;
1085 /* We want to put it after the PHDR and INTERP segments. */
1086 pm = &elf_tdata (abfd)->segment_map;
1087 while (*pm != NULL
1088 && ((*pm)->p_type == PT_PHDR
1089 || (*pm)->p_type == PT_INTERP))
1090 pm = &(*pm)->next;
1092 m->next = *pm;
1093 *pm = m;
1097 /* Install the PT_IA_64_UNWIND segment, if needed. */
1098 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
1099 if (s && (s->flags & SEC_LOAD))
1101 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1102 if (m->p_type == PT_IA_64_UNWIND)
1103 break;
1104 if (m == NULL)
1106 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
1107 if (m == NULL)
1108 return false;
1110 m->p_type = PT_IA_64_UNWIND;
1111 m->count = 1;
1112 m->sections[0] = s;
1113 m->next = NULL;
1115 /* We want to put it last. */
1116 pm = &elf_tdata (abfd)->segment_map;
1117 while (*pm != NULL)
1118 pm = &(*pm)->next;
1119 *pm = m;
1123 /* Turn on PF_IA_64_NORECOV if needed. This involves traversing all of
1124 the input sections for each output section in the segment and testing
1125 for SHF_IA_64_NORECOV on each. */
1126 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1127 if (m->p_type == PT_LOAD)
1129 int i;
1130 for (i = m->count - 1; i >= 0; --i)
1132 struct bfd_link_order *order = m->sections[i]->link_order_head;
1133 while (order)
1135 if (order->type == bfd_indirect_link_order)
1137 asection *is = order->u.indirect.section;
1138 bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
1139 if (flags & SHF_IA_64_NORECOV)
1141 m->p_flags |= PF_IA_64_NORECOV;
1142 goto found;
1145 order = order->next;
1148 found:;
1151 return true;
1155 /* According to the Tahoe assembler spec, all labels starting with a
1156 '.' are local. */
1158 static boolean
1159 elf64_ia64_is_local_label_name (abfd, name)
1160 bfd *abfd;
1161 const char *name;
1163 return name[0] == '.';
1166 /* Should we do dynamic things to this symbol? */
1168 static boolean
1169 elf64_ia64_dynamic_symbol_p (h, info)
1170 struct elf_link_hash_entry *h;
1171 struct bfd_link_info *info;
1173 if (h == NULL)
1174 return false;
1176 while (h->root.type == bfd_link_hash_indirect
1177 || h->root.type == bfd_link_hash_warning)
1178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1180 if (h->dynindx == -1)
1181 return false;
1183 if (h->root.type == bfd_link_hash_undefweak
1184 || h->root.type == bfd_link_hash_defweak)
1185 return true;
1187 if ((info->shared && !info->symbolic)
1188 || ((h->elf_link_hash_flags
1189 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))
1190 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
1191 return true;
1193 return false;
1196 static boolean
1197 elf64_ia64_local_hash_table_init (ht, abfd, new)
1198 struct elf64_ia64_local_hash_table *ht;
1199 bfd *abfd;
1200 new_hash_entry_func new;
1202 memset (ht, 0, sizeof(*ht));
1203 return bfd_hash_table_init (&ht->root, new);
1206 static struct bfd_hash_entry*
1207 elf64_ia64_new_loc_hash_entry (entry, table, string)
1208 struct bfd_hash_entry *entry;
1209 struct bfd_hash_table *table;
1210 const char *string;
1212 struct elf64_ia64_local_hash_entry *ret;
1213 ret = (struct elf64_ia64_local_hash_entry *) entry;
1215 /* Allocate the structure if it has not already been allocated by a
1216 subclass. */
1217 if (!ret)
1218 ret = bfd_hash_allocate (table, sizeof (*ret));
1220 if (!ret)
1221 return 0;
1223 /* Initialize our local data. All zeros, and definitely easier
1224 than setting a handful of bit fields. */
1225 memset (ret, 0, sizeof(*ret));
1227 /* Call the allocation method of the superclass. */
1228 ret = ((struct elf64_ia64_local_hash_entry *)
1229 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
1231 return (struct bfd_hash_entry *) ret;
1234 static struct bfd_hash_entry*
1235 elf64_ia64_new_elf_hash_entry (entry, table, string)
1236 struct bfd_hash_entry *entry;
1237 struct bfd_hash_table *table;
1238 const char *string;
1240 struct elf64_ia64_link_hash_entry *ret;
1241 ret = (struct elf64_ia64_link_hash_entry *) entry;
1243 /* Allocate the structure if it has not already been allocated by a
1244 subclass. */
1245 if (!ret)
1246 ret = bfd_hash_allocate (table, sizeof (*ret));
1248 if (!ret)
1249 return 0;
1251 /* Initialize our local data. All zeros, and definitely easier
1252 than setting a handful of bit fields. */
1253 memset (ret, 0, sizeof(*ret));
1255 /* Call the allocation method of the superclass. */
1256 ret = ((struct elf64_ia64_link_hash_entry *)
1257 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1258 table, string));
1260 return (struct bfd_hash_entry *) ret;
1263 static void
1264 elf64_ia64_hash_copy_indirect (xdir, xind)
1265 struct elf_link_hash_entry *xdir, *xind;
1267 struct elf64_ia64_link_hash_entry *dir, *ind;
1269 dir = (struct elf64_ia64_link_hash_entry *)xdir;
1270 ind = (struct elf64_ia64_link_hash_entry *)xind;
1272 /* Copy down any references that we may have already seen to the
1273 symbol which just became indirect. */
1275 dir->root.elf_link_hash_flags |=
1276 (ind->root.elf_link_hash_flags
1277 & (ELF_LINK_HASH_REF_DYNAMIC
1278 | ELF_LINK_HASH_REF_REGULAR
1279 | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
1281 /* Copy over the got and plt data. This would have been done
1282 by check_relocs. */
1284 if (dir->info == NULL)
1286 struct elf64_ia64_dyn_sym_info *dyn_i;
1288 dir->info = dyn_i = ind->info;
1289 ind->info = NULL;
1291 /* Fix up the dyn_sym_info pointers to the global symbol. */
1292 for (; dyn_i; dyn_i = dyn_i->next)
1293 dyn_i->h = &dir->root;
1295 BFD_ASSERT (ind->info == NULL);
1297 /* Copy over the dynindx. */
1299 if (dir->root.dynindx == -1)
1301 dir->root.dynindx = ind->root.dynindx;
1302 dir->root.dynstr_index = ind->root.dynstr_index;
1303 ind->root.dynindx = -1;
1304 ind->root.dynstr_index = 0;
1306 BFD_ASSERT (ind->root.dynindx == -1);
1309 static void
1310 elf64_ia64_hash_hide_symbol (xh)
1311 struct elf_link_hash_entry *xh;
1313 struct elf64_ia64_link_hash_entry *h;
1314 struct elf64_ia64_dyn_sym_info *dyn_i;
1316 h = (struct elf64_ia64_link_hash_entry *)xh;
1318 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1319 h->root.dynindx = -1;
1321 for (dyn_i = h->info; dyn_i; dyn_i = dyn_i->next)
1322 dyn_i->want_plt2 = 0;
1325 /* Create the derived linker hash table. The IA-64 ELF port uses this
1326 derived hash table to keep information specific to the IA-64 ElF
1327 linker (without using static variables). */
1329 static struct bfd_link_hash_table*
1330 elf64_ia64_hash_table_create (abfd)
1331 bfd *abfd;
1333 struct elf64_ia64_link_hash_table *ret;
1335 ret = bfd_alloc (abfd, sizeof (*ret));
1336 if (!ret)
1337 return 0;
1338 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
1339 elf64_ia64_new_elf_hash_entry))
1341 bfd_release (abfd, ret);
1342 return 0;
1345 if (!elf64_ia64_local_hash_table_init (&ret->loc_hash_table, abfd,
1346 elf64_ia64_new_loc_hash_entry))
1347 return 0;
1348 return &ret->root.root;
1351 /* Look up an entry in a Alpha ELF linker hash table. */
1353 static INLINE struct elf64_ia64_local_hash_entry *
1354 elf64_ia64_local_hash_lookup(table, string, create, copy)
1355 struct elf64_ia64_local_hash_table *table;
1356 const char *string;
1357 boolean create, copy;
1359 return ((struct elf64_ia64_local_hash_entry *)
1360 bfd_hash_lookup (&table->root, string, create, copy));
1363 /* Traverse both local and global hash tables. */
1365 struct elf64_ia64_dyn_sym_traverse_data
1367 boolean (*func) PARAMS ((struct elf64_ia64_dyn_sym_info *, PTR));
1368 PTR data;
1371 static boolean
1372 elf64_ia64_global_dyn_sym_thunk (xentry, xdata)
1373 struct bfd_hash_entry *xentry;
1374 PTR xdata;
1376 struct elf64_ia64_link_hash_entry *entry
1377 = (struct elf64_ia64_link_hash_entry *) xentry;
1378 struct elf64_ia64_dyn_sym_traverse_data *data
1379 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1380 struct elf64_ia64_dyn_sym_info *dyn_i;
1382 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1383 if (! (*data->func) (dyn_i, data->data))
1384 return false;
1385 return true;
1388 static boolean
1389 elf64_ia64_local_dyn_sym_thunk (xentry, xdata)
1390 struct bfd_hash_entry *xentry;
1391 PTR xdata;
1393 struct elf64_ia64_local_hash_entry *entry
1394 = (struct elf64_ia64_local_hash_entry *) xentry;
1395 struct elf64_ia64_dyn_sym_traverse_data *data
1396 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1397 struct elf64_ia64_dyn_sym_info *dyn_i;
1399 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1400 if (! (*data->func) (dyn_i, data->data))
1401 return false;
1402 return true;
1405 static void
1406 elf64_ia64_dyn_sym_traverse (ia64_info, func, data)
1407 struct elf64_ia64_link_hash_table *ia64_info;
1408 boolean (*func) PARAMS ((struct elf64_ia64_dyn_sym_info *, PTR));
1409 PTR data;
1411 struct elf64_ia64_dyn_sym_traverse_data xdata;
1413 xdata.func = func;
1414 xdata.data = data;
1416 elf_link_hash_traverse (&ia64_info->root,
1417 elf64_ia64_global_dyn_sym_thunk, &xdata);
1418 bfd_hash_traverse (&ia64_info->loc_hash_table.root,
1419 elf64_ia64_local_dyn_sym_thunk, &xdata);
1422 static boolean
1423 elf64_ia64_create_dynamic_sections (abfd, info)
1424 bfd *abfd;
1425 struct bfd_link_info *info;
1427 struct elf64_ia64_link_hash_table *ia64_info;
1428 struct elf_link_hash_entry *h;
1429 asection *s;
1431 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1432 return false;
1434 ia64_info = elf64_ia64_hash_table (info);
1436 ia64_info->plt_sec = bfd_get_section_by_name (abfd, ".plt");
1437 ia64_info->got_sec = bfd_get_section_by_name (abfd, ".got");
1440 flagword flags = bfd_get_section_flags (abfd, ia64_info->got_sec);
1441 bfd_set_section_flags (abfd, ia64_info->got_sec, SEC_SMALL_DATA | flags);
1444 if (!get_pltoff (abfd, info, ia64_info))
1445 return false;
1447 s = bfd_make_section(abfd, ".rela.IA_64.pltoff");
1448 if (s == NULL
1449 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1450 | SEC_HAS_CONTENTS
1451 | SEC_IN_MEMORY
1452 | SEC_LINKER_CREATED
1453 | SEC_READONLY))
1454 || !bfd_set_section_alignment (abfd, s, 3))
1455 return false;
1456 ia64_info->rel_pltoff_sec = s;
1458 s = bfd_make_section(abfd, ".rela.got");
1459 if (s == NULL
1460 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1461 | SEC_HAS_CONTENTS
1462 | SEC_IN_MEMORY
1463 | SEC_LINKER_CREATED
1464 | SEC_READONLY))
1465 || !bfd_set_section_alignment (abfd, s, 3))
1466 return false;
1467 ia64_info->rel_got_sec = s;
1469 return true;
1472 /* Find and/or create a descriptor for dynamic symbol info. This will
1473 vary based on global or local symbol, and the addend to the reloc. */
1475 static struct elf64_ia64_dyn_sym_info *
1476 get_dyn_sym_info (ia64_info, h, abfd, rel, create)
1477 struct elf64_ia64_link_hash_table *ia64_info;
1478 struct elf_link_hash_entry *h;
1479 bfd *abfd;
1480 const Elf_Internal_Rela *rel;
1481 boolean create;
1483 struct elf64_ia64_dyn_sym_info **pp;
1484 struct elf64_ia64_dyn_sym_info *dyn_i;
1485 bfd_vma addend = rel ? rel->r_addend : 0;
1487 if (h)
1488 pp = &((struct elf64_ia64_link_hash_entry *)h)->info;
1489 else
1491 struct elf64_ia64_local_hash_entry *loc_h;
1492 char *addr_name;
1493 size_t len;
1495 /* Construct a string for use in the elf64_ia64_local_hash_table.
1496 The name describes what was once anonymous memory. */
1498 len = sizeof(void*)*2 + 1 + sizeof(bfd_vma)*4 + 1 + 1;
1499 len += 10; /* %p slop */
1501 addr_name = alloca (len);
1502 sprintf (addr_name, "%p:%lx", abfd, ELF64_R_SYM (rel->r_info));
1504 /* Collect the canonical entry data for this address. */
1505 loc_h = elf64_ia64_local_hash_lookup (&ia64_info->loc_hash_table,
1506 addr_name, create, create);
1507 BFD_ASSERT (loc_h);
1509 pp = &loc_h->info;
1512 for (dyn_i = *pp; dyn_i && dyn_i->addend != addend; dyn_i = *pp)
1513 pp = &dyn_i->next;
1515 if (dyn_i == NULL && create)
1517 dyn_i = (struct elf64_ia64_dyn_sym_info *)
1518 bfd_zalloc (abfd, sizeof *dyn_i);
1519 *pp = dyn_i;
1520 dyn_i->addend = addend;
1523 return dyn_i;
1526 static asection *
1527 get_got (abfd, info, ia64_info)
1528 bfd *abfd;
1529 struct bfd_link_info *info;
1530 struct elf64_ia64_link_hash_table *ia64_info;
1532 asection *got, *srel;
1533 bfd *dynobj;
1535 got = ia64_info->got_sec;
1536 if (!got)
1538 flagword flags;
1540 dynobj = ia64_info->root.dynobj;
1541 if (!dynobj)
1542 ia64_info->root.dynobj = dynobj = abfd;
1543 if (!_bfd_elf_create_got_section (dynobj, info))
1544 return 0;
1546 got = bfd_get_section_by_name (dynobj, ".got");
1547 BFD_ASSERT (got);
1548 ia64_info->got_sec = got;
1550 flags = bfd_get_section_flags (abfd, got);
1551 bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags);
1554 return got;
1557 /* Create function descriptor section (.opd). This section is called .opd
1558 because it contains "official prodecure descriptors". The "official"
1559 refers to the fact that these descriptors are used when taking the address
1560 of a procedure, thus ensuring a unique address for each procedure. */
1562 static asection *
1563 get_fptr (abfd, info, ia64_info)
1564 bfd *abfd;
1565 struct bfd_link_info *info;
1566 struct elf64_ia64_link_hash_table *ia64_info;
1568 asection *fptr;
1569 bfd *dynobj;
1571 fptr = ia64_info->fptr_sec;
1572 if (!fptr)
1574 dynobj = ia64_info->root.dynobj;
1575 if (!dynobj)
1576 ia64_info->root.dynobj = dynobj = abfd;
1578 fptr = bfd_make_section (dynobj, ".opd");
1579 if (!fptr
1580 || !bfd_set_section_flags (dynobj, fptr,
1581 (SEC_ALLOC
1582 | SEC_LOAD
1583 | SEC_HAS_CONTENTS
1584 | SEC_IN_MEMORY
1585 | SEC_READONLY
1586 | SEC_LINKER_CREATED))
1587 || !bfd_set_section_alignment (abfd, fptr, 4))
1589 BFD_ASSERT (0);
1590 return NULL;
1593 ia64_info->fptr_sec = fptr;
1596 return fptr;
1599 static asection *
1600 get_pltoff (abfd, info, ia64_info)
1601 bfd *abfd;
1602 struct bfd_link_info *info;
1603 struct elf64_ia64_link_hash_table *ia64_info;
1605 asection *pltoff;
1606 bfd *dynobj;
1608 pltoff = ia64_info->pltoff_sec;
1609 if (!pltoff)
1611 dynobj = ia64_info->root.dynobj;
1612 if (!dynobj)
1613 ia64_info->root.dynobj = dynobj = abfd;
1615 pltoff = bfd_make_section (dynobj, ELF_STRING_ia64_pltoff);
1616 if (!pltoff
1617 || !bfd_set_section_flags (dynobj, pltoff,
1618 (SEC_ALLOC
1619 | SEC_LOAD
1620 | SEC_HAS_CONTENTS
1621 | SEC_IN_MEMORY
1622 | SEC_SMALL_DATA
1623 | SEC_LINKER_CREATED))
1624 || !bfd_set_section_alignment (abfd, pltoff, 4))
1626 BFD_ASSERT (0);
1627 return NULL;
1630 ia64_info->pltoff_sec = pltoff;
1633 return pltoff;
1636 static asection *
1637 get_reloc_section (abfd, ia64_info, sec, create)
1638 bfd *abfd;
1639 struct elf64_ia64_link_hash_table *ia64_info;
1640 asection *sec;
1641 boolean create;
1643 const char *srel_name;
1644 asection *srel;
1645 bfd *dynobj;
1647 srel_name = (bfd_elf_string_from_elf_section
1648 (abfd, elf_elfheader(abfd)->e_shstrndx,
1649 elf_section_data(sec)->rel_hdr.sh_name));
1650 if (srel_name == NULL)
1651 return NULL;
1653 BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
1654 && strcmp (bfd_get_section_name (abfd, sec),
1655 srel_name+5) == 0)
1656 || (strncmp (srel_name, ".rel", 4) == 0
1657 && strcmp (bfd_get_section_name (abfd, sec),
1658 srel_name+4) == 0));
1660 dynobj = ia64_info->root.dynobj;
1661 if (!dynobj)
1662 ia64_info->root.dynobj = dynobj = abfd;
1664 srel = bfd_get_section_by_name (dynobj, srel_name);
1665 if (srel == NULL && create)
1667 srel = bfd_make_section (dynobj, srel_name);
1668 if (srel == NULL
1669 || !bfd_set_section_flags (dynobj, srel,
1670 (SEC_ALLOC
1671 | SEC_LOAD
1672 | SEC_HAS_CONTENTS
1673 | SEC_IN_MEMORY
1674 | SEC_LINKER_CREATED
1675 | SEC_READONLY))
1676 || !bfd_set_section_alignment (dynobj, srel, 3))
1677 return NULL;
1680 return srel;
1683 static boolean
1684 count_dyn_reloc (abfd, dyn_i, srel, type)
1685 bfd *abfd;
1686 struct elf64_ia64_dyn_sym_info *dyn_i;
1687 asection *srel;
1688 int type;
1690 struct elf64_ia64_dyn_reloc_entry *rent;
1692 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
1693 if (rent->srel == srel && rent->type == type)
1694 break;
1696 if (!rent)
1698 rent = (struct elf64_ia64_dyn_reloc_entry *)
1699 bfd_alloc (abfd, sizeof (*rent));
1700 if (!rent)
1701 return false;
1703 rent->next = dyn_i->reloc_entries;
1704 rent->srel = srel;
1705 rent->type = type;
1706 rent->count = 0;
1707 dyn_i->reloc_entries = rent;
1709 rent->count++;
1711 return true;
1714 static boolean
1715 elf64_ia64_check_relocs (abfd, info, sec, relocs)
1716 bfd *abfd;
1717 struct bfd_link_info *info;
1718 asection *sec;
1719 const Elf_Internal_Rela *relocs;
1721 struct elf64_ia64_link_hash_table *ia64_info;
1722 const Elf_Internal_Rela *relend;
1723 Elf_Internal_Shdr *symtab_hdr;
1724 const Elf_Internal_Rela *rel;
1725 asection *got, *fptr, *srel;
1727 if (info->relocateable)
1728 return true;
1730 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1731 ia64_info = elf64_ia64_hash_table (info);
1733 got = fptr = srel = NULL;
1735 relend = relocs + sec->reloc_count;
1736 for (rel = relocs; rel < relend; ++rel)
1738 enum {
1739 NEED_GOT = 1,
1740 NEED_FPTR = 2,
1741 NEED_PLTOFF = 4,
1742 NEED_MIN_PLT = 8,
1743 NEED_FULL_PLT = 16,
1744 NEED_DYNREL = 32,
1745 NEED_LTOFF_FPTR = 64,
1748 struct elf_link_hash_entry *h = NULL;
1749 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
1750 struct elf64_ia64_dyn_sym_info *dyn_i;
1751 int need_entry;
1752 boolean maybe_dynamic;
1753 int dynrel_type;
1755 if (r_symndx >= symtab_hdr->sh_info)
1757 /* We're dealing with a global symbol -- find its hash entry
1758 and mark it as being referenced. */
1759 long indx = r_symndx - symtab_hdr->sh_info;
1760 h = elf_sym_hashes (abfd)[indx];
1761 while (h->root.type == bfd_link_hash_indirect
1762 || h->root.type == bfd_link_hash_warning)
1763 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1765 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1768 /* We can only get preliminary data on whether a symbol is
1769 locally or externally defined, as not all of the input files
1770 have yet been processed. Do something with what we know, as
1771 this may help reduce memory usage and processing time later. */
1772 maybe_dynamic = false;
1773 if (h && ((info->shared && ! info->symbolic)
1774 || ! (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
1775 || h->root.type == bfd_link_hash_defweak))
1776 maybe_dynamic = true;
1778 need_entry = 0;
1779 switch (ELF64_R_TYPE (rel->r_info))
1781 case R_IA64_TPREL22:
1782 case R_IA64_TPREL64MSB:
1783 case R_IA64_TPREL64LSB:
1784 case R_IA64_LTOFF_TP22:
1785 return false;
1787 case R_IA64_LTOFF_FPTR22:
1788 case R_IA64_LTOFF_FPTR64I:
1789 case R_IA64_LTOFF_FPTR64MSB:
1790 case R_IA64_LTOFF_FPTR64LSB:
1791 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
1792 break;
1794 case R_IA64_FPTR64I:
1795 case R_IA64_FPTR32MSB:
1796 case R_IA64_FPTR32LSB:
1797 case R_IA64_FPTR64MSB:
1798 case R_IA64_FPTR64LSB:
1799 if (info->shared || h)
1800 need_entry = NEED_FPTR | NEED_DYNREL;
1801 else
1802 need_entry = NEED_FPTR;
1803 dynrel_type = R_IA64_FPTR64LSB;
1804 break;
1806 case R_IA64_LTOFF22:
1807 case R_IA64_LTOFF22X:
1808 case R_IA64_LTOFF64I:
1809 need_entry = NEED_GOT;
1810 break;
1812 case R_IA64_PLTOFF22:
1813 case R_IA64_PLTOFF64I:
1814 case R_IA64_PLTOFF64MSB:
1815 case R_IA64_PLTOFF64LSB:
1816 need_entry = NEED_PLTOFF;
1817 if (h)
1819 if (maybe_dynamic)
1820 need_entry |= NEED_MIN_PLT;
1822 else
1824 (*info->callbacks->warning)
1825 (info, _("@pltoff reloc against local symbol"), 0,
1826 abfd, 0, 0);
1828 break;
1830 case R_IA64_PCREL21B:
1831 case R_IA64_PCREL60B:
1832 /* Depending on where this symbol is defined, we may or may not
1833 need a full plt entry. Only skip if we know we'll not need
1834 the entry -- static or symbolic, and the symbol definition
1835 has already been seen. */
1836 if (maybe_dynamic && rel->r_addend == 0)
1837 need_entry = NEED_FULL_PLT;
1838 break;
1840 case R_IA64_IMM14:
1841 case R_IA64_IMM22:
1842 case R_IA64_IMM64:
1843 case R_IA64_DIR32MSB:
1844 case R_IA64_DIR32LSB:
1845 case R_IA64_DIR64MSB:
1846 case R_IA64_DIR64LSB:
1847 /* Shared objects will always need at least a REL relocation. */
1848 if (info->shared || maybe_dynamic)
1849 need_entry = NEED_DYNREL;
1850 dynrel_type = R_IA64_DIR64LSB;
1851 break;
1853 case R_IA64_PCREL22:
1854 case R_IA64_PCREL64I:
1855 case R_IA64_PCREL32MSB:
1856 case R_IA64_PCREL32LSB:
1857 case R_IA64_PCREL64MSB:
1858 case R_IA64_PCREL64LSB:
1859 if (maybe_dynamic)
1860 need_entry = NEED_DYNREL;
1861 dynrel_type = R_IA64_PCREL64LSB;
1862 break;
1865 if (!need_entry)
1866 continue;
1868 if ((need_entry & NEED_FPTR) != 0
1869 && rel->r_addend)
1871 (*info->callbacks->warning)
1872 (info, _("non-zero addend in @fptr reloc"), 0,
1873 abfd, 0, 0);
1876 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, true);
1878 /* Record whether or not this is a local symbol. */
1879 dyn_i->h = h;
1881 /* Create what's needed. */
1882 if (need_entry & NEED_GOT)
1884 if (!got)
1886 got = get_got (abfd, info, ia64_info);
1887 if (!got)
1888 return false;
1890 dyn_i->want_got = 1;
1892 if (need_entry & NEED_FPTR)
1894 if (!fptr)
1896 fptr = get_fptr (abfd, info, ia64_info);
1897 if (!fptr)
1898 return false;
1901 /* FPTRs for shared libraries are allocated by the dynamic
1902 linker. Make sure this local symbol will appear in the
1903 dynamic symbol table. */
1904 if (!h && info->shared)
1906 if (! (_bfd_elf64_link_record_local_dynamic_symbol
1907 (info, abfd, r_symndx)))
1908 return false;
1911 dyn_i->want_fptr = 1;
1913 if (need_entry & NEED_LTOFF_FPTR)
1914 dyn_i->want_ltoff_fptr = 1;
1915 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
1917 if (!ia64_info->root.dynobj)
1918 ia64_info->root.dynobj = abfd;
1919 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1920 dyn_i->want_plt = 1;
1922 if (need_entry & NEED_FULL_PLT)
1923 dyn_i->want_plt2 = 1;
1924 if (need_entry & NEED_PLTOFF)
1925 dyn_i->want_pltoff = 1;
1926 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
1928 if (!srel)
1930 srel = get_reloc_section (abfd, ia64_info, sec, true);
1931 if (!srel)
1932 return false;
1934 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
1935 return false;
1939 return true;
1942 struct elf64_ia64_allocate_data
1944 struct bfd_link_info *info;
1945 bfd_size_type ofs;
1948 /* For cleanliness, and potentially faster dynamic loading, allocate
1949 external GOT entries first. */
1951 static boolean
1952 allocate_global_data_got (dyn_i, data)
1953 struct elf64_ia64_dyn_sym_info *dyn_i;
1954 PTR data;
1956 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
1958 if (dyn_i->want_got
1959 && ! dyn_i->want_fptr
1960 && elf64_ia64_dynamic_symbol_p (dyn_i->h, x->info))
1962 dyn_i->got_offset = x->ofs;
1963 x->ofs += 8;
1965 return true;
1968 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
1970 static boolean
1971 allocate_global_fptr_got (dyn_i, data)
1972 struct elf64_ia64_dyn_sym_info *dyn_i;
1973 PTR data;
1975 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
1977 if (dyn_i->want_got
1978 && dyn_i->want_fptr
1979 && elf64_ia64_dynamic_symbol_p (dyn_i->h, x->info))
1981 dyn_i->got_offset = x->ofs;
1982 x->ofs += 8;
1984 return true;
1987 /* Lastly, allocate all the GOT entries for local data. */
1989 static boolean
1990 allocate_local_got (dyn_i, data)
1991 struct elf64_ia64_dyn_sym_info *dyn_i;
1992 PTR data;
1994 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
1996 if (dyn_i->want_got
1997 && ! elf64_ia64_dynamic_symbol_p (dyn_i->h, x->info))
1999 dyn_i->got_offset = x->ofs;
2000 x->ofs += 8;
2002 return true;
2005 /* Search for the index of a global symbol in it's defining object file. */
2007 static unsigned long
2008 global_sym_index (h)
2009 struct elf_link_hash_entry *h;
2011 struct elf_link_hash_entry **p;
2012 bfd *obj;
2014 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2015 || h->root.type == bfd_link_hash_defweak);
2017 obj = h->root.u.def.section->owner;
2018 for (p = elf_sym_hashes (obj); *p != h; ++p)
2019 continue;
2021 return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
2024 /* Allocate function descriptors. We can do these for every function
2025 in a main executable that is not exported. */
2027 static boolean
2028 allocate_fptr (dyn_i, data)
2029 struct elf64_ia64_dyn_sym_info *dyn_i;
2030 PTR data;
2032 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2034 if (dyn_i->want_fptr)
2036 struct elf_link_hash_entry *h = dyn_i->h;
2038 if (h)
2039 while (h->root.type == bfd_link_hash_indirect
2040 || h->root.type == bfd_link_hash_warning)
2041 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2043 if (x->info->shared)
2045 if (h && h->dynindx == -1)
2047 BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
2048 || (h->root.type == bfd_link_hash_defweak));
2050 if (!_bfd_elf64_link_record_local_dynamic_symbol
2051 (x->info, h->root.u.def.section->owner,
2052 global_sym_index (h)))
2053 return false;
2056 dyn_i->want_fptr = 0;
2058 else if (h == NULL || h->dynindx == -1)
2060 dyn_i->fptr_offset = x->ofs;
2061 x->ofs += 16;
2063 else
2064 dyn_i->want_fptr = 0;
2066 return true;
2069 /* Allocate all the minimal PLT entries. */
2071 static boolean
2072 allocate_plt_entries (dyn_i, data)
2073 struct elf64_ia64_dyn_sym_info *dyn_i;
2074 PTR data;
2076 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2078 if (dyn_i->want_plt)
2080 struct elf_link_hash_entry *h = dyn_i->h;
2082 if (h)
2083 while (h->root.type == bfd_link_hash_indirect
2084 || h->root.type == bfd_link_hash_warning)
2085 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2087 /* ??? Versioned symbols seem to lose ELF_LINK_HASH_NEEDS_PLT. */
2088 if (elf64_ia64_dynamic_symbol_p (h, x->info))
2090 bfd_size_type offset = x->ofs;
2091 if (offset == 0)
2092 offset = PLT_HEADER_SIZE;
2093 dyn_i->plt_offset = offset;
2094 x->ofs = offset + PLT_MIN_ENTRY_SIZE;
2096 dyn_i->want_pltoff = 1;
2098 else
2100 dyn_i->want_plt = 0;
2101 dyn_i->want_plt2 = 0;
2104 return true;
2107 /* Allocate all the full PLT entries. */
2109 static boolean
2110 allocate_plt2_entries (dyn_i, data)
2111 struct elf64_ia64_dyn_sym_info *dyn_i;
2112 PTR data;
2114 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2116 if (dyn_i->want_plt2)
2118 struct elf_link_hash_entry *h = dyn_i->h;
2119 bfd_size_type ofs = x->ofs;
2121 dyn_i->plt2_offset = ofs;
2122 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2124 while (h->root.type == bfd_link_hash_indirect
2125 || h->root.type == bfd_link_hash_warning)
2126 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2127 dyn_i->h->plt.offset = ofs;
2129 return true;
2132 /* Allocate all the PLTOFF entries requested by relocations and
2133 plt entries. We can't share space with allocated FPTR entries,
2134 because the latter are not necessarily addressable by the GP.
2135 ??? Relaxation might be able to determine that they are. */
2137 static boolean
2138 allocate_pltoff_entries (dyn_i, data)
2139 struct elf64_ia64_dyn_sym_info *dyn_i;
2140 PTR data;
2142 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2144 if (dyn_i->want_pltoff)
2146 dyn_i->pltoff_offset = x->ofs;
2147 x->ofs += 16;
2149 return true;
2152 /* Allocate dynamic relocations for those symbols that turned out
2153 to be dynamic. */
2155 static boolean
2156 allocate_dynrel_entries (dyn_i, data)
2157 struct elf64_ia64_dyn_sym_info *dyn_i;
2158 PTR data;
2160 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2161 struct elf64_ia64_link_hash_table *ia64_info;
2162 struct elf64_ia64_dyn_reloc_entry *rent;
2163 boolean dynamic_symbol, shared;
2165 ia64_info = elf64_ia64_hash_table (x->info);
2166 dynamic_symbol = elf64_ia64_dynamic_symbol_p (dyn_i->h, x->info);
2167 shared = x->info->shared;
2169 /* Take care of the normal data relocations. */
2171 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2173 switch (rent->type)
2175 case R_IA64_FPTR64LSB:
2176 /* Allocate one iff !want_fptr, which by this point will
2177 be true only if we're actually allocating one statically
2178 in the main executable. */
2179 if (dyn_i->want_fptr)
2180 continue;
2181 break;
2182 case R_IA64_PCREL64LSB:
2183 if (!dynamic_symbol)
2184 continue;
2185 break;
2186 case R_IA64_DIR64LSB:
2187 if (!dynamic_symbol && !shared)
2188 continue;
2189 break;
2191 rent->srel->_raw_size += sizeof (Elf64_External_Rela) * rent->count;
2194 /* Take care of the GOT and PLT relocations. */
2196 if (((dynamic_symbol || shared) && dyn_i->want_got)
2197 || (dyn_i->want_ltoff_fptr && dyn_i->h && dyn_i->h->dynindx != -1))
2198 ia64_info->rel_got_sec->_raw_size += sizeof (Elf64_External_Rela);
2200 if (dyn_i->want_pltoff)
2202 bfd_size_type t = 0;
2204 /* Dynamic symbols get one IPLT relocation. Local symbols in
2205 shared libraries get two REL relocations. Local symbols in
2206 main applications get nothing. */
2207 if (dynamic_symbol)
2208 t = sizeof (Elf64_External_Rela);
2209 else if (shared)
2210 t = 2 * sizeof (Elf64_External_Rela);
2212 ia64_info->rel_pltoff_sec->_raw_size += t;
2215 return true;
2218 static boolean
2219 elf64_ia64_adjust_dynamic_symbol (info, h)
2220 struct bfd_link_info *info;
2221 struct elf_link_hash_entry *h;
2223 /* ??? Undefined symbols with PLT entries should be re-defined
2224 to be the PLT entry. */
2226 /* If this is a weak symbol, and there is a real definition, the
2227 processor independent code will have arranged for us to see the
2228 real definition first, and we can just use the same value. */
2229 if (h->weakdef != NULL)
2231 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2232 || h->weakdef->root.type == bfd_link_hash_defweak);
2233 h->root.u.def.section = h->weakdef->root.u.def.section;
2234 h->root.u.def.value = h->weakdef->root.u.def.value;
2235 return true;
2238 /* If this is a reference to a symbol defined by a dynamic object which
2239 is not a function, we might allocate the symbol in our .dynbss section
2240 and allocate a COPY dynamic relocation.
2242 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2243 of hackery. */
2245 return true;
2248 static boolean
2249 elf64_ia64_size_dynamic_sections (output_bfd, info)
2250 bfd *output_bfd;
2251 struct bfd_link_info *info;
2253 struct elf64_ia64_allocate_data data;
2254 struct elf64_ia64_link_hash_table *ia64_info;
2255 asection *sec;
2256 bfd *dynobj;
2257 boolean reltext = false;
2258 boolean relplt = false;
2260 dynobj = elf_hash_table(info)->dynobj;
2261 ia64_info = elf64_ia64_hash_table (info);
2262 BFD_ASSERT(dynobj != NULL);
2263 data.info = info;
2265 /* Set the contents of the .interp section to the interpreter. */
2266 if (ia64_info->root.dynamic_sections_created
2267 && !info->shared)
2269 sec = bfd_get_section_by_name (dynobj, ".interp");
2270 BFD_ASSERT (sec != NULL);
2271 sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
2272 sec->_raw_size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
2275 /* DT_INIT and DT_FINI get function descriptors not raw code addresses.
2276 Force their symbols to have pltoff entries so we can use those. */
2277 if (ia64_info->root.dynamic_sections_created)
2279 struct elf_link_hash_entry *h;
2280 struct elf64_ia64_dyn_sym_info *dyn_i;
2282 if (info->init_function
2283 && (h = elf_link_hash_lookup (elf_hash_table (info),
2284 info->init_function, false,
2285 false, false))
2286 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2287 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2289 dyn_i = get_dyn_sym_info (ia64_info, h, output_bfd, NULL, true);
2290 dyn_i->want_pltoff = 1;
2293 if (info->fini_function
2294 && (h = elf_link_hash_lookup (elf_hash_table (info),
2295 info->fini_function, false,
2296 false, false))
2297 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2298 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2300 dyn_i = get_dyn_sym_info (ia64_info, h, output_bfd, NULL, true);
2301 dyn_i->want_pltoff = 1;
2305 /* Allocate the GOT entries. */
2307 if (ia64_info->got_sec)
2309 data.ofs = 0;
2310 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2311 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2312 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
2313 ia64_info->got_sec->_raw_size = data.ofs;
2316 /* Allocate the FPTR entries. */
2318 if (ia64_info->fptr_sec)
2320 data.ofs = 0;
2321 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
2322 ia64_info->fptr_sec->_raw_size = data.ofs;
2325 /* Now that we've seen all of the input files, we can decide which
2326 symbols need plt entries. Allocate the minimal PLT entries first.
2327 We do this even though dynamic_sections_created may be false, because
2328 this has the side-effect of clearing want_plt and want_plt2. */
2330 data.ofs = 0;
2331 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
2333 ia64_info->minplt_entries = 0;
2334 if (data.ofs)
2336 ia64_info->minplt_entries
2337 = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
2340 /* Align the pointer for the plt2 entries. */
2341 data.ofs = (data.ofs + 31) & -32;
2343 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
2344 if (data.ofs != 0)
2346 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2348 ia64_info->plt_sec->_raw_size = data.ofs;
2350 /* If we've got a .plt, we need some extra memory for the dynamic
2351 linker. We stuff these in .got.plt. */
2352 sec = bfd_get_section_by_name (dynobj, ".got.plt");
2353 sec->_raw_size = 8 * PLT_RESERVED_WORDS;
2356 /* Allocate the PLTOFF entries. */
2358 if (ia64_info->pltoff_sec)
2360 data.ofs = 0;
2361 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
2362 ia64_info->pltoff_sec->_raw_size = data.ofs;
2365 if (ia64_info->root.dynamic_sections_created)
2367 /* Allocate space for the dynamic relocations that turned out to be
2368 required. */
2370 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
2373 /* We have now determined the sizes of the various dynamic sections.
2374 Allocate memory for them. */
2375 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2377 boolean strip;
2379 if (!(sec->flags & SEC_LINKER_CREATED))
2380 continue;
2382 /* If we don't need this section, strip it from the output file.
2383 There were several sections primarily related to dynamic
2384 linking that must be create before the linker maps input
2385 sections to output sections. The linker does that before
2386 bfd_elf_size_dynamic_sections is called, and it is that
2387 function which decides whether anything needs to go into
2388 these sections. */
2390 strip = (sec->_raw_size == 0);
2392 if (sec == ia64_info->got_sec)
2393 strip = false;
2394 else if (sec == ia64_info->rel_got_sec)
2396 if (strip)
2397 ia64_info->rel_got_sec = NULL;
2398 else
2399 /* We use the reloc_count field as a counter if we need to
2400 copy relocs into the output file. */
2401 sec->reloc_count = 0;
2403 else if (sec == ia64_info->fptr_sec)
2405 if (strip)
2406 ia64_info->fptr_sec = NULL;
2408 else if (sec == ia64_info->plt_sec)
2410 if (strip)
2411 ia64_info->plt_sec = NULL;
2413 else if (sec == ia64_info->pltoff_sec)
2415 if (strip)
2416 ia64_info->pltoff_sec = NULL;
2418 else if (sec == ia64_info->rel_pltoff_sec)
2420 if (strip)
2421 ia64_info->rel_pltoff_sec = NULL;
2422 else
2424 relplt = true;
2425 /* We use the reloc_count field as a counter if we need to
2426 copy relocs into the output file. */
2427 sec->reloc_count = 0;
2430 else
2432 const char *name;
2434 /* It's OK to base decisions on the section name, because none
2435 of the dynobj section names depend upon the input files. */
2436 name = bfd_get_section_name (dynobj, sec);
2438 if (strcmp (name, ".got.plt") == 0)
2439 strip = false;
2440 else if (strncmp (name, ".rel", 4) == 0)
2442 if (!strip)
2444 const char *outname;
2445 asection *target;
2447 /* If this relocation section applies to a read only
2448 section, then we probably need a DT_TEXTREL entry. */
2449 outname = bfd_get_section_name (output_bfd,
2450 sec->output_section);
2451 if (outname[4] == 'a')
2452 outname += 5;
2453 else
2454 outname += 4;
2456 target = bfd_get_section_by_name (output_bfd, outname);
2457 if (target != NULL
2458 && (target->flags & SEC_READONLY) != 0
2459 && (target->flags & SEC_ALLOC) != 0)
2460 reltext = true;
2462 /* We use the reloc_count field as a counter if we need to
2463 copy relocs into the output file. */
2464 sec->reloc_count = 0;
2467 else
2468 continue;
2471 if (strip)
2472 _bfd_strip_section_from_output (info, sec);
2473 else
2475 /* Allocate memory for the section contents. */
2476 sec->contents = (bfd_byte *) bfd_zalloc(dynobj, sec->_raw_size);
2477 if (sec->contents == NULL && sec->_raw_size != 0)
2478 return false;
2482 if (elf_hash_table (info)->dynamic_sections_created)
2484 /* Add some entries to the .dynamic section. We fill in the values
2485 later (in finish_dynamic_sections) but we must add the entries now
2486 so that we get the correct size for the .dynamic section. */
2488 if (!info->shared)
2490 /* The DT_DEBUG entry is filled in by the dynamic linker and used
2491 by the debugger. */
2492 if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
2493 return false;
2496 if (! bfd_elf64_add_dynamic_entry (info, DT_IA_64_PLT_RESERVE, 0))
2497 return false;
2498 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
2499 return false;
2501 if (relplt)
2503 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2504 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2505 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
2506 return false;
2509 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
2510 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
2511 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
2512 sizeof(Elf64_External_Rela)))
2513 return false;
2515 if (reltext)
2517 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
2518 return false;
2522 /* ??? Perhaps force __gp local. */
2524 return true;
2527 static bfd_reloc_status_type
2528 elf64_ia64_install_value (abfd, hit_addr, val, r_type)
2529 bfd *abfd;
2530 bfd_byte *hit_addr;
2531 bfd_vma val;
2532 unsigned int r_type;
2534 const struct ia64_operand *op;
2535 int bigendian = 0, shift = 0;
2536 bfd_vma t0, t1, insn, dword;
2537 enum ia64_opnd opnd;
2538 const char *err;
2539 size_t size = 8;
2541 opnd = IA64_OPND_NIL;
2542 switch (r_type)
2544 case R_IA64_NONE:
2545 case R_IA64_LDXMOV:
2546 return bfd_reloc_ok;
2548 /* Instruction relocations. */
2550 case R_IA64_IMM14: opnd = IA64_OPND_IMM14; break;
2552 case R_IA64_PCREL21F: opnd = IA64_OPND_TGT25; break;
2553 case R_IA64_PCREL21M: opnd = IA64_OPND_TGT25b; break;
2554 case R_IA64_PCREL60B: opnd = IA64_OPND_TGT64; break;
2555 case R_IA64_PCREL21B:
2556 case R_IA64_PCREL21BI:
2557 opnd = IA64_OPND_TGT25c;
2558 break;
2560 case R_IA64_IMM22:
2561 case R_IA64_GPREL22:
2562 case R_IA64_LTOFF22:
2563 case R_IA64_LTOFF22X:
2564 case R_IA64_PLTOFF22:
2565 case R_IA64_PCREL22:
2566 case R_IA64_LTOFF_FPTR22:
2567 opnd = IA64_OPND_IMM22;
2568 break;
2570 case R_IA64_IMM64:
2571 case R_IA64_GPREL64I:
2572 case R_IA64_LTOFF64I:
2573 case R_IA64_PLTOFF64I:
2574 case R_IA64_PCREL64I:
2575 case R_IA64_FPTR64I:
2576 case R_IA64_LTOFF_FPTR64I:
2577 opnd = IA64_OPND_IMMU64;
2578 break;
2580 /* Data relocations. */
2582 case R_IA64_DIR32MSB:
2583 case R_IA64_GPREL32MSB:
2584 case R_IA64_FPTR32MSB:
2585 case R_IA64_PCREL32MSB:
2586 case R_IA64_SEGREL32MSB:
2587 case R_IA64_SECREL32MSB:
2588 case R_IA64_LTV32MSB:
2589 size = 4; bigendian = 1;
2590 break;
2592 case R_IA64_DIR32LSB:
2593 case R_IA64_GPREL32LSB:
2594 case R_IA64_FPTR32LSB:
2595 case R_IA64_PCREL32LSB:
2596 case R_IA64_SEGREL32LSB:
2597 case R_IA64_SECREL32LSB:
2598 case R_IA64_LTV32LSB:
2599 size = 4; bigendian = 0;
2600 break;
2602 case R_IA64_DIR64MSB:
2603 case R_IA64_GPREL64MSB:
2604 case R_IA64_PLTOFF64MSB:
2605 case R_IA64_FPTR64MSB:
2606 case R_IA64_PCREL64MSB:
2607 case R_IA64_LTOFF_FPTR64MSB:
2608 case R_IA64_SEGREL64MSB:
2609 case R_IA64_SECREL64MSB:
2610 case R_IA64_LTV64MSB:
2611 size = 8; bigendian = 1;
2612 break;
2614 case R_IA64_DIR64LSB:
2615 case R_IA64_GPREL64LSB:
2616 case R_IA64_PLTOFF64LSB:
2617 case R_IA64_FPTR64LSB:
2618 case R_IA64_PCREL64LSB:
2619 case R_IA64_LTOFF_FPTR64LSB:
2620 case R_IA64_SEGREL64LSB:
2621 case R_IA64_SECREL64LSB:
2622 case R_IA64_LTV64LSB:
2623 size = 8; bigendian = 0;
2624 break;
2626 /* Unsupported / Dynamic relocations. */
2628 case R_IA64_REL32MSB:
2629 case R_IA64_REL32LSB:
2630 case R_IA64_REL64MSB:
2631 case R_IA64_REL64LSB:
2633 case R_IA64_IPLTMSB:
2634 case R_IA64_IPLTLSB:
2635 case R_IA64_EPLTMSB:
2636 case R_IA64_EPLTLSB:
2637 case R_IA64_COPY:
2639 case R_IA64_SEGBASE:
2641 case R_IA64_TPREL22:
2642 case R_IA64_TPREL64MSB:
2643 case R_IA64_TPREL64LSB:
2644 case R_IA64_LTOFF_TP22:
2646 default:
2647 return bfd_reloc_notsupported;
2650 switch (opnd)
2652 case IA64_OPND_IMMU64:
2653 hit_addr -= (long) hit_addr & 0x3;
2654 t0 = bfd_get_64 (abfd, hit_addr);
2655 t1 = bfd_get_64 (abfd, hit_addr + 8);
2657 /* tmpl/s: bits 0.. 5 in t0
2658 slot 0: bits 5..45 in t0
2659 slot 1: bits 46..63 in t0, bits 0..22 in t1
2660 slot 2: bits 23..63 in t1 */
2662 /* First, clear the bits that form the 64 bit constant. */
2663 t0 &= ~(0x3ffffLL << 46);
2664 t1 &= ~(0x7fffffLL
2665 | (( (0x07fLL << 13) | (0x1ffLL << 27)
2666 | (0x01fLL << 22) | (0x001LL << 21)
2667 | (0x001LL << 36)) << 23));
2669 t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */
2670 t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */
2671 t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */
2672 | (((val >> 7) & 0x1ff) << 27) /* imm9d */
2673 | (((val >> 16) & 0x01f) << 22) /* imm5c */
2674 | (((val >> 21) & 0x001) << 21) /* ic */
2675 | (((val >> 63) & 0x001) << 36)) << 23; /* i */
2677 bfd_put_64 (abfd, t0, hit_addr);
2678 bfd_put_64 (abfd, t1, hit_addr + 8);
2679 break;
2681 case IA64_OPND_TGT64:
2682 hit_addr -= (long) hit_addr & 0x3;
2683 t0 = bfd_get_64 (abfd, hit_addr);
2684 t1 = bfd_get_64 (abfd, hit_addr + 8);
2686 /* tmpl/s: bits 0.. 5 in t0
2687 slot 0: bits 5..45 in t0
2688 slot 1: bits 46..63 in t0, bits 0..22 in t1
2689 slot 2: bits 23..63 in t1 */
2691 /* First, clear the bits that form the 64 bit constant. */
2692 t0 &= ~(0x3ffffLL << 46);
2693 t1 &= ~(0x7fffffLL
2694 | ((1LL << 36 | 0xfffffLL << 13) << 23));
2696 val >>= 4;
2697 t0 |= ((val >> 20) & 0xffffLL) << 2 << 46; /* 16 lsbs of imm39 */
2698 t1 |= ((val >> 36) & 0x7fffffLL) << 0; /* 23 msbs of imm39 */
2699 t1 |= ((((val >> 0) & 0xfffffLL) << 13) /* imm20b */
2700 | (((val >> 59) & 0x1LL) << 36)) << 23; /* i */
2702 bfd_put_64 (abfd, t0, hit_addr);
2703 bfd_put_64 (abfd, t1, hit_addr + 8);
2704 break;
2706 default:
2707 switch ((long) hit_addr & 0x3)
2709 case 0: shift = 5; break;
2710 case 1: shift = 14; hit_addr += 3; break;
2711 case 2: shift = 23; hit_addr += 6; break;
2712 case 3: return bfd_reloc_notsupported; /* shouldn't happen... */
2714 dword = bfd_get_64 (abfd, hit_addr);
2715 insn = (dword >> shift) & 0x1ffffffffffLL;
2717 op = elf64_ia64_operands + opnd;
2718 err = (*op->insert) (op, val, &insn);
2719 if (err)
2720 return bfd_reloc_overflow;
2722 dword &= ~(0x1ffffffffffLL << shift);
2723 dword |= (insn << shift);
2724 bfd_put_64 (abfd, dword, hit_addr);
2725 break;
2727 case IA64_OPND_NIL:
2728 /* A data relocation. */
2729 if (bigendian)
2730 if (size == 4)
2731 bfd_putb32 (val, hit_addr);
2732 else
2733 bfd_putb64 (val, hit_addr);
2734 else
2735 if (size == 4)
2736 bfd_putl32 (val, hit_addr);
2737 else
2738 bfd_putl64 (val, hit_addr);
2739 break;
2742 return bfd_reloc_ok;
2745 static void
2746 elf64_ia64_install_dyn_reloc (abfd, info, sec, srel, offset, type,
2747 dynindx, addend)
2748 bfd *abfd;
2749 struct bfd_link_info *info;
2750 asection *sec;
2751 asection *srel;
2752 bfd_vma offset;
2753 unsigned int type;
2754 long dynindx;
2755 bfd_vma addend;
2757 Elf_Internal_Rela outrel;
2759 outrel.r_offset = (sec->output_section->vma
2760 + sec->output_offset
2761 + offset);
2763 BFD_ASSERT (dynindx != -1);
2764 outrel.r_info = ELF64_R_INFO (dynindx, type);
2765 outrel.r_addend = addend;
2767 if (elf_section_data (sec)->stab_info != NULL)
2769 /* This may be NULL for linker-generated relocations, as it is
2770 inconvenient to pass all the bits around. And this shouldn't
2771 happen. */
2772 BFD_ASSERT (info != NULL);
2774 offset = (_bfd_stab_section_offset
2775 (abfd, &elf_hash_table (info)->stab_info, sec,
2776 &elf_section_data (sec)->stab_info, offset));
2777 if (offset == (bfd_vma) -1)
2779 /* Run for the hills. We shouldn't be outputting a relocation
2780 for this. So do what everyone else does and output a no-op. */
2781 outrel.r_info = ELF64_R_INFO (0, R_IA64_NONE);
2782 outrel.r_addend = 0;
2783 offset = 0;
2785 outrel.r_offset = offset;
2788 bfd_elf64_swap_reloca_out (abfd, &outrel,
2789 ((Elf64_External_Rela *) srel->contents
2790 + srel->reloc_count++));
2791 BFD_ASSERT (sizeof(Elf64_External_Rela) * srel->reloc_count
2792 <= srel->_cooked_size);
2795 /* Store an entry for target address TARGET_ADDR in the linkage table
2796 and return the gp-relative address of the linkage table entry. */
2798 static bfd_vma
2799 set_got_entry (abfd, info, dyn_i, dynindx, addend, value, dyn_r_type)
2800 bfd *abfd;
2801 struct bfd_link_info *info;
2802 struct elf64_ia64_dyn_sym_info *dyn_i;
2803 long dynindx;
2804 bfd_vma addend;
2805 bfd_vma value;
2806 unsigned int dyn_r_type;
2808 struct elf64_ia64_link_hash_table *ia64_info;
2809 asection *got_sec;
2811 ia64_info = elf64_ia64_hash_table (info);
2812 got_sec = ia64_info->got_sec;
2814 BFD_ASSERT ((dyn_i->got_offset & 7) == 0);
2816 if (! dyn_i->got_done)
2818 dyn_i->got_done = true;
2820 /* Store the target address in the linkage table entry. */
2821 bfd_put_64 (abfd, value, got_sec->contents + dyn_i->got_offset);
2823 /* Install a dynamic relocation if needed. */
2824 if (info->shared
2825 || elf64_ia64_dynamic_symbol_p (dyn_i->h, info)
2826 || (dynindx != -1 && dyn_r_type == R_IA64_FPTR64LSB))
2828 if (dynindx == -1)
2830 dyn_r_type = R_IA64_REL64LSB;
2831 dynindx = 0;
2832 addend = value;
2835 if (bfd_big_endian (abfd))
2837 switch (dyn_r_type)
2839 case R_IA64_REL64LSB:
2840 dyn_r_type = R_IA64_REL64MSB;
2841 break;
2842 case R_IA64_DIR64LSB:
2843 dyn_r_type = R_IA64_DIR64MSB;
2844 break;
2845 case R_IA64_FPTR64LSB:
2846 dyn_r_type = R_IA64_FPTR64MSB;
2847 break;
2848 default:
2849 BFD_ASSERT (false);
2850 break;
2854 elf64_ia64_install_dyn_reloc (abfd, NULL, got_sec,
2855 ia64_info->rel_got_sec,
2856 dyn_i->got_offset, dyn_r_type,
2857 dynindx, addend);
2861 /* Return the address of the linkage table entry. */
2862 value = (got_sec->output_section->vma
2863 + got_sec->output_offset
2864 + dyn_i->got_offset);
2866 return value;
2869 /* Fill in a function descriptor consisting of the function's code
2870 address and its global pointer. Return the descriptor's address. */
2872 static bfd_vma
2873 set_fptr_entry (abfd, info, dyn_i, value)
2874 bfd *abfd;
2875 struct bfd_link_info *info;
2876 struct elf64_ia64_dyn_sym_info *dyn_i;
2877 bfd_vma value;
2879 struct elf64_ia64_link_hash_table *ia64_info;
2880 asection *fptr_sec;
2882 ia64_info = elf64_ia64_hash_table (info);
2883 fptr_sec = ia64_info->fptr_sec;
2885 if (!dyn_i->fptr_done)
2887 dyn_i->fptr_done = 1;
2889 /* Fill in the function descriptor. */
2890 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
2891 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
2892 fptr_sec->contents + dyn_i->fptr_offset + 8);
2895 /* Return the descriptor's address. */
2896 value = (fptr_sec->output_section->vma
2897 + fptr_sec->output_offset
2898 + dyn_i->fptr_offset);
2900 return value;
2903 /* Fill in a PLTOFF entry consisting of the function's code address
2904 and its global pointer. Return the descriptor's address. */
2906 static bfd_vma
2907 set_pltoff_entry (abfd, info, dyn_i, value, is_plt)
2908 bfd *abfd;
2909 struct bfd_link_info *info;
2910 struct elf64_ia64_dyn_sym_info *dyn_i;
2911 bfd_vma value;
2912 boolean is_plt;
2914 struct elf64_ia64_link_hash_table *ia64_info;
2915 asection *pltoff_sec;
2917 ia64_info = elf64_ia64_hash_table (info);
2918 pltoff_sec = ia64_info->pltoff_sec;
2920 /* Don't do anything if this symbol uses a real PLT entry. In
2921 that case, we'll fill this in during finish_dynamic_symbol. */
2922 if ((! dyn_i->want_plt || is_plt)
2923 && !dyn_i->pltoff_done)
2925 /* Fill in the function descriptor. */
2926 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
2927 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
2928 pltoff_sec->contents + dyn_i->pltoff_offset + 8);
2930 /* Install dynamic relocations if needed. */
2931 if (!is_plt && info->shared)
2933 unsigned int dyn_r_type;
2935 if (bfd_big_endian (abfd))
2936 dyn_r_type = R_IA64_REL64MSB;
2937 else
2938 dyn_r_type = R_IA64_REL64LSB;
2940 elf64_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
2941 ia64_info->rel_pltoff_sec,
2942 dyn_i->pltoff_offset,
2943 dyn_r_type, 0, 0);
2944 elf64_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
2945 ia64_info->rel_pltoff_sec,
2946 dyn_i->pltoff_offset + 8,
2947 dyn_r_type, 0, 0);
2950 dyn_i->pltoff_done = 1;
2953 /* Return the descriptor's address. */
2954 value = (pltoff_sec->output_section->vma
2955 + pltoff_sec->output_offset
2956 + dyn_i->pltoff_offset);
2958 return value;
2961 static boolean
2962 elf64_ia64_final_link (abfd, info)
2963 bfd *abfd;
2964 struct bfd_link_info *info;
2966 struct elf64_ia64_link_hash_table *ia64_info;
2967 ia64_info = elf64_ia64_hash_table (info);
2969 /* Make sure we've got ourselves a nice fat __gp value. */
2970 if (!info->relocateable)
2972 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
2973 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
2974 struct elf_link_hash_entry *gp;
2975 bfd_vma gp_val;
2976 asection *os;
2978 /* Find the min and max vma of all sections marked short. Also
2979 collect min and max vma of any type, for use in selecting a
2980 nice gp. */
2981 for (os = abfd->sections; os ; os = os->next)
2983 bfd_vma lo, hi;
2985 if ((os->flags & SEC_ALLOC) == 0)
2986 continue;
2988 lo = os->vma;
2989 hi = os->vma + os->_raw_size;
2990 if (hi < lo)
2991 hi = (bfd_vma) -1;
2993 if (min_vma > lo)
2994 min_vma = lo;
2995 if (max_vma < hi)
2996 max_vma = hi;
2997 if (os->flags & SEC_SMALL_DATA)
2999 if (min_short_vma > lo)
3000 min_short_vma = lo;
3001 if (max_short_vma < hi)
3002 max_short_vma = hi;
3006 /* See if the user wants to force a value. */
3007 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3008 false, false);
3010 if (gp
3011 && (gp->root.type == bfd_link_hash_defined
3012 || gp->root.type == bfd_link_hash_defweak))
3014 asection *gp_sec = gp->root.u.def.section;
3015 gp_val = (gp->root.u.def.value
3016 + gp_sec->output_section->vma
3017 + gp_sec->output_offset);
3019 else
3021 /* Pick a sensible value. */
3023 asection *got_sec = ia64_info->got_sec;
3025 /* Start with just the address of the .got. */
3026 if (got_sec)
3027 gp_val = got_sec->output_section->vma;
3028 else if (max_short_vma != 0)
3029 gp_val = min_short_vma;
3030 else
3031 gp_val = min_vma;
3033 /* If it is possible to address the entire image, but we
3034 don't with the choice above, adjust. */
3035 if (max_vma - min_vma < 0x400000
3036 && max_vma - gp_val <= 0x200000
3037 && gp_val - min_vma > 0x200000)
3038 gp_val = min_vma + 0x200000;
3039 else if (max_short_vma != 0)
3041 /* If we don't cover all the short data, adjust. */
3042 if (max_short_vma - gp_val >= 0x200000)
3043 gp_val = min_short_vma + 0x200000;
3045 /* If we're addressing stuff past the end, adjust back. */
3046 if (gp_val > max_vma)
3047 gp_val = max_vma - 0x200000 + 8;
3051 /* Validate whether all SHF_IA_64_SHORT sections are within
3052 range of the chosen GP. */
3054 if (max_short_vma != 0)
3056 if (max_short_vma - min_short_vma >= 0x400000)
3058 (*_bfd_error_handler)
3059 (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
3060 bfd_get_filename (abfd),
3061 (unsigned long)(max_short_vma - min_short_vma));
3062 return false;
3064 else if ((gp_val > min_short_vma
3065 && gp_val - min_short_vma > 0x200000)
3066 || (gp_val < max_short_vma
3067 && max_short_vma - gp_val >= 0x200000))
3069 (*_bfd_error_handler)
3070 (_("%s: __gp does not cover short data segment"),
3071 bfd_get_filename (abfd));
3072 return false;
3076 _bfd_set_gp_value (abfd, gp_val);
3079 /* Tricky bits. DT_INIT and DT_FINI use a pltoff entry, which is
3080 normally initialized in finish_dynamic_sections. Except that
3081 we need all non-plt pltoff entries to be initialized before
3082 finish_dynamic_symbols. This because the array of relocations
3083 used for plt entries (aka DT_JMPREL) begins after all the
3084 non-plt pltoff relocations. If the order gets confused, we
3085 munge either the array or the array base. */
3086 if (ia64_info->root.dynamic_sections_created)
3088 struct elf_link_hash_entry *h;
3089 struct elf64_ia64_dyn_sym_info *dyn_i;
3090 bfd_vma addr;
3092 if (info->init_function
3093 && (h = elf_link_hash_lookup (elf_hash_table (info),
3094 info->init_function, false,
3095 false, false))
3096 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3097 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3099 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3100 addr = (h->root.u.def.section->output_section->vma
3101 + h->root.u.def.section->output_offset
3102 + h->root.u.def.value);
3103 (void) set_pltoff_entry (abfd, info, dyn_i, addr, false);
3106 if (info->fini_function
3107 && (h = elf_link_hash_lookup (elf_hash_table (info),
3108 info->fini_function, false,
3109 false, false))
3110 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3111 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3113 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3114 addr = (h->root.u.def.section->output_section->vma
3115 + h->root.u.def.section->output_offset
3116 + h->root.u.def.value);
3117 (void) set_pltoff_entry (abfd, info, dyn_i, addr, false);
3121 /* Invoke the regular ELF backend linker to do all the work. */
3122 return bfd_elf64_bfd_final_link (abfd, info);
3125 static boolean
3126 elf64_ia64_relocate_section (output_bfd, info, input_bfd, input_section,
3127 contents, relocs, local_syms, local_sections)
3128 bfd *output_bfd;
3129 struct bfd_link_info *info;
3130 bfd *input_bfd;
3131 asection *input_section;
3132 bfd_byte *contents;
3133 Elf_Internal_Rela *relocs;
3134 Elf_Internal_Sym *local_syms;
3135 asection **local_sections;
3137 struct elf64_ia64_link_hash_table *ia64_info;
3138 Elf_Internal_Shdr *symtab_hdr;
3139 Elf_Internal_Rela *rel;
3140 Elf_Internal_Rela *relend;
3141 asection *srel;
3142 boolean ret_val = true; /* for non-fatal errors */
3143 bfd_vma gp_val;
3145 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3146 ia64_info = elf64_ia64_hash_table (info);
3148 /* Infect various flags from the input section to the output section. */
3149 if (info->relocateable)
3151 bfd_vma flags;
3153 flags = elf_section_data(input_section)->this_hdr.sh_flags;
3154 flags &= SHF_IA_64_NORECOV;
3156 elf_section_data(input_section->output_section)
3157 ->this_hdr.sh_flags |= flags;
3160 gp_val = _bfd_get_gp_value (output_bfd);
3161 srel = get_reloc_section (input_bfd, ia64_info, input_section, false);
3163 rel = relocs;
3164 relend = relocs + input_section->reloc_count;
3165 for (; rel < relend; ++rel)
3167 struct elf_link_hash_entry *h;
3168 struct elf64_ia64_dyn_sym_info *dyn_i;
3169 bfd_reloc_status_type r;
3170 reloc_howto_type *howto;
3171 unsigned long r_symndx;
3172 Elf_Internal_Sym *sym;
3173 unsigned int r_type;
3174 bfd_vma value;
3175 asection *sym_sec;
3176 bfd_byte *hit_addr;
3177 boolean dynamic_symbol_p;
3178 boolean undef_weak_ref;
3180 r_type = ELF64_R_TYPE (rel->r_info);
3181 if (r_type > R_IA64_MAX_RELOC_CODE)
3183 (*_bfd_error_handler)
3184 (_("%s: unknown relocation type %d"),
3185 bfd_get_filename (input_bfd), (int)r_type);
3186 bfd_set_error (bfd_error_bad_value);
3187 ret_val = false;
3188 continue;
3190 howto = lookup_howto (r_type);
3191 r_symndx = ELF64_R_SYM (rel->r_info);
3193 if (info->relocateable)
3195 /* This is a relocateable link. We don't have to change
3196 anything, unless the reloc is against a section symbol,
3197 in which case we have to adjust according to where the
3198 section symbol winds up in the output section. */
3199 if (r_symndx < symtab_hdr->sh_info)
3201 sym = local_syms + r_symndx;
3202 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3204 sym_sec = local_sections[r_symndx];
3205 rel->r_addend += sym_sec->output_offset;
3208 continue;
3211 /* This is a final link. */
3213 h = NULL;
3214 sym = NULL;
3215 sym_sec = NULL;
3216 undef_weak_ref = false;
3218 if (r_symndx < symtab_hdr->sh_info)
3220 /* Reloc against local symbol. */
3221 sym = local_syms + r_symndx;
3222 sym_sec = local_sections[r_symndx];
3223 value = (sym_sec->output_section->vma
3224 + sym_sec->output_offset
3225 + sym->st_value);
3227 else
3229 long indx;
3231 /* Reloc against global symbol. */
3232 indx = r_symndx - symtab_hdr->sh_info;
3233 h = elf_sym_hashes (input_bfd)[indx];
3234 while (h->root.type == bfd_link_hash_indirect
3235 || h->root.type == bfd_link_hash_warning)
3236 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3238 value = 0;
3239 if (h->root.type == bfd_link_hash_defined
3240 || h->root.type == bfd_link_hash_defweak)
3242 sym_sec = h->root.u.def.section;
3244 /* Detect the cases that sym_sec->output_section is
3245 expected to be NULL -- all cases in which the symbol
3246 is defined in another shared module. This includes
3247 PLT relocs for which we've created a PLT entry and
3248 other relocs for which we're prepared to create
3249 dynamic relocations. */
3250 /* ??? Just accept it NULL and continue. */
3252 if (sym_sec->output_section != NULL)
3254 value = (h->root.u.def.value
3255 + sym_sec->output_section->vma
3256 + sym_sec->output_offset);
3259 else if (h->root.type == bfd_link_hash_undefweak)
3260 undef_weak_ref = true;
3261 else if (info->shared && !info->symbolic
3262 && !info->no_undefined
3263 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3265 else
3267 if (! ((*info->callbacks->undefined_symbol)
3268 (info, h->root.root.string, input_bfd,
3269 input_section, rel->r_offset,
3270 (!info->shared || info->no_undefined
3271 || ELF_ST_VISIBILITY (h->other)))))
3272 return false;
3273 ret_val = false;
3274 continue;
3278 hit_addr = contents + rel->r_offset;
3279 value += rel->r_addend;
3280 dynamic_symbol_p = elf64_ia64_dynamic_symbol_p (h, info);
3282 switch (r_type)
3284 case R_IA64_NONE:
3285 case R_IA64_LDXMOV:
3286 continue;
3288 case R_IA64_IMM14:
3289 case R_IA64_IMM22:
3290 case R_IA64_IMM64:
3291 case R_IA64_DIR32MSB:
3292 case R_IA64_DIR32LSB:
3293 case R_IA64_DIR64MSB:
3294 case R_IA64_DIR64LSB:
3295 /* Install a dynamic relocation for this reloc. */
3296 if ((dynamic_symbol_p || info->shared)
3297 && (input_section->flags & SEC_ALLOC) != 0)
3299 unsigned int dyn_r_type;
3300 long dynindx;
3302 BFD_ASSERT (srel != NULL);
3304 /* If we don't need dynamic symbol lookup, find a
3305 matching RELATIVE relocation. */
3306 dyn_r_type = r_type;
3307 if (dynamic_symbol_p)
3308 dynindx = h->dynindx;
3309 else
3311 switch (r_type)
3313 case R_IA64_DIR32MSB:
3314 dyn_r_type = R_IA64_REL32MSB;
3315 break;
3316 case R_IA64_DIR32LSB:
3317 dyn_r_type = R_IA64_REL32LSB;
3318 break;
3319 case R_IA64_DIR64MSB:
3320 dyn_r_type = R_IA64_REL64MSB;
3321 break;
3322 case R_IA64_DIR64LSB:
3323 dyn_r_type = R_IA64_REL64LSB;
3324 break;
3326 default:
3327 /* We can't represent this without a dynamic symbol.
3328 Adjust the relocation to be against an output
3329 section symbol, which are always present in the
3330 dynamic symbol table. */
3331 /* ??? People shouldn't be doing non-pic code in
3332 shared libraries. Hork. */
3333 (*_bfd_error_handler)
3334 (_("%s: linking non-pic code in a shared library"),
3335 bfd_get_filename (input_bfd));
3336 ret_val = false;
3337 continue;
3339 dynindx = 0;
3342 elf64_ia64_install_dyn_reloc (output_bfd, info, input_section,
3343 srel, rel->r_offset, dyn_r_type,
3344 dynindx, rel->r_addend);
3346 /* FALLTHRU */
3348 case R_IA64_LTV32MSB:
3349 case R_IA64_LTV32LSB:
3350 case R_IA64_LTV64MSB:
3351 case R_IA64_LTV64LSB:
3352 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3353 break;
3355 case R_IA64_GPREL22:
3356 case R_IA64_GPREL64I:
3357 case R_IA64_GPREL32MSB:
3358 case R_IA64_GPREL32LSB:
3359 case R_IA64_GPREL64MSB:
3360 case R_IA64_GPREL64LSB:
3361 if (dynamic_symbol_p)
3363 (*_bfd_error_handler)
3364 (_("%s: @gprel relocation against dynamic symbol %s"),
3365 bfd_get_filename (input_bfd), h->root.root.string);
3366 ret_val = false;
3367 continue;
3369 value -= gp_val;
3370 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3371 break;
3373 case R_IA64_LTOFF22:
3374 case R_IA64_LTOFF22X:
3375 case R_IA64_LTOFF64I:
3376 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3377 value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
3378 rel->r_addend, value, R_IA64_DIR64LSB);
3379 value -= gp_val;
3380 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3381 break;
3383 case R_IA64_PLTOFF22:
3384 case R_IA64_PLTOFF64I:
3385 case R_IA64_PLTOFF64MSB:
3386 case R_IA64_PLTOFF64LSB:
3387 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3388 value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
3389 value -= gp_val;
3390 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3391 break;
3393 case R_IA64_FPTR64I:
3394 case R_IA64_FPTR32MSB:
3395 case R_IA64_FPTR32LSB:
3396 case R_IA64_FPTR64MSB:
3397 case R_IA64_FPTR64LSB:
3398 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3399 if (dyn_i->want_fptr)
3401 if (!undef_weak_ref)
3402 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3404 else
3406 long dynindx;
3408 /* Otherwise, we expect the dynamic linker to create
3409 the entry. */
3411 if (h)
3413 if (h->dynindx != -1)
3414 dynindx = h->dynindx;
3415 else
3416 dynindx = (_bfd_elf_link_lookup_local_dynindx
3417 (info, h->root.u.def.section->owner,
3418 global_sym_index (h)));
3420 else
3422 dynindx = (_bfd_elf_link_lookup_local_dynindx
3423 (info, input_bfd, r_symndx));
3426 elf64_ia64_install_dyn_reloc (output_bfd, info, input_section,
3427 srel, rel->r_offset, r_type,
3428 dynindx, rel->r_addend);
3429 value = 0;
3432 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3433 break;
3435 case R_IA64_LTOFF_FPTR22:
3436 case R_IA64_LTOFF_FPTR64I:
3437 case R_IA64_LTOFF_FPTR64MSB:
3438 case R_IA64_LTOFF_FPTR64LSB:
3440 long dynindx;
3442 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3443 if (dyn_i->want_fptr)
3445 BFD_ASSERT (h == NULL || h->dynindx == -1)
3446 if (!undef_weak_ref)
3447 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3448 dynindx = -1;
3450 else
3452 /* Otherwise, we expect the dynamic linker to create
3453 the entry. */
3454 if (h)
3456 if (h->dynindx != -1)
3457 dynindx = h->dynindx;
3458 else
3459 dynindx = (_bfd_elf_link_lookup_local_dynindx
3460 (info, h->root.u.def.section->owner,
3461 global_sym_index (h)));
3463 else
3464 dynindx = (_bfd_elf_link_lookup_local_dynindx
3465 (info, input_bfd, r_symndx));
3466 value = 0;
3469 value = set_got_entry (output_bfd, info, dyn_i, dynindx,
3470 rel->r_addend, value, R_IA64_FPTR64LSB);
3471 value -= gp_val;
3472 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3474 break;
3476 case R_IA64_PCREL32MSB:
3477 case R_IA64_PCREL32LSB:
3478 case R_IA64_PCREL64MSB:
3479 case R_IA64_PCREL64LSB:
3480 /* Install a dynamic relocation for this reloc. */
3481 if (dynamic_symbol_p)
3483 BFD_ASSERT (srel != NULL);
3485 elf64_ia64_install_dyn_reloc (output_bfd, info, input_section,
3486 srel, rel->r_offset, r_type,
3487 h->dynindx, rel->r_addend);
3489 goto finish_pcrel;
3491 case R_IA64_PCREL21BI:
3492 case R_IA64_PCREL21F:
3493 case R_IA64_PCREL21M:
3494 /* ??? These two are only used for speculation fixup code.
3495 They should never be dynamic. */
3496 if (dynamic_symbol_p)
3498 (*_bfd_error_handler)
3499 (_("%s: dynamic relocation against speculation fixup"),
3500 bfd_get_filename (input_bfd));
3501 ret_val = false;
3502 continue;
3504 if (undef_weak_ref)
3506 (*_bfd_error_handler)
3507 (_("%s: speculation fixup against undefined weak symbol"),
3508 bfd_get_filename (input_bfd));
3509 ret_val = false;
3510 continue;
3512 goto finish_pcrel;
3514 case R_IA64_PCREL21B:
3515 case R_IA64_PCREL60B:
3516 /* We should have created a PLT entry for any dynamic symbol. */
3517 dyn_i = NULL;
3518 if (h)
3519 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3521 if (dyn_i && dyn_i->want_plt2)
3523 /* Should have caught this earlier. */
3524 BFD_ASSERT (rel->r_addend == 0);
3526 value = (ia64_info->plt_sec->output_section->vma
3527 + ia64_info->plt_sec->output_offset
3528 + dyn_i->plt2_offset);
3530 else
3532 /* Since there's no PLT entry, Validate that this is
3533 locally defined. */
3534 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
3536 /* If the symbol is undef_weak, we shouldn't be trying
3537 to call it. There's every chance that we'd wind up
3538 with an out-of-range fixup here. Don't bother setting
3539 any value at all. */
3540 if (undef_weak_ref)
3541 continue;
3543 goto finish_pcrel;
3545 case R_IA64_PCREL22:
3546 case R_IA64_PCREL64I:
3547 finish_pcrel:
3548 /* Make pc-relative. */
3549 value -= (input_section->output_section->vma
3550 + input_section->output_offset
3551 + rel->r_offset) & ~ (bfd_vma) 0x3;
3552 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3553 break;
3555 case R_IA64_SEGREL32MSB:
3556 case R_IA64_SEGREL32LSB:
3557 case R_IA64_SEGREL64MSB:
3558 case R_IA64_SEGREL64LSB:
3560 struct elf_segment_map *m;
3561 Elf_Internal_Phdr *p;
3563 /* Find the segment that contains the output_section. */
3564 for (m = elf_tdata (output_bfd)->segment_map,
3565 p = elf_tdata (output_bfd)->phdr;
3566 m != NULL;
3567 m = m->next, p++)
3569 int i;
3570 for (i = m->count - 1; i >= 0; i--)
3571 if (m->sections[i] == sym_sec->output_section)
3572 break;
3573 if (i >= 0)
3574 break;
3577 if (m == NULL)
3579 /* If the input section was discarded from the output, then
3580 do nothing. */
3582 if (bfd_is_abs_section (sym_sec->output_section))
3583 r = bfd_reloc_ok;
3584 else
3585 r = bfd_reloc_notsupported;
3587 else
3589 /* The VMA of the segment is the vaddr of the associated
3590 program header. */
3591 if (value > p->p_vaddr)
3592 value -= p->p_vaddr;
3593 else
3594 value = 0;
3595 r = elf64_ia64_install_value (output_bfd, hit_addr, value,
3596 r_type);
3598 break;
3601 case R_IA64_SECREL32MSB:
3602 case R_IA64_SECREL32LSB:
3603 case R_IA64_SECREL64MSB:
3604 case R_IA64_SECREL64LSB:
3605 /* Make output-section relative. */
3606 if (value > input_section->output_section->vma)
3607 value -= input_section->output_section->vma;
3608 else
3609 value = 0;
3610 r = elf64_ia64_install_value (output_bfd, hit_addr, value, r_type);
3611 break;
3613 case R_IA64_SEGBASE:
3615 case R_IA64_REL32MSB:
3616 case R_IA64_REL32LSB:
3617 case R_IA64_REL64MSB:
3618 case R_IA64_REL64LSB:
3620 case R_IA64_IPLTMSB:
3621 case R_IA64_IPLTLSB:
3622 case R_IA64_EPLTMSB:
3623 case R_IA64_EPLTLSB:
3624 case R_IA64_COPY:
3626 case R_IA64_TPREL22:
3627 case R_IA64_TPREL64MSB:
3628 case R_IA64_TPREL64LSB:
3629 case R_IA64_LTOFF_TP22:
3630 default:
3631 r = bfd_reloc_notsupported;
3632 break;
3635 switch (r)
3637 case bfd_reloc_ok:
3638 break;
3640 case bfd_reloc_undefined:
3641 /* This can happen for global table relative relocs if
3642 __gp is undefined. This is a panic situation so we
3643 don't try to continue. */
3644 (*info->callbacks->undefined_symbol)
3645 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
3646 return false;
3648 case bfd_reloc_notsupported:
3650 const char *name;
3652 if (h)
3653 name = h->root.root.string;
3654 else
3656 name = bfd_elf_string_from_elf_section (input_bfd,
3657 symtab_hdr->sh_link,
3658 sym->st_name);
3659 if (name == NULL)
3660 return false;
3661 if (*name == '\0')
3662 name = bfd_section_name (input_bfd, input_section);
3664 if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
3665 name, input_bfd,
3666 input_section, rel->r_offset))
3667 return false;
3668 ret_val = false;
3670 break;
3672 case bfd_reloc_dangerous:
3673 case bfd_reloc_outofrange:
3674 case bfd_reloc_overflow:
3675 default:
3677 const char *name;
3679 if (h)
3680 name = h->root.root.string;
3681 else
3683 name = bfd_elf_string_from_elf_section (input_bfd,
3684 symtab_hdr->sh_link,
3685 sym->st_name);
3686 if (name == NULL)
3687 return false;
3688 if (*name == '\0')
3689 name = bfd_section_name (input_bfd, input_section);
3691 if (!(*info->callbacks->reloc_overflow) (info, name,
3692 howto->name, 0,
3693 input_bfd,
3694 input_section,
3695 rel->r_offset))
3696 return false;
3697 ret_val = false;
3699 break;
3703 return ret_val;
3706 static boolean
3707 elf64_ia64_finish_dynamic_symbol (output_bfd, info, h, sym)
3708 bfd *output_bfd;
3709 struct bfd_link_info *info;
3710 struct elf_link_hash_entry *h;
3711 Elf_Internal_Sym *sym;
3713 struct elf64_ia64_link_hash_table *ia64_info;
3714 struct elf64_ia64_dyn_sym_info *dyn_i;
3716 ia64_info = elf64_ia64_hash_table (info);
3717 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3719 /* Fill in the PLT data, if required. */
3720 if (dyn_i && dyn_i->want_plt)
3722 Elf_Internal_Rela outrel;
3723 bfd_byte *loc;
3724 asection *plt_sec;
3725 bfd_vma plt_addr, pltoff_addr, gp_val, index;
3726 Elf64_External_Rela *rel;
3728 gp_val = _bfd_get_gp_value (output_bfd);
3730 /* Initialize the minimal PLT entry. */
3732 index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
3733 plt_sec = ia64_info->plt_sec;
3734 loc = plt_sec->contents + dyn_i->plt_offset;
3736 memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
3737 elf64_ia64_install_value (output_bfd, loc, index, R_IA64_IMM22);
3738 elf64_ia64_install_value (output_bfd, loc+2, -dyn_i->plt_offset,
3739 R_IA64_PCREL21B);
3741 plt_addr = (plt_sec->output_section->vma
3742 + plt_sec->output_offset
3743 + dyn_i->plt_offset);
3744 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
3746 /* Initialize the FULL PLT entry, if needed. */
3747 if (dyn_i->want_plt2)
3749 loc = plt_sec->contents + dyn_i->plt2_offset;
3751 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
3752 elf64_ia64_install_value (output_bfd, loc, pltoff_addr - gp_val,
3753 R_IA64_IMM22);
3755 /* Mark the symbol as undefined, rather than as defined in the
3756 plt section. Leave the value alone. */
3757 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
3758 first place. But perhaps elflink.h did some for us. */
3759 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3760 sym->st_shndx = SHN_UNDEF;
3763 /* Create the dynamic relocation. */
3764 outrel.r_offset = pltoff_addr;
3765 if (bfd_little_endian (output_bfd))
3766 outrel.r_info = ELF64_R_INFO (h->dynindx, R_IA64_IPLTLSB);
3767 else
3768 outrel.r_info = ELF64_R_INFO (h->dynindx, R_IA64_IPLTMSB);
3769 outrel.r_addend = 0;
3771 /* This is fun. In the .IA_64.pltoff section, we've got entries
3772 that correspond both to real PLT entries, and those that
3773 happened to resolve to local symbols but need to be created
3774 to satisfy @pltoff relocations. The .rela.IA_64.pltoff
3775 relocations for the real PLT should come at the end of the
3776 section, so that they can be indexed by plt entry at runtime.
3778 We emitted all of the relocations for the non-PLT @pltoff
3779 entries during relocate_section. So we can consider the
3780 existing sec->reloc_count to be the base of the array of
3781 PLT relocations. */
3783 rel = (Elf64_External_Rela *)ia64_info->rel_pltoff_sec->contents;
3784 rel += ia64_info->rel_pltoff_sec->reloc_count;
3786 bfd_elf64_swap_reloca_out (output_bfd, &outrel, rel + index);
3789 /* Mark some specially defined symbols as absolute. */
3790 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3791 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3792 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3793 sym->st_shndx = SHN_ABS;
3795 return true;
3798 static boolean
3799 elf64_ia64_finish_dynamic_sections (abfd, info)
3800 bfd *abfd;
3801 struct bfd_link_info *info;
3803 struct elf64_ia64_link_hash_table *ia64_info;
3804 bfd *dynobj;
3806 ia64_info = elf64_ia64_hash_table (info);
3807 dynobj = ia64_info->root.dynobj;
3809 if (elf_hash_table (info)->dynamic_sections_created)
3811 Elf64_External_Dyn *dyncon, *dynconend;
3812 asection *sdyn, *sgotplt;
3813 bfd_vma gp_val;
3815 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3816 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3817 BFD_ASSERT (sdyn != NULL);
3818 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3819 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3821 gp_val = _bfd_get_gp_value (abfd);
3823 for (; dyncon < dynconend; dyncon++)
3825 Elf_Internal_Dyn dyn;
3826 const char *name;
3827 asection *s;
3829 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3831 switch (dyn.d_tag)
3833 case DT_PLTGOT:
3834 dyn.d_un.d_ptr = gp_val;
3835 break;
3837 case DT_PLTRELSZ:
3838 dyn.d_un.d_val = (ia64_info->minplt_entries
3839 * sizeof (Elf64_External_Rela));
3840 break;
3842 case DT_JMPREL:
3843 /* See the comment above in finish_dynamic_symbol. */
3844 dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
3845 + ia64_info->rel_pltoff_sec->output_offset
3846 + (ia64_info->rel_pltoff_sec->reloc_count
3847 * sizeof (Elf64_External_Rela)));
3848 break;
3850 case DT_IA_64_PLT_RESERVE:
3851 dyn.d_un.d_ptr = (sgotplt->output_section->vma
3852 + sgotplt->output_offset);
3853 break;
3855 case DT_RELASZ:
3856 /* Do not have RELASZ include JMPREL. This makes things
3857 easier on ld.so. This is not what the rest of BFD set up. */
3858 dyn.d_un.d_val -= (ia64_info->minplt_entries
3859 * sizeof (Elf64_External_Rela));
3860 break;
3862 case DT_INIT:
3863 case DT_FINI:
3865 struct elf_link_hash_entry *h;
3866 struct elf64_ia64_dyn_sym_info *dyn_i;
3867 const char *which;
3869 if (dyn.d_tag == DT_INIT)
3870 which = info->init_function;
3871 else
3872 which = info->fini_function;
3874 h = elf_link_hash_lookup (elf_hash_table (info), which,
3875 false, false, false);
3876 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3877 dyn.d_un.d_ptr = set_pltoff_entry (abfd, info, dyn_i,
3878 dyn.d_un.d_ptr, 0);
3882 bfd_elf64_swap_dyn_out (abfd, &dyn, dyncon);
3885 /* Initialize the PLT0 entry */
3886 if (ia64_info->plt_sec)
3888 bfd_byte *loc = ia64_info->plt_sec->contents;
3889 bfd_vma pltres;
3891 memcpy (loc, plt_header, PLT_HEADER_SIZE);
3893 pltres = (sgotplt->output_section->vma
3894 + sgotplt->output_offset
3895 - gp_val);
3897 elf64_ia64_install_value (abfd, loc+1, pltres, R_IA64_GPREL22);
3901 return true;
3904 /* ELF file flag handling: */
3906 /* Function to keep IA-64 specific file flags. */
3907 static boolean
3908 elf64_ia64_set_private_flags (abfd, flags)
3909 bfd *abfd;
3910 flagword flags;
3912 BFD_ASSERT (!elf_flags_init (abfd)
3913 || elf_elfheader (abfd)->e_flags == flags);
3915 elf_elfheader (abfd)->e_flags = flags;
3916 elf_flags_init (abfd) = true;
3917 return true;
3920 /* Copy backend specific data from one object module to another */
3921 static boolean
3922 elf64_ia64_copy_private_bfd_data (ibfd, obfd)
3923 bfd *ibfd, *obfd;
3925 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3926 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3927 return true;
3929 BFD_ASSERT (!elf_flags_init (obfd)
3930 || (elf_elfheader (obfd)->e_flags
3931 == elf_elfheader (ibfd)->e_flags));
3933 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
3934 elf_flags_init (obfd) = true;
3935 return true;
3938 /* Merge backend specific data from an object file to the output
3939 object file when linking. */
3940 static boolean
3941 elf64_ia64_merge_private_bfd_data (ibfd, obfd)
3942 bfd *ibfd, *obfd;
3944 flagword out_flags;
3945 flagword in_flags;
3946 boolean ok = true;
3948 /* Don't even pretend to support mixed-format linking. */
3949 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3950 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3951 return false;
3953 in_flags = elf_elfheader (ibfd)->e_flags;
3954 out_flags = elf_elfheader (obfd)->e_flags;
3956 if (! elf_flags_init (obfd))
3958 elf_flags_init (obfd) = true;
3959 elf_elfheader (obfd)->e_flags = in_flags;
3961 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3962 && bfd_get_arch_info (obfd)->the_default)
3964 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3965 bfd_get_mach (ibfd));
3968 return true;
3971 /* Check flag compatibility. */
3972 if (in_flags == out_flags)
3973 return true;
3975 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
3977 (*_bfd_error_handler)
3978 (_("%s: linking trap-on-NULL-dereference with non-trapping files"),
3979 bfd_get_filename (ibfd));
3981 bfd_set_error (bfd_error_bad_value);
3982 ok = false;
3984 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
3986 (*_bfd_error_handler)
3987 (_("%s: linking big-endian files with little-endian files"),
3988 bfd_get_filename (ibfd));
3990 bfd_set_error (bfd_error_bad_value);
3991 ok = false;
3993 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
3995 (*_bfd_error_handler)
3996 (_("%s: linking 64-bit files with 32-bit files"),
3997 bfd_get_filename (ibfd));
3999 bfd_set_error (bfd_error_bad_value);
4000 ok = false;
4003 return ok;
4006 static boolean
4007 elf64_ia64_print_private_bfd_data (abfd, ptr)
4008 bfd *abfd;
4009 PTR ptr;
4011 FILE *file = (FILE *) ptr;
4012 flagword flags = elf_elfheader (abfd)->e_flags;
4014 BFD_ASSERT (abfd != NULL && ptr != NULL);
4016 fprintf (file, "private flags = %s%s%s%s\n",
4017 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4018 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4019 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4020 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4021 _bfd_elf_print_private_bfd_data (abfd, ptr);
4022 return true;
4025 #define TARGET_LITTLE_SYM bfd_elf64_ia64_little_vec
4026 #define TARGET_LITTLE_NAME "elf64-ia64-little"
4027 #define TARGET_BIG_SYM bfd_elf64_ia64_big_vec
4028 #define TARGET_BIG_NAME "elf64-ia64-big"
4029 #define ELF_ARCH bfd_arch_ia64
4030 #define ELF_MACHINE_CODE EM_IA_64
4031 #define ELF_MACHINE_ALT1 1999 /* EAS2.3 */
4032 #define ELF_MACHINE_ALT2 1998 /* EAS2.2 */
4033 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
4035 #define elf_backend_section_from_shdr \
4036 elf64_ia64_section_from_shdr
4037 #define elf_backend_fake_sections \
4038 elf64_ia64_fake_sections
4039 #define elf_backend_add_symbol_hook \
4040 elf64_ia64_add_symbol_hook
4041 #define elf_backend_additional_program_headers \
4042 elf64_ia64_additional_program_headers
4043 #define elf_backend_modify_segment_map \
4044 elf64_ia64_modify_segment_map
4045 #define elf_info_to_howto \
4046 elf64_ia64_info_to_howto
4048 #define bfd_elf64_bfd_reloc_type_lookup \
4049 elf64_ia64_reloc_type_lookup
4050 #define bfd_elf64_bfd_is_local_label_name \
4051 elf64_ia64_is_local_label_name
4052 #define bfd_elf64_bfd_relax_section \
4053 elf64_ia64_relax_section
4055 /* Stuff for the BFD linker: */
4056 #define bfd_elf64_bfd_link_hash_table_create \
4057 elf64_ia64_hash_table_create
4058 #define elf_backend_create_dynamic_sections \
4059 elf64_ia64_create_dynamic_sections
4060 #define elf_backend_check_relocs \
4061 elf64_ia64_check_relocs
4062 #define elf_backend_adjust_dynamic_symbol \
4063 elf64_ia64_adjust_dynamic_symbol
4064 #define elf_backend_size_dynamic_sections \
4065 elf64_ia64_size_dynamic_sections
4066 #define elf_backend_relocate_section \
4067 elf64_ia64_relocate_section
4068 #define elf_backend_finish_dynamic_symbol \
4069 elf64_ia64_finish_dynamic_symbol
4070 #define elf_backend_finish_dynamic_sections \
4071 elf64_ia64_finish_dynamic_sections
4072 #define bfd_elf64_bfd_final_link \
4073 elf64_ia64_final_link
4075 #define bfd_elf64_bfd_copy_private_bfd_data \
4076 elf64_ia64_copy_private_bfd_data
4077 #define bfd_elf64_bfd_merge_private_bfd_data \
4078 elf64_ia64_merge_private_bfd_data
4079 #define bfd_elf64_bfd_set_private_flags \
4080 elf64_ia64_set_private_flags
4081 #define bfd_elf64_bfd_print_private_bfd_data \
4082 elf64_ia64_print_private_bfd_data
4084 #define elf_backend_plt_readonly 1
4085 #define elf_backend_want_plt_sym 0
4086 #define elf_backend_plt_alignment 5
4087 #define elf_backend_got_header_size 0
4088 #define elf_backend_plt_header_size PLT_HEADER_SIZE
4089 #define elf_backend_want_got_plt 1
4090 #define elf_backend_may_use_rel_p 1
4091 #define elf_backend_may_use_rela_p 1
4092 #define elf_backend_default_use_rela_p 1
4093 #define elf_backend_want_dynbss 0
4094 #define elf_backend_copy_indirect_symbol elf64_ia64_hash_copy_indirect
4095 #define elf_backend_hide_symbol elf64_ia64_hash_hide_symbol
4097 #include "elf64-target.h"