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