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