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