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