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