elf64-alpha.c revision 59161
1/* Alpha specific support for 64-bit ELF
2   Copyright 1996, 1997, 1998 Free Software Foundation, Inc.
3   Contributed by Richard Henderson <rth@tamu.edu>.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21/* $FreeBSD: head/contrib/binutils/bfd/elf64-alpha.c 59161 2000-04-12 03:43:09Z obrien $ */
22
23/* We need a published ABI spec for this.  Until one comes out, don't
24   assume this'll remain unchanged forever.  */
25
26#include "bfd.h"
27#include "sysdep.h"
28#include "libbfd.h"
29#include "elf-bfd.h"
30
31#include "elf/alpha.h"
32
33#define ALPHAECOFF
34
35#define NO_COFF_RELOCS
36#define NO_COFF_SYMBOLS
37#define NO_COFF_LINENOS
38
39/* Get the ECOFF swapping routines.  Needed for the debug information. */
40#include "coff/internal.h"
41#include "coff/sym.h"
42#include "coff/symconst.h"
43#include "coff/ecoff.h"
44#include "coff/alpha.h"
45#include "aout/ar.h"
46#include "libcoff.h"
47#include "libecoff.h"
48#define ECOFF_64
49#include "ecoffswap.h"
50
51static boolean elf64_alpha_mkobject PARAMS ((bfd *));
52static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
53  PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
54static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
55  PARAMS((bfd *));
56
57static bfd_reloc_status_type elf64_alpha_reloc_nil
58  PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59static bfd_reloc_status_type elf64_alpha_reloc_bad
60  PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
61static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
62  PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
63static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
64  PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
65
66static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
67  PARAMS((bfd *, bfd_reloc_code_real_type));
68static void elf64_alpha_info_to_howto
69  PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
70
71static boolean elf64_alpha_object_p
72  PARAMS((bfd *));
73static boolean elf64_alpha_section_from_shdr
74  PARAMS((bfd *, Elf64_Internal_Shdr *, char *));
75static boolean elf64_alpha_fake_sections
76  PARAMS((bfd *, Elf64_Internal_Shdr *, asection *));
77static int elf64_alpha_additional_program_headers
78  PARAMS((bfd *));
79static boolean elf64_alpha_create_got_section
80  PARAMS((bfd *, struct bfd_link_info *));
81static boolean elf64_alpha_create_dynamic_sections
82  PARAMS((bfd *, struct bfd_link_info *));
83
84static boolean elf64_alpha_read_ecoff_info
85  PARAMS((bfd *, asection *, struct ecoff_debug_info *));
86static boolean elf64_alpha_is_local_label_name
87  PARAMS((bfd *, const char *));
88static boolean elf64_alpha_find_nearest_line
89  PARAMS((bfd *, asection *, asymbol **, bfd_vma, const char **,
90	  const char **, unsigned int *));
91
92#if defined(__STDC__) || defined(ALMOST_STDC)
93struct alpha_elf_link_hash_entry;
94#endif
95
96static boolean elf64_alpha_output_extsym
97  PARAMS((struct alpha_elf_link_hash_entry *, PTR));
98
99static boolean elf64_alpha_can_merge_gots
100  PARAMS((bfd *, bfd *));
101static void elf64_alpha_merge_gots
102  PARAMS((bfd *, bfd *));
103static boolean elf64_alpha_calc_got_offsets_for_symbol
104  PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
105static void elf64_alpha_calc_got_offsets PARAMS ((struct bfd_link_info *));
106static void elf64_alpha_strip_section_from_output PARAMS ((asection *));
107static boolean elf64_alpha_always_size_sections
108  PARAMS ((bfd *, struct bfd_link_info *));
109static boolean elf64_alpha_calc_dynrel_sizes
110  PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
111static boolean elf64_alpha_check_relocs
112  PARAMS((bfd *, struct bfd_link_info *, asection *sec,
113	  const Elf_Internal_Rela *));
114static boolean elf64_alpha_adjust_dynamic_symbol
115  PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
116static boolean elf64_alpha_size_dynamic_sections
117  PARAMS((bfd *, struct bfd_link_info *));
118static boolean elf64_alpha_adjust_dynindx
119  PARAMS((struct elf_link_hash_entry *, PTR));
120static boolean elf64_alpha_relocate_section
121  PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
122	  Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
123static boolean elf64_alpha_finish_dynamic_symbol
124  PARAMS((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
125	  Elf_Internal_Sym *));
126static boolean elf64_alpha_finish_dynamic_sections
127  PARAMS((bfd *, struct bfd_link_info *));
128static boolean elf64_alpha_final_link
129  PARAMS((bfd *, struct bfd_link_info *));
130static boolean elf64_alpha_merge_ind_symbols
131  PARAMS((struct alpha_elf_link_hash_entry *, PTR));
132
133
134struct alpha_elf_link_hash_entry
135{
136  struct elf_link_hash_entry root;
137
138  /* External symbol information.  */
139  EXTR esym;
140
141  /* Cumulative flags for all the .got entries.  */
142  int flags;
143
144  /* Contexts (LITUSE) in which a literal was referenced.  */
145#define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
146#define ALPHA_ELF_LINK_HASH_LU_MEM  0x02
147#define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
148#define ALPHA_ELF_LINK_HASH_LU_FUNC 0x08
149
150  /* Used to implement multiple .got subsections.  */
151  struct alpha_elf_got_entry
152  {
153    struct alpha_elf_got_entry *next;
154
155    /* which .got subsection?  */
156    bfd *gotobj;
157
158    /* the addend in effect for this entry.  */
159    bfd_vma addend;
160
161    /* the .got offset for this entry.  */
162    int got_offset;
163
164    int flags;
165
166    /* An additional flag.  */
167#define ALPHA_ELF_GOT_ENTRY_RELOCS_DONE 0x10
168  } *got_entries;
169
170  /* used to count non-got, non-plt relocations for delayed sizing
171     of relocation sections.  */
172  struct alpha_elf_reloc_entry
173  {
174    struct alpha_elf_reloc_entry *next;
175
176    /* which .reloc section? */
177    asection *srel;
178
179    /* what kind of relocation? */
180    unsigned long rtype;
181
182    /* how many did we find?  */
183    unsigned long count;
184  } *reloc_entries;
185};
186
187/* Alpha ELF linker hash table.  */
188
189struct alpha_elf_link_hash_table
190{
191  struct elf_link_hash_table root;
192
193  /* The head of a list of .got subsections linked through
194     alpha_elf_tdata(abfd)->got_link_next.  */
195  bfd *got_list;
196};
197
198/* Look up an entry in a Alpha ELF linker hash table.  */
199
200#define alpha_elf_link_hash_lookup(table, string, create, copy, follow)	\
201  ((struct alpha_elf_link_hash_entry *)					\
202   elf_link_hash_lookup (&(table)->root, (string), (create),		\
203			 (copy), (follow)))
204
205/* Traverse a Alpha ELF linker hash table.  */
206
207#define alpha_elf_link_hash_traverse(table, func, info)			\
208  (elf_link_hash_traverse						\
209   (&(table)->root,							\
210    (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),	\
211    (info)))
212
213/* Get the Alpha ELF linker hash table from a link_info structure.  */
214
215#define alpha_elf_hash_table(p) \
216  ((struct alpha_elf_link_hash_table *) ((p)->hash))
217
218/* Get the object's symbols as our own entry type.  */
219
220#define alpha_elf_sym_hashes(abfd) \
221  ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
222
223/* Should we do forced local to this symbol?  */
224
225#define alpha_elf_force_local_symbol_p(h, info)				\
226  ((info)->shared							\
227   && ((h)->dynindx == -1						\
228       || ((info)->symbolic						\
229	   && ((h)->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))))
230
231/* Should we do dynamic things to this symbol?  */
232
233#define alpha_elf_dynamic_symbol_p(h, info) 				\
234  (((info)->shared							\
235    && ((h)->root.type == bfd_link_hash_undefweak			\
236	|| !alpha_elf_force_local_symbol_p ((h), (info))))		\
237   || (((h)->elf_link_hash_flags					\
238	& (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))	\
239       == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
240
241/* Create an entry in a Alpha ELF linker hash table.  */
242
243static struct bfd_hash_entry *
244elf64_alpha_link_hash_newfunc (entry, table, string)
245     struct bfd_hash_entry *entry;
246     struct bfd_hash_table *table;
247     const char *string;
248{
249  struct alpha_elf_link_hash_entry *ret =
250    (struct alpha_elf_link_hash_entry *) entry;
251
252  /* Allocate the structure if it has not already been allocated by a
253     subclass.  */
254  if (ret == (struct alpha_elf_link_hash_entry *) NULL)
255    ret = ((struct alpha_elf_link_hash_entry *)
256	   bfd_hash_allocate (table,
257			      sizeof (struct alpha_elf_link_hash_entry)));
258  if (ret == (struct alpha_elf_link_hash_entry *) NULL)
259    return (struct bfd_hash_entry *) ret;
260
261  /* Call the allocation method of the superclass.  */
262  ret = ((struct alpha_elf_link_hash_entry *)
263	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
264				     table, string));
265  if (ret != (struct alpha_elf_link_hash_entry *) NULL)
266    {
267      /* Set local fields.  */
268      memset (&ret->esym, 0, sizeof (EXTR));
269      /* We use -2 as a marker to indicate that the information has
270	 not been set.  -1 means there is no associated ifd.  */
271      ret->esym.ifd = -2;
272      ret->flags = 0;
273      ret->got_entries = NULL;
274      ret->reloc_entries = NULL;
275    }
276
277  return (struct bfd_hash_entry *) ret;
278}
279
280/* Create a Alpha ELF linker hash table.  */
281
282static struct bfd_link_hash_table *
283elf64_alpha_bfd_link_hash_table_create (abfd)
284     bfd *abfd;
285{
286  struct alpha_elf_link_hash_table *ret;
287
288  ret = ((struct alpha_elf_link_hash_table *)
289	 bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
290  if (ret == (struct alpha_elf_link_hash_table *) NULL)
291    return NULL;
292
293  if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
294				       elf64_alpha_link_hash_newfunc))
295    {
296      bfd_release (abfd, ret);
297      return NULL;
298    }
299
300  return &ret->root.root;
301}
302
303/* We have some private fields hanging off of the elf_tdata structure.  */
304
305struct alpha_elf_obj_tdata
306{
307  struct elf_obj_tdata root;
308
309  /* For every input file, these are the got entries for that object's
310     local symbols.  */
311  struct alpha_elf_got_entry ** local_got_entries;
312
313  /* For every input file, this is the object that owns the got that
314     this input file uses.  */
315  bfd *gotobj;
316
317  /* For every got, this is a linked list through the objects using this got */
318  bfd *in_got_link_next;
319
320  /* For every got, this is a link to the next got subsegment.  */
321  bfd *got_link_next;
322
323  /* For every got, this is the section.  */
324  asection *got;
325
326  /* For every got, this is it's total number of *entries*.  */
327  int total_got_entries;
328
329  /* For every got, this is the sum of the number of *entries* required
330     to hold all of the member object's local got.  */
331  int n_local_got_entries;
332};
333
334#define alpha_elf_tdata(abfd) \
335  ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
336
337static boolean
338elf64_alpha_mkobject (abfd)
339     bfd *abfd;
340{
341  abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
342  if (abfd->tdata.any == NULL)
343    return false;
344  return true;
345}
346
347static boolean
348elf64_alpha_object_p (abfd)
349     bfd *abfd;
350{
351  /* Allocate our special target data.  */
352  struct alpha_elf_obj_tdata *new_tdata;
353  new_tdata = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
354  if (new_tdata == NULL)
355    return false;
356  new_tdata->root = *abfd->tdata.elf_obj_data;
357  abfd->tdata.any = new_tdata;
358
359  /* Set the right machine number for an Alpha ELF file.  */
360  return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
361}
362
363/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
364   from smaller values.  Start with zero, widen, *then* decrement.  */
365#define MINUS_ONE	(((bfd_vma)0) - 1)
366
367static reloc_howto_type elf64_alpha_howto_table[] =
368{
369  HOWTO (R_ALPHA_NONE,		/* type */
370	 0,			/* rightshift */
371	 0,			/* size (0 = byte, 1 = short, 2 = long) */
372	 8,			/* bitsize */
373	 true,			/* pc_relative */
374	 0,			/* bitpos */
375	 complain_overflow_dont, /* complain_on_overflow */
376	 elf64_alpha_reloc_nil,	/* special_function */
377	 "NONE",		/* name */
378	 false,			/* partial_inplace */
379	 0,			/* src_mask */
380	 0,			/* dst_mask */
381	 true),			/* pcrel_offset */
382
383  /* A 32 bit reference to a symbol.  */
384  HOWTO (R_ALPHA_REFLONG,	/* type */
385	 0,			/* rightshift */
386	 2,			/* size (0 = byte, 1 = short, 2 = long) */
387	 32,			/* bitsize */
388	 false,			/* pc_relative */
389	 0,			/* bitpos */
390	 complain_overflow_bitfield, /* complain_on_overflow */
391	 0,			/* special_function */
392	 "REFLONG",		/* name */
393	 false,			/* partial_inplace */
394	 0xffffffff,		/* src_mask */
395	 0xffffffff,		/* dst_mask */
396	 false),		/* pcrel_offset */
397
398  /* A 64 bit reference to a symbol.  */
399  HOWTO (R_ALPHA_REFQUAD,	/* type */
400	 0,			/* rightshift */
401	 4,			/* size (0 = byte, 1 = short, 2 = long) */
402	 64,			/* bitsize */
403	 false,			/* pc_relative */
404	 0,			/* bitpos */
405	 complain_overflow_bitfield, /* complain_on_overflow */
406	 0,			/* special_function */
407	 "REFQUAD",		/* name */
408	 false,			/* partial_inplace */
409	 MINUS_ONE,		/* src_mask */
410	 MINUS_ONE,		/* dst_mask */
411	 false),		/* pcrel_offset */
412
413  /* A 32 bit GP relative offset.  This is just like REFLONG except
414     that when the value is used the value of the gp register will be
415     added in.  */
416  HOWTO (R_ALPHA_GPREL32,	/* type */
417	 0,			/* rightshift */
418	 2,			/* size (0 = byte, 1 = short, 2 = long) */
419	 32,			/* bitsize */
420	 false,			/* pc_relative */
421	 0,			/* bitpos */
422	 complain_overflow_bitfield, /* complain_on_overflow */
423	 0,			/* special_function */
424	 "GPREL32",		/* name */
425	 false,			/* partial_inplace */
426	 0xffffffff,		/* src_mask */
427	 0xffffffff,		/* dst_mask */
428	 false),		/* pcrel_offset */
429
430  /* Used for an instruction that refers to memory off the GP register.  */
431  HOWTO (R_ALPHA_LITERAL,	/* type */
432	 0,			/* rightshift */
433	 2,			/* size (0 = byte, 1 = short, 2 = long) */
434	 16,			/* bitsize */
435	 false,			/* pc_relative */
436	 0,			/* bitpos */
437	 complain_overflow_signed, /* complain_on_overflow */
438	 0,			/* special_function */
439	 "ELF_LITERAL",		/* name */
440	 false,			/* partial_inplace */
441	 0xffff,		/* src_mask */
442	 0xffff,		/* dst_mask */
443	 false),		/* pcrel_offset */
444
445  /* This reloc only appears immediately following an ELF_LITERAL reloc.
446     It identifies a use of the literal.  The symbol index is special:
447     1 means the literal address is in the base register of a memory
448     format instruction; 2 means the literal address is in the byte
449     offset register of a byte-manipulation instruction; 3 means the
450     literal address is in the target register of a jsr instruction.
451     This does not actually do any relocation.  */
452  HOWTO (R_ALPHA_LITUSE,	/* type */
453	 0,			/* rightshift */
454	 2,			/* size (0 = byte, 1 = short, 2 = long) */
455	 32,			/* bitsize */
456	 false,			/* pc_relative */
457	 0,			/* bitpos */
458	 complain_overflow_dont, /* complain_on_overflow */
459	 elf64_alpha_reloc_nil,	/* special_function */
460	 "LITUSE",		/* name */
461	 false,			/* partial_inplace */
462	 0,			/* src_mask */
463	 0,			/* dst_mask */
464	 false),		/* pcrel_offset */
465
466  /* Load the gp register.  This is always used for a ldah instruction
467     which loads the upper 16 bits of the gp register.  The symbol
468     index of the GPDISP instruction is an offset in bytes to the lda
469     instruction that loads the lower 16 bits.  The value to use for
470     the relocation is the difference between the GP value and the
471     current location; the load will always be done against a register
472     holding the current address.
473
474     NOTE: Unlike ECOFF, partial in-place relocation is not done.  If
475     any offset is present in the instructions, it is an offset from
476     the register to the ldah instruction.  This lets us avoid any
477     stupid hackery like inventing a gp value to do partial relocation
478     against.  Also unlike ECOFF, we do the whole relocation off of
479     the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair.  An odd,
480     space consuming bit, that, since all the information was present
481     in the GPDISP_HI16 reloc.  */
482  HOWTO (R_ALPHA_GPDISP,	/* type */
483	 16,			/* rightshift */
484	 2,			/* size (0 = byte, 1 = short, 2 = long) */
485	 16,			/* bitsize */
486	 false,			/* pc_relative */
487	 0,			/* bitpos */
488	 complain_overflow_dont, /* complain_on_overflow */
489	 elf64_alpha_reloc_gpdisp, /* special_function */
490	 "GPDISP",		/* name */
491	 false,			/* partial_inplace */
492	 0xffff,		/* src_mask */
493	 0xffff,		/* dst_mask */
494	 true),			/* pcrel_offset */
495
496  /* A 21 bit branch.  */
497  HOWTO (R_ALPHA_BRADDR,	/* type */
498	 2,			/* rightshift */
499	 2,			/* size (0 = byte, 1 = short, 2 = long) */
500	 21,			/* bitsize */
501	 true,			/* pc_relative */
502	 0,			/* bitpos */
503	 complain_overflow_signed, /* complain_on_overflow */
504	 0,			/* special_function */
505	 "BRADDR",		/* name */
506	 false,			/* partial_inplace */
507	 0x1fffff,		/* src_mask */
508	 0x1fffff,		/* dst_mask */
509	 true),			/* pcrel_offset */
510
511  /* A hint for a jump to a register.  */
512  HOWTO (R_ALPHA_HINT,		/* type */
513	 2,			/* rightshift */
514	 2,			/* size (0 = byte, 1 = short, 2 = long) */
515	 14,			/* bitsize */
516	 true,			/* pc_relative */
517	 0,			/* bitpos */
518	 complain_overflow_dont, /* complain_on_overflow */
519	 0,			/* special_function */
520	 "HINT",		/* name */
521	 false,			/* partial_inplace */
522	 0x3fff,		/* src_mask */
523	 0x3fff,		/* dst_mask */
524	 true),			/* pcrel_offset */
525
526  /* 16 bit PC relative offset.  */
527  HOWTO (R_ALPHA_SREL16,	/* type */
528	 0,			/* rightshift */
529	 1,			/* size (0 = byte, 1 = short, 2 = long) */
530	 16,			/* bitsize */
531	 true,			/* pc_relative */
532	 0,			/* bitpos */
533	 complain_overflow_signed, /* complain_on_overflow */
534	 0,			/* special_function */
535	 "SREL16",		/* name */
536	 false,			/* partial_inplace */
537	 0xffff,		/* src_mask */
538	 0xffff,		/* dst_mask */
539	 false),		/* pcrel_offset */
540
541  /* 32 bit PC relative offset.  */
542  HOWTO (R_ALPHA_SREL32,	/* type */
543	 0,			/* rightshift */
544	 2,			/* size (0 = byte, 1 = short, 2 = long) */
545	 32,			/* bitsize */
546	 true,			/* pc_relative */
547	 0,			/* bitpos */
548	 complain_overflow_signed, /* complain_on_overflow */
549	 0,			/* special_function */
550	 "SREL32",		/* name */
551	 false,			/* partial_inplace */
552	 0xffffffff,		/* src_mask */
553	 0xffffffff,		/* dst_mask */
554	 false),		/* pcrel_offset */
555
556  /* A 64 bit PC relative offset.  */
557  HOWTO (R_ALPHA_SREL64,	/* type */
558	 0,			/* rightshift */
559	 4,			/* size (0 = byte, 1 = short, 2 = long) */
560	 64,			/* bitsize */
561	 true,			/* pc_relative */
562	 0,			/* bitpos */
563	 complain_overflow_signed, /* complain_on_overflow */
564	 0,			/* special_function */
565	 "SREL64",		/* name */
566	 false,			/* partial_inplace */
567	 MINUS_ONE,		/* src_mask */
568	 MINUS_ONE,		/* dst_mask */
569	 false),		/* pcrel_offset */
570
571  /* Push a value on the reloc evaluation stack.  */
572  /* Not implemented -- it's dumb.  */
573  HOWTO (R_ALPHA_OP_PUSH,	/* type */
574	 0,			/* rightshift */
575	 0,			/* size (0 = byte, 1 = short, 2 = long) */
576	 0,			/* bitsize */
577	 false,			/* pc_relative */
578	 0,			/* bitpos */
579	 complain_overflow_dont, /* complain_on_overflow */
580	 elf64_alpha_reloc_bad, /* special_function */
581	 "OP_PUSH",		/* name */
582	 false,			/* partial_inplace */
583	 0,			/* src_mask */
584	 0,			/* dst_mask */
585	 false),		/* pcrel_offset */
586
587  /* Store the value from the stack at the given address.  Store it in
588     a bitfield of size r_size starting at bit position r_offset.  */
589  /* Not implemented -- it's dumb.  */
590  HOWTO (R_ALPHA_OP_STORE,	/* type */
591	 0,			/* rightshift */
592	 4,			/* size (0 = byte, 1 = short, 2 = long) */
593	 64,			/* bitsize */
594	 false,			/* pc_relative */
595	 0,			/* bitpos */
596	 complain_overflow_dont, /* complain_on_overflow */
597	 elf64_alpha_reloc_bad, /* special_function */
598	 "OP_STORE",		/* name */
599	 false,			/* partial_inplace */
600	 0,			/* src_mask */
601	 MINUS_ONE,		/* dst_mask */
602	 false),		/* pcrel_offset */
603
604  /* Subtract the reloc address from the value on the top of the
605     relocation stack.  */
606  /* Not implemented -- it's dumb.  */
607  HOWTO (R_ALPHA_OP_PSUB,	/* type */
608	 0,			/* rightshift */
609	 0,			/* size (0 = byte, 1 = short, 2 = long) */
610	 0,			/* bitsize */
611	 false,			/* pc_relative */
612	 0,			/* bitpos */
613	 complain_overflow_dont, /* complain_on_overflow */
614	 elf64_alpha_reloc_bad, /* special_function */
615	 "OP_PSUB",		/* name */
616	 false,			/* partial_inplace */
617	 0,			/* src_mask */
618	 0,			/* dst_mask */
619	 false),		/* pcrel_offset */
620
621  /* Shift the value on the top of the relocation stack right by the
622     given value.  */
623  /* Not implemented -- it's dumb.  */
624  HOWTO (R_ALPHA_OP_PRSHIFT,	/* type */
625	 0,			/* rightshift */
626	 0,			/* size (0 = byte, 1 = short, 2 = long) */
627	 0,			/* bitsize */
628	 false,			/* pc_relative */
629	 0,			/* bitpos */
630	 complain_overflow_dont, /* complain_on_overflow */
631	 elf64_alpha_reloc_bad, /* special_function */
632	 "OP_PRSHIFT",		/* name */
633	 false,			/* partial_inplace */
634	 0,			/* src_mask */
635	 0,			/* dst_mask */
636	 false),		/* pcrel_offset */
637
638  /* Change the value of GP used by +r_addend until the next GPVALUE or the
639     end of the input bfd.  */
640  /* Not implemented -- it's dumb.  */
641  HOWTO (R_ALPHA_GPVALUE,
642	 0,			/* rightshift */
643	 0,			/* size (0 = byte, 1 = short, 2 = long) */
644	 0,			/* bitsize */
645	 false,			/* pc_relative */
646	 0,			/* bitpos */
647	 complain_overflow_dont, /* complain_on_overflow */
648	 elf64_alpha_reloc_bad, /* special_function */
649	 "GPVALUE",		/* name */
650	 false,			/* partial_inplace */
651	 0,			/* src_mask */
652	 0,			/* dst_mask */
653	 false),		/* pcrel_offset */
654
655  /* The high 16 bits of the displacement from GP to the target.  */
656  /* XXX: Not implemented.  */
657  HOWTO (R_ALPHA_GPRELHIGH,
658	 0,			/* rightshift */
659	 0,			/* size (0 = byte, 1 = short, 2 = long) */
660	 0,			/* bitsize */
661	 false,			/* pc_relative */
662	 0,			/* bitpos */
663	 complain_overflow_dont, /* complain_on_overflow */
664	 elf64_alpha_reloc_bad, /* special_function */
665	 "GPRELHIGH",		/* name */
666	 false,			/* partial_inplace */
667	 0,			/* src_mask */
668	 0,			/* dst_mask */
669	 false),		/* pcrel_offset */
670
671  /* The low 16 bits of the displacement from GP to the target.  */
672  /* XXX: Not implemented.  */
673  HOWTO (R_ALPHA_GPRELLOW,
674	 0,			/* rightshift */
675	 0,			/* size (0 = byte, 1 = short, 2 = long) */
676	 0,			/* bitsize */
677	 false,			/* pc_relative */
678	 0,			/* bitpos */
679	 complain_overflow_dont, /* complain_on_overflow */
680	 elf64_alpha_reloc_bad, /* special_function */
681	 "GPRELLOW",		/* name */
682	 false,			/* partial_inplace */
683	 0,			/* src_mask */
684	 0,			/* dst_mask */
685	 false),		/* pcrel_offset */
686
687  /* A 16-bit displacement from the GP to the target.  */
688  /* XXX: Not implemented.  */
689  HOWTO (R_ALPHA_IMMED_GP_16,
690	 0,			/* rightshift */
691	 0,			/* size (0 = byte, 1 = short, 2 = long) */
692	 0,			/* bitsize */
693	 false,			/* pc_relative */
694	 0,			/* bitpos */
695	 complain_overflow_dont, /* complain_on_overflow */
696	 elf64_alpha_reloc_bad, /* special_function */
697	 "IMMED_GP_16",		/* name */
698	 false,			/* partial_inplace */
699	 0,			/* src_mask */
700	 0,			/* dst_mask */
701	 false),		/* pcrel_offset */
702
703  /* The high bits of a 32-bit displacement from the GP to the target; the
704     low bits are supplied in the subsequent R_ALPHA_IMMED_LO32 relocs.  */
705  /* XXX: Not implemented.  */
706  HOWTO (R_ALPHA_IMMED_GP_HI32,
707	 0,			/* rightshift */
708	 0,			/* size (0 = byte, 1 = short, 2 = long) */
709	 0,			/* bitsize */
710	 false,			/* pc_relative */
711	 0,			/* bitpos */
712	 complain_overflow_dont, /* complain_on_overflow */
713	 elf64_alpha_reloc_bad, /* special_function */
714	 "IMMED_GP_HI32",		/* name */
715	 false,			/* partial_inplace */
716	 0,			/* src_mask */
717	 0,			/* dst_mask */
718	 false),		/* pcrel_offset */
719
720  /* The high bits of a 32-bit displacement to the starting address of the
721     current section (the relocation target is ignored); the low bits are
722     supplied in the subsequent R_ALPHA_IMMED_LO32 relocs.  */
723  /* XXX: Not implemented.  */
724  HOWTO (R_ALPHA_IMMED_SCN_HI32,
725	 0,			/* rightshift */
726	 0,			/* size (0 = byte, 1 = short, 2 = long) */
727	 0,			/* bitsize */
728	 false,			/* pc_relative */
729	 0,			/* bitpos */
730	 complain_overflow_dont, /* complain_on_overflow */
731	 elf64_alpha_reloc_bad, /* special_function */
732	 "IMMED_SCN_HI32",		/* name */
733	 false,			/* partial_inplace */
734	 0,			/* src_mask */
735	 0,			/* dst_mask */
736	 false),		/* pcrel_offset */
737
738  /* The high bits of a 32-bit displacement from the previous br, bsr, jsr
739     or jmp insn (as tagged by a BRADDR or HINT reloc) to the target; the
740     low bits are supplied by subsequent R_ALPHA_IMMED_LO32 relocs.  */
741  /* XXX: Not implemented.  */
742  HOWTO (R_ALPHA_IMMED_BR_HI32,
743	 0,			/* rightshift */
744	 0,			/* size (0 = byte, 1 = short, 2 = long) */
745	 0,			/* bitsize */
746	 false,			/* pc_relative */
747	 0,			/* bitpos */
748	 complain_overflow_dont, /* complain_on_overflow */
749	 elf64_alpha_reloc_bad, /* special_function */
750	 "IMMED_BR_HI32",		/* name */
751	 false,			/* partial_inplace */
752	 0,			/* src_mask */
753	 0,			/* dst_mask */
754	 false),		/* pcrel_offset */
755
756  /* The low 16 bits of a displacement calculated in a previous HI32 reloc.  */
757  /* XXX: Not implemented.  */
758  HOWTO (R_ALPHA_IMMED_LO32,
759	 0,			/* rightshift */
760	 0,			/* size (0 = byte, 1 = short, 2 = long) */
761	 0,			/* bitsize */
762	 false,			/* pc_relative */
763	 0,			/* bitpos */
764	 complain_overflow_dont, /* complain_on_overflow */
765	 elf64_alpha_reloc_bad, /* special_function */
766	 "IMMED_LO32",		/* name */
767	 false,			/* partial_inplace */
768	 0,			/* src_mask */
769	 0,			/* dst_mask */
770	 false),		/* pcrel_offset */
771
772  /* Misc ELF relocations. */
773
774  /* A dynamic relocation to copy the target into our .dynbss section.  */
775  /* Not generated, as all Alpha objects use PIC, so it is not needed.  It
776     is present because every other ELF has one, but should not be used
777     because .dynbss is an ugly thing.  */
778  HOWTO (R_ALPHA_COPY,
779	 0,
780	 0,
781	 0,
782	 false,
783	 0,
784	 complain_overflow_dont,
785	 bfd_elf_generic_reloc,
786	 "COPY",
787	 false,
788	 0,
789	 0,
790	 true),
791
792  /* A dynamic relocation for a .got entry.  */
793  HOWTO (R_ALPHA_GLOB_DAT,
794	 0,
795	 0,
796	 0,
797	 false,
798	 0,
799	 complain_overflow_dont,
800	 bfd_elf_generic_reloc,
801	 "GLOB_DAT",
802	 false,
803	 0,
804	 0,
805	 true),
806
807  /* A dynamic relocation for a .plt entry.  */
808  HOWTO (R_ALPHA_JMP_SLOT,
809	 0,
810	 0,
811	 0,
812	 false,
813	 0,
814	 complain_overflow_dont,
815	 bfd_elf_generic_reloc,
816	 "JMP_SLOT",
817	 false,
818	 0,
819	 0,
820	 true),
821
822  /* A dynamic relocation to add the base of the DSO to a 64-bit field.  */
823  HOWTO (R_ALPHA_RELATIVE,
824	 0,
825	 0,
826	 0,
827	 false,
828	 0,
829	 complain_overflow_dont,
830	 bfd_elf_generic_reloc,
831	 "RELATIVE",
832	 false,
833	 0,
834	 0,
835	 true)
836};
837
838/* A relocation function which doesn't do anything.  */
839
840static bfd_reloc_status_type
841elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
842     bfd *abfd;
843     arelent *reloc;
844     asymbol *sym;
845     PTR data;
846     asection *sec;
847     bfd *output_bfd;
848     char **error_message;
849{
850  if (output_bfd)
851    reloc->address += sec->output_offset;
852  return bfd_reloc_ok;
853}
854
855/* A relocation function used for an unsupported reloc.  */
856
857static bfd_reloc_status_type
858elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
859     bfd *abfd;
860     arelent *reloc;
861     asymbol *sym;
862     PTR data;
863     asection *sec;
864     bfd *output_bfd;
865     char **error_message;
866{
867  if (output_bfd)
868    reloc->address += sec->output_offset;
869  return bfd_reloc_notsupported;
870}
871
872/* Do the work of the GPDISP relocation.  */
873
874static bfd_reloc_status_type
875elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
876     bfd *abfd;
877     bfd_vma gpdisp;
878     bfd_byte *p_ldah;
879     bfd_byte *p_lda;
880{
881  bfd_reloc_status_type ret = bfd_reloc_ok;
882  bfd_vma addend;
883  unsigned long i_ldah, i_lda;
884
885  i_ldah = bfd_get_32 (abfd, p_ldah);
886  i_lda = bfd_get_32 (abfd, p_lda);
887
888  /* Complain if the instructions are not correct.  */
889  if (((i_ldah >> 26) & 0x3f) != 0x09
890      || ((i_lda >> 26) & 0x3f) != 0x08)
891    ret = bfd_reloc_dangerous;
892
893  /* Extract the user-supplied offset, mirroring the sign extensions
894     that the instructions perform.  */
895  addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
896  addend = (addend ^ 0x80008000) - 0x80008000;
897
898  gpdisp += addend;
899
900  if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
901      || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
902    ret = bfd_reloc_overflow;
903
904  /* compensate for the sign extension again.  */
905  i_ldah = ((i_ldah & 0xffff0000)
906	    | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
907  i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
908
909  bfd_put_32 (abfd, i_ldah, p_ldah);
910  bfd_put_32 (abfd, i_lda, p_lda);
911
912  return ret;
913}
914
915/* The special function for the GPDISP reloc.  */
916
917static bfd_reloc_status_type
918elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
919			  output_bfd, err_msg)
920     bfd *abfd;
921     arelent *reloc_entry;
922     asymbol *sym;
923     PTR data;
924     asection *input_section;
925     bfd *output_bfd;
926     char **err_msg;
927{
928  bfd_reloc_status_type ret;
929  bfd_vma gp, relocation;
930  bfd_byte *p_ldah, *p_lda;
931
932  /* Don't do anything if we're not doing a final link.  */
933  if (output_bfd)
934    {
935      reloc_entry->address += input_section->output_offset;
936      return bfd_reloc_ok;
937    }
938
939  if (reloc_entry->address > input_section->_cooked_size ||
940      reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
941    return bfd_reloc_outofrange;
942
943  /* The gp used in the portion of the output object to which this
944     input object belongs is cached on the input bfd.  */
945  gp = _bfd_get_gp_value (abfd);
946
947  relocation = (input_section->output_section->vma
948		+ input_section->output_offset
949		+ reloc_entry->address);
950
951  p_ldah = (bfd_byte *) data + reloc_entry->address;
952  p_lda = p_ldah + reloc_entry->addend;
953
954  ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
955
956  /* Complain if the instructions are not correct.  */
957  if (ret == bfd_reloc_dangerous)
958    *err_msg = "GPDISP relocation did not find ldah and lda instructions";
959
960  return ret;
961}
962
963/* A mapping from BFD reloc types to Alpha ELF reloc types.  */
964
965struct elf_reloc_map
966{
967  bfd_reloc_code_real_type bfd_reloc_val;
968  int elf_reloc_val;
969};
970
971static const struct elf_reloc_map elf64_alpha_reloc_map[] =
972{
973  {BFD_RELOC_NONE,		R_ALPHA_NONE},
974  {BFD_RELOC_32,		R_ALPHA_REFLONG},
975  {BFD_RELOC_64,		R_ALPHA_REFQUAD},
976  {BFD_RELOC_CTOR,		R_ALPHA_REFQUAD},
977  {BFD_RELOC_GPREL32,		R_ALPHA_GPREL32},
978  {BFD_RELOC_ALPHA_ELF_LITERAL,	R_ALPHA_LITERAL},
979  {BFD_RELOC_ALPHA_LITUSE,	R_ALPHA_LITUSE},
980  {BFD_RELOC_ALPHA_GPDISP,	R_ALPHA_GPDISP},
981  {BFD_RELOC_23_PCREL_S2,	R_ALPHA_BRADDR},
982  {BFD_RELOC_ALPHA_HINT,	R_ALPHA_HINT},
983  {BFD_RELOC_16_PCREL,		R_ALPHA_SREL16},
984  {BFD_RELOC_32_PCREL,		R_ALPHA_SREL32},
985  {BFD_RELOC_64_PCREL,		R_ALPHA_SREL64},
986};
987
988/* Given a BFD reloc type, return a HOWTO structure.  */
989
990static reloc_howto_type *
991elf64_alpha_bfd_reloc_type_lookup (abfd, code)
992     bfd *abfd;
993     bfd_reloc_code_real_type code;
994{
995  const struct elf_reloc_map *i, *e;
996  i = e = elf64_alpha_reloc_map;
997  e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
998  for (; i != e; ++i)
999    {
1000      if (i->bfd_reloc_val == code)
1001	return &elf64_alpha_howto_table[i->elf_reloc_val];
1002    }
1003  return 0;
1004}
1005
1006/* Given an Alpha ELF reloc type, fill in an arelent structure.  */
1007
1008static void
1009elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
1010     bfd *abfd;
1011     arelent *cache_ptr;
1012     Elf64_Internal_Rela *dst;
1013{
1014  unsigned r_type;
1015
1016  r_type = ELF64_R_TYPE(dst->r_info);
1017  BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1018  cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1019}
1020
1021/* PLT/GOT Stuff */
1022#define PLT_HEADER_SIZE 32
1023#define PLT_HEADER_WORD1	0xc3600000	/* br   $27,.+4     */
1024#define PLT_HEADER_WORD2	0xa77b000c	/* ldq  $27,12($27) */
1025#define PLT_HEADER_WORD3	0x47ff041f	/* nop              */
1026#define PLT_HEADER_WORD4	0x6b7b0000	/* jmp  $27,($27)   */
1027
1028#define PLT_ENTRY_SIZE 12
1029#define PLT_ENTRY_WORD1		0xc3800000	/* br   $28, plt0   */
1030#define PLT_ENTRY_WORD2		0
1031#define PLT_ENTRY_WORD3		0
1032
1033#define MAX_GOT_ENTRIES		(64*1024 / 8)
1034
1035#if defined(__FreeBSD__)
1036#define ELF_DYNAMIC_INTERPRETER "/usr/libexec/ld-elf.so.1"
1037#else
1038#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
1039#endif
1040
1041/* Handle an Alpha specific section when reading an object file.  This
1042   is called when elfcode.h finds a section with an unknown type.
1043   FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1044   how to.  */
1045
1046static boolean
1047elf64_alpha_section_from_shdr (abfd, hdr, name)
1048     bfd *abfd;
1049     Elf64_Internal_Shdr *hdr;
1050     char *name;
1051{
1052  asection *newsect;
1053
1054  /* There ought to be a place to keep ELF backend specific flags, but
1055     at the moment there isn't one.  We just keep track of the
1056     sections by their name, instead.  Fortunately, the ABI gives
1057     suggested names for all the MIPS specific sections, so we will
1058     probably get away with this.  */
1059  switch (hdr->sh_type)
1060    {
1061    case SHT_ALPHA_DEBUG:
1062      if (strcmp (name, ".mdebug") != 0)
1063	return false;
1064      break;
1065#ifdef ERIC_neverdef
1066    case SHT_ALPHA_REGINFO:
1067      if (strcmp (name, ".reginfo") != 0
1068	  || hdr->sh_size != sizeof (Elf64_External_RegInfo))
1069	return false;
1070      break;
1071#endif
1072    default:
1073      return false;
1074    }
1075
1076  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1077    return false;
1078  newsect = hdr->bfd_section;
1079
1080  if (hdr->sh_type == SHT_ALPHA_DEBUG)
1081    {
1082      if (! bfd_set_section_flags (abfd, newsect,
1083				   (bfd_get_section_flags (abfd, newsect)
1084				    | SEC_DEBUGGING)))
1085	return false;
1086    }
1087
1088#ifdef ERIC_neverdef
1089  /* For a .reginfo section, set the gp value in the tdata information
1090     from the contents of this section.  We need the gp value while
1091     processing relocs, so we just get it now.  */
1092  if (hdr->sh_type == SHT_ALPHA_REGINFO)
1093    {
1094      Elf64_External_RegInfo ext;
1095      Elf64_RegInfo s;
1096
1097      if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1098				      (file_ptr) 0, sizeof ext))
1099	return false;
1100      bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
1101      elf_gp (abfd) = s.ri_gp_value;
1102    }
1103#endif
1104
1105  return true;
1106}
1107
1108/* Set the correct type for an Alpha ELF section.  We do this by the
1109   section name, which is a hack, but ought to work.  */
1110
1111static boolean
1112elf64_alpha_fake_sections (abfd, hdr, sec)
1113     bfd *abfd;
1114     Elf64_Internal_Shdr *hdr;
1115     asection *sec;
1116{
1117  register const char *name;
1118
1119  name = bfd_get_section_name (abfd, sec);
1120
1121  if (strcmp (name, ".mdebug") == 0)
1122    {
1123      hdr->sh_type = SHT_ALPHA_DEBUG;
1124      /* In a shared object on Irix 5.3, the .mdebug section has an
1125         entsize of 0.  FIXME: Does this matter?  */
1126      if ((abfd->flags & DYNAMIC) != 0 )
1127	hdr->sh_entsize = 0;
1128      else
1129	hdr->sh_entsize = 1;
1130    }
1131#ifdef ERIC_neverdef
1132  else if (strcmp (name, ".reginfo") == 0)
1133    {
1134      hdr->sh_type = SHT_ALPHA_REGINFO;
1135      /* In a shared object on Irix 5.3, the .reginfo section has an
1136         entsize of 0x18.  FIXME: Does this matter?  */
1137      if ((abfd->flags & DYNAMIC) != 0)
1138	hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
1139      else
1140	hdr->sh_entsize = 1;
1141
1142      /* Force the section size to the correct value, even if the
1143	 linker thinks it is larger.  The link routine below will only
1144	 write out this much data for .reginfo.  */
1145      hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
1146    }
1147  else if (strcmp (name, ".hash") == 0
1148	   || strcmp (name, ".dynamic") == 0
1149	   || strcmp (name, ".dynstr") == 0)
1150    {
1151      hdr->sh_entsize = 0;
1152      hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
1153    }
1154#endif
1155  else if (strcmp (name, ".sdata") == 0
1156	   || strcmp (name, ".sbss") == 0
1157	   || strcmp (name, ".lit4") == 0
1158	   || strcmp (name, ".lit8") == 0)
1159    hdr->sh_flags |= SHF_ALPHA_GPREL;
1160
1161  return true;
1162}
1163
1164/* Return the number of additional phdrs we will need.  */
1165
1166static int
1167elf64_alpha_additional_program_headers (abfd)
1168     bfd *abfd;
1169{
1170#ifdef ERIC_neverdef
1171  asection *s;
1172  int ret;
1173
1174  ret = 0;
1175
1176  s = bfd_get_section_by_name (abfd, ".reginfo");
1177  if (s != NULL && (s->flags & SEC_LOAD) != 0)
1178    {
1179      /* We need a PT_ALPHA_REGINFO segment.  */
1180      ++ret;
1181    }
1182
1183  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
1184      && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
1185    {
1186      /* We need a PT_ALPHA_RTPROC segment.  */
1187      ++ret;
1188    }
1189
1190  return ret;
1191#else
1192  return 0;
1193#endif
1194}
1195
1196/* Create the .got section.  */
1197
1198static boolean
1199elf64_alpha_create_got_section(abfd, info)
1200     bfd *abfd;
1201     struct bfd_link_info *info;
1202{
1203  asection *s;
1204
1205  if (bfd_get_section_by_name (abfd, ".got"))
1206    return true;
1207
1208  s = bfd_make_section (abfd, ".got");
1209  if (s == NULL
1210      || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1211					   | SEC_HAS_CONTENTS
1212					   | SEC_IN_MEMORY
1213					   | SEC_LINKER_CREATED))
1214      || !bfd_set_section_alignment (abfd, s, 3))
1215    return false;
1216
1217  alpha_elf_tdata (abfd)->got = s;
1218
1219  return true;
1220}
1221
1222/* Create all the dynamic sections.  */
1223
1224static boolean
1225elf64_alpha_create_dynamic_sections (abfd, info)
1226     bfd *abfd;
1227     struct bfd_link_info *info;
1228{
1229  asection *s;
1230  struct elf_link_hash_entry *h;
1231
1232  /* We need to create .plt, .rela.plt, .got, and .rela.got sections.  */
1233
1234  s = bfd_make_section (abfd, ".plt");
1235  if (s == NULL
1236      || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1237					    | SEC_HAS_CONTENTS
1238					    | SEC_IN_MEMORY
1239					    | SEC_LINKER_CREATED
1240					    | SEC_CODE))
1241      || ! bfd_set_section_alignment (abfd, s, 3))
1242    return false;
1243
1244  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1245     .plt section.  */
1246  h = NULL;
1247  if (! (_bfd_generic_link_add_one_symbol
1248	 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1249	  (bfd_vma) 0, (const char *) NULL, false,
1250	  get_elf_backend_data (abfd)->collect,
1251	  (struct bfd_link_hash_entry **) &h)))
1252    return false;
1253  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1254  h->type = STT_OBJECT;
1255
1256  if (info->shared
1257      && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1258    return false;
1259
1260  s = bfd_make_section (abfd, ".rela.plt");
1261  if (s == NULL
1262      || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1263					   | SEC_HAS_CONTENTS
1264					   | SEC_IN_MEMORY
1265					   | SEC_LINKER_CREATED
1266					   | SEC_READONLY))
1267      || ! bfd_set_section_alignment (abfd, s, 3))
1268    return false;
1269
1270  /* We may or may not have created a .got section for this object, but
1271     we definitely havn't done the rest of the work.  */
1272
1273  if (!elf64_alpha_create_got_section (abfd, info))
1274    return false;
1275
1276  s = bfd_make_section(abfd, ".rela.got");
1277  if (s == NULL
1278      || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1279					   | SEC_HAS_CONTENTS
1280					   | SEC_IN_MEMORY
1281					   | SEC_LINKER_CREATED
1282					   | SEC_READONLY))
1283      || !bfd_set_section_alignment (abfd, s, 3))
1284    return false;
1285
1286  /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1287     dynobj's .got section.  We don't do this in the linker script
1288     because we don't want to define the symbol if we are not creating
1289     a global offset table.  */
1290  h = NULL;
1291  if (!(_bfd_generic_link_add_one_symbol
1292	(info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
1293	 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
1294	 false, get_elf_backend_data (abfd)->collect,
1295	 (struct bfd_link_hash_entry **) &h)))
1296    return false;
1297  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1298  h->type = STT_OBJECT;
1299
1300  if (info->shared
1301      && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1302    return false;
1303
1304  elf_hash_table (info)->hgot = h;
1305
1306  return true;
1307}
1308
1309/* Read ECOFF debugging information from a .mdebug section into a
1310   ecoff_debug_info structure.  */
1311
1312static boolean
1313elf64_alpha_read_ecoff_info (abfd, section, debug)
1314     bfd *abfd;
1315     asection *section;
1316     struct ecoff_debug_info *debug;
1317{
1318  HDRR *symhdr;
1319  const struct ecoff_debug_swap *swap;
1320  char *ext_hdr = NULL;
1321
1322  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1323
1324  ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
1325  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1326    goto error_return;
1327
1328  if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1329				swap->external_hdr_size)
1330      == false)
1331    goto error_return;
1332
1333  symhdr = &debug->symbolic_header;
1334  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1335
1336  /* The symbolic header contains absolute file offsets and sizes to
1337     read.  */
1338#define READ(ptr, offset, count, size, type)				\
1339  if (symhdr->count == 0)						\
1340    debug->ptr = NULL;							\
1341  else									\
1342    {									\
1343      debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count));	\
1344      if (debug->ptr == NULL)						\
1345	goto error_return;						\
1346      if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0	\
1347	  || (bfd_read (debug->ptr, size, symhdr->count,		\
1348			abfd) != size * symhdr->count))			\
1349	goto error_return;						\
1350    }
1351
1352  READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1353  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1354  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1355  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1356  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1357  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1358	union aux_ext *);
1359  READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1360  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1361  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1362  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1363  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1364#undef READ
1365
1366  debug->fdr = NULL;
1367  debug->adjust = NULL;
1368
1369  return true;
1370
1371 error_return:
1372  if (ext_hdr != NULL)
1373    free (ext_hdr);
1374  if (debug->line != NULL)
1375    free (debug->line);
1376  if (debug->external_dnr != NULL)
1377    free (debug->external_dnr);
1378  if (debug->external_pdr != NULL)
1379    free (debug->external_pdr);
1380  if (debug->external_sym != NULL)
1381    free (debug->external_sym);
1382  if (debug->external_opt != NULL)
1383    free (debug->external_opt);
1384  if (debug->external_aux != NULL)
1385    free (debug->external_aux);
1386  if (debug->ss != NULL)
1387    free (debug->ss);
1388  if (debug->ssext != NULL)
1389    free (debug->ssext);
1390  if (debug->external_fdr != NULL)
1391    free (debug->external_fdr);
1392  if (debug->external_rfd != NULL)
1393    free (debug->external_rfd);
1394  if (debug->external_ext != NULL)
1395    free (debug->external_ext);
1396  return false;
1397}
1398
1399/* Alpha ELF local labels start with '$'.  */
1400
1401static boolean
1402elf64_alpha_is_local_label_name (abfd, name)
1403     bfd *abfd;
1404     const char *name;
1405{
1406  return name[0] == '$';
1407}
1408
1409/* Alpha ELF follows MIPS ELF in using a special find_nearest_line
1410   routine in order to handle the ECOFF debugging information.  We
1411   still call this mips_elf_find_line because of the slot
1412   find_line_info in elf_obj_tdata is declared that way.  */
1413
1414struct mips_elf_find_line
1415{
1416  struct ecoff_debug_info d;
1417  struct ecoff_find_line i;
1418};
1419
1420static boolean
1421elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
1422			       functionname_ptr, line_ptr)
1423     bfd *abfd;
1424     asection *section;
1425     asymbol **symbols;
1426     bfd_vma offset;
1427     const char **filename_ptr;
1428     const char **functionname_ptr;
1429     unsigned int *line_ptr;
1430{
1431  asection *msec;
1432
1433  msec = bfd_get_section_by_name (abfd, ".mdebug");
1434  if (msec != NULL)
1435    {
1436      flagword origflags;
1437      struct mips_elf_find_line *fi;
1438      const struct ecoff_debug_swap * const swap =
1439	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1440
1441      /* If we are called during a link, alpha_elf_final_link may have
1442	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
1443	 if appropriate (which it normally will be).  */
1444      origflags = msec->flags;
1445      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
1446	msec->flags |= SEC_HAS_CONTENTS;
1447
1448      fi = elf_tdata (abfd)->find_line_info;
1449      if (fi == NULL)
1450	{
1451	  bfd_size_type external_fdr_size;
1452	  char *fraw_src;
1453	  char *fraw_end;
1454	  struct fdr *fdr_ptr;
1455
1456	  fi = ((struct mips_elf_find_line *)
1457		bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
1458	  if (fi == NULL)
1459	    {
1460	      msec->flags = origflags;
1461	      return false;
1462	    }
1463
1464	  if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
1465	    {
1466	      msec->flags = origflags;
1467	      return false;
1468	    }
1469
1470	  /* Swap in the FDR information.  */
1471	  fi->d.fdr = ((struct fdr *)
1472		       bfd_alloc (abfd,
1473				  (fi->d.symbolic_header.ifdMax *
1474				   sizeof (struct fdr))));
1475	  if (fi->d.fdr == NULL)
1476	    {
1477	      msec->flags = origflags;
1478	      return false;
1479	    }
1480	  external_fdr_size = swap->external_fdr_size;
1481	  fdr_ptr = fi->d.fdr;
1482	  fraw_src = (char *) fi->d.external_fdr;
1483	  fraw_end = (fraw_src
1484		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
1485	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
1486	    (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
1487
1488	  elf_tdata (abfd)->find_line_info = fi;
1489
1490	  /* Note that we don't bother to ever free this information.
1491             find_nearest_line is either called all the time, as in
1492             objdump -l, so the information should be saved, or it is
1493             rarely called, as in ld error messages, so the memory
1494             wasted is unimportant.  Still, it would probably be a
1495             good idea for free_cached_info to throw it away.  */
1496	}
1497
1498      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
1499				  &fi->i, filename_ptr, functionname_ptr,
1500				  line_ptr))
1501	{
1502	  msec->flags = origflags;
1503	  return true;
1504	}
1505
1506      msec->flags = origflags;
1507    }
1508
1509  /* Fall back on the generic ELF find_nearest_line routine.  */
1510
1511  return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
1512				     filename_ptr, functionname_ptr,
1513				     line_ptr);
1514}
1515
1516/* Structure used to pass information to alpha_elf_output_extsym.  */
1517
1518struct extsym_info
1519{
1520  bfd *abfd;
1521  struct bfd_link_info *info;
1522  struct ecoff_debug_info *debug;
1523  const struct ecoff_debug_swap *swap;
1524  boolean failed;
1525};
1526
1527static boolean
1528elf64_alpha_output_extsym (h, data)
1529     struct alpha_elf_link_hash_entry *h;
1530     PTR data;
1531{
1532  struct extsym_info *einfo = (struct extsym_info *) data;
1533  boolean strip;
1534  asection *sec, *output_section;
1535
1536  if (h->root.indx == -2)
1537    strip = false;
1538  else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1539           || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1540          && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1541          && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
1542    strip = true;
1543  else if (einfo->info->strip == strip_all
1544          || (einfo->info->strip == strip_some
1545              && bfd_hash_lookup (einfo->info->keep_hash,
1546                                  h->root.root.root.string,
1547                                  false, false) == NULL))
1548    strip = true;
1549  else
1550    strip = false;
1551
1552  if (strip)
1553    return true;
1554
1555  if (h->esym.ifd == -2)
1556    {
1557      h->esym.jmptbl = 0;
1558      h->esym.cobol_main = 0;
1559      h->esym.weakext = 0;
1560      h->esym.reserved = 0;
1561      h->esym.ifd = ifdNil;
1562      h->esym.asym.value = 0;
1563      h->esym.asym.st = stGlobal;
1564
1565      if (h->root.root.type != bfd_link_hash_defined
1566         && h->root.root.type != bfd_link_hash_defweak)
1567       h->esym.asym.sc = scAbs;
1568      else
1569       {
1570         const char *name;
1571
1572         sec = h->root.root.u.def.section;
1573         output_section = sec->output_section;
1574
1575         /* When making a shared library and symbol h is the one from
1576            the another shared library, OUTPUT_SECTION may be null.  */
1577         if (output_section == NULL)
1578           h->esym.asym.sc = scUndefined;
1579         else
1580           {
1581             name = bfd_section_name (output_section->owner, output_section);
1582
1583             if (strcmp (name, ".text") == 0)
1584               h->esym.asym.sc = scText;
1585             else if (strcmp (name, ".data") == 0)
1586               h->esym.asym.sc = scData;
1587             else if (strcmp (name, ".sdata") == 0)
1588               h->esym.asym.sc = scSData;
1589             else if (strcmp (name, ".rodata") == 0
1590                      || strcmp (name, ".rdata") == 0)
1591               h->esym.asym.sc = scRData;
1592             else if (strcmp (name, ".bss") == 0)
1593               h->esym.asym.sc = scBss;
1594             else if (strcmp (name, ".sbss") == 0)
1595               h->esym.asym.sc = scSBss;
1596             else if (strcmp (name, ".init") == 0)
1597               h->esym.asym.sc = scInit;
1598             else if (strcmp (name, ".fini") == 0)
1599               h->esym.asym.sc = scFini;
1600             else
1601               h->esym.asym.sc = scAbs;
1602           }
1603       }
1604
1605      h->esym.asym.reserved = 0;
1606      h->esym.asym.index = indexNil;
1607    }
1608
1609  if (h->root.root.type == bfd_link_hash_common)
1610    h->esym.asym.value = h->root.root.u.c.size;
1611  else if (h->root.root.type == bfd_link_hash_defined
1612	   || h->root.root.type == bfd_link_hash_defweak)
1613    {
1614      if (h->esym.asym.sc == scCommon)
1615       h->esym.asym.sc = scBss;
1616      else if (h->esym.asym.sc == scSCommon)
1617       h->esym.asym.sc = scSBss;
1618
1619      sec = h->root.root.u.def.section;
1620      output_section = sec->output_section;
1621      if (output_section != NULL)
1622       h->esym.asym.value = (h->root.root.u.def.value
1623                             + sec->output_offset
1624                             + output_section->vma);
1625      else
1626       h->esym.asym.value = 0;
1627    }
1628  else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1629    {
1630      /* Set type and value for a symbol with a function stub.  */
1631      h->esym.asym.st = stProc;
1632      sec = bfd_get_section_by_name (einfo->abfd, ".plt");
1633      if (sec == NULL)
1634	h->esym.asym.value = 0;
1635      else
1636	{
1637	  output_section = sec->output_section;
1638	  if (output_section != NULL)
1639	    h->esym.asym.value = (h->root.plt_offset
1640				  + sec->output_offset
1641				  + output_section->vma);
1642	  else
1643	    h->esym.asym.value = 0;
1644	}
1645#if 0 /* FIXME?  */
1646      h->esym.ifd = 0;
1647#endif
1648    }
1649
1650  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1651                                     h->root.root.root.string,
1652                                     &h->esym))
1653    {
1654      einfo->failed = true;
1655      return false;
1656    }
1657
1658  return true;
1659}
1660
1661/* FIXME:  Create a runtime procedure table from the .mdebug section.
1662
1663static boolean
1664mips_elf_create_procedure_table (handle, abfd, info, s, debug)
1665     PTR handle;
1666     bfd *abfd;
1667     struct bfd_link_info *info;
1668     asection *s;
1669     struct ecoff_debug_info *debug;
1670*/
1671
1672/* Handle dynamic relocations when doing an Alpha ELF link.  */
1673
1674static boolean
1675elf64_alpha_check_relocs (abfd, info, sec, relocs)
1676     bfd *abfd;
1677     struct bfd_link_info *info;
1678     asection *sec;
1679     const Elf_Internal_Rela *relocs;
1680{
1681  bfd *dynobj;
1682  asection *sreloc;
1683  const char *rel_sec_name;
1684  Elf_Internal_Shdr *symtab_hdr;
1685  struct alpha_elf_link_hash_entry **sym_hashes;
1686  struct alpha_elf_got_entry **local_got_entries;
1687  const Elf_Internal_Rela *rel, *relend;
1688  int got_created;
1689
1690  if (info->relocateable)
1691    return true;
1692
1693  dynobj = elf_hash_table(info)->dynobj;
1694  if (dynobj == NULL)
1695    elf_hash_table(info)->dynobj = dynobj = abfd;
1696
1697  sreloc = NULL;
1698  rel_sec_name = NULL;
1699  symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
1700  sym_hashes = alpha_elf_sym_hashes(abfd);
1701  local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1702  got_created = 0;
1703
1704  relend = relocs + sec->reloc_count;
1705  for (rel = relocs; rel < relend; ++rel)
1706    {
1707      unsigned long r_symndx, r_type;
1708      struct alpha_elf_link_hash_entry *h;
1709
1710      r_symndx = ELF64_R_SYM (rel->r_info);
1711      if (r_symndx < symtab_hdr->sh_info)
1712	h = NULL;
1713      else
1714	{
1715	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1716
1717	  while (h->root.root.type == bfd_link_hash_indirect
1718		 || h->root.root.type == bfd_link_hash_warning)
1719	    h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
1720
1721	  h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1722	}
1723      r_type = ELF64_R_TYPE (rel->r_info);
1724
1725      switch (r_type)
1726	{
1727	case R_ALPHA_LITERAL:
1728	  {
1729	    struct alpha_elf_got_entry *gotent;
1730	    int flags = 0;
1731
1732	    if (h)
1733	      {
1734		/* Search for and possibly create a got entry.  */
1735		for (gotent = h->got_entries; gotent ; gotent = gotent->next)
1736		  if (gotent->gotobj == abfd &&
1737		      gotent->addend == rel->r_addend)
1738		    break;
1739
1740		if (!gotent)
1741		  {
1742		    gotent = ((struct alpha_elf_got_entry *)
1743			      bfd_alloc (abfd,
1744					 sizeof (struct alpha_elf_got_entry)));
1745		    if (!gotent)
1746		      return false;
1747
1748		    gotent->gotobj = abfd;
1749		    gotent->addend = rel->r_addend;
1750		    gotent->got_offset = -1;
1751		    gotent->flags = 0;
1752
1753		    gotent->next = h->got_entries;
1754		    h->got_entries = gotent;
1755
1756		    alpha_elf_tdata (abfd)->total_got_entries++;
1757		  }
1758	      }
1759	    else
1760	      {
1761		/* This is a local .got entry -- record for merge.  */
1762		if (!local_got_entries)
1763		  {
1764		    size_t size;
1765		    size = (symtab_hdr->sh_info
1766			    * sizeof (struct alpha_elf_got_entry *));
1767
1768		    local_got_entries = ((struct alpha_elf_got_entry **)
1769					 bfd_alloc (abfd, size));
1770		    if (!local_got_entries)
1771		      return false;
1772
1773		    memset (local_got_entries, 0, size);
1774		    alpha_elf_tdata (abfd)->local_got_entries =
1775		      local_got_entries;
1776		  }
1777
1778		for (gotent = local_got_entries[ELF64_R_SYM(rel->r_info)];
1779		     gotent != NULL && gotent->addend != rel->r_addend;
1780		     gotent = gotent->next)
1781		  continue;
1782		if (!gotent)
1783		  {
1784		    gotent = ((struct alpha_elf_got_entry *)
1785			      bfd_alloc (abfd,
1786					 sizeof (struct alpha_elf_got_entry)));
1787		    if (!gotent)
1788		      return false;
1789
1790		    gotent->gotobj = abfd;
1791		    gotent->addend = rel->r_addend;
1792		    gotent->got_offset = -1;
1793		    gotent->flags = 0;
1794
1795		    gotent->next = local_got_entries[ELF64_R_SYM(rel->r_info)];
1796		    local_got_entries[ELF64_R_SYM(rel->r_info)] = gotent;
1797
1798		    alpha_elf_tdata(abfd)->total_got_entries++;
1799		    alpha_elf_tdata(abfd)->n_local_got_entries++;
1800		  }
1801	      }
1802
1803	    /* Remember how this literal is used from its LITUSEs.
1804	       This will be important when it comes to decide if we can
1805	       create a .plt entry for a function symbol.  */
1806	    if (rel+1 < relend
1807		&& ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
1808	      {
1809		do
1810		  {
1811		    ++rel;
1812		    if (rel->r_addend >= 1 && rel->r_addend <= 3)
1813		      flags |= 1 << rel->r_addend;
1814		  }
1815		while (rel+1 < relend &&
1816		       ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE);
1817	      }
1818	    else
1819	      {
1820		/* No LITUSEs -- presumably the address is not being
1821		   loaded for nothing.  */
1822		flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
1823	      }
1824
1825	    gotent->flags |= flags;
1826	    if (h)
1827	      {
1828		/* Make a guess as to whether a .plt entry will be needed.  */
1829		if ((h->flags |= flags) == ALPHA_ELF_LINK_HASH_LU_FUNC)
1830		  h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1831		else
1832		  h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1833	      }
1834	  }
1835	  /* FALLTHRU */
1836
1837	case R_ALPHA_GPDISP:
1838	case R_ALPHA_GPREL32:
1839	  /* We don't actually use the .got here, but the sections must
1840	     be created before the linker maps input sections to output
1841	     sections.  */
1842	  if (!got_created)
1843	    {
1844	      if (!elf64_alpha_create_got_section (abfd, info))
1845		return false;
1846
1847	      /* Make sure the object's gotobj is set to itself so
1848		 that we default to every object with its own .got.
1849		 We'll merge .gots later once we've collected each
1850		 object's info.  */
1851	      alpha_elf_tdata(abfd)->gotobj = abfd;
1852
1853	      got_created = 1;
1854	    }
1855	  break;
1856
1857	case R_ALPHA_SREL16:
1858	case R_ALPHA_SREL32:
1859	case R_ALPHA_SREL64:
1860	  if (h == NULL)
1861	    break;
1862	  /* FALLTHRU */
1863
1864	case R_ALPHA_REFLONG:
1865	case R_ALPHA_REFQUAD:
1866	  if (rel_sec_name == NULL)
1867	    {
1868	      rel_sec_name = (bfd_elf_string_from_elf_section
1869			      (abfd, elf_elfheader(abfd)->e_shstrndx,
1870			       elf_section_data(sec)->rel_hdr.sh_name));
1871	      if (rel_sec_name == NULL)
1872		return false;
1873
1874	      BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
1875			  && strcmp (bfd_get_section_name (abfd, sec),
1876				     rel_sec_name+5) == 0);
1877	    }
1878
1879	  /* We need to create the section here now whether we eventually
1880	     use it or not so that it gets mapped to an output section by
1881	     the linker.  If not used, we'll kill it in
1882	     size_dynamic_sections.  */
1883	  if (sreloc == NULL)
1884	    {
1885	      sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
1886	      if (sreloc == NULL)
1887		{
1888		  sreloc = bfd_make_section (dynobj, rel_sec_name);
1889		  if (sreloc == NULL
1890		      || !bfd_set_section_flags (dynobj, sreloc,
1891						 (SEC_ALLOC|SEC_LOAD
1892						  | SEC_HAS_CONTENTS
1893						  | SEC_IN_MEMORY
1894						  | SEC_LINKER_CREATED
1895						  | SEC_READONLY))
1896		      || !bfd_set_section_alignment (dynobj, sreloc, 3))
1897		    return false;
1898		}
1899	    }
1900
1901	  if (h)
1902	    {
1903	      /* Since we havn't seen all of the input symbols yet, we
1904		 don't know whether we'll actually need a dynamic relocation
1905		 entry for this reloc.  So make a record of it.  Once we
1906		 find out if this thing needs dynamic relocation we'll
1907		 expand the relocation sections by the appropriate amount. */
1908
1909	      struct alpha_elf_reloc_entry *rent;
1910
1911	      for (rent = h->reloc_entries; rent; rent = rent->next)
1912		if (rent->rtype == r_type && rent->srel == sreloc)
1913		  break;
1914
1915	      if (!rent)
1916		{
1917		  rent = ((struct alpha_elf_reloc_entry *)
1918			  bfd_alloc (abfd,
1919				     sizeof (struct alpha_elf_reloc_entry)));
1920		  if (!rent)
1921		    return false;
1922
1923		  rent->srel = sreloc;
1924		  rent->rtype = r_type;
1925		  rent->count = 1;
1926
1927		  rent->next = h->reloc_entries;
1928		  h->reloc_entries = rent;
1929		}
1930	      else
1931		rent->count++;
1932	    }
1933	  else if (info->shared)
1934	    {
1935	      /* If this is a shared library, we need a RELATIVE reloc.  */
1936	      sreloc->_raw_size += sizeof (Elf64_External_Rela);
1937	    }
1938	  break;
1939	}
1940    }
1941
1942  return true;
1943}
1944
1945/* Adjust a symbol defined by a dynamic object and referenced by a
1946   regular object.  The current definition is in some section of the
1947   dynamic object, but we're not including those sections.  We have to
1948   change the definition to something the rest of the link can
1949   understand.  */
1950
1951static boolean
1952elf64_alpha_adjust_dynamic_symbol (info, h)
1953     struct bfd_link_info *info;
1954     struct elf_link_hash_entry *h;
1955{
1956  bfd *dynobj;
1957  asection *s;
1958  struct alpha_elf_link_hash_entry *ah;
1959
1960  dynobj = elf_hash_table(info)->dynobj;
1961  ah = (struct alpha_elf_link_hash_entry *)h;
1962
1963  /* Now that we've seen all of the input symbols, finalize our decision
1964     about whether this symbol should get a .plt entry.  */
1965
1966  if (h->root.type != bfd_link_hash_undefweak
1967      && alpha_elf_dynamic_symbol_p (h, info)
1968      && ((h->type == STT_FUNC
1969	   && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
1970	  || (h->type == STT_NOTYPE
1971	      && ah->flags == ALPHA_ELF_LINK_HASH_LU_FUNC))
1972      /* Don't prevent otherwise valid programs from linking by attempting
1973	 to create a new .got entry somewhere.  A Correct Solution would be
1974	 to add a new .got section to a new object file and let it be merged
1975	 somewhere later.  But for now don't bother.  */
1976      && ah->got_entries)
1977    {
1978      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1979
1980      s = bfd_get_section_by_name(dynobj, ".plt");
1981      if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
1982	return false;
1983
1984      /* The first bit of the .plt is reserved.  */
1985      if (s->_raw_size == 0)
1986	s->_raw_size = PLT_HEADER_SIZE;
1987
1988      h->plt_offset = s->_raw_size;
1989      s->_raw_size += PLT_ENTRY_SIZE;
1990
1991      /* If this symbol is not defined in a regular file, and we are not
1992	 generating a shared library, then set the symbol to the location
1993	 in the .plt.  This is required to make function pointers compare
1994	 equal between the normal executable and the shared library.  */
1995      if (! info->shared
1996	  && h->root.type != bfd_link_hash_defweak)
1997	{
1998	  h->root.u.def.section = s;
1999	  h->root.u.def.value = h->plt_offset;
2000	}
2001
2002      /* We also need a JMP_SLOT entry in the .rela.plt section.  */
2003      s = bfd_get_section_by_name (dynobj, ".rela.plt");
2004      BFD_ASSERT (s != NULL);
2005      s->_raw_size += sizeof (Elf64_External_Rela);
2006
2007      return true;
2008    }
2009  else
2010    h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2011
2012  /* If this is a weak symbol, and there is a real definition, the
2013     processor independent code will have arranged for us to see the
2014     real definition first, and we can just use the same value.  */
2015  if (h->weakdef != NULL)
2016    {
2017      BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2018		  || h->weakdef->root.type == bfd_link_hash_defweak);
2019      h->root.u.def.section = h->weakdef->root.u.def.section;
2020      h->root.u.def.value = h->weakdef->root.u.def.value;
2021      return true;
2022    }
2023
2024  /* This is a reference to a symbol defined by a dynamic object which
2025     is not a function.  The Alpha, since it uses .got entries for all
2026     symbols even in regular objects, does not need the hackery of a
2027     .dynbss section and COPY dynamic relocations.  */
2028
2029  return true;
2030}
2031
2032/* Symbol versioning can create new symbols, and make our old symbols
2033   indirect to the new ones.  Consolidate the got and reloc information
2034   in these situations.  */
2035
2036static boolean
2037elf64_alpha_merge_ind_symbols (hi, dummy)
2038     struct alpha_elf_link_hash_entry *hi;
2039     PTR dummy;
2040{
2041  struct alpha_elf_link_hash_entry *hs;
2042
2043  if (hi->root.root.type != bfd_link_hash_indirect)
2044    return true;
2045  hs = hi;
2046  do {
2047    hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
2048  } while (hs->root.root.type == bfd_link_hash_indirect);
2049
2050  /* Merge the flags.  Whee.  */
2051
2052  hs->flags |= hi->flags;
2053
2054  /* Merge the .got entries.  Cannibalize the old symbol's list in
2055     doing so, since we don't need it anymore.  */
2056
2057  if (hs->got_entries == NULL)
2058    hs->got_entries = hi->got_entries;
2059  else
2060    {
2061      struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
2062
2063      gsh = hs->got_entries;
2064      for (gi = hi->got_entries; gi ; gi = gin)
2065	{
2066	  gin = gi->next;
2067	  for (gs = gsh; gs ; gs = gs->next)
2068	    if (gi->gotobj == gs->gotobj && gi->addend == gs->addend )
2069	      goto got_found;
2070	  gi->next = hs->got_entries;
2071	  hs->got_entries = gi;
2072	got_found:;
2073	}
2074    }
2075  hi->got_entries = NULL;
2076
2077  /* And similar for the reloc entries.  */
2078
2079  if (hs->reloc_entries == NULL)
2080    hs->reloc_entries = hi->reloc_entries;
2081  else
2082    {
2083      struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
2084
2085      rsh = hs->reloc_entries;
2086      for (ri = hi->reloc_entries; ri ; ri = rin)
2087	{
2088	  rin = ri->next;
2089	  for (rs = rsh; rs ; rs = rs->next)
2090	    if (ri->rtype == rs->rtype)
2091	      {
2092		rs->count += ri->count;
2093		goto found_reloc;
2094	      }
2095	  ri->next = hs->reloc_entries;
2096	  hs->reloc_entries = ri;
2097	found_reloc:;
2098	}
2099    }
2100  hi->reloc_entries = NULL;
2101
2102  return true;
2103}
2104
2105/* Is it possible to merge two object file's .got tables?  */
2106
2107static boolean
2108elf64_alpha_can_merge_gots (a, b)
2109     bfd *a, *b;
2110{
2111  int total = alpha_elf_tdata (a)->total_got_entries;
2112
2113  /* Trivial quick fallout test.  */
2114  if (total + alpha_elf_tdata (b)->total_got_entries <= MAX_GOT_ENTRIES)
2115    return true;
2116
2117  /* By their nature, local .got entries cannot be merged.  */
2118  if ((total += alpha_elf_tdata (b)->n_local_got_entries) > MAX_GOT_ENTRIES)
2119    return false;
2120
2121  /* Failing the common trivial comparison, we must effectively
2122     perform the merge.  Not actually performing the merge means that
2123     we don't have to store undo information in case we fail.  */
2124  {
2125    struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
2126    Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
2127    int i, n;
2128
2129    n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2130    for (i = 0; i < n; ++i)
2131      {
2132	struct alpha_elf_got_entry *ae, *be;
2133	struct alpha_elf_link_hash_entry *h;
2134
2135	h = hashes[i];
2136	while (h->root.root.type == bfd_link_hash_indirect
2137	       || h->root.root.type == bfd_link_hash_warning)
2138	  h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2139
2140	for (be = h->got_entries; be ; be = be->next)
2141	  {
2142	    if (be->gotobj != b)
2143	      continue;
2144
2145	    for (ae = h->got_entries; ae ; ae = ae->next)
2146	      if (ae->gotobj == a && ae->addend == be->addend)
2147		goto global_found;
2148
2149	    if (++total > MAX_GOT_ENTRIES)
2150	      return false;
2151	  global_found:;
2152	  }
2153      }
2154  }
2155
2156  return true;
2157}
2158
2159/* Actually merge two .got tables.  */
2160
2161static void
2162elf64_alpha_merge_gots (a, b)
2163     bfd *a, *b;
2164{
2165  int total = alpha_elf_tdata(a)->total_got_entries;
2166
2167  /* Remember local expansion.  */
2168  {
2169    int e = alpha_elf_tdata(b)->n_local_got_entries;
2170    total += e;
2171    alpha_elf_tdata(a)->n_local_got_entries += e;
2172  }
2173
2174  /* Let the local .got entries know they are part of a new subsegment.  */
2175  {
2176    struct alpha_elf_got_entry **local_got_entries;
2177    local_got_entries = alpha_elf_tdata(b)->local_got_entries;
2178    if (local_got_entries)
2179      {
2180	int i, n;
2181
2182	n = elf_tdata(b)->symtab_hdr.sh_info;
2183	for (i = 0; i < n; ++i)
2184	  {
2185	    struct alpha_elf_got_entry *gotent;
2186	    for (gotent = local_got_entries[i]; gotent; gotent = gotent->next)
2187	      gotent->gotobj = a;
2188	  }
2189      }
2190  }
2191
2192  /* Merge the global .got entries.  */
2193  {
2194    struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
2195    Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
2196    int i, n;
2197
2198    n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2199    for (i = 0; i < n; ++i)
2200      {
2201	struct alpha_elf_got_entry *ae, *be, **pbe, **start;
2202	struct alpha_elf_link_hash_entry *h;
2203
2204	h = hashes[i];
2205	while (h->root.root.type == bfd_link_hash_indirect
2206	       || h->root.root.type == bfd_link_hash_warning)
2207	  h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2208
2209	start = &h->got_entries;
2210	for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
2211	  {
2212	    if (be->gotobj != b)
2213	      continue;
2214
2215	    for (ae = *start; ae ; ae = ae->next)
2216	      if (ae->gotobj == a && ae->addend == be->addend)
2217		{
2218		  ae->flags |= be->flags;
2219		  *pbe = be->next;
2220		  goto global_found;
2221		}
2222	    be->gotobj = a;
2223	    total += 1;
2224
2225	  global_found:;
2226	  }
2227      }
2228  }
2229
2230  alpha_elf_tdata(a)->total_got_entries = total;
2231  alpha_elf_tdata(b)->gotobj = a;
2232}
2233
2234/* Calculate the offsets for the got entries.  */
2235
2236static boolean
2237elf64_alpha_calc_got_offsets_for_symbol (h, arg)
2238     struct alpha_elf_link_hash_entry *h;
2239     PTR arg;
2240{
2241  struct alpha_elf_got_entry *gotent;
2242
2243  for (gotent = h->got_entries; gotent; gotent = gotent->next)
2244    {
2245      bfd_size_type *plge = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
2246      gotent->got_offset = *plge;
2247      *plge += 8;
2248    }
2249
2250  return true;
2251}
2252
2253static void
2254elf64_alpha_calc_got_offsets (info)
2255     struct bfd_link_info *info;
2256{
2257  bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
2258
2259  /* First, zero out the .got sizes, as we may be recalculating the
2260     .got after optimizing it.  */
2261  for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2262    alpha_elf_tdata(i)->got->_raw_size = 0;
2263
2264  /* Next, fill in the offsets for all the global entries.  */
2265  alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2266				elf64_alpha_calc_got_offsets_for_symbol,
2267				NULL);
2268
2269  /* Finally, fill in the offsets for the local entries.  */
2270  for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2271    {
2272      bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
2273      bfd *j;
2274
2275      for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2276	{
2277	  struct alpha_elf_got_entry **local_got_entries, *gotent;
2278	  int k, n;
2279
2280	  local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2281	  if (!local_got_entries)
2282	    continue;
2283
2284	  for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
2285	    for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
2286	      {
2287		gotent->got_offset = got_offset;
2288		got_offset += 8;
2289	      }
2290	}
2291
2292      alpha_elf_tdata(i)->got->_raw_size = got_offset;
2293    }
2294}
2295
2296/* Remove a section from the output BFD.  */
2297
2298static void
2299elf64_alpha_strip_section_from_output (s)
2300     asection *s;
2301{
2302  asection **spp;
2303
2304  for (spp = &s->output_section->owner->sections;
2305       *spp != s->output_section;
2306       spp = &(*spp)->next)
2307    continue;
2308  *spp = s->output_section->next;
2309  --s->output_section->owner->section_count;
2310}
2311
2312/* Constructs the gots.  */
2313
2314static boolean
2315elf64_alpha_always_size_sections (output_bfd, info)
2316     bfd *output_bfd;
2317     struct bfd_link_info *info;
2318{
2319  bfd *i, *got_list, *cur_got_obj, **cur_got_tail;
2320  int ngots;
2321
2322  if (info->relocateable)
2323    return true;
2324
2325  /* First, take care of the indirect symbols created by versioning.  */
2326  alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2327				elf64_alpha_merge_ind_symbols,
2328				NULL);
2329
2330  ngots = 0;
2331  got_list = NULL;
2332  cur_got_obj = NULL;
2333  cur_got_tail = NULL;
2334  for (i = info->input_bfds; i ; i = i->link_next)
2335    {
2336      bfd *this_got = alpha_elf_tdata (i)->gotobj;
2337
2338      /* Don't play if there is no .got for this input file.  */
2339      if (this_got == NULL)
2340	continue;
2341
2342      if (alpha_elf_tdata (this_got)->total_got_entries > MAX_GOT_ENTRIES)
2343	{
2344	  /* Yikes! A single object file has too many entries.  */
2345	  (*_bfd_error_handler)
2346	    ("%s: .got subsegment exceeds 64K (size %d)",
2347	     bfd_get_filename(i),
2348	     alpha_elf_tdata(this_got)->total_got_entries * 8);
2349	  return false;
2350	}
2351
2352      if (cur_got_obj)
2353	{
2354	  if (elf64_alpha_can_merge_gots (cur_got_obj, i))
2355	    {
2356	      elf64_alpha_merge_gots (cur_got_obj, i);
2357	      *cur_got_tail = i;
2358	    }
2359	  else
2360	    {
2361	      if (++ngots == 2)
2362		{
2363		  (*info->callbacks->warning)
2364		    (info, "using multiple gp values", (char *) NULL,
2365		     output_bfd, (asection *) NULL, (bfd_vma) 0);
2366		}
2367	      *cur_got_tail = NULL;
2368	      alpha_elf_tdata(cur_got_obj)->got_link_next = got_list;
2369	      got_list = cur_got_obj;
2370	      cur_got_obj = i;
2371	    }
2372	}
2373      else
2374	{
2375	  ++ngots;
2376	  cur_got_obj = i;
2377	}
2378      cur_got_tail = &alpha_elf_tdata(i)->in_got_link_next;
2379    }
2380
2381  if (cur_got_obj)
2382    alpha_elf_tdata (cur_got_obj)->got_link_next = got_list;
2383  alpha_elf_hash_table (info)->got_list = got_list = cur_got_obj;
2384
2385  /* Once the gots have been merged, fill in the got offsets for everything
2386     therein.  */
2387  elf64_alpha_calc_got_offsets (info);
2388
2389  /* Allocate space for all of the .got subsections.  */
2390  for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2391    {
2392      asection *s = alpha_elf_tdata(i)->got;
2393      if (s->_raw_size > 0)
2394	{
2395	  s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
2396	  if (s->contents == NULL)
2397	    return false;
2398	}
2399    }
2400
2401  return true;
2402}
2403
2404/* Work out the sizes of the dynamic relocation entries.  */
2405
2406static boolean
2407elf64_alpha_calc_dynrel_sizes (h, info)
2408     struct alpha_elf_link_hash_entry *h;
2409     struct bfd_link_info *info;
2410{
2411  /* If the symbol was defined as a common symbol in a regular object
2412     file, and there was no definition in any dynamic object, then the
2413     linker will have allocated space for the symbol in a common
2414     section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
2415     set.  This is done for dynamic symbols in
2416     elf_adjust_dynamic_symbol but this is not done for non-dynamic
2417     symbols, somehow.  */
2418  if (((h->root.elf_link_hash_flags
2419       & (ELF_LINK_HASH_DEF_REGULAR
2420	  | ELF_LINK_HASH_REF_REGULAR
2421	  | ELF_LINK_HASH_DEF_DYNAMIC))
2422       == ELF_LINK_HASH_REF_REGULAR)
2423      && (h->root.root.type == bfd_link_hash_defined
2424	  || h->root.root.type == bfd_link_hash_defweak)
2425      && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
2426    {
2427      h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2428    }
2429
2430  /* If the symbol is dynamic, we'll need all the relocations in their
2431     natural form.  If it has been forced local, we'll need the same
2432     number of RELATIVE relocations.  */
2433  if (alpha_elf_dynamic_symbol_p (&h->root, info)
2434      || alpha_elf_force_local_symbol_p (&h->root, info))
2435    {
2436      struct alpha_elf_reloc_entry *relent;
2437      bfd *dynobj;
2438      struct alpha_elf_got_entry *gotent;
2439      bfd_size_type count;
2440      asection *srel;
2441
2442      /* We need it when we have .o files compiled with PIC, but
2443	 linked statically. */
2444      for (relent = h->reloc_entries; relent; relent = relent->next)
2445	{
2446	  relent->srel->_raw_size +=
2447	    sizeof (Elf64_External_Rela) * relent->count;
2448	}
2449
2450      dynobj = elf_hash_table(info)->dynobj;
2451      count = 0;
2452
2453      for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2454	count++;
2455
2456      /* If we are using a .plt entry, subtract one, as the first
2457	 reference uses a .rela.plt entry instead.  */
2458      if (h->root.plt_offset != MINUS_ONE)
2459	count--;
2460
2461      if (count > 0)
2462	{
2463	  srel = bfd_get_section_by_name (dynobj, ".rela.got");
2464	  BFD_ASSERT (srel != NULL);
2465	  srel->_raw_size += sizeof (Elf64_External_Rela) * count;
2466	}
2467    }
2468
2469  /* Shared objects require at least RELATIVE relocs for all REFQUAD
2470     and REFLONG relocations.  */
2471  if (info->shared)
2472    {
2473      struct alpha_elf_reloc_entry *relent;
2474
2475      for (relent = h->reloc_entries; relent; relent = relent->next)
2476	if (relent->rtype == R_ALPHA_REFLONG
2477	    || relent->rtype == R_ALPHA_REFQUAD)
2478	  {
2479	    relent->srel->_raw_size +=
2480	      sizeof(Elf64_External_Rela) * relent->count;
2481	  }
2482    }
2483
2484  return true;
2485}
2486
2487/* Set the sizes of the dynamic sections.  */
2488
2489static boolean
2490elf64_alpha_size_dynamic_sections (output_bfd, info)
2491     bfd *output_bfd;
2492     struct bfd_link_info *info;
2493{
2494  bfd *dynobj;
2495  asection *s;
2496  boolean reltext;
2497  boolean relplt;
2498
2499  dynobj = elf_hash_table(info)->dynobj;
2500  BFD_ASSERT(dynobj != NULL);
2501
2502  if (elf_hash_table (info)->dynamic_sections_created)
2503    {
2504      /* Set the contents of the .interp section to the interpreter.  */
2505      if (!info->shared)
2506	{
2507	  s = bfd_get_section_by_name (dynobj, ".interp");
2508	  BFD_ASSERT (s != NULL);
2509	  s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2510	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2511	}
2512
2513      /* Now that we've seen all of the input files, we can decide which
2514	 symbols need dynamic relocation entries and which don't.  We've
2515	 collected information in check_relocs that we can now apply to
2516	 size the dynamic relocation sections.  */
2517      alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2518				    elf64_alpha_calc_dynrel_sizes,
2519				    info);
2520
2521      /* When building shared libraries, each local .got entry needs a
2522	 RELATIVE reloc.  */
2523      if (info->shared)
2524	{
2525	  bfd *i;
2526	  asection *srel;
2527	  bfd_size_type count;
2528
2529	  srel = bfd_get_section_by_name (dynobj, ".rela.got");
2530	  BFD_ASSERT (srel != NULL);
2531
2532	  for (i = alpha_elf_hash_table(info)->got_list, count = 0;
2533	       i != NULL;
2534	       i = alpha_elf_tdata(i)->got_link_next)
2535	    count += alpha_elf_tdata(i)->n_local_got_entries;
2536
2537	  srel->_raw_size += count * sizeof(Elf64_External_Rela);
2538	}
2539    }
2540  /* else we're not dynamic and by definition we don't need such things.  */
2541
2542  /* The check_relocs and adjust_dynamic_symbol entry points have
2543     determined the sizes of the various dynamic sections.  Allocate
2544     memory for them.  */
2545  reltext = false;
2546  relplt = false;
2547  for (s = dynobj->sections; s != NULL; s = s->next)
2548    {
2549      const char *name;
2550      boolean strip;
2551
2552      if (!(s->flags & SEC_LINKER_CREATED))
2553	continue;
2554
2555      /* It's OK to base decisions on the section name, because none
2556	 of the dynobj section names depend upon the input files.  */
2557      name = bfd_get_section_name (dynobj, s);
2558
2559      /* If we don't need this section, strip it from the output file.
2560	 This is to handle .rela.bss and .rela.plt.  We must create it
2561	 in create_dynamic_sections, because it must be created before
2562	 the linker maps input sections to output sections.  The
2563	 linker does that before adjust_dynamic_symbol is called, and
2564	 it is that function which decides whether anything needs to
2565	 go into these sections.  */
2566
2567      strip = false;
2568
2569      if (strncmp (name, ".rela", 5) == 0)
2570	{
2571	  strip = (s->_raw_size == 0);
2572
2573	  if (!strip)
2574	    {
2575	      const char *outname;
2576	      asection *target;
2577
2578	      /* If this relocation section applies to a read only
2579		 section, then we probably need a DT_TEXTREL entry.  */
2580	      outname = bfd_get_section_name (output_bfd,
2581					      s->output_section);
2582	      target = bfd_get_section_by_name (output_bfd, outname + 5);
2583	      if (target != NULL
2584		  && (target->flags & SEC_READONLY) != 0
2585		  && (target->flags & SEC_ALLOC) != 0)
2586		reltext = true;
2587
2588	      if (strcmp(name, ".rela.plt") == 0)
2589		relplt = true;
2590
2591	      /* We use the reloc_count field as a counter if we need
2592		 to copy relocs into the output file.  */
2593	      s->reloc_count = 0;
2594	    }
2595	}
2596      else if (strcmp (name, ".plt") != 0)
2597	{
2598	  /* It's not one of our dynamic sections, so don't allocate space.  */
2599	  continue;
2600	}
2601
2602      if (strip)
2603	elf64_alpha_strip_section_from_output (s);
2604      else
2605	{
2606	  /* Allocate memory for the section contents.  */
2607	  s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
2608	  if (s->contents == NULL && s->_raw_size != 0)
2609	    return false;
2610	}
2611    }
2612
2613  /* If we are generating a shared library, we generate a section
2614     symbol for each output section.  These are local symbols, which
2615     means that they must come first in the dynamic symbol table.
2616     That means we must increment the dynamic symbol index of every
2617     other dynamic symbol.  */
2618  if (info->shared)
2619    {
2620      long c[2], i;
2621      asection *p;
2622
2623      c[0] = 0;
2624      c[1] = bfd_count_sections (output_bfd);
2625
2626      elf_hash_table (info)->dynsymcount += c[1];
2627      elf_link_hash_traverse (elf_hash_table(info),
2628			      elf64_alpha_adjust_dynindx,
2629			      (PTR) c);
2630
2631      for (i = 1, p = output_bfd->sections;
2632	   p != NULL;
2633	   p = p->next, i++)
2634	{
2635	  elf_section_data (p)->dynindx = i;
2636	  /* These symbols will have no names, so we don't need to
2637	     fiddle with dynstr_index.  */
2638	}
2639    }
2640
2641  if (elf_hash_table (info)->dynamic_sections_created)
2642    {
2643      /* Add some entries to the .dynamic section.  We fill in the
2644	 values later, in elf64_alpha_finish_dynamic_sections, but we
2645	 must add the entries now so that we get the correct size for
2646	 the .dynamic section.  The DT_DEBUG entry is filled in by the
2647	 dynamic linker and used by the debugger.  */
2648      if (!info->shared)
2649	{
2650	  if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
2651	    return false;
2652	}
2653
2654      if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
2655	return false;
2656
2657      if (relplt)
2658	{
2659	  if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2660	      || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2661	      || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
2662	    return false;
2663	}
2664
2665      if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
2666	  || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
2667	  || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
2668					    sizeof(Elf64_External_Rela)))
2669	return false;
2670
2671      if (reltext)
2672	{
2673	  if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
2674	    return false;
2675	}
2676    }
2677
2678  return true;
2679}
2680
2681/* Increment the index of a dynamic symbol by a given amount.  Called
2682   via elf_link_hash_traverse.  */
2683
2684static boolean
2685elf64_alpha_adjust_dynindx (h, cparg)
2686     struct elf_link_hash_entry *h;
2687     PTR cparg;
2688{
2689  long *cp = (long *)cparg;
2690
2691  if (h->dynindx >= cp[0])
2692    h->dynindx += cp[1];
2693
2694  return true;
2695}
2696
2697/* Relocate an Alpha ELF section.  */
2698
2699static boolean
2700elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
2701			      contents, relocs, local_syms, local_sections)
2702     bfd *output_bfd;
2703     struct bfd_link_info *info;
2704     bfd *input_bfd;
2705     asection *input_section;
2706     bfd_byte *contents;
2707     Elf_Internal_Rela *relocs;
2708     Elf_Internal_Sym *local_syms;
2709     asection **local_sections;
2710{
2711  Elf_Internal_Shdr *symtab_hdr;
2712  Elf_Internal_Rela *rel;
2713  Elf_Internal_Rela *relend;
2714  asection *sec, *sgot, *srel, *srelgot;
2715  bfd *dynobj, *gotobj;
2716  bfd_vma gp;
2717
2718  srelgot = srel = NULL;
2719  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2720  dynobj = elf_hash_table (info)->dynobj;
2721  if (dynobj)
2722    {
2723      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2724    }
2725
2726  /* Find the gp value for this input bfd.  */
2727  sgot = NULL;
2728  gp = 0;
2729  gotobj = alpha_elf_tdata (input_bfd)->gotobj;
2730  if (gotobj)
2731    {
2732      sgot = alpha_elf_tdata (gotobj)->got;
2733      gp = _bfd_get_gp_value (gotobj);
2734      if (gp == 0)
2735	{
2736	  gp = (sgot->output_section->vma
2737		+ sgot->output_offset
2738		+ 0x8000);
2739	  _bfd_set_gp_value (gotobj, gp);
2740	}
2741    }
2742
2743  rel = relocs;
2744  relend = relocs + input_section->reloc_count;
2745  for (; rel < relend; rel++)
2746    {
2747      int r_type;
2748      reloc_howto_type *howto;
2749      unsigned long r_symndx;
2750      struct alpha_elf_link_hash_entry *h;
2751      Elf_Internal_Sym *sym;
2752      bfd_vma relocation;
2753      bfd_vma addend;
2754      bfd_reloc_status_type r;
2755
2756      r_type = ELF64_R_TYPE(rel->r_info);
2757      if (r_type < 0 || r_type >= (int) R_ALPHA_max)
2758	{
2759	  bfd_set_error (bfd_error_bad_value);
2760	  return false;
2761	}
2762      howto = elf64_alpha_howto_table + r_type;
2763
2764      r_symndx = ELF64_R_SYM(rel->r_info);
2765
2766      if (info->relocateable)
2767	{
2768	  /* This is a relocateable link.  We don't have to change
2769	     anything, unless the reloc is against a section symbol,
2770	     in which case we have to adjust according to where the
2771	     section symbol winds up in the output section.  */
2772	  if (r_symndx < symtab_hdr->sh_info)
2773	    {
2774	      sym = local_syms + r_symndx;
2775	      if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
2776		{
2777		  sec = local_sections[r_symndx];
2778		  rel->r_addend += sec->output_offset + sym->st_value;
2779		}
2780	    }
2781
2782	  continue;
2783	}
2784
2785      /* This is a final link.  */
2786
2787      h = NULL;
2788      sym = NULL;
2789      sec = NULL;
2790
2791      if (r_symndx < symtab_hdr->sh_info)
2792	{
2793	  sym = local_syms + r_symndx;
2794	  sec = local_sections[r_symndx];
2795	  relocation = (sec->output_section->vma
2796			+ sec->output_offset
2797			+ sym->st_value);
2798	}
2799      else
2800	{
2801	  h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
2802
2803	  while (h->root.root.type == bfd_link_hash_indirect
2804		 || h->root.root.type == bfd_link_hash_warning)
2805	    h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2806
2807	  if (h->root.root.type == bfd_link_hash_defined
2808	      || h->root.root.type == bfd_link_hash_defweak)
2809	    {
2810	      sec = h->root.root.u.def.section;
2811
2812#if rth_notdef
2813	      if ((r_type == R_ALPHA_LITERAL
2814		   && elf_hash_table(info)->dynamic_sections_created
2815		   && (!info->shared
2816		       || !info->symbolic
2817		       || !(h->root.elf_link_hash_flags
2818			    & ELF_LINK_HASH_DEF_REGULAR)))
2819		  || (info->shared
2820		      && (!info->symbolic
2821			  || !(h->root.elf_link_hash_flags
2822			       & ELF_LINK_HASH_DEF_REGULAR))
2823		      && (input_section->flags & SEC_ALLOC)
2824		      && (r_type == R_ALPHA_REFLONG
2825			  || r_type == R_ALPHA_REFQUAD
2826			  || r_type == R_ALPHA_LITERAL)))
2827		{
2828		  /* In these cases, we don't need the relocation value.
2829		     We check specially because in some obscure cases
2830		     sec->output_section will be NULL.  */
2831		  relocation = 0;
2832		}
2833#else
2834	      /* FIXME: Are not these obscure cases simply bugs?  Let's
2835		 get something working and come back to this.  */
2836	      if (sec->output_section == NULL)
2837		relocation = 0;
2838#endif /* rth_notdef */
2839	      else
2840		{
2841		  relocation = (h->root.root.u.def.value
2842				+ sec->output_section->vma
2843				+ sec->output_offset);
2844		}
2845	    }
2846	  else if (h->root.root.type == bfd_link_hash_undefweak)
2847	    relocation = 0;
2848	  else if (info->shared && !info->symbolic)
2849	    relocation = 0;
2850	  else
2851	    {
2852	      if (!((*info->callbacks->undefined_symbol)
2853		    (info, h->root.root.root.string, input_bfd,
2854		     input_section, rel->r_offset)))
2855		return false;
2856	      relocation = 0;
2857	    }
2858	}
2859      addend = rel->r_addend;
2860
2861      switch (r_type)
2862	{
2863	case R_ALPHA_GPDISP:
2864	  {
2865	    bfd_byte *p_ldah, *p_lda;
2866
2867	    BFD_ASSERT(gp != 0);
2868
2869	    relocation = (input_section->output_section->vma
2870			  + input_section->output_offset
2871			  + rel->r_offset);
2872
2873	    p_ldah = contents + rel->r_offset - input_section->vma;
2874	    p_lda = p_ldah + rel->r_addend;
2875
2876	    r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
2877					     p_ldah, p_lda);
2878	  }
2879	  break;
2880
2881	case R_ALPHA_OP_PUSH:
2882	case R_ALPHA_OP_STORE:
2883	case R_ALPHA_OP_PSUB:
2884	case R_ALPHA_OP_PRSHIFT:
2885	  /* We hate these silly beasts.  */
2886	  abort();
2887
2888	case R_ALPHA_LITERAL:
2889	  {
2890	    struct alpha_elf_got_entry *gotent;
2891
2892	    BFD_ASSERT(sgot != NULL);
2893	    BFD_ASSERT(gp != 0);
2894
2895	    if (h != NULL)
2896	      {
2897		gotent = h->got_entries;
2898		BFD_ASSERT(gotent != NULL);
2899
2900		while (gotent->gotobj != gotobj || gotent->addend != addend)
2901		  gotent = gotent->next;
2902
2903		/* Initialize the .got entry's value.  */
2904		if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
2905		  {
2906		    bfd_put_64 (output_bfd, relocation+addend,
2907				sgot->contents + gotent->got_offset);
2908
2909		    /* If the symbol has been forced local, output a
2910		       RELATIVE reloc, otherwise it will be handled in
2911		       finish_dynamic_symbol.  */
2912		    if (alpha_elf_force_local_symbol_p (&h->root, info))
2913		      {
2914			Elf_Internal_Rela outrel;
2915
2916			BFD_ASSERT(srelgot != NULL);
2917
2918			outrel.r_offset = (sgot->output_section->vma
2919					   + sgot->output_offset
2920					   + gotent->got_offset);
2921			outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
2922			outrel.r_addend = 0;
2923
2924			bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2925						   ((Elf64_External_Rela *)
2926						    srelgot->contents)
2927						   + srelgot->reloc_count++);
2928			BFD_ASSERT (sizeof(Elf64_External_Rela)
2929				      * srelgot->reloc_count
2930				    <= srelgot->_cooked_size);
2931		      }
2932
2933		    gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
2934		  }
2935	      }
2936	    else
2937	      {
2938		gotent = (alpha_elf_tdata(input_bfd)->
2939			  local_got_entries[r_symndx]);
2940		while (gotent->addend != addend)
2941		  gotent = gotent->next;
2942
2943		if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
2944		  {
2945		    bfd_put_64 (output_bfd, relocation+addend,
2946				sgot->contents + gotent->got_offset);
2947
2948		    /* Local got entries need RELATIVE relocs in shared
2949		       libraries.  */
2950		    if (info->shared)
2951		      {
2952			Elf_Internal_Rela outrel;
2953
2954			BFD_ASSERT(srelgot != NULL);
2955
2956			outrel.r_offset = (sgot->output_section->vma
2957					   + sgot->output_offset
2958					   + gotent->got_offset);
2959			outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
2960			outrel.r_addend = 0;
2961
2962			bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2963						   ((Elf64_External_Rela *)
2964						    srelgot->contents)
2965						   + srelgot->reloc_count++);
2966			BFD_ASSERT (sizeof(Elf64_External_Rela)
2967				      * srelgot->reloc_count
2968				    <= srelgot->_cooked_size);
2969		      }
2970
2971		    gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
2972		  }
2973	      }
2974
2975	    /* Figure the gprel relocation.  */
2976	    addend = 0;
2977	    relocation = (sgot->output_section->vma
2978			  + sgot->output_offset
2979			  + gotent->got_offset);
2980	    relocation -= gp;
2981	  }
2982	  /* overflow handled by _bfd_final_link_relocate */
2983	  goto default_reloc;
2984
2985	case R_ALPHA_GPREL32:
2986	  BFD_ASSERT(gp != 0);
2987	  relocation -= gp;
2988	  goto default_reloc;
2989
2990	case R_ALPHA_BRADDR:
2991	case R_ALPHA_HINT:
2992	  /* The regular PC-relative stuff measures from the start of
2993	     the instruction rather than the end.  */
2994	  addend -= 4;
2995	  goto default_reloc;
2996
2997	case R_ALPHA_REFLONG:
2998	case R_ALPHA_REFQUAD:
2999	  {
3000	    Elf_Internal_Rela outrel;
3001	    boolean skip;
3002
3003	    /* Careful here to remember RELATIVE relocations for global
3004	       variables for symbolic shared objects.  */
3005
3006	    if (h && alpha_elf_dynamic_symbol_p (&h->root, info))
3007	      {
3008		BFD_ASSERT(h->root.dynindx != -1);
3009		outrel.r_info = ELF64_R_INFO(h->root.dynindx, r_type);
3010		outrel.r_addend = addend;
3011		addend = 0, relocation = 0;
3012	      }
3013	    else if (info->shared)
3014	      {
3015		outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3016		outrel.r_addend = 0;
3017	      }
3018	    else
3019	      goto default_reloc;
3020
3021	    if (!srel)
3022	      {
3023		const char *name;
3024
3025		name = (bfd_elf_string_from_elf_section
3026			(input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
3027			 elf_section_data(input_section)->rel_hdr.sh_name));
3028		BFD_ASSERT(name != NULL);
3029
3030		srel = bfd_get_section_by_name (dynobj, name);
3031		BFD_ASSERT(srel != NULL);
3032	      }
3033
3034	    skip = false;
3035
3036	    if (elf_section_data (input_section)->stab_info == NULL)
3037	      outrel.r_offset = rel->r_offset;
3038	    else
3039	      {
3040		bfd_vma off;
3041
3042		off = (_bfd_stab_section_offset
3043		       (output_bfd, &elf_hash_table (info)->stab_info,
3044			input_section,
3045			&elf_section_data (input_section)->stab_info,
3046			rel->r_offset));
3047		if (off == (bfd_vma) -1)
3048		  skip = true;
3049		outrel.r_offset = off;
3050	      }
3051
3052	    if (! skip)
3053	      outrel.r_offset += (input_section->output_section->vma
3054				  + input_section->output_offset);
3055	    else
3056	      memset (&outrel, 0, sizeof outrel);
3057
3058	    bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3059				       ((Elf64_External_Rela *)
3060					srel->contents)
3061				       + srel->reloc_count++);
3062	    BFD_ASSERT (sizeof(Elf64_External_Rela) * srel->reloc_count
3063			<= srel->_cooked_size);
3064	  }
3065	  goto default_reloc;
3066
3067	default:
3068	default_reloc:
3069	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3070					contents, rel->r_offset, relocation,
3071					addend);
3072	  break;
3073	}
3074
3075      switch (r)
3076	{
3077	case bfd_reloc_ok:
3078	  break;
3079
3080	case bfd_reloc_overflow:
3081	  {
3082	    const char *name;
3083
3084	    if (h != NULL)
3085	      name = h->root.root.root.string;
3086	    else
3087	      {
3088		name = (bfd_elf_string_from_elf_section
3089			(input_bfd, symtab_hdr->sh_link, sym->st_name));
3090		if (name == NULL)
3091		  return false;
3092		if (*name == '\0')
3093		  name = bfd_section_name (input_bfd, sec);
3094	      }
3095	    if (! ((*info->callbacks->reloc_overflow)
3096		   (info, name, howto->name, (bfd_vma) 0,
3097		    input_bfd, input_section, rel->r_offset)))
3098	      return false;
3099	  }
3100	  break;
3101
3102	default:
3103	case bfd_reloc_outofrange:
3104	  abort ();
3105	}
3106    }
3107
3108  return true;
3109}
3110
3111/* Finish up dynamic symbol handling.  We set the contents of various
3112   dynamic sections here.  */
3113
3114static boolean
3115elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
3116     bfd *output_bfd;
3117     struct bfd_link_info *info;
3118     struct elf_link_hash_entry *h;
3119     Elf_Internal_Sym *sym;
3120{
3121  bfd *dynobj = elf_hash_table(info)->dynobj;
3122
3123  if (h->plt_offset != MINUS_ONE)
3124    {
3125      /* Fill in the .plt entry for this symbol.  */
3126      asection *splt, *sgot, *srel;
3127      Elf_Internal_Rela outrel;
3128      bfd_vma got_addr, plt_addr;
3129      bfd_vma plt_index;
3130      struct alpha_elf_got_entry *gotent;
3131
3132      BFD_ASSERT (h->dynindx != -1);
3133
3134      /* The first .got entry will be updated by the .plt with the
3135	 address of the target function.  */
3136      gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3137      BFD_ASSERT (gotent && gotent->addend == 0);
3138
3139      splt = bfd_get_section_by_name (dynobj, ".plt");
3140      BFD_ASSERT (splt != NULL);
3141      srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3142      BFD_ASSERT (srel != NULL);
3143      sgot = alpha_elf_tdata (gotent->gotobj)->got;
3144      BFD_ASSERT (sgot != NULL);
3145
3146      got_addr = (sgot->output_section->vma
3147		  + sgot->output_offset
3148		  + gotent->got_offset);
3149      plt_addr = (splt->output_section->vma
3150		  + splt->output_offset
3151		  + h->plt_offset);
3152
3153      plt_index = (h->plt_offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3154
3155      /* Fill in the entry in the procedure linkage table.  */
3156      {
3157	unsigned insn1, insn2, insn3;
3158
3159	insn1 = PLT_ENTRY_WORD1 | ((-(h->plt_offset + 4) >> 2) & 0x1fffff);
3160	insn2 = PLT_ENTRY_WORD2;
3161	insn3 = PLT_ENTRY_WORD3;
3162
3163	bfd_put_32 (output_bfd, insn1, splt->contents + h->plt_offset);
3164	bfd_put_32 (output_bfd, insn2, splt->contents + h->plt_offset + 4);
3165	bfd_put_32 (output_bfd, insn3, splt->contents + h->plt_offset + 8);
3166      }
3167
3168      /* Fill in the entry in the .rela.plt section.  */
3169      outrel.r_offset = got_addr;
3170      outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
3171      outrel.r_addend = 0;
3172
3173      bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3174				 ((Elf64_External_Rela *)srel->contents
3175				  + plt_index));
3176
3177      if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3178	{
3179	  /* Mark the symbol as undefined, rather than as defined in the
3180	     .plt section.  Leave the value alone.  */
3181	  sym->st_shndx = SHN_UNDEF;
3182	}
3183
3184      /* Fill in the entries in the .got.  */
3185      bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
3186
3187      /* Subsequent .got entries will continue to bounce through the .plt.  */
3188      if (gotent->next)
3189	{
3190	  srel = bfd_get_section_by_name (dynobj, ".rela.got");
3191	  BFD_ASSERT (! info->shared || srel != NULL);
3192
3193	  gotent = gotent->next;
3194	  do
3195	    {
3196	      sgot = alpha_elf_tdata(gotent->gotobj)->got;
3197	      BFD_ASSERT(sgot != NULL);
3198	      BFD_ASSERT(gotent->addend == 0);
3199
3200	      bfd_put_64 (output_bfd, plt_addr,
3201		          sgot->contents + gotent->got_offset);
3202
3203	      if (info->shared)
3204		{
3205		  outrel.r_offset = (sgot->output_section->vma
3206				     + sgot->output_offset
3207				     + gotent->got_offset);
3208		  outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3209		  outrel.r_addend = 0;
3210
3211		  bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3212					     ((Elf64_External_Rela *)
3213					      srel->contents)
3214					     + srel->reloc_count++);
3215		  BFD_ASSERT (sizeof(Elf64_External_Rela) * srel->reloc_count
3216			      <= srel->_cooked_size);
3217		}
3218
3219	      gotent = gotent->next;
3220	    }
3221          while (gotent != NULL);
3222	}
3223    }
3224  else if (alpha_elf_dynamic_symbol_p (h, info))
3225    {
3226      /* Fill in the dynamic relocations for this symbol's .got entries.  */
3227      asection *srel;
3228      Elf_Internal_Rela outrel;
3229      struct alpha_elf_got_entry *gotent;
3230
3231      srel = bfd_get_section_by_name (dynobj, ".rela.got");
3232      BFD_ASSERT (srel != NULL);
3233
3234      outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_GLOB_DAT);
3235      for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3236	   gotent != NULL;
3237	   gotent = gotent->next)
3238	{
3239	  asection *sgot = alpha_elf_tdata (gotent->gotobj)->got;
3240	  outrel.r_offset = (sgot->output_section->vma
3241			     + sgot->output_offset
3242			     + gotent->got_offset);
3243	  outrel.r_addend = gotent->addend;
3244
3245	  bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3246				     ((Elf64_External_Rela *)srel->contents
3247				      + srel->reloc_count++));
3248	  BFD_ASSERT (sizeof(Elf64_External_Rela) * srel->reloc_count
3249		      <= srel->_cooked_size);
3250	}
3251    }
3252
3253  /* Mark some specially defined symbols as absolute.  */
3254  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3255      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3256      || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3257    sym->st_shndx = SHN_ABS;
3258
3259  return true;
3260}
3261
3262/* Finish up the dynamic sections.  */
3263
3264static boolean
3265elf64_alpha_finish_dynamic_sections (output_bfd, info)
3266     bfd *output_bfd;
3267     struct bfd_link_info *info;
3268{
3269  bfd *dynobj;
3270  asection *sdyn;
3271
3272  dynobj = elf_hash_table (info)->dynobj;
3273  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3274
3275  if (elf_hash_table (info)->dynamic_sections_created)
3276    {
3277      asection *splt;
3278      Elf64_External_Dyn *dyncon, *dynconend;
3279
3280      splt = bfd_get_section_by_name (dynobj, ".plt");
3281      BFD_ASSERT (splt != NULL && sdyn != NULL);
3282
3283      dyncon = (Elf64_External_Dyn *) sdyn->contents;
3284      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3285      for (; dyncon < dynconend; dyncon++)
3286	{
3287	  Elf_Internal_Dyn dyn;
3288	  const char *name;
3289	  asection *s;
3290
3291	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3292
3293	  switch (dyn.d_tag)
3294	    {
3295	    case DT_PLTGOT:
3296	      name = ".plt";
3297	      goto get_vma;
3298	    case DT_PLTRELSZ:
3299	      name = ".rela.plt";
3300	      goto get_size;
3301	    case DT_JMPREL:
3302	      name = ".rela.plt";
3303	      goto get_vma;
3304
3305	    case DT_RELASZ:
3306	      /* My interpretation of the TIS v1.1 ELF document indicates
3307		 that RELASZ should not include JMPREL.  This is not what
3308		 the rest of the BFD does.  It is, however, what the
3309		 glibc ld.so wants.  Do this fixup here until we found
3310		 out who is right.  */
3311	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
3312	      if (s)
3313		{
3314		  dyn.d_un.d_val -=
3315		    (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3316		}
3317	      break;
3318
3319	    get_vma:
3320	      s = bfd_get_section_by_name (output_bfd, name);
3321	      dyn.d_un.d_ptr = (s ? s->vma : 0);
3322	      break;
3323
3324	    get_size:
3325	      s = bfd_get_section_by_name (output_bfd, name);
3326	      dyn.d_un.d_val =
3327		(s->_cooked_size ? s->_cooked_size : s->_raw_size);
3328	      break;
3329	    }
3330
3331	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3332	}
3333
3334      /* Initialize the PLT0 entry */
3335      if (splt->_raw_size > 0)
3336	{
3337	  bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
3338	  bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
3339	  bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
3340	  bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
3341
3342	  /* The next two words will be filled in by ld.so */
3343	  bfd_put_64 (output_bfd, 0, splt->contents + 16);
3344	  bfd_put_64 (output_bfd, 0, splt->contents + 24);
3345
3346	  elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3347	    PLT_HEADER_SIZE;
3348	}
3349    }
3350
3351  if (info->shared)
3352    {
3353      asection *sdynsym;
3354      asection *s;
3355      Elf_Internal_Sym sym;
3356
3357      /* Set up the section symbols for the output sections.  */
3358
3359      sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
3360      BFD_ASSERT (sdynsym != NULL);
3361
3362      sym.st_size = 0;
3363      sym.st_name = 0;
3364      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3365      sym.st_other = 0;
3366
3367      for (s = output_bfd->sections; s != NULL; s = s->next)
3368	{
3369	  int indx;
3370
3371	  sym.st_value = s->vma;
3372
3373	  indx = elf_section_data (s)->this_idx;
3374	  BFD_ASSERT (indx > 0);
3375	  sym.st_shndx = indx;
3376
3377	  bfd_elf64_swap_symbol_out (output_bfd, &sym,
3378				     (PTR) (((Elf64_External_Sym *)
3379					     sdynsym->contents)
3380					    + elf_section_data (s)->dynindx));
3381	}
3382
3383      /* Set the sh_info field of the output .dynsym section to the
3384         index of the first global symbol.  */
3385      elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
3386	bfd_count_sections (output_bfd) + 1;
3387    }
3388
3389  return true;
3390}
3391
3392/* We need to use a special link routine to handle the .reginfo and
3393   the .mdebug sections.  We need to merge all instances of these
3394   sections together, not write them all out sequentially.  */
3395
3396static boolean
3397elf64_alpha_final_link (abfd, info)
3398     bfd *abfd;
3399     struct bfd_link_info *info;
3400{
3401  asection *o;
3402  struct bfd_link_order *p;
3403  asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3404  struct ecoff_debug_info debug;
3405  const struct ecoff_debug_swap *swap
3406    = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3407  HDRR *symhdr = &debug.symbolic_header;
3408  PTR mdebug_handle = NULL;
3409
3410  /* Go through the sections and collect the .reginfo and .mdebug
3411     information.  */
3412  reginfo_sec = NULL;
3413  mdebug_sec = NULL;
3414  gptab_data_sec = NULL;
3415  gptab_bss_sec = NULL;
3416  for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3417    {
3418#ifdef ERIC_neverdef
3419      if (strcmp (o->name, ".reginfo") == 0)
3420	{
3421	  memset (&reginfo, 0, sizeof reginfo);
3422
3423	  /* We have found the .reginfo section in the output file.
3424	     Look through all the link_orders comprising it and merge
3425	     the information together.  */
3426	  for (p = o->link_order_head;
3427	       p != (struct bfd_link_order *) NULL;
3428	       p = p->next)
3429	    {
3430	      asection *input_section;
3431	      bfd *input_bfd;
3432	      Elf64_External_RegInfo ext;
3433	      Elf64_RegInfo sub;
3434
3435	      if (p->type != bfd_indirect_link_order)
3436		{
3437		  if (p->type == bfd_fill_link_order)
3438		    continue;
3439		  abort ();
3440		}
3441
3442	      input_section = p->u.indirect.section;
3443	      input_bfd = input_section->owner;
3444
3445	      /* The linker emulation code has probably clobbered the
3446                 size to be zero bytes.  */
3447	      if (input_section->_raw_size == 0)
3448		input_section->_raw_size = sizeof (Elf64_External_RegInfo);
3449
3450	      if (! bfd_get_section_contents (input_bfd, input_section,
3451					      (PTR) &ext,
3452					      (file_ptr) 0,
3453					      sizeof ext))
3454		return false;
3455
3456	      bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
3457
3458	      reginfo.ri_gprmask |= sub.ri_gprmask;
3459	      reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3460	      reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3461	      reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3462	      reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3463
3464	      /* ri_gp_value is set by the function
3465		 alpha_elf_section_processing when the section is
3466		 finally written out.  */
3467
3468	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
3469		 elf_link_input_bfd ignores this section.  */
3470	      input_section->flags &=~ SEC_HAS_CONTENTS;
3471	    }
3472
3473	  /* Force the section size to the value we want.  */
3474	  o->_raw_size = sizeof (Elf64_External_RegInfo);
3475
3476	  /* Skip this section later on (I don't think this currently
3477	     matters, but someday it might).  */
3478	  o->link_order_head = (struct bfd_link_order *) NULL;
3479
3480	  reginfo_sec = o;
3481	}
3482#endif
3483
3484      if (strcmp (o->name, ".mdebug") == 0)
3485	{
3486	  struct extsym_info einfo;
3487
3488	  /* We have found the .mdebug section in the output file.
3489	     Look through all the link_orders comprising it and merge
3490	     the information together.  */
3491	  symhdr->magic = swap->sym_magic;
3492	  /* FIXME: What should the version stamp be?  */
3493	  symhdr->vstamp = 0;
3494	  symhdr->ilineMax = 0;
3495	  symhdr->cbLine = 0;
3496	  symhdr->idnMax = 0;
3497	  symhdr->ipdMax = 0;
3498	  symhdr->isymMax = 0;
3499	  symhdr->ioptMax = 0;
3500	  symhdr->iauxMax = 0;
3501	  symhdr->issMax = 0;
3502	  symhdr->issExtMax = 0;
3503	  symhdr->ifdMax = 0;
3504	  symhdr->crfd = 0;
3505	  symhdr->iextMax = 0;
3506
3507	  /* We accumulate the debugging information itself in the
3508	     debug_info structure.  */
3509	  debug.line = NULL;
3510	  debug.external_dnr = NULL;
3511	  debug.external_pdr = NULL;
3512	  debug.external_sym = NULL;
3513	  debug.external_opt = NULL;
3514	  debug.external_aux = NULL;
3515	  debug.ss = NULL;
3516	  debug.ssext = debug.ssext_end = NULL;
3517	  debug.external_fdr = NULL;
3518	  debug.external_rfd = NULL;
3519	  debug.external_ext = debug.external_ext_end = NULL;
3520
3521	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3522	  if (mdebug_handle == (PTR) NULL)
3523	    return false;
3524
3525	  if (1)
3526	    {
3527	      asection *s;
3528	      EXTR esym;
3529	      bfd_vma last;
3530	      unsigned int i;
3531	      static const char * const name[] =
3532		{
3533		  ".text", ".init", ".fini", ".data",
3534		  ".rodata", ".sdata", ".sbss", ".bss"
3535		};
3536	      static const int sc[] = { scText, scInit, scFini, scData,
3537					  scRData, scSData, scSBss, scBss };
3538
3539	      esym.jmptbl = 0;
3540	      esym.cobol_main = 0;
3541	      esym.weakext = 0;
3542	      esym.reserved = 0;
3543	      esym.ifd = ifdNil;
3544	      esym.asym.iss = issNil;
3545	      esym.asym.st = stLocal;
3546	      esym.asym.reserved = 0;
3547	      esym.asym.index = indexNil;
3548	      for (i = 0; i < 8; i++)
3549		{
3550		  esym.asym.sc = sc[i];
3551		  s = bfd_get_section_by_name (abfd, name[i]);
3552		  if (s != NULL)
3553		    {
3554		      esym.asym.value = s->vma;
3555		      last = s->vma + s->_raw_size;
3556		    }
3557		  else
3558		    esym.asym.value = last;
3559
3560		  if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3561						      name[i], &esym))
3562		    return false;
3563		}
3564	    }
3565
3566	  for (p = o->link_order_head;
3567	       p != (struct bfd_link_order *) NULL;
3568	       p = p->next)
3569	    {
3570	      asection *input_section;
3571	      bfd *input_bfd;
3572	      const struct ecoff_debug_swap *input_swap;
3573	      struct ecoff_debug_info input_debug;
3574	      char *eraw_src;
3575	      char *eraw_end;
3576
3577	      if (p->type != bfd_indirect_link_order)
3578		{
3579		  if (p->type == bfd_fill_link_order)
3580		    continue;
3581		  abort ();
3582		}
3583
3584	      input_section = p->u.indirect.section;
3585	      input_bfd = input_section->owner;
3586
3587	      if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
3588		  || (get_elf_backend_data (input_bfd)
3589		      ->elf_backend_ecoff_debug_swap) == NULL)
3590		{
3591		  /* I don't know what a non ALPHA ELF bfd would be
3592		     doing with a .mdebug section, but I don't really
3593		     want to deal with it.  */
3594		  continue;
3595		}
3596
3597	      input_swap = (get_elf_backend_data (input_bfd)
3598			    ->elf_backend_ecoff_debug_swap);
3599
3600	      BFD_ASSERT (p->size == input_section->_raw_size);
3601
3602	      /* The ECOFF linking code expects that we have already
3603		 read in the debugging information and set up an
3604		 ecoff_debug_info structure, so we do that now.  */
3605	      if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
3606						&input_debug))
3607		return false;
3608
3609	      if (! (bfd_ecoff_debug_accumulate
3610		     (mdebug_handle, abfd, &debug, swap, input_bfd,
3611		      &input_debug, input_swap, info)))
3612		return false;
3613
3614	      /* Loop through the external symbols.  For each one with
3615		 interesting information, try to find the symbol in
3616		 the linker global hash table and save the information
3617		 for the output external symbols.  */
3618	      eraw_src = input_debug.external_ext;
3619	      eraw_end = (eraw_src
3620			  + (input_debug.symbolic_header.iextMax
3621			     * input_swap->external_ext_size));
3622	      for (;
3623		   eraw_src < eraw_end;
3624		   eraw_src += input_swap->external_ext_size)
3625		{
3626		  EXTR ext;
3627		  const char *name;
3628		  struct alpha_elf_link_hash_entry *h;
3629
3630		  (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
3631		  if (ext.asym.sc == scNil
3632		      || ext.asym.sc == scUndefined
3633		      || ext.asym.sc == scSUndefined)
3634		    continue;
3635
3636		  name = input_debug.ssext + ext.asym.iss;
3637		  h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
3638						  name, false, false, true);
3639		  if (h == NULL || h->esym.ifd != -2)
3640		    continue;
3641
3642		  if (ext.ifd != -1)
3643		    {
3644		      BFD_ASSERT (ext.ifd
3645				  < input_debug.symbolic_header.ifdMax);
3646		      ext.ifd = input_debug.ifdmap[ext.ifd];
3647		    }
3648
3649		  h->esym = ext;
3650		}
3651
3652	      /* Free up the information we just read.  */
3653	      free (input_debug.line);
3654	      free (input_debug.external_dnr);
3655	      free (input_debug.external_pdr);
3656	      free (input_debug.external_sym);
3657	      free (input_debug.external_opt);
3658	      free (input_debug.external_aux);
3659	      free (input_debug.ss);
3660	      free (input_debug.ssext);
3661	      free (input_debug.external_fdr);
3662	      free (input_debug.external_rfd);
3663	      free (input_debug.external_ext);
3664
3665	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
3666		 elf_link_input_bfd ignores this section.  */
3667	      input_section->flags &=~ SEC_HAS_CONTENTS;
3668	    }
3669
3670#ifdef ERIC_neverdef
3671	  if (info->shared)
3672	    {
3673	      /* Create .rtproc section.  */
3674	      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3675	      if (rtproc_sec == NULL)
3676		{
3677		  flagword flags = (SEC_HAS_CONTENTS
3678				    | SEC_IN_MEMORY
3679				    | SEC_LINKER_CREATED
3680				    | SEC_READONLY);
3681
3682		  rtproc_sec = bfd_make_section (abfd, ".rtproc");
3683		  if (rtproc_sec == NULL
3684		      || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
3685		      || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
3686		    return false;
3687		}
3688
3689	      if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
3690						     info, rtproc_sec, &debug))
3691		return false;
3692	    }
3693#endif
3694
3695
3696	  /* Build the external symbol information.  */
3697	  einfo.abfd = abfd;
3698	  einfo.info = info;
3699	  einfo.debug = &debug;
3700	  einfo.swap = swap;
3701	  einfo.failed = false;
3702	  elf_link_hash_traverse (elf_hash_table (info),
3703				  elf64_alpha_output_extsym,
3704				  (PTR) &einfo);
3705	  if (einfo.failed)
3706	    return false;
3707
3708	  /* Set the size of the .mdebug section.  */
3709	  o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
3710
3711	  /* Skip this section later on (I don't think this currently
3712	     matters, but someday it might).  */
3713	  o->link_order_head = (struct bfd_link_order *) NULL;
3714
3715	  mdebug_sec = o;
3716	}
3717
3718#ifdef ERIC_neverdef
3719      if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
3720	{
3721	  const char *subname;
3722	  unsigned int c;
3723	  Elf64_gptab *tab;
3724	  Elf64_External_gptab *ext_tab;
3725	  unsigned int i;
3726
3727	  /* The .gptab.sdata and .gptab.sbss sections hold
3728	     information describing how the small data area would
3729	     change depending upon the -G switch.  These sections
3730	     not used in executables files.  */
3731	  if (! info->relocateable)
3732	    {
3733	      asection **secpp;
3734
3735	      for (p = o->link_order_head;
3736		   p != (struct bfd_link_order *) NULL;
3737		   p = p->next)
3738		{
3739		  asection *input_section;
3740
3741		  if (p->type != bfd_indirect_link_order)
3742		    {
3743		      if (p->type == bfd_fill_link_order)
3744			continue;
3745		      abort ();
3746		    }
3747
3748		  input_section = p->u.indirect.section;
3749
3750		  /* Hack: reset the SEC_HAS_CONTENTS flag so that
3751		     elf_link_input_bfd ignores this section.  */
3752		  input_section->flags &=~ SEC_HAS_CONTENTS;
3753		}
3754
3755	      /* Skip this section later on (I don't think this
3756		 currently matters, but someday it might).  */
3757	      o->link_order_head = (struct bfd_link_order *) NULL;
3758
3759	      /* Really remove the section.  */
3760	      for (secpp = &abfd->sections;
3761		   *secpp != o;
3762		   secpp = &(*secpp)->next)
3763		;
3764	      *secpp = (*secpp)->next;
3765	      --abfd->section_count;
3766
3767	      continue;
3768	    }
3769
3770	  /* There is one gptab for initialized data, and one for
3771	     uninitialized data.  */
3772	  if (strcmp (o->name, ".gptab.sdata") == 0)
3773	    gptab_data_sec = o;
3774	  else if (strcmp (o->name, ".gptab.sbss") == 0)
3775	    gptab_bss_sec = o;
3776	  else
3777	    {
3778	      (*_bfd_error_handler)
3779		("%s: illegal section name `%s'",
3780		 bfd_get_filename (abfd), o->name);
3781	      bfd_set_error (bfd_error_nonrepresentable_section);
3782	      return false;
3783	    }
3784
3785	  /* The linker script always combines .gptab.data and
3786	     .gptab.sdata into .gptab.sdata, and likewise for
3787	     .gptab.bss and .gptab.sbss.  It is possible that there is
3788	     no .sdata or .sbss section in the output file, in which
3789	     case we must change the name of the output section.  */
3790	  subname = o->name + sizeof ".gptab" - 1;
3791	  if (bfd_get_section_by_name (abfd, subname) == NULL)
3792	    {
3793	      if (o == gptab_data_sec)
3794		o->name = ".gptab.data";
3795	      else
3796		o->name = ".gptab.bss";
3797	      subname = o->name + sizeof ".gptab" - 1;
3798	      BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
3799	    }
3800
3801	  /* Set up the first entry.  */
3802	  c = 1;
3803	  tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
3804	  if (tab == NULL)
3805	    return false;
3806	  tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
3807	  tab[0].gt_header.gt_unused = 0;
3808
3809	  /* Combine the input sections.  */
3810	  for (p = o->link_order_head;
3811	       p != (struct bfd_link_order *) NULL;
3812	       p = p->next)
3813	    {
3814	      asection *input_section;
3815	      bfd *input_bfd;
3816	      bfd_size_type size;
3817	      unsigned long last;
3818	      bfd_size_type gpentry;
3819
3820	      if (p->type != bfd_indirect_link_order)
3821		{
3822		  if (p->type == bfd_fill_link_order)
3823		    continue;
3824		  abort ();
3825		}
3826
3827	      input_section = p->u.indirect.section;
3828	      input_bfd = input_section->owner;
3829
3830	      /* Combine the gptab entries for this input section one
3831		 by one.  We know that the input gptab entries are
3832		 sorted by ascending -G value.  */
3833	      size = bfd_section_size (input_bfd, input_section);
3834	      last = 0;
3835	      for (gpentry = sizeof (Elf64_External_gptab);
3836		   gpentry < size;
3837		   gpentry += sizeof (Elf64_External_gptab))
3838		{
3839		  Elf64_External_gptab ext_gptab;
3840		  Elf64_gptab int_gptab;
3841		  unsigned long val;
3842		  unsigned long add;
3843		  boolean exact;
3844		  unsigned int look;
3845
3846		  if (! (bfd_get_section_contents
3847			 (input_bfd, input_section, (PTR) &ext_gptab,
3848			  gpentry, sizeof (Elf64_External_gptab))))
3849		    {
3850		      free (tab);
3851		      return false;
3852		    }
3853
3854		  bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
3855						&int_gptab);
3856		  val = int_gptab.gt_entry.gt_g_value;
3857		  add = int_gptab.gt_entry.gt_bytes - last;
3858
3859		  exact = false;
3860		  for (look = 1; look < c; look++)
3861		    {
3862		      if (tab[look].gt_entry.gt_g_value >= val)
3863			tab[look].gt_entry.gt_bytes += add;
3864
3865		      if (tab[look].gt_entry.gt_g_value == val)
3866			exact = true;
3867		    }
3868
3869		  if (! exact)
3870		    {
3871		      Elf64_gptab *new_tab;
3872		      unsigned int max;
3873
3874		      /* We need a new table entry.  */
3875		      new_tab = ((Elf64_gptab *)
3876				 bfd_realloc ((PTR) tab,
3877					      (c + 1) * sizeof (Elf64_gptab)));
3878		      if (new_tab == NULL)
3879			{
3880			  free (tab);
3881			  return false;
3882			}
3883		      tab = new_tab;
3884		      tab[c].gt_entry.gt_g_value = val;
3885		      tab[c].gt_entry.gt_bytes = add;
3886
3887		      /* Merge in the size for the next smallest -G
3888			 value, since that will be implied by this new
3889			 value.  */
3890		      max = 0;
3891		      for (look = 1; look < c; look++)
3892			{
3893			  if (tab[look].gt_entry.gt_g_value < val
3894			      && (max == 0
3895				  || (tab[look].gt_entry.gt_g_value
3896				      > tab[max].gt_entry.gt_g_value)))
3897			    max = look;
3898			}
3899		      if (max != 0)
3900			tab[c].gt_entry.gt_bytes +=
3901			  tab[max].gt_entry.gt_bytes;
3902
3903		      ++c;
3904		    }
3905
3906		  last = int_gptab.gt_entry.gt_bytes;
3907		}
3908
3909	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
3910		 elf_link_input_bfd ignores this section.  */
3911	      input_section->flags &=~ SEC_HAS_CONTENTS;
3912	    }
3913
3914	  /* The table must be sorted by -G value.  */
3915	  if (c > 2)
3916	    qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
3917
3918	  /* Swap out the table.  */
3919	  ext_tab = ((Elf64_External_gptab *)
3920		     bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
3921	  if (ext_tab == NULL)
3922	    {
3923	      free (tab);
3924	      return false;
3925	    }
3926
3927	  for (i = 0; i < c; i++)
3928	    bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
3929	  free (tab);
3930
3931	  o->_raw_size = c * sizeof (Elf64_External_gptab);
3932	  o->contents = (bfd_byte *) ext_tab;
3933
3934	  /* Skip this section later on (I don't think this currently
3935	     matters, but someday it might).  */
3936	  o->link_order_head = (struct bfd_link_order *) NULL;
3937	}
3938#endif
3939
3940    }
3941
3942  /* Invoke the regular ELF backend linker to do all the work.  */
3943  if (! bfd_elf64_bfd_final_link (abfd, info))
3944    return false;
3945
3946  /* Now write out the computed sections.  */
3947
3948  /* The .got subsections...  */
3949  {
3950    bfd *i, *dynobj = elf_hash_table(info)->dynobj;
3951    for (i = alpha_elf_hash_table(info)->got_list;
3952	 i != NULL;
3953	 i = alpha_elf_tdata(i)->got_link_next)
3954      {
3955	asection *sgot;
3956
3957	/* elf_bfd_final_link already did everything in dynobj.  */
3958	if (i == dynobj)
3959	  continue;
3960
3961	sgot = alpha_elf_tdata(i)->got;
3962	if (! bfd_set_section_contents (abfd, sgot->output_section,
3963					sgot->contents, sgot->output_offset,
3964					sgot->_raw_size))
3965	  return false;
3966      }
3967  }
3968
3969#ifdef ERIC_neverdef
3970  if (reginfo_sec != (asection *) NULL)
3971    {
3972      Elf64_External_RegInfo ext;
3973
3974      bfd_alpha_elf64_swap_reginfo_out (abfd, &reginfo, &ext);
3975      if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
3976				      (file_ptr) 0, sizeof ext))
3977	return false;
3978    }
3979#endif
3980
3981  if (mdebug_sec != (asection *) NULL)
3982    {
3983      BFD_ASSERT (abfd->output_has_begun);
3984      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
3985					       swap, info,
3986					       mdebug_sec->filepos))
3987	return false;
3988
3989      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
3990    }
3991
3992  if (gptab_data_sec != (asection *) NULL)
3993    {
3994      if (! bfd_set_section_contents (abfd, gptab_data_sec,
3995				      gptab_data_sec->contents,
3996				      (file_ptr) 0,
3997				      gptab_data_sec->_raw_size))
3998	return false;
3999    }
4000
4001  if (gptab_bss_sec != (asection *) NULL)
4002    {
4003      if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4004				      gptab_bss_sec->contents,
4005				      (file_ptr) 0,
4006				      gptab_bss_sec->_raw_size))
4007	return false;
4008    }
4009
4010  return true;
4011}
4012
4013/* ECOFF swapping routines.  These are used when dealing with the
4014   .mdebug section, which is in the ECOFF debugging format.  Copied
4015   from elf32-mips.c. */
4016static const struct ecoff_debug_swap
4017elf64_alpha_ecoff_debug_swap =
4018{
4019  /* Symbol table magic number.  */
4020  magicSym2,
4021  /* Alignment of debugging information.  E.g., 4.  */
4022  8,
4023  /* Sizes of external symbolic information.  */
4024  sizeof (struct hdr_ext),
4025  sizeof (struct dnr_ext),
4026  sizeof (struct pdr_ext),
4027  sizeof (struct sym_ext),
4028  sizeof (struct opt_ext),
4029  sizeof (struct fdr_ext),
4030  sizeof (struct rfd_ext),
4031  sizeof (struct ext_ext),
4032  /* Functions to swap in external symbolic data.  */
4033  ecoff_swap_hdr_in,
4034  ecoff_swap_dnr_in,
4035  ecoff_swap_pdr_in,
4036  ecoff_swap_sym_in,
4037  ecoff_swap_opt_in,
4038  ecoff_swap_fdr_in,
4039  ecoff_swap_rfd_in,
4040  ecoff_swap_ext_in,
4041  _bfd_ecoff_swap_tir_in,
4042  _bfd_ecoff_swap_rndx_in,
4043  /* Functions to swap out external symbolic data.  */
4044  ecoff_swap_hdr_out,
4045  ecoff_swap_dnr_out,
4046  ecoff_swap_pdr_out,
4047  ecoff_swap_sym_out,
4048  ecoff_swap_opt_out,
4049  ecoff_swap_fdr_out,
4050  ecoff_swap_rfd_out,
4051  ecoff_swap_ext_out,
4052  _bfd_ecoff_swap_tir_out,
4053  _bfd_ecoff_swap_rndx_out,
4054  /* Function to read in symbolic data.  */
4055  elf64_alpha_read_ecoff_info
4056};
4057
4058#define TARGET_LITTLE_SYM	bfd_elf64_alpha_vec
4059#define TARGET_LITTLE_NAME	"elf64-alpha"
4060#define ELF_ARCH		bfd_arch_alpha
4061#define ELF_MACHINE_CODE 	EM_ALPHA
4062#define ELF_MAXPAGESIZE 	0x10000
4063
4064#define bfd_elf64_bfd_link_hash_table_create \
4065  elf64_alpha_bfd_link_hash_table_create
4066
4067#define bfd_elf64_bfd_reloc_type_lookup \
4068  elf64_alpha_bfd_reloc_type_lookup
4069#define elf_info_to_howto \
4070  elf64_alpha_info_to_howto
4071
4072#define bfd_elf64_mkobject \
4073  elf64_alpha_mkobject
4074#define elf_backend_object_p \
4075  elf64_alpha_object_p
4076
4077#define elf_backend_section_from_shdr \
4078  elf64_alpha_section_from_shdr
4079#define elf_backend_fake_sections \
4080  elf64_alpha_fake_sections
4081#define elf_backend_additional_program_headers \
4082  elf64_alpha_additional_program_headers
4083
4084#define bfd_elf64_bfd_is_local_label_name \
4085  elf64_alpha_is_local_label_name
4086#define bfd_elf64_find_nearest_line \
4087  elf64_alpha_find_nearest_line
4088
4089#define elf_backend_check_relocs \
4090  elf64_alpha_check_relocs
4091#define elf_backend_create_dynamic_sections \
4092  elf64_alpha_create_dynamic_sections
4093#define elf_backend_adjust_dynamic_symbol \
4094  elf64_alpha_adjust_dynamic_symbol
4095#define elf_backend_always_size_sections \
4096  elf64_alpha_always_size_sections
4097#define elf_backend_size_dynamic_sections \
4098  elf64_alpha_size_dynamic_sections
4099#define elf_backend_relocate_section \
4100  elf64_alpha_relocate_section
4101#define elf_backend_finish_dynamic_symbol \
4102  elf64_alpha_finish_dynamic_symbol
4103#define elf_backend_finish_dynamic_sections \
4104  elf64_alpha_finish_dynamic_sections
4105#define bfd_elf64_bfd_final_link \
4106  elf64_alpha_final_link
4107
4108#define elf_backend_ecoff_debug_swap \
4109  &elf64_alpha_ecoff_debug_swap
4110
4111/*
4112 * A few constants that determine how the .plt section is set up.
4113 */
4114#define elf_backend_want_got_plt 0
4115#define elf_backend_plt_readonly 0
4116#define elf_backend_want_plt_sym 1
4117
4118#include "elf64-target.h"
4119