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