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