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