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