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