Mach-O: add objdump -P function_starts to display function starts.
[binutils-gdb.git] / bfd / elf64-ia64-vms.c
blob11ab8175f59228f285249b05c1eb58bd9bafdbe8
1 /* IA-64 support for OpenVMS
2 Copyright (C) 1998-2014 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 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
174 == IA64_ELF_DATA ? ((struct elf64_ia64_link_hash_table *) ((p)->hash)) : NULL)
176 struct elf64_ia64_vms_obj_tdata
178 struct elf_obj_tdata root;
180 /* Ident for shared library. */
181 bfd_uint64_t ident;
183 /* Used only during link: offset in the .fixups section for this bfd. */
184 bfd_vma fixups_off;
186 /* Max number of shared libraries. */
187 unsigned int needed_count;
190 #define elf_ia64_vms_tdata(abfd) \
191 ((struct elf64_ia64_vms_obj_tdata *)((abfd)->tdata.any))
192 #define elf_ia64_vms_ident(abfd) (elf_ia64_vms_tdata(abfd)->ident)
194 struct elf64_vms_transfer
196 unsigned char size[4];
197 unsigned char spare[4];
198 unsigned char tfradr1[8];
199 unsigned char tfradr2[8];
200 unsigned char tfradr3[8];
201 unsigned char tfradr4[8];
202 unsigned char tfradr5[8];
204 /* Local function descriptor for tfr3. */
205 unsigned char tfr3_func[8];
206 unsigned char tfr3_gp[8];
209 typedef struct
211 Elf64_External_Ehdr ehdr;
212 unsigned char vms_needed_count[8];
213 } Elf64_External_VMS_Ehdr;
215 static struct elf64_ia64_dyn_sym_info * get_dyn_sym_info
216 (struct elf64_ia64_link_hash_table *,
217 struct elf_link_hash_entry *,
218 bfd *, const Elf_Internal_Rela *, bfd_boolean);
219 static bfd_boolean elf64_ia64_dynamic_symbol_p
220 (struct elf_link_hash_entry *);
221 static bfd_boolean elf64_ia64_choose_gp
222 (bfd *, struct bfd_link_info *, bfd_boolean);
223 static void elf64_ia64_dyn_sym_traverse
224 (struct elf64_ia64_link_hash_table *,
225 bfd_boolean (*) (struct elf64_ia64_dyn_sym_info *, void *),
226 void *);
227 static bfd_boolean allocate_global_data_got
228 (struct elf64_ia64_dyn_sym_info *, void *);
229 static bfd_boolean allocate_global_fptr_got
230 (struct elf64_ia64_dyn_sym_info *, void *);
231 static bfd_boolean allocate_local_got
232 (struct elf64_ia64_dyn_sym_info *, void *);
233 static bfd_boolean allocate_dynrel_entries
234 (struct elf64_ia64_dyn_sym_info *, void *);
235 static asection *get_pltoff
236 (bfd *, struct elf64_ia64_link_hash_table *);
237 static asection *get_got
238 (bfd *, struct elf64_ia64_link_hash_table *);
241 /* Given a ELF reloc, return the matching HOWTO structure. */
243 static void
244 elf64_ia64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
245 arelent *bfd_reloc,
246 Elf_Internal_Rela *elf_reloc)
248 bfd_reloc->howto
249 = ia64_elf_lookup_howto ((unsigned int) ELF64_R_TYPE (elf_reloc->r_info));
253 #define PLT_FULL_ENTRY_SIZE (2 * 16)
255 static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
257 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
258 0x00, 0x41, 0x3c, 0x70, 0x29, 0xc0, /* ld8.acq r16=[r15],8*/
259 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
260 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
261 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
262 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
265 static const bfd_byte oor_brl[16] =
267 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
268 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.sptk.few tgt;;*/
269 0x00, 0x00, 0x00, 0xc0
273 /* These functions do relaxation for IA-64 ELF. */
275 /* Rename some of the generic section flags to better document how they
276 are used here. */
277 #define skip_relax_pass_0 sec_flg0
278 #define skip_relax_pass_1 sec_flg1
280 static void
281 elf64_ia64_update_short_info (asection *sec, bfd_vma offset,
282 struct elf64_ia64_link_hash_table *ia64_info)
284 /* Skip ABS and SHF_IA_64_SHORT sections. */
285 if (sec == bfd_abs_section_ptr
286 || (sec->flags & SEC_SMALL_DATA) != 0)
287 return;
289 if (!ia64_info->min_short_sec)
291 ia64_info->max_short_sec = sec;
292 ia64_info->max_short_offset = offset;
293 ia64_info->min_short_sec = sec;
294 ia64_info->min_short_offset = offset;
296 else if (sec == ia64_info->max_short_sec
297 && offset > ia64_info->max_short_offset)
298 ia64_info->max_short_offset = offset;
299 else if (sec == ia64_info->min_short_sec
300 && offset < ia64_info->min_short_offset)
301 ia64_info->min_short_offset = offset;
302 else if (sec->output_section->vma
303 > ia64_info->max_short_sec->vma)
305 ia64_info->max_short_sec = sec;
306 ia64_info->max_short_offset = offset;
308 else if (sec->output_section->vma
309 < ia64_info->min_short_sec->vma)
311 ia64_info->min_short_sec = sec;
312 ia64_info->min_short_offset = offset;
316 /* Use a two passes algorithm. In the first pass, branches are relaxed
317 (which may increase the size of the section). In the second pass,
318 the other relaxations are done.
321 static bfd_boolean
322 elf64_ia64_relax_section (bfd *abfd, asection *sec,
323 struct bfd_link_info *link_info,
324 bfd_boolean *again)
326 struct one_fixup
328 struct one_fixup *next;
329 asection *tsec;
330 bfd_vma toff;
331 bfd_vma trampoff;
334 Elf_Internal_Shdr *symtab_hdr;
335 Elf_Internal_Rela *internal_relocs;
336 Elf_Internal_Rela *irel, *irelend;
337 bfd_byte *contents;
338 Elf_Internal_Sym *isymbuf = NULL;
339 struct elf64_ia64_link_hash_table *ia64_info;
340 struct one_fixup *fixups = NULL;
341 bfd_boolean changed_contents = FALSE;
342 bfd_boolean changed_relocs = FALSE;
343 bfd_boolean skip_relax_pass_0 = TRUE;
344 bfd_boolean skip_relax_pass_1 = TRUE;
345 bfd_vma gp = 0;
347 /* Assume we're not going to change any sizes, and we'll only need
348 one pass. */
349 *again = FALSE;
351 if (link_info->relocatable)
352 (*link_info->callbacks->einfo)
353 (_("%P%F: --relax and -r may not be used together\n"));
355 /* Don't even try to relax for non-ELF outputs. */
356 if (!is_elf_hash_table (link_info->hash))
357 return FALSE;
359 /* Nothing to do if there are no relocations or there is no need for
360 the current pass. */
361 if ((sec->flags & SEC_RELOC) == 0
362 || sec->reloc_count == 0
363 || (link_info->relax_pass == 0 && sec->skip_relax_pass_0)
364 || (link_info->relax_pass == 1 && sec->skip_relax_pass_1))
365 return TRUE;
367 ia64_info = elf64_ia64_hash_table (link_info);
368 if (ia64_info == NULL)
369 return FALSE;
371 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
373 /* Load the relocations for this section. */
374 internal_relocs = (_bfd_elf_link_read_relocs
375 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
376 link_info->keep_memory));
377 if (internal_relocs == NULL)
378 return FALSE;
380 irelend = internal_relocs + sec->reloc_count;
382 /* Get the section contents. */
383 if (elf_section_data (sec)->this_hdr.contents != NULL)
384 contents = elf_section_data (sec)->this_hdr.contents;
385 else
387 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
388 goto error_return;
391 for (irel = internal_relocs; irel < irelend; irel++)
393 unsigned long r_type = ELF64_R_TYPE (irel->r_info);
394 bfd_vma symaddr, reladdr, trampoff, toff, roff;
395 asection *tsec;
396 struct one_fixup *f;
397 bfd_size_type amt;
398 bfd_boolean is_branch;
399 struct elf64_ia64_dyn_sym_info *dyn_i;
401 switch (r_type)
403 case R_IA64_PCREL21B:
404 case R_IA64_PCREL21BI:
405 case R_IA64_PCREL21M:
406 case R_IA64_PCREL21F:
407 /* In pass 1, all br relaxations are done. We can skip it. */
408 if (link_info->relax_pass == 1)
409 continue;
410 skip_relax_pass_0 = FALSE;
411 is_branch = TRUE;
412 break;
414 case R_IA64_PCREL60B:
415 /* We can't optimize brl to br in pass 0 since br relaxations
416 will increase the code size. Defer it to pass 1. */
417 if (link_info->relax_pass == 0)
419 skip_relax_pass_1 = FALSE;
420 continue;
422 is_branch = TRUE;
423 break;
425 case R_IA64_GPREL22:
426 /* Update max_short_sec/min_short_sec. */
428 case R_IA64_LTOFF22X:
429 case R_IA64_LDXMOV:
430 /* We can't relax ldx/mov in pass 0 since br relaxations will
431 increase the code size. Defer it to pass 1. */
432 if (link_info->relax_pass == 0)
434 skip_relax_pass_1 = FALSE;
435 continue;
437 is_branch = FALSE;
438 break;
440 default:
441 continue;
444 /* Get the value of the symbol referred to by the reloc. */
445 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
447 /* A local symbol. */
448 Elf_Internal_Sym *isym;
450 /* Read this BFD's local symbols. */
451 if (isymbuf == NULL)
453 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
454 if (isymbuf == NULL)
455 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
456 symtab_hdr->sh_info, 0,
457 NULL, NULL, NULL);
458 if (isymbuf == 0)
459 goto error_return;
462 isym = isymbuf + ELF64_R_SYM (irel->r_info);
463 if (isym->st_shndx == SHN_UNDEF)
464 continue; /* We can't do anything with undefined symbols. */
465 else if (isym->st_shndx == SHN_ABS)
466 tsec = bfd_abs_section_ptr;
467 else if (isym->st_shndx == SHN_COMMON)
468 tsec = bfd_com_section_ptr;
469 else if (isym->st_shndx == SHN_IA_64_ANSI_COMMON)
470 tsec = bfd_com_section_ptr;
471 else
472 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
474 toff = isym->st_value;
475 dyn_i = get_dyn_sym_info (ia64_info, NULL, abfd, irel, FALSE);
477 else
479 unsigned long indx;
480 struct elf_link_hash_entry *h;
482 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
483 h = elf_sym_hashes (abfd)[indx];
484 BFD_ASSERT (h != NULL);
486 while (h->root.type == bfd_link_hash_indirect
487 || h->root.type == bfd_link_hash_warning)
488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
490 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, FALSE);
492 /* For branches to dynamic symbols, we're interested instead
493 in a branch to the PLT entry. */
494 if (is_branch && dyn_i && dyn_i->want_plt2)
496 /* Internal branches shouldn't be sent to the PLT.
497 Leave this for now and we'll give an error later. */
498 if (r_type != R_IA64_PCREL21B)
499 continue;
501 tsec = ia64_info->root.splt;
502 toff = dyn_i->plt2_offset;
503 BFD_ASSERT (irel->r_addend == 0);
506 /* Can't do anything else with dynamic symbols. */
507 else if (elf64_ia64_dynamic_symbol_p (h))
508 continue;
510 else
512 /* We can't do anything with undefined symbols. */
513 if (h->root.type == bfd_link_hash_undefined
514 || h->root.type == bfd_link_hash_undefweak)
515 continue;
517 tsec = h->root.u.def.section;
518 toff = h->root.u.def.value;
522 toff += irel->r_addend;
524 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
526 roff = irel->r_offset;
528 if (is_branch)
530 bfd_signed_vma offset;
532 reladdr = (sec->output_section->vma
533 + sec->output_offset
534 + roff) & (bfd_vma) -4;
536 /* The .plt section is aligned at 32byte and the .text section
537 is aligned at 64byte. The .text section is right after the
538 .plt section. After the first relaxation pass, linker may
539 increase the gap between the .plt and .text sections up
540 to 32byte. We assume linker will always insert 32byte
541 between the .plt and .text sections after the first
542 relaxation pass. */
543 if (tsec == ia64_info->root.splt)
544 offset = -0x1000000 + 32;
545 else
546 offset = -0x1000000;
548 /* If the branch is in range, no need to do anything. */
549 if ((bfd_signed_vma) (symaddr - reladdr) >= offset
550 && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
552 /* If the 60-bit branch is in 21-bit range, optimize it. */
553 if (r_type == R_IA64_PCREL60B)
555 ia64_elf_relax_brl (contents, roff);
557 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
558 R_IA64_PCREL21B);
560 /* If the original relocation offset points to slot
561 1, change it to slot 2. */
562 if ((irel->r_offset & 3) == 1)
563 irel->r_offset += 1;
566 continue;
568 else if (r_type == R_IA64_PCREL60B)
569 continue;
570 else if (ia64_elf_relax_br (contents, roff))
572 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
573 R_IA64_PCREL60B);
575 /* Make the relocation offset point to slot 1. */
576 irel->r_offset = (irel->r_offset & ~((bfd_vma) 0x3)) + 1;
577 continue;
580 /* We can't put a trampoline in a .init/.fini section. Issue
581 an error. */
582 if (strcmp (sec->output_section->name, ".init") == 0
583 || strcmp (sec->output_section->name, ".fini") == 0)
585 (*_bfd_error_handler)
586 (_("%B: Can't relax br at 0x%lx in section `%A'. Please use brl or indirect branch."),
587 sec->owner, sec, (unsigned long) roff);
588 bfd_set_error (bfd_error_bad_value);
589 goto error_return;
592 /* If the branch and target are in the same section, you've
593 got one honking big section and we can't help you unless
594 you are branching backwards. You'll get an error message
595 later. */
596 if (tsec == sec && toff > roff)
597 continue;
599 /* Look for an existing fixup to this address. */
600 for (f = fixups; f ; f = f->next)
601 if (f->tsec == tsec && f->toff == toff)
602 break;
604 if (f == NULL)
606 /* Two alternatives: If it's a branch to a PLT entry, we can
607 make a copy of the FULL_PLT entry. Otherwise, we'll have
608 to use a `brl' insn to get where we're going. */
610 size_t size;
612 if (tsec == ia64_info->root.splt)
613 size = sizeof (plt_full_entry);
614 else
615 size = sizeof (oor_brl);
617 /* Resize the current section to make room for the new branch. */
618 trampoff = (sec->size + 15) & (bfd_vma) -16;
620 /* If trampoline is out of range, there is nothing we
621 can do. */
622 offset = trampoff - (roff & (bfd_vma) -4);
623 if (offset < -0x1000000 || offset > 0x0FFFFF0)
624 continue;
626 amt = trampoff + size;
627 contents = (bfd_byte *) bfd_realloc (contents, amt);
628 if (contents == NULL)
629 goto error_return;
630 sec->size = amt;
632 if (tsec == ia64_info->root.splt)
634 memcpy (contents + trampoff, plt_full_entry, size);
636 /* Hijack the old relocation for use as the PLTOFF reloc. */
637 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
638 R_IA64_PLTOFF22);
639 irel->r_offset = trampoff;
641 else
643 memcpy (contents + trampoff, oor_brl, size);
644 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
645 R_IA64_PCREL60B);
646 irel->r_offset = trampoff + 2;
649 /* Record the fixup so we don't do it again this section. */
650 f = (struct one_fixup *)
651 bfd_malloc ((bfd_size_type) sizeof (*f));
652 f->next = fixups;
653 f->tsec = tsec;
654 f->toff = toff;
655 f->trampoff = trampoff;
656 fixups = f;
658 else
660 /* If trampoline is out of range, there is nothing we
661 can do. */
662 offset = f->trampoff - (roff & (bfd_vma) -4);
663 if (offset < -0x1000000 || offset > 0x0FFFFF0)
664 continue;
666 /* Nop out the reloc, since we're finalizing things here. */
667 irel->r_info = ELF64_R_INFO (0, R_IA64_NONE);
670 /* Fix up the existing branch to hit the trampoline. */
671 if (ia64_elf_install_value (contents + roff, offset, r_type)
672 != bfd_reloc_ok)
673 goto error_return;
675 changed_contents = TRUE;
676 changed_relocs = TRUE;
678 else
680 /* Fetch the gp. */
681 if (gp == 0)
683 bfd *obfd = sec->output_section->owner;
684 gp = _bfd_get_gp_value (obfd);
685 if (gp == 0)
687 if (!elf64_ia64_choose_gp (obfd, link_info, FALSE))
688 goto error_return;
689 gp = _bfd_get_gp_value (obfd);
693 /* If the data is out of range, do nothing. */
694 if ((bfd_signed_vma) (symaddr - gp) >= 0x200000
695 ||(bfd_signed_vma) (symaddr - gp) < -0x200000)
696 continue;
698 if (r_type == R_IA64_GPREL22)
699 elf64_ia64_update_short_info (tsec->output_section,
700 tsec->output_offset + toff,
701 ia64_info);
702 else if (r_type == R_IA64_LTOFF22X)
704 /* Can't deal yet correctly with ABS symbols. */
705 if (bfd_is_abs_section (tsec))
706 continue;
708 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
709 R_IA64_GPREL22);
710 changed_relocs = TRUE;
712 elf64_ia64_update_short_info (tsec->output_section,
713 tsec->output_offset + toff,
714 ia64_info);
716 else
718 ia64_elf_relax_ldxmov (contents, roff);
719 irel->r_info = ELF64_R_INFO (0, R_IA64_NONE);
720 changed_contents = TRUE;
721 changed_relocs = TRUE;
726 /* ??? If we created fixups, this may push the code segment large
727 enough that the data segment moves, which will change the GP.
728 Reset the GP so that we re-calculate next round. We need to
729 do this at the _beginning_ of the next round; now will not do. */
731 /* Clean up and go home. */
732 while (fixups)
734 struct one_fixup *f = fixups;
735 fixups = fixups->next;
736 free (f);
739 if (isymbuf != NULL
740 && symtab_hdr->contents != (unsigned char *) isymbuf)
742 if (! link_info->keep_memory)
743 free (isymbuf);
744 else
746 /* Cache the symbols for elf_link_input_bfd. */
747 symtab_hdr->contents = (unsigned char *) isymbuf;
751 if (contents != NULL
752 && elf_section_data (sec)->this_hdr.contents != contents)
754 if (!changed_contents && !link_info->keep_memory)
755 free (contents);
756 else
758 /* Cache the section contents for elf_link_input_bfd. */
759 elf_section_data (sec)->this_hdr.contents = contents;
763 if (elf_section_data (sec)->relocs != internal_relocs)
765 if (!changed_relocs)
766 free (internal_relocs);
767 else
768 elf_section_data (sec)->relocs = internal_relocs;
771 if (link_info->relax_pass == 0)
773 /* Pass 0 is only needed to relax br. */
774 sec->skip_relax_pass_0 = skip_relax_pass_0;
775 sec->skip_relax_pass_1 = skip_relax_pass_1;
778 *again = changed_contents || changed_relocs;
779 return TRUE;
781 error_return:
782 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
783 free (isymbuf);
784 if (contents != NULL
785 && elf_section_data (sec)->this_hdr.contents != contents)
786 free (contents);
787 if (internal_relocs != NULL
788 && elf_section_data (sec)->relocs != internal_relocs)
789 free (internal_relocs);
790 return FALSE;
792 #undef skip_relax_pass_0
793 #undef skip_relax_pass_1
795 /* Return TRUE if NAME is an unwind table section name. */
797 static inline bfd_boolean
798 is_unwind_section_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
800 return ((CONST_STRNEQ (name, ELF_STRING_ia64_unwind)
801 && ! CONST_STRNEQ (name, ELF_STRING_ia64_unwind_info))
802 || CONST_STRNEQ (name, ELF_STRING_ia64_unwind_once));
806 /* Convert IA-64 specific section flags to bfd internal section flags. */
808 /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
809 flag. */
811 static bfd_boolean
812 elf64_ia64_section_flags (flagword *flags,
813 const Elf_Internal_Shdr *hdr)
815 if (hdr->sh_flags & SHF_IA_64_SHORT)
816 *flags |= SEC_SMALL_DATA;
818 return TRUE;
821 /* Set the correct type for an IA-64 ELF section. We do this by the
822 section name, which is a hack, but ought to work. */
824 static bfd_boolean
825 elf64_ia64_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr,
826 asection *sec)
828 const char *name;
830 name = bfd_get_section_name (abfd, sec);
832 if (is_unwind_section_name (abfd, name))
834 /* We don't have the sections numbered at this point, so sh_info
835 is set later, in elf64_ia64_final_write_processing. */
836 hdr->sh_type = SHT_IA_64_UNWIND;
837 hdr->sh_flags |= SHF_LINK_ORDER;
839 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
840 hdr->sh_type = SHT_IA_64_EXT;
842 if (sec->flags & SEC_SMALL_DATA)
843 hdr->sh_flags |= SHF_IA_64_SHORT;
845 return TRUE;
848 /* Hook called by the linker routine which adds symbols from an object
849 file. We use it to put .comm items in .sbss, and not .bss. */
851 static bfd_boolean
852 elf64_ia64_add_symbol_hook (bfd *abfd,
853 struct bfd_link_info *info,
854 Elf_Internal_Sym *sym,
855 const char **namep ATTRIBUTE_UNUSED,
856 flagword *flagsp ATTRIBUTE_UNUSED,
857 asection **secp,
858 bfd_vma *valp)
860 if (sym->st_shndx == SHN_COMMON
861 && !info->relocatable
862 && sym->st_size <= elf_gp_size (abfd))
864 /* Common symbols less than or equal to -G nn bytes are
865 automatically put into .sbss. */
867 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
869 if (scomm == NULL)
871 scomm = bfd_make_section_with_flags (abfd, ".scommon",
872 (SEC_ALLOC
873 | SEC_IS_COMMON
874 | SEC_LINKER_CREATED));
875 if (scomm == NULL)
876 return FALSE;
879 *secp = scomm;
880 *valp = sym->st_size;
883 return TRUE;
886 /* According to the Tahoe assembler spec, all labels starting with a
887 '.' are local. */
889 static bfd_boolean
890 elf64_ia64_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
891 const char *name)
893 return name[0] == '.';
896 /* Should we do dynamic things to this symbol? */
898 static bfd_boolean
899 elf64_ia64_dynamic_symbol_p (struct elf_link_hash_entry *h)
901 return h != NULL && h->def_dynamic;
904 static struct bfd_hash_entry*
905 elf64_ia64_new_elf_hash_entry (struct bfd_hash_entry *entry,
906 struct bfd_hash_table *table,
907 const char *string)
909 struct elf64_ia64_link_hash_entry *ret;
910 ret = (struct elf64_ia64_link_hash_entry *) entry;
912 /* Allocate the structure if it has not already been allocated by a
913 subclass. */
914 if (!ret)
915 ret = bfd_hash_allocate (table, sizeof (*ret));
917 if (!ret)
918 return 0;
920 /* Call the allocation method of the superclass. */
921 ret = ((struct elf64_ia64_link_hash_entry *)
922 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
923 table, string));
925 ret->info = NULL;
926 ret->count = 0;
927 ret->sorted_count = 0;
928 ret->size = 0;
929 return (struct bfd_hash_entry *) ret;
932 static void
933 elf64_ia64_hash_hide_symbol (struct bfd_link_info *info,
934 struct elf_link_hash_entry *xh,
935 bfd_boolean force_local)
937 struct elf64_ia64_link_hash_entry *h;
938 struct elf64_ia64_dyn_sym_info *dyn_i;
939 unsigned int count;
941 h = (struct elf64_ia64_link_hash_entry *)xh;
943 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
945 for (count = h->count, dyn_i = h->info;
946 count != 0;
947 count--, dyn_i++)
949 dyn_i->want_plt2 = 0;
950 dyn_i->want_plt = 0;
954 /* Compute a hash of a local hash entry. */
956 static hashval_t
957 elf64_ia64_local_htab_hash (const void *ptr)
959 struct elf64_ia64_local_hash_entry *entry
960 = (struct elf64_ia64_local_hash_entry *) ptr;
962 return ELF_LOCAL_SYMBOL_HASH (entry->id, entry->r_sym);
965 /* Compare local hash entries. */
967 static int
968 elf64_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
970 struct elf64_ia64_local_hash_entry *entry1
971 = (struct elf64_ia64_local_hash_entry *) ptr1;
972 struct elf64_ia64_local_hash_entry *entry2
973 = (struct elf64_ia64_local_hash_entry *) ptr2;
975 return entry1->id == entry2->id && entry1->r_sym == entry2->r_sym;
978 /* Create the derived linker hash table. The IA-64 ELF port uses this
979 derived hash table to keep information specific to the IA-64 ElF
980 linker (without using static variables). */
982 static struct bfd_link_hash_table *
983 elf64_ia64_hash_table_create (bfd *abfd)
985 struct elf64_ia64_link_hash_table *ret;
987 ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
988 if (!ret)
989 return NULL;
991 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
992 elf64_ia64_new_elf_hash_entry,
993 sizeof (struct elf64_ia64_link_hash_entry),
994 IA64_ELF_DATA))
996 free (ret);
997 return NULL;
1000 ret->loc_hash_table = htab_try_create (1024, elf64_ia64_local_htab_hash,
1001 elf64_ia64_local_htab_eq, NULL);
1002 ret->loc_hash_memory = objalloc_create ();
1003 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1005 free (ret);
1006 return NULL;
1009 return &ret->root.root;
1012 /* Free the global elf64_ia64_dyn_sym_info array. */
1014 static bfd_boolean
1015 elf64_ia64_global_dyn_info_free (void **xentry,
1016 void * unused ATTRIBUTE_UNUSED)
1018 struct elf64_ia64_link_hash_entry *entry
1019 = (struct elf64_ia64_link_hash_entry *) xentry;
1021 if (entry->root.root.type == bfd_link_hash_warning)
1022 entry = (struct elf64_ia64_link_hash_entry *) entry->root.root.u.i.link;
1024 if (entry->info)
1026 free (entry->info);
1027 entry->info = NULL;
1028 entry->count = 0;
1029 entry->sorted_count = 0;
1030 entry->size = 0;
1033 return TRUE;
1036 /* Free the local elf64_ia64_dyn_sym_info array. */
1038 static bfd_boolean
1039 elf64_ia64_local_dyn_info_free (void **slot,
1040 void * unused ATTRIBUTE_UNUSED)
1042 struct elf64_ia64_local_hash_entry *entry
1043 = (struct elf64_ia64_local_hash_entry *) *slot;
1045 if (entry->info)
1047 free (entry->info);
1048 entry->info = NULL;
1049 entry->count = 0;
1050 entry->sorted_count = 0;
1051 entry->size = 0;
1054 return TRUE;
1057 /* Destroy IA-64 linker hash table. */
1059 static void
1060 elf64_ia64_hash_table_free (struct bfd_link_hash_table *hash)
1062 struct elf64_ia64_link_hash_table *ia64_info
1063 = (struct elf64_ia64_link_hash_table *) hash;
1064 if (ia64_info->loc_hash_table)
1066 htab_traverse (ia64_info->loc_hash_table,
1067 elf64_ia64_local_dyn_info_free, NULL);
1068 htab_delete (ia64_info->loc_hash_table);
1070 if (ia64_info->loc_hash_memory)
1071 objalloc_free ((struct objalloc *) ia64_info->loc_hash_memory);
1072 elf_link_hash_traverse (&ia64_info->root,
1073 elf64_ia64_global_dyn_info_free, NULL);
1074 _bfd_elf_link_hash_table_free (hash);
1077 /* Traverse both local and global hash tables. */
1079 struct elf64_ia64_dyn_sym_traverse_data
1081 bfd_boolean (*func) (struct elf64_ia64_dyn_sym_info *, void *);
1082 void * data;
1085 static bfd_boolean
1086 elf64_ia64_global_dyn_sym_thunk (struct bfd_hash_entry *xentry,
1087 void * xdata)
1089 struct elf64_ia64_link_hash_entry *entry
1090 = (struct elf64_ia64_link_hash_entry *) xentry;
1091 struct elf64_ia64_dyn_sym_traverse_data *data
1092 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1093 struct elf64_ia64_dyn_sym_info *dyn_i;
1094 unsigned int count;
1096 if (entry->root.root.type == bfd_link_hash_warning)
1097 entry = (struct elf64_ia64_link_hash_entry *) entry->root.root.u.i.link;
1099 for (count = entry->count, dyn_i = entry->info;
1100 count != 0;
1101 count--, dyn_i++)
1102 if (! (*data->func) (dyn_i, data->data))
1103 return FALSE;
1104 return TRUE;
1107 static bfd_boolean
1108 elf64_ia64_local_dyn_sym_thunk (void **slot, void * xdata)
1110 struct elf64_ia64_local_hash_entry *entry
1111 = (struct elf64_ia64_local_hash_entry *) *slot;
1112 struct elf64_ia64_dyn_sym_traverse_data *data
1113 = (struct elf64_ia64_dyn_sym_traverse_data *) xdata;
1114 struct elf64_ia64_dyn_sym_info *dyn_i;
1115 unsigned int count;
1117 for (count = entry->count, dyn_i = entry->info;
1118 count != 0;
1119 count--, dyn_i++)
1120 if (! (*data->func) (dyn_i, data->data))
1121 return FALSE;
1122 return TRUE;
1125 static void
1126 elf64_ia64_dyn_sym_traverse (struct elf64_ia64_link_hash_table *ia64_info,
1127 bfd_boolean (*func) (struct elf64_ia64_dyn_sym_info *, void *),
1128 void * data)
1130 struct elf64_ia64_dyn_sym_traverse_data xdata;
1132 xdata.func = func;
1133 xdata.data = data;
1135 elf_link_hash_traverse (&ia64_info->root,
1136 elf64_ia64_global_dyn_sym_thunk, &xdata);
1137 htab_traverse (ia64_info->loc_hash_table,
1138 elf64_ia64_local_dyn_sym_thunk, &xdata);
1141 #define NOTE_NAME "IPF/VMS"
1143 static bfd_boolean
1144 create_ia64_vms_notes (bfd *abfd, struct bfd_link_info *info,
1145 unsigned int time_hi, unsigned int time_lo)
1147 #define NBR_NOTES 7
1148 Elf_Internal_Note notes[NBR_NOTES];
1149 char *module_name;
1150 int module_name_len;
1151 unsigned char cur_time[8];
1152 Elf64_External_VMS_ORIG_DYN_Note *orig_dyn;
1153 unsigned int orig_dyn_size;
1154 unsigned int note_size;
1155 int i;
1156 unsigned char *noteptr;
1157 unsigned char *note_contents;
1158 struct elf64_ia64_link_hash_table *ia64_info;
1160 ia64_info = elf64_ia64_hash_table (info);
1162 module_name = vms_get_module_name (bfd_get_filename (abfd), TRUE);
1163 module_name_len = strlen (module_name) + 1;
1165 bfd_putl32 (time_lo, cur_time + 0);
1166 bfd_putl32 (time_hi, cur_time + 4);
1168 /* Note 0: IMGNAM. */
1169 notes[0].type = NT_VMS_IMGNAM;
1170 notes[0].descdata = module_name;
1171 notes[0].descsz = module_name_len;
1173 /* Note 1: GSTNAM. */
1174 notes[1].type = NT_VMS_GSTNAM;
1175 notes[1].descdata = module_name;
1176 notes[1].descsz = module_name_len;
1178 /* Note 2: IMGID. */
1179 #define IMG_ID "V1.0"
1180 notes[2].type = NT_VMS_IMGID;
1181 notes[2].descdata = IMG_ID;
1182 notes[2].descsz = sizeof (IMG_ID);
1184 /* Note 3: Linktime. */
1185 notes[3].type = NT_VMS_LINKTIME;
1186 notes[3].descdata = (char *)cur_time;
1187 notes[3].descsz = sizeof (cur_time);
1189 /* Note 4: Linker id. */
1190 notes[4].type = NT_VMS_LINKID;
1191 notes[4].descdata = "GNU ld " BFD_VERSION_STRING;
1192 notes[4].descsz = strlen (notes[4].descdata) + 1;
1194 /* Note 5: Original dyn. */
1195 orig_dyn_size = (sizeof (*orig_dyn) + sizeof (IMG_ID) - 1 + 7) & ~7;
1196 orig_dyn = bfd_zalloc (abfd, orig_dyn_size);
1197 if (orig_dyn == NULL)
1198 return FALSE;
1199 bfd_putl32 (1, orig_dyn->major_id);
1200 bfd_putl32 (3, orig_dyn->minor_id);
1201 memcpy (orig_dyn->manipulation_date, cur_time, sizeof (cur_time));
1202 bfd_putl64 (VMS_LF_IMGSTA | VMS_LF_MAIN, orig_dyn->link_flags);
1203 bfd_putl32 (EF_IA_64_ABI64, orig_dyn->elf_flags);
1204 memcpy (orig_dyn->imgid, IMG_ID, sizeof (IMG_ID));
1205 notes[5].type = NT_VMS_ORIG_DYN;
1206 notes[5].descdata = (char *)orig_dyn;
1207 notes[5].descsz = orig_dyn_size;
1209 /* Note 3: Patchtime. */
1210 notes[6].type = NT_VMS_PATCHTIME;
1211 notes[6].descdata = (char *)cur_time;
1212 notes[6].descsz = sizeof (cur_time);
1214 /* Compute notes size. */
1215 note_size = 0;
1216 for (i = 0; i < NBR_NOTES; i++)
1217 note_size += sizeof (Elf64_External_VMS_Note) - 1
1218 + ((sizeof (NOTE_NAME) - 1 + 7) & ~7)
1219 + ((notes[i].descsz + 7) & ~7);
1221 /* Malloc a temporary buffer large enough for most notes */
1222 note_contents = (unsigned char *) bfd_zalloc (abfd, note_size);
1223 if (note_contents == NULL)
1224 return FALSE;
1225 noteptr = note_contents;
1227 /* Fill notes. */
1228 for (i = 0; i < NBR_NOTES; i++)
1230 Elf64_External_VMS_Note *enote = (Elf64_External_VMS_Note *) noteptr;
1232 bfd_putl64 (sizeof (NOTE_NAME) - 1, enote->namesz);
1233 bfd_putl64 (notes[i].descsz, enote->descsz);
1234 bfd_putl64 (notes[i].type, enote->type);
1236 noteptr = (unsigned char *)enote->name;
1237 memcpy (noteptr, NOTE_NAME, sizeof (NOTE_NAME) - 1);
1238 noteptr += (sizeof (NOTE_NAME) - 1 + 7) & ~7;
1239 memcpy (noteptr, notes[i].descdata, notes[i].descsz);
1240 noteptr += (notes[i].descsz + 7) & ~7;
1243 ia64_info->note_sec->contents = note_contents;
1244 ia64_info->note_sec->size = note_size;
1246 free (module_name);
1248 return TRUE;
1251 static bfd_boolean
1252 elf64_ia64_create_dynamic_sections (bfd *abfd,
1253 struct bfd_link_info *info)
1255 struct elf64_ia64_link_hash_table *ia64_info;
1256 asection *s;
1257 flagword flags;
1258 const struct elf_backend_data *bed;
1260 ia64_info = elf64_ia64_hash_table (info);
1261 if (ia64_info == NULL)
1262 return FALSE;
1264 if (elf_hash_table (info)->dynamic_sections_created)
1265 return TRUE;
1267 abfd = elf_hash_table (info)->dynobj;
1268 bed = get_elf_backend_data (abfd);
1270 flags = bed->dynamic_sec_flags;
1272 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic",
1273 flags | SEC_READONLY);
1274 if (s == NULL
1275 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
1276 return FALSE;
1278 s = bfd_make_section_anyway_with_flags (abfd, ".plt", flags | SEC_READONLY);
1279 if (s == NULL
1280 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1281 return FALSE;
1282 ia64_info->root.splt = s;
1284 if (!get_got (abfd, ia64_info))
1285 return FALSE;
1287 if (!get_pltoff (abfd, ia64_info))
1288 return FALSE;
1290 s = bfd_make_section_anyway_with_flags (abfd, ".vmsdynstr",
1291 (SEC_ALLOC
1292 | SEC_HAS_CONTENTS
1293 | SEC_IN_MEMORY
1294 | SEC_LINKER_CREATED));
1295 if (s == NULL
1296 || !bfd_set_section_alignment (abfd, s, 0))
1297 return FALSE;
1299 /* Create a fixup section. */
1300 s = bfd_make_section_anyway_with_flags (abfd, ".fixups",
1301 (SEC_ALLOC
1302 | SEC_HAS_CONTENTS
1303 | SEC_IN_MEMORY
1304 | SEC_LINKER_CREATED));
1305 if (s == NULL
1306 || !bfd_set_section_alignment (abfd, s, 3))
1307 return FALSE;
1308 ia64_info->fixups_sec = s;
1310 /* Create the transfer fixup section. */
1311 s = bfd_make_section_anyway_with_flags (abfd, ".transfer",
1312 (SEC_ALLOC
1313 | SEC_HAS_CONTENTS
1314 | SEC_IN_MEMORY
1315 | SEC_LINKER_CREATED));
1316 if (s == NULL
1317 || !bfd_set_section_alignment (abfd, s, 3))
1318 return FALSE;
1319 s->size = sizeof (struct elf64_vms_transfer);
1320 ia64_info->transfer_sec = s;
1322 /* Create note section. */
1323 s = bfd_make_section_anyway_with_flags (abfd, ".vms.note",
1324 (SEC_LINKER_CREATED
1325 | SEC_HAS_CONTENTS
1326 | SEC_IN_MEMORY
1327 | SEC_READONLY));
1328 if (s == NULL
1329 || !bfd_set_section_alignment (abfd, s, 3))
1330 return FALSE;
1331 ia64_info->note_sec = s;
1333 elf_hash_table (info)->dynamic_sections_created = TRUE;
1334 return TRUE;
1337 /* Find and/or create a hash entry for local symbol. */
1338 static struct elf64_ia64_local_hash_entry *
1339 get_local_sym_hash (struct elf64_ia64_link_hash_table *ia64_info,
1340 bfd *abfd, const Elf_Internal_Rela *rel,
1341 bfd_boolean create)
1343 struct elf64_ia64_local_hash_entry e, *ret;
1344 asection *sec = abfd->sections;
1345 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1346 ELF64_R_SYM (rel->r_info));
1347 void **slot;
1349 e.id = sec->id;
1350 e.r_sym = ELF64_R_SYM (rel->r_info);
1351 slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
1352 create ? INSERT : NO_INSERT);
1354 if (!slot)
1355 return NULL;
1357 if (*slot)
1358 return (struct elf64_ia64_local_hash_entry *) *slot;
1360 ret = (struct elf64_ia64_local_hash_entry *)
1361 objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
1362 sizeof (struct elf64_ia64_local_hash_entry));
1363 if (ret)
1365 memset (ret, 0, sizeof (*ret));
1366 ret->id = sec->id;
1367 ret->r_sym = ELF64_R_SYM (rel->r_info);
1368 *slot = ret;
1370 return ret;
1373 /* Used to sort elf64_ia64_dyn_sym_info array. */
1375 static int
1376 addend_compare (const void *xp, const void *yp)
1378 const struct elf64_ia64_dyn_sym_info *x
1379 = (const struct elf64_ia64_dyn_sym_info *) xp;
1380 const struct elf64_ia64_dyn_sym_info *y
1381 = (const struct elf64_ia64_dyn_sym_info *) yp;
1383 return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
1386 /* Sort elf64_ia64_dyn_sym_info array and remove duplicates. */
1388 static unsigned int
1389 sort_dyn_sym_info (struct elf64_ia64_dyn_sym_info *info,
1390 unsigned int count)
1392 bfd_vma curr, prev, got_offset;
1393 unsigned int i, kept, dupes, diff, dest, src, len;
1395 qsort (info, count, sizeof (*info), addend_compare);
1397 /* Find the first duplicate. */
1398 prev = info [0].addend;
1399 got_offset = info [0].got_offset;
1400 for (i = 1; i < count; i++)
1402 curr = info [i].addend;
1403 if (curr == prev)
1405 /* For duplicates, make sure that GOT_OFFSET is valid. */
1406 if (got_offset == (bfd_vma) -1)
1407 got_offset = info [i].got_offset;
1408 break;
1410 got_offset = info [i].got_offset;
1411 prev = curr;
1414 /* We may move a block of elements to here. */
1415 dest = i++;
1417 /* Remove duplicates. */
1418 if (i < count)
1420 while (i < count)
1422 /* For duplicates, make sure that the kept one has a valid
1423 got_offset. */
1424 kept = dest - 1;
1425 if (got_offset != (bfd_vma) -1)
1426 info [kept].got_offset = got_offset;
1428 curr = info [i].addend;
1429 got_offset = info [i].got_offset;
1431 /* Move a block of elements whose first one is different from
1432 the previous. */
1433 if (curr == prev)
1435 for (src = i + 1; src < count; src++)
1437 if (info [src].addend != curr)
1438 break;
1439 /* For duplicates, make sure that GOT_OFFSET is
1440 valid. */
1441 if (got_offset == (bfd_vma) -1)
1442 got_offset = info [src].got_offset;
1445 /* Make sure that the kept one has a valid got_offset. */
1446 if (got_offset != (bfd_vma) -1)
1447 info [kept].got_offset = got_offset;
1449 else
1450 src = i;
1452 if (src >= count)
1453 break;
1455 /* Find the next duplicate. SRC will be kept. */
1456 prev = info [src].addend;
1457 got_offset = info [src].got_offset;
1458 for (dupes = src + 1; dupes < count; dupes ++)
1460 curr = info [dupes].addend;
1461 if (curr == prev)
1463 /* Make sure that got_offset is valid. */
1464 if (got_offset == (bfd_vma) -1)
1465 got_offset = info [dupes].got_offset;
1467 /* For duplicates, make sure that the kept one has
1468 a valid got_offset. */
1469 if (got_offset != (bfd_vma) -1)
1470 info [dupes - 1].got_offset = got_offset;
1471 break;
1473 got_offset = info [dupes].got_offset;
1474 prev = curr;
1477 /* How much to move. */
1478 len = dupes - src;
1479 i = dupes + 1;
1481 if (len == 1 && dupes < count)
1483 /* If we only move 1 element, we combine it with the next
1484 one. There must be at least a duplicate. Find the
1485 next different one. */
1486 for (diff = dupes + 1, src++; diff < count; diff++, src++)
1488 if (info [diff].addend != curr)
1489 break;
1490 /* Make sure that got_offset is valid. */
1491 if (got_offset == (bfd_vma) -1)
1492 got_offset = info [diff].got_offset;
1495 /* Makre sure that the last duplicated one has an valid
1496 offset. */
1497 BFD_ASSERT (curr == prev);
1498 if (got_offset != (bfd_vma) -1)
1499 info [diff - 1].got_offset = got_offset;
1501 if (diff < count)
1503 /* Find the next duplicate. Track the current valid
1504 offset. */
1505 prev = info [diff].addend;
1506 got_offset = info [diff].got_offset;
1507 for (dupes = diff + 1; dupes < count; dupes ++)
1509 curr = info [dupes].addend;
1510 if (curr == prev)
1512 /* For duplicates, make sure that GOT_OFFSET
1513 is valid. */
1514 if (got_offset == (bfd_vma) -1)
1515 got_offset = info [dupes].got_offset;
1516 break;
1518 got_offset = info [dupes].got_offset;
1519 prev = curr;
1520 diff++;
1523 len = diff - src + 1;
1524 i = diff + 1;
1528 memmove (&info [dest], &info [src], len * sizeof (*info));
1530 dest += len;
1533 count = dest;
1535 else
1537 /* When we get here, either there is no duplicate at all or
1538 the only duplicate is the last element. */
1539 if (dest < count)
1541 /* If the last element is a duplicate, make sure that the
1542 kept one has a valid got_offset. We also update count. */
1543 if (got_offset != (bfd_vma) -1)
1544 info [dest - 1].got_offset = got_offset;
1545 count = dest;
1549 return count;
1552 /* Find and/or create a descriptor for dynamic symbol info. This will
1553 vary based on global or local symbol, and the addend to the reloc.
1555 We don't sort when inserting. Also, we sort and eliminate
1556 duplicates if there is an unsorted section. Typically, this will
1557 only happen once, because we do all insertions before lookups. We
1558 then use bsearch to do a lookup. This also allows lookups to be
1559 fast. So we have fast insertion (O(log N) due to duplicate check),
1560 fast lookup (O(log N)) and one sort (O(N log N) expected time).
1561 Previously, all lookups were O(N) because of the use of the linked
1562 list and also all insertions were O(N) because of the check for
1563 duplicates. There are some complications here because the array
1564 size grows occasionally, which may add an O(N) factor, but this
1565 should be rare. Also, we free the excess array allocation, which
1566 requires a copy which is O(N), but this only happens once. */
1568 static struct elf64_ia64_dyn_sym_info *
1569 get_dyn_sym_info (struct elf64_ia64_link_hash_table *ia64_info,
1570 struct elf_link_hash_entry *h, bfd *abfd,
1571 const Elf_Internal_Rela *rel, bfd_boolean create)
1573 struct elf64_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
1574 unsigned int *count_p, *sorted_count_p, *size_p;
1575 unsigned int count, sorted_count, size;
1576 bfd_vma addend = rel ? rel->r_addend : 0;
1577 bfd_size_type amt;
1579 if (h)
1581 struct elf64_ia64_link_hash_entry *global_h;
1583 global_h = (struct elf64_ia64_link_hash_entry *) h;
1584 info_p = &global_h->info;
1585 count_p = &global_h->count;
1586 sorted_count_p = &global_h->sorted_count;
1587 size_p = &global_h->size;
1589 else
1591 struct elf64_ia64_local_hash_entry *loc_h;
1593 loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
1594 if (!loc_h)
1596 BFD_ASSERT (!create);
1597 return NULL;
1600 info_p = &loc_h->info;
1601 count_p = &loc_h->count;
1602 sorted_count_p = &loc_h->sorted_count;
1603 size_p = &loc_h->size;
1606 count = *count_p;
1607 sorted_count = *sorted_count_p;
1608 size = *size_p;
1609 info = *info_p;
1610 if (create)
1612 /* When we create the array, we don't check for duplicates,
1613 except in the previously sorted section if one exists, and
1614 against the last inserted entry. This allows insertions to
1615 be fast. */
1616 if (info)
1618 if (sorted_count)
1620 /* Try bsearch first on the sorted section. */
1621 key.addend = addend;
1622 dyn_i = bsearch (&key, info, sorted_count,
1623 sizeof (*info), addend_compare);
1625 if (dyn_i)
1627 return dyn_i;
1631 /* Do a quick check for the last inserted entry. */
1632 dyn_i = info + count - 1;
1633 if (dyn_i->addend == addend)
1635 return dyn_i;
1639 if (size == 0)
1641 /* It is the very first element. We create the array of size
1642 1. */
1643 size = 1;
1644 amt = size * sizeof (*info);
1645 info = bfd_malloc (amt);
1647 else if (size <= count)
1649 /* We double the array size every time when we reach the
1650 size limit. */
1651 size += size;
1652 amt = size * sizeof (*info);
1653 info = bfd_realloc (info, amt);
1655 else
1656 goto has_space;
1658 if (info == NULL)
1659 return NULL;
1660 *size_p = size;
1661 *info_p = info;
1663 has_space:
1664 /* Append the new one to the array. */
1665 dyn_i = info + count;
1666 memset (dyn_i, 0, sizeof (*dyn_i));
1667 dyn_i->got_offset = (bfd_vma) -1;
1668 dyn_i->addend = addend;
1670 /* We increment count only since the new ones are unsorted and
1671 may have duplicate. */
1672 (*count_p)++;
1674 else
1676 /* It is a lookup without insertion. Sort array if part of the
1677 array isn't sorted. */
1678 if (count != sorted_count)
1680 count = sort_dyn_sym_info (info, count);
1681 *count_p = count;
1682 *sorted_count_p = count;
1685 /* Free unused memory. */
1686 if (size != count)
1688 amt = count * sizeof (*info);
1689 info = bfd_malloc (amt);
1690 if (info != NULL)
1692 memcpy (info, *info_p, amt);
1693 free (*info_p);
1694 *size_p = count;
1695 *info_p = info;
1699 key.addend = addend;
1700 dyn_i = bsearch (&key, info, count,
1701 sizeof (*info), addend_compare);
1704 return dyn_i;
1707 static asection *
1708 get_got (bfd *abfd, struct elf64_ia64_link_hash_table *ia64_info)
1710 asection *got;
1711 bfd *dynobj;
1713 got = ia64_info->root.sgot;
1714 if (!got)
1716 flagword flags;
1718 dynobj = ia64_info->root.dynobj;
1719 if (!dynobj)
1720 ia64_info->root.dynobj = dynobj = abfd;
1722 /* The .got section is always aligned at 8 bytes. */
1723 flags = get_elf_backend_data (dynobj)->dynamic_sec_flags;
1724 got = bfd_make_section_anyway_with_flags (dynobj, ".got",
1725 flags | SEC_SMALL_DATA);
1726 if (got == NULL
1727 || !bfd_set_section_alignment (dynobj, got, 3))
1728 return NULL;
1729 ia64_info->root.sgot = got;
1732 return got;
1735 /* Create function descriptor section (.opd). This section is called .opd
1736 because it contains "official procedure descriptors". The "official"
1737 refers to the fact that these descriptors are used when taking the address
1738 of a procedure, thus ensuring a unique address for each procedure. */
1740 static asection *
1741 get_fptr (bfd *abfd, struct bfd_link_info *info,
1742 struct elf64_ia64_link_hash_table *ia64_info)
1744 asection *fptr;
1745 bfd *dynobj;
1747 fptr = ia64_info->fptr_sec;
1748 if (!fptr)
1750 dynobj = ia64_info->root.dynobj;
1751 if (!dynobj)
1752 ia64_info->root.dynobj = dynobj = abfd;
1754 fptr = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1755 (SEC_ALLOC
1756 | SEC_LOAD
1757 | SEC_HAS_CONTENTS
1758 | SEC_IN_MEMORY
1759 | (info->pie ? 0
1760 : SEC_READONLY)
1761 | SEC_LINKER_CREATED));
1762 if (!fptr
1763 || !bfd_set_section_alignment (dynobj, fptr, 4))
1765 BFD_ASSERT (0);
1766 return NULL;
1769 ia64_info->fptr_sec = fptr;
1771 if (info->pie)
1773 asection *fptr_rel;
1774 fptr_rel = bfd_make_section_anyway_with_flags (dynobj, ".rela.opd",
1775 (SEC_ALLOC | SEC_LOAD
1776 | SEC_HAS_CONTENTS
1777 | SEC_IN_MEMORY
1778 | SEC_LINKER_CREATED
1779 | SEC_READONLY));
1780 if (fptr_rel == NULL
1781 || !bfd_set_section_alignment (dynobj, fptr_rel, 3))
1783 BFD_ASSERT (0);
1784 return NULL;
1787 ia64_info->rel_fptr_sec = fptr_rel;
1791 return fptr;
1794 static asection *
1795 get_pltoff (bfd *abfd, struct elf64_ia64_link_hash_table *ia64_info)
1797 asection *pltoff;
1798 bfd *dynobj;
1800 pltoff = ia64_info->pltoff_sec;
1801 if (!pltoff)
1803 dynobj = ia64_info->root.dynobj;
1804 if (!dynobj)
1805 ia64_info->root.dynobj = dynobj = abfd;
1807 pltoff = bfd_make_section_anyway_with_flags (dynobj,
1808 ELF_STRING_ia64_pltoff,
1809 (SEC_ALLOC
1810 | SEC_LOAD
1811 | SEC_HAS_CONTENTS
1812 | SEC_IN_MEMORY
1813 | SEC_SMALL_DATA
1814 | SEC_LINKER_CREATED));
1815 if (!pltoff
1816 || !bfd_set_section_alignment (dynobj, pltoff, 4))
1818 BFD_ASSERT (0);
1819 return NULL;
1822 ia64_info->pltoff_sec = pltoff;
1825 return pltoff;
1828 static asection *
1829 get_reloc_section (bfd *abfd,
1830 struct elf64_ia64_link_hash_table *ia64_info,
1831 asection *sec, bfd_boolean create)
1833 const char *srel_name;
1834 asection *srel;
1835 bfd *dynobj;
1837 srel_name = (bfd_elf_string_from_elf_section
1838 (abfd, elf_elfheader(abfd)->e_shstrndx,
1839 _bfd_elf_single_rel_hdr (sec)->sh_name));
1840 if (srel_name == NULL)
1841 return NULL;
1843 BFD_ASSERT ((CONST_STRNEQ (srel_name, ".rela")
1844 && strcmp (bfd_get_section_name (abfd, sec),
1845 srel_name+5) == 0)
1846 || (CONST_STRNEQ (srel_name, ".rel")
1847 && strcmp (bfd_get_section_name (abfd, sec),
1848 srel_name+4) == 0));
1850 dynobj = ia64_info->root.dynobj;
1851 if (!dynobj)
1852 ia64_info->root.dynobj = dynobj = abfd;
1854 srel = bfd_get_linker_section (dynobj, srel_name);
1855 if (srel == NULL && create)
1857 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
1858 (SEC_ALLOC | SEC_LOAD
1859 | SEC_HAS_CONTENTS
1860 | SEC_IN_MEMORY
1861 | SEC_LINKER_CREATED
1862 | SEC_READONLY));
1863 if (srel == NULL
1864 || !bfd_set_section_alignment (dynobj, srel, 3))
1865 return NULL;
1868 return srel;
1871 static bfd_boolean
1872 count_dyn_reloc (bfd *abfd, struct elf64_ia64_dyn_sym_info *dyn_i,
1873 asection *srel, int type)
1875 struct elf64_ia64_dyn_reloc_entry *rent;
1877 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
1878 if (rent->srel == srel && rent->type == type)
1879 break;
1881 if (!rent)
1883 rent = ((struct elf64_ia64_dyn_reloc_entry *)
1884 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
1885 if (!rent)
1886 return FALSE;
1888 rent->next = dyn_i->reloc_entries;
1889 rent->srel = srel;
1890 rent->type = type;
1891 rent->count = 0;
1892 dyn_i->reloc_entries = rent;
1894 rent->count++;
1896 return TRUE;
1899 static bfd_boolean
1900 elf64_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1901 asection *sec,
1902 const Elf_Internal_Rela *relocs)
1904 struct elf64_ia64_link_hash_table *ia64_info;
1905 const Elf_Internal_Rela *relend;
1906 Elf_Internal_Shdr *symtab_hdr;
1907 const Elf_Internal_Rela *rel;
1908 asection *got, *fptr, *srel, *pltoff;
1909 enum {
1910 NEED_GOT = 1,
1911 NEED_GOTX = 2,
1912 NEED_FPTR = 4,
1913 NEED_PLTOFF = 8,
1914 NEED_MIN_PLT = 16,
1915 NEED_FULL_PLT = 32,
1916 NEED_DYNREL = 64,
1917 NEED_LTOFF_FPTR = 128
1919 int need_entry;
1920 struct elf_link_hash_entry *h;
1921 unsigned long r_symndx;
1922 bfd_boolean maybe_dynamic;
1924 if (info->relocatable)
1925 return TRUE;
1927 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1928 ia64_info = elf64_ia64_hash_table (info);
1929 if (ia64_info == NULL)
1930 return FALSE;
1932 got = fptr = srel = pltoff = NULL;
1934 relend = relocs + sec->reloc_count;
1936 /* We scan relocations first to create dynamic relocation arrays. We
1937 modified get_dyn_sym_info to allow fast insertion and support fast
1938 lookup in the next loop. */
1939 for (rel = relocs; rel < relend; ++rel)
1941 r_symndx = ELF64_R_SYM (rel->r_info);
1942 if (r_symndx >= symtab_hdr->sh_info)
1944 long indx = r_symndx - symtab_hdr->sh_info;
1945 h = elf_sym_hashes (abfd)[indx];
1946 while (h->root.type == bfd_link_hash_indirect
1947 || h->root.type == bfd_link_hash_warning)
1948 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1950 else
1951 h = NULL;
1953 /* We can only get preliminary data on whether a symbol is
1954 locally or externally defined, as not all of the input files
1955 have yet been processed. Do something with what we know, as
1956 this may help reduce memory usage and processing time later. */
1957 maybe_dynamic = (h && ((!info->executable
1958 && (!SYMBOLIC_BIND (info, h)
1959 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
1960 || !h->def_regular
1961 || h->root.type == bfd_link_hash_defweak));
1963 need_entry = 0;
1964 switch (ELF64_R_TYPE (rel->r_info))
1966 case R_IA64_TPREL64MSB:
1967 case R_IA64_TPREL64LSB:
1968 case R_IA64_LTOFF_TPREL22:
1969 case R_IA64_DTPREL32MSB:
1970 case R_IA64_DTPREL32LSB:
1971 case R_IA64_DTPREL64MSB:
1972 case R_IA64_DTPREL64LSB:
1973 case R_IA64_LTOFF_DTPREL22:
1974 case R_IA64_DTPMOD64MSB:
1975 case R_IA64_DTPMOD64LSB:
1976 case R_IA64_LTOFF_DTPMOD22:
1977 abort ();
1978 break;
1980 case R_IA64_IPLTMSB:
1981 case R_IA64_IPLTLSB:
1982 break;
1984 case R_IA64_LTOFF_FPTR22:
1985 case R_IA64_LTOFF_FPTR64I:
1986 case R_IA64_LTOFF_FPTR32MSB:
1987 case R_IA64_LTOFF_FPTR32LSB:
1988 case R_IA64_LTOFF_FPTR64MSB:
1989 case R_IA64_LTOFF_FPTR64LSB:
1990 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
1991 break;
1993 case R_IA64_FPTR64I:
1994 case R_IA64_FPTR32MSB:
1995 case R_IA64_FPTR32LSB:
1996 case R_IA64_FPTR64MSB:
1997 case R_IA64_FPTR64LSB:
1998 if (info->shared || h)
1999 need_entry = NEED_FPTR | NEED_DYNREL;
2000 else
2001 need_entry = NEED_FPTR;
2002 break;
2004 case R_IA64_LTOFF22:
2005 case R_IA64_LTOFF64I:
2006 need_entry = NEED_GOT;
2007 break;
2009 case R_IA64_LTOFF22X:
2010 need_entry = NEED_GOTX;
2011 break;
2013 case R_IA64_PLTOFF22:
2014 case R_IA64_PLTOFF64I:
2015 case R_IA64_PLTOFF64MSB:
2016 case R_IA64_PLTOFF64LSB:
2017 need_entry = NEED_PLTOFF;
2018 if (h)
2020 if (maybe_dynamic)
2021 need_entry |= NEED_MIN_PLT;
2023 else
2025 (*info->callbacks->warning)
2026 (info, _("@pltoff reloc against local symbol"), 0,
2027 abfd, 0, (bfd_vma) 0);
2029 break;
2031 case R_IA64_PCREL21B:
2032 case R_IA64_PCREL60B:
2033 /* Depending on where this symbol is defined, we may or may not
2034 need a full plt entry. Only skip if we know we'll not need
2035 the entry -- static or symbolic, and the symbol definition
2036 has already been seen. */
2037 if (maybe_dynamic && rel->r_addend == 0)
2038 need_entry = NEED_FULL_PLT;
2039 break;
2041 case R_IA64_IMM14:
2042 case R_IA64_IMM22:
2043 case R_IA64_IMM64:
2044 case R_IA64_DIR32MSB:
2045 case R_IA64_DIR32LSB:
2046 case R_IA64_DIR64MSB:
2047 case R_IA64_DIR64LSB:
2048 /* Shared objects will always need at least a REL relocation. */
2049 if (info->shared || maybe_dynamic)
2050 need_entry = NEED_DYNREL;
2051 break;
2053 case R_IA64_PCREL22:
2054 case R_IA64_PCREL64I:
2055 case R_IA64_PCREL32MSB:
2056 case R_IA64_PCREL32LSB:
2057 case R_IA64_PCREL64MSB:
2058 case R_IA64_PCREL64LSB:
2059 if (maybe_dynamic)
2060 need_entry = NEED_DYNREL;
2061 break;
2064 if (!need_entry)
2065 continue;
2067 if ((need_entry & NEED_FPTR) != 0
2068 && rel->r_addend)
2070 (*info->callbacks->warning)
2071 (info, _("non-zero addend in @fptr reloc"), 0,
2072 abfd, 0, (bfd_vma) 0);
2075 if (get_dyn_sym_info (ia64_info, h, abfd, rel, TRUE) == NULL)
2076 return FALSE;
2079 /* Now, we only do lookup without insertion, which is very fast
2080 with the modified get_dyn_sym_info. */
2081 for (rel = relocs; rel < relend; ++rel)
2083 struct elf64_ia64_dyn_sym_info *dyn_i;
2084 int dynrel_type = R_IA64_NONE;
2086 r_symndx = ELF64_R_SYM (rel->r_info);
2087 if (r_symndx >= symtab_hdr->sh_info)
2089 /* We're dealing with a global symbol -- find its hash entry
2090 and mark it as being referenced. */
2091 long indx = r_symndx - symtab_hdr->sh_info;
2092 h = elf_sym_hashes (abfd)[indx];
2093 while (h->root.type == bfd_link_hash_indirect
2094 || h->root.type == bfd_link_hash_warning)
2095 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2097 /* PR15323, ref flags aren't set for references in the same
2098 object. */
2099 h->root.non_ir_ref = 1;
2100 h->ref_regular = 1;
2102 else
2103 h = NULL;
2105 /* We can only get preliminary data on whether a symbol is
2106 locally or externally defined, as not all of the input files
2107 have yet been processed. Do something with what we know, as
2108 this may help reduce memory usage and processing time later. */
2109 maybe_dynamic = (h && ((!info->executable
2110 && (!SYMBOLIC_BIND (info, h)
2111 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2112 || !h->def_regular
2113 || h->root.type == bfd_link_hash_defweak));
2115 need_entry = 0;
2116 switch (ELF64_R_TYPE (rel->r_info))
2118 case R_IA64_TPREL64MSB:
2119 case R_IA64_TPREL64LSB:
2120 case R_IA64_LTOFF_TPREL22:
2121 case R_IA64_DTPREL32MSB:
2122 case R_IA64_DTPREL32LSB:
2123 case R_IA64_DTPREL64MSB:
2124 case R_IA64_DTPREL64LSB:
2125 case R_IA64_LTOFF_DTPREL22:
2126 case R_IA64_DTPMOD64MSB:
2127 case R_IA64_DTPMOD64LSB:
2128 case R_IA64_LTOFF_DTPMOD22:
2129 abort ();
2130 break;
2132 case R_IA64_LTOFF_FPTR22:
2133 case R_IA64_LTOFF_FPTR64I:
2134 case R_IA64_LTOFF_FPTR32MSB:
2135 case R_IA64_LTOFF_FPTR32LSB:
2136 case R_IA64_LTOFF_FPTR64MSB:
2137 case R_IA64_LTOFF_FPTR64LSB:
2138 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2139 break;
2141 case R_IA64_FPTR64I:
2142 case R_IA64_FPTR32MSB:
2143 case R_IA64_FPTR32LSB:
2144 case R_IA64_FPTR64MSB:
2145 case R_IA64_FPTR64LSB:
2146 if (info->shared || h)
2147 need_entry = NEED_FPTR | NEED_DYNREL;
2148 else
2149 need_entry = NEED_FPTR;
2150 dynrel_type = R_IA64_FPTR64LSB;
2151 break;
2153 case R_IA64_LTOFF22:
2154 case R_IA64_LTOFF64I:
2155 need_entry = NEED_GOT;
2156 break;
2158 case R_IA64_LTOFF22X:
2159 need_entry = NEED_GOTX;
2160 break;
2162 case R_IA64_PLTOFF22:
2163 case R_IA64_PLTOFF64I:
2164 case R_IA64_PLTOFF64MSB:
2165 case R_IA64_PLTOFF64LSB:
2166 need_entry = NEED_PLTOFF;
2167 if (h)
2169 if (maybe_dynamic)
2170 need_entry |= NEED_MIN_PLT;
2172 break;
2174 case R_IA64_PCREL21B:
2175 case R_IA64_PCREL60B:
2176 /* Depending on where this symbol is defined, we may or may not
2177 need a full plt entry. Only skip if we know we'll not need
2178 the entry -- static or symbolic, and the symbol definition
2179 has already been seen. */
2180 if (maybe_dynamic && rel->r_addend == 0)
2181 need_entry = NEED_FULL_PLT;
2182 break;
2184 case R_IA64_IMM14:
2185 case R_IA64_IMM22:
2186 case R_IA64_IMM64:
2187 case R_IA64_DIR32MSB:
2188 case R_IA64_DIR32LSB:
2189 case R_IA64_DIR64MSB:
2190 case R_IA64_DIR64LSB:
2191 /* Shared objects will always need at least a REL relocation. */
2192 if (info->shared || maybe_dynamic)
2193 need_entry = NEED_DYNREL;
2194 dynrel_type = R_IA64_DIR64LSB;
2195 break;
2197 case R_IA64_IPLTMSB:
2198 case R_IA64_IPLTLSB:
2199 break;
2201 case R_IA64_PCREL22:
2202 case R_IA64_PCREL64I:
2203 case R_IA64_PCREL32MSB:
2204 case R_IA64_PCREL32LSB:
2205 case R_IA64_PCREL64MSB:
2206 case R_IA64_PCREL64LSB:
2207 if (maybe_dynamic)
2208 need_entry = NEED_DYNREL;
2209 dynrel_type = R_IA64_PCREL64LSB;
2210 break;
2213 if (!need_entry)
2214 continue;
2216 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, FALSE);
2218 /* Record whether or not this is a local symbol. */
2219 dyn_i->h = h;
2221 /* Create what's needed. */
2222 if (need_entry & (NEED_GOT | NEED_GOTX))
2224 if (!got)
2226 got = get_got (abfd, ia64_info);
2227 if (!got)
2228 return FALSE;
2230 if (need_entry & NEED_GOT)
2231 dyn_i->want_got = 1;
2232 if (need_entry & NEED_GOTX)
2233 dyn_i->want_gotx = 1;
2235 if (need_entry & NEED_FPTR)
2237 /* Create the .opd section. */
2238 if (!fptr)
2240 fptr = get_fptr (abfd, info, ia64_info);
2241 if (!fptr)
2242 return FALSE;
2244 dyn_i->want_fptr = 1;
2246 if (need_entry & NEED_LTOFF_FPTR)
2247 dyn_i->want_ltoff_fptr = 1;
2248 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
2250 if (!ia64_info->root.dynobj)
2251 ia64_info->root.dynobj = abfd;
2252 h->needs_plt = 1;
2253 dyn_i->want_plt = 1;
2255 if (need_entry & NEED_FULL_PLT)
2256 dyn_i->want_plt2 = 1;
2257 if (need_entry & NEED_PLTOFF)
2259 /* This is needed here, in case @pltoff is used in a non-shared
2260 link. */
2261 if (!pltoff)
2263 pltoff = get_pltoff (abfd, ia64_info);
2264 if (!pltoff)
2265 return FALSE;
2268 dyn_i->want_pltoff = 1;
2270 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
2272 if (!srel)
2274 srel = get_reloc_section (abfd, ia64_info, sec, TRUE);
2275 if (!srel)
2276 return FALSE;
2278 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type))
2279 return FALSE;
2283 return TRUE;
2286 /* For cleanliness, and potentially faster dynamic loading, allocate
2287 external GOT entries first. */
2289 static bfd_boolean
2290 allocate_global_data_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2291 void * data)
2293 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2295 if ((dyn_i->want_got || dyn_i->want_gotx)
2296 && ! dyn_i->want_fptr
2297 && elf64_ia64_dynamic_symbol_p (dyn_i->h))
2299 /* GOT entry with FPTR is done by allocate_global_fptr_got. */
2300 dyn_i->got_offset = x->ofs;
2301 x->ofs += 8;
2303 return TRUE;
2306 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
2308 static bfd_boolean
2309 allocate_global_fptr_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2310 void * data)
2312 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2314 if (dyn_i->want_got
2315 && dyn_i->want_fptr
2316 && elf64_ia64_dynamic_symbol_p (dyn_i->h))
2318 dyn_i->got_offset = x->ofs;
2319 x->ofs += 8;
2321 return TRUE;
2324 /* Lastly, allocate all the GOT entries for local data. */
2326 static bfd_boolean
2327 allocate_local_got (struct elf64_ia64_dyn_sym_info *dyn_i,
2328 void * data)
2330 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *) data;
2332 if ((dyn_i->want_got || dyn_i->want_gotx)
2333 && !elf64_ia64_dynamic_symbol_p (dyn_i->h))
2335 dyn_i->got_offset = x->ofs;
2336 x->ofs += 8;
2338 return TRUE;
2341 /* Allocate function descriptors. We can do these for every function
2342 in a main executable that is not exported. */
2344 static bfd_boolean
2345 allocate_fptr (struct elf64_ia64_dyn_sym_info *dyn_i, void * data)
2347 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *) data;
2349 if (dyn_i->want_fptr)
2351 struct elf_link_hash_entry *h = dyn_i->h;
2353 if (h)
2354 while (h->root.type == bfd_link_hash_indirect
2355 || h->root.type == bfd_link_hash_warning)
2356 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2358 if (h == NULL || !h->def_dynamic)
2360 /* A non dynamic symbol. */
2361 dyn_i->fptr_offset = x->ofs;
2362 x->ofs += 16;
2364 else
2365 dyn_i->want_fptr = 0;
2367 return TRUE;
2370 /* Allocate all the minimal PLT entries. */
2372 static bfd_boolean
2373 allocate_plt_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2374 void * data ATTRIBUTE_UNUSED)
2376 if (dyn_i->want_plt)
2378 struct elf_link_hash_entry *h = dyn_i->h;
2380 if (h)
2381 while (h->root.type == bfd_link_hash_indirect
2382 || h->root.type == bfd_link_hash_warning)
2383 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2385 /* ??? Versioned symbols seem to lose NEEDS_PLT. */
2386 if (elf64_ia64_dynamic_symbol_p (h))
2388 dyn_i->want_pltoff = 1;
2390 else
2392 dyn_i->want_plt = 0;
2393 dyn_i->want_plt2 = 0;
2396 return TRUE;
2399 /* Allocate all the full PLT entries. */
2401 static bfd_boolean
2402 allocate_plt2_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2403 void * data)
2405 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2407 if (dyn_i->want_plt2)
2409 struct elf_link_hash_entry *h = dyn_i->h;
2410 bfd_size_type ofs = x->ofs;
2412 dyn_i->plt2_offset = ofs;
2413 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
2415 while (h->root.type == bfd_link_hash_indirect
2416 || h->root.type == bfd_link_hash_warning)
2417 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2418 dyn_i->h->plt.offset = ofs;
2420 return TRUE;
2423 /* Allocate all the PLTOFF entries requested by relocations and
2424 plt entries. We can't share space with allocated FPTR entries,
2425 because the latter are not necessarily addressable by the GP.
2426 ??? Relaxation might be able to determine that they are. */
2428 static bfd_boolean
2429 allocate_pltoff_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2430 void * data)
2432 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2434 if (dyn_i->want_pltoff)
2436 dyn_i->pltoff_offset = x->ofs;
2437 x->ofs += 16;
2439 return TRUE;
2442 /* Allocate dynamic relocations for those symbols that turned out
2443 to be dynamic. */
2445 static bfd_boolean
2446 allocate_dynrel_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
2447 void * data)
2449 struct elf64_ia64_allocate_data *x = (struct elf64_ia64_allocate_data *)data;
2450 struct elf64_ia64_link_hash_table *ia64_info;
2451 struct elf64_ia64_dyn_reloc_entry *rent;
2452 bfd_boolean dynamic_symbol, shared, resolved_zero;
2453 struct elf64_ia64_link_hash_entry *h_ia64;
2455 ia64_info = elf64_ia64_hash_table (x->info);
2456 if (ia64_info == NULL)
2457 return FALSE;
2459 /* Note that this can't be used in relation to FPTR relocs below. */
2460 dynamic_symbol = elf64_ia64_dynamic_symbol_p (dyn_i->h);
2462 shared = x->info->shared;
2463 resolved_zero = (dyn_i->h
2464 && ELF_ST_VISIBILITY (dyn_i->h->other)
2465 && dyn_i->h->root.type == bfd_link_hash_undefweak);
2467 /* Take care of the GOT and PLT relocations. */
2469 if ((!resolved_zero
2470 && (dynamic_symbol || shared)
2471 && (dyn_i->want_got || dyn_i->want_gotx))
2472 || (dyn_i->want_ltoff_fptr
2473 && dyn_i->h
2474 && dyn_i->h->def_dynamic))
2476 /* VMS: FIX64. */
2477 if (dyn_i->h != NULL && dyn_i->h->def_dynamic)
2479 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2480 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2481 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2482 ia64_info->fixups_sec->size +=
2483 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2487 if (ia64_info->rel_fptr_sec && dyn_i->want_fptr)
2489 /* VMS: only image reloc. */
2490 if (dyn_i->h == NULL || dyn_i->h->root.type != bfd_link_hash_undefweak)
2491 ia64_info->rel_fptr_sec->size += sizeof (Elf64_External_Rela);
2494 if (!resolved_zero && dyn_i->want_pltoff)
2496 /* VMS: FIXFD. */
2497 if (dyn_i->h != NULL && dyn_i->h->def_dynamic)
2499 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2500 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2501 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2502 ia64_info->fixups_sec->size +=
2503 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2507 /* Take care of the normal data relocations. */
2509 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2511 int count = rent->count;
2513 switch (rent->type)
2515 case R_IA64_FPTR32LSB:
2516 case R_IA64_FPTR64LSB:
2517 /* Allocate one iff !want_fptr and not PIE, which by this point
2518 will be true only if we're actually allocating one statically
2519 in the main executable. Position independent executables
2520 need a relative reloc. */
2521 if (dyn_i->want_fptr && !x->info->pie)
2522 continue;
2523 break;
2524 case R_IA64_PCREL32LSB:
2525 case R_IA64_PCREL64LSB:
2526 if (!dynamic_symbol)
2527 continue;
2528 break;
2529 case R_IA64_DIR32LSB:
2530 case R_IA64_DIR64LSB:
2531 if (!dynamic_symbol && !shared)
2532 continue;
2533 break;
2534 case R_IA64_IPLTLSB:
2535 if (!dynamic_symbol && !shared)
2536 continue;
2537 /* Use two REL relocations for IPLT relocations
2538 against local symbols. */
2539 if (!dynamic_symbol)
2540 count *= 2;
2541 break;
2542 case R_IA64_DTPREL32LSB:
2543 case R_IA64_TPREL64LSB:
2544 case R_IA64_DTPREL64LSB:
2545 case R_IA64_DTPMOD64LSB:
2546 break;
2547 default:
2548 abort ();
2551 /* Add a fixup. */
2552 if (!dynamic_symbol)
2553 abort ();
2555 h_ia64 = (struct elf64_ia64_link_hash_entry *) dyn_i->h;
2556 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2557 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2558 ia64_info->fixups_sec->size +=
2559 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2562 return TRUE;
2565 static bfd_boolean
2566 elf64_ia64_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2567 struct elf_link_hash_entry *h)
2569 /* ??? Undefined symbols with PLT entries should be re-defined
2570 to be the PLT entry. */
2572 /* If this is a weak symbol, and there is a real definition, the
2573 processor independent code will have arranged for us to see the
2574 real definition first, and we can just use the same value. */
2575 if (h->u.weakdef != NULL)
2577 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2578 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2579 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2580 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2581 return TRUE;
2584 /* If this is a reference to a symbol defined by a dynamic object which
2585 is not a function, we might allocate the symbol in our .dynbss section
2586 and allocate a COPY dynamic relocation.
2588 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
2589 of hackery. */
2591 return TRUE;
2594 static bfd_boolean
2595 elf64_ia64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2596 struct bfd_link_info *info)
2598 struct elf64_ia64_allocate_data data;
2599 struct elf64_ia64_link_hash_table *ia64_info;
2600 asection *sec;
2601 bfd *dynobj;
2602 struct elf_link_hash_table *hash_table;
2604 hash_table = elf_hash_table (info);
2605 dynobj = hash_table->dynobj;
2606 ia64_info = elf64_ia64_hash_table (info);
2607 if (ia64_info == NULL)
2608 return FALSE;
2609 BFD_ASSERT(dynobj != NULL);
2610 data.info = info;
2612 /* Allocate the GOT entries. */
2614 if (ia64_info->root.sgot)
2616 data.ofs = 0;
2617 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
2618 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
2619 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
2620 ia64_info->root.sgot->size = data.ofs;
2623 /* Allocate the FPTR entries. */
2625 if (ia64_info->fptr_sec)
2627 data.ofs = 0;
2628 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
2629 ia64_info->fptr_sec->size = data.ofs;
2632 /* Now that we've seen all of the input files, we can decide which
2633 symbols need plt entries. Allocate the minimal PLT entries first.
2634 We do this even though dynamic_sections_created may be FALSE, because
2635 this has the side-effect of clearing want_plt and want_plt2. */
2637 data.ofs = 0;
2638 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
2640 /* Align the pointer for the plt2 entries. */
2641 data.ofs = (data.ofs + 31) & (bfd_vma) -32;
2643 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
2644 if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
2646 /* FIXME: we always reserve the memory for dynamic linker even if
2647 there are no PLT entries since dynamic linker may assume the
2648 reserved memory always exists. */
2650 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
2652 ia64_info->root.splt->size = data.ofs;
2655 /* Allocate the PLTOFF entries. */
2657 if (ia64_info->pltoff_sec)
2659 data.ofs = 0;
2660 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
2661 ia64_info->pltoff_sec->size = data.ofs;
2664 if (ia64_info->root.dynamic_sections_created)
2666 /* Allocate space for the dynamic relocations that turned out to be
2667 required. */
2668 elf64_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
2671 /* We have now determined the sizes of the various dynamic sections.
2672 Allocate memory for them. */
2673 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2675 bfd_boolean strip;
2677 if (!(sec->flags & SEC_LINKER_CREATED))
2678 continue;
2680 /* If we don't need this section, strip it from the output file.
2681 There were several sections primarily related to dynamic
2682 linking that must be create before the linker maps input
2683 sections to output sections. The linker does that before
2684 bfd_elf_size_dynamic_sections is called, and it is that
2685 function which decides whether anything needs to go into
2686 these sections. */
2688 strip = (sec->size == 0);
2690 if (sec == ia64_info->root.sgot)
2691 strip = FALSE;
2692 else if (sec == ia64_info->root.srelgot)
2694 if (strip)
2695 ia64_info->root.srelgot = NULL;
2696 else
2697 /* We use the reloc_count field as a counter if we need to
2698 copy relocs into the output file. */
2699 sec->reloc_count = 0;
2701 else if (sec == ia64_info->fptr_sec)
2703 if (strip)
2704 ia64_info->fptr_sec = NULL;
2706 else if (sec == ia64_info->rel_fptr_sec)
2708 if (strip)
2709 ia64_info->rel_fptr_sec = NULL;
2710 else
2711 /* We use the reloc_count field as a counter if we need to
2712 copy relocs into the output file. */
2713 sec->reloc_count = 0;
2715 else if (sec == ia64_info->root.splt)
2717 if (strip)
2718 ia64_info->root.splt = NULL;
2720 else if (sec == ia64_info->pltoff_sec)
2722 if (strip)
2723 ia64_info->pltoff_sec = NULL;
2725 else if (sec == ia64_info->fixups_sec)
2727 if (strip)
2728 ia64_info->fixups_sec = NULL;
2730 else if (sec == ia64_info->transfer_sec)
2734 else
2736 const char *name;
2738 /* It's OK to base decisions on the section name, because none
2739 of the dynobj section names depend upon the input files. */
2740 name = bfd_get_section_name (dynobj, sec);
2742 if (strcmp (name, ".got.plt") == 0)
2743 strip = FALSE;
2744 else if (CONST_STRNEQ (name, ".rel"))
2746 if (!strip)
2748 /* We use the reloc_count field as a counter if we need to
2749 copy relocs into the output file. */
2750 sec->reloc_count = 0;
2753 else
2754 continue;
2757 if (strip)
2758 sec->flags |= SEC_EXCLUDE;
2759 else
2761 /* Allocate memory for the section contents. */
2762 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
2763 if (sec->contents == NULL && sec->size != 0)
2764 return FALSE;
2768 if (elf_hash_table (info)->dynamic_sections_created)
2770 bfd *abfd;
2771 asection *dynsec;
2772 asection *dynstrsec;
2773 Elf_Internal_Dyn dyn;
2774 const struct elf_backend_data *bed;
2775 unsigned int shl_num = 0;
2776 bfd_vma fixups_off = 0;
2777 bfd_vma strdyn_off;
2778 unsigned int time_hi, time_lo;
2780 /* The .dynamic section must exist and be empty. */
2781 dynsec = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
2782 BFD_ASSERT (dynsec != NULL);
2783 BFD_ASSERT (dynsec->size == 0);
2785 dynstrsec = bfd_get_linker_section (hash_table->dynobj, ".vmsdynstr");
2786 BFD_ASSERT (dynstrsec != NULL);
2787 BFD_ASSERT (dynstrsec->size == 0);
2788 dynstrsec->size = 1; /* Initial blank. */
2790 /* Ident + link time. */
2791 vms_get_time (&time_hi, &time_lo);
2793 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_IDENT, 0))
2794 return FALSE;
2795 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_LINKTIME,
2796 (((bfd_uint64_t)time_hi) << 32)
2797 + time_lo))
2798 return FALSE;
2800 /* Strtab. */
2801 strdyn_off = dynsec->size;
2802 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_STRTAB_OFFSET, 0))
2803 return FALSE;
2804 if (!_bfd_elf_add_dynamic_entry (info, DT_STRSZ, 0))
2805 return FALSE;
2807 /* PLTGOT */
2808 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_PLTGOT_SEG, 0))
2809 return FALSE;
2810 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_PLTGOT_OFFSET, 0))
2811 return FALSE;
2813 /* Misc. */
2814 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FPMODE, 0x9800000))
2815 return FALSE;
2816 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_LNKFLAGS,
2817 VMS_LF_IMGSTA | VMS_LF_MAIN))
2818 return FALSE;
2820 /* Add entries for shared libraries. */
2821 for (abfd = info->input_bfds; abfd; abfd = abfd->link_next)
2823 char *soname;
2824 size_t soname_len;
2825 bfd_size_type strindex;
2826 bfd_byte *newcontents;
2827 bfd_vma fixups_shl_off;
2829 if (!(abfd->flags & DYNAMIC))
2830 continue;
2831 BFD_ASSERT (abfd->xvec == output_bfd->xvec);
2833 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_NEEDED_IDENT,
2834 elf_ia64_vms_ident (abfd)))
2835 return FALSE;
2837 soname = vms_get_module_name (abfd->filename, TRUE);
2838 if (soname == NULL)
2839 return FALSE;
2840 strindex = dynstrsec->size;
2841 soname_len = strlen (soname) + 1;
2842 newcontents = (bfd_byte *) bfd_realloc (dynstrsec->contents,
2843 strindex + soname_len);
2844 if (newcontents == NULL)
2845 return FALSE;
2846 memcpy (newcontents + strindex, soname, soname_len);
2847 dynstrsec->size += soname_len;
2848 dynstrsec->contents = newcontents;
2850 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2851 return FALSE;
2853 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FIXUP_NEEDED,
2854 shl_num))
2855 return FALSE;
2856 shl_num++;
2858 /* The fixups_off was in fact containing the size of the fixup
2859 section. Remap into the offset. */
2860 fixups_shl_off = elf_ia64_vms_tdata (abfd)->fixups_off;
2861 elf_ia64_vms_tdata (abfd)->fixups_off = fixups_off;
2863 if (!_bfd_elf_add_dynamic_entry
2864 (info, DT_IA_64_VMS_FIXUP_RELA_CNT,
2865 fixups_shl_off / sizeof (Elf64_External_VMS_IMAGE_FIXUP)))
2866 return FALSE;
2867 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_FIXUP_RELA_OFF,
2868 fixups_off))
2869 return FALSE;
2870 fixups_off += fixups_shl_off;
2873 /* Unwind. */
2874 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWINDSZ, 0))
2875 return FALSE;
2876 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_CODSEG, 0))
2877 return FALSE;
2878 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_INFOSEG, 0))
2879 return FALSE;
2880 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_OFFSET, 0))
2881 return FALSE;
2882 if (!_bfd_elf_add_dynamic_entry (info, DT_IA_64_VMS_UNWIND_SEG, 0))
2883 return FALSE;
2885 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0xdead))
2886 return FALSE;
2888 /* Fix the strtab entries. */
2889 bed = get_elf_backend_data (hash_table->dynobj);
2891 if (dynstrsec->size > 1)
2892 dynstrsec->contents[0] = 0;
2893 else
2894 dynstrsec->size = 0;
2896 /* Note: one 'spare' (ie DT_NULL) entry is added by
2897 bfd_elf_size_dynsym_hash_dynstr. */
2898 dyn.d_tag = DT_IA_64_VMS_STRTAB_OFFSET;
2899 dyn.d_un.d_val = dynsec->size /* + sizeof (Elf64_External_Dyn) */;
2900 bed->s->swap_dyn_out (hash_table->dynobj, &dyn,
2901 dynsec->contents + strdyn_off);
2903 dyn.d_tag = DT_STRSZ;
2904 dyn.d_un.d_val = dynstrsec->size;
2905 bed->s->swap_dyn_out (hash_table->dynobj, &dyn,
2906 dynsec->contents + strdyn_off + bed->s->sizeof_dyn);
2908 elf_ia64_vms_tdata (output_bfd)->needed_count = shl_num;
2910 /* Note section. */
2911 if (!create_ia64_vms_notes (output_bfd, info, time_hi, time_lo))
2912 return FALSE;
2915 /* ??? Perhaps force __gp local. */
2917 return TRUE;
2920 static void
2921 elf64_ia64_install_fixup (bfd *output_bfd,
2922 struct elf64_ia64_link_hash_table *ia64_info,
2923 struct elf_link_hash_entry *h,
2924 unsigned int type, asection *sec, bfd_vma offset,
2925 bfd_vma addend)
2927 asection *relsec;
2928 Elf64_External_VMS_IMAGE_FIXUP *fixup;
2929 struct elf64_ia64_link_hash_entry *h_ia64;
2930 bfd_vma fixoff;
2931 Elf_Internal_Phdr *phdr;
2933 if (h == NULL || !h->def_dynamic)
2934 abort ();
2936 h_ia64 = (struct elf64_ia64_link_hash_entry *) h;
2937 fixoff = elf_ia64_vms_tdata (h_ia64->shl)->fixups_off;
2938 elf_ia64_vms_tdata (h_ia64->shl)->fixups_off +=
2939 sizeof (Elf64_External_VMS_IMAGE_FIXUP);
2940 relsec = ia64_info->fixups_sec;
2942 fixup = (Elf64_External_VMS_IMAGE_FIXUP *)(relsec->contents + fixoff);
2943 offset += sec->output_section->vma + sec->output_offset;
2945 /* FIXME: this is slow. We should cache the last one used, or create a
2946 map. */
2947 phdr = _bfd_elf_find_segment_containing_section
2948 (output_bfd, sec->output_section);
2949 BFD_ASSERT (phdr != NULL);
2951 bfd_putl64 (offset - phdr->p_vaddr, fixup->fixup_offset);
2952 bfd_putl32 (type, fixup->type);
2953 bfd_putl32 (phdr - elf_tdata (output_bfd)->phdr, fixup->fixup_seg);
2954 bfd_putl64 (addend, fixup->addend);
2955 bfd_putl32 (h->root.u.def.value, fixup->symvec_index);
2956 bfd_putl32 (2, fixup->data_type);
2959 /* Store an entry for target address TARGET_ADDR in the linkage table
2960 and return the gp-relative address of the linkage table entry. */
2962 static bfd_vma
2963 set_got_entry (bfd *abfd, struct bfd_link_info *info,
2964 struct elf64_ia64_dyn_sym_info *dyn_i,
2965 bfd_vma addend, bfd_vma value, unsigned int dyn_r_type)
2967 struct elf64_ia64_link_hash_table *ia64_info;
2968 asection *got_sec;
2969 bfd_boolean done;
2970 bfd_vma got_offset;
2972 ia64_info = elf64_ia64_hash_table (info);
2973 if (ia64_info == NULL)
2974 return 0;
2976 got_sec = ia64_info->root.sgot;
2978 switch (dyn_r_type)
2980 case R_IA64_TPREL64LSB:
2981 case R_IA64_DTPMOD64LSB:
2982 case R_IA64_DTPREL32LSB:
2983 case R_IA64_DTPREL64LSB:
2984 abort ();
2985 break;
2986 default:
2987 done = dyn_i->got_done;
2988 dyn_i->got_done = TRUE;
2989 got_offset = dyn_i->got_offset;
2990 break;
2993 BFD_ASSERT ((got_offset & 7) == 0);
2995 if (! done)
2997 /* Store the target address in the linkage table entry. */
2998 bfd_put_64 (abfd, value, got_sec->contents + got_offset);
3000 /* Install a dynamic relocation if needed. */
3001 if (((info->shared
3002 && (!dyn_i->h
3003 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3004 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3005 || elf64_ia64_dynamic_symbol_p (dyn_i->h))
3006 && (!dyn_i->want_ltoff_fptr
3007 || !info->pie
3008 || !dyn_i->h
3009 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3011 if (!dyn_i->h || !dyn_i->h->def_dynamic)
3013 dyn_r_type = R_IA64_REL64LSB;
3014 addend = value;
3017 /* VMS: install a FIX32 or FIX64. */
3018 switch (dyn_r_type)
3020 case R_IA64_DIR32LSB:
3021 case R_IA64_FPTR32LSB:
3022 dyn_r_type = R_IA64_VMS_FIX32;
3023 break;
3024 case R_IA64_DIR64LSB:
3025 case R_IA64_FPTR64LSB:
3026 dyn_r_type = R_IA64_VMS_FIX64;
3027 break;
3028 default:
3029 BFD_ASSERT (FALSE);
3030 break;
3032 elf64_ia64_install_fixup
3033 (info->output_bfd, ia64_info, dyn_i->h,
3034 dyn_r_type, got_sec, got_offset, addend);
3038 /* Return the address of the linkage table entry. */
3039 value = (got_sec->output_section->vma
3040 + got_sec->output_offset
3041 + got_offset);
3043 return value;
3046 /* Fill in a function descriptor consisting of the function's code
3047 address and its global pointer. Return the descriptor's address. */
3049 static bfd_vma
3050 set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
3051 struct elf64_ia64_dyn_sym_info *dyn_i,
3052 bfd_vma value)
3054 struct elf64_ia64_link_hash_table *ia64_info;
3055 asection *fptr_sec;
3057 ia64_info = elf64_ia64_hash_table (info);
3058 if (ia64_info == NULL)
3059 return 0;
3061 fptr_sec = ia64_info->fptr_sec;
3063 if (!dyn_i->fptr_done)
3065 dyn_i->fptr_done = 1;
3067 /* Fill in the function descriptor. */
3068 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
3069 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
3070 fptr_sec->contents + dyn_i->fptr_offset + 8);
3073 /* Return the descriptor's address. */
3074 value = (fptr_sec->output_section->vma
3075 + fptr_sec->output_offset
3076 + dyn_i->fptr_offset);
3078 return value;
3081 /* Fill in a PLTOFF entry consisting of the function's code address
3082 and its global pointer. Return the descriptor's address. */
3084 static bfd_vma
3085 set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
3086 struct elf64_ia64_dyn_sym_info *dyn_i,
3087 bfd_vma value, bfd_boolean is_plt)
3089 struct elf64_ia64_link_hash_table *ia64_info;
3090 asection *pltoff_sec;
3092 ia64_info = elf64_ia64_hash_table (info);
3093 if (ia64_info == NULL)
3094 return 0;
3096 pltoff_sec = ia64_info->pltoff_sec;
3098 /* Don't do anything if this symbol uses a real PLT entry. In
3099 that case, we'll fill this in during finish_dynamic_symbol. */
3100 if ((! dyn_i->want_plt || is_plt)
3101 && !dyn_i->pltoff_done)
3103 bfd_vma gp = _bfd_get_gp_value (abfd);
3105 /* Fill in the function descriptor. */
3106 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
3107 bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
3109 /* Install dynamic relocations if needed. */
3110 if (!is_plt
3111 && info->shared
3112 && (!dyn_i->h
3113 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3114 || dyn_i->h->root.type != bfd_link_hash_undefweak))
3116 /* VMS: */
3117 abort ();
3120 dyn_i->pltoff_done = 1;
3123 /* Return the descriptor's address. */
3124 value = (pltoff_sec->output_section->vma
3125 + pltoff_sec->output_offset
3126 + dyn_i->pltoff_offset);
3128 return value;
3131 /* Called through qsort to sort the .IA_64.unwind section during a
3132 non-relocatable link. Set elf64_ia64_unwind_entry_compare_bfd
3133 to the output bfd so we can do proper endianness frobbing. */
3135 static bfd *elf64_ia64_unwind_entry_compare_bfd;
3137 static int
3138 elf64_ia64_unwind_entry_compare (const void * a, const void * b)
3140 bfd_vma av, bv;
3142 av = bfd_get_64 (elf64_ia64_unwind_entry_compare_bfd, a);
3143 bv = bfd_get_64 (elf64_ia64_unwind_entry_compare_bfd, b);
3145 return (av < bv ? -1 : av > bv ? 1 : 0);
3148 /* Make sure we've got ourselves a nice fat __gp value. */
3149 static bfd_boolean
3150 elf64_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info, bfd_boolean final)
3152 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3153 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3154 struct elf_link_hash_entry *gp;
3155 bfd_vma gp_val;
3156 asection *os;
3157 struct elf64_ia64_link_hash_table *ia64_info;
3159 ia64_info = elf64_ia64_hash_table (info);
3160 if (ia64_info == NULL)
3161 return FALSE;
3163 /* Find the min and max vma of all sections marked short. Also collect
3164 min and max vma of any type, for use in selecting a nice gp. */
3165 for (os = abfd->sections; os ; os = os->next)
3167 bfd_vma lo, hi;
3169 if ((os->flags & SEC_ALLOC) == 0)
3170 continue;
3172 lo = os->vma;
3173 /* When this function is called from elfNN_ia64_final_link
3174 the correct value to use is os->size. When called from
3175 elfNN_ia64_relax_section we are in the middle of section
3176 sizing; some sections will already have os->size set, others
3177 will have os->size zero and os->rawsize the previous size. */
3178 hi = os->vma + (!final && os->rawsize ? os->rawsize : os->size);
3179 if (hi < lo)
3180 hi = (bfd_vma) -1;
3182 if (min_vma > lo)
3183 min_vma = lo;
3184 if (max_vma < hi)
3185 max_vma = hi;
3186 if (os->flags & SEC_SMALL_DATA)
3188 if (min_short_vma > lo)
3189 min_short_vma = lo;
3190 if (max_short_vma < hi)
3191 max_short_vma = hi;
3195 if (ia64_info->min_short_sec)
3197 if (min_short_vma
3198 > (ia64_info->min_short_sec->vma
3199 + ia64_info->min_short_offset))
3200 min_short_vma = (ia64_info->min_short_sec->vma
3201 + ia64_info->min_short_offset);
3202 if (max_short_vma
3203 < (ia64_info->max_short_sec->vma
3204 + ia64_info->max_short_offset))
3205 max_short_vma = (ia64_info->max_short_sec->vma
3206 + ia64_info->max_short_offset);
3209 /* See if the user wants to force a value. */
3210 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
3211 FALSE, FALSE);
3213 if (gp
3214 && (gp->root.type == bfd_link_hash_defined
3215 || gp->root.type == bfd_link_hash_defweak))
3217 asection *gp_sec = gp->root.u.def.section;
3218 gp_val = (gp->root.u.def.value
3219 + gp_sec->output_section->vma
3220 + gp_sec->output_offset);
3222 else
3224 /* Pick a sensible value. */
3226 if (ia64_info->min_short_sec)
3228 bfd_vma short_range = max_short_vma - min_short_vma;
3230 /* If min_short_sec is set, pick one in the middle bewteen
3231 min_short_vma and max_short_vma. */
3232 if (short_range >= 0x400000)
3233 goto overflow;
3234 gp_val = min_short_vma + short_range / 2;
3236 else
3238 asection *got_sec = ia64_info->root.sgot;
3240 /* Start with just the address of the .got. */
3241 if (got_sec)
3242 gp_val = got_sec->output_section->vma;
3243 else if (max_short_vma != 0)
3244 gp_val = min_short_vma;
3245 else if (max_vma - min_vma < 0x200000)
3246 gp_val = min_vma;
3247 else
3248 gp_val = max_vma - 0x200000 + 8;
3251 /* If it is possible to address the entire image, but we
3252 don't with the choice above, adjust. */
3253 if (max_vma - min_vma < 0x400000
3254 && (max_vma - gp_val >= 0x200000
3255 || gp_val - min_vma > 0x200000))
3256 gp_val = min_vma + 0x200000;
3257 else if (max_short_vma != 0)
3259 /* If we don't cover all the short data, adjust. */
3260 if (max_short_vma - gp_val >= 0x200000)
3261 gp_val = min_short_vma + 0x200000;
3263 /* If we're addressing stuff past the end, adjust back. */
3264 if (gp_val > max_vma)
3265 gp_val = max_vma - 0x200000 + 8;
3269 /* Validate whether all SHF_IA_64_SHORT sections are within
3270 range of the chosen GP. */
3272 if (max_short_vma != 0)
3274 if (max_short_vma - min_short_vma >= 0x400000)
3276 overflow:
3277 (*_bfd_error_handler)
3278 (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
3279 bfd_get_filename (abfd),
3280 (unsigned long) (max_short_vma - min_short_vma));
3281 return FALSE;
3283 else if ((gp_val > min_short_vma
3284 && gp_val - min_short_vma > 0x200000)
3285 || (gp_val < max_short_vma
3286 && max_short_vma - gp_val >= 0x200000))
3288 (*_bfd_error_handler)
3289 (_("%s: __gp does not cover short data segment"),
3290 bfd_get_filename (abfd));
3291 return FALSE;
3295 _bfd_set_gp_value (abfd, gp_val);
3297 return TRUE;
3300 static bfd_boolean
3301 elf64_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
3303 struct elf64_ia64_link_hash_table *ia64_info;
3304 asection *unwind_output_sec;
3306 ia64_info = elf64_ia64_hash_table (info);
3307 if (ia64_info == NULL)
3308 return FALSE;
3310 /* Make sure we've got ourselves a nice fat __gp value. */
3311 if (!info->relocatable)
3313 bfd_vma gp_val;
3314 struct elf_link_hash_entry *gp;
3316 /* We assume after gp is set, section size will only decrease. We
3317 need to adjust gp for it. */
3318 _bfd_set_gp_value (abfd, 0);
3319 if (! elf64_ia64_choose_gp (abfd, info, TRUE))
3320 return FALSE;
3321 gp_val = _bfd_get_gp_value (abfd);
3323 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
3324 FALSE, FALSE);
3325 if (gp)
3327 gp->root.type = bfd_link_hash_defined;
3328 gp->root.u.def.value = gp_val;
3329 gp->root.u.def.section = bfd_abs_section_ptr;
3333 /* If we're producing a final executable, we need to sort the contents
3334 of the .IA_64.unwind section. Force this section to be relocated
3335 into memory rather than written immediately to the output file. */
3336 unwind_output_sec = NULL;
3337 if (!info->relocatable)
3339 asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3340 if (s)
3342 unwind_output_sec = s->output_section;
3343 unwind_output_sec->contents
3344 = bfd_malloc (unwind_output_sec->size);
3345 if (unwind_output_sec->contents == NULL)
3346 return FALSE;
3350 /* Invoke the regular ELF backend linker to do all the work. */
3351 if (!bfd_elf_final_link (abfd, info))
3352 return FALSE;
3354 if (unwind_output_sec)
3356 elf64_ia64_unwind_entry_compare_bfd = abfd;
3357 qsort (unwind_output_sec->contents,
3358 (size_t) (unwind_output_sec->size / 24),
3360 elf64_ia64_unwind_entry_compare);
3362 if (! bfd_set_section_contents (abfd, unwind_output_sec,
3363 unwind_output_sec->contents, (bfd_vma) 0,
3364 unwind_output_sec->size))
3365 return FALSE;
3368 return TRUE;
3371 static bfd_boolean
3372 elf64_ia64_relocate_section (bfd *output_bfd,
3373 struct bfd_link_info *info,
3374 bfd *input_bfd,
3375 asection *input_section,
3376 bfd_byte *contents,
3377 Elf_Internal_Rela *relocs,
3378 Elf_Internal_Sym *local_syms,
3379 asection **local_sections)
3381 struct elf64_ia64_link_hash_table *ia64_info;
3382 Elf_Internal_Shdr *symtab_hdr;
3383 Elf_Internal_Rela *rel;
3384 Elf_Internal_Rela *relend;
3385 bfd_boolean ret_val = TRUE; /* for non-fatal errors */
3386 bfd_vma gp_val;
3388 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3389 ia64_info = elf64_ia64_hash_table (info);
3390 if (ia64_info == NULL)
3391 return FALSE;
3393 /* Infect various flags from the input section to the output section. */
3394 if (info->relocatable)
3396 bfd_vma flags;
3398 flags = elf_section_data(input_section)->this_hdr.sh_flags;
3399 flags &= SHF_IA_64_NORECOV;
3401 elf_section_data(input_section->output_section)
3402 ->this_hdr.sh_flags |= flags;
3405 gp_val = _bfd_get_gp_value (output_bfd);
3407 rel = relocs;
3408 relend = relocs + input_section->reloc_count;
3409 for (; rel < relend; ++rel)
3411 struct elf_link_hash_entry *h;
3412 struct elf64_ia64_dyn_sym_info *dyn_i;
3413 bfd_reloc_status_type r;
3414 reloc_howto_type *howto;
3415 unsigned long r_symndx;
3416 Elf_Internal_Sym *sym;
3417 unsigned int r_type;
3418 bfd_vma value;
3419 asection *sym_sec;
3420 bfd_byte *hit_addr;
3421 bfd_boolean dynamic_symbol_p;
3422 bfd_boolean undef_weak_ref;
3424 r_type = ELF64_R_TYPE (rel->r_info);
3425 if (r_type > R_IA64_MAX_RELOC_CODE)
3427 (*_bfd_error_handler)
3428 (_("%B: unknown relocation type %d"),
3429 input_bfd, (int) r_type);
3430 bfd_set_error (bfd_error_bad_value);
3431 ret_val = FALSE;
3432 continue;
3435 howto = ia64_elf_lookup_howto (r_type);
3436 r_symndx = ELF64_R_SYM (rel->r_info);
3437 h = NULL;
3438 sym = NULL;
3439 sym_sec = NULL;
3440 undef_weak_ref = FALSE;
3442 if (r_symndx < symtab_hdr->sh_info)
3444 /* Reloc against local symbol. */
3445 asection *msec;
3446 sym = local_syms + r_symndx;
3447 sym_sec = local_sections[r_symndx];
3448 msec = sym_sec;
3449 value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
3450 if (!info->relocatable
3451 && (sym_sec->flags & SEC_MERGE) != 0
3452 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
3453 && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3455 struct elf64_ia64_local_hash_entry *loc_h;
3457 loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, FALSE);
3458 if (loc_h && ! loc_h->sec_merge_done)
3460 struct elf64_ia64_dyn_sym_info *dynent;
3461 unsigned int count;
3463 for (count = loc_h->count, dynent = loc_h->info;
3464 count != 0;
3465 count--, dynent++)
3467 msec = sym_sec;
3468 dynent->addend =
3469 _bfd_merged_section_offset (output_bfd, &msec,
3470 elf_section_data (msec)->
3471 sec_info,
3472 sym->st_value
3473 + dynent->addend);
3474 dynent->addend -= sym->st_value;
3475 dynent->addend += msec->output_section->vma
3476 + msec->output_offset
3477 - sym_sec->output_section->vma
3478 - sym_sec->output_offset;
3481 /* We may have introduced duplicated entries. We need
3482 to remove them properly. */
3483 count = sort_dyn_sym_info (loc_h->info, loc_h->count);
3484 if (count != loc_h->count)
3486 loc_h->count = count;
3487 loc_h->sorted_count = count;
3490 loc_h->sec_merge_done = 1;
3494 else
3496 bfd_boolean unresolved_reloc;
3497 bfd_boolean warned, ignored;
3498 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3500 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3501 r_symndx, symtab_hdr, sym_hashes,
3502 h, sym_sec, value,
3503 unresolved_reloc, warned, ignored);
3505 if (h->root.type == bfd_link_hash_undefweak)
3506 undef_weak_ref = TRUE;
3507 else if (warned)
3508 continue;
3511 /* For relocs against symbols from removed linkonce sections,
3512 or sections discarded by a linker script, we just want the
3513 section contents zeroed. Avoid any special processing. */
3514 if (sym_sec != NULL && discarded_section (sym_sec))
3515 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3516 rel, 1, relend, howto, 0, contents);
3518 if (info->relocatable)
3519 continue;
3521 hit_addr = contents + rel->r_offset;
3522 value += rel->r_addend;
3523 dynamic_symbol_p = elf64_ia64_dynamic_symbol_p (h);
3525 switch (r_type)
3527 case R_IA64_NONE:
3528 case R_IA64_LDXMOV:
3529 continue;
3531 case R_IA64_IMM14:
3532 case R_IA64_IMM22:
3533 case R_IA64_IMM64:
3534 case R_IA64_DIR32MSB:
3535 case R_IA64_DIR32LSB:
3536 case R_IA64_DIR64MSB:
3537 case R_IA64_DIR64LSB:
3538 /* Install a dynamic relocation for this reloc. */
3539 if ((dynamic_symbol_p || info->shared)
3540 && r_symndx != 0
3541 && (input_section->flags & SEC_ALLOC) != 0)
3543 unsigned int dyn_r_type;
3544 bfd_vma addend;
3546 switch (r_type)
3548 case R_IA64_IMM14:
3549 case R_IA64_IMM22:
3550 case R_IA64_IMM64:
3551 /* ??? People shouldn't be doing non-pic code in
3552 shared libraries nor dynamic executables. */
3553 (*_bfd_error_handler)
3554 (_("%B: non-pic code with imm relocation against dynamic symbol `%s'"),
3555 input_bfd,
3556 h ? h->root.root.string
3557 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3558 sym_sec));
3559 ret_val = FALSE;
3560 continue;
3562 default:
3563 break;
3566 /* If we don't need dynamic symbol lookup, find a
3567 matching RELATIVE relocation. */
3568 dyn_r_type = r_type;
3569 if (dynamic_symbol_p)
3571 addend = rel->r_addend;
3572 value = 0;
3574 else
3576 addend = value;
3579 /* VMS: install a FIX64. */
3580 switch (dyn_r_type)
3582 case R_IA64_DIR32LSB:
3583 dyn_r_type = R_IA64_VMS_FIX32;
3584 break;
3585 case R_IA64_DIR64LSB:
3586 dyn_r_type = R_IA64_VMS_FIX64;
3587 break;
3588 default:
3589 BFD_ASSERT (FALSE);
3590 break;
3592 elf64_ia64_install_fixup
3593 (output_bfd, ia64_info, h,
3594 dyn_r_type, input_section, rel->r_offset, addend);
3595 r = bfd_reloc_ok;
3596 break;
3598 /* Fall through. */
3600 case R_IA64_LTV32MSB:
3601 case R_IA64_LTV32LSB:
3602 case R_IA64_LTV64MSB:
3603 case R_IA64_LTV64LSB:
3604 r = ia64_elf_install_value (hit_addr, value, r_type);
3605 break;
3607 case R_IA64_GPREL22:
3608 case R_IA64_GPREL64I:
3609 case R_IA64_GPREL32MSB:
3610 case R_IA64_GPREL32LSB:
3611 case R_IA64_GPREL64MSB:
3612 case R_IA64_GPREL64LSB:
3613 if (dynamic_symbol_p)
3615 (*_bfd_error_handler)
3616 (_("%B: @gprel relocation against dynamic symbol %s"),
3617 input_bfd,
3618 h ? h->root.root.string
3619 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3620 sym_sec));
3621 ret_val = FALSE;
3622 continue;
3624 value -= gp_val;
3625 r = ia64_elf_install_value (hit_addr, value, r_type);
3626 break;
3628 case R_IA64_LTOFF22:
3629 case R_IA64_LTOFF22X:
3630 case R_IA64_LTOFF64I:
3631 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
3632 value = set_got_entry (input_bfd, info, dyn_i,
3633 rel->r_addend, value, R_IA64_DIR64LSB);
3634 value -= gp_val;
3635 r = ia64_elf_install_value (hit_addr, value, r_type);
3636 break;
3638 case R_IA64_PLTOFF22:
3639 case R_IA64_PLTOFF64I:
3640 case R_IA64_PLTOFF64MSB:
3641 case R_IA64_PLTOFF64LSB:
3642 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
3643 value = set_pltoff_entry (output_bfd, info, dyn_i, value, FALSE);
3644 value -= gp_val;
3645 r = ia64_elf_install_value (hit_addr, value, r_type);
3646 break;
3648 case R_IA64_FPTR64I:
3649 case R_IA64_FPTR32MSB:
3650 case R_IA64_FPTR32LSB:
3651 case R_IA64_FPTR64MSB:
3652 case R_IA64_FPTR64LSB:
3653 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
3654 if (dyn_i->want_fptr)
3656 if (!undef_weak_ref)
3657 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3659 if (!dyn_i->want_fptr || info->pie)
3661 /* Otherwise, we expect the dynamic linker to create
3662 the entry. */
3664 if (dyn_i->want_fptr)
3666 if (r_type == R_IA64_FPTR64I)
3668 /* We can't represent this without a dynamic symbol.
3669 Adjust the relocation to be against an output
3670 section symbol, which are always present in the
3671 dynamic symbol table. */
3672 /* ??? People shouldn't be doing non-pic code in
3673 shared libraries. Hork. */
3674 (*_bfd_error_handler)
3675 (_("%B: linking non-pic code in a position independent executable"),
3676 input_bfd);
3677 ret_val = FALSE;
3678 continue;
3681 else
3683 value = 0;
3686 /* VMS: FIXFD. */
3687 elf64_ia64_install_fixup
3688 (output_bfd, ia64_info, h, R_IA64_VMS_FIXFD,
3689 input_section, rel->r_offset, 0);
3690 r = bfd_reloc_ok;
3691 break;
3694 r = ia64_elf_install_value (hit_addr, value, r_type);
3695 break;
3697 case R_IA64_LTOFF_FPTR22:
3698 case R_IA64_LTOFF_FPTR64I:
3699 case R_IA64_LTOFF_FPTR32MSB:
3700 case R_IA64_LTOFF_FPTR32LSB:
3701 case R_IA64_LTOFF_FPTR64MSB:
3702 case R_IA64_LTOFF_FPTR64LSB:
3703 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
3704 if (dyn_i->want_fptr)
3706 BFD_ASSERT (h == NULL || !h->def_dynamic);
3707 if (!undef_weak_ref)
3708 value = set_fptr_entry (output_bfd, info, dyn_i, value);
3710 else
3711 value = 0;
3713 value = set_got_entry (output_bfd, info, dyn_i,
3714 rel->r_addend, value, R_IA64_FPTR64LSB);
3715 value -= gp_val;
3716 r = ia64_elf_install_value (hit_addr, value, r_type);
3717 break;
3719 case R_IA64_PCREL32MSB:
3720 case R_IA64_PCREL32LSB:
3721 case R_IA64_PCREL64MSB:
3722 case R_IA64_PCREL64LSB:
3723 /* Install a dynamic relocation for this reloc. */
3724 if (dynamic_symbol_p && r_symndx != 0)
3726 /* VMS: doesn't exist ??? */
3727 abort ();
3729 goto finish_pcrel;
3731 case R_IA64_PCREL21B:
3732 case R_IA64_PCREL60B:
3733 /* We should have created a PLT entry for any dynamic symbol. */
3734 dyn_i = NULL;
3735 if (h)
3736 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
3738 if (dyn_i && dyn_i->want_plt2)
3740 /* Should have caught this earlier. */
3741 BFD_ASSERT (rel->r_addend == 0);
3743 value = (ia64_info->root.splt->output_section->vma
3744 + ia64_info->root.splt->output_offset
3745 + dyn_i->plt2_offset);
3747 else
3749 /* Since there's no PLT entry, Validate that this is
3750 locally defined. */
3751 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
3753 /* If the symbol is undef_weak, we shouldn't be trying
3754 to call it. There's every chance that we'd wind up
3755 with an out-of-range fixup here. Don't bother setting
3756 any value at all. */
3757 if (undef_weak_ref)
3758 continue;
3760 goto finish_pcrel;
3762 case R_IA64_PCREL21BI:
3763 case R_IA64_PCREL21F:
3764 case R_IA64_PCREL21M:
3765 case R_IA64_PCREL22:
3766 case R_IA64_PCREL64I:
3767 /* The PCREL21BI reloc is specifically not intended for use with
3768 dynamic relocs. PCREL21F and PCREL21M are used for speculation
3769 fixup code, and thus probably ought not be dynamic. The
3770 PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs. */
3771 if (dynamic_symbol_p)
3773 const char *msg;
3775 if (r_type == R_IA64_PCREL21BI)
3776 msg = _("%B: @internal branch to dynamic symbol %s");
3777 else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
3778 msg = _("%B: speculation fixup to dynamic symbol %s");
3779 else
3780 msg = _("%B: @pcrel relocation against dynamic symbol %s");
3781 (*_bfd_error_handler) (msg, input_bfd,
3782 h ? h->root.root.string
3783 : bfd_elf_sym_name (input_bfd,
3784 symtab_hdr,
3785 sym,
3786 sym_sec));
3787 ret_val = FALSE;
3788 continue;
3790 goto finish_pcrel;
3792 finish_pcrel:
3793 /* Make pc-relative. */
3794 value -= (input_section->output_section->vma
3795 + input_section->output_offset
3796 + rel->r_offset) & ~ (bfd_vma) 0x3;
3797 r = ia64_elf_install_value (hit_addr, value, r_type);
3798 break;
3800 case R_IA64_SEGREL32MSB:
3801 case R_IA64_SEGREL32LSB:
3802 case R_IA64_SEGREL64MSB:
3803 case R_IA64_SEGREL64LSB:
3805 /* Find the segment that contains the output_section. */
3806 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
3807 (output_bfd, sym_sec->output_section);
3809 if (p == NULL)
3811 r = bfd_reloc_notsupported;
3813 else
3815 /* The VMA of the segment is the vaddr of the associated
3816 program header. */
3817 if (value > p->p_vaddr)
3818 value -= p->p_vaddr;
3819 else
3820 value = 0;
3821 r = ia64_elf_install_value (hit_addr, value, r_type);
3823 break;
3826 case R_IA64_SECREL32MSB:
3827 case R_IA64_SECREL32LSB:
3828 case R_IA64_SECREL64MSB:
3829 case R_IA64_SECREL64LSB:
3830 /* Make output-section relative to section where the symbol
3831 is defined. PR 475 */
3832 if (sym_sec)
3833 value -= sym_sec->output_section->vma;
3834 r = ia64_elf_install_value (hit_addr, value, r_type);
3835 break;
3837 case R_IA64_IPLTMSB:
3838 case R_IA64_IPLTLSB:
3839 /* Install a dynamic relocation for this reloc. */
3840 if ((dynamic_symbol_p || info->shared)
3841 && (input_section->flags & SEC_ALLOC) != 0)
3843 /* VMS: FIXFD ?? */
3844 abort ();
3847 if (r_type == R_IA64_IPLTMSB)
3848 r_type = R_IA64_DIR64MSB;
3849 else
3850 r_type = R_IA64_DIR64LSB;
3851 ia64_elf_install_value (hit_addr, value, r_type);
3852 r = ia64_elf_install_value (hit_addr + 8, gp_val, r_type);
3853 break;
3855 case R_IA64_TPREL14:
3856 case R_IA64_TPREL22:
3857 case R_IA64_TPREL64I:
3858 r = bfd_reloc_notsupported;
3859 break;
3861 case R_IA64_DTPREL14:
3862 case R_IA64_DTPREL22:
3863 case R_IA64_DTPREL64I:
3864 case R_IA64_DTPREL32LSB:
3865 case R_IA64_DTPREL32MSB:
3866 case R_IA64_DTPREL64LSB:
3867 case R_IA64_DTPREL64MSB:
3868 r = bfd_reloc_notsupported;
3869 break;
3871 case R_IA64_LTOFF_TPREL22:
3872 case R_IA64_LTOFF_DTPMOD22:
3873 case R_IA64_LTOFF_DTPREL22:
3874 r = bfd_reloc_notsupported;
3875 break;
3877 default:
3878 r = bfd_reloc_notsupported;
3879 break;
3882 switch (r)
3884 case bfd_reloc_ok:
3885 break;
3887 case bfd_reloc_undefined:
3888 /* This can happen for global table relative relocs if
3889 __gp is undefined. This is a panic situation so we
3890 don't try to continue. */
3891 (*info->callbacks->undefined_symbol)
3892 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
3893 return FALSE;
3895 case bfd_reloc_notsupported:
3897 const char *name;
3899 if (h)
3900 name = h->root.root.string;
3901 else
3902 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3903 sym_sec);
3904 if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
3905 name, input_bfd,
3906 input_section, rel->r_offset))
3907 return FALSE;
3908 ret_val = FALSE;
3910 break;
3912 case bfd_reloc_dangerous:
3913 case bfd_reloc_outofrange:
3914 case bfd_reloc_overflow:
3915 default:
3917 const char *name;
3919 if (h)
3920 name = h->root.root.string;
3921 else
3922 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3923 sym_sec);
3925 switch (r_type)
3927 case R_IA64_TPREL14:
3928 case R_IA64_TPREL22:
3929 case R_IA64_TPREL64I:
3930 case R_IA64_DTPREL14:
3931 case R_IA64_DTPREL22:
3932 case R_IA64_DTPREL64I:
3933 case R_IA64_DTPREL32LSB:
3934 case R_IA64_DTPREL32MSB:
3935 case R_IA64_DTPREL64LSB:
3936 case R_IA64_DTPREL64MSB:
3937 case R_IA64_LTOFF_TPREL22:
3938 case R_IA64_LTOFF_DTPMOD22:
3939 case R_IA64_LTOFF_DTPREL22:
3940 (*_bfd_error_handler)
3941 (_("%B: missing TLS section for relocation %s against `%s' at 0x%lx in section `%A'."),
3942 input_bfd, input_section, howto->name, name,
3943 rel->r_offset);
3944 break;
3946 case R_IA64_PCREL21B:
3947 case R_IA64_PCREL21BI:
3948 case R_IA64_PCREL21M:
3949 case R_IA64_PCREL21F:
3950 if (is_elf_hash_table (info->hash))
3952 /* Relaxtion is always performed for ELF output.
3953 Overflow failures for those relocations mean
3954 that the section is too big to relax. */
3955 (*_bfd_error_handler)
3956 (_("%B: Can't relax br (%s) to `%s' at 0x%lx in section `%A' with size 0x%lx (> 0x1000000)."),
3957 input_bfd, input_section, howto->name, name,
3958 rel->r_offset, input_section->size);
3959 break;
3961 default:
3962 if (!(*info->callbacks->reloc_overflow) (info,
3963 &h->root,
3964 name,
3965 howto->name,
3966 (bfd_vma) 0,
3967 input_bfd,
3968 input_section,
3969 rel->r_offset))
3970 return FALSE;
3971 break;
3974 ret_val = FALSE;
3976 break;
3980 return ret_val;
3983 static bfd_boolean
3984 elf64_ia64_finish_dynamic_symbol (bfd *output_bfd,
3985 struct bfd_link_info *info,
3986 struct elf_link_hash_entry *h,
3987 Elf_Internal_Sym *sym)
3989 struct elf64_ia64_link_hash_table *ia64_info;
3990 struct elf64_ia64_dyn_sym_info *dyn_i;
3992 ia64_info = elf64_ia64_hash_table (info);
3993 if (ia64_info == NULL)
3994 return FALSE;
3996 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
3998 /* Fill in the PLT data, if required. */
3999 if (dyn_i && dyn_i->want_plt)
4001 bfd_byte *loc;
4002 asection *plt_sec;
4003 bfd_vma plt_addr, pltoff_addr, gp_val;
4005 gp_val = _bfd_get_gp_value (output_bfd);
4007 plt_sec = ia64_info->root.splt;
4008 plt_addr = 0; /* Not used as overriden by FIXUPs. */
4009 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, TRUE);
4011 /* Initialize the FULL PLT entry, if needed. */
4012 if (dyn_i->want_plt2)
4014 loc = plt_sec->contents + dyn_i->plt2_offset;
4016 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
4017 ia64_elf_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
4019 /* Mark the symbol as undefined, rather than as defined in the
4020 plt section. Leave the value alone. */
4021 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
4022 first place. But perhaps elflink.c did some for us. */
4023 if (!h->def_regular)
4024 sym->st_shndx = SHN_UNDEF;
4027 /* VMS: FIXFD. */
4028 elf64_ia64_install_fixup
4029 (output_bfd, ia64_info, h, R_IA64_VMS_FIXFD, ia64_info->pltoff_sec,
4030 pltoff_addr - (ia64_info->pltoff_sec->output_section->vma
4031 + ia64_info->pltoff_sec->output_offset), 0);
4034 /* Mark some specially defined symbols as absolute. */
4035 if (h == ia64_info->root.hdynamic
4036 || h == ia64_info->root.hgot
4037 || h == ia64_info->root.hplt)
4038 sym->st_shndx = SHN_ABS;
4040 return TRUE;
4043 static bfd_boolean
4044 elf64_ia64_finish_dynamic_sections (bfd *abfd,
4045 struct bfd_link_info *info)
4047 struct elf64_ia64_link_hash_table *ia64_info;
4048 bfd *dynobj;
4050 ia64_info = elf64_ia64_hash_table (info);
4051 if (ia64_info == NULL)
4052 return FALSE;
4054 dynobj = ia64_info->root.dynobj;
4056 if (elf_hash_table (info)->dynamic_sections_created)
4058 Elf64_External_Dyn *dyncon, *dynconend;
4059 asection *sdyn;
4060 asection *unwind_sec;
4061 bfd_vma gp_val;
4062 unsigned int gp_seg;
4063 bfd_vma gp_off;
4064 Elf_Internal_Phdr *phdr;
4065 Elf_Internal_Phdr *base_phdr;
4066 unsigned int unwind_seg = 0;
4067 unsigned int code_seg = 0;
4069 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4070 BFD_ASSERT (sdyn != NULL);
4071 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4072 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4074 gp_val = _bfd_get_gp_value (abfd);
4075 phdr = _bfd_elf_find_segment_containing_section
4076 (info->output_bfd, ia64_info->pltoff_sec->output_section);
4077 BFD_ASSERT (phdr != NULL);
4078 base_phdr = elf_tdata (info->output_bfd)->phdr;
4079 gp_seg = phdr - base_phdr;
4080 gp_off = gp_val - phdr->p_vaddr;
4082 unwind_sec = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
4083 if (unwind_sec != NULL)
4085 asection *code_sec;
4087 phdr = _bfd_elf_find_segment_containing_section (abfd, unwind_sec);
4088 BFD_ASSERT (phdr != NULL);
4089 unwind_seg = phdr - base_phdr;
4091 code_sec = bfd_get_section_by_name (abfd, "$CODE$");
4092 phdr = _bfd_elf_find_segment_containing_section (abfd, code_sec);
4093 BFD_ASSERT (phdr != NULL);
4094 code_seg = phdr - base_phdr;
4097 for (; dyncon < dynconend; dyncon++)
4099 Elf_Internal_Dyn dyn;
4101 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4103 switch (dyn.d_tag)
4105 case DT_IA_64_VMS_FIXUP_RELA_OFF:
4106 dyn.d_un.d_val +=
4107 (ia64_info->fixups_sec->output_section->vma
4108 + ia64_info->fixups_sec->output_offset)
4109 - (sdyn->output_section->vma + sdyn->output_offset);
4110 break;
4112 case DT_IA_64_VMS_PLTGOT_OFFSET:
4113 dyn.d_un.d_val = gp_off;
4114 break;
4116 case DT_IA_64_VMS_PLTGOT_SEG:
4117 dyn.d_un.d_val = gp_seg;
4118 break;
4120 case DT_IA_64_VMS_UNWINDSZ:
4121 if (unwind_sec == NULL)
4123 dyn.d_tag = DT_NULL;
4124 dyn.d_un.d_val = 0xdead;
4126 else
4127 dyn.d_un.d_val = unwind_sec->size;
4128 break;
4130 case DT_IA_64_VMS_UNWIND_CODSEG:
4131 dyn.d_un.d_val = code_seg;
4132 break;
4134 case DT_IA_64_VMS_UNWIND_INFOSEG:
4135 case DT_IA_64_VMS_UNWIND_SEG:
4136 dyn.d_un.d_val = unwind_seg;
4137 break;
4139 case DT_IA_64_VMS_UNWIND_OFFSET:
4140 break;
4142 default:
4143 /* No need to rewrite the entry. */
4144 continue;
4147 bfd_elf64_swap_dyn_out (abfd, &dyn, dyncon);
4151 /* Handle transfer addresses. */
4153 asection *tfr_sec = ia64_info->transfer_sec;
4154 struct elf64_vms_transfer *tfr;
4155 struct elf_link_hash_entry *tfr3;
4157 tfr = (struct elf64_vms_transfer *)tfr_sec->contents;
4158 bfd_putl32 (6 * 8, tfr->size);
4159 bfd_putl64 (tfr_sec->output_section->vma
4160 + tfr_sec->output_offset
4161 + 6 * 8, tfr->tfradr3);
4163 tfr3 = elf_link_hash_lookup (elf_hash_table (info), "ELF$TFRADR", FALSE,
4164 FALSE, FALSE);
4166 if (tfr3
4167 && (tfr3->root.type == bfd_link_hash_defined
4168 || tfr3->root.type == bfd_link_hash_defweak))
4170 asection *tfr3_sec = tfr3->root.u.def.section;
4171 bfd_vma tfr3_val;
4173 tfr3_val = (tfr3->root.u.def.value
4174 + tfr3_sec->output_section->vma
4175 + tfr3_sec->output_offset);
4177 bfd_putl64 (tfr3_val, tfr->tfr3_func);
4178 bfd_putl64 (_bfd_get_gp_value (info->output_bfd), tfr->tfr3_gp);
4181 /* FIXME: set linker flags,
4182 handle lib$initialize. */
4185 return TRUE;
4188 /* ELF file flag handling: */
4190 /* Function to keep IA-64 specific file flags. */
4191 static bfd_boolean
4192 elf64_ia64_set_private_flags (bfd *abfd, flagword flags)
4194 BFD_ASSERT (!elf_flags_init (abfd)
4195 || elf_elfheader (abfd)->e_flags == flags);
4197 elf_elfheader (abfd)->e_flags = flags;
4198 elf_flags_init (abfd) = TRUE;
4199 return TRUE;
4202 /* Merge backend specific data from an object file to the output
4203 object file when linking. */
4204 static bfd_boolean
4205 elf64_ia64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4207 flagword out_flags;
4208 flagword in_flags;
4209 bfd_boolean ok = TRUE;
4211 /* Don't even pretend to support mixed-format linking. */
4212 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4213 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4214 return FALSE;
4216 in_flags = elf_elfheader (ibfd)->e_flags;
4217 out_flags = elf_elfheader (obfd)->e_flags;
4219 if (! elf_flags_init (obfd))
4221 elf_flags_init (obfd) = TRUE;
4222 elf_elfheader (obfd)->e_flags = in_flags;
4224 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4225 && bfd_get_arch_info (obfd)->the_default)
4227 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4228 bfd_get_mach (ibfd));
4231 return TRUE;
4234 /* Check flag compatibility. */
4235 if (in_flags == out_flags)
4236 return TRUE;
4238 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
4239 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4240 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4242 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4244 (*_bfd_error_handler)
4245 (_("%B: linking trap-on-NULL-dereference with non-trapping files"),
4246 ibfd);
4248 bfd_set_error (bfd_error_bad_value);
4249 ok = FALSE;
4251 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4253 (*_bfd_error_handler)
4254 (_("%B: linking big-endian files with little-endian files"),
4255 ibfd);
4257 bfd_set_error (bfd_error_bad_value);
4258 ok = FALSE;
4260 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4262 (*_bfd_error_handler)
4263 (_("%B: linking 64-bit files with 32-bit files"),
4264 ibfd);
4266 bfd_set_error (bfd_error_bad_value);
4267 ok = FALSE;
4269 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4271 (*_bfd_error_handler)
4272 (_("%B: linking constant-gp files with non-constant-gp files"),
4273 ibfd);
4275 bfd_set_error (bfd_error_bad_value);
4276 ok = FALSE;
4278 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4279 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4281 (*_bfd_error_handler)
4282 (_("%B: linking auto-pic files with non-auto-pic files"),
4283 ibfd);
4285 bfd_set_error (bfd_error_bad_value);
4286 ok = FALSE;
4289 return ok;
4292 static bfd_boolean
4293 elf64_ia64_print_private_bfd_data (bfd *abfd, void * ptr)
4295 FILE *file = (FILE *) ptr;
4296 flagword flags = elf_elfheader (abfd)->e_flags;
4298 BFD_ASSERT (abfd != NULL && ptr != NULL);
4300 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4301 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4302 (flags & EF_IA_64_EXT) ? "EXT, " : "",
4303 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4304 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4305 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4306 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4307 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4308 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4310 _bfd_elf_print_private_bfd_data (abfd, ptr);
4311 return TRUE;
4314 static enum elf_reloc_type_class
4315 elf64_ia64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4316 const asection *rel_sec ATTRIBUTE_UNUSED,
4317 const Elf_Internal_Rela *rela)
4319 switch ((int) ELF64_R_TYPE (rela->r_info))
4321 case R_IA64_REL32MSB:
4322 case R_IA64_REL32LSB:
4323 case R_IA64_REL64MSB:
4324 case R_IA64_REL64LSB:
4325 return reloc_class_relative;
4326 case R_IA64_IPLTMSB:
4327 case R_IA64_IPLTLSB:
4328 return reloc_class_plt;
4329 case R_IA64_COPY:
4330 return reloc_class_copy;
4331 default:
4332 return reloc_class_normal;
4336 static const struct bfd_elf_special_section elf64_ia64_special_sections[] =
4338 { STRING_COMMA_LEN (".sbss"), -1, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4339 { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4340 { NULL, 0, 0, 0, 0 }
4343 static bfd_boolean
4344 elf64_ia64_object_p (bfd *abfd)
4346 asection *sec;
4347 asection *group, *unwi, *unw;
4348 flagword flags;
4349 const char *name;
4350 char *unwi_name, *unw_name;
4351 bfd_size_type amt;
4353 if (abfd->flags & DYNAMIC)
4354 return TRUE;
4356 /* Flags for fake group section. */
4357 flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
4358 | SEC_EXCLUDE);
4360 /* We add a fake section group for each .gnu.linkonce.t.* section,
4361 which isn't in a section group, and its unwind sections. */
4362 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4364 if (elf_sec_group (sec) == NULL
4365 && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
4366 == (SEC_LINK_ONCE | SEC_CODE))
4367 && CONST_STRNEQ (sec->name, ".gnu.linkonce.t."))
4369 name = sec->name + 16;
4371 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
4372 unwi_name = bfd_alloc (abfd, amt);
4373 if (!unwi_name)
4374 return FALSE;
4376 strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
4377 unwi = bfd_get_section_by_name (abfd, unwi_name);
4379 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
4380 unw_name = bfd_alloc (abfd, amt);
4381 if (!unw_name)
4382 return FALSE;
4384 strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
4385 unw = bfd_get_section_by_name (abfd, unw_name);
4387 /* We need to create a fake group section for it and its
4388 unwind sections. */
4389 group = bfd_make_section_anyway_with_flags (abfd, name,
4390 flags);
4391 if (group == NULL)
4392 return FALSE;
4394 /* Move the fake group section to the beginning. */
4395 bfd_section_list_remove (abfd, group);
4396 bfd_section_list_prepend (abfd, group);
4398 elf_next_in_group (group) = sec;
4400 elf_group_name (sec) = name;
4401 elf_next_in_group (sec) = sec;
4402 elf_sec_group (sec) = group;
4404 if (unwi)
4406 elf_group_name (unwi) = name;
4407 elf_next_in_group (unwi) = sec;
4408 elf_next_in_group (sec) = unwi;
4409 elf_sec_group (unwi) = group;
4412 if (unw)
4414 elf_group_name (unw) = name;
4415 if (unwi)
4417 elf_next_in_group (unw) = elf_next_in_group (unwi);
4418 elf_next_in_group (unwi) = unw;
4420 else
4422 elf_next_in_group (unw) = sec;
4423 elf_next_in_group (sec) = unw;
4425 elf_sec_group (unw) = group;
4428 /* Fake SHT_GROUP section header. */
4429 elf_section_data (group)->this_hdr.bfd_section = group;
4430 elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
4433 return TRUE;
4436 /* Handle an IA-64 specific section when reading an object file. This
4437 is called when bfd_section_from_shdr finds a section with an unknown
4438 type. */
4440 static bfd_boolean
4441 elf64_vms_section_from_shdr (bfd *abfd,
4442 Elf_Internal_Shdr *hdr,
4443 const char *name,
4444 int shindex)
4446 flagword secflags = 0;
4448 switch (hdr->sh_type)
4450 case SHT_IA_64_VMS_TRACE:
4451 case SHT_IA_64_VMS_DEBUG:
4452 case SHT_IA_64_VMS_DEBUG_STR:
4453 secflags = SEC_DEBUGGING;
4454 break;
4456 case SHT_IA_64_UNWIND:
4457 case SHT_IA_64_HP_OPT_ANOT:
4458 break;
4460 case SHT_IA_64_EXT:
4461 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
4462 return FALSE;
4463 break;
4465 default:
4466 return FALSE;
4469 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4470 return FALSE;
4472 if (secflags != 0)
4474 asection *newsect = hdr->bfd_section;
4476 if (! bfd_set_section_flags
4477 (abfd, newsect, bfd_get_section_flags (abfd, newsect) | secflags))
4478 return FALSE;
4481 return TRUE;
4484 static bfd_boolean
4485 elf64_vms_object_p (bfd *abfd)
4487 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4488 Elf_Internal_Phdr *i_phdr = elf_tdata (abfd)->phdr;
4489 unsigned int i;
4490 unsigned int num_text = 0;
4491 unsigned int num_data = 0;
4492 unsigned int num_rodata = 0;
4493 char name[16];
4495 if (!elf64_ia64_object_p (abfd))
4496 return FALSE;
4498 /* Many VMS compilers do not generate sections for the corresponding
4499 segment. This is boring as binutils tools won't be able to disassemble
4500 the code. So we simply create all the missing sections. */
4501 for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
4503 /* Is there a section for this segment? */
4504 bfd_vma base_vma = i_phdr->p_vaddr;
4505 bfd_vma limit_vma = base_vma + i_phdr->p_filesz;
4507 if (i_phdr->p_type != PT_LOAD)
4508 continue;
4510 /* We need to cover from base_vms to limit_vma. */
4511 again:
4512 while (base_vma < limit_vma)
4514 bfd_vma next_vma = limit_vma;
4515 asection *nsec;
4516 asection *sec;
4517 flagword flags;
4518 char *nname = NULL;
4520 /* Find a section covering [base_vma;limit_vma) */
4521 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4523 /* Skip uninteresting sections (either not in memory or
4524 below base_vma. */
4525 if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == 0
4526 || sec->vma + sec->size <= base_vma)
4527 continue;
4528 if (sec->vma <= base_vma)
4530 /* This section covers (maybe partially) the beginning
4531 of the range. */
4532 base_vma = sec->vma + sec->size;
4533 goto again;
4535 if (sec->vma < next_vma)
4537 /* This section partially covers the end of the range.
4538 Used to compute the size of the hole. */
4539 next_vma = sec->vma;
4543 /* No section covering [base_vma; next_vma). Create a fake one. */
4544 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
4545 if (i_phdr->p_flags & PF_X)
4547 flags |= SEC_CODE;
4548 if (num_text++ == 0)
4549 nname = ".text";
4550 else
4551 sprintf (name, ".text$%u", num_text);
4553 else if ((i_phdr->p_flags & (PF_R | PF_W)) == PF_R)
4555 flags |= SEC_READONLY;
4556 sprintf (name, ".rodata$%u", num_rodata++);
4558 else
4560 flags |= SEC_DATA;
4561 sprintf (name, ".data$%u", num_data++);
4564 /* Allocate name. */
4565 if (nname == NULL)
4567 size_t name_len = strlen (name) + 1;
4568 nname = bfd_alloc (abfd, name_len);
4569 if (nname == NULL)
4570 return FALSE;
4571 memcpy (nname, name, name_len);
4574 /* Create and fill new section. */
4575 nsec = bfd_make_section_anyway_with_flags (abfd, nname, flags);
4576 if (nsec == NULL)
4577 return FALSE;
4578 nsec->vma = base_vma;
4579 nsec->size = next_vma - base_vma;
4580 nsec->filepos = i_phdr->p_offset + (base_vma - i_phdr->p_vaddr);
4582 base_vma = next_vma;
4585 return TRUE;
4588 static void
4589 elf64_vms_post_process_headers (bfd *abfd,
4590 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4592 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4594 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_OPENVMS;
4595 i_ehdrp->e_ident[EI_ABIVERSION] = 2;
4598 static bfd_boolean
4599 elf64_vms_section_processing (bfd *abfd ATTRIBUTE_UNUSED,
4600 Elf_Internal_Shdr *hdr)
4602 if (hdr->bfd_section != NULL)
4604 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
4606 if (strcmp (name, ".text") == 0)
4607 hdr->sh_flags |= SHF_IA_64_VMS_SHARED;
4608 else if ((strcmp (name, ".debug") == 0)
4609 || (strcmp (name, ".debug_abbrev") == 0)
4610 || (strcmp (name, ".debug_aranges") == 0)
4611 || (strcmp (name, ".debug_frame") == 0)
4612 || (strcmp (name, ".debug_info") == 0)
4613 || (strcmp (name, ".debug_loc") == 0)
4614 || (strcmp (name, ".debug_macinfo") == 0)
4615 || (strcmp (name, ".debug_pubnames") == 0)
4616 || (strcmp (name, ".debug_pubtypes") == 0))
4617 hdr->sh_type = SHT_IA_64_VMS_DEBUG;
4618 else if ((strcmp (name, ".debug_line") == 0)
4619 || (strcmp (name, ".debug_ranges") == 0)
4620 || (strcmp (name, ".trace_info") == 0)
4621 || (strcmp (name, ".trace_abbrev") == 0)
4622 || (strcmp (name, ".trace_aranges") == 0))
4623 hdr->sh_type = SHT_IA_64_VMS_TRACE;
4624 else if (strcmp (name, ".debug_str") == 0)
4625 hdr->sh_type = SHT_IA_64_VMS_DEBUG_STR;
4628 return TRUE;
4631 /* The final processing done just before writing out a VMS IA-64 ELF
4632 object file. */
4634 static void
4635 elf64_vms_final_write_processing (bfd *abfd,
4636 bfd_boolean linker ATTRIBUTE_UNUSED)
4638 Elf_Internal_Shdr *hdr;
4639 asection *s;
4640 int unwind_info_sect_idx = 0;
4642 for (s = abfd->sections; s; s = s->next)
4644 hdr = &elf_section_data (s)->this_hdr;
4646 if (strcmp (bfd_get_section_name (abfd, hdr->bfd_section),
4647 ".IA_64.unwind_info") == 0)
4648 unwind_info_sect_idx = elf_section_data (s)->this_idx;
4650 switch (hdr->sh_type)
4652 case SHT_IA_64_UNWIND:
4653 /* VMS requires sh_info to point to the unwind info section. */
4654 hdr->sh_info = unwind_info_sect_idx;
4655 break;
4659 if (! elf_flags_init (abfd))
4661 unsigned long flags = 0;
4663 if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
4664 flags |= EF_IA_64_BE;
4665 if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
4666 flags |= EF_IA_64_ABI64;
4668 elf_elfheader (abfd)->e_flags = flags;
4669 elf_flags_init (abfd) = TRUE;
4673 static bfd_boolean
4674 elf64_vms_write_shdrs_and_ehdr (bfd *abfd)
4676 unsigned char needed_count[8];
4678 if (!bfd_elf64_write_shdrs_and_ehdr (abfd))
4679 return FALSE;
4681 bfd_putl64 (elf_ia64_vms_tdata (abfd)->needed_count, needed_count);
4683 if (bfd_seek (abfd, sizeof (Elf64_External_Ehdr), SEEK_SET) != 0
4684 || bfd_bwrite (needed_count, 8, abfd) != 8)
4685 return FALSE;
4687 return TRUE;
4690 static bfd_boolean
4691 elf64_vms_close_and_cleanup (bfd *abfd)
4693 if (bfd_get_format (abfd) == bfd_object)
4695 long isize;
4697 /* Pad to 8 byte boundary for IPF/VMS. */
4698 isize = bfd_get_size (abfd);
4699 if ((isize & 7) != 0)
4701 int ishort = 8 - (isize & 7);
4702 bfd_uint64_t pad = 0;
4704 bfd_seek (abfd, isize, SEEK_SET);
4705 bfd_bwrite (&pad, ishort, abfd);
4709 return _bfd_elf_close_and_cleanup (abfd);
4712 /* Add symbols from an ELF object file to the linker hash table. */
4714 static bfd_boolean
4715 elf64_vms_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4717 Elf_Internal_Shdr *hdr;
4718 bfd_size_type symcount;
4719 bfd_size_type extsymcount;
4720 bfd_size_type extsymoff;
4721 struct elf_link_hash_entry **sym_hash;
4722 bfd_boolean dynamic;
4723 Elf_Internal_Sym *isymbuf = NULL;
4724 Elf_Internal_Sym *isym;
4725 Elf_Internal_Sym *isymend;
4726 const struct elf_backend_data *bed;
4727 struct elf_link_hash_table *htab;
4728 bfd_size_type amt;
4730 htab = elf_hash_table (info);
4731 bed = get_elf_backend_data (abfd);
4733 if ((abfd->flags & DYNAMIC) == 0)
4734 dynamic = FALSE;
4735 else
4737 dynamic = TRUE;
4739 /* You can't use -r against a dynamic object. Also, there's no
4740 hope of using a dynamic object which does not exactly match
4741 the format of the output file. */
4742 if (info->relocatable
4743 || !is_elf_hash_table (htab)
4744 || info->output_bfd->xvec != abfd->xvec)
4746 if (info->relocatable)
4747 bfd_set_error (bfd_error_invalid_operation);
4748 else
4749 bfd_set_error (bfd_error_wrong_format);
4750 goto error_return;
4754 if (! dynamic)
4756 /* If we are creating a shared library, create all the dynamic
4757 sections immediately. We need to attach them to something,
4758 so we attach them to this BFD, provided it is the right
4759 format. FIXME: If there are no input BFD's of the same
4760 format as the output, we can't make a shared library. */
4761 if (info->shared
4762 && is_elf_hash_table (htab)
4763 && info->output_bfd->xvec == abfd->xvec
4764 && !htab->dynamic_sections_created)
4766 if (! elf64_ia64_create_dynamic_sections (abfd, info))
4767 goto error_return;
4770 else if (!is_elf_hash_table (htab))
4771 goto error_return;
4772 else
4774 asection *s;
4775 bfd_byte *dynbuf;
4776 bfd_byte *extdyn;
4778 /* ld --just-symbols and dynamic objects don't mix very well.
4779 ld shouldn't allow it. */
4780 if ((s = abfd->sections) != NULL
4781 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4782 abort ();
4784 /* Be sure there are dynamic sections. */
4785 if (! elf64_ia64_create_dynamic_sections (htab->dynobj, info))
4786 goto error_return;
4788 s = bfd_get_section_by_name (abfd, ".dynamic");
4789 if (s == NULL)
4791 /* VMS libraries do not have dynamic sections. Create one from
4792 the segment. */
4793 Elf_Internal_Phdr *phdr;
4794 unsigned int i, phnum;
4796 phdr = elf_tdata (abfd)->phdr;
4797 if (phdr == NULL)
4798 goto error_return;
4799 phnum = elf_elfheader (abfd)->e_phnum;
4800 for (i = 0; i < phnum; phdr++)
4801 if (phdr->p_type == PT_DYNAMIC)
4803 s = bfd_make_section (abfd, ".dynamic");
4804 if (s == NULL)
4805 goto error_return;
4806 s->vma = phdr->p_vaddr;
4807 s->lma = phdr->p_paddr;
4808 s->size = phdr->p_filesz;
4809 s->filepos = phdr->p_offset;
4810 s->flags |= SEC_HAS_CONTENTS;
4811 s->alignment_power = bfd_log2 (phdr->p_align);
4812 break;
4814 if (s == NULL)
4815 goto error_return;
4818 /* Extract IDENT. */
4819 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4821 error_free_dyn:
4822 free (dynbuf);
4823 goto error_return;
4826 for (extdyn = dynbuf;
4827 extdyn < dynbuf + s->size;
4828 extdyn += bed->s->sizeof_dyn)
4830 Elf_Internal_Dyn dyn;
4832 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4833 if (dyn.d_tag == DT_IA_64_VMS_IDENT)
4835 bfd_uint64_t tagv = dyn.d_un.d_val;
4836 elf_ia64_vms_ident (abfd) = tagv;
4837 break;
4840 if (extdyn >= dynbuf + s->size)
4842 /* Ident not found. */
4843 goto error_free_dyn;
4845 free (dynbuf);
4847 /* We do not want to include any of the sections in a dynamic
4848 object in the output file. We hack by simply clobbering the
4849 list of sections in the BFD. This could be handled more
4850 cleanly by, say, a new section flag; the existing
4851 SEC_NEVER_LOAD flag is not the one we want, because that one
4852 still implies that the section takes up space in the output
4853 file. */
4854 bfd_section_list_clear (abfd);
4856 /* FIXME: should we detect if this library is already included ?
4857 This should be harmless and shouldn't happen in practice. */
4860 hdr = &elf_tdata (abfd)->symtab_hdr;
4861 symcount = hdr->sh_size / bed->s->sizeof_sym;
4863 /* The sh_info field of the symtab header tells us where the
4864 external symbols start. We don't care about the local symbols at
4865 this point. */
4866 extsymcount = symcount - hdr->sh_info;
4867 extsymoff = hdr->sh_info;
4869 sym_hash = NULL;
4870 if (extsymcount != 0)
4872 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4873 NULL, NULL, NULL);
4874 if (isymbuf == NULL)
4875 goto error_return;
4877 /* We store a pointer to the hash table entry for each external
4878 symbol. */
4879 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4880 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
4881 if (sym_hash == NULL)
4882 goto error_free_sym;
4883 elf_sym_hashes (abfd) = sym_hash;
4886 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4887 isym < isymend;
4888 isym++, sym_hash++)
4890 int bind;
4891 bfd_vma value;
4892 asection *sec, *new_sec;
4893 flagword flags;
4894 const char *name;
4895 struct elf_link_hash_entry *h;
4896 bfd_boolean definition;
4897 bfd_boolean size_change_ok;
4898 bfd_boolean type_change_ok;
4899 bfd_boolean common;
4900 unsigned int old_alignment;
4901 bfd *old_bfd;
4903 flags = BSF_NO_FLAGS;
4904 sec = NULL;
4905 value = isym->st_value;
4906 *sym_hash = NULL;
4907 common = bed->common_definition (isym);
4909 bind = ELF_ST_BIND (isym->st_info);
4910 switch (bind)
4912 case STB_LOCAL:
4913 /* This should be impossible, since ELF requires that all
4914 global symbols follow all local symbols, and that sh_info
4915 point to the first global symbol. Unfortunately, Irix 5
4916 screws this up. */
4917 continue;
4919 case STB_GLOBAL:
4920 if (isym->st_shndx != SHN_UNDEF && !common)
4921 flags = BSF_GLOBAL;
4922 break;
4924 case STB_WEAK:
4925 flags = BSF_WEAK;
4926 break;
4928 case STB_GNU_UNIQUE:
4929 flags = BSF_GNU_UNIQUE;
4930 break;
4932 default:
4933 /* Leave it up to the processor backend. */
4934 break;
4937 if (isym->st_shndx == SHN_UNDEF)
4938 sec = bfd_und_section_ptr;
4939 else if (isym->st_shndx == SHN_ABS)
4940 sec = bfd_abs_section_ptr;
4941 else if (isym->st_shndx == SHN_COMMON)
4943 sec = bfd_com_section_ptr;
4944 /* What ELF calls the size we call the value. What ELF
4945 calls the value we call the alignment. */
4946 value = isym->st_size;
4948 else
4950 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4951 if (sec == NULL)
4952 sec = bfd_abs_section_ptr;
4953 else if (sec->kept_section)
4955 /* Symbols from discarded section are undefined. We keep
4956 its visibility. */
4957 sec = bfd_und_section_ptr;
4958 isym->st_shndx = SHN_UNDEF;
4960 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4961 value -= sec->vma;
4964 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4965 isym->st_name);
4966 if (name == NULL)
4967 goto error_free_vers;
4969 if (bed->elf_add_symbol_hook)
4971 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4972 &sec, &value))
4973 goto error_free_vers;
4975 /* The hook function sets the name to NULL if this symbol
4976 should be skipped for some reason. */
4977 if (name == NULL)
4978 continue;
4981 /* Sanity check that all possibilities were handled. */
4982 if (sec == NULL)
4984 bfd_set_error (bfd_error_bad_value);
4985 goto error_free_vers;
4988 if (bfd_is_und_section (sec)
4989 || bfd_is_com_section (sec))
4990 definition = FALSE;
4991 else
4992 definition = TRUE;
4994 size_change_ok = FALSE;
4995 type_change_ok = bed->type_change_ok;
4996 old_alignment = 0;
4997 old_bfd = NULL;
4998 new_sec = sec;
5000 if (! bfd_is_und_section (sec))
5001 h = elf_link_hash_lookup (htab, name, TRUE, FALSE, FALSE);
5002 else
5003 h = ((struct elf_link_hash_entry *) bfd_wrapped_link_hash_lookup
5004 (abfd, info, name, TRUE, FALSE, FALSE));
5005 if (h == NULL)
5006 goto error_free_sym;
5008 *sym_hash = h;
5010 if (is_elf_hash_table (htab))
5012 while (h->root.type == bfd_link_hash_indirect
5013 || h->root.type == bfd_link_hash_warning)
5014 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5016 /* Remember the old alignment if this is a common symbol, so
5017 that we don't reduce the alignment later on. We can't
5018 check later, because _bfd_generic_link_add_one_symbol
5019 will set a default for the alignment which we want to
5020 override. We also remember the old bfd where the existing
5021 definition comes from. */
5022 switch (h->root.type)
5024 default:
5025 break;
5027 case bfd_link_hash_defined:
5028 if (abfd->selective_search)
5029 continue;
5030 /* Fall-through. */
5031 case bfd_link_hash_defweak:
5032 old_bfd = h->root.u.def.section->owner;
5033 break;
5035 case bfd_link_hash_common:
5036 old_bfd = h->root.u.c.p->section->owner;
5037 old_alignment = h->root.u.c.p->alignment_power;
5038 break;
5042 if (! (_bfd_generic_link_add_one_symbol
5043 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
5044 (struct bfd_link_hash_entry **) sym_hash)))
5045 goto error_free_vers;
5047 h = *sym_hash;
5048 while (h->root.type == bfd_link_hash_indirect
5049 || h->root.type == bfd_link_hash_warning)
5050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5052 *sym_hash = h;
5053 if (definition)
5054 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5056 /* Set the alignment of a common symbol. */
5057 if ((common || bfd_is_com_section (sec))
5058 && h->root.type == bfd_link_hash_common)
5060 unsigned int align;
5062 if (common)
5063 align = bfd_log2 (isym->st_value);
5064 else
5066 /* The new symbol is a common symbol in a shared object.
5067 We need to get the alignment from the section. */
5068 align = new_sec->alignment_power;
5070 if (align > old_alignment
5071 /* Permit an alignment power of zero if an alignment of one
5072 is specified and no other alignments have been specified. */
5073 || (isym->st_value == 1 && old_alignment == 0))
5074 h->root.u.c.p->alignment_power = align;
5075 else
5076 h->root.u.c.p->alignment_power = old_alignment;
5079 if (is_elf_hash_table (htab))
5081 /* Check the alignment when a common symbol is involved. This
5082 can change when a common symbol is overridden by a normal
5083 definition or a common symbol is ignored due to the old
5084 normal definition. We need to make sure the maximum
5085 alignment is maintained. */
5086 if ((old_alignment || common)
5087 && h->root.type != bfd_link_hash_common)
5089 unsigned int common_align;
5090 unsigned int normal_align;
5091 unsigned int symbol_align;
5092 bfd *normal_bfd;
5093 bfd *common_bfd;
5095 symbol_align = ffs (h->root.u.def.value) - 1;
5096 if (h->root.u.def.section->owner != NULL
5097 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
5099 normal_align = h->root.u.def.section->alignment_power;
5100 if (normal_align > symbol_align)
5101 normal_align = symbol_align;
5103 else
5104 normal_align = symbol_align;
5106 if (old_alignment)
5108 common_align = old_alignment;
5109 common_bfd = old_bfd;
5110 normal_bfd = abfd;
5112 else
5114 common_align = bfd_log2 (isym->st_value);
5115 common_bfd = abfd;
5116 normal_bfd = old_bfd;
5119 if (normal_align < common_align)
5121 /* PR binutils/2735 */
5122 if (normal_bfd == NULL)
5123 (*_bfd_error_handler)
5124 (_("Warning: alignment %u of common symbol `%s' in %B"
5125 " is greater than the alignment (%u) of its section %A"),
5126 common_bfd, h->root.u.def.section,
5127 1 << common_align, name, 1 << normal_align);
5128 else
5129 (*_bfd_error_handler)
5130 (_("Warning: alignment %u of symbol `%s' in %B"
5131 " is smaller than %u in %B"),
5132 normal_bfd, common_bfd,
5133 1 << normal_align, name, 1 << common_align);
5137 /* Remember the symbol size if it isn't undefined. */
5138 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
5139 && (definition || h->size == 0))
5141 if (h->size != 0
5142 && h->size != isym->st_size
5143 && ! size_change_ok)
5144 (*_bfd_error_handler)
5145 (_("Warning: size of symbol `%s' changed"
5146 " from %lu in %B to %lu in %B"),
5147 old_bfd, abfd,
5148 name, (unsigned long) h->size,
5149 (unsigned long) isym->st_size);
5151 h->size = isym->st_size;
5154 /* If this is a common symbol, then we always want H->SIZE
5155 to be the size of the common symbol. The code just above
5156 won't fix the size if a common symbol becomes larger. We
5157 don't warn about a size change here, because that is
5158 covered by --warn-common. Allow changed between different
5159 function types. */
5160 if (h->root.type == bfd_link_hash_common)
5161 h->size = h->root.u.c.size;
5163 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5164 && (definition || h->type == STT_NOTYPE))
5166 unsigned int type = ELF_ST_TYPE (isym->st_info);
5168 if (h->type != type)
5170 if (h->type != STT_NOTYPE && ! type_change_ok)
5171 (*_bfd_error_handler)
5172 (_("Warning: type of symbol `%s' changed"
5173 " from %d to %d in %B"),
5174 abfd, name, h->type, type);
5176 h->type = type;
5180 /* Set a flag in the hash table entry indicating the type of
5181 reference or definition we just found. Keep a count of
5182 the number of dynamic symbols we find. A dynamic symbol
5183 is one which is referenced or defined by both a regular
5184 object and a shared object. */
5185 if (! dynamic)
5187 if (! definition)
5189 h->ref_regular = 1;
5190 if (bind != STB_WEAK)
5191 h->ref_regular_nonweak = 1;
5193 else
5195 BFD_ASSERT (!h->def_dynamic);
5196 h->def_regular = 1;
5199 else
5201 BFD_ASSERT (definition);
5202 h->def_dynamic = 1;
5203 h->dynindx = -2;
5204 ((struct elf64_ia64_link_hash_entry *)h)->shl = abfd;
5209 if (isymbuf != NULL)
5211 free (isymbuf);
5212 isymbuf = NULL;
5215 /* If this object is the same format as the output object, and it is
5216 not a shared library, then let the backend look through the
5217 relocs.
5219 This is required to build global offset table entries and to
5220 arrange for dynamic relocs. It is not required for the
5221 particular common case of linking non PIC code, even when linking
5222 against shared libraries, but unfortunately there is no way of
5223 knowing whether an object file has been compiled PIC or not.
5224 Looking through the relocs is not particularly time consuming.
5225 The problem is that we must either (1) keep the relocs in memory,
5226 which causes the linker to require additional runtime memory or
5227 (2) read the relocs twice from the input file, which wastes time.
5228 This would be a good case for using mmap.
5230 I have no idea how to handle linking PIC code into a file of a
5231 different format. It probably can't be done. */
5232 if (! dynamic
5233 && is_elf_hash_table (htab)
5234 && bed->check_relocs != NULL
5235 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
5237 asection *o;
5239 for (o = abfd->sections; o != NULL; o = o->next)
5241 Elf_Internal_Rela *internal_relocs;
5242 bfd_boolean ok;
5244 if ((o->flags & SEC_RELOC) == 0
5245 || o->reloc_count == 0
5246 || ((info->strip == strip_all || info->strip == strip_debugger)
5247 && (o->flags & SEC_DEBUGGING) != 0)
5248 || bfd_is_abs_section (o->output_section))
5249 continue;
5251 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
5252 info->keep_memory);
5253 if (internal_relocs == NULL)
5254 goto error_return;
5256 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
5258 if (elf_section_data (o)->relocs != internal_relocs)
5259 free (internal_relocs);
5261 if (! ok)
5262 goto error_return;
5266 return TRUE;
5268 error_free_vers:
5269 error_free_sym:
5270 if (isymbuf != NULL)
5271 free (isymbuf);
5272 error_return:
5273 return FALSE;
5276 static bfd_boolean
5277 elf64_vms_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5279 int pass;
5280 struct bfd_link_hash_entry **pundef;
5281 struct bfd_link_hash_entry **next_pundef;
5283 /* We only accept VMS libraries. */
5284 if (info->output_bfd->xvec != abfd->xvec)
5286 bfd_set_error (bfd_error_wrong_format);
5287 return FALSE;
5290 /* The archive_pass field in the archive itself is used to
5291 initialize PASS, since we may search the same archive multiple
5292 times. */
5293 pass = ++abfd->archive_pass;
5295 /* Look through the list of undefined symbols. */
5296 for (pundef = &info->hash->undefs; *pundef != NULL; pundef = next_pundef)
5298 struct bfd_link_hash_entry *h;
5299 symindex symidx;
5300 bfd *element;
5301 bfd *orig_element;
5303 h = *pundef;
5304 next_pundef = &(*pundef)->u.undef.next;
5306 /* When a symbol is defined, it is not necessarily removed from
5307 the list. */
5308 if (h->type != bfd_link_hash_undefined
5309 && h->type != bfd_link_hash_common)
5311 /* Remove this entry from the list, for general cleanliness
5312 and because we are going to look through the list again
5313 if we search any more libraries. We can't remove the
5314 entry if it is the tail, because that would lose any
5315 entries we add to the list later on. */
5316 if (*pundef != info->hash->undefs_tail)
5318 *pundef = *next_pundef;
5319 next_pundef = pundef;
5321 continue;
5324 /* Look for this symbol in the archive hash table. */
5325 symidx = _bfd_vms_lib_find_symbol (abfd, h->root.string);
5326 if (symidx == BFD_NO_MORE_SYMBOLS)
5328 /* Nothing in this slot. */
5329 continue;
5332 element = bfd_get_elt_at_index (abfd, symidx);
5333 if (element == NULL)
5334 return FALSE;
5336 if (element->archive_pass == -1 || element->archive_pass == pass)
5338 /* Next symbol if this archive is wrong or already handled. */
5339 continue;
5342 orig_element = element;
5343 if (bfd_is_thin_archive (abfd))
5345 element = _bfd_vms_lib_get_imagelib_file (element);
5346 if (element == NULL || !bfd_check_format (element, bfd_object))
5348 orig_element->archive_pass = -1;
5349 return FALSE;
5352 else if (! bfd_check_format (element, bfd_object))
5354 element->archive_pass = -1;
5355 return FALSE;
5358 /* Unlike the generic linker, we know that this element provides
5359 a definition for an undefined symbol and we know that we want
5360 to include it. We don't need to check anything. */
5361 if (! (*info->callbacks->add_archive_element) (info, element,
5362 h->root.string, &element))
5363 return FALSE;
5364 if (! elf64_vms_link_add_object_symbols (element, info))
5365 return FALSE;
5367 orig_element->archive_pass = pass;
5370 return TRUE;
5373 static bfd_boolean
5374 elf64_vms_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5376 switch (bfd_get_format (abfd))
5378 case bfd_object:
5379 return elf64_vms_link_add_object_symbols (abfd, info);
5380 break;
5381 case bfd_archive:
5382 return elf64_vms_link_add_archive_symbols (abfd, info);
5383 break;
5384 default:
5385 bfd_set_error (bfd_error_wrong_format);
5386 return FALSE;
5390 static bfd_boolean
5391 elf64_ia64_vms_mkobject (bfd *abfd)
5393 return bfd_elf_allocate_object
5394 (abfd, sizeof (struct elf64_ia64_vms_obj_tdata), IA64_ELF_DATA);
5398 /* Size-dependent data and functions. */
5399 static const struct elf_size_info elf64_ia64_vms_size_info = {
5400 sizeof (Elf64_External_VMS_Ehdr),
5401 sizeof (Elf64_External_Phdr),
5402 sizeof (Elf64_External_Shdr),
5403 sizeof (Elf64_External_Rel),
5404 sizeof (Elf64_External_Rela),
5405 sizeof (Elf64_External_Sym),
5406 sizeof (Elf64_External_Dyn),
5407 sizeof (Elf_External_Note),
5410 64, 3, /* ARCH_SIZE, LOG_FILE_ALIGN */
5411 ELFCLASS64, EV_CURRENT,
5412 bfd_elf64_write_out_phdrs,
5413 elf64_vms_write_shdrs_and_ehdr,
5414 bfd_elf64_checksum_contents,
5415 bfd_elf64_write_relocs,
5416 bfd_elf64_swap_symbol_in,
5417 bfd_elf64_swap_symbol_out,
5418 bfd_elf64_slurp_reloc_table,
5419 bfd_elf64_slurp_symbol_table,
5420 bfd_elf64_swap_dyn_in,
5421 bfd_elf64_swap_dyn_out,
5422 bfd_elf64_swap_reloc_in,
5423 bfd_elf64_swap_reloc_out,
5424 bfd_elf64_swap_reloca_in,
5425 bfd_elf64_swap_reloca_out
5428 #define ELF_ARCH bfd_arch_ia64
5429 #define ELF_MACHINE_CODE EM_IA_64
5430 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
5431 #define ELF_COMMONPAGESIZE 0x200 /* 16KB */
5433 #define elf_backend_section_from_shdr \
5434 elf64_ia64_section_from_shdr
5435 #define elf_backend_section_flags \
5436 elf64_ia64_section_flags
5437 #define elf_backend_fake_sections \
5438 elf64_ia64_fake_sections
5439 #define elf_backend_final_write_processing \
5440 elf64_ia64_final_write_processing
5441 #define elf_backend_add_symbol_hook \
5442 elf64_ia64_add_symbol_hook
5443 #define elf_info_to_howto \
5444 elf64_ia64_info_to_howto
5446 #define bfd_elf64_bfd_reloc_type_lookup \
5447 ia64_elf_reloc_type_lookup
5448 #define bfd_elf64_bfd_reloc_name_lookup \
5449 ia64_elf_reloc_name_lookup
5450 #define bfd_elf64_bfd_is_local_label_name \
5451 elf64_ia64_is_local_label_name
5452 #define bfd_elf64_bfd_relax_section \
5453 elf64_ia64_relax_section
5455 #define elf_backend_object_p \
5456 elf64_ia64_object_p
5458 /* Stuff for the BFD linker: */
5459 #define bfd_elf64_bfd_link_hash_table_create \
5460 elf64_ia64_hash_table_create
5461 #define bfd_elf64_bfd_link_hash_table_free \
5462 elf64_ia64_hash_table_free
5463 #define elf_backend_create_dynamic_sections \
5464 elf64_ia64_create_dynamic_sections
5465 #define elf_backend_check_relocs \
5466 elf64_ia64_check_relocs
5467 #define elf_backend_adjust_dynamic_symbol \
5468 elf64_ia64_adjust_dynamic_symbol
5469 #define elf_backend_size_dynamic_sections \
5470 elf64_ia64_size_dynamic_sections
5471 #define elf_backend_omit_section_dynsym \
5472 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
5473 #define elf_backend_relocate_section \
5474 elf64_ia64_relocate_section
5475 #define elf_backend_finish_dynamic_symbol \
5476 elf64_ia64_finish_dynamic_symbol
5477 #define elf_backend_finish_dynamic_sections \
5478 elf64_ia64_finish_dynamic_sections
5479 #define bfd_elf64_bfd_final_link \
5480 elf64_ia64_final_link
5482 #define bfd_elf64_bfd_merge_private_bfd_data \
5483 elf64_ia64_merge_private_bfd_data
5484 #define bfd_elf64_bfd_set_private_flags \
5485 elf64_ia64_set_private_flags
5486 #define bfd_elf64_bfd_print_private_bfd_data \
5487 elf64_ia64_print_private_bfd_data
5489 #define elf_backend_plt_readonly 1
5490 #define elf_backend_want_plt_sym 0
5491 #define elf_backend_plt_alignment 5
5492 #define elf_backend_got_header_size 0
5493 #define elf_backend_want_got_plt 1
5494 #define elf_backend_may_use_rel_p 1
5495 #define elf_backend_may_use_rela_p 1
5496 #define elf_backend_default_use_rela_p 1
5497 #define elf_backend_want_dynbss 0
5498 #define elf_backend_hide_symbol elf64_ia64_hash_hide_symbol
5499 #define elf_backend_fixup_symbol _bfd_elf_link_hash_fixup_symbol
5500 #define elf_backend_reloc_type_class elf64_ia64_reloc_type_class
5501 #define elf_backend_rela_normal 1
5502 #define elf_backend_special_sections elf64_ia64_special_sections
5503 #define elf_backend_default_execstack 0
5505 /* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
5506 SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
5507 We don't want to flood users with so many error messages. We turn
5508 off the warning for now. It will be turned on later when the Intel
5509 compiler is fixed. */
5510 #define elf_backend_link_order_error_handler NULL
5512 /* VMS-specific vectors. */
5514 #undef TARGET_LITTLE_SYM
5515 #define TARGET_LITTLE_SYM bfd_elf64_ia64_vms_vec
5516 #undef TARGET_LITTLE_NAME
5517 #define TARGET_LITTLE_NAME "elf64-ia64-vms"
5518 #undef TARGET_BIG_SYM
5519 #undef TARGET_BIG_NAME
5521 /* These are VMS specific functions. */
5523 #undef elf_backend_object_p
5524 #define elf_backend_object_p elf64_vms_object_p
5526 #undef elf_backend_section_from_shdr
5527 #define elf_backend_section_from_shdr elf64_vms_section_from_shdr
5529 #undef elf_backend_post_process_headers
5530 #define elf_backend_post_process_headers elf64_vms_post_process_headers
5532 #undef elf_backend_section_processing
5533 #define elf_backend_section_processing elf64_vms_section_processing
5535 #undef elf_backend_final_write_processing
5536 #define elf_backend_final_write_processing elf64_vms_final_write_processing
5538 #undef bfd_elf64_close_and_cleanup
5539 #define bfd_elf64_close_and_cleanup elf64_vms_close_and_cleanup
5541 #undef elf_backend_section_from_bfd_section
5543 #undef elf_backend_symbol_processing
5545 #undef elf_backend_want_p_paddr_set_to_zero
5547 #undef ELF_OSABI
5548 #define ELF_OSABI ELFOSABI_OPENVMS
5550 #undef ELF_MAXPAGESIZE
5551 #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
5553 #undef elf64_bed
5554 #define elf64_bed elf64_ia64_vms_bed
5556 #define elf_backend_size_info elf64_ia64_vms_size_info
5558 /* Use VMS-style archives (in particular, don't use the standard coff
5559 archive format). */
5560 #define bfd_elf64_archive_functions
5562 #undef bfd_elf64_archive_p
5563 #define bfd_elf64_archive_p _bfd_vms_lib_ia64_archive_p
5564 #undef bfd_elf64_write_archive_contents
5565 #define bfd_elf64_write_archive_contents _bfd_vms_lib_write_archive_contents
5566 #undef bfd_elf64_mkarchive
5567 #define bfd_elf64_mkarchive _bfd_vms_lib_ia64_mkarchive
5569 #define bfd_elf64_archive_slurp_armap \
5570 _bfd_vms_lib_slurp_armap
5571 #define bfd_elf64_archive_slurp_extended_name_table \
5572 _bfd_vms_lib_slurp_extended_name_table
5573 #define bfd_elf64_archive_construct_extended_name_table \
5574 _bfd_vms_lib_construct_extended_name_table
5575 #define bfd_elf64_archive_truncate_arname \
5576 _bfd_vms_lib_truncate_arname
5577 #define bfd_elf64_archive_write_armap \
5578 _bfd_vms_lib_write_armap
5579 #define bfd_elf64_archive_read_ar_hdr \
5580 _bfd_vms_lib_read_ar_hdr
5581 #define bfd_elf64_archive_write_ar_hdr \
5582 _bfd_vms_lib_write_ar_hdr
5583 #define bfd_elf64_archive_openr_next_archived_file \
5584 _bfd_vms_lib_openr_next_archived_file
5585 #define bfd_elf64_archive_get_elt_at_index \
5586 _bfd_vms_lib_get_elt_at_index
5587 #define bfd_elf64_archive_generic_stat_arch_elt \
5588 _bfd_vms_lib_generic_stat_arch_elt
5589 #define bfd_elf64_archive_update_armap_timestamp \
5590 _bfd_vms_lib_update_armap_timestamp
5592 /* VMS link methods. */
5593 #undef bfd_elf64_bfd_link_add_symbols
5594 #define bfd_elf64_bfd_link_add_symbols elf64_vms_bfd_link_add_symbols
5596 #undef elf_backend_want_got_sym
5597 #define elf_backend_want_got_sym 0
5599 #undef bfd_elf64_mkobject
5600 #define bfd_elf64_mkobject elf64_ia64_vms_mkobject
5602 /* Redefine to align segments on block size. */
5603 #undef ELF_MAXPAGESIZE
5604 #define ELF_MAXPAGESIZE 0x200 /* 512B */
5606 #undef elf_backend_want_got_plt
5607 #define elf_backend_want_got_plt 0
5609 #include "elf64-target.h"