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