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