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