* configure.tgt (mips64*el-*-linux-gnu*): Define targ_extra_libpath
[binutils.git] / bfd / coff-alpha.c
blob38d2e699b9482f226a63087f484ab749cc69c48c
1 /* BFD back-end for ALPHA Extended-Coff files.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.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. */
38 static const bfd_target *alpha_ecoff_object_p
39 PARAMS ((bfd *));
40 static bfd_boolean alpha_ecoff_bad_format_hook
41 PARAMS ((bfd *abfd, PTR filehdr));
42 static PTR alpha_ecoff_mkobject_hook
43 PARAMS ((bfd *, PTR filehdr, PTR aouthdr));
44 static void alpha_ecoff_swap_reloc_in
45 PARAMS ((bfd *, PTR, struct internal_reloc *));
46 static void alpha_ecoff_swap_reloc_out
47 PARAMS ((bfd *, const struct internal_reloc *, PTR));
48 static void alpha_adjust_reloc_in
49 PARAMS ((bfd *, const struct internal_reloc *, arelent *));
50 static void alpha_adjust_reloc_out
51 PARAMS ((bfd *, const arelent *, struct internal_reloc *));
52 static reloc_howto_type *alpha_bfd_reloc_type_lookup
53 PARAMS ((bfd *, bfd_reloc_code_real_type));
54 static bfd_byte *alpha_ecoff_get_relocated_section_contents
55 PARAMS ((bfd *abfd, struct bfd_link_info *, struct bfd_link_order *,
56 bfd_byte *data, bfd_boolean relocatable, asymbol **symbols));
57 static bfd_vma alpha_convert_external_reloc
58 PARAMS ((bfd *, struct bfd_link_info *, bfd *, struct external_reloc *,
59 struct ecoff_link_hash_entry *));
60 static bfd_boolean alpha_relocate_section
61 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, PTR));
62 static bfd_boolean alpha_adjust_headers
63 PARAMS ((bfd *, struct internal_filehdr *, struct internal_aouthdr *));
64 static PTR alpha_ecoff_read_ar_hdr
65 PARAMS ((bfd *));
66 static bfd *alpha_ecoff_get_elt_at_filepos
67 PARAMS ((bfd *, file_ptr));
68 static bfd *alpha_ecoff_openr_next_archived_file
69 PARAMS ((bfd *, bfd *));
70 static bfd *alpha_ecoff_get_elt_at_index
71 PARAMS ((bfd *, symindex));
73 /* ECOFF has COFF sections, but the debugging information is stored in
74 a completely different format. ECOFF targets use some of the
75 swapping routines from coffswap.h, and some of the generic COFF
76 routines in coffgen.c, but, unlike the real COFF targets, do not
77 use coffcode.h itself.
79 Get the generic COFF swapping routines, except for the reloc,
80 symbol, and lineno ones. Give them ecoff names. Define some
81 accessor macros for the large sizes used for Alpha ECOFF. */
83 #define GET_FILEHDR_SYMPTR H_GET_64
84 #define PUT_FILEHDR_SYMPTR H_PUT_64
85 #define GET_AOUTHDR_TSIZE H_GET_64
86 #define PUT_AOUTHDR_TSIZE H_PUT_64
87 #define GET_AOUTHDR_DSIZE H_GET_64
88 #define PUT_AOUTHDR_DSIZE H_PUT_64
89 #define GET_AOUTHDR_BSIZE H_GET_64
90 #define PUT_AOUTHDR_BSIZE H_PUT_64
91 #define GET_AOUTHDR_ENTRY H_GET_64
92 #define PUT_AOUTHDR_ENTRY H_PUT_64
93 #define GET_AOUTHDR_TEXT_START H_GET_64
94 #define PUT_AOUTHDR_TEXT_START H_PUT_64
95 #define GET_AOUTHDR_DATA_START H_GET_64
96 #define PUT_AOUTHDR_DATA_START H_PUT_64
97 #define GET_SCNHDR_PADDR H_GET_64
98 #define PUT_SCNHDR_PADDR H_PUT_64
99 #define GET_SCNHDR_VADDR H_GET_64
100 #define PUT_SCNHDR_VADDR H_PUT_64
101 #define GET_SCNHDR_SIZE H_GET_64
102 #define PUT_SCNHDR_SIZE H_PUT_64
103 #define GET_SCNHDR_SCNPTR H_GET_64
104 #define PUT_SCNHDR_SCNPTR H_PUT_64
105 #define GET_SCNHDR_RELPTR H_GET_64
106 #define PUT_SCNHDR_RELPTR H_PUT_64
107 #define GET_SCNHDR_LNNOPTR H_GET_64
108 #define PUT_SCNHDR_LNNOPTR H_PUT_64
110 #define ALPHAECOFF
112 #define NO_COFF_RELOCS
113 #define NO_COFF_SYMBOLS
114 #define NO_COFF_LINENOS
115 #define coff_swap_filehdr_in alpha_ecoff_swap_filehdr_in
116 #define coff_swap_filehdr_out alpha_ecoff_swap_filehdr_out
117 #define coff_swap_aouthdr_in alpha_ecoff_swap_aouthdr_in
118 #define coff_swap_aouthdr_out alpha_ecoff_swap_aouthdr_out
119 #define coff_swap_scnhdr_in alpha_ecoff_swap_scnhdr_in
120 #define coff_swap_scnhdr_out alpha_ecoff_swap_scnhdr_out
121 #include "coffswap.h"
123 /* Get the ECOFF swapping routines. */
124 #define ECOFF_64
125 #include "ecoffswap.h"
127 /* How to process the various reloc types. */
129 static bfd_reloc_status_type reloc_nil
130 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
132 static bfd_reloc_status_type
133 reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
134 bfd *abfd ATTRIBUTE_UNUSED;
135 arelent *reloc ATTRIBUTE_UNUSED;
136 asymbol *sym ATTRIBUTE_UNUSED;
137 PTR data ATTRIBUTE_UNUSED;
138 asection *sec ATTRIBUTE_UNUSED;
139 bfd *output_bfd ATTRIBUTE_UNUSED;
140 char **error_message ATTRIBUTE_UNUSED;
142 return bfd_reloc_ok;
145 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
146 from smaller values. Start with zero, widen, *then* decrement. */
147 #define MINUS_ONE (((bfd_vma)0) - 1)
149 static reloc_howto_type alpha_howto_table[] =
151 /* Reloc type 0 is ignored by itself. However, it appears after a
152 GPDISP reloc to identify the location where the low order 16 bits
153 of the gp register are loaded. */
154 HOWTO (ALPHA_R_IGNORE, /* type */
155 0, /* rightshift */
156 0, /* size (0 = byte, 1 = short, 2 = long) */
157 8, /* bitsize */
158 TRUE, /* pc_relative */
159 0, /* bitpos */
160 complain_overflow_dont, /* complain_on_overflow */
161 reloc_nil, /* special_function */
162 "IGNORE", /* name */
163 TRUE, /* partial_inplace */
164 0, /* src_mask */
165 0, /* dst_mask */
166 TRUE), /* pcrel_offset */
168 /* A 32 bit reference to a symbol. */
169 HOWTO (ALPHA_R_REFLONG, /* type */
170 0, /* rightshift */
171 2, /* size (0 = byte, 1 = short, 2 = long) */
172 32, /* bitsize */
173 FALSE, /* pc_relative */
174 0, /* bitpos */
175 complain_overflow_bitfield, /* complain_on_overflow */
176 0, /* special_function */
177 "REFLONG", /* name */
178 TRUE, /* partial_inplace */
179 0xffffffff, /* src_mask */
180 0xffffffff, /* dst_mask */
181 FALSE), /* pcrel_offset */
183 /* A 64 bit reference to a symbol. */
184 HOWTO (ALPHA_R_REFQUAD, /* type */
185 0, /* rightshift */
186 4, /* size (0 = byte, 1 = short, 2 = long) */
187 64, /* bitsize */
188 FALSE, /* pc_relative */
189 0, /* bitpos */
190 complain_overflow_bitfield, /* complain_on_overflow */
191 0, /* special_function */
192 "REFQUAD", /* name */
193 TRUE, /* partial_inplace */
194 MINUS_ONE, /* src_mask */
195 MINUS_ONE, /* dst_mask */
196 FALSE), /* pcrel_offset */
198 /* A 32 bit GP relative offset. This is just like REFLONG except
199 that when the value is used the value of the gp register will be
200 added in. */
201 HOWTO (ALPHA_R_GPREL32, /* type */
202 0, /* rightshift */
203 2, /* size (0 = byte, 1 = short, 2 = long) */
204 32, /* bitsize */
205 FALSE, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_bitfield, /* complain_on_overflow */
208 0, /* special_function */
209 "GPREL32", /* name */
210 TRUE, /* partial_inplace */
211 0xffffffff, /* src_mask */
212 0xffffffff, /* dst_mask */
213 FALSE), /* pcrel_offset */
215 /* Used for an instruction that refers to memory off the GP
216 register. The offset is 16 bits of the 32 bit instruction. This
217 reloc always seems to be against the .lita section. */
218 HOWTO (ALPHA_R_LITERAL, /* type */
219 0, /* rightshift */
220 2, /* size (0 = byte, 1 = short, 2 = long) */
221 16, /* bitsize */
222 FALSE, /* pc_relative */
223 0, /* bitpos */
224 complain_overflow_signed, /* complain_on_overflow */
225 0, /* special_function */
226 "LITERAL", /* name */
227 TRUE, /* partial_inplace */
228 0xffff, /* src_mask */
229 0xffff, /* dst_mask */
230 FALSE), /* pcrel_offset */
232 /* This reloc only appears immediately following a LITERAL reloc.
233 It identifies a use of the literal. It seems that the linker can
234 use this to eliminate a portion of the .lita section. The symbol
235 index is special: 1 means the literal address is in the base
236 register of a memory format instruction; 2 means the literal
237 address is in the byte offset register of a byte-manipulation
238 instruction; 3 means the literal address is in the target
239 register of a jsr instruction. This does not actually do any
240 relocation. */
241 HOWTO (ALPHA_R_LITUSE, /* type */
242 0, /* rightshift */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
244 32, /* bitsize */
245 FALSE, /* pc_relative */
246 0, /* bitpos */
247 complain_overflow_dont, /* complain_on_overflow */
248 reloc_nil, /* special_function */
249 "LITUSE", /* name */
250 FALSE, /* partial_inplace */
251 0, /* src_mask */
252 0, /* dst_mask */
253 FALSE), /* pcrel_offset */
255 /* Load the gp register. This is always used for a ldah instruction
256 which loads the upper 16 bits of the gp register. The next reloc
257 will be an IGNORE reloc which identifies the location of the lda
258 instruction which loads the lower 16 bits. The symbol index of
259 the GPDISP instruction appears to actually be the number of bytes
260 between the ldah and lda instructions. This gives two different
261 ways to determine where the lda instruction is; I don't know why
262 both are used. The value to use for the relocation is the
263 difference between the GP value and the current location; the
264 load will always be done against a register holding the current
265 address. */
266 HOWTO (ALPHA_R_GPDISP, /* type */
267 16, /* rightshift */
268 2, /* size (0 = byte, 1 = short, 2 = long) */
269 16, /* bitsize */
270 TRUE, /* pc_relative */
271 0, /* bitpos */
272 complain_overflow_dont, /* complain_on_overflow */
273 reloc_nil, /* special_function */
274 "GPDISP", /* name */
275 TRUE, /* partial_inplace */
276 0xffff, /* src_mask */
277 0xffff, /* dst_mask */
278 TRUE), /* pcrel_offset */
280 /* A 21 bit branch. The native assembler generates these for
281 branches within the text segment, and also fills in the PC
282 relative offset in the instruction. */
283 HOWTO (ALPHA_R_BRADDR, /* type */
284 2, /* rightshift */
285 2, /* size (0 = byte, 1 = short, 2 = long) */
286 21, /* bitsize */
287 TRUE, /* pc_relative */
288 0, /* bitpos */
289 complain_overflow_signed, /* complain_on_overflow */
290 0, /* special_function */
291 "BRADDR", /* name */
292 TRUE, /* partial_inplace */
293 0x1fffff, /* src_mask */
294 0x1fffff, /* dst_mask */
295 FALSE), /* pcrel_offset */
297 /* A hint for a jump to a register. */
298 HOWTO (ALPHA_R_HINT, /* type */
299 2, /* rightshift */
300 2, /* size (0 = byte, 1 = short, 2 = long) */
301 14, /* bitsize */
302 TRUE, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_dont, /* complain_on_overflow */
305 0, /* special_function */
306 "HINT", /* name */
307 TRUE, /* partial_inplace */
308 0x3fff, /* src_mask */
309 0x3fff, /* dst_mask */
310 FALSE), /* pcrel_offset */
312 /* 16 bit PC relative offset. */
313 HOWTO (ALPHA_R_SREL16, /* type */
314 0, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 16, /* bitsize */
317 TRUE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_signed, /* complain_on_overflow */
320 0, /* special_function */
321 "SREL16", /* name */
322 TRUE, /* partial_inplace */
323 0xffff, /* src_mask */
324 0xffff, /* dst_mask */
325 FALSE), /* pcrel_offset */
327 /* 32 bit PC relative offset. */
328 HOWTO (ALPHA_R_SREL32, /* type */
329 0, /* rightshift */
330 2, /* size (0 = byte, 1 = short, 2 = long) */
331 32, /* bitsize */
332 TRUE, /* pc_relative */
333 0, /* bitpos */
334 complain_overflow_signed, /* complain_on_overflow */
335 0, /* special_function */
336 "SREL32", /* name */
337 TRUE, /* partial_inplace */
338 0xffffffff, /* src_mask */
339 0xffffffff, /* dst_mask */
340 FALSE), /* pcrel_offset */
342 /* A 64 bit PC relative offset. */
343 HOWTO (ALPHA_R_SREL64, /* type */
344 0, /* rightshift */
345 4, /* size (0 = byte, 1 = short, 2 = long) */
346 64, /* bitsize */
347 TRUE, /* pc_relative */
348 0, /* bitpos */
349 complain_overflow_signed, /* complain_on_overflow */
350 0, /* special_function */
351 "SREL64", /* name */
352 TRUE, /* partial_inplace */
353 MINUS_ONE, /* src_mask */
354 MINUS_ONE, /* dst_mask */
355 FALSE), /* pcrel_offset */
357 /* Push a value on the reloc evaluation stack. */
358 HOWTO (ALPHA_R_OP_PUSH, /* type */
359 0, /* rightshift */
360 0, /* size (0 = byte, 1 = short, 2 = long) */
361 0, /* bitsize */
362 FALSE, /* pc_relative */
363 0, /* bitpos */
364 complain_overflow_dont, /* complain_on_overflow */
365 0, /* special_function */
366 "OP_PUSH", /* name */
367 FALSE, /* partial_inplace */
368 0, /* src_mask */
369 0, /* dst_mask */
370 FALSE), /* pcrel_offset */
372 /* Store the value from the stack at the given address. Store it in
373 a bitfield of size r_size starting at bit position r_offset. */
374 HOWTO (ALPHA_R_OP_STORE, /* type */
375 0, /* rightshift */
376 4, /* size (0 = byte, 1 = short, 2 = long) */
377 64, /* bitsize */
378 FALSE, /* pc_relative */
379 0, /* bitpos */
380 complain_overflow_dont, /* complain_on_overflow */
381 0, /* special_function */
382 "OP_STORE", /* name */
383 FALSE, /* partial_inplace */
384 0, /* src_mask */
385 MINUS_ONE, /* dst_mask */
386 FALSE), /* pcrel_offset */
388 /* Subtract the reloc address from the value on the top of the
389 relocation stack. */
390 HOWTO (ALPHA_R_OP_PSUB, /* type */
391 0, /* rightshift */
392 0, /* size (0 = byte, 1 = short, 2 = long) */
393 0, /* bitsize */
394 FALSE, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_dont, /* complain_on_overflow */
397 0, /* special_function */
398 "OP_PSUB", /* name */
399 FALSE, /* partial_inplace */
400 0, /* src_mask */
401 0, /* dst_mask */
402 FALSE), /* pcrel_offset */
404 /* Shift the value on the top of the relocation stack right by the
405 given value. */
406 HOWTO (ALPHA_R_OP_PRSHIFT, /* type */
407 0, /* rightshift */
408 0, /* size (0 = byte, 1 = short, 2 = long) */
409 0, /* bitsize */
410 FALSE, /* pc_relative */
411 0, /* bitpos */
412 complain_overflow_dont, /* complain_on_overflow */
413 0, /* special_function */
414 "OP_PRSHIFT", /* name */
415 FALSE, /* partial_inplace */
416 0, /* src_mask */
417 0, /* dst_mask */
418 FALSE), /* pcrel_offset */
420 /* Adjust the GP value for a new range in the object file. */
421 HOWTO (ALPHA_R_GPVALUE, /* type */
422 0, /* rightshift */
423 0, /* size (0 = byte, 1 = short, 2 = long) */
424 0, /* bitsize */
425 FALSE, /* pc_relative */
426 0, /* bitpos */
427 complain_overflow_dont, /* complain_on_overflow */
428 0, /* special_function */
429 "GPVALUE", /* name */
430 FALSE, /* partial_inplace */
431 0, /* src_mask */
432 0, /* dst_mask */
433 FALSE) /* pcrel_offset */
436 /* Recognize an Alpha ECOFF file. */
438 static const bfd_target *
439 alpha_ecoff_object_p (abfd)
440 bfd *abfd;
442 static const bfd_target *ret;
444 ret = coff_object_p (abfd);
446 if (ret != NULL)
448 asection *sec;
450 /* Alpha ECOFF has a .pdata section. The lnnoptr field of the
451 .pdata section is the number of entries it contains. Each
452 entry takes up 8 bytes. The number of entries is required
453 since the section is aligned to a 16 byte boundary. When we
454 link .pdata sections together, we do not want to include the
455 alignment bytes. We handle this on input by faking the size
456 of the .pdata section to remove the unwanted alignment bytes.
457 On output we will set the lnnoptr field and force the
458 alignment. */
459 sec = bfd_get_section_by_name (abfd, _PDATA);
460 if (sec != (asection *) NULL)
462 bfd_size_type size;
464 size = sec->line_filepos * 8;
465 BFD_ASSERT (size == sec->size
466 || size + 8 == sec->size);
467 if (! bfd_set_section_size (abfd, sec, size))
468 return NULL;
472 return ret;
475 /* See whether the magic number matches. */
477 static bfd_boolean
478 alpha_ecoff_bad_format_hook (abfd, filehdr)
479 bfd *abfd ATTRIBUTE_UNUSED;
480 PTR filehdr;
482 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
484 if (ALPHA_ECOFF_BADMAG (*internal_f))
485 return FALSE;
487 return TRUE;
490 /* This is a hook called by coff_real_object_p to create any backend
491 specific information. */
493 static PTR
494 alpha_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
495 bfd *abfd;
496 PTR filehdr;
497 PTR aouthdr;
499 PTR ecoff;
501 ecoff = _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr);
503 if (ecoff != NULL)
505 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
507 /* Set additional BFD flags according to the object type from the
508 machine specific file header flags. */
509 switch (internal_f->f_flags & F_ALPHA_OBJECT_TYPE_MASK)
511 case F_ALPHA_SHARABLE:
512 abfd->flags |= DYNAMIC;
513 break;
514 case F_ALPHA_CALL_SHARED:
515 /* Always executable if using shared libraries as the run time
516 loader might resolve undefined references. */
517 abfd->flags |= (DYNAMIC | EXEC_P);
518 break;
521 return ecoff;
524 /* Reloc handling. */
526 /* Swap a reloc in. */
528 static void
529 alpha_ecoff_swap_reloc_in (abfd, ext_ptr, intern)
530 bfd *abfd;
531 PTR ext_ptr;
532 struct internal_reloc *intern;
534 const RELOC *ext = (RELOC *) ext_ptr;
536 intern->r_vaddr = H_GET_64 (abfd, ext->r_vaddr);
537 intern->r_symndx = H_GET_32 (abfd, ext->r_symndx);
539 BFD_ASSERT (bfd_header_little_endian (abfd));
541 intern->r_type = ((ext->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
542 >> RELOC_BITS0_TYPE_SH_LITTLE);
543 intern->r_extern = (ext->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
544 intern->r_offset = ((ext->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
545 >> RELOC_BITS1_OFFSET_SH_LITTLE);
546 /* Ignored the reserved bits. */
547 intern->r_size = ((ext->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
548 >> RELOC_BITS3_SIZE_SH_LITTLE);
550 if (intern->r_type == ALPHA_R_LITUSE
551 || intern->r_type == ALPHA_R_GPDISP)
553 /* Handle the LITUSE and GPDISP relocs specially. Its symndx
554 value is not actually a symbol index, but is instead a
555 special code. We put the code in the r_size field, and
556 clobber the symndx. */
557 if (intern->r_size != 0)
558 abort ();
559 intern->r_size = intern->r_symndx;
560 intern->r_symndx = RELOC_SECTION_NONE;
562 else if (intern->r_type == ALPHA_R_IGNORE)
564 /* The IGNORE reloc generally follows a GPDISP reloc, and is
565 against the .lita section. The section is irrelevant. */
566 if (! intern->r_extern &&
567 intern->r_symndx == RELOC_SECTION_ABS)
568 abort ();
569 if (! intern->r_extern && intern->r_symndx == RELOC_SECTION_LITA)
570 intern->r_symndx = RELOC_SECTION_ABS;
574 /* Swap a reloc out. */
576 static void
577 alpha_ecoff_swap_reloc_out (abfd, intern, dst)
578 bfd *abfd;
579 const struct internal_reloc *intern;
580 PTR dst;
582 RELOC *ext = (RELOC *) dst;
583 long symndx;
584 unsigned char size;
586 /* Undo the hackery done in swap_reloc_in. */
587 if (intern->r_type == ALPHA_R_LITUSE
588 || intern->r_type == ALPHA_R_GPDISP)
590 symndx = intern->r_size;
591 size = 0;
593 else if (intern->r_type == ALPHA_R_IGNORE
594 && ! intern->r_extern
595 && intern->r_symndx == RELOC_SECTION_ABS)
597 symndx = RELOC_SECTION_LITA;
598 size = intern->r_size;
600 else
602 symndx = intern->r_symndx;
603 size = intern->r_size;
606 BFD_ASSERT (intern->r_extern
607 || (intern->r_symndx >= 0 && intern->r_symndx <= 14));
609 H_PUT_64 (abfd, intern->r_vaddr, ext->r_vaddr);
610 H_PUT_32 (abfd, symndx, ext->r_symndx);
612 BFD_ASSERT (bfd_header_little_endian (abfd));
614 ext->r_bits[0] = ((intern->r_type << RELOC_BITS0_TYPE_SH_LITTLE)
615 & RELOC_BITS0_TYPE_LITTLE);
616 ext->r_bits[1] = ((intern->r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0)
617 | ((intern->r_offset << RELOC_BITS1_OFFSET_SH_LITTLE)
618 & RELOC_BITS1_OFFSET_LITTLE));
619 ext->r_bits[2] = 0;
620 ext->r_bits[3] = ((size << RELOC_BITS3_SIZE_SH_LITTLE)
621 & RELOC_BITS3_SIZE_LITTLE);
624 /* Finish canonicalizing a reloc. Part of this is generic to all
625 ECOFF targets, and that part is in ecoff.c. The rest is done in
626 this backend routine. It must fill in the howto field. */
628 static void
629 alpha_adjust_reloc_in (abfd, intern, rptr)
630 bfd *abfd;
631 const struct internal_reloc *intern;
632 arelent *rptr;
634 if (intern->r_type > ALPHA_R_GPVALUE)
635 abort ();
637 switch (intern->r_type)
639 case ALPHA_R_BRADDR:
640 case ALPHA_R_SREL16:
641 case ALPHA_R_SREL32:
642 case ALPHA_R_SREL64:
643 /* This relocs appear to be fully resolved when they are against
644 internal symbols. Against external symbols, BRADDR at least
645 appears to be resolved against the next instruction. */
646 if (! intern->r_extern)
647 rptr->addend = 0;
648 else
649 rptr->addend = - (intern->r_vaddr + 4);
650 break;
652 case ALPHA_R_GPREL32:
653 case ALPHA_R_LITERAL:
654 /* Copy the gp value for this object file into the addend, to
655 ensure that we are not confused by the linker. */
656 if (! intern->r_extern)
657 rptr->addend += ecoff_data (abfd)->gp;
658 break;
660 case ALPHA_R_LITUSE:
661 case ALPHA_R_GPDISP:
662 /* The LITUSE and GPDISP relocs do not use a symbol, or an
663 addend, but they do use a special code. Put this code in the
664 addend field. */
665 rptr->addend = intern->r_size;
666 break;
668 case ALPHA_R_OP_STORE:
669 /* The STORE reloc needs the size and offset fields. We store
670 them in the addend. */
671 BFD_ASSERT (intern->r_offset <= 256 && intern->r_size <= 256);
672 rptr->addend = (intern->r_offset << 8) + intern->r_size;
673 break;
675 case ALPHA_R_OP_PUSH:
676 case ALPHA_R_OP_PSUB:
677 case ALPHA_R_OP_PRSHIFT:
678 /* The PUSH, PSUB and PRSHIFT relocs do not actually use an
679 address. I believe that the address supplied is really an
680 addend. */
681 rptr->addend = intern->r_vaddr;
682 break;
684 case ALPHA_R_GPVALUE:
685 /* Set the addend field to the new GP value. */
686 rptr->addend = intern->r_symndx + ecoff_data (abfd)->gp;
687 break;
689 case ALPHA_R_IGNORE:
690 /* If the type is ALPHA_R_IGNORE, make sure this is a reference
691 to the absolute section so that the reloc is ignored. For
692 some reason the address of this reloc type is not adjusted by
693 the section vma. We record the gp value for this object file
694 here, for convenience when doing the GPDISP relocation. */
695 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
696 rptr->address = intern->r_vaddr;
697 rptr->addend = ecoff_data (abfd)->gp;
698 break;
700 default:
701 break;
704 rptr->howto = &alpha_howto_table[intern->r_type];
707 /* When writing out a reloc we need to pull some values back out of
708 the addend field into the reloc. This is roughly the reverse of
709 alpha_adjust_reloc_in, except that there are several changes we do
710 not need to undo. */
712 static void
713 alpha_adjust_reloc_out (abfd, rel, intern)
714 bfd *abfd ATTRIBUTE_UNUSED;
715 const arelent *rel;
716 struct internal_reloc *intern;
718 switch (intern->r_type)
720 case ALPHA_R_LITUSE:
721 case ALPHA_R_GPDISP:
722 intern->r_size = rel->addend;
723 break;
725 case ALPHA_R_OP_STORE:
726 intern->r_size = rel->addend & 0xff;
727 intern->r_offset = (rel->addend >> 8) & 0xff;
728 break;
730 case ALPHA_R_OP_PUSH:
731 case ALPHA_R_OP_PSUB:
732 case ALPHA_R_OP_PRSHIFT:
733 intern->r_vaddr = rel->addend;
734 break;
736 case ALPHA_R_IGNORE:
737 intern->r_vaddr = rel->address;
738 break;
740 default:
741 break;
745 /* The size of the stack for the relocation evaluator. */
746 #define RELOC_STACKSIZE (10)
748 /* Alpha ECOFF relocs have a built in expression evaluator as well as
749 other interdependencies. Rather than use a bunch of special
750 functions and global variables, we use a single routine to do all
751 the relocation for a section. I haven't yet worked out how the
752 assembler is going to handle this. */
754 static bfd_byte *
755 alpha_ecoff_get_relocated_section_contents (abfd, link_info, link_order,
756 data, relocatable, symbols)
757 bfd *abfd;
758 struct bfd_link_info *link_info;
759 struct bfd_link_order *link_order;
760 bfd_byte *data;
761 bfd_boolean relocatable;
762 asymbol **symbols;
764 bfd *input_bfd = link_order->u.indirect.section->owner;
765 asection *input_section = link_order->u.indirect.section;
766 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
767 arelent **reloc_vector = NULL;
768 long reloc_count;
769 bfd *output_bfd = relocatable ? abfd : (bfd *) NULL;
770 bfd_vma gp;
771 bfd_size_type sz;
772 bfd_boolean gp_undefined;
773 bfd_vma stack[RELOC_STACKSIZE];
774 int tos = 0;
776 if (reloc_size < 0)
777 goto error_return;
778 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
779 if (reloc_vector == NULL && reloc_size != 0)
780 goto error_return;
782 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
783 if (! bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
784 goto error_return;
786 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
787 reloc_vector, symbols);
788 if (reloc_count < 0)
789 goto error_return;
790 if (reloc_count == 0)
791 goto successful_return;
793 /* Get the GP value for the output BFD. */
794 gp_undefined = FALSE;
795 gp = _bfd_get_gp_value (abfd);
796 if (gp == 0)
798 if (relocatable)
800 asection *sec;
801 bfd_vma lo;
803 /* Make up a value. */
804 lo = (bfd_vma) -1;
805 for (sec = abfd->sections; sec != NULL; sec = sec->next)
807 if (sec->vma < lo
808 && (strcmp (sec->name, ".sbss") == 0
809 || strcmp (sec->name, ".sdata") == 0
810 || strcmp (sec->name, ".lit4") == 0
811 || strcmp (sec->name, ".lit8") == 0
812 || strcmp (sec->name, ".lita") == 0))
813 lo = sec->vma;
815 gp = lo + 0x8000;
816 _bfd_set_gp_value (abfd, gp);
818 else
820 struct bfd_link_hash_entry *h;
822 h = bfd_link_hash_lookup (link_info->hash, "_gp", FALSE, FALSE,
823 TRUE);
824 if (h == (struct bfd_link_hash_entry *) NULL
825 || h->type != bfd_link_hash_defined)
826 gp_undefined = TRUE;
827 else
829 gp = (h->u.def.value
830 + h->u.def.section->output_section->vma
831 + h->u.def.section->output_offset);
832 _bfd_set_gp_value (abfd, gp);
837 for (; *reloc_vector != (arelent *) NULL; reloc_vector++)
839 arelent *rel;
840 bfd_reloc_status_type r;
841 char *err;
843 rel = *reloc_vector;
844 r = bfd_reloc_ok;
845 switch (rel->howto->type)
847 case ALPHA_R_IGNORE:
848 rel->address += input_section->output_offset;
849 break;
851 case ALPHA_R_REFLONG:
852 case ALPHA_R_REFQUAD:
853 case ALPHA_R_BRADDR:
854 case ALPHA_R_HINT:
855 case ALPHA_R_SREL16:
856 case ALPHA_R_SREL32:
857 case ALPHA_R_SREL64:
858 if (relocatable
859 && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
861 rel->address += input_section->output_offset;
862 break;
864 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
865 output_bfd, &err);
866 break;
868 case ALPHA_R_GPREL32:
869 /* This relocation is used in a switch table. It is a 32
870 bit offset from the current GP value. We must adjust it
871 by the different between the original GP value and the
872 current GP value. The original GP value is stored in the
873 addend. We adjust the addend and let
874 bfd_perform_relocation finish the job. */
875 rel->addend -= gp;
876 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
877 output_bfd, &err);
878 if (r == bfd_reloc_ok && gp_undefined)
880 r = bfd_reloc_dangerous;
881 err = (char *) _("GP relative relocation used when GP not defined");
883 break;
885 case ALPHA_R_LITERAL:
886 /* This is a reference to a literal value, generally
887 (always?) in the .lita section. This is a 16 bit GP
888 relative relocation. Sometimes the subsequent reloc is a
889 LITUSE reloc, which indicates how this reloc is used.
890 This sometimes permits rewriting the two instructions
891 referred to by the LITERAL and the LITUSE into different
892 instructions which do not refer to .lita. This can save
893 a memory reference, and permits removing a value from
894 .lita thus saving GP relative space.
896 We do not these optimizations. To do them we would need
897 to arrange to link the .lita section first, so that by
898 the time we got here we would know the final values to
899 use. This would not be particularly difficult, but it is
900 not currently implemented. */
903 unsigned long insn;
905 /* I believe that the LITERAL reloc will only apply to a
906 ldq or ldl instruction, so check my assumption. */
907 insn = bfd_get_32 (input_bfd, data + rel->address);
908 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
909 || ((insn >> 26) & 0x3f) == 0x28);
911 rel->addend -= gp;
912 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
913 output_bfd, &err);
914 if (r == bfd_reloc_ok && gp_undefined)
916 r = bfd_reloc_dangerous;
917 err =
918 (char *) _("GP relative relocation used when GP not defined");
921 break;
923 case ALPHA_R_LITUSE:
924 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
925 does not cause anything to happen, itself. */
926 rel->address += input_section->output_offset;
927 break;
929 case ALPHA_R_GPDISP:
930 /* This marks the ldah of an ldah/lda pair which loads the
931 gp register with the difference of the gp value and the
932 current location. The second of the pair is r_size bytes
933 ahead; it used to be marked with an ALPHA_R_IGNORE reloc,
934 but that no longer happens in OSF/1 3.2. */
936 unsigned long insn1, insn2;
937 bfd_vma addend;
939 /* Get the two instructions. */
940 insn1 = bfd_get_32 (input_bfd, data + rel->address);
941 insn2 = bfd_get_32 (input_bfd, data + rel->address + rel->addend);
943 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
944 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
946 /* Get the existing addend. We must account for the sign
947 extension done by lda and ldah. */
948 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
949 if (insn1 & 0x8000)
951 addend -= 0x80000000;
952 addend -= 0x80000000;
954 if (insn2 & 0x8000)
955 addend -= 0x10000;
957 /* The existing addend includes the different between the
958 gp of the input BFD and the address in the input BFD.
959 Subtract this out. */
960 addend -= (ecoff_data (input_bfd)->gp
961 - (input_section->vma + rel->address));
963 /* Now add in the final gp value, and subtract out the
964 final address. */
965 addend += (gp
966 - (input_section->output_section->vma
967 + input_section->output_offset
968 + rel->address));
970 /* Change the instructions, accounting for the sign
971 extension, and write them out. */
972 if (addend & 0x8000)
973 addend += 0x10000;
974 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
975 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
977 bfd_put_32 (input_bfd, (bfd_vma) insn1, data + rel->address);
978 bfd_put_32 (input_bfd, (bfd_vma) insn2,
979 data + rel->address + rel->addend);
981 rel->address += input_section->output_offset;
983 break;
985 case ALPHA_R_OP_PUSH:
986 /* Push a value on the reloc evaluation stack. */
988 asymbol *symbol;
989 bfd_vma relocation;
991 if (relocatable)
993 rel->address += input_section->output_offset;
994 break;
997 /* Figure out the relocation of this symbol. */
998 symbol = *rel->sym_ptr_ptr;
1000 if (bfd_is_und_section (symbol->section))
1001 r = bfd_reloc_undefined;
1003 if (bfd_is_com_section (symbol->section))
1004 relocation = 0;
1005 else
1006 relocation = symbol->value;
1007 relocation += symbol->section->output_section->vma;
1008 relocation += symbol->section->output_offset;
1009 relocation += rel->addend;
1011 if (tos >= RELOC_STACKSIZE)
1012 abort ();
1014 stack[tos++] = relocation;
1016 break;
1018 case ALPHA_R_OP_STORE:
1019 /* Store a value from the reloc stack into a bitfield. */
1021 bfd_vma val;
1022 int offset, size;
1024 if (relocatable)
1026 rel->address += input_section->output_offset;
1027 break;
1030 if (tos == 0)
1031 abort ();
1033 /* The offset and size for this reloc are encoded into the
1034 addend field by alpha_adjust_reloc_in. */
1035 offset = (rel->addend >> 8) & 0xff;
1036 size = rel->addend & 0xff;
1038 val = bfd_get_64 (abfd, data + rel->address);
1039 val &=~ (((1 << size) - 1) << offset);
1040 val |= (stack[--tos] & ((1 << size) - 1)) << offset;
1041 bfd_put_64 (abfd, val, data + rel->address);
1043 break;
1045 case ALPHA_R_OP_PSUB:
1046 /* Subtract a value from the top of the stack. */
1048 asymbol *symbol;
1049 bfd_vma relocation;
1051 if (relocatable)
1053 rel->address += input_section->output_offset;
1054 break;
1057 /* Figure out the relocation of this symbol. */
1058 symbol = *rel->sym_ptr_ptr;
1060 if (bfd_is_und_section (symbol->section))
1061 r = bfd_reloc_undefined;
1063 if (bfd_is_com_section (symbol->section))
1064 relocation = 0;
1065 else
1066 relocation = symbol->value;
1067 relocation += symbol->section->output_section->vma;
1068 relocation += symbol->section->output_offset;
1069 relocation += rel->addend;
1071 if (tos == 0)
1072 abort ();
1074 stack[tos - 1] -= relocation;
1076 break;
1078 case ALPHA_R_OP_PRSHIFT:
1079 /* Shift the value on the top of the stack. */
1081 asymbol *symbol;
1082 bfd_vma relocation;
1084 if (relocatable)
1086 rel->address += input_section->output_offset;
1087 break;
1090 /* Figure out the relocation of this symbol. */
1091 symbol = *rel->sym_ptr_ptr;
1093 if (bfd_is_und_section (symbol->section))
1094 r = bfd_reloc_undefined;
1096 if (bfd_is_com_section (symbol->section))
1097 relocation = 0;
1098 else
1099 relocation = symbol->value;
1100 relocation += symbol->section->output_section->vma;
1101 relocation += symbol->section->output_offset;
1102 relocation += rel->addend;
1104 if (tos == 0)
1105 abort ();
1107 stack[tos - 1] >>= relocation;
1109 break;
1111 case ALPHA_R_GPVALUE:
1112 /* I really don't know if this does the right thing. */
1113 gp = rel->addend;
1114 gp_undefined = FALSE;
1115 break;
1117 default:
1118 abort ();
1121 if (relocatable)
1123 asection *os = input_section->output_section;
1125 /* A partial link, so keep the relocs. */
1126 os->orelocation[os->reloc_count] = rel;
1127 os->reloc_count++;
1130 if (r != bfd_reloc_ok)
1132 switch (r)
1134 case bfd_reloc_undefined:
1135 if (! ((*link_info->callbacks->undefined_symbol)
1136 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1137 input_bfd, input_section, rel->address, TRUE)))
1138 goto error_return;
1139 break;
1140 case bfd_reloc_dangerous:
1141 if (! ((*link_info->callbacks->reloc_dangerous)
1142 (link_info, err, input_bfd, input_section,
1143 rel->address)))
1144 goto error_return;
1145 break;
1146 case bfd_reloc_overflow:
1147 if (! ((*link_info->callbacks->reloc_overflow)
1148 (link_info, NULL,
1149 bfd_asymbol_name (*rel->sym_ptr_ptr),
1150 rel->howto->name, rel->addend, input_bfd,
1151 input_section, rel->address)))
1152 goto error_return;
1153 break;
1154 case bfd_reloc_outofrange:
1155 default:
1156 abort ();
1157 break;
1162 if (tos != 0)
1163 abort ();
1165 successful_return:
1166 if (reloc_vector != NULL)
1167 free (reloc_vector);
1168 return data;
1170 error_return:
1171 if (reloc_vector != NULL)
1172 free (reloc_vector);
1173 return NULL;
1176 /* Get the howto structure for a generic reloc type. */
1178 static reloc_howto_type *
1179 alpha_bfd_reloc_type_lookup (abfd, code)
1180 bfd *abfd ATTRIBUTE_UNUSED;
1181 bfd_reloc_code_real_type code;
1183 int alpha_type;
1185 switch (code)
1187 case BFD_RELOC_32:
1188 alpha_type = ALPHA_R_REFLONG;
1189 break;
1190 case BFD_RELOC_64:
1191 case BFD_RELOC_CTOR:
1192 alpha_type = ALPHA_R_REFQUAD;
1193 break;
1194 case BFD_RELOC_GPREL32:
1195 alpha_type = ALPHA_R_GPREL32;
1196 break;
1197 case BFD_RELOC_ALPHA_LITERAL:
1198 alpha_type = ALPHA_R_LITERAL;
1199 break;
1200 case BFD_RELOC_ALPHA_LITUSE:
1201 alpha_type = ALPHA_R_LITUSE;
1202 break;
1203 case BFD_RELOC_ALPHA_GPDISP_HI16:
1204 alpha_type = ALPHA_R_GPDISP;
1205 break;
1206 case BFD_RELOC_ALPHA_GPDISP_LO16:
1207 alpha_type = ALPHA_R_IGNORE;
1208 break;
1209 case BFD_RELOC_23_PCREL_S2:
1210 alpha_type = ALPHA_R_BRADDR;
1211 break;
1212 case BFD_RELOC_ALPHA_HINT:
1213 alpha_type = ALPHA_R_HINT;
1214 break;
1215 case BFD_RELOC_16_PCREL:
1216 alpha_type = ALPHA_R_SREL16;
1217 break;
1218 case BFD_RELOC_32_PCREL:
1219 alpha_type = ALPHA_R_SREL32;
1220 break;
1221 case BFD_RELOC_64_PCREL:
1222 alpha_type = ALPHA_R_SREL64;
1223 break;
1224 default:
1225 return (reloc_howto_type *) NULL;
1228 return &alpha_howto_table[alpha_type];
1231 /* A helper routine for alpha_relocate_section which converts an
1232 external reloc when generating relocatable output. Returns the
1233 relocation amount. */
1235 static bfd_vma
1236 alpha_convert_external_reloc (output_bfd, info, input_bfd, ext_rel, h)
1237 bfd *output_bfd ATTRIBUTE_UNUSED;
1238 struct bfd_link_info *info;
1239 bfd *input_bfd;
1240 struct external_reloc *ext_rel;
1241 struct ecoff_link_hash_entry *h;
1243 unsigned long r_symndx;
1244 bfd_vma relocation;
1246 BFD_ASSERT (info->relocatable);
1248 if (h->root.type == bfd_link_hash_defined
1249 || h->root.type == bfd_link_hash_defweak)
1251 asection *hsec;
1252 const char *name;
1254 /* This symbol is defined in the output. Convert the reloc from
1255 being against the symbol to being against the section. */
1257 /* Clear the r_extern bit. */
1258 ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE;
1260 /* Compute a new r_symndx value. */
1261 hsec = h->root.u.def.section;
1262 name = bfd_get_section_name (output_bfd, hsec->output_section);
1264 r_symndx = (unsigned long) -1;
1265 switch (name[1])
1267 case 'A':
1268 if (strcmp (name, "*ABS*") == 0)
1269 r_symndx = RELOC_SECTION_ABS;
1270 break;
1271 case 'b':
1272 if (strcmp (name, ".bss") == 0)
1273 r_symndx = RELOC_SECTION_BSS;
1274 break;
1275 case 'd':
1276 if (strcmp (name, ".data") == 0)
1277 r_symndx = RELOC_SECTION_DATA;
1278 break;
1279 case 'f':
1280 if (strcmp (name, ".fini") == 0)
1281 r_symndx = RELOC_SECTION_FINI;
1282 break;
1283 case 'i':
1284 if (strcmp (name, ".init") == 0)
1285 r_symndx = RELOC_SECTION_INIT;
1286 break;
1287 case 'l':
1288 if (strcmp (name, ".lita") == 0)
1289 r_symndx = RELOC_SECTION_LITA;
1290 else if (strcmp (name, ".lit8") == 0)
1291 r_symndx = RELOC_SECTION_LIT8;
1292 else if (strcmp (name, ".lit4") == 0)
1293 r_symndx = RELOC_SECTION_LIT4;
1294 break;
1295 case 'p':
1296 if (strcmp (name, ".pdata") == 0)
1297 r_symndx = RELOC_SECTION_PDATA;
1298 break;
1299 case 'r':
1300 if (strcmp (name, ".rdata") == 0)
1301 r_symndx = RELOC_SECTION_RDATA;
1302 else if (strcmp (name, ".rconst") == 0)
1303 r_symndx = RELOC_SECTION_RCONST;
1304 break;
1305 case 's':
1306 if (strcmp (name, ".sdata") == 0)
1307 r_symndx = RELOC_SECTION_SDATA;
1308 else if (strcmp (name, ".sbss") == 0)
1309 r_symndx = RELOC_SECTION_SBSS;
1310 break;
1311 case 't':
1312 if (strcmp (name, ".text") == 0)
1313 r_symndx = RELOC_SECTION_TEXT;
1314 break;
1315 case 'x':
1316 if (strcmp (name, ".xdata") == 0)
1317 r_symndx = RELOC_SECTION_XDATA;
1318 break;
1321 if (r_symndx == (unsigned long) -1)
1322 abort ();
1324 /* Add the section VMA and the symbol value. */
1325 relocation = (h->root.u.def.value
1326 + hsec->output_section->vma
1327 + hsec->output_offset);
1329 else
1331 /* Change the symndx value to the right one for
1332 the output BFD. */
1333 r_symndx = h->indx;
1334 if (r_symndx == (unsigned long) -1)
1336 /* Caller must give an error. */
1337 r_symndx = 0;
1339 relocation = 0;
1342 /* Write out the new r_symndx value. */
1343 H_PUT_32 (input_bfd, r_symndx, ext_rel->r_symndx);
1345 return relocation;
1348 /* Relocate a section while linking an Alpha ECOFF file. This is
1349 quite similar to get_relocated_section_contents. Perhaps they
1350 could be combined somehow. */
1352 static bfd_boolean
1353 alpha_relocate_section (output_bfd, info, input_bfd, input_section,
1354 contents, external_relocs)
1355 bfd *output_bfd;
1356 struct bfd_link_info *info;
1357 bfd *input_bfd;
1358 asection *input_section;
1359 bfd_byte *contents;
1360 PTR external_relocs;
1362 asection **symndx_to_section, *lita_sec;
1363 struct ecoff_link_hash_entry **sym_hashes;
1364 bfd_vma gp;
1365 bfd_boolean gp_undefined;
1366 bfd_vma stack[RELOC_STACKSIZE];
1367 int tos = 0;
1368 struct external_reloc *ext_rel;
1369 struct external_reloc *ext_rel_end;
1370 bfd_size_type amt;
1372 /* We keep a table mapping the symndx found in an internal reloc to
1373 the appropriate section. This is faster than looking up the
1374 section by name each time. */
1375 symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
1376 if (symndx_to_section == (asection **) NULL)
1378 amt = NUM_RELOC_SECTIONS * sizeof (asection *);
1379 symndx_to_section = (asection **) bfd_alloc (input_bfd, amt);
1380 if (!symndx_to_section)
1381 return FALSE;
1383 symndx_to_section[RELOC_SECTION_NONE] = NULL;
1384 symndx_to_section[RELOC_SECTION_TEXT] =
1385 bfd_get_section_by_name (input_bfd, ".text");
1386 symndx_to_section[RELOC_SECTION_RDATA] =
1387 bfd_get_section_by_name (input_bfd, ".rdata");
1388 symndx_to_section[RELOC_SECTION_DATA] =
1389 bfd_get_section_by_name (input_bfd, ".data");
1390 symndx_to_section[RELOC_SECTION_SDATA] =
1391 bfd_get_section_by_name (input_bfd, ".sdata");
1392 symndx_to_section[RELOC_SECTION_SBSS] =
1393 bfd_get_section_by_name (input_bfd, ".sbss");
1394 symndx_to_section[RELOC_SECTION_BSS] =
1395 bfd_get_section_by_name (input_bfd, ".bss");
1396 symndx_to_section[RELOC_SECTION_INIT] =
1397 bfd_get_section_by_name (input_bfd, ".init");
1398 symndx_to_section[RELOC_SECTION_LIT8] =
1399 bfd_get_section_by_name (input_bfd, ".lit8");
1400 symndx_to_section[RELOC_SECTION_LIT4] =
1401 bfd_get_section_by_name (input_bfd, ".lit4");
1402 symndx_to_section[RELOC_SECTION_XDATA] =
1403 bfd_get_section_by_name (input_bfd, ".xdata");
1404 symndx_to_section[RELOC_SECTION_PDATA] =
1405 bfd_get_section_by_name (input_bfd, ".pdata");
1406 symndx_to_section[RELOC_SECTION_FINI] =
1407 bfd_get_section_by_name (input_bfd, ".fini");
1408 symndx_to_section[RELOC_SECTION_LITA] =
1409 bfd_get_section_by_name (input_bfd, ".lita");
1410 symndx_to_section[RELOC_SECTION_ABS] = bfd_abs_section_ptr;
1411 symndx_to_section[RELOC_SECTION_RCONST] =
1412 bfd_get_section_by_name (input_bfd, ".rconst");
1414 ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
1417 sym_hashes = ecoff_data (input_bfd)->sym_hashes;
1419 /* On the Alpha, the .lita section must be addressable by the global
1420 pointer. To support large programs, we need to allow multiple
1421 global pointers. This works as long as each input .lita section
1422 is <64KB big. This implies that when producing relocatable
1423 output, the .lita section is limited to 64KB. . */
1425 lita_sec = symndx_to_section[RELOC_SECTION_LITA];
1426 gp = _bfd_get_gp_value (output_bfd);
1427 if (! info->relocatable && lita_sec != NULL)
1429 struct ecoff_section_tdata *lita_sec_data;
1431 /* Make sure we have a section data structure to which we can
1432 hang on to the gp value we pick for the section. */
1433 lita_sec_data = ecoff_section_data (input_bfd, lita_sec);
1434 if (lita_sec_data == NULL)
1436 amt = sizeof (struct ecoff_section_tdata);
1437 lita_sec_data = ((struct ecoff_section_tdata *)
1438 bfd_zalloc (input_bfd, amt));
1439 lita_sec->used_by_bfd = lita_sec_data;
1442 if (lita_sec_data->gp != 0)
1444 /* If we already assigned a gp to this section, we better
1445 stick with that value. */
1446 gp = lita_sec_data->gp;
1448 else
1450 bfd_vma lita_vma;
1451 bfd_size_type lita_size;
1453 lita_vma = lita_sec->output_offset + lita_sec->output_section->vma;
1454 lita_size = lita_sec->size;
1456 if (gp == 0
1457 || lita_vma < gp - 0x8000
1458 || lita_vma + lita_size >= gp + 0x8000)
1460 /* Either gp hasn't been set at all or the current gp
1461 cannot address this .lita section. In both cases we
1462 reset the gp to point into the "middle" of the
1463 current input .lita section. */
1464 if (gp && !ecoff_data (output_bfd)->issued_multiple_gp_warning)
1466 (*info->callbacks->warning) (info,
1467 _("using multiple gp values"),
1468 (char *) NULL, output_bfd,
1469 (asection *) NULL, (bfd_vma) 0);
1470 ecoff_data (output_bfd)->issued_multiple_gp_warning = TRUE;
1472 if (lita_vma < gp - 0x8000)
1473 gp = lita_vma + lita_size - 0x8000;
1474 else
1475 gp = lita_vma + 0x8000;
1479 lita_sec_data->gp = gp;
1482 _bfd_set_gp_value (output_bfd, gp);
1485 gp_undefined = (gp == 0);
1487 BFD_ASSERT (bfd_header_little_endian (output_bfd));
1488 BFD_ASSERT (bfd_header_little_endian (input_bfd));
1490 ext_rel = (struct external_reloc *) external_relocs;
1491 ext_rel_end = ext_rel + input_section->reloc_count;
1492 for (; ext_rel < ext_rel_end; ext_rel++)
1494 bfd_vma r_vaddr;
1495 unsigned long r_symndx;
1496 int r_type;
1497 int r_extern;
1498 int r_offset;
1499 int r_size;
1500 bfd_boolean relocatep;
1501 bfd_boolean adjust_addrp;
1502 bfd_boolean gp_usedp;
1503 bfd_vma addend;
1505 r_vaddr = H_GET_64 (input_bfd, ext_rel->r_vaddr);
1506 r_symndx = H_GET_32 (input_bfd, ext_rel->r_symndx);
1508 r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
1509 >> RELOC_BITS0_TYPE_SH_LITTLE);
1510 r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
1511 r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
1512 >> RELOC_BITS1_OFFSET_SH_LITTLE);
1513 /* Ignored the reserved bits. */
1514 r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
1515 >> RELOC_BITS3_SIZE_SH_LITTLE);
1517 relocatep = FALSE;
1518 adjust_addrp = TRUE;
1519 gp_usedp = FALSE;
1520 addend = 0;
1522 switch (r_type)
1524 default:
1525 abort ();
1527 case ALPHA_R_IGNORE:
1528 /* This reloc appears after a GPDISP reloc. On earlier
1529 versions of OSF/1, It marked the position of the second
1530 instruction to be altered by the GPDISP reloc, but it is
1531 not otherwise used for anything. For some reason, the
1532 address of the relocation does not appear to include the
1533 section VMA, unlike the other relocation types. */
1534 if (info->relocatable)
1535 H_PUT_64 (input_bfd, input_section->output_offset + r_vaddr,
1536 ext_rel->r_vaddr);
1537 adjust_addrp = FALSE;
1538 break;
1540 case ALPHA_R_REFLONG:
1541 case ALPHA_R_REFQUAD:
1542 case ALPHA_R_HINT:
1543 relocatep = TRUE;
1544 break;
1546 case ALPHA_R_BRADDR:
1547 case ALPHA_R_SREL16:
1548 case ALPHA_R_SREL32:
1549 case ALPHA_R_SREL64:
1550 if (r_extern)
1551 addend += - (r_vaddr + 4);
1552 relocatep = TRUE;
1553 break;
1555 case ALPHA_R_GPREL32:
1556 /* This relocation is used in a switch table. It is a 32
1557 bit offset from the current GP value. We must adjust it
1558 by the different between the original GP value and the
1559 current GP value. */
1560 relocatep = TRUE;
1561 addend = ecoff_data (input_bfd)->gp - gp;
1562 gp_usedp = TRUE;
1563 break;
1565 case ALPHA_R_LITERAL:
1566 /* This is a reference to a literal value, generally
1567 (always?) in the .lita section. This is a 16 bit GP
1568 relative relocation. Sometimes the subsequent reloc is a
1569 LITUSE reloc, which indicates how this reloc is used.
1570 This sometimes permits rewriting the two instructions
1571 referred to by the LITERAL and the LITUSE into different
1572 instructions which do not refer to .lita. This can save
1573 a memory reference, and permits removing a value from
1574 .lita thus saving GP relative space.
1576 We do not these optimizations. To do them we would need
1577 to arrange to link the .lita section first, so that by
1578 the time we got here we would know the final values to
1579 use. This would not be particularly difficult, but it is
1580 not currently implemented. */
1582 /* I believe that the LITERAL reloc will only apply to a ldq
1583 or ldl instruction, so check my assumption. */
1585 unsigned long insn;
1587 insn = bfd_get_32 (input_bfd,
1588 contents + r_vaddr - input_section->vma);
1589 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29
1590 || ((insn >> 26) & 0x3f) == 0x28);
1593 relocatep = TRUE;
1594 addend = ecoff_data (input_bfd)->gp - gp;
1595 gp_usedp = TRUE;
1596 break;
1598 case ALPHA_R_LITUSE:
1599 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
1600 does not cause anything to happen, itself. */
1601 break;
1603 case ALPHA_R_GPDISP:
1604 /* This marks the ldah of an ldah/lda pair which loads the
1605 gp register with the difference of the gp value and the
1606 current location. The second of the pair is r_symndx
1607 bytes ahead. It used to be marked with an ALPHA_R_IGNORE
1608 reloc, but OSF/1 3.2 no longer does that. */
1610 unsigned long insn1, insn2;
1612 /* Get the two instructions. */
1613 insn1 = bfd_get_32 (input_bfd,
1614 contents + r_vaddr - input_section->vma);
1615 insn2 = bfd_get_32 (input_bfd,
1616 (contents
1617 + r_vaddr
1618 - input_section->vma
1619 + r_symndx));
1621 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
1622 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
1624 /* Get the existing addend. We must account for the sign
1625 extension done by lda and ldah. */
1626 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
1627 if (insn1 & 0x8000)
1629 /* This is addend -= 0x100000000 without causing an
1630 integer overflow on a 32 bit host. */
1631 addend -= 0x80000000;
1632 addend -= 0x80000000;
1634 if (insn2 & 0x8000)
1635 addend -= 0x10000;
1637 /* The existing addend includes the difference between the
1638 gp of the input BFD and the address in the input BFD.
1639 We want to change this to the difference between the
1640 final GP and the final address. */
1641 addend += (gp
1642 - ecoff_data (input_bfd)->gp
1643 + input_section->vma
1644 - (input_section->output_section->vma
1645 + input_section->output_offset));
1647 /* Change the instructions, accounting for the sign
1648 extension, and write them out. */
1649 if (addend & 0x8000)
1650 addend += 0x10000;
1651 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
1652 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
1654 bfd_put_32 (input_bfd, (bfd_vma) insn1,
1655 contents + r_vaddr - input_section->vma);
1656 bfd_put_32 (input_bfd, (bfd_vma) insn2,
1657 contents + r_vaddr - input_section->vma + r_symndx);
1659 gp_usedp = TRUE;
1661 break;
1663 case ALPHA_R_OP_PUSH:
1664 case ALPHA_R_OP_PSUB:
1665 case ALPHA_R_OP_PRSHIFT:
1666 /* Manipulate values on the reloc evaluation stack. The
1667 r_vaddr field is not an address in input_section, it is
1668 the current value (including any addend) of the object
1669 being used. */
1670 if (! r_extern)
1672 asection *s;
1674 s = symndx_to_section[r_symndx];
1675 if (s == (asection *) NULL)
1676 abort ();
1677 addend = s->output_section->vma + s->output_offset - s->vma;
1679 else
1681 struct ecoff_link_hash_entry *h;
1683 h = sym_hashes[r_symndx];
1684 if (h == (struct ecoff_link_hash_entry *) NULL)
1685 abort ();
1687 if (! info->relocatable)
1689 if (h->root.type == bfd_link_hash_defined
1690 || h->root.type == bfd_link_hash_defweak)
1691 addend = (h->root.u.def.value
1692 + h->root.u.def.section->output_section->vma
1693 + h->root.u.def.section->output_offset);
1694 else
1696 /* Note that we pass the address as 0, since we
1697 do not have a meaningful number for the
1698 location within the section that is being
1699 relocated. */
1700 if (! ((*info->callbacks->undefined_symbol)
1701 (info, h->root.root.string, input_bfd,
1702 input_section, (bfd_vma) 0, TRUE)))
1703 return FALSE;
1704 addend = 0;
1707 else
1709 if (h->root.type != bfd_link_hash_defined
1710 && h->root.type != bfd_link_hash_defweak
1711 && h->indx == -1)
1713 /* This symbol is not being written out. Pass
1714 the address as 0, as with undefined_symbol,
1715 above. */
1716 if (! ((*info->callbacks->unattached_reloc)
1717 (info, h->root.root.string, input_bfd,
1718 input_section, (bfd_vma) 0)))
1719 return FALSE;
1722 addend = alpha_convert_external_reloc (output_bfd, info,
1723 input_bfd,
1724 ext_rel, h);
1728 addend += r_vaddr;
1730 if (info->relocatable)
1732 /* Adjust r_vaddr by the addend. */
1733 H_PUT_64 (input_bfd, addend, ext_rel->r_vaddr);
1735 else
1737 switch (r_type)
1739 case ALPHA_R_OP_PUSH:
1740 if (tos >= RELOC_STACKSIZE)
1741 abort ();
1742 stack[tos++] = addend;
1743 break;
1745 case ALPHA_R_OP_PSUB:
1746 if (tos == 0)
1747 abort ();
1748 stack[tos - 1] -= addend;
1749 break;
1751 case ALPHA_R_OP_PRSHIFT:
1752 if (tos == 0)
1753 abort ();
1754 stack[tos - 1] >>= addend;
1755 break;
1759 adjust_addrp = FALSE;
1760 break;
1762 case ALPHA_R_OP_STORE:
1763 /* Store a value from the reloc stack into a bitfield. If
1764 we are generating relocatable output, all we do is
1765 adjust the address of the reloc. */
1766 if (! info->relocatable)
1768 bfd_vma mask;
1769 bfd_vma val;
1771 if (tos == 0)
1772 abort ();
1774 /* Get the relocation mask. The separate steps and the
1775 casts to bfd_vma are attempts to avoid a bug in the
1776 Alpha OSF 1.3 C compiler. See reloc.c for more
1777 details. */
1778 mask = 1;
1779 mask <<= (bfd_vma) r_size;
1780 mask -= 1;
1782 /* FIXME: I don't know what kind of overflow checking,
1783 if any, should be done here. */
1784 val = bfd_get_64 (input_bfd,
1785 contents + r_vaddr - input_section->vma);
1786 val &=~ mask << (bfd_vma) r_offset;
1787 val |= (stack[--tos] & mask) << (bfd_vma) r_offset;
1788 bfd_put_64 (input_bfd, val,
1789 contents + r_vaddr - input_section->vma);
1791 break;
1793 case ALPHA_R_GPVALUE:
1794 /* I really don't know if this does the right thing. */
1795 gp = ecoff_data (input_bfd)->gp + r_symndx;
1796 gp_undefined = FALSE;
1797 break;
1800 if (relocatep)
1802 reloc_howto_type *howto;
1803 struct ecoff_link_hash_entry *h = NULL;
1804 asection *s = NULL;
1805 bfd_vma relocation;
1806 bfd_reloc_status_type r;
1808 /* Perform a relocation. */
1810 howto = &alpha_howto_table[r_type];
1812 if (r_extern)
1814 h = sym_hashes[r_symndx];
1815 /* If h is NULL, that means that there is a reloc
1816 against an external symbol which we thought was just
1817 a debugging symbol. This should not happen. */
1818 if (h == (struct ecoff_link_hash_entry *) NULL)
1819 abort ();
1821 else
1823 if (r_symndx >= NUM_RELOC_SECTIONS)
1824 s = NULL;
1825 else
1826 s = symndx_to_section[r_symndx];
1828 if (s == (asection *) NULL)
1829 abort ();
1832 if (info->relocatable)
1834 /* We are generating relocatable output, and must
1835 convert the existing reloc. */
1836 if (r_extern)
1838 if (h->root.type != bfd_link_hash_defined
1839 && h->root.type != bfd_link_hash_defweak
1840 && h->indx == -1)
1842 /* This symbol is not being written out. */
1843 if (! ((*info->callbacks->unattached_reloc)
1844 (info, h->root.root.string, input_bfd,
1845 input_section, r_vaddr - input_section->vma)))
1846 return FALSE;
1849 relocation = alpha_convert_external_reloc (output_bfd,
1850 info,
1851 input_bfd,
1852 ext_rel,
1855 else
1857 /* This is a relocation against a section. Adjust
1858 the value by the amount the section moved. */
1859 relocation = (s->output_section->vma
1860 + s->output_offset
1861 - s->vma);
1864 /* If this is PC relative, the existing object file
1865 appears to already have the reloc worked out. We
1866 must subtract out the old value and add in the new
1867 one. */
1868 if (howto->pc_relative)
1869 relocation -= (input_section->output_section->vma
1870 + input_section->output_offset
1871 - input_section->vma);
1873 /* Put in any addend. */
1874 relocation += addend;
1876 /* Adjust the contents. */
1877 r = _bfd_relocate_contents (howto, input_bfd, relocation,
1878 (contents
1879 + r_vaddr
1880 - input_section->vma));
1882 else
1884 /* We are producing a final executable. */
1885 if (r_extern)
1887 /* This is a reloc against a symbol. */
1888 if (h->root.type == bfd_link_hash_defined
1889 || h->root.type == bfd_link_hash_defweak)
1891 asection *hsec;
1893 hsec = h->root.u.def.section;
1894 relocation = (h->root.u.def.value
1895 + hsec->output_section->vma
1896 + hsec->output_offset);
1898 else
1900 if (! ((*info->callbacks->undefined_symbol)
1901 (info, h->root.root.string, input_bfd,
1902 input_section,
1903 r_vaddr - input_section->vma, TRUE)))
1904 return FALSE;
1905 relocation = 0;
1908 else
1910 /* This is a reloc against a section. */
1911 relocation = (s->output_section->vma
1912 + s->output_offset
1913 - s->vma);
1915 /* Adjust a PC relative relocation by removing the
1916 reference to the original source section. */
1917 if (howto->pc_relative)
1918 relocation += input_section->vma;
1921 r = _bfd_final_link_relocate (howto,
1922 input_bfd,
1923 input_section,
1924 contents,
1925 r_vaddr - input_section->vma,
1926 relocation,
1927 addend);
1930 if (r != bfd_reloc_ok)
1932 switch (r)
1934 default:
1935 case bfd_reloc_outofrange:
1936 abort ();
1937 case bfd_reloc_overflow:
1939 const char *name;
1941 if (r_extern)
1942 name = sym_hashes[r_symndx]->root.root.string;
1943 else
1944 name = bfd_section_name (input_bfd,
1945 symndx_to_section[r_symndx]);
1946 if (! ((*info->callbacks->reloc_overflow)
1947 (info, NULL, name,
1948 alpha_howto_table[r_type].name,
1949 (bfd_vma) 0, input_bfd, input_section,
1950 r_vaddr - input_section->vma)))
1951 return FALSE;
1953 break;
1958 if (info->relocatable && adjust_addrp)
1960 /* Change the address of the relocation. */
1961 H_PUT_64 (input_bfd,
1962 (input_section->output_section->vma
1963 + input_section->output_offset
1964 - input_section->vma
1965 + r_vaddr),
1966 ext_rel->r_vaddr);
1969 if (gp_usedp && gp_undefined)
1971 if (! ((*info->callbacks->reloc_dangerous)
1972 (info, _("GP relative relocation used when GP not defined"),
1973 input_bfd, input_section, r_vaddr - input_section->vma)))
1974 return FALSE;
1975 /* Only give the error once per link. */
1976 gp = 4;
1977 _bfd_set_gp_value (output_bfd, gp);
1978 gp_undefined = FALSE;
1982 if (tos != 0)
1983 abort ();
1985 return TRUE;
1988 /* Do final adjustments to the filehdr and the aouthdr. This routine
1989 sets the dynamic bits in the file header. */
1991 static bfd_boolean
1992 alpha_adjust_headers (abfd, fhdr, ahdr)
1993 bfd *abfd;
1994 struct internal_filehdr *fhdr;
1995 struct internal_aouthdr *ahdr ATTRIBUTE_UNUSED;
1997 if ((abfd->flags & (DYNAMIC | EXEC_P)) == (DYNAMIC | EXEC_P))
1998 fhdr->f_flags |= F_ALPHA_CALL_SHARED;
1999 else if ((abfd->flags & DYNAMIC) != 0)
2000 fhdr->f_flags |= F_ALPHA_SHARABLE;
2001 return TRUE;
2004 /* Archive handling. In OSF/1 (or Digital Unix) v3.2, Digital
2005 introduced archive packing, in which the elements in an archive are
2006 optionally compressed using a simple dictionary scheme. We know
2007 how to read such archives, but we don't write them. */
2009 #define alpha_ecoff_slurp_armap _bfd_ecoff_slurp_armap
2010 #define alpha_ecoff_slurp_extended_name_table \
2011 _bfd_ecoff_slurp_extended_name_table
2012 #define alpha_ecoff_construct_extended_name_table \
2013 _bfd_ecoff_construct_extended_name_table
2014 #define alpha_ecoff_truncate_arname _bfd_ecoff_truncate_arname
2015 #define alpha_ecoff_write_armap _bfd_ecoff_write_armap
2016 #define alpha_ecoff_generic_stat_arch_elt _bfd_ecoff_generic_stat_arch_elt
2017 #define alpha_ecoff_update_armap_timestamp _bfd_ecoff_update_armap_timestamp
2019 /* A compressed file uses this instead of ARFMAG. */
2021 #define ARFZMAG "Z\012"
2023 /* Read an archive header. This is like the standard routine, but it
2024 also accepts ARFZMAG. */
2026 static PTR
2027 alpha_ecoff_read_ar_hdr (abfd)
2028 bfd *abfd;
2030 struct areltdata *ret;
2031 struct ar_hdr *h;
2033 ret = (struct areltdata *) _bfd_generic_read_ar_hdr_mag (abfd, ARFZMAG);
2034 if (ret == NULL)
2035 return NULL;
2037 h = (struct ar_hdr *) ret->arch_header;
2038 if (strncmp (h->ar_fmag, ARFZMAG, 2) == 0)
2040 bfd_byte ab[8];
2042 /* This is a compressed file. We must set the size correctly.
2043 The size is the eight bytes after the dummy file header. */
2044 if (bfd_seek (abfd, (file_ptr) FILHSZ, SEEK_CUR) != 0
2045 || bfd_bread (ab, (bfd_size_type) 8, abfd) != 8
2046 || bfd_seek (abfd, (file_ptr) (- (FILHSZ + 8)), SEEK_CUR) != 0)
2047 return NULL;
2049 ret->parsed_size = H_GET_64 (abfd, ab);
2052 return (PTR) ret;
2055 /* Get an archive element at a specified file position. This is where
2056 we uncompress the archive element if necessary. */
2058 static bfd *
2059 alpha_ecoff_get_elt_at_filepos (archive, filepos)
2060 bfd *archive;
2061 file_ptr filepos;
2063 bfd *nbfd = NULL;
2064 struct areltdata *tdata;
2065 struct ar_hdr *hdr;
2066 bfd_byte ab[8];
2067 bfd_size_type size;
2068 bfd_byte *buf, *p;
2069 struct bfd_in_memory *bim;
2071 nbfd = _bfd_get_elt_at_filepos (archive, filepos);
2072 if (nbfd == NULL)
2073 goto error_return;
2075 if ((nbfd->flags & BFD_IN_MEMORY) != 0)
2077 /* We have already expanded this BFD. */
2078 return nbfd;
2081 tdata = (struct areltdata *) nbfd->arelt_data;
2082 hdr = (struct ar_hdr *) tdata->arch_header;
2083 if (strncmp (hdr->ar_fmag, ARFZMAG, 2) != 0)
2084 return nbfd;
2086 /* We must uncompress this element. We do this by copying it into a
2087 memory buffer, and making bfd_bread and bfd_seek use that buffer.
2088 This can use a lot of memory, but it's simpler than getting a
2089 temporary file, making that work with the file descriptor caching
2090 code, and making sure that it is deleted at all appropriate
2091 times. It can be changed if it ever becomes important. */
2093 /* The compressed file starts with a dummy ECOFF file header. */
2094 if (bfd_seek (nbfd, (file_ptr) FILHSZ, SEEK_SET) != 0)
2095 goto error_return;
2097 /* The next eight bytes are the real file size. */
2098 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8)
2099 goto error_return;
2100 size = H_GET_64 (nbfd, ab);
2102 if (size == 0)
2103 buf = NULL;
2104 else
2106 bfd_size_type left;
2107 bfd_byte dict[4096];
2108 unsigned int h;
2109 bfd_byte b;
2111 buf = (bfd_byte *) bfd_alloc (nbfd, size);
2112 if (buf == NULL)
2113 goto error_return;
2114 p = buf;
2116 left = size;
2118 /* I don't know what the next eight bytes are for. */
2119 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8)
2120 goto error_return;
2122 /* This is the uncompression algorithm. It's a simple
2123 dictionary based scheme in which each character is predicted
2124 by a hash of the previous three characters. A control byte
2125 indicates whether the character is predicted or whether it
2126 appears in the input stream; each control byte manages the
2127 next eight bytes in the output stream. */
2128 memset (dict, 0, sizeof dict);
2129 h = 0;
2130 while (bfd_bread (&b, (bfd_size_type) 1, nbfd) == 1)
2132 unsigned int i;
2134 for (i = 0; i < 8; i++, b >>= 1)
2136 bfd_byte n;
2138 if ((b & 1) == 0)
2139 n = dict[h];
2140 else
2142 if (! bfd_bread (&n, (bfd_size_type) 1, nbfd))
2143 goto error_return;
2144 dict[h] = n;
2147 *p++ = n;
2149 --left;
2150 if (left == 0)
2151 break;
2153 h <<= 4;
2154 h ^= n;
2155 h &= sizeof dict - 1;
2158 if (left == 0)
2159 break;
2163 /* Now the uncompressed file contents are in buf. */
2164 bim = ((struct bfd_in_memory *)
2165 bfd_alloc (nbfd, (bfd_size_type) sizeof (struct bfd_in_memory)));
2166 if (bim == NULL)
2167 goto error_return;
2168 bim->size = size;
2169 bim->buffer = buf;
2171 nbfd->mtime_set = TRUE;
2172 nbfd->mtime = strtol (hdr->ar_date, (char **) NULL, 10);
2174 nbfd->flags |= BFD_IN_MEMORY;
2175 nbfd->iostream = (PTR) bim;
2176 BFD_ASSERT (! nbfd->cacheable);
2178 return nbfd;
2180 error_return:
2181 if (nbfd != NULL)
2182 bfd_close (nbfd);
2183 return NULL;
2186 /* Open the next archived file. */
2188 static bfd *
2189 alpha_ecoff_openr_next_archived_file (archive, last_file)
2190 bfd *archive;
2191 bfd *last_file;
2193 file_ptr filestart;
2195 if (last_file == NULL)
2196 filestart = bfd_ardata (archive)->first_file_filepos;
2197 else
2199 struct areltdata *t;
2200 struct ar_hdr *h;
2201 bfd_size_type size;
2203 /* We can't use arelt_size here, because that uses parsed_size,
2204 which is the uncompressed size. We need the compressed size. */
2205 t = (struct areltdata *) last_file->arelt_data;
2206 h = (struct ar_hdr *) t->arch_header;
2207 size = strtol (h->ar_size, (char **) NULL, 10);
2209 /* Pad to an even boundary...
2210 Note that last_file->origin can be odd in the case of
2211 BSD-4.4-style element with a long odd size. */
2212 filestart = last_file->origin + size;
2213 filestart += filestart % 2;
2216 return alpha_ecoff_get_elt_at_filepos (archive, filestart);
2219 /* Open the archive file given an index into the armap. */
2221 static bfd *
2222 alpha_ecoff_get_elt_at_index (abfd, index)
2223 bfd *abfd;
2224 symindex index;
2226 carsym *entry;
2228 entry = bfd_ardata (abfd)->symdefs + index;
2229 return alpha_ecoff_get_elt_at_filepos (abfd, entry->file_offset);
2232 /* This is the ECOFF backend structure. The backend field of the
2233 target vector points to this. */
2235 static const struct ecoff_backend_data alpha_ecoff_backend_data =
2237 /* COFF backend structure. */
2239 (void (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR))) bfd_void, /* aux_in */
2240 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
2241 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
2242 (unsigned (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR)))bfd_void,/*aux_out*/
2243 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
2244 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
2245 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */
2246 alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out,
2247 alpha_ecoff_swap_scnhdr_out,
2248 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, TRUE, FALSE, 4, FALSE, 2,
2249 alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in,
2250 alpha_ecoff_swap_scnhdr_in, NULL,
2251 alpha_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
2252 alpha_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
2253 _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
2254 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2255 NULL, NULL, NULL
2257 /* Supported architecture. */
2258 bfd_arch_alpha,
2259 /* Initial portion of armap string. */
2260 "________64",
2261 /* The page boundary used to align sections in a demand-paged
2262 executable file. E.g., 0x1000. */
2263 0x2000,
2264 /* TRUE if the .rdata section is part of the text segment, as on the
2265 Alpha. FALSE if .rdata is part of the data segment, as on the
2266 MIPS. */
2267 TRUE,
2268 /* Bitsize of constructor entries. */
2270 /* Reloc to use for constructor entries. */
2271 &alpha_howto_table[ALPHA_R_REFQUAD],
2273 /* Symbol table magic number. */
2274 magicSym2,
2275 /* Alignment of debugging information. E.g., 4. */
2277 /* Sizes of external symbolic information. */
2278 sizeof (struct hdr_ext),
2279 sizeof (struct dnr_ext),
2280 sizeof (struct pdr_ext),
2281 sizeof (struct sym_ext),
2282 sizeof (struct opt_ext),
2283 sizeof (struct fdr_ext),
2284 sizeof (struct rfd_ext),
2285 sizeof (struct ext_ext),
2286 /* Functions to swap in external symbolic data. */
2287 ecoff_swap_hdr_in,
2288 ecoff_swap_dnr_in,
2289 ecoff_swap_pdr_in,
2290 ecoff_swap_sym_in,
2291 ecoff_swap_opt_in,
2292 ecoff_swap_fdr_in,
2293 ecoff_swap_rfd_in,
2294 ecoff_swap_ext_in,
2295 _bfd_ecoff_swap_tir_in,
2296 _bfd_ecoff_swap_rndx_in,
2297 /* Functions to swap out external symbolic data. */
2298 ecoff_swap_hdr_out,
2299 ecoff_swap_dnr_out,
2300 ecoff_swap_pdr_out,
2301 ecoff_swap_sym_out,
2302 ecoff_swap_opt_out,
2303 ecoff_swap_fdr_out,
2304 ecoff_swap_rfd_out,
2305 ecoff_swap_ext_out,
2306 _bfd_ecoff_swap_tir_out,
2307 _bfd_ecoff_swap_rndx_out,
2308 /* Function to read in symbolic data. */
2309 _bfd_ecoff_slurp_symbolic_info
2311 /* External reloc size. */
2312 RELSZ,
2313 /* Reloc swapping functions. */
2314 alpha_ecoff_swap_reloc_in,
2315 alpha_ecoff_swap_reloc_out,
2316 /* Backend reloc tweaking. */
2317 alpha_adjust_reloc_in,
2318 alpha_adjust_reloc_out,
2319 /* Relocate section contents while linking. */
2320 alpha_relocate_section,
2321 /* Do final adjustments to filehdr and aouthdr. */
2322 alpha_adjust_headers,
2323 /* Read an element from an archive at a given file position. */
2324 alpha_ecoff_get_elt_at_filepos
2327 /* Looking up a reloc type is Alpha specific. */
2328 #define _bfd_ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup
2330 /* So is getting relocated section contents. */
2331 #define _bfd_ecoff_bfd_get_relocated_section_contents \
2332 alpha_ecoff_get_relocated_section_contents
2334 /* Handling file windows is generic. */
2335 #define _bfd_ecoff_get_section_contents_in_window \
2336 _bfd_generic_get_section_contents_in_window
2338 /* Relaxing sections is generic. */
2339 #define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section
2340 #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
2341 #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections
2342 #define _bfd_ecoff_bfd_is_group_section bfd_generic_is_group_section
2343 #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group
2344 #define _bfd_ecoff_section_already_linked \
2345 _bfd_generic_section_already_linked
2347 const bfd_target ecoffalpha_little_vec =
2349 "ecoff-littlealpha", /* name */
2350 bfd_target_ecoff_flavour,
2351 BFD_ENDIAN_LITTLE, /* data byte order is little */
2352 BFD_ENDIAN_LITTLE, /* header byte order is little */
2354 (HAS_RELOC | EXEC_P | /* object flags */
2355 HAS_LINENO | HAS_DEBUG |
2356 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
2358 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
2359 0, /* leading underscore */
2360 ' ', /* ar_pad_char */
2361 15, /* ar_max_namelen */
2362 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2363 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2364 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2365 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2366 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2367 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2369 {_bfd_dummy_target, alpha_ecoff_object_p, /* bfd_check_format */
2370 _bfd_ecoff_archive_p, _bfd_dummy_target},
2371 {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
2372 _bfd_generic_mkarchive, bfd_false},
2373 {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
2374 _bfd_write_archive_contents, bfd_false},
2376 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2377 BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2378 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2379 BFD_JUMP_TABLE_ARCHIVE (alpha_ecoff),
2380 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2381 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2382 BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2383 BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2384 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2386 NULL,
2388 (PTR) &alpha_ecoff_backend_data