No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / bfd / elf64-alpha.c
blob76d0661a11657055261af2a583636279f8eede74
1 /* Alpha specific support for 64-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc.
4 Contributed by Richard Henderson <rth@tamu.edu>.
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 /* We need a published ABI spec for this. Until one comes out, don't
23 assume this'll remain unchanged forever. */
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
30 #include "elf/alpha.h"
32 #define ALPHAECOFF
34 #define NO_COFF_RELOCS
35 #define NO_COFF_SYMBOLS
36 #define NO_COFF_LINENOS
38 /* Get the ECOFF swapping routines. Needed for the debug information. */
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"
51 /* Instruction data for plt generation and relaxation. */
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
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)
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
70 #define INSN_JSR 0x68004000
71 #define INSN_JMP 0x68000000
72 #define INSN_JSR_MASK 0xfc00c000
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))
80 /* PLT/GOT Stuff */
82 /* Set by ld emulation. Putting this into the link_info or hash structure
83 is simply working too hard. */
84 #ifdef USE_SECUREPLT
85 bfd_boolean elf64_alpha_use_secureplt = TRUE;
86 #else
87 bfd_boolean elf64_alpha_use_secureplt = FALSE;
88 #endif
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
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)
100 #define MAX_GOT_SIZE (64*1024)
102 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
104 struct alpha_elf_link_hash_entry
106 struct elf_link_hash_entry root;
108 /* External symbol information. */
109 EXTR esym;
111 /* Cumulative flags for all the .got entries. */
112 int flags;
114 /* Contexts in which a literal was referenced. */
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
125 /* Used to implement multiple .got subsections. */
126 struct alpha_elf_got_entry
128 struct alpha_elf_got_entry *next;
130 /* Which .got subsection? */
131 bfd *gotobj;
133 /* The addend in effect for this entry. */
134 bfd_vma addend;
136 /* The .got offset for this entry. */
137 int got_offset;
139 /* The .plt offset for this entry. */
140 int plt_offset;
142 /* How many references to this entry? */
143 int use_count;
145 /* The relocation type of this entry. */
146 unsigned char reloc_type;
148 /* How a LITERAL is used. */
149 unsigned char flags;
151 /* Have we initialized the dynamic relocation for this entry? */
152 unsigned char reloc_done;
154 /* Have we adjusted this entry for SEC_MERGE? */
155 unsigned char reloc_xlated;
156 } *got_entries;
158 /* Used to count non-got, non-plt relocations for delayed sizing
159 of relocation sections. */
160 struct alpha_elf_reloc_entry
162 struct alpha_elf_reloc_entry *next;
164 /* Which .reloc section? */
165 asection *srel;
167 /* What kind of relocation? */
168 unsigned int rtype;
170 /* Is this against read-only section? */
171 unsigned int reltext : 1;
173 /* How many did we find? */
174 unsigned long count;
175 } *reloc_entries;
178 /* Alpha ELF linker hash table. */
180 struct alpha_elf_link_hash_table
182 struct elf_link_hash_table root;
184 /* The head of a list of .got subsections linked through
185 alpha_elf_tdata(abfd)->got_link_next. */
186 bfd *got_list;
189 /* Look up an entry in a Alpha ELF linker hash table. */
191 #define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
192 ((struct alpha_elf_link_hash_entry *) \
193 elf_link_hash_lookup (&(table)->root, (string), (create), \
194 (copy), (follow)))
196 /* Traverse a Alpha ELF linker hash table. */
198 #define alpha_elf_link_hash_traverse(table, func, info) \
199 (elf_link_hash_traverse \
200 (&(table)->root, \
201 (bfd_boolean (*) (struct elf_link_hash_entry *, PTR)) (func), \
202 (info)))
204 /* Get the Alpha ELF linker hash table from a link_info structure. */
206 #define alpha_elf_hash_table(p) \
207 ((struct alpha_elf_link_hash_table *) ((p)->hash))
209 /* Get the object's symbols as our own entry type. */
211 #define alpha_elf_sym_hashes(abfd) \
212 ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
214 /* Should we do dynamic things to this symbol? This differs from the
215 generic version in that we never need to consider function pointer
216 equality wrt PLT entries -- we don't create a PLT entry if a symbol's
217 address is ever taken. */
219 static inline bfd_boolean
220 alpha_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
221 struct bfd_link_info *info)
223 return _bfd_elf_dynamic_symbol_p (h, info, 0);
226 /* Create an entry in a Alpha ELF linker hash table. */
228 static struct bfd_hash_entry *
229 elf64_alpha_link_hash_newfunc (struct bfd_hash_entry *entry,
230 struct bfd_hash_table *table,
231 const char *string)
233 struct alpha_elf_link_hash_entry *ret =
234 (struct alpha_elf_link_hash_entry *) entry;
236 /* Allocate the structure if it has not already been allocated by a
237 subclass. */
238 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
239 ret = ((struct alpha_elf_link_hash_entry *)
240 bfd_hash_allocate (table,
241 sizeof (struct alpha_elf_link_hash_entry)));
242 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
243 return (struct bfd_hash_entry *) ret;
245 /* Call the allocation method of the superclass. */
246 ret = ((struct alpha_elf_link_hash_entry *)
247 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
248 table, string));
249 if (ret != (struct alpha_elf_link_hash_entry *) NULL)
251 /* Set local fields. */
252 memset (&ret->esym, 0, sizeof (EXTR));
253 /* We use -2 as a marker to indicate that the information has
254 not been set. -1 means there is no associated ifd. */
255 ret->esym.ifd = -2;
256 ret->flags = 0;
257 ret->got_entries = NULL;
258 ret->reloc_entries = NULL;
261 return (struct bfd_hash_entry *) ret;
264 /* Create a Alpha ELF linker hash table. */
266 static struct bfd_link_hash_table *
267 elf64_alpha_bfd_link_hash_table_create (bfd *abfd)
269 struct alpha_elf_link_hash_table *ret;
270 bfd_size_type amt = sizeof (struct alpha_elf_link_hash_table);
272 ret = (struct alpha_elf_link_hash_table *) bfd_zmalloc (amt);
273 if (ret == (struct alpha_elf_link_hash_table *) NULL)
274 return NULL;
276 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
277 elf64_alpha_link_hash_newfunc,
278 sizeof (struct alpha_elf_link_hash_entry)))
280 free (ret);
281 return NULL;
284 return &ret->root.root;
287 /* We have some private fields hanging off of the elf_tdata structure. */
289 struct alpha_elf_obj_tdata
291 struct elf_obj_tdata root;
293 /* For every input file, these are the got entries for that object's
294 local symbols. */
295 struct alpha_elf_got_entry ** local_got_entries;
297 /* For every input file, this is the object that owns the got that
298 this input file uses. */
299 bfd *gotobj;
301 /* For every got, this is a linked list through the objects using this got */
302 bfd *in_got_link_next;
304 /* For every got, this is a link to the next got subsegment. */
305 bfd *got_link_next;
307 /* For every got, this is the section. */
308 asection *got;
310 /* For every got, this is it's total number of words. */
311 int total_got_size;
313 /* For every got, this is the sum of the number of words required
314 to hold all of the member object's local got. */
315 int local_got_size;
318 #define alpha_elf_tdata(abfd) \
319 ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
321 static bfd_boolean
322 elf64_alpha_mkobject (bfd *abfd)
324 bfd_size_type amt = sizeof (struct alpha_elf_obj_tdata);
325 abfd->tdata.any = bfd_zalloc (abfd, amt);
326 if (abfd->tdata.any == NULL)
327 return FALSE;
328 return TRUE;
331 static bfd_boolean
332 elf64_alpha_object_p (bfd *abfd)
334 /* Set the right machine number for an Alpha ELF file. */
335 return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
338 /* A relocation function which doesn't do anything. */
340 static bfd_reloc_status_type
341 elf64_alpha_reloc_nil (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
342 asymbol *sym ATTRIBUTE_UNUSED,
343 PTR data ATTRIBUTE_UNUSED, asection *sec,
344 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
346 if (output_bfd)
347 reloc->address += sec->output_offset;
348 return bfd_reloc_ok;
351 /* A relocation function used for an unsupported reloc. */
353 static bfd_reloc_status_type
354 elf64_alpha_reloc_bad (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
355 asymbol *sym ATTRIBUTE_UNUSED,
356 PTR data ATTRIBUTE_UNUSED, asection *sec,
357 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
359 if (output_bfd)
360 reloc->address += sec->output_offset;
361 return bfd_reloc_notsupported;
364 /* Do the work of the GPDISP relocation. */
366 static bfd_reloc_status_type
367 elf64_alpha_do_reloc_gpdisp (bfd *abfd, bfd_vma gpdisp, bfd_byte *p_ldah,
368 bfd_byte *p_lda)
370 bfd_reloc_status_type ret = bfd_reloc_ok;
371 bfd_vma addend;
372 unsigned long i_ldah, i_lda;
374 i_ldah = bfd_get_32 (abfd, p_ldah);
375 i_lda = bfd_get_32 (abfd, p_lda);
377 /* Complain if the instructions are not correct. */
378 if (((i_ldah >> 26) & 0x3f) != 0x09
379 || ((i_lda >> 26) & 0x3f) != 0x08)
380 ret = bfd_reloc_dangerous;
382 /* Extract the user-supplied offset, mirroring the sign extensions
383 that the instructions perform. */
384 addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
385 addend = (addend ^ 0x80008000) - 0x80008000;
387 gpdisp += addend;
389 if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
390 || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
391 ret = bfd_reloc_overflow;
393 /* compensate for the sign extension again. */
394 i_ldah = ((i_ldah & 0xffff0000)
395 | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
396 i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
398 bfd_put_32 (abfd, (bfd_vma) i_ldah, p_ldah);
399 bfd_put_32 (abfd, (bfd_vma) i_lda, p_lda);
401 return ret;
404 /* The special function for the GPDISP reloc. */
406 static bfd_reloc_status_type
407 elf64_alpha_reloc_gpdisp (bfd *abfd, arelent *reloc_entry,
408 asymbol *sym ATTRIBUTE_UNUSED, PTR data,
409 asection *input_section, bfd *output_bfd,
410 char **err_msg)
412 bfd_reloc_status_type ret;
413 bfd_vma gp, relocation;
414 bfd_vma high_address;
415 bfd_byte *p_ldah, *p_lda;
417 /* Don't do anything if we're not doing a final link. */
418 if (output_bfd)
420 reloc_entry->address += input_section->output_offset;
421 return bfd_reloc_ok;
424 high_address = bfd_get_section_limit (abfd, input_section);
425 if (reloc_entry->address > high_address
426 || reloc_entry->address + reloc_entry->addend > high_address)
427 return bfd_reloc_outofrange;
429 /* The gp used in the portion of the output object to which this
430 input object belongs is cached on the input bfd. */
431 gp = _bfd_get_gp_value (abfd);
433 relocation = (input_section->output_section->vma
434 + input_section->output_offset
435 + reloc_entry->address);
437 p_ldah = (bfd_byte *) data + reloc_entry->address;
438 p_lda = p_ldah + reloc_entry->addend;
440 ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
442 /* Complain if the instructions are not correct. */
443 if (ret == bfd_reloc_dangerous)
444 *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
446 return ret;
449 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
450 from smaller values. Start with zero, widen, *then* decrement. */
451 #define MINUS_ONE (((bfd_vma)0) - 1)
453 #define SKIP_HOWTO(N) \
454 HOWTO(N, 0, 0, 0, 0, 0, 0, elf64_alpha_reloc_bad, 0, 0, 0, 0, 0)
456 static reloc_howto_type elf64_alpha_howto_table[] =
458 HOWTO (R_ALPHA_NONE, /* type */
459 0, /* rightshift */
460 0, /* size (0 = byte, 1 = short, 2 = long) */
461 8, /* bitsize */
462 TRUE, /* pc_relative */
463 0, /* bitpos */
464 complain_overflow_dont, /* complain_on_overflow */
465 elf64_alpha_reloc_nil, /* special_function */
466 "NONE", /* name */
467 FALSE, /* partial_inplace */
468 0, /* src_mask */
469 0, /* dst_mask */
470 TRUE), /* pcrel_offset */
472 /* A 32 bit reference to a symbol. */
473 HOWTO (R_ALPHA_REFLONG, /* type */
474 0, /* rightshift */
475 2, /* size (0 = byte, 1 = short, 2 = long) */
476 32, /* bitsize */
477 FALSE, /* pc_relative */
478 0, /* bitpos */
479 complain_overflow_bitfield, /* complain_on_overflow */
480 0, /* special_function */
481 "REFLONG", /* name */
482 FALSE, /* partial_inplace */
483 0xffffffff, /* src_mask */
484 0xffffffff, /* dst_mask */
485 FALSE), /* pcrel_offset */
487 /* A 64 bit reference to a symbol. */
488 HOWTO (R_ALPHA_REFQUAD, /* type */
489 0, /* rightshift */
490 4, /* size (0 = byte, 1 = short, 2 = long) */
491 64, /* bitsize */
492 FALSE, /* pc_relative */
493 0, /* bitpos */
494 complain_overflow_bitfield, /* complain_on_overflow */
495 0, /* special_function */
496 "REFQUAD", /* name */
497 FALSE, /* partial_inplace */
498 MINUS_ONE, /* src_mask */
499 MINUS_ONE, /* dst_mask */
500 FALSE), /* pcrel_offset */
502 /* A 32 bit GP relative offset. This is just like REFLONG except
503 that when the value is used the value of the gp register will be
504 added in. */
505 HOWTO (R_ALPHA_GPREL32, /* type */
506 0, /* rightshift */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
508 32, /* bitsize */
509 FALSE, /* pc_relative */
510 0, /* bitpos */
511 complain_overflow_bitfield, /* complain_on_overflow */
512 0, /* special_function */
513 "GPREL32", /* name */
514 FALSE, /* partial_inplace */
515 0xffffffff, /* src_mask */
516 0xffffffff, /* dst_mask */
517 FALSE), /* pcrel_offset */
519 /* Used for an instruction that refers to memory off the GP register. */
520 HOWTO (R_ALPHA_LITERAL, /* type */
521 0, /* rightshift */
522 1, /* size (0 = byte, 1 = short, 2 = long) */
523 16, /* bitsize */
524 FALSE, /* pc_relative */
525 0, /* bitpos */
526 complain_overflow_signed, /* complain_on_overflow */
527 0, /* special_function */
528 "ELF_LITERAL", /* name */
529 FALSE, /* partial_inplace */
530 0xffff, /* src_mask */
531 0xffff, /* dst_mask */
532 FALSE), /* pcrel_offset */
534 /* This reloc only appears immediately following an ELF_LITERAL reloc.
535 It identifies a use of the literal. The symbol index is special:
536 1 means the literal address is in the base register of a memory
537 format instruction; 2 means the literal address is in the byte
538 offset register of a byte-manipulation instruction; 3 means the
539 literal address is in the target register of a jsr instruction.
540 This does not actually do any relocation. */
541 HOWTO (R_ALPHA_LITUSE, /* type */
542 0, /* rightshift */
543 1, /* size (0 = byte, 1 = short, 2 = long) */
544 32, /* bitsize */
545 FALSE, /* pc_relative */
546 0, /* bitpos */
547 complain_overflow_dont, /* complain_on_overflow */
548 elf64_alpha_reloc_nil, /* special_function */
549 "LITUSE", /* name */
550 FALSE, /* partial_inplace */
551 0, /* src_mask */
552 0, /* dst_mask */
553 FALSE), /* pcrel_offset */
555 /* Load the gp register. This is always used for a ldah instruction
556 which loads the upper 16 bits of the gp register. The symbol
557 index of the GPDISP instruction is an offset in bytes to the lda
558 instruction that loads the lower 16 bits. The value to use for
559 the relocation is the difference between the GP value and the
560 current location; the load will always be done against a register
561 holding the current address.
563 NOTE: Unlike ECOFF, partial in-place relocation is not done. If
564 any offset is present in the instructions, it is an offset from
565 the register to the ldah instruction. This lets us avoid any
566 stupid hackery like inventing a gp value to do partial relocation
567 against. Also unlike ECOFF, we do the whole relocation off of
568 the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
569 space consuming bit, that, since all the information was present
570 in the GPDISP_HI16 reloc. */
571 HOWTO (R_ALPHA_GPDISP, /* type */
572 16, /* rightshift */
573 2, /* size (0 = byte, 1 = short, 2 = long) */
574 16, /* bitsize */
575 FALSE, /* pc_relative */
576 0, /* bitpos */
577 complain_overflow_dont, /* complain_on_overflow */
578 elf64_alpha_reloc_gpdisp, /* special_function */
579 "GPDISP", /* name */
580 FALSE, /* partial_inplace */
581 0xffff, /* src_mask */
582 0xffff, /* dst_mask */
583 TRUE), /* pcrel_offset */
585 /* A 21 bit branch. */
586 HOWTO (R_ALPHA_BRADDR, /* type */
587 2, /* rightshift */
588 2, /* size (0 = byte, 1 = short, 2 = long) */
589 21, /* bitsize */
590 TRUE, /* pc_relative */
591 0, /* bitpos */
592 complain_overflow_signed, /* complain_on_overflow */
593 0, /* special_function */
594 "BRADDR", /* name */
595 FALSE, /* partial_inplace */
596 0x1fffff, /* src_mask */
597 0x1fffff, /* dst_mask */
598 TRUE), /* pcrel_offset */
600 /* A hint for a jump to a register. */
601 HOWTO (R_ALPHA_HINT, /* type */
602 2, /* rightshift */
603 1, /* size (0 = byte, 1 = short, 2 = long) */
604 14, /* bitsize */
605 TRUE, /* pc_relative */
606 0, /* bitpos */
607 complain_overflow_dont, /* complain_on_overflow */
608 0, /* special_function */
609 "HINT", /* name */
610 FALSE, /* partial_inplace */
611 0x3fff, /* src_mask */
612 0x3fff, /* dst_mask */
613 TRUE), /* pcrel_offset */
615 /* 16 bit PC relative offset. */
616 HOWTO (R_ALPHA_SREL16, /* type */
617 0, /* rightshift */
618 1, /* size (0 = byte, 1 = short, 2 = long) */
619 16, /* bitsize */
620 TRUE, /* pc_relative */
621 0, /* bitpos */
622 complain_overflow_signed, /* complain_on_overflow */
623 0, /* special_function */
624 "SREL16", /* name */
625 FALSE, /* partial_inplace */
626 0xffff, /* src_mask */
627 0xffff, /* dst_mask */
628 TRUE), /* pcrel_offset */
630 /* 32 bit PC relative offset. */
631 HOWTO (R_ALPHA_SREL32, /* type */
632 0, /* rightshift */
633 2, /* size (0 = byte, 1 = short, 2 = long) */
634 32, /* bitsize */
635 TRUE, /* pc_relative */
636 0, /* bitpos */
637 complain_overflow_signed, /* complain_on_overflow */
638 0, /* special_function */
639 "SREL32", /* name */
640 FALSE, /* partial_inplace */
641 0xffffffff, /* src_mask */
642 0xffffffff, /* dst_mask */
643 TRUE), /* pcrel_offset */
645 /* A 64 bit PC relative offset. */
646 HOWTO (R_ALPHA_SREL64, /* type */
647 0, /* rightshift */
648 4, /* size (0 = byte, 1 = short, 2 = long) */
649 64, /* bitsize */
650 TRUE, /* pc_relative */
651 0, /* bitpos */
652 complain_overflow_signed, /* complain_on_overflow */
653 0, /* special_function */
654 "SREL64", /* name */
655 FALSE, /* partial_inplace */
656 MINUS_ONE, /* src_mask */
657 MINUS_ONE, /* dst_mask */
658 TRUE), /* pcrel_offset */
660 /* Skip 12 - 16; deprecated ECOFF relocs. */
661 SKIP_HOWTO (12),
662 SKIP_HOWTO (13),
663 SKIP_HOWTO (14),
664 SKIP_HOWTO (15),
665 SKIP_HOWTO (16),
667 /* The high 16 bits of the displacement from GP to the target. */
668 HOWTO (R_ALPHA_GPRELHIGH,
669 0, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
672 FALSE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_signed, /* complain_on_overflow */
675 0, /* special_function */
676 "GPRELHIGH", /* name */
677 FALSE, /* partial_inplace */
678 0xffff, /* src_mask */
679 0xffff, /* dst_mask */
680 FALSE), /* pcrel_offset */
682 /* The low 16 bits of the displacement from GP to the target. */
683 HOWTO (R_ALPHA_GPRELLOW,
684 0, /* rightshift */
685 1, /* size (0 = byte, 1 = short, 2 = long) */
686 16, /* bitsize */
687 FALSE, /* pc_relative */
688 0, /* bitpos */
689 complain_overflow_dont, /* complain_on_overflow */
690 0, /* special_function */
691 "GPRELLOW", /* name */
692 FALSE, /* partial_inplace */
693 0xffff, /* src_mask */
694 0xffff, /* dst_mask */
695 FALSE), /* pcrel_offset */
697 /* A 16-bit displacement from the GP to the target. */
698 HOWTO (R_ALPHA_GPREL16,
699 0, /* rightshift */
700 1, /* size (0 = byte, 1 = short, 2 = long) */
701 16, /* bitsize */
702 FALSE, /* pc_relative */
703 0, /* bitpos */
704 complain_overflow_signed, /* complain_on_overflow */
705 0, /* special_function */
706 "GPREL16", /* name */
707 FALSE, /* partial_inplace */
708 0xffff, /* src_mask */
709 0xffff, /* dst_mask */
710 FALSE), /* pcrel_offset */
712 /* Skip 20 - 23; deprecated ECOFF relocs. */
713 SKIP_HOWTO (20),
714 SKIP_HOWTO (21),
715 SKIP_HOWTO (22),
716 SKIP_HOWTO (23),
718 /* Misc ELF relocations. */
720 /* A dynamic relocation to copy the target into our .dynbss section. */
721 /* Not generated, as all Alpha objects use PIC, so it is not needed. It
722 is present because every other ELF has one, but should not be used
723 because .dynbss is an ugly thing. */
724 HOWTO (R_ALPHA_COPY,
728 FALSE,
730 complain_overflow_dont,
731 bfd_elf_generic_reloc,
732 "COPY",
733 FALSE,
736 TRUE),
738 /* A dynamic relocation for a .got entry. */
739 HOWTO (R_ALPHA_GLOB_DAT,
743 FALSE,
745 complain_overflow_dont,
746 bfd_elf_generic_reloc,
747 "GLOB_DAT",
748 FALSE,
751 TRUE),
753 /* A dynamic relocation for a .plt entry. */
754 HOWTO (R_ALPHA_JMP_SLOT,
758 FALSE,
760 complain_overflow_dont,
761 bfd_elf_generic_reloc,
762 "JMP_SLOT",
763 FALSE,
766 TRUE),
768 /* A dynamic relocation to add the base of the DSO to a 64-bit field. */
769 HOWTO (R_ALPHA_RELATIVE,
773 FALSE,
775 complain_overflow_dont,
776 bfd_elf_generic_reloc,
777 "RELATIVE",
778 FALSE,
781 TRUE),
783 /* A 21 bit branch that adjusts for gp loads. */
784 HOWTO (R_ALPHA_BRSGP, /* type */
785 2, /* rightshift */
786 2, /* size (0 = byte, 1 = short, 2 = long) */
787 21, /* bitsize */
788 TRUE, /* pc_relative */
789 0, /* bitpos */
790 complain_overflow_signed, /* complain_on_overflow */
791 0, /* special_function */
792 "BRSGP", /* name */
793 FALSE, /* partial_inplace */
794 0x1fffff, /* src_mask */
795 0x1fffff, /* dst_mask */
796 TRUE), /* pcrel_offset */
798 /* Creates a tls_index for the symbol in the got. */
799 HOWTO (R_ALPHA_TLSGD, /* type */
800 0, /* rightshift */
801 1, /* size (0 = byte, 1 = short, 2 = long) */
802 16, /* bitsize */
803 FALSE, /* pc_relative */
804 0, /* bitpos */
805 complain_overflow_signed, /* complain_on_overflow */
806 0, /* special_function */
807 "TLSGD", /* name */
808 FALSE, /* partial_inplace */
809 0xffff, /* src_mask */
810 0xffff, /* dst_mask */
811 FALSE), /* pcrel_offset */
813 /* Creates a tls_index for the (current) module in the got. */
814 HOWTO (R_ALPHA_TLSLDM, /* type */
815 0, /* rightshift */
816 1, /* size (0 = byte, 1 = short, 2 = long) */
817 16, /* bitsize */
818 FALSE, /* pc_relative */
819 0, /* bitpos */
820 complain_overflow_signed, /* complain_on_overflow */
821 0, /* special_function */
822 "TLSLDM", /* name */
823 FALSE, /* partial_inplace */
824 0xffff, /* src_mask */
825 0xffff, /* dst_mask */
826 FALSE), /* pcrel_offset */
828 /* A dynamic relocation for a DTP module entry. */
829 HOWTO (R_ALPHA_DTPMOD64, /* type */
830 0, /* rightshift */
831 4, /* size (0 = byte, 1 = short, 2 = long) */
832 64, /* bitsize */
833 FALSE, /* pc_relative */
834 0, /* bitpos */
835 complain_overflow_bitfield, /* complain_on_overflow */
836 0, /* special_function */
837 "DTPMOD64", /* name */
838 FALSE, /* partial_inplace */
839 MINUS_ONE, /* src_mask */
840 MINUS_ONE, /* dst_mask */
841 FALSE), /* pcrel_offset */
843 /* Creates a 64-bit offset in the got for the displacement
844 from DTP to the target. */
845 HOWTO (R_ALPHA_GOTDTPREL, /* type */
846 0, /* rightshift */
847 1, /* size (0 = byte, 1 = short, 2 = long) */
848 16, /* bitsize */
849 FALSE, /* pc_relative */
850 0, /* bitpos */
851 complain_overflow_signed, /* complain_on_overflow */
852 0, /* special_function */
853 "GOTDTPREL", /* name */
854 FALSE, /* partial_inplace */
855 0xffff, /* src_mask */
856 0xffff, /* dst_mask */
857 FALSE), /* pcrel_offset */
859 /* A dynamic relocation for a displacement from DTP to the target. */
860 HOWTO (R_ALPHA_DTPREL64, /* type */
861 0, /* rightshift */
862 4, /* size (0 = byte, 1 = short, 2 = long) */
863 64, /* bitsize */
864 FALSE, /* pc_relative */
865 0, /* bitpos */
866 complain_overflow_bitfield, /* complain_on_overflow */
867 0, /* special_function */
868 "DTPREL64", /* name */
869 FALSE, /* partial_inplace */
870 MINUS_ONE, /* src_mask */
871 MINUS_ONE, /* dst_mask */
872 FALSE), /* pcrel_offset */
874 /* The high 16 bits of the displacement from DTP to the target. */
875 HOWTO (R_ALPHA_DTPRELHI, /* type */
876 0, /* rightshift */
877 1, /* size (0 = byte, 1 = short, 2 = long) */
878 16, /* bitsize */
879 FALSE, /* pc_relative */
880 0, /* bitpos */
881 complain_overflow_signed, /* complain_on_overflow */
882 0, /* special_function */
883 "DTPRELHI", /* name */
884 FALSE, /* partial_inplace */
885 0xffff, /* src_mask */
886 0xffff, /* dst_mask */
887 FALSE), /* pcrel_offset */
889 /* The low 16 bits of the displacement from DTP to the target. */
890 HOWTO (R_ALPHA_DTPRELLO, /* type */
891 0, /* rightshift */
892 1, /* size (0 = byte, 1 = short, 2 = long) */
893 16, /* bitsize */
894 FALSE, /* pc_relative */
895 0, /* bitpos */
896 complain_overflow_dont, /* complain_on_overflow */
897 0, /* special_function */
898 "DTPRELLO", /* name */
899 FALSE, /* partial_inplace */
900 0xffff, /* src_mask */
901 0xffff, /* dst_mask */
902 FALSE), /* pcrel_offset */
904 /* A 16-bit displacement from DTP to the target. */
905 HOWTO (R_ALPHA_DTPREL16, /* type */
906 0, /* rightshift */
907 1, /* size (0 = byte, 1 = short, 2 = long) */
908 16, /* bitsize */
909 FALSE, /* pc_relative */
910 0, /* bitpos */
911 complain_overflow_signed, /* complain_on_overflow */
912 0, /* special_function */
913 "DTPREL16", /* name */
914 FALSE, /* partial_inplace */
915 0xffff, /* src_mask */
916 0xffff, /* dst_mask */
917 FALSE), /* pcrel_offset */
919 /* Creates a 64-bit offset in the got for the displacement
920 from TP to the target. */
921 HOWTO (R_ALPHA_GOTTPREL, /* type */
922 0, /* rightshift */
923 1, /* size (0 = byte, 1 = short, 2 = long) */
924 16, /* bitsize */
925 FALSE, /* pc_relative */
926 0, /* bitpos */
927 complain_overflow_signed, /* complain_on_overflow */
928 0, /* special_function */
929 "GOTTPREL", /* name */
930 FALSE, /* partial_inplace */
931 0xffff, /* src_mask */
932 0xffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
935 /* A dynamic relocation for a displacement from TP to the target. */
936 HOWTO (R_ALPHA_TPREL64, /* type */
937 0, /* rightshift */
938 4, /* size (0 = byte, 1 = short, 2 = long) */
939 64, /* bitsize */
940 FALSE, /* pc_relative */
941 0, /* bitpos */
942 complain_overflow_bitfield, /* complain_on_overflow */
943 0, /* special_function */
944 "TPREL64", /* name */
945 FALSE, /* partial_inplace */
946 MINUS_ONE, /* src_mask */
947 MINUS_ONE, /* dst_mask */
948 FALSE), /* pcrel_offset */
950 /* The high 16 bits of the displacement from TP to the target. */
951 HOWTO (R_ALPHA_TPRELHI, /* type */
952 0, /* rightshift */
953 1, /* size (0 = byte, 1 = short, 2 = long) */
954 16, /* bitsize */
955 FALSE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_signed, /* complain_on_overflow */
958 0, /* special_function */
959 "TPRELHI", /* name */
960 FALSE, /* partial_inplace */
961 0xffff, /* src_mask */
962 0xffff, /* dst_mask */
963 FALSE), /* pcrel_offset */
965 /* The low 16 bits of the displacement from TP to the target. */
966 HOWTO (R_ALPHA_TPRELLO, /* type */
967 0, /* rightshift */
968 1, /* size (0 = byte, 1 = short, 2 = long) */
969 16, /* bitsize */
970 FALSE, /* pc_relative */
971 0, /* bitpos */
972 complain_overflow_dont, /* complain_on_overflow */
973 0, /* special_function */
974 "TPRELLO", /* name */
975 FALSE, /* partial_inplace */
976 0xffff, /* src_mask */
977 0xffff, /* dst_mask */
978 FALSE), /* pcrel_offset */
980 /* A 16-bit displacement from TP to the target. */
981 HOWTO (R_ALPHA_TPREL16, /* type */
982 0, /* rightshift */
983 1, /* size (0 = byte, 1 = short, 2 = long) */
984 16, /* bitsize */
985 FALSE, /* pc_relative */
986 0, /* bitpos */
987 complain_overflow_signed, /* complain_on_overflow */
988 0, /* special_function */
989 "TPREL16", /* name */
990 FALSE, /* partial_inplace */
991 0xffff, /* src_mask */
992 0xffff, /* dst_mask */
993 FALSE), /* pcrel_offset */
996 /* A mapping from BFD reloc types to Alpha ELF reloc types. */
998 struct elf_reloc_map
1000 bfd_reloc_code_real_type bfd_reloc_val;
1001 int elf_reloc_val;
1004 static const struct elf_reloc_map elf64_alpha_reloc_map[] =
1006 {BFD_RELOC_NONE, R_ALPHA_NONE},
1007 {BFD_RELOC_32, R_ALPHA_REFLONG},
1008 {BFD_RELOC_64, R_ALPHA_REFQUAD},
1009 {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
1010 {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
1011 {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
1012 {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
1013 {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
1014 {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
1015 {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
1016 {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
1017 {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
1018 {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
1019 {BFD_RELOC_ALPHA_GPREL_HI16, R_ALPHA_GPRELHIGH},
1020 {BFD_RELOC_ALPHA_GPREL_LO16, R_ALPHA_GPRELLOW},
1021 {BFD_RELOC_GPREL16, R_ALPHA_GPREL16},
1022 {BFD_RELOC_ALPHA_BRSGP, R_ALPHA_BRSGP},
1023 {BFD_RELOC_ALPHA_TLSGD, R_ALPHA_TLSGD},
1024 {BFD_RELOC_ALPHA_TLSLDM, R_ALPHA_TLSLDM},
1025 {BFD_RELOC_ALPHA_DTPMOD64, R_ALPHA_DTPMOD64},
1026 {BFD_RELOC_ALPHA_GOTDTPREL16, R_ALPHA_GOTDTPREL},
1027 {BFD_RELOC_ALPHA_DTPREL64, R_ALPHA_DTPREL64},
1028 {BFD_RELOC_ALPHA_DTPREL_HI16, R_ALPHA_DTPRELHI},
1029 {BFD_RELOC_ALPHA_DTPREL_LO16, R_ALPHA_DTPRELLO},
1030 {BFD_RELOC_ALPHA_DTPREL16, R_ALPHA_DTPREL16},
1031 {BFD_RELOC_ALPHA_GOTTPREL16, R_ALPHA_GOTTPREL},
1032 {BFD_RELOC_ALPHA_TPREL64, R_ALPHA_TPREL64},
1033 {BFD_RELOC_ALPHA_TPREL_HI16, R_ALPHA_TPRELHI},
1034 {BFD_RELOC_ALPHA_TPREL_LO16, R_ALPHA_TPRELLO},
1035 {BFD_RELOC_ALPHA_TPREL16, R_ALPHA_TPREL16},
1038 /* Given a BFD reloc type, return a HOWTO structure. */
1040 static reloc_howto_type *
1041 elf64_alpha_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1042 bfd_reloc_code_real_type code)
1044 const struct elf_reloc_map *i, *e;
1045 i = e = elf64_alpha_reloc_map;
1046 e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
1047 for (; i != e; ++i)
1049 if (i->bfd_reloc_val == code)
1050 return &elf64_alpha_howto_table[i->elf_reloc_val];
1052 return 0;
1055 /* Given an Alpha ELF reloc type, fill in an arelent structure. */
1057 static void
1058 elf64_alpha_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
1059 Elf_Internal_Rela *dst)
1061 unsigned r_type = ELF64_R_TYPE(dst->r_info);
1062 BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1063 cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1066 /* These two relocations create a two-word entry in the got. */
1067 #define alpha_got_entry_size(r_type) \
1068 (r_type == R_ALPHA_TLSGD || r_type == R_ALPHA_TLSLDM ? 16 : 8)
1070 /* This is PT_TLS segment p_vaddr. */
1071 #define alpha_get_dtprel_base(info) \
1072 (elf_hash_table (info)->tls_sec->vma)
1074 /* Main program TLS (whose template starts at PT_TLS p_vaddr)
1075 is assigned offset round(16, PT_TLS p_align). */
1076 #define alpha_get_tprel_base(info) \
1077 (elf_hash_table (info)->tls_sec->vma \
1078 - align_power ((bfd_vma) 16, \
1079 elf_hash_table (info)->tls_sec->alignment_power))
1081 /* Handle an Alpha specific section when reading an object file. This
1082 is called when bfd_section_from_shdr finds a section with an unknown
1083 type.
1084 FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
1085 how to. */
1087 static bfd_boolean
1088 elf64_alpha_section_from_shdr (bfd *abfd,
1089 Elf_Internal_Shdr *hdr,
1090 const char *name,
1091 int shindex)
1093 asection *newsect;
1095 /* There ought to be a place to keep ELF backend specific flags, but
1096 at the moment there isn't one. We just keep track of the
1097 sections by their name, instead. Fortunately, the ABI gives
1098 suggested names for all the MIPS specific sections, so we will
1099 probably get away with this. */
1100 switch (hdr->sh_type)
1102 case SHT_ALPHA_DEBUG:
1103 if (strcmp (name, ".mdebug") != 0)
1104 return FALSE;
1105 break;
1106 default:
1107 return FALSE;
1110 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1111 return FALSE;
1112 newsect = hdr->bfd_section;
1114 if (hdr->sh_type == SHT_ALPHA_DEBUG)
1116 if (! bfd_set_section_flags (abfd, newsect,
1117 (bfd_get_section_flags (abfd, newsect)
1118 | SEC_DEBUGGING)))
1119 return FALSE;
1122 return TRUE;
1125 /* Convert Alpha specific section flags to bfd internal section flags. */
1127 static bfd_boolean
1128 elf64_alpha_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr)
1130 if (hdr->sh_flags & SHF_ALPHA_GPREL)
1131 *flags |= SEC_SMALL_DATA;
1133 return TRUE;
1136 /* Set the correct type for an Alpha ELF section. We do this by the
1137 section name, which is a hack, but ought to work. */
1139 static bfd_boolean
1140 elf64_alpha_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
1142 register const char *name;
1144 name = bfd_get_section_name (abfd, sec);
1146 if (strcmp (name, ".mdebug") == 0)
1148 hdr->sh_type = SHT_ALPHA_DEBUG;
1149 /* In a shared object on Irix 5.3, the .mdebug section has an
1150 entsize of 0. FIXME: Does this matter? */
1151 if ((abfd->flags & DYNAMIC) != 0 )
1152 hdr->sh_entsize = 0;
1153 else
1154 hdr->sh_entsize = 1;
1156 else if ((sec->flags & SEC_SMALL_DATA)
1157 || strcmp (name, ".sdata") == 0
1158 || strcmp (name, ".sbss") == 0
1159 || strcmp (name, ".lit4") == 0
1160 || strcmp (name, ".lit8") == 0)
1161 hdr->sh_flags |= SHF_ALPHA_GPREL;
1163 return TRUE;
1166 /* Hook called by the linker routine which adds symbols from an object
1167 file. We use it to put .comm items in .sbss, and not .bss. */
1169 static bfd_boolean
1170 elf64_alpha_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
1171 Elf_Internal_Sym *sym,
1172 const char **namep ATTRIBUTE_UNUSED,
1173 flagword *flagsp ATTRIBUTE_UNUSED,
1174 asection **secp, bfd_vma *valp)
1176 if (sym->st_shndx == SHN_COMMON
1177 && !info->relocatable
1178 && sym->st_size <= elf_gp_size (abfd))
1180 /* Common symbols less than or equal to -G nn bytes are
1181 automatically put into .sbss. */
1183 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1185 if (scomm == NULL)
1187 scomm = bfd_make_section_with_flags (abfd, ".scommon",
1188 (SEC_ALLOC
1189 | SEC_IS_COMMON
1190 | SEC_LINKER_CREATED));
1191 if (scomm == NULL)
1192 return FALSE;
1195 *secp = scomm;
1196 *valp = sym->st_size;
1199 return TRUE;
1202 /* Create the .got section. */
1204 static bfd_boolean
1205 elf64_alpha_create_got_section (bfd *abfd,
1206 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1208 flagword flags;
1209 asection *s;
1211 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1212 | SEC_LINKER_CREATED);
1213 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
1214 if (s == NULL
1215 || !bfd_set_section_alignment (abfd, s, 3))
1216 return FALSE;
1218 alpha_elf_tdata (abfd)->got = s;
1220 /* Make sure the object's gotobj is set to itself so that we default
1221 to every object with its own .got. We'll merge .gots later once
1222 we've collected each object's info. */
1223 alpha_elf_tdata (abfd)->gotobj = abfd;
1225 return TRUE;
1228 /* Create all the dynamic sections. */
1230 static bfd_boolean
1231 elf64_alpha_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
1233 asection *s;
1234 flagword flags;
1235 struct elf_link_hash_entry *h;
1237 /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
1239 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1240 | SEC_LINKER_CREATED
1241 | (elf64_alpha_use_secureplt ? SEC_READONLY : 0));
1242 s = bfd_make_section_anyway_with_flags (abfd, ".plt", flags);
1243 if (s == NULL || ! bfd_set_section_alignment (abfd, s, 4))
1244 return FALSE;
1246 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1247 .plt section. */
1248 h = _bfd_elf_define_linkage_sym (abfd, info, s,
1249 "_PROCEDURE_LINKAGE_TABLE_");
1250 elf_hash_table (info)->hplt = h;
1251 if (h == NULL)
1252 return FALSE;
1254 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1255 | SEC_LINKER_CREATED | SEC_READONLY);
1256 s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt", flags);
1257 if (s == NULL || ! bfd_set_section_alignment (abfd, s, 3))
1258 return FALSE;
1260 if (elf64_alpha_use_secureplt)
1262 flags = SEC_ALLOC | SEC_LINKER_CREATED;
1263 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
1264 if (s == NULL || ! bfd_set_section_alignment (abfd, s, 3))
1265 return FALSE;
1268 /* We may or may not have created a .got section for this object, but
1269 we definitely havn't done the rest of the work. */
1271 if (alpha_elf_tdata(abfd)->gotobj == NULL)
1273 if (!elf64_alpha_create_got_section (abfd, info))
1274 return FALSE;
1277 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1278 | SEC_LINKER_CREATED | SEC_READONLY);
1279 s = bfd_make_section_anyway_with_flags (abfd, ".rela.got", flags);
1280 if (s == NULL
1281 || !bfd_set_section_alignment (abfd, s, 3))
1282 return FALSE;
1284 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1285 dynobj's .got section. We don't do this in the linker script
1286 because we don't want to define the symbol if we are not creating
1287 a global offset table. */
1288 h = _bfd_elf_define_linkage_sym (abfd, info, alpha_elf_tdata(abfd)->got,
1289 "_GLOBAL_OFFSET_TABLE_");
1290 elf_hash_table (info)->hgot = h;
1291 if (h == NULL)
1292 return FALSE;
1294 return TRUE;
1297 /* Read ECOFF debugging information from a .mdebug section into a
1298 ecoff_debug_info structure. */
1300 static bfd_boolean
1301 elf64_alpha_read_ecoff_info (bfd *abfd, asection *section,
1302 struct ecoff_debug_info *debug)
1304 HDRR *symhdr;
1305 const struct ecoff_debug_swap *swap;
1306 char *ext_hdr = NULL;
1308 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1309 memset (debug, 0, sizeof (*debug));
1311 ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
1312 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1313 goto error_return;
1315 if (! bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1316 swap->external_hdr_size))
1317 goto error_return;
1319 symhdr = &debug->symbolic_header;
1320 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1322 /* The symbolic header contains absolute file offsets and sizes to
1323 read. */
1324 #define READ(ptr, offset, count, size, type) \
1325 if (symhdr->count == 0) \
1326 debug->ptr = NULL; \
1327 else \
1329 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1330 debug->ptr = (type) bfd_malloc (amt); \
1331 if (debug->ptr == NULL) \
1332 goto error_return; \
1333 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1334 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1335 goto error_return; \
1338 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1339 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1340 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1341 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1342 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1343 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1344 union aux_ext *);
1345 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1346 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1347 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1348 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1349 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1350 #undef READ
1352 debug->fdr = NULL;
1354 return TRUE;
1356 error_return:
1357 if (ext_hdr != NULL)
1358 free (ext_hdr);
1359 if (debug->line != NULL)
1360 free (debug->line);
1361 if (debug->external_dnr != NULL)
1362 free (debug->external_dnr);
1363 if (debug->external_pdr != NULL)
1364 free (debug->external_pdr);
1365 if (debug->external_sym != NULL)
1366 free (debug->external_sym);
1367 if (debug->external_opt != NULL)
1368 free (debug->external_opt);
1369 if (debug->external_aux != NULL)
1370 free (debug->external_aux);
1371 if (debug->ss != NULL)
1372 free (debug->ss);
1373 if (debug->ssext != NULL)
1374 free (debug->ssext);
1375 if (debug->external_fdr != NULL)
1376 free (debug->external_fdr);
1377 if (debug->external_rfd != NULL)
1378 free (debug->external_rfd);
1379 if (debug->external_ext != NULL)
1380 free (debug->external_ext);
1381 return FALSE;
1384 /* Alpha ELF local labels start with '$'. */
1386 static bfd_boolean
1387 elf64_alpha_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
1389 return name[0] == '$';
1392 /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
1393 routine in order to handle the ECOFF debugging information. We
1394 still call this mips_elf_find_line because of the slot
1395 find_line_info in elf_obj_tdata is declared that way. */
1397 struct mips_elf_find_line
1399 struct ecoff_debug_info d;
1400 struct ecoff_find_line i;
1403 static bfd_boolean
1404 elf64_alpha_find_nearest_line (bfd *abfd, asection *section, asymbol **symbols,
1405 bfd_vma offset, const char **filename_ptr,
1406 const char **functionname_ptr,
1407 unsigned int *line_ptr)
1409 asection *msec;
1411 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1412 filename_ptr, functionname_ptr,
1413 line_ptr, 0,
1414 &elf_tdata (abfd)->dwarf2_find_line_info))
1415 return TRUE;
1417 msec = bfd_get_section_by_name (abfd, ".mdebug");
1418 if (msec != NULL)
1420 flagword origflags;
1421 struct mips_elf_find_line *fi;
1422 const struct ecoff_debug_swap * const swap =
1423 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1425 /* If we are called during a link, alpha_elf_final_link may have
1426 cleared the SEC_HAS_CONTENTS field. We force it back on here
1427 if appropriate (which it normally will be). */
1428 origflags = msec->flags;
1429 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
1430 msec->flags |= SEC_HAS_CONTENTS;
1432 fi = elf_tdata (abfd)->find_line_info;
1433 if (fi == NULL)
1435 bfd_size_type external_fdr_size;
1436 char *fraw_src;
1437 char *fraw_end;
1438 struct fdr *fdr_ptr;
1439 bfd_size_type amt = sizeof (struct mips_elf_find_line);
1441 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
1442 if (fi == NULL)
1444 msec->flags = origflags;
1445 return FALSE;
1448 if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
1450 msec->flags = origflags;
1451 return FALSE;
1454 /* Swap in the FDR information. */
1455 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
1456 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
1457 if (fi->d.fdr == NULL)
1459 msec->flags = origflags;
1460 return FALSE;
1462 external_fdr_size = swap->external_fdr_size;
1463 fdr_ptr = fi->d.fdr;
1464 fraw_src = (char *) fi->d.external_fdr;
1465 fraw_end = (fraw_src
1466 + fi->d.symbolic_header.ifdMax * external_fdr_size);
1467 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
1468 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
1470 elf_tdata (abfd)->find_line_info = fi;
1472 /* Note that we don't bother to ever free this information.
1473 find_nearest_line is either called all the time, as in
1474 objdump -l, so the information should be saved, or it is
1475 rarely called, as in ld error messages, so the memory
1476 wasted is unimportant. Still, it would probably be a
1477 good idea for free_cached_info to throw it away. */
1480 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
1481 &fi->i, filename_ptr, functionname_ptr,
1482 line_ptr))
1484 msec->flags = origflags;
1485 return TRUE;
1488 msec->flags = origflags;
1491 /* Fall back on the generic ELF find_nearest_line routine. */
1493 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
1494 filename_ptr, functionname_ptr,
1495 line_ptr);
1498 /* Structure used to pass information to alpha_elf_output_extsym. */
1500 struct extsym_info
1502 bfd *abfd;
1503 struct bfd_link_info *info;
1504 struct ecoff_debug_info *debug;
1505 const struct ecoff_debug_swap *swap;
1506 bfd_boolean failed;
1509 static bfd_boolean
1510 elf64_alpha_output_extsym (struct alpha_elf_link_hash_entry *h, PTR data)
1512 struct extsym_info *einfo = (struct extsym_info *) data;
1513 bfd_boolean strip;
1514 asection *sec, *output_section;
1516 if (h->root.root.type == bfd_link_hash_warning)
1517 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
1519 if (h->root.indx == -2)
1520 strip = FALSE;
1521 else if ((h->root.def_dynamic
1522 || h->root.ref_dynamic
1523 || h->root.root.type == bfd_link_hash_new)
1524 && !h->root.def_regular
1525 && !h->root.ref_regular)
1526 strip = TRUE;
1527 else if (einfo->info->strip == strip_all
1528 || (einfo->info->strip == strip_some
1529 && bfd_hash_lookup (einfo->info->keep_hash,
1530 h->root.root.root.string,
1531 FALSE, FALSE) == NULL))
1532 strip = TRUE;
1533 else
1534 strip = FALSE;
1536 if (strip)
1537 return TRUE;
1539 if (h->esym.ifd == -2)
1541 h->esym.jmptbl = 0;
1542 h->esym.cobol_main = 0;
1543 h->esym.weakext = 0;
1544 h->esym.reserved = 0;
1545 h->esym.ifd = ifdNil;
1546 h->esym.asym.value = 0;
1547 h->esym.asym.st = stGlobal;
1549 if (h->root.root.type != bfd_link_hash_defined
1550 && h->root.root.type != bfd_link_hash_defweak)
1551 h->esym.asym.sc = scAbs;
1552 else
1554 const char *name;
1556 sec = h->root.root.u.def.section;
1557 output_section = sec->output_section;
1559 /* When making a shared library and symbol h is the one from
1560 the another shared library, OUTPUT_SECTION may be null. */
1561 if (output_section == NULL)
1562 h->esym.asym.sc = scUndefined;
1563 else
1565 name = bfd_section_name (output_section->owner, output_section);
1567 if (strcmp (name, ".text") == 0)
1568 h->esym.asym.sc = scText;
1569 else if (strcmp (name, ".data") == 0)
1570 h->esym.asym.sc = scData;
1571 else if (strcmp (name, ".sdata") == 0)
1572 h->esym.asym.sc = scSData;
1573 else if (strcmp (name, ".rodata") == 0
1574 || strcmp (name, ".rdata") == 0)
1575 h->esym.asym.sc = scRData;
1576 else if (strcmp (name, ".bss") == 0)
1577 h->esym.asym.sc = scBss;
1578 else if (strcmp (name, ".sbss") == 0)
1579 h->esym.asym.sc = scSBss;
1580 else if (strcmp (name, ".init") == 0)
1581 h->esym.asym.sc = scInit;
1582 else if (strcmp (name, ".fini") == 0)
1583 h->esym.asym.sc = scFini;
1584 else
1585 h->esym.asym.sc = scAbs;
1589 h->esym.asym.reserved = 0;
1590 h->esym.asym.index = indexNil;
1593 if (h->root.root.type == bfd_link_hash_common)
1594 h->esym.asym.value = h->root.root.u.c.size;
1595 else if (h->root.root.type == bfd_link_hash_defined
1596 || h->root.root.type == bfd_link_hash_defweak)
1598 if (h->esym.asym.sc == scCommon)
1599 h->esym.asym.sc = scBss;
1600 else if (h->esym.asym.sc == scSCommon)
1601 h->esym.asym.sc = scSBss;
1603 sec = h->root.root.u.def.section;
1604 output_section = sec->output_section;
1605 if (output_section != NULL)
1606 h->esym.asym.value = (h->root.root.u.def.value
1607 + sec->output_offset
1608 + output_section->vma);
1609 else
1610 h->esym.asym.value = 0;
1613 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1614 h->root.root.root.string,
1615 &h->esym))
1617 einfo->failed = TRUE;
1618 return FALSE;
1621 return TRUE;
1624 /* Search for and possibly create a got entry. */
1626 static struct alpha_elf_got_entry *
1627 get_got_entry (bfd *abfd, struct alpha_elf_link_hash_entry *h,
1628 unsigned long r_type, unsigned long r_symndx,
1629 bfd_vma r_addend)
1631 struct alpha_elf_got_entry *gotent;
1632 struct alpha_elf_got_entry **slot;
1634 if (h)
1635 slot = &h->got_entries;
1636 else
1638 /* This is a local .got entry -- record for merge. */
1640 struct alpha_elf_got_entry **local_got_entries;
1642 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1643 if (!local_got_entries)
1645 bfd_size_type size;
1646 Elf_Internal_Shdr *symtab_hdr;
1648 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
1649 size = symtab_hdr->sh_info;
1650 size *= sizeof (struct alpha_elf_got_entry *);
1652 local_got_entries
1653 = (struct alpha_elf_got_entry **) bfd_zalloc (abfd, size);
1654 if (!local_got_entries)
1655 return NULL;
1657 alpha_elf_tdata (abfd)->local_got_entries = local_got_entries;
1660 slot = &local_got_entries[r_symndx];
1663 for (gotent = *slot; gotent ; gotent = gotent->next)
1664 if (gotent->gotobj == abfd
1665 && gotent->reloc_type == r_type
1666 && gotent->addend == r_addend)
1667 break;
1669 if (!gotent)
1671 int entry_size;
1672 bfd_size_type amt;
1674 amt = sizeof (struct alpha_elf_got_entry);
1675 gotent = (struct alpha_elf_got_entry *) bfd_alloc (abfd, amt);
1676 if (!gotent)
1677 return NULL;
1679 gotent->gotobj = abfd;
1680 gotent->addend = r_addend;
1681 gotent->got_offset = -1;
1682 gotent->plt_offset = -1;
1683 gotent->use_count = 1;
1684 gotent->reloc_type = r_type;
1685 gotent->reloc_done = 0;
1686 gotent->reloc_xlated = 0;
1688 gotent->next = *slot;
1689 *slot = gotent;
1691 entry_size = alpha_got_entry_size (r_type);
1692 alpha_elf_tdata (abfd)->total_got_size += entry_size;
1693 if (!h)
1694 alpha_elf_tdata(abfd)->local_got_size += entry_size;
1696 else
1697 gotent->use_count += 1;
1699 return gotent;
1702 static bfd_boolean
1703 elf64_alpha_want_plt (struct alpha_elf_link_hash_entry *ah)
1705 return ((ah->root.type == STT_FUNC
1706 || ah->root.root.type == bfd_link_hash_undefweak
1707 || ah->root.root.type == bfd_link_hash_undefined)
1708 && (ah->flags & ALPHA_ELF_LINK_HASH_LU_PLT) != 0
1709 && (ah->flags & ~ALPHA_ELF_LINK_HASH_LU_PLT) == 0);
1712 /* Handle dynamic relocations when doing an Alpha ELF link. */
1714 static bfd_boolean
1715 elf64_alpha_check_relocs (bfd *abfd, struct bfd_link_info *info,
1716 asection *sec, const Elf_Internal_Rela *relocs)
1718 bfd *dynobj;
1719 asection *sreloc;
1720 const char *rel_sec_name;
1721 Elf_Internal_Shdr *symtab_hdr;
1722 struct alpha_elf_link_hash_entry **sym_hashes;
1723 const Elf_Internal_Rela *rel, *relend;
1724 bfd_size_type amt;
1726 if (info->relocatable)
1727 return TRUE;
1729 /* Don't do anything special with non-loaded, non-alloced sections.
1730 In particular, any relocs in such sections should not affect GOT
1731 and PLT reference counting (ie. we don't allow them to create GOT
1732 or PLT entries), there's no possibility or desire to optimize TLS
1733 relocs, and there's not much point in propagating relocs to shared
1734 libs that the dynamic linker won't relocate. */
1735 if ((sec->flags & SEC_ALLOC) == 0)
1736 return TRUE;
1738 dynobj = elf_hash_table(info)->dynobj;
1739 if (dynobj == NULL)
1740 elf_hash_table(info)->dynobj = dynobj = abfd;
1742 sreloc = NULL;
1743 rel_sec_name = NULL;
1744 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
1745 sym_hashes = alpha_elf_sym_hashes(abfd);
1747 relend = relocs + sec->reloc_count;
1748 for (rel = relocs; rel < relend; ++rel)
1750 enum {
1751 NEED_GOT = 1,
1752 NEED_GOT_ENTRY = 2,
1753 NEED_DYNREL = 4
1756 unsigned long r_symndx, r_type;
1757 struct alpha_elf_link_hash_entry *h;
1758 unsigned int gotent_flags;
1759 bfd_boolean maybe_dynamic;
1760 unsigned int need;
1761 bfd_vma addend;
1763 r_symndx = ELF64_R_SYM (rel->r_info);
1764 if (r_symndx < symtab_hdr->sh_info)
1765 h = NULL;
1766 else
1768 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1770 while (h->root.root.type == bfd_link_hash_indirect
1771 || h->root.root.type == bfd_link_hash_warning)
1772 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
1774 h->root.ref_regular = 1;
1777 /* We can only get preliminary data on whether a symbol is
1778 locally or externally defined, as not all of the input files
1779 have yet been processed. Do something with what we know, as
1780 this may help reduce memory usage and processing time later. */
1781 maybe_dynamic = FALSE;
1782 if (h && ((info->shared
1783 && (!info->symbolic
1784 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
1785 || !h->root.def_regular
1786 || h->root.root.type == bfd_link_hash_defweak))
1787 maybe_dynamic = TRUE;
1789 need = 0;
1790 gotent_flags = 0;
1791 r_type = ELF64_R_TYPE (rel->r_info);
1792 addend = rel->r_addend;
1794 switch (r_type)
1796 case R_ALPHA_LITERAL:
1797 need = NEED_GOT | NEED_GOT_ENTRY;
1799 /* Remember how this literal is used from its LITUSEs.
1800 This will be important when it comes to decide if we can
1801 create a .plt entry for a function symbol. */
1802 while (++rel < relend && ELF64_R_TYPE (rel->r_info) == R_ALPHA_LITUSE)
1803 if (rel->r_addend >= 1 && rel->r_addend <= 6)
1804 gotent_flags |= 1 << rel->r_addend;
1805 --rel;
1807 /* No LITUSEs -- presumably the address is used somehow. */
1808 if (gotent_flags == 0)
1809 gotent_flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
1810 break;
1812 case R_ALPHA_GPDISP:
1813 case R_ALPHA_GPREL16:
1814 case R_ALPHA_GPREL32:
1815 case R_ALPHA_GPRELHIGH:
1816 case R_ALPHA_GPRELLOW:
1817 case R_ALPHA_BRSGP:
1818 need = NEED_GOT;
1819 break;
1821 case R_ALPHA_REFLONG:
1822 case R_ALPHA_REFQUAD:
1823 if (info->shared || maybe_dynamic)
1824 need = NEED_DYNREL;
1825 break;
1827 case R_ALPHA_TLSLDM:
1828 /* The symbol for a TLSLDM reloc is ignored. Collapse the
1829 reloc to the 0 symbol so that they all match. */
1830 r_symndx = 0;
1831 h = 0;
1832 maybe_dynamic = FALSE;
1833 /* FALLTHRU */
1835 case R_ALPHA_TLSGD:
1836 case R_ALPHA_GOTDTPREL:
1837 need = NEED_GOT | NEED_GOT_ENTRY;
1838 break;
1840 case R_ALPHA_GOTTPREL:
1841 need = NEED_GOT | NEED_GOT_ENTRY;
1842 gotent_flags = ALPHA_ELF_LINK_HASH_TLS_IE;
1843 if (info->shared)
1844 info->flags |= DF_STATIC_TLS;
1845 break;
1847 case R_ALPHA_TPREL64:
1848 if (info->shared || maybe_dynamic)
1849 need = NEED_DYNREL;
1850 if (info->shared)
1851 info->flags |= DF_STATIC_TLS;
1852 break;
1855 if (need & NEED_GOT)
1857 if (alpha_elf_tdata(abfd)->gotobj == NULL)
1859 if (!elf64_alpha_create_got_section (abfd, info))
1860 return FALSE;
1864 if (need & NEED_GOT_ENTRY)
1866 struct alpha_elf_got_entry *gotent;
1868 gotent = get_got_entry (abfd, h, r_type, r_symndx, addend);
1869 if (!gotent)
1870 return FALSE;
1872 if (gotent_flags)
1874 gotent->flags |= gotent_flags;
1875 if (h)
1877 gotent_flags |= h->flags;
1878 h->flags = gotent_flags;
1880 /* Make a guess as to whether a .plt entry is needed. */
1881 /* ??? It appears that we won't make it into
1882 adjust_dynamic_symbol for symbols that remain
1883 totally undefined. Copying this check here means
1884 we can create a plt entry for them too. */
1885 h->root.needs_plt
1886 = (maybe_dynamic && elf64_alpha_want_plt (h));
1891 if (need & NEED_DYNREL)
1893 if (rel_sec_name == NULL)
1895 rel_sec_name = (bfd_elf_string_from_elf_section
1896 (abfd, elf_elfheader(abfd)->e_shstrndx,
1897 elf_section_data(sec)->rel_hdr.sh_name));
1898 if (rel_sec_name == NULL)
1899 return FALSE;
1901 BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
1902 && strcmp (bfd_get_section_name (abfd, sec),
1903 rel_sec_name+5) == 0);
1906 /* We need to create the section here now whether we eventually
1907 use it or not so that it gets mapped to an output section by
1908 the linker. If not used, we'll kill it in
1909 size_dynamic_sections. */
1910 if (sreloc == NULL)
1912 sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
1913 if (sreloc == NULL)
1915 flagword flags;
1917 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
1918 | SEC_LINKER_CREATED | SEC_READONLY);
1919 if (sec->flags & SEC_ALLOC)
1920 flags |= SEC_ALLOC | SEC_LOAD;
1921 sreloc = bfd_make_section_with_flags (dynobj,
1922 rel_sec_name,
1923 flags);
1924 if (sreloc == NULL
1925 || !bfd_set_section_alignment (dynobj, sreloc, 3))
1926 return FALSE;
1930 if (h)
1932 /* Since we havn't seen all of the input symbols yet, we
1933 don't know whether we'll actually need a dynamic relocation
1934 entry for this reloc. So make a record of it. Once we
1935 find out if this thing needs dynamic relocation we'll
1936 expand the relocation sections by the appropriate amount. */
1938 struct alpha_elf_reloc_entry *rent;
1940 for (rent = h->reloc_entries; rent; rent = rent->next)
1941 if (rent->rtype == r_type && rent->srel == sreloc)
1942 break;
1944 if (!rent)
1946 amt = sizeof (struct alpha_elf_reloc_entry);
1947 rent = (struct alpha_elf_reloc_entry *) bfd_alloc (abfd, amt);
1948 if (!rent)
1949 return FALSE;
1951 rent->srel = sreloc;
1952 rent->rtype = r_type;
1953 rent->count = 1;
1954 rent->reltext = (sec->flags & SEC_READONLY) != 0;
1956 rent->next = h->reloc_entries;
1957 h->reloc_entries = rent;
1959 else
1960 rent->count++;
1962 else if (info->shared)
1964 /* If this is a shared library, and the section is to be
1965 loaded into memory, we need a RELATIVE reloc. */
1966 sreloc->size += sizeof (Elf64_External_Rela);
1967 if (sec->flags & SEC_READONLY)
1968 info->flags |= DF_TEXTREL;
1973 return TRUE;
1976 /* Adjust a symbol defined by a dynamic object and referenced by a
1977 regular object. The current definition is in some section of the
1978 dynamic object, but we're not including those sections. We have to
1979 change the definition to something the rest of the link can
1980 understand. */
1982 static bfd_boolean
1983 elf64_alpha_adjust_dynamic_symbol (struct bfd_link_info *info,
1984 struct elf_link_hash_entry *h)
1986 bfd *dynobj;
1987 asection *s;
1988 struct alpha_elf_link_hash_entry *ah;
1990 dynobj = elf_hash_table(info)->dynobj;
1991 ah = (struct alpha_elf_link_hash_entry *)h;
1993 /* Now that we've seen all of the input symbols, finalize our decision
1994 about whether this symbol should get a .plt entry. Irritatingly, it
1995 is common for folk to leave undefined symbols in shared libraries,
1996 and they still expect lazy binding; accept undefined symbols in lieu
1997 of STT_FUNC. */
1998 if (alpha_elf_dynamic_symbol_p (h, info) && elf64_alpha_want_plt (ah))
2000 h->needs_plt = TRUE;
2002 s = bfd_get_section_by_name(dynobj, ".plt");
2003 if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
2004 return FALSE;
2006 /* We need one plt entry per got subsection. Delay allocation of
2007 the actual plt entries until size_plt_section, called from
2008 size_dynamic_sections or during relaxation. */
2010 return TRUE;
2012 else
2013 h->needs_plt = FALSE;
2015 /* If this is a weak symbol, and there is a real definition, the
2016 processor independent code will have arranged for us to see the
2017 real definition first, and we can just use the same value. */
2018 if (h->u.weakdef != NULL)
2020 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2021 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2022 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2023 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2024 return TRUE;
2027 /* This is a reference to a symbol defined by a dynamic object which
2028 is not a function. The Alpha, since it uses .got entries for all
2029 symbols even in regular objects, does not need the hackery of a
2030 .dynbss section and COPY dynamic relocations. */
2032 return TRUE;
2035 /* Symbol versioning can create new symbols, and make our old symbols
2036 indirect to the new ones. Consolidate the got and reloc information
2037 in these situations. */
2039 static bfd_boolean
2040 elf64_alpha_merge_ind_symbols (struct alpha_elf_link_hash_entry *hi,
2041 PTR dummy ATTRIBUTE_UNUSED)
2043 struct alpha_elf_link_hash_entry *hs;
2045 if (hi->root.root.type != bfd_link_hash_indirect)
2046 return TRUE;
2047 hs = hi;
2048 do {
2049 hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
2050 } while (hs->root.root.type == bfd_link_hash_indirect);
2052 /* Merge the flags. Whee. */
2054 hs->flags |= hi->flags;
2056 /* Merge the .got entries. Cannibalize the old symbol's list in
2057 doing so, since we don't need it anymore. */
2059 if (hs->got_entries == NULL)
2060 hs->got_entries = hi->got_entries;
2061 else
2063 struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
2065 gsh = hs->got_entries;
2066 for (gi = hi->got_entries; gi ; gi = gin)
2068 gin = gi->next;
2069 for (gs = gsh; gs ; gs = gs->next)
2070 if (gi->gotobj == gs->gotobj
2071 && gi->reloc_type == gs->reloc_type
2072 && gi->addend == gs->addend)
2074 gi->use_count += gs->use_count;
2075 goto got_found;
2077 gi->next = hs->got_entries;
2078 hs->got_entries = gi;
2079 got_found:;
2082 hi->got_entries = NULL;
2084 /* And similar for the reloc entries. */
2086 if (hs->reloc_entries == NULL)
2087 hs->reloc_entries = hi->reloc_entries;
2088 else
2090 struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
2092 rsh = hs->reloc_entries;
2093 for (ri = hi->reloc_entries; ri ; ri = rin)
2095 rin = ri->next;
2096 for (rs = rsh; rs ; rs = rs->next)
2097 if (ri->rtype == rs->rtype && ri->srel == rs->srel)
2099 rs->count += ri->count;
2100 goto found_reloc;
2102 ri->next = hs->reloc_entries;
2103 hs->reloc_entries = ri;
2104 found_reloc:;
2107 hi->reloc_entries = NULL;
2109 return TRUE;
2112 /* Is it possible to merge two object file's .got tables? */
2114 static bfd_boolean
2115 elf64_alpha_can_merge_gots (bfd *a, bfd *b)
2117 int total = alpha_elf_tdata (a)->total_got_size;
2118 bfd *bsub;
2120 /* Trivial quick fallout test. */
2121 if (total + alpha_elf_tdata (b)->total_got_size <= MAX_GOT_SIZE)
2122 return TRUE;
2124 /* By their nature, local .got entries cannot be merged. */
2125 if ((total += alpha_elf_tdata (b)->local_got_size) > MAX_GOT_SIZE)
2126 return FALSE;
2128 /* Failing the common trivial comparison, we must effectively
2129 perform the merge. Not actually performing the merge means that
2130 we don't have to store undo information in case we fail. */
2131 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2133 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
2134 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2135 int i, n;
2137 n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
2138 for (i = 0; i < n; ++i)
2140 struct alpha_elf_got_entry *ae, *be;
2141 struct alpha_elf_link_hash_entry *h;
2143 h = hashes[i];
2144 while (h->root.root.type == bfd_link_hash_indirect
2145 || h->root.root.type == bfd_link_hash_warning)
2146 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2148 for (be = h->got_entries; be ; be = be->next)
2150 if (be->use_count == 0)
2151 continue;
2152 if (be->gotobj != b)
2153 continue;
2155 for (ae = h->got_entries; ae ; ae = ae->next)
2156 if (ae->gotobj == a
2157 && ae->reloc_type == be->reloc_type
2158 && ae->addend == be->addend)
2159 goto global_found;
2161 total += alpha_got_entry_size (be->reloc_type);
2162 if (total > MAX_GOT_SIZE)
2163 return FALSE;
2164 global_found:;
2169 return TRUE;
2172 /* Actually merge two .got tables. */
2174 static void
2175 elf64_alpha_merge_gots (bfd *a, bfd *b)
2177 int total = alpha_elf_tdata (a)->total_got_size;
2178 bfd *bsub;
2180 /* Remember local expansion. */
2182 int e = alpha_elf_tdata (b)->local_got_size;
2183 total += e;
2184 alpha_elf_tdata (a)->local_got_size += e;
2187 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2189 struct alpha_elf_got_entry **local_got_entries;
2190 struct alpha_elf_link_hash_entry **hashes;
2191 Elf_Internal_Shdr *symtab_hdr;
2192 int i, n;
2194 /* Let the local .got entries know they are part of a new subsegment. */
2195 local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
2196 if (local_got_entries)
2198 n = elf_tdata (bsub)->symtab_hdr.sh_info;
2199 for (i = 0; i < n; ++i)
2201 struct alpha_elf_got_entry *ent;
2202 for (ent = local_got_entries[i]; ent; ent = ent->next)
2203 ent->gotobj = a;
2207 /* Merge the global .got entries. */
2208 hashes = alpha_elf_sym_hashes (bsub);
2209 symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2211 n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
2212 for (i = 0; i < n; ++i)
2214 struct alpha_elf_got_entry *ae, *be, **pbe, **start;
2215 struct alpha_elf_link_hash_entry *h;
2217 h = hashes[i];
2218 while (h->root.root.type == bfd_link_hash_indirect
2219 || h->root.root.type == bfd_link_hash_warning)
2220 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2222 pbe = start = &h->got_entries;
2223 while ((be = *pbe) != NULL)
2225 if (be->use_count == 0)
2227 *pbe = be->next;
2228 memset (be, 0xa5, sizeof (*be));
2229 goto kill;
2231 if (be->gotobj != b)
2232 goto next;
2234 for (ae = *start; ae ; ae = ae->next)
2235 if (ae->gotobj == a
2236 && ae->reloc_type == be->reloc_type
2237 && ae->addend == be->addend)
2239 ae->flags |= be->flags;
2240 ae->use_count += be->use_count;
2241 *pbe = be->next;
2242 memset (be, 0xa5, sizeof (*be));
2243 goto kill;
2245 be->gotobj = a;
2246 total += alpha_got_entry_size (be->reloc_type);
2248 next:;
2249 pbe = &be->next;
2250 kill:;
2254 alpha_elf_tdata (bsub)->gotobj = a;
2256 alpha_elf_tdata (a)->total_got_size = total;
2258 /* Merge the two in_got chains. */
2260 bfd *next;
2262 bsub = a;
2263 while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
2264 bsub = next;
2266 alpha_elf_tdata (bsub)->in_got_link_next = b;
2270 /* Calculate the offsets for the got entries. */
2272 static bfd_boolean
2273 elf64_alpha_calc_got_offsets_for_symbol (struct alpha_elf_link_hash_entry *h,
2274 PTR arg ATTRIBUTE_UNUSED)
2276 struct alpha_elf_got_entry *gotent;
2278 if (h->root.root.type == bfd_link_hash_warning)
2279 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
2281 for (gotent = h->got_entries; gotent; gotent = gotent->next)
2282 if (gotent->use_count > 0)
2284 struct alpha_elf_obj_tdata *td;
2285 bfd_size_type *plge;
2287 td = alpha_elf_tdata (gotent->gotobj);
2288 plge = &td->got->size;
2289 gotent->got_offset = *plge;
2290 *plge += alpha_got_entry_size (gotent->reloc_type);
2293 return TRUE;
2296 static void
2297 elf64_alpha_calc_got_offsets (struct bfd_link_info *info)
2299 bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
2301 /* First, zero out the .got sizes, as we may be recalculating the
2302 .got after optimizing it. */
2303 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2304 alpha_elf_tdata(i)->got->size = 0;
2306 /* Next, fill in the offsets for all the global entries. */
2307 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2308 elf64_alpha_calc_got_offsets_for_symbol,
2309 NULL);
2311 /* Finally, fill in the offsets for the local entries. */
2312 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2314 bfd_size_type got_offset = alpha_elf_tdata(i)->got->size;
2315 bfd *j;
2317 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2319 struct alpha_elf_got_entry **local_got_entries, *gotent;
2320 int k, n;
2322 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2323 if (!local_got_entries)
2324 continue;
2326 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
2327 for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
2328 if (gotent->use_count > 0)
2330 gotent->got_offset = got_offset;
2331 got_offset += alpha_got_entry_size (gotent->reloc_type);
2335 alpha_elf_tdata(i)->got->size = got_offset;
2339 /* Constructs the gots. */
2341 static bfd_boolean
2342 elf64_alpha_size_got_sections (struct bfd_link_info *info)
2344 bfd *i, *got_list, *cur_got_obj = NULL;
2345 int something_changed = 0;
2347 got_list = alpha_elf_hash_table (info)->got_list;
2349 /* On the first time through, pretend we have an existing got list
2350 consisting of all of the input files. */
2351 if (got_list == NULL)
2353 for (i = info->input_bfds; i ; i = i->link_next)
2355 bfd *this_got = alpha_elf_tdata (i)->gotobj;
2356 if (this_got == NULL)
2357 continue;
2359 /* We are assuming no merging has yet occurred. */
2360 BFD_ASSERT (this_got == i);
2362 if (alpha_elf_tdata (this_got)->total_got_size > MAX_GOT_SIZE)
2364 /* Yikes! A single object file has too many entries. */
2365 (*_bfd_error_handler)
2366 (_("%B: .got subsegment exceeds 64K (size %d)"),
2367 i, alpha_elf_tdata (this_got)->total_got_size);
2368 return FALSE;
2371 if (got_list == NULL)
2372 got_list = this_got;
2373 else
2374 alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
2375 cur_got_obj = this_got;
2378 /* Strange degenerate case of no got references. */
2379 if (got_list == NULL)
2380 return TRUE;
2382 alpha_elf_hash_table (info)->got_list = got_list;
2384 /* Force got offsets to be recalculated. */
2385 something_changed = 1;
2388 cur_got_obj = got_list;
2389 i = alpha_elf_tdata(cur_got_obj)->got_link_next;
2390 while (i != NULL)
2392 if (elf64_alpha_can_merge_gots (cur_got_obj, i))
2394 elf64_alpha_merge_gots (cur_got_obj, i);
2396 alpha_elf_tdata(i)->got->size = 0;
2397 i = alpha_elf_tdata(i)->got_link_next;
2398 alpha_elf_tdata(cur_got_obj)->got_link_next = i;
2400 something_changed = 1;
2402 else
2404 cur_got_obj = i;
2405 i = alpha_elf_tdata(i)->got_link_next;
2409 /* Once the gots have been merged, fill in the got offsets for
2410 everything therein. */
2411 if (1 || something_changed)
2412 elf64_alpha_calc_got_offsets (info);
2414 return TRUE;
2417 static bfd_boolean
2418 elf64_alpha_size_plt_section_1 (struct alpha_elf_link_hash_entry *h, PTR data)
2420 asection *splt = (asection *) data;
2421 struct alpha_elf_got_entry *gotent;
2422 bfd_boolean saw_one = FALSE;
2424 /* If we didn't need an entry before, we still don't. */
2425 if (!h->root.needs_plt)
2426 return TRUE;
2428 /* For each LITERAL got entry still in use, allocate a plt entry. */
2429 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2430 if (gotent->reloc_type == R_ALPHA_LITERAL
2431 && gotent->use_count > 0)
2433 if (splt->size == 0)
2434 splt->size = PLT_HEADER_SIZE;
2435 gotent->plt_offset = splt->size;
2436 splt->size += PLT_ENTRY_SIZE;
2437 saw_one = TRUE;
2440 /* If there weren't any, there's no longer a need for the PLT entry. */
2441 if (!saw_one)
2442 h->root.needs_plt = FALSE;
2444 return TRUE;
2447 /* Called from relax_section to rebuild the PLT in light of
2448 potential changes in the function's status. */
2450 static bfd_boolean
2451 elf64_alpha_size_plt_section (struct bfd_link_info *info)
2453 asection *splt, *spltrel, *sgotplt;
2454 unsigned long entries;
2455 bfd *dynobj;
2457 dynobj = elf_hash_table(info)->dynobj;
2458 splt = bfd_get_section_by_name (dynobj, ".plt");
2459 if (splt == NULL)
2460 return TRUE;
2462 splt->size = 0;
2464 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2465 elf64_alpha_size_plt_section_1, splt);
2467 /* Every plt entry requires a JMP_SLOT relocation. */
2468 spltrel = bfd_get_section_by_name (dynobj, ".rela.plt");
2469 if (splt->size)
2471 if (elf64_alpha_use_secureplt)
2472 entries = (splt->size - NEW_PLT_HEADER_SIZE) / NEW_PLT_ENTRY_SIZE;
2473 else
2474 entries = (splt->size - OLD_PLT_HEADER_SIZE) / OLD_PLT_ENTRY_SIZE;
2476 else
2477 entries = 0;
2478 spltrel->size = entries * sizeof (Elf64_External_Rela);
2480 /* When using the secureplt, we need two words somewhere in the data
2481 segment for the dynamic linker to tell us where to go. This is the
2482 entire contents of the .got.plt section. */
2483 if (elf64_alpha_use_secureplt)
2485 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2486 sgotplt->size = entries ? 16 : 0;
2489 return TRUE;
2492 static bfd_boolean
2493 elf64_alpha_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2494 struct bfd_link_info *info)
2496 bfd *i;
2498 if (info->relocatable)
2499 return TRUE;
2501 /* First, take care of the indirect symbols created by versioning. */
2502 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2503 elf64_alpha_merge_ind_symbols,
2504 NULL);
2506 if (!elf64_alpha_size_got_sections (info))
2507 return FALSE;
2509 /* Allocate space for all of the .got subsections. */
2510 i = alpha_elf_hash_table (info)->got_list;
2511 for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
2513 asection *s = alpha_elf_tdata(i)->got;
2514 if (s->size > 0)
2516 s->contents = (bfd_byte *) bfd_zalloc (i, s->size);
2517 if (s->contents == NULL)
2518 return FALSE;
2522 return TRUE;
2525 /* The number of dynamic relocations required by a static relocation. */
2527 static int
2528 alpha_dynamic_entries_for_reloc (int r_type, int dynamic, int shared)
2530 switch (r_type)
2532 /* May appear in GOT entries. */
2533 case R_ALPHA_TLSGD:
2534 return (dynamic ? 2 : shared ? 1 : 0);
2535 case R_ALPHA_TLSLDM:
2536 return shared;
2537 case R_ALPHA_LITERAL:
2538 case R_ALPHA_GOTTPREL:
2539 return dynamic || shared;
2540 case R_ALPHA_GOTDTPREL:
2541 return dynamic;
2543 /* May appear in data sections. */
2544 case R_ALPHA_REFLONG:
2545 case R_ALPHA_REFQUAD:
2546 case R_ALPHA_TPREL64:
2547 return dynamic || shared;
2549 /* Everything else is illegal. We'll issue an error during
2550 relocate_section. */
2551 default:
2552 return 0;
2556 /* Work out the sizes of the dynamic relocation entries. */
2558 static bfd_boolean
2559 elf64_alpha_calc_dynrel_sizes (struct alpha_elf_link_hash_entry *h,
2560 struct bfd_link_info *info)
2562 bfd_boolean dynamic;
2563 struct alpha_elf_reloc_entry *relent;
2564 unsigned long entries;
2566 if (h->root.root.type == bfd_link_hash_warning)
2567 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
2569 /* If the symbol was defined as a common symbol in a regular object
2570 file, and there was no definition in any dynamic object, then the
2571 linker will have allocated space for the symbol in a common
2572 section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
2573 set. This is done for dynamic symbols in
2574 elf_adjust_dynamic_symbol but this is not done for non-dynamic
2575 symbols, somehow. */
2576 if (!h->root.def_regular
2577 && h->root.ref_regular
2578 && !h->root.def_dynamic
2579 && (h->root.root.type == bfd_link_hash_defined
2580 || h->root.root.type == bfd_link_hash_defweak)
2581 && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
2582 h->root.def_regular = 1;
2584 /* If the symbol is dynamic, we'll need all the relocations in their
2585 natural form. If this is a shared object, and it has been forced
2586 local, we'll need the same number of RELATIVE relocations. */
2587 dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
2589 /* If the symbol is a hidden undefined weak, then we never have any
2590 relocations. Avoid the loop which may want to add RELATIVE relocs
2591 based on info->shared. */
2592 if (h->root.root.type == bfd_link_hash_undefweak && !dynamic)
2593 return TRUE;
2595 for (relent = h->reloc_entries; relent; relent = relent->next)
2597 entries = alpha_dynamic_entries_for_reloc (relent->rtype, dynamic,
2598 info->shared);
2599 if (entries)
2601 relent->srel->size +=
2602 entries * sizeof (Elf64_External_Rela) * relent->count;
2603 if (relent->reltext)
2604 info->flags |= DT_TEXTREL;
2608 return TRUE;
2611 /* Subroutine of elf64_alpha_size_rela_got_section for doing the
2612 global symbols. */
2614 static bfd_boolean
2615 elf64_alpha_size_rela_got_1 (struct alpha_elf_link_hash_entry *h,
2616 struct bfd_link_info *info)
2618 bfd_boolean dynamic;
2619 struct alpha_elf_got_entry *gotent;
2620 unsigned long entries;
2622 if (h->root.root.type == bfd_link_hash_warning)
2623 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
2625 /* If we're using a plt for this symbol, then all of its relocations
2626 for its got entries go into .rela.plt. */
2627 if (h->root.needs_plt)
2628 return TRUE;
2630 /* If the symbol is dynamic, we'll need all the relocations in their
2631 natural form. If this is a shared object, and it has been forced
2632 local, we'll need the same number of RELATIVE relocations. */
2633 dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
2635 /* If the symbol is a hidden undefined weak, then we never have any
2636 relocations. Avoid the loop which may want to add RELATIVE relocs
2637 based on info->shared. */
2638 if (h->root.root.type == bfd_link_hash_undefweak && !dynamic)
2639 return TRUE;
2641 entries = 0;
2642 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2643 if (gotent->use_count > 0)
2644 entries += alpha_dynamic_entries_for_reloc (gotent->reloc_type,
2645 dynamic, info->shared);
2647 if (entries > 0)
2649 bfd *dynobj = elf_hash_table(info)->dynobj;
2650 asection *srel = bfd_get_section_by_name (dynobj, ".rela.got");
2651 BFD_ASSERT (srel != NULL);
2652 srel->size += sizeof (Elf64_External_Rela) * entries;
2655 return TRUE;
2658 /* Set the sizes of the dynamic relocation sections. */
2660 static bfd_boolean
2661 elf64_alpha_size_rela_got_section (struct bfd_link_info *info)
2663 unsigned long entries;
2664 bfd *i, *dynobj;
2665 asection *srel;
2667 /* Shared libraries often require RELATIVE relocs, and some relocs
2668 require attention for the main application as well. */
2670 entries = 0;
2671 for (i = alpha_elf_hash_table(info)->got_list;
2672 i ; i = alpha_elf_tdata(i)->got_link_next)
2674 bfd *j;
2676 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2678 struct alpha_elf_got_entry **local_got_entries, *gotent;
2679 int k, n;
2681 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2682 if (!local_got_entries)
2683 continue;
2685 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
2686 for (gotent = local_got_entries[k];
2687 gotent ; gotent = gotent->next)
2688 if (gotent->use_count > 0)
2689 entries += (alpha_dynamic_entries_for_reloc
2690 (gotent->reloc_type, 0, info->shared));
2694 dynobj = elf_hash_table(info)->dynobj;
2695 srel = bfd_get_section_by_name (dynobj, ".rela.got");
2696 if (!srel)
2698 BFD_ASSERT (entries == 0);
2699 return TRUE;
2701 srel->size = sizeof (Elf64_External_Rela) * entries;
2703 /* Now do the non-local symbols. */
2704 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2705 elf64_alpha_size_rela_got_1, info);
2707 return TRUE;
2710 /* Set the sizes of the dynamic sections. */
2712 static bfd_boolean
2713 elf64_alpha_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2714 struct bfd_link_info *info)
2716 bfd *dynobj;
2717 asection *s;
2718 bfd_boolean relplt;
2720 dynobj = elf_hash_table(info)->dynobj;
2721 BFD_ASSERT(dynobj != NULL);
2723 if (elf_hash_table (info)->dynamic_sections_created)
2725 /* Set the contents of the .interp section to the interpreter. */
2726 if (info->executable)
2728 s = bfd_get_section_by_name (dynobj, ".interp");
2729 BFD_ASSERT (s != NULL);
2730 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2731 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2734 /* Now that we've seen all of the input files, we can decide which
2735 symbols need dynamic relocation entries and which don't. We've
2736 collected information in check_relocs that we can now apply to
2737 size the dynamic relocation sections. */
2738 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2739 elf64_alpha_calc_dynrel_sizes, info);
2741 elf64_alpha_size_rela_got_section (info);
2742 elf64_alpha_size_plt_section (info);
2744 /* else we're not dynamic and by definition we don't need such things. */
2746 /* The check_relocs and adjust_dynamic_symbol entry points have
2747 determined the sizes of the various dynamic sections. Allocate
2748 memory for them. */
2749 relplt = FALSE;
2750 for (s = dynobj->sections; s != NULL; s = s->next)
2752 const char *name;
2754 if (!(s->flags & SEC_LINKER_CREATED))
2755 continue;
2757 /* It's OK to base decisions on the section name, because none
2758 of the dynobj section names depend upon the input files. */
2759 name = bfd_get_section_name (dynobj, s);
2761 if (strncmp (name, ".rela", 5) == 0)
2763 if (s->size != 0)
2765 if (strcmp (name, ".rela.plt") == 0)
2766 relplt = TRUE;
2768 /* We use the reloc_count field as a counter if we need
2769 to copy relocs into the output file. */
2770 s->reloc_count = 0;
2773 else if (strncmp (name, ".got", 4) != 0
2774 && strcmp (name, ".plt") != 0
2775 && strcmp (name, ".dynbss") != 0)
2777 /* It's not one of our dynamic sections, so don't allocate space. */
2778 continue;
2781 if (s->size == 0)
2783 /* If we don't need this section, strip it from the output file.
2784 This is to handle .rela.bss and .rela.plt. We must create it
2785 in create_dynamic_sections, because it must be created before
2786 the linker maps input sections to output sections. The
2787 linker does that before adjust_dynamic_symbol is called, and
2788 it is that function which decides whether anything needs to
2789 go into these sections. */
2790 s->flags |= SEC_EXCLUDE;
2792 else if ((s->flags & SEC_HAS_CONTENTS) != 0)
2794 /* Allocate memory for the section contents. */
2795 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2796 if (s->contents == NULL)
2797 return FALSE;
2801 if (elf_hash_table (info)->dynamic_sections_created)
2803 /* Add some entries to the .dynamic section. We fill in the
2804 values later, in elf64_alpha_finish_dynamic_sections, but we
2805 must add the entries now so that we get the correct size for
2806 the .dynamic section. The DT_DEBUG entry is filled in by the
2807 dynamic linker and used by the debugger. */
2808 #define add_dynamic_entry(TAG, VAL) \
2809 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2811 if (info->executable)
2813 if (!add_dynamic_entry (DT_DEBUG, 0))
2814 return FALSE;
2817 if (relplt)
2819 if (!add_dynamic_entry (DT_PLTGOT, 0)
2820 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2821 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2822 || !add_dynamic_entry (DT_JMPREL, 0))
2823 return FALSE;
2825 if (elf64_alpha_use_secureplt
2826 && !add_dynamic_entry (DT_ALPHA_PLTRO, 1))
2827 return FALSE;
2830 if (!add_dynamic_entry (DT_RELA, 0)
2831 || !add_dynamic_entry (DT_RELASZ, 0)
2832 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2833 return FALSE;
2835 if (info->flags & DF_TEXTREL)
2837 if (!add_dynamic_entry (DT_TEXTREL, 0))
2838 return FALSE;
2841 #undef add_dynamic_entry
2843 return TRUE;
2846 /* These functions do relaxation for Alpha ELF.
2848 Currently I'm only handling what I can do with existing compiler
2849 and assembler support, which means no instructions are removed,
2850 though some may be nopped. At this time GCC does not emit enough
2851 information to do all of the relaxing that is possible. It will
2852 take some not small amount of work for that to happen.
2854 There are a couple of interesting papers that I once read on this
2855 subject, that I cannot find references to at the moment, that
2856 related to Alpha in particular. They are by David Wall, then of
2857 DEC WRL. */
2859 struct alpha_relax_info
2861 bfd *abfd;
2862 asection *sec;
2863 bfd_byte *contents;
2864 Elf_Internal_Shdr *symtab_hdr;
2865 Elf_Internal_Rela *relocs, *relend;
2866 struct bfd_link_info *link_info;
2867 bfd_vma gp;
2868 bfd *gotobj;
2869 asection *tsec;
2870 struct alpha_elf_link_hash_entry *h;
2871 struct alpha_elf_got_entry **first_gotent;
2872 struct alpha_elf_got_entry *gotent;
2873 bfd_boolean changed_contents;
2874 bfd_boolean changed_relocs;
2875 unsigned char other;
2878 static Elf_Internal_Rela *
2879 elf64_alpha_find_reloc_at_ofs (Elf_Internal_Rela *rel,
2880 Elf_Internal_Rela *relend,
2881 bfd_vma offset, int type)
2883 while (rel < relend)
2885 if (rel->r_offset == offset
2886 && ELF64_R_TYPE (rel->r_info) == (unsigned int) type)
2887 return rel;
2888 ++rel;
2890 return NULL;
2893 static bfd_boolean
2894 elf64_alpha_relax_got_load (struct alpha_relax_info *info, bfd_vma symval,
2895 Elf_Internal_Rela *irel, unsigned long r_type)
2897 unsigned int insn;
2898 bfd_signed_vma disp;
2900 /* Get the instruction. */
2901 insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
2903 if (insn >> 26 != OP_LDQ)
2905 reloc_howto_type *howto = elf64_alpha_howto_table + r_type;
2906 ((*_bfd_error_handler)
2907 ("%B: %A+0x%lx: warning: %s relocation against unexpected insn",
2908 info->abfd, info->sec,
2909 (unsigned long) irel->r_offset, howto->name));
2910 return TRUE;
2913 /* Can't relax dynamic symbols. */
2914 if (alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
2915 return TRUE;
2917 /* Can't use local-exec relocations in shared libraries. */
2918 if (r_type == R_ALPHA_GOTTPREL && info->link_info->shared)
2919 return TRUE;
2921 if (r_type == R_ALPHA_LITERAL)
2923 /* Look for nice constant addresses. This includes the not-uncommon
2924 special case of 0 for undefweak symbols. */
2925 if ((info->h && info->h->root.root.type == bfd_link_hash_undefweak)
2926 || (!info->link_info->shared
2927 && (symval >= (bfd_vma)-0x8000 || symval < 0x8000)))
2929 disp = 0;
2930 insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
2931 insn |= (symval & 0xffff);
2932 r_type = R_ALPHA_NONE;
2934 else
2936 disp = symval - info->gp;
2937 insn = (OP_LDA << 26) | (insn & 0x03ff0000);
2938 r_type = R_ALPHA_GPREL16;
2941 else
2943 bfd_vma dtp_base, tp_base;
2945 BFD_ASSERT (elf_hash_table (info->link_info)->tls_sec != NULL);
2946 dtp_base = alpha_get_dtprel_base (info->link_info);
2947 tp_base = alpha_get_tprel_base (info->link_info);
2948 disp = symval - (r_type == R_ALPHA_GOTDTPREL ? dtp_base : tp_base);
2950 insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
2952 switch (r_type)
2954 case R_ALPHA_GOTDTPREL:
2955 r_type = R_ALPHA_DTPREL16;
2956 break;
2957 case R_ALPHA_GOTTPREL:
2958 r_type = R_ALPHA_TPREL16;
2959 break;
2960 default:
2961 BFD_ASSERT (0);
2962 return FALSE;
2966 if (disp < -0x8000 || disp >= 0x8000)
2967 return TRUE;
2969 bfd_put_32 (info->abfd, (bfd_vma) insn, info->contents + irel->r_offset);
2970 info->changed_contents = TRUE;
2972 /* Reduce the use count on this got entry by one, possibly
2973 eliminating it. */
2974 if (--info->gotent->use_count == 0)
2976 int sz = alpha_got_entry_size (r_type);
2977 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
2978 if (!info->h)
2979 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
2982 /* Smash the existing GOT relocation for its 16-bit immediate pair. */
2983 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), r_type);
2984 info->changed_relocs = TRUE;
2986 /* ??? Search forward through this basic block looking for insns
2987 that use the target register. Stop after an insn modifying the
2988 register is seen, or after a branch or call.
2990 Any such memory load insn may be substituted by a load directly
2991 off the GP. This allows the memory load insn to be issued before
2992 the calculated GP register would otherwise be ready.
2994 Any such jsr insn can be replaced by a bsr if it is in range.
2996 This would mean that we'd have to _add_ relocations, the pain of
2997 which gives one pause. */
2999 return TRUE;
3002 static bfd_vma
3003 elf64_alpha_relax_opt_call (struct alpha_relax_info *info, bfd_vma symval)
3005 /* If the function has the same gp, and we can identify that the
3006 function does not use its function pointer, we can eliminate the
3007 address load. */
3009 /* If the symbol is marked NOPV, we are being told the function never
3010 needs its procedure value. */
3011 if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_NOPV)
3012 return symval;
3014 /* If the symbol is marked STD_GP, we are being told the function does
3015 a normal ldgp in the first two words. */
3016 else if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_STD_GPLOAD)
3019 /* Otherwise, we may be able to identify a GP load in the first two
3020 words, which we can then skip. */
3021 else
3023 Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
3024 bfd_vma ofs;
3026 /* Load the relocations from the section that the target symbol is in. */
3027 if (info->sec == info->tsec)
3029 tsec_relocs = info->relocs;
3030 tsec_relend = info->relend;
3031 tsec_free = NULL;
3033 else
3035 tsec_relocs = (_bfd_elf_link_read_relocs
3036 (info->abfd, info->tsec, (PTR) NULL,
3037 (Elf_Internal_Rela *) NULL,
3038 info->link_info->keep_memory));
3039 if (tsec_relocs == NULL)
3040 return 0;
3041 tsec_relend = tsec_relocs + info->tsec->reloc_count;
3042 tsec_free = (info->link_info->keep_memory ? NULL : tsec_relocs);
3045 /* Recover the symbol's offset within the section. */
3046 ofs = (symval - info->tsec->output_section->vma
3047 - info->tsec->output_offset);
3049 /* Look for a GPDISP reloc. */
3050 gpdisp = (elf64_alpha_find_reloc_at_ofs
3051 (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
3053 if (!gpdisp || gpdisp->r_addend != 4)
3055 if (tsec_free)
3056 free (tsec_free);
3057 return 0;
3059 if (tsec_free)
3060 free (tsec_free);
3063 /* We've now determined that we can skip an initial gp load. Verify
3064 that the call and the target use the same gp. */
3065 if (info->link_info->hash->creator != info->tsec->owner->xvec
3066 || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
3067 return 0;
3069 return symval + 8;
3072 static bfd_boolean
3073 elf64_alpha_relax_with_lituse (struct alpha_relax_info *info,
3074 bfd_vma symval, Elf_Internal_Rela *irel)
3076 Elf_Internal_Rela *urel, *irelend = info->relend;
3077 int flags, count, i;
3078 bfd_signed_vma disp;
3079 bfd_boolean fits16;
3080 bfd_boolean fits32;
3081 bfd_boolean lit_reused = FALSE;
3082 bfd_boolean all_optimized = TRUE;
3083 unsigned int lit_insn;
3085 lit_insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
3086 if (lit_insn >> 26 != OP_LDQ)
3088 ((*_bfd_error_handler)
3089 ("%B: %A+0x%lx: warning: LITERAL relocation against unexpected insn",
3090 info->abfd, info->sec,
3091 (unsigned long) irel->r_offset));
3092 return TRUE;
3095 /* Can't relax dynamic symbols. */
3096 if (alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
3097 return TRUE;
3099 /* Summarize how this particular LITERAL is used. */
3100 for (urel = irel+1, flags = count = 0; urel < irelend; ++urel, ++count)
3102 if (ELF64_R_TYPE (urel->r_info) != R_ALPHA_LITUSE)
3103 break;
3104 if (urel->r_addend <= 6)
3105 flags |= 1 << urel->r_addend;
3108 /* A little preparation for the loop... */
3109 disp = symval - info->gp;
3111 for (urel = irel+1, i = 0; i < count; ++i, ++urel)
3113 unsigned int insn;
3114 int insn_disp;
3115 bfd_signed_vma xdisp;
3117 insn = bfd_get_32 (info->abfd, info->contents + urel->r_offset);
3119 switch (urel->r_addend)
3121 case LITUSE_ALPHA_ADDR:
3122 default:
3123 /* This type is really just a placeholder to note that all
3124 uses cannot be optimized, but to still allow some. */
3125 all_optimized = FALSE;
3126 break;
3128 case LITUSE_ALPHA_BASE:
3129 /* We can always optimize 16-bit displacements. */
3131 /* Extract the displacement from the instruction, sign-extending
3132 it if necessary, then test whether it is within 16 or 32 bits
3133 displacement from GP. */
3134 insn_disp = ((insn & 0xffff) ^ 0x8000) - 0x8000;
3136 xdisp = disp + insn_disp;
3137 fits16 = (xdisp >= - (bfd_signed_vma) 0x8000 && xdisp < 0x8000);
3138 fits32 = (xdisp >= - (bfd_signed_vma) 0x80000000
3139 && xdisp < 0x7fff8000);
3141 if (fits16)
3143 /* Take the op code and dest from this insn, take the base
3144 register from the literal insn. Leave the offset alone. */
3145 insn = (insn & 0xffe0ffff) | (lit_insn & 0x001f0000);
3146 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3147 R_ALPHA_GPREL16);
3148 urel->r_addend = irel->r_addend;
3149 info->changed_relocs = TRUE;
3151 bfd_put_32 (info->abfd, (bfd_vma) insn,
3152 info->contents + urel->r_offset);
3153 info->changed_contents = TRUE;
3156 /* If all mem+byte, we can optimize 32-bit mem displacements. */
3157 else if (fits32 && !(flags & ~6))
3159 /* FIXME: sanity check that lit insn Ra is mem insn Rb. */
3161 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3162 R_ALPHA_GPRELHIGH);
3163 lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
3164 bfd_put_32 (info->abfd, (bfd_vma) lit_insn,
3165 info->contents + irel->r_offset);
3166 lit_reused = TRUE;
3167 info->changed_contents = TRUE;
3169 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3170 R_ALPHA_GPRELLOW);
3171 urel->r_addend = irel->r_addend;
3172 info->changed_relocs = TRUE;
3174 else
3175 all_optimized = FALSE;
3176 break;
3178 case LITUSE_ALPHA_BYTOFF:
3179 /* We can always optimize byte instructions. */
3181 /* FIXME: sanity check the insn for byte op. Check that the
3182 literal dest reg is indeed Rb in the byte insn. */
3184 insn &= ~ (unsigned) 0x001ff000;
3185 insn |= ((symval & 7) << 13) | 0x1000;
3187 urel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3188 urel->r_addend = 0;
3189 info->changed_relocs = TRUE;
3191 bfd_put_32 (info->abfd, (bfd_vma) insn,
3192 info->contents + urel->r_offset);
3193 info->changed_contents = TRUE;
3194 break;
3196 case LITUSE_ALPHA_JSR:
3197 case LITUSE_ALPHA_TLSGD:
3198 case LITUSE_ALPHA_TLSLDM:
3199 case LITUSE_ALPHA_JSRDIRECT:
3201 bfd_vma optdest, org;
3202 bfd_signed_vma odisp;
3204 /* For undefined weak symbols, we're mostly interested in getting
3205 rid of the got entry whenever possible, so optimize this to a
3206 use of the zero register. */
3207 if (info->h && info->h->root.root.type == bfd_link_hash_undefweak)
3209 insn |= 31 << 16;
3210 bfd_put_32 (info->abfd, (bfd_vma) insn,
3211 info->contents + urel->r_offset);
3213 info->changed_contents = TRUE;
3214 break;
3217 /* If not zero, place to jump without needing pv. */
3218 optdest = elf64_alpha_relax_opt_call (info, symval);
3219 org = (info->sec->output_section->vma
3220 + info->sec->output_offset
3221 + urel->r_offset + 4);
3222 odisp = (optdest ? optdest : symval) - org;
3224 if (odisp >= -0x400000 && odisp < 0x400000)
3226 Elf_Internal_Rela *xrel;
3228 /* Preserve branch prediction call stack when possible. */
3229 if ((insn & INSN_JSR_MASK) == INSN_JSR)
3230 insn = (OP_BSR << 26) | (insn & 0x03e00000);
3231 else
3232 insn = (OP_BR << 26) | (insn & 0x03e00000);
3234 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
3235 R_ALPHA_BRADDR);
3236 urel->r_addend = irel->r_addend;
3238 if (optdest)
3239 urel->r_addend += optdest - symval;
3240 else
3241 all_optimized = FALSE;
3243 bfd_put_32 (info->abfd, (bfd_vma) insn,
3244 info->contents + urel->r_offset);
3246 /* Kill any HINT reloc that might exist for this insn. */
3247 xrel = (elf64_alpha_find_reloc_at_ofs
3248 (info->relocs, info->relend, urel->r_offset,
3249 R_ALPHA_HINT));
3250 if (xrel)
3251 xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3253 info->changed_contents = TRUE;
3254 info->changed_relocs = TRUE;
3256 else
3257 all_optimized = FALSE;
3259 /* Even if the target is not in range for a direct branch,
3260 if we share a GP, we can eliminate the gp reload. */
3261 if (optdest)
3263 Elf_Internal_Rela *gpdisp
3264 = (elf64_alpha_find_reloc_at_ofs
3265 (info->relocs, irelend, urel->r_offset + 4,
3266 R_ALPHA_GPDISP));
3267 if (gpdisp)
3269 bfd_byte *p_ldah = info->contents + gpdisp->r_offset;
3270 bfd_byte *p_lda = p_ldah + gpdisp->r_addend;
3271 unsigned int ldah = bfd_get_32 (info->abfd, p_ldah);
3272 unsigned int lda = bfd_get_32 (info->abfd, p_lda);
3274 /* Verify that the instruction is "ldah $29,0($26)".
3275 Consider a function that ends in a noreturn call,
3276 and that the next function begins with an ldgp,
3277 and that by accident there is no padding between.
3278 In that case the insn would use $27 as the base. */
3279 if (ldah == 0x27ba0000 && lda == 0x23bd0000)
3281 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, p_ldah);
3282 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, p_lda);
3284 gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3285 info->changed_contents = TRUE;
3286 info->changed_relocs = TRUE;
3291 break;
3295 /* If all cases were optimized, we can reduce the use count on this
3296 got entry by one, possibly eliminating it. */
3297 if (all_optimized)
3299 if (--info->gotent->use_count == 0)
3301 int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
3302 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
3303 if (!info->h)
3304 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
3307 /* If the literal instruction is no longer needed (it may have been
3308 reused. We can eliminate it. */
3309 /* ??? For now, I don't want to deal with compacting the section,
3310 so just nop it out. */
3311 if (!lit_reused)
3313 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3314 info->changed_relocs = TRUE;
3316 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP,
3317 info->contents + irel->r_offset);
3318 info->changed_contents = TRUE;
3321 return TRUE;
3323 else
3324 return elf64_alpha_relax_got_load (info, symval, irel, R_ALPHA_LITERAL);
3327 static bfd_boolean
3328 elf64_alpha_relax_tls_get_addr (struct alpha_relax_info *info, bfd_vma symval,
3329 Elf_Internal_Rela *irel, bfd_boolean is_gd)
3331 bfd_byte *pos[5];
3332 unsigned int insn;
3333 Elf_Internal_Rela *gpdisp, *hint;
3334 bfd_boolean dynamic, use_gottprel, pos1_unusable;
3335 unsigned long new_symndx;
3337 dynamic = alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info);
3339 /* If a TLS symbol is accessed using IE at least once, there is no point
3340 to use dynamic model for it. */
3341 if (is_gd && info->h && (info->h->flags & ALPHA_ELF_LINK_HASH_TLS_IE))
3344 /* If the symbol is local, and we've already committed to DF_STATIC_TLS,
3345 then we might as well relax to IE. */
3346 else if (info->link_info->shared && !dynamic
3347 && (info->link_info->flags & DF_STATIC_TLS))
3350 /* Otherwise we must be building an executable to do anything. */
3351 else if (info->link_info->shared)
3352 return TRUE;
3354 /* The TLSGD/TLSLDM relocation must be followed by a LITERAL and
3355 the matching LITUSE_TLS relocations. */
3356 if (irel + 2 >= info->relend)
3357 return TRUE;
3358 if (ELF64_R_TYPE (irel[1].r_info) != R_ALPHA_LITERAL
3359 || ELF64_R_TYPE (irel[2].r_info) != R_ALPHA_LITUSE
3360 || irel[2].r_addend != (is_gd ? LITUSE_ALPHA_TLSGD : LITUSE_ALPHA_TLSLDM))
3361 return TRUE;
3363 /* There must be a GPDISP relocation positioned immediately after the
3364 LITUSE relocation. */
3365 gpdisp = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
3366 irel[2].r_offset + 4, R_ALPHA_GPDISP);
3367 if (!gpdisp)
3368 return TRUE;
3370 pos[0] = info->contents + irel[0].r_offset;
3371 pos[1] = info->contents + irel[1].r_offset;
3372 pos[2] = info->contents + irel[2].r_offset;
3373 pos[3] = info->contents + gpdisp->r_offset;
3374 pos[4] = pos[3] + gpdisp->r_addend;
3375 pos1_unusable = FALSE;
3377 /* Generally, the positions are not allowed to be out of order, lest the
3378 modified insn sequence have different register lifetimes. We can make
3379 an exception when pos 1 is adjacent to pos 0. */
3380 if (pos[1] + 4 == pos[0])
3382 bfd_byte *tmp = pos[0];
3383 pos[0] = pos[1];
3384 pos[1] = tmp;
3386 else if (pos[1] < pos[0])
3387 pos1_unusable = TRUE;
3388 if (pos[1] >= pos[2] || pos[2] >= pos[3])
3389 return TRUE;
3391 /* Reduce the use count on the LITERAL relocation. Do this before we
3392 smash the symndx when we adjust the relocations below. */
3394 struct alpha_elf_got_entry *lit_gotent;
3395 struct alpha_elf_link_hash_entry *lit_h;
3396 unsigned long indx;
3398 BFD_ASSERT (ELF64_R_SYM (irel[1].r_info) >= info->symtab_hdr->sh_info);
3399 indx = ELF64_R_SYM (irel[1].r_info) - info->symtab_hdr->sh_info;
3400 lit_h = alpha_elf_sym_hashes (info->abfd)[indx];
3402 while (lit_h->root.root.type == bfd_link_hash_indirect
3403 || lit_h->root.root.type == bfd_link_hash_warning)
3404 lit_h = (struct alpha_elf_link_hash_entry *) lit_h->root.root.u.i.link;
3406 for (lit_gotent = lit_h->got_entries; lit_gotent ;
3407 lit_gotent = lit_gotent->next)
3408 if (lit_gotent->gotobj == info->gotobj
3409 && lit_gotent->reloc_type == R_ALPHA_LITERAL
3410 && lit_gotent->addend == irel[1].r_addend)
3411 break;
3412 BFD_ASSERT (lit_gotent);
3414 if (--lit_gotent->use_count == 0)
3416 int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
3417 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
3421 /* Change
3423 lda $16,x($gp) !tlsgd!1
3424 ldq $27,__tls_get_addr($gp) !literal!1
3425 jsr $26,($27),__tls_get_addr !lituse_tlsgd!1
3426 ldah $29,0($26) !gpdisp!2
3427 lda $29,0($29) !gpdisp!2
3429 ldq $16,x($gp) !gottprel
3430 unop
3431 call_pal rduniq
3432 addq $16,$0,$0
3433 unop
3434 or the first pair to
3435 lda $16,x($gp) !tprel
3436 unop
3438 ldah $16,x($gp) !tprelhi
3439 lda $16,x($16) !tprello
3441 as appropriate. */
3443 use_gottprel = FALSE;
3444 new_symndx = is_gd ? ELF64_R_SYM (irel->r_info) : 0;
3445 switch (!dynamic && !info->link_info->shared)
3447 case 1:
3449 bfd_vma tp_base;
3450 bfd_signed_vma disp;
3452 BFD_ASSERT (elf_hash_table (info->link_info)->tls_sec != NULL);
3453 tp_base = alpha_get_tprel_base (info->link_info);
3454 disp = symval - tp_base;
3456 if (disp >= -0x8000 && disp < 0x8000)
3458 insn = (OP_LDA << 26) | (16 << 21) | (31 << 16);
3459 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
3460 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
3462 irel[0].r_offset = pos[0] - info->contents;
3463 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPREL16);
3464 irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3465 break;
3467 else if (disp >= -(bfd_signed_vma) 0x80000000
3468 && disp < (bfd_signed_vma) 0x7fff8000
3469 && !pos1_unusable)
3471 insn = (OP_LDAH << 26) | (16 << 21) | (31 << 16);
3472 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
3473 insn = (OP_LDA << 26) | (16 << 21) | (16 << 16);
3474 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[1]);
3476 irel[0].r_offset = pos[0] - info->contents;
3477 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELHI);
3478 irel[1].r_offset = pos[1] - info->contents;
3479 irel[1].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELLO);
3480 break;
3483 /* FALLTHRU */
3485 default:
3486 use_gottprel = TRUE;
3488 insn = (OP_LDQ << 26) | (16 << 21) | (29 << 16);
3489 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
3490 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
3492 irel[0].r_offset = pos[0] - info->contents;
3493 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_GOTTPREL);
3494 irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3495 break;
3498 bfd_put_32 (info->abfd, (bfd_vma) INSN_RDUNIQ, pos[2]);
3500 insn = INSN_ADDQ | (16 << 21) | (0 << 16) | (0 << 0);
3501 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[3]);
3503 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[4]);
3505 irel[2].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3506 gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3508 hint = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
3509 irel[2].r_offset, R_ALPHA_HINT);
3510 if (hint)
3511 hint->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
3513 info->changed_contents = TRUE;
3514 info->changed_relocs = TRUE;
3516 /* Reduce the use count on the TLSGD/TLSLDM relocation. */
3517 if (--info->gotent->use_count == 0)
3519 int sz = alpha_got_entry_size (info->gotent->reloc_type);
3520 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
3521 if (!info->h)
3522 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
3525 /* If we've switched to a GOTTPREL relocation, increment the reference
3526 count on that got entry. */
3527 if (use_gottprel)
3529 struct alpha_elf_got_entry *tprel_gotent;
3531 for (tprel_gotent = *info->first_gotent; tprel_gotent ;
3532 tprel_gotent = tprel_gotent->next)
3533 if (tprel_gotent->gotobj == info->gotobj
3534 && tprel_gotent->reloc_type == R_ALPHA_GOTTPREL
3535 && tprel_gotent->addend == irel->r_addend)
3536 break;
3537 if (tprel_gotent)
3538 tprel_gotent->use_count++;
3539 else
3541 if (info->gotent->use_count == 0)
3542 tprel_gotent = info->gotent;
3543 else
3545 tprel_gotent = (struct alpha_elf_got_entry *)
3546 bfd_alloc (info->abfd, sizeof (struct alpha_elf_got_entry));
3547 if (!tprel_gotent)
3548 return FALSE;
3550 tprel_gotent->next = *info->first_gotent;
3551 *info->first_gotent = tprel_gotent;
3553 tprel_gotent->gotobj = info->gotobj;
3554 tprel_gotent->addend = irel->r_addend;
3555 tprel_gotent->got_offset = -1;
3556 tprel_gotent->reloc_done = 0;
3557 tprel_gotent->reloc_xlated = 0;
3560 tprel_gotent->use_count = 1;
3561 tprel_gotent->reloc_type = R_ALPHA_GOTTPREL;
3565 return TRUE;
3568 static bfd_boolean
3569 elf64_alpha_relax_section (bfd *abfd, asection *sec,
3570 struct bfd_link_info *link_info, bfd_boolean *again)
3572 Elf_Internal_Shdr *symtab_hdr;
3573 Elf_Internal_Rela *internal_relocs;
3574 Elf_Internal_Rela *irel, *irelend;
3575 Elf_Internal_Sym *isymbuf = NULL;
3576 struct alpha_elf_got_entry **local_got_entries;
3577 struct alpha_relax_info info;
3579 /* We are not currently changing any sizes, so only one pass. */
3580 *again = FALSE;
3582 if (link_info->relocatable
3583 || ((sec->flags & (SEC_CODE | SEC_RELOC | SEC_ALLOC))
3584 != (SEC_CODE | SEC_RELOC | SEC_ALLOC))
3585 || sec->reloc_count == 0)
3586 return TRUE;
3588 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3589 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
3591 /* Load the relocations for this section. */
3592 internal_relocs = (_bfd_elf_link_read_relocs
3593 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3594 link_info->keep_memory));
3595 if (internal_relocs == NULL)
3596 return FALSE;
3598 memset(&info, 0, sizeof (info));
3599 info.abfd = abfd;
3600 info.sec = sec;
3601 info.link_info = link_info;
3602 info.symtab_hdr = symtab_hdr;
3603 info.relocs = internal_relocs;
3604 info.relend = irelend = internal_relocs + sec->reloc_count;
3606 /* Find the GP for this object. Do not store the result back via
3607 _bfd_set_gp_value, since this could change again before final. */
3608 info.gotobj = alpha_elf_tdata (abfd)->gotobj;
3609 if (info.gotobj)
3611 asection *sgot = alpha_elf_tdata (info.gotobj)->got;
3612 info.gp = (sgot->output_section->vma
3613 + sgot->output_offset
3614 + 0x8000);
3617 /* Get the section contents. */
3618 if (elf_section_data (sec)->this_hdr.contents != NULL)
3619 info.contents = elf_section_data (sec)->this_hdr.contents;
3620 else
3622 if (!bfd_malloc_and_get_section (abfd, sec, &info.contents))
3623 goto error_return;
3626 for (irel = internal_relocs; irel < irelend; irel++)
3628 bfd_vma symval;
3629 struct alpha_elf_got_entry *gotent;
3630 unsigned long r_type = ELF64_R_TYPE (irel->r_info);
3631 unsigned long r_symndx = ELF64_R_SYM (irel->r_info);
3633 /* Early exit for unhandled or unrelaxable relocations. */
3634 switch (r_type)
3636 case R_ALPHA_LITERAL:
3637 case R_ALPHA_GPRELHIGH:
3638 case R_ALPHA_GPRELLOW:
3639 case R_ALPHA_GOTDTPREL:
3640 case R_ALPHA_GOTTPREL:
3641 case R_ALPHA_TLSGD:
3642 break;
3644 case R_ALPHA_TLSLDM:
3645 /* The symbol for a TLSLDM reloc is ignored. Collapse the
3646 reloc to the 0 symbol so that they all match. */
3647 r_symndx = 0;
3648 break;
3650 default:
3651 continue;
3654 /* Get the value of the symbol referred to by the reloc. */
3655 if (r_symndx < symtab_hdr->sh_info)
3657 /* A local symbol. */
3658 Elf_Internal_Sym *isym;
3660 /* Read this BFD's local symbols. */
3661 if (isymbuf == NULL)
3663 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3664 if (isymbuf == NULL)
3665 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3666 symtab_hdr->sh_info, 0,
3667 NULL, NULL, NULL);
3668 if (isymbuf == NULL)
3669 goto error_return;
3672 isym = isymbuf + r_symndx;
3674 /* Given the symbol for a TLSLDM reloc is ignored, this also
3675 means forcing the symbol value to the tp base. */
3676 if (r_type == R_ALPHA_TLSLDM)
3678 info.tsec = bfd_abs_section_ptr;
3679 symval = alpha_get_tprel_base (info.link_info);
3681 else
3683 symval = isym->st_value;
3684 if (isym->st_shndx == SHN_UNDEF)
3685 continue;
3686 else if (isym->st_shndx == SHN_ABS)
3687 info.tsec = bfd_abs_section_ptr;
3688 else if (isym->st_shndx == SHN_COMMON)
3689 info.tsec = bfd_com_section_ptr;
3690 else
3691 info.tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3694 info.h = NULL;
3695 info.other = isym->st_other;
3696 if (local_got_entries)
3697 info.first_gotent = &local_got_entries[r_symndx];
3698 else
3700 info.first_gotent = &info.gotent;
3701 info.gotent = NULL;
3704 else
3706 unsigned long indx;
3707 struct alpha_elf_link_hash_entry *h;
3709 indx = r_symndx - symtab_hdr->sh_info;
3710 h = alpha_elf_sym_hashes (abfd)[indx];
3711 BFD_ASSERT (h != NULL);
3713 while (h->root.root.type == bfd_link_hash_indirect
3714 || h->root.root.type == bfd_link_hash_warning)
3715 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3717 /* If the symbol is undefined, we can't do anything with it. */
3718 if (h->root.root.type == bfd_link_hash_undefined)
3719 continue;
3721 /* If the symbol isn't defined in the current module,
3722 again we can't do anything. */
3723 if (h->root.root.type == bfd_link_hash_undefweak)
3725 info.tsec = bfd_abs_section_ptr;
3726 symval = 0;
3728 else if (!h->root.def_regular)
3730 /* Except for TLSGD relocs, which can sometimes be
3731 relaxed to GOTTPREL relocs. */
3732 if (r_type != R_ALPHA_TLSGD)
3733 continue;
3734 info.tsec = bfd_abs_section_ptr;
3735 symval = 0;
3737 else
3739 info.tsec = h->root.root.u.def.section;
3740 symval = h->root.root.u.def.value;
3743 info.h = h;
3744 info.other = h->root.other;
3745 info.first_gotent = &h->got_entries;
3748 /* Search for the got entry to be used by this relocation. */
3749 for (gotent = *info.first_gotent; gotent ; gotent = gotent->next)
3750 if (gotent->gotobj == info.gotobj
3751 && gotent->reloc_type == r_type
3752 && gotent->addend == irel->r_addend)
3753 break;
3754 info.gotent = gotent;
3756 symval += info.tsec->output_section->vma + info.tsec->output_offset;
3757 symval += irel->r_addend;
3759 switch (r_type)
3761 case R_ALPHA_LITERAL:
3762 BFD_ASSERT(info.gotent != NULL);
3764 /* If there exist LITUSE relocations immediately following, this
3765 opens up all sorts of interesting optimizations, because we
3766 now know every location that this address load is used. */
3767 if (irel+1 < irelend
3768 && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
3770 if (!elf64_alpha_relax_with_lituse (&info, symval, irel))
3771 goto error_return;
3773 else
3775 if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
3776 goto error_return;
3778 break;
3780 case R_ALPHA_GOTDTPREL:
3781 case R_ALPHA_GOTTPREL:
3782 BFD_ASSERT(info.gotent != NULL);
3783 if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
3784 goto error_return;
3785 break;
3787 case R_ALPHA_TLSGD:
3788 case R_ALPHA_TLSLDM:
3789 BFD_ASSERT(info.gotent != NULL);
3790 if (!elf64_alpha_relax_tls_get_addr (&info, symval, irel,
3791 r_type == R_ALPHA_TLSGD))
3792 goto error_return;
3793 break;
3797 if (!elf64_alpha_size_plt_section (link_info))
3798 return FALSE;
3799 if (!elf64_alpha_size_got_sections (link_info))
3800 return FALSE;
3801 if (!elf64_alpha_size_rela_got_section (link_info))
3802 return FALSE;
3804 if (isymbuf != NULL
3805 && symtab_hdr->contents != (unsigned char *) isymbuf)
3807 if (!link_info->keep_memory)
3808 free (isymbuf);
3809 else
3811 /* Cache the symbols for elf_link_input_bfd. */
3812 symtab_hdr->contents = (unsigned char *) isymbuf;
3816 if (info.contents != NULL
3817 && elf_section_data (sec)->this_hdr.contents != info.contents)
3819 if (!info.changed_contents && !link_info->keep_memory)
3820 free (info.contents);
3821 else
3823 /* Cache the section contents for elf_link_input_bfd. */
3824 elf_section_data (sec)->this_hdr.contents = info.contents;
3828 if (elf_section_data (sec)->relocs != internal_relocs)
3830 if (!info.changed_relocs)
3831 free (internal_relocs);
3832 else
3833 elf_section_data (sec)->relocs = internal_relocs;
3836 *again = info.changed_contents || info.changed_relocs;
3838 return TRUE;
3840 error_return:
3841 if (isymbuf != NULL
3842 && symtab_hdr->contents != (unsigned char *) isymbuf)
3843 free (isymbuf);
3844 if (info.contents != NULL
3845 && elf_section_data (sec)->this_hdr.contents != info.contents)
3846 free (info.contents);
3847 if (internal_relocs != NULL
3848 && elf_section_data (sec)->relocs != internal_relocs)
3849 free (internal_relocs);
3850 return FALSE;
3853 /* Emit a dynamic relocation for (DYNINDX, RTYPE, ADDEND) at (SEC, OFFSET)
3854 into the next available slot in SREL. */
3856 static void
3857 elf64_alpha_emit_dynrel (bfd *abfd, struct bfd_link_info *info,
3858 asection *sec, asection *srel, bfd_vma offset,
3859 long dynindx, long rtype, bfd_vma addend)
3861 Elf_Internal_Rela outrel;
3862 bfd_byte *loc;
3864 BFD_ASSERT (srel != NULL);
3866 outrel.r_info = ELF64_R_INFO (dynindx, rtype);
3867 outrel.r_addend = addend;
3869 offset = _bfd_elf_section_offset (abfd, info, sec, offset);
3870 if ((offset | 1) != (bfd_vma) -1)
3871 outrel.r_offset = sec->output_section->vma + sec->output_offset + offset;
3872 else
3873 memset (&outrel, 0, sizeof (outrel));
3875 loc = srel->contents;
3876 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
3877 bfd_elf64_swap_reloca_out (abfd, &outrel, loc);
3878 BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count <= srel->size);
3881 /* Relocate an Alpha ELF section for a relocatable link.
3883 We don't have to change anything unless the reloc is against a section
3884 symbol, in which case we have to adjust according to where the section
3885 symbol winds up in the output section. */
3887 static bfd_boolean
3888 elf64_alpha_relocate_section_r (bfd *output_bfd ATTRIBUTE_UNUSED,
3889 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3890 bfd *input_bfd, asection *input_section,
3891 bfd_byte *contents ATTRIBUTE_UNUSED,
3892 Elf_Internal_Rela *relocs,
3893 Elf_Internal_Sym *local_syms,
3894 asection **local_sections)
3896 unsigned long symtab_hdr_sh_info;
3897 Elf_Internal_Rela *rel;
3898 Elf_Internal_Rela *relend;
3899 bfd_boolean ret_val = TRUE;
3901 symtab_hdr_sh_info = elf_tdata (input_bfd)->symtab_hdr.sh_info;
3903 relend = relocs + input_section->reloc_count;
3904 for (rel = relocs; rel < relend; rel++)
3906 unsigned long r_symndx;
3907 Elf_Internal_Sym *sym;
3908 asection *sec;
3909 unsigned long r_type;
3911 r_type = ELF64_R_TYPE(rel->r_info);
3912 if (r_type >= R_ALPHA_max)
3914 (*_bfd_error_handler)
3915 (_("%B: unknown relocation type %d"),
3916 input_bfd, (int) r_type);
3917 bfd_set_error (bfd_error_bad_value);
3918 ret_val = FALSE;
3919 continue;
3922 r_symndx = ELF64_R_SYM(rel->r_info);
3924 /* The symbol associated with GPDISP and LITUSE is
3925 immaterial. Only the addend is significant. */
3926 if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
3927 continue;
3929 if (r_symndx < symtab_hdr_sh_info)
3931 sym = local_syms + r_symndx;
3932 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
3934 sec = local_sections[r_symndx];
3935 rel->r_addend += sec->output_offset + sym->st_value;
3940 return ret_val;
3943 /* Relocate an Alpha ELF section. */
3945 static bfd_boolean
3946 elf64_alpha_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3947 bfd *input_bfd, asection *input_section,
3948 bfd_byte *contents, Elf_Internal_Rela *relocs,
3949 Elf_Internal_Sym *local_syms,
3950 asection **local_sections)
3952 Elf_Internal_Shdr *symtab_hdr;
3953 Elf_Internal_Rela *rel;
3954 Elf_Internal_Rela *relend;
3955 asection *sgot, *srel, *srelgot;
3956 bfd *dynobj, *gotobj;
3957 bfd_vma gp, tp_base, dtp_base;
3958 struct alpha_elf_got_entry **local_got_entries;
3959 bfd_boolean ret_val;
3961 /* Handle relocatable links with a smaller loop. */
3962 if (info->relocatable)
3963 return elf64_alpha_relocate_section_r (output_bfd, info, input_bfd,
3964 input_section, contents, relocs,
3965 local_syms, local_sections);
3967 /* This is a final link. */
3969 ret_val = TRUE;
3971 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3973 dynobj = elf_hash_table (info)->dynobj;
3974 if (dynobj)
3975 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3976 else
3977 srelgot = NULL;
3979 if (input_section->flags & SEC_ALLOC)
3981 const char *section_name;
3982 section_name = (bfd_elf_string_from_elf_section
3983 (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
3984 elf_section_data(input_section)->rel_hdr.sh_name));
3985 BFD_ASSERT(section_name != NULL);
3986 srel = bfd_get_section_by_name (dynobj, section_name);
3988 else
3989 srel = NULL;
3991 /* Find the gp value for this input bfd. */
3992 gotobj = alpha_elf_tdata (input_bfd)->gotobj;
3993 if (gotobj)
3995 sgot = alpha_elf_tdata (gotobj)->got;
3996 gp = _bfd_get_gp_value (gotobj);
3997 if (gp == 0)
3999 gp = (sgot->output_section->vma
4000 + sgot->output_offset
4001 + 0x8000);
4002 _bfd_set_gp_value (gotobj, gp);
4005 else
4007 sgot = NULL;
4008 gp = 0;
4011 local_got_entries = alpha_elf_tdata(input_bfd)->local_got_entries;
4013 if (elf_hash_table (info)->tls_sec != NULL)
4015 dtp_base = alpha_get_dtprel_base (info);
4016 tp_base = alpha_get_tprel_base (info);
4018 else
4019 dtp_base = tp_base = 0;
4021 relend = relocs + input_section->reloc_count;
4022 for (rel = relocs; rel < relend; rel++)
4024 struct alpha_elf_link_hash_entry *h = NULL;
4025 struct alpha_elf_got_entry *gotent;
4026 bfd_reloc_status_type r;
4027 reloc_howto_type *howto;
4028 unsigned long r_symndx;
4029 Elf_Internal_Sym *sym = NULL;
4030 asection *sec = NULL;
4031 bfd_vma value;
4032 bfd_vma addend;
4033 bfd_boolean dynamic_symbol_p;
4034 bfd_boolean undef_weak_ref = FALSE;
4035 unsigned long r_type;
4037 r_type = ELF64_R_TYPE(rel->r_info);
4038 if (r_type >= R_ALPHA_max)
4040 (*_bfd_error_handler)
4041 (_("%B: unknown relocation type %d"),
4042 input_bfd, (int) r_type);
4043 bfd_set_error (bfd_error_bad_value);
4044 ret_val = FALSE;
4045 continue;
4048 howto = elf64_alpha_howto_table + r_type;
4049 r_symndx = ELF64_R_SYM(rel->r_info);
4051 /* The symbol for a TLSLDM reloc is ignored. Collapse the
4052 reloc to the 0 symbol so that they all match. */
4053 if (r_type == R_ALPHA_TLSLDM)
4054 r_symndx = 0;
4056 if (r_symndx < symtab_hdr->sh_info)
4058 asection *msec;
4059 sym = local_syms + r_symndx;
4060 sec = local_sections[r_symndx];
4061 msec = sec;
4062 value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
4064 /* If this is a tp-relative relocation against sym 0,
4065 this is hackery from relax_section. Force the value to
4066 be the tls module base. */
4067 if (r_symndx == 0
4068 && (r_type == R_ALPHA_TLSLDM
4069 || r_type == R_ALPHA_GOTTPREL
4070 || r_type == R_ALPHA_TPREL64
4071 || r_type == R_ALPHA_TPRELHI
4072 || r_type == R_ALPHA_TPRELLO
4073 || r_type == R_ALPHA_TPREL16))
4074 value = dtp_base;
4076 if (local_got_entries)
4077 gotent = local_got_entries[r_symndx];
4078 else
4079 gotent = NULL;
4081 /* Need to adjust local GOT entries' addends for SEC_MERGE
4082 unless it has been done already. */
4083 if ((sec->flags & SEC_MERGE)
4084 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
4085 && sec->sec_info_type == ELF_INFO_TYPE_MERGE
4086 && gotent
4087 && !gotent->reloc_xlated)
4089 struct alpha_elf_got_entry *ent;
4091 for (ent = gotent; ent; ent = ent->next)
4093 ent->reloc_xlated = 1;
4094 if (ent->use_count == 0)
4095 continue;
4096 msec = sec;
4097 ent->addend =
4098 _bfd_merged_section_offset (output_bfd, &msec,
4099 elf_section_data (sec)->
4100 sec_info,
4101 sym->st_value + ent->addend);
4102 ent->addend -= sym->st_value;
4103 ent->addend += msec->output_section->vma
4104 + msec->output_offset
4105 - sec->output_section->vma
4106 - sec->output_offset;
4110 dynamic_symbol_p = FALSE;
4112 else
4114 bfd_boolean warned;
4115 bfd_boolean unresolved_reloc;
4116 struct elf_link_hash_entry *hh;
4117 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
4119 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4120 r_symndx, symtab_hdr, sym_hashes,
4121 hh, sec, value,
4122 unresolved_reloc, warned);
4124 if (warned)
4125 continue;
4127 if (value == 0
4128 && ! unresolved_reloc
4129 && hh->root.type == bfd_link_hash_undefweak)
4130 undef_weak_ref = TRUE;
4132 h = (struct alpha_elf_link_hash_entry *) hh;
4133 dynamic_symbol_p = alpha_elf_dynamic_symbol_p (&h->root, info);
4134 gotent = h->got_entries;
4137 addend = rel->r_addend;
4138 value += addend;
4140 /* Search for the proper got entry. */
4141 for (; gotent ; gotent = gotent->next)
4142 if (gotent->gotobj == gotobj
4143 && gotent->reloc_type == r_type
4144 && gotent->addend == addend)
4145 break;
4147 switch (r_type)
4149 case R_ALPHA_GPDISP:
4151 bfd_byte *p_ldah, *p_lda;
4153 BFD_ASSERT(gp != 0);
4155 value = (input_section->output_section->vma
4156 + input_section->output_offset
4157 + rel->r_offset);
4159 p_ldah = contents + rel->r_offset;
4160 p_lda = p_ldah + rel->r_addend;
4162 r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - value,
4163 p_ldah, p_lda);
4165 break;
4167 case R_ALPHA_LITERAL:
4168 BFD_ASSERT(sgot != NULL);
4169 BFD_ASSERT(gp != 0);
4170 BFD_ASSERT(gotent != NULL);
4171 BFD_ASSERT(gotent->use_count >= 1);
4173 if (!gotent->reloc_done)
4175 gotent->reloc_done = 1;
4177 bfd_put_64 (output_bfd, value,
4178 sgot->contents + gotent->got_offset);
4180 /* If the symbol has been forced local, output a
4181 RELATIVE reloc, otherwise it will be handled in
4182 finish_dynamic_symbol. */
4183 if (info->shared && !dynamic_symbol_p && !undef_weak_ref)
4184 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4185 gotent->got_offset, 0,
4186 R_ALPHA_RELATIVE, value);
4189 value = (sgot->output_section->vma
4190 + sgot->output_offset
4191 + gotent->got_offset);
4192 value -= gp;
4193 goto default_reloc;
4195 case R_ALPHA_GPREL32:
4196 /* If the target section was a removed linkonce section,
4197 r_symndx will be zero. In this case, assume that the
4198 switch will not be used, so don't fill it in. If we
4199 do nothing here, we'll get relocation truncated messages,
4200 due to the placement of the application above 4GB. */
4201 if (r_symndx == 0)
4203 r = bfd_reloc_ok;
4204 break;
4206 /* FALLTHRU */
4208 case R_ALPHA_GPREL16:
4209 case R_ALPHA_GPRELLOW:
4210 if (dynamic_symbol_p)
4212 (*_bfd_error_handler)
4213 (_("%B: gp-relative relocation against dynamic symbol %s"),
4214 input_bfd, h->root.root.root.string);
4215 ret_val = FALSE;
4217 BFD_ASSERT(gp != 0);
4218 value -= gp;
4219 goto default_reloc;
4221 case R_ALPHA_GPRELHIGH:
4222 if (dynamic_symbol_p)
4224 (*_bfd_error_handler)
4225 (_("%B: gp-relative relocation against dynamic symbol %s"),
4226 input_bfd, h->root.root.root.string);
4227 ret_val = FALSE;
4229 BFD_ASSERT(gp != 0);
4230 value -= gp;
4231 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4232 goto default_reloc;
4234 case R_ALPHA_HINT:
4235 /* A call to a dynamic symbol is definitely out of range of
4236 the 16-bit displacement. Don't bother writing anything. */
4237 if (dynamic_symbol_p)
4239 r = bfd_reloc_ok;
4240 break;
4242 /* The regular PC-relative stuff measures from the start of
4243 the instruction rather than the end. */
4244 value -= 4;
4245 goto default_reloc;
4247 case R_ALPHA_BRADDR:
4248 if (dynamic_symbol_p)
4250 (*_bfd_error_handler)
4251 (_("%B: pc-relative relocation against dynamic symbol %s"),
4252 input_bfd, h->root.root.root.string);
4253 ret_val = FALSE;
4255 /* The regular PC-relative stuff measures from the start of
4256 the instruction rather than the end. */
4257 value -= 4;
4258 goto default_reloc;
4260 case R_ALPHA_BRSGP:
4262 int other;
4263 const char *name;
4265 /* The regular PC-relative stuff measures from the start of
4266 the instruction rather than the end. */
4267 value -= 4;
4269 /* The source and destination gp must be the same. Note that
4270 the source will always have an assigned gp, since we forced
4271 one in check_relocs, but that the destination may not, as
4272 it might not have had any relocations at all. Also take
4273 care not to crash if H is an undefined symbol. */
4274 if (h != NULL && sec != NULL
4275 && alpha_elf_tdata (sec->owner)->gotobj
4276 && gotobj != alpha_elf_tdata (sec->owner)->gotobj)
4278 (*_bfd_error_handler)
4279 (_("%B: change in gp: BRSGP %s"),
4280 input_bfd, h->root.root.root.string);
4281 ret_val = FALSE;
4284 /* The symbol should be marked either NOPV or STD_GPLOAD. */
4285 if (h != NULL)
4286 other = h->root.other;
4287 else
4288 other = sym->st_other;
4289 switch (other & STO_ALPHA_STD_GPLOAD)
4291 case STO_ALPHA_NOPV:
4292 break;
4293 case STO_ALPHA_STD_GPLOAD:
4294 value += 8;
4295 break;
4296 default:
4297 if (h != NULL)
4298 name = h->root.root.root.string;
4299 else
4301 name = (bfd_elf_string_from_elf_section
4302 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4303 if (name == NULL)
4304 name = _("<unknown>");
4305 else if (name[0] == 0)
4306 name = bfd_section_name (input_bfd, sec);
4308 (*_bfd_error_handler)
4309 (_("%B: !samegp reloc against symbol without .prologue: %s"),
4310 input_bfd, name);
4311 ret_val = FALSE;
4312 break;
4315 goto default_reloc;
4318 case R_ALPHA_REFLONG:
4319 case R_ALPHA_REFQUAD:
4320 case R_ALPHA_DTPREL64:
4321 case R_ALPHA_TPREL64:
4323 long dynindx, dyntype = r_type;
4324 bfd_vma dynaddend;
4326 /* Careful here to remember RELATIVE relocations for global
4327 variables for symbolic shared objects. */
4329 if (dynamic_symbol_p)
4331 BFD_ASSERT(h->root.dynindx != -1);
4332 dynindx = h->root.dynindx;
4333 dynaddend = addend;
4334 addend = 0, value = 0;
4336 else if (r_type == R_ALPHA_DTPREL64)
4338 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4339 value -= dtp_base;
4340 goto default_reloc;
4342 else if (r_type == R_ALPHA_TPREL64)
4344 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4345 if (!info->shared)
4347 value -= tp_base;
4348 goto default_reloc;
4350 dynindx = 0;
4351 dynaddend = value - dtp_base;
4353 else if (info->shared
4354 && r_symndx != 0
4355 && (input_section->flags & SEC_ALLOC)
4356 && !undef_weak_ref)
4358 if (r_type == R_ALPHA_REFLONG)
4360 (*_bfd_error_handler)
4361 (_("%B: unhandled dynamic relocation against %s"),
4362 input_bfd,
4363 h->root.root.root.string);
4364 ret_val = FALSE;
4366 dynindx = 0;
4367 dyntype = R_ALPHA_RELATIVE;
4368 dynaddend = value;
4370 else
4371 goto default_reloc;
4373 if (input_section->flags & SEC_ALLOC)
4374 elf64_alpha_emit_dynrel (output_bfd, info, input_section,
4375 srel, rel->r_offset, dynindx,
4376 dyntype, dynaddend);
4378 goto default_reloc;
4380 case R_ALPHA_SREL16:
4381 case R_ALPHA_SREL32:
4382 case R_ALPHA_SREL64:
4383 if (dynamic_symbol_p)
4385 (*_bfd_error_handler)
4386 (_("%B: pc-relative relocation against dynamic symbol %s"),
4387 input_bfd, h->root.root.root.string);
4388 ret_val = FALSE;
4390 else if ((info->shared || info->pie) && undef_weak_ref)
4392 (*_bfd_error_handler)
4393 (_("%B: pc-relative relocation against undefined weak symbol %s"),
4394 input_bfd, h->root.root.root.string);
4395 ret_val = FALSE;
4399 /* ??? .eh_frame references to discarded sections will be smashed
4400 to relocations against SHN_UNDEF. The .eh_frame format allows
4401 NULL to be encoded as 0 in any format, so this works here. */
4402 if (r_symndx == 0)
4403 howto = (elf64_alpha_howto_table
4404 + (r_type - R_ALPHA_SREL32 + R_ALPHA_REFLONG));
4405 goto default_reloc;
4407 case R_ALPHA_TLSLDM:
4408 /* Ignore the symbol for the relocation. The result is always
4409 the current module. */
4410 dynamic_symbol_p = 0;
4411 /* FALLTHRU */
4413 case R_ALPHA_TLSGD:
4414 if (!gotent->reloc_done)
4416 gotent->reloc_done = 1;
4418 /* Note that the module index for the main program is 1. */
4419 bfd_put_64 (output_bfd, !info->shared && !dynamic_symbol_p,
4420 sgot->contents + gotent->got_offset);
4422 /* If the symbol has been forced local, output a
4423 DTPMOD64 reloc, otherwise it will be handled in
4424 finish_dynamic_symbol. */
4425 if (info->shared && !dynamic_symbol_p)
4426 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4427 gotent->got_offset, 0,
4428 R_ALPHA_DTPMOD64, 0);
4430 if (dynamic_symbol_p || r_type == R_ALPHA_TLSLDM)
4431 value = 0;
4432 else
4434 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4435 value -= dtp_base;
4437 bfd_put_64 (output_bfd, value,
4438 sgot->contents + gotent->got_offset + 8);
4441 value = (sgot->output_section->vma
4442 + sgot->output_offset
4443 + gotent->got_offset);
4444 value -= gp;
4445 goto default_reloc;
4447 case R_ALPHA_DTPRELHI:
4448 case R_ALPHA_DTPRELLO:
4449 case R_ALPHA_DTPREL16:
4450 if (dynamic_symbol_p)
4452 (*_bfd_error_handler)
4453 (_("%B: dtp-relative relocation against dynamic symbol %s"),
4454 input_bfd, h->root.root.root.string);
4455 ret_val = FALSE;
4457 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4458 value -= dtp_base;
4459 if (r_type == R_ALPHA_DTPRELHI)
4460 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4461 goto default_reloc;
4463 case R_ALPHA_TPRELHI:
4464 case R_ALPHA_TPRELLO:
4465 case R_ALPHA_TPREL16:
4466 if (info->shared)
4468 (*_bfd_error_handler)
4469 (_("%B: TLS local exec code cannot be linked into shared objects"),
4470 input_bfd);
4471 ret_val = FALSE;
4473 else if (dynamic_symbol_p)
4475 (*_bfd_error_handler)
4476 (_("%B: tp-relative relocation against dynamic symbol %s"),
4477 input_bfd, h->root.root.root.string);
4478 ret_val = FALSE;
4480 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4481 value -= tp_base;
4482 if (r_type == R_ALPHA_TPRELHI)
4483 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4484 goto default_reloc;
4486 case R_ALPHA_GOTDTPREL:
4487 case R_ALPHA_GOTTPREL:
4488 BFD_ASSERT(sgot != NULL);
4489 BFD_ASSERT(gp != 0);
4490 BFD_ASSERT(gotent != NULL);
4491 BFD_ASSERT(gotent->use_count >= 1);
4493 if (!gotent->reloc_done)
4495 gotent->reloc_done = 1;
4497 if (dynamic_symbol_p)
4498 value = 0;
4499 else
4501 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4502 if (r_type == R_ALPHA_GOTDTPREL)
4503 value -= dtp_base;
4504 else if (!info->shared)
4505 value -= tp_base;
4506 else
4508 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4509 gotent->got_offset, 0,
4510 R_ALPHA_TPREL64,
4511 value - dtp_base);
4512 value = 0;
4515 bfd_put_64 (output_bfd, value,
4516 sgot->contents + gotent->got_offset);
4519 value = (sgot->output_section->vma
4520 + sgot->output_offset
4521 + gotent->got_offset);
4522 value -= gp;
4523 goto default_reloc;
4525 default:
4526 default_reloc:
4527 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4528 contents, rel->r_offset, value, 0);
4529 break;
4532 switch (r)
4534 case bfd_reloc_ok:
4535 break;
4537 case bfd_reloc_overflow:
4539 const char *name;
4541 /* Don't warn if the overflow is due to pc relative reloc
4542 against discarded section. Section optimization code should
4543 handle it. */
4545 if (r_symndx < symtab_hdr->sh_info
4546 && sec != NULL && howto->pc_relative
4547 && elf_discarded_section (sec))
4548 break;
4550 if (h != NULL)
4551 name = NULL;
4552 else
4554 name = (bfd_elf_string_from_elf_section
4555 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4556 if (name == NULL)
4557 return FALSE;
4558 if (*name == '\0')
4559 name = bfd_section_name (input_bfd, sec);
4561 if (! ((*info->callbacks->reloc_overflow)
4562 (info, (h ? &h->root.root : NULL), name, howto->name,
4563 (bfd_vma) 0, input_bfd, input_section,
4564 rel->r_offset)))
4565 ret_val = FALSE;
4567 break;
4569 default:
4570 case bfd_reloc_outofrange:
4571 abort ();
4575 return ret_val;
4578 /* Finish up dynamic symbol handling. We set the contents of various
4579 dynamic sections here. */
4581 static bfd_boolean
4582 elf64_alpha_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
4583 struct elf_link_hash_entry *h,
4584 Elf_Internal_Sym *sym)
4586 struct alpha_elf_link_hash_entry *ah = (struct alpha_elf_link_hash_entry *)h;
4587 bfd *dynobj = elf_hash_table(info)->dynobj;
4589 if (h->needs_plt)
4591 /* Fill in the .plt entry for this symbol. */
4592 asection *splt, *sgot, *srel;
4593 Elf_Internal_Rela outrel;
4594 bfd_byte *loc;
4595 bfd_vma got_addr, plt_addr;
4596 bfd_vma plt_index;
4597 struct alpha_elf_got_entry *gotent;
4599 BFD_ASSERT (h->dynindx != -1);
4601 splt = bfd_get_section_by_name (dynobj, ".plt");
4602 BFD_ASSERT (splt != NULL);
4603 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4604 BFD_ASSERT (srel != NULL);
4606 for (gotent = ah->got_entries; gotent ; gotent = gotent->next)
4607 if (gotent->reloc_type == R_ALPHA_LITERAL
4608 && gotent->use_count > 0)
4610 unsigned int insn;
4611 int disp;
4613 sgot = alpha_elf_tdata (gotent->gotobj)->got;
4614 BFD_ASSERT (sgot != NULL);
4616 BFD_ASSERT (gotent->got_offset != -1);
4617 BFD_ASSERT (gotent->plt_offset != -1);
4619 got_addr = (sgot->output_section->vma
4620 + sgot->output_offset
4621 + gotent->got_offset);
4622 plt_addr = (splt->output_section->vma
4623 + splt->output_offset
4624 + gotent->plt_offset);
4626 plt_index = (gotent->plt_offset-PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
4628 /* Fill in the entry in the procedure linkage table. */
4629 if (elf64_alpha_use_secureplt)
4631 disp = (PLT_HEADER_SIZE - 4) - (gotent->plt_offset + 4);
4632 insn = INSN_AD (INSN_BR, 31, disp);
4633 bfd_put_32 (output_bfd, insn,
4634 splt->contents + gotent->plt_offset);
4636 plt_index = ((gotent->plt_offset - NEW_PLT_HEADER_SIZE)
4637 / NEW_PLT_ENTRY_SIZE);
4639 else
4641 disp = -(gotent->plt_offset + 4);
4642 insn = INSN_AD (INSN_BR, 28, disp);
4643 bfd_put_32 (output_bfd, insn,
4644 splt->contents + gotent->plt_offset);
4645 bfd_put_32 (output_bfd, INSN_UNOP,
4646 splt->contents + gotent->plt_offset + 4);
4647 bfd_put_32 (output_bfd, INSN_UNOP,
4648 splt->contents + gotent->plt_offset + 8);
4650 plt_index = ((gotent->plt_offset - OLD_PLT_HEADER_SIZE)
4651 / OLD_PLT_ENTRY_SIZE);
4654 /* Fill in the entry in the .rela.plt section. */
4655 outrel.r_offset = got_addr;
4656 outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
4657 outrel.r_addend = 0;
4659 loc = srel->contents + plt_index * sizeof (Elf64_External_Rela);
4660 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
4662 /* Fill in the entry in the .got. */
4663 bfd_put_64 (output_bfd, plt_addr,
4664 sgot->contents + gotent->got_offset);
4667 else if (alpha_elf_dynamic_symbol_p (h, info))
4669 /* Fill in the dynamic relocations for this symbol's .got entries. */
4670 asection *srel;
4671 struct alpha_elf_got_entry *gotent;
4673 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4674 BFD_ASSERT (srel != NULL);
4676 for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
4677 gotent != NULL;
4678 gotent = gotent->next)
4680 asection *sgot;
4681 long r_type;
4683 if (gotent->use_count == 0)
4684 continue;
4686 sgot = alpha_elf_tdata (gotent->gotobj)->got;
4688 r_type = gotent->reloc_type;
4689 switch (r_type)
4691 case R_ALPHA_LITERAL:
4692 r_type = R_ALPHA_GLOB_DAT;
4693 break;
4694 case R_ALPHA_TLSGD:
4695 r_type = R_ALPHA_DTPMOD64;
4696 break;
4697 case R_ALPHA_GOTDTPREL:
4698 r_type = R_ALPHA_DTPREL64;
4699 break;
4700 case R_ALPHA_GOTTPREL:
4701 r_type = R_ALPHA_TPREL64;
4702 break;
4703 case R_ALPHA_TLSLDM:
4704 default:
4705 abort ();
4708 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
4709 gotent->got_offset, h->dynindx,
4710 r_type, gotent->addend);
4712 if (gotent->reloc_type == R_ALPHA_TLSGD)
4713 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
4714 gotent->got_offset + 8, h->dynindx,
4715 R_ALPHA_DTPREL64, gotent->addend);
4719 /* Mark some specially defined symbols as absolute. */
4720 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4721 || h == elf_hash_table (info)->hgot
4722 || h == elf_hash_table (info)->hplt)
4723 sym->st_shndx = SHN_ABS;
4725 return TRUE;
4728 /* Finish up the dynamic sections. */
4730 static bfd_boolean
4731 elf64_alpha_finish_dynamic_sections (bfd *output_bfd,
4732 struct bfd_link_info *info)
4734 bfd *dynobj;
4735 asection *sdyn;
4737 dynobj = elf_hash_table (info)->dynobj;
4738 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4740 if (elf_hash_table (info)->dynamic_sections_created)
4742 asection *splt, *sgotplt, *srelaplt;
4743 Elf64_External_Dyn *dyncon, *dynconend;
4744 bfd_vma plt_vma, gotplt_vma;
4746 splt = bfd_get_section_by_name (dynobj, ".plt");
4747 srelaplt = bfd_get_section_by_name (output_bfd, ".rela.plt");
4748 BFD_ASSERT (splt != NULL && sdyn != NULL);
4750 plt_vma = splt->output_section->vma + splt->output_offset;
4752 gotplt_vma = 0;
4753 if (elf64_alpha_use_secureplt)
4755 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
4756 BFD_ASSERT (sgotplt != NULL);
4757 if (sgotplt->size > 0)
4758 gotplt_vma = sgotplt->output_section->vma + sgotplt->output_offset;
4761 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4762 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4763 for (; dyncon < dynconend; dyncon++)
4765 Elf_Internal_Dyn dyn;
4767 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4769 switch (dyn.d_tag)
4771 case DT_PLTGOT:
4772 dyn.d_un.d_ptr
4773 = elf64_alpha_use_secureplt ? gotplt_vma : plt_vma;
4774 break;
4775 case DT_PLTRELSZ:
4776 dyn.d_un.d_val = srelaplt ? srelaplt->size : 0;
4777 break;
4778 case DT_JMPREL:
4779 dyn.d_un.d_ptr = srelaplt ? srelaplt->vma : 0;
4780 break;
4782 case DT_RELASZ:
4783 /* My interpretation of the TIS v1.1 ELF document indicates
4784 that RELASZ should not include JMPREL. This is not what
4785 the rest of the BFD does. It is, however, what the
4786 glibc ld.so wants. Do this fixup here until we found
4787 out who is right. */
4788 if (srelaplt)
4789 dyn.d_un.d_val -= srelaplt->size;
4790 break;
4793 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4796 /* Initialize the plt header. */
4797 if (splt->size > 0)
4799 unsigned int insn;
4800 int ofs;
4802 if (elf64_alpha_use_secureplt)
4804 ofs = gotplt_vma - (plt_vma + PLT_HEADER_SIZE);
4806 insn = INSN_ABC (INSN_SUBQ, 27, 28, 25);
4807 bfd_put_32 (output_bfd, insn, splt->contents);
4809 insn = INSN_ABO (INSN_LDAH, 28, 28, (ofs + 0x8000) >> 16);
4810 bfd_put_32 (output_bfd, insn, splt->contents + 4);
4812 insn = INSN_ABC (INSN_S4SUBQ, 25, 25, 25);
4813 bfd_put_32 (output_bfd, insn, splt->contents + 8);
4815 insn = INSN_ABO (INSN_LDA, 28, 28, ofs);
4816 bfd_put_32 (output_bfd, insn, splt->contents + 12);
4818 insn = INSN_ABO (INSN_LDQ, 27, 28, 0);
4819 bfd_put_32 (output_bfd, insn, splt->contents + 16);
4821 insn = INSN_ABC (INSN_ADDQ, 25, 25, 25);
4822 bfd_put_32 (output_bfd, insn, splt->contents + 20);
4824 insn = INSN_ABO (INSN_LDQ, 28, 28, 8);
4825 bfd_put_32 (output_bfd, insn, splt->contents + 24);
4827 insn = INSN_AB (INSN_JMP, 31, 27);
4828 bfd_put_32 (output_bfd, insn, splt->contents + 28);
4830 insn = INSN_AD (INSN_BR, 28, -PLT_HEADER_SIZE);
4831 bfd_put_32 (output_bfd, insn, splt->contents + 32);
4833 else
4835 insn = INSN_AD (INSN_BR, 27, 0); /* br $27, .+4 */
4836 bfd_put_32 (output_bfd, insn, splt->contents);
4838 insn = INSN_ABO (INSN_LDQ, 27, 27, 12);
4839 bfd_put_32 (output_bfd, insn, splt->contents + 4);
4841 insn = INSN_UNOP;
4842 bfd_put_32 (output_bfd, insn, splt->contents + 8);
4844 insn = INSN_AB (INSN_JMP, 27, 27);
4845 bfd_put_32 (output_bfd, insn, splt->contents + 12);
4847 /* The next two words will be filled in by ld.so. */
4848 bfd_put_64 (output_bfd, 0, splt->contents + 16);
4849 bfd_put_64 (output_bfd, 0, splt->contents + 24);
4852 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
4856 return TRUE;
4859 /* We need to use a special link routine to handle the .mdebug section.
4860 We need to merge all instances of these sections together, not write
4861 them all out sequentially. */
4863 static bfd_boolean
4864 elf64_alpha_final_link (bfd *abfd, struct bfd_link_info *info)
4866 asection *o;
4867 struct bfd_link_order *p;
4868 asection *mdebug_sec;
4869 struct ecoff_debug_info debug;
4870 const struct ecoff_debug_swap *swap
4871 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4872 HDRR *symhdr = &debug.symbolic_header;
4873 PTR mdebug_handle = NULL;
4875 /* Go through the sections and collect the mdebug information. */
4876 mdebug_sec = NULL;
4877 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4879 if (strcmp (o->name, ".mdebug") == 0)
4881 struct extsym_info einfo;
4883 /* We have found the .mdebug section in the output file.
4884 Look through all the link_orders comprising it and merge
4885 the information together. */
4886 symhdr->magic = swap->sym_magic;
4887 /* FIXME: What should the version stamp be? */
4888 symhdr->vstamp = 0;
4889 symhdr->ilineMax = 0;
4890 symhdr->cbLine = 0;
4891 symhdr->idnMax = 0;
4892 symhdr->ipdMax = 0;
4893 symhdr->isymMax = 0;
4894 symhdr->ioptMax = 0;
4895 symhdr->iauxMax = 0;
4896 symhdr->issMax = 0;
4897 symhdr->issExtMax = 0;
4898 symhdr->ifdMax = 0;
4899 symhdr->crfd = 0;
4900 symhdr->iextMax = 0;
4902 /* We accumulate the debugging information itself in the
4903 debug_info structure. */
4904 debug.line = NULL;
4905 debug.external_dnr = NULL;
4906 debug.external_pdr = NULL;
4907 debug.external_sym = NULL;
4908 debug.external_opt = NULL;
4909 debug.external_aux = NULL;
4910 debug.ss = NULL;
4911 debug.ssext = debug.ssext_end = NULL;
4912 debug.external_fdr = NULL;
4913 debug.external_rfd = NULL;
4914 debug.external_ext = debug.external_ext_end = NULL;
4916 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4917 if (mdebug_handle == (PTR) NULL)
4918 return FALSE;
4920 if (1)
4922 asection *s;
4923 EXTR esym;
4924 bfd_vma last = 0;
4925 unsigned int i;
4926 static const char * const name[] =
4928 ".text", ".init", ".fini", ".data",
4929 ".rodata", ".sdata", ".sbss", ".bss"
4931 static const int sc[] = { scText, scInit, scFini, scData,
4932 scRData, scSData, scSBss, scBss };
4934 esym.jmptbl = 0;
4935 esym.cobol_main = 0;
4936 esym.weakext = 0;
4937 esym.reserved = 0;
4938 esym.ifd = ifdNil;
4939 esym.asym.iss = issNil;
4940 esym.asym.st = stLocal;
4941 esym.asym.reserved = 0;
4942 esym.asym.index = indexNil;
4943 for (i = 0; i < 8; i++)
4945 esym.asym.sc = sc[i];
4946 s = bfd_get_section_by_name (abfd, name[i]);
4947 if (s != NULL)
4949 esym.asym.value = s->vma;
4950 last = s->vma + s->size;
4952 else
4953 esym.asym.value = last;
4955 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4956 name[i], &esym))
4957 return FALSE;
4961 for (p = o->map_head.link_order;
4962 p != (struct bfd_link_order *) NULL;
4963 p = p->next)
4965 asection *input_section;
4966 bfd *input_bfd;
4967 const struct ecoff_debug_swap *input_swap;
4968 struct ecoff_debug_info input_debug;
4969 char *eraw_src;
4970 char *eraw_end;
4972 if (p->type != bfd_indirect_link_order)
4974 if (p->type == bfd_data_link_order)
4975 continue;
4976 abort ();
4979 input_section = p->u.indirect.section;
4980 input_bfd = input_section->owner;
4982 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4983 || (get_elf_backend_data (input_bfd)
4984 ->elf_backend_ecoff_debug_swap) == NULL)
4986 /* I don't know what a non ALPHA ELF bfd would be
4987 doing with a .mdebug section, but I don't really
4988 want to deal with it. */
4989 continue;
4992 input_swap = (get_elf_backend_data (input_bfd)
4993 ->elf_backend_ecoff_debug_swap);
4995 BFD_ASSERT (p->size == input_section->size);
4997 /* The ECOFF linking code expects that we have already
4998 read in the debugging information and set up an
4999 ecoff_debug_info structure, so we do that now. */
5000 if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
5001 &input_debug))
5002 return FALSE;
5004 if (! (bfd_ecoff_debug_accumulate
5005 (mdebug_handle, abfd, &debug, swap, input_bfd,
5006 &input_debug, input_swap, info)))
5007 return FALSE;
5009 /* Loop through the external symbols. For each one with
5010 interesting information, try to find the symbol in
5011 the linker global hash table and save the information
5012 for the output external symbols. */
5013 eraw_src = input_debug.external_ext;
5014 eraw_end = (eraw_src
5015 + (input_debug.symbolic_header.iextMax
5016 * input_swap->external_ext_size));
5017 for (;
5018 eraw_src < eraw_end;
5019 eraw_src += input_swap->external_ext_size)
5021 EXTR ext;
5022 const char *name;
5023 struct alpha_elf_link_hash_entry *h;
5025 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
5026 if (ext.asym.sc == scNil
5027 || ext.asym.sc == scUndefined
5028 || ext.asym.sc == scSUndefined)
5029 continue;
5031 name = input_debug.ssext + ext.asym.iss;
5032 h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
5033 name, FALSE, FALSE, TRUE);
5034 if (h == NULL || h->esym.ifd != -2)
5035 continue;
5037 if (ext.ifd != -1)
5039 BFD_ASSERT (ext.ifd
5040 < input_debug.symbolic_header.ifdMax);
5041 ext.ifd = input_debug.ifdmap[ext.ifd];
5044 h->esym = ext;
5047 /* Free up the information we just read. */
5048 free (input_debug.line);
5049 free (input_debug.external_dnr);
5050 free (input_debug.external_pdr);
5051 free (input_debug.external_sym);
5052 free (input_debug.external_opt);
5053 free (input_debug.external_aux);
5054 free (input_debug.ss);
5055 free (input_debug.ssext);
5056 free (input_debug.external_fdr);
5057 free (input_debug.external_rfd);
5058 free (input_debug.external_ext);
5060 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5061 elf_link_input_bfd ignores this section. */
5062 input_section->flags &=~ SEC_HAS_CONTENTS;
5065 /* Build the external symbol information. */
5066 einfo.abfd = abfd;
5067 einfo.info = info;
5068 einfo.debug = &debug;
5069 einfo.swap = swap;
5070 einfo.failed = FALSE;
5071 elf_link_hash_traverse (elf_hash_table (info),
5072 elf64_alpha_output_extsym,
5073 (PTR) &einfo);
5074 if (einfo.failed)
5075 return FALSE;
5077 /* Set the size of the .mdebug section. */
5078 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
5080 /* Skip this section later on (I don't think this currently
5081 matters, but someday it might). */
5082 o->map_head.link_order = (struct bfd_link_order *) NULL;
5084 mdebug_sec = o;
5088 /* Invoke the regular ELF backend linker to do all the work. */
5089 if (! bfd_elf_final_link (abfd, info))
5090 return FALSE;
5092 /* Now write out the computed sections. */
5094 /* The .got subsections... */
5096 bfd *i, *dynobj = elf_hash_table(info)->dynobj;
5097 for (i = alpha_elf_hash_table(info)->got_list;
5098 i != NULL;
5099 i = alpha_elf_tdata(i)->got_link_next)
5101 asection *sgot;
5103 /* elf_bfd_final_link already did everything in dynobj. */
5104 if (i == dynobj)
5105 continue;
5107 sgot = alpha_elf_tdata(i)->got;
5108 if (! bfd_set_section_contents (abfd, sgot->output_section,
5109 sgot->contents,
5110 (file_ptr) sgot->output_offset,
5111 sgot->size))
5112 return FALSE;
5116 if (mdebug_sec != (asection *) NULL)
5118 BFD_ASSERT (abfd->output_has_begun);
5119 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5120 swap, info,
5121 mdebug_sec->filepos))
5122 return FALSE;
5124 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5127 return TRUE;
5130 static enum elf_reloc_type_class
5131 elf64_alpha_reloc_type_class (const Elf_Internal_Rela *rela)
5133 switch ((int) ELF64_R_TYPE (rela->r_info))
5135 case R_ALPHA_RELATIVE:
5136 return reloc_class_relative;
5137 case R_ALPHA_JMP_SLOT:
5138 return reloc_class_plt;
5139 case R_ALPHA_COPY:
5140 return reloc_class_copy;
5141 default:
5142 return reloc_class_normal;
5146 static const struct bfd_elf_special_section elf64_alpha_special_sections[] =
5148 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
5149 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
5150 { NULL, 0, 0, 0, 0 }
5153 /* ECOFF swapping routines. These are used when dealing with the
5154 .mdebug section, which is in the ECOFF debugging format. Copied
5155 from elf32-mips.c. */
5156 static const struct ecoff_debug_swap
5157 elf64_alpha_ecoff_debug_swap =
5159 /* Symbol table magic number. */
5160 magicSym2,
5161 /* Alignment of debugging information. E.g., 4. */
5163 /* Sizes of external symbolic information. */
5164 sizeof (struct hdr_ext),
5165 sizeof (struct dnr_ext),
5166 sizeof (struct pdr_ext),
5167 sizeof (struct sym_ext),
5168 sizeof (struct opt_ext),
5169 sizeof (struct fdr_ext),
5170 sizeof (struct rfd_ext),
5171 sizeof (struct ext_ext),
5172 /* Functions to swap in external symbolic data. */
5173 ecoff_swap_hdr_in,
5174 ecoff_swap_dnr_in,
5175 ecoff_swap_pdr_in,
5176 ecoff_swap_sym_in,
5177 ecoff_swap_opt_in,
5178 ecoff_swap_fdr_in,
5179 ecoff_swap_rfd_in,
5180 ecoff_swap_ext_in,
5181 _bfd_ecoff_swap_tir_in,
5182 _bfd_ecoff_swap_rndx_in,
5183 /* Functions to swap out external symbolic data. */
5184 ecoff_swap_hdr_out,
5185 ecoff_swap_dnr_out,
5186 ecoff_swap_pdr_out,
5187 ecoff_swap_sym_out,
5188 ecoff_swap_opt_out,
5189 ecoff_swap_fdr_out,
5190 ecoff_swap_rfd_out,
5191 ecoff_swap_ext_out,
5192 _bfd_ecoff_swap_tir_out,
5193 _bfd_ecoff_swap_rndx_out,
5194 /* Function to read in symbolic data. */
5195 elf64_alpha_read_ecoff_info
5198 /* Use a non-standard hash bucket size of 8. */
5200 static const struct elf_size_info alpha_elf_size_info =
5202 sizeof (Elf64_External_Ehdr),
5203 sizeof (Elf64_External_Phdr),
5204 sizeof (Elf64_External_Shdr),
5205 sizeof (Elf64_External_Rel),
5206 sizeof (Elf64_External_Rela),
5207 sizeof (Elf64_External_Sym),
5208 sizeof (Elf64_External_Dyn),
5209 sizeof (Elf_External_Note),
5212 64, 3,
5213 ELFCLASS64, EV_CURRENT,
5214 bfd_elf64_write_out_phdrs,
5215 bfd_elf64_write_shdrs_and_ehdr,
5216 bfd_elf64_write_relocs,
5217 bfd_elf64_swap_symbol_in,
5218 bfd_elf64_swap_symbol_out,
5219 bfd_elf64_slurp_reloc_table,
5220 bfd_elf64_slurp_symbol_table,
5221 bfd_elf64_swap_dyn_in,
5222 bfd_elf64_swap_dyn_out,
5223 bfd_elf64_swap_reloc_in,
5224 bfd_elf64_swap_reloc_out,
5225 bfd_elf64_swap_reloca_in,
5226 bfd_elf64_swap_reloca_out
5229 #define TARGET_LITTLE_SYM bfd_elf64_alpha_vec
5230 #define TARGET_LITTLE_NAME "elf64-alpha"
5231 #define ELF_ARCH bfd_arch_alpha
5232 #define ELF_MACHINE_CODE EM_ALPHA
5233 #define ELF_MAXPAGESIZE 0x10000
5235 #define bfd_elf64_bfd_link_hash_table_create \
5236 elf64_alpha_bfd_link_hash_table_create
5238 #define bfd_elf64_bfd_reloc_type_lookup \
5239 elf64_alpha_bfd_reloc_type_lookup
5240 #define elf_info_to_howto \
5241 elf64_alpha_info_to_howto
5243 #define bfd_elf64_mkobject \
5244 elf64_alpha_mkobject
5245 #define elf_backend_object_p \
5246 elf64_alpha_object_p
5248 #define elf_backend_section_from_shdr \
5249 elf64_alpha_section_from_shdr
5250 #define elf_backend_section_flags \
5251 elf64_alpha_section_flags
5252 #define elf_backend_fake_sections \
5253 elf64_alpha_fake_sections
5255 #define bfd_elf64_bfd_is_local_label_name \
5256 elf64_alpha_is_local_label_name
5257 #define bfd_elf64_find_nearest_line \
5258 elf64_alpha_find_nearest_line
5259 #define bfd_elf64_bfd_relax_section \
5260 elf64_alpha_relax_section
5262 #define elf_backend_add_symbol_hook \
5263 elf64_alpha_add_symbol_hook
5264 #define elf_backend_check_relocs \
5265 elf64_alpha_check_relocs
5266 #define elf_backend_create_dynamic_sections \
5267 elf64_alpha_create_dynamic_sections
5268 #define elf_backend_adjust_dynamic_symbol \
5269 elf64_alpha_adjust_dynamic_symbol
5270 #define elf_backend_always_size_sections \
5271 elf64_alpha_always_size_sections
5272 #define elf_backend_size_dynamic_sections \
5273 elf64_alpha_size_dynamic_sections
5274 #define elf_backend_relocate_section \
5275 elf64_alpha_relocate_section
5276 #define elf_backend_finish_dynamic_symbol \
5277 elf64_alpha_finish_dynamic_symbol
5278 #define elf_backend_finish_dynamic_sections \
5279 elf64_alpha_finish_dynamic_sections
5280 #define bfd_elf64_bfd_final_link \
5281 elf64_alpha_final_link
5282 #define elf_backend_reloc_type_class \
5283 elf64_alpha_reloc_type_class
5285 #define elf_backend_ecoff_debug_swap \
5286 &elf64_alpha_ecoff_debug_swap
5288 #define elf_backend_size_info \
5289 alpha_elf_size_info
5291 #define elf_backend_special_sections \
5292 elf64_alpha_special_sections
5294 /* A few constants that determine how the .plt section is set up. */
5295 #define elf_backend_want_got_plt 0
5296 #define elf_backend_plt_readonly 0
5297 #define elf_backend_want_plt_sym 1
5298 #define elf_backend_got_header_size 0
5300 #include "elf64-target.h"
5302 /* FreeBSD support. */
5304 #undef TARGET_LITTLE_SYM
5305 #define TARGET_LITTLE_SYM bfd_elf64_alpha_freebsd_vec
5306 #undef TARGET_LITTLE_NAME
5307 #define TARGET_LITTLE_NAME "elf64-alpha-freebsd"
5309 /* The kernel recognizes executables as valid only if they carry a
5310 "FreeBSD" label in the ELF header. So we put this label on all
5311 executables and (for simplicity) also all other object files. */
5313 static void
5314 elf64_alpha_fbsd_post_process_headers (bfd * abfd,
5315 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
5317 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
5319 i_ehdrp = elf_elfheader (abfd);
5321 /* Put an ABI label supported by FreeBSD >= 4.1. */
5322 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
5323 #ifdef OLD_FREEBSD_ABI_LABEL
5324 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
5325 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
5326 #endif
5329 #undef elf_backend_post_process_headers
5330 #define elf_backend_post_process_headers \
5331 elf64_alpha_fbsd_post_process_headers
5333 #undef elf64_bed
5334 #define elf64_bed elf64_alpha_fbsd_bed
5336 #include "elf64-target.h"