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