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