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