elf64-alpha.c revision 107497
1284345Ssjg/* Alpha specific support for 64-bit ELF
2284345Ssjg   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3284345Ssjg   Free Software Foundation, Inc.
4284345Ssjg   Contributed by Richard Henderson <rth@tamu.edu>.
5284345Ssjg
6284345SsjgThis file is part of BFD, the Binary File Descriptor library.
7284345Ssjg
8284345SsjgThis program is free software; you can redistribute it and/or modify
9284345Ssjgit under the terms of the GNU General Public License as published by
10284345Ssjgthe Free Software Foundation; either version 2 of the License, or
11284345Ssjg(at your option) any later version.
12284345Ssjg
13284345SsjgThis program is distributed in the hope that it will be useful,
14284345Ssjgbut WITHOUT ANY WARRANTY; without even the implied warranty of
15284345SsjgMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16284345SsjgGNU General Public License for more details.
17284345Ssjg
18284345SsjgYou should have received a copy of the GNU General Public License
19284345Ssjgalong with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22/* $FreeBSD: head/contrib/binutils/bfd/elf64-alpha.c 107497 2002-12-02 09:22:58Z obrien $ */
23
24/* We need a published ABI spec for this.  Until one comes out, don't
25   assume this'll remain unchanged forever.  */
26
27#include "bfd.h"
28#include "sysdep.h"
29#include "libbfd.h"
30#include "elf-bfd.h"
31
32#include "elf/alpha.h"
33
34#define ALPHAECOFF
35
36#define NO_COFF_RELOCS
37#define NO_COFF_SYMBOLS
38#define NO_COFF_LINENOS
39
40/* Get the ECOFF swapping routines.  Needed for the debug information.  */
41#include "coff/internal.h"
42#include "coff/sym.h"
43#include "coff/symconst.h"
44#include "coff/ecoff.h"
45#include "coff/alpha.h"
46#include "aout/ar.h"
47#include "libcoff.h"
48#include "libecoff.h"
49#define ECOFF_64
50#include "ecoffswap.h"
51
52static int alpha_elf_dynamic_symbol_p
53  PARAMS((struct elf_link_hash_entry *, struct bfd_link_info *));
54static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
55  PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
56static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
57  PARAMS((bfd *));
58
59static bfd_reloc_status_type elf64_alpha_reloc_nil
60  PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
61static bfd_reloc_status_type elf64_alpha_reloc_bad
62  PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
63static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
64  PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
65static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
66  PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
67
68static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
69  PARAMS((bfd *, bfd_reloc_code_real_type));
70static void elf64_alpha_info_to_howto
71  PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
72
73static boolean elf64_alpha_mkobject
74  PARAMS((bfd *));
75static boolean elf64_alpha_object_p
76  PARAMS((bfd *));
77static boolean elf64_alpha_section_from_shdr
78  PARAMS((bfd *, Elf64_Internal_Shdr *, 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#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
2321
2322/* Handle an Alpha specific section when reading an object file.  This
2323   is called when elfcode.h finds a section with an unknown type.
2324   FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
2325   how to.  */
2326
2327static boolean
2328elf64_alpha_section_from_shdr (abfd, hdr, name)
2329     bfd *abfd;
2330     Elf64_Internal_Shdr *hdr;
2331     const char *name;
2332{
2333  asection *newsect;
2334
2335  /* There ought to be a place to keep ELF backend specific flags, but
2336     at the moment there isn't one.  We just keep track of the
2337     sections by their name, instead.  Fortunately, the ABI gives
2338     suggested names for all the MIPS specific sections, so we will
2339     probably get away with this.  */
2340  switch (hdr->sh_type)
2341    {
2342    case SHT_ALPHA_DEBUG:
2343      if (strcmp (name, ".mdebug") != 0)
2344	return false;
2345      break;
2346    default:
2347      return false;
2348    }
2349
2350  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2351    return false;
2352  newsect = hdr->bfd_section;
2353
2354  if (hdr->sh_type == SHT_ALPHA_DEBUG)
2355    {
2356      if (! bfd_set_section_flags (abfd, newsect,
2357				   (bfd_get_section_flags (abfd, newsect)
2358				    | SEC_DEBUGGING)))
2359	return false;
2360    }
2361
2362  return true;
2363}
2364
2365/* Convert Alpha specific section flags to bfd internal section flags.  */
2366
2367static boolean
2368elf64_alpha_section_flags (flags, hdr)
2369     flagword *flags;
2370     Elf64_Internal_Shdr *hdr;
2371{
2372  if (hdr->sh_flags & SHF_ALPHA_GPREL)
2373    *flags |= SEC_SMALL_DATA;
2374
2375  return true;
2376}
2377
2378/* Set the correct type for an Alpha ELF section.  We do this by the
2379   section name, which is a hack, but ought to work.  */
2380
2381static boolean
2382elf64_alpha_fake_sections (abfd, hdr, sec)
2383     bfd *abfd;
2384     Elf64_Internal_Shdr *hdr;
2385     asection *sec;
2386{
2387  register const char *name;
2388
2389  name = bfd_get_section_name (abfd, sec);
2390
2391  if (strcmp (name, ".mdebug") == 0)
2392    {
2393      hdr->sh_type = SHT_ALPHA_DEBUG;
2394      /* In a shared object on Irix 5.3, the .mdebug section has an
2395         entsize of 0.  FIXME: Does this matter?  */
2396      if ((abfd->flags & DYNAMIC) != 0 )
2397	hdr->sh_entsize = 0;
2398      else
2399	hdr->sh_entsize = 1;
2400    }
2401  else if ((sec->flags & SEC_SMALL_DATA)
2402	   || strcmp (name, ".sdata") == 0
2403	   || strcmp (name, ".sbss") == 0
2404	   || strcmp (name, ".lit4") == 0
2405	   || strcmp (name, ".lit8") == 0)
2406    hdr->sh_flags |= SHF_ALPHA_GPREL;
2407
2408  return true;
2409}
2410
2411/* Hook called by the linker routine which adds symbols from an object
2412   file.  We use it to put .comm items in .sbss, and not .bss.  */
2413
2414static boolean
2415elf64_alpha_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2416     bfd *abfd;
2417     struct bfd_link_info *info;
2418     const Elf_Internal_Sym *sym;
2419     const char **namep ATTRIBUTE_UNUSED;
2420     flagword *flagsp ATTRIBUTE_UNUSED;
2421     asection **secp;
2422     bfd_vma *valp;
2423{
2424  if (sym->st_shndx == SHN_COMMON
2425      && !info->relocateable
2426      && sym->st_size <= elf_gp_size (abfd))
2427    {
2428      /* Common symbols less than or equal to -G nn bytes are
2429	 automatically put into .sbss.  */
2430
2431      asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
2432
2433      if (scomm == NULL)
2434	{
2435	  scomm = bfd_make_section (abfd, ".scommon");
2436	  if (scomm == NULL
2437	      || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
2438						       | SEC_IS_COMMON
2439						       | SEC_LINKER_CREATED)))
2440	    return false;
2441	}
2442
2443      *secp = scomm;
2444      *valp = sym->st_size;
2445    }
2446
2447  return true;
2448}
2449
2450/* Create the .got section.  */
2451
2452static boolean
2453elf64_alpha_create_got_section(abfd, info)
2454     bfd *abfd;
2455     struct bfd_link_info *info ATTRIBUTE_UNUSED;
2456{
2457  asection *s;
2458
2459  if (bfd_get_section_by_name (abfd, ".got"))
2460    return true;
2461
2462  s = bfd_make_section (abfd, ".got");
2463  if (s == NULL
2464      || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2465					   | SEC_HAS_CONTENTS
2466					   | SEC_IN_MEMORY
2467					   | SEC_LINKER_CREATED))
2468      || !bfd_set_section_alignment (abfd, s, 3))
2469    return false;
2470
2471  alpha_elf_tdata (abfd)->got = s;
2472
2473  return true;
2474}
2475
2476/* Create all the dynamic sections.  */
2477
2478static boolean
2479elf64_alpha_create_dynamic_sections (abfd, info)
2480     bfd *abfd;
2481     struct bfd_link_info *info;
2482{
2483  asection *s;
2484  struct elf_link_hash_entry *h;
2485  struct bfd_link_hash_entry *bh;
2486
2487  /* We need to create .plt, .rela.plt, .got, and .rela.got sections.  */
2488
2489  s = bfd_make_section (abfd, ".plt");
2490  if (s == NULL
2491      || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2492					    | SEC_HAS_CONTENTS
2493					    | SEC_IN_MEMORY
2494					    | SEC_LINKER_CREATED
2495					    | SEC_CODE))
2496      || ! bfd_set_section_alignment (abfd, s, 3))
2497    return false;
2498
2499  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2500     .plt section.  */
2501  bh = NULL;
2502  if (! (_bfd_generic_link_add_one_symbol
2503	 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2504	  (bfd_vma) 0, (const char *) NULL, false,
2505	  get_elf_backend_data (abfd)->collect, &bh)))
2506    return false;
2507  h = (struct elf_link_hash_entry *) bh;
2508  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2509  h->type = STT_OBJECT;
2510
2511  if (info->shared
2512      && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2513    return false;
2514
2515  s = bfd_make_section (abfd, ".rela.plt");
2516  if (s == NULL
2517      || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2518					   | SEC_HAS_CONTENTS
2519					   | SEC_IN_MEMORY
2520					   | SEC_LINKER_CREATED
2521					   | SEC_READONLY))
2522      || ! bfd_set_section_alignment (abfd, s, 3))
2523    return false;
2524
2525  /* We may or may not have created a .got section for this object, but
2526     we definitely havn't done the rest of the work.  */
2527
2528  if (!elf64_alpha_create_got_section (abfd, info))
2529    return false;
2530
2531  s = bfd_make_section(abfd, ".rela.got");
2532  if (s == NULL
2533      || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
2534					   | SEC_HAS_CONTENTS
2535					   | SEC_IN_MEMORY
2536					   | SEC_LINKER_CREATED
2537					   | SEC_READONLY))
2538      || !bfd_set_section_alignment (abfd, s, 3))
2539    return false;
2540
2541  /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
2542     dynobj's .got section.  We don't do this in the linker script
2543     because we don't want to define the symbol if we are not creating
2544     a global offset table.  */
2545  bh = NULL;
2546  if (!(_bfd_generic_link_add_one_symbol
2547	(info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
2548	 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
2549	 false, get_elf_backend_data (abfd)->collect, &bh)))
2550    return false;
2551  h = (struct elf_link_hash_entry *) bh;
2552  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2553  h->type = STT_OBJECT;
2554
2555  if (info->shared
2556      && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2557    return false;
2558
2559  elf_hash_table (info)->hgot = h;
2560
2561  return true;
2562}
2563
2564/* Read ECOFF debugging information from a .mdebug section into a
2565   ecoff_debug_info structure.  */
2566
2567static boolean
2568elf64_alpha_read_ecoff_info (abfd, section, debug)
2569     bfd *abfd;
2570     asection *section;
2571     struct ecoff_debug_info *debug;
2572{
2573  HDRR *symhdr;
2574  const struct ecoff_debug_swap *swap;
2575  char *ext_hdr = NULL;
2576
2577  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2578  memset (debug, 0, sizeof (*debug));
2579
2580  ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
2581  if (ext_hdr == NULL && swap->external_hdr_size != 0)
2582    goto error_return;
2583
2584  if (! bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2585				  swap->external_hdr_size))
2586    goto error_return;
2587
2588  symhdr = &debug->symbolic_header;
2589  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2590
2591  /* The symbolic header contains absolute file offsets and sizes to
2592     read.  */
2593#define READ(ptr, offset, count, size, type)				\
2594  if (symhdr->count == 0)						\
2595    debug->ptr = NULL;							\
2596  else									\
2597    {									\
2598      bfd_size_type amt = (bfd_size_type) size * symhdr->count;		\
2599      debug->ptr = (type) bfd_malloc (amt);				\
2600      if (debug->ptr == NULL)						\
2601	goto error_return;						\
2602      if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0	\
2603	  || bfd_bread (debug->ptr, amt, abfd) != amt)			\
2604	goto error_return;						\
2605    }
2606
2607  READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2608  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2609  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2610  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2611  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2612  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2613	union aux_ext *);
2614  READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2615  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2616  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2617  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2618  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2619#undef READ
2620
2621  debug->fdr = NULL;
2622  debug->adjust = NULL;
2623
2624  return true;
2625
2626 error_return:
2627  if (ext_hdr != NULL)
2628    free (ext_hdr);
2629  if (debug->line != NULL)
2630    free (debug->line);
2631  if (debug->external_dnr != NULL)
2632    free (debug->external_dnr);
2633  if (debug->external_pdr != NULL)
2634    free (debug->external_pdr);
2635  if (debug->external_sym != NULL)
2636    free (debug->external_sym);
2637  if (debug->external_opt != NULL)
2638    free (debug->external_opt);
2639  if (debug->external_aux != NULL)
2640    free (debug->external_aux);
2641  if (debug->ss != NULL)
2642    free (debug->ss);
2643  if (debug->ssext != NULL)
2644    free (debug->ssext);
2645  if (debug->external_fdr != NULL)
2646    free (debug->external_fdr);
2647  if (debug->external_rfd != NULL)
2648    free (debug->external_rfd);
2649  if (debug->external_ext != NULL)
2650    free (debug->external_ext);
2651  return false;
2652}
2653
2654/* Alpha ELF local labels start with '$'.  */
2655
2656static boolean
2657elf64_alpha_is_local_label_name (abfd, name)
2658     bfd *abfd ATTRIBUTE_UNUSED;
2659     const char *name;
2660{
2661  return name[0] == '$';
2662}
2663
2664/* Alpha ELF follows MIPS ELF in using a special find_nearest_line
2665   routine in order to handle the ECOFF debugging information.  We
2666   still call this mips_elf_find_line because of the slot
2667   find_line_info in elf_obj_tdata is declared that way.  */
2668
2669struct mips_elf_find_line
2670{
2671  struct ecoff_debug_info d;
2672  struct ecoff_find_line i;
2673};
2674
2675static boolean
2676elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2677			       functionname_ptr, line_ptr)
2678     bfd *abfd;
2679     asection *section;
2680     asymbol **symbols;
2681     bfd_vma offset;
2682     const char **filename_ptr;
2683     const char **functionname_ptr;
2684     unsigned int *line_ptr;
2685{
2686  asection *msec;
2687
2688  if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2689				     filename_ptr, functionname_ptr,
2690				     line_ptr, 0,
2691				     &elf_tdata (abfd)->dwarf2_find_line_info))
2692    return true;
2693
2694  msec = bfd_get_section_by_name (abfd, ".mdebug");
2695  if (msec != NULL)
2696    {
2697      flagword origflags;
2698      struct mips_elf_find_line *fi;
2699      const struct ecoff_debug_swap * const swap =
2700	get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2701
2702      /* If we are called during a link, alpha_elf_final_link may have
2703	 cleared the SEC_HAS_CONTENTS field.  We force it back on here
2704	 if appropriate (which it normally will be).  */
2705      origflags = msec->flags;
2706      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2707	msec->flags |= SEC_HAS_CONTENTS;
2708
2709      fi = elf_tdata (abfd)->find_line_info;
2710      if (fi == NULL)
2711	{
2712	  bfd_size_type external_fdr_size;
2713	  char *fraw_src;
2714	  char *fraw_end;
2715	  struct fdr *fdr_ptr;
2716	  bfd_size_type amt = sizeof (struct mips_elf_find_line);
2717
2718	  fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
2719	  if (fi == NULL)
2720	    {
2721	      msec->flags = origflags;
2722	      return false;
2723	    }
2724
2725	  if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
2726	    {
2727	      msec->flags = origflags;
2728	      return false;
2729	    }
2730
2731	  /* Swap in the FDR information.  */
2732	  amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
2733	  fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
2734	  if (fi->d.fdr == NULL)
2735	    {
2736	      msec->flags = origflags;
2737	      return false;
2738	    }
2739	  external_fdr_size = swap->external_fdr_size;
2740	  fdr_ptr = fi->d.fdr;
2741	  fraw_src = (char *) fi->d.external_fdr;
2742	  fraw_end = (fraw_src
2743		      + fi->d.symbolic_header.ifdMax * external_fdr_size);
2744	  for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2745	    (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2746
2747	  elf_tdata (abfd)->find_line_info = fi;
2748
2749	  /* Note that we don't bother to ever free this information.
2750             find_nearest_line is either called all the time, as in
2751             objdump -l, so the information should be saved, or it is
2752             rarely called, as in ld error messages, so the memory
2753             wasted is unimportant.  Still, it would probably be a
2754             good idea for free_cached_info to throw it away.  */
2755	}
2756
2757      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2758				  &fi->i, filename_ptr, functionname_ptr,
2759				  line_ptr))
2760	{
2761	  msec->flags = origflags;
2762	  return true;
2763	}
2764
2765      msec->flags = origflags;
2766    }
2767
2768  /* Fall back on the generic ELF find_nearest_line routine.  */
2769
2770  return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2771				     filename_ptr, functionname_ptr,
2772				     line_ptr);
2773}
2774
2775/* Structure used to pass information to alpha_elf_output_extsym.  */
2776
2777struct extsym_info
2778{
2779  bfd *abfd;
2780  struct bfd_link_info *info;
2781  struct ecoff_debug_info *debug;
2782  const struct ecoff_debug_swap *swap;
2783  boolean failed;
2784};
2785
2786static boolean
2787elf64_alpha_output_extsym (h, data)
2788     struct alpha_elf_link_hash_entry *h;
2789     PTR data;
2790{
2791  struct extsym_info *einfo = (struct extsym_info *) data;
2792  boolean strip;
2793  asection *sec, *output_section;
2794
2795  if (h->root.root.type == bfd_link_hash_warning)
2796    h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
2797
2798  if (h->root.indx == -2)
2799    strip = false;
2800  else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2801	    || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2802	   && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2803	   && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2804    strip = true;
2805  else if (einfo->info->strip == strip_all
2806	   || (einfo->info->strip == strip_some
2807	       && bfd_hash_lookup (einfo->info->keep_hash,
2808				   h->root.root.root.string,
2809				   false, false) == NULL))
2810    strip = true;
2811  else
2812    strip = false;
2813
2814  if (strip)
2815    return true;
2816
2817  if (h->esym.ifd == -2)
2818    {
2819      h->esym.jmptbl = 0;
2820      h->esym.cobol_main = 0;
2821      h->esym.weakext = 0;
2822      h->esym.reserved = 0;
2823      h->esym.ifd = ifdNil;
2824      h->esym.asym.value = 0;
2825      h->esym.asym.st = stGlobal;
2826
2827      if (h->root.root.type != bfd_link_hash_defined
2828	  && h->root.root.type != bfd_link_hash_defweak)
2829	h->esym.asym.sc = scAbs;
2830      else
2831	{
2832	  const char *name;
2833
2834	  sec = h->root.root.u.def.section;
2835	  output_section = sec->output_section;
2836
2837	  /* When making a shared library and symbol h is the one from
2838	     the another shared library, OUTPUT_SECTION may be null.  */
2839	  if (output_section == NULL)
2840	    h->esym.asym.sc = scUndefined;
2841	  else
2842	    {
2843	      name = bfd_section_name (output_section->owner, output_section);
2844
2845	      if (strcmp (name, ".text") == 0)
2846		h->esym.asym.sc = scText;
2847	      else if (strcmp (name, ".data") == 0)
2848		h->esym.asym.sc = scData;
2849	      else if (strcmp (name, ".sdata") == 0)
2850		h->esym.asym.sc = scSData;
2851	      else if (strcmp (name, ".rodata") == 0
2852		       || strcmp (name, ".rdata") == 0)
2853		h->esym.asym.sc = scRData;
2854	      else if (strcmp (name, ".bss") == 0)
2855		h->esym.asym.sc = scBss;
2856	      else if (strcmp (name, ".sbss") == 0)
2857		h->esym.asym.sc = scSBss;
2858	      else if (strcmp (name, ".init") == 0)
2859		h->esym.asym.sc = scInit;
2860	      else if (strcmp (name, ".fini") == 0)
2861		h->esym.asym.sc = scFini;
2862	      else
2863		h->esym.asym.sc = scAbs;
2864	    }
2865	}
2866
2867      h->esym.asym.reserved = 0;
2868      h->esym.asym.index = indexNil;
2869    }
2870
2871  if (h->root.root.type == bfd_link_hash_common)
2872    h->esym.asym.value = h->root.root.u.c.size;
2873  else if (h->root.root.type == bfd_link_hash_defined
2874	   || h->root.root.type == bfd_link_hash_defweak)
2875    {
2876      if (h->esym.asym.sc == scCommon)
2877	h->esym.asym.sc = scBss;
2878      else if (h->esym.asym.sc == scSCommon)
2879	h->esym.asym.sc = scSBss;
2880
2881      sec = h->root.root.u.def.section;
2882      output_section = sec->output_section;
2883      if (output_section != NULL)
2884	h->esym.asym.value = (h->root.root.u.def.value
2885			      + sec->output_offset
2886			      + output_section->vma);
2887      else
2888	h->esym.asym.value = 0;
2889    }
2890  else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2891    {
2892      /* Set type and value for a symbol with a function stub.  */
2893      h->esym.asym.st = stProc;
2894      sec = bfd_get_section_by_name (einfo->abfd, ".plt");
2895      if (sec == NULL)
2896	h->esym.asym.value = 0;
2897      else
2898	{
2899	  output_section = sec->output_section;
2900	  if (output_section != NULL)
2901	    h->esym.asym.value = (h->root.plt.offset
2902				  + sec->output_offset
2903				  + output_section->vma);
2904	  else
2905	    h->esym.asym.value = 0;
2906	}
2907    }
2908
2909  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2910				      h->root.root.root.string,
2911				      &h->esym))
2912    {
2913      einfo->failed = true;
2914      return false;
2915    }
2916
2917  return true;
2918}
2919
2920/* Search for and possibly create a got entry.  */
2921
2922static struct alpha_elf_got_entry *
2923get_got_entry (abfd, h, r_type, r_symndx, r_addend)
2924     bfd *abfd;
2925     struct alpha_elf_link_hash_entry *h;
2926     unsigned long r_type, r_symndx;
2927     bfd_vma r_addend;
2928{
2929  struct alpha_elf_got_entry *gotent;
2930  struct alpha_elf_got_entry **slot;
2931
2932  if (h)
2933    slot = &h->got_entries;
2934  else
2935    {
2936      /* This is a local .got entry -- record for merge.  */
2937
2938      struct alpha_elf_got_entry **local_got_entries;
2939
2940      local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2941      if (!local_got_entries)
2942	{
2943	  bfd_size_type size;
2944	  Elf_Internal_Shdr *symtab_hdr;
2945
2946	  symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2947	  size = symtab_hdr->sh_info;
2948	  size *= sizeof (struct alpha_elf_got_entry *);
2949
2950	  local_got_entries
2951	    = (struct alpha_elf_got_entry **) bfd_zalloc (abfd, size);
2952	  if (!local_got_entries)
2953	    return NULL;
2954
2955	  alpha_elf_tdata (abfd)->local_got_entries = local_got_entries;
2956	}
2957
2958      slot = &local_got_entries[r_symndx];
2959    }
2960
2961  for (gotent = *slot; gotent ; gotent = gotent->next)
2962    if (gotent->gotobj == abfd
2963	&& gotent->reloc_type == r_type
2964	&& gotent->addend == r_addend)
2965      break;
2966
2967  if (!gotent)
2968    {
2969      int entry_size;
2970      bfd_size_type amt;
2971
2972      amt = sizeof (struct alpha_elf_got_entry);
2973      gotent = (struct alpha_elf_got_entry *) bfd_alloc (abfd, amt);
2974      if (!gotent)
2975	return NULL;
2976
2977      gotent->gotobj = abfd;
2978      gotent->addend = r_addend;
2979      gotent->got_offset = -1;
2980      gotent->use_count = 1;
2981      gotent->reloc_type = r_type;
2982      gotent->reloc_done = 0;
2983      gotent->reloc_xlated = 0;
2984
2985      gotent->next = *slot;
2986      *slot = gotent;
2987
2988      entry_size = alpha_got_entry_size (r_type);
2989      alpha_elf_tdata (abfd)->total_got_size += entry_size;
2990      if (!h)
2991	alpha_elf_tdata(abfd)->local_got_size += entry_size;
2992    }
2993  else
2994    gotent->use_count += 1;
2995
2996  return gotent;
2997}
2998
2999/* Handle dynamic relocations when doing an Alpha ELF link.  */
3000
3001static boolean
3002elf64_alpha_check_relocs (abfd, info, sec, relocs)
3003     bfd *abfd;
3004     struct bfd_link_info *info;
3005     asection *sec;
3006     const Elf_Internal_Rela *relocs;
3007{
3008  bfd *dynobj;
3009  asection *sreloc;
3010  const char *rel_sec_name;
3011  Elf_Internal_Shdr *symtab_hdr;
3012  struct alpha_elf_link_hash_entry **sym_hashes;
3013  const Elf_Internal_Rela *rel, *relend;
3014  boolean got_created;
3015  bfd_size_type amt;
3016
3017  if (info->relocateable)
3018    return true;
3019
3020  dynobj = elf_hash_table(info)->dynobj;
3021  if (dynobj == NULL)
3022    elf_hash_table(info)->dynobj = dynobj = abfd;
3023
3024  sreloc = NULL;
3025  rel_sec_name = NULL;
3026  symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
3027  sym_hashes = alpha_elf_sym_hashes(abfd);
3028  got_created = false;
3029
3030  relend = relocs + sec->reloc_count;
3031  for (rel = relocs; rel < relend; ++rel)
3032    {
3033      enum {
3034	NEED_GOT = 1,
3035	NEED_GOT_ENTRY = 2,
3036	NEED_DYNREL = 4
3037      };
3038
3039      unsigned long r_symndx, r_type;
3040      struct alpha_elf_link_hash_entry *h;
3041      unsigned int gotent_flags;
3042      boolean maybe_dynamic;
3043      unsigned int need;
3044      bfd_vma addend;
3045
3046      r_symndx = ELF64_R_SYM (rel->r_info);
3047      if (r_symndx < symtab_hdr->sh_info)
3048	h = NULL;
3049      else
3050	{
3051	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3052
3053	  while (h->root.root.type == bfd_link_hash_indirect
3054		 || h->root.root.type == bfd_link_hash_warning)
3055	    h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3056
3057	  h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
3058	}
3059
3060      /* We can only get preliminary data on whether a symbol is
3061         locally or externally defined, as not all of the input files
3062         have yet been processed.  Do something with what we know, as
3063         this may help reduce memory usage and processing time later.  */
3064      maybe_dynamic = false;
3065      if (h && ((info->shared
3066		 && (!info->symbolic || info->allow_shlib_undefined))
3067		|| ! (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
3068		|| h->root.root.type == bfd_link_hash_defweak))
3069        maybe_dynamic = true;
3070
3071      need = 0;
3072      gotent_flags = 0;
3073      r_type = ELF64_R_TYPE (rel->r_info);
3074      addend = rel->r_addend;
3075
3076      switch (r_type)
3077	{
3078	case R_ALPHA_LITERAL:
3079	  need = NEED_GOT | NEED_GOT_ENTRY;
3080
3081	  /* Remember how this literal is used from its LITUSEs.
3082	     This will be important when it comes to decide if we can
3083	     create a .plt entry for a function symbol.  */
3084	  while (++rel < relend && ELF64_R_TYPE (rel->r_info) == R_ALPHA_LITUSE)
3085	    if (rel->r_addend >= 1 && rel->r_addend <= 5)
3086	      gotent_flags |= 1 << rel->r_addend;
3087	  --rel;
3088
3089	  /* No LITUSEs -- presumably the address is used somehow.  */
3090	  if (gotent_flags == 0)
3091	    gotent_flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
3092	  break;
3093
3094	case R_ALPHA_GPDISP:
3095	case R_ALPHA_GPREL16:
3096	case R_ALPHA_GPREL32:
3097	case R_ALPHA_GPRELHIGH:
3098	case R_ALPHA_GPRELLOW:
3099	case R_ALPHA_BRSGP:
3100	  need = NEED_GOT;
3101	  break;
3102
3103	case R_ALPHA_REFLONG:
3104	case R_ALPHA_REFQUAD:
3105	  if ((info->shared && (sec->flags & SEC_ALLOC)) || maybe_dynamic)
3106	    need = NEED_DYNREL;
3107	  break;
3108
3109	case R_ALPHA_TLSGD:
3110	case R_ALPHA_TLSLDM:
3111	case R_ALPHA_GOTDTPREL:
3112	  need = NEED_GOT | NEED_GOT_ENTRY;
3113	  break;
3114
3115	case R_ALPHA_GOTTPREL:
3116	  need = NEED_GOT | NEED_GOT_ENTRY;
3117	  gotent_flags = ALPHA_ELF_LINK_HASH_TLS_IE;
3118	  if (info->shared)
3119	    info->flags |= DF_STATIC_TLS;
3120	  break;
3121
3122	case R_ALPHA_TPREL64:
3123	  if (info->shared || maybe_dynamic)
3124	    need = NEED_DYNREL;
3125	  if (info->shared)
3126	    info->flags |= DF_STATIC_TLS;
3127	  break;
3128	}
3129
3130      if (need & NEED_GOT)
3131	{
3132	  if (!got_created)
3133	    {
3134	      if (!elf64_alpha_create_got_section (abfd, info))
3135		return false;
3136
3137	      /* Make sure the object's gotobj is set to itself so
3138		 that we default to every object with its own .got.
3139		 We'll merge .gots later once we've collected each
3140		 object's info.  */
3141	      alpha_elf_tdata(abfd)->gotobj = abfd;
3142
3143	      got_created = 1;
3144	    }
3145	}
3146
3147      if (need & NEED_GOT_ENTRY)
3148	{
3149	  struct alpha_elf_got_entry *gotent;
3150
3151	  gotent = get_got_entry (abfd, h, r_type, r_symndx, addend);
3152	  if (!gotent)
3153	    return false;
3154
3155	  if (gotent_flags)
3156	    {
3157	      gotent->flags |= gotent_flags;
3158	      if (h)
3159		{
3160		  gotent_flags |= h->flags;
3161		  h->flags = gotent_flags;
3162
3163		  /* Make a guess as to whether a .plt entry is needed.  */
3164		  if ((gotent_flags & ALPHA_ELF_LINK_HASH_LU_FUNC)
3165		      && !(gotent_flags & ~ALPHA_ELF_LINK_HASH_LU_FUNC))
3166		    h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3167		  else
3168		    h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3169	        }
3170	    }
3171	}
3172
3173      if (need & NEED_DYNREL)
3174	{
3175	  if (rel_sec_name == NULL)
3176	    {
3177	      rel_sec_name = (bfd_elf_string_from_elf_section
3178			      (abfd, elf_elfheader(abfd)->e_shstrndx,
3179			       elf_section_data(sec)->rel_hdr.sh_name));
3180	      if (rel_sec_name == NULL)
3181		return false;
3182
3183	      BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
3184			  && strcmp (bfd_get_section_name (abfd, sec),
3185				     rel_sec_name+5) == 0);
3186	    }
3187
3188	  /* We need to create the section here now whether we eventually
3189	     use it or not so that it gets mapped to an output section by
3190	     the linker.  If not used, we'll kill it in
3191	     size_dynamic_sections.  */
3192	  if (sreloc == NULL)
3193	    {
3194	      sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
3195	      if (sreloc == NULL)
3196		{
3197		  flagword flags;
3198
3199		  sreloc = bfd_make_section (dynobj, rel_sec_name);
3200		  flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
3201			   | SEC_LINKER_CREATED | SEC_READONLY);
3202		  if (sec->flags & SEC_ALLOC)
3203		    flags |= SEC_ALLOC | SEC_LOAD;
3204		  if (sreloc == NULL
3205		      || !bfd_set_section_flags (dynobj, sreloc, flags)
3206		      || !bfd_set_section_alignment (dynobj, sreloc, 3))
3207		    return false;
3208		}
3209	    }
3210
3211	  if (h)
3212	    {
3213	      /* Since we havn't seen all of the input symbols yet, we
3214		 don't know whether we'll actually need a dynamic relocation
3215		 entry for this reloc.  So make a record of it.  Once we
3216		 find out if this thing needs dynamic relocation we'll
3217		 expand the relocation sections by the appropriate amount.  */
3218
3219	      struct alpha_elf_reloc_entry *rent;
3220
3221	      for (rent = h->reloc_entries; rent; rent = rent->next)
3222		if (rent->rtype == r_type && rent->srel == sreloc)
3223		  break;
3224
3225	      if (!rent)
3226		{
3227		  amt = sizeof (struct alpha_elf_reloc_entry);
3228		  rent = (struct alpha_elf_reloc_entry *) bfd_alloc (abfd, amt);
3229		  if (!rent)
3230		    return false;
3231
3232		  rent->srel = sreloc;
3233		  rent->rtype = r_type;
3234		  rent->count = 1;
3235		  rent->reltext = ((sec->flags & (SEC_READONLY | SEC_ALLOC))
3236				   == (SEC_READONLY | SEC_ALLOC));
3237
3238		  rent->next = h->reloc_entries;
3239		  h->reloc_entries = rent;
3240		}
3241	      else
3242		rent->count++;
3243	    }
3244	  else if (info->shared)
3245	    {
3246	      /* If this is a shared library, and the section is to be
3247		 loaded into memory, we need a RELATIVE reloc.  */
3248	      sreloc->_raw_size += sizeof (Elf64_External_Rela);
3249	      if ((sec->flags & (SEC_READONLY | SEC_ALLOC))
3250		  == (SEC_READONLY | SEC_ALLOC))
3251		info->flags |= DF_TEXTREL;
3252	    }
3253	}
3254    }
3255
3256  return true;
3257}
3258
3259/* Adjust a symbol defined by a dynamic object and referenced by a
3260   regular object.  The current definition is in some section of the
3261   dynamic object, but we're not including those sections.  We have to
3262   change the definition to something the rest of the link can
3263   understand.  */
3264
3265static boolean
3266elf64_alpha_adjust_dynamic_symbol (info, h)
3267     struct bfd_link_info *info;
3268     struct elf_link_hash_entry *h;
3269{
3270  bfd *dynobj;
3271  asection *s;
3272  struct alpha_elf_link_hash_entry *ah;
3273
3274  dynobj = elf_hash_table(info)->dynobj;
3275  ah = (struct alpha_elf_link_hash_entry *)h;
3276
3277  /* Now that we've seen all of the input symbols, finalize our decision
3278     about whether this symbol should get a .plt entry.  */
3279
3280  if (alpha_elf_dynamic_symbol_p (h, info)
3281      && ((h->type == STT_FUNC
3282	   && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
3283	  || (h->type == STT_NOTYPE
3284	      && (ah->flags & ALPHA_ELF_LINK_HASH_LU_FUNC)
3285	      && !(ah->flags & ~ALPHA_ELF_LINK_HASH_LU_FUNC)))
3286      /* Don't prevent otherwise valid programs from linking by attempting
3287	 to create a new .got entry somewhere.  A Correct Solution would be
3288	 to add a new .got section to a new object file and let it be merged
3289	 somewhere later.  But for now don't bother.  */
3290      && ah->got_entries)
3291    {
3292      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3293
3294      s = bfd_get_section_by_name(dynobj, ".plt");
3295      if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
3296	return false;
3297
3298      /* The first bit of the .plt is reserved.  */
3299      if (s->_raw_size == 0)
3300	s->_raw_size = PLT_HEADER_SIZE;
3301
3302      h->plt.offset = s->_raw_size;
3303      s->_raw_size += PLT_ENTRY_SIZE;
3304
3305      /* If this symbol is not defined in a regular file, and we are not
3306	 generating a shared library, then set the symbol to the location
3307	 in the .plt.  This is required to make function pointers compare
3308	 equal between the normal executable and the shared library.  */
3309      if (! info->shared
3310	  && h->root.type != bfd_link_hash_defweak)
3311	{
3312	  h->root.u.def.section = s;
3313	  h->root.u.def.value = h->plt.offset;
3314	}
3315
3316      /* We also need a JMP_SLOT entry in the .rela.plt section.  */
3317      s = bfd_get_section_by_name (dynobj, ".rela.plt");
3318      BFD_ASSERT (s != NULL);
3319      s->_raw_size += sizeof (Elf64_External_Rela);
3320
3321      return true;
3322    }
3323  else
3324    h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3325
3326  /* If this is a weak symbol, and there is a real definition, the
3327     processor independent code will have arranged for us to see the
3328     real definition first, and we can just use the same value.  */
3329  if (h->weakdef != NULL)
3330    {
3331      BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3332		  || h->weakdef->root.type == bfd_link_hash_defweak);
3333      h->root.u.def.section = h->weakdef->root.u.def.section;
3334      h->root.u.def.value = h->weakdef->root.u.def.value;
3335      return true;
3336    }
3337
3338  /* This is a reference to a symbol defined by a dynamic object which
3339     is not a function.  The Alpha, since it uses .got entries for all
3340     symbols even in regular objects, does not need the hackery of a
3341     .dynbss section and COPY dynamic relocations.  */
3342
3343  return true;
3344}
3345
3346/* Symbol versioning can create new symbols, and make our old symbols
3347   indirect to the new ones.  Consolidate the got and reloc information
3348   in these situations.  */
3349
3350static boolean
3351elf64_alpha_merge_ind_symbols (hi, dummy)
3352     struct alpha_elf_link_hash_entry *hi;
3353     PTR dummy ATTRIBUTE_UNUSED;
3354{
3355  struct alpha_elf_link_hash_entry *hs;
3356
3357  if (hi->root.root.type != bfd_link_hash_indirect)
3358    return true;
3359  hs = hi;
3360  do {
3361    hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
3362  } while (hs->root.root.type == bfd_link_hash_indirect);
3363
3364  /* Merge the flags.  Whee.  */
3365
3366  hs->flags |= hi->flags;
3367
3368  /* Merge the .got entries.  Cannibalize the old symbol's list in
3369     doing so, since we don't need it anymore.  */
3370
3371  if (hs->got_entries == NULL)
3372    hs->got_entries = hi->got_entries;
3373  else
3374    {
3375      struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
3376
3377      gsh = hs->got_entries;
3378      for (gi = hi->got_entries; gi ; gi = gin)
3379	{
3380	  gin = gi->next;
3381	  for (gs = gsh; gs ; gs = gs->next)
3382	    if (gi->gotobj == gs->gotobj
3383		&& gi->reloc_type == gs->reloc_type
3384		&& gi->addend == gs->addend)
3385	      {
3386		gi->use_count += gs->use_count;
3387	        goto got_found;
3388	      }
3389	  gi->next = hs->got_entries;
3390	  hs->got_entries = gi;
3391	got_found:;
3392	}
3393    }
3394  hi->got_entries = NULL;
3395
3396  /* And similar for the reloc entries.  */
3397
3398  if (hs->reloc_entries == NULL)
3399    hs->reloc_entries = hi->reloc_entries;
3400  else
3401    {
3402      struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
3403
3404      rsh = hs->reloc_entries;
3405      for (ri = hi->reloc_entries; ri ; ri = rin)
3406	{
3407	  rin = ri->next;
3408	  for (rs = rsh; rs ; rs = rs->next)
3409	    if (ri->rtype == rs->rtype && ri->srel == rs->srel)
3410	      {
3411		rs->count += ri->count;
3412		goto found_reloc;
3413	      }
3414	  ri->next = hs->reloc_entries;
3415	  hs->reloc_entries = ri;
3416	found_reloc:;
3417	}
3418    }
3419  hi->reloc_entries = NULL;
3420
3421  return true;
3422}
3423
3424/* Is it possible to merge two object file's .got tables?  */
3425
3426static boolean
3427elf64_alpha_can_merge_gots (a, b)
3428     bfd *a, *b;
3429{
3430  int total = alpha_elf_tdata (a)->total_got_size;
3431  bfd *bsub;
3432
3433  /* Trivial quick fallout test.  */
3434  if (total + alpha_elf_tdata (b)->total_got_size <= MAX_GOT_SIZE)
3435    return true;
3436
3437  /* By their nature, local .got entries cannot be merged.  */
3438  if ((total += alpha_elf_tdata (b)->local_got_size) > MAX_GOT_SIZE)
3439    return false;
3440
3441  /* Failing the common trivial comparison, we must effectively
3442     perform the merge.  Not actually performing the merge means that
3443     we don't have to store undo information in case we fail.  */
3444  for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
3445    {
3446      struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
3447      Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
3448      int i, n;
3449
3450      n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
3451      for (i = 0; i < n; ++i)
3452	{
3453	  struct alpha_elf_got_entry *ae, *be;
3454	  struct alpha_elf_link_hash_entry *h;
3455
3456	  h = hashes[i];
3457	  while (h->root.root.type == bfd_link_hash_indirect
3458	         || h->root.root.type == bfd_link_hash_warning)
3459	    h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3460
3461	  for (be = h->got_entries; be ; be = be->next)
3462	    {
3463	      if (be->use_count == 0)
3464	        continue;
3465	      if (be->gotobj != b)
3466	        continue;
3467
3468	      for (ae = h->got_entries; ae ; ae = ae->next)
3469	        if (ae->gotobj == a
3470		    && ae->reloc_type == be->reloc_type
3471		    && ae->addend == be->addend)
3472		  goto global_found;
3473
3474	      total += alpha_got_entry_size (be->reloc_type);
3475	      if (total > MAX_GOT_SIZE)
3476	        return false;
3477	    global_found:;
3478	    }
3479	}
3480    }
3481
3482  return true;
3483}
3484
3485/* Actually merge two .got tables.  */
3486
3487static void
3488elf64_alpha_merge_gots (a, b)
3489     bfd *a, *b;
3490{
3491  int total = alpha_elf_tdata (a)->total_got_size;
3492  bfd *bsub;
3493
3494  /* Remember local expansion.  */
3495  {
3496    int e = alpha_elf_tdata (b)->local_got_size;
3497    total += e;
3498    alpha_elf_tdata (a)->local_got_size += e;
3499  }
3500
3501  for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
3502    {
3503      struct alpha_elf_got_entry **local_got_entries;
3504      struct alpha_elf_link_hash_entry **hashes;
3505      Elf_Internal_Shdr *symtab_hdr;
3506      int i, n;
3507
3508      /* Let the local .got entries know they are part of a new subsegment.  */
3509      local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
3510      if (local_got_entries)
3511        {
3512	  n = elf_tdata (bsub)->symtab_hdr.sh_info;
3513	  for (i = 0; i < n; ++i)
3514	    {
3515	      struct alpha_elf_got_entry *ent;
3516	      for (ent = local_got_entries[i]; ent; ent = ent->next)
3517	        ent->gotobj = a;
3518	    }
3519        }
3520
3521      /* Merge the global .got entries.  */
3522      hashes = alpha_elf_sym_hashes (bsub);
3523      symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
3524
3525      n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
3526      for (i = 0; i < n; ++i)
3527        {
3528	  struct alpha_elf_got_entry *ae, *be, **pbe, **start;
3529	  struct alpha_elf_link_hash_entry *h;
3530
3531	  h = hashes[i];
3532	  while (h->root.root.type == bfd_link_hash_indirect
3533	         || h->root.root.type == bfd_link_hash_warning)
3534	    h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3535
3536	  start = &h->got_entries;
3537	  for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
3538	    {
3539	      if (be->use_count == 0)
3540	        {
3541		  *pbe = be->next;
3542		  continue;
3543	        }
3544	      if (be->gotobj != b)
3545	        continue;
3546
3547	      for (ae = *start; ae ; ae = ae->next)
3548	        if (ae->gotobj == a
3549		    && ae->reloc_type == be->reloc_type
3550		    && ae->addend == be->addend)
3551		  {
3552		    ae->flags |= be->flags;
3553		    ae->use_count += be->use_count;
3554		    *pbe = be->next;
3555		    goto global_found;
3556		  }
3557	      be->gotobj = a;
3558	      total += alpha_got_entry_size (be->reloc_type);
3559
3560	    global_found:;
3561	    }
3562        }
3563
3564      alpha_elf_tdata (bsub)->gotobj = a;
3565    }
3566  alpha_elf_tdata (a)->total_got_size = total;
3567
3568  /* Merge the two in_got chains.  */
3569  {
3570    bfd *next;
3571
3572    bsub = a;
3573    while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
3574      bsub = next;
3575
3576    alpha_elf_tdata (bsub)->in_got_link_next = b;
3577  }
3578}
3579
3580/* Calculate the offsets for the got entries.  */
3581
3582static boolean
3583elf64_alpha_calc_got_offsets_for_symbol (h, arg)
3584     struct alpha_elf_link_hash_entry *h;
3585     PTR arg ATTRIBUTE_UNUSED;
3586{
3587  struct alpha_elf_got_entry *gotent;
3588
3589  if (h->root.root.type == bfd_link_hash_warning)
3590    h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3591
3592  for (gotent = h->got_entries; gotent; gotent = gotent->next)
3593    if (gotent->use_count > 0)
3594      {
3595	bfd_size_type *plge
3596	  = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
3597
3598	gotent->got_offset = *plge;
3599	*plge += alpha_got_entry_size (gotent->reloc_type);
3600      }
3601
3602  return true;
3603}
3604
3605static void
3606elf64_alpha_calc_got_offsets (info)
3607     struct bfd_link_info *info;
3608{
3609  bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
3610
3611  /* First, zero out the .got sizes, as we may be recalculating the
3612     .got after optimizing it.  */
3613  for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3614    alpha_elf_tdata(i)->got->_raw_size = 0;
3615
3616  /* Next, fill in the offsets for all the global entries.  */
3617  alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3618				elf64_alpha_calc_got_offsets_for_symbol,
3619				NULL);
3620
3621  /* Finally, fill in the offsets for the local entries.  */
3622  for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3623    {
3624      bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
3625      bfd *j;
3626
3627      for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3628	{
3629	  struct alpha_elf_got_entry **local_got_entries, *gotent;
3630	  int k, n;
3631
3632	  local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3633	  if (!local_got_entries)
3634	    continue;
3635
3636	  for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3637	    for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
3638	      if (gotent->use_count > 0)
3639	        {
3640		  gotent->got_offset = got_offset;
3641		  got_offset += alpha_got_entry_size (gotent->reloc_type);
3642	        }
3643	}
3644
3645      alpha_elf_tdata(i)->got->_raw_size = got_offset;
3646      alpha_elf_tdata(i)->got->_cooked_size = got_offset;
3647    }
3648}
3649
3650/* Constructs the gots.  */
3651
3652static boolean
3653elf64_alpha_size_got_sections (info)
3654     struct bfd_link_info *info;
3655{
3656  bfd *i, *got_list, *cur_got_obj = NULL;
3657  int something_changed = 0;
3658
3659  got_list = alpha_elf_hash_table (info)->got_list;
3660
3661  /* On the first time through, pretend we have an existing got list
3662     consisting of all of the input files.  */
3663  if (got_list == NULL)
3664    {
3665      for (i = info->input_bfds; i ; i = i->link_next)
3666	{
3667	  bfd *this_got = alpha_elf_tdata (i)->gotobj;
3668	  if (this_got == NULL)
3669	    continue;
3670
3671	  /* We are assuming no merging has yet ocurred.  */
3672	  BFD_ASSERT (this_got == i);
3673
3674          if (alpha_elf_tdata (this_got)->total_got_size > MAX_GOT_SIZE)
3675	    {
3676	      /* Yikes! A single object file has too many entries.  */
3677	      (*_bfd_error_handler)
3678	        (_("%s: .got subsegment exceeds 64K (size %d)"),
3679	         bfd_archive_filename (i),
3680	         alpha_elf_tdata (this_got)->total_got_size);
3681	      return false;
3682	    }
3683
3684	  if (got_list == NULL)
3685	    got_list = this_got;
3686	  else
3687	    alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
3688	  cur_got_obj = this_got;
3689	}
3690
3691      /* Strange degenerate case of no got references.  */
3692      if (got_list == NULL)
3693	return true;
3694
3695      alpha_elf_hash_table (info)->got_list = got_list;
3696
3697      /* Force got offsets to be recalculated.  */
3698      something_changed = 1;
3699    }
3700
3701  cur_got_obj = got_list;
3702  i = alpha_elf_tdata(cur_got_obj)->got_link_next;
3703  while (i != NULL)
3704    {
3705      if (elf64_alpha_can_merge_gots (cur_got_obj, i))
3706	{
3707	  elf64_alpha_merge_gots (cur_got_obj, i);
3708	  i = alpha_elf_tdata(i)->got_link_next;
3709	  alpha_elf_tdata(cur_got_obj)->got_link_next = i;
3710	  something_changed = 1;
3711	}
3712      else
3713	{
3714	  cur_got_obj = i;
3715	  i = alpha_elf_tdata(i)->got_link_next;
3716	}
3717    }
3718
3719  /* Once the gots have been merged, fill in the got offsets for
3720     everything therein.  */
3721  if (1 || something_changed)
3722    elf64_alpha_calc_got_offsets (info);
3723
3724  return true;
3725}
3726
3727/* Called from relax_section to rebuild the PLT in light of
3728   potential changes in the function's status.  */
3729
3730static boolean
3731elf64_alpha_size_plt_section (info)
3732     struct bfd_link_info *info;
3733{
3734  asection *splt, *spltrel;
3735  unsigned long entries;
3736  bfd *dynobj;
3737
3738  dynobj = elf_hash_table(info)->dynobj;
3739  splt = bfd_get_section_by_name(dynobj, ".plt");
3740  if (splt == NULL)
3741    return true;
3742
3743  splt->_raw_size = 0;
3744
3745  alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3746				elf64_alpha_size_plt_section_1, splt);
3747
3748  splt->_cooked_size = splt->_raw_size;
3749
3750  /* Every plt entry requires a JMP_SLOT relocation.  */
3751  spltrel = bfd_get_section_by_name (dynobj, ".rela.plt");
3752  if (splt->_raw_size)
3753    entries = (splt->_raw_size - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3754  else
3755    entries = 0;
3756  spltrel->_raw_size = entries * sizeof (Elf64_External_Rela);
3757  spltrel->_cooked_size = spltrel->_raw_size;
3758
3759  return true;
3760}
3761
3762static boolean
3763elf64_alpha_size_plt_section_1 (h, data)
3764     struct alpha_elf_link_hash_entry *h;
3765     PTR data;
3766{
3767  asection *splt = (asection *) data;
3768  struct alpha_elf_got_entry *gotent;
3769
3770  /* If we didn't need an entry before, we still don't.  */
3771  if (!(h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT))
3772    return true;
3773
3774  /* There must still be a LITERAL got entry for the function.  */
3775  for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3776    if (gotent->reloc_type == R_ALPHA_LITERAL
3777	&& gotent->use_count > 0)
3778      break;
3779
3780  /* If there is, reset the PLT offset.  If not, there's no longer
3781     a need for the PLT entry.  */
3782  if (gotent)
3783    {
3784      if (splt->_raw_size == 0)
3785	splt->_raw_size = PLT_HEADER_SIZE;
3786      h->root.plt.offset = splt->_raw_size;
3787      splt->_raw_size += PLT_ENTRY_SIZE;
3788    }
3789  else
3790    {
3791      h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3792      h->root.plt.offset = -1;
3793    }
3794
3795  return true;
3796}
3797
3798static boolean
3799elf64_alpha_always_size_sections (output_bfd, info)
3800     bfd *output_bfd ATTRIBUTE_UNUSED;
3801     struct bfd_link_info *info;
3802{
3803  bfd *i;
3804
3805  if (info->relocateable)
3806    return true;
3807
3808  /* First, take care of the indirect symbols created by versioning.  */
3809  alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3810				elf64_alpha_merge_ind_symbols,
3811				NULL);
3812
3813  if (!elf64_alpha_size_got_sections (info))
3814    return false;
3815
3816  /* Allocate space for all of the .got subsections.  */
3817  i = alpha_elf_hash_table (info)->got_list;
3818  for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
3819    {
3820      asection *s = alpha_elf_tdata(i)->got;
3821      if (s->_raw_size > 0)
3822	{
3823	  s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
3824	  if (s->contents == NULL)
3825	    return false;
3826	}
3827    }
3828
3829  return true;
3830}
3831
3832/* The number of dynamic relocations required by a static relocation.  */
3833
3834static int
3835alpha_dynamic_entries_for_reloc (r_type, dynamic, shared)
3836     int r_type, dynamic, shared;
3837{
3838  switch (r_type)
3839    {
3840    /* May appear in GOT entries.  */
3841    case R_ALPHA_TLSGD:
3842      return (dynamic ? 2 : shared ? 1 : 0);
3843    case R_ALPHA_TLSLDM:
3844      return shared;
3845    case R_ALPHA_LITERAL:
3846      return dynamic || shared;
3847    case R_ALPHA_GOTDTPREL:
3848    case R_ALPHA_GOTTPREL:
3849      return dynamic;
3850
3851    /* May appear in data sections.  */
3852    case R_ALPHA_REFLONG:
3853    case R_ALPHA_REFQUAD:
3854      return dynamic || shared;
3855    case R_ALPHA_SREL64:
3856    case R_ALPHA_TPREL64:
3857      return dynamic;
3858
3859    /* Everything else is illegal.  We'll issue an error during
3860       relocate_section.  */
3861    default:
3862      return 0;
3863    }
3864}
3865
3866/* Work out the sizes of the dynamic relocation entries.  */
3867
3868static boolean
3869elf64_alpha_calc_dynrel_sizes (h, info)
3870     struct alpha_elf_link_hash_entry *h;
3871     struct bfd_link_info *info;
3872{
3873  boolean dynamic;
3874  struct alpha_elf_reloc_entry *relent;
3875  unsigned long entries;
3876
3877  if (h->root.root.type == bfd_link_hash_warning)
3878    h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3879
3880  /* If the symbol was defined as a common symbol in a regular object
3881     file, and there was no definition in any dynamic object, then the
3882     linker will have allocated space for the symbol in a common
3883     section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
3884     set.  This is done for dynamic symbols in
3885     elf_adjust_dynamic_symbol but this is not done for non-dynamic
3886     symbols, somehow.  */
3887  if (((h->root.elf_link_hash_flags
3888       & (ELF_LINK_HASH_DEF_REGULAR
3889	  | ELF_LINK_HASH_REF_REGULAR
3890	  | ELF_LINK_HASH_DEF_DYNAMIC))
3891       == ELF_LINK_HASH_REF_REGULAR)
3892      && (h->root.root.type == bfd_link_hash_defined
3893	  || h->root.root.type == bfd_link_hash_defweak)
3894      && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
3895    h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3896
3897  /* If the symbol is dynamic, we'll need all the relocations in their
3898     natural form.  If this is a shared object, and it has been forced
3899     local, we'll need the same number of RELATIVE relocations.  */
3900
3901  dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
3902
3903  for (relent = h->reloc_entries; relent; relent = relent->next)
3904    {
3905      entries = alpha_dynamic_entries_for_reloc (relent->rtype, dynamic,
3906						 info->shared);
3907      if (entries)
3908	{
3909	  relent->srel->_raw_size +=
3910	    entries * sizeof (Elf64_External_Rela) * relent->count;
3911	  if (relent->reltext)
3912	    info->flags |= DT_TEXTREL;
3913	}
3914    }
3915
3916  return true;
3917}
3918
3919/* Set the sizes of the dynamic relocation sections.  */
3920
3921static boolean
3922elf64_alpha_size_rela_got_section (info)
3923     struct bfd_link_info *info;
3924{
3925  unsigned long entries;
3926  bfd *i, *dynobj;
3927  asection *srel;
3928
3929  /* Shared libraries often require RELATIVE relocs, and some relocs
3930     require attention for the main application as well.  */
3931
3932  entries = 0;
3933  for (i = alpha_elf_hash_table(info)->got_list;
3934       i ; i = alpha_elf_tdata(i)->got_link_next)
3935    {
3936      bfd *j;
3937
3938      for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3939	{
3940	  struct alpha_elf_got_entry **local_got_entries, *gotent;
3941	  int k, n;
3942
3943	  local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3944	  if (!local_got_entries)
3945	    continue;
3946
3947	  for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3948	    for (gotent = local_got_entries[k];
3949		 gotent ; gotent = gotent->next)
3950	      if (gotent->use_count > 0)
3951		entries += (alpha_dynamic_entries_for_reloc
3952			    (gotent->reloc_type, 0, info->shared));
3953	}
3954    }
3955
3956  dynobj = elf_hash_table(info)->dynobj;
3957  srel = bfd_get_section_by_name (dynobj, ".rela.got");
3958  if (!srel)
3959    {
3960      BFD_ASSERT (entries == 0);
3961      return true;
3962    }
3963  srel->_raw_size = sizeof (Elf64_External_Rela) * entries;
3964
3965  /* Now do the non-local symbols.  */
3966  alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3967				elf64_alpha_size_rela_got_1, info);
3968
3969  srel->_cooked_size = srel->_raw_size;
3970
3971  return true;
3972}
3973
3974/* Subroutine of elf64_alpha_size_rela_got_section for doing the
3975   global symbols.  */
3976
3977static boolean
3978elf64_alpha_size_rela_got_1 (h, info)
3979     struct alpha_elf_link_hash_entry *h;
3980     struct bfd_link_info *info;
3981{
3982  boolean dynamic;
3983  struct alpha_elf_got_entry *gotent;
3984  unsigned long entries;
3985
3986  if (h->root.root.type == bfd_link_hash_warning)
3987    h = (struct alpha_elf_link_hash_entry *) h->root.root.u.i.link;
3988
3989  /* If the symbol is dynamic, we'll need all the relocations in their
3990     natural form.  If this is a shared object, and it has been forced
3991     local, we'll need the same number of RELATIVE relocations.  */
3992
3993  dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
3994
3995  entries = 0;
3996  for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3997    if (gotent->use_count > 0)
3998      entries += alpha_dynamic_entries_for_reloc (gotent->reloc_type,
3999						  dynamic, info->shared);
4000
4001  /* If we are using a .plt entry, subtract one, as the first
4002     reference uses a .rela.plt entry instead.  */
4003  if (h->root.plt.offset != MINUS_ONE)
4004    entries--;
4005
4006  if (entries > 0)
4007    {
4008      bfd *dynobj = elf_hash_table(info)->dynobj;
4009      asection *srel = bfd_get_section_by_name (dynobj, ".rela.got");
4010      BFD_ASSERT (srel != NULL);
4011      srel->_raw_size += sizeof (Elf64_External_Rela) * entries;
4012    }
4013
4014  return true;
4015}
4016
4017/* Set the sizes of the dynamic sections.  */
4018
4019static boolean
4020elf64_alpha_size_dynamic_sections (output_bfd, info)
4021     bfd *output_bfd ATTRIBUTE_UNUSED;
4022     struct bfd_link_info *info;
4023{
4024  bfd *dynobj;
4025  asection *s;
4026  boolean relplt;
4027
4028  dynobj = elf_hash_table(info)->dynobj;
4029  BFD_ASSERT(dynobj != NULL);
4030
4031  if (elf_hash_table (info)->dynamic_sections_created)
4032    {
4033      /* Set the contents of the .interp section to the interpreter.  */
4034      if (!info->shared)
4035	{
4036	  s = bfd_get_section_by_name (dynobj, ".interp");
4037	  BFD_ASSERT (s != NULL);
4038	  s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4039	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4040	}
4041
4042      /* Now that we've seen all of the input files, we can decide which
4043	 symbols need dynamic relocation entries and which don't.  We've
4044	 collected information in check_relocs that we can now apply to
4045	 size the dynamic relocation sections.  */
4046      alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
4047				    elf64_alpha_calc_dynrel_sizes, info);
4048
4049      elf64_alpha_size_rela_got_section (info);
4050    }
4051  /* else we're not dynamic and by definition we don't need such things.  */
4052
4053  /* The check_relocs and adjust_dynamic_symbol entry points have
4054     determined the sizes of the various dynamic sections.  Allocate
4055     memory for them.  */
4056  relplt = false;
4057  for (s = dynobj->sections; s != NULL; s = s->next)
4058    {
4059      const char *name;
4060      boolean strip;
4061
4062      if (!(s->flags & SEC_LINKER_CREATED))
4063	continue;
4064
4065      /* It's OK to base decisions on the section name, because none
4066	 of the dynobj section names depend upon the input files.  */
4067      name = bfd_get_section_name (dynobj, s);
4068
4069      /* If we don't need this section, strip it from the output file.
4070	 This is to handle .rela.bss and .rela.plt.  We must create it
4071	 in create_dynamic_sections, because it must be created before
4072	 the linker maps input sections to output sections.  The
4073	 linker does that before adjust_dynamic_symbol is called, and
4074	 it is that function which decides whether anything needs to
4075	 go into these sections.  */
4076
4077      strip = false;
4078
4079      if (strncmp (name, ".rela", 5) == 0)
4080	{
4081	  strip = (s->_raw_size == 0);
4082
4083	  if (!strip)
4084	    {
4085	      if (strcmp(name, ".rela.plt") == 0)
4086		relplt = true;
4087
4088	      /* We use the reloc_count field as a counter if we need
4089		 to copy relocs into the output file.  */
4090	      s->reloc_count = 0;
4091	    }
4092	}
4093      else if (strcmp (name, ".plt") != 0)
4094	{
4095	  /* It's not one of our dynamic sections, so don't allocate space.  */
4096	  continue;
4097	}
4098
4099      if (strip)
4100	_bfd_strip_section_from_output (info, s);
4101      else
4102	{
4103	  /* Allocate memory for the section contents.  */
4104	  s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4105	  if (s->contents == NULL && s->_raw_size != 0)
4106	    return false;
4107	}
4108    }
4109
4110  if (elf_hash_table (info)->dynamic_sections_created)
4111    {
4112      /* Add some entries to the .dynamic section.  We fill in the
4113	 values later, in elf64_alpha_finish_dynamic_sections, but we
4114	 must add the entries now so that we get the correct size for
4115	 the .dynamic section.  The DT_DEBUG entry is filled in by the
4116	 dynamic linker and used by the debugger.  */
4117#define add_dynamic_entry(TAG, VAL) \
4118  bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4119
4120      if (!info->shared)
4121	{
4122	  if (!add_dynamic_entry (DT_DEBUG, 0))
4123	    return false;
4124	}
4125
4126      if (relplt)
4127	{
4128	  if (!add_dynamic_entry (DT_PLTGOT, 0)
4129	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
4130	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4131	      || !add_dynamic_entry (DT_JMPREL, 0))
4132	    return false;
4133	}
4134
4135      if (!add_dynamic_entry (DT_RELA, 0)
4136	  || !add_dynamic_entry (DT_RELASZ, 0)
4137	  || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
4138	return false;
4139
4140      if (info->flags & DF_TEXTREL)
4141	{
4142	  if (!add_dynamic_entry (DT_TEXTREL, 0))
4143	    return false;
4144	}
4145    }
4146#undef add_dynamic_entry
4147
4148  return true;
4149}
4150
4151/* Relocate an Alpha ELF section for a relocatable link.
4152
4153   We don't have to change anything unless the reloc is against a section
4154   symbol, in which case we have to adjust according to where the section
4155   symbol winds up in the output section.  */
4156
4157static boolean
4158elf64_alpha_relocate_section_r (output_bfd, info, input_bfd, input_section,
4159			        contents, relocs, local_syms, local_sections)
4160     bfd *output_bfd ATTRIBUTE_UNUSED;
4161     struct bfd_link_info *info ATTRIBUTE_UNUSED;
4162     bfd *input_bfd;
4163     asection *input_section;
4164     bfd_byte *contents ATTRIBUTE_UNUSED;
4165     Elf_Internal_Rela *relocs;
4166     Elf_Internal_Sym *local_syms;
4167     asection **local_sections;
4168{
4169  unsigned long symtab_hdr_sh_info;
4170  Elf_Internal_Rela *rel;
4171  Elf_Internal_Rela *relend;
4172  boolean ret_val = true;
4173
4174  symtab_hdr_sh_info = elf_tdata (input_bfd)->symtab_hdr.sh_info;
4175
4176  relend = relocs + input_section->reloc_count;
4177  for (rel = relocs; rel < relend; rel++)
4178    {
4179      unsigned long r_symndx;
4180      Elf_Internal_Sym *sym;
4181      asection *sec;
4182      unsigned long r_type;
4183
4184      r_type = ELF64_R_TYPE(rel->r_info);
4185      if (r_type >= R_ALPHA_max)
4186	{
4187	  (*_bfd_error_handler)
4188	    (_("%s: unknown relocation type %d"),
4189	     bfd_archive_filename (input_bfd), (int)r_type);
4190	  bfd_set_error (bfd_error_bad_value);
4191	  ret_val = false;
4192	  continue;
4193	}
4194
4195      r_symndx = ELF64_R_SYM(rel->r_info);
4196
4197      /* The symbol associated with GPDISP and LITUSE is
4198	 immaterial.  Only the addend is significant.  */
4199      if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
4200	continue;
4201
4202      if (r_symndx < symtab_hdr_sh_info)
4203	{
4204	  sym = local_syms + r_symndx;
4205	  if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
4206	    {
4207	      sec = local_sections[r_symndx];
4208	      rel->r_addend += sec->output_offset + sym->st_value;
4209	    }
4210	}
4211    }
4212
4213  return ret_val;
4214}
4215
4216/* Relocate an Alpha ELF section.  */
4217
4218static boolean
4219elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
4220			      contents, relocs, local_syms, local_sections)
4221     bfd *output_bfd;
4222     struct bfd_link_info *info;
4223     bfd *input_bfd;
4224     asection *input_section;
4225     bfd_byte *contents;
4226     Elf_Internal_Rela *relocs;
4227     Elf_Internal_Sym *local_syms;
4228     asection **local_sections;
4229{
4230  Elf_Internal_Shdr *symtab_hdr;
4231  Elf_Internal_Rela *rel;
4232  Elf_Internal_Rela *relend;
4233  struct elf_link_tls_segment *tls_segment;
4234  asection *sgot, *srel, *srelgot;
4235  bfd *dynobj, *gotobj;
4236  bfd_vma gp, tp_base, dtp_base;
4237  struct alpha_elf_got_entry **local_got_entries;
4238  boolean ret_val;
4239  const char *section_name;
4240
4241  /* Handle relocatable links with a smaller loop.  */
4242  if (info->relocateable)
4243    return elf64_alpha_relocate_section_r (output_bfd, info, input_bfd,
4244					   input_section, contents, relocs,
4245					   local_syms, local_sections);
4246
4247  /* This is a final link.  */
4248
4249  ret_val = true;
4250
4251  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4252
4253  dynobj = elf_hash_table (info)->dynobj;
4254  if (dynobj)
4255    srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4256  else
4257    srelgot = NULL;
4258
4259  section_name = (bfd_elf_string_from_elf_section
4260		  (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
4261		   elf_section_data(input_section)->rel_hdr.sh_name));
4262  BFD_ASSERT(section_name != NULL);
4263  srel = bfd_get_section_by_name (dynobj, section_name);
4264
4265  /* Find the gp value for this input bfd.  */
4266  gotobj = alpha_elf_tdata (input_bfd)->gotobj;
4267  if (gotobj)
4268    {
4269      sgot = alpha_elf_tdata (gotobj)->got;
4270      gp = _bfd_get_gp_value (gotobj);
4271      if (gp == 0)
4272	{
4273	  gp = (sgot->output_section->vma
4274		+ sgot->output_offset
4275		+ 0x8000);
4276	  _bfd_set_gp_value (gotobj, gp);
4277	}
4278    }
4279  else
4280    {
4281      sgot = NULL;
4282      gp = 0;
4283    }
4284
4285  local_got_entries = alpha_elf_tdata(input_bfd)->local_got_entries;
4286
4287  tls_segment = elf_hash_table (info)->tls_segment;
4288  if (tls_segment)
4289    {
4290      dtp_base = alpha_get_dtprel_base (tls_segment);
4291      tp_base = alpha_get_tprel_base (tls_segment);
4292    }
4293  else
4294    dtp_base = tp_base = 0;
4295
4296  relend = relocs + input_section->reloc_count;
4297  for (rel = relocs; rel < relend; rel++)
4298    {
4299      struct alpha_elf_link_hash_entry *h = NULL;
4300      struct alpha_elf_got_entry *gotent;
4301      bfd_reloc_status_type r;
4302      reloc_howto_type *howto;
4303      unsigned long r_symndx;
4304      Elf_Internal_Sym *sym = NULL;
4305      asection *sec = NULL;
4306      bfd_vma value;
4307      bfd_vma addend;
4308      boolean dynamic_symbol_p;
4309      boolean undef_weak_ref = false;
4310      unsigned long r_type;
4311
4312      r_type = ELF64_R_TYPE(rel->r_info);
4313      if (r_type >= R_ALPHA_max)
4314	{
4315	  (*_bfd_error_handler)
4316	    (_("%s: unknown relocation type %d"),
4317	     bfd_archive_filename (input_bfd), (int)r_type);
4318	  bfd_set_error (bfd_error_bad_value);
4319	  ret_val = false;
4320	  continue;
4321	}
4322
4323      howto = elf64_alpha_howto_table + r_type;
4324      r_symndx = ELF64_R_SYM(rel->r_info);
4325
4326      if (r_symndx < symtab_hdr->sh_info)
4327	{
4328	  sym = local_syms + r_symndx;
4329	  sec = local_sections[r_symndx];
4330	  value = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4331
4332	  if (local_got_entries)
4333	    gotent = local_got_entries[r_symndx];
4334	  else
4335	    gotent = NULL;
4336
4337	  /* Need to adjust local GOT entries' addends for SEC_MERGE
4338	     unless it has been done already.  */
4339	  if ((sec->flags & SEC_MERGE)
4340	      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
4341	      && (elf_section_data (sec)->sec_info_type
4342		  == ELF_INFO_TYPE_MERGE)
4343	      && gotent
4344	      && !gotent->reloc_xlated)
4345	    {
4346	      struct alpha_elf_got_entry *ent;
4347	      asection *msec;
4348
4349	      for (ent = gotent; ent; ent = ent->next)
4350		{
4351		  ent->reloc_xlated = 1;
4352		  if (ent->use_count == 0)
4353		    continue;
4354		  msec = sec;
4355		  ent->addend =
4356		    _bfd_merged_section_offset (output_bfd, &msec,
4357						elf_section_data (sec)->
4358						  sec_info,
4359						sym->st_value + ent->addend,
4360						(bfd_vma) 0);
4361		  ent->addend -= sym->st_value;
4362		  ent->addend += msec->output_section->vma
4363				 + msec->output_offset
4364				 - sec->output_section->vma
4365				 - sec->output_offset;
4366		}
4367	    }
4368
4369	  dynamic_symbol_p = false;
4370	}
4371      else
4372	{
4373	  h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
4374
4375	  while (h->root.root.type == bfd_link_hash_indirect
4376		 || h->root.root.type == bfd_link_hash_warning)
4377	    h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
4378
4379	  value = 0;
4380	  if (h->root.root.type == bfd_link_hash_defined
4381	      || h->root.root.type == bfd_link_hash_defweak)
4382	    {
4383	      sec = h->root.root.u.def.section;
4384
4385	      /* Detect the cases that sym_sec->output_section is
4386		 expected to be NULL -- all cases in which the symbol
4387		 is defined in another shared module.  This includes
4388		 PLT relocs for which we've created a PLT entry and
4389		 other relocs for which we're prepared to create
4390		 dynamic relocations.  */
4391	      /* ??? Just accept it NULL and continue.  */
4392
4393	      if (sec->output_section != NULL)
4394		value = (h->root.root.u.def.value
4395			 + sec->output_section->vma
4396			      + sec->output_offset);
4397	    }
4398	  else if (h->root.root.type == bfd_link_hash_undefweak)
4399	    undef_weak_ref = true;
4400	  else if (info->shared
4401		   && (!info->symbolic || info->allow_shlib_undefined)
4402		   && !info->no_undefined
4403		   && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4404	    ;
4405	  else
4406	    {
4407	      if (!((*info->callbacks->undefined_symbol)
4408		    (info, h->root.root.root.string, input_bfd,
4409		     input_section, rel->r_offset,
4410		     (!info->shared || info->no_undefined
4411		      || ELF_ST_VISIBILITY (h->root.other)))))
4412		return false;
4413	      ret_val = false;
4414	      continue;
4415	    }
4416
4417          dynamic_symbol_p = alpha_elf_dynamic_symbol_p (&h->root, info);
4418	  gotent = h->got_entries;
4419	}
4420
4421      addend = rel->r_addend;
4422      value += addend;
4423
4424      /* Search for the proper got entry.  */
4425      for (; gotent ; gotent = gotent->next)
4426	if (gotent->gotobj == gotobj
4427	    && gotent->reloc_type == r_type
4428	    && gotent->addend == addend)
4429	  break;
4430
4431      switch (r_type)
4432	{
4433	case R_ALPHA_GPDISP:
4434	  {
4435	    bfd_byte *p_ldah, *p_lda;
4436
4437	    BFD_ASSERT(gp != 0);
4438
4439	    value = (input_section->output_section->vma
4440		     + input_section->output_offset
4441		     + rel->r_offset);
4442
4443	    p_ldah = contents + rel->r_offset;
4444	    p_lda = p_ldah + rel->r_addend;
4445
4446	    r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - value,
4447					     p_ldah, p_lda);
4448	  }
4449	  break;
4450
4451	case R_ALPHA_LITERAL:
4452	  BFD_ASSERT(sgot != NULL);
4453	  BFD_ASSERT(gp != 0);
4454	  BFD_ASSERT(gotent != NULL);
4455	  BFD_ASSERT(gotent->use_count >= 1);
4456
4457	  if (!gotent->reloc_done)
4458	    {
4459	      gotent->reloc_done = 1;
4460
4461	      bfd_put_64 (output_bfd, value,
4462			  sgot->contents + gotent->got_offset);
4463
4464	      /* If the symbol has been forced local, output a
4465		 RELATIVE reloc, otherwise it will be handled in
4466		 finish_dynamic_symbol.  */
4467	      if (info->shared && !dynamic_symbol_p)
4468		{
4469		  Elf_Internal_Rela outrel;
4470
4471		  BFD_ASSERT(srelgot != NULL);
4472
4473		  outrel.r_offset = (sgot->output_section->vma
4474				     + sgot->output_offset
4475				     + gotent->got_offset);
4476		  outrel.r_info = ELF64_R_INFO (0, R_ALPHA_RELATIVE);
4477		  outrel.r_addend = value;
4478
4479		  bfd_elf64_swap_reloca_out (output_bfd, &outrel,
4480					     ((Elf64_External_Rela *)
4481					      srelgot->contents)
4482					     + srelgot->reloc_count++);
4483		  BFD_ASSERT (sizeof (Elf64_External_Rela)
4484			      * srelgot->reloc_count
4485			      <= srelgot->_cooked_size);
4486		}
4487	    }
4488
4489	  value = (sgot->output_section->vma
4490		   + sgot->output_offset
4491		   + gotent->got_offset);
4492	  value -= gp;
4493	  goto default_reloc;
4494
4495	case R_ALPHA_GPREL16:
4496	case R_ALPHA_GPREL32:
4497	case R_ALPHA_GPRELLOW:
4498	  if (dynamic_symbol_p)
4499            {
4500              (*_bfd_error_handler)
4501                (_("%s: gp-relative relocation against dynamic symbol %s"),
4502                 bfd_archive_filename (input_bfd), h->root.root.root.string);
4503              ret_val = false;
4504            }
4505	  BFD_ASSERT(gp != 0);
4506	  value -= gp;
4507	  goto default_reloc;
4508
4509	case R_ALPHA_GPRELHIGH:
4510	  if (dynamic_symbol_p)
4511            {
4512              (*_bfd_error_handler)
4513                (_("%s: gp-relative relocation against dynamic symbol %s"),
4514                 bfd_archive_filename (input_bfd), h->root.root.root.string);
4515              ret_val = false;
4516            }
4517	  BFD_ASSERT(gp != 0);
4518	  value -= gp;
4519	  value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4520	  goto default_reloc;
4521
4522	case R_ALPHA_HINT:
4523	  /* A call to a dynamic symbol is definitely out of range of
4524	     the 16-bit displacement.  Don't bother writing anything.  */
4525	  if (dynamic_symbol_p)
4526	    {
4527	      r = bfd_reloc_ok;
4528	      break;
4529	    }
4530	  /* The regular PC-relative stuff measures from the start of
4531	     the instruction rather than the end.  */
4532	  value -= 4;
4533	  goto default_reloc;
4534
4535	case R_ALPHA_BRADDR:
4536	  if (dynamic_symbol_p)
4537            {
4538              (*_bfd_error_handler)
4539                (_("%s: pc-relative relocation against dynamic symbol %s"),
4540                 bfd_archive_filename (input_bfd), h->root.root.root.string);
4541              ret_val = false;
4542            }
4543	  /* The regular PC-relative stuff measures from the start of
4544	     the instruction rather than the end.  */
4545	  value -= 4;
4546	  goto default_reloc;
4547
4548	case R_ALPHA_BRSGP:
4549	  {
4550	    int other;
4551	    const char *name;
4552
4553	    /* The regular PC-relative stuff measures from the start of
4554	       the instruction rather than the end.  */
4555	    value -= 4;
4556
4557	    /* The source and destination gp must be the same.  Note that
4558	       the source will always have an assigned gp, since we forced
4559	       one in check_relocs, but that the destination may not, as
4560	       it might not have had any relocations at all.  Also take
4561	       care not to crash if H is an undefined symbol.  */
4562	    if (h != NULL && sec != NULL
4563		&& alpha_elf_tdata (sec->owner)->gotobj
4564		&& gotobj != alpha_elf_tdata (sec->owner)->gotobj)
4565	      {
4566		(*_bfd_error_handler)
4567		  (_("%s: change in gp: BRSGP %s"),
4568		   bfd_archive_filename (input_bfd), h->root.root.root.string);
4569		ret_val = false;
4570	      }
4571
4572	    /* The symbol should be marked either NOPV or STD_GPLOAD.  */
4573	    if (h != NULL)
4574	      other = h->root.other;
4575	    else
4576	      other = sym->st_other;
4577	    switch (other & STO_ALPHA_STD_GPLOAD)
4578	      {
4579	      case STO_ALPHA_NOPV:
4580	        break;
4581	      case STO_ALPHA_STD_GPLOAD:
4582		value += 8;
4583		break;
4584	      default:
4585		if (h != NULL)
4586		  name = h->root.root.root.string;
4587		else
4588		  {
4589		    name = (bfd_elf_string_from_elf_section
4590			    (input_bfd, symtab_hdr->sh_link, sym->st_name));
4591		    if (name == NULL)
4592		      name = _("<unknown>");
4593		    else if (name[0] == 0)
4594		      name = bfd_section_name (input_bfd, sec);
4595		  }
4596		(*_bfd_error_handler)
4597		  (_("%s: !samegp reloc against symbol without .prologue: %s"),
4598		   bfd_archive_filename (input_bfd), name);
4599		ret_val = false;
4600		break;
4601	      }
4602
4603	    goto default_reloc;
4604	  }
4605
4606	case R_ALPHA_REFLONG:
4607	case R_ALPHA_REFQUAD:
4608	case R_ALPHA_DTPREL64:
4609	case R_ALPHA_TPREL64:
4610	  {
4611	    Elf_Internal_Rela outrel;
4612
4613	    /* Careful here to remember RELATIVE relocations for global
4614	       variables for symbolic shared objects.  */
4615
4616	    if (dynamic_symbol_p)
4617	      {
4618		BFD_ASSERT(h->root.dynindx != -1);
4619		outrel.r_info = ELF64_R_INFO (h->root.dynindx, r_type);
4620		outrel.r_addend = addend;
4621		addend = 0, value = 0;
4622	      }
4623	    else if (r_type == R_ALPHA_DTPREL64)
4624	      {
4625		BFD_ASSERT(tls_segment != NULL);
4626		value -= dtp_base;
4627		goto default_reloc;
4628	      }
4629	    else if (r_type == R_ALPHA_TPREL64)
4630	      {
4631		BFD_ASSERT(tls_segment != NULL);
4632		value -= dtp_base;
4633		goto default_reloc;
4634	      }
4635	    else if (info->shared
4636		     && r_symndx != 0
4637		     && (input_section->flags & SEC_ALLOC))
4638	      {
4639		if (r_type == R_ALPHA_REFLONG)
4640		  {
4641		    (*_bfd_error_handler)
4642		      (_("%s: unhandled dynamic relocation against %s"),
4643		       bfd_archive_filename (input_bfd),
4644		       h->root.root.root.string);
4645		    ret_val = false;
4646		  }
4647		outrel.r_info = ELF64_R_INFO (0, R_ALPHA_RELATIVE);
4648		outrel.r_addend = value;
4649	      }
4650	    else
4651	      goto default_reloc;
4652
4653	    BFD_ASSERT(srel != NULL);
4654
4655	    outrel.r_offset =
4656	      _bfd_elf_section_offset (output_bfd, info, input_section,
4657				       rel->r_offset);
4658	    if ((outrel.r_offset | 1) != (bfd_vma) -1)
4659	      outrel.r_offset += (input_section->output_section->vma
4660				  + input_section->output_offset);
4661	    else
4662	      memset (&outrel, 0, sizeof outrel);
4663
4664	    bfd_elf64_swap_reloca_out (output_bfd, &outrel,
4665				       ((Elf64_External_Rela *)
4666					srel->contents)
4667				       + srel->reloc_count++);
4668	    BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
4669			<= srel->_cooked_size);
4670	  }
4671	  goto default_reloc;
4672
4673	case R_ALPHA_SREL16:
4674	case R_ALPHA_SREL32:
4675	case R_ALPHA_SREL64:
4676	  if (dynamic_symbol_p)
4677            {
4678              (*_bfd_error_handler)
4679                (_("%s: pc-relative relocation against dynamic symbol %s"),
4680                 bfd_archive_filename (input_bfd), h->root.root.root.string);
4681              ret_val = false;
4682            }
4683
4684	  /* ??? .eh_frame references to discarded sections will be smashed
4685	     to relocations against SHN_UNDEF.  The .eh_frame format allows
4686	     NULL to be encoded as 0 in any format, so this works here.  */
4687	  if (r_symndx == 0)
4688	    howto = (elf64_alpha_howto_table
4689		     + (r_type - R_ALPHA_SREL32 + R_ALPHA_REFLONG));
4690	  goto default_reloc;
4691
4692	case R_ALPHA_TLSLDM:
4693	  /* Ignore the symbol for the relocation.  The result is always
4694	     the current module.  */
4695	  dynamic_symbol_p = 0;
4696	  /* FALLTHRU */
4697
4698	case R_ALPHA_TLSGD:
4699	  if (!gotent->reloc_done)
4700	    {
4701	      gotent->reloc_done = 1;
4702
4703	      /* Note that the module index for the main program is 1.  */
4704	      bfd_put_64 (output_bfd, !info->shared && !dynamic_symbol_p,
4705			  sgot->contents + gotent->got_offset);
4706
4707	      /* If the symbol has been forced local, output a
4708		 DTPMOD64 reloc, otherwise it will be handled in
4709		 finish_dynamic_symbol.  */
4710	      if (info->shared && !dynamic_symbol_p)
4711		{
4712		  Elf_Internal_Rela outrel;
4713
4714		  BFD_ASSERT(srelgot != NULL);
4715
4716		  outrel.r_offset = (sgot->output_section->vma
4717				     + sgot->output_offset
4718				     + gotent->got_offset);
4719		  /* ??? Proper dynindx here.  */
4720		  outrel.r_info = ELF64_R_INFO (0, R_ALPHA_DTPMOD64);
4721		  outrel.r_addend = 0;
4722
4723		  bfd_elf64_swap_reloca_out (output_bfd, &outrel,
4724					     ((Elf64_External_Rela *)
4725					      srelgot->contents)
4726					     + srelgot->reloc_count++);
4727		  BFD_ASSERT (sizeof (Elf64_External_Rela)
4728			      * srelgot->reloc_count
4729			      <= srelgot->_cooked_size);
4730		}
4731
4732	      if (dynamic_symbol_p || r_type == R_ALPHA_TLSLDM)
4733		value = 0;
4734	      else
4735		{
4736		  BFD_ASSERT(tls_segment != NULL);
4737	          value -= dtp_base;
4738		}
4739	      bfd_put_64 (output_bfd, value,
4740			  sgot->contents + gotent->got_offset + 8);
4741	    }
4742
4743	  value = (sgot->output_section->vma
4744		   + sgot->output_offset
4745		   + gotent->got_offset);
4746	  value -= gp;
4747	  goto default_reloc;
4748
4749	case R_ALPHA_DTPRELHI:
4750	case R_ALPHA_DTPRELLO:
4751	case R_ALPHA_DTPREL16:
4752	  if (dynamic_symbol_p)
4753            {
4754              (*_bfd_error_handler)
4755                (_("%s: dtp-relative relocation against dynamic symbol %s"),
4756                 bfd_archive_filename (input_bfd), h->root.root.root.string);
4757              ret_val = false;
4758            }
4759	  BFD_ASSERT(tls_segment != NULL);
4760	  value -= dtp_base;
4761	  if (r_type == R_ALPHA_DTPRELHI)
4762	    value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4763	  goto default_reloc;
4764
4765	case R_ALPHA_TPRELHI:
4766	case R_ALPHA_TPRELLO:
4767	case R_ALPHA_TPREL16:
4768	  if (info->shared)
4769	    {
4770	      (*_bfd_error_handler)
4771		(_("%s: TLS local exec code cannot be linked into shared objects"),
4772		bfd_archive_filename (input_bfd));
4773              ret_val = false;
4774	    }
4775	  else if (dynamic_symbol_p)
4776            {
4777              (*_bfd_error_handler)
4778                (_("%s: tp-relative relocation against dynamic symbol %s"),
4779                 bfd_archive_filename (input_bfd), h->root.root.root.string);
4780              ret_val = false;
4781            }
4782	  BFD_ASSERT(tls_segment != NULL);
4783	  value -= tp_base;
4784	  if (r_type == R_ALPHA_TPRELHI)
4785	    value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
4786	  goto default_reloc;
4787
4788	case R_ALPHA_GOTDTPREL:
4789	case R_ALPHA_GOTTPREL:
4790	  BFD_ASSERT(sgot != NULL);
4791	  BFD_ASSERT(gp != 0);
4792	  BFD_ASSERT(gotent != NULL);
4793	  BFD_ASSERT(gotent->use_count >= 1);
4794
4795	  if (!gotent->reloc_done)
4796	    {
4797	      gotent->reloc_done = 1;
4798
4799	      if (dynamic_symbol_p)
4800		value = 0;
4801	      else
4802		{
4803		  BFD_ASSERT(tls_segment != NULL);
4804		  value -= (r_type == R_ALPHA_GOTDTPREL ? dtp_base : tp_base);
4805		}
4806	      bfd_put_64 (output_bfd, value,
4807			  sgot->contents + gotent->got_offset);
4808	    }
4809
4810	  value = (sgot->output_section->vma
4811		   + sgot->output_offset
4812		   + gotent->got_offset);
4813	  value -= gp;
4814	  goto default_reloc;
4815
4816	default:
4817	default_reloc:
4818	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4819					contents, rel->r_offset, value, 0);
4820	  break;
4821	}
4822
4823      switch (r)
4824	{
4825	case bfd_reloc_ok:
4826	  break;
4827
4828	case bfd_reloc_overflow:
4829	  {
4830	    const char *name;
4831
4832	    /* Don't warn if the overflow is due to pc relative reloc
4833	       against discarded section.  Section optimization code should
4834	       handle it.  */
4835
4836	    if (r_symndx < symtab_hdr->sh_info
4837		&& sec != NULL && howto->pc_relative
4838		&& elf_discarded_section (sec))
4839	      break;
4840
4841	    if (h != NULL)
4842	      name = h->root.root.root.string;
4843	    else
4844	      {
4845		name = (bfd_elf_string_from_elf_section
4846			(input_bfd, symtab_hdr->sh_link, sym->st_name));
4847		if (name == NULL)
4848		  return false;
4849		if (*name == '\0')
4850		  name = bfd_section_name (input_bfd, sec);
4851	      }
4852	    if (! ((*info->callbacks->reloc_overflow)
4853		   (info, name, howto->name, (bfd_vma) 0,
4854		    input_bfd, input_section, rel->r_offset)))
4855	      ret_val = false;
4856	  }
4857	  break;
4858
4859	default:
4860	case bfd_reloc_outofrange:
4861	  abort ();
4862	}
4863    }
4864
4865  return ret_val;
4866}
4867
4868/* Finish up dynamic symbol handling.  We set the contents of various
4869   dynamic sections here.  */
4870
4871static boolean
4872elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
4873     bfd *output_bfd;
4874     struct bfd_link_info *info;
4875     struct elf_link_hash_entry *h;
4876     Elf_Internal_Sym *sym;
4877{
4878  bfd *dynobj = elf_hash_table(info)->dynobj;
4879
4880  if (h->plt.offset != MINUS_ONE)
4881    {
4882      /* Fill in the .plt entry for this symbol.  */
4883      asection *splt, *sgot, *srel;
4884      Elf_Internal_Rela outrel;
4885      bfd_vma got_addr, plt_addr;
4886      bfd_vma plt_index;
4887      struct alpha_elf_got_entry *gotent;
4888
4889      BFD_ASSERT (h->dynindx != -1);
4890
4891      /* The first .got entry will be updated by the .plt with the
4892	 address of the target function.  */
4893      gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
4894      BFD_ASSERT (gotent && gotent->addend == 0);
4895
4896      splt = bfd_get_section_by_name (dynobj, ".plt");
4897      BFD_ASSERT (splt != NULL);
4898      srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4899      BFD_ASSERT (srel != NULL);
4900      sgot = alpha_elf_tdata (gotent->gotobj)->got;
4901      BFD_ASSERT (sgot != NULL);
4902
4903      got_addr = (sgot->output_section->vma
4904		  + sgot->output_offset
4905		  + gotent->got_offset);
4906      plt_addr = (splt->output_section->vma
4907		  + splt->output_offset
4908		  + h->plt.offset);
4909
4910      plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
4911
4912      /* Fill in the entry in the procedure linkage table.  */
4913      {
4914	bfd_vma insn1, insn2, insn3;
4915
4916	insn1 = PLT_ENTRY_WORD1 | ((-(h->plt.offset + 4) >> 2) & 0x1fffff);
4917	insn2 = PLT_ENTRY_WORD2;
4918	insn3 = PLT_ENTRY_WORD3;
4919
4920	bfd_put_32 (output_bfd, insn1, splt->contents + h->plt.offset);
4921	bfd_put_32 (output_bfd, insn2, splt->contents + h->plt.offset + 4);
4922	bfd_put_32 (output_bfd, insn3, splt->contents + h->plt.offset + 8);
4923      }
4924
4925      /* Fill in the entry in the .rela.plt section.  */
4926      outrel.r_offset = got_addr;
4927      outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
4928      outrel.r_addend = 0;
4929
4930      bfd_elf64_swap_reloca_out (output_bfd, &outrel,
4931				 ((Elf64_External_Rela *)srel->contents
4932				  + plt_index));
4933
4934      if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
4935	{
4936	  /* Mark the symbol as undefined, rather than as defined in the
4937	     .plt section.  Leave the value alone.  */
4938	  sym->st_shndx = SHN_UNDEF;
4939	}
4940
4941      /* Fill in the entries in the .got.  */
4942      bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
4943
4944      /* Subsequent .got entries will continue to bounce through the .plt.  */
4945      if (gotent->next)
4946	{
4947	  srel = bfd_get_section_by_name (dynobj, ".rela.got");
4948	  BFD_ASSERT (! info->shared || srel != NULL);
4949
4950	  gotent = gotent->next;
4951	  do
4952	    {
4953	      sgot = alpha_elf_tdata(gotent->gotobj)->got;
4954	      BFD_ASSERT(sgot != NULL);
4955	      BFD_ASSERT(gotent->addend == 0);
4956
4957	      bfd_put_64 (output_bfd, plt_addr,
4958		          sgot->contents + gotent->got_offset);
4959
4960	      if (info->shared)
4961		{
4962		  outrel.r_offset = (sgot->output_section->vma
4963				     + sgot->output_offset
4964				     + gotent->got_offset);
4965		  outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
4966		  outrel.r_addend = plt_addr;
4967
4968		  bfd_elf64_swap_reloca_out (output_bfd, &outrel,
4969					     ((Elf64_External_Rela *)
4970					      srel->contents)
4971					     + srel->reloc_count++);
4972		  BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
4973			      <= srel->_cooked_size);
4974		}
4975
4976	      gotent = gotent->next;
4977	    }
4978          while (gotent != NULL);
4979	}
4980    }
4981  else if (alpha_elf_dynamic_symbol_p (h, info))
4982    {
4983      /* Fill in the dynamic relocations for this symbol's .got entries.  */
4984      asection *srel;
4985      Elf_Internal_Rela outrel;
4986      struct alpha_elf_got_entry *gotent;
4987
4988      srel = bfd_get_section_by_name (dynobj, ".rela.got");
4989      BFD_ASSERT (srel != NULL);
4990
4991      for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
4992	   gotent != NULL;
4993	   gotent = gotent->next)
4994	{
4995	  asection *sgot;
4996	  int r_type;
4997
4998	  if (gotent->use_count == 0)
4999	    continue;
5000
5001	  sgot = alpha_elf_tdata (gotent->gotobj)->got;
5002	  outrel.r_offset = (sgot->output_section->vma
5003			     + sgot->output_offset
5004			     + gotent->got_offset);
5005
5006	  r_type = gotent->reloc_type;
5007	  switch (r_type)
5008	    {
5009	    case R_ALPHA_LITERAL:
5010	      r_type = R_ALPHA_GLOB_DAT;
5011	      break;
5012	    case R_ALPHA_TLSGD:
5013	      r_type = R_ALPHA_DTPMOD64;
5014	      break;
5015	    case R_ALPHA_GOTDTPREL:
5016	      r_type = R_ALPHA_DTPREL64;
5017	      break;
5018	    case R_ALPHA_GOTTPREL:
5019	      r_type = R_ALPHA_TPREL64;
5020	      break;
5021	    case R_ALPHA_TLSLDM:
5022	    default:
5023	      abort ();
5024	    }
5025
5026	  outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
5027	  outrel.r_addend = gotent->addend;
5028
5029	  bfd_elf64_swap_reloca_out (output_bfd, &outrel,
5030				     ((Elf64_External_Rela *)srel->contents
5031				      + srel->reloc_count++));
5032
5033	  if (gotent->reloc_type == R_ALPHA_TLSGD)
5034	    {
5035	      outrel.r_offset += 8;
5036	      outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_DTPREL64);
5037
5038	      bfd_elf64_swap_reloca_out (output_bfd, &outrel,
5039				         ((Elf64_External_Rela *)srel->contents
5040				          + srel->reloc_count++));
5041	    }
5042
5043	  BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
5044		      <= srel->_cooked_size);
5045	}
5046    }
5047
5048  /* Mark some specially defined symbols as absolute.  */
5049  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5050      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
5051      || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
5052    sym->st_shndx = SHN_ABS;
5053
5054  return true;
5055}
5056
5057/* Finish up the dynamic sections.  */
5058
5059static boolean
5060elf64_alpha_finish_dynamic_sections (output_bfd, info)
5061     bfd *output_bfd;
5062     struct bfd_link_info *info;
5063{
5064  bfd *dynobj;
5065  asection *sdyn;
5066
5067  dynobj = elf_hash_table (info)->dynobj;
5068  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5069
5070  if (elf_hash_table (info)->dynamic_sections_created)
5071    {
5072      asection *splt;
5073      Elf64_External_Dyn *dyncon, *dynconend;
5074
5075      splt = bfd_get_section_by_name (dynobj, ".plt");
5076      BFD_ASSERT (splt != NULL && sdyn != NULL);
5077
5078      dyncon = (Elf64_External_Dyn *) sdyn->contents;
5079      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
5080      for (; dyncon < dynconend; dyncon++)
5081	{
5082	  Elf_Internal_Dyn dyn;
5083	  const char *name;
5084	  asection *s;
5085
5086	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
5087
5088	  switch (dyn.d_tag)
5089	    {
5090	    case DT_PLTGOT:
5091	      name = ".plt";
5092	      goto get_vma;
5093	    case DT_PLTRELSZ:
5094	      name = ".rela.plt";
5095	      goto get_size;
5096	    case DT_JMPREL:
5097	      name = ".rela.plt";
5098	      goto get_vma;
5099
5100	    case DT_RELASZ:
5101	      /* My interpretation of the TIS v1.1 ELF document indicates
5102		 that RELASZ should not include JMPREL.  This is not what
5103		 the rest of the BFD does.  It is, however, what the
5104		 glibc ld.so wants.  Do this fixup here until we found
5105		 out who is right.  */
5106	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
5107	      if (s)
5108		{
5109		  dyn.d_un.d_val -=
5110		    (s->_cooked_size ? s->_cooked_size : s->_raw_size);
5111		}
5112	      break;
5113
5114	    get_vma:
5115	      s = bfd_get_section_by_name (output_bfd, name);
5116	      dyn.d_un.d_ptr = (s ? s->vma : 0);
5117	      break;
5118
5119	    get_size:
5120	      s = bfd_get_section_by_name (output_bfd, name);
5121	      dyn.d_un.d_val =
5122		(s->_cooked_size ? s->_cooked_size : s->_raw_size);
5123	      break;
5124	    }
5125
5126	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5127	}
5128
5129      /* Initialize the PLT0 entry */
5130      if (splt->_raw_size > 0)
5131	{
5132	  bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
5133	  bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
5134	  bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
5135	  bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
5136
5137	  /* The next two words will be filled in by ld.so */
5138	  bfd_put_64 (output_bfd, (bfd_vma) 0, splt->contents + 16);
5139	  bfd_put_64 (output_bfd, (bfd_vma) 0, splt->contents + 24);
5140
5141	  elf_section_data (splt->output_section)->this_hdr.sh_entsize =
5142	    PLT_HEADER_SIZE;
5143	}
5144    }
5145
5146  return true;
5147}
5148
5149/* We need to use a special link routine to handle the .mdebug section.
5150   We need to merge all instances of these sections together, not write
5151   them all out sequentially.  */
5152
5153static boolean
5154elf64_alpha_final_link (abfd, info)
5155     bfd *abfd;
5156     struct bfd_link_info *info;
5157{
5158  asection *o;
5159  struct bfd_link_order *p;
5160  asection *mdebug_sec;
5161  struct ecoff_debug_info debug;
5162  const struct ecoff_debug_swap *swap
5163    = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
5164  HDRR *symhdr = &debug.symbolic_header;
5165  PTR mdebug_handle = NULL;
5166
5167  /* Go through the sections and collect the mdebug information.  */
5168  mdebug_sec = NULL;
5169  for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5170    {
5171      if (strcmp (o->name, ".mdebug") == 0)
5172	{
5173	  struct extsym_info einfo;
5174
5175	  /* We have found the .mdebug section in the output file.
5176	     Look through all the link_orders comprising it and merge
5177	     the information together.  */
5178	  symhdr->magic = swap->sym_magic;
5179	  /* FIXME: What should the version stamp be?  */
5180	  symhdr->vstamp = 0;
5181	  symhdr->ilineMax = 0;
5182	  symhdr->cbLine = 0;
5183	  symhdr->idnMax = 0;
5184	  symhdr->ipdMax = 0;
5185	  symhdr->isymMax = 0;
5186	  symhdr->ioptMax = 0;
5187	  symhdr->iauxMax = 0;
5188	  symhdr->issMax = 0;
5189	  symhdr->issExtMax = 0;
5190	  symhdr->ifdMax = 0;
5191	  symhdr->crfd = 0;
5192	  symhdr->iextMax = 0;
5193
5194	  /* We accumulate the debugging information itself in the
5195	     debug_info structure.  */
5196	  debug.line = NULL;
5197	  debug.external_dnr = NULL;
5198	  debug.external_pdr = NULL;
5199	  debug.external_sym = NULL;
5200	  debug.external_opt = NULL;
5201	  debug.external_aux = NULL;
5202	  debug.ss = NULL;
5203	  debug.ssext = debug.ssext_end = NULL;
5204	  debug.external_fdr = NULL;
5205	  debug.external_rfd = NULL;
5206	  debug.external_ext = debug.external_ext_end = NULL;
5207
5208	  mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
5209	  if (mdebug_handle == (PTR) NULL)
5210	    return false;
5211
5212	  if (1)
5213	    {
5214	      asection *s;
5215	      EXTR esym;
5216	      bfd_vma last = 0;
5217	      unsigned int i;
5218	      static const char * const name[] =
5219		{
5220		  ".text", ".init", ".fini", ".data",
5221		  ".rodata", ".sdata", ".sbss", ".bss"
5222		};
5223	      static const int sc[] = { scText, scInit, scFini, scData,
5224					  scRData, scSData, scSBss, scBss };
5225
5226	      esym.jmptbl = 0;
5227	      esym.cobol_main = 0;
5228	      esym.weakext = 0;
5229	      esym.reserved = 0;
5230	      esym.ifd = ifdNil;
5231	      esym.asym.iss = issNil;
5232	      esym.asym.st = stLocal;
5233	      esym.asym.reserved = 0;
5234	      esym.asym.index = indexNil;
5235	      for (i = 0; i < 8; i++)
5236		{
5237		  esym.asym.sc = sc[i];
5238		  s = bfd_get_section_by_name (abfd, name[i]);
5239		  if (s != NULL)
5240		    {
5241		      esym.asym.value = s->vma;
5242		      last = s->vma + s->_raw_size;
5243		    }
5244		  else
5245		    esym.asym.value = last;
5246
5247		  if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
5248						      name[i], &esym))
5249		    return false;
5250		}
5251	    }
5252
5253	  for (p = o->link_order_head;
5254	       p != (struct bfd_link_order *) NULL;
5255	       p = p->next)
5256	    {
5257	      asection *input_section;
5258	      bfd *input_bfd;
5259	      const struct ecoff_debug_swap *input_swap;
5260	      struct ecoff_debug_info input_debug;
5261	      char *eraw_src;
5262	      char *eraw_end;
5263
5264	      if (p->type != bfd_indirect_link_order)
5265		{
5266		  if (p->type == bfd_data_link_order)
5267		    continue;
5268		  abort ();
5269		}
5270
5271	      input_section = p->u.indirect.section;
5272	      input_bfd = input_section->owner;
5273
5274	      if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
5275		  || (get_elf_backend_data (input_bfd)
5276		      ->elf_backend_ecoff_debug_swap) == NULL)
5277		{
5278		  /* I don't know what a non ALPHA ELF bfd would be
5279		     doing with a .mdebug section, but I don't really
5280		     want to deal with it.  */
5281		  continue;
5282		}
5283
5284	      input_swap = (get_elf_backend_data (input_bfd)
5285			    ->elf_backend_ecoff_debug_swap);
5286
5287	      BFD_ASSERT (p->size == input_section->_raw_size);
5288
5289	      /* The ECOFF linking code expects that we have already
5290		 read in the debugging information and set up an
5291		 ecoff_debug_info structure, so we do that now.  */
5292	      if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
5293						&input_debug))
5294		return false;
5295
5296	      if (! (bfd_ecoff_debug_accumulate
5297		     (mdebug_handle, abfd, &debug, swap, input_bfd,
5298		      &input_debug, input_swap, info)))
5299		return false;
5300
5301	      /* Loop through the external symbols.  For each one with
5302		 interesting information, try to find the symbol in
5303		 the linker global hash table and save the information
5304		 for the output external symbols.  */
5305	      eraw_src = input_debug.external_ext;
5306	      eraw_end = (eraw_src
5307			  + (input_debug.symbolic_header.iextMax
5308			     * input_swap->external_ext_size));
5309	      for (;
5310		   eraw_src < eraw_end;
5311		   eraw_src += input_swap->external_ext_size)
5312		{
5313		  EXTR ext;
5314		  const char *name;
5315		  struct alpha_elf_link_hash_entry *h;
5316
5317		  (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
5318		  if (ext.asym.sc == scNil
5319		      || ext.asym.sc == scUndefined
5320		      || ext.asym.sc == scSUndefined)
5321		    continue;
5322
5323		  name = input_debug.ssext + ext.asym.iss;
5324		  h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
5325						  name, false, false, true);
5326		  if (h == NULL || h->esym.ifd != -2)
5327		    continue;
5328
5329		  if (ext.ifd != -1)
5330		    {
5331		      BFD_ASSERT (ext.ifd
5332				  < input_debug.symbolic_header.ifdMax);
5333		      ext.ifd = input_debug.ifdmap[ext.ifd];
5334		    }
5335
5336		  h->esym = ext;
5337		}
5338
5339	      /* Free up the information we just read.  */
5340	      free (input_debug.line);
5341	      free (input_debug.external_dnr);
5342	      free (input_debug.external_pdr);
5343	      free (input_debug.external_sym);
5344	      free (input_debug.external_opt);
5345	      free (input_debug.external_aux);
5346	      free (input_debug.ss);
5347	      free (input_debug.ssext);
5348	      free (input_debug.external_fdr);
5349	      free (input_debug.external_rfd);
5350	      free (input_debug.external_ext);
5351
5352	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
5353		 elf_link_input_bfd ignores this section.  */
5354	      input_section->flags &=~ SEC_HAS_CONTENTS;
5355	    }
5356
5357	  /* Build the external symbol information.  */
5358	  einfo.abfd = abfd;
5359	  einfo.info = info;
5360	  einfo.debug = &debug;
5361	  einfo.swap = swap;
5362	  einfo.failed = false;
5363	  elf_link_hash_traverse (elf_hash_table (info),
5364				  elf64_alpha_output_extsym,
5365				  (PTR) &einfo);
5366	  if (einfo.failed)
5367	    return false;
5368
5369	  /* Set the size of the .mdebug section.  */
5370	  o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
5371
5372	  /* Skip this section later on (I don't think this currently
5373	     matters, but someday it might).  */
5374	  o->link_order_head = (struct bfd_link_order *) NULL;
5375
5376	  mdebug_sec = o;
5377	}
5378    }
5379
5380  /* Invoke the regular ELF backend linker to do all the work.  */
5381  if (! bfd_elf64_bfd_final_link (abfd, info))
5382    return false;
5383
5384  /* Now write out the computed sections.  */
5385
5386  /* The .got subsections...  */
5387  {
5388    bfd *i, *dynobj = elf_hash_table(info)->dynobj;
5389    for (i = alpha_elf_hash_table(info)->got_list;
5390	 i != NULL;
5391	 i = alpha_elf_tdata(i)->got_link_next)
5392      {
5393	asection *sgot;
5394
5395	/* elf_bfd_final_link already did everything in dynobj.  */
5396	if (i == dynobj)
5397	  continue;
5398
5399	sgot = alpha_elf_tdata(i)->got;
5400	if (! bfd_set_section_contents (abfd, sgot->output_section,
5401					sgot->contents,
5402					(file_ptr) sgot->output_offset,
5403					sgot->_raw_size))
5404	  return false;
5405      }
5406  }
5407
5408  if (mdebug_sec != (asection *) NULL)
5409    {
5410      BFD_ASSERT (abfd->output_has_begun);
5411      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5412					       swap, info,
5413					       mdebug_sec->filepos))
5414	return false;
5415
5416      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5417    }
5418
5419  return true;
5420}
5421
5422static enum elf_reloc_type_class
5423elf64_alpha_reloc_type_class (rela)
5424     const Elf_Internal_Rela *rela;
5425{
5426  switch ((int) ELF64_R_TYPE (rela->r_info))
5427    {
5428    case R_ALPHA_RELATIVE:
5429      return reloc_class_relative;
5430    case R_ALPHA_JMP_SLOT:
5431      return reloc_class_plt;
5432    case R_ALPHA_COPY:
5433      return reloc_class_copy;
5434    default:
5435      return reloc_class_normal;
5436    }
5437}
5438
5439/* ECOFF swapping routines.  These are used when dealing with the
5440   .mdebug section, which is in the ECOFF debugging format.  Copied
5441   from elf32-mips.c.  */
5442static const struct ecoff_debug_swap
5443elf64_alpha_ecoff_debug_swap =
5444{
5445  /* Symbol table magic number.  */
5446  magicSym2,
5447  /* Alignment of debugging information.  E.g., 4.  */
5448  8,
5449  /* Sizes of external symbolic information.  */
5450  sizeof (struct hdr_ext),
5451  sizeof (struct dnr_ext),
5452  sizeof (struct pdr_ext),
5453  sizeof (struct sym_ext),
5454  sizeof (struct opt_ext),
5455  sizeof (struct fdr_ext),
5456  sizeof (struct rfd_ext),
5457  sizeof (struct ext_ext),
5458  /* Functions to swap in external symbolic data.  */
5459  ecoff_swap_hdr_in,
5460  ecoff_swap_dnr_in,
5461  ecoff_swap_pdr_in,
5462  ecoff_swap_sym_in,
5463  ecoff_swap_opt_in,
5464  ecoff_swap_fdr_in,
5465  ecoff_swap_rfd_in,
5466  ecoff_swap_ext_in,
5467  _bfd_ecoff_swap_tir_in,
5468  _bfd_ecoff_swap_rndx_in,
5469  /* Functions to swap out external symbolic data.  */
5470  ecoff_swap_hdr_out,
5471  ecoff_swap_dnr_out,
5472  ecoff_swap_pdr_out,
5473  ecoff_swap_sym_out,
5474  ecoff_swap_opt_out,
5475  ecoff_swap_fdr_out,
5476  ecoff_swap_rfd_out,
5477  ecoff_swap_ext_out,
5478  _bfd_ecoff_swap_tir_out,
5479  _bfd_ecoff_swap_rndx_out,
5480  /* Function to read in symbolic data.  */
5481  elf64_alpha_read_ecoff_info
5482};
5483
5484/* Use a non-standard hash bucket size of 8.  */
5485
5486static const struct elf_size_info alpha_elf_size_info =
5487{
5488  sizeof (Elf64_External_Ehdr),
5489  sizeof (Elf64_External_Phdr),
5490  sizeof (Elf64_External_Shdr),
5491  sizeof (Elf64_External_Rel),
5492  sizeof (Elf64_External_Rela),
5493  sizeof (Elf64_External_Sym),
5494  sizeof (Elf64_External_Dyn),
5495  sizeof (Elf_External_Note),
5496  8,
5497  1,
5498  64, 8,
5499  ELFCLASS64, EV_CURRENT,
5500  bfd_elf64_write_out_phdrs,
5501  bfd_elf64_write_shdrs_and_ehdr,
5502  bfd_elf64_write_relocs,
5503  bfd_elf64_swap_symbol_in,
5504  bfd_elf64_swap_symbol_out,
5505  bfd_elf64_slurp_reloc_table,
5506  bfd_elf64_slurp_symbol_table,
5507  bfd_elf64_swap_dyn_in,
5508  bfd_elf64_swap_dyn_out,
5509  NULL,
5510  NULL,
5511  NULL,
5512  NULL
5513};
5514
5515#ifndef ELF_ARCH
5516#define TARGET_LITTLE_SYM	bfd_elf64_alpha_vec
5517#define TARGET_LITTLE_NAME	"elf64-alpha"
5518#define ELF_ARCH		bfd_arch_alpha
5519#define ELF_MACHINE_CODE	EM_ALPHA
5520#define ELF_MAXPAGESIZE	0x10000
5521#endif /* ELF_ARCH */
5522
5523#define bfd_elf64_bfd_link_hash_table_create \
5524  elf64_alpha_bfd_link_hash_table_create
5525
5526#define bfd_elf64_bfd_reloc_type_lookup \
5527  elf64_alpha_bfd_reloc_type_lookup
5528#define elf_info_to_howto \
5529  elf64_alpha_info_to_howto
5530
5531#define bfd_elf64_mkobject \
5532  elf64_alpha_mkobject
5533#define elf_backend_object_p \
5534  elf64_alpha_object_p
5535
5536#define elf_backend_section_from_shdr \
5537  elf64_alpha_section_from_shdr
5538#define elf_backend_section_flags \
5539  elf64_alpha_section_flags
5540#define elf_backend_fake_sections \
5541  elf64_alpha_fake_sections
5542
5543#define bfd_elf64_bfd_is_local_label_name \
5544  elf64_alpha_is_local_label_name
5545#define bfd_elf64_find_nearest_line \
5546  elf64_alpha_find_nearest_line
5547#define bfd_elf64_bfd_relax_section \
5548  elf64_alpha_relax_section
5549
5550#define elf_backend_add_symbol_hook \
5551  elf64_alpha_add_symbol_hook
5552#define elf_backend_check_relocs \
5553  elf64_alpha_check_relocs
5554#define elf_backend_create_dynamic_sections \
5555  elf64_alpha_create_dynamic_sections
5556#define elf_backend_adjust_dynamic_symbol \
5557  elf64_alpha_adjust_dynamic_symbol
5558#define elf_backend_always_size_sections \
5559  elf64_alpha_always_size_sections
5560#define elf_backend_size_dynamic_sections \
5561  elf64_alpha_size_dynamic_sections
5562#define elf_backend_relocate_section \
5563  elf64_alpha_relocate_section
5564#define elf_backend_finish_dynamic_symbol \
5565  elf64_alpha_finish_dynamic_symbol
5566#define elf_backend_finish_dynamic_sections \
5567  elf64_alpha_finish_dynamic_sections
5568#define bfd_elf64_bfd_final_link \
5569  elf64_alpha_final_link
5570#define elf_backend_reloc_type_class \
5571  elf64_alpha_reloc_type_class
5572
5573#define elf_backend_ecoff_debug_swap \
5574  &elf64_alpha_ecoff_debug_swap
5575
5576#define elf_backend_size_info \
5577  alpha_elf_size_info
5578
5579/* A few constants that determine how the .plt section is set up.  */
5580#define elf_backend_want_got_plt 0
5581#define elf_backend_plt_readonly 0
5582#define elf_backend_want_plt_sym 1
5583#define elf_backend_got_header_size 0
5584#define elf_backend_plt_header_size PLT_HEADER_SIZE
5585
5586#include "elf64-target.h"
5587