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