* gas/cris/rd-dw2-1.d: Adjust for dwarf2 directory table change.
[binutils.git] / bfd / elf64-alpha.c
blob9569b38eed70652bc1b1ad04beaaff6cf52df748
1 /* Alpha specific support for 64-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
50 static int alpha_elf_dynamic_symbol_p
51 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
52 static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
53 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
54 static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
55 PARAMS ((bfd *));
57 static bfd_reloc_status_type elf64_alpha_reloc_nil
58 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static bfd_reloc_status_type elf64_alpha_reloc_bad
60 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
61 static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
62 PARAMS ((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
63 static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
64 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
66 static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
67 PARAMS ((bfd *, bfd_reloc_code_real_type));
68 static void elf64_alpha_info_to_howto
69 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
71 static bfd_boolean elf64_alpha_mkobject
72 PARAMS ((bfd *));
73 static bfd_boolean elf64_alpha_object_p
74 PARAMS ((bfd *));
75 static bfd_boolean elf64_alpha_section_from_shdr
76 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
77 static bfd_boolean elf64_alpha_section_flags
78 PARAMS ((flagword *, Elf_Internal_Shdr *));
79 static bfd_boolean elf64_alpha_fake_sections
80 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
81 static bfd_boolean elf64_alpha_create_got_section
82 PARAMS ((bfd *, struct bfd_link_info *));
83 static bfd_boolean elf64_alpha_create_dynamic_sections
84 PARAMS ((bfd *, struct bfd_link_info *));
86 static bfd_boolean elf64_alpha_read_ecoff_info
87 PARAMS ((bfd *, asection *, struct ecoff_debug_info *));
88 static bfd_boolean elf64_alpha_is_local_label_name
89 PARAMS ((bfd *, const char *));
90 static bfd_boolean elf64_alpha_find_nearest_line
91 PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
92 const char **, unsigned int *));
94 #if defined(__STDC__) || defined(ALMOST_STDC)
95 struct alpha_elf_link_hash_entry;
96 #endif
98 static bfd_boolean elf64_alpha_output_extsym
99 PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
101 static bfd_boolean elf64_alpha_can_merge_gots
102 PARAMS ((bfd *, bfd *));
103 static void elf64_alpha_merge_gots
104 PARAMS ((bfd *, bfd *));
105 static bfd_boolean elf64_alpha_calc_got_offsets_for_symbol
106 PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
107 static void elf64_alpha_calc_got_offsets
108 PARAMS ((struct bfd_link_info *));
109 static bfd_boolean elf64_alpha_size_got_sections
110 PARAMS ((struct bfd_link_info *));
111 static bfd_boolean elf64_alpha_size_plt_section
112 PARAMS ((struct bfd_link_info *));
113 static bfd_boolean elf64_alpha_size_plt_section_1
114 PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
115 static bfd_boolean elf64_alpha_always_size_sections
116 PARAMS ((bfd *, struct bfd_link_info *));
117 static int alpha_dynamic_entries_for_reloc
118 PARAMS ((int, int, int));
119 static bfd_boolean elf64_alpha_calc_dynrel_sizes
120 PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
121 static bfd_boolean elf64_alpha_size_rela_got_section
122 PARAMS ((struct bfd_link_info *));
123 static bfd_boolean elf64_alpha_size_rela_got_1
124 PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
125 static bfd_boolean elf64_alpha_add_symbol_hook
126 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
127 const char **, flagword *, asection **, bfd_vma *));
128 static struct alpha_elf_got_entry *get_got_entry
129 PARAMS ((bfd *, struct alpha_elf_link_hash_entry *, unsigned long,
130 unsigned long, bfd_vma));
131 static bfd_boolean elf64_alpha_check_relocs
132 PARAMS ((bfd *, struct bfd_link_info *, asection *sec,
133 const Elf_Internal_Rela *));
134 static bfd_boolean elf64_alpha_adjust_dynamic_symbol
135 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
136 static bfd_boolean elf64_alpha_size_dynamic_sections
137 PARAMS ((bfd *, struct bfd_link_info *));
138 static void elf64_alpha_emit_dynrel
139 PARAMS ((bfd *, struct bfd_link_info *, asection *, asection *,
140 bfd_vma, long, long, bfd_vma));
141 static bfd_boolean elf64_alpha_relocate_section_r
142 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
143 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
144 static bfd_boolean elf64_alpha_relocate_section
145 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
146 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
147 static bfd_boolean elf64_alpha_finish_dynamic_symbol
148 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
149 Elf_Internal_Sym *));
150 static bfd_boolean elf64_alpha_finish_dynamic_sections
151 PARAMS ((bfd *, struct bfd_link_info *));
152 static bfd_boolean elf64_alpha_final_link
153 PARAMS ((bfd *, struct bfd_link_info *));
154 static bfd_boolean elf64_alpha_merge_ind_symbols
155 PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
156 static Elf_Internal_Rela * elf64_alpha_find_reloc_at_ofs
157 PARAMS ((Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_vma, int));
158 static enum elf_reloc_type_class elf64_alpha_reloc_type_class
159 PARAMS ((const Elf_Internal_Rela *));
161 struct alpha_elf_link_hash_entry
163 struct elf_link_hash_entry root;
165 /* External symbol information. */
166 EXTR esym;
168 /* Cumulative flags for all the .got entries. */
169 int flags;
171 /* Contexts in which a literal was referenced. */
172 #define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
173 #define ALPHA_ELF_LINK_HASH_LU_MEM 0x02
174 #define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
175 #define ALPHA_ELF_LINK_HASH_LU_JSR 0x08
176 #define ALPHA_ELF_LINK_HASH_LU_TLSGD 0x10
177 #define ALPHA_ELF_LINK_HASH_LU_TLSLDM 0x20
178 #define ALPHA_ELF_LINK_HASH_LU_FUNC 0x38
179 #define ALPHA_ELF_LINK_HASH_TLS_IE 0x40
180 #define ALPHA_ELF_LINK_HASH_PLT_LOC 0x80
182 /* Used to undo the localization of a plt symbol. */
183 asection *plt_old_section;
184 bfd_vma plt_old_value;
186 /* Used to implement multiple .got subsections. */
187 struct alpha_elf_got_entry
189 struct alpha_elf_got_entry *next;
191 /* Which .got subsection? */
192 bfd *gotobj;
194 /* The addend in effect for this entry. */
195 bfd_vma addend;
197 /* The .got offset for this entry. */
198 int got_offset;
200 /* How many references to this entry? */
201 int use_count;
203 /* The relocation type of this entry. */
204 unsigned char reloc_type;
206 /* How a LITERAL is used. */
207 unsigned char flags;
209 /* Have we initialized the dynamic relocation for this entry? */
210 unsigned char reloc_done;
212 /* Have we adjusted this entry for SEC_MERGE? */
213 unsigned char reloc_xlated;
214 } *got_entries;
216 /* Used to count non-got, non-plt relocations for delayed sizing
217 of relocation sections. */
218 struct alpha_elf_reloc_entry
220 struct alpha_elf_reloc_entry *next;
222 /* Which .reloc section? */
223 asection *srel;
225 /* What kind of relocation? */
226 unsigned int rtype;
228 /* Is this against read-only section? */
229 unsigned int reltext : 1;
231 /* How many did we find? */
232 unsigned long count;
233 } *reloc_entries;
236 /* Alpha ELF linker hash table. */
238 struct alpha_elf_link_hash_table
240 struct elf_link_hash_table root;
242 /* The head of a list of .got subsections linked through
243 alpha_elf_tdata(abfd)->got_link_next. */
244 bfd *got_list;
247 /* Look up an entry in a Alpha ELF linker hash table. */
249 #define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
250 ((struct alpha_elf_link_hash_entry *) \
251 elf_link_hash_lookup (&(table)->root, (string), (create), \
252 (copy), (follow)))
254 /* Traverse a Alpha ELF linker hash table. */
256 #define alpha_elf_link_hash_traverse(table, func, info) \
257 (elf_link_hash_traverse \
258 (&(table)->root, \
259 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
260 (info)))
262 /* Get the Alpha ELF linker hash table from a link_info structure. */
264 #define alpha_elf_hash_table(p) \
265 ((struct alpha_elf_link_hash_table *) ((p)->hash))
267 /* Get the object's symbols as our own entry type. */
269 #define alpha_elf_sym_hashes(abfd) \
270 ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
272 /* Should we do dynamic things to this symbol? */
274 static int
275 alpha_elf_dynamic_symbol_p (h, info)
276 struct elf_link_hash_entry *h;
277 struct bfd_link_info *info;
279 if (h == NULL)
280 return FALSE;
282 while (h->root.type == bfd_link_hash_indirect
283 || h->root.type == bfd_link_hash_warning)
284 h = (struct elf_link_hash_entry *) h->root.u.i.link;
286 if (h->dynindx == -1)
287 return FALSE;
289 if (h->root.type == bfd_link_hash_undefweak
290 || h->root.type == bfd_link_hash_defweak)
291 return TRUE;
293 switch (ELF_ST_VISIBILITY (h->other))
295 case STV_DEFAULT:
296 break;
297 case STV_HIDDEN:
298 case STV_INTERNAL:
299 return FALSE;
300 case STV_PROTECTED:
301 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
302 return FALSE;
303 break;
306 if ((info->shared && !info->symbolic)
307 || ((h->elf_link_hash_flags
308 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))
309 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
310 return TRUE;
312 return FALSE;
315 /* Create an entry in a Alpha ELF linker hash table. */
317 static struct bfd_hash_entry *
318 elf64_alpha_link_hash_newfunc (entry, table, string)
319 struct bfd_hash_entry *entry;
320 struct bfd_hash_table *table;
321 const char *string;
323 struct alpha_elf_link_hash_entry *ret =
324 (struct alpha_elf_link_hash_entry *) entry;
326 /* Allocate the structure if it has not already been allocated by a
327 subclass. */
328 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
329 ret = ((struct alpha_elf_link_hash_entry *)
330 bfd_hash_allocate (table,
331 sizeof (struct alpha_elf_link_hash_entry)));
332 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
333 return (struct bfd_hash_entry *) ret;
335 /* Call the allocation method of the superclass. */
336 ret = ((struct alpha_elf_link_hash_entry *)
337 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
338 table, string));
339 if (ret != (struct alpha_elf_link_hash_entry *) NULL)
341 /* Set local fields. */
342 memset (&ret->esym, 0, sizeof (EXTR));
343 /* We use -2 as a marker to indicate that the information has
344 not been set. -1 means there is no associated ifd. */
345 ret->esym.ifd = -2;
346 ret->flags = 0;
347 ret->got_entries = NULL;
348 ret->reloc_entries = NULL;
351 return (struct bfd_hash_entry *) ret;
354 /* Create a Alpha ELF linker hash table. */
356 static struct bfd_link_hash_table *
357 elf64_alpha_bfd_link_hash_table_create (abfd)
358 bfd *abfd;
360 struct alpha_elf_link_hash_table *ret;
361 bfd_size_type amt = sizeof (struct alpha_elf_link_hash_table);
363 ret = (struct alpha_elf_link_hash_table *) bfd_zmalloc (amt);
364 if (ret == (struct alpha_elf_link_hash_table *) NULL)
365 return NULL;
367 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
368 elf64_alpha_link_hash_newfunc))
370 free (ret);
371 return NULL;
374 return &ret->root.root;
377 /* We have some private fields hanging off of the elf_tdata structure. */
379 struct alpha_elf_obj_tdata
381 struct elf_obj_tdata root;
383 /* For every input file, these are the got entries for that object's
384 local symbols. */
385 struct alpha_elf_got_entry ** local_got_entries;
387 /* For every input file, this is the object that owns the got that
388 this input file uses. */
389 bfd *gotobj;
391 /* For every got, this is a linked list through the objects using this got */
392 bfd *in_got_link_next;
394 /* For every got, this is a link to the next got subsegment. */
395 bfd *got_link_next;
397 /* For every got, this is the section. */
398 asection *got;
400 /* For every got, this is it's total number of words. */
401 int total_got_size;
403 /* For every got, this is the sum of the number of words required
404 to hold all of the member object's local got. */
405 int local_got_size;
408 #define alpha_elf_tdata(abfd) \
409 ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
411 static bfd_boolean
412 elf64_alpha_mkobject (abfd)
413 bfd *abfd;
415 bfd_size_type amt = sizeof (struct alpha_elf_obj_tdata);
416 abfd->tdata.any = bfd_zalloc (abfd, amt);
417 if (abfd->tdata.any == NULL)
418 return FALSE;
419 return TRUE;
422 static bfd_boolean
423 elf64_alpha_object_p (abfd)
424 bfd *abfd;
426 /* Allocate our special target data. */
427 struct alpha_elf_obj_tdata *new_tdata;
428 bfd_size_type amt = sizeof (struct alpha_elf_obj_tdata);
429 new_tdata = bfd_zalloc (abfd, amt);
430 if (new_tdata == NULL)
431 return FALSE;
432 new_tdata->root = *abfd->tdata.elf_obj_data;
433 abfd->tdata.any = new_tdata;
435 /* Set the right machine number for an Alpha ELF file. */
436 return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
439 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
440 from smaller values. Start with zero, widen, *then* decrement. */
441 #define MINUS_ONE (((bfd_vma)0) - 1)
443 #define SKIP_HOWTO(N) \
444 HOWTO(N, 0, 0, 0, 0, 0, 0, elf64_alpha_reloc_bad, 0, 0, 0, 0, 0)
446 static reloc_howto_type elf64_alpha_howto_table[] =
448 HOWTO (R_ALPHA_NONE, /* type */
449 0, /* rightshift */
450 0, /* size (0 = byte, 1 = short, 2 = long) */
451 8, /* bitsize */
452 TRUE, /* pc_relative */
453 0, /* bitpos */
454 complain_overflow_dont, /* complain_on_overflow */
455 elf64_alpha_reloc_nil, /* special_function */
456 "NONE", /* name */
457 FALSE, /* partial_inplace */
458 0, /* src_mask */
459 0, /* dst_mask */
460 TRUE), /* pcrel_offset */
462 /* A 32 bit reference to a symbol. */
463 HOWTO (R_ALPHA_REFLONG, /* type */
464 0, /* rightshift */
465 2, /* size (0 = byte, 1 = short, 2 = long) */
466 32, /* bitsize */
467 FALSE, /* pc_relative */
468 0, /* bitpos */
469 complain_overflow_bitfield, /* complain_on_overflow */
470 0, /* special_function */
471 "REFLONG", /* name */
472 FALSE, /* partial_inplace */
473 0xffffffff, /* src_mask */
474 0xffffffff, /* dst_mask */
475 FALSE), /* pcrel_offset */
477 /* A 64 bit reference to a symbol. */
478 HOWTO (R_ALPHA_REFQUAD, /* type */
479 0, /* rightshift */
480 4, /* size (0 = byte, 1 = short, 2 = long) */
481 64, /* bitsize */
482 FALSE, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_bitfield, /* complain_on_overflow */
485 0, /* special_function */
486 "REFQUAD", /* name */
487 FALSE, /* partial_inplace */
488 MINUS_ONE, /* src_mask */
489 MINUS_ONE, /* dst_mask */
490 FALSE), /* pcrel_offset */
492 /* A 32 bit GP relative offset. This is just like REFLONG except
493 that when the value is used the value of the gp register will be
494 added in. */
495 HOWTO (R_ALPHA_GPREL32, /* type */
496 0, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 32, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_bitfield, /* complain_on_overflow */
502 0, /* special_function */
503 "GPREL32", /* name */
504 FALSE, /* partial_inplace */
505 0xffffffff, /* src_mask */
506 0xffffffff, /* dst_mask */
507 FALSE), /* pcrel_offset */
509 /* Used for an instruction that refers to memory off the GP register. */
510 HOWTO (R_ALPHA_LITERAL, /* type */
511 0, /* rightshift */
512 1, /* size (0 = byte, 1 = short, 2 = long) */
513 16, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_signed, /* complain_on_overflow */
517 0, /* special_function */
518 "ELF_LITERAL", /* name */
519 FALSE, /* partial_inplace */
520 0xffff, /* src_mask */
521 0xffff, /* dst_mask */
522 FALSE), /* pcrel_offset */
524 /* This reloc only appears immediately following an ELF_LITERAL reloc.
525 It identifies a use of the literal. The symbol index is special:
526 1 means the literal address is in the base register of a memory
527 format instruction; 2 means the literal address is in the byte
528 offset register of a byte-manipulation instruction; 3 means the
529 literal address is in the target register of a jsr instruction.
530 This does not actually do any relocation. */
531 HOWTO (R_ALPHA_LITUSE, /* type */
532 0, /* rightshift */
533 1, /* size (0 = byte, 1 = short, 2 = long) */
534 32, /* bitsize */
535 FALSE, /* pc_relative */
536 0, /* bitpos */
537 complain_overflow_dont, /* complain_on_overflow */
538 elf64_alpha_reloc_nil, /* special_function */
539 "LITUSE", /* name */
540 FALSE, /* partial_inplace */
541 0, /* src_mask */
542 0, /* dst_mask */
543 FALSE), /* pcrel_offset */
545 /* Load the gp register. This is always used for a ldah instruction
546 which loads the upper 16 bits of the gp register. The symbol
547 index of the GPDISP instruction is an offset in bytes to the lda
548 instruction that loads the lower 16 bits. The value to use for
549 the relocation is the difference between the GP value and the
550 current location; the load will always be done against a register
551 holding the current address.
553 NOTE: Unlike ECOFF, partial in-place relocation is not done. If
554 any offset is present in the instructions, it is an offset from
555 the register to the ldah instruction. This lets us avoid any
556 stupid hackery like inventing a gp value to do partial relocation
557 against. Also unlike ECOFF, we do the whole relocation off of
558 the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
559 space consuming bit, that, since all the information was present
560 in the GPDISP_HI16 reloc. */
561 HOWTO (R_ALPHA_GPDISP, /* type */
562 16, /* rightshift */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
564 16, /* bitsize */
565 FALSE, /* pc_relative */
566 0, /* bitpos */
567 complain_overflow_dont, /* complain_on_overflow */
568 elf64_alpha_reloc_gpdisp, /* special_function */
569 "GPDISP", /* name */
570 FALSE, /* partial_inplace */
571 0xffff, /* src_mask */
572 0xffff, /* dst_mask */
573 TRUE), /* pcrel_offset */
575 /* A 21 bit branch. */
576 HOWTO (R_ALPHA_BRADDR, /* type */
577 2, /* rightshift */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
579 21, /* bitsize */
580 TRUE, /* pc_relative */
581 0, /* bitpos */
582 complain_overflow_signed, /* complain_on_overflow */
583 0, /* special_function */
584 "BRADDR", /* name */
585 FALSE, /* partial_inplace */
586 0x1fffff, /* src_mask */
587 0x1fffff, /* dst_mask */
588 TRUE), /* pcrel_offset */
590 /* A hint for a jump to a register. */
591 HOWTO (R_ALPHA_HINT, /* type */
592 2, /* rightshift */
593 1, /* size (0 = byte, 1 = short, 2 = long) */
594 14, /* bitsize */
595 TRUE, /* pc_relative */
596 0, /* bitpos */
597 complain_overflow_dont, /* complain_on_overflow */
598 0, /* special_function */
599 "HINT", /* name */
600 FALSE, /* partial_inplace */
601 0x3fff, /* src_mask */
602 0x3fff, /* dst_mask */
603 TRUE), /* pcrel_offset */
605 /* 16 bit PC relative offset. */
606 HOWTO (R_ALPHA_SREL16, /* type */
607 0, /* rightshift */
608 1, /* size (0 = byte, 1 = short, 2 = long) */
609 16, /* bitsize */
610 TRUE, /* pc_relative */
611 0, /* bitpos */
612 complain_overflow_signed, /* complain_on_overflow */
613 0, /* special_function */
614 "SREL16", /* name */
615 FALSE, /* partial_inplace */
616 0xffff, /* src_mask */
617 0xffff, /* dst_mask */
618 TRUE), /* pcrel_offset */
620 /* 32 bit PC relative offset. */
621 HOWTO (R_ALPHA_SREL32, /* type */
622 0, /* rightshift */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
624 32, /* bitsize */
625 TRUE, /* pc_relative */
626 0, /* bitpos */
627 complain_overflow_signed, /* complain_on_overflow */
628 0, /* special_function */
629 "SREL32", /* name */
630 FALSE, /* partial_inplace */
631 0xffffffff, /* src_mask */
632 0xffffffff, /* dst_mask */
633 TRUE), /* pcrel_offset */
635 /* A 64 bit PC relative offset. */
636 HOWTO (R_ALPHA_SREL64, /* type */
637 0, /* rightshift */
638 4, /* size (0 = byte, 1 = short, 2 = long) */
639 64, /* bitsize */
640 TRUE, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_signed, /* complain_on_overflow */
643 0, /* special_function */
644 "SREL64", /* name */
645 FALSE, /* partial_inplace */
646 MINUS_ONE, /* src_mask */
647 MINUS_ONE, /* dst_mask */
648 TRUE), /* pcrel_offset */
650 /* Skip 12 - 16; deprecated ECOFF relocs. */
651 SKIP_HOWTO (12),
652 SKIP_HOWTO (13),
653 SKIP_HOWTO (14),
654 SKIP_HOWTO (15),
655 SKIP_HOWTO (16),
657 /* The high 16 bits of the displacement from GP to the target. */
658 HOWTO (R_ALPHA_GPRELHIGH,
659 0, /* rightshift */
660 1, /* size (0 = byte, 1 = short, 2 = long) */
661 16, /* bitsize */
662 FALSE, /* pc_relative */
663 0, /* bitpos */
664 complain_overflow_signed, /* complain_on_overflow */
665 0, /* special_function */
666 "GPRELHIGH", /* name */
667 FALSE, /* partial_inplace */
668 0xffff, /* src_mask */
669 0xffff, /* dst_mask */
670 FALSE), /* pcrel_offset */
672 /* The low 16 bits of the displacement from GP to the target. */
673 HOWTO (R_ALPHA_GPRELLOW,
674 0, /* rightshift */
675 1, /* size (0 = byte, 1 = short, 2 = long) */
676 16, /* bitsize */
677 FALSE, /* pc_relative */
678 0, /* bitpos */
679 complain_overflow_dont, /* complain_on_overflow */
680 0, /* special_function */
681 "GPRELLOW", /* name */
682 FALSE, /* partial_inplace */
683 0xffff, /* src_mask */
684 0xffff, /* dst_mask */
685 FALSE), /* pcrel_offset */
687 /* A 16-bit displacement from the GP to the target. */
688 HOWTO (R_ALPHA_GPREL16,
689 0, /* rightshift */
690 1, /* size (0 = byte, 1 = short, 2 = long) */
691 16, /* bitsize */
692 FALSE, /* pc_relative */
693 0, /* bitpos */
694 complain_overflow_signed, /* complain_on_overflow */
695 0, /* special_function */
696 "GPREL16", /* name */
697 FALSE, /* partial_inplace */
698 0xffff, /* src_mask */
699 0xffff, /* dst_mask */
700 FALSE), /* pcrel_offset */
702 /* Skip 20 - 23; deprecated ECOFF relocs. */
703 SKIP_HOWTO (20),
704 SKIP_HOWTO (21),
705 SKIP_HOWTO (22),
706 SKIP_HOWTO (23),
708 /* Misc ELF relocations. */
710 /* A dynamic relocation to copy the target into our .dynbss section. */
711 /* Not generated, as all Alpha objects use PIC, so it is not needed. It
712 is present because every other ELF has one, but should not be used
713 because .dynbss is an ugly thing. */
714 HOWTO (R_ALPHA_COPY,
718 FALSE,
720 complain_overflow_dont,
721 bfd_elf_generic_reloc,
722 "COPY",
723 FALSE,
726 TRUE),
728 /* A dynamic relocation for a .got entry. */
729 HOWTO (R_ALPHA_GLOB_DAT,
733 FALSE,
735 complain_overflow_dont,
736 bfd_elf_generic_reloc,
737 "GLOB_DAT",
738 FALSE,
741 TRUE),
743 /* A dynamic relocation for a .plt entry. */
744 HOWTO (R_ALPHA_JMP_SLOT,
748 FALSE,
750 complain_overflow_dont,
751 bfd_elf_generic_reloc,
752 "JMP_SLOT",
753 FALSE,
756 TRUE),
758 /* A dynamic relocation to add the base of the DSO to a 64-bit field. */
759 HOWTO (R_ALPHA_RELATIVE,
763 FALSE,
765 complain_overflow_dont,
766 bfd_elf_generic_reloc,
767 "RELATIVE",
768 FALSE,
771 TRUE),
773 /* A 21 bit branch that adjusts for gp loads. */
774 HOWTO (R_ALPHA_BRSGP, /* type */
775 2, /* rightshift */
776 2, /* size (0 = byte, 1 = short, 2 = long) */
777 21, /* bitsize */
778 TRUE, /* pc_relative */
779 0, /* bitpos */
780 complain_overflow_signed, /* complain_on_overflow */
781 0, /* special_function */
782 "BRSGP", /* name */
783 FALSE, /* partial_inplace */
784 0x1fffff, /* src_mask */
785 0x1fffff, /* dst_mask */
786 TRUE), /* pcrel_offset */
788 /* Creates a tls_index for the symbol in the got. */
789 HOWTO (R_ALPHA_TLSGD, /* type */
790 0, /* rightshift */
791 1, /* size (0 = byte, 1 = short, 2 = long) */
792 16, /* bitsize */
793 FALSE, /* pc_relative */
794 0, /* bitpos */
795 complain_overflow_signed, /* complain_on_overflow */
796 0, /* special_function */
797 "TLSGD", /* name */
798 FALSE, /* partial_inplace */
799 0xffff, /* src_mask */
800 0xffff, /* dst_mask */
801 FALSE), /* pcrel_offset */
803 /* Creates a tls_index for the (current) module in the got. */
804 HOWTO (R_ALPHA_TLSLDM, /* type */
805 0, /* rightshift */
806 1, /* size (0 = byte, 1 = short, 2 = long) */
807 16, /* bitsize */
808 FALSE, /* pc_relative */
809 0, /* bitpos */
810 complain_overflow_signed, /* complain_on_overflow */
811 0, /* special_function */
812 "TLSLDM", /* name */
813 FALSE, /* partial_inplace */
814 0xffff, /* src_mask */
815 0xffff, /* dst_mask */
816 FALSE), /* pcrel_offset */
818 /* A dynamic relocation for a DTP module entry. */
819 HOWTO (R_ALPHA_DTPMOD64, /* type */
820 0, /* rightshift */
821 4, /* size (0 = byte, 1 = short, 2 = long) */
822 64, /* bitsize */
823 FALSE, /* pc_relative */
824 0, /* bitpos */
825 complain_overflow_bitfield, /* complain_on_overflow */
826 0, /* special_function */
827 "DTPMOD64", /* name */
828 FALSE, /* partial_inplace */
829 MINUS_ONE, /* src_mask */
830 MINUS_ONE, /* dst_mask */
831 FALSE), /* pcrel_offset */
833 /* Creates a 64-bit offset in the got for the displacement
834 from DTP to the target. */
835 HOWTO (R_ALPHA_GOTDTPREL, /* type */
836 0, /* rightshift */
837 1, /* size (0 = byte, 1 = short, 2 = long) */
838 16, /* bitsize */
839 FALSE, /* pc_relative */
840 0, /* bitpos */
841 complain_overflow_signed, /* complain_on_overflow */
842 0, /* special_function */
843 "GOTDTPREL", /* name */
844 FALSE, /* partial_inplace */
845 0xffff, /* src_mask */
846 0xffff, /* dst_mask */
847 FALSE), /* pcrel_offset */
849 /* A dynamic relocation for a displacement from DTP to the target. */
850 HOWTO (R_ALPHA_DTPREL64, /* type */
851 0, /* rightshift */
852 4, /* size (0 = byte, 1 = short, 2 = long) */
853 64, /* bitsize */
854 FALSE, /* pc_relative */
855 0, /* bitpos */
856 complain_overflow_bitfield, /* complain_on_overflow */
857 0, /* special_function */
858 "DTPREL64", /* name */
859 FALSE, /* partial_inplace */
860 MINUS_ONE, /* src_mask */
861 MINUS_ONE, /* dst_mask */
862 FALSE), /* pcrel_offset */
864 /* The high 16 bits of the displacement from DTP to the target. */
865 HOWTO (R_ALPHA_DTPRELHI, /* type */
866 0, /* rightshift */
867 1, /* size (0 = byte, 1 = short, 2 = long) */
868 16, /* bitsize */
869 FALSE, /* pc_relative */
870 0, /* bitpos */
871 complain_overflow_signed, /* complain_on_overflow */
872 0, /* special_function */
873 "DTPRELHI", /* name */
874 FALSE, /* partial_inplace */
875 0xffff, /* src_mask */
876 0xffff, /* dst_mask */
877 FALSE), /* pcrel_offset */
879 /* The low 16 bits of the displacement from DTP to the target. */
880 HOWTO (R_ALPHA_DTPRELLO, /* type */
881 0, /* rightshift */
882 1, /* size (0 = byte, 1 = short, 2 = long) */
883 16, /* bitsize */
884 FALSE, /* pc_relative */
885 0, /* bitpos */
886 complain_overflow_dont, /* complain_on_overflow */
887 0, /* special_function */
888 "DTPRELLO", /* name */
889 FALSE, /* partial_inplace */
890 0xffff, /* src_mask */
891 0xffff, /* dst_mask */
892 FALSE), /* pcrel_offset */
894 /* A 16-bit displacement from DTP to the target. */
895 HOWTO (R_ALPHA_DTPREL16, /* type */
896 0, /* rightshift */
897 1, /* size (0 = byte, 1 = short, 2 = long) */
898 16, /* bitsize */
899 FALSE, /* pc_relative */
900 0, /* bitpos */
901 complain_overflow_signed, /* complain_on_overflow */
902 0, /* special_function */
903 "DTPREL16", /* name */
904 FALSE, /* partial_inplace */
905 0xffff, /* src_mask */
906 0xffff, /* dst_mask */
907 FALSE), /* pcrel_offset */
909 /* Creates a 64-bit offset in the got for the displacement
910 from TP to the target. */
911 HOWTO (R_ALPHA_GOTTPREL, /* type */
912 0, /* rightshift */
913 1, /* size (0 = byte, 1 = short, 2 = long) */
914 16, /* bitsize */
915 FALSE, /* pc_relative */
916 0, /* bitpos */
917 complain_overflow_signed, /* complain_on_overflow */
918 0, /* special_function */
919 "GOTTPREL", /* name */
920 FALSE, /* partial_inplace */
921 0xffff, /* src_mask */
922 0xffff, /* dst_mask */
923 FALSE), /* pcrel_offset */
925 /* A dynamic relocation for a displacement from TP to the target. */
926 HOWTO (R_ALPHA_TPREL64, /* type */
927 0, /* rightshift */
928 4, /* size (0 = byte, 1 = short, 2 = long) */
929 64, /* bitsize */
930 FALSE, /* pc_relative */
931 0, /* bitpos */
932 complain_overflow_bitfield, /* complain_on_overflow */
933 0, /* special_function */
934 "TPREL64", /* name */
935 FALSE, /* partial_inplace */
936 MINUS_ONE, /* src_mask */
937 MINUS_ONE, /* dst_mask */
938 FALSE), /* pcrel_offset */
940 /* The high 16 bits of the displacement from TP to the target. */
941 HOWTO (R_ALPHA_TPRELHI, /* type */
942 0, /* rightshift */
943 1, /* size (0 = byte, 1 = short, 2 = long) */
944 16, /* bitsize */
945 FALSE, /* pc_relative */
946 0, /* bitpos */
947 complain_overflow_signed, /* complain_on_overflow */
948 0, /* special_function */
949 "TPRELHI", /* name */
950 FALSE, /* partial_inplace */
951 0xffff, /* src_mask */
952 0xffff, /* dst_mask */
953 FALSE), /* pcrel_offset */
955 /* The low 16 bits of the displacement from TP to the target. */
956 HOWTO (R_ALPHA_TPRELLO, /* type */
957 0, /* rightshift */
958 1, /* size (0 = byte, 1 = short, 2 = long) */
959 16, /* bitsize */
960 FALSE, /* pc_relative */
961 0, /* bitpos */
962 complain_overflow_dont, /* complain_on_overflow */
963 0, /* special_function */
964 "TPRELLO", /* name */
965 FALSE, /* partial_inplace */
966 0xffff, /* src_mask */
967 0xffff, /* dst_mask */
968 FALSE), /* pcrel_offset */
970 /* A 16-bit displacement from TP to the target. */
971 HOWTO (R_ALPHA_TPREL16, /* type */
972 0, /* rightshift */
973 1, /* size (0 = byte, 1 = short, 2 = long) */
974 16, /* bitsize */
975 FALSE, /* pc_relative */
976 0, /* bitpos */
977 complain_overflow_signed, /* complain_on_overflow */
978 0, /* special_function */
979 "TPREL16", /* name */
980 FALSE, /* partial_inplace */
981 0xffff, /* src_mask */
982 0xffff, /* dst_mask */
983 FALSE), /* pcrel_offset */
986 /* A relocation function which doesn't do anything. */
988 static bfd_reloc_status_type
989 elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
990 bfd *abfd ATTRIBUTE_UNUSED;
991 arelent *reloc;
992 asymbol *sym ATTRIBUTE_UNUSED;
993 PTR data ATTRIBUTE_UNUSED;
994 asection *sec;
995 bfd *output_bfd;
996 char **error_message ATTRIBUTE_UNUSED;
998 if (output_bfd)
999 reloc->address += sec->output_offset;
1000 return bfd_reloc_ok;
1003 /* A relocation function used for an unsupported reloc. */
1005 static bfd_reloc_status_type
1006 elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
1007 bfd *abfd ATTRIBUTE_UNUSED;
1008 arelent *reloc;
1009 asymbol *sym ATTRIBUTE_UNUSED;
1010 PTR data ATTRIBUTE_UNUSED;
1011 asection *sec;
1012 bfd *output_bfd;
1013 char **error_message ATTRIBUTE_UNUSED;
1015 if (output_bfd)
1016 reloc->address += sec->output_offset;
1017 return bfd_reloc_notsupported;
1020 /* Do the work of the GPDISP relocation. */
1022 static bfd_reloc_status_type
1023 elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
1024 bfd *abfd;
1025 bfd_vma gpdisp;
1026 bfd_byte *p_ldah;
1027 bfd_byte *p_lda;
1029 bfd_reloc_status_type ret = bfd_reloc_ok;
1030 bfd_vma addend;
1031 unsigned long i_ldah, i_lda;
1033 i_ldah = bfd_get_32 (abfd, p_ldah);
1034 i_lda = bfd_get_32 (abfd, p_lda);
1036 /* Complain if the instructions are not correct. */
1037 if (((i_ldah >> 26) & 0x3f) != 0x09
1038 || ((i_lda >> 26) & 0x3f) != 0x08)
1039 ret = bfd_reloc_dangerous;
1041 /* Extract the user-supplied offset, mirroring the sign extensions
1042 that the instructions perform. */
1043 addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
1044 addend = (addend ^ 0x80008000) - 0x80008000;
1046 gpdisp += addend;
1048 if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
1049 || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
1050 ret = bfd_reloc_overflow;
1052 /* compensate for the sign extension again. */
1053 i_ldah = ((i_ldah & 0xffff0000)
1054 | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
1055 i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
1057 bfd_put_32 (abfd, (bfd_vma) i_ldah, p_ldah);
1058 bfd_put_32 (abfd, (bfd_vma) i_lda, p_lda);
1060 return ret;
1063 /* The special function for the GPDISP reloc. */
1065 static bfd_reloc_status_type
1066 elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
1067 output_bfd, err_msg)
1068 bfd *abfd;
1069 arelent *reloc_entry;
1070 asymbol *sym ATTRIBUTE_UNUSED;
1071 PTR data;
1072 asection *input_section;
1073 bfd *output_bfd;
1074 char **err_msg;
1076 bfd_reloc_status_type ret;
1077 bfd_vma gp, relocation;
1078 bfd_byte *p_ldah, *p_lda;
1080 /* Don't do anything if we're not doing a final link. */
1081 if (output_bfd)
1083 reloc_entry->address += input_section->output_offset;
1084 return bfd_reloc_ok;
1087 if (reloc_entry->address > input_section->_cooked_size ||
1088 reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
1089 return bfd_reloc_outofrange;
1091 /* The gp used in the portion of the output object to which this
1092 input object belongs is cached on the input bfd. */
1093 gp = _bfd_get_gp_value (abfd);
1095 relocation = (input_section->output_section->vma
1096 + input_section->output_offset
1097 + reloc_entry->address);
1099 p_ldah = (bfd_byte *) data + reloc_entry->address;
1100 p_lda = p_ldah + reloc_entry->addend;
1102 ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
1104 /* Complain if the instructions are not correct. */
1105 if (ret == bfd_reloc_dangerous)
1106 *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
1108 return ret;
1111 /* A mapping from BFD reloc types to Alpha ELF reloc types. */
1113 struct elf_reloc_map
1115 bfd_reloc_code_real_type bfd_reloc_val;
1116 int elf_reloc_val;
1119 static const struct elf_reloc_map elf64_alpha_reloc_map[] =
1121 {BFD_RELOC_NONE, R_ALPHA_NONE},
1122 {BFD_RELOC_32, R_ALPHA_REFLONG},
1123 {BFD_RELOC_64, R_ALPHA_REFQUAD},
1124 {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
1125 {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
1126 {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
1127 {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
1128 {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
1129 {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
1130 {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
1131 {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
1132 {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
1133 {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
1134 {BFD_RELOC_ALPHA_GPREL_HI16, R_ALPHA_GPRELHIGH},
1135 {BFD_RELOC_ALPHA_GPREL_LO16, R_ALPHA_GPRELLOW},
1136 {BFD_RELOC_GPREL16, R_ALPHA_GPREL16},
1137 {BFD_RELOC_ALPHA_BRSGP, R_ALPHA_BRSGP},
1138 {BFD_RELOC_ALPHA_TLSGD, R_ALPHA_TLSGD},
1139 {BFD_RELOC_ALPHA_TLSLDM, R_ALPHA_TLSLDM},
1140 {BFD_RELOC_ALPHA_DTPMOD64, R_ALPHA_DTPMOD64},
1141 {BFD_RELOC_ALPHA_GOTDTPREL16, R_ALPHA_GOTDTPREL},
1142 {BFD_RELOC_ALPHA_DTPREL64, R_ALPHA_DTPREL64},
1143 {BFD_RELOC_ALPHA_DTPREL_HI16, R_ALPHA_DTPRELHI},
1144 {BFD_RELOC_ALPHA_DTPREL_LO16, R_ALPHA_DTPRELLO},
1145 {BFD_RELOC_ALPHA_DTPREL16, R_ALPHA_DTPREL16},
1146 {BFD_RELOC_ALPHA_GOTTPREL16, R_ALPHA_GOTTPREL},
1147 {BFD_RELOC_ALPHA_TPREL64, R_ALPHA_TPREL64},
1148 {BFD_RELOC_ALPHA_TPREL_HI16, R_ALPHA_TPRELHI},
1149 {BFD_RELOC_ALPHA_TPREL_LO16, R_ALPHA_TPRELLO},
1150 {BFD_RELOC_ALPHA_TPREL16, R_ALPHA_TPREL16},
1153 /* Given a BFD reloc type, return a HOWTO structure. */
1155 static reloc_howto_type *
1156 elf64_alpha_bfd_reloc_type_lookup (abfd, code)
1157 bfd *abfd ATTRIBUTE_UNUSED;
1158 bfd_reloc_code_real_type code;
1160 const struct elf_reloc_map *i, *e;
1161 i = e = elf64_alpha_reloc_map;
1162 e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
1163 for (; i != e; ++i)
1165 if (i->bfd_reloc_val == code)
1166 return &elf64_alpha_howto_table[i->elf_reloc_val];
1168 return 0;
1171 /* Given an Alpha ELF reloc type, fill in an arelent structure. */
1173 static void
1174 elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
1175 bfd *abfd ATTRIBUTE_UNUSED;
1176 arelent *cache_ptr;
1177 Elf_Internal_Rela *dst;
1179 unsigned r_type;
1181 r_type = ELF64_R_TYPE(dst->r_info);
1182 BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1183 cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1186 /* These two relocations create a two-word entry in the got. */
1187 #define alpha_got_entry_size(r_type) \
1188 (r_type == R_ALPHA_TLSGD || r_type == R_ALPHA_TLSLDM ? 16 : 8)
1190 /* This is PT_TLS segment p_vaddr. */
1191 #define alpha_get_dtprel_base(tlss) \
1192 ((tlss)->start)
1194 /* Main program TLS (whose template starts at PT_TLS p_vaddr)
1195 is assigned offset round(16, PT_TLS p_align). */
1196 #define alpha_get_tprel_base(tlss) \
1197 ((tlss)->start - align_power ((bfd_vma) 16, (tlss)->align))
1199 /* These functions do relaxation for Alpha ELF.
1201 Currently I'm only handling what I can do with existing compiler
1202 and assembler support, which means no instructions are removed,
1203 though some may be nopped. At this time GCC does not emit enough
1204 information to do all of the relaxing that is possible. It will
1205 take some not small amount of work for that to happen.
1207 There are a couple of interesting papers that I once read on this
1208 subject, that I cannot find references to at the moment, that
1209 related to Alpha in particular. They are by David Wall, then of
1210 DEC WRL. */
1212 #define OP_LDA 0x08
1213 #define OP_LDAH 0x09
1214 #define INSN_JSR 0x68004000
1215 #define INSN_JSR_MASK 0xfc00c000
1216 #define OP_LDQ 0x29
1217 #define OP_BR 0x30
1218 #define OP_BSR 0x34
1219 #define INSN_UNOP 0x2ffe0000
1220 #define INSN_ADDQ 0x40000400
1221 #define INSN_RDUNIQ 0x0000009e
1223 struct alpha_relax_info
1225 bfd *abfd;
1226 asection *sec;
1227 bfd_byte *contents;
1228 Elf_Internal_Shdr *symtab_hdr;
1229 Elf_Internal_Rela *relocs, *relend;
1230 struct bfd_link_info *link_info;
1231 struct elf_link_tls_segment *tls_segment;
1232 bfd_vma gp;
1233 bfd *gotobj;
1234 asection *tsec;
1235 struct alpha_elf_link_hash_entry *h;
1236 struct alpha_elf_got_entry **first_gotent;
1237 struct alpha_elf_got_entry *gotent;
1238 bfd_boolean changed_contents;
1239 bfd_boolean changed_relocs;
1240 unsigned char other;
1243 static bfd_boolean elf64_alpha_relax_with_lituse
1244 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1245 Elf_Internal_Rela *irel));
1246 static bfd_vma elf64_alpha_relax_opt_call
1247 PARAMS((struct alpha_relax_info *info, bfd_vma symval));
1248 static bfd_boolean elf64_alpha_relax_got_load
1249 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1250 Elf_Internal_Rela *irel, unsigned long));
1251 static bfd_boolean elf64_alpha_relax_gprelhilo
1252 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1253 Elf_Internal_Rela *irel, bfd_boolean));
1254 static bfd_boolean elf64_alpha_relax_tls_get_addr
1255 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1256 Elf_Internal_Rela *irel, bfd_boolean));
1257 static struct elf_link_tls_segment *elf64_alpha_relax_find_tls_segment
1258 PARAMS((struct alpha_relax_info *, struct elf_link_tls_segment *));
1259 static bfd_boolean elf64_alpha_relax_section
1260 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
1261 bfd_boolean *again));
1263 static Elf_Internal_Rela *
1264 elf64_alpha_find_reloc_at_ofs (rel, relend, offset, type)
1265 Elf_Internal_Rela *rel, *relend;
1266 bfd_vma offset;
1267 int type;
1269 while (rel < relend)
1271 if (rel->r_offset == offset
1272 && ELF64_R_TYPE (rel->r_info) == (unsigned int) type)
1273 return rel;
1274 ++rel;
1276 return NULL;
1279 static bfd_boolean
1280 elf64_alpha_relax_with_lituse (info, symval, irel)
1281 struct alpha_relax_info *info;
1282 bfd_vma symval;
1283 Elf_Internal_Rela *irel;
1285 Elf_Internal_Rela *urel, *irelend = info->relend;
1286 int flags, count, i;
1287 bfd_signed_vma disp;
1288 bfd_boolean fits16;
1289 bfd_boolean fits32;
1290 bfd_boolean lit_reused = FALSE;
1291 bfd_boolean all_optimized = TRUE;
1292 unsigned int lit_insn;
1294 lit_insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1295 if (lit_insn >> 26 != OP_LDQ)
1297 ((*_bfd_error_handler)
1298 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1299 bfd_archive_filename (info->abfd), info->sec->name,
1300 (unsigned long) irel->r_offset));
1301 return TRUE;
1304 /* Can't relax dynamic symbols. */
1305 if (alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
1306 return TRUE;
1308 /* Summarize how this particular LITERAL is used. */
1309 for (urel = irel+1, flags = count = 0; urel < irelend; ++urel, ++count)
1311 if (ELF64_R_TYPE (urel->r_info) != R_ALPHA_LITUSE)
1312 break;
1313 if (urel->r_addend <= 3)
1314 flags |= 1 << urel->r_addend;
1317 /* A little preparation for the loop... */
1318 disp = symval - info->gp;
1320 for (urel = irel+1, i = 0; i < count; ++i, ++urel)
1322 unsigned int insn;
1323 int insn_disp;
1324 bfd_signed_vma xdisp;
1326 insn = bfd_get_32 (info->abfd, info->contents + urel->r_offset);
1328 switch (urel->r_addend)
1330 case LITUSE_ALPHA_ADDR:
1331 default:
1332 /* This type is really just a placeholder to note that all
1333 uses cannot be optimized, but to still allow some. */
1334 all_optimized = FALSE;
1335 break;
1337 case LITUSE_ALPHA_BASE:
1338 /* We can always optimize 16-bit displacements. */
1340 /* Extract the displacement from the instruction, sign-extending
1341 it if necessary, then test whether it is within 16 or 32 bits
1342 displacement from GP. */
1343 insn_disp = insn & 0x0000ffff;
1344 if (insn_disp & 0x8000)
1345 insn_disp |= ~0xffff; /* Negative: sign-extend. */
1347 xdisp = disp + insn_disp;
1348 fits16 = (xdisp >= - (bfd_signed_vma) 0x8000 && xdisp < 0x8000);
1349 fits32 = (xdisp >= - (bfd_signed_vma) 0x80000000
1350 && xdisp < 0x7fff8000);
1352 if (fits16)
1354 /* Take the op code and dest from this insn, take the base
1355 register from the literal insn. Leave the offset alone. */
1356 insn = (insn & 0xffe0ffff) | (lit_insn & 0x001f0000);
1357 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1358 R_ALPHA_GPREL16);
1359 urel->r_addend = irel->r_addend;
1360 info->changed_relocs = TRUE;
1362 bfd_put_32 (info->abfd, (bfd_vma) insn,
1363 info->contents + urel->r_offset);
1364 info->changed_contents = TRUE;
1367 /* If all mem+byte, we can optimize 32-bit mem displacements. */
1368 else if (fits32 && !(flags & ~6))
1370 /* FIXME: sanity check that lit insn Ra is mem insn Rb. */
1372 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1373 R_ALPHA_GPRELHIGH);
1374 lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
1375 bfd_put_32 (info->abfd, (bfd_vma) lit_insn,
1376 info->contents + irel->r_offset);
1377 lit_reused = TRUE;
1378 info->changed_contents = TRUE;
1380 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1381 R_ALPHA_GPRELLOW);
1382 urel->r_addend = irel->r_addend;
1383 info->changed_relocs = TRUE;
1385 else
1386 all_optimized = FALSE;
1387 break;
1389 case LITUSE_ALPHA_BYTOFF:
1390 /* We can always optimize byte instructions. */
1392 /* FIXME: sanity check the insn for byte op. Check that the
1393 literal dest reg is indeed Rb in the byte insn. */
1395 insn &= ~ (unsigned) 0x001ff000;
1396 insn |= ((symval & 7) << 13) | 0x1000;
1398 urel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1399 urel->r_addend = 0;
1400 info->changed_relocs = TRUE;
1402 bfd_put_32 (info->abfd, (bfd_vma) insn,
1403 info->contents + urel->r_offset);
1404 info->changed_contents = TRUE;
1405 break;
1407 case LITUSE_ALPHA_JSR:
1408 case LITUSE_ALPHA_TLSGD:
1409 case LITUSE_ALPHA_TLSLDM:
1411 bfd_vma optdest, org;
1412 bfd_signed_vma odisp;
1414 /* If not zero, place to jump without needing pv. */
1415 optdest = elf64_alpha_relax_opt_call (info, symval);
1416 org = (info->sec->output_section->vma
1417 + info->sec->output_offset
1418 + urel->r_offset + 4);
1419 odisp = (optdest ? optdest : symval) - org;
1421 if (odisp >= -0x400000 && odisp < 0x400000)
1423 Elf_Internal_Rela *xrel;
1425 /* Preserve branch prediction call stack when possible. */
1426 if ((insn & INSN_JSR_MASK) == INSN_JSR)
1427 insn = (OP_BSR << 26) | (insn & 0x03e00000);
1428 else
1429 insn = (OP_BR << 26) | (insn & 0x03e00000);
1431 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1432 R_ALPHA_BRADDR);
1433 urel->r_addend = irel->r_addend;
1435 if (optdest)
1436 urel->r_addend += optdest - symval;
1437 else
1438 all_optimized = FALSE;
1440 bfd_put_32 (info->abfd, (bfd_vma) insn,
1441 info->contents + urel->r_offset);
1443 /* Kill any HINT reloc that might exist for this insn. */
1444 xrel = (elf64_alpha_find_reloc_at_ofs
1445 (info->relocs, info->relend, urel->r_offset,
1446 R_ALPHA_HINT));
1447 if (xrel)
1448 xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1450 info->changed_contents = TRUE;
1451 info->changed_relocs = TRUE;
1453 else
1454 all_optimized = FALSE;
1456 /* Even if the target is not in range for a direct branch,
1457 if we share a GP, we can eliminate the gp reload. */
1458 if (optdest)
1460 Elf_Internal_Rela *gpdisp
1461 = (elf64_alpha_find_reloc_at_ofs
1462 (info->relocs, irelend, urel->r_offset + 4,
1463 R_ALPHA_GPDISP));
1464 if (gpdisp)
1466 bfd_byte *p_ldah = info->contents + gpdisp->r_offset;
1467 bfd_byte *p_lda = p_ldah + gpdisp->r_addend;
1468 unsigned int ldah = bfd_get_32 (info->abfd, p_ldah);
1469 unsigned int lda = bfd_get_32 (info->abfd, p_lda);
1471 /* Verify that the instruction is "ldah $29,0($26)".
1472 Consider a function that ends in a noreturn call,
1473 and that the next function begins with an ldgp,
1474 and that by accident there is no padding between.
1475 In that case the insn would use $27 as the base. */
1476 if (ldah == 0x27ba0000 && lda == 0x23bd0000)
1478 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, p_ldah);
1479 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, p_lda);
1481 gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1482 info->changed_contents = TRUE;
1483 info->changed_relocs = TRUE;
1488 break;
1492 /* If all cases were optimized, we can reduce the use count on this
1493 got entry by one, possibly eliminating it. */
1494 if (all_optimized)
1496 if (--info->gotent->use_count == 0)
1498 int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
1499 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1500 if (!info->h)
1501 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
1504 /* If the literal instruction is no longer needed (it may have been
1505 reused. We can eliminate it. */
1506 /* ??? For now, I don't want to deal with compacting the section,
1507 so just nop it out. */
1508 if (!lit_reused)
1510 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1511 info->changed_relocs = TRUE;
1513 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP,
1514 info->contents + irel->r_offset);
1515 info->changed_contents = TRUE;
1519 return TRUE;
1522 static bfd_vma
1523 elf64_alpha_relax_opt_call (info, symval)
1524 struct alpha_relax_info *info;
1525 bfd_vma symval;
1527 /* If the function has the same gp, and we can identify that the
1528 function does not use its function pointer, we can eliminate the
1529 address load. */
1531 /* If the symbol is marked NOPV, we are being told the function never
1532 needs its procedure value. */
1533 if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_NOPV)
1534 return symval;
1536 /* If the symbol is marked STD_GP, we are being told the function does
1537 a normal ldgp in the first two words. */
1538 else if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_STD_GPLOAD)
1541 /* Otherwise, we may be able to identify a GP load in the first two
1542 words, which we can then skip. */
1543 else
1545 Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
1546 bfd_vma ofs;
1548 /* Load the relocations from the section that the target symbol is in. */
1549 if (info->sec == info->tsec)
1551 tsec_relocs = info->relocs;
1552 tsec_relend = info->relend;
1553 tsec_free = NULL;
1555 else
1557 tsec_relocs = (_bfd_elf64_link_read_relocs
1558 (info->abfd, info->tsec, (PTR) NULL,
1559 (Elf_Internal_Rela *) NULL,
1560 info->link_info->keep_memory));
1561 if (tsec_relocs == NULL)
1562 return 0;
1563 tsec_relend = tsec_relocs + info->tsec->reloc_count;
1564 tsec_free = (info->link_info->keep_memory ? NULL : tsec_relocs);
1567 /* Recover the symbol's offset within the section. */
1568 ofs = (symval - info->tsec->output_section->vma
1569 - info->tsec->output_offset);
1571 /* Look for a GPDISP reloc. */
1572 gpdisp = (elf64_alpha_find_reloc_at_ofs
1573 (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
1575 if (!gpdisp || gpdisp->r_addend != 4)
1577 if (tsec_free)
1578 free (tsec_free);
1579 return 0;
1581 if (tsec_free)
1582 free (tsec_free);
1585 /* We've now determined that we can skip an initial gp load. Verify
1586 that the call and the target use the same gp. */
1587 if (info->link_info->hash->creator != info->tsec->owner->xvec
1588 || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
1589 return 0;
1591 return symval + 8;
1594 static bfd_boolean
1595 elf64_alpha_relax_got_load (info, symval, irel, r_type)
1596 struct alpha_relax_info *info;
1597 bfd_vma symval;
1598 Elf_Internal_Rela *irel;
1599 unsigned long r_type;
1601 unsigned int insn;
1602 bfd_signed_vma disp;
1604 /* Get the instruction. */
1605 insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1607 if (insn >> 26 != OP_LDQ)
1609 reloc_howto_type *howto = elf64_alpha_howto_table + r_type;
1610 ((*_bfd_error_handler)
1611 ("%s: %s+0x%lx: warning: %s relocation against unexpected insn",
1612 bfd_archive_filename (info->abfd), info->sec->name,
1613 (unsigned long) irel->r_offset, howto->name));
1614 return TRUE;
1617 /* Can't relax dynamic symbols. */
1618 if (alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
1619 return TRUE;
1621 /* Can't use local-exec relocations in shared libraries. */
1622 if (r_type == R_ALPHA_GOTTPREL && info->link_info->shared)
1623 return TRUE;
1625 if (r_type == R_ALPHA_LITERAL)
1626 disp = symval - info->gp;
1627 else
1629 bfd_vma dtp_base, tp_base;
1631 BFD_ASSERT (info->tls_segment != NULL);
1632 dtp_base = alpha_get_dtprel_base (info->tls_segment);
1633 tp_base = alpha_get_tprel_base (info->tls_segment);
1634 disp = symval - (r_type == R_ALPHA_GOTDTPREL ? dtp_base : tp_base);
1637 if (disp < -0x8000 || disp >= 0x8000)
1638 return TRUE;
1640 /* Exchange LDQ for LDA. In the case of the TLS relocs, we're loading
1641 a constant, so force the base register to be $31. */
1642 if (r_type == R_ALPHA_LITERAL)
1643 insn = (OP_LDA << 26) | (insn & 0x03ff0000);
1644 else
1645 insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
1646 bfd_put_32 (info->abfd, (bfd_vma) insn, info->contents + irel->r_offset);
1647 info->changed_contents = TRUE;
1649 /* Reduce the use count on this got entry by one, possibly
1650 eliminating it. */
1651 if (--info->gotent->use_count == 0)
1653 int sz = alpha_got_entry_size (r_type);
1654 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1655 if (!info->h)
1656 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
1659 /* Smash the existing GOT relocation for its 16-bit immediate pair. */
1660 switch (r_type)
1662 case R_ALPHA_LITERAL:
1663 r_type = R_ALPHA_GPREL16;
1664 break;
1665 case R_ALPHA_GOTDTPREL:
1666 r_type = R_ALPHA_DTPREL16;
1667 break;
1668 case R_ALPHA_GOTTPREL:
1669 r_type = R_ALPHA_TPREL16;
1670 break;
1671 default:
1672 BFD_ASSERT (0);
1673 return FALSE;
1676 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), r_type);
1677 info->changed_relocs = TRUE;
1679 /* ??? Search forward through this basic block looking for insns
1680 that use the target register. Stop after an insn modifying the
1681 register is seen, or after a branch or call.
1683 Any such memory load insn may be substituted by a load directly
1684 off the GP. This allows the memory load insn to be issued before
1685 the calculated GP register would otherwise be ready.
1687 Any such jsr insn can be replaced by a bsr if it is in range.
1689 This would mean that we'd have to _add_ relocations, the pain of
1690 which gives one pause. */
1692 return TRUE;
1695 static bfd_boolean
1696 elf64_alpha_relax_gprelhilo (info, symval, irel, hi)
1697 struct alpha_relax_info *info;
1698 bfd_vma symval;
1699 Elf_Internal_Rela *irel;
1700 bfd_boolean hi;
1702 unsigned int insn;
1703 bfd_signed_vma disp;
1704 bfd_byte *pos = info->contents + irel->r_offset;
1706 /* ??? This assumes that the compiler doesn't render
1708 array[i]
1710 ldah t, array(gp) !gprelhigh
1711 s8addl i, t, t
1712 ldq r, array(t) !gprellow
1714 which would indeed be the most efficient way to implement this. */
1716 return TRUE;
1718 disp = symval - info->gp;
1719 if (disp < -0x8000 || disp >= 0x8000)
1720 return TRUE;
1722 if (hi)
1724 /* Nop out the high instruction. */
1726 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos);
1727 info->changed_contents = TRUE;
1729 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1730 irel->r_addend = 0;
1731 info->changed_relocs = TRUE;
1733 else
1735 /* Adjust the low instruction to reference GP directly. */
1737 insn = bfd_get_32 (info->abfd, pos);
1738 insn = (insn & 0xffe00000) | (29 << 16);
1739 bfd_put_32 (info->abfd, (bfd_vma) insn, pos);
1740 info->changed_contents = TRUE;
1742 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1743 R_ALPHA_GPREL16);
1744 info->changed_relocs = TRUE;
1747 return TRUE;
1750 static bfd_boolean
1751 elf64_alpha_relax_tls_get_addr (info, symval, irel, is_gd)
1752 struct alpha_relax_info *info;
1753 bfd_vma symval;
1754 Elf_Internal_Rela *irel;
1755 bfd_boolean is_gd;
1757 bfd_byte *pos[5];
1758 unsigned int insn;
1759 Elf_Internal_Rela *gpdisp, *hint;
1760 bfd_boolean dynamic, use_gottprel, pos1_unusable;
1761 unsigned long new_symndx;
1763 dynamic = alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info);
1765 /* If a TLS symbol is accessed using IE at least once, there is no point
1766 to use dynamic model for it. */
1767 if (is_gd && info->h && (info->h->flags & ALPHA_ELF_LINK_HASH_TLS_IE))
1770 /* If the symbol is local, and we've already committed to DF_STATIC_TLS,
1771 then we might as well relax to IE. */
1772 else if (info->link_info->shared && !dynamic
1773 && (info->link_info->flags & DF_STATIC_TLS))
1776 /* Otherwise we must be building an executable to do anything. */
1777 else if (info->link_info->shared)
1778 return TRUE;
1780 /* The TLSGD/TLSLDM relocation must be followed by a LITERAL and
1781 the matching LITUSE_TLS relocations. */
1782 if (irel + 2 >= info->relend)
1783 return TRUE;
1784 if (ELF64_R_TYPE (irel[1].r_info) != R_ALPHA_LITERAL
1785 || ELF64_R_TYPE (irel[2].r_info) != R_ALPHA_LITUSE
1786 || irel[2].r_addend != (is_gd ? LITUSE_ALPHA_TLSGD : LITUSE_ALPHA_TLSLDM))
1787 return TRUE;
1789 /* There must be a GPDISP relocation positioned immediately after the
1790 LITUSE relocation. */
1791 gpdisp = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
1792 irel[2].r_offset + 4, R_ALPHA_GPDISP);
1793 if (!gpdisp)
1794 return TRUE;
1796 pos[0] = info->contents + irel[0].r_offset;
1797 pos[1] = info->contents + irel[1].r_offset;
1798 pos[2] = info->contents + irel[2].r_offset;
1799 pos[3] = info->contents + gpdisp->r_offset;
1800 pos[4] = pos[3] + gpdisp->r_addend;
1801 pos1_unusable = FALSE;
1803 /* Generally, the positions are not allowed to be out of order, lest the
1804 modified insn sequence have different register lifetimes. We can make
1805 an exception when pos 1 is adjacent to pos 0. */
1806 if (pos[1] + 4 == pos[0])
1808 bfd_byte *tmp = pos[0];
1809 pos[0] = pos[1];
1810 pos[1] = tmp;
1812 else if (pos[1] < pos[0])
1813 pos1_unusable = TRUE;
1814 if (pos[1] >= pos[2] || pos[2] >= pos[3])
1815 return TRUE;
1817 /* Reduce the use count on the LITERAL relocation. Do this before we
1818 smash the symndx when we adjust the relocations below. */
1820 struct alpha_elf_got_entry *lit_gotent;
1821 struct alpha_elf_link_hash_entry *lit_h;
1822 unsigned long indx;
1824 BFD_ASSERT (ELF64_R_SYM (irel[1].r_info) >= info->symtab_hdr->sh_info);
1825 indx = ELF64_R_SYM (irel[1].r_info) - info->symtab_hdr->sh_info;
1826 lit_h = alpha_elf_sym_hashes (info->abfd)[indx];
1828 while (lit_h->root.root.type == bfd_link_hash_indirect
1829 || lit_h->root.root.type == bfd_link_hash_warning)
1830 lit_h = (struct alpha_elf_link_hash_entry *) lit_h->root.root.u.i.link;
1832 for (lit_gotent = lit_h->got_entries; lit_gotent ;
1833 lit_gotent = lit_gotent->next)
1834 if (lit_gotent->gotobj == info->gotobj
1835 && lit_gotent->reloc_type == R_ALPHA_LITERAL
1836 && lit_gotent->addend == irel[1].r_addend)
1837 break;
1838 BFD_ASSERT (lit_gotent);
1840 if (--lit_gotent->use_count == 0)
1842 int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
1843 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1847 /* Change
1849 lda $16,x($gp) !tlsgd!1
1850 ldq $27,__tls_get_addr($gp) !literal!1
1851 jsr $26,($27)__tls_get_addr !lituse_tlsgd!1
1852 ldah $29,0($26) !gpdisp!2
1853 lda $29,0($29) !gpdisp!2
1855 ldq $16,x($gp) !gottprel
1856 unop
1857 call_pal rduniq
1858 addq $16,$0,$0
1859 unop
1860 or the first pair to
1861 lda $16,x($gp) !tprel
1862 unop
1864 ldah $16,x($gp) !tprelhi
1865 lda $16,x($16) !tprello
1867 as appropriate. */
1869 use_gottprel = FALSE;
1870 new_symndx = is_gd ? ELF64_R_SYM (irel->r_info) : 0;
1871 switch (!dynamic && !info->link_info->shared)
1873 case 1:
1875 bfd_vma tp_base;
1876 bfd_signed_vma disp;
1878 BFD_ASSERT (info->tls_segment != NULL);
1879 tp_base = alpha_get_tprel_base (info->tls_segment);
1880 disp = symval - tp_base;
1882 if (disp >= -0x8000 && disp < 0x8000)
1884 insn = (OP_LDA << 26) | (16 << 21) | (31 << 16);
1885 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
1886 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
1888 irel[0].r_offset = pos[0] - info->contents;
1889 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPREL16);
1890 irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1891 break;
1893 else if (disp >= -(bfd_signed_vma) 0x80000000
1894 && disp < (bfd_signed_vma) 0x7fff8000
1895 && !pos1_unusable)
1897 insn = (OP_LDAH << 26) | (16 << 21) | (31 << 16);
1898 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
1899 insn = (OP_LDA << 26) | (16 << 21) | (16 << 16);
1900 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[1]);
1902 irel[0].r_offset = pos[0] - info->contents;
1903 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELHI);
1904 irel[1].r_offset = pos[1] - info->contents;
1905 irel[1].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELLO);
1906 break;
1909 /* FALLTHRU */
1911 default:
1912 use_gottprel = TRUE;
1914 insn = (OP_LDQ << 26) | (16 << 21) | (29 << 16);
1915 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
1916 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
1918 irel[0].r_offset = pos[0] - info->contents;
1919 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_GOTTPREL);
1920 irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1921 break;
1924 bfd_put_32 (info->abfd, (bfd_vma) INSN_RDUNIQ, pos[2]);
1926 insn = INSN_ADDQ | (16 << 21) | (0 << 16) | (0 << 0);
1927 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[3]);
1929 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[4]);
1931 irel[2].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1932 gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1934 hint = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
1935 irel[2].r_offset, R_ALPHA_HINT);
1936 if (hint)
1937 hint->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1939 info->changed_contents = TRUE;
1940 info->changed_relocs = TRUE;
1942 /* Reduce the use count on the TLSGD/TLSLDM relocation. */
1943 if (--info->gotent->use_count == 0)
1945 int sz = alpha_got_entry_size (info->gotent->reloc_type);
1946 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1947 if (!info->h)
1948 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
1951 /* If we've switched to a GOTTPREL relocation, increment the reference
1952 count on that got entry. */
1953 if (use_gottprel)
1955 struct alpha_elf_got_entry *tprel_gotent;
1957 for (tprel_gotent = *info->first_gotent; tprel_gotent ;
1958 tprel_gotent = tprel_gotent->next)
1959 if (tprel_gotent->gotobj == info->gotobj
1960 && tprel_gotent->reloc_type == R_ALPHA_GOTTPREL
1961 && tprel_gotent->addend == irel->r_addend)
1962 break;
1963 if (tprel_gotent)
1964 tprel_gotent->use_count++;
1965 else
1967 if (info->gotent->use_count == 0)
1968 tprel_gotent = info->gotent;
1969 else
1971 tprel_gotent = (struct alpha_elf_got_entry *)
1972 bfd_alloc (info->abfd, sizeof (struct alpha_elf_got_entry));
1973 if (!tprel_gotent)
1974 return FALSE;
1976 tprel_gotent->next = *info->first_gotent;
1977 *info->first_gotent = tprel_gotent;
1979 tprel_gotent->gotobj = info->gotobj;
1980 tprel_gotent->addend = irel->r_addend;
1981 tprel_gotent->got_offset = -1;
1982 tprel_gotent->reloc_done = 0;
1983 tprel_gotent->reloc_xlated = 0;
1986 tprel_gotent->use_count = 1;
1987 tprel_gotent->reloc_type = R_ALPHA_GOTTPREL;
1991 return TRUE;
1994 static struct elf_link_tls_segment *
1995 elf64_alpha_relax_find_tls_segment (info, seg)
1996 struct alpha_relax_info *info;
1997 struct elf_link_tls_segment *seg;
1999 bfd *output_bfd = info->sec->output_section->owner;
2000 asection *o;
2001 unsigned int align;
2002 bfd_vma base, end;
2004 for (o = output_bfd->sections; o ; o = o->next)
2005 if ((o->flags & SEC_THREAD_LOCAL) != 0
2006 && (o->flags & SEC_LOAD) != 0)
2007 break;
2008 if (!o)
2009 return NULL;
2011 base = o->vma;
2012 align = 0;
2016 bfd_vma size;
2018 if (bfd_get_section_alignment (output_bfd, o) > align)
2019 align = bfd_get_section_alignment (output_bfd, o);
2021 size = o->_raw_size;
2022 if (size == 0 && (o->flags & SEC_HAS_CONTENTS) == 0)
2024 struct bfd_link_order *lo;
2025 for (lo = o->link_order_head; lo ; lo = lo->next)
2026 if (size < lo->offset + lo->size)
2027 size = lo->offset + lo->size;
2029 end = o->vma + size;
2030 o = o->next;
2032 while (o && (o->flags & SEC_THREAD_LOCAL));
2034 seg->start = base;
2035 seg->size = end - base;
2036 seg->align = align;
2038 return seg;
2041 static bfd_boolean
2042 elf64_alpha_relax_section (abfd, sec, link_info, again)
2043 bfd *abfd;
2044 asection *sec;
2045 struct bfd_link_info *link_info;
2046 bfd_boolean *again;
2048 Elf_Internal_Shdr *symtab_hdr;
2049 Elf_Internal_Rela *internal_relocs;
2050 Elf_Internal_Rela *irel, *irelend;
2051 Elf_Internal_Sym *isymbuf = NULL;
2052 struct alpha_elf_got_entry **local_got_entries;
2053 struct alpha_relax_info info;
2054 struct elf_link_tls_segment tls_segment;
2056 /* We are not currently changing any sizes, so only one pass. */
2057 *again = FALSE;
2059 if (link_info->relocateable
2060 || (sec->flags & SEC_RELOC) == 0
2061 || sec->reloc_count == 0)
2062 return TRUE;
2064 /* If this is the first time we have been called for this section,
2065 initialize the cooked size. */
2066 if (sec->_cooked_size == 0)
2067 sec->_cooked_size = sec->_raw_size;
2069 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2070 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2072 /* Load the relocations for this section. */
2073 internal_relocs = (_bfd_elf64_link_read_relocs
2074 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2075 link_info->keep_memory));
2076 if (internal_relocs == NULL)
2077 return FALSE;
2079 memset(&info, 0, sizeof (info));
2080 info.abfd = abfd;
2081 info.sec = sec;
2082 info.link_info = link_info;
2083 info.symtab_hdr = symtab_hdr;
2084 info.relocs = internal_relocs;
2085 info.relend = irelend = internal_relocs + sec->reloc_count;
2087 /* Find the GP for this object. Do not store the result back via
2088 _bfd_set_gp_value, since this could change again before final. */
2089 info.gotobj = alpha_elf_tdata (abfd)->gotobj;
2090 if (info.gotobj)
2092 asection *sgot = alpha_elf_tdata (info.gotobj)->got;
2093 info.gp = (sgot->output_section->vma
2094 + sgot->output_offset
2095 + 0x8000);
2098 /* Get the section contents. */
2099 if (elf_section_data (sec)->this_hdr.contents != NULL)
2100 info.contents = elf_section_data (sec)->this_hdr.contents;
2101 else
2103 info.contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2104 if (info.contents == NULL)
2105 goto error_return;
2107 if (! bfd_get_section_contents (abfd, sec, info.contents,
2108 (file_ptr) 0, sec->_raw_size))
2109 goto error_return;
2112 /* Compute the TLS segment information. The version normally found in
2113 elf_hash_table (link_info)->tls_segment isn't built until final_link.
2114 ??? Probably should look into extracting this into a common function. */
2115 info.tls_segment = elf64_alpha_relax_find_tls_segment (&info, &tls_segment);
2117 for (irel = internal_relocs; irel < irelend; irel++)
2119 bfd_vma symval;
2120 struct alpha_elf_got_entry *gotent;
2121 unsigned long r_type = ELF64_R_TYPE (irel->r_info);
2122 unsigned long r_symndx = ELF64_R_SYM (irel->r_info);
2124 /* Early exit for unhandled or unrelaxable relocations. */
2125 switch (r_type)
2127 case R_ALPHA_LITERAL:
2128 case R_ALPHA_GPRELHIGH:
2129 case R_ALPHA_GPRELLOW:
2130 case R_ALPHA_GOTDTPREL:
2131 case R_ALPHA_GOTTPREL:
2132 case R_ALPHA_TLSGD:
2133 break;
2135 case R_ALPHA_TLSLDM:
2136 /* The symbol for a TLSLDM reloc is ignored. Collapse the
2137 reloc to the 0 symbol so that they all match. */
2138 r_symndx = 0;
2139 break;
2141 default:
2142 continue;
2145 /* Get the value of the symbol referred to by the reloc. */
2146 if (r_symndx < symtab_hdr->sh_info)
2148 /* A local symbol. */
2149 Elf_Internal_Sym *isym;
2151 /* Read this BFD's local symbols. */
2152 if (isymbuf == NULL)
2154 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2155 if (isymbuf == NULL)
2156 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2157 symtab_hdr->sh_info, 0,
2158 NULL, NULL, NULL);
2159 if (isymbuf == NULL)
2160 goto error_return;
2163 isym = isymbuf + r_symndx;
2165 /* Given the symbol for a TLSLDM reloc is ignored, this also
2166 means forcing the symbol value to the tp base. */
2167 if (r_type == R_ALPHA_TLSLDM)
2169 info.tsec = bfd_abs_section_ptr;
2170 symval = alpha_get_tprel_base (info.tls_segment);
2172 else
2174 symval = isym->st_value;
2175 if (isym->st_shndx == SHN_UNDEF)
2176 continue;
2177 else if (isym->st_shndx == SHN_ABS)
2178 info.tsec = bfd_abs_section_ptr;
2179 else if (isym->st_shndx == SHN_COMMON)
2180 info.tsec = bfd_com_section_ptr;
2181 else
2182 info.tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2185 info.h = NULL;
2186 info.other = isym->st_other;
2187 info.first_gotent = &local_got_entries[r_symndx];
2189 else
2191 unsigned long indx;
2192 struct alpha_elf_link_hash_entry *h;
2194 indx = r_symndx - symtab_hdr->sh_info;
2195 h = alpha_elf_sym_hashes (abfd)[indx];
2196 BFD_ASSERT (h != NULL);
2198 while (h->root.root.type == bfd_link_hash_indirect
2199 || h->root.root.type == bfd_link_hash_warning)
2200 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2202 /* If the symbol is undefined, we can't do anything with it. */
2203 if (h->root.root.type == bfd_link_hash_undefweak
2204 || h->root.root.type == bfd_link_hash_undefined)
2205 continue;
2207 /* If the symbol isn't defined in the current module, again
2208 we can't do anything. */
2209 if (!(h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2211 /* Except for TLSGD relocs, which can sometimes be
2212 relaxed to GOTTPREL relocs. */
2213 if (r_type != R_ALPHA_TLSGD)
2214 continue;
2215 info.tsec = bfd_abs_section_ptr;
2216 symval = 0;
2218 else
2220 info.tsec = h->root.root.u.def.section;
2221 symval = h->root.root.u.def.value;
2224 info.h = h;
2225 info.other = h->root.other;
2226 info.first_gotent = &h->got_entries;
2229 /* Search for the got entry to be used by this relocation. */
2230 for (gotent = *info.first_gotent; gotent ; gotent = gotent->next)
2231 if (gotent->gotobj == info.gotobj
2232 && gotent->reloc_type == r_type
2233 && gotent->addend == irel->r_addend)
2234 break;
2235 info.gotent = gotent;
2237 symval += info.tsec->output_section->vma + info.tsec->output_offset;
2238 symval += irel->r_addend;
2240 switch (r_type)
2242 case R_ALPHA_LITERAL:
2243 BFD_ASSERT(info.gotent != NULL);
2245 /* If there exist LITUSE relocations immediately following, this
2246 opens up all sorts of interesting optimizations, because we
2247 now know every location that this address load is used. */
2248 if (irel+1 < irelend
2249 && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
2251 if (!elf64_alpha_relax_with_lituse (&info, symval, irel))
2252 goto error_return;
2254 else
2256 if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
2257 goto error_return;
2259 break;
2261 case R_ALPHA_GPRELHIGH:
2262 case R_ALPHA_GPRELLOW:
2263 if (!elf64_alpha_relax_gprelhilo (&info, symval, irel,
2264 r_type == R_ALPHA_GPRELHIGH))
2265 goto error_return;
2266 break;
2268 case R_ALPHA_GOTDTPREL:
2269 case R_ALPHA_GOTTPREL:
2270 BFD_ASSERT(info.gotent != NULL);
2271 if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
2272 goto error_return;
2273 break;
2275 case R_ALPHA_TLSGD:
2276 case R_ALPHA_TLSLDM:
2277 BFD_ASSERT(info.gotent != NULL);
2278 if (!elf64_alpha_relax_tls_get_addr (&info, symval, irel,
2279 r_type == R_ALPHA_TLSGD))
2280 goto error_return;
2281 break;
2285 if (!elf64_alpha_size_plt_section (link_info))
2286 return FALSE;
2287 if (!elf64_alpha_size_got_sections (link_info))
2288 return FALSE;
2289 if (!elf64_alpha_size_rela_got_section (link_info))
2290 return FALSE;
2292 if (isymbuf != NULL
2293 && symtab_hdr->contents != (unsigned char *) isymbuf)
2295 if (!link_info->keep_memory)
2296 free (isymbuf);
2297 else
2299 /* Cache the symbols for elf_link_input_bfd. */
2300 symtab_hdr->contents = (unsigned char *) isymbuf;
2304 if (info.contents != NULL
2305 && elf_section_data (sec)->this_hdr.contents != info.contents)
2307 if (!info.changed_contents && !link_info->keep_memory)
2308 free (info.contents);
2309 else
2311 /* Cache the section contents for elf_link_input_bfd. */
2312 elf_section_data (sec)->this_hdr.contents = info.contents;
2316 if (elf_section_data (sec)->relocs != internal_relocs)
2318 if (!info.changed_relocs)
2319 free (internal_relocs);
2320 else
2321 elf_section_data (sec)->relocs = internal_relocs;
2324 *again = info.changed_contents || info.changed_relocs;
2326 return TRUE;
2328 error_return:
2329 if (isymbuf != NULL
2330 && symtab_hdr->contents != (unsigned char *) isymbuf)
2331 free (isymbuf);
2332 if (info.contents != NULL
2333 && elf_section_data (sec)->this_hdr.contents != info.contents)
2334 free (info.contents);
2335 if (internal_relocs != NULL
2336 && elf_section_data (sec)->relocs != internal_relocs)
2337 free (internal_relocs);
2338 return FALSE;
2341 /* PLT/GOT Stuff */
2342 #define PLT_HEADER_SIZE 32
2343 #define PLT_HEADER_WORD1 (bfd_vma) 0xc3600000 /* br $27,.+4 */
2344 #define PLT_HEADER_WORD2 (bfd_vma) 0xa77b000c /* ldq $27,12($27) */
2345 #define PLT_HEADER_WORD3 (bfd_vma) 0x47ff041f /* nop */
2346 #define PLT_HEADER_WORD4 (bfd_vma) 0x6b7b0000 /* jmp $27,($27) */
2348 #define PLT_ENTRY_SIZE 12
2349 #define PLT_ENTRY_WORD1 0xc3800000 /* br $28, plt0 */
2350 #define PLT_ENTRY_WORD2 0
2351 #define PLT_ENTRY_WORD3 0
2353 #define MAX_GOT_SIZE (64*1024)
2355 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
2357 /* Handle an Alpha specific section when reading an object file. This
2358 is called when elfcode.h finds a section with an unknown type.
2359 FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
2360 how to. */
2362 static bfd_boolean
2363 elf64_alpha_section_from_shdr (abfd, hdr, name)
2364 bfd *abfd;
2365 Elf_Internal_Shdr *hdr;
2366 const char *name;
2368 asection *newsect;
2370 /* There ought to be a place to keep ELF backend specific flags, but
2371 at the moment there isn't one. We just keep track of the
2372 sections by their name, instead. Fortunately, the ABI gives
2373 suggested names for all the MIPS specific sections, so we will
2374 probably get away with this. */
2375 switch (hdr->sh_type)
2377 case SHT_ALPHA_DEBUG:
2378 if (strcmp (name, ".mdebug") != 0)
2379 return FALSE;
2380 break;
2381 default:
2382 return FALSE;
2385 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2386 return FALSE;
2387 newsect = hdr->bfd_section;
2389 if (hdr->sh_type == SHT_ALPHA_DEBUG)
2391 if (! bfd_set_section_flags (abfd, newsect,
2392 (bfd_get_section_flags (abfd, newsect)
2393 | SEC_DEBUGGING)))
2394 return FALSE;
2397 return TRUE;
2400 /* Convert Alpha specific section flags to bfd internal section flags. */
2402 static bfd_boolean
2403 elf64_alpha_section_flags (flags, hdr)
2404 flagword *flags;
2405 Elf_Internal_Shdr *hdr;
2407 if (hdr->sh_flags & SHF_ALPHA_GPREL)
2408 *flags |= SEC_SMALL_DATA;
2410 return TRUE;
2413 /* Set the correct type for an Alpha ELF section. We do this by the
2414 section name, which is a hack, but ought to work. */
2416 static bfd_boolean
2417 elf64_alpha_fake_sections (abfd, hdr, sec)
2418 bfd *abfd;
2419 Elf_Internal_Shdr *hdr;
2420 asection *sec;
2422 register const char *name;
2424 name = bfd_get_section_name (abfd, sec);
2426 if (strcmp (name, ".mdebug") == 0)
2428 hdr->sh_type = SHT_ALPHA_DEBUG;
2429 /* In a shared object on Irix 5.3, the .mdebug section has an
2430 entsize of 0. FIXME: Does this matter? */
2431 if ((abfd->flags & DYNAMIC) != 0 )
2432 hdr->sh_entsize = 0;
2433 else
2434 hdr->sh_entsize = 1;
2436 else if ((sec->flags & SEC_SMALL_DATA)
2437 || strcmp (name, ".sdata") == 0
2438 || strcmp (name, ".sbss") == 0
2439 || strcmp (name, ".lit4") == 0
2440 || strcmp (name, ".lit8") == 0)
2441 hdr->sh_flags |= SHF_ALPHA_GPREL;
2443 return TRUE;
2446 /* Hook called by the linker routine which adds symbols from an object
2447 file. We use it to put .comm items in .sbss, and not .bss. */
2449 static bfd_boolean
2450 elf64_alpha_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2451 bfd *abfd;
2452 struct bfd_link_info *info;
2453 const Elf_Internal_Sym *sym;
2454 const char **namep ATTRIBUTE_UNUSED;
2455 flagword *flagsp ATTRIBUTE_UNUSED;
2456 asection **secp;
2457 bfd_vma *valp;
2459 if (sym->st_shndx == SHN_COMMON
2460 && !info->relocateable
2461 && sym->st_size <= elf_gp_size (abfd))
2463 /* Common symbols less than or equal to -G nn bytes are
2464 automatically put into .sbss. */
2466 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
2468 if (scomm == NULL)
2470 scomm = bfd_make_section (abfd, ".scommon");
2471 if (scomm == NULL
2472 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
2473 | SEC_IS_COMMON
2474 | SEC_LINKER_CREATED)))
2475 return FALSE;
2478 *secp = scomm;
2479 *valp = sym->st_size;
2482 return TRUE;
2485 /* Create the .got section. */
2487 static bfd_boolean
2488 elf64_alpha_create_got_section(abfd, info)
2489 bfd *abfd;
2490 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2492 asection *s;
2494 if (bfd_get_section_by_name (abfd, ".got"))
2495 return TRUE;
2497 s = bfd_make_section (abfd, ".got");
2498 if (s == NULL
2499 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2500 | SEC_HAS_CONTENTS
2501 | SEC_IN_MEMORY
2502 | SEC_LINKER_CREATED))
2503 || !bfd_set_section_alignment (abfd, s, 3))
2504 return FALSE;
2506 alpha_elf_tdata (abfd)->got = s;
2508 return TRUE;
2511 /* Create all the dynamic sections. */
2513 static bfd_boolean
2514 elf64_alpha_create_dynamic_sections (abfd, info)
2515 bfd *abfd;
2516 struct bfd_link_info *info;
2518 asection *s;
2519 struct elf_link_hash_entry *h;
2520 struct bfd_link_hash_entry *bh;
2522 /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
2524 s = bfd_make_section (abfd, ".plt");
2525 if (s == NULL
2526 || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2527 | SEC_HAS_CONTENTS
2528 | SEC_IN_MEMORY
2529 | SEC_LINKER_CREATED
2530 | SEC_CODE))
2531 || ! bfd_set_section_alignment (abfd, s, 3))
2532 return FALSE;
2534 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2535 .plt section. */
2536 bh = NULL;
2537 if (! (_bfd_generic_link_add_one_symbol
2538 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2539 (bfd_vma) 0, (const char *) NULL, FALSE,
2540 get_elf_backend_data (abfd)->collect, &bh)))
2541 return FALSE;
2542 h = (struct elf_link_hash_entry *) bh;
2543 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2544 h->type = STT_OBJECT;
2546 if (info->shared
2547 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2548 return FALSE;
2550 s = bfd_make_section (abfd, ".rela.plt");
2551 if (s == NULL
2552 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2553 | SEC_HAS_CONTENTS
2554 | SEC_IN_MEMORY
2555 | SEC_LINKER_CREATED
2556 | SEC_READONLY))
2557 || ! bfd_set_section_alignment (abfd, s, 3))
2558 return FALSE;
2560 /* We may or may not have created a .got section for this object, but
2561 we definitely havn't done the rest of the work. */
2563 if (!elf64_alpha_create_got_section (abfd, info))
2564 return FALSE;
2566 s = bfd_make_section(abfd, ".rela.got");
2567 if (s == NULL
2568 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2569 | SEC_HAS_CONTENTS
2570 | SEC_IN_MEMORY
2571 | SEC_LINKER_CREATED
2572 | SEC_READONLY))
2573 || !bfd_set_section_alignment (abfd, s, 3))
2574 return FALSE;
2576 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
2577 dynobj's .got section. We don't do this in the linker script
2578 because we don't want to define the symbol if we are not creating
2579 a global offset table. */
2580 bh = NULL;
2581 if (!(_bfd_generic_link_add_one_symbol
2582 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
2583 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
2584 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2585 return FALSE;
2586 h = (struct elf_link_hash_entry *) bh;
2587 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2588 h->type = STT_OBJECT;
2590 if (info->shared
2591 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2592 return FALSE;
2594 elf_hash_table (info)->hgot = h;
2596 return TRUE;
2599 /* Read ECOFF debugging information from a .mdebug section into a
2600 ecoff_debug_info structure. */
2602 static bfd_boolean
2603 elf64_alpha_read_ecoff_info (abfd, section, debug)
2604 bfd *abfd;
2605 asection *section;
2606 struct ecoff_debug_info *debug;
2608 HDRR *symhdr;
2609 const struct ecoff_debug_swap *swap;
2610 char *ext_hdr = NULL;
2612 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2613 memset (debug, 0, sizeof (*debug));
2615 ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
2616 if (ext_hdr == NULL && swap->external_hdr_size != 0)
2617 goto error_return;
2619 if (! bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2620 swap->external_hdr_size))
2621 goto error_return;
2623 symhdr = &debug->symbolic_header;
2624 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2626 /* The symbolic header contains absolute file offsets and sizes to
2627 read. */
2628 #define READ(ptr, offset, count, size, type) \
2629 if (symhdr->count == 0) \
2630 debug->ptr = NULL; \
2631 else \
2633 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
2634 debug->ptr = (type) bfd_malloc (amt); \
2635 if (debug->ptr == NULL) \
2636 goto error_return; \
2637 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2638 || bfd_bread (debug->ptr, amt, abfd) != amt) \
2639 goto error_return; \
2642 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2643 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2644 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2645 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2646 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2647 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2648 union aux_ext *);
2649 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2650 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2651 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2652 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2653 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2654 #undef READ
2656 debug->fdr = NULL;
2657 debug->adjust = NULL;
2659 return TRUE;
2661 error_return:
2662 if (ext_hdr != NULL)
2663 free (ext_hdr);
2664 if (debug->line != NULL)
2665 free (debug->line);
2666 if (debug->external_dnr != NULL)
2667 free (debug->external_dnr);
2668 if (debug->external_pdr != NULL)
2669 free (debug->external_pdr);
2670 if (debug->external_sym != NULL)
2671 free (debug->external_sym);
2672 if (debug->external_opt != NULL)
2673 free (debug->external_opt);
2674 if (debug->external_aux != NULL)
2675 free (debug->external_aux);
2676 if (debug->ss != NULL)
2677 free (debug->ss);
2678 if (debug->ssext != NULL)
2679 free (debug->ssext);
2680 if (debug->external_fdr != NULL)
2681 free (debug->external_fdr);
2682 if (debug->external_rfd != NULL)
2683 free (debug->external_rfd);
2684 if (debug->external_ext != NULL)
2685 free (debug->external_ext);
2686 return FALSE;
2689 /* Alpha ELF local labels start with '$'. */
2691 static bfd_boolean
2692 elf64_alpha_is_local_label_name (abfd, name)
2693 bfd *abfd ATTRIBUTE_UNUSED;
2694 const char *name;
2696 return name[0] == '$';
2699 /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
2700 routine in order to handle the ECOFF debugging information. We
2701 still call this mips_elf_find_line because of the slot
2702 find_line_info in elf_obj_tdata is declared that way. */
2704 struct mips_elf_find_line
2706 struct ecoff_debug_info d;
2707 struct ecoff_find_line i;
2710 static bfd_boolean
2711 elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2712 functionname_ptr, line_ptr)
2713 bfd *abfd;
2714 asection *section;
2715 asymbol **symbols;
2716 bfd_vma offset;
2717 const char **filename_ptr;
2718 const char **functionname_ptr;
2719 unsigned int *line_ptr;
2721 asection *msec;
2723 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2724 filename_ptr, functionname_ptr,
2725 line_ptr, 0,
2726 &elf_tdata (abfd)->dwarf2_find_line_info))
2727 return TRUE;
2729 msec = bfd_get_section_by_name (abfd, ".mdebug");
2730 if (msec != NULL)
2732 flagword origflags;
2733 struct mips_elf_find_line *fi;
2734 const struct ecoff_debug_swap * const swap =
2735 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2737 /* If we are called during a link, alpha_elf_final_link may have
2738 cleared the SEC_HAS_CONTENTS field. We force it back on here
2739 if appropriate (which it normally will be). */
2740 origflags = msec->flags;
2741 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2742 msec->flags |= SEC_HAS_CONTENTS;
2744 fi = elf_tdata (abfd)->find_line_info;
2745 if (fi == NULL)
2747 bfd_size_type external_fdr_size;
2748 char *fraw_src;
2749 char *fraw_end;
2750 struct fdr *fdr_ptr;
2751 bfd_size_type amt = sizeof (struct mips_elf_find_line);
2753 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
2754 if (fi == NULL)
2756 msec->flags = origflags;
2757 return FALSE;
2760 if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
2762 msec->flags = origflags;
2763 return FALSE;
2766 /* Swap in the FDR information. */
2767 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
2768 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
2769 if (fi->d.fdr == NULL)
2771 msec->flags = origflags;
2772 return FALSE;
2774 external_fdr_size = swap->external_fdr_size;
2775 fdr_ptr = fi->d.fdr;
2776 fraw_src = (char *) fi->d.external_fdr;
2777 fraw_end = (fraw_src
2778 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2779 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2780 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2782 elf_tdata (abfd)->find_line_info = fi;
2784 /* Note that we don't bother to ever free this information.
2785 find_nearest_line is either called all the time, as in
2786 objdump -l, so the information should be saved, or it is
2787 rarely called, as in ld error messages, so the memory
2788 wasted is unimportant. Still, it would probably be a
2789 good idea for free_cached_info to throw it away. */
2792 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2793 &fi->i, filename_ptr, functionname_ptr,
2794 line_ptr))
2796 msec->flags = origflags;
2797 return TRUE;
2800 msec->flags = origflags;
2803 /* Fall back on the generic ELF find_nearest_line routine. */
2805 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2806 filename_ptr, functionname_ptr,
2807 line_ptr);
2810 /* Structure used to pass information to alpha_elf_output_extsym. */
2812 struct extsym_info
2814 bfd *abfd;
2815 struct bfd_link_info *info;
2816 struct ecoff_debug_info *debug;
2817 const struct ecoff_debug_swap *swap;
2818 bfd_boolean failed;
2821 static bfd_boolean
2822 elf64_alpha_output_extsym (h, data)
2823 struct alpha_elf_link_hash_entry *h;
2824 PTR data;
2826 struct extsym_info *einfo = (struct extsym_info *) data;
2827 bfd_boolean strip;
2828 asection *sec, *output_section;
2830 if (h->root.root.type == bfd_link_hash_warning)
2831 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
2833 if (h->root.indx == -2)
2834 strip = FALSE;
2835 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2836 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2837 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2838 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2839 strip = TRUE;
2840 else if (einfo->info->strip == strip_all
2841 || (einfo->info->strip == strip_some
2842 && bfd_hash_lookup (einfo->info->keep_hash,
2843 h->root.root.root.string,
2844 FALSE, FALSE) == NULL))
2845 strip = TRUE;
2846 else
2847 strip = FALSE;
2849 if (strip)
2850 return TRUE;
2852 if (h->esym.ifd == -2)
2854 h->esym.jmptbl = 0;
2855 h->esym.cobol_main = 0;
2856 h->esym.weakext = 0;
2857 h->esym.reserved = 0;
2858 h->esym.ifd = ifdNil;
2859 h->esym.asym.value = 0;
2860 h->esym.asym.st = stGlobal;
2862 if (h->root.root.type != bfd_link_hash_defined
2863 && h->root.root.type != bfd_link_hash_defweak)
2864 h->esym.asym.sc = scAbs;
2865 else
2867 const char *name;
2869 sec = h->root.root.u.def.section;
2870 output_section = sec->output_section;
2872 /* When making a shared library and symbol h is the one from
2873 the another shared library, OUTPUT_SECTION may be null. */
2874 if (output_section == NULL)
2875 h->esym.asym.sc = scUndefined;
2876 else
2878 name = bfd_section_name (output_section->owner, output_section);
2880 if (strcmp (name, ".text") == 0)
2881 h->esym.asym.sc = scText;
2882 else if (strcmp (name, ".data") == 0)
2883 h->esym.asym.sc = scData;
2884 else if (strcmp (name, ".sdata") == 0)
2885 h->esym.asym.sc = scSData;
2886 else if (strcmp (name, ".rodata") == 0
2887 || strcmp (name, ".rdata") == 0)
2888 h->esym.asym.sc = scRData;
2889 else if (strcmp (name, ".bss") == 0)
2890 h->esym.asym.sc = scBss;
2891 else if (strcmp (name, ".sbss") == 0)
2892 h->esym.asym.sc = scSBss;
2893 else if (strcmp (name, ".init") == 0)
2894 h->esym.asym.sc = scInit;
2895 else if (strcmp (name, ".fini") == 0)
2896 h->esym.asym.sc = scFini;
2897 else
2898 h->esym.asym.sc = scAbs;
2902 h->esym.asym.reserved = 0;
2903 h->esym.asym.index = indexNil;
2906 if (h->root.root.type == bfd_link_hash_common)
2907 h->esym.asym.value = h->root.root.u.c.size;
2908 else if (h->root.root.type == bfd_link_hash_defined
2909 || h->root.root.type == bfd_link_hash_defweak)
2911 if (h->esym.asym.sc == scCommon)
2912 h->esym.asym.sc = scBss;
2913 else if (h->esym.asym.sc == scSCommon)
2914 h->esym.asym.sc = scSBss;
2916 sec = h->root.root.u.def.section;
2917 output_section = sec->output_section;
2918 if (output_section != NULL)
2919 h->esym.asym.value = (h->root.root.u.def.value
2920 + sec->output_offset
2921 + output_section->vma);
2922 else
2923 h->esym.asym.value = 0;
2925 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2927 /* Set type and value for a symbol with a function stub. */
2928 h->esym.asym.st = stProc;
2929 sec = bfd_get_section_by_name (einfo->abfd, ".plt");
2930 if (sec == NULL)
2931 h->esym.asym.value = 0;
2932 else
2934 output_section = sec->output_section;
2935 if (output_section != NULL)
2936 h->esym.asym.value = (h->root.plt.offset
2937 + sec->output_offset
2938 + output_section->vma);
2939 else
2940 h->esym.asym.value = 0;
2944 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2945 h->root.root.root.string,
2946 &h->esym))
2948 einfo->failed = TRUE;
2949 return FALSE;
2952 return TRUE;
2955 /* Search for and possibly create a got entry. */
2957 static struct alpha_elf_got_entry *
2958 get_got_entry (abfd, h, r_type, r_symndx, r_addend)
2959 bfd *abfd;
2960 struct alpha_elf_link_hash_entry *h;
2961 unsigned long r_type, r_symndx;
2962 bfd_vma r_addend;
2964 struct alpha_elf_got_entry *gotent;
2965 struct alpha_elf_got_entry **slot;
2967 if (h)
2968 slot = &h->got_entries;
2969 else
2971 /* This is a local .got entry -- record for merge. */
2973 struct alpha_elf_got_entry **local_got_entries;
2975 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2976 if (!local_got_entries)
2978 bfd_size_type size;
2979 Elf_Internal_Shdr *symtab_hdr;
2981 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2982 size = symtab_hdr->sh_info;
2983 size *= sizeof (struct alpha_elf_got_entry *);
2985 local_got_entries
2986 = (struct alpha_elf_got_entry **) bfd_zalloc (abfd, size);
2987 if (!local_got_entries)
2988 return NULL;
2990 alpha_elf_tdata (abfd)->local_got_entries = local_got_entries;
2993 slot = &local_got_entries[r_symndx];
2996 for (gotent = *slot; gotent ; gotent = gotent->next)
2997 if (gotent->gotobj == abfd
2998 && gotent->reloc_type == r_type
2999 && gotent->addend == r_addend)
3000 break;
3002 if (!gotent)
3004 int entry_size;
3005 bfd_size_type amt;
3007 amt = sizeof (struct alpha_elf_got_entry);
3008 gotent = (struct alpha_elf_got_entry *) bfd_alloc (abfd, amt);
3009 if (!gotent)
3010 return NULL;
3012 gotent->gotobj = abfd;
3013 gotent->addend = r_addend;
3014 gotent->got_offset = -1;
3015 gotent->use_count = 1;
3016 gotent->reloc_type = r_type;
3017 gotent->reloc_done = 0;
3018 gotent->reloc_xlated = 0;
3020 gotent->next = *slot;
3021 *slot = gotent;
3023 entry_size = alpha_got_entry_size (r_type);
3024 alpha_elf_tdata (abfd)->total_got_size += entry_size;
3025 if (!h)
3026 alpha_elf_tdata(abfd)->local_got_size += entry_size;
3028 else
3029 gotent->use_count += 1;
3031 return gotent;
3034 /* Handle dynamic relocations when doing an Alpha ELF link. */
3036 static bfd_boolean
3037 elf64_alpha_check_relocs (abfd, info, sec, relocs)
3038 bfd *abfd;
3039 struct bfd_link_info *info;
3040 asection *sec;
3041 const Elf_Internal_Rela *relocs;
3043 bfd *dynobj;
3044 asection *sreloc;
3045 const char *rel_sec_name;
3046 Elf_Internal_Shdr *symtab_hdr;
3047 struct alpha_elf_link_hash_entry **sym_hashes;
3048 const Elf_Internal_Rela *rel, *relend;
3049 bfd_boolean got_created;
3050 bfd_size_type amt;
3052 if (info->relocateable)
3053 return TRUE;
3055 dynobj = elf_hash_table(info)->dynobj;
3056 if (dynobj == NULL)
3057 elf_hash_table(info)->dynobj = dynobj = abfd;
3059 sreloc = NULL;
3060 rel_sec_name = NULL;
3061 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
3062 sym_hashes = alpha_elf_sym_hashes(abfd);
3063 got_created = FALSE;
3065 relend = relocs + sec->reloc_count;
3066 for (rel = relocs; rel < relend; ++rel)
3068 enum {
3069 NEED_GOT = 1,
3070 NEED_GOT_ENTRY = 2,
3071 NEED_DYNREL = 4
3074 unsigned long r_symndx, r_type;
3075 struct alpha_elf_link_hash_entry *h;
3076 unsigned int gotent_flags;
3077 bfd_boolean maybe_dynamic;
3078 unsigned int need;
3079 bfd_vma addend;
3081 r_symndx = ELF64_R_SYM (rel->r_info);
3082 if (r_symndx < symtab_hdr->sh_info)
3083 h = NULL;
3084 else
3086 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3088 while (h->root.root.type == bfd_link_hash_indirect
3089 || h->root.root.type == bfd_link_hash_warning)
3090 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3092 h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3095 /* We can only get preliminary data on whether a symbol is
3096 locally or externally defined, as not all of the input files
3097 have yet been processed. Do something with what we know, as
3098 this may help reduce memory usage and processing time later. */
3099 maybe_dynamic = FALSE;
3100 if (h && ((info->shared
3101 && (!info->symbolic || info->allow_shlib_undefined))
3102 || ! (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
3103 || h->root.root.type == bfd_link_hash_defweak))
3104 maybe_dynamic = TRUE;
3106 need = 0;
3107 gotent_flags = 0;
3108 r_type = ELF64_R_TYPE (rel->r_info);
3109 addend = rel->r_addend;
3111 switch (r_type)
3113 case R_ALPHA_LITERAL:
3114 need = NEED_GOT | NEED_GOT_ENTRY;
3116 /* Remember how this literal is used from its LITUSEs.
3117 This will be important when it comes to decide if we can
3118 create a .plt entry for a function symbol. */
3119 while (++rel < relend && ELF64_R_TYPE (rel->r_info) == R_ALPHA_LITUSE)
3120 if (rel->r_addend >= 1 && rel->r_addend <= 5)
3121 gotent_flags |= 1 << rel->r_addend;
3122 --rel;
3124 /* No LITUSEs -- presumably the address is used somehow. */
3125 if (gotent_flags == 0)
3126 gotent_flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
3127 break;
3129 case R_ALPHA_GPDISP:
3130 case R_ALPHA_GPREL16:
3131 case R_ALPHA_GPREL32:
3132 case R_ALPHA_GPRELHIGH:
3133 case R_ALPHA_GPRELLOW:
3134 case R_ALPHA_BRSGP:
3135 need = NEED_GOT;
3136 break;
3138 case R_ALPHA_REFLONG:
3139 case R_ALPHA_REFQUAD:
3140 if ((info->shared && (sec->flags & SEC_ALLOC)) || maybe_dynamic)
3141 need = NEED_DYNREL;
3142 break;
3144 case R_ALPHA_TLSLDM:
3145 /* The symbol for a TLSLDM reloc is ignored. Collapse the
3146 reloc to the 0 symbol so that they all match. */
3147 r_symndx = 0;
3148 h = 0;
3149 maybe_dynamic = FALSE;
3150 /* FALLTHRU */
3152 case R_ALPHA_TLSGD:
3153 case R_ALPHA_GOTDTPREL:
3154 need = NEED_GOT | NEED_GOT_ENTRY;
3155 break;
3157 case R_ALPHA_GOTTPREL:
3158 need = NEED_GOT | NEED_GOT_ENTRY;
3159 gotent_flags = ALPHA_ELF_LINK_HASH_TLS_IE;
3160 if (info->shared)
3161 info->flags |= DF_STATIC_TLS;
3162 break;
3164 case R_ALPHA_TPREL64:
3165 if (info->shared || maybe_dynamic)
3166 need = NEED_DYNREL;
3167 if (info->shared)
3168 info->flags |= DF_STATIC_TLS;
3169 break;
3172 if (need & NEED_GOT)
3174 if (!got_created)
3176 if (!elf64_alpha_create_got_section (abfd, info))
3177 return FALSE;
3179 /* Make sure the object's gotobj is set to itself so
3180 that we default to every object with its own .got.
3181 We'll merge .gots later once we've collected each
3182 object's info. */
3183 alpha_elf_tdata(abfd)->gotobj = abfd;
3185 got_created = 1;
3189 if (need & NEED_GOT_ENTRY)
3191 struct alpha_elf_got_entry *gotent;
3193 gotent = get_got_entry (abfd, h, r_type, r_symndx, addend);
3194 if (!gotent)
3195 return FALSE;
3197 if (gotent_flags)
3199 gotent->flags |= gotent_flags;
3200 if (h)
3202 gotent_flags |= h->flags;
3203 h->flags = gotent_flags;
3205 /* Make a guess as to whether a .plt entry is needed. */
3206 if ((gotent_flags & ALPHA_ELF_LINK_HASH_LU_FUNC)
3207 && !(gotent_flags & ~ALPHA_ELF_LINK_HASH_LU_FUNC))
3208 h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3209 else
3210 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3215 if (need & NEED_DYNREL)
3217 if (rel_sec_name == NULL)
3219 rel_sec_name = (bfd_elf_string_from_elf_section
3220 (abfd, elf_elfheader(abfd)->e_shstrndx,
3221 elf_section_data(sec)->rel_hdr.sh_name));
3222 if (rel_sec_name == NULL)
3223 return FALSE;
3225 BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
3226 && strcmp (bfd_get_section_name (abfd, sec),
3227 rel_sec_name+5) == 0);
3230 /* We need to create the section here now whether we eventually
3231 use it or not so that it gets mapped to an output section by
3232 the linker. If not used, we'll kill it in
3233 size_dynamic_sections. */
3234 if (sreloc == NULL)
3236 sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
3237 if (sreloc == NULL)
3239 flagword flags;
3241 sreloc = bfd_make_section (dynobj, rel_sec_name);
3242 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
3243 | SEC_LINKER_CREATED | SEC_READONLY);
3244 if (sec->flags & SEC_ALLOC)
3245 flags |= SEC_ALLOC | SEC_LOAD;
3246 if (sreloc == NULL
3247 || !bfd_set_section_flags (dynobj, sreloc, flags)
3248 || !bfd_set_section_alignment (dynobj, sreloc, 3))
3249 return FALSE;
3253 if (h)
3255 /* Since we havn't seen all of the input symbols yet, we
3256 don't know whether we'll actually need a dynamic relocation
3257 entry for this reloc. So make a record of it. Once we
3258 find out if this thing needs dynamic relocation we'll
3259 expand the relocation sections by the appropriate amount. */
3261 struct alpha_elf_reloc_entry *rent;
3263 for (rent = h->reloc_entries; rent; rent = rent->next)
3264 if (rent->rtype == r_type && rent->srel == sreloc)
3265 break;
3267 if (!rent)
3269 amt = sizeof (struct alpha_elf_reloc_entry);
3270 rent = (struct alpha_elf_reloc_entry *) bfd_alloc (abfd, amt);
3271 if (!rent)
3272 return FALSE;
3274 rent->srel = sreloc;
3275 rent->rtype = r_type;
3276 rent->count = 1;
3277 rent->reltext = ((sec->flags & (SEC_READONLY | SEC_ALLOC))
3278 == (SEC_READONLY | SEC_ALLOC));
3280 rent->next = h->reloc_entries;
3281 h->reloc_entries = rent;
3283 else
3284 rent->count++;
3286 else if (info->shared)
3288 /* If this is a shared library, and the section is to be
3289 loaded into memory, we need a RELATIVE reloc. */
3290 sreloc->_raw_size += sizeof (Elf64_External_Rela);
3291 if ((sec->flags & (SEC_READONLY | SEC_ALLOC))
3292 == (SEC_READONLY | SEC_ALLOC))
3293 info->flags |= DF_TEXTREL;
3298 return TRUE;
3301 /* Adjust a symbol defined by a dynamic object and referenced by a
3302 regular object. The current definition is in some section of the
3303 dynamic object, but we're not including those sections. We have to
3304 change the definition to something the rest of the link can
3305 understand. */
3307 static bfd_boolean
3308 elf64_alpha_adjust_dynamic_symbol (info, h)
3309 struct bfd_link_info *info;
3310 struct elf_link_hash_entry *h;
3312 bfd *dynobj;
3313 asection *s;
3314 struct alpha_elf_link_hash_entry *ah;
3316 dynobj = elf_hash_table(info)->dynobj;
3317 ah = (struct alpha_elf_link_hash_entry *)h;
3319 /* Now that we've seen all of the input symbols, finalize our decision
3320 about whether this symbol should get a .plt entry. */
3322 if (alpha_elf_dynamic_symbol_p (h, info)
3323 && ((h->type == STT_FUNC
3324 && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
3325 || (h->type == STT_NOTYPE
3326 && (ah->flags & ALPHA_ELF_LINK_HASH_LU_FUNC)
3327 && !(ah->flags & ~ALPHA_ELF_LINK_HASH_LU_FUNC)))
3328 /* Don't prevent otherwise valid programs from linking by attempting
3329 to create a new .got entry somewhere. A Correct Solution would be
3330 to add a new .got section to a new object file and let it be merged
3331 somewhere later. But for now don't bother. */
3332 && ah->got_entries)
3334 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3336 s = bfd_get_section_by_name(dynobj, ".plt");
3337 if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
3338 return FALSE;
3340 /* The first bit of the .plt is reserved. */
3341 if (s->_raw_size == 0)
3342 s->_raw_size = PLT_HEADER_SIZE;
3344 h->plt.offset = s->_raw_size;
3345 s->_raw_size += PLT_ENTRY_SIZE;
3347 /* If this symbol is not defined in a regular file, and we are not
3348 generating a shared library, then set the symbol to the location
3349 in the .plt. This is required to make function pointers compare
3350 equal between the normal executable and the shared library. */
3351 if (! info->shared
3352 && h->root.type != bfd_link_hash_defweak)
3354 ah->plt_old_section = h->root.u.def.section;
3355 ah->plt_old_value = h->root.u.def.value;
3356 ah->flags |= ALPHA_ELF_LINK_HASH_PLT_LOC;
3357 h->root.u.def.section = s;
3358 h->root.u.def.value = h->plt.offset;
3361 /* We also need a JMP_SLOT entry in the .rela.plt section. */
3362 s = bfd_get_section_by_name (dynobj, ".rela.plt");
3363 BFD_ASSERT (s != NULL);
3364 s->_raw_size += sizeof (Elf64_External_Rela);
3366 return TRUE;
3368 else
3369 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3371 /* If this is a weak symbol, and there is a real definition, the
3372 processor independent code will have arranged for us to see the
3373 real definition first, and we can just use the same value. */
3374 if (h->weakdef != NULL)
3376 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3377 || h->weakdef->root.type == bfd_link_hash_defweak);
3378 h->root.u.def.section = h->weakdef->root.u.def.section;
3379 h->root.u.def.value = h->weakdef->root.u.def.value;
3380 return TRUE;
3383 /* This is a reference to a symbol defined by a dynamic object which
3384 is not a function. The Alpha, since it uses .got entries for all
3385 symbols even in regular objects, does not need the hackery of a
3386 .dynbss section and COPY dynamic relocations. */
3388 return TRUE;
3391 /* Symbol versioning can create new symbols, and make our old symbols
3392 indirect to the new ones. Consolidate the got and reloc information
3393 in these situations. */
3395 static bfd_boolean
3396 elf64_alpha_merge_ind_symbols (hi, dummy)
3397 struct alpha_elf_link_hash_entry *hi;
3398 PTR dummy ATTRIBUTE_UNUSED;
3400 struct alpha_elf_link_hash_entry *hs;
3402 if (hi->root.root.type != bfd_link_hash_indirect)
3403 return TRUE;
3404 hs = hi;
3405 do {
3406 hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
3407 } while (hs->root.root.type == bfd_link_hash_indirect);
3409 /* Merge the flags. Whee. */
3411 hs->flags |= hi->flags;
3413 /* Merge the .got entries. Cannibalize the old symbol's list in
3414 doing so, since we don't need it anymore. */
3416 if (hs->got_entries == NULL)
3417 hs->got_entries = hi->got_entries;
3418 else
3420 struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
3422 gsh = hs->got_entries;
3423 for (gi = hi->got_entries; gi ; gi = gin)
3425 gin = gi->next;
3426 for (gs = gsh; gs ; gs = gs->next)
3427 if (gi->gotobj == gs->gotobj
3428 && gi->reloc_type == gs->reloc_type
3429 && gi->addend == gs->addend)
3431 gi->use_count += gs->use_count;
3432 goto got_found;
3434 gi->next = hs->got_entries;
3435 hs->got_entries = gi;
3436 got_found:;
3439 hi->got_entries = NULL;
3441 /* And similar for the reloc entries. */
3443 if (hs->reloc_entries == NULL)
3444 hs->reloc_entries = hi->reloc_entries;
3445 else
3447 struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
3449 rsh = hs->reloc_entries;
3450 for (ri = hi->reloc_entries; ri ; ri = rin)
3452 rin = ri->next;
3453 for (rs = rsh; rs ; rs = rs->next)
3454 if (ri->rtype == rs->rtype && ri->srel == rs->srel)
3456 rs->count += ri->count;
3457 goto found_reloc;
3459 ri->next = hs->reloc_entries;
3460 hs->reloc_entries = ri;
3461 found_reloc:;
3464 hi->reloc_entries = NULL;
3466 return TRUE;
3469 /* Is it possible to merge two object file's .got tables? */
3471 static bfd_boolean
3472 elf64_alpha_can_merge_gots (a, b)
3473 bfd *a, *b;
3475 int total = alpha_elf_tdata (a)->total_got_size;
3476 bfd *bsub;
3478 /* Trivial quick fallout test. */
3479 if (total + alpha_elf_tdata (b)->total_got_size <= MAX_GOT_SIZE)
3480 return TRUE;
3482 /* By their nature, local .got entries cannot be merged. */
3483 if ((total += alpha_elf_tdata (b)->local_got_size) > MAX_GOT_SIZE)
3484 return FALSE;
3486 /* Failing the common trivial comparison, we must effectively
3487 perform the merge. Not actually performing the merge means that
3488 we don't have to store undo information in case we fail. */
3489 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
3491 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
3492 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
3493 int i, n;
3495 n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
3496 for (i = 0; i < n; ++i)
3498 struct alpha_elf_got_entry *ae, *be;
3499 struct alpha_elf_link_hash_entry *h;
3501 h = hashes[i];
3502 while (h->root.root.type == bfd_link_hash_indirect
3503 || h->root.root.type == bfd_link_hash_warning)
3504 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3506 for (be = h->got_entries; be ; be = be->next)
3508 if (be->use_count == 0)
3509 continue;
3510 if (be->gotobj != b)
3511 continue;
3513 for (ae = h->got_entries; ae ; ae = ae->next)
3514 if (ae->gotobj == a
3515 && ae->reloc_type == be->reloc_type
3516 && ae->addend == be->addend)
3517 goto global_found;
3519 total += alpha_got_entry_size (be->reloc_type);
3520 if (total > MAX_GOT_SIZE)
3521 return FALSE;
3522 global_found:;
3527 return TRUE;
3530 /* Actually merge two .got tables. */
3532 static void
3533 elf64_alpha_merge_gots (a, b)
3534 bfd *a, *b;
3536 int total = alpha_elf_tdata (a)->total_got_size;
3537 bfd *bsub;
3539 /* Remember local expansion. */
3541 int e = alpha_elf_tdata (b)->local_got_size;
3542 total += e;
3543 alpha_elf_tdata (a)->local_got_size += e;
3546 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
3548 struct alpha_elf_got_entry **local_got_entries;
3549 struct alpha_elf_link_hash_entry **hashes;
3550 Elf_Internal_Shdr *symtab_hdr;
3551 int i, n;
3553 /* Let the local .got entries know they are part of a new subsegment. */
3554 local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
3555 if (local_got_entries)
3557 n = elf_tdata (bsub)->symtab_hdr.sh_info;
3558 for (i = 0; i < n; ++i)
3560 struct alpha_elf_got_entry *ent;
3561 for (ent = local_got_entries[i]; ent; ent = ent->next)
3562 ent->gotobj = a;
3566 /* Merge the global .got entries. */
3567 hashes = alpha_elf_sym_hashes (bsub);
3568 symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
3570 n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
3571 for (i = 0; i < n; ++i)
3573 struct alpha_elf_got_entry *ae, *be, **pbe, **start;
3574 struct alpha_elf_link_hash_entry *h;
3576 h = hashes[i];
3577 while (h->root.root.type == bfd_link_hash_indirect
3578 || h->root.root.type == bfd_link_hash_warning)
3579 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3581 start = &h->got_entries;
3582 for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
3584 if (be->use_count == 0)
3586 *pbe = be->next;
3587 continue;
3589 if (be->gotobj != b)
3590 continue;
3592 for (ae = *start; ae ; ae = ae->next)
3593 if (ae->gotobj == a
3594 && ae->reloc_type == be->reloc_type
3595 && ae->addend == be->addend)
3597 ae->flags |= be->flags;
3598 ae->use_count += be->use_count;
3599 *pbe = be->next;
3600 goto global_found;
3602 be->gotobj = a;
3603 total += alpha_got_entry_size (be->reloc_type);
3605 global_found:;
3609 alpha_elf_tdata (bsub)->gotobj = a;
3611 alpha_elf_tdata (a)->total_got_size = total;
3613 /* Merge the two in_got chains. */
3615 bfd *next;
3617 bsub = a;
3618 while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
3619 bsub = next;
3621 alpha_elf_tdata (bsub)->in_got_link_next = b;
3625 /* Calculate the offsets for the got entries. */
3627 static bfd_boolean
3628 elf64_alpha_calc_got_offsets_for_symbol (h, arg)
3629 struct alpha_elf_link_hash_entry *h;
3630 PTR arg ATTRIBUTE_UNUSED;
3632 struct alpha_elf_got_entry *gotent;
3634 if (h->root.root.type == bfd_link_hash_warning)
3635 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3637 for (gotent = h->got_entries; gotent; gotent = gotent->next)
3638 if (gotent->use_count > 0)
3640 bfd_size_type *plge
3641 = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
3643 gotent->got_offset = *plge;
3644 *plge += alpha_got_entry_size (gotent->reloc_type);
3647 return TRUE;
3650 static void
3651 elf64_alpha_calc_got_offsets (info)
3652 struct bfd_link_info *info;
3654 bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
3656 /* First, zero out the .got sizes, as we may be recalculating the
3657 .got after optimizing it. */
3658 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3659 alpha_elf_tdata(i)->got->_raw_size = 0;
3661 /* Next, fill in the offsets for all the global entries. */
3662 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3663 elf64_alpha_calc_got_offsets_for_symbol,
3664 NULL);
3666 /* Finally, fill in the offsets for the local entries. */
3667 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3669 bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
3670 bfd *j;
3672 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3674 struct alpha_elf_got_entry **local_got_entries, *gotent;
3675 int k, n;
3677 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3678 if (!local_got_entries)
3679 continue;
3681 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3682 for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
3683 if (gotent->use_count > 0)
3685 gotent->got_offset = got_offset;
3686 got_offset += alpha_got_entry_size (gotent->reloc_type);
3690 alpha_elf_tdata(i)->got->_raw_size = got_offset;
3691 alpha_elf_tdata(i)->got->_cooked_size = got_offset;
3695 /* Constructs the gots. */
3697 static bfd_boolean
3698 elf64_alpha_size_got_sections (info)
3699 struct bfd_link_info *info;
3701 bfd *i, *got_list, *cur_got_obj = NULL;
3702 int something_changed = 0;
3704 got_list = alpha_elf_hash_table (info)->got_list;
3706 /* On the first time through, pretend we have an existing got list
3707 consisting of all of the input files. */
3708 if (got_list == NULL)
3710 for (i = info->input_bfds; i ; i = i->link_next)
3712 bfd *this_got = alpha_elf_tdata (i)->gotobj;
3713 if (this_got == NULL)
3714 continue;
3716 /* We are assuming no merging has yet ocurred. */
3717 BFD_ASSERT (this_got == i);
3719 if (alpha_elf_tdata (this_got)->total_got_size > MAX_GOT_SIZE)
3721 /* Yikes! A single object file has too many entries. */
3722 (*_bfd_error_handler)
3723 (_("%s: .got subsegment exceeds 64K (size %d)"),
3724 bfd_archive_filename (i),
3725 alpha_elf_tdata (this_got)->total_got_size);
3726 return FALSE;
3729 if (got_list == NULL)
3730 got_list = this_got;
3731 else
3732 alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
3733 cur_got_obj = this_got;
3736 /* Strange degenerate case of no got references. */
3737 if (got_list == NULL)
3738 return TRUE;
3740 alpha_elf_hash_table (info)->got_list = got_list;
3742 /* Force got offsets to be recalculated. */
3743 something_changed = 1;
3746 cur_got_obj = got_list;
3747 i = alpha_elf_tdata(cur_got_obj)->got_link_next;
3748 while (i != NULL)
3750 if (elf64_alpha_can_merge_gots (cur_got_obj, i))
3752 elf64_alpha_merge_gots (cur_got_obj, i);
3753 i = alpha_elf_tdata(i)->got_link_next;
3754 alpha_elf_tdata(cur_got_obj)->got_link_next = i;
3755 something_changed = 1;
3757 else
3759 cur_got_obj = i;
3760 i = alpha_elf_tdata(i)->got_link_next;
3764 /* Once the gots have been merged, fill in the got offsets for
3765 everything therein. */
3766 if (1 || something_changed)
3767 elf64_alpha_calc_got_offsets (info);
3769 return TRUE;
3772 /* Called from relax_section to rebuild the PLT in light of
3773 potential changes in the function's status. */
3775 static bfd_boolean
3776 elf64_alpha_size_plt_section (info)
3777 struct bfd_link_info *info;
3779 asection *splt, *spltrel;
3780 unsigned long entries;
3781 bfd *dynobj;
3783 dynobj = elf_hash_table(info)->dynobj;
3784 splt = bfd_get_section_by_name(dynobj, ".plt");
3785 if (splt == NULL)
3786 return TRUE;
3788 splt->_raw_size = 0;
3790 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3791 elf64_alpha_size_plt_section_1, splt);
3793 splt->_cooked_size = splt->_raw_size;
3795 /* Every plt entry requires a JMP_SLOT relocation. */
3796 spltrel = bfd_get_section_by_name (dynobj, ".rela.plt");
3797 if (splt->_raw_size)
3798 entries = (splt->_raw_size - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3799 else
3800 entries = 0;
3801 spltrel->_raw_size = entries * sizeof (Elf64_External_Rela);
3802 spltrel->_cooked_size = spltrel->_raw_size;
3804 return TRUE;
3807 static bfd_boolean
3808 elf64_alpha_size_plt_section_1 (h, data)
3809 struct alpha_elf_link_hash_entry *h;
3810 PTR data;
3812 asection *splt = (asection *) data;
3813 struct alpha_elf_got_entry *gotent;
3815 /* If we didn't need an entry before, we still don't. */
3816 if (!(h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT))
3817 return TRUE;
3819 /* There must still be a LITERAL got entry for the function. */
3820 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3821 if (gotent->reloc_type == R_ALPHA_LITERAL
3822 && gotent->use_count > 0)
3823 break;
3825 /* If there is, reset the PLT offset. If not, there's no longer
3826 a need for the PLT entry. */
3827 if (gotent)
3829 if (splt->_raw_size == 0)
3830 splt->_raw_size = PLT_HEADER_SIZE;
3831 h->root.plt.offset = splt->_raw_size;
3832 splt->_raw_size += PLT_ENTRY_SIZE;
3834 else
3836 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3837 h->root.plt.offset = -1;
3839 /* Undo the definition frobbing begun in adjust_dynamic_symbol. */
3840 if (h->flags & ALPHA_ELF_LINK_HASH_PLT_LOC)
3842 h->root.root.u.def.section = h->plt_old_section;
3843 h->root.root.u.def.value = h->plt_old_value;
3844 h->flags &= ~ALPHA_ELF_LINK_HASH_PLT_LOC;
3848 return TRUE;
3851 static bfd_boolean
3852 elf64_alpha_always_size_sections (output_bfd, info)
3853 bfd *output_bfd ATTRIBUTE_UNUSED;
3854 struct bfd_link_info *info;
3856 bfd *i;
3858 if (info->relocateable)
3859 return TRUE;
3861 /* First, take care of the indirect symbols created by versioning. */
3862 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3863 elf64_alpha_merge_ind_symbols,
3864 NULL);
3866 if (!elf64_alpha_size_got_sections (info))
3867 return FALSE;
3869 /* Allocate space for all of the .got subsections. */
3870 i = alpha_elf_hash_table (info)->got_list;
3871 for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
3873 asection *s = alpha_elf_tdata(i)->got;
3874 if (s->_raw_size > 0)
3876 s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
3877 if (s->contents == NULL)
3878 return FALSE;
3882 return TRUE;
3885 /* The number of dynamic relocations required by a static relocation. */
3887 static int
3888 alpha_dynamic_entries_for_reloc (r_type, dynamic, shared)
3889 int r_type, dynamic, shared;
3891 switch (r_type)
3893 /* May appear in GOT entries. */
3894 case R_ALPHA_TLSGD:
3895 return (dynamic ? 2 : shared ? 1 : 0);
3896 case R_ALPHA_TLSLDM:
3897 return shared;
3898 case R_ALPHA_LITERAL:
3899 case R_ALPHA_GOTTPREL:
3900 return dynamic || shared;
3901 case R_ALPHA_GOTDTPREL:
3902 return dynamic;
3904 /* May appear in data sections. */
3905 case R_ALPHA_REFLONG:
3906 case R_ALPHA_REFQUAD:
3907 case R_ALPHA_TPREL64:
3908 return dynamic || shared;
3910 /* Everything else is illegal. We'll issue an error during
3911 relocate_section. */
3912 default:
3913 return 0;
3917 /* Work out the sizes of the dynamic relocation entries. */
3919 static bfd_boolean
3920 elf64_alpha_calc_dynrel_sizes (h, info)
3921 struct alpha_elf_link_hash_entry *h;
3922 struct bfd_link_info *info;
3924 bfd_boolean dynamic;
3925 struct alpha_elf_reloc_entry *relent;
3926 unsigned long entries;
3928 if (h->root.root.type == bfd_link_hash_warning)
3929 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3931 /* If the symbol was defined as a common symbol in a regular object
3932 file, and there was no definition in any dynamic object, then the
3933 linker will have allocated space for the symbol in a common
3934 section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
3935 set. This is done for dynamic symbols in
3936 elf_adjust_dynamic_symbol but this is not done for non-dynamic
3937 symbols, somehow. */
3938 if (((h->root.elf_link_hash_flags
3939 & (ELF_LINK_HASH_DEF_REGULAR
3940 | ELF_LINK_HASH_REF_REGULAR
3941 | ELF_LINK_HASH_DEF_DYNAMIC))
3942 == ELF_LINK_HASH_REF_REGULAR)
3943 && (h->root.root.type == bfd_link_hash_defined
3944 || h->root.root.type == bfd_link_hash_defweak)
3945 && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
3946 h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3948 /* If the symbol is dynamic, we'll need all the relocations in their
3949 natural form. If this is a shared object, and it has been forced
3950 local, we'll need the same number of RELATIVE relocations. */
3952 dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
3954 for (relent = h->reloc_entries; relent; relent = relent->next)
3956 entries = alpha_dynamic_entries_for_reloc (relent->rtype, dynamic,
3957 info->shared);
3958 if (entries)
3960 relent->srel->_raw_size +=
3961 entries * sizeof (Elf64_External_Rela) * relent->count;
3962 if (relent->reltext)
3963 info->flags |= DT_TEXTREL;
3967 return TRUE;
3970 /* Set the sizes of the dynamic relocation sections. */
3972 static bfd_boolean
3973 elf64_alpha_size_rela_got_section (info)
3974 struct bfd_link_info *info;
3976 unsigned long entries;
3977 bfd *i, *dynobj;
3978 asection *srel;
3980 /* Shared libraries often require RELATIVE relocs, and some relocs
3981 require attention for the main application as well. */
3983 entries = 0;
3984 for (i = alpha_elf_hash_table(info)->got_list;
3985 i ; i = alpha_elf_tdata(i)->got_link_next)
3987 bfd *j;
3989 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3991 struct alpha_elf_got_entry **local_got_entries, *gotent;
3992 int k, n;
3994 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3995 if (!local_got_entries)
3996 continue;
3998 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3999 for (gotent = local_got_entries[k];
4000 gotent ; gotent = gotent->next)
4001 if (gotent->use_count > 0)
4002 entries += (alpha_dynamic_entries_for_reloc
4003 (gotent->reloc_type, 0, info->shared));
4007 dynobj = elf_hash_table(info)->dynobj;
4008 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4009 if (!srel)
4011 BFD_ASSERT (entries == 0);
4012 return TRUE;
4014 srel->_raw_size = sizeof (Elf64_External_Rela) * entries;
4016 /* Now do the non-local symbols. */
4017 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
4018 elf64_alpha_size_rela_got_1, info);
4020 srel->_cooked_size = srel->_raw_size;
4022 return TRUE;
4025 /* Subroutine of elf64_alpha_size_rela_got_section for doing the
4026 global symbols. */
4028 static bfd_boolean
4029 elf64_alpha_size_rela_got_1 (h, info)
4030 struct alpha_elf_link_hash_entry *h;
4031 struct bfd_link_info *info;
4033 bfd_boolean dynamic;
4034 struct alpha_elf_got_entry *gotent;
4035 unsigned long entries;
4037 if (h->root.root.type == bfd_link_hash_warning)
4038 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
4040 /* If the symbol is dynamic, we'll need all the relocations in their
4041 natural form. If this is a shared object, and it has been forced
4042 local, we'll need the same number of RELATIVE relocations. */
4044 dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
4046 entries = 0;
4047 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
4048 if (gotent->use_count > 0)
4049 entries += alpha_dynamic_entries_for_reloc (gotent->reloc_type,
4050 dynamic, info->shared);
4052 /* If we are using a .plt entry, subtract one, as the first
4053 reference uses a .rela.plt entry instead. */
4054 if (h->root.plt.offset != MINUS_ONE)
4055 entries--;
4057 if (entries > 0)
4059 bfd *dynobj = elf_hash_table(info)->dynobj;
4060 asection *srel = bfd_get_section_by_name (dynobj, ".rela.got");
4061 BFD_ASSERT (srel != NULL);
4062 srel->_raw_size += sizeof (Elf64_External_Rela) * entries;
4065 return TRUE;
4068 /* Set the sizes of the dynamic sections. */
4070 static bfd_boolean
4071 elf64_alpha_size_dynamic_sections (output_bfd, info)
4072 bfd *output_bfd ATTRIBUTE_UNUSED;
4073 struct bfd_link_info *info;
4075 bfd *dynobj;
4076 asection *s;
4077 bfd_boolean relplt;
4079 dynobj = elf_hash_table(info)->dynobj;
4080 BFD_ASSERT(dynobj != NULL);
4082 if (elf_hash_table (info)->dynamic_sections_created)
4084 /* Set the contents of the .interp section to the interpreter. */
4085 if (!info->shared)
4087 s = bfd_get_section_by_name (dynobj, ".interp");
4088 BFD_ASSERT (s != NULL);
4089 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4090 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4093 /* Now that we've seen all of the input files, we can decide which
4094 symbols need dynamic relocation entries and which don't. We've
4095 collected information in check_relocs that we can now apply to
4096 size the dynamic relocation sections. */
4097 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
4098 elf64_alpha_calc_dynrel_sizes, info);
4100 elf64_alpha_size_rela_got_section (info);
4102 /* else we're not dynamic and by definition we don't need such things. */
4104 /* The check_relocs and adjust_dynamic_symbol entry points have
4105 determined the sizes of the various dynamic sections. Allocate
4106 memory for them. */
4107 relplt = FALSE;
4108 for (s = dynobj->sections; s != NULL; s = s->next)
4110 const char *name;
4111 bfd_boolean strip;
4113 if (!(s->flags & SEC_LINKER_CREATED))
4114 continue;
4116 /* It's OK to base decisions on the section name, because none
4117 of the dynobj section names depend upon the input files. */
4118 name = bfd_get_section_name (dynobj, s);
4120 /* If we don't need this section, strip it from the output file.
4121 This is to handle .rela.bss and .rela.plt. We must create it
4122 in create_dynamic_sections, because it must be created before
4123 the linker maps input sections to output sections. The
4124 linker does that before adjust_dynamic_symbol is called, and
4125 it is that function which decides whether anything needs to
4126 go into these sections. */
4128 strip = FALSE;
4130 if (strncmp (name, ".rela", 5) == 0)
4132 strip = (s->_raw_size == 0);
4134 if (!strip)
4136 if (strcmp(name, ".rela.plt") == 0)
4137 relplt = TRUE;
4139 /* We use the reloc_count field as a counter if we need
4140 to copy relocs into the output file. */
4141 s->reloc_count = 0;
4144 else if (strcmp (name, ".plt") != 0)
4146 /* It's not one of our dynamic sections, so don't allocate space. */
4147 continue;
4150 if (strip)
4151 _bfd_strip_section_from_output (info, s);
4152 else
4154 /* Allocate memory for the section contents. */
4155 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4156 if (s->contents == NULL && s->_raw_size != 0)
4157 return FALSE;
4161 if (elf_hash_table (info)->dynamic_sections_created)
4163 /* Add some entries to the .dynamic section. We fill in the
4164 values later, in elf64_alpha_finish_dynamic_sections, but we
4165 must add the entries now so that we get the correct size for
4166 the .dynamic section. The DT_DEBUG entry is filled in by the
4167 dynamic linker and used by the debugger. */
4168 #define add_dynamic_entry(TAG, VAL) \
4169 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4171 if (!info->shared)
4173 if (!add_dynamic_entry (DT_DEBUG, 0))
4174 return FALSE;
4177 if (relplt)
4179 if (!add_dynamic_entry (DT_PLTGOT, 0)
4180 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4181 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4182 || !add_dynamic_entry (DT_JMPREL, 0))
4183 return FALSE;
4186 if (!add_dynamic_entry (DT_RELA, 0)
4187 || !add_dynamic_entry (DT_RELASZ, 0)
4188 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
4189 return FALSE;
4191 if (info->flags & DF_TEXTREL)
4193 if (!add_dynamic_entry (DT_TEXTREL, 0))
4194 return FALSE;
4197 #undef add_dynamic_entry
4199 return TRUE;
4202 /* Emit a dynamic relocation for (DYNINDX, RTYPE, ADDEND) at (SEC, OFFSET)
4203 into the next available slot in SREL. */
4205 static void
4206 elf64_alpha_emit_dynrel (abfd, info, sec, srel, offset, dynindx, rtype, addend)
4207 bfd *abfd;
4208 struct bfd_link_info *info;
4209 asection *sec, *srel;
4210 bfd_vma offset, addend;
4211 long dynindx, rtype;
4213 Elf_Internal_Rela outrel;
4214 bfd_byte *loc;
4216 BFD_ASSERT (srel != NULL);
4218 outrel.r_info = ELF64_R_INFO (dynindx, rtype);
4219 outrel.r_addend = addend;
4221 offset = _bfd_elf_section_offset (abfd, info, sec, offset);
4222 if ((offset | 1) != (bfd_vma) -1)
4223 outrel.r_offset = sec->output_section->vma + sec->output_offset + offset;
4224 else
4225 memset (&outrel, 0, sizeof (outrel));
4227 loc = srel->contents;
4228 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
4229 bfd_elf64_swap_reloca_out (abfd, &outrel, loc);
4230 BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
4231 <= srel->_cooked_size);
4234 /* Relocate an Alpha ELF section for a relocatable link.
4236 We don't have to change anything unless the reloc is against a section
4237 symbol, in which case we have to adjust according to where the section
4238 symbol winds up in the output section. */
4240 static bfd_boolean
4241 elf64_alpha_relocate_section_r (output_bfd, info, input_bfd, input_section,
4242 contents, relocs, local_syms, local_sections)
4243 bfd *output_bfd ATTRIBUTE_UNUSED;
4244 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4245 bfd *input_bfd;
4246 asection *input_section;
4247 bfd_byte *contents ATTRIBUTE_UNUSED;
4248 Elf_Internal_Rela *relocs;
4249 Elf_Internal_Sym *local_syms;
4250 asection **local_sections;
4252 unsigned long symtab_hdr_sh_info;
4253 Elf_Internal_Rela *rel;
4254 Elf_Internal_Rela *relend;
4255 bfd_boolean ret_val = TRUE;
4257 symtab_hdr_sh_info = elf_tdata (input_bfd)->symtab_hdr.sh_info;
4259 relend = relocs + input_section->reloc_count;
4260 for (rel = relocs; rel < relend; rel++)
4262 unsigned long r_symndx;
4263 Elf_Internal_Sym *sym;
4264 asection *sec;
4265 unsigned long r_type;
4267 r_type = ELF64_R_TYPE(rel->r_info);
4268 if (r_type >= R_ALPHA_max)
4270 (*_bfd_error_handler)
4271 (_("%s: unknown relocation type %d"),
4272 bfd_archive_filename (input_bfd), (int)r_type);
4273 bfd_set_error (bfd_error_bad_value);
4274 ret_val = FALSE;
4275 continue;
4278 r_symndx = ELF64_R_SYM(rel->r_info);
4280 /* The symbol associated with GPDISP and LITUSE is
4281 immaterial. Only the addend is significant. */
4282 if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
4283 continue;
4285 if (r_symndx < symtab_hdr_sh_info)
4287 sym = local_syms + r_symndx;
4288 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
4290 sec = local_sections[r_symndx];
4291 rel->r_addend += sec->output_offset + sym->st_value;
4296 return ret_val;
4299 /* Relocate an Alpha ELF section. */
4301 static bfd_boolean
4302 elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
4303 contents, relocs, local_syms, local_sections)
4304 bfd *output_bfd;
4305 struct bfd_link_info *info;
4306 bfd *input_bfd;
4307 asection *input_section;
4308 bfd_byte *contents;
4309 Elf_Internal_Rela *relocs;
4310 Elf_Internal_Sym *local_syms;
4311 asection **local_sections;
4313 Elf_Internal_Shdr *symtab_hdr;
4314 Elf_Internal_Rela *rel;
4315 Elf_Internal_Rela *relend;
4316 struct elf_link_tls_segment *tls_segment;
4317 asection *sgot, *srel, *srelgot;
4318 bfd *dynobj, *gotobj;
4319 bfd_vma gp, tp_base, dtp_base;
4320 struct alpha_elf_got_entry **local_got_entries;
4321 bfd_boolean ret_val;
4322 const char *section_name;
4324 /* Handle relocatable links with a smaller loop. */
4325 if (info->relocateable)
4326 return elf64_alpha_relocate_section_r (output_bfd, info, input_bfd,
4327 input_section, contents, relocs,
4328 local_syms, local_sections);
4330 /* This is a final link. */
4332 ret_val = TRUE;
4334 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4336 dynobj = elf_hash_table (info)->dynobj;
4337 if (dynobj)
4338 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4339 else
4340 srelgot = NULL;
4342 section_name = (bfd_elf_string_from_elf_section
4343 (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
4344 elf_section_data(input_section)->rel_hdr.sh_name));
4345 BFD_ASSERT(section_name != NULL);
4346 srel = bfd_get_section_by_name (dynobj, section_name);
4348 /* Find the gp value for this input bfd. */
4349 gotobj = alpha_elf_tdata (input_bfd)->gotobj;
4350 if (gotobj)
4352 sgot = alpha_elf_tdata (gotobj)->got;
4353 gp = _bfd_get_gp_value (gotobj);
4354 if (gp == 0)
4356 gp = (sgot->output_section->vma
4357 + sgot->output_offset
4358 + 0x8000);
4359 _bfd_set_gp_value (gotobj, gp);
4362 else
4364 sgot = NULL;
4365 gp = 0;
4368 local_got_entries = alpha_elf_tdata(input_bfd)->local_got_entries;
4370 tls_segment = elf_hash_table (info)->tls_segment;
4371 if (tls_segment)
4373 dtp_base = alpha_get_dtprel_base (tls_segment);
4374 tp_base = alpha_get_tprel_base (tls_segment);
4376 else
4377 dtp_base = tp_base = 0;
4379 relend = relocs + input_section->reloc_count;
4380 for (rel = relocs; rel < relend; rel++)
4382 struct alpha_elf_link_hash_entry *h = NULL;
4383 struct alpha_elf_got_entry *gotent;
4384 bfd_reloc_status_type r;
4385 reloc_howto_type *howto;
4386 unsigned long r_symndx;
4387 Elf_Internal_Sym *sym = NULL;
4388 asection *sec = NULL;
4389 bfd_vma value;
4390 bfd_vma addend;
4391 bfd_boolean dynamic_symbol_p;
4392 bfd_boolean undef_weak_ref = FALSE;
4393 unsigned long r_type;
4395 r_type = ELF64_R_TYPE(rel->r_info);
4396 if (r_type >= R_ALPHA_max)
4398 (*_bfd_error_handler)
4399 (_("%s: unknown relocation type %d"),
4400 bfd_archive_filename (input_bfd), (int)r_type);
4401 bfd_set_error (bfd_error_bad_value);
4402 ret_val = FALSE;
4403 continue;
4406 howto = elf64_alpha_howto_table + r_type;
4407 r_symndx = ELF64_R_SYM(rel->r_info);
4409 /* The symbol for a TLSLDM reloc is ignored. Collapse the
4410 reloc to the 0 symbol so that they all match. */
4411 if (r_type == R_ALPHA_TLSLDM)
4412 r_symndx = 0;
4414 if (r_symndx < symtab_hdr->sh_info)
4416 sym = local_syms + r_symndx;
4417 sec = local_sections[r_symndx];
4418 value = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4420 /* If this is a tp-relative relocation against sym 0,
4421 this is hackery from relax_section. Force the value to
4422 be the tls base. */
4423 if (r_symndx == 0
4424 && (r_type == R_ALPHA_TLSLDM
4425 || r_type == R_ALPHA_GOTTPREL
4426 || r_type == R_ALPHA_TPREL64
4427 || r_type == R_ALPHA_TPRELHI
4428 || r_type == R_ALPHA_TPRELLO
4429 || r_type == R_ALPHA_TPREL16))
4430 value = tp_base;
4432 if (local_got_entries)
4433 gotent = local_got_entries[r_symndx];
4434 else
4435 gotent = NULL;
4437 /* Need to adjust local GOT entries' addends for SEC_MERGE
4438 unless it has been done already. */
4439 if ((sec->flags & SEC_MERGE)
4440 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
4441 && (elf_section_data (sec)->sec_info_type
4442 == ELF_INFO_TYPE_MERGE)
4443 && gotent
4444 && !gotent->reloc_xlated)
4446 struct alpha_elf_got_entry *ent;
4447 asection *msec;
4449 for (ent = gotent; ent; ent = ent->next)
4451 ent->reloc_xlated = 1;
4452 if (ent->use_count == 0)
4453 continue;
4454 msec = sec;
4455 ent->addend =
4456 _bfd_merged_section_offset (output_bfd, &msec,
4457 elf_section_data (sec)->
4458 sec_info,
4459 sym->st_value + ent->addend,
4460 (bfd_vma) 0);
4461 ent->addend -= sym->st_value;
4462 ent->addend += msec->output_section->vma
4463 + msec->output_offset
4464 - sec->output_section->vma
4465 - sec->output_offset;
4469 dynamic_symbol_p = FALSE;
4471 else
4473 h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
4475 while (h->root.root.type == bfd_link_hash_indirect
4476 || h->root.root.type == bfd_link_hash_warning)
4477 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
4479 value = 0;
4480 if (h->root.root.type == bfd_link_hash_defined
4481 || h->root.root.type == bfd_link_hash_defweak)
4483 sec = h->root.root.u.def.section;
4485 /* Detect the cases that sym_sec->output_section is
4486 expected to be NULL -- all cases in which the symbol
4487 is defined in another shared module. This includes
4488 PLT relocs for which we've created a PLT entry and
4489 other relocs for which we're prepared to create
4490 dynamic relocations. */
4491 /* ??? Just accept it NULL and continue. */
4493 if (sec->output_section != NULL)
4494 value = (h->root.root.u.def.value
4495 + sec->output_section->vma
4496 + sec->output_offset);
4498 else if (h->root.root.type == bfd_link_hash_undefweak)
4499 undef_weak_ref = TRUE;
4500 else if (info->shared
4501 && (!info->symbolic || info->allow_shlib_undefined)
4502 && !info->no_undefined
4503 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4505 else
4507 if (!((*info->callbacks->undefined_symbol)
4508 (info, h->root.root.root.string, input_bfd,
4509 input_section, rel->r_offset,
4510 (!info->shared || info->no_undefined
4511 || ELF_ST_VISIBILITY (h->root.other)))))
4512 return FALSE;
4513 ret_val = FALSE;
4514 continue;
4517 dynamic_symbol_p = alpha_elf_dynamic_symbol_p (&h->root, info);
4518 gotent = h->got_entries;
4521 addend = rel->r_addend;
4522 value += addend;
4524 /* Search for the proper got entry. */
4525 for (; gotent ; gotent = gotent->next)
4526 if (gotent->gotobj == gotobj
4527 && gotent->reloc_type == r_type
4528 && gotent->addend == addend)
4529 break;
4531 switch (r_type)
4533 case R_ALPHA_GPDISP:
4535 bfd_byte *p_ldah, *p_lda;
4537 BFD_ASSERT(gp != 0);
4539 value = (input_section->output_section->vma
4540 + input_section->output_offset
4541 + rel->r_offset);
4543 p_ldah = contents + rel->r_offset;
4544 p_lda = p_ldah + rel->r_addend;
4546 r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - value,
4547 p_ldah, p_lda);
4549 break;
4551 case R_ALPHA_LITERAL:
4552 BFD_ASSERT(sgot != NULL);
4553 BFD_ASSERT(gp != 0);
4554 BFD_ASSERT(gotent != NULL);
4555 BFD_ASSERT(gotent->use_count >= 1);
4557 if (!gotent->reloc_done)
4559 gotent->reloc_done = 1;
4561 bfd_put_64 (output_bfd, value,
4562 sgot->contents + gotent->got_offset);
4564 /* If the symbol has been forced local, output a
4565 RELATIVE reloc, otherwise it will be handled in
4566 finish_dynamic_symbol. */
4567 if (info->shared && !dynamic_symbol_p)
4568 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4569 gotent->got_offset, 0,
4570 R_ALPHA_RELATIVE, value);
4573 value = (sgot->output_section->vma
4574 + sgot->output_offset
4575 + gotent->got_offset);
4576 value -= gp;
4577 goto default_reloc;
4579 case R_ALPHA_GPREL16:
4580 case R_ALPHA_GPREL32:
4581 case R_ALPHA_GPRELLOW:
4582 if (dynamic_symbol_p)
4584 (*_bfd_error_handler)
4585 (_("%s: gp-relative relocation against dynamic symbol %s"),
4586 bfd_archive_filename (input_bfd), h->root.root.root.string);
4587 ret_val = FALSE;
4589 BFD_ASSERT(gp != 0);
4590 value -= gp;
4591 goto default_reloc;
4593 case R_ALPHA_GPRELHIGH:
4594 if (dynamic_symbol_p)
4596 (*_bfd_error_handler)
4597 (_("%s: gp-relative relocation against dynamic symbol %s"),
4598 bfd_archive_filename (input_bfd), h->root.root.root.string);
4599 ret_val = FALSE;
4601 BFD_ASSERT(gp != 0);
4602 value -= gp;
4603 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4604 goto default_reloc;
4606 case R_ALPHA_HINT:
4607 /* A call to a dynamic symbol is definitely out of range of
4608 the 16-bit displacement. Don't bother writing anything. */
4609 if (dynamic_symbol_p)
4611 r = bfd_reloc_ok;
4612 break;
4614 /* The regular PC-relative stuff measures from the start of
4615 the instruction rather than the end. */
4616 value -= 4;
4617 goto default_reloc;
4619 case R_ALPHA_BRADDR:
4620 if (dynamic_symbol_p)
4622 (*_bfd_error_handler)
4623 (_("%s: pc-relative relocation against dynamic symbol %s"),
4624 bfd_archive_filename (input_bfd), h->root.root.root.string);
4625 ret_val = FALSE;
4627 /* The regular PC-relative stuff measures from the start of
4628 the instruction rather than the end. */
4629 value -= 4;
4630 goto default_reloc;
4632 case R_ALPHA_BRSGP:
4634 int other;
4635 const char *name;
4637 /* The regular PC-relative stuff measures from the start of
4638 the instruction rather than the end. */
4639 value -= 4;
4641 /* The source and destination gp must be the same. Note that
4642 the source will always have an assigned gp, since we forced
4643 one in check_relocs, but that the destination may not, as
4644 it might not have had any relocations at all. Also take
4645 care not to crash if H is an undefined symbol. */
4646 if (h != NULL && sec != NULL
4647 && alpha_elf_tdata (sec->owner)->gotobj
4648 && gotobj != alpha_elf_tdata (sec->owner)->gotobj)
4650 (*_bfd_error_handler)
4651 (_("%s: change in gp: BRSGP %s"),
4652 bfd_archive_filename (input_bfd), h->root.root.root.string);
4653 ret_val = FALSE;
4656 /* The symbol should be marked either NOPV or STD_GPLOAD. */
4657 if (h != NULL)
4658 other = h->root.other;
4659 else
4660 other = sym->st_other;
4661 switch (other & STO_ALPHA_STD_GPLOAD)
4663 case STO_ALPHA_NOPV:
4664 break;
4665 case STO_ALPHA_STD_GPLOAD:
4666 value += 8;
4667 break;
4668 default:
4669 if (h != NULL)
4670 name = h->root.root.root.string;
4671 else
4673 name = (bfd_elf_string_from_elf_section
4674 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4675 if (name == NULL)
4676 name = _("<unknown>");
4677 else if (name[0] == 0)
4678 name = bfd_section_name (input_bfd, sec);
4680 (*_bfd_error_handler)
4681 (_("%s: !samegp reloc against symbol without .prologue: %s"),
4682 bfd_archive_filename (input_bfd), name);
4683 ret_val = FALSE;
4684 break;
4687 goto default_reloc;
4690 case R_ALPHA_REFLONG:
4691 case R_ALPHA_REFQUAD:
4692 case R_ALPHA_DTPREL64:
4693 case R_ALPHA_TPREL64:
4695 long dynindx, dyntype = r_type;
4696 bfd_vma dynaddend;
4698 /* Careful here to remember RELATIVE relocations for global
4699 variables for symbolic shared objects. */
4701 if (dynamic_symbol_p)
4703 BFD_ASSERT(h->root.dynindx != -1);
4704 dynindx = h->root.dynindx;
4705 dynaddend = addend;
4706 addend = 0, value = 0;
4708 else if (r_type == R_ALPHA_DTPREL64)
4710 BFD_ASSERT(tls_segment != NULL);
4711 value -= dtp_base;
4712 goto default_reloc;
4714 else if (r_type == R_ALPHA_TPREL64)
4716 BFD_ASSERT(tls_segment != NULL);
4717 if (!info->shared)
4719 value -= tp_base;
4720 goto default_reloc;
4722 dynindx = 0;
4723 dynaddend = value - dtp_base;
4725 else if (info->shared
4726 && r_symndx != 0
4727 && (input_section->flags & SEC_ALLOC))
4729 if (r_type == R_ALPHA_REFLONG)
4731 (*_bfd_error_handler)
4732 (_("%s: unhandled dynamic relocation against %s"),
4733 bfd_archive_filename (input_bfd),
4734 h->root.root.root.string);
4735 ret_val = FALSE;
4737 dynindx = 0;
4738 dyntype = R_ALPHA_RELATIVE;
4739 dynaddend = value;
4741 else
4742 goto default_reloc;
4744 elf64_alpha_emit_dynrel (output_bfd, info, input_section,
4745 srel, rel->r_offset, dynindx,
4746 dyntype, dynaddend);
4748 goto default_reloc;
4750 case R_ALPHA_SREL16:
4751 case R_ALPHA_SREL32:
4752 case R_ALPHA_SREL64:
4753 if (dynamic_symbol_p)
4755 (*_bfd_error_handler)
4756 (_("%s: pc-relative relocation against dynamic symbol %s"),
4757 bfd_archive_filename (input_bfd), h->root.root.root.string);
4758 ret_val = FALSE;
4761 /* ??? .eh_frame references to discarded sections will be smashed
4762 to relocations against SHN_UNDEF. The .eh_frame format allows
4763 NULL to be encoded as 0 in any format, so this works here. */
4764 if (r_symndx == 0)
4765 howto = (elf64_alpha_howto_table
4766 + (r_type - R_ALPHA_SREL32 + R_ALPHA_REFLONG));
4767 goto default_reloc;
4769 case R_ALPHA_TLSLDM:
4770 /* Ignore the symbol for the relocation. The result is always
4771 the current module. */
4772 dynamic_symbol_p = 0;
4773 /* FALLTHRU */
4775 case R_ALPHA_TLSGD:
4776 if (!gotent->reloc_done)
4778 gotent->reloc_done = 1;
4780 /* Note that the module index for the main program is 1. */
4781 bfd_put_64 (output_bfd, !info->shared && !dynamic_symbol_p,
4782 sgot->contents + gotent->got_offset);
4784 /* If the symbol has been forced local, output a
4785 DTPMOD64 reloc, otherwise it will be handled in
4786 finish_dynamic_symbol. */
4787 if (info->shared && !dynamic_symbol_p)
4788 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4789 gotent->got_offset, 0,
4790 R_ALPHA_DTPMOD64, 0);
4792 if (dynamic_symbol_p || r_type == R_ALPHA_TLSLDM)
4793 value = 0;
4794 else
4796 BFD_ASSERT(tls_segment != NULL);
4797 value -= dtp_base;
4799 bfd_put_64 (output_bfd, value,
4800 sgot->contents + gotent->got_offset + 8);
4803 value = (sgot->output_section->vma
4804 + sgot->output_offset
4805 + gotent->got_offset);
4806 value -= gp;
4807 goto default_reloc;
4809 case R_ALPHA_DTPRELHI:
4810 case R_ALPHA_DTPRELLO:
4811 case R_ALPHA_DTPREL16:
4812 if (dynamic_symbol_p)
4814 (*_bfd_error_handler)
4815 (_("%s: dtp-relative relocation against dynamic symbol %s"),
4816 bfd_archive_filename (input_bfd), h->root.root.root.string);
4817 ret_val = FALSE;
4819 BFD_ASSERT(tls_segment != NULL);
4820 value -= dtp_base;
4821 if (r_type == R_ALPHA_DTPRELHI)
4822 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4823 goto default_reloc;
4825 case R_ALPHA_TPRELHI:
4826 case R_ALPHA_TPRELLO:
4827 case R_ALPHA_TPREL16:
4828 if (info->shared)
4830 (*_bfd_error_handler)
4831 (_("%s: TLS local exec code cannot be linked into shared objects"),
4832 bfd_archive_filename (input_bfd));
4833 ret_val = FALSE;
4835 else if (dynamic_symbol_p)
4837 (*_bfd_error_handler)
4838 (_("%s: tp-relative relocation against dynamic symbol %s"),
4839 bfd_archive_filename (input_bfd), h->root.root.root.string);
4840 ret_val = FALSE;
4842 BFD_ASSERT(tls_segment != NULL);
4843 value -= tp_base;
4844 if (r_type == R_ALPHA_TPRELHI)
4845 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4846 goto default_reloc;
4848 case R_ALPHA_GOTDTPREL:
4849 case R_ALPHA_GOTTPREL:
4850 BFD_ASSERT(sgot != NULL);
4851 BFD_ASSERT(gp != 0);
4852 BFD_ASSERT(gotent != NULL);
4853 BFD_ASSERT(gotent->use_count >= 1);
4855 if (!gotent->reloc_done)
4857 gotent->reloc_done = 1;
4859 if (dynamic_symbol_p)
4860 value = 0;
4861 else
4863 BFD_ASSERT(tls_segment != NULL);
4864 if (r_type == R_ALPHA_GOTDTPREL)
4865 value -= dtp_base;
4866 else if (!info->shared)
4867 value -= tp_base;
4868 else
4870 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4871 gotent->got_offset, 0,
4872 R_ALPHA_TPREL64,
4873 value - dtp_base);
4874 value = 0;
4877 bfd_put_64 (output_bfd, value,
4878 sgot->contents + gotent->got_offset);
4881 value = (sgot->output_section->vma
4882 + sgot->output_offset
4883 + gotent->got_offset);
4884 value -= gp;
4885 goto default_reloc;
4887 default:
4888 default_reloc:
4889 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4890 contents, rel->r_offset, value, 0);
4891 break;
4894 switch (r)
4896 case bfd_reloc_ok:
4897 break;
4899 case bfd_reloc_overflow:
4901 const char *name;
4903 /* Don't warn if the overflow is due to pc relative reloc
4904 against discarded section. Section optimization code should
4905 handle it. */
4907 if (r_symndx < symtab_hdr->sh_info
4908 && sec != NULL && howto->pc_relative
4909 && elf_discarded_section (sec))
4910 break;
4912 if (h != NULL)
4913 name = h->root.root.root.string;
4914 else
4916 name = (bfd_elf_string_from_elf_section
4917 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4918 if (name == NULL)
4919 return FALSE;
4920 if (*name == '\0')
4921 name = bfd_section_name (input_bfd, sec);
4923 if (! ((*info->callbacks->reloc_overflow)
4924 (info, name, howto->name, (bfd_vma) 0,
4925 input_bfd, input_section, rel->r_offset)))
4926 ret_val = FALSE;
4928 break;
4930 default:
4931 case bfd_reloc_outofrange:
4932 abort ();
4936 return ret_val;
4939 /* Finish up dynamic symbol handling. We set the contents of various
4940 dynamic sections here. */
4942 static bfd_boolean
4943 elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
4944 bfd *output_bfd;
4945 struct bfd_link_info *info;
4946 struct elf_link_hash_entry *h;
4947 Elf_Internal_Sym *sym;
4949 bfd *dynobj = elf_hash_table(info)->dynobj;
4951 if (h->plt.offset != MINUS_ONE)
4953 /* Fill in the .plt entry for this symbol. */
4954 asection *splt, *sgot, *srel;
4955 Elf_Internal_Rela outrel;
4956 bfd_byte *loc;
4957 bfd_vma got_addr, plt_addr;
4958 bfd_vma plt_index;
4959 struct alpha_elf_got_entry *gotent;
4961 BFD_ASSERT (h->dynindx != -1);
4963 /* The first .got entry will be updated by the .plt with the
4964 address of the target function. */
4965 gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
4966 BFD_ASSERT (gotent && gotent->addend == 0);
4968 splt = bfd_get_section_by_name (dynobj, ".plt");
4969 BFD_ASSERT (splt != NULL);
4970 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4971 BFD_ASSERT (srel != NULL);
4972 sgot = alpha_elf_tdata (gotent->gotobj)->got;
4973 BFD_ASSERT (sgot != NULL);
4975 got_addr = (sgot->output_section->vma
4976 + sgot->output_offset
4977 + gotent->got_offset);
4978 plt_addr = (splt->output_section->vma
4979 + splt->output_offset
4980 + h->plt.offset);
4982 plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
4984 /* Fill in the entry in the procedure linkage table. */
4986 bfd_vma insn1, insn2, insn3;
4988 insn1 = PLT_ENTRY_WORD1 | ((-(h->plt.offset + 4) >> 2) & 0x1fffff);
4989 insn2 = PLT_ENTRY_WORD2;
4990 insn3 = PLT_ENTRY_WORD3;
4992 bfd_put_32 (output_bfd, insn1, splt->contents + h->plt.offset);
4993 bfd_put_32 (output_bfd, insn2, splt->contents + h->plt.offset + 4);
4994 bfd_put_32 (output_bfd, insn3, splt->contents + h->plt.offset + 8);
4997 /* Fill in the entry in the .rela.plt section. */
4998 outrel.r_offset = got_addr;
4999 outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
5000 outrel.r_addend = 0;
5002 loc = srel->contents + plt_index * sizeof (Elf64_External_Rela);
5003 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5005 if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
5007 /* Mark the symbol as undefined, rather than as defined in the
5008 .plt section. Leave the value alone. */
5009 sym->st_shndx = SHN_UNDEF;
5012 /* Fill in the entries in the .got. */
5013 bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
5015 /* Subsequent .got entries will continue to bounce through the .plt. */
5016 if (gotent->next)
5018 srel = bfd_get_section_by_name (dynobj, ".rela.got");
5019 BFD_ASSERT (! info->shared || srel != NULL);
5021 gotent = gotent->next;
5024 sgot = alpha_elf_tdata(gotent->gotobj)->got;
5025 BFD_ASSERT(sgot != NULL);
5026 BFD_ASSERT(gotent->addend == 0);
5028 bfd_put_64 (output_bfd, plt_addr,
5029 sgot->contents + gotent->got_offset);
5031 if (info->shared)
5032 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
5033 gotent->got_offset, 0,
5034 R_ALPHA_RELATIVE, plt_addr);
5036 gotent = gotent->next;
5038 while (gotent != NULL);
5041 else if (alpha_elf_dynamic_symbol_p (h, info))
5043 /* Fill in the dynamic relocations for this symbol's .got entries. */
5044 asection *srel;
5045 struct alpha_elf_got_entry *gotent;
5047 srel = bfd_get_section_by_name (dynobj, ".rela.got");
5048 BFD_ASSERT (srel != NULL);
5050 for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
5051 gotent != NULL;
5052 gotent = gotent->next)
5054 asection *sgot;
5055 long r_type;
5057 if (gotent->use_count == 0)
5058 continue;
5060 sgot = alpha_elf_tdata (gotent->gotobj)->got;
5062 r_type = gotent->reloc_type;
5063 switch (r_type)
5065 case R_ALPHA_LITERAL:
5066 r_type = R_ALPHA_GLOB_DAT;
5067 break;
5068 case R_ALPHA_TLSGD:
5069 r_type = R_ALPHA_DTPMOD64;
5070 break;
5071 case R_ALPHA_GOTDTPREL:
5072 r_type = R_ALPHA_DTPREL64;
5073 break;
5074 case R_ALPHA_GOTTPREL:
5075 r_type = R_ALPHA_TPREL64;
5076 break;
5077 case R_ALPHA_TLSLDM:
5078 default:
5079 abort ();
5082 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
5083 gotent->got_offset, h->dynindx,
5084 r_type, gotent->addend);
5086 if (gotent->reloc_type == R_ALPHA_TLSGD)
5087 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
5088 gotent->got_offset + 8, h->dynindx,
5089 R_ALPHA_DTPREL64, gotent->addend);
5093 /* Mark some specially defined symbols as absolute. */
5094 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5095 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
5096 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
5097 sym->st_shndx = SHN_ABS;
5099 return TRUE;
5102 /* Finish up the dynamic sections. */
5104 static bfd_boolean
5105 elf64_alpha_finish_dynamic_sections (output_bfd, info)
5106 bfd *output_bfd;
5107 struct bfd_link_info *info;
5109 bfd *dynobj;
5110 asection *sdyn;
5112 dynobj = elf_hash_table (info)->dynobj;
5113 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5115 if (elf_hash_table (info)->dynamic_sections_created)
5117 asection *splt;
5118 Elf64_External_Dyn *dyncon, *dynconend;
5120 splt = bfd_get_section_by_name (dynobj, ".plt");
5121 BFD_ASSERT (splt != NULL && sdyn != NULL);
5123 dyncon = (Elf64_External_Dyn *) sdyn->contents;
5124 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
5125 for (; dyncon < dynconend; dyncon++)
5127 Elf_Internal_Dyn dyn;
5128 const char *name;
5129 asection *s;
5131 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
5133 switch (dyn.d_tag)
5135 case DT_PLTGOT:
5136 name = ".plt";
5137 goto get_vma;
5138 case DT_PLTRELSZ:
5139 name = ".rela.plt";
5140 goto get_size;
5141 case DT_JMPREL:
5142 name = ".rela.plt";
5143 goto get_vma;
5145 case DT_RELASZ:
5146 /* My interpretation of the TIS v1.1 ELF document indicates
5147 that RELASZ should not include JMPREL. This is not what
5148 the rest of the BFD does. It is, however, what the
5149 glibc ld.so wants. Do this fixup here until we found
5150 out who is right. */
5151 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
5152 if (s)
5154 dyn.d_un.d_val -=
5155 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
5157 break;
5159 get_vma:
5160 s = bfd_get_section_by_name (output_bfd, name);
5161 dyn.d_un.d_ptr = (s ? s->vma : 0);
5162 break;
5164 get_size:
5165 s = bfd_get_section_by_name (output_bfd, name);
5166 dyn.d_un.d_val =
5167 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
5168 break;
5171 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5174 /* Initialize the PLT0 entry */
5175 if (splt->_raw_size > 0)
5177 bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
5178 bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
5179 bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
5180 bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
5182 /* The next two words will be filled in by ld.so */
5183 bfd_put_64 (output_bfd, (bfd_vma) 0, splt->contents + 16);
5184 bfd_put_64 (output_bfd, (bfd_vma) 0, splt->contents + 24);
5186 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
5190 return TRUE;
5193 /* We need to use a special link routine to handle the .mdebug section.
5194 We need to merge all instances of these sections together, not write
5195 them all out sequentially. */
5197 static bfd_boolean
5198 elf64_alpha_final_link (abfd, info)
5199 bfd *abfd;
5200 struct bfd_link_info *info;
5202 asection *o;
5203 struct bfd_link_order *p;
5204 asection *mdebug_sec;
5205 struct ecoff_debug_info debug;
5206 const struct ecoff_debug_swap *swap
5207 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
5208 HDRR *symhdr = &debug.symbolic_header;
5209 PTR mdebug_handle = NULL;
5211 /* Go through the sections and collect the mdebug information. */
5212 mdebug_sec = NULL;
5213 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5215 if (strcmp (o->name, ".mdebug") == 0)
5217 struct extsym_info einfo;
5219 /* We have found the .mdebug section in the output file.
5220 Look through all the link_orders comprising it and merge
5221 the information together. */
5222 symhdr->magic = swap->sym_magic;
5223 /* FIXME: What should the version stamp be? */
5224 symhdr->vstamp = 0;
5225 symhdr->ilineMax = 0;
5226 symhdr->cbLine = 0;
5227 symhdr->idnMax = 0;
5228 symhdr->ipdMax = 0;
5229 symhdr->isymMax = 0;
5230 symhdr->ioptMax = 0;
5231 symhdr->iauxMax = 0;
5232 symhdr->issMax = 0;
5233 symhdr->issExtMax = 0;
5234 symhdr->ifdMax = 0;
5235 symhdr->crfd = 0;
5236 symhdr->iextMax = 0;
5238 /* We accumulate the debugging information itself in the
5239 debug_info structure. */
5240 debug.line = NULL;
5241 debug.external_dnr = NULL;
5242 debug.external_pdr = NULL;
5243 debug.external_sym = NULL;
5244 debug.external_opt = NULL;
5245 debug.external_aux = NULL;
5246 debug.ss = NULL;
5247 debug.ssext = debug.ssext_end = NULL;
5248 debug.external_fdr = NULL;
5249 debug.external_rfd = NULL;
5250 debug.external_ext = debug.external_ext_end = NULL;
5252 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
5253 if (mdebug_handle == (PTR) NULL)
5254 return FALSE;
5256 if (1)
5258 asection *s;
5259 EXTR esym;
5260 bfd_vma last = 0;
5261 unsigned int i;
5262 static const char * const name[] =
5264 ".text", ".init", ".fini", ".data",
5265 ".rodata", ".sdata", ".sbss", ".bss"
5267 static const int sc[] = { scText, scInit, scFini, scData,
5268 scRData, scSData, scSBss, scBss };
5270 esym.jmptbl = 0;
5271 esym.cobol_main = 0;
5272 esym.weakext = 0;
5273 esym.reserved = 0;
5274 esym.ifd = ifdNil;
5275 esym.asym.iss = issNil;
5276 esym.asym.st = stLocal;
5277 esym.asym.reserved = 0;
5278 esym.asym.index = indexNil;
5279 for (i = 0; i < 8; i++)
5281 esym.asym.sc = sc[i];
5282 s = bfd_get_section_by_name (abfd, name[i]);
5283 if (s != NULL)
5285 esym.asym.value = s->vma;
5286 last = s->vma + s->_raw_size;
5288 else
5289 esym.asym.value = last;
5291 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
5292 name[i], &esym))
5293 return FALSE;
5297 for (p = o->link_order_head;
5298 p != (struct bfd_link_order *) NULL;
5299 p = p->next)
5301 asection *input_section;
5302 bfd *input_bfd;
5303 const struct ecoff_debug_swap *input_swap;
5304 struct ecoff_debug_info input_debug;
5305 char *eraw_src;
5306 char *eraw_end;
5308 if (p->type != bfd_indirect_link_order)
5310 if (p->type == bfd_data_link_order)
5311 continue;
5312 abort ();
5315 input_section = p->u.indirect.section;
5316 input_bfd = input_section->owner;
5318 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
5319 || (get_elf_backend_data (input_bfd)
5320 ->elf_backend_ecoff_debug_swap) == NULL)
5322 /* I don't know what a non ALPHA ELF bfd would be
5323 doing with a .mdebug section, but I don't really
5324 want to deal with it. */
5325 continue;
5328 input_swap = (get_elf_backend_data (input_bfd)
5329 ->elf_backend_ecoff_debug_swap);
5331 BFD_ASSERT (p->size == input_section->_raw_size);
5333 /* The ECOFF linking code expects that we have already
5334 read in the debugging information and set up an
5335 ecoff_debug_info structure, so we do that now. */
5336 if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
5337 &input_debug))
5338 return FALSE;
5340 if (! (bfd_ecoff_debug_accumulate
5341 (mdebug_handle, abfd, &debug, swap, input_bfd,
5342 &input_debug, input_swap, info)))
5343 return FALSE;
5345 /* Loop through the external symbols. For each one with
5346 interesting information, try to find the symbol in
5347 the linker global hash table and save the information
5348 for the output external symbols. */
5349 eraw_src = input_debug.external_ext;
5350 eraw_end = (eraw_src
5351 + (input_debug.symbolic_header.iextMax
5352 * input_swap->external_ext_size));
5353 for (;
5354 eraw_src < eraw_end;
5355 eraw_src += input_swap->external_ext_size)
5357 EXTR ext;
5358 const char *name;
5359 struct alpha_elf_link_hash_entry *h;
5361 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
5362 if (ext.asym.sc == scNil
5363 || ext.asym.sc == scUndefined
5364 || ext.asym.sc == scSUndefined)
5365 continue;
5367 name = input_debug.ssext + ext.asym.iss;
5368 h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
5369 name, FALSE, FALSE, TRUE);
5370 if (h == NULL || h->esym.ifd != -2)
5371 continue;
5373 if (ext.ifd != -1)
5375 BFD_ASSERT (ext.ifd
5376 < input_debug.symbolic_header.ifdMax);
5377 ext.ifd = input_debug.ifdmap[ext.ifd];
5380 h->esym = ext;
5383 /* Free up the information we just read. */
5384 free (input_debug.line);
5385 free (input_debug.external_dnr);
5386 free (input_debug.external_pdr);
5387 free (input_debug.external_sym);
5388 free (input_debug.external_opt);
5389 free (input_debug.external_aux);
5390 free (input_debug.ss);
5391 free (input_debug.ssext);
5392 free (input_debug.external_fdr);
5393 free (input_debug.external_rfd);
5394 free (input_debug.external_ext);
5396 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5397 elf_link_input_bfd ignores this section. */
5398 input_section->flags &=~ SEC_HAS_CONTENTS;
5401 /* Build the external symbol information. */
5402 einfo.abfd = abfd;
5403 einfo.info = info;
5404 einfo.debug = &debug;
5405 einfo.swap = swap;
5406 einfo.failed = FALSE;
5407 elf_link_hash_traverse (elf_hash_table (info),
5408 elf64_alpha_output_extsym,
5409 (PTR) &einfo);
5410 if (einfo.failed)
5411 return FALSE;
5413 /* Set the size of the .mdebug section. */
5414 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
5416 /* Skip this section later on (I don't think this currently
5417 matters, but someday it might). */
5418 o->link_order_head = (struct bfd_link_order *) NULL;
5420 mdebug_sec = o;
5424 /* Invoke the regular ELF backend linker to do all the work. */
5425 if (! bfd_elf64_bfd_final_link (abfd, info))
5426 return FALSE;
5428 /* Now write out the computed sections. */
5430 /* The .got subsections... */
5432 bfd *i, *dynobj = elf_hash_table(info)->dynobj;
5433 for (i = alpha_elf_hash_table(info)->got_list;
5434 i != NULL;
5435 i = alpha_elf_tdata(i)->got_link_next)
5437 asection *sgot;
5439 /* elf_bfd_final_link already did everything in dynobj. */
5440 if (i == dynobj)
5441 continue;
5443 sgot = alpha_elf_tdata(i)->got;
5444 if (! bfd_set_section_contents (abfd, sgot->output_section,
5445 sgot->contents,
5446 (file_ptr) sgot->output_offset,
5447 sgot->_raw_size))
5448 return FALSE;
5452 if (mdebug_sec != (asection *) NULL)
5454 BFD_ASSERT (abfd->output_has_begun);
5455 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5456 swap, info,
5457 mdebug_sec->filepos))
5458 return FALSE;
5460 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5463 return TRUE;
5466 static enum elf_reloc_type_class
5467 elf64_alpha_reloc_type_class (rela)
5468 const Elf_Internal_Rela *rela;
5470 switch ((int) ELF64_R_TYPE (rela->r_info))
5472 case R_ALPHA_RELATIVE:
5473 return reloc_class_relative;
5474 case R_ALPHA_JMP_SLOT:
5475 return reloc_class_plt;
5476 case R_ALPHA_COPY:
5477 return reloc_class_copy;
5478 default:
5479 return reloc_class_normal;
5483 /* ECOFF swapping routines. These are used when dealing with the
5484 .mdebug section, which is in the ECOFF debugging format. Copied
5485 from elf32-mips.c. */
5486 static const struct ecoff_debug_swap
5487 elf64_alpha_ecoff_debug_swap =
5489 /* Symbol table magic number. */
5490 magicSym2,
5491 /* Alignment of debugging information. E.g., 4. */
5493 /* Sizes of external symbolic information. */
5494 sizeof (struct hdr_ext),
5495 sizeof (struct dnr_ext),
5496 sizeof (struct pdr_ext),
5497 sizeof (struct sym_ext),
5498 sizeof (struct opt_ext),
5499 sizeof (struct fdr_ext),
5500 sizeof (struct rfd_ext),
5501 sizeof (struct ext_ext),
5502 /* Functions to swap in external symbolic data. */
5503 ecoff_swap_hdr_in,
5504 ecoff_swap_dnr_in,
5505 ecoff_swap_pdr_in,
5506 ecoff_swap_sym_in,
5507 ecoff_swap_opt_in,
5508 ecoff_swap_fdr_in,
5509 ecoff_swap_rfd_in,
5510 ecoff_swap_ext_in,
5511 _bfd_ecoff_swap_tir_in,
5512 _bfd_ecoff_swap_rndx_in,
5513 /* Functions to swap out external symbolic data. */
5514 ecoff_swap_hdr_out,
5515 ecoff_swap_dnr_out,
5516 ecoff_swap_pdr_out,
5517 ecoff_swap_sym_out,
5518 ecoff_swap_opt_out,
5519 ecoff_swap_fdr_out,
5520 ecoff_swap_rfd_out,
5521 ecoff_swap_ext_out,
5522 _bfd_ecoff_swap_tir_out,
5523 _bfd_ecoff_swap_rndx_out,
5524 /* Function to read in symbolic data. */
5525 elf64_alpha_read_ecoff_info
5528 /* Use a non-standard hash bucket size of 8. */
5530 static const struct elf_size_info alpha_elf_size_info =
5532 sizeof (Elf64_External_Ehdr),
5533 sizeof (Elf64_External_Phdr),
5534 sizeof (Elf64_External_Shdr),
5535 sizeof (Elf64_External_Rel),
5536 sizeof (Elf64_External_Rela),
5537 sizeof (Elf64_External_Sym),
5538 sizeof (Elf64_External_Dyn),
5539 sizeof (Elf_External_Note),
5542 64, 8,
5543 ELFCLASS64, EV_CURRENT,
5544 bfd_elf64_write_out_phdrs,
5545 bfd_elf64_write_shdrs_and_ehdr,
5546 bfd_elf64_write_relocs,
5547 bfd_elf64_swap_symbol_in,
5548 bfd_elf64_swap_symbol_out,
5549 bfd_elf64_slurp_reloc_table,
5550 bfd_elf64_slurp_symbol_table,
5551 bfd_elf64_swap_dyn_in,
5552 bfd_elf64_swap_dyn_out,
5553 bfd_elf64_swap_reloc_in,
5554 bfd_elf64_swap_reloc_out,
5555 bfd_elf64_swap_reloca_in,
5556 bfd_elf64_swap_reloca_out
5559 #define TARGET_LITTLE_SYM bfd_elf64_alpha_vec
5560 #define TARGET_LITTLE_NAME "elf64-alpha"
5561 #define ELF_ARCH bfd_arch_alpha
5562 #define ELF_MACHINE_CODE EM_ALPHA
5563 #define ELF_MAXPAGESIZE 0x10000
5565 #define bfd_elf64_bfd_link_hash_table_create \
5566 elf64_alpha_bfd_link_hash_table_create
5568 #define bfd_elf64_bfd_reloc_type_lookup \
5569 elf64_alpha_bfd_reloc_type_lookup
5570 #define elf_info_to_howto \
5571 elf64_alpha_info_to_howto
5573 #define bfd_elf64_mkobject \
5574 elf64_alpha_mkobject
5575 #define elf_backend_object_p \
5576 elf64_alpha_object_p
5578 #define elf_backend_section_from_shdr \
5579 elf64_alpha_section_from_shdr
5580 #define elf_backend_section_flags \
5581 elf64_alpha_section_flags
5582 #define elf_backend_fake_sections \
5583 elf64_alpha_fake_sections
5585 #define bfd_elf64_bfd_is_local_label_name \
5586 elf64_alpha_is_local_label_name
5587 #define bfd_elf64_find_nearest_line \
5588 elf64_alpha_find_nearest_line
5589 #define bfd_elf64_bfd_relax_section \
5590 elf64_alpha_relax_section
5592 #define elf_backend_add_symbol_hook \
5593 elf64_alpha_add_symbol_hook
5594 #define elf_backend_check_relocs \
5595 elf64_alpha_check_relocs
5596 #define elf_backend_create_dynamic_sections \
5597 elf64_alpha_create_dynamic_sections
5598 #define elf_backend_adjust_dynamic_symbol \
5599 elf64_alpha_adjust_dynamic_symbol
5600 #define elf_backend_always_size_sections \
5601 elf64_alpha_always_size_sections
5602 #define elf_backend_size_dynamic_sections \
5603 elf64_alpha_size_dynamic_sections
5604 #define elf_backend_relocate_section \
5605 elf64_alpha_relocate_section
5606 #define elf_backend_finish_dynamic_symbol \
5607 elf64_alpha_finish_dynamic_symbol
5608 #define elf_backend_finish_dynamic_sections \
5609 elf64_alpha_finish_dynamic_sections
5610 #define bfd_elf64_bfd_final_link \
5611 elf64_alpha_final_link
5612 #define elf_backend_reloc_type_class \
5613 elf64_alpha_reloc_type_class
5615 #define elf_backend_ecoff_debug_swap \
5616 &elf64_alpha_ecoff_debug_swap
5618 #define elf_backend_size_info \
5619 alpha_elf_size_info
5621 /* A few constants that determine how the .plt section is set up. */
5622 #define elf_backend_want_got_plt 0
5623 #define elf_backend_plt_readonly 0
5624 #define elf_backend_want_plt_sym 1
5625 #define elf_backend_got_header_size 0
5626 #define elf_backend_plt_header_size PLT_HEADER_SIZE
5628 #include "elf64-target.h"
5630 /* FreeBSD support. */
5632 #undef TARGET_LITTLE_SYM
5633 #define TARGET_LITTLE_SYM bfd_elf64_alpha_freebsd_vec
5634 #undef TARGET_LITTLE_NAME
5635 #define TARGET_LITTLE_NAME "elf64-alpha-freebsd"
5637 /* The kernel recognizes executables as valid only if they carry a
5638 "FreeBSD" label in the ELF header. So we put this label on all
5639 executables and (for simplicity) also all other object files. */
5641 static void elf64_alpha_fbsd_post_process_headers
5642 PARAMS ((bfd *, struct bfd_link_info *));
5644 static void
5645 elf64_alpha_fbsd_post_process_headers (abfd, link_info)
5646 bfd * abfd;
5647 struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
5649 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
5651 i_ehdrp = elf_elfheader (abfd);
5653 /* Put an ABI label supported by FreeBSD >= 4.1. */
5654 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
5655 #ifdef OLD_FREEBSD_ABI_LABEL
5656 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
5657 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
5658 #endif
5661 #undef elf_backend_post_process_headers
5662 #define elf_backend_post_process_headers \
5663 elf64_alpha_fbsd_post_process_headers
5665 #undef elf64_bed
5666 #define elf64_bed elf64_alpha_fbsd_bed
5668 #include "elf64-target.h"