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