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