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