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