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