Coverage Report

Created: 2025-06-24 06:45

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