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