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