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