bfd/
[binutils.git] / bfd / elf64-alpha.c
blob8bd03c20fc3980f4e17b0f2b983aa0c906e8143f
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 bfd_boolean 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? This differs from the
273 generic version in that we never need to consider function pointer
274 equality wrt PLT entries -- we don't create a PLT entry if a symbol's
275 address is ever taken. */
277 static inline bfd_boolean
278 alpha_elf_dynamic_symbol_p (h, info)
279 struct elf_link_hash_entry *h;
280 struct bfd_link_info *info;
282 return _bfd_elf_dynamic_symbol_p (h, info, 0);
285 /* Create an entry in a Alpha ELF linker hash table. */
287 static struct bfd_hash_entry *
288 elf64_alpha_link_hash_newfunc (entry, table, string)
289 struct bfd_hash_entry *entry;
290 struct bfd_hash_table *table;
291 const char *string;
293 struct alpha_elf_link_hash_entry *ret =
294 (struct alpha_elf_link_hash_entry *) entry;
296 /* Allocate the structure if it has not already been allocated by a
297 subclass. */
298 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
299 ret = ((struct alpha_elf_link_hash_entry *)
300 bfd_hash_allocate (table,
301 sizeof (struct alpha_elf_link_hash_entry)));
302 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
303 return (struct bfd_hash_entry *) ret;
305 /* Call the allocation method of the superclass. */
306 ret = ((struct alpha_elf_link_hash_entry *)
307 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
308 table, string));
309 if (ret != (struct alpha_elf_link_hash_entry *) NULL)
311 /* Set local fields. */
312 memset (&ret->esym, 0, sizeof (EXTR));
313 /* We use -2 as a marker to indicate that the information has
314 not been set. -1 means there is no associated ifd. */
315 ret->esym.ifd = -2;
316 ret->flags = 0;
317 ret->got_entries = NULL;
318 ret->reloc_entries = NULL;
321 return (struct bfd_hash_entry *) ret;
324 /* Create a Alpha ELF linker hash table. */
326 static struct bfd_link_hash_table *
327 elf64_alpha_bfd_link_hash_table_create (abfd)
328 bfd *abfd;
330 struct alpha_elf_link_hash_table *ret;
331 bfd_size_type amt = sizeof (struct alpha_elf_link_hash_table);
333 ret = (struct alpha_elf_link_hash_table *) bfd_zmalloc (amt);
334 if (ret == (struct alpha_elf_link_hash_table *) NULL)
335 return NULL;
337 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
338 elf64_alpha_link_hash_newfunc))
340 free (ret);
341 return NULL;
344 return &ret->root.root;
347 /* We have some private fields hanging off of the elf_tdata structure. */
349 struct alpha_elf_obj_tdata
351 struct elf_obj_tdata root;
353 /* For every input file, these are the got entries for that object's
354 local symbols. */
355 struct alpha_elf_got_entry ** local_got_entries;
357 /* For every input file, this is the object that owns the got that
358 this input file uses. */
359 bfd *gotobj;
361 /* For every got, this is a linked list through the objects using this got */
362 bfd *in_got_link_next;
364 /* For every got, this is a link to the next got subsegment. */
365 bfd *got_link_next;
367 /* For every got, this is the section. */
368 asection *got;
370 /* For every got, this is it's total number of words. */
371 int total_got_size;
373 /* For every got, this is the sum of the number of words required
374 to hold all of the member object's local got. */
375 int local_got_size;
378 #define alpha_elf_tdata(abfd) \
379 ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
381 static bfd_boolean
382 elf64_alpha_mkobject (abfd)
383 bfd *abfd;
385 bfd_size_type amt = sizeof (struct alpha_elf_obj_tdata);
386 abfd->tdata.any = bfd_zalloc (abfd, amt);
387 if (abfd->tdata.any == NULL)
388 return FALSE;
389 return TRUE;
392 static bfd_boolean
393 elf64_alpha_object_p (abfd)
394 bfd *abfd;
396 /* Set the right machine number for an Alpha ELF file. */
397 return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
400 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
401 from smaller values. Start with zero, widen, *then* decrement. */
402 #define MINUS_ONE (((bfd_vma)0) - 1)
404 #define SKIP_HOWTO(N) \
405 HOWTO(N, 0, 0, 0, 0, 0, 0, elf64_alpha_reloc_bad, 0, 0, 0, 0, 0)
407 static reloc_howto_type elf64_alpha_howto_table[] =
409 HOWTO (R_ALPHA_NONE, /* type */
410 0, /* rightshift */
411 0, /* size (0 = byte, 1 = short, 2 = long) */
412 8, /* bitsize */
413 TRUE, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_dont, /* complain_on_overflow */
416 elf64_alpha_reloc_nil, /* special_function */
417 "NONE", /* name */
418 FALSE, /* partial_inplace */
419 0, /* src_mask */
420 0, /* dst_mask */
421 TRUE), /* pcrel_offset */
423 /* A 32 bit reference to a symbol. */
424 HOWTO (R_ALPHA_REFLONG, /* type */
425 0, /* rightshift */
426 2, /* size (0 = byte, 1 = short, 2 = long) */
427 32, /* bitsize */
428 FALSE, /* pc_relative */
429 0, /* bitpos */
430 complain_overflow_bitfield, /* complain_on_overflow */
431 0, /* special_function */
432 "REFLONG", /* name */
433 FALSE, /* partial_inplace */
434 0xffffffff, /* src_mask */
435 0xffffffff, /* dst_mask */
436 FALSE), /* pcrel_offset */
438 /* A 64 bit reference to a symbol. */
439 HOWTO (R_ALPHA_REFQUAD, /* type */
440 0, /* rightshift */
441 4, /* size (0 = byte, 1 = short, 2 = long) */
442 64, /* bitsize */
443 FALSE, /* pc_relative */
444 0, /* bitpos */
445 complain_overflow_bitfield, /* complain_on_overflow */
446 0, /* special_function */
447 "REFQUAD", /* name */
448 FALSE, /* partial_inplace */
449 MINUS_ONE, /* src_mask */
450 MINUS_ONE, /* dst_mask */
451 FALSE), /* pcrel_offset */
453 /* A 32 bit GP relative offset. This is just like REFLONG except
454 that when the value is used the value of the gp register will be
455 added in. */
456 HOWTO (R_ALPHA_GPREL32, /* type */
457 0, /* rightshift */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
459 32, /* bitsize */
460 FALSE, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_bitfield, /* complain_on_overflow */
463 0, /* special_function */
464 "GPREL32", /* name */
465 FALSE, /* partial_inplace */
466 0xffffffff, /* src_mask */
467 0xffffffff, /* dst_mask */
468 FALSE), /* pcrel_offset */
470 /* Used for an instruction that refers to memory off the GP register. */
471 HOWTO (R_ALPHA_LITERAL, /* type */
472 0, /* rightshift */
473 1, /* size (0 = byte, 1 = short, 2 = long) */
474 16, /* bitsize */
475 FALSE, /* pc_relative */
476 0, /* bitpos */
477 complain_overflow_signed, /* complain_on_overflow */
478 0, /* special_function */
479 "ELF_LITERAL", /* name */
480 FALSE, /* partial_inplace */
481 0xffff, /* src_mask */
482 0xffff, /* dst_mask */
483 FALSE), /* pcrel_offset */
485 /* This reloc only appears immediately following an ELF_LITERAL reloc.
486 It identifies a use of the literal. The symbol index is special:
487 1 means the literal address is in the base register of a memory
488 format instruction; 2 means the literal address is in the byte
489 offset register of a byte-manipulation instruction; 3 means the
490 literal address is in the target register of a jsr instruction.
491 This does not actually do any relocation. */
492 HOWTO (R_ALPHA_LITUSE, /* type */
493 0, /* rightshift */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
495 32, /* bitsize */
496 FALSE, /* pc_relative */
497 0, /* bitpos */
498 complain_overflow_dont, /* complain_on_overflow */
499 elf64_alpha_reloc_nil, /* special_function */
500 "LITUSE", /* name */
501 FALSE, /* partial_inplace */
502 0, /* src_mask */
503 0, /* dst_mask */
504 FALSE), /* pcrel_offset */
506 /* Load the gp register. This is always used for a ldah instruction
507 which loads the upper 16 bits of the gp register. The symbol
508 index of the GPDISP instruction is an offset in bytes to the lda
509 instruction that loads the lower 16 bits. The value to use for
510 the relocation is the difference between the GP value and the
511 current location; the load will always be done against a register
512 holding the current address.
514 NOTE: Unlike ECOFF, partial in-place relocation is not done. If
515 any offset is present in the instructions, it is an offset from
516 the register to the ldah instruction. This lets us avoid any
517 stupid hackery like inventing a gp value to do partial relocation
518 against. Also unlike ECOFF, we do the whole relocation off of
519 the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
520 space consuming bit, that, since all the information was present
521 in the GPDISP_HI16 reloc. */
522 HOWTO (R_ALPHA_GPDISP, /* type */
523 16, /* rightshift */
524 2, /* size (0 = byte, 1 = short, 2 = long) */
525 16, /* bitsize */
526 FALSE, /* pc_relative */
527 0, /* bitpos */
528 complain_overflow_dont, /* complain_on_overflow */
529 elf64_alpha_reloc_gpdisp, /* special_function */
530 "GPDISP", /* name */
531 FALSE, /* partial_inplace */
532 0xffff, /* src_mask */
533 0xffff, /* dst_mask */
534 TRUE), /* pcrel_offset */
536 /* A 21 bit branch. */
537 HOWTO (R_ALPHA_BRADDR, /* type */
538 2, /* rightshift */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
540 21, /* bitsize */
541 TRUE, /* pc_relative */
542 0, /* bitpos */
543 complain_overflow_signed, /* complain_on_overflow */
544 0, /* special_function */
545 "BRADDR", /* name */
546 FALSE, /* partial_inplace */
547 0x1fffff, /* src_mask */
548 0x1fffff, /* dst_mask */
549 TRUE), /* pcrel_offset */
551 /* A hint for a jump to a register. */
552 HOWTO (R_ALPHA_HINT, /* type */
553 2, /* rightshift */
554 1, /* size (0 = byte, 1 = short, 2 = long) */
555 14, /* bitsize */
556 TRUE, /* pc_relative */
557 0, /* bitpos */
558 complain_overflow_dont, /* complain_on_overflow */
559 0, /* special_function */
560 "HINT", /* name */
561 FALSE, /* partial_inplace */
562 0x3fff, /* src_mask */
563 0x3fff, /* dst_mask */
564 TRUE), /* pcrel_offset */
566 /* 16 bit PC relative offset. */
567 HOWTO (R_ALPHA_SREL16, /* type */
568 0, /* rightshift */
569 1, /* size (0 = byte, 1 = short, 2 = long) */
570 16, /* bitsize */
571 TRUE, /* pc_relative */
572 0, /* bitpos */
573 complain_overflow_signed, /* complain_on_overflow */
574 0, /* special_function */
575 "SREL16", /* name */
576 FALSE, /* partial_inplace */
577 0xffff, /* src_mask */
578 0xffff, /* dst_mask */
579 TRUE), /* pcrel_offset */
581 /* 32 bit PC relative offset. */
582 HOWTO (R_ALPHA_SREL32, /* type */
583 0, /* rightshift */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
585 32, /* bitsize */
586 TRUE, /* pc_relative */
587 0, /* bitpos */
588 complain_overflow_signed, /* complain_on_overflow */
589 0, /* special_function */
590 "SREL32", /* name */
591 FALSE, /* partial_inplace */
592 0xffffffff, /* src_mask */
593 0xffffffff, /* dst_mask */
594 TRUE), /* pcrel_offset */
596 /* A 64 bit PC relative offset. */
597 HOWTO (R_ALPHA_SREL64, /* type */
598 0, /* rightshift */
599 4, /* size (0 = byte, 1 = short, 2 = long) */
600 64, /* bitsize */
601 TRUE, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_signed, /* complain_on_overflow */
604 0, /* special_function */
605 "SREL64", /* name */
606 FALSE, /* partial_inplace */
607 MINUS_ONE, /* src_mask */
608 MINUS_ONE, /* dst_mask */
609 TRUE), /* pcrel_offset */
611 /* Skip 12 - 16; deprecated ECOFF relocs. */
612 SKIP_HOWTO (12),
613 SKIP_HOWTO (13),
614 SKIP_HOWTO (14),
615 SKIP_HOWTO (15),
616 SKIP_HOWTO (16),
618 /* The high 16 bits of the displacement from GP to the target. */
619 HOWTO (R_ALPHA_GPRELHIGH,
620 0, /* rightshift */
621 1, /* size (0 = byte, 1 = short, 2 = long) */
622 16, /* bitsize */
623 FALSE, /* pc_relative */
624 0, /* bitpos */
625 complain_overflow_signed, /* complain_on_overflow */
626 0, /* special_function */
627 "GPRELHIGH", /* name */
628 FALSE, /* partial_inplace */
629 0xffff, /* src_mask */
630 0xffff, /* dst_mask */
631 FALSE), /* pcrel_offset */
633 /* The low 16 bits of the displacement from GP to the target. */
634 HOWTO (R_ALPHA_GPRELLOW,
635 0, /* rightshift */
636 1, /* size (0 = byte, 1 = short, 2 = long) */
637 16, /* bitsize */
638 FALSE, /* pc_relative */
639 0, /* bitpos */
640 complain_overflow_dont, /* complain_on_overflow */
641 0, /* special_function */
642 "GPRELLOW", /* name */
643 FALSE, /* partial_inplace */
644 0xffff, /* src_mask */
645 0xffff, /* dst_mask */
646 FALSE), /* pcrel_offset */
648 /* A 16-bit displacement from the GP to the target. */
649 HOWTO (R_ALPHA_GPREL16,
650 0, /* rightshift */
651 1, /* size (0 = byte, 1 = short, 2 = long) */
652 16, /* bitsize */
653 FALSE, /* pc_relative */
654 0, /* bitpos */
655 complain_overflow_signed, /* complain_on_overflow */
656 0, /* special_function */
657 "GPREL16", /* name */
658 FALSE, /* partial_inplace */
659 0xffff, /* src_mask */
660 0xffff, /* dst_mask */
661 FALSE), /* pcrel_offset */
663 /* Skip 20 - 23; deprecated ECOFF relocs. */
664 SKIP_HOWTO (20),
665 SKIP_HOWTO (21),
666 SKIP_HOWTO (22),
667 SKIP_HOWTO (23),
669 /* Misc ELF relocations. */
671 /* A dynamic relocation to copy the target into our .dynbss section. */
672 /* Not generated, as all Alpha objects use PIC, so it is not needed. It
673 is present because every other ELF has one, but should not be used
674 because .dynbss is an ugly thing. */
675 HOWTO (R_ALPHA_COPY,
679 FALSE,
681 complain_overflow_dont,
682 bfd_elf_generic_reloc,
683 "COPY",
684 FALSE,
687 TRUE),
689 /* A dynamic relocation for a .got entry. */
690 HOWTO (R_ALPHA_GLOB_DAT,
694 FALSE,
696 complain_overflow_dont,
697 bfd_elf_generic_reloc,
698 "GLOB_DAT",
699 FALSE,
702 TRUE),
704 /* A dynamic relocation for a .plt entry. */
705 HOWTO (R_ALPHA_JMP_SLOT,
709 FALSE,
711 complain_overflow_dont,
712 bfd_elf_generic_reloc,
713 "JMP_SLOT",
714 FALSE,
717 TRUE),
719 /* A dynamic relocation to add the base of the DSO to a 64-bit field. */
720 HOWTO (R_ALPHA_RELATIVE,
724 FALSE,
726 complain_overflow_dont,
727 bfd_elf_generic_reloc,
728 "RELATIVE",
729 FALSE,
732 TRUE),
734 /* A 21 bit branch that adjusts for gp loads. */
735 HOWTO (R_ALPHA_BRSGP, /* type */
736 2, /* rightshift */
737 2, /* size (0 = byte, 1 = short, 2 = long) */
738 21, /* bitsize */
739 TRUE, /* pc_relative */
740 0, /* bitpos */
741 complain_overflow_signed, /* complain_on_overflow */
742 0, /* special_function */
743 "BRSGP", /* name */
744 FALSE, /* partial_inplace */
745 0x1fffff, /* src_mask */
746 0x1fffff, /* dst_mask */
747 TRUE), /* pcrel_offset */
749 /* Creates a tls_index for the symbol in the got. */
750 HOWTO (R_ALPHA_TLSGD, /* type */
751 0, /* rightshift */
752 1, /* size (0 = byte, 1 = short, 2 = long) */
753 16, /* bitsize */
754 FALSE, /* pc_relative */
755 0, /* bitpos */
756 complain_overflow_signed, /* complain_on_overflow */
757 0, /* special_function */
758 "TLSGD", /* name */
759 FALSE, /* partial_inplace */
760 0xffff, /* src_mask */
761 0xffff, /* dst_mask */
762 FALSE), /* pcrel_offset */
764 /* Creates a tls_index for the (current) module in the got. */
765 HOWTO (R_ALPHA_TLSLDM, /* type */
766 0, /* rightshift */
767 1, /* size (0 = byte, 1 = short, 2 = long) */
768 16, /* bitsize */
769 FALSE, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_signed, /* complain_on_overflow */
772 0, /* special_function */
773 "TLSLDM", /* name */
774 FALSE, /* partial_inplace */
775 0xffff, /* src_mask */
776 0xffff, /* dst_mask */
777 FALSE), /* pcrel_offset */
779 /* A dynamic relocation for a DTP module entry. */
780 HOWTO (R_ALPHA_DTPMOD64, /* type */
781 0, /* rightshift */
782 4, /* size (0 = byte, 1 = short, 2 = long) */
783 64, /* bitsize */
784 FALSE, /* pc_relative */
785 0, /* bitpos */
786 complain_overflow_bitfield, /* complain_on_overflow */
787 0, /* special_function */
788 "DTPMOD64", /* name */
789 FALSE, /* partial_inplace */
790 MINUS_ONE, /* src_mask */
791 MINUS_ONE, /* dst_mask */
792 FALSE), /* pcrel_offset */
794 /* Creates a 64-bit offset in the got for the displacement
795 from DTP to the target. */
796 HOWTO (R_ALPHA_GOTDTPREL, /* type */
797 0, /* rightshift */
798 1, /* size (0 = byte, 1 = short, 2 = long) */
799 16, /* bitsize */
800 FALSE, /* pc_relative */
801 0, /* bitpos */
802 complain_overflow_signed, /* complain_on_overflow */
803 0, /* special_function */
804 "GOTDTPREL", /* name */
805 FALSE, /* partial_inplace */
806 0xffff, /* src_mask */
807 0xffff, /* dst_mask */
808 FALSE), /* pcrel_offset */
810 /* A dynamic relocation for a displacement from DTP to the target. */
811 HOWTO (R_ALPHA_DTPREL64, /* type */
812 0, /* rightshift */
813 4, /* size (0 = byte, 1 = short, 2 = long) */
814 64, /* bitsize */
815 FALSE, /* pc_relative */
816 0, /* bitpos */
817 complain_overflow_bitfield, /* complain_on_overflow */
818 0, /* special_function */
819 "DTPREL64", /* name */
820 FALSE, /* partial_inplace */
821 MINUS_ONE, /* src_mask */
822 MINUS_ONE, /* dst_mask */
823 FALSE), /* pcrel_offset */
825 /* The high 16 bits of the displacement from DTP to the target. */
826 HOWTO (R_ALPHA_DTPRELHI, /* type */
827 0, /* rightshift */
828 1, /* size (0 = byte, 1 = short, 2 = long) */
829 16, /* bitsize */
830 FALSE, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_signed, /* complain_on_overflow */
833 0, /* special_function */
834 "DTPRELHI", /* name */
835 FALSE, /* partial_inplace */
836 0xffff, /* src_mask */
837 0xffff, /* dst_mask */
838 FALSE), /* pcrel_offset */
840 /* The low 16 bits of the displacement from DTP to the target. */
841 HOWTO (R_ALPHA_DTPRELLO, /* type */
842 0, /* rightshift */
843 1, /* size (0 = byte, 1 = short, 2 = long) */
844 16, /* bitsize */
845 FALSE, /* pc_relative */
846 0, /* bitpos */
847 complain_overflow_dont, /* complain_on_overflow */
848 0, /* special_function */
849 "DTPRELLO", /* name */
850 FALSE, /* partial_inplace */
851 0xffff, /* src_mask */
852 0xffff, /* dst_mask */
853 FALSE), /* pcrel_offset */
855 /* A 16-bit displacement from DTP to the target. */
856 HOWTO (R_ALPHA_DTPREL16, /* type */
857 0, /* rightshift */
858 1, /* size (0 = byte, 1 = short, 2 = long) */
859 16, /* bitsize */
860 FALSE, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_signed, /* complain_on_overflow */
863 0, /* special_function */
864 "DTPREL16", /* name */
865 FALSE, /* partial_inplace */
866 0xffff, /* src_mask */
867 0xffff, /* dst_mask */
868 FALSE), /* pcrel_offset */
870 /* Creates a 64-bit offset in the got for the displacement
871 from TP to the target. */
872 HOWTO (R_ALPHA_GOTTPREL, /* type */
873 0, /* rightshift */
874 1, /* size (0 = byte, 1 = short, 2 = long) */
875 16, /* bitsize */
876 FALSE, /* pc_relative */
877 0, /* bitpos */
878 complain_overflow_signed, /* complain_on_overflow */
879 0, /* special_function */
880 "GOTTPREL", /* name */
881 FALSE, /* partial_inplace */
882 0xffff, /* src_mask */
883 0xffff, /* dst_mask */
884 FALSE), /* pcrel_offset */
886 /* A dynamic relocation for a displacement from TP to the target. */
887 HOWTO (R_ALPHA_TPREL64, /* type */
888 0, /* rightshift */
889 4, /* size (0 = byte, 1 = short, 2 = long) */
890 64, /* bitsize */
891 FALSE, /* pc_relative */
892 0, /* bitpos */
893 complain_overflow_bitfield, /* complain_on_overflow */
894 0, /* special_function */
895 "TPREL64", /* name */
896 FALSE, /* partial_inplace */
897 MINUS_ONE, /* src_mask */
898 MINUS_ONE, /* dst_mask */
899 FALSE), /* pcrel_offset */
901 /* The high 16 bits of the displacement from TP to the target. */
902 HOWTO (R_ALPHA_TPRELHI, /* type */
903 0, /* rightshift */
904 1, /* size (0 = byte, 1 = short, 2 = long) */
905 16, /* bitsize */
906 FALSE, /* pc_relative */
907 0, /* bitpos */
908 complain_overflow_signed, /* complain_on_overflow */
909 0, /* special_function */
910 "TPRELHI", /* name */
911 FALSE, /* partial_inplace */
912 0xffff, /* src_mask */
913 0xffff, /* dst_mask */
914 FALSE), /* pcrel_offset */
916 /* The low 16 bits of the displacement from TP to the target. */
917 HOWTO (R_ALPHA_TPRELLO, /* type */
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
921 FALSE, /* pc_relative */
922 0, /* bitpos */
923 complain_overflow_dont, /* complain_on_overflow */
924 0, /* special_function */
925 "TPRELLO", /* name */
926 FALSE, /* partial_inplace */
927 0xffff, /* src_mask */
928 0xffff, /* dst_mask */
929 FALSE), /* pcrel_offset */
931 /* A 16-bit displacement from TP to the target. */
932 HOWTO (R_ALPHA_TPREL16, /* type */
933 0, /* rightshift */
934 1, /* size (0 = byte, 1 = short, 2 = long) */
935 16, /* bitsize */
936 FALSE, /* pc_relative */
937 0, /* bitpos */
938 complain_overflow_signed, /* complain_on_overflow */
939 0, /* special_function */
940 "TPREL16", /* name */
941 FALSE, /* partial_inplace */
942 0xffff, /* src_mask */
943 0xffff, /* dst_mask */
944 FALSE), /* pcrel_offset */
947 /* A relocation function which doesn't do anything. */
949 static bfd_reloc_status_type
950 elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
951 bfd *abfd ATTRIBUTE_UNUSED;
952 arelent *reloc;
953 asymbol *sym ATTRIBUTE_UNUSED;
954 PTR data ATTRIBUTE_UNUSED;
955 asection *sec;
956 bfd *output_bfd;
957 char **error_message ATTRIBUTE_UNUSED;
959 if (output_bfd)
960 reloc->address += sec->output_offset;
961 return bfd_reloc_ok;
964 /* A relocation function used for an unsupported reloc. */
966 static bfd_reloc_status_type
967 elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
968 bfd *abfd ATTRIBUTE_UNUSED;
969 arelent *reloc;
970 asymbol *sym ATTRIBUTE_UNUSED;
971 PTR data ATTRIBUTE_UNUSED;
972 asection *sec;
973 bfd *output_bfd;
974 char **error_message ATTRIBUTE_UNUSED;
976 if (output_bfd)
977 reloc->address += sec->output_offset;
978 return bfd_reloc_notsupported;
981 /* Do the work of the GPDISP relocation. */
983 static bfd_reloc_status_type
984 elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
985 bfd *abfd;
986 bfd_vma gpdisp;
987 bfd_byte *p_ldah;
988 bfd_byte *p_lda;
990 bfd_reloc_status_type ret = bfd_reloc_ok;
991 bfd_vma addend;
992 unsigned long i_ldah, i_lda;
994 i_ldah = bfd_get_32 (abfd, p_ldah);
995 i_lda = bfd_get_32 (abfd, p_lda);
997 /* Complain if the instructions are not correct. */
998 if (((i_ldah >> 26) & 0x3f) != 0x09
999 || ((i_lda >> 26) & 0x3f) != 0x08)
1000 ret = bfd_reloc_dangerous;
1002 /* Extract the user-supplied offset, mirroring the sign extensions
1003 that the instructions perform. */
1004 addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
1005 addend = (addend ^ 0x80008000) - 0x80008000;
1007 gpdisp += addend;
1009 if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
1010 || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
1011 ret = bfd_reloc_overflow;
1013 /* compensate for the sign extension again. */
1014 i_ldah = ((i_ldah & 0xffff0000)
1015 | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
1016 i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
1018 bfd_put_32 (abfd, (bfd_vma) i_ldah, p_ldah);
1019 bfd_put_32 (abfd, (bfd_vma) i_lda, p_lda);
1021 return ret;
1024 /* The special function for the GPDISP reloc. */
1026 static bfd_reloc_status_type
1027 elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
1028 output_bfd, err_msg)
1029 bfd *abfd;
1030 arelent *reloc_entry;
1031 asymbol *sym ATTRIBUTE_UNUSED;
1032 PTR data;
1033 asection *input_section;
1034 bfd *output_bfd;
1035 char **err_msg;
1037 bfd_reloc_status_type ret;
1038 bfd_vma gp, relocation;
1039 bfd_byte *p_ldah, *p_lda;
1041 /* Don't do anything if we're not doing a final link. */
1042 if (output_bfd)
1044 reloc_entry->address += input_section->output_offset;
1045 return bfd_reloc_ok;
1048 if (reloc_entry->address > input_section->_cooked_size ||
1049 reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
1050 return bfd_reloc_outofrange;
1052 /* The gp used in the portion of the output object to which this
1053 input object belongs is cached on the input bfd. */
1054 gp = _bfd_get_gp_value (abfd);
1056 relocation = (input_section->output_section->vma
1057 + input_section->output_offset
1058 + reloc_entry->address);
1060 p_ldah = (bfd_byte *) data + reloc_entry->address;
1061 p_lda = p_ldah + reloc_entry->addend;
1063 ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
1065 /* Complain if the instructions are not correct. */
1066 if (ret == bfd_reloc_dangerous)
1067 *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
1069 return ret;
1072 /* A mapping from BFD reloc types to Alpha ELF reloc types. */
1074 struct elf_reloc_map
1076 bfd_reloc_code_real_type bfd_reloc_val;
1077 int elf_reloc_val;
1080 static const struct elf_reloc_map elf64_alpha_reloc_map[] =
1082 {BFD_RELOC_NONE, R_ALPHA_NONE},
1083 {BFD_RELOC_32, R_ALPHA_REFLONG},
1084 {BFD_RELOC_64, R_ALPHA_REFQUAD},
1085 {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
1086 {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
1087 {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
1088 {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
1089 {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
1090 {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
1091 {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
1092 {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
1093 {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
1094 {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
1095 {BFD_RELOC_ALPHA_GPREL_HI16, R_ALPHA_GPRELHIGH},
1096 {BFD_RELOC_ALPHA_GPREL_LO16, R_ALPHA_GPRELLOW},
1097 {BFD_RELOC_GPREL16, R_ALPHA_GPREL16},
1098 {BFD_RELOC_ALPHA_BRSGP, R_ALPHA_BRSGP},
1099 {BFD_RELOC_ALPHA_TLSGD, R_ALPHA_TLSGD},
1100 {BFD_RELOC_ALPHA_TLSLDM, R_ALPHA_TLSLDM},
1101 {BFD_RELOC_ALPHA_DTPMOD64, R_ALPHA_DTPMOD64},
1102 {BFD_RELOC_ALPHA_GOTDTPREL16, R_ALPHA_GOTDTPREL},
1103 {BFD_RELOC_ALPHA_DTPREL64, R_ALPHA_DTPREL64},
1104 {BFD_RELOC_ALPHA_DTPREL_HI16, R_ALPHA_DTPRELHI},
1105 {BFD_RELOC_ALPHA_DTPREL_LO16, R_ALPHA_DTPRELLO},
1106 {BFD_RELOC_ALPHA_DTPREL16, R_ALPHA_DTPREL16},
1107 {BFD_RELOC_ALPHA_GOTTPREL16, R_ALPHA_GOTTPREL},
1108 {BFD_RELOC_ALPHA_TPREL64, R_ALPHA_TPREL64},
1109 {BFD_RELOC_ALPHA_TPREL_HI16, R_ALPHA_TPRELHI},
1110 {BFD_RELOC_ALPHA_TPREL_LO16, R_ALPHA_TPRELLO},
1111 {BFD_RELOC_ALPHA_TPREL16, R_ALPHA_TPREL16},
1114 /* Given a BFD reloc type, return a HOWTO structure. */
1116 static reloc_howto_type *
1117 elf64_alpha_bfd_reloc_type_lookup (abfd, code)
1118 bfd *abfd ATTRIBUTE_UNUSED;
1119 bfd_reloc_code_real_type code;
1121 const struct elf_reloc_map *i, *e;
1122 i = e = elf64_alpha_reloc_map;
1123 e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
1124 for (; i != e; ++i)
1126 if (i->bfd_reloc_val == code)
1127 return &elf64_alpha_howto_table[i->elf_reloc_val];
1129 return 0;
1132 /* Given an Alpha ELF reloc type, fill in an arelent structure. */
1134 static void
1135 elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
1136 bfd *abfd ATTRIBUTE_UNUSED;
1137 arelent *cache_ptr;
1138 Elf_Internal_Rela *dst;
1140 unsigned r_type;
1142 r_type = ELF64_R_TYPE(dst->r_info);
1143 BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1144 cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1147 /* These two relocations create a two-word entry in the got. */
1148 #define alpha_got_entry_size(r_type) \
1149 (r_type == R_ALPHA_TLSGD || r_type == R_ALPHA_TLSLDM ? 16 : 8)
1151 /* This is PT_TLS segment p_vaddr. */
1152 #define alpha_get_dtprel_base(info) \
1153 (elf_hash_table (info)->tls_sec->vma)
1155 /* Main program TLS (whose template starts at PT_TLS p_vaddr)
1156 is assigned offset round(16, PT_TLS p_align). */
1157 #define alpha_get_tprel_base(info) \
1158 (elf_hash_table (info)->tls_sec->vma \
1159 - align_power ((bfd_vma) 16, \
1160 elf_hash_table (info)->tls_sec->alignment_power))
1162 /* These functions do relaxation for Alpha ELF.
1164 Currently I'm only handling what I can do with existing compiler
1165 and assembler support, which means no instructions are removed,
1166 though some may be nopped. At this time GCC does not emit enough
1167 information to do all of the relaxing that is possible. It will
1168 take some not small amount of work for that to happen.
1170 There are a couple of interesting papers that I once read on this
1171 subject, that I cannot find references to at the moment, that
1172 related to Alpha in particular. They are by David Wall, then of
1173 DEC WRL. */
1175 #define OP_LDA 0x08
1176 #define OP_LDAH 0x09
1177 #define INSN_JSR 0x68004000
1178 #define INSN_JSR_MASK 0xfc00c000
1179 #define OP_LDQ 0x29
1180 #define OP_BR 0x30
1181 #define OP_BSR 0x34
1182 #define INSN_UNOP 0x2ffe0000
1183 #define INSN_ADDQ 0x40000400
1184 #define INSN_RDUNIQ 0x0000009e
1186 struct alpha_relax_info
1188 bfd *abfd;
1189 asection *sec;
1190 bfd_byte *contents;
1191 Elf_Internal_Shdr *symtab_hdr;
1192 Elf_Internal_Rela *relocs, *relend;
1193 struct bfd_link_info *link_info;
1194 bfd_vma gp;
1195 bfd *gotobj;
1196 asection *tsec;
1197 struct alpha_elf_link_hash_entry *h;
1198 struct alpha_elf_got_entry **first_gotent;
1199 struct alpha_elf_got_entry *gotent;
1200 bfd_boolean changed_contents;
1201 bfd_boolean changed_relocs;
1202 unsigned char other;
1205 static bfd_boolean elf64_alpha_relax_with_lituse
1206 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1207 Elf_Internal_Rela *irel));
1208 static bfd_vma elf64_alpha_relax_opt_call
1209 PARAMS((struct alpha_relax_info *info, bfd_vma symval));
1210 static bfd_boolean elf64_alpha_relax_got_load
1211 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1212 Elf_Internal_Rela *irel, unsigned long));
1213 static bfd_boolean elf64_alpha_relax_gprelhilo
1214 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1215 Elf_Internal_Rela *irel, bfd_boolean));
1216 static bfd_boolean elf64_alpha_relax_tls_get_addr
1217 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1218 Elf_Internal_Rela *irel, bfd_boolean));
1219 static bfd_boolean elf64_alpha_relax_section
1220 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
1221 bfd_boolean *again));
1223 static Elf_Internal_Rela *
1224 elf64_alpha_find_reloc_at_ofs (rel, relend, offset, type)
1225 Elf_Internal_Rela *rel, *relend;
1226 bfd_vma offset;
1227 int type;
1229 while (rel < relend)
1231 if (rel->r_offset == offset
1232 && ELF64_R_TYPE (rel->r_info) == (unsigned int) type)
1233 return rel;
1234 ++rel;
1236 return NULL;
1239 static bfd_boolean
1240 elf64_alpha_relax_with_lituse (info, symval, irel)
1241 struct alpha_relax_info *info;
1242 bfd_vma symval;
1243 Elf_Internal_Rela *irel;
1245 Elf_Internal_Rela *urel, *irelend = info->relend;
1246 int flags, count, i;
1247 bfd_signed_vma disp;
1248 bfd_boolean fits16;
1249 bfd_boolean fits32;
1250 bfd_boolean lit_reused = FALSE;
1251 bfd_boolean all_optimized = TRUE;
1252 unsigned int lit_insn;
1254 lit_insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1255 if (lit_insn >> 26 != OP_LDQ)
1257 ((*_bfd_error_handler)
1258 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1259 bfd_archive_filename (info->abfd), info->sec->name,
1260 (unsigned long) irel->r_offset));
1261 return TRUE;
1264 /* Can't relax dynamic symbols. */
1265 if (alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
1266 return TRUE;
1268 /* Summarize how this particular LITERAL is used. */
1269 for (urel = irel+1, flags = count = 0; urel < irelend; ++urel, ++count)
1271 if (ELF64_R_TYPE (urel->r_info) != R_ALPHA_LITUSE)
1272 break;
1273 if (urel->r_addend <= 3)
1274 flags |= 1 << urel->r_addend;
1277 /* A little preparation for the loop... */
1278 disp = symval - info->gp;
1280 for (urel = irel+1, i = 0; i < count; ++i, ++urel)
1282 unsigned int insn;
1283 int insn_disp;
1284 bfd_signed_vma xdisp;
1286 insn = bfd_get_32 (info->abfd, info->contents + urel->r_offset);
1288 switch (urel->r_addend)
1290 case LITUSE_ALPHA_ADDR:
1291 default:
1292 /* This type is really just a placeholder to note that all
1293 uses cannot be optimized, but to still allow some. */
1294 all_optimized = FALSE;
1295 break;
1297 case LITUSE_ALPHA_BASE:
1298 /* We can always optimize 16-bit displacements. */
1300 /* Extract the displacement from the instruction, sign-extending
1301 it if necessary, then test whether it is within 16 or 32 bits
1302 displacement from GP. */
1303 insn_disp = insn & 0x0000ffff;
1304 if (insn_disp & 0x8000)
1305 insn_disp |= ~0xffff; /* Negative: sign-extend. */
1307 xdisp = disp + insn_disp;
1308 fits16 = (xdisp >= - (bfd_signed_vma) 0x8000 && xdisp < 0x8000);
1309 fits32 = (xdisp >= - (bfd_signed_vma) 0x80000000
1310 && xdisp < 0x7fff8000);
1312 if (fits16)
1314 /* Take the op code and dest from this insn, take the base
1315 register from the literal insn. Leave the offset alone. */
1316 insn = (insn & 0xffe0ffff) | (lit_insn & 0x001f0000);
1317 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1318 R_ALPHA_GPREL16);
1319 urel->r_addend = irel->r_addend;
1320 info->changed_relocs = TRUE;
1322 bfd_put_32 (info->abfd, (bfd_vma) insn,
1323 info->contents + urel->r_offset);
1324 info->changed_contents = TRUE;
1327 /* If all mem+byte, we can optimize 32-bit mem displacements. */
1328 else if (fits32 && !(flags & ~6))
1330 /* FIXME: sanity check that lit insn Ra is mem insn Rb. */
1332 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1333 R_ALPHA_GPRELHIGH);
1334 lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
1335 bfd_put_32 (info->abfd, (bfd_vma) lit_insn,
1336 info->contents + irel->r_offset);
1337 lit_reused = TRUE;
1338 info->changed_contents = TRUE;
1340 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1341 R_ALPHA_GPRELLOW);
1342 urel->r_addend = irel->r_addend;
1343 info->changed_relocs = TRUE;
1345 else
1346 all_optimized = FALSE;
1347 break;
1349 case LITUSE_ALPHA_BYTOFF:
1350 /* We can always optimize byte instructions. */
1352 /* FIXME: sanity check the insn for byte op. Check that the
1353 literal dest reg is indeed Rb in the byte insn. */
1355 insn &= ~ (unsigned) 0x001ff000;
1356 insn |= ((symval & 7) << 13) | 0x1000;
1358 urel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1359 urel->r_addend = 0;
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;
1365 break;
1367 case LITUSE_ALPHA_JSR:
1368 case LITUSE_ALPHA_TLSGD:
1369 case LITUSE_ALPHA_TLSLDM:
1371 bfd_vma optdest, org;
1372 bfd_signed_vma odisp;
1374 /* If not zero, place to jump without needing pv. */
1375 optdest = elf64_alpha_relax_opt_call (info, symval);
1376 org = (info->sec->output_section->vma
1377 + info->sec->output_offset
1378 + urel->r_offset + 4);
1379 odisp = (optdest ? optdest : symval) - org;
1381 if (odisp >= -0x400000 && odisp < 0x400000)
1383 Elf_Internal_Rela *xrel;
1385 /* Preserve branch prediction call stack when possible. */
1386 if ((insn & INSN_JSR_MASK) == INSN_JSR)
1387 insn = (OP_BSR << 26) | (insn & 0x03e00000);
1388 else
1389 insn = (OP_BR << 26) | (insn & 0x03e00000);
1391 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1392 R_ALPHA_BRADDR);
1393 urel->r_addend = irel->r_addend;
1395 if (optdest)
1396 urel->r_addend += optdest - symval;
1397 else
1398 all_optimized = FALSE;
1400 bfd_put_32 (info->abfd, (bfd_vma) insn,
1401 info->contents + urel->r_offset);
1403 /* Kill any HINT reloc that might exist for this insn. */
1404 xrel = (elf64_alpha_find_reloc_at_ofs
1405 (info->relocs, info->relend, urel->r_offset,
1406 R_ALPHA_HINT));
1407 if (xrel)
1408 xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1410 info->changed_contents = TRUE;
1411 info->changed_relocs = TRUE;
1413 else
1414 all_optimized = FALSE;
1416 /* Even if the target is not in range for a direct branch,
1417 if we share a GP, we can eliminate the gp reload. */
1418 if (optdest)
1420 Elf_Internal_Rela *gpdisp
1421 = (elf64_alpha_find_reloc_at_ofs
1422 (info->relocs, irelend, urel->r_offset + 4,
1423 R_ALPHA_GPDISP));
1424 if (gpdisp)
1426 bfd_byte *p_ldah = info->contents + gpdisp->r_offset;
1427 bfd_byte *p_lda = p_ldah + gpdisp->r_addend;
1428 unsigned int ldah = bfd_get_32 (info->abfd, p_ldah);
1429 unsigned int lda = bfd_get_32 (info->abfd, p_lda);
1431 /* Verify that the instruction is "ldah $29,0($26)".
1432 Consider a function that ends in a noreturn call,
1433 and that the next function begins with an ldgp,
1434 and that by accident there is no padding between.
1435 In that case the insn would use $27 as the base. */
1436 if (ldah == 0x27ba0000 && lda == 0x23bd0000)
1438 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, p_ldah);
1439 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, p_lda);
1441 gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1442 info->changed_contents = TRUE;
1443 info->changed_relocs = TRUE;
1448 break;
1452 /* If all cases were optimized, we can reduce the use count on this
1453 got entry by one, possibly eliminating it. */
1454 if (all_optimized)
1456 if (--info->gotent->use_count == 0)
1458 int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
1459 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1460 if (!info->h)
1461 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
1464 /* If the literal instruction is no longer needed (it may have been
1465 reused. We can eliminate it. */
1466 /* ??? For now, I don't want to deal with compacting the section,
1467 so just nop it out. */
1468 if (!lit_reused)
1470 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1471 info->changed_relocs = TRUE;
1473 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP,
1474 info->contents + irel->r_offset);
1475 info->changed_contents = TRUE;
1479 return TRUE;
1482 static bfd_vma
1483 elf64_alpha_relax_opt_call (info, symval)
1484 struct alpha_relax_info *info;
1485 bfd_vma symval;
1487 /* If the function has the same gp, and we can identify that the
1488 function does not use its function pointer, we can eliminate the
1489 address load. */
1491 /* If the symbol is marked NOPV, we are being told the function never
1492 needs its procedure value. */
1493 if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_NOPV)
1494 return symval;
1496 /* If the symbol is marked STD_GP, we are being told the function does
1497 a normal ldgp in the first two words. */
1498 else if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_STD_GPLOAD)
1501 /* Otherwise, we may be able to identify a GP load in the first two
1502 words, which we can then skip. */
1503 else
1505 Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
1506 bfd_vma ofs;
1508 /* Load the relocations from the section that the target symbol is in. */
1509 if (info->sec == info->tsec)
1511 tsec_relocs = info->relocs;
1512 tsec_relend = info->relend;
1513 tsec_free = NULL;
1515 else
1517 tsec_relocs = (_bfd_elf_link_read_relocs
1518 (info->abfd, info->tsec, (PTR) NULL,
1519 (Elf_Internal_Rela *) NULL,
1520 info->link_info->keep_memory));
1521 if (tsec_relocs == NULL)
1522 return 0;
1523 tsec_relend = tsec_relocs + info->tsec->reloc_count;
1524 tsec_free = (info->link_info->keep_memory ? NULL : tsec_relocs);
1527 /* Recover the symbol's offset within the section. */
1528 ofs = (symval - info->tsec->output_section->vma
1529 - info->tsec->output_offset);
1531 /* Look for a GPDISP reloc. */
1532 gpdisp = (elf64_alpha_find_reloc_at_ofs
1533 (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
1535 if (!gpdisp || gpdisp->r_addend != 4)
1537 if (tsec_free)
1538 free (tsec_free);
1539 return 0;
1541 if (tsec_free)
1542 free (tsec_free);
1545 /* We've now determined that we can skip an initial gp load. Verify
1546 that the call and the target use the same gp. */
1547 if (info->link_info->hash->creator != info->tsec->owner->xvec
1548 || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
1549 return 0;
1551 return symval + 8;
1554 static bfd_boolean
1555 elf64_alpha_relax_got_load (info, symval, irel, r_type)
1556 struct alpha_relax_info *info;
1557 bfd_vma symval;
1558 Elf_Internal_Rela *irel;
1559 unsigned long r_type;
1561 unsigned int insn;
1562 bfd_signed_vma disp;
1564 /* Get the instruction. */
1565 insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1567 if (insn >> 26 != OP_LDQ)
1569 reloc_howto_type *howto = elf64_alpha_howto_table + r_type;
1570 ((*_bfd_error_handler)
1571 ("%s: %s+0x%lx: warning: %s relocation against unexpected insn",
1572 bfd_archive_filename (info->abfd), info->sec->name,
1573 (unsigned long) irel->r_offset, howto->name));
1574 return TRUE;
1577 /* Can't relax dynamic symbols. */
1578 if (alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
1579 return TRUE;
1581 /* Can't use local-exec relocations in shared libraries. */
1582 if (r_type == R_ALPHA_GOTTPREL && info->link_info->shared)
1583 return TRUE;
1585 if (r_type == R_ALPHA_LITERAL)
1586 disp = symval - info->gp;
1587 else
1589 bfd_vma dtp_base, tp_base;
1591 BFD_ASSERT (elf_hash_table (info->link_info)->tls_sec != NULL);
1592 dtp_base = alpha_get_dtprel_base (info->link_info);
1593 tp_base = alpha_get_tprel_base (info->link_info);
1594 disp = symval - (r_type == R_ALPHA_GOTDTPREL ? dtp_base : tp_base);
1597 if (disp < -0x8000 || disp >= 0x8000)
1598 return TRUE;
1600 /* Exchange LDQ for LDA. In the case of the TLS relocs, we're loading
1601 a constant, so force the base register to be $31. */
1602 if (r_type == R_ALPHA_LITERAL)
1603 insn = (OP_LDA << 26) | (insn & 0x03ff0000);
1604 else
1605 insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
1606 bfd_put_32 (info->abfd, (bfd_vma) insn, info->contents + irel->r_offset);
1607 info->changed_contents = TRUE;
1609 /* Reduce the use count on this got entry by one, possibly
1610 eliminating it. */
1611 if (--info->gotent->use_count == 0)
1613 int sz = alpha_got_entry_size (r_type);
1614 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1615 if (!info->h)
1616 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
1619 /* Smash the existing GOT relocation for its 16-bit immediate pair. */
1620 switch (r_type)
1622 case R_ALPHA_LITERAL:
1623 r_type = R_ALPHA_GPREL16;
1624 break;
1625 case R_ALPHA_GOTDTPREL:
1626 r_type = R_ALPHA_DTPREL16;
1627 break;
1628 case R_ALPHA_GOTTPREL:
1629 r_type = R_ALPHA_TPREL16;
1630 break;
1631 default:
1632 BFD_ASSERT (0);
1633 return FALSE;
1636 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), r_type);
1637 info->changed_relocs = TRUE;
1639 /* ??? Search forward through this basic block looking for insns
1640 that use the target register. Stop after an insn modifying the
1641 register is seen, or after a branch or call.
1643 Any such memory load insn may be substituted by a load directly
1644 off the GP. This allows the memory load insn to be issued before
1645 the calculated GP register would otherwise be ready.
1647 Any such jsr insn can be replaced by a bsr if it is in range.
1649 This would mean that we'd have to _add_ relocations, the pain of
1650 which gives one pause. */
1652 return TRUE;
1655 static bfd_boolean
1656 elf64_alpha_relax_gprelhilo (info, symval, irel, hi)
1657 struct alpha_relax_info *info;
1658 bfd_vma symval;
1659 Elf_Internal_Rela *irel;
1660 bfd_boolean hi;
1662 unsigned int insn;
1663 bfd_signed_vma disp;
1664 bfd_byte *pos = info->contents + irel->r_offset;
1666 /* ??? This assumes that the compiler doesn't render
1668 array[i]
1670 ldah t, array(gp) !gprelhigh
1671 s8addl i, t, t
1672 ldq r, array(t) !gprellow
1674 which would indeed be the most efficient way to implement this. */
1676 return TRUE;
1678 disp = symval - info->gp;
1679 if (disp < -0x8000 || disp >= 0x8000)
1680 return TRUE;
1682 if (hi)
1684 /* Nop out the high instruction. */
1686 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos);
1687 info->changed_contents = TRUE;
1689 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1690 irel->r_addend = 0;
1691 info->changed_relocs = TRUE;
1693 else
1695 /* Adjust the low instruction to reference GP directly. */
1697 insn = bfd_get_32 (info->abfd, pos);
1698 insn = (insn & 0xffe00000) | (29 << 16);
1699 bfd_put_32 (info->abfd, (bfd_vma) insn, pos);
1700 info->changed_contents = TRUE;
1702 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1703 R_ALPHA_GPREL16);
1704 info->changed_relocs = TRUE;
1707 return TRUE;
1710 static bfd_boolean
1711 elf64_alpha_relax_tls_get_addr (info, symval, irel, is_gd)
1712 struct alpha_relax_info *info;
1713 bfd_vma symval;
1714 Elf_Internal_Rela *irel;
1715 bfd_boolean is_gd;
1717 bfd_byte *pos[5];
1718 unsigned int insn;
1719 Elf_Internal_Rela *gpdisp, *hint;
1720 bfd_boolean dynamic, use_gottprel, pos1_unusable;
1721 unsigned long new_symndx;
1723 dynamic = alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info);
1725 /* If a TLS symbol is accessed using IE at least once, there is no point
1726 to use dynamic model for it. */
1727 if (is_gd && info->h && (info->h->flags & ALPHA_ELF_LINK_HASH_TLS_IE))
1730 /* If the symbol is local, and we've already committed to DF_STATIC_TLS,
1731 then we might as well relax to IE. */
1732 else if (info->link_info->shared && !dynamic
1733 && (info->link_info->flags & DF_STATIC_TLS))
1736 /* Otherwise we must be building an executable to do anything. */
1737 else if (info->link_info->shared)
1738 return TRUE;
1740 /* The TLSGD/TLSLDM relocation must be followed by a LITERAL and
1741 the matching LITUSE_TLS relocations. */
1742 if (irel + 2 >= info->relend)
1743 return TRUE;
1744 if (ELF64_R_TYPE (irel[1].r_info) != R_ALPHA_LITERAL
1745 || ELF64_R_TYPE (irel[2].r_info) != R_ALPHA_LITUSE
1746 || irel[2].r_addend != (is_gd ? LITUSE_ALPHA_TLSGD : LITUSE_ALPHA_TLSLDM))
1747 return TRUE;
1749 /* There must be a GPDISP relocation positioned immediately after the
1750 LITUSE relocation. */
1751 gpdisp = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
1752 irel[2].r_offset + 4, R_ALPHA_GPDISP);
1753 if (!gpdisp)
1754 return TRUE;
1756 pos[0] = info->contents + irel[0].r_offset;
1757 pos[1] = info->contents + irel[1].r_offset;
1758 pos[2] = info->contents + irel[2].r_offset;
1759 pos[3] = info->contents + gpdisp->r_offset;
1760 pos[4] = pos[3] + gpdisp->r_addend;
1761 pos1_unusable = FALSE;
1763 /* Generally, the positions are not allowed to be out of order, lest the
1764 modified insn sequence have different register lifetimes. We can make
1765 an exception when pos 1 is adjacent to pos 0. */
1766 if (pos[1] + 4 == pos[0])
1768 bfd_byte *tmp = pos[0];
1769 pos[0] = pos[1];
1770 pos[1] = tmp;
1772 else if (pos[1] < pos[0])
1773 pos1_unusable = TRUE;
1774 if (pos[1] >= pos[2] || pos[2] >= pos[3])
1775 return TRUE;
1777 /* Reduce the use count on the LITERAL relocation. Do this before we
1778 smash the symndx when we adjust the relocations below. */
1780 struct alpha_elf_got_entry *lit_gotent;
1781 struct alpha_elf_link_hash_entry *lit_h;
1782 unsigned long indx;
1784 BFD_ASSERT (ELF64_R_SYM (irel[1].r_info) >= info->symtab_hdr->sh_info);
1785 indx = ELF64_R_SYM (irel[1].r_info) - info->symtab_hdr->sh_info;
1786 lit_h = alpha_elf_sym_hashes (info->abfd)[indx];
1788 while (lit_h->root.root.type == bfd_link_hash_indirect
1789 || lit_h->root.root.type == bfd_link_hash_warning)
1790 lit_h = (struct alpha_elf_link_hash_entry *) lit_h->root.root.u.i.link;
1792 for (lit_gotent = lit_h->got_entries; lit_gotent ;
1793 lit_gotent = lit_gotent->next)
1794 if (lit_gotent->gotobj == info->gotobj
1795 && lit_gotent->reloc_type == R_ALPHA_LITERAL
1796 && lit_gotent->addend == irel[1].r_addend)
1797 break;
1798 BFD_ASSERT (lit_gotent);
1800 if (--lit_gotent->use_count == 0)
1802 int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
1803 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1807 /* Change
1809 lda $16,x($gp) !tlsgd!1
1810 ldq $27,__tls_get_addr($gp) !literal!1
1811 jsr $26,($27)__tls_get_addr !lituse_tlsgd!1
1812 ldah $29,0($26) !gpdisp!2
1813 lda $29,0($29) !gpdisp!2
1815 ldq $16,x($gp) !gottprel
1816 unop
1817 call_pal rduniq
1818 addq $16,$0,$0
1819 unop
1820 or the first pair to
1821 lda $16,x($gp) !tprel
1822 unop
1824 ldah $16,x($gp) !tprelhi
1825 lda $16,x($16) !tprello
1827 as appropriate. */
1829 use_gottprel = FALSE;
1830 new_symndx = is_gd ? ELF64_R_SYM (irel->r_info) : 0;
1831 switch (!dynamic && !info->link_info->shared)
1833 case 1:
1835 bfd_vma tp_base;
1836 bfd_signed_vma disp;
1838 BFD_ASSERT (elf_hash_table (info->link_info)->tls_sec != NULL);
1839 tp_base = alpha_get_tprel_base (info->link_info);
1840 disp = symval - tp_base;
1842 if (disp >= -0x8000 && disp < 0x8000)
1844 insn = (OP_LDA << 26) | (16 << 21) | (31 << 16);
1845 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
1846 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
1848 irel[0].r_offset = pos[0] - info->contents;
1849 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPREL16);
1850 irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1851 break;
1853 else if (disp >= -(bfd_signed_vma) 0x80000000
1854 && disp < (bfd_signed_vma) 0x7fff8000
1855 && !pos1_unusable)
1857 insn = (OP_LDAH << 26) | (16 << 21) | (31 << 16);
1858 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
1859 insn = (OP_LDA << 26) | (16 << 21) | (16 << 16);
1860 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[1]);
1862 irel[0].r_offset = pos[0] - info->contents;
1863 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELHI);
1864 irel[1].r_offset = pos[1] - info->contents;
1865 irel[1].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELLO);
1866 break;
1869 /* FALLTHRU */
1871 default:
1872 use_gottprel = TRUE;
1874 insn = (OP_LDQ << 26) | (16 << 21) | (29 << 16);
1875 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
1876 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
1878 irel[0].r_offset = pos[0] - info->contents;
1879 irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_GOTTPREL);
1880 irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1881 break;
1884 bfd_put_32 (info->abfd, (bfd_vma) INSN_RDUNIQ, pos[2]);
1886 insn = INSN_ADDQ | (16 << 21) | (0 << 16) | (0 << 0);
1887 bfd_put_32 (info->abfd, (bfd_vma) insn, pos[3]);
1889 bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[4]);
1891 irel[2].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1892 gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1894 hint = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
1895 irel[2].r_offset, R_ALPHA_HINT);
1896 if (hint)
1897 hint->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1899 info->changed_contents = TRUE;
1900 info->changed_relocs = TRUE;
1902 /* Reduce the use count on the TLSGD/TLSLDM relocation. */
1903 if (--info->gotent->use_count == 0)
1905 int sz = alpha_got_entry_size (info->gotent->reloc_type);
1906 alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
1907 if (!info->h)
1908 alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
1911 /* If we've switched to a GOTTPREL relocation, increment the reference
1912 count on that got entry. */
1913 if (use_gottprel)
1915 struct alpha_elf_got_entry *tprel_gotent;
1917 for (tprel_gotent = *info->first_gotent; tprel_gotent ;
1918 tprel_gotent = tprel_gotent->next)
1919 if (tprel_gotent->gotobj == info->gotobj
1920 && tprel_gotent->reloc_type == R_ALPHA_GOTTPREL
1921 && tprel_gotent->addend == irel->r_addend)
1922 break;
1923 if (tprel_gotent)
1924 tprel_gotent->use_count++;
1925 else
1927 if (info->gotent->use_count == 0)
1928 tprel_gotent = info->gotent;
1929 else
1931 tprel_gotent = (struct alpha_elf_got_entry *)
1932 bfd_alloc (info->abfd, sizeof (struct alpha_elf_got_entry));
1933 if (!tprel_gotent)
1934 return FALSE;
1936 tprel_gotent->next = *info->first_gotent;
1937 *info->first_gotent = tprel_gotent;
1939 tprel_gotent->gotobj = info->gotobj;
1940 tprel_gotent->addend = irel->r_addend;
1941 tprel_gotent->got_offset = -1;
1942 tprel_gotent->reloc_done = 0;
1943 tprel_gotent->reloc_xlated = 0;
1946 tprel_gotent->use_count = 1;
1947 tprel_gotent->reloc_type = R_ALPHA_GOTTPREL;
1951 return TRUE;
1954 static bfd_boolean
1955 elf64_alpha_relax_section (abfd, sec, link_info, again)
1956 bfd *abfd;
1957 asection *sec;
1958 struct bfd_link_info *link_info;
1959 bfd_boolean *again;
1961 Elf_Internal_Shdr *symtab_hdr;
1962 Elf_Internal_Rela *internal_relocs;
1963 Elf_Internal_Rela *irel, *irelend;
1964 Elf_Internal_Sym *isymbuf = NULL;
1965 struct alpha_elf_got_entry **local_got_entries;
1966 struct alpha_relax_info info;
1968 /* We are not currently changing any sizes, so only one pass. */
1969 *again = FALSE;
1971 if (link_info->relocatable
1972 || (sec->flags & SEC_RELOC) == 0
1973 || sec->reloc_count == 0)
1974 return TRUE;
1976 /* If this is the first time we have been called for this section,
1977 initialize the cooked size. */
1978 if (sec->_cooked_size == 0)
1979 sec->_cooked_size = sec->_raw_size;
1981 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1982 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1984 /* Load the relocations for this section. */
1985 internal_relocs = (_bfd_elf_link_read_relocs
1986 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1987 link_info->keep_memory));
1988 if (internal_relocs == NULL)
1989 return FALSE;
1991 memset(&info, 0, sizeof (info));
1992 info.abfd = abfd;
1993 info.sec = sec;
1994 info.link_info = link_info;
1995 info.symtab_hdr = symtab_hdr;
1996 info.relocs = internal_relocs;
1997 info.relend = irelend = internal_relocs + sec->reloc_count;
1999 /* Find the GP for this object. Do not store the result back via
2000 _bfd_set_gp_value, since this could change again before final. */
2001 info.gotobj = alpha_elf_tdata (abfd)->gotobj;
2002 if (info.gotobj)
2004 asection *sgot = alpha_elf_tdata (info.gotobj)->got;
2005 info.gp = (sgot->output_section->vma
2006 + sgot->output_offset
2007 + 0x8000);
2010 /* Get the section contents. */
2011 if (elf_section_data (sec)->this_hdr.contents != NULL)
2012 info.contents = elf_section_data (sec)->this_hdr.contents;
2013 else
2015 info.contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
2016 if (info.contents == NULL)
2017 goto error_return;
2019 if (! bfd_get_section_contents (abfd, sec, info.contents,
2020 (file_ptr) 0, sec->_raw_size))
2021 goto error_return;
2024 for (irel = internal_relocs; irel < irelend; irel++)
2026 bfd_vma symval;
2027 struct alpha_elf_got_entry *gotent;
2028 unsigned long r_type = ELF64_R_TYPE (irel->r_info);
2029 unsigned long r_symndx = ELF64_R_SYM (irel->r_info);
2031 /* Early exit for unhandled or unrelaxable relocations. */
2032 switch (r_type)
2034 case R_ALPHA_LITERAL:
2035 case R_ALPHA_GPRELHIGH:
2036 case R_ALPHA_GPRELLOW:
2037 case R_ALPHA_GOTDTPREL:
2038 case R_ALPHA_GOTTPREL:
2039 case R_ALPHA_TLSGD:
2040 break;
2042 case R_ALPHA_TLSLDM:
2043 /* The symbol for a TLSLDM reloc is ignored. Collapse the
2044 reloc to the 0 symbol so that they all match. */
2045 r_symndx = 0;
2046 break;
2048 default:
2049 continue;
2052 /* Get the value of the symbol referred to by the reloc. */
2053 if (r_symndx < symtab_hdr->sh_info)
2055 /* A local symbol. */
2056 Elf_Internal_Sym *isym;
2058 /* Read this BFD's local symbols. */
2059 if (isymbuf == NULL)
2061 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2062 if (isymbuf == NULL)
2063 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2064 symtab_hdr->sh_info, 0,
2065 NULL, NULL, NULL);
2066 if (isymbuf == NULL)
2067 goto error_return;
2070 isym = isymbuf + r_symndx;
2072 /* Given the symbol for a TLSLDM reloc is ignored, this also
2073 means forcing the symbol value to the tp base. */
2074 if (r_type == R_ALPHA_TLSLDM)
2076 info.tsec = bfd_abs_section_ptr;
2077 symval = alpha_get_tprel_base (info.link_info);
2079 else
2081 symval = isym->st_value;
2082 if (isym->st_shndx == SHN_UNDEF)
2083 continue;
2084 else if (isym->st_shndx == SHN_ABS)
2085 info.tsec = bfd_abs_section_ptr;
2086 else if (isym->st_shndx == SHN_COMMON)
2087 info.tsec = bfd_com_section_ptr;
2088 else
2089 info.tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2092 info.h = NULL;
2093 info.other = isym->st_other;
2094 if (local_got_entries)
2095 info.first_gotent = &local_got_entries[r_symndx];
2096 else
2098 info.first_gotent = &info.gotent;
2099 info.gotent = NULL;
2102 else
2104 unsigned long indx;
2105 struct alpha_elf_link_hash_entry *h;
2107 indx = r_symndx - symtab_hdr->sh_info;
2108 h = alpha_elf_sym_hashes (abfd)[indx];
2109 BFD_ASSERT (h != NULL);
2111 while (h->root.root.type == bfd_link_hash_indirect
2112 || h->root.root.type == bfd_link_hash_warning)
2113 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2115 /* If the symbol is undefined, we can't do anything with it. */
2116 if (h->root.root.type == bfd_link_hash_undefweak
2117 || h->root.root.type == bfd_link_hash_undefined)
2118 continue;
2120 /* If the symbol isn't defined in the current module, again
2121 we can't do anything. */
2122 if (!(h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2124 /* Except for TLSGD relocs, which can sometimes be
2125 relaxed to GOTTPREL relocs. */
2126 if (r_type != R_ALPHA_TLSGD)
2127 continue;
2128 info.tsec = bfd_abs_section_ptr;
2129 symval = 0;
2131 else
2133 info.tsec = h->root.root.u.def.section;
2134 symval = h->root.root.u.def.value;
2137 info.h = h;
2138 info.other = h->root.other;
2139 info.first_gotent = &h->got_entries;
2142 /* Search for the got entry to be used by this relocation. */
2143 for (gotent = *info.first_gotent; gotent ; gotent = gotent->next)
2144 if (gotent->gotobj == info.gotobj
2145 && gotent->reloc_type == r_type
2146 && gotent->addend == irel->r_addend)
2147 break;
2148 info.gotent = gotent;
2150 symval += info.tsec->output_section->vma + info.tsec->output_offset;
2151 symval += irel->r_addend;
2153 switch (r_type)
2155 case R_ALPHA_LITERAL:
2156 BFD_ASSERT(info.gotent != NULL);
2158 /* If there exist LITUSE relocations immediately following, this
2159 opens up all sorts of interesting optimizations, because we
2160 now know every location that this address load is used. */
2161 if (irel+1 < irelend
2162 && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
2164 if (!elf64_alpha_relax_with_lituse (&info, symval, irel))
2165 goto error_return;
2167 else
2169 if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
2170 goto error_return;
2172 break;
2174 case R_ALPHA_GPRELHIGH:
2175 case R_ALPHA_GPRELLOW:
2176 if (!elf64_alpha_relax_gprelhilo (&info, symval, irel,
2177 r_type == R_ALPHA_GPRELHIGH))
2178 goto error_return;
2179 break;
2181 case R_ALPHA_GOTDTPREL:
2182 case R_ALPHA_GOTTPREL:
2183 BFD_ASSERT(info.gotent != NULL);
2184 if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
2185 goto error_return;
2186 break;
2188 case R_ALPHA_TLSGD:
2189 case R_ALPHA_TLSLDM:
2190 BFD_ASSERT(info.gotent != NULL);
2191 if (!elf64_alpha_relax_tls_get_addr (&info, symval, irel,
2192 r_type == R_ALPHA_TLSGD))
2193 goto error_return;
2194 break;
2198 if (!elf64_alpha_size_plt_section (link_info))
2199 return FALSE;
2200 if (!elf64_alpha_size_got_sections (link_info))
2201 return FALSE;
2202 if (!elf64_alpha_size_rela_got_section (link_info))
2203 return FALSE;
2205 if (isymbuf != NULL
2206 && symtab_hdr->contents != (unsigned char *) isymbuf)
2208 if (!link_info->keep_memory)
2209 free (isymbuf);
2210 else
2212 /* Cache the symbols for elf_link_input_bfd. */
2213 symtab_hdr->contents = (unsigned char *) isymbuf;
2217 if (info.contents != NULL
2218 && elf_section_data (sec)->this_hdr.contents != info.contents)
2220 if (!info.changed_contents && !link_info->keep_memory)
2221 free (info.contents);
2222 else
2224 /* Cache the section contents for elf_link_input_bfd. */
2225 elf_section_data (sec)->this_hdr.contents = info.contents;
2229 if (elf_section_data (sec)->relocs != internal_relocs)
2231 if (!info.changed_relocs)
2232 free (internal_relocs);
2233 else
2234 elf_section_data (sec)->relocs = internal_relocs;
2237 *again = info.changed_contents || info.changed_relocs;
2239 return TRUE;
2241 error_return:
2242 if (isymbuf != NULL
2243 && symtab_hdr->contents != (unsigned char *) isymbuf)
2244 free (isymbuf);
2245 if (info.contents != NULL
2246 && elf_section_data (sec)->this_hdr.contents != info.contents)
2247 free (info.contents);
2248 if (internal_relocs != NULL
2249 && elf_section_data (sec)->relocs != internal_relocs)
2250 free (internal_relocs);
2251 return FALSE;
2254 /* PLT/GOT Stuff */
2255 #define PLT_HEADER_SIZE 32
2256 #define PLT_HEADER_WORD1 (bfd_vma) 0xc3600000 /* br $27,.+4 */
2257 #define PLT_HEADER_WORD2 (bfd_vma) 0xa77b000c /* ldq $27,12($27) */
2258 #define PLT_HEADER_WORD3 (bfd_vma) 0x47ff041f /* nop */
2259 #define PLT_HEADER_WORD4 (bfd_vma) 0x6b7b0000 /* jmp $27,($27) */
2261 #define PLT_ENTRY_SIZE 12
2262 #define PLT_ENTRY_WORD1 0xc3800000 /* br $28, plt0 */
2263 #define PLT_ENTRY_WORD2 0
2264 #define PLT_ENTRY_WORD3 0
2266 #define MAX_GOT_SIZE (64*1024)
2268 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
2270 /* Handle an Alpha specific section when reading an object file. This
2271 is called when elfcode.h finds a section with an unknown type.
2272 FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
2273 how to. */
2275 static bfd_boolean
2276 elf64_alpha_section_from_shdr (abfd, hdr, name)
2277 bfd *abfd;
2278 Elf_Internal_Shdr *hdr;
2279 const char *name;
2281 asection *newsect;
2283 /* There ought to be a place to keep ELF backend specific flags, but
2284 at the moment there isn't one. We just keep track of the
2285 sections by their name, instead. Fortunately, the ABI gives
2286 suggested names for all the MIPS specific sections, so we will
2287 probably get away with this. */
2288 switch (hdr->sh_type)
2290 case SHT_ALPHA_DEBUG:
2291 if (strcmp (name, ".mdebug") != 0)
2292 return FALSE;
2293 break;
2294 default:
2295 return FALSE;
2298 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2299 return FALSE;
2300 newsect = hdr->bfd_section;
2302 if (hdr->sh_type == SHT_ALPHA_DEBUG)
2304 if (! bfd_set_section_flags (abfd, newsect,
2305 (bfd_get_section_flags (abfd, newsect)
2306 | SEC_DEBUGGING)))
2307 return FALSE;
2310 return TRUE;
2313 /* Convert Alpha specific section flags to bfd internal section flags. */
2315 static bfd_boolean
2316 elf64_alpha_section_flags (flags, hdr)
2317 flagword *flags;
2318 Elf_Internal_Shdr *hdr;
2320 if (hdr->sh_flags & SHF_ALPHA_GPREL)
2321 *flags |= SEC_SMALL_DATA;
2323 return TRUE;
2326 /* Set the correct type for an Alpha ELF section. We do this by the
2327 section name, which is a hack, but ought to work. */
2329 static bfd_boolean
2330 elf64_alpha_fake_sections (abfd, hdr, sec)
2331 bfd *abfd;
2332 Elf_Internal_Shdr *hdr;
2333 asection *sec;
2335 register const char *name;
2337 name = bfd_get_section_name (abfd, sec);
2339 if (strcmp (name, ".mdebug") == 0)
2341 hdr->sh_type = SHT_ALPHA_DEBUG;
2342 /* In a shared object on Irix 5.3, the .mdebug section has an
2343 entsize of 0. FIXME: Does this matter? */
2344 if ((abfd->flags & DYNAMIC) != 0 )
2345 hdr->sh_entsize = 0;
2346 else
2347 hdr->sh_entsize = 1;
2349 else if ((sec->flags & SEC_SMALL_DATA)
2350 || strcmp (name, ".sdata") == 0
2351 || strcmp (name, ".sbss") == 0
2352 || strcmp (name, ".lit4") == 0
2353 || strcmp (name, ".lit8") == 0)
2354 hdr->sh_flags |= SHF_ALPHA_GPREL;
2356 return TRUE;
2359 /* Hook called by the linker routine which adds symbols from an object
2360 file. We use it to put .comm items in .sbss, and not .bss. */
2362 static bfd_boolean
2363 elf64_alpha_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2364 bfd *abfd;
2365 struct bfd_link_info *info;
2366 const Elf_Internal_Sym *sym;
2367 const char **namep ATTRIBUTE_UNUSED;
2368 flagword *flagsp ATTRIBUTE_UNUSED;
2369 asection **secp;
2370 bfd_vma *valp;
2372 if (sym->st_shndx == SHN_COMMON
2373 && !info->relocatable
2374 && sym->st_size <= elf_gp_size (abfd))
2376 /* Common symbols less than or equal to -G nn bytes are
2377 automatically put into .sbss. */
2379 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
2381 if (scomm == NULL)
2383 scomm = bfd_make_section (abfd, ".scommon");
2384 if (scomm == NULL
2385 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
2386 | SEC_IS_COMMON
2387 | SEC_LINKER_CREATED)))
2388 return FALSE;
2391 *secp = scomm;
2392 *valp = sym->st_size;
2395 return TRUE;
2398 /* Create the .got section. */
2400 static bfd_boolean
2401 elf64_alpha_create_got_section(abfd, info)
2402 bfd *abfd;
2403 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2405 asection *s;
2407 if ((s = bfd_get_section_by_name (abfd, ".got")))
2409 /* Check for a non-linker created .got? */
2410 if (alpha_elf_tdata (abfd)->got == NULL)
2411 alpha_elf_tdata (abfd)->got = s;
2412 return TRUE;
2415 s = bfd_make_section (abfd, ".got");
2416 if (s == NULL
2417 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2418 | SEC_HAS_CONTENTS
2419 | SEC_IN_MEMORY
2420 | SEC_LINKER_CREATED))
2421 || !bfd_set_section_alignment (abfd, s, 3))
2422 return FALSE;
2424 alpha_elf_tdata (abfd)->got = s;
2426 return TRUE;
2429 /* Create all the dynamic sections. */
2431 static bfd_boolean
2432 elf64_alpha_create_dynamic_sections (abfd, info)
2433 bfd *abfd;
2434 struct bfd_link_info *info;
2436 asection *s;
2437 struct elf_link_hash_entry *h;
2438 struct bfd_link_hash_entry *bh;
2440 /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
2442 s = bfd_make_section (abfd, ".plt");
2443 if (s == NULL
2444 || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2445 | SEC_HAS_CONTENTS
2446 | SEC_IN_MEMORY
2447 | SEC_LINKER_CREATED
2448 | SEC_CODE))
2449 || ! bfd_set_section_alignment (abfd, s, 3))
2450 return FALSE;
2452 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2453 .plt section. */
2454 bh = NULL;
2455 if (! (_bfd_generic_link_add_one_symbol
2456 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2457 (bfd_vma) 0, (const char *) NULL, FALSE,
2458 get_elf_backend_data (abfd)->collect, &bh)))
2459 return FALSE;
2460 h = (struct elf_link_hash_entry *) bh;
2461 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2462 h->type = STT_OBJECT;
2464 if (info->shared
2465 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2466 return FALSE;
2468 s = bfd_make_section (abfd, ".rela.plt");
2469 if (s == NULL
2470 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2471 | SEC_HAS_CONTENTS
2472 | SEC_IN_MEMORY
2473 | SEC_LINKER_CREATED
2474 | SEC_READONLY))
2475 || ! bfd_set_section_alignment (abfd, s, 3))
2476 return FALSE;
2478 /* We may or may not have created a .got section for this object, but
2479 we definitely havn't done the rest of the work. */
2481 if (!elf64_alpha_create_got_section (abfd, info))
2482 return FALSE;
2484 s = bfd_make_section(abfd, ".rela.got");
2485 if (s == NULL
2486 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2487 | SEC_HAS_CONTENTS
2488 | SEC_IN_MEMORY
2489 | SEC_LINKER_CREATED
2490 | SEC_READONLY))
2491 || !bfd_set_section_alignment (abfd, s, 3))
2492 return FALSE;
2494 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
2495 dynobj's .got section. We don't do this in the linker script
2496 because we don't want to define the symbol if we are not creating
2497 a global offset table. */
2498 bh = NULL;
2499 if (!(_bfd_generic_link_add_one_symbol
2500 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
2501 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
2502 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2503 return FALSE;
2504 h = (struct elf_link_hash_entry *) bh;
2505 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2506 h->type = STT_OBJECT;
2508 if (info->shared
2509 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2510 return FALSE;
2512 elf_hash_table (info)->hgot = h;
2514 return TRUE;
2517 /* Read ECOFF debugging information from a .mdebug section into a
2518 ecoff_debug_info structure. */
2520 static bfd_boolean
2521 elf64_alpha_read_ecoff_info (abfd, section, debug)
2522 bfd *abfd;
2523 asection *section;
2524 struct ecoff_debug_info *debug;
2526 HDRR *symhdr;
2527 const struct ecoff_debug_swap *swap;
2528 char *ext_hdr = NULL;
2530 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2531 memset (debug, 0, sizeof (*debug));
2533 ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
2534 if (ext_hdr == NULL && swap->external_hdr_size != 0)
2535 goto error_return;
2537 if (! bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2538 swap->external_hdr_size))
2539 goto error_return;
2541 symhdr = &debug->symbolic_header;
2542 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2544 /* The symbolic header contains absolute file offsets and sizes to
2545 read. */
2546 #define READ(ptr, offset, count, size, type) \
2547 if (symhdr->count == 0) \
2548 debug->ptr = NULL; \
2549 else \
2551 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
2552 debug->ptr = (type) bfd_malloc (amt); \
2553 if (debug->ptr == NULL) \
2554 goto error_return; \
2555 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2556 || bfd_bread (debug->ptr, amt, abfd) != amt) \
2557 goto error_return; \
2560 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2561 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2562 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2563 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2564 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2565 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2566 union aux_ext *);
2567 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2568 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2569 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2570 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2571 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2572 #undef READ
2574 debug->fdr = NULL;
2575 debug->adjust = NULL;
2577 return TRUE;
2579 error_return:
2580 if (ext_hdr != NULL)
2581 free (ext_hdr);
2582 if (debug->line != NULL)
2583 free (debug->line);
2584 if (debug->external_dnr != NULL)
2585 free (debug->external_dnr);
2586 if (debug->external_pdr != NULL)
2587 free (debug->external_pdr);
2588 if (debug->external_sym != NULL)
2589 free (debug->external_sym);
2590 if (debug->external_opt != NULL)
2591 free (debug->external_opt);
2592 if (debug->external_aux != NULL)
2593 free (debug->external_aux);
2594 if (debug->ss != NULL)
2595 free (debug->ss);
2596 if (debug->ssext != NULL)
2597 free (debug->ssext);
2598 if (debug->external_fdr != NULL)
2599 free (debug->external_fdr);
2600 if (debug->external_rfd != NULL)
2601 free (debug->external_rfd);
2602 if (debug->external_ext != NULL)
2603 free (debug->external_ext);
2604 return FALSE;
2607 /* Alpha ELF local labels start with '$'. */
2609 static bfd_boolean
2610 elf64_alpha_is_local_label_name (abfd, name)
2611 bfd *abfd ATTRIBUTE_UNUSED;
2612 const char *name;
2614 return name[0] == '$';
2617 /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
2618 routine in order to handle the ECOFF debugging information. We
2619 still call this mips_elf_find_line because of the slot
2620 find_line_info in elf_obj_tdata is declared that way. */
2622 struct mips_elf_find_line
2624 struct ecoff_debug_info d;
2625 struct ecoff_find_line i;
2628 static bfd_boolean
2629 elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2630 functionname_ptr, line_ptr)
2631 bfd *abfd;
2632 asection *section;
2633 asymbol **symbols;
2634 bfd_vma offset;
2635 const char **filename_ptr;
2636 const char **functionname_ptr;
2637 unsigned int *line_ptr;
2639 asection *msec;
2641 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2642 filename_ptr, functionname_ptr,
2643 line_ptr, 0,
2644 &elf_tdata (abfd)->dwarf2_find_line_info))
2645 return TRUE;
2647 msec = bfd_get_section_by_name (abfd, ".mdebug");
2648 if (msec != NULL)
2650 flagword origflags;
2651 struct mips_elf_find_line *fi;
2652 const struct ecoff_debug_swap * const swap =
2653 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2655 /* If we are called during a link, alpha_elf_final_link may have
2656 cleared the SEC_HAS_CONTENTS field. We force it back on here
2657 if appropriate (which it normally will be). */
2658 origflags = msec->flags;
2659 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2660 msec->flags |= SEC_HAS_CONTENTS;
2662 fi = elf_tdata (abfd)->find_line_info;
2663 if (fi == NULL)
2665 bfd_size_type external_fdr_size;
2666 char *fraw_src;
2667 char *fraw_end;
2668 struct fdr *fdr_ptr;
2669 bfd_size_type amt = sizeof (struct mips_elf_find_line);
2671 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
2672 if (fi == NULL)
2674 msec->flags = origflags;
2675 return FALSE;
2678 if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
2680 msec->flags = origflags;
2681 return FALSE;
2684 /* Swap in the FDR information. */
2685 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
2686 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
2687 if (fi->d.fdr == NULL)
2689 msec->flags = origflags;
2690 return FALSE;
2692 external_fdr_size = swap->external_fdr_size;
2693 fdr_ptr = fi->d.fdr;
2694 fraw_src = (char *) fi->d.external_fdr;
2695 fraw_end = (fraw_src
2696 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2697 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2698 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2700 elf_tdata (abfd)->find_line_info = fi;
2702 /* Note that we don't bother to ever free this information.
2703 find_nearest_line is either called all the time, as in
2704 objdump -l, so the information should be saved, or it is
2705 rarely called, as in ld error messages, so the memory
2706 wasted is unimportant. Still, it would probably be a
2707 good idea for free_cached_info to throw it away. */
2710 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2711 &fi->i, filename_ptr, functionname_ptr,
2712 line_ptr))
2714 msec->flags = origflags;
2715 return TRUE;
2718 msec->flags = origflags;
2721 /* Fall back on the generic ELF find_nearest_line routine. */
2723 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2724 filename_ptr, functionname_ptr,
2725 line_ptr);
2728 /* Structure used to pass information to alpha_elf_output_extsym. */
2730 struct extsym_info
2732 bfd *abfd;
2733 struct bfd_link_info *info;
2734 struct ecoff_debug_info *debug;
2735 const struct ecoff_debug_swap *swap;
2736 bfd_boolean failed;
2739 static bfd_boolean
2740 elf64_alpha_output_extsym (h, data)
2741 struct alpha_elf_link_hash_entry *h;
2742 PTR data;
2744 struct extsym_info *einfo = (struct extsym_info *) data;
2745 bfd_boolean strip;
2746 asection *sec, *output_section;
2748 if (h->root.root.type == bfd_link_hash_warning)
2749 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
2751 if (h->root.indx == -2)
2752 strip = FALSE;
2753 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2754 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2755 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2756 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2757 strip = TRUE;
2758 else if (einfo->info->strip == strip_all
2759 || (einfo->info->strip == strip_some
2760 && bfd_hash_lookup (einfo->info->keep_hash,
2761 h->root.root.root.string,
2762 FALSE, FALSE) == NULL))
2763 strip = TRUE;
2764 else
2765 strip = FALSE;
2767 if (strip)
2768 return TRUE;
2770 if (h->esym.ifd == -2)
2772 h->esym.jmptbl = 0;
2773 h->esym.cobol_main = 0;
2774 h->esym.weakext = 0;
2775 h->esym.reserved = 0;
2776 h->esym.ifd = ifdNil;
2777 h->esym.asym.value = 0;
2778 h->esym.asym.st = stGlobal;
2780 if (h->root.root.type != bfd_link_hash_defined
2781 && h->root.root.type != bfd_link_hash_defweak)
2782 h->esym.asym.sc = scAbs;
2783 else
2785 const char *name;
2787 sec = h->root.root.u.def.section;
2788 output_section = sec->output_section;
2790 /* When making a shared library and symbol h is the one from
2791 the another shared library, OUTPUT_SECTION may be null. */
2792 if (output_section == NULL)
2793 h->esym.asym.sc = scUndefined;
2794 else
2796 name = bfd_section_name (output_section->owner, output_section);
2798 if (strcmp (name, ".text") == 0)
2799 h->esym.asym.sc = scText;
2800 else if (strcmp (name, ".data") == 0)
2801 h->esym.asym.sc = scData;
2802 else if (strcmp (name, ".sdata") == 0)
2803 h->esym.asym.sc = scSData;
2804 else if (strcmp (name, ".rodata") == 0
2805 || strcmp (name, ".rdata") == 0)
2806 h->esym.asym.sc = scRData;
2807 else if (strcmp (name, ".bss") == 0)
2808 h->esym.asym.sc = scBss;
2809 else if (strcmp (name, ".sbss") == 0)
2810 h->esym.asym.sc = scSBss;
2811 else if (strcmp (name, ".init") == 0)
2812 h->esym.asym.sc = scInit;
2813 else if (strcmp (name, ".fini") == 0)
2814 h->esym.asym.sc = scFini;
2815 else
2816 h->esym.asym.sc = scAbs;
2820 h->esym.asym.reserved = 0;
2821 h->esym.asym.index = indexNil;
2824 if (h->root.root.type == bfd_link_hash_common)
2825 h->esym.asym.value = h->root.root.u.c.size;
2826 else if (h->root.root.type == bfd_link_hash_defined
2827 || h->root.root.type == bfd_link_hash_defweak)
2829 if (h->esym.asym.sc == scCommon)
2830 h->esym.asym.sc = scBss;
2831 else if (h->esym.asym.sc == scSCommon)
2832 h->esym.asym.sc = scSBss;
2834 sec = h->root.root.u.def.section;
2835 output_section = sec->output_section;
2836 if (output_section != NULL)
2837 h->esym.asym.value = (h->root.root.u.def.value
2838 + sec->output_offset
2839 + output_section->vma);
2840 else
2841 h->esym.asym.value = 0;
2843 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2845 /* Set type and value for a symbol with a function stub. */
2846 h->esym.asym.st = stProc;
2847 sec = bfd_get_section_by_name (einfo->abfd, ".plt");
2848 if (sec == NULL)
2849 h->esym.asym.value = 0;
2850 else
2852 output_section = sec->output_section;
2853 if (output_section != NULL)
2854 h->esym.asym.value = (h->root.plt.offset
2855 + sec->output_offset
2856 + output_section->vma);
2857 else
2858 h->esym.asym.value = 0;
2862 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2863 h->root.root.root.string,
2864 &h->esym))
2866 einfo->failed = TRUE;
2867 return FALSE;
2870 return TRUE;
2873 /* Search for and possibly create a got entry. */
2875 static struct alpha_elf_got_entry *
2876 get_got_entry (abfd, h, r_type, r_symndx, r_addend)
2877 bfd *abfd;
2878 struct alpha_elf_link_hash_entry *h;
2879 unsigned long r_type, r_symndx;
2880 bfd_vma r_addend;
2882 struct alpha_elf_got_entry *gotent;
2883 struct alpha_elf_got_entry **slot;
2885 if (h)
2886 slot = &h->got_entries;
2887 else
2889 /* This is a local .got entry -- record for merge. */
2891 struct alpha_elf_got_entry **local_got_entries;
2893 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2894 if (!local_got_entries)
2896 bfd_size_type size;
2897 Elf_Internal_Shdr *symtab_hdr;
2899 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2900 size = symtab_hdr->sh_info;
2901 size *= sizeof (struct alpha_elf_got_entry *);
2903 local_got_entries
2904 = (struct alpha_elf_got_entry **) bfd_zalloc (abfd, size);
2905 if (!local_got_entries)
2906 return NULL;
2908 alpha_elf_tdata (abfd)->local_got_entries = local_got_entries;
2911 slot = &local_got_entries[r_symndx];
2914 for (gotent = *slot; gotent ; gotent = gotent->next)
2915 if (gotent->gotobj == abfd
2916 && gotent->reloc_type == r_type
2917 && gotent->addend == r_addend)
2918 break;
2920 if (!gotent)
2922 int entry_size;
2923 bfd_size_type amt;
2925 amt = sizeof (struct alpha_elf_got_entry);
2926 gotent = (struct alpha_elf_got_entry *) bfd_alloc (abfd, amt);
2927 if (!gotent)
2928 return NULL;
2930 gotent->gotobj = abfd;
2931 gotent->addend = r_addend;
2932 gotent->got_offset = -1;
2933 gotent->use_count = 1;
2934 gotent->reloc_type = r_type;
2935 gotent->reloc_done = 0;
2936 gotent->reloc_xlated = 0;
2938 gotent->next = *slot;
2939 *slot = gotent;
2941 entry_size = alpha_got_entry_size (r_type);
2942 alpha_elf_tdata (abfd)->total_got_size += entry_size;
2943 if (!h)
2944 alpha_elf_tdata(abfd)->local_got_size += entry_size;
2946 else
2947 gotent->use_count += 1;
2949 return gotent;
2952 /* Handle dynamic relocations when doing an Alpha ELF link. */
2954 static bfd_boolean
2955 elf64_alpha_check_relocs (abfd, info, sec, relocs)
2956 bfd *abfd;
2957 struct bfd_link_info *info;
2958 asection *sec;
2959 const Elf_Internal_Rela *relocs;
2961 bfd *dynobj;
2962 asection *sreloc;
2963 const char *rel_sec_name;
2964 Elf_Internal_Shdr *symtab_hdr;
2965 struct alpha_elf_link_hash_entry **sym_hashes;
2966 const Elf_Internal_Rela *rel, *relend;
2967 bfd_boolean got_created;
2968 bfd_size_type amt;
2970 if (info->relocatable)
2971 return TRUE;
2973 dynobj = elf_hash_table(info)->dynobj;
2974 if (dynobj == NULL)
2975 elf_hash_table(info)->dynobj = dynobj = abfd;
2977 sreloc = NULL;
2978 rel_sec_name = NULL;
2979 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2980 sym_hashes = alpha_elf_sym_hashes(abfd);
2981 got_created = FALSE;
2983 relend = relocs + sec->reloc_count;
2984 for (rel = relocs; rel < relend; ++rel)
2986 enum {
2987 NEED_GOT = 1,
2988 NEED_GOT_ENTRY = 2,
2989 NEED_DYNREL = 4
2992 unsigned long r_symndx, r_type;
2993 struct alpha_elf_link_hash_entry *h;
2994 unsigned int gotent_flags;
2995 bfd_boolean maybe_dynamic;
2996 unsigned int need;
2997 bfd_vma addend;
2999 r_symndx = ELF64_R_SYM (rel->r_info);
3000 if (r_symndx < symtab_hdr->sh_info)
3001 h = NULL;
3002 else
3004 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3006 while (h->root.root.type == bfd_link_hash_indirect
3007 || h->root.root.type == bfd_link_hash_warning)
3008 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3010 h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3013 /* We can only get preliminary data on whether a symbol is
3014 locally or externally defined, as not all of the input files
3015 have yet been processed. Do something with what we know, as
3016 this may help reduce memory usage and processing time later. */
3017 maybe_dynamic = FALSE;
3018 if (h && ((info->shared
3019 && (!info->symbolic || info->unresolved_syms_in_shared_libs == RM_IGNORE))
3020 || ! (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
3021 || h->root.root.type == bfd_link_hash_defweak))
3022 maybe_dynamic = TRUE;
3024 need = 0;
3025 gotent_flags = 0;
3026 r_type = ELF64_R_TYPE (rel->r_info);
3027 addend = rel->r_addend;
3029 switch (r_type)
3031 case R_ALPHA_LITERAL:
3032 need = NEED_GOT | NEED_GOT_ENTRY;
3034 /* Remember how this literal is used from its LITUSEs.
3035 This will be important when it comes to decide if we can
3036 create a .plt entry for a function symbol. */
3037 while (++rel < relend && ELF64_R_TYPE (rel->r_info) == R_ALPHA_LITUSE)
3038 if (rel->r_addend >= 1 && rel->r_addend <= 5)
3039 gotent_flags |= 1 << rel->r_addend;
3040 --rel;
3042 /* No LITUSEs -- presumably the address is used somehow. */
3043 if (gotent_flags == 0)
3044 gotent_flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
3045 break;
3047 case R_ALPHA_GPDISP:
3048 case R_ALPHA_GPREL16:
3049 case R_ALPHA_GPREL32:
3050 case R_ALPHA_GPRELHIGH:
3051 case R_ALPHA_GPRELLOW:
3052 case R_ALPHA_BRSGP:
3053 need = NEED_GOT;
3054 break;
3056 case R_ALPHA_REFLONG:
3057 case R_ALPHA_REFQUAD:
3058 if ((info->shared && (sec->flags & SEC_ALLOC)) || maybe_dynamic)
3059 need = NEED_DYNREL;
3060 break;
3062 case R_ALPHA_TLSLDM:
3063 /* The symbol for a TLSLDM reloc is ignored. Collapse the
3064 reloc to the 0 symbol so that they all match. */
3065 r_symndx = 0;
3066 h = 0;
3067 maybe_dynamic = FALSE;
3068 /* FALLTHRU */
3070 case R_ALPHA_TLSGD:
3071 case R_ALPHA_GOTDTPREL:
3072 need = NEED_GOT | NEED_GOT_ENTRY;
3073 break;
3075 case R_ALPHA_GOTTPREL:
3076 need = NEED_GOT | NEED_GOT_ENTRY;
3077 gotent_flags = ALPHA_ELF_LINK_HASH_TLS_IE;
3078 if (info->shared)
3079 info->flags |= DF_STATIC_TLS;
3080 break;
3082 case R_ALPHA_TPREL64:
3083 if (info->shared || maybe_dynamic)
3084 need = NEED_DYNREL;
3085 if (info->shared)
3086 info->flags |= DF_STATIC_TLS;
3087 break;
3090 if (need & NEED_GOT)
3092 if (!got_created)
3094 if (!elf64_alpha_create_got_section (abfd, info))
3095 return FALSE;
3097 /* Make sure the object's gotobj is set to itself so
3098 that we default to every object with its own .got.
3099 We'll merge .gots later once we've collected each
3100 object's info. */
3101 alpha_elf_tdata(abfd)->gotobj = abfd;
3103 got_created = 1;
3107 if (need & NEED_GOT_ENTRY)
3109 struct alpha_elf_got_entry *gotent;
3111 gotent = get_got_entry (abfd, h, r_type, r_symndx, addend);
3112 if (!gotent)
3113 return FALSE;
3115 if (gotent_flags)
3117 gotent->flags |= gotent_flags;
3118 if (h)
3120 gotent_flags |= h->flags;
3121 h->flags = gotent_flags;
3123 /* Make a guess as to whether a .plt entry is needed. */
3124 if ((gotent_flags & ALPHA_ELF_LINK_HASH_LU_FUNC)
3125 && !(gotent_flags & ~ALPHA_ELF_LINK_HASH_LU_FUNC))
3126 h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3127 else
3128 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3133 if (need & NEED_DYNREL)
3135 if (rel_sec_name == NULL)
3137 rel_sec_name = (bfd_elf_string_from_elf_section
3138 (abfd, elf_elfheader(abfd)->e_shstrndx,
3139 elf_section_data(sec)->rel_hdr.sh_name));
3140 if (rel_sec_name == NULL)
3141 return FALSE;
3143 BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
3144 && strcmp (bfd_get_section_name (abfd, sec),
3145 rel_sec_name+5) == 0);
3148 /* We need to create the section here now whether we eventually
3149 use it or not so that it gets mapped to an output section by
3150 the linker. If not used, we'll kill it in
3151 size_dynamic_sections. */
3152 if (sreloc == NULL)
3154 sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
3155 if (sreloc == NULL)
3157 flagword flags;
3159 sreloc = bfd_make_section (dynobj, rel_sec_name);
3160 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
3161 | SEC_LINKER_CREATED | SEC_READONLY);
3162 if (sec->flags & SEC_ALLOC)
3163 flags |= SEC_ALLOC | SEC_LOAD;
3164 if (sreloc == NULL
3165 || !bfd_set_section_flags (dynobj, sreloc, flags)
3166 || !bfd_set_section_alignment (dynobj, sreloc, 3))
3167 return FALSE;
3171 if (h)
3173 /* Since we havn't seen all of the input symbols yet, we
3174 don't know whether we'll actually need a dynamic relocation
3175 entry for this reloc. So make a record of it. Once we
3176 find out if this thing needs dynamic relocation we'll
3177 expand the relocation sections by the appropriate amount. */
3179 struct alpha_elf_reloc_entry *rent;
3181 for (rent = h->reloc_entries; rent; rent = rent->next)
3182 if (rent->rtype == r_type && rent->srel == sreloc)
3183 break;
3185 if (!rent)
3187 amt = sizeof (struct alpha_elf_reloc_entry);
3188 rent = (struct alpha_elf_reloc_entry *) bfd_alloc (abfd, amt);
3189 if (!rent)
3190 return FALSE;
3192 rent->srel = sreloc;
3193 rent->rtype = r_type;
3194 rent->count = 1;
3195 rent->reltext = ((sec->flags & (SEC_READONLY | SEC_ALLOC))
3196 == (SEC_READONLY | SEC_ALLOC));
3198 rent->next = h->reloc_entries;
3199 h->reloc_entries = rent;
3201 else
3202 rent->count++;
3204 else if (info->shared)
3206 /* If this is a shared library, and the section is to be
3207 loaded into memory, we need a RELATIVE reloc. */
3208 sreloc->_raw_size += sizeof (Elf64_External_Rela);
3209 if ((sec->flags & (SEC_READONLY | SEC_ALLOC))
3210 == (SEC_READONLY | SEC_ALLOC))
3211 info->flags |= DF_TEXTREL;
3216 return TRUE;
3219 /* Adjust a symbol defined by a dynamic object and referenced by a
3220 regular object. The current definition is in some section of the
3221 dynamic object, but we're not including those sections. We have to
3222 change the definition to something the rest of the link can
3223 understand. */
3225 static bfd_boolean
3226 elf64_alpha_adjust_dynamic_symbol (info, h)
3227 struct bfd_link_info *info;
3228 struct elf_link_hash_entry *h;
3230 bfd *dynobj;
3231 asection *s;
3232 struct alpha_elf_link_hash_entry *ah;
3234 dynobj = elf_hash_table(info)->dynobj;
3235 ah = (struct alpha_elf_link_hash_entry *)h;
3237 /* Now that we've seen all of the input symbols, finalize our decision
3238 about whether this symbol should get a .plt entry. */
3240 if (alpha_elf_dynamic_symbol_p (h, info)
3241 && ((h->type == STT_FUNC
3242 && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
3243 || (h->type == STT_NOTYPE
3244 && (ah->flags & ALPHA_ELF_LINK_HASH_LU_FUNC)
3245 && !(ah->flags & ~ALPHA_ELF_LINK_HASH_LU_FUNC)))
3246 /* Don't prevent otherwise valid programs from linking by attempting
3247 to create a new .got entry somewhere. A Correct Solution would be
3248 to add a new .got section to a new object file and let it be merged
3249 somewhere later. But for now don't bother. */
3250 && ah->got_entries)
3252 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3254 s = bfd_get_section_by_name(dynobj, ".plt");
3255 if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
3256 return FALSE;
3258 /* The first bit of the .plt is reserved. */
3259 if (s->_raw_size == 0)
3260 s->_raw_size = PLT_HEADER_SIZE;
3262 h->plt.offset = s->_raw_size;
3263 s->_raw_size += PLT_ENTRY_SIZE;
3265 /* If this symbol is not defined in a regular file, and we are not
3266 generating a shared library, then set the symbol to the location
3267 in the .plt. This is required to make function pointers compare
3268 equal between the normal executable and the shared library. */
3269 if (! info->shared
3270 && h->root.type != bfd_link_hash_defweak)
3272 ah->plt_old_section = h->root.u.def.section;
3273 ah->plt_old_value = h->root.u.def.value;
3274 ah->flags |= ALPHA_ELF_LINK_HASH_PLT_LOC;
3275 h->root.u.def.section = s;
3276 h->root.u.def.value = h->plt.offset;
3279 /* We also need a JMP_SLOT entry in the .rela.plt section. */
3280 s = bfd_get_section_by_name (dynobj, ".rela.plt");
3281 BFD_ASSERT (s != NULL);
3282 s->_raw_size += sizeof (Elf64_External_Rela);
3284 return TRUE;
3286 else
3287 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3289 /* If this is a weak symbol, and there is a real definition, the
3290 processor independent code will have arranged for us to see the
3291 real definition first, and we can just use the same value. */
3292 if (h->weakdef != NULL)
3294 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3295 || h->weakdef->root.type == bfd_link_hash_defweak);
3296 h->root.u.def.section = h->weakdef->root.u.def.section;
3297 h->root.u.def.value = h->weakdef->root.u.def.value;
3298 return TRUE;
3301 /* This is a reference to a symbol defined by a dynamic object which
3302 is not a function. The Alpha, since it uses .got entries for all
3303 symbols even in regular objects, does not need the hackery of a
3304 .dynbss section and COPY dynamic relocations. */
3306 return TRUE;
3309 /* Symbol versioning can create new symbols, and make our old symbols
3310 indirect to the new ones. Consolidate the got and reloc information
3311 in these situations. */
3313 static bfd_boolean
3314 elf64_alpha_merge_ind_symbols (hi, dummy)
3315 struct alpha_elf_link_hash_entry *hi;
3316 PTR dummy ATTRIBUTE_UNUSED;
3318 struct alpha_elf_link_hash_entry *hs;
3320 if (hi->root.root.type != bfd_link_hash_indirect)
3321 return TRUE;
3322 hs = hi;
3323 do {
3324 hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
3325 } while (hs->root.root.type == bfd_link_hash_indirect);
3327 /* Merge the flags. Whee. */
3329 hs->flags |= hi->flags;
3331 /* Merge the .got entries. Cannibalize the old symbol's list in
3332 doing so, since we don't need it anymore. */
3334 if (hs->got_entries == NULL)
3335 hs->got_entries = hi->got_entries;
3336 else
3338 struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
3340 gsh = hs->got_entries;
3341 for (gi = hi->got_entries; gi ; gi = gin)
3343 gin = gi->next;
3344 for (gs = gsh; gs ; gs = gs->next)
3345 if (gi->gotobj == gs->gotobj
3346 && gi->reloc_type == gs->reloc_type
3347 && gi->addend == gs->addend)
3349 gi->use_count += gs->use_count;
3350 goto got_found;
3352 gi->next = hs->got_entries;
3353 hs->got_entries = gi;
3354 got_found:;
3357 hi->got_entries = NULL;
3359 /* And similar for the reloc entries. */
3361 if (hs->reloc_entries == NULL)
3362 hs->reloc_entries = hi->reloc_entries;
3363 else
3365 struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
3367 rsh = hs->reloc_entries;
3368 for (ri = hi->reloc_entries; ri ; ri = rin)
3370 rin = ri->next;
3371 for (rs = rsh; rs ; rs = rs->next)
3372 if (ri->rtype == rs->rtype && ri->srel == rs->srel)
3374 rs->count += ri->count;
3375 goto found_reloc;
3377 ri->next = hs->reloc_entries;
3378 hs->reloc_entries = ri;
3379 found_reloc:;
3382 hi->reloc_entries = NULL;
3384 return TRUE;
3387 /* Is it possible to merge two object file's .got tables? */
3389 static bfd_boolean
3390 elf64_alpha_can_merge_gots (a, b)
3391 bfd *a, *b;
3393 int total = alpha_elf_tdata (a)->total_got_size;
3394 bfd *bsub;
3396 /* Trivial quick fallout test. */
3397 if (total + alpha_elf_tdata (b)->total_got_size <= MAX_GOT_SIZE)
3398 return TRUE;
3400 /* By their nature, local .got entries cannot be merged. */
3401 if ((total += alpha_elf_tdata (b)->local_got_size) > MAX_GOT_SIZE)
3402 return FALSE;
3404 /* Failing the common trivial comparison, we must effectively
3405 perform the merge. Not actually performing the merge means that
3406 we don't have to store undo information in case we fail. */
3407 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
3409 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
3410 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
3411 int i, n;
3413 n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
3414 for (i = 0; i < n; ++i)
3416 struct alpha_elf_got_entry *ae, *be;
3417 struct alpha_elf_link_hash_entry *h;
3419 h = hashes[i];
3420 while (h->root.root.type == bfd_link_hash_indirect
3421 || h->root.root.type == bfd_link_hash_warning)
3422 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3424 for (be = h->got_entries; be ; be = be->next)
3426 if (be->use_count == 0)
3427 continue;
3428 if (be->gotobj != b)
3429 continue;
3431 for (ae = h->got_entries; ae ; ae = ae->next)
3432 if (ae->gotobj == a
3433 && ae->reloc_type == be->reloc_type
3434 && ae->addend == be->addend)
3435 goto global_found;
3437 total += alpha_got_entry_size (be->reloc_type);
3438 if (total > MAX_GOT_SIZE)
3439 return FALSE;
3440 global_found:;
3445 return TRUE;
3448 /* Actually merge two .got tables. */
3450 static void
3451 elf64_alpha_merge_gots (a, b)
3452 bfd *a, *b;
3454 int total = alpha_elf_tdata (a)->total_got_size;
3455 bfd *bsub;
3457 /* Remember local expansion. */
3459 int e = alpha_elf_tdata (b)->local_got_size;
3460 total += e;
3461 alpha_elf_tdata (a)->local_got_size += e;
3464 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
3466 struct alpha_elf_got_entry **local_got_entries;
3467 struct alpha_elf_link_hash_entry **hashes;
3468 Elf_Internal_Shdr *symtab_hdr;
3469 int i, n;
3471 /* Let the local .got entries know they are part of a new subsegment. */
3472 local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
3473 if (local_got_entries)
3475 n = elf_tdata (bsub)->symtab_hdr.sh_info;
3476 for (i = 0; i < n; ++i)
3478 struct alpha_elf_got_entry *ent;
3479 for (ent = local_got_entries[i]; ent; ent = ent->next)
3480 ent->gotobj = a;
3484 /* Merge the global .got entries. */
3485 hashes = alpha_elf_sym_hashes (bsub);
3486 symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
3488 n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
3489 for (i = 0; i < n; ++i)
3491 struct alpha_elf_got_entry *ae, *be, **pbe, **start;
3492 struct alpha_elf_link_hash_entry *h;
3494 h = hashes[i];
3495 while (h->root.root.type == bfd_link_hash_indirect
3496 || h->root.root.type == bfd_link_hash_warning)
3497 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3499 start = &h->got_entries;
3500 for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
3502 if (be->use_count == 0)
3504 *pbe = be->next;
3505 continue;
3507 if (be->gotobj != b)
3508 continue;
3510 for (ae = *start; ae ; ae = ae->next)
3511 if (ae->gotobj == a
3512 && ae->reloc_type == be->reloc_type
3513 && ae->addend == be->addend)
3515 ae->flags |= be->flags;
3516 ae->use_count += be->use_count;
3517 *pbe = be->next;
3518 goto global_found;
3520 be->gotobj = a;
3521 total += alpha_got_entry_size (be->reloc_type);
3523 global_found:;
3527 alpha_elf_tdata (bsub)->gotobj = a;
3529 alpha_elf_tdata (a)->total_got_size = total;
3531 /* Merge the two in_got chains. */
3533 bfd *next;
3535 bsub = a;
3536 while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
3537 bsub = next;
3539 alpha_elf_tdata (bsub)->in_got_link_next = b;
3543 /* Calculate the offsets for the got entries. */
3545 static bfd_boolean
3546 elf64_alpha_calc_got_offsets_for_symbol (h, arg)
3547 struct alpha_elf_link_hash_entry *h;
3548 PTR arg ATTRIBUTE_UNUSED;
3550 bfd_boolean result = TRUE;
3551 struct alpha_elf_got_entry *gotent;
3553 if (h->root.root.type == bfd_link_hash_warning)
3554 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3556 for (gotent = h->got_entries; gotent; gotent = gotent->next)
3557 if (gotent->use_count > 0)
3559 struct alpha_elf_obj_tdata *td;
3560 bfd_size_type *plge;
3562 td = alpha_elf_tdata (gotent->gotobj);
3563 if (td == NULL)
3565 _bfd_error_handler (_("Symbol %s has no GOT subsection for offset 0x%x"),
3566 h->root.root.root.string, gotent->got_offset);
3567 result = FALSE;
3568 continue;
3570 plge = &td->got->_raw_size;
3571 gotent->got_offset = *plge;
3572 *plge += alpha_got_entry_size (gotent->reloc_type);
3575 return result;
3578 static void
3579 elf64_alpha_calc_got_offsets (info)
3580 struct bfd_link_info *info;
3582 bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
3584 /* First, zero out the .got sizes, as we may be recalculating the
3585 .got after optimizing it. */
3586 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3587 alpha_elf_tdata(i)->got->_raw_size = 0;
3589 /* Next, fill in the offsets for all the global entries. */
3590 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3591 elf64_alpha_calc_got_offsets_for_symbol,
3592 NULL);
3594 /* Finally, fill in the offsets for the local entries. */
3595 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3597 bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
3598 bfd *j;
3600 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3602 struct alpha_elf_got_entry **local_got_entries, *gotent;
3603 int k, n;
3605 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3606 if (!local_got_entries)
3607 continue;
3609 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3610 for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
3611 if (gotent->use_count > 0)
3613 gotent->got_offset = got_offset;
3614 got_offset += alpha_got_entry_size (gotent->reloc_type);
3618 alpha_elf_tdata(i)->got->_raw_size = got_offset;
3619 alpha_elf_tdata(i)->got->_cooked_size = got_offset;
3623 /* Constructs the gots. */
3625 static bfd_boolean
3626 elf64_alpha_size_got_sections (info)
3627 struct bfd_link_info *info;
3629 bfd *i, *got_list, *cur_got_obj = NULL;
3630 int something_changed = 0;
3632 got_list = alpha_elf_hash_table (info)->got_list;
3634 /* On the first time through, pretend we have an existing got list
3635 consisting of all of the input files. */
3636 if (got_list == NULL)
3638 for (i = info->input_bfds; i ; i = i->link_next)
3640 bfd *this_got = alpha_elf_tdata (i)->gotobj;
3641 if (this_got == NULL)
3642 continue;
3644 /* We are assuming no merging has yet occurred. */
3645 BFD_ASSERT (this_got == i);
3647 if (alpha_elf_tdata (this_got)->total_got_size > MAX_GOT_SIZE)
3649 /* Yikes! A single object file has too many entries. */
3650 (*_bfd_error_handler)
3651 (_("%s: .got subsegment exceeds 64K (size %d)"),
3652 bfd_archive_filename (i),
3653 alpha_elf_tdata (this_got)->total_got_size);
3654 return FALSE;
3657 if (got_list == NULL)
3658 got_list = this_got;
3659 else
3660 alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
3661 cur_got_obj = this_got;
3664 /* Strange degenerate case of no got references. */
3665 if (got_list == NULL)
3666 return TRUE;
3668 alpha_elf_hash_table (info)->got_list = got_list;
3670 /* Force got offsets to be recalculated. */
3671 something_changed = 1;
3674 cur_got_obj = got_list;
3675 i = alpha_elf_tdata(cur_got_obj)->got_link_next;
3676 while (i != NULL)
3678 if (elf64_alpha_can_merge_gots (cur_got_obj, i))
3680 elf64_alpha_merge_gots (cur_got_obj, i);
3681 i = alpha_elf_tdata(i)->got_link_next;
3682 alpha_elf_tdata(cur_got_obj)->got_link_next = i;
3683 something_changed = 1;
3685 else
3687 cur_got_obj = i;
3688 i = alpha_elf_tdata(i)->got_link_next;
3692 /* Once the gots have been merged, fill in the got offsets for
3693 everything therein. */
3694 if (1 || something_changed)
3695 elf64_alpha_calc_got_offsets (info);
3697 return TRUE;
3700 /* Called from relax_section to rebuild the PLT in light of
3701 potential changes in the function's status. */
3703 static bfd_boolean
3704 elf64_alpha_size_plt_section (info)
3705 struct bfd_link_info *info;
3707 asection *splt, *spltrel;
3708 unsigned long entries;
3709 bfd *dynobj;
3711 dynobj = elf_hash_table(info)->dynobj;
3712 splt = bfd_get_section_by_name(dynobj, ".plt");
3713 if (splt == NULL)
3714 return TRUE;
3716 splt->_raw_size = 0;
3718 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3719 elf64_alpha_size_plt_section_1, splt);
3721 splt->_cooked_size = splt->_raw_size;
3723 /* Every plt entry requires a JMP_SLOT relocation. */
3724 spltrel = bfd_get_section_by_name (dynobj, ".rela.plt");
3725 if (splt->_raw_size)
3726 entries = (splt->_raw_size - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3727 else
3728 entries = 0;
3729 spltrel->_raw_size = entries * sizeof (Elf64_External_Rela);
3730 spltrel->_cooked_size = spltrel->_raw_size;
3732 return TRUE;
3735 static bfd_boolean
3736 elf64_alpha_size_plt_section_1 (h, data)
3737 struct alpha_elf_link_hash_entry *h;
3738 PTR data;
3740 asection *splt = (asection *) data;
3741 struct alpha_elf_got_entry *gotent;
3743 /* If we didn't need an entry before, we still don't. */
3744 if (!(h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT))
3745 return TRUE;
3747 /* There must still be a LITERAL got entry for the function. */
3748 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3749 if (gotent->reloc_type == R_ALPHA_LITERAL
3750 && gotent->use_count > 0)
3751 break;
3753 /* If there is, reset the PLT offset. If not, there's no longer
3754 a need for the PLT entry. */
3755 if (gotent)
3757 if (splt->_raw_size == 0)
3758 splt->_raw_size = PLT_HEADER_SIZE;
3759 h->root.plt.offset = splt->_raw_size;
3760 splt->_raw_size += PLT_ENTRY_SIZE;
3762 else
3764 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3765 h->root.plt.offset = -1;
3767 /* Undo the definition frobbing begun in adjust_dynamic_symbol. */
3768 if (h->flags & ALPHA_ELF_LINK_HASH_PLT_LOC)
3770 h->root.root.u.def.section = h->plt_old_section;
3771 h->root.root.u.def.value = h->plt_old_value;
3772 h->flags &= ~ALPHA_ELF_LINK_HASH_PLT_LOC;
3776 return TRUE;
3779 static bfd_boolean
3780 elf64_alpha_always_size_sections (output_bfd, info)
3781 bfd *output_bfd ATTRIBUTE_UNUSED;
3782 struct bfd_link_info *info;
3784 bfd *i;
3786 if (info->relocatable)
3787 return TRUE;
3789 /* First, take care of the indirect symbols created by versioning. */
3790 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3791 elf64_alpha_merge_ind_symbols,
3792 NULL);
3794 if (!elf64_alpha_size_got_sections (info))
3795 return FALSE;
3797 /* Allocate space for all of the .got subsections. */
3798 i = alpha_elf_hash_table (info)->got_list;
3799 for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
3801 asection *s = alpha_elf_tdata(i)->got;
3802 if (s->_raw_size > 0)
3804 s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
3805 if (s->contents == NULL)
3806 return FALSE;
3810 return TRUE;
3813 /* The number of dynamic relocations required by a static relocation. */
3815 static int
3816 alpha_dynamic_entries_for_reloc (r_type, dynamic, shared)
3817 int r_type, dynamic, shared;
3819 switch (r_type)
3821 /* May appear in GOT entries. */
3822 case R_ALPHA_TLSGD:
3823 return (dynamic ? 2 : shared ? 1 : 0);
3824 case R_ALPHA_TLSLDM:
3825 return shared;
3826 case R_ALPHA_LITERAL:
3827 case R_ALPHA_GOTTPREL:
3828 return dynamic || shared;
3829 case R_ALPHA_GOTDTPREL:
3830 return dynamic;
3832 /* May appear in data sections. */
3833 case R_ALPHA_REFLONG:
3834 case R_ALPHA_REFQUAD:
3835 case R_ALPHA_TPREL64:
3836 return dynamic || shared;
3838 /* Everything else is illegal. We'll issue an error during
3839 relocate_section. */
3840 default:
3841 return 0;
3845 /* Work out the sizes of the dynamic relocation entries. */
3847 static bfd_boolean
3848 elf64_alpha_calc_dynrel_sizes (h, info)
3849 struct alpha_elf_link_hash_entry *h;
3850 struct bfd_link_info *info;
3852 bfd_boolean dynamic;
3853 struct alpha_elf_reloc_entry *relent;
3854 unsigned long entries;
3856 if (h->root.root.type == bfd_link_hash_warning)
3857 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3859 /* If the symbol was defined as a common symbol in a regular object
3860 file, and there was no definition in any dynamic object, then the
3861 linker will have allocated space for the symbol in a common
3862 section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
3863 set. This is done for dynamic symbols in
3864 elf_adjust_dynamic_symbol but this is not done for non-dynamic
3865 symbols, somehow. */
3866 if (((h->root.elf_link_hash_flags
3867 & (ELF_LINK_HASH_DEF_REGULAR
3868 | ELF_LINK_HASH_REF_REGULAR
3869 | ELF_LINK_HASH_DEF_DYNAMIC))
3870 == ELF_LINK_HASH_REF_REGULAR)
3871 && (h->root.root.type == bfd_link_hash_defined
3872 || h->root.root.type == bfd_link_hash_defweak)
3873 && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
3874 h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3876 /* If the symbol is dynamic, we'll need all the relocations in their
3877 natural form. If this is a shared object, and it has been forced
3878 local, we'll need the same number of RELATIVE relocations. */
3880 dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
3882 for (relent = h->reloc_entries; relent; relent = relent->next)
3884 entries = alpha_dynamic_entries_for_reloc (relent->rtype, dynamic,
3885 info->shared);
3886 if (entries)
3888 relent->srel->_raw_size +=
3889 entries * sizeof (Elf64_External_Rela) * relent->count;
3890 if (relent->reltext)
3891 info->flags |= DT_TEXTREL;
3895 return TRUE;
3898 /* Set the sizes of the dynamic relocation sections. */
3900 static bfd_boolean
3901 elf64_alpha_size_rela_got_section (info)
3902 struct bfd_link_info *info;
3904 unsigned long entries;
3905 bfd *i, *dynobj;
3906 asection *srel;
3908 /* Shared libraries often require RELATIVE relocs, and some relocs
3909 require attention for the main application as well. */
3911 entries = 0;
3912 for (i = alpha_elf_hash_table(info)->got_list;
3913 i ; i = alpha_elf_tdata(i)->got_link_next)
3915 bfd *j;
3917 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3919 struct alpha_elf_got_entry **local_got_entries, *gotent;
3920 int k, n;
3922 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3923 if (!local_got_entries)
3924 continue;
3926 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3927 for (gotent = local_got_entries[k];
3928 gotent ; gotent = gotent->next)
3929 if (gotent->use_count > 0)
3930 entries += (alpha_dynamic_entries_for_reloc
3931 (gotent->reloc_type, 0, info->shared));
3935 dynobj = elf_hash_table(info)->dynobj;
3936 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3937 if (!srel)
3939 BFD_ASSERT (entries == 0);
3940 return TRUE;
3942 srel->_raw_size = sizeof (Elf64_External_Rela) * entries;
3944 /* Now do the non-local symbols. */
3945 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3946 elf64_alpha_size_rela_got_1, info);
3948 srel->_cooked_size = srel->_raw_size;
3950 return TRUE;
3953 /* Subroutine of elf64_alpha_size_rela_got_section for doing the
3954 global symbols. */
3956 static bfd_boolean
3957 elf64_alpha_size_rela_got_1 (h, info)
3958 struct alpha_elf_link_hash_entry *h;
3959 struct bfd_link_info *info;
3961 bfd_boolean dynamic;
3962 struct alpha_elf_got_entry *gotent;
3963 unsigned long entries;
3965 if (h->root.root.type == bfd_link_hash_warning)
3966 h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3968 /* If the symbol is dynamic, we'll need all the relocations in their
3969 natural form. If this is a shared object, and it has been forced
3970 local, we'll need the same number of RELATIVE relocations. */
3972 dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
3974 entries = 0;
3975 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3976 if (gotent->use_count > 0)
3977 entries += alpha_dynamic_entries_for_reloc (gotent->reloc_type,
3978 dynamic, info->shared);
3980 /* If we are using a .plt entry, subtract one, as the first
3981 reference uses a .rela.plt entry instead. */
3982 if (h->root.plt.offset != MINUS_ONE)
3983 entries--;
3985 if (entries > 0)
3987 bfd *dynobj = elf_hash_table(info)->dynobj;
3988 asection *srel = bfd_get_section_by_name (dynobj, ".rela.got");
3989 BFD_ASSERT (srel != NULL);
3990 srel->_raw_size += sizeof (Elf64_External_Rela) * entries;
3993 return TRUE;
3996 /* Set the sizes of the dynamic sections. */
3998 static bfd_boolean
3999 elf64_alpha_size_dynamic_sections (output_bfd, info)
4000 bfd *output_bfd ATTRIBUTE_UNUSED;
4001 struct bfd_link_info *info;
4003 bfd *dynobj;
4004 asection *s;
4005 bfd_boolean relplt;
4007 dynobj = elf_hash_table(info)->dynobj;
4008 BFD_ASSERT(dynobj != NULL);
4010 if (elf_hash_table (info)->dynamic_sections_created)
4012 /* Set the contents of the .interp section to the interpreter. */
4013 if (info->executable)
4015 s = bfd_get_section_by_name (dynobj, ".interp");
4016 BFD_ASSERT (s != NULL);
4017 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4018 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4021 /* Now that we've seen all of the input files, we can decide which
4022 symbols need dynamic relocation entries and which don't. We've
4023 collected information in check_relocs that we can now apply to
4024 size the dynamic relocation sections. */
4025 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
4026 elf64_alpha_calc_dynrel_sizes, info);
4028 elf64_alpha_size_rela_got_section (info);
4030 /* else we're not dynamic and by definition we don't need such things. */
4032 /* The check_relocs and adjust_dynamic_symbol entry points have
4033 determined the sizes of the various dynamic sections. Allocate
4034 memory for them. */
4035 relplt = FALSE;
4036 for (s = dynobj->sections; s != NULL; s = s->next)
4038 const char *name;
4039 bfd_boolean strip;
4041 if (!(s->flags & SEC_LINKER_CREATED))
4042 continue;
4044 /* It's OK to base decisions on the section name, because none
4045 of the dynobj section names depend upon the input files. */
4046 name = bfd_get_section_name (dynobj, s);
4048 /* If we don't need this section, strip it from the output file.
4049 This is to handle .rela.bss and .rela.plt. We must create it
4050 in create_dynamic_sections, because it must be created before
4051 the linker maps input sections to output sections. The
4052 linker does that before adjust_dynamic_symbol is called, and
4053 it is that function which decides whether anything needs to
4054 go into these sections. */
4056 strip = FALSE;
4058 if (strncmp (name, ".rela", 5) == 0)
4060 strip = (s->_raw_size == 0);
4062 if (!strip)
4064 if (strcmp(name, ".rela.plt") == 0)
4065 relplt = TRUE;
4067 /* We use the reloc_count field as a counter if we need
4068 to copy relocs into the output file. */
4069 s->reloc_count = 0;
4072 else if (strcmp (name, ".plt") != 0)
4074 /* It's not one of our dynamic sections, so don't allocate space. */
4075 continue;
4078 if (strip)
4079 _bfd_strip_section_from_output (info, s);
4080 else
4082 /* Allocate memory for the section contents. */
4083 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4084 if (s->contents == NULL && s->_raw_size != 0)
4085 return FALSE;
4089 if (elf_hash_table (info)->dynamic_sections_created)
4091 /* Add some entries to the .dynamic section. We fill in the
4092 values later, in elf64_alpha_finish_dynamic_sections, but we
4093 must add the entries now so that we get the correct size for
4094 the .dynamic section. The DT_DEBUG entry is filled in by the
4095 dynamic linker and used by the debugger. */
4096 #define add_dynamic_entry(TAG, VAL) \
4097 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4099 if (info->executable)
4101 if (!add_dynamic_entry (DT_DEBUG, 0))
4102 return FALSE;
4105 if (relplt)
4107 if (!add_dynamic_entry (DT_PLTGOT, 0)
4108 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4109 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4110 || !add_dynamic_entry (DT_JMPREL, 0))
4111 return FALSE;
4114 if (!add_dynamic_entry (DT_RELA, 0)
4115 || !add_dynamic_entry (DT_RELASZ, 0)
4116 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
4117 return FALSE;
4119 if (info->flags & DF_TEXTREL)
4121 if (!add_dynamic_entry (DT_TEXTREL, 0))
4122 return FALSE;
4125 #undef add_dynamic_entry
4127 return TRUE;
4130 /* Emit a dynamic relocation for (DYNINDX, RTYPE, ADDEND) at (SEC, OFFSET)
4131 into the next available slot in SREL. */
4133 static void
4134 elf64_alpha_emit_dynrel (abfd, info, sec, srel, offset, dynindx, rtype, addend)
4135 bfd *abfd;
4136 struct bfd_link_info *info;
4137 asection *sec, *srel;
4138 bfd_vma offset, addend;
4139 long dynindx, rtype;
4141 Elf_Internal_Rela outrel;
4142 bfd_byte *loc;
4144 BFD_ASSERT (srel != NULL);
4146 outrel.r_info = ELF64_R_INFO (dynindx, rtype);
4147 outrel.r_addend = addend;
4149 offset = _bfd_elf_section_offset (abfd, info, sec, offset);
4150 if ((offset | 1) != (bfd_vma) -1)
4151 outrel.r_offset = sec->output_section->vma + sec->output_offset + offset;
4152 else
4153 memset (&outrel, 0, sizeof (outrel));
4155 loc = srel->contents;
4156 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
4157 bfd_elf64_swap_reloca_out (abfd, &outrel, loc);
4158 BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
4159 <= srel->_cooked_size);
4162 /* Relocate an Alpha ELF section for a relocatable link.
4164 We don't have to change anything unless the reloc is against a section
4165 symbol, in which case we have to adjust according to where the section
4166 symbol winds up in the output section. */
4168 static bfd_boolean
4169 elf64_alpha_relocate_section_r (output_bfd, info, input_bfd, input_section,
4170 contents, relocs, local_syms, local_sections)
4171 bfd *output_bfd ATTRIBUTE_UNUSED;
4172 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4173 bfd *input_bfd;
4174 asection *input_section;
4175 bfd_byte *contents ATTRIBUTE_UNUSED;
4176 Elf_Internal_Rela *relocs;
4177 Elf_Internal_Sym *local_syms;
4178 asection **local_sections;
4180 unsigned long symtab_hdr_sh_info;
4181 Elf_Internal_Rela *rel;
4182 Elf_Internal_Rela *relend;
4183 bfd_boolean ret_val = TRUE;
4185 symtab_hdr_sh_info = elf_tdata (input_bfd)->symtab_hdr.sh_info;
4187 relend = relocs + input_section->reloc_count;
4188 for (rel = relocs; rel < relend; rel++)
4190 unsigned long r_symndx;
4191 Elf_Internal_Sym *sym;
4192 asection *sec;
4193 unsigned long r_type;
4195 r_type = ELF64_R_TYPE(rel->r_info);
4196 if (r_type >= R_ALPHA_max)
4198 (*_bfd_error_handler)
4199 (_("%s: unknown relocation type %d"),
4200 bfd_archive_filename (input_bfd), (int)r_type);
4201 bfd_set_error (bfd_error_bad_value);
4202 ret_val = FALSE;
4203 continue;
4206 r_symndx = ELF64_R_SYM(rel->r_info);
4208 /* The symbol associated with GPDISP and LITUSE is
4209 immaterial. Only the addend is significant. */
4210 if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
4211 continue;
4213 if (r_symndx < symtab_hdr_sh_info)
4215 sym = local_syms + r_symndx;
4216 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
4218 sec = local_sections[r_symndx];
4219 rel->r_addend += sec->output_offset + sym->st_value;
4224 return ret_val;
4227 /* Relocate an Alpha ELF section. */
4229 static bfd_boolean
4230 elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
4231 contents, relocs, local_syms, local_sections)
4232 bfd *output_bfd;
4233 struct bfd_link_info *info;
4234 bfd *input_bfd;
4235 asection *input_section;
4236 bfd_byte *contents;
4237 Elf_Internal_Rela *relocs;
4238 Elf_Internal_Sym *local_syms;
4239 asection **local_sections;
4241 Elf_Internal_Shdr *symtab_hdr;
4242 Elf_Internal_Rela *rel;
4243 Elf_Internal_Rela *relend;
4244 asection *sgot, *srel, *srelgot;
4245 bfd *dynobj, *gotobj;
4246 bfd_vma gp, tp_base, dtp_base;
4247 struct alpha_elf_got_entry **local_got_entries;
4248 bfd_boolean ret_val;
4249 const char *section_name;
4251 /* Handle relocatable links with a smaller loop. */
4252 if (info->relocatable)
4253 return elf64_alpha_relocate_section_r (output_bfd, info, input_bfd,
4254 input_section, contents, relocs,
4255 local_syms, local_sections);
4257 /* This is a final link. */
4259 ret_val = TRUE;
4261 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4263 dynobj = elf_hash_table (info)->dynobj;
4264 if (dynobj)
4265 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4266 else
4267 srelgot = NULL;
4269 section_name = (bfd_elf_string_from_elf_section
4270 (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
4271 elf_section_data(input_section)->rel_hdr.sh_name));
4272 BFD_ASSERT(section_name != NULL);
4273 srel = bfd_get_section_by_name (dynobj, section_name);
4275 /* Find the gp value for this input bfd. */
4276 gotobj = alpha_elf_tdata (input_bfd)->gotobj;
4277 if (gotobj)
4279 sgot = alpha_elf_tdata (gotobj)->got;
4280 gp = _bfd_get_gp_value (gotobj);
4281 if (gp == 0)
4283 gp = (sgot->output_section->vma
4284 + sgot->output_offset
4285 + 0x8000);
4286 _bfd_set_gp_value (gotobj, gp);
4289 else
4291 sgot = NULL;
4292 gp = 0;
4295 local_got_entries = alpha_elf_tdata(input_bfd)->local_got_entries;
4297 if (elf_hash_table (info)->tls_sec != NULL)
4299 dtp_base = alpha_get_dtprel_base (info);
4300 tp_base = alpha_get_tprel_base (info);
4302 else
4303 dtp_base = tp_base = 0;
4305 relend = relocs + input_section->reloc_count;
4306 for (rel = relocs; rel < relend; rel++)
4308 struct alpha_elf_link_hash_entry *h = NULL;
4309 struct alpha_elf_got_entry *gotent;
4310 bfd_reloc_status_type r;
4311 reloc_howto_type *howto;
4312 unsigned long r_symndx;
4313 Elf_Internal_Sym *sym = NULL;
4314 asection *sec = NULL;
4315 bfd_vma value;
4316 bfd_vma addend;
4317 bfd_boolean dynamic_symbol_p;
4318 bfd_boolean undef_weak_ref = FALSE;
4319 unsigned long r_type;
4321 r_type = ELF64_R_TYPE(rel->r_info);
4322 if (r_type >= R_ALPHA_max)
4324 (*_bfd_error_handler)
4325 (_("%s: unknown relocation type %d"),
4326 bfd_archive_filename (input_bfd), (int)r_type);
4327 bfd_set_error (bfd_error_bad_value);
4328 ret_val = FALSE;
4329 continue;
4332 howto = elf64_alpha_howto_table + r_type;
4333 r_symndx = ELF64_R_SYM(rel->r_info);
4335 /* The symbol for a TLSLDM reloc is ignored. Collapse the
4336 reloc to the 0 symbol so that they all match. */
4337 if (r_type == R_ALPHA_TLSLDM)
4338 r_symndx = 0;
4340 if (r_symndx < symtab_hdr->sh_info)
4342 asection *msec;
4343 sym = local_syms + r_symndx;
4344 sec = local_sections[r_symndx];
4345 msec = sec;
4346 value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
4348 /* If this is a tp-relative relocation against sym 0,
4349 this is hackery from relax_section. Force the value to
4350 be the tls base. */
4351 if (r_symndx == 0
4352 && (r_type == R_ALPHA_TLSLDM
4353 || r_type == R_ALPHA_GOTTPREL
4354 || r_type == R_ALPHA_TPREL64
4355 || r_type == R_ALPHA_TPRELHI
4356 || r_type == R_ALPHA_TPRELLO
4357 || r_type == R_ALPHA_TPREL16))
4358 value = tp_base;
4360 if (local_got_entries)
4361 gotent = local_got_entries[r_symndx];
4362 else
4363 gotent = NULL;
4365 /* Need to adjust local GOT entries' addends for SEC_MERGE
4366 unless it has been done already. */
4367 if ((sec->flags & SEC_MERGE)
4368 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
4369 && sec->sec_info_type == ELF_INFO_TYPE_MERGE
4370 && gotent
4371 && !gotent->reloc_xlated)
4373 struct alpha_elf_got_entry *ent;
4375 for (ent = gotent; ent; ent = ent->next)
4377 ent->reloc_xlated = 1;
4378 if (ent->use_count == 0)
4379 continue;
4380 msec = sec;
4381 ent->addend =
4382 _bfd_merged_section_offset (output_bfd, &msec,
4383 elf_section_data (sec)->
4384 sec_info,
4385 sym->st_value + ent->addend,
4386 (bfd_vma) 0);
4387 ent->addend -= sym->st_value;
4388 ent->addend += msec->output_section->vma
4389 + msec->output_offset
4390 - sec->output_section->vma
4391 - sec->output_offset;
4395 dynamic_symbol_p = FALSE;
4397 else
4399 bfd_boolean warned;
4400 bfd_boolean unresolved_reloc;
4401 struct elf_link_hash_entry *hh;
4403 RELOC_FOR_GLOBAL_SYMBOL (hh,
4404 (struct elf_link_hash_entry *) alpha_elf_sym_hashes (input_bfd),
4405 r_symndx, symtab_hdr, value,
4406 sec, unresolved_reloc, info,
4407 warned);
4409 if (warned)
4410 continue;
4412 if (value == 0
4413 && ! unresolved_reloc
4414 && hh->root.type == bfd_link_hash_undefweak)
4415 undef_weak_ref = TRUE;
4417 h = (struct alpha_elf_link_hash_entry *) hh;
4418 dynamic_symbol_p = alpha_elf_dynamic_symbol_p (&h->root, info);
4419 gotent = h->got_entries;
4422 addend = rel->r_addend;
4423 value += addend;
4425 /* Search for the proper got entry. */
4426 for (; gotent ; gotent = gotent->next)
4427 if (gotent->gotobj == gotobj
4428 && gotent->reloc_type == r_type
4429 && gotent->addend == addend)
4430 break;
4432 switch (r_type)
4434 case R_ALPHA_GPDISP:
4436 bfd_byte *p_ldah, *p_lda;
4438 BFD_ASSERT(gp != 0);
4440 value = (input_section->output_section->vma
4441 + input_section->output_offset
4442 + rel->r_offset);
4444 p_ldah = contents + rel->r_offset;
4445 p_lda = p_ldah + rel->r_addend;
4447 r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - value,
4448 p_ldah, p_lda);
4450 break;
4452 case R_ALPHA_LITERAL:
4453 BFD_ASSERT(sgot != NULL);
4454 BFD_ASSERT(gp != 0);
4455 BFD_ASSERT(gotent != NULL);
4456 BFD_ASSERT(gotent->use_count >= 1);
4458 if (!gotent->reloc_done)
4460 gotent->reloc_done = 1;
4462 bfd_put_64 (output_bfd, value,
4463 sgot->contents + gotent->got_offset);
4465 /* If the symbol has been forced local, output a
4466 RELATIVE reloc, otherwise it will be handled in
4467 finish_dynamic_symbol. */
4468 if (info->shared && !dynamic_symbol_p)
4469 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4470 gotent->got_offset, 0,
4471 R_ALPHA_RELATIVE, value);
4474 value = (sgot->output_section->vma
4475 + sgot->output_offset
4476 + gotent->got_offset);
4477 value -= gp;
4478 goto default_reloc;
4480 case R_ALPHA_GPREL32:
4481 /* If the target section was a removed linkonce section,
4482 r_symndx will be zero. In this case, assume that the
4483 switch will not be used, so don't fill it in. If we
4484 do nothing here, we'll get relocation truncated messages,
4485 due to the placement of the application above 4GB. */
4486 if (r_symndx == 0)
4488 r = bfd_reloc_ok;
4489 break;
4491 /* FALLTHRU */
4493 case R_ALPHA_GPREL16:
4494 case R_ALPHA_GPRELLOW:
4495 if (dynamic_symbol_p)
4497 (*_bfd_error_handler)
4498 (_("%s: gp-relative relocation against dynamic symbol %s"),
4499 bfd_archive_filename (input_bfd), h->root.root.root.string);
4500 ret_val = FALSE;
4502 BFD_ASSERT(gp != 0);
4503 value -= gp;
4504 goto default_reloc;
4506 case R_ALPHA_GPRELHIGH:
4507 if (dynamic_symbol_p)
4509 (*_bfd_error_handler)
4510 (_("%s: gp-relative relocation against dynamic symbol %s"),
4511 bfd_archive_filename (input_bfd), h->root.root.root.string);
4512 ret_val = FALSE;
4514 BFD_ASSERT(gp != 0);
4515 value -= gp;
4516 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4517 goto default_reloc;
4519 case R_ALPHA_HINT:
4520 /* A call to a dynamic symbol is definitely out of range of
4521 the 16-bit displacement. Don't bother writing anything. */
4522 if (dynamic_symbol_p)
4524 r = bfd_reloc_ok;
4525 break;
4527 /* The regular PC-relative stuff measures from the start of
4528 the instruction rather than the end. */
4529 value -= 4;
4530 goto default_reloc;
4532 case R_ALPHA_BRADDR:
4533 if (dynamic_symbol_p)
4535 (*_bfd_error_handler)
4536 (_("%s: pc-relative relocation against dynamic symbol %s"),
4537 bfd_archive_filename (input_bfd), h->root.root.root.string);
4538 ret_val = FALSE;
4540 /* The regular PC-relative stuff measures from the start of
4541 the instruction rather than the end. */
4542 value -= 4;
4543 goto default_reloc;
4545 case R_ALPHA_BRSGP:
4547 int other;
4548 const char *name;
4550 /* The regular PC-relative stuff measures from the start of
4551 the instruction rather than the end. */
4552 value -= 4;
4554 /* The source and destination gp must be the same. Note that
4555 the source will always have an assigned gp, since we forced
4556 one in check_relocs, but that the destination may not, as
4557 it might not have had any relocations at all. Also take
4558 care not to crash if H is an undefined symbol. */
4559 if (h != NULL && sec != NULL
4560 && alpha_elf_tdata (sec->owner)->gotobj
4561 && gotobj != alpha_elf_tdata (sec->owner)->gotobj)
4563 (*_bfd_error_handler)
4564 (_("%s: change in gp: BRSGP %s"),
4565 bfd_archive_filename (input_bfd), h->root.root.root.string);
4566 ret_val = FALSE;
4569 /* The symbol should be marked either NOPV or STD_GPLOAD. */
4570 if (h != NULL)
4571 other = h->root.other;
4572 else
4573 other = sym->st_other;
4574 switch (other & STO_ALPHA_STD_GPLOAD)
4576 case STO_ALPHA_NOPV:
4577 break;
4578 case STO_ALPHA_STD_GPLOAD:
4579 value += 8;
4580 break;
4581 default:
4582 if (h != NULL)
4583 name = h->root.root.root.string;
4584 else
4586 name = (bfd_elf_string_from_elf_section
4587 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4588 if (name == NULL)
4589 name = _("<unknown>");
4590 else if (name[0] == 0)
4591 name = bfd_section_name (input_bfd, sec);
4593 (*_bfd_error_handler)
4594 (_("%s: !samegp reloc against symbol without .prologue: %s"),
4595 bfd_archive_filename (input_bfd), name);
4596 ret_val = FALSE;
4597 break;
4600 goto default_reloc;
4603 case R_ALPHA_REFLONG:
4604 case R_ALPHA_REFQUAD:
4605 case R_ALPHA_DTPREL64:
4606 case R_ALPHA_TPREL64:
4608 long dynindx, dyntype = r_type;
4609 bfd_vma dynaddend;
4611 /* Careful here to remember RELATIVE relocations for global
4612 variables for symbolic shared objects. */
4614 if (dynamic_symbol_p)
4616 BFD_ASSERT(h->root.dynindx != -1);
4617 dynindx = h->root.dynindx;
4618 dynaddend = addend;
4619 addend = 0, value = 0;
4621 else if (r_type == R_ALPHA_DTPREL64)
4623 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4624 value -= dtp_base;
4625 goto default_reloc;
4627 else if (r_type == R_ALPHA_TPREL64)
4629 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4630 if (!info->shared)
4632 value -= tp_base;
4633 goto default_reloc;
4635 dynindx = 0;
4636 dynaddend = value - dtp_base;
4638 else if (info->shared
4639 && r_symndx != 0
4640 && (input_section->flags & SEC_ALLOC))
4642 if (r_type == R_ALPHA_REFLONG)
4644 (*_bfd_error_handler)
4645 (_("%s: unhandled dynamic relocation against %s"),
4646 bfd_archive_filename (input_bfd),
4647 h->root.root.root.string);
4648 ret_val = FALSE;
4650 dynindx = 0;
4651 dyntype = R_ALPHA_RELATIVE;
4652 dynaddend = value;
4654 else
4655 goto default_reloc;
4657 elf64_alpha_emit_dynrel (output_bfd, info, input_section,
4658 srel, rel->r_offset, dynindx,
4659 dyntype, dynaddend);
4661 goto default_reloc;
4663 case R_ALPHA_SREL16:
4664 case R_ALPHA_SREL32:
4665 case R_ALPHA_SREL64:
4666 if (dynamic_symbol_p)
4668 (*_bfd_error_handler)
4669 (_("%s: pc-relative relocation against dynamic symbol %s"),
4670 bfd_archive_filename (input_bfd), h->root.root.root.string);
4671 ret_val = FALSE;
4674 /* ??? .eh_frame references to discarded sections will be smashed
4675 to relocations against SHN_UNDEF. The .eh_frame format allows
4676 NULL to be encoded as 0 in any format, so this works here. */
4677 if (r_symndx == 0)
4678 howto = (elf64_alpha_howto_table
4679 + (r_type - R_ALPHA_SREL32 + R_ALPHA_REFLONG));
4680 goto default_reloc;
4682 case R_ALPHA_TLSLDM:
4683 /* Ignore the symbol for the relocation. The result is always
4684 the current module. */
4685 dynamic_symbol_p = 0;
4686 /* FALLTHRU */
4688 case R_ALPHA_TLSGD:
4689 if (!gotent->reloc_done)
4691 gotent->reloc_done = 1;
4693 /* Note that the module index for the main program is 1. */
4694 bfd_put_64 (output_bfd, !info->shared && !dynamic_symbol_p,
4695 sgot->contents + gotent->got_offset);
4697 /* If the symbol has been forced local, output a
4698 DTPMOD64 reloc, otherwise it will be handled in
4699 finish_dynamic_symbol. */
4700 if (info->shared && !dynamic_symbol_p)
4701 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4702 gotent->got_offset, 0,
4703 R_ALPHA_DTPMOD64, 0);
4705 if (dynamic_symbol_p || r_type == R_ALPHA_TLSLDM)
4706 value = 0;
4707 else
4709 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4710 value -= dtp_base;
4712 bfd_put_64 (output_bfd, value,
4713 sgot->contents + gotent->got_offset + 8);
4716 value = (sgot->output_section->vma
4717 + sgot->output_offset
4718 + gotent->got_offset);
4719 value -= gp;
4720 goto default_reloc;
4722 case R_ALPHA_DTPRELHI:
4723 case R_ALPHA_DTPRELLO:
4724 case R_ALPHA_DTPREL16:
4725 if (dynamic_symbol_p)
4727 (*_bfd_error_handler)
4728 (_("%s: dtp-relative relocation against dynamic symbol %s"),
4729 bfd_archive_filename (input_bfd), h->root.root.root.string);
4730 ret_val = FALSE;
4732 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4733 value -= dtp_base;
4734 if (r_type == R_ALPHA_DTPRELHI)
4735 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4736 goto default_reloc;
4738 case R_ALPHA_TPRELHI:
4739 case R_ALPHA_TPRELLO:
4740 case R_ALPHA_TPREL16:
4741 if (info->shared)
4743 (*_bfd_error_handler)
4744 (_("%s: TLS local exec code cannot be linked into shared objects"),
4745 bfd_archive_filename (input_bfd));
4746 ret_val = FALSE;
4748 else if (dynamic_symbol_p)
4750 (*_bfd_error_handler)
4751 (_("%s: tp-relative relocation against dynamic symbol %s"),
4752 bfd_archive_filename (input_bfd), h->root.root.root.string);
4753 ret_val = FALSE;
4755 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4756 value -= tp_base;
4757 if (r_type == R_ALPHA_TPRELHI)
4758 value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4759 goto default_reloc;
4761 case R_ALPHA_GOTDTPREL:
4762 case R_ALPHA_GOTTPREL:
4763 BFD_ASSERT(sgot != NULL);
4764 BFD_ASSERT(gp != 0);
4765 BFD_ASSERT(gotent != NULL);
4766 BFD_ASSERT(gotent->use_count >= 1);
4768 if (!gotent->reloc_done)
4770 gotent->reloc_done = 1;
4772 if (dynamic_symbol_p)
4773 value = 0;
4774 else
4776 BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
4777 if (r_type == R_ALPHA_GOTDTPREL)
4778 value -= dtp_base;
4779 else if (!info->shared)
4780 value -= tp_base;
4781 else
4783 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
4784 gotent->got_offset, 0,
4785 R_ALPHA_TPREL64,
4786 value - dtp_base);
4787 value = 0;
4790 bfd_put_64 (output_bfd, value,
4791 sgot->contents + gotent->got_offset);
4794 value = (sgot->output_section->vma
4795 + sgot->output_offset
4796 + gotent->got_offset);
4797 value -= gp;
4798 goto default_reloc;
4800 default:
4801 default_reloc:
4802 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4803 contents, rel->r_offset, value, 0);
4804 break;
4807 switch (r)
4809 case bfd_reloc_ok:
4810 break;
4812 case bfd_reloc_overflow:
4814 const char *name;
4816 /* Don't warn if the overflow is due to pc relative reloc
4817 against discarded section. Section optimization code should
4818 handle it. */
4820 if (r_symndx < symtab_hdr->sh_info
4821 && sec != NULL && howto->pc_relative
4822 && elf_discarded_section (sec))
4823 break;
4825 if (h != NULL)
4826 name = h->root.root.root.string;
4827 else
4829 name = (bfd_elf_string_from_elf_section
4830 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4831 if (name == NULL)
4832 return FALSE;
4833 if (*name == '\0')
4834 name = bfd_section_name (input_bfd, sec);
4836 if (! ((*info->callbacks->reloc_overflow)
4837 (info, name, howto->name, (bfd_vma) 0,
4838 input_bfd, input_section, rel->r_offset)))
4839 ret_val = FALSE;
4841 break;
4843 default:
4844 case bfd_reloc_outofrange:
4845 abort ();
4849 return ret_val;
4852 /* Finish up dynamic symbol handling. We set the contents of various
4853 dynamic sections here. */
4855 static bfd_boolean
4856 elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
4857 bfd *output_bfd;
4858 struct bfd_link_info *info;
4859 struct elf_link_hash_entry *h;
4860 Elf_Internal_Sym *sym;
4862 bfd *dynobj = elf_hash_table(info)->dynobj;
4864 if (h->plt.offset != MINUS_ONE)
4866 /* Fill in the .plt entry for this symbol. */
4867 asection *splt, *sgot, *srel;
4868 Elf_Internal_Rela outrel;
4869 bfd_byte *loc;
4870 bfd_vma got_addr, plt_addr;
4871 bfd_vma plt_index;
4872 struct alpha_elf_got_entry *gotent;
4874 BFD_ASSERT (h->dynindx != -1);
4876 /* The first .got entry will be updated by the .plt with the
4877 address of the target function. */
4878 gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
4879 BFD_ASSERT (gotent && gotent->addend == 0);
4881 splt = bfd_get_section_by_name (dynobj, ".plt");
4882 BFD_ASSERT (splt != NULL);
4883 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4884 BFD_ASSERT (srel != NULL);
4885 sgot = alpha_elf_tdata (gotent->gotobj)->got;
4886 BFD_ASSERT (sgot != NULL);
4888 got_addr = (sgot->output_section->vma
4889 + sgot->output_offset
4890 + gotent->got_offset);
4891 plt_addr = (splt->output_section->vma
4892 + splt->output_offset
4893 + h->plt.offset);
4895 plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
4897 /* Fill in the entry in the procedure linkage table. */
4899 bfd_vma insn1, insn2, insn3;
4901 insn1 = PLT_ENTRY_WORD1 | ((-(h->plt.offset + 4) >> 2) & 0x1fffff);
4902 insn2 = PLT_ENTRY_WORD2;
4903 insn3 = PLT_ENTRY_WORD3;
4905 bfd_put_32 (output_bfd, insn1, splt->contents + h->plt.offset);
4906 bfd_put_32 (output_bfd, insn2, splt->contents + h->plt.offset + 4);
4907 bfd_put_32 (output_bfd, insn3, splt->contents + h->plt.offset + 8);
4910 /* Fill in the entry in the .rela.plt section. */
4911 outrel.r_offset = got_addr;
4912 outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
4913 outrel.r_addend = 0;
4915 loc = srel->contents + plt_index * sizeof (Elf64_External_Rela);
4916 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
4918 if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
4920 /* Mark the symbol as undefined, rather than as defined in the
4921 .plt section. Leave the value alone. */
4922 sym->st_shndx = SHN_UNDEF;
4925 /* Fill in the entries in the .got. */
4926 bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
4928 /* Subsequent .got entries will continue to bounce through the .plt. */
4929 if (gotent->next)
4931 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4932 BFD_ASSERT (! info->shared || srel != NULL);
4934 gotent = gotent->next;
4937 sgot = alpha_elf_tdata(gotent->gotobj)->got;
4938 BFD_ASSERT(sgot != NULL);
4939 BFD_ASSERT(gotent->addend == 0);
4941 bfd_put_64 (output_bfd, plt_addr,
4942 sgot->contents + gotent->got_offset);
4944 if (info->shared)
4945 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
4946 gotent->got_offset, 0,
4947 R_ALPHA_RELATIVE, plt_addr);
4949 gotent = gotent->next;
4951 while (gotent != NULL);
4954 else if (alpha_elf_dynamic_symbol_p (h, info))
4956 /* Fill in the dynamic relocations for this symbol's .got entries. */
4957 asection *srel;
4958 struct alpha_elf_got_entry *gotent;
4960 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4961 BFD_ASSERT (srel != NULL);
4963 for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
4964 gotent != NULL;
4965 gotent = gotent->next)
4967 asection *sgot;
4968 long r_type;
4970 if (gotent->use_count == 0)
4971 continue;
4973 sgot = alpha_elf_tdata (gotent->gotobj)->got;
4975 r_type = gotent->reloc_type;
4976 switch (r_type)
4978 case R_ALPHA_LITERAL:
4979 r_type = R_ALPHA_GLOB_DAT;
4980 break;
4981 case R_ALPHA_TLSGD:
4982 r_type = R_ALPHA_DTPMOD64;
4983 break;
4984 case R_ALPHA_GOTDTPREL:
4985 r_type = R_ALPHA_DTPREL64;
4986 break;
4987 case R_ALPHA_GOTTPREL:
4988 r_type = R_ALPHA_TPREL64;
4989 break;
4990 case R_ALPHA_TLSLDM:
4991 default:
4992 abort ();
4995 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
4996 gotent->got_offset, h->dynindx,
4997 r_type, gotent->addend);
4999 if (gotent->reloc_type == R_ALPHA_TLSGD)
5000 elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
5001 gotent->got_offset + 8, h->dynindx,
5002 R_ALPHA_DTPREL64, gotent->addend);
5006 /* Mark some specially defined symbols as absolute. */
5007 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5008 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
5009 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
5010 sym->st_shndx = SHN_ABS;
5012 return TRUE;
5015 /* Finish up the dynamic sections. */
5017 static bfd_boolean
5018 elf64_alpha_finish_dynamic_sections (output_bfd, info)
5019 bfd *output_bfd;
5020 struct bfd_link_info *info;
5022 bfd *dynobj;
5023 asection *sdyn;
5025 dynobj = elf_hash_table (info)->dynobj;
5026 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5028 if (elf_hash_table (info)->dynamic_sections_created)
5030 asection *splt;
5031 Elf64_External_Dyn *dyncon, *dynconend;
5033 splt = bfd_get_section_by_name (dynobj, ".plt");
5034 BFD_ASSERT (splt != NULL && sdyn != NULL);
5036 dyncon = (Elf64_External_Dyn *) sdyn->contents;
5037 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
5038 for (; dyncon < dynconend; dyncon++)
5040 Elf_Internal_Dyn dyn;
5041 const char *name;
5042 asection *s;
5044 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
5046 switch (dyn.d_tag)
5048 case DT_PLTGOT:
5049 name = ".plt";
5050 goto get_vma;
5051 case DT_PLTRELSZ:
5052 name = ".rela.plt";
5053 goto get_size;
5054 case DT_JMPREL:
5055 name = ".rela.plt";
5056 goto get_vma;
5058 case DT_RELASZ:
5059 /* My interpretation of the TIS v1.1 ELF document indicates
5060 that RELASZ should not include JMPREL. This is not what
5061 the rest of the BFD does. It is, however, what the
5062 glibc ld.so wants. Do this fixup here until we found
5063 out who is right. */
5064 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
5065 if (s)
5067 dyn.d_un.d_val -=
5068 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
5070 break;
5072 get_vma:
5073 s = bfd_get_section_by_name (output_bfd, name);
5074 dyn.d_un.d_ptr = (s ? s->vma : 0);
5075 break;
5077 get_size:
5078 s = bfd_get_section_by_name (output_bfd, name);
5079 dyn.d_un.d_val =
5080 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
5081 break;
5084 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5087 /* Initialize the PLT0 entry. */
5088 if (splt->_raw_size > 0)
5090 bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
5091 bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
5092 bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
5093 bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
5095 /* The next two words will be filled in by ld.so */
5096 bfd_put_64 (output_bfd, (bfd_vma) 0, splt->contents + 16);
5097 bfd_put_64 (output_bfd, (bfd_vma) 0, splt->contents + 24);
5099 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
5103 return TRUE;
5106 /* We need to use a special link routine to handle the .mdebug section.
5107 We need to merge all instances of these sections together, not write
5108 them all out sequentially. */
5110 static bfd_boolean
5111 elf64_alpha_final_link (abfd, info)
5112 bfd *abfd;
5113 struct bfd_link_info *info;
5115 asection *o;
5116 struct bfd_link_order *p;
5117 asection *mdebug_sec;
5118 struct ecoff_debug_info debug;
5119 const struct ecoff_debug_swap *swap
5120 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
5121 HDRR *symhdr = &debug.symbolic_header;
5122 PTR mdebug_handle = NULL;
5124 /* Go through the sections and collect the mdebug information. */
5125 mdebug_sec = NULL;
5126 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5128 if (strcmp (o->name, ".mdebug") == 0)
5130 struct extsym_info einfo;
5132 /* We have found the .mdebug section in the output file.
5133 Look through all the link_orders comprising it and merge
5134 the information together. */
5135 symhdr->magic = swap->sym_magic;
5136 /* FIXME: What should the version stamp be? */
5137 symhdr->vstamp = 0;
5138 symhdr->ilineMax = 0;
5139 symhdr->cbLine = 0;
5140 symhdr->idnMax = 0;
5141 symhdr->ipdMax = 0;
5142 symhdr->isymMax = 0;
5143 symhdr->ioptMax = 0;
5144 symhdr->iauxMax = 0;
5145 symhdr->issMax = 0;
5146 symhdr->issExtMax = 0;
5147 symhdr->ifdMax = 0;
5148 symhdr->crfd = 0;
5149 symhdr->iextMax = 0;
5151 /* We accumulate the debugging information itself in the
5152 debug_info structure. */
5153 debug.line = NULL;
5154 debug.external_dnr = NULL;
5155 debug.external_pdr = NULL;
5156 debug.external_sym = NULL;
5157 debug.external_opt = NULL;
5158 debug.external_aux = NULL;
5159 debug.ss = NULL;
5160 debug.ssext = debug.ssext_end = NULL;
5161 debug.external_fdr = NULL;
5162 debug.external_rfd = NULL;
5163 debug.external_ext = debug.external_ext_end = NULL;
5165 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
5166 if (mdebug_handle == (PTR) NULL)
5167 return FALSE;
5169 if (1)
5171 asection *s;
5172 EXTR esym;
5173 bfd_vma last = 0;
5174 unsigned int i;
5175 static const char * const name[] =
5177 ".text", ".init", ".fini", ".data",
5178 ".rodata", ".sdata", ".sbss", ".bss"
5180 static const int sc[] = { scText, scInit, scFini, scData,
5181 scRData, scSData, scSBss, scBss };
5183 esym.jmptbl = 0;
5184 esym.cobol_main = 0;
5185 esym.weakext = 0;
5186 esym.reserved = 0;
5187 esym.ifd = ifdNil;
5188 esym.asym.iss = issNil;
5189 esym.asym.st = stLocal;
5190 esym.asym.reserved = 0;
5191 esym.asym.index = indexNil;
5192 for (i = 0; i < 8; i++)
5194 esym.asym.sc = sc[i];
5195 s = bfd_get_section_by_name (abfd, name[i]);
5196 if (s != NULL)
5198 esym.asym.value = s->vma;
5199 last = s->vma + s->_raw_size;
5201 else
5202 esym.asym.value = last;
5204 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
5205 name[i], &esym))
5206 return FALSE;
5210 for (p = o->link_order_head;
5211 p != (struct bfd_link_order *) NULL;
5212 p = p->next)
5214 asection *input_section;
5215 bfd *input_bfd;
5216 const struct ecoff_debug_swap *input_swap;
5217 struct ecoff_debug_info input_debug;
5218 char *eraw_src;
5219 char *eraw_end;
5221 if (p->type != bfd_indirect_link_order)
5223 if (p->type == bfd_data_link_order)
5224 continue;
5225 abort ();
5228 input_section = p->u.indirect.section;
5229 input_bfd = input_section->owner;
5231 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
5232 || (get_elf_backend_data (input_bfd)
5233 ->elf_backend_ecoff_debug_swap) == NULL)
5235 /* I don't know what a non ALPHA ELF bfd would be
5236 doing with a .mdebug section, but I don't really
5237 want to deal with it. */
5238 continue;
5241 input_swap = (get_elf_backend_data (input_bfd)
5242 ->elf_backend_ecoff_debug_swap);
5244 BFD_ASSERT (p->size == input_section->_raw_size);
5246 /* The ECOFF linking code expects that we have already
5247 read in the debugging information and set up an
5248 ecoff_debug_info structure, so we do that now. */
5249 if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
5250 &input_debug))
5251 return FALSE;
5253 if (! (bfd_ecoff_debug_accumulate
5254 (mdebug_handle, abfd, &debug, swap, input_bfd,
5255 &input_debug, input_swap, info)))
5256 return FALSE;
5258 /* Loop through the external symbols. For each one with
5259 interesting information, try to find the symbol in
5260 the linker global hash table and save the information
5261 for the output external symbols. */
5262 eraw_src = input_debug.external_ext;
5263 eraw_end = (eraw_src
5264 + (input_debug.symbolic_header.iextMax
5265 * input_swap->external_ext_size));
5266 for (;
5267 eraw_src < eraw_end;
5268 eraw_src += input_swap->external_ext_size)
5270 EXTR ext;
5271 const char *name;
5272 struct alpha_elf_link_hash_entry *h;
5274 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
5275 if (ext.asym.sc == scNil
5276 || ext.asym.sc == scUndefined
5277 || ext.asym.sc == scSUndefined)
5278 continue;
5280 name = input_debug.ssext + ext.asym.iss;
5281 h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
5282 name, FALSE, FALSE, TRUE);
5283 if (h == NULL || h->esym.ifd != -2)
5284 continue;
5286 if (ext.ifd != -1)
5288 BFD_ASSERT (ext.ifd
5289 < input_debug.symbolic_header.ifdMax);
5290 ext.ifd = input_debug.ifdmap[ext.ifd];
5293 h->esym = ext;
5296 /* Free up the information we just read. */
5297 free (input_debug.line);
5298 free (input_debug.external_dnr);
5299 free (input_debug.external_pdr);
5300 free (input_debug.external_sym);
5301 free (input_debug.external_opt);
5302 free (input_debug.external_aux);
5303 free (input_debug.ss);
5304 free (input_debug.ssext);
5305 free (input_debug.external_fdr);
5306 free (input_debug.external_rfd);
5307 free (input_debug.external_ext);
5309 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5310 elf_link_input_bfd ignores this section. */
5311 input_section->flags &=~ SEC_HAS_CONTENTS;
5314 /* Build the external symbol information. */
5315 einfo.abfd = abfd;
5316 einfo.info = info;
5317 einfo.debug = &debug;
5318 einfo.swap = swap;
5319 einfo.failed = FALSE;
5320 elf_link_hash_traverse (elf_hash_table (info),
5321 elf64_alpha_output_extsym,
5322 (PTR) &einfo);
5323 if (einfo.failed)
5324 return FALSE;
5326 /* Set the size of the .mdebug section. */
5327 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
5329 /* Skip this section later on (I don't think this currently
5330 matters, but someday it might). */
5331 o->link_order_head = (struct bfd_link_order *) NULL;
5333 mdebug_sec = o;
5337 /* Invoke the regular ELF backend linker to do all the work. */
5338 if (! bfd_elf64_bfd_final_link (abfd, info))
5339 return FALSE;
5341 /* Now write out the computed sections. */
5343 /* The .got subsections... */
5345 bfd *i, *dynobj = elf_hash_table(info)->dynobj;
5346 for (i = alpha_elf_hash_table(info)->got_list;
5347 i != NULL;
5348 i = alpha_elf_tdata(i)->got_link_next)
5350 asection *sgot;
5352 /* elf_bfd_final_link already did everything in dynobj. */
5353 if (i == dynobj)
5354 continue;
5356 sgot = alpha_elf_tdata(i)->got;
5357 if (! bfd_set_section_contents (abfd, sgot->output_section,
5358 sgot->contents,
5359 (file_ptr) sgot->output_offset,
5360 sgot->_raw_size))
5361 return FALSE;
5365 if (mdebug_sec != (asection *) NULL)
5367 BFD_ASSERT (abfd->output_has_begun);
5368 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5369 swap, info,
5370 mdebug_sec->filepos))
5371 return FALSE;
5373 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5376 return TRUE;
5379 static enum elf_reloc_type_class
5380 elf64_alpha_reloc_type_class (rela)
5381 const Elf_Internal_Rela *rela;
5383 switch ((int) ELF64_R_TYPE (rela->r_info))
5385 case R_ALPHA_RELATIVE:
5386 return reloc_class_relative;
5387 case R_ALPHA_JMP_SLOT:
5388 return reloc_class_plt;
5389 case R_ALPHA_COPY:
5390 return reloc_class_copy;
5391 default:
5392 return reloc_class_normal;
5396 static struct bfd_elf_special_section const elf64_alpha_special_sections[]=
5398 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
5399 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
5400 { NULL, 0, 0, 0, 0 }
5403 /* ECOFF swapping routines. These are used when dealing with the
5404 .mdebug section, which is in the ECOFF debugging format. Copied
5405 from elf32-mips.c. */
5406 static const struct ecoff_debug_swap
5407 elf64_alpha_ecoff_debug_swap =
5409 /* Symbol table magic number. */
5410 magicSym2,
5411 /* Alignment of debugging information. E.g., 4. */
5413 /* Sizes of external symbolic information. */
5414 sizeof (struct hdr_ext),
5415 sizeof (struct dnr_ext),
5416 sizeof (struct pdr_ext),
5417 sizeof (struct sym_ext),
5418 sizeof (struct opt_ext),
5419 sizeof (struct fdr_ext),
5420 sizeof (struct rfd_ext),
5421 sizeof (struct ext_ext),
5422 /* Functions to swap in external symbolic data. */
5423 ecoff_swap_hdr_in,
5424 ecoff_swap_dnr_in,
5425 ecoff_swap_pdr_in,
5426 ecoff_swap_sym_in,
5427 ecoff_swap_opt_in,
5428 ecoff_swap_fdr_in,
5429 ecoff_swap_rfd_in,
5430 ecoff_swap_ext_in,
5431 _bfd_ecoff_swap_tir_in,
5432 _bfd_ecoff_swap_rndx_in,
5433 /* Functions to swap out external symbolic data. */
5434 ecoff_swap_hdr_out,
5435 ecoff_swap_dnr_out,
5436 ecoff_swap_pdr_out,
5437 ecoff_swap_sym_out,
5438 ecoff_swap_opt_out,
5439 ecoff_swap_fdr_out,
5440 ecoff_swap_rfd_out,
5441 ecoff_swap_ext_out,
5442 _bfd_ecoff_swap_tir_out,
5443 _bfd_ecoff_swap_rndx_out,
5444 /* Function to read in symbolic data. */
5445 elf64_alpha_read_ecoff_info
5448 /* Use a non-standard hash bucket size of 8. */
5450 static const struct elf_size_info alpha_elf_size_info =
5452 sizeof (Elf64_External_Ehdr),
5453 sizeof (Elf64_External_Phdr),
5454 sizeof (Elf64_External_Shdr),
5455 sizeof (Elf64_External_Rel),
5456 sizeof (Elf64_External_Rela),
5457 sizeof (Elf64_External_Sym),
5458 sizeof (Elf64_External_Dyn),
5459 sizeof (Elf_External_Note),
5462 64, 3,
5463 ELFCLASS64, EV_CURRENT,
5464 bfd_elf64_write_out_phdrs,
5465 bfd_elf64_write_shdrs_and_ehdr,
5466 bfd_elf64_write_relocs,
5467 bfd_elf64_swap_symbol_in,
5468 bfd_elf64_swap_symbol_out,
5469 bfd_elf64_slurp_reloc_table,
5470 bfd_elf64_slurp_symbol_table,
5471 bfd_elf64_swap_dyn_in,
5472 bfd_elf64_swap_dyn_out,
5473 bfd_elf64_swap_reloc_in,
5474 bfd_elf64_swap_reloc_out,
5475 bfd_elf64_swap_reloca_in,
5476 bfd_elf64_swap_reloca_out
5479 #define TARGET_LITTLE_SYM bfd_elf64_alpha_vec
5480 #define TARGET_LITTLE_NAME "elf64-alpha"
5481 #define ELF_ARCH bfd_arch_alpha
5482 #define ELF_MACHINE_CODE EM_ALPHA
5483 #define ELF_MAXPAGESIZE 0x10000
5485 #define bfd_elf64_bfd_link_hash_table_create \
5486 elf64_alpha_bfd_link_hash_table_create
5488 #define bfd_elf64_bfd_reloc_type_lookup \
5489 elf64_alpha_bfd_reloc_type_lookup
5490 #define elf_info_to_howto \
5491 elf64_alpha_info_to_howto
5493 #define bfd_elf64_mkobject \
5494 elf64_alpha_mkobject
5495 #define elf_backend_object_p \
5496 elf64_alpha_object_p
5498 #define elf_backend_section_from_shdr \
5499 elf64_alpha_section_from_shdr
5500 #define elf_backend_section_flags \
5501 elf64_alpha_section_flags
5502 #define elf_backend_fake_sections \
5503 elf64_alpha_fake_sections
5505 #define bfd_elf64_bfd_is_local_label_name \
5506 elf64_alpha_is_local_label_name
5507 #define bfd_elf64_find_nearest_line \
5508 elf64_alpha_find_nearest_line
5509 #define bfd_elf64_bfd_relax_section \
5510 elf64_alpha_relax_section
5512 #define elf_backend_add_symbol_hook \
5513 elf64_alpha_add_symbol_hook
5514 #define elf_backend_check_relocs \
5515 elf64_alpha_check_relocs
5516 #define elf_backend_create_dynamic_sections \
5517 elf64_alpha_create_dynamic_sections
5518 #define elf_backend_adjust_dynamic_symbol \
5519 elf64_alpha_adjust_dynamic_symbol
5520 #define elf_backend_always_size_sections \
5521 elf64_alpha_always_size_sections
5522 #define elf_backend_size_dynamic_sections \
5523 elf64_alpha_size_dynamic_sections
5524 #define elf_backend_relocate_section \
5525 elf64_alpha_relocate_section
5526 #define elf_backend_finish_dynamic_symbol \
5527 elf64_alpha_finish_dynamic_symbol
5528 #define elf_backend_finish_dynamic_sections \
5529 elf64_alpha_finish_dynamic_sections
5530 #define bfd_elf64_bfd_final_link \
5531 elf64_alpha_final_link
5532 #define elf_backend_reloc_type_class \
5533 elf64_alpha_reloc_type_class
5535 #define elf_backend_ecoff_debug_swap \
5536 &elf64_alpha_ecoff_debug_swap
5538 #define elf_backend_size_info \
5539 alpha_elf_size_info
5541 #define elf_backend_special_sections \
5542 elf64_alpha_special_sections
5544 /* A few constants that determine how the .plt section is set up. */
5545 #define elf_backend_want_got_plt 0
5546 #define elf_backend_plt_readonly 0
5547 #define elf_backend_want_plt_sym 1
5548 #define elf_backend_got_header_size 0
5550 #include "elf64-target.h"
5552 /* FreeBSD support. */
5554 #undef TARGET_LITTLE_SYM
5555 #define TARGET_LITTLE_SYM bfd_elf64_alpha_freebsd_vec
5556 #undef TARGET_LITTLE_NAME
5557 #define TARGET_LITTLE_NAME "elf64-alpha-freebsd"
5559 /* The kernel recognizes executables as valid only if they carry a
5560 "FreeBSD" label in the ELF header. So we put this label on all
5561 executables and (for simplicity) also all other object files. */
5563 static void elf64_alpha_fbsd_post_process_headers
5564 PARAMS ((bfd *, struct bfd_link_info *));
5566 static void
5567 elf64_alpha_fbsd_post_process_headers (abfd, link_info)
5568 bfd * abfd;
5569 struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
5571 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
5573 i_ehdrp = elf_elfheader (abfd);
5575 /* Put an ABI label supported by FreeBSD >= 4.1. */
5576 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
5577 #ifdef OLD_FREEBSD_ABI_LABEL
5578 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
5579 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
5580 #endif
5583 #undef elf_backend_post_process_headers
5584 #define elf_backend_post_process_headers \
5585 elf64_alpha_fbsd_post_process_headers
5587 #undef elf64_bed
5588 #define elf64_bed elf64_alpha_fbsd_bed
5590 #include "elf64-target.h"