Coverage Report

Created: 2026-11-21 09:00

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