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