OSDN Git Service

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