OSDN Git Service

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