]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/binutils/bfd/elf64-alpha.c
Initial import of GNU binutils version 2.8.1. Believe it or not,
[FreeBSD/FreeBSD.git] / contrib / binutils / bfd / elf64-alpha.c
1 /* Alpha specific support for 64-bit ELF
2    Copyright 1996, 1997 Free Software Foundation, Inc.
3    Contributed by Richard Henderson <rth@tamu.edu>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* We need a published ABI spec for this.  Until one comes out, don't
22    assume this'll remain unchanged forever.  */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28
29 #include "elf/alpha.h"
30
31 #define ALPHAECOFF
32
33 #define NO_COFF_RELOCS
34 #define NO_COFF_SYMBOLS
35 #define NO_COFF_LINENOS
36
37 /* Get the ECOFF swapping routines.  Needed for the debug information. */
38 #include "coff/internal.h"
39 #include "coff/sym.h"
40 #include "coff/symconst.h"
41 #include "coff/ecoff.h"
42 #include "coff/alpha.h"
43 #include "aout/ar.h"
44 #include "libcoff.h"
45 #include "libecoff.h"
46 #define ECOFF_64
47 #include "ecoffswap.h"
48
49 static boolean elf64_alpha_mkobject PARAMS ((bfd *));
50 static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
51   PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
52 static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
53   PARAMS((bfd *));
54
55 static bfd_reloc_status_type elf64_alpha_reloc_nil
56   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
57 static bfd_reloc_status_type elf64_alpha_reloc_bad
58   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
60   PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
61 static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
62   PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
63
64 static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
65   PARAMS((bfd *, bfd_reloc_code_real_type));
66 static void elf64_alpha_info_to_howto
67   PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
68
69 static boolean elf64_alpha_object_p
70   PARAMS((bfd *));
71 static boolean elf64_alpha_section_from_shdr
72   PARAMS((bfd *, Elf64_Internal_Shdr *, char *));
73 static boolean elf64_alpha_fake_sections
74   PARAMS((bfd *, Elf64_Internal_Shdr *, asection *));
75 static int elf64_alpha_additional_program_headers
76   PARAMS((bfd *));
77 static boolean elf64_alpha_create_got_section
78   PARAMS((bfd *, struct bfd_link_info *));
79 static boolean elf64_alpha_create_dynamic_sections
80   PARAMS((bfd *, struct bfd_link_info *));
81
82 static boolean elf64_alpha_read_ecoff_info
83   PARAMS((bfd *, asection *, struct ecoff_debug_info *));
84 static boolean elf64_alpha_is_local_label_name
85   PARAMS((bfd *, const char *));
86 static boolean elf64_alpha_find_nearest_line
87   PARAMS((bfd *, asection *, asymbol **, bfd_vma, const char **,
88           const char **, unsigned int *));
89
90 #if defined(__STDC__) || defined(ALMOST_STDC)
91 struct alpha_elf_link_hash_entry;
92 #endif
93
94 static boolean elf64_alpha_output_extsym
95   PARAMS((struct alpha_elf_link_hash_entry *, PTR));
96
97 static boolean elf64_alpha_can_merge_gots
98   PARAMS((bfd *, bfd *));
99 static void elf64_alpha_merge_gots
100   PARAMS((bfd *, bfd *));
101 static boolean elf64_alpha_calc_got_offsets_for_symbol
102   PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
103 static void elf64_alpha_calc_got_offsets PARAMS ((struct bfd_link_info *));
104 static void elf64_alpha_strip_section_from_output PARAMS ((asection *));
105 static boolean elf64_alpha_always_size_sections
106   PARAMS ((bfd *, struct bfd_link_info *));
107 static boolean elf64_alpha_calc_dynrel_sizes
108   PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
109 static boolean elf64_alpha_check_relocs
110   PARAMS((bfd *, struct bfd_link_info *, asection *sec,
111           const Elf_Internal_Rela *));
112 static boolean elf64_alpha_adjust_dynamic_symbol
113   PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
114 static boolean elf64_alpha_size_dynamic_sections
115   PARAMS((bfd *, struct bfd_link_info *));
116 static boolean elf64_alpha_adjust_dynindx
117   PARAMS((struct elf_link_hash_entry *, PTR));
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   } *got_entries;
165
166   /* used to count non-got, non-plt relocations for delayed sizing
167      of relocation sections.  */
168   struct alpha_elf_reloc_entry
169   {
170     struct alpha_elf_reloc_entry *next;
171
172     /* which .reloc section? */
173     asection *srel;
174
175     /* what kind of relocation? */
176     unsigned long rtype;
177
178     /* how many did we find?  */
179     unsigned long count;
180   } *reloc_entries;
181 };
182
183 /* Alpha ELF linker hash table.  */
184
185 struct alpha_elf_link_hash_table
186 {
187   struct elf_link_hash_table root;
188
189   /* The head of a list of .got subsections linked through
190      alpha_elf_tdata(abfd)->got_link_next.  */
191   bfd *got_list;
192 };
193
194 /* Look up an entry in a Alpha ELF linker hash table.  */
195
196 #define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
197   ((struct alpha_elf_link_hash_entry *)                                 \
198    elf_link_hash_lookup (&(table)->root, (string), (create),            \
199                          (copy), (follow)))
200
201 /* Traverse a Alpha ELF linker hash table.  */
202
203 #define alpha_elf_link_hash_traverse(table, func, info)                 \
204   (elf_link_hash_traverse                                               \
205    (&(table)->root,                                                     \
206     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
207     (info)))
208
209 /* Get the Alpha ELF linker hash table from a link_info structure.  */
210
211 #define alpha_elf_hash_table(p) \
212   ((struct alpha_elf_link_hash_table *) ((p)->hash))
213
214 /* Get the object's symbols as our own entry type.  */
215
216 #define alpha_elf_sym_hashes(abfd) \
217   ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
218
219 /* Should we do dynamic things to this symbol?  */
220
221 #define alpha_elf_dynamic_symbol_p(h, info)                             \
222   (((info)->shared && !(info)->symbolic && (h)->dynindx != -1)          \
223    || (((h)->elf_link_hash_flags                                        \
224         & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))      \
225        == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
226
227 /* Create an entry in a Alpha ELF linker hash table.  */
228
229 static struct bfd_hash_entry *
230 elf64_alpha_link_hash_newfunc (entry, table, string)
231      struct bfd_hash_entry *entry;
232      struct bfd_hash_table *table;
233      const char *string;
234 {
235   struct alpha_elf_link_hash_entry *ret =
236     (struct alpha_elf_link_hash_entry *) entry;
237
238   /* Allocate the structure if it has not already been allocated by a
239      subclass.  */
240   if (ret == (struct alpha_elf_link_hash_entry *) NULL)
241     ret = ((struct alpha_elf_link_hash_entry *)
242            bfd_hash_allocate (table,
243                               sizeof (struct alpha_elf_link_hash_entry)));
244   if (ret == (struct alpha_elf_link_hash_entry *) NULL)
245     return (struct bfd_hash_entry *) ret;
246
247   /* Call the allocation method of the superclass.  */
248   ret = ((struct alpha_elf_link_hash_entry *)
249          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
250                                      table, string));
251   if (ret != (struct alpha_elf_link_hash_entry *) NULL)
252     {
253       /* Set local fields.  */
254       memset (&ret->esym, 0, sizeof (EXTR));
255       /* We use -2 as a marker to indicate that the information has
256          not been set.  -1 means there is no associated ifd.  */
257       ret->esym.ifd = -2;
258       ret->flags = 0;
259       ret->got_entries = NULL;
260       ret->reloc_entries = NULL;
261     }
262
263   return (struct bfd_hash_entry *) ret;
264 }
265
266 /* Create a Alpha ELF linker hash table.  */
267
268 static struct bfd_link_hash_table *
269 elf64_alpha_bfd_link_hash_table_create (abfd)
270      bfd *abfd;
271 {
272   struct alpha_elf_link_hash_table *ret;
273
274   ret = ((struct alpha_elf_link_hash_table *)
275          bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
276   if (ret == (struct alpha_elf_link_hash_table *) NULL)
277     return NULL;
278
279   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
280                                        elf64_alpha_link_hash_newfunc))
281     {
282       bfd_release (abfd, ret);
283       return NULL;
284     }
285
286   return &ret->root.root;
287 }
288 \f
289 /* We have some private fields hanging off of the elf_tdata structure.  */
290
291 struct alpha_elf_obj_tdata
292 {
293   struct elf_obj_tdata root;
294
295   /* For every input file, these are the got entries for that object's
296      local symbols.  */
297   struct alpha_elf_got_entry ** local_got_entries;
298
299   /* For every input file, this is the object that owns the got that
300      this input file uses.  */
301   bfd *gotobj;
302
303   /* For every got, this is a linked list through the objects using this got */
304   bfd *in_got_link_next;
305
306   /* For every got, this is a link to the next got subsegment.  */
307   bfd *got_link_next;
308
309   /* For every got, this is the section.  */
310   asection *got;
311
312   /* For every got, this is it's total number of *entries*.  */
313   int total_got_entries;
314
315   /* For every got, this is the sum of the number of *entries* required
316      to hold all of the member object's local got.  */
317   int n_local_got_entries;
318 };
319
320 #define alpha_elf_tdata(abfd) \
321   ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
322
323 static boolean
324 elf64_alpha_mkobject (abfd)
325      bfd *abfd;
326 {
327   abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
328   if (abfd->tdata.any == NULL)
329     return false;
330   return true;
331 }
332
333 static boolean
334 elf64_alpha_object_p (abfd)
335      bfd *abfd;
336 {
337   /* Allocate our special target data.  */
338   struct alpha_elf_obj_tdata *new_tdata;
339   new_tdata = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
340   if (new_tdata == NULL)
341     return false;
342   new_tdata->root = *abfd->tdata.elf_obj_data;
343   abfd->tdata.any = new_tdata;
344
345   /* Set the right machine number for an Alpha ELF file.  */
346   return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
347 }
348 \f
349 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
350    from smaller values.  Start with zero, widen, *then* decrement.  */
351 #define MINUS_ONE       (((bfd_vma)0) - 1)
352
353 static reloc_howto_type elf64_alpha_howto_table[] =
354 {
355   HOWTO (R_ALPHA_NONE,          /* type */
356          0,                     /* rightshift */
357          0,                     /* size (0 = byte, 1 = short, 2 = long) */
358          8,                     /* bitsize */
359          true,                  /* pc_relative */
360          0,                     /* bitpos */
361          complain_overflow_dont, /* complain_on_overflow */
362          elf64_alpha_reloc_nil, /* special_function */
363          "NONE",                /* name */
364          false,                 /* partial_inplace */
365          0,                     /* src_mask */
366          0,                     /* dst_mask */
367          true),                 /* pcrel_offset */
368
369   /* A 32 bit reference to a symbol.  */
370   HOWTO (R_ALPHA_REFLONG,       /* type */
371          0,                     /* rightshift */
372          2,                     /* size (0 = byte, 1 = short, 2 = long) */
373          32,                    /* bitsize */
374          false,                 /* pc_relative */
375          0,                     /* bitpos */
376          complain_overflow_bitfield, /* complain_on_overflow */
377          0,                     /* special_function */
378          "REFLONG",             /* name */
379          false,                 /* partial_inplace */
380          0xffffffff,            /* src_mask */
381          0xffffffff,            /* dst_mask */
382          false),                /* pcrel_offset */
383
384   /* A 64 bit reference to a symbol.  */
385   HOWTO (R_ALPHA_REFQUAD,       /* type */
386          0,                     /* rightshift */
387          4,                     /* size (0 = byte, 1 = short, 2 = long) */
388          64,                    /* bitsize */
389          false,                 /* pc_relative */
390          0,                     /* bitpos */
391          complain_overflow_bitfield, /* complain_on_overflow */
392          0,                     /* special_function */
393          "REFQUAD",             /* name */
394          false,                 /* partial_inplace */
395          MINUS_ONE,             /* src_mask */
396          MINUS_ONE,             /* dst_mask */
397          false),                /* pcrel_offset */
398
399   /* A 32 bit GP relative offset.  This is just like REFLONG except
400      that when the value is used the value of the gp register will be
401      added in.  */
402   HOWTO (R_ALPHA_GPREL32,       /* type */
403          0,                     /* rightshift */
404          2,                     /* size (0 = byte, 1 = short, 2 = long) */
405          32,                    /* bitsize */
406          false,                 /* pc_relative */
407          0,                     /* bitpos */
408          complain_overflow_bitfield, /* complain_on_overflow */
409          0,                     /* special_function */
410          "GPREL32",             /* name */
411          false,                 /* partial_inplace */
412          0xffffffff,            /* src_mask */
413          0xffffffff,            /* dst_mask */
414          false),                /* pcrel_offset */
415
416   /* Used for an instruction that refers to memory off the GP register.  */
417   HOWTO (R_ALPHA_LITERAL,       /* type */
418          0,                     /* rightshift */
419          2,                     /* size (0 = byte, 1 = short, 2 = long) */
420          16,                    /* bitsize */
421          false,                 /* pc_relative */
422          0,                     /* bitpos */
423          complain_overflow_signed, /* complain_on_overflow */
424          0,                     /* special_function */
425          "ELF_LITERAL",         /* name */
426          false,                 /* partial_inplace */
427          0xffff,                /* src_mask */
428          0xffff,                /* dst_mask */
429          false),                /* pcrel_offset */
430
431   /* This reloc only appears immediately following an ELF_LITERAL reloc.
432      It identifies a use of the literal.  The symbol index is special:
433      1 means the literal address is in the base register of a memory
434      format instruction; 2 means the literal address is in the byte
435      offset register of a byte-manipulation instruction; 3 means the
436      literal address is in the target register of a jsr instruction.
437      This does not actually do any relocation.  */
438   HOWTO (R_ALPHA_LITUSE,        /* type */
439          0,                     /* rightshift */
440          2,                     /* size (0 = byte, 1 = short, 2 = long) */
441          32,                    /* bitsize */
442          false,                 /* pc_relative */
443          0,                     /* bitpos */
444          complain_overflow_dont, /* complain_on_overflow */
445          elf64_alpha_reloc_nil, /* special_function */
446          "LITUSE",              /* name */
447          false,                 /* partial_inplace */
448          0,                     /* src_mask */
449          0,                     /* dst_mask */
450          false),                /* pcrel_offset */
451
452   /* Load the gp register.  This is always used for a ldah instruction
453      which loads the upper 16 bits of the gp register.  The symbol
454      index of the GPDISP instruction is an offset in bytes to the lda
455      instruction that loads the lower 16 bits.  The value to use for
456      the relocation is the difference between the GP value and the
457      current location; the load will always be done against a register
458      holding the current address.
459
460      NOTE: Unlike ECOFF, partial in-place relocation is not done.  If
461      any offset is present in the instructions, it is an offset from
462      the register to the ldah instruction.  This lets us avoid any
463      stupid hackery like inventing a gp value to do partial relocation
464      against.  Also unlike ECOFF, we do the whole relocation off of
465      the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair.  An odd,
466      space consuming bit, that, since all the information was present
467      in the GPDISP_HI16 reloc.  */
468   HOWTO (R_ALPHA_GPDISP,        /* type */
469          16,                    /* rightshift */
470          2,                     /* size (0 = byte, 1 = short, 2 = long) */
471          16,                    /* bitsize */
472          false,                 /* pc_relative */
473          0,                     /* bitpos */
474          complain_overflow_dont, /* complain_on_overflow */
475          elf64_alpha_reloc_gpdisp, /* special_function */
476          "GPDISP",              /* name */
477          false,                 /* partial_inplace */
478          0xffff,                /* src_mask */
479          0xffff,                /* dst_mask */
480          true),                 /* pcrel_offset */
481
482   /* A 21 bit branch.  */
483   HOWTO (R_ALPHA_BRADDR,        /* type */
484          2,                     /* rightshift */
485          2,                     /* size (0 = byte, 1 = short, 2 = long) */
486          21,                    /* bitsize */
487          true,                  /* pc_relative */
488          0,                     /* bitpos */
489          complain_overflow_signed, /* complain_on_overflow */
490          0,                     /* special_function */
491          "BRADDR",              /* name */
492          false,                 /* partial_inplace */
493          0x1fffff,              /* src_mask */
494          0x1fffff,              /* dst_mask */
495          true),                 /* pcrel_offset */
496
497   /* A hint for a jump to a register.  */
498   HOWTO (R_ALPHA_HINT,          /* type */
499          2,                     /* rightshift */
500          2,                     /* size (0 = byte, 1 = short, 2 = long) */
501          14,                    /* bitsize */
502          true,                  /* pc_relative */
503          0,                     /* bitpos */
504          complain_overflow_dont, /* complain_on_overflow */
505          0,                     /* special_function */
506          "HINT",                /* name */
507          false,                 /* partial_inplace */
508          0x3fff,                /* src_mask */
509          0x3fff,                /* dst_mask */
510          true),                 /* pcrel_offset */
511
512   /* 16 bit PC relative offset.  */
513   HOWTO (R_ALPHA_SREL16,        /* type */
514          0,                     /* rightshift */
515          1,                     /* size (0 = byte, 1 = short, 2 = long) */
516          16,                    /* bitsize */
517          true,                  /* pc_relative */
518          0,                     /* bitpos */
519          complain_overflow_signed, /* complain_on_overflow */
520          0,                     /* special_function */
521          "SREL16",              /* name */
522          false,                 /* partial_inplace */
523          0xffff,                /* src_mask */
524          0xffff,                /* dst_mask */
525          false),                /* pcrel_offset */
526
527   /* 32 bit PC relative offset.  */
528   HOWTO (R_ALPHA_SREL32,        /* type */
529          0,                     /* rightshift */
530          2,                     /* size (0 = byte, 1 = short, 2 = long) */
531          32,                    /* bitsize */
532          true,                  /* pc_relative */
533          0,                     /* bitpos */
534          complain_overflow_signed, /* complain_on_overflow */
535          0,                     /* special_function */
536          "SREL32",              /* name */
537          false,                 /* partial_inplace */
538          0xffffffff,            /* src_mask */
539          0xffffffff,            /* dst_mask */
540          false),                /* pcrel_offset */
541
542   /* A 64 bit PC relative offset.  */
543   HOWTO (R_ALPHA_SREL64,        /* type */
544          0,                     /* rightshift */
545          4,                     /* size (0 = byte, 1 = short, 2 = long) */
546          64,                    /* bitsize */
547          true,                  /* pc_relative */
548          0,                     /* bitpos */
549          complain_overflow_signed, /* complain_on_overflow */
550          0,                     /* special_function */
551          "SREL64",              /* name */
552          false,                 /* partial_inplace */
553          MINUS_ONE,             /* src_mask */
554          MINUS_ONE,             /* dst_mask */
555          false),                /* pcrel_offset */
556
557   /* Push a value on the reloc evaluation stack.  */
558   HOWTO (ALPHA_R_OP_PUSH,       /* type */
559          0,                     /* rightshift */
560          0,                     /* size (0 = byte, 1 = short, 2 = long) */
561          0,                     /* bitsize */
562          false,                 /* pc_relative */
563          0,                     /* bitpos */
564          complain_overflow_dont, /* complain_on_overflow */
565          elf64_alpha_reloc_bad, /* special_function */
566          "OP_PUSH",             /* name */
567          false,                 /* partial_inplace */
568          0,                     /* src_mask */
569          0,                     /* dst_mask */
570          false),                /* pcrel_offset */
571
572   /* Store the value from the stack at the given address.  Store it in
573      a bitfield of size r_size starting at bit position r_offset.  */
574   HOWTO (ALPHA_R_OP_STORE,      /* type */
575          0,                     /* rightshift */
576          4,                     /* size (0 = byte, 1 = short, 2 = long) */
577          64,                    /* bitsize */
578          false,                 /* pc_relative */
579          0,                     /* bitpos */
580          complain_overflow_dont, /* complain_on_overflow */
581          elf64_alpha_reloc_bad, /* special_function */
582          "OP_STORE",            /* name */
583          false,                 /* partial_inplace */
584          0,                     /* src_mask */
585          MINUS_ONE,             /* dst_mask */
586          false),                /* pcrel_offset */
587
588   /* Subtract the reloc address from the value on the top of the
589      relocation stack.  */
590   HOWTO (ALPHA_R_OP_PSUB,       /* type */
591          0,                     /* rightshift */
592          0,                     /* size (0 = byte, 1 = short, 2 = long) */
593          0,                     /* bitsize */
594          false,                 /* pc_relative */
595          0,                     /* bitpos */
596          complain_overflow_dont, /* complain_on_overflow */
597          elf64_alpha_reloc_bad, /* special_function */
598          "OP_PSUB",             /* name */
599          false,                 /* partial_inplace */
600          0,                     /* src_mask */
601          0,                     /* dst_mask */
602          false),                /* pcrel_offset */
603
604   /* Shift the value on the top of the relocation stack right by the
605      given value.  */
606   HOWTO (ALPHA_R_OP_PRSHIFT,    /* type */
607          0,                     /* rightshift */
608          0,                     /* size (0 = byte, 1 = short, 2 = long) */
609          0,                     /* bitsize */
610          false,                 /* pc_relative */
611          0,                     /* bitpos */
612          complain_overflow_dont, /* complain_on_overflow */
613          elf64_alpha_reloc_bad, /* special_function */
614          "OP_PRSHIFT",          /* name */
615          false,                 /* partial_inplace */
616          0,                     /* src_mask */
617          0,                     /* dst_mask */
618          false),                /* pcrel_offset */
619
620   /* Misc ELF relocations. */
621   HOWTO (R_ALPHA_COPY,
622          0,
623          0,
624          0,
625          false,
626          0,
627          complain_overflow_dont,
628          bfd_elf_generic_reloc,
629          "COPY",
630          false,
631          0,
632          0,
633          true),
634
635   HOWTO (R_ALPHA_GLOB_DAT,
636          0,
637          0,
638          0,
639          false,
640          0,
641          complain_overflow_dont,
642          bfd_elf_generic_reloc,
643          "GLOB_DAT",
644          false,
645          0,
646          0,
647          true),
648
649   HOWTO (R_ALPHA_JMP_SLOT,
650          0,
651          0,
652          0,
653          false,
654          0,
655          complain_overflow_dont,
656          bfd_elf_generic_reloc,
657          "JMP_SLOT",
658          false,
659          0,
660          0,
661          true),
662
663   HOWTO (R_ALPHA_RELATIVE,
664          0,
665          0,
666          0,
667          false,
668          0,
669          complain_overflow_dont,
670          bfd_elf_generic_reloc,
671          "RELATIVE",
672          false,
673          0,
674          0,
675          true)
676 };
677
678 /* A relocation function which doesn't do anything.  */
679
680 static bfd_reloc_status_type
681 elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
682      bfd *abfd;
683      arelent *reloc;
684      asymbol *sym;
685      PTR data;
686      asection *sec;
687      bfd *output_bfd;
688      char **error_message;
689 {
690   if (output_bfd)
691     reloc->address += sec->output_offset;
692   return bfd_reloc_ok;
693 }
694
695 /* A relocation function used for an unsupported reloc.  */
696
697 static bfd_reloc_status_type
698 elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
699      bfd *abfd;
700      arelent *reloc;
701      asymbol *sym;
702      PTR data;
703      asection *sec;
704      bfd *output_bfd;
705      char **error_message;
706 {
707   if (output_bfd)
708     reloc->address += sec->output_offset;
709   return bfd_reloc_notsupported;
710 }
711
712 /* Do the work of the GPDISP relocation.  */
713
714 static bfd_reloc_status_type
715 elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
716      bfd *abfd;
717      bfd_vma gpdisp;
718      bfd_byte *p_ldah;
719      bfd_byte *p_lda;
720 {
721   bfd_reloc_status_type ret = bfd_reloc_ok;
722   bfd_vma addend;
723   unsigned long i_ldah, i_lda;
724
725   i_ldah = bfd_get_32 (abfd, p_ldah);
726   i_lda = bfd_get_32 (abfd, p_lda);
727
728   /* Complain if the instructions are not correct.  */
729   if (((i_ldah >> 26) & 0x3f) != 0x09
730       || ((i_lda >> 26) & 0x3f) != 0x08)
731     ret = bfd_reloc_dangerous;
732
733   /* Extract the user-supplied offset, mirroring the sign extensions
734      that the instructions perform.  */
735   addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
736   addend = (addend ^ 0x80008000) - 0x80008000;
737
738   gpdisp += addend;
739
740   if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma)0x80000000
741       || gpdisp >= 0x7fff8000)
742     ret = bfd_reloc_overflow;
743
744   /* compensate for the sign extension again.  */
745   i_ldah = ((i_ldah & 0xffff0000)
746             | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
747   i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
748
749   bfd_put_32 (abfd, i_ldah, p_ldah);
750   bfd_put_32 (abfd, i_lda, p_lda);
751
752   return ret;
753 }
754
755 /* The special function for the GPDISP reloc.  */
756
757 static bfd_reloc_status_type
758 elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
759                           output_bfd, err_msg)
760      bfd *abfd;
761      arelent *reloc_entry;
762      asymbol *sym;
763      PTR data;
764      asection *input_section;
765      bfd *output_bfd;
766      char **err_msg;
767 {
768   bfd_reloc_status_type ret;
769   bfd_vma gp, relocation;
770   bfd_byte *p_ldah, *p_lda;
771
772   /* Don't do anything if we're not doing a final link.  */
773   if (output_bfd)
774     {
775       reloc_entry->address += input_section->output_offset;
776       return bfd_reloc_ok;
777     }
778
779   if (reloc_entry->address > input_section->_cooked_size ||
780       reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
781     return bfd_reloc_outofrange;
782
783   /* The gp used in the portion of the output object to which this
784      input object belongs is cached on the input bfd.  */
785   gp = _bfd_get_gp_value (abfd);
786
787   relocation = (input_section->output_section->vma
788                 + input_section->output_offset
789                 + reloc_entry->address);
790
791   p_ldah = (bfd_byte *) data + reloc_entry->address;
792   p_lda = p_ldah + reloc_entry->addend;
793
794   ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
795
796   /* Complain if the instructions are not correct.  */
797   if (ret == bfd_reloc_dangerous)
798     *err_msg = "GPDISP relocation did not find ldah and lda instructions";
799
800   return ret;
801 }
802
803 /* A mapping from BFD reloc types to Alpha ELF reloc types.  */
804
805 struct elf_reloc_map
806 {
807   bfd_reloc_code_real_type bfd_reloc_val;
808   int elf_reloc_val;
809 };
810
811 static const struct elf_reloc_map elf64_alpha_reloc_map[] =
812 {
813   {BFD_RELOC_NONE,              R_ALPHA_NONE},
814   {BFD_RELOC_32,                R_ALPHA_REFLONG},
815   {BFD_RELOC_64,                R_ALPHA_REFQUAD},
816   {BFD_RELOC_CTOR,              R_ALPHA_REFQUAD},
817   {BFD_RELOC_GPREL32,           R_ALPHA_GPREL32},
818   {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
819   {BFD_RELOC_ALPHA_LITUSE,      R_ALPHA_LITUSE},
820   {BFD_RELOC_ALPHA_GPDISP,      R_ALPHA_GPDISP},
821   {BFD_RELOC_23_PCREL_S2,       R_ALPHA_BRADDR},
822   {BFD_RELOC_ALPHA_HINT,        R_ALPHA_HINT},
823   {BFD_RELOC_16_PCREL,          R_ALPHA_SREL16},
824   {BFD_RELOC_32_PCREL,          R_ALPHA_SREL32},
825   {BFD_RELOC_64_PCREL,          R_ALPHA_SREL64},
826 };
827
828 /* Given a BFD reloc type, return a HOWTO structure.  */
829
830 static reloc_howto_type *
831 elf64_alpha_bfd_reloc_type_lookup (abfd, code)
832      bfd *abfd;
833      bfd_reloc_code_real_type code;
834 {
835   const struct elf_reloc_map *i, *e;
836   i = e = elf64_alpha_reloc_map;
837   e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
838   for (; i != e; ++i)
839     {
840       if (i->bfd_reloc_val == code)
841         return &elf64_alpha_howto_table[i->elf_reloc_val];
842     }
843   return 0;
844 }
845
846 /* Given an Alpha ELF reloc type, fill in an arelent structure.  */
847
848 static void
849 elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
850      bfd *abfd;
851      arelent *cache_ptr;
852      Elf64_Internal_Rela *dst;
853 {
854   unsigned r_type;
855
856   r_type = ELF64_R_TYPE(dst->r_info);
857   BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
858   cache_ptr->howto = &elf64_alpha_howto_table[r_type];
859 }
860 \f
861 /* PLT/GOT Stuff */
862 #define PLT_HEADER_SIZE 32
863 #define PLT_HEADER_WORD1        0xc3600000      /* br   $27,.+4     */
864 #define PLT_HEADER_WORD2        0xa77b000c      /* ldq  $27,12($27) */
865 #define PLT_HEADER_WORD3        0x47ff041f      /* nop              */
866 #define PLT_HEADER_WORD4        0x6b7b0000      /* jmp  $27,($27)   */
867
868 #define PLT_ENTRY_SIZE 12
869 #define PLT_ENTRY_WORD1         0x279f0000      /* ldah $28, 0($31) */
870 #define PLT_ENTRY_WORD2         0x239c0000      /* lda  $28, 0($28) */
871 #define PLT_ENTRY_WORD3         0xc3e00000      /* br   $31, plt0   */
872
873 #define MAX_GOT_ENTRIES         (64*1024 / 8)
874
875 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
876 \f
877 /* Handle an Alpha specific section when reading an object file.  This
878    is called when elfcode.h finds a section with an unknown type.
879    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
880    how to.  */
881
882 static boolean
883 elf64_alpha_section_from_shdr (abfd, hdr, name)
884      bfd *abfd;
885      Elf64_Internal_Shdr *hdr;
886      char *name;
887 {
888   asection *newsect;
889
890   /* There ought to be a place to keep ELF backend specific flags, but
891      at the moment there isn't one.  We just keep track of the
892      sections by their name, instead.  Fortunately, the ABI gives
893      suggested names for all the MIPS specific sections, so we will
894      probably get away with this.  */
895   switch (hdr->sh_type)
896     {
897     case SHT_ALPHA_DEBUG:
898       if (strcmp (name, ".mdebug") != 0)
899         return false;
900       break;
901 #ifdef ERIC_neverdef
902     case SHT_ALPHA_REGINFO:
903       if (strcmp (name, ".reginfo") != 0
904           || hdr->sh_size != sizeof (Elf64_External_RegInfo))
905         return false;
906       break;
907 #endif
908     default:
909       return false;
910     }
911
912   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
913     return false;
914   newsect = hdr->bfd_section;
915
916   if (hdr->sh_type == SHT_ALPHA_DEBUG)
917     {
918       if (! bfd_set_section_flags (abfd, newsect,
919                                    (bfd_get_section_flags (abfd, newsect)
920                                     | SEC_DEBUGGING)))
921         return false;
922     }
923
924 #ifdef ERIC_neverdef
925   /* For a .reginfo section, set the gp value in the tdata information
926      from the contents of this section.  We need the gp value while
927      processing relocs, so we just get it now.  */
928   if (hdr->sh_type == SHT_ALPHA_REGINFO)
929     {
930       Elf64_External_RegInfo ext;
931       Elf64_RegInfo s;
932
933       if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
934                                       (file_ptr) 0, sizeof ext))
935         return false;
936       bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
937       elf_gp (abfd) = s.ri_gp_value;
938     }
939 #endif
940
941   return true;
942 }
943
944 /* Set the correct type for an Alpha ELF section.  We do this by the
945    section name, which is a hack, but ought to work.  */
946
947 static boolean
948 elf64_alpha_fake_sections (abfd, hdr, sec)
949      bfd *abfd;
950      Elf64_Internal_Shdr *hdr;
951      asection *sec;
952 {
953   register const char *name;
954
955   name = bfd_get_section_name (abfd, sec);
956
957   if (strcmp (name, ".mdebug") == 0)
958     {
959       hdr->sh_type = SHT_ALPHA_DEBUG;
960       /* In a shared object on Irix 5.3, the .mdebug section has an
961          entsize of 0.  FIXME: Does this matter?  */
962       if ((abfd->flags & DYNAMIC) != 0 )
963         hdr->sh_entsize = 0;
964       else
965         hdr->sh_entsize = 1;
966     }
967 #ifdef ERIC_neverdef
968   else if (strcmp (name, ".reginfo") == 0)
969     {
970       hdr->sh_type = SHT_ALPHA_REGINFO;
971       /* In a shared object on Irix 5.3, the .reginfo section has an
972          entsize of 0x18.  FIXME: Does this matter?  */
973       if ((abfd->flags & DYNAMIC) != 0)
974         hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
975       else
976         hdr->sh_entsize = 1;
977
978       /* Force the section size to the correct value, even if the
979          linker thinks it is larger.  The link routine below will only
980          write out this much data for .reginfo.  */
981       hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
982     }
983   else if (strcmp (name, ".hash") == 0
984            || strcmp (name, ".dynamic") == 0
985            || strcmp (name, ".dynstr") == 0)
986     {
987       hdr->sh_entsize = 0;
988       hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
989     }
990 #endif
991   else if (strcmp (name, ".sdata") == 0
992            || strcmp (name, ".sbss") == 0
993            || strcmp (name, ".lit4") == 0
994            || strcmp (name, ".lit8") == 0)
995     hdr->sh_flags |= SHF_ALPHA_GPREL;
996
997   return true;
998 }
999
1000 /* Return the number of additional phdrs we will need.  */
1001
1002 static int
1003 elf64_alpha_additional_program_headers (abfd)
1004      bfd *abfd;
1005 {
1006   asection *s;
1007   int ret;
1008
1009   ret = 0;
1010
1011   s = bfd_get_section_by_name (abfd, ".reginfo");
1012   if (s != NULL && (s->flags & SEC_LOAD) != 0)
1013     {
1014       /* We need a PT_ALPHA_REGINFO segment.  */
1015       ++ret;
1016     }
1017
1018   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
1019       && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
1020     {
1021       /* We need a PT_ALPHA_RTPROC segment.  */
1022       ++ret;
1023     }
1024
1025   return ret;
1026 }
1027
1028 /* Create the .got section.  */
1029
1030 static boolean
1031 elf64_alpha_create_got_section(abfd, info)
1032      bfd *abfd;
1033      struct bfd_link_info *info;
1034 {
1035   asection *s;
1036
1037   if (bfd_get_section_by_name (abfd, ".got"))
1038     return true;
1039
1040   s = bfd_make_section (abfd, ".got");
1041   if (s == NULL
1042       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1043                                            | SEC_HAS_CONTENTS
1044                                            | SEC_IN_MEMORY
1045                                            | SEC_LINKER_CREATED))
1046       || !bfd_set_section_alignment (abfd, s, 3))
1047     return false;
1048
1049   alpha_elf_tdata (abfd)->got = s;
1050
1051   return true;
1052 }
1053
1054 /* Create all the dynamic sections.  */
1055
1056 static boolean
1057 elf64_alpha_create_dynamic_sections (abfd, info)
1058      bfd *abfd;
1059      struct bfd_link_info *info;
1060 {
1061   asection *s;
1062   struct elf_link_hash_entry *h;
1063
1064   /* We need to create .plt, .rela.plt, .got, and .rela.got sections.  */
1065
1066   s = bfd_make_section (abfd, ".plt");
1067   if (s == NULL
1068       || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1069                                             | SEC_HAS_CONTENTS
1070                                             | SEC_IN_MEMORY
1071                                             | SEC_LINKER_CREATED
1072                                             | SEC_CODE))
1073       || ! bfd_set_section_alignment (abfd, s, 3))
1074     return false;
1075
1076   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1077      .plt section.  */
1078   h = NULL;
1079   if (! (_bfd_generic_link_add_one_symbol
1080          (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1081           (bfd_vma) 0, (const char *) NULL, false,
1082           get_elf_backend_data (abfd)->collect,
1083           (struct bfd_link_hash_entry **) &h)))
1084     return false;
1085   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1086   h->type = STT_OBJECT;
1087
1088   if (info->shared
1089       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1090     return false;
1091
1092   s = bfd_make_section (abfd, ".rela.plt");
1093   if (s == NULL
1094       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1095                                            | SEC_HAS_CONTENTS
1096                                            | SEC_IN_MEMORY
1097                                            | SEC_LINKER_CREATED
1098                                            | SEC_READONLY))
1099       || ! bfd_set_section_alignment (abfd, s, 3))
1100     return false;
1101
1102   /* We may or may not have created a .got section for this object, but
1103      we definitely havn't done the rest of the work.  */
1104
1105   if (!elf64_alpha_create_got_section (abfd, info))
1106     return false;
1107
1108   s = bfd_make_section(abfd, ".rela.got");
1109   if (s == NULL
1110       || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1111                                            | SEC_HAS_CONTENTS
1112                                            | SEC_IN_MEMORY
1113                                            | SEC_LINKER_CREATED
1114                                            | SEC_READONLY))
1115       || !bfd_set_section_alignment (abfd, s, 3))
1116     return false;
1117
1118   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1119      dynobj's .got section.  We don't do this in the linker script
1120      because we don't want to define the symbol if we are not creating
1121      a global offset table.  */
1122   h = NULL;
1123   if (!(_bfd_generic_link_add_one_symbol
1124         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
1125          alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
1126          false, get_elf_backend_data (abfd)->collect,
1127          (struct bfd_link_hash_entry **) &h)))
1128     return false;
1129   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1130   h->type = STT_OBJECT;
1131
1132   if (info->shared
1133       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1134     return false;
1135
1136   elf_hash_table (info)->hgot = h;
1137
1138   return true;
1139 }
1140 \f
1141 /* Read ECOFF debugging information from a .mdebug section into a
1142    ecoff_debug_info structure.  */
1143
1144 static boolean
1145 elf64_alpha_read_ecoff_info (abfd, section, debug)
1146      bfd *abfd;
1147      asection *section;
1148      struct ecoff_debug_info *debug;
1149 {
1150   HDRR *symhdr;
1151   const struct ecoff_debug_swap *swap;
1152   char *ext_hdr = NULL;
1153
1154   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1155
1156   ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
1157   if (ext_hdr == NULL && swap->external_hdr_size != 0)
1158     goto error_return;
1159
1160   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1161                                 swap->external_hdr_size)
1162       == false)
1163     goto error_return;
1164
1165   symhdr = &debug->symbolic_header;
1166   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1167
1168   /* The symbolic header contains absolute file offsets and sizes to
1169      read.  */
1170 #define READ(ptr, offset, count, size, type)                            \
1171   if (symhdr->count == 0)                                               \
1172     debug->ptr = NULL;                                                  \
1173   else                                                                  \
1174     {                                                                   \
1175       debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
1176       if (debug->ptr == NULL)                                           \
1177         goto error_return;                                              \
1178       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0     \
1179           || (bfd_read (debug->ptr, size, symhdr->count,                \
1180                         abfd) != size * symhdr->count))                 \
1181         goto error_return;                                              \
1182     }
1183
1184   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1185   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1186   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1187   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1188   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1189   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1190         union aux_ext *);
1191   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1192   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1193   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1194   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1195   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1196 #undef READ
1197
1198   debug->fdr = NULL;
1199   debug->adjust = NULL;
1200
1201   return true;
1202
1203  error_return:
1204   if (ext_hdr != NULL)
1205     free (ext_hdr);
1206   if (debug->line != NULL)
1207     free (debug->line);
1208   if (debug->external_dnr != NULL)
1209     free (debug->external_dnr);
1210   if (debug->external_pdr != NULL)
1211     free (debug->external_pdr);
1212   if (debug->external_sym != NULL)
1213     free (debug->external_sym);
1214   if (debug->external_opt != NULL)
1215     free (debug->external_opt);
1216   if (debug->external_aux != NULL)
1217     free (debug->external_aux);
1218   if (debug->ss != NULL)
1219     free (debug->ss);
1220   if (debug->ssext != NULL)
1221     free (debug->ssext);
1222   if (debug->external_fdr != NULL)
1223     free (debug->external_fdr);
1224   if (debug->external_rfd != NULL)
1225     free (debug->external_rfd);
1226   if (debug->external_ext != NULL)
1227     free (debug->external_ext);
1228   return false;
1229 }
1230
1231 /* Alpha ELF local labels start with '$'.  */
1232
1233 static boolean
1234 elf64_alpha_is_local_label_name (abfd, name)
1235      bfd *abfd;
1236      const char *name;
1237 {
1238   return name[0] == '$';
1239 }
1240
1241 /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
1242    routine in order to handle the ECOFF debugging information.  We
1243    still call this mips_elf_find_line because of the slot
1244    find_line_info in elf_obj_tdata is declared that way.  */
1245
1246 struct mips_elf_find_line
1247 {
1248   struct ecoff_debug_info d;
1249   struct ecoff_find_line i;
1250 };
1251
1252 static boolean
1253 elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
1254                                functionname_ptr, line_ptr)
1255      bfd *abfd;
1256      asection *section;
1257      asymbol **symbols;
1258      bfd_vma offset;
1259      const char **filename_ptr;
1260      const char **functionname_ptr;
1261      unsigned int *line_ptr;
1262 {
1263   asection *msec;
1264
1265   msec = bfd_get_section_by_name (abfd, ".mdebug");
1266   if (msec != NULL)
1267     {
1268       flagword origflags;
1269       struct mips_elf_find_line *fi;
1270       const struct ecoff_debug_swap * const swap =
1271         get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1272
1273       /* If we are called during a link, alpha_elf_final_link may have
1274          cleared the SEC_HAS_CONTENTS field.  We force it back on here
1275          if appropriate (which it normally will be).  */
1276       origflags = msec->flags;
1277       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
1278         msec->flags |= SEC_HAS_CONTENTS;
1279
1280       fi = elf_tdata (abfd)->find_line_info;
1281       if (fi == NULL)
1282         {
1283           bfd_size_type external_fdr_size;
1284           char *fraw_src;
1285           char *fraw_end;
1286           struct fdr *fdr_ptr;
1287
1288           fi = ((struct mips_elf_find_line *)
1289                 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
1290           if (fi == NULL)
1291             {
1292               msec->flags = origflags;
1293               return false;
1294             }
1295
1296           if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
1297             {
1298               msec->flags = origflags;
1299               return false;
1300             }
1301
1302           /* Swap in the FDR information.  */
1303           fi->d.fdr = ((struct fdr *)
1304                        bfd_alloc (abfd,
1305                                   (fi->d.symbolic_header.ifdMax *
1306                                    sizeof (struct fdr))));
1307           if (fi->d.fdr == NULL)
1308             {
1309               msec->flags = origflags;
1310               return false;
1311             }
1312           external_fdr_size = swap->external_fdr_size;
1313           fdr_ptr = fi->d.fdr;
1314           fraw_src = (char *) fi->d.external_fdr;
1315           fraw_end = (fraw_src
1316                       + fi->d.symbolic_header.ifdMax * external_fdr_size);
1317           for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
1318             (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
1319
1320           elf_tdata (abfd)->find_line_info = fi;
1321
1322           /* Note that we don't bother to ever free this information.
1323              find_nearest_line is either called all the time, as in
1324              objdump -l, so the information should be saved, or it is
1325              rarely called, as in ld error messages, so the memory
1326              wasted is unimportant.  Still, it would probably be a
1327              good idea for free_cached_info to throw it away.  */
1328         }
1329
1330       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
1331                                   &fi->i, filename_ptr, functionname_ptr,
1332                                   line_ptr))
1333         {
1334           msec->flags = origflags;
1335           return true;
1336         }
1337
1338       msec->flags = origflags;
1339     }
1340
1341   /* Fall back on the generic ELF find_nearest_line routine.  */
1342
1343   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
1344                                      filename_ptr, functionname_ptr,
1345                                      line_ptr);
1346 }
1347 \f
1348 /* Structure used to pass information to alpha_elf_output_extsym.  */
1349
1350 struct extsym_info
1351 {
1352   bfd *abfd;
1353   struct bfd_link_info *info;
1354   struct ecoff_debug_info *debug;
1355   const struct ecoff_debug_swap *swap;
1356   boolean failed;
1357 };
1358
1359 static boolean
1360 elf64_alpha_output_extsym (h, data)
1361      struct alpha_elf_link_hash_entry *h;
1362      PTR data;
1363 {
1364   struct extsym_info *einfo = (struct extsym_info *) data;
1365   boolean strip;
1366   asection *sec, *output_section;
1367
1368   if (h->root.indx == -2)
1369     strip = false;
1370   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1371            || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1372           && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1373           && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
1374     strip = true;
1375   else if (einfo->info->strip == strip_all
1376           || (einfo->info->strip == strip_some
1377               && bfd_hash_lookup (einfo->info->keep_hash,
1378                                   h->root.root.root.string,
1379                                   false, false) == NULL))
1380     strip = true;
1381   else
1382     strip = false;
1383
1384   if (strip)
1385     return true;
1386
1387   if (h->esym.ifd == -2)
1388     {
1389       h->esym.jmptbl = 0;
1390       h->esym.cobol_main = 0;
1391       h->esym.weakext = 0;
1392       h->esym.reserved = 0;
1393       h->esym.ifd = ifdNil;
1394       h->esym.asym.value = 0;
1395       h->esym.asym.st = stGlobal;
1396
1397       if (h->root.root.type != bfd_link_hash_defined
1398          && h->root.root.type != bfd_link_hash_defweak)
1399        h->esym.asym.sc = scAbs;
1400       else
1401        {
1402          const char *name;
1403
1404          sec = h->root.root.u.def.section;
1405          output_section = sec->output_section;
1406
1407          /* When making a shared library and symbol h is the one from
1408             the another shared library, OUTPUT_SECTION may be null.  */
1409          if (output_section == NULL)
1410            h->esym.asym.sc = scUndefined;
1411          else
1412            {
1413              name = bfd_section_name (output_section->owner, output_section);
1414
1415              if (strcmp (name, ".text") == 0)
1416                h->esym.asym.sc = scText;
1417              else if (strcmp (name, ".data") == 0)
1418                h->esym.asym.sc = scData;
1419              else if (strcmp (name, ".sdata") == 0)
1420                h->esym.asym.sc = scSData;
1421              else if (strcmp (name, ".rodata") == 0
1422                       || strcmp (name, ".rdata") == 0)
1423                h->esym.asym.sc = scRData;
1424              else if (strcmp (name, ".bss") == 0)
1425                h->esym.asym.sc = scBss;
1426              else if (strcmp (name, ".sbss") == 0)
1427                h->esym.asym.sc = scSBss;
1428              else if (strcmp (name, ".init") == 0)
1429                h->esym.asym.sc = scInit;
1430              else if (strcmp (name, ".fini") == 0)
1431                h->esym.asym.sc = scFini;
1432              else
1433                h->esym.asym.sc = scAbs;
1434            }
1435        }
1436
1437       h->esym.asym.reserved = 0;
1438       h->esym.asym.index = indexNil;
1439     }
1440
1441   if (h->root.root.type == bfd_link_hash_common)
1442     h->esym.asym.value = h->root.root.u.c.size;
1443   else if (h->root.root.type == bfd_link_hash_defined
1444            || h->root.root.type == bfd_link_hash_defweak)
1445     {
1446       if (h->esym.asym.sc == scCommon)
1447        h->esym.asym.sc = scBss;
1448       else if (h->esym.asym.sc == scSCommon)
1449        h->esym.asym.sc = scSBss;
1450
1451       sec = h->root.root.u.def.section;
1452       output_section = sec->output_section;
1453       if (output_section != NULL)
1454        h->esym.asym.value = (h->root.root.u.def.value
1455                              + sec->output_offset
1456                              + output_section->vma);
1457       else
1458        h->esym.asym.value = 0;
1459     }
1460   else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1461     {
1462       /* Set type and value for a symbol with a function stub.  */
1463       h->esym.asym.st = stProc;
1464       sec = bfd_get_section_by_name (einfo->abfd, ".plt");
1465       if (sec == NULL)
1466         h->esym.asym.value = 0;
1467       else
1468         {
1469           output_section = sec->output_section;
1470           if (output_section != NULL)
1471             h->esym.asym.value = (h->root.plt_offset
1472                                   + sec->output_offset
1473                                   + output_section->vma);
1474           else
1475             h->esym.asym.value = 0;
1476         }
1477 #if 0 /* FIXME?  */
1478       h->esym.ifd = 0;
1479 #endif
1480     }
1481
1482   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1483                                      h->root.root.root.string,
1484                                      &h->esym))
1485     {
1486       einfo->failed = true;
1487       return false;
1488     }
1489
1490   return true;
1491 }
1492
1493 /* FIXME:  Create a runtime procedure table from the .mdebug section.
1494
1495 static boolean
1496 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
1497      PTR handle;
1498      bfd *abfd;
1499      struct bfd_link_info *info;
1500      asection *s;
1501      struct ecoff_debug_info *debug;
1502 */
1503 \f
1504 /* Handle dynamic relocations when doing an Alpha ELF link.  */
1505
1506 static boolean
1507 elf64_alpha_check_relocs (abfd, info, sec, relocs)
1508      bfd *abfd;
1509      struct bfd_link_info *info;
1510      asection *sec;
1511      const Elf_Internal_Rela *relocs;
1512 {
1513   bfd *dynobj;
1514   asection *sreloc;
1515   const char *rel_sec_name;
1516   Elf_Internal_Shdr *symtab_hdr;
1517   struct alpha_elf_link_hash_entry **sym_hashes;
1518   struct alpha_elf_got_entry **local_got_entries;
1519   const Elf_Internal_Rela *rel, *relend;
1520   int got_created;
1521
1522   if (info->relocateable)
1523     return true;
1524
1525   dynobj = elf_hash_table(info)->dynobj;
1526   if (dynobj == NULL)
1527     elf_hash_table(info)->dynobj = dynobj = abfd;
1528
1529   sreloc = NULL;
1530   rel_sec_name = NULL;
1531   symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
1532   sym_hashes = alpha_elf_sym_hashes(abfd);
1533   local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1534   got_created = 0;
1535
1536   relend = relocs + sec->reloc_count;
1537   for (rel = relocs; rel < relend; ++rel)
1538     {
1539       unsigned long r_symndx, r_type;
1540       struct alpha_elf_link_hash_entry *h;
1541
1542       r_symndx = ELF64_R_SYM (rel->r_info);
1543       if (r_symndx < symtab_hdr->sh_info)
1544         h = NULL;
1545       else
1546         {
1547           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1548           h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1549         }
1550       r_type = ELF64_R_TYPE (rel->r_info);
1551
1552       switch (r_type)
1553         {
1554         case R_ALPHA_LITERAL:
1555           {
1556             struct alpha_elf_got_entry *gotent;
1557             int flags = 0;
1558
1559             if (h)
1560               {
1561                 /* Search for and possibly create a got entry.  */
1562                 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
1563                   if (gotent->gotobj == abfd &&
1564                       gotent->addend == rel->r_addend)
1565                     break;
1566
1567                 if (!gotent)
1568                   {
1569                     gotent = ((struct alpha_elf_got_entry *)
1570                               bfd_alloc (abfd,
1571                                          sizeof (struct alpha_elf_got_entry)));
1572                     if (!gotent)
1573                       return false;
1574
1575                     gotent->gotobj = abfd;
1576                     gotent->addend = rel->r_addend;
1577                     gotent->got_offset = -1;
1578                     gotent->flags = 0;
1579
1580                     gotent->next = h->got_entries;
1581                     h->got_entries = gotent;
1582
1583                     alpha_elf_tdata (abfd)->total_got_entries++;
1584                   }
1585               }
1586             else
1587               {
1588                 /* This is a local .got entry -- record for merge.  */
1589                 if (!local_got_entries)
1590                   {
1591                     size_t size;
1592                     size = (symtab_hdr->sh_info
1593                             * sizeof (struct alpha_elf_got_entry *));
1594
1595                     local_got_entries = ((struct alpha_elf_got_entry **)
1596                                          bfd_alloc (abfd, size));
1597                     if (!local_got_entries)
1598                       return false;
1599
1600                     memset (local_got_entries, 0, size);
1601                     alpha_elf_tdata (abfd)->local_got_entries =
1602                       local_got_entries;
1603                   }
1604
1605                 for (gotent = local_got_entries[ELF64_R_SYM(rel->r_info)];
1606                      gotent != NULL && gotent->addend != rel->r_addend;
1607                      gotent = gotent->next)
1608                   continue;
1609                 if (!gotent)
1610                   {
1611                     gotent = ((struct alpha_elf_got_entry *)
1612                               bfd_alloc (abfd,
1613                                          sizeof (struct alpha_elf_got_entry)));
1614                     if (!gotent)
1615                       return false;
1616
1617                     gotent->gotobj = abfd;
1618                     gotent->addend = rel->r_addend;
1619                     gotent->got_offset = -1;
1620                     gotent->flags = 0;
1621
1622                     gotent->next = local_got_entries[ELF64_R_SYM(rel->r_info)];
1623                     local_got_entries[ELF64_R_SYM(rel->r_info)] = gotent;
1624
1625                     alpha_elf_tdata(abfd)->total_got_entries++;
1626                     alpha_elf_tdata(abfd)->n_local_got_entries++;
1627                   }
1628               }
1629
1630             /* Remember how this literal is used from its LITUSEs.
1631                This will be important when it comes to decide if we can
1632                create a .plt entry for a function symbol.  */
1633             if (rel+1 < relend
1634                 && ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
1635               {
1636                 do
1637                   {
1638                     ++rel;
1639                     if (rel->r_addend >= 1 && rel->r_addend <= 3)
1640                       flags |= 1 << rel->r_addend;
1641                   }
1642                 while (rel+1 < relend &&
1643                        ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE);
1644               }
1645             else
1646               {
1647                 /* No LITUSEs -- presumably the address is not being
1648                    loaded for nothing.  */
1649                 flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
1650               }
1651
1652             gotent->flags |= flags;
1653             if (h)
1654               {
1655                 /* Make a guess as to whether a .plt entry will be needed.  */
1656                 if ((h->flags |= flags) == ALPHA_ELF_LINK_HASH_LU_FUNC)
1657                   h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1658                 else
1659                   h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1660               }
1661           }
1662           /* FALLTHRU */
1663
1664         case R_ALPHA_GPDISP:
1665         case R_ALPHA_GPREL32:
1666           /* We don't actually use the .got here, but the sections must
1667              be created before the linker maps input sections to output
1668              sections.  */
1669           if (!got_created)
1670             {
1671               if (!elf64_alpha_create_got_section (abfd, info))
1672                 return false;
1673
1674               /* Make sure the object's gotobj is set to itself so
1675                  that we default to every object with its own .got.
1676                  We'll merge .gots later once we've collected each
1677                  object's info.  */
1678               alpha_elf_tdata(abfd)->gotobj = abfd;
1679
1680               got_created = 1;
1681             }
1682           break;
1683
1684         case R_ALPHA_SREL16:
1685         case R_ALPHA_SREL32:
1686         case R_ALPHA_SREL64:
1687           if (h == NULL)
1688             break;
1689           /* FALLTHRU */
1690
1691         case R_ALPHA_REFLONG:
1692         case R_ALPHA_REFQUAD:
1693           if (rel_sec_name == NULL)
1694             {
1695               rel_sec_name = (bfd_elf_string_from_elf_section
1696                               (abfd, elf_elfheader(abfd)->e_shstrndx,
1697                                elf_section_data(sec)->rel_hdr.sh_name));
1698               if (rel_sec_name == NULL)
1699                 return false;
1700
1701               BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
1702                           && strcmp (bfd_get_section_name (abfd, sec),
1703                                      rel_sec_name+5) == 0);
1704             }
1705
1706           /* We need to create the section here now whether we eventually
1707              use it or not so that it gets mapped to an output section by
1708              the linker.  If not used, we'll kill it in
1709              size_dynamic_sections.  */
1710           if (sreloc == NULL)
1711             {
1712               sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
1713               if (sreloc == NULL)
1714                 {
1715                   sreloc = bfd_make_section (dynobj, rel_sec_name);
1716                   if (sreloc == NULL
1717                       || !bfd_set_section_flags (dynobj, sreloc,
1718                                                  (SEC_ALLOC|SEC_LOAD
1719                                                   | SEC_HAS_CONTENTS
1720                                                   | SEC_IN_MEMORY
1721                                                   | SEC_LINKER_CREATED
1722                                                   | SEC_READONLY))
1723                       || !bfd_set_section_alignment (dynobj, sreloc, 3))
1724                     return false;
1725                 }
1726             }
1727
1728           if (h)
1729             {
1730               /* Since we havn't seen all of the input symbols yet, we
1731                  don't know whether we'll actually need a dynamic relocation
1732                  entry for this reloc.  So make a record of it.  Once we
1733                  find out if this thing needs dynamic relocation we'll
1734                  expand the relocation sections by the appropriate amount. */
1735
1736               struct alpha_elf_reloc_entry *rent;
1737
1738               for (rent = h->reloc_entries; rent; rent = rent->next)
1739                 if (rent->rtype == r_type && rent->srel == sreloc)
1740                   break;
1741
1742               if (!rent)
1743                 {
1744                   rent = ((struct alpha_elf_reloc_entry *)
1745                           bfd_alloc (abfd,
1746                                      sizeof (struct alpha_elf_reloc_entry)));
1747                   if (!rent)
1748                     return false;
1749
1750                   rent->srel = sreloc;
1751                   rent->rtype = r_type;
1752                   rent->count = 1;
1753
1754                   rent->next = h->reloc_entries;
1755                   h->reloc_entries = rent;
1756                 }
1757               else
1758                 rent->count++;
1759             }
1760           else if (info->shared)
1761             {
1762               /* If this is a shared library, we need a RELATIVE reloc.  */
1763               sreloc->_raw_size += sizeof (Elf64_External_Rela);
1764             }
1765           break;
1766         }
1767     }
1768
1769   return true;
1770 }
1771
1772 /* Adjust a symbol defined by a dynamic object and referenced by a
1773    regular object.  The current definition is in some section of the
1774    dynamic object, but we're not including those sections.  We have to
1775    change the definition to something the rest of the link can
1776    understand.  */
1777
1778 static boolean
1779 elf64_alpha_adjust_dynamic_symbol (info, h)
1780      struct bfd_link_info *info;
1781      struct elf_link_hash_entry *h;
1782 {
1783   bfd *dynobj;
1784   asection *s;
1785   struct alpha_elf_link_hash_entry *ah;
1786
1787   dynobj = elf_hash_table(info)->dynobj;
1788   ah = (struct alpha_elf_link_hash_entry *)h;
1789
1790   /* Now that we've seen all of the input symbols, finalize our decision
1791      about whether this symbol should get a .plt entry.  */
1792
1793   if (h->root.type != bfd_link_hash_undefweak
1794       && alpha_elf_dynamic_symbol_p (h, info)
1795       && ((h->type == STT_FUNC
1796            && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
1797           || (h->type == STT_NOTYPE
1798               && ah->flags == ALPHA_ELF_LINK_HASH_LU_FUNC))
1799       /* Don't prevent otherwise valid programs from linking by attempting
1800          to create a new .got entry somewhere.  A Correct Solution would be
1801          to add a new .got section to a new object file and let it be merged
1802          somewhere later.  But for now don't bother.  */
1803       && ah->got_entries)
1804     {
1805       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1806
1807       s = bfd_get_section_by_name(dynobj, ".plt");
1808       if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
1809         return false;
1810
1811       /* The first bit of the .plt is reserved.  */
1812       if (s->_raw_size == 0)
1813         s->_raw_size = PLT_HEADER_SIZE;
1814
1815       h->plt_offset = s->_raw_size;
1816       s->_raw_size += PLT_ENTRY_SIZE;
1817
1818       /* If this symbol is not defined in a regular file, and we are not
1819          generating a shared library, then set the symbol to the location
1820          in the .plt.  This is required to make function pointers compare
1821          equal between the normal executable and the shared library.  */
1822       if (!info->shared)
1823         {
1824           h->root.u.def.section = s;
1825           h->root.u.def.value = h->plt_offset;
1826         }
1827
1828       /* We also need a JMP_SLOT entry in the .rela.plt section.  */
1829       s = bfd_get_section_by_name (dynobj, ".rela.plt");
1830       BFD_ASSERT (s != NULL);
1831       s->_raw_size += sizeof (Elf64_External_Rela);
1832
1833       return true;
1834     }
1835   else
1836     h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1837
1838   /* If this is a weak symbol, and there is a real definition, the
1839      processor independent code will have arranged for us to see the
1840      real definition first, and we can just use the same value.  */
1841   if (h->weakdef != NULL)
1842     {
1843       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1844                   || h->weakdef->root.type == bfd_link_hash_defweak);
1845       h->root.u.def.section = h->weakdef->root.u.def.section;
1846       h->root.u.def.value = h->weakdef->root.u.def.value;
1847       return true;
1848     }
1849
1850   /* This is a reference to a symbol defined by a dynamic object which
1851      is not a function.  The Alpha, since it uses .got entries for all
1852      symbols even in regular objects, does not need the hackery of a
1853      .dynbss section and COPY dynamic relocations.  */
1854
1855   return true;
1856 }
1857
1858 /* Is it possible to merge two object file's .got tables?  */
1859
1860 static boolean
1861 elf64_alpha_can_merge_gots (a, b)
1862      bfd *a, *b;
1863 {
1864   int total = alpha_elf_tdata (a)->total_got_entries;
1865
1866   /* Trivial quick fallout test.  */
1867   if (total + alpha_elf_tdata (b)->total_got_entries <= MAX_GOT_ENTRIES)
1868     return true;
1869
1870   /* By their nature, local .got entries cannot be merged.  */
1871   if ((total += alpha_elf_tdata (b)->n_local_got_entries) > MAX_GOT_ENTRIES)
1872     return false;
1873
1874   /* Failing the common trivial comparison, we must effectively
1875      perform the merge.  Not actually performing the merge means that
1876      we don't have to store undo information in case we fail.  */
1877   {
1878     struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
1879     Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
1880     int i, n;
1881
1882     n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
1883     for (i = 0; i < n; ++i)
1884       {
1885         struct alpha_elf_got_entry *ae, *be;
1886         for (be = hashes[i]->got_entries; be ; be = be->next)
1887           {
1888             if (be->gotobj != b)
1889               continue;
1890
1891             for (ae = hashes[i]->got_entries; ae ; ae = ae->next)
1892               if (ae->gotobj == a && ae->addend == be->addend)
1893                 goto global_found;
1894
1895             if (++total > MAX_GOT_ENTRIES)
1896               return false;
1897           global_found:;
1898           }
1899       }
1900   }
1901
1902   return true;
1903 }
1904
1905 /* Actually merge two .got tables.  */
1906
1907 static void
1908 elf64_alpha_merge_gots (a, b)
1909      bfd *a, *b;
1910 {
1911   int total = alpha_elf_tdata(a)->total_got_entries;
1912
1913   /* Remember local expansion.  */
1914   {
1915     int e = alpha_elf_tdata(b)->n_local_got_entries;
1916     total += e;
1917     alpha_elf_tdata(a)->n_local_got_entries += e;
1918   }
1919
1920   /* Let the local .got entries know they are part of a new subsegment.  */
1921   {
1922     struct alpha_elf_got_entry **local_got_entries;
1923     local_got_entries = alpha_elf_tdata(b)->local_got_entries;
1924     if (local_got_entries)
1925       {
1926         int i, n;
1927
1928         n = elf_tdata(b)->symtab_hdr.sh_info;
1929         for (i = 0; i < n; ++i)
1930           {
1931             struct alpha_elf_got_entry *gotent;
1932             for (gotent = local_got_entries[i]; gotent; gotent = gotent->next)
1933               gotent->gotobj = a;
1934           }
1935       }
1936   }
1937
1938   /* Merge the global .got entries.  */
1939   {
1940     struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
1941     Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
1942     int i, n;
1943
1944     n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
1945     for (i = 0; i < n; ++i)
1946       {
1947         struct alpha_elf_got_entry *ae, *be, **pbe, **start;
1948         start = &hashes[i]->got_entries;
1949         for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
1950           {
1951             if (be->gotobj != b)
1952               continue;
1953
1954             for (ae = *start; ae ; ae = ae->next)
1955               if (ae->gotobj == a && ae->addend == be->addend)
1956                 {
1957                   ae->flags |= be->flags;
1958                   *pbe = be->next;
1959                   goto global_found;
1960                 }
1961             be->gotobj = a;
1962             total += 1;
1963
1964           global_found:;
1965           }
1966       }
1967   }
1968
1969   alpha_elf_tdata(a)->total_got_entries = total;
1970   alpha_elf_tdata(b)->gotobj = a;
1971 }
1972
1973 /* Calculate the offsets for the got entries.  */
1974
1975 static boolean
1976 elf64_alpha_calc_got_offsets_for_symbol (h, arg)
1977      struct alpha_elf_link_hash_entry *h;
1978      PTR arg;
1979 {
1980   struct alpha_elf_got_entry *gotent;
1981
1982   for (gotent = h->got_entries; gotent; gotent = gotent->next)
1983     {
1984       bfd_size_type *plge = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
1985       gotent->got_offset = *plge;
1986       *plge += 8;
1987     }
1988
1989   return true;
1990 }
1991
1992 static void
1993 elf64_alpha_calc_got_offsets (info)
1994      struct bfd_link_info *info;
1995 {
1996   bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
1997
1998   /* First, zero out the .got sizes, as we may be recalculating the
1999      .got after optimizing it.  */
2000   for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2001     alpha_elf_tdata(i)->got->_raw_size = 0;
2002
2003   /* Next, fill in the offsets for all the global entries.  */
2004   alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2005                                 elf64_alpha_calc_got_offsets_for_symbol,
2006                                 NULL);
2007
2008   /* Finally, fill in the offsets for the local entries.  */
2009   for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2010     {
2011       bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
2012       bfd *j;
2013
2014       for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2015         {
2016           struct alpha_elf_got_entry **local_got_entries, *gotent;
2017           int k, n;
2018
2019           local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2020           if (!local_got_entries)
2021             continue;
2022
2023           for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
2024             for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
2025               {
2026                 gotent->got_offset = got_offset;
2027                 got_offset += 8;
2028               }
2029         }
2030
2031       alpha_elf_tdata(i)->got->_raw_size = got_offset;
2032     }
2033 }
2034
2035 /* Remove a section from the output BFD.  */
2036
2037 static void
2038 elf64_alpha_strip_section_from_output (s)
2039      asection *s;
2040 {
2041   asection **spp;
2042
2043   for (spp = &s->output_section->owner->sections;
2044        *spp != s->output_section;
2045        spp = &(*spp)->next)
2046     continue;
2047   *spp = s->output_section->next;
2048   --s->output_section->owner->section_count;
2049 }
2050
2051 /* Constructs the gots.  */
2052
2053 static boolean
2054 elf64_alpha_always_size_sections (output_bfd, info)
2055      bfd *output_bfd;
2056      struct bfd_link_info *info;
2057 {
2058   bfd *i, *got_list, *cur_got_obj, **cur_got_tail;
2059   int ngots;
2060
2061   if (info->relocateable)
2062     return true;
2063
2064   ngots = 0;
2065   got_list = NULL;
2066   cur_got_obj = NULL;
2067   cur_got_tail = NULL;
2068   for (i = info->input_bfds; i ; i = i->link_next)
2069     {
2070       bfd *this_got = alpha_elf_tdata (i)->gotobj;
2071
2072       /* Don't play if there is no .got for this input file.  */
2073       if (this_got == NULL)
2074         continue;
2075
2076       if (alpha_elf_tdata (this_got)->total_got_entries > MAX_GOT_ENTRIES)
2077         {
2078           /* Yikes! A single object file has too many entries.  */
2079           (*_bfd_error_handler)
2080             ("%s: .got subsegment exceeds 64K (size %d)",
2081              bfd_get_filename(i),
2082              alpha_elf_tdata(this_got)->total_got_entries * 8);
2083           return false;
2084         }
2085
2086       if (cur_got_obj)
2087         {
2088           if (elf64_alpha_can_merge_gots (cur_got_obj, i))
2089             {
2090               elf64_alpha_merge_gots (cur_got_obj, i);
2091               *cur_got_tail = i;
2092             }
2093           else
2094             {
2095               if (++ngots == 2)
2096                 {
2097                   (*info->callbacks->warning)
2098                     (info, "using multiple gp values", (char *) NULL,
2099                      output_bfd, (asection *) NULL, (bfd_vma) 0);
2100                 }
2101               *cur_got_tail = NULL;
2102               alpha_elf_tdata(cur_got_obj)->got_link_next = got_list;
2103               got_list = cur_got_obj;
2104               cur_got_obj = i;
2105             }
2106         }
2107       else
2108         {
2109           ++ngots;
2110           cur_got_obj = i;
2111         }
2112       cur_got_tail = &alpha_elf_tdata(i)->in_got_link_next;
2113     }
2114
2115   if (cur_got_obj)
2116     alpha_elf_tdata (cur_got_obj)->got_link_next = got_list;
2117   alpha_elf_hash_table (info)->got_list = got_list = cur_got_obj;
2118
2119   /* Once the gots have been merged, fill in the got offsets for everything
2120      therein.  */
2121   elf64_alpha_calc_got_offsets (info);
2122
2123   /* Allocate space for all of the .got subsections.  */
2124   for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2125     {
2126       asection *s = alpha_elf_tdata(i)->got;
2127       if (s->_raw_size > 0)
2128         {
2129           s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
2130           if (s->contents == NULL)
2131             return false;
2132         }
2133     }
2134
2135   return true;
2136 }
2137
2138 /* Work out the sizes of the dynamic relocation entries.  */
2139
2140 static boolean
2141 elf64_alpha_calc_dynrel_sizes (h, info)
2142      struct alpha_elf_link_hash_entry *h;
2143      struct bfd_link_info *info;
2144 {
2145   /* If the symbol was defined as a common symbol in a regular object
2146      file, and there was no definition in any dynamic object, then the
2147      linker will have allocated space for the symbol in a common
2148      section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
2149      set.  This is done for dynamic symbols in
2150      elf_adjust_dynamic_symbol but this is not done for non-dynamic
2151      symbols, somehow.  */
2152   if (((h->root.elf_link_hash_flags
2153        & (ELF_LINK_HASH_DEF_REGULAR
2154           | ELF_LINK_HASH_REF_REGULAR
2155           | ELF_LINK_HASH_DEF_DYNAMIC))
2156        == ELF_LINK_HASH_REF_REGULAR)
2157       && (h->root.root.type == bfd_link_hash_defined
2158           || h->root.root.type == bfd_link_hash_defweak)
2159       && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
2160     {
2161       h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2162     }
2163
2164   /* If the symbol is dynamic, we'll need all the relocations in their
2165      natural form.  */
2166   if (alpha_elf_dynamic_symbol_p (&h->root, info))
2167     {
2168       struct alpha_elf_reloc_entry *relent;
2169
2170       for (relent = h->reloc_entries; relent; relent = relent->next)
2171         {
2172           relent->srel->_raw_size +=
2173             sizeof (Elf64_External_Rela) * relent->count;
2174         }
2175
2176       /* Only add a .rela.got entry if we're not using a .plt entry.  */
2177       if (h->root.plt_offset == MINUS_ONE)
2178         {
2179           bfd *dynobj = elf_hash_table(info)->dynobj;
2180           struct alpha_elf_got_entry *gotent;
2181           bfd_size_type count = 0;
2182           asection *srel;
2183
2184           for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2185             count++;
2186           if (count > 0)
2187             {
2188               srel = bfd_get_section_by_name (dynobj, ".rela.got");
2189               BFD_ASSERT (srel != NULL);
2190               srel->_raw_size += sizeof (Elf64_External_Rela) * count;
2191             }
2192         }
2193     }
2194   /* Otherwise, shared objects require RELATIVE relocs for all REFQUAD
2195      and REFLONG relocations.  */
2196   else if (info->shared)
2197     {
2198       struct alpha_elf_reloc_entry *relent;
2199
2200       for (relent = h->reloc_entries; relent; relent = relent->next)
2201         if (relent->rtype == R_ALPHA_REFLONG
2202             || relent->rtype == R_ALPHA_REFQUAD)
2203           {
2204             relent->srel->_raw_size +=
2205               sizeof(Elf64_External_Rela) * relent->count;
2206           }
2207     }
2208
2209   return true;
2210 }
2211
2212 /* Set the sizes of the dynamic sections.  */
2213
2214 static boolean
2215 elf64_alpha_size_dynamic_sections (output_bfd, info)
2216      bfd *output_bfd;
2217      struct bfd_link_info *info;
2218 {
2219   bfd *dynobj;
2220   asection *s;
2221   boolean reltext;
2222   boolean relplt;
2223
2224   dynobj = elf_hash_table(info)->dynobj;
2225   BFD_ASSERT(dynobj != NULL);
2226
2227   if (elf_hash_table (info)->dynamic_sections_created)
2228     {
2229       /* Set the contents of the .interp section to the interpreter.  */
2230       if (!info->shared)
2231         {
2232           s = bfd_get_section_by_name (dynobj, ".interp");
2233           BFD_ASSERT (s != NULL);
2234           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2235           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2236         }
2237
2238       /* Now that we've seen all of the input files, we can decide which
2239          symbols need dynamic relocation entries and which don't.  We've
2240          collected information in check_relocs that we can now apply to
2241          size the dynamic relocation sections.  */
2242       alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2243                                     elf64_alpha_calc_dynrel_sizes,
2244                                     info);
2245
2246       /* When building shared libraries, each local .got entry needs a
2247          RELATIVE reloc.  */
2248       if (info->shared)
2249         {
2250           bfd *i;
2251           asection *srel;
2252           bfd_size_type count;
2253
2254           srel = bfd_get_section_by_name (dynobj, ".rela.got");
2255           BFD_ASSERT (srel != NULL);
2256
2257           for (i = alpha_elf_hash_table(info)->got_list, count = 0;
2258                i != NULL;
2259                i = alpha_elf_tdata(i)->got_link_next)
2260             count += alpha_elf_tdata(i)->n_local_got_entries;
2261
2262           srel->_raw_size += count * sizeof(Elf64_External_Rela);
2263         }
2264     }
2265   /* else we're not dynamic and by definition we don't need such things.  */
2266
2267   /* The check_relocs and adjust_dynamic_symbol entry points have
2268      determined the sizes of the various dynamic sections.  Allocate
2269      memory for them.  */
2270   reltext = false;
2271   relplt = false;
2272   for (s = dynobj->sections; s != NULL; s = s->next)
2273     {
2274       const char *name;
2275       boolean strip;
2276
2277       if (!(s->flags & SEC_LINKER_CREATED))
2278         continue;
2279
2280       /* It's OK to base decisions on the section name, because none
2281          of the dynobj section names depend upon the input files.  */
2282       name = bfd_get_section_name (dynobj, s);
2283
2284       /* If we don't need this section, strip it from the output file.
2285          This is to handle .rela.bss and .rela.plt.  We must create it
2286          in create_dynamic_sections, because it must be created before
2287          the linker maps input sections to output sections.  The
2288          linker does that before adjust_dynamic_symbol is called, and
2289          it is that function which decides whether anything needs to
2290          go into these sections.  */
2291
2292       strip = false;
2293
2294       if (strncmp (name, ".rela", 5) == 0)
2295         {
2296           strip = (s->_raw_size == 0);
2297
2298           if (!strip)
2299             {
2300               const char *outname;
2301               asection *target;
2302
2303               /* If this relocation section applies to a read only
2304                  section, then we probably need a DT_TEXTREL entry.  */
2305               outname = bfd_get_section_name (output_bfd,
2306                                               s->output_section);
2307               target = bfd_get_section_by_name (output_bfd, outname + 5);
2308               if (target != NULL
2309                   && (target->flags & SEC_READONLY) != 0)
2310                 reltext = true;
2311
2312               if (strcmp(name, ".rela.plt") == 0)
2313                 relplt = true;
2314
2315               /* We use the reloc_count field as a counter if we need
2316                  to copy relocs into the output file.  */
2317               s->reloc_count = 0;
2318             }
2319         }
2320       else if (strcmp (name, ".plt") != 0)
2321         {
2322           /* It's not one of our dynamic sections, so don't allocate space.  */
2323           continue;
2324         }
2325
2326       if (strip)
2327         elf64_alpha_strip_section_from_output (s);
2328       else
2329         {
2330           /* Allocate memory for the section contents.  */
2331           s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
2332           if (s->contents == NULL && s->_raw_size != 0)
2333             return false;
2334         }
2335     }
2336
2337   /* If we are generating a shared library, we generate a section
2338      symbol for each output section.  These are local symbols, which
2339      means that they must come first in the dynamic symbol table.
2340      That means we must increment the dynamic symbol index of every
2341      other dynamic symbol.  */
2342   if (info->shared)
2343     {
2344       long c[2], i;
2345       asection *p;
2346
2347       c[0] = 0;
2348       c[1] = bfd_count_sections (output_bfd);
2349
2350       elf_hash_table (info)->dynsymcount += c[1];
2351       elf_link_hash_traverse (elf_hash_table(info),
2352                               elf64_alpha_adjust_dynindx,
2353                               (PTR) c);
2354
2355       for (i = 1, p = output_bfd->sections;
2356            p != NULL;
2357            p = p->next, i++)
2358         {
2359           elf_section_data (p)->dynindx = i;
2360           /* These symbols will have no names, so we don't need to
2361              fiddle with dynstr_index.  */
2362         }
2363     }
2364
2365   if (elf_hash_table (info)->dynamic_sections_created)
2366     {
2367       /* Add some entries to the .dynamic section.  We fill in the
2368          values later, in elf64_alpha_finish_dynamic_sections, but we
2369          must add the entries now so that we get the correct size for
2370          the .dynamic section.  The DT_DEBUG entry is filled in by the
2371          dynamic linker and used by the debugger.  */
2372       if (!info->shared)
2373         {
2374           if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
2375             return false;
2376         }
2377
2378       if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
2379         return false;
2380
2381       if (relplt)
2382         {
2383           if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2384               || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2385               || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
2386             return false;
2387         }
2388
2389       if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
2390           || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
2391           || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
2392                                             sizeof(Elf64_External_Rela)))
2393         return false;
2394
2395       if (reltext)
2396         {
2397           if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
2398             return false;
2399         }
2400     }
2401
2402   return true;
2403 }
2404
2405 /* Increment the index of a dynamic symbol by a given amount.  Called
2406    via elf_link_hash_traverse.  */
2407
2408 static boolean
2409 elf64_alpha_adjust_dynindx (h, cparg)
2410      struct elf_link_hash_entry *h;
2411      PTR cparg;
2412 {
2413   long *cp = (long *)cparg;
2414
2415   if (h->dynindx >= cp[0])
2416     h->dynindx += cp[1];
2417
2418   return true;
2419 }
2420
2421 /* Relocate an Alpha ELF section.  */
2422
2423 static boolean
2424 elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
2425                               contents, relocs, local_syms, local_sections)
2426      bfd *output_bfd;
2427      struct bfd_link_info *info;
2428      bfd *input_bfd;
2429      asection *input_section;
2430      bfd_byte *contents;
2431      Elf_Internal_Rela *relocs;
2432      Elf_Internal_Sym *local_syms;
2433      asection **local_sections;
2434 {
2435   Elf_Internal_Shdr *symtab_hdr;
2436   Elf_Internal_Rela *rel;
2437   Elf_Internal_Rela *relend;
2438   asection *sec, *sgot, *srel, *srelgot;
2439   bfd *dynobj, *gotobj;
2440   bfd_vma gp;
2441
2442   srelgot = srel = NULL;
2443   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2444   dynobj = elf_hash_table (info)->dynobj;
2445   if (dynobj)
2446     {
2447       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2448     }
2449
2450   /* Find the gp value for this input bfd.  */
2451   sgot = NULL;
2452   gp = 0;
2453   gotobj = alpha_elf_tdata (input_bfd)->gotobj;
2454   if (gotobj)
2455     {
2456       sgot = alpha_elf_tdata (gotobj)->got;
2457       gp = _bfd_get_gp_value (gotobj);
2458       if (gp == 0)
2459         {
2460           gp = (sgot->output_section->vma
2461                 + sgot->output_offset
2462                 + 0x8000);
2463           _bfd_set_gp_value (gotobj, gp);
2464         }
2465     }
2466
2467   rel = relocs;
2468   relend = relocs + input_section->reloc_count;
2469   for (; rel < relend; rel++)
2470     {
2471       int r_type;
2472       reloc_howto_type *howto;
2473       unsigned long r_symndx;
2474       struct alpha_elf_link_hash_entry *h;
2475       Elf_Internal_Sym *sym;
2476       bfd_vma relocation;
2477       bfd_vma addend;
2478       bfd_reloc_status_type r;
2479
2480       r_type = ELF64_R_TYPE(rel->r_info);
2481       if (r_type < 0 || r_type >= (int) R_ALPHA_max)
2482         {
2483           bfd_set_error (bfd_error_bad_value);
2484           return false;
2485         }
2486       howto = elf64_alpha_howto_table + r_type;
2487
2488       r_symndx = ELF64_R_SYM(rel->r_info);
2489
2490       if (info->relocateable)
2491         {
2492           /* This is a relocateable link.  We don't have to change
2493              anything, unless the reloc is against a section symbol,
2494              in which case we have to adjust according to where the
2495              section symbol winds up in the output section.  */
2496           if (r_symndx < symtab_hdr->sh_info)
2497             {
2498               sym = local_syms + r_symndx;
2499               if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
2500                 {
2501                   sec = local_sections[r_symndx];
2502                   rel->r_addend += sec->output_offset + sym->st_value;
2503                 }
2504             }
2505
2506           continue;
2507         }
2508
2509       /* This is a final link.  */
2510
2511       h = NULL;
2512       sym = NULL;
2513       sec = NULL;
2514
2515       if (r_symndx < symtab_hdr->sh_info)
2516         {
2517           sym = local_syms + r_symndx;
2518           sec = local_sections[r_symndx];
2519           relocation = (sec->output_section->vma
2520                         + sec->output_offset
2521                         + sym->st_value);
2522         }
2523       else
2524         {
2525           h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
2526
2527           while (h->root.root.type == bfd_link_hash_indirect
2528                  || h->root.root.type == bfd_link_hash_warning)
2529             h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2530
2531           if (h->root.root.type == bfd_link_hash_defined
2532               || h->root.root.type == bfd_link_hash_defweak)
2533             {
2534               sec = h->root.root.u.def.section;
2535
2536 #if rth_notdef
2537               if ((r_type == R_ALPHA_LITERAL
2538                    && elf_hash_table(info)->dynamic_sections_created
2539                    && (!info->shared
2540                        || !info->symbolic
2541                        || !(h->root.elf_link_hash_flags
2542                             & ELF_LINK_HASH_DEF_REGULAR)))
2543                   || (info->shared
2544                       && (!info->symbolic
2545                           || !(h->root.elf_link_hash_flags
2546                                & ELF_LINK_HASH_DEF_REGULAR))
2547                       && (input_section->flags & SEC_ALLOC)
2548                       && (r_type == R_ALPHA_REFLONG
2549                           || r_type == R_ALPHA_REFQUAD
2550                           || r_type == R_ALPHA_LITERAL)))
2551                 {
2552                   /* In these cases, we don't need the relocation value.
2553                      We check specially because in some obscure cases
2554                      sec->output_section will be NULL.  */
2555                   relocation = 0;
2556                 }
2557 #else
2558               /* FIXME: Are not these obscure cases simply bugs?  Let's
2559                  get something working and come back to this.  */
2560               if (sec->output_section == NULL)
2561                 relocation = 0;
2562 #endif /* rth_notdef */
2563               else
2564                 {
2565                   relocation = (h->root.root.u.def.value
2566                                 + sec->output_section->vma
2567                                 + sec->output_offset);
2568                 }
2569             }
2570           else if (h->root.root.type == bfd_link_hash_undefweak)
2571             relocation = 0;
2572           else if (info->shared && !info->symbolic)
2573             relocation = 0;
2574           else
2575             {
2576               if (!((*info->callbacks->undefined_symbol)
2577                     (info, h->root.root.root.string, input_bfd,
2578                      input_section, rel->r_offset)))
2579                 return false;
2580               relocation = 0;
2581             }
2582         }
2583       addend = rel->r_addend;
2584
2585       switch (r_type)
2586         {
2587         case R_ALPHA_GPDISP:
2588           {
2589             bfd_byte *p_ldah, *p_lda;
2590
2591             BFD_ASSERT(gp != 0);
2592
2593             relocation = (input_section->output_section->vma
2594                           + input_section->output_offset
2595                           + rel->r_offset);
2596
2597             p_ldah = contents + rel->r_offset - input_section->vma;
2598             p_lda = p_ldah + rel->r_addend;
2599
2600             r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
2601                                              p_ldah, p_lda);
2602           }
2603           break;
2604
2605         case R_ALPHA_OP_PUSH:
2606         case R_ALPHA_OP_STORE:
2607         case R_ALPHA_OP_PSUB:
2608         case R_ALPHA_OP_PRSHIFT:
2609           /* We hate these silly beasts.  */
2610           abort();
2611
2612         case R_ALPHA_LITERAL:
2613           {
2614             struct alpha_elf_got_entry *gotent;
2615
2616             BFD_ASSERT(sgot != NULL);
2617             BFD_ASSERT(gp != 0);
2618
2619             if (h != NULL)
2620               {
2621                 gotent = h->got_entries;
2622                 while (gotent->gotobj != gotobj || gotent->addend != addend)
2623                   gotent = gotent->next;
2624
2625                 /* Initialize the .got entry's value.  */
2626                 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
2627                   {
2628                     bfd_put_64 (output_bfd, relocation+addend,
2629                                 sgot->contents + gotent->got_offset);
2630
2631                     /* The dynamic relocations for the .got entries are
2632                        done in finish_dynamic_symbol.  */
2633
2634                     gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
2635                   }
2636               }
2637             else
2638               {
2639                 gotent = (alpha_elf_tdata(input_bfd)->
2640                           local_got_entries[r_symndx]);
2641                 while (gotent->addend != addend)
2642                   gotent = gotent->next;
2643
2644                 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
2645                   {
2646                     bfd_put_64 (output_bfd, relocation+addend,
2647                                 sgot->contents + gotent->got_offset);
2648
2649                     /* Local got entries need RELATIVE relocs in shared
2650                        libraries.  */
2651                     if (info->shared)
2652                       {
2653                         Elf_Internal_Rela outrel;
2654
2655                         BFD_ASSERT(srelgot != NULL);
2656
2657                         outrel.r_offset = (sgot->output_section->vma
2658                                            + sgot->output_offset
2659                                            + gotent->got_offset);
2660                         outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
2661                         outrel.r_addend = 0;
2662
2663                         bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2664                                                    ((Elf64_External_Rela *)
2665                                                     srelgot->contents)
2666                                                    + srelgot->reloc_count++);
2667                       }
2668
2669                     gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
2670                   }
2671               }
2672
2673             /* Figure the gprel relocation.  */
2674             addend = 0;
2675             relocation = (sgot->output_section->vma
2676                           + sgot->output_offset
2677                           + gotent->got_offset);
2678             relocation -= gp;
2679           }
2680           /* overflow handled by _bfd_final_link_relocate */
2681           goto default_reloc;
2682
2683         case R_ALPHA_GPREL32:
2684           BFD_ASSERT(gp != 0);
2685           relocation -= gp;
2686           goto default_reloc;
2687
2688         case R_ALPHA_BRADDR:
2689         case R_ALPHA_HINT:
2690           /* The regular PC-relative stuff measures from the start of
2691              the instruction rather than the end.  */
2692           addend -= 4;
2693           goto default_reloc;
2694
2695         case R_ALPHA_REFLONG:
2696         case R_ALPHA_REFQUAD:
2697           {
2698             Elf_Internal_Rela outrel;
2699             boolean skip;
2700
2701             /* Careful here to remember RELATIVE relocations for global
2702                variables for symbolic shared objects.  */
2703
2704             if (h && alpha_elf_dynamic_symbol_p (&h->root, info))
2705               {
2706                 BFD_ASSERT(h->root.dynindx != -1);
2707                 outrel.r_info = ELF64_R_INFO(h->root.dynindx, r_type);
2708                 outrel.r_addend = addend;
2709                 addend = 0, relocation = 0;
2710               }
2711             else if (info->shared)
2712               {
2713                 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
2714                 outrel.r_addend = 0;
2715               }
2716             else
2717               goto default_reloc;
2718
2719             if (!srel)
2720               {
2721                 const char *name;
2722
2723                 name = (bfd_elf_string_from_elf_section
2724                         (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
2725                          elf_section_data(input_section)->rel_hdr.sh_name));
2726                 BFD_ASSERT(name != NULL);
2727
2728                 srel = bfd_get_section_by_name (dynobj, name);
2729                 BFD_ASSERT(srel != NULL);
2730               }
2731
2732             skip = false;
2733
2734             if (elf_section_data (input_section)->stab_info == NULL)
2735               outrel.r_offset = rel->r_offset;
2736             else
2737               {
2738                 bfd_vma off;
2739
2740                 off = (_bfd_stab_section_offset
2741                        (output_bfd, &elf_hash_table (info)->stab_info,
2742                         input_section,
2743                         &elf_section_data (input_section)->stab_info,
2744                         rel->r_offset));
2745                 if (off == (bfd_vma) -1)
2746                   skip = true;
2747                 outrel.r_offset = off;
2748               }
2749
2750             if (! skip)
2751               outrel.r_offset += (input_section->output_section->vma
2752                                   + input_section->output_offset);
2753             else
2754               memset (&outrel, 0, sizeof outrel);
2755
2756             bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2757                                        ((Elf64_External_Rela *)
2758                                         srel->contents)
2759                                        + srel->reloc_count++);
2760           }
2761           goto default_reloc;
2762
2763         default:
2764         default_reloc:
2765           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2766                                         contents, rel->r_offset, relocation,
2767                                         addend);
2768           break;
2769         }
2770
2771       switch (r)
2772         {
2773         case bfd_reloc_ok:
2774           break;
2775
2776         case bfd_reloc_overflow:
2777           {
2778             const char *name;
2779
2780             if (h != NULL)
2781               name = h->root.root.root.string;
2782             else
2783               {
2784                 name = (bfd_elf_string_from_elf_section
2785                         (input_bfd, symtab_hdr->sh_link, sym->st_name));
2786                 if (name == NULL)
2787                   return false;
2788                 if (*name == '\0')
2789                   name = bfd_section_name (input_bfd, sec);
2790               }
2791             if (! ((*info->callbacks->reloc_overflow)
2792                    (info, name, howto->name, (bfd_vma) 0,
2793                     input_bfd, input_section, rel->r_offset)))
2794               return false;
2795           }
2796           break;
2797
2798         default:
2799         case bfd_reloc_outofrange:
2800           abort ();
2801         }
2802     }
2803
2804   return true;
2805 }
2806
2807 /* Finish up dynamic symbol handling.  We set the contents of various
2808    dynamic sections here.  */
2809
2810 static boolean
2811 elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
2812      bfd *output_bfd;
2813      struct bfd_link_info *info;
2814      struct elf_link_hash_entry *h;
2815      Elf_Internal_Sym *sym;
2816 {
2817   bfd *dynobj = elf_hash_table(info)->dynobj;
2818
2819   if (h->plt_offset != MINUS_ONE)
2820     {
2821       /* Fill in the .plt entry for this symbol.  */
2822       asection *splt, *sgot, *srel;
2823       Elf_Internal_Rela outrel;
2824       bfd_vma got_addr, plt_addr;
2825       bfd_vma plt_index;
2826       struct alpha_elf_got_entry *gotent;
2827
2828       BFD_ASSERT (h->dynindx != -1);
2829
2830       /* The first .got entry will be updated by the .plt with the
2831          address of the target function.  */
2832       gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
2833       BFD_ASSERT (gotent && gotent->addend == 0);
2834
2835       splt = bfd_get_section_by_name (dynobj, ".plt");
2836       BFD_ASSERT (splt != NULL);
2837       srel = bfd_get_section_by_name (dynobj, ".rela.plt");
2838       BFD_ASSERT (srel != NULL);
2839       sgot = alpha_elf_tdata (gotent->gotobj)->got;
2840       BFD_ASSERT (sgot != NULL);
2841
2842       got_addr = (sgot->output_section->vma
2843                   + sgot->output_offset
2844                   + gotent->got_offset);
2845       plt_addr = (splt->output_section->vma
2846                   + splt->output_offset
2847                   + h->plt_offset);
2848
2849       plt_index = (h->plt_offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2850
2851       /* Fill in the entry in the procedure linkage table.  */
2852       {
2853         unsigned insn1, insn2, insn3;
2854         long hi, lo;
2855
2856         /* decompose the reloc offset for the plt for ldah+lda */
2857         hi = plt_index * sizeof(Elf64_External_Rela);
2858         lo = ((hi & 0xffff) ^ 0x8000) - 0x8000;
2859         hi = (hi - lo) >> 16;
2860
2861         insn1 = PLT_ENTRY_WORD1 | (hi & 0xffff);
2862         insn2 = PLT_ENTRY_WORD2 | (lo & 0xffff);
2863         insn3 = PLT_ENTRY_WORD3 | ((-(h->plt_offset + 12) >> 2) & 0x1fffff);
2864
2865         bfd_put_32 (output_bfd, insn1, splt->contents + h->plt_offset);
2866         bfd_put_32 (output_bfd, insn2, splt->contents + h->plt_offset + 4);
2867         bfd_put_32 (output_bfd, insn3, splt->contents + h->plt_offset + 8);
2868       }
2869
2870       /* Fill in the entry in the .rela.plt section.  */
2871       outrel.r_offset = got_addr;
2872       outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
2873       outrel.r_addend = 0;
2874
2875       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2876                                  ((Elf64_External_Rela *)srel->contents
2877                                   + plt_index));
2878
2879       if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2880         {
2881           /* Mark the symbol as undefined, rather than as defined in the
2882              .plt section.  Leave the value alone.  */
2883           sym->st_shndx = SHN_UNDEF;
2884         }
2885
2886       /* Fill in the entries in the .got.  */
2887       bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
2888
2889       /* Subsequent .got entries will continue to bounce through the .plt.  */
2890       while ((gotent = gotent->next) != NULL)
2891         {
2892           sgot = alpha_elf_tdata(gotent->gotobj)->got;
2893           BFD_ASSERT(sgot != NULL);
2894           BFD_ASSERT(gotent->addend == 0);
2895
2896           bfd_put_64 (output_bfd, plt_addr,
2897                       sgot->contents + gotent->got_offset);
2898         }
2899     }
2900   else if (alpha_elf_dynamic_symbol_p (h, info))
2901     {
2902       /* Fill in the dynamic relocations for this symbol's .got entries.  */
2903       asection *srel;
2904       Elf_Internal_Rela outrel;
2905       struct alpha_elf_got_entry *gotent;
2906
2907       srel = bfd_get_section_by_name (dynobj, ".rela.got");
2908       BFD_ASSERT (srel != NULL);
2909
2910       outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_GLOB_DAT);
2911       for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
2912            gotent != NULL;
2913            gotent = gotent->next)
2914         {
2915           asection *sgot = alpha_elf_tdata (gotent->gotobj)->got;
2916           outrel.r_offset = (sgot->output_section->vma
2917                              + sgot->output_offset
2918                              + gotent->got_offset);
2919           outrel.r_addend = gotent->addend;
2920
2921           bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2922                                      ((Elf64_External_Rela *)srel->contents
2923                                       + srel->reloc_count++));
2924         }
2925     }
2926
2927   /* Mark some specially defined symbols as absolute.  */
2928   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2929       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2930       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2931     sym->st_shndx = SHN_ABS;
2932
2933   return true;
2934 }
2935
2936 /* Finish up the dynamic sections.  */
2937
2938 static boolean
2939 elf64_alpha_finish_dynamic_sections (output_bfd, info)
2940      bfd *output_bfd;
2941      struct bfd_link_info *info;
2942 {
2943   bfd *dynobj;
2944   asection *sdyn;
2945
2946   dynobj = elf_hash_table (info)->dynobj;
2947   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2948
2949   if (elf_hash_table (info)->dynamic_sections_created)
2950     {
2951       asection *splt;
2952       Elf64_External_Dyn *dyncon, *dynconend;
2953
2954       splt = bfd_get_section_by_name (dynobj, ".plt");
2955       BFD_ASSERT (splt != NULL && sdyn != NULL);
2956
2957       dyncon = (Elf64_External_Dyn *) sdyn->contents;
2958       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2959       for (; dyncon < dynconend; dyncon++)
2960         {
2961           Elf_Internal_Dyn dyn;
2962           const char *name;
2963           asection *s;
2964
2965           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2966
2967           switch (dyn.d_tag)
2968             {
2969             case DT_PLTGOT:
2970               name = ".plt";
2971               goto get_vma;
2972             case DT_PLTRELSZ:
2973               name = ".rela.plt";
2974               goto get_size;
2975             case DT_JMPREL:
2976               name = ".rela.plt";
2977               goto get_vma;
2978
2979             case DT_RELASZ:
2980               /* My interpretation of the TIS v1.1 ELF document indicates
2981                  that RELASZ should not include JMPREL.  This is not what
2982                  the rest of the BFD does.  It is, however, what the
2983                  glibc ld.so wants.  Do this fixup here until we found
2984                  out who is right.  */
2985               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2986               if (s)
2987                 {
2988                   dyn.d_un.d_val -=
2989                     (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2990                 }
2991               break;
2992
2993             get_vma:
2994               s = bfd_get_section_by_name (output_bfd, name);
2995               dyn.d_un.d_ptr = (s ? s->vma : 0);
2996               break;
2997
2998             get_size:
2999               s = bfd_get_section_by_name (output_bfd, name);
3000               dyn.d_un.d_val =
3001                 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3002               break;
3003             }
3004
3005           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3006         }
3007
3008       /* Initialize the PLT0 entry */
3009       if (splt->_raw_size > 0)
3010         {
3011           bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
3012           bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
3013           bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
3014           bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
3015
3016           /* The next two words will be filled in by ld.so */
3017           bfd_put_64 (output_bfd, 0, splt->contents + 16);
3018           bfd_put_64 (output_bfd, 0, splt->contents + 24);
3019
3020           elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3021             PLT_HEADER_SIZE;
3022         }
3023     }
3024
3025   if (info->shared)
3026     {
3027       asection *sdynsym;
3028       asection *s;
3029       Elf_Internal_Sym sym;
3030
3031       /* Set up the section symbols for the output sections.  */
3032
3033       sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
3034       BFD_ASSERT (sdynsym != NULL);
3035
3036       sym.st_size = 0;
3037       sym.st_name = 0;
3038       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3039       sym.st_other = 0;
3040
3041       for (s = output_bfd->sections; s != NULL; s = s->next)
3042         {
3043           int indx;
3044
3045           sym.st_value = s->vma;
3046
3047           indx = elf_section_data (s)->this_idx;
3048           BFD_ASSERT (indx > 0);
3049           sym.st_shndx = indx;
3050
3051           bfd_elf64_swap_symbol_out (output_bfd, &sym,
3052                                      (PTR) (((Elf64_External_Sym *)
3053                                              sdynsym->contents)
3054                                             + elf_section_data (s)->dynindx));
3055         }
3056
3057       /* Set the sh_info field of the output .dynsym section to the
3058          index of the first global symbol.  */
3059       elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
3060         bfd_count_sections (output_bfd) + 1;
3061     }
3062
3063   return true;
3064 }
3065
3066 /* We need to use a special link routine to handle the .reginfo and
3067    the .mdebug sections.  We need to merge all instances of these
3068    sections together, not write them all out sequentially.  */
3069
3070 static boolean
3071 elf64_alpha_final_link (abfd, info)
3072      bfd *abfd;
3073      struct bfd_link_info *info;
3074 {
3075   asection *o;
3076   struct bfd_link_order *p;
3077   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3078   struct ecoff_debug_info debug;
3079   const struct ecoff_debug_swap *swap
3080     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3081   HDRR *symhdr = &debug.symbolic_header;
3082   PTR mdebug_handle = NULL;
3083
3084   /* Go through the sections and collect the .reginfo and .mdebug
3085      information.  */
3086   reginfo_sec = NULL;
3087   mdebug_sec = NULL;
3088   gptab_data_sec = NULL;
3089   gptab_bss_sec = NULL;
3090   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3091     {
3092 #ifdef ERIC_neverdef
3093       if (strcmp (o->name, ".reginfo") == 0)
3094         {
3095           memset (&reginfo, 0, sizeof reginfo);
3096
3097           /* We have found the .reginfo section in the output file.
3098              Look through all the link_orders comprising it and merge
3099              the information together.  */
3100           for (p = o->link_order_head;
3101                p != (struct bfd_link_order *) NULL;
3102                p = p->next)
3103             {
3104               asection *input_section;
3105               bfd *input_bfd;
3106               Elf64_External_RegInfo ext;
3107               Elf64_RegInfo sub;
3108
3109               if (p->type != bfd_indirect_link_order)
3110                 {
3111                   if (p->type == bfd_fill_link_order)
3112                     continue;
3113                   abort ();
3114                 }
3115
3116               input_section = p->u.indirect.section;
3117               input_bfd = input_section->owner;
3118
3119               /* The linker emulation code has probably clobbered the
3120                  size to be zero bytes.  */
3121               if (input_section->_raw_size == 0)
3122                 input_section->_raw_size = sizeof (Elf64_External_RegInfo);
3123
3124               if (! bfd_get_section_contents (input_bfd, input_section,
3125                                               (PTR) &ext,
3126                                               (file_ptr) 0,
3127                                               sizeof ext))
3128                 return false;
3129
3130               bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
3131
3132               reginfo.ri_gprmask |= sub.ri_gprmask;
3133               reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3134               reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3135               reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3136               reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3137
3138               /* ri_gp_value is set by the function
3139                  alpha_elf_section_processing when the section is
3140                  finally written out.  */
3141
3142               /* Hack: reset the SEC_HAS_CONTENTS flag so that
3143                  elf_link_input_bfd ignores this section.  */
3144               input_section->flags &=~ SEC_HAS_CONTENTS;
3145             }
3146
3147           /* Force the section size to the value we want.  */
3148           o->_raw_size = sizeof (Elf64_External_RegInfo);
3149
3150           /* Skip this section later on (I don't think this currently
3151              matters, but someday it might).  */
3152           o->link_order_head = (struct bfd_link_order *) NULL;
3153
3154           reginfo_sec = o;
3155         }
3156 #endif
3157
3158       if (strcmp (o->name, ".mdebug") == 0)
3159         {
3160           struct extsym_info einfo;
3161
3162           /* We have found the .mdebug section in the output file.
3163              Look through all the link_orders comprising it and merge
3164              the information together.  */
3165           symhdr->magic = swap->sym_magic;
3166           /* FIXME: What should the version stamp be?  */
3167           symhdr->vstamp = 0;
3168           symhdr->ilineMax = 0;
3169           symhdr->cbLine = 0;
3170           symhdr->idnMax = 0;
3171           symhdr->ipdMax = 0;
3172           symhdr->isymMax = 0;
3173           symhdr->ioptMax = 0;
3174           symhdr->iauxMax = 0;
3175           symhdr->issMax = 0;
3176           symhdr->issExtMax = 0;
3177           symhdr->ifdMax = 0;
3178           symhdr->crfd = 0;
3179           symhdr->iextMax = 0;
3180
3181           /* We accumulate the debugging information itself in the
3182              debug_info structure.  */
3183           debug.line = NULL;
3184           debug.external_dnr = NULL;
3185           debug.external_pdr = NULL;
3186           debug.external_sym = NULL;
3187           debug.external_opt = NULL;
3188           debug.external_aux = NULL;
3189           debug.ss = NULL;
3190           debug.ssext = debug.ssext_end = NULL;
3191           debug.external_fdr = NULL;
3192           debug.external_rfd = NULL;
3193           debug.external_ext = debug.external_ext_end = NULL;
3194
3195           mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3196           if (mdebug_handle == (PTR) NULL)
3197             return false;
3198
3199           if (1)
3200             {
3201               asection *s;
3202               EXTR esym;
3203               bfd_vma last;
3204               unsigned int i;
3205               static const char * const name[] =
3206                 {
3207                   ".text", ".init", ".fini", ".data",
3208                   ".rodata", ".sdata", ".sbss", ".bss"
3209                 };
3210               static const int sc[] = { scText, scInit, scFini, scData,
3211                                           scRData, scSData, scSBss, scBss };
3212
3213               esym.jmptbl = 0;
3214               esym.cobol_main = 0;
3215               esym.weakext = 0;
3216               esym.reserved = 0;
3217               esym.ifd = ifdNil;
3218               esym.asym.iss = issNil;
3219               esym.asym.st = stLocal;
3220               esym.asym.reserved = 0;
3221               esym.asym.index = indexNil;
3222               for (i = 0; i < 8; i++)
3223                 {
3224                   esym.asym.sc = sc[i];
3225                   s = bfd_get_section_by_name (abfd, name[i]);
3226                   if (s != NULL)
3227                     {
3228                       esym.asym.value = s->vma;
3229                       last = s->vma + s->_raw_size;
3230                     }
3231                   else
3232                     esym.asym.value = last;
3233
3234                   if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3235                                                       name[i], &esym))
3236                     return false;
3237                 }
3238             }
3239
3240           for (p = o->link_order_head;
3241                p != (struct bfd_link_order *) NULL;
3242                p = p->next)
3243             {
3244               asection *input_section;
3245               bfd *input_bfd;
3246               const struct ecoff_debug_swap *input_swap;
3247               struct ecoff_debug_info input_debug;
3248               char *eraw_src;
3249               char *eraw_end;
3250
3251               if (p->type != bfd_indirect_link_order)
3252                 {
3253                   if (p->type == bfd_fill_link_order)
3254                     continue;
3255                   abort ();
3256                 }
3257
3258               input_section = p->u.indirect.section;
3259               input_bfd = input_section->owner;
3260
3261               if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
3262                   || (get_elf_backend_data (input_bfd)
3263                       ->elf_backend_ecoff_debug_swap) == NULL)
3264                 {
3265                   /* I don't know what a non ALPHA ELF bfd would be
3266                      doing with a .mdebug section, but I don't really
3267                      want to deal with it.  */
3268                   continue;
3269                 }
3270
3271               input_swap = (get_elf_backend_data (input_bfd)
3272                             ->elf_backend_ecoff_debug_swap);
3273
3274               BFD_ASSERT (p->size == input_section->_raw_size);
3275
3276               /* The ECOFF linking code expects that we have already
3277                  read in the debugging information and set up an
3278                  ecoff_debug_info structure, so we do that now.  */
3279               if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
3280                                                 &input_debug))
3281                 return false;
3282
3283               if (! (bfd_ecoff_debug_accumulate
3284                      (mdebug_handle, abfd, &debug, swap, input_bfd,
3285                       &input_debug, input_swap, info)))
3286                 return false;
3287
3288               /* Loop through the external symbols.  For each one with
3289                  interesting information, try to find the symbol in
3290                  the linker global hash table and save the information
3291                  for the output external symbols.  */
3292               eraw_src = input_debug.external_ext;
3293               eraw_end = (eraw_src
3294                           + (input_debug.symbolic_header.iextMax
3295                              * input_swap->external_ext_size));
3296               for (;
3297                    eraw_src < eraw_end;
3298                    eraw_src += input_swap->external_ext_size)
3299                 {
3300                   EXTR ext;
3301                   const char *name;
3302                   struct alpha_elf_link_hash_entry *h;
3303
3304                   (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
3305                   if (ext.asym.sc == scNil
3306                       || ext.asym.sc == scUndefined
3307                       || ext.asym.sc == scSUndefined)
3308                     continue;
3309
3310                   name = input_debug.ssext + ext.asym.iss;
3311                   h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
3312                                                   name, false, false, true);
3313                   if (h == NULL || h->esym.ifd != -2)
3314                     continue;
3315
3316                   if (ext.ifd != -1)
3317                     {
3318                       BFD_ASSERT (ext.ifd
3319                                   < input_debug.symbolic_header.ifdMax);
3320                       ext.ifd = input_debug.ifdmap[ext.ifd];
3321                     }
3322
3323                   h->esym = ext;
3324                 }
3325
3326               /* Free up the information we just read.  */
3327               free (input_debug.line);
3328               free (input_debug.external_dnr);
3329               free (input_debug.external_pdr);
3330               free (input_debug.external_sym);
3331               free (input_debug.external_opt);
3332               free (input_debug.external_aux);
3333               free (input_debug.ss);
3334               free (input_debug.ssext);
3335               free (input_debug.external_fdr);
3336               free (input_debug.external_rfd);
3337               free (input_debug.external_ext);
3338
3339               /* Hack: reset the SEC_HAS_CONTENTS flag so that
3340                  elf_link_input_bfd ignores this section.  */
3341               input_section->flags &=~ SEC_HAS_CONTENTS;
3342             }
3343
3344 #ifdef ERIC_neverdef
3345           if (info->shared)
3346             {
3347               /* Create .rtproc section.  */
3348               rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3349               if (rtproc_sec == NULL)
3350                 {
3351                   flagword flags = (SEC_HAS_CONTENTS
3352                                     | SEC_IN_MEMORY
3353                                     | SEC_LINKER_CREATED
3354                                     | SEC_READONLY);
3355
3356                   rtproc_sec = bfd_make_section (abfd, ".rtproc");
3357                   if (rtproc_sec == NULL
3358                       || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
3359                       || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
3360                     return false;
3361                 }
3362
3363               if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
3364                                                      info, rtproc_sec, &debug))
3365                 return false;
3366             }
3367 #endif
3368
3369
3370           /* Build the external symbol information.  */
3371           einfo.abfd = abfd;
3372           einfo.info = info;
3373           einfo.debug = &debug;
3374           einfo.swap = swap;
3375           einfo.failed = false;
3376           elf_link_hash_traverse (elf_hash_table (info),
3377                                   elf64_alpha_output_extsym,
3378                                   (PTR) &einfo);
3379           if (einfo.failed)
3380             return false;
3381
3382           /* Set the size of the .mdebug section.  */
3383           o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
3384
3385           /* Skip this section later on (I don't think this currently
3386              matters, but someday it might).  */
3387           o->link_order_head = (struct bfd_link_order *) NULL;
3388
3389           mdebug_sec = o;
3390         }
3391
3392 #ifdef ERIC_neverdef
3393       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
3394         {
3395           const char *subname;
3396           unsigned int c;
3397           Elf64_gptab *tab;
3398           Elf64_External_gptab *ext_tab;
3399           unsigned int i;
3400
3401           /* The .gptab.sdata and .gptab.sbss sections hold
3402              information describing how the small data area would
3403              change depending upon the -G switch.  These sections
3404              not used in executables files.  */
3405           if (! info->relocateable)
3406             {
3407               asection **secpp;
3408
3409               for (p = o->link_order_head;
3410                    p != (struct bfd_link_order *) NULL;
3411                    p = p->next)
3412                 {
3413                   asection *input_section;
3414
3415                   if (p->type != bfd_indirect_link_order)
3416                     {
3417                       if (p->type == bfd_fill_link_order)
3418                         continue;
3419                       abort ();
3420                     }
3421
3422                   input_section = p->u.indirect.section;
3423
3424                   /* Hack: reset the SEC_HAS_CONTENTS flag so that
3425                      elf_link_input_bfd ignores this section.  */
3426                   input_section->flags &=~ SEC_HAS_CONTENTS;
3427                 }
3428
3429               /* Skip this section later on (I don't think this
3430                  currently matters, but someday it might).  */
3431               o->link_order_head = (struct bfd_link_order *) NULL;
3432
3433               /* Really remove the section.  */
3434               for (secpp = &abfd->sections;
3435                    *secpp != o;
3436                    secpp = &(*secpp)->next)
3437                 ;
3438               *secpp = (*secpp)->next;
3439               --abfd->section_count;
3440
3441               continue;
3442             }
3443
3444           /* There is one gptab for initialized data, and one for
3445              uninitialized data.  */
3446           if (strcmp (o->name, ".gptab.sdata") == 0)
3447             gptab_data_sec = o;
3448           else if (strcmp (o->name, ".gptab.sbss") == 0)
3449             gptab_bss_sec = o;
3450           else
3451             {
3452               (*_bfd_error_handler)
3453                 ("%s: illegal section name `%s'",
3454                  bfd_get_filename (abfd), o->name);
3455               bfd_set_error (bfd_error_nonrepresentable_section);
3456               return false;
3457             }
3458
3459           /* The linker script always combines .gptab.data and
3460              .gptab.sdata into .gptab.sdata, and likewise for
3461              .gptab.bss and .gptab.sbss.  It is possible that there is
3462              no .sdata or .sbss section in the output file, in which
3463              case we must change the name of the output section.  */
3464           subname = o->name + sizeof ".gptab" - 1;
3465           if (bfd_get_section_by_name (abfd, subname) == NULL)
3466             {
3467               if (o == gptab_data_sec)
3468                 o->name = ".gptab.data";
3469               else
3470                 o->name = ".gptab.bss";
3471               subname = o->name + sizeof ".gptab" - 1;
3472               BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
3473             }
3474
3475           /* Set up the first entry.  */
3476           c = 1;
3477           tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
3478           if (tab == NULL)
3479             return false;
3480           tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
3481           tab[0].gt_header.gt_unused = 0;
3482
3483           /* Combine the input sections.  */
3484           for (p = o->link_order_head;
3485                p != (struct bfd_link_order *) NULL;
3486                p = p->next)
3487             {
3488               asection *input_section;
3489               bfd *input_bfd;
3490               bfd_size_type size;
3491               unsigned long last;
3492               bfd_size_type gpentry;
3493
3494               if (p->type != bfd_indirect_link_order)
3495                 {
3496                   if (p->type == bfd_fill_link_order)
3497                     continue;
3498                   abort ();
3499                 }
3500
3501               input_section = p->u.indirect.section;
3502               input_bfd = input_section->owner;
3503
3504               /* Combine the gptab entries for this input section one
3505                  by one.  We know that the input gptab entries are
3506                  sorted by ascending -G value.  */
3507               size = bfd_section_size (input_bfd, input_section);
3508               last = 0;
3509               for (gpentry = sizeof (Elf64_External_gptab);
3510                    gpentry < size;
3511                    gpentry += sizeof (Elf64_External_gptab))
3512                 {
3513                   Elf64_External_gptab ext_gptab;
3514                   Elf64_gptab int_gptab;
3515                   unsigned long val;
3516                   unsigned long add;
3517                   boolean exact;
3518                   unsigned int look;
3519
3520                   if (! (bfd_get_section_contents
3521                          (input_bfd, input_section, (PTR) &ext_gptab,
3522                           gpentry, sizeof (Elf64_External_gptab))))
3523                     {
3524                       free (tab);
3525                       return false;
3526                     }
3527
3528                   bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
3529                                                 &int_gptab);
3530                   val = int_gptab.gt_entry.gt_g_value;
3531                   add = int_gptab.gt_entry.gt_bytes - last;
3532
3533                   exact = false;
3534                   for (look = 1; look < c; look++)
3535                     {
3536                       if (tab[look].gt_entry.gt_g_value >= val)
3537                         tab[look].gt_entry.gt_bytes += add;
3538
3539                       if (tab[look].gt_entry.gt_g_value == val)
3540                         exact = true;
3541                     }
3542
3543                   if (! exact)
3544                     {
3545                       Elf64_gptab *new_tab;
3546                       unsigned int max;
3547
3548                       /* We need a new table entry.  */
3549                       new_tab = ((Elf64_gptab *)
3550                                  bfd_realloc ((PTR) tab,
3551                                               (c + 1) * sizeof (Elf64_gptab)));
3552                       if (new_tab == NULL)
3553                         {
3554                           free (tab);
3555                           return false;
3556                         }
3557                       tab = new_tab;
3558                       tab[c].gt_entry.gt_g_value = val;
3559                       tab[c].gt_entry.gt_bytes = add;
3560
3561                       /* Merge in the size for the next smallest -G
3562                          value, since that will be implied by this new
3563                          value.  */
3564                       max = 0;
3565                       for (look = 1; look < c; look++)
3566                         {
3567                           if (tab[look].gt_entry.gt_g_value < val
3568                               && (max == 0
3569                                   || (tab[look].gt_entry.gt_g_value
3570                                       > tab[max].gt_entry.gt_g_value)))
3571                             max = look;
3572                         }
3573                       if (max != 0)
3574                         tab[c].gt_entry.gt_bytes +=
3575                           tab[max].gt_entry.gt_bytes;
3576
3577                       ++c;
3578                     }
3579
3580                   last = int_gptab.gt_entry.gt_bytes;
3581                 }
3582
3583               /* Hack: reset the SEC_HAS_CONTENTS flag so that
3584                  elf_link_input_bfd ignores this section.  */
3585               input_section->flags &=~ SEC_HAS_CONTENTS;
3586             }
3587
3588           /* The table must be sorted by -G value.  */
3589           if (c > 2)
3590             qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
3591
3592           /* Swap out the table.  */
3593           ext_tab = ((Elf64_External_gptab *)
3594                      bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
3595           if (ext_tab == NULL)
3596             {
3597               free (tab);
3598               return false;
3599             }
3600
3601           for (i = 0; i < c; i++)
3602             bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
3603           free (tab);
3604
3605           o->_raw_size = c * sizeof (Elf64_External_gptab);
3606           o->contents = (bfd_byte *) ext_tab;
3607
3608           /* Skip this section later on (I don't think this currently
3609              matters, but someday it might).  */
3610           o->link_order_head = (struct bfd_link_order *) NULL;
3611         }
3612 #endif
3613
3614     }
3615
3616   /* Invoke the regular ELF backend linker to do all the work.  */
3617   if (! bfd_elf64_bfd_final_link (abfd, info))
3618     return false;
3619
3620   /* Now write out the computed sections.  */
3621
3622   /* The .got subsections...  */
3623   {
3624     bfd *i, *dynobj = elf_hash_table(info)->dynobj;
3625     for (i = alpha_elf_hash_table(info)->got_list;
3626          i != NULL;
3627          i = alpha_elf_tdata(i)->got_link_next)
3628       {
3629         asection *sgot;
3630
3631         /* elf_bfd_final_link already did everything in dynobj.  */
3632         if (i == dynobj)
3633           continue;
3634
3635         sgot = alpha_elf_tdata(i)->got;
3636         if (! bfd_set_section_contents (abfd, sgot->output_section,
3637                                         sgot->contents, sgot->output_offset,
3638                                         sgot->_raw_size))
3639           return false;
3640       }
3641   }
3642
3643 #ifdef ERIC_neverdef
3644   if (reginfo_sec != (asection *) NULL)
3645     {
3646       Elf64_External_RegInfo ext;
3647
3648       bfd_alpha_elf64_swap_reginfo_out (abfd, &reginfo, &ext);
3649       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
3650                                       (file_ptr) 0, sizeof ext))
3651         return false;
3652     }
3653 #endif
3654
3655   if (mdebug_sec != (asection *) NULL)
3656     {
3657       BFD_ASSERT (abfd->output_has_begun);
3658       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
3659                                                swap, info,
3660                                                mdebug_sec->filepos))
3661         return false;
3662
3663       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
3664     }
3665
3666   if (gptab_data_sec != (asection *) NULL)
3667     {
3668       if (! bfd_set_section_contents (abfd, gptab_data_sec,
3669                                       gptab_data_sec->contents,
3670                                       (file_ptr) 0,
3671                                       gptab_data_sec->_raw_size))
3672         return false;
3673     }
3674
3675   if (gptab_bss_sec != (asection *) NULL)
3676     {
3677       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
3678                                       gptab_bss_sec->contents,
3679                                       (file_ptr) 0,
3680                                       gptab_bss_sec->_raw_size))
3681         return false;
3682     }
3683
3684   return true;
3685 }
3686 \f
3687 /* ECOFF swapping routines.  These are used when dealing with the
3688    .mdebug section, which is in the ECOFF debugging format.  Copied
3689    from elf32-mips.c. */
3690 static const struct ecoff_debug_swap
3691 elf64_alpha_ecoff_debug_swap =
3692 {
3693   /* Symbol table magic number.  */
3694   magicSym2,
3695   /* Alignment of debugging information.  E.g., 4.  */
3696   8,
3697   /* Sizes of external symbolic information.  */
3698   sizeof (struct hdr_ext),
3699   sizeof (struct dnr_ext),
3700   sizeof (struct pdr_ext),
3701   sizeof (struct sym_ext),
3702   sizeof (struct opt_ext),
3703   sizeof (struct fdr_ext),
3704   sizeof (struct rfd_ext),
3705   sizeof (struct ext_ext),
3706   /* Functions to swap in external symbolic data.  */
3707   ecoff_swap_hdr_in,
3708   ecoff_swap_dnr_in,
3709   ecoff_swap_pdr_in,
3710   ecoff_swap_sym_in,
3711   ecoff_swap_opt_in,
3712   ecoff_swap_fdr_in,
3713   ecoff_swap_rfd_in,
3714   ecoff_swap_ext_in,
3715   _bfd_ecoff_swap_tir_in,
3716   _bfd_ecoff_swap_rndx_in,
3717   /* Functions to swap out external symbolic data.  */
3718   ecoff_swap_hdr_out,
3719   ecoff_swap_dnr_out,
3720   ecoff_swap_pdr_out,
3721   ecoff_swap_sym_out,
3722   ecoff_swap_opt_out,
3723   ecoff_swap_fdr_out,
3724   ecoff_swap_rfd_out,
3725   ecoff_swap_ext_out,
3726   _bfd_ecoff_swap_tir_out,
3727   _bfd_ecoff_swap_rndx_out,
3728   /* Function to read in symbolic data.  */
3729   elf64_alpha_read_ecoff_info
3730 };
3731 \f
3732 #define TARGET_LITTLE_SYM       bfd_elf64_alpha_vec
3733 #define TARGET_LITTLE_NAME      "elf64-alpha"
3734 #define ELF_ARCH                bfd_arch_alpha
3735 #define ELF_MACHINE_CODE        EM_ALPHA
3736 #define ELF_MAXPAGESIZE         0x100000
3737
3738 #define bfd_elf64_bfd_link_hash_table_create \
3739   elf64_alpha_bfd_link_hash_table_create
3740
3741 #define bfd_elf64_bfd_reloc_type_lookup \
3742   elf64_alpha_bfd_reloc_type_lookup
3743 #define elf_info_to_howto \
3744   elf64_alpha_info_to_howto
3745
3746 #define bfd_elf64_mkobject \
3747   elf64_alpha_mkobject
3748 #define elf_backend_object_p \
3749   elf64_alpha_object_p
3750
3751 #define elf_backend_section_from_shdr \
3752   elf64_alpha_section_from_shdr
3753 #define elf_backend_fake_sections \
3754   elf64_alpha_fake_sections
3755 #define elf_backend_additional_program_headers \
3756   elf64_alpha_additional_program_headers
3757
3758 #define bfd_elf64_bfd_is_local_label_name \
3759   elf64_alpha_is_local_label_name
3760 #define bfd_elf64_find_nearest_line \
3761   elf64_alpha_find_nearest_line
3762
3763 #define elf_backend_check_relocs \
3764   elf64_alpha_check_relocs
3765 #define elf_backend_create_dynamic_sections \
3766   elf64_alpha_create_dynamic_sections
3767 #define elf_backend_adjust_dynamic_symbol \
3768   elf64_alpha_adjust_dynamic_symbol
3769 #define elf_backend_always_size_sections \
3770   elf64_alpha_always_size_sections
3771 #define elf_backend_size_dynamic_sections \
3772   elf64_alpha_size_dynamic_sections
3773 #define elf_backend_relocate_section \
3774   elf64_alpha_relocate_section
3775 #define elf_backend_finish_dynamic_symbol \
3776   elf64_alpha_finish_dynamic_symbol
3777 #define elf_backend_finish_dynamic_sections \
3778   elf64_alpha_finish_dynamic_sections
3779 #define bfd_elf64_bfd_final_link \
3780   elf64_alpha_final_link
3781
3782 #define elf_backend_ecoff_debug_swap \
3783   &elf64_alpha_ecoff_debug_swap
3784
3785 /*
3786  * A few constants that determine how the .plt section is set up.
3787  */
3788 #define elf_backend_want_got_plt 0
3789 #define elf_backend_plt_readonly 0
3790 #define elf_backend_want_plt_sym 1
3791
3792 #include "elf64-target.h"