* oasys.c: Add missing prototypes.
[platform/upstream/binutils.git] / bfd / elf64-alpha.c
1 /* Alpha specific support for 64-bit ELF
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Contributed by Richard Henderson <rth@tamu.edu>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* We need a published ABI spec for this.  Until one comes out, don't
23    assume this'll remain unchanged forever.  */
24
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29
30 #include "elf/alpha.h"
31
32 #define ALPHAECOFF
33
34 #define NO_COFF_RELOCS
35 #define NO_COFF_SYMBOLS
36 #define NO_COFF_LINENOS
37
38 /* Get the ECOFF swapping routines.  Needed for the debug information.  */
39 #include "coff/internal.h"
40 #include "coff/sym.h"
41 #include "coff/symconst.h"
42 #include "coff/ecoff.h"
43 #include "coff/alpha.h"
44 #include "aout/ar.h"
45 #include "libcoff.h"
46 #include "libecoff.h"
47 #define ECOFF_64
48 #include "ecoffswap.h"
49
50 static int alpha_elf_dynamic_symbol_p
51   PARAMS((struct elf_link_hash_entry *, struct bfd_link_info *));
52 static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
53   PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
54 static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
55   PARAMS((bfd *));
56
57 static bfd_reloc_status_type elf64_alpha_reloc_nil
58   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static bfd_reloc_status_type elf64_alpha_reloc_bad
60   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
61 static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
62   PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
63 static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
64   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
65
66 static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
67   PARAMS((bfd *, bfd_reloc_code_real_type));
68 static void elf64_alpha_info_to_howto
69   PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
70
71 static boolean elf64_alpha_mkobject
72   PARAMS((bfd *));
73 static boolean elf64_alpha_object_p
74   PARAMS((bfd *));
75 static boolean elf64_alpha_section_from_shdr
76   PARAMS((bfd *, Elf64_Internal_Shdr *, char *));
77 static boolean elf64_alpha_fake_sections
78   PARAMS((bfd *, Elf64_Internal_Shdr *, asection *));
79 static boolean elf64_alpha_create_got_section
80   PARAMS((bfd *, struct bfd_link_info *));
81 static boolean elf64_alpha_create_dynamic_sections
82   PARAMS((bfd *, struct bfd_link_info *));
83
84 static boolean elf64_alpha_read_ecoff_info
85   PARAMS((bfd *, asection *, struct ecoff_debug_info *));
86 static boolean elf64_alpha_is_local_label_name
87   PARAMS((bfd *, const char *));
88 static boolean elf64_alpha_find_nearest_line
89   PARAMS((bfd *, asection *, asymbol **, bfd_vma, const char **,
90           const char **, unsigned int *));
91
92 #if defined(__STDC__) || defined(ALMOST_STDC)
93 struct alpha_elf_link_hash_entry;
94 #endif
95
96 static boolean elf64_alpha_output_extsym
97   PARAMS((struct alpha_elf_link_hash_entry *, PTR));
98
99 static boolean elf64_alpha_can_merge_gots
100   PARAMS((bfd *, bfd *));
101 static void elf64_alpha_merge_gots
102   PARAMS((bfd *, bfd *));
103 static boolean elf64_alpha_calc_got_offsets_for_symbol
104   PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
105 static void elf64_alpha_calc_got_offsets PARAMS ((struct bfd_link_info *));
106 static boolean elf64_alpha_size_got_sections
107   PARAMS ((bfd *, struct bfd_link_info *));
108 static boolean elf64_alpha_always_size_sections
109   PARAMS ((bfd *, struct bfd_link_info *));
110 static boolean elf64_alpha_calc_dynrel_sizes
111   PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
112 static boolean elf64_alpha_add_symbol_hook
113   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
114            const char **, flagword *, asection **, bfd_vma *));
115 static boolean elf64_alpha_check_relocs
116   PARAMS((bfd *, struct bfd_link_info *, asection *sec,
117           const Elf_Internal_Rela *));
118 static boolean elf64_alpha_adjust_dynamic_symbol
119   PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
120 static boolean elf64_alpha_size_dynamic_sections
121   PARAMS((bfd *, struct bfd_link_info *));
122 static boolean elf64_alpha_relocate_section
123   PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
124           Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
125 static boolean elf64_alpha_finish_dynamic_symbol
126   PARAMS((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
127           Elf_Internal_Sym *));
128 static boolean elf64_alpha_finish_dynamic_sections
129   PARAMS((bfd *, struct bfd_link_info *));
130 static boolean elf64_alpha_final_link
131   PARAMS((bfd *, struct bfd_link_info *));
132 static boolean elf64_alpha_merge_ind_symbols
133   PARAMS((struct alpha_elf_link_hash_entry *, PTR));
134 static Elf_Internal_Rela * elf64_alpha_find_reloc_at_ofs
135   PARAMS ((Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_vma, int));
136 static enum elf_reloc_type_class elf64_alpha_reloc_type_class
137   PARAMS ((int));
138 \f
139 struct alpha_elf_link_hash_entry
140 {
141   struct elf_link_hash_entry root;
142
143   /* External symbol information.  */
144   EXTR esym;
145
146   /* Cumulative flags for all the .got entries.  */
147   int flags;
148
149   /* Contexts (LITUSE) in which a literal was referenced.  */
150 #define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
151 #define ALPHA_ELF_LINK_HASH_LU_MEM  0x02
152 #define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
153 #define ALPHA_ELF_LINK_HASH_LU_FUNC 0x08
154
155   /* Used to implement multiple .got subsections.  */
156   struct alpha_elf_got_entry
157   {
158     struct alpha_elf_got_entry *next;
159
160     /* which .got subsection?  */
161     bfd *gotobj;
162
163     /* the addend in effect for this entry.  */
164     bfd_signed_vma addend;
165
166     /* the .got offset for this entry.  */
167     int got_offset;
168
169     int flags;
170
171     /* An additional flag.  */
172 #define ALPHA_ELF_GOT_ENTRY_RELOCS_DONE 0x10
173
174     int use_count;
175   } *got_entries;
176
177   /* used to count non-got, non-plt relocations for delayed sizing
178      of relocation sections.  */
179   struct alpha_elf_reloc_entry
180   {
181     struct alpha_elf_reloc_entry *next;
182
183     /* which .reloc section? */
184     asection *srel;
185
186     /* what kind of relocation? */
187     unsigned int rtype;
188
189     /* is this against read-only section? */
190     unsigned int reltext : 1;
191
192     /* how many did we find?  */
193     unsigned long count;
194   } *reloc_entries;
195 };
196
197 /* Alpha ELF linker hash table.  */
198
199 struct alpha_elf_link_hash_table
200 {
201   struct elf_link_hash_table root;
202
203   /* The head of a list of .got subsections linked through
204      alpha_elf_tdata(abfd)->got_link_next.  */
205   bfd *got_list;
206 };
207
208 /* Look up an entry in a Alpha ELF linker hash table.  */
209
210 #define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
211   ((struct alpha_elf_link_hash_entry *)                                 \
212    elf_link_hash_lookup (&(table)->root, (string), (create),            \
213                          (copy), (follow)))
214
215 /* Traverse a Alpha ELF linker hash table.  */
216
217 #define alpha_elf_link_hash_traverse(table, func, info)                 \
218   (elf_link_hash_traverse                                               \
219    (&(table)->root,                                                     \
220     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
221     (info)))
222
223 /* Get the Alpha ELF linker hash table from a link_info structure.  */
224
225 #define alpha_elf_hash_table(p) \
226   ((struct alpha_elf_link_hash_table *) ((p)->hash))
227
228 /* Get the object's symbols as our own entry type.  */
229
230 #define alpha_elf_sym_hashes(abfd) \
231   ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
232
233 /* Should we do dynamic things to this symbol?  */
234
235 static int
236 alpha_elf_dynamic_symbol_p (h, info)
237      struct elf_link_hash_entry *h;
238      struct bfd_link_info *info;
239 {
240   if (h == NULL)
241     return false;
242
243   while (h->root.type == bfd_link_hash_indirect
244          || h->root.type == bfd_link_hash_warning)
245     h = (struct elf_link_hash_entry *) h->root.u.i.link;
246
247   if (h->dynindx == -1)
248     return false;
249
250   if (h->root.type == bfd_link_hash_undefweak
251       || h->root.type == bfd_link_hash_defweak)
252     return true;
253
254   switch (ELF_ST_VISIBILITY (h->other))
255     {
256     case STV_DEFAULT:
257       break;
258     case STV_HIDDEN:
259     case STV_INTERNAL:
260       return false;
261     case STV_PROTECTED:
262       if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
263         return false;
264       break;
265     }
266
267   if ((info->shared && !info->symbolic)
268       || ((h->elf_link_hash_flags
269            & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))
270           == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
271     return true;
272
273   return false;
274 }
275
276 /* Create an entry in a Alpha ELF linker hash table.  */
277
278 static struct bfd_hash_entry *
279 elf64_alpha_link_hash_newfunc (entry, table, string)
280      struct bfd_hash_entry *entry;
281      struct bfd_hash_table *table;
282      const char *string;
283 {
284   struct alpha_elf_link_hash_entry *ret =
285     (struct alpha_elf_link_hash_entry *) entry;
286
287   /* Allocate the structure if it has not already been allocated by a
288      subclass.  */
289   if (ret == (struct alpha_elf_link_hash_entry *) NULL)
290     ret = ((struct alpha_elf_link_hash_entry *)
291            bfd_hash_allocate (table,
292                               sizeof (struct alpha_elf_link_hash_entry)));
293   if (ret == (struct alpha_elf_link_hash_entry *) NULL)
294     return (struct bfd_hash_entry *) ret;
295
296   /* Call the allocation method of the superclass.  */
297   ret = ((struct alpha_elf_link_hash_entry *)
298          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
299                                      table, string));
300   if (ret != (struct alpha_elf_link_hash_entry *) NULL)
301     {
302       /* Set local fields.  */
303       memset (&ret->esym, 0, sizeof (EXTR));
304       /* We use -2 as a marker to indicate that the information has
305          not been set.  -1 means there is no associated ifd.  */
306       ret->esym.ifd = -2;
307       ret->flags = 0;
308       ret->got_entries = NULL;
309       ret->reloc_entries = NULL;
310     }
311
312   return (struct bfd_hash_entry *) ret;
313 }
314
315 /* Create a Alpha ELF linker hash table.  */
316
317 static struct bfd_link_hash_table *
318 elf64_alpha_bfd_link_hash_table_create (abfd)
319      bfd *abfd;
320 {
321   struct alpha_elf_link_hash_table *ret;
322
323   ret = ((struct alpha_elf_link_hash_table *)
324          bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
325   if (ret == (struct alpha_elf_link_hash_table *) NULL)
326     return NULL;
327
328   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
329                                        elf64_alpha_link_hash_newfunc))
330     {
331       bfd_release (abfd, ret);
332       return NULL;
333     }
334
335   return &ret->root.root;
336 }
337 \f
338 /* We have some private fields hanging off of the elf_tdata structure.  */
339
340 struct alpha_elf_obj_tdata
341 {
342   struct elf_obj_tdata root;
343
344   /* For every input file, these are the got entries for that object's
345      local symbols.  */
346   struct alpha_elf_got_entry ** local_got_entries;
347
348   /* For every input file, this is the object that owns the got that
349      this input file uses.  */
350   bfd *gotobj;
351
352   /* For every got, this is a linked list through the objects using this got */
353   bfd *in_got_link_next;
354
355   /* For every got, this is a link to the next got subsegment.  */
356   bfd *got_link_next;
357
358   /* For every got, this is the section.  */
359   asection *got;
360
361   /* For every got, this is it's total number of *entries*.  */
362   int total_got_entries;
363
364   /* For every got, this is the sum of the number of *entries* required
365      to hold all of the member object's local got.  */
366   int n_local_got_entries;
367 };
368
369 #define alpha_elf_tdata(abfd) \
370   ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
371
372 static boolean
373 elf64_alpha_mkobject (abfd)
374      bfd *abfd;
375 {
376   abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
377   if (abfd->tdata.any == NULL)
378     return false;
379   return true;
380 }
381
382 static boolean
383 elf64_alpha_object_p (abfd)
384      bfd *abfd;
385 {
386   /* Allocate our special target data.  */
387   struct alpha_elf_obj_tdata *new_tdata;
388   new_tdata = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
389   if (new_tdata == NULL)
390     return false;
391   new_tdata->root = *abfd->tdata.elf_obj_data;
392   abfd->tdata.any = new_tdata;
393
394   /* Set the right machine number for an Alpha ELF file.  */
395   return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
396 }
397 \f
398 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
399    from smaller values.  Start with zero, widen, *then* decrement.  */
400 #define MINUS_ONE       (((bfd_vma)0) - 1)
401
402 static reloc_howto_type elf64_alpha_howto_table[] =
403 {
404   HOWTO (R_ALPHA_NONE,          /* type */
405          0,                     /* rightshift */
406          0,                     /* size (0 = byte, 1 = short, 2 = long) */
407          8,                     /* bitsize */
408          true,                  /* pc_relative */
409          0,                     /* bitpos */
410          complain_overflow_dont, /* complain_on_overflow */
411          elf64_alpha_reloc_nil, /* special_function */
412          "NONE",                /* name */
413          false,                 /* partial_inplace */
414          0,                     /* src_mask */
415          0,                     /* dst_mask */
416          true),                 /* pcrel_offset */
417
418   /* A 32 bit reference to a symbol.  */
419   HOWTO (R_ALPHA_REFLONG,       /* type */
420          0,                     /* rightshift */
421          2,                     /* size (0 = byte, 1 = short, 2 = long) */
422          32,                    /* bitsize */
423          false,                 /* pc_relative */
424          0,                     /* bitpos */
425          complain_overflow_bitfield, /* complain_on_overflow */
426          0,                     /* special_function */
427          "REFLONG",             /* name */
428          false,                 /* partial_inplace */
429          0xffffffff,            /* src_mask */
430          0xffffffff,            /* dst_mask */
431          false),                /* pcrel_offset */
432
433   /* A 64 bit reference to a symbol.  */
434   HOWTO (R_ALPHA_REFQUAD,       /* type */
435          0,                     /* rightshift */
436          4,                     /* size (0 = byte, 1 = short, 2 = long) */
437          64,                    /* bitsize */
438          false,                 /* pc_relative */
439          0,                     /* bitpos */
440          complain_overflow_bitfield, /* complain_on_overflow */
441          0,                     /* special_function */
442          "REFQUAD",             /* name */
443          false,                 /* partial_inplace */
444          MINUS_ONE,             /* src_mask */
445          MINUS_ONE,             /* dst_mask */
446          false),                /* pcrel_offset */
447
448   /* A 32 bit GP relative offset.  This is just like REFLONG except
449      that when the value is used the value of the gp register will be
450      added in.  */
451   HOWTO (R_ALPHA_GPREL32,       /* type */
452          0,                     /* rightshift */
453          2,                     /* size (0 = byte, 1 = short, 2 = long) */
454          32,                    /* bitsize */
455          false,                 /* pc_relative */
456          0,                     /* bitpos */
457          complain_overflow_bitfield, /* complain_on_overflow */
458          0,                     /* special_function */
459          "GPREL32",             /* name */
460          false,                 /* partial_inplace */
461          0xffffffff,            /* src_mask */
462          0xffffffff,            /* dst_mask */
463          false),                /* pcrel_offset */
464
465   /* Used for an instruction that refers to memory off the GP register.  */
466   HOWTO (R_ALPHA_LITERAL,       /* type */
467          0,                     /* rightshift */
468          2,                     /* size (0 = byte, 1 = short, 2 = long) */
469          16,                    /* bitsize */
470          false,                 /* pc_relative */
471          0,                     /* bitpos */
472          complain_overflow_signed, /* complain_on_overflow */
473          0,                     /* special_function */
474          "ELF_LITERAL",         /* name */
475          false,                 /* partial_inplace */
476          0xffff,                /* src_mask */
477          0xffff,                /* dst_mask */
478          false),                /* pcrel_offset */
479
480   /* This reloc only appears immediately following an ELF_LITERAL reloc.
481      It identifies a use of the literal.  The symbol index is special:
482      1 means the literal address is in the base register of a memory
483      format instruction; 2 means the literal address is in the byte
484      offset register of a byte-manipulation instruction; 3 means the
485      literal address is in the target register of a jsr instruction.
486      This does not actually do any relocation.  */
487   HOWTO (R_ALPHA_LITUSE,        /* type */
488          0,                     /* rightshift */
489          2,                     /* size (0 = byte, 1 = short, 2 = long) */
490          32,                    /* bitsize */
491          false,                 /* pc_relative */
492          0,                     /* bitpos */
493          complain_overflow_dont, /* complain_on_overflow */
494          elf64_alpha_reloc_nil, /* special_function */
495          "LITUSE",              /* name */
496          false,                 /* partial_inplace */
497          0,                     /* src_mask */
498          0,                     /* dst_mask */
499          false),                /* pcrel_offset */
500
501   /* Load the gp register.  This is always used for a ldah instruction
502      which loads the upper 16 bits of the gp register.  The symbol
503      index of the GPDISP instruction is an offset in bytes to the lda
504      instruction that loads the lower 16 bits.  The value to use for
505      the relocation is the difference between the GP value and the
506      current location; the load will always be done against a register
507      holding the current address.
508
509      NOTE: Unlike ECOFF, partial in-place relocation is not done.  If
510      any offset is present in the instructions, it is an offset from
511      the register to the ldah instruction.  This lets us avoid any
512      stupid hackery like inventing a gp value to do partial relocation
513      against.  Also unlike ECOFF, we do the whole relocation off of
514      the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair.  An odd,
515      space consuming bit, that, since all the information was present
516      in the GPDISP_HI16 reloc.  */
517   HOWTO (R_ALPHA_GPDISP,        /* type */
518          16,                    /* rightshift */
519          2,                     /* size (0 = byte, 1 = short, 2 = long) */
520          16,                    /* bitsize */
521          false,                 /* pc_relative */
522          0,                     /* bitpos */
523          complain_overflow_dont, /* complain_on_overflow */
524          elf64_alpha_reloc_gpdisp, /* special_function */
525          "GPDISP",              /* name */
526          false,                 /* partial_inplace */
527          0xffff,                /* src_mask */
528          0xffff,                /* dst_mask */
529          true),                 /* pcrel_offset */
530
531   /* A 21 bit branch.  */
532   HOWTO (R_ALPHA_BRADDR,        /* type */
533          2,                     /* rightshift */
534          2,                     /* size (0 = byte, 1 = short, 2 = long) */
535          21,                    /* bitsize */
536          true,                  /* pc_relative */
537          0,                     /* bitpos */
538          complain_overflow_signed, /* complain_on_overflow */
539          0,                     /* special_function */
540          "BRADDR",              /* name */
541          false,                 /* partial_inplace */
542          0x1fffff,              /* src_mask */
543          0x1fffff,              /* dst_mask */
544          true),                 /* pcrel_offset */
545
546   /* A hint for a jump to a register.  */
547   HOWTO (R_ALPHA_HINT,          /* type */
548          2,                     /* rightshift */
549          2,                     /* size (0 = byte, 1 = short, 2 = long) */
550          14,                    /* bitsize */
551          true,                  /* pc_relative */
552          0,                     /* bitpos */
553          complain_overflow_dont, /* complain_on_overflow */
554          0,                     /* special_function */
555          "HINT",                /* name */
556          false,                 /* partial_inplace */
557          0x3fff,                /* src_mask */
558          0x3fff,                /* dst_mask */
559          true),                 /* pcrel_offset */
560
561   /* 16 bit PC relative offset.  */
562   HOWTO (R_ALPHA_SREL16,        /* type */
563          0,                     /* rightshift */
564          1,                     /* size (0 = byte, 1 = short, 2 = long) */
565          16,                    /* bitsize */
566          true,                  /* pc_relative */
567          0,                     /* bitpos */
568          complain_overflow_signed, /* complain_on_overflow */
569          0,                     /* special_function */
570          "SREL16",              /* name */
571          false,                 /* partial_inplace */
572          0xffff,                /* src_mask */
573          0xffff,                /* dst_mask */
574          true),                 /* pcrel_offset */
575
576   /* 32 bit PC relative offset.  */
577   HOWTO (R_ALPHA_SREL32,        /* type */
578          0,                     /* rightshift */
579          2,                     /* size (0 = byte, 1 = short, 2 = long) */
580          32,                    /* bitsize */
581          true,                  /* pc_relative */
582          0,                     /* bitpos */
583          complain_overflow_signed, /* complain_on_overflow */
584          0,                     /* special_function */
585          "SREL32",              /* name */
586          false,                 /* partial_inplace */
587          0xffffffff,            /* src_mask */
588          0xffffffff,            /* dst_mask */
589          true),                 /* pcrel_offset */
590
591   /* A 64 bit PC relative offset.  */
592   HOWTO (R_ALPHA_SREL64,        /* type */
593          0,                     /* rightshift */
594          4,                     /* size (0 = byte, 1 = short, 2 = long) */
595          64,                    /* bitsize */
596          true,                  /* pc_relative */
597          0,                     /* bitpos */
598          complain_overflow_signed, /* complain_on_overflow */
599          0,                     /* special_function */
600          "SREL64",              /* name */
601          false,                 /* partial_inplace */
602          MINUS_ONE,             /* src_mask */
603          MINUS_ONE,             /* dst_mask */
604          true),                 /* pcrel_offset */
605
606   /* Push a value on the reloc evaluation stack.  */
607   /* Not implemented -- it's dumb.  */
608   HOWTO (R_ALPHA_OP_PUSH,       /* type */
609          0,                     /* rightshift */
610          0,                     /* size (0 = byte, 1 = short, 2 = long) */
611          0,                     /* bitsize */
612          false,                 /* pc_relative */
613          0,                     /* bitpos */
614          complain_overflow_dont, /* complain_on_overflow */
615          elf64_alpha_reloc_bad, /* special_function */
616          "OP_PUSH",             /* name */
617          false,                 /* partial_inplace */
618          0,                     /* src_mask */
619          0,                     /* dst_mask */
620          false),                /* pcrel_offset */
621
622   /* Store the value from the stack at the given address.  Store it in
623      a bitfield of size r_size starting at bit position r_offset.  */
624   /* Not implemented -- it's dumb.  */
625   HOWTO (R_ALPHA_OP_STORE,      /* type */
626          0,                     /* rightshift */
627          4,                     /* size (0 = byte, 1 = short, 2 = long) */
628          64,                    /* bitsize */
629          false,                 /* pc_relative */
630          0,                     /* bitpos */
631          complain_overflow_dont, /* complain_on_overflow */
632          elf64_alpha_reloc_bad, /* special_function */
633          "OP_STORE",            /* name */
634          false,                 /* partial_inplace */
635          0,                     /* src_mask */
636          MINUS_ONE,             /* dst_mask */
637          false),                /* pcrel_offset */
638
639   /* Subtract the reloc address from the value on the top of the
640      relocation stack.  */
641   /* Not implemented -- it's dumb.  */
642   HOWTO (R_ALPHA_OP_PSUB,       /* type */
643          0,                     /* rightshift */
644          0,                     /* size (0 = byte, 1 = short, 2 = long) */
645          0,                     /* bitsize */
646          false,                 /* pc_relative */
647          0,                     /* bitpos */
648          complain_overflow_dont, /* complain_on_overflow */
649          elf64_alpha_reloc_bad, /* special_function */
650          "OP_PSUB",             /* name */
651          false,                 /* partial_inplace */
652          0,                     /* src_mask */
653          0,                     /* dst_mask */
654          false),                /* pcrel_offset */
655
656   /* Shift the value on the top of the relocation stack right by the
657      given value.  */
658   /* Not implemented -- it's dumb.  */
659   HOWTO (R_ALPHA_OP_PRSHIFT,    /* type */
660          0,                     /* rightshift */
661          0,                     /* size (0 = byte, 1 = short, 2 = long) */
662          0,                     /* bitsize */
663          false,                 /* pc_relative */
664          0,                     /* bitpos */
665          complain_overflow_dont, /* complain_on_overflow */
666          elf64_alpha_reloc_bad, /* special_function */
667          "OP_PRSHIFT",          /* name */
668          false,                 /* partial_inplace */
669          0,                     /* src_mask */
670          0,                     /* dst_mask */
671          false),                /* pcrel_offset */
672
673   /* Change the value of GP used by +r_addend until the next GPVALUE or the
674      end of the input bfd.  */
675   /* Not implemented -- it's dumb.  */
676   HOWTO (R_ALPHA_GPVALUE,
677          0,                     /* rightshift */
678          0,                     /* size (0 = byte, 1 = short, 2 = long) */
679          0,                     /* bitsize */
680          false,                 /* pc_relative */
681          0,                     /* bitpos */
682          complain_overflow_dont, /* complain_on_overflow */
683          elf64_alpha_reloc_bad, /* special_function */
684          "GPVALUE",             /* name */
685          false,                 /* partial_inplace */
686          0,                     /* src_mask */
687          0,                     /* dst_mask */
688          false),                /* pcrel_offset */
689
690   /* The high 16 bits of the displacement from GP to the target.  */
691   HOWTO (R_ALPHA_GPRELHIGH,
692          0,                     /* rightshift */
693          2,                     /* size (0 = byte, 1 = short, 2 = long) */
694          16,                    /* bitsize */
695          false,                 /* pc_relative */
696          0,                     /* bitpos */
697          complain_overflow_signed, /* complain_on_overflow */
698          elf64_alpha_reloc_bad, /* special_function */
699          "GPRELHIGH",           /* name */
700          false,                 /* partial_inplace */
701          0xffff,                /* src_mask */
702          0xffff,                /* dst_mask */
703          false),                /* pcrel_offset */
704
705   /* The low 16 bits of the displacement from GP to the target.  */
706   HOWTO (R_ALPHA_GPRELLOW,
707          0,                     /* rightshift */
708          2,                     /* size (0 = byte, 1 = short, 2 = long) */
709          16,                    /* bitsize */
710          false,                 /* pc_relative */
711          0,                     /* bitpos */
712          complain_overflow_dont, /* complain_on_overflow */
713          elf64_alpha_reloc_bad, /* special_function */
714          "GPRELLOW",            /* name */
715          false,                 /* partial_inplace */
716          0xffff,                /* src_mask */
717          0xffff,                /* dst_mask */
718          false),                /* pcrel_offset */
719
720   /* A 16-bit displacement from the GP to the target.  */
721   /* XXX: Not implemented.  */
722   HOWTO (R_ALPHA_IMMED_GP_16,
723          0,                     /* rightshift */
724          2,                     /* size (0 = byte, 1 = short, 2 = long) */
725          16,                    /* bitsize */
726          false,                 /* pc_relative */
727          0,                     /* bitpos */
728          complain_overflow_signed, /* complain_on_overflow */
729          0,                     /* special_function */
730          "IMMED_GP_16",         /* name */
731          false,                 /* partial_inplace */
732          0xffff,                /* src_mask */
733          0xffff,                /* dst_mask */
734          false),                /* pcrel_offset */
735
736   /* The high bits of a 32-bit displacement from the GP to the target; the
737      low bits are supplied in the subsequent R_ALPHA_IMMED_LO32 relocs.  */
738   /* XXX: Not implemented.  */
739   HOWTO (R_ALPHA_IMMED_GP_HI32,
740          0,                     /* rightshift */
741          0,                     /* size (0 = byte, 1 = short, 2 = long) */
742          0,                     /* bitsize */
743          false,                 /* pc_relative */
744          0,                     /* bitpos */
745          complain_overflow_dont, /* complain_on_overflow */
746          elf64_alpha_reloc_bad, /* special_function */
747          "IMMED_GP_HI32",               /* name */
748          false,                 /* partial_inplace */
749          0,                     /* src_mask */
750          0,                     /* dst_mask */
751          false),                /* pcrel_offset */
752
753   /* The high bits of a 32-bit displacement to the starting address of the
754      current section (the relocation target is ignored); the low bits are
755      supplied in the subsequent R_ALPHA_IMMED_LO32 relocs.  */
756   /* XXX: Not implemented.  */
757   HOWTO (R_ALPHA_IMMED_SCN_HI32,
758          0,                     /* rightshift */
759          0,                     /* size (0 = byte, 1 = short, 2 = long) */
760          0,                     /* bitsize */
761          false,                 /* pc_relative */
762          0,                     /* bitpos */
763          complain_overflow_dont, /* complain_on_overflow */
764          elf64_alpha_reloc_bad, /* special_function */
765          "IMMED_SCN_HI32",              /* name */
766          false,                 /* partial_inplace */
767          0,                     /* src_mask */
768          0,                     /* dst_mask */
769          false),                /* pcrel_offset */
770
771   /* The high bits of a 32-bit displacement from the previous br, bsr, jsr
772      or jmp insn (as tagged by a BRADDR or HINT reloc) to the target; the
773      low bits are supplied by subsequent R_ALPHA_IMMED_LO32 relocs.  */
774   /* XXX: Not implemented.  */
775   HOWTO (R_ALPHA_IMMED_BR_HI32,
776          0,                     /* rightshift */
777          0,                     /* size (0 = byte, 1 = short, 2 = long) */
778          0,                     /* bitsize */
779          false,                 /* pc_relative */
780          0,                     /* bitpos */
781          complain_overflow_dont, /* complain_on_overflow */
782          elf64_alpha_reloc_bad, /* special_function */
783          "IMMED_BR_HI32",               /* name */
784          false,                 /* partial_inplace */
785          0,                     /* src_mask */
786          0,                     /* dst_mask */
787          false),                /* pcrel_offset */
788
789   /* The low 16 bits of a displacement calculated in a previous HI32 reloc.  */
790   /* XXX: Not implemented.  */
791   HOWTO (R_ALPHA_IMMED_LO32,
792          0,                     /* rightshift */
793          0,                     /* size (0 = byte, 1 = short, 2 = long) */
794          0,                     /* bitsize */
795          false,                 /* pc_relative */
796          0,                     /* bitpos */
797          complain_overflow_dont, /* complain_on_overflow */
798          elf64_alpha_reloc_bad, /* special_function */
799          "IMMED_LO32",          /* name */
800          false,                 /* partial_inplace */
801          0,                     /* src_mask */
802          0,                     /* dst_mask */
803          false),                /* pcrel_offset */
804
805   /* Misc ELF relocations.  */
806
807   /* A dynamic relocation to copy the target into our .dynbss section.  */
808   /* Not generated, as all Alpha objects use PIC, so it is not needed.  It
809      is present because every other ELF has one, but should not be used
810      because .dynbss is an ugly thing.  */
811   HOWTO (R_ALPHA_COPY,
812          0,
813          0,
814          0,
815          false,
816          0,
817          complain_overflow_dont,
818          bfd_elf_generic_reloc,
819          "COPY",
820          false,
821          0,
822          0,
823          true),
824
825   /* A dynamic relocation for a .got entry.  */
826   HOWTO (R_ALPHA_GLOB_DAT,
827          0,
828          0,
829          0,
830          false,
831          0,
832          complain_overflow_dont,
833          bfd_elf_generic_reloc,
834          "GLOB_DAT",
835          false,
836          0,
837          0,
838          true),
839
840   /* A dynamic relocation for a .plt entry.  */
841   HOWTO (R_ALPHA_JMP_SLOT,
842          0,
843          0,
844          0,
845          false,
846          0,
847          complain_overflow_dont,
848          bfd_elf_generic_reloc,
849          "JMP_SLOT",
850          false,
851          0,
852          0,
853          true),
854
855   /* A dynamic relocation to add the base of the DSO to a 64-bit field.  */
856   HOWTO (R_ALPHA_RELATIVE,
857          0,
858          0,
859          0,
860          false,
861          0,
862          complain_overflow_dont,
863          bfd_elf_generic_reloc,
864          "RELATIVE",
865          false,
866          0,
867          0,
868          true)
869 };
870
871 /* A relocation function which doesn't do anything.  */
872
873 static bfd_reloc_status_type
874 elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
875      bfd *abfd ATTRIBUTE_UNUSED;
876      arelent *reloc;
877      asymbol *sym ATTRIBUTE_UNUSED;
878      PTR data ATTRIBUTE_UNUSED;
879      asection *sec;
880      bfd *output_bfd;
881      char **error_message ATTRIBUTE_UNUSED;
882 {
883   if (output_bfd)
884     reloc->address += sec->output_offset;
885   return bfd_reloc_ok;
886 }
887
888 /* A relocation function used for an unsupported reloc.  */
889
890 static bfd_reloc_status_type
891 elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
892      bfd *abfd ATTRIBUTE_UNUSED;
893      arelent *reloc;
894      asymbol *sym ATTRIBUTE_UNUSED;
895      PTR data ATTRIBUTE_UNUSED;
896      asection *sec;
897      bfd *output_bfd;
898      char **error_message ATTRIBUTE_UNUSED;
899 {
900   if (output_bfd)
901     reloc->address += sec->output_offset;
902   return bfd_reloc_notsupported;
903 }
904
905 /* Do the work of the GPDISP relocation.  */
906
907 static bfd_reloc_status_type
908 elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
909      bfd *abfd;
910      bfd_vma gpdisp;
911      bfd_byte *p_ldah;
912      bfd_byte *p_lda;
913 {
914   bfd_reloc_status_type ret = bfd_reloc_ok;
915   bfd_vma addend;
916   unsigned long i_ldah, i_lda;
917
918   i_ldah = bfd_get_32 (abfd, p_ldah);
919   i_lda = bfd_get_32 (abfd, p_lda);
920
921   /* Complain if the instructions are not correct.  */
922   if (((i_ldah >> 26) & 0x3f) != 0x09
923       || ((i_lda >> 26) & 0x3f) != 0x08)
924     ret = bfd_reloc_dangerous;
925
926   /* Extract the user-supplied offset, mirroring the sign extensions
927      that the instructions perform.  */
928   addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
929   addend = (addend ^ 0x80008000) - 0x80008000;
930
931   gpdisp += addend;
932
933   if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
934       || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
935     ret = bfd_reloc_overflow;
936
937   /* compensate for the sign extension again.  */
938   i_ldah = ((i_ldah & 0xffff0000)
939             | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
940   i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
941
942   bfd_put_32 (abfd, i_ldah, p_ldah);
943   bfd_put_32 (abfd, i_lda, p_lda);
944
945   return ret;
946 }
947
948 /* The special function for the GPDISP reloc.  */
949
950 static bfd_reloc_status_type
951 elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
952                           output_bfd, err_msg)
953      bfd *abfd;
954      arelent *reloc_entry;
955      asymbol *sym ATTRIBUTE_UNUSED;
956      PTR data;
957      asection *input_section;
958      bfd *output_bfd;
959      char **err_msg;
960 {
961   bfd_reloc_status_type ret;
962   bfd_vma gp, relocation;
963   bfd_byte *p_ldah, *p_lda;
964
965   /* Don't do anything if we're not doing a final link.  */
966   if (output_bfd)
967     {
968       reloc_entry->address += input_section->output_offset;
969       return bfd_reloc_ok;
970     }
971
972   if (reloc_entry->address > input_section->_cooked_size ||
973       reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
974     return bfd_reloc_outofrange;
975
976   /* The gp used in the portion of the output object to which this
977      input object belongs is cached on the input bfd.  */
978   gp = _bfd_get_gp_value (abfd);
979
980   relocation = (input_section->output_section->vma
981                 + input_section->output_offset
982                 + reloc_entry->address);
983
984   p_ldah = (bfd_byte *) data + reloc_entry->address;
985   p_lda = p_ldah + reloc_entry->addend;
986
987   ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
988
989   /* Complain if the instructions are not correct.  */
990   if (ret == bfd_reloc_dangerous)
991     *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
992
993   return ret;
994 }
995
996 /* A mapping from BFD reloc types to Alpha ELF reloc types.  */
997
998 struct elf_reloc_map
999 {
1000   bfd_reloc_code_real_type bfd_reloc_val;
1001   int elf_reloc_val;
1002 };
1003
1004 static const struct elf_reloc_map elf64_alpha_reloc_map[] =
1005 {
1006   {BFD_RELOC_NONE,              R_ALPHA_NONE},
1007   {BFD_RELOC_32,                R_ALPHA_REFLONG},
1008   {BFD_RELOC_64,                R_ALPHA_REFQUAD},
1009   {BFD_RELOC_CTOR,              R_ALPHA_REFQUAD},
1010   {BFD_RELOC_GPREL32,           R_ALPHA_GPREL32},
1011   {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
1012   {BFD_RELOC_ALPHA_LITUSE,      R_ALPHA_LITUSE},
1013   {BFD_RELOC_ALPHA_GPDISP,      R_ALPHA_GPDISP},
1014   {BFD_RELOC_23_PCREL_S2,       R_ALPHA_BRADDR},
1015   {BFD_RELOC_ALPHA_HINT,        R_ALPHA_HINT},
1016   {BFD_RELOC_16_PCREL,          R_ALPHA_SREL16},
1017   {BFD_RELOC_32_PCREL,          R_ALPHA_SREL32},
1018   {BFD_RELOC_64_PCREL,          R_ALPHA_SREL64},
1019
1020 /* The BFD_RELOC_ALPHA_USER_* relocations are used by the assembler to process
1021    the explicit !<reloc>!sequence relocations, and are mapped into the normal
1022    relocations at the end of processing.  */
1023   {BFD_RELOC_ALPHA_USER_LITERAL,        R_ALPHA_LITERAL},
1024   {BFD_RELOC_ALPHA_USER_LITUSE_BASE,    R_ALPHA_LITUSE},
1025   {BFD_RELOC_ALPHA_USER_LITUSE_BYTOFF,  R_ALPHA_LITUSE},
1026   {BFD_RELOC_ALPHA_USER_LITUSE_JSR,     R_ALPHA_LITUSE},
1027   {BFD_RELOC_ALPHA_USER_GPDISP,         R_ALPHA_GPDISP},
1028   {BFD_RELOC_ALPHA_USER_GPRELHIGH,      R_ALPHA_GPRELHIGH},
1029   {BFD_RELOC_ALPHA_USER_GPRELLOW,       R_ALPHA_GPRELLOW},
1030 };
1031
1032 /* Given a BFD reloc type, return a HOWTO structure.  */
1033
1034 static reloc_howto_type *
1035 elf64_alpha_bfd_reloc_type_lookup (abfd, code)
1036      bfd *abfd ATTRIBUTE_UNUSED;
1037      bfd_reloc_code_real_type code;
1038 {
1039   const struct elf_reloc_map *i, *e;
1040   i = e = elf64_alpha_reloc_map;
1041   e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
1042   for (; i != e; ++i)
1043     {
1044       if (i->bfd_reloc_val == code)
1045         return &elf64_alpha_howto_table[i->elf_reloc_val];
1046     }
1047   return 0;
1048 }
1049
1050 /* Given an Alpha ELF reloc type, fill in an arelent structure.  */
1051
1052 static void
1053 elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
1054      bfd *abfd ATTRIBUTE_UNUSED;
1055      arelent *cache_ptr;
1056      Elf64_Internal_Rela *dst;
1057 {
1058   unsigned r_type;
1059
1060   r_type = ELF64_R_TYPE(dst->r_info);
1061   BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1062   cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1063 }
1064 \f
1065 /* These functions do relaxation for Alpha ELF.
1066
1067    Currently I'm only handling what I can do with existing compiler
1068    and assembler support, which means no instructions are removed,
1069    though some may be nopped.  At this time GCC does not emit enough
1070    information to do all of the relaxing that is possible.  It will
1071    take some not small amount of work for that to happen.
1072
1073    There are a couple of interesting papers that I once read on this
1074    subject, that I cannot find references to at the moment, that
1075    related to Alpha in particular.  They are by David Wall, then of
1076    DEC WRL.  */
1077
1078 #define OP_LDA          0x08
1079 #define OP_LDAH         0x09
1080 #define INSN_JSR        0x68004000
1081 #define INSN_JSR_MASK   0xfc00c000
1082 #define OP_LDQ          0x29
1083 #define OP_BR           0x30
1084 #define OP_BSR          0x34
1085 #define INSN_UNOP       0x2fe00000
1086
1087 struct alpha_relax_info
1088 {
1089   bfd *abfd;
1090   asection *sec;
1091   bfd_byte *contents;
1092   Elf_Internal_Rela *relocs, *relend;
1093   struct bfd_link_info *link_info;
1094   boolean changed_contents;
1095   boolean changed_relocs;
1096   bfd_vma gp;
1097   bfd *gotobj;
1098   asection *tsec;
1099   struct alpha_elf_link_hash_entry *h;
1100   struct alpha_elf_got_entry *gotent;
1101   unsigned char other;
1102 };
1103
1104 static Elf_Internal_Rela * elf64_alpha_relax_with_lituse
1105   PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1106           Elf_Internal_Rela *irel, Elf_Internal_Rela *irelend));
1107
1108 static boolean elf64_alpha_relax_without_lituse
1109   PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1110           Elf_Internal_Rela *irel));
1111
1112 static bfd_vma elf64_alpha_relax_opt_call
1113   PARAMS((struct alpha_relax_info *info, bfd_vma symval));
1114
1115 static boolean elf64_alpha_relax_section
1116   PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
1117           boolean *again));
1118
1119 static Elf_Internal_Rela *
1120 elf64_alpha_find_reloc_at_ofs (rel, relend, offset, type)
1121      Elf_Internal_Rela *rel, *relend;
1122      bfd_vma offset;
1123      int type;
1124 {
1125   while (rel < relend)
1126     {
1127       if (rel->r_offset == offset
1128           && ELF64_R_TYPE (rel->r_info) == (unsigned int) type)
1129         return rel;
1130       ++rel;
1131     }
1132   return NULL;
1133 }
1134
1135 static Elf_Internal_Rela *
1136 elf64_alpha_relax_with_lituse (info, symval, irel, irelend)
1137      struct alpha_relax_info *info;
1138      bfd_vma symval;
1139      Elf_Internal_Rela *irel, *irelend;
1140 {
1141   Elf_Internal_Rela *urel;
1142   int flags, count, i;
1143   bfd_signed_vma disp;
1144   boolean fits16;
1145   boolean fits32;
1146   boolean lit_reused = false;
1147   boolean all_optimized = true;
1148   unsigned int lit_insn;
1149
1150   lit_insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1151   if (lit_insn >> 26 != OP_LDQ)
1152     {
1153       ((*_bfd_error_handler)
1154        ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1155         bfd_get_filename (info->abfd), info->sec->name,
1156         (unsigned long)irel->r_offset));
1157       return irel;
1158     }
1159
1160   /* Summarize how this particular LITERAL is used.  */
1161   for (urel = irel+1, flags = count = 0; urel < irelend; ++urel, ++count)
1162     {
1163       if (ELF64_R_TYPE (urel->r_info) != R_ALPHA_LITUSE)
1164         break;
1165       if (urel->r_addend >= 0 && urel->r_addend <= 3)
1166         flags |= 1 << urel->r_addend;
1167     }
1168
1169   /* A little preparation for the loop...  */
1170   disp = symval - info->gp;
1171
1172   for (urel = irel+1, i = 0; i < count; ++i, ++urel)
1173     {
1174       unsigned int insn;
1175       int insn_disp;
1176       bfd_signed_vma xdisp;
1177
1178       insn = bfd_get_32 (info->abfd, info->contents + urel->r_offset);
1179
1180       switch (urel->r_addend)
1181         {
1182         default: /* 0 = ADDRESS FORMAT */
1183           /* This type is really just a placeholder to note that all
1184              uses cannot be optimized, but to still allow some.  */
1185           all_optimized = false;
1186           break;
1187
1188         case 1: /* MEM FORMAT */
1189           /* We can always optimize 16-bit displacements.  */
1190
1191           /* Extract the displacement from the instruction, sign-extending
1192              it if necessary, then test whether it is within 16 or 32 bits
1193              displacement from GP.  */
1194           insn_disp = insn & 0x0000ffff;
1195           if (insn_disp & 0x00008000)
1196             insn_disp |= 0xffff0000;  /* Negative: sign-extend.  */
1197
1198           xdisp = disp + insn_disp;
1199           fits16 = (xdisp >= - (bfd_signed_vma) 0x00008000 && xdisp < 0x00008000);
1200           fits32 = (xdisp >= - (bfd_signed_vma) 0x80000000 && xdisp < 0x7fff8000);
1201
1202           if (fits16)
1203             {
1204               /* Take the op code and dest from this insn, take the base
1205                  register from the literal insn.  Leave the offset alone.  */
1206               insn = (insn & 0xffe0ffff) | (lit_insn & 0x001f0000);
1207               urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1208                                            R_ALPHA_GPRELLOW);
1209               urel->r_addend = irel->r_addend;
1210               info->changed_relocs = true;
1211
1212               bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1213               info->changed_contents = true;
1214             }
1215
1216           /* If all mem+byte, we can optimize 32-bit mem displacements.  */
1217           else if (fits32 && !(flags & ~6))
1218             {
1219               /* FIXME: sanity check that lit insn Ra is mem insn Rb.  */
1220
1221               irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1222                                            R_ALPHA_GPRELHIGH);
1223               lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
1224               bfd_put_32 (info->abfd, lit_insn,
1225                           info->contents + irel->r_offset);
1226               lit_reused = true;
1227               info->changed_contents = true;
1228
1229               urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1230                                            R_ALPHA_GPRELLOW);
1231               urel->r_addend = irel->r_addend;
1232               info->changed_relocs = true;
1233             }
1234           else
1235             all_optimized = false;
1236           break;
1237
1238         case 2: /* BYTE OFFSET FORMAT */
1239           /* We can always optimize byte instructions.  */
1240
1241           /* FIXME: sanity check the insn for byte op.  Check that the
1242              literal dest reg is indeed Rb in the byte insn.  */
1243
1244           insn = (insn & ~0x001ff000) | ((symval & 7) << 13) | 0x1000;
1245
1246           urel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1247           urel->r_addend = 0;
1248           info->changed_relocs = true;
1249
1250           bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1251           info->changed_contents = true;
1252           break;
1253
1254         case 3: /* CALL FORMAT */
1255           {
1256             /* If not zero, place to jump without needing pv.  */
1257             bfd_vma optdest = elf64_alpha_relax_opt_call (info, symval);
1258             bfd_vma org = (info->sec->output_section->vma
1259                            + info->sec->output_offset
1260                            + urel->r_offset + 4);
1261             bfd_signed_vma odisp;
1262
1263             odisp = (optdest ? optdest : symval) - org;
1264             if (odisp >= -0x400000 && odisp < 0x400000)
1265               {
1266                 Elf_Internal_Rela *xrel;
1267
1268                 /* Preserve branch prediction call stack when possible.  */
1269                 if ((insn & INSN_JSR_MASK) == INSN_JSR)
1270                   insn = (OP_BSR << 26) | (insn & 0x03e00000);
1271                 else
1272                   insn = (OP_BR << 26) | (insn & 0x03e00000);
1273
1274                 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1275                                              R_ALPHA_BRADDR);
1276                 urel->r_addend = irel->r_addend;
1277
1278                 if (optdest)
1279                   urel->r_addend += optdest - symval;
1280                 else
1281                   all_optimized = false;
1282
1283                 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1284
1285                 /* Kill any HINT reloc that might exist for this insn.  */
1286                 xrel = (elf64_alpha_find_reloc_at_ofs
1287                         (info->relocs, info->relend, urel->r_offset,
1288                          R_ALPHA_HINT));
1289                 if (xrel)
1290                   xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1291
1292                 info->changed_contents = true;
1293                 info->changed_relocs = true;
1294               }
1295             else
1296               all_optimized = false;
1297
1298             /* ??? If target gp == current gp we can eliminate the gp reload.
1299                This does depend on every place a gp could be reloaded will
1300                be, which currently happens for all code produced by gcc, but
1301                not necessarily by hand-coded assembly, or if sibling calls
1302                are enabled in gcc.
1303
1304                Perhaps conditionalize this on a flag being set in the target
1305                object file's header, and have gcc set it?  */
1306           }
1307           break;
1308         }
1309     }
1310
1311   /* If all cases were optimized, we can reduce the use count on this
1312      got entry by one, possibly eliminating it.  */
1313   if (all_optimized)
1314     {
1315       info->gotent->use_count -= 1;
1316       alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1317       if (!info->h)
1318         alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1319
1320       /* If the literal instruction is no longer needed (it may have been
1321          reused.  We can eliminate it.
1322          ??? For now, I don't want to deal with compacting the section,
1323          so just nop it out.  */
1324       if (!lit_reused)
1325         {
1326           irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1327           info->changed_relocs = true;
1328
1329           bfd_put_32 (info->abfd, INSN_UNOP, info->contents + irel->r_offset);
1330           info->changed_contents = true;
1331         }
1332     }
1333
1334   return irel + count;
1335 }
1336
1337 static bfd_vma
1338 elf64_alpha_relax_opt_call (info, symval)
1339      struct alpha_relax_info *info;
1340      bfd_vma symval;
1341 {
1342   /* If the function has the same gp, and we can identify that the
1343      function does not use its function pointer, we can eliminate the
1344      address load.  */
1345
1346   /* If the symbol is marked NOPV, we are being told the function never
1347      needs its procedure value.  */
1348   if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_NOPV)
1349     return symval;
1350
1351   /* If the symbol is marked STD_GP, we are being told the function does
1352      a normal ldgp in the first two words.  */
1353   else if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_STD_GPLOAD)
1354     ;
1355
1356   /* Otherwise, we may be able to identify a GP load in the first two
1357      words, which we can then skip.  */
1358   else
1359     {
1360       Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
1361       bfd_vma ofs;
1362
1363       /* Load the relocations from the section that the target symbol is in.  */
1364       if (info->sec == info->tsec)
1365         {
1366           tsec_relocs = info->relocs;
1367           tsec_relend = info->relend;
1368           tsec_free = NULL;
1369         }
1370       else
1371         {
1372           tsec_relocs = (_bfd_elf64_link_read_relocs
1373                          (info->abfd, info->tsec, (PTR) NULL,
1374                          (Elf_Internal_Rela *) NULL,
1375                          info->link_info->keep_memory));
1376           if (tsec_relocs == NULL)
1377             return 0;
1378           tsec_relend = tsec_relocs + info->tsec->reloc_count;
1379           tsec_free = (info->link_info->keep_memory ? NULL : tsec_relocs);
1380         }
1381
1382       /* Recover the symbol's offset within the section.  */
1383       ofs = (symval - info->tsec->output_section->vma
1384              - info->tsec->output_offset);
1385
1386       /* Look for a GPDISP reloc.  */
1387       gpdisp = (elf64_alpha_find_reloc_at_ofs
1388                 (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
1389
1390       if (!gpdisp || gpdisp->r_addend != 4)
1391         {
1392           if (tsec_free)
1393             free (tsec_free);
1394           return 0;
1395         }
1396       if (tsec_free)
1397         free (tsec_free);
1398     }
1399
1400   /* We've now determined that we can skip an initial gp load.  Verify
1401      that the call and the target use the same gp.   */
1402   if (info->link_info->hash->creator != info->tsec->owner->xvec
1403       || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
1404     return 0;
1405
1406   return symval + 8;
1407 }
1408
1409 static boolean
1410 elf64_alpha_relax_without_lituse (info, symval, irel)
1411      struct alpha_relax_info *info;
1412      bfd_vma symval;
1413      Elf_Internal_Rela *irel;
1414 {
1415   unsigned int insn;
1416   bfd_signed_vma disp;
1417
1418   /* Get the instruction.  */
1419   insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1420
1421   if (insn >> 26 != OP_LDQ)
1422     {
1423       ((*_bfd_error_handler)
1424        ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1425         bfd_get_filename (info->abfd), info->sec->name,
1426         (unsigned long) irel->r_offset));
1427       return true;
1428     }
1429
1430   /* So we aren't told much.  Do what we can with the address load and
1431      fake the rest.  All of the optimizations here require that the
1432      offset from the GP fit in 16 bits.  */
1433
1434   disp = symval - info->gp;
1435   if (disp < -0x8000 || disp >= 0x8000)
1436     return true;
1437
1438   /* On the LITERAL instruction itself, consider exchanging
1439      `ldq R,X(gp)' for `lda R,Y(gp)'.  */
1440
1441   insn = (OP_LDA << 26) | (insn & 0x03ff0000);
1442   bfd_put_32 (info->abfd, insn, info->contents + irel->r_offset);
1443   info->changed_contents = true;
1444
1445   irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), R_ALPHA_GPRELLOW);
1446   info->changed_relocs = true;
1447
1448   /* Reduce the use count on this got entry by one, possibly
1449      eliminating it.  */
1450   info->gotent->use_count -= 1;
1451   alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1452   if (!info->h)
1453     alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1454
1455   /* ??? Search forward through this basic block looking for insns
1456      that use the target register.  Stop after an insn modifying the
1457      register is seen, or after a branch or call.
1458
1459      Any such memory load insn may be substituted by a load directly
1460      off the GP.  This allows the memory load insn to be issued before
1461      the calculated GP register would otherwise be ready.
1462
1463      Any such jsr insn can be replaced by a bsr if it is in range.
1464
1465      This would mean that we'd have to _add_ relocations, the pain of
1466      which gives one pause.  */
1467
1468   return true;
1469 }
1470
1471 static boolean
1472 elf64_alpha_relax_section (abfd, sec, link_info, again)
1473      bfd *abfd;
1474      asection *sec;
1475      struct bfd_link_info *link_info;
1476      boolean *again;
1477 {
1478   Elf_Internal_Shdr *symtab_hdr;
1479   Elf_Internal_Rela *internal_relocs;
1480   Elf_Internal_Rela *free_relocs = NULL;
1481   Elf_Internal_Rela *irel, *irelend;
1482   bfd_byte *free_contents = NULL;
1483   Elf64_External_Sym *extsyms = NULL;
1484   Elf64_External_Sym *free_extsyms = NULL;
1485   struct alpha_elf_got_entry **local_got_entries;
1486   struct alpha_relax_info info;
1487
1488   /* We are not currently changing any sizes, so only one pass.  */
1489   *again = false;
1490
1491   if (link_info->relocateable
1492       || (sec->flags & SEC_RELOC) == 0
1493       || sec->reloc_count == 0)
1494     return true;
1495
1496   /* If this is the first time we have been called for this section,
1497      initialize the cooked size.  */
1498   if (sec->_cooked_size == 0)
1499     sec->_cooked_size = sec->_raw_size;
1500
1501   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1502   local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1503
1504   /* Load the relocations for this section.  */
1505   internal_relocs = (_bfd_elf64_link_read_relocs
1506                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1507                       link_info->keep_memory));
1508   if (internal_relocs == NULL)
1509     goto error_return;
1510   if (! link_info->keep_memory)
1511     free_relocs = internal_relocs;
1512
1513   memset(&info, 0, sizeof (info));
1514   info.abfd = abfd;
1515   info.sec = sec;
1516   info.link_info = link_info;
1517   info.relocs = internal_relocs;
1518   info.relend = irelend = internal_relocs + sec->reloc_count;
1519
1520   /* Find the GP for this object.  */
1521   info.gotobj = alpha_elf_tdata (abfd)->gotobj;
1522   if (info.gotobj)
1523     {
1524       asection *sgot = alpha_elf_tdata (info.gotobj)->got;
1525       info.gp = _bfd_get_gp_value (info.gotobj);
1526       if (info.gp == 0)
1527         {
1528           info.gp = (sgot->output_section->vma
1529                      + sgot->output_offset
1530                      + 0x8000);
1531           _bfd_set_gp_value (info.gotobj, info.gp);
1532         }
1533     }
1534
1535   for (irel = internal_relocs; irel < irelend; irel++)
1536     {
1537       bfd_vma symval;
1538       Elf_Internal_Sym isym;
1539       struct alpha_elf_got_entry *gotent;
1540
1541       if (ELF64_R_TYPE (irel->r_info) != (int) R_ALPHA_LITERAL)
1542         continue;
1543
1544       /* Get the section contents.  */
1545       if (info.contents == NULL)
1546         {
1547           if (elf_section_data (sec)->this_hdr.contents != NULL)
1548             info.contents = elf_section_data (sec)->this_hdr.contents;
1549           else
1550             {
1551               info.contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1552               if (info.contents == NULL)
1553                 goto error_return;
1554               free_contents = info.contents;
1555
1556               if (! bfd_get_section_contents (abfd, sec, info.contents,
1557                                               (file_ptr) 0, sec->_raw_size))
1558                 goto error_return;
1559             }
1560         }
1561
1562       /* Read this BFD's symbols if we haven't done so already.  */
1563       if (extsyms == NULL)
1564         {
1565           if (symtab_hdr->contents != NULL)
1566             extsyms = (Elf64_External_Sym *) symtab_hdr->contents;
1567           else
1568             {
1569               extsyms = ((Elf64_External_Sym *)
1570                          bfd_malloc (symtab_hdr->sh_size));
1571               if (extsyms == NULL)
1572                 goto error_return;
1573               free_extsyms = extsyms;
1574               if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1575                   || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1576                       != symtab_hdr->sh_size))
1577                 goto error_return;
1578             }
1579         }
1580
1581       /* Get the value of the symbol referred to by the reloc.  */
1582       if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1583         {
1584           /* A local symbol.  */
1585           bfd_elf64_swap_symbol_in (abfd,
1586                                     extsyms + ELF64_R_SYM (irel->r_info),
1587                                     &isym);
1588           if (isym.st_shndx == SHN_UNDEF)
1589             info.tsec = bfd_und_section_ptr;
1590           else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1591             info.tsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1592           else if (isym.st_shndx == SHN_ABS)
1593             info.tsec = bfd_abs_section_ptr;
1594           else if (isym.st_shndx == SHN_COMMON)
1595             info.tsec = bfd_com_section_ptr;
1596           else
1597             continue;   /* who knows.  */
1598
1599           info.h = NULL;
1600           info.other = isym.st_other;
1601           gotent = local_got_entries[ELF64_R_SYM(irel->r_info)];
1602           symval = isym.st_value;
1603         }
1604       else
1605         {
1606           unsigned long indx;
1607           struct alpha_elf_link_hash_entry *h;
1608
1609           indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1610           h = alpha_elf_sym_hashes (abfd)[indx];
1611           BFD_ASSERT (h != NULL);
1612
1613           while (h->root.root.type == bfd_link_hash_indirect
1614                  || h->root.root.type == bfd_link_hash_warning)
1615             h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
1616
1617           /* We can't do anthing with undefined or dynamic symbols.  */
1618           if (h->root.root.type == bfd_link_hash_undefined
1619               || h->root.root.type == bfd_link_hash_undefweak
1620               || alpha_elf_dynamic_symbol_p (&h->root, link_info))
1621             continue;
1622
1623           info.h = h;
1624           info.tsec = h->root.root.u.def.section;
1625           info.other = h->root.other;
1626           gotent = h->got_entries;
1627           symval = h->root.root.u.def.value;
1628         }
1629
1630       /* Search for the got entry to be used by this relocation.  */
1631       while (gotent->gotobj != info.gotobj || gotent->addend != irel->r_addend)
1632         gotent = gotent->next;
1633       info.gotent = gotent;
1634
1635       symval += info.tsec->output_section->vma + info.tsec->output_offset;
1636       symval += irel->r_addend;
1637
1638       BFD_ASSERT(info.gotent != NULL);
1639
1640       /* If there exist LITUSE relocations immediately following, this
1641          opens up all sorts of interesting optimizations, because we
1642          now know every location that this address load is used.  */
1643
1644       if (irel+1 < irelend && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
1645         {
1646           irel = elf64_alpha_relax_with_lituse (&info, symval, irel, irelend);
1647           if (irel == NULL)
1648             goto error_return;
1649         }
1650       else
1651         {
1652           if (!elf64_alpha_relax_without_lituse (&info, symval, irel))
1653             goto error_return;
1654         }
1655     }
1656
1657   if (!elf64_alpha_size_got_sections (abfd, link_info))
1658     return false;
1659
1660   if (info.changed_relocs)
1661     {
1662       elf_section_data (sec)->relocs = internal_relocs;
1663     }
1664   else if (free_relocs != NULL)
1665     {
1666       free (free_relocs);
1667     }
1668
1669   if (info.changed_contents)
1670     {
1671       elf_section_data (sec)->this_hdr.contents = info.contents;
1672     }
1673   else if (free_contents != NULL)
1674     {
1675       if (! link_info->keep_memory)
1676         free (free_contents);
1677       else
1678         {
1679           /* Cache the section contents for elf_link_input_bfd.  */
1680           elf_section_data (sec)->this_hdr.contents = info.contents;
1681         }
1682     }
1683
1684   if (free_extsyms != NULL)
1685     {
1686       if (! link_info->keep_memory)
1687         free (free_extsyms);
1688       else
1689         {
1690           /* Cache the symbols for elf_link_input_bfd.  */
1691           symtab_hdr->contents = extsyms;
1692         }
1693     }
1694
1695   *again = info.changed_contents || info.changed_relocs;
1696
1697   return true;
1698
1699  error_return:
1700   if (free_relocs != NULL)
1701     free (free_relocs);
1702   if (free_contents != NULL)
1703     free (free_contents);
1704   if (free_extsyms != NULL)
1705     free (free_extsyms);
1706   return false;
1707 }
1708 \f
1709 /* PLT/GOT Stuff */
1710 #define PLT_HEADER_SIZE 32
1711 #define PLT_HEADER_WORD1        0xc3600000      /* br   $27,.+4     */
1712 #define PLT_HEADER_WORD2        0xa77b000c      /* ldq  $27,12($27) */
1713 #define PLT_HEADER_WORD3        0x47ff041f      /* nop              */
1714 #define PLT_HEADER_WORD4        0x6b7b0000      /* jmp  $27,($27)   */
1715
1716 #define PLT_ENTRY_SIZE 12
1717 #define PLT_ENTRY_WORD1         0xc3800000      /* br   $28, plt0   */
1718 #define PLT_ENTRY_WORD2         0
1719 #define PLT_ENTRY_WORD3         0
1720
1721 #define MAX_GOT_ENTRIES         (64*1024 / 8)
1722
1723 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
1724 \f
1725 /* Handle an Alpha specific section when reading an object file.  This
1726    is called when elfcode.h finds a section with an unknown type.
1727    FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
1728    how to.  */
1729
1730 static boolean
1731 elf64_alpha_section_from_shdr (abfd, hdr, name)
1732      bfd *abfd;
1733      Elf64_Internal_Shdr *hdr;
1734      char *name;
1735 {
1736   asection *newsect;
1737
1738   /* There ought to be a place to keep ELF backend specific flags, but
1739      at the moment there isn't one.  We just keep track of the
1740      sections by their name, instead.  Fortunately, the ABI gives
1741      suggested names for all the MIPS specific sections, so we will
1742      probably get away with this.  */
1743   switch (hdr->sh_type)
1744     {
1745     case SHT_ALPHA_DEBUG:
1746       if (strcmp (name, ".mdebug") != 0)
1747         return false;
1748       break;
1749 #ifdef ERIC_neverdef
1750     case SHT_ALPHA_REGINFO:
1751       if (strcmp (name, ".reginfo") != 0
1752           || hdr->sh_size != sizeof (Elf64_External_RegInfo))
1753         return false;
1754       break;
1755 #endif
1756     default:
1757       return false;
1758     }
1759
1760   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1761     return false;
1762   newsect = hdr->bfd_section;
1763
1764   if (hdr->sh_type == SHT_ALPHA_DEBUG)
1765     {
1766       if (! bfd_set_section_flags (abfd, newsect,
1767                                    (bfd_get_section_flags (abfd, newsect)
1768                                     | SEC_DEBUGGING)))
1769         return false;
1770     }
1771
1772 #ifdef ERIC_neverdef
1773   /* For a .reginfo section, set the gp value in the tdata information
1774      from the contents of this section.  We need the gp value while
1775      processing relocs, so we just get it now.  */
1776   if (hdr->sh_type == SHT_ALPHA_REGINFO)
1777     {
1778       Elf64_External_RegInfo ext;
1779       Elf64_RegInfo s;
1780
1781       if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1782                                       (file_ptr) 0, sizeof ext))
1783         return false;
1784       bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
1785       elf_gp (abfd) = s.ri_gp_value;
1786     }
1787 #endif
1788
1789   return true;
1790 }
1791
1792 /* Set the correct type for an Alpha ELF section.  We do this by the
1793    section name, which is a hack, but ought to work.  */
1794
1795 static boolean
1796 elf64_alpha_fake_sections (abfd, hdr, sec)
1797      bfd *abfd;
1798      Elf64_Internal_Shdr *hdr;
1799      asection *sec;
1800 {
1801   register const char *name;
1802
1803   name = bfd_get_section_name (abfd, sec);
1804
1805   if (strcmp (name, ".mdebug") == 0)
1806     {
1807       hdr->sh_type = SHT_ALPHA_DEBUG;
1808       /* In a shared object on Irix 5.3, the .mdebug section has an
1809          entsize of 0.  FIXME: Does this matter?  */
1810       if ((abfd->flags & DYNAMIC) != 0 )
1811         hdr->sh_entsize = 0;
1812       else
1813         hdr->sh_entsize = 1;
1814     }
1815 #ifdef ERIC_neverdef
1816   else if (strcmp (name, ".reginfo") == 0)
1817     {
1818       hdr->sh_type = SHT_ALPHA_REGINFO;
1819       /* In a shared object on Irix 5.3, the .reginfo section has an
1820          entsize of 0x18.  FIXME: Does this matter?  */
1821       if ((abfd->flags & DYNAMIC) != 0)
1822         hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
1823       else
1824         hdr->sh_entsize = 1;
1825
1826       /* Force the section size to the correct value, even if the
1827          linker thinks it is larger.  The link routine below will only
1828          write out this much data for .reginfo.  */
1829       hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
1830     }
1831   else if (strcmp (name, ".hash") == 0
1832            || strcmp (name, ".dynamic") == 0
1833            || strcmp (name, ".dynstr") == 0)
1834     {
1835       hdr->sh_entsize = 0;
1836       hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
1837     }
1838 #endif
1839   else if (strcmp (name, ".sdata") == 0
1840            || strcmp (name, ".sbss") == 0
1841            || strcmp (name, ".lit4") == 0
1842            || strcmp (name, ".lit8") == 0)
1843     hdr->sh_flags |= SHF_ALPHA_GPREL;
1844
1845   return true;
1846 }
1847
1848 /* Hook called by the linker routine which adds symbols from an object
1849    file.  We use it to put .comm items in .sbss, and not .bss.  */
1850
1851 static boolean
1852 elf64_alpha_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1853      bfd *abfd;
1854      struct bfd_link_info *info;
1855      const Elf_Internal_Sym *sym;
1856      const char **namep ATTRIBUTE_UNUSED;
1857      flagword *flagsp ATTRIBUTE_UNUSED;
1858      asection **secp;
1859      bfd_vma *valp;
1860 {
1861   if (sym->st_shndx == SHN_COMMON
1862       && !info->relocateable
1863       && sym->st_size <= elf_gp_size (abfd))
1864     {
1865       /* Common symbols less than or equal to -G nn bytes are
1866          automatically put into .sbss.  */
1867
1868       asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1869
1870       if (scomm == NULL)
1871         {
1872           scomm = bfd_make_section (abfd, ".scommon");
1873           if (scomm == NULL
1874               || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
1875                                                        | SEC_IS_COMMON
1876                                                        | SEC_LINKER_CREATED)))
1877             return false;
1878         }
1879
1880       *secp = scomm;
1881       *valp = sym->st_size;
1882     }
1883
1884   return true;
1885 }
1886
1887 /* Create the .got section.  */
1888
1889 static boolean
1890 elf64_alpha_create_got_section(abfd, info)
1891      bfd *abfd;
1892      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1893 {
1894   asection *s;
1895
1896   if (bfd_get_section_by_name (abfd, ".got"))
1897     return true;
1898
1899   s = bfd_make_section (abfd, ".got");
1900   if (s == NULL
1901       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1902                                            | SEC_HAS_CONTENTS
1903                                            | SEC_IN_MEMORY
1904                                            | SEC_LINKER_CREATED))
1905       || !bfd_set_section_alignment (abfd, s, 3))
1906     return false;
1907
1908   alpha_elf_tdata (abfd)->got = s;
1909
1910   return true;
1911 }
1912
1913 /* Create all the dynamic sections.  */
1914
1915 static boolean
1916 elf64_alpha_create_dynamic_sections (abfd, info)
1917      bfd *abfd;
1918      struct bfd_link_info *info;
1919 {
1920   asection *s;
1921   struct elf_link_hash_entry *h;
1922
1923   /* We need to create .plt, .rela.plt, .got, and .rela.got sections.  */
1924
1925   s = bfd_make_section (abfd, ".plt");
1926   if (s == NULL
1927       || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1928                                             | SEC_HAS_CONTENTS
1929                                             | SEC_IN_MEMORY
1930                                             | SEC_LINKER_CREATED
1931                                             | SEC_CODE))
1932       || ! bfd_set_section_alignment (abfd, s, 3))
1933     return false;
1934
1935   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1936      .plt section.  */
1937   h = NULL;
1938   if (! (_bfd_generic_link_add_one_symbol
1939          (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1940           (bfd_vma) 0, (const char *) NULL, false,
1941           get_elf_backend_data (abfd)->collect,
1942           (struct bfd_link_hash_entry **) &h)))
1943     return false;
1944   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1945   h->type = STT_OBJECT;
1946
1947   if (info->shared
1948       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1949     return false;
1950
1951   s = bfd_make_section (abfd, ".rela.plt");
1952   if (s == NULL
1953       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1954                                            | SEC_HAS_CONTENTS
1955                                            | SEC_IN_MEMORY
1956                                            | SEC_LINKER_CREATED
1957                                            | SEC_READONLY))
1958       || ! bfd_set_section_alignment (abfd, s, 3))
1959     return false;
1960
1961   /* We may or may not have created a .got section for this object, but
1962      we definitely havn't done the rest of the work.  */
1963
1964   if (!elf64_alpha_create_got_section (abfd, info))
1965     return false;
1966
1967   s = bfd_make_section(abfd, ".rela.got");
1968   if (s == NULL
1969       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1970                                            | SEC_HAS_CONTENTS
1971                                            | SEC_IN_MEMORY
1972                                            | SEC_LINKER_CREATED
1973                                            | SEC_READONLY))
1974       || !bfd_set_section_alignment (abfd, s, 3))
1975     return false;
1976
1977   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1978      dynobj's .got section.  We don't do this in the linker script
1979      because we don't want to define the symbol if we are not creating
1980      a global offset table.  */
1981   h = NULL;
1982   if (!(_bfd_generic_link_add_one_symbol
1983         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
1984          alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
1985          false, get_elf_backend_data (abfd)->collect,
1986          (struct bfd_link_hash_entry **) &h)))
1987     return false;
1988   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1989   h->type = STT_OBJECT;
1990
1991   if (info->shared
1992       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1993     return false;
1994
1995   elf_hash_table (info)->hgot = h;
1996
1997   return true;
1998 }
1999 \f
2000 /* Read ECOFF debugging information from a .mdebug section into a
2001    ecoff_debug_info structure.  */
2002
2003 static boolean
2004 elf64_alpha_read_ecoff_info (abfd, section, debug)
2005      bfd *abfd;
2006      asection *section;
2007      struct ecoff_debug_info *debug;
2008 {
2009   HDRR *symhdr;
2010   const struct ecoff_debug_swap *swap;
2011   char *ext_hdr = NULL;
2012
2013   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2014   memset (debug, 0, sizeof (*debug));
2015
2016   ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
2017   if (ext_hdr == NULL && swap->external_hdr_size != 0)
2018     goto error_return;
2019
2020   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2021                                 swap->external_hdr_size)
2022       == false)
2023     goto error_return;
2024
2025   symhdr = &debug->symbolic_header;
2026   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2027
2028   /* The symbolic header contains absolute file offsets and sizes to
2029      read.  */
2030 #define READ(ptr, offset, count, size, type)                            \
2031   if (symhdr->count == 0)                                               \
2032     debug->ptr = NULL;                                                  \
2033   else                                                                  \
2034     {                                                                   \
2035       debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2036       if (debug->ptr == NULL)                                           \
2037         goto error_return;                                              \
2038       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0     \
2039           || (bfd_read (debug->ptr, size, symhdr->count,                \
2040                         abfd) != size * symhdr->count))                 \
2041         goto error_return;                                              \
2042     }
2043
2044   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2045   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2046   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2047   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2048   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2049   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2050         union aux_ext *);
2051   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2052   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2053   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2054   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2055   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2056 #undef READ
2057
2058   debug->fdr = NULL;
2059   debug->adjust = NULL;
2060
2061   return true;
2062
2063  error_return:
2064   if (ext_hdr != NULL)
2065     free (ext_hdr);
2066   if (debug->line != NULL)
2067     free (debug->line);
2068   if (debug->external_dnr != NULL)
2069     free (debug->external_dnr);
2070   if (debug->external_pdr != NULL)
2071     free (debug->external_pdr);
2072   if (debug->external_sym != NULL)
2073     free (debug->external_sym);
2074   if (debug->external_opt != NULL)
2075     free (debug->external_opt);
2076   if (debug->external_aux != NULL)
2077     free (debug->external_aux);
2078   if (debug->ss != NULL)
2079     free (debug->ss);
2080   if (debug->ssext != NULL)
2081     free (debug->ssext);
2082   if (debug->external_fdr != NULL)
2083     free (debug->external_fdr);
2084   if (debug->external_rfd != NULL)
2085     free (debug->external_rfd);
2086   if (debug->external_ext != NULL)
2087     free (debug->external_ext);
2088   return false;
2089 }
2090
2091 /* Alpha ELF local labels start with '$'.  */
2092
2093 static boolean
2094 elf64_alpha_is_local_label_name (abfd, name)
2095      bfd *abfd ATTRIBUTE_UNUSED;
2096      const char *name;
2097 {
2098   return name[0] == '$';
2099 }
2100
2101 /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
2102    routine in order to handle the ECOFF debugging information.  We
2103    still call this mips_elf_find_line because of the slot
2104    find_line_info in elf_obj_tdata is declared that way.  */
2105
2106 struct mips_elf_find_line
2107 {
2108   struct ecoff_debug_info d;
2109   struct ecoff_find_line i;
2110 };
2111
2112 static boolean
2113 elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2114                                functionname_ptr, line_ptr)
2115      bfd *abfd;
2116      asection *section;
2117      asymbol **symbols;
2118      bfd_vma offset;
2119      const char **filename_ptr;
2120      const char **functionname_ptr;
2121      unsigned int *line_ptr;
2122 {
2123   asection *msec;
2124
2125   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
2126                                      filename_ptr, functionname_ptr,
2127                                      line_ptr, 0,
2128                                      &elf_tdata (abfd)->dwarf2_find_line_info))
2129     return true;
2130
2131   msec = bfd_get_section_by_name (abfd, ".mdebug");
2132   if (msec != NULL)
2133     {
2134       flagword origflags;
2135       struct mips_elf_find_line *fi;
2136       const struct ecoff_debug_swap * const swap =
2137         get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2138
2139       /* If we are called during a link, alpha_elf_final_link may have
2140          cleared the SEC_HAS_CONTENTS field.  We force it back on here
2141          if appropriate (which it normally will be).  */
2142       origflags = msec->flags;
2143       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2144         msec->flags |= SEC_HAS_CONTENTS;
2145
2146       fi = elf_tdata (abfd)->find_line_info;
2147       if (fi == NULL)
2148         {
2149           bfd_size_type external_fdr_size;
2150           char *fraw_src;
2151           char *fraw_end;
2152           struct fdr *fdr_ptr;
2153
2154           fi = ((struct mips_elf_find_line *)
2155                 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
2156           if (fi == NULL)
2157             {
2158               msec->flags = origflags;
2159               return false;
2160             }
2161
2162           if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
2163             {
2164               msec->flags = origflags;
2165               return false;
2166             }
2167
2168           /* Swap in the FDR information.  */
2169           fi->d.fdr = ((struct fdr *)
2170                        bfd_alloc (abfd,
2171                                   (fi->d.symbolic_header.ifdMax *
2172                                    sizeof (struct fdr))));
2173           if (fi->d.fdr == NULL)
2174             {
2175               msec->flags = origflags;
2176               return false;
2177             }
2178           external_fdr_size = swap->external_fdr_size;
2179           fdr_ptr = fi->d.fdr;
2180           fraw_src = (char *) fi->d.external_fdr;
2181           fraw_end = (fraw_src
2182                       + fi->d.symbolic_header.ifdMax * external_fdr_size);
2183           for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2184             (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2185
2186           elf_tdata (abfd)->find_line_info = fi;
2187
2188           /* Note that we don't bother to ever free this information.
2189              find_nearest_line is either called all the time, as in
2190              objdump -l, so the information should be saved, or it is
2191              rarely called, as in ld error messages, so the memory
2192              wasted is unimportant.  Still, it would probably be a
2193              good idea for free_cached_info to throw it away.  */
2194         }
2195
2196       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2197                                   &fi->i, filename_ptr, functionname_ptr,
2198                                   line_ptr))
2199         {
2200           msec->flags = origflags;
2201           return true;
2202         }
2203
2204       msec->flags = origflags;
2205     }
2206
2207   /* Fall back on the generic ELF find_nearest_line routine.  */
2208
2209   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2210                                      filename_ptr, functionname_ptr,
2211                                      line_ptr);
2212 }
2213 \f
2214 /* Structure used to pass information to alpha_elf_output_extsym.  */
2215
2216 struct extsym_info
2217 {
2218   bfd *abfd;
2219   struct bfd_link_info *info;
2220   struct ecoff_debug_info *debug;
2221   const struct ecoff_debug_swap *swap;
2222   boolean failed;
2223 };
2224
2225 static boolean
2226 elf64_alpha_output_extsym (h, data)
2227      struct alpha_elf_link_hash_entry *h;
2228      PTR data;
2229 {
2230   struct extsym_info *einfo = (struct extsym_info *) data;
2231   boolean strip;
2232   asection *sec, *output_section;
2233
2234   if (h->root.indx == -2)
2235     strip = false;
2236   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2237            || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2238           && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2239           && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2240     strip = true;
2241   else if (einfo->info->strip == strip_all
2242           || (einfo->info->strip == strip_some
2243               && bfd_hash_lookup (einfo->info->keep_hash,
2244                                   h->root.root.root.string,
2245                                   false, false) == NULL))
2246     strip = true;
2247   else
2248     strip = false;
2249
2250   if (strip)
2251     return true;
2252
2253   if (h->esym.ifd == -2)
2254     {
2255       h->esym.jmptbl = 0;
2256       h->esym.cobol_main = 0;
2257       h->esym.weakext = 0;
2258       h->esym.reserved = 0;
2259       h->esym.ifd = ifdNil;
2260       h->esym.asym.value = 0;
2261       h->esym.asym.st = stGlobal;
2262
2263       if (h->root.root.type != bfd_link_hash_defined
2264          && h->root.root.type != bfd_link_hash_defweak)
2265        h->esym.asym.sc = scAbs;
2266       else
2267        {
2268          const char *name;
2269
2270          sec = h->root.root.u.def.section;
2271          output_section = sec->output_section;
2272
2273          /* When making a shared library and symbol h is the one from
2274             the another shared library, OUTPUT_SECTION may be null.  */
2275          if (output_section == NULL)
2276            h->esym.asym.sc = scUndefined;
2277          else
2278            {
2279              name = bfd_section_name (output_section->owner, output_section);
2280
2281              if (strcmp (name, ".text") == 0)
2282                h->esym.asym.sc = scText;
2283              else if (strcmp (name, ".data") == 0)
2284                h->esym.asym.sc = scData;
2285              else if (strcmp (name, ".sdata") == 0)
2286                h->esym.asym.sc = scSData;
2287              else if (strcmp (name, ".rodata") == 0
2288                       || strcmp (name, ".rdata") == 0)
2289                h->esym.asym.sc = scRData;
2290              else if (strcmp (name, ".bss") == 0)
2291                h->esym.asym.sc = scBss;
2292              else if (strcmp (name, ".sbss") == 0)
2293                h->esym.asym.sc = scSBss;
2294              else if (strcmp (name, ".init") == 0)
2295                h->esym.asym.sc = scInit;
2296              else if (strcmp (name, ".fini") == 0)
2297                h->esym.asym.sc = scFini;
2298              else
2299                h->esym.asym.sc = scAbs;
2300            }
2301        }
2302
2303       h->esym.asym.reserved = 0;
2304       h->esym.asym.index = indexNil;
2305     }
2306
2307   if (h->root.root.type == bfd_link_hash_common)
2308     h->esym.asym.value = h->root.root.u.c.size;
2309   else if (h->root.root.type == bfd_link_hash_defined
2310            || h->root.root.type == bfd_link_hash_defweak)
2311     {
2312       if (h->esym.asym.sc == scCommon)
2313        h->esym.asym.sc = scBss;
2314       else if (h->esym.asym.sc == scSCommon)
2315        h->esym.asym.sc = scSBss;
2316
2317       sec = h->root.root.u.def.section;
2318       output_section = sec->output_section;
2319       if (output_section != NULL)
2320        h->esym.asym.value = (h->root.root.u.def.value
2321                              + sec->output_offset
2322                              + output_section->vma);
2323       else
2324        h->esym.asym.value = 0;
2325     }
2326   else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2327     {
2328       /* Set type and value for a symbol with a function stub.  */
2329       h->esym.asym.st = stProc;
2330       sec = bfd_get_section_by_name (einfo->abfd, ".plt");
2331       if (sec == NULL)
2332         h->esym.asym.value = 0;
2333       else
2334         {
2335           output_section = sec->output_section;
2336           if (output_section != NULL)
2337             h->esym.asym.value = (h->root.plt.offset
2338                                   + sec->output_offset
2339                                   + output_section->vma);
2340           else
2341             h->esym.asym.value = 0;
2342         }
2343 #if 0 /* FIXME?  */
2344       h->esym.ifd = 0;
2345 #endif
2346     }
2347
2348   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2349                                      h->root.root.root.string,
2350                                      &h->esym))
2351     {
2352       einfo->failed = true;
2353       return false;
2354     }
2355
2356   return true;
2357 }
2358
2359 /* FIXME:  Create a runtime procedure table from the .mdebug section.
2360
2361 static boolean
2362 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
2363      PTR handle;
2364      bfd *abfd;
2365      struct bfd_link_info *info;
2366      asection *s;
2367      struct ecoff_debug_info *debug;
2368 */
2369 \f
2370 /* Handle dynamic relocations when doing an Alpha ELF link.  */
2371
2372 static boolean
2373 elf64_alpha_check_relocs (abfd, info, sec, relocs)
2374      bfd *abfd;
2375      struct bfd_link_info *info;
2376      asection *sec;
2377      const Elf_Internal_Rela *relocs;
2378 {
2379   bfd *dynobj;
2380   asection *sreloc;
2381   const char *rel_sec_name;
2382   Elf_Internal_Shdr *symtab_hdr;
2383   struct alpha_elf_link_hash_entry **sym_hashes;
2384   struct alpha_elf_got_entry **local_got_entries;
2385   const Elf_Internal_Rela *rel, *relend;
2386   int got_created;
2387
2388   if (info->relocateable)
2389     return true;
2390
2391   dynobj = elf_hash_table(info)->dynobj;
2392   if (dynobj == NULL)
2393     elf_hash_table(info)->dynobj = dynobj = abfd;
2394
2395   sreloc = NULL;
2396   rel_sec_name = NULL;
2397   symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2398   sym_hashes = alpha_elf_sym_hashes(abfd);
2399   local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2400   got_created = 0;
2401
2402   relend = relocs + sec->reloc_count;
2403   for (rel = relocs; rel < relend; ++rel)
2404     {
2405       unsigned long r_symndx, r_type;
2406       struct alpha_elf_link_hash_entry *h;
2407
2408       r_symndx = ELF64_R_SYM (rel->r_info);
2409       if (r_symndx < symtab_hdr->sh_info)
2410         h = NULL;
2411       else
2412         {
2413           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2414
2415           while (h->root.root.type == bfd_link_hash_indirect
2416                  || h->root.root.type == bfd_link_hash_warning)
2417             h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2418
2419           h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2420         }
2421       r_type = ELF64_R_TYPE (rel->r_info);
2422
2423       switch (r_type)
2424         {
2425         case R_ALPHA_LITERAL:
2426           {
2427             struct alpha_elf_got_entry *gotent;
2428             int flags = 0;
2429
2430             if (h)
2431               {
2432                 /* Search for and possibly create a got entry.  */
2433                 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2434                   if (gotent->gotobj == abfd &&
2435                       gotent->addend == rel->r_addend)
2436                     break;
2437
2438                 if (!gotent)
2439                   {
2440                     gotent = ((struct alpha_elf_got_entry *)
2441                               bfd_alloc (abfd,
2442                                          sizeof (struct alpha_elf_got_entry)));
2443                     if (!gotent)
2444                       return false;
2445
2446                     gotent->gotobj = abfd;
2447                     gotent->addend = rel->r_addend;
2448                     gotent->got_offset = -1;
2449                     gotent->flags = 0;
2450                     gotent->use_count = 1;
2451
2452                     gotent->next = h->got_entries;
2453                     h->got_entries = gotent;
2454
2455                     alpha_elf_tdata (abfd)->total_got_entries++;
2456                   }
2457                 else
2458                   gotent->use_count += 1;
2459               }
2460             else
2461               {
2462                 /* This is a local .got entry -- record for merge.  */
2463                 if (!local_got_entries)
2464                   {
2465                     size_t size;
2466                     size = (symtab_hdr->sh_info
2467                             * sizeof (struct alpha_elf_got_entry *));
2468
2469                     local_got_entries = ((struct alpha_elf_got_entry **)
2470                                          bfd_alloc (abfd, size));
2471                     if (!local_got_entries)
2472                       return false;
2473
2474                     memset (local_got_entries, 0, size);
2475                     alpha_elf_tdata (abfd)->local_got_entries =
2476                       local_got_entries;
2477                   }
2478
2479                 for (gotent = local_got_entries[ELF64_R_SYM(rel->r_info)];
2480                      gotent != NULL && gotent->addend != rel->r_addend;
2481                      gotent = gotent->next)
2482                   continue;
2483                 if (!gotent)
2484                   {
2485                     gotent = ((struct alpha_elf_got_entry *)
2486                               bfd_alloc (abfd,
2487                                          sizeof (struct alpha_elf_got_entry)));
2488                     if (!gotent)
2489                       return false;
2490
2491                     gotent->gotobj = abfd;
2492                     gotent->addend = rel->r_addend;
2493                     gotent->got_offset = -1;
2494                     gotent->flags = 0;
2495                     gotent->use_count = 1;
2496
2497                     gotent->next = local_got_entries[ELF64_R_SYM(rel->r_info)];
2498                     local_got_entries[ELF64_R_SYM(rel->r_info)] = gotent;
2499
2500                     alpha_elf_tdata(abfd)->total_got_entries++;
2501                     alpha_elf_tdata(abfd)->n_local_got_entries++;
2502                   }
2503                 else
2504                   gotent->use_count += 1;
2505               }
2506
2507             /* Remember how this literal is used from its LITUSEs.
2508                This will be important when it comes to decide if we can
2509                create a .plt entry for a function symbol.  */
2510             if (rel+1 < relend
2511                 && ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
2512               {
2513                 do
2514                   {
2515                     ++rel;
2516                     if (rel->r_addend >= 1 && rel->r_addend <= 3)
2517                       flags |= 1 << rel->r_addend;
2518                   }
2519                 while (rel+1 < relend &&
2520                        ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE);
2521               }
2522             else
2523               {
2524                 /* No LITUSEs -- presumably the address is not being
2525                    loaded for nothing.  */
2526                 flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
2527               }
2528
2529             gotent->flags |= flags;
2530             if (h)
2531               {
2532                 /* Make a guess as to whether a .plt entry will be needed.  */
2533                 if ((h->flags |= flags) == ALPHA_ELF_LINK_HASH_LU_FUNC)
2534                   h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2535                 else
2536                   h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2537               }
2538           }
2539           /* FALLTHRU */
2540
2541         case R_ALPHA_GPDISP:
2542         case R_ALPHA_GPREL32:
2543         case R_ALPHA_GPRELHIGH:
2544         case R_ALPHA_GPRELLOW:
2545           /* We don't actually use the .got here, but the sections must
2546              be created before the linker maps input sections to output
2547              sections.  */
2548           if (!got_created)
2549             {
2550               if (!elf64_alpha_create_got_section (abfd, info))
2551                 return false;
2552
2553               /* Make sure the object's gotobj is set to itself so
2554                  that we default to every object with its own .got.
2555                  We'll merge .gots later once we've collected each
2556                  object's info.  */
2557               alpha_elf_tdata(abfd)->gotobj = abfd;
2558
2559               got_created = 1;
2560             }
2561           break;
2562
2563         case R_ALPHA_SREL16:
2564         case R_ALPHA_SREL32:
2565         case R_ALPHA_SREL64:
2566           if (h == NULL)
2567             break;
2568           /* FALLTHRU */
2569
2570         case R_ALPHA_REFLONG:
2571         case R_ALPHA_REFQUAD:
2572           if (rel_sec_name == NULL)
2573             {
2574               rel_sec_name = (bfd_elf_string_from_elf_section
2575                               (abfd, elf_elfheader(abfd)->e_shstrndx,
2576                                elf_section_data(sec)->rel_hdr.sh_name));
2577               if (rel_sec_name == NULL)
2578                 return false;
2579
2580               BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
2581                           && strcmp (bfd_get_section_name (abfd, sec),
2582                                      rel_sec_name+5) == 0);
2583             }
2584
2585           /* We need to create the section here now whether we eventually
2586              use it or not so that it gets mapped to an output section by
2587              the linker.  If not used, we'll kill it in
2588              size_dynamic_sections.  */
2589           if (sreloc == NULL)
2590             {
2591               sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
2592               if (sreloc == NULL)
2593                 {
2594                   sreloc = bfd_make_section (dynobj, rel_sec_name);
2595                   if (sreloc == NULL
2596                       || !bfd_set_section_flags (dynobj, sreloc,
2597                                                  (((sec->flags
2598                                                     & SEC_ALLOC)
2599                                                    ? (SEC_ALLOC
2600                                                       | SEC_LOAD) : 0)
2601                                                   | SEC_HAS_CONTENTS
2602                                                   | SEC_IN_MEMORY
2603                                                   | SEC_LINKER_CREATED
2604                                                   | SEC_READONLY))
2605                       || !bfd_set_section_alignment (dynobj, sreloc, 3))
2606                     return false;
2607                 }
2608             }
2609
2610           if (h)
2611             {
2612               /* Since we havn't seen all of the input symbols yet, we
2613                  don't know whether we'll actually need a dynamic relocation
2614                  entry for this reloc.  So make a record of it.  Once we
2615                  find out if this thing needs dynamic relocation we'll
2616                  expand the relocation sections by the appropriate amount.  */
2617
2618               struct alpha_elf_reloc_entry *rent;
2619
2620               for (rent = h->reloc_entries; rent; rent = rent->next)
2621                 if (rent->rtype == r_type && rent->srel == sreloc)
2622                   break;
2623
2624               if (!rent)
2625                 {
2626                   rent = ((struct alpha_elf_reloc_entry *)
2627                           bfd_alloc (abfd,
2628                                      sizeof (struct alpha_elf_reloc_entry)));
2629                   if (!rent)
2630                     return false;
2631
2632                   rent->srel = sreloc;
2633                   rent->rtype = r_type;
2634                   rent->count = 1;
2635                   rent->reltext = (sec->flags & SEC_READONLY) != 0;
2636
2637                   rent->next = h->reloc_entries;
2638                   h->reloc_entries = rent;
2639                 }
2640               else
2641                 rent->count++;
2642             }
2643           else if (info->shared && (sec->flags & SEC_ALLOC))
2644             {
2645               /* If this is a shared library, and the section is to be
2646                  loaded into memory, we need a RELATIVE reloc.  */
2647               sreloc->_raw_size += sizeof (Elf64_External_Rela);
2648               if (sec->flags & SEC_READONLY)
2649                 info->flags |= DF_TEXTREL;
2650             }
2651           break;
2652         }
2653     }
2654
2655   return true;
2656 }
2657
2658 /* Adjust a symbol defined by a dynamic object and referenced by a
2659    regular object.  The current definition is in some section of the
2660    dynamic object, but we're not including those sections.  We have to
2661    change the definition to something the rest of the link can
2662    understand.  */
2663
2664 static boolean
2665 elf64_alpha_adjust_dynamic_symbol (info, h)
2666      struct bfd_link_info *info;
2667      struct elf_link_hash_entry *h;
2668 {
2669   bfd *dynobj;
2670   asection *s;
2671   struct alpha_elf_link_hash_entry *ah;
2672
2673   dynobj = elf_hash_table(info)->dynobj;
2674   ah = (struct alpha_elf_link_hash_entry *)h;
2675
2676   /* Now that we've seen all of the input symbols, finalize our decision
2677      about whether this symbol should get a .plt entry.  */
2678
2679   if (h->root.type != bfd_link_hash_undefweak
2680       && alpha_elf_dynamic_symbol_p (h, info)
2681       && ((h->type == STT_FUNC
2682            && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
2683           || (h->type == STT_NOTYPE
2684               && ah->flags == ALPHA_ELF_LINK_HASH_LU_FUNC))
2685       /* Don't prevent otherwise valid programs from linking by attempting
2686          to create a new .got entry somewhere.  A Correct Solution would be
2687          to add a new .got section to a new object file and let it be merged
2688          somewhere later.  But for now don't bother.  */
2689       && ah->got_entries)
2690     {
2691       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2692
2693       s = bfd_get_section_by_name(dynobj, ".plt");
2694       if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
2695         return false;
2696
2697       /* The first bit of the .plt is reserved.  */
2698       if (s->_raw_size == 0)
2699         s->_raw_size = PLT_HEADER_SIZE;
2700
2701       h->plt.offset = s->_raw_size;
2702       s->_raw_size += PLT_ENTRY_SIZE;
2703
2704       /* If this symbol is not defined in a regular file, and we are not
2705          generating a shared library, then set the symbol to the location
2706          in the .plt.  This is required to make function pointers compare
2707          equal between the normal executable and the shared library.  */
2708       if (! info->shared
2709           && h->root.type != bfd_link_hash_defweak)
2710         {
2711           h->root.u.def.section = s;
2712           h->root.u.def.value = h->plt.offset;
2713         }
2714
2715       /* We also need a JMP_SLOT entry in the .rela.plt section.  */
2716       s = bfd_get_section_by_name (dynobj, ".rela.plt");
2717       BFD_ASSERT (s != NULL);
2718       s->_raw_size += sizeof (Elf64_External_Rela);
2719
2720       return true;
2721     }
2722   else
2723     h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2724
2725   /* If this is a weak symbol, and there is a real definition, the
2726      processor independent code will have arranged for us to see the
2727      real definition first, and we can just use the same value.  */
2728   if (h->weakdef != NULL)
2729     {
2730       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2731                   || h->weakdef->root.type == bfd_link_hash_defweak);
2732       h->root.u.def.section = h->weakdef->root.u.def.section;
2733       h->root.u.def.value = h->weakdef->root.u.def.value;
2734       return true;
2735     }
2736
2737   /* This is a reference to a symbol defined by a dynamic object which
2738      is not a function.  The Alpha, since it uses .got entries for all
2739      symbols even in regular objects, does not need the hackery of a
2740      .dynbss section and COPY dynamic relocations.  */
2741
2742   return true;
2743 }
2744
2745 /* Symbol versioning can create new symbols, and make our old symbols
2746    indirect to the new ones.  Consolidate the got and reloc information
2747    in these situations.  */
2748
2749 static boolean
2750 elf64_alpha_merge_ind_symbols (hi, dummy)
2751      struct alpha_elf_link_hash_entry *hi;
2752      PTR dummy ATTRIBUTE_UNUSED;
2753 {
2754   struct alpha_elf_link_hash_entry *hs;
2755
2756   if (hi->root.root.type != bfd_link_hash_indirect)
2757     return true;
2758   hs = hi;
2759   do {
2760     hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
2761   } while (hs->root.root.type == bfd_link_hash_indirect);
2762
2763   /* Merge the flags.  Whee.  */
2764
2765   hs->flags |= hi->flags;
2766
2767   /* Merge the .got entries.  Cannibalize the old symbol's list in
2768      doing so, since we don't need it anymore.  */
2769
2770   if (hs->got_entries == NULL)
2771     hs->got_entries = hi->got_entries;
2772   else
2773     {
2774       struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
2775
2776       gsh = hs->got_entries;
2777       for (gi = hi->got_entries; gi ; gi = gin)
2778         {
2779           gin = gi->next;
2780           for (gs = gsh; gs ; gs = gs->next)
2781             if (gi->gotobj == gs->gotobj && gi->addend == gs->addend)
2782               goto got_found;
2783           gi->next = hs->got_entries;
2784           hs->got_entries = gi;
2785         got_found:;
2786         }
2787     }
2788   hi->got_entries = NULL;
2789
2790   /* And similar for the reloc entries.  */
2791
2792   if (hs->reloc_entries == NULL)
2793     hs->reloc_entries = hi->reloc_entries;
2794   else
2795     {
2796       struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
2797
2798       rsh = hs->reloc_entries;
2799       for (ri = hi->reloc_entries; ri ; ri = rin)
2800         {
2801           rin = ri->next;
2802           for (rs = rsh; rs ; rs = rs->next)
2803             if (ri->rtype == rs->rtype)
2804               {
2805                 rs->count += ri->count;
2806                 goto found_reloc;
2807               }
2808           ri->next = hs->reloc_entries;
2809           hs->reloc_entries = ri;
2810         found_reloc:;
2811         }
2812     }
2813   hi->reloc_entries = NULL;
2814
2815   return true;
2816 }
2817
2818 /* Is it possible to merge two object file's .got tables?  */
2819
2820 static boolean
2821 elf64_alpha_can_merge_gots (a, b)
2822      bfd *a, *b;
2823 {
2824   int total = alpha_elf_tdata (a)->total_got_entries;
2825   bfd *bsub;
2826
2827   /* Trivial quick fallout test.  */
2828   if (total + alpha_elf_tdata (b)->total_got_entries <= MAX_GOT_ENTRIES)
2829     return true;
2830
2831   /* By their nature, local .got entries cannot be merged.  */
2832   if ((total += alpha_elf_tdata (b)->n_local_got_entries) > MAX_GOT_ENTRIES)
2833     return false;
2834
2835   /* Failing the common trivial comparison, we must effectively
2836      perform the merge.  Not actually performing the merge means that
2837      we don't have to store undo information in case we fail.  */
2838   for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2839     {
2840       struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
2841       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2842       int i, n;
2843
2844       n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
2845       for (i = 0; i < n; ++i)
2846         {
2847           struct alpha_elf_got_entry *ae, *be;
2848           struct alpha_elf_link_hash_entry *h;
2849
2850           h = hashes[i];
2851           while (h->root.root.type == bfd_link_hash_indirect
2852                  || h->root.root.type == bfd_link_hash_warning)
2853             h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2854
2855           for (be = h->got_entries; be ; be = be->next)
2856             {
2857               if (be->use_count == 0)
2858                 continue;
2859               if (be->gotobj != b)
2860                 continue;
2861
2862               for (ae = h->got_entries; ae ; ae = ae->next)
2863                 if (ae->gotobj == a && ae->addend == be->addend)
2864                   goto global_found;
2865
2866               if (++total > MAX_GOT_ENTRIES)
2867                 return false;
2868             global_found:;
2869             }
2870         }
2871     }
2872
2873   return true;
2874 }
2875
2876 /* Actually merge two .got tables.  */
2877
2878 static void
2879 elf64_alpha_merge_gots (a, b)
2880      bfd *a, *b;
2881 {
2882   int total = alpha_elf_tdata (a)->total_got_entries;
2883   bfd *bsub;
2884
2885   /* Remember local expansion.  */
2886   {
2887     int e = alpha_elf_tdata (b)->n_local_got_entries;
2888     total += e;
2889     alpha_elf_tdata (a)->n_local_got_entries += e;
2890   }
2891
2892   for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
2893     {
2894       struct alpha_elf_got_entry **local_got_entries;
2895       struct alpha_elf_link_hash_entry **hashes;
2896       Elf_Internal_Shdr *symtab_hdr;
2897       int i, n;
2898
2899       /* Let the local .got entries know they are part of a new subsegment.  */
2900       local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
2901       if (local_got_entries)
2902         {
2903           n = elf_tdata (bsub)->symtab_hdr.sh_info;
2904           for (i = 0; i < n; ++i)
2905             {
2906               struct alpha_elf_got_entry *ent;
2907               for (ent = local_got_entries[i]; ent; ent = ent->next)
2908                 ent->gotobj = a;
2909             }
2910         }
2911
2912       /* Merge the global .got entries.  */
2913       hashes = alpha_elf_sym_hashes (bsub);
2914       symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
2915
2916       n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
2917       for (i = 0; i < n; ++i)
2918         {
2919           struct alpha_elf_got_entry *ae, *be, **pbe, **start;
2920           struct alpha_elf_link_hash_entry *h;
2921
2922           h = hashes[i];
2923           while (h->root.root.type == bfd_link_hash_indirect
2924                  || h->root.root.type == bfd_link_hash_warning)
2925             h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2926
2927           start = &h->got_entries;
2928           for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
2929             {
2930               if (be->use_count == 0)
2931                 {
2932                   *pbe = be->next;
2933                   continue;
2934                 }
2935               if (be->gotobj != b)
2936                 continue;
2937
2938               for (ae = *start; ae ; ae = ae->next)
2939                 if (ae->gotobj == a && ae->addend == be->addend)
2940                   {
2941                     ae->flags |= be->flags;
2942                     ae->use_count += be->use_count;
2943                     *pbe = be->next;
2944                     goto global_found;
2945                   }
2946               be->gotobj = a;
2947               total += 1;
2948
2949             global_found:;
2950             }
2951         }
2952
2953       alpha_elf_tdata (bsub)->gotobj = a;
2954     }
2955   alpha_elf_tdata (a)->total_got_entries = total;
2956
2957   /* Merge the two in_got chains.  */
2958   {
2959     bfd *next;
2960
2961     bsub = a;
2962     while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
2963       bsub = next;
2964
2965     alpha_elf_tdata (bsub)->in_got_link_next = b;
2966   }
2967 }
2968
2969 /* Calculate the offsets for the got entries.  */
2970
2971 static boolean
2972 elf64_alpha_calc_got_offsets_for_symbol (h, arg)
2973      struct alpha_elf_link_hash_entry *h;
2974      PTR arg ATTRIBUTE_UNUSED;
2975 {
2976   struct alpha_elf_got_entry *gotent;
2977
2978   for (gotent = h->got_entries; gotent; gotent = gotent->next)
2979     if (gotent->use_count > 0)
2980       {
2981         bfd_size_type *plge
2982           = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
2983
2984         gotent->got_offset = *plge;
2985         *plge += 8;
2986       }
2987
2988   return true;
2989 }
2990
2991 static void
2992 elf64_alpha_calc_got_offsets (info)
2993      struct bfd_link_info *info;
2994 {
2995   bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
2996
2997   /* First, zero out the .got sizes, as we may be recalculating the
2998      .got after optimizing it.  */
2999   for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3000     alpha_elf_tdata(i)->got->_raw_size = 0;
3001
3002   /* Next, fill in the offsets for all the global entries.  */
3003   alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3004                                 elf64_alpha_calc_got_offsets_for_symbol,
3005                                 NULL);
3006
3007   /* Finally, fill in the offsets for the local entries.  */
3008   for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
3009     {
3010       bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
3011       bfd *j;
3012
3013       for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
3014         {
3015           struct alpha_elf_got_entry **local_got_entries, *gotent;
3016           int k, n;
3017
3018           local_got_entries = alpha_elf_tdata(j)->local_got_entries;
3019           if (!local_got_entries)
3020             continue;
3021
3022           for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
3023             for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
3024               if (gotent->use_count > 0)
3025                 {
3026                   gotent->got_offset = got_offset;
3027                   got_offset += 8;
3028                 }
3029         }
3030
3031       alpha_elf_tdata(i)->got->_raw_size = got_offset;
3032       alpha_elf_tdata(i)->got->_cooked_size = got_offset;
3033     }
3034 }
3035
3036 /* Constructs the gots.  */
3037
3038 static boolean
3039 elf64_alpha_size_got_sections (output_bfd, info)
3040      bfd *output_bfd ATTRIBUTE_UNUSED;
3041      struct bfd_link_info *info;
3042 {
3043   bfd *i, *got_list, *cur_got_obj = NULL;
3044   int something_changed = 0;
3045
3046   got_list = alpha_elf_hash_table (info)->got_list;
3047
3048   /* On the first time through, pretend we have an existing got list
3049      consisting of all of the input files.  */
3050   if (got_list == NULL)
3051     {
3052       for (i = info->input_bfds; i ; i = i->link_next)
3053         {
3054           bfd *this_got = alpha_elf_tdata (i)->gotobj;
3055           if (this_got == NULL)
3056             continue;
3057
3058           /* We are assuming no merging has yet ocurred.  */
3059           BFD_ASSERT (this_got == i);
3060
3061           if (alpha_elf_tdata (this_got)->total_got_entries > MAX_GOT_ENTRIES)
3062             {
3063               /* Yikes! A single object file has too many entries.  */
3064               (*_bfd_error_handler)
3065                 (_("%s: .got subsegment exceeds 64K (size %d)"),
3066                  bfd_get_filename (i),
3067                  alpha_elf_tdata (this_got)->total_got_entries * 8);
3068               return false;
3069             }
3070
3071           if (got_list == NULL)
3072             got_list = this_got;
3073           else
3074             alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
3075           cur_got_obj = this_got;
3076         }
3077
3078       /* Strange degenerate case of no got references.  */
3079       if (got_list == NULL)
3080         return true;
3081
3082       alpha_elf_hash_table (info)->got_list = got_list;
3083
3084       /* Force got offsets to be recalculated.  */
3085       something_changed = 1;
3086     }
3087
3088   cur_got_obj = got_list;
3089   i = alpha_elf_tdata(cur_got_obj)->got_link_next;
3090   while (i != NULL)
3091     {
3092       if (elf64_alpha_can_merge_gots (cur_got_obj, i))
3093         {
3094           elf64_alpha_merge_gots (cur_got_obj, i);
3095           i = alpha_elf_tdata(i)->got_link_next;
3096           alpha_elf_tdata(cur_got_obj)->got_link_next = i;
3097           something_changed = 1;
3098         }
3099       else
3100         {
3101           cur_got_obj = i;
3102           i = alpha_elf_tdata(i)->got_link_next;
3103         }
3104     }
3105
3106   /* Once the gots have been merged, fill in the got offsets for
3107      everything therein.  */
3108   if (1 || something_changed)
3109     elf64_alpha_calc_got_offsets (info);
3110
3111   return true;
3112 }
3113
3114 static boolean
3115 elf64_alpha_always_size_sections (output_bfd, info)
3116      bfd *output_bfd;
3117      struct bfd_link_info *info;
3118 {
3119   bfd *i;
3120
3121   if (info->relocateable)
3122     return true;
3123
3124   /* First, take care of the indirect symbols created by versioning.  */
3125   alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3126                                 elf64_alpha_merge_ind_symbols,
3127                                 NULL);
3128
3129   if (!elf64_alpha_size_got_sections (output_bfd, info))
3130     return false;
3131
3132   /* Allocate space for all of the .got subsections.  */
3133   i = alpha_elf_hash_table (info)->got_list;
3134   for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
3135     {
3136       asection *s = alpha_elf_tdata(i)->got;
3137       if (s->_raw_size > 0)
3138         {
3139           s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
3140           if (s->contents == NULL)
3141             return false;
3142         }
3143     }
3144
3145   return true;
3146 }
3147
3148 /* Work out the sizes of the dynamic relocation entries.  */
3149
3150 static boolean
3151 elf64_alpha_calc_dynrel_sizes (h, info)
3152      struct alpha_elf_link_hash_entry *h;
3153      struct bfd_link_info *info;
3154 {
3155   /* If the symbol was defined as a common symbol in a regular object
3156      file, and there was no definition in any dynamic object, then the
3157      linker will have allocated space for the symbol in a common
3158      section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
3159      set.  This is done for dynamic symbols in
3160      elf_adjust_dynamic_symbol but this is not done for non-dynamic
3161      symbols, somehow.  */
3162   if (((h->root.elf_link_hash_flags
3163        & (ELF_LINK_HASH_DEF_REGULAR
3164           | ELF_LINK_HASH_REF_REGULAR
3165           | ELF_LINK_HASH_DEF_DYNAMIC))
3166        == ELF_LINK_HASH_REF_REGULAR)
3167       && (h->root.root.type == bfd_link_hash_defined
3168           || h->root.root.type == bfd_link_hash_defweak)
3169       && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
3170     {
3171       h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3172     }
3173
3174   /* If the symbol is dynamic, we'll need all the relocations in their
3175      natural form.  If this is a shared object, and it has been forced
3176      local, we'll need the same number of RELATIVE relocations.  */
3177
3178   if (alpha_elf_dynamic_symbol_p (&h->root, info) || info->shared)
3179     {
3180       struct alpha_elf_reloc_entry *relent;
3181       bfd *dynobj;
3182       struct alpha_elf_got_entry *gotent;
3183       bfd_size_type count;
3184       asection *srel;
3185
3186       for (relent = h->reloc_entries; relent; relent = relent->next)
3187         if (relent->rtype == R_ALPHA_REFLONG
3188             || relent->rtype == R_ALPHA_REFQUAD)
3189           {
3190             relent->srel->_raw_size +=
3191               sizeof (Elf64_External_Rela) * relent->count;
3192             if (relent->reltext)
3193               info->flags |= DT_TEXTREL;
3194           }
3195
3196       dynobj = elf_hash_table(info)->dynobj;
3197       count = 0;
3198
3199       for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3200         count++;
3201
3202       /* If we are using a .plt entry, subtract one, as the first
3203          reference uses a .rela.plt entry instead.  */
3204       if (h->root.plt.offset != MINUS_ONE)
3205         count--;
3206
3207       if (count > 0)
3208         {
3209           srel = bfd_get_section_by_name (dynobj, ".rela.got");
3210           BFD_ASSERT (srel != NULL);
3211           srel->_raw_size += sizeof (Elf64_External_Rela) * count;
3212         }
3213     }
3214
3215   return true;
3216 }
3217
3218 /* Set the sizes of the dynamic sections.  */
3219
3220 static boolean
3221 elf64_alpha_size_dynamic_sections (output_bfd, info)
3222      bfd *output_bfd ATTRIBUTE_UNUSED;
3223      struct bfd_link_info *info;
3224 {
3225   bfd *dynobj;
3226   asection *s;
3227   boolean relplt;
3228
3229   dynobj = elf_hash_table(info)->dynobj;
3230   BFD_ASSERT(dynobj != NULL);
3231
3232   if (elf_hash_table (info)->dynamic_sections_created)
3233     {
3234       /* Set the contents of the .interp section to the interpreter.  */
3235       if (!info->shared)
3236         {
3237           s = bfd_get_section_by_name (dynobj, ".interp");
3238           BFD_ASSERT (s != NULL);
3239           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3240           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3241         }
3242
3243       /* Now that we've seen all of the input files, we can decide which
3244          symbols need dynamic relocation entries and which don't.  We've
3245          collected information in check_relocs that we can now apply to
3246          size the dynamic relocation sections.  */
3247       alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3248                                     elf64_alpha_calc_dynrel_sizes,
3249                                     info);
3250
3251       /* When building shared libraries, each local .got entry needs a
3252          RELATIVE reloc.  */
3253       if (info->shared)
3254         {
3255           bfd *i;
3256           asection *srel;
3257           bfd_size_type count;
3258
3259           srel = bfd_get_section_by_name (dynobj, ".rela.got");
3260           BFD_ASSERT (srel != NULL);
3261
3262           for (i = alpha_elf_hash_table(info)->got_list, count = 0;
3263                i != NULL;
3264                i = alpha_elf_tdata(i)->got_link_next)
3265             count += alpha_elf_tdata(i)->n_local_got_entries;
3266
3267           srel->_raw_size += count * sizeof (Elf64_External_Rela);
3268         }
3269     }
3270   /* else we're not dynamic and by definition we don't need such things.  */
3271
3272   /* The check_relocs and adjust_dynamic_symbol entry points have
3273      determined the sizes of the various dynamic sections.  Allocate
3274      memory for them.  */
3275   relplt = false;
3276   for (s = dynobj->sections; s != NULL; s = s->next)
3277     {
3278       const char *name;
3279       boolean strip;
3280
3281       if (!(s->flags & SEC_LINKER_CREATED))
3282         continue;
3283
3284       /* It's OK to base decisions on the section name, because none
3285          of the dynobj section names depend upon the input files.  */
3286       name = bfd_get_section_name (dynobj, s);
3287
3288       /* If we don't need this section, strip it from the output file.
3289          This is to handle .rela.bss and .rela.plt.  We must create it
3290          in create_dynamic_sections, because it must be created before
3291          the linker maps input sections to output sections.  The
3292          linker does that before adjust_dynamic_symbol is called, and
3293          it is that function which decides whether anything needs to
3294          go into these sections.  */
3295
3296       strip = false;
3297
3298       if (strncmp (name, ".rela", 5) == 0)
3299         {
3300           strip = (s->_raw_size == 0);
3301
3302           if (!strip)
3303             {
3304               if (strcmp(name, ".rela.plt") == 0)
3305                 relplt = true;
3306
3307               /* We use the reloc_count field as a counter if we need
3308                  to copy relocs into the output file.  */
3309               s->reloc_count = 0;
3310             }
3311         }
3312       else if (strcmp (name, ".plt") != 0)
3313         {
3314           /* It's not one of our dynamic sections, so don't allocate space.  */
3315           continue;
3316         }
3317
3318       if (strip)
3319         _bfd_strip_section_from_output (info, s);
3320       else
3321         {
3322           /* Allocate memory for the section contents.  */
3323           s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
3324           if (s->contents == NULL && s->_raw_size != 0)
3325             return false;
3326         }
3327     }
3328
3329   if (elf_hash_table (info)->dynamic_sections_created)
3330     {
3331       /* Add some entries to the .dynamic section.  We fill in the
3332          values later, in elf64_alpha_finish_dynamic_sections, but we
3333          must add the entries now so that we get the correct size for
3334          the .dynamic section.  The DT_DEBUG entry is filled in by the
3335          dynamic linker and used by the debugger.  */
3336       if (!info->shared)
3337         {
3338           if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
3339             return false;
3340         }
3341
3342       if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
3343         return false;
3344
3345       if (relplt)
3346         {
3347           if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3348               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
3349               || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
3350             return false;
3351         }
3352
3353       if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
3354           || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
3355           || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
3356                                             sizeof (Elf64_External_Rela)))
3357         return false;
3358
3359       if (info->flags & DF_TEXTREL)
3360         {
3361           if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
3362             return false;
3363         }
3364     }
3365
3366   return true;
3367 }
3368
3369 /* Relocate an Alpha ELF section.  */
3370
3371 static boolean
3372 elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
3373                               contents, relocs, local_syms, local_sections)
3374      bfd *output_bfd;
3375      struct bfd_link_info *info;
3376      bfd *input_bfd;
3377      asection *input_section;
3378      bfd_byte *contents;
3379      Elf_Internal_Rela *relocs;
3380      Elf_Internal_Sym *local_syms;
3381      asection **local_sections;
3382 {
3383   Elf_Internal_Shdr *symtab_hdr;
3384   Elf_Internal_Rela *rel;
3385   Elf_Internal_Rela *relend;
3386   asection *sec, *sgot, *srel, *srelgot;
3387   bfd *dynobj, *gotobj;
3388   bfd_vma gp;
3389
3390   srelgot = srel = NULL;
3391   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3392   dynobj = elf_hash_table (info)->dynobj;
3393   if (dynobj)
3394     {
3395       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3396     }
3397
3398   /* Find the gp value for this input bfd.  */
3399   sgot = NULL;
3400   gp = 0;
3401   gotobj = alpha_elf_tdata (input_bfd)->gotobj;
3402   if (gotobj)
3403     {
3404       sgot = alpha_elf_tdata (gotobj)->got;
3405       gp = _bfd_get_gp_value (gotobj);
3406       if (gp == 0)
3407         {
3408           gp = (sgot->output_section->vma
3409                 + sgot->output_offset
3410                 + 0x8000);
3411           _bfd_set_gp_value (gotobj, gp);
3412         }
3413     }
3414
3415   rel = relocs;
3416   relend = relocs + input_section->reloc_count;
3417   for (; rel < relend; rel++)
3418     {
3419       int r_type;
3420       reloc_howto_type *howto;
3421       unsigned long r_symndx;
3422       struct alpha_elf_link_hash_entry *h;
3423       Elf_Internal_Sym *sym;
3424       bfd_vma relocation;
3425       bfd_signed_vma addend;
3426       bfd_reloc_status_type r;
3427
3428       r_type = ELF64_R_TYPE(rel->r_info);
3429       if (r_type < 0 || r_type >= (int) R_ALPHA_max)
3430         {
3431           bfd_set_error (bfd_error_bad_value);
3432           return false;
3433         }
3434       howto = elf64_alpha_howto_table + r_type;
3435
3436       r_symndx = ELF64_R_SYM(rel->r_info);
3437
3438       if (info->relocateable)
3439         {
3440           /* This is a relocateable link.  We don't have to change
3441              anything, unless the reloc is against a section symbol,
3442              in which case we have to adjust according to where the
3443              section symbol winds up in the output section.  */
3444
3445           /* The symbol associated with GPDISP and LITUSE is
3446              immaterial.  Only the addend is significant.  */
3447           if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
3448             continue;
3449
3450           if (r_symndx < symtab_hdr->sh_info)
3451             {
3452               sym = local_syms + r_symndx;
3453               if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
3454                 {
3455                   sec = local_sections[r_symndx];
3456                   rel->r_addend += sec->output_offset + sym->st_value;
3457                 }
3458             }
3459
3460           continue;
3461         }
3462
3463       /* This is a final link.  */
3464
3465       h = NULL;
3466       sym = NULL;
3467       sec = NULL;
3468
3469       if (r_symndx < symtab_hdr->sh_info)
3470         {
3471           sym = local_syms + r_symndx;
3472           sec = local_sections[r_symndx];
3473           relocation = (sec->output_section->vma
3474                         + sec->output_offset
3475                         + sym->st_value);
3476         }
3477       else
3478         {
3479           h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
3480
3481           while (h->root.root.type == bfd_link_hash_indirect
3482                  || h->root.root.type == bfd_link_hash_warning)
3483             h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
3484
3485           if (h->root.root.type == bfd_link_hash_defined
3486               || h->root.root.type == bfd_link_hash_defweak)
3487             {
3488               sec = h->root.root.u.def.section;
3489
3490 #if rth_notdef
3491               if ((r_type == R_ALPHA_LITERAL
3492                    && elf_hash_table(info)->dynamic_sections_created
3493                    && (!info->shared
3494                        || !info->symbolic
3495                        || !(h->root.elf_link_hash_flags
3496                             & ELF_LINK_HASH_DEF_REGULAR)))
3497                   || (info->shared
3498                       && (!info->symbolic
3499                           || !(h->root.elf_link_hash_flags
3500                                & ELF_LINK_HASH_DEF_REGULAR))
3501                       && (input_section->flags & SEC_ALLOC)
3502                       && (r_type == R_ALPHA_REFLONG
3503                           || r_type == R_ALPHA_REFQUAD
3504                           || r_type == R_ALPHA_LITERAL)))
3505                 {
3506                   /* In these cases, we don't need the relocation value.
3507                      We check specially because in some obscure cases
3508                      sec->output_section will be NULL.  */
3509                   relocation = 0;
3510                 }
3511 #else
3512               /* FIXME: Are not these obscure cases simply bugs?  Let's
3513                  get something working and come back to this.  */
3514               if (sec->output_section == NULL)
3515                 relocation = 0;
3516 #endif /* rth_notdef */
3517               else
3518                 {
3519                   relocation = (h->root.root.u.def.value
3520                                 + sec->output_section->vma
3521                                 + sec->output_offset);
3522                 }
3523             }
3524           else if (h->root.root.type == bfd_link_hash_undefweak)
3525             relocation = 0;
3526           else if (info->shared && !info->symbolic
3527                    && !info->no_undefined
3528                    && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
3529             relocation = 0;
3530           else
3531             {
3532               if (!((*info->callbacks->undefined_symbol)
3533                     (info, h->root.root.root.string, input_bfd,
3534                      input_section, rel->r_offset,
3535                      (!info->shared || info->no_undefined
3536                       || ELF_ST_VISIBILITY (h->root.other)))))
3537                 return false;
3538               relocation = 0;
3539             }
3540         }
3541       addend = rel->r_addend;
3542
3543       switch (r_type)
3544         {
3545         case R_ALPHA_GPDISP:
3546           {
3547             bfd_byte *p_ldah, *p_lda;
3548
3549             BFD_ASSERT(gp != 0);
3550
3551             relocation = (input_section->output_section->vma
3552                           + input_section->output_offset
3553                           + rel->r_offset);
3554
3555             p_ldah = contents + rel->r_offset - input_section->vma;
3556             p_lda = p_ldah + rel->r_addend;
3557
3558             r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
3559                                              p_ldah, p_lda);
3560           }
3561           break;
3562
3563         case R_ALPHA_OP_PUSH:
3564         case R_ALPHA_OP_STORE:
3565         case R_ALPHA_OP_PSUB:
3566         case R_ALPHA_OP_PRSHIFT:
3567           /* We hate these silly beasts.  */
3568           abort ();
3569
3570         case R_ALPHA_LITERAL:
3571           {
3572             struct alpha_elf_got_entry *gotent;
3573             boolean dynamic_symbol;
3574
3575             BFD_ASSERT(sgot != NULL);
3576             BFD_ASSERT(gp != 0);
3577
3578             if (h != NULL)
3579               {
3580                 gotent = h->got_entries;
3581                 dynamic_symbol = alpha_elf_dynamic_symbol_p (&h->root, info);
3582               }
3583             else
3584               {
3585                 gotent = (alpha_elf_tdata(input_bfd)->
3586                           local_got_entries[r_symndx]);
3587                 dynamic_symbol = false;
3588               }
3589
3590             BFD_ASSERT(gotent != NULL);
3591
3592             while (gotent->gotobj != gotobj || gotent->addend != addend)
3593               gotent = gotent->next;
3594
3595             BFD_ASSERT(gotent->use_count >= 1);
3596
3597             /* Initialize the .got entry's value.  */
3598             if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
3599               {
3600                 bfd_put_64 (output_bfd, relocation+addend,
3601                             sgot->contents + gotent->got_offset);
3602
3603                 /* If the symbol has been forced local, output a
3604                    RELATIVE reloc, otherwise it will be handled in
3605                    finish_dynamic_symbol.  */
3606                 if (info->shared && !dynamic_symbol)
3607                   {
3608                     Elf_Internal_Rela outrel;
3609
3610                     BFD_ASSERT(srelgot != NULL);
3611
3612                     outrel.r_offset = (sgot->output_section->vma
3613                                        + sgot->output_offset
3614                                        + gotent->got_offset);
3615                     outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3616                     outrel.r_addend = 0;
3617
3618                     bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3619                                                ((Elf64_External_Rela *)
3620                                                 srelgot->contents)
3621                                                + srelgot->reloc_count++);
3622                     BFD_ASSERT (sizeof (Elf64_External_Rela)
3623                                 * srelgot->reloc_count
3624                                 <= srelgot->_cooked_size);
3625                   }
3626
3627                 gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
3628               }
3629
3630             /* Figure the gprel relocation.  */
3631             addend = 0;
3632             relocation = (sgot->output_section->vma
3633                           + sgot->output_offset
3634                           + gotent->got_offset);
3635             relocation -= gp;
3636           }
3637           /* overflow handled by _bfd_final_link_relocate */
3638           goto default_reloc;
3639
3640         case R_ALPHA_GPREL32:
3641         case R_ALPHA_GPRELLOW:
3642           BFD_ASSERT(gp != 0);
3643           relocation -= gp;
3644           goto default_reloc;
3645
3646         case R_ALPHA_GPRELHIGH:
3647           BFD_ASSERT(gp != 0);
3648           relocation -= gp;
3649           relocation += addend;
3650           addend = 0;
3651           relocation = (((bfd_signed_vma) relocation >> 16)
3652                         + ((relocation >> 15) & 1));
3653           goto default_reloc;
3654
3655         case R_ALPHA_BRADDR:
3656         case R_ALPHA_HINT:
3657           /* The regular PC-relative stuff measures from the start of
3658              the instruction rather than the end.  */
3659           addend -= 4;
3660           goto default_reloc;
3661
3662         case R_ALPHA_REFLONG:
3663         case R_ALPHA_REFQUAD:
3664           {
3665             Elf_Internal_Rela outrel;
3666             boolean skip;
3667
3668             /* Careful here to remember RELATIVE relocations for global
3669                variables for symbolic shared objects.  */
3670
3671             if (h && alpha_elf_dynamic_symbol_p (&h->root, info))
3672               {
3673                 BFD_ASSERT(h->root.dynindx != -1);
3674                 outrel.r_info = ELF64_R_INFO(h->root.dynindx, r_type);
3675                 outrel.r_addend = addend;
3676                 addend = 0, relocation = 0;
3677               }
3678             else if (info->shared && (input_section->flags & SEC_ALLOC))
3679               {
3680                 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3681                 outrel.r_addend = 0;
3682               }
3683             else
3684               goto default_reloc;
3685
3686             if (!srel)
3687               {
3688                 const char *name;
3689
3690                 name = (bfd_elf_string_from_elf_section
3691                         (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
3692                          elf_section_data(input_section)->rel_hdr.sh_name));
3693                 BFD_ASSERT(name != NULL);
3694
3695                 srel = bfd_get_section_by_name (dynobj, name);
3696                 BFD_ASSERT(srel != NULL);
3697               }
3698
3699             skip = false;
3700
3701             if (elf_section_data (input_section)->stab_info == NULL)
3702               outrel.r_offset = rel->r_offset;
3703             else
3704               {
3705                 bfd_vma off;
3706
3707                 off = (_bfd_stab_section_offset
3708                        (output_bfd, &elf_hash_table (info)->stab_info,
3709                         input_section,
3710                         &elf_section_data (input_section)->stab_info,
3711                         rel->r_offset));
3712                 if (off == (bfd_vma) -1)
3713                   skip = true;
3714                 outrel.r_offset = off;
3715               }
3716
3717             if (! skip)
3718               outrel.r_offset += (input_section->output_section->vma
3719                                   + input_section->output_offset);
3720             else
3721               memset (&outrel, 0, sizeof outrel);
3722
3723             bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3724                                        ((Elf64_External_Rela *)
3725                                         srel->contents)
3726                                        + srel->reloc_count++);
3727             BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
3728                         <= srel->_cooked_size);
3729           }
3730           goto default_reloc;
3731
3732         default:
3733         default_reloc:
3734           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3735                                         contents, rel->r_offset, relocation,
3736                                         addend);
3737           break;
3738         }
3739
3740       switch (r)
3741         {
3742         case bfd_reloc_ok:
3743           break;
3744
3745         case bfd_reloc_overflow:
3746           {
3747             const char *name;
3748
3749             if (h != NULL)
3750               name = h->root.root.root.string;
3751             else
3752               {
3753                 name = (bfd_elf_string_from_elf_section
3754                         (input_bfd, symtab_hdr->sh_link, sym->st_name));
3755                 if (name == NULL)
3756                   return false;
3757                 if (*name == '\0')
3758                   name = bfd_section_name (input_bfd, sec);
3759               }
3760             if (! ((*info->callbacks->reloc_overflow)
3761                    (info, name, howto->name, (bfd_vma) 0,
3762                     input_bfd, input_section, rel->r_offset)))
3763               return false;
3764           }
3765           break;
3766
3767         default:
3768         case bfd_reloc_outofrange:
3769           abort ();
3770         }
3771     }
3772
3773   return true;
3774 }
3775
3776 /* Finish up dynamic symbol handling.  We set the contents of various
3777    dynamic sections here.  */
3778
3779 static boolean
3780 elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
3781      bfd *output_bfd;
3782      struct bfd_link_info *info;
3783      struct elf_link_hash_entry *h;
3784      Elf_Internal_Sym *sym;
3785 {
3786   bfd *dynobj = elf_hash_table(info)->dynobj;
3787
3788   if (h->plt.offset != MINUS_ONE)
3789     {
3790       /* Fill in the .plt entry for this symbol.  */
3791       asection *splt, *sgot, *srel;
3792       Elf_Internal_Rela outrel;
3793       bfd_vma got_addr, plt_addr;
3794       bfd_vma plt_index;
3795       struct alpha_elf_got_entry *gotent;
3796
3797       BFD_ASSERT (h->dynindx != -1);
3798
3799       /* The first .got entry will be updated by the .plt with the
3800          address of the target function.  */
3801       gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3802       BFD_ASSERT (gotent && gotent->addend == 0);
3803
3804       splt = bfd_get_section_by_name (dynobj, ".plt");
3805       BFD_ASSERT (splt != NULL);
3806       srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3807       BFD_ASSERT (srel != NULL);
3808       sgot = alpha_elf_tdata (gotent->gotobj)->got;
3809       BFD_ASSERT (sgot != NULL);
3810
3811       got_addr = (sgot->output_section->vma
3812                   + sgot->output_offset
3813                   + gotent->got_offset);
3814       plt_addr = (splt->output_section->vma
3815                   + splt->output_offset
3816                   + h->plt.offset);
3817
3818       plt_index = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3819
3820       /* Fill in the entry in the procedure linkage table.  */
3821       {
3822         unsigned insn1, insn2, insn3;
3823
3824         insn1 = PLT_ENTRY_WORD1 | ((-(h->plt.offset + 4) >> 2) & 0x1fffff);
3825         insn2 = PLT_ENTRY_WORD2;
3826         insn3 = PLT_ENTRY_WORD3;
3827
3828         bfd_put_32 (output_bfd, insn1, splt->contents + h->plt.offset);
3829         bfd_put_32 (output_bfd, insn2, splt->contents + h->plt.offset + 4);
3830         bfd_put_32 (output_bfd, insn3, splt->contents + h->plt.offset + 8);
3831       }
3832
3833       /* Fill in the entry in the .rela.plt section.  */
3834       outrel.r_offset = got_addr;
3835       outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
3836       outrel.r_addend = 0;
3837
3838       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3839                                  ((Elf64_External_Rela *)srel->contents
3840                                   + plt_index));
3841
3842       if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3843         {
3844           /* Mark the symbol as undefined, rather than as defined in the
3845              .plt section.  Leave the value alone.  */
3846           sym->st_shndx = SHN_UNDEF;
3847         }
3848
3849       /* Fill in the entries in the .got.  */
3850       bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
3851
3852       /* Subsequent .got entries will continue to bounce through the .plt.  */
3853       if (gotent->next)
3854         {
3855           srel = bfd_get_section_by_name (dynobj, ".rela.got");
3856           BFD_ASSERT (! info->shared || srel != NULL);
3857
3858           gotent = gotent->next;
3859           do
3860             {
3861               sgot = alpha_elf_tdata(gotent->gotobj)->got;
3862               BFD_ASSERT(sgot != NULL);
3863               BFD_ASSERT(gotent->addend == 0);
3864
3865               bfd_put_64 (output_bfd, plt_addr,
3866                           sgot->contents + gotent->got_offset);
3867
3868               if (info->shared)
3869                 {
3870                   outrel.r_offset = (sgot->output_section->vma
3871                                      + sgot->output_offset
3872                                      + gotent->got_offset);
3873                   outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3874                   outrel.r_addend = 0;
3875
3876                   bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3877                                              ((Elf64_External_Rela *)
3878                                               srel->contents)
3879                                              + srel->reloc_count++);
3880                   BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
3881                               <= srel->_cooked_size);
3882                 }
3883
3884               gotent = gotent->next;
3885             }
3886           while (gotent != NULL);
3887         }
3888     }
3889   else if (alpha_elf_dynamic_symbol_p (h, info))
3890     {
3891       /* Fill in the dynamic relocations for this symbol's .got entries.  */
3892       asection *srel;
3893       Elf_Internal_Rela outrel;
3894       struct alpha_elf_got_entry *gotent;
3895
3896       srel = bfd_get_section_by_name (dynobj, ".rela.got");
3897       BFD_ASSERT (srel != NULL);
3898
3899       outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_GLOB_DAT);
3900       for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3901            gotent != NULL;
3902            gotent = gotent->next)
3903         {
3904           asection *sgot = alpha_elf_tdata (gotent->gotobj)->got;
3905           outrel.r_offset = (sgot->output_section->vma
3906                              + sgot->output_offset
3907                              + gotent->got_offset);
3908           outrel.r_addend = gotent->addend;
3909
3910           bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3911                                      ((Elf64_External_Rela *)srel->contents
3912                                       + srel->reloc_count++));
3913           BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count
3914                       <= srel->_cooked_size);
3915         }
3916     }
3917
3918   /* Mark some specially defined symbols as absolute.  */
3919   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3920       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3921       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3922     sym->st_shndx = SHN_ABS;
3923
3924   return true;
3925 }
3926
3927 /* Finish up the dynamic sections.  */
3928
3929 static boolean
3930 elf64_alpha_finish_dynamic_sections (output_bfd, info)
3931      bfd *output_bfd;
3932      struct bfd_link_info *info;
3933 {
3934   bfd *dynobj;
3935   asection *sdyn;
3936
3937   dynobj = elf_hash_table (info)->dynobj;
3938   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3939
3940   if (elf_hash_table (info)->dynamic_sections_created)
3941     {
3942       asection *splt;
3943       Elf64_External_Dyn *dyncon, *dynconend;
3944
3945       splt = bfd_get_section_by_name (dynobj, ".plt");
3946       BFD_ASSERT (splt != NULL && sdyn != NULL);
3947
3948       dyncon = (Elf64_External_Dyn *) sdyn->contents;
3949       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3950       for (; dyncon < dynconend; dyncon++)
3951         {
3952           Elf_Internal_Dyn dyn;
3953           const char *name;
3954           asection *s;
3955
3956           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3957
3958           switch (dyn.d_tag)
3959             {
3960             case DT_PLTGOT:
3961               name = ".plt";
3962               goto get_vma;
3963             case DT_PLTRELSZ:
3964               name = ".rela.plt";
3965               goto get_size;
3966             case DT_JMPREL:
3967               name = ".rela.plt";
3968               goto get_vma;
3969
3970             case DT_RELASZ:
3971               /* My interpretation of the TIS v1.1 ELF document indicates
3972                  that RELASZ should not include JMPREL.  This is not what
3973                  the rest of the BFD does.  It is, however, what the
3974                  glibc ld.so wants.  Do this fixup here until we found
3975                  out who is right.  */
3976               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
3977               if (s)
3978                 {
3979                   dyn.d_un.d_val -=
3980                     (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3981                 }
3982               break;
3983
3984             get_vma:
3985               s = bfd_get_section_by_name (output_bfd, name);
3986               dyn.d_un.d_ptr = (s ? s->vma : 0);
3987               break;
3988
3989             get_size:
3990               s = bfd_get_section_by_name (output_bfd, name);
3991               dyn.d_un.d_val =
3992                 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3993               break;
3994             }
3995
3996           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3997         }
3998
3999       /* Initialize the PLT0 entry */
4000       if (splt->_raw_size > 0)
4001         {
4002           bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
4003           bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
4004           bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
4005           bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
4006
4007           /* The next two words will be filled in by ld.so */
4008           bfd_put_64 (output_bfd, 0, splt->contents + 16);
4009           bfd_put_64 (output_bfd, 0, splt->contents + 24);
4010
4011           elf_section_data (splt->output_section)->this_hdr.sh_entsize =
4012             PLT_HEADER_SIZE;
4013         }
4014     }
4015
4016   return true;
4017 }
4018
4019 /* We need to use a special link routine to handle the .reginfo and
4020    the .mdebug sections.  We need to merge all instances of these
4021    sections together, not write them all out sequentially.  */
4022
4023 static boolean
4024 elf64_alpha_final_link (abfd, info)
4025      bfd *abfd;
4026      struct bfd_link_info *info;
4027 {
4028   asection *o;
4029   struct bfd_link_order *p;
4030   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4031   struct ecoff_debug_info debug;
4032   const struct ecoff_debug_swap *swap
4033     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4034   HDRR *symhdr = &debug.symbolic_header;
4035   PTR mdebug_handle = NULL;
4036
4037 #if 0
4038               if (++ngots == 2)
4039                 {
4040                   (*info->callbacks->warning)
4041                     (info, _("using multiple gp values"), (char *) NULL,
4042                      output_bfd, (asection *) NULL, (bfd_vma) 0);
4043                 }
4044 #endif
4045
4046   /* Go through the sections and collect the .reginfo and .mdebug
4047      information.  */
4048   reginfo_sec = NULL;
4049   mdebug_sec = NULL;
4050   gptab_data_sec = NULL;
4051   gptab_bss_sec = NULL;
4052   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4053     {
4054 #ifdef ERIC_neverdef
4055       if (strcmp (o->name, ".reginfo") == 0)
4056         {
4057           memset (&reginfo, 0, sizeof reginfo);
4058
4059           /* We have found the .reginfo section in the output file.
4060              Look through all the link_orders comprising it and merge
4061              the information together.  */
4062           for (p = o->link_order_head;
4063                p != (struct bfd_link_order *) NULL;
4064                p = p->next)
4065             {
4066               asection *input_section;
4067               bfd *input_bfd;
4068               Elf64_External_RegInfo ext;
4069               Elf64_RegInfo sub;
4070
4071               if (p->type != bfd_indirect_link_order)
4072                 {
4073                   if (p->type == bfd_fill_link_order)
4074                     continue;
4075                   abort ();
4076                 }
4077
4078               input_section = p->u.indirect.section;
4079               input_bfd = input_section->owner;
4080
4081               /* The linker emulation code has probably clobbered the
4082                  size to be zero bytes.  */
4083               if (input_section->_raw_size == 0)
4084                 input_section->_raw_size = sizeof (Elf64_External_RegInfo);
4085
4086               if (! bfd_get_section_contents (input_bfd, input_section,
4087                                               (PTR) &ext,
4088                                               (file_ptr) 0,
4089                                               sizeof ext))
4090                 return false;
4091
4092               bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
4093
4094               reginfo.ri_gprmask |= sub.ri_gprmask;
4095               reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4096               reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4097               reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4098               reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4099
4100               /* ri_gp_value is set by the function
4101                  alpha_elf_section_processing when the section is
4102                  finally written out.  */
4103
4104               /* Hack: reset the SEC_HAS_CONTENTS flag so that
4105                  elf_link_input_bfd ignores this section.  */
4106               input_section->flags &=~ SEC_HAS_CONTENTS;
4107             }
4108
4109           /* Force the section size to the value we want.  */
4110           o->_raw_size = sizeof (Elf64_External_RegInfo);
4111
4112           /* Skip this section later on (I don't think this currently
4113              matters, but someday it might).  */
4114           o->link_order_head = (struct bfd_link_order *) NULL;
4115
4116           reginfo_sec = o;
4117         }
4118 #endif
4119
4120       if (strcmp (o->name, ".mdebug") == 0)
4121         {
4122           struct extsym_info einfo;
4123
4124           /* We have found the .mdebug section in the output file.
4125              Look through all the link_orders comprising it and merge
4126              the information together.  */
4127           symhdr->magic = swap->sym_magic;
4128           /* FIXME: What should the version stamp be?  */
4129           symhdr->vstamp = 0;
4130           symhdr->ilineMax = 0;
4131           symhdr->cbLine = 0;
4132           symhdr->idnMax = 0;
4133           symhdr->ipdMax = 0;
4134           symhdr->isymMax = 0;
4135           symhdr->ioptMax = 0;
4136           symhdr->iauxMax = 0;
4137           symhdr->issMax = 0;
4138           symhdr->issExtMax = 0;
4139           symhdr->ifdMax = 0;
4140           symhdr->crfd = 0;
4141           symhdr->iextMax = 0;
4142
4143           /* We accumulate the debugging information itself in the
4144              debug_info structure.  */
4145           debug.line = NULL;
4146           debug.external_dnr = NULL;
4147           debug.external_pdr = NULL;
4148           debug.external_sym = NULL;
4149           debug.external_opt = NULL;
4150           debug.external_aux = NULL;
4151           debug.ss = NULL;
4152           debug.ssext = debug.ssext_end = NULL;
4153           debug.external_fdr = NULL;
4154           debug.external_rfd = NULL;
4155           debug.external_ext = debug.external_ext_end = NULL;
4156
4157           mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4158           if (mdebug_handle == (PTR) NULL)
4159             return false;
4160
4161           if (1)
4162             {
4163               asection *s;
4164               EXTR esym;
4165               bfd_vma last = 0;
4166               unsigned int i;
4167               static const char * const name[] =
4168                 {
4169                   ".text", ".init", ".fini", ".data",
4170                   ".rodata", ".sdata", ".sbss", ".bss"
4171                 };
4172               static const int sc[] = { scText, scInit, scFini, scData,
4173                                           scRData, scSData, scSBss, scBss };
4174
4175               esym.jmptbl = 0;
4176               esym.cobol_main = 0;
4177               esym.weakext = 0;
4178               esym.reserved = 0;
4179               esym.ifd = ifdNil;
4180               esym.asym.iss = issNil;
4181               esym.asym.st = stLocal;
4182               esym.asym.reserved = 0;
4183               esym.asym.index = indexNil;
4184               for (i = 0; i < 8; i++)
4185                 {
4186                   esym.asym.sc = sc[i];
4187                   s = bfd_get_section_by_name (abfd, name[i]);
4188                   if (s != NULL)
4189                     {
4190                       esym.asym.value = s->vma;
4191                       last = s->vma + s->_raw_size;
4192                     }
4193                   else
4194                     esym.asym.value = last;
4195
4196                   if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4197                                                       name[i], &esym))
4198                     return false;
4199                 }
4200             }
4201
4202           for (p = o->link_order_head;
4203                p != (struct bfd_link_order *) NULL;
4204                p = p->next)
4205             {
4206               asection *input_section;
4207               bfd *input_bfd;
4208               const struct ecoff_debug_swap *input_swap;
4209               struct ecoff_debug_info input_debug;
4210               char *eraw_src;
4211               char *eraw_end;
4212
4213               if (p->type != bfd_indirect_link_order)
4214                 {
4215                   if (p->type == bfd_fill_link_order)
4216                     continue;
4217                   abort ();
4218                 }
4219
4220               input_section = p->u.indirect.section;
4221               input_bfd = input_section->owner;
4222
4223               if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4224                   || (get_elf_backend_data (input_bfd)
4225                       ->elf_backend_ecoff_debug_swap) == NULL)
4226                 {
4227                   /* I don't know what a non ALPHA ELF bfd would be
4228                      doing with a .mdebug section, but I don't really
4229                      want to deal with it.  */
4230                   continue;
4231                 }
4232
4233               input_swap = (get_elf_backend_data (input_bfd)
4234                             ->elf_backend_ecoff_debug_swap);
4235
4236               BFD_ASSERT (p->size == input_section->_raw_size);
4237
4238               /* The ECOFF linking code expects that we have already
4239                  read in the debugging information and set up an
4240                  ecoff_debug_info structure, so we do that now.  */
4241               if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
4242                                                 &input_debug))
4243                 return false;
4244
4245               if (! (bfd_ecoff_debug_accumulate
4246                      (mdebug_handle, abfd, &debug, swap, input_bfd,
4247                       &input_debug, input_swap, info)))
4248                 return false;
4249
4250               /* Loop through the external symbols.  For each one with
4251                  interesting information, try to find the symbol in
4252                  the linker global hash table and save the information
4253                  for the output external symbols.  */
4254               eraw_src = input_debug.external_ext;
4255               eraw_end = (eraw_src
4256                           + (input_debug.symbolic_header.iextMax
4257                              * input_swap->external_ext_size));
4258               for (;
4259                    eraw_src < eraw_end;
4260                    eraw_src += input_swap->external_ext_size)
4261                 {
4262                   EXTR ext;
4263                   const char *name;
4264                   struct alpha_elf_link_hash_entry *h;
4265
4266                   (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4267                   if (ext.asym.sc == scNil
4268                       || ext.asym.sc == scUndefined
4269                       || ext.asym.sc == scSUndefined)
4270                     continue;
4271
4272                   name = input_debug.ssext + ext.asym.iss;
4273                   h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
4274                                                   name, false, false, true);
4275                   if (h == NULL || h->esym.ifd != -2)
4276                     continue;
4277
4278                   if (ext.ifd != -1)
4279                     {
4280                       BFD_ASSERT (ext.ifd
4281                                   < input_debug.symbolic_header.ifdMax);
4282                       ext.ifd = input_debug.ifdmap[ext.ifd];
4283                     }
4284
4285                   h->esym = ext;
4286                 }
4287
4288               /* Free up the information we just read.  */
4289               free (input_debug.line);
4290               free (input_debug.external_dnr);
4291               free (input_debug.external_pdr);
4292               free (input_debug.external_sym);
4293               free (input_debug.external_opt);
4294               free (input_debug.external_aux);
4295               free (input_debug.ss);
4296               free (input_debug.ssext);
4297               free (input_debug.external_fdr);
4298               free (input_debug.external_rfd);
4299               free (input_debug.external_ext);
4300
4301               /* Hack: reset the SEC_HAS_CONTENTS flag so that
4302                  elf_link_input_bfd ignores this section.  */
4303               input_section->flags &=~ SEC_HAS_CONTENTS;
4304             }
4305
4306 #ifdef ERIC_neverdef
4307           if (info->shared)
4308             {
4309               /* Create .rtproc section.  */
4310               rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4311               if (rtproc_sec == NULL)
4312                 {
4313                   flagword flags = (SEC_HAS_CONTENTS
4314                                     | SEC_IN_MEMORY
4315                                     | SEC_LINKER_CREATED
4316                                     | SEC_READONLY);
4317
4318                   rtproc_sec = bfd_make_section (abfd, ".rtproc");
4319                   if (rtproc_sec == NULL
4320                       || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4321                       || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
4322                     return false;
4323                 }
4324
4325               if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
4326                                                      info, rtproc_sec, &debug))
4327                 return false;
4328             }
4329 #endif
4330
4331           /* Build the external symbol information.  */
4332           einfo.abfd = abfd;
4333           einfo.info = info;
4334           einfo.debug = &debug;
4335           einfo.swap = swap;
4336           einfo.failed = false;
4337           elf_link_hash_traverse (elf_hash_table (info),
4338                                   elf64_alpha_output_extsym,
4339                                   (PTR) &einfo);
4340           if (einfo.failed)
4341             return false;
4342
4343           /* Set the size of the .mdebug section.  */
4344           o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4345
4346           /* Skip this section later on (I don't think this currently
4347              matters, but someday it might).  */
4348           o->link_order_head = (struct bfd_link_order *) NULL;
4349
4350           mdebug_sec = o;
4351         }
4352
4353 #ifdef ERIC_neverdef
4354       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4355         {
4356           const char *subname;
4357           unsigned int c;
4358           Elf64_gptab *tab;
4359           Elf64_External_gptab *ext_tab;
4360           unsigned int i;
4361
4362           /* The .gptab.sdata and .gptab.sbss sections hold
4363              information describing how the small data area would
4364              change depending upon the -G switch.  These sections
4365              not used in executables files.  */
4366           if (! info->relocateable)
4367             {
4368               asection **secpp;
4369
4370               for (p = o->link_order_head;
4371                    p != (struct bfd_link_order *) NULL;
4372                    p = p->next)
4373                 {
4374                   asection *input_section;
4375
4376                   if (p->type != bfd_indirect_link_order)
4377                     {
4378                       if (p->type == bfd_fill_link_order)
4379                         continue;
4380                       abort ();
4381                     }
4382
4383                   input_section = p->u.indirect.section;
4384
4385                   /* Hack: reset the SEC_HAS_CONTENTS flag so that
4386                      elf_link_input_bfd ignores this section.  */
4387                   input_section->flags &=~ SEC_HAS_CONTENTS;
4388                 }
4389
4390               /* Skip this section later on (I don't think this
4391                  currently matters, but someday it might).  */
4392               o->link_order_head = (struct bfd_link_order *) NULL;
4393
4394               /* Really remove the section.  */
4395               for (secpp = &abfd->sections;
4396                    *secpp != o;
4397                    secpp = &(*secpp)->next)
4398                 ;
4399               *secpp = (*secpp)->next;
4400               --abfd->section_count;
4401
4402               continue;
4403             }
4404
4405           /* There is one gptab for initialized data, and one for
4406              uninitialized data.  */
4407           if (strcmp (o->name, ".gptab.sdata") == 0)
4408             gptab_data_sec = o;
4409           else if (strcmp (o->name, ".gptab.sbss") == 0)
4410             gptab_bss_sec = o;
4411           else
4412             {
4413               (*_bfd_error_handler)
4414                 (_("%s: illegal section name `%s'"),
4415                  bfd_get_filename (abfd), o->name);
4416               bfd_set_error (bfd_error_nonrepresentable_section);
4417               return false;
4418             }
4419
4420           /* The linker script always combines .gptab.data and
4421              .gptab.sdata into .gptab.sdata, and likewise for
4422              .gptab.bss and .gptab.sbss.  It is possible that there is
4423              no .sdata or .sbss section in the output file, in which
4424              case we must change the name of the output section.  */
4425           subname = o->name + sizeof ".gptab" - 1;
4426           if (bfd_get_section_by_name (abfd, subname) == NULL)
4427             {
4428               if (o == gptab_data_sec)
4429                 o->name = ".gptab.data";
4430               else
4431                 o->name = ".gptab.bss";
4432               subname = o->name + sizeof ".gptab" - 1;
4433               BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4434             }
4435
4436           /* Set up the first entry.  */
4437           c = 1;
4438           tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
4439           if (tab == NULL)
4440             return false;
4441           tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4442           tab[0].gt_header.gt_unused = 0;
4443
4444           /* Combine the input sections.  */
4445           for (p = o->link_order_head;
4446                p != (struct bfd_link_order *) NULL;
4447                p = p->next)
4448             {
4449               asection *input_section;
4450               bfd *input_bfd;
4451               bfd_size_type size;
4452               unsigned long last;
4453               bfd_size_type gpentry;
4454
4455               if (p->type != bfd_indirect_link_order)
4456                 {
4457                   if (p->type == bfd_fill_link_order)
4458                     continue;
4459                   abort ();
4460                 }
4461
4462               input_section = p->u.indirect.section;
4463               input_bfd = input_section->owner;
4464
4465               /* Combine the gptab entries for this input section one
4466                  by one.  We know that the input gptab entries are
4467                  sorted by ascending -G value.  */
4468               size = bfd_section_size (input_bfd, input_section);
4469               last = 0;
4470               for (gpentry = sizeof (Elf64_External_gptab);
4471                    gpentry < size;
4472                    gpentry += sizeof (Elf64_External_gptab))
4473                 {
4474                   Elf64_External_gptab ext_gptab;
4475                   Elf64_gptab int_gptab;
4476                   unsigned long val;
4477                   unsigned long add;
4478                   boolean exact;
4479                   unsigned int look;
4480
4481                   if (! (bfd_get_section_contents
4482                          (input_bfd, input_section, (PTR) &ext_gptab,
4483                           gpentry, sizeof (Elf64_External_gptab))))
4484                     {
4485                       free (tab);
4486                       return false;
4487                     }
4488
4489                   bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
4490                                                 &int_gptab);
4491                   val = int_gptab.gt_entry.gt_g_value;
4492                   add = int_gptab.gt_entry.gt_bytes - last;
4493
4494                   exact = false;
4495                   for (look = 1; look < c; look++)
4496                     {
4497                       if (tab[look].gt_entry.gt_g_value >= val)
4498                         tab[look].gt_entry.gt_bytes += add;
4499
4500                       if (tab[look].gt_entry.gt_g_value == val)
4501                         exact = true;
4502                     }
4503
4504                   if (! exact)
4505                     {
4506                       Elf64_gptab *new_tab;
4507                       unsigned int max;
4508
4509                       /* We need a new table entry.  */
4510                       new_tab = ((Elf64_gptab *)
4511                                  bfd_realloc ((PTR) tab,
4512                                               (c + 1) * sizeof (Elf64_gptab)));
4513                       if (new_tab == NULL)
4514                         {
4515                           free (tab);
4516                           return false;
4517                         }
4518                       tab = new_tab;
4519                       tab[c].gt_entry.gt_g_value = val;
4520                       tab[c].gt_entry.gt_bytes = add;
4521
4522                       /* Merge in the size for the next smallest -G
4523                          value, since that will be implied by this new
4524                          value.  */
4525                       max = 0;
4526                       for (look = 1; look < c; look++)
4527                         {
4528                           if (tab[look].gt_entry.gt_g_value < val
4529                               && (max == 0
4530                                   || (tab[look].gt_entry.gt_g_value
4531                                       > tab[max].gt_entry.gt_g_value)))
4532                             max = look;
4533                         }
4534                       if (max != 0)
4535                         tab[c].gt_entry.gt_bytes +=
4536                           tab[max].gt_entry.gt_bytes;
4537
4538                       ++c;
4539                     }
4540
4541                   last = int_gptab.gt_entry.gt_bytes;
4542                 }
4543
4544               /* Hack: reset the SEC_HAS_CONTENTS flag so that
4545                  elf_link_input_bfd ignores this section.  */
4546               input_section->flags &=~ SEC_HAS_CONTENTS;
4547             }
4548
4549           /* The table must be sorted by -G value.  */
4550           if (c > 2)
4551             qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4552
4553           /* Swap out the table.  */
4554           ext_tab = ((Elf64_External_gptab *)
4555                      bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
4556           if (ext_tab == NULL)
4557             {
4558               free (tab);
4559               return false;
4560             }
4561
4562           for (i = 0; i < c; i++)
4563             bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
4564           free (tab);
4565
4566           o->_raw_size = c * sizeof (Elf64_External_gptab);
4567           o->contents = (bfd_byte *) ext_tab;
4568
4569           /* Skip this section later on (I don't think this currently
4570              matters, but someday it might).  */
4571           o->link_order_head = (struct bfd_link_order *) NULL;
4572         }
4573 #endif
4574
4575     }
4576
4577   /* Invoke the regular ELF backend linker to do all the work.  */
4578   if (! bfd_elf64_bfd_final_link (abfd, info))
4579     return false;
4580
4581   /* Now write out the computed sections.  */
4582
4583   /* The .got subsections...  */
4584   {
4585     bfd *i, *dynobj = elf_hash_table(info)->dynobj;
4586     for (i = alpha_elf_hash_table(info)->got_list;
4587          i != NULL;
4588          i = alpha_elf_tdata(i)->got_link_next)
4589       {
4590         asection *sgot;
4591
4592         /* elf_bfd_final_link already did everything in dynobj.  */
4593         if (i == dynobj)
4594           continue;
4595
4596         sgot = alpha_elf_tdata(i)->got;
4597         if (! bfd_set_section_contents (abfd, sgot->output_section,
4598                                         sgot->contents, sgot->output_offset,
4599                                         sgot->_raw_size))
4600           return false;
4601       }
4602   }
4603
4604 #ifdef ERIC_neverdef
4605   if (reginfo_sec != (asection *) NULL)
4606     {
4607       Elf64_External_RegInfo ext;
4608
4609       bfd_alpha_elf64_swap_reginfo_out (abfd, &reginfo, &ext);
4610       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4611                                       (file_ptr) 0, sizeof ext))
4612         return false;
4613     }
4614 #endif
4615
4616   if (mdebug_sec != (asection *) NULL)
4617     {
4618       BFD_ASSERT (abfd->output_has_begun);
4619       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4620                                                swap, info,
4621                                                mdebug_sec->filepos))
4622         return false;
4623
4624       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4625     }
4626
4627   if (gptab_data_sec != (asection *) NULL)
4628     {
4629       if (! bfd_set_section_contents (abfd, gptab_data_sec,
4630                                       gptab_data_sec->contents,
4631                                       (file_ptr) 0,
4632                                       gptab_data_sec->_raw_size))
4633         return false;
4634     }
4635
4636   if (gptab_bss_sec != (asection *) NULL)
4637     {
4638       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4639                                       gptab_bss_sec->contents,
4640                                       (file_ptr) 0,
4641                                       gptab_bss_sec->_raw_size))
4642         return false;
4643     }
4644
4645   return true;
4646 }
4647
4648 static enum elf_reloc_type_class
4649 elf64_alpha_reloc_type_class (type)
4650      int type;
4651 {
4652   switch (type)
4653     {
4654     case R_ALPHA_RELATIVE:
4655       return reloc_class_relative;
4656     case R_ALPHA_JMP_SLOT:
4657       return reloc_class_plt;
4658     case R_ALPHA_COPY:
4659       return reloc_class_copy;
4660     default:
4661       return reloc_class_normal;
4662     }
4663 }
4664 \f
4665 /* ECOFF swapping routines.  These are used when dealing with the
4666    .mdebug section, which is in the ECOFF debugging format.  Copied
4667    from elf32-mips.c.  */
4668 static const struct ecoff_debug_swap
4669 elf64_alpha_ecoff_debug_swap =
4670 {
4671   /* Symbol table magic number.  */
4672   magicSym2,
4673   /* Alignment of debugging information.  E.g., 4.  */
4674   8,
4675   /* Sizes of external symbolic information.  */
4676   sizeof (struct hdr_ext),
4677   sizeof (struct dnr_ext),
4678   sizeof (struct pdr_ext),
4679   sizeof (struct sym_ext),
4680   sizeof (struct opt_ext),
4681   sizeof (struct fdr_ext),
4682   sizeof (struct rfd_ext),
4683   sizeof (struct ext_ext),
4684   /* Functions to swap in external symbolic data.  */
4685   ecoff_swap_hdr_in,
4686   ecoff_swap_dnr_in,
4687   ecoff_swap_pdr_in,
4688   ecoff_swap_sym_in,
4689   ecoff_swap_opt_in,
4690   ecoff_swap_fdr_in,
4691   ecoff_swap_rfd_in,
4692   ecoff_swap_ext_in,
4693   _bfd_ecoff_swap_tir_in,
4694   _bfd_ecoff_swap_rndx_in,
4695   /* Functions to swap out external symbolic data.  */
4696   ecoff_swap_hdr_out,
4697   ecoff_swap_dnr_out,
4698   ecoff_swap_pdr_out,
4699   ecoff_swap_sym_out,
4700   ecoff_swap_opt_out,
4701   ecoff_swap_fdr_out,
4702   ecoff_swap_rfd_out,
4703   ecoff_swap_ext_out,
4704   _bfd_ecoff_swap_tir_out,
4705   _bfd_ecoff_swap_rndx_out,
4706   /* Function to read in symbolic data.  */
4707   elf64_alpha_read_ecoff_info
4708 };
4709 \f
4710 /* Use a non-standard hash bucket size of 8.  */
4711
4712 const struct elf_size_info alpha_elf_size_info =
4713 {
4714   sizeof (Elf64_External_Ehdr),
4715   sizeof (Elf64_External_Phdr),
4716   sizeof (Elf64_External_Shdr),
4717   sizeof (Elf64_External_Rel),
4718   sizeof (Elf64_External_Rela),
4719   sizeof (Elf64_External_Sym),
4720   sizeof (Elf64_External_Dyn),
4721   sizeof (Elf_External_Note),
4722   8,
4723   1,
4724   64, 8,
4725   ELFCLASS64, EV_CURRENT,
4726   bfd_elf64_write_out_phdrs,
4727   bfd_elf64_write_shdrs_and_ehdr,
4728   bfd_elf64_write_relocs,
4729   bfd_elf64_swap_symbol_out,
4730   bfd_elf64_slurp_reloc_table,
4731   bfd_elf64_slurp_symbol_table,
4732   bfd_elf64_swap_dyn_in,
4733   bfd_elf64_swap_dyn_out,
4734   NULL,
4735   NULL,
4736   NULL,
4737   NULL
4738 };
4739
4740 #define TARGET_LITTLE_SYM       bfd_elf64_alpha_vec
4741 #define TARGET_LITTLE_NAME      "elf64-alpha"
4742 #define ELF_ARCH                bfd_arch_alpha
4743 #define ELF_MACHINE_CODE        EM_ALPHA
4744 #define ELF_MAXPAGESIZE 0x10000
4745
4746 #define bfd_elf64_bfd_link_hash_table_create \
4747   elf64_alpha_bfd_link_hash_table_create
4748
4749 #define bfd_elf64_bfd_reloc_type_lookup \
4750   elf64_alpha_bfd_reloc_type_lookup
4751 #define elf_info_to_howto \
4752   elf64_alpha_info_to_howto
4753
4754 #define bfd_elf64_mkobject \
4755   elf64_alpha_mkobject
4756 #define elf_backend_object_p \
4757   elf64_alpha_object_p
4758
4759 #define elf_backend_section_from_shdr \
4760   elf64_alpha_section_from_shdr
4761 #define elf_backend_fake_sections \
4762   elf64_alpha_fake_sections
4763
4764 #define bfd_elf64_bfd_is_local_label_name \
4765   elf64_alpha_is_local_label_name
4766 #define bfd_elf64_find_nearest_line \
4767   elf64_alpha_find_nearest_line
4768 #define bfd_elf64_bfd_relax_section \
4769   elf64_alpha_relax_section
4770
4771 #define elf_backend_add_symbol_hook \
4772   elf64_alpha_add_symbol_hook
4773 #define elf_backend_check_relocs \
4774   elf64_alpha_check_relocs
4775 #define elf_backend_create_dynamic_sections \
4776   elf64_alpha_create_dynamic_sections
4777 #define elf_backend_adjust_dynamic_symbol \
4778   elf64_alpha_adjust_dynamic_symbol
4779 #define elf_backend_always_size_sections \
4780   elf64_alpha_always_size_sections
4781 #define elf_backend_size_dynamic_sections \
4782   elf64_alpha_size_dynamic_sections
4783 #define elf_backend_relocate_section \
4784   elf64_alpha_relocate_section
4785 #define elf_backend_finish_dynamic_symbol \
4786   elf64_alpha_finish_dynamic_symbol
4787 #define elf_backend_finish_dynamic_sections \
4788   elf64_alpha_finish_dynamic_sections
4789 #define bfd_elf64_bfd_final_link \
4790   elf64_alpha_final_link
4791 #define elf_backend_reloc_type_class \
4792   elf64_alpha_reloc_type_class
4793
4794 #define elf_backend_ecoff_debug_swap \
4795   &elf64_alpha_ecoff_debug_swap
4796
4797 #define elf_backend_size_info \
4798   alpha_elf_size_info
4799
4800 /* A few constants that determine how the .plt section is set up.  */
4801 #define elf_backend_want_got_plt 0
4802 #define elf_backend_plt_readonly 0
4803 #define elf_backend_want_plt_sym 1
4804 #define elf_backend_got_header_size 0
4805 #define elf_backend_plt_header_size PLT_HEADER_SIZE
4806
4807 #include "elf64-target.h"