Automatic date update in version.in
[binutils-gdb.git] / bfd / coff-alpha.c
blob15fea1fde34ff7df9d9380ac73fd6118b64bb7fd
1 /* BFD back-end for ALPHA Extended-Coff files.
2 Copyright (C) 1993-2021 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 0, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 4, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 1, /* size (0 = byte, 1 = short, 2 = long) */
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 2, /* size (0 = byte, 1 = short, 2 = long) */
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 4, /* size (0 = byte, 1 = short, 2 = long) */
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 (0 = byte, 1 = short, 2 = long) */
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 4, /* size (0 = byte, 1 = short, 2 = long) */
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 (0 = byte, 1 = short, 2 = long) */
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 (0 = byte, 1 = short, 2 = long) */
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 (0 = byte, 1 = short, 2 = long) */
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 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
749 return NULL;
751 if (data == NULL)
752 return NULL;
754 if (reloc_size == 0)
755 return data;
757 reloc_vector = (arelent **) bfd_malloc (reloc_size);
758 if (reloc_vector == NULL)
759 return NULL;
761 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
762 reloc_vector, symbols);
763 if (reloc_count < 0)
764 goto error_return;
765 if (reloc_count == 0)
766 goto successful_return;
768 /* Get the GP value for the output BFD. */
769 gp_undefined = false;
770 gp = _bfd_get_gp_value (abfd);
771 if (gp == 0)
773 if (relocatable)
775 asection *sec;
776 bfd_vma lo;
778 /* Make up a value. */
779 lo = (bfd_vma) -1;
780 for (sec = abfd->sections; sec != NULL; sec = sec->next)
782 if (sec->vma < lo
783 && (strcmp (sec->name, ".sbss") == 0
784 || strcmp (sec->name, ".sdata") == 0
785 || strcmp (sec->name, ".lit4") == 0
786 || strcmp (sec->name, ".lit8") == 0
787 || strcmp (sec->name, ".lita") == 0))
788 lo = sec->vma;
790 gp = lo + 0x8000;
791 _bfd_set_gp_value (abfd, gp);
793 else
795 struct bfd_link_hash_entry *h;
797 h = bfd_link_hash_lookup (link_info->hash, "_gp", false, false,
798 true);
799 if (h == (struct bfd_link_hash_entry *) NULL
800 || h->type != bfd_link_hash_defined)
801 gp_undefined = true;
802 else
804 gp = (h->u.def.value
805 + h->u.def.section->output_section->vma
806 + h->u.def.section->output_offset);
807 _bfd_set_gp_value (abfd, gp);
812 for (; *reloc_vector != (arelent *) NULL; reloc_vector++)
814 arelent *rel;
815 bfd_reloc_status_type r;
816 char *err;
818 rel = *reloc_vector;
819 r = bfd_reloc_ok;
820 switch (rel->howto->type)
822 case ALPHA_R_IGNORE:
823 rel->address += input_section->output_offset;
824 break;
826 case ALPHA_R_REFLONG:
827 case ALPHA_R_REFQUAD:
828 case ALPHA_R_BRADDR:
829 case ALPHA_R_HINT:
830 case ALPHA_R_SREL16:
831 case ALPHA_R_SREL32:
832 case ALPHA_R_SREL64:
833 if (relocatable
834 && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
836 rel->address += input_section->output_offset;
837 break;
839 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
840 output_bfd, &err);
841 break;
843 case ALPHA_R_GPREL32:
844 /* This relocation is used in a switch table. It is a 32
845 bit offset from the current GP value. We must adjust it
846 by the different between the original GP value and the
847 current GP value. The original GP value is stored in the
848 addend. We adjust the addend and let
849 bfd_perform_relocation finish the job. */
850 rel->addend -= gp;
851 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
852 output_bfd, &err);
853 if (r == bfd_reloc_ok && gp_undefined)
855 r = bfd_reloc_dangerous;
856 err = (char *) _("GP relative relocation used when GP not defined");
858 break;
860 case ALPHA_R_LITERAL:
861 /* This is a reference to a literal value, generally
862 (always?) in the .lita section. This is a 16 bit GP
863 relative relocation. Sometimes the subsequent reloc is a
864 LITUSE reloc, which indicates how this reloc is used.
865 This sometimes permits rewriting the two instructions
866 referred to by the LITERAL and the LITUSE into different
867 instructions which do not refer to .lita. This can save
868 a memory reference, and permits removing a value from
869 .lita thus saving GP relative space.
871 We do not these optimizations. To do them we would need
872 to arrange to link the .lita section first, so that by
873 the time we got here we would know the final values to
874 use. This would not be particularly difficult, but it is
875 not currently implemented. */
878 unsigned long insn;
880 /* I believe that the LITERAL reloc will only apply to a
881 ldq or ldl instruction, so check my assumption. */
882 insn = bfd_get_32 (input_bfd, data + rel->address);
883 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
884 || ((insn >> 26) & 0x3f) == 0x28);
886 rel->addend -= gp;
887 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
888 output_bfd, &err);
889 if (r == bfd_reloc_ok && gp_undefined)
891 r = bfd_reloc_dangerous;
892 err =
893 (char *) _("GP relative relocation used when GP not defined");
896 break;
898 case ALPHA_R_LITUSE:
899 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
900 does not cause anything to happen, itself. */
901 rel->address += input_section->output_offset;
902 break;
904 case ALPHA_R_GPDISP:
905 /* This marks the ldah of an ldah/lda pair which loads the
906 gp register with the difference of the gp value and the
907 current location. The second of the pair is r_size bytes
908 ahead; it used to be marked with an ALPHA_R_IGNORE reloc,
909 but that no longer happens in OSF/1 3.2. */
911 unsigned long insn1, insn2;
912 bfd_vma addend;
914 /* Get the two instructions. */
915 insn1 = bfd_get_32 (input_bfd, data + rel->address);
916 insn2 = bfd_get_32 (input_bfd, data + rel->address + rel->addend);
918 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
919 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
921 /* Get the existing addend. We must account for the sign
922 extension done by lda and ldah. */
923 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
924 if (insn1 & 0x8000)
926 addend -= 0x80000000;
927 addend -= 0x80000000;
929 if (insn2 & 0x8000)
930 addend -= 0x10000;
932 /* The existing addend includes the different between the
933 gp of the input BFD and the address in the input BFD.
934 Subtract this out. */
935 addend -= (ecoff_data (input_bfd)->gp
936 - (input_section->vma + rel->address));
938 /* Now add in the final gp value, and subtract out the
939 final address. */
940 addend += (gp
941 - (input_section->output_section->vma
942 + input_section->output_offset
943 + rel->address));
945 /* Change the instructions, accounting for the sign
946 extension, and write them out. */
947 if (addend & 0x8000)
948 addend += 0x10000;
949 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
950 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
952 bfd_put_32 (input_bfd, (bfd_vma) insn1, data + rel->address);
953 bfd_put_32 (input_bfd, (bfd_vma) insn2,
954 data + rel->address + rel->addend);
956 rel->address += input_section->output_offset;
958 break;
960 case ALPHA_R_OP_PUSH:
961 /* Push a value on the reloc evaluation stack. */
963 asymbol *symbol;
964 bfd_vma relocation;
966 if (relocatable)
968 rel->address += input_section->output_offset;
969 break;
972 /* Figure out the relocation of this symbol. */
973 symbol = *rel->sym_ptr_ptr;
975 if (bfd_is_und_section (symbol->section))
976 r = bfd_reloc_undefined;
978 if (bfd_is_com_section (symbol->section))
979 relocation = 0;
980 else
981 relocation = symbol->value;
982 relocation += symbol->section->output_section->vma;
983 relocation += symbol->section->output_offset;
984 relocation += rel->addend;
986 if (tos >= RELOC_STACKSIZE)
987 abort ();
989 stack[tos++] = relocation;
991 break;
993 case ALPHA_R_OP_STORE:
994 /* Store a value from the reloc stack into a bitfield. */
996 bfd_vma val;
997 int offset, size;
999 if (relocatable)
1001 rel->address += input_section->output_offset;
1002 break;
1005 if (tos == 0)
1006 abort ();
1008 /* The offset and size for this reloc are encoded into the
1009 addend field by alpha_adjust_reloc_in. */
1010 offset = (rel->addend >> 8) & 0xff;
1011 size = rel->addend & 0xff;
1013 val = bfd_get_64 (abfd, data + rel->address);
1014 val &=~ (((1 << size) - 1) << offset);
1015 val |= (stack[--tos] & ((1 << size) - 1)) << offset;
1016 bfd_put_64 (abfd, val, data + rel->address);
1018 break;
1020 case ALPHA_R_OP_PSUB:
1021 /* Subtract a value from the top of the stack. */
1023 asymbol *symbol;
1024 bfd_vma relocation;
1026 if (relocatable)
1028 rel->address += input_section->output_offset;
1029 break;
1032 /* Figure out the relocation of this symbol. */
1033 symbol = *rel->sym_ptr_ptr;
1035 if (bfd_is_und_section (symbol->section))
1036 r = bfd_reloc_undefined;
1038 if (bfd_is_com_section (symbol->section))
1039 relocation = 0;
1040 else
1041 relocation = symbol->value;
1042 relocation += symbol->section->output_section->vma;
1043 relocation += symbol->section->output_offset;
1044 relocation += rel->addend;
1046 if (tos == 0)
1047 abort ();
1049 stack[tos - 1] -= relocation;
1051 break;
1053 case ALPHA_R_OP_PRSHIFT:
1054 /* Shift the value on the top of the stack. */
1056 asymbol *symbol;
1057 bfd_vma relocation;
1059 if (relocatable)
1061 rel->address += input_section->output_offset;
1062 break;
1065 /* Figure out the relocation of this symbol. */
1066 symbol = *rel->sym_ptr_ptr;
1068 if (bfd_is_und_section (symbol->section))
1069 r = bfd_reloc_undefined;
1071 if (bfd_is_com_section (symbol->section))
1072 relocation = 0;
1073 else
1074 relocation = symbol->value;
1075 relocation += symbol->section->output_section->vma;
1076 relocation += symbol->section->output_offset;
1077 relocation += rel->addend;
1079 if (tos == 0)
1080 abort ();
1082 stack[tos - 1] >>= relocation;
1084 break;
1086 case ALPHA_R_GPVALUE:
1087 /* I really don't know if this does the right thing. */
1088 gp = rel->addend;
1089 gp_undefined = false;
1090 break;
1092 default:
1093 abort ();
1096 if (relocatable)
1098 asection *os = input_section->output_section;
1100 /* A partial link, so keep the relocs. */
1101 os->orelocation[os->reloc_count] = rel;
1102 os->reloc_count++;
1105 if (r != bfd_reloc_ok)
1107 switch (r)
1109 case bfd_reloc_undefined:
1110 (*link_info->callbacks->undefined_symbol)
1111 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1112 input_bfd, input_section, rel->address, true);
1113 break;
1114 case bfd_reloc_dangerous:
1115 (*link_info->callbacks->reloc_dangerous)
1116 (link_info, err, input_bfd, input_section, rel->address);
1117 break;
1118 case bfd_reloc_overflow:
1119 (*link_info->callbacks->reloc_overflow)
1120 (link_info, NULL, bfd_asymbol_name (*rel->sym_ptr_ptr),
1121 rel->howto->name, rel->addend, input_bfd,
1122 input_section, rel->address);
1123 break;
1124 case bfd_reloc_outofrange:
1125 default:
1126 abort ();
1127 break;
1132 if (tos != 0)
1133 abort ();
1135 successful_return:
1136 free (reloc_vector);
1137 return data;
1139 error_return:
1140 free (reloc_vector);
1141 return NULL;
1144 /* Get the howto structure for a generic reloc type. */
1146 static reloc_howto_type *
1147 alpha_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1148 bfd_reloc_code_real_type code)
1150 int alpha_type;
1152 switch (code)
1154 case BFD_RELOC_32:
1155 alpha_type = ALPHA_R_REFLONG;
1156 break;
1157 case BFD_RELOC_64:
1158 case BFD_RELOC_CTOR:
1159 alpha_type = ALPHA_R_REFQUAD;
1160 break;
1161 case BFD_RELOC_GPREL32:
1162 alpha_type = ALPHA_R_GPREL32;
1163 break;
1164 case BFD_RELOC_ALPHA_LITERAL:
1165 alpha_type = ALPHA_R_LITERAL;
1166 break;
1167 case BFD_RELOC_ALPHA_LITUSE:
1168 alpha_type = ALPHA_R_LITUSE;
1169 break;
1170 case BFD_RELOC_ALPHA_GPDISP_HI16:
1171 alpha_type = ALPHA_R_GPDISP;
1172 break;
1173 case BFD_RELOC_ALPHA_GPDISP_LO16:
1174 alpha_type = ALPHA_R_IGNORE;
1175 break;
1176 case BFD_RELOC_23_PCREL_S2:
1177 alpha_type = ALPHA_R_BRADDR;
1178 break;
1179 case BFD_RELOC_ALPHA_HINT:
1180 alpha_type = ALPHA_R_HINT;
1181 break;
1182 case BFD_RELOC_16_PCREL:
1183 alpha_type = ALPHA_R_SREL16;
1184 break;
1185 case BFD_RELOC_32_PCREL:
1186 alpha_type = ALPHA_R_SREL32;
1187 break;
1188 case BFD_RELOC_64_PCREL:
1189 alpha_type = ALPHA_R_SREL64;
1190 break;
1191 default:
1192 return (reloc_howto_type *) NULL;
1195 return &alpha_howto_table[alpha_type];
1198 static reloc_howto_type *
1199 alpha_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1200 const char *r_name)
1202 unsigned int i;
1204 for (i = 0;
1205 i < sizeof (alpha_howto_table) / sizeof (alpha_howto_table[0]);
1206 i++)
1207 if (alpha_howto_table[i].name != NULL
1208 && strcasecmp (alpha_howto_table[i].name, r_name) == 0)
1209 return &alpha_howto_table[i];
1211 return NULL;
1214 /* A helper routine for alpha_relocate_section which converts an
1215 external reloc when generating relocatable output. Returns the
1216 relocation amount. */
1218 static bfd_vma
1219 alpha_convert_external_reloc (bfd *output_bfd ATTRIBUTE_UNUSED,
1220 struct bfd_link_info *info,
1221 bfd *input_bfd,
1222 struct external_reloc *ext_rel,
1223 struct ecoff_link_hash_entry *h)
1225 unsigned long r_symndx;
1226 bfd_vma relocation;
1228 BFD_ASSERT (bfd_link_relocatable (info));
1230 if (h->root.type == bfd_link_hash_defined
1231 || h->root.type == bfd_link_hash_defweak)
1233 asection *hsec;
1234 const char *name;
1236 /* This symbol is defined in the output. Convert the reloc from
1237 being against the symbol to being against the section. */
1239 /* Clear the r_extern bit. */
1240 ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE;
1242 /* Compute a new r_symndx value. */
1243 hsec = h->root.u.def.section;
1244 name = bfd_section_name (hsec->output_section);
1246 r_symndx = (unsigned long) -1;
1247 switch (name[1])
1249 case 'A':
1250 if (strcmp (name, "*ABS*") == 0)
1251 r_symndx = RELOC_SECTION_ABS;
1252 break;
1253 case 'b':
1254 if (strcmp (name, ".bss") == 0)
1255 r_symndx = RELOC_SECTION_BSS;
1256 break;
1257 case 'd':
1258 if (strcmp (name, ".data") == 0)
1259 r_symndx = RELOC_SECTION_DATA;
1260 break;
1261 case 'f':
1262 if (strcmp (name, ".fini") == 0)
1263 r_symndx = RELOC_SECTION_FINI;
1264 break;
1265 case 'i':
1266 if (strcmp (name, ".init") == 0)
1267 r_symndx = RELOC_SECTION_INIT;
1268 break;
1269 case 'l':
1270 if (strcmp (name, ".lita") == 0)
1271 r_symndx = RELOC_SECTION_LITA;
1272 else if (strcmp (name, ".lit8") == 0)
1273 r_symndx = RELOC_SECTION_LIT8;
1274 else if (strcmp (name, ".lit4") == 0)
1275 r_symndx = RELOC_SECTION_LIT4;
1276 break;
1277 case 'p':
1278 if (strcmp (name, ".pdata") == 0)
1279 r_symndx = RELOC_SECTION_PDATA;
1280 break;
1281 case 'r':
1282 if (strcmp (name, ".rdata") == 0)
1283 r_symndx = RELOC_SECTION_RDATA;
1284 else if (strcmp (name, ".rconst") == 0)
1285 r_symndx = RELOC_SECTION_RCONST;
1286 break;
1287 case 's':
1288 if (strcmp (name, ".sdata") == 0)
1289 r_symndx = RELOC_SECTION_SDATA;
1290 else if (strcmp (name, ".sbss") == 0)
1291 r_symndx = RELOC_SECTION_SBSS;
1292 break;
1293 case 't':
1294 if (strcmp (name, ".text") == 0)
1295 r_symndx = RELOC_SECTION_TEXT;
1296 break;
1297 case 'x':
1298 if (strcmp (name, ".xdata") == 0)
1299 r_symndx = RELOC_SECTION_XDATA;
1300 break;
1303 if (r_symndx == (unsigned long) -1)
1304 abort ();
1306 /* Add the section VMA and the symbol value. */
1307 relocation = (h->root.u.def.value
1308 + hsec->output_section->vma
1309 + hsec->output_offset);
1311 else
1313 /* Change the symndx value to the right one for
1314 the output BFD. */
1315 r_symndx = h->indx;
1316 if (r_symndx == (unsigned long) -1)
1318 /* Caller must give an error. */
1319 r_symndx = 0;
1321 relocation = 0;
1324 /* Write out the new r_symndx value. */
1325 H_PUT_32 (input_bfd, r_symndx, ext_rel->r_symndx);
1327 return relocation;
1330 /* Relocate a section while linking an Alpha ECOFF file. This is
1331 quite similar to get_relocated_section_contents. Perhaps they
1332 could be combined somehow. */
1334 static bool
1335 alpha_relocate_section (bfd *output_bfd,
1336 struct bfd_link_info *info,
1337 bfd *input_bfd,
1338 asection *input_section,
1339 bfd_byte *contents,
1340 void * external_relocs)
1342 asection **symndx_to_section, *lita_sec;
1343 struct ecoff_link_hash_entry **sym_hashes;
1344 bfd_vma gp;
1345 bool gp_undefined;
1346 bfd_vma stack[RELOC_STACKSIZE];
1347 int tos = 0;
1348 struct external_reloc *ext_rel;
1349 struct external_reloc *ext_rel_end;
1350 bfd_size_type amt;
1352 /* We keep a table mapping the symndx found in an internal reloc to
1353 the appropriate section. This is faster than looking up the
1354 section by name each time. */
1355 symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
1356 if (symndx_to_section == (asection **) NULL)
1358 amt = NUM_RELOC_SECTIONS * sizeof (asection *);
1359 symndx_to_section = (asection **) bfd_alloc (input_bfd, amt);
1360 if (!symndx_to_section)
1361 return false;
1363 symndx_to_section[RELOC_SECTION_NONE] = NULL;
1364 symndx_to_section[RELOC_SECTION_TEXT] =
1365 bfd_get_section_by_name (input_bfd, ".text");
1366 symndx_to_section[RELOC_SECTION_RDATA] =
1367 bfd_get_section_by_name (input_bfd, ".rdata");
1368 symndx_to_section[RELOC_SECTION_DATA] =
1369 bfd_get_section_by_name (input_bfd, ".data");
1370 symndx_to_section[RELOC_SECTION_SDATA] =
1371 bfd_get_section_by_name (input_bfd, ".sdata");
1372 symndx_to_section[RELOC_SECTION_SBSS] =
1373 bfd_get_section_by_name (input_bfd, ".sbss");
1374 symndx_to_section[RELOC_SECTION_BSS] =
1375 bfd_get_section_by_name (input_bfd, ".bss");
1376 symndx_to_section[RELOC_SECTION_INIT] =
1377 bfd_get_section_by_name (input_bfd, ".init");
1378 symndx_to_section[RELOC_SECTION_LIT8] =
1379 bfd_get_section_by_name (input_bfd, ".lit8");
1380 symndx_to_section[RELOC_SECTION_LIT4] =
1381 bfd_get_section_by_name (input_bfd, ".lit4");
1382 symndx_to_section[RELOC_SECTION_XDATA] =
1383 bfd_get_section_by_name (input_bfd, ".xdata");
1384 symndx_to_section[RELOC_SECTION_PDATA] =
1385 bfd_get_section_by_name (input_bfd, ".pdata");
1386 symndx_to_section[RELOC_SECTION_FINI] =
1387 bfd_get_section_by_name (input_bfd, ".fini");
1388 symndx_to_section[RELOC_SECTION_LITA] =
1389 bfd_get_section_by_name (input_bfd, ".lita");
1390 symndx_to_section[RELOC_SECTION_ABS] = bfd_abs_section_ptr;
1391 symndx_to_section[RELOC_SECTION_RCONST] =
1392 bfd_get_section_by_name (input_bfd, ".rconst");
1394 ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
1397 sym_hashes = ecoff_data (input_bfd)->sym_hashes;
1399 /* On the Alpha, the .lita section must be addressable by the global
1400 pointer. To support large programs, we need to allow multiple
1401 global pointers. This works as long as each input .lita section
1402 is <64KB big. This implies that when producing relocatable
1403 output, the .lita section is limited to 64KB. . */
1405 lita_sec = symndx_to_section[RELOC_SECTION_LITA];
1406 gp = _bfd_get_gp_value (output_bfd);
1407 if (! bfd_link_relocatable (info) && lita_sec != NULL)
1409 struct ecoff_section_tdata *lita_sec_data;
1411 /* Make sure we have a section data structure to which we can
1412 hang on to the gp value we pick for the section. */
1413 lita_sec_data = ecoff_section_data (input_bfd, lita_sec);
1414 if (lita_sec_data == NULL)
1416 amt = sizeof (struct ecoff_section_tdata);
1417 lita_sec_data = ((struct ecoff_section_tdata *)
1418 bfd_zalloc (input_bfd, amt));
1419 lita_sec->used_by_bfd = lita_sec_data;
1422 if (lita_sec_data->gp != 0)
1424 /* If we already assigned a gp to this section, we better
1425 stick with that value. */
1426 gp = lita_sec_data->gp;
1428 else
1430 bfd_vma lita_vma;
1431 bfd_size_type lita_size;
1433 lita_vma = lita_sec->output_offset + lita_sec->output_section->vma;
1434 lita_size = lita_sec->size;
1436 if (gp == 0
1437 || lita_vma < gp - 0x8000
1438 || lita_vma + lita_size >= gp + 0x8000)
1440 /* Either gp hasn't been set at all or the current gp
1441 cannot address this .lita section. In both cases we
1442 reset the gp to point into the "middle" of the
1443 current input .lita section. */
1444 if (gp && !ecoff_data (output_bfd)->issued_multiple_gp_warning)
1446 (*info->callbacks->warning) (info,
1447 _("using multiple gp values"),
1448 (char *) NULL, output_bfd,
1449 (asection *) NULL, (bfd_vma) 0);
1450 ecoff_data (output_bfd)->issued_multiple_gp_warning = true;
1452 if (lita_vma < gp - 0x8000)
1453 gp = lita_vma + lita_size - 0x8000;
1454 else
1455 gp = lita_vma + 0x8000;
1459 lita_sec_data->gp = gp;
1462 _bfd_set_gp_value (output_bfd, gp);
1465 gp_undefined = (gp == 0);
1467 BFD_ASSERT (bfd_header_little_endian (output_bfd));
1468 BFD_ASSERT (bfd_header_little_endian (input_bfd));
1470 ext_rel = (struct external_reloc *) external_relocs;
1471 ext_rel_end = ext_rel + input_section->reloc_count;
1472 for (; ext_rel < ext_rel_end; ext_rel++)
1474 bfd_vma r_vaddr;
1475 unsigned long r_symndx;
1476 int r_type;
1477 int r_extern;
1478 int r_offset;
1479 int r_size;
1480 bool relocatep;
1481 bool adjust_addrp;
1482 bool gp_usedp;
1483 bfd_vma addend;
1485 r_vaddr = H_GET_64 (input_bfd, ext_rel->r_vaddr);
1486 r_symndx = H_GET_32 (input_bfd, ext_rel->r_symndx);
1488 r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
1489 >> RELOC_BITS0_TYPE_SH_LITTLE);
1490 r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
1491 r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
1492 >> RELOC_BITS1_OFFSET_SH_LITTLE);
1493 /* Ignored the reserved bits. */
1494 r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
1495 >> RELOC_BITS3_SIZE_SH_LITTLE);
1497 relocatep = false;
1498 adjust_addrp = true;
1499 gp_usedp = false;
1500 addend = 0;
1502 switch (r_type)
1504 case ALPHA_R_GPRELHIGH:
1505 _bfd_error_handler (_("%pB: %s unsupported"),
1506 input_bfd, "ALPHA_R_GPRELHIGH");
1507 bfd_set_error (bfd_error_bad_value);
1508 continue;
1510 case ALPHA_R_GPRELLOW:
1511 _bfd_error_handler (_("%pB: %s unsupported"),
1512 input_bfd, "ALPHA_R_GPRELLOW");
1513 bfd_set_error (bfd_error_bad_value);
1514 continue;
1516 default:
1517 /* xgettext:c-format */
1518 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1519 input_bfd, (int) r_type);
1520 bfd_set_error (bfd_error_bad_value);
1521 continue;
1523 case ALPHA_R_IGNORE:
1524 /* This reloc appears after a GPDISP reloc. On earlier
1525 versions of OSF/1, It marked the position of the second
1526 instruction to be altered by the GPDISP reloc, but it is
1527 not otherwise used for anything. For some reason, the
1528 address of the relocation does not appear to include the
1529 section VMA, unlike the other relocation types. */
1530 if (bfd_link_relocatable (info))
1531 H_PUT_64 (input_bfd, input_section->output_offset + r_vaddr,
1532 ext_rel->r_vaddr);
1533 adjust_addrp = false;
1534 break;
1536 case ALPHA_R_REFLONG:
1537 case ALPHA_R_REFQUAD:
1538 case ALPHA_R_HINT:
1539 relocatep = true;
1540 break;
1542 case ALPHA_R_BRADDR:
1543 case ALPHA_R_SREL16:
1544 case ALPHA_R_SREL32:
1545 case ALPHA_R_SREL64:
1546 if (r_extern)
1547 addend += - (r_vaddr + 4);
1548 relocatep = true;
1549 break;
1551 case ALPHA_R_GPREL32:
1552 /* This relocation is used in a switch table. It is a 32
1553 bit offset from the current GP value. We must adjust it
1554 by the different between the original GP value and the
1555 current GP value. */
1556 relocatep = true;
1557 addend = ecoff_data (input_bfd)->gp - gp;
1558 gp_usedp = true;
1559 break;
1561 case ALPHA_R_LITERAL:
1562 /* This is a reference to a literal value, generally
1563 (always?) in the .lita section. This is a 16 bit GP
1564 relative relocation. Sometimes the subsequent reloc is a
1565 LITUSE reloc, which indicates how this reloc is used.
1566 This sometimes permits rewriting the two instructions
1567 referred to by the LITERAL and the LITUSE into different
1568 instructions which do not refer to .lita. This can save
1569 a memory reference, and permits removing a value from
1570 .lita thus saving GP relative space.
1572 We do not these optimizations. To do them we would need
1573 to arrange to link the .lita section first, so that by
1574 the time we got here we would know the final values to
1575 use. This would not be particularly difficult, but it is
1576 not currently implemented. */
1578 /* I believe that the LITERAL reloc will only apply to a ldq
1579 or ldl instruction, so check my assumption. */
1581 unsigned long insn;
1583 insn = bfd_get_32 (input_bfd,
1584 contents + r_vaddr - input_section->vma);
1585 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
1586 || ((insn >> 26) & 0x3f) == 0x28);
1589 relocatep = true;
1590 addend = ecoff_data (input_bfd)->gp - gp;
1591 gp_usedp = true;
1592 break;
1594 case ALPHA_R_LITUSE:
1595 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
1596 does not cause anything to happen, itself. */
1597 break;
1599 case ALPHA_R_GPDISP:
1600 /* This marks the ldah of an ldah/lda pair which loads the
1601 gp register with the difference of the gp value and the
1602 current location. The second of the pair is r_symndx
1603 bytes ahead. It used to be marked with an ALPHA_R_IGNORE
1604 reloc, but OSF/1 3.2 no longer does that. */
1606 unsigned long insn1, insn2;
1608 /* Get the two instructions. */
1609 insn1 = bfd_get_32 (input_bfd,
1610 contents + r_vaddr - input_section->vma);
1611 insn2 = bfd_get_32 (input_bfd,
1612 (contents
1613 + r_vaddr
1614 - input_section->vma
1615 + r_symndx));
1617 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
1618 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
1620 /* Get the existing addend. We must account for the sign
1621 extension done by lda and ldah. */
1622 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
1623 if (insn1 & 0x8000)
1625 /* This is addend -= 0x100000000 without causing an
1626 integer overflow on a 32 bit host. */
1627 addend -= 0x80000000;
1628 addend -= 0x80000000;
1630 if (insn2 & 0x8000)
1631 addend -= 0x10000;
1633 /* The existing addend includes the difference between the
1634 gp of the input BFD and the address in the input BFD.
1635 We want to change this to the difference between the
1636 final GP and the final address. */
1637 addend += (gp
1638 - ecoff_data (input_bfd)->gp
1639 + input_section->vma
1640 - (input_section->output_section->vma
1641 + input_section->output_offset));
1643 /* Change the instructions, accounting for the sign
1644 extension, and write them out. */
1645 if (addend & 0x8000)
1646 addend += 0x10000;
1647 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
1648 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
1650 bfd_put_32 (input_bfd, (bfd_vma) insn1,
1651 contents + r_vaddr - input_section->vma);
1652 bfd_put_32 (input_bfd, (bfd_vma) insn2,
1653 contents + r_vaddr - input_section->vma + r_symndx);
1655 gp_usedp = true;
1657 break;
1659 case ALPHA_R_OP_PUSH:
1660 case ALPHA_R_OP_PSUB:
1661 case ALPHA_R_OP_PRSHIFT:
1662 /* Manipulate values on the reloc evaluation stack. The
1663 r_vaddr field is not an address in input_section, it is
1664 the current value (including any addend) of the object
1665 being used. */
1666 if (! r_extern)
1668 asection *s;
1670 s = symndx_to_section[r_symndx];
1671 if (s == (asection *) NULL)
1672 abort ();
1673 addend = s->output_section->vma + s->output_offset - s->vma;
1675 else
1677 struct ecoff_link_hash_entry *h;
1679 h = sym_hashes[r_symndx];
1680 if (h == (struct ecoff_link_hash_entry *) NULL)
1681 abort ();
1683 if (! bfd_link_relocatable (info))
1685 if (h->root.type == bfd_link_hash_defined
1686 || h->root.type == bfd_link_hash_defweak)
1687 addend = (h->root.u.def.value
1688 + h->root.u.def.section->output_section->vma
1689 + h->root.u.def.section->output_offset);
1690 else
1692 /* Note that we pass the address as 0, since we
1693 do not have a meaningful number for the
1694 location within the section that is being
1695 relocated. */
1696 (*info->callbacks->undefined_symbol)
1697 (info, h->root.root.string, input_bfd,
1698 input_section, (bfd_vma) 0, true);
1699 addend = 0;
1702 else
1704 if (h->root.type != bfd_link_hash_defined
1705 && h->root.type != bfd_link_hash_defweak
1706 && h->indx == -1)
1708 /* This symbol is not being written out. Pass
1709 the address as 0, as with undefined_symbol,
1710 above. */
1711 (*info->callbacks->unattached_reloc)
1712 (info, h->root.root.string,
1713 input_bfd, input_section, (bfd_vma) 0);
1716 addend = alpha_convert_external_reloc (output_bfd, info,
1717 input_bfd,
1718 ext_rel, h);
1722 addend += r_vaddr;
1724 if (bfd_link_relocatable (info))
1726 /* Adjust r_vaddr by the addend. */
1727 H_PUT_64 (input_bfd, addend, ext_rel->r_vaddr);
1729 else
1731 switch (r_type)
1733 case ALPHA_R_OP_PUSH:
1734 if (tos >= RELOC_STACKSIZE)
1735 abort ();
1736 stack[tos++] = addend;
1737 break;
1739 case ALPHA_R_OP_PSUB:
1740 if (tos == 0)
1741 abort ();
1742 stack[tos - 1] -= addend;
1743 break;
1745 case ALPHA_R_OP_PRSHIFT:
1746 if (tos == 0)
1747 abort ();
1748 stack[tos - 1] >>= addend;
1749 break;
1753 adjust_addrp = false;
1754 break;
1756 case ALPHA_R_OP_STORE:
1757 /* Store a value from the reloc stack into a bitfield. If
1758 we are generating relocatable output, all we do is
1759 adjust the address of the reloc. */
1760 if (! bfd_link_relocatable (info))
1762 bfd_vma mask;
1763 bfd_vma val;
1765 if (tos == 0)
1766 abort ();
1768 /* Get the relocation mask. The separate steps and the
1769 casts to bfd_vma are attempts to avoid a bug in the
1770 Alpha OSF 1.3 C compiler. See reloc.c for more
1771 details. */
1772 mask = 1;
1773 mask <<= (bfd_vma) r_size;
1774 mask -= 1;
1776 /* FIXME: I don't know what kind of overflow checking,
1777 if any, should be done here. */
1778 val = bfd_get_64 (input_bfd,
1779 contents + r_vaddr - input_section->vma);
1780 val &=~ mask << (bfd_vma) r_offset;
1781 val |= (stack[--tos] & mask) << (bfd_vma) r_offset;
1782 bfd_put_64 (input_bfd, val,
1783 contents + r_vaddr - input_section->vma);
1785 break;
1787 case ALPHA_R_GPVALUE:
1788 /* I really don't know if this does the right thing. */
1789 gp = ecoff_data (input_bfd)->gp + r_symndx;
1790 gp_undefined = false;
1791 break;
1794 if (relocatep)
1796 reloc_howto_type *howto;
1797 struct ecoff_link_hash_entry *h = NULL;
1798 asection *s = NULL;
1799 bfd_vma relocation;
1800 bfd_reloc_status_type r;
1802 /* Perform a relocation. */
1804 howto = &alpha_howto_table[r_type];
1806 if (r_extern)
1808 h = sym_hashes[r_symndx];
1809 /* If h is NULL, that means that there is a reloc
1810 against an external symbol which we thought was just
1811 a debugging symbol. This should not happen. */
1812 if (h == (struct ecoff_link_hash_entry *) NULL)
1813 abort ();
1815 else
1817 if (r_symndx >= NUM_RELOC_SECTIONS)
1818 s = NULL;
1819 else
1820 s = symndx_to_section[r_symndx];
1822 if (s == (asection *) NULL)
1823 abort ();
1826 if (bfd_link_relocatable (info))
1828 /* We are generating relocatable output, and must
1829 convert the existing reloc. */
1830 if (r_extern)
1832 if (h->root.type != bfd_link_hash_defined
1833 && h->root.type != bfd_link_hash_defweak
1834 && h->indx == -1)
1836 /* This symbol is not being written out. */
1837 (*info->callbacks->unattached_reloc)
1838 (info, h->root.root.string, input_bfd,
1839 input_section, r_vaddr - input_section->vma);
1842 relocation = alpha_convert_external_reloc (output_bfd,
1843 info,
1844 input_bfd,
1845 ext_rel,
1848 else
1850 /* This is a relocation against a section. Adjust
1851 the value by the amount the section moved. */
1852 relocation = (s->output_section->vma
1853 + s->output_offset
1854 - s->vma);
1857 /* If this is PC relative, the existing object file
1858 appears to already have the reloc worked out. We
1859 must subtract out the old value and add in the new
1860 one. */
1861 if (howto->pc_relative)
1862 relocation -= (input_section->output_section->vma
1863 + input_section->output_offset
1864 - input_section->vma);
1866 /* Put in any addend. */
1867 relocation += addend;
1869 /* Adjust the contents. */
1870 r = _bfd_relocate_contents (howto, input_bfd, relocation,
1871 (contents
1872 + r_vaddr
1873 - input_section->vma));
1875 else
1877 /* We are producing a final executable. */
1878 if (r_extern)
1880 /* This is a reloc against a symbol. */
1881 if (h->root.type == bfd_link_hash_defined
1882 || h->root.type == bfd_link_hash_defweak)
1884 asection *hsec;
1886 hsec = h->root.u.def.section;
1887 relocation = (h->root.u.def.value
1888 + hsec->output_section->vma
1889 + hsec->output_offset);
1891 else
1893 (*info->callbacks->undefined_symbol)
1894 (info, h->root.root.string, input_bfd, input_section,
1895 r_vaddr - input_section->vma, true);
1896 relocation = 0;
1899 else
1901 /* This is a reloc against a section. */
1902 relocation = (s->output_section->vma
1903 + s->output_offset
1904 - s->vma);
1906 /* Adjust a PC relative relocation by removing the
1907 reference to the original source section. */
1908 if (howto->pc_relative)
1909 relocation += input_section->vma;
1912 r = _bfd_final_link_relocate (howto,
1913 input_bfd,
1914 input_section,
1915 contents,
1916 r_vaddr - input_section->vma,
1917 relocation,
1918 addend);
1921 if (r != bfd_reloc_ok)
1923 switch (r)
1925 default:
1926 case bfd_reloc_outofrange:
1927 abort ();
1928 case bfd_reloc_overflow:
1930 const char *name;
1932 if (r_extern)
1933 name = sym_hashes[r_symndx]->root.root.string;
1934 else
1935 name = bfd_section_name (symndx_to_section[r_symndx]);
1936 (*info->callbacks->reloc_overflow)
1937 (info, NULL, name, alpha_howto_table[r_type].name,
1938 (bfd_vma) 0, input_bfd, input_section,
1939 r_vaddr - input_section->vma);
1941 break;
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 (*info->callbacks->reloc_dangerous)
1960 (info, _("GP relative relocation used when GP not defined"),
1961 input_bfd, input_section, r_vaddr - input_section->vma);
1962 /* Only give the error once per link. */
1963 gp = 4;
1964 _bfd_set_gp_value (output_bfd, gp);
1965 gp_undefined = false;
1969 if (tos != 0)
1970 abort ();
1972 return true;
1975 /* Do final adjustments to the filehdr and the aouthdr. This routine
1976 sets the dynamic bits in the file header. */
1978 static bool
1979 alpha_adjust_headers (bfd *abfd,
1980 struct internal_filehdr *fhdr,
1981 struct internal_aouthdr *ahdr ATTRIBUTE_UNUSED)
1983 if ((abfd->flags & (DYNAMIC | EXEC_P)) == (DYNAMIC | EXEC_P))
1984 fhdr->f_flags |= F_ALPHA_CALL_SHARED;
1985 else if ((abfd->flags & DYNAMIC) != 0)
1986 fhdr->f_flags |= F_ALPHA_SHARABLE;
1987 return true;
1990 /* Archive handling. In OSF/1 (or Digital Unix) v3.2, Digital
1991 introduced archive packing, in which the elements in an archive are
1992 optionally compressed using a simple dictionary scheme. We know
1993 how to read such archives, but we don't write them. */
1995 #define alpha_ecoff_slurp_armap _bfd_ecoff_slurp_armap
1996 #define alpha_ecoff_slurp_extended_name_table \
1997 _bfd_ecoff_slurp_extended_name_table
1998 #define alpha_ecoff_construct_extended_name_table \
1999 _bfd_ecoff_construct_extended_name_table
2000 #define alpha_ecoff_truncate_arname _bfd_ecoff_truncate_arname
2001 #define alpha_ecoff_write_armap _bfd_ecoff_write_armap
2002 #define alpha_ecoff_write_ar_hdr _bfd_generic_write_ar_hdr
2003 #define alpha_ecoff_generic_stat_arch_elt _bfd_ecoff_generic_stat_arch_elt
2004 #define alpha_ecoff_update_armap_timestamp _bfd_ecoff_update_armap_timestamp
2006 /* A compressed file uses this instead of ARFMAG. */
2008 #define ARFZMAG "Z\012"
2010 /* Read an archive header. This is like the standard routine, but it
2011 also accepts ARFZMAG. */
2013 static void *
2014 alpha_ecoff_read_ar_hdr (bfd *abfd)
2016 struct areltdata *ret;
2017 struct ar_hdr *h;
2019 ret = (struct areltdata *) _bfd_generic_read_ar_hdr_mag (abfd, ARFZMAG);
2020 if (ret == NULL)
2021 return NULL;
2023 h = (struct ar_hdr *) ret->arch_header;
2024 if (strncmp (h->ar_fmag, ARFZMAG, 2) == 0)
2026 bfd_byte ab[8];
2028 /* This is a compressed file. We must set the size correctly.
2029 The size is the eight bytes after the dummy file header. */
2030 if (bfd_seek (abfd, (file_ptr) FILHSZ, SEEK_CUR) != 0
2031 || bfd_bread (ab, (bfd_size_type) 8, abfd) != 8
2032 || bfd_seek (abfd, (file_ptr) (- (FILHSZ + 8)), SEEK_CUR) != 0)
2034 free (ret);
2035 return NULL;
2038 ret->parsed_size = H_GET_64 (abfd, ab);
2041 return ret;
2044 /* Get an archive element at a specified file position. This is where
2045 we uncompress the archive element if necessary. */
2047 static bfd *
2048 alpha_ecoff_get_elt_at_filepos (bfd *archive, file_ptr filepos)
2050 bfd *nbfd = NULL;
2051 struct areltdata *tdata;
2052 struct ar_hdr *hdr;
2053 bfd_byte ab[8];
2054 bfd_size_type size;
2055 bfd_byte *buf, *p;
2056 struct bfd_in_memory *bim;
2057 ufile_ptr filesize;
2059 buf = NULL;
2060 nbfd = _bfd_get_elt_at_filepos (archive, filepos);
2061 if (nbfd == NULL)
2062 goto error_return;
2064 if ((nbfd->flags & BFD_IN_MEMORY) != 0)
2066 /* We have already expanded this BFD. */
2067 return nbfd;
2070 tdata = (struct areltdata *) nbfd->arelt_data;
2071 hdr = (struct ar_hdr *) tdata->arch_header;
2072 if (strncmp (hdr->ar_fmag, ARFZMAG, 2) != 0)
2073 return nbfd;
2075 /* We must uncompress this element. We do this by copying it into a
2076 memory buffer, and making bfd_bread and bfd_seek use that buffer.
2077 This can use a lot of memory, but it's simpler than getting a
2078 temporary file, making that work with the file descriptor caching
2079 code, and making sure that it is deleted at all appropriate
2080 times. It can be changed if it ever becomes important. */
2082 /* The compressed file starts with a dummy ECOFF file header. */
2083 if (bfd_seek (nbfd, (file_ptr) FILHSZ, SEEK_SET) != 0)
2084 goto error_return;
2086 /* The next eight bytes are the real file size. */
2087 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8)
2088 goto error_return;
2089 size = H_GET_64 (nbfd, ab);
2091 /* The decompression algorithm will at most expand by eight times. */
2092 filesize = bfd_get_file_size (archive);
2093 if (filesize != 0 && size / 8 > filesize)
2095 bfd_set_error (bfd_error_malformed_archive);
2096 goto error_return;
2099 if (size != 0)
2101 bfd_size_type left;
2102 bfd_byte dict[4096];
2103 unsigned int h;
2104 bfd_byte b;
2106 buf = (bfd_byte *) bfd_malloc (size);
2107 if (buf == NULL)
2108 goto error_return;
2109 p = buf;
2111 left = size;
2113 /* I don't know what the next eight bytes are for. */
2114 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8)
2115 goto error_return;
2117 /* This is the uncompression algorithm. It's a simple
2118 dictionary based scheme in which each character is predicted
2119 by a hash of the previous three characters. A control byte
2120 indicates whether the character is predicted or whether it
2121 appears in the input stream; each control byte manages the
2122 next eight bytes in the output stream. */
2123 memset (dict, 0, sizeof dict);
2124 h = 0;
2125 while (bfd_bread (&b, (bfd_size_type) 1, nbfd) == 1)
2127 unsigned int i;
2129 for (i = 0; i < 8; i++, b >>= 1)
2131 bfd_byte n;
2133 if ((b & 1) == 0)
2134 n = dict[h];
2135 else
2137 if (bfd_bread (&n, 1, nbfd) != 1)
2138 goto error_return;
2139 dict[h] = n;
2142 *p++ = n;
2144 --left;
2145 if (left == 0)
2146 break;
2148 h <<= 4;
2149 h ^= n;
2150 h &= sizeof dict - 1;
2153 if (left == 0)
2154 break;
2158 /* Now the uncompressed file contents are in buf. */
2159 bim = ((struct bfd_in_memory *)
2160 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
2161 if (bim == NULL)
2162 goto error_return;
2163 bim->size = size;
2164 bim->buffer = buf;
2166 nbfd->mtime_set = true;
2167 nbfd->mtime = strtol (hdr->ar_date, (char **) NULL, 10);
2169 nbfd->flags |= BFD_IN_MEMORY;
2170 nbfd->iostream = bim;
2171 nbfd->iovec = &_bfd_memory_iovec;
2172 nbfd->origin = 0;
2173 BFD_ASSERT (! nbfd->cacheable);
2175 return nbfd;
2177 error_return:
2178 free (buf);
2179 if (nbfd != NULL)
2180 bfd_close (nbfd);
2181 return NULL;
2184 /* Open the next archived file. */
2186 static bfd *
2187 alpha_ecoff_openr_next_archived_file (bfd *archive, bfd *last_file)
2189 ufile_ptr filestart;
2191 if (last_file == NULL)
2192 filestart = bfd_ardata (archive)->first_file_filepos;
2193 else
2195 struct areltdata *t;
2196 struct ar_hdr *h;
2197 bfd_size_type size;
2199 /* We can't use arelt_size here, because that uses parsed_size,
2200 which is the uncompressed size. We need the compressed size. */
2201 t = (struct areltdata *) last_file->arelt_data;
2202 h = (struct ar_hdr *) t->arch_header;
2203 size = strtol (h->ar_size, (char **) NULL, 10);
2205 /* Pad to an even boundary...
2206 Note that last_file->origin can be odd in the case of
2207 BSD-4.4-style element with a long odd size. */
2208 filestart = last_file->proxy_origin + size;
2209 filestart += filestart % 2;
2210 if (filestart < last_file->proxy_origin)
2212 /* Prevent looping. See PR19256. */
2213 bfd_set_error (bfd_error_malformed_archive);
2214 return NULL;
2218 return alpha_ecoff_get_elt_at_filepos (archive, filestart);
2221 /* Open the archive file given an index into the armap. */
2223 static bfd *
2224 alpha_ecoff_get_elt_at_index (bfd *abfd, symindex sym_index)
2226 carsym *entry;
2228 entry = bfd_ardata (abfd)->symdefs + sym_index;
2229 return alpha_ecoff_get_elt_at_filepos (abfd, entry->file_offset);
2232 static void
2233 alpha_ecoff_swap_coff_aux_in (bfd *abfd ATTRIBUTE_UNUSED,
2234 void *ext1 ATTRIBUTE_UNUSED,
2235 int type ATTRIBUTE_UNUSED,
2236 int in_class ATTRIBUTE_UNUSED,
2237 int indx ATTRIBUTE_UNUSED,
2238 int numaux ATTRIBUTE_UNUSED,
2239 void *in1 ATTRIBUTE_UNUSED)
2243 static void
2244 alpha_ecoff_swap_coff_sym_in (bfd *abfd ATTRIBUTE_UNUSED,
2245 void *ext1 ATTRIBUTE_UNUSED,
2246 void *in1 ATTRIBUTE_UNUSED)
2250 static void
2251 alpha_ecoff_swap_coff_lineno_in (bfd *abfd ATTRIBUTE_UNUSED,
2252 void *ext1 ATTRIBUTE_UNUSED,
2253 void *in1 ATTRIBUTE_UNUSED)
2257 static unsigned int
2258 alpha_ecoff_swap_coff_aux_out (bfd *abfd ATTRIBUTE_UNUSED,
2259 void *inp ATTRIBUTE_UNUSED,
2260 int type ATTRIBUTE_UNUSED,
2261 int in_class ATTRIBUTE_UNUSED,
2262 int indx ATTRIBUTE_UNUSED,
2263 int numaux ATTRIBUTE_UNUSED,
2264 void *extp ATTRIBUTE_UNUSED)
2266 return 0;
2269 static unsigned int
2270 alpha_ecoff_swap_coff_sym_out (bfd *abfd ATTRIBUTE_UNUSED,
2271 void *inp ATTRIBUTE_UNUSED,
2272 void *extp ATTRIBUTE_UNUSED)
2274 return 0;
2277 static unsigned int
2278 alpha_ecoff_swap_coff_lineno_out (bfd *abfd ATTRIBUTE_UNUSED,
2279 void *inp ATTRIBUTE_UNUSED,
2280 void *extp ATTRIBUTE_UNUSED)
2282 return 0;
2285 static unsigned int
2286 alpha_ecoff_swap_coff_reloc_out (bfd *abfd ATTRIBUTE_UNUSED,
2287 void *inp ATTRIBUTE_UNUSED,
2288 void *extp ATTRIBUTE_UNUSED)
2290 return 0;
2293 /* This is the ECOFF backend structure. The backend field of the
2294 target vector points to this. */
2296 static const struct ecoff_backend_data alpha_ecoff_backend_data =
2298 /* COFF backend structure. */
2300 alpha_ecoff_swap_coff_aux_in, alpha_ecoff_swap_coff_sym_in,
2301 alpha_ecoff_swap_coff_lineno_in, alpha_ecoff_swap_coff_aux_out,
2302 alpha_ecoff_swap_coff_sym_out, alpha_ecoff_swap_coff_lineno_out,
2303 alpha_ecoff_swap_coff_reloc_out,
2304 alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out,
2305 alpha_ecoff_swap_scnhdr_out,
2306 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, true,
2307 ECOFF_NO_LONG_SECTION_NAMES, 4, false, 2, 32768,
2308 alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in,
2309 alpha_ecoff_swap_scnhdr_in, NULL,
2310 alpha_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
2311 alpha_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
2312 _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
2313 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2314 NULL, NULL, NULL, NULL
2316 /* Supported architecture. */
2317 bfd_arch_alpha,
2318 /* Initial portion of armap string. */
2319 "________64",
2320 /* The page boundary used to align sections in a demand-paged
2321 executable file. E.g., 0x1000. */
2322 0x2000,
2323 /* TRUE if the .rdata section is part of the text segment, as on the
2324 Alpha. FALSE if .rdata is part of the data segment, as on the
2325 MIPS. */
2326 true,
2327 /* Bitsize of constructor entries. */
2329 /* Reloc to use for constructor entries. */
2330 &alpha_howto_table[ALPHA_R_REFQUAD],
2332 /* Symbol table magic number. */
2333 magicSym2,
2334 /* Alignment of debugging information. E.g., 4. */
2336 /* Sizes of external symbolic information. */
2337 sizeof (struct hdr_ext),
2338 sizeof (struct dnr_ext),
2339 sizeof (struct pdr_ext),
2340 sizeof (struct sym_ext),
2341 sizeof (struct opt_ext),
2342 sizeof (struct fdr_ext),
2343 sizeof (struct rfd_ext),
2344 sizeof (struct ext_ext),
2345 /* Functions to swap in external symbolic data. */
2346 ecoff_swap_hdr_in,
2347 ecoff_swap_dnr_in,
2348 ecoff_swap_pdr_in,
2349 ecoff_swap_sym_in,
2350 ecoff_swap_opt_in,
2351 ecoff_swap_fdr_in,
2352 ecoff_swap_rfd_in,
2353 ecoff_swap_ext_in,
2354 _bfd_ecoff_swap_tir_in,
2355 _bfd_ecoff_swap_rndx_in,
2356 /* Functions to swap out external symbolic data. */
2357 ecoff_swap_hdr_out,
2358 ecoff_swap_dnr_out,
2359 ecoff_swap_pdr_out,
2360 ecoff_swap_sym_out,
2361 ecoff_swap_opt_out,
2362 ecoff_swap_fdr_out,
2363 ecoff_swap_rfd_out,
2364 ecoff_swap_ext_out,
2365 _bfd_ecoff_swap_tir_out,
2366 _bfd_ecoff_swap_rndx_out,
2367 /* Function to read in symbolic data. */
2368 _bfd_ecoff_slurp_symbolic_info
2370 /* External reloc size. */
2371 RELSZ,
2372 /* Reloc swapping functions. */
2373 alpha_ecoff_swap_reloc_in,
2374 alpha_ecoff_swap_reloc_out,
2375 /* Backend reloc tweaking. */
2376 alpha_adjust_reloc_in,
2377 alpha_adjust_reloc_out,
2378 /* Relocate section contents while linking. */
2379 alpha_relocate_section,
2380 /* Do final adjustments to filehdr and aouthdr. */
2381 alpha_adjust_headers,
2382 /* Read an element from an archive at a given file position. */
2383 alpha_ecoff_get_elt_at_filepos
2386 /* Looking up a reloc type is Alpha specific. */
2387 #define _bfd_ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup
2388 #define _bfd_ecoff_bfd_reloc_name_lookup \
2389 alpha_bfd_reloc_name_lookup
2391 /* So is getting relocated section contents. */
2392 #define _bfd_ecoff_bfd_get_relocated_section_contents \
2393 alpha_ecoff_get_relocated_section_contents
2395 /* Handling file windows is generic. */
2396 #define _bfd_ecoff_get_section_contents_in_window \
2397 _bfd_generic_get_section_contents_in_window
2399 /* Input section flag lookup is generic. */
2400 #define _bfd_ecoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
2402 /* Relaxing sections is generic. */
2403 #define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section
2404 #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
2405 #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections
2406 #define _bfd_ecoff_bfd_is_group_section bfd_generic_is_group_section
2407 #define _bfd_ecoff_bfd_group_name bfd_generic_group_name
2408 #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group
2409 #define _bfd_ecoff_section_already_linked \
2410 _bfd_coff_section_already_linked
2411 #define _bfd_ecoff_bfd_define_common_symbol bfd_generic_define_common_symbol
2412 #define _bfd_ecoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
2413 #define _bfd_ecoff_bfd_define_start_stop bfd_generic_define_start_stop
2414 #define _bfd_ecoff_bfd_link_check_relocs _bfd_generic_link_check_relocs
2416 /* Installing internal relocations in a section is also generic. */
2417 #define _bfd_ecoff_set_reloc _bfd_generic_set_reloc
2419 const bfd_target alpha_ecoff_le_vec =
2421 "ecoff-littlealpha", /* name */
2422 bfd_target_ecoff_flavour,
2423 BFD_ENDIAN_LITTLE, /* data byte order is little */
2424 BFD_ENDIAN_LITTLE, /* header byte order is little */
2426 (HAS_RELOC | EXEC_P /* object flags */
2427 | HAS_LINENO | HAS_DEBUG
2428 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
2430 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE
2431 | SEC_DATA | SEC_SMALL_DATA),
2432 0, /* leading underscore */
2433 ' ', /* ar_pad_char */
2434 15, /* ar_max_namelen */
2435 0, /* match priority. */
2436 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
2437 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2438 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2439 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2440 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2441 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2442 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2444 { /* bfd_check_format */
2445 _bfd_dummy_target,
2446 alpha_ecoff_object_p,
2447 bfd_generic_archive_p,
2448 _bfd_dummy_target
2450 { /* bfd_set_format */
2451 _bfd_bool_bfd_false_error,
2452 _bfd_ecoff_mkobject,
2453 _bfd_generic_mkarchive,
2454 _bfd_bool_bfd_false_error
2456 { /* bfd_write_contents */
2457 _bfd_bool_bfd_false_error,
2458 _bfd_ecoff_write_object_contents,
2459 _bfd_write_archive_contents,
2460 _bfd_bool_bfd_false_error
2463 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2464 BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2465 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2466 BFD_JUMP_TABLE_ARCHIVE (alpha_ecoff),
2467 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2468 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2469 BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2470 BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2471 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2473 NULL,
2475 &alpha_ecoff_backend_data