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