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