Coverage Report

Created: 2026-03-10 08:46

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