No empty .Rs/.Re
[netbsd-mini2440.git] / external / gpl3 / binutils / dist / bfd / coff-alpha.c
blobb9872fe3373c65e9c9a3dbfd9be5c7bb74d19589
1 /* BFD back-end for ALPHA Extended-Coff files.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
4 Modified from coff-mips.c by Steve Chamberlain <sac@cygnus.com> and
5 Ian Lance Taylor <ian@cygnus.com>.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "coff/internal.h"
29 #include "coff/sym.h"
30 #include "coff/symconst.h"
31 #include "coff/ecoff.h"
32 #include "coff/alpha.h"
33 #include "aout/ar.h"
34 #include "libcoff.h"
35 #include "libecoff.h"
37 /* Prototypes for static functions. */
39 static const bfd_target *alpha_ecoff_object_p
40 PARAMS ((bfd *));
41 static bfd_boolean alpha_ecoff_bad_format_hook
42 PARAMS ((bfd *abfd, PTR filehdr));
43 static PTR alpha_ecoff_mkobject_hook
44 PARAMS ((bfd *, PTR filehdr, PTR aouthdr));
45 static void alpha_ecoff_swap_reloc_in
46 PARAMS ((bfd *, PTR, struct internal_reloc *));
47 static void alpha_ecoff_swap_reloc_out
48 PARAMS ((bfd *, const struct internal_reloc *, PTR));
49 static void alpha_adjust_reloc_in
50 PARAMS ((bfd *, const struct internal_reloc *, arelent *));
51 static void alpha_adjust_reloc_out
52 PARAMS ((bfd *, const arelent *, struct internal_reloc *));
53 static reloc_howto_type *alpha_bfd_reloc_type_lookup
54 PARAMS ((bfd *, bfd_reloc_code_real_type));
55 static bfd_byte *alpha_ecoff_get_relocated_section_contents
56 PARAMS ((bfd *abfd, struct bfd_link_info *, struct bfd_link_order *,
57 bfd_byte *data, bfd_boolean relocatable, asymbol **symbols));
58 static bfd_vma alpha_convert_external_reloc
59 PARAMS ((bfd *, struct bfd_link_info *, bfd *, struct external_reloc *,
60 struct ecoff_link_hash_entry *));
61 static bfd_boolean alpha_relocate_section
62 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, PTR));
63 static bfd_boolean alpha_adjust_headers
64 PARAMS ((bfd *, struct internal_filehdr *, struct internal_aouthdr *));
65 static PTR alpha_ecoff_read_ar_hdr
66 PARAMS ((bfd *));
67 static bfd *alpha_ecoff_get_elt_at_filepos
68 PARAMS ((bfd *, file_ptr));
69 static bfd *alpha_ecoff_openr_next_archived_file
70 PARAMS ((bfd *, bfd *));
71 static bfd *alpha_ecoff_get_elt_at_index
72 PARAMS ((bfd *, symindex));
74 /* ECOFF has COFF sections, but the debugging information is stored in
75 a completely different format. ECOFF targets use some of the
76 swapping routines from coffswap.h, and some of the generic COFF
77 routines in coffgen.c, but, unlike the real COFF targets, do not
78 use coffcode.h itself.
80 Get the generic COFF swapping routines, except for the reloc,
81 symbol, and lineno ones. Give them ecoff names. Define some
82 accessor macros for the large sizes used for Alpha ECOFF. */
84 #define GET_FILEHDR_SYMPTR H_GET_64
85 #define PUT_FILEHDR_SYMPTR H_PUT_64
86 #define GET_AOUTHDR_TSIZE H_GET_64
87 #define PUT_AOUTHDR_TSIZE H_PUT_64
88 #define GET_AOUTHDR_DSIZE H_GET_64
89 #define PUT_AOUTHDR_DSIZE H_PUT_64
90 #define GET_AOUTHDR_BSIZE H_GET_64
91 #define PUT_AOUTHDR_BSIZE H_PUT_64
92 #define GET_AOUTHDR_ENTRY H_GET_64
93 #define PUT_AOUTHDR_ENTRY H_PUT_64
94 #define GET_AOUTHDR_TEXT_START H_GET_64
95 #define PUT_AOUTHDR_TEXT_START H_PUT_64
96 #define GET_AOUTHDR_DATA_START H_GET_64
97 #define PUT_AOUTHDR_DATA_START H_PUT_64
98 #define GET_SCNHDR_PADDR H_GET_64
99 #define PUT_SCNHDR_PADDR H_PUT_64
100 #define GET_SCNHDR_VADDR H_GET_64
101 #define PUT_SCNHDR_VADDR H_PUT_64
102 #define GET_SCNHDR_SIZE H_GET_64
103 #define PUT_SCNHDR_SIZE H_PUT_64
104 #define GET_SCNHDR_SCNPTR H_GET_64
105 #define PUT_SCNHDR_SCNPTR H_PUT_64
106 #define GET_SCNHDR_RELPTR H_GET_64
107 #define PUT_SCNHDR_RELPTR H_PUT_64
108 #define GET_SCNHDR_LNNOPTR H_GET_64
109 #define PUT_SCNHDR_LNNOPTR H_PUT_64
111 #define ALPHAECOFF
113 #define NO_COFF_RELOCS
114 #define NO_COFF_SYMBOLS
115 #define NO_COFF_LINENOS
116 #define coff_swap_filehdr_in alpha_ecoff_swap_filehdr_in
117 #define coff_swap_filehdr_out alpha_ecoff_swap_filehdr_out
118 #define coff_swap_aouthdr_in alpha_ecoff_swap_aouthdr_in
119 #define coff_swap_aouthdr_out alpha_ecoff_swap_aouthdr_out
120 #define coff_swap_scnhdr_in alpha_ecoff_swap_scnhdr_in
121 #define coff_swap_scnhdr_out alpha_ecoff_swap_scnhdr_out
122 #include "coffswap.h"
124 /* Get the ECOFF swapping routines. */
125 #define ECOFF_64
126 #include "ecoffswap.h"
128 /* How to process the various reloc types. */
130 static bfd_reloc_status_type reloc_nil
131 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
133 static bfd_reloc_status_type
134 reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
135 bfd *abfd ATTRIBUTE_UNUSED;
136 arelent *reloc ATTRIBUTE_UNUSED;
137 asymbol *sym ATTRIBUTE_UNUSED;
138 PTR data ATTRIBUTE_UNUSED;
139 asection *sec ATTRIBUTE_UNUSED;
140 bfd *output_bfd ATTRIBUTE_UNUSED;
141 char **error_message ATTRIBUTE_UNUSED;
143 return bfd_reloc_ok;
146 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
147 from smaller values. Start with zero, widen, *then* decrement. */
148 #define MINUS_ONE (((bfd_vma)0) - 1)
150 static reloc_howto_type alpha_howto_table[] =
152 /* Reloc type 0 is ignored by itself. However, it appears after a
153 GPDISP reloc to identify the location where the low order 16 bits
154 of the gp register are loaded. */
155 HOWTO (ALPHA_R_IGNORE, /* type */
156 0, /* rightshift */
157 0, /* size (0 = byte, 1 = short, 2 = long) */
158 8, /* bitsize */
159 TRUE, /* pc_relative */
160 0, /* bitpos */
161 complain_overflow_dont, /* complain_on_overflow */
162 reloc_nil, /* special_function */
163 "IGNORE", /* name */
164 TRUE, /* partial_inplace */
165 0, /* src_mask */
166 0, /* dst_mask */
167 TRUE), /* pcrel_offset */
169 /* A 32 bit reference to a symbol. */
170 HOWTO (ALPHA_R_REFLONG, /* type */
171 0, /* rightshift */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
173 32, /* bitsize */
174 FALSE, /* pc_relative */
175 0, /* bitpos */
176 complain_overflow_bitfield, /* complain_on_overflow */
177 0, /* special_function */
178 "REFLONG", /* name */
179 TRUE, /* partial_inplace */
180 0xffffffff, /* src_mask */
181 0xffffffff, /* dst_mask */
182 FALSE), /* pcrel_offset */
184 /* A 64 bit reference to a symbol. */
185 HOWTO (ALPHA_R_REFQUAD, /* type */
186 0, /* rightshift */
187 4, /* size (0 = byte, 1 = short, 2 = long) */
188 64, /* bitsize */
189 FALSE, /* pc_relative */
190 0, /* bitpos */
191 complain_overflow_bitfield, /* complain_on_overflow */
192 0, /* special_function */
193 "REFQUAD", /* name */
194 TRUE, /* partial_inplace */
195 MINUS_ONE, /* src_mask */
196 MINUS_ONE, /* dst_mask */
197 FALSE), /* pcrel_offset */
199 /* A 32 bit GP relative offset. This is just like REFLONG except
200 that when the value is used the value of the gp register will be
201 added in. */
202 HOWTO (ALPHA_R_GPREL32, /* type */
203 0, /* rightshift */
204 2, /* size (0 = byte, 1 = short, 2 = long) */
205 32, /* bitsize */
206 FALSE, /* pc_relative */
207 0, /* bitpos */
208 complain_overflow_bitfield, /* complain_on_overflow */
209 0, /* special_function */
210 "GPREL32", /* name */
211 TRUE, /* partial_inplace */
212 0xffffffff, /* src_mask */
213 0xffffffff, /* dst_mask */
214 FALSE), /* pcrel_offset */
216 /* Used for an instruction that refers to memory off the GP
217 register. The offset is 16 bits of the 32 bit instruction. This
218 reloc always seems to be against the .lita section. */
219 HOWTO (ALPHA_R_LITERAL, /* type */
220 0, /* rightshift */
221 2, /* size (0 = byte, 1 = short, 2 = long) */
222 16, /* bitsize */
223 FALSE, /* pc_relative */
224 0, /* bitpos */
225 complain_overflow_signed, /* complain_on_overflow */
226 0, /* special_function */
227 "LITERAL", /* name */
228 TRUE, /* partial_inplace */
229 0xffff, /* src_mask */
230 0xffff, /* dst_mask */
231 FALSE), /* pcrel_offset */
233 /* This reloc only appears immediately following a LITERAL reloc.
234 It identifies a use of the literal. It seems that the linker can
235 use this to eliminate a portion of the .lita section. The symbol
236 index is special: 1 means the literal address is in the base
237 register of a memory format instruction; 2 means the literal
238 address is in the byte offset register of a byte-manipulation
239 instruction; 3 means the literal address is in the target
240 register of a jsr instruction. This does not actually do any
241 relocation. */
242 HOWTO (ALPHA_R_LITUSE, /* type */
243 0, /* rightshift */
244 2, /* size (0 = byte, 1 = short, 2 = long) */
245 32, /* bitsize */
246 FALSE, /* pc_relative */
247 0, /* bitpos */
248 complain_overflow_dont, /* complain_on_overflow */
249 reloc_nil, /* special_function */
250 "LITUSE", /* name */
251 FALSE, /* partial_inplace */
252 0, /* src_mask */
253 0, /* dst_mask */
254 FALSE), /* pcrel_offset */
256 /* Load the gp register. This is always used for a ldah instruction
257 which loads the upper 16 bits of the gp register. The next reloc
258 will be an IGNORE reloc which identifies the location of the lda
259 instruction which loads the lower 16 bits. The symbol index of
260 the GPDISP instruction appears to actually be the number of bytes
261 between the ldah and lda instructions. This gives two different
262 ways to determine where the lda instruction is; I don't know why
263 both are used. The value to use for the relocation is the
264 difference between the GP value and the current location; the
265 load will always be done against a register holding the current
266 address. */
267 HOWTO (ALPHA_R_GPDISP, /* type */
268 16, /* rightshift */
269 2, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
271 TRUE, /* pc_relative */
272 0, /* bitpos */
273 complain_overflow_dont, /* complain_on_overflow */
274 reloc_nil, /* special_function */
275 "GPDISP", /* name */
276 TRUE, /* partial_inplace */
277 0xffff, /* src_mask */
278 0xffff, /* dst_mask */
279 TRUE), /* pcrel_offset */
281 /* A 21 bit branch. The native assembler generates these for
282 branches within the text segment, and also fills in the PC
283 relative offset in the instruction. */
284 HOWTO (ALPHA_R_BRADDR, /* type */
285 2, /* rightshift */
286 2, /* size (0 = byte, 1 = short, 2 = long) */
287 21, /* bitsize */
288 TRUE, /* pc_relative */
289 0, /* bitpos */
290 complain_overflow_signed, /* complain_on_overflow */
291 0, /* special_function */
292 "BRADDR", /* name */
293 TRUE, /* partial_inplace */
294 0x1fffff, /* src_mask */
295 0x1fffff, /* dst_mask */
296 FALSE), /* pcrel_offset */
298 /* A hint for a jump to a register. */
299 HOWTO (ALPHA_R_HINT, /* type */
300 2, /* rightshift */
301 2, /* size (0 = byte, 1 = short, 2 = long) */
302 14, /* bitsize */
303 TRUE, /* pc_relative */
304 0, /* bitpos */
305 complain_overflow_dont, /* complain_on_overflow */
306 0, /* special_function */
307 "HINT", /* name */
308 TRUE, /* partial_inplace */
309 0x3fff, /* src_mask */
310 0x3fff, /* dst_mask */
311 FALSE), /* pcrel_offset */
313 /* 16 bit PC relative offset. */
314 HOWTO (ALPHA_R_SREL16, /* type */
315 0, /* rightshift */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
318 TRUE, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_signed, /* complain_on_overflow */
321 0, /* special_function */
322 "SREL16", /* name */
323 TRUE, /* partial_inplace */
324 0xffff, /* src_mask */
325 0xffff, /* dst_mask */
326 FALSE), /* pcrel_offset */
328 /* 32 bit PC relative offset. */
329 HOWTO (ALPHA_R_SREL32, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 32, /* bitsize */
333 TRUE, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_signed, /* complain_on_overflow */
336 0, /* special_function */
337 "SREL32", /* name */
338 TRUE, /* partial_inplace */
339 0xffffffff, /* src_mask */
340 0xffffffff, /* dst_mask */
341 FALSE), /* pcrel_offset */
343 /* A 64 bit PC relative offset. */
344 HOWTO (ALPHA_R_SREL64, /* type */
345 0, /* rightshift */
346 4, /* size (0 = byte, 1 = short, 2 = long) */
347 64, /* bitsize */
348 TRUE, /* pc_relative */
349 0, /* bitpos */
350 complain_overflow_signed, /* complain_on_overflow */
351 0, /* special_function */
352 "SREL64", /* name */
353 TRUE, /* partial_inplace */
354 MINUS_ONE, /* src_mask */
355 MINUS_ONE, /* dst_mask */
356 FALSE), /* pcrel_offset */
358 /* Push a value on the reloc evaluation stack. */
359 HOWTO (ALPHA_R_OP_PUSH, /* type */
360 0, /* rightshift */
361 0, /* size (0 = byte, 1 = short, 2 = long) */
362 0, /* bitsize */
363 FALSE, /* pc_relative */
364 0, /* bitpos */
365 complain_overflow_dont, /* complain_on_overflow */
366 0, /* special_function */
367 "OP_PUSH", /* name */
368 FALSE, /* partial_inplace */
369 0, /* src_mask */
370 0, /* dst_mask */
371 FALSE), /* pcrel_offset */
373 /* Store the value from the stack at the given address. Store it in
374 a bitfield of size r_size starting at bit position r_offset. */
375 HOWTO (ALPHA_R_OP_STORE, /* type */
376 0, /* rightshift */
377 4, /* size (0 = byte, 1 = short, 2 = long) */
378 64, /* bitsize */
379 FALSE, /* pc_relative */
380 0, /* bitpos */
381 complain_overflow_dont, /* complain_on_overflow */
382 0, /* special_function */
383 "OP_STORE", /* name */
384 FALSE, /* partial_inplace */
385 0, /* src_mask */
386 MINUS_ONE, /* dst_mask */
387 FALSE), /* pcrel_offset */
389 /* Subtract the reloc address from the value on the top of the
390 relocation stack. */
391 HOWTO (ALPHA_R_OP_PSUB, /* type */
392 0, /* rightshift */
393 0, /* size (0 = byte, 1 = short, 2 = long) */
394 0, /* bitsize */
395 FALSE, /* pc_relative */
396 0, /* bitpos */
397 complain_overflow_dont, /* complain_on_overflow */
398 0, /* special_function */
399 "OP_PSUB", /* name */
400 FALSE, /* partial_inplace */
401 0, /* src_mask */
402 0, /* dst_mask */
403 FALSE), /* pcrel_offset */
405 /* Shift the value on the top of the relocation stack right by the
406 given value. */
407 HOWTO (ALPHA_R_OP_PRSHIFT, /* type */
408 0, /* rightshift */
409 0, /* size (0 = byte, 1 = short, 2 = long) */
410 0, /* bitsize */
411 FALSE, /* pc_relative */
412 0, /* bitpos */
413 complain_overflow_dont, /* complain_on_overflow */
414 0, /* special_function */
415 "OP_PRSHIFT", /* name */
416 FALSE, /* partial_inplace */
417 0, /* src_mask */
418 0, /* dst_mask */
419 FALSE), /* pcrel_offset */
421 /* Adjust the GP value for a new range in the object file. */
422 HOWTO (ALPHA_R_GPVALUE, /* type */
423 0, /* rightshift */
424 0, /* size (0 = byte, 1 = short, 2 = long) */
425 0, /* bitsize */
426 FALSE, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_dont, /* complain_on_overflow */
429 0, /* special_function */
430 "GPVALUE", /* name */
431 FALSE, /* partial_inplace */
432 0, /* src_mask */
433 0, /* dst_mask */
434 FALSE) /* pcrel_offset */
437 /* Recognize an Alpha ECOFF file. */
439 static const bfd_target *
440 alpha_ecoff_object_p (abfd)
441 bfd *abfd;
443 static const bfd_target *ret;
445 ret = coff_object_p (abfd);
447 if (ret != NULL)
449 asection *sec;
451 /* Alpha ECOFF has a .pdata section. The lnnoptr field of the
452 .pdata section is the number of entries it contains. Each
453 entry takes up 8 bytes. The number of entries is required
454 since the section is aligned to a 16 byte boundary. When we
455 link .pdata sections together, we do not want to include the
456 alignment bytes. We handle this on input by faking the size
457 of the .pdata section to remove the unwanted alignment bytes.
458 On output we will set the lnnoptr field and force the
459 alignment. */
460 sec = bfd_get_section_by_name (abfd, _PDATA);
461 if (sec != (asection *) NULL)
463 bfd_size_type size;
465 size = sec->line_filepos * 8;
466 BFD_ASSERT (size == sec->size
467 || size + 8 == sec->size);
468 if (! bfd_set_section_size (abfd, sec, size))
469 return NULL;
473 return ret;
476 /* See whether the magic number matches. */
478 static bfd_boolean
479 alpha_ecoff_bad_format_hook (abfd, filehdr)
480 bfd *abfd ATTRIBUTE_UNUSED;
481 PTR filehdr;
483 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
485 if (! ALPHA_ECOFF_BADMAG (*internal_f))
486 return TRUE;
488 if (ALPHA_ECOFF_COMPRESSEDMAG (*internal_f))
489 (*_bfd_error_handler)
490 (_("%B: Cannot handle compressed Alpha binaries.\n"
491 " Use compiler flags, or objZ, to generate uncompressed binaries."),
492 abfd);
494 return FALSE;
497 /* This is a hook called by coff_real_object_p to create any backend
498 specific information. */
500 static PTR
501 alpha_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
502 bfd *abfd;
503 PTR filehdr;
504 PTR aouthdr;
506 PTR ecoff;
508 ecoff = _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr);
510 if (ecoff != NULL)
512 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
514 /* Set additional BFD flags according to the object type from the
515 machine specific file header flags. */
516 switch (internal_f->f_flags & F_ALPHA_OBJECT_TYPE_MASK)
518 case F_ALPHA_SHARABLE:
519 abfd->flags |= DYNAMIC;
520 break;
521 case F_ALPHA_CALL_SHARED:
522 /* Always executable if using shared libraries as the run time
523 loader might resolve undefined references. */
524 abfd->flags |= (DYNAMIC | EXEC_P);
525 break;
528 return ecoff;
531 /* Reloc handling. */
533 /* Swap a reloc in. */
535 static void
536 alpha_ecoff_swap_reloc_in (abfd, ext_ptr, intern)
537 bfd *abfd;
538 PTR ext_ptr;
539 struct internal_reloc *intern;
541 const RELOC *ext = (RELOC *) ext_ptr;
543 intern->r_vaddr = H_GET_64 (abfd, ext->r_vaddr);
544 intern->r_symndx = H_GET_32 (abfd, ext->r_symndx);
546 BFD_ASSERT (bfd_header_little_endian (abfd));
548 intern->r_type = ((ext->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
549 >> RELOC_BITS0_TYPE_SH_LITTLE);
550 intern->r_extern = (ext->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
551 intern->r_offset = ((ext->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
552 >> RELOC_BITS1_OFFSET_SH_LITTLE);
553 /* Ignored the reserved bits. */
554 intern->r_size = ((ext->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
555 >> RELOC_BITS3_SIZE_SH_LITTLE);
557 if (intern->r_type == ALPHA_R_LITUSE
558 || intern->r_type == ALPHA_R_GPDISP)
560 /* Handle the LITUSE and GPDISP relocs specially. Its symndx
561 value is not actually a symbol index, but is instead a
562 special code. We put the code in the r_size field, and
563 clobber the symndx. */
564 if (intern->r_size != 0)
565 abort ();
566 intern->r_size = intern->r_symndx;
567 intern->r_symndx = RELOC_SECTION_NONE;
569 else if (intern->r_type == ALPHA_R_IGNORE)
571 /* The IGNORE reloc generally follows a GPDISP reloc, and is
572 against the .lita section. The section is irrelevant. */
573 if (! intern->r_extern &&
574 intern->r_symndx == RELOC_SECTION_ABS)
575 abort ();
576 if (! intern->r_extern && intern->r_symndx == RELOC_SECTION_LITA)
577 intern->r_symndx = RELOC_SECTION_ABS;
581 /* Swap a reloc out. */
583 static void
584 alpha_ecoff_swap_reloc_out (abfd, intern, dst)
585 bfd *abfd;
586 const struct internal_reloc *intern;
587 PTR dst;
589 RELOC *ext = (RELOC *) dst;
590 long symndx;
591 unsigned char size;
593 /* Undo the hackery done in swap_reloc_in. */
594 if (intern->r_type == ALPHA_R_LITUSE
595 || intern->r_type == ALPHA_R_GPDISP)
597 symndx = intern->r_size;
598 size = 0;
600 else if (intern->r_type == ALPHA_R_IGNORE
601 && ! intern->r_extern
602 && intern->r_symndx == RELOC_SECTION_ABS)
604 symndx = RELOC_SECTION_LITA;
605 size = intern->r_size;
607 else
609 symndx = intern->r_symndx;
610 size = intern->r_size;
613 /* XXX FIXME: The maximum symndx value used to be 14 but this
614 fails with object files produced by DEC's C++ compiler.
615 Where does the value 14 (or 15) come from anyway ? */
616 BFD_ASSERT (intern->r_extern
617 || (intern->r_symndx >= 0 && intern->r_symndx <= 15));
619 H_PUT_64 (abfd, intern->r_vaddr, ext->r_vaddr);
620 H_PUT_32 (abfd, symndx, ext->r_symndx);
622 BFD_ASSERT (bfd_header_little_endian (abfd));
624 ext->r_bits[0] = ((intern->r_type << RELOC_BITS0_TYPE_SH_LITTLE)
625 & RELOC_BITS0_TYPE_LITTLE);
626 ext->r_bits[1] = ((intern->r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0)
627 | ((intern->r_offset << RELOC_BITS1_OFFSET_SH_LITTLE)
628 & RELOC_BITS1_OFFSET_LITTLE));
629 ext->r_bits[2] = 0;
630 ext->r_bits[3] = ((size << RELOC_BITS3_SIZE_SH_LITTLE)
631 & RELOC_BITS3_SIZE_LITTLE);
634 /* Finish canonicalizing a reloc. Part of this is generic to all
635 ECOFF targets, and that part is in ecoff.c. The rest is done in
636 this backend routine. It must fill in the howto field. */
638 static void
639 alpha_adjust_reloc_in (abfd, intern, rptr)
640 bfd *abfd;
641 const struct internal_reloc *intern;
642 arelent *rptr;
644 if (intern->r_type > ALPHA_R_GPVALUE)
646 (*_bfd_error_handler)
647 (_("%B: unknown/unsupported relocation type %d"),
648 abfd, intern->r_type);
649 bfd_set_error (bfd_error_bad_value);
650 rptr->addend = 0;
651 rptr->howto = NULL;
652 return;
655 switch (intern->r_type)
657 case ALPHA_R_BRADDR:
658 case ALPHA_R_SREL16:
659 case ALPHA_R_SREL32:
660 case ALPHA_R_SREL64:
661 /* This relocs appear to be fully resolved when they are against
662 internal symbols. Against external symbols, BRADDR at least
663 appears to be resolved against the next instruction. */
664 if (! intern->r_extern)
665 rptr->addend = 0;
666 else
667 rptr->addend = - (intern->r_vaddr + 4);
668 break;
670 case ALPHA_R_GPREL32:
671 case ALPHA_R_LITERAL:
672 /* Copy the gp value for this object file into the addend, to
673 ensure that we are not confused by the linker. */
674 if (! intern->r_extern)
675 rptr->addend += ecoff_data (abfd)->gp;
676 break;
678 case ALPHA_R_LITUSE:
679 case ALPHA_R_GPDISP:
680 /* The LITUSE and GPDISP relocs do not use a symbol, or an
681 addend, but they do use a special code. Put this code in the
682 addend field. */
683 rptr->addend = intern->r_size;
684 break;
686 case ALPHA_R_OP_STORE:
687 /* The STORE reloc needs the size and offset fields. We store
688 them in the addend. */
689 #if 0
690 BFD_ASSERT (intern->r_offset <= 256);
691 #endif
692 rptr->addend = (intern->r_offset << 8) + intern->r_size;
693 break;
695 case ALPHA_R_OP_PUSH:
696 case ALPHA_R_OP_PSUB:
697 case ALPHA_R_OP_PRSHIFT:
698 /* The PUSH, PSUB and PRSHIFT relocs do not actually use an
699 address. I believe that the address supplied is really an
700 addend. */
701 rptr->addend = intern->r_vaddr;
702 break;
704 case ALPHA_R_GPVALUE:
705 /* Set the addend field to the new GP value. */
706 rptr->addend = intern->r_symndx + ecoff_data (abfd)->gp;
707 break;
709 case ALPHA_R_IGNORE:
710 /* If the type is ALPHA_R_IGNORE, make sure this is a reference
711 to the absolute section so that the reloc is ignored. For
712 some reason the address of this reloc type is not adjusted by
713 the section vma. We record the gp value for this object file
714 here, for convenience when doing the GPDISP relocation. */
715 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
716 rptr->address = intern->r_vaddr;
717 rptr->addend = ecoff_data (abfd)->gp;
718 break;
720 default:
721 break;
724 rptr->howto = &alpha_howto_table[intern->r_type];
727 /* When writing out a reloc we need to pull some values back out of
728 the addend field into the reloc. This is roughly the reverse of
729 alpha_adjust_reloc_in, except that there are several changes we do
730 not need to undo. */
732 static void
733 alpha_adjust_reloc_out (abfd, rel, intern)
734 bfd *abfd ATTRIBUTE_UNUSED;
735 const arelent *rel;
736 struct internal_reloc *intern;
738 switch (intern->r_type)
740 case ALPHA_R_LITUSE:
741 case ALPHA_R_GPDISP:
742 intern->r_size = rel->addend;
743 break;
745 case ALPHA_R_OP_STORE:
746 intern->r_size = rel->addend & 0xff;
747 intern->r_offset = (rel->addend >> 8) & 0xff;
748 break;
750 case ALPHA_R_OP_PUSH:
751 case ALPHA_R_OP_PSUB:
752 case ALPHA_R_OP_PRSHIFT:
753 intern->r_vaddr = rel->addend;
754 break;
756 case ALPHA_R_IGNORE:
757 intern->r_vaddr = rel->address;
758 break;
760 default:
761 break;
765 /* The size of the stack for the relocation evaluator. */
766 #define RELOC_STACKSIZE (10)
768 /* Alpha ECOFF relocs have a built in expression evaluator as well as
769 other interdependencies. Rather than use a bunch of special
770 functions and global variables, we use a single routine to do all
771 the relocation for a section. I haven't yet worked out how the
772 assembler is going to handle this. */
774 static bfd_byte *
775 alpha_ecoff_get_relocated_section_contents (abfd, link_info, link_order,
776 data, relocatable, symbols)
777 bfd *abfd;
778 struct bfd_link_info *link_info;
779 struct bfd_link_order *link_order;
780 bfd_byte *data;
781 bfd_boolean relocatable;
782 asymbol **symbols;
784 bfd *input_bfd = link_order->u.indirect.section->owner;
785 asection *input_section = link_order->u.indirect.section;
786 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
787 arelent **reloc_vector = NULL;
788 long reloc_count;
789 bfd *output_bfd = relocatable ? abfd : (bfd *) NULL;
790 bfd_vma gp;
791 bfd_size_type sz;
792 bfd_boolean gp_undefined;
793 bfd_vma stack[RELOC_STACKSIZE];
794 int tos = 0;
796 if (reloc_size < 0)
797 goto error_return;
798 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
799 if (reloc_vector == NULL && reloc_size != 0)
800 goto error_return;
802 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
803 if (! bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
804 goto error_return;
806 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
807 reloc_vector, symbols);
808 if (reloc_count < 0)
809 goto error_return;
810 if (reloc_count == 0)
811 goto successful_return;
813 /* Get the GP value for the output BFD. */
814 gp_undefined = FALSE;
815 gp = _bfd_get_gp_value (abfd);
816 if (gp == 0)
818 if (relocatable)
820 asection *sec;
821 bfd_vma lo;
823 /* Make up a value. */
824 lo = (bfd_vma) -1;
825 for (sec = abfd->sections; sec != NULL; sec = sec->next)
827 if (sec->vma < lo
828 && (strcmp (sec->name, ".sbss") == 0
829 || strcmp (sec->name, ".sdata") == 0
830 || strcmp (sec->name, ".lit4") == 0
831 || strcmp (sec->name, ".lit8") == 0
832 || strcmp (sec->name, ".lita") == 0))
833 lo = sec->vma;
835 gp = lo + 0x8000;
836 _bfd_set_gp_value (abfd, gp);
838 else
840 struct bfd_link_hash_entry *h;
842 h = bfd_link_hash_lookup (link_info->hash, "_gp", FALSE, FALSE,
843 TRUE);
844 if (h == (struct bfd_link_hash_entry *) NULL
845 || h->type != bfd_link_hash_defined)
846 gp_undefined = TRUE;
847 else
849 gp = (h->u.def.value
850 + h->u.def.section->output_section->vma
851 + h->u.def.section->output_offset);
852 _bfd_set_gp_value (abfd, gp);
857 for (; *reloc_vector != (arelent *) NULL; reloc_vector++)
859 arelent *rel;
860 bfd_reloc_status_type r;
861 char *err;
863 rel = *reloc_vector;
864 r = bfd_reloc_ok;
865 switch (rel->howto->type)
867 case ALPHA_R_IGNORE:
868 rel->address += input_section->output_offset;
869 break;
871 case ALPHA_R_REFLONG:
872 case ALPHA_R_REFQUAD:
873 case ALPHA_R_BRADDR:
874 case ALPHA_R_HINT:
875 case ALPHA_R_SREL16:
876 case ALPHA_R_SREL32:
877 case ALPHA_R_SREL64:
878 if (relocatable
879 && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
881 rel->address += input_section->output_offset;
882 break;
884 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
885 output_bfd, &err);
886 break;
888 case ALPHA_R_GPREL32:
889 /* This relocation is used in a switch table. It is a 32
890 bit offset from the current GP value. We must adjust it
891 by the different between the original GP value and the
892 current GP value. The original GP value is stored in the
893 addend. We adjust the addend and let
894 bfd_perform_relocation finish the job. */
895 rel->addend -= gp;
896 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
897 output_bfd, &err);
898 if (r == bfd_reloc_ok && gp_undefined)
900 r = bfd_reloc_dangerous;
901 err = (char *) _("GP relative relocation used when GP not defined");
903 break;
905 case ALPHA_R_LITERAL:
906 /* This is a reference to a literal value, generally
907 (always?) in the .lita section. This is a 16 bit GP
908 relative relocation. Sometimes the subsequent reloc is a
909 LITUSE reloc, which indicates how this reloc is used.
910 This sometimes permits rewriting the two instructions
911 referred to by the LITERAL and the LITUSE into different
912 instructions which do not refer to .lita. This can save
913 a memory reference, and permits removing a value from
914 .lita thus saving GP relative space.
916 We do not these optimizations. To do them we would need
917 to arrange to link the .lita section first, so that by
918 the time we got here we would know the final values to
919 use. This would not be particularly difficult, but it is
920 not currently implemented. */
923 unsigned long insn;
925 /* I believe that the LITERAL reloc will only apply to a
926 ldq or ldl instruction, so check my assumption. */
927 insn = bfd_get_32 (input_bfd, data + rel->address);
928 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
929 || ((insn >> 26) & 0x3f) == 0x28);
931 rel->addend -= gp;
932 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
933 output_bfd, &err);
934 if (r == bfd_reloc_ok && gp_undefined)
936 r = bfd_reloc_dangerous;
937 err =
938 (char *) _("GP relative relocation used when GP not defined");
941 break;
943 case ALPHA_R_LITUSE:
944 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
945 does not cause anything to happen, itself. */
946 rel->address += input_section->output_offset;
947 break;
949 case ALPHA_R_GPDISP:
950 /* This marks the ldah of an ldah/lda pair which loads the
951 gp register with the difference of the gp value and the
952 current location. The second of the pair is r_size bytes
953 ahead; it used to be marked with an ALPHA_R_IGNORE reloc,
954 but that no longer happens in OSF/1 3.2. */
956 unsigned long insn1, insn2;
957 bfd_vma addend;
959 /* Get the two instructions. */
960 insn1 = bfd_get_32 (input_bfd, data + rel->address);
961 insn2 = bfd_get_32 (input_bfd, data + rel->address + rel->addend);
963 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
964 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
966 /* Get the existing addend. We must account for the sign
967 extension done by lda and ldah. */
968 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
969 if (insn1 & 0x8000)
971 addend -= 0x80000000;
972 addend -= 0x80000000;
974 if (insn2 & 0x8000)
975 addend -= 0x10000;
977 /* The existing addend includes the different between the
978 gp of the input BFD and the address in the input BFD.
979 Subtract this out. */
980 addend -= (ecoff_data (input_bfd)->gp
981 - (input_section->vma + rel->address));
983 /* Now add in the final gp value, and subtract out the
984 final address. */
985 addend += (gp
986 - (input_section->output_section->vma
987 + input_section->output_offset
988 + rel->address));
990 /* Change the instructions, accounting for the sign
991 extension, and write them out. */
992 if (addend & 0x8000)
993 addend += 0x10000;
994 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
995 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
997 bfd_put_32 (input_bfd, (bfd_vma) insn1, data + rel->address);
998 bfd_put_32 (input_bfd, (bfd_vma) insn2,
999 data + rel->address + rel->addend);
1001 rel->address += input_section->output_offset;
1003 break;
1005 case ALPHA_R_OP_PUSH:
1006 /* Push a value on the reloc evaluation stack. */
1008 asymbol *symbol;
1009 bfd_vma relocation;
1011 if (relocatable)
1013 rel->address += input_section->output_offset;
1014 break;
1017 /* Figure out the relocation of this symbol. */
1018 symbol = *rel->sym_ptr_ptr;
1020 if (bfd_is_und_section (symbol->section))
1021 r = bfd_reloc_undefined;
1023 if (bfd_is_com_section (symbol->section))
1024 relocation = 0;
1025 else
1026 relocation = symbol->value;
1027 relocation += symbol->section->output_section->vma;
1028 relocation += symbol->section->output_offset;
1029 relocation += rel->addend;
1031 if (tos >= RELOC_STACKSIZE)
1032 abort ();
1034 stack[tos++] = relocation;
1036 break;
1038 case ALPHA_R_OP_STORE:
1039 /* Store a value from the reloc stack into a bitfield. */
1041 bfd_vma val;
1042 int offset, size;
1044 if (relocatable)
1046 rel->address += input_section->output_offset;
1047 break;
1050 if (tos == 0)
1051 abort ();
1053 /* The offset and size for this reloc are encoded into the
1054 addend field by alpha_adjust_reloc_in. */
1055 offset = (rel->addend >> 8) & 0xff;
1056 size = rel->addend & 0xff;
1058 val = bfd_get_64 (abfd, data + rel->address);
1059 val &=~ (((1 << size) - 1) << offset);
1060 val |= (stack[--tos] & ((1 << size) - 1)) << offset;
1061 bfd_put_64 (abfd, val, data + rel->address);
1063 break;
1065 case ALPHA_R_OP_PSUB:
1066 /* Subtract a value from the top of the stack. */
1068 asymbol *symbol;
1069 bfd_vma relocation;
1071 if (relocatable)
1073 rel->address += input_section->output_offset;
1074 break;
1077 /* Figure out the relocation of this symbol. */
1078 symbol = *rel->sym_ptr_ptr;
1080 if (bfd_is_und_section (symbol->section))
1081 r = bfd_reloc_undefined;
1083 if (bfd_is_com_section (symbol->section))
1084 relocation = 0;
1085 else
1086 relocation = symbol->value;
1087 relocation += symbol->section->output_section->vma;
1088 relocation += symbol->section->output_offset;
1089 relocation += rel->addend;
1091 if (tos == 0)
1092 abort ();
1094 stack[tos - 1] -= relocation;
1096 break;
1098 case ALPHA_R_OP_PRSHIFT:
1099 /* Shift the value on the top of the stack. */
1101 asymbol *symbol;
1102 bfd_vma relocation;
1104 if (relocatable)
1106 rel->address += input_section->output_offset;
1107 break;
1110 /* Figure out the relocation of this symbol. */
1111 symbol = *rel->sym_ptr_ptr;
1113 if (bfd_is_und_section (symbol->section))
1114 r = bfd_reloc_undefined;
1116 if (bfd_is_com_section (symbol->section))
1117 relocation = 0;
1118 else
1119 relocation = symbol->value;
1120 relocation += symbol->section->output_section->vma;
1121 relocation += symbol->section->output_offset;
1122 relocation += rel->addend;
1124 if (tos == 0)
1125 abort ();
1127 stack[tos - 1] >>= relocation;
1129 break;
1131 case ALPHA_R_GPVALUE:
1132 /* I really don't know if this does the right thing. */
1133 gp = rel->addend;
1134 gp_undefined = FALSE;
1135 break;
1137 default:
1138 abort ();
1141 if (relocatable)
1143 asection *os = input_section->output_section;
1145 /* A partial link, so keep the relocs. */
1146 os->orelocation[os->reloc_count] = rel;
1147 os->reloc_count++;
1150 if (r != bfd_reloc_ok)
1152 switch (r)
1154 case bfd_reloc_undefined:
1155 if (! ((*link_info->callbacks->undefined_symbol)
1156 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1157 input_bfd, input_section, rel->address, TRUE)))
1158 goto error_return;
1159 break;
1160 case bfd_reloc_dangerous:
1161 if (! ((*link_info->callbacks->reloc_dangerous)
1162 (link_info, err, input_bfd, input_section,
1163 rel->address)))
1164 goto error_return;
1165 break;
1166 case bfd_reloc_overflow:
1167 if (! ((*link_info->callbacks->reloc_overflow)
1168 (link_info, NULL,
1169 bfd_asymbol_name (*rel->sym_ptr_ptr),
1170 rel->howto->name, rel->addend, input_bfd,
1171 input_section, rel->address)))
1172 goto error_return;
1173 break;
1174 case bfd_reloc_outofrange:
1175 default:
1176 abort ();
1177 break;
1182 if (tos != 0)
1183 abort ();
1185 successful_return:
1186 if (reloc_vector != NULL)
1187 free (reloc_vector);
1188 return data;
1190 error_return:
1191 if (reloc_vector != NULL)
1192 free (reloc_vector);
1193 return NULL;
1196 /* Get the howto structure for a generic reloc type. */
1198 static reloc_howto_type *
1199 alpha_bfd_reloc_type_lookup (abfd, code)
1200 bfd *abfd ATTRIBUTE_UNUSED;
1201 bfd_reloc_code_real_type code;
1203 int alpha_type;
1205 switch (code)
1207 case BFD_RELOC_32:
1208 alpha_type = ALPHA_R_REFLONG;
1209 break;
1210 case BFD_RELOC_64:
1211 case BFD_RELOC_CTOR:
1212 alpha_type = ALPHA_R_REFQUAD;
1213 break;
1214 case BFD_RELOC_GPREL32:
1215 alpha_type = ALPHA_R_GPREL32;
1216 break;
1217 case BFD_RELOC_ALPHA_LITERAL:
1218 alpha_type = ALPHA_R_LITERAL;
1219 break;
1220 case BFD_RELOC_ALPHA_LITUSE:
1221 alpha_type = ALPHA_R_LITUSE;
1222 break;
1223 case BFD_RELOC_ALPHA_GPDISP_HI16:
1224 alpha_type = ALPHA_R_GPDISP;
1225 break;
1226 case BFD_RELOC_ALPHA_GPDISP_LO16:
1227 alpha_type = ALPHA_R_IGNORE;
1228 break;
1229 case BFD_RELOC_23_PCREL_S2:
1230 alpha_type = ALPHA_R_BRADDR;
1231 break;
1232 case BFD_RELOC_ALPHA_HINT:
1233 alpha_type = ALPHA_R_HINT;
1234 break;
1235 case BFD_RELOC_16_PCREL:
1236 alpha_type = ALPHA_R_SREL16;
1237 break;
1238 case BFD_RELOC_32_PCREL:
1239 alpha_type = ALPHA_R_SREL32;
1240 break;
1241 case BFD_RELOC_64_PCREL:
1242 alpha_type = ALPHA_R_SREL64;
1243 break;
1244 default:
1245 return (reloc_howto_type *) NULL;
1248 return &alpha_howto_table[alpha_type];
1251 static reloc_howto_type *
1252 alpha_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1253 const char *r_name)
1255 unsigned int i;
1257 for (i = 0;
1258 i < sizeof (alpha_howto_table) / sizeof (alpha_howto_table[0]);
1259 i++)
1260 if (alpha_howto_table[i].name != NULL
1261 && strcasecmp (alpha_howto_table[i].name, r_name) == 0)
1262 return &alpha_howto_table[i];
1264 return NULL;
1267 /* A helper routine for alpha_relocate_section which converts an
1268 external reloc when generating relocatable output. Returns the
1269 relocation amount. */
1271 static bfd_vma
1272 alpha_convert_external_reloc (output_bfd, info, input_bfd, ext_rel, h)
1273 bfd *output_bfd ATTRIBUTE_UNUSED;
1274 struct bfd_link_info *info;
1275 bfd *input_bfd;
1276 struct external_reloc *ext_rel;
1277 struct ecoff_link_hash_entry *h;
1279 unsigned long r_symndx;
1280 bfd_vma relocation;
1282 BFD_ASSERT (info->relocatable);
1284 if (h->root.type == bfd_link_hash_defined
1285 || h->root.type == bfd_link_hash_defweak)
1287 asection *hsec;
1288 const char *name;
1290 /* This symbol is defined in the output. Convert the reloc from
1291 being against the symbol to being against the section. */
1293 /* Clear the r_extern bit. */
1294 ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE;
1296 /* Compute a new r_symndx value. */
1297 hsec = h->root.u.def.section;
1298 name = bfd_get_section_name (output_bfd, hsec->output_section);
1300 r_symndx = (unsigned long) -1;
1301 switch (name[1])
1303 case 'A':
1304 if (strcmp (name, "*ABS*") == 0)
1305 r_symndx = RELOC_SECTION_ABS;
1306 break;
1307 case 'b':
1308 if (strcmp (name, ".bss") == 0)
1309 r_symndx = RELOC_SECTION_BSS;
1310 break;
1311 case 'd':
1312 if (strcmp (name, ".data") == 0)
1313 r_symndx = RELOC_SECTION_DATA;
1314 break;
1315 case 'f':
1316 if (strcmp (name, ".fini") == 0)
1317 r_symndx = RELOC_SECTION_FINI;
1318 break;
1319 case 'i':
1320 if (strcmp (name, ".init") == 0)
1321 r_symndx = RELOC_SECTION_INIT;
1322 break;
1323 case 'l':
1324 if (strcmp (name, ".lita") == 0)
1325 r_symndx = RELOC_SECTION_LITA;
1326 else if (strcmp (name, ".lit8") == 0)
1327 r_symndx = RELOC_SECTION_LIT8;
1328 else if (strcmp (name, ".lit4") == 0)
1329 r_symndx = RELOC_SECTION_LIT4;
1330 break;
1331 case 'p':
1332 if (strcmp (name, ".pdata") == 0)
1333 r_symndx = RELOC_SECTION_PDATA;
1334 break;
1335 case 'r':
1336 if (strcmp (name, ".rdata") == 0)
1337 r_symndx = RELOC_SECTION_RDATA;
1338 else if (strcmp (name, ".rconst") == 0)
1339 r_symndx = RELOC_SECTION_RCONST;
1340 break;
1341 case 's':
1342 if (strcmp (name, ".sdata") == 0)
1343 r_symndx = RELOC_SECTION_SDATA;
1344 else if (strcmp (name, ".sbss") == 0)
1345 r_symndx = RELOC_SECTION_SBSS;
1346 break;
1347 case 't':
1348 if (strcmp (name, ".text") == 0)
1349 r_symndx = RELOC_SECTION_TEXT;
1350 break;
1351 case 'x':
1352 if (strcmp (name, ".xdata") == 0)
1353 r_symndx = RELOC_SECTION_XDATA;
1354 break;
1357 if (r_symndx == (unsigned long) -1)
1358 abort ();
1360 /* Add the section VMA and the symbol value. */
1361 relocation = (h->root.u.def.value
1362 + hsec->output_section->vma
1363 + hsec->output_offset);
1365 else
1367 /* Change the symndx value to the right one for
1368 the output BFD. */
1369 r_symndx = h->indx;
1370 if (r_symndx == (unsigned long) -1)
1372 /* Caller must give an error. */
1373 r_symndx = 0;
1375 relocation = 0;
1378 /* Write out the new r_symndx value. */
1379 H_PUT_32 (input_bfd, r_symndx, ext_rel->r_symndx);
1381 return relocation;
1384 /* Relocate a section while linking an Alpha ECOFF file. This is
1385 quite similar to get_relocated_section_contents. Perhaps they
1386 could be combined somehow. */
1388 static bfd_boolean
1389 alpha_relocate_section (output_bfd, info, input_bfd, input_section,
1390 contents, external_relocs)
1391 bfd *output_bfd;
1392 struct bfd_link_info *info;
1393 bfd *input_bfd;
1394 asection *input_section;
1395 bfd_byte *contents;
1396 PTR external_relocs;
1398 asection **symndx_to_section, *lita_sec;
1399 struct ecoff_link_hash_entry **sym_hashes;
1400 bfd_vma gp;
1401 bfd_boolean gp_undefined;
1402 bfd_vma stack[RELOC_STACKSIZE];
1403 int tos = 0;
1404 struct external_reloc *ext_rel;
1405 struct external_reloc *ext_rel_end;
1406 bfd_size_type amt;
1408 /* We keep a table mapping the symndx found in an internal reloc to
1409 the appropriate section. This is faster than looking up the
1410 section by name each time. */
1411 symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
1412 if (symndx_to_section == (asection **) NULL)
1414 amt = NUM_RELOC_SECTIONS * sizeof (asection *);
1415 symndx_to_section = (asection **) bfd_alloc (input_bfd, amt);
1416 if (!symndx_to_section)
1417 return FALSE;
1419 symndx_to_section[RELOC_SECTION_NONE] = NULL;
1420 symndx_to_section[RELOC_SECTION_TEXT] =
1421 bfd_get_section_by_name (input_bfd, ".text");
1422 symndx_to_section[RELOC_SECTION_RDATA] =
1423 bfd_get_section_by_name (input_bfd, ".rdata");
1424 symndx_to_section[RELOC_SECTION_DATA] =
1425 bfd_get_section_by_name (input_bfd, ".data");
1426 symndx_to_section[RELOC_SECTION_SDATA] =
1427 bfd_get_section_by_name (input_bfd, ".sdata");
1428 symndx_to_section[RELOC_SECTION_SBSS] =
1429 bfd_get_section_by_name (input_bfd, ".sbss");
1430 symndx_to_section[RELOC_SECTION_BSS] =
1431 bfd_get_section_by_name (input_bfd, ".bss");
1432 symndx_to_section[RELOC_SECTION_INIT] =
1433 bfd_get_section_by_name (input_bfd, ".init");
1434 symndx_to_section[RELOC_SECTION_LIT8] =
1435 bfd_get_section_by_name (input_bfd, ".lit8");
1436 symndx_to_section[RELOC_SECTION_LIT4] =
1437 bfd_get_section_by_name (input_bfd, ".lit4");
1438 symndx_to_section[RELOC_SECTION_XDATA] =
1439 bfd_get_section_by_name (input_bfd, ".xdata");
1440 symndx_to_section[RELOC_SECTION_PDATA] =
1441 bfd_get_section_by_name (input_bfd, ".pdata");
1442 symndx_to_section[RELOC_SECTION_FINI] =
1443 bfd_get_section_by_name (input_bfd, ".fini");
1444 symndx_to_section[RELOC_SECTION_LITA] =
1445 bfd_get_section_by_name (input_bfd, ".lita");
1446 symndx_to_section[RELOC_SECTION_ABS] = bfd_abs_section_ptr;
1447 symndx_to_section[RELOC_SECTION_RCONST] =
1448 bfd_get_section_by_name (input_bfd, ".rconst");
1450 ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
1453 sym_hashes = ecoff_data (input_bfd)->sym_hashes;
1455 /* On the Alpha, the .lita section must be addressable by the global
1456 pointer. To support large programs, we need to allow multiple
1457 global pointers. This works as long as each input .lita section
1458 is <64KB big. This implies that when producing relocatable
1459 output, the .lita section is limited to 64KB. . */
1461 lita_sec = symndx_to_section[RELOC_SECTION_LITA];
1462 gp = _bfd_get_gp_value (output_bfd);
1463 if (! info->relocatable && lita_sec != NULL)
1465 struct ecoff_section_tdata *lita_sec_data;
1467 /* Make sure we have a section data structure to which we can
1468 hang on to the gp value we pick for the section. */
1469 lita_sec_data = ecoff_section_data (input_bfd, lita_sec);
1470 if (lita_sec_data == NULL)
1472 amt = sizeof (struct ecoff_section_tdata);
1473 lita_sec_data = ((struct ecoff_section_tdata *)
1474 bfd_zalloc (input_bfd, amt));
1475 lita_sec->used_by_bfd = lita_sec_data;
1478 if (lita_sec_data->gp != 0)
1480 /* If we already assigned a gp to this section, we better
1481 stick with that value. */
1482 gp = lita_sec_data->gp;
1484 else
1486 bfd_vma lita_vma;
1487 bfd_size_type lita_size;
1489 lita_vma = lita_sec->output_offset + lita_sec->output_section->vma;
1490 lita_size = lita_sec->size;
1492 if (gp == 0
1493 || lita_vma < gp - 0x8000
1494 || lita_vma + lita_size >= gp + 0x8000)
1496 /* Either gp hasn't been set at all or the current gp
1497 cannot address this .lita section. In both cases we
1498 reset the gp to point into the "middle" of the
1499 current input .lita section. */
1500 if (gp && !ecoff_data (output_bfd)->issued_multiple_gp_warning)
1502 (*info->callbacks->warning) (info,
1503 _("using multiple gp values"),
1504 (char *) NULL, output_bfd,
1505 (asection *) NULL, (bfd_vma) 0);
1506 ecoff_data (output_bfd)->issued_multiple_gp_warning = TRUE;
1508 if (lita_vma < gp - 0x8000)
1509 gp = lita_vma + lita_size - 0x8000;
1510 else
1511 gp = lita_vma + 0x8000;
1515 lita_sec_data->gp = gp;
1518 _bfd_set_gp_value (output_bfd, gp);
1521 gp_undefined = (gp == 0);
1523 BFD_ASSERT (bfd_header_little_endian (output_bfd));
1524 BFD_ASSERT (bfd_header_little_endian (input_bfd));
1526 ext_rel = (struct external_reloc *) external_relocs;
1527 ext_rel_end = ext_rel + input_section->reloc_count;
1528 for (; ext_rel < ext_rel_end; ext_rel++)
1530 bfd_vma r_vaddr;
1531 unsigned long r_symndx;
1532 int r_type;
1533 int r_extern;
1534 int r_offset;
1535 int r_size;
1536 bfd_boolean relocatep;
1537 bfd_boolean adjust_addrp;
1538 bfd_boolean gp_usedp;
1539 bfd_vma addend;
1541 r_vaddr = H_GET_64 (input_bfd, ext_rel->r_vaddr);
1542 r_symndx = H_GET_32 (input_bfd, ext_rel->r_symndx);
1544 r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
1545 >> RELOC_BITS0_TYPE_SH_LITTLE);
1546 r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
1547 r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
1548 >> RELOC_BITS1_OFFSET_SH_LITTLE);
1549 /* Ignored the reserved bits. */
1550 r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
1551 >> RELOC_BITS3_SIZE_SH_LITTLE);
1553 relocatep = FALSE;
1554 adjust_addrp = TRUE;
1555 gp_usedp = FALSE;
1556 addend = 0;
1558 switch (r_type)
1560 case ALPHA_R_GPRELHIGH:
1561 (*_bfd_error_handler)
1562 (_("%B: unsupported relocation: ALPHA_R_GPRELHIGH"),
1563 input_bfd);
1564 bfd_set_error (bfd_error_bad_value);
1565 continue;
1567 case ALPHA_R_GPRELLOW:
1568 (*_bfd_error_handler)
1569 (_("%B: unsupported relocation: ALPHA_R_GPRELLOW"),
1570 input_bfd);
1571 bfd_set_error (bfd_error_bad_value);
1572 continue;
1574 default:
1575 (*_bfd_error_handler)
1576 (_("%B: unknown relocation type %d"),
1577 input_bfd, (int) r_type);
1578 bfd_set_error (bfd_error_bad_value);
1579 continue;
1581 case ALPHA_R_IGNORE:
1582 /* This reloc appears after a GPDISP reloc. On earlier
1583 versions of OSF/1, It marked the position of the second
1584 instruction to be altered by the GPDISP reloc, but it is
1585 not otherwise used for anything. For some reason, the
1586 address of the relocation does not appear to include the
1587 section VMA, unlike the other relocation types. */
1588 if (info->relocatable)
1589 H_PUT_64 (input_bfd, input_section->output_offset + r_vaddr,
1590 ext_rel->r_vaddr);
1591 adjust_addrp = FALSE;
1592 break;
1594 case ALPHA_R_REFLONG:
1595 case ALPHA_R_REFQUAD:
1596 case ALPHA_R_HINT:
1597 relocatep = TRUE;
1598 break;
1600 case ALPHA_R_BRADDR:
1601 case ALPHA_R_SREL16:
1602 case ALPHA_R_SREL32:
1603 case ALPHA_R_SREL64:
1604 if (r_extern)
1605 addend += - (r_vaddr + 4);
1606 relocatep = TRUE;
1607 break;
1609 case ALPHA_R_GPREL32:
1610 /* This relocation is used in a switch table. It is a 32
1611 bit offset from the current GP value. We must adjust it
1612 by the different between the original GP value and the
1613 current GP value. */
1614 relocatep = TRUE;
1615 addend = ecoff_data (input_bfd)->gp - gp;
1616 gp_usedp = TRUE;
1617 break;
1619 case ALPHA_R_LITERAL:
1620 /* This is a reference to a literal value, generally
1621 (always?) in the .lita section. This is a 16 bit GP
1622 relative relocation. Sometimes the subsequent reloc is a
1623 LITUSE reloc, which indicates how this reloc is used.
1624 This sometimes permits rewriting the two instructions
1625 referred to by the LITERAL and the LITUSE into different
1626 instructions which do not refer to .lita. This can save
1627 a memory reference, and permits removing a value from
1628 .lita thus saving GP relative space.
1630 We do not these optimizations. To do them we would need
1631 to arrange to link the .lita section first, so that by
1632 the time we got here we would know the final values to
1633 use. This would not be particularly difficult, but it is
1634 not currently implemented. */
1636 /* I believe that the LITERAL reloc will only apply to a ldq
1637 or ldl instruction, so check my assumption. */
1639 unsigned long insn;
1641 insn = bfd_get_32 (input_bfd,
1642 contents + r_vaddr - input_section->vma);
1643 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
1644 || ((insn >> 26) & 0x3f) == 0x28);
1647 relocatep = TRUE;
1648 addend = ecoff_data (input_bfd)->gp - gp;
1649 gp_usedp = TRUE;
1650 break;
1652 case ALPHA_R_LITUSE:
1653 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
1654 does not cause anything to happen, itself. */
1655 break;
1657 case ALPHA_R_GPDISP:
1658 /* This marks the ldah of an ldah/lda pair which loads the
1659 gp register with the difference of the gp value and the
1660 current location. The second of the pair is r_symndx
1661 bytes ahead. It used to be marked with an ALPHA_R_IGNORE
1662 reloc, but OSF/1 3.2 no longer does that. */
1664 unsigned long insn1, insn2;
1666 /* Get the two instructions. */
1667 insn1 = bfd_get_32 (input_bfd,
1668 contents + r_vaddr - input_section->vma);
1669 insn2 = bfd_get_32 (input_bfd,
1670 (contents
1671 + r_vaddr
1672 - input_section->vma
1673 + r_symndx));
1675 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
1676 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
1678 /* Get the existing addend. We must account for the sign
1679 extension done by lda and ldah. */
1680 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
1681 if (insn1 & 0x8000)
1683 /* This is addend -= 0x100000000 without causing an
1684 integer overflow on a 32 bit host. */
1685 addend -= 0x80000000;
1686 addend -= 0x80000000;
1688 if (insn2 & 0x8000)
1689 addend -= 0x10000;
1691 /* The existing addend includes the difference between the
1692 gp of the input BFD and the address in the input BFD.
1693 We want to change this to the difference between the
1694 final GP and the final address. */
1695 addend += (gp
1696 - ecoff_data (input_bfd)->gp
1697 + input_section->vma
1698 - (input_section->output_section->vma
1699 + input_section->output_offset));
1701 /* Change the instructions, accounting for the sign
1702 extension, and write them out. */
1703 if (addend & 0x8000)
1704 addend += 0x10000;
1705 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
1706 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
1708 bfd_put_32 (input_bfd, (bfd_vma) insn1,
1709 contents + r_vaddr - input_section->vma);
1710 bfd_put_32 (input_bfd, (bfd_vma) insn2,
1711 contents + r_vaddr - input_section->vma + r_symndx);
1713 gp_usedp = TRUE;
1715 break;
1717 case ALPHA_R_OP_PUSH:
1718 case ALPHA_R_OP_PSUB:
1719 case ALPHA_R_OP_PRSHIFT:
1720 /* Manipulate values on the reloc evaluation stack. The
1721 r_vaddr field is not an address in input_section, it is
1722 the current value (including any addend) of the object
1723 being used. */
1724 if (! r_extern)
1726 asection *s;
1728 s = symndx_to_section[r_symndx];
1729 if (s == (asection *) NULL)
1730 abort ();
1731 addend = s->output_section->vma + s->output_offset - s->vma;
1733 else
1735 struct ecoff_link_hash_entry *h;
1737 h = sym_hashes[r_symndx];
1738 if (h == (struct ecoff_link_hash_entry *) NULL)
1739 abort ();
1741 if (! info->relocatable)
1743 if (h->root.type == bfd_link_hash_defined
1744 || h->root.type == bfd_link_hash_defweak)
1745 addend = (h->root.u.def.value
1746 + h->root.u.def.section->output_section->vma
1747 + h->root.u.def.section->output_offset);
1748 else
1750 /* Note that we pass the address as 0, since we
1751 do not have a meaningful number for the
1752 location within the section that is being
1753 relocated. */
1754 if (! ((*info->callbacks->undefined_symbol)
1755 (info, h->root.root.string, input_bfd,
1756 input_section, (bfd_vma) 0, TRUE)))
1757 return FALSE;
1758 addend = 0;
1761 else
1763 if (h->root.type != bfd_link_hash_defined
1764 && h->root.type != bfd_link_hash_defweak
1765 && h->indx == -1)
1767 /* This symbol is not being written out. Pass
1768 the address as 0, as with undefined_symbol,
1769 above. */
1770 if (! ((*info->callbacks->unattached_reloc)
1771 (info, h->root.root.string, input_bfd,
1772 input_section, (bfd_vma) 0)))
1773 return FALSE;
1776 addend = alpha_convert_external_reloc (output_bfd, info,
1777 input_bfd,
1778 ext_rel, h);
1782 addend += r_vaddr;
1784 if (info->relocatable)
1786 /* Adjust r_vaddr by the addend. */
1787 H_PUT_64 (input_bfd, addend, ext_rel->r_vaddr);
1789 else
1791 switch (r_type)
1793 case ALPHA_R_OP_PUSH:
1794 if (tos >= RELOC_STACKSIZE)
1795 abort ();
1796 stack[tos++] = addend;
1797 break;
1799 case ALPHA_R_OP_PSUB:
1800 if (tos == 0)
1801 abort ();
1802 stack[tos - 1] -= addend;
1803 break;
1805 case ALPHA_R_OP_PRSHIFT:
1806 if (tos == 0)
1807 abort ();
1808 stack[tos - 1] >>= addend;
1809 break;
1813 adjust_addrp = FALSE;
1814 break;
1816 case ALPHA_R_OP_STORE:
1817 /* Store a value from the reloc stack into a bitfield. If
1818 we are generating relocatable output, all we do is
1819 adjust the address of the reloc. */
1820 if (! info->relocatable)
1822 bfd_vma mask;
1823 bfd_vma val;
1825 if (tos == 0)
1826 abort ();
1828 /* Get the relocation mask. The separate steps and the
1829 casts to bfd_vma are attempts to avoid a bug in the
1830 Alpha OSF 1.3 C compiler. See reloc.c for more
1831 details. */
1832 mask = 1;
1833 mask <<= (bfd_vma) r_size;
1834 mask -= 1;
1836 /* FIXME: I don't know what kind of overflow checking,
1837 if any, should be done here. */
1838 val = bfd_get_64 (input_bfd,
1839 contents + r_vaddr - input_section->vma);
1840 val &=~ mask << (bfd_vma) r_offset;
1841 val |= (stack[--tos] & mask) << (bfd_vma) r_offset;
1842 bfd_put_64 (input_bfd, val,
1843 contents + r_vaddr - input_section->vma);
1845 break;
1847 case ALPHA_R_GPVALUE:
1848 /* I really don't know if this does the right thing. */
1849 gp = ecoff_data (input_bfd)->gp + r_symndx;
1850 gp_undefined = FALSE;
1851 break;
1854 if (relocatep)
1856 reloc_howto_type *howto;
1857 struct ecoff_link_hash_entry *h = NULL;
1858 asection *s = NULL;
1859 bfd_vma relocation;
1860 bfd_reloc_status_type r;
1862 /* Perform a relocation. */
1864 howto = &alpha_howto_table[r_type];
1866 if (r_extern)
1868 h = sym_hashes[r_symndx];
1869 /* If h is NULL, that means that there is a reloc
1870 against an external symbol which we thought was just
1871 a debugging symbol. This should not happen. */
1872 if (h == (struct ecoff_link_hash_entry *) NULL)
1873 abort ();
1875 else
1877 if (r_symndx >= NUM_RELOC_SECTIONS)
1878 s = NULL;
1879 else
1880 s = symndx_to_section[r_symndx];
1882 if (s == (asection *) NULL)
1883 abort ();
1886 if (info->relocatable)
1888 /* We are generating relocatable output, and must
1889 convert the existing reloc. */
1890 if (r_extern)
1892 if (h->root.type != bfd_link_hash_defined
1893 && h->root.type != bfd_link_hash_defweak
1894 && h->indx == -1)
1896 /* This symbol is not being written out. */
1897 if (! ((*info->callbacks->unattached_reloc)
1898 (info, h->root.root.string, input_bfd,
1899 input_section, r_vaddr - input_section->vma)))
1900 return FALSE;
1903 relocation = alpha_convert_external_reloc (output_bfd,
1904 info,
1905 input_bfd,
1906 ext_rel,
1909 else
1911 /* This is a relocation against a section. Adjust
1912 the value by the amount the section moved. */
1913 relocation = (s->output_section->vma
1914 + s->output_offset
1915 - s->vma);
1918 /* If this is PC relative, the existing object file
1919 appears to already have the reloc worked out. We
1920 must subtract out the old value and add in the new
1921 one. */
1922 if (howto->pc_relative)
1923 relocation -= (input_section->output_section->vma
1924 + input_section->output_offset
1925 - input_section->vma);
1927 /* Put in any addend. */
1928 relocation += addend;
1930 /* Adjust the contents. */
1931 r = _bfd_relocate_contents (howto, input_bfd, relocation,
1932 (contents
1933 + r_vaddr
1934 - input_section->vma));
1936 else
1938 /* We are producing a final executable. */
1939 if (r_extern)
1941 /* This is a reloc against a symbol. */
1942 if (h->root.type == bfd_link_hash_defined
1943 || h->root.type == bfd_link_hash_defweak)
1945 asection *hsec;
1947 hsec = h->root.u.def.section;
1948 relocation = (h->root.u.def.value
1949 + hsec->output_section->vma
1950 + hsec->output_offset);
1952 else
1954 if (! ((*info->callbacks->undefined_symbol)
1955 (info, h->root.root.string, input_bfd,
1956 input_section,
1957 r_vaddr - input_section->vma, TRUE)))
1958 return FALSE;
1959 relocation = 0;
1962 else
1964 /* This is a reloc against a section. */
1965 relocation = (s->output_section->vma
1966 + s->output_offset
1967 - s->vma);
1969 /* Adjust a PC relative relocation by removing the
1970 reference to the original source section. */
1971 if (howto->pc_relative)
1972 relocation += input_section->vma;
1975 r = _bfd_final_link_relocate (howto,
1976 input_bfd,
1977 input_section,
1978 contents,
1979 r_vaddr - input_section->vma,
1980 relocation,
1981 addend);
1984 if (r != bfd_reloc_ok)
1986 switch (r)
1988 default:
1989 case bfd_reloc_outofrange:
1990 abort ();
1991 case bfd_reloc_overflow:
1993 const char *name;
1995 if (r_extern)
1996 name = sym_hashes[r_symndx]->root.root.string;
1997 else
1998 name = bfd_section_name (input_bfd,
1999 symndx_to_section[r_symndx]);
2000 if (! ((*info->callbacks->reloc_overflow)
2001 (info, NULL, name,
2002 alpha_howto_table[r_type].name,
2003 (bfd_vma) 0, input_bfd, input_section,
2004 r_vaddr - input_section->vma)))
2005 return FALSE;
2007 break;
2012 if (info->relocatable && adjust_addrp)
2014 /* Change the address of the relocation. */
2015 H_PUT_64 (input_bfd,
2016 (input_section->output_section->vma
2017 + input_section->output_offset
2018 - input_section->vma
2019 + r_vaddr),
2020 ext_rel->r_vaddr);
2023 if (gp_usedp && gp_undefined)
2025 if (! ((*info->callbacks->reloc_dangerous)
2026 (info, _("GP relative relocation used when GP not defined"),
2027 input_bfd, input_section, r_vaddr - input_section->vma)))
2028 return FALSE;
2029 /* Only give the error once per link. */
2030 gp = 4;
2031 _bfd_set_gp_value (output_bfd, gp);
2032 gp_undefined = FALSE;
2036 if (tos != 0)
2037 abort ();
2039 return TRUE;
2042 /* Do final adjustments to the filehdr and the aouthdr. This routine
2043 sets the dynamic bits in the file header. */
2045 static bfd_boolean
2046 alpha_adjust_headers (abfd, fhdr, ahdr)
2047 bfd *abfd;
2048 struct internal_filehdr *fhdr;
2049 struct internal_aouthdr *ahdr ATTRIBUTE_UNUSED;
2051 if ((abfd->flags & (DYNAMIC | EXEC_P)) == (DYNAMIC | EXEC_P))
2052 fhdr->f_flags |= F_ALPHA_CALL_SHARED;
2053 else if ((abfd->flags & DYNAMIC) != 0)
2054 fhdr->f_flags |= F_ALPHA_SHARABLE;
2055 return TRUE;
2058 /* Archive handling. In OSF/1 (or Digital Unix) v3.2, Digital
2059 introduced archive packing, in which the elements in an archive are
2060 optionally compressed using a simple dictionary scheme. We know
2061 how to read such archives, but we don't write them. */
2063 #define alpha_ecoff_slurp_armap _bfd_ecoff_slurp_armap
2064 #define alpha_ecoff_slurp_extended_name_table \
2065 _bfd_ecoff_slurp_extended_name_table
2066 #define alpha_ecoff_construct_extended_name_table \
2067 _bfd_ecoff_construct_extended_name_table
2068 #define alpha_ecoff_truncate_arname _bfd_ecoff_truncate_arname
2069 #define alpha_ecoff_write_armap _bfd_ecoff_write_armap
2070 #define alpha_ecoff_generic_stat_arch_elt _bfd_ecoff_generic_stat_arch_elt
2071 #define alpha_ecoff_update_armap_timestamp _bfd_ecoff_update_armap_timestamp
2073 /* A compressed file uses this instead of ARFMAG. */
2075 #define ARFZMAG "Z\012"
2077 /* Read an archive header. This is like the standard routine, but it
2078 also accepts ARFZMAG. */
2080 static PTR
2081 alpha_ecoff_read_ar_hdr (abfd)
2082 bfd *abfd;
2084 struct areltdata *ret;
2085 struct ar_hdr *h;
2087 ret = (struct areltdata *) _bfd_generic_read_ar_hdr_mag (abfd, ARFZMAG);
2088 if (ret == NULL)
2089 return NULL;
2091 h = (struct ar_hdr *) ret->arch_header;
2092 if (strncmp (h->ar_fmag, ARFZMAG, 2) == 0)
2094 bfd_byte ab[8];
2096 /* This is a compressed file. We must set the size correctly.
2097 The size is the eight bytes after the dummy file header. */
2098 if (bfd_seek (abfd, (file_ptr) FILHSZ, SEEK_CUR) != 0
2099 || bfd_bread (ab, (bfd_size_type) 8, abfd) != 8
2100 || bfd_seek (abfd, (file_ptr) (- (FILHSZ + 8)), SEEK_CUR) != 0)
2101 return NULL;
2103 ret->parsed_size = H_GET_64 (abfd, ab);
2106 return (PTR) ret;
2109 /* Get an archive element at a specified file position. This is where
2110 we uncompress the archive element if necessary. */
2112 static bfd *
2113 alpha_ecoff_get_elt_at_filepos (archive, filepos)
2114 bfd *archive;
2115 file_ptr filepos;
2117 bfd *nbfd = NULL;
2118 struct areltdata *tdata;
2119 struct ar_hdr *hdr;
2120 bfd_byte ab[8];
2121 bfd_size_type size;
2122 bfd_byte *buf, *p;
2123 struct bfd_in_memory *bim;
2125 nbfd = _bfd_get_elt_at_filepos (archive, filepos);
2126 if (nbfd == NULL)
2127 goto error_return;
2129 if ((nbfd->flags & BFD_IN_MEMORY) != 0)
2131 /* We have already expanded this BFD. */
2132 return nbfd;
2135 tdata = (struct areltdata *) nbfd->arelt_data;
2136 hdr = (struct ar_hdr *) tdata->arch_header;
2137 if (strncmp (hdr->ar_fmag, ARFZMAG, 2) != 0)
2138 return nbfd;
2140 /* We must uncompress this element. We do this by copying it into a
2141 memory buffer, and making bfd_bread and bfd_seek use that buffer.
2142 This can use a lot of memory, but it's simpler than getting a
2143 temporary file, making that work with the file descriptor caching
2144 code, and making sure that it is deleted at all appropriate
2145 times. It can be changed if it ever becomes important. */
2147 /* The compressed file starts with a dummy ECOFF file header. */
2148 if (bfd_seek (nbfd, (file_ptr) FILHSZ, SEEK_SET) != 0)
2149 goto error_return;
2151 /* The next eight bytes are the real file size. */
2152 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8)
2153 goto error_return;
2154 size = H_GET_64 (nbfd, ab);
2156 if (size == 0)
2157 buf = NULL;
2158 else
2160 bfd_size_type left;
2161 bfd_byte dict[4096];
2162 unsigned int h;
2163 bfd_byte b;
2165 buf = (bfd_byte *) bfd_alloc (nbfd, size);
2166 if (buf == NULL)
2167 goto error_return;
2168 p = buf;
2170 left = size;
2172 /* I don't know what the next eight bytes are for. */
2173 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8)
2174 goto error_return;
2176 /* This is the uncompression algorithm. It's a simple
2177 dictionary based scheme in which each character is predicted
2178 by a hash of the previous three characters. A control byte
2179 indicates whether the character is predicted or whether it
2180 appears in the input stream; each control byte manages the
2181 next eight bytes in the output stream. */
2182 memset (dict, 0, sizeof dict);
2183 h = 0;
2184 while (bfd_bread (&b, (bfd_size_type) 1, nbfd) == 1)
2186 unsigned int i;
2188 for (i = 0; i < 8; i++, b >>= 1)
2190 bfd_byte n;
2192 if ((b & 1) == 0)
2193 n = dict[h];
2194 else
2196 if (! bfd_bread (&n, (bfd_size_type) 1, nbfd))
2197 goto error_return;
2198 dict[h] = n;
2201 *p++ = n;
2203 --left;
2204 if (left == 0)
2205 break;
2207 h <<= 4;
2208 h ^= n;
2209 h &= sizeof dict - 1;
2212 if (left == 0)
2213 break;
2217 /* Now the uncompressed file contents are in buf. */
2218 bim = ((struct bfd_in_memory *)
2219 bfd_alloc (nbfd, (bfd_size_type) sizeof (struct bfd_in_memory)));
2220 if (bim == NULL)
2221 goto error_return;
2222 bim->size = size;
2223 bim->buffer = buf;
2225 nbfd->mtime_set = TRUE;
2226 nbfd->mtime = strtol (hdr->ar_date, (char **) NULL, 10);
2228 nbfd->flags |= BFD_IN_MEMORY;
2229 nbfd->iostream = (PTR) bim;
2230 BFD_ASSERT (! nbfd->cacheable);
2232 return nbfd;
2234 error_return:
2235 if (nbfd != NULL)
2236 bfd_close (nbfd);
2237 return NULL;
2240 /* Open the next archived file. */
2242 static bfd *
2243 alpha_ecoff_openr_next_archived_file (archive, last_file)
2244 bfd *archive;
2245 bfd *last_file;
2247 file_ptr filestart;
2249 if (last_file == NULL)
2250 filestart = bfd_ardata (archive)->first_file_filepos;
2251 else
2253 struct areltdata *t;
2254 struct ar_hdr *h;
2255 bfd_size_type size;
2257 /* We can't use arelt_size here, because that uses parsed_size,
2258 which is the uncompressed size. We need the compressed size. */
2259 t = (struct areltdata *) last_file->arelt_data;
2260 h = (struct ar_hdr *) t->arch_header;
2261 size = strtol (h->ar_size, (char **) NULL, 10);
2263 /* Pad to an even boundary...
2264 Note that last_file->origin can be odd in the case of
2265 BSD-4.4-style element with a long odd size. */
2266 filestart = last_file->origin + size;
2267 filestart += filestart % 2;
2270 return alpha_ecoff_get_elt_at_filepos (archive, filestart);
2273 /* Open the archive file given an index into the armap. */
2275 static bfd *
2276 alpha_ecoff_get_elt_at_index (abfd, index)
2277 bfd *abfd;
2278 symindex index;
2280 carsym *entry;
2282 entry = bfd_ardata (abfd)->symdefs + index;
2283 return alpha_ecoff_get_elt_at_filepos (abfd, entry->file_offset);
2286 /* This is the ECOFF backend structure. The backend field of the
2287 target vector points to this. */
2289 static const struct ecoff_backend_data alpha_ecoff_backend_data =
2291 /* COFF backend structure. */
2293 (void (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR))) bfd_void, /* aux_in */
2294 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
2295 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
2296 (unsigned (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR)))bfd_void,/*aux_out*/
2297 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
2298 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
2299 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */
2300 alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out,
2301 alpha_ecoff_swap_scnhdr_out,
2302 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, TRUE, FALSE, 4, FALSE, 2,
2303 alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in,
2304 alpha_ecoff_swap_scnhdr_in, NULL,
2305 alpha_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
2306 alpha_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
2307 _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
2308 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2309 NULL, NULL, NULL, NULL
2311 /* Supported architecture. */
2312 bfd_arch_alpha,
2313 /* Initial portion of armap string. */
2314 "________64",
2315 /* The page boundary used to align sections in a demand-paged
2316 executable file. E.g., 0x1000. */
2317 0x2000,
2318 /* TRUE if the .rdata section is part of the text segment, as on the
2319 Alpha. FALSE if .rdata is part of the data segment, as on the
2320 MIPS. */
2321 TRUE,
2322 /* Bitsize of constructor entries. */
2324 /* Reloc to use for constructor entries. */
2325 &alpha_howto_table[ALPHA_R_REFQUAD],
2327 /* Symbol table magic number. */
2328 magicSym2,
2329 /* Alignment of debugging information. E.g., 4. */
2331 /* Sizes of external symbolic information. */
2332 sizeof (struct hdr_ext),
2333 sizeof (struct dnr_ext),
2334 sizeof (struct pdr_ext),
2335 sizeof (struct sym_ext),
2336 sizeof (struct opt_ext),
2337 sizeof (struct fdr_ext),
2338 sizeof (struct rfd_ext),
2339 sizeof (struct ext_ext),
2340 /* Functions to swap in external symbolic data. */
2341 ecoff_swap_hdr_in,
2342 ecoff_swap_dnr_in,
2343 ecoff_swap_pdr_in,
2344 ecoff_swap_sym_in,
2345 ecoff_swap_opt_in,
2346 ecoff_swap_fdr_in,
2347 ecoff_swap_rfd_in,
2348 ecoff_swap_ext_in,
2349 _bfd_ecoff_swap_tir_in,
2350 _bfd_ecoff_swap_rndx_in,
2351 /* Functions to swap out external symbolic data. */
2352 ecoff_swap_hdr_out,
2353 ecoff_swap_dnr_out,
2354 ecoff_swap_pdr_out,
2355 ecoff_swap_sym_out,
2356 ecoff_swap_opt_out,
2357 ecoff_swap_fdr_out,
2358 ecoff_swap_rfd_out,
2359 ecoff_swap_ext_out,
2360 _bfd_ecoff_swap_tir_out,
2361 _bfd_ecoff_swap_rndx_out,
2362 /* Function to read in symbolic data. */
2363 _bfd_ecoff_slurp_symbolic_info
2365 /* External reloc size. */
2366 RELSZ,
2367 /* Reloc swapping functions. */
2368 alpha_ecoff_swap_reloc_in,
2369 alpha_ecoff_swap_reloc_out,
2370 /* Backend reloc tweaking. */
2371 alpha_adjust_reloc_in,
2372 alpha_adjust_reloc_out,
2373 /* Relocate section contents while linking. */
2374 alpha_relocate_section,
2375 /* Do final adjustments to filehdr and aouthdr. */
2376 alpha_adjust_headers,
2377 /* Read an element from an archive at a given file position. */
2378 alpha_ecoff_get_elt_at_filepos
2381 /* Looking up a reloc type is Alpha specific. */
2382 #define _bfd_ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup
2383 #define _bfd_ecoff_bfd_reloc_name_lookup \
2384 alpha_bfd_reloc_name_lookup
2386 /* So is getting relocated section contents. */
2387 #define _bfd_ecoff_bfd_get_relocated_section_contents \
2388 alpha_ecoff_get_relocated_section_contents
2390 /* Handling file windows is generic. */
2391 #define _bfd_ecoff_get_section_contents_in_window \
2392 _bfd_generic_get_section_contents_in_window
2394 /* Relaxing sections is generic. */
2395 #define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section
2396 #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
2397 #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections
2398 #define _bfd_ecoff_bfd_is_group_section bfd_generic_is_group_section
2399 #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group
2400 #define _bfd_ecoff_section_already_linked \
2401 _bfd_generic_section_already_linked
2403 const bfd_target ecoffalpha_little_vec =
2405 "ecoff-littlealpha", /* name */
2406 bfd_target_ecoff_flavour,
2407 BFD_ENDIAN_LITTLE, /* data byte order is little */
2408 BFD_ENDIAN_LITTLE, /* header byte order is little */
2410 (HAS_RELOC | EXEC_P | /* object flags */
2411 HAS_LINENO | HAS_DEBUG |
2412 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
2414 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
2415 0, /* leading underscore */
2416 ' ', /* ar_pad_char */
2417 15, /* ar_max_namelen */
2418 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2419 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2420 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2421 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2422 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2423 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2425 {_bfd_dummy_target, alpha_ecoff_object_p, /* bfd_check_format */
2426 bfd_generic_archive_p, _bfd_dummy_target},
2427 {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
2428 _bfd_generic_mkarchive, bfd_false},
2429 {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
2430 _bfd_write_archive_contents, bfd_false},
2432 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2433 BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2434 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2435 BFD_JUMP_TABLE_ARCHIVE (alpha_ecoff),
2436 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2437 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2438 BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2439 BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2440 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2442 NULL,
2444 (PTR) &alpha_ecoff_backend_data