°
[binutils.git] / bfd / elf64-alpha.c
bloba8a7c4054e16490e19df7fcd904c482c7fd5f14a
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 boolean elf64_alpha_mkobject PARAMS ((bfd *));
50 static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
51 PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
52 static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
53 PARAMS((bfd *));
55 static bfd_reloc_status_type elf64_alpha_reloc_nil
56 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
57 static bfd_reloc_status_type elf64_alpha_reloc_bad
58 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
60 PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
61 static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
62 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
64 static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
65 PARAMS((bfd *, bfd_reloc_code_real_type));
66 static void elf64_alpha_info_to_howto
67 PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
69 static boolean elf64_alpha_object_p
70 PARAMS((bfd *));
71 static boolean elf64_alpha_section_from_shdr
72 PARAMS((bfd *, Elf64_Internal_Shdr *, char *));
73 static boolean elf64_alpha_fake_sections
74 PARAMS((bfd *, Elf64_Internal_Shdr *, asection *));
75 static boolean elf64_alpha_create_got_section
76 PARAMS((bfd *, struct bfd_link_info *));
77 static boolean elf64_alpha_create_dynamic_sections
78 PARAMS((bfd *, struct bfd_link_info *));
80 static boolean elf64_alpha_read_ecoff_info
81 PARAMS((bfd *, asection *, struct ecoff_debug_info *));
82 static boolean elf64_alpha_is_local_label_name
83 PARAMS((bfd *, const char *));
84 static boolean elf64_alpha_find_nearest_line
85 PARAMS((bfd *, asection *, asymbol **, bfd_vma, const char **,
86 const char **, unsigned int *));
88 #if defined(__STDC__) || defined(ALMOST_STDC)
89 struct alpha_elf_link_hash_entry;
90 #endif
92 static boolean elf64_alpha_output_extsym
93 PARAMS((struct alpha_elf_link_hash_entry *, PTR));
95 static boolean elf64_alpha_can_merge_gots
96 PARAMS((bfd *, bfd *));
97 static void elf64_alpha_merge_gots
98 PARAMS((bfd *, bfd *));
99 static boolean elf64_alpha_calc_got_offsets_for_symbol
100 PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
101 static void elf64_alpha_calc_got_offsets PARAMS ((struct bfd_link_info *));
102 static boolean elf64_alpha_size_got_sections
103 PARAMS ((bfd *, struct bfd_link_info *));
104 static boolean elf64_alpha_always_size_sections
105 PARAMS ((bfd *, struct bfd_link_info *));
106 static boolean elf64_alpha_calc_dynrel_sizes
107 PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
108 static boolean elf64_alpha_add_symbol_hook
109 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
110 const char **, flagword *, asection **, bfd_vma *));
111 static boolean elf64_alpha_check_relocs
112 PARAMS((bfd *, struct bfd_link_info *, asection *sec,
113 const Elf_Internal_Rela *));
114 static boolean elf64_alpha_adjust_dynamic_symbol
115 PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
116 static boolean elf64_alpha_size_dynamic_sections
117 PARAMS((bfd *, struct bfd_link_info *));
118 static boolean elf64_alpha_adjust_dynindx
119 PARAMS((struct elf_link_hash_entry *, PTR));
120 static boolean elf64_alpha_relocate_section
121 PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
122 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
123 static boolean elf64_alpha_finish_dynamic_symbol
124 PARAMS((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
125 Elf_Internal_Sym *));
126 static boolean elf64_alpha_finish_dynamic_sections
127 PARAMS((bfd *, struct bfd_link_info *));
128 static boolean elf64_alpha_final_link
129 PARAMS((bfd *, struct bfd_link_info *));
132 struct alpha_elf_link_hash_entry
134 struct elf_link_hash_entry root;
136 /* External symbol information. */
137 EXTR esym;
139 /* Cumulative flags for all the .got entries. */
140 int flags;
142 /* Contexts (LITUSE) in which a literal was referenced. */
143 #define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
144 #define ALPHA_ELF_LINK_HASH_LU_MEM 0x02
145 #define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
146 #define ALPHA_ELF_LINK_HASH_LU_FUNC 0x08
148 /* Used to implement multiple .got subsections. */
149 struct alpha_elf_got_entry
151 struct alpha_elf_got_entry *next;
153 /* which .got subsection? */
154 bfd *gotobj;
156 /* the addend in effect for this entry. */
157 bfd_vma addend;
159 /* the .got offset for this entry. */
160 int got_offset;
162 int flags;
164 /* An additional flag. */
165 #define ALPHA_ELF_GOT_ENTRY_RELOCS_DONE 0x10
167 int use_count;
168 } *got_entries;
170 /* used to count non-got, non-plt relocations for delayed sizing
171 of relocation sections. */
172 struct alpha_elf_reloc_entry
174 struct alpha_elf_reloc_entry *next;
176 /* which .reloc section? */
177 asection *srel;
179 /* what kind of relocation? */
180 unsigned long rtype;
182 /* how many did we find? */
183 unsigned long count;
184 } *reloc_entries;
187 /* Alpha ELF linker hash table. */
189 struct alpha_elf_link_hash_table
191 struct elf_link_hash_table root;
193 /* The head of a list of .got subsections linked through
194 alpha_elf_tdata(abfd)->got_link_next. */
195 bfd *got_list;
198 /* Look up an entry in a Alpha ELF linker hash table. */
200 #define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
201 ((struct alpha_elf_link_hash_entry *) \
202 elf_link_hash_lookup (&(table)->root, (string), (create), \
203 (copy), (follow)))
205 /* Traverse a Alpha ELF linker hash table. */
207 #define alpha_elf_link_hash_traverse(table, func, info) \
208 (elf_link_hash_traverse \
209 (&(table)->root, \
210 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
211 (info)))
213 /* Get the Alpha ELF linker hash table from a link_info structure. */
215 #define alpha_elf_hash_table(p) \
216 ((struct alpha_elf_link_hash_table *) ((p)->hash))
218 /* Get the object's symbols as our own entry type. */
220 #define alpha_elf_sym_hashes(abfd) \
221 ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
223 /* Should we do dynamic things to this symbol? */
225 #define alpha_elf_dynamic_symbol_p(h, info) \
226 ((((info)->shared && !(info)->symbolic) \
227 || (((h)->elf_link_hash_flags \
228 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)) \
229 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)) \
230 || (h)->root.type == bfd_link_hash_undefweak \
231 || (h)->root.type == bfd_link_hash_defweak) \
232 && (h)->dynindx != -1)
234 /* Create an entry in a Alpha ELF linker hash table. */
236 static struct bfd_hash_entry *
237 elf64_alpha_link_hash_newfunc (entry, table, string)
238 struct bfd_hash_entry *entry;
239 struct bfd_hash_table *table;
240 const char *string;
242 struct alpha_elf_link_hash_entry *ret =
243 (struct alpha_elf_link_hash_entry *) entry;
245 /* Allocate the structure if it has not already been allocated by a
246 subclass. */
247 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
248 ret = ((struct alpha_elf_link_hash_entry *)
249 bfd_hash_allocate (table,
250 sizeof (struct alpha_elf_link_hash_entry)));
251 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
252 return (struct bfd_hash_entry *) ret;
254 /* Call the allocation method of the superclass. */
255 ret = ((struct alpha_elf_link_hash_entry *)
256 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
257 table, string));
258 if (ret != (struct alpha_elf_link_hash_entry *) NULL)
260 /* Set local fields. */
261 memset (&ret->esym, 0, sizeof (EXTR));
262 /* We use -2 as a marker to indicate that the information has
263 not been set. -1 means there is no associated ifd. */
264 ret->esym.ifd = -2;
265 ret->flags = 0;
266 ret->got_entries = NULL;
267 ret->reloc_entries = NULL;
270 return (struct bfd_hash_entry *) ret;
273 /* Create a Alpha ELF linker hash table. */
275 static struct bfd_link_hash_table *
276 elf64_alpha_bfd_link_hash_table_create (abfd)
277 bfd *abfd;
279 struct alpha_elf_link_hash_table *ret;
281 ret = ((struct alpha_elf_link_hash_table *)
282 bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
283 if (ret == (struct alpha_elf_link_hash_table *) NULL)
284 return NULL;
286 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
287 elf64_alpha_link_hash_newfunc))
289 bfd_release (abfd, ret);
290 return NULL;
293 return &ret->root.root;
296 /* We have some private fields hanging off of the elf_tdata structure. */
298 struct alpha_elf_obj_tdata
300 struct elf_obj_tdata root;
302 /* For every input file, these are the got entries for that object's
303 local symbols. */
304 struct alpha_elf_got_entry ** local_got_entries;
306 /* For every input file, this is the object that owns the got that
307 this input file uses. */
308 bfd *gotobj;
310 /* For every got, this is a linked list through the objects using this got */
311 bfd *in_got_link_next;
313 /* For every got, this is a link to the next got subsegment. */
314 bfd *got_link_next;
316 /* For every got, this is the section. */
317 asection *got;
319 /* For every got, this is it's total number of *entries*. */
320 int total_got_entries;
322 /* For every got, this is the sum of the number of *entries* required
323 to hold all of the member object's local got. */
324 int n_local_got_entries;
327 #define alpha_elf_tdata(abfd) \
328 ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
330 static boolean
331 elf64_alpha_mkobject (abfd)
332 bfd *abfd;
334 abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
335 if (abfd->tdata.any == NULL)
336 return false;
337 return true;
340 static boolean
341 elf64_alpha_object_p (abfd)
342 bfd *abfd;
344 /* Allocate our special target data. */
345 struct alpha_elf_obj_tdata *new_tdata;
346 new_tdata = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
347 if (new_tdata == NULL)
348 return false;
349 new_tdata->root = *abfd->tdata.elf_obj_data;
350 abfd->tdata.any = new_tdata;
352 /* Set the right machine number for an Alpha ELF file. */
353 return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
356 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
357 from smaller values. Start with zero, widen, *then* decrement. */
358 #define MINUS_ONE (((bfd_vma)0) - 1)
360 static reloc_howto_type elf64_alpha_howto_table[] =
362 HOWTO (R_ALPHA_NONE, /* type */
363 0, /* rightshift */
364 0, /* size (0 = byte, 1 = short, 2 = long) */
365 8, /* bitsize */
366 true, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_dont, /* complain_on_overflow */
369 elf64_alpha_reloc_nil, /* special_function */
370 "NONE", /* name */
371 false, /* partial_inplace */
372 0, /* src_mask */
373 0, /* dst_mask */
374 true), /* pcrel_offset */
376 /* A 32 bit reference to a symbol. */
377 HOWTO (R_ALPHA_REFLONG, /* type */
378 0, /* rightshift */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
380 32, /* bitsize */
381 false, /* pc_relative */
382 0, /* bitpos */
383 complain_overflow_bitfield, /* complain_on_overflow */
384 0, /* special_function */
385 "REFLONG", /* name */
386 false, /* partial_inplace */
387 0xffffffff, /* src_mask */
388 0xffffffff, /* dst_mask */
389 false), /* pcrel_offset */
391 /* A 64 bit reference to a symbol. */
392 HOWTO (R_ALPHA_REFQUAD, /* type */
393 0, /* rightshift */
394 4, /* size (0 = byte, 1 = short, 2 = long) */
395 64, /* bitsize */
396 false, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_bitfield, /* complain_on_overflow */
399 0, /* special_function */
400 "REFQUAD", /* name */
401 false, /* partial_inplace */
402 MINUS_ONE, /* src_mask */
403 MINUS_ONE, /* dst_mask */
404 false), /* pcrel_offset */
406 /* A 32 bit GP relative offset. This is just like REFLONG except
407 that when the value is used the value of the gp register will be
408 added in. */
409 HOWTO (R_ALPHA_GPREL32, /* type */
410 0, /* rightshift */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
412 32, /* bitsize */
413 false, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_bitfield, /* complain_on_overflow */
416 0, /* special_function */
417 "GPREL32", /* name */
418 false, /* partial_inplace */
419 0xffffffff, /* src_mask */
420 0xffffffff, /* dst_mask */
421 false), /* pcrel_offset */
423 /* Used for an instruction that refers to memory off the GP register. */
424 HOWTO (R_ALPHA_LITERAL, /* type */
425 0, /* rightshift */
426 2, /* size (0 = byte, 1 = short, 2 = long) */
427 16, /* bitsize */
428 false, /* pc_relative */
429 0, /* bitpos */
430 complain_overflow_signed, /* complain_on_overflow */
431 0, /* special_function */
432 "ELF_LITERAL", /* name */
433 false, /* partial_inplace */
434 0xffff, /* src_mask */
435 0xffff, /* dst_mask */
436 false), /* pcrel_offset */
438 /* This reloc only appears immediately following an ELF_LITERAL reloc.
439 It identifies a use of the literal. The symbol index is special:
440 1 means the literal address is in the base register of a memory
441 format instruction; 2 means the literal address is in the byte
442 offset register of a byte-manipulation instruction; 3 means the
443 literal address is in the target register of a jsr instruction.
444 This does not actually do any relocation. */
445 HOWTO (R_ALPHA_LITUSE, /* 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_dont, /* complain_on_overflow */
452 elf64_alpha_reloc_nil, /* special_function */
453 "LITUSE", /* name */
454 false, /* partial_inplace */
455 0, /* src_mask */
456 0, /* dst_mask */
457 false), /* pcrel_offset */
459 /* Load the gp register. This is always used for a ldah instruction
460 which loads the upper 16 bits of the gp register. The symbol
461 index of the GPDISP instruction is an offset in bytes to the lda
462 instruction that loads the lower 16 bits. The value to use for
463 the relocation is the difference between the GP value and the
464 current location; the load will always be done against a register
465 holding the current address.
467 NOTE: Unlike ECOFF, partial in-place relocation is not done. If
468 any offset is present in the instructions, it is an offset from
469 the register to the ldah instruction. This lets us avoid any
470 stupid hackery like inventing a gp value to do partial relocation
471 against. Also unlike ECOFF, we do the whole relocation off of
472 the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
473 space consuming bit, that, since all the information was present
474 in the GPDISP_HI16 reloc. */
475 HOWTO (R_ALPHA_GPDISP, /* type */
476 16, /* rightshift */
477 2, /* size (0 = byte, 1 = short, 2 = long) */
478 16, /* bitsize */
479 false, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont, /* complain_on_overflow */
482 elf64_alpha_reloc_gpdisp, /* special_function */
483 "GPDISP", /* name */
484 false, /* partial_inplace */
485 0xffff, /* src_mask */
486 0xffff, /* dst_mask */
487 true), /* pcrel_offset */
489 /* A 21 bit branch. */
490 HOWTO (R_ALPHA_BRADDR, /* type */
491 2, /* rightshift */
492 2, /* size (0 = byte, 1 = short, 2 = long) */
493 21, /* bitsize */
494 true, /* pc_relative */
495 0, /* bitpos */
496 complain_overflow_signed, /* complain_on_overflow */
497 0, /* special_function */
498 "BRADDR", /* name */
499 false, /* partial_inplace */
500 0x1fffff, /* src_mask */
501 0x1fffff, /* dst_mask */
502 true), /* pcrel_offset */
504 /* A hint for a jump to a register. */
505 HOWTO (R_ALPHA_HINT, /* type */
506 2, /* rightshift */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
508 14, /* bitsize */
509 true, /* pc_relative */
510 0, /* bitpos */
511 complain_overflow_dont, /* complain_on_overflow */
512 0, /* special_function */
513 "HINT", /* name */
514 false, /* partial_inplace */
515 0x3fff, /* src_mask */
516 0x3fff, /* dst_mask */
517 true), /* pcrel_offset */
519 /* 16 bit PC relative offset. */
520 HOWTO (R_ALPHA_SREL16, /* type */
521 0, /* rightshift */
522 1, /* size (0 = byte, 1 = short, 2 = long) */
523 16, /* bitsize */
524 true, /* pc_relative */
525 0, /* bitpos */
526 complain_overflow_signed, /* complain_on_overflow */
527 0, /* special_function */
528 "SREL16", /* name */
529 false, /* partial_inplace */
530 0xffff, /* src_mask */
531 0xffff, /* dst_mask */
532 false), /* pcrel_offset */
534 /* 32 bit PC relative offset. */
535 HOWTO (R_ALPHA_SREL32, /* type */
536 0, /* rightshift */
537 2, /* size (0 = byte, 1 = short, 2 = long) */
538 32, /* bitsize */
539 true, /* pc_relative */
540 0, /* bitpos */
541 complain_overflow_signed, /* complain_on_overflow */
542 0, /* special_function */
543 "SREL32", /* name */
544 false, /* partial_inplace */
545 0xffffffff, /* src_mask */
546 0xffffffff, /* dst_mask */
547 false), /* pcrel_offset */
549 /* A 64 bit PC relative offset. */
550 HOWTO (R_ALPHA_SREL64, /* type */
551 0, /* rightshift */
552 4, /* size (0 = byte, 1 = short, 2 = long) */
553 64, /* bitsize */
554 true, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_signed, /* complain_on_overflow */
557 0, /* special_function */
558 "SREL64", /* name */
559 false, /* partial_inplace */
560 MINUS_ONE, /* src_mask */
561 MINUS_ONE, /* dst_mask */
562 false), /* pcrel_offset */
564 /* Push a value on the reloc evaluation stack. */
565 /* Not implemented -- it's dumb. */
566 HOWTO (R_ALPHA_OP_PUSH, /* type */
567 0, /* rightshift */
568 0, /* size (0 = byte, 1 = short, 2 = long) */
569 0, /* bitsize */
570 false, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_dont, /* complain_on_overflow */
573 elf64_alpha_reloc_bad, /* special_function */
574 "OP_PUSH", /* name */
575 false, /* partial_inplace */
576 0, /* src_mask */
577 0, /* dst_mask */
578 false), /* pcrel_offset */
580 /* Store the value from the stack at the given address. Store it in
581 a bitfield of size r_size starting at bit position r_offset. */
582 /* Not implemented -- it's dumb. */
583 HOWTO (R_ALPHA_OP_STORE, /* type */
584 0, /* rightshift */
585 4, /* size (0 = byte, 1 = short, 2 = long) */
586 64, /* bitsize */
587 false, /* pc_relative */
588 0, /* bitpos */
589 complain_overflow_dont, /* complain_on_overflow */
590 elf64_alpha_reloc_bad, /* special_function */
591 "OP_STORE", /* name */
592 false, /* partial_inplace */
593 0, /* src_mask */
594 MINUS_ONE, /* dst_mask */
595 false), /* pcrel_offset */
597 /* Subtract the reloc address from the value on the top of the
598 relocation stack. */
599 /* Not implemented -- it's dumb. */
600 HOWTO (R_ALPHA_OP_PSUB, /* type */
601 0, /* rightshift */
602 0, /* size (0 = byte, 1 = short, 2 = long) */
603 0, /* bitsize */
604 false, /* pc_relative */
605 0, /* bitpos */
606 complain_overflow_dont, /* complain_on_overflow */
607 elf64_alpha_reloc_bad, /* special_function */
608 "OP_PSUB", /* name */
609 false, /* partial_inplace */
610 0, /* src_mask */
611 0, /* dst_mask */
612 false), /* pcrel_offset */
614 /* Shift the value on the top of the relocation stack right by the
615 given value. */
616 /* Not implemented -- it's dumb. */
617 HOWTO (R_ALPHA_OP_PRSHIFT, /* type */
618 0, /* rightshift */
619 0, /* size (0 = byte, 1 = short, 2 = long) */
620 0, /* bitsize */
621 false, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_dont, /* complain_on_overflow */
624 elf64_alpha_reloc_bad, /* special_function */
625 "OP_PRSHIFT", /* name */
626 false, /* partial_inplace */
627 0, /* src_mask */
628 0, /* dst_mask */
629 false), /* pcrel_offset */
631 /* Change the value of GP used by +r_addend until the next GPVALUE or the
632 end of the input bfd. */
633 /* Not implemented -- it's dumb. */
634 HOWTO (R_ALPHA_GPVALUE,
635 0, /* rightshift */
636 0, /* size (0 = byte, 1 = short, 2 = long) */
637 0, /* bitsize */
638 false, /* pc_relative */
639 0, /* bitpos */
640 complain_overflow_dont, /* complain_on_overflow */
641 elf64_alpha_reloc_bad, /* special_function */
642 "GPVALUE", /* name */
643 false, /* partial_inplace */
644 0, /* src_mask */
645 0, /* dst_mask */
646 false), /* pcrel_offset */
648 /* The high 16 bits of the displacement from GP to the target. */
649 HOWTO (R_ALPHA_GPRELHIGH,
650 0, /* rightshift */
651 2, /* size (0 = byte, 1 = short, 2 = long) */
652 16, /* bitsize */
653 false, /* pc_relative */
654 0, /* bitpos */
655 complain_overflow_signed, /* complain_on_overflow */
656 elf64_alpha_reloc_bad, /* special_function */
657 "GPRELHIGH", /* name */
658 false, /* partial_inplace */
659 0xffff, /* src_mask */
660 0xffff, /* dst_mask */
661 false), /* pcrel_offset */
663 /* The low 16 bits of the displacement from GP to the target. */
664 HOWTO (R_ALPHA_GPRELLOW,
665 0, /* rightshift */
666 2, /* size (0 = byte, 1 = short, 2 = long) */
667 16, /* bitsize */
668 false, /* pc_relative */
669 0, /* bitpos */
670 complain_overflow_dont, /* complain_on_overflow */
671 elf64_alpha_reloc_bad, /* special_function */
672 "GPRELLOW", /* name */
673 false, /* partial_inplace */
674 0xffff, /* src_mask */
675 0xffff, /* dst_mask */
676 false), /* pcrel_offset */
678 /* A 16-bit displacement from the GP to the target. */
679 /* XXX: Not implemented. */
680 HOWTO (R_ALPHA_IMMED_GP_16,
681 0, /* rightshift */
682 2, /* size (0 = byte, 1 = short, 2 = long) */
683 16, /* bitsize */
684 false, /* pc_relative */
685 0, /* bitpos */
686 complain_overflow_signed, /* complain_on_overflow */
687 0, /* special_function */
688 "IMMED_GP_16", /* name */
689 false, /* partial_inplace */
690 0xffff, /* src_mask */
691 0xffff, /* dst_mask */
692 false), /* pcrel_offset */
694 /* The high bits of a 32-bit displacement from the GP to the target; the
695 low bits are supplied in the subsequent R_ALPHA_IMMED_LO32 relocs. */
696 /* XXX: Not implemented. */
697 HOWTO (R_ALPHA_IMMED_GP_HI32,
698 0, /* rightshift */
699 0, /* size (0 = byte, 1 = short, 2 = long) */
700 0, /* bitsize */
701 false, /* pc_relative */
702 0, /* bitpos */
703 complain_overflow_dont, /* complain_on_overflow */
704 elf64_alpha_reloc_bad, /* special_function */
705 "IMMED_GP_HI32", /* name */
706 false, /* partial_inplace */
707 0, /* src_mask */
708 0, /* dst_mask */
709 false), /* pcrel_offset */
711 /* The high bits of a 32-bit displacement to the starting address of the
712 current section (the relocation target is ignored); the low bits are
713 supplied in the subsequent R_ALPHA_IMMED_LO32 relocs. */
714 /* XXX: Not implemented. */
715 HOWTO (R_ALPHA_IMMED_SCN_HI32,
716 0, /* rightshift */
717 0, /* size (0 = byte, 1 = short, 2 = long) */
718 0, /* bitsize */
719 false, /* pc_relative */
720 0, /* bitpos */
721 complain_overflow_dont, /* complain_on_overflow */
722 elf64_alpha_reloc_bad, /* special_function */
723 "IMMED_SCN_HI32", /* name */
724 false, /* partial_inplace */
725 0, /* src_mask */
726 0, /* dst_mask */
727 false), /* pcrel_offset */
729 /* The high bits of a 32-bit displacement from the previous br, bsr, jsr
730 or jmp insn (as tagged by a BRADDR or HINT reloc) to the target; the
731 low bits are supplied by subsequent R_ALPHA_IMMED_LO32 relocs. */
732 /* XXX: Not implemented. */
733 HOWTO (R_ALPHA_IMMED_BR_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_BR_HI32", /* name */
742 false, /* partial_inplace */
743 0, /* src_mask */
744 0, /* dst_mask */
745 false), /* pcrel_offset */
747 /* The low 16 bits of a displacement calculated in a previous HI32 reloc. */
748 /* XXX: Not implemented. */
749 HOWTO (R_ALPHA_IMMED_LO32,
750 0, /* rightshift */
751 0, /* size (0 = byte, 1 = short, 2 = long) */
752 0, /* bitsize */
753 false, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_dont, /* complain_on_overflow */
756 elf64_alpha_reloc_bad, /* special_function */
757 "IMMED_LO32", /* name */
758 false, /* partial_inplace */
759 0, /* src_mask */
760 0, /* dst_mask */
761 false), /* pcrel_offset */
763 /* Misc ELF relocations. */
765 /* A dynamic relocation to copy the target into our .dynbss section. */
766 /* Not generated, as all Alpha objects use PIC, so it is not needed. It
767 is present because every other ELF has one, but should not be used
768 because .dynbss is an ugly thing. */
769 HOWTO (R_ALPHA_COPY,
773 false,
775 complain_overflow_dont,
776 bfd_elf_generic_reloc,
777 "COPY",
778 false,
781 true),
783 /* A dynamic relocation for a .got entry. */
784 HOWTO (R_ALPHA_GLOB_DAT,
788 false,
790 complain_overflow_dont,
791 bfd_elf_generic_reloc,
792 "GLOB_DAT",
793 false,
796 true),
798 /* A dynamic relocation for a .plt entry. */
799 HOWTO (R_ALPHA_JMP_SLOT,
803 false,
805 complain_overflow_dont,
806 bfd_elf_generic_reloc,
807 "JMP_SLOT",
808 false,
811 true),
813 /* A dynamic relocation to add the base of the DSO to a 64-bit field. */
814 HOWTO (R_ALPHA_RELATIVE,
818 false,
820 complain_overflow_dont,
821 bfd_elf_generic_reloc,
822 "RELATIVE",
823 false,
826 true)
829 /* A relocation function which doesn't do anything. */
831 static bfd_reloc_status_type
832 elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
833 bfd *abfd;
834 arelent *reloc;
835 asymbol *sym;
836 PTR data;
837 asection *sec;
838 bfd *output_bfd;
839 char **error_message;
841 if (output_bfd)
842 reloc->address += sec->output_offset;
843 return bfd_reloc_ok;
846 /* A relocation function used for an unsupported reloc. */
848 static bfd_reloc_status_type
849 elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
850 bfd *abfd;
851 arelent *reloc;
852 asymbol *sym;
853 PTR data;
854 asection *sec;
855 bfd *output_bfd;
856 char **error_message;
858 if (output_bfd)
859 reloc->address += sec->output_offset;
860 return bfd_reloc_notsupported;
863 /* Do the work of the GPDISP relocation. */
865 static bfd_reloc_status_type
866 elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
867 bfd *abfd;
868 bfd_vma gpdisp;
869 bfd_byte *p_ldah;
870 bfd_byte *p_lda;
872 bfd_reloc_status_type ret = bfd_reloc_ok;
873 bfd_vma addend;
874 unsigned long i_ldah, i_lda;
876 i_ldah = bfd_get_32 (abfd, p_ldah);
877 i_lda = bfd_get_32 (abfd, p_lda);
879 /* Complain if the instructions are not correct. */
880 if (((i_ldah >> 26) & 0x3f) != 0x09
881 || ((i_lda >> 26) & 0x3f) != 0x08)
882 ret = bfd_reloc_dangerous;
884 /* Extract the user-supplied offset, mirroring the sign extensions
885 that the instructions perform. */
886 addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
887 addend = (addend ^ 0x80008000) - 0x80008000;
889 gpdisp += addend;
891 if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
892 || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
893 ret = bfd_reloc_overflow;
895 /* compensate for the sign extension again. */
896 i_ldah = ((i_ldah & 0xffff0000)
897 | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
898 i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
900 bfd_put_32 (abfd, i_ldah, p_ldah);
901 bfd_put_32 (abfd, i_lda, p_lda);
903 return ret;
906 /* The special function for the GPDISP reloc. */
908 static bfd_reloc_status_type
909 elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
910 output_bfd, err_msg)
911 bfd *abfd;
912 arelent *reloc_entry;
913 asymbol *sym;
914 PTR data;
915 asection *input_section;
916 bfd *output_bfd;
917 char **err_msg;
919 bfd_reloc_status_type ret;
920 bfd_vma gp, relocation;
921 bfd_byte *p_ldah, *p_lda;
923 /* Don't do anything if we're not doing a final link. */
924 if (output_bfd)
926 reloc_entry->address += input_section->output_offset;
927 return bfd_reloc_ok;
930 if (reloc_entry->address > input_section->_cooked_size ||
931 reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
932 return bfd_reloc_outofrange;
934 /* The gp used in the portion of the output object to which this
935 input object belongs is cached on the input bfd. */
936 gp = _bfd_get_gp_value (abfd);
938 relocation = (input_section->output_section->vma
939 + input_section->output_offset
940 + reloc_entry->address);
942 p_ldah = (bfd_byte *) data + reloc_entry->address;
943 p_lda = p_ldah + reloc_entry->addend;
945 ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
947 /* Complain if the instructions are not correct. */
948 if (ret == bfd_reloc_dangerous)
949 *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
951 return ret;
954 /* A mapping from BFD reloc types to Alpha ELF reloc types. */
956 struct elf_reloc_map
958 bfd_reloc_code_real_type bfd_reloc_val;
959 int elf_reloc_val;
962 static const struct elf_reloc_map elf64_alpha_reloc_map[] =
964 {BFD_RELOC_NONE, R_ALPHA_NONE},
965 {BFD_RELOC_32, R_ALPHA_REFLONG},
966 {BFD_RELOC_64, R_ALPHA_REFQUAD},
967 {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
968 {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
969 {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
970 {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
971 {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
972 {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
973 {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
974 {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
975 {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
976 {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
979 /* Given a BFD reloc type, return a HOWTO structure. */
981 static reloc_howto_type *
982 elf64_alpha_bfd_reloc_type_lookup (abfd, code)
983 bfd *abfd;
984 bfd_reloc_code_real_type code;
986 const struct elf_reloc_map *i, *e;
987 i = e = elf64_alpha_reloc_map;
988 e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
989 for (; i != e; ++i)
991 if (i->bfd_reloc_val == code)
992 return &elf64_alpha_howto_table[i->elf_reloc_val];
994 return 0;
997 /* Given an Alpha ELF reloc type, fill in an arelent structure. */
999 static void
1000 elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
1001 bfd *abfd;
1002 arelent *cache_ptr;
1003 Elf64_Internal_Rela *dst;
1005 unsigned r_type;
1007 r_type = ELF64_R_TYPE(dst->r_info);
1008 BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1009 cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1012 /* These functions do relaxation for Alpha ELF.
1014 Currently I'm only handling what I can do with existing compiler
1015 and assembler support, which means no instructions are removed,
1016 though some may be nopped. At this time GCC does not emit enough
1017 information to do all of the relaxing that is possible. It will
1018 take some not small amount of work for that to happen.
1020 There are a couple of interesting papers that I once read on this
1021 subject, that I cannot find references to at the moment, that
1022 related to Alpha in particular. They are by David Wall, then of
1023 DEC WRL. */
1025 #define OP_LDA 0x08
1026 #define OP_LDAH 0x09
1027 #define INSN_JSR 0x68004000
1028 #define INSN_JSR_MASK 0xfc00c000
1029 #define OP_LDQ 0x29
1030 #define OP_BR 0x30
1031 #define OP_BSR 0x34
1032 #define INSN_UNOP 0x2fe00000
1034 struct alpha_relax_info
1036 bfd *abfd;
1037 asection *sec;
1038 bfd_byte *contents;
1039 Elf_Internal_Rela *relocs, *relend;
1040 struct bfd_link_info *link_info;
1041 boolean changed_contents;
1042 boolean changed_relocs;
1043 bfd_vma gp;
1044 bfd *gotobj;
1045 asection *tsec;
1046 struct alpha_elf_link_hash_entry *h;
1047 struct alpha_elf_got_entry *gotent;
1048 unsigned char other;
1051 static Elf_Internal_Rela * elf64_alpha_relax_with_lituse
1052 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1053 Elf_Internal_Rela *irel, Elf_Internal_Rela *irelend));
1055 static boolean elf64_alpha_relax_without_lituse
1056 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1057 Elf_Internal_Rela *irel));
1059 static bfd_vma elf64_alpha_relax_opt_call
1060 PARAMS((struct alpha_relax_info *info, bfd_vma symval));
1062 static boolean elf64_alpha_relax_section
1063 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
1064 boolean *again));
1066 static Elf_Internal_Rela *
1067 elf64_alpha_find_reloc_at_ofs (rel, relend, offset, type)
1068 Elf_Internal_Rela *rel, *relend;
1069 bfd_vma offset;
1070 int type;
1072 while (rel < relend)
1074 if (rel->r_offset == offset && ELF64_R_TYPE (rel->r_info) == type)
1075 return rel;
1076 ++rel;
1078 return NULL;
1081 static Elf_Internal_Rela *
1082 elf64_alpha_relax_with_lituse (info, symval, irel, irelend)
1083 struct alpha_relax_info *info;
1084 bfd_vma symval;
1085 Elf_Internal_Rela *irel, *irelend;
1087 Elf_Internal_Rela *urel;
1088 int flags, count, i;
1089 bfd_signed_vma disp;
1090 boolean fits16;
1091 boolean fits32;
1092 boolean lit_reused = false;
1093 boolean all_optimized = true;
1094 unsigned int lit_insn;
1096 lit_insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1097 if (lit_insn >> 26 != OP_LDQ)
1099 ((*_bfd_error_handler)
1100 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1101 bfd_get_filename (info->abfd), info->sec->name,
1102 (unsigned long)irel->r_offset));
1103 return irel;
1106 /* Summarize how this particular LITERAL is used. */
1107 for (urel = irel+1, flags = count = 0; urel < irelend; ++urel, ++count)
1109 if (ELF64_R_TYPE (urel->r_info) != R_ALPHA_LITUSE)
1110 break;
1111 if (urel->r_addend >= 0 && urel->r_addend <= 3)
1112 flags |= 1 << urel->r_addend;
1115 /* A little preparation for the loop... */
1116 disp = symval - info->gp;
1117 fits16 = (disp >= -(bfd_signed_vma)0x8000 && disp < 0x8000);
1118 fits32 = (disp >= -(bfd_signed_vma)0x80000000 && disp < 0x7fff8000);
1120 for (urel = irel+1, i = 0; i < count; ++i, ++urel)
1122 unsigned int insn;
1123 insn = bfd_get_32 (info->abfd, info->contents + urel->r_offset);
1125 switch (urel->r_addend)
1127 default: /* 0 = ADDRESS FORMAT */
1128 /* This type is really just a placeholder to note that all
1129 uses cannot be optimized, but to still allow some. */
1130 all_optimized = false;
1131 break;
1133 case 1: /* MEM FORMAT */
1134 /* We can always optimize 16-bit displacements. */
1135 if (fits16)
1137 /* FIXME: sanity check the insn for mem format with
1138 zero addend. */
1140 /* Take the op code and dest from this insn, take the base
1141 register from the literal insn. Leave the offset alone. */
1142 insn = (insn & 0xffe00000) | (lit_insn & 0x001f0000);
1143 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1144 R_ALPHA_GPRELLOW);
1145 urel->r_addend = irel->r_addend;
1146 info->changed_relocs = true;
1148 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1149 info->changed_contents = true;
1152 /* If all mem+byte, we can optimize 32-bit mem displacements. */
1153 else if (fits32 && !(flags & ~6))
1155 /* FIXME: sanity check that lit insn Ra is mem insn Rb, and
1156 that mem_insn disp is zero. */
1158 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1159 R_ALPHA_GPRELHIGH);
1160 lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
1161 bfd_put_32 (info->abfd, lit_insn,
1162 info->contents + irel->r_offset);
1163 lit_reused = true;
1164 info->changed_contents = true;
1166 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1167 R_ALPHA_GPRELLOW);
1168 urel->r_addend = irel->r_addend;
1169 info->changed_relocs = true;
1171 else
1172 all_optimized = false;
1173 break;
1175 case 2: /* BYTE OFFSET FORMAT */
1176 /* We can always optimize byte instructions. */
1178 /* FIXME: sanity check the insn for byte op. Check that the
1179 literal dest reg is indeed Rb in the byte insn. */
1181 insn = (insn & ~0x001ff000) | ((symval & 7) << 13) | 0x1000;
1183 urel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1184 urel->r_addend = 0;
1185 info->changed_relocs = true;
1187 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1188 info->changed_contents = true;
1189 break;
1191 case 3: /* CALL FORMAT */
1193 /* If not zero, place to jump without needing pv. */
1194 bfd_vma optdest = elf64_alpha_relax_opt_call (info, symval);
1195 bfd_vma org = (info->sec->output_section->vma
1196 + info->sec->output_offset
1197 + urel->r_offset + 4);
1198 bfd_signed_vma odisp;
1200 odisp = (optdest ? optdest : symval) - org;
1201 if (odisp >= -0x400000 && odisp < 0x400000)
1203 Elf_Internal_Rela *xrel;
1205 /* Preserve branch prediction call stack when possible. */
1206 if ((insn & INSN_JSR_MASK) == INSN_JSR)
1207 insn = (OP_BSR << 26) | (insn & 0x03e00000);
1208 else
1209 insn = (OP_BR << 26) | (insn & 0x03e00000);
1211 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1212 R_ALPHA_BRADDR);
1213 urel->r_addend = irel->r_addend;
1215 if (optdest)
1216 urel->r_addend += optdest - symval;
1217 else
1218 all_optimized = false;
1220 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1222 /* Kill any HINT reloc that might exist for this insn. */
1223 xrel = (elf64_alpha_find_reloc_at_ofs
1224 (info->relocs, info->relend, urel->r_offset,
1225 R_ALPHA_HINT));
1226 if (xrel)
1227 xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1229 info->changed_contents = true;
1230 info->changed_relocs = true;
1232 else
1233 all_optimized = false;
1235 /* ??? If target gp == current gp we can eliminate the gp reload.
1236 This does depend on every place a gp could be reloaded will
1237 be, which currently happens for all code produced by gcc, but
1238 not necessarily by hand-coded assembly, or if sibling calls
1239 are enabled in gcc.
1241 Perhaps conditionalize this on a flag being set in the target
1242 object file's header, and have gcc set it? */
1244 break;
1248 /* If all cases were optimized, we can reduce the use count on this
1249 got entry by one, possibly eliminating it. */
1250 if (all_optimized)
1252 info->gotent->use_count -= 1;
1253 alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1254 if (!info->h)
1255 alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1257 /* If the literal instruction is no longer needed (it may have been
1258 reused. We can eliminate it.
1259 ??? For now, I don't want to deal with compacting the section,
1260 so just nop it out. */
1261 if (!lit_reused)
1263 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1264 info->changed_relocs = true;
1266 bfd_put_32 (info->abfd, INSN_UNOP, info->contents + irel->r_offset);
1267 info->changed_contents = true;
1271 return irel + count;
1274 static bfd_vma
1275 elf64_alpha_relax_opt_call (info, symval)
1276 struct alpha_relax_info *info;
1277 bfd_vma symval;
1279 /* If the function has the same gp, and we can identify that the
1280 function does not use its function pointer, we can eliminate the
1281 address load. */
1283 /* If the symbol is marked NOPV, we are being told the function never
1284 needs its procedure value. */
1285 if (info->other == STO_ALPHA_NOPV)
1286 return symval;
1288 /* If the symbol is marked STD_GP, we are being told the function does
1289 a normal ldgp in the first two words. */
1290 else if (info->other == STO_ALPHA_STD_GPLOAD)
1293 /* Otherwise, we may be able to identify a GP load in the first two
1294 words, which we can then skip. */
1295 else
1297 Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
1298 bfd_vma ofs;
1300 /* Load the relocations from the section that the target symbol is in. */
1301 if (info->sec == info->tsec)
1303 tsec_relocs = info->relocs;
1304 tsec_relend = info->relend;
1305 tsec_free = NULL;
1307 else
1309 tsec_relocs = (_bfd_elf64_link_read_relocs
1310 (info->abfd, info->tsec, (PTR) NULL,
1311 (Elf_Internal_Rela *) NULL,
1312 info->link_info->keep_memory));
1313 if (tsec_relocs == NULL)
1314 return 0;
1315 tsec_relend = tsec_relocs + info->tsec->reloc_count;
1316 tsec_free = (info->link_info->keep_memory ? NULL : tsec_relocs);
1319 /* Recover the symbol's offset within the section. */
1320 ofs = (symval - info->tsec->output_section->vma
1321 - info->tsec->output_offset);
1323 /* Look for a GPDISP reloc. */
1324 gpdisp = (elf64_alpha_find_reloc_at_ofs
1325 (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
1327 if (!gpdisp || gpdisp->r_addend != 4)
1329 if (tsec_free)
1330 free (tsec_free);
1331 return 0;
1333 if (tsec_free)
1334 free (tsec_free);
1337 /* We've now determined that we can skip an initial gp load. Verify
1338 that the call and the target use the same gp. */
1339 if (info->link_info->hash->creator != info->tsec->owner->xvec
1340 || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
1341 return 0;
1343 return symval + 8;
1346 static boolean
1347 elf64_alpha_relax_without_lituse (info, symval, irel)
1348 struct alpha_relax_info *info;
1349 bfd_vma symval;
1350 Elf_Internal_Rela *irel;
1352 unsigned int insn;
1353 bfd_signed_vma disp;
1355 /* Get the instruction. */
1356 insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1358 if (insn >> 26 != OP_LDQ)
1360 ((*_bfd_error_handler)
1361 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1362 bfd_get_filename (info->abfd), info->sec->name,
1363 (unsigned long) irel->r_offset));
1364 return true;
1367 /* So we aren't told much. Do what we can with the address load and
1368 fake the rest. All of the optimizations here require that the
1369 offset from the GP fit in 16 bits. */
1371 disp = symval - info->gp;
1372 if (disp < -0x8000 || disp >= 0x8000)
1373 return true;
1375 /* On the LITERAL instruction itself, consider exchanging
1376 `ldq R,X(gp)' for `lda R,Y(gp)'. */
1378 insn = (OP_LDA << 26) | (insn & 0x03ff0000);
1379 bfd_put_32 (info->abfd, insn, info->contents + irel->r_offset);
1380 info->changed_contents = true;
1382 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), R_ALPHA_GPRELLOW);
1383 info->changed_relocs = true;
1385 /* Reduce the use count on this got entry by one, possibly
1386 eliminating it. */
1387 info->gotent->use_count -= 1;
1388 alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1389 if (!info->h)
1390 alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1392 /* ??? Search forward through this basic block looking for insns
1393 that use the target register. Stop after an insn modifying the
1394 register is seen, or after a branch or call.
1396 Any such memory load insn may be substituted by a load directly
1397 off the GP. This allows the memory load insn to be issued before
1398 the calculated GP register would otherwise be ready.
1400 Any such jsr insn can be replaced by a bsr if it is in range.
1402 This would mean that we'd have to _add_ relocations, the pain of
1403 which gives one pause. */
1405 return true;
1408 static boolean
1409 elf64_alpha_relax_section (abfd, sec, link_info, again)
1410 bfd *abfd;
1411 asection *sec;
1412 struct bfd_link_info *link_info;
1413 boolean *again;
1415 Elf_Internal_Shdr *symtab_hdr;
1416 Elf_Internal_Rela *internal_relocs;
1417 Elf_Internal_Rela *free_relocs = NULL;
1418 Elf_Internal_Rela *irel, *irelend;
1419 bfd_byte *free_contents = NULL;
1420 Elf64_External_Sym *extsyms = NULL;
1421 Elf64_External_Sym *free_extsyms = NULL;
1422 struct alpha_elf_got_entry **local_got_entries;
1423 struct alpha_relax_info info;
1425 /* We are not currently changing any sizes, so only one pass. */
1426 *again = false;
1428 if (link_info->relocateable
1429 || (sec->flags & SEC_RELOC) == 0
1430 || sec->reloc_count == 0)
1431 return true;
1433 /* If this is the first time we have been called for this section,
1434 initialize the cooked size. */
1435 if (sec->_cooked_size == 0)
1436 sec->_cooked_size = sec->_raw_size;
1438 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1439 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1441 /* Load the relocations for this section. */
1442 internal_relocs = (_bfd_elf64_link_read_relocs
1443 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1444 link_info->keep_memory));
1445 if (internal_relocs == NULL)
1446 goto error_return;
1447 if (! link_info->keep_memory)
1448 free_relocs = internal_relocs;
1450 memset(&info, 0, sizeof(info));
1451 info.abfd = abfd;
1452 info.sec = sec;
1453 info.link_info = link_info;
1454 info.relocs = internal_relocs;
1455 info.relend = irelend = internal_relocs + sec->reloc_count;
1457 /* Find the GP for this object. */
1458 info.gotobj = alpha_elf_tdata (abfd)->gotobj;
1459 if (info.gotobj)
1461 asection *sgot = alpha_elf_tdata (info.gotobj)->got;
1462 info.gp = _bfd_get_gp_value (info.gotobj);
1463 if (info.gp == 0)
1465 info.gp = (sgot->output_section->vma
1466 + sgot->output_offset
1467 + 0x8000);
1468 _bfd_set_gp_value (info.gotobj, info.gp);
1472 for (irel = internal_relocs; irel < irelend; irel++)
1474 bfd_vma symval;
1475 Elf_Internal_Sym isym;
1476 struct alpha_elf_got_entry *gotent;
1478 if (ELF64_R_TYPE (irel->r_info) != (int) R_ALPHA_LITERAL)
1479 continue;
1481 /* Get the section contents. */
1482 if (info.contents == NULL)
1484 if (elf_section_data (sec)->this_hdr.contents != NULL)
1485 info.contents = elf_section_data (sec)->this_hdr.contents;
1486 else
1488 info.contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1489 if (info.contents == NULL)
1490 goto error_return;
1491 free_contents = info.contents;
1493 if (! bfd_get_section_contents (abfd, sec, info.contents,
1494 (file_ptr) 0, sec->_raw_size))
1495 goto error_return;
1499 /* Read this BFD's symbols if we haven't done so already. */
1500 if (extsyms == NULL)
1502 if (symtab_hdr->contents != NULL)
1503 extsyms = (Elf64_External_Sym *) symtab_hdr->contents;
1504 else
1506 extsyms = ((Elf64_External_Sym *)
1507 bfd_malloc (symtab_hdr->sh_size));
1508 if (extsyms == NULL)
1509 goto error_return;
1510 free_extsyms = extsyms;
1511 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1512 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1513 != symtab_hdr->sh_size))
1514 goto error_return;
1518 /* Get the value of the symbol referred to by the reloc. */
1519 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1521 /* A local symbol. */
1522 bfd_elf64_swap_symbol_in (abfd,
1523 extsyms + ELF64_R_SYM (irel->r_info),
1524 &isym);
1525 if (isym.st_shndx == SHN_UNDEF)
1526 info.tsec = bfd_und_section_ptr;
1527 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1528 info.tsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1529 else if (isym.st_shndx == SHN_ABS)
1530 info.tsec = bfd_abs_section_ptr;
1531 else if (isym.st_shndx == SHN_COMMON)
1532 info.tsec = bfd_com_section_ptr;
1533 else
1534 continue; /* who knows. */
1536 info.h = NULL;
1537 info.other = isym.st_other;
1538 gotent = local_got_entries[ELF64_R_SYM(irel->r_info)];
1539 symval = isym.st_value;
1541 else
1543 unsigned long indx;
1544 struct alpha_elf_link_hash_entry *h;
1546 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1547 h = alpha_elf_sym_hashes (abfd)[indx];
1548 BFD_ASSERT (h != NULL);
1550 while (h->root.root.type == bfd_link_hash_indirect
1551 || h->root.root.type == bfd_link_hash_warning)
1552 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
1554 /* We can't do anthing with undefined or dynamic symbols. */
1555 if (h->root.root.type == bfd_link_hash_undefined
1556 || h->root.root.type == bfd_link_hash_undefweak
1557 || alpha_elf_dynamic_symbol_p (&h->root, link_info))
1558 continue;
1560 info.h = h;
1561 info.gotent = gotent;
1562 info.tsec = h->root.root.u.def.section;
1563 info.other = h->root.other;
1564 gotent = h->got_entries;
1565 symval = h->root.root.u.def.value;
1568 /* Search for the got entry to be used by this relocation. */
1569 while (gotent->gotobj != info.gotobj || gotent->addend != irel->r_addend)
1570 gotent = gotent->next;
1571 info.gotent = gotent;
1573 symval += info.tsec->output_section->vma + info.tsec->output_offset;
1574 symval += irel->r_addend;
1576 BFD_ASSERT(info.gotent != NULL);
1578 /* If there exist LITUSE relocations immediately following, this
1579 opens up all sorts of interesting optimizations, because we
1580 now know every location that this address load is used. */
1582 if (irel+1 < irelend && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
1584 irel = elf64_alpha_relax_with_lituse (&info, symval, irel, irelend);
1585 if (irel == NULL)
1586 goto error_return;
1588 else
1590 if (!elf64_alpha_relax_without_lituse (&info, symval, irel))
1591 goto error_return;
1595 if (!elf64_alpha_size_got_sections (abfd, link_info))
1596 return false;
1598 if (info.changed_relocs)
1600 elf_section_data (sec)->relocs = internal_relocs;
1602 else if (free_relocs != NULL)
1604 free (free_relocs);
1607 if (info.changed_contents)
1609 elf_section_data (sec)->this_hdr.contents = info.contents;
1611 else if (free_contents != NULL)
1613 if (! link_info->keep_memory)
1614 free (free_contents);
1615 else
1617 /* Cache the section contents for elf_link_input_bfd. */
1618 elf_section_data (sec)->this_hdr.contents = info.contents;
1622 if (free_extsyms != NULL)
1624 if (! link_info->keep_memory)
1625 free (free_extsyms);
1626 else
1628 /* Cache the symbols for elf_link_input_bfd. */
1629 symtab_hdr->contents = extsyms;
1633 *again = info.changed_contents || info.changed_relocs;
1635 return true;
1637 error_return:
1638 if (free_relocs != NULL)
1639 free (free_relocs);
1640 if (free_contents != NULL)
1641 free (free_contents);
1642 if (free_extsyms != NULL)
1643 free (free_extsyms);
1644 return false;
1647 /* PLT/GOT Stuff */
1648 #define PLT_HEADER_SIZE 32
1649 #define PLT_HEADER_WORD1 0xc3600000 /* br $27,.+4 */
1650 #define PLT_HEADER_WORD2 0xa77b000c /* ldq $27,12($27) */
1651 #define PLT_HEADER_WORD3 0x47ff041f /* nop */
1652 #define PLT_HEADER_WORD4 0x6b7b0000 /* jmp $27,($27) */
1654 #define PLT_ENTRY_SIZE 12
1655 #define PLT_ENTRY_WORD1 0xc3800000 /* br $28, plt0 */
1656 #define PLT_ENTRY_WORD2 0
1657 #define PLT_ENTRY_WORD3 0
1659 #define MAX_GOT_ENTRIES (64*1024 / 8)
1661 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
1663 /* Handle an Alpha specific section when reading an object file. This
1664 is called when elfcode.h finds a section with an unknown type.
1665 FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
1666 how to. */
1668 static boolean
1669 elf64_alpha_section_from_shdr (abfd, hdr, name)
1670 bfd *abfd;
1671 Elf64_Internal_Shdr *hdr;
1672 char *name;
1674 asection *newsect;
1676 /* There ought to be a place to keep ELF backend specific flags, but
1677 at the moment there isn't one. We just keep track of the
1678 sections by their name, instead. Fortunately, the ABI gives
1679 suggested names for all the MIPS specific sections, so we will
1680 probably get away with this. */
1681 switch (hdr->sh_type)
1683 case SHT_ALPHA_DEBUG:
1684 if (strcmp (name, ".mdebug") != 0)
1685 return false;
1686 break;
1687 #ifdef ERIC_neverdef
1688 case SHT_ALPHA_REGINFO:
1689 if (strcmp (name, ".reginfo") != 0
1690 || hdr->sh_size != sizeof (Elf64_External_RegInfo))
1691 return false;
1692 break;
1693 #endif
1694 default:
1695 return false;
1698 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1699 return false;
1700 newsect = hdr->bfd_section;
1702 if (hdr->sh_type == SHT_ALPHA_DEBUG)
1704 if (! bfd_set_section_flags (abfd, newsect,
1705 (bfd_get_section_flags (abfd, newsect)
1706 | SEC_DEBUGGING)))
1707 return false;
1710 #ifdef ERIC_neverdef
1711 /* For a .reginfo section, set the gp value in the tdata information
1712 from the contents of this section. We need the gp value while
1713 processing relocs, so we just get it now. */
1714 if (hdr->sh_type == SHT_ALPHA_REGINFO)
1716 Elf64_External_RegInfo ext;
1717 Elf64_RegInfo s;
1719 if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1720 (file_ptr) 0, sizeof ext))
1721 return false;
1722 bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
1723 elf_gp (abfd) = s.ri_gp_value;
1725 #endif
1727 return true;
1730 /* Set the correct type for an Alpha ELF section. We do this by the
1731 section name, which is a hack, but ought to work. */
1733 static boolean
1734 elf64_alpha_fake_sections (abfd, hdr, sec)
1735 bfd *abfd;
1736 Elf64_Internal_Shdr *hdr;
1737 asection *sec;
1739 register const char *name;
1741 name = bfd_get_section_name (abfd, sec);
1743 if (strcmp (name, ".mdebug") == 0)
1745 hdr->sh_type = SHT_ALPHA_DEBUG;
1746 /* In a shared object on Irix 5.3, the .mdebug section has an
1747 entsize of 0. FIXME: Does this matter? */
1748 if ((abfd->flags & DYNAMIC) != 0 )
1749 hdr->sh_entsize = 0;
1750 else
1751 hdr->sh_entsize = 1;
1753 #ifdef ERIC_neverdef
1754 else if (strcmp (name, ".reginfo") == 0)
1756 hdr->sh_type = SHT_ALPHA_REGINFO;
1757 /* In a shared object on Irix 5.3, the .reginfo section has an
1758 entsize of 0x18. FIXME: Does this matter? */
1759 if ((abfd->flags & DYNAMIC) != 0)
1760 hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
1761 else
1762 hdr->sh_entsize = 1;
1764 /* Force the section size to the correct value, even if the
1765 linker thinks it is larger. The link routine below will only
1766 write out this much data for .reginfo. */
1767 hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
1769 else if (strcmp (name, ".hash") == 0
1770 || strcmp (name, ".dynamic") == 0
1771 || strcmp (name, ".dynstr") == 0)
1773 hdr->sh_entsize = 0;
1774 hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
1776 #endif
1777 else if (strcmp (name, ".sdata") == 0
1778 || strcmp (name, ".sbss") == 0
1779 || strcmp (name, ".lit4") == 0
1780 || strcmp (name, ".lit8") == 0)
1781 hdr->sh_flags |= SHF_ALPHA_GPREL;
1783 return true;
1786 /* Hook called by the linker routine which adds symbols from an object
1787 file. We use it to put .comm items in .sbss, and not .bss. */
1789 static boolean
1790 elf64_alpha_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1791 bfd *abfd;
1792 struct bfd_link_info *info;
1793 const Elf_Internal_Sym *sym;
1794 const char **namep;
1795 flagword *flagsp;
1796 asection **secp;
1797 bfd_vma *valp;
1799 if (sym->st_shndx == SHN_COMMON
1800 && !info->relocateable
1801 && sym->st_size <= bfd_get_gp_size (abfd))
1803 /* Common symbols less than or equal to -G nn bytes are
1804 automatically put into .sbss. */
1806 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1808 if (scomm == NULL)
1810 scomm = bfd_make_section (abfd, ".scommon");
1811 if (scomm == NULL
1812 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
1813 | SEC_IS_COMMON
1814 | SEC_LINKER_CREATED)))
1815 return false;
1818 *secp = scomm;
1819 *valp = sym->st_size;
1822 return true;
1825 /* Create the .got section. */
1827 static boolean
1828 elf64_alpha_create_got_section(abfd, info)
1829 bfd *abfd;
1830 struct bfd_link_info *info;
1832 asection *s;
1834 if (bfd_get_section_by_name (abfd, ".got"))
1835 return true;
1837 s = bfd_make_section (abfd, ".got");
1838 if (s == NULL
1839 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1840 | SEC_HAS_CONTENTS
1841 | SEC_IN_MEMORY
1842 | SEC_LINKER_CREATED))
1843 || !bfd_set_section_alignment (abfd, s, 3))
1844 return false;
1846 alpha_elf_tdata (abfd)->got = s;
1848 return true;
1851 /* Create all the dynamic sections. */
1853 static boolean
1854 elf64_alpha_create_dynamic_sections (abfd, info)
1855 bfd *abfd;
1856 struct bfd_link_info *info;
1858 asection *s;
1859 struct elf_link_hash_entry *h;
1861 /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
1863 s = bfd_make_section (abfd, ".plt");
1864 if (s == NULL
1865 || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1866 | SEC_HAS_CONTENTS
1867 | SEC_IN_MEMORY
1868 | SEC_LINKER_CREATED
1869 | SEC_CODE))
1870 || ! bfd_set_section_alignment (abfd, s, 3))
1871 return false;
1873 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1874 .plt section. */
1875 h = NULL;
1876 if (! (_bfd_generic_link_add_one_symbol
1877 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1878 (bfd_vma) 0, (const char *) NULL, false,
1879 get_elf_backend_data (abfd)->collect,
1880 (struct bfd_link_hash_entry **) &h)))
1881 return false;
1882 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1883 h->type = STT_OBJECT;
1885 if (info->shared
1886 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1887 return false;
1889 s = bfd_make_section (abfd, ".rela.plt");
1890 if (s == NULL
1891 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1892 | SEC_HAS_CONTENTS
1893 | SEC_IN_MEMORY
1894 | SEC_LINKER_CREATED
1895 | SEC_READONLY))
1896 || ! bfd_set_section_alignment (abfd, s, 3))
1897 return false;
1899 /* We may or may not have created a .got section for this object, but
1900 we definitely havn't done the rest of the work. */
1902 if (!elf64_alpha_create_got_section (abfd, info))
1903 return false;
1905 s = bfd_make_section(abfd, ".rela.got");
1906 if (s == NULL
1907 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1908 | SEC_HAS_CONTENTS
1909 | SEC_IN_MEMORY
1910 | SEC_LINKER_CREATED
1911 | SEC_READONLY))
1912 || !bfd_set_section_alignment (abfd, s, 3))
1913 return false;
1915 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1916 dynobj's .got section. We don't do this in the linker script
1917 because we don't want to define the symbol if we are not creating
1918 a global offset table. */
1919 h = NULL;
1920 if (!(_bfd_generic_link_add_one_symbol
1921 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
1922 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
1923 false, get_elf_backend_data (abfd)->collect,
1924 (struct bfd_link_hash_entry **) &h)))
1925 return false;
1926 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1927 h->type = STT_OBJECT;
1929 if (info->shared
1930 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1931 return false;
1933 elf_hash_table (info)->hgot = h;
1935 return true;
1938 /* Read ECOFF debugging information from a .mdebug section into a
1939 ecoff_debug_info structure. */
1941 static boolean
1942 elf64_alpha_read_ecoff_info (abfd, section, debug)
1943 bfd *abfd;
1944 asection *section;
1945 struct ecoff_debug_info *debug;
1947 HDRR *symhdr;
1948 const struct ecoff_debug_swap *swap;
1949 char *ext_hdr = NULL;
1951 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1952 memset (debug, 0, sizeof(*debug));
1954 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
1955 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1956 goto error_return;
1958 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1959 swap->external_hdr_size)
1960 == false)
1961 goto error_return;
1963 symhdr = &debug->symbolic_header;
1964 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1966 /* The symbolic header contains absolute file offsets and sizes to
1967 read. */
1968 #define READ(ptr, offset, count, size, type) \
1969 if (symhdr->count == 0) \
1970 debug->ptr = NULL; \
1971 else \
1973 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
1974 if (debug->ptr == NULL) \
1975 goto error_return; \
1976 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1977 || (bfd_read (debug->ptr, size, symhdr->count, \
1978 abfd) != size * symhdr->count)) \
1979 goto error_return; \
1982 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1983 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1984 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1985 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1986 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1987 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1988 union aux_ext *);
1989 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1990 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1991 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1992 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1993 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1994 #undef READ
1996 debug->fdr = NULL;
1997 debug->adjust = NULL;
1999 return true;
2001 error_return:
2002 if (ext_hdr != NULL)
2003 free (ext_hdr);
2004 if (debug->line != NULL)
2005 free (debug->line);
2006 if (debug->external_dnr != NULL)
2007 free (debug->external_dnr);
2008 if (debug->external_pdr != NULL)
2009 free (debug->external_pdr);
2010 if (debug->external_sym != NULL)
2011 free (debug->external_sym);
2012 if (debug->external_opt != NULL)
2013 free (debug->external_opt);
2014 if (debug->external_aux != NULL)
2015 free (debug->external_aux);
2016 if (debug->ss != NULL)
2017 free (debug->ss);
2018 if (debug->ssext != NULL)
2019 free (debug->ssext);
2020 if (debug->external_fdr != NULL)
2021 free (debug->external_fdr);
2022 if (debug->external_rfd != NULL)
2023 free (debug->external_rfd);
2024 if (debug->external_ext != NULL)
2025 free (debug->external_ext);
2026 return false;
2029 /* Alpha ELF local labels start with '$'. */
2031 static boolean
2032 elf64_alpha_is_local_label_name (abfd, name)
2033 bfd *abfd;
2034 const char *name;
2036 return name[0] == '$';
2039 /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
2040 routine in order to handle the ECOFF debugging information. We
2041 still call this mips_elf_find_line because of the slot
2042 find_line_info in elf_obj_tdata is declared that way. */
2044 struct mips_elf_find_line
2046 struct ecoff_debug_info d;
2047 struct ecoff_find_line i;
2050 static boolean
2051 elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2052 functionname_ptr, line_ptr)
2053 bfd *abfd;
2054 asection *section;
2055 asymbol **symbols;
2056 bfd_vma offset;
2057 const char **filename_ptr;
2058 const char **functionname_ptr;
2059 unsigned int *line_ptr;
2061 asection *msec;
2063 msec = bfd_get_section_by_name (abfd, ".mdebug");
2064 if (msec != NULL)
2066 flagword origflags;
2067 struct mips_elf_find_line *fi;
2068 const struct ecoff_debug_swap * const swap =
2069 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2071 /* If we are called during a link, alpha_elf_final_link may have
2072 cleared the SEC_HAS_CONTENTS field. We force it back on here
2073 if appropriate (which it normally will be). */
2074 origflags = msec->flags;
2075 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2076 msec->flags |= SEC_HAS_CONTENTS;
2078 fi = elf_tdata (abfd)->find_line_info;
2079 if (fi == NULL)
2081 bfd_size_type external_fdr_size;
2082 char *fraw_src;
2083 char *fraw_end;
2084 struct fdr *fdr_ptr;
2086 fi = ((struct mips_elf_find_line *)
2087 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
2088 if (fi == NULL)
2090 msec->flags = origflags;
2091 return false;
2094 if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
2096 msec->flags = origflags;
2097 return false;
2100 /* Swap in the FDR information. */
2101 fi->d.fdr = ((struct fdr *)
2102 bfd_alloc (abfd,
2103 (fi->d.symbolic_header.ifdMax *
2104 sizeof (struct fdr))));
2105 if (fi->d.fdr == NULL)
2107 msec->flags = origflags;
2108 return false;
2110 external_fdr_size = swap->external_fdr_size;
2111 fdr_ptr = fi->d.fdr;
2112 fraw_src = (char *) fi->d.external_fdr;
2113 fraw_end = (fraw_src
2114 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2115 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2116 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2118 elf_tdata (abfd)->find_line_info = fi;
2120 /* Note that we don't bother to ever free this information.
2121 find_nearest_line is either called all the time, as in
2122 objdump -l, so the information should be saved, or it is
2123 rarely called, as in ld error messages, so the memory
2124 wasted is unimportant. Still, it would probably be a
2125 good idea for free_cached_info to throw it away. */
2128 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2129 &fi->i, filename_ptr, functionname_ptr,
2130 line_ptr))
2132 msec->flags = origflags;
2133 return true;
2136 msec->flags = origflags;
2139 /* Fall back on the generic ELF find_nearest_line routine. */
2141 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2142 filename_ptr, functionname_ptr,
2143 line_ptr);
2146 /* Structure used to pass information to alpha_elf_output_extsym. */
2148 struct extsym_info
2150 bfd *abfd;
2151 struct bfd_link_info *info;
2152 struct ecoff_debug_info *debug;
2153 const struct ecoff_debug_swap *swap;
2154 boolean failed;
2157 static boolean
2158 elf64_alpha_output_extsym (h, data)
2159 struct alpha_elf_link_hash_entry *h;
2160 PTR data;
2162 struct extsym_info *einfo = (struct extsym_info *) data;
2163 boolean strip;
2164 asection *sec, *output_section;
2166 if (h->root.indx == -2)
2167 strip = false;
2168 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2169 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2170 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2171 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2172 strip = true;
2173 else if (einfo->info->strip == strip_all
2174 || (einfo->info->strip == strip_some
2175 && bfd_hash_lookup (einfo->info->keep_hash,
2176 h->root.root.root.string,
2177 false, false) == NULL))
2178 strip = true;
2179 else
2180 strip = false;
2182 if (strip)
2183 return true;
2185 if (h->esym.ifd == -2)
2187 h->esym.jmptbl = 0;
2188 h->esym.cobol_main = 0;
2189 h->esym.weakext = 0;
2190 h->esym.reserved = 0;
2191 h->esym.ifd = ifdNil;
2192 h->esym.asym.value = 0;
2193 h->esym.asym.st = stGlobal;
2195 if (h->root.root.type != bfd_link_hash_defined
2196 && h->root.root.type != bfd_link_hash_defweak)
2197 h->esym.asym.sc = scAbs;
2198 else
2200 const char *name;
2202 sec = h->root.root.u.def.section;
2203 output_section = sec->output_section;
2205 /* When making a shared library and symbol h is the one from
2206 the another shared library, OUTPUT_SECTION may be null. */
2207 if (output_section == NULL)
2208 h->esym.asym.sc = scUndefined;
2209 else
2211 name = bfd_section_name (output_section->owner, output_section);
2213 if (strcmp (name, ".text") == 0)
2214 h->esym.asym.sc = scText;
2215 else if (strcmp (name, ".data") == 0)
2216 h->esym.asym.sc = scData;
2217 else if (strcmp (name, ".sdata") == 0)
2218 h->esym.asym.sc = scSData;
2219 else if (strcmp (name, ".rodata") == 0
2220 || strcmp (name, ".rdata") == 0)
2221 h->esym.asym.sc = scRData;
2222 else if (strcmp (name, ".bss") == 0)
2223 h->esym.asym.sc = scBss;
2224 else if (strcmp (name, ".sbss") == 0)
2225 h->esym.asym.sc = scSBss;
2226 else if (strcmp (name, ".init") == 0)
2227 h->esym.asym.sc = scInit;
2228 else if (strcmp (name, ".fini") == 0)
2229 h->esym.asym.sc = scFini;
2230 else
2231 h->esym.asym.sc = scAbs;
2235 h->esym.asym.reserved = 0;
2236 h->esym.asym.index = indexNil;
2239 if (h->root.root.type == bfd_link_hash_common)
2240 h->esym.asym.value = h->root.root.u.c.size;
2241 else if (h->root.root.type == bfd_link_hash_defined
2242 || h->root.root.type == bfd_link_hash_defweak)
2244 if (h->esym.asym.sc == scCommon)
2245 h->esym.asym.sc = scBss;
2246 else if (h->esym.asym.sc == scSCommon)
2247 h->esym.asym.sc = scSBss;
2249 sec = h->root.root.u.def.section;
2250 output_section = sec->output_section;
2251 if (output_section != NULL)
2252 h->esym.asym.value = (h->root.root.u.def.value
2253 + sec->output_offset
2254 + output_section->vma);
2255 else
2256 h->esym.asym.value = 0;
2258 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2260 /* Set type and value for a symbol with a function stub. */
2261 h->esym.asym.st = stProc;
2262 sec = bfd_get_section_by_name (einfo->abfd, ".plt");
2263 if (sec == NULL)
2264 h->esym.asym.value = 0;
2265 else
2267 output_section = sec->output_section;
2268 if (output_section != NULL)
2269 h->esym.asym.value = (h->root.plt.offset
2270 + sec->output_offset
2271 + output_section->vma);
2272 else
2273 h->esym.asym.value = 0;
2275 #if 0 /* FIXME? */
2276 h->esym.ifd = 0;
2277 #endif
2280 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2281 h->root.root.root.string,
2282 &h->esym))
2284 einfo->failed = true;
2285 return false;
2288 return true;
2291 /* FIXME: Create a runtime procedure table from the .mdebug section.
2293 static boolean
2294 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
2295 PTR handle;
2296 bfd *abfd;
2297 struct bfd_link_info *info;
2298 asection *s;
2299 struct ecoff_debug_info *debug;
2302 /* Handle dynamic relocations when doing an Alpha ELF link. */
2304 static boolean
2305 elf64_alpha_check_relocs (abfd, info, sec, relocs)
2306 bfd *abfd;
2307 struct bfd_link_info *info;
2308 asection *sec;
2309 const Elf_Internal_Rela *relocs;
2311 bfd *dynobj;
2312 asection *sreloc;
2313 const char *rel_sec_name;
2314 Elf_Internal_Shdr *symtab_hdr;
2315 struct alpha_elf_link_hash_entry **sym_hashes;
2316 struct alpha_elf_got_entry **local_got_entries;
2317 const Elf_Internal_Rela *rel, *relend;
2318 int got_created;
2320 if (info->relocateable)
2321 return true;
2323 dynobj = elf_hash_table(info)->dynobj;
2324 if (dynobj == NULL)
2325 elf_hash_table(info)->dynobj = dynobj = abfd;
2327 sreloc = NULL;
2328 rel_sec_name = NULL;
2329 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2330 sym_hashes = alpha_elf_sym_hashes(abfd);
2331 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2332 got_created = 0;
2334 relend = relocs + sec->reloc_count;
2335 for (rel = relocs; rel < relend; ++rel)
2337 unsigned long r_symndx, r_type;
2338 struct alpha_elf_link_hash_entry *h;
2340 r_symndx = ELF64_R_SYM (rel->r_info);
2341 if (r_symndx < symtab_hdr->sh_info)
2342 h = NULL;
2343 else
2345 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2347 while (h->root.root.type == bfd_link_hash_indirect
2348 || h->root.root.type == bfd_link_hash_warning)
2349 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2351 h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2353 r_type = ELF64_R_TYPE (rel->r_info);
2355 switch (r_type)
2357 case R_ALPHA_LITERAL:
2359 struct alpha_elf_got_entry *gotent;
2360 int flags = 0;
2362 if (h)
2364 /* Search for and possibly create a got entry. */
2365 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2366 if (gotent->gotobj == abfd &&
2367 gotent->addend == rel->r_addend)
2368 break;
2370 if (!gotent)
2372 gotent = ((struct alpha_elf_got_entry *)
2373 bfd_alloc (abfd,
2374 sizeof (struct alpha_elf_got_entry)));
2375 if (!gotent)
2376 return false;
2378 gotent->gotobj = abfd;
2379 gotent->addend = rel->r_addend;
2380 gotent->got_offset = -1;
2381 gotent->flags = 0;
2382 gotent->use_count = 1;
2384 gotent->next = h->got_entries;
2385 h->got_entries = gotent;
2387 alpha_elf_tdata (abfd)->total_got_entries++;
2389 else
2390 gotent->use_count += 1;
2392 else
2394 /* This is a local .got entry -- record for merge. */
2395 if (!local_got_entries)
2397 size_t size;
2398 size = (symtab_hdr->sh_info
2399 * sizeof (struct alpha_elf_got_entry *));
2401 local_got_entries = ((struct alpha_elf_got_entry **)
2402 bfd_alloc (abfd, size));
2403 if (!local_got_entries)
2404 return false;
2406 memset (local_got_entries, 0, size);
2407 alpha_elf_tdata (abfd)->local_got_entries =
2408 local_got_entries;
2411 for (gotent = local_got_entries[ELF64_R_SYM(rel->r_info)];
2412 gotent != NULL && gotent->addend != rel->r_addend;
2413 gotent = gotent->next)
2414 continue;
2415 if (!gotent)
2417 gotent = ((struct alpha_elf_got_entry *)
2418 bfd_alloc (abfd,
2419 sizeof (struct alpha_elf_got_entry)));
2420 if (!gotent)
2421 return false;
2423 gotent->gotobj = abfd;
2424 gotent->addend = rel->r_addend;
2425 gotent->got_offset = -1;
2426 gotent->flags = 0;
2427 gotent->use_count = 1;
2429 gotent->next = local_got_entries[ELF64_R_SYM(rel->r_info)];
2430 local_got_entries[ELF64_R_SYM(rel->r_info)] = gotent;
2432 alpha_elf_tdata(abfd)->total_got_entries++;
2433 alpha_elf_tdata(abfd)->n_local_got_entries++;
2435 else
2436 gotent->use_count += 1;
2439 /* Remember how this literal is used from its LITUSEs.
2440 This will be important when it comes to decide if we can
2441 create a .plt entry for a function symbol. */
2442 if (rel+1 < relend
2443 && ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
2447 ++rel;
2448 if (rel->r_addend >= 1 && rel->r_addend <= 3)
2449 flags |= 1 << rel->r_addend;
2451 while (rel+1 < relend &&
2452 ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE);
2454 else
2456 /* No LITUSEs -- presumably the address is not being
2457 loaded for nothing. */
2458 flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
2461 gotent->flags |= flags;
2462 if (h)
2464 /* Make a guess as to whether a .plt entry will be needed. */
2465 if ((h->flags |= flags) == ALPHA_ELF_LINK_HASH_LU_FUNC)
2466 h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2467 else
2468 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2471 /* FALLTHRU */
2473 case R_ALPHA_GPDISP:
2474 case R_ALPHA_GPREL32:
2475 case R_ALPHA_GPRELHIGH:
2476 case R_ALPHA_GPRELLOW:
2477 /* We don't actually use the .got here, but the sections must
2478 be created before the linker maps input sections to output
2479 sections. */
2480 if (!got_created)
2482 if (!elf64_alpha_create_got_section (abfd, info))
2483 return false;
2485 /* Make sure the object's gotobj is set to itself so
2486 that we default to every object with its own .got.
2487 We'll merge .gots later once we've collected each
2488 object's info. */
2489 alpha_elf_tdata(abfd)->gotobj = abfd;
2491 got_created = 1;
2493 break;
2495 case R_ALPHA_SREL16:
2496 case R_ALPHA_SREL32:
2497 case R_ALPHA_SREL64:
2498 if (h == NULL)
2499 break;
2500 /* FALLTHRU */
2502 case R_ALPHA_REFLONG:
2503 case R_ALPHA_REFQUAD:
2504 if (rel_sec_name == NULL)
2506 rel_sec_name = (bfd_elf_string_from_elf_section
2507 (abfd, elf_elfheader(abfd)->e_shstrndx,
2508 elf_section_data(sec)->rel_hdr.sh_name));
2509 if (rel_sec_name == NULL)
2510 return false;
2512 BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
2513 && strcmp (bfd_get_section_name (abfd, sec),
2514 rel_sec_name+5) == 0);
2517 /* We need to create the section here now whether we eventually
2518 use it or not so that it gets mapped to an output section by
2519 the linker. If not used, we'll kill it in
2520 size_dynamic_sections. */
2521 if (sreloc == NULL)
2523 sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
2524 if (sreloc == NULL)
2526 sreloc = bfd_make_section (dynobj, rel_sec_name);
2527 if (sreloc == NULL
2528 || !bfd_set_section_flags (dynobj, sreloc,
2529 (SEC_ALLOC|SEC_LOAD
2530 | SEC_HAS_CONTENTS
2531 | SEC_IN_MEMORY
2532 | SEC_LINKER_CREATED
2533 | SEC_READONLY))
2534 || !bfd_set_section_alignment (dynobj, sreloc, 3))
2535 return false;
2539 if (h)
2541 /* Since we havn't seen all of the input symbols yet, we
2542 don't know whether we'll actually need a dynamic relocation
2543 entry for this reloc. So make a record of it. Once we
2544 find out if this thing needs dynamic relocation we'll
2545 expand the relocation sections by the appropriate amount. */
2547 struct alpha_elf_reloc_entry *rent;
2549 for (rent = h->reloc_entries; rent; rent = rent->next)
2550 if (rent->rtype == r_type && rent->srel == sreloc)
2551 break;
2553 if (!rent)
2555 rent = ((struct alpha_elf_reloc_entry *)
2556 bfd_alloc (abfd,
2557 sizeof (struct alpha_elf_reloc_entry)));
2558 if (!rent)
2559 return false;
2561 rent->srel = sreloc;
2562 rent->rtype = r_type;
2563 rent->count = 1;
2565 rent->next = h->reloc_entries;
2566 h->reloc_entries = rent;
2568 else
2569 rent->count++;
2571 else if (info->shared)
2573 /* If this is a shared library, we need a RELATIVE reloc. */
2574 sreloc->_raw_size += sizeof (Elf64_External_Rela);
2576 break;
2580 return true;
2583 /* Adjust a symbol defined by a dynamic object and referenced by a
2584 regular object. The current definition is in some section of the
2585 dynamic object, but we're not including those sections. We have to
2586 change the definition to something the rest of the link can
2587 understand. */
2589 static boolean
2590 elf64_alpha_adjust_dynamic_symbol (info, h)
2591 struct bfd_link_info *info;
2592 struct elf_link_hash_entry *h;
2594 bfd *dynobj;
2595 asection *s;
2596 struct alpha_elf_link_hash_entry *ah;
2598 dynobj = elf_hash_table(info)->dynobj;
2599 ah = (struct alpha_elf_link_hash_entry *)h;
2601 /* Now that we've seen all of the input symbols, finalize our decision
2602 about whether this symbol should get a .plt entry. */
2604 if (h->root.type != bfd_link_hash_undefweak
2605 && alpha_elf_dynamic_symbol_p (h, info)
2606 && ((h->type == STT_FUNC
2607 && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
2608 || (h->type == STT_NOTYPE
2609 && ah->flags == ALPHA_ELF_LINK_HASH_LU_FUNC))
2610 /* Don't prevent otherwise valid programs from linking by attempting
2611 to create a new .got entry somewhere. A Correct Solution would be
2612 to add a new .got section to a new object file and let it be merged
2613 somewhere later. But for now don't bother. */
2614 && ah->got_entries)
2616 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2618 s = bfd_get_section_by_name(dynobj, ".plt");
2619 if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
2620 return false;
2622 /* The first bit of the .plt is reserved. */
2623 if (s->_raw_size == 0)
2624 s->_raw_size = PLT_HEADER_SIZE;
2626 h->plt.offset = s->_raw_size;
2627 s->_raw_size += PLT_ENTRY_SIZE;
2629 /* If this symbol is not defined in a regular file, and we are not
2630 generating a shared library, then set the symbol to the location
2631 in the .plt. This is required to make function pointers compare
2632 equal between the normal executable and the shared library. */
2633 if (! info->shared
2634 && h->root.type != bfd_link_hash_defweak)
2636 h->root.u.def.section = s;
2637 h->root.u.def.value = h->plt.offset;
2640 /* We also need a JMP_SLOT entry in the .rela.plt section. */
2641 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2642 BFD_ASSERT (s != NULL);
2643 s->_raw_size += sizeof (Elf64_External_Rela);
2645 return true;
2647 else
2648 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2650 /* If this is a weak symbol, and there is a real definition, the
2651 processor independent code will have arranged for us to see the
2652 real definition first, and we can just use the same value. */
2653 if (h->weakdef != NULL)
2655 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2656 || h->weakdef->root.type == bfd_link_hash_defweak);
2657 h->root.u.def.section = h->weakdef->root.u.def.section;
2658 h->root.u.def.value = h->weakdef->root.u.def.value;
2659 return true;
2662 /* This is a reference to a symbol defined by a dynamic object which
2663 is not a function. The Alpha, since it uses .got entries for all
2664 symbols even in regular objects, does not need the hackery of a
2665 .dynbss section and COPY dynamic relocations. */
2667 return true;
2670 /* Symbol versioning can create new symbols, and make our old symbols
2671 indirect to the new ones. Consolidate the got and reloc information
2672 in these situations. */
2674 static boolean
2675 elf64_alpha_merge_ind_symbols (hi, dummy)
2676 struct alpha_elf_link_hash_entry *hi;
2677 PTR dummy;
2679 struct alpha_elf_link_hash_entry *hs;
2681 if (hi->root.root.type != bfd_link_hash_indirect)
2682 return true;
2683 hs = hi;
2684 do {
2685 hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
2686 } while (hs->root.root.type == bfd_link_hash_indirect);
2688 /* Merge the flags. Whee. */
2690 hs->flags |= hi->flags;
2692 /* Merge the .got entries. Cannibalize the old symbol's list in
2693 doing so, since we don't need it anymore. */
2695 if (hs->got_entries == NULL)
2696 hs->got_entries = hi->got_entries;
2697 else
2699 struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
2701 gsh = hs->got_entries;
2702 for (gi = hi->got_entries; gi ; gi = gin)
2704 gin = gi->next;
2705 for (gs = gsh; gs ; gs = gs->next)
2706 if (gi->gotobj == gs->gotobj && gi->addend == gs->addend)
2707 goto got_found;
2708 gi->next = hs->got_entries;
2709 hs->got_entries = gi;
2710 got_found:;
2713 hi->got_entries = NULL;
2715 /* And similar for the reloc entries. */
2717 if (hs->reloc_entries == NULL)
2718 hs->reloc_entries = hi->reloc_entries;
2719 else
2721 struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
2723 rsh = hs->reloc_entries;
2724 for (ri = hi->reloc_entries; ri ; ri = rin)
2726 rin = ri->next;
2727 for (rs = rsh; rs ; rs = rs->next)
2728 if (ri->rtype == rs->rtype)
2730 rs->count += ri->count;
2731 goto found_reloc;
2733 ri->next = hs->reloc_entries;
2734 hs->reloc_entries = ri;
2735 found_reloc:;
2738 hi->reloc_entries = NULL;
2740 return true;
2743 /* Is it possible to merge two object file's .got tables? */
2745 static boolean
2746 elf64_alpha_can_merge_gots (a, b)
2747 bfd *a, *b;
2749 int total = alpha_elf_tdata (a)->total_got_entries;
2750 bfd *bsub;
2752 /* Trivial quick fallout test. */
2753 if (total + alpha_elf_tdata (b)->total_got_entries <= MAX_GOT_ENTRIES)
2754 return true;
2756 /* By their nature, local .got entries cannot be merged. */
2757 if ((total += alpha_elf_tdata (b)->n_local_got_entries) > MAX_GOT_ENTRIES)
2758 return false;
2760 /* Failing the common trivial comparison, we must effectively
2761 perform the merge. Not actually performing the merge means that
2762 we don't have to store undo information in case we fail. */
2763 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2765 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
2766 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2767 int i, n;
2769 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2770 for (i = 0; i < n; ++i)
2772 struct alpha_elf_got_entry *ae, *be;
2773 struct alpha_elf_link_hash_entry *h;
2775 h = hashes[i];
2776 while (h->root.root.type == bfd_link_hash_indirect
2777 || h->root.root.type == bfd_link_hash_warning)
2778 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2780 for (be = h->got_entries; be ; be = be->next)
2782 if (be->use_count == 0)
2783 continue;
2784 if (be->gotobj != b)
2785 continue;
2787 for (ae = h->got_entries; ae ; ae = ae->next)
2788 if (ae->gotobj == a && ae->addend == be->addend)
2789 goto global_found;
2791 if (++total > MAX_GOT_ENTRIES)
2792 return false;
2793 global_found:;
2798 return true;
2801 /* Actually merge two .got tables. */
2803 static void
2804 elf64_alpha_merge_gots (a, b)
2805 bfd *a, *b;
2807 int total = alpha_elf_tdata (a)->total_got_entries;
2808 bfd *bsub;
2810 /* Remember local expansion. */
2812 int e = alpha_elf_tdata (b)->n_local_got_entries;
2813 total += e;
2814 alpha_elf_tdata (a)->n_local_got_entries += e;
2817 for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2819 struct alpha_elf_got_entry **local_got_entries;
2820 struct alpha_elf_link_hash_entry **hashes;
2821 Elf_Internal_Shdr *symtab_hdr;
2822 int i, n;
2824 /* Let the local .got entries know they are part of a new subsegment. */
2825 local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
2826 if (local_got_entries)
2828 n = elf_tdata (bsub)->symtab_hdr.sh_info;
2829 for (i = 0; i < n; ++i)
2831 struct alpha_elf_got_entry *ent;
2832 for (ent = local_got_entries[i]; ent; ent = ent->next)
2833 ent->gotobj = a;
2837 /* Merge the global .got entries. */
2838 hashes = alpha_elf_sym_hashes (bsub);
2839 symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2841 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2842 for (i = 0; i < n; ++i)
2844 struct alpha_elf_got_entry *ae, *be, **pbe, **start;
2845 struct alpha_elf_link_hash_entry *h;
2847 h = hashes[i];
2848 while (h->root.root.type == bfd_link_hash_indirect
2849 || h->root.root.type == bfd_link_hash_warning)
2850 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2852 start = &h->got_entries;
2853 for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
2855 if (be->use_count == 0)
2857 *pbe = be->next;
2858 continue;
2860 if (be->gotobj != b)
2861 continue;
2863 for (ae = *start; ae ; ae = ae->next)
2864 if (ae->gotobj == a && ae->addend == be->addend)
2866 ae->flags |= be->flags;
2867 ae->use_count += be->use_count;
2868 *pbe = be->next;
2869 goto global_found;
2871 be->gotobj = a;
2872 total += 1;
2874 global_found:;
2878 alpha_elf_tdata (bsub)->gotobj = a;
2880 alpha_elf_tdata (a)->total_got_entries = total;
2882 /* Merge the two in_got chains. */
2884 bfd *next;
2886 bsub = a;
2887 while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
2888 bsub = next;
2890 alpha_elf_tdata (bsub)->in_got_link_next = b;
2894 /* Calculate the offsets for the got entries. */
2896 static boolean
2897 elf64_alpha_calc_got_offsets_for_symbol (h, arg)
2898 struct alpha_elf_link_hash_entry *h;
2899 PTR arg;
2901 struct alpha_elf_got_entry *gotent;
2903 for (gotent = h->got_entries; gotent; gotent = gotent->next)
2904 if (gotent->use_count > 0)
2906 bfd_size_type *plge
2907 = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
2909 gotent->got_offset = *plge;
2910 *plge += 8;
2913 return true;
2916 static void
2917 elf64_alpha_calc_got_offsets (info)
2918 struct bfd_link_info *info;
2920 bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
2922 /* First, zero out the .got sizes, as we may be recalculating the
2923 .got after optimizing it. */
2924 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2925 alpha_elf_tdata(i)->got->_raw_size = 0;
2927 /* Next, fill in the offsets for all the global entries. */
2928 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2929 elf64_alpha_calc_got_offsets_for_symbol,
2930 NULL);
2932 /* Finally, fill in the offsets for the local entries. */
2933 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2935 bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
2936 bfd *j;
2938 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2940 struct alpha_elf_got_entry **local_got_entries, *gotent;
2941 int k, n;
2943 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2944 if (!local_got_entries)
2945 continue;
2947 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
2948 for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
2949 if (gotent->use_count > 0)
2951 gotent->got_offset = got_offset;
2952 got_offset += 8;
2956 alpha_elf_tdata(i)->got->_raw_size = got_offset;
2957 alpha_elf_tdata(i)->got->_cooked_size = got_offset;
2961 /* Constructs the gots. */
2963 static boolean
2964 elf64_alpha_size_got_sections (output_bfd, info)
2965 bfd *output_bfd;
2966 struct bfd_link_info *info;
2968 bfd *i, *got_list, *cur_got_obj;
2969 int something_changed = 0;
2971 got_list = alpha_elf_hash_table (info)->got_list;
2973 /* On the first time through, pretend we have an existing got list
2974 consisting of all of the input files. */
2975 if (got_list == NULL)
2977 for (i = info->input_bfds; i ; i = i->link_next)
2979 bfd *this_got = alpha_elf_tdata (i)->gotobj;
2980 if (this_got == NULL)
2981 continue;
2983 /* We are assuming no merging has yet ocurred. */
2984 BFD_ASSERT (this_got == i);
2986 if (alpha_elf_tdata (this_got)->total_got_entries > MAX_GOT_ENTRIES)
2988 /* Yikes! A single object file has too many entries. */
2989 (*_bfd_error_handler)
2990 (_("%s: .got subsegment exceeds 64K (size %d)"),
2991 bfd_get_filename (i),
2992 alpha_elf_tdata (this_got)->total_got_entries * 8);
2993 return false;
2996 if (got_list == NULL)
2997 got_list = this_got;
2998 else
2999 alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
3000 cur_got_obj = this_got;
3003 /* Strange degenerate case of no got references. */
3004 if (got_list == NULL)
3005 return true;
3007 alpha_elf_hash_table (info)->got_list = got_list;
3009 /* Force got offsets to be recalculated. */
3010 something_changed = 1;
3013 cur_got_obj = got_list;
3014 i = alpha_elf_tdata(cur_got_obj)->got_link_next;
3015 while (i != NULL)
3017 if (elf64_alpha_can_merge_gots (cur_got_obj, i))
3019 elf64_alpha_merge_gots (cur_got_obj, i);
3020 i = alpha_elf_tdata(i)->got_link_next;
3021 alpha_elf_tdata(cur_got_obj)->got_link_next = i;
3022 something_changed = 1;
3024 else
3026 cur_got_obj = i;
3027 i = alpha_elf_tdata(i)->got_link_next;
3031 /* Once the gots have been merged, fill in the got offsets for
3032 everything therein. */
3033 if (1 || something_changed)
3034 elf64_alpha_calc_got_offsets (info);
3036 return true;
3039 static boolean
3040 elf64_alpha_always_size_sections (output_bfd, info)
3041 bfd *output_bfd;
3042 struct bfd_link_info *info;
3044 bfd *i;
3046 if (info->relocateable)
3047 return true;
3049 /* First, take care of the indirect symbols created by versioning. */
3050 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3051 elf64_alpha_merge_ind_symbols,
3052 NULL);
3054 if (!elf64_alpha_size_got_sections (output_bfd, info))
3055 return false;
3057 /* Allocate space for all of the .got subsections. */
3058 i = alpha_elf_hash_table (info)->got_list;
3059 for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
3061 asection *s = alpha_elf_tdata(i)->got;
3062 if (s->_raw_size > 0)
3064 s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
3065 if (s->contents == NULL)
3066 return false;
3070 return true;
3073 /* Work out the sizes of the dynamic relocation entries. */
3075 static boolean
3076 elf64_alpha_calc_dynrel_sizes (h, info)
3077 struct alpha_elf_link_hash_entry *h;
3078 struct bfd_link_info *info;
3080 /* If the symbol was defined as a common symbol in a regular object
3081 file, and there was no definition in any dynamic object, then the
3082 linker will have allocated space for the symbol in a common
3083 section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
3084 set. This is done for dynamic symbols in
3085 elf_adjust_dynamic_symbol but this is not done for non-dynamic
3086 symbols, somehow. */
3087 if (((h->root.elf_link_hash_flags
3088 & (ELF_LINK_HASH_DEF_REGULAR
3089 | ELF_LINK_HASH_REF_REGULAR
3090 | ELF_LINK_HASH_DEF_DYNAMIC))
3091 == ELF_LINK_HASH_REF_REGULAR)
3092 && (h->root.root.type == bfd_link_hash_defined
3093 || h->root.root.type == bfd_link_hash_defweak)
3094 && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
3096 h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3099 /* If the symbol is dynamic, we'll need all the relocations in their
3100 natural form. If this is a shared object, and it has been forced
3101 local, we'll need the same number of RELATIVE relocations. */
3103 if (alpha_elf_dynamic_symbol_p (&h->root, info) || info->shared)
3105 struct alpha_elf_reloc_entry *relent;
3106 bfd *dynobj;
3107 struct alpha_elf_got_entry *gotent;
3108 bfd_size_type count;
3109 asection *srel;
3111 for (relent = h->reloc_entries; relent; relent = relent->next)
3112 if (relent->rtype == R_ALPHA_REFLONG
3113 || relent->rtype == R_ALPHA_REFQUAD)
3115 relent->srel->_raw_size +=
3116 sizeof(Elf64_External_Rela) * relent->count;
3119 dynobj = elf_hash_table(info)->dynobj;
3120 count = 0;
3122 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3123 count++;
3125 /* If we are using a .plt entry, subtract one, as the first
3126 reference uses a .rela.plt entry instead. */
3127 if (h->root.plt.offset != MINUS_ONE)
3128 count--;
3130 if (count > 0)
3132 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3133 BFD_ASSERT (srel != NULL);
3134 srel->_raw_size += sizeof (Elf64_External_Rela) * count;
3138 return true;
3141 /* Set the sizes of the dynamic sections. */
3143 static boolean
3144 elf64_alpha_size_dynamic_sections (output_bfd, info)
3145 bfd *output_bfd;
3146 struct bfd_link_info *info;
3148 bfd *dynobj;
3149 asection *s;
3150 boolean reltext;
3151 boolean relplt;
3153 dynobj = elf_hash_table(info)->dynobj;
3154 BFD_ASSERT(dynobj != NULL);
3156 if (elf_hash_table (info)->dynamic_sections_created)
3158 /* Set the contents of the .interp section to the interpreter. */
3159 if (!info->shared)
3161 s = bfd_get_section_by_name (dynobj, ".interp");
3162 BFD_ASSERT (s != NULL);
3163 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3164 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3167 /* Now that we've seen all of the input files, we can decide which
3168 symbols need dynamic relocation entries and which don't. We've
3169 collected information in check_relocs that we can now apply to
3170 size the dynamic relocation sections. */
3171 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3172 elf64_alpha_calc_dynrel_sizes,
3173 info);
3175 /* When building shared libraries, each local .got entry needs a
3176 RELATIVE reloc. */
3177 if (info->shared)
3179 bfd *i;
3180 asection *srel;
3181 bfd_size_type count;
3183 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3184 BFD_ASSERT (srel != NULL);
3186 for (i = alpha_elf_hash_table(info)->got_list, count = 0;
3187 i != NULL;
3188 i = alpha_elf_tdata(i)->got_link_next)
3189 count += alpha_elf_tdata(i)->n_local_got_entries;
3191 srel->_raw_size += count * sizeof(Elf64_External_Rela);
3194 /* else we're not dynamic and by definition we don't need such things. */
3196 /* The check_relocs and adjust_dynamic_symbol entry points have
3197 determined the sizes of the various dynamic sections. Allocate
3198 memory for them. */
3199 reltext = false;
3200 relplt = false;
3201 for (s = dynobj->sections; s != NULL; s = s->next)
3203 const char *name;
3204 boolean strip;
3206 if (!(s->flags & SEC_LINKER_CREATED))
3207 continue;
3209 /* It's OK to base decisions on the section name, because none
3210 of the dynobj section names depend upon the input files. */
3211 name = bfd_get_section_name (dynobj, s);
3213 /* If we don't need this section, strip it from the output file.
3214 This is to handle .rela.bss and .rela.plt. We must create it
3215 in create_dynamic_sections, because it must be created before
3216 the linker maps input sections to output sections. The
3217 linker does that before adjust_dynamic_symbol is called, and
3218 it is that function which decides whether anything needs to
3219 go into these sections. */
3221 strip = false;
3223 if (strncmp (name, ".rela", 5) == 0)
3225 strip = (s->_raw_size == 0);
3227 if (!strip)
3229 const char *outname;
3230 asection *target;
3232 /* If this relocation section applies to a read only
3233 section, then we probably need a DT_TEXTREL entry. */
3234 outname = bfd_get_section_name (output_bfd,
3235 s->output_section);
3236 target = bfd_get_section_by_name (output_bfd, outname + 5);
3237 if (target != NULL
3238 && (target->flags & SEC_READONLY) != 0
3239 && (target->flags & SEC_ALLOC) != 0)
3240 reltext = true;
3242 if (strcmp(name, ".rela.plt") == 0)
3243 relplt = true;
3245 /* We use the reloc_count field as a counter if we need
3246 to copy relocs into the output file. */
3247 s->reloc_count = 0;
3250 else if (strcmp (name, ".plt") != 0)
3252 /* It's not one of our dynamic sections, so don't allocate space. */
3253 continue;
3256 if (strip)
3257 _bfd_strip_section_from_output (s);
3258 else
3260 /* Allocate memory for the section contents. */
3261 s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
3262 if (s->contents == NULL && s->_raw_size != 0)
3263 return false;
3267 /* If we are generating a shared library, we generate a section
3268 symbol for each output section. These are local symbols, which
3269 means that they must come first in the dynamic symbol table.
3270 That means we must increment the dynamic symbol index of every
3271 other dynamic symbol. */
3272 if (info->shared)
3274 long c[2], i;
3275 asection *p;
3277 c[0] = 0;
3278 c[1] = bfd_count_sections (output_bfd);
3280 elf_hash_table (info)->dynsymcount += c[1];
3281 elf_link_hash_traverse (elf_hash_table(info),
3282 elf64_alpha_adjust_dynindx,
3283 (PTR) c);
3285 for (i = 1, p = output_bfd->sections;
3286 p != NULL;
3287 p = p->next, i++)
3289 elf_section_data (p)->dynindx = i;
3290 /* These symbols will have no names, so we don't need to
3291 fiddle with dynstr_index. */
3295 if (elf_hash_table (info)->dynamic_sections_created)
3297 /* Add some entries to the .dynamic section. We fill in the
3298 values later, in elf64_alpha_finish_dynamic_sections, but we
3299 must add the entries now so that we get the correct size for
3300 the .dynamic section. The DT_DEBUG entry is filled in by the
3301 dynamic linker and used by the debugger. */
3302 if (!info->shared)
3304 if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
3305 return false;
3308 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
3309 return false;
3311 if (relplt)
3313 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3314 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
3315 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
3316 return false;
3319 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
3320 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
3321 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
3322 sizeof(Elf64_External_Rela)))
3323 return false;
3325 if (reltext)
3327 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
3328 return false;
3332 return true;
3335 /* Increment the index of a dynamic symbol by a given amount. Called
3336 via elf_link_hash_traverse. */
3338 static boolean
3339 elf64_alpha_adjust_dynindx (h, cparg)
3340 struct elf_link_hash_entry *h;
3341 PTR cparg;
3343 long *cp = (long *)cparg;
3345 if (h->dynindx >= cp[0])
3346 h->dynindx += cp[1];
3348 return true;
3351 /* Relocate an Alpha ELF section. */
3353 static boolean
3354 elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
3355 contents, relocs, local_syms, local_sections)
3356 bfd *output_bfd;
3357 struct bfd_link_info *info;
3358 bfd *input_bfd;
3359 asection *input_section;
3360 bfd_byte *contents;
3361 Elf_Internal_Rela *relocs;
3362 Elf_Internal_Sym *local_syms;
3363 asection **local_sections;
3365 Elf_Internal_Shdr *symtab_hdr;
3366 Elf_Internal_Rela *rel;
3367 Elf_Internal_Rela *relend;
3368 asection *sec, *sgot, *srel, *srelgot;
3369 bfd *dynobj, *gotobj;
3370 bfd_vma gp;
3372 srelgot = srel = NULL;
3373 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3374 dynobj = elf_hash_table (info)->dynobj;
3375 if (dynobj)
3377 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3380 /* Find the gp value for this input bfd. */
3381 sgot = NULL;
3382 gp = 0;
3383 gotobj = alpha_elf_tdata (input_bfd)->gotobj;
3384 if (gotobj)
3386 sgot = alpha_elf_tdata (gotobj)->got;
3387 gp = _bfd_get_gp_value (gotobj);
3388 if (gp == 0)
3390 gp = (sgot->output_section->vma
3391 + sgot->output_offset
3392 + 0x8000);
3393 _bfd_set_gp_value (gotobj, gp);
3397 rel = relocs;
3398 relend = relocs + input_section->reloc_count;
3399 for (; rel < relend; rel++)
3401 int r_type;
3402 reloc_howto_type *howto;
3403 unsigned long r_symndx;
3404 struct alpha_elf_link_hash_entry *h;
3405 Elf_Internal_Sym *sym;
3406 bfd_vma relocation;
3407 bfd_vma addend;
3408 bfd_reloc_status_type r;
3410 r_type = ELF64_R_TYPE(rel->r_info);
3411 if (r_type < 0 || r_type >= (int) R_ALPHA_max)
3413 bfd_set_error (bfd_error_bad_value);
3414 return false;
3416 howto = elf64_alpha_howto_table + r_type;
3418 r_symndx = ELF64_R_SYM(rel->r_info);
3420 if (info->relocateable)
3422 /* This is a relocateable link. We don't have to change
3423 anything, unless the reloc is against a section symbol,
3424 in which case we have to adjust according to where the
3425 section symbol winds up in the output section. */
3427 /* The symbol associated with GPDISP and LITUSE is
3428 immaterial. Only the addend is significant. */
3429 if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
3430 continue;
3432 if (r_symndx < symtab_hdr->sh_info)
3434 sym = local_syms + r_symndx;
3435 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
3437 sec = local_sections[r_symndx];
3438 rel->r_addend += sec->output_offset + sym->st_value;
3442 continue;
3445 /* This is a final link. */
3447 h = NULL;
3448 sym = NULL;
3449 sec = NULL;
3451 if (r_symndx < symtab_hdr->sh_info)
3453 sym = local_syms + r_symndx;
3454 sec = local_sections[r_symndx];
3455 relocation = (sec->output_section->vma
3456 + sec->output_offset
3457 + sym->st_value);
3459 else
3461 h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
3463 while (h->root.root.type == bfd_link_hash_indirect
3464 || h->root.root.type == bfd_link_hash_warning)
3465 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3467 if (h->root.root.type == bfd_link_hash_defined
3468 || h->root.root.type == bfd_link_hash_defweak)
3470 sec = h->root.root.u.def.section;
3472 #if rth_notdef
3473 if ((r_type == R_ALPHA_LITERAL
3474 && elf_hash_table(info)->dynamic_sections_created
3475 && (!info->shared
3476 || !info->symbolic
3477 || !(h->root.elf_link_hash_flags
3478 & ELF_LINK_HASH_DEF_REGULAR)))
3479 || (info->shared
3480 && (!info->symbolic
3481 || !(h->root.elf_link_hash_flags
3482 & ELF_LINK_HASH_DEF_REGULAR))
3483 && (input_section->flags & SEC_ALLOC)
3484 && (r_type == R_ALPHA_REFLONG
3485 || r_type == R_ALPHA_REFQUAD
3486 || r_type == R_ALPHA_LITERAL)))
3488 /* In these cases, we don't need the relocation value.
3489 We check specially because in some obscure cases
3490 sec->output_section will be NULL. */
3491 relocation = 0;
3493 #else
3494 /* FIXME: Are not these obscure cases simply bugs? Let's
3495 get something working and come back to this. */
3496 if (sec->output_section == NULL)
3497 relocation = 0;
3498 #endif /* rth_notdef */
3499 else
3501 relocation = (h->root.root.u.def.value
3502 + sec->output_section->vma
3503 + sec->output_offset);
3506 else if (h->root.root.type == bfd_link_hash_undefweak)
3507 relocation = 0;
3508 else if (info->shared && !info->symbolic && !info->no_undefined)
3509 relocation = 0;
3510 else
3512 if (!((*info->callbacks->undefined_symbol)
3513 (info, h->root.root.root.string, input_bfd,
3514 input_section, rel->r_offset)))
3515 return false;
3516 relocation = 0;
3519 addend = rel->r_addend;
3521 switch (r_type)
3523 case R_ALPHA_GPDISP:
3525 bfd_byte *p_ldah, *p_lda;
3527 BFD_ASSERT(gp != 0);
3529 relocation = (input_section->output_section->vma
3530 + input_section->output_offset
3531 + rel->r_offset);
3533 p_ldah = contents + rel->r_offset - input_section->vma;
3534 p_lda = p_ldah + rel->r_addend;
3536 r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
3537 p_ldah, p_lda);
3539 break;
3541 case R_ALPHA_OP_PUSH:
3542 case R_ALPHA_OP_STORE:
3543 case R_ALPHA_OP_PSUB:
3544 case R_ALPHA_OP_PRSHIFT:
3545 /* We hate these silly beasts. */
3546 abort();
3548 case R_ALPHA_LITERAL:
3550 struct alpha_elf_got_entry *gotent;
3551 boolean dynamic_symbol;
3553 BFD_ASSERT(sgot != NULL);
3554 BFD_ASSERT(gp != 0);
3556 if (h != NULL)
3558 gotent = h->got_entries;
3559 dynamic_symbol = alpha_elf_dynamic_symbol_p (&h->root, info);
3561 else
3563 gotent = (alpha_elf_tdata(input_bfd)->
3564 local_got_entries[r_symndx]);
3565 dynamic_symbol = false;
3568 BFD_ASSERT(gotent != NULL);
3570 while (gotent->gotobj != gotobj || gotent->addend != addend)
3571 gotent = gotent->next;
3573 BFD_ASSERT(gotent->use_count >= 1);
3575 /* Initialize the .got entry's value. */
3576 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
3578 bfd_put_64 (output_bfd, relocation+addend,
3579 sgot->contents + gotent->got_offset);
3581 /* If the symbol has been forced local, output a
3582 RELATIVE reloc, otherwise it will be handled in
3583 finish_dynamic_symbol. */
3584 if (info->shared && !dynamic_symbol)
3586 Elf_Internal_Rela outrel;
3588 BFD_ASSERT(srelgot != NULL);
3590 outrel.r_offset = (sgot->output_section->vma
3591 + sgot->output_offset
3592 + gotent->got_offset);
3593 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3594 outrel.r_addend = 0;
3596 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3597 ((Elf64_External_Rela *)
3598 srelgot->contents)
3599 + srelgot->reloc_count++);
3600 BFD_ASSERT (sizeof(Elf64_External_Rela)
3601 * srelgot->reloc_count
3602 <= srelgot->_cooked_size);
3605 gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
3608 /* Figure the gprel relocation. */
3609 addend = 0;
3610 relocation = (sgot->output_section->vma
3611 + sgot->output_offset
3612 + gotent->got_offset);
3613 relocation -= gp;
3615 /* overflow handled by _bfd_final_link_relocate */
3616 goto default_reloc;
3618 case R_ALPHA_GPREL32:
3619 case R_ALPHA_GPRELLOW:
3620 BFD_ASSERT(gp != 0);
3621 relocation -= gp;
3622 goto default_reloc;
3624 case R_ALPHA_GPRELHIGH:
3625 BFD_ASSERT(gp != 0);
3626 relocation -= gp;
3627 relocation += addend;
3628 addend = 0;
3629 relocation = (((bfd_signed_vma) relocation >> 16)
3630 + ((relocation >> 15) & 1));
3631 goto default_reloc;
3633 case R_ALPHA_BRADDR:
3634 case R_ALPHA_HINT:
3635 /* The regular PC-relative stuff measures from the start of
3636 the instruction rather than the end. */
3637 addend -= 4;
3638 goto default_reloc;
3640 case R_ALPHA_REFLONG:
3641 case R_ALPHA_REFQUAD:
3643 Elf_Internal_Rela outrel;
3644 boolean skip;
3646 /* Careful here to remember RELATIVE relocations for global
3647 variables for symbolic shared objects. */
3649 if (h && alpha_elf_dynamic_symbol_p (&h->root, info))
3651 BFD_ASSERT(h->root.dynindx != -1);
3652 outrel.r_info = ELF64_R_INFO(h->root.dynindx, r_type);
3653 outrel.r_addend = addend;
3654 addend = 0, relocation = 0;
3656 else if (info->shared)
3658 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3659 outrel.r_addend = 0;
3661 else
3662 goto default_reloc;
3664 if (!srel)
3666 const char *name;
3668 name = (bfd_elf_string_from_elf_section
3669 (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
3670 elf_section_data(input_section)->rel_hdr.sh_name));
3671 BFD_ASSERT(name != NULL);
3673 srel = bfd_get_section_by_name (dynobj, name);
3674 BFD_ASSERT(srel != NULL);
3677 skip = false;
3679 if (elf_section_data (input_section)->stab_info == NULL)
3680 outrel.r_offset = rel->r_offset;
3681 else
3683 bfd_vma off;
3685 off = (_bfd_stab_section_offset
3686 (output_bfd, &elf_hash_table (info)->stab_info,
3687 input_section,
3688 &elf_section_data (input_section)->stab_info,
3689 rel->r_offset));
3690 if (off == (bfd_vma) -1)
3691 skip = true;
3692 outrel.r_offset = off;
3695 if (! skip)
3696 outrel.r_offset += (input_section->output_section->vma
3697 + input_section->output_offset);
3698 else
3699 memset (&outrel, 0, sizeof outrel);
3701 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3702 ((Elf64_External_Rela *)
3703 srel->contents)
3704 + srel->reloc_count++);
3705 BFD_ASSERT (sizeof(Elf64_External_Rela) * srel->reloc_count
3706 <= srel->_cooked_size);
3708 goto default_reloc;
3710 default:
3711 default_reloc:
3712 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3713 contents, rel->r_offset, relocation,
3714 addend);
3715 break;
3718 switch (r)
3720 case bfd_reloc_ok:
3721 break;
3723 case bfd_reloc_overflow:
3725 const char *name;
3727 if (h != NULL)
3728 name = h->root.root.root.string;
3729 else
3731 name = (bfd_elf_string_from_elf_section
3732 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3733 if (name == NULL)
3734 return false;
3735 if (*name == '\0')
3736 name = bfd_section_name (input_bfd, sec);
3738 if (! ((*info->callbacks->reloc_overflow)
3739 (info, name, howto->name, (bfd_vma) 0,
3740 input_bfd, input_section, rel->r_offset)))
3741 return false;
3743 break;
3745 default:
3746 case bfd_reloc_outofrange:
3747 abort ();
3751 return true;
3754 /* Finish up dynamic symbol handling. We set the contents of various
3755 dynamic sections here. */
3757 static boolean
3758 elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
3759 bfd *output_bfd;
3760 struct bfd_link_info *info;
3761 struct elf_link_hash_entry *h;
3762 Elf_Internal_Sym *sym;
3764 bfd *dynobj = elf_hash_table(info)->dynobj;
3766 if (h->plt.offset != MINUS_ONE)
3768 /* Fill in the .plt entry for this symbol. */
3769 asection *splt, *sgot, *srel;
3770 Elf_Internal_Rela outrel;
3771 bfd_vma got_addr, plt_addr;
3772 bfd_vma plt_index;
3773 struct alpha_elf_got_entry *gotent;
3775 BFD_ASSERT (h->dynindx != -1);
3777 /* The first .got entry will be updated by the .plt with the
3778 address of the target function. */
3779 gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3780 BFD_ASSERT (gotent && gotent->addend == 0);
3782 splt = bfd_get_section_by_name (dynobj, ".plt");
3783 BFD_ASSERT (splt != NULL);
3784 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3785 BFD_ASSERT (srel != NULL);
3786 sgot = alpha_elf_tdata (gotent->gotobj)->got;
3787 BFD_ASSERT (sgot != NULL);
3789 got_addr = (sgot->output_section->vma
3790 + sgot->output_offset
3791 + gotent->got_offset);
3792 plt_addr = (splt->output_section->vma
3793 + splt->output_offset
3794 + h->plt.offset);
3796 plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3798 /* Fill in the entry in the procedure linkage table. */
3800 unsigned insn1, insn2, insn3;
3802 insn1 = PLT_ENTRY_WORD1 | ((-(h->plt.offset + 4) >> 2) & 0x1fffff);
3803 insn2 = PLT_ENTRY_WORD2;
3804 insn3 = PLT_ENTRY_WORD3;
3806 bfd_put_32 (output_bfd, insn1, splt->contents + h->plt.offset);
3807 bfd_put_32 (output_bfd, insn2, splt->contents + h->plt.offset + 4);
3808 bfd_put_32 (output_bfd, insn3, splt->contents + h->plt.offset + 8);
3811 /* Fill in the entry in the .rela.plt section. */
3812 outrel.r_offset = got_addr;
3813 outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
3814 outrel.r_addend = 0;
3816 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3817 ((Elf64_External_Rela *)srel->contents
3818 + plt_index));
3820 if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3822 /* Mark the symbol as undefined, rather than as defined in the
3823 .plt section. Leave the value alone. */
3824 sym->st_shndx = SHN_UNDEF;
3827 /* Fill in the entries in the .got. */
3828 bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
3830 /* Subsequent .got entries will continue to bounce through the .plt. */
3831 if (gotent->next)
3833 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3834 BFD_ASSERT (! info->shared || srel != NULL);
3836 gotent = gotent->next;
3839 sgot = alpha_elf_tdata(gotent->gotobj)->got;
3840 BFD_ASSERT(sgot != NULL);
3841 BFD_ASSERT(gotent->addend == 0);
3843 bfd_put_64 (output_bfd, plt_addr,
3844 sgot->contents + gotent->got_offset);
3846 if (info->shared)
3848 outrel.r_offset = (sgot->output_section->vma
3849 + sgot->output_offset
3850 + gotent->got_offset);
3851 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3852 outrel.r_addend = 0;
3854 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3855 ((Elf64_External_Rela *)
3856 srel->contents)
3857 + srel->reloc_count++);
3858 BFD_ASSERT (sizeof(Elf64_External_Rela) * srel->reloc_count
3859 <= srel->_cooked_size);
3862 gotent = gotent->next;
3864 while (gotent != NULL);
3867 else if (alpha_elf_dynamic_symbol_p (h, info))
3869 /* Fill in the dynamic relocations for this symbol's .got entries. */
3870 asection *srel;
3871 Elf_Internal_Rela outrel;
3872 struct alpha_elf_got_entry *gotent;
3874 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3875 BFD_ASSERT (srel != NULL);
3877 outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_GLOB_DAT);
3878 for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3879 gotent != NULL;
3880 gotent = gotent->next)
3882 asection *sgot = alpha_elf_tdata (gotent->gotobj)->got;
3883 outrel.r_offset = (sgot->output_section->vma
3884 + sgot->output_offset
3885 + gotent->got_offset);
3886 outrel.r_addend = gotent->addend;
3888 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3889 ((Elf64_External_Rela *)srel->contents
3890 + srel->reloc_count++));
3891 BFD_ASSERT (sizeof(Elf64_External_Rela) * srel->reloc_count
3892 <= srel->_cooked_size);
3896 /* Mark some specially defined symbols as absolute. */
3897 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3898 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3899 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3900 sym->st_shndx = SHN_ABS;
3902 return true;
3905 /* Finish up the dynamic sections. */
3907 static boolean
3908 elf64_alpha_finish_dynamic_sections (output_bfd, info)
3909 bfd *output_bfd;
3910 struct bfd_link_info *info;
3912 bfd *dynobj;
3913 asection *sdyn;
3915 dynobj = elf_hash_table (info)->dynobj;
3916 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3918 if (elf_hash_table (info)->dynamic_sections_created)
3920 asection *splt;
3921 Elf64_External_Dyn *dyncon, *dynconend;
3923 splt = bfd_get_section_by_name (dynobj, ".plt");
3924 BFD_ASSERT (splt != NULL && sdyn != NULL);
3926 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3927 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3928 for (; dyncon < dynconend; dyncon++)
3930 Elf_Internal_Dyn dyn;
3931 const char *name;
3932 asection *s;
3934 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3936 switch (dyn.d_tag)
3938 case DT_PLTGOT:
3939 name = ".plt";
3940 goto get_vma;
3941 case DT_PLTRELSZ:
3942 name = ".rela.plt";
3943 goto get_size;
3944 case DT_JMPREL:
3945 name = ".rela.plt";
3946 goto get_vma;
3948 case DT_RELASZ:
3949 /* My interpretation of the TIS v1.1 ELF document indicates
3950 that RELASZ should not include JMPREL. This is not what
3951 the rest of the BFD does. It is, however, what the
3952 glibc ld.so wants. Do this fixup here until we found
3953 out who is right. */
3954 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
3955 if (s)
3957 dyn.d_un.d_val -=
3958 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3960 break;
3962 get_vma:
3963 s = bfd_get_section_by_name (output_bfd, name);
3964 dyn.d_un.d_ptr = (s ? s->vma : 0);
3965 break;
3967 get_size:
3968 s = bfd_get_section_by_name (output_bfd, name);
3969 dyn.d_un.d_val =
3970 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3971 break;
3974 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3977 /* Initialize the PLT0 entry */
3978 if (splt->_raw_size > 0)
3980 bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
3981 bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
3982 bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
3983 bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
3985 /* The next two words will be filled in by ld.so */
3986 bfd_put_64 (output_bfd, 0, splt->contents + 16);
3987 bfd_put_64 (output_bfd, 0, splt->contents + 24);
3989 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3990 PLT_HEADER_SIZE;
3994 if (info->shared)
3996 asection *sdynsym;
3997 asection *s;
3998 Elf_Internal_Sym sym;
4000 /* Set up the section symbols for the output sections. */
4002 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
4003 BFD_ASSERT (sdynsym != NULL);
4005 sym.st_size = 0;
4006 sym.st_name = 0;
4007 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4008 sym.st_other = 0;
4010 for (s = output_bfd->sections; s != NULL; s = s->next)
4012 int indx;
4014 sym.st_value = s->vma;
4016 indx = elf_section_data (s)->this_idx;
4017 BFD_ASSERT (indx > 0);
4018 sym.st_shndx = indx;
4020 bfd_elf64_swap_symbol_out (output_bfd, &sym,
4021 (PTR) (((Elf64_External_Sym *)
4022 sdynsym->contents)
4023 + elf_section_data (s)->dynindx));
4026 /* Set the sh_info field of the output .dynsym section to the
4027 index of the first global symbol. */
4028 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
4029 bfd_count_sections (output_bfd) + 1;
4032 return true;
4035 /* We need to use a special link routine to handle the .reginfo and
4036 the .mdebug sections. We need to merge all instances of these
4037 sections together, not write them all out sequentially. */
4039 static boolean
4040 elf64_alpha_final_link (abfd, info)
4041 bfd *abfd;
4042 struct bfd_link_info *info;
4044 asection *o;
4045 struct bfd_link_order *p;
4046 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4047 struct ecoff_debug_info debug;
4048 const struct ecoff_debug_swap *swap
4049 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4050 HDRR *symhdr = &debug.symbolic_header;
4051 PTR mdebug_handle = NULL;
4053 #if 0
4054 if (++ngots == 2)
4056 (*info->callbacks->warning)
4057 (info, _("using multiple gp values"), (char *) NULL,
4058 output_bfd, (asection *) NULL, (bfd_vma) 0);
4060 #endif
4062 /* Go through the sections and collect the .reginfo and .mdebug
4063 information. */
4064 reginfo_sec = NULL;
4065 mdebug_sec = NULL;
4066 gptab_data_sec = NULL;
4067 gptab_bss_sec = NULL;
4068 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4070 #ifdef ERIC_neverdef
4071 if (strcmp (o->name, ".reginfo") == 0)
4073 memset (&reginfo, 0, sizeof reginfo);
4075 /* We have found the .reginfo section in the output file.
4076 Look through all the link_orders comprising it and merge
4077 the information together. */
4078 for (p = o->link_order_head;
4079 p != (struct bfd_link_order *) NULL;
4080 p = p->next)
4082 asection *input_section;
4083 bfd *input_bfd;
4084 Elf64_External_RegInfo ext;
4085 Elf64_RegInfo sub;
4087 if (p->type != bfd_indirect_link_order)
4089 if (p->type == bfd_fill_link_order)
4090 continue;
4091 abort ();
4094 input_section = p->u.indirect.section;
4095 input_bfd = input_section->owner;
4097 /* The linker emulation code has probably clobbered the
4098 size to be zero bytes. */
4099 if (input_section->_raw_size == 0)
4100 input_section->_raw_size = sizeof (Elf64_External_RegInfo);
4102 if (! bfd_get_section_contents (input_bfd, input_section,
4103 (PTR) &ext,
4104 (file_ptr) 0,
4105 sizeof ext))
4106 return false;
4108 bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
4110 reginfo.ri_gprmask |= sub.ri_gprmask;
4111 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4112 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4113 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4114 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4116 /* ri_gp_value is set by the function
4117 alpha_elf_section_processing when the section is
4118 finally written out. */
4120 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4121 elf_link_input_bfd ignores this section. */
4122 input_section->flags &=~ SEC_HAS_CONTENTS;
4125 /* Force the section size to the value we want. */
4126 o->_raw_size = sizeof (Elf64_External_RegInfo);
4128 /* Skip this section later on (I don't think this currently
4129 matters, but someday it might). */
4130 o->link_order_head = (struct bfd_link_order *) NULL;
4132 reginfo_sec = o;
4134 #endif
4136 if (strcmp (o->name, ".mdebug") == 0)
4138 struct extsym_info einfo;
4140 /* We have found the .mdebug section in the output file.
4141 Look through all the link_orders comprising it and merge
4142 the information together. */
4143 symhdr->magic = swap->sym_magic;
4144 /* FIXME: What should the version stamp be? */
4145 symhdr->vstamp = 0;
4146 symhdr->ilineMax = 0;
4147 symhdr->cbLine = 0;
4148 symhdr->idnMax = 0;
4149 symhdr->ipdMax = 0;
4150 symhdr->isymMax = 0;
4151 symhdr->ioptMax = 0;
4152 symhdr->iauxMax = 0;
4153 symhdr->issMax = 0;
4154 symhdr->issExtMax = 0;
4155 symhdr->ifdMax = 0;
4156 symhdr->crfd = 0;
4157 symhdr->iextMax = 0;
4159 /* We accumulate the debugging information itself in the
4160 debug_info structure. */
4161 debug.line = NULL;
4162 debug.external_dnr = NULL;
4163 debug.external_pdr = NULL;
4164 debug.external_sym = NULL;
4165 debug.external_opt = NULL;
4166 debug.external_aux = NULL;
4167 debug.ss = NULL;
4168 debug.ssext = debug.ssext_end = NULL;
4169 debug.external_fdr = NULL;
4170 debug.external_rfd = NULL;
4171 debug.external_ext = debug.external_ext_end = NULL;
4173 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4174 if (mdebug_handle == (PTR) NULL)
4175 return false;
4177 if (1)
4179 asection *s;
4180 EXTR esym;
4181 bfd_vma last;
4182 unsigned int i;
4183 static const char * const name[] =
4185 ".text", ".init", ".fini", ".data",
4186 ".rodata", ".sdata", ".sbss", ".bss"
4188 static const int sc[] = { scText, scInit, scFini, scData,
4189 scRData, scSData, scSBss, scBss };
4191 esym.jmptbl = 0;
4192 esym.cobol_main = 0;
4193 esym.weakext = 0;
4194 esym.reserved = 0;
4195 esym.ifd = ifdNil;
4196 esym.asym.iss = issNil;
4197 esym.asym.st = stLocal;
4198 esym.asym.reserved = 0;
4199 esym.asym.index = indexNil;
4200 for (i = 0; i < 8; i++)
4202 esym.asym.sc = sc[i];
4203 s = bfd_get_section_by_name (abfd, name[i]);
4204 if (s != NULL)
4206 esym.asym.value = s->vma;
4207 last = s->vma + s->_raw_size;
4209 else
4210 esym.asym.value = last;
4212 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4213 name[i], &esym))
4214 return false;
4218 for (p = o->link_order_head;
4219 p != (struct bfd_link_order *) NULL;
4220 p = p->next)
4222 asection *input_section;
4223 bfd *input_bfd;
4224 const struct ecoff_debug_swap *input_swap;
4225 struct ecoff_debug_info input_debug;
4226 char *eraw_src;
4227 char *eraw_end;
4229 if (p->type != bfd_indirect_link_order)
4231 if (p->type == bfd_fill_link_order)
4232 continue;
4233 abort ();
4236 input_section = p->u.indirect.section;
4237 input_bfd = input_section->owner;
4239 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4240 || (get_elf_backend_data (input_bfd)
4241 ->elf_backend_ecoff_debug_swap) == NULL)
4243 /* I don't know what a non ALPHA ELF bfd would be
4244 doing with a .mdebug section, but I don't really
4245 want to deal with it. */
4246 continue;
4249 input_swap = (get_elf_backend_data (input_bfd)
4250 ->elf_backend_ecoff_debug_swap);
4252 BFD_ASSERT (p->size == input_section->_raw_size);
4254 /* The ECOFF linking code expects that we have already
4255 read in the debugging information and set up an
4256 ecoff_debug_info structure, so we do that now. */
4257 if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
4258 &input_debug))
4259 return false;
4261 if (! (bfd_ecoff_debug_accumulate
4262 (mdebug_handle, abfd, &debug, swap, input_bfd,
4263 &input_debug, input_swap, info)))
4264 return false;
4266 /* Loop through the external symbols. For each one with
4267 interesting information, try to find the symbol in
4268 the linker global hash table and save the information
4269 for the output external symbols. */
4270 eraw_src = input_debug.external_ext;
4271 eraw_end = (eraw_src
4272 + (input_debug.symbolic_header.iextMax
4273 * input_swap->external_ext_size));
4274 for (;
4275 eraw_src < eraw_end;
4276 eraw_src += input_swap->external_ext_size)
4278 EXTR ext;
4279 const char *name;
4280 struct alpha_elf_link_hash_entry *h;
4282 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4283 if (ext.asym.sc == scNil
4284 || ext.asym.sc == scUndefined
4285 || ext.asym.sc == scSUndefined)
4286 continue;
4288 name = input_debug.ssext + ext.asym.iss;
4289 h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
4290 name, false, false, true);
4291 if (h == NULL || h->esym.ifd != -2)
4292 continue;
4294 if (ext.ifd != -1)
4296 BFD_ASSERT (ext.ifd
4297 < input_debug.symbolic_header.ifdMax);
4298 ext.ifd = input_debug.ifdmap[ext.ifd];
4301 h->esym = ext;
4304 /* Free up the information we just read. */
4305 free (input_debug.line);
4306 free (input_debug.external_dnr);
4307 free (input_debug.external_pdr);
4308 free (input_debug.external_sym);
4309 free (input_debug.external_opt);
4310 free (input_debug.external_aux);
4311 free (input_debug.ss);
4312 free (input_debug.ssext);
4313 free (input_debug.external_fdr);
4314 free (input_debug.external_rfd);
4315 free (input_debug.external_ext);
4317 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4318 elf_link_input_bfd ignores this section. */
4319 input_section->flags &=~ SEC_HAS_CONTENTS;
4322 #ifdef ERIC_neverdef
4323 if (info->shared)
4325 /* Create .rtproc section. */
4326 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4327 if (rtproc_sec == NULL)
4329 flagword flags = (SEC_HAS_CONTENTS
4330 | SEC_IN_MEMORY
4331 | SEC_LINKER_CREATED
4332 | SEC_READONLY);
4334 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4335 if (rtproc_sec == NULL
4336 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4337 || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
4338 return false;
4341 if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
4342 info, rtproc_sec, &debug))
4343 return false;
4345 #endif
4348 /* Build the external symbol information. */
4349 einfo.abfd = abfd;
4350 einfo.info = info;
4351 einfo.debug = &debug;
4352 einfo.swap = swap;
4353 einfo.failed = false;
4354 elf_link_hash_traverse (elf_hash_table (info),
4355 elf64_alpha_output_extsym,
4356 (PTR) &einfo);
4357 if (einfo.failed)
4358 return false;
4360 /* Set the size of the .mdebug section. */
4361 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4363 /* Skip this section later on (I don't think this currently
4364 matters, but someday it might). */
4365 o->link_order_head = (struct bfd_link_order *) NULL;
4367 mdebug_sec = o;
4370 #ifdef ERIC_neverdef
4371 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4373 const char *subname;
4374 unsigned int c;
4375 Elf64_gptab *tab;
4376 Elf64_External_gptab *ext_tab;
4377 unsigned int i;
4379 /* The .gptab.sdata and .gptab.sbss sections hold
4380 information describing how the small data area would
4381 change depending upon the -G switch. These sections
4382 not used in executables files. */
4383 if (! info->relocateable)
4385 asection **secpp;
4387 for (p = o->link_order_head;
4388 p != (struct bfd_link_order *) NULL;
4389 p = p->next)
4391 asection *input_section;
4393 if (p->type != bfd_indirect_link_order)
4395 if (p->type == bfd_fill_link_order)
4396 continue;
4397 abort ();
4400 input_section = p->u.indirect.section;
4402 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4403 elf_link_input_bfd ignores this section. */
4404 input_section->flags &=~ SEC_HAS_CONTENTS;
4407 /* Skip this section later on (I don't think this
4408 currently matters, but someday it might). */
4409 o->link_order_head = (struct bfd_link_order *) NULL;
4411 /* Really remove the section. */
4412 for (secpp = &abfd->sections;
4413 *secpp != o;
4414 secpp = &(*secpp)->next)
4416 *secpp = (*secpp)->next;
4417 --abfd->section_count;
4419 continue;
4422 /* There is one gptab for initialized data, and one for
4423 uninitialized data. */
4424 if (strcmp (o->name, ".gptab.sdata") == 0)
4425 gptab_data_sec = o;
4426 else if (strcmp (o->name, ".gptab.sbss") == 0)
4427 gptab_bss_sec = o;
4428 else
4430 (*_bfd_error_handler)
4431 (_("%s: illegal section name `%s'"),
4432 bfd_get_filename (abfd), o->name);
4433 bfd_set_error (bfd_error_nonrepresentable_section);
4434 return false;
4437 /* The linker script always combines .gptab.data and
4438 .gptab.sdata into .gptab.sdata, and likewise for
4439 .gptab.bss and .gptab.sbss. It is possible that there is
4440 no .sdata or .sbss section in the output file, in which
4441 case we must change the name of the output section. */
4442 subname = o->name + sizeof ".gptab" - 1;
4443 if (bfd_get_section_by_name (abfd, subname) == NULL)
4445 if (o == gptab_data_sec)
4446 o->name = ".gptab.data";
4447 else
4448 o->name = ".gptab.bss";
4449 subname = o->name + sizeof ".gptab" - 1;
4450 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4453 /* Set up the first entry. */
4454 c = 1;
4455 tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
4456 if (tab == NULL)
4457 return false;
4458 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4459 tab[0].gt_header.gt_unused = 0;
4461 /* Combine the input sections. */
4462 for (p = o->link_order_head;
4463 p != (struct bfd_link_order *) NULL;
4464 p = p->next)
4466 asection *input_section;
4467 bfd *input_bfd;
4468 bfd_size_type size;
4469 unsigned long last;
4470 bfd_size_type gpentry;
4472 if (p->type != bfd_indirect_link_order)
4474 if (p->type == bfd_fill_link_order)
4475 continue;
4476 abort ();
4479 input_section = p->u.indirect.section;
4480 input_bfd = input_section->owner;
4482 /* Combine the gptab entries for this input section one
4483 by one. We know that the input gptab entries are
4484 sorted by ascending -G value. */
4485 size = bfd_section_size (input_bfd, input_section);
4486 last = 0;
4487 for (gpentry = sizeof (Elf64_External_gptab);
4488 gpentry < size;
4489 gpentry += sizeof (Elf64_External_gptab))
4491 Elf64_External_gptab ext_gptab;
4492 Elf64_gptab int_gptab;
4493 unsigned long val;
4494 unsigned long add;
4495 boolean exact;
4496 unsigned int look;
4498 if (! (bfd_get_section_contents
4499 (input_bfd, input_section, (PTR) &ext_gptab,
4500 gpentry, sizeof (Elf64_External_gptab))))
4502 free (tab);
4503 return false;
4506 bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
4507 &int_gptab);
4508 val = int_gptab.gt_entry.gt_g_value;
4509 add = int_gptab.gt_entry.gt_bytes - last;
4511 exact = false;
4512 for (look = 1; look < c; look++)
4514 if (tab[look].gt_entry.gt_g_value >= val)
4515 tab[look].gt_entry.gt_bytes += add;
4517 if (tab[look].gt_entry.gt_g_value == val)
4518 exact = true;
4521 if (! exact)
4523 Elf64_gptab *new_tab;
4524 unsigned int max;
4526 /* We need a new table entry. */
4527 new_tab = ((Elf64_gptab *)
4528 bfd_realloc ((PTR) tab,
4529 (c + 1) * sizeof (Elf64_gptab)));
4530 if (new_tab == NULL)
4532 free (tab);
4533 return false;
4535 tab = new_tab;
4536 tab[c].gt_entry.gt_g_value = val;
4537 tab[c].gt_entry.gt_bytes = add;
4539 /* Merge in the size for the next smallest -G
4540 value, since that will be implied by this new
4541 value. */
4542 max = 0;
4543 for (look = 1; look < c; look++)
4545 if (tab[look].gt_entry.gt_g_value < val
4546 && (max == 0
4547 || (tab[look].gt_entry.gt_g_value
4548 > tab[max].gt_entry.gt_g_value)))
4549 max = look;
4551 if (max != 0)
4552 tab[c].gt_entry.gt_bytes +=
4553 tab[max].gt_entry.gt_bytes;
4555 ++c;
4558 last = int_gptab.gt_entry.gt_bytes;
4561 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4562 elf_link_input_bfd ignores this section. */
4563 input_section->flags &=~ SEC_HAS_CONTENTS;
4566 /* The table must be sorted by -G value. */
4567 if (c > 2)
4568 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4570 /* Swap out the table. */
4571 ext_tab = ((Elf64_External_gptab *)
4572 bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
4573 if (ext_tab == NULL)
4575 free (tab);
4576 return false;
4579 for (i = 0; i < c; i++)
4580 bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
4581 free (tab);
4583 o->_raw_size = c * sizeof (Elf64_External_gptab);
4584 o->contents = (bfd_byte *) ext_tab;
4586 /* Skip this section later on (I don't think this currently
4587 matters, but someday it might). */
4588 o->link_order_head = (struct bfd_link_order *) NULL;
4590 #endif
4594 /* Invoke the regular ELF backend linker to do all the work. */
4595 if (! bfd_elf64_bfd_final_link (abfd, info))
4596 return false;
4598 /* Now write out the computed sections. */
4600 /* The .got subsections... */
4602 bfd *i, *dynobj = elf_hash_table(info)->dynobj;
4603 for (i = alpha_elf_hash_table(info)->got_list;
4604 i != NULL;
4605 i = alpha_elf_tdata(i)->got_link_next)
4607 asection *sgot;
4609 /* elf_bfd_final_link already did everything in dynobj. */
4610 if (i == dynobj)
4611 continue;
4613 sgot = alpha_elf_tdata(i)->got;
4614 if (! bfd_set_section_contents (abfd, sgot->output_section,
4615 sgot->contents, sgot->output_offset,
4616 sgot->_raw_size))
4617 return false;
4621 #ifdef ERIC_neverdef
4622 if (reginfo_sec != (asection *) NULL)
4624 Elf64_External_RegInfo ext;
4626 bfd_alpha_elf64_swap_reginfo_out (abfd, &reginfo, &ext);
4627 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4628 (file_ptr) 0, sizeof ext))
4629 return false;
4631 #endif
4633 if (mdebug_sec != (asection *) NULL)
4635 BFD_ASSERT (abfd->output_has_begun);
4636 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4637 swap, info,
4638 mdebug_sec->filepos))
4639 return false;
4641 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4644 if (gptab_data_sec != (asection *) NULL)
4646 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4647 gptab_data_sec->contents,
4648 (file_ptr) 0,
4649 gptab_data_sec->_raw_size))
4650 return false;
4653 if (gptab_bss_sec != (asection *) NULL)
4655 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4656 gptab_bss_sec->contents,
4657 (file_ptr) 0,
4658 gptab_bss_sec->_raw_size))
4659 return false;
4662 return true;
4665 /* ECOFF swapping routines. These are used when dealing with the
4666 .mdebug section, which is in the ECOFF debugging format. Copied
4667 from elf32-mips.c. */
4668 static const struct ecoff_debug_swap
4669 elf64_alpha_ecoff_debug_swap =
4671 /* Symbol table magic number. */
4672 magicSym2,
4673 /* Alignment of debugging information. E.g., 4. */
4675 /* Sizes of external symbolic information. */
4676 sizeof (struct hdr_ext),
4677 sizeof (struct dnr_ext),
4678 sizeof (struct pdr_ext),
4679 sizeof (struct sym_ext),
4680 sizeof (struct opt_ext),
4681 sizeof (struct fdr_ext),
4682 sizeof (struct rfd_ext),
4683 sizeof (struct ext_ext),
4684 /* Functions to swap in external symbolic data. */
4685 ecoff_swap_hdr_in,
4686 ecoff_swap_dnr_in,
4687 ecoff_swap_pdr_in,
4688 ecoff_swap_sym_in,
4689 ecoff_swap_opt_in,
4690 ecoff_swap_fdr_in,
4691 ecoff_swap_rfd_in,
4692 ecoff_swap_ext_in,
4693 _bfd_ecoff_swap_tir_in,
4694 _bfd_ecoff_swap_rndx_in,
4695 /* Functions to swap out external symbolic data. */
4696 ecoff_swap_hdr_out,
4697 ecoff_swap_dnr_out,
4698 ecoff_swap_pdr_out,
4699 ecoff_swap_sym_out,
4700 ecoff_swap_opt_out,
4701 ecoff_swap_fdr_out,
4702 ecoff_swap_rfd_out,
4703 ecoff_swap_ext_out,
4704 _bfd_ecoff_swap_tir_out,
4705 _bfd_ecoff_swap_rndx_out,
4706 /* Function to read in symbolic data. */
4707 elf64_alpha_read_ecoff_info
4710 #define TARGET_LITTLE_SYM bfd_elf64_alpha_vec
4711 #define TARGET_LITTLE_NAME "elf64-alpha"
4712 #define ELF_ARCH bfd_arch_alpha
4713 #define ELF_MACHINE_CODE EM_ALPHA
4714 #define ELF_MAXPAGESIZE 0x10000
4716 #define bfd_elf64_bfd_link_hash_table_create \
4717 elf64_alpha_bfd_link_hash_table_create
4719 #define bfd_elf64_bfd_reloc_type_lookup \
4720 elf64_alpha_bfd_reloc_type_lookup
4721 #define elf_info_to_howto \
4722 elf64_alpha_info_to_howto
4724 #define bfd_elf64_mkobject \
4725 elf64_alpha_mkobject
4726 #define elf_backend_object_p \
4727 elf64_alpha_object_p
4729 #define elf_backend_section_from_shdr \
4730 elf64_alpha_section_from_shdr
4731 #define elf_backend_fake_sections \
4732 elf64_alpha_fake_sections
4734 #define bfd_elf64_bfd_is_local_label_name \
4735 elf64_alpha_is_local_label_name
4736 #define bfd_elf64_find_nearest_line \
4737 elf64_alpha_find_nearest_line
4738 #define bfd_elf64_bfd_relax_section \
4739 elf64_alpha_relax_section
4741 #define elf_backend_add_symbol_hook \
4742 elf64_alpha_add_symbol_hook
4743 #define elf_backend_check_relocs \
4744 elf64_alpha_check_relocs
4745 #define elf_backend_create_dynamic_sections \
4746 elf64_alpha_create_dynamic_sections
4747 #define elf_backend_adjust_dynamic_symbol \
4748 elf64_alpha_adjust_dynamic_symbol
4749 #define elf_backend_always_size_sections \
4750 elf64_alpha_always_size_sections
4751 #define elf_backend_size_dynamic_sections \
4752 elf64_alpha_size_dynamic_sections
4753 #define elf_backend_relocate_section \
4754 elf64_alpha_relocate_section
4755 #define elf_backend_finish_dynamic_symbol \
4756 elf64_alpha_finish_dynamic_symbol
4757 #define elf_backend_finish_dynamic_sections \
4758 elf64_alpha_finish_dynamic_sections
4759 #define bfd_elf64_bfd_final_link \
4760 elf64_alpha_final_link
4762 #define elf_backend_ecoff_debug_swap \
4763 &elf64_alpha_ecoff_debug_swap
4766 * A few constants that determine how the .plt section is set up.
4768 #define elf_backend_want_got_plt 0
4769 #define elf_backend_plt_readonly 0
4770 #define elf_backend_want_plt_sym 1
4771 #define elf_backend_got_header_size 0
4772 #define elf_backend_plt_header_size PLT_HEADER_SIZE
4774 #include "elf64-target.h"