* tc-mips.c (RELAX_BRANCH_ENCODE): Remove reloc_s2 argument.
[binutils.git] / bfd / elfxx-ia64.c
blobaa4c5ad8136792608d2d16bf72eb0fdebf1fb2bf
1 /* IA-64 support for 64-bit ELF
2 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
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"
28 /* THE RULES for all the stuff the linker creates --
30 GOT Entries created in response to LTOFF or LTOFF_FPTR
31 relocations. Dynamic relocs created for dynamic
32 symbols in an application; REL relocs for locals
33 in a shared library.
35 FPTR The canonical function descriptor. Created for local
36 symbols in applications. Descriptors for dynamic symbols
37 and local symbols in shared libraries are created by
38 ld.so. Thus there are no dynamic relocs against these
39 objects. The FPTR relocs for such _are_ passed through
40 to the dynamic relocation tables.
42 FULL_PLT Created for a PCREL21B relocation against a dynamic symbol.
43 Requires the creation of a PLTOFF entry. This does not
44 require any dynamic relocations.
46 PLTOFF Created by PLTOFF relocations. For local symbols, this
47 is an alternate function descriptor, and in shared libraries
48 requires two REL relocations. Note that this cannot be
49 transformed into an FPTR relocation, since it must be in
50 range of the GP. For dynamic symbols, this is a function
51 descriptor for a MIN_PLT entry, and requires one IPLT reloc.
53 MIN_PLT Created by PLTOFF entries against dynamic symbols. This
54 does not reqire dynamic relocations. */
56 #define NELEMS(a) ((int) (sizeof (a) / sizeof ((a)[0])))
58 typedef struct bfd_hash_entry *(*new_hash_entry_func)
59 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
61 /* In dynamically (linker-) created sections, we generally need to keep track
62 of the place a symbol or expression got allocated to. This is done via hash
63 tables that store entries of the following type. */
65 struct elfNN_ia64_dyn_sym_info
67 /* The addend for which this entry is relevant. */
68 bfd_vma addend;
70 /* Next addend in the list. */
71 struct elfNN_ia64_dyn_sym_info *next;
73 bfd_vma got_offset;
74 bfd_vma fptr_offset;
75 bfd_vma pltoff_offset;
76 bfd_vma plt_offset;
77 bfd_vma plt2_offset;
78 bfd_vma tprel_offset;
79 bfd_vma dtpmod_offset;
80 bfd_vma dtprel_offset;
82 /* The symbol table entry, if any, that this was derrived from. */
83 struct elf_link_hash_entry *h;
85 /* Used to count non-got, non-plt relocations for delayed sizing
86 of relocation sections. */
87 struct elfNN_ia64_dyn_reloc_entry
89 struct elfNN_ia64_dyn_reloc_entry *next;
90 asection *srel;
91 int type;
92 int count;
93 } *reloc_entries;
95 /* TRUE when the section contents have been updated. */
96 unsigned got_done : 1;
97 unsigned fptr_done : 1;
98 unsigned pltoff_done : 1;
99 unsigned tprel_done : 1;
100 unsigned dtpmod_done : 1;
101 unsigned dtprel_done : 1;
103 /* TRUE for the different kinds of linker data we want created. */
104 unsigned want_got : 1;
105 unsigned want_fptr : 1;
106 unsigned want_ltoff_fptr : 1;
107 unsigned want_plt : 1;
108 unsigned want_plt2 : 1;
109 unsigned want_pltoff : 1;
110 unsigned want_tprel : 1;
111 unsigned want_dtpmod : 1;
112 unsigned want_dtprel : 1;
115 struct elfNN_ia64_local_hash_entry
117 struct bfd_hash_entry root;
118 struct elfNN_ia64_dyn_sym_info *info;
120 /* TRUE if this hash entry's addends was translated for
121 SHF_MERGE optimization. */
122 unsigned sec_merge_done : 1;
125 struct elfNN_ia64_local_hash_table
127 struct bfd_hash_table root;
128 /* No additional fields for now. */
131 struct elfNN_ia64_link_hash_entry
133 struct elf_link_hash_entry root;
134 struct elfNN_ia64_dyn_sym_info *info;
137 struct elfNN_ia64_link_hash_table
139 /* The main hash table. */
140 struct elf_link_hash_table root;
142 asection *got_sec; /* the linkage table section (or NULL) */
143 asection *rel_got_sec; /* dynamic relocation section for same */
144 asection *fptr_sec; /* function descriptor table (or NULL) */
145 asection *plt_sec; /* the primary plt section (or NULL) */
146 asection *pltoff_sec; /* private descriptors for plt (or NULL) */
147 asection *rel_pltoff_sec; /* dynamic relocation section for same */
149 bfd_size_type minplt_entries; /* number of minplt entries */
150 unsigned reltext : 1; /* are there relocs against readonly sections? */
152 struct elfNN_ia64_local_hash_table loc_hash_table;
155 #define elfNN_ia64_hash_table(p) \
156 ((struct elfNN_ia64_link_hash_table *) ((p)->hash))
158 static bfd_reloc_status_type elfNN_ia64_reloc
159 PARAMS ((bfd *abfd, arelent *reloc, asymbol *sym, PTR data,
160 asection *input_section, bfd *output_bfd, char **error_message));
161 static reloc_howto_type * lookup_howto
162 PARAMS ((unsigned int rtype));
163 static reloc_howto_type *elfNN_ia64_reloc_type_lookup
164 PARAMS ((bfd *abfd, bfd_reloc_code_real_type bfd_code));
165 static void elfNN_ia64_info_to_howto
166 PARAMS ((bfd *abfd, arelent *bfd_reloc, Elf_Internal_Rela *elf_reloc));
167 static bfd_boolean elfNN_ia64_relax_section
168 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
169 bfd_boolean *again));
170 static bfd_boolean is_unwind_section_name
171 PARAMS ((bfd *abfd, const char *));
172 static bfd_boolean elfNN_ia64_section_from_shdr
173 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
174 static bfd_boolean elfNN_ia64_section_flags
175 PARAMS ((flagword *, Elf_Internal_Shdr *));
176 static bfd_boolean elfNN_ia64_fake_sections
177 PARAMS ((bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec));
178 static void elfNN_ia64_final_write_processing
179 PARAMS ((bfd *abfd, bfd_boolean linker));
180 static bfd_boolean elfNN_ia64_add_symbol_hook
181 PARAMS ((bfd *abfd, struct bfd_link_info *info, const Elf_Internal_Sym *sym,
182 const char **namep, flagword *flagsp, asection **secp,
183 bfd_vma *valp));
184 static bfd_boolean elfNN_ia64_aix_vec
185 PARAMS ((const bfd_target *vec));
186 static bfd_boolean elfNN_ia64_aix_add_symbol_hook
187 PARAMS ((bfd *abfd, struct bfd_link_info *info, const Elf_Internal_Sym *sym,
188 const char **namep, flagword *flagsp, asection **secp,
189 bfd_vma *valp));
190 static bfd_boolean elfNN_ia64_aix_link_add_symbols
191 PARAMS ((bfd *abfd, struct bfd_link_info *info));
192 static int elfNN_ia64_additional_program_headers
193 PARAMS ((bfd *abfd));
194 static bfd_boolean elfNN_ia64_modify_segment_map
195 PARAMS ((bfd *));
196 static bfd_boolean elfNN_ia64_is_local_label_name
197 PARAMS ((bfd *abfd, const char *name));
198 static bfd_boolean elfNN_ia64_dynamic_symbol_p
199 PARAMS ((struct elf_link_hash_entry *h, struct bfd_link_info *info));
200 static bfd_boolean elfNN_ia64_local_hash_table_init
201 PARAMS ((struct elfNN_ia64_local_hash_table *ht, bfd *abfd,
202 new_hash_entry_func new));
203 static struct bfd_hash_entry *elfNN_ia64_new_loc_hash_entry
204 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
205 const char *string));
206 static struct bfd_hash_entry *elfNN_ia64_new_elf_hash_entry
207 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
208 const char *string));
209 static void elfNN_ia64_hash_copy_indirect
210 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
211 struct elf_link_hash_entry *));
212 static void elfNN_ia64_hash_hide_symbol
213 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
214 static struct bfd_link_hash_table *elfNN_ia64_hash_table_create
215 PARAMS ((bfd *abfd));
216 static struct elfNN_ia64_local_hash_entry *elfNN_ia64_local_hash_lookup
217 PARAMS ((struct elfNN_ia64_local_hash_table *table, const char *string,
218 bfd_boolean create, bfd_boolean copy));
219 static bfd_boolean elfNN_ia64_global_dyn_sym_thunk
220 PARAMS ((struct bfd_hash_entry *, PTR));
221 static bfd_boolean elfNN_ia64_local_dyn_sym_thunk
222 PARAMS ((struct bfd_hash_entry *, PTR));
223 static void elfNN_ia64_dyn_sym_traverse
224 PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
225 bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR),
226 PTR info));
227 static bfd_boolean elfNN_ia64_create_dynamic_sections
228 PARAMS ((bfd *abfd, struct bfd_link_info *info));
229 static struct elfNN_ia64_local_hash_entry * get_local_sym_hash
230 PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
231 bfd *abfd, const Elf_Internal_Rela *rel, bfd_boolean create));
232 static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
233 PARAMS ((struct elfNN_ia64_link_hash_table *ia64_info,
234 struct elf_link_hash_entry *h,
235 bfd *abfd, const Elf_Internal_Rela *rel, bfd_boolean create));
236 static asection *get_got
237 PARAMS ((bfd *abfd, struct bfd_link_info *info,
238 struct elfNN_ia64_link_hash_table *ia64_info));
239 static asection *get_fptr
240 PARAMS ((bfd *abfd, struct bfd_link_info *info,
241 struct elfNN_ia64_link_hash_table *ia64_info));
242 static asection *get_pltoff
243 PARAMS ((bfd *abfd, struct bfd_link_info *info,
244 struct elfNN_ia64_link_hash_table *ia64_info));
245 static asection *get_reloc_section
246 PARAMS ((bfd *abfd, struct elfNN_ia64_link_hash_table *ia64_info,
247 asection *sec, bfd_boolean create));
248 static bfd_boolean count_dyn_reloc
249 PARAMS ((bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
250 asection *srel, int type));
251 static bfd_boolean elfNN_ia64_check_relocs
252 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
253 const Elf_Internal_Rela *relocs));
254 static bfd_boolean elfNN_ia64_adjust_dynamic_symbol
255 PARAMS ((struct bfd_link_info *info, struct elf_link_hash_entry *h));
256 static long global_sym_index
257 PARAMS ((struct elf_link_hash_entry *h));
258 static bfd_boolean allocate_fptr
259 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
260 static bfd_boolean allocate_global_data_got
261 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
262 static bfd_boolean allocate_global_fptr_got
263 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
264 static bfd_boolean allocate_local_got
265 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
266 static bfd_boolean allocate_pltoff_entries
267 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
268 static bfd_boolean allocate_plt_entries
269 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
270 static bfd_boolean allocate_plt2_entries
271 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
272 static bfd_boolean allocate_dynrel_entries
273 PARAMS ((struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data));
274 static bfd_boolean elfNN_ia64_size_dynamic_sections
275 PARAMS ((bfd *output_bfd, struct bfd_link_info *info));
276 static bfd_reloc_status_type elfNN_ia64_install_value
277 PARAMS ((bfd *abfd, bfd_byte *hit_addr, bfd_vma val, unsigned int r_type));
278 static void elfNN_ia64_install_dyn_reloc
279 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
280 asection *srel, bfd_vma offset, unsigned int type,
281 long dynindx, bfd_vma addend));
282 static bfd_vma set_got_entry
283 PARAMS ((bfd *abfd, struct bfd_link_info *info,
284 struct elfNN_ia64_dyn_sym_info *dyn_i, long dynindx,
285 bfd_vma addend, bfd_vma value, unsigned int dyn_r_type));
286 static bfd_vma set_fptr_entry
287 PARAMS ((bfd *abfd, struct bfd_link_info *info,
288 struct elfNN_ia64_dyn_sym_info *dyn_i,
289 bfd_vma value));
290 static bfd_vma set_pltoff_entry
291 PARAMS ((bfd *abfd, struct bfd_link_info *info,
292 struct elfNN_ia64_dyn_sym_info *dyn_i,
293 bfd_vma value, bfd_boolean));
294 static bfd_vma elfNN_ia64_tprel_base
295 PARAMS ((struct bfd_link_info *info));
296 static bfd_vma elfNN_ia64_dtprel_base
297 PARAMS ((struct bfd_link_info *info));
298 static int elfNN_ia64_unwind_entry_compare
299 PARAMS ((const PTR, const PTR));
300 static bfd_boolean elfNN_ia64_final_link
301 PARAMS ((bfd *abfd, struct bfd_link_info *info));
302 static bfd_boolean elfNN_ia64_relocate_section
303 PARAMS ((bfd *output_bfd, struct bfd_link_info *info, bfd *input_bfd,
304 asection *input_section, bfd_byte *contents,
305 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
306 asection **local_sections));
307 static bfd_boolean elfNN_ia64_finish_dynamic_symbol
308 PARAMS ((bfd *output_bfd, struct bfd_link_info *info,
309 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
310 static bfd_boolean elfNN_ia64_finish_dynamic_sections
311 PARAMS ((bfd *abfd, struct bfd_link_info *info));
312 static bfd_boolean elfNN_ia64_set_private_flags
313 PARAMS ((bfd *abfd, flagword flags));
314 static bfd_boolean elfNN_ia64_merge_private_bfd_data
315 PARAMS ((bfd *ibfd, bfd *obfd));
316 static bfd_boolean elfNN_ia64_print_private_bfd_data
317 PARAMS ((bfd *abfd, PTR ptr));
318 static enum elf_reloc_type_class elfNN_ia64_reloc_type_class
319 PARAMS ((const Elf_Internal_Rela *));
320 static bfd_boolean elfNN_ia64_hpux_vec
321 PARAMS ((const bfd_target *vec));
322 static void elfNN_hpux_post_process_headers
323 PARAMS ((bfd *abfd, struct bfd_link_info *info));
324 bfd_boolean elfNN_hpux_backend_section_from_bfd_section
325 PARAMS ((bfd *abfd, asection *sec, int *retval));
327 /* ia64-specific relocation. */
329 /* Perform a relocation. Not much to do here as all the hard work is
330 done in elfNN_ia64_final_link_relocate. */
331 static bfd_reloc_status_type
332 elfNN_ia64_reloc (abfd, reloc, sym, data, input_section,
333 output_bfd, error_message)
334 bfd *abfd ATTRIBUTE_UNUSED;
335 arelent *reloc;
336 asymbol *sym ATTRIBUTE_UNUSED;
337 PTR data ATTRIBUTE_UNUSED;
338 asection *input_section;
339 bfd *output_bfd;
340 char **error_message;
342 if (output_bfd)
344 reloc->address += input_section->output_offset;
345 return bfd_reloc_ok;
347 *error_message = "Unsupported call to elfNN_ia64_reloc";
348 return bfd_reloc_notsupported;
351 #define IA64_HOWTO(TYPE, NAME, SIZE, PCREL, IN) \
352 HOWTO (TYPE, 0, SIZE, 0, PCREL, 0, complain_overflow_signed, \
353 elfNN_ia64_reloc, NAME, FALSE, 0, 0, IN)
355 /* This table has to be sorted according to increasing number of the
356 TYPE field. */
357 static reloc_howto_type ia64_howto_table[] =
359 IA64_HOWTO (R_IA64_NONE, "NONE", 0, FALSE, TRUE),
361 IA64_HOWTO (R_IA64_IMM14, "IMM14", 0, FALSE, TRUE),
362 IA64_HOWTO (R_IA64_IMM22, "IMM22", 0, FALSE, TRUE),
363 IA64_HOWTO (R_IA64_IMM64, "IMM64", 0, FALSE, TRUE),
364 IA64_HOWTO (R_IA64_DIR32MSB, "DIR32MSB", 2, FALSE, TRUE),
365 IA64_HOWTO (R_IA64_DIR32LSB, "DIR32LSB", 2, FALSE, TRUE),
366 IA64_HOWTO (R_IA64_DIR64MSB, "DIR64MSB", 4, FALSE, TRUE),
367 IA64_HOWTO (R_IA64_DIR64LSB, "DIR64LSB", 4, FALSE, TRUE),
369 IA64_HOWTO (R_IA64_GPREL22, "GPREL22", 0, FALSE, TRUE),
370 IA64_HOWTO (R_IA64_GPREL64I, "GPREL64I", 0, FALSE, TRUE),
371 IA64_HOWTO (R_IA64_GPREL32MSB, "GPREL32MSB", 2, FALSE, TRUE),
372 IA64_HOWTO (R_IA64_GPREL32LSB, "GPREL32LSB", 2, FALSE, TRUE),
373 IA64_HOWTO (R_IA64_GPREL64MSB, "GPREL64MSB", 4, FALSE, TRUE),
374 IA64_HOWTO (R_IA64_GPREL64LSB, "GPREL64LSB", 4, FALSE, TRUE),
376 IA64_HOWTO (R_IA64_LTOFF22, "LTOFF22", 0, FALSE, TRUE),
377 IA64_HOWTO (R_IA64_LTOFF64I, "LTOFF64I", 0, FALSE, TRUE),
379 IA64_HOWTO (R_IA64_PLTOFF22, "PLTOFF22", 0, FALSE, TRUE),
380 IA64_HOWTO (R_IA64_PLTOFF64I, "PLTOFF64I", 0, FALSE, TRUE),
381 IA64_HOWTO (R_IA64_PLTOFF64MSB, "PLTOFF64MSB", 4, FALSE, TRUE),
382 IA64_HOWTO (R_IA64_PLTOFF64LSB, "PLTOFF64LSB", 4, FALSE, TRUE),
384 IA64_HOWTO (R_IA64_FPTR64I, "FPTR64I", 0, FALSE, TRUE),
385 IA64_HOWTO (R_IA64_FPTR32MSB, "FPTR32MSB", 2, FALSE, TRUE),
386 IA64_HOWTO (R_IA64_FPTR32LSB, "FPTR32LSB", 2, FALSE, TRUE),
387 IA64_HOWTO (R_IA64_FPTR64MSB, "FPTR64MSB", 4, FALSE, TRUE),
388 IA64_HOWTO (R_IA64_FPTR64LSB, "FPTR64LSB", 4, FALSE, TRUE),
390 IA64_HOWTO (R_IA64_PCREL60B, "PCREL60B", 0, TRUE, TRUE),
391 IA64_HOWTO (R_IA64_PCREL21B, "PCREL21B", 0, TRUE, TRUE),
392 IA64_HOWTO (R_IA64_PCREL21M, "PCREL21M", 0, TRUE, TRUE),
393 IA64_HOWTO (R_IA64_PCREL21F, "PCREL21F", 0, TRUE, TRUE),
394 IA64_HOWTO (R_IA64_PCREL32MSB, "PCREL32MSB", 2, TRUE, TRUE),
395 IA64_HOWTO (R_IA64_PCREL32LSB, "PCREL32LSB", 2, TRUE, TRUE),
396 IA64_HOWTO (R_IA64_PCREL64MSB, "PCREL64MSB", 4, TRUE, TRUE),
397 IA64_HOWTO (R_IA64_PCREL64LSB, "PCREL64LSB", 4, TRUE, TRUE),
399 IA64_HOWTO (R_IA64_LTOFF_FPTR22, "LTOFF_FPTR22", 0, FALSE, TRUE),
400 IA64_HOWTO (R_IA64_LTOFF_FPTR64I, "LTOFF_FPTR64I", 0, FALSE, TRUE),
401 IA64_HOWTO (R_IA64_LTOFF_FPTR32MSB, "LTOFF_FPTR32MSB", 2, FALSE, TRUE),
402 IA64_HOWTO (R_IA64_LTOFF_FPTR32LSB, "LTOFF_FPTR32LSB", 2, FALSE, TRUE),
403 IA64_HOWTO (R_IA64_LTOFF_FPTR64MSB, "LTOFF_FPTR64MSB", 4, FALSE, TRUE),
404 IA64_HOWTO (R_IA64_LTOFF_FPTR64LSB, "LTOFF_FPTR64LSB", 4, FALSE, TRUE),
406 IA64_HOWTO (R_IA64_SEGREL32MSB, "SEGREL32MSB", 2, FALSE, TRUE),
407 IA64_HOWTO (R_IA64_SEGREL32LSB, "SEGREL32LSB", 2, FALSE, TRUE),
408 IA64_HOWTO (R_IA64_SEGREL64MSB, "SEGREL64MSB", 4, FALSE, TRUE),
409 IA64_HOWTO (R_IA64_SEGREL64LSB, "SEGREL64LSB", 4, FALSE, TRUE),
411 IA64_HOWTO (R_IA64_SECREL32MSB, "SECREL32MSB", 2, FALSE, TRUE),
412 IA64_HOWTO (R_IA64_SECREL32LSB, "SECREL32LSB", 2, FALSE, TRUE),
413 IA64_HOWTO (R_IA64_SECREL64MSB, "SECREL64MSB", 4, FALSE, TRUE),
414 IA64_HOWTO (R_IA64_SECREL64LSB, "SECREL64LSB", 4, FALSE, TRUE),
416 IA64_HOWTO (R_IA64_REL32MSB, "REL32MSB", 2, FALSE, TRUE),
417 IA64_HOWTO (R_IA64_REL32LSB, "REL32LSB", 2, FALSE, TRUE),
418 IA64_HOWTO (R_IA64_REL64MSB, "REL64MSB", 4, FALSE, TRUE),
419 IA64_HOWTO (R_IA64_REL64LSB, "REL64LSB", 4, FALSE, TRUE),
421 IA64_HOWTO (R_IA64_LTV32MSB, "LTV32MSB", 2, FALSE, TRUE),
422 IA64_HOWTO (R_IA64_LTV32LSB, "LTV32LSB", 2, FALSE, TRUE),
423 IA64_HOWTO (R_IA64_LTV64MSB, "LTV64MSB", 4, FALSE, TRUE),
424 IA64_HOWTO (R_IA64_LTV64LSB, "LTV64LSB", 4, FALSE, TRUE),
426 IA64_HOWTO (R_IA64_PCREL21BI, "PCREL21BI", 0, TRUE, TRUE),
427 IA64_HOWTO (R_IA64_PCREL22, "PCREL22", 0, TRUE, TRUE),
428 IA64_HOWTO (R_IA64_PCREL64I, "PCREL64I", 0, TRUE, TRUE),
430 IA64_HOWTO (R_IA64_IPLTMSB, "IPLTMSB", 4, FALSE, TRUE),
431 IA64_HOWTO (R_IA64_IPLTLSB, "IPLTLSB", 4, FALSE, TRUE),
432 IA64_HOWTO (R_IA64_COPY, "COPY", 4, FALSE, TRUE),
433 IA64_HOWTO (R_IA64_LTOFF22X, "LTOFF22X", 0, FALSE, TRUE),
434 IA64_HOWTO (R_IA64_LDXMOV, "LDXMOV", 0, FALSE, TRUE),
436 IA64_HOWTO (R_IA64_TPREL14, "TPREL14", 0, FALSE, FALSE),
437 IA64_HOWTO (R_IA64_TPREL22, "TPREL22", 0, FALSE, FALSE),
438 IA64_HOWTO (R_IA64_TPREL64I, "TPREL64I", 0, FALSE, FALSE),
439 IA64_HOWTO (R_IA64_TPREL64MSB, "TPREL64MSB", 8, FALSE, FALSE),
440 IA64_HOWTO (R_IA64_TPREL64LSB, "TPREL64LSB", 8, FALSE, FALSE),
441 IA64_HOWTO (R_IA64_LTOFF_TPREL22, "LTOFF_TPREL22", 0, FALSE, FALSE),
443 IA64_HOWTO (R_IA64_DTPMOD64MSB, "TPREL64MSB", 8, FALSE, FALSE),
444 IA64_HOWTO (R_IA64_DTPMOD64LSB, "TPREL64LSB", 8, FALSE, FALSE),
445 IA64_HOWTO (R_IA64_LTOFF_DTPMOD22, "LTOFF_DTPMOD22", 0, FALSE, FALSE),
447 IA64_HOWTO (R_IA64_DTPREL14, "DTPREL14", 0, FALSE, FALSE),
448 IA64_HOWTO (R_IA64_DTPREL22, "DTPREL22", 0, FALSE, FALSE),
449 IA64_HOWTO (R_IA64_DTPREL64I, "DTPREL64I", 0, FALSE, FALSE),
450 IA64_HOWTO (R_IA64_DTPREL32MSB, "DTPREL32MSB", 4, FALSE, FALSE),
451 IA64_HOWTO (R_IA64_DTPREL32LSB, "DTPREL32LSB", 4, FALSE, FALSE),
452 IA64_HOWTO (R_IA64_DTPREL64MSB, "DTPREL64MSB", 8, FALSE, FALSE),
453 IA64_HOWTO (R_IA64_DTPREL64LSB, "DTPREL64LSB", 8, FALSE, FALSE),
454 IA64_HOWTO (R_IA64_LTOFF_DTPREL22, "LTOFF_DTPREL22", 0, FALSE, FALSE),
457 static unsigned char elf_code_to_howto_index[R_IA64_MAX_RELOC_CODE + 1];
459 /* Given a BFD reloc type, return the matching HOWTO structure. */
461 static reloc_howto_type *
462 lookup_howto (rtype)
463 unsigned int rtype;
465 static int inited = 0;
466 int i;
468 if (!inited)
470 inited = 1;
472 memset (elf_code_to_howto_index, 0xff, sizeof (elf_code_to_howto_index));
473 for (i = 0; i < NELEMS (ia64_howto_table); ++i)
474 elf_code_to_howto_index[ia64_howto_table[i].type] = i;
477 BFD_ASSERT (rtype <= R_IA64_MAX_RELOC_CODE);
478 i = elf_code_to_howto_index[rtype];
479 if (i >= NELEMS (ia64_howto_table))
480 return 0;
481 return ia64_howto_table + i;
484 static reloc_howto_type*
485 elfNN_ia64_reloc_type_lookup (abfd, bfd_code)
486 bfd *abfd ATTRIBUTE_UNUSED;
487 bfd_reloc_code_real_type bfd_code;
489 unsigned int rtype;
491 switch (bfd_code)
493 case BFD_RELOC_NONE: rtype = R_IA64_NONE; break;
495 case BFD_RELOC_IA64_IMM14: rtype = R_IA64_IMM14; break;
496 case BFD_RELOC_IA64_IMM22: rtype = R_IA64_IMM22; break;
497 case BFD_RELOC_IA64_IMM64: rtype = R_IA64_IMM64; break;
499 case BFD_RELOC_IA64_DIR32MSB: rtype = R_IA64_DIR32MSB; break;
500 case BFD_RELOC_IA64_DIR32LSB: rtype = R_IA64_DIR32LSB; break;
501 case BFD_RELOC_IA64_DIR64MSB: rtype = R_IA64_DIR64MSB; break;
502 case BFD_RELOC_IA64_DIR64LSB: rtype = R_IA64_DIR64LSB; break;
504 case BFD_RELOC_IA64_GPREL22: rtype = R_IA64_GPREL22; break;
505 case BFD_RELOC_IA64_GPREL64I: rtype = R_IA64_GPREL64I; break;
506 case BFD_RELOC_IA64_GPREL32MSB: rtype = R_IA64_GPREL32MSB; break;
507 case BFD_RELOC_IA64_GPREL32LSB: rtype = R_IA64_GPREL32LSB; break;
508 case BFD_RELOC_IA64_GPREL64MSB: rtype = R_IA64_GPREL64MSB; break;
509 case BFD_RELOC_IA64_GPREL64LSB: rtype = R_IA64_GPREL64LSB; break;
511 case BFD_RELOC_IA64_LTOFF22: rtype = R_IA64_LTOFF22; break;
512 case BFD_RELOC_IA64_LTOFF64I: rtype = R_IA64_LTOFF64I; break;
514 case BFD_RELOC_IA64_PLTOFF22: rtype = R_IA64_PLTOFF22; break;
515 case BFD_RELOC_IA64_PLTOFF64I: rtype = R_IA64_PLTOFF64I; break;
516 case BFD_RELOC_IA64_PLTOFF64MSB: rtype = R_IA64_PLTOFF64MSB; break;
517 case BFD_RELOC_IA64_PLTOFF64LSB: rtype = R_IA64_PLTOFF64LSB; break;
518 case BFD_RELOC_IA64_FPTR64I: rtype = R_IA64_FPTR64I; break;
519 case BFD_RELOC_IA64_FPTR32MSB: rtype = R_IA64_FPTR32MSB; break;
520 case BFD_RELOC_IA64_FPTR32LSB: rtype = R_IA64_FPTR32LSB; break;
521 case BFD_RELOC_IA64_FPTR64MSB: rtype = R_IA64_FPTR64MSB; break;
522 case BFD_RELOC_IA64_FPTR64LSB: rtype = R_IA64_FPTR64LSB; break;
524 case BFD_RELOC_IA64_PCREL21B: rtype = R_IA64_PCREL21B; break;
525 case BFD_RELOC_IA64_PCREL21BI: rtype = R_IA64_PCREL21BI; break;
526 case BFD_RELOC_IA64_PCREL21M: rtype = R_IA64_PCREL21M; break;
527 case BFD_RELOC_IA64_PCREL21F: rtype = R_IA64_PCREL21F; break;
528 case BFD_RELOC_IA64_PCREL22: rtype = R_IA64_PCREL22; break;
529 case BFD_RELOC_IA64_PCREL60B: rtype = R_IA64_PCREL60B; break;
530 case BFD_RELOC_IA64_PCREL64I: rtype = R_IA64_PCREL64I; break;
531 case BFD_RELOC_IA64_PCREL32MSB: rtype = R_IA64_PCREL32MSB; break;
532 case BFD_RELOC_IA64_PCREL32LSB: rtype = R_IA64_PCREL32LSB; break;
533 case BFD_RELOC_IA64_PCREL64MSB: rtype = R_IA64_PCREL64MSB; break;
534 case BFD_RELOC_IA64_PCREL64LSB: rtype = R_IA64_PCREL64LSB; break;
536 case BFD_RELOC_IA64_LTOFF_FPTR22: rtype = R_IA64_LTOFF_FPTR22; break;
537 case BFD_RELOC_IA64_LTOFF_FPTR64I: rtype = R_IA64_LTOFF_FPTR64I; break;
538 case BFD_RELOC_IA64_LTOFF_FPTR32MSB: rtype = R_IA64_LTOFF_FPTR32MSB; break;
539 case BFD_RELOC_IA64_LTOFF_FPTR32LSB: rtype = R_IA64_LTOFF_FPTR32LSB; break;
540 case BFD_RELOC_IA64_LTOFF_FPTR64MSB: rtype = R_IA64_LTOFF_FPTR64MSB; break;
541 case BFD_RELOC_IA64_LTOFF_FPTR64LSB: rtype = R_IA64_LTOFF_FPTR64LSB; break;
543 case BFD_RELOC_IA64_SEGREL32MSB: rtype = R_IA64_SEGREL32MSB; break;
544 case BFD_RELOC_IA64_SEGREL32LSB: rtype = R_IA64_SEGREL32LSB; break;
545 case BFD_RELOC_IA64_SEGREL64MSB: rtype = R_IA64_SEGREL64MSB; break;
546 case BFD_RELOC_IA64_SEGREL64LSB: rtype = R_IA64_SEGREL64LSB; break;
548 case BFD_RELOC_IA64_SECREL32MSB: rtype = R_IA64_SECREL32MSB; break;
549 case BFD_RELOC_IA64_SECREL32LSB: rtype = R_IA64_SECREL32LSB; break;
550 case BFD_RELOC_IA64_SECREL64MSB: rtype = R_IA64_SECREL64MSB; break;
551 case BFD_RELOC_IA64_SECREL64LSB: rtype = R_IA64_SECREL64LSB; break;
553 case BFD_RELOC_IA64_REL32MSB: rtype = R_IA64_REL32MSB; break;
554 case BFD_RELOC_IA64_REL32LSB: rtype = R_IA64_REL32LSB; break;
555 case BFD_RELOC_IA64_REL64MSB: rtype = R_IA64_REL64MSB; break;
556 case BFD_RELOC_IA64_REL64LSB: rtype = R_IA64_REL64LSB; break;
558 case BFD_RELOC_IA64_LTV32MSB: rtype = R_IA64_LTV32MSB; break;
559 case BFD_RELOC_IA64_LTV32LSB: rtype = R_IA64_LTV32LSB; break;
560 case BFD_RELOC_IA64_LTV64MSB: rtype = R_IA64_LTV64MSB; break;
561 case BFD_RELOC_IA64_LTV64LSB: rtype = R_IA64_LTV64LSB; break;
563 case BFD_RELOC_IA64_IPLTMSB: rtype = R_IA64_IPLTMSB; break;
564 case BFD_RELOC_IA64_IPLTLSB: rtype = R_IA64_IPLTLSB; break;
565 case BFD_RELOC_IA64_COPY: rtype = R_IA64_COPY; break;
566 case BFD_RELOC_IA64_LTOFF22X: rtype = R_IA64_LTOFF22X; break;
567 case BFD_RELOC_IA64_LDXMOV: rtype = R_IA64_LDXMOV; break;
569 case BFD_RELOC_IA64_TPREL14: rtype = R_IA64_TPREL14; break;
570 case BFD_RELOC_IA64_TPREL22: rtype = R_IA64_TPREL22; break;
571 case BFD_RELOC_IA64_TPREL64I: rtype = R_IA64_TPREL64I; break;
572 case BFD_RELOC_IA64_TPREL64MSB: rtype = R_IA64_TPREL64MSB; break;
573 case BFD_RELOC_IA64_TPREL64LSB: rtype = R_IA64_TPREL64LSB; break;
574 case BFD_RELOC_IA64_LTOFF_TPREL22: rtype = R_IA64_LTOFF_TPREL22; break;
576 case BFD_RELOC_IA64_DTPMOD64MSB: rtype = R_IA64_DTPMOD64MSB; break;
577 case BFD_RELOC_IA64_DTPMOD64LSB: rtype = R_IA64_DTPMOD64LSB; break;
578 case BFD_RELOC_IA64_LTOFF_DTPMOD22: rtype = R_IA64_LTOFF_DTPMOD22; break;
580 case BFD_RELOC_IA64_DTPREL14: rtype = R_IA64_DTPREL14; break;
581 case BFD_RELOC_IA64_DTPREL22: rtype = R_IA64_DTPREL22; break;
582 case BFD_RELOC_IA64_DTPREL64I: rtype = R_IA64_DTPREL64I; break;
583 case BFD_RELOC_IA64_DTPREL32MSB: rtype = R_IA64_DTPREL32MSB; break;
584 case BFD_RELOC_IA64_DTPREL32LSB: rtype = R_IA64_DTPREL32LSB; break;
585 case BFD_RELOC_IA64_DTPREL64MSB: rtype = R_IA64_DTPREL64MSB; break;
586 case BFD_RELOC_IA64_DTPREL64LSB: rtype = R_IA64_DTPREL64LSB; break;
587 case BFD_RELOC_IA64_LTOFF_DTPREL22: rtype = R_IA64_LTOFF_DTPREL22; break;
589 default: return 0;
591 return lookup_howto (rtype);
594 /* Given a ELF reloc, return the matching HOWTO structure. */
596 static void
597 elfNN_ia64_info_to_howto (abfd, bfd_reloc, elf_reloc)
598 bfd *abfd ATTRIBUTE_UNUSED;
599 arelent *bfd_reloc;
600 Elf_Internal_Rela *elf_reloc;
602 bfd_reloc->howto
603 = lookup_howto ((unsigned int) ELFNN_R_TYPE (elf_reloc->r_info));
606 #define PLT_HEADER_SIZE (3 * 16)
607 #define PLT_MIN_ENTRY_SIZE (1 * 16)
608 #define PLT_FULL_ENTRY_SIZE (2 * 16)
609 #define PLT_RESERVED_WORDS 3
611 static const bfd_byte plt_header[PLT_HEADER_SIZE] =
613 0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21, /* [MMI] mov r2=r14;; */
614 0xe0, 0x00, 0x08, 0x00, 0x48, 0x00, /* addl r14=0,r2 */
615 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
616 0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14, /* [MMI] ld8 r16=[r14],8;; */
617 0x10, 0x41, 0x38, 0x30, 0x28, 0x00, /* ld8 r17=[r14],8 */
618 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
619 0x11, 0x08, 0x00, 0x1c, 0x18, 0x10, /* [MIB] ld8 r1=[r14] */
620 0x60, 0x88, 0x04, 0x80, 0x03, 0x00, /* mov b6=r17 */
621 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
624 static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
626 0x11, 0x78, 0x00, 0x00, 0x00, 0x24, /* [MIB] mov r15=0 */
627 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, /* nop.i 0x0 */
628 0x00, 0x00, 0x00, 0x40 /* br.few 0 <PLT0>;; */
631 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
633 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
634 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
635 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
636 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
637 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
638 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
641 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
642 #define AIX_DYNAMIC_INTERPRETER "/usr/lib/ia64l64/libc.so.1"
643 #define DYNAMIC_INTERPRETER(abfd) \
644 (elfNN_ia64_aix_vec (abfd->xvec) ? AIX_DYNAMIC_INTERPRETER : ELF_DYNAMIC_INTERPRETER)
646 /* Select out of range branch fixup type. Note that Itanium does
647 not support brl, and so it gets emulated by the kernel. */
648 #undef USE_BRL
650 #ifdef USE_BRL
651 static const bfd_byte oor_brl[16] =
653 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
654 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.sptk.few tgt;; */
655 0x00, 0x00, 0x00, 0xc0
657 #else
658 static const bfd_byte oor_ip[48] =
660 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
661 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, /* movl r15=0 */
662 0x01, 0x00, 0x00, 0x60,
663 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MII] nop.m 0 */
664 0x00, 0x01, 0x00, 0x60, 0x00, 0x00, /* mov r16=ip;; */
665 0xf2, 0x80, 0x00, 0x80, /* add r16=r15,r16;; */
666 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
667 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
668 0x60, 0x00, 0x80, 0x00 /* br b6;; */
670 #endif
672 /* These functions do relaxation for IA-64 ELF.
674 This is primarily to support branches to targets out of range;
675 relaxation of R_IA64_LTOFF22X and R_IA64_LDXMOV is handled in
676 relocate_section directly. */
678 static bfd_boolean
679 elfNN_ia64_relax_section (abfd, sec, link_info, again)
680 bfd *abfd;
681 asection *sec;
682 struct bfd_link_info *link_info;
683 bfd_boolean *again;
685 struct one_fixup
687 struct one_fixup *next;
688 asection *tsec;
689 bfd_vma toff;
690 bfd_vma trampoff;
693 Elf_Internal_Shdr *symtab_hdr;
694 Elf_Internal_Rela *internal_relocs;
695 Elf_Internal_Rela *irel, *irelend;
696 bfd_byte *contents;
697 Elf_Internal_Sym *isymbuf = NULL;
698 struct elfNN_ia64_link_hash_table *ia64_info;
699 struct one_fixup *fixups = NULL;
700 bfd_boolean changed_contents = FALSE;
701 bfd_boolean changed_relocs = FALSE;
703 /* Assume we're not going to change any sizes, and we'll only need
704 one pass. */
705 *again = FALSE;
707 /* Nothing to do if there are no relocations. */
708 if ((sec->flags & SEC_RELOC) == 0
709 || sec->reloc_count == 0)
710 return TRUE;
712 /* If this is the first time we have been called for this section,
713 initialize the cooked size. */
714 if (sec->_cooked_size == 0)
715 sec->_cooked_size = sec->_raw_size;
717 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
719 /* Load the relocations for this section. */
720 internal_relocs = (_bfd_elfNN_link_read_relocs
721 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
722 link_info->keep_memory));
723 if (internal_relocs == NULL)
724 return FALSE;
726 ia64_info = elfNN_ia64_hash_table (link_info);
727 irelend = internal_relocs + sec->reloc_count;
729 for (irel = internal_relocs; irel < irelend; irel++)
731 unsigned long r_type = ELFNN_R_TYPE (irel->r_info);
732 if (r_type == R_IA64_PCREL21B
733 || r_type == R_IA64_PCREL21BI
734 || r_type == R_IA64_PCREL21M
735 || r_type == R_IA64_PCREL21F)
736 break;
739 /* No branch-type relocations. */
740 if (irel == irelend)
742 if (elf_section_data (sec)->relocs != internal_relocs)
743 free (internal_relocs);
744 return TRUE;
747 /* Get the section contents. */
748 if (elf_section_data (sec)->this_hdr.contents != NULL)
749 contents = elf_section_data (sec)->this_hdr.contents;
750 else
752 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
753 if (contents == NULL)
754 goto error_return;
756 if (! bfd_get_section_contents (abfd, sec, contents,
757 (file_ptr) 0, sec->_raw_size))
758 goto error_return;
761 for (; irel < irelend; irel++)
763 unsigned long r_type = ELFNN_R_TYPE (irel->r_info);
764 bfd_vma symaddr, reladdr, trampoff, toff, roff;
765 asection *tsec;
766 struct one_fixup *f;
767 bfd_size_type amt;
769 if (r_type != R_IA64_PCREL21B
770 && r_type != R_IA64_PCREL21BI
771 && r_type != R_IA64_PCREL21M
772 && r_type != R_IA64_PCREL21F)
773 continue;
775 /* Get the value of the symbol referred to by the reloc. */
776 if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
778 /* A local symbol. */
779 Elf_Internal_Sym *isym;
781 /* Read this BFD's local symbols. */
782 if (isymbuf == NULL)
784 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
785 if (isymbuf == NULL)
786 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
787 symtab_hdr->sh_info, 0,
788 NULL, NULL, NULL);
789 if (isymbuf == 0)
790 goto error_return;
793 isym = isymbuf + ELF64_R_SYM (irel->r_info);
794 if (isym->st_shndx == SHN_UNDEF)
795 continue; /* We can't do anthing with undefined symbols. */
796 else if (isym->st_shndx == SHN_ABS)
797 tsec = bfd_abs_section_ptr;
798 else if (isym->st_shndx == SHN_COMMON)
799 tsec = bfd_com_section_ptr;
800 else if (isym->st_shndx == SHN_IA_64_ANSI_COMMON)
801 tsec = bfd_com_section_ptr;
802 else
803 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
805 toff = isym->st_value;
807 else
809 unsigned long indx;
810 struct elf_link_hash_entry *h;
811 struct elfNN_ia64_dyn_sym_info *dyn_i;
813 indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
814 h = elf_sym_hashes (abfd)[indx];
815 BFD_ASSERT (h != NULL);
817 while (h->root.type == bfd_link_hash_indirect
818 || h->root.type == bfd_link_hash_warning)
819 h = (struct elf_link_hash_entry *) h->root.u.i.link;
821 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, FALSE);
823 /* For branches to dynamic symbols, we're interested instead
824 in a branch to the PLT entry. */
825 if (dyn_i && dyn_i->want_plt2)
827 /* Internal branches shouldn't be sent to the PLT.
828 Leave this for now and we'll give an error later. */
829 if (r_type != R_IA64_PCREL21B)
830 continue;
832 tsec = ia64_info->plt_sec;
833 toff = dyn_i->plt2_offset;
835 else
837 /* We can't do anthing with undefined symbols. */
838 if (h->root.type == bfd_link_hash_undefined
839 || h->root.type == bfd_link_hash_undefweak)
840 continue;
842 tsec = h->root.u.def.section;
843 toff = h->root.u.def.value;
847 symaddr = (tsec->output_section->vma
848 + tsec->output_offset
849 + toff
850 + irel->r_addend);
852 roff = irel->r_offset;
853 reladdr = (sec->output_section->vma
854 + sec->output_offset
855 + roff) & (bfd_vma) -4;
857 /* If the branch is in range, no need to do anything. */
858 if ((bfd_signed_vma) (symaddr - reladdr) >= -0x1000000
859 && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
860 continue;
862 /* If the branch and target are in the same section, you've
863 got one honking big section and we can't help you. You'll
864 get an error message later. */
865 if (tsec == sec)
866 continue;
868 /* Look for an existing fixup to this address. */
869 for (f = fixups; f ; f = f->next)
870 if (f->tsec == tsec && f->toff == toff)
871 break;
873 if (f == NULL)
875 /* Two alternatives: If it's a branch to a PLT entry, we can
876 make a copy of the FULL_PLT entry. Otherwise, we'll have
877 to use a `brl' insn to get where we're going. */
879 size_t size;
881 if (tsec == ia64_info->plt_sec)
882 size = sizeof (plt_full_entry);
883 else
885 #ifdef USE_BRL
886 size = sizeof (oor_brl);
887 #else
888 size = sizeof (oor_ip);
889 #endif
892 /* Resize the current section to make room for the new branch. */
893 trampoff = (sec->_cooked_size + 15) & (bfd_vma) -16;
894 amt = trampoff + size;
895 contents = (bfd_byte *) bfd_realloc (contents, amt);
896 if (contents == NULL)
897 goto error_return;
898 sec->_cooked_size = amt;
900 if (tsec == ia64_info->plt_sec)
902 memcpy (contents + trampoff, plt_full_entry, size);
904 /* Hijack the old relocation for use as the PLTOFF reloc. */
905 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
906 R_IA64_PLTOFF22);
907 irel->r_offset = trampoff;
909 else
911 #ifdef USE_BRL
912 memcpy (contents + trampoff, oor_brl, size);
913 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
914 R_IA64_PCREL60B);
915 irel->r_offset = trampoff + 2;
916 #else
917 memcpy (contents + trampoff, oor_ip, size);
918 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
919 R_IA64_PCREL64I);
920 irel->r_addend -= 16;
921 irel->r_offset = trampoff + 2;
922 #endif
925 /* Record the fixup so we don't do it again this section. */
926 f = (struct one_fixup *) bfd_malloc ((bfd_size_type) sizeof (*f));
927 f->next = fixups;
928 f->tsec = tsec;
929 f->toff = toff;
930 f->trampoff = trampoff;
931 fixups = f;
933 else
935 /* Nop out the reloc, since we're finalizing things here. */
936 irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
939 /* Fix up the existing branch to hit the trampoline. Hope like
940 hell this doesn't overflow too. */
941 if (elfNN_ia64_install_value (abfd, contents + roff,
942 f->trampoff - (roff & (bfd_vma) -4),
943 r_type) != bfd_reloc_ok)
944 goto error_return;
946 changed_contents = TRUE;
947 changed_relocs = TRUE;
950 /* Clean up and go home. */
951 while (fixups)
953 struct one_fixup *f = fixups;
954 fixups = fixups->next;
955 free (f);
958 if (isymbuf != NULL
959 && symtab_hdr->contents != (unsigned char *) isymbuf)
961 if (! link_info->keep_memory)
962 free (isymbuf);
963 else
965 /* Cache the symbols for elf_link_input_bfd. */
966 symtab_hdr->contents = (unsigned char *) isymbuf;
970 if (contents != NULL
971 && elf_section_data (sec)->this_hdr.contents != contents)
973 if (!changed_contents && !link_info->keep_memory)
974 free (contents);
975 else
977 /* Cache the section contents for elf_link_input_bfd. */
978 elf_section_data (sec)->this_hdr.contents = contents;
982 if (elf_section_data (sec)->relocs != internal_relocs)
984 if (!changed_relocs)
985 free (internal_relocs);
986 else
987 elf_section_data (sec)->relocs = internal_relocs;
990 *again = changed_contents || changed_relocs;
991 return TRUE;
993 error_return:
994 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
995 free (isymbuf);
996 if (contents != NULL
997 && elf_section_data (sec)->this_hdr.contents != contents)
998 free (contents);
999 if (internal_relocs != NULL
1000 && elf_section_data (sec)->relocs != internal_relocs)
1001 free (internal_relocs);
1002 return FALSE;
1005 /* Return TRUE if NAME is an unwind table section name. */
1007 static inline bfd_boolean
1008 is_unwind_section_name (abfd, name)
1009 bfd *abfd;
1010 const char *name;
1012 size_t len1, len2, len3;
1014 if (elfNN_ia64_hpux_vec (abfd->xvec)
1015 && !strcmp (name, ELF_STRING_ia64_unwind_hdr))
1016 return FALSE;
1018 len1 = sizeof (ELF_STRING_ia64_unwind) - 1;
1019 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
1020 len3 = sizeof (ELF_STRING_ia64_unwind_once) - 1;
1021 return ((strncmp (name, ELF_STRING_ia64_unwind, len1) == 0
1022 && strncmp (name, ELF_STRING_ia64_unwind_info, len2) != 0)
1023 || strncmp (name, ELF_STRING_ia64_unwind_once, len3) == 0);
1026 /* Handle an IA-64 specific section when reading an object file. This
1027 is called when elfcode.h finds a section with an unknown type. */
1029 static bfd_boolean
1030 elfNN_ia64_section_from_shdr (abfd, hdr, name)
1031 bfd *abfd;
1032 Elf_Internal_Shdr *hdr;
1033 const char *name;
1035 asection *newsect;
1037 /* There ought to be a place to keep ELF backend specific flags, but
1038 at the moment there isn't one. We just keep track of the
1039 sections by their name, instead. Fortunately, the ABI gives
1040 suggested names for all the MIPS specific sections, so we will
1041 probably get away with this. */
1042 switch (hdr->sh_type)
1044 case SHT_IA_64_UNWIND:
1045 case SHT_IA_64_HP_OPT_ANOT:
1046 break;
1048 case SHT_IA_64_EXT:
1049 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
1050 return FALSE;
1051 break;
1053 default:
1054 return FALSE;
1057 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1058 return FALSE;
1059 newsect = hdr->bfd_section;
1061 return TRUE;
1064 /* Convert IA-64 specific section flags to bfd internal section flags. */
1066 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
1067 flag. */
1069 static bfd_boolean
1070 elfNN_ia64_section_flags (flags, hdr)
1071 flagword *flags;
1072 Elf_Internal_Shdr *hdr;
1074 if (hdr->sh_flags & SHF_IA_64_SHORT)
1075 *flags |= SEC_SMALL_DATA;
1077 return TRUE;
1080 /* Set the correct type for an IA-64 ELF section. We do this by the
1081 section name, which is a hack, but ought to work. */
1083 static bfd_boolean
1084 elfNN_ia64_fake_sections (abfd, hdr, sec)
1085 bfd *abfd ATTRIBUTE_UNUSED;
1086 Elf_Internal_Shdr *hdr;
1087 asection *sec;
1089 register const char *name;
1091 name = bfd_get_section_name (abfd, sec);
1093 if (is_unwind_section_name (abfd, name))
1095 /* We don't have the sections numbered at this point, so sh_info
1096 is set later, in elfNN_ia64_final_write_processing. */
1097 hdr->sh_type = SHT_IA_64_UNWIND;
1098 hdr->sh_flags |= SHF_LINK_ORDER;
1100 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
1101 hdr->sh_type = SHT_IA_64_EXT;
1102 else if (strcmp (name, ".HP.opt_annot") == 0)
1103 hdr->sh_type = SHT_IA_64_HP_OPT_ANOT;
1104 else if (strcmp (name, ".reloc") == 0)
1105 /* This is an ugly, but unfortunately necessary hack that is
1106 needed when producing EFI binaries on IA-64. It tells
1107 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1108 containing ELF relocation info. We need this hack in order to
1109 be able to generate ELF binaries that can be translated into
1110 EFI applications (which are essentially COFF objects). Those
1111 files contain a COFF ".reloc" section inside an ELFNN object,
1112 which would normally cause BFD to segfault because it would
1113 attempt to interpret this section as containing relocation
1114 entries for section "oc". With this hack enabled, ".reloc"
1115 will be treated as a normal data section, which will avoid the
1116 segfault. However, you won't be able to create an ELFNN binary
1117 with a section named "oc" that needs relocations, but that's
1118 the kind of ugly side-effects you get when detecting section
1119 types based on their names... In practice, this limitation is
1120 unlikely to bite. */
1121 hdr->sh_type = SHT_PROGBITS;
1123 if (sec->flags & SEC_SMALL_DATA)
1124 hdr->sh_flags |= SHF_IA_64_SHORT;
1126 return TRUE;
1129 /* The final processing done just before writing out an IA-64 ELF
1130 object file. */
1132 static void
1133 elfNN_ia64_final_write_processing (abfd, linker)
1134 bfd *abfd;
1135 bfd_boolean linker ATTRIBUTE_UNUSED;
1137 Elf_Internal_Shdr *hdr;
1138 const char *sname;
1139 asection *text_sect, *s;
1140 size_t len;
1142 for (s = abfd->sections; s; s = s->next)
1144 hdr = &elf_section_data (s)->this_hdr;
1145 switch (hdr->sh_type)
1147 case SHT_IA_64_UNWIND:
1148 /* See comments in gas/config/tc-ia64.c:dot_endp on why we
1149 have to do this. */
1150 sname = bfd_get_section_name (abfd, s);
1151 len = sizeof (ELF_STRING_ia64_unwind) - 1;
1152 if (sname && strncmp (sname, ELF_STRING_ia64_unwind, len) == 0)
1154 sname += len;
1156 if (sname[0] == '\0')
1157 /* .IA_64.unwind -> .text */
1158 text_sect = bfd_get_section_by_name (abfd, ".text");
1159 else
1160 /* .IA_64.unwindFOO -> FOO */
1161 text_sect = bfd_get_section_by_name (abfd, sname);
1163 else if (sname
1164 && (len = sizeof (ELF_STRING_ia64_unwind_once) - 1,
1165 strncmp (sname, ELF_STRING_ia64_unwind_once, len)) == 0)
1167 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.t.FOO */
1168 size_t len2 = sizeof (".gnu.linkonce.t.") - 1;
1169 char *once_name = bfd_malloc (len2 + strlen (sname + len) + 1);
1171 if (once_name != NULL)
1173 memcpy (once_name, ".gnu.linkonce.t.", len2);
1174 strcpy (once_name + len2, sname + len);
1175 text_sect = bfd_get_section_by_name (abfd, once_name);
1176 free (once_name);
1178 else
1179 /* Should only happen if we run out of memory, in
1180 which case we're probably toast anyway. Try to
1181 cope by finding the section the slow way. */
1182 for (text_sect = abfd->sections;
1183 text_sect != NULL;
1184 text_sect = text_sect->next)
1186 if (strncmp (bfd_section_name (abfd, text_sect),
1187 ".gnu.linkonce.t.", len2) == 0
1188 && strcmp (bfd_section_name (abfd, text_sect) + len2,
1189 sname + len) == 0)
1190 break;
1193 else
1194 /* last resort: fall back on .text */
1195 text_sect = bfd_get_section_by_name (abfd, ".text");
1197 if (text_sect)
1199 /* The IA-64 processor-specific ABI requires setting
1200 sh_link to the unwind section, whereas HP-UX requires
1201 sh_info to do so. For maximum compatibility, we'll
1202 set both for now... */
1203 hdr->sh_link = elf_section_data (text_sect)->this_idx;
1204 hdr->sh_info = elf_section_data (text_sect)->this_idx;
1206 break;
1210 if (! elf_flags_init (abfd))
1212 unsigned long flags = 0;
1214 if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
1215 flags |= EF_IA_64_BE;
1216 if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
1217 flags |= EF_IA_64_ABI64;
1219 elf_elfheader(abfd)->e_flags = flags;
1220 elf_flags_init (abfd) = TRUE;
1224 /* Hook called by the linker routine which adds symbols from an object
1225 file. We use it to put .comm items in .sbss, and not .bss. */
1227 static bfd_boolean
1228 elfNN_ia64_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1229 bfd *abfd;
1230 struct bfd_link_info *info;
1231 const Elf_Internal_Sym *sym;
1232 const char **namep ATTRIBUTE_UNUSED;
1233 flagword *flagsp ATTRIBUTE_UNUSED;
1234 asection **secp;
1235 bfd_vma *valp;
1237 if (sym->st_shndx == SHN_COMMON
1238 && !info->relocateable
1239 && sym->st_size <= elf_gp_size (abfd))
1241 /* Common symbols less than or equal to -G nn bytes are
1242 automatically put into .sbss. */
1244 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1246 if (scomm == NULL)
1248 scomm = bfd_make_section (abfd, ".scommon");
1249 if (scomm == NULL
1250 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
1251 | SEC_IS_COMMON
1252 | SEC_LINKER_CREATED)))
1253 return FALSE;
1256 *secp = scomm;
1257 *valp = sym->st_size;
1260 return TRUE;
1263 static bfd_boolean
1264 elfNN_ia64_aix_vec (const bfd_target *vec)
1266 extern const bfd_target bfd_elfNN_ia64_aix_little_vec;
1267 extern const bfd_target bfd_elfNN_ia64_aix_big_vec;
1269 return (/**/vec == & bfd_elfNN_ia64_aix_little_vec
1270 || vec == & bfd_elfNN_ia64_aix_big_vec);
1273 /* Hook called by the linker routine which adds symbols from an object
1274 file. We use it to handle OS-specific symbols. */
1276 static bfd_boolean
1277 elfNN_ia64_aix_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1278 bfd *abfd;
1279 struct bfd_link_info *info;
1280 const Elf_Internal_Sym *sym;
1281 const char **namep;
1282 flagword *flagsp;
1283 asection **secp;
1284 bfd_vma *valp;
1286 if (strcmp (*namep, "__GLOB_DATA_PTR") == 0)
1288 /* Define __GLOB_DATA_PTR when it is encountered. This is expected to
1289 be a linker-defined symbol by the Aix C runtime startup code. IBM sez
1290 no one else should use it b/c it is undocumented. */
1291 struct elf_link_hash_entry *h;
1293 h = elf_link_hash_lookup (elf_hash_table (info), *namep,
1294 FALSE, FALSE, FALSE);
1295 if (h == NULL)
1297 struct elf_backend_data *bed;
1298 struct elfNN_ia64_link_hash_table *ia64_info;
1299 struct bfd_link_hash_entry *bh = NULL;
1301 bed = get_elf_backend_data (abfd);
1302 ia64_info = elfNN_ia64_hash_table (info);
1304 if (!(_bfd_generic_link_add_one_symbol
1305 (info, abfd, *namep, BSF_GLOBAL,
1306 bfd_get_section_by_name (abfd, ".bss"),
1307 bed->got_symbol_offset, (const char *) NULL, FALSE,
1308 bed->collect, &bh)))
1309 return FALSE;
1311 h = (struct elf_link_hash_entry *) bh;
1312 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1313 h->type = STT_OBJECT;
1315 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1316 return FALSE;
1319 return TRUE;
1321 else if (sym->st_shndx == SHN_LOOS)
1323 unsigned int i;
1325 /* SHN_AIX_SYSCALL: Treat this as any other symbol. The special symbol
1326 is only relevant when compiling code for extended system calls.
1327 Replace the "special" section with .text, if possible.
1328 Note that these symbols are always assumed to be in .text. */
1329 for (i = 1; i < elf_numsections (abfd); i++)
1331 asection * sec = bfd_section_from_elf_index (abfd, i);
1333 if (sec && strcmp (sec->name, ".text") == 0)
1335 *secp = sec;
1336 break;
1340 if (*secp == NULL)
1341 *secp = bfd_abs_section_ptr;
1343 *valp = sym->st_size;
1345 return TRUE;
1347 else
1349 return elfNN_ia64_add_symbol_hook (abfd, info, sym,
1350 namep, flagsp, secp, valp);
1354 bfd_boolean
1355 elfNN_ia64_aix_link_add_symbols (abfd, info)
1356 bfd *abfd;
1357 struct bfd_link_info *info;
1359 /* Make sure dynamic sections are always created. */
1360 if (! elf_hash_table (info)->dynamic_sections_created
1361 && abfd->xvec == info->hash->creator)
1363 if (! bfd_elfNN_link_create_dynamic_sections (abfd, info))
1364 return FALSE;
1367 /* Now do the standard call. */
1368 return bfd_elfNN_bfd_link_add_symbols (abfd, info);
1371 /* Return the number of additional phdrs we will need. */
1373 static int
1374 elfNN_ia64_additional_program_headers (abfd)
1375 bfd *abfd;
1377 asection *s;
1378 int ret = 0;
1380 /* See if we need a PT_IA_64_ARCHEXT segment. */
1381 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1382 if (s && (s->flags & SEC_LOAD))
1383 ++ret;
1385 /* Count how many PT_IA_64_UNWIND segments we need. */
1386 for (s = abfd->sections; s; s = s->next)
1387 if (is_unwind_section_name (abfd, s->name) && (s->flags & SEC_LOAD))
1388 ++ret;
1390 return ret;
1393 static bfd_boolean
1394 elfNN_ia64_modify_segment_map (abfd)
1395 bfd *abfd;
1397 struct elf_segment_map *m, **pm;
1398 Elf_Internal_Shdr *hdr;
1399 asection *s;
1401 /* If we need a PT_IA_64_ARCHEXT segment, it must come before
1402 all PT_LOAD segments. */
1403 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1404 if (s && (s->flags & SEC_LOAD))
1406 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1407 if (m->p_type == PT_IA_64_ARCHEXT)
1408 break;
1409 if (m == NULL)
1411 m = ((struct elf_segment_map *)
1412 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1413 if (m == NULL)
1414 return FALSE;
1416 m->p_type = PT_IA_64_ARCHEXT;
1417 m->count = 1;
1418 m->sections[0] = s;
1420 /* We want to put it after the PHDR and INTERP segments. */
1421 pm = &elf_tdata (abfd)->segment_map;
1422 while (*pm != NULL
1423 && ((*pm)->p_type == PT_PHDR
1424 || (*pm)->p_type == PT_INTERP))
1425 pm = &(*pm)->next;
1427 m->next = *pm;
1428 *pm = m;
1432 /* Install PT_IA_64_UNWIND segments, if needed. */
1433 for (s = abfd->sections; s; s = s->next)
1435 hdr = &elf_section_data (s)->this_hdr;
1436 if (hdr->sh_type != SHT_IA_64_UNWIND)
1437 continue;
1439 if (s && (s->flags & SEC_LOAD))
1441 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1442 if (m->p_type == PT_IA_64_UNWIND)
1444 int i;
1446 /* Look through all sections in the unwind segment
1447 for a match since there may be multiple sections
1448 to a segment. */
1449 for (i = m->count - 1; i >= 0; --i)
1450 if (m->sections[i] == s)
1451 break;
1453 if (i >= 0)
1454 break;
1457 if (m == NULL)
1459 m = ((struct elf_segment_map *)
1460 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1461 if (m == NULL)
1462 return FALSE;
1464 m->p_type = PT_IA_64_UNWIND;
1465 m->count = 1;
1466 m->sections[0] = s;
1467 m->next = NULL;
1469 /* We want to put it last. */
1470 pm = &elf_tdata (abfd)->segment_map;
1471 while (*pm != NULL)
1472 pm = &(*pm)->next;
1473 *pm = m;
1478 /* Turn on PF_IA_64_NORECOV if needed. This involves traversing all of
1479 the input sections for each output section in the segment and testing
1480 for SHF_IA_64_NORECOV on each. */
1481 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1482 if (m->p_type == PT_LOAD)
1484 int i;
1485 for (i = m->count - 1; i >= 0; --i)
1487 struct bfd_link_order *order = m->sections[i]->link_order_head;
1488 while (order)
1490 if (order->type == bfd_indirect_link_order)
1492 asection *is = order->u.indirect.section;
1493 bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
1494 if (flags & SHF_IA_64_NORECOV)
1496 m->p_flags |= PF_IA_64_NORECOV;
1497 goto found;
1500 order = order->next;
1503 found:;
1506 return TRUE;
1509 /* According to the Tahoe assembler spec, all labels starting with a
1510 '.' are local. */
1512 static bfd_boolean
1513 elfNN_ia64_is_local_label_name (abfd, name)
1514 bfd *abfd ATTRIBUTE_UNUSED;
1515 const char *name;
1517 return name[0] == '.';
1520 /* Should we do dynamic things to this symbol? */
1522 static bfd_boolean
1523 elfNN_ia64_dynamic_symbol_p (h, info)
1524 struct elf_link_hash_entry *h;
1525 struct bfd_link_info *info;
1527 if (h == NULL)
1528 return FALSE;
1530 while (h->root.type == bfd_link_hash_indirect
1531 || h->root.type == bfd_link_hash_warning)
1532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1534 if (h->dynindx == -1)
1535 return FALSE;
1536 switch (ELF_ST_VISIBILITY (h->other))
1538 case STV_INTERNAL:
1539 case STV_HIDDEN:
1540 return FALSE;
1541 default:
1542 break;
1545 if (h->root.type == bfd_link_hash_undefweak
1546 || h->root.type == bfd_link_hash_defweak)
1547 return TRUE;
1549 if ((info->shared && (!info->symbolic || info->allow_shlib_undefined))
1550 || ((h->elf_link_hash_flags
1551 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))
1552 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
1553 return TRUE;
1555 return FALSE;
1558 static bfd_boolean
1559 elfNN_ia64_local_hash_table_init (ht, abfd, new)
1560 struct elfNN_ia64_local_hash_table *ht;
1561 bfd *abfd ATTRIBUTE_UNUSED;
1562 new_hash_entry_func new;
1564 memset (ht, 0, sizeof (*ht));
1565 return bfd_hash_table_init (&ht->root, new);
1568 static struct bfd_hash_entry*
1569 elfNN_ia64_new_loc_hash_entry (entry, table, string)
1570 struct bfd_hash_entry *entry;
1571 struct bfd_hash_table *table;
1572 const char *string;
1574 struct elfNN_ia64_local_hash_entry *ret;
1575 ret = (struct elfNN_ia64_local_hash_entry *) entry;
1577 /* Allocate the structure if it has not already been allocated by a
1578 subclass. */
1579 if (!ret)
1580 ret = bfd_hash_allocate (table, sizeof (*ret));
1582 if (!ret)
1583 return 0;
1585 /* Initialize our local data. All zeros, and definitely easier
1586 than setting a handful of bit fields. */
1587 memset (ret, 0, sizeof (*ret));
1589 /* Call the allocation method of the superclass. */
1590 ret = ((struct elfNN_ia64_local_hash_entry *)
1591 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
1593 return (struct bfd_hash_entry *) ret;
1596 static struct bfd_hash_entry*
1597 elfNN_ia64_new_elf_hash_entry (entry, table, string)
1598 struct bfd_hash_entry *entry;
1599 struct bfd_hash_table *table;
1600 const char *string;
1602 struct elfNN_ia64_link_hash_entry *ret;
1603 ret = (struct elfNN_ia64_link_hash_entry *) entry;
1605 /* Allocate the structure if it has not already been allocated by a
1606 subclass. */
1607 if (!ret)
1608 ret = bfd_hash_allocate (table, sizeof (*ret));
1610 if (!ret)
1611 return 0;
1613 /* Initialize our local data. All zeros, and definitely easier
1614 than setting a handful of bit fields. */
1615 memset (ret, 0, sizeof (*ret));
1617 /* Call the allocation method of the superclass. */
1618 ret = ((struct elfNN_ia64_link_hash_entry *)
1619 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1620 table, string));
1622 return (struct bfd_hash_entry *) ret;
1625 static void
1626 elfNN_ia64_hash_copy_indirect (bed, xdir, xind)
1627 struct elf_backend_data *bed ATTRIBUTE_UNUSED;
1628 struct elf_link_hash_entry *xdir, *xind;
1630 struct elfNN_ia64_link_hash_entry *dir, *ind;
1632 dir = (struct elfNN_ia64_link_hash_entry *) xdir;
1633 ind = (struct elfNN_ia64_link_hash_entry *) xind;
1635 /* Copy down any references that we may have already seen to the
1636 symbol which just became indirect. */
1638 dir->root.elf_link_hash_flags |=
1639 (ind->root.elf_link_hash_flags
1640 & (ELF_LINK_HASH_REF_DYNAMIC
1641 | ELF_LINK_HASH_REF_REGULAR
1642 | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
1644 if (ind->root.root.type != bfd_link_hash_indirect)
1645 return;
1647 /* Copy over the got and plt data. This would have been done
1648 by check_relocs. */
1650 if (dir->info == NULL)
1652 struct elfNN_ia64_dyn_sym_info *dyn_i;
1654 dir->info = dyn_i = ind->info;
1655 ind->info = NULL;
1657 /* Fix up the dyn_sym_info pointers to the global symbol. */
1658 for (; dyn_i; dyn_i = dyn_i->next)
1659 dyn_i->h = &dir->root;
1661 BFD_ASSERT (ind->info == NULL);
1663 /* Copy over the dynindx. */
1665 if (dir->root.dynindx == -1)
1667 dir->root.dynindx = ind->root.dynindx;
1668 dir->root.dynstr_index = ind->root.dynstr_index;
1669 ind->root.dynindx = -1;
1670 ind->root.dynstr_index = 0;
1672 BFD_ASSERT (ind->root.dynindx == -1);
1675 static void
1676 elfNN_ia64_hash_hide_symbol (info, xh, force_local)
1677 struct bfd_link_info *info;
1678 struct elf_link_hash_entry *xh;
1679 bfd_boolean force_local;
1681 struct elfNN_ia64_link_hash_entry *h;
1682 struct elfNN_ia64_dyn_sym_info *dyn_i;
1684 h = (struct elfNN_ia64_link_hash_entry *)xh;
1686 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
1688 for (dyn_i = h->info; dyn_i; dyn_i = dyn_i->next)
1689 dyn_i->want_plt2 = 0;
1692 /* Create the derived linker hash table. The IA-64 ELF port uses this
1693 derived hash table to keep information specific to the IA-64 ElF
1694 linker (without using static variables). */
1696 static struct bfd_link_hash_table*
1697 elfNN_ia64_hash_table_create (abfd)
1698 bfd *abfd;
1700 struct elfNN_ia64_link_hash_table *ret;
1702 ret = bfd_zalloc (abfd, (bfd_size_type) sizeof (*ret));
1703 if (!ret)
1704 return 0;
1705 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
1706 elfNN_ia64_new_elf_hash_entry))
1708 bfd_release (abfd, ret);
1709 return 0;
1712 if (!elfNN_ia64_local_hash_table_init (&ret->loc_hash_table, abfd,
1713 elfNN_ia64_new_loc_hash_entry))
1714 return 0;
1715 return &ret->root.root;
1718 /* Look up an entry in a Alpha ELF linker hash table. */
1720 static INLINE struct elfNN_ia64_local_hash_entry *
1721 elfNN_ia64_local_hash_lookup(table, string, create, copy)
1722 struct elfNN_ia64_local_hash_table *table;
1723 const char *string;
1724 bfd_boolean create, copy;
1726 return ((struct elfNN_ia64_local_hash_entry *)
1727 bfd_hash_lookup (&table->root, string, create, copy));
1730 /* Traverse both local and global hash tables. */
1732 struct elfNN_ia64_dyn_sym_traverse_data
1734 bfd_boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
1735 PTR data;
1738 static bfd_boolean
1739 elfNN_ia64_global_dyn_sym_thunk (xentry, xdata)
1740 struct bfd_hash_entry *xentry;
1741 PTR xdata;
1743 struct elfNN_ia64_link_hash_entry *entry
1744 = (struct elfNN_ia64_link_hash_entry *) xentry;
1745 struct elfNN_ia64_dyn_sym_traverse_data *data
1746 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1747 struct elfNN_ia64_dyn_sym_info *dyn_i;
1749 if (entry->root.root.type == bfd_link_hash_warning)
1750 entry = (struct elfNN_ia64_link_hash_entry *) entry->root.root.u.i.link;
1752 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1753 if (! (*data->func) (dyn_i, data->data))
1754 return FALSE;
1755 return TRUE;
1758 static bfd_boolean
1759 elfNN_ia64_local_dyn_sym_thunk (xentry, xdata)
1760 struct bfd_hash_entry *xentry;
1761 PTR xdata;
1763 struct elfNN_ia64_local_hash_entry *entry
1764 = (struct elfNN_ia64_local_hash_entry *) xentry;
1765 struct elfNN_ia64_dyn_sym_traverse_data *data
1766 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1767 struct elfNN_ia64_dyn_sym_info *dyn_i;
1769 for (dyn_i = entry->info; dyn_i; dyn_i = dyn_i->next)
1770 if (! (*data->func) (dyn_i, data->data))
1771 return FALSE;
1772 return TRUE;
1775 static void
1776 elfNN_ia64_dyn_sym_traverse (ia64_info, func, data)
1777 struct elfNN_ia64_link_hash_table *ia64_info;
1778 bfd_boolean (*func) PARAMS ((struct elfNN_ia64_dyn_sym_info *, PTR));
1779 PTR data;
1781 struct elfNN_ia64_dyn_sym_traverse_data xdata;
1783 xdata.func = func;
1784 xdata.data = data;
1786 elf_link_hash_traverse (&ia64_info->root,
1787 elfNN_ia64_global_dyn_sym_thunk, &xdata);
1788 bfd_hash_traverse (&ia64_info->loc_hash_table.root,
1789 elfNN_ia64_local_dyn_sym_thunk, &xdata);
1792 static bfd_boolean
1793 elfNN_ia64_create_dynamic_sections (abfd, info)
1794 bfd *abfd;
1795 struct bfd_link_info *info;
1797 struct elfNN_ia64_link_hash_table *ia64_info;
1798 asection *s;
1800 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1801 return FALSE;
1803 ia64_info = elfNN_ia64_hash_table (info);
1805 ia64_info->plt_sec = bfd_get_section_by_name (abfd, ".plt");
1806 ia64_info->got_sec = bfd_get_section_by_name (abfd, ".got");
1809 flagword flags = bfd_get_section_flags (abfd, ia64_info->got_sec);
1810 bfd_set_section_flags (abfd, ia64_info->got_sec, SEC_SMALL_DATA | flags);
1813 if (!get_pltoff (abfd, info, ia64_info))
1814 return FALSE;
1816 s = bfd_make_section(abfd, ".rela.IA_64.pltoff");
1817 if (s == NULL
1818 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1819 | SEC_HAS_CONTENTS
1820 | SEC_IN_MEMORY
1821 | SEC_LINKER_CREATED
1822 | SEC_READONLY))
1823 || !bfd_set_section_alignment (abfd, s, 3))
1824 return FALSE;
1825 ia64_info->rel_pltoff_sec = s;
1827 s = bfd_make_section(abfd, ".rela.got");
1828 if (s == NULL
1829 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1830 | SEC_HAS_CONTENTS
1831 | SEC_IN_MEMORY
1832 | SEC_LINKER_CREATED
1833 | SEC_READONLY))
1834 || !bfd_set_section_alignment (abfd, s, 3))
1835 return FALSE;
1836 ia64_info->rel_got_sec = s;
1838 return TRUE;
1841 /* Find and/or create a hash entry for local symbol. */
1842 static struct elfNN_ia64_local_hash_entry *
1843 get_local_sym_hash (ia64_info, abfd, rel, create)
1844 struct elfNN_ia64_link_hash_table *ia64_info;
1845 bfd *abfd;
1846 const Elf_Internal_Rela *rel;
1847 bfd_boolean create;
1849 struct elfNN_ia64_local_hash_entry *ret;
1850 asection *sec = abfd->sections;
1851 char addr_name [34];
1853 BFD_ASSERT ((sizeof (sec->id)*2 + 1 + sizeof (unsigned long)*2 + 1) <= 34);
1854 BFD_ASSERT (sec);
1856 /* Construct a string for use in the elfNN_ia64_local_hash_table.
1857 name describes what was once anonymous memory. */
1859 sprintf (addr_name, "%x:%lx",
1860 sec->id, (unsigned long) ELFNN_R_SYM (rel->r_info));
1862 /* Collect the canonical entry data for this address. */
1863 ret = elfNN_ia64_local_hash_lookup (&ia64_info->loc_hash_table,
1864 addr_name, create, create);
1865 return ret;
1868 /* Find and/or create a descriptor for dynamic symbol info. This will
1869 vary based on global or local symbol, and the addend to the reloc. */
1871 static struct elfNN_ia64_dyn_sym_info *
1872 get_dyn_sym_info (ia64_info, h, abfd, rel, create)
1873 struct elfNN_ia64_link_hash_table *ia64_info;
1874 struct elf_link_hash_entry *h;
1875 bfd *abfd;
1876 const Elf_Internal_Rela *rel;
1877 bfd_boolean create;
1879 struct elfNN_ia64_dyn_sym_info **pp;
1880 struct elfNN_ia64_dyn_sym_info *dyn_i;
1881 bfd_vma addend = rel ? rel->r_addend : 0;
1883 if (h)
1884 pp = &((struct elfNN_ia64_link_hash_entry *)h)->info;
1885 else
1887 struct elfNN_ia64_local_hash_entry *loc_h;
1889 loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
1890 BFD_ASSERT (loc_h);
1892 pp = &loc_h->info;
1895 for (dyn_i = *pp; dyn_i && dyn_i->addend != addend; dyn_i = *pp)
1896 pp = &dyn_i->next;
1898 if (dyn_i == NULL && create)
1900 dyn_i = ((struct elfNN_ia64_dyn_sym_info *)
1901 bfd_zalloc (abfd, (bfd_size_type) sizeof *dyn_i));
1902 *pp = dyn_i;
1903 dyn_i->addend = addend;
1906 return dyn_i;
1909 static asection *
1910 get_got (abfd, info, ia64_info)
1911 bfd *abfd;
1912 struct bfd_link_info *info;
1913 struct elfNN_ia64_link_hash_table *ia64_info;
1915 asection *got;
1916 bfd *dynobj;
1918 got = ia64_info->got_sec;
1919 if (!got)
1921 flagword flags;
1923 dynobj = ia64_info->root.dynobj;
1924 if (!dynobj)
1925 ia64_info->root.dynobj = dynobj = abfd;
1926 if (!_bfd_elf_create_got_section (dynobj, info))
1927 return 0;
1929 got = bfd_get_section_by_name (dynobj, ".got");
1930 BFD_ASSERT (got);
1931 ia64_info->got_sec = got;
1933 flags = bfd_get_section_flags (abfd, got);
1934 bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags);
1937 return got;
1940 /* Create function descriptor section (.opd). This section is called .opd
1941 because it contains "official prodecure descriptors". The "official"
1942 refers to the fact that these descriptors are used when taking the address
1943 of a procedure, thus ensuring a unique address for each procedure. */
1945 static asection *
1946 get_fptr (abfd, info, ia64_info)
1947 bfd *abfd;
1948 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1949 struct elfNN_ia64_link_hash_table *ia64_info;
1951 asection *fptr;
1952 bfd *dynobj;
1954 fptr = ia64_info->fptr_sec;
1955 if (!fptr)
1957 dynobj = ia64_info->root.dynobj;
1958 if (!dynobj)
1959 ia64_info->root.dynobj = dynobj = abfd;
1961 fptr = bfd_make_section (dynobj, ".opd");
1962 if (!fptr
1963 || !bfd_set_section_flags (dynobj, fptr,
1964 (SEC_ALLOC
1965 | SEC_LOAD
1966 | SEC_HAS_CONTENTS
1967 | SEC_IN_MEMORY
1968 | SEC_READONLY
1969 | SEC_LINKER_CREATED))
1970 || !bfd_set_section_alignment (abfd, fptr, 4))
1972 BFD_ASSERT (0);
1973 return NULL;
1976 ia64_info->fptr_sec = fptr;
1979 return fptr;
1982 static asection *
1983 get_pltoff (abfd, info, ia64_info)
1984 bfd *abfd;
1985 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1986 struct elfNN_ia64_link_hash_table *ia64_info;
1988 asection *pltoff;
1989 bfd *dynobj;
1991 pltoff = ia64_info->pltoff_sec;
1992 if (!pltoff)
1994 dynobj = ia64_info->root.dynobj;
1995 if (!dynobj)
1996 ia64_info->root.dynobj = dynobj = abfd;
1998 pltoff = bfd_make_section (dynobj, ELF_STRING_ia64_pltoff);
1999 if (!pltoff
2000 || !bfd_set_section_flags (dynobj, pltoff,
2001 (SEC_ALLOC
2002 | SEC_LOAD
2003 | SEC_HAS_CONTENTS
2004 | SEC_IN_MEMORY
2005 | SEC_SMALL_DATA
2006 | SEC_LINKER_CREATED))
2007 || !bfd_set_section_alignment (abfd, pltoff, 4))
2009 BFD_ASSERT (0);
2010 return NULL;
2013 ia64_info->pltoff_sec = pltoff;
2016 return pltoff;
2019 static asection *
2020 get_reloc_section (abfd, ia64_info, sec, create)
2021 bfd *abfd;
2022 struct elfNN_ia64_link_hash_table *ia64_info;
2023 asection *sec;
2024 bfd_boolean create;
2026 const char *srel_name;
2027 asection *srel;
2028 bfd *dynobj;
2030 srel_name = (bfd_elf_string_from_elf_section
2031 (abfd, elf_elfheader(abfd)->e_shstrndx,
2032 elf_section_data(sec)->rel_hdr.sh_name));
2033 if (srel_name == NULL)
2034 return NULL;
2036 BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
2037 && strcmp (bfd_get_section_name (abfd, sec),
2038 srel_name+5) == 0)
2039 || (strncmp (srel_name, ".rel", 4) == 0
2040 && strcmp (bfd_get_section_name (abfd, sec),
2041 srel_name+4) == 0));
2043 dynobj = ia64_info->root.dynobj;
2044 if (!dynobj)
2045 ia64_info->root.dynobj = dynobj = abfd;
2047 srel = bfd_get_section_by_name (dynobj, srel_name);
2048 if (srel == NULL && create)
2050 srel = bfd_make_section (dynobj, srel_name);
2051 if (srel == NULL
2052 || !bfd_set_section_flags (dynobj, srel,
2053 (SEC_ALLOC
2054 | SEC_LOAD
2055 | SEC_HAS_CONTENTS
2056 | SEC_IN_MEMORY
2057 | SEC_LINKER_CREATED
2058 | SEC_READONLY))
2059 || !bfd_set_section_alignment (dynobj, srel, 3))
2060 return NULL;
2063 if (sec->flags & SEC_READONLY)
2064 ia64_info->reltext = 1;
2066 return srel;
2069 static bfd_boolean
2070 count_dyn_reloc (abfd, dyn_i, srel, type)
2071 bfd *abfd;
2072 struct elfNN_ia64_dyn_sym_info *dyn_i;
2073 asection *srel;
2074 int type;
2076 struct elfNN_ia64_dyn_reloc_entry *rent;
2078 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2079 if (rent->srel == srel && rent->type == type)
2080 break;
2082 if (!rent)
2084 rent = ((struct elfNN_ia64_dyn_reloc_entry *)
2085 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
2086 if (!rent)
2087 return FALSE;
2089 rent->next = dyn_i->reloc_entries;
2090 rent->srel = srel;
2091 rent->type = type;
2092 rent->count = 0;
2093 dyn_i->reloc_entries = rent;
2095 rent->count++;
2097 return TRUE;
2100 static bfd_boolean
2101 elfNN_ia64_check_relocs (abfd, info, sec, relocs)
2102 bfd *abfd;
2103 struct bfd_link_info *info;
2104 asection *sec;
2105 const Elf_Internal_Rela *relocs;
2107 struct elfNN_ia64_link_hash_table *ia64_info;
2108 const Elf_Internal_Rela *relend;
2109 Elf_Internal_Shdr *symtab_hdr;
2110 const Elf_Internal_Rela *rel;
2111 asection *got, *fptr, *srel;
2113 if (info->relocateable)
2114 return TRUE;
2116 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2117 ia64_info = elfNN_ia64_hash_table (info);
2119 got = fptr = srel = NULL;
2121 relend = relocs + sec->reloc_count;
2122 for (rel = relocs; rel < relend; ++rel)
2124 enum {
2125 NEED_GOT = 1,
2126 NEED_FPTR = 2,
2127 NEED_PLTOFF = 4,
2128 NEED_MIN_PLT = 8,
2129 NEED_FULL_PLT = 16,
2130 NEED_DYNREL = 32,
2131 NEED_LTOFF_FPTR = 64,
2132 NEED_TPREL = 128,
2133 NEED_DTPMOD = 256,
2134 NEED_DTPREL = 512
2137 struct elf_link_hash_entry *h = NULL;
2138 unsigned long r_symndx = ELFNN_R_SYM (rel->r_info);
2139 struct elfNN_ia64_dyn_sym_info *dyn_i;
2140 int need_entry;
2141 bfd_boolean maybe_dynamic;
2142 int dynrel_type = R_IA64_NONE;
2144 if (r_symndx >= symtab_hdr->sh_info)
2146 /* We're dealing with a global symbol -- find its hash entry
2147 and mark it as being referenced. */
2148 long indx = r_symndx - symtab_hdr->sh_info;
2149 h = elf_sym_hashes (abfd)[indx];
2150 while (h->root.type == bfd_link_hash_indirect
2151 || h->root.type == bfd_link_hash_warning)
2152 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2154 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2157 /* We can only get preliminary data on whether a symbol is
2158 locally or externally defined, as not all of the input files
2159 have yet been processed. Do something with what we know, as
2160 this may help reduce memory usage and processing time later. */
2161 maybe_dynamic = FALSE;
2162 if (h && ((info->shared
2163 && (!info->symbolic || info->allow_shlib_undefined))
2164 || ! (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
2165 || h->root.type == bfd_link_hash_defweak
2166 || elfNN_ia64_aix_vec (abfd->xvec)))
2167 maybe_dynamic = TRUE;
2169 need_entry = 0;
2170 switch (ELFNN_R_TYPE (rel->r_info))
2172 case R_IA64_TPREL64MSB:
2173 case R_IA64_TPREL64LSB:
2174 if (info->shared || maybe_dynamic)
2175 need_entry = NEED_DYNREL;
2176 dynrel_type = R_IA64_TPREL64LSB;
2177 if (info->shared)
2178 info->flags |= DF_STATIC_TLS;
2179 break;
2181 case R_IA64_LTOFF_TPREL22:
2182 need_entry = NEED_TPREL;
2183 if (info->shared)
2184 info->flags |= DF_STATIC_TLS;
2185 break;
2187 case R_IA64_DTPREL64MSB:
2188 case R_IA64_DTPREL64LSB:
2189 if (info->shared || maybe_dynamic)
2190 need_entry = NEED_DYNREL;
2191 dynrel_type = R_IA64_DTPREL64LSB;
2192 break;
2194 case R_IA64_LTOFF_DTPREL22:
2195 need_entry = NEED_DTPREL;
2196 break;
2198 case R_IA64_DTPMOD64MSB:
2199 case R_IA64_DTPMOD64LSB:
2200 if (info->shared || maybe_dynamic)
2201 need_entry = NEED_DYNREL;
2202 dynrel_type = R_IA64_DTPMOD64LSB;
2203 break;
2205 case R_IA64_LTOFF_DTPMOD22:
2206 need_entry = NEED_DTPMOD;
2207 break;
2209 case R_IA64_LTOFF_FPTR22:
2210 case R_IA64_LTOFF_FPTR64I:
2211 case R_IA64_LTOFF_FPTR32MSB:
2212 case R_IA64_LTOFF_FPTR32LSB:
2213 case R_IA64_LTOFF_FPTR64MSB:
2214 case R_IA64_LTOFF_FPTR64LSB:
2215 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2216 break;
2218 case R_IA64_FPTR64I:
2219 case R_IA64_FPTR32MSB:
2220 case R_IA64_FPTR32LSB:
2221 case R_IA64_FPTR64MSB:
2222 case R_IA64_FPTR64LSB:
2223 if (info->shared || h || elfNN_ia64_aix_vec (abfd->xvec))
2224 need_entry = NEED_FPTR | NEED_DYNREL;
2225 else
2226 need_entry = NEED_FPTR;
2227 dynrel_type = R_IA64_FPTR64LSB;
2228 break;
2230 case R_IA64_LTOFF22:
2231 case R_IA64_LTOFF22X:
2232 case R_IA64_LTOFF64I:
2233 need_entry = NEED_GOT;
2234 break;
2236 case R_IA64_PLTOFF22:
2237 case R_IA64_PLTOFF64I:
2238 case R_IA64_PLTOFF64MSB:
2239 case R_IA64_PLTOFF64LSB:
2240 need_entry = NEED_PLTOFF;
2241 if (h)
2243 if (maybe_dynamic)
2244 need_entry |= NEED_MIN_PLT;
2246 else
2248 (*info->callbacks->warning)
2249 (info, _("@pltoff reloc against local symbol"), 0,
2250 abfd, 0, (bfd_vma) 0);
2252 break;
2254 case R_IA64_PCREL21B:
2255 case R_IA64_PCREL60B:
2256 /* Depending on where this symbol is defined, we may or may not
2257 need a full plt entry. Only skip if we know we'll not need
2258 the entry -- static or symbolic, and the symbol definition
2259 has already been seen. */
2260 if (maybe_dynamic && rel->r_addend == 0)
2261 need_entry = NEED_FULL_PLT;
2262 break;
2264 case R_IA64_IMM14:
2265 case R_IA64_IMM22:
2266 case R_IA64_IMM64:
2267 case R_IA64_DIR32MSB:
2268 case R_IA64_DIR32LSB:
2269 case R_IA64_DIR64MSB:
2270 case R_IA64_DIR64LSB:
2271 /* Shared objects will always need at least a REL relocation. */
2272 if (info->shared || maybe_dynamic
2273 || (elfNN_ia64_aix_vec (abfd->xvec)
2274 && (!h || strcmp (h->root.root.string,
2275 "__GLOB_DATA_PTR") != 0)))
2276 need_entry = NEED_DYNREL;
2277 dynrel_type = R_IA64_DIR64LSB;
2278 break;
2280 case R_IA64_IPLTMSB:
2281 case R_IA64_IPLTLSB:
2282 /* Shared objects will always need at least a REL relocation. */
2283 if (info->shared || maybe_dynamic)
2284 need_entry = NEED_DYNREL;
2285 dynrel_type = R_IA64_IPLTLSB;
2286 break;
2288 case R_IA64_PCREL22:
2289 case R_IA64_PCREL64I:
2290 case R_IA64_PCREL32MSB:
2291 case R_IA64_PCREL32LSB:
2292 case R_IA64_PCREL64MSB:
2293 case R_IA64_PCREL64LSB:
2294 if (maybe_dynamic)
2295 need_entry = NEED_DYNREL;
2296 dynrel_type = R_IA64_PCREL64LSB;
2297 break;
2300 if (!need_entry)
2301 continue;
2303 if ((need_entry & NEED_FPTR) != 0
2304 && rel->r_addend)
2306 (*info->callbacks->warning)
2307 (info, _("non-zero addend in @fptr reloc"), 0,
2308 abfd, 0, (bfd_vma) 0);
2311 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, TRUE);
2313 /* Record whether or not this is a local symbol. */
2314 dyn_i->h = h;
2316 /* Create what's needed. */
2317 if (need_entry & (NEED_GOT | NEED_TPREL | NEED_DTPMOD | NEED_DTPREL))
2319 if (!got)
2321 got = get_got (abfd, info, ia64_info);
2322 if (!got)
2323 return FALSE;
2325 if (need_entry & NEED_GOT)
2326 dyn_i->want_got = 1;
2327 if (need_entry & NEED_TPREL)
2328 dyn_i->want_tprel = 1;
2329 if (need_entry & NEED_DTPMOD)
2330 dyn_i->want_dtpmod = 1;
2331 if (need_entry & NEED_DTPREL)
2332 dyn_i->want_dtprel = 1;
2334 if (need_entry & NEED_FPTR)
2336 if (!fptr)
2338 fptr = get_fptr (abfd, info, ia64_info);
2339 if (!fptr)
2340 return FALSE;
2343 /* FPTRs for shared libraries are allocated by the dynamic
2344 linker. Make sure this local symbol will appear in the
2345 dynamic symbol table. */
2346 if (!h && (info->shared
2347 /* AIX also needs one */
2348 || elfNN_ia64_aix_vec (abfd->xvec)))
2350 if (! (_bfd_elfNN_link_record_local_dynamic_symbol
2351 (info, abfd, (long) r_symndx)))
2352 return FALSE;
2355 dyn_i->want_fptr = 1;
2357 if (need_entry & NEED_LTOFF_FPTR)
2358 dyn_i->want_ltoff_fptr = 1;
2359 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
2361 if (!ia64_info->root.dynobj)
2362 ia64_info->root.dynobj = abfd;
2363 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2364 dyn_i->want_plt = 1;
2366 if (need_entry & NEED_FULL_PLT)
2367 dyn_i->want_plt2 = 1;
2368 if (need_entry & NEED_PLTOFF)
2369 dyn_i->want_pltoff = 1;
2370 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
2372 if (!srel)
2374 srel = get_reloc_section (abfd, ia64_info, sec, TRUE);
2375 if (!srel)
2376 return FALSE;
2378 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
2379 return FALSE;
2383 return TRUE;
2386 struct elfNN_ia64_allocate_data
2388 struct bfd_link_info *info;
2389 bfd_size_type ofs;
2392 /* For cleanliness, and potentially faster dynamic loading, allocate
2393 external GOT entries first. */
2395 static bfd_boolean
2396 allocate_global_data_got (dyn_i, data)
2397 struct elfNN_ia64_dyn_sym_info *dyn_i;
2398 PTR data;
2400 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2402 if (dyn_i->want_got
2403 && ! dyn_i->want_fptr
2404 && (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2405 || (elfNN_ia64_aix_vec (x->info->hash->creator)
2406 && (!dyn_i->h || strcmp (dyn_i->h->root.root.string,
2407 "__GLOB_DATA_PTR") != 0))))
2409 dyn_i->got_offset = x->ofs;
2410 x->ofs += 8;
2412 if (dyn_i->want_tprel)
2414 dyn_i->tprel_offset = x->ofs;
2415 x->ofs += 8;
2417 if (dyn_i->want_dtpmod)
2419 dyn_i->dtpmod_offset = x->ofs;
2420 x->ofs += 8;
2422 if (dyn_i->want_dtprel)
2424 dyn_i->dtprel_offset = x->ofs;
2425 x->ofs += 8;
2427 return TRUE;
2430 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
2432 static bfd_boolean
2433 allocate_global_fptr_got (dyn_i, data)
2434 struct elfNN_ia64_dyn_sym_info *dyn_i;
2435 PTR data;
2437 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2439 if (dyn_i->want_got
2440 && dyn_i->want_fptr
2441 && (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2442 || elfNN_ia64_aix_vec (x->info->hash->creator)))
2444 dyn_i->got_offset = x->ofs;
2445 x->ofs += 8;
2447 return TRUE;
2450 /* Lastly, allocate all the GOT entries for local data. */
2452 static bfd_boolean
2453 allocate_local_got (dyn_i, data)
2454 struct elfNN_ia64_dyn_sym_info *dyn_i;
2455 PTR data;
2457 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2459 if (dyn_i->want_got
2460 && ! (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2461 || elfNN_ia64_aix_vec (x->info->hash->creator)))
2463 dyn_i->got_offset = x->ofs;
2464 x->ofs += 8;
2466 return TRUE;
2469 /* Search for the index of a global symbol in it's defining object file. */
2471 static long
2472 global_sym_index (h)
2473 struct elf_link_hash_entry *h;
2475 struct elf_link_hash_entry **p;
2476 bfd *obj;
2478 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2479 || h->root.type == bfd_link_hash_defweak);
2481 obj = h->root.u.def.section->owner;
2482 for (p = elf_sym_hashes (obj); *p != h; ++p)
2483 continue;
2485 return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
2488 /* Allocate function descriptors. We can do these for every function
2489 in a main executable that is not exported. */
2491 static bfd_boolean
2492 allocate_fptr (dyn_i, data)
2493 struct elfNN_ia64_dyn_sym_info *dyn_i;
2494 PTR data;
2496 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2498 if (dyn_i->want_fptr)
2500 struct elf_link_hash_entry *h = dyn_i->h;
2502 if (h)
2503 while (h->root.type == bfd_link_hash_indirect
2504 || h->root.type == bfd_link_hash_warning)
2505 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2507 if (x->info->shared
2508 /* AIX needs an FPTR in this case. */
2509 || (elfNN_ia64_aix_vec (x->info->hash->creator)
2510 && (!h
2511 || h->root.type == bfd_link_hash_defined
2512 || h->root.type == bfd_link_hash_defweak)))
2514 if (h && h->dynindx == -1)
2516 BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
2517 || (h->root.type == bfd_link_hash_defweak));
2519 if (!_bfd_elfNN_link_record_local_dynamic_symbol
2520 (x->info, h->root.u.def.section->owner,
2521 global_sym_index (h)))
2522 return FALSE;
2525 dyn_i->want_fptr = 0;
2527 else if (h == NULL || h->dynindx == -1)
2529 dyn_i->fptr_offset = x->ofs;
2530 x->ofs += 16;
2532 else
2533 dyn_i->want_fptr = 0;
2535 return TRUE;
2538 /* Allocate all the minimal PLT entries. */
2540 static bfd_boolean
2541 allocate_plt_entries (dyn_i, data)
2542 struct elfNN_ia64_dyn_sym_info *dyn_i;
2543 PTR data;
2545 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2547 if (dyn_i->want_plt)
2549 struct elf_link_hash_entry *h = dyn_i->h;
2551 if (h)
2552 while (h->root.type == bfd_link_hash_indirect
2553 || h->root.type == bfd_link_hash_warning)
2554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2556 /* ??? Versioned symbols seem to lose ELF_LINK_HASH_NEEDS_PLT. */
2557 if (elfNN_ia64_dynamic_symbol_p (h, x->info))
2559 bfd_size_type offset = x->ofs;
2560 if (offset == 0)
2561 offset = PLT_HEADER_SIZE;
2562 dyn_i->plt_offset = offset;
2563 x->ofs = offset + PLT_MIN_ENTRY_SIZE;
2565 dyn_i->want_pltoff = 1;
2567 else
2569 dyn_i->want_plt = 0;
2570 dyn_i->want_plt2 = 0;
2573 return TRUE;
2576 /* Allocate all the full PLT entries. */
2578 static bfd_boolean
2579 allocate_plt2_entries (dyn_i, data)
2580 struct elfNN_ia64_dyn_sym_info *dyn_i;
2581 PTR data;
2583 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2585 if (dyn_i->want_plt2)
2587 struct elf_link_hash_entry *h = dyn_i->h;
2588 bfd_size_type ofs = x->ofs;
2590 dyn_i->plt2_offset = ofs;
2591 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2593 while (h->root.type == bfd_link_hash_indirect
2594 || h->root.type == bfd_link_hash_warning)
2595 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2596 dyn_i->h->plt.offset = ofs;
2598 return TRUE;
2601 /* Allocate all the PLTOFF entries requested by relocations and
2602 plt entries. We can't share space with allocated FPTR entries,
2603 because the latter are not necessarily addressable by the GP.
2604 ??? Relaxation might be able to determine that they are. */
2606 static bfd_boolean
2607 allocate_pltoff_entries (dyn_i, data)
2608 struct elfNN_ia64_dyn_sym_info *dyn_i;
2609 PTR data;
2611 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2613 if (dyn_i->want_pltoff)
2615 dyn_i->pltoff_offset = x->ofs;
2616 x->ofs += 16;
2618 return TRUE;
2621 /* Allocate dynamic relocations for those symbols that turned out
2622 to be dynamic. */
2624 static bfd_boolean
2625 allocate_dynrel_entries (dyn_i, data)
2626 struct elfNN_ia64_dyn_sym_info *dyn_i;
2627 PTR data;
2629 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
2630 struct elfNN_ia64_link_hash_table *ia64_info;
2631 struct elfNN_ia64_dyn_reloc_entry *rent;
2632 bfd_boolean dynamic_symbol, shared;
2634 ia64_info = elfNN_ia64_hash_table (x->info);
2635 dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info)
2636 || (elfNN_ia64_aix_vec (x->info->hash->creator)
2637 /* Don't allocate an entry for __GLOB_DATA_PTR */
2638 && (!dyn_i->h || strcmp (dyn_i->h->root.root.string,
2639 "__GLOB_DATA_PTR") != 0));
2640 shared = x->info->shared;
2642 /* Take care of the normal data relocations. */
2644 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2646 int count = rent->count;
2648 switch (rent->type)
2650 case R_IA64_FPTR64LSB:
2651 /* Allocate one iff !want_fptr, which by this point will
2652 be true only if we're actually allocating one statically
2653 in the main executable. */
2654 if (dyn_i->want_fptr)
2655 continue;
2656 break;
2657 case R_IA64_PCREL64LSB:
2658 if (!dynamic_symbol)
2659 continue;
2660 break;
2661 case R_IA64_DIR64LSB:
2662 if (!dynamic_symbol && !shared)
2663 continue;
2664 break;
2665 case R_IA64_IPLTLSB:
2666 if (!dynamic_symbol && !shared)
2667 continue;
2668 /* Use two REL relocations for IPLT relocations
2669 against local symbols. */
2670 if (!dynamic_symbol)
2671 count *= 2;
2672 break;
2673 case R_IA64_TPREL64LSB:
2674 case R_IA64_DTPREL64LSB:
2675 case R_IA64_DTPMOD64LSB:
2676 break;
2677 default:
2678 abort ();
2680 rent->srel->_raw_size += sizeof (ElfNN_External_Rela) * count;
2683 /* Take care of the GOT and PLT relocations. */
2685 if (((dynamic_symbol || shared) && dyn_i->want_got)
2686 || (dyn_i->want_ltoff_fptr && dyn_i->h && dyn_i->h->dynindx != -1))
2687 ia64_info->rel_got_sec->_raw_size += sizeof (ElfNN_External_Rela);
2688 if ((dynamic_symbol || shared) && dyn_i->want_tprel)
2689 ia64_info->rel_got_sec->_raw_size += sizeof (ElfNN_External_Rela);
2690 if ((dynamic_symbol || shared) && dyn_i->want_dtpmod)
2691 ia64_info->rel_got_sec->_raw_size += sizeof (ElfNN_External_Rela);
2692 if (dynamic_symbol && dyn_i->want_dtprel)
2693 ia64_info->rel_got_sec->_raw_size += sizeof (ElfNN_External_Rela);
2695 if (dyn_i->want_pltoff)
2697 bfd_size_type t = 0;
2699 /* Dynamic symbols get one IPLT relocation. Local symbols in
2700 shared libraries get two REL relocations. Local symbols in
2701 main applications get nothing. */
2702 if (dynamic_symbol)
2703 t = sizeof (ElfNN_External_Rela);
2704 else if (shared)
2705 t = 2 * sizeof (ElfNN_External_Rela);
2707 ia64_info->rel_pltoff_sec->_raw_size += t;
2710 return TRUE;
2713 static bfd_boolean
2714 elfNN_ia64_adjust_dynamic_symbol (info, h)
2715 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2716 struct elf_link_hash_entry *h;
2718 /* ??? Undefined symbols with PLT entries should be re-defined
2719 to be the PLT entry. */
2721 /* If this is a weak symbol, and there is a real definition, the
2722 processor independent code will have arranged for us to see the
2723 real definition first, and we can just use the same value. */
2724 if (h->weakdef != NULL)
2726 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2727 || h->weakdef->root.type == bfd_link_hash_defweak);
2728 h->root.u.def.section = h->weakdef->root.u.def.section;
2729 h->root.u.def.value = h->weakdef->root.u.def.value;
2730 return TRUE;
2733 /* If this is a reference to a symbol defined by a dynamic object which
2734 is not a function, we might allocate the symbol in our .dynbss section
2735 and allocate a COPY dynamic relocation.
2737 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2738 of hackery. */
2740 return TRUE;
2743 static bfd_boolean
2744 elfNN_ia64_size_dynamic_sections (output_bfd, info)
2745 bfd *output_bfd;
2746 struct bfd_link_info *info;
2748 struct elfNN_ia64_allocate_data data;
2749 struct elfNN_ia64_link_hash_table *ia64_info;
2750 asection *sec;
2751 bfd *dynobj;
2752 bfd_boolean relplt = FALSE;
2754 dynobj = elf_hash_table(info)->dynobj;
2755 ia64_info = elfNN_ia64_hash_table (info);
2756 BFD_ASSERT(dynobj != NULL);
2757 data.info = info;
2759 /* Set the contents of the .interp section to the interpreter. */
2760 if (ia64_info->root.dynamic_sections_created
2761 && !info->shared)
2763 sec = bfd_get_section_by_name (dynobj, ".interp");
2764 BFD_ASSERT (sec != NULL);
2765 sec->contents = (bfd_byte *) DYNAMIC_INTERPRETER (output_bfd);
2766 sec->_raw_size = strlen (DYNAMIC_INTERPRETER (output_bfd)) + 1;
2769 /* Allocate the GOT entries. */
2771 if (ia64_info->got_sec)
2773 data.ofs = 0;
2774 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2775 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2776 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
2777 ia64_info->got_sec->_raw_size = data.ofs;
2780 /* Allocate the FPTR entries. */
2782 if (ia64_info->fptr_sec)
2784 data.ofs = 0;
2785 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
2786 ia64_info->fptr_sec->_raw_size = data.ofs;
2789 /* Now that we've seen all of the input files, we can decide which
2790 symbols need plt entries. Allocate the minimal PLT entries first.
2791 We do this even though dynamic_sections_created may be FALSE, because
2792 this has the side-effect of clearing want_plt and want_plt2. */
2794 data.ofs = 0;
2795 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
2797 ia64_info->minplt_entries = 0;
2798 if (data.ofs)
2800 ia64_info->minplt_entries
2801 = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
2804 /* Align the pointer for the plt2 entries. */
2805 data.ofs = (data.ofs + 31) & (bfd_vma) -32;
2807 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
2808 if (data.ofs != 0)
2810 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2812 ia64_info->plt_sec->_raw_size = data.ofs;
2814 /* If we've got a .plt, we need some extra memory for the dynamic
2815 linker. We stuff these in .got.plt. */
2816 sec = bfd_get_section_by_name (dynobj, ".got.plt");
2817 sec->_raw_size = 8 * PLT_RESERVED_WORDS;
2820 /* Allocate the PLTOFF entries. */
2822 if (ia64_info->pltoff_sec)
2824 data.ofs = 0;
2825 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
2826 ia64_info->pltoff_sec->_raw_size = data.ofs;
2829 if (ia64_info->root.dynamic_sections_created)
2831 /* Allocate space for the dynamic relocations that turned out to be
2832 required. */
2834 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
2837 /* We have now determined the sizes of the various dynamic sections.
2838 Allocate memory for them. */
2839 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2841 bfd_boolean strip;
2843 if (!(sec->flags & SEC_LINKER_CREATED))
2844 continue;
2846 /* If we don't need this section, strip it from the output file.
2847 There were several sections primarily related to dynamic
2848 linking that must be create before the linker maps input
2849 sections to output sections. The linker does that before
2850 bfd_elf_size_dynamic_sections is called, and it is that
2851 function which decides whether anything needs to go into
2852 these sections. */
2854 strip = (sec->_raw_size == 0);
2856 if (sec == ia64_info->got_sec)
2857 strip = FALSE;
2858 else if (sec == ia64_info->rel_got_sec)
2860 if (strip)
2861 ia64_info->rel_got_sec = NULL;
2862 else
2863 /* We use the reloc_count field as a counter if we need to
2864 copy relocs into the output file. */
2865 sec->reloc_count = 0;
2867 else if (sec == ia64_info->fptr_sec)
2869 if (strip)
2870 ia64_info->fptr_sec = NULL;
2872 else if (sec == ia64_info->plt_sec)
2874 if (strip)
2875 ia64_info->plt_sec = NULL;
2877 else if (sec == ia64_info->pltoff_sec)
2879 if (strip)
2880 ia64_info->pltoff_sec = NULL;
2882 else if (sec == ia64_info->rel_pltoff_sec)
2884 if (strip)
2885 ia64_info->rel_pltoff_sec = NULL;
2886 else
2888 relplt = TRUE;
2889 /* We use the reloc_count field as a counter if we need to
2890 copy relocs into the output file. */
2891 sec->reloc_count = 0;
2894 else
2896 const char *name;
2898 /* It's OK to base decisions on the section name, because none
2899 of the dynobj section names depend upon the input files. */
2900 name = bfd_get_section_name (dynobj, sec);
2902 if (strcmp (name, ".got.plt") == 0)
2903 strip = FALSE;
2904 else if (strncmp (name, ".rel", 4) == 0)
2906 if (!strip)
2908 /* We use the reloc_count field as a counter if we need to
2909 copy relocs into the output file. */
2910 sec->reloc_count = 0;
2913 else
2914 continue;
2917 if (strip)
2918 _bfd_strip_section_from_output (info, sec);
2919 else
2921 /* Allocate memory for the section contents. */
2922 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->_raw_size);
2923 if (sec->contents == NULL && sec->_raw_size != 0)
2924 return FALSE;
2928 if (elf_hash_table (info)->dynamic_sections_created)
2930 /* Add some entries to the .dynamic section. We fill in the values
2931 later (in finish_dynamic_sections) but we must add the entries now
2932 so that we get the correct size for the .dynamic section. */
2934 if (!info->shared)
2936 /* The DT_DEBUG entry is filled in by the dynamic linker and used
2937 by the debugger. */
2938 #define add_dynamic_entry(TAG, VAL) \
2939 bfd_elfNN_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2941 if (!add_dynamic_entry (DT_DEBUG, 0))
2942 return FALSE;
2945 if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
2946 return FALSE;
2947 if (!add_dynamic_entry (DT_PLTGOT, 0))
2948 return FALSE;
2950 if (relplt)
2952 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2953 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2954 || !add_dynamic_entry (DT_JMPREL, 0))
2955 return FALSE;
2958 if (!add_dynamic_entry (DT_RELA, 0)
2959 || !add_dynamic_entry (DT_RELASZ, 0)
2960 || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
2961 return FALSE;
2963 if (ia64_info->reltext)
2965 if (!add_dynamic_entry (DT_TEXTREL, 0))
2966 return FALSE;
2967 info->flags |= DF_TEXTREL;
2971 /* ??? Perhaps force __gp local. */
2973 return TRUE;
2976 static bfd_reloc_status_type
2977 elfNN_ia64_install_value (abfd, hit_addr, v, r_type)
2978 bfd *abfd;
2979 bfd_byte *hit_addr;
2980 bfd_vma v;
2981 unsigned int r_type;
2983 const struct ia64_operand *op;
2984 int bigendian = 0, shift = 0;
2985 bfd_vma t0, t1, insn, dword;
2986 enum ia64_opnd opnd;
2987 const char *err;
2988 size_t size = 8;
2989 #ifdef BFD_HOST_U_64_BIT
2990 BFD_HOST_U_64_BIT val = (BFD_HOST_U_64_BIT) v;
2991 #else
2992 bfd_vma val = v;
2993 #endif
2995 opnd = IA64_OPND_NIL;
2996 switch (r_type)
2998 case R_IA64_NONE:
2999 case R_IA64_LDXMOV:
3000 return bfd_reloc_ok;
3002 /* Instruction relocations. */
3004 case R_IA64_IMM14:
3005 case R_IA64_TPREL14:
3006 case R_IA64_DTPREL14:
3007 opnd = IA64_OPND_IMM14;
3008 break;
3010 case R_IA64_PCREL21F: opnd = IA64_OPND_TGT25; break;
3011 case R_IA64_PCREL21M: opnd = IA64_OPND_TGT25b; break;
3012 case R_IA64_PCREL60B: opnd = IA64_OPND_TGT64; break;
3013 case R_IA64_PCREL21B:
3014 case R_IA64_PCREL21BI:
3015 opnd = IA64_OPND_TGT25c;
3016 break;
3018 case R_IA64_IMM22:
3019 case R_IA64_GPREL22:
3020 case R_IA64_LTOFF22:
3021 case R_IA64_LTOFF22X:
3022 case R_IA64_PLTOFF22:
3023 case R_IA64_PCREL22:
3024 case R_IA64_LTOFF_FPTR22:
3025 case R_IA64_TPREL22:
3026 case R_IA64_DTPREL22:
3027 case R_IA64_LTOFF_TPREL22:
3028 case R_IA64_LTOFF_DTPMOD22:
3029 case R_IA64_LTOFF_DTPREL22:
3030 opnd = IA64_OPND_IMM22;
3031 break;
3033 case R_IA64_IMM64:
3034 case R_IA64_GPREL64I:
3035 case R_IA64_LTOFF64I:
3036 case R_IA64_PLTOFF64I:
3037 case R_IA64_PCREL64I:
3038 case R_IA64_FPTR64I:
3039 case R_IA64_LTOFF_FPTR64I:
3040 case R_IA64_TPREL64I:
3041 case R_IA64_DTPREL64I:
3042 opnd = IA64_OPND_IMMU64;
3043 break;
3045 /* Data relocations. */
3047 case R_IA64_DIR32MSB:
3048 case R_IA64_GPREL32MSB:
3049 case R_IA64_FPTR32MSB:
3050 case R_IA64_PCREL32MSB:
3051 case R_IA64_LTOFF_FPTR32MSB:
3052 case R_IA64_SEGREL32MSB:
3053 case R_IA64_SECREL32MSB:
3054 case R_IA64_LTV32MSB:
3055 case R_IA64_DTPREL32MSB:
3056 size = 4; bigendian = 1;
3057 break;
3059 case R_IA64_DIR32LSB:
3060 case R_IA64_GPREL32LSB:
3061 case R_IA64_FPTR32LSB:
3062 case R_IA64_PCREL32LSB:
3063 case R_IA64_LTOFF_FPTR32LSB:
3064 case R_IA64_SEGREL32LSB:
3065 case R_IA64_SECREL32LSB:
3066 case R_IA64_LTV32LSB:
3067 case R_IA64_DTPREL32LSB:
3068 size = 4; bigendian = 0;
3069 break;
3071 case R_IA64_DIR64MSB:
3072 case R_IA64_GPREL64MSB:
3073 case R_IA64_PLTOFF64MSB:
3074 case R_IA64_FPTR64MSB:
3075 case R_IA64_PCREL64MSB:
3076 case R_IA64_LTOFF_FPTR64MSB:
3077 case R_IA64_SEGREL64MSB:
3078 case R_IA64_SECREL64MSB:
3079 case R_IA64_LTV64MSB:
3080 case R_IA64_TPREL64MSB:
3081 case R_IA64_DTPMOD64MSB:
3082 case R_IA64_DTPREL64MSB:
3083 size = 8; bigendian = 1;
3084 break;
3086 case R_IA64_DIR64LSB:
3087 case R_IA64_GPREL64LSB:
3088 case R_IA64_PLTOFF64LSB:
3089 case R_IA64_FPTR64LSB:
3090 case R_IA64_PCREL64LSB:
3091 case R_IA64_LTOFF_FPTR64LSB:
3092 case R_IA64_SEGREL64LSB:
3093 case R_IA64_SECREL64LSB:
3094 case R_IA64_LTV64LSB:
3095 case R_IA64_TPREL64LSB:
3096 case R_IA64_DTPMOD64LSB:
3097 case R_IA64_DTPREL64LSB:
3098 size = 8; bigendian = 0;
3099 break;
3101 /* Unsupported / Dynamic relocations. */
3102 default:
3103 return bfd_reloc_notsupported;
3106 switch (opnd)
3108 case IA64_OPND_IMMU64:
3109 hit_addr -= (long) hit_addr & 0x3;
3110 t0 = bfd_get_64 (abfd, hit_addr);
3111 t1 = bfd_get_64 (abfd, hit_addr + 8);
3113 /* tmpl/s: bits 0.. 5 in t0
3114 slot 0: bits 5..45 in t0
3115 slot 1: bits 46..63 in t0, bits 0..22 in t1
3116 slot 2: bits 23..63 in t1 */
3118 /* First, clear the bits that form the 64 bit constant. */
3119 t0 &= ~(0x3ffffLL << 46);
3120 t1 &= ~(0x7fffffLL
3121 | (( (0x07fLL << 13) | (0x1ffLL << 27)
3122 | (0x01fLL << 22) | (0x001LL << 21)
3123 | (0x001LL << 36)) << 23));
3125 t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */
3126 t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */
3127 t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */
3128 | (((val >> 7) & 0x1ff) << 27) /* imm9d */
3129 | (((val >> 16) & 0x01f) << 22) /* imm5c */
3130 | (((val >> 21) & 0x001) << 21) /* ic */
3131 | (((val >> 63) & 0x001) << 36)) << 23; /* i */
3133 bfd_put_64 (abfd, t0, hit_addr);
3134 bfd_put_64 (abfd, t1, hit_addr + 8);
3135 break;
3137 case IA64_OPND_TGT64:
3138 hit_addr -= (long) hit_addr & 0x3;
3139 t0 = bfd_get_64 (abfd, hit_addr);
3140 t1 = bfd_get_64 (abfd, hit_addr + 8);
3142 /* tmpl/s: bits 0.. 5 in t0
3143 slot 0: bits 5..45 in t0
3144 slot 1: bits 46..63 in t0, bits 0..22 in t1
3145 slot 2: bits 23..63 in t1 */
3147 /* First, clear the bits that form the 64 bit constant. */
3148 t0 &= ~(0x3ffffLL << 46);
3149 t1 &= ~(0x7fffffLL
3150 | ((1LL << 36 | 0xfffffLL << 13) << 23));
3152 val >>= 4;
3153 t0 |= ((val >> 20) & 0xffffLL) << 2 << 46; /* 16 lsbs of imm39 */
3154 t1 |= ((val >> 36) & 0x7fffffLL) << 0; /* 23 msbs of imm39 */
3155 t1 |= ((((val >> 0) & 0xfffffLL) << 13) /* imm20b */
3156 | (((val >> 59) & 0x1LL) << 36)) << 23; /* i */
3158 bfd_put_64 (abfd, t0, hit_addr);
3159 bfd_put_64 (abfd, t1, hit_addr + 8);
3160 break;
3162 default:
3163 switch ((long) hit_addr & 0x3)
3165 case 0: shift = 5; break;
3166 case 1: shift = 14; hit_addr += 3; break;
3167 case 2: shift = 23; hit_addr += 6; break;
3168 case 3: return bfd_reloc_notsupported; /* shouldn't happen... */
3170 dword = bfd_get_64 (abfd, hit_addr);
3171 insn = (dword >> shift) & 0x1ffffffffffLL;
3173 op = elf64_ia64_operands + opnd;
3174 err = (*op->insert) (op, val, (ia64_insn *)& insn);
3175 if (err)
3176 return bfd_reloc_overflow;
3178 dword &= ~(0x1ffffffffffLL << shift);
3179 dword |= (insn << shift);
3180 bfd_put_64 (abfd, dword, hit_addr);
3181 break;
3183 case IA64_OPND_NIL:
3184 /* A data relocation. */
3185 if (bigendian)
3186 if (size == 4)
3187 bfd_putb32 (val, hit_addr);
3188 else
3189 bfd_putb64 (val, hit_addr);
3190 else
3191 if (size == 4)
3192 bfd_putl32 (val, hit_addr);
3193 else
3194 bfd_putl64 (val, hit_addr);
3195 break;
3198 return bfd_reloc_ok;
3201 static void
3202 elfNN_ia64_install_dyn_reloc (abfd, info, sec, srel, offset, type,
3203 dynindx, addend)
3204 bfd *abfd;
3205 struct bfd_link_info *info;
3206 asection *sec;
3207 asection *srel;
3208 bfd_vma offset;
3209 unsigned int type;
3210 long dynindx;
3211 bfd_vma addend;
3213 Elf_Internal_Rela outrel;
3214 bfd_byte *loc;
3216 BFD_ASSERT (dynindx != -1);
3217 outrel.r_info = ELFNN_R_INFO (dynindx, type);
3218 outrel.r_addend = addend;
3219 outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
3220 if (outrel.r_offset >= (bfd_vma) -2)
3222 /* Run for the hills. We shouldn't be outputting a relocation
3223 for this. So do what everyone else does and output a no-op. */
3224 outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
3225 outrel.r_addend = 0;
3226 outrel.r_offset = 0;
3228 else
3229 outrel.r_offset += sec->output_section->vma + sec->output_offset;
3231 loc = srel->contents;
3232 loc += srel->reloc_count++ * sizeof (ElfNN_External_Rela);
3233 bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
3234 BFD_ASSERT (sizeof (ElfNN_External_Rela) * srel->reloc_count
3235 <= srel->_cooked_size);
3238 /* Store an entry for target address TARGET_ADDR in the linkage table
3239 and return the gp-relative address of the linkage table entry. */
3241 static bfd_vma
3242 set_got_entry (abfd, info, dyn_i, dynindx, addend, value, dyn_r_type)
3243 bfd *abfd;
3244 struct bfd_link_info *info;
3245 struct elfNN_ia64_dyn_sym_info *dyn_i;
3246 long dynindx;
3247 bfd_vma addend;
3248 bfd_vma value;
3249 unsigned int dyn_r_type;
3251 struct elfNN_ia64_link_hash_table *ia64_info;
3252 asection *got_sec;
3253 bfd_boolean done;
3254 bfd_vma got_offset;
3256 ia64_info = elfNN_ia64_hash_table (info);
3257 got_sec = ia64_info->got_sec;
3259 switch (dyn_r_type)
3261 case R_IA64_TPREL64LSB:
3262 done = dyn_i->tprel_done;
3263 dyn_i->tprel_done = TRUE;
3264 got_offset = dyn_i->tprel_offset;
3265 break;
3266 case R_IA64_DTPMOD64LSB:
3267 done = dyn_i->dtpmod_done;
3268 dyn_i->dtpmod_done = TRUE;
3269 got_offset = dyn_i->dtpmod_offset;
3270 break;
3271 case R_IA64_DTPREL64LSB:
3272 done = dyn_i->dtprel_done;
3273 dyn_i->dtprel_done = TRUE;
3274 got_offset = dyn_i->dtprel_offset;
3275 break;
3276 default:
3277 done = dyn_i->got_done;
3278 dyn_i->got_done = TRUE;
3279 got_offset = dyn_i->got_offset;
3280 break;
3283 BFD_ASSERT ((got_offset & 7) == 0);
3285 if (! done)
3287 /* Store the target address in the linkage table entry. */
3288 bfd_put_64 (abfd, value, got_sec->contents + got_offset);
3290 /* Install a dynamic relocation if needed. */
3291 if ((info->shared && dyn_r_type != R_IA64_DTPREL64LSB)
3292 || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info)
3293 || elfNN_ia64_aix_vec (abfd->xvec)
3294 || (dynindx != -1 && dyn_r_type == R_IA64_FPTR64LSB))
3296 if (dynindx == -1
3297 && dyn_r_type != R_IA64_TPREL64LSB
3298 && dyn_r_type != R_IA64_DTPMOD64LSB
3299 && dyn_r_type != R_IA64_DTPREL64LSB)
3301 dyn_r_type = R_IA64_REL64LSB;
3302 dynindx = 0;
3303 addend = value;
3306 if (bfd_big_endian (abfd))
3308 switch (dyn_r_type)
3310 case R_IA64_REL64LSB:
3311 dyn_r_type = R_IA64_REL64MSB;
3312 break;
3313 case R_IA64_DIR64LSB:
3314 dyn_r_type = R_IA64_DIR64MSB;
3315 break;
3316 case R_IA64_FPTR64LSB:
3317 dyn_r_type = R_IA64_FPTR64MSB;
3318 break;
3319 case R_IA64_TPREL64LSB:
3320 dyn_r_type = R_IA64_TPREL64MSB;
3321 break;
3322 case R_IA64_DTPMOD64LSB:
3323 dyn_r_type = R_IA64_DTPMOD64MSB;
3324 break;
3325 case R_IA64_DTPREL64LSB:
3326 dyn_r_type = R_IA64_DTPREL64MSB;
3327 break;
3328 default:
3329 BFD_ASSERT (FALSE);
3330 break;
3334 elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
3335 ia64_info->rel_got_sec,
3336 got_offset, dyn_r_type,
3337 dynindx, addend);
3341 /* Return the address of the linkage table entry. */
3342 value = (got_sec->output_section->vma
3343 + got_sec->output_offset
3344 + got_offset);
3346 return value;
3349 /* Fill in a function descriptor consisting of the function's code
3350 address and its global pointer. Return the descriptor's address. */
3352 static bfd_vma
3353 set_fptr_entry (abfd, info, dyn_i, value)
3354 bfd *abfd;
3355 struct bfd_link_info *info;
3356 struct elfNN_ia64_dyn_sym_info *dyn_i;
3357 bfd_vma value;
3359 struct elfNN_ia64_link_hash_table *ia64_info;
3360 asection *fptr_sec;
3362 ia64_info = elfNN_ia64_hash_table (info);
3363 fptr_sec = ia64_info->fptr_sec;
3365 if (!dyn_i->fptr_done)
3367 dyn_i->fptr_done = 1;
3369 /* Fill in the function descriptor. */
3370 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
3371 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
3372 fptr_sec->contents + dyn_i->fptr_offset + 8);
3375 /* Return the descriptor's address. */
3376 value = (fptr_sec->output_section->vma
3377 + fptr_sec->output_offset
3378 + dyn_i->fptr_offset);
3380 return value;
3383 /* Fill in a PLTOFF entry consisting of the function's code address
3384 and its global pointer. Return the descriptor's address. */
3386 static bfd_vma
3387 set_pltoff_entry (abfd, info, dyn_i, value, is_plt)
3388 bfd *abfd;
3389 struct bfd_link_info *info;
3390 struct elfNN_ia64_dyn_sym_info *dyn_i;
3391 bfd_vma value;
3392 bfd_boolean is_plt;
3394 struct elfNN_ia64_link_hash_table *ia64_info;
3395 asection *pltoff_sec;
3397 ia64_info = elfNN_ia64_hash_table (info);
3398 pltoff_sec = ia64_info->pltoff_sec;
3400 /* Don't do anything if this symbol uses a real PLT entry. In
3401 that case, we'll fill this in during finish_dynamic_symbol. */
3402 if ((! dyn_i->want_plt || is_plt)
3403 && !dyn_i->pltoff_done)
3405 bfd_vma gp = _bfd_get_gp_value (abfd);
3407 /* Fill in the function descriptor. */
3408 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
3409 bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
3411 /* Install dynamic relocations if needed. */
3412 if (!is_plt && info->shared)
3414 unsigned int dyn_r_type;
3416 if (bfd_big_endian (abfd))
3417 dyn_r_type = R_IA64_REL64MSB;
3418 else
3419 dyn_r_type = R_IA64_REL64LSB;
3421 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
3422 ia64_info->rel_pltoff_sec,
3423 dyn_i->pltoff_offset,
3424 dyn_r_type, 0, value);
3425 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
3426 ia64_info->rel_pltoff_sec,
3427 dyn_i->pltoff_offset + 8,
3428 dyn_r_type, 0, gp);
3431 dyn_i->pltoff_done = 1;
3434 /* Return the descriptor's address. */
3435 value = (pltoff_sec->output_section->vma
3436 + pltoff_sec->output_offset
3437 + dyn_i->pltoff_offset);
3439 return value;
3442 /* Return the base VMA address which should be subtracted from real addresses
3443 when resolving @tprel() relocation.
3444 Main program TLS (whose template starts at PT_TLS p_vaddr)
3445 is assigned offset round(16, PT_TLS p_align). */
3447 static bfd_vma
3448 elfNN_ia64_tprel_base (info)
3449 struct bfd_link_info *info;
3451 struct elf_link_tls_segment *tls_segment
3452 = elf_hash_table (info)->tls_segment;
3454 BFD_ASSERT (tls_segment != NULL);
3455 return (tls_segment->start
3456 - align_power ((bfd_vma) 16, tls_segment->align));
3459 /* Return the base VMA address which should be subtracted from real addresses
3460 when resolving @dtprel() relocation.
3461 This is PT_TLS segment p_vaddr. */
3463 static bfd_vma
3464 elfNN_ia64_dtprel_base (info)
3465 struct bfd_link_info *info;
3467 BFD_ASSERT (elf_hash_table (info)->tls_segment != NULL);
3468 return elf_hash_table (info)->tls_segment->start;
3471 /* Called through qsort to sort the .IA_64.unwind section during a
3472 non-relocatable link. Set elfNN_ia64_unwind_entry_compare_bfd
3473 to the output bfd so we can do proper endianness frobbing. */
3475 static bfd *elfNN_ia64_unwind_entry_compare_bfd;
3477 static int
3478 elfNN_ia64_unwind_entry_compare (a, b)
3479 const PTR a;
3480 const PTR b;
3482 bfd_vma av, bv;
3484 av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
3485 bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
3487 return (av < bv ? -1 : av > bv ? 1 : 0);
3490 static bfd_boolean
3491 elfNN_ia64_final_link (abfd, info)
3492 bfd *abfd;
3493 struct bfd_link_info *info;
3495 struct elfNN_ia64_link_hash_table *ia64_info;
3496 asection *unwind_output_sec;
3498 ia64_info = elfNN_ia64_hash_table (info);
3500 /* Make sure we've got ourselves a nice fat __gp value. */
3501 if (!info->relocateable)
3503 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3504 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3505 struct elf_link_hash_entry *gp;
3506 bfd_vma gp_val;
3507 asection *os;
3509 /* Find the min and max vma of all sections marked short. Also
3510 collect min and max vma of any type, for use in selecting a
3511 nice gp. */
3512 for (os = abfd->sections; os ; os = os->next)
3514 bfd_vma lo, hi;
3516 if ((os->flags & SEC_ALLOC) == 0)
3517 continue;
3519 lo = os->vma;
3520 hi = os->vma + os->_raw_size;
3521 if (hi < lo)
3522 hi = (bfd_vma) -1;
3524 if (min_vma > lo)
3525 min_vma = lo;
3526 if (max_vma < hi)
3527 max_vma = hi;
3528 if (os->flags & SEC_SMALL_DATA)
3530 if (min_short_vma > lo)
3531 min_short_vma = lo;
3532 if (max_short_vma < hi)
3533 max_short_vma = hi;
3537 /* See if the user wants to force a value. */
3538 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
3539 FALSE, FALSE);
3541 if (gp
3542 && (gp->root.type == bfd_link_hash_defined
3543 || gp->root.type == bfd_link_hash_defweak))
3545 asection *gp_sec = gp->root.u.def.section;
3546 gp_val = (gp->root.u.def.value
3547 + gp_sec->output_section->vma
3548 + gp_sec->output_offset);
3550 else
3552 /* Pick a sensible value. */
3554 asection *got_sec = ia64_info->got_sec;
3556 /* Start with just the address of the .got. */
3557 if (got_sec)
3558 gp_val = got_sec->output_section->vma;
3559 else if (max_short_vma != 0)
3560 gp_val = min_short_vma;
3561 else
3562 gp_val = min_vma;
3564 /* If it is possible to address the entire image, but we
3565 don't with the choice above, adjust. */
3566 if (max_vma - min_vma < 0x400000
3567 && max_vma - gp_val <= 0x200000
3568 && gp_val - min_vma > 0x200000)
3569 gp_val = min_vma + 0x200000;
3570 else if (max_short_vma != 0)
3572 /* If we don't cover all the short data, adjust. */
3573 if (max_short_vma - gp_val >= 0x200000)
3574 gp_val = min_short_vma + 0x200000;
3576 /* If we're addressing stuff past the end, adjust back. */
3577 if (gp_val > max_vma)
3578 gp_val = max_vma - 0x200000 + 8;
3582 /* Validate whether all SHF_IA_64_SHORT sections are within
3583 range of the chosen GP. */
3585 if (max_short_vma != 0)
3587 if (max_short_vma - min_short_vma >= 0x400000)
3589 (*_bfd_error_handler)
3590 (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
3591 bfd_get_filename (abfd),
3592 (unsigned long) (max_short_vma - min_short_vma));
3593 return FALSE;
3595 else if ((gp_val > min_short_vma
3596 && gp_val - min_short_vma > 0x200000)
3597 || (gp_val < max_short_vma
3598 && max_short_vma - gp_val >= 0x200000))
3600 (*_bfd_error_handler)
3601 (_("%s: __gp does not cover short data segment"),
3602 bfd_get_filename (abfd));
3603 return FALSE;
3607 _bfd_set_gp_value (abfd, gp_val);
3609 if (gp)
3611 gp->root.type = bfd_link_hash_defined;
3612 gp->root.u.def.value = gp_val;
3613 gp->root.u.def.section = bfd_abs_section_ptr;
3617 /* If we're producing a final executable, we need to sort the contents
3618 of the .IA_64.unwind section. Force this section to be relocated
3619 into memory rather than written immediately to the output file. */
3620 unwind_output_sec = NULL;
3621 if (!info->relocateable)
3623 asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3624 if (s)
3626 unwind_output_sec = s->output_section;
3627 unwind_output_sec->contents
3628 = bfd_malloc (unwind_output_sec->_raw_size);
3629 if (unwind_output_sec->contents == NULL)
3630 return FALSE;
3634 /* Invoke the regular ELF backend linker to do all the work. */
3635 if (!bfd_elfNN_bfd_final_link (abfd, info))
3636 return FALSE;
3638 if (unwind_output_sec)
3640 elfNN_ia64_unwind_entry_compare_bfd = abfd;
3641 qsort (unwind_output_sec->contents,
3642 (size_t) (unwind_output_sec->_raw_size / 24),
3644 elfNN_ia64_unwind_entry_compare);
3646 if (! bfd_set_section_contents (abfd, unwind_output_sec,
3647 unwind_output_sec->contents, (bfd_vma) 0,
3648 unwind_output_sec->_raw_size))
3649 return FALSE;
3652 return TRUE;
3655 static bfd_boolean
3656 elfNN_ia64_relocate_section (output_bfd, info, input_bfd, input_section,
3657 contents, relocs, local_syms, local_sections)
3658 bfd *output_bfd;
3659 struct bfd_link_info *info;
3660 bfd *input_bfd;
3661 asection *input_section;
3662 bfd_byte *contents;
3663 Elf_Internal_Rela *relocs;
3664 Elf_Internal_Sym *local_syms;
3665 asection **local_sections;
3667 struct elfNN_ia64_link_hash_table *ia64_info;
3668 Elf_Internal_Shdr *symtab_hdr;
3669 Elf_Internal_Rela *rel;
3670 Elf_Internal_Rela *relend;
3671 asection *srel;
3672 bfd_boolean ret_val = TRUE; /* for non-fatal errors */
3673 bfd_vma gp_val;
3675 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3676 ia64_info = elfNN_ia64_hash_table (info);
3678 /* Infect various flags from the input section to the output section. */
3679 if (info->relocateable)
3681 bfd_vma flags;
3683 flags = elf_section_data(input_section)->this_hdr.sh_flags;
3684 flags &= SHF_IA_64_NORECOV;
3686 elf_section_data(input_section->output_section)
3687 ->this_hdr.sh_flags |= flags;
3688 return TRUE;
3691 gp_val = _bfd_get_gp_value (output_bfd);
3692 srel = get_reloc_section (input_bfd, ia64_info, input_section, FALSE);
3694 rel = relocs;
3695 relend = relocs + input_section->reloc_count;
3696 for (; rel < relend; ++rel)
3698 struct elf_link_hash_entry *h;
3699 struct elfNN_ia64_dyn_sym_info *dyn_i;
3700 bfd_reloc_status_type r;
3701 reloc_howto_type *howto;
3702 unsigned long r_symndx;
3703 Elf_Internal_Sym *sym;
3704 unsigned int r_type;
3705 bfd_vma value;
3706 asection *sym_sec;
3707 bfd_byte *hit_addr;
3708 bfd_boolean dynamic_symbol_p;
3709 bfd_boolean undef_weak_ref;
3711 r_type = ELFNN_R_TYPE (rel->r_info);
3712 if (r_type > R_IA64_MAX_RELOC_CODE)
3714 (*_bfd_error_handler)
3715 (_("%s: unknown relocation type %d"),
3716 bfd_archive_filename (input_bfd), (int)r_type);
3717 bfd_set_error (bfd_error_bad_value);
3718 ret_val = FALSE;
3719 continue;
3722 howto = lookup_howto (r_type);
3723 r_symndx = ELFNN_R_SYM (rel->r_info);
3724 h = NULL;
3725 sym = NULL;
3726 sym_sec = NULL;
3727 undef_weak_ref = FALSE;
3729 if (r_symndx < symtab_hdr->sh_info)
3731 /* Reloc against local symbol. */
3732 sym = local_syms + r_symndx;
3733 sym_sec = local_sections[r_symndx];
3734 value = _bfd_elf_rela_local_sym (output_bfd, sym, sym_sec, rel);
3735 if ((sym_sec->flags & SEC_MERGE)
3736 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
3737 && (elf_section_data (sym_sec)->sec_info_type
3738 == ELF_INFO_TYPE_MERGE))
3740 struct elfNN_ia64_local_hash_entry *loc_h;
3742 loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, FALSE);
3743 if (loc_h && ! loc_h->sec_merge_done)
3745 struct elfNN_ia64_dyn_sym_info *dynent;
3746 asection *msec;
3748 for (dynent = loc_h->info; dynent; dynent = dynent->next)
3750 msec = sym_sec;
3751 dynent->addend =
3752 _bfd_merged_section_offset (output_bfd, &msec,
3753 elf_section_data (msec)->
3754 sec_info,
3755 sym->st_value
3756 + dynent->addend,
3757 (bfd_vma) 0);
3758 dynent->addend -= sym->st_value;
3759 dynent->addend += msec->output_section->vma
3760 + msec->output_offset
3761 - sym_sec->output_section->vma
3762 - sym_sec->output_offset;
3764 loc_h->sec_merge_done = 1;
3768 else
3770 long indx;
3772 /* Reloc against global symbol. */
3773 indx = r_symndx - symtab_hdr->sh_info;
3774 h = elf_sym_hashes (input_bfd)[indx];
3775 while (h->root.type == bfd_link_hash_indirect
3776 || h->root.type == bfd_link_hash_warning)
3777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3779 value = 0;
3780 if (h->root.type == bfd_link_hash_defined
3781 || h->root.type == bfd_link_hash_defweak)
3783 sym_sec = h->root.u.def.section;
3785 /* Detect the cases that sym_sec->output_section is
3786 expected to be NULL -- all cases in which the symbol
3787 is defined in another shared module. This includes
3788 PLT relocs for which we've created a PLT entry and
3789 other relocs for which we're prepared to create
3790 dynamic relocations. */
3791 /* ??? Just accept it NULL and continue. */
3793 if (sym_sec->output_section != NULL)
3795 value = (h->root.u.def.value
3796 + sym_sec->output_section->vma
3797 + sym_sec->output_offset);
3800 else if (h->root.type == bfd_link_hash_undefweak)
3801 undef_weak_ref = TRUE;
3802 else if (info->shared
3803 && (!info->symbolic || info->allow_shlib_undefined)
3804 && !info->no_undefined
3805 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3807 else
3809 if (! ((*info->callbacks->undefined_symbol)
3810 (info, h->root.root.string, input_bfd,
3811 input_section, rel->r_offset,
3812 (!info->shared || info->no_undefined
3813 || ELF_ST_VISIBILITY (h->other)))))
3814 return FALSE;
3815 ret_val = FALSE;
3816 continue;
3820 hit_addr = contents + rel->r_offset;
3821 value += rel->r_addend;
3822 dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info);
3824 switch (r_type)
3826 case R_IA64_NONE:
3827 case R_IA64_LDXMOV:
3828 continue;
3830 case R_IA64_IMM14:
3831 case R_IA64_IMM22:
3832 case R_IA64_IMM64:
3833 case R_IA64_DIR32MSB:
3834 case R_IA64_DIR32LSB:
3835 case R_IA64_DIR64MSB:
3836 case R_IA64_DIR64LSB:
3837 /* Install a dynamic relocation for this reloc. */
3838 if ((dynamic_symbol_p || info->shared
3839 || (elfNN_ia64_aix_vec (info->hash->creator)
3840 /* Don't emit relocs for __GLOB_DATA_PTR on AIX. */
3841 && (!h || strcmp (h->root.root.string,
3842 "__GLOB_DATA_PTR") != 0)))
3843 && r_symndx != 0
3844 && (input_section->flags & SEC_ALLOC) != 0)
3846 unsigned int dyn_r_type;
3847 long dynindx;
3848 bfd_vma addend;
3850 BFD_ASSERT (srel != NULL);
3852 /* If we don't need dynamic symbol lookup, find a
3853 matching RELATIVE relocation. */
3854 dyn_r_type = r_type;
3855 if (dynamic_symbol_p)
3857 dynindx = h->dynindx;
3858 addend = rel->r_addend;
3859 value = 0;
3861 else
3863 switch (r_type)
3865 case R_IA64_DIR32MSB:
3866 dyn_r_type = R_IA64_REL32MSB;
3867 break;
3868 case R_IA64_DIR32LSB:
3869 dyn_r_type = R_IA64_REL32LSB;
3870 break;
3871 case R_IA64_DIR64MSB:
3872 dyn_r_type = R_IA64_REL64MSB;
3873 break;
3874 case R_IA64_DIR64LSB:
3875 dyn_r_type = R_IA64_REL64LSB;
3876 break;
3878 default:
3879 /* We can't represent this without a dynamic symbol.
3880 Adjust the relocation to be against an output
3881 section symbol, which are always present in the
3882 dynamic symbol table. */
3883 /* ??? People shouldn't be doing non-pic code in
3884 shared libraries. Hork. */
3885 (*_bfd_error_handler)
3886 (_("%s: linking non-pic code in a shared library"),
3887 bfd_archive_filename (input_bfd));
3888 ret_val = FALSE;
3889 continue;
3891 dynindx = 0;
3892 addend = value;
3895 if (elfNN_ia64_aix_vec (info->hash->creator))
3896 rel->r_addend = value;
3897 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3898 srel, rel->r_offset, dyn_r_type,
3899 dynindx, addend);
3901 /* FALLTHRU */
3903 case R_IA64_LTV32MSB:
3904 case R_IA64_LTV32LSB:
3905 case R_IA64_LTV64MSB:
3906 case R_IA64_LTV64LSB:
3907 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3908 break;
3910 case R_IA64_GPREL22:
3911 case R_IA64_GPREL64I:
3912 case R_IA64_GPREL32MSB:
3913 case R_IA64_GPREL32LSB:
3914 case R_IA64_GPREL64MSB:
3915 case R_IA64_GPREL64LSB:
3916 if (dynamic_symbol_p)
3918 (*_bfd_error_handler)
3919 (_("%s: @gprel relocation against dynamic symbol %s"),
3920 bfd_archive_filename (input_bfd), h->root.root.string);
3921 ret_val = FALSE;
3922 continue;
3924 value -= gp_val;
3925 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3926 break;
3928 case R_IA64_LTOFF22:
3929 case R_IA64_LTOFF22X:
3930 case R_IA64_LTOFF64I:
3931 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
3932 value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
3933 rel->r_addend, value, R_IA64_DIR64LSB);
3934 value -= gp_val;
3935 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3936 break;
3938 case R_IA64_PLTOFF22:
3939 case R_IA64_PLTOFF64I:
3940 case R_IA64_PLTOFF64MSB:
3941 case R_IA64_PLTOFF64LSB:
3942 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
3943 value = set_pltoff_entry (output_bfd, info, dyn_i, value, FALSE);
3944 value -= gp_val;
3945 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3946 break;
3948 case R_IA64_FPTR64I:
3949 case R_IA64_FPTR32MSB:
3950 case R_IA64_FPTR32LSB:
3951 case R_IA64_FPTR64MSB:
3952 case R_IA64_FPTR64LSB:
3953 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
3954 if (dyn_i->want_fptr)
3956 if (!undef_weak_ref)
3957 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3959 else
3961 long dynindx;
3963 /* Otherwise, we expect the dynamic linker to create
3964 the entry. */
3966 if (h)
3968 if (h->dynindx != -1)
3969 dynindx = h->dynindx;
3970 else
3971 dynindx = (_bfd_elf_link_lookup_local_dynindx
3972 (info, h->root.u.def.section->owner,
3973 global_sym_index (h)));
3975 else
3977 dynindx = (_bfd_elf_link_lookup_local_dynindx
3978 (info, input_bfd, (long) r_symndx));
3981 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
3982 srel, rel->r_offset, r_type,
3983 dynindx, rel->r_addend);
3984 value = 0;
3987 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
3988 break;
3990 case R_IA64_LTOFF_FPTR22:
3991 case R_IA64_LTOFF_FPTR64I:
3992 case R_IA64_LTOFF_FPTR32MSB:
3993 case R_IA64_LTOFF_FPTR32LSB:
3994 case R_IA64_LTOFF_FPTR64MSB:
3995 case R_IA64_LTOFF_FPTR64LSB:
3997 long dynindx;
3999 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
4000 if (dyn_i->want_fptr)
4002 BFD_ASSERT (h == NULL || h->dynindx == -1)
4003 if (!undef_weak_ref)
4004 value = set_fptr_entry (output_bfd, info, dyn_i, value);
4005 dynindx = -1;
4007 else
4009 /* Otherwise, we expect the dynamic linker to create
4010 the entry. */
4011 if (h)
4013 if (h->dynindx != -1)
4014 dynindx = h->dynindx;
4015 else
4016 dynindx = (_bfd_elf_link_lookup_local_dynindx
4017 (info, h->root.u.def.section->owner,
4018 global_sym_index (h)));
4020 else
4021 dynindx = (_bfd_elf_link_lookup_local_dynindx
4022 (info, input_bfd, (long) r_symndx));
4023 value = 0;
4026 value = set_got_entry (output_bfd, info, dyn_i, dynindx,
4027 rel->r_addend, value, R_IA64_FPTR64LSB);
4028 value -= gp_val;
4029 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4031 break;
4033 case R_IA64_PCREL32MSB:
4034 case R_IA64_PCREL32LSB:
4035 case R_IA64_PCREL64MSB:
4036 case R_IA64_PCREL64LSB:
4037 /* Install a dynamic relocation for this reloc. */
4038 if ((dynamic_symbol_p
4039 || elfNN_ia64_aix_vec (info->hash->creator))
4040 && r_symndx != 0)
4042 BFD_ASSERT (srel != NULL);
4044 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
4045 srel, rel->r_offset, r_type,
4046 h->dynindx, rel->r_addend);
4048 goto finish_pcrel;
4050 case R_IA64_PCREL21B:
4051 case R_IA64_PCREL60B:
4052 /* We should have created a PLT entry for any dynamic symbol. */
4053 dyn_i = NULL;
4054 if (h)
4055 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
4057 if (dyn_i && dyn_i->want_plt2)
4059 /* Should have caught this earlier. */
4060 BFD_ASSERT (rel->r_addend == 0);
4062 value = (ia64_info->plt_sec->output_section->vma
4063 + ia64_info->plt_sec->output_offset
4064 + dyn_i->plt2_offset);
4066 else
4068 /* Since there's no PLT entry, Validate that this is
4069 locally defined. */
4070 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
4072 /* If the symbol is undef_weak, we shouldn't be trying
4073 to call it. There's every chance that we'd wind up
4074 with an out-of-range fixup here. Don't bother setting
4075 any value at all. */
4076 if (undef_weak_ref)
4077 continue;
4079 goto finish_pcrel;
4081 case R_IA64_PCREL21BI:
4082 case R_IA64_PCREL21F:
4083 case R_IA64_PCREL21M:
4084 case R_IA64_PCREL22:
4085 case R_IA64_PCREL64I:
4086 /* The PCREL21BI reloc is specifically not intended for use with
4087 dynamic relocs. PCREL21F and PCREL21M are used for speculation
4088 fixup code, and thus probably ought not be dynamic. The
4089 PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs. */
4090 if (dynamic_symbol_p)
4092 const char *msg;
4094 if (r_type == R_IA64_PCREL21BI)
4095 msg = _("%s: @internal branch to dynamic symbol %s");
4096 else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
4097 msg = _("%s: speculation fixup to dynamic symbol %s");
4098 else
4099 msg = _("%s: @pcrel relocation against dynamic symbol %s");
4100 (*_bfd_error_handler) (msg, bfd_archive_filename (input_bfd),
4101 h->root.root.string);
4102 ret_val = FALSE;
4103 continue;
4105 goto finish_pcrel;
4107 finish_pcrel:
4108 /* Make pc-relative. */
4109 value -= (input_section->output_section->vma
4110 + input_section->output_offset
4111 + rel->r_offset) & ~ (bfd_vma) 0x3;
4112 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4113 break;
4115 case R_IA64_SEGREL32MSB:
4116 case R_IA64_SEGREL32LSB:
4117 case R_IA64_SEGREL64MSB:
4118 case R_IA64_SEGREL64LSB:
4119 if (r_symndx == 0)
4121 /* If the input section was discarded from the output, then
4122 do nothing. */
4123 r = bfd_reloc_ok;
4125 else
4127 struct elf_segment_map *m;
4128 Elf_Internal_Phdr *p;
4130 /* Find the segment that contains the output_section. */
4131 for (m = elf_tdata (output_bfd)->segment_map,
4132 p = elf_tdata (output_bfd)->phdr;
4133 m != NULL;
4134 m = m->next, p++)
4136 int i;
4137 for (i = m->count - 1; i >= 0; i--)
4138 if (m->sections[i] == sym_sec->output_section)
4139 break;
4140 if (i >= 0)
4141 break;
4144 if (m == NULL)
4146 r = bfd_reloc_notsupported;
4148 else
4150 /* The VMA of the segment is the vaddr of the associated
4151 program header. */
4152 if (value > p->p_vaddr)
4153 value -= p->p_vaddr;
4154 else
4155 value = 0;
4156 r = elfNN_ia64_install_value (output_bfd, hit_addr, value,
4157 r_type);
4159 break;
4162 case R_IA64_SECREL32MSB:
4163 case R_IA64_SECREL32LSB:
4164 case R_IA64_SECREL64MSB:
4165 case R_IA64_SECREL64LSB:
4166 /* Make output-section relative. */
4167 if (value > input_section->output_section->vma)
4168 value -= input_section->output_section->vma;
4169 else
4170 value = 0;
4171 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4172 break;
4174 case R_IA64_IPLTMSB:
4175 case R_IA64_IPLTLSB:
4176 /* Install a dynamic relocation for this reloc. */
4177 if ((dynamic_symbol_p || info->shared)
4178 && (input_section->flags & SEC_ALLOC) != 0)
4180 BFD_ASSERT (srel != NULL);
4182 /* If we don't need dynamic symbol lookup, install two
4183 RELATIVE relocations. */
4184 if (! dynamic_symbol_p)
4186 unsigned int dyn_r_type;
4188 if (r_type == R_IA64_IPLTMSB)
4189 dyn_r_type = R_IA64_REL64MSB;
4190 else
4191 dyn_r_type = R_IA64_REL64LSB;
4193 elfNN_ia64_install_dyn_reloc (output_bfd, info,
4194 input_section,
4195 srel, rel->r_offset,
4196 dyn_r_type, 0, value);
4197 elfNN_ia64_install_dyn_reloc (output_bfd, info,
4198 input_section,
4199 srel, rel->r_offset + 8,
4200 dyn_r_type, 0, gp_val);
4202 else
4203 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
4204 srel, rel->r_offset, r_type,
4205 h->dynindx, rel->r_addend);
4208 if (r_type == R_IA64_IPLTMSB)
4209 r_type = R_IA64_DIR64MSB;
4210 else
4211 r_type = R_IA64_DIR64LSB;
4212 elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4213 r = elfNN_ia64_install_value (output_bfd, hit_addr + 8, gp_val,
4214 r_type);
4215 break;
4217 case R_IA64_TPREL14:
4218 case R_IA64_TPREL22:
4219 case R_IA64_TPREL64I:
4220 value -= elfNN_ia64_tprel_base (info);
4221 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4222 break;
4224 case R_IA64_DTPREL14:
4225 case R_IA64_DTPREL22:
4226 case R_IA64_DTPREL64I:
4227 value -= elfNN_ia64_dtprel_base (info);
4228 r = elfNN_ia64_install_value (output_bfd, hit_addr, value, r_type);
4229 break;
4231 case R_IA64_LTOFF_TPREL22:
4232 case R_IA64_LTOFF_DTPMOD22:
4233 case R_IA64_LTOFF_DTPREL22:
4235 int got_r_type;
4237 switch (r_type)
4239 default:
4240 case R_IA64_LTOFF_TPREL22:
4241 if (!dynamic_symbol_p && !info->shared)
4242 value -= elfNN_ia64_tprel_base (info);
4243 got_r_type = R_IA64_TPREL64LSB;
4244 break;
4245 case R_IA64_LTOFF_DTPMOD22:
4246 if (!dynamic_symbol_p && !info->shared)
4247 value = 1;
4248 got_r_type = R_IA64_DTPMOD64LSB;
4249 break;
4250 case R_IA64_LTOFF_DTPREL22:
4251 if (!dynamic_symbol_p)
4252 value -= elfNN_ia64_dtprel_base (info);
4253 got_r_type = R_IA64_DTPREL64LSB;
4254 break;
4256 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
4257 value = set_got_entry (input_bfd, info, dyn_i,
4258 (h ? h->dynindx : -1), rel->r_addend,
4259 value, got_r_type);
4260 value -= gp_val;
4261 r = elfNN_ia64_install_value (output_bfd, hit_addr, value,
4262 r_type);
4264 break;
4266 default:
4267 r = bfd_reloc_notsupported;
4268 break;
4271 switch (r)
4273 case bfd_reloc_ok:
4274 break;
4276 case bfd_reloc_undefined:
4277 /* This can happen for global table relative relocs if
4278 __gp is undefined. This is a panic situation so we
4279 don't try to continue. */
4280 (*info->callbacks->undefined_symbol)
4281 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
4282 return FALSE;
4284 case bfd_reloc_notsupported:
4286 const char *name;
4288 if (h)
4289 name = h->root.root.string;
4290 else
4292 name = bfd_elf_string_from_elf_section (input_bfd,
4293 symtab_hdr->sh_link,
4294 sym->st_name);
4295 if (name == NULL)
4296 return FALSE;
4297 if (*name == '\0')
4298 name = bfd_section_name (input_bfd, input_section);
4300 if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
4301 name, input_bfd,
4302 input_section, rel->r_offset))
4303 return FALSE;
4304 ret_val = FALSE;
4306 break;
4308 case bfd_reloc_dangerous:
4309 case bfd_reloc_outofrange:
4310 case bfd_reloc_overflow:
4311 default:
4313 const char *name;
4315 if (h)
4316 name = h->root.root.string;
4317 else
4319 name = bfd_elf_string_from_elf_section (input_bfd,
4320 symtab_hdr->sh_link,
4321 sym->st_name);
4322 if (name == NULL)
4323 return FALSE;
4324 if (*name == '\0')
4325 name = bfd_section_name (input_bfd, input_section);
4327 if (!(*info->callbacks->reloc_overflow) (info, name,
4328 howto->name,
4329 (bfd_vma) 0,
4330 input_bfd,
4331 input_section,
4332 rel->r_offset))
4333 return FALSE;
4334 ret_val = FALSE;
4336 break;
4340 return ret_val;
4343 static bfd_boolean
4344 elfNN_ia64_finish_dynamic_symbol (output_bfd, info, h, sym)
4345 bfd *output_bfd;
4346 struct bfd_link_info *info;
4347 struct elf_link_hash_entry *h;
4348 Elf_Internal_Sym *sym;
4350 struct elfNN_ia64_link_hash_table *ia64_info;
4351 struct elfNN_ia64_dyn_sym_info *dyn_i;
4353 ia64_info = elfNN_ia64_hash_table (info);
4354 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
4356 /* Fill in the PLT data, if required. */
4357 if (dyn_i && dyn_i->want_plt)
4359 Elf_Internal_Rela outrel;
4360 bfd_byte *loc;
4361 asection *plt_sec;
4362 bfd_vma plt_addr, pltoff_addr, gp_val, index;
4364 gp_val = _bfd_get_gp_value (output_bfd);
4366 /* Initialize the minimal PLT entry. */
4368 index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
4369 plt_sec = ia64_info->plt_sec;
4370 loc = plt_sec->contents + dyn_i->plt_offset;
4372 memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
4373 elfNN_ia64_install_value (output_bfd, loc, index, R_IA64_IMM22);
4374 elfNN_ia64_install_value (output_bfd, loc+2, -dyn_i->plt_offset,
4375 R_IA64_PCREL21B);
4377 plt_addr = (plt_sec->output_section->vma
4378 + plt_sec->output_offset
4379 + dyn_i->plt_offset);
4380 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, TRUE);
4382 /* Initialize the FULL PLT entry, if needed. */
4383 if (dyn_i->want_plt2)
4385 loc = plt_sec->contents + dyn_i->plt2_offset;
4387 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
4388 elfNN_ia64_install_value (output_bfd, loc, pltoff_addr - gp_val,
4389 R_IA64_IMM22);
4391 /* Mark the symbol as undefined, rather than as defined in the
4392 plt section. Leave the value alone. */
4393 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
4394 first place. But perhaps elflink.h did some for us. */
4395 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4396 sym->st_shndx = SHN_UNDEF;
4399 /* Create the dynamic relocation. */
4400 outrel.r_offset = pltoff_addr;
4401 if (bfd_little_endian (output_bfd))
4402 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
4403 else
4404 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
4405 outrel.r_addend = 0;
4407 /* This is fun. In the .IA_64.pltoff section, we've got entries
4408 that correspond both to real PLT entries, and those that
4409 happened to resolve to local symbols but need to be created
4410 to satisfy @pltoff relocations. The .rela.IA_64.pltoff
4411 relocations for the real PLT should come at the end of the
4412 section, so that they can be indexed by plt entry at runtime.
4414 We emitted all of the relocations for the non-PLT @pltoff
4415 entries during relocate_section. So we can consider the
4416 existing sec->reloc_count to be the base of the array of
4417 PLT relocations. */
4419 loc = ia64_info->rel_pltoff_sec->contents;
4420 loc += ((ia64_info->rel_pltoff_sec->reloc_count + index)
4421 * sizeof (Elf64_External_Rela));
4422 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
4425 /* Mark some specially defined symbols as absolute. */
4426 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4427 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4428 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4429 sym->st_shndx = SHN_ABS;
4431 return TRUE;
4434 static bfd_boolean
4435 elfNN_ia64_finish_dynamic_sections (abfd, info)
4436 bfd *abfd;
4437 struct bfd_link_info *info;
4439 struct elfNN_ia64_link_hash_table *ia64_info;
4440 bfd *dynobj;
4442 ia64_info = elfNN_ia64_hash_table (info);
4443 dynobj = ia64_info->root.dynobj;
4445 if (elf_hash_table (info)->dynamic_sections_created)
4447 ElfNN_External_Dyn *dyncon, *dynconend;
4448 asection *sdyn, *sgotplt;
4449 bfd_vma gp_val;
4451 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4452 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
4453 BFD_ASSERT (sdyn != NULL);
4454 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
4455 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4457 gp_val = _bfd_get_gp_value (abfd);
4459 for (; dyncon < dynconend; dyncon++)
4461 Elf_Internal_Dyn dyn;
4463 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
4465 switch (dyn.d_tag)
4467 case DT_PLTGOT:
4468 dyn.d_un.d_ptr = gp_val;
4469 break;
4471 case DT_PLTRELSZ:
4472 dyn.d_un.d_val = (ia64_info->minplt_entries
4473 * sizeof (ElfNN_External_Rela));
4474 break;
4476 case DT_JMPREL:
4477 /* See the comment above in finish_dynamic_symbol. */
4478 dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
4479 + ia64_info->rel_pltoff_sec->output_offset
4480 + (ia64_info->rel_pltoff_sec->reloc_count
4481 * sizeof (ElfNN_External_Rela)));
4482 break;
4484 case DT_IA_64_PLT_RESERVE:
4485 dyn.d_un.d_ptr = (sgotplt->output_section->vma
4486 + sgotplt->output_offset);
4487 break;
4489 case DT_RELASZ:
4490 /* Do not have RELASZ include JMPREL. This makes things
4491 easier on ld.so. This is not what the rest of BFD set up. */
4492 dyn.d_un.d_val -= (ia64_info->minplt_entries
4493 * sizeof (ElfNN_External_Rela));
4494 break;
4497 bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
4500 /* Initialize the PLT0 entry */
4501 if (ia64_info->plt_sec)
4503 bfd_byte *loc = ia64_info->plt_sec->contents;
4504 bfd_vma pltres;
4506 memcpy (loc, plt_header, PLT_HEADER_SIZE);
4508 pltres = (sgotplt->output_section->vma
4509 + sgotplt->output_offset
4510 - gp_val);
4512 elfNN_ia64_install_value (abfd, loc+1, pltres, R_IA64_GPREL22);
4516 return TRUE;
4519 /* ELF file flag handling: */
4521 /* Function to keep IA-64 specific file flags. */
4522 static bfd_boolean
4523 elfNN_ia64_set_private_flags (abfd, flags)
4524 bfd *abfd;
4525 flagword flags;
4527 BFD_ASSERT (!elf_flags_init (abfd)
4528 || elf_elfheader (abfd)->e_flags == flags);
4530 elf_elfheader (abfd)->e_flags = flags;
4531 elf_flags_init (abfd) = TRUE;
4532 return TRUE;
4535 /* Merge backend specific data from an object file to the output
4536 object file when linking. */
4537 static bfd_boolean
4538 elfNN_ia64_merge_private_bfd_data (ibfd, obfd)
4539 bfd *ibfd, *obfd;
4541 flagword out_flags;
4542 flagword in_flags;
4543 bfd_boolean ok = TRUE;
4545 /* Don't even pretend to support mixed-format linking. */
4546 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4547 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4548 return FALSE;
4550 in_flags = elf_elfheader (ibfd)->e_flags;
4551 out_flags = elf_elfheader (obfd)->e_flags;
4553 if (! elf_flags_init (obfd))
4555 elf_flags_init (obfd) = TRUE;
4556 elf_elfheader (obfd)->e_flags = in_flags;
4558 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4559 && bfd_get_arch_info (obfd)->the_default)
4561 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4562 bfd_get_mach (ibfd));
4565 return TRUE;
4568 /* Check flag compatibility. */
4569 if (in_flags == out_flags)
4570 return TRUE;
4572 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
4573 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4574 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4576 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4578 (*_bfd_error_handler)
4579 (_("%s: linking trap-on-NULL-dereference with non-trapping files"),
4580 bfd_archive_filename (ibfd));
4582 bfd_set_error (bfd_error_bad_value);
4583 ok = FALSE;
4585 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4587 (*_bfd_error_handler)
4588 (_("%s: linking big-endian files with little-endian files"),
4589 bfd_archive_filename (ibfd));
4591 bfd_set_error (bfd_error_bad_value);
4592 ok = FALSE;
4594 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4596 (*_bfd_error_handler)
4597 (_("%s: linking 64-bit files with 32-bit files"),
4598 bfd_archive_filename (ibfd));
4600 bfd_set_error (bfd_error_bad_value);
4601 ok = FALSE;
4603 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4605 (*_bfd_error_handler)
4606 (_("%s: linking constant-gp files with non-constant-gp files"),
4607 bfd_archive_filename (ibfd));
4609 bfd_set_error (bfd_error_bad_value);
4610 ok = FALSE;
4612 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4613 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4615 (*_bfd_error_handler)
4616 (_("%s: linking auto-pic files with non-auto-pic files"),
4617 bfd_archive_filename (ibfd));
4619 bfd_set_error (bfd_error_bad_value);
4620 ok = FALSE;
4623 return ok;
4626 static bfd_boolean
4627 elfNN_ia64_print_private_bfd_data (abfd, ptr)
4628 bfd *abfd;
4629 PTR ptr;
4631 FILE *file = (FILE *) ptr;
4632 flagword flags = elf_elfheader (abfd)->e_flags;
4634 BFD_ASSERT (abfd != NULL && ptr != NULL);
4636 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4637 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4638 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4639 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4640 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4641 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4642 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4643 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4644 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4646 _bfd_elf_print_private_bfd_data (abfd, ptr);
4647 return TRUE;
4650 static enum elf_reloc_type_class
4651 elfNN_ia64_reloc_type_class (rela)
4652 const Elf_Internal_Rela *rela;
4654 switch ((int) ELFNN_R_TYPE (rela->r_info))
4656 case R_IA64_REL32MSB:
4657 case R_IA64_REL32LSB:
4658 case R_IA64_REL64MSB:
4659 case R_IA64_REL64LSB:
4660 return reloc_class_relative;
4661 case R_IA64_IPLTMSB:
4662 case R_IA64_IPLTLSB:
4663 return reloc_class_plt;
4664 case R_IA64_COPY:
4665 return reloc_class_copy;
4666 default:
4667 return reloc_class_normal;
4671 static bfd_boolean
4672 elfNN_ia64_hpux_vec (const bfd_target *vec)
4674 extern const bfd_target bfd_elfNN_ia64_hpux_big_vec;
4675 return (vec == & bfd_elfNN_ia64_hpux_big_vec);
4678 static void
4679 elfNN_hpux_post_process_headers (abfd, info)
4680 bfd *abfd;
4681 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4683 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4685 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4686 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
4689 bfd_boolean
4690 elfNN_hpux_backend_section_from_bfd_section (abfd, sec, retval)
4691 bfd *abfd ATTRIBUTE_UNUSED;
4692 asection *sec;
4693 int *retval;
4695 if (bfd_is_com_section (sec))
4697 *retval = SHN_IA_64_ANSI_COMMON;
4698 return TRUE;
4700 return FALSE;
4703 #define TARGET_LITTLE_SYM bfd_elfNN_ia64_little_vec
4704 #define TARGET_LITTLE_NAME "elfNN-ia64-little"
4705 #define TARGET_BIG_SYM bfd_elfNN_ia64_big_vec
4706 #define TARGET_BIG_NAME "elfNN-ia64-big"
4707 #define ELF_ARCH bfd_arch_ia64
4708 #define ELF_MACHINE_CODE EM_IA_64
4709 #define ELF_MACHINE_ALT1 1999 /* EAS2.3 */
4710 #define ELF_MACHINE_ALT2 1998 /* EAS2.2 */
4711 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
4713 #define elf_backend_section_from_shdr \
4714 elfNN_ia64_section_from_shdr
4715 #define elf_backend_section_flags \
4716 elfNN_ia64_section_flags
4717 #define elf_backend_fake_sections \
4718 elfNN_ia64_fake_sections
4719 #define elf_backend_final_write_processing \
4720 elfNN_ia64_final_write_processing
4721 #define elf_backend_add_symbol_hook \
4722 elfNN_ia64_add_symbol_hook
4723 #define elf_backend_additional_program_headers \
4724 elfNN_ia64_additional_program_headers
4725 #define elf_backend_modify_segment_map \
4726 elfNN_ia64_modify_segment_map
4727 #define elf_info_to_howto \
4728 elfNN_ia64_info_to_howto
4730 #define bfd_elfNN_bfd_reloc_type_lookup \
4731 elfNN_ia64_reloc_type_lookup
4732 #define bfd_elfNN_bfd_is_local_label_name \
4733 elfNN_ia64_is_local_label_name
4734 #define bfd_elfNN_bfd_relax_section \
4735 elfNN_ia64_relax_section
4737 /* Stuff for the BFD linker: */
4738 #define bfd_elfNN_bfd_link_hash_table_create \
4739 elfNN_ia64_hash_table_create
4740 #define elf_backend_create_dynamic_sections \
4741 elfNN_ia64_create_dynamic_sections
4742 #define elf_backend_check_relocs \
4743 elfNN_ia64_check_relocs
4744 #define elf_backend_adjust_dynamic_symbol \
4745 elfNN_ia64_adjust_dynamic_symbol
4746 #define elf_backend_size_dynamic_sections \
4747 elfNN_ia64_size_dynamic_sections
4748 #define elf_backend_relocate_section \
4749 elfNN_ia64_relocate_section
4750 #define elf_backend_finish_dynamic_symbol \
4751 elfNN_ia64_finish_dynamic_symbol
4752 #define elf_backend_finish_dynamic_sections \
4753 elfNN_ia64_finish_dynamic_sections
4754 #define bfd_elfNN_bfd_final_link \
4755 elfNN_ia64_final_link
4757 #define bfd_elfNN_bfd_merge_private_bfd_data \
4758 elfNN_ia64_merge_private_bfd_data
4759 #define bfd_elfNN_bfd_set_private_flags \
4760 elfNN_ia64_set_private_flags
4761 #define bfd_elfNN_bfd_print_private_bfd_data \
4762 elfNN_ia64_print_private_bfd_data
4764 #define elf_backend_plt_readonly 1
4765 #define elf_backend_want_plt_sym 0
4766 #define elf_backend_plt_alignment 5
4767 #define elf_backend_got_header_size 0
4768 #define elf_backend_plt_header_size PLT_HEADER_SIZE
4769 #define elf_backend_want_got_plt 1
4770 #define elf_backend_may_use_rel_p 1
4771 #define elf_backend_may_use_rela_p 1
4772 #define elf_backend_default_use_rela_p 1
4773 #define elf_backend_want_dynbss 0
4774 #define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
4775 #define elf_backend_hide_symbol elfNN_ia64_hash_hide_symbol
4776 #define elf_backend_reloc_type_class elfNN_ia64_reloc_type_class
4777 #define elf_backend_rela_normal 1
4779 #include "elfNN-target.h"
4781 /* AIX-specific vectors. */
4783 #undef TARGET_LITTLE_SYM
4784 #define TARGET_LITTLE_SYM bfd_elfNN_ia64_aix_little_vec
4785 #undef TARGET_LITTLE_NAME
4786 #define TARGET_LITTLE_NAME "elfNN-ia64-aix-little"
4787 #undef TARGET_BIG_SYM
4788 #define TARGET_BIG_SYM bfd_elfNN_ia64_aix_big_vec
4789 #undef TARGET_BIG_NAME
4790 #define TARGET_BIG_NAME "elfNN-ia64-aix-big"
4792 #undef elf_backend_add_symbol_hook
4793 #define elf_backend_add_symbol_hook elfNN_ia64_aix_add_symbol_hook
4795 #undef bfd_elfNN_bfd_link_add_symbols
4796 #define bfd_elfNN_bfd_link_add_symbols elfNN_ia64_aix_link_add_symbols
4798 #define elfNN_bed elfNN_ia64_aix_bed
4800 #include "elfNN-target.h"
4802 /* HPUX-specific vectors. */
4804 #undef TARGET_LITTLE_SYM
4805 #undef TARGET_LITTLE_NAME
4806 #undef TARGET_BIG_SYM
4807 #define TARGET_BIG_SYM bfd_elfNN_ia64_hpux_big_vec
4808 #undef TARGET_BIG_NAME
4809 #define TARGET_BIG_NAME "elfNN-ia64-hpux-big"
4811 /* We need to undo the AIX specific functions. */
4813 #undef elf_backend_add_symbol_hook
4814 #define elf_backend_add_symbol_hook elfNN_ia64_add_symbol_hook
4816 #undef bfd_elfNN_bfd_link_add_symbols
4817 #define bfd_elfNN_bfd_link_add_symbols _bfd_generic_link_add_symbols
4819 /* These are HP-UX specific functions. */
4821 #undef elf_backend_post_process_headers
4822 #define elf_backend_post_process_headers elfNN_hpux_post_process_headers
4824 #undef elf_backend_section_from_bfd_section
4825 #define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
4827 #undef elf_backend_want_p_paddr_set_to_zero
4828 #define elf_backend_want_p_paddr_set_to_zero 1
4830 #undef ELF_MAXPAGESIZE
4831 #define ELF_MAXPAGESIZE 0x1000 /* 1K */
4833 #undef elfNN_bed
4834 #define elfNN_bed elfNN_ia64_hpux_bed
4836 #include "elfNN-target.h"
4838 #undef elf_backend_want_p_paddr_set_to_zero