Coverage Report

Created: 2024-05-21 06:29

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