* bfd/aoutx.h (aout_link_input_section_std,
[deliverable/binutils-gdb.git] / bfd / coff-alpha.c
1 /* BFD back-end for ALPHA Extended-Coff files.
2 Copyright 1993 Free Software Foundation, Inc.
3 Modified from coff-mips.c by Steve Chamberlain <sac@cygnus.com> and
4 Ian Lance Taylor <ian@cygnus.com>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "coff/internal.h"
27 #include "coff/sym.h"
28 #include "coff/symconst.h"
29 #include "coff/ecoff.h"
30 #include "coff/alpha.h"
31 #include "libcoff.h"
32 #include "libecoff.h"
33 \f
34 /* Prototypes for static functions. */
35
36 static boolean alpha_ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
37 static void alpha_ecoff_swap_reloc_in PARAMS ((bfd *, PTR,
38 struct internal_reloc *));
39 static void alpha_ecoff_swap_reloc_out PARAMS ((bfd *,
40 const struct internal_reloc *,
41 PTR));
42 static void alpha_adjust_reloc_in PARAMS ((bfd *,
43 const struct internal_reloc *,
44 arelent *));
45 static void alpha_adjust_reloc_out PARAMS ((bfd *, const arelent *,
46 struct internal_reloc *));
47 static bfd_byte *alpha_ecoff_get_relocated_section_contents
48 PARAMS ((bfd *abfd, struct bfd_link_info *, struct bfd_link_order *,
49 bfd_byte *data, boolean relocateable, asymbol **symbols));
50 static bfd_vma alpha_convert_external_reloc
51 PARAMS ((bfd *, struct bfd_link_info *, bfd *, struct external_reloc *,
52 struct ecoff_link_hash_entry *));
53 static boolean alpha_relocate_section PARAMS ((bfd *, struct bfd_link_info *,
54 bfd *, asection *,
55 bfd_byte *, PTR));
56 \f
57 /* ECOFF has COFF sections, but the debugging information is stored in
58 a completely different format. ECOFF targets use some of the
59 swapping routines from coffswap.h, and some of the generic COFF
60 routines in coffgen.c, but, unlike the real COFF targets, do not
61 use coffcode.h itself.
62
63 Get the generic COFF swapping routines, except for the reloc,
64 symbol, and lineno ones. Give them ecoff names. Define some
65 accessor macros for the large sizes used for Alpha ECOFF. */
66
67 #define GET_FILEHDR_SYMPTR bfd_h_get_64
68 #define PUT_FILEHDR_SYMPTR bfd_h_put_64
69 #define GET_AOUTHDR_TSIZE bfd_h_get_64
70 #define PUT_AOUTHDR_TSIZE bfd_h_put_64
71 #define GET_AOUTHDR_DSIZE bfd_h_get_64
72 #define PUT_AOUTHDR_DSIZE bfd_h_put_64
73 #define GET_AOUTHDR_BSIZE bfd_h_get_64
74 #define PUT_AOUTHDR_BSIZE bfd_h_put_64
75 #define GET_AOUTHDR_ENTRY bfd_h_get_64
76 #define PUT_AOUTHDR_ENTRY bfd_h_put_64
77 #define GET_AOUTHDR_TEXT_START bfd_h_get_64
78 #define PUT_AOUTHDR_TEXT_START bfd_h_put_64
79 #define GET_AOUTHDR_DATA_START bfd_h_get_64
80 #define PUT_AOUTHDR_DATA_START bfd_h_put_64
81 #define GET_SCNHDR_PADDR bfd_h_get_64
82 #define PUT_SCNHDR_PADDR bfd_h_put_64
83 #define GET_SCNHDR_VADDR bfd_h_get_64
84 #define PUT_SCNHDR_VADDR bfd_h_put_64
85 #define GET_SCNHDR_SIZE bfd_h_get_64
86 #define PUT_SCNHDR_SIZE bfd_h_put_64
87 #define GET_SCNHDR_SCNPTR bfd_h_get_64
88 #define PUT_SCNHDR_SCNPTR bfd_h_put_64
89 #define GET_SCNHDR_RELPTR bfd_h_get_64
90 #define PUT_SCNHDR_RELPTR bfd_h_put_64
91 #define GET_SCNHDR_LNNOPTR bfd_h_get_64
92 #define PUT_SCNHDR_LNNOPTR bfd_h_put_64
93
94 #define ALPHAECOFF
95
96 #define NO_COFF_RELOCS
97 #define NO_COFF_SYMBOLS
98 #define NO_COFF_LINENOS
99 #define coff_swap_filehdr_in alpha_ecoff_swap_filehdr_in
100 #define coff_swap_filehdr_out alpha_ecoff_swap_filehdr_out
101 #define coff_swap_aouthdr_in alpha_ecoff_swap_aouthdr_in
102 #define coff_swap_aouthdr_out alpha_ecoff_swap_aouthdr_out
103 #define coff_swap_scnhdr_in alpha_ecoff_swap_scnhdr_in
104 #define coff_swap_scnhdr_out alpha_ecoff_swap_scnhdr_out
105 #include "coffswap.h"
106
107 /* Get the ECOFF swapping routines. */
108 #define ECOFF_64
109 #include "ecoffswap.h"
110 \f
111 /* How to process the various reloc types. */
112
113 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
114 from smaller values. Start with zero, widen, *then* decrement. */
115 #define MINUS_ONE (((bfd_vma)0) - 1)
116
117 static reloc_howto_type alpha_howto_table[] =
118 {
119 /* Reloc type 0 is ignored by itself. However, it appears after a
120 GPDISP reloc to identify the location where the low order 16 bits
121 of the gp register are loaded. */
122 HOWTO (ALPHA_R_IGNORE, /* type */
123 0, /* rightshift */
124 0, /* size (0 = byte, 1 = short, 2 = long) */
125 8, /* bitsize */
126 true, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_dont, /* complain_on_overflow */
129 0, /* special_function */
130 "IGNORE", /* name */
131 false, /* partial_inplace */
132 0, /* src_mask */
133 0, /* dst_mask */
134 true), /* pcrel_offset */
135
136 /* A 32 bit reference to a symbol. */
137 HOWTO (ALPHA_R_REFLONG, /* type */
138 0, /* rightshift */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
140 32, /* bitsize */
141 false, /* pc_relative */
142 0, /* bitpos */
143 complain_overflow_bitfield, /* complain_on_overflow */
144 0, /* special_function */
145 "REFLONG", /* name */
146 true, /* partial_inplace */
147 0xffffffff, /* src_mask */
148 0xffffffff, /* dst_mask */
149 false), /* pcrel_offset */
150
151 /* A 64 bit reference to a symbol. */
152 HOWTO (ALPHA_R_REFQUAD, /* type */
153 0, /* rightshift */
154 4, /* size (0 = byte, 1 = short, 2 = long) */
155 64, /* bitsize */
156 false, /* pc_relative */
157 0, /* bitpos */
158 complain_overflow_bitfield, /* complain_on_overflow */
159 0, /* special_function */
160 "REFQUAD", /* name */
161 true, /* partial_inplace */
162 MINUS_ONE, /* src_mask */
163 MINUS_ONE, /* dst_mask */
164 false), /* pcrel_offset */
165
166 /* A 32 bit GP relative offset. This is just like REFLONG except
167 that when the value is used the value of the gp register will be
168 added in. */
169 HOWTO (ALPHA_R_GPREL32, /* type */
170 0, /* rightshift */
171 2, /* size (0 = byte, 1 = short, 2 = long) */
172 32, /* bitsize */
173 false, /* pc_relative */
174 0, /* bitpos */
175 complain_overflow_bitfield, /* complain_on_overflow */
176 0, /* special_function */
177 "GPREL32", /* name */
178 true, /* partial_inplace */
179 0xffffffff, /* src_mask */
180 0xffffffff, /* dst_mask */
181 false), /* pcrel_offset */
182
183 /* Used for an instruction that refers to memory off the GP
184 register. The offset is 16 bits of the 32 bit instruction. This
185 reloc always seems to be against the .lita section. */
186 HOWTO (ALPHA_R_LITERAL, /* type */
187 0, /* rightshift */
188 2, /* size (0 = byte, 1 = short, 2 = long) */
189 16, /* bitsize */
190 false, /* pc_relative */
191 0, /* bitpos */
192 complain_overflow_signed, /* complain_on_overflow */
193 0, /* special_function */
194 "LITERAL", /* name */
195 true, /* partial_inplace */
196 0xffff, /* src_mask */
197 0xffff, /* dst_mask */
198 false), /* pcrel_offset */
199
200 /* This reloc only appears immediately following a LITERAL reloc.
201 It identifies a use of the literal. It seems that the linker can
202 use this to eliminate a portion of the .lita section. The symbol
203 index is special: 1 means the literal address is in the base
204 register of a memory format instruction; 2 means the literal
205 address is in the byte offset register of a byte-manipulation
206 instruction; 3 means the literal address is in the target
207 register of a jsr instruction. This does not actually do any
208 relocation. */
209 HOWTO (ALPHA_R_LITUSE, /* type */
210 0, /* rightshift */
211 2, /* size (0 = byte, 1 = short, 2 = long) */
212 32, /* bitsize */
213 false, /* pc_relative */
214 0, /* bitpos */
215 complain_overflow_dont, /* complain_on_overflow */
216 0, /* special_function */
217 "LITUSE", /* name */
218 false, /* partial_inplace */
219 0, /* src_mask */
220 0, /* dst_mask */
221 false), /* pcrel_offset */
222
223 /* Load the gp register. This is always used for a ldah instruction
224 which loads the upper 16 bits of the gp register. The next reloc
225 will be an IGNORE reloc which identifies the location of the lda
226 instruction which loads the lower 16 bits. The symbol index of
227 the GPDISP instruction appears to actually be the number of bytes
228 between the ldah and lda instructions. This gives two different
229 ways to determine where the lda instruction is; I don't know why
230 both are used. The value to use for the relocation is the
231 difference between the GP value and the current location; the
232 load will always be done against a register holding the current
233 address. */
234 HOWTO (ALPHA_R_GPDISP, /* type */
235 16, /* rightshift */
236 2, /* size (0 = byte, 1 = short, 2 = long) */
237 16, /* bitsize */
238 true, /* pc_relative */
239 0, /* bitpos */
240 complain_overflow_dont, /* complain_on_overflow */
241 0, /* special_function */
242 "GPDISP", /* name */
243 true, /* partial_inplace */
244 0xffff, /* src_mask */
245 0xffff, /* dst_mask */
246 true), /* pcrel_offset */
247
248 /* A 21 bit branch. The native assembler generates these for
249 branches within the text segment, and also fills in the PC
250 relative offset in the instruction. */
251 HOWTO (ALPHA_R_BRADDR, /* type */
252 2, /* rightshift */
253 2, /* size (0 = byte, 1 = short, 2 = long) */
254 21, /* bitsize */
255 true, /* pc_relative */
256 0, /* bitpos */
257 complain_overflow_signed, /* complain_on_overflow */
258 0, /* special_function */
259 "BRADDR", /* name */
260 true, /* partial_inplace */
261 0x1fffff, /* src_mask */
262 0x1fffff, /* dst_mask */
263 false), /* pcrel_offset */
264
265 /* A hint for a jump to a register. */
266 HOWTO (ALPHA_R_HINT, /* type */
267 2, /* rightshift */
268 2, /* size (0 = byte, 1 = short, 2 = long) */
269 14, /* bitsize */
270 true, /* pc_relative */
271 0, /* bitpos */
272 complain_overflow_dont, /* complain_on_overflow */
273 0, /* special_function */
274 "HINT", /* name */
275 true, /* partial_inplace */
276 0x3fff, /* src_mask */
277 0x3fff, /* dst_mask */
278 false), /* pcrel_offset */
279
280 /* 16 bit PC relative offset. */
281 HOWTO (ALPHA_R_SREL16, /* type */
282 0, /* rightshift */
283 1, /* size (0 = byte, 1 = short, 2 = long) */
284 16, /* bitsize */
285 true, /* pc_relative */
286 0, /* bitpos */
287 complain_overflow_signed, /* complain_on_overflow */
288 0, /* special_function */
289 "SREL16", /* name */
290 true, /* partial_inplace */
291 0xffff, /* src_mask */
292 0xffff, /* dst_mask */
293 false), /* pcrel_offset */
294
295 /* 32 bit PC relative offset. */
296 HOWTO (ALPHA_R_SREL32, /* type */
297 0, /* rightshift */
298 2, /* size (0 = byte, 1 = short, 2 = long) */
299 32, /* bitsize */
300 true, /* pc_relative */
301 0, /* bitpos */
302 complain_overflow_signed, /* complain_on_overflow */
303 0, /* special_function */
304 "SREL32", /* name */
305 true, /* partial_inplace */
306 0xffffffff, /* src_mask */
307 0xffffffff, /* dst_mask */
308 false), /* pcrel_offset */
309
310 /* A 64 bit PC relative offset. */
311 HOWTO (ALPHA_R_SREL64, /* type */
312 0, /* rightshift */
313 4, /* size (0 = byte, 1 = short, 2 = long) */
314 64, /* bitsize */
315 true, /* pc_relative */
316 0, /* bitpos */
317 complain_overflow_signed, /* complain_on_overflow */
318 0, /* special_function */
319 "SREL64", /* name */
320 true, /* partial_inplace */
321 MINUS_ONE, /* src_mask */
322 MINUS_ONE, /* dst_mask */
323 false), /* pcrel_offset */
324
325 /* Push a value on the reloc evaluation stack. */
326 HOWTO (ALPHA_R_OP_PUSH, /* type */
327 0, /* rightshift */
328 0, /* size (0 = byte, 1 = short, 2 = long) */
329 0, /* bitsize */
330 false, /* pc_relative */
331 0, /* bitpos */
332 complain_overflow_dont, /* complain_on_overflow */
333 0, /* special_function */
334 "OP_PUSH", /* name */
335 false, /* partial_inplace */
336 0, /* src_mask */
337 0, /* dst_mask */
338 false), /* pcrel_offset */
339
340 /* Store the value from the stack at the given address. Store it in
341 a bitfield of size r_size starting at bit position r_offset. */
342 HOWTO (ALPHA_R_OP_STORE, /* type */
343 0, /* rightshift */
344 4, /* size (0 = byte, 1 = short, 2 = long) */
345 64, /* bitsize */
346 false, /* pc_relative */
347 0, /* bitpos */
348 complain_overflow_dont, /* complain_on_overflow */
349 0, /* special_function */
350 "OP_STORE", /* name */
351 false, /* partial_inplace */
352 0, /* src_mask */
353 MINUS_ONE, /* dst_mask */
354 false), /* pcrel_offset */
355
356 /* Subtract the reloc address from the value on the top of the
357 relocation stack. */
358 HOWTO (ALPHA_R_OP_PSUB, /* type */
359 0, /* rightshift */
360 0, /* size (0 = byte, 1 = short, 2 = long) */
361 0, /* bitsize */
362 false, /* pc_relative */
363 0, /* bitpos */
364 complain_overflow_dont, /* complain_on_overflow */
365 0, /* special_function */
366 "OP_PSUB", /* name */
367 false, /* partial_inplace */
368 0, /* src_mask */
369 0, /* dst_mask */
370 false), /* pcrel_offset */
371
372 /* Shift the value on the top of the relocation stack right by the
373 given value. */
374 HOWTO (ALPHA_R_OP_PRSHIFT, /* type */
375 0, /* rightshift */
376 0, /* size (0 = byte, 1 = short, 2 = long) */
377 0, /* bitsize */
378 false, /* pc_relative */
379 0, /* bitpos */
380 complain_overflow_dont, /* complain_on_overflow */
381 0, /* special_function */
382 "OP_PRSHIFT", /* name */
383 false, /* partial_inplace */
384 0, /* src_mask */
385 0, /* dst_mask */
386 false), /* pcrel_offset */
387
388 /* Adjust the GP value for a new range in the object file. */
389 HOWTO (ALPHA_R_GPVALUE, /* type */
390 0, /* rightshift */
391 0, /* size (0 = byte, 1 = short, 2 = long) */
392 0, /* bitsize */
393 false, /* pc_relative */
394 0, /* bitpos */
395 complain_overflow_dont, /* complain_on_overflow */
396 0, /* special_function */
397 "GPVALUE", /* name */
398 false, /* partial_inplace */
399 0, /* src_mask */
400 0, /* dst_mask */
401 false) /* pcrel_offset */
402 };
403 \f
404 /* See whether the magic number matches. */
405
406 static boolean
407 alpha_ecoff_bad_format_hook (abfd, filehdr)
408 bfd *abfd;
409 PTR filehdr;
410 {
411 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
412
413 if (ALPHA_ECOFF_BADMAG (*internal_f))
414 return false;
415
416 return true;
417 }
418 \f
419 /* Reloc handling. */
420
421 /* Swap a reloc in. */
422
423 static void
424 alpha_ecoff_swap_reloc_in (abfd, ext_ptr, intern)
425 bfd *abfd;
426 PTR ext_ptr;
427 struct internal_reloc *intern;
428 {
429 const RELOC *ext = (RELOC *) ext_ptr;
430
431 intern->r_vaddr = bfd_h_get_64 (abfd, (bfd_byte *) ext->r_vaddr);
432 intern->r_symndx = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_symndx);
433
434 BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false);
435
436 intern->r_type = ((ext->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
437 >> RELOC_BITS0_TYPE_SH_LITTLE);
438 intern->r_extern = (ext->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
439 intern->r_offset = ((ext->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
440 >> RELOC_BITS1_OFFSET_SH_LITTLE);
441 /* Ignored the reserved bits. */
442 intern->r_size = ((ext->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
443 >> RELOC_BITS3_SIZE_SH_LITTLE);
444
445 if (intern->r_type == ALPHA_R_LITUSE
446 || intern->r_type == ALPHA_R_GPDISP)
447 {
448 /* Handle the LITUSE and GPDISP relocs specially. Its symndx
449 value is not actually a symbol index, but is instead a
450 special code. We put the code in the r_size field, and
451 clobber the symndx. */
452 if (intern->r_size != 0)
453 abort ();
454 intern->r_size = intern->r_symndx;
455 intern->r_symndx = RELOC_SECTION_NONE;
456 }
457 else if (intern->r_type == ALPHA_R_IGNORE)
458 {
459 /* The IGNORE reloc generally follows a GPDISP reloc, and is
460 against the .lita section. The section is irrelevant. */
461 if (! intern->r_extern &&
462 (intern->r_symndx == RELOC_SECTION_NONE
463 || intern->r_symndx == RELOC_SECTION_ABS))
464 abort ();
465 if (! intern->r_extern && intern->r_symndx == RELOC_SECTION_LITA)
466 intern->r_symndx = RELOC_SECTION_NONE;
467 }
468 }
469
470 /* Swap a reloc out. */
471
472 static void
473 alpha_ecoff_swap_reloc_out (abfd, intern, dst)
474 bfd *abfd;
475 const struct internal_reloc *intern;
476 PTR dst;
477 {
478 RELOC *ext = (RELOC *) dst;
479 long symndx;
480 unsigned char size;
481
482 /* Undo the hackery done in swap_reloc_in. */
483 if (intern->r_type == ALPHA_R_LITUSE
484 || intern->r_type == ALPHA_R_GPDISP)
485 {
486 symndx = intern->r_size;
487 size = 0;
488 }
489 else if (intern->r_type == ALPHA_R_IGNORE
490 && ! intern->r_extern
491 && intern->r_symndx == RELOC_SECTION_NONE)
492 {
493 symndx = RELOC_SECTION_LITA;
494 size = intern->r_size;
495 }
496 else
497 {
498 symndx = intern->r_symndx;
499 size = intern->r_size;
500 }
501
502 BFD_ASSERT (intern->r_extern
503 || (intern->r_symndx >= 0 && intern->r_symndx <= 14));
504
505 bfd_h_put_64 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
506 bfd_h_put_32 (abfd, symndx, (bfd_byte *) ext->r_symndx);
507
508 BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false);
509
510 ext->r_bits[0] = ((intern->r_type << RELOC_BITS0_TYPE_SH_LITTLE)
511 & RELOC_BITS0_TYPE_LITTLE);
512 ext->r_bits[1] = ((intern->r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0)
513 | ((intern->r_offset << RELOC_BITS1_OFFSET_SH_LITTLE)
514 & RELOC_BITS1_OFFSET_LITTLE));
515 ext->r_bits[2] = 0;
516 ext->r_bits[3] = ((size << RELOC_BITS3_SIZE_SH_LITTLE)
517 & RELOC_BITS3_SIZE_LITTLE);
518 }
519
520 /* Finish canonicalizing a reloc. Part of this is generic to all
521 ECOFF targets, and that part is in ecoff.c. The rest is done in
522 this backend routine. It must fill in the howto field. */
523
524 static void
525 alpha_adjust_reloc_in (abfd, intern, rptr)
526 bfd *abfd;
527 const struct internal_reloc *intern;
528 arelent *rptr;
529 {
530 if (intern->r_type > ALPHA_R_GPVALUE)
531 abort ();
532
533 switch (intern->r_type)
534 {
535 case ALPHA_R_BRADDR:
536 case ALPHA_R_SREL16:
537 case ALPHA_R_SREL32:
538 case ALPHA_R_SREL64:
539 /* The PC relative relocs do not seem to use the section VMA as
540 a negative addend. */
541 rptr->addend = 0;
542 break;
543
544 case ALPHA_R_GPREL32:
545 case ALPHA_R_LITERAL:
546 /* Copy the gp value for this object file into the addend, to
547 ensure that we are not confused by the linker. */
548 if (! intern->r_extern)
549 rptr->addend += ecoff_data (abfd)->gp;
550 break;
551
552 case ALPHA_R_LITUSE:
553 case ALPHA_R_GPDISP:
554 /* The LITUSE and GPDISP relocs do not use a symbol, or an
555 addend, but they do use a special code. Put this code in the
556 addend field. */
557 rptr->addend = intern->r_size;
558 break;
559
560 case ALPHA_R_OP_STORE:
561 /* The STORE reloc needs the size and offset fields. We store
562 them in the addend. */
563 BFD_ASSERT (intern->r_offset <= 256 && intern->r_size <= 256);
564 rptr->addend = (intern->r_offset << 8) + intern->r_size;
565 break;
566
567 case ALPHA_R_OP_PUSH:
568 case ALPHA_R_OP_PSUB:
569 case ALPHA_R_OP_PRSHIFT:
570 /* The PUSH, PSUB and PRSHIFT relocs do not actually use an
571 address. I believe that the address supplied is really an
572 addend. */
573 rptr->addend = intern->r_vaddr;
574 break;
575
576 case ALPHA_R_GPVALUE:
577 /* Set the addend field to the new GP value. */
578 rptr->addend = intern->r_symndx + ecoff_data (abfd)->gp;
579 break;
580
581 case ALPHA_R_IGNORE:
582 /* If the type is ALPHA_R_IGNORE, make sure this is a reference
583 to the absolute section so that the reloc is ignored. For
584 some reason the address of this reloc type is not adjusted by
585 the section vma. We record the gp value for this object file
586 here, for convenience when doing the GPDISP relocation. */
587 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
588 rptr->address = intern->r_vaddr;
589 rptr->addend = ecoff_data (abfd)->gp;
590 break;
591
592 default:
593 break;
594 }
595
596 rptr->howto = &alpha_howto_table[intern->r_type];
597 }
598
599 /* When writing out a reloc we need to pull some values back out of
600 the addend field into the reloc. This is roughly the reverse of
601 alpha_adjust_reloc_in, except that there are several changes we do
602 not need to undo. */
603
604 static void
605 alpha_adjust_reloc_out (abfd, rel, intern)
606 bfd *abfd;
607 const arelent *rel;
608 struct internal_reloc *intern;
609 {
610 switch (intern->r_type)
611 {
612 case ALPHA_R_LITUSE:
613 case ALPHA_R_GPDISP:
614 intern->r_size = rel->addend;
615 break;
616
617 case ALPHA_R_OP_STORE:
618 intern->r_size = rel->addend & 0xff;
619 intern->r_offset = (rel->addend >> 8) & 0xff;
620 break;
621
622 case ALPHA_R_OP_PUSH:
623 case ALPHA_R_OP_PSUB:
624 case ALPHA_R_OP_PRSHIFT:
625 intern->r_vaddr = rel->addend;
626 break;
627
628 case ALPHA_R_IGNORE:
629 intern->r_vaddr = rel->address;
630 if (intern->r_symndx == RELOC_SECTION_ABS)
631 intern->r_symndx = RELOC_SECTION_NONE;
632 break;
633
634 default:
635 break;
636 }
637 }
638
639 /* The size of the stack for the relocation evaluator. */
640 #define RELOC_STACKSIZE (10)
641
642 /* Alpha ECOFF relocs have a built in expression evaluator as well as
643 other interdependencies. Rather than use a bunch of special
644 functions and global variables, we use a single routine to do all
645 the relocation for a section. I haven't yet worked out how the
646 assembler is going to handle this. */
647
648 static bfd_byte *
649 alpha_ecoff_get_relocated_section_contents (abfd, link_info, link_order,
650 data, relocateable, symbols)
651 bfd *abfd;
652 struct bfd_link_info *link_info;
653 struct bfd_link_order *link_order;
654 bfd_byte *data;
655 boolean relocateable;
656 asymbol **symbols;
657 {
658 bfd *input_bfd = link_order->u.indirect.section->owner;
659 asection *input_section = link_order->u.indirect.section;
660 size_t reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
661 arelent **reloc_vector = (arelent **) alloca (reloc_size);
662 bfd *output_bfd = relocateable ? abfd : (bfd *) NULL;
663 bfd_vma gp;
664 boolean gp_undefined;
665 bfd_vma stack[RELOC_STACKSIZE];
666 int tos = 0;
667
668 if (! bfd_get_section_contents (input_bfd, input_section, data,
669 (file_ptr) 0, input_section->_raw_size))
670 return NULL;
671
672 /* The section size is not going to change. */
673 input_section->_cooked_size = input_section->_raw_size;
674 input_section->reloc_done = true;
675
676 if (bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
677 symbols)
678 == 0)
679 return data;
680
681 /* Get the GP value for the output BFD. */
682 gp_undefined = false;
683 if (ecoff_data (abfd)->gp == 0)
684 {
685 if (relocateable != false)
686 {
687 asection *sec;
688 bfd_vma lo;
689
690 /* Make up a value. */
691 lo = (bfd_vma) -1;
692 for (sec = abfd->sections; sec != NULL; sec = sec->next)
693 {
694 if (sec->vma < lo
695 && (strcmp (sec->name, ".sbss") == 0
696 || strcmp (sec->name, ".sdata") == 0
697 || strcmp (sec->name, ".lit4") == 0
698 || strcmp (sec->name, ".lit8") == 0
699 || strcmp (sec->name, ".lita") == 0))
700 lo = sec->vma;
701 }
702 ecoff_data (abfd)->gp = lo + 0x8000;
703 }
704 else
705 {
706 struct bfd_link_hash_entry *h;
707
708 h = bfd_link_hash_lookup (link_info->hash, "_gp", false, false,
709 true);
710 if (h == (struct bfd_link_hash_entry *) NULL
711 || h->type != bfd_link_hash_defined)
712 gp_undefined = true;
713 else
714 ecoff_data (abfd)->gp = (h->u.def.value
715 + h->u.def.section->output_section->vma
716 + h->u.def.section->output_offset);
717 }
718 }
719 gp = ecoff_data (abfd)->gp;
720
721 for (; *reloc_vector != (arelent *) NULL; reloc_vector++)
722 {
723 arelent *rel;
724 bfd_reloc_status_type r;
725 char *err;
726
727 rel = *reloc_vector;
728 r = bfd_reloc_ok;
729 switch (rel->howto->type)
730 {
731 case ALPHA_R_IGNORE:
732 rel->address += input_section->output_offset;
733 break;
734
735 case ALPHA_R_REFLONG:
736 case ALPHA_R_REFQUAD:
737 case ALPHA_R_BRADDR:
738 case ALPHA_R_HINT:
739 case ALPHA_R_SREL16:
740 case ALPHA_R_SREL32:
741 case ALPHA_R_SREL64:
742 if (relocateable
743 && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
744 {
745 rel->address += input_section->output_offset;
746 break;
747 }
748 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
749 output_bfd, &err);
750 break;
751
752 case ALPHA_R_GPREL32:
753 /* This relocation is used in a switch table. It is a 32
754 bit offset from the current GP value. We must adjust it
755 by the different between the original GP value and the
756 current GP value. The original GP value is stored in the
757 addend. We adjust the addend and let
758 bfd_perform_relocation finish the job. */
759 rel->addend -= gp;
760 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
761 output_bfd, &err);
762 if (r == bfd_reloc_ok && gp_undefined)
763 {
764 r = bfd_reloc_dangerous;
765 err = (char *) "GP relative relocation used when GP not defined";
766 }
767 break;
768
769 case ALPHA_R_LITERAL:
770 /* This is a reference to a literal value, generally
771 (always?) in the .lita section. This is a 16 bit GP
772 relative relocation. Sometimes the subsequent reloc is a
773 LITUSE reloc, which indicates how this reloc is used.
774 This sometimes permits rewriting the two instructions
775 referred to by the LITERAL and the LITUSE into different
776 instructions which do not refer to .lita. This can save
777 a memory reference, and permits removing a value from
778 .lita thus saving GP relative space.
779
780 We do not these optimizations. To do them we would need
781 to arrange to link the .lita section first, so that by
782 the time we got here we would know the final values to
783 use. This would not be particularly difficult, but it is
784 not currently implemented. */
785
786 {
787 unsigned long insn;
788
789 /* I believe that the LITERAL reloc will only apply to a
790 ldq instruction, so check my assumption. */
791 insn = bfd_get_32 (input_bfd, data + rel->address);
792 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29);
793
794 rel->addend -= gp;
795 r = bfd_perform_relocation (input_bfd, rel, data, input_section,
796 output_bfd, &err);
797 if (r == bfd_reloc_ok && gp_undefined)
798 {
799 r = bfd_reloc_dangerous;
800 err =
801 (char *) "GP relative relocation used when GP not defined";
802 }
803 }
804 break;
805
806 case ALPHA_R_LITUSE:
807 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
808 does not cause anything to happen, itself. */
809 rel->address += input_section->output_offset;
810 break;
811
812 case ALPHA_R_GPDISP:
813 /* This marks the ldah of an ldah/lda pair which loads the
814 gp register with the difference of the gp value and the
815 current location. The second of the pair is r_size bytes
816 ahead, and is marked with an ALPHA_R_IGNORE reloc. */
817 {
818 unsigned long insn1, insn2;
819 bfd_vma addend;
820
821 BFD_ASSERT (reloc_vector[1] != NULL
822 && reloc_vector[1]->howto->type == ALPHA_R_IGNORE
823 && (rel->address + rel->addend
824 == reloc_vector[1]->address));
825
826 /* Get the two instructions. */
827 insn1 = bfd_get_32 (input_bfd, data + rel->address);
828 insn2 = bfd_get_32 (input_bfd, data + rel->address + rel->addend);
829
830 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
831 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
832
833 /* Get the existing addend. We must account for the sign
834 extension done by lda and ldah. */
835 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
836 if (insn1 & 0x8000)
837 {
838 addend -= 0x80000000;
839 addend -= 0x80000000;
840 }
841 if (insn2 & 0x8000)
842 addend -= 0x10000;
843
844 /* The existing addend includes the different between the
845 gp of the input BFD and the address in the input BFD.
846 Subtract this out. */
847 addend -= (reloc_vector[1]->addend
848 - (input_section->vma + rel->address));
849
850 /* Now add in the final gp value, and subtract out the
851 final address. */
852 addend += (gp
853 - (input_section->output_section->vma
854 + input_section->output_offset
855 + rel->address));
856
857 /* Change the instructions, accounting for the sign
858 extension, and write them out. */
859 if (addend & 0x8000)
860 addend += 0x10000;
861 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
862 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
863
864 bfd_put_32 (input_bfd, (bfd_vma) insn1, data + rel->address);
865 bfd_put_32 (input_bfd, (bfd_vma) insn2,
866 data + rel->address + rel->addend);
867
868 rel->address += input_section->output_offset;
869 }
870 break;
871
872 case ALPHA_R_OP_PUSH:
873 /* Push a value on the reloc evaluation stack. */
874 {
875 asymbol *symbol;
876 bfd_vma relocation;
877
878 if (relocateable)
879 {
880 rel->address += input_section->output_offset;
881 break;
882 }
883
884 /* Figure out the relocation of this symbol. */
885 symbol = *rel->sym_ptr_ptr;
886
887 if (symbol->section == &bfd_und_section)
888 r = bfd_reloc_undefined;
889
890 if (bfd_is_com_section (symbol->section))
891 relocation = 0;
892 else
893 relocation = symbol->value;
894 relocation += symbol->section->output_section->vma;
895 relocation += symbol->section->output_offset;
896 relocation += rel->addend;
897
898 if (tos >= RELOC_STACKSIZE)
899 abort ();
900
901 stack[tos++] = relocation;
902 }
903 break;
904
905 case ALPHA_R_OP_STORE:
906 /* Store a value from the reloc stack into a bitfield. */
907 {
908 bfd_vma val;
909 int offset, size;
910
911 if (relocateable)
912 {
913 rel->address += input_section->output_offset;
914 break;
915 }
916
917 if (tos == 0)
918 abort ();
919
920 /* The offset and size for this reloc are encoded into the
921 addend field by alpha_adjust_reloc_in. */
922 offset = (rel->addend >> 8) & 0xff;
923 size = rel->addend & 0xff;
924
925 val = bfd_get_64 (abfd, data + rel->address);
926 val &=~ (((1 << size) - 1) << offset);
927 val |= (stack[--tos] & ((1 << size) - 1)) << offset;
928 bfd_put_64 (abfd, val, data + rel->address);
929 }
930 break;
931
932 case ALPHA_R_OP_PSUB:
933 /* Subtract a value from the top of the stack. */
934 {
935 asymbol *symbol;
936 bfd_vma relocation;
937
938 if (relocateable)
939 {
940 rel->address += input_section->output_offset;
941 break;
942 }
943
944 /* Figure out the relocation of this symbol. */
945 symbol = *rel->sym_ptr_ptr;
946
947 if (symbol->section == &bfd_und_section)
948 r = bfd_reloc_undefined;
949
950 if (bfd_is_com_section (symbol->section))
951 relocation = 0;
952 else
953 relocation = symbol->value;
954 relocation += symbol->section->output_section->vma;
955 relocation += symbol->section->output_offset;
956 relocation += rel->addend;
957
958 if (tos == 0)
959 abort ();
960
961 stack[tos - 1] -= relocation;
962 }
963 break;
964
965 case ALPHA_R_OP_PRSHIFT:
966 /* Shift the value on the top of the stack. */
967 {
968 asymbol *symbol;
969 bfd_vma relocation;
970
971 if (relocateable)
972 {
973 rel->address += input_section->output_offset;
974 break;
975 }
976
977 /* Figure out the relocation of this symbol. */
978 symbol = *rel->sym_ptr_ptr;
979
980 if (symbol->section == &bfd_und_section)
981 r = bfd_reloc_undefined;
982
983 if (bfd_is_com_section (symbol->section))
984 relocation = 0;
985 else
986 relocation = symbol->value;
987 relocation += symbol->section->output_section->vma;
988 relocation += symbol->section->output_offset;
989 relocation += rel->addend;
990
991 if (tos == 0)
992 abort ();
993
994 stack[tos - 1] >>= relocation;
995 }
996 break;
997
998 case ALPHA_R_GPVALUE:
999 /* I really don't know if this does the right thing. */
1000 gp = rel->addend;
1001 gp_undefined = false;
1002 break;
1003
1004 default:
1005 abort ();
1006 }
1007
1008 if (relocateable)
1009 {
1010 asection *os = input_section->output_section;
1011
1012 /* A partial link, so keep the relocs. */
1013 os->orelocation[os->reloc_count] = rel;
1014 os->reloc_count++;
1015 }
1016
1017 if (r != bfd_reloc_ok)
1018 {
1019 switch (r)
1020 {
1021 case bfd_reloc_undefined:
1022 if (! ((*link_info->callbacks->undefined_symbol)
1023 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1024 input_bfd, input_section, rel->address)))
1025 return NULL;
1026 break;
1027 case bfd_reloc_dangerous:
1028 if (! ((*link_info->callbacks->reloc_dangerous)
1029 (link_info, err, input_bfd, input_section,
1030 rel->address)))
1031 return NULL;
1032 break;
1033 case bfd_reloc_overflow:
1034 if (! ((*link_info->callbacks->reloc_overflow)
1035 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
1036 rel->howto->name, rel->addend, input_bfd,
1037 input_section, rel->address)))
1038 return NULL;
1039 break;
1040 case bfd_reloc_outofrange:
1041 default:
1042 abort ();
1043 break;
1044 }
1045 }
1046 }
1047
1048 if (tos != 0)
1049 abort ();
1050
1051 return data;
1052 }
1053
1054 /* Get the howto structure for a generic reloc type. */
1055
1056 static CONST struct reloc_howto_struct *
1057 alpha_bfd_reloc_type_lookup (abfd, code)
1058 bfd *abfd;
1059 bfd_reloc_code_real_type code;
1060 {
1061 int alpha_type;
1062
1063 switch (code)
1064 {
1065 case BFD_RELOC_32:
1066 alpha_type = ALPHA_R_REFLONG;
1067 break;
1068 case BFD_RELOC_64:
1069 alpha_type = ALPHA_R_REFQUAD;
1070 break;
1071 case BFD_RELOC_GPREL32:
1072 alpha_type = ALPHA_R_GPREL32;
1073 break;
1074 case BFD_RELOC_ALPHA_LITERAL:
1075 alpha_type = ALPHA_R_LITERAL;
1076 break;
1077 case BFD_RELOC_ALPHA_LITUSE:
1078 alpha_type = ALPHA_R_LITUSE;
1079 break;
1080 case BFD_RELOC_ALPHA_GPDISP_HI16:
1081 alpha_type = ALPHA_R_GPDISP;
1082 break;
1083 case BFD_RELOC_ALPHA_GPDISP_LO16:
1084 alpha_type = ALPHA_R_IGNORE;
1085 break;
1086 case BFD_RELOC_23_PCREL_S2:
1087 alpha_type = ALPHA_R_BRADDR;
1088 break;
1089 case BFD_RELOC_ALPHA_HINT:
1090 alpha_type = ALPHA_R_HINT;
1091 break;
1092 case BFD_RELOC_16_PCREL:
1093 alpha_type = ALPHA_R_SREL16;
1094 break;
1095 case BFD_RELOC_32_PCREL:
1096 alpha_type = ALPHA_R_SREL32;
1097 break;
1098 case BFD_RELOC_64_PCREL:
1099 alpha_type = ALPHA_R_SREL64;
1100 break;
1101 #if 0
1102 case ???:
1103 alpha_type = ALPHA_R_OP_PUSH;
1104 break;
1105 case ???:
1106 alpha_type = ALPHA_R_OP_STORE;
1107 break;
1108 case ???:
1109 alpha_type = ALPHA_R_OP_PSUB;
1110 break;
1111 case ???:
1112 alpha_type = ALPHA_R_OP_PRSHIFT;
1113 break;
1114 case ???:
1115 alpha_type = ALPHA_R_GPVALUE;
1116 break;
1117 #endif
1118 default:
1119 return (CONST struct reloc_howto_struct *) NULL;
1120 }
1121
1122 return &alpha_howto_table[alpha_type];
1123 }
1124 \f
1125 /* A helper routine for alpha_relocate_section which converts an
1126 external reloc when generating relocateable output. Returns the
1127 relocation amount. */
1128
1129 static bfd_vma
1130 alpha_convert_external_reloc (output_bfd, info, input_bfd, ext_rel, h)
1131 bfd *output_bfd;
1132 struct bfd_link_info *info;
1133 bfd *input_bfd;
1134 struct external_reloc *ext_rel;
1135 struct ecoff_link_hash_entry *h;
1136 {
1137 unsigned long r_symndx;
1138 bfd_vma relocation;
1139
1140 BFD_ASSERT (info->relocateable);
1141
1142 if (h->root.type == bfd_link_hash_defined)
1143 {
1144 asection *hsec;
1145 const char *name;
1146
1147 /* This symbol is defined in the output. Convert the reloc from
1148 being against the symbol to being against the section. */
1149
1150 /* Clear the r_extern bit. */
1151 ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE;
1152
1153 /* Compute a new r_symndx value. */
1154 hsec = h->root.u.def.section;
1155 name = bfd_get_section_name (output_bfd, hsec->output_section);
1156
1157 r_symndx = -1;
1158 switch (name[1])
1159 {
1160 case 'A':
1161 if (strcmp (name, "*ABS*") == 0)
1162 r_symndx = RELOC_SECTION_ABS;
1163 break;
1164 case 'b':
1165 if (strcmp (name, ".bss") == 0)
1166 r_symndx = RELOC_SECTION_BSS;
1167 break;
1168 case 'd':
1169 if (strcmp (name, ".data") == 0)
1170 r_symndx = RELOC_SECTION_DATA;
1171 break;
1172 case 'f':
1173 if (strcmp (name, ".fini") == 0)
1174 r_symndx = RELOC_SECTION_FINI;
1175 break;
1176 case 'i':
1177 if (strcmp (name, ".init") == 0)
1178 r_symndx = RELOC_SECTION_INIT;
1179 break;
1180 case 'l':
1181 if (strcmp (name, ".lita") == 0)
1182 r_symndx = RELOC_SECTION_LITA;
1183 else if (strcmp (name, ".lit8") == 0)
1184 r_symndx = RELOC_SECTION_LIT8;
1185 else if (strcmp (name, ".lit4") == 0)
1186 r_symndx = RELOC_SECTION_LIT4;
1187 break;
1188 case 'p':
1189 if (strcmp (name, ".pdata") == 0)
1190 r_symndx = RELOC_SECTION_PDATA;
1191 break;
1192 case 'r':
1193 if (strcmp (name, ".rdata") == 0)
1194 r_symndx = RELOC_SECTION_RDATA;
1195 break;
1196 case 's':
1197 if (strcmp (name, ".sdata") == 0)
1198 r_symndx = RELOC_SECTION_SDATA;
1199 else if (strcmp (name, ".sbss") == 0)
1200 r_symndx = RELOC_SECTION_SBSS;
1201 break;
1202 case 't':
1203 if (strcmp (name, ".text") == 0)
1204 r_symndx = RELOC_SECTION_TEXT;
1205 break;
1206 case 'x':
1207 if (strcmp (name, ".xdata") == 0)
1208 r_symndx = RELOC_SECTION_XDATA;
1209 break;
1210 }
1211
1212 if (r_symndx == -1)
1213 abort ();
1214
1215 /* Add the section VMA and the symbol value. */
1216 relocation = (h->root.u.def.value
1217 + hsec->output_section->vma
1218 + hsec->output_offset);
1219 }
1220 else
1221 {
1222 /* Change the symndx value to the right one for
1223 the output BFD. */
1224 r_symndx = h->indx;
1225 if (r_symndx == -1)
1226 {
1227 /* Caller must give an error. */
1228 r_symndx = 0;
1229 }
1230 relocation = 0;
1231 }
1232
1233 /* Write out the new r_symndx value. */
1234 bfd_h_put_32 (input_bfd, (bfd_vma) r_symndx,
1235 (bfd_byte *) ext_rel->r_symndx);
1236
1237 return relocation;
1238 }
1239
1240 /* Relocate a section while linking an Alpha ECOFF file. This is
1241 quite similar to get_relocated_section_contents. Perhaps they
1242 could be combined somehow. */
1243
1244 static boolean
1245 alpha_relocate_section (output_bfd, info, input_bfd, input_section,
1246 contents, external_relocs)
1247 bfd *output_bfd;
1248 struct bfd_link_info *info;
1249 bfd *input_bfd;
1250 asection *input_section;
1251 bfd_byte *contents;
1252 PTR external_relocs;
1253 {
1254 asection **symndx_to_section;
1255 struct ecoff_link_hash_entry **sym_hashes;
1256 bfd_vma gp;
1257 boolean gp_undefined;
1258 bfd_vma stack[RELOC_STACKSIZE];
1259 int tos = 0;
1260 struct external_reloc *ext_rel;
1261 struct external_reloc *ext_rel_end;
1262
1263 /* We keep a table mapping the symndx found in an internal reloc to
1264 the appropriate section. This is faster than looking up the
1265 section by name each time. */
1266 symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
1267 if (symndx_to_section == (asection **) NULL)
1268 {
1269 symndx_to_section = ((asection **)
1270 bfd_alloc (input_bfd,
1271 (NUM_RELOC_SECTIONS
1272 * sizeof (asection *))));
1273
1274 symndx_to_section[RELOC_SECTION_NONE] = NULL;
1275 symndx_to_section[RELOC_SECTION_TEXT] =
1276 bfd_get_section_by_name (input_bfd, ".text");
1277 symndx_to_section[RELOC_SECTION_RDATA] =
1278 bfd_get_section_by_name (input_bfd, ".rdata");
1279 symndx_to_section[RELOC_SECTION_DATA] =
1280 bfd_get_section_by_name (input_bfd, ".data");
1281 symndx_to_section[RELOC_SECTION_SDATA] =
1282 bfd_get_section_by_name (input_bfd, ".sdata");
1283 symndx_to_section[RELOC_SECTION_SBSS] =
1284 bfd_get_section_by_name (input_bfd, ".sbss");
1285 symndx_to_section[RELOC_SECTION_BSS] =
1286 bfd_get_section_by_name (input_bfd, ".bss");
1287 symndx_to_section[RELOC_SECTION_INIT] =
1288 bfd_get_section_by_name (input_bfd, ".init");
1289 symndx_to_section[RELOC_SECTION_LIT8] =
1290 bfd_get_section_by_name (input_bfd, ".lit8");
1291 symndx_to_section[RELOC_SECTION_LIT4] =
1292 bfd_get_section_by_name (input_bfd, ".lit4");
1293 symndx_to_section[RELOC_SECTION_XDATA] =
1294 bfd_get_section_by_name (input_bfd, ".xdata");
1295 symndx_to_section[RELOC_SECTION_PDATA] =
1296 bfd_get_section_by_name (input_bfd, ".pdata");
1297 symndx_to_section[RELOC_SECTION_FINI] =
1298 bfd_get_section_by_name (input_bfd, ".fini");
1299 symndx_to_section[RELOC_SECTION_LITA] =
1300 bfd_get_section_by_name (input_bfd, ".lita");
1301 symndx_to_section[RELOC_SECTION_ABS] = &bfd_abs_section;
1302
1303 ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
1304 }
1305
1306 sym_hashes = ecoff_data (input_bfd)->sym_hashes;
1307
1308 gp = ecoff_data (output_bfd)->gp;
1309 if (gp == 0)
1310 gp_undefined = true;
1311 else
1312 gp_undefined = false;
1313
1314 BFD_ASSERT (output_bfd->xvec->header_byteorder_big_p == false);
1315 BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p == false);
1316
1317 ext_rel = (struct external_reloc *) external_relocs;
1318 ext_rel_end = ext_rel + input_section->reloc_count;
1319 for (; ext_rel < ext_rel_end; ext_rel++)
1320 {
1321 bfd_vma r_vaddr;
1322 unsigned long r_symndx;
1323 int r_type;
1324 int r_extern;
1325 int r_offset;
1326 int r_size;
1327 boolean relocatep;
1328 boolean adjust_addrp;
1329 boolean gp_usedp;
1330 bfd_vma addend;
1331
1332 r_vaddr = bfd_h_get_64 (input_bfd, (bfd_byte *) ext_rel->r_vaddr);
1333 r_symndx = bfd_h_get_32 (input_bfd, (bfd_byte *) ext_rel->r_symndx);
1334
1335 r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
1336 >> RELOC_BITS0_TYPE_SH_LITTLE);
1337 r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
1338 r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
1339 >> RELOC_BITS1_OFFSET_SH_LITTLE);
1340 /* Ignored the reserved bits. */
1341 r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
1342 >> RELOC_BITS3_SIZE_SH_LITTLE);
1343
1344 relocatep = false;
1345 adjust_addrp = true;
1346 gp_usedp = false;
1347 addend = 0;
1348
1349 switch (r_type)
1350 {
1351 default:
1352 abort ();
1353
1354 case ALPHA_R_IGNORE:
1355 /* This reloc appears after a GPDISP reloc. It marks the
1356 position of the second instruction to be altered by the
1357 GPDISP reloc, but is not otherwise used for anything.
1358 For some reason, the address of the relocation does not
1359 appear to include the section VMA, unlike the other
1360 relocation types. */
1361 if (info->relocateable)
1362 bfd_h_put_64 (input_bfd,
1363 input_section->output_offset + r_vaddr,
1364 (bfd_byte *) ext_rel->r_vaddr);
1365 adjust_addrp = false;
1366 break;
1367
1368 case ALPHA_R_REFLONG:
1369 case ALPHA_R_REFQUAD:
1370 case ALPHA_R_BRADDR:
1371 case ALPHA_R_HINT:
1372 case ALPHA_R_SREL16:
1373 case ALPHA_R_SREL32:
1374 case ALPHA_R_SREL64:
1375 relocatep = true;
1376 break;
1377
1378 case ALPHA_R_GPREL32:
1379 /* This relocation is used in a switch table. It is a 32
1380 bit offset from the current GP value. We must adjust it
1381 by the different between the original GP value and the
1382 current GP value. */
1383 relocatep = true;
1384 addend = ecoff_data (input_bfd)->gp - gp;
1385 gp_usedp = true;
1386 break;
1387
1388 case ALPHA_R_LITERAL:
1389 /* This is a reference to a literal value, generally
1390 (always?) in the .lita section. This is a 16 bit GP
1391 relative relocation. Sometimes the subsequent reloc is a
1392 LITUSE reloc, which indicates how this reloc is used.
1393 This sometimes permits rewriting the two instructions
1394 referred to by the LITERAL and the LITUSE into different
1395 instructions which do not refer to .lita. This can save
1396 a memory reference, and permits removing a value from
1397 .lita thus saving GP relative space.
1398
1399 We do not these optimizations. To do them we would need
1400 to arrange to link the .lita section first, so that by
1401 the time we got here we would know the final values to
1402 use. This would not be particularly difficult, but it is
1403 not currently implemented. */
1404
1405 /* I believe that the LITERAL reloc will only apply to a ldq
1406 instruction, so check my assumption. */
1407 BFD_ASSERT (((bfd_get_32 (input_bfd,
1408 contents + r_vaddr - input_section->vma)
1409 >> 26)
1410 & 0x3f)
1411 == 0x29);
1412
1413 relocatep = true;
1414 addend = ecoff_data (input_bfd)->gp - gp;
1415 gp_usedp = true;
1416 break;
1417
1418 case ALPHA_R_LITUSE:
1419 /* See ALPHA_R_LITERAL above for the uses of this reloc. It
1420 does not cause anything to happen, itself. */
1421 break;
1422
1423 case ALPHA_R_GPDISP:
1424 /* This marks the ldah of an ldah/lda pair which loads the
1425 gp register with the difference of the gp value and the
1426 current location. The second of the pair is r_symndx
1427 bytes ahead, and is also marked with an ALPHA_R_IGNORE
1428 reloc. */
1429 {
1430 unsigned long insn1, insn2;
1431
1432 BFD_ASSERT (ext_rel + 1 < ext_rel_end
1433 && (((ext_rel + 1)->r_bits[0]
1434 & RELOC_BITS0_TYPE_LITTLE)
1435 >> RELOC_BITS0_TYPE_SH_LITTLE) == ALPHA_R_IGNORE
1436 && (bfd_h_get_64 (input_bfd,
1437 (bfd_byte *) (ext_rel + 1)->r_vaddr)
1438 == r_vaddr - input_section->vma + r_symndx));
1439
1440 /* Get the two instructions. */
1441 insn1 = bfd_get_32 (input_bfd,
1442 contents + r_vaddr - input_section->vma);
1443 insn2 = bfd_get_32 (input_bfd,
1444 (contents
1445 + r_vaddr
1446 - input_section->vma
1447 + r_symndx));
1448
1449 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */
1450 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */
1451
1452 /* Get the existing addend. We must account for the sign
1453 extension done by lda and ldah. */
1454 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff);
1455 if (insn1 & 0x8000)
1456 {
1457 /* This is addend -= 0x100000000 without causing an
1458 integer overflow on a 32 bit host. */
1459 addend -= 0x80000000;
1460 addend -= 0x80000000;
1461 }
1462 if (insn2 & 0x8000)
1463 addend -= 0x10000;
1464
1465 /* The existing addend includes the difference between the
1466 gp of the input BFD and the address in the input BFD.
1467 We want to change this to the difference between the
1468 final GP and the final address. */
1469 addend += (gp
1470 - ecoff_data (input_bfd)->gp
1471 + input_section->vma
1472 - (input_section->output_section->vma
1473 + input_section->output_offset));
1474
1475 /* Change the instructions, accounting for the sign
1476 extension, and write them out. */
1477 if (addend & 0x8000)
1478 addend += 0x10000;
1479 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff);
1480 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff);
1481
1482 bfd_put_32 (input_bfd, (bfd_vma) insn1,
1483 contents + r_vaddr - input_section->vma);
1484 bfd_put_32 (input_bfd, (bfd_vma) insn2,
1485 contents + r_vaddr - input_section->vma + r_symndx);
1486
1487 gp_usedp = true;
1488 }
1489 break;
1490
1491 case ALPHA_R_OP_PUSH:
1492 case ALPHA_R_OP_PSUB:
1493 case ALPHA_R_OP_PRSHIFT:
1494 /* Manipulate values on the reloc evaluation stack. The
1495 r_vaddr field is not an address in input_section, it is
1496 the current value (including any addend) of the object
1497 being used. */
1498 if (! r_extern)
1499 {
1500 asection *s;
1501
1502 s = symndx_to_section[r_symndx];
1503 if (s == (asection *) NULL)
1504 abort ();
1505 addend = s->output_section->vma + s->output_offset - s->vma;
1506 }
1507 else
1508 {
1509 struct ecoff_link_hash_entry *h;
1510
1511 h = sym_hashes[r_symndx];
1512 if (h == (struct ecoff_link_hash_entry *) NULL)
1513 abort ();
1514
1515 if (! info->relocateable)
1516 {
1517 if (h->root.type == bfd_link_hash_defined)
1518 addend = (h->root.u.def.value
1519 + h->root.u.def.section->output_section->vma
1520 + h->root.u.def.section->output_offset);
1521 else
1522 {
1523 /* Note that we pass the address as 0, since we
1524 do not have a meaningful number for the
1525 location within the section that is being
1526 relocated. */
1527 if (! ((*info->callbacks->undefined_symbol)
1528 (info, h->root.root.string, input_bfd,
1529 input_section, (bfd_vma) 0)))
1530 return false;
1531 addend = 0;
1532 }
1533 }
1534 else
1535 {
1536 if (h->root.type != bfd_link_hash_defined
1537 && h->indx == -1)
1538 {
1539 /* This symbol is not being written out. Pass
1540 the address as 0, as with undefined_symbol,
1541 above. */
1542 if (! ((*info->callbacks->unattached_reloc)
1543 (info, h->root.root.string, input_bfd,
1544 input_section, (bfd_vma) 0)))
1545 return false;
1546 }
1547
1548 addend = alpha_convert_external_reloc (output_bfd, info,
1549 input_bfd,
1550 ext_rel, h);
1551 }
1552 }
1553
1554 addend += r_vaddr;
1555
1556 if (info->relocateable)
1557 {
1558 /* Adjust r_vaddr by the addend. */
1559 bfd_h_put_64 (input_bfd, addend,
1560 (bfd_byte *) ext_rel->r_vaddr);
1561 }
1562 else
1563 {
1564 switch (r_type)
1565 {
1566 case ALPHA_R_OP_PUSH:
1567 if (tos >= RELOC_STACKSIZE)
1568 abort ();
1569 stack[tos++] = addend;
1570 break;
1571
1572 case ALPHA_R_OP_PSUB:
1573 if (tos == 0)
1574 abort ();
1575 stack[tos - 1] -= addend;
1576 break;
1577
1578 case ALPHA_R_OP_PRSHIFT:
1579 if (tos == 0)
1580 abort ();
1581 stack[tos - 1] >>= addend;
1582 break;
1583 }
1584 }
1585
1586 adjust_addrp = false;
1587 break;
1588
1589 case ALPHA_R_OP_STORE:
1590 /* Store a value from the reloc stack into a bitfield. If
1591 we are generating relocateable output, all we do is
1592 adjust the address of the reloc. */
1593 if (! info->relocateable)
1594 {
1595 bfd_vma val;
1596
1597 if (tos == 0)
1598 abort ();
1599
1600 /* FIXME: I don't know what kind of overflow checking,
1601 if any, should be done here. */
1602 val = bfd_get_64 (input_bfd,
1603 contents + r_vaddr - input_section->vma);
1604 val &=~ (((1 << r_size) - 1) << r_offset);
1605 val |= (stack[--tos] & ((1 << r_size) - 1)) << r_offset;
1606 bfd_put_64 (input_bfd, val,
1607 contents + r_vaddr - input_section->vma);
1608 }
1609 break;
1610
1611 case ALPHA_R_GPVALUE:
1612 /* I really don't know if this does the right thing. */
1613 gp = ecoff_data (input_bfd)->gp + r_symndx;
1614 gp_undefined = false;
1615 break;
1616 }
1617
1618 if (relocatep)
1619 {
1620 reloc_howto_type *howto;
1621 struct ecoff_link_hash_entry *h = NULL;
1622 asection *s = NULL;
1623 bfd_vma relocation;
1624 bfd_reloc_status_type r;
1625
1626 /* Perform a relocation. */
1627
1628 howto = &alpha_howto_table[r_type];
1629
1630 if (r_extern)
1631 {
1632 h = sym_hashes[r_symndx];
1633 /* If h is NULL, that means that there is a reloc
1634 against an external symbol which we thought was just
1635 a debugging symbol. This should not happen. */
1636 if (h == (struct ecoff_link_hash_entry *) NULL)
1637 abort ();
1638 }
1639 else
1640 {
1641 if (r_symndx < 0 || r_symndx >= NUM_RELOC_SECTIONS)
1642 s = NULL;
1643 else
1644 s = symndx_to_section[r_symndx];
1645
1646 if (s == (asection *) NULL)
1647 abort ();
1648 }
1649
1650 if (info->relocateable)
1651 {
1652 /* We are generating relocateable output, and must
1653 convert the existing reloc. */
1654 if (r_extern)
1655 {
1656 if (h->root.type != bfd_link_hash_defined
1657 && h->indx == -1)
1658 {
1659 /* This symbol is not being written out. */
1660 if (! ((*info->callbacks->unattached_reloc)
1661 (info, h->root.root.string, input_bfd,
1662 input_section, r_vaddr - input_section->vma)))
1663 return false;
1664 }
1665
1666 relocation = alpha_convert_external_reloc (output_bfd,
1667 info,
1668 input_bfd,
1669 ext_rel,
1670 h);
1671 }
1672 else
1673 {
1674 /* This is a relocation against a section. Adjust
1675 the value by the amount the section moved. */
1676 relocation = (s->output_section->vma
1677 + s->output_offset
1678 - s->vma);
1679 }
1680
1681 /* If this is PC relative, the existing object file
1682 appears to already have the reloc worked out. We
1683 must subtract out the old value and add in the new
1684 one. */
1685 if (howto->pc_relative)
1686 relocation -= (input_section->output_section->vma
1687 + input_section->output_offset
1688 - input_section->vma);
1689
1690 /* Put in any addend. */
1691 relocation += addend;
1692
1693 /* Adjust the contents. */
1694 r = _bfd_relocate_contents (howto, input_bfd, relocation,
1695 (contents
1696 + r_vaddr
1697 - input_section->vma));
1698 }
1699 else
1700 {
1701 /* We are producing a final executable. */
1702 if (r_extern)
1703 {
1704 /* This is a reloc against a symbol. */
1705 if (h->root.type == bfd_link_hash_defined)
1706 {
1707 asection *hsec;
1708
1709 hsec = h->root.u.def.section;
1710 relocation = (h->root.u.def.value
1711 + hsec->output_section->vma
1712 + hsec->output_offset);
1713 }
1714 else
1715 {
1716 if (! ((*info->callbacks->undefined_symbol)
1717 (info, h->root.root.string, input_bfd,
1718 input_section,
1719 r_vaddr - input_section->vma)))
1720 return false;
1721 relocation = 0;
1722 }
1723 }
1724 else
1725 {
1726 /* This is a reloc against a section. */
1727 relocation = (s->output_section->vma
1728 + s->output_offset
1729 - s->vma);
1730
1731 /* Adjust a PC relative relocation by removing the
1732 reference to the original source section. */
1733 if (howto->pc_relative)
1734 relocation += input_section->vma;
1735 }
1736
1737 r = _bfd_final_link_relocate (howto,
1738 input_bfd,
1739 input_section,
1740 contents,
1741 r_vaddr - input_section->vma,
1742 relocation,
1743 addend);
1744 }
1745
1746 if (r != bfd_reloc_ok)
1747 {
1748 switch (r)
1749 {
1750 default:
1751 case bfd_reloc_outofrange:
1752 abort ();
1753 case bfd_reloc_overflow:
1754 {
1755 const char *name;
1756
1757 if (r_extern)
1758 name = sym_hashes[r_symndx]->root.root.string;
1759 else
1760 name = bfd_section_name (input_bfd,
1761 symndx_to_section[r_symndx]);
1762 if (! ((*info->callbacks->reloc_overflow)
1763 (info, name, alpha_howto_table[r_type].name,
1764 (bfd_vma) 0, input_bfd, input_section,
1765 r_vaddr - input_section->vma)))
1766 return false;
1767 }
1768 break;
1769 }
1770 }
1771 }
1772
1773 if (info->relocateable && adjust_addrp)
1774 {
1775 /* Change the address of the relocation. */
1776 bfd_h_put_64 (input_bfd,
1777 (input_section->output_section->vma
1778 + input_section->output_offset
1779 - input_section->vma
1780 + r_vaddr),
1781 (bfd_byte *) ext_rel->r_vaddr);
1782 }
1783
1784 if (gp_usedp && gp_undefined)
1785 {
1786 if (! ((*info->callbacks->reloc_dangerous)
1787 (info, "GP relative relocation when GP not defined",
1788 input_bfd, input_section, r_vaddr - input_section->vma)))
1789 return false;
1790 /* Only give the error once per link. */
1791 ecoff_data (output_bfd)->gp = gp = 4;
1792 gp_undefined = false;
1793 }
1794 }
1795
1796 if (tos != 0)
1797 abort ();
1798
1799 return true;
1800 }
1801 \f
1802 #define ecoff_core_file_p _bfd_dummy_target
1803 #define ecoff_core_file_failing_command _bfd_dummy_core_file_failing_command
1804 #define ecoff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
1805 #define ecoff_core_file_matches_executable_p \
1806 _bfd_dummy_core_file_matches_executable_p
1807 \f
1808 /* This is the ECOFF backend structure. The backend field of the
1809 target vector points to this. */
1810
1811 static const struct ecoff_backend_data alpha_ecoff_backend_data =
1812 {
1813 /* COFF backend structure. */
1814 {
1815 (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
1816 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
1817 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
1818 (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
1819 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
1820 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
1821 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */
1822 alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out,
1823 alpha_ecoff_swap_scnhdr_out,
1824 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
1825 alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in,
1826 alpha_ecoff_swap_scnhdr_in, alpha_ecoff_bad_format_hook,
1827 ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
1828 ecoff_styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
1829 ecoff_slurp_symbol_table, NULL, NULL
1830 },
1831 /* Supported architecture. */
1832 bfd_arch_alpha,
1833 /* Initial portion of armap string. */
1834 "________64",
1835 /* The page boundary used to align sections in a demand-paged
1836 executable file. E.g., 0x1000. */
1837 0x2000,
1838 /* True if the .rdata section is part of the text segment, as on the
1839 Alpha. False if .rdata is part of the data segment, as on the
1840 MIPS. */
1841 true,
1842 /* Bitsize of constructor entries. */
1843 64,
1844 /* Reloc to use for constructor entries. */
1845 &alpha_howto_table[ALPHA_R_REFQUAD],
1846 {
1847 /* Symbol table magic number. */
1848 magicSym2,
1849 /* Alignment of debugging information. E.g., 4. */
1850 8,
1851 /* Sizes of external symbolic information. */
1852 sizeof (struct hdr_ext),
1853 sizeof (struct dnr_ext),
1854 sizeof (struct pdr_ext),
1855 sizeof (struct sym_ext),
1856 sizeof (struct opt_ext),
1857 sizeof (struct fdr_ext),
1858 sizeof (struct rfd_ext),
1859 sizeof (struct ext_ext),
1860 /* Functions to swap in external symbolic data. */
1861 ecoff_swap_hdr_in,
1862 ecoff_swap_dnr_in,
1863 ecoff_swap_pdr_in,
1864 ecoff_swap_sym_in,
1865 ecoff_swap_opt_in,
1866 ecoff_swap_fdr_in,
1867 ecoff_swap_rfd_in,
1868 ecoff_swap_ext_in,
1869 /* Functions to swap out external symbolic data. */
1870 ecoff_swap_hdr_out,
1871 ecoff_swap_dnr_out,
1872 ecoff_swap_pdr_out,
1873 ecoff_swap_sym_out,
1874 ecoff_swap_opt_out,
1875 ecoff_swap_fdr_out,
1876 ecoff_swap_rfd_out,
1877 ecoff_swap_ext_out
1878 },
1879 /* External reloc size. */
1880 RELSZ,
1881 /* Reloc swapping functions. */
1882 alpha_ecoff_swap_reloc_in,
1883 alpha_ecoff_swap_reloc_out,
1884 /* Backend reloc tweaking. */
1885 alpha_adjust_reloc_in,
1886 alpha_adjust_reloc_out,
1887 /* Relocate section contents while linking. */
1888 alpha_relocate_section
1889 };
1890
1891 /* Looking up a reloc type is Alpha specific. */
1892 #define ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup
1893
1894 /* So is getting relocated section contents. */
1895 #define ecoff_bfd_get_relocated_section_contents \
1896 alpha_ecoff_get_relocated_section_contents
1897
1898 bfd_target ecoffalpha_little_vec =
1899 {
1900 "ecoff-littlealpha", /* name */
1901 bfd_target_ecoff_flavour,
1902 false, /* data byte order is little */
1903 false, /* header byte order is little */
1904
1905 (HAS_RELOC | EXEC_P | /* object flags */
1906 HAS_LINENO | HAS_DEBUG |
1907 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1908
1909 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
1910 flags */
1911 0, /* leading underscore */
1912 ' ', /* ar_pad_char */
1913 15, /* ar_max_namelen */
1914 4, /* minimum alignment power */
1915 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1916 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1917 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1918 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1919 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1920 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
1921
1922 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1923 ecoff_archive_p, _bfd_dummy_target},
1924 {bfd_false, ecoff_mkobject, /* bfd_set_format */
1925 _bfd_generic_mkarchive, bfd_false},
1926 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
1927 _bfd_write_archive_contents, bfd_false},
1928 JUMP_TABLE (ecoff),
1929 (PTR) &alpha_ecoff_backend_data
1930 };
This page took 0.070041 seconds and 5 git commands to generate.