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