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