Coverage Report

Created: 2026-09-14 08:07

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