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