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