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