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