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