source: trunk/src/binutils/bfd/elfxx-ia64.c@ 610

Last change on this file since 610 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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