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