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