Automatic date update in version.in
[binutils-gdb.git] / bfd / elf64-ia64-vms.c
blob54133c94c9101b9fcf8de483c3cffa30b1c5dee7
1 /* IA-64 support for OpenVMS
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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"
33 /* THE RULES for all the stuff the linker creates --
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.
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.
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.
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. */
58 typedef struct bfd_hash_entry *(*new_hash_entry_func)
59 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
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. */
65 struct elf64_ia64_dyn_sym_info
67 /* The addend for which this entry is relevant. */
68 bfd_vma addend;
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;
76 /* The symbol table entry, if any, that this was derived from. */
77 struct elf_link_hash_entry *h;
79 /* Used to count non-got, non-plt relocations for delayed sizing
80 of relocation sections. */
81 struct elf64_ia64_dyn_reloc_entry
83 struct elf64_ia64_dyn_reloc_entry *next;
84 asection *srel;
85 int type;
86 int count;
87 } *reloc_entries;
89 /* TRUE when the section contents have been updated. */
90 unsigned got_done : 1;
91 unsigned fptr_done : 1;
92 unsigned pltoff_done : 1;
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;
104 struct elf64_ia64_local_hash_entry
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;
117 /* TRUE if this hash entry's addends was translated for
118 SHF_MERGE optimization. */
119 unsigned sec_merge_done : 1;
122 struct elf64_ia64_link_hash_entry
124 struct elf_link_hash_entry root;
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;
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;
141 struct elf64_ia64_link_hash_table
143 /* The main hash table. */
144 struct elf_link_hash_table root;
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. */
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. */
162 htab_t loc_hash_table;
163 void *loc_hash_memory;
166 struct elf64_ia64_allocate_data
168 struct bfd_link_info *info;
169 bfd_size_type ofs;
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)
177 struct elf64_ia64_vms_obj_tdata
179 struct elf_obj_tdata root;
181 /* Ident for shared library. */
182 uint64_t ident;
184 /* Used only during link: offset in the .fixups section for this bfd. */
185 bfd_vma fixups_off;
187 /* Max number of shared libraries. */
188 unsigned int needed_count;
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)
195 struct elf64_vms_transfer
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];
205 /* Local function descriptor for tfr3. */
206 unsigned char tfr3_func[8];
207 unsigned char tfr3_gp[8];
210 typedef struct
212 Elf64_External_Ehdr ehdr;
213 unsigned char vms_needed_count[8];
214 } Elf64_External_VMS_Ehdr;
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 *);
242 /* Given a ELF reloc, return the matching HOWTO structure. */
244 static bool
245 elf64_ia64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
246 arelent *bfd_reloc,
247 Elf_Internal_Rela *elf_reloc)
249 unsigned int r_type = ELF32_R_TYPE (elf_reloc->r_info);
251 bfd_reloc->howto = ia64_elf_lookup_howto (r_type);
252 if (bfd_reloc->howto == NULL)
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;
261 return true;
265 #define PLT_FULL_ENTRY_SIZE (2 * 16)
267 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
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;; */
277 static const bfd_byte oor_brl[16] =
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
285 /* These functions do relaxation for IA-64 ELF. */
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
292 static void
293 elf64_ia64_update_short_info (asection *sec, bfd_vma offset,
294 struct elf64_ia64_link_hash_table *ia64_info)
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;
301 if (!ia64_info->min_short_sec)
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;
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)
317 ia64_info->max_short_sec = sec;
318 ia64_info->max_short_offset = offset;
320 else if (sec->output_section->vma
321 < ia64_info->min_short_sec->vma)
323 ia64_info->min_short_sec = sec;
324 ia64_info->min_short_offset = offset;
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.
333 static bool
334 elf64_ia64_relax_section (bfd *abfd, asection *sec,
335 struct bfd_link_info *link_info,
336 bool *again)
338 struct one_fixup
340 struct one_fixup *next;
341 asection *tsec;
342 bfd_vma toff;
343 bfd_vma trampoff;
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;
359 /* Assume we're not going to change any sizes, and we'll only need
360 one pass. */
361 *again = false;
363 if (bfd_link_relocatable (link_info))
364 (*link_info->callbacks->einfo)
365 (_("%P%F: --relax and -r may not be used together\n"));
367 /* Don't even try to relax for non-ELF outputs. */
368 if (!is_elf_hash_table (link_info->hash))
369 return false;
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;
380 ia64_info = elf64_ia64_hash_table (link_info);
381 if (ia64_info == NULL)
382 return false;
384 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
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;
393 irelend = internal_relocs + sec->reloc_count;
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
400 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
401 goto error_return;
404 for (irel = internal_relocs; irel < irelend; irel++)
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;
414 switch (r_type)
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;
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)
432 skip_relax_pass_1 = false;
433 continue;
435 is_branch = true;
436 break;
438 case R_IA64_GPREL22:
439 /* Update max_short_sec/min_short_sec. */
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)
447 skip_relax_pass_1 = false;
448 continue;
450 is_branch = false;
451 break;
453 default:
454 continue;
457 /* Get the value of the symbol referred to by the reloc. */
458 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
460 /* A local symbol. */
461 Elf_Internal_Sym *isym;
463 /* Read this BFD's local symbols. */
464 if (isymbuf == NULL)
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;
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);
487 toff = isym->st_value;
488 dyn_i = get_dyn_sym_info (ia64_info, NULL, abfd, irel, false);
490 else
492 unsigned long indx;
493 struct elf_link_hash_entry *h;
495 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
496 h = elf_sym_hashes (abfd)[indx];
497 BFD_ASSERT (h != NULL);
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;
503 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, false);
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)
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;
514 tsec = ia64_info->root.splt;
515 toff = dyn_i->plt2_offset;
516 BFD_ASSERT (irel->r_addend == 0);
519 /* Can't do anything else with dynamic symbols. */
520 else if (elf64_ia64_dynamic_symbol_p (h))
521 continue;
523 else
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;
530 tsec = h->root.u.def.section;
531 toff = h->root.u.def.value;
535 toff += irel->r_addend;
537 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
539 roff = irel->r_offset;
541 if (is_branch)
543 bfd_signed_vma offset;
545 reladdr = (sec->output_section->vma
546 + sec->output_offset
547 + roff) & (bfd_vma) -4;
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;
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)
565 /* If the 60-bit branch is in 21-bit range, optimize it. */
566 if (r_type == R_IA64_PCREL60B)
568 ia64_elf_relax_brl (contents, roff);
570 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
571 R_IA64_PCREL21B);
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;
579 continue;
581 else if (r_type == R_IA64_PCREL60B)
582 continue;
583 else if (ia64_elf_relax_br (contents, roff))
585 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
586 R_IA64_PCREL60B);
588 /* Make the relocation offset point to slot 1. */
589 irel->r_offset = (irel->r_offset & ~((bfd_vma) 0x3)) + 1;
590 continue;
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)
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;
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;
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;
619 if (f == NULL)
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. */
625 size_t size;
627 if (tsec == ia64_info->root.splt)
628 size = sizeof (plt_full_entry);
629 else
630 size = sizeof (oor_brl);
632 /* Resize the current section to make room for the new branch. */
633 trampoff = (sec->size + 15) & (bfd_vma) -16;
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;
641 amt = trampoff + size;
642 contents = (bfd_byte *) bfd_realloc (contents, amt);
643 if (contents == NULL)
644 goto error_return;
645 sec->size = amt;
647 if (tsec == ia64_info->root.splt)
649 memcpy (contents + trampoff, plt_full_entry, size);
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;
656 else
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;
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;
673 else
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;
681 /* Nop out the reloc, since we're finalizing things here. */
682 irel->r_info = ELF64_R_INFO (0, R_IA64_NONE);
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;
690 changed_contents = true;
691 changed_relocs = true;
693 else
695 /* Fetch the gp. */
696 if (gp == 0)
698 bfd *obfd = sec->output_section->owner;
699 gp = _bfd_get_gp_value (obfd);
700 if (gp == 0)
702 if (!elf64_ia64_choose_gp (obfd, link_info, false))
703 goto error_return;
704 gp = _bfd_get_gp_value (obfd);
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;
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)
719 /* Can't deal yet correctly with ABS symbols. */
720 if (bfd_is_abs_section (tsec))
721 continue;
723 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
724 R_IA64_GPREL22);
725 changed_relocs = true;
727 elf64_ia64_update_short_info (tsec->output_section,
728 tsec->output_offset + toff,
729 ia64_info);
731 else
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;
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. */
746 /* Clean up and go home. */
747 while (fixups)
749 struct one_fixup *f = fixups;
750 fixups = fixups->next;
751 free (f);
754 if (isymbuf != NULL
755 && symtab_hdr->contents != (unsigned char *) isymbuf)
757 if (! link_info->keep_memory)
758 free (isymbuf);
759 else
761 /* Cache the symbols for elf_link_input_bfd. */
762 symtab_hdr->contents = (unsigned char *) isymbuf;
766 if (contents != NULL
767 && elf_section_data (sec)->this_hdr.contents != contents)
769 if (!changed_contents && !link_info->keep_memory)
770 free (contents);
771 else
773 /* Cache the section contents for elf_link_input_bfd. */
774 elf_section_data (sec)->this_hdr.contents = contents;
778 if (elf_section_data (sec)->relocs != internal_relocs)
780 if (!changed_relocs)
781 free (internal_relocs);
782 else
783 elf_section_data (sec)->relocs = internal_relocs;
786 if (link_info->relax_pass == 0)
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;
793 *again = changed_contents || changed_relocs;
794 return true;
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;
805 #undef skip_relax_pass_0
806 #undef skip_relax_pass_1
808 /* Return TRUE if NAME is an unwind table section name. */
810 static inline bool
811 is_unwind_section_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
813 return ((startswith (name, ELF_STRING_ia64_unwind)
814 && ! startswith (name, ELF_STRING_ia64_unwind_info))
815 || startswith (name, ELF_STRING_ia64_unwind_once));
819 /* Convert IA-64 specific section flags to bfd internal section flags. */
821 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
822 flag. */
824 static bool
825 elf64_ia64_section_flags (const Elf_Internal_Shdr *hdr)
827 if (hdr->sh_flags & SHF_IA_64_SHORT)
828 hdr->bfd_section->flags |= SEC_SMALL_DATA;
830 return true;
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. */
836 static bool
837 elf64_ia64_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr,
838 asection *sec)
840 const char *name;
842 name = bfd_section_name (sec);
844 if (is_unwind_section_name (abfd, name))
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;
851 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
852 hdr->sh_type = SHT_IA_64_EXT;
854 if (sec->flags & SEC_SMALL_DATA)
855 hdr->sh_flags |= SHF_IA_64_SHORT;
857 return true;
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. */
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)
872 if (sym->st_shndx == SHN_COMMON
873 && !bfd_link_relocatable (info)
874 && sym->st_size <= elf_gp_size (abfd))
876 /* Common symbols less than or equal to -G nn bytes are
877 automatically put into .sbss. */
879 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
881 if (scomm == NULL)
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;
892 *secp = scomm;
893 *valp = sym->st_size;
896 return true;
899 /* According to the Tahoe assembler spec, all labels starting with a
900 '.' are local. */
902 static bool
903 elf64_ia64_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
904 const char *name)
906 return name[0] == '.';
909 /* Should we do dynamic things to this symbol? */
911 static bool
912 elf64_ia64_dynamic_symbol_p (struct elf_link_hash_entry *h)
914 return h != NULL && h->def_dynamic;
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)
922 struct elf64_ia64_link_hash_entry *ret;
923 ret = (struct elf64_ia64_link_hash_entry *) entry;
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));
930 if (!ret)
931 return 0;
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));
938 ret->info = NULL;
939 ret->count = 0;
940 ret->sorted_count = 0;
941 ret->size = 0;
942 return (struct bfd_hash_entry *) ret;
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)
950 struct elf64_ia64_link_hash_entry *h;
951 struct elf64_ia64_dyn_sym_info *dyn_i;
952 unsigned int count;
954 h = (struct elf64_ia64_link_hash_entry *)xh;
956 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
958 for (count = h->count, dyn_i = h->info;
959 count != 0;
960 count--, dyn_i++)
962 dyn_i->want_plt2 = 0;
963 dyn_i->want_plt = 0;
967 /* Compute a hash of a local hash entry. */
969 static hashval_t
970 elf64_ia64_local_htab_hash (const void *ptr)
972 struct elf64_ia64_local_hash_entry *entry
973 = (struct elf64_ia64_local_hash_entry *) ptr;
975 return ELF_LOCAL_SYMBOL_HASH (entry->id, entry->r_sym);
978 /* Compare local hash entries. */
980 static int
981 elf64_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
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;
988 return entry1->id == entry2->id && entry1->r_sym == entry2->r_sym;
991 /* Free the global elf64_ia64_dyn_sym_info array. */
993 static bool
994 elf64_ia64_global_dyn_info_free (struct elf_link_hash_entry *xentry,
995 void * unused ATTRIBUTE_UNUSED)
997 struct elf64_ia64_link_hash_entry *entry
998 = (struct elf64_ia64_link_hash_entry *) xentry;
1000 if (entry->root.root.type == bfd_link_hash_warning)
1001 entry = (struct elf64_ia64_link_hash_entry *) entry->root.root.u.i.link;
1003 free (entry->info);
1004 entry->info = NULL;
1005 entry->count = 0;
1006 entry->sorted_count = 0;
1007 entry->size = 0;
1009 return true;
1012 /* Free the local elf64_ia64_dyn_sym_info array. */
1014 static int
1015 elf64_ia64_local_dyn_info_free (void **slot,
1016 void * unused ATTRIBUTE_UNUSED)
1018 struct elf64_ia64_local_hash_entry *entry
1019 = (struct elf64_ia64_local_hash_entry *) *slot;
1021 free (entry->info);
1022 entry->info = NULL;
1023 entry->count = 0;
1024 entry->sorted_count = 0;
1025 entry->size = 0;
1027 return true;
1030 /* Destroy IA-64 linker hash table. */
1032 static void
1033 elf64_ia64_link_hash_table_free (bfd *obfd)
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)
1039 htab_traverse (ia64_info->loc_hash_table,
1040 elf64_ia64_local_dyn_info_free, NULL);
1041 htab_delete (ia64_info->loc_hash_table);
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);
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). */
1054 static struct bfd_link_hash_table *
1055 elf64_ia64_hash_table_create (bfd *abfd)
1057 struct elf64_ia64_link_hash_table *ret;
1059 ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
1060 if (!ret)
1061 return NULL;
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 IA64_ELF_DATA))
1068 free (ret);
1069 return NULL;
1072 ret->loc_hash_table = htab_try_create (1024, elf64_ia64_local_htab_hash,
1073 elf64_ia64_local_htab_eq, NULL);
1074 ret->loc_hash_memory = objalloc_create ();
1075 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1077 elf64_ia64_link_hash_table_free (abfd);
1078 return NULL;
1080 ret->root.root.hash_table_free = elf64_ia64_link_hash_table_free;
1082 return &ret->root.root;
1085 /* Traverse both local and global hash tables. */
1087 struct elf64_ia64_dyn_sym_traverse_data
1089 bool (*func) (struct elf64_ia64_dyn_sym_info *, void *);
1090 void * data;
1093 static bool
1094 elf64_ia64_global_dyn_sym_thunk (struct elf_link_hash_entry *xentry,
1095 void * xdata)
1097 struct elf64_ia64_link_hash_entry *entry
1098 = (struct elf64_ia64_link_hash_entry *) xentry;
1099 struct elf64_ia64_dyn_sym_traverse_data *data
1100 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1101 struct elf64_ia64_dyn_sym_info *dyn_i;
1102 unsigned int count;
1104 if (entry->root.root.type == bfd_link_hash_warning)
1105 entry = (struct elf64_ia64_link_hash_entry *) entry->root.root.u.i.link;
1107 for (count = entry->count, dyn_i = entry->info;
1108 count != 0;
1109 count--, dyn_i++)
1110 if (! (*data->func) (dyn_i, data->data))
1111 return false;
1112 return true;
1115 static int
1116 elf64_ia64_local_dyn_sym_thunk (void **slot, void * xdata)
1118 struct elf64_ia64_local_hash_entry *entry
1119 = (struct elf64_ia64_local_hash_entry *) *slot;
1120 struct elf64_ia64_dyn_sym_traverse_data *data
1121 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1122 struct elf64_ia64_dyn_sym_info *dyn_i;
1123 unsigned int count;
1125 for (count = entry->count, dyn_i = entry->info;
1126 count != 0;
1127 count--, dyn_i++)
1128 if (! (*data->func) (dyn_i, data->data))
1129 return false;
1130 return true;
1133 static void
1134 elf64_ia64_dyn_sym_traverse (struct elf64_ia64_link_hash_table *ia64_info,
1135 bool (*func) (struct elf64_ia64_dyn_sym_info *, void *),
1136 void * data)
1138 struct elf64_ia64_dyn_sym_traverse_data xdata;
1140 xdata.func = func;
1141 xdata.data = data;
1143 elf_link_hash_traverse (&ia64_info->root,
1144 elf64_ia64_global_dyn_sym_thunk, &xdata);
1145 htab_traverse (ia64_info->loc_hash_table,
1146 elf64_ia64_local_dyn_sym_thunk, &xdata);
1149 #define NOTE_NAME "IPF/VMS"
1151 static bool
1152 create_ia64_vms_notes (bfd *abfd, struct bfd_link_info *info,
1153 unsigned int time_hi, unsigned int time_lo)
1155 #define NBR_NOTES 7
1156 Elf_Internal_Note notes[NBR_NOTES];
1157 char *module_name;
1158 int module_name_len;
1159 unsigned char cur_time[8];
1160 Elf64_External_VMS_ORIG_DYN_Note *orig_dyn;
1161 unsigned int orig_dyn_size;
1162 unsigned int note_size;
1163 int i;
1164 unsigned char *noteptr;
1165 unsigned char *note_contents;
1166 struct elf64_ia64_link_hash_table *ia64_info;
1168 ia64_info = elf64_ia64_hash_table (info);
1170 module_name = vms_get_module_name (bfd_get_filename (abfd), true);
1171 module_name_len = strlen (module_name) + 1;
1173 bfd_putl32 (time_lo, cur_time + 0);
1174 bfd_putl32 (time_hi, cur_time + 4);
1176 /* Note 0: IMGNAM. */
1177 notes[0].type = NT_VMS_IMGNAM;
1178 notes[0].descdata = module_name;
1179 notes[0].descsz = module_name_len;
1181 /* Note 1: GSTNAM. */
1182 notes[1].type = NT_VMS_GSTNAM;
1183 notes[1].descdata = module_name;
1184 notes[1].descsz = module_name_len;
1186 /* Note 2: IMGID. */
1187 #define IMG_ID "V1.0"
1188 notes[2].type = NT_VMS_IMGID;
1189 notes[2].descdata = IMG_ID;
1190 notes[2].descsz = sizeof (IMG_ID);
1192 /* Note 3: Linktime. */
1193 notes[3].type = NT_VMS_LINKTIME;
1194 notes[3].descdata = (char *)cur_time;
1195 notes[3].descsz = sizeof (cur_time);
1197 /* Note 4: Linker id. */
1198 notes[4].type = NT_VMS_LINKID;
1199 notes[4].descdata = "GNU ld " BFD_VERSION_STRING;
1200 notes[4].descsz = strlen (notes[4].descdata) + 1;
1202 /* Note 5: Original dyn. */
1203 orig_dyn_size = (sizeof (*orig_dyn) + sizeof (IMG_ID) - 1 + 7) & ~7;
1204 orig_dyn = bfd_zalloc (abfd, orig_dyn_size);
1205 if (orig_dyn == NULL)
1206 return false;
1207 bfd_putl32 (1, orig_dyn->major_id);
1208 bfd_putl32 (3, orig_dyn->minor_id);
1209 memcpy (orig_dyn->manipulation_date, cur_time, sizeof (cur_time));
1210 bfd_putl64 (VMS_LF_IMGSTA | VMS_LF_MAIN, orig_dyn->link_flags);
1211 bfd_putl32 (EF_IA_64_ABI64, orig_dyn->elf_flags);
1212 memcpy (orig_dyn->imgid, IMG_ID, sizeof (IMG_ID));
1213 notes[5].type = NT_VMS_ORIG_DYN;
1214 notes[5].descdata = (char *)orig_dyn;
1215 notes[5].descsz = orig_dyn_size;
1217 /* Note 3: Patchtime. */
1218 notes[6].type = NT_VMS_PATCHTIME;
1219 notes[6].descdata = (char *)cur_time;
1220 notes[6].descsz = sizeof (cur_time);
1222 /* Compute notes size. */
1223 note_size = 0;
1224 for (i = 0; i < NBR_NOTES; i++)
1225 note_size += sizeof (Elf64_External_VMS_Note) - 1
1226 + ((sizeof (NOTE_NAME) - 1 + 7) & ~7)
1227 + ((notes[i].descsz + 7) & ~7);
1229 /* Malloc a temporary buffer large enough for most notes */
1230 note_contents = (unsigned char *) bfd_zalloc (abfd, note_size);
1231 if (note_contents == NULL)
1232 return false;
1233 noteptr = note_contents;
1235 /* Fill notes. */
1236 for (i = 0; i < NBR_NOTES; i++)
1238 Elf64_External_VMS_Note *enote = (Elf64_External_VMS_Note *) noteptr;
1240 bfd_putl64 (sizeof (NOTE_NAME) - 1, enote->namesz);
1241 bfd_putl64 (notes[i].descsz, enote->descsz);
1242 bfd_putl64 (notes[i].type, enote->type);
1244 noteptr = (unsigned char *)enote->name;
1245 memcpy (noteptr, NOTE_NAME, sizeof (NOTE_NAME) - 1);
1246 noteptr += (sizeof (NOTE_NAME) - 1 + 7) & ~7;
1247 memcpy (noteptr, notes[i].descdata, notes[i].descsz);
1248 noteptr += (notes[i].descsz + 7) & ~7;
1251 ia64_info->note_sec->contents = note_contents;
1252 ia64_info->note_sec->size = note_size;
1254 free (module_name);
1256 return true;
1259 static bool
1260 elf64_ia64_create_dynamic_sections (bfd *abfd,
1261 struct bfd_link_info *info)
1263 struct elf64_ia64_link_hash_table *ia64_info;
1264 asection *s;
1265 flagword flags;
1266 const struct elf_backend_data *bed;
1268 ia64_info = elf64_ia64_hash_table (info);
1269 if (ia64_info == NULL)
1270 return false;
1272 if (elf_hash_table (info)->dynamic_sections_created)
1273 return true;
1275 abfd = elf_hash_table (info)->dynobj;
1276 bed = get_elf_backend_data (abfd);
1278 flags = bed->dynamic_sec_flags;
1280 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic",
1281 flags | SEC_READONLY);
1282 if (s == NULL
1283 || !bfd_set_section_alignment (s, bed->s->log_file_align))
1284 return false;
1286 s = bfd_make_section_anyway_with_flags (abfd, ".plt", flags | SEC_READONLY);
1287 if (s == NULL
1288 || !bfd_set_section_alignment (s, bed->plt_alignment))
1289 return false;
1290 ia64_info->root.splt = s;
1292 if (!get_got (abfd, ia64_info))
1293 return false;
1295 if (!get_pltoff (abfd, ia64_info))
1296 return false;
1298 s = bfd_make_section_anyway_with_flags (abfd, ".vmsdynstr",
1299 (SEC_ALLOC
1300 | SEC_HAS_CONTENTS
1301 | SEC_IN_MEMORY
1302 | SEC_LINKER_CREATED));
1303 if (s == NULL
1304 || !bfd_set_section_alignment (s, 0))
1305 return false;
1307 /* Create a fixup section. */
1308 s = bfd_make_section_anyway_with_flags (abfd, ".fixups",
1309 (SEC_ALLOC
1310 | SEC_HAS_CONTENTS
1311 | SEC_IN_MEMORY
1312 | SEC_LINKER_CREATED));
1313 if (s == NULL
1314 || !bfd_set_section_alignment (s, 3))
1315 return false;
1316 ia64_info->fixups_sec = s;
1318 /* Create the transfer fixup section. */
1319 s = bfd_make_section_anyway_with_flags (abfd, ".transfer",
1320 (SEC_ALLOC
1321 | SEC_HAS_CONTENTS
1322 | SEC_IN_MEMORY
1323 | SEC_LINKER_CREATED));
1324 if (s == NULL
1325 || !bfd_set_section_alignment (s, 3))
1326 return false;
1327 s->size = sizeof (struct elf64_vms_transfer);
1328 ia64_info->transfer_sec = s;
1330 /* Create note section. */
1331 s = bfd_make_section_anyway_with_flags (abfd, ".vms.note",
1332 (SEC_LINKER_CREATED
1333 | SEC_HAS_CONTENTS
1334 | SEC_IN_MEMORY
1335 | SEC_READONLY));
1336 if (s == NULL
1337 || !bfd_set_section_alignment (s, 3))
1338 return false;
1339 ia64_info->note_sec = s;
1341 elf_hash_table (info)->dynamic_sections_created = true;
1342 return true;
1345 /* Find and/or create a hash entry for local symbol. */
1346 static struct elf64_ia64_local_hash_entry *
1347 get_local_sym_hash (struct elf64_ia64_link_hash_table *ia64_info,
1348 bfd *abfd, const Elf_Internal_Rela *rel,
1349 bool create)
1351 struct elf64_ia64_local_hash_entry e, *ret;
1352 asection *sec = abfd->sections;
1353 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1354 ELF64_R_SYM (rel->r_info));
1355 void **slot;
1357 e.id = sec->id;
1358 e.r_sym = ELF64_R_SYM (rel->r_info);
1359 slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
1360 create ? INSERT : NO_INSERT);
1362 if (!slot)
1363 return NULL;
1365 if (*slot)
1366 return (struct elf64_ia64_local_hash_entry *) *slot;
1368 ret = (struct elf64_ia64_local_hash_entry *)
1369 objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
1370 sizeof (struct elf64_ia64_local_hash_entry));
1371 if (ret)
1373 memset (ret, 0, sizeof (*ret));
1374 ret->id = sec->id;
1375 ret->r_sym = ELF64_R_SYM (rel->r_info);
1376 *slot = ret;
1378 return ret;
1381 /* Used to sort elf64_ia64_dyn_sym_info array. */
1383 static int
1384 addend_compare (const void *xp, const void *yp)
1386 const struct elf64_ia64_dyn_sym_info *x
1387 = (const struct elf64_ia64_dyn_sym_info *) xp;
1388 const struct elf64_ia64_dyn_sym_info *y
1389 = (const struct elf64_ia64_dyn_sym_info *) yp;
1391 return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
1394 /* Sort elf64_ia64_dyn_sym_info array and remove duplicates. */
1396 static unsigned int
1397 sort_dyn_sym_info (struct elf64_ia64_dyn_sym_info *info,
1398 unsigned int count)
1400 bfd_vma curr, prev, got_offset;
1401 unsigned int i, kept, dupes, diff, dest, src, len;
1403 qsort (info, count, sizeof (*info), addend_compare);
1405 /* Find the first duplicate. */
1406 prev = info [0].addend;
1407 got_offset = info [0].got_offset;
1408 for (i = 1; i < count; i++)
1410 curr = info [i].addend;
1411 if (curr == prev)
1413 /* For duplicates, make sure that GOT_OFFSET is valid. */
1414 if (got_offset == (bfd_vma) -1)
1415 got_offset = info [i].got_offset;
1416 break;
1418 got_offset = info [i].got_offset;
1419 prev = curr;
1422 /* We may move a block of elements to here. */
1423 dest = i++;
1425 /* Remove duplicates. */
1426 if (i < count)
1428 while (i < count)
1430 /* For duplicates, make sure that the kept one has a valid
1431 got_offset. */
1432 kept = dest - 1;
1433 if (got_offset != (bfd_vma) -1)
1434 info [kept].got_offset = got_offset;
1436 curr = info [i].addend;
1437 got_offset = info [i].got_offset;
1439 /* Move a block of elements whose first one is different from
1440 the previous. */
1441 if (curr == prev)
1443 for (src = i + 1; src < count; src++)
1445 if (info [src].addend != curr)
1446 break;
1447 /* For duplicates, make sure that GOT_OFFSET is
1448 valid. */
1449 if (got_offset == (bfd_vma) -1)
1450 got_offset = info [src].got_offset;
1453 /* Make sure that the kept one has a valid got_offset. */
1454 if (got_offset != (bfd_vma) -1)
1455 info [kept].got_offset = got_offset;
1457 else
1458 src = i;
1460 if (src >= count)
1461 break;
1463 /* Find the next duplicate. SRC will be kept. */
1464 prev = info [src].addend;
1465 got_offset = info [src].got_offset;
1466 for (dupes = src + 1; dupes < count; dupes ++)
1468 curr = info [dupes].addend;
1469 if (curr == prev)
1471 /* Make sure that got_offset is valid. */
1472 if (got_offset == (bfd_vma) -1)
1473 got_offset = info [dupes].got_offset;
1475 /* For duplicates, make sure that the kept one has
1476 a valid got_offset. */
1477 if (got_offset != (bfd_vma) -1)
1478 info [dupes - 1].got_offset = got_offset;
1479 break;
1481 got_offset = info [dupes].got_offset;
1482 prev = curr;
1485 /* How much to move. */
1486 len = dupes - src;
1487 i = dupes + 1;
1489 if (len == 1 && dupes < count)
1491 /* If we only move 1 element, we combine it with the next
1492 one. There must be at least a duplicate. Find the
1493 next different one. */
1494 for (diff = dupes + 1, src++; diff < count; diff++, src++)
1496 if (info [diff].addend != curr)
1497 break;
1498 /* Make sure that got_offset is valid. */
1499 if (got_offset == (bfd_vma) -1)
1500 got_offset = info [diff].got_offset;
1503 /* Makre sure that the last duplicated one has an valid
1504 offset. */
1505 BFD_ASSERT (curr == prev);
1506 if (got_offset != (bfd_vma) -1)
1507 info [diff - 1].got_offset = got_offset;
1509 if (diff < count)
1511 /* Find the next duplicate. Track the current valid
1512 offset. */
1513 prev = info [diff].addend;
1514 got_offset = info [diff].got_offset;
1515 for (dupes = diff + 1; dupes < count; dupes ++)
1517 curr = info [dupes].addend;
1518 if (curr == prev)
1520 /* For duplicates, make sure that GOT_OFFSET
1521 is valid. */
1522 if (got_offset == (bfd_vma) -1)
1523 got_offset = info [dupes].got_offset;
1524 break;
1526 got_offset = info [dupes].got_offset;
1527 prev = curr;
1528 diff++;
1531 len = diff - src + 1;
1532 i = diff + 1;
1536 memmove (&info [dest], &info [src], len * sizeof (*info));
1538 dest += len;
1541 count = dest;
1543 else
1545 /* When we get here, either there is no duplicate at all or
1546 the only duplicate is the last element. */
1547 if (dest < count)
1549 /* If the last element is a duplicate, make sure that the
1550 kept one has a valid got_offset. We also update count. */
1551 if (got_offset != (bfd_vma) -1)
1552 info [dest - 1].got_offset = got_offset;
1553 count = dest;
1557 return count;
1560 /* Find and/or create a descriptor for dynamic symbol info. This will
1561 vary based on global or local symbol, and the addend to the reloc.
1563 We don't sort when inserting. Also, we sort and eliminate
1564 duplicates if there is an unsorted section. Typically, this will
1565 only happen once, because we do all insertions before lookups. We
1566 then use bsearch to do a lookup. This also allows lookups to be
1567 fast. So we have fast insertion (O(log N) due to duplicate check),
1568 fast lookup (O(log N)) and one sort (O(N log N) expected time).
1569 Previously, all lookups were O(N) because of the use of the linked
1570 list and also all insertions were O(N) because of the check for
1571 duplicates. There are some complications here because the array
1572 size grows occasionally, which may add an O(N) factor, but this
1573 should be rare. Also, we free the excess array allocation, which
1574 requires a copy which is O(N), but this only happens once. */
1576 static struct elf64_ia64_dyn_sym_info *
1577 get_dyn_sym_info (struct elf64_ia64_link_hash_table *ia64_info,
1578 struct elf_link_hash_entry *h, bfd *abfd,
1579 const Elf_Internal_Rela *rel, bool create)
1581 struct elf64_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
1582 unsigned int *count_p, *sorted_count_p, *size_p;
1583 unsigned int count, sorted_count, size;
1584 bfd_vma addend = rel ? rel->r_addend : 0;
1585 bfd_size_type amt;
1587 if (h)
1589 struct elf64_ia64_link_hash_entry *global_h;
1591 global_h = (struct elf64_ia64_link_hash_entry *) h;
1592 info_p = &global_h->info;
1593 count_p = &global_h->count;
1594 sorted_count_p = &global_h->sorted_count;
1595 size_p = &global_h->size;
1597 else
1599 struct elf64_ia64_local_hash_entry *loc_h;
1601 loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
1602 if (!loc_h)
1604 BFD_ASSERT (!create);
1605 return NULL;
1608 info_p = &loc_h->info;
1609 count_p = &loc_h->count;
1610 sorted_count_p = &loc_h->sorted_count;
1611 size_p = &loc_h->size;
1614 count = *count_p;
1615 sorted_count = *sorted_count_p;
1616 size = *size_p;
1617 info = *info_p;
1618 if (create)
1620 /* When we create the array, we don't check for duplicates,
1621 except in the previously sorted section if one exists, and
1622 against the last inserted entry. This allows insertions to
1623 be fast. */
1624 if (info)
1626 if (sorted_count)
1628 /* Try bsearch first on the sorted section. */
1629 key.addend = addend;
1630 dyn_i = bsearch (&key, info, sorted_count,
1631 sizeof (*info), addend_compare);
1633 if (dyn_i)
1635 return dyn_i;
1639 /* Do a quick check for the last inserted entry. */
1640 dyn_i = info + count - 1;
1641 if (dyn_i->addend == addend)
1643 return dyn_i;
1647 if (size == 0)
1649 /* It is the very first element. We create the array of size
1650 1. */
1651 size = 1;
1652 amt = size * sizeof (*info);
1653 info = bfd_malloc (amt);
1655 else if (size <= count)
1657 /* We double the array size every time when we reach the
1658 size limit. */
1659 size += size;
1660 amt = size * sizeof (*info);
1661 info = bfd_realloc (info, amt);
1663 else
1664 goto has_space;
1666 if (info == NULL)
1667 return NULL;
1668 *size_p = size;
1669 *info_p = info;
1671 has_space:
1672 /* Append the new one to the array. */
1673 dyn_i = info + count;
1674 memset (dyn_i, 0, sizeof (*dyn_i));
1675 dyn_i->got_offset = (bfd_vma) -1;
1676 dyn_i->addend = addend;
1678 /* We increment count only since the new ones are unsorted and
1679 may have duplicate. */
1680 (*count_p)++;
1682 else
1684 /* It is a lookup without insertion. Sort array if part of the
1685 array isn't sorted. */
1686 if (count != sorted_count)
1688 count = sort_dyn_sym_info (info, count);
1689 *count_p = count;
1690 *sorted_count_p = count;
1693 /* Free unused memory. */
1694 if (size != count)
1696 amt = count * sizeof (*info);
1697 info = bfd_malloc (amt);
1698 if (info != NULL)
1700 memcpy (info, *info_p, amt);
1701 free (*info_p);
1702 *size_p = count;
1703 *info_p = info;
1707 key.addend = addend;
1708 dyn_i = bsearch (&key, info, count,
1709 sizeof (*info), addend_compare);
1712 return dyn_i;
1715 static asection *
1716 get_got (bfd *abfd, struct elf64_ia64_link_hash_table *ia64_info)
1718 asection *got;
1719 bfd *dynobj;
1721 got = ia64_info->root.sgot;
1722 if (!got)
1724 flagword flags;
1726 dynobj = ia64_info->root.dynobj;
1727 if (!dynobj)
1728 ia64_info->root.dynobj = dynobj = abfd;
1730 /* The .got section is always aligned at 8 bytes. */
1731 flags = get_elf_backend_data (dynobj)->dynamic_sec_flags;
1732 got = bfd_make_section_anyway_with_flags (dynobj, ".got",
1733 flags | SEC_SMALL_DATA);
1734 if (got == NULL
1735 || !bfd_set_section_alignment (got, 3))
1736 return NULL;
1737 ia64_info->root.sgot = got;
1740 return got;
1743 /* Create function descriptor section (.opd). This section is called .opd
1744 because it contains "official procedure descriptors". The "official"
1745 refers to the fact that these descriptors are used when taking the address
1746 of a procedure, thus ensuring a unique address for each procedure. */
1748 static asection *
1749 get_fptr (bfd *abfd, struct bfd_link_info *info,
1750 struct elf64_ia64_link_hash_table *ia64_info)
1752 asection *fptr;
1753 bfd *dynobj;
1755 fptr = ia64_info->fptr_sec;
1756 if (!fptr)
1758 dynobj = ia64_info->root.dynobj;
1759 if (!dynobj)
1760 ia64_info->root.dynobj = dynobj = abfd;
1762 fptr = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1763 (SEC_ALLOC
1764 | SEC_LOAD
1765 | SEC_HAS_CONTENTS
1766 | SEC_IN_MEMORY
1767 | (bfd_link_pie (info) ? 0
1768 : SEC_READONLY)
1769 | SEC_LINKER_CREATED));
1770 if (!fptr
1771 || !bfd_set_section_alignment (fptr, 4))
1773 BFD_ASSERT (0);
1774 return NULL;
1777 ia64_info->fptr_sec = fptr;
1779 if (bfd_link_pie (info))
1781 asection *fptr_rel;
1782 fptr_rel = bfd_make_section_anyway_with_flags (dynobj, ".rela.opd",
1783 (SEC_ALLOC | SEC_LOAD
1784 | SEC_HAS_CONTENTS
1785 | SEC_IN_MEMORY
1786 | SEC_LINKER_CREATED
1787 | SEC_READONLY));
1788 if (fptr_rel == NULL
1789 || !bfd_set_section_alignment (fptr_rel, 3))
1791 BFD_ASSERT (0);
1792 return NULL;
1795 ia64_info->rel_fptr_sec = fptr_rel;
1799 return fptr;
1802 static asection *
1803 get_pltoff (bfd *abfd, struct elf64_ia64_link_hash_table *ia64_info)
1805 asection *pltoff;
1806 bfd *dynobj;
1808 pltoff = ia64_info->pltoff_sec;
1809 if (!pltoff)
1811 dynobj = ia64_info->root.dynobj;
1812 if (!dynobj)
1813 ia64_info->root.dynobj = dynobj = abfd;
1815 pltoff = bfd_make_section_anyway_with_flags (dynobj,
1816 ELF_STRING_ia64_pltoff,
1817 (SEC_ALLOC
1818 | SEC_LOAD
1819 | SEC_HAS_CONTENTS
1820 | SEC_IN_MEMORY
1821 | SEC_SMALL_DATA
1822 | SEC_LINKER_CREATED));
1823 if (!pltoff
1824 || !bfd_set_section_alignment (pltoff, 4))
1826 BFD_ASSERT (0);
1827 return NULL;
1830 ia64_info->pltoff_sec = pltoff;
1833 return pltoff;
1836 static asection *
1837 get_reloc_section (bfd *abfd,
1838 struct elf64_ia64_link_hash_table *ia64_info,
1839 asection *sec, bool create)
1841 const char *srel_name;
1842 asection *srel;
1843 bfd *dynobj;
1845 srel_name = (bfd_elf_string_from_elf_section
1846 (abfd, elf_elfheader(abfd)->e_shstrndx,
1847 _bfd_elf_single_rel_hdr (sec)->sh_name));
1848 if (srel_name == NULL)
1849 return NULL;
1851 BFD_ASSERT ((startswith (srel_name, ".rela")
1852 && strcmp (bfd_section_name (sec), srel_name+5) == 0)
1853 || (startswith (srel_name, ".rel")
1854 && strcmp (bfd_section_name (sec), srel_name+4) == 0));
1856 dynobj = ia64_info->root.dynobj;
1857 if (!dynobj)
1858 ia64_info->root.dynobj = dynobj = abfd;
1860 srel = bfd_get_linker_section (dynobj, srel_name);
1861 if (srel == NULL && create)
1863 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
1864 (SEC_ALLOC | SEC_LOAD
1865 | SEC_HAS_CONTENTS
1866 | SEC_IN_MEMORY
1867 | SEC_LINKER_CREATED
1868 | SEC_READONLY));
1869 if (srel == NULL
1870 || !bfd_set_section_alignment (srel, 3))
1871 return NULL;
1874 return srel;
1877 static bool
1878 count_dyn_reloc (bfd *abfd, struct elf64_ia64_dyn_sym_info *dyn_i,
1879 asection *srel, int type)
1881 struct elf64_ia64_dyn_reloc_entry *rent;
1883 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
1884 if (rent->srel == srel && rent->type == type)
1885 break;
1887 if (!rent)
1889 rent = ((struct elf64_ia64_dyn_reloc_entry *)
1890 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
1891 if (!rent)
1892 return false;
1894 rent->next = dyn_i->reloc_entries;
1895 rent->srel = srel;
1896 rent->type = type;
1897 rent->count = 0;
1898 dyn_i->reloc_entries = rent;
1900 rent->count++;
1902 return true;
1905 static bool
1906 elf64_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1907 asection *sec,
1908 const Elf_Internal_Rela *relocs)
1910 struct elf64_ia64_link_hash_table *ia64_info;
1911 const Elf_Internal_Rela *relend;
1912 Elf_Internal_Shdr *symtab_hdr;
1913 const Elf_Internal_Rela *rel;
1914 asection *got, *fptr, *srel, *pltoff;
1915 enum {
1916 NEED_GOT = 1,
1917 NEED_GOTX = 2,
1918 NEED_FPTR = 4,
1919 NEED_PLTOFF = 8,
1920 NEED_MIN_PLT = 16,
1921 NEED_FULL_PLT = 32,
1922 NEED_DYNREL = 64,
1923 NEED_LTOFF_FPTR = 128
1925 int need_entry;
1926 struct elf_link_hash_entry *h;
1927 unsigned long r_symndx;
1928 bool maybe_dynamic;
1930 if (bfd_link_relocatable (info))
1931 return true;
1933 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1934 ia64_info = elf64_ia64_hash_table (info);
1935 if (ia64_info == NULL)
1936 return false;
1938 got = fptr = srel = pltoff = NULL;
1940 relend = relocs + sec->reloc_count;
1942 /* We scan relocations first to create dynamic relocation arrays. We
1943 modified get_dyn_sym_info to allow fast insertion and support fast
1944 lookup in the next loop. */
1945 for (rel = relocs; rel < relend; ++rel)
1947 r_symndx = ELF64_R_SYM (rel->r_info);
1948 if (r_symndx >= symtab_hdr->sh_info)
1950 long indx = r_symndx - symtab_hdr->sh_info;
1951 h = elf_sym_hashes (abfd)[indx];
1952 while (h->root.type == bfd_link_hash_indirect
1953 || h->root.type == bfd_link_hash_warning)
1954 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1956 else
1957 h = NULL;
1959 /* We can only get preliminary data on whether a symbol is
1960 locally or externally defined, as not all of the input files
1961 have yet been processed. Do something with what we know, as
1962 this may help reduce memory usage and processing time later. */
1963 maybe_dynamic = (h && ((!bfd_link_executable (info)
1964 && (!SYMBOLIC_BIND (info, h)
1965 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
1966 || !h->def_regular
1967 || h->root.type == bfd_link_hash_defweak));
1969 need_entry = 0;
1970 switch (ELF64_R_TYPE (rel->r_info))
1972 case R_IA64_TPREL64MSB:
1973 case R_IA64_TPREL64LSB:
1974 case R_IA64_LTOFF_TPREL22:
1975 case R_IA64_DTPREL32MSB:
1976 case R_IA64_DTPREL32LSB:
1977 case R_IA64_DTPREL64MSB:
1978 case R_IA64_DTPREL64LSB:
1979 case R_IA64_LTOFF_DTPREL22:
1980 case R_IA64_DTPMOD64MSB:
1981 case R_IA64_DTPMOD64LSB:
1982 case R_IA64_LTOFF_DTPMOD22:
1983 abort ();
1984 break;
1986 case R_IA64_IPLTMSB:
1987 case R_IA64_IPLTLSB:
1988 break;
1990 case R_IA64_LTOFF_FPTR22:
1991 case R_IA64_LTOFF_FPTR64I:
1992 case R_IA64_LTOFF_FPTR32MSB:
1993 case R_IA64_LTOFF_FPTR32LSB:
1994 case R_IA64_LTOFF_FPTR64MSB:
1995 case R_IA64_LTOFF_FPTR64LSB:
1996 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
1997 break;
1999 case R_IA64_FPTR64I:
2000 case R_IA64_FPTR32MSB:
2001 case R_IA64_FPTR32LSB:
2002 case R_IA64_FPTR64MSB:
2003 case R_IA64_FPTR64LSB:
2004 if (bfd_link_pic (info) || h)
2005 need_entry = NEED_FPTR | NEED_DYNREL;
2006 else
2007 need_entry = NEED_FPTR;
2008 break;
2010 case R_IA64_LTOFF22:
2011 case R_IA64_LTOFF64I:
2012 need_entry = NEED_GOT;
2013 break;
2015 case R_IA64_LTOFF22X:
2016 need_entry = NEED_GOTX;
2017 break;
2019 case R_IA64_PLTOFF22:
2020 case R_IA64_PLTOFF64I:
2021 case R_IA64_PLTOFF64MSB:
2022 case R_IA64_PLTOFF64LSB:
2023 need_entry = NEED_PLTOFF;
2024 if (h)
2026 if (maybe_dynamic)
2027 need_entry |= NEED_MIN_PLT;
2029 else
2031 (*info->callbacks->warning)
2032 (info, _("@pltoff reloc against local symbol"), 0,
2033 abfd, 0, (bfd_vma) 0);
2035 break;
2037 case R_IA64_PCREL21B:
2038 case R_IA64_PCREL60B:
2039 /* Depending on where this symbol is defined, we may or may not
2040 need a full plt entry. Only skip if we know we'll not need
2041 the entry -- static or symbolic, and the symbol definition
2042 has already been seen. */
2043 if (maybe_dynamic && rel->r_addend == 0)
2044 need_entry = NEED_FULL_PLT;
2045 break;
2047 case R_IA64_IMM14:
2048 case R_IA64_IMM22:
2049 case R_IA64_IMM64:
2050 case R_IA64_DIR32MSB:
2051 case R_IA64_DIR32LSB:
2052 case R_IA64_DIR64MSB:
2053 case R_IA64_DIR64LSB:
2054 /* Shared objects will always need at least a REL relocation. */
2055 if (bfd_link_pic (info) || maybe_dynamic)
2056 need_entry = NEED_DYNREL;
2057 break;
2059 case R_IA64_PCREL22:
2060 case R_IA64_PCREL64I:
2061 case R_IA64_PCREL32MSB:
2062 case R_IA64_PCREL32LSB:
2063 case R_IA64_PCREL64MSB:
2064 case R_IA64_PCREL64LSB:
2065 if (maybe_dynamic)
2066 need_entry = NEED_DYNREL;
2067 break;
2070 if (!need_entry)
2071 continue;
2073 if ((need_entry & NEED_FPTR) != 0
2074 && rel->r_addend)
2076 (*info->callbacks->warning)
2077 (info, _("non-zero addend in @fptr reloc"), 0,
2078 abfd, 0, (bfd_vma) 0);
2081 if (get_dyn_sym_info (ia64_info, h, abfd, rel, true) == NULL)
2082 return false;
2085 /* Now, we only do lookup without insertion, which is very fast
2086 with the modified get_dyn_sym_info. */
2087 for (rel = relocs; rel < relend; ++rel)
2089 struct elf64_ia64_dyn_sym_info *dyn_i;
2090 int dynrel_type = R_IA64_NONE;
2092 r_symndx = ELF64_R_SYM (rel->r_info);
2093 if (r_symndx >= symtab_hdr->sh_info)
2095 /* We're dealing with a global symbol -- find its hash entry
2096 and mark it as being referenced. */
2097 long indx = r_symndx - symtab_hdr->sh_info;
2098 h = elf_sym_hashes (abfd)[indx];
2099 while (h->root.type == bfd_link_hash_indirect
2100 || h->root.type == bfd_link_hash_warning)
2101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2103 /* PR15323, ref flags aren't set for references in the same
2104 object. */
2105 h->ref_regular = 1;
2107 else
2108 h = NULL;
2110 /* We can only get preliminary data on whether a symbol is
2111 locally or externally defined, as not all of the input files
2112 have yet been processed. Do something with what we know, as
2113 this may help reduce memory usage and processing time later. */
2114 maybe_dynamic = (h && ((!bfd_link_executable (info)
2115 && (!SYMBOLIC_BIND (info, h)
2116 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2117 || !h->def_regular
2118 || h->root.type == bfd_link_hash_defweak));
2120 need_entry = 0;
2121 switch (ELF64_R_TYPE (rel->r_info))
2123 case R_IA64_TPREL64MSB:
2124 case R_IA64_TPREL64LSB:
2125 case R_IA64_LTOFF_TPREL22:
2126 case R_IA64_DTPREL32MSB:
2127 case R_IA64_DTPREL32LSB:
2128 case R_IA64_DTPREL64MSB:
2129 case R_IA64_DTPREL64LSB:
2130 case R_IA64_LTOFF_DTPREL22:
2131 case R_IA64_DTPMOD64MSB:
2132 case R_IA64_DTPMOD64LSB:
2133 case R_IA64_LTOFF_DTPMOD22:
2134 abort ();
2135 break;
2137 case R_IA64_LTOFF_FPTR22:
2138 case R_IA64_LTOFF_FPTR64I:
2139 case R_IA64_LTOFF_FPTR32MSB:
2140 case R_IA64_LTOFF_FPTR32LSB:
2141 case R_IA64_LTOFF_FPTR64MSB:
2142 case R_IA64_LTOFF_FPTR64LSB:
2143 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2144 break;
2146 case R_IA64_FPTR64I:
2147 case R_IA64_FPTR32MSB:
2148 case R_IA64_FPTR32LSB:
2149 case R_IA64_FPTR64MSB:
2150 case R_IA64_FPTR64LSB:
2151 if (bfd_link_pic (info) || h)
2152 need_entry = NEED_FPTR | NEED_DYNREL;
2153 else
2154 need_entry = NEED_FPTR;
2155 dynrel_type = R_IA64_FPTR64LSB;
2156 break;
2158 case R_IA64_LTOFF22:
2159 case R_IA64_LTOFF64I:
2160 need_entry = NEED_GOT;
2161 break;
2163 case R_IA64_LTOFF22X:
2164 need_entry = NEED_GOTX;
2165 break;
2167 case R_IA64_PLTOFF22:
2168 case R_IA64_PLTOFF64I:
2169 case R_IA64_PLTOFF64MSB:
2170 case R_IA64_PLTOFF64LSB:
2171 need_entry = NEED_PLTOFF;
2172 if (h)
2174 if (maybe_dynamic)
2175 need_entry |= NEED_MIN_PLT;
2177 break;
2179 case R_IA64_PCREL21B:
2180 case R_IA64_PCREL60B:
2181 /* Depending on where this symbol is defined, we may or may not
2182 need a full plt entry. Only skip if we know we'll not need
2183 the entry -- static or symbolic, and the symbol definition
2184 has already been seen. */
2185 if (maybe_dynamic && rel->r_addend == 0)
2186 need_entry = NEED_FULL_PLT;
2187 break;
2189 case R_IA64_IMM14:
2190 case R_IA64_IMM22:
2191 case R_IA64_IMM64:
2192 case R_IA64_DIR32MSB:
2193 case R_IA64_DIR32LSB:
2194 case R_IA64_DIR64MSB:
2195 case R_IA64_DIR64LSB:
2196 /* Shared objects will always need at least a REL relocation. */
2197 if (bfd_link_pic (info) || maybe_dynamic)
2198 need_entry = NEED_DYNREL;
2199 dynrel_type = R_IA64_DIR64LSB;
2200 break;
2202 case R_IA64_IPLTMSB:
2203 case R_IA64_IPLTLSB:
2204 break;
2206 case R_IA64_PCREL22:
2207 case R_IA64_PCREL64I:
2208 case R_IA64_PCREL32MSB:
2209 case R_IA64_PCREL32LSB:
2210 case R_IA64_PCREL64MSB:
2211 case R_IA64_PCREL64LSB:
2212 if (maybe_dynamic)
2213 need_entry = NEED_DYNREL;
2214 dynrel_type = R_IA64_PCREL64LSB;
2215 break;
2218 if (!need_entry)
2219 continue;
2221 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, false);
2223 /* Record whether or not this is a local symbol. */
2224 dyn_i->h = h;
2226 /* Create what's needed. */
2227 if (need_entry & (NEED_GOT | NEED_GOTX))
2229 if (!got)
2231 got = get_got (abfd, ia64_info);
2232 if (!got)
2233 return false;
2235 if (need_entry & NEED_GOT)
2236 dyn_i->want_got = 1;
2237 if (need_entry & NEED_GOTX)
2238 dyn_i->want_gotx = 1;
2240 if (need_entry & NEED_FPTR)
2242 /* Create the .opd section. */
2243 if (!fptr)
2245 fptr = get_fptr (abfd, info, ia64_info);
2246 if (!fptr)
2247 return false;
2249 dyn_i->want_fptr = 1;
2251 if (need_entry & NEED_LTOFF_FPTR)
2252 dyn_i->want_ltoff_fptr = 1;
2253 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
2255 if (!ia64_info->root.dynobj)
2256 ia64_info->root.dynobj = abfd;
2257 h->needs_plt = 1;
2258 dyn_i->want_plt = 1;
2260 if (need_entry & NEED_FULL_PLT)
2261 dyn_i->want_plt2 = 1;
2262 if (need_entry & NEED_PLTOFF)
2264 /* This is needed here, in case @pltoff is used in a non-shared
2265 link. */
2266 if (!pltoff)
2268 pltoff = get_pltoff (abfd, ia64_info);
2269 if (!pltoff)
2270 return false;
2273 dyn_i->want_pltoff = 1;
2275 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
2277 if (!srel)
2279 srel = get_reloc_section (abfd, ia64_info, sec, true);
2280 if (!srel)
2281 return false;
2283 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
2284 return false;
2288 return true;
2291 /* For cleanliness, and potentially faster dynamic loading, allocate
2292 external GOT entries first. */
2294 static bool
2295 allocate_global_data_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2296 void * data)
2298 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2300 if ((dyn_i->want_got || dyn_i->want_gotx)
2301 && ! dyn_i->want_fptr
2302 && elf64_ia64_dynamic_symbol_p (dyn_i->h))
2304 /* GOT entry with FPTR is done by allocate_global_fptr_got. */
2305 dyn_i->got_offset = x->ofs;
2306 x->ofs += 8;
2308 return true;
2311 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
2313 static bool
2314 allocate_global_fptr_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2315 void * data)
2317 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2319 if (dyn_i->want_got
2320 && dyn_i->want_fptr
2321 && elf64_ia64_dynamic_symbol_p (dyn_i->h))
2323 dyn_i->got_offset = x->ofs;
2324 x->ofs += 8;
2326 return true;
2329 /* Lastly, allocate all the GOT entries for local data. */
2331 static bool
2332 allocate_local_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2333 void * data)
2335 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *) data;
2337 if ((dyn_i->want_got || dyn_i->want_gotx)
2338 && !elf64_ia64_dynamic_symbol_p (dyn_i->h))
2340 dyn_i->got_offset = x->ofs;
2341 x->ofs += 8;
2343 return true;
2346 /* Allocate function descriptors. We can do these for every function
2347 in a main executable that is not exported. */
2349 static bool
2350 allocate_fptr (struct elf64_ia64_dyn_sym_info *dyn_i, void * data)
2352 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *) data;
2354 if (dyn_i->want_fptr)
2356 struct elf_link_hash_entry *h = dyn_i->h;
2358 if (h)
2359 while (h->root.type == bfd_link_hash_indirect
2360 || h->root.type == bfd_link_hash_warning)
2361 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2363 if (h == NULL || !h->def_dynamic)
2365 /* A non dynamic symbol. */
2366 dyn_i->fptr_offset = x->ofs;
2367 x->ofs += 16;
2369 else
2370 dyn_i->want_fptr = 0;
2372 return true;
2375 /* Allocate all the minimal PLT entries. */
2377 static bool
2378 allocate_plt_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2379 void * data ATTRIBUTE_UNUSED)
2381 if (dyn_i->want_plt)
2383 struct elf_link_hash_entry *h = dyn_i->h;
2385 if (h)
2386 while (h->root.type == bfd_link_hash_indirect
2387 || h->root.type == bfd_link_hash_warning)
2388 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2390 /* ??? Versioned symbols seem to lose NEEDS_PLT. */
2391 if (elf64_ia64_dynamic_symbol_p (h))
2393 dyn_i->want_pltoff = 1;
2395 else
2397 dyn_i->want_plt = 0;
2398 dyn_i->want_plt2 = 0;
2401 return true;
2404 /* Allocate all the full PLT entries. */
2406 static bool
2407 allocate_plt2_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2408 void * data)
2410 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2412 if (dyn_i->want_plt2)
2414 struct elf_link_hash_entry *h = dyn_i->h;
2415 bfd_size_type ofs = x->ofs;
2417 dyn_i->plt2_offset = ofs;
2418 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2420 while (h->root.type == bfd_link_hash_indirect
2421 || h->root.type == bfd_link_hash_warning)
2422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2423 dyn_i->h->plt.offset = ofs;
2425 return true;
2428 /* Allocate all the PLTOFF entries requested by relocations and
2429 plt entries. We can't share space with allocated FPTR entries,
2430 because the latter are not necessarily addressable by the GP.
2431 ??? Relaxation might be able to determine that they are. */
2433 static bool
2434 allocate_pltoff_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2435 void * data)
2437 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2439 if (dyn_i->want_pltoff)
2441 dyn_i->pltoff_offset = x->ofs;
2442 x->ofs += 16;
2444 return true;
2447 /* Allocate dynamic relocations for those symbols that turned out
2448 to be dynamic. */
2450 static bool
2451 allocate_dynrel_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2452 void * data)
2454 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2455 struct elf64_ia64_link_hash_table *ia64_info;
2456 struct elf64_ia64_dyn_reloc_entry *rent;
2457 bool dynamic_symbol, shared, resolved_zero;
2458 struct elf64_ia64_link_hash_entry *h_ia64;
2460 ia64_info = elf64_ia64_hash_table (x->info);
2461 if (ia64_info == NULL)
2462 return false;
2464 /* Note that this can't be used in relation to FPTR relocs below. */
2465 dynamic_symbol = elf64_ia64_dynamic_symbol_p (dyn_i->h);
2467 shared = bfd_link_pic (x->info);
2468 resolved_zero = (dyn_i->h
2469 && ELF_ST_VISIBILITY (dyn_i->h->other)
2470 && dyn_i->h->root.type == bfd_link_hash_undefweak);
2472 /* Take care of the GOT and PLT relocations. */
2474 if ((!resolved_zero
2475 && (dynamic_symbol || shared)
2476 && (dyn_i->want_got || dyn_i->want_gotx))
2477 || (dyn_i->want_ltoff_fptr
2478 && dyn_i->h
2479 && dyn_i->h->def_dynamic))
2481 /* VMS: FIX64. */
2482 if (dyn_i->h != NULL && dyn_i->h->def_dynamic)
2484 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2485 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2486 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2487 ia64_info->fixups_sec->size +=
2488 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2492 if (ia64_info->rel_fptr_sec && dyn_i->want_fptr)
2494 /* VMS: only image reloc. */
2495 if (dyn_i->h == NULL || dyn_i->h->root.type != bfd_link_hash_undefweak)
2496 ia64_info->rel_fptr_sec->size += sizeof (Elf64_External_Rela);
2499 if (!resolved_zero && dyn_i->want_pltoff)
2501 /* VMS: FIXFD. */
2502 if (dyn_i->h != NULL && dyn_i->h->def_dynamic)
2504 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2505 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2506 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2507 ia64_info->fixups_sec->size +=
2508 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2512 /* Take care of the normal data relocations. */
2514 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2516 switch (rent->type)
2518 case R_IA64_FPTR32LSB:
2519 case R_IA64_FPTR64LSB:
2520 /* Allocate one iff !want_fptr and not PIE, which by this point
2521 will be true only if we're actually allocating one statically
2522 in the main executable. Position independent executables
2523 need a relative reloc. */
2524 if (dyn_i->want_fptr && !bfd_link_pie (x->info))
2525 continue;
2526 break;
2527 case R_IA64_PCREL32LSB:
2528 case R_IA64_PCREL64LSB:
2529 if (!dynamic_symbol)
2530 continue;
2531 break;
2532 case R_IA64_DIR32LSB:
2533 case R_IA64_DIR64LSB:
2534 if (!dynamic_symbol && !shared)
2535 continue;
2536 break;
2537 case R_IA64_IPLTLSB:
2538 if (!dynamic_symbol && !shared)
2539 continue;
2540 break;
2541 case R_IA64_DTPREL32LSB:
2542 case R_IA64_TPREL64LSB:
2543 case R_IA64_DTPREL64LSB:
2544 case R_IA64_DTPMOD64LSB:
2545 break;
2546 default:
2547 abort ();
2550 /* Add a fixup. */
2551 if (!dynamic_symbol)
2552 abort ();
2554 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2555 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2556 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2557 ia64_info->fixups_sec->size +=
2558 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2561 return true;
2564 static bool
2565 elf64_ia64_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2566 struct elf_link_hash_entry *h)
2568 /* ??? Undefined symbols with PLT entries should be re-defined
2569 to be the PLT entry. */
2571 /* If this is a weak symbol, and there is a real definition, the
2572 processor independent code will have arranged for us to see the
2573 real definition first, and we can just use the same value. */
2574 if (h->is_weakalias)
2576 struct elf_link_hash_entry *def = weakdef (h);
2577 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2578 h->root.u.def.section = def->root.u.def.section;
2579 h->root.u.def.value = def->root.u.def.value;
2580 return true;
2583 /* If this is a reference to a symbol defined by a dynamic object which
2584 is not a function, we might allocate the symbol in our .dynbss section
2585 and allocate a COPY dynamic relocation.
2587 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2588 of hackery. */
2590 return true;
2593 static bool
2594 elf64_ia64_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2595 struct bfd_link_info *info)
2597 struct elf64_ia64_allocate_data data;
2598 struct elf64_ia64_link_hash_table *ia64_info;
2599 asection *sec;
2600 bfd *dynobj;
2601 struct elf_link_hash_table *hash_table;
2603 hash_table = elf_hash_table (info);
2604 ia64_info = elf64_ia64_hash_table (info);
2605 if (ia64_info == NULL)
2606 return false;
2607 dynobj = hash_table->dynobj;
2608 if (dynobj == NULL)
2609 return true;
2610 data.info = info;
2612 /* Allocate the GOT entries. */
2614 if (ia64_info->root.sgot)
2616 data.ofs = 0;
2617 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2618 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2619 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
2620 ia64_info->root.sgot->size = data.ofs;
2623 /* Allocate the FPTR entries. */
2625 if (ia64_info->fptr_sec)
2627 data.ofs = 0;
2628 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
2629 ia64_info->fptr_sec->size = data.ofs;
2632 /* Now that we've seen all of the input files, we can decide which
2633 symbols need plt entries. Allocate the minimal PLT entries first.
2634 We do this even though dynamic_sections_created may be FALSE, because
2635 this has the side-effect of clearing want_plt and want_plt2. */
2637 data.ofs = 0;
2638 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
2640 /* Align the pointer for the plt2 entries. */
2641 data.ofs = (data.ofs + 31) & (bfd_vma) -32;
2643 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
2644 if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
2646 /* FIXME: we always reserve the memory for dynamic linker even if
2647 there are no PLT entries since dynamic linker may assume the
2648 reserved memory always exists. */
2650 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2652 ia64_info->root.splt->size = data.ofs;
2655 /* Allocate the PLTOFF entries. */
2657 if (ia64_info->pltoff_sec)
2659 data.ofs = 0;
2660 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
2661 ia64_info->pltoff_sec->size = data.ofs;
2664 if (ia64_info->root.dynamic_sections_created)
2666 /* Allocate space for the dynamic relocations that turned out to be
2667 required. */
2668 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
2671 /* We have now determined the sizes of the various dynamic sections.
2672 Allocate memory for them. */
2673 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2675 bool strip;
2677 if (!(sec->flags & SEC_LINKER_CREATED))
2678 continue;
2680 /* If we don't need this section, strip it from the output file.
2681 There were several sections primarily related to dynamic
2682 linking that must be create before the linker maps input
2683 sections to output sections. The linker does that before
2684 bfd_elf_size_dynamic_sections is called, and it is that
2685 function which decides whether anything needs to go into
2686 these sections. */
2688 strip = (sec->size == 0);
2690 if (sec == ia64_info->root.sgot)
2691 strip = false;
2692 else if (sec == ia64_info->root.srelgot)
2694 if (strip)
2695 ia64_info->root.srelgot = NULL;
2696 else
2697 /* We use the reloc_count field as a counter if we need to
2698 copy relocs into the output file. */
2699 sec->reloc_count = 0;
2701 else if (sec == ia64_info->fptr_sec)
2703 if (strip)
2704 ia64_info->fptr_sec = NULL;
2706 else if (sec == ia64_info->rel_fptr_sec)
2708 if (strip)
2709 ia64_info->rel_fptr_sec = NULL;
2710 else
2711 /* We use the reloc_count field as a counter if we need to
2712 copy relocs into the output file. */
2713 sec->reloc_count = 0;
2715 else if (sec == ia64_info->root.splt)
2717 if (strip)
2718 ia64_info->root.splt = NULL;
2720 else if (sec == ia64_info->pltoff_sec)
2722 if (strip)
2723 ia64_info->pltoff_sec = NULL;
2725 else if (sec == ia64_info->fixups_sec)
2727 if (strip)
2728 ia64_info->fixups_sec = NULL;
2730 else if (sec == ia64_info->transfer_sec)
2734 else
2736 const char *name;
2738 /* It's OK to base decisions on the section name, because none
2739 of the dynobj section names depend upon the input files. */
2740 name = bfd_section_name (sec);
2742 if (strcmp (name, ".got.plt") == 0)
2743 strip = false;
2744 else if (startswith (name, ".rel"))
2746 if (!strip)
2748 /* We use the reloc_count field as a counter if we need to
2749 copy relocs into the output file. */
2750 sec->reloc_count = 0;
2753 else
2754 continue;
2757 if (strip)
2758 sec->flags |= SEC_EXCLUDE;
2759 else
2761 /* Allocate memory for the section contents. */
2762 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
2763 if (sec->contents == NULL && sec->size != 0)
2764 return false;
2768 if (elf_hash_table (info)->dynamic_sections_created)
2770 bfd *abfd;
2771 asection *dynsec;
2772 asection *dynstrsec;
2773 Elf_Internal_Dyn dyn;
2774 const struct elf_backend_data *bed;
2775 unsigned int shl_num = 0;
2776 bfd_vma fixups_off = 0;
2777 bfd_vma strdyn_off;
2778 unsigned int time_hi, time_lo;
2780 /* The .dynamic section must exist and be empty. */
2781 dynsec = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
2782 BFD_ASSERT (dynsec != NULL);
2783 BFD_ASSERT (dynsec->size == 0);
2785 dynstrsec = bfd_get_linker_section (hash_table->dynobj, ".vmsdynstr");
2786 BFD_ASSERT (dynstrsec != NULL);
2787 BFD_ASSERT (dynstrsec->size == 0);
2788 dynstrsec->size = 1; /* Initial blank. */
2790 /* Ident + link time. */
2791 vms_get_time (&time_hi, &time_lo);
2793 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_IDENT, 0))
2794 return false;
2795 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_LINKTIME,
2796 ((uint64_t) time_hi << 32)
2797 + time_lo))
2798 return false;
2800 /* Strtab. */
2801 strdyn_off = dynsec->size;
2802 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_STRTAB_OFFSET, 0))
2803 return false;
2804 if (!_bfd_elf_add_dynamic_entry (info, DT_STRSZ, 0))
2805 return false;
2807 /* PLTGOT */
2808 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_PLTGOT_SEG, 0))
2809 return false;
2810 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_PLTGOT_OFFSET, 0))
2811 return false;
2813 /* Misc. */
2814 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FPMODE, 0x9800000))
2815 return false;
2816 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_LNKFLAGS,
2817 VMS_LF_IMGSTA | VMS_LF_MAIN))
2818 return false;
2820 /* Add entries for shared libraries. */
2821 for (abfd = info->input_bfds; abfd; abfd = abfd->link.next)
2823 char *soname;
2824 size_t soname_len;
2825 bfd_size_type strindex;
2826 bfd_byte *newcontents;
2827 bfd_vma fixups_shl_off;
2829 if (!(abfd->flags & DYNAMIC))
2830 continue;
2831 BFD_ASSERT (abfd->xvec == output_bfd->xvec);
2833 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_NEEDED_IDENT,
2834 elf_ia64_vms_ident (abfd)))
2835 return false;
2837 soname = vms_get_module_name (bfd_get_filename (abfd), true);
2838 if (soname == NULL)
2839 return false;
2840 strindex = dynstrsec->size;
2841 soname_len = strlen (soname) + 1;
2842 newcontents = (bfd_byte *) bfd_realloc (dynstrsec->contents,
2843 strindex + soname_len);
2844 if (newcontents == NULL)
2845 return false;
2846 memcpy (newcontents + strindex, soname, soname_len);
2847 dynstrsec->size += soname_len;
2848 dynstrsec->contents = newcontents;
2850 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2851 return false;
2853 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FIXUP_NEEDED,
2854 shl_num))
2855 return false;
2856 shl_num++;
2858 /* The fixups_off was in fact containing the size of the fixup
2859 section. Remap into the offset. */
2860 fixups_shl_off = elf_ia64_vms_tdata (abfd)->fixups_off;
2861 elf_ia64_vms_tdata (abfd)->fixups_off = fixups_off;
2863 if (!_bfd_elf_add_dynamic_entry
2864 (info, DT_IA_64_VMS_FIXUP_RELA_CNT,
2865 fixups_shl_off / sizeof (Elf64_External_VMS_IMAGE_FIXUP)))
2866 return false;
2867 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FIXUP_RELA_OFF,
2868 fixups_off))
2869 return false;
2870 fixups_off += fixups_shl_off;
2873 /* Unwind. */
2874 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWINDSZ, 0))
2875 return false;
2876 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_CODSEG, 0))
2877 return false;
2878 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_INFOSEG, 0))
2879 return false;
2880 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_OFFSET, 0))
2881 return false;
2882 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_SEG, 0))
2883 return false;
2885 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0xdead))
2886 return false;
2888 /* Fix the strtab entries. */
2889 bed = get_elf_backend_data (hash_table->dynobj);
2891 if (dynstrsec->size > 1)
2892 dynstrsec->contents[0] = 0;
2893 else
2894 dynstrsec->size = 0;
2896 /* Note: one 'spare' (ie DT_NULL) entry is added by
2897 bfd_elf_size_dynsym_hash_dynstr. */
2898 dyn.d_tag = DT_IA_64_VMS_STRTAB_OFFSET;
2899 dyn.d_un.d_val = dynsec->size /* + sizeof (Elf64_External_Dyn) */;
2900 bed->s->swap_dyn_out (hash_table->dynobj, &dyn,
2901 dynsec->contents + strdyn_off);
2903 dyn.d_tag = DT_STRSZ;
2904 dyn.d_un.d_val = dynstrsec->size;
2905 bed->s->swap_dyn_out (hash_table->dynobj, &dyn,
2906 dynsec->contents + strdyn_off + bed->s->sizeof_dyn);
2908 elf_ia64_vms_tdata (output_bfd)->needed_count = shl_num;
2910 /* Note section. */
2911 if (!create_ia64_vms_notes (output_bfd, info, time_hi, time_lo))
2912 return false;
2915 /* ??? Perhaps force __gp local. */
2917 return true;
2920 static void
2921 elf64_ia64_install_fixup (bfd *output_bfd,
2922 struct elf64_ia64_link_hash_table *ia64_info,
2923 struct elf_link_hash_entry *h,
2924 unsigned int type, asection *sec, bfd_vma offset,
2925 bfd_vma addend)
2927 asection *relsec;
2928 Elf64_External_VMS_IMAGE_FIXUP *fixup;
2929 struct elf64_ia64_link_hash_entry *h_ia64;
2930 bfd_vma fixoff;
2931 Elf_Internal_Phdr *phdr;
2933 if (h == NULL || !h->def_dynamic)
2934 abort ();
2936 h_ia64 = (struct elf64_ia64_link_hash_entry *) h;
2937 fixoff = elf_ia64_vms_tdata (h_ia64->shl)->fixups_off;
2938 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2939 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2940 relsec = ia64_info->fixups_sec;
2942 fixup = (Elf64_External_VMS_IMAGE_FIXUP *)(relsec->contents + fixoff);
2943 offset += sec->output_section->vma + sec->output_offset;
2945 /* FIXME: this is slow. We should cache the last one used, or create a
2946 map. */
2947 phdr = _bfd_elf_find_segment_containing_section
2948 (output_bfd, sec->output_section);
2949 BFD_ASSERT (phdr != NULL);
2951 bfd_putl64 (offset - phdr->p_vaddr, fixup->fixup_offset);
2952 bfd_putl32 (type, fixup->type);
2953 bfd_putl32 (phdr - elf_tdata (output_bfd)->phdr, fixup->fixup_seg);
2954 bfd_putl64 (addend, fixup->addend);
2955 bfd_putl32 (h->root.u.def.value, fixup->symvec_index);
2956 bfd_putl32 (2, fixup->data_type);
2959 /* Store an entry for target address TARGET_ADDR in the linkage table
2960 and return the gp-relative address of the linkage table entry. */
2962 static bfd_vma
2963 set_got_entry (bfd *abfd, struct bfd_link_info *info,
2964 struct elf64_ia64_dyn_sym_info *dyn_i,
2965 bfd_vma addend, bfd_vma value, unsigned int dyn_r_type)
2967 struct elf64_ia64_link_hash_table *ia64_info;
2968 asection *got_sec;
2969 bool done;
2970 bfd_vma got_offset;
2972 ia64_info = elf64_ia64_hash_table (info);
2973 if (ia64_info == NULL)
2974 return 0;
2976 got_sec = ia64_info->root.sgot;
2978 switch (dyn_r_type)
2980 case R_IA64_TPREL64LSB:
2981 case R_IA64_DTPMOD64LSB:
2982 case R_IA64_DTPREL32LSB:
2983 case R_IA64_DTPREL64LSB:
2984 abort ();
2985 break;
2986 default:
2987 done = dyn_i->got_done;
2988 dyn_i->got_done = true;
2989 got_offset = dyn_i->got_offset;
2990 break;
2993 BFD_ASSERT ((got_offset & 7) == 0);
2995 if (! done)
2997 /* Store the target address in the linkage table entry. */
2998 bfd_put_64 (abfd, value, got_sec->contents + got_offset);
3000 /* Install a dynamic relocation if needed. */
3001 if (((bfd_link_pic (info)
3002 && (!dyn_i->h
3003 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3004 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3005 || elf64_ia64_dynamic_symbol_p (dyn_i->h))
3006 && (!dyn_i->want_ltoff_fptr
3007 || !bfd_link_pie (info)
3008 || !dyn_i->h
3009 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3011 if (!dyn_i->h || !dyn_i->h->def_dynamic)
3013 dyn_r_type = R_IA64_REL64LSB;
3014 addend = value;
3017 /* VMS: install a FIX32 or FIX64. */
3018 switch (dyn_r_type)
3020 case R_IA64_DIR32LSB:
3021 case R_IA64_FPTR32LSB:
3022 dyn_r_type = R_IA64_VMS_FIX32;
3023 break;
3024 case R_IA64_DIR64LSB:
3025 case R_IA64_FPTR64LSB:
3026 dyn_r_type = R_IA64_VMS_FIX64;
3027 break;
3028 default:
3029 BFD_ASSERT (false);
3030 break;
3032 elf64_ia64_install_fixup
3033 (info->output_bfd, ia64_info, dyn_i->h,
3034 dyn_r_type, got_sec, got_offset, addend);
3038 /* Return the address of the linkage table entry. */
3039 value = (got_sec->output_section->vma
3040 + got_sec->output_offset
3041 + got_offset);
3043 return value;
3046 /* Fill in a function descriptor consisting of the function's code
3047 address and its global pointer. Return the descriptor's address. */
3049 static bfd_vma
3050 set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
3051 struct elf64_ia64_dyn_sym_info *dyn_i,
3052 bfd_vma value)
3054 struct elf64_ia64_link_hash_table *ia64_info;
3055 asection *fptr_sec;
3057 ia64_info = elf64_ia64_hash_table (info);
3058 if (ia64_info == NULL)
3059 return 0;
3061 fptr_sec = ia64_info->fptr_sec;
3063 if (!dyn_i->fptr_done)
3065 dyn_i->fptr_done = 1;
3067 /* Fill in the function descriptor. */
3068 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
3069 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
3070 fptr_sec->contents + dyn_i->fptr_offset + 8);
3073 /* Return the descriptor's address. */
3074 value = (fptr_sec->output_section->vma
3075 + fptr_sec->output_offset
3076 + dyn_i->fptr_offset);
3078 return value;
3081 /* Fill in a PLTOFF entry consisting of the function's code address
3082 and its global pointer. Return the descriptor's address. */
3084 static bfd_vma
3085 set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
3086 struct elf64_ia64_dyn_sym_info *dyn_i,
3087 bfd_vma value, bool is_plt)
3089 struct elf64_ia64_link_hash_table *ia64_info;
3090 asection *pltoff_sec;
3092 ia64_info = elf64_ia64_hash_table (info);
3093 if (ia64_info == NULL)
3094 return 0;
3096 pltoff_sec = ia64_info->pltoff_sec;
3098 /* Don't do anything if this symbol uses a real PLT entry. In
3099 that case, we'll fill this in during finish_dynamic_symbol. */
3100 if ((! dyn_i->want_plt || is_plt)
3101 && !dyn_i->pltoff_done)
3103 bfd_vma gp = _bfd_get_gp_value (abfd);
3105 /* Fill in the function descriptor. */
3106 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
3107 bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
3109 /* Install dynamic relocations if needed. */
3110 if (!is_plt
3111 && bfd_link_pic (info)
3112 && (!dyn_i->h
3113 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3114 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3116 /* VMS: */
3117 abort ();
3120 dyn_i->pltoff_done = 1;
3123 /* Return the descriptor's address. */
3124 value = (pltoff_sec->output_section->vma
3125 + pltoff_sec->output_offset
3126 + dyn_i->pltoff_offset);
3128 return value;
3131 /* Called through qsort to sort the .IA_64.unwind section during a
3132 non-relocatable link. Set elf64_ia64_unwind_entry_compare_bfd
3133 to the output bfd so we can do proper endianness frobbing. */
3135 static bfd *elf64_ia64_unwind_entry_compare_bfd;
3137 static int
3138 elf64_ia64_unwind_entry_compare (const void * a, const void * b)
3140 bfd_vma av, bv;
3142 av = bfd_get_64 (elf64_ia64_unwind_entry_compare_bfd, a);
3143 bv = bfd_get_64 (elf64_ia64_unwind_entry_compare_bfd, b);
3145 return (av < bv ? -1 : av > bv ? 1 : 0);
3148 /* Make sure we've got ourselves a nice fat __gp value. */
3149 static bool
3150 elf64_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info, bool final)
3152 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3153 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3154 struct elf_link_hash_entry *gp;
3155 bfd_vma gp_val;
3156 asection *os;
3157 struct elf64_ia64_link_hash_table *ia64_info;
3159 ia64_info = elf64_ia64_hash_table (info);
3160 if (ia64_info == NULL)
3161 return false;
3163 /* Find the min and max vma of all sections marked short. Also collect
3164 min and max vma of any type, for use in selecting a nice gp. */
3165 for (os = abfd->sections; os ; os = os->next)
3167 bfd_vma lo, hi;
3169 if ((os->flags & SEC_ALLOC) == 0)
3170 continue;
3172 lo = os->vma;
3173 /* When this function is called from elfNN_ia64_final_link
3174 the correct value to use is os->size. When called from
3175 elfNN_ia64_relax_section we are in the middle of section
3176 sizing; some sections will already have os->size set, others
3177 will have os->size zero and os->rawsize the previous size. */
3178 hi = os->vma + (!final && os->rawsize ? os->rawsize : os->size);
3179 if (hi < lo)
3180 hi = (bfd_vma) -1;
3182 if (min_vma > lo)
3183 min_vma = lo;
3184 if (max_vma < hi)
3185 max_vma = hi;
3186 if (os->flags & SEC_SMALL_DATA)
3188 if (min_short_vma > lo)
3189 min_short_vma = lo;
3190 if (max_short_vma < hi)
3191 max_short_vma = hi;
3195 if (ia64_info->min_short_sec)
3197 if (min_short_vma
3198 > (ia64_info->min_short_sec->vma
3199 + ia64_info->min_short_offset))
3200 min_short_vma = (ia64_info->min_short_sec->vma
3201 + ia64_info->min_short_offset);
3202 if (max_short_vma
3203 < (ia64_info->max_short_sec->vma
3204 + ia64_info->max_short_offset))
3205 max_short_vma = (ia64_info->max_short_sec->vma
3206 + ia64_info->max_short_offset);
3209 /* See if the user wants to force a value. */
3210 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3211 false, false);
3213 if (gp
3214 && (gp->root.type == bfd_link_hash_defined
3215 || gp->root.type == bfd_link_hash_defweak))
3217 asection *gp_sec = gp->root.u.def.section;
3218 gp_val = (gp->root.u.def.value
3219 + gp_sec->output_section->vma
3220 + gp_sec->output_offset);
3222 else
3224 /* Pick a sensible value. */
3226 if (ia64_info->min_short_sec)
3228 bfd_vma short_range = max_short_vma - min_short_vma;
3230 /* If min_short_sec is set, pick one in the middle bewteen
3231 min_short_vma and max_short_vma. */
3232 if (short_range >= 0x400000)
3233 goto overflow;
3234 gp_val = min_short_vma + short_range / 2;
3236 else
3238 asection *got_sec = ia64_info->root.sgot;
3240 /* Start with just the address of the .got. */
3241 if (got_sec)
3242 gp_val = got_sec->output_section->vma;
3243 else if (max_short_vma != 0)
3244 gp_val = min_short_vma;
3245 else if (max_vma - min_vma < 0x200000)
3246 gp_val = min_vma;
3247 else
3248 gp_val = max_vma - 0x200000 + 8;
3251 /* If it is possible to address the entire image, but we
3252 don't with the choice above, adjust. */
3253 if (max_vma - min_vma < 0x400000
3254 && (max_vma - gp_val >= 0x200000
3255 || gp_val - min_vma > 0x200000))
3256 gp_val = min_vma + 0x200000;
3257 else if (max_short_vma != 0)
3259 /* If we don't cover all the short data, adjust. */
3260 if (max_short_vma - gp_val >= 0x200000)
3261 gp_val = min_short_vma + 0x200000;
3263 /* If we're addressing stuff past the end, adjust back. */
3264 if (gp_val > max_vma)
3265 gp_val = max_vma - 0x200000 + 8;
3269 /* Validate whether all SHF_IA_64_SHORT sections are within
3270 range of the chosen GP. */
3272 if (max_short_vma != 0)
3274 if (max_short_vma - min_short_vma >= 0x400000)
3276 overflow:
3277 _bfd_error_handler
3278 /* xgettext:c-format */
3279 (_("%pB: short data segment overflowed (%#" PRIx64 " >= 0x400000)"),
3280 abfd, (uint64_t) (max_short_vma - min_short_vma));
3281 return false;
3283 else if ((gp_val > min_short_vma
3284 && gp_val - min_short_vma > 0x200000)
3285 || (gp_val < max_short_vma
3286 && max_short_vma - gp_val >= 0x200000))
3288 _bfd_error_handler
3289 (_("%pB: __gp does not cover short data segment"), abfd);
3290 return false;
3294 _bfd_set_gp_value (abfd, gp_val);
3296 return true;
3299 static bool
3300 elf64_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
3302 struct elf64_ia64_link_hash_table *ia64_info;
3303 asection *unwind_output_sec;
3305 ia64_info = elf64_ia64_hash_table (info);
3306 if (ia64_info == NULL)
3307 return false;
3309 /* Make sure we've got ourselves a nice fat __gp value. */
3310 if (!bfd_link_relocatable (info))
3312 bfd_vma gp_val;
3313 struct elf_link_hash_entry *gp;
3315 /* We assume after gp is set, section size will only decrease. We
3316 need to adjust gp for it. */
3317 _bfd_set_gp_value (abfd, 0);
3318 if (! elf64_ia64_choose_gp (abfd, info, true))
3319 return false;
3320 gp_val = _bfd_get_gp_value (abfd);
3322 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3323 false, false);
3324 if (gp)
3326 gp->root.type = bfd_link_hash_defined;
3327 gp->root.u.def.value = gp_val;
3328 gp->root.u.def.section = bfd_abs_section_ptr;
3332 /* If we're producing a final executable, we need to sort the contents
3333 of the .IA_64.unwind section. Force this section to be relocated
3334 into memory rather than written immediately to the output file. */
3335 unwind_output_sec = NULL;
3336 if (!bfd_link_relocatable (info))
3338 asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3339 if (s)
3341 unwind_output_sec = s->output_section;
3342 unwind_output_sec->contents
3343 = bfd_malloc (unwind_output_sec->size);
3344 if (unwind_output_sec->contents == NULL)
3345 return false;
3349 /* Invoke the regular ELF backend linker to do all the work. */
3350 if (!bfd_elf_final_link (abfd, info))
3351 return false;
3353 if (unwind_output_sec)
3355 elf64_ia64_unwind_entry_compare_bfd = abfd;
3356 qsort (unwind_output_sec->contents,
3357 (size_t) (unwind_output_sec->size / 24),
3359 elf64_ia64_unwind_entry_compare);
3361 if (! bfd_set_section_contents (abfd, unwind_output_sec,
3362 unwind_output_sec->contents, (bfd_vma) 0,
3363 unwind_output_sec->size))
3364 return false;
3367 return true;
3370 static int
3371 elf64_ia64_relocate_section (bfd *output_bfd,
3372 struct bfd_link_info *info,
3373 bfd *input_bfd,
3374 asection *input_section,
3375 bfd_byte *contents,
3376 Elf_Internal_Rela *relocs,
3377 Elf_Internal_Sym *local_syms,
3378 asection **local_sections)
3380 struct elf64_ia64_link_hash_table *ia64_info;
3381 Elf_Internal_Shdr *symtab_hdr;
3382 Elf_Internal_Rela *rel;
3383 Elf_Internal_Rela *relend;
3384 bool ret_val = true; /* for non-fatal errors */
3385 bfd_vma gp_val;
3387 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3388 ia64_info = elf64_ia64_hash_table (info);
3389 if (ia64_info == NULL)
3390 return false;
3392 /* Infect various flags from the input section to the output section. */
3393 if (bfd_link_relocatable (info))
3395 bfd_vma flags;
3397 flags = elf_section_data(input_section)->this_hdr.sh_flags;
3398 flags &= SHF_IA_64_NORECOV;
3400 elf_section_data(input_section->output_section)
3401 ->this_hdr.sh_flags |= flags;
3404 gp_val = _bfd_get_gp_value (output_bfd);
3406 rel = relocs;
3407 relend = relocs + input_section->reloc_count;
3408 for (; rel < relend; ++rel)
3410 struct elf_link_hash_entry *h;
3411 struct elf64_ia64_dyn_sym_info *dyn_i;
3412 bfd_reloc_status_type r;
3413 reloc_howto_type *howto;
3414 unsigned long r_symndx;
3415 Elf_Internal_Sym *sym;
3416 unsigned int r_type;
3417 bfd_vma value;
3418 asection *sym_sec;
3419 bfd_byte *hit_addr;
3420 bool dynamic_symbol_p;
3421 bool undef_weak_ref;
3423 r_type = ELF64_R_TYPE (rel->r_info);
3424 if (r_type > R_IA64_MAX_RELOC_CODE)
3426 /* xgettext:c-format */
3427 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3428 input_bfd, (int) r_type);
3429 bfd_set_error (bfd_error_bad_value);
3430 ret_val = false;
3431 continue;
3434 howto = ia64_elf_lookup_howto (r_type);
3435 if (howto == NULL)
3437 ret_val = false;
3438 continue;
3440 r_symndx = ELF64_R_SYM (rel->r_info);
3441 h = NULL;
3442 sym = NULL;
3443 sym_sec = NULL;
3444 undef_weak_ref = false;
3446 if (r_symndx < symtab_hdr->sh_info)
3448 /* Reloc against local symbol. */
3449 asection *msec;
3450 sym = local_syms + r_symndx;
3451 sym_sec = local_sections[r_symndx];
3452 msec = sym_sec;
3453 value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
3454 if (!bfd_link_relocatable (info)
3455 && (sym_sec->flags & SEC_MERGE) != 0
3456 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
3457 && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3459 struct elf64_ia64_local_hash_entry *loc_h;
3461 loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, false);
3462 if (loc_h && ! loc_h->sec_merge_done)
3464 struct elf64_ia64_dyn_sym_info *dynent;
3465 unsigned int count;
3467 for (count = loc_h->count, dynent = loc_h->info;
3468 count != 0;
3469 count--, dynent++)
3471 msec = sym_sec;
3472 dynent->addend =
3473 _bfd_merged_section_offset (output_bfd, &msec,
3474 elf_section_data (msec)->
3475 sec_info,
3476 sym->st_value
3477 + dynent->addend);
3478 dynent->addend -= sym->st_value;
3479 dynent->addend += msec->output_section->vma
3480 + msec->output_offset
3481 - sym_sec->output_section->vma
3482 - sym_sec->output_offset;
3485 /* We may have introduced duplicated entries. We need
3486 to remove them properly. */
3487 count = sort_dyn_sym_info (loc_h->info, loc_h->count);
3488 if (count != loc_h->count)
3490 loc_h->count = count;
3491 loc_h->sorted_count = count;
3494 loc_h->sec_merge_done = 1;
3498 else
3500 bool unresolved_reloc;
3501 bool warned, ignored;
3502 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3504 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3505 r_symndx, symtab_hdr, sym_hashes,
3506 h, sym_sec, value,
3507 unresolved_reloc, warned, ignored);
3509 if (h->root.type == bfd_link_hash_undefweak)
3510 undef_weak_ref = true;
3511 else if (warned)
3512 continue;
3515 /* For relocs against symbols from removed linkonce sections,
3516 or sections discarded by a linker script, we just want the
3517 section contents zeroed. Avoid any special processing. */
3518 if (sym_sec != NULL && discarded_section (sym_sec))
3519 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3520 rel, 1, relend, howto, 0, contents);
3522 if (bfd_link_relocatable (info))
3523 continue;
3525 hit_addr = contents + rel->r_offset;
3526 value += rel->r_addend;
3527 dynamic_symbol_p = elf64_ia64_dynamic_symbol_p (h);
3529 switch (r_type)
3531 case R_IA64_NONE:
3532 case R_IA64_LDXMOV:
3533 continue;
3535 case R_IA64_IMM14:
3536 case R_IA64_IMM22:
3537 case R_IA64_IMM64:
3538 case R_IA64_DIR32MSB:
3539 case R_IA64_DIR32LSB:
3540 case R_IA64_DIR64MSB:
3541 case R_IA64_DIR64LSB:
3542 /* Install a dynamic relocation for this reloc. */
3543 if ((dynamic_symbol_p || bfd_link_pic (info))
3544 && r_symndx != 0
3545 && (input_section->flags & SEC_ALLOC) != 0)
3547 unsigned int dyn_r_type;
3548 bfd_vma addend;
3550 switch (r_type)
3552 case R_IA64_IMM14:
3553 case R_IA64_IMM22:
3554 case R_IA64_IMM64:
3555 /* ??? People shouldn't be doing non-pic code in
3556 shared libraries nor dynamic executables. */
3557 _bfd_error_handler
3558 /* xgettext:c-format */
3559 (_("%pB: non-pic code with imm relocation against"
3560 " dynamic symbol `%s'"),
3561 input_bfd,
3562 h ? h->root.root.string
3563 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3564 sym_sec));
3565 ret_val = false;
3566 continue;
3568 default:
3569 break;
3572 /* If we don't need dynamic symbol lookup, find a
3573 matching RELATIVE relocation. */
3574 dyn_r_type = r_type;
3575 if (dynamic_symbol_p)
3577 addend = rel->r_addend;
3578 value = 0;
3580 else
3582 addend = value;
3585 /* VMS: install a FIX64. */
3586 switch (dyn_r_type)
3588 case R_IA64_DIR32LSB:
3589 dyn_r_type = R_IA64_VMS_FIX32;
3590 break;
3591 case R_IA64_DIR64LSB:
3592 dyn_r_type = R_IA64_VMS_FIX64;
3593 break;
3594 default:
3595 BFD_ASSERT (false);
3596 break;
3598 elf64_ia64_install_fixup
3599 (output_bfd, ia64_info, h,
3600 dyn_r_type, input_section, rel->r_offset, addend);
3601 r = bfd_reloc_ok;
3602 break;
3604 /* Fall through. */
3606 case R_IA64_LTV32MSB:
3607 case R_IA64_LTV32LSB:
3608 case R_IA64_LTV64MSB:
3609 case R_IA64_LTV64LSB:
3610 r = ia64_elf_install_value (hit_addr, value, r_type);
3611 break;
3613 case R_IA64_GPREL22:
3614 case R_IA64_GPREL64I:
3615 case R_IA64_GPREL32MSB:
3616 case R_IA64_GPREL32LSB:
3617 case R_IA64_GPREL64MSB:
3618 case R_IA64_GPREL64LSB:
3619 if (dynamic_symbol_p)
3621 _bfd_error_handler
3622 /* xgettext:c-format */
3623 (_("%pB: @gprel relocation against dynamic symbol %s"),
3624 input_bfd,
3625 h ? h->root.root.string
3626 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3627 sym_sec));
3628 ret_val = false;
3629 continue;
3631 value -= gp_val;
3632 r = ia64_elf_install_value (hit_addr, value, r_type);
3633 break;
3635 case R_IA64_LTOFF22:
3636 case R_IA64_LTOFF22X:
3637 case R_IA64_LTOFF64I:
3638 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3639 value = set_got_entry (input_bfd, info, dyn_i,
3640 rel->r_addend, value, R_IA64_DIR64LSB);
3641 value -= gp_val;
3642 r = ia64_elf_install_value (hit_addr, value, r_type);
3643 break;
3645 case R_IA64_PLTOFF22:
3646 case R_IA64_PLTOFF64I:
3647 case R_IA64_PLTOFF64MSB:
3648 case R_IA64_PLTOFF64LSB:
3649 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3650 value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
3651 value -= gp_val;
3652 r = ia64_elf_install_value (hit_addr, value, r_type);
3653 break;
3655 case R_IA64_FPTR64I:
3656 case R_IA64_FPTR32MSB:
3657 case R_IA64_FPTR32LSB:
3658 case R_IA64_FPTR64MSB:
3659 case R_IA64_FPTR64LSB:
3660 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3661 if (dyn_i->want_fptr)
3663 if (!undef_weak_ref)
3664 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3666 if (!dyn_i->want_fptr || bfd_link_pie (info))
3668 /* Otherwise, we expect the dynamic linker to create
3669 the entry. */
3671 if (dyn_i->want_fptr)
3673 if (r_type == R_IA64_FPTR64I)
3675 /* We can't represent this without a dynamic symbol.
3676 Adjust the relocation to be against an output
3677 section symbol, which are always present in the
3678 dynamic symbol table. */
3679 /* ??? People shouldn't be doing non-pic code in
3680 shared libraries. Hork. */
3681 _bfd_error_handler
3682 (_("%pB: linking non-pic code in a position independent executable"),
3683 input_bfd);
3684 ret_val = false;
3685 continue;
3688 else
3690 value = 0;
3693 /* VMS: FIXFD. */
3694 elf64_ia64_install_fixup
3695 (output_bfd, ia64_info, h, R_IA64_VMS_FIXFD,
3696 input_section, rel->r_offset, 0);
3697 r = bfd_reloc_ok;
3698 break;
3701 r = ia64_elf_install_value (hit_addr, value, r_type);
3702 break;
3704 case R_IA64_LTOFF_FPTR22:
3705 case R_IA64_LTOFF_FPTR64I:
3706 case R_IA64_LTOFF_FPTR32MSB:
3707 case R_IA64_LTOFF_FPTR32LSB:
3708 case R_IA64_LTOFF_FPTR64MSB:
3709 case R_IA64_LTOFF_FPTR64LSB:
3710 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3711 if (dyn_i->want_fptr)
3713 BFD_ASSERT (h == NULL || !h->def_dynamic);
3714 if (!undef_weak_ref)
3715 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3717 else
3718 value = 0;
3720 value = set_got_entry (output_bfd, info, dyn_i,
3721 rel->r_addend, value, R_IA64_FPTR64LSB);
3722 value -= gp_val;
3723 r = ia64_elf_install_value (hit_addr, value, r_type);
3724 break;
3726 case R_IA64_PCREL32MSB:
3727 case R_IA64_PCREL32LSB:
3728 case R_IA64_PCREL64MSB:
3729 case R_IA64_PCREL64LSB:
3730 /* Install a dynamic relocation for this reloc. */
3731 if (dynamic_symbol_p && r_symndx != 0)
3733 /* VMS: doesn't exist ??? */
3734 abort ();
3736 goto finish_pcrel;
3738 case R_IA64_PCREL21B:
3739 case R_IA64_PCREL60B:
3740 /* We should have created a PLT entry for any dynamic symbol. */
3741 dyn_i = NULL;
3742 if (h)
3743 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3745 if (dyn_i && dyn_i->want_plt2)
3747 /* Should have caught this earlier. */
3748 BFD_ASSERT (rel->r_addend == 0);
3750 value = (ia64_info->root.splt->output_section->vma
3751 + ia64_info->root.splt->output_offset
3752 + dyn_i->plt2_offset);
3754 else
3756 /* Since there's no PLT entry, Validate that this is
3757 locally defined. */
3758 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
3760 /* If the symbol is undef_weak, we shouldn't be trying
3761 to call it. There's every chance that we'd wind up
3762 with an out-of-range fixup here. Don't bother setting
3763 any value at all. */
3764 if (undef_weak_ref)
3765 continue;
3767 goto finish_pcrel;
3769 case R_IA64_PCREL21BI:
3770 case R_IA64_PCREL21F:
3771 case R_IA64_PCREL21M:
3772 case R_IA64_PCREL22:
3773 case R_IA64_PCREL64I:
3774 /* The PCREL21BI reloc is specifically not intended for use with
3775 dynamic relocs. PCREL21F and PCREL21M are used for speculation
3776 fixup code, and thus probably ought not be dynamic. The
3777 PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs. */
3778 if (dynamic_symbol_p)
3780 const char *msg;
3782 if (r_type == R_IA64_PCREL21BI)
3783 /* xgettext:c-format */
3784 msg = _("%pB: @internal branch to dynamic symbol %s");
3785 else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
3786 /* xgettext:c-format */
3787 msg = _("%pB: speculation fixup to dynamic symbol %s");
3788 else
3789 /* xgettext:c-format */
3790 msg = _("%pB: @pcrel relocation against dynamic symbol %s");
3791 _bfd_error_handler (msg, input_bfd,
3792 h ? h->root.root.string
3793 : bfd_elf_sym_name (input_bfd,
3794 symtab_hdr,
3795 sym,
3796 sym_sec));
3797 ret_val = false;
3798 continue;
3800 goto finish_pcrel;
3802 finish_pcrel:
3803 /* Make pc-relative. */
3804 value -= (input_section->output_section->vma
3805 + input_section->output_offset
3806 + rel->r_offset) & ~ (bfd_vma) 0x3;
3807 r = ia64_elf_install_value (hit_addr, value, r_type);
3808 break;
3810 case R_IA64_SEGREL32MSB:
3811 case R_IA64_SEGREL32LSB:
3812 case R_IA64_SEGREL64MSB:
3813 case R_IA64_SEGREL64LSB:
3815 /* Find the segment that contains the output_section. */
3816 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
3817 (output_bfd, sym_sec->output_section);
3819 if (p == NULL)
3821 r = bfd_reloc_notsupported;
3823 else
3825 /* The VMA of the segment is the vaddr of the associated
3826 program header. */
3827 if (value > p->p_vaddr)
3828 value -= p->p_vaddr;
3829 else
3830 value = 0;
3831 r = ia64_elf_install_value (hit_addr, value, r_type);
3833 break;
3836 case R_IA64_SECREL32MSB:
3837 case R_IA64_SECREL32LSB:
3838 case R_IA64_SECREL64MSB:
3839 case R_IA64_SECREL64LSB:
3840 /* Make output-section relative to section where the symbol
3841 is defined. PR 475 */
3842 if (sym_sec)
3843 value -= sym_sec->output_section->vma;
3844 r = ia64_elf_install_value (hit_addr, value, r_type);
3845 break;
3847 case R_IA64_IPLTMSB:
3848 case R_IA64_IPLTLSB:
3849 /* Install a dynamic relocation for this reloc. */
3850 if ((dynamic_symbol_p || bfd_link_pic (info))
3851 && (input_section->flags & SEC_ALLOC) != 0)
3853 /* VMS: FIXFD ?? */
3854 abort ();
3857 if (r_type == R_IA64_IPLTMSB)
3858 r_type = R_IA64_DIR64MSB;
3859 else
3860 r_type = R_IA64_DIR64LSB;
3861 ia64_elf_install_value (hit_addr, value, r_type);
3862 r = ia64_elf_install_value (hit_addr + 8, gp_val, r_type);
3863 break;
3865 case R_IA64_TPREL14:
3866 case R_IA64_TPREL22:
3867 case R_IA64_TPREL64I:
3868 r = bfd_reloc_notsupported;
3869 break;
3871 case R_IA64_DTPREL14:
3872 case R_IA64_DTPREL22:
3873 case R_IA64_DTPREL64I:
3874 case R_IA64_DTPREL32LSB:
3875 case R_IA64_DTPREL32MSB:
3876 case R_IA64_DTPREL64LSB:
3877 case R_IA64_DTPREL64MSB:
3878 r = bfd_reloc_notsupported;
3879 break;
3881 case R_IA64_LTOFF_TPREL22:
3882 case R_IA64_LTOFF_DTPMOD22:
3883 case R_IA64_LTOFF_DTPREL22:
3884 r = bfd_reloc_notsupported;
3885 break;
3887 default:
3888 r = bfd_reloc_notsupported;
3889 break;
3892 switch (r)
3894 case bfd_reloc_ok:
3895 break;
3897 case bfd_reloc_undefined:
3898 /* This can happen for global table relative relocs if
3899 __gp is undefined. This is a panic situation so we
3900 don't try to continue. */
3901 (*info->callbacks->undefined_symbol)
3902 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
3903 return false;
3905 case bfd_reloc_notsupported:
3907 const char *name;
3909 if (h)
3910 name = h->root.root.string;
3911 else
3912 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3913 sym_sec);
3914 (*info->callbacks->warning) (info, _("unsupported reloc"),
3915 name, input_bfd,
3916 input_section, rel->r_offset);
3917 ret_val = false;
3919 break;
3921 case bfd_reloc_dangerous:
3922 case bfd_reloc_outofrange:
3923 case bfd_reloc_overflow:
3924 default:
3926 const char *name;
3928 if (h)
3929 name = h->root.root.string;
3930 else
3931 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3932 sym_sec);
3934 switch (r_type)
3936 case R_IA64_TPREL14:
3937 case R_IA64_TPREL22:
3938 case R_IA64_TPREL64I:
3939 case R_IA64_DTPREL14:
3940 case R_IA64_DTPREL22:
3941 case R_IA64_DTPREL64I:
3942 case R_IA64_DTPREL32LSB:
3943 case R_IA64_DTPREL32MSB:
3944 case R_IA64_DTPREL64LSB:
3945 case R_IA64_DTPREL64MSB:
3946 case R_IA64_LTOFF_TPREL22:
3947 case R_IA64_LTOFF_DTPMOD22:
3948 case R_IA64_LTOFF_DTPREL22:
3949 _bfd_error_handler
3950 /* xgettext:c-format */
3951 (_("%pB: missing TLS section for relocation %s against `%s'"
3952 " at %#" PRIx64 " in section `%pA'."),
3953 input_bfd, howto->name, name,
3954 (uint64_t) rel->r_offset, input_section);
3955 break;
3957 case R_IA64_PCREL21B:
3958 case R_IA64_PCREL21BI:
3959 case R_IA64_PCREL21M:
3960 case R_IA64_PCREL21F:
3961 if (is_elf_hash_table (info->hash))
3963 /* Relaxtion is always performed for ELF output.
3964 Overflow failures for those relocations mean
3965 that the section is too big to relax. */
3966 _bfd_error_handler
3967 /* xgettext:c-format */
3968 (_("%pB: Can't relax br (%s) to `%s' "
3969 "at %#" PRIx64 " in section `%pA' "
3970 "with size %#" PRIx64 " (> 0x1000000)."),
3971 input_bfd, howto->name, name, (uint64_t) rel->r_offset,
3972 input_section, (uint64_t) input_section->size);
3973 break;
3975 /* Fall through. */
3976 default:
3977 (*info->callbacks->reloc_overflow) (info,
3978 &h->root,
3979 name,
3980 howto->name,
3981 (bfd_vma) 0,
3982 input_bfd,
3983 input_section,
3984 rel->r_offset);
3985 break;
3988 ret_val = false;
3990 break;
3994 return ret_val;
3997 static bool
3998 elf64_ia64_finish_dynamic_symbol (bfd *output_bfd,
3999 struct bfd_link_info *info,
4000 struct elf_link_hash_entry *h,
4001 Elf_Internal_Sym *sym)
4003 struct elf64_ia64_link_hash_table *ia64_info;
4004 struct elf64_ia64_dyn_sym_info *dyn_i;
4006 ia64_info = elf64_ia64_hash_table (info);
4008 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
4010 /* Fill in the PLT data, if required. */
4011 if (dyn_i && dyn_i->want_plt)
4013 bfd_byte *loc;
4014 asection *plt_sec;
4015 bfd_vma plt_addr, pltoff_addr, gp_val;
4017 gp_val = _bfd_get_gp_value (output_bfd);
4019 plt_sec = ia64_info->root.splt;
4020 plt_addr = 0; /* Not used as overriden by FIXUPs. */
4021 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
4023 /* Initialize the FULL PLT entry, if needed. */
4024 if (dyn_i->want_plt2)
4026 loc = plt_sec->contents + dyn_i->plt2_offset;
4028 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
4029 ia64_elf_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
4031 /* Mark the symbol as undefined, rather than as defined in the
4032 plt section. Leave the value alone. */
4033 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
4034 first place. But perhaps elflink.c did some for us. */
4035 if (!h->def_regular)
4036 sym->st_shndx = SHN_UNDEF;
4039 /* VMS: FIXFD. */
4040 elf64_ia64_install_fixup
4041 (output_bfd, ia64_info, h, R_IA64_VMS_FIXFD, ia64_info->pltoff_sec,
4042 pltoff_addr - (ia64_info->pltoff_sec->output_section->vma
4043 + ia64_info->pltoff_sec->output_offset), 0);
4046 /* Mark some specially defined symbols as absolute. */
4047 if (h == ia64_info->root.hdynamic
4048 || h == ia64_info->root.hgot
4049 || h == ia64_info->root.hplt)
4050 sym->st_shndx = SHN_ABS;
4052 return true;
4055 static bool
4056 elf64_ia64_finish_dynamic_sections (bfd *abfd,
4057 struct bfd_link_info *info)
4059 struct elf64_ia64_link_hash_table *ia64_info;
4060 bfd *dynobj;
4062 ia64_info = elf64_ia64_hash_table (info);
4063 if (ia64_info == NULL)
4064 return false;
4066 dynobj = ia64_info->root.dynobj;
4068 if (elf_hash_table (info)->dynamic_sections_created)
4070 Elf64_External_Dyn *dyncon, *dynconend;
4071 asection *sdyn;
4072 asection *unwind_sec;
4073 bfd_vma gp_val;
4074 unsigned int gp_seg;
4075 bfd_vma gp_off;
4076 Elf_Internal_Phdr *phdr;
4077 Elf_Internal_Phdr *base_phdr;
4078 unsigned int unwind_seg = 0;
4079 unsigned int code_seg = 0;
4081 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4082 BFD_ASSERT (sdyn != NULL);
4083 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4084 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4086 gp_val = _bfd_get_gp_value (abfd);
4087 phdr = _bfd_elf_find_segment_containing_section
4088 (info->output_bfd, ia64_info->pltoff_sec->output_section);
4089 BFD_ASSERT (phdr != NULL);
4090 base_phdr = elf_tdata (info->output_bfd)->phdr;
4091 gp_seg = phdr - base_phdr;
4092 gp_off = gp_val - phdr->p_vaddr;
4094 unwind_sec = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
4095 if (unwind_sec != NULL)
4097 asection *code_sec;
4099 phdr = _bfd_elf_find_segment_containing_section (abfd, unwind_sec);
4100 BFD_ASSERT (phdr != NULL);
4101 unwind_seg = phdr - base_phdr;
4103 code_sec = bfd_get_section_by_name (abfd, "$CODE$");
4104 phdr = _bfd_elf_find_segment_containing_section (abfd, code_sec);
4105 BFD_ASSERT (phdr != NULL);
4106 code_seg = phdr - base_phdr;
4109 for (; dyncon < dynconend; dyncon++)
4111 Elf_Internal_Dyn dyn;
4113 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4115 switch (dyn.d_tag)
4117 case DT_IA_64_VMS_FIXUP_RELA_OFF:
4118 dyn.d_un.d_val +=
4119 (ia64_info->fixups_sec->output_section->vma
4120 + ia64_info->fixups_sec->output_offset)
4121 - (sdyn->output_section->vma + sdyn->output_offset);
4122 break;
4124 case DT_IA_64_VMS_PLTGOT_OFFSET:
4125 dyn.d_un.d_val = gp_off;
4126 break;
4128 case DT_IA_64_VMS_PLTGOT_SEG:
4129 dyn.d_un.d_val = gp_seg;
4130 break;
4132 case DT_IA_64_VMS_UNWINDSZ:
4133 if (unwind_sec == NULL)
4135 dyn.d_tag = DT_NULL;
4136 dyn.d_un.d_val = 0xdead;
4138 else
4139 dyn.d_un.d_val = unwind_sec->size;
4140 break;
4142 case DT_IA_64_VMS_UNWIND_CODSEG:
4143 dyn.d_un.d_val = code_seg;
4144 break;
4146 case DT_IA_64_VMS_UNWIND_INFOSEG:
4147 case DT_IA_64_VMS_UNWIND_SEG:
4148 dyn.d_un.d_val = unwind_seg;
4149 break;
4151 case DT_IA_64_VMS_UNWIND_OFFSET:
4152 break;
4154 default:
4155 /* No need to rewrite the entry. */
4156 continue;
4159 bfd_elf64_swap_dyn_out (abfd, &dyn, dyncon);
4163 /* Handle transfer addresses. */
4165 asection *tfr_sec = ia64_info->transfer_sec;
4166 struct elf64_vms_transfer *tfr;
4167 struct elf_link_hash_entry *tfr3;
4169 tfr = (struct elf64_vms_transfer *)tfr_sec->contents;
4170 bfd_putl32 (6 * 8, tfr->size);
4171 bfd_putl64 (tfr_sec->output_section->vma
4172 + tfr_sec->output_offset
4173 + 6 * 8, tfr->tfradr3);
4175 tfr3 = elf_link_hash_lookup (elf_hash_table (info), "ELF$TFRADR", false,
4176 false, false);
4178 if (tfr3
4179 && (tfr3->root.type == bfd_link_hash_defined
4180 || tfr3->root.type == bfd_link_hash_defweak))
4182 asection *tfr3_sec = tfr3->root.u.def.section;
4183 bfd_vma tfr3_val;
4185 tfr3_val = (tfr3->root.u.def.value
4186 + tfr3_sec->output_section->vma
4187 + tfr3_sec->output_offset);
4189 bfd_putl64 (tfr3_val, tfr->tfr3_func);
4190 bfd_putl64 (_bfd_get_gp_value (info->output_bfd), tfr->tfr3_gp);
4193 /* FIXME: set linker flags,
4194 handle lib$initialize. */
4197 return true;
4200 /* ELF file flag handling: */
4202 /* Function to keep IA-64 specific file flags. */
4203 static bool
4204 elf64_ia64_set_private_flags (bfd *abfd, flagword flags)
4206 BFD_ASSERT (!elf_flags_init (abfd)
4207 || elf_elfheader (abfd)->e_flags == flags);
4209 elf_elfheader (abfd)->e_flags = flags;
4210 elf_flags_init (abfd) = true;
4211 return true;
4214 /* Merge backend specific data from an object file to the output
4215 object file when linking. */
4216 static bool
4217 elf64_ia64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4219 bfd *obfd = info->output_bfd;
4220 flagword out_flags;
4221 flagword in_flags;
4222 bool ok = true;
4224 /* FIXME: What should be checked when linking shared libraries? */
4225 if ((ibfd->flags & DYNAMIC) != 0)
4226 return true;
4228 /* Don't even pretend to support mixed-format linking. */
4229 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4230 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4231 return false;
4233 in_flags = elf_elfheader (ibfd)->e_flags;
4234 out_flags = elf_elfheader (obfd)->e_flags;
4236 if (! elf_flags_init (obfd))
4238 elf_flags_init (obfd) = true;
4239 elf_elfheader (obfd)->e_flags = in_flags;
4241 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4242 && bfd_get_arch_info (obfd)->the_default)
4244 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4245 bfd_get_mach (ibfd));
4248 return true;
4251 /* Check flag compatibility. */
4252 if (in_flags == out_flags)
4253 return true;
4255 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
4256 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4257 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4259 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4261 _bfd_error_handler
4262 (_("%pB: linking trap-on-NULL-dereference with non-trapping files"),
4263 ibfd);
4265 bfd_set_error (bfd_error_bad_value);
4266 ok = false;
4268 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4270 _bfd_error_handler
4271 (_("%pB: linking big-endian files with little-endian files"),
4272 ibfd);
4274 bfd_set_error (bfd_error_bad_value);
4275 ok = false;
4277 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4279 _bfd_error_handler
4280 (_("%pB: linking 64-bit files with 32-bit files"),
4281 ibfd);
4283 bfd_set_error (bfd_error_bad_value);
4284 ok = false;
4286 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4288 _bfd_error_handler
4289 (_("%pB: linking constant-gp files with non-constant-gp files"),
4290 ibfd);
4292 bfd_set_error (bfd_error_bad_value);
4293 ok = false;
4295 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4296 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4298 _bfd_error_handler
4299 (_("%pB: linking auto-pic files with non-auto-pic files"),
4300 ibfd);
4302 bfd_set_error (bfd_error_bad_value);
4303 ok = false;
4306 return ok;
4309 static bool
4310 elf64_ia64_print_private_bfd_data (bfd *abfd, void * ptr)
4312 FILE *file = (FILE *) ptr;
4313 flagword flags = elf_elfheader (abfd)->e_flags;
4315 BFD_ASSERT (abfd != NULL && ptr != NULL);
4317 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4318 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4319 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4320 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4321 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4322 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4323 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4324 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4325 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4327 _bfd_elf_print_private_bfd_data (abfd, ptr);
4328 return true;
4331 static enum elf_reloc_type_class
4332 elf64_ia64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4333 const asection *rel_sec ATTRIBUTE_UNUSED,
4334 const Elf_Internal_Rela *rela)
4336 switch ((int) ELF64_R_TYPE (rela->r_info))
4338 case R_IA64_REL32MSB:
4339 case R_IA64_REL32LSB:
4340 case R_IA64_REL64MSB:
4341 case R_IA64_REL64LSB:
4342 return reloc_class_relative;
4343 case R_IA64_IPLTMSB:
4344 case R_IA64_IPLTLSB:
4345 return reloc_class_plt;
4346 case R_IA64_COPY:
4347 return reloc_class_copy;
4348 default:
4349 return reloc_class_normal;
4353 static const struct bfd_elf_special_section elf64_ia64_special_sections[] =
4355 { STRING_COMMA_LEN (".sbss"), -1, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4356 { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4357 { NULL, 0, 0, 0, 0 }
4360 static bool
4361 elf64_ia64_object_p (bfd *abfd)
4363 asection *sec;
4364 asection *group, *unwi, *unw;
4365 flagword flags;
4366 const char *name;
4367 char *unwi_name, *unw_name;
4368 size_t amt;
4370 if (abfd->flags & DYNAMIC)
4371 return true;
4373 /* Flags for fake group section. */
4374 flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
4375 | SEC_EXCLUDE);
4377 /* We add a fake section group for each .gnu.linkonce.t.* section,
4378 which isn't in a section group, and its unwind sections. */
4379 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4381 if (elf_sec_group (sec) == NULL
4382 && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
4383 == (SEC_LINK_ONCE | SEC_CODE))
4384 && startswith (sec->name, ".gnu.linkonce.t."))
4386 name = sec->name + 16;
4388 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
4389 unwi_name = bfd_alloc (abfd, amt);
4390 if (!unwi_name)
4391 return false;
4393 strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
4394 unwi = bfd_get_section_by_name (abfd, unwi_name);
4396 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
4397 unw_name = bfd_alloc (abfd, amt);
4398 if (!unw_name)
4399 return false;
4401 strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
4402 unw = bfd_get_section_by_name (abfd, unw_name);
4404 /* We need to create a fake group section for it and its
4405 unwind sections. */
4406 group = bfd_make_section_anyway_with_flags (abfd, name,
4407 flags);
4408 if (group == NULL)
4409 return false;
4411 /* Move the fake group section to the beginning. */
4412 bfd_section_list_remove (abfd, group);
4413 bfd_section_list_prepend (abfd, group);
4415 elf_next_in_group (group) = sec;
4417 elf_group_name (sec) = name;
4418 elf_next_in_group (sec) = sec;
4419 elf_sec_group (sec) = group;
4421 if (unwi)
4423 elf_group_name (unwi) = name;
4424 elf_next_in_group (unwi) = sec;
4425 elf_next_in_group (sec) = unwi;
4426 elf_sec_group (unwi) = group;
4429 if (unw)
4431 elf_group_name (unw) = name;
4432 if (unwi)
4434 elf_next_in_group (unw) = elf_next_in_group (unwi);
4435 elf_next_in_group (unwi) = unw;
4437 else
4439 elf_next_in_group (unw) = sec;
4440 elf_next_in_group (sec) = unw;
4442 elf_sec_group (unw) = group;
4445 /* Fake SHT_GROUP section header. */
4446 elf_section_data (group)->this_hdr.bfd_section = group;
4447 elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
4450 return true;
4453 /* Handle an IA-64 specific section when reading an object file. This
4454 is called when bfd_section_from_shdr finds a section with an unknown
4455 type. */
4457 static bool
4458 elf64_vms_section_from_shdr (bfd *abfd,
4459 Elf_Internal_Shdr *hdr,
4460 const char *name,
4461 int shindex)
4463 flagword secflags = 0;
4465 switch (hdr->sh_type)
4467 case SHT_IA_64_VMS_TRACE:
4468 case SHT_IA_64_VMS_DEBUG:
4469 case SHT_IA_64_VMS_DEBUG_STR:
4470 secflags = SEC_DEBUGGING;
4471 break;
4473 case SHT_IA_64_UNWIND:
4474 case SHT_IA_64_HP_OPT_ANOT:
4475 break;
4477 case SHT_IA_64_EXT:
4478 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
4479 return false;
4480 break;
4482 default:
4483 return false;
4486 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4487 return false;
4489 if (secflags != 0)
4491 asection *newsect = hdr->bfd_section;
4493 if (!bfd_set_section_flags (newsect,
4494 bfd_section_flags (newsect) | secflags))
4495 return false;
4498 return true;
4501 static bool
4502 elf64_vms_object_p (bfd *abfd)
4504 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4505 Elf_Internal_Phdr *i_phdr = elf_tdata (abfd)->phdr;
4506 unsigned int i;
4507 unsigned int num_text = 0;
4508 unsigned int num_data = 0;
4509 unsigned int num_rodata = 0;
4510 char name[16];
4512 if (!elf64_ia64_object_p (abfd))
4513 return false;
4515 /* Many VMS compilers do not generate sections for the corresponding
4516 segment. This is boring as binutils tools won't be able to disassemble
4517 the code. So we simply create all the missing sections. */
4518 for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
4520 /* Is there a section for this segment? */
4521 bfd_vma base_vma = i_phdr->p_vaddr;
4522 bfd_vma limit_vma = base_vma + i_phdr->p_filesz;
4524 if (i_phdr->p_type != PT_LOAD)
4525 continue;
4527 /* We need to cover from base_vms to limit_vma. */
4528 again:
4529 while (base_vma < limit_vma)
4531 bfd_vma next_vma = limit_vma;
4532 asection *nsec;
4533 asection *sec;
4534 flagword flags;
4535 char *nname = NULL;
4537 /* Find a section covering [base_vma;limit_vma) */
4538 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4540 /* Skip uninteresting sections (either not in memory or
4541 below base_vma. */
4542 if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == 0
4543 || sec->vma + sec->size <= base_vma)
4544 continue;
4545 if (sec->vma <= base_vma)
4547 /* This section covers (maybe partially) the beginning
4548 of the range. */
4549 base_vma = sec->vma + sec->size;
4550 goto again;
4552 if (sec->vma < next_vma)
4554 /* This section partially covers the end of the range.
4555 Used to compute the size of the hole. */
4556 next_vma = sec->vma;
4560 /* No section covering [base_vma; next_vma). Create a fake one. */
4561 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
4562 if (i_phdr->p_flags & PF_X)
4564 flags |= SEC_CODE;
4565 if (num_text++ == 0)
4566 nname = ".text";
4567 else
4568 sprintf (name, ".text$%u", num_text);
4570 else if ((i_phdr->p_flags & (PF_R | PF_W)) == PF_R)
4572 flags |= SEC_READONLY;
4573 sprintf (name, ".rodata$%u", num_rodata++);
4575 else
4577 flags |= SEC_DATA;
4578 sprintf (name, ".data$%u", num_data++);
4581 /* Allocate name. */
4582 if (nname == NULL)
4584 size_t name_len = strlen (name) + 1;
4585 nname = bfd_alloc (abfd, name_len);
4586 if (nname == NULL)
4587 return false;
4588 memcpy (nname, name, name_len);
4591 /* Create and fill new section. */
4592 nsec = bfd_make_section_anyway_with_flags (abfd, nname, flags);
4593 if (nsec == NULL)
4594 return false;
4595 nsec->vma = base_vma;
4596 nsec->size = next_vma - base_vma;
4597 nsec->filepos = i_phdr->p_offset + (base_vma - i_phdr->p_vaddr);
4599 base_vma = next_vma;
4602 return true;
4605 static bool
4606 elf64_vms_init_file_header (bfd *abfd, struct bfd_link_info *info)
4608 Elf_Internal_Ehdr *i_ehdrp;
4610 if (!_bfd_elf_init_file_header (abfd, info))
4611 return false;
4613 i_ehdrp = elf_elfheader (abfd);
4614 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_OPENVMS;
4615 i_ehdrp->e_ident[EI_ABIVERSION] = 2;
4616 return true;
4619 static bool
4620 elf64_vms_section_processing (bfd *abfd ATTRIBUTE_UNUSED,
4621 Elf_Internal_Shdr *hdr)
4623 if (hdr->bfd_section != NULL)
4625 const char *name = bfd_section_name (hdr->bfd_section);
4627 if (strcmp (name, ".text") == 0)
4628 hdr->sh_flags |= SHF_IA_64_VMS_SHARED;
4629 else if ((strcmp (name, ".debug") == 0)
4630 || (strcmp (name, ".debug_abbrev") == 0)
4631 || (strcmp (name, ".debug_aranges") == 0)
4632 || (strcmp (name, ".debug_frame") == 0)
4633 || (strcmp (name, ".debug_info") == 0)
4634 || (strcmp (name, ".debug_loc") == 0)
4635 || (strcmp (name, ".debug_macinfo") == 0)
4636 || (strcmp (name, ".debug_pubnames") == 0)
4637 || (strcmp (name, ".debug_pubtypes") == 0))
4638 hdr->sh_type = SHT_IA_64_VMS_DEBUG;
4639 else if ((strcmp (name, ".debug_line") == 0)
4640 || (strcmp (name, ".debug_ranges") == 0)
4641 || (strcmp (name, ".trace_info") == 0)
4642 || (strcmp (name, ".trace_abbrev") == 0)
4643 || (strcmp (name, ".trace_aranges") == 0))
4644 hdr->sh_type = SHT_IA_64_VMS_TRACE;
4645 else if (strcmp (name, ".debug_str") == 0)
4646 hdr->sh_type = SHT_IA_64_VMS_DEBUG_STR;
4649 return true;
4652 /* The final processing done just before writing out a VMS IA-64 ELF
4653 object file. */
4655 static bool
4656 elf64_vms_final_write_processing (bfd *abfd)
4658 Elf_Internal_Shdr *hdr;
4659 asection *s;
4660 int unwind_info_sect_idx = 0;
4662 for (s = abfd->sections; s; s = s->next)
4664 hdr = &elf_section_data (s)->this_hdr;
4666 if (strcmp (bfd_section_name (hdr->bfd_section),
4667 ".IA_64.unwind_info") == 0)
4668 unwind_info_sect_idx = elf_section_data (s)->this_idx;
4670 switch (hdr->sh_type)
4672 case SHT_IA_64_UNWIND:
4673 /* VMS requires sh_info to point to the unwind info section. */
4674 hdr->sh_info = unwind_info_sect_idx;
4675 break;
4679 if (! elf_flags_init (abfd))
4681 unsigned long flags = 0;
4683 if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
4684 flags |= EF_IA_64_BE;
4685 if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
4686 flags |= EF_IA_64_ABI64;
4688 elf_elfheader (abfd)->e_flags = flags;
4689 elf_flags_init (abfd) = true;
4691 return _bfd_elf_final_write_processing (abfd);
4694 static bool
4695 elf64_vms_write_shdrs_and_ehdr (bfd *abfd)
4697 unsigned char needed_count[8];
4699 if (!bfd_elf64_write_shdrs_and_ehdr (abfd))
4700 return false;
4702 bfd_putl64 (elf_ia64_vms_tdata (abfd)->needed_count, needed_count);
4704 if (bfd_seek (abfd, sizeof (Elf64_External_Ehdr), SEEK_SET) != 0
4705 || bfd_write (needed_count, 8, abfd) != 8)
4706 return false;
4708 return true;
4711 static bool
4712 elf64_vms_close_and_cleanup (bfd *abfd)
4714 bool ret = true;
4715 if (bfd_get_format (abfd) == bfd_object
4716 && bfd_write_p (abfd))
4718 long isize;
4720 /* Pad to 8 byte boundary for IPF/VMS. */
4721 isize = bfd_get_size (abfd);
4722 if ((isize & 7) != 0)
4724 unsigned int ishort = 8 - (isize & 7);
4725 uint64_t pad = 0;
4727 if (bfd_seek (abfd, isize, SEEK_SET) != 0
4728 || bfd_write (&pad, ishort, abfd) != ishort)
4729 ret = false;
4733 return _bfd_generic_close_and_cleanup (abfd) && ret;
4736 /* Add symbols from an ELF object file to the linker hash table. */
4738 static bool
4739 elf64_vms_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4741 Elf_Internal_Shdr *hdr;
4742 bfd_size_type symcount;
4743 bfd_size_type extsymcount;
4744 bfd_size_type extsymoff;
4745 struct elf_link_hash_entry **sym_hash;
4746 bool dynamic;
4747 Elf_Internal_Sym *isymbuf = NULL;
4748 Elf_Internal_Sym *isym;
4749 Elf_Internal_Sym *isymend;
4750 const struct elf_backend_data *bed;
4751 struct elf_link_hash_table *htab;
4752 bfd_size_type amt;
4754 htab = elf_hash_table (info);
4755 bed = get_elf_backend_data (abfd);
4757 if ((abfd->flags & DYNAMIC) == 0)
4758 dynamic = false;
4759 else
4761 dynamic = true;
4763 /* You can't use -r against a dynamic object. Also, there's no
4764 hope of using a dynamic object which does not exactly match
4765 the format of the output file. */
4766 if (bfd_link_relocatable (info)
4767 || !is_elf_hash_table (&htab->root)
4768 || info->output_bfd->xvec != abfd->xvec)
4770 if (bfd_link_relocatable (info))
4771 bfd_set_error (bfd_error_invalid_operation);
4772 else
4773 bfd_set_error (bfd_error_wrong_format);
4774 goto error_return;
4778 if (! dynamic)
4780 /* If we are creating a shared library, create all the dynamic
4781 sections immediately. We need to attach them to something,
4782 so we attach them to this BFD, provided it is the right
4783 format. FIXME: If there are no input BFD's of the same
4784 format as the output, we can't make a shared library. */
4785 if (bfd_link_pic (info)
4786 && is_elf_hash_table (&htab->root)
4787 && info->output_bfd->xvec == abfd->xvec
4788 && !htab->dynamic_sections_created)
4790 if (! elf64_ia64_create_dynamic_sections (abfd, info))
4791 goto error_return;
4794 else if (!is_elf_hash_table (&htab->root))
4795 goto error_return;
4796 else
4798 asection *s;
4799 bfd_byte *dynbuf;
4800 bfd_byte *extdyn;
4802 /* ld --just-symbols and dynamic objects don't mix very well.
4803 ld shouldn't allow it. */
4804 if ((s = abfd->sections) != NULL
4805 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4806 abort ();
4808 /* Be sure there are dynamic sections. */
4809 if (! elf64_ia64_create_dynamic_sections (htab->dynobj, info))
4810 goto error_return;
4812 s = bfd_get_section_by_name (abfd, ".dynamic");
4813 if (s == NULL)
4815 /* VMS libraries do not have dynamic sections. Create one from
4816 the segment. */
4817 Elf_Internal_Phdr *phdr;
4818 unsigned int i, phnum;
4820 phdr = elf_tdata (abfd)->phdr;
4821 if (phdr == NULL)
4822 goto error_return;
4823 phnum = elf_elfheader (abfd)->e_phnum;
4824 for (i = 0; i < phnum; phdr++)
4825 if (phdr->p_type == PT_DYNAMIC)
4827 s = bfd_make_section (abfd, ".dynamic");
4828 if (s == NULL)
4829 goto error_return;
4830 s->vma = phdr->p_vaddr;
4831 s->lma = phdr->p_paddr;
4832 s->size = phdr->p_filesz;
4833 s->filepos = phdr->p_offset;
4834 s->flags |= SEC_HAS_CONTENTS;
4835 s->alignment_power = bfd_log2 (phdr->p_align);
4836 break;
4838 if (s == NULL)
4839 goto error_return;
4842 /* Extract IDENT. */
4843 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4845 error_free_dyn:
4846 free (dynbuf);
4847 goto error_return;
4850 for (extdyn = dynbuf;
4851 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4852 extdyn += bed->s->sizeof_dyn)
4854 Elf_Internal_Dyn dyn;
4856 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4857 if (dyn.d_tag == DT_IA_64_VMS_IDENT)
4859 uint64_t tagv = dyn.d_un.d_val;
4860 elf_ia64_vms_ident (abfd) = tagv;
4861 break;
4864 if (extdyn >= dynbuf + s->size)
4866 /* Ident not found. */
4867 goto error_free_dyn;
4869 free (dynbuf);
4871 /* We do not want to include any of the sections in a dynamic
4872 object in the output file. We hack by simply clobbering the
4873 list of sections in the BFD. This could be handled more
4874 cleanly by, say, a new section flag; the existing
4875 SEC_NEVER_LOAD flag is not the one we want, because that one
4876 still implies that the section takes up space in the output
4877 file. */
4878 bfd_section_list_clear (abfd);
4880 /* FIXME: should we detect if this library is already included ?
4881 This should be harmless and shouldn't happen in practice. */
4884 hdr = &elf_tdata (abfd)->symtab_hdr;
4885 symcount = hdr->sh_size / bed->s->sizeof_sym;
4887 /* The sh_info field of the symtab header tells us where the
4888 external symbols start. We don't care about the local symbols at
4889 this point. */
4890 extsymcount = symcount - hdr->sh_info;
4891 extsymoff = hdr->sh_info;
4893 sym_hash = NULL;
4894 if (extsymcount != 0)
4896 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4897 NULL, NULL, NULL);
4898 if (isymbuf == NULL)
4899 goto error_return;
4901 /* We store a pointer to the hash table entry for each external
4902 symbol. */
4903 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4904 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4905 if (sym_hash == NULL)
4906 goto error_free_sym;
4907 elf_sym_hashes (abfd) = sym_hash;
4910 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4911 isym < isymend;
4912 isym++, sym_hash++)
4914 int bind;
4915 bfd_vma value;
4916 asection *sec, *new_sec;
4917 flagword flags;
4918 const char *name;
4919 struct elf_link_hash_entry *h;
4920 bool definition;
4921 bool size_change_ok;
4922 bool type_change_ok;
4923 bool common;
4924 unsigned int old_alignment;
4925 bfd *old_bfd;
4927 flags = BSF_NO_FLAGS;
4928 sec = NULL;
4929 value = isym->st_value;
4930 *sym_hash = NULL;
4931 common = bed->common_definition (isym);
4933 bind = ELF_ST_BIND (isym->st_info);
4934 switch (bind)
4936 case STB_LOCAL:
4937 /* This should be impossible, since ELF requires that all
4938 global symbols follow all local symbols, and that sh_info
4939 point to the first global symbol. Unfortunately, Irix 5
4940 screws this up. */
4941 continue;
4943 case STB_GLOBAL:
4944 if (isym->st_shndx != SHN_UNDEF && !common)
4945 flags = BSF_GLOBAL;
4946 break;
4948 case STB_WEAK:
4949 flags = BSF_WEAK;
4950 break;
4952 case STB_GNU_UNIQUE:
4953 flags = BSF_GNU_UNIQUE;
4954 break;
4956 default:
4957 /* Leave it up to the processor backend. */
4958 break;
4961 if (isym->st_shndx == SHN_UNDEF)
4962 sec = bfd_und_section_ptr;
4963 else if (isym->st_shndx == SHN_ABS)
4964 sec = bfd_abs_section_ptr;
4965 else if (isym->st_shndx == SHN_COMMON)
4967 sec = bfd_com_section_ptr;
4968 /* What ELF calls the size we call the value. What ELF
4969 calls the value we call the alignment. */
4970 value = isym->st_size;
4972 else
4974 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4975 if (sec == NULL)
4976 sec = bfd_abs_section_ptr;
4977 else if (sec->kept_section)
4979 /* Symbols from discarded section are undefined. We keep
4980 its visibility. */
4981 sec = bfd_und_section_ptr;
4982 isym->st_shndx = SHN_UNDEF;
4984 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4985 value -= sec->vma;
4988 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4989 isym->st_name);
4990 if (name == NULL)
4991 goto error_free_vers;
4993 if (bed->elf_add_symbol_hook)
4995 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4996 &sec, &value))
4997 goto error_free_vers;
4999 /* The hook function sets the name to NULL if this symbol
5000 should be skipped for some reason. */
5001 if (name == NULL)
5002 continue;
5005 /* Sanity check that all possibilities were handled. */
5006 if (sec == NULL)
5008 bfd_set_error (bfd_error_bad_value);
5009 goto error_free_vers;
5012 if (bfd_is_und_section (sec)
5013 || bfd_is_com_section (sec))
5014 definition = false;
5015 else
5016 definition = true;
5018 size_change_ok = false;
5019 type_change_ok = bed->type_change_ok;
5020 old_alignment = 0;
5021 old_bfd = NULL;
5022 new_sec = sec;
5024 if (! bfd_is_und_section (sec))
5025 h = elf_link_hash_lookup (htab, name, true, false, false);
5026 else
5027 h = ((struct elf_link_hash_entry *) bfd_wrapped_link_hash_lookup
5028 (abfd, info, name, true, false, false));
5029 if (h == NULL)
5030 goto error_free_sym;
5032 *sym_hash = h;
5034 if (is_elf_hash_table (&htab->root))
5036 while (h->root.type == bfd_link_hash_indirect
5037 || h->root.type == bfd_link_hash_warning)
5038 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5040 /* Remember the old alignment if this is a common symbol, so
5041 that we don't reduce the alignment later on. We can't
5042 check later, because _bfd_generic_link_add_one_symbol
5043 will set a default for the alignment which we want to
5044 override. We also remember the old bfd where the existing
5045 definition comes from. */
5046 switch (h->root.type)
5048 default:
5049 break;
5051 case bfd_link_hash_defined:
5052 if (abfd->selective_search)
5053 continue;
5054 /* Fall-through. */
5055 case bfd_link_hash_defweak:
5056 old_bfd = h->root.u.def.section->owner;
5057 break;
5059 case bfd_link_hash_common:
5060 old_bfd = h->root.u.c.p->section->owner;
5061 old_alignment = h->root.u.c.p->alignment_power;
5062 break;
5066 if (! (_bfd_generic_link_add_one_symbol
5067 (info, abfd, name, flags, sec, value, NULL, false, bed->collect,
5068 (struct bfd_link_hash_entry **) sym_hash)))
5069 goto error_free_vers;
5071 h = *sym_hash;
5072 while (h->root.type == bfd_link_hash_indirect
5073 || h->root.type == bfd_link_hash_warning)
5074 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5076 *sym_hash = h;
5077 if (definition)
5078 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5080 /* Set the alignment of a common symbol. */
5081 if ((common || bfd_is_com_section (sec))
5082 && h->root.type == bfd_link_hash_common)
5084 unsigned int align;
5086 if (common)
5087 align = bfd_log2 (isym->st_value);
5088 else
5090 /* The new symbol is a common symbol in a shared object.
5091 We need to get the alignment from the section. */
5092 align = new_sec->alignment_power;
5094 if (align > old_alignment
5095 /* Permit an alignment power of zero if an alignment of one
5096 is specified and no other alignments have been specified. */
5097 || (isym->st_value == 1 && old_alignment == 0))
5098 h->root.u.c.p->alignment_power = align;
5099 else
5100 h->root.u.c.p->alignment_power = old_alignment;
5103 if (is_elf_hash_table (&htab->root))
5105 /* Check the alignment when a common symbol is involved. This
5106 can change when a common symbol is overridden by a normal
5107 definition or a common symbol is ignored due to the old
5108 normal definition. We need to make sure the maximum
5109 alignment is maintained. */
5110 if ((old_alignment || common)
5111 && h->root.type != bfd_link_hash_common)
5113 unsigned int common_align;
5114 unsigned int normal_align;
5115 unsigned int symbol_align;
5116 bfd *normal_bfd;
5117 bfd *common_bfd;
5119 symbol_align = ffs (h->root.u.def.value) - 1;
5120 if (h->root.u.def.section->owner != NULL
5121 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
5123 normal_align = h->root.u.def.section->alignment_power;
5124 if (normal_align > symbol_align)
5125 normal_align = symbol_align;
5127 else
5128 normal_align = symbol_align;
5130 if (old_alignment)
5132 common_align = old_alignment;
5133 common_bfd = old_bfd;
5134 normal_bfd = abfd;
5136 else
5138 common_align = bfd_log2 (isym->st_value);
5139 common_bfd = abfd;
5140 normal_bfd = old_bfd;
5143 if (normal_align < common_align)
5145 /* PR binutils/2735 */
5146 if (normal_bfd == NULL)
5147 _bfd_error_handler
5148 /* xgettext:c-format */
5149 (_("warning: alignment %u of common symbol `%s' in %pB"
5150 " is greater than the alignment (%u) of its section %pA"),
5151 1 << common_align, name, common_bfd,
5152 1 << normal_align, h->root.u.def.section);
5153 else
5154 _bfd_error_handler
5155 /* xgettext:c-format */
5156 (_("warning: alignment %u of symbol `%s' in %pB"
5157 " is smaller than %u in %pB"),
5158 1 << normal_align, name, normal_bfd,
5159 1 << common_align, common_bfd);
5163 /* Remember the symbol size if it isn't undefined. */
5164 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
5165 && (definition || h->size == 0))
5167 if (h->size != 0
5168 && h->size != isym->st_size
5169 && ! size_change_ok)
5170 _bfd_error_handler
5171 /* xgettext:c-format */
5172 (_("warning: size of symbol `%s' changed"
5173 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5174 name, (uint64_t) h->size, old_bfd,
5175 (uint64_t) isym->st_size, abfd);
5177 h->size = isym->st_size;
5180 /* If this is a common symbol, then we always want H->SIZE
5181 to be the size of the common symbol. The code just above
5182 won't fix the size if a common symbol becomes larger. We
5183 don't warn about a size change here, because that is
5184 covered by --warn-common. Allow changed between different
5185 function types. */
5186 if (h->root.type == bfd_link_hash_common)
5187 h->size = h->root.u.c.size;
5189 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5190 && (definition || h->type == STT_NOTYPE))
5192 unsigned int type = ELF_ST_TYPE (isym->st_info);
5194 if (h->type != type)
5196 if (h->type != STT_NOTYPE && ! type_change_ok)
5197 _bfd_error_handler
5198 /* xgettext:c-format */
5199 (_("warning: type of symbol `%s' changed"
5200 " from %d to %d in %pB"),
5201 name, h->type, type, abfd);
5203 h->type = type;
5207 /* Set a flag in the hash table entry indicating the type of
5208 reference or definition we just found. Keep a count of
5209 the number of dynamic symbols we find. A dynamic symbol
5210 is one which is referenced or defined by both a regular
5211 object and a shared object. */
5212 if (! dynamic)
5214 if (! definition)
5216 h->ref_regular = 1;
5217 if (bind != STB_WEAK)
5218 h->ref_regular_nonweak = 1;
5220 else
5222 BFD_ASSERT (!h->def_dynamic);
5223 h->def_regular = 1;
5226 else
5228 BFD_ASSERT (definition);
5229 h->def_dynamic = 1;
5230 h->dynindx = -2;
5231 ((struct elf64_ia64_link_hash_entry *)h)->shl = abfd;
5236 free (isymbuf);
5237 isymbuf = NULL;
5239 /* If this object is the same format as the output object, and it is
5240 not a shared library, then let the backend look through the
5241 relocs.
5243 This is required to build global offset table entries and to
5244 arrange for dynamic relocs. It is not required for the
5245 particular common case of linking non PIC code, even when linking
5246 against shared libraries, but unfortunately there is no way of
5247 knowing whether an object file has been compiled PIC or not.
5248 Looking through the relocs is not particularly time consuming.
5249 The problem is that we must either (1) keep the relocs in memory,
5250 which causes the linker to require additional runtime memory or
5251 (2) read the relocs twice from the input file, which wastes time.
5252 This would be a good case for using mmap.
5254 I have no idea how to handle linking PIC code into a file of a
5255 different format. It probably can't be done. */
5256 if (! dynamic
5257 && is_elf_hash_table (&htab->root)
5258 && bed->check_relocs != NULL
5259 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
5261 asection *o;
5263 for (o = abfd->sections; o != NULL; o = o->next)
5265 Elf_Internal_Rela *internal_relocs;
5266 bool ok;
5268 if ((o->flags & SEC_RELOC) == 0
5269 || o->reloc_count == 0
5270 || ((info->strip == strip_all || info->strip == strip_debugger)
5271 && (o->flags & SEC_DEBUGGING) != 0)
5272 || bfd_is_abs_section (o->output_section))
5273 continue;
5275 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
5276 info->keep_memory);
5277 if (internal_relocs == NULL)
5278 goto error_return;
5280 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
5282 if (elf_section_data (o)->relocs != internal_relocs)
5283 free (internal_relocs);
5285 if (! ok)
5286 goto error_return;
5290 return true;
5292 error_free_vers:
5293 error_free_sym:
5294 free (isymbuf);
5295 error_return:
5296 return false;
5299 static bool
5300 elf64_vms_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5302 int pass;
5303 struct bfd_link_hash_entry **pundef;
5304 struct bfd_link_hash_entry **next_pundef;
5306 /* We only accept VMS libraries. */
5307 if (info->output_bfd->xvec != abfd->xvec)
5309 bfd_set_error (bfd_error_wrong_format);
5310 return false;
5313 /* The archive_pass field in the archive itself is used to
5314 initialize PASS, since we may search the same archive multiple
5315 times. */
5316 pass = ++abfd->archive_pass;
5318 /* Look through the list of undefined symbols. */
5319 for (pundef = &info->hash->undefs; *pundef != NULL; pundef = next_pundef)
5321 struct bfd_link_hash_entry *h;
5322 symindex symidx;
5323 bfd *element;
5324 bfd *orig_element;
5326 h = *pundef;
5327 next_pundef = &(*pundef)->u.undef.next;
5329 /* When a symbol is defined, it is not necessarily removed from
5330 the list. */
5331 if (h->type != bfd_link_hash_undefined
5332 && h->type != bfd_link_hash_common)
5334 /* Remove this entry from the list, for general cleanliness
5335 and because we are going to look through the list again
5336 if we search any more libraries. We can't remove the
5337 entry if it is the tail, because that would lose any
5338 entries we add to the list later on. */
5339 if (*pundef != info->hash->undefs_tail)
5341 *pundef = *next_pundef;
5342 next_pundef = pundef;
5344 continue;
5347 /* Look for this symbol in the archive hash table. */
5348 symidx = _bfd_vms_lib_find_symbol (abfd, h->root.string);
5349 if (symidx == BFD_NO_MORE_SYMBOLS)
5351 /* Nothing in this slot. */
5352 continue;
5355 element = bfd_get_elt_at_index (abfd, symidx);
5356 if (element == NULL)
5357 return false;
5359 if (element->archive_pass == -1 || element->archive_pass == pass)
5361 /* Next symbol if this archive is wrong or already handled. */
5362 continue;
5365 orig_element = element;
5366 if (bfd_is_thin_archive (abfd))
5368 element = _bfd_vms_lib_get_imagelib_file (element);
5369 if (element == NULL || !bfd_check_format (element, bfd_object))
5371 orig_element->archive_pass = -1;
5372 return false;
5375 else if (! bfd_check_format (element, bfd_object))
5377 element->archive_pass = -1;
5378 return false;
5381 /* Unlike the generic linker, we know that this element provides
5382 a definition for an undefined symbol and we know that we want
5383 to include it. We don't need to check anything. */
5384 if (! (*info->callbacks->add_archive_element) (info, element,
5385 h->root.string, &element))
5386 continue;
5387 if (! elf64_vms_link_add_object_symbols (element, info))
5388 return false;
5390 orig_element->archive_pass = pass;
5393 return true;
5396 static bool
5397 elf64_vms_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5399 switch (bfd_get_format (abfd))
5401 case bfd_object:
5402 return elf64_vms_link_add_object_symbols (abfd, info);
5403 break;
5404 case bfd_archive:
5405 return elf64_vms_link_add_archive_symbols (abfd, info);
5406 break;
5407 default:
5408 bfd_set_error (bfd_error_wrong_format);
5409 return false;
5413 static bool
5414 elf64_ia64_vms_mkobject (bfd *abfd)
5416 return bfd_elf_allocate_object
5417 (abfd, sizeof (struct elf64_ia64_vms_obj_tdata), IA64_ELF_DATA);
5421 /* Size-dependent data and functions. */
5422 static const struct elf_size_info elf64_ia64_vms_size_info = {
5423 sizeof (Elf64_External_VMS_Ehdr),
5424 sizeof (Elf64_External_Phdr),
5425 sizeof (Elf64_External_Shdr),
5426 sizeof (Elf64_External_Rel),
5427 sizeof (Elf64_External_Rela),
5428 sizeof (Elf64_External_Sym),
5429 sizeof (Elf64_External_Dyn),
5430 sizeof (Elf_External_Note),
5433 64, 3, /* ARCH_SIZE, LOG_FILE_ALIGN */
5434 ELFCLASS64, EV_CURRENT,
5435 bfd_elf64_write_out_phdrs,
5436 elf64_vms_write_shdrs_and_ehdr,
5437 bfd_elf64_checksum_contents,
5438 bfd_elf64_write_relocs,
5439 bfd_elf64_swap_symbol_in,
5440 bfd_elf64_swap_symbol_out,
5441 bfd_elf64_slurp_reloc_table,
5442 bfd_elf64_slurp_symbol_table,
5443 bfd_elf64_swap_dyn_in,
5444 bfd_elf64_swap_dyn_out,
5445 bfd_elf64_swap_reloc_in,
5446 bfd_elf64_swap_reloc_out,
5447 bfd_elf64_swap_reloca_in,
5448 bfd_elf64_swap_reloca_out
5451 #define ELF_ARCH bfd_arch_ia64
5452 #define ELF_MACHINE_CODE EM_IA_64
5453 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
5454 #define ELF_COMMONPAGESIZE 0x200 /* 16KB */
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
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
5478 #define elf_backend_object_p \
5479 elf64_ia64_object_p
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
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
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
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
5533 /* VMS-specific vectors. */
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
5542 /* These are VMS specific functions. */
5544 #undef elf_backend_object_p
5545 #define elf_backend_object_p elf64_vms_object_p
5547 #undef elf_backend_section_from_shdr
5548 #define elf_backend_section_from_shdr elf64_vms_section_from_shdr
5550 #undef elf_backend_init_file_header
5551 #define elf_backend_init_file_header elf64_vms_init_file_header
5553 #undef elf_backend_section_processing
5554 #define elf_backend_section_processing elf64_vms_section_processing
5556 #undef elf_backend_final_write_processing
5557 #define elf_backend_final_write_processing elf64_vms_final_write_processing
5559 #undef bfd_elf64_close_and_cleanup
5560 #define bfd_elf64_close_and_cleanup elf64_vms_close_and_cleanup
5562 #undef elf_backend_section_from_bfd_section
5564 #undef elf_backend_symbol_processing
5566 #undef elf_backend_want_p_paddr_set_to_zero
5568 #undef ELF_OSABI
5569 #define ELF_OSABI ELFOSABI_OPENVMS
5571 #undef ELF_MAXPAGESIZE
5572 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
5574 #undef elf64_bed
5575 #define elf64_bed elf64_ia64_vms_bed
5577 #define elf_backend_size_info elf64_ia64_vms_size_info
5579 /* Use VMS-style archives (in particular, don't use the standard coff
5580 archive format). */
5581 #define bfd_elf64_archive_functions
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
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
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
5617 #undef elf_backend_want_got_sym
5618 #define elf_backend_want_got_sym 0
5620 #undef bfd_elf64_mkobject
5621 #define bfd_elf64_mkobject elf64_ia64_vms_mkobject
5623 /* Redefine to align segments on block size. */
5624 #undef ELF_MAXPAGESIZE
5625 #define ELF_MAXPAGESIZE 0x200 /* 512B */
5627 #undef elf_backend_want_got_plt
5628 #define elf_backend_want_got_plt 0
5630 #include "elf64-target.h"