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