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