OSDN Git Service

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