Coverage Report

Created: 2026-11-21 09:00

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