Add support for 8-bit and 24-bit shifts in the z80 assembler.
[binutils-gdb.git] / bfd / elf64-ia64-vms.c
blobc72015b450c570187e08664f9f78ab727a3292d5
1 /* IA-64 support for OpenVMS
2 Copyright (C) 1998-2021 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 bfd_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->flags & SEC_RELOC) == 0
374 || sec->reloc_count == 0
375 || (link_info->relax_pass == 0 && sec->skip_relax_pass_0)
376 || (link_info->relax_pass == 1 && sec->skip_relax_pass_1))
377 return true;
379 ia64_info = elf64_ia64_hash_table (link_info);
380 if (ia64_info == NULL)
381 return false;
383 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
385 /* Load the relocations for this section. */
386 internal_relocs = (_bfd_elf_link_read_relocs
387 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
388 link_info->keep_memory));
389 if (internal_relocs == NULL)
390 return false;
392 irelend = internal_relocs + sec->reloc_count;
394 /* Get the section contents. */
395 if (elf_section_data (sec)->this_hdr.contents != NULL)
396 contents = elf_section_data (sec)->this_hdr.contents;
397 else
399 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
400 goto error_return;
403 for (irel = internal_relocs; irel < irelend; irel++)
405 unsigned long r_type = ELF64_R_TYPE (irel->r_info);
406 bfd_vma symaddr, reladdr, trampoff, toff, roff;
407 asection *tsec;
408 struct one_fixup *f;
409 bfd_size_type amt;
410 bool is_branch;
411 struct elf64_ia64_dyn_sym_info *dyn_i;
413 switch (r_type)
415 case R_IA64_PCREL21B:
416 case R_IA64_PCREL21BI:
417 case R_IA64_PCREL21M:
418 case R_IA64_PCREL21F:
419 /* In pass 1, all br relaxations are done. We can skip it. */
420 if (link_info->relax_pass == 1)
421 continue;
422 skip_relax_pass_0 = false;
423 is_branch = true;
424 break;
426 case R_IA64_PCREL60B:
427 /* We can't optimize brl to br in pass 0 since br relaxations
428 will increase the code size. Defer it to pass 1. */
429 if (link_info->relax_pass == 0)
431 skip_relax_pass_1 = false;
432 continue;
434 is_branch = true;
435 break;
437 case R_IA64_GPREL22:
438 /* Update max_short_sec/min_short_sec. */
440 case R_IA64_LTOFF22X:
441 case R_IA64_LDXMOV:
442 /* We can't relax ldx/mov in pass 0 since br relaxations will
443 increase the code size. Defer it to pass 1. */
444 if (link_info->relax_pass == 0)
446 skip_relax_pass_1 = false;
447 continue;
449 is_branch = false;
450 break;
452 default:
453 continue;
456 /* Get the value of the symbol referred to by the reloc. */
457 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
459 /* A local symbol. */
460 Elf_Internal_Sym *isym;
462 /* Read this BFD's local symbols. */
463 if (isymbuf == NULL)
465 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
466 if (isymbuf == NULL)
467 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
468 symtab_hdr->sh_info, 0,
469 NULL, NULL, NULL);
470 if (isymbuf == 0)
471 goto error_return;
474 isym = isymbuf + ELF64_R_SYM (irel->r_info);
475 if (isym->st_shndx == SHN_UNDEF)
476 continue; /* We can't do anything with undefined symbols. */
477 else if (isym->st_shndx == SHN_ABS)
478 tsec = bfd_abs_section_ptr;
479 else if (isym->st_shndx == SHN_COMMON)
480 tsec = bfd_com_section_ptr;
481 else if (isym->st_shndx == SHN_IA_64_ANSI_COMMON)
482 tsec = bfd_com_section_ptr;
483 else
484 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
486 toff = isym->st_value;
487 dyn_i = get_dyn_sym_info (ia64_info, NULL, abfd, irel, false);
489 else
491 unsigned long indx;
492 struct elf_link_hash_entry *h;
494 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
495 h = elf_sym_hashes (abfd)[indx];
496 BFD_ASSERT (h != NULL);
498 while (h->root.type == bfd_link_hash_indirect
499 || h->root.type == bfd_link_hash_warning)
500 h = (struct elf_link_hash_entry *) h->root.u.i.link;
502 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, false);
504 /* For branches to dynamic symbols, we're interested instead
505 in a branch to the PLT entry. */
506 if (is_branch && dyn_i && dyn_i->want_plt2)
508 /* Internal branches shouldn't be sent to the PLT.
509 Leave this for now and we'll give an error later. */
510 if (r_type != R_IA64_PCREL21B)
511 continue;
513 tsec = ia64_info->root.splt;
514 toff = dyn_i->plt2_offset;
515 BFD_ASSERT (irel->r_addend == 0);
518 /* Can't do anything else with dynamic symbols. */
519 else if (elf64_ia64_dynamic_symbol_p (h))
520 continue;
522 else
524 /* We can't do anything with undefined symbols. */
525 if (h->root.type == bfd_link_hash_undefined
526 || h->root.type == bfd_link_hash_undefweak)
527 continue;
529 tsec = h->root.u.def.section;
530 toff = h->root.u.def.value;
534 toff += irel->r_addend;
536 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
538 roff = irel->r_offset;
540 if (is_branch)
542 bfd_signed_vma offset;
544 reladdr = (sec->output_section->vma
545 + sec->output_offset
546 + roff) & (bfd_vma) -4;
548 /* The .plt section is aligned at 32byte and the .text section
549 is aligned at 64byte. The .text section is right after the
550 .plt section. After the first relaxation pass, linker may
551 increase the gap between the .plt and .text sections up
552 to 32byte. We assume linker will always insert 32byte
553 between the .plt and .text sections after the first
554 relaxation pass. */
555 if (tsec == ia64_info->root.splt)
556 offset = -0x1000000 + 32;
557 else
558 offset = -0x1000000;
560 /* If the branch is in range, no need to do anything. */
561 if ((bfd_signed_vma) (symaddr - reladdr) >= offset
562 && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
564 /* If the 60-bit branch is in 21-bit range, optimize it. */
565 if (r_type == R_IA64_PCREL60B)
567 ia64_elf_relax_brl (contents, roff);
569 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
570 R_IA64_PCREL21B);
572 /* If the original relocation offset points to slot
573 1, change it to slot 2. */
574 if ((irel->r_offset & 3) == 1)
575 irel->r_offset += 1;
578 continue;
580 else if (r_type == R_IA64_PCREL60B)
581 continue;
582 else if (ia64_elf_relax_br (contents, roff))
584 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
585 R_IA64_PCREL60B);
587 /* Make the relocation offset point to slot 1. */
588 irel->r_offset = (irel->r_offset & ~((bfd_vma) 0x3)) + 1;
589 continue;
592 /* We can't put a trampoline in a .init/.fini section. Issue
593 an error. */
594 if (strcmp (sec->output_section->name, ".init") == 0
595 || strcmp (sec->output_section->name, ".fini") == 0)
597 _bfd_error_handler
598 /* xgettext:c-format */
599 (_("%pB: can't relax br at %#" PRIx64 " in section `%pA';"
600 " please use brl or indirect branch"),
601 sec->owner, (uint64_t) roff, sec);
602 bfd_set_error (bfd_error_bad_value);
603 goto error_return;
606 /* If the branch and target are in the same section, you've
607 got one honking big section and we can't help you unless
608 you are branching backwards. You'll get an error message
609 later. */
610 if (tsec == sec && toff > roff)
611 continue;
613 /* Look for an existing fixup to this address. */
614 for (f = fixups; f ; f = f->next)
615 if (f->tsec == tsec && f->toff == toff)
616 break;
618 if (f == NULL)
620 /* Two alternatives: If it's a branch to a PLT entry, we can
621 make a copy of the FULL_PLT entry. Otherwise, we'll have
622 to use a `brl' insn to get where we're going. */
624 size_t size;
626 if (tsec == ia64_info->root.splt)
627 size = sizeof (plt_full_entry);
628 else
629 size = sizeof (oor_brl);
631 /* Resize the current section to make room for the new branch. */
632 trampoff = (sec->size + 15) & (bfd_vma) -16;
634 /* If trampoline is out of range, there is nothing we
635 can do. */
636 offset = trampoff - (roff & (bfd_vma) -4);
637 if (offset < -0x1000000 || offset > 0x0FFFFF0)
638 continue;
640 amt = trampoff + size;
641 contents = (bfd_byte *) bfd_realloc (contents, amt);
642 if (contents == NULL)
643 goto error_return;
644 sec->size = amt;
646 if (tsec == ia64_info->root.splt)
648 memcpy (contents + trampoff, plt_full_entry, size);
650 /* Hijack the old relocation for use as the PLTOFF reloc. */
651 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
652 R_IA64_PLTOFF22);
653 irel->r_offset = trampoff;
655 else
657 memcpy (contents + trampoff, oor_brl, size);
658 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
659 R_IA64_PCREL60B);
660 irel->r_offset = trampoff + 2;
663 /* Record the fixup so we don't do it again this section. */
664 f = (struct one_fixup *)
665 bfd_malloc ((bfd_size_type) sizeof (*f));
666 f->next = fixups;
667 f->tsec = tsec;
668 f->toff = toff;
669 f->trampoff = trampoff;
670 fixups = f;
672 else
674 /* If trampoline is out of range, there is nothing we
675 can do. */
676 offset = f->trampoff - (roff & (bfd_vma) -4);
677 if (offset < -0x1000000 || offset > 0x0FFFFF0)
678 continue;
680 /* Nop out the reloc, since we're finalizing things here. */
681 irel->r_info = ELF64_R_INFO (0, R_IA64_NONE);
684 /* Fix up the existing branch to hit the trampoline. */
685 if (ia64_elf_install_value (contents + roff, offset, r_type)
686 != bfd_reloc_ok)
687 goto error_return;
689 changed_contents = true;
690 changed_relocs = true;
692 else
694 /* Fetch the gp. */
695 if (gp == 0)
697 bfd *obfd = sec->output_section->owner;
698 gp = _bfd_get_gp_value (obfd);
699 if (gp == 0)
701 if (!elf64_ia64_choose_gp (obfd, link_info, false))
702 goto error_return;
703 gp = _bfd_get_gp_value (obfd);
707 /* If the data is out of range, do nothing. */
708 if ((bfd_signed_vma) (symaddr - gp) >= 0x200000
709 ||(bfd_signed_vma) (symaddr - gp) < -0x200000)
710 continue;
712 if (r_type == R_IA64_GPREL22)
713 elf64_ia64_update_short_info (tsec->output_section,
714 tsec->output_offset + toff,
715 ia64_info);
716 else if (r_type == R_IA64_LTOFF22X)
718 /* Can't deal yet correctly with ABS symbols. */
719 if (bfd_is_abs_section (tsec))
720 continue;
722 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
723 R_IA64_GPREL22);
724 changed_relocs = true;
726 elf64_ia64_update_short_info (tsec->output_section,
727 tsec->output_offset + toff,
728 ia64_info);
730 else
732 ia64_elf_relax_ldxmov (contents, roff);
733 irel->r_info = ELF64_R_INFO (0, R_IA64_NONE);
734 changed_contents = true;
735 changed_relocs = true;
740 /* ??? If we created fixups, this may push the code segment large
741 enough that the data segment moves, which will change the GP.
742 Reset the GP so that we re-calculate next round. We need to
743 do this at the _beginning_ of the next round; now will not do. */
745 /* Clean up and go home. */
746 while (fixups)
748 struct one_fixup *f = fixups;
749 fixups = fixups->next;
750 free (f);
753 if (isymbuf != NULL
754 && symtab_hdr->contents != (unsigned char *) isymbuf)
756 if (! link_info->keep_memory)
757 free (isymbuf);
758 else
760 /* Cache the symbols for elf_link_input_bfd. */
761 symtab_hdr->contents = (unsigned char *) isymbuf;
765 if (contents != NULL
766 && elf_section_data (sec)->this_hdr.contents != contents)
768 if (!changed_contents && !link_info->keep_memory)
769 free (contents);
770 else
772 /* Cache the section contents for elf_link_input_bfd. */
773 elf_section_data (sec)->this_hdr.contents = contents;
777 if (elf_section_data (sec)->relocs != internal_relocs)
779 if (!changed_relocs)
780 free (internal_relocs);
781 else
782 elf_section_data (sec)->relocs = internal_relocs;
785 if (link_info->relax_pass == 0)
787 /* Pass 0 is only needed to relax br. */
788 sec->skip_relax_pass_0 = skip_relax_pass_0;
789 sec->skip_relax_pass_1 = skip_relax_pass_1;
792 *again = changed_contents || changed_relocs;
793 return true;
795 error_return:
796 if ((unsigned char *) isymbuf != symtab_hdr->contents)
797 free (isymbuf);
798 if (elf_section_data (sec)->this_hdr.contents != contents)
799 free (contents);
800 if (elf_section_data (sec)->relocs != internal_relocs)
801 free (internal_relocs);
802 return false;
804 #undef skip_relax_pass_0
805 #undef skip_relax_pass_1
807 /* Return TRUE if NAME is an unwind table section name. */
809 static inline bool
810 is_unwind_section_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
812 return ((startswith (name, ELF_STRING_ia64_unwind)
813 && ! startswith (name, ELF_STRING_ia64_unwind_info))
814 || startswith (name, ELF_STRING_ia64_unwind_once));
818 /* Convert IA-64 specific section flags to bfd internal section flags. */
820 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
821 flag. */
823 static bool
824 elf64_ia64_section_flags (const Elf_Internal_Shdr *hdr)
826 if (hdr->sh_flags & SHF_IA_64_SHORT)
827 hdr->bfd_section->flags |= SEC_SMALL_DATA;
829 return true;
832 /* Set the correct type for an IA-64 ELF section. We do this by the
833 section name, which is a hack, but ought to work. */
835 static bool
836 elf64_ia64_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr,
837 asection *sec)
839 const char *name;
841 name = bfd_section_name (sec);
843 if (is_unwind_section_name (abfd, name))
845 /* We don't have the sections numbered at this point, so sh_info
846 is set later, in elf64_ia64_final_write_processing. */
847 hdr->sh_type = SHT_IA_64_UNWIND;
848 hdr->sh_flags |= SHF_LINK_ORDER;
850 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
851 hdr->sh_type = SHT_IA_64_EXT;
853 if (sec->flags & SEC_SMALL_DATA)
854 hdr->sh_flags |= SHF_IA_64_SHORT;
856 return true;
859 /* Hook called by the linker routine which adds symbols from an object
860 file. We use it to put .comm items in .sbss, and not .bss. */
862 static bool
863 elf64_ia64_add_symbol_hook (bfd *abfd,
864 struct bfd_link_info *info,
865 Elf_Internal_Sym *sym,
866 const char **namep ATTRIBUTE_UNUSED,
867 flagword *flagsp ATTRIBUTE_UNUSED,
868 asection **secp,
869 bfd_vma *valp)
871 if (sym->st_shndx == SHN_COMMON
872 && !bfd_link_relocatable (info)
873 && sym->st_size <= elf_gp_size (abfd))
875 /* Common symbols less than or equal to -G nn bytes are
876 automatically put into .sbss. */
878 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
880 if (scomm == NULL)
882 scomm = bfd_make_section_with_flags (abfd, ".scommon",
883 (SEC_ALLOC
884 | SEC_IS_COMMON
885 | SEC_SMALL_DATA
886 | SEC_LINKER_CREATED));
887 if (scomm == NULL)
888 return false;
891 *secp = scomm;
892 *valp = sym->st_size;
895 return true;
898 /* According to the Tahoe assembler spec, all labels starting with a
899 '.' are local. */
901 static bool
902 elf64_ia64_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
903 const char *name)
905 return name[0] == '.';
908 /* Should we do dynamic things to this symbol? */
910 static bool
911 elf64_ia64_dynamic_symbol_p (struct elf_link_hash_entry *h)
913 return h != NULL && h->def_dynamic;
916 static struct bfd_hash_entry*
917 elf64_ia64_new_elf_hash_entry (struct bfd_hash_entry *entry,
918 struct bfd_hash_table *table,
919 const char *string)
921 struct elf64_ia64_link_hash_entry *ret;
922 ret = (struct elf64_ia64_link_hash_entry *) entry;
924 /* Allocate the structure if it has not already been allocated by a
925 subclass. */
926 if (!ret)
927 ret = bfd_hash_allocate (table, sizeof (*ret));
929 if (!ret)
930 return 0;
932 /* Call the allocation method of the superclass. */
933 ret = ((struct elf64_ia64_link_hash_entry *)
934 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
935 table, string));
937 ret->info = NULL;
938 ret->count = 0;
939 ret->sorted_count = 0;
940 ret->size = 0;
941 return (struct bfd_hash_entry *) ret;
944 static void
945 elf64_ia64_hash_hide_symbol (struct bfd_link_info *info,
946 struct elf_link_hash_entry *xh,
947 bool force_local)
949 struct elf64_ia64_link_hash_entry *h;
950 struct elf64_ia64_dyn_sym_info *dyn_i;
951 unsigned int count;
953 h = (struct elf64_ia64_link_hash_entry *)xh;
955 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
957 for (count = h->count, dyn_i = h->info;
958 count != 0;
959 count--, dyn_i++)
961 dyn_i->want_plt2 = 0;
962 dyn_i->want_plt = 0;
966 /* Compute a hash of a local hash entry. */
968 static hashval_t
969 elf64_ia64_local_htab_hash (const void *ptr)
971 struct elf64_ia64_local_hash_entry *entry
972 = (struct elf64_ia64_local_hash_entry *) ptr;
974 return ELF_LOCAL_SYMBOL_HASH (entry->id, entry->r_sym);
977 /* Compare local hash entries. */
979 static int
980 elf64_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
982 struct elf64_ia64_local_hash_entry *entry1
983 = (struct elf64_ia64_local_hash_entry *) ptr1;
984 struct elf64_ia64_local_hash_entry *entry2
985 = (struct elf64_ia64_local_hash_entry *) ptr2;
987 return entry1->id == entry2->id && entry1->r_sym == entry2->r_sym;
990 /* Free the global elf64_ia64_dyn_sym_info array. */
992 static bool
993 elf64_ia64_global_dyn_info_free (struct elf_link_hash_entry *xentry,
994 void * unused ATTRIBUTE_UNUSED)
996 struct elf64_ia64_link_hash_entry *entry
997 = (struct elf64_ia64_link_hash_entry *) xentry;
999 if (entry->root.root.type == bfd_link_hash_warning)
1000 entry = (struct elf64_ia64_link_hash_entry *) entry->root.root.u.i.link;
1002 free (entry->info);
1003 entry->info = NULL;
1004 entry->count = 0;
1005 entry->sorted_count = 0;
1006 entry->size = 0;
1008 return true;
1011 /* Free the local elf64_ia64_dyn_sym_info array. */
1013 static int
1014 elf64_ia64_local_dyn_info_free (void **slot,
1015 void * unused ATTRIBUTE_UNUSED)
1017 struct elf64_ia64_local_hash_entry *entry
1018 = (struct elf64_ia64_local_hash_entry *) *slot;
1020 free (entry->info);
1021 entry->info = NULL;
1022 entry->count = 0;
1023 entry->sorted_count = 0;
1024 entry->size = 0;
1026 return true;
1029 /* Destroy IA-64 linker hash table. */
1031 static void
1032 elf64_ia64_link_hash_table_free (bfd *obfd)
1034 struct elf64_ia64_link_hash_table *ia64_info
1035 = (struct elf64_ia64_link_hash_table *) obfd->link.hash;
1036 if (ia64_info->loc_hash_table)
1038 htab_traverse (ia64_info->loc_hash_table,
1039 elf64_ia64_local_dyn_info_free, NULL);
1040 htab_delete (ia64_info->loc_hash_table);
1042 if (ia64_info->loc_hash_memory)
1043 objalloc_free ((struct objalloc *) ia64_info->loc_hash_memory);
1044 elf_link_hash_traverse (&ia64_info->root,
1045 elf64_ia64_global_dyn_info_free, NULL);
1046 _bfd_elf_link_hash_table_free (obfd);
1049 /* Create the derived linker hash table. The IA-64 ELF port uses this
1050 derived hash table to keep information specific to the IA-64 ElF
1051 linker (without using static variables). */
1053 static struct bfd_link_hash_table *
1054 elf64_ia64_hash_table_create (bfd *abfd)
1056 struct elf64_ia64_link_hash_table *ret;
1058 ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
1059 if (!ret)
1060 return NULL;
1062 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
1063 elf64_ia64_new_elf_hash_entry,
1064 sizeof (struct elf64_ia64_link_hash_entry),
1065 IA64_ELF_DATA))
1067 free (ret);
1068 return NULL;
1071 ret->loc_hash_table = htab_try_create (1024, elf64_ia64_local_htab_hash,
1072 elf64_ia64_local_htab_eq, NULL);
1073 ret->loc_hash_memory = objalloc_create ();
1074 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1076 elf64_ia64_link_hash_table_free (abfd);
1077 return NULL;
1079 ret->root.root.hash_table_free = elf64_ia64_link_hash_table_free;
1081 return &ret->root.root;
1084 /* Traverse both local and global hash tables. */
1086 struct elf64_ia64_dyn_sym_traverse_data
1088 bool (*func) (struct elf64_ia64_dyn_sym_info *, void *);
1089 void * data;
1092 static bool
1093 elf64_ia64_global_dyn_sym_thunk (struct elf_link_hash_entry *xentry,
1094 void * xdata)
1096 struct elf64_ia64_link_hash_entry *entry
1097 = (struct elf64_ia64_link_hash_entry *) xentry;
1098 struct elf64_ia64_dyn_sym_traverse_data *data
1099 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1100 struct elf64_ia64_dyn_sym_info *dyn_i;
1101 unsigned int count;
1103 if (entry->root.root.type == bfd_link_hash_warning)
1104 entry = (struct elf64_ia64_link_hash_entry *) entry->root.root.u.i.link;
1106 for (count = entry->count, dyn_i = entry->info;
1107 count != 0;
1108 count--, dyn_i++)
1109 if (! (*data->func) (dyn_i, data->data))
1110 return false;
1111 return true;
1114 static int
1115 elf64_ia64_local_dyn_sym_thunk (void **slot, void * xdata)
1117 struct elf64_ia64_local_hash_entry *entry
1118 = (struct elf64_ia64_local_hash_entry *) *slot;
1119 struct elf64_ia64_dyn_sym_traverse_data *data
1120 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1121 struct elf64_ia64_dyn_sym_info *dyn_i;
1122 unsigned int count;
1124 for (count = entry->count, dyn_i = entry->info;
1125 count != 0;
1126 count--, dyn_i++)
1127 if (! (*data->func) (dyn_i, data->data))
1128 return false;
1129 return true;
1132 static void
1133 elf64_ia64_dyn_sym_traverse (struct elf64_ia64_link_hash_table *ia64_info,
1134 bool (*func) (struct elf64_ia64_dyn_sym_info *, void *),
1135 void * data)
1137 struct elf64_ia64_dyn_sym_traverse_data xdata;
1139 xdata.func = func;
1140 xdata.data = data;
1142 elf_link_hash_traverse (&ia64_info->root,
1143 elf64_ia64_global_dyn_sym_thunk, &xdata);
1144 htab_traverse (ia64_info->loc_hash_table,
1145 elf64_ia64_local_dyn_sym_thunk, &xdata);
1148 #define NOTE_NAME "IPF/VMS"
1150 static bool
1151 create_ia64_vms_notes (bfd *abfd, struct bfd_link_info *info,
1152 unsigned int time_hi, unsigned int time_lo)
1154 #define NBR_NOTES 7
1155 Elf_Internal_Note notes[NBR_NOTES];
1156 char *module_name;
1157 int module_name_len;
1158 unsigned char cur_time[8];
1159 Elf64_External_VMS_ORIG_DYN_Note *orig_dyn;
1160 unsigned int orig_dyn_size;
1161 unsigned int note_size;
1162 int i;
1163 unsigned char *noteptr;
1164 unsigned char *note_contents;
1165 struct elf64_ia64_link_hash_table *ia64_info;
1167 ia64_info = elf64_ia64_hash_table (info);
1169 module_name = vms_get_module_name (bfd_get_filename (abfd), true);
1170 module_name_len = strlen (module_name) + 1;
1172 bfd_putl32 (time_lo, cur_time + 0);
1173 bfd_putl32 (time_hi, cur_time + 4);
1175 /* Note 0: IMGNAM. */
1176 notes[0].type = NT_VMS_IMGNAM;
1177 notes[0].descdata = module_name;
1178 notes[0].descsz = module_name_len;
1180 /* Note 1: GSTNAM. */
1181 notes[1].type = NT_VMS_GSTNAM;
1182 notes[1].descdata = module_name;
1183 notes[1].descsz = module_name_len;
1185 /* Note 2: IMGID. */
1186 #define IMG_ID "V1.0"
1187 notes[2].type = NT_VMS_IMGID;
1188 notes[2].descdata = IMG_ID;
1189 notes[2].descsz = sizeof (IMG_ID);
1191 /* Note 3: Linktime. */
1192 notes[3].type = NT_VMS_LINKTIME;
1193 notes[3].descdata = (char *)cur_time;
1194 notes[3].descsz = sizeof (cur_time);
1196 /* Note 4: Linker id. */
1197 notes[4].type = NT_VMS_LINKID;
1198 notes[4].descdata = "GNU ld " BFD_VERSION_STRING;
1199 notes[4].descsz = strlen (notes[4].descdata) + 1;
1201 /* Note 5: Original dyn. */
1202 orig_dyn_size = (sizeof (*orig_dyn) + sizeof (IMG_ID) - 1 + 7) & ~7;
1203 orig_dyn = bfd_zalloc (abfd, orig_dyn_size);
1204 if (orig_dyn == NULL)
1205 return false;
1206 bfd_putl32 (1, orig_dyn->major_id);
1207 bfd_putl32 (3, orig_dyn->minor_id);
1208 memcpy (orig_dyn->manipulation_date, cur_time, sizeof (cur_time));
1209 bfd_putl64 (VMS_LF_IMGSTA | VMS_LF_MAIN, orig_dyn->link_flags);
1210 bfd_putl32 (EF_IA_64_ABI64, orig_dyn->elf_flags);
1211 memcpy (orig_dyn->imgid, IMG_ID, sizeof (IMG_ID));
1212 notes[5].type = NT_VMS_ORIG_DYN;
1213 notes[5].descdata = (char *)orig_dyn;
1214 notes[5].descsz = orig_dyn_size;
1216 /* Note 3: Patchtime. */
1217 notes[6].type = NT_VMS_PATCHTIME;
1218 notes[6].descdata = (char *)cur_time;
1219 notes[6].descsz = sizeof (cur_time);
1221 /* Compute notes size. */
1222 note_size = 0;
1223 for (i = 0; i < NBR_NOTES; i++)
1224 note_size += sizeof (Elf64_External_VMS_Note) - 1
1225 + ((sizeof (NOTE_NAME) - 1 + 7) & ~7)
1226 + ((notes[i].descsz + 7) & ~7);
1228 /* Malloc a temporary buffer large enough for most notes */
1229 note_contents = (unsigned char *) bfd_zalloc (abfd, note_size);
1230 if (note_contents == NULL)
1231 return false;
1232 noteptr = note_contents;
1234 /* Fill notes. */
1235 for (i = 0; i < NBR_NOTES; i++)
1237 Elf64_External_VMS_Note *enote = (Elf64_External_VMS_Note *) noteptr;
1239 bfd_putl64 (sizeof (NOTE_NAME) - 1, enote->namesz);
1240 bfd_putl64 (notes[i].descsz, enote->descsz);
1241 bfd_putl64 (notes[i].type, enote->type);
1243 noteptr = (unsigned char *)enote->name;
1244 memcpy (noteptr, NOTE_NAME, sizeof (NOTE_NAME) - 1);
1245 noteptr += (sizeof (NOTE_NAME) - 1 + 7) & ~7;
1246 memcpy (noteptr, notes[i].descdata, notes[i].descsz);
1247 noteptr += (notes[i].descsz + 7) & ~7;
1250 ia64_info->note_sec->contents = note_contents;
1251 ia64_info->note_sec->size = note_size;
1253 free (module_name);
1255 return true;
1258 static bool
1259 elf64_ia64_create_dynamic_sections (bfd *abfd,
1260 struct bfd_link_info *info)
1262 struct elf64_ia64_link_hash_table *ia64_info;
1263 asection *s;
1264 flagword flags;
1265 const struct elf_backend_data *bed;
1267 ia64_info = elf64_ia64_hash_table (info);
1268 if (ia64_info == NULL)
1269 return false;
1271 if (elf_hash_table (info)->dynamic_sections_created)
1272 return true;
1274 abfd = elf_hash_table (info)->dynobj;
1275 bed = get_elf_backend_data (abfd);
1277 flags = bed->dynamic_sec_flags;
1279 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic",
1280 flags | SEC_READONLY);
1281 if (s == NULL
1282 || !bfd_set_section_alignment (s, bed->s->log_file_align))
1283 return false;
1285 s = bfd_make_section_anyway_with_flags (abfd, ".plt", flags | SEC_READONLY);
1286 if (s == NULL
1287 || !bfd_set_section_alignment (s, bed->plt_alignment))
1288 return false;
1289 ia64_info->root.splt = s;
1291 if (!get_got (abfd, ia64_info))
1292 return false;
1294 if (!get_pltoff (abfd, ia64_info))
1295 return false;
1297 s = bfd_make_section_anyway_with_flags (abfd, ".vmsdynstr",
1298 (SEC_ALLOC
1299 | SEC_HAS_CONTENTS
1300 | SEC_IN_MEMORY
1301 | SEC_LINKER_CREATED));
1302 if (s == NULL
1303 || !bfd_set_section_alignment (s, 0))
1304 return false;
1306 /* Create a fixup section. */
1307 s = bfd_make_section_anyway_with_flags (abfd, ".fixups",
1308 (SEC_ALLOC
1309 | SEC_HAS_CONTENTS
1310 | SEC_IN_MEMORY
1311 | SEC_LINKER_CREATED));
1312 if (s == NULL
1313 || !bfd_set_section_alignment (s, 3))
1314 return false;
1315 ia64_info->fixups_sec = s;
1317 /* Create the transfer fixup section. */
1318 s = bfd_make_section_anyway_with_flags (abfd, ".transfer",
1319 (SEC_ALLOC
1320 | SEC_HAS_CONTENTS
1321 | SEC_IN_MEMORY
1322 | SEC_LINKER_CREATED));
1323 if (s == NULL
1324 || !bfd_set_section_alignment (s, 3))
1325 return false;
1326 s->size = sizeof (struct elf64_vms_transfer);
1327 ia64_info->transfer_sec = s;
1329 /* Create note section. */
1330 s = bfd_make_section_anyway_with_flags (abfd, ".vms.note",
1331 (SEC_LINKER_CREATED
1332 | SEC_HAS_CONTENTS
1333 | SEC_IN_MEMORY
1334 | SEC_READONLY));
1335 if (s == NULL
1336 || !bfd_set_section_alignment (s, 3))
1337 return false;
1338 ia64_info->note_sec = s;
1340 elf_hash_table (info)->dynamic_sections_created = true;
1341 return true;
1344 /* Find and/or create a hash entry for local symbol. */
1345 static struct elf64_ia64_local_hash_entry *
1346 get_local_sym_hash (struct elf64_ia64_link_hash_table *ia64_info,
1347 bfd *abfd, const Elf_Internal_Rela *rel,
1348 bool create)
1350 struct elf64_ia64_local_hash_entry e, *ret;
1351 asection *sec = abfd->sections;
1352 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1353 ELF64_R_SYM (rel->r_info));
1354 void **slot;
1356 e.id = sec->id;
1357 e.r_sym = ELF64_R_SYM (rel->r_info);
1358 slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
1359 create ? INSERT : NO_INSERT);
1361 if (!slot)
1362 return NULL;
1364 if (*slot)
1365 return (struct elf64_ia64_local_hash_entry *) *slot;
1367 ret = (struct elf64_ia64_local_hash_entry *)
1368 objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
1369 sizeof (struct elf64_ia64_local_hash_entry));
1370 if (ret)
1372 memset (ret, 0, sizeof (*ret));
1373 ret->id = sec->id;
1374 ret->r_sym = ELF64_R_SYM (rel->r_info);
1375 *slot = ret;
1377 return ret;
1380 /* Used to sort elf64_ia64_dyn_sym_info array. */
1382 static int
1383 addend_compare (const void *xp, const void *yp)
1385 const struct elf64_ia64_dyn_sym_info *x
1386 = (const struct elf64_ia64_dyn_sym_info *) xp;
1387 const struct elf64_ia64_dyn_sym_info *y
1388 = (const struct elf64_ia64_dyn_sym_info *) yp;
1390 return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
1393 /* Sort elf64_ia64_dyn_sym_info array and remove duplicates. */
1395 static unsigned int
1396 sort_dyn_sym_info (struct elf64_ia64_dyn_sym_info *info,
1397 unsigned int count)
1399 bfd_vma curr, prev, got_offset;
1400 unsigned int i, kept, dupes, diff, dest, src, len;
1402 qsort (info, count, sizeof (*info), addend_compare);
1404 /* Find the first duplicate. */
1405 prev = info [0].addend;
1406 got_offset = info [0].got_offset;
1407 for (i = 1; i < count; i++)
1409 curr = info [i].addend;
1410 if (curr == prev)
1412 /* For duplicates, make sure that GOT_OFFSET is valid. */
1413 if (got_offset == (bfd_vma) -1)
1414 got_offset = info [i].got_offset;
1415 break;
1417 got_offset = info [i].got_offset;
1418 prev = curr;
1421 /* We may move a block of elements to here. */
1422 dest = i++;
1424 /* Remove duplicates. */
1425 if (i < count)
1427 while (i < count)
1429 /* For duplicates, make sure that the kept one has a valid
1430 got_offset. */
1431 kept = dest - 1;
1432 if (got_offset != (bfd_vma) -1)
1433 info [kept].got_offset = got_offset;
1435 curr = info [i].addend;
1436 got_offset = info [i].got_offset;
1438 /* Move a block of elements whose first one is different from
1439 the previous. */
1440 if (curr == prev)
1442 for (src = i + 1; src < count; src++)
1444 if (info [src].addend != curr)
1445 break;
1446 /* For duplicates, make sure that GOT_OFFSET is
1447 valid. */
1448 if (got_offset == (bfd_vma) -1)
1449 got_offset = info [src].got_offset;
1452 /* Make sure that the kept one has a valid got_offset. */
1453 if (got_offset != (bfd_vma) -1)
1454 info [kept].got_offset = got_offset;
1456 else
1457 src = i;
1459 if (src >= count)
1460 break;
1462 /* Find the next duplicate. SRC will be kept. */
1463 prev = info [src].addend;
1464 got_offset = info [src].got_offset;
1465 for (dupes = src + 1; dupes < count; dupes ++)
1467 curr = info [dupes].addend;
1468 if (curr == prev)
1470 /* Make sure that got_offset is valid. */
1471 if (got_offset == (bfd_vma) -1)
1472 got_offset = info [dupes].got_offset;
1474 /* For duplicates, make sure that the kept one has
1475 a valid got_offset. */
1476 if (got_offset != (bfd_vma) -1)
1477 info [dupes - 1].got_offset = got_offset;
1478 break;
1480 got_offset = info [dupes].got_offset;
1481 prev = curr;
1484 /* How much to move. */
1485 len = dupes - src;
1486 i = dupes + 1;
1488 if (len == 1 && dupes < count)
1490 /* If we only move 1 element, we combine it with the next
1491 one. There must be at least a duplicate. Find the
1492 next different one. */
1493 for (diff = dupes + 1, src++; diff < count; diff++, src++)
1495 if (info [diff].addend != curr)
1496 break;
1497 /* Make sure that got_offset is valid. */
1498 if (got_offset == (bfd_vma) -1)
1499 got_offset = info [diff].got_offset;
1502 /* Makre sure that the last duplicated one has an valid
1503 offset. */
1504 BFD_ASSERT (curr == prev);
1505 if (got_offset != (bfd_vma) -1)
1506 info [diff - 1].got_offset = got_offset;
1508 if (diff < count)
1510 /* Find the next duplicate. Track the current valid
1511 offset. */
1512 prev = info [diff].addend;
1513 got_offset = info [diff].got_offset;
1514 for (dupes = diff + 1; dupes < count; dupes ++)
1516 curr = info [dupes].addend;
1517 if (curr == prev)
1519 /* For duplicates, make sure that GOT_OFFSET
1520 is valid. */
1521 if (got_offset == (bfd_vma) -1)
1522 got_offset = info [dupes].got_offset;
1523 break;
1525 got_offset = info [dupes].got_offset;
1526 prev = curr;
1527 diff++;
1530 len = diff - src + 1;
1531 i = diff + 1;
1535 memmove (&info [dest], &info [src], len * sizeof (*info));
1537 dest += len;
1540 count = dest;
1542 else
1544 /* When we get here, either there is no duplicate at all or
1545 the only duplicate is the last element. */
1546 if (dest < count)
1548 /* If the last element is a duplicate, make sure that the
1549 kept one has a valid got_offset. We also update count. */
1550 if (got_offset != (bfd_vma) -1)
1551 info [dest - 1].got_offset = got_offset;
1552 count = dest;
1556 return count;
1559 /* Find and/or create a descriptor for dynamic symbol info. This will
1560 vary based on global or local symbol, and the addend to the reloc.
1562 We don't sort when inserting. Also, we sort and eliminate
1563 duplicates if there is an unsorted section. Typically, this will
1564 only happen once, because we do all insertions before lookups. We
1565 then use bsearch to do a lookup. This also allows lookups to be
1566 fast. So we have fast insertion (O(log N) due to duplicate check),
1567 fast lookup (O(log N)) and one sort (O(N log N) expected time).
1568 Previously, all lookups were O(N) because of the use of the linked
1569 list and also all insertions were O(N) because of the check for
1570 duplicates. There are some complications here because the array
1571 size grows occasionally, which may add an O(N) factor, but this
1572 should be rare. Also, we free the excess array allocation, which
1573 requires a copy which is O(N), but this only happens once. */
1575 static struct elf64_ia64_dyn_sym_info *
1576 get_dyn_sym_info (struct elf64_ia64_link_hash_table *ia64_info,
1577 struct elf_link_hash_entry *h, bfd *abfd,
1578 const Elf_Internal_Rela *rel, bool create)
1580 struct elf64_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
1581 unsigned int *count_p, *sorted_count_p, *size_p;
1582 unsigned int count, sorted_count, size;
1583 bfd_vma addend = rel ? rel->r_addend : 0;
1584 bfd_size_type amt;
1586 if (h)
1588 struct elf64_ia64_link_hash_entry *global_h;
1590 global_h = (struct elf64_ia64_link_hash_entry *) h;
1591 info_p = &global_h->info;
1592 count_p = &global_h->count;
1593 sorted_count_p = &global_h->sorted_count;
1594 size_p = &global_h->size;
1596 else
1598 struct elf64_ia64_local_hash_entry *loc_h;
1600 loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
1601 if (!loc_h)
1603 BFD_ASSERT (!create);
1604 return NULL;
1607 info_p = &loc_h->info;
1608 count_p = &loc_h->count;
1609 sorted_count_p = &loc_h->sorted_count;
1610 size_p = &loc_h->size;
1613 count = *count_p;
1614 sorted_count = *sorted_count_p;
1615 size = *size_p;
1616 info = *info_p;
1617 if (create)
1619 /* When we create the array, we don't check for duplicates,
1620 except in the previously sorted section if one exists, and
1621 against the last inserted entry. This allows insertions to
1622 be fast. */
1623 if (info)
1625 if (sorted_count)
1627 /* Try bsearch first on the sorted section. */
1628 key.addend = addend;
1629 dyn_i = bsearch (&key, info, sorted_count,
1630 sizeof (*info), addend_compare);
1632 if (dyn_i)
1634 return dyn_i;
1638 /* Do a quick check for the last inserted entry. */
1639 dyn_i = info + count - 1;
1640 if (dyn_i->addend == addend)
1642 return dyn_i;
1646 if (size == 0)
1648 /* It is the very first element. We create the array of size
1649 1. */
1650 size = 1;
1651 amt = size * sizeof (*info);
1652 info = bfd_malloc (amt);
1654 else if (size <= count)
1656 /* We double the array size every time when we reach the
1657 size limit. */
1658 size += size;
1659 amt = size * sizeof (*info);
1660 info = bfd_realloc (info, amt);
1662 else
1663 goto has_space;
1665 if (info == NULL)
1666 return NULL;
1667 *size_p = size;
1668 *info_p = info;
1670 has_space:
1671 /* Append the new one to the array. */
1672 dyn_i = info + count;
1673 memset (dyn_i, 0, sizeof (*dyn_i));
1674 dyn_i->got_offset = (bfd_vma) -1;
1675 dyn_i->addend = addend;
1677 /* We increment count only since the new ones are unsorted and
1678 may have duplicate. */
1679 (*count_p)++;
1681 else
1683 /* It is a lookup without insertion. Sort array if part of the
1684 array isn't sorted. */
1685 if (count != sorted_count)
1687 count = sort_dyn_sym_info (info, count);
1688 *count_p = count;
1689 *sorted_count_p = count;
1692 /* Free unused memory. */
1693 if (size != count)
1695 amt = count * sizeof (*info);
1696 info = bfd_malloc (amt);
1697 if (info != NULL)
1699 memcpy (info, *info_p, amt);
1700 free (*info_p);
1701 *size_p = count;
1702 *info_p = info;
1706 key.addend = addend;
1707 dyn_i = bsearch (&key, info, count,
1708 sizeof (*info), addend_compare);
1711 return dyn_i;
1714 static asection *
1715 get_got (bfd *abfd, struct elf64_ia64_link_hash_table *ia64_info)
1717 asection *got;
1718 bfd *dynobj;
1720 got = ia64_info->root.sgot;
1721 if (!got)
1723 flagword flags;
1725 dynobj = ia64_info->root.dynobj;
1726 if (!dynobj)
1727 ia64_info->root.dynobj = dynobj = abfd;
1729 /* The .got section is always aligned at 8 bytes. */
1730 flags = get_elf_backend_data (dynobj)->dynamic_sec_flags;
1731 got = bfd_make_section_anyway_with_flags (dynobj, ".got",
1732 flags | SEC_SMALL_DATA);
1733 if (got == NULL
1734 || !bfd_set_section_alignment (got, 3))
1735 return NULL;
1736 ia64_info->root.sgot = got;
1739 return got;
1742 /* Create function descriptor section (.opd). This section is called .opd
1743 because it contains "official procedure descriptors". The "official"
1744 refers to the fact that these descriptors are used when taking the address
1745 of a procedure, thus ensuring a unique address for each procedure. */
1747 static asection *
1748 get_fptr (bfd *abfd, struct bfd_link_info *info,
1749 struct elf64_ia64_link_hash_table *ia64_info)
1751 asection *fptr;
1752 bfd *dynobj;
1754 fptr = ia64_info->fptr_sec;
1755 if (!fptr)
1757 dynobj = ia64_info->root.dynobj;
1758 if (!dynobj)
1759 ia64_info->root.dynobj = dynobj = abfd;
1761 fptr = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1762 (SEC_ALLOC
1763 | SEC_LOAD
1764 | SEC_HAS_CONTENTS
1765 | SEC_IN_MEMORY
1766 | (bfd_link_pie (info) ? 0
1767 : SEC_READONLY)
1768 | SEC_LINKER_CREATED));
1769 if (!fptr
1770 || !bfd_set_section_alignment (fptr, 4))
1772 BFD_ASSERT (0);
1773 return NULL;
1776 ia64_info->fptr_sec = fptr;
1778 if (bfd_link_pie (info))
1780 asection *fptr_rel;
1781 fptr_rel = bfd_make_section_anyway_with_flags (dynobj, ".rela.opd",
1782 (SEC_ALLOC | SEC_LOAD
1783 | SEC_HAS_CONTENTS
1784 | SEC_IN_MEMORY
1785 | SEC_LINKER_CREATED
1786 | SEC_READONLY));
1787 if (fptr_rel == NULL
1788 || !bfd_set_section_alignment (fptr_rel, 3))
1790 BFD_ASSERT (0);
1791 return NULL;
1794 ia64_info->rel_fptr_sec = fptr_rel;
1798 return fptr;
1801 static asection *
1802 get_pltoff (bfd *abfd, struct elf64_ia64_link_hash_table *ia64_info)
1804 asection *pltoff;
1805 bfd *dynobj;
1807 pltoff = ia64_info->pltoff_sec;
1808 if (!pltoff)
1810 dynobj = ia64_info->root.dynobj;
1811 if (!dynobj)
1812 ia64_info->root.dynobj = dynobj = abfd;
1814 pltoff = bfd_make_section_anyway_with_flags (dynobj,
1815 ELF_STRING_ia64_pltoff,
1816 (SEC_ALLOC
1817 | SEC_LOAD
1818 | SEC_HAS_CONTENTS
1819 | SEC_IN_MEMORY
1820 | SEC_SMALL_DATA
1821 | SEC_LINKER_CREATED));
1822 if (!pltoff
1823 || !bfd_set_section_alignment (pltoff, 4))
1825 BFD_ASSERT (0);
1826 return NULL;
1829 ia64_info->pltoff_sec = pltoff;
1832 return pltoff;
1835 static asection *
1836 get_reloc_section (bfd *abfd,
1837 struct elf64_ia64_link_hash_table *ia64_info,
1838 asection *sec, bool create)
1840 const char *srel_name;
1841 asection *srel;
1842 bfd *dynobj;
1844 srel_name = (bfd_elf_string_from_elf_section
1845 (abfd, elf_elfheader(abfd)->e_shstrndx,
1846 _bfd_elf_single_rel_hdr (sec)->sh_name));
1847 if (srel_name == NULL)
1848 return NULL;
1850 BFD_ASSERT ((startswith (srel_name, ".rela")
1851 && strcmp (bfd_section_name (sec), srel_name+5) == 0)
1852 || (startswith (srel_name, ".rel")
1853 && strcmp (bfd_section_name (sec), srel_name+4) == 0));
1855 dynobj = ia64_info->root.dynobj;
1856 if (!dynobj)
1857 ia64_info->root.dynobj = dynobj = abfd;
1859 srel = bfd_get_linker_section (dynobj, srel_name);
1860 if (srel == NULL && create)
1862 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
1863 (SEC_ALLOC | SEC_LOAD
1864 | SEC_HAS_CONTENTS
1865 | SEC_IN_MEMORY
1866 | SEC_LINKER_CREATED
1867 | SEC_READONLY));
1868 if (srel == NULL
1869 || !bfd_set_section_alignment (srel, 3))
1870 return NULL;
1873 return srel;
1876 static bool
1877 count_dyn_reloc (bfd *abfd, struct elf64_ia64_dyn_sym_info *dyn_i,
1878 asection *srel, int type)
1880 struct elf64_ia64_dyn_reloc_entry *rent;
1882 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
1883 if (rent->srel == srel && rent->type == type)
1884 break;
1886 if (!rent)
1888 rent = ((struct elf64_ia64_dyn_reloc_entry *)
1889 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
1890 if (!rent)
1891 return false;
1893 rent->next = dyn_i->reloc_entries;
1894 rent->srel = srel;
1895 rent->type = type;
1896 rent->count = 0;
1897 dyn_i->reloc_entries = rent;
1899 rent->count++;
1901 return true;
1904 static bool
1905 elf64_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1906 asection *sec,
1907 const Elf_Internal_Rela *relocs)
1909 struct elf64_ia64_link_hash_table *ia64_info;
1910 const Elf_Internal_Rela *relend;
1911 Elf_Internal_Shdr *symtab_hdr;
1912 const Elf_Internal_Rela *rel;
1913 asection *got, *fptr, *srel, *pltoff;
1914 enum {
1915 NEED_GOT = 1,
1916 NEED_GOTX = 2,
1917 NEED_FPTR = 4,
1918 NEED_PLTOFF = 8,
1919 NEED_MIN_PLT = 16,
1920 NEED_FULL_PLT = 32,
1921 NEED_DYNREL = 64,
1922 NEED_LTOFF_FPTR = 128
1924 int need_entry;
1925 struct elf_link_hash_entry *h;
1926 unsigned long r_symndx;
1927 bool maybe_dynamic;
1929 if (bfd_link_relocatable (info))
1930 return true;
1932 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1933 ia64_info = elf64_ia64_hash_table (info);
1934 if (ia64_info == NULL)
1935 return false;
1937 got = fptr = srel = pltoff = NULL;
1939 relend = relocs + sec->reloc_count;
1941 /* We scan relocations first to create dynamic relocation arrays. We
1942 modified get_dyn_sym_info to allow fast insertion and support fast
1943 lookup in the next loop. */
1944 for (rel = relocs; rel < relend; ++rel)
1946 r_symndx = ELF64_R_SYM (rel->r_info);
1947 if (r_symndx >= symtab_hdr->sh_info)
1949 long indx = r_symndx - symtab_hdr->sh_info;
1950 h = elf_sym_hashes (abfd)[indx];
1951 while (h->root.type == bfd_link_hash_indirect
1952 || h->root.type == bfd_link_hash_warning)
1953 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1955 else
1956 h = NULL;
1958 /* We can only get preliminary data on whether a symbol is
1959 locally or externally defined, as not all of the input files
1960 have yet been processed. Do something with what we know, as
1961 this may help reduce memory usage and processing time later. */
1962 maybe_dynamic = (h && ((!bfd_link_executable (info)
1963 && (!SYMBOLIC_BIND (info, h)
1964 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
1965 || !h->def_regular
1966 || h->root.type == bfd_link_hash_defweak));
1968 need_entry = 0;
1969 switch (ELF64_R_TYPE (rel->r_info))
1971 case R_IA64_TPREL64MSB:
1972 case R_IA64_TPREL64LSB:
1973 case R_IA64_LTOFF_TPREL22:
1974 case R_IA64_DTPREL32MSB:
1975 case R_IA64_DTPREL32LSB:
1976 case R_IA64_DTPREL64MSB:
1977 case R_IA64_DTPREL64LSB:
1978 case R_IA64_LTOFF_DTPREL22:
1979 case R_IA64_DTPMOD64MSB:
1980 case R_IA64_DTPMOD64LSB:
1981 case R_IA64_LTOFF_DTPMOD22:
1982 abort ();
1983 break;
1985 case R_IA64_IPLTMSB:
1986 case R_IA64_IPLTLSB:
1987 break;
1989 case R_IA64_LTOFF_FPTR22:
1990 case R_IA64_LTOFF_FPTR64I:
1991 case R_IA64_LTOFF_FPTR32MSB:
1992 case R_IA64_LTOFF_FPTR32LSB:
1993 case R_IA64_LTOFF_FPTR64MSB:
1994 case R_IA64_LTOFF_FPTR64LSB:
1995 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
1996 break;
1998 case R_IA64_FPTR64I:
1999 case R_IA64_FPTR32MSB:
2000 case R_IA64_FPTR32LSB:
2001 case R_IA64_FPTR64MSB:
2002 case R_IA64_FPTR64LSB:
2003 if (bfd_link_pic (info) || h)
2004 need_entry = NEED_FPTR | NEED_DYNREL;
2005 else
2006 need_entry = NEED_FPTR;
2007 break;
2009 case R_IA64_LTOFF22:
2010 case R_IA64_LTOFF64I:
2011 need_entry = NEED_GOT;
2012 break;
2014 case R_IA64_LTOFF22X:
2015 need_entry = NEED_GOTX;
2016 break;
2018 case R_IA64_PLTOFF22:
2019 case R_IA64_PLTOFF64I:
2020 case R_IA64_PLTOFF64MSB:
2021 case R_IA64_PLTOFF64LSB:
2022 need_entry = NEED_PLTOFF;
2023 if (h)
2025 if (maybe_dynamic)
2026 need_entry |= NEED_MIN_PLT;
2028 else
2030 (*info->callbacks->warning)
2031 (info, _("@pltoff reloc against local symbol"), 0,
2032 abfd, 0, (bfd_vma) 0);
2034 break;
2036 case R_IA64_PCREL21B:
2037 case R_IA64_PCREL60B:
2038 /* Depending on where this symbol is defined, we may or may not
2039 need a full plt entry. Only skip if we know we'll not need
2040 the entry -- static or symbolic, and the symbol definition
2041 has already been seen. */
2042 if (maybe_dynamic && rel->r_addend == 0)
2043 need_entry = NEED_FULL_PLT;
2044 break;
2046 case R_IA64_IMM14:
2047 case R_IA64_IMM22:
2048 case R_IA64_IMM64:
2049 case R_IA64_DIR32MSB:
2050 case R_IA64_DIR32LSB:
2051 case R_IA64_DIR64MSB:
2052 case R_IA64_DIR64LSB:
2053 /* Shared objects will always need at least a REL relocation. */
2054 if (bfd_link_pic (info) || maybe_dynamic)
2055 need_entry = NEED_DYNREL;
2056 break;
2058 case R_IA64_PCREL22:
2059 case R_IA64_PCREL64I:
2060 case R_IA64_PCREL32MSB:
2061 case R_IA64_PCREL32LSB:
2062 case R_IA64_PCREL64MSB:
2063 case R_IA64_PCREL64LSB:
2064 if (maybe_dynamic)
2065 need_entry = NEED_DYNREL;
2066 break;
2069 if (!need_entry)
2070 continue;
2072 if ((need_entry & NEED_FPTR) != 0
2073 && rel->r_addend)
2075 (*info->callbacks->warning)
2076 (info, _("non-zero addend in @fptr reloc"), 0,
2077 abfd, 0, (bfd_vma) 0);
2080 if (get_dyn_sym_info (ia64_info, h, abfd, rel, true) == NULL)
2081 return false;
2084 /* Now, we only do lookup without insertion, which is very fast
2085 with the modified get_dyn_sym_info. */
2086 for (rel = relocs; rel < relend; ++rel)
2088 struct elf64_ia64_dyn_sym_info *dyn_i;
2089 int dynrel_type = R_IA64_NONE;
2091 r_symndx = ELF64_R_SYM (rel->r_info);
2092 if (r_symndx >= symtab_hdr->sh_info)
2094 /* We're dealing with a global symbol -- find its hash entry
2095 and mark it as being referenced. */
2096 long indx = r_symndx - symtab_hdr->sh_info;
2097 h = elf_sym_hashes (abfd)[indx];
2098 while (h->root.type == bfd_link_hash_indirect
2099 || h->root.type == bfd_link_hash_warning)
2100 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2102 /* PR15323, ref flags aren't set for references in the same
2103 object. */
2104 h->ref_regular = 1;
2106 else
2107 h = NULL;
2109 /* We can only get preliminary data on whether a symbol is
2110 locally or externally defined, as not all of the input files
2111 have yet been processed. Do something with what we know, as
2112 this may help reduce memory usage and processing time later. */
2113 maybe_dynamic = (h && ((!bfd_link_executable (info)
2114 && (!SYMBOLIC_BIND (info, h)
2115 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2116 || !h->def_regular
2117 || h->root.type == bfd_link_hash_defweak));
2119 need_entry = 0;
2120 switch (ELF64_R_TYPE (rel->r_info))
2122 case R_IA64_TPREL64MSB:
2123 case R_IA64_TPREL64LSB:
2124 case R_IA64_LTOFF_TPREL22:
2125 case R_IA64_DTPREL32MSB:
2126 case R_IA64_DTPREL32LSB:
2127 case R_IA64_DTPREL64MSB:
2128 case R_IA64_DTPREL64LSB:
2129 case R_IA64_LTOFF_DTPREL22:
2130 case R_IA64_DTPMOD64MSB:
2131 case R_IA64_DTPMOD64LSB:
2132 case R_IA64_LTOFF_DTPMOD22:
2133 abort ();
2134 break;
2136 case R_IA64_LTOFF_FPTR22:
2137 case R_IA64_LTOFF_FPTR64I:
2138 case R_IA64_LTOFF_FPTR32MSB:
2139 case R_IA64_LTOFF_FPTR32LSB:
2140 case R_IA64_LTOFF_FPTR64MSB:
2141 case R_IA64_LTOFF_FPTR64LSB:
2142 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2143 break;
2145 case R_IA64_FPTR64I:
2146 case R_IA64_FPTR32MSB:
2147 case R_IA64_FPTR32LSB:
2148 case R_IA64_FPTR64MSB:
2149 case R_IA64_FPTR64LSB:
2150 if (bfd_link_pic (info) || h)
2151 need_entry = NEED_FPTR | NEED_DYNREL;
2152 else
2153 need_entry = NEED_FPTR;
2154 dynrel_type = R_IA64_FPTR64LSB;
2155 break;
2157 case R_IA64_LTOFF22:
2158 case R_IA64_LTOFF64I:
2159 need_entry = NEED_GOT;
2160 break;
2162 case R_IA64_LTOFF22X:
2163 need_entry = NEED_GOTX;
2164 break;
2166 case R_IA64_PLTOFF22:
2167 case R_IA64_PLTOFF64I:
2168 case R_IA64_PLTOFF64MSB:
2169 case R_IA64_PLTOFF64LSB:
2170 need_entry = NEED_PLTOFF;
2171 if (h)
2173 if (maybe_dynamic)
2174 need_entry |= NEED_MIN_PLT;
2176 break;
2178 case R_IA64_PCREL21B:
2179 case R_IA64_PCREL60B:
2180 /* Depending on where this symbol is defined, we may or may not
2181 need a full plt entry. Only skip if we know we'll not need
2182 the entry -- static or symbolic, and the symbol definition
2183 has already been seen. */
2184 if (maybe_dynamic && rel->r_addend == 0)
2185 need_entry = NEED_FULL_PLT;
2186 break;
2188 case R_IA64_IMM14:
2189 case R_IA64_IMM22:
2190 case R_IA64_IMM64:
2191 case R_IA64_DIR32MSB:
2192 case R_IA64_DIR32LSB:
2193 case R_IA64_DIR64MSB:
2194 case R_IA64_DIR64LSB:
2195 /* Shared objects will always need at least a REL relocation. */
2196 if (bfd_link_pic (info) || maybe_dynamic)
2197 need_entry = NEED_DYNREL;
2198 dynrel_type = R_IA64_DIR64LSB;
2199 break;
2201 case R_IA64_IPLTMSB:
2202 case R_IA64_IPLTLSB:
2203 break;
2205 case R_IA64_PCREL22:
2206 case R_IA64_PCREL64I:
2207 case R_IA64_PCREL32MSB:
2208 case R_IA64_PCREL32LSB:
2209 case R_IA64_PCREL64MSB:
2210 case R_IA64_PCREL64LSB:
2211 if (maybe_dynamic)
2212 need_entry = NEED_DYNREL;
2213 dynrel_type = R_IA64_PCREL64LSB;
2214 break;
2217 if (!need_entry)
2218 continue;
2220 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, false);
2222 /* Record whether or not this is a local symbol. */
2223 dyn_i->h = h;
2225 /* Create what's needed. */
2226 if (need_entry & (NEED_GOT | NEED_GOTX))
2228 if (!got)
2230 got = get_got (abfd, ia64_info);
2231 if (!got)
2232 return false;
2234 if (need_entry & NEED_GOT)
2235 dyn_i->want_got = 1;
2236 if (need_entry & NEED_GOTX)
2237 dyn_i->want_gotx = 1;
2239 if (need_entry & NEED_FPTR)
2241 /* Create the .opd section. */
2242 if (!fptr)
2244 fptr = get_fptr (abfd, info, ia64_info);
2245 if (!fptr)
2246 return false;
2248 dyn_i->want_fptr = 1;
2250 if (need_entry & NEED_LTOFF_FPTR)
2251 dyn_i->want_ltoff_fptr = 1;
2252 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
2254 if (!ia64_info->root.dynobj)
2255 ia64_info->root.dynobj = abfd;
2256 h->needs_plt = 1;
2257 dyn_i->want_plt = 1;
2259 if (need_entry & NEED_FULL_PLT)
2260 dyn_i->want_plt2 = 1;
2261 if (need_entry & NEED_PLTOFF)
2263 /* This is needed here, in case @pltoff is used in a non-shared
2264 link. */
2265 if (!pltoff)
2267 pltoff = get_pltoff (abfd, ia64_info);
2268 if (!pltoff)
2269 return false;
2272 dyn_i->want_pltoff = 1;
2274 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
2276 if (!srel)
2278 srel = get_reloc_section (abfd, ia64_info, sec, true);
2279 if (!srel)
2280 return false;
2282 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
2283 return false;
2287 return true;
2290 /* For cleanliness, and potentially faster dynamic loading, allocate
2291 external GOT entries first. */
2293 static bool
2294 allocate_global_data_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2295 void * data)
2297 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2299 if ((dyn_i->want_got || dyn_i->want_gotx)
2300 && ! dyn_i->want_fptr
2301 && elf64_ia64_dynamic_symbol_p (dyn_i->h))
2303 /* GOT entry with FPTR is done by allocate_global_fptr_got. */
2304 dyn_i->got_offset = x->ofs;
2305 x->ofs += 8;
2307 return true;
2310 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
2312 static bool
2313 allocate_global_fptr_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2314 void * data)
2316 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2318 if (dyn_i->want_got
2319 && dyn_i->want_fptr
2320 && elf64_ia64_dynamic_symbol_p (dyn_i->h))
2322 dyn_i->got_offset = x->ofs;
2323 x->ofs += 8;
2325 return true;
2328 /* Lastly, allocate all the GOT entries for local data. */
2330 static bool
2331 allocate_local_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2332 void * data)
2334 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *) data;
2336 if ((dyn_i->want_got || dyn_i->want_gotx)
2337 && !elf64_ia64_dynamic_symbol_p (dyn_i->h))
2339 dyn_i->got_offset = x->ofs;
2340 x->ofs += 8;
2342 return true;
2345 /* Allocate function descriptors. We can do these for every function
2346 in a main executable that is not exported. */
2348 static bool
2349 allocate_fptr (struct elf64_ia64_dyn_sym_info *dyn_i, void * data)
2351 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *) data;
2353 if (dyn_i->want_fptr)
2355 struct elf_link_hash_entry *h = dyn_i->h;
2357 if (h)
2358 while (h->root.type == bfd_link_hash_indirect
2359 || h->root.type == bfd_link_hash_warning)
2360 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2362 if (h == NULL || !h->def_dynamic)
2364 /* A non dynamic symbol. */
2365 dyn_i->fptr_offset = x->ofs;
2366 x->ofs += 16;
2368 else
2369 dyn_i->want_fptr = 0;
2371 return true;
2374 /* Allocate all the minimal PLT entries. */
2376 static bool
2377 allocate_plt_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2378 void * data ATTRIBUTE_UNUSED)
2380 if (dyn_i->want_plt)
2382 struct elf_link_hash_entry *h = dyn_i->h;
2384 if (h)
2385 while (h->root.type == bfd_link_hash_indirect
2386 || h->root.type == bfd_link_hash_warning)
2387 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2389 /* ??? Versioned symbols seem to lose NEEDS_PLT. */
2390 if (elf64_ia64_dynamic_symbol_p (h))
2392 dyn_i->want_pltoff = 1;
2394 else
2396 dyn_i->want_plt = 0;
2397 dyn_i->want_plt2 = 0;
2400 return true;
2403 /* Allocate all the full PLT entries. */
2405 static bool
2406 allocate_plt2_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2407 void * data)
2409 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2411 if (dyn_i->want_plt2)
2413 struct elf_link_hash_entry *h = dyn_i->h;
2414 bfd_size_type ofs = x->ofs;
2416 dyn_i->plt2_offset = ofs;
2417 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2419 while (h->root.type == bfd_link_hash_indirect
2420 || h->root.type == bfd_link_hash_warning)
2421 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2422 dyn_i->h->plt.offset = ofs;
2424 return true;
2427 /* Allocate all the PLTOFF entries requested by relocations and
2428 plt entries. We can't share space with allocated FPTR entries,
2429 because the latter are not necessarily addressable by the GP.
2430 ??? Relaxation might be able to determine that they are. */
2432 static bool
2433 allocate_pltoff_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2434 void * data)
2436 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2438 if (dyn_i->want_pltoff)
2440 dyn_i->pltoff_offset = x->ofs;
2441 x->ofs += 16;
2443 return true;
2446 /* Allocate dynamic relocations for those symbols that turned out
2447 to be dynamic. */
2449 static bool
2450 allocate_dynrel_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2451 void * data)
2453 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2454 struct elf64_ia64_link_hash_table *ia64_info;
2455 struct elf64_ia64_dyn_reloc_entry *rent;
2456 bool dynamic_symbol, shared, resolved_zero;
2457 struct elf64_ia64_link_hash_entry *h_ia64;
2459 ia64_info = elf64_ia64_hash_table (x->info);
2460 if (ia64_info == NULL)
2461 return false;
2463 /* Note that this can't be used in relation to FPTR relocs below. */
2464 dynamic_symbol = elf64_ia64_dynamic_symbol_p (dyn_i->h);
2466 shared = bfd_link_pic (x->info);
2467 resolved_zero = (dyn_i->h
2468 && ELF_ST_VISIBILITY (dyn_i->h->other)
2469 && dyn_i->h->root.type == bfd_link_hash_undefweak);
2471 /* Take care of the GOT and PLT relocations. */
2473 if ((!resolved_zero
2474 && (dynamic_symbol || shared)
2475 && (dyn_i->want_got || dyn_i->want_gotx))
2476 || (dyn_i->want_ltoff_fptr
2477 && dyn_i->h
2478 && dyn_i->h->def_dynamic))
2480 /* VMS: FIX64. */
2481 if (dyn_i->h != NULL && dyn_i->h->def_dynamic)
2483 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2484 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2485 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2486 ia64_info->fixups_sec->size +=
2487 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2491 if (ia64_info->rel_fptr_sec && dyn_i->want_fptr)
2493 /* VMS: only image reloc. */
2494 if (dyn_i->h == NULL || dyn_i->h->root.type != bfd_link_hash_undefweak)
2495 ia64_info->rel_fptr_sec->size += sizeof (Elf64_External_Rela);
2498 if (!resolved_zero && dyn_i->want_pltoff)
2500 /* VMS: FIXFD. */
2501 if (dyn_i->h != NULL && dyn_i->h->def_dynamic)
2503 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2504 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2505 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2506 ia64_info->fixups_sec->size +=
2507 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2511 /* Take care of the normal data relocations. */
2513 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2515 int count = rent->count;
2517 switch (rent->type)
2519 case R_IA64_FPTR32LSB:
2520 case R_IA64_FPTR64LSB:
2521 /* Allocate one iff !want_fptr and not PIE, which by this point
2522 will be true only if we're actually allocating one statically
2523 in the main executable. Position independent executables
2524 need a relative reloc. */
2525 if (dyn_i->want_fptr && !bfd_link_pie (x->info))
2526 continue;
2527 break;
2528 case R_IA64_PCREL32LSB:
2529 case R_IA64_PCREL64LSB:
2530 if (!dynamic_symbol)
2531 continue;
2532 break;
2533 case R_IA64_DIR32LSB:
2534 case R_IA64_DIR64LSB:
2535 if (!dynamic_symbol && !shared)
2536 continue;
2537 break;
2538 case R_IA64_IPLTLSB:
2539 if (!dynamic_symbol && !shared)
2540 continue;
2541 /* Use two REL relocations for IPLT relocations
2542 against local symbols. */
2543 if (!dynamic_symbol)
2544 count *= 2;
2545 break;
2546 case R_IA64_DTPREL32LSB:
2547 case R_IA64_TPREL64LSB:
2548 case R_IA64_DTPREL64LSB:
2549 case R_IA64_DTPMOD64LSB:
2550 break;
2551 default:
2552 abort ();
2555 /* Add a fixup. */
2556 if (!dynamic_symbol)
2557 abort ();
2559 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2560 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2561 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2562 ia64_info->fixups_sec->size +=
2563 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2566 return true;
2569 static bool
2570 elf64_ia64_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2571 struct elf_link_hash_entry *h)
2573 /* ??? Undefined symbols with PLT entries should be re-defined
2574 to be the PLT entry. */
2576 /* If this is a weak symbol, and there is a real definition, the
2577 processor independent code will have arranged for us to see the
2578 real definition first, and we can just use the same value. */
2579 if (h->is_weakalias)
2581 struct elf_link_hash_entry *def = weakdef (h);
2582 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2583 h->root.u.def.section = def->root.u.def.section;
2584 h->root.u.def.value = def->root.u.def.value;
2585 return true;
2588 /* If this is a reference to a symbol defined by a dynamic object which
2589 is not a function, we might allocate the symbol in our .dynbss section
2590 and allocate a COPY dynamic relocation.
2592 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2593 of hackery. */
2595 return true;
2598 static bool
2599 elf64_ia64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2600 struct bfd_link_info *info)
2602 struct elf64_ia64_allocate_data data;
2603 struct elf64_ia64_link_hash_table *ia64_info;
2604 asection *sec;
2605 bfd *dynobj;
2606 struct elf_link_hash_table *hash_table;
2608 hash_table = elf_hash_table (info);
2609 dynobj = hash_table->dynobj;
2610 ia64_info = elf64_ia64_hash_table (info);
2611 if (ia64_info == NULL)
2612 return false;
2613 BFD_ASSERT(dynobj != NULL);
2614 data.info = info;
2616 /* Allocate the GOT entries. */
2618 if (ia64_info->root.sgot)
2620 data.ofs = 0;
2621 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2622 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2623 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
2624 ia64_info->root.sgot->size = data.ofs;
2627 /* Allocate the FPTR entries. */
2629 if (ia64_info->fptr_sec)
2631 data.ofs = 0;
2632 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
2633 ia64_info->fptr_sec->size = data.ofs;
2636 /* Now that we've seen all of the input files, we can decide which
2637 symbols need plt entries. Allocate the minimal PLT entries first.
2638 We do this even though dynamic_sections_created may be FALSE, because
2639 this has the side-effect of clearing want_plt and want_plt2. */
2641 data.ofs = 0;
2642 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
2644 /* Align the pointer for the plt2 entries. */
2645 data.ofs = (data.ofs + 31) & (bfd_vma) -32;
2647 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
2648 if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
2650 /* FIXME: we always reserve the memory for dynamic linker even if
2651 there are no PLT entries since dynamic linker may assume the
2652 reserved memory always exists. */
2654 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2656 ia64_info->root.splt->size = data.ofs;
2659 /* Allocate the PLTOFF entries. */
2661 if (ia64_info->pltoff_sec)
2663 data.ofs = 0;
2664 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
2665 ia64_info->pltoff_sec->size = data.ofs;
2668 if (ia64_info->root.dynamic_sections_created)
2670 /* Allocate space for the dynamic relocations that turned out to be
2671 required. */
2672 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
2675 /* We have now determined the sizes of the various dynamic sections.
2676 Allocate memory for them. */
2677 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2679 bool strip;
2681 if (!(sec->flags & SEC_LINKER_CREATED))
2682 continue;
2684 /* If we don't need this section, strip it from the output file.
2685 There were several sections primarily related to dynamic
2686 linking that must be create before the linker maps input
2687 sections to output sections. The linker does that before
2688 bfd_elf_size_dynamic_sections is called, and it is that
2689 function which decides whether anything needs to go into
2690 these sections. */
2692 strip = (sec->size == 0);
2694 if (sec == ia64_info->root.sgot)
2695 strip = false;
2696 else if (sec == ia64_info->root.srelgot)
2698 if (strip)
2699 ia64_info->root.srelgot = NULL;
2700 else
2701 /* We use the reloc_count field as a counter if we need to
2702 copy relocs into the output file. */
2703 sec->reloc_count = 0;
2705 else if (sec == ia64_info->fptr_sec)
2707 if (strip)
2708 ia64_info->fptr_sec = NULL;
2710 else if (sec == ia64_info->rel_fptr_sec)
2712 if (strip)
2713 ia64_info->rel_fptr_sec = NULL;
2714 else
2715 /* We use the reloc_count field as a counter if we need to
2716 copy relocs into the output file. */
2717 sec->reloc_count = 0;
2719 else if (sec == ia64_info->root.splt)
2721 if (strip)
2722 ia64_info->root.splt = NULL;
2724 else if (sec == ia64_info->pltoff_sec)
2726 if (strip)
2727 ia64_info->pltoff_sec = NULL;
2729 else if (sec == ia64_info->fixups_sec)
2731 if (strip)
2732 ia64_info->fixups_sec = NULL;
2734 else if (sec == ia64_info->transfer_sec)
2738 else
2740 const char *name;
2742 /* It's OK to base decisions on the section name, because none
2743 of the dynobj section names depend upon the input files. */
2744 name = bfd_section_name (sec);
2746 if (strcmp (name, ".got.plt") == 0)
2747 strip = false;
2748 else if (startswith (name, ".rel"))
2750 if (!strip)
2752 /* We use the reloc_count field as a counter if we need to
2753 copy relocs into the output file. */
2754 sec->reloc_count = 0;
2757 else
2758 continue;
2761 if (strip)
2762 sec->flags |= SEC_EXCLUDE;
2763 else
2765 /* Allocate memory for the section contents. */
2766 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
2767 if (sec->contents == NULL && sec->size != 0)
2768 return false;
2772 if (elf_hash_table (info)->dynamic_sections_created)
2774 bfd *abfd;
2775 asection *dynsec;
2776 asection *dynstrsec;
2777 Elf_Internal_Dyn dyn;
2778 const struct elf_backend_data *bed;
2779 unsigned int shl_num = 0;
2780 bfd_vma fixups_off = 0;
2781 bfd_vma strdyn_off;
2782 unsigned int time_hi, time_lo;
2784 /* The .dynamic section must exist and be empty. */
2785 dynsec = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
2786 BFD_ASSERT (dynsec != NULL);
2787 BFD_ASSERT (dynsec->size == 0);
2789 dynstrsec = bfd_get_linker_section (hash_table->dynobj, ".vmsdynstr");
2790 BFD_ASSERT (dynstrsec != NULL);
2791 BFD_ASSERT (dynstrsec->size == 0);
2792 dynstrsec->size = 1; /* Initial blank. */
2794 /* Ident + link time. */
2795 vms_get_time (&time_hi, &time_lo);
2797 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_IDENT, 0))
2798 return false;
2799 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_LINKTIME,
2800 (((bfd_uint64_t)time_hi) << 32)
2801 + time_lo))
2802 return false;
2804 /* Strtab. */
2805 strdyn_off = dynsec->size;
2806 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_STRTAB_OFFSET, 0))
2807 return false;
2808 if (!_bfd_elf_add_dynamic_entry (info, DT_STRSZ, 0))
2809 return false;
2811 /* PLTGOT */
2812 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_PLTGOT_SEG, 0))
2813 return false;
2814 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_PLTGOT_OFFSET, 0))
2815 return false;
2817 /* Misc. */
2818 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FPMODE, 0x9800000))
2819 return false;
2820 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_LNKFLAGS,
2821 VMS_LF_IMGSTA | VMS_LF_MAIN))
2822 return false;
2824 /* Add entries for shared libraries. */
2825 for (abfd = info->input_bfds; abfd; abfd = abfd->link.next)
2827 char *soname;
2828 size_t soname_len;
2829 bfd_size_type strindex;
2830 bfd_byte *newcontents;
2831 bfd_vma fixups_shl_off;
2833 if (!(abfd->flags & DYNAMIC))
2834 continue;
2835 BFD_ASSERT (abfd->xvec == output_bfd->xvec);
2837 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_NEEDED_IDENT,
2838 elf_ia64_vms_ident (abfd)))
2839 return false;
2841 soname = vms_get_module_name (bfd_get_filename (abfd), true);
2842 if (soname == NULL)
2843 return false;
2844 strindex = dynstrsec->size;
2845 soname_len = strlen (soname) + 1;
2846 newcontents = (bfd_byte *) bfd_realloc (dynstrsec->contents,
2847 strindex + soname_len);
2848 if (newcontents == NULL)
2849 return false;
2850 memcpy (newcontents + strindex, soname, soname_len);
2851 dynstrsec->size += soname_len;
2852 dynstrsec->contents = newcontents;
2854 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2855 return false;
2857 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FIXUP_NEEDED,
2858 shl_num))
2859 return false;
2860 shl_num++;
2862 /* The fixups_off was in fact containing the size of the fixup
2863 section. Remap into the offset. */
2864 fixups_shl_off = elf_ia64_vms_tdata (abfd)->fixups_off;
2865 elf_ia64_vms_tdata (abfd)->fixups_off = fixups_off;
2867 if (!_bfd_elf_add_dynamic_entry
2868 (info, DT_IA_64_VMS_FIXUP_RELA_CNT,
2869 fixups_shl_off / sizeof (Elf64_External_VMS_IMAGE_FIXUP)))
2870 return false;
2871 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FIXUP_RELA_OFF,
2872 fixups_off))
2873 return false;
2874 fixups_off += fixups_shl_off;
2877 /* Unwind. */
2878 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWINDSZ, 0))
2879 return false;
2880 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_CODSEG, 0))
2881 return false;
2882 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_INFOSEG, 0))
2883 return false;
2884 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_OFFSET, 0))
2885 return false;
2886 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_SEG, 0))
2887 return false;
2889 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0xdead))
2890 return false;
2892 /* Fix the strtab entries. */
2893 bed = get_elf_backend_data (hash_table->dynobj);
2895 if (dynstrsec->size > 1)
2896 dynstrsec->contents[0] = 0;
2897 else
2898 dynstrsec->size = 0;
2900 /* Note: one 'spare' (ie DT_NULL) entry is added by
2901 bfd_elf_size_dynsym_hash_dynstr. */
2902 dyn.d_tag = DT_IA_64_VMS_STRTAB_OFFSET;
2903 dyn.d_un.d_val = dynsec->size /* + sizeof (Elf64_External_Dyn) */;
2904 bed->s->swap_dyn_out (hash_table->dynobj, &dyn,
2905 dynsec->contents + strdyn_off);
2907 dyn.d_tag = DT_STRSZ;
2908 dyn.d_un.d_val = dynstrsec->size;
2909 bed->s->swap_dyn_out (hash_table->dynobj, &dyn,
2910 dynsec->contents + strdyn_off + bed->s->sizeof_dyn);
2912 elf_ia64_vms_tdata (output_bfd)->needed_count = shl_num;
2914 /* Note section. */
2915 if (!create_ia64_vms_notes (output_bfd, info, time_hi, time_lo))
2916 return false;
2919 /* ??? Perhaps force __gp local. */
2921 return true;
2924 static void
2925 elf64_ia64_install_fixup (bfd *output_bfd,
2926 struct elf64_ia64_link_hash_table *ia64_info,
2927 struct elf_link_hash_entry *h,
2928 unsigned int type, asection *sec, bfd_vma offset,
2929 bfd_vma addend)
2931 asection *relsec;
2932 Elf64_External_VMS_IMAGE_FIXUP *fixup;
2933 struct elf64_ia64_link_hash_entry *h_ia64;
2934 bfd_vma fixoff;
2935 Elf_Internal_Phdr *phdr;
2937 if (h == NULL || !h->def_dynamic)
2938 abort ();
2940 h_ia64 = (struct elf64_ia64_link_hash_entry *) h;
2941 fixoff = elf_ia64_vms_tdata (h_ia64->shl)->fixups_off;
2942 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2943 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2944 relsec = ia64_info->fixups_sec;
2946 fixup = (Elf64_External_VMS_IMAGE_FIXUP *)(relsec->contents + fixoff);
2947 offset += sec->output_section->vma + sec->output_offset;
2949 /* FIXME: this is slow. We should cache the last one used, or create a
2950 map. */
2951 phdr = _bfd_elf_find_segment_containing_section
2952 (output_bfd, sec->output_section);
2953 BFD_ASSERT (phdr != NULL);
2955 bfd_putl64 (offset - phdr->p_vaddr, fixup->fixup_offset);
2956 bfd_putl32 (type, fixup->type);
2957 bfd_putl32 (phdr - elf_tdata (output_bfd)->phdr, fixup->fixup_seg);
2958 bfd_putl64 (addend, fixup->addend);
2959 bfd_putl32 (h->root.u.def.value, fixup->symvec_index);
2960 bfd_putl32 (2, fixup->data_type);
2963 /* Store an entry for target address TARGET_ADDR in the linkage table
2964 and return the gp-relative address of the linkage table entry. */
2966 static bfd_vma
2967 set_got_entry (bfd *abfd, struct bfd_link_info *info,
2968 struct elf64_ia64_dyn_sym_info *dyn_i,
2969 bfd_vma addend, bfd_vma value, unsigned int dyn_r_type)
2971 struct elf64_ia64_link_hash_table *ia64_info;
2972 asection *got_sec;
2973 bool done;
2974 bfd_vma got_offset;
2976 ia64_info = elf64_ia64_hash_table (info);
2977 if (ia64_info == NULL)
2978 return 0;
2980 got_sec = ia64_info->root.sgot;
2982 switch (dyn_r_type)
2984 case R_IA64_TPREL64LSB:
2985 case R_IA64_DTPMOD64LSB:
2986 case R_IA64_DTPREL32LSB:
2987 case R_IA64_DTPREL64LSB:
2988 abort ();
2989 break;
2990 default:
2991 done = dyn_i->got_done;
2992 dyn_i->got_done = true;
2993 got_offset = dyn_i->got_offset;
2994 break;
2997 BFD_ASSERT ((got_offset & 7) == 0);
2999 if (! done)
3001 /* Store the target address in the linkage table entry. */
3002 bfd_put_64 (abfd, value, got_sec->contents + got_offset);
3004 /* Install a dynamic relocation if needed. */
3005 if (((bfd_link_pic (info)
3006 && (!dyn_i->h
3007 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3008 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3009 || elf64_ia64_dynamic_symbol_p (dyn_i->h))
3010 && (!dyn_i->want_ltoff_fptr
3011 || !bfd_link_pie (info)
3012 || !dyn_i->h
3013 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3015 if (!dyn_i->h || !dyn_i->h->def_dynamic)
3017 dyn_r_type = R_IA64_REL64LSB;
3018 addend = value;
3021 /* VMS: install a FIX32 or FIX64. */
3022 switch (dyn_r_type)
3024 case R_IA64_DIR32LSB:
3025 case R_IA64_FPTR32LSB:
3026 dyn_r_type = R_IA64_VMS_FIX32;
3027 break;
3028 case R_IA64_DIR64LSB:
3029 case R_IA64_FPTR64LSB:
3030 dyn_r_type = R_IA64_VMS_FIX64;
3031 break;
3032 default:
3033 BFD_ASSERT (false);
3034 break;
3036 elf64_ia64_install_fixup
3037 (info->output_bfd, ia64_info, dyn_i->h,
3038 dyn_r_type, got_sec, got_offset, addend);
3042 /* Return the address of the linkage table entry. */
3043 value = (got_sec->output_section->vma
3044 + got_sec->output_offset
3045 + got_offset);
3047 return value;
3050 /* Fill in a function descriptor consisting of the function's code
3051 address and its global pointer. Return the descriptor's address. */
3053 static bfd_vma
3054 set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
3055 struct elf64_ia64_dyn_sym_info *dyn_i,
3056 bfd_vma value)
3058 struct elf64_ia64_link_hash_table *ia64_info;
3059 asection *fptr_sec;
3061 ia64_info = elf64_ia64_hash_table (info);
3062 if (ia64_info == NULL)
3063 return 0;
3065 fptr_sec = ia64_info->fptr_sec;
3067 if (!dyn_i->fptr_done)
3069 dyn_i->fptr_done = 1;
3071 /* Fill in the function descriptor. */
3072 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
3073 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
3074 fptr_sec->contents + dyn_i->fptr_offset + 8);
3077 /* Return the descriptor's address. */
3078 value = (fptr_sec->output_section->vma
3079 + fptr_sec->output_offset
3080 + dyn_i->fptr_offset);
3082 return value;
3085 /* Fill in a PLTOFF entry consisting of the function's code address
3086 and its global pointer. Return the descriptor's address. */
3088 static bfd_vma
3089 set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
3090 struct elf64_ia64_dyn_sym_info *dyn_i,
3091 bfd_vma value, bool is_plt)
3093 struct elf64_ia64_link_hash_table *ia64_info;
3094 asection *pltoff_sec;
3096 ia64_info = elf64_ia64_hash_table (info);
3097 if (ia64_info == NULL)
3098 return 0;
3100 pltoff_sec = ia64_info->pltoff_sec;
3102 /* Don't do anything if this symbol uses a real PLT entry. In
3103 that case, we'll fill this in during finish_dynamic_symbol. */
3104 if ((! dyn_i->want_plt || is_plt)
3105 && !dyn_i->pltoff_done)
3107 bfd_vma gp = _bfd_get_gp_value (abfd);
3109 /* Fill in the function descriptor. */
3110 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
3111 bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
3113 /* Install dynamic relocations if needed. */
3114 if (!is_plt
3115 && bfd_link_pic (info)
3116 && (!dyn_i->h
3117 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3118 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3120 /* VMS: */
3121 abort ();
3124 dyn_i->pltoff_done = 1;
3127 /* Return the descriptor's address. */
3128 value = (pltoff_sec->output_section->vma
3129 + pltoff_sec->output_offset
3130 + dyn_i->pltoff_offset);
3132 return value;
3135 /* Called through qsort to sort the .IA_64.unwind section during a
3136 non-relocatable link. Set elf64_ia64_unwind_entry_compare_bfd
3137 to the output bfd so we can do proper endianness frobbing. */
3139 static bfd *elf64_ia64_unwind_entry_compare_bfd;
3141 static int
3142 elf64_ia64_unwind_entry_compare (const void * a, const void * b)
3144 bfd_vma av, bv;
3146 av = bfd_get_64 (elf64_ia64_unwind_entry_compare_bfd, a);
3147 bv = bfd_get_64 (elf64_ia64_unwind_entry_compare_bfd, b);
3149 return (av < bv ? -1 : av > bv ? 1 : 0);
3152 /* Make sure we've got ourselves a nice fat __gp value. */
3153 static bool
3154 elf64_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info, bool final)
3156 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3157 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3158 struct elf_link_hash_entry *gp;
3159 bfd_vma gp_val;
3160 asection *os;
3161 struct elf64_ia64_link_hash_table *ia64_info;
3163 ia64_info = elf64_ia64_hash_table (info);
3164 if (ia64_info == NULL)
3165 return false;
3167 /* Find the min and max vma of all sections marked short. Also collect
3168 min and max vma of any type, for use in selecting a nice gp. */
3169 for (os = abfd->sections; os ; os = os->next)
3171 bfd_vma lo, hi;
3173 if ((os->flags & SEC_ALLOC) == 0)
3174 continue;
3176 lo = os->vma;
3177 /* When this function is called from elfNN_ia64_final_link
3178 the correct value to use is os->size. When called from
3179 elfNN_ia64_relax_section we are in the middle of section
3180 sizing; some sections will already have os->size set, others
3181 will have os->size zero and os->rawsize the previous size. */
3182 hi = os->vma + (!final && os->rawsize ? os->rawsize : os->size);
3183 if (hi < lo)
3184 hi = (bfd_vma) -1;
3186 if (min_vma > lo)
3187 min_vma = lo;
3188 if (max_vma < hi)
3189 max_vma = hi;
3190 if (os->flags & SEC_SMALL_DATA)
3192 if (min_short_vma > lo)
3193 min_short_vma = lo;
3194 if (max_short_vma < hi)
3195 max_short_vma = hi;
3199 if (ia64_info->min_short_sec)
3201 if (min_short_vma
3202 > (ia64_info->min_short_sec->vma
3203 + ia64_info->min_short_offset))
3204 min_short_vma = (ia64_info->min_short_sec->vma
3205 + ia64_info->min_short_offset);
3206 if (max_short_vma
3207 < (ia64_info->max_short_sec->vma
3208 + ia64_info->max_short_offset))
3209 max_short_vma = (ia64_info->max_short_sec->vma
3210 + ia64_info->max_short_offset);
3213 /* See if the user wants to force a value. */
3214 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3215 false, false);
3217 if (gp
3218 && (gp->root.type == bfd_link_hash_defined
3219 || gp->root.type == bfd_link_hash_defweak))
3221 asection *gp_sec = gp->root.u.def.section;
3222 gp_val = (gp->root.u.def.value
3223 + gp_sec->output_section->vma
3224 + gp_sec->output_offset);
3226 else
3228 /* Pick a sensible value. */
3230 if (ia64_info->min_short_sec)
3232 bfd_vma short_range = max_short_vma - min_short_vma;
3234 /* If min_short_sec is set, pick one in the middle bewteen
3235 min_short_vma and max_short_vma. */
3236 if (short_range >= 0x400000)
3237 goto overflow;
3238 gp_val = min_short_vma + short_range / 2;
3240 else
3242 asection *got_sec = ia64_info->root.sgot;
3244 /* Start with just the address of the .got. */
3245 if (got_sec)
3246 gp_val = got_sec->output_section->vma;
3247 else if (max_short_vma != 0)
3248 gp_val = min_short_vma;
3249 else if (max_vma - min_vma < 0x200000)
3250 gp_val = min_vma;
3251 else
3252 gp_val = max_vma - 0x200000 + 8;
3255 /* If it is possible to address the entire image, but we
3256 don't with the choice above, adjust. */
3257 if (max_vma - min_vma < 0x400000
3258 && (max_vma - gp_val >= 0x200000
3259 || gp_val - min_vma > 0x200000))
3260 gp_val = min_vma + 0x200000;
3261 else if (max_short_vma != 0)
3263 /* If we don't cover all the short data, adjust. */
3264 if (max_short_vma - gp_val >= 0x200000)
3265 gp_val = min_short_vma + 0x200000;
3267 /* If we're addressing stuff past the end, adjust back. */
3268 if (gp_val > max_vma)
3269 gp_val = max_vma - 0x200000 + 8;
3273 /* Validate whether all SHF_IA_64_SHORT sections are within
3274 range of the chosen GP. */
3276 if (max_short_vma != 0)
3278 if (max_short_vma - min_short_vma >= 0x400000)
3280 overflow:
3281 _bfd_error_handler
3282 /* xgettext:c-format */
3283 (_("%pB: short data segment overflowed (%#" PRIx64 " >= 0x400000)"),
3284 abfd, (uint64_t) (max_short_vma - min_short_vma));
3285 return false;
3287 else if ((gp_val > min_short_vma
3288 && gp_val - min_short_vma > 0x200000)
3289 || (gp_val < max_short_vma
3290 && max_short_vma - gp_val >= 0x200000))
3292 _bfd_error_handler
3293 (_("%pB: __gp does not cover short data segment"), abfd);
3294 return false;
3298 _bfd_set_gp_value (abfd, gp_val);
3300 return true;
3303 static bool
3304 elf64_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
3306 struct elf64_ia64_link_hash_table *ia64_info;
3307 asection *unwind_output_sec;
3309 ia64_info = elf64_ia64_hash_table (info);
3310 if (ia64_info == NULL)
3311 return false;
3313 /* Make sure we've got ourselves a nice fat __gp value. */
3314 if (!bfd_link_relocatable (info))
3316 bfd_vma gp_val;
3317 struct elf_link_hash_entry *gp;
3319 /* We assume after gp is set, section size will only decrease. We
3320 need to adjust gp for it. */
3321 _bfd_set_gp_value (abfd, 0);
3322 if (! elf64_ia64_choose_gp (abfd, info, true))
3323 return false;
3324 gp_val = _bfd_get_gp_value (abfd);
3326 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3327 false, false);
3328 if (gp)
3330 gp->root.type = bfd_link_hash_defined;
3331 gp->root.u.def.value = gp_val;
3332 gp->root.u.def.section = bfd_abs_section_ptr;
3336 /* If we're producing a final executable, we need to sort the contents
3337 of the .IA_64.unwind section. Force this section to be relocated
3338 into memory rather than written immediately to the output file. */
3339 unwind_output_sec = NULL;
3340 if (!bfd_link_relocatable (info))
3342 asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3343 if (s)
3345 unwind_output_sec = s->output_section;
3346 unwind_output_sec->contents
3347 = bfd_malloc (unwind_output_sec->size);
3348 if (unwind_output_sec->contents == NULL)
3349 return false;
3353 /* Invoke the regular ELF backend linker to do all the work. */
3354 if (!bfd_elf_final_link (abfd, info))
3355 return false;
3357 if (unwind_output_sec)
3359 elf64_ia64_unwind_entry_compare_bfd = abfd;
3360 qsort (unwind_output_sec->contents,
3361 (size_t) (unwind_output_sec->size / 24),
3363 elf64_ia64_unwind_entry_compare);
3365 if (! bfd_set_section_contents (abfd, unwind_output_sec,
3366 unwind_output_sec->contents, (bfd_vma) 0,
3367 unwind_output_sec->size))
3368 return false;
3371 return true;
3374 static int
3375 elf64_ia64_relocate_section (bfd *output_bfd,
3376 struct bfd_link_info *info,
3377 bfd *input_bfd,
3378 asection *input_section,
3379 bfd_byte *contents,
3380 Elf_Internal_Rela *relocs,
3381 Elf_Internal_Sym *local_syms,
3382 asection **local_sections)
3384 struct elf64_ia64_link_hash_table *ia64_info;
3385 Elf_Internal_Shdr *symtab_hdr;
3386 Elf_Internal_Rela *rel;
3387 Elf_Internal_Rela *relend;
3388 bool ret_val = true; /* for non-fatal errors */
3389 bfd_vma gp_val;
3391 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3392 ia64_info = elf64_ia64_hash_table (info);
3393 if (ia64_info == NULL)
3394 return false;
3396 /* Infect various flags from the input section to the output section. */
3397 if (bfd_link_relocatable (info))
3399 bfd_vma flags;
3401 flags = elf_section_data(input_section)->this_hdr.sh_flags;
3402 flags &= SHF_IA_64_NORECOV;
3404 elf_section_data(input_section->output_section)
3405 ->this_hdr.sh_flags |= flags;
3408 gp_val = _bfd_get_gp_value (output_bfd);
3410 rel = relocs;
3411 relend = relocs + input_section->reloc_count;
3412 for (; rel < relend; ++rel)
3414 struct elf_link_hash_entry *h;
3415 struct elf64_ia64_dyn_sym_info *dyn_i;
3416 bfd_reloc_status_type r;
3417 reloc_howto_type *howto;
3418 unsigned long r_symndx;
3419 Elf_Internal_Sym *sym;
3420 unsigned int r_type;
3421 bfd_vma value;
3422 asection *sym_sec;
3423 bfd_byte *hit_addr;
3424 bool dynamic_symbol_p;
3425 bool undef_weak_ref;
3427 r_type = ELF64_R_TYPE (rel->r_info);
3428 if (r_type > R_IA64_MAX_RELOC_CODE)
3430 /* xgettext:c-format */
3431 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3432 input_bfd, (int) r_type);
3433 bfd_set_error (bfd_error_bad_value);
3434 ret_val = false;
3435 continue;
3438 howto = ia64_elf_lookup_howto (r_type);
3439 if (howto == NULL)
3441 ret_val = false;
3442 continue;
3444 r_symndx = ELF64_R_SYM (rel->r_info);
3445 h = NULL;
3446 sym = NULL;
3447 sym_sec = NULL;
3448 undef_weak_ref = false;
3450 if (r_symndx < symtab_hdr->sh_info)
3452 /* Reloc against local symbol. */
3453 asection *msec;
3454 sym = local_syms + r_symndx;
3455 sym_sec = local_sections[r_symndx];
3456 msec = sym_sec;
3457 value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
3458 if (!bfd_link_relocatable (info)
3459 && (sym_sec->flags & SEC_MERGE) != 0
3460 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
3461 && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3463 struct elf64_ia64_local_hash_entry *loc_h;
3465 loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, false);
3466 if (loc_h && ! loc_h->sec_merge_done)
3468 struct elf64_ia64_dyn_sym_info *dynent;
3469 unsigned int count;
3471 for (count = loc_h->count, dynent = loc_h->info;
3472 count != 0;
3473 count--, dynent++)
3475 msec = sym_sec;
3476 dynent->addend =
3477 _bfd_merged_section_offset (output_bfd, &msec,
3478 elf_section_data (msec)->
3479 sec_info,
3480 sym->st_value
3481 + dynent->addend);
3482 dynent->addend -= sym->st_value;
3483 dynent->addend += msec->output_section->vma
3484 + msec->output_offset
3485 - sym_sec->output_section->vma
3486 - sym_sec->output_offset;
3489 /* We may have introduced duplicated entries. We need
3490 to remove them properly. */
3491 count = sort_dyn_sym_info (loc_h->info, loc_h->count);
3492 if (count != loc_h->count)
3494 loc_h->count = count;
3495 loc_h->sorted_count = count;
3498 loc_h->sec_merge_done = 1;
3502 else
3504 bool unresolved_reloc;
3505 bool warned, ignored;
3506 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3508 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3509 r_symndx, symtab_hdr, sym_hashes,
3510 h, sym_sec, value,
3511 unresolved_reloc, warned, ignored);
3513 if (h->root.type == bfd_link_hash_undefweak)
3514 undef_weak_ref = true;
3515 else if (warned)
3516 continue;
3519 /* For relocs against symbols from removed linkonce sections,
3520 or sections discarded by a linker script, we just want the
3521 section contents zeroed. Avoid any special processing. */
3522 if (sym_sec != NULL && discarded_section (sym_sec))
3523 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3524 rel, 1, relend, howto, 0, contents);
3526 if (bfd_link_relocatable (info))
3527 continue;
3529 hit_addr = contents + rel->r_offset;
3530 value += rel->r_addend;
3531 dynamic_symbol_p = elf64_ia64_dynamic_symbol_p (h);
3533 switch (r_type)
3535 case R_IA64_NONE:
3536 case R_IA64_LDXMOV:
3537 continue;
3539 case R_IA64_IMM14:
3540 case R_IA64_IMM22:
3541 case R_IA64_IMM64:
3542 case R_IA64_DIR32MSB:
3543 case R_IA64_DIR32LSB:
3544 case R_IA64_DIR64MSB:
3545 case R_IA64_DIR64LSB:
3546 /* Install a dynamic relocation for this reloc. */
3547 if ((dynamic_symbol_p || bfd_link_pic (info))
3548 && r_symndx != 0
3549 && (input_section->flags & SEC_ALLOC) != 0)
3551 unsigned int dyn_r_type;
3552 bfd_vma addend;
3554 switch (r_type)
3556 case R_IA64_IMM14:
3557 case R_IA64_IMM22:
3558 case R_IA64_IMM64:
3559 /* ??? People shouldn't be doing non-pic code in
3560 shared libraries nor dynamic executables. */
3561 _bfd_error_handler
3562 /* xgettext:c-format */
3563 (_("%pB: non-pic code with imm relocation against"
3564 " dynamic symbol `%s'"),
3565 input_bfd,
3566 h ? h->root.root.string
3567 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3568 sym_sec));
3569 ret_val = false;
3570 continue;
3572 default:
3573 break;
3576 /* If we don't need dynamic symbol lookup, find a
3577 matching RELATIVE relocation. */
3578 dyn_r_type = r_type;
3579 if (dynamic_symbol_p)
3581 addend = rel->r_addend;
3582 value = 0;
3584 else
3586 addend = value;
3589 /* VMS: install a FIX64. */
3590 switch (dyn_r_type)
3592 case R_IA64_DIR32LSB:
3593 dyn_r_type = R_IA64_VMS_FIX32;
3594 break;
3595 case R_IA64_DIR64LSB:
3596 dyn_r_type = R_IA64_VMS_FIX64;
3597 break;
3598 default:
3599 BFD_ASSERT (false);
3600 break;
3602 elf64_ia64_install_fixup
3603 (output_bfd, ia64_info, h,
3604 dyn_r_type, input_section, rel->r_offset, addend);
3605 r = bfd_reloc_ok;
3606 break;
3608 /* Fall through. */
3610 case R_IA64_LTV32MSB:
3611 case R_IA64_LTV32LSB:
3612 case R_IA64_LTV64MSB:
3613 case R_IA64_LTV64LSB:
3614 r = ia64_elf_install_value (hit_addr, value, r_type);
3615 break;
3617 case R_IA64_GPREL22:
3618 case R_IA64_GPREL64I:
3619 case R_IA64_GPREL32MSB:
3620 case R_IA64_GPREL32LSB:
3621 case R_IA64_GPREL64MSB:
3622 case R_IA64_GPREL64LSB:
3623 if (dynamic_symbol_p)
3625 _bfd_error_handler
3626 /* xgettext:c-format */
3627 (_("%pB: @gprel relocation against dynamic symbol %s"),
3628 input_bfd,
3629 h ? h->root.root.string
3630 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3631 sym_sec));
3632 ret_val = false;
3633 continue;
3635 value -= gp_val;
3636 r = ia64_elf_install_value (hit_addr, value, r_type);
3637 break;
3639 case R_IA64_LTOFF22:
3640 case R_IA64_LTOFF22X:
3641 case R_IA64_LTOFF64I:
3642 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3643 value = set_got_entry (input_bfd, info, dyn_i,
3644 rel->r_addend, value, R_IA64_DIR64LSB);
3645 value -= gp_val;
3646 r = ia64_elf_install_value (hit_addr, value, r_type);
3647 break;
3649 case R_IA64_PLTOFF22:
3650 case R_IA64_PLTOFF64I:
3651 case R_IA64_PLTOFF64MSB:
3652 case R_IA64_PLTOFF64LSB:
3653 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3654 value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
3655 value -= gp_val;
3656 r = ia64_elf_install_value (hit_addr, value, r_type);
3657 break;
3659 case R_IA64_FPTR64I:
3660 case R_IA64_FPTR32MSB:
3661 case R_IA64_FPTR32LSB:
3662 case R_IA64_FPTR64MSB:
3663 case R_IA64_FPTR64LSB:
3664 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3665 if (dyn_i->want_fptr)
3667 if (!undef_weak_ref)
3668 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3670 if (!dyn_i->want_fptr || bfd_link_pie (info))
3672 /* Otherwise, we expect the dynamic linker to create
3673 the entry. */
3675 if (dyn_i->want_fptr)
3677 if (r_type == R_IA64_FPTR64I)
3679 /* We can't represent this without a dynamic symbol.
3680 Adjust the relocation to be against an output
3681 section symbol, which are always present in the
3682 dynamic symbol table. */
3683 /* ??? People shouldn't be doing non-pic code in
3684 shared libraries. Hork. */
3685 _bfd_error_handler
3686 (_("%pB: linking non-pic code in a position independent executable"),
3687 input_bfd);
3688 ret_val = false;
3689 continue;
3692 else
3694 value = 0;
3697 /* VMS: FIXFD. */
3698 elf64_ia64_install_fixup
3699 (output_bfd, ia64_info, h, R_IA64_VMS_FIXFD,
3700 input_section, rel->r_offset, 0);
3701 r = bfd_reloc_ok;
3702 break;
3705 r = ia64_elf_install_value (hit_addr, value, r_type);
3706 break;
3708 case R_IA64_LTOFF_FPTR22:
3709 case R_IA64_LTOFF_FPTR64I:
3710 case R_IA64_LTOFF_FPTR32MSB:
3711 case R_IA64_LTOFF_FPTR32LSB:
3712 case R_IA64_LTOFF_FPTR64MSB:
3713 case R_IA64_LTOFF_FPTR64LSB:
3714 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
3715 if (dyn_i->want_fptr)
3717 BFD_ASSERT (h == NULL || !h->def_dynamic);
3718 if (!undef_weak_ref)
3719 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3721 else
3722 value = 0;
3724 value = set_got_entry (output_bfd, info, dyn_i,
3725 rel->r_addend, value, R_IA64_FPTR64LSB);
3726 value -= gp_val;
3727 r = ia64_elf_install_value (hit_addr, value, r_type);
3728 break;
3730 case R_IA64_PCREL32MSB:
3731 case R_IA64_PCREL32LSB:
3732 case R_IA64_PCREL64MSB:
3733 case R_IA64_PCREL64LSB:
3734 /* Install a dynamic relocation for this reloc. */
3735 if (dynamic_symbol_p && r_symndx != 0)
3737 /* VMS: doesn't exist ??? */
3738 abort ();
3740 goto finish_pcrel;
3742 case R_IA64_PCREL21B:
3743 case R_IA64_PCREL60B:
3744 /* We should have created a PLT entry for any dynamic symbol. */
3745 dyn_i = NULL;
3746 if (h)
3747 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
3749 if (dyn_i && dyn_i->want_plt2)
3751 /* Should have caught this earlier. */
3752 BFD_ASSERT (rel->r_addend == 0);
3754 value = (ia64_info->root.splt->output_section->vma
3755 + ia64_info->root.splt->output_offset
3756 + dyn_i->plt2_offset);
3758 else
3760 /* Since there's no PLT entry, Validate that this is
3761 locally defined. */
3762 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
3764 /* If the symbol is undef_weak, we shouldn't be trying
3765 to call it. There's every chance that we'd wind up
3766 with an out-of-range fixup here. Don't bother setting
3767 any value at all. */
3768 if (undef_weak_ref)
3769 continue;
3771 goto finish_pcrel;
3773 case R_IA64_PCREL21BI:
3774 case R_IA64_PCREL21F:
3775 case R_IA64_PCREL21M:
3776 case R_IA64_PCREL22:
3777 case R_IA64_PCREL64I:
3778 /* The PCREL21BI reloc is specifically not intended for use with
3779 dynamic relocs. PCREL21F and PCREL21M are used for speculation
3780 fixup code, and thus probably ought not be dynamic. The
3781 PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs. */
3782 if (dynamic_symbol_p)
3784 const char *msg;
3786 if (r_type == R_IA64_PCREL21BI)
3787 /* xgettext:c-format */
3788 msg = _("%pB: @internal branch to dynamic symbol %s");
3789 else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
3790 /* xgettext:c-format */
3791 msg = _("%pB: speculation fixup to dynamic symbol %s");
3792 else
3793 /* xgettext:c-format */
3794 msg = _("%pB: @pcrel relocation against dynamic symbol %s");
3795 _bfd_error_handler (msg, input_bfd,
3796 h ? h->root.root.string
3797 : bfd_elf_sym_name (input_bfd,
3798 symtab_hdr,
3799 sym,
3800 sym_sec));
3801 ret_val = false;
3802 continue;
3804 goto finish_pcrel;
3806 finish_pcrel:
3807 /* Make pc-relative. */
3808 value -= (input_section->output_section->vma
3809 + input_section->output_offset
3810 + rel->r_offset) & ~ (bfd_vma) 0x3;
3811 r = ia64_elf_install_value (hit_addr, value, r_type);
3812 break;
3814 case R_IA64_SEGREL32MSB:
3815 case R_IA64_SEGREL32LSB:
3816 case R_IA64_SEGREL64MSB:
3817 case R_IA64_SEGREL64LSB:
3819 /* Find the segment that contains the output_section. */
3820 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
3821 (output_bfd, sym_sec->output_section);
3823 if (p == NULL)
3825 r = bfd_reloc_notsupported;
3827 else
3829 /* The VMA of the segment is the vaddr of the associated
3830 program header. */
3831 if (value > p->p_vaddr)
3832 value -= p->p_vaddr;
3833 else
3834 value = 0;
3835 r = ia64_elf_install_value (hit_addr, value, r_type);
3837 break;
3840 case R_IA64_SECREL32MSB:
3841 case R_IA64_SECREL32LSB:
3842 case R_IA64_SECREL64MSB:
3843 case R_IA64_SECREL64LSB:
3844 /* Make output-section relative to section where the symbol
3845 is defined. PR 475 */
3846 if (sym_sec)
3847 value -= sym_sec->output_section->vma;
3848 r = ia64_elf_install_value (hit_addr, value, r_type);
3849 break;
3851 case R_IA64_IPLTMSB:
3852 case R_IA64_IPLTLSB:
3853 /* Install a dynamic relocation for this reloc. */
3854 if ((dynamic_symbol_p || bfd_link_pic (info))
3855 && (input_section->flags & SEC_ALLOC) != 0)
3857 /* VMS: FIXFD ?? */
3858 abort ();
3861 if (r_type == R_IA64_IPLTMSB)
3862 r_type = R_IA64_DIR64MSB;
3863 else
3864 r_type = R_IA64_DIR64LSB;
3865 ia64_elf_install_value (hit_addr, value, r_type);
3866 r = ia64_elf_install_value (hit_addr + 8, gp_val, r_type);
3867 break;
3869 case R_IA64_TPREL14:
3870 case R_IA64_TPREL22:
3871 case R_IA64_TPREL64I:
3872 r = bfd_reloc_notsupported;
3873 break;
3875 case R_IA64_DTPREL14:
3876 case R_IA64_DTPREL22:
3877 case R_IA64_DTPREL64I:
3878 case R_IA64_DTPREL32LSB:
3879 case R_IA64_DTPREL32MSB:
3880 case R_IA64_DTPREL64LSB:
3881 case R_IA64_DTPREL64MSB:
3882 r = bfd_reloc_notsupported;
3883 break;
3885 case R_IA64_LTOFF_TPREL22:
3886 case R_IA64_LTOFF_DTPMOD22:
3887 case R_IA64_LTOFF_DTPREL22:
3888 r = bfd_reloc_notsupported;
3889 break;
3891 default:
3892 r = bfd_reloc_notsupported;
3893 break;
3896 switch (r)
3898 case bfd_reloc_ok:
3899 break;
3901 case bfd_reloc_undefined:
3902 /* This can happen for global table relative relocs if
3903 __gp is undefined. This is a panic situation so we
3904 don't try to continue. */
3905 (*info->callbacks->undefined_symbol)
3906 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
3907 return false;
3909 case bfd_reloc_notsupported:
3911 const char *name;
3913 if (h)
3914 name = h->root.root.string;
3915 else
3916 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3917 sym_sec);
3918 (*info->callbacks->warning) (info, _("unsupported reloc"),
3919 name, input_bfd,
3920 input_section, rel->r_offset);
3921 ret_val = false;
3923 break;
3925 case bfd_reloc_dangerous:
3926 case bfd_reloc_outofrange:
3927 case bfd_reloc_overflow:
3928 default:
3930 const char *name;
3932 if (h)
3933 name = h->root.root.string;
3934 else
3935 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3936 sym_sec);
3938 switch (r_type)
3940 case R_IA64_TPREL14:
3941 case R_IA64_TPREL22:
3942 case R_IA64_TPREL64I:
3943 case R_IA64_DTPREL14:
3944 case R_IA64_DTPREL22:
3945 case R_IA64_DTPREL64I:
3946 case R_IA64_DTPREL32LSB:
3947 case R_IA64_DTPREL32MSB:
3948 case R_IA64_DTPREL64LSB:
3949 case R_IA64_DTPREL64MSB:
3950 case R_IA64_LTOFF_TPREL22:
3951 case R_IA64_LTOFF_DTPMOD22:
3952 case R_IA64_LTOFF_DTPREL22:
3953 _bfd_error_handler
3954 /* xgettext:c-format */
3955 (_("%pB: missing TLS section for relocation %s against `%s'"
3956 " at %#" PRIx64 " in section `%pA'."),
3957 input_bfd, howto->name, name,
3958 (uint64_t) rel->r_offset, input_section);
3959 break;
3961 case R_IA64_PCREL21B:
3962 case R_IA64_PCREL21BI:
3963 case R_IA64_PCREL21M:
3964 case R_IA64_PCREL21F:
3965 if (is_elf_hash_table (info->hash))
3967 /* Relaxtion is always performed for ELF output.
3968 Overflow failures for those relocations mean
3969 that the section is too big to relax. */
3970 _bfd_error_handler
3971 /* xgettext:c-format */
3972 (_("%pB: Can't relax br (%s) to `%s' "
3973 "at %#" PRIx64 " in section `%pA' "
3974 "with size %#" PRIx64 " (> 0x1000000)."),
3975 input_bfd, howto->name, name, (uint64_t) rel->r_offset,
3976 input_section, (uint64_t) input_section->size);
3977 break;
3979 /* Fall through. */
3980 default:
3981 (*info->callbacks->reloc_overflow) (info,
3982 &h->root,
3983 name,
3984 howto->name,
3985 (bfd_vma) 0,
3986 input_bfd,
3987 input_section,
3988 rel->r_offset);
3989 break;
3992 ret_val = false;
3994 break;
3998 return ret_val;
4001 static bool
4002 elf64_ia64_finish_dynamic_symbol (bfd *output_bfd,
4003 struct bfd_link_info *info,
4004 struct elf_link_hash_entry *h,
4005 Elf_Internal_Sym *sym)
4007 struct elf64_ia64_link_hash_table *ia64_info;
4008 struct elf64_ia64_dyn_sym_info *dyn_i;
4010 ia64_info = elf64_ia64_hash_table (info);
4011 if (ia64_info == NULL)
4012 return false;
4014 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
4016 /* Fill in the PLT data, if required. */
4017 if (dyn_i && dyn_i->want_plt)
4019 bfd_byte *loc;
4020 asection *plt_sec;
4021 bfd_vma plt_addr, pltoff_addr, gp_val;
4023 gp_val = _bfd_get_gp_value (output_bfd);
4025 plt_sec = ia64_info->root.splt;
4026 plt_addr = 0; /* Not used as overriden by FIXUPs. */
4027 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
4029 /* Initialize the FULL PLT entry, if needed. */
4030 if (dyn_i->want_plt2)
4032 loc = plt_sec->contents + dyn_i->plt2_offset;
4034 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
4035 ia64_elf_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
4037 /* Mark the symbol as undefined, rather than as defined in the
4038 plt section. Leave the value alone. */
4039 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
4040 first place. But perhaps elflink.c did some for us. */
4041 if (!h->def_regular)
4042 sym->st_shndx = SHN_UNDEF;
4045 /* VMS: FIXFD. */
4046 elf64_ia64_install_fixup
4047 (output_bfd, ia64_info, h, R_IA64_VMS_FIXFD, ia64_info->pltoff_sec,
4048 pltoff_addr - (ia64_info->pltoff_sec->output_section->vma
4049 + ia64_info->pltoff_sec->output_offset), 0);
4052 /* Mark some specially defined symbols as absolute. */
4053 if (h == ia64_info->root.hdynamic
4054 || h == ia64_info->root.hgot
4055 || h == ia64_info->root.hplt)
4056 sym->st_shndx = SHN_ABS;
4058 return true;
4061 static bool
4062 elf64_ia64_finish_dynamic_sections (bfd *abfd,
4063 struct bfd_link_info *info)
4065 struct elf64_ia64_link_hash_table *ia64_info;
4066 bfd *dynobj;
4068 ia64_info = elf64_ia64_hash_table (info);
4069 if (ia64_info == NULL)
4070 return false;
4072 dynobj = ia64_info->root.dynobj;
4074 if (elf_hash_table (info)->dynamic_sections_created)
4076 Elf64_External_Dyn *dyncon, *dynconend;
4077 asection *sdyn;
4078 asection *unwind_sec;
4079 bfd_vma gp_val;
4080 unsigned int gp_seg;
4081 bfd_vma gp_off;
4082 Elf_Internal_Phdr *phdr;
4083 Elf_Internal_Phdr *base_phdr;
4084 unsigned int unwind_seg = 0;
4085 unsigned int code_seg = 0;
4087 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4088 BFD_ASSERT (sdyn != NULL);
4089 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4090 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4092 gp_val = _bfd_get_gp_value (abfd);
4093 phdr = _bfd_elf_find_segment_containing_section
4094 (info->output_bfd, ia64_info->pltoff_sec->output_section);
4095 BFD_ASSERT (phdr != NULL);
4096 base_phdr = elf_tdata (info->output_bfd)->phdr;
4097 gp_seg = phdr - base_phdr;
4098 gp_off = gp_val - phdr->p_vaddr;
4100 unwind_sec = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
4101 if (unwind_sec != NULL)
4103 asection *code_sec;
4105 phdr = _bfd_elf_find_segment_containing_section (abfd, unwind_sec);
4106 BFD_ASSERT (phdr != NULL);
4107 unwind_seg = phdr - base_phdr;
4109 code_sec = bfd_get_section_by_name (abfd, "$CODE$");
4110 phdr = _bfd_elf_find_segment_containing_section (abfd, code_sec);
4111 BFD_ASSERT (phdr != NULL);
4112 code_seg = phdr - base_phdr;
4115 for (; dyncon < dynconend; dyncon++)
4117 Elf_Internal_Dyn dyn;
4119 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4121 switch (dyn.d_tag)
4123 case DT_IA_64_VMS_FIXUP_RELA_OFF:
4124 dyn.d_un.d_val +=
4125 (ia64_info->fixups_sec->output_section->vma
4126 + ia64_info->fixups_sec->output_offset)
4127 - (sdyn->output_section->vma + sdyn->output_offset);
4128 break;
4130 case DT_IA_64_VMS_PLTGOT_OFFSET:
4131 dyn.d_un.d_val = gp_off;
4132 break;
4134 case DT_IA_64_VMS_PLTGOT_SEG:
4135 dyn.d_un.d_val = gp_seg;
4136 break;
4138 case DT_IA_64_VMS_UNWINDSZ:
4139 if (unwind_sec == NULL)
4141 dyn.d_tag = DT_NULL;
4142 dyn.d_un.d_val = 0xdead;
4144 else
4145 dyn.d_un.d_val = unwind_sec->size;
4146 break;
4148 case DT_IA_64_VMS_UNWIND_CODSEG:
4149 dyn.d_un.d_val = code_seg;
4150 break;
4152 case DT_IA_64_VMS_UNWIND_INFOSEG:
4153 case DT_IA_64_VMS_UNWIND_SEG:
4154 dyn.d_un.d_val = unwind_seg;
4155 break;
4157 case DT_IA_64_VMS_UNWIND_OFFSET:
4158 break;
4160 default:
4161 /* No need to rewrite the entry. */
4162 continue;
4165 bfd_elf64_swap_dyn_out (abfd, &dyn, dyncon);
4169 /* Handle transfer addresses. */
4171 asection *tfr_sec = ia64_info->transfer_sec;
4172 struct elf64_vms_transfer *tfr;
4173 struct elf_link_hash_entry *tfr3;
4175 tfr = (struct elf64_vms_transfer *)tfr_sec->contents;
4176 bfd_putl32 (6 * 8, tfr->size);
4177 bfd_putl64 (tfr_sec->output_section->vma
4178 + tfr_sec->output_offset
4179 + 6 * 8, tfr->tfradr3);
4181 tfr3 = elf_link_hash_lookup (elf_hash_table (info), "ELF$TFRADR", false,
4182 false, false);
4184 if (tfr3
4185 && (tfr3->root.type == bfd_link_hash_defined
4186 || tfr3->root.type == bfd_link_hash_defweak))
4188 asection *tfr3_sec = tfr3->root.u.def.section;
4189 bfd_vma tfr3_val;
4191 tfr3_val = (tfr3->root.u.def.value
4192 + tfr3_sec->output_section->vma
4193 + tfr3_sec->output_offset);
4195 bfd_putl64 (tfr3_val, tfr->tfr3_func);
4196 bfd_putl64 (_bfd_get_gp_value (info->output_bfd), tfr->tfr3_gp);
4199 /* FIXME: set linker flags,
4200 handle lib$initialize. */
4203 return true;
4206 /* ELF file flag handling: */
4208 /* Function to keep IA-64 specific file flags. */
4209 static bool
4210 elf64_ia64_set_private_flags (bfd *abfd, flagword flags)
4212 BFD_ASSERT (!elf_flags_init (abfd)
4213 || elf_elfheader (abfd)->e_flags == flags);
4215 elf_elfheader (abfd)->e_flags = flags;
4216 elf_flags_init (abfd) = true;
4217 return true;
4220 /* Merge backend specific data from an object file to the output
4221 object file when linking. */
4222 static bool
4223 elf64_ia64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4225 bfd *obfd = info->output_bfd;
4226 flagword out_flags;
4227 flagword in_flags;
4228 bool ok = true;
4230 /* FIXME: What should be checked when linking shared libraries? */
4231 if ((ibfd->flags & DYNAMIC) != 0)
4232 return true;
4234 /* Don't even pretend to support mixed-format linking. */
4235 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4236 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4237 return false;
4239 in_flags = elf_elfheader (ibfd)->e_flags;
4240 out_flags = elf_elfheader (obfd)->e_flags;
4242 if (! elf_flags_init (obfd))
4244 elf_flags_init (obfd) = true;
4245 elf_elfheader (obfd)->e_flags = in_flags;
4247 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4248 && bfd_get_arch_info (obfd)->the_default)
4250 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4251 bfd_get_mach (ibfd));
4254 return true;
4257 /* Check flag compatibility. */
4258 if (in_flags == out_flags)
4259 return true;
4261 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
4262 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4263 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4265 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4267 _bfd_error_handler
4268 (_("%pB: linking trap-on-NULL-dereference with non-trapping files"),
4269 ibfd);
4271 bfd_set_error (bfd_error_bad_value);
4272 ok = false;
4274 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4276 _bfd_error_handler
4277 (_("%pB: linking big-endian files with little-endian files"),
4278 ibfd);
4280 bfd_set_error (bfd_error_bad_value);
4281 ok = false;
4283 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4285 _bfd_error_handler
4286 (_("%pB: linking 64-bit files with 32-bit files"),
4287 ibfd);
4289 bfd_set_error (bfd_error_bad_value);
4290 ok = false;
4292 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4294 _bfd_error_handler
4295 (_("%pB: linking constant-gp files with non-constant-gp files"),
4296 ibfd);
4298 bfd_set_error (bfd_error_bad_value);
4299 ok = false;
4301 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4302 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4304 _bfd_error_handler
4305 (_("%pB: linking auto-pic files with non-auto-pic files"),
4306 ibfd);
4308 bfd_set_error (bfd_error_bad_value);
4309 ok = false;
4312 return ok;
4315 static bool
4316 elf64_ia64_print_private_bfd_data (bfd *abfd, void * ptr)
4318 FILE *file = (FILE *) ptr;
4319 flagword flags = elf_elfheader (abfd)->e_flags;
4321 BFD_ASSERT (abfd != NULL && ptr != NULL);
4323 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4324 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4325 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4326 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4327 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4328 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4329 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4330 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4331 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4333 _bfd_elf_print_private_bfd_data (abfd, ptr);
4334 return true;
4337 static enum elf_reloc_type_class
4338 elf64_ia64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4339 const asection *rel_sec ATTRIBUTE_UNUSED,
4340 const Elf_Internal_Rela *rela)
4342 switch ((int) ELF64_R_TYPE (rela->r_info))
4344 case R_IA64_REL32MSB:
4345 case R_IA64_REL32LSB:
4346 case R_IA64_REL64MSB:
4347 case R_IA64_REL64LSB:
4348 return reloc_class_relative;
4349 case R_IA64_IPLTMSB:
4350 case R_IA64_IPLTLSB:
4351 return reloc_class_plt;
4352 case R_IA64_COPY:
4353 return reloc_class_copy;
4354 default:
4355 return reloc_class_normal;
4359 static const struct bfd_elf_special_section elf64_ia64_special_sections[] =
4361 { STRING_COMMA_LEN (".sbss"), -1, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4362 { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4363 { NULL, 0, 0, 0, 0 }
4366 static bool
4367 elf64_ia64_object_p (bfd *abfd)
4369 asection *sec;
4370 asection *group, *unwi, *unw;
4371 flagword flags;
4372 const char *name;
4373 char *unwi_name, *unw_name;
4374 size_t amt;
4376 if (abfd->flags & DYNAMIC)
4377 return true;
4379 /* Flags for fake group section. */
4380 flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
4381 | SEC_EXCLUDE);
4383 /* We add a fake section group for each .gnu.linkonce.t.* section,
4384 which isn't in a section group, and its unwind sections. */
4385 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4387 if (elf_sec_group (sec) == NULL
4388 && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
4389 == (SEC_LINK_ONCE | SEC_CODE))
4390 && startswith (sec->name, ".gnu.linkonce.t."))
4392 name = sec->name + 16;
4394 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
4395 unwi_name = bfd_alloc (abfd, amt);
4396 if (!unwi_name)
4397 return false;
4399 strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
4400 unwi = bfd_get_section_by_name (abfd, unwi_name);
4402 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
4403 unw_name = bfd_alloc (abfd, amt);
4404 if (!unw_name)
4405 return false;
4407 strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
4408 unw = bfd_get_section_by_name (abfd, unw_name);
4410 /* We need to create a fake group section for it and its
4411 unwind sections. */
4412 group = bfd_make_section_anyway_with_flags (abfd, name,
4413 flags);
4414 if (group == NULL)
4415 return false;
4417 /* Move the fake group section to the beginning. */
4418 bfd_section_list_remove (abfd, group);
4419 bfd_section_list_prepend (abfd, group);
4421 elf_next_in_group (group) = sec;
4423 elf_group_name (sec) = name;
4424 elf_next_in_group (sec) = sec;
4425 elf_sec_group (sec) = group;
4427 if (unwi)
4429 elf_group_name (unwi) = name;
4430 elf_next_in_group (unwi) = sec;
4431 elf_next_in_group (sec) = unwi;
4432 elf_sec_group (unwi) = group;
4435 if (unw)
4437 elf_group_name (unw) = name;
4438 if (unwi)
4440 elf_next_in_group (unw) = elf_next_in_group (unwi);
4441 elf_next_in_group (unwi) = unw;
4443 else
4445 elf_next_in_group (unw) = sec;
4446 elf_next_in_group (sec) = unw;
4448 elf_sec_group (unw) = group;
4451 /* Fake SHT_GROUP section header. */
4452 elf_section_data (group)->this_hdr.bfd_section = group;
4453 elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
4456 return true;
4459 /* Handle an IA-64 specific section when reading an object file. This
4460 is called when bfd_section_from_shdr finds a section with an unknown
4461 type. */
4463 static bool
4464 elf64_vms_section_from_shdr (bfd *abfd,
4465 Elf_Internal_Shdr *hdr,
4466 const char *name,
4467 int shindex)
4469 flagword secflags = 0;
4471 switch (hdr->sh_type)
4473 case SHT_IA_64_VMS_TRACE:
4474 case SHT_IA_64_VMS_DEBUG:
4475 case SHT_IA_64_VMS_DEBUG_STR:
4476 secflags = SEC_DEBUGGING;
4477 break;
4479 case SHT_IA_64_UNWIND:
4480 case SHT_IA_64_HP_OPT_ANOT:
4481 break;
4483 case SHT_IA_64_EXT:
4484 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
4485 return false;
4486 break;
4488 default:
4489 return false;
4492 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4493 return false;
4495 if (secflags != 0)
4497 asection *newsect = hdr->bfd_section;
4499 if (!bfd_set_section_flags (newsect,
4500 bfd_section_flags (newsect) | secflags))
4501 return false;
4504 return true;
4507 static bool
4508 elf64_vms_object_p (bfd *abfd)
4510 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4511 Elf_Internal_Phdr *i_phdr = elf_tdata (abfd)->phdr;
4512 unsigned int i;
4513 unsigned int num_text = 0;
4514 unsigned int num_data = 0;
4515 unsigned int num_rodata = 0;
4516 char name[16];
4518 if (!elf64_ia64_object_p (abfd))
4519 return false;
4521 /* Many VMS compilers do not generate sections for the corresponding
4522 segment. This is boring as binutils tools won't be able to disassemble
4523 the code. So we simply create all the missing sections. */
4524 for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
4526 /* Is there a section for this segment? */
4527 bfd_vma base_vma = i_phdr->p_vaddr;
4528 bfd_vma limit_vma = base_vma + i_phdr->p_filesz;
4530 if (i_phdr->p_type != PT_LOAD)
4531 continue;
4533 /* We need to cover from base_vms to limit_vma. */
4534 again:
4535 while (base_vma < limit_vma)
4537 bfd_vma next_vma = limit_vma;
4538 asection *nsec;
4539 asection *sec;
4540 flagword flags;
4541 char *nname = NULL;
4543 /* Find a section covering [base_vma;limit_vma) */
4544 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4546 /* Skip uninteresting sections (either not in memory or
4547 below base_vma. */
4548 if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == 0
4549 || sec->vma + sec->size <= base_vma)
4550 continue;
4551 if (sec->vma <= base_vma)
4553 /* This section covers (maybe partially) the beginning
4554 of the range. */
4555 base_vma = sec->vma + sec->size;
4556 goto again;
4558 if (sec->vma < next_vma)
4560 /* This section partially covers the end of the range.
4561 Used to compute the size of the hole. */
4562 next_vma = sec->vma;
4566 /* No section covering [base_vma; next_vma). Create a fake one. */
4567 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
4568 if (i_phdr->p_flags & PF_X)
4570 flags |= SEC_CODE;
4571 if (num_text++ == 0)
4572 nname = ".text";
4573 else
4574 sprintf (name, ".text$%u", num_text);
4576 else if ((i_phdr->p_flags & (PF_R | PF_W)) == PF_R)
4578 flags |= SEC_READONLY;
4579 sprintf (name, ".rodata$%u", num_rodata++);
4581 else
4583 flags |= SEC_DATA;
4584 sprintf (name, ".data$%u", num_data++);
4587 /* Allocate name. */
4588 if (nname == NULL)
4590 size_t name_len = strlen (name) + 1;
4591 nname = bfd_alloc (abfd, name_len);
4592 if (nname == NULL)
4593 return false;
4594 memcpy (nname, name, name_len);
4597 /* Create and fill new section. */
4598 nsec = bfd_make_section_anyway_with_flags (abfd, nname, flags);
4599 if (nsec == NULL)
4600 return false;
4601 nsec->vma = base_vma;
4602 nsec->size = next_vma - base_vma;
4603 nsec->filepos = i_phdr->p_offset + (base_vma - i_phdr->p_vaddr);
4605 base_vma = next_vma;
4608 return true;
4611 static bool
4612 elf64_vms_init_file_header (bfd *abfd, struct bfd_link_info *info)
4614 Elf_Internal_Ehdr *i_ehdrp;
4616 if (!_bfd_elf_init_file_header (abfd, info))
4617 return false;
4619 i_ehdrp = elf_elfheader (abfd);
4620 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_OPENVMS;
4621 i_ehdrp->e_ident[EI_ABIVERSION] = 2;
4622 return true;
4625 static bool
4626 elf64_vms_section_processing (bfd *abfd ATTRIBUTE_UNUSED,
4627 Elf_Internal_Shdr *hdr)
4629 if (hdr->bfd_section != NULL)
4631 const char *name = bfd_section_name (hdr->bfd_section);
4633 if (strcmp (name, ".text") == 0)
4634 hdr->sh_flags |= SHF_IA_64_VMS_SHARED;
4635 else if ((strcmp (name, ".debug") == 0)
4636 || (strcmp (name, ".debug_abbrev") == 0)
4637 || (strcmp (name, ".debug_aranges") == 0)
4638 || (strcmp (name, ".debug_frame") == 0)
4639 || (strcmp (name, ".debug_info") == 0)
4640 || (strcmp (name, ".debug_loc") == 0)
4641 || (strcmp (name, ".debug_macinfo") == 0)
4642 || (strcmp (name, ".debug_pubnames") == 0)
4643 || (strcmp (name, ".debug_pubtypes") == 0))
4644 hdr->sh_type = SHT_IA_64_VMS_DEBUG;
4645 else if ((strcmp (name, ".debug_line") == 0)
4646 || (strcmp (name, ".debug_ranges") == 0)
4647 || (strcmp (name, ".trace_info") == 0)
4648 || (strcmp (name, ".trace_abbrev") == 0)
4649 || (strcmp (name, ".trace_aranges") == 0))
4650 hdr->sh_type = SHT_IA_64_VMS_TRACE;
4651 else if (strcmp (name, ".debug_str") == 0)
4652 hdr->sh_type = SHT_IA_64_VMS_DEBUG_STR;
4655 return true;
4658 /* The final processing done just before writing out a VMS IA-64 ELF
4659 object file. */
4661 static bool
4662 elf64_vms_final_write_processing (bfd *abfd)
4664 Elf_Internal_Shdr *hdr;
4665 asection *s;
4666 int unwind_info_sect_idx = 0;
4668 for (s = abfd->sections; s; s = s->next)
4670 hdr = &elf_section_data (s)->this_hdr;
4672 if (strcmp (bfd_section_name (hdr->bfd_section),
4673 ".IA_64.unwind_info") == 0)
4674 unwind_info_sect_idx = elf_section_data (s)->this_idx;
4676 switch (hdr->sh_type)
4678 case SHT_IA_64_UNWIND:
4679 /* VMS requires sh_info to point to the unwind info section. */
4680 hdr->sh_info = unwind_info_sect_idx;
4681 break;
4685 if (! elf_flags_init (abfd))
4687 unsigned long flags = 0;
4689 if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
4690 flags |= EF_IA_64_BE;
4691 if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
4692 flags |= EF_IA_64_ABI64;
4694 elf_elfheader (abfd)->e_flags = flags;
4695 elf_flags_init (abfd) = true;
4697 return _bfd_elf_final_write_processing (abfd);
4700 static bool
4701 elf64_vms_write_shdrs_and_ehdr (bfd *abfd)
4703 unsigned char needed_count[8];
4705 if (!bfd_elf64_write_shdrs_and_ehdr (abfd))
4706 return false;
4708 bfd_putl64 (elf_ia64_vms_tdata (abfd)->needed_count, needed_count);
4710 if (bfd_seek (abfd, sizeof (Elf64_External_Ehdr), SEEK_SET) != 0
4711 || bfd_bwrite (needed_count, 8, abfd) != 8)
4712 return false;
4714 return true;
4717 static bool
4718 elf64_vms_close_and_cleanup (bfd *abfd)
4720 if (bfd_get_format (abfd) == bfd_object)
4722 long isize;
4724 /* Pad to 8 byte boundary for IPF/VMS. */
4725 isize = bfd_get_size (abfd);
4726 if ((isize & 7) != 0)
4728 int ishort = 8 - (isize & 7);
4729 bfd_uint64_t pad = 0;
4731 bfd_seek (abfd, isize, SEEK_SET);
4732 bfd_bwrite (&pad, ishort, abfd);
4736 return _bfd_elf_close_and_cleanup (abfd);
4739 /* Add symbols from an ELF object file to the linker hash table. */
4741 static bool
4742 elf64_vms_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4744 Elf_Internal_Shdr *hdr;
4745 bfd_size_type symcount;
4746 bfd_size_type extsymcount;
4747 bfd_size_type extsymoff;
4748 struct elf_link_hash_entry **sym_hash;
4749 bool dynamic;
4750 Elf_Internal_Sym *isymbuf = NULL;
4751 Elf_Internal_Sym *isym;
4752 Elf_Internal_Sym *isymend;
4753 const struct elf_backend_data *bed;
4754 struct elf_link_hash_table *htab;
4755 bfd_size_type amt;
4757 htab = elf_hash_table (info);
4758 bed = get_elf_backend_data (abfd);
4760 if ((abfd->flags & DYNAMIC) == 0)
4761 dynamic = false;
4762 else
4764 dynamic = true;
4766 /* You can't use -r against a dynamic object. Also, there's no
4767 hope of using a dynamic object which does not exactly match
4768 the format of the output file. */
4769 if (bfd_link_relocatable (info)
4770 || !is_elf_hash_table (&htab->root)
4771 || info->output_bfd->xvec != abfd->xvec)
4773 if (bfd_link_relocatable (info))
4774 bfd_set_error (bfd_error_invalid_operation);
4775 else
4776 bfd_set_error (bfd_error_wrong_format);
4777 goto error_return;
4781 if (! dynamic)
4783 /* If we are creating a shared library, create all the dynamic
4784 sections immediately. We need to attach them to something,
4785 so we attach them to this BFD, provided it is the right
4786 format. FIXME: If there are no input BFD's of the same
4787 format as the output, we can't make a shared library. */
4788 if (bfd_link_pic (info)
4789 && is_elf_hash_table (&htab->root)
4790 && info->output_bfd->xvec == abfd->xvec
4791 && !htab->dynamic_sections_created)
4793 if (! elf64_ia64_create_dynamic_sections (abfd, info))
4794 goto error_return;
4797 else if (!is_elf_hash_table (&htab->root))
4798 goto error_return;
4799 else
4801 asection *s;
4802 bfd_byte *dynbuf;
4803 bfd_byte *extdyn;
4805 /* ld --just-symbols and dynamic objects don't mix very well.
4806 ld shouldn't allow it. */
4807 if ((s = abfd->sections) != NULL
4808 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4809 abort ();
4811 /* Be sure there are dynamic sections. */
4812 if (! elf64_ia64_create_dynamic_sections (htab->dynobj, info))
4813 goto error_return;
4815 s = bfd_get_section_by_name (abfd, ".dynamic");
4816 if (s == NULL)
4818 /* VMS libraries do not have dynamic sections. Create one from
4819 the segment. */
4820 Elf_Internal_Phdr *phdr;
4821 unsigned int i, phnum;
4823 phdr = elf_tdata (abfd)->phdr;
4824 if (phdr == NULL)
4825 goto error_return;
4826 phnum = elf_elfheader (abfd)->e_phnum;
4827 for (i = 0; i < phnum; phdr++)
4828 if (phdr->p_type == PT_DYNAMIC)
4830 s = bfd_make_section (abfd, ".dynamic");
4831 if (s == NULL)
4832 goto error_return;
4833 s->vma = phdr->p_vaddr;
4834 s->lma = phdr->p_paddr;
4835 s->size = phdr->p_filesz;
4836 s->filepos = phdr->p_offset;
4837 s->flags |= SEC_HAS_CONTENTS;
4838 s->alignment_power = bfd_log2 (phdr->p_align);
4839 break;
4841 if (s == NULL)
4842 goto error_return;
4845 /* Extract IDENT. */
4846 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4848 error_free_dyn:
4849 free (dynbuf);
4850 goto error_return;
4853 for (extdyn = dynbuf;
4854 extdyn < dynbuf + s->size;
4855 extdyn += bed->s->sizeof_dyn)
4857 Elf_Internal_Dyn dyn;
4859 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4860 if (dyn.d_tag == DT_IA_64_VMS_IDENT)
4862 bfd_uint64_t tagv = dyn.d_un.d_val;
4863 elf_ia64_vms_ident (abfd) = tagv;
4864 break;
4867 if (extdyn >= dynbuf + s->size)
4869 /* Ident not found. */
4870 goto error_free_dyn;
4872 free (dynbuf);
4874 /* We do not want to include any of the sections in a dynamic
4875 object in the output file. We hack by simply clobbering the
4876 list of sections in the BFD. This could be handled more
4877 cleanly by, say, a new section flag; the existing
4878 SEC_NEVER_LOAD flag is not the one we want, because that one
4879 still implies that the section takes up space in the output
4880 file. */
4881 bfd_section_list_clear (abfd);
4883 /* FIXME: should we detect if this library is already included ?
4884 This should be harmless and shouldn't happen in practice. */
4887 hdr = &elf_tdata (abfd)->symtab_hdr;
4888 symcount = hdr->sh_size / bed->s->sizeof_sym;
4890 /* The sh_info field of the symtab header tells us where the
4891 external symbols start. We don't care about the local symbols at
4892 this point. */
4893 extsymcount = symcount - hdr->sh_info;
4894 extsymoff = hdr->sh_info;
4896 sym_hash = NULL;
4897 if (extsymcount != 0)
4899 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4900 NULL, NULL, NULL);
4901 if (isymbuf == NULL)
4902 goto error_return;
4904 /* We store a pointer to the hash table entry for each external
4905 symbol. */
4906 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4907 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4908 if (sym_hash == NULL)
4909 goto error_free_sym;
4910 elf_sym_hashes (abfd) = sym_hash;
4913 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4914 isym < isymend;
4915 isym++, sym_hash++)
4917 int bind;
4918 bfd_vma value;
4919 asection *sec, *new_sec;
4920 flagword flags;
4921 const char *name;
4922 struct elf_link_hash_entry *h;
4923 bool definition;
4924 bool size_change_ok;
4925 bool type_change_ok;
4926 bool common;
4927 unsigned int old_alignment;
4928 bfd *old_bfd;
4930 flags = BSF_NO_FLAGS;
4931 sec = NULL;
4932 value = isym->st_value;
4933 *sym_hash = NULL;
4934 common = bed->common_definition (isym);
4936 bind = ELF_ST_BIND (isym->st_info);
4937 switch (bind)
4939 case STB_LOCAL:
4940 /* This should be impossible, since ELF requires that all
4941 global symbols follow all local symbols, and that sh_info
4942 point to the first global symbol. Unfortunately, Irix 5
4943 screws this up. */
4944 continue;
4946 case STB_GLOBAL:
4947 if (isym->st_shndx != SHN_UNDEF && !common)
4948 flags = BSF_GLOBAL;
4949 break;
4951 case STB_WEAK:
4952 flags = BSF_WEAK;
4953 break;
4955 case STB_GNU_UNIQUE:
4956 flags = BSF_GNU_UNIQUE;
4957 break;
4959 default:
4960 /* Leave it up to the processor backend. */
4961 break;
4964 if (isym->st_shndx == SHN_UNDEF)
4965 sec = bfd_und_section_ptr;
4966 else if (isym->st_shndx == SHN_ABS)
4967 sec = bfd_abs_section_ptr;
4968 else if (isym->st_shndx == SHN_COMMON)
4970 sec = bfd_com_section_ptr;
4971 /* What ELF calls the size we call the value. What ELF
4972 calls the value we call the alignment. */
4973 value = isym->st_size;
4975 else
4977 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4978 if (sec == NULL)
4979 sec = bfd_abs_section_ptr;
4980 else if (sec->kept_section)
4982 /* Symbols from discarded section are undefined. We keep
4983 its visibility. */
4984 sec = bfd_und_section_ptr;
4985 isym->st_shndx = SHN_UNDEF;
4987 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4988 value -= sec->vma;
4991 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4992 isym->st_name);
4993 if (name == NULL)
4994 goto error_free_vers;
4996 if (bed->elf_add_symbol_hook)
4998 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4999 &sec, &value))
5000 goto error_free_vers;
5002 /* The hook function sets the name to NULL if this symbol
5003 should be skipped for some reason. */
5004 if (name == NULL)
5005 continue;
5008 /* Sanity check that all possibilities were handled. */
5009 if (sec == NULL)
5011 bfd_set_error (bfd_error_bad_value);
5012 goto error_free_vers;
5015 if (bfd_is_und_section (sec)
5016 || bfd_is_com_section (sec))
5017 definition = false;
5018 else
5019 definition = true;
5021 size_change_ok = false;
5022 type_change_ok = bed->type_change_ok;
5023 old_alignment = 0;
5024 old_bfd = NULL;
5025 new_sec = sec;
5027 if (! bfd_is_und_section (sec))
5028 h = elf_link_hash_lookup (htab, name, true, false, false);
5029 else
5030 h = ((struct elf_link_hash_entry *) bfd_wrapped_link_hash_lookup
5031 (abfd, info, name, true, false, false));
5032 if (h == NULL)
5033 goto error_free_sym;
5035 *sym_hash = h;
5037 if (is_elf_hash_table (&htab->root))
5039 while (h->root.type == bfd_link_hash_indirect
5040 || h->root.type == bfd_link_hash_warning)
5041 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5043 /* Remember the old alignment if this is a common symbol, so
5044 that we don't reduce the alignment later on. We can't
5045 check later, because _bfd_generic_link_add_one_symbol
5046 will set a default for the alignment which we want to
5047 override. We also remember the old bfd where the existing
5048 definition comes from. */
5049 switch (h->root.type)
5051 default:
5052 break;
5054 case bfd_link_hash_defined:
5055 if (abfd->selective_search)
5056 continue;
5057 /* Fall-through. */
5058 case bfd_link_hash_defweak:
5059 old_bfd = h->root.u.def.section->owner;
5060 break;
5062 case bfd_link_hash_common:
5063 old_bfd = h->root.u.c.p->section->owner;
5064 old_alignment = h->root.u.c.p->alignment_power;
5065 break;
5069 if (! (_bfd_generic_link_add_one_symbol
5070 (info, abfd, name, flags, sec, value, NULL, false, bed->collect,
5071 (struct bfd_link_hash_entry **) sym_hash)))
5072 goto error_free_vers;
5074 h = *sym_hash;
5075 while (h->root.type == bfd_link_hash_indirect
5076 || h->root.type == bfd_link_hash_warning)
5077 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5079 *sym_hash = h;
5080 if (definition)
5081 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5083 /* Set the alignment of a common symbol. */
5084 if ((common || bfd_is_com_section (sec))
5085 && h->root.type == bfd_link_hash_common)
5087 unsigned int align;
5089 if (common)
5090 align = bfd_log2 (isym->st_value);
5091 else
5093 /* The new symbol is a common symbol in a shared object.
5094 We need to get the alignment from the section. */
5095 align = new_sec->alignment_power;
5097 if (align > old_alignment
5098 /* Permit an alignment power of zero if an alignment of one
5099 is specified and no other alignments have been specified. */
5100 || (isym->st_value == 1 && old_alignment == 0))
5101 h->root.u.c.p->alignment_power = align;
5102 else
5103 h->root.u.c.p->alignment_power = old_alignment;
5106 if (is_elf_hash_table (&htab->root))
5108 /* Check the alignment when a common symbol is involved. This
5109 can change when a common symbol is overridden by a normal
5110 definition or a common symbol is ignored due to the old
5111 normal definition. We need to make sure the maximum
5112 alignment is maintained. */
5113 if ((old_alignment || common)
5114 && h->root.type != bfd_link_hash_common)
5116 unsigned int common_align;
5117 unsigned int normal_align;
5118 unsigned int symbol_align;
5119 bfd *normal_bfd;
5120 bfd *common_bfd;
5122 symbol_align = ffs (h->root.u.def.value) - 1;
5123 if (h->root.u.def.section->owner != NULL
5124 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
5126 normal_align = h->root.u.def.section->alignment_power;
5127 if (normal_align > symbol_align)
5128 normal_align = symbol_align;
5130 else
5131 normal_align = symbol_align;
5133 if (old_alignment)
5135 common_align = old_alignment;
5136 common_bfd = old_bfd;
5137 normal_bfd = abfd;
5139 else
5141 common_align = bfd_log2 (isym->st_value);
5142 common_bfd = abfd;
5143 normal_bfd = old_bfd;
5146 if (normal_align < common_align)
5148 /* PR binutils/2735 */
5149 if (normal_bfd == NULL)
5150 _bfd_error_handler
5151 /* xgettext:c-format */
5152 (_("warning: alignment %u of common symbol `%s' in %pB"
5153 " is greater than the alignment (%u) of its section %pA"),
5154 1 << common_align, name, common_bfd,
5155 1 << normal_align, h->root.u.def.section);
5156 else
5157 _bfd_error_handler
5158 /* xgettext:c-format */
5159 (_("warning: alignment %u of symbol `%s' in %pB"
5160 " is smaller than %u in %pB"),
5161 1 << normal_align, name, normal_bfd,
5162 1 << common_align, common_bfd);
5166 /* Remember the symbol size if it isn't undefined. */
5167 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
5168 && (definition || h->size == 0))
5170 if (h->size != 0
5171 && h->size != isym->st_size
5172 && ! size_change_ok)
5173 _bfd_error_handler
5174 /* xgettext:c-format */
5175 (_("warning: size of symbol `%s' changed"
5176 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5177 name, (uint64_t) h->size, old_bfd,
5178 (uint64_t) isym->st_size, abfd);
5180 h->size = isym->st_size;
5183 /* If this is a common symbol, then we always want H->SIZE
5184 to be the size of the common symbol. The code just above
5185 won't fix the size if a common symbol becomes larger. We
5186 don't warn about a size change here, because that is
5187 covered by --warn-common. Allow changed between different
5188 function types. */
5189 if (h->root.type == bfd_link_hash_common)
5190 h->size = h->root.u.c.size;
5192 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5193 && (definition || h->type == STT_NOTYPE))
5195 unsigned int type = ELF_ST_TYPE (isym->st_info);
5197 if (h->type != type)
5199 if (h->type != STT_NOTYPE && ! type_change_ok)
5200 _bfd_error_handler
5201 /* xgettext:c-format */
5202 (_("warning: type of symbol `%s' changed"
5203 " from %d to %d in %pB"),
5204 name, h->type, type, abfd);
5206 h->type = type;
5210 /* Set a flag in the hash table entry indicating the type of
5211 reference or definition we just found. Keep a count of
5212 the number of dynamic symbols we find. A dynamic symbol
5213 is one which is referenced or defined by both a regular
5214 object and a shared object. */
5215 if (! dynamic)
5217 if (! definition)
5219 h->ref_regular = 1;
5220 if (bind != STB_WEAK)
5221 h->ref_regular_nonweak = 1;
5223 else
5225 BFD_ASSERT (!h->def_dynamic);
5226 h->def_regular = 1;
5229 else
5231 BFD_ASSERT (definition);
5232 h->def_dynamic = 1;
5233 h->dynindx = -2;
5234 ((struct elf64_ia64_link_hash_entry *)h)->shl = abfd;
5239 free (isymbuf);
5240 isymbuf = NULL;
5242 /* If this object is the same format as the output object, and it is
5243 not a shared library, then let the backend look through the
5244 relocs.
5246 This is required to build global offset table entries and to
5247 arrange for dynamic relocs. It is not required for the
5248 particular common case of linking non PIC code, even when linking
5249 against shared libraries, but unfortunately there is no way of
5250 knowing whether an object file has been compiled PIC or not.
5251 Looking through the relocs is not particularly time consuming.
5252 The problem is that we must either (1) keep the relocs in memory,
5253 which causes the linker to require additional runtime memory or
5254 (2) read the relocs twice from the input file, which wastes time.
5255 This would be a good case for using mmap.
5257 I have no idea how to handle linking PIC code into a file of a
5258 different format. It probably can't be done. */
5259 if (! dynamic
5260 && is_elf_hash_table (&htab->root)
5261 && bed->check_relocs != NULL
5262 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
5264 asection *o;
5266 for (o = abfd->sections; o != NULL; o = o->next)
5268 Elf_Internal_Rela *internal_relocs;
5269 bool ok;
5271 if ((o->flags & SEC_RELOC) == 0
5272 || o->reloc_count == 0
5273 || ((info->strip == strip_all || info->strip == strip_debugger)
5274 && (o->flags & SEC_DEBUGGING) != 0)
5275 || bfd_is_abs_section (o->output_section))
5276 continue;
5278 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
5279 info->keep_memory);
5280 if (internal_relocs == NULL)
5281 goto error_return;
5283 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
5285 if (elf_section_data (o)->relocs != internal_relocs)
5286 free (internal_relocs);
5288 if (! ok)
5289 goto error_return;
5293 return true;
5295 error_free_vers:
5296 error_free_sym:
5297 free (isymbuf);
5298 error_return:
5299 return false;
5302 static bool
5303 elf64_vms_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5305 int pass;
5306 struct bfd_link_hash_entry **pundef;
5307 struct bfd_link_hash_entry **next_pundef;
5309 /* We only accept VMS libraries. */
5310 if (info->output_bfd->xvec != abfd->xvec)
5312 bfd_set_error (bfd_error_wrong_format);
5313 return false;
5316 /* The archive_pass field in the archive itself is used to
5317 initialize PASS, since we may search the same archive multiple
5318 times. */
5319 pass = ++abfd->archive_pass;
5321 /* Look through the list of undefined symbols. */
5322 for (pundef = &info->hash->undefs; *pundef != NULL; pundef = next_pundef)
5324 struct bfd_link_hash_entry *h;
5325 symindex symidx;
5326 bfd *element;
5327 bfd *orig_element;
5329 h = *pundef;
5330 next_pundef = &(*pundef)->u.undef.next;
5332 /* When a symbol is defined, it is not necessarily removed from
5333 the list. */
5334 if (h->type != bfd_link_hash_undefined
5335 && h->type != bfd_link_hash_common)
5337 /* Remove this entry from the list, for general cleanliness
5338 and because we are going to look through the list again
5339 if we search any more libraries. We can't remove the
5340 entry if it is the tail, because that would lose any
5341 entries we add to the list later on. */
5342 if (*pundef != info->hash->undefs_tail)
5344 *pundef = *next_pundef;
5345 next_pundef = pundef;
5347 continue;
5350 /* Look for this symbol in the archive hash table. */
5351 symidx = _bfd_vms_lib_find_symbol (abfd, h->root.string);
5352 if (symidx == BFD_NO_MORE_SYMBOLS)
5354 /* Nothing in this slot. */
5355 continue;
5358 element = bfd_get_elt_at_index (abfd, symidx);
5359 if (element == NULL)
5360 return false;
5362 if (element->archive_pass == -1 || element->archive_pass == pass)
5364 /* Next symbol if this archive is wrong or already handled. */
5365 continue;
5368 orig_element = element;
5369 if (bfd_is_thin_archive (abfd))
5371 element = _bfd_vms_lib_get_imagelib_file (element);
5372 if (element == NULL || !bfd_check_format (element, bfd_object))
5374 orig_element->archive_pass = -1;
5375 return false;
5378 else if (! bfd_check_format (element, bfd_object))
5380 element->archive_pass = -1;
5381 return false;
5384 /* Unlike the generic linker, we know that this element provides
5385 a definition for an undefined symbol and we know that we want
5386 to include it. We don't need to check anything. */
5387 if (! (*info->callbacks->add_archive_element) (info, element,
5388 h->root.string, &element))
5389 continue;
5390 if (! elf64_vms_link_add_object_symbols (element, info))
5391 return false;
5393 orig_element->archive_pass = pass;
5396 return true;
5399 static bool
5400 elf64_vms_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5402 switch (bfd_get_format (abfd))
5404 case bfd_object:
5405 return elf64_vms_link_add_object_symbols (abfd, info);
5406 break;
5407 case bfd_archive:
5408 return elf64_vms_link_add_archive_symbols (abfd, info);
5409 break;
5410 default:
5411 bfd_set_error (bfd_error_wrong_format);
5412 return false;
5416 static bool
5417 elf64_ia64_vms_mkobject (bfd *abfd)
5419 return bfd_elf_allocate_object
5420 (abfd, sizeof (struct elf64_ia64_vms_obj_tdata), IA64_ELF_DATA);
5424 /* Size-dependent data and functions. */
5425 static const struct elf_size_info elf64_ia64_vms_size_info = {
5426 sizeof (Elf64_External_VMS_Ehdr),
5427 sizeof (Elf64_External_Phdr),
5428 sizeof (Elf64_External_Shdr),
5429 sizeof (Elf64_External_Rel),
5430 sizeof (Elf64_External_Rela),
5431 sizeof (Elf64_External_Sym),
5432 sizeof (Elf64_External_Dyn),
5433 sizeof (Elf_External_Note),
5436 64, 3, /* ARCH_SIZE, LOG_FILE_ALIGN */
5437 ELFCLASS64, EV_CURRENT,
5438 bfd_elf64_write_out_phdrs,
5439 elf64_vms_write_shdrs_and_ehdr,
5440 bfd_elf64_checksum_contents,
5441 bfd_elf64_write_relocs,
5442 bfd_elf64_swap_symbol_in,
5443 bfd_elf64_swap_symbol_out,
5444 bfd_elf64_slurp_reloc_table,
5445 bfd_elf64_slurp_symbol_table,
5446 bfd_elf64_swap_dyn_in,
5447 bfd_elf64_swap_dyn_out,
5448 bfd_elf64_swap_reloc_in,
5449 bfd_elf64_swap_reloc_out,
5450 bfd_elf64_swap_reloca_in,
5451 bfd_elf64_swap_reloca_out
5454 #define ELF_ARCH bfd_arch_ia64
5455 #define ELF_MACHINE_CODE EM_IA_64
5456 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
5457 #define ELF_COMMONPAGESIZE 0x200 /* 16KB */
5459 #define elf_backend_section_from_shdr \
5460 elf64_ia64_section_from_shdr
5461 #define elf_backend_section_flags \
5462 elf64_ia64_section_flags
5463 #define elf_backend_fake_sections \
5464 elf64_ia64_fake_sections
5465 #define elf_backend_final_write_processing \
5466 elf64_ia64_final_write_processing
5467 #define elf_backend_add_symbol_hook \
5468 elf64_ia64_add_symbol_hook
5469 #define elf_info_to_howto \
5470 elf64_ia64_info_to_howto
5472 #define bfd_elf64_bfd_reloc_type_lookup \
5473 ia64_elf_reloc_type_lookup
5474 #define bfd_elf64_bfd_reloc_name_lookup \
5475 ia64_elf_reloc_name_lookup
5476 #define bfd_elf64_bfd_is_local_label_name \
5477 elf64_ia64_is_local_label_name
5478 #define bfd_elf64_bfd_relax_section \
5479 elf64_ia64_relax_section
5481 #define elf_backend_object_p \
5482 elf64_ia64_object_p
5484 /* Stuff for the BFD linker: */
5485 #define bfd_elf64_bfd_link_hash_table_create \
5486 elf64_ia64_hash_table_create
5487 #define elf_backend_create_dynamic_sections \
5488 elf64_ia64_create_dynamic_sections
5489 #define elf_backend_check_relocs \
5490 elf64_ia64_check_relocs
5491 #define elf_backend_adjust_dynamic_symbol \
5492 elf64_ia64_adjust_dynamic_symbol
5493 #define elf_backend_size_dynamic_sections \
5494 elf64_ia64_size_dynamic_sections
5495 #define elf_backend_omit_section_dynsym \
5496 _bfd_elf_omit_section_dynsym_all
5497 #define elf_backend_relocate_section \
5498 elf64_ia64_relocate_section
5499 #define elf_backend_finish_dynamic_symbol \
5500 elf64_ia64_finish_dynamic_symbol
5501 #define elf_backend_finish_dynamic_sections \
5502 elf64_ia64_finish_dynamic_sections
5503 #define bfd_elf64_bfd_final_link \
5504 elf64_ia64_final_link
5506 #define bfd_elf64_bfd_merge_private_bfd_data \
5507 elf64_ia64_merge_private_bfd_data
5508 #define bfd_elf64_bfd_set_private_flags \
5509 elf64_ia64_set_private_flags
5510 #define bfd_elf64_bfd_print_private_bfd_data \
5511 elf64_ia64_print_private_bfd_data
5513 #define elf_backend_plt_readonly 1
5514 #define elf_backend_want_plt_sym 0
5515 #define elf_backend_plt_alignment 5
5516 #define elf_backend_got_header_size 0
5517 #define elf_backend_want_got_plt 1
5518 #define elf_backend_may_use_rel_p 1
5519 #define elf_backend_may_use_rela_p 1
5520 #define elf_backend_default_use_rela_p 1
5521 #define elf_backend_want_dynbss 0
5522 #define elf_backend_hide_symbol elf64_ia64_hash_hide_symbol
5523 #define elf_backend_fixup_symbol _bfd_elf_link_hash_fixup_symbol
5524 #define elf_backend_reloc_type_class elf64_ia64_reloc_type_class
5525 #define elf_backend_rela_normal 1
5526 #define elf_backend_special_sections elf64_ia64_special_sections
5527 #define elf_backend_default_execstack 0
5529 /* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
5530 SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
5531 We don't want to flood users with so many error messages. We turn
5532 off the warning for now. It will be turned on later when the Intel
5533 compiler is fixed. */
5534 #define elf_backend_link_order_error_handler NULL
5536 /* VMS-specific vectors. */
5538 #undef TARGET_LITTLE_SYM
5539 #define TARGET_LITTLE_SYM ia64_elf64_vms_vec
5540 #undef TARGET_LITTLE_NAME
5541 #define TARGET_LITTLE_NAME "elf64-ia64-vms"
5542 #undef TARGET_BIG_SYM
5543 #undef TARGET_BIG_NAME
5545 /* These are VMS specific functions. */
5547 #undef elf_backend_object_p
5548 #define elf_backend_object_p elf64_vms_object_p
5550 #undef elf_backend_section_from_shdr
5551 #define elf_backend_section_from_shdr elf64_vms_section_from_shdr
5553 #undef elf_backend_init_file_header
5554 #define elf_backend_init_file_header elf64_vms_init_file_header
5556 #undef elf_backend_section_processing
5557 #define elf_backend_section_processing elf64_vms_section_processing
5559 #undef elf_backend_final_write_processing
5560 #define elf_backend_final_write_processing elf64_vms_final_write_processing
5562 #undef bfd_elf64_close_and_cleanup
5563 #define bfd_elf64_close_and_cleanup elf64_vms_close_and_cleanup
5565 #undef elf_backend_section_from_bfd_section
5567 #undef elf_backend_symbol_processing
5569 #undef elf_backend_want_p_paddr_set_to_zero
5571 #undef ELF_OSABI
5572 #define ELF_OSABI ELFOSABI_OPENVMS
5574 #undef ELF_MAXPAGESIZE
5575 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
5577 #undef elf64_bed
5578 #define elf64_bed elf64_ia64_vms_bed
5580 #define elf_backend_size_info elf64_ia64_vms_size_info
5582 /* Use VMS-style archives (in particular, don't use the standard coff
5583 archive format). */
5584 #define bfd_elf64_archive_functions
5586 #undef bfd_elf64_archive_p
5587 #define bfd_elf64_archive_p _bfd_vms_lib_ia64_archive_p
5588 #undef bfd_elf64_write_archive_contents
5589 #define bfd_elf64_write_archive_contents _bfd_vms_lib_write_archive_contents
5590 #undef bfd_elf64_mkarchive
5591 #define bfd_elf64_mkarchive _bfd_vms_lib_ia64_mkarchive
5593 #define bfd_elf64_archive_slurp_armap \
5594 _bfd_vms_lib_slurp_armap
5595 #define bfd_elf64_archive_slurp_extended_name_table \
5596 _bfd_vms_lib_slurp_extended_name_table
5597 #define bfd_elf64_archive_construct_extended_name_table \
5598 _bfd_vms_lib_construct_extended_name_table
5599 #define bfd_elf64_archive_truncate_arname \
5600 _bfd_vms_lib_truncate_arname
5601 #define bfd_elf64_archive_write_armap \
5602 _bfd_vms_lib_write_armap
5603 #define bfd_elf64_archive_read_ar_hdr \
5604 _bfd_vms_lib_read_ar_hdr
5605 #define bfd_elf64_archive_write_ar_hdr \
5606 _bfd_vms_lib_write_ar_hdr
5607 #define bfd_elf64_archive_openr_next_archived_file \
5608 _bfd_vms_lib_openr_next_archived_file
5609 #define bfd_elf64_archive_get_elt_at_index \
5610 _bfd_vms_lib_get_elt_at_index
5611 #define bfd_elf64_archive_generic_stat_arch_elt \
5612 _bfd_vms_lib_generic_stat_arch_elt
5613 #define bfd_elf64_archive_update_armap_timestamp \
5614 _bfd_vms_lib_update_armap_timestamp
5616 /* VMS link methods. */
5617 #undef bfd_elf64_bfd_link_add_symbols
5618 #define bfd_elf64_bfd_link_add_symbols elf64_vms_bfd_link_add_symbols
5620 #undef elf_backend_want_got_sym
5621 #define elf_backend_want_got_sym 0
5623 #undef bfd_elf64_mkobject
5624 #define bfd_elf64_mkobject elf64_ia64_vms_mkobject
5626 /* Redefine to align segments on block size. */
5627 #undef ELF_MAXPAGESIZE
5628 #define ELF_MAXPAGESIZE 0x200 /* 512B */
5630 #undef elf_backend_want_got_plt
5631 #define elf_backend_want_got_plt 0
5633 #include "elf64-target.h"