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