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