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