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