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