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-alpha.c
Line
Count
Source
1
/* Alpha specific support for 64-bit ELF
2
   Copyright (C) 1996-2026 Free Software Foundation, Inc.
3
   Contributed by Richard Henderson <rth@tamu.edu>.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
23
/* We need a published ABI spec for this.  Until one comes out, don't
24
   assume this'll remain unchanged forever.  */
25
26
#include "sysdep.h"
27
#include "bfd.h"
28
#include "libbfd.h"
29
#include "elf-bfd.h"
30
#include "ecoff-bfd.h"
31
32
#include "elf/alpha.h"
33
34
#define ALPHAECOFF
35
36
#define NO_COFF_RELOCS
37
#define NO_COFF_SYMBOLS
38
#define NO_COFF_LINENOS
39
40
/* Get the ECOFF swapping routines.  Needed for the debug information.  */
41
#include "coff/internal.h"
42
#include "coff/sym.h"
43
#include "coff/symconst.h"
44
#include "coff/ecoff.h"
45
#include "coff/alpha.h"
46
#include "aout/ar.h"
47
#include "libcoff.h"
48
#include "libecoff.h"
49
#define ECOFF_64
50
#include "ecoffswap.h"
51
52

53
/* Instruction data for plt generation and relaxation.  */
54
55
0
#define OP_LDA    0x08U
56
0
#define OP_LDAH   0x09U
57
0
#define OP_LDQ    0x29U
58
0
#define OP_BR   0x30U
59
0
#define OP_BSR    0x34U
60
61
#define INSN_LDA  (OP_LDA << 26)
62
#define INSN_LDAH (OP_LDAH << 26)
63
#define INSN_LDQ  (OP_LDQ << 26)
64
#define INSN_BR   (OP_BR << 26)
65
66
0
#define INSN_ADDQ 0x40000400
67
#define INSN_RDUNIQ 0x0000009e
68
#define INSN_SUBQ 0x40000520
69
#define INSN_S4SUBQ 0x40000560
70
0
#define INSN_UNOP 0x2ffe0000
71
72
0
#define INSN_JSR  0x68004000
73
#define INSN_JMP  0x68000000
74
0
#define INSN_JSR_MASK 0xfc00c000
75
76
0
#define INSN_A(I,A)   (I | ((unsigned) A << 21))
77
0
#define INSN_AB(I,A,B)    (INSN_A (I, A) | (B << 16))
78
0
#define INSN_ABC(I,A,B,C) (INSN_A (I, A) | (B << 16) | C)
79
0
#define INSN_ABO(I,A,B,O) (INSN_A (I, A) | (B << 16) | ((O) & 0xffff))
80
0
#define INSN_AD(I,A,D)    (INSN_A (I, A) | (((D) >> 2) & 0x1fffff))
81
82
/* PLT/GOT Stuff */
83
84
/* Set by ld emulation.  Putting this into the link_info or hash structure
85
   is simply working too hard.  */
86
#ifdef USE_SECUREPLT
87
bool elf64_alpha_use_secureplt = true;
88
#else
89
bool elf64_alpha_use_secureplt = false;
90
#endif
91
92
0
#define OLD_PLT_HEADER_SIZE 32
93
0
#define OLD_PLT_ENTRY_SIZE  12
94
0
#define NEW_PLT_HEADER_SIZE 36
95
0
#define NEW_PLT_ENTRY_SIZE  4
96
97
#define PLT_HEADER_SIZE \
98
0
  (elf64_alpha_use_secureplt ? NEW_PLT_HEADER_SIZE : OLD_PLT_HEADER_SIZE)
99
#define PLT_ENTRY_SIZE \
100
0
  (elf64_alpha_use_secureplt ? NEW_PLT_ENTRY_SIZE : OLD_PLT_ENTRY_SIZE)
101
102
0
#define MAX_GOT_SIZE    (64*1024)
103
104
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
105

106
107
/* Used to implement multiple .got subsections.  */
108
struct alpha_elf_got_entry
109
{
110
  struct alpha_elf_got_entry *next;
111
112
  /* Which .got subsection?  */
113
  bfd *gotobj;
114
115
  /* The addend in effect for this entry.  */
116
  bfd_vma addend;
117
118
  /* The .got offset for this entry.  */
119
  int got_offset;
120
121
  /* The .plt offset for this entry.  */
122
  int plt_offset;
123
124
  /* How many references to this entry?  */
125
  int use_count;
126
127
  /* The relocation type of this entry.  */
128
  unsigned char reloc_type;
129
130
  /* How a LITERAL is used.  */
131
  unsigned char flags;
132
133
  /* Have we initialized the dynamic relocation for this entry?  */
134
  unsigned char reloc_done;
135
136
  /* Have we adjusted this entry for SEC_MERGE?  */
137
  unsigned char reloc_xlated;
138
139
  /* Does a section that survives into the output refer to this entry?
140
     Only such an entry is ever filled in, so only such an entry gets an
141
     R_ALPHA_IRELATIVE reserved for it.  */
142
  unsigned char irel_live;
143
};
144
145
struct alpha_elf_reloc_entry
146
{
147
  struct alpha_elf_reloc_entry *next;
148
149
  /* Which .reloc section? */
150
  asection *srel;
151
152
  /* Which section this relocation is against? */
153
  asection *sec;
154
155
  /* How many did we find?  */
156
  unsigned long count;
157
158
  /* What kind of relocation? */
159
  unsigned int rtype;
160
};
161
162
struct alpha_elf_link_hash_entry
163
{
164
  struct elf_link_hash_entry root;
165
166
  /* External symbol information.  */
167
  EXTR esym;
168
169
  /* Cumulative flags for all the .got entries.  */
170
  int flags;
171
172
  /* Contexts in which a literal was referenced.  */
173
0
#define ALPHA_ELF_LINK_HASH_LU_ADDR  0x01
174
#define ALPHA_ELF_LINK_HASH_LU_MEM   0x02
175
#define ALPHA_ELF_LINK_HASH_LU_BYTE  0x04
176
#define ALPHA_ELF_LINK_HASH_LU_JSR   0x08
177
#define ALPHA_ELF_LINK_HASH_LU_TLSGD   0x10
178
#define ALPHA_ELF_LINK_HASH_LU_TLSLDM  0x20
179
#define ALPHA_ELF_LINK_HASH_LU_JSRDIRECT 0x40
180
0
#define ALPHA_ELF_LINK_HASH_LU_PLT   0x38
181
0
#define ALPHA_ELF_LINK_HASH_TLS_IE   0x80
182
183
  /* Used to implement multiple .got subsections.  */
184
  struct alpha_elf_got_entry *got_entries;
185
186
  /* Used to count non-got, non-plt relocations for delayed sizing
187
     of relocation sections.  */
188
  struct alpha_elf_reloc_entry *reloc_entries;
189
};
190
191
/* Alpha ELF linker hash table.  */
192
193
struct alpha_elf_link_hash_table
194
{
195
  struct elf_link_hash_table root;
196
197
  /* The head of a list of .got subsections linked through
198
     alpha_elf_tdata(abfd)->got_link_next.  */
199
  bfd *got_list;
200
201
  /* The most recent relax pass that we've seen.  The GOTs
202
     should be regenerated if this doesn't match.  */
203
  int relax_trip;
204
205
  /* True once relaxation has reached its second pass.  ld may rerun
206
     relaxation from pass 0, but .got subsections must not be merged
207
     after GPREL relocs exist.  */
208
  bool no_got_merge;
209
210
  /* The part of the size of .rela.iplt that is due to data references to
211
     an IFUNC, counted once the sections that survive into the output are
212
     known.  The rest comes from got entries and is recomputed whenever the
213
     GOTs are.  */
214
  bfd_size_type irelplt_data_size;
215
};
216
217
/* Look up an entry in a Alpha ELF linker hash table.  */
218
219
#define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
220
0
  ((struct alpha_elf_link_hash_entry *)         \
221
0
   elf_link_hash_lookup (&(table)->root, (string), (create),    \
222
0
       (copy), (follow)))
223
224
/* Traverse a Alpha ELF linker hash table.  */
225
226
#define alpha_elf_link_hash_traverse(table, func, info)     \
227
0
  (elf_link_hash_traverse           \
228
0
   (&(table)->root,             \
229
0
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
230
0
    (info)))
231
232
/* Get the Alpha ELF linker hash table from a link_info structure.  */
233
234
#define alpha_elf_hash_table(p) \
235
0
  ((is_elf_hash_table ((p)->hash)          \
236
0
    && elf_hash_table_id (elf_hash_table (p)) == ALPHA_ELF_DATA) \
237
0
   ? (struct alpha_elf_link_hash_table *) (p)->hash : NULL)
238
239
/* Get the object's symbols as our own entry type.  */
240
241
#define alpha_elf_sym_hashes(abfd) \
242
0
  ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
243
244
/* Should we do dynamic things to this symbol?  This differs from the
245
   generic version in that we never need to consider function pointer
246
   equality wrt PLT entries -- we don't create a PLT entry if a symbol's
247
   address is ever taken.  */
248
249
static inline bool
250
alpha_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
251
          struct bfd_link_info *info)
252
0
{
253
0
  return _bfd_elf_dynamic_symbol_p (h, info, 0);
254
0
}
255
256
/* Create an entry in a Alpha ELF linker hash table.  */
257
258
static struct bfd_hash_entry *
259
elf64_alpha_link_hash_newfunc (struct bfd_hash_entry *entry,
260
             struct bfd_hash_table *table,
261
             const char *string)
262
0
{
263
0
  struct alpha_elf_link_hash_entry *ret =
264
0
    (struct alpha_elf_link_hash_entry *) entry;
265
266
  /* Allocate the structure if it has not already been allocated by a
267
     subclass.  */
268
0
  if (ret == (struct alpha_elf_link_hash_entry *) NULL)
269
0
    ret = ((struct alpha_elf_link_hash_entry *)
270
0
     bfd_hash_allocate (table,
271
0
            sizeof (struct alpha_elf_link_hash_entry)));
272
0
  if (ret == (struct alpha_elf_link_hash_entry *) NULL)
273
0
    return (struct bfd_hash_entry *) ret;
274
275
  /* Call the allocation method of the superclass.  */
276
0
  ret = ((struct alpha_elf_link_hash_entry *)
277
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
278
0
             table, string));
279
0
  if (ret != (struct alpha_elf_link_hash_entry *) NULL)
280
0
    {
281
      /* Set local fields.  */
282
0
      memset (&ret->esym, 0, sizeof (EXTR));
283
      /* We use -2 as a marker to indicate that the information has
284
   not been set.  -1 means there is no associated ifd.  */
285
0
      ret->esym.ifd = -2;
286
0
      ret->flags = 0;
287
0
      ret->got_entries = NULL;
288
0
      ret->reloc_entries = NULL;
289
0
    }
290
291
0
  return (struct bfd_hash_entry *) ret;
292
0
}
293
294
/* Create a Alpha ELF linker hash table.  */
295
296
static struct bfd_link_hash_table *
297
elf64_alpha_bfd_link_hash_table_create (bfd *abfd)
298
0
{
299
0
  struct alpha_elf_link_hash_table *ret;
300
0
  size_t amt = sizeof (struct alpha_elf_link_hash_table);
301
302
0
  ret = (struct alpha_elf_link_hash_table *) bfd_zmalloc (amt);
303
0
  if (ret == (struct alpha_elf_link_hash_table *) NULL)
304
0
    return NULL;
305
306
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
307
0
              elf64_alpha_link_hash_newfunc,
308
0
              sizeof (struct alpha_elf_link_hash_entry)))
309
0
    {
310
0
      free (ret);
311
0
      return NULL;
312
0
    }
313
314
0
  return &ret->root.root;
315
0
}
316

317
/* Alpha ELF follows MIPS ELF in using a special find_nearest_line
318
   routine in order to handle the ECOFF debugging information.  */
319
320
struct alpha_elf_find_line
321
{
322
  struct ecoff_debug_info d;
323
  struct ecoff_find_line i;
324
};
325
326
/* We have some private fields hanging off of the elf_tdata structure.  */
327
328
struct alpha_elf_obj_tdata
329
{
330
  struct elf_obj_tdata root;
331
332
  /* For every input file, these are the got entries for that object's
333
     local symbols.  */
334
  struct alpha_elf_got_entry ** local_got_entries;
335
336
  /* For every input file, this is the object that owns the got that
337
     this input file uses.  */
338
  bfd *gotobj;
339
340
  /* For every got, this is a linked list through the objects using this got */
341
  bfd *in_got_link_next;
342
343
  /* For every got, this is a link to the next got subsegment.  */
344
  bfd *got_link_next;
345
346
  /* For every got, this is the section.  */
347
  asection *got;
348
349
  /* For every got, this is it's total number of words.  */
350
  int total_got_size;
351
352
  /* For every got, this is the sum of the number of words required
353
     to hold all of the member object's local got.  */
354
  int local_got_size;
355
356
  /* Used by elf64_alpha_find_nearest_line entry point.  */
357
  struct alpha_elf_find_line *find_line_info;
358
359
};
360
361
#define alpha_elf_tdata(abfd) \
362
0
  ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
363
364
#define is_alpha_elf(bfd) \
365
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
366
0
   && elf_tdata (bfd) != NULL \
367
0
   && elf_object_id (bfd) == ALPHA_ELF_DATA)
368
369
static bool
370
elf64_alpha_mkobject (bfd *abfd)
371
32.6k
{
372
32.6k
  return bfd_elf_allocate_object (abfd, sizeof (struct alpha_elf_obj_tdata));
373
32.6k
}
374
375
static bool
376
elf64_alpha_object_p (bfd *abfd)
377
47
{
378
  /* Set the right machine number for an Alpha ELF file.  */
379
47
  return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
380
47
}
381

382
/* A relocation function which doesn't do anything.  */
383
384
static bfd_reloc_status_type
385
elf64_alpha_reloc_nil (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
386
           asymbol *sym ATTRIBUTE_UNUSED,
387
           void * data ATTRIBUTE_UNUSED, asection *sec,
388
           bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
389
0
{
390
0
  if (output_bfd)
391
0
    reloc->address += sec->output_offset;
392
0
  return bfd_reloc_ok;
393
0
}
394
395
/* A relocation function used for an unsupported reloc.  */
396
397
static bfd_reloc_status_type
398
elf64_alpha_reloc_bad (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
399
           asymbol *sym ATTRIBUTE_UNUSED,
400
           void * data ATTRIBUTE_UNUSED, asection *sec,
401
           bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
402
0
{
403
0
  if (output_bfd)
404
0
    reloc->address += sec->output_offset;
405
0
  return bfd_reloc_notsupported;
406
0
}
407
408
/* Do the work of the GPDISP relocation.  */
409
410
static bfd_reloc_status_type
411
elf64_alpha_do_reloc_gpdisp (bfd *abfd, bfd_vma gpdisp, bfd_byte *p_ldah,
412
           bfd_byte *p_lda)
413
0
{
414
0
  bfd_reloc_status_type ret = bfd_reloc_ok;
415
0
  bfd_vma addend;
416
0
  unsigned long i_ldah, i_lda;
417
418
0
  i_ldah = bfd_get_32 (abfd, p_ldah);
419
0
  i_lda = bfd_get_32 (abfd, p_lda);
420
421
  /* Complain if the instructions are not correct.  */
422
0
  if (((i_ldah >> 26) & 0x3f) != 0x09
423
0
      || ((i_lda >> 26) & 0x3f) != 0x08)
424
0
    ret = bfd_reloc_dangerous;
425
426
  /* Extract the user-supplied offset, mirroring the sign extensions
427
     that the instructions perform.  */
428
0
  addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
429
0
  addend = (addend ^ 0x80008000) - 0x80008000;
430
431
0
  gpdisp += addend;
432
433
0
  if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
434
0
      || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
435
0
    ret = bfd_reloc_overflow;
436
437
  /* compensate for the sign extension again.  */
438
0
  i_ldah = ((i_ldah & 0xffff0000)
439
0
      | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
440
0
  i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
441
442
0
  bfd_put_32 (abfd, (bfd_vma) i_ldah, p_ldah);
443
0
  bfd_put_32 (abfd, (bfd_vma) i_lda, p_lda);
444
445
0
  return ret;
446
0
}
447
448
/* The special function for the GPDISP reloc.  */
449
450
static bfd_reloc_status_type
451
elf64_alpha_reloc_gpdisp (bfd *abfd, arelent *reloc_entry,
452
        asymbol *sym ATTRIBUTE_UNUSED, void * data,
453
        asection *input_section, bfd *output_bfd,
454
        char **err_msg)
455
0
{
456
0
  bfd_reloc_status_type ret;
457
0
  bfd_vma gp, relocation;
458
0
  bfd_vma high_address;
459
0
  bfd_byte *p_ldah, *p_lda;
460
461
  /* Don't do anything if we're not doing a final link.  */
462
0
  if (output_bfd)
463
0
    {
464
0
      reloc_entry->address += input_section->output_offset;
465
0
      return bfd_reloc_ok;
466
0
    }
467
468
0
  high_address = bfd_get_section_limit (abfd, input_section);
469
0
  if (reloc_entry->address > high_address
470
0
      || reloc_entry->address + reloc_entry->addend > high_address)
471
0
    return bfd_reloc_outofrange;
472
473
  /* The gp used in the portion of the output object to which this
474
     input object belongs is cached on the input bfd.  */
475
0
  gp = _bfd_get_gp_value (abfd);
476
477
0
  relocation = (input_section->output_section->vma
478
0
    + input_section->output_offset
479
0
    + reloc_entry->address);
480
481
0
  p_ldah = (bfd_byte *) data + reloc_entry->address;
482
0
  p_lda = p_ldah + reloc_entry->addend;
483
484
0
  ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
485
486
  /* Complain if the instructions are not correct.  */
487
0
  if (ret == bfd_reloc_dangerous)
488
0
    *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
489
490
0
  return ret;
491
0
}
492
493
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
494
   from smaller values.  Start with zero, widen, *then* decrement.  */
495
#define MINUS_ONE (((bfd_vma)0) - 1)
496
497
498
#define SKIP_HOWTO(N) \
499
  HOWTO(N, 0, 0, 0, 0, 0, complain_overflow_dont, elf64_alpha_reloc_bad, 0, 0, 0, 0, 0)
500
501
static reloc_howto_type elf64_alpha_howto_table[] =
502
{
503
  HOWTO (R_ALPHA_NONE,    /* type */
504
   0,     /* rightshift */
505
   0,     /* size */
506
   0,     /* bitsize */
507
   true,      /* pc_relative */
508
   0,     /* bitpos */
509
   complain_overflow_dont, /* complain_on_overflow */
510
   elf64_alpha_reloc_nil, /* special_function */
511
   "NONE",    /* name */
512
   false,     /* partial_inplace */
513
   0,     /* src_mask */
514
   0,     /* dst_mask */
515
   true),     /* pcrel_offset */
516
517
  /* A 32 bit reference to a symbol.  */
518
  HOWTO (R_ALPHA_REFLONG, /* type */
519
   0,     /* rightshift */
520
   4,     /* size */
521
   32,      /* bitsize */
522
   false,     /* pc_relative */
523
   0,     /* bitpos */
524
   complain_overflow_bitfield, /* complain_on_overflow */
525
   bfd_elf_generic_reloc, /* special_function */
526
   "REFLONG",   /* name */
527
   false,     /* partial_inplace */
528
   0xffffffff,    /* src_mask */
529
   0xffffffff,    /* dst_mask */
530
   false),    /* pcrel_offset */
531
532
  /* A 64 bit reference to a symbol.  */
533
  HOWTO (R_ALPHA_REFQUAD, /* type */
534
   0,     /* rightshift */
535
   8,     /* size */
536
   64,      /* bitsize */
537
   false,     /* pc_relative */
538
   0,     /* bitpos */
539
   complain_overflow_bitfield, /* complain_on_overflow */
540
   bfd_elf_generic_reloc, /* special_function */
541
   "REFQUAD",   /* name */
542
   false,     /* partial_inplace */
543
   MINUS_ONE,   /* src_mask */
544
   MINUS_ONE,   /* dst_mask */
545
   false),    /* pcrel_offset */
546
547
  /* A 32 bit GP relative offset.  This is just like REFLONG except
548
     that when the value is used the value of the gp register will be
549
     added in.  */
550
  HOWTO (R_ALPHA_GPREL32, /* type */
551
   0,     /* rightshift */
552
   4,     /* size */
553
   32,      /* bitsize */
554
   false,     /* pc_relative */
555
   0,     /* bitpos */
556
   complain_overflow_bitfield, /* complain_on_overflow */
557
   bfd_elf_generic_reloc, /* special_function */
558
   "GPREL32",   /* name */
559
   false,     /* partial_inplace */
560
   0xffffffff,    /* src_mask */
561
   0xffffffff,    /* dst_mask */
562
   false),    /* pcrel_offset */
563
564
  /* Used for an instruction that refers to memory off the GP register.  */
565
  HOWTO (R_ALPHA_LITERAL, /* type */
566
   0,     /* rightshift */
567
   2,     /* size */
568
   16,      /* bitsize */
569
   false,     /* pc_relative */
570
   0,     /* bitpos */
571
   complain_overflow_signed, /* complain_on_overflow */
572
   bfd_elf_generic_reloc, /* special_function */
573
   "ELF_LITERAL",   /* name */
574
   false,     /* partial_inplace */
575
   0xffff,    /* src_mask */
576
   0xffff,    /* dst_mask */
577
   false),    /* pcrel_offset */
578
579
  /* This reloc only appears immediately following an ELF_LITERAL reloc.
580
     It identifies a use of the literal.  The symbol index is special:
581
     1 means the literal address is in the base register of a memory
582
     format instruction; 2 means the literal address is in the byte
583
     offset register of a byte-manipulation instruction; 3 means the
584
     literal address is in the target register of a jsr instruction.
585
     This does not actually do any relocation.  */
586
  HOWTO (R_ALPHA_LITUSE,  /* type */
587
   0,     /* rightshift */
588
   2,     /* size */
589
   32,      /* bitsize */
590
   false,     /* pc_relative */
591
   0,     /* bitpos */
592
   complain_overflow_dont, /* complain_on_overflow */
593
   elf64_alpha_reloc_nil, /* special_function */
594
   "LITUSE",    /* name */
595
   false,     /* partial_inplace */
596
   0,     /* src_mask */
597
   0,     /* dst_mask */
598
   false),    /* pcrel_offset */
599
600
  /* Load the gp register.  This is always used for a ldah instruction
601
     which loads the upper 16 bits of the gp register.  The symbol
602
     index of the GPDISP instruction is an offset in bytes to the lda
603
     instruction that loads the lower 16 bits.  The value to use for
604
     the relocation is the difference between the GP value and the
605
     current location; the load will always be done against a register
606
     holding the current address.
607
608
     NOTE: Unlike ECOFF, partial in-place relocation is not done.  If
609
     any offset is present in the instructions, it is an offset from
610
     the register to the ldah instruction.  This lets us avoid any
611
     stupid hackery like inventing a gp value to do partial relocation
612
     against.  Also unlike ECOFF, we do the whole relocation off of
613
     the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair.  An odd,
614
     space consuming bit, that, since all the information was present
615
     in the GPDISP_HI16 reloc.  */
616
  HOWTO (R_ALPHA_GPDISP,  /* type */
617
   16,      /* rightshift */
618
   4,     /* size */
619
   16,      /* bitsize */
620
   false,     /* pc_relative */
621
   0,     /* bitpos */
622
   complain_overflow_dont, /* complain_on_overflow */
623
   elf64_alpha_reloc_gpdisp, /* special_function */
624
   "GPDISP",    /* name */
625
   false,     /* partial_inplace */
626
   0xffff,    /* src_mask */
627
   0xffff,    /* dst_mask */
628
   true),     /* pcrel_offset */
629
630
  /* A 21 bit branch.  */
631
  HOWTO (R_ALPHA_BRADDR,  /* type */
632
   2,     /* rightshift */
633
   4,     /* size */
634
   21,      /* bitsize */
635
   true,      /* pc_relative */
636
   0,     /* bitpos */
637
   complain_overflow_signed, /* complain_on_overflow */
638
   bfd_elf_generic_reloc, /* special_function */
639
   "BRADDR",    /* name */
640
   false,     /* partial_inplace */
641
   0x1fffff,    /* src_mask */
642
   0x1fffff,    /* dst_mask */
643
   true),     /* pcrel_offset */
644
645
  /* A hint for a jump to a register.  */
646
  HOWTO (R_ALPHA_HINT,    /* type */
647
   2,     /* rightshift */
648
   2,     /* size */
649
   14,      /* bitsize */
650
   true,      /* pc_relative */
651
   0,     /* bitpos */
652
   complain_overflow_dont, /* complain_on_overflow */
653
   bfd_elf_generic_reloc, /* special_function */
654
   "HINT",    /* name */
655
   false,     /* partial_inplace */
656
   0x3fff,    /* src_mask */
657
   0x3fff,    /* dst_mask */
658
   true),     /* pcrel_offset */
659
660
  /* 16 bit PC relative offset.  */
661
  HOWTO (R_ALPHA_SREL16,  /* type */
662
   0,     /* rightshift */
663
   2,     /* size */
664
   16,      /* bitsize */
665
   true,      /* pc_relative */
666
   0,     /* bitpos */
667
   complain_overflow_signed, /* complain_on_overflow */
668
   bfd_elf_generic_reloc, /* special_function */
669
   "SREL16",    /* name */
670
   false,     /* partial_inplace */
671
   0xffff,    /* src_mask */
672
   0xffff,    /* dst_mask */
673
   true),     /* pcrel_offset */
674
675
  /* 32 bit PC relative offset.  */
676
  HOWTO (R_ALPHA_SREL32,  /* type */
677
   0,     /* rightshift */
678
   4,     /* size */
679
   32,      /* bitsize */
680
   true,      /* pc_relative */
681
   0,     /* bitpos */
682
   complain_overflow_signed, /* complain_on_overflow */
683
   bfd_elf_generic_reloc, /* special_function */
684
   "SREL32",    /* name */
685
   false,     /* partial_inplace */
686
   0xffffffff,    /* src_mask */
687
   0xffffffff,    /* dst_mask */
688
   true),     /* pcrel_offset */
689
690
  /* A 64 bit PC relative offset.  */
691
  HOWTO (R_ALPHA_SREL64,  /* type */
692
   0,     /* rightshift */
693
   8,     /* size */
694
   64,      /* bitsize */
695
   true,      /* pc_relative */
696
   0,     /* bitpos */
697
   complain_overflow_signed, /* complain_on_overflow */
698
   bfd_elf_generic_reloc, /* special_function */
699
   "SREL64",    /* name */
700
   false,     /* partial_inplace */
701
   MINUS_ONE,   /* src_mask */
702
   MINUS_ONE,   /* dst_mask */
703
   true),     /* pcrel_offset */
704
705
  /* Skip 12 - 16; deprecated ECOFF relocs.  */
706
  SKIP_HOWTO (12),
707
  SKIP_HOWTO (13),
708
  SKIP_HOWTO (14),
709
  SKIP_HOWTO (15),
710
  SKIP_HOWTO (16),
711
712
  /* The high 16 bits of the displacement from GP to the target.  */
713
  HOWTO (R_ALPHA_GPRELHIGH,
714
   0,     /* rightshift */
715
   2,     /* size */
716
   16,      /* bitsize */
717
   false,     /* pc_relative */
718
   0,     /* bitpos */
719
   complain_overflow_signed, /* complain_on_overflow */
720
   bfd_elf_generic_reloc, /* special_function */
721
   "GPRELHIGH",   /* name */
722
   false,     /* partial_inplace */
723
   0xffff,    /* src_mask */
724
   0xffff,    /* dst_mask */
725
   false),    /* pcrel_offset */
726
727
  /* The low 16 bits of the displacement from GP to the target.  */
728
  HOWTO (R_ALPHA_GPRELLOW,
729
   0,     /* rightshift */
730
   2,     /* size */
731
   16,      /* bitsize */
732
   false,     /* pc_relative */
733
   0,     /* bitpos */
734
   complain_overflow_dont, /* complain_on_overflow */
735
   bfd_elf_generic_reloc, /* special_function */
736
   "GPRELLOW",    /* name */
737
   false,     /* partial_inplace */
738
   0xffff,    /* src_mask */
739
   0xffff,    /* dst_mask */
740
   false),    /* pcrel_offset */
741
742
  /* A 16-bit displacement from the GP to the target.  */
743
  HOWTO (R_ALPHA_GPREL16,
744
   0,     /* rightshift */
745
   2,     /* size */
746
   16,      /* bitsize */
747
   false,     /* pc_relative */
748
   0,     /* bitpos */
749
   complain_overflow_signed, /* complain_on_overflow */
750
   bfd_elf_generic_reloc, /* special_function */
751
   "GPREL16",   /* name */
752
   false,     /* partial_inplace */
753
   0xffff,    /* src_mask */
754
   0xffff,    /* dst_mask */
755
   false),    /* pcrel_offset */
756
757
  /* Skip 20 - 23; deprecated ECOFF relocs.  */
758
  SKIP_HOWTO (20),
759
  SKIP_HOWTO (21),
760
  SKIP_HOWTO (22),
761
  SKIP_HOWTO (23),
762
763
  /* Misc ELF relocations.  */
764
765
  /* A dynamic relocation to copy the target into our .dynbss section.  */
766
  /* Not generated, as all Alpha objects use PIC, so it is not needed.  It
767
     is present because every other ELF has one, but should not be used
768
     because .dynbss is an ugly thing.  */
769
  HOWTO (R_ALPHA_COPY,
770
   0,
771
   0,
772
   0,
773
   false,
774
   0,
775
   complain_overflow_dont,
776
   bfd_elf_generic_reloc,
777
   "COPY",
778
   false,
779
   0,
780
   0,
781
   true),
782
783
  /* A dynamic relocation for a .got entry.  */
784
  HOWTO (R_ALPHA_GLOB_DAT,
785
   0,
786
   0,
787
   0,
788
   false,
789
   0,
790
   complain_overflow_dont,
791
   bfd_elf_generic_reloc,
792
   "GLOB_DAT",
793
   false,
794
   0,
795
   0,
796
   true),
797
798
  /* A dynamic relocation for a .plt entry.  */
799
  HOWTO (R_ALPHA_JMP_SLOT,
800
   0,
801
   0,
802
   0,
803
   false,
804
   0,
805
   complain_overflow_dont,
806
   bfd_elf_generic_reloc,
807
   "JMP_SLOT",
808
   false,
809
   0,
810
   0,
811
   true),
812
813
  /* A dynamic relocation to add the base of the DSO to a 64-bit field.  */
814
  HOWTO (R_ALPHA_RELATIVE,
815
   0,
816
   0,
817
   0,
818
   false,
819
   0,
820
   complain_overflow_dont,
821
   bfd_elf_generic_reloc,
822
   "RELATIVE",
823
   false,
824
   0,
825
   0,
826
   true),
827
828
  /* A 21 bit branch that adjusts for gp loads.  */
829
  HOWTO (R_ALPHA_BRSGP,   /* type */
830
   2,     /* rightshift */
831
   4,     /* size */
832
   21,      /* bitsize */
833
   true,      /* pc_relative */
834
   0,     /* bitpos */
835
   complain_overflow_signed, /* complain_on_overflow */
836
   bfd_elf_generic_reloc, /* special_function */
837
   "BRSGP",   /* name */
838
   false,     /* partial_inplace */
839
   0x1fffff,    /* src_mask */
840
   0x1fffff,    /* dst_mask */
841
   true),     /* pcrel_offset */
842
843
  /* Creates a tls_index for the symbol in the got.  */
844
  HOWTO (R_ALPHA_TLSGD,   /* type */
845
   0,     /* rightshift */
846
   2,     /* size */
847
   16,      /* bitsize */
848
   false,     /* pc_relative */
849
   0,     /* bitpos */
850
   complain_overflow_signed, /* complain_on_overflow */
851
   bfd_elf_generic_reloc, /* special_function */
852
   "TLSGD",   /* name */
853
   false,     /* partial_inplace */
854
   0xffff,    /* src_mask */
855
   0xffff,    /* dst_mask */
856
   false),    /* pcrel_offset */
857
858
  /* Creates a tls_index for the (current) module in the got.  */
859
  HOWTO (R_ALPHA_TLSLDM,  /* type */
860
   0,     /* rightshift */
861
   2,     /* size */
862
   16,      /* bitsize */
863
   false,     /* pc_relative */
864
   0,     /* bitpos */
865
   complain_overflow_signed, /* complain_on_overflow */
866
   bfd_elf_generic_reloc, /* special_function */
867
   "TLSLDM",    /* name */
868
   false,     /* partial_inplace */
869
   0xffff,    /* src_mask */
870
   0xffff,    /* dst_mask */
871
   false),    /* pcrel_offset */
872
873
  /* A dynamic relocation for a DTP module entry.  */
874
  HOWTO (R_ALPHA_DTPMOD64,  /* type */
875
   0,     /* rightshift */
876
   8,     /* size */
877
   64,      /* bitsize */
878
   false,     /* pc_relative */
879
   0,     /* bitpos */
880
   complain_overflow_bitfield, /* complain_on_overflow */
881
   bfd_elf_generic_reloc, /* special_function */
882
   "DTPMOD64",    /* name */
883
   false,     /* partial_inplace */
884
   MINUS_ONE,   /* src_mask */
885
   MINUS_ONE,   /* dst_mask */
886
   false),    /* pcrel_offset */
887
888
  /* Creates a 64-bit offset in the got for the displacement
889
     from DTP to the target.  */
890
  HOWTO (R_ALPHA_GOTDTPREL, /* type */
891
   0,     /* rightshift */
892
   2,     /* size */
893
   16,      /* bitsize */
894
   false,     /* pc_relative */
895
   0,     /* bitpos */
896
   complain_overflow_signed, /* complain_on_overflow */
897
   bfd_elf_generic_reloc, /* special_function */
898
   "GOTDTPREL",   /* name */
899
   false,     /* partial_inplace */
900
   0xffff,    /* src_mask */
901
   0xffff,    /* dst_mask */
902
   false),    /* pcrel_offset */
903
904
  /* A dynamic relocation for a displacement from DTP to the target.  */
905
  HOWTO (R_ALPHA_DTPREL64,  /* type */
906
   0,     /* rightshift */
907
   8,     /* size */
908
   64,      /* bitsize */
909
   false,     /* pc_relative */
910
   0,     /* bitpos */
911
   complain_overflow_bitfield, /* complain_on_overflow */
912
   bfd_elf_generic_reloc, /* special_function */
913
   "DTPREL64",    /* name */
914
   false,     /* partial_inplace */
915
   MINUS_ONE,   /* src_mask */
916
   MINUS_ONE,   /* dst_mask */
917
   false),    /* pcrel_offset */
918
919
  /* The high 16 bits of the displacement from DTP to the target.  */
920
  HOWTO (R_ALPHA_DTPRELHI,  /* type */
921
   0,     /* rightshift */
922
   2,     /* size */
923
   16,      /* bitsize */
924
   false,     /* pc_relative */
925
   0,     /* bitpos */
926
   complain_overflow_signed, /* complain_on_overflow */
927
   bfd_elf_generic_reloc, /* special_function */
928
   "DTPRELHI",    /* name */
929
   false,     /* partial_inplace */
930
   0xffff,    /* src_mask */
931
   0xffff,    /* dst_mask */
932
   false),    /* pcrel_offset */
933
934
  /* The low 16 bits of the displacement from DTP to the target.  */
935
  HOWTO (R_ALPHA_DTPRELLO,  /* type */
936
   0,     /* rightshift */
937
   2,     /* size */
938
   16,      /* bitsize */
939
   false,     /* pc_relative */
940
   0,     /* bitpos */
941
   complain_overflow_dont, /* complain_on_overflow */
942
   bfd_elf_generic_reloc, /* special_function */
943
   "DTPRELLO",    /* name */
944
   false,     /* partial_inplace */
945
   0xffff,    /* src_mask */
946
   0xffff,    /* dst_mask */
947
   false),    /* pcrel_offset */
948
949
  /* A 16-bit displacement from DTP to the target.  */
950
  HOWTO (R_ALPHA_DTPREL16,  /* type */
951
   0,     /* rightshift */
952
   2,     /* size */
953
   16,      /* bitsize */
954
   false,     /* pc_relative */
955
   0,     /* bitpos */
956
   complain_overflow_signed, /* complain_on_overflow */
957
   bfd_elf_generic_reloc, /* special_function */
958
   "DTPREL16",    /* name */
959
   false,     /* partial_inplace */
960
   0xffff,    /* src_mask */
961
   0xffff,    /* dst_mask */
962
   false),    /* pcrel_offset */
963
964
  /* Creates a 64-bit offset in the got for the displacement
965
     from TP to the target.  */
966
  HOWTO (R_ALPHA_GOTTPREL,  /* type */
967
   0,     /* rightshift */
968
   2,     /* size */
969
   16,      /* bitsize */
970
   false,     /* pc_relative */
971
   0,     /* bitpos */
972
   complain_overflow_signed, /* complain_on_overflow */
973
   bfd_elf_generic_reloc, /* special_function */
974
   "GOTTPREL",    /* name */
975
   false,     /* partial_inplace */
976
   0xffff,    /* src_mask */
977
   0xffff,    /* dst_mask */
978
   false),    /* pcrel_offset */
979
980
  /* A dynamic relocation for a displacement from TP to the target.  */
981
  HOWTO (R_ALPHA_TPREL64, /* type */
982
   0,     /* rightshift */
983
   8,     /* size */
984
   64,      /* bitsize */
985
   false,     /* pc_relative */
986
   0,     /* bitpos */
987
   complain_overflow_bitfield, /* complain_on_overflow */
988
   bfd_elf_generic_reloc, /* special_function */
989
   "TPREL64",   /* name */
990
   false,     /* partial_inplace */
991
   MINUS_ONE,   /* src_mask */
992
   MINUS_ONE,   /* dst_mask */
993
   false),    /* pcrel_offset */
994
995
  /* The high 16 bits of the displacement from TP to the target.  */
996
  HOWTO (R_ALPHA_TPRELHI, /* type */
997
   0,     /* rightshift */
998
   2,     /* size */
999
   16,      /* bitsize */
1000
   false,     /* pc_relative */
1001
   0,     /* bitpos */
1002
   complain_overflow_signed, /* complain_on_overflow */
1003
   bfd_elf_generic_reloc, /* special_function */
1004
   "TPRELHI",   /* name */
1005
   false,     /* partial_inplace */
1006
   0xffff,    /* src_mask */
1007
   0xffff,    /* dst_mask */
1008
   false),    /* pcrel_offset */
1009
1010
  /* The low 16 bits of the displacement from TP to the target.  */
1011
  HOWTO (R_ALPHA_TPRELLO, /* type */
1012
   0,     /* rightshift */
1013
   2,     /* size */
1014
   16,      /* bitsize */
1015
   false,     /* pc_relative */
1016
   0,     /* bitpos */
1017
   complain_overflow_dont, /* complain_on_overflow */
1018
   bfd_elf_generic_reloc, /* special_function */
1019
   "TPRELLO",   /* name */
1020
   false,     /* partial_inplace */
1021
   0xffff,    /* src_mask */
1022
   0xffff,    /* dst_mask */
1023
   false),    /* pcrel_offset */
1024
1025
  /* A 16-bit displacement from TP to the target.  */
1026
  HOWTO (R_ALPHA_TPREL16, /* type */
1027
   0,     /* rightshift */
1028
   2,     /* size */
1029
   16,      /* bitsize */
1030
   false,     /* pc_relative */
1031
   0,     /* bitpos */
1032
   complain_overflow_signed, /* complain_on_overflow */
1033
   bfd_elf_generic_reloc, /* special_function */
1034
   "TPREL16",   /* name */
1035
   false,     /* partial_inplace */
1036
   0xffff,    /* src_mask */
1037
   0xffff,    /* dst_mask */
1038
   false),    /* pcrel_offset */
1039
1040
  /* A dynamic relocation to set a 64-bit field to the value returned by
1041
     the resolver whose address is the addend.  */
1042
  HOWTO (R_ALPHA_IRELATIVE,
1043
   0,
1044
   0,
1045
   0,
1046
   false,
1047
   0,
1048
   complain_overflow_dont,
1049
   bfd_elf_generic_reloc,
1050
   "IRELATIVE",
1051
   false,
1052
   0,
1053
   0,
1054
   true),
1055
};
1056
1057
/* A mapping from BFD reloc types to Alpha ELF reloc types.  */
1058
1059
struct elf_reloc_map
1060
{
1061
  bfd_reloc_code_real_type bfd_reloc_val;
1062
  int elf_reloc_val;
1063
};
1064
1065
static const struct elf_reloc_map elf64_alpha_reloc_map[] =
1066
{
1067
  {BFD_RELOC_NONE,      R_ALPHA_NONE},
1068
  {BFD_RELOC_32,      R_ALPHA_REFLONG},
1069
  {BFD_RELOC_64,      R_ALPHA_REFQUAD},
1070
  {BFD_RELOC_CTOR,      R_ALPHA_REFQUAD},
1071
  {BFD_RELOC_GPREL32,     R_ALPHA_GPREL32},
1072
  {BFD_RELOC_ALPHA_ELF_LITERAL,   R_ALPHA_LITERAL},
1073
  {BFD_RELOC_ALPHA_LITUSE,    R_ALPHA_LITUSE},
1074
  {BFD_RELOC_ALPHA_GPDISP,    R_ALPHA_GPDISP},
1075
  {BFD_RELOC_23_PCREL_S2,   R_ALPHA_BRADDR},
1076
  {BFD_RELOC_ALPHA_HINT,    R_ALPHA_HINT},
1077
  {BFD_RELOC_16_PCREL,      R_ALPHA_SREL16},
1078
  {BFD_RELOC_32_PCREL,      R_ALPHA_SREL32},
1079
  {BFD_RELOC_64_PCREL,      R_ALPHA_SREL64},
1080
  {BFD_RELOC_ALPHA_GPREL_HI16,    R_ALPHA_GPRELHIGH},
1081
  {BFD_RELOC_ALPHA_GPREL_LO16,    R_ALPHA_GPRELLOW},
1082
  {BFD_RELOC_GPREL16,     R_ALPHA_GPREL16},
1083
  {BFD_RELOC_ALPHA_BRSGP,   R_ALPHA_BRSGP},
1084
  {BFD_RELOC_ALPHA_TLSGD,   R_ALPHA_TLSGD},
1085
  {BFD_RELOC_ALPHA_TLSLDM,    R_ALPHA_TLSLDM},
1086
  {BFD_RELOC_ALPHA_DTPMOD64,    R_ALPHA_DTPMOD64},
1087
  {BFD_RELOC_ALPHA_GOTDTPREL16,   R_ALPHA_GOTDTPREL},
1088
  {BFD_RELOC_ALPHA_DTPREL64,    R_ALPHA_DTPREL64},
1089
  {BFD_RELOC_ALPHA_DTPREL_HI16,   R_ALPHA_DTPRELHI},
1090
  {BFD_RELOC_ALPHA_DTPREL_LO16,   R_ALPHA_DTPRELLO},
1091
  {BFD_RELOC_ALPHA_DTPREL16,    R_ALPHA_DTPREL16},
1092
  {BFD_RELOC_ALPHA_GOTTPREL16,    R_ALPHA_GOTTPREL},
1093
  {BFD_RELOC_ALPHA_TPREL64,   R_ALPHA_TPREL64},
1094
  {BFD_RELOC_ALPHA_TPREL_HI16,    R_ALPHA_TPRELHI},
1095
  {BFD_RELOC_ALPHA_TPREL_LO16,    R_ALPHA_TPRELLO},
1096
  {BFD_RELOC_ALPHA_TPREL16,   R_ALPHA_TPREL16},
1097
  {BFD_RELOC_IRELATIVE,     R_ALPHA_IRELATIVE},
1098
};
1099
1100
/* Given a BFD reloc type, return a HOWTO structure.  */
1101
1102
static reloc_howto_type *
1103
elf64_alpha_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1104
           bfd_reloc_code_real_type code)
1105
0
{
1106
0
  const struct elf_reloc_map *i, *e;
1107
0
  i = e = elf64_alpha_reloc_map;
1108
0
  e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
1109
0
  for (; i != e; ++i)
1110
0
    {
1111
0
      if (i->bfd_reloc_val == code)
1112
0
  return &elf64_alpha_howto_table[i->elf_reloc_val];
1113
0
    }
1114
0
  return 0;
1115
0
}
1116
1117
static reloc_howto_type *
1118
elf64_alpha_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1119
           const char *r_name)
1120
0
{
1121
0
  unsigned int i;
1122
1123
0
  for (i = 0;
1124
0
       i < (sizeof (elf64_alpha_howto_table)
1125
0
      / sizeof (elf64_alpha_howto_table[0]));
1126
0
       i++)
1127
0
    if (elf64_alpha_howto_table[i].name != NULL
1128
0
  && strcasecmp (elf64_alpha_howto_table[i].name, r_name) == 0)
1129
0
      return &elf64_alpha_howto_table[i];
1130
1131
0
  return NULL;
1132
0
}
1133
1134
/* Given an Alpha ELF reloc type, fill in an arelent structure.  */
1135
1136
static bool
1137
elf64_alpha_info_to_howto (bfd *abfd, arelent *cache_ptr,
1138
         Elf_Internal_Rela *dst)
1139
0
{
1140
0
  unsigned r_type = ELF64_R_TYPE(dst->r_info);
1141
1142
0
  if (r_type >= R_ALPHA_max)
1143
0
    {
1144
      /* xgettext:c-format */
1145
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1146
0
        abfd, r_type);
1147
0
      bfd_set_error (bfd_error_bad_value);
1148
0
      return false;
1149
0
    }
1150
0
  cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1151
0
  return true;
1152
0
}
1153
1154
/* These two relocations create a two-word entry in the got.  */
1155
#define alpha_got_entry_size(r_type) \
1156
0
  (r_type == R_ALPHA_TLSGD || r_type == R_ALPHA_TLSLDM ? 16 : 8)
1157
1158
/* Return the address of the word at OFF bytes into GOTENT's got slot.
1159
   The got is sized repeatedly and its subsections re-merged as relaxation
1160
   proceeds, so check that the slot really lies inside the subsection
1161
   rather than writing past the end of it.  */
1162
1163
static bfd_byte *
1164
alpha_got_slot (struct alpha_elf_got_entry *gotent, unsigned int off)
1165
0
{
1166
0
  struct alpha_elf_obj_tdata *td = alpha_elf_tdata (gotent->gotobj);
1167
1168
0
  BFD_ASSERT (td->got->contents != NULL);
1169
0
  BFD_ASSERT (gotent->got_offset >= 0);
1170
0
  BFD_ASSERT ((bfd_vma) gotent->got_offset + off + 8 <= td->got->size);
1171
0
  return td->got->contents + gotent->got_offset + off;
1172
0
}
1173
1174
/* This is PT_TLS segment p_vaddr.  */
1175
#define alpha_get_dtprel_base(info) \
1176
0
  (elf_hash_table (info)->tls_sec->vma)
1177
1178
/* Main program TLS (whose template starts at PT_TLS p_vaddr)
1179
   is assigned offset round(16, PT_TLS p_align).  */
1180
#define alpha_get_tprel_base(info) \
1181
0
  (elf_hash_table (info)->tls_sec->vma          \
1182
0
   - align_power ((bfd_vma) 16,           \
1183
0
      elf_hash_table (info)->tls_sec->alignment_power))
1184

1185
/* Handle an Alpha specific section when reading an object file.  This
1186
   is called when bfd_section_from_shdr finds a section with an unknown
1187
   type.  */
1188
1189
static bool
1190
elf64_alpha_section_from_shdr (bfd *abfd,
1191
             Elf_Internal_Shdr *hdr,
1192
             const char *name,
1193
             int shindex)
1194
78
{
1195
78
  asection *newsect;
1196
1197
  /* There ought to be a place to keep ELF backend specific flags, but
1198
     at the moment there isn't one.  We just keep track of the
1199
     sections by their name, instead.  Fortunately, the ABI gives
1200
     suggested names for all the MIPS specific sections, so we will
1201
     probably get away with this.  */
1202
78
  switch (hdr->sh_type)
1203
78
    {
1204
0
    case SHT_ALPHA_DEBUG:
1205
0
      if (strcmp (name, ".mdebug") != 0)
1206
0
  return false;
1207
0
      break;
1208
78
    default:
1209
78
      return false;
1210
78
    }
1211
1212
0
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1213
0
    return false;
1214
0
  newsect = hdr->bfd_section;
1215
1216
0
  if (hdr->sh_type == SHT_ALPHA_DEBUG)
1217
0
    {
1218
0
      if (!bfd_set_section_flags (newsect,
1219
0
          bfd_section_flags (newsect) | SEC_DEBUGGING))
1220
0
  return false;
1221
0
    }
1222
1223
0
  return true;
1224
0
}
1225
1226
/* Convert Alpha specific section flags to bfd internal section flags.  */
1227
1228
static bool
1229
elf64_alpha_section_flags (const Elf_Internal_Shdr *hdr)
1230
101
{
1231
101
  if (hdr->sh_flags & SHF_ALPHA_GPREL)
1232
36
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
1233
1234
101
  return true;
1235
101
}
1236
1237
/* Set the correct type for an Alpha ELF section.  We do this by the
1238
   section name, which is a hack, but ought to work.  */
1239
1240
static bool
1241
elf64_alpha_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
1242
0
{
1243
0
  register const char *name;
1244
1245
0
  name = bfd_section_name (sec);
1246
1247
0
  if (strcmp (name, ".mdebug") == 0)
1248
0
    {
1249
0
      hdr->sh_type = SHT_ALPHA_DEBUG;
1250
      /* In a shared object on Irix 5.3, the .mdebug section has an
1251
   entsize of 0.  FIXME: Does this matter?  */
1252
0
      if ((abfd->flags & DYNAMIC) != 0 )
1253
0
  hdr->sh_entsize = 0;
1254
0
      else
1255
0
  hdr->sh_entsize = 1;
1256
0
    }
1257
0
  else if ((sec->flags & SEC_SMALL_DATA)
1258
0
     || strcmp (name, ".sdata") == 0
1259
0
     || strcmp (name, ".sbss") == 0
1260
0
     || strcmp (name, ".lit4") == 0
1261
0
     || strcmp (name, ".lit8") == 0)
1262
0
    hdr->sh_flags |= SHF_ALPHA_GPREL;
1263
1264
0
  return true;
1265
0
}
1266
1267
/* Hook called by the linker routine which adds symbols from an object
1268
   file.  We use it to put .comm items in .sbss, and not .bss.  */
1269
1270
static bool
1271
elf64_alpha_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
1272
           Elf_Internal_Sym *sym,
1273
           const char **namep ATTRIBUTE_UNUSED,
1274
           flagword *flagsp ATTRIBUTE_UNUSED,
1275
           asection **secp, bfd_vma *valp)
1276
0
{
1277
0
  if (sym->st_shndx == SHN_COMMON
1278
0
      && !bfd_link_relocatable (info)
1279
0
      && sym->st_size <= elf_gp_size (abfd))
1280
0
    {
1281
      /* Common symbols less than or equal to -G nn bytes are
1282
   automatically put into .sbss.  */
1283
1284
0
      asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1285
1286
0
      if (scomm == NULL)
1287
0
  {
1288
0
    scomm = bfd_make_section_with_flags (abfd, ".scommon",
1289
0
                 (SEC_ALLOC
1290
0
            | SEC_IS_COMMON
1291
0
            | SEC_SMALL_DATA
1292
0
            | SEC_LINKER_CREATED));
1293
0
    if (scomm == NULL)
1294
0
      return false;
1295
0
  }
1296
1297
0
      *secp = scomm;
1298
0
      *valp = sym->st_size;
1299
0
    }
1300
1301
0
  return true;
1302
0
}
1303
1304
/* Create the .got section.  */
1305
1306
static bool
1307
elf64_alpha_create_got_section (bfd *abfd,
1308
        struct bfd_link_info *info ATTRIBUTE_UNUSED)
1309
0
{
1310
0
  flagword flags;
1311
0
  asection *s;
1312
1313
0
  if (! is_alpha_elf (abfd))
1314
0
    return false;
1315
1316
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1317
0
     | SEC_LINKER_CREATED);
1318
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
1319
0
  if (s == NULL
1320
0
      || !bfd_set_section_alignment (s, 3))
1321
0
    return false;
1322
1323
0
  alpha_elf_tdata (abfd)->got = s;
1324
1325
  /* Make sure the object's gotobj is set to itself so that we default
1326
     to every object with its own .got.  We'll merge .gots later once
1327
     we've collected each object's info.  */
1328
0
  alpha_elf_tdata (abfd)->gotobj = abfd;
1329
1330
0
  return true;
1331
0
}
1332
1333
/* Create all the dynamic sections.  */
1334
1335
static bool
1336
elf64_alpha_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
1337
0
{
1338
0
  asection *s;
1339
0
  flagword flags;
1340
0
  struct elf_link_hash_entry *h;
1341
1342
0
  if (! is_alpha_elf (abfd))
1343
0
    return false;
1344
1345
  /* We need to create .plt, .rela.plt, .got, and .rela.got sections.  */
1346
1347
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1348
0
     | SEC_LINKER_CREATED
1349
0
     | (elf64_alpha_use_secureplt ? SEC_READONLY : 0));
1350
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", flags);
1351
0
  elf_hash_table (info)->splt = s;
1352
0
  if (s == NULL || ! bfd_set_section_alignment (s, 4))
1353
0
    return false;
1354
1355
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1356
     .plt section.  */
1357
0
  h = _bfd_elf_define_linkage_sym (abfd, info, s,
1358
0
           "_PROCEDURE_LINKAGE_TABLE_");
1359
0
  elf_hash_table (info)->hplt = h;
1360
0
  if (h == NULL)
1361
0
    return false;
1362
1363
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1364
0
     | SEC_LINKER_CREATED | SEC_READONLY);
1365
0
  s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt", flags);
1366
0
  elf_hash_table (info)->srelplt = s;
1367
0
  if (s == NULL || ! bfd_set_section_alignment (s, 3))
1368
0
    return false;
1369
1370
0
  if (elf64_alpha_use_secureplt)
1371
0
    {
1372
0
      flags = SEC_ALLOC | SEC_LINKER_CREATED;
1373
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
1374
0
      elf_hash_table (info)->sgotplt = s;
1375
0
      if (s == NULL || ! bfd_set_section_alignment (s, 3))
1376
0
  return false;
1377
0
    }
1378
1379
  /* We may or may not have created a .got section for this object, but
1380
     we definitely havn't done the rest of the work.  */
1381
1382
0
  if (alpha_elf_tdata(abfd)->gotobj == NULL)
1383
0
    {
1384
0
      if (!elf64_alpha_create_got_section (abfd, info))
1385
0
  return false;
1386
0
    }
1387
1388
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1389
0
     | SEC_LINKER_CREATED | SEC_READONLY);
1390
0
  s = bfd_make_section_anyway_with_flags (abfd, ".rela.got", flags);
1391
0
  elf_hash_table (info)->srelgot = s;
1392
0
  if (s == NULL
1393
0
      || !bfd_set_section_alignment (s, 3))
1394
0
    return false;
1395
1396
  /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1397
     dynobj's .got section.  We don't do this in the linker script
1398
     because we don't want to define the symbol if we are not creating
1399
     a global offset table.  */
1400
0
  h = _bfd_elf_define_linkage_sym (abfd, info, alpha_elf_tdata(abfd)->got,
1401
0
           "_GLOBAL_OFFSET_TABLE_");
1402
0
  elf_hash_table (info)->hgot = h;
1403
0
  if (h == NULL)
1404
0
    return false;
1405
1406
0
  return true;
1407
0
}
1408

1409
/* Read ECOFF debugging information from a .mdebug section into a
1410
   ecoff_debug_info structure.  */
1411
1412
static bool
1413
elf64_alpha_read_ecoff_info (bfd *abfd, asection *section,
1414
           struct ecoff_debug_info *debug)
1415
0
{
1416
0
  HDRR *symhdr;
1417
0
  const struct ecoff_debug_swap *swap;
1418
0
  char *ext_hdr = NULL;
1419
1420
0
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1421
0
  memset (debug, 0, sizeof (*debug));
1422
1423
0
  ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
1424
0
  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1425
0
    goto error_return;
1426
1427
0
  if (! bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1428
0
          swap->external_hdr_size))
1429
0
    goto error_return;
1430
1431
0
  symhdr = &debug->symbolic_header;
1432
0
  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1433
1434
  /* The symbolic header contains absolute file offsets and sizes to
1435
     read.  */
1436
0
#define READ(ptr, offset, count, size, type)        \
1437
0
  do                  \
1438
0
    {                 \
1439
0
      size_t amt;             \
1440
0
      debug->ptr = NULL;            \
1441
0
      if (symhdr->count == 0)           \
1442
0
  break;               \
1443
0
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
1444
0
  {               \
1445
0
    bfd_set_error (bfd_error_file_too_big);     \
1446
0
    goto error_return;            \
1447
0
  }                \
1448
0
      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)   \
1449
0
  goto error_return;           \
1450
0
      debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt);  \
1451
0
      if (debug->ptr == NULL)           \
1452
0
  goto error_return;           \
1453
0
    } while (0)
1454
1455
0
  READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1456
0
  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1457
0
  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1458
0
  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1459
0
  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1460
0
  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1461
0
  union aux_ext *);
1462
0
  READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1463
0
  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1464
0
  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1465
0
  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1466
0
  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1467
0
#undef READ
1468
1469
0
  debug->fdr = NULL;
1470
1471
0
  return true;
1472
1473
0
 error_return:
1474
0
  free (ext_hdr);
1475
0
  _bfd_ecoff_free_ecoff_debug_info (debug);
1476
0
  return false;
1477
0
}
1478
1479
/* Alpha ELF local labels start with '$'.  */
1480
1481
static bool
1482
elf64_alpha_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
1483
0
{
1484
0
  return name[0] == '$';
1485
0
}
1486
1487
static bool
1488
elf64_alpha_find_nearest_line (bfd *abfd, asymbol **symbols,
1489
             asection *section, bfd_vma offset,
1490
             const char **filename_ptr,
1491
             const char **functionname_ptr,
1492
             unsigned int *line_ptr,
1493
             unsigned int *discriminator_ptr)
1494
0
{
1495
0
  asection *msec;
1496
1497
0
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
1498
0
             filename_ptr, functionname_ptr,
1499
0
             line_ptr, discriminator_ptr,
1500
0
             dwarf_debug_sections,
1501
0
             &elf_tdata (abfd)->dwarf2_find_line_info)
1502
0
      == 1)
1503
0
    return true;
1504
1505
0
  msec = bfd_get_section_by_name (abfd, ".mdebug");
1506
0
  if (msec != NULL)
1507
0
    {
1508
0
      flagword origflags;
1509
0
      struct alpha_elf_find_line *fi;
1510
0
      const struct ecoff_debug_swap * const swap =
1511
0
  get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1512
1513
      /* If we are called during a link, alpha_elf_final_link may have
1514
   cleared the SEC_HAS_CONTENTS field.  We force it back on here
1515
   if appropriate (which it normally will be).  */
1516
0
      origflags = msec->flags;
1517
0
      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
1518
0
  msec->flags |= SEC_HAS_CONTENTS;
1519
1520
0
      fi = alpha_elf_tdata (abfd)->find_line_info;
1521
0
      if (fi == NULL)
1522
0
  {
1523
0
    bfd_size_type external_fdr_size;
1524
0
    char *fraw_src;
1525
0
    char *fraw_end;
1526
0
    struct fdr *fdr_ptr;
1527
0
    bfd_size_type amt = sizeof (struct alpha_elf_find_line);
1528
1529
0
    fi = (struct alpha_elf_find_line *) bfd_zalloc (abfd, amt);
1530
0
    if (fi == NULL)
1531
0
      {
1532
0
        msec->flags = origflags;
1533
0
        return false;
1534
0
      }
1535
1536
0
    if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
1537
0
      {
1538
0
        msec->flags = origflags;
1539
0
        return false;
1540
0
      }
1541
1542
    /* Swap in the FDR information.  */
1543
0
    amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
1544
0
    fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
1545
0
    if (fi->d.fdr == NULL)
1546
0
      {
1547
0
        msec->flags = origflags;
1548
0
        return false;
1549
0
      }
1550
0
    external_fdr_size = swap->external_fdr_size;
1551
0
    fdr_ptr = fi->d.fdr;
1552
0
    fraw_src = (char *) fi->d.external_fdr;
1553
0
    fraw_end = (fraw_src
1554
0
          + fi->d.symbolic_header.ifdMax * external_fdr_size);
1555
0
    for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
1556
0
      (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
1557
1558
0
    alpha_elf_tdata (abfd)->find_line_info = fi;
1559
0
  }
1560
1561
0
      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
1562
0
          &fi->i, filename_ptr, functionname_ptr,
1563
0
          line_ptr))
1564
0
  {
1565
0
    msec->flags = origflags;
1566
0
    return true;
1567
0
  }
1568
1569
0
      msec->flags = origflags;
1570
0
    }
1571
1572
  /* Fall back on the generic ELF find_nearest_line routine.  */
1573
1574
0
  return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
1575
0
             filename_ptr, functionname_ptr,
1576
0
             line_ptr, discriminator_ptr);
1577
0
}
1578

1579
/* Structure used to pass information to alpha_elf_output_extsym.  */
1580
1581
struct extsym_info
1582
{
1583
  bfd *abfd;
1584
  struct bfd_link_info *info;
1585
  struct ecoff_debug_info *debug;
1586
  const struct ecoff_debug_swap *swap;
1587
  bool failed;
1588
};
1589
1590
static bool
1591
elf64_alpha_output_extsym (struct elf_link_hash_entry *x, void * data)
1592
0
{
1593
0
  struct alpha_elf_link_hash_entry *h = (struct alpha_elf_link_hash_entry *) x;
1594
0
  struct extsym_info *einfo = (struct extsym_info *) data;
1595
0
  bool strip;
1596
0
  asection *sec, *output_section;
1597
1598
0
  if (h->root.indx == -2)
1599
0
    strip = false;
1600
0
  else if ((h->root.def_dynamic
1601
0
      || h->root.ref_dynamic
1602
0
      || h->root.root.type == bfd_link_hash_new)
1603
0
     && !h->root.def_regular
1604
0
     && !h->root.ref_regular)
1605
0
    strip = true;
1606
0
  else if (einfo->info->strip == strip_all
1607
0
     || (einfo->info->strip == strip_some
1608
0
         && bfd_hash_lookup (einfo->info->keep_hash,
1609
0
           h->root.root.root.string,
1610
0
           false, false) == NULL))
1611
0
    strip = true;
1612
0
  else
1613
0
    strip = false;
1614
1615
0
  if (strip)
1616
0
    return true;
1617
1618
0
  if (h->esym.ifd == -2)
1619
0
    {
1620
0
      h->esym.jmptbl = 0;
1621
0
      h->esym.cobol_main = 0;
1622
0
      h->esym.weakext = 0;
1623
0
      h->esym.reserved = 0;
1624
0
      h->esym.ifd = ifdNil;
1625
0
      h->esym.asym.value = 0;
1626
0
      h->esym.asym.st = stGlobal;
1627
1628
0
      if (h->root.root.type != bfd_link_hash_defined
1629
0
    && h->root.root.type != bfd_link_hash_defweak)
1630
0
  h->esym.asym.sc = scAbs;
1631
0
      else
1632
0
  {
1633
0
    const char *name;
1634
1635
0
    sec = h->root.root.u.def.section;
1636
0
    output_section = sec->output_section;
1637
1638
    /* When making a shared library and symbol h is the one from
1639
       the another shared library, OUTPUT_SECTION may be null.  */
1640
0
    if (output_section == NULL)
1641
0
      h->esym.asym.sc = scUndefined;
1642
0
    else
1643
0
      {
1644
0
        name = bfd_section_name (output_section);
1645
1646
0
        if (strcmp (name, ".text") == 0)
1647
0
    h->esym.asym.sc = scText;
1648
0
        else if (strcmp (name, ".data") == 0)
1649
0
    h->esym.asym.sc = scData;
1650
0
        else if (strcmp (name, ".sdata") == 0)
1651
0
    h->esym.asym.sc = scSData;
1652
0
        else if (strcmp (name, ".rodata") == 0
1653
0
           || strcmp (name, ".rdata") == 0)
1654
0
    h->esym.asym.sc = scRData;
1655
0
        else if (strcmp (name, ".bss") == 0)
1656
0
    h->esym.asym.sc = scBss;
1657
0
        else if (strcmp (name, ".sbss") == 0)
1658
0
    h->esym.asym.sc = scSBss;
1659
0
        else if (strcmp (name, ".init") == 0)
1660
0
    h->esym.asym.sc = scInit;
1661
0
        else if (strcmp (name, ".fini") == 0)
1662
0
    h->esym.asym.sc = scFini;
1663
0
        else
1664
0
    h->esym.asym.sc = scAbs;
1665
0
      }
1666
0
  }
1667
1668
0
      h->esym.asym.reserved = 0;
1669
0
      h->esym.asym.index = indexNil;
1670
0
    }
1671
1672
0
  if (h->root.root.type == bfd_link_hash_common)
1673
0
    h->esym.asym.value = h->root.root.u.c.size;
1674
0
  else if (h->root.root.type == bfd_link_hash_defined
1675
0
     || h->root.root.type == bfd_link_hash_defweak)
1676
0
    {
1677
0
      if (h->esym.asym.sc == scCommon)
1678
0
  h->esym.asym.sc = scBss;
1679
0
      else if (h->esym.asym.sc == scSCommon)
1680
0
  h->esym.asym.sc = scSBss;
1681
1682
0
      sec = h->root.root.u.def.section;
1683
0
      output_section = sec->output_section;
1684
0
      if (output_section != NULL)
1685
0
  h->esym.asym.value = (h->root.root.u.def.value
1686
0
            + sec->output_offset
1687
0
            + output_section->vma);
1688
0
      else
1689
0
  h->esym.asym.value = 0;
1690
0
    }
1691
1692
0
  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1693
0
              h->root.root.root.string,
1694
0
              &h->esym))
1695
0
    {
1696
0
      einfo->failed = true;
1697
0
      return false;
1698
0
    }
1699
1700
0
  return true;
1701
0
}
1702

1703
/* Search for and possibly create a got entry.  */
1704
1705
static struct alpha_elf_got_entry *
1706
get_got_entry (bfd *abfd, struct alpha_elf_link_hash_entry *h,
1707
         unsigned long r_type, unsigned long r_symndx,
1708
         bfd_vma r_addend)
1709
0
{
1710
0
  struct alpha_elf_got_entry *gotent;
1711
0
  struct alpha_elf_got_entry **slot;
1712
1713
0
  if (h)
1714
0
    slot = &h->got_entries;
1715
0
  else
1716
0
    {
1717
      /* This is a local .got entry -- record for merge.  */
1718
1719
0
      struct alpha_elf_got_entry **local_got_entries;
1720
1721
0
      local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1722
0
      if (!local_got_entries)
1723
0
  {
1724
0
    bfd_size_type size;
1725
0
    Elf_Internal_Shdr *symtab_hdr;
1726
1727
0
    symtab_hdr = &elf_symtab_hdr (abfd);
1728
0
    size = symtab_hdr->sh_info;
1729
0
    size *= sizeof (struct alpha_elf_got_entry *);
1730
1731
0
    local_got_entries
1732
0
      = (struct alpha_elf_got_entry **) bfd_zalloc (abfd, size);
1733
0
    if (!local_got_entries)
1734
0
      return NULL;
1735
1736
0
    alpha_elf_tdata (abfd)->local_got_entries = local_got_entries;
1737
0
  }
1738
1739
0
      slot = &local_got_entries[r_symndx];
1740
0
    }
1741
1742
0
  for (gotent = *slot; gotent ; gotent = gotent->next)
1743
0
    if (gotent->gotobj == abfd
1744
0
  && gotent->reloc_type == r_type
1745
0
  && gotent->addend == r_addend)
1746
0
      break;
1747
1748
0
  if (!gotent)
1749
0
    {
1750
0
      int entry_size;
1751
0
      size_t amt;
1752
1753
0
      amt = sizeof (struct alpha_elf_got_entry);
1754
0
      gotent = (struct alpha_elf_got_entry *) bfd_zalloc (abfd, amt);
1755
0
      if (!gotent)
1756
0
  return NULL;
1757
1758
0
      gotent->gotobj = abfd;
1759
0
      gotent->addend = r_addend;
1760
0
      gotent->got_offset = -1;
1761
0
      gotent->plt_offset = -1;
1762
0
      gotent->use_count = 1;
1763
0
      gotent->reloc_type = r_type;
1764
1765
0
      gotent->next = *slot;
1766
0
      *slot = gotent;
1767
1768
0
      entry_size = alpha_got_entry_size (r_type);
1769
0
      alpha_elf_tdata (abfd)->total_got_size += entry_size;
1770
0
      if (!h)
1771
0
  alpha_elf_tdata(abfd)->local_got_size += entry_size;
1772
0
    }
1773
0
  else
1774
0
    gotent->use_count += 1;
1775
1776
0
  return gotent;
1777
0
}
1778
1779
static bool
1780
elf64_alpha_want_plt (struct alpha_elf_link_hash_entry *ah)
1781
0
{
1782
0
  return ((ah->root.type == STT_FUNC
1783
0
    || ah->root.type == STT_GNU_IFUNC
1784
0
    || ah->root.root.type == bfd_link_hash_undefweak
1785
0
    || ah->root.root.type == bfd_link_hash_undefined)
1786
0
    && (ah->flags & ALPHA_ELF_LINK_HASH_LU_PLT) != 0
1787
0
    && (ah->flags & ~ALPHA_ELF_LINK_HASH_LU_PLT) == 0);
1788
0
}
1789
1790
/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
1791
   Don't do so for code sections.  We want to keep ordering of LITERAL/LITUSE
1792
   as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
1793
   relocs to be sorted.  */
1794
1795
static bool
1796
elf64_alpha_sort_relocs_p (asection *sec)
1797
0
{
1798
0
  return (sec->flags & SEC_CODE) == 0;
1799
0
}
1800
1801
1802
/* True if a relocation refers to an IFUNC defined in a regular object.  H
1803
   describes the symbol when it is global and SYM when it is local.  An
1804
   undefined symbol can carry the IFUNC type from a .type directive; it is
1805
   treated as an ordinary undefined function, as the generic code does when
1806
   it writes it out.  */
1807
1808
static bool
1809
elf64_alpha_ifunc_p (struct alpha_elf_link_hash_entry *h,
1810
         const Elf_Internal_Sym *sym)
1811
0
{
1812
0
  if (h != NULL)
1813
0
    return h->root.type == STT_GNU_IFUNC && h->root.def_regular;
1814
1815
0
  return sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
1816
0
}
1817
1818
/* True if a relocation of type R_TYPE against an IFUNC can end up as an
1819
   R_ALPHA_IRELATIVE.  */
1820
1821
static bool
1822
elf64_alpha_ifunc_reloc_p (unsigned long r_type)
1823
0
{
1824
0
  return r_type == R_ALPHA_LITERAL || r_type == R_ALPHA_REFQUAD;
1825
0
}
1826
1827
/* True if a reference to the IFUNC described by H (global) or SYM (local)
1828
   needs its R_ALPHA_IRELATIVE in .rela.iplt: the link is not PIC, so
1829
   nothing has reserved a slot for it alongside the R_ALPHA_RELATIVE it
1830
   would otherwise have had, and the symbol is not preemptible.  */
1831
1832
static bool
1833
elf64_alpha_ifunc_irelplt_p (struct alpha_elf_link_hash_entry *h,
1834
           const Elf_Internal_Sym *sym,
1835
           struct bfd_link_info *info)
1836
0
{
1837
0
  if (!elf64_alpha_ifunc_p (h, sym) || bfd_link_pic (info))
1838
0
    return false;
1839
1840
0
  return h == NULL || !alpha_elf_dynamic_symbol_p (&h->root, info);
1841
0
}
1842
1843
/* Create .rela.iplt.  Called from check_relocs, since the section has to
1844
   exist before input sections are mapped to output sections.  One that
1845
   stays empty is stripped by elf64_alpha_late_size_sections.  */
1846
1847
static bool
1848
elf64_alpha_create_irelplt (struct bfd_link_info *info)
1849
0
{
1850
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
1851
0
  const struct elf_backend_data *bed;
1852
0
  asection *s;
1853
1854
0
  if (htab->irelplt != NULL)
1855
0
    return true;
1856
1857
0
  bed = get_elf_backend_data (info->output_bfd);
1858
0
  s = bfd_make_section_anyway_with_flags (htab->dynobj, ".rela.iplt",
1859
0
            (bed->dynamic_sec_flags
1860
0
             | SEC_READONLY));
1861
0
  if (s == NULL || !bfd_set_section_alignment (s, 3))
1862
0
    return false;
1863
1864
0
  htab->irelplt = s;
1865
0
  return true;
1866
0
}
1867
1868
/* Note that an R_ALPHA_IRELATIVE will be applied to SEC.  A read-only one
1869
   needs the page made writable at startup, which only the dynamic linker
1870
   can do.  Only a data relocation can land in such a section; the GOT is
1871
   always writable.  */
1872
1873
static void
1874
elf64_alpha_note_irelative (struct bfd_link_info *info, asection *sec)
1875
0
{
1876
0
  if ((sec->flags & SEC_READONLY) != 0)
1877
0
    {
1878
0
      info->flags |= DF_TEXTREL;
1879
0
      info->callbacks->minfo
1880
0
  (_("%pB: R_ALPHA_IRELATIVE in read-only section `%pA'\n"),
1881
0
   sec->owner, sec);
1882
0
    }
1883
0
}
1884
1885
/* Note the references to an IFUNC that need an R_ALPHA_IRELATIVE in
1886
   .rela.iplt: mark the got entries that get one and count the data
1887
   relocations that do.  Only the input sections that make it into the
1888
   output are relocated, and only their references are emitted, so this runs
1889
   once the output sections are known rather than in check_relocs, which
1890
   still sees the sections a linker script goes on to discard.  */
1891
1892
static bool
1893
elf64_alpha_scan_irelative (struct bfd_link_info *info)
1894
0
{
1895
0
  struct alpha_elf_link_hash_table *htab = alpha_elf_hash_table (info);
1896
0
  bfd *abfd;
1897
1898
0
  htab->irelplt_data_size = 0;
1899
1900
0
  for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
1901
0
    {
1902
0
      Elf_Internal_Shdr *symtab_hdr;
1903
0
      asection *sec;
1904
1905
0
      if (!is_alpha_elf (abfd) || (abfd->flags & DYNAMIC) != 0)
1906
0
  continue;
1907
1908
0
      symtab_hdr = &elf_symtab_hdr (abfd);
1909
1910
0
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
1911
0
  {
1912
0
    Elf_Internal_Rela *relocs, *rel, *relend;
1913
0
    bool noted = false;
1914
1915
    /* The sections elf_link_input_bfd will relocate.  One a linker
1916
       script discarded and one discarded as a duplicate both have the
1917
       absolute section for their output.  */
1918
0
    if ((sec->flags & (SEC_ALLOC | SEC_RELOC | SEC_HAS_CONTENTS))
1919
0
        != (SEC_ALLOC | SEC_RELOC | SEC_HAS_CONTENTS)
1920
0
        || sec->reloc_count == 0
1921
0
        || sec->output_section == NULL
1922
0
        || bfd_is_abs_section (sec->output_section))
1923
0
      continue;
1924
1925
0
    relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
1926
0
                info->keep_memory);
1927
0
    if (relocs == NULL)
1928
0
      return false;
1929
1930
0
    relend = relocs + sec->reloc_count;
1931
0
    for (rel = relocs; rel < relend; rel++)
1932
0
      {
1933
0
        unsigned long r_type = ELF64_R_TYPE (rel->r_info);
1934
0
        unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
1935
0
        struct alpha_elf_link_hash_entry *h = NULL;
1936
0
        Elf_Internal_Sym *isym = NULL;
1937
0
        struct alpha_elf_got_entry *gotent;
1938
1939
0
        if (!elf64_alpha_ifunc_reloc_p (r_type))
1940
0
    continue;
1941
1942
0
        if (r_symndx < symtab_hdr->sh_info)
1943
0
    {
1944
0
      isym = bfd_sym_from_r_symndx
1945
0
        (&elf_hash_table (info)->sym_cache, abfd, r_symndx);
1946
0
      if (isym == NULL)
1947
0
        return false;
1948
0
    }
1949
0
        else
1950
0
    {
1951
0
      h = (alpha_elf_sym_hashes (abfd)
1952
0
           [r_symndx - symtab_hdr->sh_info]);
1953
0
      while (h->root.root.type == bfd_link_hash_indirect
1954
0
       || h->root.root.type == bfd_link_hash_warning)
1955
0
        h = ((struct alpha_elf_link_hash_entry *)
1956
0
       h->root.root.u.i.link);
1957
0
    }
1958
1959
        /* A symbol defined in a section the linker discarded reaches
1960
     relocate_section as an error rather than as a relocation to
1961
     emit, so nothing may be reserved for it.  */
1962
0
        if (h != NULL)
1963
0
    {
1964
0
      if ((h->root.root.type == bfd_link_hash_defined
1965
0
           || h->root.root.type == bfd_link_hash_defweak)
1966
0
          && discarded_section (h->root.root.u.def.section))
1967
0
        continue;
1968
0
    }
1969
0
        else if (isym->st_shndx < SHN_LORESERVE
1970
0
           || isym->st_shndx > SHN_HIRESERVE)
1971
0
    {
1972
      /* An index above the reserved range is one that
1973
         bfd_sym_from_r_symndx expanded from an SHN_XINDEX
1974
         entry; elf_link_input_bfd looks that one up too.  */
1975
0
      asection *isec;
1976
1977
0
      isec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1978
0
      if (isec != NULL && discarded_section (isec))
1979
0
        continue;
1980
0
    }
1981
1982
0
        if (!elf64_alpha_ifunc_irelplt_p (h, isym, info))
1983
0
    continue;
1984
1985
0
        if (r_type == R_ALPHA_REFQUAD)
1986
0
    {
1987
0
      htab->irelplt_data_size += sizeof (Elf64_External_Rela);
1988
0
      if (!noted)
1989
0
        {
1990
0
          elf64_alpha_note_irelative (info, sec);
1991
0
          noted = true;
1992
0
        }
1993
0
      continue;
1994
0
    }
1995
1996
        /* However many references reach a got entry, it holds one
1997
     address and gets one R_ALPHA_IRELATIVE.  Find it the way
1998
     relocate_section will.  */
1999
0
        if (h != NULL)
2000
0
    gotent = h->got_entries;
2001
0
        else if (alpha_elf_tdata (abfd)->local_got_entries != NULL)
2002
0
    gotent = alpha_elf_tdata (abfd)->local_got_entries[r_symndx];
2003
0
        else
2004
0
    gotent = NULL;
2005
2006
0
        for (; gotent != NULL; gotent = gotent->next)
2007
0
    if (gotent->gotobj == alpha_elf_tdata (abfd)->gotobj
2008
0
        && gotent->reloc_type == r_type
2009
0
        && gotent->addend == rel->r_addend)
2010
0
      {
2011
0
        gotent->irel_live = 1;
2012
0
        break;
2013
0
      }
2014
0
      }
2015
2016
0
    if (elf_section_data (sec)->relocs != relocs)
2017
0
      free (relocs);
2018
0
  }
2019
0
    }
2020
2021
0
  return true;
2022
0
}
2023
2024
/* Reserve space in .rela.iplt for the got entries of a global IFUNC that
2025
   elf64_alpha_scan_irelative marked.  */
2026
2027
static bool
2028
elf64_alpha_size_irelative (struct alpha_elf_link_hash_entry *h,
2029
          struct bfd_link_info *info)
2030
0
{
2031
0
  asection *irelplt = elf_hash_table (info)->irelplt;
2032
0
  struct alpha_elf_got_entry *gotent;
2033
2034
0
  for (gotent = h->got_entries; gotent != NULL; gotent = gotent->next)
2035
0
    if (gotent->irel_live && gotent->use_count > 0)
2036
0
      irelplt->size += sizeof (Elf64_External_Rela);
2037
2038
0
  return true;
2039
0
}
2040
2041
/* Size .rela.iplt.  The data references are counted once, since nothing
2042
   later changes them; the got entries of a global IFUNC can still merge
2043
   when the GOTs are re-merged during relaxation, so the part of the size
2044
   that comes from them is recomputed alongside them.  */
2045
2046
static void
2047
elf64_alpha_size_irelplt_section (struct bfd_link_info *info)
2048
0
{
2049
0
  struct alpha_elf_link_hash_table *htab = alpha_elf_hash_table (info);
2050
0
  asection *irelplt = elf_hash_table (info)->irelplt;
2051
0
  bfd *i;
2052
2053
0
  if (irelplt == NULL)
2054
0
    return;
2055
2056
0
  irelplt->size = htab->irelplt_data_size;
2057
0
  alpha_elf_link_hash_traverse (htab, elf64_alpha_size_irelative, info);
2058
2059
  /* A local symbol has no hash entry for the traversal to reach.  */
2060
0
  for (i = htab->got_list; i != NULL; i = alpha_elf_tdata (i)->got_link_next)
2061
0
    {
2062
0
      bfd *j;
2063
2064
0
      for (j = i; j != NULL; j = alpha_elf_tdata (j)->in_got_link_next)
2065
0
  {
2066
0
    struct alpha_elf_got_entry **local_got_entries, *gotent;
2067
0
    int k, n;
2068
2069
0
    local_got_entries = alpha_elf_tdata (j)->local_got_entries;
2070
0
    if (local_got_entries == NULL)
2071
0
      continue;
2072
2073
0
    for (k = 0, n = elf_symtab_hdr (j).sh_info; k < n; ++k)
2074
0
      for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
2075
0
        if (gotent->irel_live && gotent->use_count > 0)
2076
0
    irelplt->size += sizeof (Elf64_External_Rela);
2077
0
  }
2078
0
    }
2079
0
}
2080
2081
/* Handle dynamic relocations when doing an Alpha ELF link.  */
2082
2083
static bool
2084
elf64_alpha_check_relocs (bfd *abfd, struct bfd_link_info *info,
2085
        asection *sec, const Elf_Internal_Rela *relocs)
2086
0
{
2087
0
  bfd *dynobj;
2088
0
  asection *sreloc;
2089
0
  Elf_Internal_Shdr *symtab_hdr;
2090
0
  struct alpha_elf_link_hash_entry **sym_hashes;
2091
0
  const Elf_Internal_Rela *rel, *relend;
2092
2093
0
  if (bfd_link_relocatable (info))
2094
0
    return true;
2095
2096
0
  BFD_ASSERT (is_alpha_elf (abfd));
2097
2098
0
  dynobj = _bfd_elf_link_dynobj (info);
2099
0
  if (dynobj == NULL)
2100
0
    return false;
2101
2102
0
  sreloc = NULL;
2103
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2104
0
  sym_hashes = alpha_elf_sym_hashes (abfd);
2105
2106
0
  relend = relocs + sec->reloc_count;
2107
0
  for (rel = relocs; rel < relend; ++rel)
2108
0
    {
2109
0
      enum {
2110
0
  NEED_GOT = 1,
2111
0
  NEED_GOT_ENTRY = 2,
2112
0
  NEED_DYNREL = 4
2113
0
      };
2114
2115
0
      unsigned long r_symndx, r_type;
2116
0
      struct alpha_elf_link_hash_entry *h;
2117
0
      const Elf_Internal_Sym *isym;
2118
0
      unsigned int gotent_flags;
2119
0
      bool maybe_dynamic;
2120
0
      unsigned int need;
2121
0
      bfd_vma addend;
2122
2123
0
      r_symndx = ELF64_R_SYM (rel->r_info);
2124
0
      r_type = ELF64_R_TYPE (rel->r_info);
2125
0
      isym = NULL;
2126
0
      if (r_symndx < symtab_hdr->sh_info)
2127
0
  {
2128
0
    h = NULL;
2129
2130
    /* A local IFUNC needs the same treatment as a global one, but
2131
       only the symbol table says that it is one.  */
2132
0
    if (elf64_alpha_ifunc_reloc_p (r_type))
2133
0
      {
2134
0
        isym = bfd_sym_from_r_symndx (&elf_hash_table (info)->sym_cache,
2135
0
              abfd, r_symndx);
2136
0
        if (isym == NULL)
2137
0
    return false;
2138
0
      }
2139
0
  }
2140
0
      else
2141
0
  {
2142
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2143
2144
0
    while (h->root.root.type == bfd_link_hash_indirect
2145
0
     || h->root.root.type == bfd_link_hash_warning)
2146
0
      h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2147
2148
    /* PR15323, ref flags aren't set for references in the same
2149
       object.  */
2150
0
    h->root.ref_regular = 1;
2151
0
  }
2152
2153
      /* We can only get preliminary data on whether a symbol is
2154
   locally or externally defined, as not all of the input files
2155
   have yet been processed.  Do something with what we know, as
2156
   this may help reduce memory usage and processing time later.  */
2157
0
      maybe_dynamic = false;
2158
0
      if (h && ((bfd_link_pic (info)
2159
0
     && (!info->symbolic
2160
0
         || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2161
0
    || !h->root.def_regular
2162
0
    || h->root.root.type == bfd_link_hash_defweak))
2163
0
  maybe_dynamic = true;
2164
2165
      /* Whether a global IFUNC needs .rela.iplt is not settled until
2166
   sizing, so create the section for every reference that might.  */
2167
0
      if (elf64_alpha_ifunc_reloc_p (r_type)
2168
0
    && elf64_alpha_ifunc_p (h, isym)
2169
0
    && !bfd_link_pic (info)
2170
0
    && !elf64_alpha_create_irelplt (info))
2171
0
  return false;
2172
2173
0
      need = 0;
2174
0
      gotent_flags = 0;
2175
0
      addend = rel->r_addend;
2176
2177
0
      switch (r_type)
2178
0
  {
2179
0
  case R_ALPHA_LITERAL:
2180
0
    need = NEED_GOT | NEED_GOT_ENTRY;
2181
2182
    /* Remember how this literal is used from its LITUSEs.
2183
       This will be important when it comes to decide if we can
2184
       create a .plt entry for a function symbol.  */
2185
0
    while (++rel < relend && ELF64_R_TYPE (rel->r_info) == R_ALPHA_LITUSE)
2186
0
      if (rel->r_addend >= 1 && rel->r_addend <= 6)
2187
0
        gotent_flags |= 1 << rel->r_addend;
2188
0
    --rel;
2189
2190
    /* No LITUSEs -- presumably the address is used somehow.  */
2191
0
    if (gotent_flags == 0)
2192
0
      gotent_flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
2193
0
    break;
2194
2195
0
  case R_ALPHA_GPDISP:
2196
0
  case R_ALPHA_GPREL16:
2197
0
  case R_ALPHA_GPREL32:
2198
0
  case R_ALPHA_GPRELHIGH:
2199
0
  case R_ALPHA_GPRELLOW:
2200
0
  case R_ALPHA_BRSGP:
2201
0
    need = NEED_GOT;
2202
0
    break;
2203
2204
0
  case R_ALPHA_REFLONG:
2205
0
  case R_ALPHA_REFQUAD:
2206
0
    if (r_type == R_ALPHA_REFQUAD && elf64_alpha_ifunc_p (h, isym))
2207
0
      {
2208
        /* A reference that becomes an R_ALPHA_IRELATIVE in .rela.iplt
2209
     is sized later, from the sections that survive.  Whether a
2210
     global one does is not settled until then, so record it
2211
     either way; a local symbol never becomes dynamic.  */
2212
0
        if (h != NULL || !elf64_alpha_ifunc_irelplt_p (NULL, isym, info))
2213
0
    need = NEED_DYNREL;
2214
0
      }
2215
0
    else if (bfd_link_pic (info) || maybe_dynamic)
2216
0
      need = NEED_DYNREL;
2217
0
    break;
2218
2219
0
  case R_ALPHA_TLSLDM:
2220
    /* The symbol for a TLSLDM reloc is ignored.  Collapse the
2221
       reloc to the STN_UNDEF (0) symbol so that they all match.  */
2222
0
    r_symndx = STN_UNDEF;
2223
0
    h = 0;
2224
0
    maybe_dynamic = false;
2225
    /* FALLTHRU */
2226
2227
0
  case R_ALPHA_TLSGD:
2228
0
  case R_ALPHA_GOTDTPREL:
2229
0
    need = NEED_GOT | NEED_GOT_ENTRY;
2230
0
    break;
2231
2232
0
  case R_ALPHA_GOTTPREL:
2233
0
    need = NEED_GOT | NEED_GOT_ENTRY;
2234
0
    gotent_flags = ALPHA_ELF_LINK_HASH_TLS_IE;
2235
0
    if (bfd_link_pic (info))
2236
0
      info->flags |= DF_STATIC_TLS;
2237
0
    break;
2238
2239
0
  case R_ALPHA_TPREL64:
2240
0
    if (bfd_link_dll (info))
2241
0
      {
2242
0
        info->flags |= DF_STATIC_TLS;
2243
0
        need = NEED_DYNREL;
2244
0
      }
2245
0
    else if (maybe_dynamic)
2246
0
      need = NEED_DYNREL;
2247
0
    break;
2248
0
  }
2249
2250
0
      if (need & NEED_GOT)
2251
0
  {
2252
0
    if (alpha_elf_tdata(abfd)->gotobj == NULL)
2253
0
      {
2254
0
        if (!elf64_alpha_create_got_section (abfd, info))
2255
0
    return false;
2256
0
      }
2257
0
  }
2258
2259
0
      if (need & NEED_GOT_ENTRY)
2260
0
  {
2261
0
    struct alpha_elf_got_entry *gotent;
2262
2263
0
    gotent = get_got_entry (abfd, h, r_type, r_symndx, addend);
2264
0
    if (!gotent)
2265
0
      return false;
2266
2267
0
    if (gotent_flags)
2268
0
      {
2269
0
        gotent->flags |= gotent_flags;
2270
0
        if (h)
2271
0
    {
2272
0
      gotent_flags |= h->flags;
2273
0
      h->flags = gotent_flags;
2274
2275
      /* Make a guess as to whether a .plt entry is needed.  */
2276
      /* ??? It appears that we won't make it into
2277
         adjust_dynamic_symbol for symbols that remain
2278
         totally undefined.  Copying this check here means
2279
         we can create a plt entry for them too.  */
2280
0
      h->root.needs_plt
2281
0
        = (maybe_dynamic && elf64_alpha_want_plt (h));
2282
0
    }
2283
0
      }
2284
0
  }
2285
2286
0
      if (need & NEED_DYNREL)
2287
0
  {
2288
    /* We need to create the section here now whether we eventually
2289
       use it or not so that it gets mapped to an output section by
2290
       the linker.  If not used, we'll kill it in size_dynamic_sections.  */
2291
0
    if (sreloc == NULL)
2292
0
      {
2293
0
        sreloc = _bfd_elf_make_dynamic_reloc_section
2294
0
    (sec, dynobj, 3, abfd, /*rela?*/ true);
2295
2296
0
        if (sreloc == NULL)
2297
0
    return false;
2298
0
      }
2299
2300
0
    if (h)
2301
0
      {
2302
        /* Since we havn't seen all of the input symbols yet, we
2303
     don't know whether we'll actually need a dynamic relocation
2304
     entry for this reloc.  So make a record of it.  Once we
2305
     find out if this thing needs dynamic relocation we'll
2306
     expand the relocation sections by the appropriate amount.  */
2307
2308
0
        struct alpha_elf_reloc_entry *rent;
2309
2310
0
        for (rent = h->reloc_entries; rent; rent = rent->next)
2311
0
    if (rent->rtype == r_type && rent->srel == sreloc)
2312
0
      break;
2313
2314
0
        if (!rent)
2315
0
    {
2316
0
      rent = bfd_alloc (abfd, sizeof (*rent));
2317
0
      if (!rent)
2318
0
        return false;
2319
2320
0
      rent->srel = sreloc;
2321
0
      rent->sec = sec;
2322
0
      rent->rtype = r_type;
2323
0
      rent->count = 1;
2324
2325
0
      rent->next = h->reloc_entries;
2326
0
      h->reloc_entries = rent;
2327
0
    }
2328
0
        else
2329
0
    rent->count++;
2330
0
      }
2331
0
    else if (bfd_link_pic (info))
2332
0
      {
2333
        /* If this is a shared library, and the section is to be
2334
     loaded into memory, we need a RELATIVE reloc.  */
2335
0
        sreloc->size += sizeof (Elf64_External_Rela);
2336
0
        if (sec->flags & SEC_READONLY)
2337
0
    {
2338
0
      info->flags |= DF_TEXTREL;
2339
0
      info->callbacks->minfo
2340
0
        (_("%pB: dynamic relocation against a local symbol in "
2341
0
           "read-only section `%pA'\n"),
2342
0
         sec->owner, sec);
2343
0
    }
2344
0
      }
2345
0
  }
2346
0
    }
2347
2348
0
  return true;
2349
0
}
2350
2351
/* Return the section that should be marked against GC for a given
2352
   relocation.  */
2353
2354
static asection *
2355
elf64_alpha_gc_mark_hook (asection *sec, struct bfd_link_info *info,
2356
        struct elf_reloc_cookie *cookie,
2357
        struct elf_link_hash_entry *h, unsigned int symndx)
2358
0
{
2359
  /* These relocations don't really reference a symbol.  Instead we store
2360
     extra data in their addend slot.  Ignore the symbol.  */
2361
0
  switch (ELF64_R_TYPE (cookie->rel->r_info))
2362
0
    {
2363
0
    case R_ALPHA_LITUSE:
2364
0
    case R_ALPHA_GPDISP:
2365
0
    case R_ALPHA_HINT:
2366
0
      return NULL;
2367
0
    }
2368
2369
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
2370
0
}
2371
2372
/* Adjust a symbol defined by a dynamic object and referenced by a
2373
   regular object.  The current definition is in some section of the
2374
   dynamic object, but we're not including those sections.  We have to
2375
   change the definition to something the rest of the link can
2376
   understand.  */
2377
2378
static bool
2379
elf64_alpha_adjust_dynamic_symbol (struct bfd_link_info *info,
2380
           struct elf_link_hash_entry *h)
2381
0
{
2382
0
  bfd *dynobj;
2383
0
  asection *s;
2384
0
  struct alpha_elf_link_hash_entry *ah;
2385
2386
0
  dynobj = elf_hash_table(info)->dynobj;
2387
0
  ah = (struct alpha_elf_link_hash_entry *)h;
2388
2389
  /* Now that we've seen all of the input symbols, finalize our decision
2390
     about whether this symbol should get a .plt entry.  Irritatingly, it
2391
     is common for folk to leave undefined symbols in shared libraries,
2392
     and they still expect lazy binding; accept undefined symbols in lieu
2393
     of STT_FUNC.  */
2394
0
  if (alpha_elf_dynamic_symbol_p (h, info) && elf64_alpha_want_plt (ah))
2395
0
    {
2396
0
      h->needs_plt = true;
2397
2398
0
      s = elf_hash_table(info)->splt;
2399
0
      if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
2400
0
  return false;
2401
2402
      /* We need one plt entry per got subsection.  Delay allocation of
2403
   the actual plt entries until size_plt_section, called from
2404
   size_dynamic_sections or during relaxation.  */
2405
2406
0
      return true;
2407
0
    }
2408
0
  else
2409
0
    h->needs_plt = false;
2410
2411
  /* If this is a weak symbol, and there is a real definition, the
2412
     processor independent code will have arranged for us to see the
2413
     real definition first, and we can just use the same value.  */
2414
0
  if (h->is_weakalias)
2415
0
    {
2416
0
      struct elf_link_hash_entry *def = weakdef (h);
2417
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2418
0
      h->root.u.def.section = def->root.u.def.section;
2419
0
      h->root.u.def.value = def->root.u.def.value;
2420
0
      return true;
2421
0
    }
2422
2423
  /* This is a reference to a symbol defined by a dynamic object which
2424
     is not a function.  The Alpha, since it uses .got entries for all
2425
     symbols even in regular objects, does not need the hackery of a
2426
     .dynbss section and COPY dynamic relocations.  */
2427
2428
0
  return true;
2429
0
}
2430
2431
/* Record STO_ALPHA_NOPV and STO_ALPHA_STD_GPLOAD.  */
2432
2433
static void
2434
elf64_alpha_merge_symbol_attribute (struct elf_link_hash_entry *h,
2435
            unsigned int st_other,
2436
            bool definition,
2437
            bool dynamic)
2438
0
{
2439
0
  if (!dynamic && definition)
2440
0
    h->other = ((h->other & ELF_ST_VISIBILITY (-1))
2441
0
    | (st_other & ~ELF_ST_VISIBILITY (-1)));
2442
0
}
2443
2444
/* Symbol versioning can create new symbols, and make our old symbols
2445
   indirect to the new ones.  Consolidate the got and reloc information
2446
   in these situations.  */
2447
2448
static void
2449
elf64_alpha_copy_indirect_symbol (struct bfd_link_info *info,
2450
          struct elf_link_hash_entry *dir,
2451
          struct elf_link_hash_entry *ind)
2452
0
{
2453
0
  struct alpha_elf_link_hash_entry *hi
2454
0
    = (struct alpha_elf_link_hash_entry *) ind;
2455
0
  struct alpha_elf_link_hash_entry *hs
2456
0
    = (struct alpha_elf_link_hash_entry *) dir;
2457
2458
  /* Do the merging in the superclass.  */
2459
0
  _bfd_elf_link_hash_copy_indirect(info, dir, ind);
2460
2461
  /* Merge the flags.  Whee.  */
2462
0
  hs->flags |= hi->flags;
2463
2464
  /* ??? It's unclear to me what's really supposed to happen when
2465
     "merging" defweak and defined symbols, given that we don't
2466
     actually throw away the defweak.  This more-or-less copies
2467
     the logic related to got and plt entries in the superclass.  */
2468
0
  if (ind->root.type != bfd_link_hash_indirect)
2469
0
    return;
2470
2471
  /* Merge the .got entries.  Cannibalize the old symbol's list in
2472
     doing so, since we don't need it anymore.  */
2473
2474
0
  if (hs->got_entries == NULL)
2475
0
    hs->got_entries = hi->got_entries;
2476
0
  else
2477
0
    {
2478
0
      struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
2479
2480
0
      gsh = hs->got_entries;
2481
0
      for (gi = hi->got_entries; gi ; gi = gin)
2482
0
  {
2483
0
    gin = gi->next;
2484
0
    for (gs = gsh; gs ; gs = gs->next)
2485
0
      if (gi->gotobj == gs->gotobj
2486
0
    && gi->reloc_type == gs->reloc_type
2487
0
    && gi->addend == gs->addend)
2488
0
        {
2489
0
    gs->use_count += gi->use_count;
2490
0
    goto got_found;
2491
0
        }
2492
0
    gi->next = hs->got_entries;
2493
0
    hs->got_entries = gi;
2494
0
  got_found:;
2495
0
  }
2496
0
    }
2497
0
  hi->got_entries = NULL;
2498
2499
  /* And similar for the reloc entries.  */
2500
2501
0
  if (hs->reloc_entries == NULL)
2502
0
    hs->reloc_entries = hi->reloc_entries;
2503
0
  else
2504
0
    {
2505
0
      struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
2506
2507
0
      rsh = hs->reloc_entries;
2508
0
      for (ri = hi->reloc_entries; ri ; ri = rin)
2509
0
  {
2510
0
    rin = ri->next;
2511
0
    for (rs = rsh; rs ; rs = rs->next)
2512
0
      if (ri->rtype == rs->rtype && ri->srel == rs->srel)
2513
0
        {
2514
0
    rs->count += ri->count;
2515
0
    goto found_reloc;
2516
0
        }
2517
0
    ri->next = hs->reloc_entries;
2518
0
    hs->reloc_entries = ri;
2519
0
  found_reloc:;
2520
0
  }
2521
0
    }
2522
0
  hi->reloc_entries = NULL;
2523
0
}
2524
2525
/* Is it possible to merge two object file's .got tables?  */
2526
2527
static bool
2528
elf64_alpha_can_merge_gots (bfd *a, bfd *b)
2529
0
{
2530
0
  int total = alpha_elf_tdata (a)->total_got_size;
2531
0
  bfd *bsub;
2532
2533
  /* Trivial quick fallout test.  */
2534
0
  if (total + alpha_elf_tdata (b)->total_got_size <= MAX_GOT_SIZE)
2535
0
    return true;
2536
2537
  /* By their nature, local .got entries cannot be merged.  */
2538
0
  if ((total += alpha_elf_tdata (b)->local_got_size) > MAX_GOT_SIZE)
2539
0
    return false;
2540
2541
  /* Failing the common trivial comparison, we must effectively
2542
     perform the merge.  Not actually performing the merge means that
2543
     we don't have to store undo information in case we fail.  */
2544
0
  for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2545
0
    {
2546
0
      struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
2547
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (bsub);
2548
0
      int i, n;
2549
2550
0
      n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
2551
0
      for (i = 0; i < n; ++i)
2552
0
  {
2553
0
    struct alpha_elf_got_entry *ae, *be;
2554
0
    struct alpha_elf_link_hash_entry *h;
2555
2556
0
    h = hashes[i];
2557
0
    while (h->root.root.type == bfd_link_hash_indirect
2558
0
     || h->root.root.type == bfd_link_hash_warning)
2559
0
      h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2560
2561
0
    for (be = h->got_entries; be ; be = be->next)
2562
0
      {
2563
0
        if (be->use_count == 0)
2564
0
    continue;
2565
0
        if (be->gotobj != b)
2566
0
    continue;
2567
2568
0
        for (ae = h->got_entries; ae ; ae = ae->next)
2569
0
    if (ae->gotobj == a
2570
0
        && ae->reloc_type == be->reloc_type
2571
0
        && ae->addend == be->addend)
2572
0
      goto global_found;
2573
2574
0
        total += alpha_got_entry_size (be->reloc_type);
2575
0
        if (total > MAX_GOT_SIZE)
2576
0
    return false;
2577
0
      global_found:;
2578
0
      }
2579
0
  }
2580
0
    }
2581
2582
0
  return true;
2583
0
}
2584
2585
/* Actually merge two .got tables.  */
2586
2587
static void
2588
elf64_alpha_merge_gots (bfd *a, bfd *b)
2589
0
{
2590
0
  int total = alpha_elf_tdata (a)->total_got_size;
2591
0
  bfd *bsub;
2592
2593
  /* Remember local expansion.  */
2594
0
  {
2595
0
    int e = alpha_elf_tdata (b)->local_got_size;
2596
0
    total += e;
2597
0
    alpha_elf_tdata (a)->local_got_size += e;
2598
0
  }
2599
2600
0
  for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2601
0
    {
2602
0
      struct alpha_elf_got_entry **local_got_entries;
2603
0
      struct alpha_elf_link_hash_entry **hashes;
2604
0
      Elf_Internal_Shdr *symtab_hdr;
2605
0
      int i, n;
2606
2607
      /* Let the local .got entries know they are part of a new subsegment.  */
2608
0
      local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
2609
0
      if (local_got_entries)
2610
0
  {
2611
0
    n = elf_symtab_hdr (bsub).sh_info;
2612
0
    for (i = 0; i < n; ++i)
2613
0
      {
2614
0
        struct alpha_elf_got_entry *ent;
2615
0
        for (ent = local_got_entries[i]; ent; ent = ent->next)
2616
0
    ent->gotobj = a;
2617
0
      }
2618
0
  }
2619
2620
      /* Merge the global .got entries.  */
2621
0
      hashes = alpha_elf_sym_hashes (bsub);
2622
0
      symtab_hdr = &elf_symtab_hdr (bsub);
2623
2624
0
      n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
2625
0
      for (i = 0; i < n; ++i)
2626
0
  {
2627
0
    struct alpha_elf_got_entry *ae, *be, **pbe, **start;
2628
0
    struct alpha_elf_link_hash_entry *h;
2629
2630
0
    h = hashes[i];
2631
0
    while (h->root.root.type == bfd_link_hash_indirect
2632
0
     || h->root.root.type == bfd_link_hash_warning)
2633
0
      h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2634
2635
0
    pbe = start = &h->got_entries;
2636
0
    while ((be = *pbe) != NULL)
2637
0
      {
2638
0
        if (be->use_count == 0)
2639
0
    {
2640
0
      *pbe = be->next;
2641
0
      memset (be, 0xa5, sizeof (*be));
2642
0
      goto kill;
2643
0
    }
2644
0
        if (be->gotobj != b)
2645
0
    goto next;
2646
2647
0
        for (ae = *start; ae ; ae = ae->next)
2648
0
    if (ae->gotobj == a
2649
0
        && ae->reloc_type == be->reloc_type
2650
0
        && ae->addend == be->addend)
2651
0
      {
2652
0
        ae->flags |= be->flags;
2653
0
        ae->irel_live |= be->irel_live;
2654
0
        ae->use_count += be->use_count;
2655
0
        *pbe = be->next;
2656
0
        memset (be, 0xa5, sizeof (*be));
2657
0
        goto kill;
2658
0
      }
2659
0
        be->gotobj = a;
2660
0
        total += alpha_got_entry_size (be->reloc_type);
2661
2662
0
      next:;
2663
0
        pbe = &be->next;
2664
0
      kill:;
2665
0
      }
2666
0
  }
2667
2668
0
      alpha_elf_tdata (bsub)->gotobj = a;
2669
0
    }
2670
0
  alpha_elf_tdata (a)->total_got_size = total;
2671
2672
  /* Merge the two in_got chains.  */
2673
0
  {
2674
0
    bfd *next;
2675
2676
0
    bsub = a;
2677
0
    while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
2678
0
      bsub = next;
2679
2680
0
    alpha_elf_tdata (bsub)->in_got_link_next = b;
2681
0
  }
2682
0
}
2683
2684
/* Calculate the offsets for the got entries.  */
2685
2686
static bool
2687
elf64_alpha_calc_got_offsets_for_symbol (struct alpha_elf_link_hash_entry *h,
2688
           void * arg ATTRIBUTE_UNUSED)
2689
0
{
2690
0
  struct alpha_elf_got_entry *gotent;
2691
2692
0
  for (gotent = h->got_entries; gotent; gotent = gotent->next)
2693
0
    if (gotent->use_count > 0)
2694
0
      {
2695
0
  struct alpha_elf_obj_tdata *td;
2696
0
  bfd_size_type *plge;
2697
2698
0
  td = alpha_elf_tdata (gotent->gotobj);
2699
0
  plge = &td->got->size;
2700
0
  gotent->got_offset = *plge;
2701
0
  *plge += alpha_got_entry_size (gotent->reloc_type);
2702
0
      }
2703
2704
0
  return true;
2705
0
}
2706
2707
static void
2708
elf64_alpha_calc_got_offsets (struct bfd_link_info *info)
2709
0
{
2710
0
  bfd *i, *got_list;
2711
0
  struct alpha_elf_link_hash_table * htab;
2712
2713
0
  htab = alpha_elf_hash_table (info);
2714
0
  if (htab == NULL)
2715
0
    return;
2716
0
  got_list = htab->got_list;
2717
2718
  /* First, zero out the .got sizes, as we may be recalculating the
2719
     .got after optimizing it.  */
2720
0
  for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2721
0
    alpha_elf_tdata(i)->got->size = 0;
2722
2723
  /* Next, fill in the offsets for all the global entries.  */
2724
0
  alpha_elf_link_hash_traverse (htab,
2725
0
        elf64_alpha_calc_got_offsets_for_symbol,
2726
0
        NULL);
2727
2728
  /* Finally, fill in the offsets for the local entries.  */
2729
0
  for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2730
0
    {
2731
0
      bfd_size_type got_offset = alpha_elf_tdata(i)->got->size;
2732
0
      bfd *j;
2733
2734
0
      for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2735
0
  {
2736
0
    struct alpha_elf_got_entry **local_got_entries, *gotent;
2737
0
    int k, n;
2738
2739
0
    local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2740
0
    if (!local_got_entries)
2741
0
      continue;
2742
2743
0
    for (k = 0, n = elf_symtab_hdr (j).sh_info; k < n; ++k)
2744
0
      for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
2745
0
        if (gotent->use_count > 0)
2746
0
    {
2747
0
      gotent->got_offset = got_offset;
2748
0
      got_offset += alpha_got_entry_size (gotent->reloc_type);
2749
0
    }
2750
0
  }
2751
2752
0
      alpha_elf_tdata(i)->got->size = got_offset;
2753
0
    }
2754
0
}
2755
2756
/* Constructs the gots.  */
2757
2758
static bool
2759
elf64_alpha_size_got_sections (struct bfd_link_info *info,
2760
             bool may_merge)
2761
0
{
2762
0
  bfd *i, *got_list, *cur_got_obj = NULL;
2763
0
  struct alpha_elf_link_hash_table * htab;
2764
2765
0
  htab = alpha_elf_hash_table (info);
2766
0
  if (htab == NULL)
2767
0
    return false;
2768
0
  got_list = htab->got_list;
2769
2770
  /* On the first time through, pretend we have an existing got list
2771
     consisting of all of the input files.  */
2772
0
  if (got_list == NULL)
2773
0
    {
2774
0
      for (i = info->input_bfds; i ; i = i->link.next)
2775
0
  {
2776
0
    bfd *this_got;
2777
2778
0
    if (! is_alpha_elf (i))
2779
0
      continue;
2780
2781
0
    this_got = alpha_elf_tdata (i)->gotobj;
2782
0
    if (this_got == NULL)
2783
0
      continue;
2784
2785
    /* We are assuming no merging has yet occurred.  */
2786
0
    BFD_ASSERT (this_got == i);
2787
2788
0
    if (alpha_elf_tdata (this_got)->total_got_size > MAX_GOT_SIZE)
2789
0
      {
2790
        /* Yikes! A single object file has too many entries.  */
2791
0
        _bfd_error_handler
2792
    /* xgettext:c-format */
2793
0
    (_("%pB: .got subsegment exceeds 64K (size %d)"),
2794
0
     i, alpha_elf_tdata (this_got)->total_got_size);
2795
0
        return false;
2796
0
      }
2797
2798
0
    if (got_list == NULL)
2799
0
      got_list = this_got;
2800
0
    else
2801
0
      alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
2802
0
    cur_got_obj = this_got;
2803
0
  }
2804
2805
      /* Strange degenerate case of no got references.  */
2806
0
      if (got_list == NULL)
2807
0
  return true;
2808
2809
0
      htab->got_list = got_list;
2810
0
    }
2811
2812
0
  cur_got_obj = got_list;
2813
0
  if (cur_got_obj == NULL)
2814
0
    return false;
2815
2816
0
  if (may_merge)
2817
0
    {
2818
0
      i = alpha_elf_tdata(cur_got_obj)->got_link_next;
2819
0
      while (i != NULL)
2820
0
  {
2821
0
    if (elf64_alpha_can_merge_gots (cur_got_obj, i))
2822
0
      {
2823
0
        elf64_alpha_merge_gots (cur_got_obj, i);
2824
2825
0
        alpha_elf_tdata(i)->got->size = 0;
2826
0
        i = alpha_elf_tdata(i)->got_link_next;
2827
0
        alpha_elf_tdata(cur_got_obj)->got_link_next = i;
2828
0
      }
2829
0
    else
2830
0
      {
2831
0
        cur_got_obj = i;
2832
0
        i = alpha_elf_tdata(i)->got_link_next;
2833
0
      }
2834
0
  }
2835
0
    }
2836
2837
  /* Once the gots have been merged, fill in the got offsets for
2838
     everything therein.  */
2839
0
  elf64_alpha_calc_got_offsets (info);
2840
2841
0
  return true;
2842
0
}
2843
2844
static bool
2845
elf64_alpha_size_plt_section_1 (struct alpha_elf_link_hash_entry *h,
2846
        void * data)
2847
0
{
2848
0
  asection *splt = (asection *) data;
2849
0
  struct alpha_elf_got_entry *gotent;
2850
0
  bool saw_one = false;
2851
2852
  /* If we didn't need an entry before, we still don't.  */
2853
0
  if (!h->root.needs_plt)
2854
0
    return true;
2855
2856
  /* For each LITERAL got entry still in use, allocate a plt entry.  */
2857
0
  for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2858
0
    if (gotent->reloc_type == R_ALPHA_LITERAL
2859
0
  && gotent->use_count > 0)
2860
0
      {
2861
0
  if (splt->size == 0)
2862
0
    splt->size = PLT_HEADER_SIZE;
2863
0
  gotent->plt_offset = splt->size;
2864
0
  splt->size += PLT_ENTRY_SIZE;
2865
0
  saw_one = true;
2866
0
      }
2867
2868
  /* If there weren't any, there's no longer a need for the PLT entry.  */
2869
0
  if (!saw_one)
2870
0
    h->root.needs_plt = false;
2871
2872
0
  return true;
2873
0
}
2874
2875
/* Called from relax_section to rebuild the PLT in light of potential changes
2876
   in the function's status.  */
2877
2878
static void
2879
elf64_alpha_size_plt_section (struct bfd_link_info *info)
2880
0
{
2881
0
  asection *splt, *spltrel, *sgotplt;
2882
0
  unsigned long entries;
2883
0
  struct alpha_elf_link_hash_table * htab;
2884
2885
0
  htab = alpha_elf_hash_table (info);
2886
0
  if (htab == NULL)
2887
0
    return;
2888
2889
0
  splt = elf_hash_table(info)->splt;
2890
0
  if (splt == NULL)
2891
0
    return;
2892
2893
0
  splt->size = 0;
2894
2895
0
  alpha_elf_link_hash_traverse (htab,
2896
0
        elf64_alpha_size_plt_section_1, splt);
2897
2898
  /* Every plt entry requires a JMP_SLOT relocation.  */
2899
0
  spltrel = elf_hash_table(info)->srelplt;
2900
0
  entries = 0;
2901
0
  if (splt->size)
2902
0
    {
2903
0
      if (elf64_alpha_use_secureplt)
2904
0
  entries = (splt->size - NEW_PLT_HEADER_SIZE) / NEW_PLT_ENTRY_SIZE;
2905
0
      else
2906
0
  entries = (splt->size - OLD_PLT_HEADER_SIZE) / OLD_PLT_ENTRY_SIZE;
2907
0
    }
2908
0
  spltrel->size = entries * sizeof (Elf64_External_Rela);
2909
2910
  /* When using the secureplt, we need two words somewhere in the data
2911
     segment for the dynamic linker to tell us where to go.  This is the
2912
     entire contents of the .got.plt section.  */
2913
0
  if (elf64_alpha_use_secureplt)
2914
0
    {
2915
0
      sgotplt = elf_hash_table(info)->sgotplt;
2916
0
      sgotplt->size = entries ? 16 : 0;
2917
0
    }
2918
0
}
2919
2920
static bool
2921
elf64_alpha_early_size_sections (struct bfd_link_info *info)
2922
0
{
2923
0
  if (bfd_link_relocatable (info))
2924
0
    return true;
2925
2926
  /* Size the .got subsections, but do not allocate their contents here.
2927
     This sizing is not final: relaxation re-runs it, and the re-merge
2928
     there can grow a subsection even though the table as a whole only
2929
     shrinks.  elf64_alpha_final_link allocates the contents once the
2930
     sizes have settled.  */
2931
0
  return elf64_alpha_size_got_sections (info, true);
2932
0
}
2933
2934
/* The number of dynamic relocations required by a static relocation.  */
2935
2936
static int
2937
alpha_dynamic_entries_for_reloc (int r_type, int dynamic, int shared, int pie)
2938
0
{
2939
0
  switch (r_type)
2940
0
    {
2941
    /* May appear in GOT entries.  */
2942
0
    case R_ALPHA_TLSGD:
2943
0
      return (dynamic ? 2 : shared ? 1 : 0);
2944
0
    case R_ALPHA_TLSLDM:
2945
0
      return shared;
2946
0
    case R_ALPHA_LITERAL:
2947
0
      return dynamic || shared;
2948
0
    case R_ALPHA_GOTTPREL:
2949
0
      return dynamic || (shared && !pie);
2950
0
    case R_ALPHA_GOTDTPREL:
2951
0
      return dynamic;
2952
2953
    /* May appear in data sections.  */
2954
0
    case R_ALPHA_REFLONG:
2955
0
    case R_ALPHA_REFQUAD:
2956
0
      return dynamic || shared;
2957
0
    case R_ALPHA_TPREL64:
2958
0
      return dynamic || (shared && !pie);
2959
2960
    /* Everything else is illegal.  We'll issue an error during
2961
       relocate_section.  */
2962
0
    default:
2963
0
      return 0;
2964
0
    }
2965
0
}
2966
2967
/* Work out the sizes of the dynamic relocation entries.  */
2968
2969
static bool
2970
elf64_alpha_calc_dynrel_sizes (struct alpha_elf_link_hash_entry *h,
2971
             struct bfd_link_info *info)
2972
0
{
2973
0
  bool dynamic;
2974
0
  struct alpha_elf_reloc_entry *relent;
2975
0
  unsigned long entries;
2976
2977
  /* If the symbol was defined as a common symbol in a regular object
2978
     file, and there was no definition in any dynamic object, then the
2979
     linker will have allocated space for the symbol in a common
2980
     section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
2981
     set.  This is done for dynamic symbols in
2982
     elf_adjust_dynamic_symbol but this is not done for non-dynamic
2983
     symbols, somehow.  */
2984
0
  if (!h->root.def_regular
2985
0
      && h->root.ref_regular
2986
0
      && !h->root.def_dynamic
2987
0
      && (h->root.root.type == bfd_link_hash_defined
2988
0
    || h->root.root.type == bfd_link_hash_defweak)
2989
0
      && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
2990
0
    h->root.def_regular = 1;
2991
2992
  /* If the symbol is dynamic, we'll need all the relocations in their
2993
     natural form.  If this is a shared object, and it has been forced
2994
     local, we'll need the same number of RELATIVE relocations.  */
2995
0
  dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
2996
2997
  /* If the symbol is a hidden undefined weak, then we never have any
2998
     relocations.  Avoid the loop which may want to add RELATIVE relocs
2999
     based on bfd_link_pic (info).  */
3000
0
  if (h->root.root.type == bfd_link_hash_undefweak && !dynamic)
3001
0
    return true;
3002
3003
0
  for (relent = h->reloc_entries; relent; relent = relent->next)
3004
0
    {
3005
0
      entries = alpha_dynamic_entries_for_reloc (relent->rtype, dynamic,
3006
0
             bfd_link_pic (info),
3007
0
             bfd_link_pie (info));
3008
0
      if (entries)
3009
0
  {
3010
0
    asection *sec = relent->sec;
3011
0
    relent->srel->size +=
3012
0
      entries * sizeof (Elf64_External_Rela) * relent->count;
3013
0
    if ((sec->flags & SEC_READONLY) != 0)
3014
0
      {
3015
0
        info->flags |= DF_TEXTREL;
3016
0
        info->callbacks->minfo
3017
0
    (_("%pB: dynamic relocation against `%pT' in "
3018
0
       "read-only section `%pA'\n"),
3019
0
     sec->owner, h->root.root.root.string, sec);
3020
0
      }
3021
0
  }
3022
0
    }
3023
3024
0
  return true;
3025
0
}
3026
3027
/* Subroutine of elf64_alpha_size_rela_got_section for doing the
3028
   global symbols.  */
3029
3030
static bool
3031
elf64_alpha_size_rela_got_1 (struct alpha_elf_link_hash_entry *h,
3032
           struct bfd_link_info *info)
3033
0
{
3034
0
  bool dynamic;
3035
0
  struct alpha_elf_got_entry *gotent;
3036
0
  unsigned long entries;
3037
3038
  /* If we're using a plt for this symbol, then all of its relocations
3039
     for its got entries go into .rela.plt.  */
3040
0
  if (h->root.needs_plt)
3041
0
    return true;
3042
3043
  /* If the symbol is dynamic, we'll need all the relocations in their
3044
     natural form.  If this is a shared object, and it has been forced
3045
     local, we'll need the same number of RELATIVE relocations.  */
3046
0
  dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
3047
3048
  /* If the symbol is a hidden undefined weak, then we never have any
3049
     relocations.  Avoid the loop which may want to add RELATIVE relocs
3050
     based on bfd_link_pic (info).  */
3051
0
  if (h->root.root.type == bfd_link_hash_undefweak && !dynamic)
3052
0
    return true;
3053
3054
0
  entries = 0;
3055
0
  for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3056
0
    if (gotent->use_count > 0)
3057
0
      entries += alpha_dynamic_entries_for_reloc (gotent->reloc_type, dynamic,
3058
0
              bfd_link_pic (info),
3059
0
              bfd_link_pie (info));
3060
3061
0
  if (entries > 0)
3062
0
    {
3063
0
      asection *srel = elf_hash_table(info)->srelgot;
3064
0
      BFD_ASSERT (srel != NULL);
3065
0
      srel->size += sizeof (Elf64_External_Rela) * entries;
3066
0
    }
3067
3068
0
  return true;
3069
0
}
3070
3071
/* Set the sizes of the dynamic relocation sections.  */
3072
3073
static void
3074
elf64_alpha_size_rela_got_section (struct bfd_link_info *info)
3075
0
{
3076
0
  unsigned long entries;
3077
0
  bfd *i;
3078
0
  asection *srel;
3079
0
  struct alpha_elf_link_hash_table * htab;
3080
3081
0
  htab = alpha_elf_hash_table (info);
3082
0
  if (htab == NULL)
3083
0
    return;
3084
3085
  /* Shared libraries often require RELATIVE relocs, and some relocs
3086
     require attention for the main application as well.  */
3087
3088
0
  entries = 0;
3089
0
  for (i = htab->got_list;
3090
0
       i ; i = alpha_elf_tdata(i)->got_link_next)
3091
0
    {
3092
0
      bfd *j;
3093
3094
0
      for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3095
0
  {
3096
0
    struct alpha_elf_got_entry **local_got_entries, *gotent;
3097
0
    int k, n;
3098
3099
0
    local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3100
0
    if (!local_got_entries)
3101
0
      continue;
3102
3103
0
    for (k = 0, n = elf_symtab_hdr (j).sh_info; k < n; ++k)
3104
0
      for (gotent = local_got_entries[k];
3105
0
     gotent ; gotent = gotent->next)
3106
0
        if (gotent->use_count > 0)
3107
0
    entries += (alpha_dynamic_entries_for_reloc
3108
0
          (gotent->reloc_type, 0, bfd_link_pic (info),
3109
0
           bfd_link_pie (info)));
3110
0
  }
3111
0
    }
3112
3113
0
  srel = elf_hash_table(info)->srelgot;
3114
0
  if (!srel)
3115
0
    {
3116
0
      BFD_ASSERT (entries == 0);
3117
0
      return;
3118
0
    }
3119
0
  srel->size = sizeof (Elf64_External_Rela) * entries;
3120
3121
  /* Now do the non-local symbols.  */
3122
0
  alpha_elf_link_hash_traverse (htab,
3123
0
        elf64_alpha_size_rela_got_1, info);
3124
0
}
3125
3126
/* Set the sizes of the dynamic sections.  */
3127
3128
static bool
3129
elf64_alpha_late_size_sections (struct bfd_link_info *info)
3130
0
{
3131
0
  bfd *dynobj;
3132
0
  asection *s;
3133
0
  bool relplt, relocs;
3134
0
  struct alpha_elf_link_hash_table * htab;
3135
3136
0
  htab = alpha_elf_hash_table (info);
3137
0
  if (htab == NULL)
3138
0
    return false;
3139
3140
0
  dynobj = elf_hash_table(info)->dynobj;
3141
0
  if (dynobj == NULL)
3142
0
    return true;
3143
3144
0
  if (elf_hash_table (info)->dynamic_sections_created)
3145
0
    {
3146
      /* Set the contents of the .interp section to the interpreter.  */
3147
0
      if (bfd_link_executable (info) && !info->nointerp)
3148
0
  {
3149
0
    s = elf_hash_table (info)->interp;
3150
0
    BFD_ASSERT (s != NULL);
3151
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3152
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3153
0
    s->alloced = 1;
3154
0
  }
3155
3156
      /* Now that we've seen all of the input files, we can decide which
3157
   symbols need dynamic relocation entries and which don't.  We've
3158
   collected information in check_relocs that we can now apply to
3159
   size the dynamic relocation sections.  */
3160
0
      alpha_elf_link_hash_traverse (htab,
3161
0
            elf64_alpha_calc_dynrel_sizes, info);
3162
3163
0
      elf64_alpha_size_rela_got_section (info);
3164
0
      elf64_alpha_size_plt_section (info);
3165
0
    }
3166
3167
  /* The sizing above reserves nothing for a non-dynamic IFUNC, since the
3168
     dynamic linker never sees it; its relocations go in .rela.iplt.  */
3169
0
  if (elf_hash_table (info)->irelplt != NULL)
3170
0
    {
3171
0
      if (!elf64_alpha_scan_irelative (info))
3172
0
  return false;
3173
0
      elf64_alpha_size_irelplt_section (info);
3174
0
    }
3175
3176
  /* The check_relocs and adjust_dynamic_symbol entry points have
3177
     determined the sizes of the various dynamic sections.  Allocate
3178
     memory for them.  */
3179
0
  relplt = false;
3180
0
  relocs = false;
3181
0
  for (s = dynobj->sections; s != NULL; s = s->next)
3182
0
    {
3183
0
      const char *name;
3184
3185
0
      if (!(s->flags & SEC_LINKER_CREATED))
3186
0
  continue;
3187
3188
      /* It's OK to base decisions on the section name, because none
3189
   of the dynobj section names depend upon the input files.  */
3190
0
      name = bfd_section_name (s);
3191
3192
      /* The .got subsection of DYNOBJ is sized along with all the other
3193
   got subsections, and relaxation can still change that size.  Leave
3194
   it for elf64_alpha_final_link to allocate.  */
3195
0
      if (strcmp (name, ".got") == 0)
3196
0
  continue;
3197
3198
0
      if (startswith (name, ".rela"))
3199
0
  {
3200
0
    if (s->size != 0)
3201
0
      {
3202
0
        if (strcmp (name, ".rela.plt") == 0)
3203
0
    relplt = true;
3204
0
        else
3205
0
    relocs = true;
3206
3207
        /* We use the reloc_count field as a counter if we need
3208
     to copy relocs into the output file.  */
3209
0
        s->reloc_count = 0;
3210
0
      }
3211
0
  }
3212
0
      else if (! startswith (name, ".got")
3213
0
         && strcmp (name, ".plt") != 0
3214
0
         && strcmp (name, ".dynbss") != 0)
3215
0
  {
3216
    /* It's not one of our dynamic sections, so don't allocate space.  */
3217
0
    continue;
3218
0
  }
3219
3220
0
      if (s->size == 0)
3221
0
  {
3222
    /* If we don't need this section, strip it from the output file.
3223
       This is to handle .rela.bss and .rela.plt.  We must create it
3224
       in create_dynamic_sections, because it must be created before
3225
       the linker maps input sections to output sections.  The
3226
       linker does that before adjust_dynamic_symbol is called, and
3227
       it is that function which decides whether anything needs to
3228
       go into these sections.  */
3229
0
    if (!startswith (name, ".got"))
3230
0
      s->flags |= SEC_EXCLUDE;
3231
0
  }
3232
0
      else if ((s->flags & SEC_HAS_CONTENTS) != 0)
3233
0
  {
3234
    /* Allocate memory for the section contents.  */
3235
0
    s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3236
0
    if (s->contents == NULL)
3237
0
      return false;
3238
0
    s->alloced = 1;
3239
0
  }
3240
0
    }
3241
3242
0
  if (elf_hash_table (info)->dynamic_sections_created)
3243
0
    {
3244
      /* Add some entries to the .dynamic section.  We fill in the
3245
   values later, in elf64_alpha_finish_dynamic_sections, but we
3246
   must add the entries now so that we get the correct size for
3247
   the .dynamic section.  The DT_DEBUG entry is filled in by the
3248
   dynamic linker and used by the debugger.  */
3249
0
#define add_dynamic_entry(TAG, VAL) \
3250
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3251
3252
0
      if (!_bfd_elf_add_dynamic_tags (info, relocs || relplt))
3253
0
  return false;
3254
3255
0
      if (relplt
3256
0
    && elf64_alpha_use_secureplt
3257
0
    && !add_dynamic_entry (DT_ALPHA_PLTRO, 1))
3258
0
  return false;
3259
0
    }
3260
0
#undef add_dynamic_entry
3261
3262
0
  return true;
3263
0
}
3264

3265
/* These functions do relaxation for Alpha ELF.
3266
3267
   Currently I'm only handling what I can do with existing compiler
3268
   and assembler support, which means no instructions are removed,
3269
   though some may be nopped.  At this time GCC does not emit enough
3270
   information to do all of the relaxing that is possible.  It will
3271
   take some not small amount of work for that to happen.
3272
3273
   There are a couple of interesting papers that I once read on this
3274
   subject, that I cannot find references to at the moment, that
3275
   related to Alpha in particular.  They are by David Wall, then of
3276
   DEC WRL.  */
3277
3278
struct alpha_relax_info
3279
{
3280
  bfd *abfd;
3281
  asection *sec;
3282
  bfd_byte *contents;
3283
  Elf_Internal_Shdr *symtab_hdr;
3284
  Elf_Internal_Rela *relocs, *relend;
3285
  struct bfd_link_info *link_info;
3286
  bfd_vma gp;
3287
  bfd *gotobj;
3288
  asection *tsec;
3289
  struct alpha_elf_link_hash_entry *h;
3290
  struct alpha_elf_got_entry **first_gotent;
3291
  struct alpha_elf_got_entry *gotent;
3292
  bool changed_contents;
3293
  bool changed_relocs;
3294
  unsigned char other;
3295
};
3296
3297
static Elf_Internal_Rela *
3298
elf64_alpha_find_reloc_at_ofs (Elf_Internal_Rela *rel,
3299
             Elf_Internal_Rela *relend,
3300
             bfd_vma offset, int type)
3301
0
{
3302
0
  while (rel < relend)
3303
0
    {
3304
0
      if (rel->r_offset == offset
3305
0
    && ELF64_R_TYPE (rel->r_info) == (unsigned int) type)
3306
0
  return rel;
3307
0
      ++rel;
3308
0
    }
3309
0
  return NULL;
3310
0
}
3311
3312
static bool
3313
elf64_alpha_relax_got_load (struct alpha_relax_info *info, bfd_vma symval,
3314
          Elf_Internal_Rela *irel, unsigned long r_type)
3315
0
{
3316
0
  unsigned int insn;
3317
0
  bfd_signed_vma disp;
3318
3319
  /* Get the instruction.  */
3320
0
  insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
3321
3322
0
  if (insn >> 26 != OP_LDQ)
3323
0
    {
3324
0
      reloc_howto_type *howto = elf64_alpha_howto_table + r_type;
3325
0
      _bfd_error_handler
3326
  /* xgettext:c-format */
3327
0
  (_("%pB: %pA+%#" PRIx64 ": warning: "
3328
0
     "%s relocation against unexpected insn"),
3329
0
   info->abfd, info->sec, (uint64_t) irel->r_offset, howto->name);
3330
0
      return true;
3331
0
    }
3332
3333
  /* Can't relax dynamic symbols.  */
3334
0
  if (info->h != NULL
3335
0
      && alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
3336
0
    return true;
3337
3338
  /* Can't use local-exec relocations in shared libraries.  */
3339
0
  if (r_type == R_ALPHA_GOTTPREL
3340
0
      && bfd_link_dll (info->link_info))
3341
0
    return true;
3342
3343
0
  if (r_type == R_ALPHA_LITERAL)
3344
0
    {
3345
      /* Look for nice constant addresses.  This includes the not-uncommon
3346
   special case of 0 for undefweak symbols.  */
3347
0
      if ((info->h && info->h->root.root.type == bfd_link_hash_undefweak)
3348
0
    || (!bfd_link_pic (info->link_info)
3349
0
        && (symval >= (bfd_vma)-0x8000 || symval < 0x8000)))
3350
0
  {
3351
0
    disp = 0;
3352
0
    insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
3353
0
    insn |= (symval & 0xffff);
3354
0
    r_type = R_ALPHA_NONE;
3355
0
  }
3356
0
      else
3357
0
  {
3358
    /* We may only create GPREL relocs during the second pass.  */
3359
0
    if (info->link_info->relax_pass == 0)
3360
0
      return true;
3361
3362
0
    disp = symval - info->gp;
3363
0
    insn = (OP_LDA << 26) | (insn & 0x03ff0000);
3364
0
    r_type = R_ALPHA_GPREL16;
3365
0
  }
3366
0
    }
3367
0
  else
3368
0
    {
3369
0
      bfd_vma dtp_base, tp_base;
3370
3371
0
      if (elf_hash_table (info->link_info)->tls_sec != NULL)
3372
0
  {
3373
0
    dtp_base = alpha_get_dtprel_base (info->link_info);
3374
0
    tp_base = alpha_get_tprel_base (info->link_info);
3375
0
  }
3376
0
      else
3377
0
  dtp_base = tp_base = 0;
3378
0
      disp = symval - (r_type == R_ALPHA_GOTDTPREL ? dtp_base : tp_base);
3379
3380
0
      insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
3381
3382
0
      switch (r_type)
3383
0
  {
3384
0
  case R_ALPHA_GOTDTPREL:
3385
0
    r_type = R_ALPHA_DTPREL16;
3386
0
    break;
3387
0
  case R_ALPHA_GOTTPREL:
3388
0
    r_type = R_ALPHA_TPREL16;
3389
0
    break;
3390
0
  default:
3391
0
    BFD_ASSERT (0);
3392
0
    return false;
3393
0
  }
3394
0
    }
3395
3396
0
  if (disp < -0x8000 || disp >= 0x8000)
3397
0
    return true;
3398
3399
0
  bfd_put_32 (info->abfd, (bfd_vma) insn, info->contents + irel->r_offset);
3400
0
  info->changed_contents = true;
3401
3402
  /* Reduce the use count on this got entry by one, possibly
3403
     eliminating it.  */
3404
0
  if (--info->gotent->use_count == 0)
3405
0
    {
3406
0
      int sz = alpha_got_entry_size (r_type);
3407
0
      alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
3408
0
      if (!info->h)
3409
0
  alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
3410
0
    }
3411
3412
  /* Smash the existing GOT relocation for its 16-bit immediate pair.  */
3413
0
  irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), r_type);
3414
0
  info->changed_relocs = true;
3415
3416
  /* ??? Search forward through this basic block looking for insns
3417
     that use the target register.  Stop after an insn modifying the
3418
     register is seen, or after a branch or call.
3419
3420
     Any such memory load insn may be substituted by a load directly
3421
     off the GP.  This allows the memory load insn to be issued before
3422
     the calculated GP register would otherwise be ready.
3423
3424
     Any such jsr insn can be replaced by a bsr if it is in range.
3425
3426
     This would mean that we'd have to _add_ relocations, the pain of
3427
     which gives one pause.  */
3428
3429
0
  return true;
3430
0
}
3431
3432
static bfd_vma
3433
elf64_alpha_relax_opt_call (struct alpha_relax_info *info, bfd_vma symval)
3434
0
{
3435
  /* If the function has the same gp, and we can identify that the
3436
     function does not use its function pointer, we can eliminate the
3437
     address load.  */
3438
3439
  /* If the symbol is marked NOPV, we are being told the function never
3440
     needs its procedure value.  */
3441
0
  if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_NOPV)
3442
0
    return symval;
3443
3444
  /* If the symbol is marked STD_GP, we are being told the function does
3445
     a normal ldgp in the first two words.  */
3446
0
  else if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_STD_GPLOAD)
3447
0
    ;
3448
3449
  /* Otherwise, we may be able to identify a GP load in the first two
3450
     words, which we can then skip.  */
3451
0
  else
3452
0
    {
3453
0
      Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
3454
0
      bfd_vma ofs;
3455
3456
      /* Load the relocations from the section that the target symbol is in.  */
3457
0
      if (info->sec == info->tsec)
3458
0
  {
3459
0
    tsec_relocs = info->relocs;
3460
0
    tsec_relend = info->relend;
3461
0
    tsec_free = NULL;
3462
0
  }
3463
0
      else
3464
0
  {
3465
0
    tsec_relocs
3466
0
      = _bfd_elf_link_read_relocs (info->abfd, info->tsec, NULL, NULL,
3467
0
           info->link_info->keep_memory);
3468
0
    if (tsec_relocs == NULL)
3469
0
      return 0;
3470
0
    tsec_relend = tsec_relocs + info->tsec->reloc_count;
3471
0
    tsec_free = (elf_section_data (info->tsec)->relocs == tsec_relocs
3472
0
           ? NULL
3473
0
           : tsec_relocs);
3474
0
  }
3475
3476
      /* Recover the symbol's offset within the section.  */
3477
0
      ofs = (symval - info->tsec->output_section->vma
3478
0
       - info->tsec->output_offset);
3479
3480
      /* Look for a GPDISP reloc.  */
3481
0
      gpdisp = (elf64_alpha_find_reloc_at_ofs
3482
0
    (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
3483
3484
0
      if (!gpdisp || gpdisp->r_addend != 4)
3485
0
  {
3486
0
    free (tsec_free);
3487
0
    return 0;
3488
0
  }
3489
0
      free (tsec_free);
3490
0
    }
3491
3492
  /* We've now determined that we can skip an initial gp load.  Verify
3493
     that the call and the target use the same gp.   */
3494
0
  if (info->link_info->output_bfd->xvec != info->tsec->owner->xvec
3495
0
      || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
3496
0
    return 0;
3497
3498
0
  return symval + 8;
3499
0
}
3500
3501
static bool
3502
elf64_alpha_relax_with_lituse (struct alpha_relax_info *info,
3503
             bfd_vma symval, Elf_Internal_Rela *irel)
3504
0
{
3505
0
  Elf_Internal_Rela *urel, *erel, *irelend = info->relend;
3506
0
  int flags;
3507
0
  bfd_signed_vma disp;
3508
0
  bool fits16;
3509
0
  bool fits32;
3510
0
  bool lit_reused = false;
3511
0
  bool all_optimized = true;
3512
0
  bool changed_contents;
3513
0
  bool changed_relocs;
3514
0
  bfd_byte *contents = info->contents;
3515
0
  bfd *abfd = info->abfd;
3516
0
  bfd_vma sec_output_vma;
3517
0
  unsigned int lit_insn;
3518
0
  int relax_pass;
3519
3520
0
  lit_insn = bfd_get_32 (abfd, contents + irel->r_offset);
3521
0
  if (lit_insn >> 26 != OP_LDQ)
3522
0
    {
3523
0
      _bfd_error_handler
3524
  /* xgettext:c-format */
3525
0
  (_("%pB: %pA+%#" PRIx64 ": warning: "
3526
0
     "%s relocation against unexpected insn"),
3527
0
   abfd, info->sec, (uint64_t) irel->r_offset, "LITERAL");
3528
0
      return true;
3529
0
    }
3530
3531
  /* Can't relax dynamic symbols.  */
3532
0
  if (info->h != NULL
3533
0
      && alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
3534
0
    return true;
3535
3536
0
  changed_contents = info->changed_contents;
3537
0
  changed_relocs = info->changed_relocs;
3538
0
  sec_output_vma = info->sec->output_section->vma + info->sec->output_offset;
3539
0
  relax_pass = info->link_info->relax_pass;
3540
3541
  /* Summarize how this particular LITERAL is used.  */
3542
0
  for (erel = irel+1, flags = 0; erel < irelend; ++erel)
3543
0
    {
3544
0
      if (ELF64_R_TYPE (erel->r_info) != R_ALPHA_LITUSE)
3545
0
  break;
3546
0
      if (erel->r_addend <= 6)
3547
0
  flags |= 1 << erel->r_addend;
3548
0
    }
3549
3550
  /* A little preparation for the loop...  */
3551
0
  disp = symval - info->gp;
3552
3553
0
  for (urel = irel+1; urel < erel; ++urel)
3554
0
    {
3555
0
      bfd_vma urel_r_offset = urel->r_offset;
3556
0
      unsigned int insn;
3557
0
      int insn_disp;
3558
0
      bfd_signed_vma xdisp;
3559
0
      Elf_Internal_Rela nrel;
3560
3561
0
      insn = bfd_get_32 (abfd, contents + urel_r_offset);
3562
3563
0
      switch (urel->r_addend)
3564
0
  {
3565
0
  case LITUSE_ALPHA_ADDR:
3566
0
  default:
3567
    /* This type is really just a placeholder to note that all
3568
       uses cannot be optimized, but to still allow some.  */
3569
0
    all_optimized = false;
3570
0
    break;
3571
3572
0
  case LITUSE_ALPHA_BASE:
3573
    /* We may only create GPREL relocs during the second pass.  */
3574
0
    if (relax_pass == 0)
3575
0
      {
3576
0
        all_optimized = false;
3577
0
        break;
3578
0
      }
3579
3580
    /* We can always optimize 16-bit displacements.  */
3581
3582
    /* Extract the displacement from the instruction, sign-extending
3583
       it if necessary, then test whether it is within 16 or 32 bits
3584
       displacement from GP.  */
3585
0
    insn_disp = ((insn & 0xffff) ^ 0x8000) - 0x8000;
3586
3587
0
    xdisp = disp + insn_disp;
3588
0
    fits16 = (xdisp >= - (bfd_signed_vma) 0x8000 && xdisp < 0x8000);
3589
0
    fits32 = (xdisp >= - (bfd_signed_vma) 0x80000000
3590
0
        && xdisp < 0x7fff8000);
3591
3592
0
    if (fits16)
3593
0
      {
3594
        /* Take the op code and dest from this insn, take the base
3595
     register from the literal insn.  Leave the offset alone.  */
3596
0
        insn = (insn & 0xffe0ffff) | (lit_insn & 0x001f0000);
3597
0
        bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
3598
0
        changed_contents = true;
3599
3600
0
        nrel = *urel;
3601
0
        nrel.r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3602
0
            R_ALPHA_GPREL16);
3603
0
        nrel.r_addend = irel->r_addend;
3604
3605
        /* As we adjust, move the reloc to the end so that we don't
3606
     break the LITERAL+LITUSE chain.  */
3607
0
        if (urel < --erel)
3608
0
    *urel-- = *erel;
3609
0
        *erel = nrel;
3610
0
        changed_relocs = true;
3611
0
      }
3612
3613
    /* If all mem+byte, we can optimize 32-bit mem displacements.  */
3614
0
    else if (fits32 && !(flags & ~6))
3615
0
      {
3616
        /* FIXME: sanity check that lit insn Ra is mem insn Rb.  */
3617
3618
0
        irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3619
0
             R_ALPHA_GPRELHIGH);
3620
0
        lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
3621
0
        bfd_put_32 (abfd, (bfd_vma) lit_insn, contents + irel->r_offset);
3622
0
        lit_reused = true;
3623
0
        changed_contents = true;
3624
3625
        /* Since all relocs must be optimized, don't bother swapping
3626
     this relocation to the end.  */
3627
0
        urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3628
0
             R_ALPHA_GPRELLOW);
3629
0
        urel->r_addend = irel->r_addend;
3630
0
        changed_relocs = true;
3631
0
      }
3632
0
    else
3633
0
      all_optimized = false;
3634
0
    break;
3635
3636
0
  case LITUSE_ALPHA_BYTOFF:
3637
    /* We can always optimize byte instructions.  */
3638
3639
    /* FIXME: sanity check the insn for byte op.  Check that the
3640
       literal dest reg is indeed Rb in the byte insn.  */
3641
3642
0
    insn &= ~ (unsigned) 0x001ff000;
3643
0
    insn |= ((symval & 7) << 13) | 0x1000;
3644
0
    bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
3645
0
    changed_contents = true;
3646
3647
0
    nrel = *urel;
3648
0
    nrel.r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3649
0
    nrel.r_addend = 0;
3650
3651
    /* As we adjust, move the reloc to the end so that we don't
3652
       break the LITERAL+LITUSE chain.  */
3653
0
    if (urel < --erel)
3654
0
      *urel-- = *erel;
3655
0
    *erel = nrel;
3656
0
    changed_relocs = true;
3657
0
    break;
3658
3659
0
  case LITUSE_ALPHA_JSR:
3660
0
  case LITUSE_ALPHA_TLSGD:
3661
0
  case LITUSE_ALPHA_TLSLDM:
3662
0
  case LITUSE_ALPHA_JSRDIRECT:
3663
0
    {
3664
0
      bfd_vma optdest, org;
3665
0
      bfd_signed_vma odisp;
3666
3667
      /* For undefined weak symbols, we're mostly interested in getting
3668
         rid of the got entry whenever possible, so optimize this to a
3669
         use of the zero register.  */
3670
0
      if (info->h && info->h->root.root.type == bfd_link_hash_undefweak)
3671
0
        {
3672
0
    insn |= 31 << 16;
3673
0
    bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
3674
3675
0
    changed_contents = true;
3676
0
    break;
3677
0
        }
3678
3679
      /* If not zero, place to jump without needing pv.  */
3680
0
      optdest = elf64_alpha_relax_opt_call (info, symval);
3681
0
      org = sec_output_vma + urel_r_offset + 4;
3682
0
      odisp = (optdest ? optdest : symval) - org;
3683
3684
0
      if (odisp >= -0x400000 && odisp < 0x400000)
3685
0
        {
3686
0
    Elf_Internal_Rela *xrel;
3687
3688
    /* Preserve branch prediction call stack when possible.  */
3689
0
    if ((insn & INSN_JSR_MASK) == INSN_JSR)
3690
0
      insn = (OP_BSR << 26) | (insn & 0x03e00000);
3691
0
    else
3692
0
      insn = (OP_BR << 26) | (insn & 0x03e00000);
3693
0
    bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
3694
0
    changed_contents = true;
3695
3696
0
    nrel = *urel;
3697
0
    nrel.r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3698
0
              R_ALPHA_BRADDR);
3699
0
    nrel.r_addend = irel->r_addend;
3700
3701
0
    if (optdest)
3702
0
      nrel.r_addend += optdest - symval;
3703
0
    else
3704
0
      all_optimized = false;
3705
3706
    /* Kill any HINT reloc that might exist for this insn.  */
3707
0
    xrel = (elf64_alpha_find_reloc_at_ofs
3708
0
      (info->relocs, info->relend, urel_r_offset,
3709
0
       R_ALPHA_HINT));
3710
0
    if (xrel)
3711
0
      xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3712
3713
    /* As we adjust, move the reloc to the end so that we don't
3714
       break the LITERAL+LITUSE chain.  */
3715
0
    if (urel < --erel)
3716
0
      *urel-- = *erel;
3717
0
    *erel = nrel;
3718
3719
0
    info->changed_relocs = true;
3720
0
        }
3721
0
      else
3722
0
        all_optimized = false;
3723
3724
      /* Keep the caller's GPDISP ldah/lda pair.  A non-zero optdest
3725
         says the callee starts with our gp, which is enough to enter
3726
         it past its own ldgp, but says nothing about the gp we get
3727
         back: $gp is caller-saved, and a callee ending in a tail call
3728
         returns with the tail target's gp in $29.  If that target
3729
         lives in another sub-GOT, later displacements off $gp in this
3730
         function silently name slots in the wrong sub-GOT.  Eliding
3731
         the reload again would mean proving here that the callee
3732
         returns with gp intact, which needs a scan of its
3733
         instructions.  Cheaper would be a new STO_ALPHA_* bit that
3734
         gas sets at .end, alongside STO_ALPHA_STD_GPLOAD, when it saw
3735
         nothing in the function that can leave $29 clobbered.  Either
3736
         way the payoff is two unops: the pair is already emitted, so
3737
         the rewrite never shrinks .text, and dropping the got entry
3738
         depends on optdest rather than on this.  */
3739
0
    }
3740
0
    break;
3741
0
  }
3742
0
    }
3743
3744
  /* If we reused the literal instruction, we must have optimized all.  */
3745
0
  BFD_ASSERT(!lit_reused || all_optimized);
3746
3747
  /* If all cases were optimized, we can reduce the use count on this
3748
     got entry by one, possibly eliminating it.  */
3749
0
  if (all_optimized)
3750
0
    {
3751
0
      if (--info->gotent->use_count == 0)
3752
0
  {
3753
0
    int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
3754
0
    alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
3755
0
    if (!info->h)
3756
0
      alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
3757
0
  }
3758
3759
      /* If the literal instruction is no longer needed (it may have been
3760
   reused.  We can eliminate it.  */
3761
      /* ??? For now, I don't want to deal with compacting the section,
3762
   so just nop it out.  */
3763
0
      if (!lit_reused)
3764
0
  {
3765
0
    irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3766
0
    changed_relocs = true;
3767
3768
0
    bfd_put_32 (abfd, (bfd_vma) INSN_UNOP, contents + irel->r_offset);
3769
0
    changed_contents = true;
3770
0
  }
3771
0
    }
3772
3773
0
  info->changed_contents = changed_contents;
3774
0
  info->changed_relocs = changed_relocs;
3775
3776
0
  if (all_optimized || relax_pass == 0)
3777
0
    return true;
3778
0
  return elf64_alpha_relax_got_load (info, symval, irel, R_ALPHA_LITERAL);
3779
0
}
3780
3781
static bool
3782
elf64_alpha_relax_tls_get_addr (struct alpha_relax_info *info, bfd_vma symval,
3783
        Elf_Internal_Rela *irel, bool is_gd)
3784
0
{
3785
0
  bfd_byte *pos[5];
3786
0
  unsigned int insn, tlsgd_reg;
3787
0
  Elf_Internal_Rela *gpdisp, *hint;
3788
0
  bool dynamic, use_gottprel;
3789
0
  unsigned long new_symndx;
3790
3791
0
  dynamic = (info->h != NULL
3792
0
       && alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info));
3793
3794
  /* If a TLS symbol is accessed using IE at least once, there is no point
3795
     to use dynamic model for it.  */
3796
0
  if (is_gd && info->h && (info->h->flags & ALPHA_ELF_LINK_HASH_TLS_IE))
3797
0
    ;
3798
3799
  /* If the symbol is local, and we've already committed to DF_STATIC_TLS,
3800
     then we might as well relax to IE.  */
3801
0
  else if (bfd_link_pic (info->link_info) && !dynamic
3802
0
     && (info->link_info->flags & DF_STATIC_TLS))
3803
0
    ;
3804
3805
  /* Otherwise we must be building an executable to do anything.  */
3806
0
  else if (bfd_link_pic (info->link_info))
3807
0
    return true;
3808
3809
  /* The TLSGD/TLSLDM relocation must be followed by a LITERAL and
3810
     the matching LITUSE_TLS relocations.  */
3811
0
  if (irel + 2 >= info->relend)
3812
0
    return true;
3813
0
  if (ELF64_R_TYPE (irel[1].r_info) != R_ALPHA_LITERAL
3814
0
      || ELF64_R_TYPE (irel[2].r_info) != R_ALPHA_LITUSE
3815
0
      || irel[2].r_addend != (is_gd ? LITUSE_ALPHA_TLSGD : LITUSE_ALPHA_TLSLDM))
3816
0
    return true;
3817
3818
  /* There must be a GPDISP relocation positioned immediately after the
3819
     LITUSE relocation.  */
3820
0
  gpdisp = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
3821
0
            irel[2].r_offset + 4, R_ALPHA_GPDISP);
3822
0
  if (!gpdisp)
3823
0
    return true;
3824
3825
0
  pos[0] = info->contents + irel[0].r_offset;
3826
0
  pos[1] = info->contents + irel[1].r_offset;
3827
0
  pos[2] = info->contents + irel[2].r_offset;
3828
0
  pos[3] = info->contents + gpdisp->r_offset;
3829
0
  pos[4] = pos[3] + gpdisp->r_addend;
3830
3831
  /* Beware of the compiler hoisting part of the sequence out a loop
3832
     and adjusting the destination register for the TLSGD insn.  If this
3833
     happens, there will be a move into $16 before the JSR insn, so only
3834
     transformations of the first insn pair should use this register.  */
3835
0
  tlsgd_reg = bfd_get_32 (info->abfd, pos[0]);
3836
0
  tlsgd_reg = (tlsgd_reg >> 21) & 31;
3837
3838
  /* Generally, the positions are not allowed to be out of order, lest the
3839
     modified insn sequence have different register lifetimes.  We can make
3840
     an exception when pos 1 is adjacent to pos 0.  */
3841
0
  if (pos[1] + 4 == pos[0])
3842
0
    {
3843
0
      bfd_byte *tmp = pos[0];
3844
0
      pos[0] = pos[1];
3845
0
      pos[1] = tmp;
3846
0
    }
3847
0
  if (pos[1] >= pos[2] || pos[2] >= pos[3])
3848
0
    return true;
3849
3850
  /* Reduce the use count on the LITERAL relocation.  Do this before we
3851
     smash the symndx when we adjust the relocations below.  */
3852
0
  {
3853
0
    struct alpha_elf_got_entry *lit_gotent;
3854
0
    struct alpha_elf_link_hash_entry *lit_h;
3855
0
    unsigned long indx;
3856
3857
0
    BFD_ASSERT (ELF64_R_SYM (irel[1].r_info) >= info->symtab_hdr->sh_info);
3858
0
    indx = ELF64_R_SYM (irel[1].r_info) - info->symtab_hdr->sh_info;
3859
0
    lit_h = alpha_elf_sym_hashes (info->abfd)[indx];
3860
3861
0
    while (lit_h->root.root.type == bfd_link_hash_indirect
3862
0
     || lit_h->root.root.type == bfd_link_hash_warning)
3863
0
      lit_h = (struct alpha_elf_link_hash_entry *) lit_h->root.root.u.i.link;
3864
3865
0
    for (lit_gotent = lit_h->got_entries; lit_gotent ;
3866
0
   lit_gotent = lit_gotent->next)
3867
0
      if (lit_gotent->gotobj == info->gotobj
3868
0
    && lit_gotent->reloc_type == R_ALPHA_LITERAL
3869
0
    && lit_gotent->addend == irel[1].r_addend)
3870
0
  break;
3871
0
    BFD_ASSERT (lit_gotent);
3872
3873
0
    if (--lit_gotent->use_count == 0)
3874
0
      {
3875
0
  int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
3876
0
  alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
3877
0
      }
3878
0
  }
3879
3880
  /* Change
3881
3882
  lda $16,x($gp)      !tlsgd!1
3883
  ldq $27,__tls_get_addr($gp)   !literal!1
3884
  jsr $26,($27),__tls_get_addr  !lituse_tlsgd!1
3885
  ldah  $29,0($26)      !gpdisp!2
3886
  lda $29,0($29)      !gpdisp!2
3887
     to
3888
  ldq $16,x($gp)      !gottprel
3889
  unop
3890
  call_pal rduniq
3891
  addq  $16,$0,$0
3892
  unop
3893
     or the first pair to
3894
  lda $16,x($gp)      !tprel
3895
  unop
3896
     or
3897
  ldah  $16,x($gp)      !tprelhi
3898
  lda $16,x($16)      !tprello
3899
3900
     as appropriate.  */
3901
3902
0
  use_gottprel = false;
3903
0
  new_symndx = is_gd ? ELF64_R_SYM (irel->r_info) : STN_UNDEF;
3904
3905
  /* Some compilers warn about a Boolean-looking expression being
3906
     used in a switch.  The explicit cast silences them.  */
3907
0
  switch ((int) (!dynamic && !bfd_link_pic (info->link_info)))
3908
0
    {
3909
0
    case 1:
3910
0
      {
3911
0
  bfd_vma tp_base;
3912
0
  bfd_signed_vma disp;
3913
3914
0
  BFD_ASSERT (elf_hash_table (info->link_info)->tls_sec != NULL);
3915
0
  tp_base = alpha_get_tprel_base (info->link_info);
3916
0
  disp = symval - tp_base;
3917
3918
0
  if (disp >= -0x8000 && disp < 0x8000)
3919
0
    {
3920
0
      insn = (OP_LDA << 26) | (tlsgd_reg << 21) | (31 << 16);
3921
0
      bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
3922
0
      bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
3923
3924
0
      irel[0].r_offset = pos[0] - info->contents;
3925
0
      irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPREL16);
3926
0
      irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3927
0
      break;
3928
0
    }
3929
0
  else if (disp >= -(bfd_signed_vma) 0x80000000
3930
0
     && disp < (bfd_signed_vma) 0x7fff8000
3931
0
     && pos[0] + 4 == pos[1])
3932
0
    {
3933
0
      insn = (OP_LDAH << 26) | (tlsgd_reg << 21) | (31 << 16);
3934
0
      bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
3935
0
      insn = (OP_LDA << 26) | (tlsgd_reg << 21) | (tlsgd_reg << 16);
3936
0
      bfd_put_32 (info->abfd, (bfd_vma) insn, pos[1]);
3937
3938
0
      irel[0].r_offset = pos[0] - info->contents;
3939
0
      irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELHI);
3940
0
      irel[1].r_offset = pos[1] - info->contents;
3941
0
      irel[1].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELLO);
3942
0
      break;
3943
0
    }
3944
0
      }
3945
      /* FALLTHRU */
3946
3947
0
    default:
3948
0
      use_gottprel = true;
3949
3950
0
      insn = (OP_LDQ << 26) | (tlsgd_reg << 21) | (29 << 16);
3951
0
      bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
3952
0
      bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
3953
3954
0
      irel[0].r_offset = pos[0] - info->contents;
3955
0
      irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_GOTTPREL);
3956
0
      irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3957
0
      break;
3958
0
    }
3959
3960
0
  bfd_put_32 (info->abfd, (bfd_vma) INSN_RDUNIQ, pos[2]);
3961
3962
0
  insn = INSN_ADDQ | (16 << 21) | (0 << 16) | (0 << 0);
3963
0
  bfd_put_32 (info->abfd, (bfd_vma) insn, pos[3]);
3964
3965
0
  bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[4]);
3966
3967
0
  irel[2].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3968
0
  gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3969
3970
0
  hint = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
3971
0
          irel[2].r_offset, R_ALPHA_HINT);
3972
0
  if (hint)
3973
0
    hint->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3974
3975
0
  info->changed_contents = true;
3976
0
  info->changed_relocs = true;
3977
3978
  /* Reduce the use count on the TLSGD/TLSLDM relocation.  */
3979
0
  if (--info->gotent->use_count == 0)
3980
0
    {
3981
0
      int sz = alpha_got_entry_size (info->gotent->reloc_type);
3982
0
      alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
3983
0
      if (!info->h)
3984
0
  alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
3985
0
    }
3986
3987
  /* If we've switched to a GOTTPREL relocation, increment the reference
3988
     count on that got entry.  */
3989
0
  if (use_gottprel)
3990
0
    {
3991
0
      struct alpha_elf_got_entry *tprel_gotent;
3992
3993
0
      for (tprel_gotent = *info->first_gotent; tprel_gotent ;
3994
0
     tprel_gotent = tprel_gotent->next)
3995
0
  if (tprel_gotent->gotobj == info->gotobj
3996
0
      && tprel_gotent->reloc_type == R_ALPHA_GOTTPREL
3997
0
      && tprel_gotent->addend == irel->r_addend)
3998
0
    break;
3999
0
      if (tprel_gotent)
4000
0
  tprel_gotent->use_count++;
4001
0
      else
4002
0
  {
4003
0
    if (info->gotent->use_count == 0)
4004
0
      tprel_gotent = info->gotent;
4005
0
    else
4006
0
      {
4007
0
        tprel_gotent = (struct alpha_elf_got_entry *)
4008
0
    bfd_zalloc (info->abfd, sizeof (struct alpha_elf_got_entry));
4009
0
        if (!tprel_gotent)
4010
0
    return false;
4011
4012
0
        tprel_gotent->next = *info->first_gotent;
4013
0
        *info->first_gotent = tprel_gotent;
4014
4015
0
        tprel_gotent->gotobj = info->gotobj;
4016
0
        tprel_gotent->addend = irel->r_addend;
4017
0
        tprel_gotent->got_offset = -1;
4018
0
        tprel_gotent->plt_offset = -1;
4019
0
      }
4020
4021
0
    tprel_gotent->use_count = 1;
4022
0
    tprel_gotent->reloc_type = R_ALPHA_GOTTPREL;
4023
0
  }
4024
0
    }
4025
4026
0
  return true;
4027
0
}
4028
4029
static bool
4030
elf64_alpha_relax_section (bfd *abfd, asection *sec,
4031
         struct bfd_link_info *link_info, bool *again)
4032
0
{
4033
0
  Elf_Internal_Shdr *symtab_hdr;
4034
0
  Elf_Internal_Rela *internal_relocs;
4035
0
  Elf_Internal_Rela *irel, *irelend;
4036
0
  Elf_Internal_Sym *isymbuf = NULL;
4037
0
  struct alpha_elf_got_entry **local_got_entries;
4038
0
  struct alpha_relax_info info;
4039
0
  struct alpha_elf_link_hash_table * htab;
4040
0
  int relax_pass;
4041
4042
0
  htab = alpha_elf_hash_table (link_info);
4043
0
  if (htab == NULL)
4044
0
    return false;
4045
4046
  /* There's nothing to change, yet.  */
4047
0
  *again = false;
4048
4049
0
  if (bfd_link_relocatable (link_info)
4050
0
      || ((sec->flags & (SEC_CODE | SEC_RELOC | SEC_ALLOC | SEC_HAS_CONTENTS))
4051
0
    != (SEC_CODE | SEC_RELOC | SEC_ALLOC | SEC_HAS_CONTENTS))
4052
0
      || sec->reloc_count == 0)
4053
0
    return true;
4054
4055
0
  BFD_ASSERT (is_alpha_elf (abfd));
4056
0
  relax_pass = link_info->relax_pass;
4057
4058
0
  if (relax_pass != 0)
4059
0
    htab->no_got_merge = true;
4060
4061
  /* Make sure our GOT and PLT tables are up-to-date.  */
4062
0
  if (htab->relax_trip != link_info->relax_trip)
4063
0
    {
4064
0
      htab->relax_trip = link_info->relax_trip;
4065
4066
      /* This should never fail after the initial round, since the only error
4067
   is GOT overflow, and relaxation only shrinks the table overall.
4068
   However, we may only merge got sections before GPREL relocs have
4069
   been created.  If we merge sections afterwards, the GP for the
4070
   merged section backs up which may put the relocs out of range.  */
4071
0
      if (!elf64_alpha_size_got_sections (link_info, !htab->no_got_merge))
4072
0
  abort ();
4073
0
      if (elf_hash_table (link_info)->dynamic_sections_created)
4074
0
  {
4075
0
    elf64_alpha_size_plt_section (link_info);
4076
0
    elf64_alpha_size_rela_got_section (link_info);
4077
0
  }
4078
0
      elf64_alpha_size_irelplt_section (link_info);
4079
0
    }
4080
4081
0
  symtab_hdr = &elf_symtab_hdr (abfd);
4082
0
  local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
4083
4084
  /* Load the relocations for this section.  */
4085
0
  internal_relocs
4086
0
    = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
4087
0
         link_info->keep_memory);
4088
0
  if (internal_relocs == NULL)
4089
0
    return false;
4090
4091
0
  memset(&info, 0, sizeof (info));
4092
0
  info.abfd = abfd;
4093
0
  info.sec = sec;
4094
0
  info.link_info = link_info;
4095
0
  info.symtab_hdr = symtab_hdr;
4096
0
  info.relocs = internal_relocs;
4097
0
  info.relend = irelend = internal_relocs + sec->reloc_count;
4098
4099
  /* Find the GP for this object.  Do not store the result back via
4100
     _bfd_set_gp_value, since this could change again before final.  */
4101
0
  info.gotobj = alpha_elf_tdata (abfd)->gotobj;
4102
0
  if (info.gotobj)
4103
0
    {
4104
0
      asection *sgot = alpha_elf_tdata (info.gotobj)->got;
4105
0
      info.gp = (sgot->output_section->vma
4106
0
     + sgot->output_offset
4107
0
     + 0x8000);
4108
0
    }
4109
4110
  /* Get the section contents.  */
4111
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
4112
0
    info.contents = elf_section_data (sec)->this_hdr.contents;
4113
0
  else
4114
0
    {
4115
0
      if (!bfd_malloc_and_get_section (abfd, sec, &info.contents))
4116
0
  goto error_return;
4117
0
    }
4118
4119
0
  for (irel = internal_relocs; irel < irelend; irel++)
4120
0
    {
4121
0
      bfd_vma symval;
4122
0
      struct alpha_elf_got_entry *gotent;
4123
0
      unsigned long r_type = ELF64_R_TYPE (irel->r_info);
4124
0
      unsigned long r_symndx = ELF64_R_SYM (irel->r_info);
4125
4126
      /* Early exit for unhandled or unrelaxable relocations.  */
4127
0
      if (r_type != R_ALPHA_LITERAL)
4128
0
  {
4129
    /* We complete everything except LITERAL in the first pass.  */
4130
0
    if (relax_pass != 0)
4131
0
      continue;
4132
0
    if (r_type == R_ALPHA_TLSLDM)
4133
0
      {
4134
        /* The symbol for a TLSLDM reloc is ignored.  Collapse the
4135
     reloc to the STN_UNDEF (0) symbol so that they all match.  */
4136
0
        r_symndx = STN_UNDEF;
4137
0
      }
4138
0
    else if (r_type != R_ALPHA_GOTDTPREL
4139
0
       && r_type != R_ALPHA_GOTTPREL
4140
0
       && r_type != R_ALPHA_TLSGD)
4141
0
      continue;
4142
0
  }
4143
4144
      /* Get the value of the symbol referred to by the reloc.  */
4145
0
      if (r_symndx < symtab_hdr->sh_info)
4146
0
  {
4147
    /* A local symbol.  */
4148
0
    Elf_Internal_Sym *isym;
4149
4150
    /* Read this BFD's local symbols.  */
4151
0
    if (isymbuf == NULL)
4152
0
      {
4153
0
        isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4154
0
        if (isymbuf == NULL)
4155
0
    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4156
0
            symtab_hdr->sh_info, 0,
4157
0
            NULL, NULL, NULL);
4158
0
        if (isymbuf == NULL)
4159
0
    goto error_return;
4160
0
      }
4161
4162
0
    isym = isymbuf + r_symndx;
4163
4164
    /* A reference to an IFUNC has to keep going through the GOT: a
4165
       direct branch or a gp-relative address would reach the resolver
4166
       rather than the function it selects.  */
4167
0
    if (elf64_alpha_ifunc_p (NULL, isym))
4168
0
      continue;
4169
4170
    /* Given the symbol for a TLSLDM reloc is ignored, this also
4171
       means forcing the symbol value to the tp base.  */
4172
0
    if (r_type == R_ALPHA_TLSLDM)
4173
0
      {
4174
0
        info.tsec = bfd_abs_section_ptr;
4175
0
        symval = alpha_get_tprel_base (info.link_info);
4176
0
      }
4177
0
    else
4178
0
      {
4179
0
        symval = isym->st_value;
4180
0
        if (isym->st_shndx == SHN_UNDEF)
4181
0
    continue;
4182
0
        else if (isym->st_shndx == SHN_ABS)
4183
0
    info.tsec = bfd_abs_section_ptr;
4184
0
        else if (isym->st_shndx == SHN_COMMON)
4185
0
    info.tsec = bfd_com_section_ptr;
4186
0
        else
4187
0
    info.tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4188
0
      }
4189
4190
0
    info.h = NULL;
4191
0
    info.other = isym->st_other;
4192
0
    if (local_got_entries)
4193
0
      info.first_gotent = &local_got_entries[r_symndx];
4194
0
    else
4195
0
      {
4196
0
        info.first_gotent = &info.gotent;
4197
0
        info.gotent = NULL;
4198
0
      }
4199
0
  }
4200
0
      else
4201
0
  {
4202
0
    unsigned long indx;
4203
0
    struct alpha_elf_link_hash_entry *h;
4204
4205
0
    indx = r_symndx - symtab_hdr->sh_info;
4206
0
    h = alpha_elf_sym_hashes (abfd)[indx];
4207
0
    BFD_ASSERT (h != NULL);
4208
4209
0
    while (h->root.root.type == bfd_link_hash_indirect
4210
0
     || h->root.root.type == bfd_link_hash_warning)
4211
0
      h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
4212
4213
    /* A reference to an IFUNC has to keep going through the GOT.  */
4214
0
    if (elf64_alpha_ifunc_p (h, NULL))
4215
0
      continue;
4216
4217
    /* If the symbol is undefined, we can't do anything with it.  */
4218
0
    if (h->root.root.type == bfd_link_hash_undefined)
4219
0
      continue;
4220
4221
    /* If the symbol isn't defined in the current module,
4222
       again we can't do anything.  */
4223
0
    if (h->root.root.type == bfd_link_hash_undefweak)
4224
0
      {
4225
0
        info.tsec = bfd_abs_section_ptr;
4226
0
        symval = 0;
4227
0
      }
4228
0
    else if (!h->root.def_regular)
4229
0
      {
4230
        /* Except for TLSGD relocs, which can sometimes be
4231
     relaxed to GOTTPREL relocs.  */
4232
0
        if (r_type != R_ALPHA_TLSGD)
4233
0
    continue;
4234
0
        info.tsec = bfd_abs_section_ptr;
4235
0
        symval = 0;
4236
0
      }
4237
0
    else
4238
0
      {
4239
0
        info.tsec = h->root.root.u.def.section;
4240
0
        symval = h->root.root.u.def.value;
4241
0
      }
4242
4243
0
    info.h = h;
4244
0
    info.other = h->root.other;
4245
0
    info.first_gotent = &h->got_entries;
4246
0
  }
4247
4248
      /* Search for the got entry to be used by this relocation.  */
4249
0
      for (gotent = *info.first_gotent; gotent ; gotent = gotent->next)
4250
0
  if (gotent->gotobj == info.gotobj
4251
0
      && gotent->reloc_type == r_type
4252
0
      && gotent->addend == irel->r_addend)
4253
0
    break;
4254
0
      info.gotent = gotent;
4255
4256
0
      symval += info.tsec->output_section->vma + info.tsec->output_offset;
4257
0
      symval += irel->r_addend;
4258
4259
0
      switch (r_type)
4260
0
  {
4261
0
  case R_ALPHA_LITERAL:
4262
0
    BFD_ASSERT(info.gotent != NULL);
4263
4264
    /* If there exist LITUSE relocations immediately following, this
4265
       opens up all sorts of interesting optimizations, because we
4266
       now know every location that this address load is used.  */
4267
0
    if (irel+1 < irelend
4268
0
        && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
4269
0
      {
4270
0
        if (!elf64_alpha_relax_with_lituse (&info, symval, irel))
4271
0
    goto error_return;
4272
0
      }
4273
0
    else
4274
0
      {
4275
0
        if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
4276
0
    goto error_return;
4277
0
      }
4278
0
    break;
4279
4280
0
  case R_ALPHA_GOTDTPREL:
4281
0
  case R_ALPHA_GOTTPREL:
4282
0
    BFD_ASSERT(info.gotent != NULL);
4283
0
    if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
4284
0
      goto error_return;
4285
0
    break;
4286
4287
0
  case R_ALPHA_TLSGD:
4288
0
  case R_ALPHA_TLSLDM:
4289
0
    BFD_ASSERT(info.gotent != NULL);
4290
0
    if (!elf64_alpha_relax_tls_get_addr (&info, symval, irel,
4291
0
                 r_type == R_ALPHA_TLSGD))
4292
0
      goto error_return;
4293
0
    break;
4294
0
  }
4295
0
    }
4296
4297
0
  if (isymbuf != NULL
4298
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
4299
0
    {
4300
0
      if (!link_info->keep_memory)
4301
0
  free (isymbuf);
4302
0
      else
4303
0
  {
4304
    /* Cache the symbols for elf_link_input_bfd.  */
4305
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
4306
0
  }
4307
0
    }
4308
4309
0
  if (info.contents != NULL
4310
0
      && elf_section_data (sec)->this_hdr.contents != info.contents)
4311
0
    {
4312
0
      if (!info.changed_contents && !link_info->keep_memory)
4313
0
  free (info.contents);
4314
0
      else
4315
0
  {
4316
    /* Cache the section contents for elf_link_input_bfd.  */
4317
0
    elf_section_data (sec)->this_hdr.contents = info.contents;
4318
0
  }
4319
0
    }
4320
4321
0
  if (elf_section_data (sec)->relocs != internal_relocs)
4322
0
    {
4323
0
      if (!info.changed_relocs)
4324
0
  free (internal_relocs);
4325
0
      else
4326
0
  elf_section_data (sec)->relocs = internal_relocs;
4327
0
    }
4328
4329
0
  *again = info.changed_contents || info.changed_relocs;
4330
4331
0
  return true;
4332
4333
0
 error_return:
4334
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
4335
0
    free (isymbuf);
4336
0
  if (elf_section_data (sec)->this_hdr.contents != info.contents)
4337
0
    free (info.contents);
4338
0
  if (elf_section_data (sec)->relocs != internal_relocs)
4339
0
    free (internal_relocs);
4340
0
  return false;
4341
0
}
4342

4343
/* Emit a dynamic relocation for (DYNINDX, RTYPE, ADDEND) at (SEC, OFFSET)
4344
   into the next available slot in SREL.  */
4345
4346
static void
4347
elf64_alpha_emit_dynrel (bfd *abfd, struct bfd_link_info *info,
4348
       asection *sec, asection *srel, bfd_vma offset,
4349
       long dynindx, long rtype, bfd_vma addend)
4350
0
{
4351
0
  Elf_Internal_Rela outrel;
4352
0
  bfd_byte *loc;
4353
4354
0
  BFD_ASSERT (srel != NULL);
4355
4356
0
  outrel.r_info = ELF64_R_INFO (dynindx, rtype);
4357
0
  outrel.r_addend = addend;
4358
4359
0
  offset = _bfd_elf_section_offset (abfd, info, sec, offset);
4360
0
  if ((offset | 1) != (bfd_vma) -1)
4361
0
    outrel.r_offset = sec->output_section->vma + sec->output_offset + offset;
4362
0
  else
4363
0
    memset (&outrel, 0, sizeof (outrel));
4364
4365
0
  loc = srel->contents;
4366
0
  loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
4367
0
  bfd_elf64_swap_reloca_out (abfd, &outrel, loc);
4368
0
  BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count <= srel->size);
4369
0
}
4370
4371
/* Relocate an Alpha ELF section for a relocatable link.
4372
4373
   We don't have to change anything unless the reloc is against a section
4374
   symbol, in which case we have to adjust according to where the section
4375
   symbol winds up in the output section.  */
4376
4377
static int
4378
elf64_alpha_relocate_section_r (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4379
        bfd *input_bfd, asection *input_section,
4380
        bfd_byte *contents ATTRIBUTE_UNUSED,
4381
        Elf_Internal_Rela *relocs,
4382
        Elf_Internal_Sym *local_syms,
4383
        asection **local_sections)
4384
0
{
4385
0
  unsigned long symtab_hdr_sh_info;
4386
0
  Elf_Internal_Rela *rel;
4387
0
  Elf_Internal_Rela *relend;
4388
0
  struct elf_link_hash_entry **sym_hashes;
4389
0
  bool ret_val = true;
4390
4391
0
  symtab_hdr_sh_info = elf_symtab_hdr (input_bfd).sh_info;
4392
0
  sym_hashes = elf_sym_hashes (input_bfd);
4393
4394
0
  relend = relocs + input_section->reloc_count;
4395
0
  for (rel = relocs; rel < relend; rel++)
4396
0
    {
4397
0
      unsigned long r_symndx;
4398
0
      Elf_Internal_Sym *sym;
4399
0
      asection *sec;
4400
0
      unsigned long r_type;
4401
4402
0
      r_type = ELF64_R_TYPE (rel->r_info);
4403
0
      if (r_type >= R_ALPHA_max)
4404
0
  {
4405
0
    _bfd_error_handler
4406
      /* xgettext:c-format */
4407
0
      (_("%pB: unsupported relocation type %#x"),
4408
0
       input_bfd, (int) r_type);
4409
0
    bfd_set_error (bfd_error_bad_value);
4410
0
    ret_val = false;
4411
0
    continue;
4412
0
  }
4413
4414
      /* The symbol associated with GPDISP and LITUSE is
4415
   immaterial.  Only the addend is significant.  */
4416
0
      if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
4417
0
  continue;
4418
4419
0
      r_symndx = ELF64_R_SYM (rel->r_info);
4420
0
      if (r_symndx < symtab_hdr_sh_info)
4421
0
  {
4422
0
    sym = local_syms + r_symndx;
4423
0
    sec = local_sections[r_symndx];
4424
0
  }
4425
0
      else
4426
0
  {
4427
0
    struct elf_link_hash_entry *h;
4428
4429
0
    h = sym_hashes[r_symndx - symtab_hdr_sh_info];
4430
4431
0
    while (h->root.type == bfd_link_hash_indirect
4432
0
     || h->root.type == bfd_link_hash_warning)
4433
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4434
4435
0
    if (h->root.type != bfd_link_hash_defined
4436
0
        && h->root.type != bfd_link_hash_defweak)
4437
0
      continue;
4438
4439
0
    sym = NULL;
4440
0
    sec = h->root.u.def.section;
4441
0
  }
4442
4443
0
      if (sec != NULL && discarded_section (sec))
4444
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4445
0
           rel, 1, relend, R_ALPHA_NONE,
4446
0
           elf64_alpha_howto_table + r_type, 0,
4447
0
           contents);
4448
4449
0
      if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4450
0
  rel->r_addend += sec->output_offset;
4451
0
    }
4452
4453
0
  return ret_val;
4454
0
}
4455
4456
/* The name of the symbol a relocation refers to, for diagnostics.  H
4457
   describes the symbol when it is global and SYM when it is local.  */
4458
4459
static const char *
4460
elf64_alpha_sym_name (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4461
          struct alpha_elf_link_hash_entry *h,
4462
          const Elf_Internal_Sym *sym, asection *sec)
4463
0
{
4464
0
  if (h != NULL)
4465
0
    return h->root.root.root.string;
4466
4467
0
  return bfd_elf_sym_name (abfd, symtab_hdr, sym, sec);
4468
0
}
4469
4470
/* Relocate an Alpha ELF section.  */
4471
4472
static int
4473
elf64_alpha_relocate_section (struct bfd_link_info *info,
4474
            bfd *input_bfd, asection *input_section,
4475
            bfd_byte *contents, Elf_Internal_Rela *relocs,
4476
            Elf_Internal_Sym *local_syms,
4477
            asection **local_sections)
4478
0
{
4479
0
  Elf_Internal_Shdr *symtab_hdr;
4480
0
  Elf_Internal_Rela *rel;
4481
0
  Elf_Internal_Rela *relend;
4482
0
  asection *sgot, *srel, *srelgot;
4483
0
  bfd *gotobj;
4484
0
  bfd_vma gp, tp_base, dtp_base;
4485
0
  struct alpha_elf_got_entry **local_got_entries;
4486
0
  bool ret_val;
4487
4488
0
  BFD_ASSERT (is_alpha_elf (input_bfd));
4489
4490
  /* Handle relocatable links with a smaller loop.  */
4491
0
  if (bfd_link_relocatable (info))
4492
0
    return elf64_alpha_relocate_section_r (info, input_bfd,
4493
0
             input_section, contents, relocs,
4494
0
             local_syms, local_sections);
4495
4496
  /* This is a final link.  */
4497
4498
0
  ret_val = true;
4499
4500
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
4501
4502
0
  srelgot = elf_hash_table (info)->srelgot;
4503
0
  srel = elf_section_data (input_section)->sreloc;
4504
4505
  /* Find the gp value for this input bfd.  */
4506
0
  gotobj = alpha_elf_tdata (input_bfd)->gotobj;
4507
0
  if (gotobj)
4508
0
    {
4509
0
      sgot = alpha_elf_tdata (gotobj)->got;
4510
0
      gp = _bfd_get_gp_value (gotobj);
4511
0
      if (gp == 0)
4512
0
  {
4513
0
    gp = (sgot->output_section->vma
4514
0
    + sgot->output_offset
4515
0
    + 0x8000);
4516
0
    _bfd_set_gp_value (gotobj, gp);
4517
0
  }
4518
0
    }
4519
0
  else
4520
0
    {
4521
0
      sgot = NULL;
4522
0
      gp = 0;
4523
0
    }
4524
4525
0
  local_got_entries = alpha_elf_tdata(input_bfd)->local_got_entries;
4526
4527
0
  if (elf_hash_table (info)->tls_sec != NULL)
4528
0
    {
4529
0
      dtp_base = alpha_get_dtprel_base (info);
4530
0
      tp_base = alpha_get_tprel_base (info);
4531
0
    }
4532
0
  else
4533
0
    dtp_base = tp_base = 0;
4534
4535
0
  relend = relocs + input_section->reloc_count;
4536
0
  for (rel = relocs; rel < relend; rel++)
4537
0
    {
4538
0
      struct alpha_elf_link_hash_entry *h = NULL;
4539
0
      struct alpha_elf_got_entry *gotent;
4540
0
      bfd_reloc_status_type r;
4541
0
      reloc_howto_type *howto;
4542
0
      unsigned long r_symndx;
4543
0
      const Elf_Internal_Sym *sym = NULL;
4544
0
      asection *sec = NULL;
4545
0
      bfd_vma value;
4546
0
      bfd_vma addend;
4547
0
      bool dynamic_symbol_p;
4548
0
      bool unresolved_reloc = false;
4549
0
      bool undef_weak_ref = false;
4550
0
      unsigned long r_type;
4551
4552
0
      r_type = ELF64_R_TYPE(rel->r_info);
4553
0
      if (r_type >= R_ALPHA_max)
4554
0
  {
4555
0
    _bfd_error_handler
4556
      /* xgettext:c-format */
4557
0
      (_("%pB: unsupported relocation type %#x"),
4558
0
       input_bfd, (int) r_type);
4559
0
    bfd_set_error (bfd_error_bad_value);
4560
0
    ret_val = false;
4561
0
    continue;
4562
0
  }
4563
4564
0
      howto = elf64_alpha_howto_table + r_type;
4565
0
      r_symndx = ELF64_R_SYM(rel->r_info);
4566
4567
      /* The symbol for a TLSLDM reloc is ignored.  Collapse the
4568
   reloc to the STN_UNDEF (0) symbol so that they all match.  */
4569
0
      if (r_type == R_ALPHA_TLSLDM)
4570
0
  r_symndx = STN_UNDEF;
4571
4572
0
      if (r_symndx < symtab_hdr->sh_info)
4573
0
  {
4574
0
    asection *msec;
4575
0
    sym = local_syms + r_symndx;
4576
0
    sec = local_sections[r_symndx];
4577
0
    msec = sec;
4578
0
    value = _bfd_elf_rela_local_sym (info->output_bfd, sym, &msec, rel);
4579
4580
    /* If this is a tp-relative relocation against sym STN_UNDEF (0),
4581
       this is hackery from relax_section.  Force the value to
4582
       be the tls module base.  */
4583
0
    if (r_symndx == STN_UNDEF
4584
0
        && (r_type == R_ALPHA_TLSLDM
4585
0
      || r_type == R_ALPHA_GOTTPREL
4586
0
      || r_type == R_ALPHA_TPREL64
4587
0
      || r_type == R_ALPHA_TPRELHI
4588
0
      || r_type == R_ALPHA_TPRELLO
4589
0
      || r_type == R_ALPHA_TPREL16))
4590
0
      value = dtp_base;
4591
4592
0
    if (local_got_entries)
4593
0
      gotent = local_got_entries[r_symndx];
4594
0
    else
4595
0
      gotent = NULL;
4596
4597
    /* Need to adjust local GOT entries' addends for SEC_MERGE
4598
       unless it has been done already.  */
4599
0
    if ((sec->flags & SEC_MERGE)
4600
0
        && ELF_ST_TYPE (sym->st_info) == STT_SECTION
4601
0
        && sec->sec_info_type == SEC_INFO_TYPE_MERGE
4602
0
        && gotent
4603
0
        && !gotent->reloc_xlated)
4604
0
      {
4605
0
        struct alpha_elf_got_entry *ent;
4606
4607
0
        for (ent = gotent; ent; ent = ent->next)
4608
0
    {
4609
0
      ent->reloc_xlated = 1;
4610
0
      if (ent->use_count == 0)
4611
0
        continue;
4612
0
      msec = sec;
4613
0
      ent->addend =
4614
0
        _bfd_merged_section_offset (info->output_bfd, &msec,
4615
0
            sym->st_value + ent->addend);
4616
0
      ent->addend -= sym->st_value;
4617
0
      ent->addend += msec->output_section->vma
4618
0
         + msec->output_offset
4619
0
         - sec->output_section->vma
4620
0
         - sec->output_offset;
4621
0
    }
4622
0
      }
4623
4624
0
    dynamic_symbol_p = false;
4625
0
  }
4626
0
      else
4627
0
  {
4628
0
    bool warned, ignored;
4629
0
    struct elf_link_hash_entry *hh;
4630
0
    struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
4631
4632
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4633
0
           r_symndx, symtab_hdr, sym_hashes,
4634
0
           hh, sec, value,
4635
0
           unresolved_reloc, warned, ignored);
4636
4637
0
    if (warned)
4638
0
      continue;
4639
4640
0
    if (value == 0
4641
0
        && ! unresolved_reloc
4642
0
        && hh->root.type == bfd_link_hash_undefweak)
4643
0
      undef_weak_ref = true;
4644
4645
0
    h = (struct alpha_elf_link_hash_entry *) hh;
4646
0
    dynamic_symbol_p = alpha_elf_dynamic_symbol_p (&h->root, info);
4647
0
    gotent = h->got_entries;
4648
0
  }
4649
4650
0
      if (sec != NULL && discarded_section (sec))
4651
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4652
0
           rel, 1, relend, R_ALPHA_NONE,
4653
0
           howto, 0, contents);
4654
4655
0
      addend = rel->r_addend;
4656
0
      value += addend;
4657
4658
      /* Search for the proper got entry.  */
4659
0
      for (; gotent ; gotent = gotent->next)
4660
0
  if (gotent->gotobj == gotobj
4661
0
      && gotent->reloc_type == r_type
4662
0
      && gotent->addend == addend)
4663
0
    break;
4664
4665
      /* A reference to an IFUNC has to go through the GOT entry or the data
4666
   word that an R_ALPHA_IRELATIVE fills in with the address the
4667
   resolver returns.  A relocation that names the symbol directly
4668
   reaches the resolver instead, and R_ALPHA_REFLONG has no room for
4669
   the address in the first place.  */
4670
0
      if (elf64_alpha_ifunc_p (h, sym) && (input_section->flags & SEC_ALLOC))
4671
0
  switch (r_type)
4672
0
    {
4673
0
    case R_ALPHA_REFLONG:
4674
0
    case R_ALPHA_BRADDR:
4675
0
    case R_ALPHA_BRSGP:
4676
0
    case R_ALPHA_GPREL16:
4677
0
    case R_ALPHA_GPREL32:
4678
0
    case R_ALPHA_GPRELLOW:
4679
0
    case R_ALPHA_GPRELHIGH:
4680
0
    case R_ALPHA_SREL16:
4681
0
    case R_ALPHA_SREL32:
4682
0
    case R_ALPHA_SREL64:
4683
0
      _bfd_error_handler
4684
        /* xgettext:c-format */
4685
0
        (_("%pB: %s relocation against STT_GNU_IFUNC symbol `%s' is "
4686
0
     "not supported"),
4687
0
         input_bfd, howto->name,
4688
0
         elf64_alpha_sym_name (input_bfd, symtab_hdr, h, sym, sec));
4689
0
      ret_val = false;
4690
0
      continue;
4691
4692
0
    default:
4693
0
      break;
4694
0
    }
4695
4696
      /* An R_ALPHA_IRELATIVE carries the address of the resolver in its
4697
   addend, so there is no room in it for an offset from the symbol.
4698
   A reference the dynamic linker resolves is not affected: it keeps
4699
   the addend of the symbolic relocation it gets instead.  */
4700
0
      if (addend != 0
4701
0
    && elf64_alpha_ifunc_reloc_p (r_type)
4702
0
    && elf64_alpha_ifunc_p (h, sym)
4703
0
    && (input_section->flags & SEC_ALLOC)
4704
0
    && (elf64_alpha_ifunc_irelplt_p (h, sym, info)
4705
0
        || (bfd_link_pic (info) && !dynamic_symbol_p)))
4706
0
  {
4707
0
    _bfd_error_handler
4708
      /* xgettext:c-format */
4709
0
      (_("%pB: %s relocation against STT_GNU_IFUNC symbol `%s' has a "
4710
0
         "non-zero addend"),
4711
0
       input_bfd, howto->name,
4712
0
       elf64_alpha_sym_name (input_bfd, symtab_hdr, h, sym, sec));
4713
0
    ret_val = false;
4714
0
    continue;
4715
0
  }
4716
4717
0
      switch (r_type)
4718
0
  {
4719
0
  case R_ALPHA_GPDISP:
4720
0
    {
4721
0
      bfd_byte *p_ldah, *p_lda;
4722
4723
0
      BFD_ASSERT(gp != 0);
4724
4725
0
      value = (input_section->output_section->vma
4726
0
         + input_section->output_offset
4727
0
         + rel->r_offset);
4728
4729
0
      p_ldah = contents + rel->r_offset;
4730
0
      p_lda = p_ldah + rel->r_addend;
4731
4732
0
      r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - value,
4733
0
               p_ldah, p_lda);
4734
0
    }
4735
0
    break;
4736
4737
0
  case R_ALPHA_LITERAL:
4738
0
    BFD_ASSERT(sgot != NULL);
4739
0
    BFD_ASSERT(gp != 0);
4740
0
    BFD_ASSERT(gotent != NULL);
4741
0
    BFD_ASSERT(gotent->use_count >= 1);
4742
4743
0
    if (!gotent->reloc_done)
4744
0
      {
4745
0
        gotent->reloc_done = 1;
4746
4747
0
        bfd_put_64 (info->output_bfd, value,
4748
0
        alpha_got_slot (gotent, 0));
4749
4750
        /* If the symbol has been forced local, output a RELATIVE
4751
     reloc, otherwise it will be handled in finish_dynamic_symbol.
4752
     An IFUNC gets an IRELATIVE instead.  */
4753
0
        if (elf64_alpha_ifunc_irelplt_p (h, sym, info))
4754
0
    elf64_alpha_emit_dynrel (info->output_bfd, info, sgot,
4755
0
           elf_hash_table (info)->irelplt,
4756
0
           gotent->got_offset, 0,
4757
0
           R_ALPHA_IRELATIVE, value);
4758
0
        else if (bfd_link_pic (info)
4759
0
           && !dynamic_symbol_p
4760
0
           && !undef_weak_ref)
4761
0
    elf64_alpha_emit_dynrel (info->output_bfd, info, sgot, srelgot,
4762
0
           gotent->got_offset, 0,
4763
0
           (elf64_alpha_ifunc_p (h, sym)
4764
0
            ? R_ALPHA_IRELATIVE
4765
0
            : R_ALPHA_RELATIVE),
4766
0
           value);
4767
0
      }
4768
4769
0
    value = (sgot->output_section->vma
4770
0
       + sgot->output_offset
4771
0
       + gotent->got_offset);
4772
0
    value -= gp;
4773
0
    goto default_reloc;
4774
4775
0
  case R_ALPHA_GPREL32:
4776
0
  case R_ALPHA_GPREL16:
4777
0
  case R_ALPHA_GPRELLOW:
4778
0
    if (dynamic_symbol_p)
4779
0
      {
4780
0
        _bfd_error_handler
4781
    /* xgettext:c-format */
4782
0
    (_("%pB: gp-relative relocation against dynamic symbol %s"),
4783
0
     input_bfd, h->root.root.root.string);
4784
0
        ret_val = false;
4785
0
      }
4786
0
    BFD_ASSERT(gp != 0);
4787
0
    value -= gp;
4788
0
    goto default_reloc;
4789
4790
0
  case R_ALPHA_GPRELHIGH:
4791
0
    if (dynamic_symbol_p)
4792
0
      {
4793
0
        _bfd_error_handler
4794
    /* xgettext:c-format */
4795
0
    (_("%pB: gp-relative relocation against dynamic symbol %s"),
4796
0
     input_bfd, h->root.root.root.string);
4797
0
        ret_val = false;
4798
0
      }
4799
0
    BFD_ASSERT(gp != 0);
4800
0
    value -= gp;
4801
0
    value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4802
0
    goto default_reloc;
4803
4804
0
  case R_ALPHA_HINT:
4805
    /* A call to a dynamic symbol is definitely out of range of
4806
       the 16-bit displacement.  Don't bother writing anything.  */
4807
0
    if (dynamic_symbol_p)
4808
0
      {
4809
0
        r = bfd_reloc_ok;
4810
0
        break;
4811
0
      }
4812
    /* The regular PC-relative stuff measures from the start of
4813
       the instruction rather than the end.  */
4814
0
    value -= 4;
4815
0
    goto default_reloc;
4816
4817
0
  case R_ALPHA_BRADDR:
4818
0
    if (dynamic_symbol_p)
4819
0
      {
4820
0
        _bfd_error_handler
4821
    /* xgettext:c-format */
4822
0
    (_("%pB: pc-relative relocation against dynamic symbol %s"),
4823
0
     input_bfd, h->root.root.root.string);
4824
0
        ret_val = false;
4825
0
      }
4826
    /* The regular PC-relative stuff measures from the start of
4827
       the instruction rather than the end.  */
4828
0
    value -= 4;
4829
0
    goto default_reloc;
4830
4831
0
  case R_ALPHA_BRSGP:
4832
0
    {
4833
0
      int other;
4834
0
      const char *name;
4835
4836
      /* The regular PC-relative stuff measures from the start of
4837
         the instruction rather than the end.  */
4838
0
      value -= 4;
4839
4840
      /* The source and destination gp must be the same.  Note that
4841
         the source will always have an assigned gp, since we forced
4842
         one in check_relocs, but that the destination may not, as
4843
         it might not have had any relocations at all.  Also take
4844
         care not to crash if H is an undefined symbol.  */
4845
0
      if (h != NULL && sec != NULL
4846
0
    && alpha_elf_tdata (sec->owner)->gotobj
4847
0
    && gotobj != alpha_elf_tdata (sec->owner)->gotobj)
4848
0
        {
4849
0
    _bfd_error_handler
4850
      /* xgettext:c-format */
4851
0
      (_("%pB: change in gp: BRSGP %s"),
4852
0
       input_bfd, h->root.root.root.string);
4853
0
    ret_val = false;
4854
0
        }
4855
4856
      /* The symbol should be marked either NOPV or STD_GPLOAD.  */
4857
0
      if (h != NULL)
4858
0
        other = h->root.other;
4859
0
      else
4860
0
        other = sym->st_other;
4861
0
      switch (other & STO_ALPHA_STD_GPLOAD)
4862
0
        {
4863
0
        case STO_ALPHA_NOPV:
4864
0
    break;
4865
0
        case STO_ALPHA_STD_GPLOAD:
4866
0
    value += 8;
4867
0
    break;
4868
0
        default:
4869
0
    name = elf64_alpha_sym_name (input_bfd, symtab_hdr, h, sym,
4870
0
               sec);
4871
0
    _bfd_error_handler
4872
      /* xgettext:c-format */
4873
0
      (_("%pB: !samegp reloc against symbol without .prologue: %s"),
4874
0
       input_bfd, name);
4875
0
    ret_val = false;
4876
0
    break;
4877
0
        }
4878
4879
0
      goto default_reloc;
4880
0
    }
4881
4882
0
  case R_ALPHA_REFLONG:
4883
0
  case R_ALPHA_REFQUAD:
4884
0
  case R_ALPHA_DTPREL64:
4885
0
  case R_ALPHA_TPREL64:
4886
0
    {
4887
0
      long dynindx, dyntype = r_type;
4888
0
      bfd_vma dynaddend;
4889
0
      asection *srel_out = srel;
4890
4891
      /* Careful here to remember RELATIVE relocations for global
4892
         variables for symbolic shared objects.  */
4893
4894
0
      if (dynamic_symbol_p)
4895
0
        {
4896
0
    BFD_ASSERT(h->root.dynindx != -1);
4897
0
    dynindx = h->root.dynindx;
4898
0
    dynaddend = addend;
4899
0
    addend = 0, value = 0;
4900
0
        }
4901
0
      else if (r_type == R_ALPHA_DTPREL64)
4902
0
        {
4903
0
    BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4904
0
    value -= dtp_base;
4905
0
    goto default_reloc;
4906
0
        }
4907
0
      else if (r_type == R_ALPHA_TPREL64)
4908
0
        {
4909
0
    BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4910
0
    if (!bfd_link_dll (info))
4911
0
      {
4912
0
        value -= tp_base;
4913
0
        goto default_reloc;
4914
0
      }
4915
0
    dynindx = 0;
4916
0
    dynaddend = value - dtp_base;
4917
0
        }
4918
0
      else if (elf64_alpha_ifunc_reloc_p (r_type)
4919
0
         && elf64_alpha_ifunc_p (h, sym)
4920
0
         && (input_section->flags & SEC_ALLOC))
4921
0
        {
4922
    /* In a PIC link the RELATIVE this would otherwise get would
4923
       store the address of the resolver, and space for it is
4924
       already reserved, so the IRELATIVE takes its place.  A
4925
       non-PIC link reserved nothing there and puts it in
4926
       .rela.iplt instead.  */
4927
0
    dynindx = 0;
4928
0
    dyntype = R_ALPHA_IRELATIVE;
4929
0
    dynaddend = value;
4930
4931
0
    if (elf64_alpha_ifunc_irelplt_p (h, sym, info))
4932
0
      {
4933
0
        srel_out = elf_hash_table (info)->irelplt;
4934
4935
        /* Every entry of .rela.iplt is applied, so it must not
4936
           contain the R_ALPHA_NONE that elf64_alpha_emit_dynrel
4937
           writes for a relocation whose place in the output has
4938
           been removed.  Sizing .rela.iplt skips the sections that
4939
           do not make it into the output, but .eh_frame editing
4940
           deletes the place of a relocation later still, long
4941
           after the section has been sized and given an address;
4942
           there is nothing left to shrink it by.  Report it rather
4943
           than produce an executable that faults at startup.  */
4944
0
        if ((_bfd_elf_section_offset (info->output_bfd, info,
4945
0
              input_section, rel->r_offset)
4946
0
       | 1) == (bfd_vma) -1)
4947
0
          {
4948
0
      _bfd_error_handler
4949
        /* xgettext:c-format */
4950
0
        (_("%pB: cannot resolve STT_GNU_IFUNC symbol `%s': "
4951
0
           "the place of its %s relocation in `%pA' was "
4952
0
           "deleted"),
4953
0
         input_bfd,
4954
0
         elf64_alpha_sym_name (input_bfd, symtab_hdr, h,
4955
0
             sym, sec),
4956
0
         howto->name, input_section);
4957
0
      ret_val = false;
4958
0
      continue;
4959
0
          }
4960
4961
        /* Startup code in a static executable applies this after
4962
           the kernel has mapped the segment, and unlike the
4963
           dynamic linker it cannot make a read-only one writable.
4964
           Other targets hand out the address of a PLT stub
4965
           instead; alpha has no PLT entry for a non-dynamic
4966
           function.  */
4967
0
        if ((input_section->flags & SEC_READONLY) != 0
4968
0
      && !elf_hash_table (info)->dynamic_sections_created)
4969
0
          {
4970
0
      _bfd_error_handler
4971
        /* xgettext:c-format */
4972
0
        (_("%pB: address of STT_GNU_IFUNC symbol `%s' in "
4973
0
           "read-only section `%pA' cannot be relocated in "
4974
0
           "a static link"),
4975
0
         input_bfd,
4976
0
         elf64_alpha_sym_name (input_bfd, symtab_hdr, h,
4977
0
             sym, sec),
4978
0
         input_section);
4979
0
      ret_val = false;
4980
0
          }
4981
0
      }
4982
0
        }
4983
0
      else if (bfd_link_pic (info)
4984
0
         && r_symndx != STN_UNDEF
4985
0
         && (input_section->flags & SEC_ALLOC)
4986
0
         && !undef_weak_ref
4987
0
         && !(unresolved_reloc
4988
0
        && (_bfd_elf_section_offset (info->output_bfd, info,
4989
0
                   input_section,
4990
0
                   rel->r_offset)
4991
0
            == (bfd_vma) -1)))
4992
0
        {
4993
0
    if (r_type == R_ALPHA_REFLONG)
4994
0
      {
4995
0
        _bfd_error_handler
4996
          /* xgettext:c-format */
4997
0
          (_("%pB: unhandled dynamic relocation against %s"),
4998
0
           input_bfd,
4999
0
           h->root.root.root.string);
5000
0
        ret_val = false;
5001
0
      }
5002
0
    dynindx = 0;
5003
0
    dyntype = R_ALPHA_RELATIVE;
5004
0
    dynaddend = value;
5005
0
        }
5006
0
      else
5007
0
        goto default_reloc;
5008
5009
0
      if (input_section->flags & SEC_ALLOC)
5010
0
        elf64_alpha_emit_dynrel (info->output_bfd, info, input_section,
5011
0
               srel_out, rel->r_offset, dynindx,
5012
0
               dyntype, dynaddend);
5013
0
    }
5014
0
    goto default_reloc;
5015
5016
0
  case R_ALPHA_SREL16:
5017
0
  case R_ALPHA_SREL32:
5018
0
  case R_ALPHA_SREL64:
5019
0
    if (dynamic_symbol_p)
5020
0
      {
5021
0
        _bfd_error_handler
5022
    /* xgettext:c-format */
5023
0
    (_("%pB: pc-relative relocation against dynamic symbol %s"),
5024
0
     input_bfd, h->root.root.root.string);
5025
0
        ret_val = false;
5026
0
      }
5027
0
    else if (bfd_link_pic (info)
5028
0
       && undef_weak_ref)
5029
0
      {
5030
0
        _bfd_error_handler
5031
    /* xgettext:c-format */
5032
0
    (_("%pB: pc-relative relocation against undefined weak symbol %s"),
5033
0
     input_bfd, h->root.root.root.string);
5034
0
        ret_val = false;
5035
0
      }
5036
5037
5038
    /* ??? .eh_frame references to discarded sections will be smashed
5039
       to relocations against SHN_UNDEF.  The .eh_frame format allows
5040
       NULL to be encoded as 0 in any format, so this works here.  */
5041
0
    if (r_symndx == STN_UNDEF
5042
0
        || (unresolved_reloc
5043
0
      && _bfd_elf_section_offset (info->output_bfd, info,
5044
0
                input_section,
5045
0
                rel->r_offset) == (bfd_vma) -1))
5046
0
      howto = (elf64_alpha_howto_table
5047
0
         + (r_type - R_ALPHA_SREL32 + R_ALPHA_REFLONG));
5048
0
    goto default_reloc;
5049
5050
0
  case R_ALPHA_TLSLDM:
5051
    /* Ignore the symbol for the relocation.  The result is always
5052
       the current module.  */
5053
0
    dynamic_symbol_p = 0;
5054
    /* FALLTHRU */
5055
5056
0
  case R_ALPHA_TLSGD:
5057
0
    if (!gotent->reloc_done)
5058
0
      {
5059
0
        gotent->reloc_done = 1;
5060
5061
        /* Note that the module index for the main program is 1.  */
5062
0
        bfd_put_64 (info->output_bfd,
5063
0
        !bfd_link_pic (info) && !dynamic_symbol_p,
5064
0
        alpha_got_slot (gotent, 0));
5065
5066
        /* If the symbol has been forced local, output a
5067
     DTPMOD64 reloc, otherwise it will be handled in
5068
     finish_dynamic_symbol.  */
5069
0
        if (bfd_link_pic (info) && !dynamic_symbol_p)
5070
0
    elf64_alpha_emit_dynrel (info->output_bfd, info,
5071
0
           sgot, srelgot,
5072
0
           gotent->got_offset, 0,
5073
0
           R_ALPHA_DTPMOD64, 0);
5074
5075
0
        if (dynamic_symbol_p || r_type == R_ALPHA_TLSLDM)
5076
0
    value = 0;
5077
0
        else
5078
0
    {
5079
0
      BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
5080
0
      value -= dtp_base;
5081
0
    }
5082
0
        bfd_put_64 (info->output_bfd, value,
5083
0
        alpha_got_slot (gotent, 8));
5084
0
      }
5085
5086
0
    value = (sgot->output_section->vma
5087
0
       + sgot->output_offset
5088
0
       + gotent->got_offset);
5089
0
    value -= gp;
5090
0
    goto default_reloc;
5091
5092
0
  case R_ALPHA_DTPRELHI:
5093
0
  case R_ALPHA_DTPRELLO:
5094
0
  case R_ALPHA_DTPREL16:
5095
0
    if (dynamic_symbol_p)
5096
0
      {
5097
0
        _bfd_error_handler
5098
    /* xgettext:c-format */
5099
0
    (_("%pB: dtp-relative relocation against dynamic symbol %s"),
5100
0
     input_bfd, h->root.root.root.string);
5101
0
        ret_val = false;
5102
0
      }
5103
0
    BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
5104
0
    value -= dtp_base;
5105
0
    if (r_type == R_ALPHA_DTPRELHI)
5106
0
      value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
5107
0
    goto default_reloc;
5108
5109
0
  case R_ALPHA_TPRELHI:
5110
0
  case R_ALPHA_TPRELLO:
5111
0
  case R_ALPHA_TPREL16:
5112
0
    if (bfd_link_dll (info))
5113
0
      {
5114
0
        _bfd_error_handler
5115
    /* xgettext:c-format */
5116
0
    (_("%pB: TLS local exec code cannot be linked into shared objects"),
5117
0
    input_bfd);
5118
0
        ret_val = false;
5119
0
      }
5120
0
    else if (dynamic_symbol_p)
5121
0
      {
5122
0
        _bfd_error_handler
5123
    /* xgettext:c-format */
5124
0
    (_("%pB: tp-relative relocation against dynamic symbol %s"),
5125
0
     input_bfd, h->root.root.root.string);
5126
0
        ret_val = false;
5127
0
      }
5128
0
    else if (elf_link_local_undefweak_p (h ? &h->root : NULL, info))
5129
      /* NB: The local undefined TLS symbol address isn't usable
5130
         since it isn't mapped to any TLS storage.  Set it to 0
5131
         to avoid relocation overflow.  */
5132
0
      value = 0;
5133
0
    else
5134
0
      {
5135
0
        value -= tp_base;
5136
0
        if (r_type == R_ALPHA_TPRELHI)
5137
0
    value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
5138
0
      }
5139
0
    goto default_reloc;
5140
5141
0
  case R_ALPHA_GOTDTPREL:
5142
0
  case R_ALPHA_GOTTPREL:
5143
0
    BFD_ASSERT(sgot != NULL);
5144
0
    BFD_ASSERT(gp != 0);
5145
0
    BFD_ASSERT(gotent != NULL);
5146
0
    BFD_ASSERT(gotent->use_count >= 1);
5147
5148
0
    if (!gotent->reloc_done)
5149
0
      {
5150
0
        gotent->reloc_done = 1;
5151
5152
0
        if (dynamic_symbol_p)
5153
0
    value = 0;
5154
0
        else
5155
0
    {
5156
0
      if (elf_link_local_undefweak_p (h ? &h->root : NULL, info))
5157
        /* NB: The local undefined TLS symbol address isn't
5158
           usable since it isn't mapped to any TLS storage.
5159
           Set it to 0 to avoid relocation overflow.  */
5160
0
        value = 0;
5161
0
      else if (r_type == R_ALPHA_GOTDTPREL)
5162
0
        value -= dtp_base;
5163
0
      else if (bfd_link_executable (info))
5164
0
        value -= tp_base;
5165
0
      else
5166
0
        {
5167
0
          elf64_alpha_emit_dynrel (info->output_bfd, info,
5168
0
                 sgot, srelgot,
5169
0
                 gotent->got_offset, 0,
5170
0
                 R_ALPHA_TPREL64,
5171
0
                 value - dtp_base);
5172
0
          value = 0;
5173
0
        }
5174
0
    }
5175
0
        bfd_put_64 (info->output_bfd, value,
5176
0
        alpha_got_slot (gotent, 0));
5177
0
      }
5178
5179
0
    value = (sgot->output_section->vma
5180
0
       + sgot->output_offset
5181
0
       + gotent->got_offset);
5182
0
    value -= gp;
5183
0
    goto default_reloc;
5184
5185
0
  default:
5186
0
  default_reloc:
5187
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5188
0
          contents, rel->r_offset, value, 0);
5189
0
    break;
5190
0
  }
5191
5192
0
      switch (r)
5193
0
  {
5194
0
  case bfd_reloc_ok:
5195
0
    break;
5196
5197
0
  case bfd_reloc_overflow:
5198
0
    {
5199
0
      const char *name;
5200
5201
      /* Don't warn if the overflow is due to pc relative reloc
5202
         against discarded section.  Section optimization code should
5203
         handle it.  */
5204
5205
0
      if (r_symndx < symtab_hdr->sh_info
5206
0
    && sec != NULL && howto->pc_relative
5207
0
    && discarded_section (sec))
5208
0
        break;
5209
5210
0
      if (h != NULL)
5211
0
        name = NULL;
5212
0
      else
5213
0
        {
5214
0
    name = (bfd_elf_string_from_elf_section
5215
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
5216
0
    if (name == NULL)
5217
0
      return false;
5218
0
    if (*name == '\0')
5219
0
      name = bfd_section_name (sec);
5220
0
        }
5221
0
      (*info->callbacks->reloc_overflow)
5222
0
        (info, (h ? &h->root.root : NULL), name, howto->name,
5223
0
         (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5224
0
    }
5225
0
    break;
5226
5227
0
  default:
5228
0
  case bfd_reloc_outofrange:
5229
0
    abort ();
5230
0
  }
5231
0
    }
5232
5233
0
  return ret_val;
5234
0
}
5235
5236
/* Finish up dynamic symbol handling.  We set the contents of various
5237
   dynamic sections here.  */
5238
5239
static bool
5240
elf64_alpha_finish_dynamic_symbol (struct bfd_link_info *info,
5241
           struct elf_link_hash_entry *h,
5242
           Elf_Internal_Sym *sym)
5243
0
{
5244
0
  struct alpha_elf_link_hash_entry *ah = (struct alpha_elf_link_hash_entry *)h;
5245
5246
  /* Only a defined IFUNC gets here without dynamic sections.  There is no
5247
     PLT to fill in, whatever needs_plt says, and relocate_section has
5248
     already written its GOT entries.  */
5249
0
  if (!elf_hash_table (info)->dynamic_sections_created)
5250
0
    return true;
5251
5252
0
  if (h->needs_plt)
5253
0
    {
5254
      /* Fill in the .plt entry for this symbol.  */
5255
0
      asection *splt, *sgot, *srel;
5256
0
      Elf_Internal_Rela outrel;
5257
0
      bfd_byte *loc;
5258
0
      bfd_vma got_addr, plt_addr;
5259
0
      bfd_vma plt_index;
5260
0
      struct alpha_elf_got_entry *gotent;
5261
5262
0
      BFD_ASSERT (h->dynindx != -1);
5263
5264
0
      splt = elf_hash_table (info)->splt;
5265
0
      BFD_ASSERT (splt != NULL);
5266
0
      srel = elf_hash_table (info)->srelplt;
5267
0
      BFD_ASSERT (srel != NULL);
5268
5269
0
      for (gotent = ah->got_entries; gotent ; gotent = gotent->next)
5270
0
  if (gotent->reloc_type == R_ALPHA_LITERAL
5271
0
      && gotent->use_count > 0)
5272
0
    {
5273
0
      unsigned int insn;
5274
0
      int disp;
5275
5276
0
      sgot = alpha_elf_tdata (gotent->gotobj)->got;
5277
0
      BFD_ASSERT (sgot != NULL);
5278
5279
0
      BFD_ASSERT (gotent->got_offset != -1);
5280
0
      BFD_ASSERT (gotent->plt_offset != -1);
5281
5282
0
      got_addr = (sgot->output_section->vma
5283
0
      + sgot->output_offset
5284
0
      + gotent->got_offset);
5285
0
      plt_addr = (splt->output_section->vma
5286
0
      + splt->output_offset
5287
0
      + gotent->plt_offset);
5288
5289
0
      plt_index = (gotent->plt_offset-PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
5290
5291
      /* Fill in the entry in the procedure linkage table.  */
5292
0
      if (elf64_alpha_use_secureplt)
5293
0
        {
5294
0
    disp = (PLT_HEADER_SIZE - 4) - (gotent->plt_offset + 4);
5295
0
    insn = INSN_AD (INSN_BR, 31, disp);
5296
0
    bfd_put_32 (info->output_bfd, insn,
5297
0
          splt->contents + gotent->plt_offset);
5298
5299
0
    plt_index = ((gotent->plt_offset - NEW_PLT_HEADER_SIZE)
5300
0
           / NEW_PLT_ENTRY_SIZE);
5301
0
        }
5302
0
      else
5303
0
        {
5304
0
    disp = -(gotent->plt_offset + 4);
5305
0
    insn = INSN_AD (INSN_BR, 28, disp);
5306
0
    bfd_put_32 (info->output_bfd, insn,
5307
0
          splt->contents + gotent->plt_offset);
5308
0
    bfd_put_32 (info->output_bfd, INSN_UNOP,
5309
0
          splt->contents + gotent->plt_offset + 4);
5310
0
    bfd_put_32 (info->output_bfd, INSN_UNOP,
5311
0
          splt->contents + gotent->plt_offset + 8);
5312
5313
0
    plt_index = ((gotent->plt_offset - OLD_PLT_HEADER_SIZE)
5314
0
           / OLD_PLT_ENTRY_SIZE);
5315
0
        }
5316
5317
      /* Fill in the entry in the .rela.plt section.  */
5318
0
      outrel.r_offset = got_addr;
5319
0
      outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
5320
0
      outrel.r_addend = 0;
5321
5322
0
      loc = srel->contents + plt_index * sizeof (Elf64_External_Rela);
5323
0
      bfd_elf64_swap_reloca_out (info->output_bfd, &outrel, loc);
5324
5325
      /* Fill in the entry in the .got.  */
5326
0
      bfd_put_64 (info->output_bfd, plt_addr,
5327
0
      alpha_got_slot (gotent, 0));
5328
0
    }
5329
0
    }
5330
0
  else if (alpha_elf_dynamic_symbol_p (h, info))
5331
0
    {
5332
      /* Fill in the dynamic relocations for this symbol's .got entries.  */
5333
0
      asection *srel;
5334
0
      struct alpha_elf_got_entry *gotent;
5335
5336
0
      srel = elf_hash_table (info)->srelgot;
5337
0
      BFD_ASSERT (srel != NULL);
5338
5339
0
      for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
5340
0
     gotent != NULL;
5341
0
     gotent = gotent->next)
5342
0
  {
5343
0
    asection *sgot;
5344
0
    long r_type;
5345
5346
0
    if (gotent->use_count == 0)
5347
0
      continue;
5348
5349
0
    sgot = alpha_elf_tdata (gotent->gotobj)->got;
5350
5351
0
    r_type = gotent->reloc_type;
5352
0
    switch (r_type)
5353
0
      {
5354
0
      case R_ALPHA_LITERAL:
5355
0
        r_type = R_ALPHA_GLOB_DAT;
5356
0
        break;
5357
0
      case R_ALPHA_TLSGD:
5358
0
        r_type = R_ALPHA_DTPMOD64;
5359
0
        break;
5360
0
      case R_ALPHA_GOTDTPREL:
5361
0
        r_type = R_ALPHA_DTPREL64;
5362
0
        break;
5363
0
      case R_ALPHA_GOTTPREL:
5364
0
        r_type = R_ALPHA_TPREL64;
5365
0
        break;
5366
0
      case R_ALPHA_TLSLDM:
5367
0
      default:
5368
0
        abort ();
5369
0
      }
5370
5371
0
    elf64_alpha_emit_dynrel (info->output_bfd, info, sgot, srel,
5372
0
           gotent->got_offset, h->dynindx,
5373
0
           r_type, gotent->addend);
5374
5375
0
    if (gotent->reloc_type == R_ALPHA_TLSGD)
5376
0
      elf64_alpha_emit_dynrel (info->output_bfd, info, sgot, srel,
5377
0
             gotent->got_offset + 8, h->dynindx,
5378
0
             R_ALPHA_DTPREL64, gotent->addend);
5379
0
  }
5380
0
    }
5381
5382
  /* Mark some specially defined symbols as absolute.  */
5383
0
  if (h == elf_hash_table (info)->hdynamic
5384
0
      || h == elf_hash_table (info)->hgot
5385
0
      || h == elf_hash_table (info)->hplt)
5386
0
    sym->st_shndx = SHN_ABS;
5387
5388
0
  return true;
5389
0
}
5390
5391
/* Finish up the dynamic sections.  */
5392
5393
static bool
5394
elf64_alpha_finish_dynamic_sections (struct bfd_link_info *info,
5395
             bfd_byte *buf ATTRIBUTE_UNUSED)
5396
0
{
5397
0
  bfd *dynobj;
5398
0
  asection *sdyn;
5399
5400
0
  dynobj = elf_hash_table (info)->dynobj;
5401
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5402
5403
0
  if (elf_hash_table (info)->dynamic_sections_created)
5404
0
    {
5405
0
      asection *splt, *sgotplt, *srelaplt;
5406
0
      Elf64_External_Dyn *dyncon, *dynconend;
5407
0
      bfd_vma plt_vma, gotplt_vma;
5408
5409
0
      splt = elf_hash_table (info)->splt;
5410
0
      srelaplt = elf_hash_table (info)->srelplt;
5411
0
      BFD_ASSERT (splt != NULL && sdyn != NULL);
5412
5413
0
      plt_vma = splt->output_section->vma + splt->output_offset;
5414
5415
0
      gotplt_vma = 0;
5416
0
      if (elf64_alpha_use_secureplt)
5417
0
  {
5418
0
    sgotplt = elf_hash_table (info)->sgotplt;
5419
0
    BFD_ASSERT (sgotplt != NULL);
5420
0
    if (sgotplt->size > 0)
5421
0
      gotplt_vma = sgotplt->output_section->vma + sgotplt->output_offset;
5422
0
  }
5423
5424
0
      dyncon = (Elf64_External_Dyn *) sdyn->contents;
5425
0
      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5426
0
      for (; dyncon < dynconend; dyncon++)
5427
0
  {
5428
0
    Elf_Internal_Dyn dyn;
5429
5430
0
    bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
5431
5432
0
    switch (dyn.d_tag)
5433
0
      {
5434
0
      case DT_PLTGOT:
5435
0
        dyn.d_un.d_ptr
5436
0
    = elf64_alpha_use_secureplt ? gotplt_vma : plt_vma;
5437
0
        break;
5438
0
      case DT_PLTRELSZ:
5439
0
        dyn.d_un.d_val = srelaplt ? srelaplt->size : 0;
5440
0
        break;
5441
0
      case DT_JMPREL:
5442
0
        dyn.d_un.d_ptr = srelaplt ? (srelaplt->output_section->vma
5443
0
             + srelaplt->output_offset) : 0;
5444
0
        break;
5445
0
      }
5446
5447
0
    bfd_elf64_swap_dyn_out (info->output_bfd, &dyn, dyncon);
5448
0
  }
5449
5450
      /* Initialize the plt header.  */
5451
0
      if (splt->size > 0)
5452
0
  {
5453
0
    unsigned int insn;
5454
0
    int ofs;
5455
5456
0
    if (elf64_alpha_use_secureplt)
5457
0
      {
5458
0
        ofs = gotplt_vma - (plt_vma + PLT_HEADER_SIZE);
5459
5460
0
        insn = INSN_ABC (INSN_SUBQ, 27, 28, 25);
5461
0
        bfd_put_32 (info->output_bfd, insn, splt->contents);
5462
5463
0
        insn = INSN_ABO (INSN_LDAH, 28, 28, (ofs + 0x8000) >> 16);
5464
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 4);
5465
5466
0
        insn = INSN_ABC (INSN_S4SUBQ, 25, 25, 25);
5467
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 8);
5468
5469
0
        insn = INSN_ABO (INSN_LDA, 28, 28, ofs);
5470
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 12);
5471
5472
0
        insn = INSN_ABO (INSN_LDQ, 27, 28, 0);
5473
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 16);
5474
5475
0
        insn = INSN_ABC (INSN_ADDQ, 25, 25, 25);
5476
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 20);
5477
5478
0
        insn = INSN_ABO (INSN_LDQ, 28, 28, 8);
5479
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 24);
5480
5481
0
        insn = INSN_AB (INSN_JMP, 31, 27);
5482
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 28);
5483
5484
0
        insn = INSN_AD (INSN_BR, 28, -PLT_HEADER_SIZE);
5485
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 32);
5486
0
      }
5487
0
    else
5488
0
      {
5489
0
        insn = INSN_AD (INSN_BR, 27, 0);  /* br $27, .+4 */
5490
0
        bfd_put_32 (info->output_bfd, insn, splt->contents);
5491
5492
0
        insn = INSN_ABO (INSN_LDQ, 27, 27, 12);
5493
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 4);
5494
5495
0
        insn = INSN_UNOP;
5496
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 8);
5497
5498
0
        insn = INSN_AB (INSN_JMP, 27, 27);
5499
0
        bfd_put_32 (info->output_bfd, insn, splt->contents + 12);
5500
5501
        /* The next two words will be filled in by ld.so.  */
5502
0
        bfd_put_64 (info->output_bfd, 0, splt->contents + 16);
5503
0
        bfd_put_64 (info->output_bfd, 0, splt->contents + 24);
5504
0
      }
5505
5506
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
5507
0
  }
5508
0
    }
5509
5510
0
  return true;
5511
0
}
5512
5513
/* We need to use a special link routine to handle the .mdebug section.
5514
   We need to merge all instances of these sections together, not write
5515
   them all out sequentially.  */
5516
5517
static bool
5518
elf64_alpha_final_link (bfd *abfd, struct bfd_link_info *info)
5519
0
{
5520
0
  asection *o;
5521
0
  struct bfd_link_order *p;
5522
0
  asection *mdebug_sec;
5523
0
  asection *irelplt;
5524
0
  struct ecoff_debug_info debug;
5525
0
  const struct ecoff_debug_swap *swap
5526
0
    = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
5527
0
  HDRR *symhdr = &debug.symbolic_header;
5528
0
  void * mdebug_handle = NULL;
5529
0
  struct alpha_elf_link_hash_table * htab;
5530
5531
0
  htab = alpha_elf_hash_table (info);
5532
0
  if (htab == NULL)
5533
0
    return false;
5534
5535
  /* Go through the sections and collect the mdebug information.  */
5536
0
  mdebug_sec = NULL;
5537
0
  for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5538
0
    {
5539
0
      if (strcmp (o->name, ".mdebug") == 0)
5540
0
  {
5541
0
    struct extsym_info einfo;
5542
5543
    /* We have found the .mdebug section in the output file.
5544
       Look through all the link_orders comprising it and merge
5545
       the information together.  */
5546
0
    symhdr->magic = swap->sym_magic;
5547
    /* FIXME: What should the version stamp be?  */
5548
0
    symhdr->vstamp = 0;
5549
0
    symhdr->ilineMax = 0;
5550
0
    symhdr->cbLine = 0;
5551
0
    symhdr->idnMax = 0;
5552
0
    symhdr->ipdMax = 0;
5553
0
    symhdr->isymMax = 0;
5554
0
    symhdr->ioptMax = 0;
5555
0
    symhdr->iauxMax = 0;
5556
0
    symhdr->issMax = 0;
5557
0
    symhdr->issExtMax = 0;
5558
0
    symhdr->ifdMax = 0;
5559
0
    symhdr->crfd = 0;
5560
0
    symhdr->iextMax = 0;
5561
5562
    /* We accumulate the debugging information itself in the
5563
       debug_info structure.  */
5564
0
    debug.line = NULL;
5565
0
    debug.external_dnr = NULL;
5566
0
    debug.external_pdr = NULL;
5567
0
    debug.external_sym = NULL;
5568
0
    debug.external_opt = NULL;
5569
0
    debug.external_aux = NULL;
5570
0
    debug.ss = NULL;
5571
0
    debug.ssext = debug.ssext_end = NULL;
5572
0
    debug.external_fdr = NULL;
5573
0
    debug.external_rfd = NULL;
5574
0
    debug.external_ext = debug.external_ext_end = NULL;
5575
5576
0
    mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
5577
0
    if (mdebug_handle == NULL)
5578
0
      return false;
5579
5580
0
    if (1)
5581
0
      {
5582
0
        asection *s;
5583
0
        EXTR esym;
5584
0
        bfd_vma last = 0;
5585
0
        unsigned int i;
5586
0
        static const char * const name[] =
5587
0
    {
5588
0
      ".text", ".init", ".fini", ".data",
5589
0
      ".rodata", ".sdata", ".sbss", ".bss"
5590
0
    };
5591
0
        static const int sc[] = { scText, scInit, scFini, scData,
5592
0
            scRData, scSData, scSBss, scBss };
5593
5594
0
        esym.jmptbl = 0;
5595
0
        esym.cobol_main = 0;
5596
0
        esym.weakext = 0;
5597
0
        esym.reserved = 0;
5598
0
        esym.ifd = ifdNil;
5599
0
        esym.asym.iss = issNil;
5600
0
        esym.asym.st = stLocal;
5601
0
        esym.asym.reserved = 0;
5602
0
        esym.asym.index = indexNil;
5603
0
        for (i = 0; i < 8; i++)
5604
0
    {
5605
0
      esym.asym.sc = sc[i];
5606
0
      s = bfd_get_section_by_name (abfd, name[i]);
5607
0
      if (s != NULL)
5608
0
        {
5609
0
          esym.asym.value = s->vma;
5610
0
          last = s->vma + s->size;
5611
0
        }
5612
0
      else
5613
0
        esym.asym.value = last;
5614
5615
0
      if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
5616
0
                  name[i], &esym))
5617
0
        return false;
5618
0
    }
5619
0
      }
5620
5621
0
    for (p = o->map_head.link_order;
5622
0
         p != (struct bfd_link_order *) NULL;
5623
0
         p = p->next)
5624
0
      {
5625
0
        asection *input_section;
5626
0
        bfd *input_bfd;
5627
0
        const struct ecoff_debug_swap *input_swap;
5628
0
        struct ecoff_debug_info input_debug;
5629
0
        char *eraw_src;
5630
0
        char *eraw_end;
5631
5632
0
        if (p->type != bfd_indirect_link_order)
5633
0
    {
5634
0
      if (p->type == bfd_data_link_order)
5635
0
        continue;
5636
0
      abort ();
5637
0
    }
5638
5639
0
        input_section = p->u.indirect.section;
5640
0
        input_bfd = input_section->owner;
5641
5642
0
        if (! is_alpha_elf (input_bfd))
5643
    /* I don't know what a non ALPHA ELF bfd would be
5644
       doing with a .mdebug section, but I don't really
5645
       want to deal with it.  */
5646
0
    continue;
5647
5648
0
        input_swap = (get_elf_backend_data (input_bfd)
5649
0
          ->elf_backend_ecoff_debug_swap);
5650
5651
0
        BFD_ASSERT (p->size == input_section->size);
5652
5653
        /* The ECOFF linking code expects that we have already
5654
     read in the debugging information and set up an
5655
     ecoff_debug_info structure, so we do that now.  */
5656
0
        if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
5657
0
            &input_debug))
5658
0
    return false;
5659
5660
0
        if (! (bfd_ecoff_debug_accumulate
5661
0
         (mdebug_handle, abfd, &debug, swap, input_bfd,
5662
0
          &input_debug, input_swap, info)))
5663
0
    return false;
5664
5665
        /* Loop through the external symbols.  For each one with
5666
     interesting information, try to find the symbol in
5667
     the linker global hash table and save the information
5668
     for the output external symbols.  */
5669
0
        eraw_src = (char *) input_debug.external_ext;
5670
0
        eraw_end = (eraw_src
5671
0
        + (input_debug.symbolic_header.iextMax
5672
0
           * input_swap->external_ext_size));
5673
0
        for (;
5674
0
       eraw_src < eraw_end;
5675
0
       eraw_src += input_swap->external_ext_size)
5676
0
    {
5677
0
      EXTR ext;
5678
0
      const char *name;
5679
0
      struct alpha_elf_link_hash_entry *h;
5680
5681
0
      (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
5682
0
      if (ext.asym.sc == scNil
5683
0
          || ext.asym.sc == scUndefined
5684
0
          || ext.asym.sc == scSUndefined)
5685
0
        continue;
5686
5687
0
      name = input_debug.ssext + ext.asym.iss;
5688
0
      h = alpha_elf_link_hash_lookup (htab, name, false, false, true);
5689
0
      if (h == NULL || h->esym.ifd != -2)
5690
0
        continue;
5691
5692
0
      if (ext.ifd != -1)
5693
0
        {
5694
0
          BFD_ASSERT (ext.ifd
5695
0
          < input_debug.symbolic_header.ifdMax);
5696
0
          ext.ifd = input_debug.ifdmap[ext.ifd];
5697
0
        }
5698
5699
0
      h->esym = ext;
5700
0
    }
5701
5702
        /* Free up the information we just read.  */
5703
0
        free (input_debug.line);
5704
0
        free (input_debug.external_dnr);
5705
0
        free (input_debug.external_pdr);
5706
0
        free (input_debug.external_sym);
5707
0
        free (input_debug.external_opt);
5708
0
        free (input_debug.external_aux);
5709
0
        free (input_debug.ss);
5710
0
        free (input_debug.ssext);
5711
0
        free (input_debug.external_fdr);
5712
0
        free (input_debug.external_rfd);
5713
0
        free (input_debug.external_ext);
5714
5715
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
5716
     elf_link_input_bfd ignores this section.  */
5717
0
        input_section->flags &=~ SEC_HAS_CONTENTS;
5718
0
      }
5719
5720
    /* Build the external symbol information.  */
5721
0
    einfo.abfd = abfd;
5722
0
    einfo.info = info;
5723
0
    einfo.debug = &debug;
5724
0
    einfo.swap = swap;
5725
0
    einfo.failed = false;
5726
0
    elf_link_hash_traverse (elf_hash_table (info),
5727
0
          elf64_alpha_output_extsym,
5728
0
          &einfo);
5729
0
    if (einfo.failed)
5730
0
      return false;
5731
5732
    /* Set the size of the .mdebug section.  */
5733
0
    o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
5734
5735
    /* Skip this section later on (I don't think this currently
5736
       matters, but someday it might).  */
5737
0
    o->map_head.link_order = (struct bfd_link_order *) NULL;
5738
5739
0
    mdebug_sec = o;
5740
0
  }
5741
0
    }
5742
5743
  /* Allocate the contents of the .got subsections.  This is left until
5744
     now because the sizes are not final until relaxation has finished
5745
     with them: a re-merge there can grow a subsection even though the
5746
     got as a whole only shrinks.  */
5747
0
  for (bfd *i = htab->got_list;
5748
0
       i != NULL;
5749
0
       i = alpha_elf_tdata (i)->got_link_next)
5750
0
    {
5751
0
      asection *sgot = alpha_elf_tdata (i)->got;
5752
5753
0
      if (sgot->size > 0)
5754
0
  {
5755
0
    sgot->contents = (bfd_byte *) bfd_zalloc (i, sgot->size);
5756
0
    if (sgot->contents == NULL)
5757
0
      return false;
5758
0
    sgot->alloced = 1;
5759
0
  }
5760
0
    }
5761
5762
  /* Invoke the regular ELF backend linker to do all the work.  */
5763
0
  if (! _bfd_elf_final_link (abfd, info))
5764
0
    return false;
5765
5766
  /* libc's startup code applies every entry between __rela_iplt_start and
5767
     __rela_iplt_end, so an entry left as R_ALPHA_NONE would fault.  Every
5768
     way for relocate_section to reserve a slot and then not fill it is
5769
     diagnosed there, so this is a consistency check on the sizing rather
5770
     than something a user can provoke.  */
5771
0
  irelplt = elf_hash_table (info)->irelplt;
5772
0
  if (irelplt != NULL
5773
0
      && irelplt->reloc_count * sizeof (Elf64_External_Rela) != irelplt->size)
5774
0
    {
5775
0
      _bfd_error_handler
5776
  /* xgettext:c-format */
5777
0
  (_("%pB: internal error: %lu .rela.iplt entries were reserved but "
5778
0
     "%lu were written"),
5779
0
   abfd,
5780
0
   (unsigned long) (irelplt->size / sizeof (Elf64_External_Rela)),
5781
0
   (unsigned long) irelplt->reloc_count);
5782
0
      bfd_set_error (bfd_error_bad_value);
5783
0
      return false;
5784
0
    }
5785
5786
  /* Now write out the computed sections.  */
5787
5788
  /* The .got subsections...  */
5789
0
  bfd *dynobj = elf_hash_table(info)->dynobj;
5790
0
  for (bfd *i = htab->got_list;
5791
0
       i != NULL;
5792
0
       i = alpha_elf_tdata(i)->got_link_next)
5793
0
    {
5794
0
      asection *sgot;
5795
5796
      /* elf_bfd_final_link already did everything in dynobj.  */
5797
0
      if (i == dynobj)
5798
0
  continue;
5799
5800
0
      sgot = alpha_elf_tdata(i)->got;
5801
0
      if (sgot != NULL
5802
0
    && !bfd_is_abs_section (sgot->output_section)
5803
0
    && !bfd_set_section_contents (abfd, sgot->output_section,
5804
0
          sgot->contents,
5805
0
          (file_ptr) sgot->output_offset,
5806
0
          sgot->size))
5807
0
  return false;
5808
0
    }
5809
5810
0
  if (mdebug_sec != NULL)
5811
0
    {
5812
0
      BFD_ASSERT (abfd->output_has_begun);
5813
0
      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5814
0
                 swap, info,
5815
0
                 mdebug_sec->filepos))
5816
0
  return false;
5817
5818
0
      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5819
0
    }
5820
5821
0
  return true;
5822
0
}
5823
5824
static enum elf_reloc_type_class
5825
elf64_alpha_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5826
            const asection *rel_sec ATTRIBUTE_UNUSED,
5827
            const Elf_Internal_Rela *rela)
5828
0
{
5829
0
  switch ((int) ELF64_R_TYPE (rela->r_info))
5830
0
    {
5831
0
    case R_ALPHA_RELATIVE:
5832
0
      return reloc_class_relative;
5833
0
    case R_ALPHA_JMP_SLOT:
5834
0
      return reloc_class_plt;
5835
0
    case R_ALPHA_COPY:
5836
0
      return reloc_class_copy;
5837
0
    case R_ALPHA_IRELATIVE:
5838
0
      return reloc_class_ifunc;
5839
0
    default:
5840
0
      return reloc_class_normal;
5841
0
    }
5842
0
}
5843

5844
static const struct bfd_elf_special_section elf64_alpha_special_sections[] =
5845
{
5846
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
5847
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
5848
  { NULL,         0,  0, 0,      0 }
5849
};
5850
5851
/* ECOFF swapping routines.  These are used when dealing with the
5852
   .mdebug section, which is in the ECOFF debugging format.  Copied
5853
   from elf32-mips.c.  */
5854
static const struct ecoff_debug_swap
5855
elf64_alpha_ecoff_debug_swap =
5856
{
5857
  /* Symbol table magic number.  */
5858
  magicSym2,
5859
  /* Alignment of debugging information.  E.g., 4.  */
5860
  8,
5861
  /* Sizes of external symbolic information.  */
5862
  sizeof (struct hdr_ext),
5863
  sizeof (struct dnr_ext),
5864
  sizeof (struct pdr_ext),
5865
  sizeof (struct sym_ext),
5866
  sizeof (struct opt_ext),
5867
  sizeof (struct fdr_ext),
5868
  sizeof (struct rfd_ext),
5869
  sizeof (struct ext_ext),
5870
  /* Functions to swap in external symbolic data.  */
5871
  ecoff_swap_hdr_in,
5872
  ecoff_swap_dnr_in,
5873
  ecoff_swap_pdr_in,
5874
  ecoff_swap_sym_in,
5875
  ecoff_swap_opt_in,
5876
  ecoff_swap_fdr_in,
5877
  ecoff_swap_rfd_in,
5878
  ecoff_swap_ext_in,
5879
  _bfd_ecoff_swap_tir_in,
5880
  _bfd_ecoff_swap_rndx_in,
5881
  /* Functions to swap out external symbolic data.  */
5882
  ecoff_swap_hdr_out,
5883
  ecoff_swap_dnr_out,
5884
  ecoff_swap_pdr_out,
5885
  ecoff_swap_sym_out,
5886
  ecoff_swap_opt_out,
5887
  ecoff_swap_fdr_out,
5888
  ecoff_swap_rfd_out,
5889
  ecoff_swap_ext_out,
5890
  _bfd_ecoff_swap_tir_out,
5891
  _bfd_ecoff_swap_rndx_out,
5892
  /* Function to read in symbolic data.  */
5893
  elf64_alpha_read_ecoff_info
5894
};
5895

5896
/* Use a non-standard hash bucket size of 8.  */
5897
5898
static const struct elf_size_info alpha_elf_size_info =
5899
{
5900
  sizeof (Elf64_External_Ehdr),
5901
  sizeof (Elf64_External_Phdr),
5902
  sizeof (Elf64_External_Shdr),
5903
  sizeof (Elf64_External_Rel),
5904
  sizeof (Elf64_External_Rela),
5905
  sizeof (Elf64_External_Sym),
5906
  sizeof (Elf64_External_Dyn),
5907
  sizeof (Elf_External_Note),
5908
  8,
5909
  1,
5910
  64, 3,
5911
  ELFCLASS64, EV_CURRENT,
5912
  bfd_elf64_write_out_phdrs,
5913
  bfd_elf64_write_shdrs_and_ehdr,
5914
  bfd_elf64_checksum_contents,
5915
  bfd_elf64_write_relocs,
5916
  bfd_elf64_swap_symbol_in,
5917
  bfd_elf64_swap_symbol_out,
5918
  bfd_elf64_slurp_reloc_table,
5919
  bfd_elf64_slurp_symbol_table,
5920
  bfd_elf64_swap_dyn_in,
5921
  bfd_elf64_swap_dyn_out,
5922
  bfd_elf64_swap_reloc_in,
5923
  bfd_elf64_swap_reloc_out,
5924
  bfd_elf64_swap_reloca_in,
5925
  bfd_elf64_swap_reloca_out
5926
};
5927
5928
#define TARGET_LITTLE_SYM alpha_elf64_vec
5929
#define TARGET_LITTLE_NAME  "elf64-alpha"
5930
#define ELF_ARCH    bfd_arch_alpha
5931
#define ELF_TARGET_ID   ALPHA_ELF_DATA
5932
#define ELF_MACHINE_CODE  EM_ALPHA
5933
#define ELF_MAXPAGESIZE 0x10000
5934
#define ELF_COMMONPAGESIZE  0x2000
5935
5936
#define bfd_elf64_bfd_link_hash_table_create \
5937
  elf64_alpha_bfd_link_hash_table_create
5938
5939
#define bfd_elf64_bfd_reloc_type_lookup \
5940
  elf64_alpha_bfd_reloc_type_lookup
5941
#define bfd_elf64_bfd_reloc_name_lookup \
5942
  elf64_alpha_bfd_reloc_name_lookup
5943
#define elf_info_to_howto \
5944
  elf64_alpha_info_to_howto
5945
5946
#define bfd_elf64_mkobject \
5947
  elf64_alpha_mkobject
5948
#define elf_backend_object_p \
5949
  elf64_alpha_object_p
5950
5951
#define elf_backend_section_from_shdr \
5952
  elf64_alpha_section_from_shdr
5953
#define elf_backend_section_flags \
5954
  elf64_alpha_section_flags
5955
#define elf_backend_fake_sections \
5956
  elf64_alpha_fake_sections
5957
5958
#define bfd_elf64_bfd_is_local_label_name \
5959
  elf64_alpha_is_local_label_name
5960
#define bfd_elf64_find_nearest_line \
5961
  elf64_alpha_find_nearest_line
5962
#define bfd_elf64_bfd_relax_section \
5963
  elf64_alpha_relax_section
5964
5965
#define elf_backend_add_symbol_hook \
5966
  elf64_alpha_add_symbol_hook
5967
#define elf_backend_relocs_compatible \
5968
  _bfd_elf_relocs_compatible
5969
#define elf_backend_sort_relocs_p \
5970
  elf64_alpha_sort_relocs_p
5971
#define elf_backend_check_relocs \
5972
  elf64_alpha_check_relocs
5973
#define elf_backend_create_dynamic_sections \
5974
  elf64_alpha_create_dynamic_sections
5975
#define elf_backend_adjust_dynamic_symbol \
5976
  elf64_alpha_adjust_dynamic_symbol
5977
#define elf_backend_merge_symbol_attribute \
5978
  elf64_alpha_merge_symbol_attribute
5979
#define elf_backend_copy_indirect_symbol \
5980
  elf64_alpha_copy_indirect_symbol
5981
#define elf_backend_early_size_sections \
5982
  elf64_alpha_early_size_sections
5983
#define elf_backend_late_size_sections \
5984
  elf64_alpha_late_size_sections
5985
#define elf_backend_omit_section_dynsym \
5986
  _bfd_elf_omit_section_dynsym_all
5987
#define elf_backend_relocate_section \
5988
  elf64_alpha_relocate_section
5989
#define elf_backend_finish_dynamic_symbol \
5990
  elf64_alpha_finish_dynamic_symbol
5991
#define elf_backend_finish_dynamic_sections \
5992
  elf64_alpha_finish_dynamic_sections
5993
#define bfd_elf64_bfd_final_link \
5994
  elf64_alpha_final_link
5995
#define elf_backend_reloc_type_class \
5996
  elf64_alpha_reloc_type_class
5997
5998
#define elf_backend_can_gc_sections 1
5999
#define elf_backend_gc_mark_hook  elf64_alpha_gc_mark_hook
6000
6001
#define elf_backend_ecoff_debug_swap \
6002
  &elf64_alpha_ecoff_debug_swap
6003
6004
#define elf_backend_size_info \
6005
  alpha_elf_size_info
6006
6007
#define elf_backend_special_sections \
6008
  elf64_alpha_special_sections
6009
6010
#define elf_backend_strip_zero_sized_dynamic_sections \
6011
  _bfd_elf_strip_zero_sized_dynamic_sections
6012
6013
/* A few constants that determine how the .plt section is set up.  */
6014
#define elf_backend_want_got_plt 0
6015
#define elf_backend_plt_readonly 0
6016
#define elf_backend_want_plt_sym 1
6017
#define elf_backend_got_header_size 0
6018
#define elf_backend_dtrel_excludes_plt 1
6019
6020
#include "elf64-target.h"
6021

6022
/* FreeBSD support.  */
6023
6024
#undef TARGET_LITTLE_SYM
6025
#define TARGET_LITTLE_SYM alpha_elf64_fbsd_vec
6026
#undef TARGET_LITTLE_NAME
6027
#define TARGET_LITTLE_NAME  "elf64-alpha-freebsd"
6028
#undef  ELF_OSABI
6029
#define ELF_OSABI   ELFOSABI_FREEBSD
6030
#undef  ELF_OSABI_EXACT
6031
#define ELF_OSABI_EXACT   1
6032
6033
/* The kernel recognizes executables as valid only if they carry a
6034
   "FreeBSD" label in the ELF header.  So we put this label on all
6035
   executables and (for simplicity) also all other object files.  */
6036
6037
static bool
6038
elf64_alpha_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
6039
0
{
6040
0
  Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
6041
6042
0
  if (!_bfd_elf_init_file_header (abfd, info))
6043
0
    return false;
6044
6045
0
  i_ehdrp = elf_elfheader (abfd);
6046
6047
  /* Put an ABI label supported by FreeBSD >= 4.1.  */
6048
0
  i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
6049
#ifdef OLD_FREEBSD_ABI_LABEL
6050
  /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
6051
  memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
6052
#endif
6053
  return true;
6054
0
}
6055
6056
#undef elf_backend_init_file_header
6057
#define elf_backend_init_file_header \
6058
  elf64_alpha_fbsd_init_file_header
6059
6060
#undef  elf64_bed
6061
#define elf64_bed elf64_alpha_fbsd_bed
6062
6063
#include "elf64-target.h"