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