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