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