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