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