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