Implement tp_richcompare for gdb.Block
[binutils-gdb.git] / bfd / coff-alpha.c
blob38d2c10ade3951069206a32b778de6cfdb7adb74
1 /* BFD back-end for ALPHA Extended-Coff files.
2 Copyright (C) 1993-2024 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "coff/internal.h"
28 #include "coff/sym.h"
29 #include "coff/symconst.h"
30 #include "coff/ecoff.h"
31 #include "coff/alpha.h"
32 #include "aout/ar.h"
33 #include "libcoff.h"
34 #include "libecoff.h"
36 /* Prototypes for static functions. */
40 /* ECOFF has COFF sections, but the debugging information is stored in
41 a completely different format. ECOFF targets use some of the
42 swapping routines from coffswap.h, and some of the generic COFF
43 routines in coffgen.c, but, unlike the real COFF targets, do not
44 use coffcode.h itself.
46 Get the generic COFF swapping routines, except for the reloc,
47 symbol, and lineno ones. Give them ecoff names. Define some
48 accessor macros for the large sizes used for Alpha ECOFF. */
50 #define GET_FILEHDR_SYMPTR H_GET_64
51 #define PUT_FILEHDR_SYMPTR H_PUT_64
52 #define GET_AOUTHDR_TSIZE H_GET_64
53 #define PUT_AOUTHDR_TSIZE H_PUT_64
54 #define GET_AOUTHDR_DSIZE H_GET_64
55 #define PUT_AOUTHDR_DSIZE H_PUT_64
56 #define GET_AOUTHDR_BSIZE H_GET_64
57 #define PUT_AOUTHDR_BSIZE H_PUT_64
58 #define GET_AOUTHDR_ENTRY H_GET_64
59 #define PUT_AOUTHDR_ENTRY H_PUT_64
60 #define GET_AOUTHDR_TEXT_START H_GET_64
61 #define PUT_AOUTHDR_TEXT_START H_PUT_64
62 #define GET_AOUTHDR_DATA_START H_GET_64
63 #define PUT_AOUTHDR_DATA_START H_PUT_64
64 #define GET_SCNHDR_PADDR H_GET_64
65 #define PUT_SCNHDR_PADDR H_PUT_64
66 #define GET_SCNHDR_VADDR H_GET_64
67 #define PUT_SCNHDR_VADDR H_PUT_64
68 #define GET_SCNHDR_SIZE H_GET_64
69 #define PUT_SCNHDR_SIZE H_PUT_64
70 #define GET_SCNHDR_SCNPTR H_GET_64
71 #define PUT_SCNHDR_SCNPTR H_PUT_64
72 #define GET_SCNHDR_RELPTR H_GET_64
73 #define PUT_SCNHDR_RELPTR H_PUT_64
74 #define GET_SCNHDR_LNNOPTR H_GET_64
75 #define PUT_SCNHDR_LNNOPTR H_PUT_64
77 #define ALPHAECOFF
79 #define NO_COFF_RELOCS
80 #define NO_COFF_SYMBOLS
81 #define NO_COFF_LINENOS
82 #define coff_swap_filehdr_in alpha_ecoff_swap_filehdr_in
83 #define coff_swap_filehdr_out alpha_ecoff_swap_filehdr_out
84 #define coff_swap_aouthdr_in alpha_ecoff_swap_aouthdr_in
85 #define coff_swap_aouthdr_out alpha_ecoff_swap_aouthdr_out
86 #define coff_swap_scnhdr_in alpha_ecoff_swap_scnhdr_in
87 #define coff_swap_scnhdr_out alpha_ecoff_swap_scnhdr_out
88 #include "coffswap.h"
90 /* Get the ECOFF swapping routines. */
91 #define ECOFF_64
92 #include "ecoffswap.h"
94 /* How to process the various reloc types. */
96 static bfd_reloc_status_type
97 reloc_nil (bfd *abfd ATTRIBUTE_UNUSED,
98 arelent *reloc ATTRIBUTE_UNUSED,
99 asymbol *sym ATTRIBUTE_UNUSED,
100 void * data ATTRIBUTE_UNUSED,
101 asection *sec ATTRIBUTE_UNUSED,
102 bfd *output_bfd ATTRIBUTE_UNUSED,
103 char **error_message ATTRIBUTE_UNUSED)
105 return bfd_reloc_ok;
108 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
109 from smaller values. Start with zero, widen, *then* decrement. */
110 #define MINUS_ONE (((bfd_vma)0) - 1)
112 static reloc_howto_type alpha_howto_table[] =
114 /* Reloc type 0 is ignored by itself. However, it appears after a
115 GPDISP reloc to identify the location where the low order 16 bits
116 of the gp register are loaded. */
117 HOWTO (ALPHA_R_IGNORE, /* type */
118 0, /* rightshift */
119 1, /* size */
120 8, /* bitsize */
121 true, /* pc_relative */
122 0, /* bitpos */
123 complain_overflow_dont, /* complain_on_overflow */
124 reloc_nil, /* special_function */
125 "IGNORE", /* name */
126 true, /* partial_inplace */
127 0, /* src_mask */
128 0, /* dst_mask */
129 true), /* pcrel_offset */
131 /* A 32 bit reference to a symbol. */
132 HOWTO (ALPHA_R_REFLONG, /* type */
133 0, /* rightshift */
134 4, /* size */
135 32, /* bitsize */
136 false, /* pc_relative */
137 0, /* bitpos */
138 complain_overflow_bitfield, /* complain_on_overflow */
139 0, /* special_function */
140 "REFLONG", /* name */
141 true, /* partial_inplace */
142 0xffffffff, /* src_mask */
143 0xffffffff, /* dst_mask */
144 false), /* pcrel_offset */
146 /* A 64 bit reference to a symbol. */
147 HOWTO (ALPHA_R_REFQUAD, /* type */
148 0, /* rightshift */
149 8, /* size */
150 64, /* bitsize */
151 false, /* pc_relative */
152 0, /* bitpos */
153 complain_overflow_bitfield, /* complain_on_overflow */
154 0, /* special_function */
155 "REFQUAD", /* name */
156 true, /* partial_inplace */
157 MINUS_ONE, /* src_mask */
158 MINUS_ONE, /* dst_mask */
159 false), /* pcrel_offset */
161 /* A 32 bit GP relative offset. This is just like REFLONG except
162 that when the value is used the value of the gp register will be
163 added in. */
164 HOWTO (ALPHA_R_GPREL32, /* type */
165 0, /* rightshift */
166 4, /* size */
167 32, /* bitsize */
168 false, /* pc_relative */
169 0, /* bitpos */
170 complain_overflow_bitfield, /* complain_on_overflow */
171 0, /* special_function */
172 "GPREL32", /* name */
173 true, /* partial_inplace */
174 0xffffffff, /* src_mask */
175 0xffffffff, /* dst_mask */
176 false), /* pcrel_offset */
178 /* Used for an instruction that refers to memory off the GP
179 register. The offset is 16 bits of the 32 bit instruction. This
180 reloc always seems to be against the .lita section. */
181 HOWTO (ALPHA_R_LITERAL, /* type */
182 0, /* rightshift */
183 4, /* size */
184 16, /* bitsize */
185 false, /* pc_relative */
186 0, /* bitpos */
187 complain_overflow_signed, /* complain_on_overflow */
188 0, /* special_function */
189 "LITERAL", /* name */
190 true, /* partial_inplace */
191 0xffff, /* src_mask */
192 0xffff, /* dst_mask */
193 false), /* pcrel_offset */
195 /* This reloc only appears immediately following a LITERAL reloc.
196 It identifies a use of the literal. It seems that the linker can
197 use this to eliminate a portion of the .lita section. The symbol
198 index is special: 1 means the literal address is in the base
199 register of a memory format instruction; 2 means the literal
200 address is in the byte offset register of a byte-manipulation
201 instruction; 3 means the literal address is in the target
202 register of a jsr instruction. This does not actually do any
203 relocation. */
204 HOWTO (ALPHA_R_LITUSE, /* type */
205 0, /* rightshift */
206 4, /* size */
207 32, /* bitsize */
208 false, /* pc_relative */
209 0, /* bitpos */
210 complain_overflow_dont, /* complain_on_overflow */
211 reloc_nil, /* special_function */
212 "LITUSE", /* name */
213 false, /* partial_inplace */
214 0, /* src_mask */
215 0, /* dst_mask */
216 false), /* pcrel_offset */
218 /* Load the gp register. This is always used for a ldah instruction
219 which loads the upper 16 bits of the gp register. The next reloc
220 will be an IGNORE reloc which identifies the location of the lda
221 instruction which loads the lower 16 bits. The symbol index of
222 the GPDISP instruction appears to actually be the number of bytes
223 between the ldah and lda instructions. This gives two different
224 ways to determine where the lda instruction is; I don't know why
225 both are used. The value to use for the relocation is the
226 difference between the GP value and the current location; the
227 load will always be done against a register holding the current
228 address. */
229 HOWTO (ALPHA_R_GPDISP, /* type */
230 16, /* rightshift */
231 4, /* size */
232 16, /* bitsize */
233 true, /* pc_relative */
234 0, /* bitpos */
235 complain_overflow_dont, /* complain_on_overflow */
236 reloc_nil, /* special_function */
237 "GPDISP", /* name */
238 true, /* partial_inplace */
239 0xffff, /* src_mask */
240 0xffff, /* dst_mask */
241 true), /* pcrel_offset */
243 /* A 21 bit branch. The native assembler generates these for
244 branches within the text segment, and also fills in the PC
245 relative offset in the instruction. */
246 HOWTO (ALPHA_R_BRADDR, /* type */
247 2, /* rightshift */
248 4, /* size */
249 21, /* bitsize */
250 true, /* pc_relative */
251 0, /* bitpos */
252 complain_overflow_signed, /* complain_on_overflow */
253 0, /* special_function */
254 "BRADDR", /* name */
255 true, /* partial_inplace */
256 0x1fffff, /* src_mask */
257 0x1fffff, /* dst_mask */
258 false), /* pcrel_offset */
260 /* A hint for a jump to a register. */
261 HOWTO (ALPHA_R_HINT, /* type */
262 2, /* rightshift */
263 4, /* size */
264 14, /* bitsize */
265 true, /* pc_relative */
266 0, /* bitpos */
267 complain_overflow_dont, /* complain_on_overflow */
268 0, /* special_function */
269 "HINT", /* name */
270 true, /* partial_inplace */
271 0x3fff, /* src_mask */
272 0x3fff, /* dst_mask */
273 false), /* pcrel_offset */
275 /* 16 bit PC relative offset. */
276 HOWTO (ALPHA_R_SREL16, /* type */
277 0, /* rightshift */
278 2, /* size */
279 16, /* bitsize */
280 true, /* pc_relative */
281 0, /* bitpos */
282 complain_overflow_signed, /* complain_on_overflow */
283 0, /* special_function */
284 "SREL16", /* name */
285 true, /* partial_inplace */
286 0xffff, /* src_mask */
287 0xffff, /* dst_mask */
288 false), /* pcrel_offset */
290 /* 32 bit PC relative offset. */
291 HOWTO (ALPHA_R_SREL32, /* type */
292 0, /* rightshift */
293 4, /* size */
294 32, /* bitsize */
295 true, /* pc_relative */
296 0, /* bitpos */
297 complain_overflow_signed, /* complain_on_overflow */
298 0, /* special_function */
299 "SREL32", /* name */
300 true, /* partial_inplace */
301 0xffffffff, /* src_mask */
302 0xffffffff, /* dst_mask */
303 false), /* pcrel_offset */
305 /* A 64 bit PC relative offset. */
306 HOWTO (ALPHA_R_SREL64, /* type */
307 0, /* rightshift */
308 8, /* size */
309 64, /* bitsize */
310 true, /* pc_relative */
311 0, /* bitpos */
312 complain_overflow_signed, /* complain_on_overflow */
313 0, /* special_function */
314 "SREL64", /* name */
315 true, /* partial_inplace */
316 MINUS_ONE, /* src_mask */
317 MINUS_ONE, /* dst_mask */
318 false), /* pcrel_offset */
320 /* Push a value on the reloc evaluation stack. */
321 HOWTO (ALPHA_R_OP_PUSH, /* type */
322 0, /* rightshift */
323 0, /* size */
324 0, /* bitsize */
325 false, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_dont, /* complain_on_overflow */
328 0, /* special_function */
329 "OP_PUSH", /* name */
330 false, /* partial_inplace */
331 0, /* src_mask */
332 0, /* dst_mask */
333 false), /* pcrel_offset */
335 /* Store the value from the stack at the given address. Store it in
336 a bitfield of size r_size starting at bit position r_offset. */
337 HOWTO (ALPHA_R_OP_STORE, /* type */
338 0, /* rightshift */
339 8, /* size */
340 64, /* bitsize */
341 false, /* pc_relative */
342 0, /* bitpos */
343 complain_overflow_dont, /* complain_on_overflow */
344 0, /* special_function */
345 "OP_STORE", /* name */
346 false, /* partial_inplace */
347 0, /* src_mask */
348 MINUS_ONE, /* dst_mask */
349 false), /* pcrel_offset */
351 /* Subtract the reloc address from the value on the top of the
352 relocation stack. */
353 HOWTO (ALPHA_R_OP_PSUB, /* type */
354 0, /* rightshift */
355 0, /* size */
356 0, /* bitsize */
357 false, /* pc_relative */
358 0, /* bitpos */
359 complain_overflow_dont, /* complain_on_overflow */
360 0, /* special_function */
361 "OP_PSUB", /* name */
362 false, /* partial_inplace */
363 0, /* src_mask */
364 0, /* dst_mask */
365 false), /* pcrel_offset */
367 /* Shift the value on the top of the relocation stack right by the
368 given value. */
369 HOWTO (ALPHA_R_OP_PRSHIFT, /* type */
370 0, /* rightshift */
371 0, /* size */
372 0, /* bitsize */
373 false, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_dont, /* complain_on_overflow */
376 0, /* special_function */
377 "OP_PRSHIFT", /* name */
378 false, /* partial_inplace */
379 0, /* src_mask */
380 0, /* dst_mask */
381 false), /* pcrel_offset */
383 /* Adjust the GP value for a new range in the object file. */
384 HOWTO (ALPHA_R_GPVALUE, /* type */
385 0, /* rightshift */
386 0, /* size */
387 0, /* bitsize */
388 false, /* pc_relative */
389 0, /* bitpos */
390 complain_overflow_dont, /* complain_on_overflow */
391 0, /* special_function */
392 "GPVALUE", /* name */
393 false, /* partial_inplace */
394 0, /* src_mask */
395 0, /* dst_mask */
396 false) /* pcrel_offset */
399 /* Recognize an Alpha ECOFF file. */
401 static bfd_cleanup
402 alpha_ecoff_object_p (bfd *abfd)
404 bfd_cleanup ret;
406 ret = coff_object_p (abfd);
408 if (ret != NULL)
410 asection *sec;
412 /* Alpha ECOFF has a .pdata section. The lnnoptr field of the
413 .pdata section is the number of entries it contains. Each
414 entry takes up 8 bytes. The number of entries is required
415 since the section is aligned to a 16 byte boundary. When we
416 link .pdata sections together, we do not want to include the
417 alignment bytes. We handle this on input by faking the size
418 of the .pdata section to remove the unwanted alignment bytes.
419 On output we will set the lnnoptr field and force the
420 alignment. */
421 sec = bfd_get_section_by_name (abfd, _PDATA);
422 if (sec != (asection *) NULL)
424 bfd_size_type size;
426 size = (bfd_size_type) sec->line_filepos * 8;
427 BFD_ASSERT (size == sec->size
428 || size + 8 == sec->size);
429 if (!bfd_set_section_size (sec, size))
430 return NULL;
434 return ret;
437 /* See whether the magic number matches. */
439 static bool
440 alpha_ecoff_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED,
441 void * filehdr)
443 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
445 if (! ALPHA_ECOFF_BADMAG (*internal_f))
446 return true;
448 if (ALPHA_ECOFF_COMPRESSEDMAG (*internal_f))
449 _bfd_error_handler
450 (_("%pB: cannot handle compressed Alpha binaries; "
451 "use compiler flags, or objZ, to generate uncompressed binaries"),
452 abfd);
454 return false;
457 /* This is a hook called by coff_real_object_p to create any backend
458 specific information. */
460 static void *
461 alpha_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr)
463 void * ecoff;
465 ecoff = _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr);
467 if (ecoff != NULL)
469 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
471 /* Set additional BFD flags according to the object type from the
472 machine specific file header flags. */
473 switch (internal_f->f_flags & F_ALPHA_OBJECT_TYPE_MASK)
475 case F_ALPHA_SHARABLE:
476 abfd->flags |= DYNAMIC;
477 break;
478 case F_ALPHA_CALL_SHARED:
479 /* Always executable if using shared libraries as the run time
480 loader might resolve undefined references. */
481 abfd->flags |= (DYNAMIC | EXEC_P);
482 break;
485 return ecoff;
488 /* Reloc handling. */
490 /* Swap a reloc in. */
492 static void
493 alpha_ecoff_swap_reloc_in (bfd *abfd,
494 void * ext_ptr,
495 struct internal_reloc *intern)
497 const RELOC *ext = (RELOC *) ext_ptr;
499 intern->r_vaddr = H_GET_64 (abfd, ext->r_vaddr);
500 intern->r_symndx = H_GET_32 (abfd, ext->r_symndx);
502 BFD_ASSERT (bfd_header_little_endian (abfd));
504 intern->r_type = ((ext->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
505 >> RELOC_BITS0_TYPE_SH_LITTLE);
506 intern->r_extern = (ext->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
507 intern->r_offset = ((ext->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
508 >> RELOC_BITS1_OFFSET_SH_LITTLE);
509 /* Ignored the reserved bits. */
510 intern->r_size = ((ext->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
511 >> RELOC_BITS3_SIZE_SH_LITTLE);
513 if (intern->r_type == ALPHA_R_LITUSE
514 || intern->r_type == ALPHA_R_GPDISP)
516 /* Handle the LITUSE and GPDISP relocs specially. Its symndx
517 value is not actually a symbol index, but is instead a
518 special code. We put the code in the r_size field, and
519 clobber the symndx. */
520 if (intern->r_size != 0)
521 abort ();
522 intern->r_size = intern->r_symndx;
523 intern->r_symndx = RELOC_SECTION_NONE;
525 else if (intern->r_type == ALPHA_R_IGNORE)
527 /* The IGNORE reloc generally follows a GPDISP reloc, and is
528 against the .lita section. The section is irrelevant. */
529 if (! intern->r_extern &&
530 intern->r_symndx == RELOC_SECTION_ABS)
531 abort ();
532 if (! intern->r_extern && intern->r_symndx == RELOC_SECTION_LITA)
533 intern->r_symndx = RELOC_SECTION_ABS;
537 /* Swap a reloc out. */
539 static void
540 alpha_ecoff_swap_reloc_out (bfd *abfd,
541 const struct internal_reloc *intern,
542 void * dst)
544 RELOC *ext = (RELOC *) dst;
545 long symndx;
546 unsigned char size;
548 /* Undo the hackery done in swap_reloc_in. */
549 if (intern->r_type == ALPHA_R_LITUSE
550 || intern->r_type == ALPHA_R_GPDISP)
552 symndx = intern->r_size;
553 size = 0;
555 else if (intern->r_type == ALPHA_R_IGNORE
556 && ! intern->r_extern
557 && intern->r_symndx == RELOC_SECTION_ABS)
559 symndx = RELOC_SECTION_LITA;
560 size = intern->r_size;
562 else
564 symndx = intern->r_symndx;
565 size = intern->r_size;
568 /* XXX FIXME: The maximum symndx value used to be 14 but this
569 fails with object files produced by DEC's C++ compiler.
570 Where does the value 14 (or 15) come from anyway ? */
571 BFD_ASSERT (intern->r_extern
572 || (intern->r_symndx >= 0 && intern->r_symndx <= 15));
574 H_PUT_64 (abfd, intern->r_vaddr, ext->r_vaddr);
575 H_PUT_32 (abfd, symndx, ext->r_symndx);
577 BFD_ASSERT (bfd_header_little_endian (abfd));
579 ext->r_bits[0] = ((intern->r_type << RELOC_BITS0_TYPE_SH_LITTLE)
580 & RELOC_BITS0_TYPE_LITTLE);
581 ext->r_bits[1] = ((intern->r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0)
582 | ((intern->r_offset << RELOC_BITS1_OFFSET_SH_LITTLE)
583 & RELOC_BITS1_OFFSET_LITTLE));
584 ext->r_bits[2] = 0;
585 ext->r_bits[3] = ((size << RELOC_BITS3_SIZE_SH_LITTLE)
586 & RELOC_BITS3_SIZE_LITTLE);
589 /* Finish canonicalizing a reloc. Part of this is generic to all
590 ECOFF targets, and that part is in ecoff.c. The rest is done in
591 this backend routine. It must fill in the howto field. */
593 static void
594 alpha_adjust_reloc_in (bfd *abfd,
595 const struct internal_reloc *intern,
596 arelent *rptr)
598 if (intern->r_type > ALPHA_R_GPVALUE)
600 /* xgettext:c-format */
601 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
602 abfd, intern->r_type);
603 bfd_set_error (bfd_error_bad_value);
604 rptr->addend = 0;
605 rptr->howto = NULL;
606 return;
609 switch (intern->r_type)
611 case ALPHA_R_BRADDR:
612 case ALPHA_R_SREL16:
613 case ALPHA_R_SREL32:
614 case ALPHA_R_SREL64:
615 /* This relocs appear to be fully resolved when they are against
616 internal symbols. Against external symbols, BRADDR at least
617 appears to be resolved against the next instruction. */
618 if (! intern->r_extern)
619 rptr->addend = 0;
620 else
621 rptr->addend = - (intern->r_vaddr + 4);
622 break;
624 case ALPHA_R_GPREL32:
625 case ALPHA_R_LITERAL:
626 /* Copy the gp value for this object file into the addend, to
627 ensure that we are not confused by the linker. */
628 if (! intern->r_extern)
629 rptr->addend += ecoff_data (abfd)->gp;
630 break;
632 case ALPHA_R_LITUSE:
633 case ALPHA_R_GPDISP:
634 /* The LITUSE and GPDISP relocs do not use a symbol, or an
635 addend, but they do use a special code. Put this code in the
636 addend field. */
637 rptr->addend = intern->r_size;
638 break;
640 case ALPHA_R_OP_STORE:
641 /* The STORE reloc needs the size and offset fields. We store
642 them in the addend. */
643 BFD_ASSERT (intern->r_offset <= 256);
644 rptr->addend = (intern->r_offset << 8) + intern->r_size;
645 break;
647 case ALPHA_R_OP_PUSH:
648 case ALPHA_R_OP_PSUB:
649 case ALPHA_R_OP_PRSHIFT:
650 /* The PUSH, PSUB and PRSHIFT relocs do not actually use an
651 address. I believe that the address supplied is really an
652 addend. */
653 rptr->addend = intern->r_vaddr;
654 break;
656 case ALPHA_R_GPVALUE:
657 /* Set the addend field to the new GP value. */
658 rptr->addend = intern->r_symndx + ecoff_data (abfd)->gp;
659 break;
661 case ALPHA_R_IGNORE:
662 /* If the type is ALPHA_R_IGNORE, make sure this is a reference
663 to the absolute section so that the reloc is ignored. For
664 some reason the address of this reloc type is not adjusted by
665 the section vma. We record the gp value for this object file
666 here, for convenience when doing the GPDISP relocation. */
667 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
668 rptr->address = intern->r_vaddr;
669 rptr->addend = ecoff_data (abfd)->gp;
670 break;
672 default:
673 break;
676 rptr->howto = &alpha_howto_table[intern->r_type];
679 /* When writing out a reloc we need to pull some values back out of
680 the addend field into the reloc. This is roughly the reverse of
681 alpha_adjust_reloc_in, except that there are several changes we do
682 not need to undo. */
684 static void
685 alpha_adjust_reloc_out (bfd *abfd ATTRIBUTE_UNUSED,
686 const arelent *rel,
687 struct internal_reloc *intern)
689 switch (intern->r_type)
691 case ALPHA_R_LITUSE:
692 case ALPHA_R_GPDISP:
693 intern->r_size = rel->addend;
694 break;
696 case ALPHA_R_OP_STORE:
697 intern->r_size = rel->addend & 0xff;
698 intern->r_offset = (rel->addend >> 8) & 0xff;
699 break;
701 case ALPHA_R_OP_PUSH:
702 case ALPHA_R_OP_PSUB:
703 case ALPHA_R_OP_PRSHIFT:
704 intern->r_vaddr = rel->addend;
705 break;
707 case ALPHA_R_IGNORE:
708 intern->r_vaddr = rel->address;
709 break;
711 default:
712 break;
716 /* The size of the stack for the relocation evaluator. */
717 #define RELOC_STACKSIZE (10)
719 /* Alpha ECOFF relocs have a built in expression evaluator as well as
720 other interdependencies. Rather than use a bunch of special
721 functions and global variables, we use a single routine to do all
722 the relocation for a section. I haven't yet worked out how the
723 assembler is going to handle this. */
725 static bfd_byte *
726 alpha_ecoff_get_relocated_section_contents (bfd *abfd,
727 struct bfd_link_info *link_info,
728 struct bfd_link_order *link_order,
729 bfd_byte *data,
730 bool relocatable,
731 asymbol **symbols)
733 bfd *input_bfd = link_order->u.indirect.section->owner;
734 asection *input_section = link_order->u.indirect.section;
735 long reloc_size;
736 arelent **reloc_vector;
737 long reloc_count;
738 bfd *output_bfd = relocatable ? abfd : (bfd *) NULL;
739 bfd_vma gp;
740 bool gp_undefined;
741 bfd_vma stack[RELOC_STACKSIZE];
742 int tos = 0;
744 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
745 if (reloc_size < 0)
746 return NULL;
748 bfd_byte *orig_data = data;
749 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
750 return NULL;
752 if (data == NULL)
753 return NULL;
755 if (reloc_size == 0)
756 return data;
758 reloc_vector = (arelent **) bfd_malloc (reloc_size);
759 if (reloc_vector == NULL)
760 goto error_return;
762 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
763 reloc_vector, symbols);
764 if (reloc_count < 0)
765 goto error_return;
766 if (reloc_count == 0)
767 goto successful_return;
769 /* Get the GP value for the output BFD. */
770 gp_undefined = false;
771 gp = _bfd_get_gp_value (abfd);
772 if (gp == 0)
774 if (relocatable)
776 asection *sec;
777 bfd_vma lo;
779 /* Make up a value. */
780 lo = (bfd_vma) -1;
781 for (sec = abfd->sections; sec != NULL; sec = sec->next)
783 if (sec->vma < lo
784 && (strcmp (sec->name, ".sbss") == 0
785 || strcmp (sec->name, ".sdata") == 0
786 || strcmp (sec->name, ".lit4") == 0
787 || strcmp (sec->name, ".lit8") == 0
788 || strcmp (sec->name, ".lita") == 0))
789 lo = sec->vma;
791 gp = lo + 0x8000;
792 _bfd_set_gp_value (abfd, gp);
794 else
796 struct bfd_link_hash_entry *h;
798 h = bfd_link_hash_lookup (link_info->hash, "_gp", false, false,
799 true);
800 if (h == (struct bfd_link_hash_entry *) NULL
801 || h->type != bfd_link_hash_defined)
802 gp_undefined = true;
803 else
805 gp = (h->u.def.value
806 + h->u.def.section->output_section->vma
807 + h->u.def.section->output_offset);
808 _bfd_set_gp_value (abfd, gp);
813 for (arelent **relp = reloc_vector; *relp != NULL; relp++)
815 arelent *rel;
816 bfd_reloc_status_type r;
817 char *err;
818 unsigned int r_type;
820 rel = *relp;
821 if (rel->howto == NULL)
823 r = bfd_reloc_notsupported;
824 r_type = ALPHA_R_IGNORE;
826 else
828 r = bfd_reloc_ok;
829 r_type = rel->howto->type;
831 switch (r_type)
833 case ALPHA_R_IGNORE:
834 rel->address += input_section->output_offset;
835 break;
837 case ALPHA_R_REFLONG:
838 case ALPHA_R_REFQUAD:
839 case ALPHA_R_BRADDR:
840 case ALPHA_R_HINT:
841 case ALPHA_R_SREL16:
842 case ALPHA_R_SREL32:
843 case ALPHA_R_SREL64:
844 if (relocatable
845 && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
847 rel->address += input_section->output_offset;
848 break;
850 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
851 output_bfd, &err);
852 break;
854 case ALPHA_R_GPREL32:
855 /* This relocation is used in a switch table. It is a 32
856 bit offset from the current GP value. We must adjust it
857 by the different between the original GP value and the
858 current GP value. The original GP value is stored in the
859 addend. We adjust the addend and let
860 bfd_perform_relocation finish the job. */
861 rel->addend -= gp;
862 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
863 output_bfd, &err);
864 if (r == bfd_reloc_ok && gp_undefined)
866 r = bfd_reloc_dangerous;
867 err = (char *) _("GP relative relocation used when GP not defined");
869 break;
871 case ALPHA_R_LITERAL:
872 /* This is a reference to a literal value, generally
873 (always?) in the .lita section. This is a 16 bit GP
874 relative relocation. Sometimes the subsequent reloc is a
875 LITUSE reloc, which indicates how this reloc is used.
876 This sometimes permits rewriting the two instructions
877 referred to by the LITERAL and the LITUSE into different
878 instructions which do not refer to .lita. This can save
879 a memory reference, and permits removing a value from
880 .lita thus saving GP relative space.
882 We do not these optimizations. To do them we would need
883 to arrange to link the .lita section first, so that by
884 the time we got here we would know the final values to
885 use. This would not be particularly difficult, but it is
886 not currently implemented. */
888 rel->addend -= gp;
889 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
890 output_bfd, &err);
891 if (r == bfd_reloc_ok && gp_undefined)
893 r = bfd_reloc_dangerous;
894 err = (char *) _("GP relative relocation used"
895 " when GP not defined");
897 break;
899 case ALPHA_R_LITUSE:
900 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
901 does not cause anything to happen, itself. */
902 rel->address += input_section->output_offset;
903 break;
905 case ALPHA_R_GPDISP:
906 /* This marks the ldah of an ldah/lda pair which loads the
907 gp register with the difference of the gp value and the
908 current location. The second of the pair is r_size bytes
909 ahead; it used to be marked with an ALPHA_R_IGNORE reloc,
910 but that no longer happens in OSF/1 3.2. */
911 if (bfd_reloc_offset_in_range (rel->howto, input_bfd, input_section,
912 rel->address)
913 && bfd_reloc_offset_in_range (rel->howto, input_bfd, input_section,
914 rel->address + rel->addend))
916 /* Get the two instructions. */
917 bfd_byte *p = data + rel->address;
918 bfd_vma insn1 = bfd_get_32 (input_bfd, p);
919 bfd_vma insn2 = bfd_get_32 (input_bfd, p + rel->addend);
921 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
922 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
924 /* Get the existing addend. We must account for the sign
925 extension done by lda and ldah. */
926 bfd_vma addend = (((((insn1 & 0xffff) ^ 0x8000) - 0x8000) << 16)
927 + ((((insn2 & 0xffff) ^ 0x8000) - 0x8000)));
929 /* The existing addend includes the different between the
930 gp of the input BFD and the address in the input BFD.
931 Subtract this out. */
932 addend -= ecoff_data (input_bfd)->gp - input_section->vma;
934 /* Now add in the final gp value, and subtract out the
935 final address. */
936 addend += gp - (input_section->output_section->vma
937 + input_section->output_offset);
939 /* Change the instructions, accounting for the sign
940 extension, and write them out. */
941 insn1 = (insn1 & ~0xffff) | (((addend + 0x8000) >> 16) & 0xffff);
942 insn2 = (insn2 & ~0xffff) | (addend & 0xffff);
944 bfd_put_32 (input_bfd, insn1, p);
945 bfd_put_32 (input_bfd, insn2, p + rel->addend);
947 else
948 r = bfd_reloc_outofrange;
950 rel->address += input_section->output_offset;
951 break;
953 case ALPHA_R_OP_PUSH:
954 /* Push a value on the reloc evaluation stack. */
956 asymbol *symbol;
957 bfd_vma relocation;
959 if (relocatable)
961 rel->address += input_section->output_offset;
962 break;
965 /* Figure out the relocation of this symbol. */
966 symbol = *rel->sym_ptr_ptr;
968 if (bfd_is_und_section (symbol->section))
969 r = bfd_reloc_undefined;
971 if (bfd_is_com_section (symbol->section))
972 relocation = 0;
973 else
974 relocation = symbol->value;
975 relocation += symbol->section->output_section->vma;
976 relocation += symbol->section->output_offset;
977 relocation += rel->addend;
979 if (tos >= RELOC_STACKSIZE)
981 r = bfd_reloc_notsupported;
982 break;
985 stack[tos++] = relocation;
987 break;
989 case ALPHA_R_OP_STORE:
990 /* Store a value from the reloc stack into a bitfield. */
992 if (relocatable)
994 rel->address += input_section->output_offset;
995 break;
998 if (tos == 0)
1000 r = bfd_reloc_notsupported;
1001 break;
1004 /* The offset and size in bits for this reloc are encoded
1005 into the addend field by alpha_adjust_reloc_in. */
1006 unsigned int offset = (rel->addend >> 8) & 0xff;
1007 unsigned int size = rel->addend & 0xff;
1008 unsigned int startbyte = offset >> 3;
1009 unsigned int endbyte = (offset + size + 7) >> 3;
1010 unsigned int bytes = endbyte + 1 - startbyte;
1012 if (bytes <= 8
1013 && rel->address + startbyte + bytes >= rel->address
1014 && (rel->address + startbyte + bytes
1015 <= bfd_get_section_limit_octets (input_bfd, input_section)))
1017 uint64_t val = 0;
1018 for (int off = bytes - 1; off >= 0; --off)
1019 val = (val << 8) | data[rel->address + startbyte + off];
1021 offset -= startbyte << 3;
1022 size -= startbyte << 3;
1023 uint64_t mask = (((uint64_t) 1 << size) - 1) << offset;
1024 val = (val & ~mask) | ((stack[--tos] << offset) & mask);
1026 for (unsigned int off = 0; off < bytes; ++off)
1028 data[rel->address + startbyte + off] = val & 0xff;
1029 val >>= 8;
1032 else
1033 r = bfd_reloc_outofrange;
1035 break;
1037 case ALPHA_R_OP_PSUB:
1038 /* Subtract a value from the top of the stack. */
1040 asymbol *symbol;
1041 bfd_vma relocation;
1043 if (relocatable)
1045 rel->address += input_section->output_offset;
1046 break;
1049 /* Figure out the relocation of this symbol. */
1050 symbol = *rel->sym_ptr_ptr;
1052 if (bfd_is_und_section (symbol->section))
1053 r = bfd_reloc_undefined;
1055 if (bfd_is_com_section (symbol->section))
1056 relocation = 0;
1057 else
1058 relocation = symbol->value;
1059 relocation += symbol->section->output_section->vma;
1060 relocation += symbol->section->output_offset;
1061 relocation += rel->addend;
1063 if (tos == 0)
1065 r = bfd_reloc_notsupported;
1066 break;
1069 stack[tos - 1] -= relocation;
1071 break;
1073 case ALPHA_R_OP_PRSHIFT:
1074 /* Shift the value on the top of the stack. */
1076 asymbol *symbol;
1077 bfd_vma relocation;
1079 if (relocatable)
1081 rel->address += input_section->output_offset;
1082 break;
1085 /* Figure out the relocation of this symbol. */
1086 symbol = *rel->sym_ptr_ptr;
1088 if (bfd_is_und_section (symbol->section))
1089 r = bfd_reloc_undefined;
1091 if (bfd_is_com_section (symbol->section))
1092 relocation = 0;
1093 else
1094 relocation = symbol->value;
1095 relocation += symbol->section->output_section->vma;
1096 relocation += symbol->section->output_offset;
1097 relocation += rel->addend;
1099 if (tos == 0)
1101 r = bfd_reloc_notsupported;
1102 break;
1105 stack[tos - 1] >>= relocation;
1107 break;
1109 case ALPHA_R_GPVALUE:
1110 /* I really don't know if this does the right thing. */
1111 gp = rel->addend;
1112 gp_undefined = false;
1113 break;
1115 default:
1116 r = bfd_reloc_notsupported;
1117 break;
1120 if (relocatable)
1122 asection *os = input_section->output_section;
1124 /* A partial link, so keep the relocs. */
1125 os->orelocation[os->reloc_count] = rel;
1126 os->reloc_count++;
1129 if (r != bfd_reloc_ok)
1131 switch (r)
1133 case bfd_reloc_undefined:
1134 (*link_info->callbacks->undefined_symbol)
1135 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1136 input_bfd, input_section, rel->address, true);
1137 break;
1138 case bfd_reloc_dangerous:
1139 (*link_info->callbacks->reloc_dangerous)
1140 (link_info, err, input_bfd, input_section, rel->address);
1141 break;
1142 case bfd_reloc_overflow:
1143 (*link_info->callbacks->reloc_overflow)
1144 (link_info, NULL, bfd_asymbol_name (*rel->sym_ptr_ptr),
1145 rel->howto->name, rel->addend, input_bfd,
1146 input_section, rel->address);
1147 break;
1148 case bfd_reloc_outofrange:
1149 (*link_info->callbacks->einfo)
1150 /* xgettext:c-format */
1151 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
1152 input_bfd, input_section, rel);
1153 goto error_return;
1154 case bfd_reloc_notsupported:
1155 (*link_info->callbacks->einfo)
1156 /* xgettext:c-format */
1157 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
1158 input_bfd, input_section, rel);
1159 goto error_return;
1160 default:
1161 (*link_info->callbacks->einfo)
1162 /* xgettext:c-format */
1163 (_("%X%P: %pB(%pA): relocation \"%pR\""
1164 " returns an unrecognized value %x\n"),
1165 input_bfd, input_section, rel, r);
1166 break;
1171 if (tos != 0)
1172 goto error_return;
1174 successful_return:
1175 free (reloc_vector);
1176 return data;
1178 error_return:
1179 free (reloc_vector);
1180 if (orig_data == NULL)
1181 free (data);
1182 return NULL;
1185 /* Get the howto structure for a generic reloc type. */
1187 static reloc_howto_type *
1188 alpha_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1189 bfd_reloc_code_real_type code)
1191 int alpha_type;
1193 switch (code)
1195 case BFD_RELOC_32:
1196 alpha_type = ALPHA_R_REFLONG;
1197 break;
1198 case BFD_RELOC_64:
1199 case BFD_RELOC_CTOR:
1200 alpha_type = ALPHA_R_REFQUAD;
1201 break;
1202 case BFD_RELOC_GPREL32:
1203 alpha_type = ALPHA_R_GPREL32;
1204 break;
1205 case BFD_RELOC_ALPHA_LITERAL:
1206 alpha_type = ALPHA_R_LITERAL;
1207 break;
1208 case BFD_RELOC_ALPHA_LITUSE:
1209 alpha_type = ALPHA_R_LITUSE;
1210 break;
1211 case BFD_RELOC_ALPHA_GPDISP_HI16:
1212 alpha_type = ALPHA_R_GPDISP;
1213 break;
1214 case BFD_RELOC_ALPHA_GPDISP_LO16:
1215 alpha_type = ALPHA_R_IGNORE;
1216 break;
1217 case BFD_RELOC_23_PCREL_S2:
1218 alpha_type = ALPHA_R_BRADDR;
1219 break;
1220 case BFD_RELOC_ALPHA_HINT:
1221 alpha_type = ALPHA_R_HINT;
1222 break;
1223 case BFD_RELOC_16_PCREL:
1224 alpha_type = ALPHA_R_SREL16;
1225 break;
1226 case BFD_RELOC_32_PCREL:
1227 alpha_type = ALPHA_R_SREL32;
1228 break;
1229 case BFD_RELOC_64_PCREL:
1230 alpha_type = ALPHA_R_SREL64;
1231 break;
1232 default:
1233 return (reloc_howto_type *) NULL;
1236 return &alpha_howto_table[alpha_type];
1239 static reloc_howto_type *
1240 alpha_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1241 const char *r_name)
1243 unsigned int i;
1245 for (i = 0;
1246 i < sizeof (alpha_howto_table) / sizeof (alpha_howto_table[0]);
1247 i++)
1248 if (alpha_howto_table[i].name != NULL
1249 && strcasecmp (alpha_howto_table[i].name, r_name) == 0)
1250 return &alpha_howto_table[i];
1252 return NULL;
1255 /* A helper routine for alpha_relocate_section which converts an
1256 external reloc when generating relocatable output. Returns the
1257 relocation amount. */
1259 static bfd_vma
1260 alpha_convert_external_reloc (bfd *output_bfd ATTRIBUTE_UNUSED,
1261 struct bfd_link_info *info,
1262 bfd *input_bfd,
1263 struct external_reloc *ext_rel,
1264 struct ecoff_link_hash_entry *h)
1266 unsigned long r_symndx;
1267 bfd_vma relocation;
1269 BFD_ASSERT (bfd_link_relocatable (info));
1271 if (h->root.type == bfd_link_hash_defined
1272 || h->root.type == bfd_link_hash_defweak)
1274 asection *hsec;
1275 const char *name;
1277 /* This symbol is defined in the output. Convert the reloc from
1278 being against the symbol to being against the section. */
1280 /* Clear the r_extern bit. */
1281 ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE;
1283 /* Compute a new r_symndx value. */
1284 hsec = h->root.u.def.section;
1285 name = bfd_section_name (hsec->output_section);
1287 r_symndx = (unsigned long) -1;
1288 switch (name[1])
1290 case 'A':
1291 if (strcmp (name, "*ABS*") == 0)
1292 r_symndx = RELOC_SECTION_ABS;
1293 break;
1294 case 'b':
1295 if (strcmp (name, ".bss") == 0)
1296 r_symndx = RELOC_SECTION_BSS;
1297 break;
1298 case 'd':
1299 if (strcmp (name, ".data") == 0)
1300 r_symndx = RELOC_SECTION_DATA;
1301 break;
1302 case 'f':
1303 if (strcmp (name, ".fini") == 0)
1304 r_symndx = RELOC_SECTION_FINI;
1305 break;
1306 case 'i':
1307 if (strcmp (name, ".init") == 0)
1308 r_symndx = RELOC_SECTION_INIT;
1309 break;
1310 case 'l':
1311 if (strcmp (name, ".lita") == 0)
1312 r_symndx = RELOC_SECTION_LITA;
1313 else if (strcmp (name, ".lit8") == 0)
1314 r_symndx = RELOC_SECTION_LIT8;
1315 else if (strcmp (name, ".lit4") == 0)
1316 r_symndx = RELOC_SECTION_LIT4;
1317 break;
1318 case 'p':
1319 if (strcmp (name, ".pdata") == 0)
1320 r_symndx = RELOC_SECTION_PDATA;
1321 break;
1322 case 'r':
1323 if (strcmp (name, ".rdata") == 0)
1324 r_symndx = RELOC_SECTION_RDATA;
1325 else if (strcmp (name, ".rconst") == 0)
1326 r_symndx = RELOC_SECTION_RCONST;
1327 break;
1328 case 's':
1329 if (strcmp (name, ".sdata") == 0)
1330 r_symndx = RELOC_SECTION_SDATA;
1331 else if (strcmp (name, ".sbss") == 0)
1332 r_symndx = RELOC_SECTION_SBSS;
1333 break;
1334 case 't':
1335 if (strcmp (name, ".text") == 0)
1336 r_symndx = RELOC_SECTION_TEXT;
1337 break;
1338 case 'x':
1339 if (strcmp (name, ".xdata") == 0)
1340 r_symndx = RELOC_SECTION_XDATA;
1341 break;
1344 if (r_symndx == (unsigned long) -1)
1345 abort ();
1347 /* Add the section VMA and the symbol value. */
1348 relocation = (h->root.u.def.value
1349 + hsec->output_section->vma
1350 + hsec->output_offset);
1352 else
1354 /* Change the symndx value to the right one for
1355 the output BFD. */
1356 r_symndx = h->indx;
1357 if (r_symndx == (unsigned long) -1)
1359 /* Caller must give an error. */
1360 r_symndx = 0;
1362 relocation = 0;
1365 /* Write out the new r_symndx value. */
1366 H_PUT_32 (input_bfd, r_symndx, ext_rel->r_symndx);
1368 return relocation;
1371 /* Relocate a section while linking an Alpha ECOFF file. This is
1372 quite similar to get_relocated_section_contents. Perhaps they
1373 could be combined somehow. */
1375 static bool
1376 alpha_relocate_section (bfd *output_bfd,
1377 struct bfd_link_info *info,
1378 bfd *input_bfd,
1379 asection *input_section,
1380 bfd_byte *contents,
1381 void * external_relocs)
1383 asection **symndx_to_section, *lita_sec;
1384 struct ecoff_link_hash_entry **sym_hashes;
1385 bfd_vma gp;
1386 bool gp_undefined;
1387 bfd_vma stack[RELOC_STACKSIZE];
1388 int tos = 0;
1389 struct external_reloc *ext_rel;
1390 struct external_reloc *ext_rel_end;
1391 bfd_size_type amt;
1392 bool ret = true;
1394 /* We keep a table mapping the symndx found in an internal reloc to
1395 the appropriate section. This is faster than looking up the
1396 section by name each time. */
1397 symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
1398 if (symndx_to_section == (asection **) NULL)
1400 amt = NUM_RELOC_SECTIONS * sizeof (asection *);
1401 symndx_to_section = (asection **) bfd_alloc (input_bfd, amt);
1402 if (!symndx_to_section)
1403 return false;
1405 symndx_to_section[RELOC_SECTION_NONE] = NULL;
1406 symndx_to_section[RELOC_SECTION_TEXT] =
1407 bfd_get_section_by_name (input_bfd, ".text");
1408 symndx_to_section[RELOC_SECTION_RDATA] =
1409 bfd_get_section_by_name (input_bfd, ".rdata");
1410 symndx_to_section[RELOC_SECTION_DATA] =
1411 bfd_get_section_by_name (input_bfd, ".data");
1412 symndx_to_section[RELOC_SECTION_SDATA] =
1413 bfd_get_section_by_name (input_bfd, ".sdata");
1414 symndx_to_section[RELOC_SECTION_SBSS] =
1415 bfd_get_section_by_name (input_bfd, ".sbss");
1416 symndx_to_section[RELOC_SECTION_BSS] =
1417 bfd_get_section_by_name (input_bfd, ".bss");
1418 symndx_to_section[RELOC_SECTION_INIT] =
1419 bfd_get_section_by_name (input_bfd, ".init");
1420 symndx_to_section[RELOC_SECTION_LIT8] =
1421 bfd_get_section_by_name (input_bfd, ".lit8");
1422 symndx_to_section[RELOC_SECTION_LIT4] =
1423 bfd_get_section_by_name (input_bfd, ".lit4");
1424 symndx_to_section[RELOC_SECTION_XDATA] =
1425 bfd_get_section_by_name (input_bfd, ".xdata");
1426 symndx_to_section[RELOC_SECTION_PDATA] =
1427 bfd_get_section_by_name (input_bfd, ".pdata");
1428 symndx_to_section[RELOC_SECTION_FINI] =
1429 bfd_get_section_by_name (input_bfd, ".fini");
1430 symndx_to_section[RELOC_SECTION_LITA] =
1431 bfd_get_section_by_name (input_bfd, ".lita");
1432 symndx_to_section[RELOC_SECTION_ABS] = bfd_abs_section_ptr;
1433 symndx_to_section[RELOC_SECTION_RCONST] =
1434 bfd_get_section_by_name (input_bfd, ".rconst");
1436 ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
1439 sym_hashes = ecoff_data (input_bfd)->sym_hashes;
1441 /* On the Alpha, the .lita section must be addressable by the global
1442 pointer. To support large programs, we need to allow multiple
1443 global pointers. This works as long as each input .lita section
1444 is <64KB big. This implies that when producing relocatable
1445 output, the .lita section is limited to 64KB. . */
1447 lita_sec = symndx_to_section[RELOC_SECTION_LITA];
1448 gp = _bfd_get_gp_value (output_bfd);
1449 if (! bfd_link_relocatable (info) && lita_sec != NULL)
1451 struct ecoff_section_tdata *lita_sec_data;
1453 /* Make sure we have a section data structure to which we can
1454 hang on to the gp value we pick for the section. */
1455 lita_sec_data = ecoff_section_data (input_bfd, lita_sec);
1456 if (lita_sec_data == NULL)
1458 amt = sizeof (struct ecoff_section_tdata);
1459 lita_sec_data = ((struct ecoff_section_tdata *)
1460 bfd_zalloc (input_bfd, amt));
1461 lita_sec->used_by_bfd = lita_sec_data;
1464 if (lita_sec_data->gp != 0)
1466 /* If we already assigned a gp to this section, we better
1467 stick with that value. */
1468 gp = lita_sec_data->gp;
1470 else
1472 bfd_vma lita_vma;
1473 bfd_size_type lita_size;
1475 lita_vma = lita_sec->output_offset + lita_sec->output_section->vma;
1476 lita_size = lita_sec->size;
1478 if (gp == 0
1479 || lita_vma < gp - 0x8000
1480 || lita_vma + lita_size >= gp + 0x8000)
1482 /* Either gp hasn't been set at all or the current gp
1483 cannot address this .lita section. In both cases we
1484 reset the gp to point into the "middle" of the
1485 current input .lita section. */
1486 if (gp && !ecoff_data (output_bfd)->issued_multiple_gp_warning)
1488 (*info->callbacks->warning) (info,
1489 _("using multiple gp values"),
1490 (char *) NULL, output_bfd,
1491 (asection *) NULL, (bfd_vma) 0);
1492 ecoff_data (output_bfd)->issued_multiple_gp_warning = true;
1494 if (lita_vma < gp - 0x8000)
1495 gp = lita_vma + lita_size - 0x8000;
1496 else
1497 gp = lita_vma + 0x8000;
1501 lita_sec_data->gp = gp;
1504 _bfd_set_gp_value (output_bfd, gp);
1507 gp_undefined = (gp == 0);
1509 BFD_ASSERT (bfd_header_little_endian (output_bfd));
1510 BFD_ASSERT (bfd_header_little_endian (input_bfd));
1512 ext_rel = (struct external_reloc *) external_relocs;
1513 ext_rel_end = ext_rel + input_section->reloc_count;
1514 for (; ext_rel < ext_rel_end; ext_rel++)
1516 bfd_vma r_vaddr;
1517 unsigned long r_symndx;
1518 int r_type;
1519 int r_extern;
1520 int r_offset;
1521 int r_size;
1522 bool relocatep;
1523 bool adjust_addrp;
1524 bool gp_usedp;
1525 bfd_vma addend;
1526 bfd_reloc_status_type r;
1528 r_vaddr = H_GET_64 (input_bfd, ext_rel->r_vaddr);
1529 r_symndx = H_GET_32 (input_bfd, ext_rel->r_symndx);
1531 r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
1532 >> RELOC_BITS0_TYPE_SH_LITTLE);
1533 r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
1534 r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
1535 >> RELOC_BITS1_OFFSET_SH_LITTLE);
1536 /* Ignored the reserved bits. */
1537 r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
1538 >> RELOC_BITS3_SIZE_SH_LITTLE);
1540 relocatep = false;
1541 adjust_addrp = true;
1542 gp_usedp = false;
1543 addend = 0;
1544 r = bfd_reloc_ok;
1546 switch (r_type)
1548 default:
1549 r = bfd_reloc_notsupported;
1550 break;
1552 case ALPHA_R_IGNORE:
1553 /* This reloc appears after a GPDISP reloc. On earlier
1554 versions of OSF/1, It marked the position of the second
1555 instruction to be altered by the GPDISP reloc, but it is
1556 not otherwise used for anything. For some reason, the
1557 address of the relocation does not appear to include the
1558 section VMA, unlike the other relocation types. */
1559 if (bfd_link_relocatable (info))
1560 H_PUT_64 (input_bfd, input_section->output_offset + r_vaddr,
1561 ext_rel->r_vaddr);
1562 adjust_addrp = false;
1563 break;
1565 case ALPHA_R_REFLONG:
1566 case ALPHA_R_REFQUAD:
1567 case ALPHA_R_HINT:
1568 relocatep = true;
1569 break;
1571 case ALPHA_R_BRADDR:
1572 case ALPHA_R_SREL16:
1573 case ALPHA_R_SREL32:
1574 case ALPHA_R_SREL64:
1575 if (r_extern)
1576 addend += - (r_vaddr + 4);
1577 relocatep = true;
1578 break;
1580 case ALPHA_R_GPREL32:
1581 /* This relocation is used in a switch table. It is a 32
1582 bit offset from the current GP value. We must adjust it
1583 by the different between the original GP value and the
1584 current GP value. */
1585 relocatep = true;
1586 addend = ecoff_data (input_bfd)->gp - gp;
1587 gp_usedp = true;
1588 break;
1590 case ALPHA_R_LITERAL:
1591 /* This is a reference to a literal value, generally
1592 (always?) in the .lita section. This is a 16 bit GP
1593 relative relocation. Sometimes the subsequent reloc is a
1594 LITUSE reloc, which indicates how this reloc is used.
1595 This sometimes permits rewriting the two instructions
1596 referred to by the LITERAL and the LITUSE into different
1597 instructions which do not refer to .lita. This can save
1598 a memory reference, and permits removing a value from
1599 .lita thus saving GP relative space.
1601 We do not these optimizations. To do them we would need
1602 to arrange to link the .lita section first, so that by
1603 the time we got here we would know the final values to
1604 use. This would not be particularly difficult, but it is
1605 not currently implemented. */
1607 relocatep = true;
1608 addend = ecoff_data (input_bfd)->gp - gp;
1609 gp_usedp = true;
1610 break;
1612 case ALPHA_R_LITUSE:
1613 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
1614 does not cause anything to happen, itself. */
1615 break;
1617 case ALPHA_R_GPDISP:
1618 /* This marks the ldah of an ldah/lda pair which loads the
1619 gp register with the difference of the gp value and the
1620 current location. The second of the pair is r_symndx
1621 bytes ahead. It used to be marked with an ALPHA_R_IGNORE
1622 reloc, but OSF/1 3.2 no longer does that. */
1623 if (r_vaddr >= input_section->vma
1624 && r_vaddr - input_section->vma < input_section->size
1625 && input_section->size - (r_vaddr - input_section->vma) > r_symndx
1626 && (input_section->size - (r_vaddr - input_section->vma)
1627 - r_symndx >= 4))
1629 /* Get the two instructions. */
1630 bfd_byte *p = contents + r_vaddr - input_section->vma;
1631 bfd_vma insn1 = bfd_get_32 (input_bfd, p);
1632 bfd_vma insn2 = bfd_get_32 (input_bfd, p + r_symndx);
1634 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
1635 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
1637 /* Get the existing addend. We must account for the sign
1638 extension done by lda and ldah. */
1639 addend = (((((insn1 & 0xffff) ^ 0x8000) - 0x8000) << 16)
1640 + (((insn2 & 0xffff) ^ 0x8000) - 0x8000));
1642 /* The existing addend includes the difference between the
1643 gp of the input BFD and the address in the input BFD.
1644 We want to change this to the difference between the
1645 final GP and the final address. */
1646 addend -= ecoff_data (input_bfd)->gp - input_section->vma;
1647 addend += gp - (input_section->output_section->vma
1648 + input_section->output_offset);
1650 /* Change the instructions, accounting for the sign
1651 extension, and write them out. */
1652 insn1 = (insn1 & ~0xffff) | (((addend + 0x8000) >> 16) & 0xffff);
1653 insn2 = (insn2 & ~0xffff) | (addend & 0xffff);
1655 bfd_put_32 (input_bfd, insn1, p);
1656 bfd_put_32 (input_bfd, insn2, p + r_symndx);
1658 gp_usedp = true;
1660 else
1661 r = bfd_reloc_outofrange;
1662 break;
1664 case ALPHA_R_OP_PUSH:
1665 case ALPHA_R_OP_PSUB:
1666 case ALPHA_R_OP_PRSHIFT:
1667 /* Manipulate values on the reloc evaluation stack. The
1668 r_vaddr field is not an address in input_section, it is
1669 the current value (including any addend) of the object
1670 being used. */
1671 if (! r_extern)
1673 asection *s;
1675 s = symndx_to_section[r_symndx];
1676 if (s == NULL)
1678 r = bfd_reloc_notsupported;
1679 break;
1681 addend = s->output_section->vma + s->output_offset - s->vma;
1683 else
1685 struct ecoff_link_hash_entry *h;
1687 h = sym_hashes[r_symndx];
1688 if (h == NULL)
1690 r = bfd_reloc_notsupported;
1691 break;
1694 if (! bfd_link_relocatable (info))
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);
1701 else
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
1706 relocated. */
1707 (*info->callbacks->undefined_symbol)
1708 (info, h->root.root.string, input_bfd,
1709 input_section, (bfd_vma) 0, true);
1710 addend = 0;
1713 else
1715 if (h->root.type != bfd_link_hash_defined
1716 && h->root.type != bfd_link_hash_defweak
1717 && h->indx == -1)
1719 /* This symbol is not being written out. Pass
1720 the address as 0, as with undefined_symbol,
1721 above. */
1722 (*info->callbacks->unattached_reloc)
1723 (info, h->root.root.string,
1724 input_bfd, input_section, (bfd_vma) 0);
1727 addend = alpha_convert_external_reloc (output_bfd, info,
1728 input_bfd,
1729 ext_rel, h);
1733 addend += r_vaddr;
1735 if (bfd_link_relocatable (info))
1737 /* Adjust r_vaddr by the addend. */
1738 H_PUT_64 (input_bfd, addend, ext_rel->r_vaddr);
1740 else
1742 switch (r_type)
1744 case ALPHA_R_OP_PUSH:
1745 if (tos >= RELOC_STACKSIZE)
1747 r = bfd_reloc_notsupported;
1748 break;
1750 stack[tos++] = addend;
1751 break;
1753 case ALPHA_R_OP_PSUB:
1754 if (tos == 0)
1756 r = bfd_reloc_notsupported;
1757 break;
1759 stack[tos - 1] -= addend;
1760 break;
1762 case ALPHA_R_OP_PRSHIFT:
1763 if (tos == 0)
1765 r = bfd_reloc_notsupported;
1766 break;
1768 stack[tos - 1] >>= addend;
1769 break;
1773 adjust_addrp = false;
1774 break;
1776 case ALPHA_R_OP_STORE:
1777 /* Store a value from the reloc stack into a bitfield. If
1778 we are generating relocatable output, all we do is
1779 adjust the address of the reloc. */
1780 if (! bfd_link_relocatable (info))
1782 unsigned int startbyte = r_offset >> 3;
1783 unsigned int endbyte = (r_offset + r_size + 7) >> 3;
1784 unsigned int bytes = endbyte + 1 - startbyte;
1786 if (bytes <= 8
1787 && r_vaddr >= input_section->vma
1788 && r_vaddr - input_section->vma < input_section->size
1789 && (input_section->size - (r_vaddr - input_section->vma)
1790 >= startbyte + bytes))
1792 bfd_byte *p = contents + (r_vaddr - input_section->vma);
1793 uint64_t val = 0;
1794 for (int off = bytes - 1; off >= 0; --off)
1795 val = (val << 8) | p[startbyte + off];
1797 r_offset -= startbyte << 3;
1798 r_size -= startbyte << 3;
1799 uint64_t mask = (((uint64_t) 1 << r_size) - 1) << r_offset;
1800 val = (val & ~mask) | ((stack[--tos] << r_offset) & mask);
1802 for (unsigned int off = 0; off < bytes; ++off)
1804 p[startbyte + off] = val & 0xff;
1805 val >>= 8;
1808 else
1809 r = bfd_reloc_outofrange;
1811 break;
1813 case ALPHA_R_GPVALUE:
1814 /* I really don't know if this does the right thing. */
1815 gp = ecoff_data (input_bfd)->gp + r_symndx;
1816 gp_undefined = false;
1817 break;
1820 if (relocatep && r == bfd_reloc_ok)
1822 reloc_howto_type *howto;
1823 struct ecoff_link_hash_entry *h = NULL;
1824 asection *s = NULL;
1825 bfd_vma relocation;
1827 /* Perform a relocation. */
1829 howto = &alpha_howto_table[r_type];
1831 if (r_extern)
1833 h = sym_hashes[r_symndx];
1834 /* If h is NULL, that means that there is a reloc
1835 against an external symbol which we thought was just
1836 a debugging symbol. This should not happen. */
1837 if (h == NULL)
1838 r = bfd_reloc_notsupported;
1840 else
1842 if (r_symndx >= NUM_RELOC_SECTIONS)
1843 s = NULL;
1844 else
1845 s = symndx_to_section[r_symndx];
1847 if (s == NULL)
1848 r = bfd_reloc_notsupported;
1852 if (r != bfd_reloc_ok)
1854 else if (bfd_link_relocatable (info))
1856 /* We are generating relocatable output, and must
1857 convert the existing reloc. */
1858 if (r_extern)
1860 if (h->root.type != bfd_link_hash_defined
1861 && h->root.type != bfd_link_hash_defweak
1862 && h->indx == -1)
1864 /* This symbol is not being written out. */
1865 (*info->callbacks->unattached_reloc)
1866 (info, h->root.root.string, input_bfd,
1867 input_section, r_vaddr - input_section->vma);
1870 relocation = alpha_convert_external_reloc (output_bfd,
1871 info,
1872 input_bfd,
1873 ext_rel,
1876 else
1878 /* This is a relocation against a section. Adjust
1879 the value by the amount the section moved. */
1880 relocation = (s->output_section->vma
1881 + s->output_offset
1882 - s->vma);
1885 /* If this is PC relative, the existing object file
1886 appears to already have the reloc worked out. We
1887 must subtract out the old value and add in the new
1888 one. */
1889 if (howto->pc_relative)
1890 relocation -= (input_section->output_section->vma
1891 + input_section->output_offset
1892 - input_section->vma);
1894 /* Put in any addend. */
1895 relocation += addend;
1897 /* Adjust the contents. */
1898 r = _bfd_relocate_contents (howto, input_bfd, relocation,
1899 (contents
1900 + r_vaddr
1901 - input_section->vma));
1903 else
1905 /* We are producing a final executable. */
1906 if (r_extern)
1908 /* This is a reloc against a symbol. */
1909 if (h->root.type == bfd_link_hash_defined
1910 || h->root.type == bfd_link_hash_defweak)
1912 asection *hsec;
1914 hsec = h->root.u.def.section;
1915 relocation = (h->root.u.def.value
1916 + hsec->output_section->vma
1917 + hsec->output_offset);
1919 else
1920 r = bfd_reloc_undefined;
1922 else
1924 /* This is a reloc against a section. */
1925 relocation = (s->output_section->vma
1926 + s->output_offset
1927 - s->vma);
1929 /* Adjust a PC relative relocation by removing the
1930 reference to the original source section. */
1931 if (howto->pc_relative)
1932 relocation += input_section->vma;
1935 if (r == bfd_reloc_ok)
1936 r = _bfd_final_link_relocate (howto,
1937 input_bfd,
1938 input_section,
1939 contents,
1940 r_vaddr - input_section->vma,
1941 relocation,
1942 addend);
1946 if (bfd_link_relocatable (info) && adjust_addrp)
1948 /* Change the address of the relocation. */
1949 H_PUT_64 (input_bfd,
1950 (input_section->output_section->vma
1951 + input_section->output_offset
1952 - input_section->vma
1953 + r_vaddr),
1954 ext_rel->r_vaddr);
1957 if (gp_usedp && gp_undefined)
1959 r = bfd_reloc_dangerous;
1960 /* Only give the error once per link. */
1961 gp = 4;
1962 _bfd_set_gp_value (output_bfd, gp);
1963 gp_undefined = false;
1966 if (r != bfd_reloc_ok)
1968 switch (r)
1970 case bfd_reloc_overflow:
1972 const char *name;
1974 if (r_extern)
1975 name = sym_hashes[r_symndx]->root.root.string;
1976 else
1977 name = bfd_section_name (symndx_to_section[r_symndx]);
1978 (*info->callbacks->reloc_overflow)
1979 (info, NULL, name, alpha_howto_table[r_type].name,
1980 (bfd_vma) 0, input_bfd, input_section,
1981 r_vaddr - input_section->vma);
1983 break;
1984 case bfd_reloc_outofrange:
1985 (*info->callbacks->einfo)
1986 /* xgettext:c-format */
1987 (_("%X%P: %pB(%pA): relocation out of range\n"),
1988 input_bfd, input_section);
1989 break;
1990 case bfd_reloc_undefined:
1991 (*info->callbacks->undefined_symbol)
1992 (info, sym_hashes[r_symndx]->root.root.string,
1993 input_bfd, input_section,
1994 r_vaddr - input_section->vma, true);
1995 break;
1996 case bfd_reloc_notsupported:
1997 (*info->callbacks->einfo)
1998 /* xgettext:c-format */
1999 (_("%X%P: %pB(%pA): relocation is not supported\n"),
2000 input_bfd, input_section);
2001 break;
2002 case bfd_reloc_dangerous:
2003 (*info->callbacks->reloc_dangerous)
2004 (info, _("GP relative relocation used when GP not defined"),
2005 input_bfd, input_section, r_vaddr - input_section->vma);
2006 break;
2007 default:
2008 abort ();
2010 ret = false;
2014 if (tos != 0)
2015 ret = false;
2017 return ret;
2020 /* Do final adjustments to the filehdr and the aouthdr. This routine
2021 sets the dynamic bits in the file header. */
2023 static bool
2024 alpha_adjust_headers (bfd *abfd,
2025 struct internal_filehdr *fhdr,
2026 struct internal_aouthdr *ahdr ATTRIBUTE_UNUSED)
2028 if ((abfd->flags & (DYNAMIC | EXEC_P)) == (DYNAMIC | EXEC_P))
2029 fhdr->f_flags |= F_ALPHA_CALL_SHARED;
2030 else if ((abfd->flags & DYNAMIC) != 0)
2031 fhdr->f_flags |= F_ALPHA_SHARABLE;
2032 return true;
2035 /* Archive handling. In OSF/1 (or Digital Unix) v3.2, Digital
2036 introduced archive packing, in which the elements in an archive are
2037 optionally compressed using a simple dictionary scheme. We know
2038 how to read such archives, but we don't write them. */
2040 #define alpha_ecoff_slurp_armap _bfd_ecoff_slurp_armap
2041 #define alpha_ecoff_slurp_extended_name_table \
2042 _bfd_ecoff_slurp_extended_name_table
2043 #define alpha_ecoff_construct_extended_name_table \
2044 _bfd_ecoff_construct_extended_name_table
2045 #define alpha_ecoff_truncate_arname _bfd_ecoff_truncate_arname
2046 #define alpha_ecoff_write_armap _bfd_ecoff_write_armap
2047 #define alpha_ecoff_write_ar_hdr _bfd_generic_write_ar_hdr
2048 #define alpha_ecoff_generic_stat_arch_elt _bfd_ecoff_generic_stat_arch_elt
2049 #define alpha_ecoff_update_armap_timestamp _bfd_ecoff_update_armap_timestamp
2051 /* A compressed file uses this instead of ARFMAG. */
2053 #define ARFZMAG "Z\012"
2055 /* Read an archive header. This is like the standard routine, but it
2056 also accepts ARFZMAG. */
2058 static void *
2059 alpha_ecoff_read_ar_hdr (bfd *abfd)
2061 struct areltdata *ret;
2062 struct ar_hdr *h;
2064 ret = (struct areltdata *) _bfd_generic_read_ar_hdr_mag (abfd, ARFZMAG);
2065 if (ret == NULL)
2066 return NULL;
2068 h = (struct ar_hdr *) ret->arch_header;
2069 if (strncmp (h->ar_fmag, ARFZMAG, 2) == 0)
2071 bfd_byte ab[8];
2073 /* This is a compressed file. We must set the size correctly.
2074 The size is the eight bytes after the dummy file header. */
2075 if (bfd_seek (abfd, FILHSZ, SEEK_CUR) != 0
2076 || bfd_read (ab, 8, abfd) != 8
2077 || bfd_seek (abfd, -(FILHSZ + 8), SEEK_CUR) != 0)
2079 free (ret);
2080 return NULL;
2083 ret->parsed_size = H_GET_64 (abfd, ab);
2086 return ret;
2089 /* Get an archive element at a specified file position. This is where
2090 we uncompress the archive element if necessary. */
2092 static bfd *
2093 alpha_ecoff_get_elt_at_filepos (bfd *archive, file_ptr filepos,
2094 struct bfd_link_info *info)
2096 bfd *nbfd = NULL;
2097 struct areltdata *tdata;
2098 struct ar_hdr *hdr;
2099 bfd_byte ab[8];
2100 bfd_size_type size;
2101 bfd_byte *buf, *p;
2102 struct bfd_in_memory *bim;
2103 ufile_ptr filesize;
2105 buf = NULL;
2106 nbfd = _bfd_get_elt_at_filepos (archive, filepos, info);
2107 if (nbfd == NULL)
2108 goto error_return;
2110 if ((nbfd->flags & BFD_IN_MEMORY) != 0)
2112 /* We have already expanded this BFD. */
2113 return nbfd;
2116 tdata = (struct areltdata *) nbfd->arelt_data;
2117 hdr = (struct ar_hdr *) tdata->arch_header;
2118 if (strncmp (hdr->ar_fmag, ARFZMAG, 2) != 0)
2119 return nbfd;
2121 /* We must uncompress this element. We do this by copying it into a
2122 memory buffer, and making bfd_read and bfd_seek use that buffer.
2123 This can use a lot of memory, but it's simpler than getting a
2124 temporary file, making that work with the file descriptor caching
2125 code, and making sure that it is deleted at all appropriate
2126 times. It can be changed if it ever becomes important. */
2128 /* The compressed file starts with a dummy ECOFF file header. */
2129 if (bfd_seek (nbfd, FILHSZ, SEEK_SET) != 0)
2130 goto error_return;
2132 /* The next eight bytes are the real file size. */
2133 if (bfd_read (ab, 8, nbfd) != 8)
2134 goto error_return;
2135 size = H_GET_64 (nbfd, ab);
2137 /* The decompression algorithm will at most expand by eight times. */
2138 filesize = bfd_get_file_size (archive);
2139 if (filesize != 0 && size / 8 > filesize)
2141 bfd_set_error (bfd_error_malformed_archive);
2142 goto error_return;
2145 if (size != 0)
2147 bfd_size_type left;
2148 bfd_byte dict[4096];
2149 unsigned int h;
2150 bfd_byte b;
2152 buf = (bfd_byte *) bfd_malloc (size);
2153 if (buf == NULL)
2154 goto error_return;
2155 p = buf;
2157 left = size;
2159 /* I don't know what the next eight bytes are for. */
2160 if (bfd_read (ab, 8, nbfd) != 8)
2161 goto error_return;
2163 /* This is the uncompression algorithm. It's a simple
2164 dictionary based scheme in which each character is predicted
2165 by a hash of the previous three characters. A control byte
2166 indicates whether the character is predicted or whether it
2167 appears in the input stream; each control byte manages the
2168 next eight bytes in the output stream. */
2169 memset (dict, 0, sizeof dict);
2170 h = 0;
2171 while (bfd_read (&b, 1, nbfd) == 1)
2173 unsigned int i;
2175 for (i = 0; i < 8; i++, b >>= 1)
2177 bfd_byte n;
2179 if ((b & 1) == 0)
2180 n = dict[h];
2181 else
2183 if (bfd_read (&n, 1, nbfd) != 1)
2184 goto error_return;
2185 dict[h] = n;
2188 *p++ = n;
2190 --left;
2191 if (left == 0)
2192 break;
2194 h <<= 4;
2195 h ^= n;
2196 h &= sizeof dict - 1;
2199 if (left == 0)
2200 break;
2204 /* Now the uncompressed file contents are in buf. */
2205 bim = ((struct bfd_in_memory *)
2206 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
2207 if (bim == NULL)
2208 goto error_return;
2209 bim->size = size;
2210 bim->buffer = buf;
2212 nbfd->mtime_set = true;
2213 nbfd->mtime = strtol (hdr->ar_date, (char **) NULL, 10);
2215 nbfd->flags |= BFD_IN_MEMORY;
2216 nbfd->iostream = bim;
2217 nbfd->iovec = &_bfd_memory_iovec;
2218 nbfd->origin = 0;
2219 nbfd->size = 0;
2220 BFD_ASSERT (! nbfd->cacheable);
2222 return nbfd;
2224 error_return:
2225 free (buf);
2226 if (nbfd != NULL)
2227 bfd_close (nbfd);
2228 return NULL;
2231 /* Open the next archived file. */
2233 static bfd *
2234 alpha_ecoff_openr_next_archived_file (bfd *archive, bfd *last_file)
2236 ufile_ptr filestart;
2238 if (last_file == NULL)
2239 filestart = bfd_ardata (archive)->first_file_filepos;
2240 else
2242 struct areltdata *t;
2243 struct ar_hdr *h;
2244 bfd_size_type size;
2246 /* We can't use arelt_size here, because that uses parsed_size,
2247 which is the uncompressed size. We need the compressed size. */
2248 t = (struct areltdata *) last_file->arelt_data;
2249 h = (struct ar_hdr *) t->arch_header;
2250 size = strtol (h->ar_size, (char **) NULL, 10);
2252 /* Pad to an even boundary...
2253 Note that last_file->origin can be odd in the case of
2254 BSD-4.4-style element with a long odd size. */
2255 filestart = last_file->proxy_origin + size;
2256 filestart += filestart % 2;
2257 if (filestart < last_file->proxy_origin)
2259 /* Prevent looping. See PR19256. */
2260 bfd_set_error (bfd_error_malformed_archive);
2261 return NULL;
2265 return alpha_ecoff_get_elt_at_filepos (archive, filestart, NULL);
2268 /* Open the archive file given an index into the armap. */
2270 static bfd *
2271 alpha_ecoff_get_elt_at_index (bfd *abfd, symindex sym_index)
2273 carsym *entry;
2275 entry = bfd_ardata (abfd)->symdefs + sym_index;
2276 return alpha_ecoff_get_elt_at_filepos (abfd, entry->file_offset,
2277 NULL);
2280 static void
2281 alpha_ecoff_swap_coff_aux_in (bfd *abfd ATTRIBUTE_UNUSED,
2282 void *ext1 ATTRIBUTE_UNUSED,
2283 int type ATTRIBUTE_UNUSED,
2284 int in_class ATTRIBUTE_UNUSED,
2285 int indx ATTRIBUTE_UNUSED,
2286 int numaux ATTRIBUTE_UNUSED,
2287 void *in1 ATTRIBUTE_UNUSED)
2291 static void
2292 alpha_ecoff_swap_coff_sym_in (bfd *abfd ATTRIBUTE_UNUSED,
2293 void *ext1 ATTRIBUTE_UNUSED,
2294 void *in1 ATTRIBUTE_UNUSED)
2298 static void
2299 alpha_ecoff_swap_coff_lineno_in (bfd *abfd ATTRIBUTE_UNUSED,
2300 void *ext1 ATTRIBUTE_UNUSED,
2301 void *in1 ATTRIBUTE_UNUSED)
2305 static unsigned int
2306 alpha_ecoff_swap_coff_aux_out (bfd *abfd ATTRIBUTE_UNUSED,
2307 void *inp ATTRIBUTE_UNUSED,
2308 int type ATTRIBUTE_UNUSED,
2309 int in_class ATTRIBUTE_UNUSED,
2310 int indx ATTRIBUTE_UNUSED,
2311 int numaux ATTRIBUTE_UNUSED,
2312 void *extp ATTRIBUTE_UNUSED)
2314 return 0;
2317 static unsigned int
2318 alpha_ecoff_swap_coff_sym_out (bfd *abfd ATTRIBUTE_UNUSED,
2319 void *inp ATTRIBUTE_UNUSED,
2320 void *extp ATTRIBUTE_UNUSED)
2322 return 0;
2325 static unsigned int
2326 alpha_ecoff_swap_coff_lineno_out (bfd *abfd ATTRIBUTE_UNUSED,
2327 void *inp ATTRIBUTE_UNUSED,
2328 void *extp ATTRIBUTE_UNUSED)
2330 return 0;
2333 static unsigned int
2334 alpha_ecoff_swap_coff_reloc_out (bfd *abfd ATTRIBUTE_UNUSED,
2335 void *inp ATTRIBUTE_UNUSED,
2336 void *extp ATTRIBUTE_UNUSED)
2338 return 0;
2341 /* This is the ECOFF backend structure. The backend field of the
2342 target vector points to this. */
2344 static const struct ecoff_backend_data alpha_ecoff_backend_data =
2346 /* COFF backend structure. */
2348 alpha_ecoff_swap_coff_aux_in, alpha_ecoff_swap_coff_sym_in,
2349 alpha_ecoff_swap_coff_lineno_in, alpha_ecoff_swap_coff_aux_out,
2350 alpha_ecoff_swap_coff_sym_out, alpha_ecoff_swap_coff_lineno_out,
2351 alpha_ecoff_swap_coff_reloc_out,
2352 alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out,
2353 alpha_ecoff_swap_scnhdr_out,
2354 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, true,
2355 ECOFF_NO_LONG_SECTION_NAMES, 4, false, 2, 32768,
2356 alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in,
2357 alpha_ecoff_swap_scnhdr_in, NULL,
2358 alpha_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
2359 alpha_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
2360 _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
2361 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2362 NULL, NULL, NULL, NULL
2364 /* Supported architecture. */
2365 bfd_arch_alpha,
2366 /* Initial portion of armap string. */
2367 "________64",
2368 /* The page boundary used to align sections in a demand-paged
2369 executable file. E.g., 0x1000. */
2370 0x2000,
2371 /* TRUE if the .rdata section is part of the text segment, as on the
2372 Alpha. FALSE if .rdata is part of the data segment, as on the
2373 MIPS. */
2374 true,
2375 /* Bitsize of constructor entries. */
2377 /* Reloc to use for constructor entries. */
2378 &alpha_howto_table[ALPHA_R_REFQUAD],
2380 /* Symbol table magic number. */
2381 magicSym2,
2382 /* Alignment of debugging information. E.g., 4. */
2384 /* Sizes of external symbolic information. */
2385 sizeof (struct hdr_ext),
2386 sizeof (struct dnr_ext),
2387 sizeof (struct pdr_ext),
2388 sizeof (struct sym_ext),
2389 sizeof (struct opt_ext),
2390 sizeof (struct fdr_ext),
2391 sizeof (struct rfd_ext),
2392 sizeof (struct ext_ext),
2393 /* Functions to swap in external symbolic data. */
2394 ecoff_swap_hdr_in,
2395 ecoff_swap_dnr_in,
2396 ecoff_swap_pdr_in,
2397 ecoff_swap_sym_in,
2398 ecoff_swap_opt_in,
2399 ecoff_swap_fdr_in,
2400 ecoff_swap_rfd_in,
2401 ecoff_swap_ext_in,
2402 _bfd_ecoff_swap_tir_in,
2403 _bfd_ecoff_swap_rndx_in,
2404 /* Functions to swap out external symbolic data. */
2405 ecoff_swap_hdr_out,
2406 ecoff_swap_dnr_out,
2407 ecoff_swap_pdr_out,
2408 ecoff_swap_sym_out,
2409 ecoff_swap_opt_out,
2410 ecoff_swap_fdr_out,
2411 ecoff_swap_rfd_out,
2412 ecoff_swap_ext_out,
2413 _bfd_ecoff_swap_tir_out,
2414 _bfd_ecoff_swap_rndx_out,
2415 /* Function to read in symbolic data. */
2416 _bfd_ecoff_slurp_symbolic_info
2418 /* External reloc size. */
2419 RELSZ,
2420 /* Reloc swapping functions. */
2421 alpha_ecoff_swap_reloc_in,
2422 alpha_ecoff_swap_reloc_out,
2423 /* Backend reloc tweaking. */
2424 alpha_adjust_reloc_in,
2425 alpha_adjust_reloc_out,
2426 /* Relocate section contents while linking. */
2427 alpha_relocate_section,
2428 /* Do final adjustments to filehdr and aouthdr. */
2429 alpha_adjust_headers,
2430 /* Read an element from an archive at a given file position. */
2431 alpha_ecoff_get_elt_at_filepos
2434 /* Looking up a reloc type is Alpha specific. */
2435 #define _bfd_ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup
2436 #define _bfd_ecoff_bfd_reloc_name_lookup \
2437 alpha_bfd_reloc_name_lookup
2439 /* So is getting relocated section contents. */
2440 #define _bfd_ecoff_bfd_get_relocated_section_contents \
2441 alpha_ecoff_get_relocated_section_contents
2443 /* Input section flag lookup is generic. */
2444 #define _bfd_ecoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
2446 /* Relaxing sections is generic. */
2447 #define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section
2448 #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
2449 #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections
2450 #define _bfd_ecoff_bfd_is_group_section bfd_generic_is_group_section
2451 #define _bfd_ecoff_bfd_group_name bfd_generic_group_name
2452 #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group
2453 #define _bfd_ecoff_section_already_linked \
2454 _bfd_coff_section_already_linked
2455 #define _bfd_ecoff_bfd_define_common_symbol bfd_generic_define_common_symbol
2456 #define _bfd_ecoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
2457 #define _bfd_ecoff_bfd_define_start_stop bfd_generic_define_start_stop
2458 #define _bfd_ecoff_bfd_link_check_relocs _bfd_generic_link_check_relocs
2460 /* Installing internal relocations in a section is also generic. */
2461 #define _bfd_ecoff_set_reloc _bfd_generic_set_reloc
2463 const bfd_target alpha_ecoff_le_vec =
2465 "ecoff-littlealpha", /* name */
2466 bfd_target_ecoff_flavour,
2467 BFD_ENDIAN_LITTLE, /* data byte order is little */
2468 BFD_ENDIAN_LITTLE, /* header byte order is little */
2470 (HAS_RELOC | EXEC_P /* object flags */
2471 | HAS_LINENO | HAS_DEBUG
2472 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
2474 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE
2475 | SEC_DATA | SEC_SMALL_DATA),
2476 0, /* leading underscore */
2477 ' ', /* ar_pad_char */
2478 15, /* ar_max_namelen */
2479 0, /* match priority. */
2480 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
2481 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2482 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2483 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2484 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2485 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2486 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2488 { /* bfd_check_format */
2489 _bfd_dummy_target,
2490 alpha_ecoff_object_p,
2491 bfd_generic_archive_p,
2492 _bfd_dummy_target
2494 { /* bfd_set_format */
2495 _bfd_bool_bfd_false_error,
2496 _bfd_ecoff_mkobject,
2497 _bfd_generic_mkarchive,
2498 _bfd_bool_bfd_false_error
2500 { /* bfd_write_contents */
2501 _bfd_bool_bfd_false_error,
2502 _bfd_ecoff_write_object_contents,
2503 _bfd_write_archive_contents,
2504 _bfd_bool_bfd_false_error
2507 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2508 BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2509 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2510 BFD_JUMP_TABLE_ARCHIVE (alpha_ecoff),
2511 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2512 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2513 BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2514 BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2515 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2517 NULL,
2519 &alpha_ecoff_backend_data