elfnn-ia64.c revision 1.1.1.5.4.1 1 /* IA-64 support for 64-bit ELF
2 Copyright (C) 1998-2019 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 typedef struct bfd_hash_entry *(*new_hash_entry_func)
43 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
44
45 /* In dynamically (linker-) created sections, we generally need to keep track
46 of the place a symbol or expression got allocated to. This is done via hash
47 tables that store entries of the following type. */
48
49 struct elfNN_ia64_dyn_sym_info
50 {
51 /* The addend for which this entry is relevant. */
52 bfd_vma addend;
53
54 bfd_vma got_offset;
55 bfd_vma fptr_offset;
56 bfd_vma pltoff_offset;
57 bfd_vma plt_offset;
58 bfd_vma plt2_offset;
59 bfd_vma tprel_offset;
60 bfd_vma dtpmod_offset;
61 bfd_vma dtprel_offset;
62
63 /* The symbol table entry, if any, that this was derived from. */
64 struct elf_link_hash_entry *h;
65
66 /* Used to count non-got, non-plt relocations for delayed sizing
67 of relocation sections. */
68 struct elfNN_ia64_dyn_reloc_entry
69 {
70 struct elfNN_ia64_dyn_reloc_entry *next;
71 asection *srel;
72 int type;
73 int count;
74
75 /* Is this reloc against readonly section? */
76 bfd_boolean reltext;
77 } *reloc_entries;
78
79 /* TRUE when the section contents have been updated. */
80 unsigned got_done : 1;
81 unsigned fptr_done : 1;
82 unsigned pltoff_done : 1;
83 unsigned tprel_done : 1;
84 unsigned dtpmod_done : 1;
85 unsigned dtprel_done : 1;
86
87 /* TRUE for the different kinds of linker data we want created. */
88 unsigned want_got : 1;
89 unsigned want_gotx : 1;
90 unsigned want_fptr : 1;
91 unsigned want_ltoff_fptr : 1;
92 unsigned want_plt : 1;
93 unsigned want_plt2 : 1;
94 unsigned want_pltoff : 1;
95 unsigned want_tprel : 1;
96 unsigned want_dtpmod : 1;
97 unsigned want_dtprel : 1;
98 };
99
100 struct elfNN_ia64_local_hash_entry
101 {
102 int id;
103 unsigned int r_sym;
104 /* The number of elements in elfNN_ia64_dyn_sym_info array. */
105 unsigned int count;
106 /* The number of sorted elements in elfNN_ia64_dyn_sym_info array. */
107 unsigned int sorted_count;
108 /* The size of elfNN_ia64_dyn_sym_info array. */
109 unsigned int size;
110 /* The array of elfNN_ia64_dyn_sym_info. */
111 struct elfNN_ia64_dyn_sym_info *info;
112
113 /* TRUE if this hash entry's addends was translated for
114 SHF_MERGE optimization. */
115 unsigned sec_merge_done : 1;
116 };
117
118 struct elfNN_ia64_link_hash_entry
119 {
120 struct elf_link_hash_entry root;
121 /* The number of elements in elfNN_ia64_dyn_sym_info array. */
122 unsigned int count;
123 /* The number of sorted elements in elfNN_ia64_dyn_sym_info array. */
124 unsigned int sorted_count;
125 /* The size of elfNN_ia64_dyn_sym_info array. */
126 unsigned int size;
127 /* The array of elfNN_ia64_dyn_sym_info. */
128 struct elfNN_ia64_dyn_sym_info *info;
129 };
130
131 struct elfNN_ia64_link_hash_table
132 {
133 /* The main hash table. */
134 struct elf_link_hash_table root;
135
136 asection *fptr_sec; /* Function descriptor table (or NULL). */
137 asection *rel_fptr_sec; /* Dynamic relocation section for same. */
138 asection *pltoff_sec; /* Private descriptors for plt (or NULL). */
139 asection *rel_pltoff_sec; /* Dynamic relocation section for same. */
140
141 bfd_size_type minplt_entries; /* Number of minplt entries. */
142 unsigned reltext : 1; /* Are there relocs against readonly sections? */
143 unsigned self_dtpmod_done : 1;/* Has self DTPMOD entry been finished? */
144 bfd_vma self_dtpmod_offset; /* .got offset to self DTPMOD entry. */
145 /* There are maybe R_IA64_GPREL22 relocations, including those
146 optimized from R_IA64_LTOFF22X, against non-SHF_IA_64_SHORT
147 sections. We need to record those sections so that we can choose
148 a proper GP to cover all R_IA64_GPREL22 relocations. */
149 asection *max_short_sec; /* Maximum short output section. */
150 bfd_vma max_short_offset; /* Maximum short offset. */
151 asection *min_short_sec; /* Minimum short output section. */
152 bfd_vma min_short_offset; /* Minimum short offset. */
153
154 htab_t loc_hash_table;
155 void *loc_hash_memory;
156 };
157
158 struct elfNN_ia64_allocate_data
159 {
160 struct bfd_link_info *info;
161 bfd_size_type ofs;
162 bfd_boolean only_got;
163 };
164
165 #define elfNN_ia64_hash_table(p) \
166 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
167 == IA64_ELF_DATA ? ((struct elfNN_ia64_link_hash_table *) ((p)->hash)) : NULL)
168
169 static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
170 (struct elfNN_ia64_link_hash_table *ia64_info,
171 struct elf_link_hash_entry *h,
172 bfd *abfd, const Elf_Internal_Rela *rel, bfd_boolean create);
173 static bfd_boolean elfNN_ia64_dynamic_symbol_p
174 (struct elf_link_hash_entry *h, struct bfd_link_info *info, int);
175 static bfd_boolean elfNN_ia64_choose_gp
176 (bfd *abfd, struct bfd_link_info *info, bfd_boolean final);
177 static void elfNN_ia64_dyn_sym_traverse
178 (struct elfNN_ia64_link_hash_table *ia64_info,
179 bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, void *),
180 void * info);
181 static bfd_boolean allocate_global_data_got
182 (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
183 static bfd_boolean allocate_global_fptr_got
184 (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
185 static bfd_boolean allocate_local_got
186 (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
187 static bfd_boolean elfNN_ia64_hpux_vec
188 (const bfd_target *vec);
189 static bfd_boolean allocate_dynrel_entries
190 (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
191 static asection *get_pltoff
192 (bfd *abfd, struct bfd_link_info *info,
193 struct elfNN_ia64_link_hash_table *ia64_info);
194
195 /* ia64-specific relocation. */
197
198 /* Given a ELF reloc, return the matching HOWTO structure. */
199
200 static bfd_boolean
201 elfNN_ia64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
202 arelent *bfd_reloc,
203 Elf_Internal_Rela *elf_reloc)
204 {
205 unsigned int r_type = ELF32_R_TYPE (elf_reloc->r_info);
206
207 bfd_reloc->howto = ia64_elf_lookup_howto (r_type);
208 if (bfd_reloc->howto == NULL)
209 {
210 /* xgettext:c-format */
211 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
212 abfd, r_type);
213 bfd_set_error (bfd_error_bad_value);
214 return FALSE;
215 }
216
217 return TRUE;
218 }
219
220 #define PLT_HEADER_SIZE (3 * 16)
222 #define PLT_MIN_ENTRY_SIZE (1 * 16)
223 #define PLT_FULL_ENTRY_SIZE (2 * 16)
224 #define PLT_RESERVED_WORDS 3
225
226 static const bfd_byte plt_header[PLT_HEADER_SIZE] =
227 {
228 0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21, /* [MMI] mov r2=r14;; */
229 0xe0, 0x00, 0x08, 0x00, 0x48, 0x00, /* addl r14=0,r2 */
230 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
231 0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14, /* [MMI] ld8 r16=[r14],8;; */
232 0x10, 0x41, 0x38, 0x30, 0x28, 0x00, /* ld8 r17=[r14],8 */
233 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
234 0x11, 0x08, 0x00, 0x1c, 0x18, 0x10, /* [MIB] ld8 r1=[r14] */
235 0x60, 0x88, 0x04, 0x80, 0x03, 0x00, /* mov b6=r17 */
236 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
237 };
238
239 static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
240 {
241 0x11, 0x78, 0x00, 0x00, 0x00, 0x24, /* [MIB] mov r15=0 */
242 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, /* nop.i 0x0 */
243 0x00, 0x00, 0x00, 0x40 /* br.few 0 <PLT0>;; */
244 };
245
246 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
247 {
248 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
249 0x00, 0x41, 0x3c, 0x70, 0x29, 0xc0, /* ld8.acq r16=[r15],8*/
250 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
251 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
252 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
253 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
254 };
255
256 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
257
258 static const bfd_byte oor_brl[16] =
259 {
260 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
261 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.sptk.few tgt;; */
262 0x00, 0x00, 0x00, 0xc0
263 };
264
265 static const bfd_byte oor_ip[48] =
266 {
267 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
268 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, /* movl r15=0 */
269 0x01, 0x00, 0x00, 0x60,
270 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MII] nop.m 0 */
271 0x00, 0x01, 0x00, 0x60, 0x00, 0x00, /* mov r16=ip;; */
272 0xf2, 0x80, 0x00, 0x80, /* add r16=r15,r16;; */
273 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
274 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
275 0x60, 0x00, 0x80, 0x00 /* br b6;; */
276 };
277
278 static size_t oor_branch_size = sizeof (oor_brl);
279
280 void
281 bfd_elfNN_ia64_after_parse (int itanium)
282 {
283 oor_branch_size = itanium ? sizeof (oor_ip) : sizeof (oor_brl);
284 }
285
286
288 /* Rename some of the generic section flags to better document how they
289 are used here. */
290 #define skip_relax_pass_0 sec_flg0
291 #define skip_relax_pass_1 sec_flg1
292
293 /* These functions do relaxation for IA-64 ELF. */
294
295 static void
296 elfNN_ia64_update_short_info (asection *sec, bfd_vma offset,
297 struct elfNN_ia64_link_hash_table *ia64_info)
298 {
299 /* Skip ABS and SHF_IA_64_SHORT sections. */
300 if (sec == bfd_abs_section_ptr
301 || (sec->flags & SEC_SMALL_DATA) != 0)
302 return;
303
304 if (!ia64_info->min_short_sec)
305 {
306 ia64_info->max_short_sec = sec;
307 ia64_info->max_short_offset = offset;
308 ia64_info->min_short_sec = sec;
309 ia64_info->min_short_offset = offset;
310 }
311 else if (sec == ia64_info->max_short_sec
312 && offset > ia64_info->max_short_offset)
313 ia64_info->max_short_offset = offset;
314 else if (sec == ia64_info->min_short_sec
315 && offset < ia64_info->min_short_offset)
316 ia64_info->min_short_offset = offset;
317 else if (sec->output_section->vma
318 > ia64_info->max_short_sec->vma)
319 {
320 ia64_info->max_short_sec = sec;
321 ia64_info->max_short_offset = offset;
322 }
323 else if (sec->output_section->vma
324 < ia64_info->min_short_sec->vma)
325 {
326 ia64_info->min_short_sec = sec;
327 ia64_info->min_short_offset = offset;
328 }
329 }
330
331 static bfd_boolean
332 elfNN_ia64_relax_section (bfd *abfd, asection *sec,
333 struct bfd_link_info *link_info,
334 bfd_boolean *again)
335 {
336 struct one_fixup
337 {
338 struct one_fixup *next;
339 asection *tsec;
340 bfd_vma toff;
341 bfd_vma trampoff;
342 };
343
344 Elf_Internal_Shdr *symtab_hdr;
345 Elf_Internal_Rela *internal_relocs;
346 Elf_Internal_Rela *irel, *irelend;
347 bfd_byte *contents;
348 Elf_Internal_Sym *isymbuf = NULL;
349 struct elfNN_ia64_link_hash_table *ia64_info;
350 struct one_fixup *fixups = NULL;
351 bfd_boolean changed_contents = FALSE;
352 bfd_boolean changed_relocs = FALSE;
353 bfd_boolean changed_got = FALSE;
354 bfd_boolean skip_relax_pass_0 = TRUE;
355 bfd_boolean skip_relax_pass_1 = TRUE;
356 bfd_vma gp = 0;
357
358 /* Assume we're not going to change any sizes, and we'll only need
359 one pass. */
360 *again = FALSE;
361
362 if (bfd_link_relocatable (link_info))
363 (*link_info->callbacks->einfo)
364 (_("%P%F: --relax and -r may not be used together\n"));
365
366 /* Don't even try to relax for non-ELF outputs. */
367 if (!is_elf_hash_table (link_info->hash))
368 return FALSE;
369
370 /* Nothing to do if there are no relocations or there is no need for
371 the current pass. */
372 if ((sec->flags & SEC_RELOC) == 0
373 || sec->reloc_count == 0
374 || (link_info->relax_pass == 0 && sec->skip_relax_pass_0)
375 || (link_info->relax_pass == 1 && sec->skip_relax_pass_1))
376 return TRUE;
377
378 ia64_info = elfNN_ia64_hash_table (link_info);
379 if (ia64_info == NULL)
380 return FALSE;
381
382 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
383
384 /* Load the relocations for this section. */
385 internal_relocs = (_bfd_elf_link_read_relocs
386 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
387 link_info->keep_memory));
388 if (internal_relocs == NULL)
389 return FALSE;
390
391 irelend = internal_relocs + sec->reloc_count;
392
393 /* Get the section contents. */
394 if (elf_section_data (sec)->this_hdr.contents != NULL)
395 contents = elf_section_data (sec)->this_hdr.contents;
396 else
397 {
398 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
399 goto error_return;
400 }
401
402 for (irel = internal_relocs; irel < irelend; irel++)
403 {
404 unsigned long r_type = ELFNN_R_TYPE (irel->r_info);
405 bfd_vma symaddr, reladdr, trampoff, toff, roff;
406 asection *tsec;
407 struct one_fixup *f;
408 bfd_size_type amt;
409 bfd_boolean is_branch;
410 struct elfNN_ia64_dyn_sym_info *dyn_i;
411 char symtype;
412
413 switch (r_type)
414 {
415 case R_IA64_PCREL21B:
416 case R_IA64_PCREL21BI:
417 case R_IA64_PCREL21M:
418 case R_IA64_PCREL21F:
419 /* In pass 1, all br relaxations are done. We can skip it. */
420 if (link_info->relax_pass == 1)
421 continue;
422 skip_relax_pass_0 = FALSE;
423 is_branch = TRUE;
424 break;
425
426 case R_IA64_PCREL60B:
427 /* We can't optimize brl to br in pass 0 since br relaxations
428 will increase the code size. Defer it to pass 1. */
429 if (link_info->relax_pass == 0)
430 {
431 skip_relax_pass_1 = FALSE;
432 continue;
433 }
434 is_branch = TRUE;
435 break;
436
437 case R_IA64_GPREL22:
438 /* Update max_short_sec/min_short_sec. */
439
440 case R_IA64_LTOFF22X:
441 case R_IA64_LDXMOV:
442 /* We can't relax ldx/mov in pass 0 since br relaxations will
443 increase the code size. Defer it to pass 1. */
444 if (link_info->relax_pass == 0)
445 {
446 skip_relax_pass_1 = FALSE;
447 continue;
448 }
449 is_branch = FALSE;
450 break;
451
452 default:
453 continue;
454 }
455
456 /* Get the value of the symbol referred to by the reloc. */
457 if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
458 {
459 /* A local symbol. */
460 Elf_Internal_Sym *isym;
461
462 /* Read this BFD's local symbols. */
463 if (isymbuf == NULL)
464 {
465 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
466 if (isymbuf == NULL)
467 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
468 symtab_hdr->sh_info, 0,
469 NULL, NULL, NULL);
470 if (isymbuf == 0)
471 goto error_return;
472 }
473
474 isym = isymbuf + ELFNN_R_SYM (irel->r_info);
475 if (isym->st_shndx == SHN_UNDEF)
476 continue; /* We can't do anything with undefined symbols. */
477 else if (isym->st_shndx == SHN_ABS)
478 tsec = bfd_abs_section_ptr;
479 else if (isym->st_shndx == SHN_COMMON)
480 tsec = bfd_com_section_ptr;
481 else if (isym->st_shndx == SHN_IA_64_ANSI_COMMON)
482 tsec = bfd_com_section_ptr;
483 else
484 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
485
486 toff = isym->st_value;
487 dyn_i = get_dyn_sym_info (ia64_info, NULL, abfd, irel, FALSE);
488 symtype = ELF_ST_TYPE (isym->st_info);
489 }
490 else
491 {
492 unsigned long indx;
493 struct elf_link_hash_entry *h;
494
495 indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
496 h = elf_sym_hashes (abfd)[indx];
497 BFD_ASSERT (h != NULL);
498
499 while (h->root.type == bfd_link_hash_indirect
500 || h->root.type == bfd_link_hash_warning)
501 h = (struct elf_link_hash_entry *) h->root.u.i.link;
502
503 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, FALSE);
504
505 /* For branches to dynamic symbols, we're interested instead
506 in a branch to the PLT entry. */
507 if (is_branch && dyn_i && dyn_i->want_plt2)
508 {
509 /* Internal branches shouldn't be sent to the PLT.
510 Leave this for now and we'll give an error later. */
511 if (r_type != R_IA64_PCREL21B)
512 continue;
513
514 tsec = ia64_info->root.splt;
515 toff = dyn_i->plt2_offset;
516 BFD_ASSERT (irel->r_addend == 0);
517 }
518
519 /* Can't do anything else with dynamic symbols. */
520 else if (elfNN_ia64_dynamic_symbol_p (h, link_info, r_type))
521 continue;
522
523 else
524 {
525 /* We can't do anything with undefined symbols. */
526 if (h->root.type == bfd_link_hash_undefined
527 || h->root.type == bfd_link_hash_undefweak)
528 continue;
529
530 tsec = h->root.u.def.section;
531 toff = h->root.u.def.value;
532 }
533
534 symtype = h->type;
535 }
536
537 if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
538 {
539 /* At this stage in linking, no SEC_MERGE symbol has been
540 adjusted, so all references to such symbols need to be
541 passed through _bfd_merged_section_offset. (Later, in
542 relocate_section, all SEC_MERGE symbols *except* for
543 section symbols have been adjusted.)
544
545 gas may reduce relocations against symbols in SEC_MERGE
546 sections to a relocation against the section symbol when
547 the original addend was zero. When the reloc is against
548 a section symbol we should include the addend in the
549 offset passed to _bfd_merged_section_offset, since the
550 location of interest is the original symbol. On the
551 other hand, an access to "sym+addend" where "sym" is not
552 a section symbol should not include the addend; Such an
553 access is presumed to be an offset from "sym"; The
554 location of interest is just "sym". */
555 if (symtype == STT_SECTION)
556 toff += irel->r_addend;
557
558 toff = _bfd_merged_section_offset (abfd, &tsec,
559 elf_section_data (tsec)->sec_info,
560 toff);
561
562 if (symtype != STT_SECTION)
563 toff += irel->r_addend;
564 }
565 else
566 toff += irel->r_addend;
567
568 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
569
570 roff = irel->r_offset;
571
572 if (is_branch)
573 {
574 bfd_signed_vma offset;
575
576 reladdr = (sec->output_section->vma
577 + sec->output_offset
578 + roff) & (bfd_vma) -4;
579
580 /* The .plt section is aligned at 32byte and the .text section
581 is aligned at 64byte. The .text section is right after the
582 .plt section. After the first relaxation pass, linker may
583 increase the gap between the .plt and .text sections up
584 to 32byte. We assume linker will always insert 32byte
585 between the .plt and .text sections after the first
586 relaxation pass. */
587 if (tsec == ia64_info->root.splt)
588 offset = -0x1000000 + 32;
589 else
590 offset = -0x1000000;
591
592 /* If the branch is in range, no need to do anything. */
593 if ((bfd_signed_vma) (symaddr - reladdr) >= offset
594 && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
595 {
596 /* If the 60-bit branch is in 21-bit range, optimize it. */
597 if (r_type == R_IA64_PCREL60B)
598 {
599 ia64_elf_relax_brl (contents, roff);
600
601 irel->r_info
602 = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
603 R_IA64_PCREL21B);
604
605 /* If the original relocation offset points to slot
606 1, change it to slot 2. */
607 if ((irel->r_offset & 3) == 1)
608 irel->r_offset += 1;
609
610 changed_contents = TRUE;
611 changed_relocs = TRUE;
612 }
613
614 continue;
615 }
616 else if (r_type == R_IA64_PCREL60B)
617 continue;
618 else if (ia64_elf_relax_br (contents, roff))
619 {
620 irel->r_info
621 = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
622 R_IA64_PCREL60B);
623
624 /* Make the relocation offset point to slot 1. */
625 irel->r_offset = (irel->r_offset & ~((bfd_vma) 0x3)) + 1;
626
627 changed_contents = TRUE;
628 changed_relocs = TRUE;
629 continue;
630 }
631
632 /* We can't put a trampoline in a .init/.fini section. Issue
633 an error. */
634 if (strcmp (sec->output_section->name, ".init") == 0
635 || strcmp (sec->output_section->name, ".fini") == 0)
636 {
637 _bfd_error_handler
638 /* xgettext:c-format */
639 (_("%pB: can't relax br at %#" PRIx64 " in section `%pA';"
640 " please use brl or indirect branch"),
641 sec->owner, (uint64_t) roff, sec);
642 bfd_set_error (bfd_error_bad_value);
643 goto error_return;
644 }
645
646 /* If the branch and target are in the same section, you've
647 got one honking big section and we can't help you unless
648 you are branching backwards. You'll get an error message
649 later. */
650 if (tsec == sec && toff > roff)
651 continue;
652
653 /* Look for an existing fixup to this address. */
654 for (f = fixups; f ; f = f->next)
655 if (f->tsec == tsec && f->toff == toff)
656 break;
657
658 if (f == NULL)
659 {
660 /* Two alternatives: If it's a branch to a PLT entry, we can
661 make a copy of the FULL_PLT entry. Otherwise, we'll have
662 to use a `brl' insn to get where we're going. */
663
664 size_t size;
665
666 if (tsec == ia64_info->root.splt)
667 size = sizeof (plt_full_entry);
668 else
669 size = oor_branch_size;
670
671 /* Resize the current section to make room for the new branch. */
672 trampoff = (sec->size + 15) & (bfd_vma) -16;
673
674 /* If trampoline is out of range, there is nothing we
675 can do. */
676 offset = trampoff - (roff & (bfd_vma) -4);
677 if (offset < -0x1000000 || offset > 0x0FFFFF0)
678 continue;
679
680 amt = trampoff + size;
681 contents = (bfd_byte *) bfd_realloc (contents, amt);
682 if (contents == NULL)
683 goto error_return;
684 sec->size = amt;
685
686 if (tsec == ia64_info->root.splt)
687 {
688 memcpy (contents + trampoff, plt_full_entry, size);
689
690 /* Hijack the old relocation for use as the PLTOFF reloc. */
691 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
692 R_IA64_PLTOFF22);
693 irel->r_offset = trampoff;
694 }
695 else
696 {
697 if (size == sizeof (oor_ip))
698 {
699 memcpy (contents + trampoff, oor_ip, size);
700 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
701 R_IA64_PCREL64I);
702 irel->r_addend -= 16;
703 irel->r_offset = trampoff + 2;
704 }
705 else
706 {
707 memcpy (contents + trampoff, oor_brl, size);
708 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
709 R_IA64_PCREL60B);
710 irel->r_offset = trampoff + 2;
711 }
712
713 }
714
715 /* Record the fixup so we don't do it again this section. */
716 f = (struct one_fixup *)
717 bfd_malloc ((bfd_size_type) sizeof (*f));
718 f->next = fixups;
719 f->tsec = tsec;
720 f->toff = toff;
721 f->trampoff = trampoff;
722 fixups = f;
723 }
724 else
725 {
726 /* If trampoline is out of range, there is nothing we
727 can do. */
728 offset = f->trampoff - (roff & (bfd_vma) -4);
729 if (offset < -0x1000000 || offset > 0x0FFFFF0)
730 continue;
731
732 /* Nop out the reloc, since we're finalizing things here. */
733 irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
734 }
735
736 /* Fix up the existing branch to hit the trampoline. */
737 if (ia64_elf_install_value (contents + roff, offset, r_type)
738 != bfd_reloc_ok)
739 goto error_return;
740
741 changed_contents = TRUE;
742 changed_relocs = TRUE;
743 }
744 else
745 {
746 /* Fetch the gp. */
747 if (gp == 0)
748 {
749 bfd *obfd = sec->output_section->owner;
750 gp = _bfd_get_gp_value (obfd);
751 if (gp == 0)
752 {
753 if (!elfNN_ia64_choose_gp (obfd, link_info, FALSE))
754 goto error_return;
755 gp = _bfd_get_gp_value (obfd);
756 }
757 }
758
759 /* If the data is out of range, do nothing. */
760 if ((bfd_signed_vma) (symaddr - gp) >= 0x200000
761 ||(bfd_signed_vma) (symaddr - gp) < -0x200000)
762 continue;
763
764 if (r_type == R_IA64_GPREL22)
765 elfNN_ia64_update_short_info (tsec->output_section,
766 tsec->output_offset + toff,
767 ia64_info);
768 else if (r_type == R_IA64_LTOFF22X)
769 {
770 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
771 R_IA64_GPREL22);
772 changed_relocs = TRUE;
773 if (dyn_i->want_gotx)
774 {
775 dyn_i->want_gotx = 0;
776 changed_got |= !dyn_i->want_got;
777 }
778
779 elfNN_ia64_update_short_info (tsec->output_section,
780 tsec->output_offset + toff,
781 ia64_info);
782 }
783 else
784 {
785 ia64_elf_relax_ldxmov (contents, roff);
786 irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
787 changed_contents = TRUE;
788 changed_relocs = TRUE;
789 }
790 }
791 }
792
793 /* ??? If we created fixups, this may push the code segment large
794 enough that the data segment moves, which will change the GP.
795 Reset the GP so that we re-calculate next round. We need to
796 do this at the _beginning_ of the next round; now will not do. */
797
798 /* Clean up and go home. */
799 while (fixups)
800 {
801 struct one_fixup *f = fixups;
802 fixups = fixups->next;
803 free (f);
804 }
805
806 if (isymbuf != NULL
807 && symtab_hdr->contents != (unsigned char *) isymbuf)
808 {
809 if (! link_info->keep_memory)
810 free (isymbuf);
811 else
812 {
813 /* Cache the symbols for elf_link_input_bfd. */
814 symtab_hdr->contents = (unsigned char *) isymbuf;
815 }
816 }
817
818 if (contents != NULL
819 && elf_section_data (sec)->this_hdr.contents != contents)
820 {
821 if (!changed_contents && !link_info->keep_memory)
822 free (contents);
823 else
824 {
825 /* Cache the section contents for elf_link_input_bfd. */
826 elf_section_data (sec)->this_hdr.contents = contents;
827 }
828 }
829
830 if (elf_section_data (sec)->relocs != internal_relocs)
831 {
832 if (!changed_relocs)
833 free (internal_relocs);
834 else
835 elf_section_data (sec)->relocs = internal_relocs;
836 }
837
838 if (changed_got)
839 {
840 struct elfNN_ia64_allocate_data data;
841 data.info = link_info;
842 data.ofs = 0;
843 ia64_info->self_dtpmod_offset = (bfd_vma) -1;
844
845 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
846 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
847 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
848 ia64_info->root.sgot->size = data.ofs;
849
850 if (ia64_info->root.dynamic_sections_created
851 && ia64_info->root.srelgot != NULL)
852 {
853 /* Resize .rela.got. */
854 ia64_info->root.srelgot->size = 0;
855 if (bfd_link_pic (link_info)
856 && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
857 ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
858 data.only_got = TRUE;
859 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries,
860 &data);
861 }
862 }
863
864 if (link_info->relax_pass == 0)
865 {
866 /* Pass 0 is only needed to relax br. */
867 sec->skip_relax_pass_0 = skip_relax_pass_0;
868 sec->skip_relax_pass_1 = skip_relax_pass_1;
869 }
870
871 *again = changed_contents || changed_relocs;
872 return TRUE;
873
874 error_return:
875 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
876 free (isymbuf);
877 if (contents != NULL
878 && elf_section_data (sec)->this_hdr.contents != contents)
879 free (contents);
880 if (internal_relocs != NULL
881 && elf_section_data (sec)->relocs != internal_relocs)
882 free (internal_relocs);
883 return FALSE;
884 }
885 #undef skip_relax_pass_0
886 #undef skip_relax_pass_1
887
888 /* Return TRUE if NAME is an unwind table section name. */
890
891 static inline bfd_boolean
892 is_unwind_section_name (bfd *abfd, const char *name)
893 {
894 if (elfNN_ia64_hpux_vec (abfd->xvec)
895 && !strcmp (name, ELF_STRING_ia64_unwind_hdr))
896 return FALSE;
897
898 return ((CONST_STRNEQ (name, ELF_STRING_ia64_unwind)
899 && ! CONST_STRNEQ (name, ELF_STRING_ia64_unwind_info))
900 || CONST_STRNEQ (name, ELF_STRING_ia64_unwind_once));
901 }
902
903 /* Handle an IA-64 specific section when reading an object file. This
904 is called when bfd_section_from_shdr finds a section with an unknown
905 type. */
906
907 static bfd_boolean
908 elfNN_ia64_section_from_shdr (bfd *abfd,
909 Elf_Internal_Shdr *hdr,
910 const char *name,
911 int shindex)
912 {
913 /* There ought to be a place to keep ELF backend specific flags, but
914 at the moment there isn't one. We just keep track of the
915 sections by their name, instead. Fortunately, the ABI gives
916 suggested names for all the MIPS specific sections, so we will
917 probably get away with this. */
918 switch (hdr->sh_type)
919 {
920 case SHT_IA_64_UNWIND:
921 case SHT_IA_64_HP_OPT_ANOT:
922 break;
923
924 case SHT_IA_64_EXT:
925 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
926 return FALSE;
927 break;
928
929 default:
930 return FALSE;
931 }
932
933 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
934 return FALSE;
935
936 return TRUE;
937 }
938
939 /* Convert IA-64 specific section flags to bfd internal section flags. */
940
941 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
942 flag. */
943
944 static bfd_boolean
945 elfNN_ia64_section_flags (flagword *flags,
946 const Elf_Internal_Shdr *hdr)
947 {
948 if (hdr->sh_flags & SHF_IA_64_SHORT)
949 *flags |= SEC_SMALL_DATA;
950
951 return TRUE;
952 }
953
954 /* Set the correct type for an IA-64 ELF section. We do this by the
955 section name, which is a hack, but ought to work. */
956
957 static bfd_boolean
958 elfNN_ia64_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr,
959 asection *sec)
960 {
961 const char *name;
962
963 name = bfd_get_section_name (abfd, sec);
964
965 if (is_unwind_section_name (abfd, name))
966 {
967 /* We don't have the sections numbered at this point, so sh_info
968 is set later, in elfNN_ia64_final_write_processing. */
969 hdr->sh_type = SHT_IA_64_UNWIND;
970 hdr->sh_flags |= SHF_LINK_ORDER;
971 }
972 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
973 hdr->sh_type = SHT_IA_64_EXT;
974 else if (strcmp (name, ".HP.opt_annot") == 0)
975 hdr->sh_type = SHT_IA_64_HP_OPT_ANOT;
976 else if (strcmp (name, ".reloc") == 0)
977 /* This is an ugly, but unfortunately necessary hack that is
978 needed when producing EFI binaries on IA-64. It tells
979 elf.c:elf_fake_sections() not to consider ".reloc" as a section
980 containing ELF relocation info. We need this hack in order to
981 be able to generate ELF binaries that can be translated into
982 EFI applications (which are essentially COFF objects). Those
983 files contain a COFF ".reloc" section inside an ELFNN object,
984 which would normally cause BFD to segfault because it would
985 attempt to interpret this section as containing relocation
986 entries for section "oc". With this hack enabled, ".reloc"
987 will be treated as a normal data section, which will avoid the
988 segfault. However, you won't be able to create an ELFNN binary
989 with a section named "oc" that needs relocations, but that's
990 the kind of ugly side-effects you get when detecting section
991 types based on their names... In practice, this limitation is
992 unlikely to bite. */
993 hdr->sh_type = SHT_PROGBITS;
994
995 if (sec->flags & SEC_SMALL_DATA)
996 hdr->sh_flags |= SHF_IA_64_SHORT;
997
998 /* Some HP linkers look for the SHF_IA_64_HP_TLS flag instead of SHF_TLS. */
999
1000 if (elfNN_ia64_hpux_vec (abfd->xvec) && (sec->flags & SHF_TLS))
1001 hdr->sh_flags |= SHF_IA_64_HP_TLS;
1002
1003 return TRUE;
1004 }
1005
1006 /* The final processing done just before writing out an IA-64 ELF
1007 object file. */
1008
1009 static void
1010 elfNN_ia64_final_write_processing (bfd *abfd,
1011 bfd_boolean linker ATTRIBUTE_UNUSED)
1012 {
1013 Elf_Internal_Shdr *hdr;
1014 asection *s;
1015
1016 for (s = abfd->sections; s; s = s->next)
1017 {
1018 hdr = &elf_section_data (s)->this_hdr;
1019 switch (hdr->sh_type)
1020 {
1021 case SHT_IA_64_UNWIND:
1022 /* The IA-64 processor-specific ABI requires setting sh_link
1023 to the unwind section, whereas HP-UX requires sh_info to
1024 do so. For maximum compatibility, we'll set both for
1025 now... */
1026 hdr->sh_info = hdr->sh_link;
1027 break;
1028 }
1029 }
1030
1031 if (! elf_flags_init (abfd))
1032 {
1033 unsigned long flags = 0;
1034
1035 if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
1036 flags |= EF_IA_64_BE;
1037 if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
1038 flags |= EF_IA_64_ABI64;
1039
1040 elf_elfheader(abfd)->e_flags = flags;
1041 elf_flags_init (abfd) = TRUE;
1042 }
1043 }
1044
1045 /* Hook called by the linker routine which adds symbols from an object
1046 file. We use it to put .comm items in .sbss, and not .bss. */
1047
1048 static bfd_boolean
1049 elfNN_ia64_add_symbol_hook (bfd *abfd,
1050 struct bfd_link_info *info,
1051 Elf_Internal_Sym *sym,
1052 const char **namep ATTRIBUTE_UNUSED,
1053 flagword *flagsp ATTRIBUTE_UNUSED,
1054 asection **secp,
1055 bfd_vma *valp)
1056 {
1057 if (sym->st_shndx == SHN_COMMON
1058 && !bfd_link_relocatable (info)
1059 && sym->st_size <= elf_gp_size (abfd))
1060 {
1061 /* Common symbols less than or equal to -G nn bytes are
1062 automatically put into .sbss. */
1063
1064 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1065
1066 if (scomm == NULL)
1067 {
1068 scomm = bfd_make_section_with_flags (abfd, ".scommon",
1069 (SEC_ALLOC
1070 | SEC_IS_COMMON
1071 | SEC_LINKER_CREATED));
1072 if (scomm == NULL)
1073 return FALSE;
1074 }
1075
1076 *secp = scomm;
1077 *valp = sym->st_size;
1078 }
1079
1080 return TRUE;
1081 }
1082
1083 /* Return the number of additional phdrs we will need. */
1084
1085 static int
1086 elfNN_ia64_additional_program_headers (bfd *abfd,
1087 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1088 {
1089 asection *s;
1090 int ret = 0;
1091
1092 /* See if we need a PT_IA_64_ARCHEXT segment. */
1093 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1094 if (s && (s->flags & SEC_LOAD))
1095 ++ret;
1096
1097 /* Count how many PT_IA_64_UNWIND segments we need. */
1098 for (s = abfd->sections; s; s = s->next)
1099 if (is_unwind_section_name (abfd, s->name) && (s->flags & SEC_LOAD))
1100 ++ret;
1101
1102 return ret;
1103 }
1104
1105 static bfd_boolean
1106 elfNN_ia64_modify_segment_map (bfd *abfd,
1107 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1108 {
1109 struct elf_segment_map *m, **pm;
1110 Elf_Internal_Shdr *hdr;
1111 asection *s;
1112
1113 /* If we need a PT_IA_64_ARCHEXT segment, it must come before
1114 all PT_LOAD segments. */
1115 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1116 if (s && (s->flags & SEC_LOAD))
1117 {
1118 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
1119 if (m->p_type == PT_IA_64_ARCHEXT)
1120 break;
1121 if (m == NULL)
1122 {
1123 m = ((struct elf_segment_map *)
1124 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1125 if (m == NULL)
1126 return FALSE;
1127
1128 m->p_type = PT_IA_64_ARCHEXT;
1129 m->count = 1;
1130 m->sections[0] = s;
1131
1132 /* We want to put it after the PHDR and INTERP segments. */
1133 pm = &elf_seg_map (abfd);
1134 while (*pm != NULL
1135 && ((*pm)->p_type == PT_PHDR
1136 || (*pm)->p_type == PT_INTERP))
1137 pm = &(*pm)->next;
1138
1139 m->next = *pm;
1140 *pm = m;
1141 }
1142 }
1143
1144 /* Install PT_IA_64_UNWIND segments, if needed. */
1145 for (s = abfd->sections; s; s = s->next)
1146 {
1147 hdr = &elf_section_data (s)->this_hdr;
1148 if (hdr->sh_type != SHT_IA_64_UNWIND)
1149 continue;
1150
1151 if (s && (s->flags & SEC_LOAD))
1152 {
1153 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
1154 if (m->p_type == PT_IA_64_UNWIND)
1155 {
1156 int i;
1157
1158 /* Look through all sections in the unwind segment
1159 for a match since there may be multiple sections
1160 to a segment. */
1161 for (i = m->count - 1; i >= 0; --i)
1162 if (m->sections[i] == s)
1163 break;
1164
1165 if (i >= 0)
1166 break;
1167 }
1168
1169 if (m == NULL)
1170 {
1171 m = ((struct elf_segment_map *)
1172 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
1173 if (m == NULL)
1174 return FALSE;
1175
1176 m->p_type = PT_IA_64_UNWIND;
1177 m->count = 1;
1178 m->sections[0] = s;
1179 m->next = NULL;
1180
1181 /* We want to put it last. */
1182 pm = &elf_seg_map (abfd);
1183 while (*pm != NULL)
1184 pm = &(*pm)->next;
1185 *pm = m;
1186 }
1187 }
1188 }
1189
1190 return TRUE;
1191 }
1192
1193 /* Turn on PF_IA_64_NORECOV if needed. This involves traversing all of
1194 the input sections for each output section in the segment and testing
1195 for SHF_IA_64_NORECOV on each. */
1196
1197 static bfd_boolean
1198 elfNN_ia64_modify_program_headers (bfd *abfd,
1199 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1200 {
1201 struct elf_obj_tdata *tdata = elf_tdata (abfd);
1202 struct elf_segment_map *m;
1203 Elf_Internal_Phdr *p;
1204
1205 for (p = tdata->phdr, m = elf_seg_map (abfd); m != NULL; m = m->next, p++)
1206 if (m->p_type == PT_LOAD)
1207 {
1208 int i;
1209 for (i = m->count - 1; i >= 0; --i)
1210 {
1211 struct bfd_link_order *order = m->sections[i]->map_head.link_order;
1212
1213 while (order != NULL)
1214 {
1215 if (order->type == bfd_indirect_link_order)
1216 {
1217 asection *is = order->u.indirect.section;
1218 bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
1219 if (flags & SHF_IA_64_NORECOV)
1220 {
1221 p->p_flags |= PF_IA_64_NORECOV;
1222 goto found;
1223 }
1224 }
1225 order = order->next;
1226 }
1227 }
1228 found:;
1229 }
1230
1231 return TRUE;
1232 }
1233
1234 /* According to the Tahoe assembler spec, all labels starting with a
1235 '.' are local. */
1236
1237 static bfd_boolean
1238 elfNN_ia64_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
1239 const char *name)
1240 {
1241 return name[0] == '.';
1242 }
1243
1244 /* Should we do dynamic things to this symbol? */
1245
1246 static bfd_boolean
1247 elfNN_ia64_dynamic_symbol_p (struct elf_link_hash_entry *h,
1248 struct bfd_link_info *info, int r_type)
1249 {
1250 bfd_boolean ignore_protected
1251 = ((r_type & 0xf8) == 0x40 /* FPTR relocs */
1252 || (r_type & 0xf8) == 0x50); /* LTOFF_FPTR relocs */
1253
1254 return _bfd_elf_dynamic_symbol_p (h, info, ignore_protected);
1255 }
1256
1257 static struct bfd_hash_entry*
1259 elfNN_ia64_new_elf_hash_entry (struct bfd_hash_entry *entry,
1260 struct bfd_hash_table *table,
1261 const char *string)
1262 {
1263 struct elfNN_ia64_link_hash_entry *ret;
1264 ret = (struct elfNN_ia64_link_hash_entry *) entry;
1265
1266 /* Allocate the structure if it has not already been allocated by a
1267 subclass. */
1268 if (!ret)
1269 ret = bfd_hash_allocate (table, sizeof (*ret));
1270
1271 if (!ret)
1272 return 0;
1273
1274 /* Call the allocation method of the superclass. */
1275 ret = ((struct elfNN_ia64_link_hash_entry *)
1276 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1277 table, string));
1278
1279 ret->info = NULL;
1280 ret->count = 0;
1281 ret->sorted_count = 0;
1282 ret->size = 0;
1283 return (struct bfd_hash_entry *) ret;
1284 }
1285
1286 static void
1287 elfNN_ia64_hash_copy_indirect (struct bfd_link_info *info,
1288 struct elf_link_hash_entry *xdir,
1289 struct elf_link_hash_entry *xind)
1290 {
1291 struct elfNN_ia64_link_hash_entry *dir, *ind;
1292
1293 dir = (struct elfNN_ia64_link_hash_entry *) xdir;
1294 ind = (struct elfNN_ia64_link_hash_entry *) xind;
1295
1296 /* Copy down any references that we may have already seen to the
1297 symbol which just became indirect. */
1298
1299 if (dir->root.versioned != versioned_hidden)
1300 dir->root.ref_dynamic |= ind->root.ref_dynamic;
1301 dir->root.ref_regular |= ind->root.ref_regular;
1302 dir->root.ref_regular_nonweak |= ind->root.ref_regular_nonweak;
1303 dir->root.needs_plt |= ind->root.needs_plt;
1304
1305 if (ind->root.root.type != bfd_link_hash_indirect)
1306 return;
1307
1308 /* Copy over the got and plt data. This would have been done
1309 by check_relocs. */
1310
1311 if (ind->info != NULL)
1312 {
1313 struct elfNN_ia64_dyn_sym_info *dyn_i;
1314 unsigned int count;
1315
1316 if (dir->info)
1317 free (dir->info);
1318
1319 dir->info = ind->info;
1320 dir->count = ind->count;
1321 dir->sorted_count = ind->sorted_count;
1322 dir->size = ind->size;
1323
1324 ind->info = NULL;
1325 ind->count = 0;
1326 ind->sorted_count = 0;
1327 ind->size = 0;
1328
1329 /* Fix up the dyn_sym_info pointers to the global symbol. */
1330 for (count = dir->count, dyn_i = dir->info;
1331 count != 0;
1332 count--, dyn_i++)
1333 dyn_i->h = &dir->root;
1334 }
1335
1336 /* Copy over the dynindx. */
1337
1338 if (ind->root.dynindx != -1)
1339 {
1340 if (dir->root.dynindx != -1)
1341 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1342 dir->root.dynstr_index);
1343 dir->root.dynindx = ind->root.dynindx;
1344 dir->root.dynstr_index = ind->root.dynstr_index;
1345 ind->root.dynindx = -1;
1346 ind->root.dynstr_index = 0;
1347 }
1348 }
1349
1350 static void
1351 elfNN_ia64_hash_hide_symbol (struct bfd_link_info *info,
1352 struct elf_link_hash_entry *xh,
1353 bfd_boolean force_local)
1354 {
1355 struct elfNN_ia64_link_hash_entry *h;
1356 struct elfNN_ia64_dyn_sym_info *dyn_i;
1357 unsigned int count;
1358
1359 h = (struct elfNN_ia64_link_hash_entry *)xh;
1360
1361 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
1362
1363 for (count = h->count, dyn_i = h->info;
1364 count != 0;
1365 count--, dyn_i++)
1366 {
1367 dyn_i->want_plt2 = 0;
1368 dyn_i->want_plt = 0;
1369 }
1370 }
1371
1372 /* Compute a hash of a local hash entry. */
1373
1374 static hashval_t
1375 elfNN_ia64_local_htab_hash (const void *ptr)
1376 {
1377 struct elfNN_ia64_local_hash_entry *entry
1378 = (struct elfNN_ia64_local_hash_entry *) ptr;
1379
1380 return ELF_LOCAL_SYMBOL_HASH (entry->id, entry->r_sym);
1381 }
1382
1383 /* Compare local hash entries. */
1384
1385 static int
1386 elfNN_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
1387 {
1388 struct elfNN_ia64_local_hash_entry *entry1
1389 = (struct elfNN_ia64_local_hash_entry *) ptr1;
1390 struct elfNN_ia64_local_hash_entry *entry2
1391 = (struct elfNN_ia64_local_hash_entry *) ptr2;
1392
1393 return entry1->id == entry2->id && entry1->r_sym == entry2->r_sym;
1394 }
1395
1396 /* Free the global elfNN_ia64_dyn_sym_info array. */
1397
1398 static bfd_boolean
1399 elfNN_ia64_global_dyn_info_free (void **xentry,
1400 void * unused ATTRIBUTE_UNUSED)
1401 {
1402 struct elfNN_ia64_link_hash_entry *entry
1403 = (struct elfNN_ia64_link_hash_entry *) xentry;
1404
1405 if (entry->info)
1406 {
1407 free (entry->info);
1408 entry->info = NULL;
1409 entry->count = 0;
1410 entry->sorted_count = 0;
1411 entry->size = 0;
1412 }
1413
1414 return TRUE;
1415 }
1416
1417 /* Free the local elfNN_ia64_dyn_sym_info array. */
1418
1419 static bfd_boolean
1420 elfNN_ia64_local_dyn_info_free (void **slot,
1421 void * unused ATTRIBUTE_UNUSED)
1422 {
1423 struct elfNN_ia64_local_hash_entry *entry
1424 = (struct elfNN_ia64_local_hash_entry *) *slot;
1425
1426 if (entry->info)
1427 {
1428 free (entry->info);
1429 entry->info = NULL;
1430 entry->count = 0;
1431 entry->sorted_count = 0;
1432 entry->size = 0;
1433 }
1434
1435 return TRUE;
1436 }
1437
1438 /* Destroy IA-64 linker hash table. */
1439
1440 static void
1441 elfNN_ia64_link_hash_table_free (bfd *obfd)
1442 {
1443 struct elfNN_ia64_link_hash_table *ia64_info
1444 = (struct elfNN_ia64_link_hash_table *) obfd->link.hash;
1445 if (ia64_info->loc_hash_table)
1446 {
1447 htab_traverse (ia64_info->loc_hash_table,
1448 elfNN_ia64_local_dyn_info_free, NULL);
1449 htab_delete (ia64_info->loc_hash_table);
1450 }
1451 if (ia64_info->loc_hash_memory)
1452 objalloc_free ((struct objalloc *) ia64_info->loc_hash_memory);
1453 elf_link_hash_traverse (&ia64_info->root,
1454 elfNN_ia64_global_dyn_info_free, NULL);
1455 _bfd_elf_link_hash_table_free (obfd);
1456 }
1457
1458 /* Create the derived linker hash table. The IA-64 ELF port uses this
1459 derived hash table to keep information specific to the IA-64 ElF
1460 linker (without using static variables). */
1461
1462 static struct bfd_link_hash_table *
1463 elfNN_ia64_hash_table_create (bfd *abfd)
1464 {
1465 struct elfNN_ia64_link_hash_table *ret;
1466
1467 ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
1468 if (!ret)
1469 return NULL;
1470
1471 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
1472 elfNN_ia64_new_elf_hash_entry,
1473 sizeof (struct elfNN_ia64_link_hash_entry),
1474 IA64_ELF_DATA))
1475 {
1476 free (ret);
1477 return NULL;
1478 }
1479
1480 ret->loc_hash_table = htab_try_create (1024, elfNN_ia64_local_htab_hash,
1481 elfNN_ia64_local_htab_eq, NULL);
1482 ret->loc_hash_memory = objalloc_create ();
1483 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1484 {
1485 elfNN_ia64_link_hash_table_free (abfd);
1486 return NULL;
1487 }
1488 ret->root.root.hash_table_free = elfNN_ia64_link_hash_table_free;
1489
1490 return &ret->root.root;
1491 }
1492
1493 /* Traverse both local and global hash tables. */
1494
1495 struct elfNN_ia64_dyn_sym_traverse_data
1496 {
1497 bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, void *);
1498 void * data;
1499 };
1500
1501 static bfd_boolean
1502 elfNN_ia64_global_dyn_sym_thunk (struct bfd_hash_entry *xentry,
1503 void * xdata)
1504 {
1505 struct elfNN_ia64_link_hash_entry *entry
1506 = (struct elfNN_ia64_link_hash_entry *) xentry;
1507 struct elfNN_ia64_dyn_sym_traverse_data *data
1508 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1509 struct elfNN_ia64_dyn_sym_info *dyn_i;
1510 unsigned int count;
1511
1512 for (count = entry->count, dyn_i = entry->info;
1513 count != 0;
1514 count--, dyn_i++)
1515 if (! (*data->func) (dyn_i, data->data))
1516 return FALSE;
1517 return TRUE;
1518 }
1519
1520 static bfd_boolean
1521 elfNN_ia64_local_dyn_sym_thunk (void **slot, void * xdata)
1522 {
1523 struct elfNN_ia64_local_hash_entry *entry
1524 = (struct elfNN_ia64_local_hash_entry *) *slot;
1525 struct elfNN_ia64_dyn_sym_traverse_data *data
1526 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
1527 struct elfNN_ia64_dyn_sym_info *dyn_i;
1528 unsigned int count;
1529
1530 for (count = entry->count, dyn_i = entry->info;
1531 count != 0;
1532 count--, dyn_i++)
1533 if (! (*data->func) (dyn_i, data->data))
1534 return FALSE;
1535 return TRUE;
1536 }
1537
1538 static void
1539 elfNN_ia64_dyn_sym_traverse (struct elfNN_ia64_link_hash_table *ia64_info,
1540 bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, void *),
1541 void * data)
1542 {
1543 struct elfNN_ia64_dyn_sym_traverse_data xdata;
1544
1545 xdata.func = func;
1546 xdata.data = data;
1547
1548 elf_link_hash_traverse (&ia64_info->root,
1549 elfNN_ia64_global_dyn_sym_thunk, &xdata);
1550 htab_traverse (ia64_info->loc_hash_table,
1551 elfNN_ia64_local_dyn_sym_thunk, &xdata);
1552 }
1553
1554 static bfd_boolean
1556 elfNN_ia64_create_dynamic_sections (bfd *abfd,
1557 struct bfd_link_info *info)
1558 {
1559 struct elfNN_ia64_link_hash_table *ia64_info;
1560 asection *s;
1561
1562 if (! _bfd_elf_create_dynamic_sections (abfd, info))
1563 return FALSE;
1564
1565 ia64_info = elfNN_ia64_hash_table (info);
1566 if (ia64_info == NULL)
1567 return FALSE;
1568
1569 {
1570 flagword flags = bfd_get_section_flags (abfd, ia64_info->root.sgot);
1571 bfd_set_section_flags (abfd, ia64_info->root.sgot,
1572 SEC_SMALL_DATA | flags);
1573 /* The .got section is always aligned at 8 bytes. */
1574 if (! bfd_set_section_alignment (abfd, ia64_info->root.sgot, 3))
1575 return FALSE;
1576 }
1577
1578 if (!get_pltoff (abfd, info, ia64_info))
1579 return FALSE;
1580
1581 s = bfd_make_section_anyway_with_flags (abfd, ".rela.IA_64.pltoff",
1582 (SEC_ALLOC | SEC_LOAD
1583 | SEC_HAS_CONTENTS
1584 | SEC_IN_MEMORY
1585 | SEC_LINKER_CREATED
1586 | SEC_READONLY));
1587 if (s == NULL
1588 || !bfd_set_section_alignment (abfd, s, LOG_SECTION_ALIGN))
1589 return FALSE;
1590 ia64_info->rel_pltoff_sec = s;
1591
1592 return TRUE;
1593 }
1594
1595 /* Find and/or create a hash entry for local symbol. */
1596 static struct elfNN_ia64_local_hash_entry *
1597 get_local_sym_hash (struct elfNN_ia64_link_hash_table *ia64_info,
1598 bfd *abfd, const Elf_Internal_Rela *rel,
1599 bfd_boolean create)
1600 {
1601 struct elfNN_ia64_local_hash_entry e, *ret;
1602 asection *sec = abfd->sections;
1603 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1604 ELFNN_R_SYM (rel->r_info));
1605 void **slot;
1606
1607 e.id = sec->id;
1608 e.r_sym = ELFNN_R_SYM (rel->r_info);
1609 slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
1610 create ? INSERT : NO_INSERT);
1611
1612 if (!slot)
1613 return NULL;
1614
1615 if (*slot)
1616 return (struct elfNN_ia64_local_hash_entry *) *slot;
1617
1618 ret = (struct elfNN_ia64_local_hash_entry *)
1619 objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
1620 sizeof (struct elfNN_ia64_local_hash_entry));
1621 if (ret)
1622 {
1623 memset (ret, 0, sizeof (*ret));
1624 ret->id = sec->id;
1625 ret->r_sym = ELFNN_R_SYM (rel->r_info);
1626 *slot = ret;
1627 }
1628 return ret;
1629 }
1630
1631 /* Used to sort elfNN_ia64_dyn_sym_info array. */
1632
1633 static int
1634 addend_compare (const void *xp, const void *yp)
1635 {
1636 const struct elfNN_ia64_dyn_sym_info *x
1637 = (const struct elfNN_ia64_dyn_sym_info *) xp;
1638 const struct elfNN_ia64_dyn_sym_info *y
1639 = (const struct elfNN_ia64_dyn_sym_info *) yp;
1640
1641 return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
1642 }
1643
1644 /* Sort elfNN_ia64_dyn_sym_info array and remove duplicates. */
1645
1646 static unsigned int
1647 sort_dyn_sym_info (struct elfNN_ia64_dyn_sym_info *info,
1648 unsigned int count)
1649 {
1650 bfd_vma curr, prev, got_offset;
1651 unsigned int i, kept, dupes, diff, dest, src, len;
1652
1653 qsort (info, count, sizeof (*info), addend_compare);
1654
1655 /* Find the first duplicate. */
1656 prev = info [0].addend;
1657 got_offset = info [0].got_offset;
1658 for (i = 1; i < count; i++)
1659 {
1660 curr = info [i].addend;
1661 if (curr == prev)
1662 {
1663 /* For duplicates, make sure that GOT_OFFSET is valid. */
1664 if (got_offset == (bfd_vma) -1)
1665 got_offset = info [i].got_offset;
1666 break;
1667 }
1668 got_offset = info [i].got_offset;
1669 prev = curr;
1670 }
1671
1672 /* We may move a block of elements to here. */
1673 dest = i++;
1674
1675 /* Remove duplicates. */
1676 if (i < count)
1677 {
1678 while (i < count)
1679 {
1680 /* For duplicates, make sure that the kept one has a valid
1681 got_offset. */
1682 kept = dest - 1;
1683 if (got_offset != (bfd_vma) -1)
1684 info [kept].got_offset = got_offset;
1685
1686 curr = info [i].addend;
1687 got_offset = info [i].got_offset;
1688
1689 /* Move a block of elements whose first one is different from
1690 the previous. */
1691 if (curr == prev)
1692 {
1693 for (src = i + 1; src < count; src++)
1694 {
1695 if (info [src].addend != curr)
1696 break;
1697 /* For duplicates, make sure that GOT_OFFSET is
1698 valid. */
1699 if (got_offset == (bfd_vma) -1)
1700 got_offset = info [src].got_offset;
1701 }
1702
1703 /* Make sure that the kept one has a valid got_offset. */
1704 if (got_offset != (bfd_vma) -1)
1705 info [kept].got_offset = got_offset;
1706 }
1707 else
1708 src = i;
1709
1710 if (src >= count)
1711 break;
1712
1713 /* Find the next duplicate. SRC will be kept. */
1714 prev = info [src].addend;
1715 got_offset = info [src].got_offset;
1716 for (dupes = src + 1; dupes < count; dupes ++)
1717 {
1718 curr = info [dupes].addend;
1719 if (curr == prev)
1720 {
1721 /* Make sure that got_offset is valid. */
1722 if (got_offset == (bfd_vma) -1)
1723 got_offset = info [dupes].got_offset;
1724
1725 /* For duplicates, make sure that the kept one has
1726 a valid got_offset. */
1727 if (got_offset != (bfd_vma) -1)
1728 info [dupes - 1].got_offset = got_offset;
1729 break;
1730 }
1731 got_offset = info [dupes].got_offset;
1732 prev = curr;
1733 }
1734
1735 /* How much to move. */
1736 len = dupes - src;
1737 i = dupes + 1;
1738
1739 if (len == 1 && dupes < count)
1740 {
1741 /* If we only move 1 element, we combine it with the next
1742 one. There must be at least a duplicate. Find the
1743 next different one. */
1744 for (diff = dupes + 1, src++; diff < count; diff++, src++)
1745 {
1746 if (info [diff].addend != curr)
1747 break;
1748 /* Make sure that got_offset is valid. */
1749 if (got_offset == (bfd_vma) -1)
1750 got_offset = info [diff].got_offset;
1751 }
1752
1753 /* Makre sure that the last duplicated one has an valid
1754 offset. */
1755 BFD_ASSERT (curr == prev);
1756 if (got_offset != (bfd_vma) -1)
1757 info [diff - 1].got_offset = got_offset;
1758
1759 if (diff < count)
1760 {
1761 /* Find the next duplicate. Track the current valid
1762 offset. */
1763 prev = info [diff].addend;
1764 got_offset = info [diff].got_offset;
1765 for (dupes = diff + 1; dupes < count; dupes ++)
1766 {
1767 curr = info [dupes].addend;
1768 if (curr == prev)
1769 {
1770 /* For duplicates, make sure that GOT_OFFSET
1771 is valid. */
1772 if (got_offset == (bfd_vma) -1)
1773 got_offset = info [dupes].got_offset;
1774 break;
1775 }
1776 got_offset = info [dupes].got_offset;
1777 prev = curr;
1778 diff++;
1779 }
1780
1781 len = diff - src + 1;
1782 i = diff + 1;
1783 }
1784 }
1785
1786 memmove (&info [dest], &info [src], len * sizeof (*info));
1787
1788 dest += len;
1789 }
1790
1791 count = dest;
1792 }
1793 else
1794 {
1795 /* When we get here, either there is no duplicate at all or
1796 the only duplicate is the last element. */
1797 if (dest < count)
1798 {
1799 /* If the last element is a duplicate, make sure that the
1800 kept one has a valid got_offset. We also update count. */
1801 if (got_offset != (bfd_vma) -1)
1802 info [dest - 1].got_offset = got_offset;
1803 count = dest;
1804 }
1805 }
1806
1807 return count;
1808 }
1809
1810 /* Find and/or create a descriptor for dynamic symbol info. This will
1811 vary based on global or local symbol, and the addend to the reloc.
1812
1813 We don't sort when inserting. Also, we sort and eliminate
1814 duplicates if there is an unsorted section. Typically, this will
1815 only happen once, because we do all insertions before lookups. We
1816 then use bsearch to do a lookup. This also allows lookups to be
1817 fast. So we have fast insertion (O(log N) due to duplicate check),
1818 fast lookup (O(log N)) and one sort (O(N log N) expected time).
1819 Previously, all lookups were O(N) because of the use of the linked
1820 list and also all insertions were O(N) because of the check for
1821 duplicates. There are some complications here because the array
1822 size grows occasionally, which may add an O(N) factor, but this
1823 should be rare. Also, we free the excess array allocation, which
1824 requires a copy which is O(N), but this only happens once. */
1825
1826 static struct elfNN_ia64_dyn_sym_info *
1827 get_dyn_sym_info (struct elfNN_ia64_link_hash_table *ia64_info,
1828 struct elf_link_hash_entry *h, bfd *abfd,
1829 const Elf_Internal_Rela *rel, bfd_boolean create)
1830 {
1831 struct elfNN_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
1832 unsigned int *count_p, *sorted_count_p, *size_p;
1833 unsigned int count, sorted_count, size;
1834 bfd_vma addend = rel ? rel->r_addend : 0;
1835 bfd_size_type amt;
1836
1837 if (h)
1838 {
1839 struct elfNN_ia64_link_hash_entry *global_h;
1840
1841 global_h = (struct elfNN_ia64_link_hash_entry *) h;
1842 info_p = &global_h->info;
1843 count_p = &global_h->count;
1844 sorted_count_p = &global_h->sorted_count;
1845 size_p = &global_h->size;
1846 }
1847 else
1848 {
1849 struct elfNN_ia64_local_hash_entry *loc_h;
1850
1851 loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
1852 if (!loc_h)
1853 {
1854 BFD_ASSERT (!create);
1855 return NULL;
1856 }
1857
1858 info_p = &loc_h->info;
1859 count_p = &loc_h->count;
1860 sorted_count_p = &loc_h->sorted_count;
1861 size_p = &loc_h->size;
1862 }
1863
1864 count = *count_p;
1865 sorted_count = *sorted_count_p;
1866 size = *size_p;
1867 info = *info_p;
1868 if (create)
1869 {
1870 /* When we create the array, we don't check for duplicates,
1871 except in the previously sorted section if one exists, and
1872 against the last inserted entry. This allows insertions to
1873 be fast. */
1874 if (info)
1875 {
1876 if (sorted_count)
1877 {
1878 /* Try bsearch first on the sorted section. */
1879 key.addend = addend;
1880 dyn_i = bsearch (&key, info, sorted_count,
1881 sizeof (*info), addend_compare);
1882
1883 if (dyn_i)
1884 {
1885 return dyn_i;
1886 }
1887 }
1888
1889 /* Do a quick check for the last inserted entry. */
1890 dyn_i = info + count - 1;
1891 if (dyn_i->addend == addend)
1892 {
1893 return dyn_i;
1894 }
1895 }
1896
1897 if (size == 0)
1898 {
1899 /* It is the very first element. We create the array of size
1900 1. */
1901 size = 1;
1902 amt = size * sizeof (*info);
1903 info = bfd_malloc (amt);
1904 }
1905 else if (size <= count)
1906 {
1907 /* We double the array size every time when we reach the
1908 size limit. */
1909 size += size;
1910 amt = size * sizeof (*info);
1911 info = bfd_realloc (info, amt);
1912 }
1913 else
1914 goto has_space;
1915
1916 if (info == NULL)
1917 return NULL;
1918 *size_p = size;
1919 *info_p = info;
1920
1921 has_space:
1922 /* Append the new one to the array. */
1923 dyn_i = info + count;
1924 memset (dyn_i, 0, sizeof (*dyn_i));
1925 dyn_i->got_offset = (bfd_vma) -1;
1926 dyn_i->addend = addend;
1927
1928 /* We increment count only since the new ones are unsorted and
1929 may have duplicate. */
1930 (*count_p)++;
1931 }
1932 else
1933 {
1934 /* It is a lookup without insertion. Sort array if part of the
1935 array isn't sorted. */
1936 if (count != sorted_count)
1937 {
1938 count = sort_dyn_sym_info (info, count);
1939 *count_p = count;
1940 *sorted_count_p = count;
1941 }
1942
1943 /* Free unused memory. */
1944 if (size != count)
1945 {
1946 amt = count * sizeof (*info);
1947 info = bfd_malloc (amt);
1948 if (info != NULL)
1949 {
1950 memcpy (info, *info_p, amt);
1951 free (*info_p);
1952 *size_p = count;
1953 *info_p = info;
1954 }
1955 }
1956
1957 key.addend = addend;
1958 dyn_i = bsearch (&key, info, count,
1959 sizeof (*info), addend_compare);
1960 }
1961
1962 return dyn_i;
1963 }
1964
1965 static asection *
1966 get_got (bfd *abfd, struct bfd_link_info *info,
1967 struct elfNN_ia64_link_hash_table *ia64_info)
1968 {
1969 asection *got;
1970 bfd *dynobj;
1971
1972 got = ia64_info->root.sgot;
1973 if (!got)
1974 {
1975 flagword flags;
1976
1977 dynobj = ia64_info->root.dynobj;
1978 if (!dynobj)
1979 ia64_info->root.dynobj = dynobj = abfd;
1980 if (!_bfd_elf_create_got_section (dynobj, info))
1981 return NULL;
1982
1983 got = ia64_info->root.sgot;
1984
1985 /* The .got section is always aligned at 8 bytes. */
1986 if (!bfd_set_section_alignment (abfd, got, 3))
1987 return NULL;
1988
1989 flags = bfd_get_section_flags (abfd, got);
1990 if (! bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags))
1991 return NULL;
1992 }
1993
1994 return got;
1995 }
1996
1997 /* Create function descriptor section (.opd). This section is called .opd
1998 because it contains "official procedure descriptors". The "official"
1999 refers to the fact that these descriptors are used when taking the address
2000 of a procedure, thus ensuring a unique address for each procedure. */
2001
2002 static asection *
2003 get_fptr (bfd *abfd, struct bfd_link_info *info,
2004 struct elfNN_ia64_link_hash_table *ia64_info)
2005 {
2006 asection *fptr;
2007 bfd *dynobj;
2008
2009 fptr = ia64_info->fptr_sec;
2010 if (!fptr)
2011 {
2012 dynobj = ia64_info->root.dynobj;
2013 if (!dynobj)
2014 ia64_info->root.dynobj = dynobj = abfd;
2015
2016 fptr = bfd_make_section_anyway_with_flags (dynobj, ".opd",
2017 (SEC_ALLOC
2018 | SEC_LOAD
2019 | SEC_HAS_CONTENTS
2020 | SEC_IN_MEMORY
2021 | (bfd_link_pie (info)
2022 ? 0 : SEC_READONLY)
2023 | SEC_LINKER_CREATED));
2024 if (!fptr
2025 || !bfd_set_section_alignment (abfd, fptr, 4))
2026 {
2027 BFD_ASSERT (0);
2028 return NULL;
2029 }
2030
2031 ia64_info->fptr_sec = fptr;
2032
2033 if (bfd_link_pie (info))
2034 {
2035 asection *fptr_rel;
2036 fptr_rel = bfd_make_section_anyway_with_flags (dynobj, ".rela.opd",
2037 (SEC_ALLOC | SEC_LOAD
2038 | SEC_HAS_CONTENTS
2039 | SEC_IN_MEMORY
2040 | SEC_LINKER_CREATED
2041 | SEC_READONLY));
2042 if (fptr_rel == NULL
2043 || !bfd_set_section_alignment (abfd, fptr_rel,
2044 LOG_SECTION_ALIGN))
2045 {
2046 BFD_ASSERT (0);
2047 return NULL;
2048 }
2049
2050 ia64_info->rel_fptr_sec = fptr_rel;
2051 }
2052 }
2053
2054 return fptr;
2055 }
2056
2057 static asection *
2058 get_pltoff (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED,
2059 struct elfNN_ia64_link_hash_table *ia64_info)
2060 {
2061 asection *pltoff;
2062 bfd *dynobj;
2063
2064 pltoff = ia64_info->pltoff_sec;
2065 if (!pltoff)
2066 {
2067 dynobj = ia64_info->root.dynobj;
2068 if (!dynobj)
2069 ia64_info->root.dynobj = dynobj = abfd;
2070
2071 pltoff = bfd_make_section_anyway_with_flags (dynobj,
2072 ELF_STRING_ia64_pltoff,
2073 (SEC_ALLOC
2074 | SEC_LOAD
2075 | SEC_HAS_CONTENTS
2076 | SEC_IN_MEMORY
2077 | SEC_SMALL_DATA
2078 | SEC_LINKER_CREATED));
2079 if (!pltoff
2080 || !bfd_set_section_alignment (abfd, pltoff, 4))
2081 {
2082 BFD_ASSERT (0);
2083 return NULL;
2084 }
2085
2086 ia64_info->pltoff_sec = pltoff;
2087 }
2088
2089 return pltoff;
2090 }
2091
2092 static asection *
2093 get_reloc_section (bfd *abfd,
2094 struct elfNN_ia64_link_hash_table *ia64_info,
2095 asection *sec, bfd_boolean create)
2096 {
2097 const char *srel_name;
2098 asection *srel;
2099 bfd *dynobj;
2100
2101 srel_name = (bfd_elf_string_from_elf_section
2102 (abfd, elf_elfheader(abfd)->e_shstrndx,
2103 _bfd_elf_single_rel_hdr (sec)->sh_name));
2104 if (srel_name == NULL)
2105 return NULL;
2106
2107 dynobj = ia64_info->root.dynobj;
2108 if (!dynobj)
2109 ia64_info->root.dynobj = dynobj = abfd;
2110
2111 srel = bfd_get_linker_section (dynobj, srel_name);
2112 if (srel == NULL && create)
2113 {
2114 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
2115 (SEC_ALLOC | SEC_LOAD
2116 | SEC_HAS_CONTENTS
2117 | SEC_IN_MEMORY
2118 | SEC_LINKER_CREATED
2119 | SEC_READONLY));
2120 if (srel == NULL
2121 || !bfd_set_section_alignment (dynobj, srel,
2122 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_get_section_name (dynobj, 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 static bfd_boolean
4747 elfNN_ia64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4748 {
4749 bfd *obfd = info->output_bfd;
4750 flagword out_flags;
4751 flagword in_flags;
4752 bfd_boolean ok = TRUE;
4753
4754 /* Don't even pretend to support mixed-format linking. */
4755 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4756 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4757 return FALSE;
4758
4759 in_flags = elf_elfheader (ibfd)->e_flags;
4760 out_flags = elf_elfheader (obfd)->e_flags;
4761
4762 if (! elf_flags_init (obfd))
4763 {
4764 elf_flags_init (obfd) = TRUE;
4765 elf_elfheader (obfd)->e_flags = in_flags;
4766
4767 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4768 && bfd_get_arch_info (obfd)->the_default)
4769 {
4770 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4771 bfd_get_mach (ibfd));
4772 }
4773
4774 return TRUE;
4775 }
4776
4777 /* Check flag compatibility. */
4778 if (in_flags == out_flags)
4779 return TRUE;
4780
4781 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
4782 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4783 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4784
4785 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4786 {
4787 _bfd_error_handler
4788 (_("%pB: linking trap-on-NULL-dereference with non-trapping files"),
4789 ibfd);
4790
4791 bfd_set_error (bfd_error_bad_value);
4792 ok = FALSE;
4793 }
4794 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4795 {
4796 _bfd_error_handler
4797 (_("%pB: linking big-endian files with little-endian files"),
4798 ibfd);
4799
4800 bfd_set_error (bfd_error_bad_value);
4801 ok = FALSE;
4802 }
4803 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4804 {
4805 _bfd_error_handler
4806 (_("%pB: linking 64-bit files with 32-bit files"),
4807 ibfd);
4808
4809 bfd_set_error (bfd_error_bad_value);
4810 ok = FALSE;
4811 }
4812 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4813 {
4814 _bfd_error_handler
4815 (_("%pB: linking constant-gp files with non-constant-gp files"),
4816 ibfd);
4817
4818 bfd_set_error (bfd_error_bad_value);
4819 ok = FALSE;
4820 }
4821 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4822 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4823 {
4824 _bfd_error_handler
4825 (_("%pB: linking auto-pic files with non-auto-pic files"),
4826 ibfd);
4827
4828 bfd_set_error (bfd_error_bad_value);
4829 ok = FALSE;
4830 }
4831
4832 return ok;
4833 }
4834
4835 static bfd_boolean
4836 elfNN_ia64_print_private_bfd_data (bfd *abfd, void * ptr)
4837 {
4838 FILE *file = (FILE *) ptr;
4839 flagword flags = elf_elfheader (abfd)->e_flags;
4840
4841 BFD_ASSERT (abfd != NULL && ptr != NULL);
4842
4843 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4844 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4845 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4846 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4847 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4848 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4849 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4850 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4851 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4852
4853 _bfd_elf_print_private_bfd_data (abfd, ptr);
4854 return TRUE;
4855 }
4856
4857 static enum elf_reloc_type_class
4858 elfNN_ia64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4859 const asection *rel_sec ATTRIBUTE_UNUSED,
4860 const Elf_Internal_Rela *rela)
4861 {
4862 switch ((int) ELFNN_R_TYPE (rela->r_info))
4863 {
4864 case R_IA64_REL32MSB:
4865 case R_IA64_REL32LSB:
4866 case R_IA64_REL64MSB:
4867 case R_IA64_REL64LSB:
4868 return reloc_class_relative;
4869 case R_IA64_IPLTMSB:
4870 case R_IA64_IPLTLSB:
4871 return reloc_class_plt;
4872 case R_IA64_COPY:
4873 return reloc_class_copy;
4874 default:
4875 return reloc_class_normal;
4876 }
4877 }
4878
4879 static const struct bfd_elf_special_section elfNN_ia64_special_sections[] =
4880 {
4881 { STRING_COMMA_LEN (".sbss"), -1, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4882 { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4883 { NULL, 0, 0, 0, 0 }
4884 };
4885
4886 static bfd_boolean
4887 elfNN_ia64_object_p (bfd *abfd)
4888 {
4889 asection *sec;
4890 asection *group, *unwi, *unw;
4891 flagword flags;
4892 const char *name;
4893 char *unwi_name, *unw_name;
4894 bfd_size_type amt;
4895
4896 if (abfd->flags & DYNAMIC)
4897 return TRUE;
4898
4899 /* Flags for fake group section. */
4900 flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
4901 | SEC_EXCLUDE);
4902
4903 /* We add a fake section group for each .gnu.linkonce.t.* section,
4904 which isn't in a section group, and its unwind sections. */
4905 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4906 {
4907 if (elf_sec_group (sec) == NULL
4908 && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
4909 == (SEC_LINK_ONCE | SEC_CODE))
4910 && CONST_STRNEQ (sec->name, ".gnu.linkonce.t."))
4911 {
4912 name = sec->name + 16;
4913
4914 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
4915 unwi_name = bfd_alloc (abfd, amt);
4916 if (!unwi_name)
4917 return FALSE;
4918
4919 strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
4920 unwi = bfd_get_section_by_name (abfd, unwi_name);
4921
4922 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
4923 unw_name = bfd_alloc (abfd, amt);
4924 if (!unw_name)
4925 return FALSE;
4926
4927 strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
4928 unw = bfd_get_section_by_name (abfd, unw_name);
4929
4930 /* We need to create a fake group section for it and its
4931 unwind sections. */
4932 group = bfd_make_section_anyway_with_flags (abfd, name,
4933 flags);
4934 if (group == NULL)
4935 return FALSE;
4936
4937 /* Move the fake group section to the beginning. */
4938 bfd_section_list_remove (abfd, group);
4939 bfd_section_list_prepend (abfd, group);
4940
4941 elf_next_in_group (group) = sec;
4942
4943 elf_group_name (sec) = name;
4944 elf_next_in_group (sec) = sec;
4945 elf_sec_group (sec) = group;
4946
4947 if (unwi)
4948 {
4949 elf_group_name (unwi) = name;
4950 elf_next_in_group (unwi) = sec;
4951 elf_next_in_group (sec) = unwi;
4952 elf_sec_group (unwi) = group;
4953 }
4954
4955 if (unw)
4956 {
4957 elf_group_name (unw) = name;
4958 if (unwi)
4959 {
4960 elf_next_in_group (unw) = elf_next_in_group (unwi);
4961 elf_next_in_group (unwi) = unw;
4962 }
4963 else
4964 {
4965 elf_next_in_group (unw) = sec;
4966 elf_next_in_group (sec) = unw;
4967 }
4968 elf_sec_group (unw) = group;
4969 }
4970
4971 /* Fake SHT_GROUP section header. */
4972 elf_section_data (group)->this_hdr.bfd_section = group;
4973 elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
4974 }
4975 }
4976 return TRUE;
4977 }
4978
4979 static bfd_boolean
4980 elfNN_ia64_hpux_vec (const bfd_target *vec)
4981 {
4982 extern const bfd_target ia64_elfNN_hpux_be_vec;
4983 return (vec == &ia64_elfNN_hpux_be_vec);
4984 }
4985
4986 static void
4987 elfNN_hpux_post_process_headers (bfd *abfd,
4988 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4989 {
4990 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4991
4992 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
4993 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
4994 }
4995
4996 static bfd_boolean
4997 elfNN_hpux_backend_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4998 asection *sec, int *retval)
4999 {
5000 if (bfd_is_com_section (sec))
5001 {
5002 *retval = SHN_IA_64_ANSI_COMMON;
5003 return TRUE;
5004 }
5005 return FALSE;
5006 }
5007
5008 static void
5009 elfNN_hpux_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5010 asymbol *asym)
5011 {
5012 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5013
5014 switch (elfsym->internal_elf_sym.st_shndx)
5015 {
5016 case SHN_IA_64_ANSI_COMMON:
5017 asym->section = bfd_com_section_ptr;
5018 asym->value = elfsym->internal_elf_sym.st_size;
5019 asym->flags &= ~BSF_GLOBAL;
5020 break;
5021 }
5022 }
5023
5024 #define TARGET_LITTLE_SYM ia64_elfNN_le_vec
5026 #define TARGET_LITTLE_NAME "elfNN-ia64-little"
5027 #define TARGET_BIG_SYM ia64_elfNN_be_vec
5028 #define TARGET_BIG_NAME "elfNN-ia64-big"
5029 #define ELF_ARCH bfd_arch_ia64
5030 #define ELF_TARGET_ID IA64_ELF_DATA
5031 #define ELF_MACHINE_CODE EM_IA_64
5032 #define ELF_MACHINE_ALT1 1999 /* EAS2.3 */
5033 #define ELF_MACHINE_ALT2 1998 /* EAS2.2 */
5034 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
5035 #define ELF_COMMONPAGESIZE 0x4000 /* 16KB */
5036
5037 #define elf_backend_section_from_shdr \
5038 elfNN_ia64_section_from_shdr
5039 #define elf_backend_section_flags \
5040 elfNN_ia64_section_flags
5041 #define elf_backend_fake_sections \
5042 elfNN_ia64_fake_sections
5043 #define elf_backend_final_write_processing \
5044 elfNN_ia64_final_write_processing
5045 #define elf_backend_add_symbol_hook \
5046 elfNN_ia64_add_symbol_hook
5047 #define elf_backend_additional_program_headers \
5048 elfNN_ia64_additional_program_headers
5049 #define elf_backend_modify_segment_map \
5050 elfNN_ia64_modify_segment_map
5051 #define elf_backend_modify_program_headers \
5052 elfNN_ia64_modify_program_headers
5053 #define elf_info_to_howto \
5054 elfNN_ia64_info_to_howto
5055
5056 #define bfd_elfNN_bfd_reloc_type_lookup \
5057 ia64_elf_reloc_type_lookup
5058 #define bfd_elfNN_bfd_reloc_name_lookup \
5059 ia64_elf_reloc_name_lookup
5060 #define bfd_elfNN_bfd_is_local_label_name \
5061 elfNN_ia64_is_local_label_name
5062 #define bfd_elfNN_bfd_relax_section \
5063 elfNN_ia64_relax_section
5064
5065 #define elf_backend_object_p \
5066 elfNN_ia64_object_p
5067
5068 /* Stuff for the BFD linker: */
5069 #define bfd_elfNN_bfd_link_hash_table_create \
5070 elfNN_ia64_hash_table_create
5071 #define elf_backend_create_dynamic_sections \
5072 elfNN_ia64_create_dynamic_sections
5073 #define elf_backend_check_relocs \
5074 elfNN_ia64_check_relocs
5075 #define elf_backend_adjust_dynamic_symbol \
5076 elfNN_ia64_adjust_dynamic_symbol
5077 #define elf_backend_size_dynamic_sections \
5078 elfNN_ia64_size_dynamic_sections
5079 #define elf_backend_omit_section_dynsym \
5080 _bfd_elf_omit_section_dynsym_all
5081 #define elf_backend_relocate_section \
5082 elfNN_ia64_relocate_section
5083 #define elf_backend_finish_dynamic_symbol \
5084 elfNN_ia64_finish_dynamic_symbol
5085 #define elf_backend_finish_dynamic_sections \
5086 elfNN_ia64_finish_dynamic_sections
5087 #define bfd_elfNN_bfd_final_link \
5088 elfNN_ia64_final_link
5089
5090 #define bfd_elfNN_bfd_merge_private_bfd_data \
5091 elfNN_ia64_merge_private_bfd_data
5092 #define bfd_elfNN_bfd_set_private_flags \
5093 elfNN_ia64_set_private_flags
5094 #define bfd_elfNN_bfd_print_private_bfd_data \
5095 elfNN_ia64_print_private_bfd_data
5096
5097 #define elf_backend_plt_readonly 1
5098 #define elf_backend_can_gc_sections 1
5099 #define elf_backend_want_plt_sym 0
5100 #define elf_backend_plt_alignment 5
5101 #define elf_backend_got_header_size 0
5102 #define elf_backend_want_got_plt 1
5103 #define elf_backend_may_use_rel_p 1
5104 #define elf_backend_may_use_rela_p 1
5105 #define elf_backend_default_use_rela_p 1
5106 #define elf_backend_want_dynbss 0
5107 #define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
5108 #define elf_backend_hide_symbol elfNN_ia64_hash_hide_symbol
5109 #define elf_backend_fixup_symbol _bfd_elf_link_hash_fixup_symbol
5110 #define elf_backend_reloc_type_class elfNN_ia64_reloc_type_class
5111 #define elf_backend_rela_normal 1
5112 #define elf_backend_dtrel_excludes_plt 1
5113 #define elf_backend_special_sections elfNN_ia64_special_sections
5114 #define elf_backend_default_execstack 0
5115
5116 /* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
5117 SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
5118 We don't want to flood users with so many error messages. We turn
5119 off the warning for now. It will be turned on later when the Intel
5120 compiler is fixed. */
5121 #define elf_backend_link_order_error_handler NULL
5122
5123 #include "elfNN-target.h"
5124
5125 /* HPUX-specific vectors. */
5126
5127 #undef TARGET_LITTLE_SYM
5128 #undef TARGET_LITTLE_NAME
5129 #undef TARGET_BIG_SYM
5130 #define TARGET_BIG_SYM ia64_elfNN_hpux_be_vec
5131 #undef TARGET_BIG_NAME
5132 #define TARGET_BIG_NAME "elfNN-ia64-hpux-big"
5133
5134 /* These are HP-UX specific functions. */
5135
5136 #undef elf_backend_post_process_headers
5137 #define elf_backend_post_process_headers elfNN_hpux_post_process_headers
5138
5139 #undef elf_backend_section_from_bfd_section
5140 #define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
5141
5142 #undef elf_backend_symbol_processing
5143 #define elf_backend_symbol_processing elfNN_hpux_backend_symbol_processing
5144
5145 #undef elf_backend_want_p_paddr_set_to_zero
5146 #define elf_backend_want_p_paddr_set_to_zero 1
5147
5148 #undef ELF_COMMONPAGESIZE
5149 #undef ELF_OSABI
5150 #define ELF_OSABI ELFOSABI_HPUX
5151
5152 #undef elfNN_bed
5153 #define elfNN_bed elfNN_ia64_hpux_bed
5154
5155 #include "elfNN-target.h"
5156