2001-02-08 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / elf64-alpha.c
blob28f0f4c2948dccf4ea56e252f3e2ead3f6e95a2b
1 /* Alpha specific support for 64-bit ELF
2 Copyright 1996, 97, 98, 1999 Free Software Foundation, Inc.
3 Contributed by Richard Henderson <rth@tamu.edu>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* We need a published ABI spec for this. Until one comes out, don't
22 assume this'll remain unchanged forever. */
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
29 #include "elf/alpha.h"
31 #define ALPHAECOFF
33 #define NO_COFF_RELOCS
34 #define NO_COFF_SYMBOLS
35 #define NO_COFF_LINENOS
37 /* Get the ECOFF swapping routines. Needed for the debug information. */
38 #include "coff/internal.h"
39 #include "coff/sym.h"
40 #include "coff/symconst.h"
41 #include "coff/ecoff.h"
42 #include "coff/alpha.h"
43 #include "aout/ar.h"
44 #include "libcoff.h"
45 #include "libecoff.h"
46 #define ECOFF_64
47 #include "ecoffswap.h"
49 static int alpha_elf_dynamic_symbol_p
50 PARAMS((struct elf_link_hash_entry *, struct bfd_link_info *));
51 static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
52 PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
53 static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
54 PARAMS((bfd *));
56 static bfd_reloc_status_type elf64_alpha_reloc_nil
57 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
58 static bfd_reloc_status_type elf64_alpha_reloc_bad
59 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
60 static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
61 PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
62 static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
63 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
65 static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
66 PARAMS((bfd *, bfd_reloc_code_real_type));
67 static void elf64_alpha_info_to_howto
68 PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
70 static boolean elf64_alpha_mkobject
71 PARAMS((bfd *));
72 static boolean elf64_alpha_object_p
73 PARAMS((bfd *));
74 static boolean elf64_alpha_section_from_shdr
75 PARAMS((bfd *, Elf64_Internal_Shdr *, char *));
76 static boolean elf64_alpha_fake_sections
77 PARAMS((bfd *, Elf64_Internal_Shdr *, asection *));
78 static boolean elf64_alpha_create_got_section
79 PARAMS((bfd *, struct bfd_link_info *));
80 static boolean elf64_alpha_create_dynamic_sections
81 PARAMS((bfd *, struct bfd_link_info *));
83 static boolean elf64_alpha_read_ecoff_info
84 PARAMS((bfd *, asection *, struct ecoff_debug_info *));
85 static boolean elf64_alpha_is_local_label_name
86 PARAMS((bfd *, const char *));
87 static boolean elf64_alpha_find_nearest_line
88 PARAMS((bfd *, asection *, asymbol **, bfd_vma, const char **,
89 const char **, unsigned int *));
91 #if defined(__STDC__) || defined(ALMOST_STDC)
92 struct alpha_elf_link_hash_entry;
93 #endif
95 static boolean elf64_alpha_output_extsym
96 PARAMS((struct alpha_elf_link_hash_entry *, PTR));
98 static boolean elf64_alpha_can_merge_gots
99 PARAMS((bfd *, bfd *));
100 static void elf64_alpha_merge_gots
101 PARAMS((bfd *, bfd *));
102 static boolean elf64_alpha_calc_got_offsets_for_symbol
103 PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
104 static void elf64_alpha_calc_got_offsets PARAMS ((struct bfd_link_info *));
105 static boolean elf64_alpha_size_got_sections
106 PARAMS ((bfd *, struct bfd_link_info *));
107 static boolean elf64_alpha_always_size_sections
108 PARAMS ((bfd *, struct bfd_link_info *));
109 static boolean elf64_alpha_calc_dynrel_sizes
110 PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
111 static boolean elf64_alpha_add_symbol_hook
112 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
113 const char **, flagword *, asection **, bfd_vma *));
114 static boolean elf64_alpha_check_relocs
115 PARAMS((bfd *, struct bfd_link_info *, asection *sec,
116 const Elf_Internal_Rela *));
117 static boolean elf64_alpha_adjust_dynamic_symbol
118 PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
119 static boolean elf64_alpha_size_dynamic_sections
120 PARAMS((bfd *, struct bfd_link_info *));
121 static boolean elf64_alpha_relocate_section
122 PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
123 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
124 static boolean elf64_alpha_finish_dynamic_symbol
125 PARAMS((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
126 Elf_Internal_Sym *));
127 static boolean elf64_alpha_finish_dynamic_sections
128 PARAMS((bfd *, struct bfd_link_info *));
129 static boolean elf64_alpha_final_link
130 PARAMS((bfd *, struct bfd_link_info *));
131 static boolean elf64_alpha_merge_ind_symbols
132 PARAMS((struct alpha_elf_link_hash_entry *, PTR));
133 static Elf_Internal_Rela * elf64_alpha_find_reloc_at_ofs
134 PARAMS ((Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_vma, int));
136 struct alpha_elf_link_hash_entry
138 struct elf_link_hash_entry root;
140 /* External symbol information. */
141 EXTR esym;
143 /* Cumulative flags for all the .got entries. */
144 int flags;
146 /* Contexts (LITUSE) in which a literal was referenced. */
147 #define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
148 #define ALPHA_ELF_LINK_HASH_LU_MEM 0x02
149 #define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
150 #define ALPHA_ELF_LINK_HASH_LU_FUNC 0x08
152 /* Used to implement multiple .got subsections. */
153 struct alpha_elf_got_entry
155 struct alpha_elf_got_entry *next;
157 /* which .got subsection? */
158 bfd *gotobj;
160 /* the addend in effect for this entry. */
161 bfd_vma addend;
163 /* the .got offset for this entry. */
164 int got_offset;
166 int flags;
168 /* An additional flag. */
169 #define ALPHA_ELF_GOT_ENTRY_RELOCS_DONE 0x10
171 int use_count;
172 } *got_entries;
174 /* used to count non-got, non-plt relocations for delayed sizing
175 of relocation sections. */
176 struct alpha_elf_reloc_entry
178 struct alpha_elf_reloc_entry *next;
180 /* which .reloc section? */
181 asection *srel;
183 /* what kind of relocation? */
184 unsigned long rtype;
186 /* how many did we find? */
187 unsigned long count;
188 } *reloc_entries;
191 /* Alpha ELF linker hash table. */
193 struct alpha_elf_link_hash_table
195 struct elf_link_hash_table root;
197 /* The head of a list of .got subsections linked through
198 alpha_elf_tdata(abfd)->got_link_next. */
199 bfd *got_list;
202 /* Look up an entry in a Alpha ELF linker hash table. */
204 #define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
205 ((struct alpha_elf_link_hash_entry *) \
206 elf_link_hash_lookup (&(table)->root, (string), (create), \
207 (copy), (follow)))
209 /* Traverse a Alpha ELF linker hash table. */
211 #define alpha_elf_link_hash_traverse(table, func, info) \
212 (elf_link_hash_traverse \
213 (&(table)->root, \
214 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
215 (info)))
217 /* Get the Alpha ELF linker hash table from a link_info structure. */
219 #define alpha_elf_hash_table(p) \
220 ((struct alpha_elf_link_hash_table *) ((p)->hash))
222 /* Get the object's symbols as our own entry type. */
224 #define alpha_elf_sym_hashes(abfd) \
225 ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
227 /* Should we do dynamic things to this symbol? */
229 static int
230 alpha_elf_dynamic_symbol_p (h, info)
231 struct elf_link_hash_entry *h;
232 struct bfd_link_info *info;
234 if (h == NULL)
235 return false;
237 while (h->root.type == bfd_link_hash_indirect
238 || h->root.type == bfd_link_hash_warning)
239 h = (struct elf_link_hash_entry *) h->root.u.i.link;
241 if (h->dynindx == -1)
242 return false;
244 if (h->root.type == bfd_link_hash_undefweak
245 || h->root.type == bfd_link_hash_defweak)
246 return true;
248 switch (ELF_ST_VISIBILITY (h->other))
250 case STV_DEFAULT:
251 break;
252 case STV_HIDDEN:
253 case STV_INTERNAL:
254 return false;
255 case STV_PROTECTED:
256 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
257 return false;
258 break;
261 if ((info->shared && !info->symbolic)
262 || ((h->elf_link_hash_flags
263 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))
264 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
265 return true;
267 return false;
270 /* Create an entry in a Alpha ELF linker hash table. */
272 static struct bfd_hash_entry *
273 elf64_alpha_link_hash_newfunc (entry, table, string)
274 struct bfd_hash_entry *entry;
275 struct bfd_hash_table *table;
276 const char *string;
278 struct alpha_elf_link_hash_entry *ret =
279 (struct alpha_elf_link_hash_entry *) entry;
281 /* Allocate the structure if it has not already been allocated by a
282 subclass. */
283 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
284 ret = ((struct alpha_elf_link_hash_entry *)
285 bfd_hash_allocate (table,
286 sizeof (struct alpha_elf_link_hash_entry)));
287 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
288 return (struct bfd_hash_entry *) ret;
290 /* Call the allocation method of the superclass. */
291 ret = ((struct alpha_elf_link_hash_entry *)
292 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
293 table, string));
294 if (ret != (struct alpha_elf_link_hash_entry *) NULL)
296 /* Set local fields. */
297 memset (&ret->esym, 0, sizeof (EXTR));
298 /* We use -2 as a marker to indicate that the information has
299 not been set. -1 means there is no associated ifd. */
300 ret->esym.ifd = -2;
301 ret->flags = 0;
302 ret->got_entries = NULL;
303 ret->reloc_entries = NULL;
306 return (struct bfd_hash_entry *) ret;
309 /* Create a Alpha ELF linker hash table. */
311 static struct bfd_link_hash_table *
312 elf64_alpha_bfd_link_hash_table_create (abfd)
313 bfd *abfd;
315 struct alpha_elf_link_hash_table *ret;
317 ret = ((struct alpha_elf_link_hash_table *)
318 bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
319 if (ret == (struct alpha_elf_link_hash_table *) NULL)
320 return NULL;
322 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
323 elf64_alpha_link_hash_newfunc))
325 bfd_release (abfd, ret);
326 return NULL;
329 return &ret->root.root;
332 /* We have some private fields hanging off of the elf_tdata structure. */
334 struct alpha_elf_obj_tdata
336 struct elf_obj_tdata root;
338 /* For every input file, these are the got entries for that object's
339 local symbols. */
340 struct alpha_elf_got_entry ** local_got_entries;
342 /* For every input file, this is the object that owns the got that
343 this input file uses. */
344 bfd *gotobj;
346 /* For every got, this is a linked list through the objects using this got */
347 bfd *in_got_link_next;
349 /* For every got, this is a link to the next got subsegment. */
350 bfd *got_link_next;
352 /* For every got, this is the section. */
353 asection *got;
355 /* For every got, this is it's total number of *entries*. */
356 int total_got_entries;
358 /* For every got, this is the sum of the number of *entries* required
359 to hold all of the member object's local got. */
360 int n_local_got_entries;
363 #define alpha_elf_tdata(abfd) \
364 ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
366 static boolean
367 elf64_alpha_mkobject (abfd)
368 bfd *abfd;
370 abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
371 if (abfd->tdata.any == NULL)
372 return false;
373 return true;
376 static boolean
377 elf64_alpha_object_p (abfd)
378 bfd *abfd;
380 /* Allocate our special target data. */
381 struct alpha_elf_obj_tdata *new_tdata;
382 new_tdata = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
383 if (new_tdata == NULL)
384 return false;
385 new_tdata->root = *abfd->tdata.elf_obj_data;
386 abfd->tdata.any = new_tdata;
388 /* Set the right machine number for an Alpha ELF file. */
389 return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
392 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
393 from smaller values. Start with zero, widen, *then* decrement. */
394 #define MINUS_ONE (((bfd_vma)0) - 1)
396 static reloc_howto_type elf64_alpha_howto_table[] =
398 HOWTO (R_ALPHA_NONE, /* type */
399 0, /* rightshift */
400 0, /* size (0 = byte, 1 = short, 2 = long) */
401 8, /* bitsize */
402 true, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_dont, /* complain_on_overflow */
405 elf64_alpha_reloc_nil, /* special_function */
406 "NONE", /* name */
407 false, /* partial_inplace */
408 0, /* src_mask */
409 0, /* dst_mask */
410 true), /* pcrel_offset */
412 /* A 32 bit reference to a symbol. */
413 HOWTO (R_ALPHA_REFLONG, /* type */
414 0, /* rightshift */
415 2, /* size (0 = byte, 1 = short, 2 = long) */
416 32, /* bitsize */
417 false, /* pc_relative */
418 0, /* bitpos */
419 complain_overflow_bitfield, /* complain_on_overflow */
420 0, /* special_function */
421 "REFLONG", /* name */
422 false, /* partial_inplace */
423 0xffffffff, /* src_mask */
424 0xffffffff, /* dst_mask */
425 false), /* pcrel_offset */
427 /* A 64 bit reference to a symbol. */
428 HOWTO (R_ALPHA_REFQUAD, /* type */
429 0, /* rightshift */
430 4, /* size (0 = byte, 1 = short, 2 = long) */
431 64, /* bitsize */
432 false, /* pc_relative */
433 0, /* bitpos */
434 complain_overflow_bitfield, /* complain_on_overflow */
435 0, /* special_function */
436 "REFQUAD", /* name */
437 false, /* partial_inplace */
438 MINUS_ONE, /* src_mask */
439 MINUS_ONE, /* dst_mask */
440 false), /* pcrel_offset */
442 /* A 32 bit GP relative offset. This is just like REFLONG except
443 that when the value is used the value of the gp register will be
444 added in. */
445 HOWTO (R_ALPHA_GPREL32, /* type */
446 0, /* rightshift */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
448 32, /* bitsize */
449 false, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_bitfield, /* complain_on_overflow */
452 0, /* special_function */
453 "GPREL32", /* name */
454 false, /* partial_inplace */
455 0xffffffff, /* src_mask */
456 0xffffffff, /* dst_mask */
457 false), /* pcrel_offset */
459 /* Used for an instruction that refers to memory off the GP register. */
460 HOWTO (R_ALPHA_LITERAL, /* type */
461 0, /* rightshift */
462 2, /* size (0 = byte, 1 = short, 2 = long) */
463 16, /* bitsize */
464 false, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_signed, /* complain_on_overflow */
467 0, /* special_function */
468 "ELF_LITERAL", /* name */
469 false, /* partial_inplace */
470 0xffff, /* src_mask */
471 0xffff, /* dst_mask */
472 false), /* pcrel_offset */
474 /* This reloc only appears immediately following an ELF_LITERAL reloc.
475 It identifies a use of the literal. The symbol index is special:
476 1 means the literal address is in the base register of a memory
477 format instruction; 2 means the literal address is in the byte
478 offset register of a byte-manipulation instruction; 3 means the
479 literal address is in the target register of a jsr instruction.
480 This does not actually do any relocation. */
481 HOWTO (R_ALPHA_LITUSE, /* type */
482 0, /* rightshift */
483 2, /* size (0 = byte, 1 = short, 2 = long) */
484 32, /* bitsize */
485 false, /* pc_relative */
486 0, /* bitpos */
487 complain_overflow_dont, /* complain_on_overflow */
488 elf64_alpha_reloc_nil, /* special_function */
489 "LITUSE", /* name */
490 false, /* partial_inplace */
491 0, /* src_mask */
492 0, /* dst_mask */
493 false), /* pcrel_offset */
495 /* Load the gp register. This is always used for a ldah instruction
496 which loads the upper 16 bits of the gp register. The symbol
497 index of the GPDISP instruction is an offset in bytes to the lda
498 instruction that loads the lower 16 bits. The value to use for
499 the relocation is the difference between the GP value and the
500 current location; the load will always be done against a register
501 holding the current address.
503 NOTE: Unlike ECOFF, partial in-place relocation is not done. If
504 any offset is present in the instructions, it is an offset from
505 the register to the ldah instruction. This lets us avoid any
506 stupid hackery like inventing a gp value to do partial relocation
507 against. Also unlike ECOFF, we do the whole relocation off of
508 the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
509 space consuming bit, that, since all the information was present
510 in the GPDISP_HI16 reloc. */
511 HOWTO (R_ALPHA_GPDISP, /* type */
512 16, /* rightshift */
513 2, /* size (0 = byte, 1 = short, 2 = long) */
514 16, /* bitsize */
515 false, /* pc_relative */
516 0, /* bitpos */
517 complain_overflow_dont, /* complain_on_overflow */
518 elf64_alpha_reloc_gpdisp, /* special_function */
519 "GPDISP", /* name */
520 false, /* partial_inplace */
521 0xffff, /* src_mask */
522 0xffff, /* dst_mask */
523 true), /* pcrel_offset */
525 /* A 21 bit branch. */
526 HOWTO (R_ALPHA_BRADDR, /* type */
527 2, /* rightshift */
528 2, /* size (0 = byte, 1 = short, 2 = long) */
529 21, /* bitsize */
530 true, /* pc_relative */
531 0, /* bitpos */
532 complain_overflow_signed, /* complain_on_overflow */
533 0, /* special_function */
534 "BRADDR", /* name */
535 false, /* partial_inplace */
536 0x1fffff, /* src_mask */
537 0x1fffff, /* dst_mask */
538 true), /* pcrel_offset */
540 /* A hint for a jump to a register. */
541 HOWTO (R_ALPHA_HINT, /* type */
542 2, /* rightshift */
543 2, /* size (0 = byte, 1 = short, 2 = long) */
544 14, /* bitsize */
545 true, /* pc_relative */
546 0, /* bitpos */
547 complain_overflow_dont, /* complain_on_overflow */
548 0, /* special_function */
549 "HINT", /* name */
550 false, /* partial_inplace */
551 0x3fff, /* src_mask */
552 0x3fff, /* dst_mask */
553 true), /* pcrel_offset */
555 /* 16 bit PC relative offset. */
556 HOWTO (R_ALPHA_SREL16, /* type */
557 0, /* rightshift */
558 1, /* size (0 = byte, 1 = short, 2 = long) */
559 16, /* bitsize */
560 true, /* pc_relative */
561 0, /* bitpos */
562 complain_overflow_signed, /* complain_on_overflow */
563 0, /* special_function */
564 "SREL16", /* name */
565 false, /* partial_inplace */
566 0xffff, /* src_mask */
567 0xffff, /* dst_mask */
568 false), /* pcrel_offset */
570 /* 32 bit PC relative offset. */
571 HOWTO (R_ALPHA_SREL32, /* type */
572 0, /* rightshift */
573 2, /* size (0 = byte, 1 = short, 2 = long) */
574 32, /* bitsize */
575 true, /* pc_relative */
576 0, /* bitpos */
577 complain_overflow_signed, /* complain_on_overflow */
578 0, /* special_function */
579 "SREL32", /* name */
580 false, /* partial_inplace */
581 0xffffffff, /* src_mask */
582 0xffffffff, /* dst_mask */
583 false), /* pcrel_offset */
585 /* A 64 bit PC relative offset. */
586 HOWTO (R_ALPHA_SREL64, /* type */
587 0, /* rightshift */
588 4, /* size (0 = byte, 1 = short, 2 = long) */
589 64, /* bitsize */
590 true, /* pc_relative */
591 0, /* bitpos */
592 complain_overflow_signed, /* complain_on_overflow */
593 0, /* special_function */
594 "SREL64", /* name */
595 false, /* partial_inplace */
596 MINUS_ONE, /* src_mask */
597 MINUS_ONE, /* dst_mask */
598 false), /* pcrel_offset */
600 /* Push a value on the reloc evaluation stack. */
601 /* Not implemented -- it's dumb. */
602 HOWTO (R_ALPHA_OP_PUSH, /* type */
603 0, /* rightshift */
604 0, /* size (0 = byte, 1 = short, 2 = long) */
605 0, /* bitsize */
606 false, /* pc_relative */
607 0, /* bitpos */
608 complain_overflow_dont, /* complain_on_overflow */
609 elf64_alpha_reloc_bad, /* special_function */
610 "OP_PUSH", /* name */
611 false, /* partial_inplace */
612 0, /* src_mask */
613 0, /* dst_mask */
614 false), /* pcrel_offset */
616 /* Store the value from the stack at the given address. Store it in
617 a bitfield of size r_size starting at bit position r_offset. */
618 /* Not implemented -- it's dumb. */
619 HOWTO (R_ALPHA_OP_STORE, /* type */
620 0, /* rightshift */
621 4, /* size (0 = byte, 1 = short, 2 = long) */
622 64, /* bitsize */
623 false, /* pc_relative */
624 0, /* bitpos */
625 complain_overflow_dont, /* complain_on_overflow */
626 elf64_alpha_reloc_bad, /* special_function */
627 "OP_STORE", /* name */
628 false, /* partial_inplace */
629 0, /* src_mask */
630 MINUS_ONE, /* dst_mask */
631 false), /* pcrel_offset */
633 /* Subtract the reloc address from the value on the top of the
634 relocation stack. */
635 /* Not implemented -- it's dumb. */
636 HOWTO (R_ALPHA_OP_PSUB, /* type */
637 0, /* rightshift */
638 0, /* size (0 = byte, 1 = short, 2 = long) */
639 0, /* bitsize */
640 false, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_dont, /* complain_on_overflow */
643 elf64_alpha_reloc_bad, /* special_function */
644 "OP_PSUB", /* name */
645 false, /* partial_inplace */
646 0, /* src_mask */
647 0, /* dst_mask */
648 false), /* pcrel_offset */
650 /* Shift the value on the top of the relocation stack right by the
651 given value. */
652 /* Not implemented -- it's dumb. */
653 HOWTO (R_ALPHA_OP_PRSHIFT, /* type */
654 0, /* rightshift */
655 0, /* size (0 = byte, 1 = short, 2 = long) */
656 0, /* bitsize */
657 false, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_dont, /* complain_on_overflow */
660 elf64_alpha_reloc_bad, /* special_function */
661 "OP_PRSHIFT", /* name */
662 false, /* partial_inplace */
663 0, /* src_mask */
664 0, /* dst_mask */
665 false), /* pcrel_offset */
667 /* Change the value of GP used by +r_addend until the next GPVALUE or the
668 end of the input bfd. */
669 /* Not implemented -- it's dumb. */
670 HOWTO (R_ALPHA_GPVALUE,
671 0, /* rightshift */
672 0, /* size (0 = byte, 1 = short, 2 = long) */
673 0, /* bitsize */
674 false, /* pc_relative */
675 0, /* bitpos */
676 complain_overflow_dont, /* complain_on_overflow */
677 elf64_alpha_reloc_bad, /* special_function */
678 "GPVALUE", /* name */
679 false, /* partial_inplace */
680 0, /* src_mask */
681 0, /* dst_mask */
682 false), /* pcrel_offset */
684 /* The high 16 bits of the displacement from GP to the target. */
685 HOWTO (R_ALPHA_GPRELHIGH,
686 0, /* rightshift */
687 2, /* size (0 = byte, 1 = short, 2 = long) */
688 16, /* bitsize */
689 false, /* pc_relative */
690 0, /* bitpos */
691 complain_overflow_signed, /* complain_on_overflow */
692 elf64_alpha_reloc_bad, /* special_function */
693 "GPRELHIGH", /* name */
694 false, /* partial_inplace */
695 0xffff, /* src_mask */
696 0xffff, /* dst_mask */
697 false), /* pcrel_offset */
699 /* The low 16 bits of the displacement from GP to the target. */
700 HOWTO (R_ALPHA_GPRELLOW,
701 0, /* rightshift */
702 2, /* size (0 = byte, 1 = short, 2 = long) */
703 16, /* bitsize */
704 false, /* pc_relative */
705 0, /* bitpos */
706 complain_overflow_dont, /* complain_on_overflow */
707 elf64_alpha_reloc_bad, /* special_function */
708 "GPRELLOW", /* name */
709 false, /* partial_inplace */
710 0xffff, /* src_mask */
711 0xffff, /* dst_mask */
712 false), /* pcrel_offset */
714 /* A 16-bit displacement from the GP to the target. */
715 /* XXX: Not implemented. */
716 HOWTO (R_ALPHA_IMMED_GP_16,
717 0, /* rightshift */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
720 false, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_signed, /* complain_on_overflow */
723 0, /* special_function */
724 "IMMED_GP_16", /* name */
725 false, /* partial_inplace */
726 0xffff, /* src_mask */
727 0xffff, /* dst_mask */
728 false), /* pcrel_offset */
730 /* The high bits of a 32-bit displacement from the GP to the target; the
731 low bits are supplied in the subsequent R_ALPHA_IMMED_LO32 relocs. */
732 /* XXX: Not implemented. */
733 HOWTO (R_ALPHA_IMMED_GP_HI32,
734 0, /* rightshift */
735 0, /* size (0 = byte, 1 = short, 2 = long) */
736 0, /* bitsize */
737 false, /* pc_relative */
738 0, /* bitpos */
739 complain_overflow_dont, /* complain_on_overflow */
740 elf64_alpha_reloc_bad, /* special_function */
741 "IMMED_GP_HI32", /* name */
742 false, /* partial_inplace */
743 0, /* src_mask */
744 0, /* dst_mask */
745 false), /* pcrel_offset */
747 /* The high bits of a 32-bit displacement to the starting address of the
748 current section (the relocation target is ignored); the low bits are
749 supplied in the subsequent R_ALPHA_IMMED_LO32 relocs. */
750 /* XXX: Not implemented. */
751 HOWTO (R_ALPHA_IMMED_SCN_HI32,
752 0, /* rightshift */
753 0, /* size (0 = byte, 1 = short, 2 = long) */
754 0, /* bitsize */
755 false, /* pc_relative */
756 0, /* bitpos */
757 complain_overflow_dont, /* complain_on_overflow */
758 elf64_alpha_reloc_bad, /* special_function */
759 "IMMED_SCN_HI32", /* name */
760 false, /* partial_inplace */
761 0, /* src_mask */
762 0, /* dst_mask */
763 false), /* pcrel_offset */
765 /* The high bits of a 32-bit displacement from the previous br, bsr, jsr
766 or jmp insn (as tagged by a BRADDR or HINT reloc) to the target; the
767 low bits are supplied by subsequent R_ALPHA_IMMED_LO32 relocs. */
768 /* XXX: Not implemented. */
769 HOWTO (R_ALPHA_IMMED_BR_HI32,
770 0, /* rightshift */
771 0, /* size (0 = byte, 1 = short, 2 = long) */
772 0, /* bitsize */
773 false, /* pc_relative */
774 0, /* bitpos */
775 complain_overflow_dont, /* complain_on_overflow */
776 elf64_alpha_reloc_bad, /* special_function */
777 "IMMED_BR_HI32", /* name */
778 false, /* partial_inplace */
779 0, /* src_mask */
780 0, /* dst_mask */
781 false), /* pcrel_offset */
783 /* The low 16 bits of a displacement calculated in a previous HI32 reloc. */
784 /* XXX: Not implemented. */
785 HOWTO (R_ALPHA_IMMED_LO32,
786 0, /* rightshift */
787 0, /* size (0 = byte, 1 = short, 2 = long) */
788 0, /* bitsize */
789 false, /* pc_relative */
790 0, /* bitpos */
791 complain_overflow_dont, /* complain_on_overflow */
792 elf64_alpha_reloc_bad, /* special_function */
793 "IMMED_LO32", /* name */
794 false, /* partial_inplace */
795 0, /* src_mask */
796 0, /* dst_mask */
797 false), /* pcrel_offset */
799 /* Misc ELF relocations. */
801 /* A dynamic relocation to copy the target into our .dynbss section. */
802 /* Not generated, as all Alpha objects use PIC, so it is not needed. It
803 is present because every other ELF has one, but should not be used
804 because .dynbss is an ugly thing. */
805 HOWTO (R_ALPHA_COPY,
809 false,
811 complain_overflow_dont,
812 bfd_elf_generic_reloc,
813 "COPY",
814 false,
817 true),
819 /* A dynamic relocation for a .got entry. */
820 HOWTO (R_ALPHA_GLOB_DAT,
824 false,
826 complain_overflow_dont,
827 bfd_elf_generic_reloc,
828 "GLOB_DAT",
829 false,
832 true),
834 /* A dynamic relocation for a .plt entry. */
835 HOWTO (R_ALPHA_JMP_SLOT,
839 false,
841 complain_overflow_dont,
842 bfd_elf_generic_reloc,
843 "JMP_SLOT",
844 false,
847 true),
849 /* A dynamic relocation to add the base of the DSO to a 64-bit field. */
850 HOWTO (R_ALPHA_RELATIVE,
854 false,
856 complain_overflow_dont,
857 bfd_elf_generic_reloc,
858 "RELATIVE",
859 false,
862 true)
865 /* A relocation function which doesn't do anything. */
867 static bfd_reloc_status_type
868 elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
869 bfd *abfd;
870 arelent *reloc;
871 asymbol *sym;
872 PTR data;
873 asection *sec;
874 bfd *output_bfd;
875 char **error_message;
877 if (output_bfd)
878 reloc->address += sec->output_offset;
879 return bfd_reloc_ok;
882 /* A relocation function used for an unsupported reloc. */
884 static bfd_reloc_status_type
885 elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
886 bfd *abfd;
887 arelent *reloc;
888 asymbol *sym;
889 PTR data;
890 asection *sec;
891 bfd *output_bfd;
892 char **error_message;
894 if (output_bfd)
895 reloc->address += sec->output_offset;
896 return bfd_reloc_notsupported;
899 /* Do the work of the GPDISP relocation. */
901 static bfd_reloc_status_type
902 elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
903 bfd *abfd;
904 bfd_vma gpdisp;
905 bfd_byte *p_ldah;
906 bfd_byte *p_lda;
908 bfd_reloc_status_type ret = bfd_reloc_ok;
909 bfd_vma addend;
910 unsigned long i_ldah, i_lda;
912 i_ldah = bfd_get_32 (abfd, p_ldah);
913 i_lda = bfd_get_32 (abfd, p_lda);
915 /* Complain if the instructions are not correct. */
916 if (((i_ldah >> 26) & 0x3f) != 0x09
917 || ((i_lda >> 26) & 0x3f) != 0x08)
918 ret = bfd_reloc_dangerous;
920 /* Extract the user-supplied offset, mirroring the sign extensions
921 that the instructions perform. */
922 addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
923 addend = (addend ^ 0x80008000) - 0x80008000;
925 gpdisp += addend;
927 if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
928 || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
929 ret = bfd_reloc_overflow;
931 /* compensate for the sign extension again. */
932 i_ldah = ((i_ldah & 0xffff0000)
933 | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
934 i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
936 bfd_put_32 (abfd, i_ldah, p_ldah);
937 bfd_put_32 (abfd, i_lda, p_lda);
939 return ret;
942 /* The special function for the GPDISP reloc. */
944 static bfd_reloc_status_type
945 elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
946 output_bfd, err_msg)
947 bfd *abfd;
948 arelent *reloc_entry;
949 asymbol *sym;
950 PTR data;
951 asection *input_section;
952 bfd *output_bfd;
953 char **err_msg;
955 bfd_reloc_status_type ret;
956 bfd_vma gp, relocation;
957 bfd_byte *p_ldah, *p_lda;
959 /* Don't do anything if we're not doing a final link. */
960 if (output_bfd)
962 reloc_entry->address += input_section->output_offset;
963 return bfd_reloc_ok;
966 if (reloc_entry->address > input_section->_cooked_size ||
967 reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
968 return bfd_reloc_outofrange;
970 /* The gp used in the portion of the output object to which this
971 input object belongs is cached on the input bfd. */
972 gp = _bfd_get_gp_value (abfd);
974 relocation = (input_section->output_section->vma
975 + input_section->output_offset
976 + reloc_entry->address);
978 p_ldah = (bfd_byte *) data + reloc_entry->address;
979 p_lda = p_ldah + reloc_entry->addend;
981 ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
983 /* Complain if the instructions are not correct. */
984 if (ret == bfd_reloc_dangerous)
985 *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
987 return ret;
990 /* A mapping from BFD reloc types to Alpha ELF reloc types. */
992 struct elf_reloc_map
994 bfd_reloc_code_real_type bfd_reloc_val;
995 int elf_reloc_val;
998 static const struct elf_reloc_map elf64_alpha_reloc_map[] =
1000 {BFD_RELOC_NONE, R_ALPHA_NONE},
1001 {BFD_RELOC_32, R_ALPHA_REFLONG},
1002 {BFD_RELOC_64, R_ALPHA_REFQUAD},
1003 {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
1004 {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
1005 {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
1006 {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
1007 {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
1008 {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
1009 {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
1010 {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
1011 {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
1012 {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
1014 /* The BFD_RELOC_ALPHA_USER_* relocations are used by the assembler to process
1015 the explicit !<reloc>!sequence relocations, and are mapped into the normal
1016 relocations at the end of processing. */
1017 {BFD_RELOC_ALPHA_USER_LITERAL, R_ALPHA_LITERAL},
1018 {BFD_RELOC_ALPHA_USER_LITUSE_BASE, R_ALPHA_LITUSE},
1019 {BFD_RELOC_ALPHA_USER_LITUSE_BYTOFF, R_ALPHA_LITUSE},
1020 {BFD_RELOC_ALPHA_USER_LITUSE_JSR, R_ALPHA_LITUSE},
1021 {BFD_RELOC_ALPHA_USER_GPDISP, R_ALPHA_GPDISP},
1022 {BFD_RELOC_ALPHA_USER_GPRELHIGH, R_ALPHA_GPRELHIGH},
1023 {BFD_RELOC_ALPHA_USER_GPRELLOW, R_ALPHA_GPRELLOW},
1026 /* Given a BFD reloc type, return a HOWTO structure. */
1028 static reloc_howto_type *
1029 elf64_alpha_bfd_reloc_type_lookup (abfd, code)
1030 bfd *abfd;
1031 bfd_reloc_code_real_type code;
1033 const struct elf_reloc_map *i, *e;
1034 i = e = elf64_alpha_reloc_map;
1035 e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
1036 for (; i != e; ++i)
1038 if (i->bfd_reloc_val == code)
1039 return &elf64_alpha_howto_table[i->elf_reloc_val];
1041 return 0;
1044 /* Given an Alpha ELF reloc type, fill in an arelent structure. */
1046 static void
1047 elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
1048 bfd *abfd;
1049 arelent *cache_ptr;
1050 Elf64_Internal_Rela *dst;
1052 unsigned r_type;
1054 r_type = ELF64_R_TYPE(dst->r_info);
1055 BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1056 cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1059 /* These functions do relaxation for Alpha ELF.
1061 Currently I'm only handling what I can do with existing compiler
1062 and assembler support, which means no instructions are removed,
1063 though some may be nopped. At this time GCC does not emit enough
1064 information to do all of the relaxing that is possible. It will
1065 take some not small amount of work for that to happen.
1067 There are a couple of interesting papers that I once read on this
1068 subject, that I cannot find references to at the moment, that
1069 related to Alpha in particular. They are by David Wall, then of
1070 DEC WRL. */
1072 #define OP_LDA 0x08
1073 #define OP_LDAH 0x09
1074 #define INSN_JSR 0x68004000
1075 #define INSN_JSR_MASK 0xfc00c000
1076 #define OP_LDQ 0x29
1077 #define OP_BR 0x30
1078 #define OP_BSR 0x34
1079 #define INSN_UNOP 0x2fe00000
1081 struct alpha_relax_info
1083 bfd *abfd;
1084 asection *sec;
1085 bfd_byte *contents;
1086 Elf_Internal_Rela *relocs, *relend;
1087 struct bfd_link_info *link_info;
1088 boolean changed_contents;
1089 boolean changed_relocs;
1090 bfd_vma gp;
1091 bfd *gotobj;
1092 asection *tsec;
1093 struct alpha_elf_link_hash_entry *h;
1094 struct alpha_elf_got_entry *gotent;
1095 unsigned char other;
1098 static Elf_Internal_Rela * elf64_alpha_relax_with_lituse
1099 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1100 Elf_Internal_Rela *irel, Elf_Internal_Rela *irelend));
1102 static boolean elf64_alpha_relax_without_lituse
1103 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1104 Elf_Internal_Rela *irel));
1106 static bfd_vma elf64_alpha_relax_opt_call
1107 PARAMS((struct alpha_relax_info *info, bfd_vma symval));
1109 static boolean elf64_alpha_relax_section
1110 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
1111 boolean *again));
1113 static Elf_Internal_Rela *
1114 elf64_alpha_find_reloc_at_ofs (rel, relend, offset, type)
1115 Elf_Internal_Rela *rel, *relend;
1116 bfd_vma offset;
1117 int type;
1119 while (rel < relend)
1121 if (rel->r_offset == offset && ELF64_R_TYPE (rel->r_info) == type)
1122 return rel;
1123 ++rel;
1125 return NULL;
1128 static Elf_Internal_Rela *
1129 elf64_alpha_relax_with_lituse (info, symval, irel, irelend)
1130 struct alpha_relax_info *info;
1131 bfd_vma symval;
1132 Elf_Internal_Rela *irel, *irelend;
1134 Elf_Internal_Rela *urel;
1135 int flags, count, i;
1136 bfd_signed_vma disp;
1137 boolean fits16;
1138 boolean fits32;
1139 boolean lit_reused = false;
1140 boolean all_optimized = true;
1141 unsigned int lit_insn;
1143 lit_insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1144 if (lit_insn >> 26 != OP_LDQ)
1146 ((*_bfd_error_handler)
1147 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1148 bfd_get_filename (info->abfd), info->sec->name,
1149 (unsigned long)irel->r_offset));
1150 return irel;
1153 /* Summarize how this particular LITERAL is used. */
1154 for (urel = irel+1, flags = count = 0; urel < irelend; ++urel, ++count)
1156 if (ELF64_R_TYPE (urel->r_info) != R_ALPHA_LITUSE)
1157 break;
1158 if (urel->r_addend >= 0 && urel->r_addend <= 3)
1159 flags |= 1 << urel->r_addend;
1162 /* A little preparation for the loop... */
1163 disp = symval - info->gp;
1165 for (urel = irel+1, i = 0; i < count; ++i, ++urel)
1167 unsigned int insn;
1168 int insn_disp;
1169 bfd_signed_vma xdisp;
1171 insn = bfd_get_32 (info->abfd, info->contents + urel->r_offset);
1173 switch (urel->r_addend)
1175 default: /* 0 = ADDRESS FORMAT */
1176 /* This type is really just a placeholder to note that all
1177 uses cannot be optimized, but to still allow some. */
1178 all_optimized = false;
1179 break;
1181 case 1: /* MEM FORMAT */
1182 /* We can always optimize 16-bit displacements. */
1184 /* Extract the displacement from the instruction, sign-extending
1185 it if necessary, then test whether it is within 16 or 32 bits
1186 displacement from GP. */
1187 insn_disp = insn & 0x0000ffff;
1188 if (insn_disp & 0x00008000)
1189 insn_disp |= 0xffff0000; /* Negative: sign-extend. */
1191 xdisp = disp + insn_disp;
1192 fits16 = (xdisp >= - (bfd_signed_vma) 0x00008000 && xdisp < 0x00008000);
1193 fits32 = (xdisp >= - (bfd_signed_vma) 0x80000000 && xdisp < 0x7fff8000);
1195 if (fits16)
1197 /* Take the op code and dest from this insn, take the base
1198 register from the literal insn. Leave the offset alone. */
1199 insn = (insn & 0xffe0ffff) | (lit_insn & 0x001f0000);
1200 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1201 R_ALPHA_GPRELLOW);
1202 urel->r_addend = irel->r_addend;
1203 info->changed_relocs = true;
1205 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1206 info->changed_contents = true;
1209 /* If all mem+byte, we can optimize 32-bit mem displacements. */
1210 else if (fits32 && !(flags & ~6))
1212 /* FIXME: sanity check that lit insn Ra is mem insn Rb. */
1214 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1215 R_ALPHA_GPRELHIGH);
1216 lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
1217 bfd_put_32 (info->abfd, lit_insn,
1218 info->contents + irel->r_offset);
1219 lit_reused = true;
1220 info->changed_contents = true;
1222 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1223 R_ALPHA_GPRELLOW);
1224 urel->r_addend = irel->r_addend;
1225 info->changed_relocs = true;
1227 else
1228 all_optimized = false;
1229 break;
1231 case 2: /* BYTE OFFSET FORMAT */
1232 /* We can always optimize byte instructions. */
1234 /* FIXME: sanity check the insn for byte op. Check that the
1235 literal dest reg is indeed Rb in the byte insn. */
1237 insn = (insn & ~0x001ff000) | ((symval & 7) << 13) | 0x1000;
1239 urel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1240 urel->r_addend = 0;
1241 info->changed_relocs = true;
1243 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1244 info->changed_contents = true;
1245 break;
1247 case 3: /* CALL FORMAT */
1249 /* If not zero, place to jump without needing pv. */
1250 bfd_vma optdest = elf64_alpha_relax_opt_call (info, symval);
1251 bfd_vma org = (info->sec->output_section->vma
1252 + info->sec->output_offset
1253 + urel->r_offset + 4);
1254 bfd_signed_vma odisp;
1256 odisp = (optdest ? optdest : symval) - org;
1257 if (odisp >= -0x400000 && odisp < 0x400000)
1259 Elf_Internal_Rela *xrel;
1261 /* Preserve branch prediction call stack when possible. */
1262 if ((insn & INSN_JSR_MASK) == INSN_JSR)
1263 insn = (OP_BSR << 26) | (insn & 0x03e00000);
1264 else
1265 insn = (OP_BR << 26) | (insn & 0x03e00000);
1267 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1268 R_ALPHA_BRADDR);
1269 urel->r_addend = irel->r_addend;
1271 if (optdest)
1272 urel->r_addend += optdest - symval;
1273 else
1274 all_optimized = false;
1276 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1278 /* Kill any HINT reloc that might exist for this insn. */
1279 xrel = (elf64_alpha_find_reloc_at_ofs
1280 (info->relocs, info->relend, urel->r_offset,
1281 R_ALPHA_HINT));
1282 if (xrel)
1283 xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1285 info->changed_contents = true;
1286 info->changed_relocs = true;
1288 else
1289 all_optimized = false;
1291 /* ??? If target gp == current gp we can eliminate the gp reload.
1292 This does depend on every place a gp could be reloaded will
1293 be, which currently happens for all code produced by gcc, but
1294 not necessarily by hand-coded assembly, or if sibling calls
1295 are enabled in gcc.
1297 Perhaps conditionalize this on a flag being set in the target
1298 object file's header, and have gcc set it? */
1300 break;
1304 /* If all cases were optimized, we can reduce the use count on this
1305 got entry by one, possibly eliminating it. */
1306 if (all_optimized)
1308 info->gotent->use_count -= 1;
1309 alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1310 if (!info->h)
1311 alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1313 /* If the literal instruction is no longer needed (it may have been
1314 reused. We can eliminate it.
1315 ??? For now, I don't want to deal with compacting the section,
1316 so just nop it out. */
1317 if (!lit_reused)
1319 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1320 info->changed_relocs = true;
1322 bfd_put_32 (info->abfd, INSN_UNOP, info->contents + irel->r_offset);
1323 info->changed_contents = true;
1327 return irel + count;
1330 static bfd_vma
1331 elf64_alpha_relax_opt_call (info, symval)
1332 struct alpha_relax_info *info;
1333 bfd_vma symval;
1335 /* If the function has the same gp, and we can identify that the
1336 function does not use its function pointer, we can eliminate the
1337 address load. */
1339 /* If the symbol is marked NOPV, we are being told the function never
1340 needs its procedure value. */
1341 if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_NOPV)
1342 return symval;
1344 /* If the symbol is marked STD_GP, we are being told the function does
1345 a normal ldgp in the first two words. */
1346 else if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_STD_GPLOAD)
1349 /* Otherwise, we may be able to identify a GP load in the first two
1350 words, which we can then skip. */
1351 else
1353 Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
1354 bfd_vma ofs;
1356 /* Load the relocations from the section that the target symbol is in. */
1357 if (info->sec == info->tsec)
1359 tsec_relocs = info->relocs;
1360 tsec_relend = info->relend;
1361 tsec_free = NULL;
1363 else
1365 tsec_relocs = (_bfd_elf64_link_read_relocs
1366 (info->abfd, info->tsec, (PTR) NULL,
1367 (Elf_Internal_Rela *) NULL,
1368 info->link_info->keep_memory));
1369 if (tsec_relocs == NULL)
1370 return 0;
1371 tsec_relend = tsec_relocs + info->tsec->reloc_count;
1372 tsec_free = (info->link_info->keep_memory ? NULL : tsec_relocs);
1375 /* Recover the symbol's offset within the section. */
1376 ofs = (symval - info->tsec->output_section->vma
1377 - info->tsec->output_offset);
1379 /* Look for a GPDISP reloc. */
1380 gpdisp = (elf64_alpha_find_reloc_at_ofs
1381 (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
1383 if (!gpdisp || gpdisp->r_addend != 4)
1385 if (tsec_free)
1386 free (tsec_free);
1387 return 0;
1389 if (tsec_free)
1390 free (tsec_free);
1393 /* We've now determined that we can skip an initial gp load. Verify
1394 that the call and the target use the same gp. */
1395 if (info->link_info->hash->creator != info->tsec->owner->xvec
1396 || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
1397 return 0;
1399 return symval + 8;
1402 static boolean
1403 elf64_alpha_relax_without_lituse (info, symval, irel)
1404 struct alpha_relax_info *info;
1405 bfd_vma symval;
1406 Elf_Internal_Rela *irel;
1408 unsigned int insn;
1409 bfd_signed_vma disp;
1411 /* Get the instruction. */
1412 insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1414 if (insn >> 26 != OP_LDQ)
1416 ((*_bfd_error_handler)
1417 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1418 bfd_get_filename (info->abfd), info->sec->name,
1419 (unsigned long) irel->r_offset));
1420 return true;
1423 /* So we aren't told much. Do what we can with the address load and
1424 fake the rest. All of the optimizations here require that the
1425 offset from the GP fit in 16 bits. */
1427 disp = symval - info->gp;
1428 if (disp < -0x8000 || disp >= 0x8000)
1429 return true;
1431 /* On the LITERAL instruction itself, consider exchanging
1432 `ldq R,X(gp)' for `lda R,Y(gp)'. */
1434 insn = (OP_LDA << 26) | (insn & 0x03ff0000);
1435 bfd_put_32 (info->abfd, insn, info->contents + irel->r_offset);
1436 info->changed_contents = true;
1438 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), R_ALPHA_GPRELLOW);
1439 info->changed_relocs = true;
1441 /* Reduce the use count on this got entry by one, possibly
1442 eliminating it. */
1443 info->gotent->use_count -= 1;
1444 alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1445 if (!info->h)
1446 alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1448 /* ??? Search forward through this basic block looking for insns
1449 that use the target register. Stop after an insn modifying the
1450 register is seen, or after a branch or call.
1452 Any such memory load insn may be substituted by a load directly
1453 off the GP. This allows the memory load insn to be issued before
1454 the calculated GP register would otherwise be ready.
1456 Any such jsr insn can be replaced by a bsr if it is in range.
1458 This would mean that we'd have to _add_ relocations, the pain of
1459 which gives one pause. */
1461 return true;
1464 static boolean
1465 elf64_alpha_relax_section (abfd, sec, link_info, again)
1466 bfd *abfd;
1467 asection *sec;
1468 struct bfd_link_info *link_info;
1469 boolean *again;
1471 Elf_Internal_Shdr *symtab_hdr;
1472 Elf_Internal_Rela *internal_relocs;
1473 Elf_Internal_Rela *free_relocs = NULL;
1474 Elf_Internal_Rela *irel, *irelend;
1475 bfd_byte *free_contents = NULL;
1476 Elf64_External_Sym *extsyms = NULL;
1477 Elf64_External_Sym *free_extsyms = NULL;
1478 struct alpha_elf_got_entry **local_got_entries;
1479 struct alpha_relax_info info;
1481 /* We are not currently changing any sizes, so only one pass. */
1482 *again = false;
1484 if (link_info->relocateable
1485 || (sec->flags & SEC_RELOC) == 0
1486 || sec->reloc_count == 0)
1487 return true;
1489 /* If this is the first time we have been called for this section,
1490 initialize the cooked size. */
1491 if (sec->_cooked_size == 0)
1492 sec->_cooked_size = sec->_raw_size;
1494 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1495 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1497 /* Load the relocations for this section. */
1498 internal_relocs = (_bfd_elf64_link_read_relocs
1499 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1500 link_info->keep_memory));
1501 if (internal_relocs == NULL)
1502 goto error_return;
1503 if (! link_info->keep_memory)
1504 free_relocs = internal_relocs;
1506 memset(&info, 0, sizeof (info));
1507 info.abfd = abfd;
1508 info.sec = sec;
1509 info.link_info = link_info;
1510 info.relocs = internal_relocs;
1511 info.relend = irelend = internal_relocs + sec->reloc_count;
1513 /* Find the GP for this object. */
1514 info.gotobj = alpha_elf_tdata (abfd)->gotobj;
1515 if (info.gotobj)
1517 asection *sgot = alpha_elf_tdata (info.gotobj)->got;
1518 info.gp = _bfd_get_gp_value (info.gotobj);
1519 if (info.gp == 0)
1521 info.gp = (sgot->output_section->vma
1522 + sgot->output_offset
1523 + 0x8000);
1524 _bfd_set_gp_value (info.gotobj, info.gp);
1528 for (irel = internal_relocs; irel < irelend; irel++)
1530 bfd_vma symval;
1531 Elf_Internal_Sym isym;
1532 struct alpha_elf_got_entry *gotent;
1534 if (ELF64_R_TYPE (irel->r_info) != (int) R_ALPHA_LITERAL)
1535 continue;
1537 /* Get the section contents. */
1538 if (info.contents == NULL)
1540 if (elf_section_data (sec)->this_hdr.contents != NULL)
1541 info.contents = elf_section_data (sec)->this_hdr.contents;
1542 else
1544 info.contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1545 if (info.contents == NULL)
1546 goto error_return;
1547 free_contents = info.contents;
1549 if (! bfd_get_section_contents (abfd, sec, info.contents,
1550 (file_ptr) 0, sec->_raw_size))
1551 goto error_return;
1555 /* Read this BFD's symbols if we haven't done so already. */
1556 if (extsyms == NULL)
1558 if (symtab_hdr->contents != NULL)
1559 extsyms = (Elf64_External_Sym *) symtab_hdr->contents;
1560 else
1562 extsyms = ((Elf64_External_Sym *)
1563 bfd_malloc (symtab_hdr->sh_size));
1564 if (extsyms == NULL)
1565 goto error_return;
1566 free_extsyms = extsyms;
1567 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1568 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1569 != symtab_hdr->sh_size))
1570 goto error_return;
1574 /* Get the value of the symbol referred to by the reloc. */
1575 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1577 /* A local symbol. */
1578 bfd_elf64_swap_symbol_in (abfd,
1579 extsyms + ELF64_R_SYM (irel->r_info),
1580 &isym);
1581 if (isym.st_shndx == SHN_UNDEF)
1582 info.tsec = bfd_und_section_ptr;
1583 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1584 info.tsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1585 else if (isym.st_shndx == SHN_ABS)
1586 info.tsec = bfd_abs_section_ptr;
1587 else if (isym.st_shndx == SHN_COMMON)
1588 info.tsec = bfd_com_section_ptr;
1589 else
1590 continue; /* who knows. */
1592 info.h = NULL;
1593 info.other = isym.st_other;
1594 gotent = local_got_entries[ELF64_R_SYM(irel->r_info)];
1595 symval = isym.st_value;
1597 else
1599 unsigned long indx;
1600 struct alpha_elf_link_hash_entry *h;
1602 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1603 h = alpha_elf_sym_hashes (abfd)[indx];
1604 BFD_ASSERT (h != NULL);
1606 while (h->root.root.type == bfd_link_hash_indirect
1607 || h->root.root.type == bfd_link_hash_warning)
1608 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
1610 /* We can't do anthing with undefined or dynamic symbols. */
1611 if (h->root.root.type == bfd_link_hash_undefined
1612 || h->root.root.type == bfd_link_hash_undefweak
1613 || alpha_elf_dynamic_symbol_p (&h->root, link_info))
1614 continue;
1616 info.h = h;
1617 info.gotent = gotent;
1618 info.tsec = h->root.root.u.def.section;
1619 info.other = h->root.other;
1620 gotent = h->got_entries;
1621 symval = h->root.root.u.def.value;
1624 /* Search for the got entry to be used by this relocation. */
1625 while (gotent->gotobj != info.gotobj || gotent->addend != irel->r_addend)
1626 gotent = gotent->next;
1627 info.gotent = gotent;
1629 symval += info.tsec->output_section->vma + info.tsec->output_offset;
1630 symval += irel->r_addend;
1632 BFD_ASSERT(info.gotent != NULL);
1634 /* If there exist LITUSE relocations immediately following, this
1635 opens up all sorts of interesting optimizations, because we
1636 now know every location that this address load is used. */
1638 if (irel+1 < irelend && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
1640 irel = elf64_alpha_relax_with_lituse (&info, symval, irel, irelend);
1641 if (irel == NULL)
1642 goto error_return;
1644 else
1646 if (!elf64_alpha_relax_without_lituse (&info, symval, irel))
1647 goto error_return;
1651 if (!elf64_alpha_size_got_sections (abfd, link_info))
1652 return false;
1654 if (info.changed_relocs)
1656 elf_section_data (sec)->relocs = internal_relocs;
1658 else if (free_relocs != NULL)
1660 free (free_relocs);
1663 if (info.changed_contents)
1665 elf_section_data (sec)->this_hdr.contents = info.contents;
1667 else if (free_contents != NULL)
1669 if (! link_info->keep_memory)
1670 free (free_contents);
1671 else
1673 /* Cache the section contents for elf_link_input_bfd. */
1674 elf_section_data (sec)->this_hdr.contents = info.contents;
1678 if (free_extsyms != NULL)
1680 if (! link_info->keep_memory)
1681 free (free_extsyms);
1682 else
1684 /* Cache the symbols for elf_link_input_bfd. */
1685 symtab_hdr->contents = extsyms;
1689 *again = info.changed_contents || info.changed_relocs;
1691 return true;
1693 error_return:
1694 if (free_relocs != NULL)
1695 free (free_relocs);
1696 if (free_contents != NULL)
1697 free (free_contents);
1698 if (free_extsyms != NULL)
1699 free (free_extsyms);
1700 return false;
1703 /* PLT/GOT Stuff */
1704 #define PLT_HEADER_SIZE 32
1705 #define PLT_HEADER_WORD1 0xc3600000 /* br $27,.+4 */
1706 #define PLT_HEADER_WORD2 0xa77b000c /* ldq $27,12($27) */
1707 #define PLT_HEADER_WORD3 0x47ff041f /* nop */
1708 #define PLT_HEADER_WORD4 0x6b7b0000 /* jmp $27,($27) */
1710 #define PLT_ENTRY_SIZE 12
1711 #define PLT_ENTRY_WORD1 0xc3800000 /* br $28, plt0 */
1712 #define PLT_ENTRY_WORD2 0
1713 #define PLT_ENTRY_WORD3 0
1715 #define MAX_GOT_ENTRIES (64*1024 / 8)
1717 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
1719 /* Handle an Alpha specific section when reading an object file. This
1720 is called when elfcode.h finds a section with an unknown type.
1721 FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
1722 how to. */
1724 static boolean
1725 elf64_alpha_section_from_shdr (abfd, hdr, name)
1726 bfd *abfd;
1727 Elf64_Internal_Shdr *hdr;
1728 char *name;
1730 asection *newsect;
1732 /* There ought to be a place to keep ELF backend specific flags, but
1733 at the moment there isn't one. We just keep track of the
1734 sections by their name, instead. Fortunately, the ABI gives
1735 suggested names for all the MIPS specific sections, so we will
1736 probably get away with this. */
1737 switch (hdr->sh_type)
1739 case SHT_ALPHA_DEBUG:
1740 if (strcmp (name, ".mdebug") != 0)
1741 return false;
1742 break;
1743 #ifdef ERIC_neverdef
1744 case SHT_ALPHA_REGINFO:
1745 if (strcmp (name, ".reginfo") != 0
1746 || hdr->sh_size != sizeof (Elf64_External_RegInfo))
1747 return false;
1748 break;
1749 #endif
1750 default:
1751 return false;
1754 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1755 return false;
1756 newsect = hdr->bfd_section;
1758 if (hdr->sh_type == SHT_ALPHA_DEBUG)
1760 if (! bfd_set_section_flags (abfd, newsect,
1761 (bfd_get_section_flags (abfd, newsect)
1762 | SEC_DEBUGGING)))
1763 return false;
1766 #ifdef ERIC_neverdef
1767 /* For a .reginfo section, set the gp value in the tdata information
1768 from the contents of this section. We need the gp value while
1769 processing relocs, so we just get it now. */
1770 if (hdr->sh_type == SHT_ALPHA_REGINFO)
1772 Elf64_External_RegInfo ext;
1773 Elf64_RegInfo s;
1775 if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1776 (file_ptr) 0, sizeof ext))
1777 return false;
1778 bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
1779 elf_gp (abfd) = s.ri_gp_value;
1781 #endif
1783 return true;
1786 /* Set the correct type for an Alpha ELF section. We do this by the
1787 section name, which is a hack, but ought to work. */
1789 static boolean
1790 elf64_alpha_fake_sections (abfd, hdr, sec)
1791 bfd *abfd;
1792 Elf64_Internal_Shdr *hdr;
1793 asection *sec;
1795 register const char *name;
1797 name = bfd_get_section_name (abfd, sec);
1799 if (strcmp (name, ".mdebug") == 0)
1801 hdr->sh_type = SHT_ALPHA_DEBUG;
1802 /* In a shared object on Irix 5.3, the .mdebug section has an
1803 entsize of 0. FIXME: Does this matter? */
1804 if ((abfd->flags & DYNAMIC) != 0 )
1805 hdr->sh_entsize = 0;
1806 else
1807 hdr->sh_entsize = 1;
1809 #ifdef ERIC_neverdef
1810 else if (strcmp (name, ".reginfo") == 0)
1812 hdr->sh_type = SHT_ALPHA_REGINFO;
1813 /* In a shared object on Irix 5.3, the .reginfo section has an
1814 entsize of 0x18. FIXME: Does this matter? */
1815 if ((abfd->flags & DYNAMIC) != 0)
1816 hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
1817 else
1818 hdr->sh_entsize = 1;
1820 /* Force the section size to the correct value, even if the
1821 linker thinks it is larger. The link routine below will only
1822 write out this much data for .reginfo. */
1823 hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
1825 else if (strcmp (name, ".hash") == 0
1826 || strcmp (name, ".dynamic") == 0
1827 || strcmp (name, ".dynstr") == 0)
1829 hdr->sh_entsize = 0;
1830 hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
1832 #endif
1833 else if (strcmp (name, ".sdata") == 0
1834 || strcmp (name, ".sbss") == 0
1835 || strcmp (name, ".lit4") == 0
1836 || strcmp (name, ".lit8") == 0)
1837 hdr->sh_flags |= SHF_ALPHA_GPREL;
1839 return true;
1842 /* Hook called by the linker routine which adds symbols from an object
1843 file. We use it to put .comm items in .sbss, and not .bss. */
1845 static boolean
1846 elf64_alpha_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1847 bfd *abfd;
1848 struct bfd_link_info *info;
1849 const Elf_Internal_Sym *sym;
1850 const char **namep;
1851 flagword *flagsp;
1852 asection **secp;
1853 bfd_vma *valp;
1855 if (sym->st_shndx == SHN_COMMON
1856 && !info->relocateable
1857 && sym->st_size <= bfd_get_gp_size (abfd))
1859 /* Common symbols less than or equal to -G nn bytes are
1860 automatically put into .sbss. */
1862 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1864 if (scomm == NULL)
1866 scomm = bfd_make_section (abfd, ".scommon");
1867 if (scomm == NULL
1868 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
1869 | SEC_IS_COMMON
1870 | SEC_LINKER_CREATED)))
1871 return false;
1874 *secp = scomm;
1875 *valp = sym->st_size;
1878 return true;
1881 /* Create the .got section. */
1883 static boolean
1884 elf64_alpha_create_got_section(abfd, info)
1885 bfd *abfd;
1886 struct bfd_link_info *info;
1888 asection *s;
1890 if (bfd_get_section_by_name (abfd, ".got"))
1891 return true;
1893 s = bfd_make_section (abfd, ".got");
1894 if (s == NULL
1895 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1896 | SEC_HAS_CONTENTS
1897 | SEC_IN_MEMORY
1898 | SEC_LINKER_CREATED))
1899 || !bfd_set_section_alignment (abfd, s, 3))
1900 return false;
1902 alpha_elf_tdata (abfd)->got = s;
1904 return true;
1907 /* Create all the dynamic sections. */
1909 static boolean
1910 elf64_alpha_create_dynamic_sections (abfd, info)
1911 bfd *abfd;
1912 struct bfd_link_info *info;
1914 asection *s;
1915 struct elf_link_hash_entry *h;
1917 /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
1919 s = bfd_make_section (abfd, ".plt");
1920 if (s == NULL
1921 || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1922 | SEC_HAS_CONTENTS
1923 | SEC_IN_MEMORY
1924 | SEC_LINKER_CREATED
1925 | SEC_CODE))
1926 || ! bfd_set_section_alignment (abfd, s, 3))
1927 return false;
1929 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1930 .plt section. */
1931 h = NULL;
1932 if (! (_bfd_generic_link_add_one_symbol
1933 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1934 (bfd_vma) 0, (const char *) NULL, false,
1935 get_elf_backend_data (abfd)->collect,
1936 (struct bfd_link_hash_entry **) &h)))
1937 return false;
1938 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1939 h->type = STT_OBJECT;
1941 if (info->shared
1942 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1943 return false;
1945 s = bfd_make_section (abfd, ".rela.plt");
1946 if (s == NULL
1947 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1948 | SEC_HAS_CONTENTS
1949 | SEC_IN_MEMORY
1950 | SEC_LINKER_CREATED
1951 | SEC_READONLY))
1952 || ! bfd_set_section_alignment (abfd, s, 3))
1953 return false;
1955 /* We may or may not have created a .got section for this object, but
1956 we definitely havn't done the rest of the work. */
1958 if (!elf64_alpha_create_got_section (abfd, info))
1959 return false;
1961 s = bfd_make_section(abfd, ".rela.got");
1962 if (s == NULL
1963 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1964 | SEC_HAS_CONTENTS
1965 | SEC_IN_MEMORY
1966 | SEC_LINKER_CREATED
1967 | SEC_READONLY))
1968 || !bfd_set_section_alignment (abfd, s, 3))
1969 return false;
1971 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1972 dynobj's .got section. We don't do this in the linker script
1973 because we don't want to define the symbol if we are not creating
1974 a global offset table. */
1975 h = NULL;
1976 if (!(_bfd_generic_link_add_one_symbol
1977 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
1978 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
1979 false, get_elf_backend_data (abfd)->collect,
1980 (struct bfd_link_hash_entry **) &h)))
1981 return false;
1982 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1983 h->type = STT_OBJECT;
1985 if (info->shared
1986 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1987 return false;
1989 elf_hash_table (info)->hgot = h;
1991 return true;
1994 /* Read ECOFF debugging information from a .mdebug section into a
1995 ecoff_debug_info structure. */
1997 static boolean
1998 elf64_alpha_read_ecoff_info (abfd, section, debug)
1999 bfd *abfd;
2000 asection *section;
2001 struct ecoff_debug_info *debug;
2003 HDRR *symhdr;
2004 const struct ecoff_debug_swap *swap;
2005 char *ext_hdr = NULL;
2007 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2008 memset (debug, 0, sizeof (*debug));
2010 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
2011 if (ext_hdr == NULL && swap->external_hdr_size != 0)
2012 goto error_return;
2014 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2015 swap->external_hdr_size)
2016 == false)
2017 goto error_return;
2019 symhdr = &debug->symbolic_header;
2020 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2022 /* The symbolic header contains absolute file offsets and sizes to
2023 read. */
2024 #define READ(ptr, offset, count, size, type) \
2025 if (symhdr->count == 0) \
2026 debug->ptr = NULL; \
2027 else \
2029 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2030 if (debug->ptr == NULL) \
2031 goto error_return; \
2032 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2033 || (bfd_read (debug->ptr, size, symhdr->count, \
2034 abfd) != size * symhdr->count)) \
2035 goto error_return; \
2038 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2039 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2040 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2041 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2042 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2043 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2044 union aux_ext *);
2045 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2046 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2047 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2048 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2049 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2050 #undef READ
2052 debug->fdr = NULL;
2053 debug->adjust = NULL;
2055 return true;
2057 error_return:
2058 if (ext_hdr != NULL)
2059 free (ext_hdr);
2060 if (debug->line != NULL)
2061 free (debug->line);
2062 if (debug->external_dnr != NULL)
2063 free (debug->external_dnr);
2064 if (debug->external_pdr != NULL)
2065 free (debug->external_pdr);
2066 if (debug->external_sym != NULL)
2067 free (debug->external_sym);
2068 if (debug->external_opt != NULL)
2069 free (debug->external_opt);
2070 if (debug->external_aux != NULL)
2071 free (debug->external_aux);
2072 if (debug->ss != NULL)
2073 free (debug->ss);
2074 if (debug->ssext != NULL)
2075 free (debug->ssext);
2076 if (debug->external_fdr != NULL)
2077 free (debug->external_fdr);
2078 if (debug->external_rfd != NULL)
2079 free (debug->external_rfd);
2080 if (debug->external_ext != NULL)
2081 free (debug->external_ext);
2082 return false;
2085 /* Alpha ELF local labels start with '$'. */
2087 static boolean
2088 elf64_alpha_is_local_label_name (abfd, name)
2089 bfd *abfd;
2090 const char *name;
2092 return name[0] == '$';
2095 /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
2096 routine in order to handle the ECOFF debugging information. We
2097 still call this mips_elf_find_line because of the slot
2098 find_line_info in elf_obj_tdata is declared that way. */
2100 struct mips_elf_find_line
2102 struct ecoff_debug_info d;
2103 struct ecoff_find_line i;
2106 static boolean
2107 elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2108 functionname_ptr, line_ptr)
2109 bfd *abfd;
2110 asection *section;
2111 asymbol **symbols;
2112 bfd_vma offset;
2113 const char **filename_ptr;
2114 const char **functionname_ptr;
2115 unsigned int *line_ptr;
2117 asection *msec;
2119 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2120 filename_ptr, functionname_ptr,
2121 line_ptr, 0,
2122 &elf_tdata (abfd)->dwarf2_find_line_info))
2123 return true;
2125 msec = bfd_get_section_by_name (abfd, ".mdebug");
2126 if (msec != NULL)
2128 flagword origflags;
2129 struct mips_elf_find_line *fi;
2130 const struct ecoff_debug_swap * const swap =
2131 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2133 /* If we are called during a link, alpha_elf_final_link may have
2134 cleared the SEC_HAS_CONTENTS field. We force it back on here
2135 if appropriate (which it normally will be). */
2136 origflags = msec->flags;
2137 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2138 msec->flags |= SEC_HAS_CONTENTS;
2140 fi = elf_tdata (abfd)->find_line_info;
2141 if (fi == NULL)
2143 bfd_size_type external_fdr_size;
2144 char *fraw_src;
2145 char *fraw_end;
2146 struct fdr *fdr_ptr;
2148 fi = ((struct mips_elf_find_line *)
2149 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
2150 if (fi == NULL)
2152 msec->flags = origflags;
2153 return false;
2156 if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
2158 msec->flags = origflags;
2159 return false;
2162 /* Swap in the FDR information. */
2163 fi->d.fdr = ((struct fdr *)
2164 bfd_alloc (abfd,
2165 (fi->d.symbolic_header.ifdMax *
2166 sizeof (struct fdr))));
2167 if (fi->d.fdr == NULL)
2169 msec->flags = origflags;
2170 return false;
2172 external_fdr_size = swap->external_fdr_size;
2173 fdr_ptr = fi->d.fdr;
2174 fraw_src = (char *) fi->d.external_fdr;
2175 fraw_end = (fraw_src
2176 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2177 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2178 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2180 elf_tdata (abfd)->find_line_info = fi;
2182 /* Note that we don't bother to ever free this information.
2183 find_nearest_line is either called all the time, as in
2184 objdump -l, so the information should be saved, or it is
2185 rarely called, as in ld error messages, so the memory
2186 wasted is unimportant. Still, it would probably be a
2187 good idea for free_cached_info to throw it away. */
2190 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2191 &fi->i, filename_ptr, functionname_ptr,
2192 line_ptr))
2194 msec->flags = origflags;
2195 return true;
2198 msec->flags = origflags;
2201 /* Fall back on the generic ELF find_nearest_line routine. */
2203 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2204 filename_ptr, functionname_ptr,
2205 line_ptr);
2208 /* Structure used to pass information to alpha_elf_output_extsym. */
2210 struct extsym_info
2212 bfd *abfd;
2213 struct bfd_link_info *info;
2214 struct ecoff_debug_info *debug;
2215 const struct ecoff_debug_swap *swap;
2216 boolean failed;
2219 static boolean
2220 elf64_alpha_output_extsym (h, data)
2221 struct alpha_elf_link_hash_entry *h;
2222 PTR data;
2224 struct extsym_info *einfo = (struct extsym_info *) data;
2225 boolean strip;
2226 asection *sec, *output_section;
2228 if (h->root.indx == -2)
2229 strip = false;
2230 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2231 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2232 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2233 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2234 strip = true;
2235 else if (einfo->info->strip == strip_all
2236 || (einfo->info->strip == strip_some
2237 && bfd_hash_lookup (einfo->info->keep_hash,
2238 h->root.root.root.string,
2239 false, false) == NULL))
2240 strip = true;
2241 else
2242 strip = false;
2244 if (strip)
2245 return true;
2247 if (h->esym.ifd == -2)
2249 h->esym.jmptbl = 0;
2250 h->esym.cobol_main = 0;
2251 h->esym.weakext = 0;
2252 h->esym.reserved = 0;
2253 h->esym.ifd = ifdNil;
2254 h->esym.asym.value = 0;
2255 h->esym.asym.st = stGlobal;
2257 if (h->root.root.type != bfd_link_hash_defined
2258 && h->root.root.type != bfd_link_hash_defweak)
2259 h->esym.asym.sc = scAbs;
2260 else
2262 const char *name;
2264 sec = h->root.root.u.def.section;
2265 output_section = sec->output_section;
2267 /* When making a shared library and symbol h is the one from
2268 the another shared library, OUTPUT_SECTION may be null. */
2269 if (output_section == NULL)
2270 h->esym.asym.sc = scUndefined;
2271 else
2273 name = bfd_section_name (output_section->owner, output_section);
2275 if (strcmp (name, ".text") == 0)
2276 h->esym.asym.sc = scText;
2277 else if (strcmp (name, ".data") == 0)
2278 h->esym.asym.sc = scData;
2279 else if (strcmp (name, ".sdata") == 0)
2280 h->esym.asym.sc = scSData;
2281 else if (strcmp (name, ".rodata") == 0
2282 || strcmp (name, ".rdata") == 0)
2283 h->esym.asym.sc = scRData;
2284 else if (strcmp (name, ".bss") == 0)
2285 h->esym.asym.sc = scBss;
2286 else if (strcmp (name, ".sbss") == 0)
2287 h->esym.asym.sc = scSBss;
2288 else if (strcmp (name, ".init") == 0)
2289 h->esym.asym.sc = scInit;
2290 else if (strcmp (name, ".fini") == 0)
2291 h->esym.asym.sc = scFini;
2292 else
2293 h->esym.asym.sc = scAbs;
2297 h->esym.asym.reserved = 0;
2298 h->esym.asym.index = indexNil;
2301 if (h->root.root.type == bfd_link_hash_common)
2302 h->esym.asym.value = h->root.root.u.c.size;
2303 else if (h->root.root.type == bfd_link_hash_defined
2304 || h->root.root.type == bfd_link_hash_defweak)
2306 if (h->esym.asym.sc == scCommon)
2307 h->esym.asym.sc = scBss;
2308 else if (h->esym.asym.sc == scSCommon)
2309 h->esym.asym.sc = scSBss;
2311 sec = h->root.root.u.def.section;
2312 output_section = sec->output_section;
2313 if (output_section != NULL)
2314 h->esym.asym.value = (h->root.root.u.def.value
2315 + sec->output_offset
2316 + output_section->vma);
2317 else
2318 h->esym.asym.value = 0;
2320 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2322 /* Set type and value for a symbol with a function stub. */
2323 h->esym.asym.st = stProc;
2324 sec = bfd_get_section_by_name (einfo->abfd, ".plt");
2325 if (sec == NULL)
2326 h->esym.asym.value = 0;
2327 else
2329 output_section = sec->output_section;
2330 if (output_section != NULL)
2331 h->esym.asym.value = (h->root.plt.offset
2332 + sec->output_offset
2333 + output_section->vma);
2334 else
2335 h->esym.asym.value = 0;
2337 #if 0 /* FIXME? */
2338 h->esym.ifd = 0;
2339 #endif
2342 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2343 h->root.root.root.string,
2344 &h->esym))
2346 einfo->failed = true;
2347 return false;
2350 return true;
2353 /* FIXME: Create a runtime procedure table from the .mdebug section.
2355 static boolean
2356 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
2357 PTR handle;
2358 bfd *abfd;
2359 struct bfd_link_info *info;
2360 asection *s;
2361 struct ecoff_debug_info *debug;
2364 /* Handle dynamic relocations when doing an Alpha ELF link. */
2366 static boolean
2367 elf64_alpha_check_relocs (abfd, info, sec, relocs)
2368 bfd *abfd;
2369 struct bfd_link_info *info;
2370 asection *sec;
2371 const Elf_Internal_Rela *relocs;
2373 bfd *dynobj;
2374 asection *sreloc;
2375 const char *rel_sec_name;
2376 Elf_Internal_Shdr *symtab_hdr;
2377 struct alpha_elf_link_hash_entry **sym_hashes;
2378 struct alpha_elf_got_entry **local_got_entries;
2379 const Elf_Internal_Rela *rel, *relend;
2380 int got_created;
2382 if (info->relocateable)
2383 return true;
2385 dynobj = elf_hash_table(info)->dynobj;
2386 if (dynobj == NULL)
2387 elf_hash_table(info)->dynobj = dynobj = abfd;
2389 sreloc = NULL;
2390 rel_sec_name = NULL;
2391 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2392 sym_hashes = alpha_elf_sym_hashes(abfd);
2393 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2394 got_created = 0;
2396 relend = relocs + sec->reloc_count;
2397 for (rel = relocs; rel < relend; ++rel)
2399 unsigned long r_symndx, r_type;
2400 struct alpha_elf_link_hash_entry *h;
2402 r_symndx = ELF64_R_SYM (rel->r_info);
2403 if (r_symndx < symtab_hdr->sh_info)
2404 h = NULL;
2405 else
2407 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2409 while (h->root.root.type == bfd_link_hash_indirect
2410 || h->root.root.type == bfd_link_hash_warning)
2411 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2413 h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2415 r_type = ELF64_R_TYPE (rel->r_info);
2417 switch (r_type)
2419 case R_ALPHA_LITERAL:
2421 struct alpha_elf_got_entry *gotent;
2422 int flags = 0;
2424 if (h)
2426 /* Search for and possibly create a got entry. */
2427 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2428 if (gotent->gotobj == abfd &&
2429 gotent->addend == rel->r_addend)
2430 break;
2432 if (!gotent)
2434 gotent = ((struct alpha_elf_got_entry *)
2435 bfd_alloc (abfd,
2436 sizeof (struct alpha_elf_got_entry)));
2437 if (!gotent)
2438 return false;
2440 gotent->gotobj = abfd;
2441 gotent->addend = rel->r_addend;
2442 gotent->got_offset = -1;
2443 gotent->flags = 0;
2444 gotent->use_count = 1;
2446 gotent->next = h->got_entries;
2447 h->got_entries = gotent;
2449 alpha_elf_tdata (abfd)->total_got_entries++;
2451 else
2452 gotent->use_count += 1;
2454 else
2456 /* This is a local .got entry -- record for merge. */
2457 if (!local_got_entries)
2459 size_t size;
2460 size = (symtab_hdr->sh_info
2461 * sizeof (struct alpha_elf_got_entry *));
2463 local_got_entries = ((struct alpha_elf_got_entry **)
2464 bfd_alloc (abfd, size));
2465 if (!local_got_entries)
2466 return false;
2468 memset (local_got_entries, 0, size);
2469 alpha_elf_tdata (abfd)->local_got_entries =
2470 local_got_entries;
2473 for (gotent = local_got_entries[ELF64_R_SYM(rel->r_info)];
2474 gotent != NULL && gotent->addend != rel->r_addend;
2475 gotent = gotent->next)
2476 continue;
2477 if (!gotent)
2479 gotent = ((struct alpha_elf_got_entry *)
2480 bfd_alloc (abfd,
2481 sizeof (struct alpha_elf_got_entry)));
2482 if (!gotent)
2483 return false;
2485 gotent->gotobj = abfd;
2486 gotent->addend = rel->r_addend;
2487 gotent->got_offset = -1;
2488 gotent->flags = 0;
2489 gotent->use_count = 1;
2491 gotent->next = local_got_entries[ELF64_R_SYM(rel->r_info)];
2492 local_got_entries[ELF64_R_SYM(rel->r_info)] = gotent;
2494 alpha_elf_tdata(abfd)->total_got_entries++;
2495 alpha_elf_tdata(abfd)->n_local_got_entries++;
2497 else
2498 gotent->use_count += 1;
2501 /* Remember how this literal is used from its LITUSEs.
2502 This will be important when it comes to decide if we can
2503 create a .plt entry for a function symbol. */
2504 if (rel+1 < relend
2505 && ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
2509 ++rel;
2510 if (rel->r_addend >= 1 && rel->r_addend <= 3)
2511 flags |= 1 << rel->r_addend;
2513 while (rel+1 < relend &&
2514 ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE);
2516 else
2518 /* No LITUSEs -- presumably the address is not being
2519 loaded for nothing. */
2520 flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
2523 gotent->flags |= flags;
2524 if (h)
2526 /* Make a guess as to whether a .plt entry will be needed. */
2527 if ((h->flags |= flags) == ALPHA_ELF_LINK_HASH_LU_FUNC)
2528 h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2529 else
2530 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2533 /* FALLTHRU */
2535 case R_ALPHA_GPDISP:
2536 case R_ALPHA_GPREL32:
2537 case R_ALPHA_GPRELHIGH:
2538 case R_ALPHA_GPRELLOW:
2539 /* We don't actually use the .got here, but the sections must
2540 be created before the linker maps input sections to output
2541 sections. */
2542 if (!got_created)
2544 if (!elf64_alpha_create_got_section (abfd, info))
2545 return false;
2547 /* Make sure the object's gotobj is set to itself so
2548 that we default to every object with its own .got.
2549 We'll merge .gots later once we've collected each
2550 object's info. */
2551 alpha_elf_tdata(abfd)->gotobj = abfd;
2553 got_created = 1;
2555 break;
2557 case R_ALPHA_SREL16:
2558 case R_ALPHA_SREL32:
2559 case R_ALPHA_SREL64:
2560 if (h == NULL)
2561 break;
2562 /* FALLTHRU */
2564 case R_ALPHA_REFLONG:
2565 case R_ALPHA_REFQUAD:
2566 if (rel_sec_name == NULL)
2568 rel_sec_name = (bfd_elf_string_from_elf_section
2569 (abfd, elf_elfheader(abfd)->e_shstrndx,
2570 elf_section_data(sec)->rel_hdr.sh_name));
2571 if (rel_sec_name == NULL)
2572 return false;
2574 BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
2575 && strcmp (bfd_get_section_name (abfd, sec),
2576 rel_sec_name+5) == 0);
2579 /* We need to create the section here now whether we eventually
2580 use it or not so that it gets mapped to an output section by
2581 the linker. If not used, we'll kill it in
2582 size_dynamic_sections. */
2583 if (sreloc == NULL)
2585 sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
2586 if (sreloc == NULL)
2588 sreloc = bfd_make_section (dynobj, rel_sec_name);
2589 if (sreloc == NULL
2590 || !bfd_set_section_flags (dynobj, sreloc,
2591 ((sec->flags & (SEC_ALLOC
2592 | SEC_LOAD))
2593 | SEC_HAS_CONTENTS
2594 | SEC_IN_MEMORY
2595 | SEC_LINKER_CREATED
2596 | SEC_READONLY))
2597 || !bfd_set_section_alignment (dynobj, sreloc, 3))
2598 return false;
2602 if (h)
2604 /* Since we havn't seen all of the input symbols yet, we
2605 don't know whether we'll actually need a dynamic relocation
2606 entry for this reloc. So make a record of it. Once we
2607 find out if this thing needs dynamic relocation we'll
2608 expand the relocation sections by the appropriate amount. */
2610 struct alpha_elf_reloc_entry *rent;
2612 for (rent = h->reloc_entries; rent; rent = rent->next)
2613 if (rent->rtype == r_type && rent->srel == sreloc)
2614 break;
2616 if (!rent)
2618 rent = ((struct alpha_elf_reloc_entry *)
2619 bfd_alloc (abfd,
2620 sizeof (struct alpha_elf_reloc_entry)));
2621 if (!rent)
2622 return false;
2624 rent->srel = sreloc;
2625 rent->rtype = r_type;
2626 rent->count = 1;
2628 rent->next = h->reloc_entries;
2629 h->reloc_entries = rent;
2631 else
2632 rent->count++;
2634 else if (info->shared && (sec->flags & SEC_ALLOC))
2636 /* If this is a shared library, and the section is to be
2637 loaded into memory, we need a RELATIVE reloc. */
2638 sreloc->_raw_size += sizeof (Elf64_External_Rela);
2640 break;
2644 return true;
2647 /* Adjust a symbol defined by a dynamic object and referenced by a
2648 regular object. The current definition is in some section of the
2649 dynamic object, but we're not including those sections. We have to
2650 change the definition to something the rest of the link can
2651 understand. */
2653 static boolean
2654 elf64_alpha_adjust_dynamic_symbol (info, h)
2655 struct bfd_link_info *info;
2656 struct elf_link_hash_entry *h;
2658 bfd *dynobj;
2659 asection *s;
2660 struct alpha_elf_link_hash_entry *ah;
2662 dynobj = elf_hash_table(info)->dynobj;
2663 ah = (struct alpha_elf_link_hash_entry *)h;
2665 /* Now that we've seen all of the input symbols, finalize our decision
2666 about whether this symbol should get a .plt entry. */
2668 if (h->root.type != bfd_link_hash_undefweak
2669 && alpha_elf_dynamic_symbol_p (h, info)
2670 && ((h->type == STT_FUNC
2671 && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
2672 || (h->type == STT_NOTYPE
2673 && ah->flags == ALPHA_ELF_LINK_HASH_LU_FUNC))
2674 /* Don't prevent otherwise valid programs from linking by attempting
2675 to create a new .got entry somewhere. A Correct Solution would be
2676 to add a new .got section to a new object file and let it be merged
2677 somewhere later. But for now don't bother. */
2678 && ah->got_entries)
2680 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2682 s = bfd_get_section_by_name(dynobj, ".plt");
2683 if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
2684 return false;
2686 /* The first bit of the .plt is reserved. */
2687 if (s->_raw_size == 0)
2688 s->_raw_size = PLT_HEADER_SIZE;
2690 h->plt.offset = s->_raw_size;
2691 s->_raw_size += PLT_ENTRY_SIZE;
2693 /* If this symbol is not defined in a regular file, and we are not
2694 generating a shared library, then set the symbol to the location
2695 in the .plt. This is required to make function pointers compare
2696 equal between the normal executable and the shared library. */
2697 if (! info->shared
2698 && h->root.type != bfd_link_hash_defweak)
2700 h->root.u.def.section = s;
2701 h->root.u.def.value = h->plt.offset;
2704 /* We also need a JMP_SLOT entry in the .rela.plt section. */
2705 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2706 BFD_ASSERT (s != NULL);
2707 s->_raw_size += sizeof (Elf64_External_Rela);
2709 return true;
2711 else
2712 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2714 /* If this is a weak symbol, and there is a real definition, the
2715 processor independent code will have arranged for us to see the
2716 real definition first, and we can just use the same value. */
2717 if (h->weakdef != NULL)
2719 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2720 || h->weakdef->root.type == bfd_link_hash_defweak);
2721 h->root.u.def.section = h->weakdef->root.u.def.section;
2722 h->root.u.def.value = h->weakdef->root.u.def.value;
2723 return true;
2726 /* This is a reference to a symbol defined by a dynamic object which
2727 is not a function. The Alpha, since it uses .got entries for all
2728 symbols even in regular objects, does not need the hackery of a
2729 .dynbss section and COPY dynamic relocations. */
2731 return true;
2734 /* Symbol versioning can create new symbols, and make our old symbols
2735 indirect to the new ones. Consolidate the got and reloc information
2736 in these situations. */
2738 static boolean
2739 elf64_alpha_merge_ind_symbols (hi, dummy)
2740 struct alpha_elf_link_hash_entry *hi;
2741 PTR dummy;
2743 struct alpha_elf_link_hash_entry *hs;
2745 if (hi->root.root.type != bfd_link_hash_indirect)
2746 return true;
2747 hs = hi;
2748 do {
2749 hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
2750 } while (hs->root.root.type == bfd_link_hash_indirect);
2752 /* Merge the flags. Whee. */
2754 hs->flags |= hi->flags;
2756 /* Merge the .got entries. Cannibalize the old symbol's list in
2757 doing so, since we don't need it anymore. */
2759 if (hs->got_entries == NULL)
2760 hs->got_entries = hi->got_entries;
2761 else
2763 struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
2765 gsh = hs->got_entries;
2766 for (gi = hi->got_entries; gi ; gi = gin)
2768 gin = gi->next;
2769 for (gs = gsh; gs ; gs = gs->next)
2770 if (gi->gotobj == gs->gotobj && gi->addend == gs->addend)
2771 goto got_found;
2772 gi->next = hs->got_entries;
2773 hs->got_entries = gi;
2774 got_found:;
2777 hi->got_entries = NULL;
2779 /* And similar for the reloc entries. */
2781 if (hs->reloc_entries == NULL)
2782 hs->reloc_entries = hi->reloc_entries;
2783 else
2785 struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
2787 rsh = hs->reloc_entries;
2788 for (ri = hi->reloc_entries; ri ; ri = rin)
2790 rin = ri->next;
2791 for (rs = rsh; rs ; rs = rs->next)
2792 if (ri->rtype == rs->rtype)
2794 rs->count += ri->count;
2795 goto found_reloc;
2797 ri->next = hs->reloc_entries;
2798 hs->reloc_entries = ri;
2799 found_reloc:;
2802 hi->reloc_entries = NULL;
2804 return true;
2807 /* Is it possible to merge two object file's .got tables? */
2809 static boolean
2810 elf64_alpha_can_merge_gots (a, b)
2811 bfd *a, *b;
2813 int total = alpha_elf_tdata (a)->total_got_entries;
2814 bfd *bsub;
2816 /* Trivial quick fallout test. */
2817 if (total + alpha_elf_tdata (b)->total_got_entries <= MAX_GOT_ENTRIES)
2818 return true;
2820 /* By their nature, local .got entries cannot be merged. */
2821 if ((total += alpha_elf_tdata (b)->n_local_got_entries) > MAX_GOT_ENTRIES)
2822 return false;
2824 /* Failing the common trivial comparison, we must effectively
2825 perform the merge. Not actually performing the merge means that
2826 we don't have to store undo information in case we fail. */
2827 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2829 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
2830 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2831 int i, n;
2833 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2834 for (i = 0; i < n; ++i)
2836 struct alpha_elf_got_entry *ae, *be;
2837 struct alpha_elf_link_hash_entry *h;
2839 h = hashes[i];
2840 while (h->root.root.type == bfd_link_hash_indirect
2841 || h->root.root.type == bfd_link_hash_warning)
2842 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2844 for (be = h->got_entries; be ; be = be->next)
2846 if (be->use_count == 0)
2847 continue;
2848 if (be->gotobj != b)
2849 continue;
2851 for (ae = h->got_entries; ae ; ae = ae->next)
2852 if (ae->gotobj == a && ae->addend == be->addend)
2853 goto global_found;
2855 if (++total > MAX_GOT_ENTRIES)
2856 return false;
2857 global_found:;
2862 return true;
2865 /* Actually merge two .got tables. */
2867 static void
2868 elf64_alpha_merge_gots (a, b)
2869 bfd *a, *b;
2871 int total = alpha_elf_tdata (a)->total_got_entries;
2872 bfd *bsub;
2874 /* Remember local expansion. */
2876 int e = alpha_elf_tdata (b)->n_local_got_entries;
2877 total += e;
2878 alpha_elf_tdata (a)->n_local_got_entries += e;
2881 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2883 struct alpha_elf_got_entry **local_got_entries;
2884 struct alpha_elf_link_hash_entry **hashes;
2885 Elf_Internal_Shdr *symtab_hdr;
2886 int i, n;
2888 /* Let the local .got entries know they are part of a new subsegment. */
2889 local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
2890 if (local_got_entries)
2892 n = elf_tdata (bsub)->symtab_hdr.sh_info;
2893 for (i = 0; i < n; ++i)
2895 struct alpha_elf_got_entry *ent;
2896 for (ent = local_got_entries[i]; ent; ent = ent->next)
2897 ent->gotobj = a;
2901 /* Merge the global .got entries. */
2902 hashes = alpha_elf_sym_hashes (bsub);
2903 symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2905 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2906 for (i = 0; i < n; ++i)
2908 struct alpha_elf_got_entry *ae, *be, **pbe, **start;
2909 struct alpha_elf_link_hash_entry *h;
2911 h = hashes[i];
2912 while (h->root.root.type == bfd_link_hash_indirect
2913 || h->root.root.type == bfd_link_hash_warning)
2914 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2916 start = &h->got_entries;
2917 for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
2919 if (be->use_count == 0)
2921 *pbe = be->next;
2922 continue;
2924 if (be->gotobj != b)
2925 continue;
2927 for (ae = *start; ae ; ae = ae->next)
2928 if (ae->gotobj == a && ae->addend == be->addend)
2930 ae->flags |= be->flags;
2931 ae->use_count += be->use_count;
2932 *pbe = be->next;
2933 goto global_found;
2935 be->gotobj = a;
2936 total += 1;
2938 global_found:;
2942 alpha_elf_tdata (bsub)->gotobj = a;
2944 alpha_elf_tdata (a)->total_got_entries = total;
2946 /* Merge the two in_got chains. */
2948 bfd *next;
2950 bsub = a;
2951 while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
2952 bsub = next;
2954 alpha_elf_tdata (bsub)->in_got_link_next = b;
2958 /* Calculate the offsets for the got entries. */
2960 static boolean
2961 elf64_alpha_calc_got_offsets_for_symbol (h, arg)
2962 struct alpha_elf_link_hash_entry *h;
2963 PTR arg;
2965 struct alpha_elf_got_entry *gotent;
2967 for (gotent = h->got_entries; gotent; gotent = gotent->next)
2968 if (gotent->use_count > 0)
2970 bfd_size_type *plge
2971 = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
2973 gotent->got_offset = *plge;
2974 *plge += 8;
2977 return true;
2980 static void
2981 elf64_alpha_calc_got_offsets (info)
2982 struct bfd_link_info *info;
2984 bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
2986 /* First, zero out the .got sizes, as we may be recalculating the
2987 .got after optimizing it. */
2988 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2989 alpha_elf_tdata(i)->got->_raw_size = 0;
2991 /* Next, fill in the offsets for all the global entries. */
2992 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2993 elf64_alpha_calc_got_offsets_for_symbol,
2994 NULL);
2996 /* Finally, fill in the offsets for the local entries. */
2997 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2999 bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
3000 bfd *j;
3002 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3004 struct alpha_elf_got_entry **local_got_entries, *gotent;
3005 int k, n;
3007 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3008 if (!local_got_entries)
3009 continue;
3011 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3012 for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
3013 if (gotent->use_count > 0)
3015 gotent->got_offset = got_offset;
3016 got_offset += 8;
3020 alpha_elf_tdata(i)->got->_raw_size = got_offset;
3021 alpha_elf_tdata(i)->got->_cooked_size = got_offset;
3025 /* Constructs the gots. */
3027 static boolean
3028 elf64_alpha_size_got_sections (output_bfd, info)
3029 bfd *output_bfd;
3030 struct bfd_link_info *info;
3032 bfd *i, *got_list, *cur_got_obj;
3033 int something_changed = 0;
3035 got_list = alpha_elf_hash_table (info)->got_list;
3037 /* On the first time through, pretend we have an existing got list
3038 consisting of all of the input files. */
3039 if (got_list == NULL)
3041 for (i = info->input_bfds; i ; i = i->link_next)
3043 bfd *this_got = alpha_elf_tdata (i)->gotobj;
3044 if (this_got == NULL)
3045 continue;
3047 /* We are assuming no merging has yet ocurred. */
3048 BFD_ASSERT (this_got == i);
3050 if (alpha_elf_tdata (this_got)->total_got_entries > MAX_GOT_ENTRIES)
3052 /* Yikes! A single object file has too many entries. */
3053 (*_bfd_error_handler)
3054 (_("%s: .got subsegment exceeds 64K (size %d)"),
3055 bfd_get_filename (i),
3056 alpha_elf_tdata (this_got)->total_got_entries * 8);
3057 return false;
3060 if (got_list == NULL)
3061 got_list = this_got;
3062 else
3063 alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
3064 cur_got_obj = this_got;
3067 /* Strange degenerate case of no got references. */
3068 if (got_list == NULL)
3069 return true;
3071 alpha_elf_hash_table (info)->got_list = got_list;
3073 /* Force got offsets to be recalculated. */
3074 something_changed = 1;
3077 cur_got_obj = got_list;
3078 i = alpha_elf_tdata(cur_got_obj)->got_link_next;
3079 while (i != NULL)
3081 if (elf64_alpha_can_merge_gots (cur_got_obj, i))
3083 elf64_alpha_merge_gots (cur_got_obj, i);
3084 i = alpha_elf_tdata(i)->got_link_next;
3085 alpha_elf_tdata(cur_got_obj)->got_link_next = i;
3086 something_changed = 1;
3088 else
3090 cur_got_obj = i;
3091 i = alpha_elf_tdata(i)->got_link_next;
3095 /* Once the gots have been merged, fill in the got offsets for
3096 everything therein. */
3097 if (1 || something_changed)
3098 elf64_alpha_calc_got_offsets (info);
3100 return true;
3103 static boolean
3104 elf64_alpha_always_size_sections (output_bfd, info)
3105 bfd *output_bfd;
3106 struct bfd_link_info *info;
3108 bfd *i;
3110 if (info->relocateable)
3111 return true;
3113 /* First, take care of the indirect symbols created by versioning. */
3114 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3115 elf64_alpha_merge_ind_symbols,
3116 NULL);
3118 if (!elf64_alpha_size_got_sections (output_bfd, info))
3119 return false;
3121 /* Allocate space for all of the .got subsections. */
3122 i = alpha_elf_hash_table (info)->got_list;
3123 for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
3125 asection *s = alpha_elf_tdata(i)->got;
3126 if (s->_raw_size > 0)
3128 s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
3129 if (s->contents == NULL)
3130 return false;
3134 return true;
3137 /* Work out the sizes of the dynamic relocation entries. */
3139 static boolean
3140 elf64_alpha_calc_dynrel_sizes (h, info)
3141 struct alpha_elf_link_hash_entry *h;
3142 struct bfd_link_info *info;
3144 /* If the symbol was defined as a common symbol in a regular object
3145 file, and there was no definition in any dynamic object, then the
3146 linker will have allocated space for the symbol in a common
3147 section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
3148 set. This is done for dynamic symbols in
3149 elf_adjust_dynamic_symbol but this is not done for non-dynamic
3150 symbols, somehow. */
3151 if (((h->root.elf_link_hash_flags
3152 & (ELF_LINK_HASH_DEF_REGULAR
3153 | ELF_LINK_HASH_REF_REGULAR
3154 | ELF_LINK_HASH_DEF_DYNAMIC))
3155 == ELF_LINK_HASH_REF_REGULAR)
3156 && (h->root.root.type == bfd_link_hash_defined
3157 || h->root.root.type == bfd_link_hash_defweak)
3158 && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
3160 h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3163 /* If the symbol is dynamic, we'll need all the relocations in their
3164 natural form. If this is a shared object, and it has been forced
3165 local, we'll need the same number of RELATIVE relocations. */
3167 if (alpha_elf_dynamic_symbol_p (&h->root, info) || info->shared)
3169 struct alpha_elf_reloc_entry *relent;
3170 bfd *dynobj;
3171 struct alpha_elf_got_entry *gotent;
3172 bfd_size_type count;
3173 asection *srel;
3175 for (relent = h->reloc_entries; relent; relent = relent->next)
3176 if (relent->rtype == R_ALPHA_REFLONG
3177 || relent->rtype == R_ALPHA_REFQUAD)
3179 relent->srel->_raw_size +=
3180 sizeof (Elf64_External_Rela) * relent->count;
3183 dynobj = elf_hash_table(info)->dynobj;
3184 count = 0;
3186 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3187 count++;
3189 /* If we are using a .plt entry, subtract one, as the first
3190 reference uses a .rela.plt entry instead. */
3191 if (h->root.plt.offset != MINUS_ONE)
3192 count--;
3194 if (count > 0)
3196 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3197 BFD_ASSERT (srel != NULL);
3198 srel->_raw_size += sizeof (Elf64_External_Rela) * count;
3202 return true;
3205 /* Set the sizes of the dynamic sections. */
3207 static boolean
3208 elf64_alpha_size_dynamic_sections (output_bfd, info)
3209 bfd *output_bfd;
3210 struct bfd_link_info *info;
3212 bfd *dynobj;
3213 asection *s;
3214 boolean reltext;
3215 boolean relplt;
3217 dynobj = elf_hash_table(info)->dynobj;
3218 BFD_ASSERT(dynobj != NULL);
3220 if (elf_hash_table (info)->dynamic_sections_created)
3222 /* Set the contents of the .interp section to the interpreter. */
3223 if (!info->shared)
3225 s = bfd_get_section_by_name (dynobj, ".interp");
3226 BFD_ASSERT (s != NULL);
3227 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3228 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3231 /* Now that we've seen all of the input files, we can decide which
3232 symbols need dynamic relocation entries and which don't. We've
3233 collected information in check_relocs that we can now apply to
3234 size the dynamic relocation sections. */
3235 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3236 elf64_alpha_calc_dynrel_sizes,
3237 info);
3239 /* When building shared libraries, each local .got entry needs a
3240 RELATIVE reloc. */
3241 if (info->shared)
3243 bfd *i;
3244 asection *srel;
3245 bfd_size_type count;
3247 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3248 BFD_ASSERT (srel != NULL);
3250 for (i = alpha_elf_hash_table(info)->got_list, count = 0;
3251 i != NULL;
3252 i = alpha_elf_tdata(i)->got_link_next)
3253 count += alpha_elf_tdata(i)->n_local_got_entries;
3255 srel->_raw_size += count * sizeof (Elf64_External_Rela);
3258 /* else we're not dynamic and by definition we don't need such things. */
3260 /* The check_relocs and adjust_dynamic_symbol entry points have
3261 determined the sizes of the various dynamic sections. Allocate
3262 memory for them. */
3263 reltext = false;
3264 relplt = false;
3265 for (s = dynobj->sections; s != NULL; s = s->next)
3267 const char *name;
3268 boolean strip;
3270 if (!(s->flags & SEC_LINKER_CREATED))
3271 continue;
3273 /* It's OK to base decisions on the section name, because none
3274 of the dynobj section names depend upon the input files. */
3275 name = bfd_get_section_name (dynobj, s);
3277 /* If we don't need this section, strip it from the output file.
3278 This is to handle .rela.bss and .rela.plt. We must create it
3279 in create_dynamic_sections, because it must be created before
3280 the linker maps input sections to output sections. The
3281 linker does that before adjust_dynamic_symbol is called, and
3282 it is that function which decides whether anything needs to
3283 go into these sections. */
3285 strip = false;
3287 if (strncmp (name, ".rela", 5) == 0)
3289 strip = (s->_raw_size == 0);
3291 if (!strip)
3293 const char *outname;
3294 asection *target;
3296 /* If this relocation section applies to a read only
3297 section, then we probably need a DT_TEXTREL entry. */
3298 outname = bfd_get_section_name (output_bfd,
3299 s->output_section);
3300 target = bfd_get_section_by_name (output_bfd, outname + 5);
3301 if (target != NULL
3302 && (target->flags & SEC_READONLY) != 0
3303 && (target->flags & SEC_ALLOC) != 0)
3304 reltext = true;
3306 if (strcmp(name, ".rela.plt") == 0)
3307 relplt = true;
3309 /* We use the reloc_count field as a counter if we need
3310 to copy relocs into the output file. */
3311 s->reloc_count = 0;
3314 else if (strcmp (name, ".plt") != 0)
3316 /* It's not one of our dynamic sections, so don't allocate space. */
3317 continue;
3320 if (strip)
3321 _bfd_strip_section_from_output (info, s);
3322 else
3324 /* Allocate memory for the section contents. */
3325 s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
3326 if (s->contents == NULL && s->_raw_size != 0)
3327 return false;
3331 if (elf_hash_table (info)->dynamic_sections_created)
3333 /* Add some entries to the .dynamic section. We fill in the
3334 values later, in elf64_alpha_finish_dynamic_sections, but we
3335 must add the entries now so that we get the correct size for
3336 the .dynamic section. The DT_DEBUG entry is filled in by the
3337 dynamic linker and used by the debugger. */
3338 if (!info->shared)
3340 if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
3341 return false;
3344 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
3345 return false;
3347 if (relplt)
3349 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3350 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
3351 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
3352 return false;
3355 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
3356 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
3357 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
3358 sizeof (Elf64_External_Rela)))
3359 return false;
3361 if (reltext)
3363 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
3364 return false;
3365 info->flags |= DF_TEXTREL;
3369 return true;
3372 /* Relocate an Alpha ELF section. */
3374 static boolean
3375 elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
3376 contents, relocs, local_syms, local_sections)
3377 bfd *output_bfd;
3378 struct bfd_link_info *info;
3379 bfd *input_bfd;
3380 asection *input_section;
3381 bfd_byte *contents;
3382 Elf_Internal_Rela *relocs;
3383 Elf_Internal_Sym *local_syms;
3384 asection **local_sections;
3386 Elf_Internal_Shdr *symtab_hdr;
3387 Elf_Internal_Rela *rel;
3388 Elf_Internal_Rela *relend;
3389 asection *sec, *sgot, *srel, *srelgot;
3390 bfd *dynobj, *gotobj;
3391 bfd_vma gp;
3393 srelgot = srel = NULL;
3394 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3395 dynobj = elf_hash_table (info)->dynobj;
3396 if (dynobj)
3398 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3401 /* Find the gp value for this input bfd. */
3402 sgot = NULL;
3403 gp = 0;
3404 gotobj = alpha_elf_tdata (input_bfd)->gotobj;
3405 if (gotobj)
3407 sgot = alpha_elf_tdata (gotobj)->got;
3408 gp = _bfd_get_gp_value (gotobj);
3409 if (gp == 0)
3411 gp = (sgot->output_section->vma
3412 + sgot->output_offset
3413 + 0x8000);
3414 _bfd_set_gp_value (gotobj, gp);
3418 rel = relocs;
3419 relend = relocs + input_section->reloc_count;
3420 for (; rel < relend; rel++)
3422 int r_type;
3423 reloc_howto_type *howto;
3424 unsigned long r_symndx;
3425 struct alpha_elf_link_hash_entry *h;
3426 Elf_Internal_Sym *sym;
3427 bfd_vma relocation;
3428 bfd_vma addend;
3429 bfd_reloc_status_type r;
3431 r_type = ELF64_R_TYPE(rel->r_info);
3432 if (r_type < 0 || r_type >= (int) R_ALPHA_max)
3434 bfd_set_error (bfd_error_bad_value);
3435 return false;
3437 howto = elf64_alpha_howto_table + r_type;
3439 r_symndx = ELF64_R_SYM(rel->r_info);
3441 if (info->relocateable)
3443 /* This is a relocateable link. We don't have to change
3444 anything, unless the reloc is against a section symbol,
3445 in which case we have to adjust according to where the
3446 section symbol winds up in the output section. */
3448 /* The symbol associated with GPDISP and LITUSE is
3449 immaterial. Only the addend is significant. */
3450 if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
3451 continue;
3453 if (r_symndx < symtab_hdr->sh_info)
3455 sym = local_syms + r_symndx;
3456 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
3458 sec = local_sections[r_symndx];
3459 rel->r_addend += sec->output_offset + sym->st_value;
3463 continue;
3466 /* This is a final link. */
3468 h = NULL;
3469 sym = NULL;
3470 sec = NULL;
3472 if (r_symndx < symtab_hdr->sh_info)
3474 sym = local_syms + r_symndx;
3475 sec = local_sections[r_symndx];
3476 relocation = (sec->output_section->vma
3477 + sec->output_offset
3478 + sym->st_value);
3480 else
3482 h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
3484 while (h->root.root.type == bfd_link_hash_indirect
3485 || h->root.root.type == bfd_link_hash_warning)
3486 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3488 if (h->root.root.type == bfd_link_hash_defined
3489 || h->root.root.type == bfd_link_hash_defweak)
3491 sec = h->root.root.u.def.section;
3493 #if rth_notdef
3494 if ((r_type == R_ALPHA_LITERAL
3495 && elf_hash_table(info)->dynamic_sections_created
3496 && (!info->shared
3497 || !info->symbolic
3498 || !(h->root.elf_link_hash_flags
3499 & ELF_LINK_HASH_DEF_REGULAR)))
3500 || (info->shared
3501 && (!info->symbolic
3502 || !(h->root.elf_link_hash_flags
3503 & ELF_LINK_HASH_DEF_REGULAR))
3504 && (input_section->flags & SEC_ALLOC)
3505 && (r_type == R_ALPHA_REFLONG
3506 || r_type == R_ALPHA_REFQUAD
3507 || r_type == R_ALPHA_LITERAL)))
3509 /* In these cases, we don't need the relocation value.
3510 We check specially because in some obscure cases
3511 sec->output_section will be NULL. */
3512 relocation = 0;
3514 #else
3515 /* FIXME: Are not these obscure cases simply bugs? Let's
3516 get something working and come back to this. */
3517 if (sec->output_section == NULL)
3518 relocation = 0;
3519 #endif /* rth_notdef */
3520 else
3522 relocation = (h->root.root.u.def.value
3523 + sec->output_section->vma
3524 + sec->output_offset);
3527 else if (h->root.root.type == bfd_link_hash_undefweak)
3528 relocation = 0;
3529 else if (info->shared && !info->symbolic
3530 && !info->no_undefined
3531 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
3532 relocation = 0;
3533 else
3535 if (!((*info->callbacks->undefined_symbol)
3536 (info, h->root.root.root.string, input_bfd,
3537 input_section, rel->r_offset,
3538 (!info->shared || info->no_undefined
3539 || ELF_ST_VISIBILITY (h->root.other)))))
3540 return false;
3541 relocation = 0;
3544 addend = rel->r_addend;
3546 switch (r_type)
3548 case R_ALPHA_GPDISP:
3550 bfd_byte *p_ldah, *p_lda;
3552 BFD_ASSERT(gp != 0);
3554 relocation = (input_section->output_section->vma
3555 + input_section->output_offset
3556 + rel->r_offset);
3558 p_ldah = contents + rel->r_offset - input_section->vma;
3559 p_lda = p_ldah + rel->r_addend;
3561 r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
3562 p_ldah, p_lda);
3564 break;
3566 case R_ALPHA_OP_PUSH:
3567 case R_ALPHA_OP_STORE:
3568 case R_ALPHA_OP_PSUB:
3569 case R_ALPHA_OP_PRSHIFT:
3570 /* We hate these silly beasts. */
3571 abort ();
3573 case R_ALPHA_LITERAL:
3575 struct alpha_elf_got_entry *gotent;
3576 boolean dynamic_symbol;
3578 BFD_ASSERT(sgot != NULL);
3579 BFD_ASSERT(gp != 0);
3581 if (h != NULL)
3583 gotent = h->got_entries;
3584 dynamic_symbol = alpha_elf_dynamic_symbol_p (&h->root, info);
3586 else
3588 gotent = (alpha_elf_tdata(input_bfd)->
3589 local_got_entries[r_symndx]);
3590 dynamic_symbol = false;
3593 BFD_ASSERT(gotent != NULL);
3595 while (gotent->gotobj != gotobj || gotent->addend != addend)
3596 gotent = gotent->next;
3598 BFD_ASSERT(gotent->use_count >= 1);
3600 /* Initialize the .got entry's value. */
3601 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
3603 bfd_put_64 (output_bfd, relocation+addend,
3604 sgot->contents + gotent->got_offset);
3606 /* If the symbol has been forced local, output a
3607 RELATIVE reloc, otherwise it will be handled in
3608 finish_dynamic_symbol. */
3609 if (info->shared && !dynamic_symbol)
3611 Elf_Internal_Rela outrel;
3613 BFD_ASSERT(srelgot != NULL);
3615 outrel.r_offset = (sgot->output_section->vma
3616 + sgot->output_offset
3617 + gotent->got_offset);
3618 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3619 outrel.r_addend = 0;
3621 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3622 ((Elf64_External_Rela *)
3623 srelgot->contents)
3624 + srelgot->reloc_count++);
3625 BFD_ASSERT (sizeof (Elf64_External_Rela)
3626 * srelgot->reloc_count
3627 <= srelgot->_cooked_size);
3630 gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
3633 /* Figure the gprel relocation. */
3634 addend = 0;
3635 relocation = (sgot->output_section->vma
3636 + sgot->output_offset
3637 + gotent->got_offset);
3638 relocation -= gp;
3640 /* overflow handled by _bfd_final_link_relocate */
3641 goto default_reloc;
3643 case R_ALPHA_GPREL32:
3644 case R_ALPHA_GPRELLOW:
3645 BFD_ASSERT(gp != 0);
3646 relocation -= gp;
3647 goto default_reloc;
3649 case R_ALPHA_GPRELHIGH:
3650 BFD_ASSERT(gp != 0);
3651 relocation -= gp;
3652 relocation += addend;
3653 addend = 0;
3654 relocation = (((bfd_signed_vma) relocation >> 16)
3655 + ((relocation >> 15) & 1));
3656 goto default_reloc;
3658 case R_ALPHA_BRADDR:
3659 case R_ALPHA_HINT:
3660 /* The regular PC-relative stuff measures from the start of
3661 the instruction rather than the end. */
3662 addend -= 4;
3663 goto default_reloc;
3665 case R_ALPHA_REFLONG:
3666 case R_ALPHA_REFQUAD:
3668 Elf_Internal_Rela outrel;
3669 boolean skip;
3671 /* Careful here to remember RELATIVE relocations for global
3672 variables for symbolic shared objects. */
3674 if (h && alpha_elf_dynamic_symbol_p (&h->root, info))
3676 BFD_ASSERT(h->root.dynindx != -1);
3677 outrel.r_info = ELF64_R_INFO(h->root.dynindx, r_type);
3678 outrel.r_addend = addend;
3679 addend = 0, relocation = 0;
3681 else if (info->shared && (input_section->flags & SEC_ALLOC))
3683 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3684 outrel.r_addend = 0;
3686 else
3687 goto default_reloc;
3689 if (!srel)
3691 const char *name;
3693 name = (bfd_elf_string_from_elf_section
3694 (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
3695 elf_section_data(input_section)->rel_hdr.sh_name));
3696 BFD_ASSERT(name != NULL);
3698 srel = bfd_get_section_by_name (dynobj, name);
3699 BFD_ASSERT(srel != NULL);
3702 skip = false;
3704 if (elf_section_data (input_section)->stab_info == NULL)
3705 outrel.r_offset = rel->r_offset;
3706 else
3708 bfd_vma off;
3710 off = (_bfd_stab_section_offset
3711 (output_bfd, &elf_hash_table (info)->stab_info,
3712 input_section,
3713 &elf_section_data (input_section)->stab_info,
3714 rel->r_offset));
3715 if (off == (bfd_vma) -1)
3716 skip = true;
3717 outrel.r_offset = off;
3720 if (! skip)
3721 outrel.r_offset += (input_section->output_section->vma
3722 + input_section->output_offset);
3723 else
3724 memset (&outrel, 0, sizeof outrel);
3726 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3727 ((Elf64_External_Rela *)
3728 srel->contents)
3729 + srel->reloc_count++);
3730 BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
3731 <= srel->_cooked_size);
3733 goto default_reloc;
3735 default:
3736 default_reloc:
3737 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3738 contents, rel->r_offset, relocation,
3739 addend);
3740 break;
3743 switch (r)
3745 case bfd_reloc_ok:
3746 break;
3748 case bfd_reloc_overflow:
3750 const char *name;
3752 if (h != NULL)
3753 name = h->root.root.root.string;
3754 else
3756 name = (bfd_elf_string_from_elf_section
3757 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3758 if (name == NULL)
3759 return false;
3760 if (*name == '\0')
3761 name = bfd_section_name (input_bfd, sec);
3763 if (! ((*info->callbacks->reloc_overflow)
3764 (info, name, howto->name, (bfd_vma) 0,
3765 input_bfd, input_section, rel->r_offset)))
3766 return false;
3768 break;
3770 default:
3771 case bfd_reloc_outofrange:
3772 abort ();
3776 return true;
3779 /* Finish up dynamic symbol handling. We set the contents of various
3780 dynamic sections here. */
3782 static boolean
3783 elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
3784 bfd *output_bfd;
3785 struct bfd_link_info *info;
3786 struct elf_link_hash_entry *h;
3787 Elf_Internal_Sym *sym;
3789 bfd *dynobj = elf_hash_table(info)->dynobj;
3791 if (h->plt.offset != MINUS_ONE)
3793 /* Fill in the .plt entry for this symbol. */
3794 asection *splt, *sgot, *srel;
3795 Elf_Internal_Rela outrel;
3796 bfd_vma got_addr, plt_addr;
3797 bfd_vma plt_index;
3798 struct alpha_elf_got_entry *gotent;
3800 BFD_ASSERT (h->dynindx != -1);
3802 /* The first .got entry will be updated by the .plt with the
3803 address of the target function. */
3804 gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3805 BFD_ASSERT (gotent && gotent->addend == 0);
3807 splt = bfd_get_section_by_name (dynobj, ".plt");
3808 BFD_ASSERT (splt != NULL);
3809 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3810 BFD_ASSERT (srel != NULL);
3811 sgot = alpha_elf_tdata (gotent->gotobj)->got;
3812 BFD_ASSERT (sgot != NULL);
3814 got_addr = (sgot->output_section->vma
3815 + sgot->output_offset
3816 + gotent->got_offset);
3817 plt_addr = (splt->output_section->vma
3818 + splt->output_offset
3819 + h->plt.offset);
3821 plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3823 /* Fill in the entry in the procedure linkage table. */
3825 unsigned insn1, insn2, insn3;
3827 insn1 = PLT_ENTRY_WORD1 | ((-(h->plt.offset + 4) >> 2) & 0x1fffff);
3828 insn2 = PLT_ENTRY_WORD2;
3829 insn3 = PLT_ENTRY_WORD3;
3831 bfd_put_32 (output_bfd, insn1, splt->contents + h->plt.offset);
3832 bfd_put_32 (output_bfd, insn2, splt->contents + h->plt.offset + 4);
3833 bfd_put_32 (output_bfd, insn3, splt->contents + h->plt.offset + 8);
3836 /* Fill in the entry in the .rela.plt section. */
3837 outrel.r_offset = got_addr;
3838 outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
3839 outrel.r_addend = 0;
3841 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3842 ((Elf64_External_Rela *)srel->contents
3843 + plt_index));
3845 if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3847 /* Mark the symbol as undefined, rather than as defined in the
3848 .plt section. Leave the value alone. */
3849 sym->st_shndx = SHN_UNDEF;
3852 /* Fill in the entries in the .got. */
3853 bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
3855 /* Subsequent .got entries will continue to bounce through the .plt. */
3856 if (gotent->next)
3858 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3859 BFD_ASSERT (! info->shared || srel != NULL);
3861 gotent = gotent->next;
3864 sgot = alpha_elf_tdata(gotent->gotobj)->got;
3865 BFD_ASSERT(sgot != NULL);
3866 BFD_ASSERT(gotent->addend == 0);
3868 bfd_put_64 (output_bfd, plt_addr,
3869 sgot->contents + gotent->got_offset);
3871 if (info->shared)
3873 outrel.r_offset = (sgot->output_section->vma
3874 + sgot->output_offset
3875 + gotent->got_offset);
3876 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3877 outrel.r_addend = 0;
3879 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3880 ((Elf64_External_Rela *)
3881 srel->contents)
3882 + srel->reloc_count++);
3883 BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
3884 <= srel->_cooked_size);
3887 gotent = gotent->next;
3889 while (gotent != NULL);
3892 else if (alpha_elf_dynamic_symbol_p (h, info))
3894 /* Fill in the dynamic relocations for this symbol's .got entries. */
3895 asection *srel;
3896 Elf_Internal_Rela outrel;
3897 struct alpha_elf_got_entry *gotent;
3899 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3900 BFD_ASSERT (srel != NULL);
3902 outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_GLOB_DAT);
3903 for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3904 gotent != NULL;
3905 gotent = gotent->next)
3907 asection *sgot = alpha_elf_tdata (gotent->gotobj)->got;
3908 outrel.r_offset = (sgot->output_section->vma
3909 + sgot->output_offset
3910 + gotent->got_offset);
3911 outrel.r_addend = gotent->addend;
3913 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3914 ((Elf64_External_Rela *)srel->contents
3915 + srel->reloc_count++));
3916 BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
3917 <= srel->_cooked_size);
3921 /* Mark some specially defined symbols as absolute. */
3922 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3923 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3924 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3925 sym->st_shndx = SHN_ABS;
3927 return true;
3930 /* Finish up the dynamic sections. */
3932 static boolean
3933 elf64_alpha_finish_dynamic_sections (output_bfd, info)
3934 bfd *output_bfd;
3935 struct bfd_link_info *info;
3937 bfd *dynobj;
3938 asection *sdyn;
3940 dynobj = elf_hash_table (info)->dynobj;
3941 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3943 if (elf_hash_table (info)->dynamic_sections_created)
3945 asection *splt;
3946 Elf64_External_Dyn *dyncon, *dynconend;
3948 splt = bfd_get_section_by_name (dynobj, ".plt");
3949 BFD_ASSERT (splt != NULL && sdyn != NULL);
3951 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3952 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3953 for (; dyncon < dynconend; dyncon++)
3955 Elf_Internal_Dyn dyn;
3956 const char *name;
3957 asection *s;
3959 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3961 switch (dyn.d_tag)
3963 case DT_PLTGOT:
3964 name = ".plt";
3965 goto get_vma;
3966 case DT_PLTRELSZ:
3967 name = ".rela.plt";
3968 goto get_size;
3969 case DT_JMPREL:
3970 name = ".rela.plt";
3971 goto get_vma;
3973 case DT_RELASZ:
3974 /* My interpretation of the TIS v1.1 ELF document indicates
3975 that RELASZ should not include JMPREL. This is not what
3976 the rest of the BFD does. It is, however, what the
3977 glibc ld.so wants. Do this fixup here until we found
3978 out who is right. */
3979 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
3980 if (s)
3982 dyn.d_un.d_val -=
3983 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3985 break;
3987 get_vma:
3988 s = bfd_get_section_by_name (output_bfd, name);
3989 dyn.d_un.d_ptr = (s ? s->vma : 0);
3990 break;
3992 get_size:
3993 s = bfd_get_section_by_name (output_bfd, name);
3994 dyn.d_un.d_val =
3995 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3996 break;
3999 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4002 /* Initialize the PLT0 entry */
4003 if (splt->_raw_size > 0)
4005 bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
4006 bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
4007 bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
4008 bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
4010 /* The next two words will be filled in by ld.so */
4011 bfd_put_64 (output_bfd, 0, splt->contents + 16);
4012 bfd_put_64 (output_bfd, 0, splt->contents + 24);
4014 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
4015 PLT_HEADER_SIZE;
4019 return true;
4022 /* We need to use a special link routine to handle the .reginfo and
4023 the .mdebug sections. We need to merge all instances of these
4024 sections together, not write them all out sequentially. */
4026 static boolean
4027 elf64_alpha_final_link (abfd, info)
4028 bfd *abfd;
4029 struct bfd_link_info *info;
4031 asection *o;
4032 struct bfd_link_order *p;
4033 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4034 struct ecoff_debug_info debug;
4035 const struct ecoff_debug_swap *swap
4036 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4037 HDRR *symhdr = &debug.symbolic_header;
4038 PTR mdebug_handle = NULL;
4040 #if 0
4041 if (++ngots == 2)
4043 (*info->callbacks->warning)
4044 (info, _("using multiple gp values"), (char *) NULL,
4045 output_bfd, (asection *) NULL, (bfd_vma) 0);
4047 #endif
4049 /* Go through the sections and collect the .reginfo and .mdebug
4050 information. */
4051 reginfo_sec = NULL;
4052 mdebug_sec = NULL;
4053 gptab_data_sec = NULL;
4054 gptab_bss_sec = NULL;
4055 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4057 #ifdef ERIC_neverdef
4058 if (strcmp (o->name, ".reginfo") == 0)
4060 memset (&reginfo, 0, sizeof reginfo);
4062 /* We have found the .reginfo section in the output file.
4063 Look through all the link_orders comprising it and merge
4064 the information together. */
4065 for (p = o->link_order_head;
4066 p != (struct bfd_link_order *) NULL;
4067 p = p->next)
4069 asection *input_section;
4070 bfd *input_bfd;
4071 Elf64_External_RegInfo ext;
4072 Elf64_RegInfo sub;
4074 if (p->type != bfd_indirect_link_order)
4076 if (p->type == bfd_fill_link_order)
4077 continue;
4078 abort ();
4081 input_section = p->u.indirect.section;
4082 input_bfd = input_section->owner;
4084 /* The linker emulation code has probably clobbered the
4085 size to be zero bytes. */
4086 if (input_section->_raw_size == 0)
4087 input_section->_raw_size = sizeof (Elf64_External_RegInfo);
4089 if (! bfd_get_section_contents (input_bfd, input_section,
4090 (PTR) &ext,
4091 (file_ptr) 0,
4092 sizeof ext))
4093 return false;
4095 bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
4097 reginfo.ri_gprmask |= sub.ri_gprmask;
4098 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4099 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4100 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4101 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4103 /* ri_gp_value is set by the function
4104 alpha_elf_section_processing when the section is
4105 finally written out. */
4107 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4108 elf_link_input_bfd ignores this section. */
4109 input_section->flags &=~ SEC_HAS_CONTENTS;
4112 /* Force the section size to the value we want. */
4113 o->_raw_size = sizeof (Elf64_External_RegInfo);
4115 /* Skip this section later on (I don't think this currently
4116 matters, but someday it might). */
4117 o->link_order_head = (struct bfd_link_order *) NULL;
4119 reginfo_sec = o;
4121 #endif
4123 if (strcmp (o->name, ".mdebug") == 0)
4125 struct extsym_info einfo;
4127 /* We have found the .mdebug section in the output file.
4128 Look through all the link_orders comprising it and merge
4129 the information together. */
4130 symhdr->magic = swap->sym_magic;
4131 /* FIXME: What should the version stamp be? */
4132 symhdr->vstamp = 0;
4133 symhdr->ilineMax = 0;
4134 symhdr->cbLine = 0;
4135 symhdr->idnMax = 0;
4136 symhdr->ipdMax = 0;
4137 symhdr->isymMax = 0;
4138 symhdr->ioptMax = 0;
4139 symhdr->iauxMax = 0;
4140 symhdr->issMax = 0;
4141 symhdr->issExtMax = 0;
4142 symhdr->ifdMax = 0;
4143 symhdr->crfd = 0;
4144 symhdr->iextMax = 0;
4146 /* We accumulate the debugging information itself in the
4147 debug_info structure. */
4148 debug.line = NULL;
4149 debug.external_dnr = NULL;
4150 debug.external_pdr = NULL;
4151 debug.external_sym = NULL;
4152 debug.external_opt = NULL;
4153 debug.external_aux = NULL;
4154 debug.ss = NULL;
4155 debug.ssext = debug.ssext_end = NULL;
4156 debug.external_fdr = NULL;
4157 debug.external_rfd = NULL;
4158 debug.external_ext = debug.external_ext_end = NULL;
4160 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4161 if (mdebug_handle == (PTR) NULL)
4162 return false;
4164 if (1)
4166 asection *s;
4167 EXTR esym;
4168 bfd_vma last;
4169 unsigned int i;
4170 static const char * const name[] =
4172 ".text", ".init", ".fini", ".data",
4173 ".rodata", ".sdata", ".sbss", ".bss"
4175 static const int sc[] = { scText, scInit, scFini, scData,
4176 scRData, scSData, scSBss, scBss };
4178 esym.jmptbl = 0;
4179 esym.cobol_main = 0;
4180 esym.weakext = 0;
4181 esym.reserved = 0;
4182 esym.ifd = ifdNil;
4183 esym.asym.iss = issNil;
4184 esym.asym.st = stLocal;
4185 esym.asym.reserved = 0;
4186 esym.asym.index = indexNil;
4187 for (i = 0; i < 8; i++)
4189 esym.asym.sc = sc[i];
4190 s = bfd_get_section_by_name (abfd, name[i]);
4191 if (s != NULL)
4193 esym.asym.value = s->vma;
4194 last = s->vma + s->_raw_size;
4196 else
4197 esym.asym.value = last;
4199 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4200 name[i], &esym))
4201 return false;
4205 for (p = o->link_order_head;
4206 p != (struct bfd_link_order *) NULL;
4207 p = p->next)
4209 asection *input_section;
4210 bfd *input_bfd;
4211 const struct ecoff_debug_swap *input_swap;
4212 struct ecoff_debug_info input_debug;
4213 char *eraw_src;
4214 char *eraw_end;
4216 if (p->type != bfd_indirect_link_order)
4218 if (p->type == bfd_fill_link_order)
4219 continue;
4220 abort ();
4223 input_section = p->u.indirect.section;
4224 input_bfd = input_section->owner;
4226 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4227 || (get_elf_backend_data (input_bfd)
4228 ->elf_backend_ecoff_debug_swap) == NULL)
4230 /* I don't know what a non ALPHA ELF bfd would be
4231 doing with a .mdebug section, but I don't really
4232 want to deal with it. */
4233 continue;
4236 input_swap = (get_elf_backend_data (input_bfd)
4237 ->elf_backend_ecoff_debug_swap);
4239 BFD_ASSERT (p->size == input_section->_raw_size);
4241 /* The ECOFF linking code expects that we have already
4242 read in the debugging information and set up an
4243 ecoff_debug_info structure, so we do that now. */
4244 if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
4245 &input_debug))
4246 return false;
4248 if (! (bfd_ecoff_debug_accumulate
4249 (mdebug_handle, abfd, &debug, swap, input_bfd,
4250 &input_debug, input_swap, info)))
4251 return false;
4253 /* Loop through the external symbols. For each one with
4254 interesting information, try to find the symbol in
4255 the linker global hash table and save the information
4256 for the output external symbols. */
4257 eraw_src = input_debug.external_ext;
4258 eraw_end = (eraw_src
4259 + (input_debug.symbolic_header.iextMax
4260 * input_swap->external_ext_size));
4261 for (;
4262 eraw_src < eraw_end;
4263 eraw_src += input_swap->external_ext_size)
4265 EXTR ext;
4266 const char *name;
4267 struct alpha_elf_link_hash_entry *h;
4269 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4270 if (ext.asym.sc == scNil
4271 || ext.asym.sc == scUndefined
4272 || ext.asym.sc == scSUndefined)
4273 continue;
4275 name = input_debug.ssext + ext.asym.iss;
4276 h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
4277 name, false, false, true);
4278 if (h == NULL || h->esym.ifd != -2)
4279 continue;
4281 if (ext.ifd != -1)
4283 BFD_ASSERT (ext.ifd
4284 < input_debug.symbolic_header.ifdMax);
4285 ext.ifd = input_debug.ifdmap[ext.ifd];
4288 h->esym = ext;
4291 /* Free up the information we just read. */
4292 free (input_debug.line);
4293 free (input_debug.external_dnr);
4294 free (input_debug.external_pdr);
4295 free (input_debug.external_sym);
4296 free (input_debug.external_opt);
4297 free (input_debug.external_aux);
4298 free (input_debug.ss);
4299 free (input_debug.ssext);
4300 free (input_debug.external_fdr);
4301 free (input_debug.external_rfd);
4302 free (input_debug.external_ext);
4304 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4305 elf_link_input_bfd ignores this section. */
4306 input_section->flags &=~ SEC_HAS_CONTENTS;
4309 #ifdef ERIC_neverdef
4310 if (info->shared)
4312 /* Create .rtproc section. */
4313 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4314 if (rtproc_sec == NULL)
4316 flagword flags = (SEC_HAS_CONTENTS
4317 | SEC_IN_MEMORY
4318 | SEC_LINKER_CREATED
4319 | SEC_READONLY);
4321 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4322 if (rtproc_sec == NULL
4323 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4324 || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
4325 return false;
4328 if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
4329 info, rtproc_sec, &debug))
4330 return false;
4332 #endif
4334 /* Build the external symbol information. */
4335 einfo.abfd = abfd;
4336 einfo.info = info;
4337 einfo.debug = &debug;
4338 einfo.swap = swap;
4339 einfo.failed = false;
4340 elf_link_hash_traverse (elf_hash_table (info),
4341 elf64_alpha_output_extsym,
4342 (PTR) &einfo);
4343 if (einfo.failed)
4344 return false;
4346 /* Set the size of the .mdebug section. */
4347 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4349 /* Skip this section later on (I don't think this currently
4350 matters, but someday it might). */
4351 o->link_order_head = (struct bfd_link_order *) NULL;
4353 mdebug_sec = o;
4356 #ifdef ERIC_neverdef
4357 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4359 const char *subname;
4360 unsigned int c;
4361 Elf64_gptab *tab;
4362 Elf64_External_gptab *ext_tab;
4363 unsigned int i;
4365 /* The .gptab.sdata and .gptab.sbss sections hold
4366 information describing how the small data area would
4367 change depending upon the -G switch. These sections
4368 not used in executables files. */
4369 if (! info->relocateable)
4371 asection **secpp;
4373 for (p = o->link_order_head;
4374 p != (struct bfd_link_order *) NULL;
4375 p = p->next)
4377 asection *input_section;
4379 if (p->type != bfd_indirect_link_order)
4381 if (p->type == bfd_fill_link_order)
4382 continue;
4383 abort ();
4386 input_section = p->u.indirect.section;
4388 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4389 elf_link_input_bfd ignores this section. */
4390 input_section->flags &=~ SEC_HAS_CONTENTS;
4393 /* Skip this section later on (I don't think this
4394 currently matters, but someday it might). */
4395 o->link_order_head = (struct bfd_link_order *) NULL;
4397 /* Really remove the section. */
4398 for (secpp = &abfd->sections;
4399 *secpp != o;
4400 secpp = &(*secpp)->next)
4402 *secpp = (*secpp)->next;
4403 --abfd->section_count;
4405 continue;
4408 /* There is one gptab for initialized data, and one for
4409 uninitialized data. */
4410 if (strcmp (o->name, ".gptab.sdata") == 0)
4411 gptab_data_sec = o;
4412 else if (strcmp (o->name, ".gptab.sbss") == 0)
4413 gptab_bss_sec = o;
4414 else
4416 (*_bfd_error_handler)
4417 (_("%s: illegal section name `%s'"),
4418 bfd_get_filename (abfd), o->name);
4419 bfd_set_error (bfd_error_nonrepresentable_section);
4420 return false;
4423 /* The linker script always combines .gptab.data and
4424 .gptab.sdata into .gptab.sdata, and likewise for
4425 .gptab.bss and .gptab.sbss. It is possible that there is
4426 no .sdata or .sbss section in the output file, in which
4427 case we must change the name of the output section. */
4428 subname = o->name + sizeof ".gptab" - 1;
4429 if (bfd_get_section_by_name (abfd, subname) == NULL)
4431 if (o == gptab_data_sec)
4432 o->name = ".gptab.data";
4433 else
4434 o->name = ".gptab.bss";
4435 subname = o->name + sizeof ".gptab" - 1;
4436 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4439 /* Set up the first entry. */
4440 c = 1;
4441 tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
4442 if (tab == NULL)
4443 return false;
4444 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4445 tab[0].gt_header.gt_unused = 0;
4447 /* Combine the input sections. */
4448 for (p = o->link_order_head;
4449 p != (struct bfd_link_order *) NULL;
4450 p = p->next)
4452 asection *input_section;
4453 bfd *input_bfd;
4454 bfd_size_type size;
4455 unsigned long last;
4456 bfd_size_type gpentry;
4458 if (p->type != bfd_indirect_link_order)
4460 if (p->type == bfd_fill_link_order)
4461 continue;
4462 abort ();
4465 input_section = p->u.indirect.section;
4466 input_bfd = input_section->owner;
4468 /* Combine the gptab entries for this input section one
4469 by one. We know that the input gptab entries are
4470 sorted by ascending -G value. */
4471 size = bfd_section_size (input_bfd, input_section);
4472 last = 0;
4473 for (gpentry = sizeof (Elf64_External_gptab);
4474 gpentry < size;
4475 gpentry += sizeof (Elf64_External_gptab))
4477 Elf64_External_gptab ext_gptab;
4478 Elf64_gptab int_gptab;
4479 unsigned long val;
4480 unsigned long add;
4481 boolean exact;
4482 unsigned int look;
4484 if (! (bfd_get_section_contents
4485 (input_bfd, input_section, (PTR) &ext_gptab,
4486 gpentry, sizeof (Elf64_External_gptab))))
4488 free (tab);
4489 return false;
4492 bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
4493 &int_gptab);
4494 val = int_gptab.gt_entry.gt_g_value;
4495 add = int_gptab.gt_entry.gt_bytes - last;
4497 exact = false;
4498 for (look = 1; look < c; look++)
4500 if (tab[look].gt_entry.gt_g_value >= val)
4501 tab[look].gt_entry.gt_bytes += add;
4503 if (tab[look].gt_entry.gt_g_value == val)
4504 exact = true;
4507 if (! exact)
4509 Elf64_gptab *new_tab;
4510 unsigned int max;
4512 /* We need a new table entry. */
4513 new_tab = ((Elf64_gptab *)
4514 bfd_realloc ((PTR) tab,
4515 (c + 1) * sizeof (Elf64_gptab)));
4516 if (new_tab == NULL)
4518 free (tab);
4519 return false;
4521 tab = new_tab;
4522 tab[c].gt_entry.gt_g_value = val;
4523 tab[c].gt_entry.gt_bytes = add;
4525 /* Merge in the size for the next smallest -G
4526 value, since that will be implied by this new
4527 value. */
4528 max = 0;
4529 for (look = 1; look < c; look++)
4531 if (tab[look].gt_entry.gt_g_value < val
4532 && (max == 0
4533 || (tab[look].gt_entry.gt_g_value
4534 > tab[max].gt_entry.gt_g_value)))
4535 max = look;
4537 if (max != 0)
4538 tab[c].gt_entry.gt_bytes +=
4539 tab[max].gt_entry.gt_bytes;
4541 ++c;
4544 last = int_gptab.gt_entry.gt_bytes;
4547 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4548 elf_link_input_bfd ignores this section. */
4549 input_section->flags &=~ SEC_HAS_CONTENTS;
4552 /* The table must be sorted by -G value. */
4553 if (c > 2)
4554 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4556 /* Swap out the table. */
4557 ext_tab = ((Elf64_External_gptab *)
4558 bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
4559 if (ext_tab == NULL)
4561 free (tab);
4562 return false;
4565 for (i = 0; i < c; i++)
4566 bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
4567 free (tab);
4569 o->_raw_size = c * sizeof (Elf64_External_gptab);
4570 o->contents = (bfd_byte *) ext_tab;
4572 /* Skip this section later on (I don't think this currently
4573 matters, but someday it might). */
4574 o->link_order_head = (struct bfd_link_order *) NULL;
4576 #endif
4580 /* Invoke the regular ELF backend linker to do all the work. */
4581 if (! bfd_elf64_bfd_final_link (abfd, info))
4582 return false;
4584 /* Now write out the computed sections. */
4586 /* The .got subsections... */
4588 bfd *i, *dynobj = elf_hash_table(info)->dynobj;
4589 for (i = alpha_elf_hash_table(info)->got_list;
4590 i != NULL;
4591 i = alpha_elf_tdata(i)->got_link_next)
4593 asection *sgot;
4595 /* elf_bfd_final_link already did everything in dynobj. */
4596 if (i == dynobj)
4597 continue;
4599 sgot = alpha_elf_tdata(i)->got;
4600 if (! bfd_set_section_contents (abfd, sgot->output_section,
4601 sgot->contents, sgot->output_offset,
4602 sgot->_raw_size))
4603 return false;
4607 #ifdef ERIC_neverdef
4608 if (reginfo_sec != (asection *) NULL)
4610 Elf64_External_RegInfo ext;
4612 bfd_alpha_elf64_swap_reginfo_out (abfd, &reginfo, &ext);
4613 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4614 (file_ptr) 0, sizeof ext))
4615 return false;
4617 #endif
4619 if (mdebug_sec != (asection *) NULL)
4621 BFD_ASSERT (abfd->output_has_begun);
4622 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4623 swap, info,
4624 mdebug_sec->filepos))
4625 return false;
4627 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4630 if (gptab_data_sec != (asection *) NULL)
4632 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4633 gptab_data_sec->contents,
4634 (file_ptr) 0,
4635 gptab_data_sec->_raw_size))
4636 return false;
4639 if (gptab_bss_sec != (asection *) NULL)
4641 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4642 gptab_bss_sec->contents,
4643 (file_ptr) 0,
4644 gptab_bss_sec->_raw_size))
4645 return false;
4648 return true;
4651 /* ECOFF swapping routines. These are used when dealing with the
4652 .mdebug section, which is in the ECOFF debugging format. Copied
4653 from elf32-mips.c. */
4654 static const struct ecoff_debug_swap
4655 elf64_alpha_ecoff_debug_swap =
4657 /* Symbol table magic number. */
4658 magicSym2,
4659 /* Alignment of debugging information. E.g., 4. */
4661 /* Sizes of external symbolic information. */
4662 sizeof (struct hdr_ext),
4663 sizeof (struct dnr_ext),
4664 sizeof (struct pdr_ext),
4665 sizeof (struct sym_ext),
4666 sizeof (struct opt_ext),
4667 sizeof (struct fdr_ext),
4668 sizeof (struct rfd_ext),
4669 sizeof (struct ext_ext),
4670 /* Functions to swap in external symbolic data. */
4671 ecoff_swap_hdr_in,
4672 ecoff_swap_dnr_in,
4673 ecoff_swap_pdr_in,
4674 ecoff_swap_sym_in,
4675 ecoff_swap_opt_in,
4676 ecoff_swap_fdr_in,
4677 ecoff_swap_rfd_in,
4678 ecoff_swap_ext_in,
4679 _bfd_ecoff_swap_tir_in,
4680 _bfd_ecoff_swap_rndx_in,
4681 /* Functions to swap out external symbolic data. */
4682 ecoff_swap_hdr_out,
4683 ecoff_swap_dnr_out,
4684 ecoff_swap_pdr_out,
4685 ecoff_swap_sym_out,
4686 ecoff_swap_opt_out,
4687 ecoff_swap_fdr_out,
4688 ecoff_swap_rfd_out,
4689 ecoff_swap_ext_out,
4690 _bfd_ecoff_swap_tir_out,
4691 _bfd_ecoff_swap_rndx_out,
4692 /* Function to read in symbolic data. */
4693 elf64_alpha_read_ecoff_info
4696 /* Use a non-standard hash bucket size of 8. */
4698 const struct elf_size_info alpha_elf_size_info =
4700 sizeof (Elf64_External_Ehdr),
4701 sizeof (Elf64_External_Phdr),
4702 sizeof (Elf64_External_Shdr),
4703 sizeof (Elf64_External_Rel),
4704 sizeof (Elf64_External_Rela),
4705 sizeof (Elf64_External_Sym),
4706 sizeof (Elf64_External_Dyn),
4707 sizeof (Elf_External_Note),
4710 64, 8,
4711 ELFCLASS64, EV_CURRENT,
4712 bfd_elf64_write_out_phdrs,
4713 bfd_elf64_write_shdrs_and_ehdr,
4714 bfd_elf64_write_relocs,
4715 bfd_elf64_swap_symbol_out,
4716 bfd_elf64_slurp_reloc_table,
4717 bfd_elf64_slurp_symbol_table,
4718 bfd_elf64_swap_dyn_in,
4719 bfd_elf64_swap_dyn_out,
4720 NULL,
4721 NULL,
4722 NULL,
4723 NULL
4726 #define TARGET_LITTLE_SYM bfd_elf64_alpha_vec
4727 #define TARGET_LITTLE_NAME "elf64-alpha"
4728 #define ELF_ARCH bfd_arch_alpha
4729 #define ELF_MACHINE_CODE EM_ALPHA
4730 #define ELF_MAXPAGESIZE 0x10000
4732 #define bfd_elf64_bfd_link_hash_table_create \
4733 elf64_alpha_bfd_link_hash_table_create
4735 #define bfd_elf64_bfd_reloc_type_lookup \
4736 elf64_alpha_bfd_reloc_type_lookup
4737 #define elf_info_to_howto \
4738 elf64_alpha_info_to_howto
4740 #define bfd_elf64_mkobject \
4741 elf64_alpha_mkobject
4742 #define elf_backend_object_p \
4743 elf64_alpha_object_p
4745 #define elf_backend_section_from_shdr \
4746 elf64_alpha_section_from_shdr
4747 #define elf_backend_fake_sections \
4748 elf64_alpha_fake_sections
4750 #define bfd_elf64_bfd_is_local_label_name \
4751 elf64_alpha_is_local_label_name
4752 #define bfd_elf64_find_nearest_line \
4753 elf64_alpha_find_nearest_line
4754 #define bfd_elf64_bfd_relax_section \
4755 elf64_alpha_relax_section
4757 #define elf_backend_add_symbol_hook \
4758 elf64_alpha_add_symbol_hook
4759 #define elf_backend_check_relocs \
4760 elf64_alpha_check_relocs
4761 #define elf_backend_create_dynamic_sections \
4762 elf64_alpha_create_dynamic_sections
4763 #define elf_backend_adjust_dynamic_symbol \
4764 elf64_alpha_adjust_dynamic_symbol
4765 #define elf_backend_always_size_sections \
4766 elf64_alpha_always_size_sections
4767 #define elf_backend_size_dynamic_sections \
4768 elf64_alpha_size_dynamic_sections
4769 #define elf_backend_relocate_section \
4770 elf64_alpha_relocate_section
4771 #define elf_backend_finish_dynamic_symbol \
4772 elf64_alpha_finish_dynamic_symbol
4773 #define elf_backend_finish_dynamic_sections \
4774 elf64_alpha_finish_dynamic_sections
4775 #define bfd_elf64_bfd_final_link \
4776 elf64_alpha_final_link
4778 #define elf_backend_ecoff_debug_swap \
4779 &elf64_alpha_ecoff_debug_swap
4781 #define elf_backend_size_info \
4782 alpha_elf_size_info
4784 /* A few constants that determine how the .plt section is set up. */
4785 #define elf_backend_want_got_plt 0
4786 #define elf_backend_plt_readonly 0
4787 #define elf_backend_want_plt_sym 1
4788 #define elf_backend_got_header_size 0
4789 #define elf_backend_plt_header_size PLT_HEADER_SIZE
4791 #include "elf64-target.h"