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