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