Coverage Report

Created: 2023-08-28 06:26

/src/binutils-gdb/bfd/elf32-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-2023 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 32
34
35
#if ARCH_SIZE == 64
36
#define LOG_SECTION_ALIGN 3
37
#endif
38
39
#if ARCH_SIZE == 32
40
0
#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 elf32_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 elf32_ia64_dyn_reloc_entry
74
  {
75
    struct elf32_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 elf32_ia64_local_hash_entry
106
{
107
  int id;
108
  unsigned int r_sym;
109
  /* The number of elements in elf32_ia64_dyn_sym_info array.  */
110
  unsigned int count;
111
  /* The number of sorted elements in elf32_ia64_dyn_sym_info array.  */
112
  unsigned int sorted_count;
113
  /* The size of elf32_ia64_dyn_sym_info array.  */
114
  unsigned int size;
115
  /* The array of elf32_ia64_dyn_sym_info.  */
116
  struct elf32_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 elf32_ia64_link_hash_entry
124
{
125
  struct elf_link_hash_entry root;
126
  /* The number of elements in elf32_ia64_dyn_sym_info array.  */
127
  unsigned int count;
128
  /* The number of sorted elements in elf32_ia64_dyn_sym_info array.  */
129
  unsigned int sorted_count;
130
  /* The size of elf32_ia64_dyn_sym_info array.  */
131
  unsigned int size;
132
  /* The array of elf32_ia64_dyn_sym_info.  */
133
  struct elf32_ia64_dyn_sym_info *info;
134
};
135
136
struct elf32_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 elf32_ia64_allocate_data
163
{
164
  struct bfd_link_info *info;
165
  bfd_size_type ofs;
166
  bool only_got;
167
};
168
169
#define elf32_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 elf32_ia64_link_hash_table *) (p)->hash : NULL)
173
174
static struct elf32_ia64_dyn_sym_info * get_dyn_sym_info
175
  (struct elf32_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 elf32_ia64_dynamic_symbol_p
179
  (struct elf_link_hash_entry *h, struct bfd_link_info *info, int);
180
static bool elf32_ia64_choose_gp
181
  (bfd *abfd, struct bfd_link_info *info, bool final);
182
static void elf32_ia64_dyn_sym_traverse
183
  (struct elf32_ia64_link_hash_table *ia64_info,
184
   bool (*func) (struct elf32_ia64_dyn_sym_info *, void *),
185
   void * info);
186
static bool allocate_global_data_got
187
  (struct elf32_ia64_dyn_sym_info *dyn_i, void * data);
188
static bool allocate_global_fptr_got
189
  (struct elf32_ia64_dyn_sym_info *dyn_i, void * data);
190
static bool allocate_local_got
191
  (struct elf32_ia64_dyn_sym_info *dyn_i, void * data);
192
static bool elf32_ia64_hpux_vec
193
  (const bfd_target *vec);
194
static bool allocate_dynrel_entries
195
  (struct elf32_ia64_dyn_sym_info *dyn_i, void * data);
196
static asection *get_pltoff
197
  (bfd *abfd, struct bfd_link_info *info,
198
   struct elf32_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
elf32_ia64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
206
        arelent *bfd_reloc,
207
        Elf_Internal_Rela *elf_reloc)
208
0
{
209
0
  unsigned int r_type = ELF32_R_TYPE (elf_reloc->r_info);
210
211
0
  bfd_reloc->howto = ia64_elf_lookup_howto (r_type);
212
0
  if (bfd_reloc->howto == NULL)
213
0
    {
214
      /* xgettext:c-format */
215
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
216
0
        abfd, r_type);
217
0
      bfd_set_error (bfd_error_bad_value);
218
0
      return false;
219
0
    }
220
221
0
  return true;
222
0
}
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_elf32_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
elf32_ia64_update_short_info (asection *sec, bfd_vma offset,
299
            struct elf32_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
elf32_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 elf32_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 = elf32_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 = ELF32_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 elf32_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 (ELF32_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 + ELF32_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 = ELF32_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 (elf32_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
        = ELF32_R_INFO (ELF32_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
    = ELF32_R_INFO (ELF32_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 = ELF32_R_INFO (ELF32_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 = ELF32_R_INFO (ELF32_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 = ELF32_R_INFO (ELF32_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 = ELF32_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 (!elf32_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
      elf32_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 = ELF32_R_INFO (ELF32_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
        elf32_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 = ELF32_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 elf32_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
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
849
0
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
850
0
      elf32_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 (Elf32_External_Rela);
861
0
    data.only_got = true;
862
0
    elf32_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 (elf32_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
elf32_ia64_section_from_shdr (bfd *abfd,
909
            Elf_Internal_Shdr *hdr,
910
            const char *name,
911
            int shindex)
912
0
{
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
0
  switch (hdr->sh_type)
919
0
    {
920
0
    case SHT_IA_64_UNWIND:
921
0
    case SHT_IA_64_HP_OPT_ANOT:
922
0
      break;
923
924
0
    case SHT_IA_64_EXT:
925
0
      if (strcmp (name, ELF_STRING_ia64_archext) != 0)
926
0
  return false;
927
0
      break;
928
929
0
    default:
930
0
      return false;
931
0
    }
932
933
0
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
934
0
    return false;
935
936
0
  return true;
937
0
}
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
elf32_ia64_section_flags (const Elf_Internal_Shdr *hdr)
946
0
{
947
0
  if (hdr->sh_flags & SHF_IA_64_SHORT)
948
0
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
949
950
0
  return true;
951
0
}
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
elf32_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 elf32_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 ELF32 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 ELF32 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 (elf32_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
elf32_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
elf32_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
elf32_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
elf32_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
elf32_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
elf32_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
elf32_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
elf32_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 elf32_ia64_link_hash_entry *ret;
1262
0
  ret = (struct elf32_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 elf32_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
elf32_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 elf32_ia64_link_hash_entry *dir, *ind;
1290
1291
0
  dir = (struct elf32_ia64_link_hash_entry *) xdir;
1292
0
  ind = (struct elf32_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 elf32_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
elf32_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 elf32_ia64_link_hash_entry *h;
1353
0
  struct elf32_ia64_dyn_sym_info *dyn_i;
1354
0
  unsigned int count;
1355
1356
0
  h = (struct elf32_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
elf32_ia64_local_htab_hash (const void *ptr)
1373
0
{
1374
0
  struct elf32_ia64_local_hash_entry *entry
1375
0
    = (struct elf32_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
elf32_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
1384
0
{
1385
0
  struct elf32_ia64_local_hash_entry *entry1
1386
0
    = (struct elf32_ia64_local_hash_entry *) ptr1;
1387
0
  struct elf32_ia64_local_hash_entry *entry2
1388
0
    = (struct elf32_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 elf32_ia64_dyn_sym_info array.  */
1394
1395
static bool
1396
elf32_ia64_global_dyn_info_free (struct elf_link_hash_entry *xentry,
1397
         void *unused ATTRIBUTE_UNUSED)
1398
0
{
1399
0
  struct elf32_ia64_link_hash_entry *entry
1400
0
    = (struct elf32_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 elf32_ia64_dyn_sym_info array.  */
1412
1413
static int
1414
elf32_ia64_local_dyn_info_free (void **slot,
1415
        void * unused ATTRIBUTE_UNUSED)
1416
0
{
1417
0
  struct elf32_ia64_local_hash_entry *entry
1418
0
    = (struct elf32_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
elf32_ia64_link_hash_table_free (bfd *obfd)
1433
0
{
1434
0
  struct elf32_ia64_link_hash_table *ia64_info
1435
0
    = (struct elf32_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
         elf32_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
        elf32_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
elf32_ia64_hash_table_create (bfd *abfd)
1455
0
{
1456
0
  struct elf32_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
              elf32_ia64_new_elf_hash_entry,
1464
0
              sizeof (struct elf32_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, elf32_ia64_local_htab_hash,
1472
0
           elf32_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
      elf32_ia64_link_hash_table_free (abfd);
1477
0
      return NULL;
1478
0
    }
1479
0
  ret->root.root.hash_table_free = elf32_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 elf32_ia64_dyn_sym_traverse_data
1488
{
1489
  bool (*func) (struct elf32_ia64_dyn_sym_info *, void *);
1490
  void * data;
1491
};
1492
1493
static bool
1494
elf32_ia64_global_dyn_sym_thunk (struct elf_link_hash_entry *xentry,
1495
         void * xdata)
1496
0
{
1497
0
  struct elf32_ia64_link_hash_entry *entry
1498
0
    = (struct elf32_ia64_link_hash_entry *) xentry;
1499
0
  struct elf32_ia64_dyn_sym_traverse_data *data
1500
0
    = (struct elf32_ia64_dyn_sym_traverse_data *) xdata;
1501
0
  struct elf32_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
elf32_ia64_local_dyn_sym_thunk (void **slot, void * xdata)
1514
0
{
1515
0
  struct elf32_ia64_local_hash_entry *entry
1516
0
    = (struct elf32_ia64_local_hash_entry *) *slot;
1517
0
  struct elf32_ia64_dyn_sym_traverse_data *data
1518
0
    = (struct elf32_ia64_dyn_sym_traverse_data *) xdata;
1519
0
  struct elf32_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
elf32_ia64_dyn_sym_traverse (struct elf32_ia64_link_hash_table *ia64_info,
1532
           bool (*func) (struct elf32_ia64_dyn_sym_info *,
1533
             void *),
1534
           void * data)
1535
0
{
1536
0
  struct elf32_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
        elf32_ia64_global_dyn_sym_thunk, &xdata);
1543
0
  htab_traverse (ia64_info->loc_hash_table,
1544
0
     elf32_ia64_local_dyn_sym_thunk, &xdata);
1545
0
}
1546

1547
static bool
1548
elf32_ia64_create_dynamic_sections (bfd *abfd,
1549
            struct bfd_link_info *info)
1550
0
{
1551
0
  struct elf32_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 = elf32_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 elf32_ia64_local_hash_entry *
1588
get_local_sym_hash (struct elf32_ia64_link_hash_table *ia64_info,
1589
        bfd *abfd, const Elf_Internal_Rela *rel,
1590
        bool create)
1591
0
{
1592
0
  struct elf32_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
               ELF32_R_SYM (rel->r_info));
1596
0
  void **slot;
1597
1598
0
  e.id = sec->id;
1599
0
  e.r_sym = ELF32_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 elf32_ia64_local_hash_entry *) *slot;
1608
1609
0
  ret = (struct elf32_ia64_local_hash_entry *)
1610
0
  objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
1611
0
      sizeof (struct elf32_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 = ELF32_R_SYM (rel->r_info);
1617
0
      *slot = ret;
1618
0
    }
1619
0
  return ret;
1620
0
}
1621
1622
/* Used to sort elf32_ia64_dyn_sym_info array.  */
1623
1624
static int
1625
addend_compare (const void *xp, const void *yp)
1626
0
{
1627
0
  const struct elf32_ia64_dyn_sym_info *x
1628
0
    = (const struct elf32_ia64_dyn_sym_info *) xp;
1629
0
  const struct elf32_ia64_dyn_sym_info *y
1630
0
    = (const struct elf32_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 elf32_ia64_dyn_sym_info array and remove duplicates.  */
1636
1637
static unsigned int
1638
sort_dyn_sym_info (struct elf32_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 elf32_ia64_dyn_sym_info *
1818
get_dyn_sym_info (struct elf32_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 elf32_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 elf32_ia64_link_hash_entry *global_h;
1831
1832
0
      global_h = (struct elf32_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 elf32_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 elf32_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 elf32_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 elf32_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 elf32_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 elf32_ia64_dyn_sym_info *dyn_i,
2122
     asection *srel, int type, bool reltext)
2123
0
{
2124
0
  struct elf32_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 elf32_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
elf32_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2151
       asection *sec,
2152
       const Elf_Internal_Rela *relocs)
2153
0
{
2154
0
  struct elf32_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 = elf32_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 = ELF32_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 (ELF32_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 elf32_ia64_dyn_sym_info *dyn_i;
2360
0
      int dynrel_type = R_IA64_NONE;
2361
2362
0
      r_symndx = ELF32_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 (ELF32_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_DTPREL32LSB;
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_FPTR32LSB;
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_DIR32LSB;
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_PCREL32LSB;
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 elf32_ia64_dyn_sym_info *dyn_i,
2613
        void * data)
2614
0
{
2615
0
  struct elf32_ia64_allocate_data *x = (struct elf32_ia64_allocate_data *)data;
2616
2617
0
  if ((dyn_i->want_got || dyn_i->want_gotx)
2618
0
      && ! dyn_i->want_fptr
2619
0
      && elf32_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 (elf32_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 elf32_ia64_link_hash_table *ia64_info;
2639
2640
0
    ia64_info = elf32_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 elf32_ia64_dyn_sym_info *dyn_i,
2664
        void * data)
2665
0
{
2666
0
  struct elf32_ia64_allocate_data *x = (struct elf32_ia64_allocate_data *)data;
2667
2668
0
  if (dyn_i->want_got
2669
0
      && dyn_i->want_fptr
2670
0
      && elf32_ia64_dynamic_symbol_p (dyn_i->h, x->info, R_IA64_FPTR32LSB))
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 elf32_ia64_dyn_sym_info *dyn_i,
2682
        void * data)
2683
0
{
2684
0
  struct elf32_ia64_allocate_data *x = (struct elf32_ia64_allocate_data *)data;
2685
2686
0
  if ((dyn_i->want_got || dyn_i->want_gotx)
2687
0
      && !elf32_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 elf32_ia64_dyn_sym_info *dyn_i, void * data)
2718
0
{
2719
0
  struct elf32_ia64_allocate_data *x = (struct elf32_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 elf32_ia64_dyn_sym_info *dyn_i,
2764
          void * data)
2765
0
{
2766
0
  struct elf32_ia64_allocate_data *x = (struct elf32_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 (elf32_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 elf32_ia64_dyn_sym_info *dyn_i,
2801
           void * data)
2802
0
{
2803
0
  struct elf32_ia64_allocate_data *x = (struct elf32_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 elf32_ia64_dyn_sym_info *dyn_i,
2828
       void * data)
2829
0
{
2830
0
  struct elf32_ia64_allocate_data *x = (struct elf32_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 elf32_ia64_dyn_sym_info *dyn_i,
2845
       void * data)
2846
0
{
2847
0
  struct elf32_ia64_allocate_data *x = (struct elf32_ia64_allocate_data *)data;
2848
0
  struct elf32_ia64_link_hash_table *ia64_info;
2849
0
  struct elf32_ia64_dyn_reloc_entry *rent;
2850
0
  bool dynamic_symbol, shared, resolved_zero;
2851
2852
0
  ia64_info = elf32_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 = elf32_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 (Elf32_External_Rela);
2878
0
    }
2879
0
  if ((dynamic_symbol || shared) && dyn_i->want_tprel)
2880
0
    ia64_info->root.srelgot->size += sizeof (Elf32_External_Rela);
2881
0
  if (dynamic_symbol && dyn_i->want_dtpmod)
2882
0
    ia64_info->root.srelgot->size += sizeof (Elf32_External_Rela);
2883
0
  if (dynamic_symbol && dyn_i->want_dtprel)
2884
0
    ia64_info->root.srelgot->size += sizeof (Elf32_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 (Elf32_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 (Elf32_External_Rela);
2904
0
      else if (shared)
2905
0
  t = 2 * sizeof (Elf32_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 (Elf32_External_Rela) * count;
2956
0
    }
2957
2958
0
  return true;
2959
0
}
2960
2961
static bool
2962
elf32_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
elf32_ia64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2992
          struct bfd_link_info *info)
2993
0
{
2994
0
  struct elf32_ia64_allocate_data data;
2995
0
  struct elf32_ia64_link_hash_table *ia64_info;
2996
0
  asection *sec;
2997
0
  bfd *dynobj;
2998
2999
0
  ia64_info = elf32_ia64_hash_table (info);
3000
0
  if (ia64_info == NULL)
3001
0
    return false;
3002
0
  dynobj = ia64_info->root.dynobj;
3003
0
  ia64_info->self_dtpmod_offset = (bfd_vma) -1;
3004
0
  BFD_ASSERT(dynobj != NULL);
3005
0
  data.info = info;
3006
3007
  /* Set the contents of the .interp section to the interpreter.  */
3008
0
  if (ia64_info->root.dynamic_sections_created
3009
0
      && bfd_link_executable (info) && !info->nointerp)
3010
0
    {
3011
0
      sec = bfd_get_linker_section (dynobj, ".interp");
3012
0
      BFD_ASSERT (sec != NULL);
3013
0
      sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
3014
0
      sec->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
3015
0
    }
3016
3017
  /* Allocate the GOT entries.  */
3018
3019
0
  if (ia64_info->root.sgot)
3020
0
    {
3021
0
      data.ofs = 0;
3022
0
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
3023
0
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
3024
0
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
3025
0
      ia64_info->root.sgot->size = data.ofs;
3026
0
    }
3027
3028
  /* Allocate the FPTR entries.  */
3029
3030
0
  if (ia64_info->fptr_sec)
3031
0
    {
3032
0
      data.ofs = 0;
3033
0
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
3034
0
      ia64_info->fptr_sec->size = data.ofs;
3035
0
    }
3036
3037
  /* Now that we've seen all of the input files, we can decide which
3038
     symbols need plt entries.  Allocate the minimal PLT entries first.
3039
     We do this even though dynamic_sections_created may be FALSE, because
3040
     this has the side-effect of clearing want_plt and want_plt2.  */
3041
3042
0
  data.ofs = 0;
3043
0
  elf32_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
3044
3045
0
  ia64_info->minplt_entries = 0;
3046
0
  if (data.ofs)
3047
0
    {
3048
0
      ia64_info->minplt_entries
3049
0
  = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
3050
0
    }
3051
3052
  /* Align the pointer for the plt2 entries.  */
3053
0
  data.ofs = (data.ofs + 31) & (bfd_vma) -32;
3054
3055
0
  elf32_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
3056
0
  if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
3057
0
    {
3058
      /* FIXME: we always reserve the memory for dynamic linker even if
3059
   there are no PLT entries since dynamic linker may assume the
3060
   reserved memory always exists.  */
3061
3062
0
      BFD_ASSERT (ia64_info->root.dynamic_sections_created);
3063
3064
0
      ia64_info->root.splt->size = data.ofs;
3065
3066
      /* If we've got a .plt, we need some extra memory for the dynamic
3067
   linker.  We stuff these in .got.plt.  */
3068
0
      ia64_info->root.sgotplt->size = 8 * PLT_RESERVED_WORDS;
3069
0
    }
3070
3071
  /* Allocate the PLTOFF entries.  */
3072
3073
0
  if (ia64_info->pltoff_sec)
3074
0
    {
3075
0
      data.ofs = 0;
3076
0
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
3077
0
      ia64_info->pltoff_sec->size = data.ofs;
3078
0
    }
3079
3080
0
  if (ia64_info->root.dynamic_sections_created)
3081
0
    {
3082
      /* Allocate space for the dynamic relocations that turned out to be
3083
   required.  */
3084
3085
0
      if (bfd_link_pic (info) && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
3086
0
  ia64_info->root.srelgot->size += sizeof (Elf32_External_Rela);
3087
0
      data.only_got = false;
3088
0
      elf32_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
3089
0
    }
3090
3091
  /* We have now determined the sizes of the various dynamic sections.
3092
     Allocate memory for them.  */
3093
0
  for (sec = dynobj->sections; sec != NULL; sec = sec->next)
3094
0
    {
3095
0
      bool strip;
3096
3097
0
      if (!(sec->flags & SEC_LINKER_CREATED))
3098
0
  continue;
3099
3100
      /* If we don't need this section, strip it from the output file.
3101
   There were several sections primarily related to dynamic
3102
   linking that must be create before the linker maps input
3103
   sections to output sections.  The linker does that before
3104
   bfd_elf_size_dynamic_sections is called, and it is that
3105
   function which decides whether anything needs to go into
3106
   these sections.  */
3107
3108
0
      strip = (sec->size == 0);
3109
3110
0
      if (sec == ia64_info->root.sgot)
3111
0
  strip = false;
3112
0
      else if (sec == ia64_info->root.srelgot)
3113
0
  {
3114
0
    if (strip)
3115
0
      ia64_info->root.srelgot = NULL;
3116
0
    else
3117
      /* We use the reloc_count field as a counter if we need to
3118
         copy relocs into the output file.  */
3119
0
      sec->reloc_count = 0;
3120
0
  }
3121
0
      else if (sec == ia64_info->fptr_sec)
3122
0
  {
3123
0
    if (strip)
3124
0
      ia64_info->fptr_sec = NULL;
3125
0
  }
3126
0
      else if (sec == ia64_info->rel_fptr_sec)
3127
0
  {
3128
0
    if (strip)
3129
0
      ia64_info->rel_fptr_sec = NULL;
3130
0
    else
3131
      /* We use the reloc_count field as a counter if we need to
3132
         copy relocs into the output file.  */
3133
0
      sec->reloc_count = 0;
3134
0
  }
3135
0
      else if (sec == ia64_info->root.splt)
3136
0
  {
3137
0
    if (strip)
3138
0
      ia64_info->root.splt = NULL;
3139
0
  }
3140
0
      else if (sec == ia64_info->pltoff_sec)
3141
0
  {
3142
0
    if (strip)
3143
0
      ia64_info->pltoff_sec = NULL;
3144
0
  }
3145
0
      else if (sec == ia64_info->rel_pltoff_sec)
3146
0
  {
3147
0
    if (strip)
3148
0
      ia64_info->rel_pltoff_sec = NULL;
3149
0
    else
3150
0
      {
3151
0
        ia64_info->root.dt_jmprel_required = true;
3152
        /* We use the reloc_count field as a counter if we need to
3153
     copy relocs into the output file.  */
3154
0
        sec->reloc_count = 0;
3155
0
      }
3156
0
  }
3157
0
      else
3158
0
  {
3159
0
    const char *name;
3160
3161
    /* It's OK to base decisions on the section name, because none
3162
       of the dynobj section names depend upon the input files.  */
3163
0
    name = bfd_section_name (sec);
3164
3165
0
    if (strcmp (name, ".got.plt") == 0)
3166
0
      strip = false;
3167
0
    else if (startswith (name, ".rel"))
3168
0
      {
3169
0
        if (!strip)
3170
0
    {
3171
      /* We use the reloc_count field as a counter if we need to
3172
         copy relocs into the output file.  */
3173
0
      sec->reloc_count = 0;
3174
0
    }
3175
0
      }
3176
0
    else
3177
0
      continue;
3178
0
  }
3179
3180
0
      if (strip)
3181
0
  sec->flags |= SEC_EXCLUDE;
3182
0
      else
3183
0
  {
3184
    /* Allocate memory for the section contents.  */
3185
0
    sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
3186
0
    if (sec->contents == NULL && sec->size != 0)
3187
0
      return false;
3188
0
  }
3189
0
    }
3190
3191
0
  if (ia64_info->root.dynamic_sections_created)
3192
0
    {
3193
      /* Add some entries to the .dynamic section.  We fill in the values
3194
   later (in finish_dynamic_sections) but we must add the entries now
3195
   so that we get the correct size for the .dynamic section.  */
3196
3197
0
#define add_dynamic_entry(TAG, VAL) \
3198
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3199
3200
0
      if (!_bfd_elf_add_dynamic_tags (output_bfd, info, true))
3201
0
  return false;
3202
3203
0
      if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
3204
0
  return false;
3205
0
    }
3206
3207
  /* ??? Perhaps force __gp local.  */
3208
3209
0
  return true;
3210
0
}
3211
3212
static void
3213
elf32_ia64_install_dyn_reloc (bfd *abfd, struct bfd_link_info *info,
3214
            asection *sec, asection *srel,
3215
            bfd_vma offset, unsigned int type,
3216
            long dynindx, bfd_vma addend)
3217
0
{
3218
0
  Elf_Internal_Rela outrel;
3219
0
  bfd_byte *loc;
3220
3221
0
  BFD_ASSERT (dynindx != -1);
3222
0
  outrel.r_info = ELF32_R_INFO (dynindx, type);
3223
0
  outrel.r_addend = addend;
3224
0
  outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
3225
0
  if (outrel.r_offset >= (bfd_vma) -2)
3226
0
    {
3227
      /* Run for the hills.  We shouldn't be outputting a relocation
3228
   for this.  So do what everyone else does and output a no-op.  */
3229
0
      outrel.r_info = ELF32_R_INFO (0, R_IA64_NONE);
3230
0
      outrel.r_addend = 0;
3231
0
      outrel.r_offset = 0;
3232
0
    }
3233
0
  else
3234
0
    outrel.r_offset += sec->output_section->vma + sec->output_offset;
3235
3236
0
  loc = srel->contents;
3237
0
  loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
3238
0
  bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
3239
0
  BFD_ASSERT (sizeof (Elf32_External_Rela) * srel->reloc_count <= srel->size);
3240
0
}
3241
3242
/* Store an entry for target address TARGET_ADDR in the linkage table
3243
   and return the gp-relative address of the linkage table entry.  */
3244
3245
static bfd_vma
3246
set_got_entry (bfd *abfd, struct bfd_link_info *info,
3247
         struct elf32_ia64_dyn_sym_info *dyn_i,
3248
         long dynindx, bfd_vma addend, bfd_vma value,
3249
         unsigned int dyn_r_type)
3250
0
{
3251
0
  struct elf32_ia64_link_hash_table *ia64_info;
3252
0
  asection *got_sec;
3253
0
  bool done;
3254
0
  bfd_vma got_offset;
3255
3256
0
  ia64_info = elf32_ia64_hash_table (info);
3257
0
  if (ia64_info == NULL)
3258
0
    return 0;
3259
3260
0
  got_sec = ia64_info->root.sgot;
3261
3262
0
  switch (dyn_r_type)
3263
0
    {
3264
0
    case R_IA64_TPREL64LSB:
3265
0
      done = dyn_i->tprel_done;
3266
0
      dyn_i->tprel_done = true;
3267
0
      got_offset = dyn_i->tprel_offset;
3268
0
      break;
3269
0
    case R_IA64_DTPMOD64LSB:
3270
0
      if (dyn_i->dtpmod_offset != ia64_info->self_dtpmod_offset)
3271
0
  {
3272
0
    done = dyn_i->dtpmod_done;
3273
0
    dyn_i->dtpmod_done = true;
3274
0
  }
3275
0
      else
3276
0
  {
3277
0
    done = ia64_info->self_dtpmod_done;
3278
0
    ia64_info->self_dtpmod_done = true;
3279
0
    dynindx = 0;
3280
0
  }
3281
0
      got_offset = dyn_i->dtpmod_offset;
3282
0
      break;
3283
0
    case R_IA64_DTPREL32LSB:
3284
0
    case R_IA64_DTPREL64LSB:
3285
0
      done = dyn_i->dtprel_done;
3286
0
      dyn_i->dtprel_done = true;
3287
0
      got_offset = dyn_i->dtprel_offset;
3288
0
      break;
3289
0
    default:
3290
0
      done = dyn_i->got_done;
3291
0
      dyn_i->got_done = true;
3292
0
      got_offset = dyn_i->got_offset;
3293
0
      break;
3294
0
    }
3295
3296
0
  BFD_ASSERT ((got_offset & 7) == 0);
3297
3298
0
  if (! done)
3299
0
    {
3300
      /* Store the target address in the linkage table entry.  */
3301
0
      bfd_put_64 (abfd, value, got_sec->contents + got_offset);
3302
3303
      /* Install a dynamic relocation if needed.  */
3304
0
      if (((bfd_link_pic (info)
3305
0
      && (!dyn_i->h
3306
0
    || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3307
0
    || dyn_i->h->root.type != bfd_link_hash_undefweak)
3308
0
      && dyn_r_type != R_IA64_DTPREL32LSB
3309
0
      && dyn_r_type != R_IA64_DTPREL64LSB)
3310
0
     || elf32_ia64_dynamic_symbol_p (dyn_i->h, info, dyn_r_type)
3311
0
     || (dynindx != -1
3312
0
         && (dyn_r_type == R_IA64_FPTR32LSB
3313
0
       || dyn_r_type == R_IA64_FPTR64LSB)))
3314
0
    && (!dyn_i->want_ltoff_fptr
3315
0
        || !bfd_link_pie (info)
3316
0
        || !dyn_i->h
3317
0
        || dyn_i->h->root.type != bfd_link_hash_undefweak))
3318
0
  {
3319
0
    if (dynindx == -1
3320
0
        && dyn_r_type != R_IA64_TPREL64LSB
3321
0
        && dyn_r_type != R_IA64_DTPMOD64LSB
3322
0
        && dyn_r_type != R_IA64_DTPREL32LSB
3323
0
        && dyn_r_type != R_IA64_DTPREL64LSB)
3324
0
      {
3325
0
        dyn_r_type = R_IA64_REL32LSB;
3326
0
        dynindx = 0;
3327
0
        addend = value;
3328
0
      }
3329
3330
0
    if (bfd_big_endian (abfd))
3331
0
      {
3332
0
        switch (dyn_r_type)
3333
0
    {
3334
0
    case R_IA64_REL32LSB:
3335
0
      dyn_r_type = R_IA64_REL32MSB;
3336
0
      break;
3337
0
    case R_IA64_DIR32LSB:
3338
0
      dyn_r_type = R_IA64_DIR32MSB;
3339
0
      break;
3340
0
    case R_IA64_FPTR32LSB:
3341
0
      dyn_r_type = R_IA64_FPTR32MSB;
3342
0
      break;
3343
0
    case R_IA64_DTPREL32LSB:
3344
0
      dyn_r_type = R_IA64_DTPREL32MSB;
3345
0
      break;
3346
0
    case R_IA64_REL64LSB:
3347
0
      dyn_r_type = R_IA64_REL64MSB;
3348
0
      break;
3349
0
    case R_IA64_DIR64LSB:
3350
0
      dyn_r_type = R_IA64_DIR64MSB;
3351
0
      break;
3352
0
    case R_IA64_FPTR64LSB:
3353
0
      dyn_r_type = R_IA64_FPTR64MSB;
3354
0
      break;
3355
0
    case R_IA64_TPREL64LSB:
3356
0
      dyn_r_type = R_IA64_TPREL64MSB;
3357
0
      break;
3358
0
    case R_IA64_DTPMOD64LSB:
3359
0
      dyn_r_type = R_IA64_DTPMOD64MSB;
3360
0
      break;
3361
0
    case R_IA64_DTPREL64LSB:
3362
0
      dyn_r_type = R_IA64_DTPREL64MSB;
3363
0
      break;
3364
0
    default:
3365
0
      BFD_ASSERT (false);
3366
0
      break;
3367
0
    }
3368
0
      }
3369
3370
0
    elf32_ia64_install_dyn_reloc (abfd, NULL, got_sec,
3371
0
          ia64_info->root.srelgot,
3372
0
          got_offset, dyn_r_type,
3373
0
          dynindx, addend);
3374
0
  }
3375
0
    }
3376
3377
  /* Return the address of the linkage table entry.  */
3378
0
  value = (got_sec->output_section->vma
3379
0
     + got_sec->output_offset
3380
0
     + got_offset);
3381
3382
0
  return value;
3383
0
}
3384
3385
/* Fill in a function descriptor consisting of the function's code
3386
   address and its global pointer.  Return the descriptor's address.  */
3387
3388
static bfd_vma
3389
set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
3390
    struct elf32_ia64_dyn_sym_info *dyn_i,
3391
    bfd_vma value)
3392
0
{
3393
0
  struct elf32_ia64_link_hash_table *ia64_info;
3394
0
  asection *fptr_sec;
3395
3396
0
  ia64_info = elf32_ia64_hash_table (info);
3397
0
  if (ia64_info == NULL)
3398
0
    return 0;
3399
3400
0
  fptr_sec = ia64_info->fptr_sec;
3401
3402
0
  if (!dyn_i->fptr_done)
3403
0
    {
3404
0
      dyn_i->fptr_done = 1;
3405
3406
      /* Fill in the function descriptor.  */
3407
0
      bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
3408
0
      bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
3409
0
      fptr_sec->contents + dyn_i->fptr_offset + 8);
3410
0
      if (ia64_info->rel_fptr_sec)
3411
0
  {
3412
0
    Elf_Internal_Rela outrel;
3413
0
    bfd_byte *loc;
3414
3415
0
    if (bfd_little_endian (abfd))
3416
0
      outrel.r_info = ELF32_R_INFO (0, R_IA64_IPLTLSB);
3417
0
    else
3418
0
      outrel.r_info = ELF32_R_INFO (0, R_IA64_IPLTMSB);
3419
0
    outrel.r_addend = value;
3420
0
    outrel.r_offset = (fptr_sec->output_section->vma
3421
0
           + fptr_sec->output_offset
3422
0
           + dyn_i->fptr_offset);
3423
0
    loc = ia64_info->rel_fptr_sec->contents;
3424
0
    loc += ia64_info->rel_fptr_sec->reloc_count++
3425
0
     * sizeof (Elf32_External_Rela);
3426
0
    bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
3427
0
  }
3428
0
    }
3429
3430
  /* Return the descriptor's address.  */
3431
0
  value = (fptr_sec->output_section->vma
3432
0
     + fptr_sec->output_offset
3433
0
     + dyn_i->fptr_offset);
3434
3435
0
  return value;
3436
0
}
3437
3438
/* Fill in a PLTOFF entry consisting of the function's code address
3439
   and its global pointer.  Return the descriptor's address.  */
3440
3441
static bfd_vma
3442
set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
3443
      struct elf32_ia64_dyn_sym_info *dyn_i,
3444
      bfd_vma value, bool is_plt)
3445
0
{
3446
0
  struct elf32_ia64_link_hash_table *ia64_info;
3447
0
  asection *pltoff_sec;
3448
3449
0
  ia64_info = elf32_ia64_hash_table (info);
3450
0
  if (ia64_info == NULL)
3451
0
    return 0;
3452
3453
0
  pltoff_sec = ia64_info->pltoff_sec;
3454
3455
  /* Don't do anything if this symbol uses a real PLT entry.  In
3456
     that case, we'll fill this in during finish_dynamic_symbol.  */
3457
0
  if ((! dyn_i->want_plt || is_plt)
3458
0
      && !dyn_i->pltoff_done)
3459
0
    {
3460
0
      bfd_vma gp = _bfd_get_gp_value (abfd);
3461
3462
      /* Fill in the function descriptor.  */
3463
0
      bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
3464
0
      bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
3465
3466
      /* Install dynamic relocations if needed.  */
3467
0
      if (!is_plt
3468
0
    && bfd_link_pic (info)
3469
0
    && (!dyn_i->h
3470
0
        || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
3471
0
        || dyn_i->h->root.type != bfd_link_hash_undefweak))
3472
0
  {
3473
0
    unsigned int dyn_r_type;
3474
3475
0
    if (bfd_big_endian (abfd))
3476
0
      dyn_r_type = R_IA64_REL32MSB;
3477
0
    else
3478
0
      dyn_r_type = R_IA64_REL32LSB;
3479
3480
0
    elf32_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
3481
0
          ia64_info->rel_pltoff_sec,
3482
0
          dyn_i->pltoff_offset,
3483
0
          dyn_r_type, 0, value);
3484
0
    elf32_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
3485
0
          ia64_info->rel_pltoff_sec,
3486
0
          dyn_i->pltoff_offset + ARCH_SIZE / 8,
3487
0
          dyn_r_type, 0, gp);
3488
0
  }
3489
3490
0
      dyn_i->pltoff_done = 1;
3491
0
    }
3492
3493
  /* Return the descriptor's address.  */
3494
0
  value = (pltoff_sec->output_section->vma
3495
0
     + pltoff_sec->output_offset
3496
0
     + dyn_i->pltoff_offset);
3497
3498
0
  return value;
3499
0
}
3500
3501
/* Return the base VMA address which should be subtracted from real addresses
3502
   when resolving @tprel() relocation.
3503
   Main program TLS (whose template starts at PT_TLS p_vaddr)
3504
   is assigned offset round(2 * size of pointer, PT_TLS p_align).  */
3505
3506
static bfd_vma
3507
elf32_ia64_tprel_base (struct bfd_link_info *info)
3508
0
{
3509
0
  asection *tls_sec = elf_hash_table (info)->tls_sec;
3510
0
  return tls_sec->vma - align_power ((bfd_vma) ARCH_SIZE / 4,
3511
0
             tls_sec->alignment_power);
3512
0
}
3513
3514
/* Return the base VMA address which should be subtracted from real addresses
3515
   when resolving @dtprel() relocation.
3516
   This is PT_TLS segment p_vaddr.  */
3517
3518
static bfd_vma
3519
elf32_ia64_dtprel_base (struct bfd_link_info *info)
3520
0
{
3521
0
  return elf_hash_table (info)->tls_sec->vma;
3522
0
}
3523
3524
/* Called through qsort to sort the .IA_64.unwind section during a
3525
   non-relocatable link.  Set elf32_ia64_unwind_entry_compare_bfd
3526
   to the output bfd so we can do proper endianness frobbing.  */
3527
3528
static bfd *elf32_ia64_unwind_entry_compare_bfd;
3529
3530
static int
3531
elf32_ia64_unwind_entry_compare (const void * a, const void * b)
3532
0
{
3533
0
  bfd_vma av, bv;
3534
3535
0
  av = bfd_get_64 (elf32_ia64_unwind_entry_compare_bfd, a);
3536
0
  bv = bfd_get_64 (elf32_ia64_unwind_entry_compare_bfd, b);
3537
3538
0
  return (av < bv ? -1 : av > bv ? 1 : 0);
3539
0
}
3540
3541
/* Make sure we've got ourselves a nice fat __gp value.  */
3542
static bool
3543
elf32_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info, bool final)
3544
0
{
3545
0
  bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
3546
0
  bfd_vma min_short_vma = min_vma, max_short_vma = 0;
3547
0
  struct elf_link_hash_entry *gp;
3548
0
  bfd_vma gp_val;
3549
0
  asection *os;
3550
0
  struct elf32_ia64_link_hash_table *ia64_info;
3551
3552
0
  ia64_info = elf32_ia64_hash_table (info);
3553
0
  if (ia64_info == NULL)
3554
0
    return false;
3555
3556
  /* Find the min and max vma of all sections marked short.  Also collect
3557
     min and max vma of any type, for use in selecting a nice gp.  */
3558
0
  for (os = abfd->sections; os ; os = os->next)
3559
0
    {
3560
0
      bfd_vma lo, hi;
3561
3562
0
      if ((os->flags & SEC_ALLOC) == 0)
3563
0
  continue;
3564
3565
0
      lo = os->vma;
3566
      /* When this function is called from elf32_ia64_final_link
3567
   the correct value to use is os->size.  When called from
3568
   elf32_ia64_relax_section we are in the middle of section
3569
   sizing; some sections will already have os->size set, others
3570
   will have os->size zero and os->rawsize the previous size.  */
3571
0
      hi = os->vma + (!final && os->rawsize ? os->rawsize : os->size);
3572
0
      if (hi < lo)
3573
0
  hi = (bfd_vma) -1;
3574
3575
0
      if (min_vma > lo)
3576
0
  min_vma = lo;
3577
0
      if (max_vma < hi)
3578
0
  max_vma = hi;
3579
0
      if (os->flags & SEC_SMALL_DATA)
3580
0
  {
3581
0
    if (min_short_vma > lo)
3582
0
      min_short_vma = lo;
3583
0
    if (max_short_vma < hi)
3584
0
      max_short_vma = hi;
3585
0
  }
3586
0
    }
3587
3588
0
  if (ia64_info->min_short_sec)
3589
0
    {
3590
0
      if (min_short_vma
3591
0
    > (ia64_info->min_short_sec->vma
3592
0
       + ia64_info->min_short_offset))
3593
0
  min_short_vma = (ia64_info->min_short_sec->vma
3594
0
       + ia64_info->min_short_offset);
3595
0
      if (max_short_vma
3596
0
    < (ia64_info->max_short_sec->vma
3597
0
       + ia64_info->max_short_offset))
3598
0
  max_short_vma = (ia64_info->max_short_sec->vma
3599
0
       + ia64_info->max_short_offset);
3600
0
    }
3601
3602
  /* See if the user wants to force a value.  */
3603
0
  gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3604
0
           false, false);
3605
3606
0
  if (gp
3607
0
      && (gp->root.type == bfd_link_hash_defined
3608
0
    || gp->root.type == bfd_link_hash_defweak))
3609
0
    {
3610
0
      asection *gp_sec = gp->root.u.def.section;
3611
0
      gp_val = (gp->root.u.def.value
3612
0
    + gp_sec->output_section->vma
3613
0
    + gp_sec->output_offset);
3614
0
    }
3615
0
  else
3616
0
    {
3617
      /* Pick a sensible value.  */
3618
3619
0
      if (ia64_info->min_short_sec)
3620
0
  {
3621
0
    bfd_vma short_range = max_short_vma - min_short_vma;
3622
3623
    /* If min_short_sec is set, pick one in the middle bewteen
3624
       min_short_vma and max_short_vma.  */
3625
0
    if (short_range >= 0x400000)
3626
0
      goto overflow;
3627
0
    gp_val = min_short_vma + short_range / 2;
3628
0
  }
3629
0
      else
3630
0
  {
3631
0
    asection *got_sec = ia64_info->root.sgot;
3632
3633
    /* Start with just the address of the .got.  */
3634
0
    if (got_sec)
3635
0
      gp_val = got_sec->output_section->vma;
3636
0
    else if (max_short_vma != 0)
3637
0
      gp_val = min_short_vma;
3638
0
    else if (max_vma - min_vma < 0x200000)
3639
0
      gp_val = min_vma;
3640
0
    else
3641
0
      gp_val = max_vma - 0x200000 + 8;
3642
0
  }
3643
3644
      /* If it is possible to address the entire image, but we
3645
   don't with the choice above, adjust.  */
3646
0
      if (max_vma - min_vma < 0x400000
3647
0
    && (max_vma - gp_val >= 0x200000
3648
0
        || gp_val - min_vma > 0x200000))
3649
0
  gp_val = min_vma + 0x200000;
3650
0
      else if (max_short_vma != 0)
3651
0
  {
3652
    /* If we don't cover all the short data, adjust.  */
3653
0
    if (max_short_vma - gp_val >= 0x200000)
3654
0
      gp_val = min_short_vma + 0x200000;
3655
3656
    /* If we're addressing stuff past the end, adjust back.  */
3657
0
    if (gp_val > max_vma)
3658
0
      gp_val = max_vma - 0x200000 + 8;
3659
0
  }
3660
0
    }
3661
3662
  /* Validate whether all SHF_IA_64_SHORT sections are within
3663
     range of the chosen GP.  */
3664
3665
0
  if (max_short_vma != 0)
3666
0
    {
3667
0
      if (max_short_vma - min_short_vma >= 0x400000)
3668
0
  {
3669
0
  overflow:
3670
0
    _bfd_error_handler
3671
      /* xgettext:c-format */
3672
0
      (_("%pB: short data segment overflowed (%#" PRIx64 " >= 0x400000)"),
3673
0
       abfd, (uint64_t) (max_short_vma - min_short_vma));
3674
0
    return false;
3675
0
  }
3676
0
      else if ((gp_val > min_short_vma
3677
0
    && gp_val - min_short_vma > 0x200000)
3678
0
         || (gp_val < max_short_vma
3679
0
       && max_short_vma - gp_val >= 0x200000))
3680
0
  {
3681
0
    _bfd_error_handler
3682
0
      (_("%pB: __gp does not cover short data segment"), abfd);
3683
0
    return false;
3684
0
  }
3685
0
    }
3686
3687
0
  _bfd_set_gp_value (abfd, gp_val);
3688
3689
0
  return true;
3690
0
}
3691
3692
static bool
3693
elf32_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
3694
0
{
3695
0
  struct elf32_ia64_link_hash_table *ia64_info;
3696
0
  asection *unwind_output_sec;
3697
3698
0
  ia64_info = elf32_ia64_hash_table (info);
3699
0
  if (ia64_info == NULL)
3700
0
    return false;
3701
3702
  /* Make sure we've got ourselves a nice fat __gp value.  */
3703
0
  if (!bfd_link_relocatable (info))
3704
0
    {
3705
0
      bfd_vma gp_val;
3706
0
      struct elf_link_hash_entry *gp;
3707
3708
      /* We assume after gp is set, section size will only decrease. We
3709
   need to adjust gp for it.  */
3710
0
      _bfd_set_gp_value (abfd, 0);
3711
0
      if (! elf32_ia64_choose_gp (abfd, info, true))
3712
0
  return false;
3713
0
      gp_val = _bfd_get_gp_value (abfd);
3714
3715
0
      gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
3716
0
         false, false);
3717
0
      if (gp)
3718
0
  {
3719
0
    gp->root.type = bfd_link_hash_defined;
3720
0
    gp->root.u.def.value = gp_val;
3721
0
    gp->root.u.def.section = bfd_abs_section_ptr;
3722
0
  }
3723
0
    }
3724
3725
  /* If we're producing a final executable, we need to sort the contents
3726
     of the .IA_64.unwind section.  Force this section to be relocated
3727
     into memory rather than written immediately to the output file.  */
3728
0
  unwind_output_sec = NULL;
3729
0
  if (!bfd_link_relocatable (info))
3730
0
    {
3731
0
      asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
3732
0
      if (s)
3733
0
  {
3734
0
    unwind_output_sec = s->output_section;
3735
0
    unwind_output_sec->contents
3736
0
      = bfd_malloc (unwind_output_sec->size);
3737
0
    if (unwind_output_sec->contents == NULL)
3738
0
      return false;
3739
0
  }
3740
0
    }
3741
3742
  /* Invoke the regular ELF backend linker to do all the work.  */
3743
0
  if (!bfd_elf_final_link (abfd, info))
3744
0
    return false;
3745
3746
0
  if (unwind_output_sec)
3747
0
    {
3748
0
      elf32_ia64_unwind_entry_compare_bfd = abfd;
3749
0
      qsort (unwind_output_sec->contents,
3750
0
       (size_t) (unwind_output_sec->size / 24),
3751
0
       24,
3752
0
       elf32_ia64_unwind_entry_compare);
3753
3754
0
      if (! bfd_set_section_contents (abfd, unwind_output_sec,
3755
0
              unwind_output_sec->contents, (bfd_vma) 0,
3756
0
              unwind_output_sec->size))
3757
0
  return false;
3758
0
    }
3759
3760
0
  return true;
3761
0
}
3762
3763
static int
3764
elf32_ia64_relocate_section (bfd *output_bfd,
3765
           struct bfd_link_info *info,
3766
           bfd *input_bfd,
3767
           asection *input_section,
3768
           bfd_byte *contents,
3769
           Elf_Internal_Rela *relocs,
3770
           Elf_Internal_Sym *local_syms,
3771
           asection **local_sections)
3772
0
{
3773
0
  struct elf32_ia64_link_hash_table *ia64_info;
3774
0
  Elf_Internal_Shdr *symtab_hdr;
3775
0
  Elf_Internal_Rela *rel;
3776
0
  Elf_Internal_Rela *relend;
3777
0
  asection *srel;
3778
0
  bool ret_val = true;  /* for non-fatal errors */
3779
0
  bfd_vma gp_val;
3780
3781
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3782
0
  ia64_info = elf32_ia64_hash_table (info);
3783
0
  if (ia64_info == NULL)
3784
0
    return false;
3785
3786
  /* Infect various flags from the input section to the output section.  */
3787
0
  if (bfd_link_relocatable (info))
3788
0
    {
3789
0
      bfd_vma flags;
3790
3791
0
      flags = elf_section_data(input_section)->this_hdr.sh_flags;
3792
0
      flags &= SHF_IA_64_NORECOV;
3793
3794
0
      elf_section_data(input_section->output_section)
3795
0
  ->this_hdr.sh_flags |= flags;
3796
0
    }
3797
3798
0
  gp_val = _bfd_get_gp_value (output_bfd);
3799
0
  srel = get_reloc_section (input_bfd, ia64_info, input_section, false);
3800
3801
0
  rel = relocs;
3802
0
  relend = relocs + input_section->reloc_count;
3803
0
  for (; rel < relend; ++rel)
3804
0
    {
3805
0
      struct elf_link_hash_entry *h;
3806
0
      struct elf32_ia64_dyn_sym_info *dyn_i;
3807
0
      bfd_reloc_status_type r;
3808
0
      reloc_howto_type *howto;
3809
0
      unsigned long r_symndx;
3810
0
      Elf_Internal_Sym *sym;
3811
0
      unsigned int r_type;
3812
0
      bfd_vma value;
3813
0
      asection *sym_sec;
3814
0
      bfd_byte *hit_addr;
3815
0
      bool dynamic_symbol_p;
3816
0
      bool undef_weak_ref;
3817
3818
0
      r_type = ELF32_R_TYPE (rel->r_info);
3819
0
      if (r_type > R_IA64_MAX_RELOC_CODE)
3820
0
  {
3821
    /* xgettext:c-format */
3822
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3823
0
            input_bfd, (int) r_type);
3824
0
    bfd_set_error (bfd_error_bad_value);
3825
0
    ret_val = false;
3826
0
    continue;
3827
0
  }
3828
3829
0
      howto = ia64_elf_lookup_howto (r_type);
3830
0
      if (howto == NULL)
3831
0
  {
3832
0
    ret_val = false;
3833
0
    continue;
3834
0
  }
3835
3836
0
      r_symndx = ELF32_R_SYM (rel->r_info);
3837
0
      h = NULL;
3838
0
      sym = NULL;
3839
0
      sym_sec = NULL;
3840
0
      undef_weak_ref = false;
3841
3842
0
      if (r_symndx < symtab_hdr->sh_info)
3843
0
  {
3844
    /* Reloc against local symbol.  */
3845
0
    asection *msec;
3846
0
    sym = local_syms + r_symndx;
3847
0
    sym_sec = local_sections[r_symndx];
3848
0
    msec = sym_sec;
3849
0
    value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
3850
0
    if (!bfd_link_relocatable (info)
3851
0
        && (sym_sec->flags & SEC_MERGE) != 0
3852
0
        && ELF_ST_TYPE (sym->st_info) == STT_SECTION
3853
0
        && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3854
0
      {
3855
0
        struct elf32_ia64_local_hash_entry *loc_h;
3856
3857
0
        loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, false);
3858
0
        if (loc_h && ! loc_h->sec_merge_done)
3859
0
    {
3860
0
      struct elf32_ia64_dyn_sym_info *dynent;
3861
0
      unsigned int count;
3862
3863
0
      for (count = loc_h->count, dynent = loc_h->info;
3864
0
           count != 0;
3865
0
           count--, dynent++)
3866
0
        {
3867
0
          msec = sym_sec;
3868
0
          dynent->addend =
3869
0
      _bfd_merged_section_offset (output_bfd, &msec,
3870
0
                elf_section_data (msec)->
3871
0
                sec_info,
3872
0
                sym->st_value
3873
0
                + dynent->addend);
3874
0
          dynent->addend -= sym->st_value;
3875
0
          dynent->addend += msec->output_section->vma
3876
0
          + msec->output_offset
3877
0
          - sym_sec->output_section->vma
3878
0
          - sym_sec->output_offset;
3879
0
        }
3880
3881
      /* We may have introduced duplicated entries. We need
3882
         to remove them properly.  */
3883
0
      count = sort_dyn_sym_info (loc_h->info, loc_h->count);
3884
0
      if (count != loc_h->count)
3885
0
        {
3886
0
          loc_h->count = count;
3887
0
          loc_h->sorted_count = count;
3888
0
        }
3889
3890
0
      loc_h->sec_merge_done = 1;
3891
0
    }
3892
0
      }
3893
0
  }
3894
0
      else
3895
0
  {
3896
0
    bool unresolved_reloc;
3897
0
    bool warned, ignored;
3898
0
    struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3899
3900
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3901
0
           r_symndx, symtab_hdr, sym_hashes,
3902
0
           h, sym_sec, value,
3903
0
           unresolved_reloc, warned, ignored);
3904
3905
0
    if (h->root.type == bfd_link_hash_undefweak)
3906
0
      undef_weak_ref = true;
3907
0
    else if (warned || (ignored && bfd_link_executable (info)))
3908
0
      continue;
3909
0
  }
3910
3911
0
      if (sym_sec != NULL && discarded_section (sym_sec))
3912
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3913
0
           rel, 1, relend, howto, 0, contents);
3914
3915
0
      if (bfd_link_relocatable (info))
3916
0
  continue;
3917
3918
0
      hit_addr = contents + rel->r_offset;
3919
0
      value += rel->r_addend;
3920
0
      dynamic_symbol_p = elf32_ia64_dynamic_symbol_p (h, info, r_type);
3921
3922
0
      switch (r_type)
3923
0
  {
3924
0
  case R_IA64_NONE:
3925
0
  case R_IA64_LDXMOV:
3926
0
    continue;
3927
3928
0
  case R_IA64_IMM14:
3929
0
  case R_IA64_IMM22:
3930
0
  case R_IA64_IMM64:
3931
0
  case R_IA64_DIR32MSB:
3932
0
  case R_IA64_DIR32LSB:
3933
0
  case R_IA64_DIR64MSB:
3934
0
  case R_IA64_DIR64LSB:
3935
    /* Install a dynamic relocation for this reloc.  */
3936
0
    if ((dynamic_symbol_p || bfd_link_pic (info))
3937
0
        && r_symndx != STN_UNDEF
3938
0
        && (input_section->flags & SEC_ALLOC) != 0)
3939
0
      {
3940
0
        unsigned int dyn_r_type;
3941
0
        long dynindx;
3942
0
        bfd_vma addend;
3943
3944
0
        BFD_ASSERT (srel != NULL);
3945
3946
0
        switch (r_type)
3947
0
    {
3948
0
    case R_IA64_IMM14:
3949
0
    case R_IA64_IMM22:
3950
0
    case R_IA64_IMM64:
3951
      /* ??? People shouldn't be doing non-pic code in
3952
         shared libraries nor dynamic executables.  */
3953
0
      _bfd_error_handler
3954
        /* xgettext:c-format */
3955
0
        (_("%pB: non-pic code with imm relocation against dynamic symbol `%s'"),
3956
0
         input_bfd,
3957
0
         h ? h->root.root.string
3958
0
           : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3959
0
             sym_sec));
3960
0
      ret_val = false;
3961
0
      continue;
3962
3963
0
    default:
3964
0
      break;
3965
0
    }
3966
3967
        /* If we don't need dynamic symbol lookup, find a
3968
     matching RELATIVE relocation.  */
3969
0
        dyn_r_type = r_type;
3970
0
        if (dynamic_symbol_p)
3971
0
    {
3972
0
      dynindx = h->dynindx;
3973
0
      addend = rel->r_addend;
3974
0
      value = 0;
3975
0
    }
3976
0
        else
3977
0
    {
3978
0
      switch (r_type)
3979
0
        {
3980
0
        case R_IA64_DIR32MSB:
3981
0
          dyn_r_type = R_IA64_REL32MSB;
3982
0
          break;
3983
0
        case R_IA64_DIR32LSB:
3984
0
          dyn_r_type = R_IA64_REL32LSB;
3985
0
          break;
3986
0
        case R_IA64_DIR64MSB:
3987
0
          dyn_r_type = R_IA64_REL64MSB;
3988
0
          break;
3989
0
        case R_IA64_DIR64LSB:
3990
0
          dyn_r_type = R_IA64_REL64LSB;
3991
0
          break;
3992
3993
0
        default:
3994
0
          break;
3995
0
        }
3996
0
      dynindx = 0;
3997
0
      addend = value;
3998
0
    }
3999
4000
0
        elf32_ia64_install_dyn_reloc (output_bfd, info, input_section,
4001
0
              srel, rel->r_offset, dyn_r_type,
4002
0
              dynindx, addend);
4003
0
      }
4004
    /* Fall through.  */
4005
4006
0
  case R_IA64_LTV32MSB:
4007
0
  case R_IA64_LTV32LSB:
4008
0
  case R_IA64_LTV64MSB:
4009
0
  case R_IA64_LTV64LSB:
4010
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4011
0
    break;
4012
4013
0
  case R_IA64_GPREL22:
4014
0
  case R_IA64_GPREL64I:
4015
0
  case R_IA64_GPREL32MSB:
4016
0
  case R_IA64_GPREL32LSB:
4017
0
  case R_IA64_GPREL64MSB:
4018
0
  case R_IA64_GPREL64LSB:
4019
0
    if (dynamic_symbol_p)
4020
0
      {
4021
0
        _bfd_error_handler
4022
    /* xgettext:c-format */
4023
0
    (_("%pB: @gprel relocation against dynamic symbol %s"),
4024
0
     input_bfd,
4025
0
     h ? h->root.root.string
4026
0
       : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4027
0
               sym_sec));
4028
0
        ret_val = false;
4029
0
        continue;
4030
0
      }
4031
0
    value -= gp_val;
4032
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4033
0
    break;
4034
4035
0
  case R_IA64_LTOFF22:
4036
0
  case R_IA64_LTOFF22X:
4037
0
  case R_IA64_LTOFF64I:
4038
0
    dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
4039
0
    value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
4040
0
         rel->r_addend, value, R_IA64_DIR32LSB);
4041
0
    value -= gp_val;
4042
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4043
0
    break;
4044
4045
0
  case R_IA64_PLTOFF22:
4046
0
  case R_IA64_PLTOFF64I:
4047
0
  case R_IA64_PLTOFF64MSB:
4048
0
  case R_IA64_PLTOFF64LSB:
4049
0
    dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
4050
0
    value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
4051
0
    value -= gp_val;
4052
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4053
0
    break;
4054
4055
0
  case R_IA64_FPTR64I:
4056
0
  case R_IA64_FPTR32MSB:
4057
0
  case R_IA64_FPTR32LSB:
4058
0
  case R_IA64_FPTR64MSB:
4059
0
  case R_IA64_FPTR64LSB:
4060
0
    dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
4061
0
    if (dyn_i->want_fptr)
4062
0
      {
4063
0
        if (!undef_weak_ref)
4064
0
    value = set_fptr_entry (output_bfd, info, dyn_i, value);
4065
0
      }
4066
0
    if (!dyn_i->want_fptr || bfd_link_pie (info))
4067
0
      {
4068
0
        long dynindx;
4069
0
        unsigned int dyn_r_type = r_type;
4070
0
        bfd_vma addend = rel->r_addend;
4071
4072
        /* Otherwise, we expect the dynamic linker to create
4073
     the entry.  */
4074
4075
0
        if (dyn_i->want_fptr)
4076
0
    {
4077
0
      if (r_type == R_IA64_FPTR64I)
4078
0
        {
4079
          /* We can't represent this without a dynamic symbol.
4080
       Adjust the relocation to be against an output
4081
       section symbol, which are always present in the
4082
       dynamic symbol table.  */
4083
          /* ??? People shouldn't be doing non-pic code in
4084
       shared libraries.  Hork.  */
4085
0
          _bfd_error_handler
4086
0
      (_("%pB: linking non-pic code in a position independent executable"),
4087
0
       input_bfd);
4088
0
          ret_val = false;
4089
0
          continue;
4090
0
        }
4091
0
      dynindx = 0;
4092
0
      addend = value;
4093
0
      dyn_r_type = r_type + R_IA64_REL32LSB - R_IA64_FPTR32LSB;
4094
0
    }
4095
0
        else if (h)
4096
0
    {
4097
0
      if (h->dynindx != -1)
4098
0
        dynindx = h->dynindx;
4099
0
      else
4100
0
        dynindx = (_bfd_elf_link_lookup_local_dynindx
4101
0
             (info, h->root.u.def.section->owner,
4102
0
        global_sym_index (h)));
4103
0
      value = 0;
4104
0
    }
4105
0
        else
4106
0
    {
4107
0
      dynindx = (_bfd_elf_link_lookup_local_dynindx
4108
0
           (info, input_bfd, (long) r_symndx));
4109
0
      value = 0;
4110
0
    }
4111
4112
0
        elf32_ia64_install_dyn_reloc (output_bfd, info, input_section,
4113
0
              srel, rel->r_offset, dyn_r_type,
4114
0
              dynindx, addend);
4115
0
      }
4116
4117
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4118
0
    break;
4119
4120
0
  case R_IA64_LTOFF_FPTR22:
4121
0
  case R_IA64_LTOFF_FPTR64I:
4122
0
  case R_IA64_LTOFF_FPTR32MSB:
4123
0
  case R_IA64_LTOFF_FPTR32LSB:
4124
0
  case R_IA64_LTOFF_FPTR64MSB:
4125
0
  case R_IA64_LTOFF_FPTR64LSB:
4126
0
    {
4127
0
      long dynindx;
4128
4129
0
      dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
4130
0
      if (dyn_i->want_fptr)
4131
0
        {
4132
0
    BFD_ASSERT (h == NULL || h->dynindx == -1);
4133
0
    if (!undef_weak_ref)
4134
0
      value = set_fptr_entry (output_bfd, info, dyn_i, value);
4135
0
    dynindx = -1;
4136
0
        }
4137
0
      else
4138
0
        {
4139
    /* Otherwise, we expect the dynamic linker to create
4140
       the entry.  */
4141
0
    if (h)
4142
0
      {
4143
0
        if (h->dynindx != -1)
4144
0
          dynindx = h->dynindx;
4145
0
        else
4146
0
          dynindx = (_bfd_elf_link_lookup_local_dynindx
4147
0
         (info, h->root.u.def.section->owner,
4148
0
          global_sym_index (h)));
4149
0
      }
4150
0
    else
4151
0
      dynindx = (_bfd_elf_link_lookup_local_dynindx
4152
0
           (info, input_bfd, (long) r_symndx));
4153
0
    value = 0;
4154
0
        }
4155
4156
0
      value = set_got_entry (output_bfd, info, dyn_i, dynindx,
4157
0
           rel->r_addend, value, R_IA64_FPTR32LSB);
4158
0
      value -= gp_val;
4159
0
      r = ia64_elf_install_value (hit_addr, value, r_type);
4160
0
    }
4161
0
    break;
4162
4163
0
  case R_IA64_PCREL32MSB:
4164
0
  case R_IA64_PCREL32LSB:
4165
0
  case R_IA64_PCREL64MSB:
4166
0
  case R_IA64_PCREL64LSB:
4167
    /* Install a dynamic relocation for this reloc.  */
4168
0
    if (dynamic_symbol_p && r_symndx != STN_UNDEF)
4169
0
      {
4170
0
        BFD_ASSERT (srel != NULL);
4171
4172
0
        elf32_ia64_install_dyn_reloc (output_bfd, info, input_section,
4173
0
              srel, rel->r_offset, r_type,
4174
0
              h->dynindx, rel->r_addend);
4175
0
      }
4176
0
    goto finish_pcrel;
4177
4178
0
  case R_IA64_PCREL21B:
4179
0
  case R_IA64_PCREL60B:
4180
    /* We should have created a PLT entry for any dynamic symbol.  */
4181
0
    dyn_i = NULL;
4182
0
    if (h)
4183
0
      dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
4184
4185
0
    if (dyn_i && dyn_i->want_plt2)
4186
0
      {
4187
        /* Should have caught this earlier.  */
4188
0
        BFD_ASSERT (rel->r_addend == 0);
4189
4190
0
        value = (ia64_info->root.splt->output_section->vma
4191
0
           + ia64_info->root.splt->output_offset
4192
0
           + dyn_i->plt2_offset);
4193
0
      }
4194
0
    else
4195
0
      {
4196
        /* Since there's no PLT entry, Validate that this is
4197
     locally defined.  */
4198
0
        BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
4199
4200
        /* If the symbol is undef_weak, we shouldn't be trying
4201
     to call it.  There's every chance that we'd wind up
4202
     with an out-of-range fixup here.  Don't bother setting
4203
     any value at all.  */
4204
0
        if (undef_weak_ref)
4205
0
    continue;
4206
0
      }
4207
0
    goto finish_pcrel;
4208
4209
0
  case R_IA64_PCREL21BI:
4210
0
  case R_IA64_PCREL21F:
4211
0
  case R_IA64_PCREL21M:
4212
0
  case R_IA64_PCREL22:
4213
0
  case R_IA64_PCREL64I:
4214
    /* The PCREL21BI reloc is specifically not intended for use with
4215
       dynamic relocs.  PCREL21F and PCREL21M are used for speculation
4216
       fixup code, and thus probably ought not be dynamic.  The
4217
       PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs.  */
4218
0
    if (dynamic_symbol_p)
4219
0
      {
4220
0
        const char *msg;
4221
4222
0
        if (r_type == R_IA64_PCREL21BI)
4223
    /* xgettext:c-format */
4224
0
    msg = _("%pB: @internal branch to dynamic symbol %s");
4225
0
        else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
4226
    /* xgettext:c-format */
4227
0
    msg = _("%pB: speculation fixup to dynamic symbol %s");
4228
0
        else
4229
    /* xgettext:c-format */
4230
0
    msg = _("%pB: @pcrel relocation against dynamic symbol %s");
4231
0
        _bfd_error_handler (msg, input_bfd,
4232
0
          h ? h->root.root.string
4233
0
          : bfd_elf_sym_name (input_bfd,
4234
0
                  symtab_hdr,
4235
0
                  sym,
4236
0
                  sym_sec));
4237
0
        ret_val = false;
4238
0
        continue;
4239
0
      }
4240
0
    goto finish_pcrel;
4241
4242
0
  finish_pcrel:
4243
    /* Make pc-relative.  */
4244
0
    value -= (input_section->output_section->vma
4245
0
        + input_section->output_offset
4246
0
        + rel->r_offset) & ~ (bfd_vma) 0x3;
4247
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4248
0
    break;
4249
4250
0
  case R_IA64_SEGREL32MSB:
4251
0
  case R_IA64_SEGREL32LSB:
4252
0
  case R_IA64_SEGREL64MSB:
4253
0
  case R_IA64_SEGREL64LSB:
4254
0
      {
4255
        /* Find the segment that contains the output_section.  */
4256
0
        Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
4257
0
    (output_bfd, input_section->output_section);
4258
4259
0
        if (p == NULL)
4260
0
    {
4261
0
      r = bfd_reloc_notsupported;
4262
0
    }
4263
0
        else
4264
0
    {
4265
      /* The VMA of the segment is the vaddr of the associated
4266
         program header.  */
4267
0
      if (value > p->p_vaddr)
4268
0
        value -= p->p_vaddr;
4269
0
      else
4270
0
        value = 0;
4271
0
      r = ia64_elf_install_value (hit_addr, value, r_type);
4272
0
    }
4273
0
        break;
4274
0
      }
4275
4276
0
  case R_IA64_SECREL32MSB:
4277
0
  case R_IA64_SECREL32LSB:
4278
0
  case R_IA64_SECREL64MSB:
4279
0
  case R_IA64_SECREL64LSB:
4280
    /* Make output-section relative to section where the symbol
4281
       is defined. PR 475  */
4282
0
    if (sym_sec)
4283
0
      value -= sym_sec->output_section->vma;
4284
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4285
0
    break;
4286
4287
0
  case R_IA64_IPLTMSB:
4288
0
  case R_IA64_IPLTLSB:
4289
    /* Install a dynamic relocation for this reloc.  */
4290
0
    if ((dynamic_symbol_p || bfd_link_pic (info))
4291
0
        && (input_section->flags & SEC_ALLOC) != 0)
4292
0
      {
4293
0
        BFD_ASSERT (srel != NULL);
4294
4295
        /* If we don't need dynamic symbol lookup, install two
4296
     RELATIVE relocations.  */
4297
0
        if (!dynamic_symbol_p)
4298
0
    {
4299
0
      unsigned int dyn_r_type;
4300
4301
0
      if (r_type == R_IA64_IPLTMSB)
4302
0
        dyn_r_type = R_IA64_REL64MSB;
4303
0
      else
4304
0
        dyn_r_type = R_IA64_REL64LSB;
4305
4306
0
      elf32_ia64_install_dyn_reloc (output_bfd, info,
4307
0
            input_section,
4308
0
            srel, rel->r_offset,
4309
0
            dyn_r_type, 0, value);
4310
0
      elf32_ia64_install_dyn_reloc (output_bfd, info,
4311
0
            input_section,
4312
0
            srel, rel->r_offset + 8,
4313
0
            dyn_r_type, 0, gp_val);
4314
0
    }
4315
0
        else
4316
0
    elf32_ia64_install_dyn_reloc (output_bfd, info, input_section,
4317
0
                srel, rel->r_offset, r_type,
4318
0
                h->dynindx, rel->r_addend);
4319
0
      }
4320
4321
0
    if (r_type == R_IA64_IPLTMSB)
4322
0
      r_type = R_IA64_DIR64MSB;
4323
0
    else
4324
0
      r_type = R_IA64_DIR64LSB;
4325
0
    ia64_elf_install_value (hit_addr, value, r_type);
4326
0
    r = ia64_elf_install_value (hit_addr + 8, gp_val, r_type);
4327
0
    break;
4328
4329
0
  case R_IA64_TPREL14:
4330
0
  case R_IA64_TPREL22:
4331
0
  case R_IA64_TPREL64I:
4332
0
    if (elf_hash_table (info)->tls_sec == NULL)
4333
0
      goto missing_tls_sec;
4334
0
    value -= elf32_ia64_tprel_base (info);
4335
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4336
0
    break;
4337
4338
0
  case R_IA64_DTPREL14:
4339
0
  case R_IA64_DTPREL22:
4340
0
  case R_IA64_DTPREL64I:
4341
0
  case R_IA64_DTPREL32LSB:
4342
0
  case R_IA64_DTPREL32MSB:
4343
0
  case R_IA64_DTPREL64LSB:
4344
0
  case R_IA64_DTPREL64MSB:
4345
0
    if (elf_hash_table (info)->tls_sec == NULL)
4346
0
      goto missing_tls_sec;
4347
0
    value -= elf32_ia64_dtprel_base (info);
4348
0
    r = ia64_elf_install_value (hit_addr, value, r_type);
4349
0
    break;
4350
4351
0
  case R_IA64_LTOFF_TPREL22:
4352
0
  case R_IA64_LTOFF_DTPMOD22:
4353
0
  case R_IA64_LTOFF_DTPREL22:
4354
0
    {
4355
0
      int got_r_type;
4356
0
      long dynindx = h ? h->dynindx : -1;
4357
0
      bfd_vma r_addend = rel->r_addend;
4358
4359
0
      switch (r_type)
4360
0
        {
4361
0
        default:
4362
0
        case R_IA64_LTOFF_TPREL22:
4363
0
    if (!dynamic_symbol_p)
4364
0
      {
4365
0
        if (elf_hash_table (info)->tls_sec == NULL)
4366
0
          goto missing_tls_sec;
4367
0
        if (!bfd_link_pic (info))
4368
0
          value -= elf32_ia64_tprel_base (info);
4369
0
        else
4370
0
          {
4371
0
      r_addend += value - elf32_ia64_dtprel_base (info);
4372
0
      dynindx = 0;
4373
0
          }
4374
0
      }
4375
0
    got_r_type = R_IA64_TPREL64LSB;
4376
0
    break;
4377
0
        case R_IA64_LTOFF_DTPMOD22:
4378
0
    if (!dynamic_symbol_p && !bfd_link_pic (info))
4379
0
      value = 1;
4380
0
    got_r_type = R_IA64_DTPMOD64LSB;
4381
0
    break;
4382
0
        case R_IA64_LTOFF_DTPREL22:
4383
0
    if (!dynamic_symbol_p)
4384
0
      {
4385
0
        if (elf_hash_table (info)->tls_sec == NULL)
4386
0
          goto missing_tls_sec;
4387
0
        value -= elf32_ia64_dtprel_base (info);
4388
0
      }
4389
0
    got_r_type = R_IA64_DTPREL32LSB;
4390
0
    break;
4391
0
        }
4392
0
      dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
4393
0
      value = set_got_entry (input_bfd, info, dyn_i, dynindx, r_addend,
4394
0
           value, got_r_type);
4395
0
      value -= gp_val;
4396
0
      r = ia64_elf_install_value (hit_addr, value, r_type);
4397
0
    }
4398
0
    break;
4399
4400
0
  default:
4401
0
    r = bfd_reloc_notsupported;
4402
0
    break;
4403
0
  }
4404
4405
0
      switch (r)
4406
0
  {
4407
0
  case bfd_reloc_ok:
4408
0
    break;
4409
4410
0
  case bfd_reloc_undefined:
4411
    /* This can happen for global table relative relocs if
4412
       __gp is undefined.  This is a panic situation so we
4413
       don't try to continue.  */
4414
0
    (*info->callbacks->undefined_symbol)
4415
0
      (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
4416
0
    return false;
4417
4418
0
  case bfd_reloc_notsupported:
4419
0
    {
4420
0
      const char *name;
4421
4422
0
      if (h)
4423
0
        name = h->root.root.string;
4424
0
      else
4425
0
        name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4426
0
               sym_sec);
4427
0
      (*info->callbacks->warning) (info, _("unsupported reloc"),
4428
0
           name, input_bfd,
4429
0
           input_section, rel->r_offset);
4430
0
      ret_val = false;
4431
0
    }
4432
0
    break;
4433
4434
0
  case bfd_reloc_dangerous:
4435
0
  case bfd_reloc_outofrange:
4436
0
  case bfd_reloc_overflow:
4437
0
  default:
4438
0
  missing_tls_sec:
4439
0
    {
4440
0
      const char *name;
4441
4442
0
      if (h)
4443
0
        name = h->root.root.string;
4444
0
      else
4445
0
        name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4446
0
               sym_sec);
4447
4448
0
      switch (r_type)
4449
0
        {
4450
0
        case R_IA64_TPREL14:
4451
0
        case R_IA64_TPREL22:
4452
0
        case R_IA64_TPREL64I:
4453
0
        case R_IA64_DTPREL14:
4454
0
        case R_IA64_DTPREL22:
4455
0
        case R_IA64_DTPREL64I:
4456
0
        case R_IA64_DTPREL32LSB:
4457
0
        case R_IA64_DTPREL32MSB:
4458
0
        case R_IA64_DTPREL64LSB:
4459
0
        case R_IA64_DTPREL64MSB:
4460
0
        case R_IA64_LTOFF_TPREL22:
4461
0
        case R_IA64_LTOFF_DTPMOD22:
4462
0
        case R_IA64_LTOFF_DTPREL22:
4463
0
    _bfd_error_handler
4464
      /* xgettext:c-format */
4465
0
      (_("%pB: missing TLS section for relocation %s against `%s'"
4466
0
         " at %#" PRIx64 " in section `%pA'."),
4467
0
       input_bfd, howto->name, name,
4468
0
       (uint64_t) rel->r_offset, input_section);
4469
0
    break;
4470
4471
0
        case R_IA64_PCREL21B:
4472
0
        case R_IA64_PCREL21BI:
4473
0
        case R_IA64_PCREL21M:
4474
0
        case R_IA64_PCREL21F:
4475
0
    if (is_elf_hash_table (info->hash))
4476
0
      {
4477
        /* Relaxtion is always performed for ELF output.
4478
           Overflow failures for those relocations mean
4479
           that the section is too big to relax.  */
4480
0
        _bfd_error_handler
4481
          /* xgettext:c-format */
4482
0
          (_("%pB: Can't relax br (%s) to `%s' at %#" PRIx64
4483
0
       " in section `%pA' with size %#" PRIx64
4484
0
       " (> 0x1000000)."),
4485
0
           input_bfd, howto->name, name, (uint64_t) rel->r_offset,
4486
0
           input_section, (uint64_t) input_section->size);
4487
0
        break;
4488
0
      }
4489
    /* Fall through.  */
4490
0
        default:
4491
0
    (*info->callbacks->reloc_overflow) (info,
4492
0
                &h->root,
4493
0
                name,
4494
0
                howto->name,
4495
0
                (bfd_vma) 0,
4496
0
                input_bfd,
4497
0
                input_section,
4498
0
                rel->r_offset);
4499
0
    break;
4500
0
        }
4501
4502
0
      ret_val = false;
4503
0
    }
4504
0
    break;
4505
0
  }
4506
0
    }
4507
4508
0
  return ret_val;
4509
0
}
4510
4511
static bool
4512
elf32_ia64_finish_dynamic_symbol (bfd *output_bfd,
4513
          struct bfd_link_info *info,
4514
          struct elf_link_hash_entry *h,
4515
          Elf_Internal_Sym *sym)
4516
0
{
4517
0
  struct elf32_ia64_link_hash_table *ia64_info;
4518
0
  struct elf32_ia64_dyn_sym_info *dyn_i;
4519
4520
0
  ia64_info = elf32_ia64_hash_table (info);
4521
0
  if (ia64_info == NULL)
4522
0
    return false;
4523
4524
0
  dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
4525
4526
  /* Fill in the PLT data, if required.  */
4527
0
  if (dyn_i && dyn_i->want_plt)
4528
0
    {
4529
0
      Elf_Internal_Rela outrel;
4530
0
      bfd_byte *loc;
4531
0
      asection *plt_sec;
4532
0
      bfd_vma plt_addr, pltoff_addr, gp_val, plt_index;
4533
4534
0
      gp_val = _bfd_get_gp_value (output_bfd);
4535
4536
      /* Initialize the minimal PLT entry.  */
4537
4538
0
      plt_index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
4539
0
      plt_sec = ia64_info->root.splt;
4540
0
      loc = plt_sec->contents + dyn_i->plt_offset;
4541
4542
0
      memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
4543
0
      ia64_elf_install_value (loc, plt_index, R_IA64_IMM22);
4544
0
      ia64_elf_install_value (loc+2, -dyn_i->plt_offset, R_IA64_PCREL21B);
4545
4546
0
      plt_addr = (plt_sec->output_section->vma
4547
0
      + plt_sec->output_offset
4548
0
      + dyn_i->plt_offset);
4549
0
      pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
4550
4551
      /* Initialize the FULL PLT entry, if needed.  */
4552
0
      if (dyn_i->want_plt2)
4553
0
  {
4554
0
    loc = plt_sec->contents + dyn_i->plt2_offset;
4555
4556
0
    memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
4557
0
    ia64_elf_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
4558
4559
    /* Mark the symbol as undefined, rather than as defined in the
4560
       plt section.  Leave the value alone.  */
4561
    /* ??? We didn't redefine it in adjust_dynamic_symbol in the
4562
       first place.  But perhaps elflink.c did some for us.  */
4563
0
    if (!h->def_regular)
4564
0
      sym->st_shndx = SHN_UNDEF;
4565
0
  }
4566
4567
      /* Create the dynamic relocation.  */
4568
0
      outrel.r_offset = pltoff_addr;
4569
0
      if (bfd_little_endian (output_bfd))
4570
0
  outrel.r_info = ELF32_R_INFO (h->dynindx, R_IA64_IPLTLSB);
4571
0
      else
4572
0
  outrel.r_info = ELF32_R_INFO (h->dynindx, R_IA64_IPLTMSB);
4573
0
      outrel.r_addend = 0;
4574
4575
      /* This is fun.  In the .IA_64.pltoff section, we've got entries
4576
   that correspond both to real PLT entries, and those that
4577
   happened to resolve to local symbols but need to be created
4578
   to satisfy @pltoff relocations.  The .rela.IA_64.pltoff
4579
   relocations for the real PLT should come at the end of the
4580
   section, so that they can be indexed by plt entry at runtime.
4581
4582
   We emitted all of the relocations for the non-PLT @pltoff
4583
   entries during relocate_section.  So we can consider the
4584
   existing sec->reloc_count to be the base of the array of
4585
   PLT relocations.  */
4586
4587
0
      loc = ia64_info->rel_pltoff_sec->contents;
4588
0
      loc += ((ia64_info->rel_pltoff_sec->reloc_count + plt_index)
4589
0
        * sizeof (Elf32_External_Rela));
4590
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4591
0
    }
4592
4593
  /* Mark some specially defined symbols as absolute.  */
4594
0
  if (h == ia64_info->root.hdynamic
4595
0
      || h == ia64_info->root.hgot
4596
0
      || h == ia64_info->root.hplt)
4597
0
    sym->st_shndx = SHN_ABS;
4598
4599
0
  return true;
4600
0
}
4601
4602
static bool
4603
elf32_ia64_finish_dynamic_sections (bfd *abfd,
4604
            struct bfd_link_info *info)
4605
0
{
4606
0
  struct elf32_ia64_link_hash_table *ia64_info;
4607
0
  bfd *dynobj;
4608
4609
0
  ia64_info = elf32_ia64_hash_table (info);
4610
0
  if (ia64_info == NULL)
4611
0
    return false;
4612
4613
0
  dynobj = ia64_info->root.dynobj;
4614
4615
0
  if (ia64_info->root.dynamic_sections_created)
4616
0
    {
4617
0
      Elf32_External_Dyn *dyncon, *dynconend;
4618
0
      asection *sdyn, *sgotplt;
4619
0
      bfd_vma gp_val;
4620
4621
0
      sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4622
0
      sgotplt = ia64_info->root.sgotplt;
4623
0
      BFD_ASSERT (sdyn != NULL);
4624
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
4625
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4626
4627
0
      gp_val = _bfd_get_gp_value (abfd);
4628
4629
0
      for (; dyncon < dynconend; dyncon++)
4630
0
  {
4631
0
    Elf_Internal_Dyn dyn;
4632
4633
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4634
4635
0
    switch (dyn.d_tag)
4636
0
      {
4637
0
      case DT_PLTGOT:
4638
0
        dyn.d_un.d_ptr = gp_val;
4639
0
        break;
4640
4641
0
      case DT_PLTRELSZ:
4642
0
        dyn.d_un.d_val = (ia64_info->minplt_entries
4643
0
        * sizeof (Elf32_External_Rela));
4644
0
        break;
4645
4646
0
      case DT_JMPREL:
4647
        /* See the comment above in finish_dynamic_symbol.  */
4648
0
        dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
4649
0
        + ia64_info->rel_pltoff_sec->output_offset
4650
0
        + (ia64_info->rel_pltoff_sec->reloc_count
4651
0
           * sizeof (Elf32_External_Rela)));
4652
0
        break;
4653
4654
0
      case DT_IA_64_PLT_RESERVE:
4655
0
        dyn.d_un.d_ptr = (sgotplt->output_section->vma
4656
0
        + sgotplt->output_offset);
4657
0
        break;
4658
0
      }
4659
4660
0
    bfd_elf32_swap_dyn_out (abfd, &dyn, dyncon);
4661
0
  }
4662
4663
      /* Initialize the PLT0 entry.  */
4664
0
      if (ia64_info->root.splt)
4665
0
  {
4666
0
    bfd_byte *loc = ia64_info->root.splt->contents;
4667
0
    bfd_vma pltres;
4668
4669
0
    memcpy (loc, plt_header, PLT_HEADER_SIZE);
4670
4671
0
    pltres = (sgotplt->output_section->vma
4672
0
        + sgotplt->output_offset
4673
0
        - gp_val);
4674
4675
0
    ia64_elf_install_value (loc+1, pltres, R_IA64_GPREL22);
4676
0
  }
4677
0
    }
4678
4679
0
  return true;
4680
0
}
4681

4682
/* ELF file flag handling:  */
4683
4684
/* Function to keep IA-64 specific file flags.  */
4685
static bool
4686
elf32_ia64_set_private_flags (bfd *abfd, flagword flags)
4687
0
{
4688
0
  BFD_ASSERT (!elf_flags_init (abfd)
4689
0
        || elf_elfheader (abfd)->e_flags == flags);
4690
4691
0
  elf_elfheader (abfd)->e_flags = flags;
4692
0
  elf_flags_init (abfd) = true;
4693
0
  return true;
4694
0
}
4695
4696
/* Merge backend specific data from an object file to the output
4697
   object file when linking.  */
4698
4699
static bool
4700
elf32_ia64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4701
0
{
4702
0
  bfd *obfd = info->output_bfd;
4703
0
  flagword out_flags;
4704
0
  flagword in_flags;
4705
0
  bool ok = true;
4706
4707
  /* FIXME: What should be checked when linking shared libraries?  */
4708
0
  if ((ibfd->flags & DYNAMIC) != 0)
4709
0
    return true;
4710
4711
0
  if (!is_ia64_elf (ibfd) || !is_ia64_elf (obfd))
4712
0
    return true;
4713
4714
0
  in_flags  = elf_elfheader (ibfd)->e_flags;
4715
0
  out_flags = elf_elfheader (obfd)->e_flags;
4716
4717
0
  if (! elf_flags_init (obfd))
4718
0
    {
4719
0
      elf_flags_init (obfd) = true;
4720
0
      elf_elfheader (obfd)->e_flags = in_flags;
4721
4722
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4723
0
    && bfd_get_arch_info (obfd)->the_default)
4724
0
  {
4725
0
    return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4726
0
            bfd_get_mach (ibfd));
4727
0
  }
4728
4729
0
      return true;
4730
0
    }
4731
4732
  /* Check flag compatibility.  */
4733
0
  if (in_flags == out_flags)
4734
0
    return true;
4735
4736
  /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set.  */
4737
0
  if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
4738
0
    elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
4739
4740
0
  if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
4741
0
    {
4742
0
      _bfd_error_handler
4743
0
  (_("%pB: linking trap-on-NULL-dereference with non-trapping files"),
4744
0
   ibfd);
4745
4746
0
      bfd_set_error (bfd_error_bad_value);
4747
0
      ok = false;
4748
0
    }
4749
0
  if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
4750
0
    {
4751
0
      _bfd_error_handler
4752
0
  (_("%pB: linking big-endian files with little-endian files"),
4753
0
   ibfd);
4754
4755
0
      bfd_set_error (bfd_error_bad_value);
4756
0
      ok = false;
4757
0
    }
4758
0
  if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
4759
0
    {
4760
0
      _bfd_error_handler
4761
0
  (_("%pB: linking 64-bit files with 32-bit files"),
4762
0
   ibfd);
4763
4764
0
      bfd_set_error (bfd_error_bad_value);
4765
0
      ok = false;
4766
0
    }
4767
0
  if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
4768
0
    {
4769
0
      _bfd_error_handler
4770
0
  (_("%pB: linking constant-gp files with non-constant-gp files"),
4771
0
   ibfd);
4772
4773
0
      bfd_set_error (bfd_error_bad_value);
4774
0
      ok = false;
4775
0
    }
4776
0
  if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
4777
0
      != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4778
0
    {
4779
0
      _bfd_error_handler
4780
0
  (_("%pB: linking auto-pic files with non-auto-pic files"),
4781
0
   ibfd);
4782
4783
0
      bfd_set_error (bfd_error_bad_value);
4784
0
      ok = false;
4785
0
    }
4786
4787
0
  return ok;
4788
0
}
4789
4790
static bool
4791
elf32_ia64_print_private_bfd_data (bfd *abfd, void * ptr)
4792
0
{
4793
0
  FILE *file = (FILE *) ptr;
4794
0
  flagword flags = elf_elfheader (abfd)->e_flags;
4795
4796
0
  BFD_ASSERT (abfd != NULL && ptr != NULL);
4797
4798
0
  fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
4799
0
     (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
4800
0
     (flags & EF_IA_64_EXT) ? "EXT, " : "",
4801
0
     (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
4802
0
     (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
4803
0
     (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
4804
0
     (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
4805
0
     (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
4806
0
     (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
4807
4808
0
  _bfd_elf_print_private_bfd_data (abfd, ptr);
4809
0
  return true;
4810
0
}
4811
4812
static enum elf_reloc_type_class
4813
elf32_ia64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4814
           const asection *rel_sec ATTRIBUTE_UNUSED,
4815
           const Elf_Internal_Rela *rela)
4816
0
{
4817
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
4818
0
    {
4819
0
    case R_IA64_REL32MSB:
4820
0
    case R_IA64_REL32LSB:
4821
0
    case R_IA64_REL64MSB:
4822
0
    case R_IA64_REL64LSB:
4823
0
      return reloc_class_relative;
4824
0
    case R_IA64_IPLTMSB:
4825
0
    case R_IA64_IPLTLSB:
4826
0
      return reloc_class_plt;
4827
0
    case R_IA64_COPY:
4828
0
      return reloc_class_copy;
4829
0
    default:
4830
0
      return reloc_class_normal;
4831
0
    }
4832
0
}
4833
4834
static const struct bfd_elf_special_section elf32_ia64_special_sections[] =
4835
{
4836
  { STRING_COMMA_LEN (".sbss"),  -1, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4837
  { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
4838
  { NULL,        0,   0, 0,      0 }
4839
};
4840
4841
static bool
4842
elf32_ia64_object_p (bfd *abfd)
4843
0
{
4844
0
  asection *sec;
4845
0
  asection *group, *unwi, *unw;
4846
0
  flagword flags;
4847
0
  const char *name;
4848
0
  char *unwi_name, *unw_name;
4849
0
  size_t amt;
4850
4851
0
  if (abfd->flags & DYNAMIC)
4852
0
    return true;
4853
4854
  /* Flags for fake group section.  */
4855
0
  flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
4856
0
     | SEC_EXCLUDE);
4857
4858
  /* We add a fake section group for each .gnu.linkonce.t.* section,
4859
     which isn't in a section group, and its unwind sections.  */
4860
0
  for (sec = abfd->sections; sec != NULL; sec = sec->next)
4861
0
    {
4862
0
      if (elf_sec_group (sec) == NULL
4863
0
    && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
4864
0
        == (SEC_LINK_ONCE | SEC_CODE))
4865
0
    && startswith (sec->name, ".gnu.linkonce.t."))
4866
0
  {
4867
0
    name = sec->name + 16;
4868
4869
0
    amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
4870
0
    unwi_name = bfd_alloc (abfd, amt);
4871
0
    if (!unwi_name)
4872
0
      return false;
4873
4874
0
    strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
4875
0
    unwi = bfd_get_section_by_name (abfd, unwi_name);
4876
4877
0
    amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
4878
0
    unw_name = bfd_alloc (abfd, amt);
4879
0
    if (!unw_name)
4880
0
      return false;
4881
4882
0
    strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
4883
0
    unw = bfd_get_section_by_name (abfd, unw_name);
4884
4885
    /* We need to create a fake group section for it and its
4886
       unwind sections.  */
4887
0
    group = bfd_make_section_anyway_with_flags (abfd, name,
4888
0
                  flags);
4889
0
    if (group == NULL)
4890
0
      return false;
4891
4892
    /* Move the fake group section to the beginning.  */
4893
0
    bfd_section_list_remove (abfd, group);
4894
0
    bfd_section_list_prepend (abfd, group);
4895
4896
0
    elf_next_in_group (group) = sec;
4897
4898
0
    elf_group_name (sec) = name;
4899
0
    elf_next_in_group (sec) = sec;
4900
0
    elf_sec_group (sec) = group;
4901
4902
0
    if (unwi)
4903
0
      {
4904
0
        elf_group_name (unwi) = name;
4905
0
        elf_next_in_group (unwi) = sec;
4906
0
        elf_next_in_group (sec) = unwi;
4907
0
        elf_sec_group (unwi) = group;
4908
0
      }
4909
4910
0
     if (unw)
4911
0
       {
4912
0
         elf_group_name (unw) = name;
4913
0
         if (unwi)
4914
0
     {
4915
0
       elf_next_in_group (unw) = elf_next_in_group (unwi);
4916
0
       elf_next_in_group (unwi) = unw;
4917
0
     }
4918
0
         else
4919
0
     {
4920
0
       elf_next_in_group (unw) = sec;
4921
0
       elf_next_in_group (sec) = unw;
4922
0
     }
4923
0
         elf_sec_group (unw) = group;
4924
0
       }
4925
4926
     /* Fake SHT_GROUP section header.  */
4927
0
    elf_section_data (group)->this_hdr.bfd_section = group;
4928
0
    elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
4929
0
  }
4930
0
    }
4931
0
  return true;
4932
0
}
4933
4934
static bool
4935
elf32_ia64_hpux_vec (const bfd_target *vec)
4936
0
{
4937
0
  extern const bfd_target ia64_elf32_hpux_be_vec;
4938
0
  return (vec == &ia64_elf32_hpux_be_vec);
4939
0
}
4940
4941
static bool
4942
elf32_hpux_init_file_header (bfd *abfd, struct bfd_link_info *info)
4943
0
{
4944
0
  Elf_Internal_Ehdr *i_ehdrp;
4945
4946
0
  if (!_bfd_elf_init_file_header (abfd, info))
4947
0
    return false;
4948
4949
0
  i_ehdrp = elf_elfheader (abfd);
4950
0
  i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
4951
0
  i_ehdrp->e_ident[EI_ABIVERSION] = 1;
4952
0
  return true;
4953
0
}
4954
4955
static bool
4956
elf32_hpux_backend_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4957
               asection *sec, int *retval)
4958
0
{
4959
0
  if (bfd_is_com_section (sec))
4960
0
    {
4961
0
      *retval = SHN_IA_64_ANSI_COMMON;
4962
0
      return true;
4963
0
    }
4964
0
  return false;
4965
0
}
4966
4967
static void
4968
elf32_hpux_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4969
              asymbol *asym)
4970
0
{
4971
0
  elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4972
4973
0
  switch (elfsym->internal_elf_sym.st_shndx)
4974
0
    {
4975
0
    case SHN_IA_64_ANSI_COMMON:
4976
0
      asym->section = bfd_com_section_ptr;
4977
0
      asym->value = elfsym->internal_elf_sym.st_size;
4978
0
      asym->flags &= ~BSF_GLOBAL;
4979
0
      break;
4980
0
    }
4981
0
}
4982
4983
static void
4984
ignore_errors (const char *fmt ATTRIBUTE_UNUSED, ...)
4985
0
{
4986
0
}
4987

4988
#define TARGET_LITTLE_SYM   ia64_elf32_le_vec
4989
#define TARGET_LITTLE_NAME    "elf32-ia64-little"
4990
#define TARGET_BIG_SYM      ia64_elf32_be_vec
4991
#define TARGET_BIG_NAME     "elf32-ia64-big"
4992
#define ELF_ARCH      bfd_arch_ia64
4993
#define ELF_TARGET_ID     IA64_ELF_DATA
4994
#define ELF_MACHINE_CODE    EM_IA_64
4995
#define ELF_MACHINE_ALT1    1999  /* EAS2.3 */
4996
#define ELF_MACHINE_ALT2    1998  /* EAS2.2 */
4997
#define ELF_MAXPAGESIZE     0x10000 /* 64KB */
4998
#define ELF_COMMONPAGESIZE    0x4000  /* 16KB */
4999
5000
#define elf_backend_section_from_shdr \
5001
  elf32_ia64_section_from_shdr
5002
#define elf_backend_section_flags \
5003
  elf32_ia64_section_flags
5004
#define elf_backend_fake_sections \
5005
  elf32_ia64_fake_sections
5006
#define elf_backend_final_write_processing \
5007
  elf32_ia64_final_write_processing
5008
#define elf_backend_add_symbol_hook \
5009
  elf32_ia64_add_symbol_hook
5010
#define elf_backend_additional_program_headers \
5011
  elf32_ia64_additional_program_headers
5012
#define elf_backend_modify_segment_map \
5013
  elf32_ia64_modify_segment_map
5014
#define elf_backend_modify_headers \
5015
  elf32_ia64_modify_headers
5016
#define elf_info_to_howto \
5017
  elf32_ia64_info_to_howto
5018
5019
#define bfd_elf32_bfd_reloc_type_lookup \
5020
  ia64_elf_reloc_type_lookup
5021
#define bfd_elf32_bfd_reloc_name_lookup \
5022
  ia64_elf_reloc_name_lookup
5023
#define bfd_elf32_bfd_is_local_label_name \
5024
  elf32_ia64_is_local_label_name
5025
#define bfd_elf32_bfd_relax_section \
5026
  elf32_ia64_relax_section
5027
5028
#define elf_backend_object_p \
5029
  elf32_ia64_object_p
5030
5031
/* Stuff for the BFD linker: */
5032
#define bfd_elf32_bfd_link_hash_table_create \
5033
  elf32_ia64_hash_table_create
5034
#define elf_backend_create_dynamic_sections \
5035
  elf32_ia64_create_dynamic_sections
5036
#define elf_backend_check_relocs \
5037
  elf32_ia64_check_relocs
5038
#define elf_backend_adjust_dynamic_symbol \
5039
  elf32_ia64_adjust_dynamic_symbol
5040
#define elf_backend_size_dynamic_sections \
5041
  elf32_ia64_size_dynamic_sections
5042
#define elf_backend_omit_section_dynsym \
5043
  _bfd_elf_omit_section_dynsym_all
5044
#define elf_backend_relocate_section \
5045
  elf32_ia64_relocate_section
5046
#define elf_backend_finish_dynamic_symbol \
5047
  elf32_ia64_finish_dynamic_symbol
5048
#define elf_backend_finish_dynamic_sections \
5049
  elf32_ia64_finish_dynamic_sections
5050
#define bfd_elf32_bfd_final_link \
5051
  elf32_ia64_final_link
5052
5053
#define bfd_elf32_bfd_merge_private_bfd_data \
5054
  elf32_ia64_merge_private_bfd_data
5055
#define bfd_elf32_bfd_set_private_flags \
5056
  elf32_ia64_set_private_flags
5057
#define bfd_elf32_bfd_print_private_bfd_data \
5058
  elf32_ia64_print_private_bfd_data
5059
5060
#define elf_backend_plt_readonly  1
5061
#define elf_backend_can_gc_sections 1
5062
#define elf_backend_want_plt_sym  0
5063
#define elf_backend_plt_alignment 5
5064
#define elf_backend_got_header_size 0
5065
#define elf_backend_want_got_plt  1
5066
#define elf_backend_may_use_rel_p 1
5067
#define elf_backend_may_use_rela_p  1
5068
#define elf_backend_default_use_rela_p  1
5069
#define elf_backend_want_dynbss   0
5070
#define elf_backend_copy_indirect_symbol elf32_ia64_hash_copy_indirect
5071
#define elf_backend_hide_symbol   elf32_ia64_hash_hide_symbol
5072
#define elf_backend_fixup_symbol  _bfd_elf_link_hash_fixup_symbol
5073
#define elf_backend_reloc_type_class  elf32_ia64_reloc_type_class
5074
#define elf_backend_rela_normal   1
5075
#define elf_backend_dtrel_excludes_plt  1
5076
#define elf_backend_special_sections  elf32_ia64_special_sections
5077
#define elf_backend_default_execstack 0
5078
5079
/* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
5080
   SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
5081
   We don't want to flood users with so many error messages. We turn
5082
   off the warning for now. It will be turned on later when the Intel
5083
   compiler is fixed.   */
5084
#define elf_backend_link_order_error_handler ignore_errors
5085
5086
#include "elf32-target.h"
5087
5088
/* HPUX-specific vectors.  */
5089
5090
#undef  TARGET_LITTLE_SYM
5091
#undef  TARGET_LITTLE_NAME
5092
#undef  TARGET_BIG_SYM
5093
#define TARGET_BIG_SYM      ia64_elf32_hpux_be_vec
5094
#undef  TARGET_BIG_NAME
5095
#define TARGET_BIG_NAME     "elf32-ia64-hpux-big"
5096
5097
/* These are HP-UX specific functions.  */
5098
5099
#undef  elf_backend_init_file_header
5100
#define elf_backend_init_file_header elf32_hpux_init_file_header
5101
5102
#undef  elf_backend_section_from_bfd_section
5103
#define elf_backend_section_from_bfd_section elf32_hpux_backend_section_from_bfd_section
5104
5105
#undef elf_backend_symbol_processing
5106
#define elf_backend_symbol_processing elf32_hpux_backend_symbol_processing
5107
5108
#undef  elf_backend_want_p_paddr_set_to_zero
5109
#define elf_backend_want_p_paddr_set_to_zero 1
5110
5111
#undef ELF_COMMONPAGESIZE
5112
#undef ELF_OSABI
5113
#define ELF_OSABI     ELFOSABI_HPUX
5114
5115
#undef  elf32_bed
5116
#define elf32_bed elf32_ia64_hpux_bed
5117
5118
#include "elf32-target.h"