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