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