Home | History | Annotate | Download | only in bfd
      1 /* IA-64 support for 64-bit ELF
      2    Copyright (C) 1998-2014 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 /* Free the global elfNN_ia64_dyn_sym_info array.  */
   1378 
   1379 static bfd_boolean
   1380 elfNN_ia64_global_dyn_info_free (void **xentry,
   1381 				 void * unused ATTRIBUTE_UNUSED)
   1382 {
   1383   struct elfNN_ia64_link_hash_entry *entry
   1384     = (struct elfNN_ia64_link_hash_entry *) xentry;
   1385 
   1386   if (entry->info)
   1387     {
   1388       free (entry->info);
   1389       entry->info = NULL;
   1390       entry->count = 0;
   1391       entry->sorted_count = 0;
   1392       entry->size = 0;
   1393     }
   1394 
   1395   return TRUE;
   1396 }
   1397 
   1398 /* Free the local elfNN_ia64_dyn_sym_info array.  */
   1399 
   1400 static bfd_boolean
   1401 elfNN_ia64_local_dyn_info_free (void **slot,
   1402 				void * unused ATTRIBUTE_UNUSED)
   1403 {
   1404   struct elfNN_ia64_local_hash_entry *entry
   1405     = (struct elfNN_ia64_local_hash_entry *) *slot;
   1406 
   1407   if (entry->info)
   1408     {
   1409       free (entry->info);
   1410       entry->info = NULL;
   1411       entry->count = 0;
   1412       entry->sorted_count = 0;
   1413       entry->size = 0;
   1414     }
   1415 
   1416   return TRUE;
   1417 }
   1418 
   1419 /* Destroy IA-64 linker hash table.  */
   1420 
   1421 static void
   1422 elfNN_ia64_link_hash_table_free (bfd *obfd)
   1423 {
   1424   struct elfNN_ia64_link_hash_table *ia64_info
   1425     = (struct elfNN_ia64_link_hash_table *) obfd->link.hash;
   1426   if (ia64_info->loc_hash_table)
   1427     {
   1428       htab_traverse (ia64_info->loc_hash_table,
   1429 		     elfNN_ia64_local_dyn_info_free, NULL);
   1430       htab_delete (ia64_info->loc_hash_table);
   1431     }
   1432   if (ia64_info->loc_hash_memory)
   1433     objalloc_free ((struct objalloc *) ia64_info->loc_hash_memory);
   1434   elf_link_hash_traverse (&ia64_info->root,
   1435 			  elfNN_ia64_global_dyn_info_free, NULL);
   1436   _bfd_elf_link_hash_table_free (obfd);
   1437 }
   1438 
   1439 /* Create the derived linker hash table.  The IA-64 ELF port uses this
   1440    derived hash table to keep information specific to the IA-64 ElF
   1441    linker (without using static variables).  */
   1442 
   1443 static struct bfd_link_hash_table *
   1444 elfNN_ia64_hash_table_create (bfd *abfd)
   1445 {
   1446   struct elfNN_ia64_link_hash_table *ret;
   1447 
   1448   ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
   1449   if (!ret)
   1450     return NULL;
   1451 
   1452   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   1453 				      elfNN_ia64_new_elf_hash_entry,
   1454 				      sizeof (struct elfNN_ia64_link_hash_entry),
   1455 				      IA64_ELF_DATA))
   1456     {
   1457       free (ret);
   1458       return NULL;
   1459     }
   1460 
   1461   ret->loc_hash_table = htab_try_create (1024, elfNN_ia64_local_htab_hash,
   1462 					 elfNN_ia64_local_htab_eq, NULL);
   1463   ret->loc_hash_memory = objalloc_create ();
   1464   if (!ret->loc_hash_table || !ret->loc_hash_memory)
   1465     {
   1466       elfNN_ia64_link_hash_table_free (abfd);
   1467       return NULL;
   1468     }
   1469   ret->root.root.hash_table_free = elfNN_ia64_link_hash_table_free;
   1470 
   1471   return &ret->root.root;
   1472 }
   1473 
   1474 /* Traverse both local and global hash tables.  */
   1475 
   1476 struct elfNN_ia64_dyn_sym_traverse_data
   1477 {
   1478   bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, void *);
   1479   void * data;
   1480 };
   1481 
   1482 static bfd_boolean
   1483 elfNN_ia64_global_dyn_sym_thunk (struct bfd_hash_entry *xentry,
   1484 				 void * xdata)
   1485 {
   1486   struct elfNN_ia64_link_hash_entry *entry
   1487     = (struct elfNN_ia64_link_hash_entry *) xentry;
   1488   struct elfNN_ia64_dyn_sym_traverse_data *data
   1489     = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
   1490   struct elfNN_ia64_dyn_sym_info *dyn_i;
   1491   unsigned int count;
   1492 
   1493   for (count = entry->count, dyn_i = entry->info;
   1494        count != 0;
   1495        count--, dyn_i++)
   1496     if (! (*data->func) (dyn_i, data->data))
   1497       return FALSE;
   1498   return TRUE;
   1499 }
   1500 
   1501 static bfd_boolean
   1502 elfNN_ia64_local_dyn_sym_thunk (void **slot, void * xdata)
   1503 {
   1504   struct elfNN_ia64_local_hash_entry *entry
   1505     = (struct elfNN_ia64_local_hash_entry *) *slot;
   1506   struct elfNN_ia64_dyn_sym_traverse_data *data
   1507     = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
   1508   struct elfNN_ia64_dyn_sym_info *dyn_i;
   1509   unsigned int count;
   1510 
   1511   for (count = entry->count, dyn_i = entry->info;
   1512        count != 0;
   1513        count--, dyn_i++)
   1514     if (! (*data->func) (dyn_i, data->data))
   1515       return FALSE;
   1516   return TRUE;
   1517 }
   1518 
   1519 static void
   1520 elfNN_ia64_dyn_sym_traverse (struct elfNN_ia64_link_hash_table *ia64_info,
   1521 			     bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, void *),
   1522 			     void * data)
   1523 {
   1524   struct elfNN_ia64_dyn_sym_traverse_data xdata;
   1525 
   1526   xdata.func = func;
   1527   xdata.data = data;
   1528 
   1529   elf_link_hash_traverse (&ia64_info->root,
   1530 			  elfNN_ia64_global_dyn_sym_thunk, &xdata);
   1531   htab_traverse (ia64_info->loc_hash_table,
   1532 		 elfNN_ia64_local_dyn_sym_thunk, &xdata);
   1533 }
   1534 
   1535 static bfd_boolean
   1537 elfNN_ia64_create_dynamic_sections (bfd *abfd,
   1538 				    struct bfd_link_info *info)
   1539 {
   1540   struct elfNN_ia64_link_hash_table *ia64_info;
   1541   asection *s;
   1542 
   1543   if (! _bfd_elf_create_dynamic_sections (abfd, info))
   1544     return FALSE;
   1545 
   1546   ia64_info = elfNN_ia64_hash_table (info);
   1547   if (ia64_info == NULL)
   1548     return FALSE;
   1549 
   1550   {
   1551     flagword flags = bfd_get_section_flags (abfd, ia64_info->root.sgot);
   1552     bfd_set_section_flags (abfd, ia64_info->root.sgot,
   1553 			   SEC_SMALL_DATA | flags);
   1554     /* The .got section is always aligned at 8 bytes.  */
   1555     if (! bfd_set_section_alignment (abfd, ia64_info->root.sgot, 3))
   1556       return FALSE;
   1557   }
   1558 
   1559   if (!get_pltoff (abfd, info, ia64_info))
   1560     return FALSE;
   1561 
   1562   s = bfd_make_section_anyway_with_flags (abfd, ".rela.IA_64.pltoff",
   1563 					  (SEC_ALLOC | SEC_LOAD
   1564 					   | SEC_HAS_CONTENTS
   1565 					   | SEC_IN_MEMORY
   1566 					   | SEC_LINKER_CREATED
   1567 					   | SEC_READONLY));
   1568   if (s == NULL
   1569       || !bfd_set_section_alignment (abfd, s, LOG_SECTION_ALIGN))
   1570     return FALSE;
   1571   ia64_info->rel_pltoff_sec = s;
   1572 
   1573   return TRUE;
   1574 }
   1575 
   1576 /* Find and/or create a hash entry for local symbol.  */
   1577 static struct elfNN_ia64_local_hash_entry *
   1578 get_local_sym_hash (struct elfNN_ia64_link_hash_table *ia64_info,
   1579 		    bfd *abfd, const Elf_Internal_Rela *rel,
   1580 		    bfd_boolean create)
   1581 {
   1582   struct elfNN_ia64_local_hash_entry e, *ret;
   1583   asection *sec = abfd->sections;
   1584   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
   1585 				       ELFNN_R_SYM (rel->r_info));
   1586   void **slot;
   1587 
   1588   e.id = sec->id;
   1589   e.r_sym = ELFNN_R_SYM (rel->r_info);
   1590   slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
   1591 				   create ? INSERT : NO_INSERT);
   1592 
   1593   if (!slot)
   1594     return NULL;
   1595 
   1596   if (*slot)
   1597     return (struct elfNN_ia64_local_hash_entry *) *slot;
   1598 
   1599   ret = (struct elfNN_ia64_local_hash_entry *)
   1600 	objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
   1601 			sizeof (struct elfNN_ia64_local_hash_entry));
   1602   if (ret)
   1603     {
   1604       memset (ret, 0, sizeof (*ret));
   1605       ret->id = sec->id;
   1606       ret->r_sym = ELFNN_R_SYM (rel->r_info);
   1607       *slot = ret;
   1608     }
   1609   return ret;
   1610 }
   1611 
   1612 /* Used to sort elfNN_ia64_dyn_sym_info array.  */
   1613 
   1614 static int
   1615 addend_compare (const void *xp, const void *yp)
   1616 {
   1617   const struct elfNN_ia64_dyn_sym_info *x
   1618     = (const struct elfNN_ia64_dyn_sym_info *) xp;
   1619   const struct elfNN_ia64_dyn_sym_info *y
   1620     = (const struct elfNN_ia64_dyn_sym_info *) yp;
   1621 
   1622   return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
   1623 }
   1624 
   1625 /* Sort elfNN_ia64_dyn_sym_info array and remove duplicates.  */
   1626 
   1627 static unsigned int
   1628 sort_dyn_sym_info (struct elfNN_ia64_dyn_sym_info *info,
   1629 		   unsigned int count)
   1630 {
   1631   bfd_vma curr, prev, got_offset;
   1632   unsigned int i, kept, dupes, diff, dest, src, len;
   1633 
   1634   qsort (info, count, sizeof (*info), addend_compare);
   1635 
   1636   /* Find the first duplicate.  */
   1637   prev = info [0].addend;
   1638   got_offset = info [0].got_offset;
   1639   for (i = 1; i < count; i++)
   1640     {
   1641       curr = info [i].addend;
   1642       if (curr == prev)
   1643 	{
   1644 	  /* For duplicates, make sure that GOT_OFFSET is valid.  */
   1645 	  if (got_offset == (bfd_vma) -1)
   1646 	    got_offset = info [i].got_offset;
   1647 	  break;
   1648 	}
   1649       got_offset = info [i].got_offset;
   1650       prev = curr;
   1651     }
   1652 
   1653   /* We may move a block of elements to here.  */
   1654   dest = i++;
   1655 
   1656   /* Remove duplicates.  */
   1657   if (i < count)
   1658     {
   1659       while (i < count)
   1660 	{
   1661 	  /* For duplicates, make sure that the kept one has a valid
   1662 	     got_offset.  */
   1663 	  kept = dest - 1;
   1664 	  if (got_offset != (bfd_vma) -1)
   1665 	    info [kept].got_offset = got_offset;
   1666 
   1667 	  curr = info [i].addend;
   1668 	  got_offset = info [i].got_offset;
   1669 
   1670 	  /* Move a block of elements whose first one is different from
   1671 	     the previous.  */
   1672 	  if (curr == prev)
   1673 	    {
   1674 	      for (src = i + 1; src < count; src++)
   1675 		{
   1676 		  if (info [src].addend != curr)
   1677 		    break;
   1678 		  /* For duplicates, make sure that GOT_OFFSET is
   1679 		     valid.  */
   1680 		  if (got_offset == (bfd_vma) -1)
   1681 		    got_offset = info [src].got_offset;
   1682 		}
   1683 
   1684 	      /* Make sure that the kept one has a valid got_offset.  */
   1685 	      if (got_offset != (bfd_vma) -1)
   1686 		info [kept].got_offset = got_offset;
   1687 	    }
   1688 	  else
   1689 	    src = i;
   1690 
   1691 	  if (src >= count)
   1692 	    break;
   1693 
   1694 	  /* Find the next duplicate.  SRC will be kept.  */
   1695 	  prev = info [src].addend;
   1696 	  got_offset = info [src].got_offset;
   1697 	  for (dupes = src + 1; dupes < count; dupes ++)
   1698 	    {
   1699 	      curr = info [dupes].addend;
   1700 	      if (curr == prev)
   1701 		{
   1702 		  /* Make sure that got_offset is valid.  */
   1703 		  if (got_offset == (bfd_vma) -1)
   1704 		    got_offset = info [dupes].got_offset;
   1705 
   1706 		  /* For duplicates, make sure that the kept one has
   1707 		     a valid got_offset.  */
   1708 		  if (got_offset != (bfd_vma) -1)
   1709 		    info [dupes - 1].got_offset = got_offset;
   1710 		  break;
   1711 		}
   1712 	      got_offset = info [dupes].got_offset;
   1713 	      prev = curr;
   1714 	    }
   1715 
   1716 	  /* How much to move.  */
   1717 	  len = dupes - src;
   1718 	  i = dupes + 1;
   1719 
   1720 	  if (len == 1 && dupes < count)
   1721 	    {
   1722 	      /* If we only move 1 element, we combine it with the next
   1723 		 one.  There must be at least a duplicate.  Find the
   1724 		 next different one.  */
   1725 	      for (diff = dupes + 1, src++; diff < count; diff++, src++)
   1726 		{
   1727 		  if (info [diff].addend != curr)
   1728 		    break;
   1729 		  /* Make sure that got_offset is valid.  */
   1730 		  if (got_offset == (bfd_vma) -1)
   1731 		    got_offset = info [diff].got_offset;
   1732 		}
   1733 
   1734 	      /* Makre sure that the last duplicated one has an valid
   1735 		 offset.  */
   1736 	      BFD_ASSERT (curr == prev);
   1737 	      if (got_offset != (bfd_vma) -1)
   1738 		info [diff - 1].got_offset = got_offset;
   1739 
   1740 	      if (diff < count)
   1741 		{
   1742 		  /* Find the next duplicate.  Track the current valid
   1743 		     offset.  */
   1744 		  prev = info [diff].addend;
   1745 		  got_offset = info [diff].got_offset;
   1746 		  for (dupes = diff + 1; dupes < count; dupes ++)
   1747 		    {
   1748 		      curr = info [dupes].addend;
   1749 		      if (curr == prev)
   1750 			{
   1751 			  /* For duplicates, make sure that GOT_OFFSET
   1752 			     is valid.  */
   1753 			  if (got_offset == (bfd_vma) -1)
   1754 			    got_offset = info [dupes].got_offset;
   1755 			  break;
   1756 			}
   1757 		      got_offset = info [dupes].got_offset;
   1758 		      prev = curr;
   1759 		      diff++;
   1760 		    }
   1761 
   1762 		  len = diff - src + 1;
   1763 		  i = diff + 1;
   1764 		}
   1765 	    }
   1766 
   1767 	  memmove (&info [dest], &info [src], len * sizeof (*info));
   1768 
   1769 	  dest += len;
   1770 	}
   1771 
   1772       count = dest;
   1773     }
   1774   else
   1775     {
   1776       /* When we get here, either there is no duplicate at all or
   1777 	 the only duplicate is the last element.  */
   1778       if (dest < count)
   1779 	{
   1780 	  /* If the last element is a duplicate, make sure that the
   1781 	     kept one has a valid got_offset.  We also update count.  */
   1782 	  if (got_offset != (bfd_vma) -1)
   1783 	    info [dest - 1].got_offset = got_offset;
   1784 	  count = dest;
   1785 	}
   1786     }
   1787 
   1788   return count;
   1789 }
   1790 
   1791 /* Find and/or create a descriptor for dynamic symbol info.  This will
   1792    vary based on global or local symbol, and the addend to the reloc.
   1793 
   1794    We don't sort when inserting.  Also, we sort and eliminate
   1795    duplicates if there is an unsorted section.  Typically, this will
   1796    only happen once, because we do all insertions before lookups.  We
   1797    then use bsearch to do a lookup.  This also allows lookups to be
   1798    fast.  So we have fast insertion (O(log N) due to duplicate check),
   1799    fast lookup (O(log N)) and one sort (O(N log N) expected time).
   1800    Previously, all lookups were O(N) because of the use of the linked
   1801    list and also all insertions were O(N) because of the check for
   1802    duplicates.  There are some complications here because the array
   1803    size grows occasionally, which may add an O(N) factor, but this
   1804    should be rare.  Also,  we free the excess array allocation, which
   1805    requires a copy which is O(N), but this only happens once.  */
   1806 
   1807 static struct elfNN_ia64_dyn_sym_info *
   1808 get_dyn_sym_info (struct elfNN_ia64_link_hash_table *ia64_info,
   1809 		  struct elf_link_hash_entry *h, bfd *abfd,
   1810 		  const Elf_Internal_Rela *rel, bfd_boolean create)
   1811 {
   1812   struct elfNN_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
   1813   unsigned int *count_p, *sorted_count_p, *size_p;
   1814   unsigned int count, sorted_count, size;
   1815   bfd_vma addend = rel ? rel->r_addend : 0;
   1816   bfd_size_type amt;
   1817 
   1818   if (h)
   1819     {
   1820       struct elfNN_ia64_link_hash_entry *global_h;
   1821 
   1822       global_h = (struct elfNN_ia64_link_hash_entry *) h;
   1823       info_p = &global_h->info;
   1824       count_p = &global_h->count;
   1825       sorted_count_p = &global_h->sorted_count;
   1826       size_p = &global_h->size;
   1827     }
   1828   else
   1829     {
   1830       struct elfNN_ia64_local_hash_entry *loc_h;
   1831 
   1832       loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
   1833       if (!loc_h)
   1834 	{
   1835 	  BFD_ASSERT (!create);
   1836 	  return NULL;
   1837 	}
   1838 
   1839       info_p = &loc_h->info;
   1840       count_p = &loc_h->count;
   1841       sorted_count_p = &loc_h->sorted_count;
   1842       size_p = &loc_h->size;
   1843     }
   1844 
   1845   count = *count_p;
   1846   sorted_count = *sorted_count_p;
   1847   size = *size_p;
   1848   info = *info_p;
   1849   if (create)
   1850     {
   1851       /* When we create the array, we don't check for duplicates,
   1852          except in the previously sorted section if one exists, and
   1853 	 against the last inserted entry.  This allows insertions to
   1854 	 be fast.  */
   1855       if (info)
   1856 	{
   1857 	  if (sorted_count)
   1858 	    {
   1859 	      /* Try bsearch first on the sorted section.  */
   1860 	      key.addend = addend;
   1861 	      dyn_i = bsearch (&key, info, sorted_count,
   1862 			       sizeof (*info), addend_compare);
   1863 
   1864 	      if (dyn_i)
   1865 		{
   1866 		  return dyn_i;
   1867 		}
   1868 	    }
   1869 
   1870 	  /* Do a quick check for the last inserted entry.  */
   1871 	  dyn_i = info + count - 1;
   1872 	  if (dyn_i->addend == addend)
   1873 	    {
   1874 	      return dyn_i;
   1875 	    }
   1876 	}
   1877 
   1878       if (size == 0)
   1879 	{
   1880 	  /* It is the very first element. We create the array of size
   1881 	     1.  */
   1882 	  size = 1;
   1883 	  amt = size * sizeof (*info);
   1884 	  info = bfd_malloc (amt);
   1885 	}
   1886       else if (size <= count)
   1887 	{
   1888 	  /* We double the array size every time when we reach the
   1889 	     size limit.  */
   1890 	  size += size;
   1891 	  amt = size * sizeof (*info);
   1892 	  info = bfd_realloc (info, amt);
   1893 	}
   1894       else
   1895 	goto has_space;
   1896 
   1897       if (info == NULL)
   1898 	return NULL;
   1899       *size_p = size;
   1900       *info_p = info;
   1901 
   1902 has_space:
   1903       /* Append the new one to the array.  */
   1904       dyn_i = info + count;
   1905       memset (dyn_i, 0, sizeof (*dyn_i));
   1906       dyn_i->got_offset = (bfd_vma) -1;
   1907       dyn_i->addend = addend;
   1908 
   1909       /* We increment count only since the new ones are unsorted and
   1910 	 may have duplicate.  */
   1911       (*count_p)++;
   1912     }
   1913   else
   1914     {
   1915       /* It is a lookup without insertion.  Sort array if part of the
   1916 	 array isn't sorted.  */
   1917       if (count != sorted_count)
   1918 	{
   1919 	  count = sort_dyn_sym_info (info, count);
   1920 	  *count_p = count;
   1921 	  *sorted_count_p = count;
   1922 	}
   1923 
   1924       /* Free unused memory.  */
   1925       if (size != count)
   1926 	{
   1927 	  amt = count * sizeof (*info);
   1928 	  info = bfd_malloc (amt);
   1929 	  if (info != NULL)
   1930 	    {
   1931 	      memcpy (info, *info_p, amt);
   1932 	      free (*info_p);
   1933 	      *size_p = count;
   1934 	      *info_p = info;
   1935 	    }
   1936 	}
   1937 
   1938       key.addend = addend;
   1939       dyn_i = bsearch (&key, info, count,
   1940 		       sizeof (*info), addend_compare);
   1941     }
   1942 
   1943   return dyn_i;
   1944 }
   1945 
   1946 static asection *
   1947 get_got (bfd *abfd, struct bfd_link_info *info,
   1948 	 struct elfNN_ia64_link_hash_table *ia64_info)
   1949 {
   1950   asection *got;
   1951   bfd *dynobj;
   1952 
   1953   got = ia64_info->root.sgot;
   1954   if (!got)
   1955     {
   1956       flagword flags;
   1957 
   1958       dynobj = ia64_info->root.dynobj;
   1959       if (!dynobj)
   1960 	ia64_info->root.dynobj = dynobj = abfd;
   1961       if (!_bfd_elf_create_got_section (dynobj, info))
   1962 	return NULL;
   1963 
   1964       got = ia64_info->root.sgot;
   1965 
   1966       /* The .got section is always aligned at 8 bytes.  */
   1967       if (!bfd_set_section_alignment (abfd, got, 3))
   1968 	return NULL;
   1969 
   1970       flags = bfd_get_section_flags (abfd, got);
   1971       if (! bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags))
   1972 	return NULL;
   1973     }
   1974 
   1975   return got;
   1976 }
   1977 
   1978 /* Create function descriptor section (.opd).  This section is called .opd
   1979    because it contains "official procedure descriptors".  The "official"
   1980    refers to the fact that these descriptors are used when taking the address
   1981    of a procedure, thus ensuring a unique address for each procedure.  */
   1982 
   1983 static asection *
   1984 get_fptr (bfd *abfd, struct bfd_link_info *info,
   1985 	  struct elfNN_ia64_link_hash_table *ia64_info)
   1986 {
   1987   asection *fptr;
   1988   bfd *dynobj;
   1989 
   1990   fptr = ia64_info->fptr_sec;
   1991   if (!fptr)
   1992     {
   1993       dynobj = ia64_info->root.dynobj;
   1994       if (!dynobj)
   1995 	ia64_info->root.dynobj = dynobj = abfd;
   1996 
   1997       fptr = bfd_make_section_anyway_with_flags (dynobj, ".opd",
   1998 						 (SEC_ALLOC
   1999 						  | SEC_LOAD
   2000 						  | SEC_HAS_CONTENTS
   2001 						  | SEC_IN_MEMORY
   2002 						  | (info->pie ? 0
   2003 						     : SEC_READONLY)
   2004 						  | SEC_LINKER_CREATED));
   2005       if (!fptr
   2006 	  || !bfd_set_section_alignment (abfd, fptr, 4))
   2007 	{
   2008 	  BFD_ASSERT (0);
   2009 	  return NULL;
   2010 	}
   2011 
   2012       ia64_info->fptr_sec = fptr;
   2013 
   2014       if (info->pie)
   2015 	{
   2016 	  asection *fptr_rel;
   2017 	  fptr_rel = bfd_make_section_anyway_with_flags (dynobj, ".rela.opd",
   2018 							 (SEC_ALLOC | SEC_LOAD
   2019 							  | SEC_HAS_CONTENTS
   2020 							  | SEC_IN_MEMORY
   2021 							  | SEC_LINKER_CREATED
   2022 							  | SEC_READONLY));
   2023 	  if (fptr_rel == NULL
   2024 	      || !bfd_set_section_alignment (abfd, fptr_rel,
   2025 					     LOG_SECTION_ALIGN))
   2026 	    {
   2027 	      BFD_ASSERT (0);
   2028 	      return NULL;
   2029 	    }
   2030 
   2031 	  ia64_info->rel_fptr_sec = fptr_rel;
   2032 	}
   2033     }
   2034 
   2035   return fptr;
   2036 }
   2037 
   2038 static asection *
   2039 get_pltoff (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2040 	    struct elfNN_ia64_link_hash_table *ia64_info)
   2041 {
   2042   asection *pltoff;
   2043   bfd *dynobj;
   2044 
   2045   pltoff = ia64_info->pltoff_sec;
   2046   if (!pltoff)
   2047     {
   2048       dynobj = ia64_info->root.dynobj;
   2049       if (!dynobj)
   2050 	ia64_info->root.dynobj = dynobj = abfd;
   2051 
   2052       pltoff = bfd_make_section_anyway_with_flags (dynobj,
   2053 						   ELF_STRING_ia64_pltoff,
   2054 						   (SEC_ALLOC
   2055 						    | SEC_LOAD
   2056 						    | SEC_HAS_CONTENTS
   2057 						    | SEC_IN_MEMORY
   2058 						    | SEC_SMALL_DATA
   2059 						    | SEC_LINKER_CREATED));
   2060       if (!pltoff
   2061 	  || !bfd_set_section_alignment (abfd, pltoff, 4))
   2062 	{
   2063 	  BFD_ASSERT (0);
   2064 	  return NULL;
   2065 	}
   2066 
   2067       ia64_info->pltoff_sec = pltoff;
   2068     }
   2069 
   2070   return pltoff;
   2071 }
   2072 
   2073 static asection *
   2074 get_reloc_section (bfd *abfd,
   2075 		   struct elfNN_ia64_link_hash_table *ia64_info,
   2076 		   asection *sec, bfd_boolean create)
   2077 {
   2078   const char *srel_name;
   2079   asection *srel;
   2080   bfd *dynobj;
   2081 
   2082   srel_name = (bfd_elf_string_from_elf_section
   2083 	       (abfd, elf_elfheader(abfd)->e_shstrndx,
   2084 		_bfd_elf_single_rel_hdr (sec)->sh_name));
   2085   if (srel_name == NULL)
   2086     return NULL;
   2087 
   2088   dynobj = ia64_info->root.dynobj;
   2089   if (!dynobj)
   2090     ia64_info->root.dynobj = dynobj = abfd;
   2091 
   2092   srel = bfd_get_linker_section (dynobj, srel_name);
   2093   if (srel == NULL && create)
   2094     {
   2095       srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
   2096 						 (SEC_ALLOC | SEC_LOAD
   2097 						  | SEC_HAS_CONTENTS
   2098 						  | SEC_IN_MEMORY
   2099 						  | SEC_LINKER_CREATED
   2100 						  | SEC_READONLY));
   2101       if (srel == NULL
   2102 	  || !bfd_set_section_alignment (dynobj, srel,
   2103 					 LOG_SECTION_ALIGN))
   2104 	return NULL;
   2105     }
   2106 
   2107   return srel;
   2108 }
   2109 
   2110 static bfd_boolean
   2111 count_dyn_reloc (bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
   2112 		 asection *srel, int type, bfd_boolean reltext)
   2113 {
   2114   struct elfNN_ia64_dyn_reloc_entry *rent;
   2115 
   2116   for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
   2117     if (rent->srel == srel && rent->type == type)
   2118       break;
   2119 
   2120   if (!rent)
   2121     {
   2122       rent = ((struct elfNN_ia64_dyn_reloc_entry *)
   2123 	      bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
   2124       if (!rent)
   2125 	return FALSE;
   2126 
   2127       rent->next = dyn_i->reloc_entries;
   2128       rent->srel = srel;
   2129       rent->type = type;
   2130       rent->count = 0;
   2131       dyn_i->reloc_entries = rent;
   2132     }
   2133   rent->reltext = reltext;
   2134   rent->count++;
   2135 
   2136   return TRUE;
   2137 }
   2138 
   2139 static bfd_boolean
   2140 elfNN_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
   2141 			 asection *sec,
   2142 			 const Elf_Internal_Rela *relocs)
   2143 {
   2144   struct elfNN_ia64_link_hash_table *ia64_info;
   2145   const Elf_Internal_Rela *relend;
   2146   Elf_Internal_Shdr *symtab_hdr;
   2147   const Elf_Internal_Rela *rel;
   2148   asection *got, *fptr, *srel, *pltoff;
   2149   enum {
   2150     NEED_GOT = 1,
   2151     NEED_GOTX = 2,
   2152     NEED_FPTR = 4,
   2153     NEED_PLTOFF = 8,
   2154     NEED_MIN_PLT = 16,
   2155     NEED_FULL_PLT = 32,
   2156     NEED_DYNREL = 64,
   2157     NEED_LTOFF_FPTR = 128,
   2158     NEED_TPREL = 256,
   2159     NEED_DTPMOD = 512,
   2160     NEED_DTPREL = 1024
   2161   };
   2162   int need_entry;
   2163   struct elf_link_hash_entry *h;
   2164   unsigned long r_symndx;
   2165   bfd_boolean maybe_dynamic;
   2166 
   2167   if (info->relocatable)
   2168     return TRUE;
   2169 
   2170   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2171   ia64_info = elfNN_ia64_hash_table (info);
   2172   if (ia64_info == NULL)
   2173     return FALSE;
   2174 
   2175   got = fptr = srel = pltoff = NULL;
   2176 
   2177   relend = relocs + sec->reloc_count;
   2178 
   2179   /* We scan relocations first to create dynamic relocation arrays.  We
   2180      modified get_dyn_sym_info to allow fast insertion and support fast
   2181      lookup in the next loop.  */
   2182   for (rel = relocs; rel < relend; ++rel)
   2183     {
   2184       r_symndx = ELFNN_R_SYM (rel->r_info);
   2185       if (r_symndx >= symtab_hdr->sh_info)
   2186 	{
   2187 	  long indx = r_symndx - symtab_hdr->sh_info;
   2188 	  h = elf_sym_hashes (abfd)[indx];
   2189 	  while (h->root.type == bfd_link_hash_indirect
   2190 		 || h->root.type == bfd_link_hash_warning)
   2191 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2192 	}
   2193       else
   2194 	h = NULL;
   2195 
   2196       /* We can only get preliminary data on whether a symbol is
   2197 	 locally or externally defined, as not all of the input files
   2198 	 have yet been processed.  Do something with what we know, as
   2199 	 this may help reduce memory usage and processing time later.  */
   2200       maybe_dynamic = (h && ((!info->executable
   2201 			      && (!SYMBOLIC_BIND (info, h)
   2202 				  || info->unresolved_syms_in_shared_libs == RM_IGNORE))
   2203 			     || !h->def_regular
   2204 			     || h->root.type == bfd_link_hash_defweak));
   2205 
   2206       need_entry = 0;
   2207       switch (ELFNN_R_TYPE (rel->r_info))
   2208 	{
   2209 	case R_IA64_TPREL64MSB:
   2210 	case R_IA64_TPREL64LSB:
   2211 	  if (info->shared || maybe_dynamic)
   2212 	    need_entry = NEED_DYNREL;
   2213 	  break;
   2214 
   2215 	case R_IA64_LTOFF_TPREL22:
   2216 	  need_entry = NEED_TPREL;
   2217 	  if (info->shared)
   2218 	    info->flags |= DF_STATIC_TLS;
   2219 	  break;
   2220 
   2221 	case R_IA64_DTPREL32MSB:
   2222 	case R_IA64_DTPREL32LSB:
   2223 	case R_IA64_DTPREL64MSB:
   2224 	case R_IA64_DTPREL64LSB:
   2225 	  if (info->shared || maybe_dynamic)
   2226 	    need_entry = NEED_DYNREL;
   2227 	  break;
   2228 
   2229 	case R_IA64_LTOFF_DTPREL22:
   2230 	  need_entry = NEED_DTPREL;
   2231 	  break;
   2232 
   2233 	case R_IA64_DTPMOD64MSB:
   2234 	case R_IA64_DTPMOD64LSB:
   2235 	  if (info->shared || maybe_dynamic)
   2236 	    need_entry = NEED_DYNREL;
   2237 	  break;
   2238 
   2239 	case R_IA64_LTOFF_DTPMOD22:
   2240 	  need_entry = NEED_DTPMOD;
   2241 	  break;
   2242 
   2243 	case R_IA64_LTOFF_FPTR22:
   2244 	case R_IA64_LTOFF_FPTR64I:
   2245 	case R_IA64_LTOFF_FPTR32MSB:
   2246 	case R_IA64_LTOFF_FPTR32LSB:
   2247 	case R_IA64_LTOFF_FPTR64MSB:
   2248 	case R_IA64_LTOFF_FPTR64LSB:
   2249 	  need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
   2250 	  break;
   2251 
   2252 	case R_IA64_FPTR64I:
   2253 	case R_IA64_FPTR32MSB:
   2254 	case R_IA64_FPTR32LSB:
   2255 	case R_IA64_FPTR64MSB:
   2256 	case R_IA64_FPTR64LSB:
   2257 	  if (info->shared || h)
   2258 	    need_entry = NEED_FPTR | NEED_DYNREL;
   2259 	  else
   2260 	    need_entry = NEED_FPTR;
   2261 	  break;
   2262 
   2263 	case R_IA64_LTOFF22:
   2264 	case R_IA64_LTOFF64I:
   2265 	  need_entry = NEED_GOT;
   2266 	  break;
   2267 
   2268 	case R_IA64_LTOFF22X:
   2269 	  need_entry = NEED_GOTX;
   2270 	  break;
   2271 
   2272 	case R_IA64_PLTOFF22:
   2273 	case R_IA64_PLTOFF64I:
   2274 	case R_IA64_PLTOFF64MSB:
   2275 	case R_IA64_PLTOFF64LSB:
   2276 	  need_entry = NEED_PLTOFF;
   2277 	  if (h)
   2278 	    {
   2279 	      if (maybe_dynamic)
   2280 		need_entry |= NEED_MIN_PLT;
   2281 	    }
   2282 	  else
   2283 	    {
   2284 	      (*info->callbacks->warning)
   2285 		(info, _("@pltoff reloc against local symbol"), 0,
   2286 		 abfd, 0, (bfd_vma) 0);
   2287 	    }
   2288 	  break;
   2289 
   2290 	case R_IA64_PCREL21B:
   2291         case R_IA64_PCREL60B:
   2292 	  /* Depending on where this symbol is defined, we may or may not
   2293 	     need a full plt entry.  Only skip if we know we'll not need
   2294 	     the entry -- static or symbolic, and the symbol definition
   2295 	     has already been seen.  */
   2296 	  if (maybe_dynamic && rel->r_addend == 0)
   2297 	    need_entry = NEED_FULL_PLT;
   2298 	  break;
   2299 
   2300 	case R_IA64_IMM14:
   2301 	case R_IA64_IMM22:
   2302 	case R_IA64_IMM64:
   2303 	case R_IA64_DIR32MSB:
   2304 	case R_IA64_DIR32LSB:
   2305 	case R_IA64_DIR64MSB:
   2306 	case R_IA64_DIR64LSB:
   2307 	  /* Shared objects will always need at least a REL relocation.  */
   2308 	  if (info->shared || maybe_dynamic)
   2309 	    need_entry = NEED_DYNREL;
   2310 	  break;
   2311 
   2312 	case R_IA64_IPLTMSB:
   2313 	case R_IA64_IPLTLSB:
   2314 	  /* Shared objects will always need at least a REL relocation.  */
   2315 	  if (info->shared || maybe_dynamic)
   2316 	    need_entry = NEED_DYNREL;
   2317 	  break;
   2318 
   2319 	case R_IA64_PCREL22:
   2320 	case R_IA64_PCREL64I:
   2321 	case R_IA64_PCREL32MSB:
   2322 	case R_IA64_PCREL32LSB:
   2323 	case R_IA64_PCREL64MSB:
   2324 	case R_IA64_PCREL64LSB:
   2325 	  if (maybe_dynamic)
   2326 	    need_entry = NEED_DYNREL;
   2327 	  break;
   2328 	}
   2329 
   2330       if (!need_entry)
   2331 	continue;
   2332 
   2333       if ((need_entry & NEED_FPTR) != 0
   2334 	  && rel->r_addend)
   2335 	{
   2336 	  (*info->callbacks->warning)
   2337 	    (info, _("non-zero addend in @fptr reloc"), 0,
   2338 	     abfd, 0, (bfd_vma) 0);
   2339 	}
   2340 
   2341       if (get_dyn_sym_info (ia64_info, h, abfd, rel, TRUE) == NULL)
   2342 	return FALSE;
   2343     }
   2344 
   2345   /* Now, we only do lookup without insertion, which is very fast
   2346      with the modified get_dyn_sym_info.  */
   2347   for (rel = relocs; rel < relend; ++rel)
   2348     {
   2349       struct elfNN_ia64_dyn_sym_info *dyn_i;
   2350       int dynrel_type = R_IA64_NONE;
   2351 
   2352       r_symndx = ELFNN_R_SYM (rel->r_info);
   2353       if (r_symndx >= symtab_hdr->sh_info)
   2354 	{
   2355 	  /* We're dealing with a global symbol -- find its hash entry
   2356 	     and mark it as being referenced.  */
   2357 	  long indx = r_symndx - symtab_hdr->sh_info;
   2358 	  h = elf_sym_hashes (abfd)[indx];
   2359 	  while (h->root.type == bfd_link_hash_indirect
   2360 		 || h->root.type == bfd_link_hash_warning)
   2361 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2362 
   2363 	  /* PR15323, ref flags aren't set for references in the same
   2364 	     object.  */
   2365 	  h->root.non_ir_ref = 1;
   2366 	  h->ref_regular = 1;
   2367 	}
   2368       else
   2369 	h = NULL;
   2370 
   2371       /* We can only get preliminary data on whether a symbol is
   2372 	 locally or externally defined, as not all of the input files
   2373 	 have yet been processed.  Do something with what we know, as
   2374 	 this may help reduce memory usage and processing time later.  */
   2375       maybe_dynamic = (h && ((!info->executable
   2376 			      && (!SYMBOLIC_BIND (info, h)
   2377 				  || info->unresolved_syms_in_shared_libs == RM_IGNORE))
   2378 			     || !h->def_regular
   2379 			     || h->root.type == bfd_link_hash_defweak));
   2380 
   2381       need_entry = 0;
   2382       switch (ELFNN_R_TYPE (rel->r_info))
   2383 	{
   2384 	case R_IA64_TPREL64MSB:
   2385 	case R_IA64_TPREL64LSB:
   2386 	  if (info->shared || maybe_dynamic)
   2387 	    need_entry = NEED_DYNREL;
   2388 	  dynrel_type = R_IA64_TPREL64LSB;
   2389 	  if (info->shared)
   2390 	    info->flags |= DF_STATIC_TLS;
   2391 	  break;
   2392 
   2393 	case R_IA64_LTOFF_TPREL22:
   2394 	  need_entry = NEED_TPREL;
   2395 	  if (info->shared)
   2396 	    info->flags |= DF_STATIC_TLS;
   2397 	  break;
   2398 
   2399 	case R_IA64_DTPREL32MSB:
   2400 	case R_IA64_DTPREL32LSB:
   2401 	case R_IA64_DTPREL64MSB:
   2402 	case R_IA64_DTPREL64LSB:
   2403 	  if (info->shared || maybe_dynamic)
   2404 	    need_entry = NEED_DYNREL;
   2405 	  dynrel_type = R_IA64_DTPRELNNLSB;
   2406 	  break;
   2407 
   2408 	case R_IA64_LTOFF_DTPREL22:
   2409 	  need_entry = NEED_DTPREL;
   2410 	  break;
   2411 
   2412 	case R_IA64_DTPMOD64MSB:
   2413 	case R_IA64_DTPMOD64LSB:
   2414 	  if (info->shared || maybe_dynamic)
   2415 	    need_entry = NEED_DYNREL;
   2416 	  dynrel_type = R_IA64_DTPMOD64LSB;
   2417 	  break;
   2418 
   2419 	case R_IA64_LTOFF_DTPMOD22:
   2420 	  need_entry = NEED_DTPMOD;
   2421 	  break;
   2422 
   2423 	case R_IA64_LTOFF_FPTR22:
   2424 	case R_IA64_LTOFF_FPTR64I:
   2425 	case R_IA64_LTOFF_FPTR32MSB:
   2426 	case R_IA64_LTOFF_FPTR32LSB:
   2427 	case R_IA64_LTOFF_FPTR64MSB:
   2428 	case R_IA64_LTOFF_FPTR64LSB:
   2429 	  need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
   2430 	  break;
   2431 
   2432 	case R_IA64_FPTR64I:
   2433 	case R_IA64_FPTR32MSB:
   2434 	case R_IA64_FPTR32LSB:
   2435 	case R_IA64_FPTR64MSB:
   2436 	case R_IA64_FPTR64LSB:
   2437 	  if (info->shared || h)
   2438 	    need_entry = NEED_FPTR | NEED_DYNREL;
   2439 	  else
   2440 	    need_entry = NEED_FPTR;
   2441 	  dynrel_type = R_IA64_FPTRNNLSB;
   2442 	  break;
   2443 
   2444 	case R_IA64_LTOFF22:
   2445 	case R_IA64_LTOFF64I:
   2446 	  need_entry = NEED_GOT;
   2447 	  break;
   2448 
   2449 	case R_IA64_LTOFF22X:
   2450 	  need_entry = NEED_GOTX;
   2451 	  break;
   2452 
   2453 	case R_IA64_PLTOFF22:
   2454 	case R_IA64_PLTOFF64I:
   2455 	case R_IA64_PLTOFF64MSB:
   2456 	case R_IA64_PLTOFF64LSB:
   2457 	  need_entry = NEED_PLTOFF;
   2458 	  if (h)
   2459 	    {
   2460 	      if (maybe_dynamic)
   2461 		need_entry |= NEED_MIN_PLT;
   2462 	    }
   2463 	  break;
   2464 
   2465 	case R_IA64_PCREL21B:
   2466         case R_IA64_PCREL60B:
   2467 	  /* Depending on where this symbol is defined, we may or may not
   2468 	     need a full plt entry.  Only skip if we know we'll not need
   2469 	     the entry -- static or symbolic, and the symbol definition
   2470 	     has already been seen.  */
   2471 	  if (maybe_dynamic && rel->r_addend == 0)
   2472 	    need_entry = NEED_FULL_PLT;
   2473 	  break;
   2474 
   2475 	case R_IA64_IMM14:
   2476 	case R_IA64_IMM22:
   2477 	case R_IA64_IMM64:
   2478 	case R_IA64_DIR32MSB:
   2479 	case R_IA64_DIR32LSB:
   2480 	case R_IA64_DIR64MSB:
   2481 	case R_IA64_DIR64LSB:
   2482 	  /* Shared objects will always need at least a REL relocation.  */
   2483 	  if (info->shared || maybe_dynamic)
   2484 	    need_entry = NEED_DYNREL;
   2485 	  dynrel_type = R_IA64_DIRNNLSB;
   2486 	  break;
   2487 
   2488 	case R_IA64_IPLTMSB:
   2489 	case R_IA64_IPLTLSB:
   2490 	  /* Shared objects will always need at least a REL relocation.  */
   2491 	  if (info->shared || maybe_dynamic)
   2492 	    need_entry = NEED_DYNREL;
   2493 	  dynrel_type = R_IA64_IPLTLSB;
   2494 	  break;
   2495 
   2496 	case R_IA64_PCREL22:
   2497 	case R_IA64_PCREL64I:
   2498 	case R_IA64_PCREL32MSB:
   2499 	case R_IA64_PCREL32LSB:
   2500 	case R_IA64_PCREL64MSB:
   2501 	case R_IA64_PCREL64LSB:
   2502 	  if (maybe_dynamic)
   2503 	    need_entry = NEED_DYNREL;
   2504 	  dynrel_type = R_IA64_PCRELNNLSB;
   2505 	  break;
   2506 	}
   2507 
   2508       if (!need_entry)
   2509 	continue;
   2510 
   2511       dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, FALSE);
   2512 
   2513       /* Record whether or not this is a local symbol.  */
   2514       dyn_i->h = h;
   2515 
   2516       /* Create what's needed.  */
   2517       if (need_entry & (NEED_GOT | NEED_GOTX | NEED_TPREL
   2518 			| NEED_DTPMOD | NEED_DTPREL))
   2519 	{
   2520 	  if (!got)
   2521 	    {
   2522 	      got = get_got (abfd, info, ia64_info);
   2523 	      if (!got)
   2524 		return FALSE;
   2525 	    }
   2526 	  if (need_entry & NEED_GOT)
   2527 	    dyn_i->want_got = 1;
   2528 	  if (need_entry & NEED_GOTX)
   2529 	    dyn_i->want_gotx = 1;
   2530 	  if (need_entry & NEED_TPREL)
   2531 	    dyn_i->want_tprel = 1;
   2532 	  if (need_entry & NEED_DTPMOD)
   2533 	    dyn_i->want_dtpmod = 1;
   2534 	  if (need_entry & NEED_DTPREL)
   2535 	    dyn_i->want_dtprel = 1;
   2536 	}
   2537       if (need_entry & NEED_FPTR)
   2538 	{
   2539 	  if (!fptr)
   2540 	    {
   2541 	      fptr = get_fptr (abfd, info, ia64_info);
   2542 	      if (!fptr)
   2543 		return FALSE;
   2544 	    }
   2545 
   2546 	  /* FPTRs for shared libraries are allocated by the dynamic
   2547 	     linker.  Make sure this local symbol will appear in the
   2548 	     dynamic symbol table.  */
   2549 	  if (!h && info->shared)
   2550 	    {
   2551 	      if (! (bfd_elf_link_record_local_dynamic_symbol
   2552 		     (info, abfd, (long) r_symndx)))
   2553 		return FALSE;
   2554 	    }
   2555 
   2556 	  dyn_i->want_fptr = 1;
   2557 	}
   2558       if (need_entry & NEED_LTOFF_FPTR)
   2559 	dyn_i->want_ltoff_fptr = 1;
   2560       if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
   2561 	{
   2562           if (!ia64_info->root.dynobj)
   2563 	    ia64_info->root.dynobj = abfd;
   2564 	  h->needs_plt = 1;
   2565 	  dyn_i->want_plt = 1;
   2566 	}
   2567       if (need_entry & NEED_FULL_PLT)
   2568 	dyn_i->want_plt2 = 1;
   2569       if (need_entry & NEED_PLTOFF)
   2570 	{
   2571 	  /* This is needed here, in case @pltoff is used in a non-shared
   2572 	     link.  */
   2573 	  if (!pltoff)
   2574 	    {
   2575 	      pltoff = get_pltoff (abfd, info, ia64_info);
   2576 	      if (!pltoff)
   2577 		return FALSE;
   2578 	    }
   2579 
   2580 	  dyn_i->want_pltoff = 1;
   2581 	}
   2582       if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
   2583 	{
   2584 	  if (!srel)
   2585 	    {
   2586 	      srel = get_reloc_section (abfd, ia64_info, sec, TRUE);
   2587 	      if (!srel)
   2588 		return FALSE;
   2589 	    }
   2590 	  if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type,
   2591 				(sec->flags & SEC_READONLY) != 0))
   2592 	    return FALSE;
   2593 	}
   2594     }
   2595 
   2596   return TRUE;
   2597 }
   2598 
   2599 /* For cleanliness, and potentially faster dynamic loading, allocate
   2600    external GOT entries first.  */
   2601 
   2602 static bfd_boolean
   2603 allocate_global_data_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
   2604 			  void * data)
   2605 {
   2606   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2607 
   2608   if ((dyn_i->want_got || dyn_i->want_gotx)
   2609       && ! dyn_i->want_fptr
   2610       && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
   2611      {
   2612        dyn_i->got_offset = x->ofs;
   2613        x->ofs += 8;
   2614      }
   2615   if (dyn_i->want_tprel)
   2616     {
   2617       dyn_i->tprel_offset = x->ofs;
   2618       x->ofs += 8;
   2619     }
   2620   if (dyn_i->want_dtpmod)
   2621     {
   2622       if (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
   2623 	{
   2624 	  dyn_i->dtpmod_offset = x->ofs;
   2625 	  x->ofs += 8;
   2626 	}
   2627       else
   2628 	{
   2629 	  struct elfNN_ia64_link_hash_table *ia64_info;
   2630 
   2631 	  ia64_info = elfNN_ia64_hash_table (x->info);
   2632 	  if (ia64_info == NULL)
   2633 	    return FALSE;
   2634 
   2635 	  if (ia64_info->self_dtpmod_offset == (bfd_vma) -1)
   2636 	    {
   2637 	      ia64_info->self_dtpmod_offset = x->ofs;
   2638 	      x->ofs += 8;
   2639 	    }
   2640 	  dyn_i->dtpmod_offset = ia64_info->self_dtpmod_offset;
   2641 	}
   2642     }
   2643   if (dyn_i->want_dtprel)
   2644     {
   2645       dyn_i->dtprel_offset = x->ofs;
   2646       x->ofs += 8;
   2647     }
   2648   return TRUE;
   2649 }
   2650 
   2651 /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs.  */
   2652 
   2653 static bfd_boolean
   2654 allocate_global_fptr_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
   2655 			  void * data)
   2656 {
   2657   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2658 
   2659   if (dyn_i->want_got
   2660       && dyn_i->want_fptr
   2661       && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, R_IA64_FPTRNNLSB))
   2662     {
   2663       dyn_i->got_offset = x->ofs;
   2664       x->ofs += 8;
   2665     }
   2666   return TRUE;
   2667 }
   2668 
   2669 /* Lastly, allocate all the GOT entries for local data.  */
   2670 
   2671 static bfd_boolean
   2672 allocate_local_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
   2673 		    void * data)
   2674 {
   2675   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2676 
   2677   if ((dyn_i->want_got || dyn_i->want_gotx)
   2678       && !elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
   2679     {
   2680       dyn_i->got_offset = x->ofs;
   2681       x->ofs += 8;
   2682     }
   2683   return TRUE;
   2684 }
   2685 
   2686 /* Search for the index of a global symbol in it's defining object file.  */
   2687 
   2688 static long
   2689 global_sym_index (struct elf_link_hash_entry *h)
   2690 {
   2691   struct elf_link_hash_entry **p;
   2692   bfd *obj;
   2693 
   2694   BFD_ASSERT (h->root.type == bfd_link_hash_defined
   2695 	      || h->root.type == bfd_link_hash_defweak);
   2696 
   2697   obj = h->root.u.def.section->owner;
   2698   for (p = elf_sym_hashes (obj); *p != h; ++p)
   2699     continue;
   2700 
   2701   return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
   2702 }
   2703 
   2704 /* Allocate function descriptors.  We can do these for every function
   2705    in a main executable that is not exported.  */
   2706 
   2707 static bfd_boolean
   2708 allocate_fptr (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data)
   2709 {
   2710   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2711 
   2712   if (dyn_i->want_fptr)
   2713     {
   2714       struct elf_link_hash_entry *h = dyn_i->h;
   2715 
   2716       if (h)
   2717 	while (h->root.type == bfd_link_hash_indirect
   2718 	       || h->root.type == bfd_link_hash_warning)
   2719 	  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2720 
   2721       if (!x->info->executable
   2722 	  && (!h
   2723 	      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2724 	      || (h->root.type != bfd_link_hash_undefweak
   2725 		  && h->root.type != bfd_link_hash_undefined)))
   2726 	{
   2727 	  if (h && h->dynindx == -1)
   2728 	    {
   2729 	      BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
   2730 			  || (h->root.type == bfd_link_hash_defweak));
   2731 
   2732 	      if (!bfd_elf_link_record_local_dynamic_symbol
   2733 		    (x->info, h->root.u.def.section->owner,
   2734 		     global_sym_index (h)))
   2735 		return FALSE;
   2736 	    }
   2737 
   2738 	  dyn_i->want_fptr = 0;
   2739 	}
   2740       else if (h == NULL || h->dynindx == -1)
   2741 	{
   2742 	  dyn_i->fptr_offset = x->ofs;
   2743 	  x->ofs += 16;
   2744 	}
   2745       else
   2746 	dyn_i->want_fptr = 0;
   2747     }
   2748   return TRUE;
   2749 }
   2750 
   2751 /* Allocate all the minimal PLT entries.  */
   2752 
   2753 static bfd_boolean
   2754 allocate_plt_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
   2755 		      void * data)
   2756 {
   2757   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2758 
   2759   if (dyn_i->want_plt)
   2760     {
   2761       struct elf_link_hash_entry *h = dyn_i->h;
   2762 
   2763       if (h)
   2764 	while (h->root.type == bfd_link_hash_indirect
   2765 	       || h->root.type == bfd_link_hash_warning)
   2766 	  h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2767 
   2768       /* ??? Versioned symbols seem to lose NEEDS_PLT.  */
   2769       if (elfNN_ia64_dynamic_symbol_p (h, x->info, 0))
   2770 	{
   2771 	  bfd_size_type offset = x->ofs;
   2772 	  if (offset == 0)
   2773 	    offset = PLT_HEADER_SIZE;
   2774 	  dyn_i->plt_offset = offset;
   2775 	  x->ofs = offset + PLT_MIN_ENTRY_SIZE;
   2776 
   2777 	  dyn_i->want_pltoff = 1;
   2778 	}
   2779       else
   2780 	{
   2781 	  dyn_i->want_plt = 0;
   2782 	  dyn_i->want_plt2 = 0;
   2783 	}
   2784     }
   2785   return TRUE;
   2786 }
   2787 
   2788 /* Allocate all the full PLT entries.  */
   2789 
   2790 static bfd_boolean
   2791 allocate_plt2_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
   2792 		       void * data)
   2793 {
   2794   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2795 
   2796   if (dyn_i->want_plt2)
   2797     {
   2798       struct elf_link_hash_entry *h = dyn_i->h;
   2799       bfd_size_type ofs = x->ofs;
   2800 
   2801       dyn_i->plt2_offset = ofs;
   2802       x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
   2803 
   2804       while (h->root.type == bfd_link_hash_indirect
   2805 	     || h->root.type == bfd_link_hash_warning)
   2806 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2807       dyn_i->h->plt.offset = ofs;
   2808     }
   2809   return TRUE;
   2810 }
   2811 
   2812 /* Allocate all the PLTOFF entries requested by relocations and
   2813    plt entries.  We can't share space with allocated FPTR entries,
   2814    because the latter are not necessarily addressable by the GP.
   2815    ??? Relaxation might be able to determine that they are.  */
   2816 
   2817 static bfd_boolean
   2818 allocate_pltoff_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
   2819 			 void * data)
   2820 {
   2821   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2822 
   2823   if (dyn_i->want_pltoff)
   2824     {
   2825       dyn_i->pltoff_offset = x->ofs;
   2826       x->ofs += 16;
   2827     }
   2828   return TRUE;
   2829 }
   2830 
   2831 /* Allocate dynamic relocations for those symbols that turned out
   2832    to be dynamic.  */
   2833 
   2834 static bfd_boolean
   2835 allocate_dynrel_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
   2836 			 void * data)
   2837 {
   2838   struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
   2839   struct elfNN_ia64_link_hash_table *ia64_info;
   2840   struct elfNN_ia64_dyn_reloc_entry *rent;
   2841   bfd_boolean dynamic_symbol, shared, resolved_zero;
   2842 
   2843   ia64_info = elfNN_ia64_hash_table (x->info);
   2844   if (ia64_info == NULL)
   2845     return FALSE;
   2846 
   2847   /* Note that this can't be used in relation to FPTR relocs below.  */
   2848   dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0);
   2849 
   2850   shared = x->info->shared;
   2851   resolved_zero = (dyn_i->h
   2852 		   && ELF_ST_VISIBILITY (dyn_i->h->other)
   2853 		   && dyn_i->h->root.type == bfd_link_hash_undefweak);
   2854 
   2855   /* Take care of the GOT and PLT relocations.  */
   2856 
   2857   if ((!resolved_zero
   2858        && (dynamic_symbol || shared)
   2859        && (dyn_i->want_got || dyn_i->want_gotx))
   2860       || (dyn_i->want_ltoff_fptr
   2861 	  && dyn_i->h
   2862 	  && dyn_i->h->dynindx != -1))
   2863     {
   2864       if (!dyn_i->want_ltoff_fptr
   2865 	  || !x->info->pie
   2866 	  || dyn_i->h == NULL
   2867 	  || dyn_i->h->root.type != bfd_link_hash_undefweak)
   2868 	ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
   2869     }
   2870   if ((dynamic_symbol || shared) && dyn_i->want_tprel)
   2871     ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
   2872   if (dynamic_symbol && dyn_i->want_dtpmod)
   2873     ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
   2874   if (dynamic_symbol && dyn_i->want_dtprel)
   2875     ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
   2876 
   2877   if (x->only_got)
   2878     return TRUE;
   2879 
   2880   if (ia64_info->rel_fptr_sec && dyn_i->want_fptr)
   2881     {
   2882       if (dyn_i->h == NULL || dyn_i->h->root.type != bfd_link_hash_undefweak)
   2883 	ia64_info->rel_fptr_sec->size += sizeof (ElfNN_External_Rela);
   2884     }
   2885 
   2886   if (!resolved_zero && dyn_i->want_pltoff)
   2887     {
   2888       bfd_size_type t = 0;
   2889 
   2890       /* Dynamic symbols get one IPLT relocation.  Local symbols in
   2891 	 shared libraries get two REL relocations.  Local symbols in
   2892 	 main applications get nothing.  */
   2893       if (dynamic_symbol)
   2894 	t = sizeof (ElfNN_External_Rela);
   2895       else if (shared)
   2896 	t = 2 * sizeof (ElfNN_External_Rela);
   2897 
   2898       ia64_info->rel_pltoff_sec->size += t;
   2899     }
   2900 
   2901   /* Take care of the normal data relocations.  */
   2902 
   2903   for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
   2904     {
   2905       int count = rent->count;
   2906 
   2907       switch (rent->type)
   2908 	{
   2909 	case R_IA64_FPTR32LSB:
   2910 	case R_IA64_FPTR64LSB:
   2911 	  /* Allocate one iff !want_fptr and not PIE, which by this point
   2912 	     will be true only if we're actually allocating one statically
   2913 	     in the main executable.  Position independent executables
   2914 	     need a relative reloc.  */
   2915 	  if (dyn_i->want_fptr && !x->info->pie)
   2916 	    continue;
   2917 	  break;
   2918 	case R_IA64_PCREL32LSB:
   2919 	case R_IA64_PCREL64LSB:
   2920 	  if (!dynamic_symbol)
   2921 	    continue;
   2922 	  break;
   2923 	case R_IA64_DIR32LSB:
   2924 	case R_IA64_DIR64LSB:
   2925 	  if (!dynamic_symbol && !shared)
   2926 	    continue;
   2927 	  break;
   2928 	case R_IA64_IPLTLSB:
   2929 	  if (!dynamic_symbol && !shared)
   2930 	    continue;
   2931 	  /* Use two REL relocations for IPLT relocations
   2932 	     against local symbols.  */
   2933 	  if (!dynamic_symbol)
   2934 	    count *= 2;
   2935 	  break;
   2936 	case R_IA64_DTPREL32LSB:
   2937 	case R_IA64_TPREL64LSB:
   2938 	case R_IA64_DTPREL64LSB:
   2939 	case R_IA64_DTPMOD64LSB:
   2940 	  break;
   2941 	default:
   2942 	  abort ();
   2943 	}
   2944       if (rent->reltext)
   2945 	ia64_info->reltext = 1;
   2946       rent->srel->size += sizeof (ElfNN_External_Rela) * count;
   2947     }
   2948 
   2949   return TRUE;
   2950 }
   2951 
   2952 static bfd_boolean
   2953 elfNN_ia64_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2954 				  struct elf_link_hash_entry *h)
   2955 {
   2956   /* ??? Undefined symbols with PLT entries should be re-defined
   2957      to be the PLT entry.  */
   2958 
   2959   /* If this is a weak symbol, and there is a real definition, the
   2960      processor independent code will have arranged for us to see the
   2961      real definition first, and we can just use the same value.  */
   2962   if (h->u.weakdef != NULL)
   2963     {
   2964       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   2965                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
   2966       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   2967       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   2968       return TRUE;
   2969     }
   2970 
   2971   /* If this is a reference to a symbol defined by a dynamic object which
   2972      is not a function, we might allocate the symbol in our .dynbss section
   2973      and allocate a COPY dynamic relocation.
   2974 
   2975      But IA-64 code is canonically PIC, so as a rule we can avoid this sort
   2976      of hackery.  */
   2977 
   2978   return TRUE;
   2979 }
   2980 
   2981 static bfd_boolean
   2982 elfNN_ia64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   2983 				  struct bfd_link_info *info)
   2984 {
   2985   struct elfNN_ia64_allocate_data data;
   2986   struct elfNN_ia64_link_hash_table *ia64_info;
   2987   asection *sec;
   2988   bfd *dynobj;
   2989   bfd_boolean relplt = FALSE;
   2990 
   2991   dynobj = elf_hash_table(info)->dynobj;
   2992   ia64_info = elfNN_ia64_hash_table (info);
   2993   if (ia64_info == NULL)
   2994     return FALSE;
   2995   ia64_info->self_dtpmod_offset = (bfd_vma) -1;
   2996   BFD_ASSERT(dynobj != NULL);
   2997   data.info = info;
   2998 
   2999   /* Set the contents of the .interp section to the interpreter.  */
   3000   if (ia64_info->root.dynamic_sections_created
   3001       && info->executable)
   3002     {
   3003       sec = bfd_get_linker_section (dynobj, ".interp");
   3004       BFD_ASSERT (sec != NULL);
   3005       sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
   3006       sec->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
   3007     }
   3008 
   3009   /* Allocate the GOT entries.  */
   3010 
   3011   if (ia64_info->root.sgot)
   3012     {
   3013       data.ofs = 0;
   3014       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
   3015       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
   3016       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
   3017       ia64_info->root.sgot->size = data.ofs;
   3018     }
   3019 
   3020   /* Allocate the FPTR entries.  */
   3021 
   3022   if (ia64_info->fptr_sec)
   3023     {
   3024       data.ofs = 0;
   3025       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
   3026       ia64_info->fptr_sec->size = data.ofs;
   3027     }
   3028 
   3029   /* Now that we've seen all of the input files, we can decide which
   3030      symbols need plt entries.  Allocate the minimal PLT entries first.
   3031      We do this even though dynamic_sections_created may be FALSE, because
   3032      this has the side-effect of clearing want_plt and want_plt2.  */
   3033 
   3034   data.ofs = 0;
   3035   elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
   3036 
   3037   ia64_info->minplt_entries = 0;
   3038   if (data.ofs)
   3039     {
   3040       ia64_info->minplt_entries
   3041 	= (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
   3042     }
   3043 
   3044   /* Align the pointer for the plt2 entries.  */
   3045   data.ofs = (data.ofs + 31) & (bfd_vma) -32;
   3046 
   3047   elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
   3048   if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
   3049     {
   3050       /* FIXME: we always reserve the memory for dynamic linker even if
   3051 	 there are no PLT entries since dynamic linker may assume the
   3052 	 reserved memory always exists.  */
   3053 
   3054       BFD_ASSERT (ia64_info->root.dynamic_sections_created);
   3055 
   3056       ia64_info->root.splt->size = data.ofs;
   3057 
   3058       /* If we've got a .plt, we need some extra memory for the dynamic
   3059 	 linker.  We stuff these in .got.plt.  */
   3060       sec = bfd_get_linker_section (dynobj, ".got.plt");
   3061       sec->size = 8 * PLT_RESERVED_WORDS;
   3062     }
   3063 
   3064   /* Allocate the PLTOFF entries.  */
   3065 
   3066   if (ia64_info->pltoff_sec)
   3067     {
   3068       data.ofs = 0;
   3069       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
   3070       ia64_info->pltoff_sec->size = data.ofs;
   3071     }
   3072 
   3073   if (ia64_info->root.dynamic_sections_created)
   3074     {
   3075       /* Allocate space for the dynamic relocations that turned out to be
   3076 	 required.  */
   3077 
   3078       if (info->shared && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
   3079 	ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
   3080       data.only_got = FALSE;
   3081       elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
   3082     }
   3083 
   3084   /* We have now determined the sizes of the various dynamic sections.
   3085      Allocate memory for them.  */
   3086   for (sec = dynobj->sections; sec != NULL; sec = sec->next)
   3087     {
   3088       bfd_boolean strip;
   3089 
   3090       if (!(sec->flags & SEC_LINKER_CREATED))
   3091 	continue;
   3092 
   3093       /* If we don't need this section, strip it from the output file.
   3094 	 There were several sections primarily related to dynamic
   3095 	 linking that must be create before the linker maps input
   3096 	 sections to output sections.  The linker does that before
   3097 	 bfd_elf_size_dynamic_sections is called, and it is that
   3098 	 function which decides whether anything needs to go into
   3099 	 these sections.  */
   3100 
   3101       strip = (sec->size == 0);
   3102 
   3103       if (sec == ia64_info->root.sgot)
   3104 	strip = FALSE;
   3105       else if (sec == ia64_info->root.srelgot)
   3106 	{
   3107 	  if (strip)
   3108 	    ia64_info->root.srelgot = NULL;
   3109 	  else
   3110 	    /* We use the reloc_count field as a counter if we need to
   3111 	       copy relocs into the output file.  */
   3112 	    sec->reloc_count = 0;
   3113 	}
   3114       else if (sec == ia64_info->fptr_sec)
   3115 	{
   3116 	  if (strip)
   3117 	    ia64_info->fptr_sec = NULL;
   3118 	}
   3119       else if (sec == ia64_info->rel_fptr_sec)
   3120 	{
   3121 	  if (strip)
   3122 	    ia64_info->rel_fptr_sec = NULL;
   3123 	  else
   3124 	    /* We use the reloc_count field as a counter if we need to
   3125 	       copy relocs into the output file.  */
   3126 	    sec->reloc_count = 0;
   3127 	}
   3128       else if (sec == ia64_info->root.splt)
   3129 	{
   3130 	  if (strip)
   3131 	    ia64_info->root.splt = NULL;
   3132 	}
   3133       else if (sec == ia64_info->pltoff_sec)
   3134 	{
   3135 	  if (strip)
   3136 	    ia64_info->pltoff_sec = NULL;
   3137 	}
   3138       else if (sec == ia64_info->rel_pltoff_sec)
   3139 	{
   3140 	  if (strip)
   3141 	    ia64_info->rel_pltoff_sec = NULL;
   3142 	  else
   3143 	    {
   3144 	      relplt = TRUE;
   3145 	      /* We use the reloc_count field as a counter if we need to
   3146 		 copy relocs into the output file.  */
   3147 	      sec->reloc_count = 0;
   3148 	    }
   3149 	}
   3150       else
   3151 	{
   3152 	  const char *name;
   3153 
   3154 	  /* It's OK to base decisions on the section name, because none
   3155 	     of the dynobj section names depend upon the input files.  */
   3156 	  name = bfd_get_section_name (dynobj, sec);
   3157 
   3158 	  if (strcmp (name, ".got.plt") == 0)
   3159 	    strip = FALSE;
   3160 	  else if (CONST_STRNEQ (name, ".rel"))
   3161 	    {
   3162 	      if (!strip)
   3163 		{
   3164 		  /* We use the reloc_count field as a counter if we need to
   3165 		     copy relocs into the output file.  */
   3166 		  sec->reloc_count = 0;
   3167 		}
   3168 	    }
   3169 	  else
   3170 	    continue;
   3171 	}
   3172 
   3173       if (strip)
   3174 	sec->flags |= SEC_EXCLUDE;
   3175       else
   3176 	{
   3177 	  /* Allocate memory for the section contents.  */
   3178 	  sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
   3179 	  if (sec->contents == NULL && sec->size != 0)
   3180 	    return FALSE;
   3181 	}
   3182     }
   3183 
   3184   if (elf_hash_table (info)->dynamic_sections_created)
   3185     {
   3186       /* Add some entries to the .dynamic section.  We fill in the values
   3187 	 later (in finish_dynamic_sections) but we must add the entries now
   3188 	 so that we get the correct size for the .dynamic section.  */
   3189 
   3190       if (info->executable)
   3191 	{
   3192 	  /* The DT_DEBUG entry is filled in by the dynamic linker and used
   3193 	     by the debugger.  */
   3194 #define add_dynamic_entry(TAG, VAL) \
   3195   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   3196 
   3197 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   3198 	    return FALSE;
   3199 	}
   3200 
   3201       if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
   3202 	return FALSE;
   3203       if (!add_dynamic_entry (DT_PLTGOT, 0))
   3204 	return FALSE;
   3205 
   3206       if (relplt)
   3207 	{
   3208 	  if (!add_dynamic_entry (DT_PLTRELSZ, 0)
   3209 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   3210 	      || !add_dynamic_entry (DT_JMPREL, 0))
   3211 	    return FALSE;
   3212 	}
   3213 
   3214       if (!add_dynamic_entry (DT_RELA, 0)
   3215 	  || !add_dynamic_entry (DT_RELASZ, 0)
   3216 	  || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
   3217 	return FALSE;
   3218 
   3219       if (ia64_info->reltext)
   3220 	{
   3221 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
   3222 	    return FALSE;
   3223 	  info->flags |= DF_TEXTREL;
   3224 	}
   3225     }
   3226 
   3227   /* ??? Perhaps force __gp local.  */
   3228 
   3229   return TRUE;
   3230 }
   3231 
   3232 static void
   3233 elfNN_ia64_install_dyn_reloc (bfd *abfd, struct bfd_link_info *info,
   3234 			      asection *sec, asection *srel,
   3235 			      bfd_vma offset, unsigned int type,
   3236 			      long dynindx, bfd_vma addend)
   3237 {
   3238   Elf_Internal_Rela outrel;
   3239   bfd_byte *loc;
   3240 
   3241   BFD_ASSERT (dynindx != -1);
   3242   outrel.r_info = ELFNN_R_INFO (dynindx, type);
   3243   outrel.r_addend = addend;
   3244   outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
   3245   if (outrel.r_offset >= (bfd_vma) -2)
   3246     {
   3247       /* Run for the hills.  We shouldn't be outputting a relocation
   3248 	 for this.  So do what everyone else does and output a no-op.  */
   3249       outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
   3250       outrel.r_addend = 0;
   3251       outrel.r_offset = 0;
   3252     }
   3253   else
   3254     outrel.r_offset += sec->output_section->vma + sec->output_offset;
   3255 
   3256   loc = srel->contents;
   3257   loc += srel->reloc_count++ * sizeof (ElfNN_External_Rela);
   3258   bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
   3259   BFD_ASSERT (sizeof (ElfNN_External_Rela) * srel->reloc_count <= srel->size);
   3260 }
   3261 
   3262 /* Store an entry for target address TARGET_ADDR in the linkage table
   3263    and return the gp-relative address of the linkage table entry.  */
   3264 
   3265 static bfd_vma
   3266 set_got_entry (bfd *abfd, struct bfd_link_info *info,
   3267 	       struct elfNN_ia64_dyn_sym_info *dyn_i,
   3268 	       long dynindx, bfd_vma addend, bfd_vma value,
   3269 	       unsigned int dyn_r_type)
   3270 {
   3271   struct elfNN_ia64_link_hash_table *ia64_info;
   3272   asection *got_sec;
   3273   bfd_boolean done;
   3274   bfd_vma got_offset;
   3275 
   3276   ia64_info = elfNN_ia64_hash_table (info);
   3277   if (ia64_info == NULL)
   3278     return 0;
   3279 
   3280   got_sec = ia64_info->root.sgot;
   3281 
   3282   switch (dyn_r_type)
   3283     {
   3284     case R_IA64_TPREL64LSB:
   3285       done = dyn_i->tprel_done;
   3286       dyn_i->tprel_done = TRUE;
   3287       got_offset = dyn_i->tprel_offset;
   3288       break;
   3289     case R_IA64_DTPMOD64LSB:
   3290       if (dyn_i->dtpmod_offset != ia64_info->self_dtpmod_offset)
   3291 	{
   3292 	  done = dyn_i->dtpmod_done;
   3293 	  dyn_i->dtpmod_done = TRUE;
   3294 	}
   3295       else
   3296 	{
   3297 	  done = ia64_info->self_dtpmod_done;
   3298 	  ia64_info->self_dtpmod_done = TRUE;
   3299 	  dynindx = 0;
   3300 	}
   3301       got_offset = dyn_i->dtpmod_offset;
   3302       break;
   3303     case R_IA64_DTPREL32LSB:
   3304     case R_IA64_DTPREL64LSB:
   3305       done = dyn_i->dtprel_done;
   3306       dyn_i->dtprel_done = TRUE;
   3307       got_offset = dyn_i->dtprel_offset;
   3308       break;
   3309     default:
   3310       done = dyn_i->got_done;
   3311       dyn_i->got_done = TRUE;
   3312       got_offset = dyn_i->got_offset;
   3313       break;
   3314     }
   3315 
   3316   BFD_ASSERT ((got_offset & 7) == 0);
   3317 
   3318   if (! done)
   3319     {
   3320       /* Store the target address in the linkage table entry.  */
   3321       bfd_put_64 (abfd, value, got_sec->contents + got_offset);
   3322 
   3323       /* Install a dynamic relocation if needed.  */
   3324       if (((info->shared
   3325 	    && (!dyn_i->h
   3326 		|| ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
   3327 		|| dyn_i->h->root.type != bfd_link_hash_undefweak)
   3328 	    && dyn_r_type != R_IA64_DTPREL32LSB
   3329 	    && dyn_r_type != R_IA64_DTPREL64LSB)
   3330            || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info, dyn_r_type)
   3331 	   || (dynindx != -1
   3332 	       && (dyn_r_type == R_IA64_FPTR32LSB
   3333 		   || dyn_r_type == R_IA64_FPTR64LSB)))
   3334 	  && (!dyn_i->want_ltoff_fptr
   3335 	      || !info->pie
   3336 	      || !dyn_i->h
   3337 	      || dyn_i->h->root.type != bfd_link_hash_undefweak))
   3338 	{
   3339 	  if (dynindx == -1
   3340 	      && dyn_r_type != R_IA64_TPREL64LSB
   3341 	      && dyn_r_type != R_IA64_DTPMOD64LSB
   3342 	      && dyn_r_type != R_IA64_DTPREL32LSB
   3343 	      && dyn_r_type != R_IA64_DTPREL64LSB)
   3344 	    {
   3345 	      dyn_r_type = R_IA64_RELNNLSB;
   3346 	      dynindx = 0;
   3347 	      addend = value;
   3348 	    }
   3349 
   3350 	  if (bfd_big_endian (abfd))
   3351 	    {
   3352 	      switch (dyn_r_type)
   3353 		{
   3354 		case R_IA64_REL32LSB:
   3355 		  dyn_r_type = R_IA64_REL32MSB;
   3356 		  break;
   3357 		case R_IA64_DIR32LSB:
   3358 		  dyn_r_type = R_IA64_DIR32MSB;
   3359 		  break;
   3360 		case R_IA64_FPTR32LSB:
   3361 		  dyn_r_type = R_IA64_FPTR32MSB;
   3362 		  break;
   3363 		case R_IA64_DTPREL32LSB:
   3364 		  dyn_r_type = R_IA64_DTPREL32MSB;
   3365 		  break;
   3366 		case R_IA64_REL64LSB:
   3367 		  dyn_r_type = R_IA64_REL64MSB;
   3368 		  break;
   3369 		case R_IA64_DIR64LSB:
   3370 		  dyn_r_type = R_IA64_DIR64MSB;
   3371 		  break;
   3372 		case R_IA64_FPTR64LSB:
   3373 		  dyn_r_type = R_IA64_FPTR64MSB;
   3374 		  break;
   3375 		case R_IA64_TPREL64LSB:
   3376 		  dyn_r_type = R_IA64_TPREL64MSB;
   3377 		  break;
   3378 		case R_IA64_DTPMOD64LSB:
   3379 		  dyn_r_type = R_IA64_DTPMOD64MSB;
   3380 		  break;
   3381 		case R_IA64_DTPREL64LSB:
   3382 		  dyn_r_type = R_IA64_DTPREL64MSB;
   3383 		  break;
   3384 		default:
   3385 		  BFD_ASSERT (FALSE);
   3386 		  break;
   3387 		}
   3388 	    }
   3389 
   3390 	  elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
   3391 					ia64_info->root.srelgot,
   3392 					got_offset, dyn_r_type,
   3393 					dynindx, addend);
   3394 	}
   3395     }
   3396 
   3397   /* Return the address of the linkage table entry.  */
   3398   value = (got_sec->output_section->vma
   3399 	   + got_sec->output_offset
   3400 	   + got_offset);
   3401 
   3402   return value;
   3403 }
   3404 
   3405 /* Fill in a function descriptor consisting of the function's code
   3406    address and its global pointer.  Return the descriptor's address.  */
   3407 
   3408 static bfd_vma
   3409 set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
   3410 		struct elfNN_ia64_dyn_sym_info *dyn_i,
   3411 		bfd_vma value)
   3412 {
   3413   struct elfNN_ia64_link_hash_table *ia64_info;
   3414   asection *fptr_sec;
   3415 
   3416   ia64_info = elfNN_ia64_hash_table (info);
   3417   if (ia64_info == NULL)
   3418     return 0;
   3419 
   3420   fptr_sec = ia64_info->fptr_sec;
   3421 
   3422   if (!dyn_i->fptr_done)
   3423     {
   3424       dyn_i->fptr_done = 1;
   3425 
   3426       /* Fill in the function descriptor.  */
   3427       bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
   3428       bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
   3429 		  fptr_sec->contents + dyn_i->fptr_offset + 8);
   3430       if (ia64_info->rel_fptr_sec)
   3431 	{
   3432 	  Elf_Internal_Rela outrel;
   3433 	  bfd_byte *loc;
   3434 
   3435 	  if (bfd_little_endian (abfd))
   3436 	    outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTLSB);
   3437 	  else
   3438 	    outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTMSB);
   3439 	  outrel.r_addend = value;
   3440 	  outrel.r_offset = (fptr_sec->output_section->vma
   3441 			     + fptr_sec->output_offset
   3442 			     + dyn_i->fptr_offset);
   3443 	  loc = ia64_info->rel_fptr_sec->contents;
   3444 	  loc += ia64_info->rel_fptr_sec->reloc_count++
   3445 		 * sizeof (ElfNN_External_Rela);
   3446 	  bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
   3447 	}
   3448     }
   3449 
   3450   /* Return the descriptor's address.  */
   3451   value = (fptr_sec->output_section->vma
   3452 	   + fptr_sec->output_offset
   3453 	   + dyn_i->fptr_offset);
   3454 
   3455   return value;
   3456 }
   3457 
   3458 /* Fill in a PLTOFF entry consisting of the function's code address
   3459    and its global pointer.  Return the descriptor's address.  */
   3460 
   3461 static bfd_vma
   3462 set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
   3463 		  struct elfNN_ia64_dyn_sym_info *dyn_i,
   3464 		  bfd_vma value, bfd_boolean is_plt)
   3465 {
   3466   struct elfNN_ia64_link_hash_table *ia64_info;
   3467   asection *pltoff_sec;
   3468 
   3469   ia64_info = elfNN_ia64_hash_table (info);
   3470   if (ia64_info == NULL)
   3471     return 0;
   3472 
   3473   pltoff_sec = ia64_info->pltoff_sec;
   3474 
   3475   /* Don't do anything if this symbol uses a real PLT entry.  In
   3476      that case, we'll fill this in during finish_dynamic_symbol.  */
   3477   if ((! dyn_i->want_plt || is_plt)
   3478       && !dyn_i->pltoff_done)
   3479     {
   3480       bfd_vma gp = _bfd_get_gp_value (abfd);
   3481 
   3482       /* Fill in the function descriptor.  */
   3483       bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
   3484       bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
   3485 
   3486       /* Install dynamic relocations if needed.  */
   3487       if (!is_plt
   3488 	  && info->shared
   3489 	  && (!dyn_i->h
   3490 	      || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
   3491 	      || dyn_i->h->root.type != bfd_link_hash_undefweak))
   3492 	{
   3493 	  unsigned int dyn_r_type;
   3494 
   3495 	  if (bfd_big_endian (abfd))
   3496 	    dyn_r_type = R_IA64_RELNNMSB;
   3497 	  else
   3498 	    dyn_r_type = R_IA64_RELNNLSB;
   3499 
   3500 	  elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
   3501 					ia64_info->rel_pltoff_sec,
   3502 					dyn_i->pltoff_offset,
   3503 					dyn_r_type, 0, value);
   3504 	  elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
   3505 					ia64_info->rel_pltoff_sec,
   3506 					dyn_i->pltoff_offset + ARCH_SIZE / 8,
   3507 					dyn_r_type, 0, gp);
   3508 	}
   3509 
   3510       dyn_i->pltoff_done = 1;
   3511     }
   3512 
   3513   /* Return the descriptor's address.  */
   3514   value = (pltoff_sec->output_section->vma
   3515 	   + pltoff_sec->output_offset
   3516 	   + dyn_i->pltoff_offset);
   3517 
   3518   return value;
   3519 }
   3520 
   3521 /* Return the base VMA address which should be subtracted from real addresses
   3522    when resolving @tprel() relocation.
   3523    Main program TLS (whose template starts at PT_TLS p_vaddr)
   3524    is assigned offset round(2 * size of pointer, PT_TLS p_align).  */
   3525 
   3526 static bfd_vma
   3527 elfNN_ia64_tprel_base (struct bfd_link_info *info)
   3528 {
   3529   asection *tls_sec = elf_hash_table (info)->tls_sec;
   3530   return tls_sec->vma - align_power ((bfd_vma) ARCH_SIZE / 4,
   3531 				     tls_sec->alignment_power);
   3532 }
   3533 
   3534 /* Return the base VMA address which should be subtracted from real addresses
   3535    when resolving @dtprel() relocation.
   3536    This is PT_TLS segment p_vaddr.  */
   3537 
   3538 static bfd_vma
   3539 elfNN_ia64_dtprel_base (struct bfd_link_info *info)
   3540 {
   3541   return elf_hash_table (info)->tls_sec->vma;
   3542 }
   3543 
   3544 /* Called through qsort to sort the .IA_64.unwind section during a
   3545    non-relocatable link.  Set elfNN_ia64_unwind_entry_compare_bfd
   3546    to the output bfd so we can do proper endianness frobbing.  */
   3547 
   3548 static bfd *elfNN_ia64_unwind_entry_compare_bfd;
   3549 
   3550 static int
   3551 elfNN_ia64_unwind_entry_compare (const void * a, const void * b)
   3552 {
   3553   bfd_vma av, bv;
   3554 
   3555   av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
   3556   bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
   3557 
   3558   return (av < bv ? -1 : av > bv ? 1 : 0);
   3559 }
   3560 
   3561 /* Make sure we've got ourselves a nice fat __gp value.  */
   3562 static bfd_boolean
   3563 elfNN_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info, bfd_boolean final)
   3564 {
   3565   bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
   3566   bfd_vma min_short_vma = min_vma, max_short_vma = 0;
   3567   struct elf_link_hash_entry *gp;
   3568   bfd_vma gp_val;
   3569   asection *os;
   3570   struct elfNN_ia64_link_hash_table *ia64_info;
   3571 
   3572   ia64_info = elfNN_ia64_hash_table (info);
   3573   if (ia64_info == NULL)
   3574     return FALSE;
   3575 
   3576   /* Find the min and max vma of all sections marked short.  Also collect
   3577      min and max vma of any type, for use in selecting a nice gp.  */
   3578   for (os = abfd->sections; os ; os = os->next)
   3579     {
   3580       bfd_vma lo, hi;
   3581 
   3582       if ((os->flags & SEC_ALLOC) == 0)
   3583 	continue;
   3584 
   3585       lo = os->vma;
   3586       /* When this function is called from elfNN_ia64_final_link
   3587 	 the correct value to use is os->size.  When called from
   3588 	 elfNN_ia64_relax_section we are in the middle of section
   3589 	 sizing; some sections will already have os->size set, others
   3590 	 will have os->size zero and os->rawsize the previous size.  */
   3591       hi = os->vma + (!final && os->rawsize ? os->rawsize : os->size);
   3592       if (hi < lo)
   3593 	hi = (bfd_vma) -1;
   3594 
   3595       if (min_vma > lo)
   3596 	min_vma = lo;
   3597       if (max_vma < hi)
   3598 	max_vma = hi;
   3599       if (os->flags & SEC_SMALL_DATA)
   3600 	{
   3601 	  if (min_short_vma > lo)
   3602 	    min_short_vma = lo;
   3603 	  if (max_short_vma < hi)
   3604 	    max_short_vma = hi;
   3605 	}
   3606     }
   3607 
   3608   if (ia64_info->min_short_sec)
   3609     {
   3610       if (min_short_vma
   3611 	  > (ia64_info->min_short_sec->vma
   3612 	     + ia64_info->min_short_offset))
   3613 	min_short_vma = (ia64_info->min_short_sec->vma
   3614 			 + ia64_info->min_short_offset);
   3615       if (max_short_vma
   3616 	  < (ia64_info->max_short_sec->vma
   3617 	     + ia64_info->max_short_offset))
   3618 	max_short_vma = (ia64_info->max_short_sec->vma
   3619 			 + ia64_info->max_short_offset);
   3620     }
   3621 
   3622   /* See if the user wants to force a value.  */
   3623   gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
   3624 			     FALSE, FALSE);
   3625 
   3626   if (gp
   3627       && (gp->root.type == bfd_link_hash_defined
   3628 	  || gp->root.type == bfd_link_hash_defweak))
   3629     {
   3630       asection *gp_sec = gp->root.u.def.section;
   3631       gp_val = (gp->root.u.def.value
   3632 		+ gp_sec->output_section->vma
   3633 		+ gp_sec->output_offset);
   3634     }
   3635   else
   3636     {
   3637       /* Pick a sensible value.  */
   3638 
   3639       if (ia64_info->min_short_sec)
   3640 	{
   3641 	  bfd_vma short_range = max_short_vma - min_short_vma;
   3642 
   3643 	  /* If min_short_sec is set, pick one in the middle bewteen
   3644 	     min_short_vma and max_short_vma.  */
   3645 	  if (short_range >= 0x400000)
   3646 	    goto overflow;
   3647 	  gp_val = min_short_vma + short_range / 2;
   3648 	}
   3649       else
   3650 	{
   3651 	  asection *got_sec = ia64_info->root.sgot;
   3652 
   3653 	  /* Start with just the address of the .got.  */
   3654 	  if (got_sec)
   3655 	    gp_val = got_sec->output_section->vma;
   3656 	  else if (max_short_vma != 0)
   3657 	    gp_val = min_short_vma;
   3658 	  else if (max_vma - min_vma < 0x200000)
   3659 	    gp_val = min_vma;
   3660 	  else
   3661 	    gp_val = max_vma - 0x200000 + 8;
   3662 	}
   3663 
   3664       /* If it is possible to address the entire image, but we
   3665 	 don't with the choice above, adjust.  */
   3666       if (max_vma - min_vma < 0x400000
   3667 	  && (max_vma - gp_val >= 0x200000
   3668 	      || gp_val - min_vma > 0x200000))
   3669 	gp_val = min_vma + 0x200000;
   3670       else if (max_short_vma != 0)
   3671 	{
   3672 	  /* If we don't cover all the short data, adjust.  */
   3673 	  if (max_short_vma - gp_val >= 0x200000)
   3674 	    gp_val = min_short_vma + 0x200000;
   3675 
   3676 	  /* If we're addressing stuff past the end, adjust back.  */
   3677 	  if (gp_val > max_vma)
   3678 	    gp_val = max_vma - 0x200000 + 8;
   3679 	}
   3680     }
   3681 
   3682   /* Validate whether all SHF_IA_64_SHORT sections are within
   3683      range of the chosen GP.  */
   3684 
   3685   if (max_short_vma != 0)
   3686     {
   3687       if (max_short_vma - min_short_vma >= 0x400000)
   3688 	{
   3689 overflow:
   3690 	  (*_bfd_error_handler)
   3691 	    (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
   3692 	     bfd_get_filename (abfd),
   3693 	     (unsigned long) (max_short_vma - min_short_vma));
   3694 	  return FALSE;
   3695 	}
   3696       else if ((gp_val > min_short_vma
   3697 		&& gp_val - min_short_vma > 0x200000)
   3698 	       || (gp_val < max_short_vma
   3699 		   && max_short_vma - gp_val >= 0x200000))
   3700 	{
   3701 	  (*_bfd_error_handler)
   3702 	    (_("%s: __gp does not cover short data segment"),
   3703 	     bfd_get_filename (abfd));
   3704 	  return FALSE;
   3705 	}
   3706     }
   3707 
   3708   _bfd_set_gp_value (abfd, gp_val);
   3709 
   3710   return TRUE;
   3711 }
   3712 
   3713 static bfd_boolean
   3714 elfNN_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
   3715 {
   3716   struct elfNN_ia64_link_hash_table *ia64_info;
   3717   asection *unwind_output_sec;
   3718 
   3719   ia64_info = elfNN_ia64_hash_table (info);
   3720   if (ia64_info == NULL)
   3721     return FALSE;
   3722 
   3723   /* Make sure we've got ourselves a nice fat __gp value.  */
   3724   if (!info->relocatable)
   3725     {
   3726       bfd_vma gp_val;
   3727       struct elf_link_hash_entry *gp;
   3728 
   3729       /* We assume after gp is set, section size will only decrease. We
   3730 	 need to adjust gp for it.  */
   3731       _bfd_set_gp_value (abfd, 0);
   3732       if (! elfNN_ia64_choose_gp (abfd, info, TRUE))
   3733 	return FALSE;
   3734       gp_val = _bfd_get_gp_value (abfd);
   3735 
   3736       gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
   3737 			         FALSE, FALSE);
   3738       if (gp)
   3739 	{
   3740 	  gp->root.type = bfd_link_hash_defined;
   3741 	  gp->root.u.def.value = gp_val;
   3742 	  gp->root.u.def.section = bfd_abs_section_ptr;
   3743 	}
   3744     }
   3745 
   3746   /* If we're producing a final executable, we need to sort the contents
   3747      of the .IA_64.unwind section.  Force this section to be relocated
   3748      into memory rather than written immediately to the output file.  */
   3749   unwind_output_sec = NULL;
   3750   if (!info->relocatable)
   3751     {
   3752       asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
   3753       if (s)
   3754 	{
   3755 	  unwind_output_sec = s->output_section;
   3756 	  unwind_output_sec->contents
   3757 	    = bfd_malloc (unwind_output_sec->size);
   3758 	  if (unwind_output_sec->contents == NULL)
   3759 	    return FALSE;
   3760 	}
   3761     }
   3762 
   3763   /* Invoke the regular ELF backend linker to do all the work.  */
   3764   if (!bfd_elf_final_link (abfd, info))
   3765     return FALSE;
   3766 
   3767   if (unwind_output_sec)
   3768     {
   3769       elfNN_ia64_unwind_entry_compare_bfd = abfd;
   3770       qsort (unwind_output_sec->contents,
   3771 	     (size_t) (unwind_output_sec->size / 24),
   3772 	     24,
   3773 	     elfNN_ia64_unwind_entry_compare);
   3774 
   3775       if (! bfd_set_section_contents (abfd, unwind_output_sec,
   3776 				      unwind_output_sec->contents, (bfd_vma) 0,
   3777 				      unwind_output_sec->size))
   3778 	return FALSE;
   3779     }
   3780 
   3781   return TRUE;
   3782 }
   3783 
   3784 static bfd_boolean
   3785 elfNN_ia64_relocate_section (bfd *output_bfd,
   3786 			     struct bfd_link_info *info,
   3787 			     bfd *input_bfd,
   3788 			     asection *input_section,
   3789 			     bfd_byte *contents,
   3790 			     Elf_Internal_Rela *relocs,
   3791 			     Elf_Internal_Sym *local_syms,
   3792 			     asection **local_sections)
   3793 {
   3794   struct elfNN_ia64_link_hash_table *ia64_info;
   3795   Elf_Internal_Shdr *symtab_hdr;
   3796   Elf_Internal_Rela *rel;
   3797   Elf_Internal_Rela *relend;
   3798   asection *srel;
   3799   bfd_boolean ret_val = TRUE;	/* for non-fatal errors */
   3800   bfd_vma gp_val;
   3801 
   3802   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   3803   ia64_info = elfNN_ia64_hash_table (info);
   3804   if (ia64_info == NULL)
   3805     return FALSE;
   3806 
   3807   /* Infect various flags from the input section to the output section.  */
   3808   if (info->relocatable)
   3809     {
   3810       bfd_vma flags;
   3811 
   3812       flags = elf_section_data(input_section)->this_hdr.sh_flags;
   3813       flags &= SHF_IA_64_NORECOV;
   3814 
   3815       elf_section_data(input_section->output_section)
   3816 	->this_hdr.sh_flags |= flags;
   3817     }
   3818 
   3819   gp_val = _bfd_get_gp_value (output_bfd);
   3820   srel = get_reloc_section (input_bfd, ia64_info, input_section, FALSE);
   3821 
   3822   rel = relocs;
   3823   relend = relocs + input_section->reloc_count;
   3824   for (; rel < relend; ++rel)
   3825     {
   3826       struct elf_link_hash_entry *h;
   3827       struct elfNN_ia64_dyn_sym_info *dyn_i;
   3828       bfd_reloc_status_type r;
   3829       reloc_howto_type *howto;
   3830       unsigned long r_symndx;
   3831       Elf_Internal_Sym *sym;
   3832       unsigned int r_type;
   3833       bfd_vma value;
   3834       asection *sym_sec;
   3835       bfd_byte *hit_addr;
   3836       bfd_boolean dynamic_symbol_p;
   3837       bfd_boolean undef_weak_ref;
   3838 
   3839       r_type = ELFNN_R_TYPE (rel->r_info);
   3840       if (r_type > R_IA64_MAX_RELOC_CODE)
   3841 	{
   3842 	  (*_bfd_error_handler)
   3843 	    (_("%B: unknown relocation type %d"),
   3844 	     input_bfd, (int) r_type);
   3845 	  bfd_set_error (bfd_error_bad_value);
   3846 	  ret_val = FALSE;
   3847 	  continue;
   3848 	}
   3849 
   3850       howto = ia64_elf_lookup_howto (r_type);
   3851       r_symndx = ELFNN_R_SYM (rel->r_info);
   3852       h = NULL;
   3853       sym = NULL;
   3854       sym_sec = NULL;
   3855       undef_weak_ref = FALSE;
   3856 
   3857       if (r_symndx < symtab_hdr->sh_info)
   3858 	{
   3859 	  /* Reloc against local symbol.  */
   3860 	  asection *msec;
   3861 	  sym = local_syms + r_symndx;
   3862 	  sym_sec = local_sections[r_symndx];
   3863 	  msec = sym_sec;
   3864 	  value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
   3865 	  if (!info->relocatable
   3866 	      && (sym_sec->flags & SEC_MERGE) != 0
   3867 	      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
   3868 	      && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   3869 	    {
   3870 	      struct elfNN_ia64_local_hash_entry *loc_h;
   3871 
   3872 	      loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, FALSE);
   3873 	      if (loc_h && ! loc_h->sec_merge_done)
   3874 		{
   3875 		  struct elfNN_ia64_dyn_sym_info *dynent;
   3876 		  unsigned int count;
   3877 
   3878 		  for (count = loc_h->count, dynent = loc_h->info;
   3879 		       count != 0;
   3880 		       count--, dynent++)
   3881 		    {
   3882 		      msec = sym_sec;
   3883 		      dynent->addend =
   3884 			_bfd_merged_section_offset (output_bfd, &msec,
   3885 						    elf_section_data (msec)->
   3886 						    sec_info,
   3887 						    sym->st_value
   3888 						    + dynent->addend);
   3889 		      dynent->addend -= sym->st_value;
   3890 		      dynent->addend += msec->output_section->vma
   3891 					+ msec->output_offset
   3892 					- sym_sec->output_section->vma
   3893 					- sym_sec->output_offset;
   3894 		    }
   3895 
   3896 		  /* We may have introduced duplicated entries. We need
   3897 		     to remove them properly.  */
   3898 		  count = sort_dyn_sym_info (loc_h->info, loc_h->count);
   3899 		  if (count != loc_h->count)
   3900 		    {
   3901 		      loc_h->count = count;
   3902 		      loc_h->sorted_count = count;
   3903 		    }
   3904 
   3905 		  loc_h->sec_merge_done = 1;
   3906 		}
   3907 	    }
   3908 	}
   3909       else
   3910 	{
   3911 	  bfd_boolean unresolved_reloc;
   3912 	  bfd_boolean warned, ignored;
   3913 	  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
   3914 
   3915 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   3916 				   r_symndx, symtab_hdr, sym_hashes,
   3917 				   h, sym_sec, value,
   3918 				   unresolved_reloc, warned, ignored);
   3919 
   3920 	  if (h->root.type == bfd_link_hash_undefweak)
   3921 	    undef_weak_ref = TRUE;
   3922 	  else if (warned || (ignored && info->executable))
   3923 	    continue;
   3924 	}
   3925 
   3926       if (sym_sec != NULL && discarded_section (sym_sec))
   3927 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   3928 					 rel, 1, relend, howto, 0, contents);
   3929 
   3930       if (info->relocatable)
   3931 	continue;
   3932 
   3933       hit_addr = contents + rel->r_offset;
   3934       value += rel->r_addend;
   3935       dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info, r_type);
   3936 
   3937       switch (r_type)
   3938 	{
   3939 	case R_IA64_NONE:
   3940 	case R_IA64_LDXMOV:
   3941 	  continue;
   3942 
   3943 	case R_IA64_IMM14:
   3944 	case R_IA64_IMM22:
   3945 	case R_IA64_IMM64:
   3946 	case R_IA64_DIR32MSB:
   3947 	case R_IA64_DIR32LSB:
   3948 	case R_IA64_DIR64MSB:
   3949 	case R_IA64_DIR64LSB:
   3950 	  /* Install a dynamic relocation for this reloc.  */
   3951 	  if ((dynamic_symbol_p || info->shared)
   3952 	      && r_symndx != STN_UNDEF
   3953 	      && (input_section->flags & SEC_ALLOC) != 0)
   3954 	    {
   3955 	      unsigned int dyn_r_type;
   3956 	      long dynindx;
   3957 	      bfd_vma addend;
   3958 
   3959 	      BFD_ASSERT (srel != NULL);
   3960 
   3961 	      switch (r_type)
   3962 		{
   3963 		case R_IA64_IMM14:
   3964 		case R_IA64_IMM22:
   3965 		case R_IA64_IMM64:
   3966 		  /* ??? People shouldn't be doing non-pic code in
   3967 		     shared libraries nor dynamic executables.  */
   3968 		  (*_bfd_error_handler)
   3969 		    (_("%B: non-pic code with imm relocation against dynamic symbol `%s'"),
   3970 		     input_bfd,
   3971 		     h ? h->root.root.string
   3972 		       : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
   3973 					   sym_sec));
   3974 		  ret_val = FALSE;
   3975 		  continue;
   3976 
   3977 		default:
   3978 		  break;
   3979 		}
   3980 
   3981 	      /* If we don't need dynamic symbol lookup, find a
   3982 		 matching RELATIVE relocation.  */
   3983 	      dyn_r_type = r_type;
   3984 	      if (dynamic_symbol_p)
   3985 		{
   3986 		  dynindx = h->dynindx;
   3987 		  addend = rel->r_addend;
   3988 		  value = 0;
   3989 		}
   3990 	      else
   3991 		{
   3992 		  switch (r_type)
   3993 		    {
   3994 		    case R_IA64_DIR32MSB:
   3995 		      dyn_r_type = R_IA64_REL32MSB;
   3996 		      break;
   3997 		    case R_IA64_DIR32LSB:
   3998 		      dyn_r_type = R_IA64_REL32LSB;
   3999 		      break;
   4000 		    case R_IA64_DIR64MSB:
   4001 		      dyn_r_type = R_IA64_REL64MSB;
   4002 		      break;
   4003 		    case R_IA64_DIR64LSB:
   4004 		      dyn_r_type = R_IA64_REL64LSB;
   4005 		      break;
   4006 
   4007 		    default:
   4008 		      break;
   4009 		    }
   4010 		  dynindx = 0;
   4011 		  addend = value;
   4012 		}
   4013 
   4014 	      elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
   4015 					    srel, rel->r_offset, dyn_r_type,
   4016 					    dynindx, addend);
   4017 	    }
   4018 	  /* Fall through.  */
   4019 
   4020 	case R_IA64_LTV32MSB:
   4021 	case R_IA64_LTV32LSB:
   4022 	case R_IA64_LTV64MSB:
   4023 	case R_IA64_LTV64LSB:
   4024 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4025 	  break;
   4026 
   4027 	case R_IA64_GPREL22:
   4028 	case R_IA64_GPREL64I:
   4029 	case R_IA64_GPREL32MSB:
   4030 	case R_IA64_GPREL32LSB:
   4031 	case R_IA64_GPREL64MSB:
   4032 	case R_IA64_GPREL64LSB:
   4033 	  if (dynamic_symbol_p)
   4034 	    {
   4035 	      (*_bfd_error_handler)
   4036 		(_("%B: @gprel relocation against dynamic symbol %s"),
   4037 		 input_bfd,
   4038 		 h ? h->root.root.string
   4039 		   : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
   4040 				       sym_sec));
   4041 	      ret_val = FALSE;
   4042 	      continue;
   4043 	    }
   4044 	  value -= gp_val;
   4045 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4046 	  break;
   4047 
   4048 	case R_IA64_LTOFF22:
   4049 	case R_IA64_LTOFF22X:
   4050 	case R_IA64_LTOFF64I:
   4051           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
   4052 	  value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
   4053 				 rel->r_addend, value, R_IA64_DIRNNLSB);
   4054 	  value -= gp_val;
   4055 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4056 	  break;
   4057 
   4058 	case R_IA64_PLTOFF22:
   4059 	case R_IA64_PLTOFF64I:
   4060 	case R_IA64_PLTOFF64MSB:
   4061 	case R_IA64_PLTOFF64LSB:
   4062           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
   4063 	  value = set_pltoff_entry (output_bfd, info, dyn_i, value, FALSE);
   4064 	  value -= gp_val;
   4065 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4066 	  break;
   4067 
   4068 	case R_IA64_FPTR64I:
   4069 	case R_IA64_FPTR32MSB:
   4070 	case R_IA64_FPTR32LSB:
   4071 	case R_IA64_FPTR64MSB:
   4072 	case R_IA64_FPTR64LSB:
   4073           dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
   4074 	  if (dyn_i->want_fptr)
   4075 	    {
   4076 	      if (!undef_weak_ref)
   4077 		value = set_fptr_entry (output_bfd, info, dyn_i, value);
   4078 	    }
   4079 	  if (!dyn_i->want_fptr || info->pie)
   4080 	    {
   4081 	      long dynindx;
   4082 	      unsigned int dyn_r_type = r_type;
   4083 	      bfd_vma addend = rel->r_addend;
   4084 
   4085 	      /* Otherwise, we expect the dynamic linker to create
   4086 		 the entry.  */
   4087 
   4088 	      if (dyn_i->want_fptr)
   4089 		{
   4090 		  if (r_type == R_IA64_FPTR64I)
   4091 		    {
   4092 		      /* We can't represent this without a dynamic symbol.
   4093 			 Adjust the relocation to be against an output
   4094 			 section symbol, which are always present in the
   4095 			 dynamic symbol table.  */
   4096 		      /* ??? People shouldn't be doing non-pic code in
   4097 			 shared libraries.  Hork.  */
   4098 		      (*_bfd_error_handler)
   4099 			(_("%B: linking non-pic code in a position independent executable"),
   4100 			 input_bfd);
   4101 		      ret_val = FALSE;
   4102 		      continue;
   4103 		    }
   4104 		  dynindx = 0;
   4105 		  addend = value;
   4106 		  dyn_r_type = r_type + R_IA64_RELNNLSB - R_IA64_FPTRNNLSB;
   4107 		}
   4108 	      else if (h)
   4109 		{
   4110 		  if (h->dynindx != -1)
   4111 		    dynindx = h->dynindx;
   4112 		  else
   4113 		    dynindx = (_bfd_elf_link_lookup_local_dynindx
   4114 			       (info, h->root.u.def.section->owner,
   4115 				global_sym_index (h)));
   4116 		  value = 0;
   4117 		}
   4118 	      else
   4119 		{
   4120 		  dynindx = (_bfd_elf_link_lookup_local_dynindx
   4121 			     (info, input_bfd, (long) r_symndx));
   4122 		  value = 0;
   4123 		}
   4124 
   4125 	      elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
   4126 					    srel, rel->r_offset, dyn_r_type,
   4127 					    dynindx, addend);
   4128 	    }
   4129 
   4130 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4131 	  break;
   4132 
   4133 	case R_IA64_LTOFF_FPTR22:
   4134 	case R_IA64_LTOFF_FPTR64I:
   4135 	case R_IA64_LTOFF_FPTR32MSB:
   4136 	case R_IA64_LTOFF_FPTR32LSB:
   4137 	case R_IA64_LTOFF_FPTR64MSB:
   4138 	case R_IA64_LTOFF_FPTR64LSB:
   4139 	  {
   4140 	    long dynindx;
   4141 
   4142 	    dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
   4143 	    if (dyn_i->want_fptr)
   4144 	      {
   4145 		BFD_ASSERT (h == NULL || h->dynindx == -1);
   4146 	        if (!undef_weak_ref)
   4147 	          value = set_fptr_entry (output_bfd, info, dyn_i, value);
   4148 		dynindx = -1;
   4149 	      }
   4150 	    else
   4151 	      {
   4152 	        /* Otherwise, we expect the dynamic linker to create
   4153 		   the entry.  */
   4154 	        if (h)
   4155 		  {
   4156 		    if (h->dynindx != -1)
   4157 		      dynindx = h->dynindx;
   4158 		    else
   4159 		      dynindx = (_bfd_elf_link_lookup_local_dynindx
   4160 				 (info, h->root.u.def.section->owner,
   4161 				  global_sym_index (h)));
   4162 		  }
   4163 		else
   4164 		  dynindx = (_bfd_elf_link_lookup_local_dynindx
   4165 			     (info, input_bfd, (long) r_symndx));
   4166 		value = 0;
   4167 	      }
   4168 
   4169 	    value = set_got_entry (output_bfd, info, dyn_i, dynindx,
   4170 				   rel->r_addend, value, R_IA64_FPTRNNLSB);
   4171 	    value -= gp_val;
   4172 	    r = ia64_elf_install_value (hit_addr, value, r_type);
   4173 	  }
   4174 	  break;
   4175 
   4176 	case R_IA64_PCREL32MSB:
   4177 	case R_IA64_PCREL32LSB:
   4178 	case R_IA64_PCREL64MSB:
   4179 	case R_IA64_PCREL64LSB:
   4180 	  /* Install a dynamic relocation for this reloc.  */
   4181 	  if (dynamic_symbol_p && r_symndx != STN_UNDEF)
   4182 	    {
   4183 	      BFD_ASSERT (srel != NULL);
   4184 
   4185 	      elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
   4186 					    srel, rel->r_offset, r_type,
   4187 					    h->dynindx, rel->r_addend);
   4188 	    }
   4189 	  goto finish_pcrel;
   4190 
   4191 	case R_IA64_PCREL21B:
   4192 	case R_IA64_PCREL60B:
   4193 	  /* We should have created a PLT entry for any dynamic symbol.  */
   4194 	  dyn_i = NULL;
   4195 	  if (h)
   4196 	    dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
   4197 
   4198 	  if (dyn_i && dyn_i->want_plt2)
   4199 	    {
   4200 	      /* Should have caught this earlier.  */
   4201 	      BFD_ASSERT (rel->r_addend == 0);
   4202 
   4203 	      value = (ia64_info->root.splt->output_section->vma
   4204 		       + ia64_info->root.splt->output_offset
   4205 		       + dyn_i->plt2_offset);
   4206 	    }
   4207 	  else
   4208 	    {
   4209 	      /* Since there's no PLT entry, Validate that this is
   4210 		 locally defined.  */
   4211 	      BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
   4212 
   4213 	      /* If the symbol is undef_weak, we shouldn't be trying
   4214 		 to call it.  There's every chance that we'd wind up
   4215 		 with an out-of-range fixup here.  Don't bother setting
   4216 		 any value at all.  */
   4217 	      if (undef_weak_ref)
   4218 		continue;
   4219 	    }
   4220 	  goto finish_pcrel;
   4221 
   4222 	case R_IA64_PCREL21BI:
   4223 	case R_IA64_PCREL21F:
   4224 	case R_IA64_PCREL21M:
   4225 	case R_IA64_PCREL22:
   4226 	case R_IA64_PCREL64I:
   4227 	  /* The PCREL21BI reloc is specifically not intended for use with
   4228 	     dynamic relocs.  PCREL21F and PCREL21M are used for speculation
   4229 	     fixup code, and thus probably ought not be dynamic.  The
   4230 	     PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs.  */
   4231 	  if (dynamic_symbol_p)
   4232 	    {
   4233 	      const char *msg;
   4234 
   4235 	      if (r_type == R_IA64_PCREL21BI)
   4236 		msg = _("%B: @internal branch to dynamic symbol %s");
   4237 	      else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
   4238 		msg = _("%B: speculation fixup to dynamic symbol %s");
   4239 	      else
   4240 		msg = _("%B: @pcrel relocation against dynamic symbol %s");
   4241 	      (*_bfd_error_handler) (msg, input_bfd,
   4242 				     h ? h->root.root.string
   4243 				       : bfd_elf_sym_name (input_bfd,
   4244 							   symtab_hdr,
   4245 							   sym,
   4246 							   sym_sec));
   4247 	      ret_val = FALSE;
   4248 	      continue;
   4249 	    }
   4250 	  goto finish_pcrel;
   4251 
   4252 	finish_pcrel:
   4253 	  /* Make pc-relative.  */
   4254 	  value -= (input_section->output_section->vma
   4255 		    + input_section->output_offset
   4256 		    + rel->r_offset) & ~ (bfd_vma) 0x3;
   4257 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4258 	  break;
   4259 
   4260 	case R_IA64_SEGREL32MSB:
   4261 	case R_IA64_SEGREL32LSB:
   4262 	case R_IA64_SEGREL64MSB:
   4263 	case R_IA64_SEGREL64LSB:
   4264 	    {
   4265 	      /* Find the segment that contains the output_section.  */
   4266 	      Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
   4267 		(output_bfd, input_section->output_section);
   4268 
   4269 	      if (p == NULL)
   4270 		{
   4271 		  r = bfd_reloc_notsupported;
   4272 		}
   4273 	      else
   4274 		{
   4275 		  /* The VMA of the segment is the vaddr of the associated
   4276 		     program header.  */
   4277 		  if (value > p->p_vaddr)
   4278 		    value -= p->p_vaddr;
   4279 		  else
   4280 		    value = 0;
   4281 		  r = ia64_elf_install_value (hit_addr, value, r_type);
   4282 		}
   4283 	      break;
   4284 	    }
   4285 
   4286 	case R_IA64_SECREL32MSB:
   4287 	case R_IA64_SECREL32LSB:
   4288 	case R_IA64_SECREL64MSB:
   4289 	case R_IA64_SECREL64LSB:
   4290 	  /* Make output-section relative to section where the symbol
   4291 	     is defined. PR 475  */
   4292 	  if (sym_sec)
   4293 	    value -= sym_sec->output_section->vma;
   4294 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4295 	  break;
   4296 
   4297 	case R_IA64_IPLTMSB:
   4298 	case R_IA64_IPLTLSB:
   4299 	  /* Install a dynamic relocation for this reloc.  */
   4300 	  if ((dynamic_symbol_p || info->shared)
   4301 	      && (input_section->flags & SEC_ALLOC) != 0)
   4302 	    {
   4303 	      BFD_ASSERT (srel != NULL);
   4304 
   4305 	      /* If we don't need dynamic symbol lookup, install two
   4306 		 RELATIVE relocations.  */
   4307 	      if (!dynamic_symbol_p)
   4308 		{
   4309 		  unsigned int dyn_r_type;
   4310 
   4311 		  if (r_type == R_IA64_IPLTMSB)
   4312 		    dyn_r_type = R_IA64_REL64MSB;
   4313 		  else
   4314 		    dyn_r_type = R_IA64_REL64LSB;
   4315 
   4316 		  elfNN_ia64_install_dyn_reloc (output_bfd, info,
   4317 						input_section,
   4318 						srel, rel->r_offset,
   4319 						dyn_r_type, 0, value);
   4320 		  elfNN_ia64_install_dyn_reloc (output_bfd, info,
   4321 						input_section,
   4322 						srel, rel->r_offset + 8,
   4323 						dyn_r_type, 0, gp_val);
   4324 		}
   4325 	      else
   4326 		elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
   4327 					      srel, rel->r_offset, r_type,
   4328 					      h->dynindx, rel->r_addend);
   4329 	    }
   4330 
   4331 	  if (r_type == R_IA64_IPLTMSB)
   4332 	    r_type = R_IA64_DIR64MSB;
   4333 	  else
   4334 	    r_type = R_IA64_DIR64LSB;
   4335 	  ia64_elf_install_value (hit_addr, value, r_type);
   4336 	  r = ia64_elf_install_value (hit_addr + 8, gp_val, r_type);
   4337 	  break;
   4338 
   4339 	case R_IA64_TPREL14:
   4340 	case R_IA64_TPREL22:
   4341 	case R_IA64_TPREL64I:
   4342 	  if (elf_hash_table (info)->tls_sec == NULL)
   4343 	    goto missing_tls_sec;
   4344 	  value -= elfNN_ia64_tprel_base (info);
   4345 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4346 	  break;
   4347 
   4348 	case R_IA64_DTPREL14:
   4349 	case R_IA64_DTPREL22:
   4350 	case R_IA64_DTPREL64I:
   4351 	case R_IA64_DTPREL32LSB:
   4352 	case R_IA64_DTPREL32MSB:
   4353 	case R_IA64_DTPREL64LSB:
   4354 	case R_IA64_DTPREL64MSB:
   4355 	  if (elf_hash_table (info)->tls_sec == NULL)
   4356 	    goto missing_tls_sec;
   4357 	  value -= elfNN_ia64_dtprel_base (info);
   4358 	  r = ia64_elf_install_value (hit_addr, value, r_type);
   4359 	  break;
   4360 
   4361 	case R_IA64_LTOFF_TPREL22:
   4362 	case R_IA64_LTOFF_DTPMOD22:
   4363 	case R_IA64_LTOFF_DTPREL22:
   4364 	  {
   4365 	    int got_r_type;
   4366 	    long dynindx = h ? h->dynindx : -1;
   4367 	    bfd_vma r_addend = rel->r_addend;
   4368 
   4369 	    switch (r_type)
   4370 	      {
   4371 	      default:
   4372 	      case R_IA64_LTOFF_TPREL22:
   4373 		if (!dynamic_symbol_p)
   4374 		  {
   4375 		    if (elf_hash_table (info)->tls_sec == NULL)
   4376 		      goto missing_tls_sec;
   4377 		    if (!info->shared)
   4378 		      value -= elfNN_ia64_tprel_base (info);
   4379 		    else
   4380 		      {
   4381 			r_addend += value - elfNN_ia64_dtprel_base (info);
   4382 			dynindx = 0;
   4383 		      }
   4384 		  }
   4385 		got_r_type = R_IA64_TPREL64LSB;
   4386 		break;
   4387 	      case R_IA64_LTOFF_DTPMOD22:
   4388 		if (!dynamic_symbol_p && !info->shared)
   4389 		  value = 1;
   4390 		got_r_type = R_IA64_DTPMOD64LSB;
   4391 		break;
   4392 	      case R_IA64_LTOFF_DTPREL22:
   4393 		if (!dynamic_symbol_p)
   4394 		  {
   4395 		    if (elf_hash_table (info)->tls_sec == NULL)
   4396 		      goto missing_tls_sec;
   4397 		    value -= elfNN_ia64_dtprel_base (info);
   4398 		  }
   4399 		got_r_type = R_IA64_DTPRELNNLSB;
   4400 		break;
   4401 	      }
   4402 	    dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
   4403 	    value = set_got_entry (input_bfd, info, dyn_i, dynindx, r_addend,
   4404 				   value, got_r_type);
   4405 	    value -= gp_val;
   4406 	    r = ia64_elf_install_value (hit_addr, value, r_type);
   4407 	  }
   4408 	  break;
   4409 
   4410 	default:
   4411 	  r = bfd_reloc_notsupported;
   4412 	  break;
   4413 	}
   4414 
   4415       switch (r)
   4416 	{
   4417 	case bfd_reloc_ok:
   4418 	  break;
   4419 
   4420 	case bfd_reloc_undefined:
   4421 	  /* This can happen for global table relative relocs if
   4422 	     __gp is undefined.  This is a panic situation so we
   4423 	     don't try to continue.  */
   4424 	  (*info->callbacks->undefined_symbol)
   4425 	    (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
   4426 	  return FALSE;
   4427 
   4428 	case bfd_reloc_notsupported:
   4429 	  {
   4430 	    const char *name;
   4431 
   4432 	    if (h)
   4433 	      name = h->root.root.string;
   4434 	    else
   4435 	      name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
   4436 				       sym_sec);
   4437 	    if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
   4438 					      name, input_bfd,
   4439 					      input_section, rel->r_offset))
   4440 	      return FALSE;
   4441 	    ret_val = FALSE;
   4442 	  }
   4443 	  break;
   4444 
   4445 	case bfd_reloc_dangerous:
   4446 	case bfd_reloc_outofrange:
   4447 	case bfd_reloc_overflow:
   4448 	default:
   4449 missing_tls_sec:
   4450 	  {
   4451 	    const char *name;
   4452 
   4453 	    if (h)
   4454 	      name = h->root.root.string;
   4455 	    else
   4456 	      name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
   4457 				       sym_sec);
   4458 
   4459 	    switch (r_type)
   4460 	      {
   4461 	      case R_IA64_TPREL14:
   4462 	      case R_IA64_TPREL22:
   4463 	      case R_IA64_TPREL64I:
   4464 	      case R_IA64_DTPREL14:
   4465 	      case R_IA64_DTPREL22:
   4466 	      case R_IA64_DTPREL64I:
   4467 	      case R_IA64_DTPREL32LSB:
   4468 	      case R_IA64_DTPREL32MSB:
   4469 	      case R_IA64_DTPREL64LSB:
   4470 	      case R_IA64_DTPREL64MSB:
   4471 	      case R_IA64_LTOFF_TPREL22:
   4472 	      case R_IA64_LTOFF_DTPMOD22:
   4473 	      case R_IA64_LTOFF_DTPREL22:
   4474 		(*_bfd_error_handler)
   4475 		  (_("%B: missing TLS section for relocation %s against `%s' at 0x%lx in section `%A'."),
   4476 		   input_bfd, input_section, howto->name, name,
   4477 		   rel->r_offset);
   4478 		break;
   4479 
   4480 	      case R_IA64_PCREL21B:
   4481 	      case R_IA64_PCREL21BI:
   4482 	      case R_IA64_PCREL21M:
   4483 	      case R_IA64_PCREL21F:
   4484 		if (is_elf_hash_table (info->hash))
   4485 		  {
   4486 		    /* Relaxtion is always performed for ELF output.
   4487 		       Overflow failures for those relocations mean
   4488 		       that the section is too big to relax.  */
   4489 		    (*_bfd_error_handler)
   4490 		      (_("%B: Can't relax br (%s) to `%s' at 0x%lx in section `%A' with size 0x%lx (> 0x1000000)."),
   4491 		       input_bfd, input_section, howto->name, name,
   4492 		       rel->r_offset, input_section->size);
   4493 		    break;
   4494 		  }
   4495 	      default:
   4496 		if (!(*info->callbacks->reloc_overflow) (info,
   4497 							 &h->root,
   4498 							 name,
   4499 							 howto->name,
   4500 							 (bfd_vma) 0,
   4501 							 input_bfd,
   4502 							 input_section,
   4503 							 rel->r_offset))
   4504 		  return FALSE;
   4505 		break;
   4506 	      }
   4507 
   4508 	    ret_val = FALSE;
   4509 	  }
   4510 	  break;
   4511 	}
   4512     }
   4513 
   4514   return ret_val;
   4515 }
   4516 
   4517 static bfd_boolean
   4518 elfNN_ia64_finish_dynamic_symbol (bfd *output_bfd,
   4519 				  struct bfd_link_info *info,
   4520 				  struct elf_link_hash_entry *h,
   4521 				  Elf_Internal_Sym *sym)
   4522 {
   4523   struct elfNN_ia64_link_hash_table *ia64_info;
   4524   struct elfNN_ia64_dyn_sym_info *dyn_i;
   4525 
   4526   ia64_info = elfNN_ia64_hash_table (info);
   4527   if (ia64_info == NULL)
   4528     return FALSE;
   4529 
   4530   dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
   4531 
   4532   /* Fill in the PLT data, if required.  */
   4533   if (dyn_i && dyn_i->want_plt)
   4534     {
   4535       Elf_Internal_Rela outrel;
   4536       bfd_byte *loc;
   4537       asection *plt_sec;
   4538       bfd_vma plt_addr, pltoff_addr, gp_val, plt_index;
   4539 
   4540       gp_val = _bfd_get_gp_value (output_bfd);
   4541 
   4542       /* Initialize the minimal PLT entry.  */
   4543 
   4544       plt_index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
   4545       plt_sec = ia64_info->root.splt;
   4546       loc = plt_sec->contents + dyn_i->plt_offset;
   4547 
   4548       memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
   4549       ia64_elf_install_value (loc, plt_index, R_IA64_IMM22);
   4550       ia64_elf_install_value (loc+2, -dyn_i->plt_offset, R_IA64_PCREL21B);
   4551 
   4552       plt_addr = (plt_sec->output_section->vma
   4553 		  + plt_sec->output_offset
   4554 		  + dyn_i->plt_offset);
   4555       pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, TRUE);
   4556 
   4557       /* Initialize the FULL PLT entry, if needed.  */
   4558       if (dyn_i->want_plt2)
   4559 	{
   4560 	  loc = plt_sec->contents + dyn_i->plt2_offset;
   4561 
   4562 	  memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
   4563 	  ia64_elf_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
   4564 
   4565 	  /* Mark the symbol as undefined, rather than as defined in the
   4566 	     plt section.  Leave the value alone.  */
   4567 	  /* ??? We didn't redefine it in adjust_dynamic_symbol in the
   4568 	     first place.  But perhaps elflink.c did some for us.  */
   4569 	  if (!h->def_regular)
   4570 	    sym->st_shndx = SHN_UNDEF;
   4571 	}
   4572 
   4573       /* Create the dynamic relocation.  */
   4574       outrel.r_offset = pltoff_addr;
   4575       if (bfd_little_endian (output_bfd))
   4576 	outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
   4577       else
   4578 	outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
   4579       outrel.r_addend = 0;
   4580 
   4581       /* This is fun.  In the .IA_64.pltoff section, we've got entries
   4582 	 that correspond both to real PLT entries, and those that
   4583 	 happened to resolve to local symbols but need to be created
   4584 	 to satisfy @pltoff relocations.  The .rela.IA_64.pltoff
   4585 	 relocations for the real PLT should come at the end of the
   4586 	 section, so that they can be indexed by plt entry at runtime.
   4587 
   4588 	 We emitted all of the relocations for the non-PLT @pltoff
   4589 	 entries during relocate_section.  So we can consider the
   4590 	 existing sec->reloc_count to be the base of the array of
   4591 	 PLT relocations.  */
   4592 
   4593       loc = ia64_info->rel_pltoff_sec->contents;
   4594       loc += ((ia64_info->rel_pltoff_sec->reloc_count + plt_index)
   4595 	      * sizeof (ElfNN_External_Rela));
   4596       bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
   4597     }
   4598 
   4599   /* Mark some specially defined symbols as absolute.  */
   4600   if (h == ia64_info->root.hdynamic
   4601       || h == ia64_info->root.hgot
   4602       || h == ia64_info->root.hplt)
   4603     sym->st_shndx = SHN_ABS;
   4604 
   4605   return TRUE;
   4606 }
   4607 
   4608 static bfd_boolean
   4609 elfNN_ia64_finish_dynamic_sections (bfd *abfd,
   4610 				    struct bfd_link_info *info)
   4611 {
   4612   struct elfNN_ia64_link_hash_table *ia64_info;
   4613   bfd *dynobj;
   4614 
   4615   ia64_info = elfNN_ia64_hash_table (info);
   4616   if (ia64_info == NULL)
   4617     return FALSE;
   4618 
   4619   dynobj = ia64_info->root.dynobj;
   4620 
   4621   if (elf_hash_table (info)->dynamic_sections_created)
   4622     {
   4623       ElfNN_External_Dyn *dyncon, *dynconend;
   4624       asection *sdyn, *sgotplt;
   4625       bfd_vma gp_val;
   4626 
   4627       sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   4628       sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
   4629       BFD_ASSERT (sdyn != NULL);
   4630       dyncon = (ElfNN_External_Dyn *) sdyn->contents;
   4631       dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
   4632 
   4633       gp_val = _bfd_get_gp_value (abfd);
   4634 
   4635       for (; dyncon < dynconend; dyncon++)
   4636 	{
   4637 	  Elf_Internal_Dyn dyn;
   4638 
   4639 	  bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
   4640 
   4641 	  switch (dyn.d_tag)
   4642 	    {
   4643 	    case DT_PLTGOT:
   4644 	      dyn.d_un.d_ptr = gp_val;
   4645 	      break;
   4646 
   4647 	    case DT_PLTRELSZ:
   4648 	      dyn.d_un.d_val = (ia64_info->minplt_entries
   4649 				* sizeof (ElfNN_External_Rela));
   4650 	      break;
   4651 
   4652 	    case DT_JMPREL:
   4653 	      /* See the comment above in finish_dynamic_symbol.  */
   4654 	      dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
   4655 				+ ia64_info->rel_pltoff_sec->output_offset
   4656 				+ (ia64_info->rel_pltoff_sec->reloc_count
   4657 				   * sizeof (ElfNN_External_Rela)));
   4658 	      break;
   4659 
   4660 	    case DT_IA_64_PLT_RESERVE:
   4661 	      dyn.d_un.d_ptr = (sgotplt->output_section->vma
   4662 				+ sgotplt->output_offset);
   4663 	      break;
   4664 
   4665 	    case DT_RELASZ:
   4666 	      /* Do not have RELASZ include JMPREL.  This makes things
   4667 		 easier on ld.so.  This is not what the rest of BFD set up.  */
   4668 	      dyn.d_un.d_val -= (ia64_info->minplt_entries
   4669 				 * sizeof (ElfNN_External_Rela));
   4670 	      break;
   4671 	    }
   4672 
   4673 	  bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
   4674 	}
   4675 
   4676       /* Initialize the PLT0 entry.  */
   4677       if (ia64_info->root.splt)
   4678 	{
   4679 	  bfd_byte *loc = ia64_info->root.splt->contents;
   4680 	  bfd_vma pltres;
   4681 
   4682 	  memcpy (loc, plt_header, PLT_HEADER_SIZE);
   4683 
   4684 	  pltres = (sgotplt->output_section->vma
   4685 		    + sgotplt->output_offset
   4686 		    - gp_val);
   4687 
   4688 	  ia64_elf_install_value (loc+1, pltres, R_IA64_GPREL22);
   4689 	}
   4690     }
   4691 
   4692   return TRUE;
   4693 }
   4694 
   4695 /* ELF file flag handling:  */
   4697 
   4698 /* Function to keep IA-64 specific file flags.  */
   4699 static bfd_boolean
   4700 elfNN_ia64_set_private_flags (bfd *abfd, flagword flags)
   4701 {
   4702   BFD_ASSERT (!elf_flags_init (abfd)
   4703 	      || elf_elfheader (abfd)->e_flags == flags);
   4704 
   4705   elf_elfheader (abfd)->e_flags = flags;
   4706   elf_flags_init (abfd) = TRUE;
   4707   return TRUE;
   4708 }
   4709 
   4710 /* Merge backend specific data from an object file to the output
   4711    object file when linking.  */
   4712 static bfd_boolean
   4713 elfNN_ia64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   4714 {
   4715   flagword out_flags;
   4716   flagword in_flags;
   4717   bfd_boolean ok = TRUE;
   4718 
   4719   /* Don't even pretend to support mixed-format linking.  */
   4720   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   4721       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   4722     return FALSE;
   4723 
   4724   in_flags  = elf_elfheader (ibfd)->e_flags;
   4725   out_flags = elf_elfheader (obfd)->e_flags;
   4726 
   4727   if (! elf_flags_init (obfd))
   4728     {
   4729       elf_flags_init (obfd) = TRUE;
   4730       elf_elfheader (obfd)->e_flags = in_flags;
   4731 
   4732       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   4733 	  && bfd_get_arch_info (obfd)->the_default)
   4734 	{
   4735 	  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   4736 				    bfd_get_mach (ibfd));
   4737 	}
   4738 
   4739       return TRUE;
   4740     }
   4741 
   4742   /* Check flag compatibility.  */
   4743   if (in_flags == out_flags)
   4744     return TRUE;
   4745 
   4746   /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set.  */
   4747   if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
   4748     elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
   4749 
   4750   if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
   4751     {
   4752       (*_bfd_error_handler)
   4753 	(_("%B: linking trap-on-NULL-dereference with non-trapping files"),
   4754 	 ibfd);
   4755 
   4756       bfd_set_error (bfd_error_bad_value);
   4757       ok = FALSE;
   4758     }
   4759   if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
   4760     {
   4761       (*_bfd_error_handler)
   4762 	(_("%B: linking big-endian files with little-endian files"),
   4763 	 ibfd);
   4764 
   4765       bfd_set_error (bfd_error_bad_value);
   4766       ok = FALSE;
   4767     }
   4768   if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
   4769     {
   4770       (*_bfd_error_handler)
   4771 	(_("%B: linking 64-bit files with 32-bit files"),
   4772 	 ibfd);
   4773 
   4774       bfd_set_error (bfd_error_bad_value);
   4775       ok = FALSE;
   4776     }
   4777   if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
   4778     {
   4779       (*_bfd_error_handler)
   4780 	(_("%B: linking constant-gp files with non-constant-gp files"),
   4781 	 ibfd);
   4782 
   4783       bfd_set_error (bfd_error_bad_value);
   4784       ok = FALSE;
   4785     }
   4786   if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
   4787       != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
   4788     {
   4789       (*_bfd_error_handler)
   4790 	(_("%B: linking auto-pic files with non-auto-pic files"),
   4791 	 ibfd);
   4792 
   4793       bfd_set_error (bfd_error_bad_value);
   4794       ok = FALSE;
   4795     }
   4796 
   4797   return ok;
   4798 }
   4799 
   4800 static bfd_boolean
   4801 elfNN_ia64_print_private_bfd_data (bfd *abfd, void * ptr)
   4802 {
   4803   FILE *file = (FILE *) ptr;
   4804   flagword flags = elf_elfheader (abfd)->e_flags;
   4805 
   4806   BFD_ASSERT (abfd != NULL && ptr != NULL);
   4807 
   4808   fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
   4809 	   (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
   4810 	   (flags & EF_IA_64_EXT) ? "EXT, " : "",
   4811 	   (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
   4812 	   (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
   4813 	   (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
   4814 	   (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
   4815 	   (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
   4816 	   (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
   4817 
   4818   _bfd_elf_print_private_bfd_data (abfd, ptr);
   4819   return TRUE;
   4820 }
   4821 
   4822 static enum elf_reloc_type_class
   4823 elfNN_ia64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   4824 			     const asection *rel_sec ATTRIBUTE_UNUSED,
   4825 			     const Elf_Internal_Rela *rela)
   4826 {
   4827   switch ((int) ELFNN_R_TYPE (rela->r_info))
   4828     {
   4829     case R_IA64_REL32MSB:
   4830     case R_IA64_REL32LSB:
   4831     case R_IA64_REL64MSB:
   4832     case R_IA64_REL64LSB:
   4833       return reloc_class_relative;
   4834     case R_IA64_IPLTMSB:
   4835     case R_IA64_IPLTLSB:
   4836       return reloc_class_plt;
   4837     case R_IA64_COPY:
   4838       return reloc_class_copy;
   4839     default:
   4840       return reloc_class_normal;
   4841     }
   4842 }
   4843 
   4844 static const struct bfd_elf_special_section elfNN_ia64_special_sections[] =
   4845 {
   4846   { STRING_COMMA_LEN (".sbss"),  -1, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
   4847   { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
   4848   { NULL,                    0,   0, 0,            0 }
   4849 };
   4850 
   4851 static bfd_boolean
   4852 elfNN_ia64_object_p (bfd *abfd)
   4853 {
   4854   asection *sec;
   4855   asection *group, *unwi, *unw;
   4856   flagword flags;
   4857   const char *name;
   4858   char *unwi_name, *unw_name;
   4859   bfd_size_type amt;
   4860 
   4861   if (abfd->flags & DYNAMIC)
   4862     return TRUE;
   4863 
   4864   /* Flags for fake group section.  */
   4865   flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
   4866 	   | SEC_EXCLUDE);
   4867 
   4868   /* We add a fake section group for each .gnu.linkonce.t.* section,
   4869      which isn't in a section group, and its unwind sections.  */
   4870   for (sec = abfd->sections; sec != NULL; sec = sec->next)
   4871     {
   4872       if (elf_sec_group (sec) == NULL
   4873 	  && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
   4874 	      == (SEC_LINK_ONCE | SEC_CODE))
   4875 	  && CONST_STRNEQ (sec->name, ".gnu.linkonce.t."))
   4876 	{
   4877 	  name = sec->name + 16;
   4878 
   4879 	  amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
   4880 	  unwi_name = bfd_alloc (abfd, amt);
   4881 	  if (!unwi_name)
   4882 	    return FALSE;
   4883 
   4884 	  strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
   4885 	  unwi = bfd_get_section_by_name (abfd, unwi_name);
   4886 
   4887 	  amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
   4888 	  unw_name = bfd_alloc (abfd, amt);
   4889 	  if (!unw_name)
   4890 	    return FALSE;
   4891 
   4892 	  strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
   4893 	  unw = bfd_get_section_by_name (abfd, unw_name);
   4894 
   4895 	  /* We need to create a fake group section for it and its
   4896 	     unwind sections.  */
   4897 	  group = bfd_make_section_anyway_with_flags (abfd, name,
   4898 						      flags);
   4899 	  if (group == NULL)
   4900 	    return FALSE;
   4901 
   4902 	  /* Move the fake group section to the beginning.  */
   4903 	  bfd_section_list_remove (abfd, group);
   4904 	  bfd_section_list_prepend (abfd, group);
   4905 
   4906 	  elf_next_in_group (group) = sec;
   4907 
   4908 	  elf_group_name (sec) = name;
   4909 	  elf_next_in_group (sec) = sec;
   4910 	  elf_sec_group (sec) = group;
   4911 
   4912 	  if (unwi)
   4913 	    {
   4914 	      elf_group_name (unwi) = name;
   4915 	      elf_next_in_group (unwi) = sec;
   4916 	      elf_next_in_group (sec) = unwi;
   4917 	      elf_sec_group (unwi) = group;
   4918 	    }
   4919 
   4920 	   if (unw)
   4921 	     {
   4922 	       elf_group_name (unw) = name;
   4923 	       if (unwi)
   4924 		 {
   4925 		   elf_next_in_group (unw) = elf_next_in_group (unwi);
   4926 		   elf_next_in_group (unwi) = unw;
   4927 		 }
   4928 	       else
   4929 		 {
   4930 		   elf_next_in_group (unw) = sec;
   4931 		   elf_next_in_group (sec) = unw;
   4932 		 }
   4933 	       elf_sec_group (unw) = group;
   4934 	     }
   4935 
   4936 	   /* Fake SHT_GROUP section header.  */
   4937 	  elf_section_data (group)->this_hdr.bfd_section = group;
   4938 	  elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
   4939 	}
   4940     }
   4941   return TRUE;
   4942 }
   4943 
   4944 static bfd_boolean
   4945 elfNN_ia64_hpux_vec (const bfd_target *vec)
   4946 {
   4947   extern const bfd_target ia64_elfNN_hpux_be_vec;
   4948   return (vec == &ia64_elfNN_hpux_be_vec);
   4949 }
   4950 
   4951 static void
   4952 elfNN_hpux_post_process_headers (bfd *abfd,
   4953 				 struct bfd_link_info *info ATTRIBUTE_UNUSED)
   4954 {
   4955   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   4956 
   4957   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
   4958   i_ehdrp->e_ident[EI_ABIVERSION] = 1;
   4959 }
   4960 
   4961 static bfd_boolean
   4962 elfNN_hpux_backend_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
   4963 					     asection *sec, int *retval)
   4964 {
   4965   if (bfd_is_com_section (sec))
   4966     {
   4967       *retval = SHN_IA_64_ANSI_COMMON;
   4968       return TRUE;
   4969     }
   4970   return FALSE;
   4971 }
   4972 
   4973 static void
   4974 elfNN_hpux_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
   4975 				      asymbol *asym)
   4976 {
   4977   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
   4978 
   4979   switch (elfsym->internal_elf_sym.st_shndx)
   4980     {
   4981     case SHN_IA_64_ANSI_COMMON:
   4982       asym->section = bfd_com_section_ptr;
   4983       asym->value = elfsym->internal_elf_sym.st_size;
   4984       asym->flags &= ~BSF_GLOBAL;
   4985       break;
   4986     }
   4987 }
   4988 
   4989 #define TARGET_LITTLE_SYM		ia64_elfNN_le_vec
   4991 #define TARGET_LITTLE_NAME		"elfNN-ia64-little"
   4992 #define TARGET_BIG_SYM			ia64_elfNN_be_vec
   4993 #define TARGET_BIG_NAME			"elfNN-ia64-big"
   4994 #define ELF_ARCH			bfd_arch_ia64
   4995 #define ELF_TARGET_ID			IA64_ELF_DATA
   4996 #define ELF_MACHINE_CODE		EM_IA_64
   4997 #define ELF_MACHINE_ALT1		1999	/* EAS2.3 */
   4998 #define ELF_MACHINE_ALT2		1998	/* EAS2.2 */
   4999 #define ELF_MAXPAGESIZE			0x10000	/* 64KB */
   5000 #define ELF_COMMONPAGESIZE		0x4000	/* 16KB */
   5001 
   5002 #define elf_backend_section_from_shdr \
   5003 	elfNN_ia64_section_from_shdr
   5004 #define elf_backend_section_flags \
   5005 	elfNN_ia64_section_flags
   5006 #define elf_backend_fake_sections \
   5007 	elfNN_ia64_fake_sections
   5008 #define elf_backend_final_write_processing \
   5009 	elfNN_ia64_final_write_processing
   5010 #define elf_backend_add_symbol_hook \
   5011 	elfNN_ia64_add_symbol_hook
   5012 #define elf_backend_additional_program_headers \
   5013 	elfNN_ia64_additional_program_headers
   5014 #define elf_backend_modify_segment_map \
   5015 	elfNN_ia64_modify_segment_map
   5016 #define elf_backend_modify_program_headers \
   5017 	elfNN_ia64_modify_program_headers
   5018 #define elf_info_to_howto \
   5019 	elfNN_ia64_info_to_howto
   5020 
   5021 #define bfd_elfNN_bfd_reloc_type_lookup \
   5022 	ia64_elf_reloc_type_lookup
   5023 #define bfd_elfNN_bfd_reloc_name_lookup \
   5024 	ia64_elf_reloc_name_lookup
   5025 #define bfd_elfNN_bfd_is_local_label_name \
   5026 	elfNN_ia64_is_local_label_name
   5027 #define bfd_elfNN_bfd_relax_section \
   5028 	elfNN_ia64_relax_section
   5029 
   5030 #define elf_backend_object_p \
   5031 	elfNN_ia64_object_p
   5032 
   5033 /* Stuff for the BFD linker: */
   5034 #define bfd_elfNN_bfd_link_hash_table_create \
   5035 	elfNN_ia64_hash_table_create
   5036 #define elf_backend_create_dynamic_sections \
   5037 	elfNN_ia64_create_dynamic_sections
   5038 #define elf_backend_check_relocs \
   5039 	elfNN_ia64_check_relocs
   5040 #define elf_backend_adjust_dynamic_symbol \
   5041 	elfNN_ia64_adjust_dynamic_symbol
   5042 #define elf_backend_size_dynamic_sections \
   5043 	elfNN_ia64_size_dynamic_sections
   5044 #define elf_backend_omit_section_dynsym \
   5045   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
   5046 #define elf_backend_relocate_section \
   5047 	elfNN_ia64_relocate_section
   5048 #define elf_backend_finish_dynamic_symbol \
   5049 	elfNN_ia64_finish_dynamic_symbol
   5050 #define elf_backend_finish_dynamic_sections \
   5051 	elfNN_ia64_finish_dynamic_sections
   5052 #define bfd_elfNN_bfd_final_link \
   5053 	elfNN_ia64_final_link
   5054 
   5055 #define bfd_elfNN_bfd_merge_private_bfd_data \
   5056 	elfNN_ia64_merge_private_bfd_data
   5057 #define bfd_elfNN_bfd_set_private_flags \
   5058 	elfNN_ia64_set_private_flags
   5059 #define bfd_elfNN_bfd_print_private_bfd_data \
   5060 	elfNN_ia64_print_private_bfd_data
   5061 
   5062 #define elf_backend_plt_readonly	1
   5063 #define elf_backend_want_plt_sym	0
   5064 #define elf_backend_plt_alignment	5
   5065 #define elf_backend_got_header_size	0
   5066 #define elf_backend_want_got_plt	1
   5067 #define elf_backend_may_use_rel_p	1
   5068 #define elf_backend_may_use_rela_p	1
   5069 #define elf_backend_default_use_rela_p	1
   5070 #define elf_backend_want_dynbss		0
   5071 #define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
   5072 #define elf_backend_hide_symbol		elfNN_ia64_hash_hide_symbol
   5073 #define elf_backend_fixup_symbol	_bfd_elf_link_hash_fixup_symbol
   5074 #define elf_backend_reloc_type_class	elfNN_ia64_reloc_type_class
   5075 #define elf_backend_rela_normal		1
   5076 #define elf_backend_special_sections	elfNN_ia64_special_sections
   5077 #define elf_backend_default_execstack	0
   5078 
   5079 /* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
   5080    SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
   5081    We don't want to flood users with so many error messages. We turn
   5082    off the warning for now. It will be turned on later when the Intel
   5083    compiler is fixed.   */
   5084 #define elf_backend_link_order_error_handler NULL
   5085 
   5086 #include "elfNN-target.h"
   5087 
   5088 /* HPUX-specific vectors.  */
   5089 
   5090 #undef  TARGET_LITTLE_SYM
   5091 #undef  TARGET_LITTLE_NAME
   5092 #undef  TARGET_BIG_SYM
   5093 #define TARGET_BIG_SYM                  ia64_elfNN_hpux_be_vec
   5094 #undef  TARGET_BIG_NAME
   5095 #define TARGET_BIG_NAME                 "elfNN-ia64-hpux-big"
   5096 
   5097 /* These are HP-UX specific functions.  */
   5098 
   5099 #undef  elf_backend_post_process_headers
   5100 #define elf_backend_post_process_headers elfNN_hpux_post_process_headers
   5101 
   5102 #undef  elf_backend_section_from_bfd_section
   5103 #define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
   5104 
   5105 #undef elf_backend_symbol_processing
   5106 #define elf_backend_symbol_processing elfNN_hpux_backend_symbol_processing
   5107 
   5108 #undef  elf_backend_want_p_paddr_set_to_zero
   5109 #define elf_backend_want_p_paddr_set_to_zero 1
   5110 
   5111 #undef ELF_COMMONPAGESIZE
   5112 #undef ELF_OSABI
   5113 #define ELF_OSABI			ELFOSABI_HPUX
   5114 
   5115 #undef  elfNN_bed
   5116 #define elfNN_bed elfNN_ia64_hpux_bed
   5117 
   5118 #include "elfNN-target.h"
   5119