1 /* BFD back-end for ALPHA Extended-Coff files.
2 Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
3 Modified from coff-mips.c by Steve Chamberlain <sac@cygnus.com> and
4 Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "coff/internal.h"
28 #include "coff/symconst.h"
29 #include "coff/ecoff.h"
30 #include "coff/alpha.h"
35 /* Prototypes for static functions. */
37 static const bfd_target *alpha_ecoff_object_p PARAMS ((bfd *));
38 static boolean alpha_ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
39 static PTR alpha_ecoff_mkobject_hook PARAMS ((bfd *, PTR filehdr, PTR aouthdr));
40 static void alpha_ecoff_swap_reloc_in PARAMS ((bfd *, PTR,
41 struct internal_reloc *));
42 static void alpha_ecoff_swap_reloc_out PARAMS ((bfd *,
43 const struct internal_reloc *,
45 static void alpha_adjust_reloc_in PARAMS ((bfd *,
46 const struct internal_reloc *,
48 static void alpha_adjust_reloc_out PARAMS ((bfd *, const arelent *,
49 struct internal_reloc *));
50 static bfd_byte *alpha_ecoff_get_relocated_section_contents
51 PARAMS ((bfd *abfd, struct bfd_link_info *, struct bfd_link_order *,
52 bfd_byte *data, boolean relocateable, asymbol **symbols));
53 static bfd_vma alpha_convert_external_reloc
54 PARAMS ((bfd *, struct bfd_link_info *, bfd *, struct external_reloc *,
55 struct ecoff_link_hash_entry *));
56 static boolean alpha_relocate_section PARAMS ((bfd *, struct bfd_link_info *,
59 static boolean alpha_adjust_headers
60 PARAMS ((bfd *, struct internal_filehdr *, struct internal_aouthdr *));
61 static PTR alpha_ecoff_read_ar_hdr PARAMS ((bfd *));
62 static bfd *alpha_ecoff_get_elt_at_filepos PARAMS ((bfd *, file_ptr));
63 static bfd *alpha_ecoff_openr_next_archived_file PARAMS ((bfd *, bfd *));
64 static bfd *alpha_ecoff_get_elt_at_index PARAMS ((bfd *, symindex));
66 /* ECOFF has COFF sections, but the debugging information is stored in
67 a completely different format. ECOFF targets use some of the
68 swapping routines from coffswap.h, and some of the generic COFF
69 routines in coffgen.c, but, unlike the real COFF targets, do not
70 use coffcode.h itself.
72 Get the generic COFF swapping routines, except for the reloc,
73 symbol, and lineno ones. Give them ecoff names. Define some
74 accessor macros for the large sizes used for Alpha ECOFF. */
76 #define GET_FILEHDR_SYMPTR bfd_h_get_64
77 #define PUT_FILEHDR_SYMPTR bfd_h_put_64
78 #define GET_AOUTHDR_TSIZE bfd_h_get_64
79 #define PUT_AOUTHDR_TSIZE bfd_h_put_64
80 #define GET_AOUTHDR_DSIZE bfd_h_get_64
81 #define PUT_AOUTHDR_DSIZE bfd_h_put_64
82 #define GET_AOUTHDR_BSIZE bfd_h_get_64
83 #define PUT_AOUTHDR_BSIZE bfd_h_put_64
84 #define GET_AOUTHDR_ENTRY bfd_h_get_64
85 #define PUT_AOUTHDR_ENTRY bfd_h_put_64
86 #define GET_AOUTHDR_TEXT_START bfd_h_get_64
87 #define PUT_AOUTHDR_TEXT_START bfd_h_put_64
88 #define GET_AOUTHDR_DATA_START bfd_h_get_64
89 #define PUT_AOUTHDR_DATA_START bfd_h_put_64
90 #define GET_SCNHDR_PADDR bfd_h_get_64
91 #define PUT_SCNHDR_PADDR bfd_h_put_64
92 #define GET_SCNHDR_VADDR bfd_h_get_64
93 #define PUT_SCNHDR_VADDR bfd_h_put_64
94 #define GET_SCNHDR_SIZE bfd_h_get_64
95 #define PUT_SCNHDR_SIZE bfd_h_put_64
96 #define GET_SCNHDR_SCNPTR bfd_h_get_64
97 #define PUT_SCNHDR_SCNPTR bfd_h_put_64
98 #define GET_SCNHDR_RELPTR bfd_h_get_64
99 #define PUT_SCNHDR_RELPTR bfd_h_put_64
100 #define GET_SCNHDR_LNNOPTR bfd_h_get_64
101 #define PUT_SCNHDR_LNNOPTR bfd_h_put_64
105 #define NO_COFF_RELOCS
106 #define NO_COFF_SYMBOLS
107 #define NO_COFF_LINENOS
108 #define coff_swap_filehdr_in alpha_ecoff_swap_filehdr_in
109 #define coff_swap_filehdr_out alpha_ecoff_swap_filehdr_out
110 #define coff_swap_aouthdr_in alpha_ecoff_swap_aouthdr_in
111 #define coff_swap_aouthdr_out alpha_ecoff_swap_aouthdr_out
112 #define coff_swap_scnhdr_in alpha_ecoff_swap_scnhdr_in
113 #define coff_swap_scnhdr_out alpha_ecoff_swap_scnhdr_out
114 #include "coffswap.h"
116 /* Get the ECOFF swapping routines. */
118 #include "ecoffswap.h"
120 /* How to process the various reloc types. */
122 static bfd_reloc_status_type
123 reloc_nil PARAMS ((bfd *, arelent *, asymbol *, PTR,
124 asection *, bfd *, char **));
126 static bfd_reloc_status_type
127 reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
134 char **error_message;
139 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
140 from smaller values. Start with zero, widen, *then* decrement. */
141 #define MINUS_ONE (((bfd_vma)0) - 1)
143 static reloc_howto_type alpha_howto_table[] =
145 /* Reloc type 0 is ignored by itself. However, it appears after a
146 GPDISP reloc to identify the location where the low order 16 bits
147 of the gp register are loaded. */
148 HOWTO (ALPHA_R_IGNORE, /* type */
150 0, /* size (0 = byte, 1 = short, 2 = long) */
152 true, /* pc_relative */
154 complain_overflow_dont, /* complain_on_overflow */
155 reloc_nil, /* special_function */
157 true, /* partial_inplace */
160 true), /* pcrel_offset */
162 /* A 32 bit reference to a symbol. */
163 HOWTO (ALPHA_R_REFLONG, /* type */
165 2, /* size (0 = byte, 1 = short, 2 = long) */
167 false, /* pc_relative */
169 complain_overflow_bitfield, /* complain_on_overflow */
170 0, /* special_function */
171 "REFLONG", /* name */
172 true, /* partial_inplace */
173 0xffffffff, /* src_mask */
174 0xffffffff, /* dst_mask */
175 false), /* pcrel_offset */
177 /* A 64 bit reference to a symbol. */
178 HOWTO (ALPHA_R_REFQUAD, /* type */
180 4, /* size (0 = byte, 1 = short, 2 = long) */
182 false, /* pc_relative */
184 complain_overflow_bitfield, /* complain_on_overflow */
185 0, /* special_function */
186 "REFQUAD", /* name */
187 true, /* partial_inplace */
188 MINUS_ONE, /* src_mask */
189 MINUS_ONE, /* dst_mask */
190 false), /* pcrel_offset */
192 /* A 32 bit GP relative offset. This is just like REFLONG except
193 that when the value is used the value of the gp register will be
195 HOWTO (ALPHA_R_GPREL32, /* type */
197 2, /* size (0 = byte, 1 = short, 2 = long) */
199 false, /* pc_relative */
201 complain_overflow_bitfield, /* complain_on_overflow */
202 0, /* special_function */
203 "GPREL32", /* name */
204 true, /* partial_inplace */
205 0xffffffff, /* src_mask */
206 0xffffffff, /* dst_mask */
207 false), /* pcrel_offset */
209 /* Used for an instruction that refers to memory off the GP
210 register. The offset is 16 bits of the 32 bit instruction. This
211 reloc always seems to be against the .lita section. */
212 HOWTO (ALPHA_R_LITERAL, /* type */
214 2, /* size (0 = byte, 1 = short, 2 = long) */
216 false, /* pc_relative */
218 complain_overflow_signed, /* complain_on_overflow */
219 0, /* special_function */
220 "LITERAL", /* name */
221 true, /* partial_inplace */
222 0xffff, /* src_mask */
223 0xffff, /* dst_mask */
224 false), /* pcrel_offset */
226 /* This reloc only appears immediately following a LITERAL reloc.
227 It identifies a use of the literal. It seems that the linker can
228 use this to eliminate a portion of the .lita section. The symbol
229 index is special: 1 means the literal address is in the base
230 register of a memory format instruction; 2 means the literal
231 address is in the byte offset register of a byte-manipulation
232 instruction; 3 means the literal address is in the target
233 register of a jsr instruction. This does not actually do any
235 HOWTO (ALPHA_R_LITUSE, /* type */
237 2, /* size (0 = byte, 1 = short, 2 = long) */
239 false, /* pc_relative */
241 complain_overflow_dont, /* complain_on_overflow */
242 reloc_nil, /* special_function */
244 false, /* partial_inplace */
247 false), /* pcrel_offset */
249 /* Load the gp register. This is always used for a ldah instruction
250 which loads the upper 16 bits of the gp register. The next reloc
251 will be an IGNORE reloc which identifies the location of the lda
252 instruction which loads the lower 16 bits. The symbol index of
253 the GPDISP instruction appears to actually be the number of bytes
254 between the ldah and lda instructions. This gives two different
255 ways to determine where the lda instruction is; I don't know why
256 both are used. The value to use for the relocation is the
257 difference between the GP value and the current location; the
258 load will always be done against a register holding the current
260 HOWTO (ALPHA_R_GPDISP, /* type */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
264 true, /* pc_relative */
266 complain_overflow_dont, /* complain_on_overflow */
267 reloc_nil, /* special_function */
269 true, /* partial_inplace */
270 0xffff, /* src_mask */
271 0xffff, /* dst_mask */
272 true), /* pcrel_offset */
274 /* A 21 bit branch. The native assembler generates these for
275 branches within the text segment, and also fills in the PC
276 relative offset in the instruction. */
277 HOWTO (ALPHA_R_BRADDR, /* type */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
281 true, /* pc_relative */
283 complain_overflow_signed, /* complain_on_overflow */
284 0, /* special_function */
286 true, /* partial_inplace */
287 0x1fffff, /* src_mask */
288 0x1fffff, /* dst_mask */
289 false), /* pcrel_offset */
291 /* A hint for a jump to a register. */
292 HOWTO (ALPHA_R_HINT, /* type */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
296 true, /* pc_relative */
298 complain_overflow_dont, /* complain_on_overflow */
299 0, /* special_function */
301 true, /* partial_inplace */
302 0x3fff, /* src_mask */
303 0x3fff, /* dst_mask */
304 false), /* pcrel_offset */
306 /* 16 bit PC relative offset. */
307 HOWTO (ALPHA_R_SREL16, /* type */
309 1, /* size (0 = byte, 1 = short, 2 = long) */
311 true, /* pc_relative */
313 complain_overflow_signed, /* complain_on_overflow */
314 0, /* special_function */
316 true, /* partial_inplace */
317 0xffff, /* src_mask */
318 0xffff, /* dst_mask */
319 false), /* pcrel_offset */
321 /* 32 bit PC relative offset. */
322 HOWTO (ALPHA_R_SREL32, /* type */
324 2, /* size (0 = byte, 1 = short, 2 = long) */
326 true, /* pc_relative */
328 complain_overflow_signed, /* complain_on_overflow */
329 0, /* special_function */
331 true, /* partial_inplace */
332 0xffffffff, /* src_mask */
333 0xffffffff, /* dst_mask */
334 false), /* pcrel_offset */
336 /* A 64 bit PC relative offset. */
337 HOWTO (ALPHA_R_SREL64, /* type */
339 4, /* size (0 = byte, 1 = short, 2 = long) */
341 true, /* pc_relative */
343 complain_overflow_signed, /* complain_on_overflow */
344 0, /* special_function */
346 true, /* partial_inplace */
347 MINUS_ONE, /* src_mask */
348 MINUS_ONE, /* dst_mask */
349 false), /* pcrel_offset */
351 /* Push a value on the reloc evaluation stack. */
352 HOWTO (ALPHA_R_OP_PUSH, /* type */
354 0, /* size (0 = byte, 1 = short, 2 = long) */
356 false, /* pc_relative */
358 complain_overflow_dont, /* complain_on_overflow */
359 0, /* special_function */
360 "OP_PUSH", /* name */
361 false, /* partial_inplace */
364 false), /* pcrel_offset */
366 /* Store the value from the stack at the given address. Store it in
367 a bitfield of size r_size starting at bit position r_offset. */
368 HOWTO (ALPHA_R_OP_STORE, /* type */
370 4, /* size (0 = byte, 1 = short, 2 = long) */
372 false, /* pc_relative */
374 complain_overflow_dont, /* complain_on_overflow */
375 0, /* special_function */
376 "OP_STORE", /* name */
377 false, /* partial_inplace */
379 MINUS_ONE, /* dst_mask */
380 false), /* pcrel_offset */
382 /* Subtract the reloc address from the value on the top of the
384 HOWTO (ALPHA_R_OP_PSUB, /* type */
386 0, /* size (0 = byte, 1 = short, 2 = long) */
388 false, /* pc_relative */
390 complain_overflow_dont, /* complain_on_overflow */
391 0, /* special_function */
392 "OP_PSUB", /* name */
393 false, /* partial_inplace */
396 false), /* pcrel_offset */
398 /* Shift the value on the top of the relocation stack right by the
400 HOWTO (ALPHA_R_OP_PRSHIFT, /* type */
402 0, /* size (0 = byte, 1 = short, 2 = long) */
404 false, /* pc_relative */
406 complain_overflow_dont, /* complain_on_overflow */
407 0, /* special_function */
408 "OP_PRSHIFT", /* name */
409 false, /* partial_inplace */
412 false), /* pcrel_offset */
414 /* Adjust the GP value for a new range in the object file. */
415 HOWTO (ALPHA_R_GPVALUE, /* type */
417 0, /* size (0 = byte, 1 = short, 2 = long) */
419 false, /* pc_relative */
421 complain_overflow_dont, /* complain_on_overflow */
422 0, /* special_function */
423 "GPVALUE", /* name */
424 false, /* partial_inplace */
427 false) /* pcrel_offset */
430 /* Recognize an Alpha ECOFF file. */
432 static const bfd_target *
433 alpha_ecoff_object_p (abfd)
436 static const bfd_target *ret;
438 ret = coff_object_p (abfd);
444 /* Alpha ECOFF has a .pdata section. The lnnoptr field of the
445 .pdata section is the number of entries it contains. Each
446 entry takes up 8 bytes. The number of entries is required
447 since the section is aligned to a 16 byte boundary. When we
448 link .pdata sections together, we do not want to include the
449 alignment bytes. We handle this on input by faking the size
450 of the .pdata section to remove the unwanted alignment bytes.
451 On output we will set the lnnoptr field and force the
453 sec = bfd_get_section_by_name (abfd, _PDATA);
454 if (sec != (asection *) NULL)
458 size = sec->line_filepos * 8;
459 BFD_ASSERT (size == bfd_section_size (abfd, sec)
460 || size + 8 == bfd_section_size (abfd, sec));
461 if (! bfd_set_section_size (abfd, sec, size))
469 /* See whether the magic number matches. */
472 alpha_ecoff_bad_format_hook (abfd, filehdr)
476 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
478 if (ALPHA_ECOFF_BADMAG (*internal_f))
484 /* This is a hook called by coff_real_object_p to create any backend
485 specific information. */
488 alpha_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
495 ecoff = _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr);
499 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
501 /* Set additional BFD flags according to the object type from the
502 machine specific file header flags. */
503 switch (internal_f->f_flags & F_ALPHA_OBJECT_TYPE_MASK)
505 case F_ALPHA_SHARABLE:
506 abfd->flags |= DYNAMIC;
508 case F_ALPHA_CALL_SHARED:
509 /* Always executable if using shared libraries as the run time
510 loader might resolve undefined references. */
511 abfd->flags |= (DYNAMIC | EXEC_P);
518 /* Reloc handling. */
520 /* Swap a reloc in. */
523 alpha_ecoff_swap_reloc_in (abfd, ext_ptr, intern)
526 struct internal_reloc *intern;
528 const RELOC *ext = (RELOC *) ext_ptr;
530 intern->r_vaddr = bfd_h_get_64 (abfd, (bfd_byte *) ext->r_vaddr);
531 intern->r_symndx = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_symndx);
533 BFD_ASSERT (bfd_header_little_endian (abfd));
535 intern->r_type = ((ext->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
536 >> RELOC_BITS0_TYPE_SH_LITTLE);
537 intern->r_extern = (ext->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
538 intern->r_offset = ((ext->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
539 >> RELOC_BITS1_OFFSET_SH_LITTLE);
540 /* Ignored the reserved bits. */
541 intern->r_size = ((ext->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
542 >> RELOC_BITS3_SIZE_SH_LITTLE);
544 if (intern->r_type == ALPHA_R_LITUSE
545 || intern->r_type == ALPHA_R_GPDISP)
547 /* Handle the LITUSE and GPDISP relocs specially. Its symndx
548 value is not actually a symbol index, but is instead a
549 special code. We put the code in the r_size field, and
550 clobber the symndx. */
551 if (intern->r_size != 0)
553 intern->r_size = intern->r_symndx;
554 intern->r_symndx = RELOC_SECTION_NONE;
556 else if (intern->r_type == ALPHA_R_IGNORE)
558 /* The IGNORE reloc generally follows a GPDISP reloc, and is
559 against the .lita section. The section is irrelevant. */
560 if (! intern->r_extern &&
561 intern->r_symndx == RELOC_SECTION_ABS)
563 if (! intern->r_extern && intern->r_symndx == RELOC_SECTION_LITA)
564 intern->r_symndx = RELOC_SECTION_ABS;
568 /* Swap a reloc out. */
571 alpha_ecoff_swap_reloc_out (abfd, intern, dst)
573 const struct internal_reloc *intern;
576 RELOC *ext = (RELOC *) dst;
580 /* Undo the hackery done in swap_reloc_in. */
581 if (intern->r_type == ALPHA_R_LITUSE
582 || intern->r_type == ALPHA_R_GPDISP)
584 symndx = intern->r_size;
587 else if (intern->r_type == ALPHA_R_IGNORE
588 && ! intern->r_extern
589 && intern->r_symndx == RELOC_SECTION_ABS)
591 symndx = RELOC_SECTION_LITA;
592 size = intern->r_size;
596 symndx = intern->r_symndx;
597 size = intern->r_size;
600 BFD_ASSERT (intern->r_extern
601 || (intern->r_symndx >= 0 && intern->r_symndx <= 14));
603 bfd_h_put_64 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
604 bfd_h_put_32 (abfd, symndx, (bfd_byte *) ext->r_symndx);
606 BFD_ASSERT (bfd_header_little_endian (abfd));
608 ext->r_bits[0] = ((intern->r_type << RELOC_BITS0_TYPE_SH_LITTLE)
609 & RELOC_BITS0_TYPE_LITTLE);
610 ext->r_bits[1] = ((intern->r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0)
611 | ((intern->r_offset << RELOC_BITS1_OFFSET_SH_LITTLE)
612 & RELOC_BITS1_OFFSET_LITTLE));
614 ext->r_bits[3] = ((size << RELOC_BITS3_SIZE_SH_LITTLE)
615 & RELOC_BITS3_SIZE_LITTLE);
618 /* Finish canonicalizing a reloc. Part of this is generic to all
619 ECOFF targets, and that part is in ecoff.c. The rest is done in
620 this backend routine. It must fill in the howto field. */
623 alpha_adjust_reloc_in (abfd, intern, rptr)
625 const struct internal_reloc *intern;
628 if (intern->r_type > ALPHA_R_GPVALUE)
631 switch (intern->r_type)
637 /* The PC relative relocs do not seem to use the section VMA as
638 a negative addend. */
642 case ALPHA_R_GPREL32:
643 case ALPHA_R_LITERAL:
644 /* Copy the gp value for this object file into the addend, to
645 ensure that we are not confused by the linker. */
646 if (! intern->r_extern)
647 rptr->addend += ecoff_data (abfd)->gp;
652 /* The LITUSE and GPDISP relocs do not use a symbol, or an
653 addend, but they do use a special code. Put this code in the
655 rptr->addend = intern->r_size;
658 case ALPHA_R_OP_STORE:
659 /* The STORE reloc needs the size and offset fields. We store
660 them in the addend. */
661 BFD_ASSERT (intern->r_offset <= 256 && intern->r_size <= 256);
662 rptr->addend = (intern->r_offset << 8) + intern->r_size;
665 case ALPHA_R_OP_PUSH:
666 case ALPHA_R_OP_PSUB:
667 case ALPHA_R_OP_PRSHIFT:
668 /* The PUSH, PSUB and PRSHIFT relocs do not actually use an
669 address. I believe that the address supplied is really an
671 rptr->addend = intern->r_vaddr;
674 case ALPHA_R_GPVALUE:
675 /* Set the addend field to the new GP value. */
676 rptr->addend = intern->r_symndx + ecoff_data (abfd)->gp;
680 /* If the type is ALPHA_R_IGNORE, make sure this is a reference
681 to the absolute section so that the reloc is ignored. For
682 some reason the address of this reloc type is not adjusted by
683 the section vma. We record the gp value for this object file
684 here, for convenience when doing the GPDISP relocation. */
685 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
686 rptr->address = intern->r_vaddr;
687 rptr->addend = ecoff_data (abfd)->gp;
694 rptr->howto = &alpha_howto_table[intern->r_type];
697 /* When writing out a reloc we need to pull some values back out of
698 the addend field into the reloc. This is roughly the reverse of
699 alpha_adjust_reloc_in, except that there are several changes we do
703 alpha_adjust_reloc_out (abfd, rel, intern)
706 struct internal_reloc *intern;
708 switch (intern->r_type)
712 intern->r_size = rel->addend;
715 case ALPHA_R_OP_STORE:
716 intern->r_size = rel->addend & 0xff;
717 intern->r_offset = (rel->addend >> 8) & 0xff;
720 case ALPHA_R_OP_PUSH:
721 case ALPHA_R_OP_PSUB:
722 case ALPHA_R_OP_PRSHIFT:
723 intern->r_vaddr = rel->addend;
727 intern->r_vaddr = rel->address;
735 /* The size of the stack for the relocation evaluator. */
736 #define RELOC_STACKSIZE (10)
738 /* Alpha ECOFF relocs have a built in expression evaluator as well as
739 other interdependencies. Rather than use a bunch of special
740 functions and global variables, we use a single routine to do all
741 the relocation for a section. I haven't yet worked out how the
742 assembler is going to handle this. */
745 alpha_ecoff_get_relocated_section_contents (abfd, link_info, link_order,
746 data, relocateable, symbols)
748 struct bfd_link_info *link_info;
749 struct bfd_link_order *link_order;
751 boolean relocateable;
754 bfd *input_bfd = link_order->u.indirect.section->owner;
755 asection *input_section = link_order->u.indirect.section;
756 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
757 arelent **reloc_vector = NULL;
759 bfd *output_bfd = relocateable ? abfd : (bfd *) NULL;
761 boolean gp_undefined;
762 bfd_vma stack[RELOC_STACKSIZE];
767 reloc_vector = (arelent **) bfd_malloc (reloc_size);
768 if (reloc_vector == NULL && reloc_size != 0)
771 if (! bfd_get_section_contents (input_bfd, input_section, data,
772 (file_ptr) 0, input_section->_raw_size))
775 /* The section size is not going to change. */
776 input_section->_cooked_size = input_section->_raw_size;
777 input_section->reloc_done = true;
779 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
780 reloc_vector, symbols);
783 if (reloc_count == 0)
784 goto successful_return;
786 /* Get the GP value for the output BFD. */
787 gp_undefined = false;
788 if (_bfd_get_gp_value (abfd) == 0)
790 if (relocateable != false)
795 /* Make up a value. */
797 for (sec = abfd->sections; sec != NULL; sec = sec->next)
800 && (strcmp (sec->name, ".sbss") == 0
801 || strcmp (sec->name, ".sdata") == 0
802 || strcmp (sec->name, ".lit4") == 0
803 || strcmp (sec->name, ".lit8") == 0
804 || strcmp (sec->name, ".lita") == 0))
807 _bfd_set_gp_value (abfd, lo + 0x8000);
811 struct bfd_link_hash_entry *h;
813 h = bfd_link_hash_lookup (link_info->hash, "_gp", false, false,
815 if (h == (struct bfd_link_hash_entry *) NULL
816 || h->type != bfd_link_hash_defined)
819 _bfd_set_gp_value (abfd,
821 + h->u.def.section->output_section->vma
822 + h->u.def.section->output_offset));
825 gp = _bfd_get_gp_value (abfd);
827 for (; *reloc_vector != (arelent *) NULL; reloc_vector++)
830 bfd_reloc_status_type r;
835 switch (rel->howto->type)
838 rel->address += input_section->output_offset;
841 case ALPHA_R_REFLONG:
842 case ALPHA_R_REFQUAD:
849 && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
851 rel->address += input_section->output_offset;
854 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
858 case ALPHA_R_GPREL32:
859 /* This relocation is used in a switch table. It is a 32
860 bit offset from the current GP value. We must adjust it
861 by the different between the original GP value and the
862 current GP value. The original GP value is stored in the
863 addend. We adjust the addend and let
864 bfd_perform_relocation finish the job. */
866 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
868 if (r == bfd_reloc_ok && gp_undefined)
870 r = bfd_reloc_dangerous;
871 err = (char *) "GP relative relocation used when GP not defined";
875 case ALPHA_R_LITERAL:
876 /* This is a reference to a literal value, generally
877 (always?) in the .lita section. This is a 16 bit GP
878 relative relocation. Sometimes the subsequent reloc is a
879 LITUSE reloc, which indicates how this reloc is used.
880 This sometimes permits rewriting the two instructions
881 referred to by the LITERAL and the LITUSE into different
882 instructions which do not refer to .lita. This can save
883 a memory reference, and permits removing a value from
884 .lita thus saving GP relative space.
886 We do not these optimizations. To do them we would need
887 to arrange to link the .lita section first, so that by
888 the time we got here we would know the final values to
889 use. This would not be particularly difficult, but it is
890 not currently implemented. */
895 /* I believe that the LITERAL reloc will only apply to a
896 ldq or ldl instruction, so check my assumption. */
897 insn = bfd_get_32 (input_bfd, data + rel->address);
898 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
899 || ((insn >> 26) & 0x3f) == 0x28);
902 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
904 if (r == bfd_reloc_ok && gp_undefined)
906 r = bfd_reloc_dangerous;
908 (char *) "GP relative relocation used when GP not defined";
914 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
915 does not cause anything to happen, itself. */
916 rel->address += input_section->output_offset;
920 /* This marks the ldah of an ldah/lda pair which loads the
921 gp register with the difference of the gp value and the
922 current location. The second of the pair is r_size bytes
923 ahead; it used to be marked with an ALPHA_R_IGNORE reloc,
924 but that no longer happens in OSF/1 3.2. */
926 unsigned long insn1, insn2;
929 /* Get the two instructions. */
930 insn1 = bfd_get_32 (input_bfd, data + rel->address);
931 insn2 = bfd_get_32 (input_bfd, data + rel->address + rel->addend);
933 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
934 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
936 /* Get the existing addend. We must account for the sign
937 extension done by lda and ldah. */
938 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
941 addend -= 0x80000000;
942 addend -= 0x80000000;
947 /* The existing addend includes the different between the
948 gp of the input BFD and the address in the input BFD.
949 Subtract this out. */
950 addend -= (ecoff_data (input_bfd)->gp
951 - (input_section->vma + rel->address));
953 /* Now add in the final gp value, and subtract out the
956 - (input_section->output_section->vma
957 + input_section->output_offset
960 /* Change the instructions, accounting for the sign
961 extension, and write them out. */
964 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
965 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
967 bfd_put_32 (input_bfd, (bfd_vma) insn1, data + rel->address);
968 bfd_put_32 (input_bfd, (bfd_vma) insn2,
969 data + rel->address + rel->addend);
971 rel->address += input_section->output_offset;
975 case ALPHA_R_OP_PUSH:
976 /* Push a value on the reloc evaluation stack. */
983 rel->address += input_section->output_offset;
987 /* Figure out the relocation of this symbol. */
988 symbol = *rel->sym_ptr_ptr;
990 if (bfd_is_und_section (symbol->section))
991 r = bfd_reloc_undefined;
993 if (bfd_is_com_section (symbol->section))
996 relocation = symbol->value;
997 relocation += symbol->section->output_section->vma;
998 relocation += symbol->section->output_offset;
999 relocation += rel->addend;
1001 if (tos >= RELOC_STACKSIZE)
1004 stack[tos++] = relocation;
1008 case ALPHA_R_OP_STORE:
1009 /* Store a value from the reloc stack into a bitfield. */
1016 rel->address += input_section->output_offset;
1023 /* The offset and size for this reloc are encoded into the
1024 addend field by alpha_adjust_reloc_in. */
1025 offset = (rel->addend >> 8) & 0xff;
1026 size = rel->addend & 0xff;
1028 val = bfd_get_64 (abfd, data + rel->address);
1029 val &=~ (((1 << size) - 1) << offset);
1030 val |= (stack[--tos] & ((1 << size) - 1)) << offset;
1031 bfd_put_64 (abfd, val, data + rel->address);
1035 case ALPHA_R_OP_PSUB:
1036 /* Subtract a value from the top of the stack. */
1043 rel->address += input_section->output_offset;
1047 /* Figure out the relocation of this symbol. */
1048 symbol = *rel->sym_ptr_ptr;
1050 if (bfd_is_und_section (symbol->section))
1051 r = bfd_reloc_undefined;
1053 if (bfd_is_com_section (symbol->section))
1056 relocation = symbol->value;
1057 relocation += symbol->section->output_section->vma;
1058 relocation += symbol->section->output_offset;
1059 relocation += rel->addend;
1064 stack[tos - 1] -= relocation;
1068 case ALPHA_R_OP_PRSHIFT:
1069 /* Shift the value on the top of the stack. */
1076 rel->address += input_section->output_offset;
1080 /* Figure out the relocation of this symbol. */
1081 symbol = *rel->sym_ptr_ptr;
1083 if (bfd_is_und_section (symbol->section))
1084 r = bfd_reloc_undefined;
1086 if (bfd_is_com_section (symbol->section))
1089 relocation = symbol->value;
1090 relocation += symbol->section->output_section->vma;
1091 relocation += symbol->section->output_offset;
1092 relocation += rel->addend;
1097 stack[tos - 1] >>= relocation;
1101 case ALPHA_R_GPVALUE:
1102 /* I really don't know if this does the right thing. */
1104 gp_undefined = false;
1113 asection *os = input_section->output_section;
1115 /* A partial link, so keep the relocs. */
1116 os->orelocation[os->reloc_count] = rel;
1120 if (r != bfd_reloc_ok)
1124 case bfd_reloc_undefined:
1125 if (! ((*link_info->callbacks->undefined_symbol)
1126 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1127 input_bfd, input_section, rel->address)))
1130 case bfd_reloc_dangerous:
1131 if (! ((*link_info->callbacks->reloc_dangerous)
1132 (link_info, err, input_bfd, input_section,
1136 case bfd_reloc_overflow:
1137 if (! ((*link_info->callbacks->reloc_overflow)
1138 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1139 rel->howto->name, rel->addend, input_bfd,
1140 input_section, rel->address)))
1143 case bfd_reloc_outofrange:
1155 if (reloc_vector != NULL)
1156 free (reloc_vector);
1160 if (reloc_vector != NULL)
1161 free (reloc_vector);
1165 /* Get the howto structure for a generic reloc type. */
1167 static reloc_howto_type *
1168 alpha_bfd_reloc_type_lookup (abfd, code)
1170 bfd_reloc_code_real_type code;
1177 alpha_type = ALPHA_R_REFLONG;
1180 case BFD_RELOC_CTOR:
1181 alpha_type = ALPHA_R_REFQUAD;
1183 case BFD_RELOC_GPREL32:
1184 alpha_type = ALPHA_R_GPREL32;
1186 case BFD_RELOC_ALPHA_LITERAL:
1187 alpha_type = ALPHA_R_LITERAL;
1189 case BFD_RELOC_ALPHA_LITUSE:
1190 alpha_type = ALPHA_R_LITUSE;
1192 case BFD_RELOC_ALPHA_GPDISP_HI16:
1193 alpha_type = ALPHA_R_GPDISP;
1195 case BFD_RELOC_ALPHA_GPDISP_LO16:
1196 alpha_type = ALPHA_R_IGNORE;
1198 case BFD_RELOC_23_PCREL_S2:
1199 alpha_type = ALPHA_R_BRADDR;
1201 case BFD_RELOC_ALPHA_HINT:
1202 alpha_type = ALPHA_R_HINT;
1204 case BFD_RELOC_16_PCREL:
1205 alpha_type = ALPHA_R_SREL16;
1207 case BFD_RELOC_32_PCREL:
1208 alpha_type = ALPHA_R_SREL32;
1210 case BFD_RELOC_64_PCREL:
1211 alpha_type = ALPHA_R_SREL64;
1215 alpha_type = ALPHA_R_OP_PUSH;
1218 alpha_type = ALPHA_R_OP_STORE;
1221 alpha_type = ALPHA_R_OP_PSUB;
1224 alpha_type = ALPHA_R_OP_PRSHIFT;
1227 alpha_type = ALPHA_R_GPVALUE;
1231 return (reloc_howto_type *) NULL;
1234 return &alpha_howto_table[alpha_type];
1237 /* A helper routine for alpha_relocate_section which converts an
1238 external reloc when generating relocateable output. Returns the
1239 relocation amount. */
1242 alpha_convert_external_reloc (output_bfd, info, input_bfd, ext_rel, h)
1244 struct bfd_link_info *info;
1246 struct external_reloc *ext_rel;
1247 struct ecoff_link_hash_entry *h;
1249 unsigned long r_symndx;
1252 BFD_ASSERT (info->relocateable);
1254 if (h->root.type == bfd_link_hash_defined
1255 || h->root.type == bfd_link_hash_defweak)
1260 /* This symbol is defined in the output. Convert the reloc from
1261 being against the symbol to being against the section. */
1263 /* Clear the r_extern bit. */
1264 ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE;
1266 /* Compute a new r_symndx value. */
1267 hsec = h->root.u.def.section;
1268 name = bfd_get_section_name (output_bfd, hsec->output_section);
1274 if (strcmp (name, "*ABS*") == 0)
1275 r_symndx = RELOC_SECTION_ABS;
1278 if (strcmp (name, ".bss") == 0)
1279 r_symndx = RELOC_SECTION_BSS;
1282 if (strcmp (name, ".data") == 0)
1283 r_symndx = RELOC_SECTION_DATA;
1286 if (strcmp (name, ".fini") == 0)
1287 r_symndx = RELOC_SECTION_FINI;
1290 if (strcmp (name, ".init") == 0)
1291 r_symndx = RELOC_SECTION_INIT;
1294 if (strcmp (name, ".lita") == 0)
1295 r_symndx = RELOC_SECTION_LITA;
1296 else if (strcmp (name, ".lit8") == 0)
1297 r_symndx = RELOC_SECTION_LIT8;
1298 else if (strcmp (name, ".lit4") == 0)
1299 r_symndx = RELOC_SECTION_LIT4;
1302 if (strcmp (name, ".pdata") == 0)
1303 r_symndx = RELOC_SECTION_PDATA;
1306 if (strcmp (name, ".rdata") == 0)
1307 r_symndx = RELOC_SECTION_RDATA;
1308 else if (strcmp (name, ".rconst") == 0)
1309 r_symndx = RELOC_SECTION_RCONST;
1312 if (strcmp (name, ".sdata") == 0)
1313 r_symndx = RELOC_SECTION_SDATA;
1314 else if (strcmp (name, ".sbss") == 0)
1315 r_symndx = RELOC_SECTION_SBSS;
1318 if (strcmp (name, ".text") == 0)
1319 r_symndx = RELOC_SECTION_TEXT;
1322 if (strcmp (name, ".xdata") == 0)
1323 r_symndx = RELOC_SECTION_XDATA;
1330 /* Add the section VMA and the symbol value. */
1331 relocation = (h->root.u.def.value
1332 + hsec->output_section->vma
1333 + hsec->output_offset);
1337 /* Change the symndx value to the right one for
1342 /* Caller must give an error. */
1348 /* Write out the new r_symndx value. */
1349 bfd_h_put_32 (input_bfd, (bfd_vma) r_symndx,
1350 (bfd_byte *) ext_rel->r_symndx);
1355 /* Relocate a section while linking an Alpha ECOFF file. This is
1356 quite similar to get_relocated_section_contents. Perhaps they
1357 could be combined somehow. */
1360 alpha_relocate_section (output_bfd, info, input_bfd, input_section,
1361 contents, external_relocs)
1363 struct bfd_link_info *info;
1365 asection *input_section;
1367 PTR external_relocs;
1369 asection **symndx_to_section, *lita_sec;
1370 struct ecoff_link_hash_entry **sym_hashes;
1372 boolean gp_undefined;
1373 bfd_vma stack[RELOC_STACKSIZE];
1375 struct external_reloc *ext_rel;
1376 struct external_reloc *ext_rel_end;
1378 /* We keep a table mapping the symndx found in an internal reloc to
1379 the appropriate section. This is faster than looking up the
1380 section by name each time. */
1381 symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
1382 if (symndx_to_section == (asection **) NULL)
1384 symndx_to_section = ((asection **)
1385 bfd_alloc (input_bfd,
1387 * sizeof (asection *))));
1388 if (!symndx_to_section)
1391 symndx_to_section[RELOC_SECTION_NONE] = NULL;
1392 symndx_to_section[RELOC_SECTION_TEXT] =
1393 bfd_get_section_by_name (input_bfd, ".text");
1394 symndx_to_section[RELOC_SECTION_RDATA] =
1395 bfd_get_section_by_name (input_bfd, ".rdata");
1396 symndx_to_section[RELOC_SECTION_DATA] =
1397 bfd_get_section_by_name (input_bfd, ".data");
1398 symndx_to_section[RELOC_SECTION_SDATA] =
1399 bfd_get_section_by_name (input_bfd, ".sdata");
1400 symndx_to_section[RELOC_SECTION_SBSS] =
1401 bfd_get_section_by_name (input_bfd, ".sbss");
1402 symndx_to_section[RELOC_SECTION_BSS] =
1403 bfd_get_section_by_name (input_bfd, ".bss");
1404 symndx_to_section[RELOC_SECTION_INIT] =
1405 bfd_get_section_by_name (input_bfd, ".init");
1406 symndx_to_section[RELOC_SECTION_LIT8] =
1407 bfd_get_section_by_name (input_bfd, ".lit8");
1408 symndx_to_section[RELOC_SECTION_LIT4] =
1409 bfd_get_section_by_name (input_bfd, ".lit4");
1410 symndx_to_section[RELOC_SECTION_XDATA] =
1411 bfd_get_section_by_name (input_bfd, ".xdata");
1412 symndx_to_section[RELOC_SECTION_PDATA] =
1413 bfd_get_section_by_name (input_bfd, ".pdata");
1414 symndx_to_section[RELOC_SECTION_FINI] =
1415 bfd_get_section_by_name (input_bfd, ".fini");
1416 symndx_to_section[RELOC_SECTION_LITA] =
1417 bfd_get_section_by_name (input_bfd, ".lita");
1418 symndx_to_section[RELOC_SECTION_ABS] = bfd_abs_section_ptr;
1419 symndx_to_section[RELOC_SECTION_RCONST] =
1420 bfd_get_section_by_name (input_bfd, ".rconst");
1422 ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
1425 sym_hashes = ecoff_data (input_bfd)->sym_hashes;
1427 /* On the Alpha, the .lita section must be addressable by the global
1428 pointer. To support large programs, we need to allow multiple
1429 global pointers. This works as long as each input .lita section
1430 is <64KB big. This implies that when producing relocatable
1431 output, the .lita section is limited to 64KB. . */
1433 lita_sec = symndx_to_section[RELOC_SECTION_LITA];
1434 gp = _bfd_get_gp_value (output_bfd);
1435 if (! info->relocateable && lita_sec != NULL)
1437 struct ecoff_section_tdata *lita_sec_data;
1439 /* Make sure we have a section data structure to which we can
1440 hang on to the gp value we pick for the section. */
1441 lita_sec_data = ecoff_section_data (input_bfd, lita_sec);
1442 if (lita_sec_data == NULL)
1444 lita_sec_data = ((struct ecoff_section_tdata *)
1445 bfd_zalloc (input_bfd,
1446 sizeof (struct ecoff_section_tdata)));
1447 ecoff_section_data (input_bfd, lita_sec) = lita_sec_data;
1450 if (lita_sec_data->gp != 0)
1452 /* If we already assigned a gp to this section, we better
1453 stick with that value. */
1454 gp = lita_sec_data->gp;
1459 bfd_size_type lita_size;
1461 lita_vma = lita_sec->output_offset + lita_sec->output_section->vma;
1462 lita_size = lita_sec->_cooked_size;
1464 lita_size = lita_sec->_raw_size;
1467 || lita_vma < gp - 0x8000
1468 || lita_vma + lita_size >= gp + 0x8000)
1470 /* Either gp hasn't been set at all or the current gp
1471 cannot address this .lita section. In both cases we
1472 reset the gp to point into the "middle" of the
1473 current input .lita section. For now, we issue a
1474 warning when redefining the gp value (probably should
1475 be made optional). */
1476 if (gp && !ecoff_data (output_bfd)->issued_multiple_gp_warning)
1478 (*_bfd_error_handler)
1479 ("%s: warning: using multiple gp values",
1480 bfd_get_filename (output_bfd));
1481 ecoff_data (output_bfd)->issued_multiple_gp_warning = true;
1483 if (lita_vma < gp - 0x8000)
1484 gp = lita_vma + lita_size - 0x8000;
1486 gp = lita_vma + 0x8000;
1490 lita_sec_data->gp = gp;
1493 _bfd_set_gp_value (output_bfd, gp);
1496 gp_undefined = (gp == 0);
1498 BFD_ASSERT (bfd_header_little_endian (output_bfd));
1499 BFD_ASSERT (bfd_header_little_endian (input_bfd));
1501 ext_rel = (struct external_reloc *) external_relocs;
1502 ext_rel_end = ext_rel + input_section->reloc_count;
1503 for (; ext_rel < ext_rel_end; ext_rel++)
1506 unsigned long r_symndx;
1512 boolean adjust_addrp;
1516 r_vaddr = bfd_h_get_64 (input_bfd, (bfd_byte *) ext_rel->r_vaddr);
1517 r_symndx = bfd_h_get_32 (input_bfd, (bfd_byte *) ext_rel->r_symndx);
1519 r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
1520 >> RELOC_BITS0_TYPE_SH_LITTLE);
1521 r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
1522 r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
1523 >> RELOC_BITS1_OFFSET_SH_LITTLE);
1524 /* Ignored the reserved bits. */
1525 r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
1526 >> RELOC_BITS3_SIZE_SH_LITTLE);
1529 adjust_addrp = true;
1538 case ALPHA_R_IGNORE:
1539 /* This reloc appears after a GPDISP reloc. On earlier
1540 versions of OSF/1, It marked the position of the second
1541 instruction to be altered by the GPDISP reloc, but it is
1542 not otherwise used for anything. For some reason, the
1543 address of the relocation does not appear to include the
1544 section VMA, unlike the other relocation types. */
1545 if (info->relocateable)
1546 bfd_h_put_64 (input_bfd,
1547 input_section->output_offset + r_vaddr,
1548 (bfd_byte *) ext_rel->r_vaddr);
1549 adjust_addrp = false;
1552 case ALPHA_R_REFLONG:
1553 case ALPHA_R_REFQUAD:
1554 case ALPHA_R_BRADDR:
1556 case ALPHA_R_SREL16:
1557 case ALPHA_R_SREL32:
1558 case ALPHA_R_SREL64:
1562 case ALPHA_R_GPREL32:
1563 /* This relocation is used in a switch table. It is a 32
1564 bit offset from the current GP value. We must adjust it
1565 by the different between the original GP value and the
1566 current GP value. */
1568 addend = ecoff_data (input_bfd)->gp - gp;
1572 case ALPHA_R_LITERAL:
1573 /* This is a reference to a literal value, generally
1574 (always?) in the .lita section. This is a 16 bit GP
1575 relative relocation. Sometimes the subsequent reloc is a
1576 LITUSE reloc, which indicates how this reloc is used.
1577 This sometimes permits rewriting the two instructions
1578 referred to by the LITERAL and the LITUSE into different
1579 instructions which do not refer to .lita. This can save
1580 a memory reference, and permits removing a value from
1581 .lita thus saving GP relative space.
1583 We do not these optimizations. To do them we would need
1584 to arrange to link the .lita section first, so that by
1585 the time we got here we would know the final values to
1586 use. This would not be particularly difficult, but it is
1587 not currently implemented. */
1589 /* I believe that the LITERAL reloc will only apply to a ldq
1590 or ldl instruction, so check my assumption. */
1594 insn = bfd_get_32 (input_bfd,
1595 contents + r_vaddr - input_section->vma);
1596 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
1597 || ((insn >> 26) & 0x3f) == 0x28);
1601 addend = ecoff_data (input_bfd)->gp - gp;
1605 case ALPHA_R_LITUSE:
1606 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
1607 does not cause anything to happen, itself. */
1610 case ALPHA_R_GPDISP:
1611 /* This marks the ldah of an ldah/lda pair which loads the
1612 gp register with the difference of the gp value and the
1613 current location. The second of the pair is r_symndx
1614 bytes ahead. It used to be marked with an ALPHA_R_IGNORE
1615 reloc, but OSF/1 3.2 no longer does that. */
1617 unsigned long insn1, insn2;
1619 /* Get the two instructions. */
1620 insn1 = bfd_get_32 (input_bfd,
1621 contents + r_vaddr - input_section->vma);
1622 insn2 = bfd_get_32 (input_bfd,
1625 - input_section->vma
1628 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
1629 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
1631 /* Get the existing addend. We must account for the sign
1632 extension done by lda and ldah. */
1633 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
1636 /* This is addend -= 0x100000000 without causing an
1637 integer overflow on a 32 bit host. */
1638 addend -= 0x80000000;
1639 addend -= 0x80000000;
1644 /* The existing addend includes the difference between the
1645 gp of the input BFD and the address in the input BFD.
1646 We want to change this to the difference between the
1647 final GP and the final address. */
1649 - ecoff_data (input_bfd)->gp
1650 + input_section->vma
1651 - (input_section->output_section->vma
1652 + input_section->output_offset));
1654 /* Change the instructions, accounting for the sign
1655 extension, and write them out. */
1656 if (addend & 0x8000)
1658 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
1659 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
1661 bfd_put_32 (input_bfd, (bfd_vma) insn1,
1662 contents + r_vaddr - input_section->vma);
1663 bfd_put_32 (input_bfd, (bfd_vma) insn2,
1664 contents + r_vaddr - input_section->vma + r_symndx);
1670 case ALPHA_R_OP_PUSH:
1671 case ALPHA_R_OP_PSUB:
1672 case ALPHA_R_OP_PRSHIFT:
1673 /* Manipulate values on the reloc evaluation stack. The
1674 r_vaddr field is not an address in input_section, it is
1675 the current value (including any addend) of the object
1681 s = symndx_to_section[r_symndx];
1682 if (s == (asection *) NULL)
1684 addend = s->output_section->vma + s->output_offset - s->vma;
1688 struct ecoff_link_hash_entry *h;
1690 h = sym_hashes[r_symndx];
1691 if (h == (struct ecoff_link_hash_entry *) NULL)
1694 if (! info->relocateable)
1696 if (h->root.type == bfd_link_hash_defined
1697 || h->root.type == bfd_link_hash_defweak)
1698 addend = (h->root.u.def.value
1699 + h->root.u.def.section->output_section->vma
1700 + h->root.u.def.section->output_offset);
1703 /* Note that we pass the address as 0, since we
1704 do not have a meaningful number for the
1705 location within the section that is being
1707 if (! ((*info->callbacks->undefined_symbol)
1708 (info, h->root.root.string, input_bfd,
1709 input_section, (bfd_vma) 0)))
1716 if (h->root.type != bfd_link_hash_defined
1717 && h->root.type != bfd_link_hash_defweak
1720 /* This symbol is not being written out. Pass
1721 the address as 0, as with undefined_symbol,
1723 if (! ((*info->callbacks->unattached_reloc)
1724 (info, h->root.root.string, input_bfd,
1725 input_section, (bfd_vma) 0)))
1729 addend = alpha_convert_external_reloc (output_bfd, info,
1737 if (info->relocateable)
1739 /* Adjust r_vaddr by the addend. */
1740 bfd_h_put_64 (input_bfd, addend,
1741 (bfd_byte *) ext_rel->r_vaddr);
1747 case ALPHA_R_OP_PUSH:
1748 if (tos >= RELOC_STACKSIZE)
1750 stack[tos++] = addend;
1753 case ALPHA_R_OP_PSUB:
1756 stack[tos - 1] -= addend;
1759 case ALPHA_R_OP_PRSHIFT:
1762 stack[tos - 1] >>= addend;
1767 adjust_addrp = false;
1770 case ALPHA_R_OP_STORE:
1771 /* Store a value from the reloc stack into a bitfield. If
1772 we are generating relocateable output, all we do is
1773 adjust the address of the reloc. */
1774 if (! info->relocateable)
1782 /* Get the relocation mask. The separate steps and the
1783 casts to bfd_vma are attempts to avoid a bug in the
1784 Alpha OSF 1.3 C compiler. See reloc.c for more
1787 mask <<= (bfd_vma) r_size;
1790 /* FIXME: I don't know what kind of overflow checking,
1791 if any, should be done here. */
1792 val = bfd_get_64 (input_bfd,
1793 contents + r_vaddr - input_section->vma);
1794 val &=~ mask << (bfd_vma) r_offset;
1795 val |= (stack[--tos] & mask) << (bfd_vma) r_offset;
1796 bfd_put_64 (input_bfd, val,
1797 contents + r_vaddr - input_section->vma);
1801 case ALPHA_R_GPVALUE:
1802 /* I really don't know if this does the right thing. */
1803 gp = ecoff_data (input_bfd)->gp + r_symndx;
1804 gp_undefined = false;
1810 reloc_howto_type *howto;
1811 struct ecoff_link_hash_entry *h = NULL;
1814 bfd_reloc_status_type r;
1816 /* Perform a relocation. */
1818 howto = &alpha_howto_table[r_type];
1822 h = sym_hashes[r_symndx];
1823 /* If h is NULL, that means that there is a reloc
1824 against an external symbol which we thought was just
1825 a debugging symbol. This should not happen. */
1826 if (h == (struct ecoff_link_hash_entry *) NULL)
1831 if (r_symndx >= NUM_RELOC_SECTIONS)
1834 s = symndx_to_section[r_symndx];
1836 if (s == (asection *) NULL)
1840 if (info->relocateable)
1842 /* We are generating relocateable output, and must
1843 convert the existing reloc. */
1846 if (h->root.type != bfd_link_hash_defined
1847 && h->root.type != bfd_link_hash_defweak
1850 /* This symbol is not being written out. */
1851 if (! ((*info->callbacks->unattached_reloc)
1852 (info, h->root.root.string, input_bfd,
1853 input_section, r_vaddr - input_section->vma)))
1857 relocation = alpha_convert_external_reloc (output_bfd,
1865 /* This is a relocation against a section. Adjust
1866 the value by the amount the section moved. */
1867 relocation = (s->output_section->vma
1872 /* If this is PC relative, the existing object file
1873 appears to already have the reloc worked out. We
1874 must subtract out the old value and add in the new
1876 if (howto->pc_relative)
1877 relocation -= (input_section->output_section->vma
1878 + input_section->output_offset
1879 - input_section->vma);
1881 /* Put in any addend. */
1882 relocation += addend;
1884 /* Adjust the contents. */
1885 r = _bfd_relocate_contents (howto, input_bfd, relocation,
1888 - input_section->vma));
1892 /* We are producing a final executable. */
1895 /* This is a reloc against a symbol. */
1896 if (h->root.type == bfd_link_hash_defined
1897 || h->root.type == bfd_link_hash_defweak)
1901 hsec = h->root.u.def.section;
1902 relocation = (h->root.u.def.value
1903 + hsec->output_section->vma
1904 + hsec->output_offset);
1908 if (! ((*info->callbacks->undefined_symbol)
1909 (info, h->root.root.string, input_bfd,
1911 r_vaddr - input_section->vma)))
1918 /* This is a reloc against a section. */
1919 relocation = (s->output_section->vma
1923 /* Adjust a PC relative relocation by removing the
1924 reference to the original source section. */
1925 if (howto->pc_relative)
1926 relocation += input_section->vma;
1929 r = _bfd_final_link_relocate (howto,
1933 r_vaddr - input_section->vma,
1938 if (r != bfd_reloc_ok)
1943 case bfd_reloc_outofrange:
1945 case bfd_reloc_overflow:
1950 name = sym_hashes[r_symndx]->root.root.string;
1952 name = bfd_section_name (input_bfd,
1953 symndx_to_section[r_symndx]);
1954 if (! ((*info->callbacks->reloc_overflow)
1955 (info, name, alpha_howto_table[r_type].name,
1956 (bfd_vma) 0, input_bfd, input_section,
1957 r_vaddr - input_section->vma)))
1965 if (info->relocateable && adjust_addrp)
1967 /* Change the address of the relocation. */
1968 bfd_h_put_64 (input_bfd,
1969 (input_section->output_section->vma
1970 + input_section->output_offset
1971 - input_section->vma
1973 (bfd_byte *) ext_rel->r_vaddr);
1976 if (gp_usedp && gp_undefined)
1978 if (! ((*info->callbacks->reloc_dangerous)
1979 (info, "GP relative relocation when GP not defined",
1980 input_bfd, input_section, r_vaddr - input_section->vma)))
1982 /* Only give the error once per link. */
1984 _bfd_set_gp_value (output_bfd, gp);
1985 gp_undefined = false;
1995 /* Do final adjustments to the filehdr and the aouthdr. This routine
1996 sets the dynamic bits in the file header. */
2000 alpha_adjust_headers (abfd, fhdr, ahdr)
2002 struct internal_filehdr *fhdr;
2003 struct internal_aouthdr *ahdr;
2005 if ((abfd->flags & (DYNAMIC | EXEC_P)) == (DYNAMIC | EXEC_P))
2006 fhdr->f_flags |= F_ALPHA_CALL_SHARED;
2007 else if ((abfd->flags & DYNAMIC) != 0)
2008 fhdr->f_flags |= F_ALPHA_SHARABLE;
2012 /* Archive handling. In OSF/1 (or Digital Unix) v3.2, Digital
2013 introduced archive packing, in which the elements in an archive are
2014 optionally compressed using a simple dictionary scheme. We know
2015 how to read such archives, but we don't write them. */
2017 #define alpha_ecoff_slurp_armap _bfd_ecoff_slurp_armap
2018 #define alpha_ecoff_slurp_extended_name_table \
2019 _bfd_ecoff_slurp_extended_name_table
2020 #define alpha_ecoff_construct_extended_name_table \
2021 _bfd_ecoff_construct_extended_name_table
2022 #define alpha_ecoff_truncate_arname _bfd_ecoff_truncate_arname
2023 #define alpha_ecoff_write_armap _bfd_ecoff_write_armap
2024 #define alpha_ecoff_generic_stat_arch_elt _bfd_ecoff_generic_stat_arch_elt
2025 #define alpha_ecoff_update_armap_timestamp _bfd_ecoff_update_armap_timestamp
2027 /* A compressed file uses this instead of ARFMAG. */
2029 #define ARFZMAG "Z\012"
2031 /* Read an archive header. This is like the standard routine, but it
2032 also accepts ARFZMAG. */
2035 alpha_ecoff_read_ar_hdr (abfd)
2038 struct areltdata *ret;
2041 ret = (struct areltdata *) _bfd_generic_read_ar_hdr_mag (abfd, ARFZMAG);
2045 h = (struct ar_hdr *) ret->arch_header;
2046 if (strncmp (h->ar_fmag, ARFZMAG, 2) == 0)
2050 /* This is a compressed file. We must set the size correctly.
2051 The size is the eight bytes after the dummy file header. */
2052 if (bfd_seek (abfd, FILHSZ, SEEK_CUR) != 0
2053 || bfd_read (ab, 1, 8, abfd) != 8
2054 || bfd_seek (abfd, - (FILHSZ + 8), SEEK_CUR) != 0)
2057 ret->parsed_size = bfd_h_get_64 (abfd, ab);
2063 /* Get an archive element at a specified file position. This is where
2064 we uncompress the archive element if necessary. */
2067 alpha_ecoff_get_elt_at_filepos (archive, filepos)
2072 struct areltdata *tdata;
2077 struct bfd_in_memory *bim;
2079 nbfd = _bfd_get_elt_at_filepos (archive, filepos);
2083 if ((nbfd->flags & BFD_IN_MEMORY) != 0)
2085 /* We have already expanded this BFD. */
2089 tdata = (struct areltdata *) nbfd->arelt_data;
2090 hdr = (struct ar_hdr *) tdata->arch_header;
2091 if (strncmp (hdr->ar_fmag, ARFZMAG, 2) != 0)
2094 /* We must uncompress this element. We do this by copying it into a
2095 memory buffer, and making bfd_read and bfd_seek use that buffer.
2096 This can use a lot of memory, but it's simpler than getting a
2097 temporary file, making that work with the file descriptor caching
2098 code, and making sure that it is deleted at all appropriate
2099 times. It can be changed if it ever becomes important. */
2101 /* The compressed file starts with a dummy ECOFF file header. */
2102 if (bfd_seek (nbfd, FILHSZ, SEEK_SET) != 0)
2105 /* The next eight bytes are the real file size. */
2106 if (bfd_read (ab, 1, 8, nbfd) != 8)
2108 size = bfd_h_get_64 (nbfd, ab);
2115 bfd_byte dict[4096];
2119 buf = (bfd_byte *) bfd_alloc (nbfd, size);
2126 /* I don't know what the next eight bytes are for. */
2127 if (bfd_read (ab, 1, 8, nbfd) != 8)
2130 /* This is the uncompression algorithm. It's a simple
2131 dictionary based scheme in which each character is predicted
2132 by a hash of the previous three characters. A control byte
2133 indicates whether the character is predicted or whether it
2134 appears in the input stream; each control byte manages the
2135 next eight bytes in the output stream. */
2136 memset (dict, 0, sizeof dict);
2138 while (bfd_read (&b, 1, 1, nbfd) == 1)
2142 for (i = 0; i < 8; i++, b >>= 1)
2150 if (! bfd_read (&n, 1, 1, nbfd))
2163 h &= sizeof dict - 1;
2171 /* Now the uncompressed file contents are in buf. */
2172 bim = ((struct bfd_in_memory *)
2173 bfd_alloc (nbfd, sizeof (struct bfd_in_memory)));
2179 nbfd->mtime_set = true;
2180 nbfd->mtime = strtol (hdr->ar_date, (char **) NULL, 10);
2182 nbfd->flags |= BFD_IN_MEMORY;
2183 nbfd->iostream = (PTR) bim;
2184 BFD_ASSERT (! nbfd->cacheable);
2194 /* Open the next archived file. */
2197 alpha_ecoff_openr_next_archived_file (archive, last_file)
2203 if (last_file == NULL)
2204 filestart = bfd_ardata (archive)->first_file_filepos;
2207 struct areltdata *t;
2211 /* We can't use arelt_size here, because that uses parsed_size,
2212 which is the uncompressed size. We need the compressed size. */
2213 t = (struct areltdata *) last_file->arelt_data;
2214 h = (struct ar_hdr *) t->arch_header;
2215 size = strtol (h->ar_size, (char **) NULL, 10);
2217 /* Pad to an even boundary...
2218 Note that last_file->origin can be odd in the case of
2219 BSD-4.4-style element with a long odd size. */
2220 filestart = last_file->origin + size;
2221 filestart += filestart % 2;
2224 return alpha_ecoff_get_elt_at_filepos (archive, filestart);
2227 /* Open the archive file given an index into the armap. */
2230 alpha_ecoff_get_elt_at_index (abfd, index)
2236 entry = bfd_ardata (abfd)->symdefs + index;
2237 return alpha_ecoff_get_elt_at_filepos (abfd, entry->file_offset);
2240 /* This is the ECOFF backend structure. The backend field of the
2241 target vector points to this. */
2243 static const struct ecoff_backend_data alpha_ecoff_backend_data =
2245 /* COFF backend structure. */
2247 (void (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR))) bfd_void, /* aux_in */
2248 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
2249 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
2250 (unsigned (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR)))bfd_void,/*aux_out*/
2251 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
2252 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
2253 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */
2254 alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out,
2255 alpha_ecoff_swap_scnhdr_out,
2256 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, true,
2257 alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in,
2258 alpha_ecoff_swap_scnhdr_in, NULL,
2259 alpha_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
2260 alpha_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
2261 _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
2262 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
2264 /* Supported architecture. */
2266 /* Initial portion of armap string. */
2268 /* The page boundary used to align sections in a demand-paged
2269 executable file. E.g., 0x1000. */
2271 /* True if the .rdata section is part of the text segment, as on the
2272 Alpha. False if .rdata is part of the data segment, as on the
2275 /* Bitsize of constructor entries. */
2277 /* Reloc to use for constructor entries. */
2278 &alpha_howto_table[ALPHA_R_REFQUAD],
2280 /* Symbol table magic number. */
2282 /* Alignment of debugging information. E.g., 4. */
2284 /* Sizes of external symbolic information. */
2285 sizeof (struct hdr_ext),
2286 sizeof (struct dnr_ext),
2287 sizeof (struct pdr_ext),
2288 sizeof (struct sym_ext),
2289 sizeof (struct opt_ext),
2290 sizeof (struct fdr_ext),
2291 sizeof (struct rfd_ext),
2292 sizeof (struct ext_ext),
2293 /* Functions to swap in external symbolic data. */
2302 _bfd_ecoff_swap_tir_in,
2303 _bfd_ecoff_swap_rndx_in,
2304 /* Functions to swap out external symbolic data. */
2313 _bfd_ecoff_swap_tir_out,
2314 _bfd_ecoff_swap_rndx_out,
2315 /* Function to read in symbolic data. */
2316 _bfd_ecoff_slurp_symbolic_info
2318 /* External reloc size. */
2320 /* Reloc swapping functions. */
2321 alpha_ecoff_swap_reloc_in,
2322 alpha_ecoff_swap_reloc_out,
2323 /* Backend reloc tweaking. */
2324 alpha_adjust_reloc_in,
2325 alpha_adjust_reloc_out,
2326 /* Relocate section contents while linking. */
2327 alpha_relocate_section,
2328 /* Do final adjustments to filehdr and aouthdr. */
2329 alpha_adjust_headers,
2330 /* Read an element from an archive at a given file position. */
2331 alpha_ecoff_get_elt_at_filepos
2334 /* Looking up a reloc type is Alpha specific. */
2335 #define _bfd_ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup
2337 /* So is getting relocated section contents. */
2338 #define _bfd_ecoff_bfd_get_relocated_section_contents \
2339 alpha_ecoff_get_relocated_section_contents
2341 /* Handling file windows is generic. */
2342 #define _bfd_ecoff_get_section_contents_in_window \
2343 _bfd_generic_get_section_contents_in_window
2345 /* Relaxing sections is generic. */
2346 #define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section
2348 const bfd_target ecoffalpha_little_vec =
2350 "ecoff-littlealpha", /* name */
2351 bfd_target_ecoff_flavour,
2352 BFD_ENDIAN_LITTLE, /* data byte order is little */
2353 BFD_ENDIAN_LITTLE, /* header byte order is little */
2355 (HAS_RELOC | EXEC_P | /* object flags */
2356 HAS_LINENO | HAS_DEBUG |
2357 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
2359 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
2360 0, /* leading underscore */
2361 ' ', /* ar_pad_char */
2362 15, /* ar_max_namelen */
2363 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2364 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2365 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2366 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2367 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2368 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2370 {_bfd_dummy_target, alpha_ecoff_object_p, /* bfd_check_format */
2371 _bfd_ecoff_archive_p, _bfd_dummy_target},
2372 {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
2373 _bfd_generic_mkarchive, bfd_false},
2374 {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
2375 _bfd_write_archive_contents, bfd_false},
2377 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2378 BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2379 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2380 BFD_JUMP_TABLE_ARCHIVE (alpha_ecoff),
2381 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2382 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2383 BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2384 BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2385 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2387 (PTR) &alpha_ecoff_backend_data