Coverage Report

Created: 2026-09-14 08:07

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