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