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