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