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