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