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