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