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