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