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