Output line-ref to original igen source file when generating trace
[deliverable/binutils-gdb.git] / bfd / elf64-alpha.c
CommitLineData
ff12f303 1/* Alpha specific support for 64-bit ELF
303e7257 2 Copyright 1996, 1997 Free Software Foundation, Inc.
297a4f1a
ILT
3 Contributed by Richard Henderson <rth@tamu.edu>.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* We need a published ABI spec for this. Until one comes out, don't
22 assume this'll remain unchanged forever. */
23
24#include "bfd.h"
25#include "sysdep.h"
26#include "libbfd.h"
27#include "elf-bfd.h"
28
29#include "elf/alpha.h"
30
31#define ALPHAECOFF
32
33#define NO_COFF_RELOCS
34#define NO_COFF_SYMBOLS
35#define NO_COFF_LINENOS
36
37/* Get the ECOFF swapping routines. Needed for the debug information. */
38#include "coff/internal.h"
39#include "coff/sym.h"
40#include "coff/symconst.h"
41#include "coff/ecoff.h"
42#include "coff/alpha.h"
43#include "aout/ar.h"
44#include "libcoff.h"
45#include "libecoff.h"
46#define ECOFF_64
47#include "ecoffswap.h"
48
303e7257 49static boolean elf64_alpha_mkobject PARAMS ((bfd *));
297a4f1a
ILT
50static struct bfd_hash_entry * elf64_alpha_link_hash_newfunc
51 PARAMS((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
52static struct bfd_link_hash_table * elf64_alpha_bfd_link_hash_table_create
53 PARAMS((bfd *));
54
55static bfd_reloc_status_type elf64_alpha_reloc_nil
56 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
ff12f303
ILT
57static bfd_reloc_status_type elf64_alpha_reloc_bad
58 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
297a4f1a
ILT
59static bfd_reloc_status_type elf64_alpha_do_reloc_gpdisp
60 PARAMS((bfd *, bfd_vma, bfd_byte *, bfd_byte *));
61static bfd_reloc_status_type elf64_alpha_reloc_gpdisp
62 PARAMS((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
297a4f1a
ILT
63
64static reloc_howto_type * elf64_alpha_bfd_reloc_type_lookup
65 PARAMS((bfd *, bfd_reloc_code_real_type));
66static void elf64_alpha_info_to_howto
67 PARAMS((bfd *, arelent *, Elf64_Internal_Rela *));
68
69static boolean elf64_alpha_object_p
70 PARAMS((bfd *));
71static boolean elf64_alpha_section_from_shdr
72 PARAMS((bfd *, Elf64_Internal_Shdr *, char *));
73static boolean elf64_alpha_fake_sections
74 PARAMS((bfd *, Elf64_Internal_Shdr *, asection *));
75static int elf64_alpha_additional_program_headers
76 PARAMS((bfd *));
77static boolean elf64_alpha_create_got_section
78 PARAMS((bfd *, struct bfd_link_info *));
79static boolean elf64_alpha_create_dynamic_sections
80 PARAMS((bfd *, struct bfd_link_info *));
81
82static boolean elf64_alpha_read_ecoff_info
83 PARAMS((bfd *, asection *, struct ecoff_debug_info *));
303e7257
ILT
84static boolean elf64_alpha_is_local_label_name
85 PARAMS((bfd *, const char *));
297a4f1a
ILT
86static boolean elf64_alpha_find_nearest_line
87 PARAMS((bfd *, asection *, asymbol **, bfd_vma, const char **,
88 const char **, unsigned int *));
89
37d7888d 90#if defined(__STDC__) || defined(ALMOST_STDC)
297a4f1a 91struct alpha_elf_link_hash_entry;
37d7888d
ILT
92#endif
93
297a4f1a
ILT
94static boolean elf64_alpha_output_extsym
95 PARAMS((struct alpha_elf_link_hash_entry *, PTR));
96
ff12f303
ILT
97static boolean elf64_alpha_can_merge_gots
98 PARAMS((bfd *, bfd *));
99static void elf64_alpha_merge_gots
100 PARAMS((bfd *, bfd *));
303e7257
ILT
101static boolean elf64_alpha_calc_got_offsets_for_symbol
102 PARAMS ((struct alpha_elf_link_hash_entry *, PTR));
103static void elf64_alpha_calc_got_offsets PARAMS ((struct bfd_link_info *));
104static void elf64_alpha_strip_section_from_output PARAMS ((asection *));
105static boolean elf64_alpha_always_size_sections
106 PARAMS ((bfd *, struct bfd_link_info *));
107static boolean elf64_alpha_calc_dynrel_sizes
108 PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
297a4f1a
ILT
109static boolean elf64_alpha_check_relocs
110 PARAMS((bfd *, struct bfd_link_info *, asection *sec,
111 const Elf_Internal_Rela *));
112static boolean elf64_alpha_adjust_dynamic_symbol
113 PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
114static boolean elf64_alpha_size_dynamic_sections
115 PARAMS((bfd *, struct bfd_link_info *));
116static boolean elf64_alpha_adjust_dynindx
117 PARAMS((struct elf_link_hash_entry *, PTR));
118static boolean elf64_alpha_relocate_section
119 PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
120 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
121static boolean elf64_alpha_finish_dynamic_symbol
122 PARAMS((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
123 Elf_Internal_Sym *));
124static boolean elf64_alpha_finish_dynamic_sections
125 PARAMS((bfd *, struct bfd_link_info *));
126static boolean elf64_alpha_final_link
127 PARAMS((bfd *, struct bfd_link_info *));
128
129\f
297a4f1a
ILT
130struct alpha_elf_link_hash_entry
131{
132 struct elf_link_hash_entry root;
133
134 /* External symbol information. */
135 EXTR esym;
37d7888d 136
ff12f303
ILT
137 /* Cumulative flags for all the .got entries. */
138 int flags;
139
37d7888d 140 /* Contexts (LITUSE) in which a literal was referenced. */
ff12f303
ILT
141#define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
142#define ALPHA_ELF_LINK_HASH_LU_MEM 0x02
143#define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
144#define ALPHA_ELF_LINK_HASH_LU_FUNC 0x08
145
146 /* Used to implement multiple .got subsections. */
147 struct alpha_elf_got_entry
148 {
149 struct alpha_elf_got_entry *next;
150
151 /* which .got subsection? */
152 bfd *gotobj;
153
154 /* the addend in effect for this entry. */
155 bfd_vma addend;
156
157 /* the .got offset for this entry. */
158 int got_offset;
159
160 int flags;
161
162 /* An additional flag. */
163#define ALPHA_ELF_GOT_ENTRY_RELOCS_DONE 0x10
164 } *got_entries;
165
166 /* used to count non-got, non-plt relocations for delayed sizing
167 of relocation sections. */
168 struct alpha_elf_reloc_entry
169 {
170 struct alpha_elf_reloc_entry *next;
171
172 /* which .reloc section? */
173 asection *srel;
174
175 /* what kind of relocation? */
176 unsigned long rtype;
177
178 /* how many did we find? */
179 unsigned long count;
180 } *reloc_entries;
297a4f1a
ILT
181};
182
183/* Alpha ELF linker hash table. */
184
185struct alpha_elf_link_hash_table
186{
187 struct elf_link_hash_table root;
ff12f303
ILT
188
189 /* The head of a list of .got subsections linked through
190 alpha_elf_tdata(abfd)->got_link_next. */
191 bfd *got_list;
297a4f1a
ILT
192};
193
194/* Look up an entry in a Alpha ELF linker hash table. */
195
196#define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
197 ((struct alpha_elf_link_hash_entry *) \
198 elf_link_hash_lookup (&(table)->root, (string), (create), \
199 (copy), (follow)))
200
201/* Traverse a Alpha ELF linker hash table. */
202
203#define alpha_elf_link_hash_traverse(table, func, info) \
204 (elf_link_hash_traverse \
205 (&(table)->root, \
206 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
207 (info)))
208
209/* Get the Alpha ELF linker hash table from a link_info structure. */
210
211#define alpha_elf_hash_table(p) \
212 ((struct alpha_elf_link_hash_table *) ((p)->hash))
213
ff12f303
ILT
214/* Get the object's symbols as our own entry type. */
215
216#define alpha_elf_sym_hashes(abfd) \
217 ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
218
219/* Should we do dynamic things to this symbol? */
220
221#define alpha_elf_dynamic_symbol_p(h, info) \
303e7257 222 (((info)->shared && !(info)->symbolic && (h)->dynindx != -1) \
ff12f303
ILT
223 || (((h)->elf_link_hash_flags \
224 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)) \
225 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
226
297a4f1a
ILT
227/* Create an entry in a Alpha ELF linker hash table. */
228
229static struct bfd_hash_entry *
230elf64_alpha_link_hash_newfunc (entry, table, string)
231 struct bfd_hash_entry *entry;
232 struct bfd_hash_table *table;
233 const char *string;
234{
235 struct alpha_elf_link_hash_entry *ret =
236 (struct alpha_elf_link_hash_entry *) entry;
237
238 /* Allocate the structure if it has not already been allocated by a
239 subclass. */
240 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
241 ret = ((struct alpha_elf_link_hash_entry *)
242 bfd_hash_allocate (table,
243 sizeof (struct alpha_elf_link_hash_entry)));
244 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
245 return (struct bfd_hash_entry *) ret;
246
247 /* Call the allocation method of the superclass. */
248 ret = ((struct alpha_elf_link_hash_entry *)
249 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
250 table, string));
251 if (ret != (struct alpha_elf_link_hash_entry *) NULL)
252 {
253 /* Set local fields. */
254 memset (&ret->esym, 0, sizeof (EXTR));
255 /* We use -2 as a marker to indicate that the information has
256 not been set. -1 means there is no associated ifd. */
257 ret->esym.ifd = -2;
37d7888d 258 ret->flags = 0;
ff12f303
ILT
259 ret->got_entries = NULL;
260 ret->reloc_entries = NULL;
297a4f1a
ILT
261 }
262
263 return (struct bfd_hash_entry *) ret;
264}
265
266/* Create a Alpha ELF linker hash table. */
267
268static struct bfd_link_hash_table *
269elf64_alpha_bfd_link_hash_table_create (abfd)
270 bfd *abfd;
271{
272 struct alpha_elf_link_hash_table *ret;
273
274 ret = ((struct alpha_elf_link_hash_table *)
275 bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
276 if (ret == (struct alpha_elf_link_hash_table *) NULL)
277 return NULL;
278
279 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
280 elf64_alpha_link_hash_newfunc))
281 {
282 bfd_release (abfd, ret);
283 return NULL;
284 }
285
286 return &ret->root.root;
287}
288\f
ff12f303
ILT
289/* We have some private fields hanging off of the elf_tdata structure. */
290
291struct alpha_elf_obj_tdata
292{
293 struct elf_obj_tdata root;
294
295 /* For every input file, these are the got entries for that object's
296 local symbols. */
297 struct alpha_elf_got_entry ** local_got_entries;
297a4f1a 298
ff12f303
ILT
299 /* For every input file, this is the object that owns the got that
300 this input file uses. */
301 bfd *gotobj;
302
303 /* For every got, this is a linked list through the objects using this got */
304 bfd *in_got_link_next;
305
306 /* For every got, this is a link to the next got subsegment. */
307 bfd *got_link_next;
308
309 /* For every got, this is the section. */
310 asection *got;
311
312 /* For every got, this is it's total number of *entries*. */
313 int total_got_entries;
314
315 /* For every got, this is the sum of the number of *entries* required
316 to hold all of the member object's local got. */
317 int n_local_got_entries;
318};
319
320#define alpha_elf_tdata(abfd) \
321 ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
322
323static boolean
324elf64_alpha_mkobject (abfd)
325 bfd *abfd;
326{
327 abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
328 if (abfd->tdata.any == NULL)
329 return false;
330 return true;
331}
332\f
297a4f1a
ILT
333/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
334 from smaller values. Start with zero, widen, *then* decrement. */
335#define MINUS_ONE (((bfd_vma)0) - 1)
336
337static reloc_howto_type elf64_alpha_howto_table[] =
338{
339 HOWTO (R_ALPHA_NONE, /* type */
340 0, /* rightshift */
341 0, /* size (0 = byte, 1 = short, 2 = long) */
342 8, /* bitsize */
343 true, /* pc_relative */
344 0, /* bitpos */
345 complain_overflow_dont, /* complain_on_overflow */
346 elf64_alpha_reloc_nil, /* special_function */
347 "NONE", /* name */
348 false, /* partial_inplace */
349 0, /* src_mask */
350 0, /* dst_mask */
351 true), /* pcrel_offset */
352
353 /* A 32 bit reference to a symbol. */
354 HOWTO (R_ALPHA_REFLONG, /* type */
355 0, /* rightshift */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
357 32, /* bitsize */
358 false, /* pc_relative */
359 0, /* bitpos */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 0, /* special_function */
362 "REFLONG", /* name */
363 false, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
366 false), /* pcrel_offset */
367
368 /* A 64 bit reference to a symbol. */
369 HOWTO (R_ALPHA_REFQUAD, /* type */
370 0, /* rightshift */
371 4, /* size (0 = byte, 1 = short, 2 = long) */
372 64, /* bitsize */
373 false, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_bitfield, /* complain_on_overflow */
376 0, /* special_function */
377 "REFQUAD", /* name */
378 false, /* partial_inplace */
379 MINUS_ONE, /* src_mask */
380 MINUS_ONE, /* dst_mask */
381 false), /* pcrel_offset */
382
383 /* A 32 bit GP relative offset. This is just like REFLONG except
384 that when the value is used the value of the gp register will be
385 added in. */
386 HOWTO (R_ALPHA_GPREL32, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 32, /* bitsize */
390 false, /* pc_relative */
391 0, /* bitpos */
392 complain_overflow_bitfield, /* complain_on_overflow */
393 0, /* special_function */
394 "GPREL32", /* name */
395 false, /* partial_inplace */
396 0xffffffff, /* src_mask */
397 0xffffffff, /* dst_mask */
398 false), /* pcrel_offset */
399
400 /* Used for an instruction that refers to memory off the GP register. */
401 HOWTO (R_ALPHA_LITERAL, /* type */
402 0, /* rightshift */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* bitsize */
405 false, /* pc_relative */
406 0, /* bitpos */
407 complain_overflow_signed, /* complain_on_overflow */
408 0, /* special_function */
ff12f303 409 "ELF_LITERAL", /* name */
297a4f1a
ILT
410 false, /* partial_inplace */
411 0xffff, /* src_mask */
412 0xffff, /* dst_mask */
413 false), /* pcrel_offset */
414
ff12f303 415 /* This reloc only appears immediately following an ELF_LITERAL reloc.
297a4f1a
ILT
416 It identifies a use of the literal. The symbol index is special:
417 1 means the literal address is in the base register of a memory
418 format instruction; 2 means the literal address is in the byte
419 offset register of a byte-manipulation instruction; 3 means the
420 literal address is in the target register of a jsr instruction.
421 This does not actually do any relocation. */
422 HOWTO (R_ALPHA_LITUSE, /* type */
423 0, /* rightshift */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
425 32, /* bitsize */
426 false, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_dont, /* complain_on_overflow */
429 elf64_alpha_reloc_nil, /* special_function */
430 "LITUSE", /* name */
431 false, /* partial_inplace */
432 0, /* src_mask */
433 0, /* dst_mask */
434 false), /* pcrel_offset */
435
436 /* Load the gp register. This is always used for a ldah instruction
437 which loads the upper 16 bits of the gp register. The symbol
438 index of the GPDISP instruction is an offset in bytes to the lda
439 instruction that loads the lower 16 bits. The value to use for
440 the relocation is the difference between the GP value and the
441 current location; the load will always be done against a register
442 holding the current address.
443
ff12f303 444 NOTE: Unlike ECOFF, partial in-place relocation is not done. If
297a4f1a
ILT
445 any offset is present in the instructions, it is an offset from
446 the register to the ldah instruction. This lets us avoid any
447 stupid hackery like inventing a gp value to do partial relocation
448 against. Also unlike ECOFF, we do the whole relocation off of
449 the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
450 space consuming bit, that, since all the information was present
451 in the GPDISP_HI16 reloc. */
452 HOWTO (R_ALPHA_GPDISP, /* type */
453 16, /* rightshift */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
455 16, /* bitsize */
456 false, /* pc_relative */
457 0, /* bitpos */
458 complain_overflow_dont, /* complain_on_overflow */
459 elf64_alpha_reloc_gpdisp, /* special_function */
460 "GPDISP", /* name */
461 false, /* partial_inplace */
462 0xffff, /* src_mask */
463 0xffff, /* dst_mask */
464 true), /* pcrel_offset */
465
466 /* A 21 bit branch. */
467 HOWTO (R_ALPHA_BRADDR, /* type */
468 2, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 21, /* bitsize */
471 true, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_signed, /* complain_on_overflow */
474 0, /* special_function */
475 "BRADDR", /* name */
476 false, /* partial_inplace */
477 0x1fffff, /* src_mask */
478 0x1fffff, /* dst_mask */
479 true), /* pcrel_offset */
480
481 /* A hint for a jump to a register. */
482 HOWTO (R_ALPHA_HINT, /* type */
483 2, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 14, /* bitsize */
486 true, /* pc_relative */
487 0, /* bitpos */
488 complain_overflow_dont, /* complain_on_overflow */
489 0, /* special_function */
490 "HINT", /* name */
491 false, /* partial_inplace */
492 0x3fff, /* src_mask */
493 0x3fff, /* dst_mask */
494 true), /* pcrel_offset */
495
496 /* 16 bit PC relative offset. */
497 HOWTO (R_ALPHA_SREL16, /* type */
498 0, /* rightshift */
499 1, /* size (0 = byte, 1 = short, 2 = long) */
500 16, /* bitsize */
501 true, /* pc_relative */
502 0, /* bitpos */
503 complain_overflow_signed, /* complain_on_overflow */
504 0, /* special_function */
505 "SREL16", /* name */
506 false, /* partial_inplace */
507 0xffff, /* src_mask */
508 0xffff, /* dst_mask */
509 false), /* pcrel_offset */
510
511 /* 32 bit PC relative offset. */
512 HOWTO (R_ALPHA_SREL32, /* type */
513 0, /* rightshift */
514 2, /* size (0 = byte, 1 = short, 2 = long) */
515 32, /* bitsize */
516 true, /* pc_relative */
517 0, /* bitpos */
518 complain_overflow_signed, /* complain_on_overflow */
519 0, /* special_function */
520 "SREL32", /* name */
521 false, /* partial_inplace */
522 0xffffffff, /* src_mask */
523 0xffffffff, /* dst_mask */
524 false), /* pcrel_offset */
525
526 /* A 64 bit PC relative offset. */
527 HOWTO (R_ALPHA_SREL64, /* type */
528 0, /* rightshift */
529 4, /* size (0 = byte, 1 = short, 2 = long) */
530 64, /* bitsize */
531 true, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_signed, /* complain_on_overflow */
534 0, /* special_function */
535 "SREL64", /* name */
536 false, /* partial_inplace */
537 MINUS_ONE, /* src_mask */
538 MINUS_ONE, /* dst_mask */
539 false), /* pcrel_offset */
540
541 /* Push a value on the reloc evaluation stack. */
542 HOWTO (ALPHA_R_OP_PUSH, /* type */
543 0, /* rightshift */
544 0, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
546 false, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
ff12f303 549 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
550 "OP_PUSH", /* name */
551 false, /* partial_inplace */
552 0, /* src_mask */
553 0, /* dst_mask */
554 false), /* pcrel_offset */
555
556 /* Store the value from the stack at the given address. Store it in
557 a bitfield of size r_size starting at bit position r_offset. */
558 HOWTO (ALPHA_R_OP_STORE, /* type */
559 0, /* rightshift */
560 4, /* size (0 = byte, 1 = short, 2 = long) */
561 64, /* bitsize */
562 false, /* pc_relative */
563 0, /* bitpos */
564 complain_overflow_dont, /* complain_on_overflow */
ff12f303 565 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
566 "OP_STORE", /* name */
567 false, /* partial_inplace */
568 0, /* src_mask */
569 MINUS_ONE, /* dst_mask */
570 false), /* pcrel_offset */
571
572 /* Subtract the reloc address from the value on the top of the
573 relocation stack. */
574 HOWTO (ALPHA_R_OP_PSUB, /* type */
575 0, /* rightshift */
576 0, /* size (0 = byte, 1 = short, 2 = long) */
577 0, /* bitsize */
578 false, /* pc_relative */
579 0, /* bitpos */
580 complain_overflow_dont, /* complain_on_overflow */
ff12f303 581 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
582 "OP_PSUB", /* name */
583 false, /* partial_inplace */
584 0, /* src_mask */
585 0, /* dst_mask */
586 false), /* pcrel_offset */
587
588 /* Shift the value on the top of the relocation stack right by the
589 given value. */
590 HOWTO (ALPHA_R_OP_PRSHIFT, /* type */
591 0, /* rightshift */
592 0, /* size (0 = byte, 1 = short, 2 = long) */
593 0, /* bitsize */
594 false, /* pc_relative */
595 0, /* bitpos */
596 complain_overflow_dont, /* complain_on_overflow */
ff12f303 597 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
598 "OP_PRSHIFT", /* name */
599 false, /* partial_inplace */
600 0, /* src_mask */
601 0, /* dst_mask */
602 false), /* pcrel_offset */
603
604 /* Misc ELF relocations. */
605 HOWTO (R_ALPHA_COPY,
606 0,
607 0,
608 0,
609 false,
610 0,
611 complain_overflow_dont,
612 bfd_elf_generic_reloc,
613 "COPY",
614 false,
615 0,
ff12f303 616 0,
297a4f1a
ILT
617 true),
618
619 HOWTO (R_ALPHA_GLOB_DAT,
620 0,
621 0,
622 0,
623 false,
624 0,
625 complain_overflow_dont,
626 bfd_elf_generic_reloc,
627 "GLOB_DAT",
628 false,
629 0,
ff12f303 630 0,
297a4f1a
ILT
631 true),
632
633 HOWTO (R_ALPHA_JMP_SLOT,
634 0,
635 0,
636 0,
637 false,
638 0,
639 complain_overflow_dont,
640 bfd_elf_generic_reloc,
641 "JMP_SLOT",
642 false,
643 0,
644 0,
645 true),
646
647 HOWTO (R_ALPHA_RELATIVE,
648 0,
649 0,
650 0,
651 false,
652 0,
653 complain_overflow_dont,
654 bfd_elf_generic_reloc,
ff12f303 655 "RELATIVE",
297a4f1a
ILT
656 false,
657 0,
658 0,
659 true)
660};
661
ff12f303
ILT
662/* A relocation function which doesn't do anything. */
663
297a4f1a
ILT
664static bfd_reloc_status_type
665elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
666 bfd *abfd;
667 arelent *reloc;
668 asymbol *sym;
669 PTR data;
670 asection *sec;
671 bfd *output_bfd;
672 char **error_message;
673{
674 if (output_bfd)
675 reloc->address += sec->output_offset;
676 return bfd_reloc_ok;
677}
678
ff12f303
ILT
679/* A relocation function used for an unsupported reloc. */
680
681static bfd_reloc_status_type
682elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
683 bfd *abfd;
684 arelent *reloc;
685 asymbol *sym;
686 PTR data;
687 asection *sec;
688 bfd *output_bfd;
689 char **error_message;
690{
691 if (output_bfd)
692 reloc->address += sec->output_offset;
693 return bfd_reloc_notsupported;
694}
695
696/* Do the work of the GPDISP relocation. */
697
297a4f1a
ILT
698static bfd_reloc_status_type
699elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
700 bfd *abfd;
701 bfd_vma gpdisp;
ff12f303
ILT
702 bfd_byte *p_ldah;
703 bfd_byte *p_lda;
297a4f1a
ILT
704{
705 bfd_reloc_status_type ret = bfd_reloc_ok;
706 bfd_vma addend;
707 unsigned long i_ldah, i_lda;
708
ff12f303
ILT
709 i_ldah = bfd_get_32 (abfd, p_ldah);
710 i_lda = bfd_get_32 (abfd, p_lda);
297a4f1a
ILT
711
712 /* Complain if the instructions are not correct. */
713 if (((i_ldah >> 26) & 0x3f) != 0x09
714 || ((i_lda >> 26) & 0x3f) != 0x08)
715 ret = bfd_reloc_dangerous;
716
717 /* Extract the user-supplied offset, mirroring the sign extensions
718 that the instructions perform. */
719 addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
720 addend = (addend ^ 0x80008000) - 0x80008000;
721
722 gpdisp += addend;
723
ff12f303 724 if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma)0x80000000
297a4f1a
ILT
725 || gpdisp >= 0x7fff8000)
726 ret = bfd_reloc_overflow;
727
728 /* compensate for the sign extension again. */
729 i_ldah = ((i_ldah & 0xffff0000)
730 | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
731 i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
732
733 bfd_put_32 (abfd, i_ldah, p_ldah);
734 bfd_put_32 (abfd, i_lda, p_lda);
735
736 return ret;
737}
738
ff12f303
ILT
739/* The special function for the GPDISP reloc. */
740
297a4f1a
ILT
741static bfd_reloc_status_type
742elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
743 output_bfd, err_msg)
744 bfd *abfd;
745 arelent *reloc_entry;
746 asymbol *sym;
747 PTR data;
748 asection *input_section;
749 bfd *output_bfd;
750 char **err_msg;
751{
752 bfd_reloc_status_type ret;
753 bfd_vma gp, relocation;
754 bfd_byte *p_ldah, *p_lda;
755
756 /* Don't do anything if we're not doing a final link. */
757 if (output_bfd)
758 {
759 reloc_entry->address += input_section->output_offset;
760 return bfd_reloc_ok;
761 }
762
763 if (reloc_entry->address > input_section->_cooked_size ||
764 reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
765 return bfd_reloc_outofrange;
766
ff12f303 767 /* The gp used in the portion of the output object to which this
297a4f1a
ILT
768 input object belongs is cached on the input bfd. */
769 gp = _bfd_get_gp_value (abfd);
770
771 relocation = (input_section->output_section->vma
772 + input_section->output_offset
773 + reloc_entry->address);
774
ff12f303 775 p_ldah = (bfd_byte *) data + reloc_entry->address;
297a4f1a
ILT
776 p_lda = p_ldah + reloc_entry->addend;
777
778 ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
779
780 /* Complain if the instructions are not correct. */
781 if (ret == bfd_reloc_dangerous)
ff12f303 782 *err_msg = "GPDISP relocation did not find ldah and lda instructions";
297a4f1a
ILT
783
784 return ret;
785}
786
297a4f1a
ILT
787/* A mapping from BFD reloc types to Alpha ELF reloc types. */
788
789struct elf_reloc_map
790{
791 bfd_reloc_code_real_type bfd_reloc_val;
792 int elf_reloc_val;
793};
794
795static const struct elf_reloc_map elf64_alpha_reloc_map[] =
796{
797 {BFD_RELOC_NONE, R_ALPHA_NONE},
798 {BFD_RELOC_32, R_ALPHA_REFLONG},
799 {BFD_RELOC_64, R_ALPHA_REFQUAD},
800 {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
801 {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
ff12f303 802 {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
297a4f1a
ILT
803 {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
804 {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
ff12f303
ILT
805 {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
806 {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
807 {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
808 {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
809 {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
297a4f1a
ILT
810};
811
812/* Given a BFD reloc type, return a HOWTO structure. */
813
814static reloc_howto_type *
815elf64_alpha_bfd_reloc_type_lookup (abfd, code)
816 bfd *abfd;
817 bfd_reloc_code_real_type code;
818{
819 const struct elf_reloc_map *i, *e;
820 i = e = elf64_alpha_reloc_map;
821 e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
822 for (; i != e; ++i)
823 {
824 if (i->bfd_reloc_val == code)
825 return &elf64_alpha_howto_table[i->elf_reloc_val];
826 }
827 return 0;
828}
829
830/* Given an Alpha ELF reloc type, fill in an arelent structure. */
831
832static void
833elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
834 bfd *abfd;
835 arelent *cache_ptr;
836 Elf64_Internal_Rela *dst;
837{
838 unsigned r_type;
839
840 r_type = ELF64_R_TYPE(dst->r_info);
841 BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
842 cache_ptr->howto = &elf64_alpha_howto_table[r_type];
843}
844\f
845/* PLT/GOT Stuff */
846#define PLT_HEADER_SIZE 32
847#define PLT_HEADER_WORD1 0xc3600000 /* br $27,.+4 */
848#define PLT_HEADER_WORD2 0xa77b000c /* ldq $27,12($27) */
849#define PLT_HEADER_WORD3 0x47ff041f /* nop */
850#define PLT_HEADER_WORD4 0x6b7b0000 /* jmp $27,($27) */
851
852#define PLT_ENTRY_SIZE 12
853#define PLT_ENTRY_WORD1 0x279f0000 /* ldah $28, 0($31) */
854#define PLT_ENTRY_WORD2 0x239c0000 /* lda $28, 0($28) */
855#define PLT_ENTRY_WORD3 0xc3e00000 /* br $31, plt0 */
856
ff12f303 857#define MAX_GOT_ENTRIES (64*1024 / 8)
297a4f1a
ILT
858
859#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
860\f
861/* Set the right machine number for an Alpha ELF file. */
862
863static boolean
864elf64_alpha_object_p (abfd)
865 bfd *abfd;
866{
867 return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
868}
869\f
ff12f303 870/* Handle an Alpha specific section when reading an object file. This
297a4f1a
ILT
871 is called when elfcode.h finds a section with an unknown type.
872 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
873 how to. */
874
875static boolean
876elf64_alpha_section_from_shdr (abfd, hdr, name)
877 bfd *abfd;
878 Elf64_Internal_Shdr *hdr;
879 char *name;
880{
881 asection *newsect;
882
883 /* There ought to be a place to keep ELF backend specific flags, but
884 at the moment there isn't one. We just keep track of the
885 sections by their name, instead. Fortunately, the ABI gives
886 suggested names for all the MIPS specific sections, so we will
887 probably get away with this. */
888 switch (hdr->sh_type)
889 {
890 case SHT_ALPHA_DEBUG:
891 if (strcmp (name, ".mdebug") != 0)
892 return false;
893 break;
894#ifdef ERIC_neverdef
895 case SHT_ALPHA_REGINFO:
896 if (strcmp (name, ".reginfo") != 0
897 || hdr->sh_size != sizeof (Elf64_External_RegInfo))
898 return false;
899 break;
900#endif
901 default:
902 return false;
903 }
904
905 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
906 return false;
907 newsect = hdr->bfd_section;
908
909 if (hdr->sh_type == SHT_ALPHA_DEBUG)
910 {
911 if (! bfd_set_section_flags (abfd, newsect,
912 (bfd_get_section_flags (abfd, newsect)
913 | SEC_DEBUGGING)))
914 return false;
915 }
916
917#ifdef ERIC_neverdef
918 /* For a .reginfo section, set the gp value in the tdata information
919 from the contents of this section. We need the gp value while
920 processing relocs, so we just get it now. */
921 if (hdr->sh_type == SHT_ALPHA_REGINFO)
922 {
923 Elf64_External_RegInfo ext;
924 Elf64_RegInfo s;
925
926 if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
927 (file_ptr) 0, sizeof ext))
928 return false;
929 bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
930 elf_gp (abfd) = s.ri_gp_value;
931 }
932#endif
933
934 return true;
935}
936
937/* Set the correct type for an Alpha ELF section. We do this by the
938 section name, which is a hack, but ought to work. */
939
940static boolean
941elf64_alpha_fake_sections (abfd, hdr, sec)
942 bfd *abfd;
943 Elf64_Internal_Shdr *hdr;
944 asection *sec;
945{
946 register const char *name;
947
948 name = bfd_get_section_name (abfd, sec);
949
950 if (strcmp (name, ".mdebug") == 0)
951 {
952 hdr->sh_type = SHT_ALPHA_DEBUG;
953 /* In a shared object on Irix 5.3, the .mdebug section has an
954 entsize of 0. FIXME: Does this matter? */
955 if ((abfd->flags & DYNAMIC) != 0 )
956 hdr->sh_entsize = 0;
957 else
958 hdr->sh_entsize = 1;
959 }
960#ifdef ERIC_neverdef
961 else if (strcmp (name, ".reginfo") == 0)
962 {
963 hdr->sh_type = SHT_ALPHA_REGINFO;
964 /* In a shared object on Irix 5.3, the .reginfo section has an
965 entsize of 0x18. FIXME: Does this matter? */
966 if ((abfd->flags & DYNAMIC) != 0)
967 hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
968 else
969 hdr->sh_entsize = 1;
970
971 /* Force the section size to the correct value, even if the
972 linker thinks it is larger. The link routine below will only
973 write out this much data for .reginfo. */
974 hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
975 }
976 else if (strcmp (name, ".hash") == 0
977 || strcmp (name, ".dynamic") == 0
978 || strcmp (name, ".dynstr") == 0)
979 {
980 hdr->sh_entsize = 0;
981 hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
982 }
ff12f303 983#endif
297a4f1a
ILT
984 else if (strcmp (name, ".sdata") == 0
985 || strcmp (name, ".sbss") == 0
986 || strcmp (name, ".lit4") == 0
987 || strcmp (name, ".lit8") == 0)
988 hdr->sh_flags |= SHF_ALPHA_GPREL;
297a4f1a
ILT
989
990 return true;
991}
992
ff12f303
ILT
993/* Return the number of additional phdrs we will need. */
994
297a4f1a
ILT
995static int
996elf64_alpha_additional_program_headers (abfd)
997 bfd *abfd;
998{
999 asection *s;
1000 int ret;
1001
1002 ret = 0;
1003
1004 s = bfd_get_section_by_name (abfd, ".reginfo");
1005 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1006 {
1007 /* We need a PT_ALPHA_REGINFO segment. */
1008 ++ret;
1009 }
1010
1011 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
1012 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
1013 {
1014 /* We need a PT_ALPHA_RTPROC segment. */
1015 ++ret;
1016 }
1017
1018 return ret;
1019}
1020
ff12f303
ILT
1021/* Create the .got section. */
1022
297a4f1a
ILT
1023static boolean
1024elf64_alpha_create_got_section(abfd, info)
1025 bfd *abfd;
1026 struct bfd_link_info *info;
1027{
1028 asection *s;
297a4f1a
ILT
1029
1030 if (bfd_get_section_by_name (abfd, ".got"))
1031 return true;
1032
ff12f303 1033 s = bfd_make_section (abfd, ".got");
297a4f1a
ILT
1034 if (s == NULL
1035 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
ff12f303
ILT
1036 | SEC_HAS_CONTENTS
1037 | SEC_IN_MEMORY
1038 | SEC_LINKER_CREATED))
297a4f1a
ILT
1039 || !bfd_set_section_alignment (abfd, s, 3))
1040 return false;
1041
ff12f303 1042 alpha_elf_tdata (abfd)->got = s;
297a4f1a
ILT
1043
1044 return true;
1045}
1046
ff12f303
ILT
1047/* Create all the dynamic sections. */
1048
297a4f1a
ILT
1049static boolean
1050elf64_alpha_create_dynamic_sections (abfd, info)
1051 bfd *abfd;
1052 struct bfd_link_info *info;
1053{
ff12f303 1054 asection *s;
297a4f1a
ILT
1055 struct elf_link_hash_entry *h;
1056
1057 /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
1058
1059 s = bfd_make_section (abfd, ".plt");
1060 if (s == NULL
1061 || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
ff12f303
ILT
1062 | SEC_HAS_CONTENTS
1063 | SEC_IN_MEMORY
1064 | SEC_LINKER_CREATED
297a4f1a
ILT
1065 | SEC_CODE))
1066 || ! bfd_set_section_alignment (abfd, s, 3))
1067 return false;
1068
1069 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1070 .plt section. */
1071 h = NULL;
1072 if (! (_bfd_generic_link_add_one_symbol
1073 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1074 (bfd_vma) 0, (const char *) NULL, false,
1075 get_elf_backend_data (abfd)->collect,
1076 (struct bfd_link_hash_entry **) &h)))
1077 return false;
1078 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1079 h->type = STT_OBJECT;
1080
1081 if (info->shared
1082 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1083 return false;
1084
1085 s = bfd_make_section (abfd, ".rela.plt");
1086 if (s == NULL
1087 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
ff12f303
ILT
1088 | SEC_HAS_CONTENTS
1089 | SEC_IN_MEMORY
1090 | SEC_LINKER_CREATED
1091 | SEC_READONLY))
297a4f1a
ILT
1092 || ! bfd_set_section_alignment (abfd, s, 3))
1093 return false;
1094
ff12f303
ILT
1095 /* We may or may not have created a .got section for this object, but
1096 we definitely havn't done the rest of the work. */
1097
297a4f1a
ILT
1098 if (!elf64_alpha_create_got_section (abfd, info))
1099 return false;
1100
ff12f303
ILT
1101 s = bfd_make_section(abfd, ".rela.got");
1102 if (s == NULL
1103 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1104 | SEC_HAS_CONTENTS
1105 | SEC_IN_MEMORY
1106 | SEC_LINKER_CREATED
1107 | SEC_READONLY))
1108 || !bfd_set_section_alignment (abfd, s, 3))
1109 return false;
297a4f1a 1110
ff12f303
ILT
1111 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1112 dynobj's .got section. We don't do this in the linker script
1113 because we don't want to define the symbol if we are not creating
1114 a global offset table. */
1115 h = NULL;
1116 if (!(_bfd_generic_link_add_one_symbol
1117 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
1118 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
1119 false, get_elf_backend_data (abfd)->collect,
1120 (struct bfd_link_hash_entry **) &h)))
1121 return false;
1122 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1123 h->type = STT_OBJECT;
297a4f1a 1124
ff12f303
ILT
1125 if (info->shared
1126 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1127 return false;
1128
1129 elf_hash_table (info)->hgot = h;
1130
1131 return true;
1132}
297a4f1a
ILT
1133\f
1134/* Read ECOFF debugging information from a .mdebug section into a
1135 ecoff_debug_info structure. */
1136
1137static boolean
1138elf64_alpha_read_ecoff_info (abfd, section, debug)
1139 bfd *abfd;
1140 asection *section;
1141 struct ecoff_debug_info *debug;
1142{
1143 HDRR *symhdr;
1144 const struct ecoff_debug_swap *swap;
1145 char *ext_hdr = NULL;
1146
1147 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1148
1149 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
1150 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1151 goto error_return;
1152
1153 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1154 swap->external_hdr_size)
1155 == false)
1156 goto error_return;
1157
1158 symhdr = &debug->symbolic_header;
1159 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1160
1161 /* The symbolic header contains absolute file offsets and sizes to
1162 read. */
1163#define READ(ptr, offset, count, size, type) \
1164 if (symhdr->count == 0) \
1165 debug->ptr = NULL; \
1166 else \
1167 { \
1168 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
1169 if (debug->ptr == NULL) \
1170 goto error_return; \
1171 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1172 || (bfd_read (debug->ptr, size, symhdr->count, \
1173 abfd) != size * symhdr->count)) \
1174 goto error_return; \
1175 }
1176
1177 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1178 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1179 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1180 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1181 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1182 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1183 union aux_ext *);
1184 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1185 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1186 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1187 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1188 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1189#undef READ
1190
1191 debug->fdr = NULL;
1192 debug->adjust = NULL;
1193
1194 return true;
1195
1196 error_return:
1197 if (ext_hdr != NULL)
1198 free (ext_hdr);
1199 if (debug->line != NULL)
1200 free (debug->line);
1201 if (debug->external_dnr != NULL)
1202 free (debug->external_dnr);
1203 if (debug->external_pdr != NULL)
1204 free (debug->external_pdr);
1205 if (debug->external_sym != NULL)
1206 free (debug->external_sym);
1207 if (debug->external_opt != NULL)
1208 free (debug->external_opt);
1209 if (debug->external_aux != NULL)
1210 free (debug->external_aux);
1211 if (debug->ss != NULL)
1212 free (debug->ss);
1213 if (debug->ssext != NULL)
1214 free (debug->ssext);
1215 if (debug->external_fdr != NULL)
1216 free (debug->external_fdr);
1217 if (debug->external_rfd != NULL)
1218 free (debug->external_rfd);
1219 if (debug->external_ext != NULL)
1220 free (debug->external_ext);
1221 return false;
1222}
1223
1224/* Alpha ELF local labels start with '$'. */
1225
1226static boolean
303e7257 1227elf64_alpha_is_local_label_name (abfd, name)
297a4f1a 1228 bfd *abfd;
303e7257 1229 const char *name;
297a4f1a 1230{
303e7257 1231 return name[0] == '$';
297a4f1a
ILT
1232}
1233
1234/* Alpha ELF follows MIPS ELF in using a special find_nearest_line
1235 routine in order to handle the ECOFF debugging information. We
1236 still call this mips_elf_find_line because of the slot
1237 find_line_info in elf_obj_tdata is declared that way. */
1238
1239struct mips_elf_find_line
1240{
1241 struct ecoff_debug_info d;
1242 struct ecoff_find_line i;
1243};
1244
1245static boolean
1246elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
1247 functionname_ptr, line_ptr)
1248 bfd *abfd;
1249 asection *section;
1250 asymbol **symbols;
1251 bfd_vma offset;
1252 const char **filename_ptr;
1253 const char **functionname_ptr;
1254 unsigned int *line_ptr;
1255{
1256 asection *msec;
1257
1258 msec = bfd_get_section_by_name (abfd, ".mdebug");
1259 if (msec != NULL)
1260 {
1261 flagword origflags;
1262 struct mips_elf_find_line *fi;
1263 const struct ecoff_debug_swap * const swap =
1264 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1265
1266 /* If we are called during a link, alpha_elf_final_link may have
1267 cleared the SEC_HAS_CONTENTS field. We force it back on here
1268 if appropriate (which it normally will be). */
1269 origflags = msec->flags;
1270 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
1271 msec->flags |= SEC_HAS_CONTENTS;
1272
1273 fi = elf_tdata (abfd)->find_line_info;
1274 if (fi == NULL)
1275 {
1276 bfd_size_type external_fdr_size;
1277 char *fraw_src;
1278 char *fraw_end;
1279 struct fdr *fdr_ptr;
1280
1281 fi = ((struct mips_elf_find_line *)
1282 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
1283 if (fi == NULL)
1284 {
1285 msec->flags = origflags;
1286 return false;
1287 }
1288
1289 if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
1290 {
1291 msec->flags = origflags;
1292 return false;
1293 }
1294
1295 /* Swap in the FDR information. */
1296 fi->d.fdr = ((struct fdr *)
1297 bfd_alloc (abfd,
1298 (fi->d.symbolic_header.ifdMax *
1299 sizeof (struct fdr))));
1300 if (fi->d.fdr == NULL)
1301 {
1302 msec->flags = origflags;
1303 return false;
1304 }
1305 external_fdr_size = swap->external_fdr_size;
1306 fdr_ptr = fi->d.fdr;
1307 fraw_src = (char *) fi->d.external_fdr;
1308 fraw_end = (fraw_src
1309 + fi->d.symbolic_header.ifdMax * external_fdr_size);
1310 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
1311 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
1312
1313 elf_tdata (abfd)->find_line_info = fi;
1314
1315 /* Note that we don't bother to ever free this information.
1316 find_nearest_line is either called all the time, as in
1317 objdump -l, so the information should be saved, or it is
1318 rarely called, as in ld error messages, so the memory
1319 wasted is unimportant. Still, it would probably be a
1320 good idea for free_cached_info to throw it away. */
1321 }
1322
1323 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
1324 &fi->i, filename_ptr, functionname_ptr,
1325 line_ptr))
1326 {
1327 msec->flags = origflags;
1328 return true;
1329 }
1330
1331 msec->flags = origflags;
1332 }
1333
1334 /* Fall back on the generic ELF find_nearest_line routine. */
1335
1336 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
1337 filename_ptr, functionname_ptr,
1338 line_ptr);
1339}
1340\f
1341/* Structure used to pass information to alpha_elf_output_extsym. */
1342
1343struct extsym_info
1344{
1345 bfd *abfd;
1346 struct bfd_link_info *info;
1347 struct ecoff_debug_info *debug;
1348 const struct ecoff_debug_swap *swap;
1349 boolean failed;
1350};
1351
1352static boolean
1353elf64_alpha_output_extsym (h, data)
1354 struct alpha_elf_link_hash_entry *h;
1355 PTR data;
1356{
1357 struct extsym_info *einfo = (struct extsym_info *) data;
1358 boolean strip;
1359 asection *sec, *output_section;
1360
1361 if (h->root.indx == -2)
1362 strip = false;
1363 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1364 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1365 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1366 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
1367 strip = true;
1368 else if (einfo->info->strip == strip_all
1369 || (einfo->info->strip == strip_some
1370 && bfd_hash_lookup (einfo->info->keep_hash,
1371 h->root.root.root.string,
1372 false, false) == NULL))
1373 strip = true;
1374 else
1375 strip = false;
1376
1377 if (strip)
1378 return true;
1379
1380 if (h->esym.ifd == -2)
1381 {
1382 h->esym.jmptbl = 0;
1383 h->esym.cobol_main = 0;
1384 h->esym.weakext = 0;
1385 h->esym.reserved = 0;
1386 h->esym.ifd = ifdNil;
1387 h->esym.asym.value = 0;
1388 h->esym.asym.st = stGlobal;
1389
1390 if (h->root.root.type != bfd_link_hash_defined
1391 && h->root.root.type != bfd_link_hash_defweak)
1392 h->esym.asym.sc = scAbs;
1393 else
1394 {
1395 const char *name;
1396
1397 sec = h->root.root.u.def.section;
1398 output_section = sec->output_section;
1399
1400 /* When making a shared library and symbol h is the one from
1401 the another shared library, OUTPUT_SECTION may be null. */
1402 if (output_section == NULL)
1403 h->esym.asym.sc = scUndefined;
1404 else
1405 {
1406 name = bfd_section_name (output_section->owner, output_section);
ff12f303 1407
297a4f1a
ILT
1408 if (strcmp (name, ".text") == 0)
1409 h->esym.asym.sc = scText;
1410 else if (strcmp (name, ".data") == 0)
1411 h->esym.asym.sc = scData;
1412 else if (strcmp (name, ".sdata") == 0)
1413 h->esym.asym.sc = scSData;
1414 else if (strcmp (name, ".rodata") == 0
1415 || strcmp (name, ".rdata") == 0)
1416 h->esym.asym.sc = scRData;
1417 else if (strcmp (name, ".bss") == 0)
1418 h->esym.asym.sc = scBss;
1419 else if (strcmp (name, ".sbss") == 0)
1420 h->esym.asym.sc = scSBss;
1421 else if (strcmp (name, ".init") == 0)
1422 h->esym.asym.sc = scInit;
1423 else if (strcmp (name, ".fini") == 0)
1424 h->esym.asym.sc = scFini;
1425 else
1426 h->esym.asym.sc = scAbs;
1427 }
1428 }
1429
1430 h->esym.asym.reserved = 0;
1431 h->esym.asym.index = indexNil;
1432 }
1433
1434 if (h->root.root.type == bfd_link_hash_common)
1435 h->esym.asym.value = h->root.root.u.c.size;
1436 else if (h->root.root.type == bfd_link_hash_defined
1437 || h->root.root.type == bfd_link_hash_defweak)
1438 {
1439 if (h->esym.asym.sc == scCommon)
1440 h->esym.asym.sc = scBss;
1441 else if (h->esym.asym.sc == scSCommon)
1442 h->esym.asym.sc = scSBss;
1443
1444 sec = h->root.root.u.def.section;
1445 output_section = sec->output_section;
1446 if (output_section != NULL)
1447 h->esym.asym.value = (h->root.root.u.def.value
1448 + sec->output_offset
1449 + output_section->vma);
1450 else
1451 h->esym.asym.value = 0;
1452 }
1453 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1454 {
1455 /* Set type and value for a symbol with a function stub. */
1456 h->esym.asym.st = stProc;
ff12f303 1457 sec = bfd_get_section_by_name (einfo->abfd, ".plt");
297a4f1a 1458 if (sec == NULL)
ff12f303 1459 h->esym.asym.value = 0;
297a4f1a 1460 else
ff12f303
ILT
1461 {
1462 output_section = sec->output_section;
1463 if (output_section != NULL)
1464 h->esym.asym.value = (h->root.plt_offset
1465 + sec->output_offset
1466 + output_section->vma);
1467 else
1468 h->esym.asym.value = 0;
1469 }
297a4f1a
ILT
1470#if 0 /* FIXME? */
1471 h->esym.ifd = 0;
1472#endif
ff12f303 1473 }
297a4f1a
ILT
1474
1475 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1476 h->root.root.root.string,
1477 &h->esym))
1478 {
1479 einfo->failed = true;
1480 return false;
1481 }
1482
1483 return true;
1484}
1485
1486/* FIXME: Create a runtime procedure table from the .mdebug section.
1487
1488static boolean
1489mips_elf_create_procedure_table (handle, abfd, info, s, debug)
1490 PTR handle;
1491 bfd *abfd;
1492 struct bfd_link_info *info;
1493 asection *s;
1494 struct ecoff_debug_info *debug;
ff12f303 1495*/
297a4f1a 1496\f
ff12f303
ILT
1497/* Handle dynamic relocations when doing an Alpha ELF link. */
1498
297a4f1a
ILT
1499static boolean
1500elf64_alpha_check_relocs (abfd, info, sec, relocs)
1501 bfd *abfd;
1502 struct bfd_link_info *info;
1503 asection *sec;
1504 const Elf_Internal_Rela *relocs;
1505{
1506 bfd *dynobj;
297a4f1a 1507 asection *sreloc;
ff12f303 1508 const char *rel_sec_name;
297a4f1a 1509 Elf_Internal_Shdr *symtab_hdr;
ff12f303
ILT
1510 struct alpha_elf_link_hash_entry **sym_hashes;
1511 struct alpha_elf_got_entry **local_got_entries;
297a4f1a 1512 const Elf_Internal_Rela *rel, *relend;
ff12f303 1513 int got_created;
297a4f1a
ILT
1514
1515 if (info->relocateable)
1516 return true;
1517
297a4f1a 1518 dynobj = elf_hash_table(info)->dynobj;
ff12f303
ILT
1519 if (dynobj == NULL)
1520 elf_hash_table(info)->dynobj = dynobj = abfd;
1521
1522 sreloc = NULL;
1523 rel_sec_name = NULL;
1524 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
1525 sym_hashes = alpha_elf_sym_hashes(abfd);
1526 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1527 got_created = 0;
297a4f1a
ILT
1528
1529 relend = relocs + sec->reloc_count;
1530 for (rel = relocs; rel < relend; ++rel)
1531 {
ff12f303 1532 unsigned long r_symndx, r_type;
37d7888d 1533 struct alpha_elf_link_hash_entry *h;
297a4f1a 1534
37d7888d 1535 r_symndx = ELF64_R_SYM (rel->r_info);
297a4f1a
ILT
1536 if (r_symndx < symtab_hdr->sh_info)
1537 h = NULL;
1538 else
ff12f303
ILT
1539 {
1540 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1541 h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1542 }
1543 r_type = ELF64_R_TYPE (rel->r_info);
297a4f1a 1544
ff12f303 1545 switch (r_type)
297a4f1a
ILT
1546 {
1547 case R_ALPHA_LITERAL:
ff12f303
ILT
1548 {
1549 struct alpha_elf_got_entry *gotent;
1550 int flags = 0;
297a4f1a 1551
ff12f303
ILT
1552 if (h)
1553 {
1554 /* Search for and possibly create a got entry. */
1555 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
1556 if (gotent->gotobj == abfd &&
1557 gotent->addend == rel->r_addend)
1558 break;
1559
1560 if (!gotent)
1561 {
1562 gotent = ((struct alpha_elf_got_entry *)
1563 bfd_alloc (abfd,
1564 sizeof (struct alpha_elf_got_entry)));
1565 if (!gotent)
1566 return false;
297a4f1a 1567
ff12f303
ILT
1568 gotent->gotobj = abfd;
1569 gotent->addend = rel->r_addend;
1570 gotent->got_offset = -1;
1571 gotent->flags = 0;
297a4f1a 1572
ff12f303
ILT
1573 gotent->next = h->got_entries;
1574 h->got_entries = gotent;
37d7888d 1575
ff12f303
ILT
1576 alpha_elf_tdata (abfd)->total_got_entries++;
1577 }
1578 }
1579 else
1580 {
1581 /* This is a local .got entry -- record for merge. */
1582 if (!local_got_entries)
1583 {
1584 size_t size;
1585 size = (symtab_hdr->sh_info
1586 * sizeof (struct alpha_elf_got_entry *));
1587
1588 local_got_entries = ((struct alpha_elf_got_entry **)
1589 bfd_alloc (abfd, size));
1590 if (!local_got_entries)
1591 return false;
1592
1593 memset (local_got_entries, 0, size);
1594 alpha_elf_tdata (abfd)->local_got_entries =
1595 local_got_entries;
1596 }
1597
1598 for (gotent = local_got_entries[ELF64_R_SYM(rel->r_info)];
1599 gotent != NULL && gotent->addend != rel->r_addend;
1600 gotent = gotent->next)
1601 continue;
1602 if (!gotent)
1603 {
1604 gotent = ((struct alpha_elf_got_entry *)
1605 bfd_alloc (abfd,
1606 sizeof (struct alpha_elf_got_entry)));
1607 if (!gotent)
1608 return false;
1609
1610 gotent->gotobj = abfd;
1611 gotent->addend = rel->r_addend;
1612 gotent->got_offset = -1;
1613 gotent->flags = 0;
1614
1615 gotent->next = local_got_entries[ELF64_R_SYM(rel->r_info)];
1616 local_got_entries[ELF64_R_SYM(rel->r_info)] = gotent;
1617
1618 alpha_elf_tdata(abfd)->total_got_entries++;
1619 alpha_elf_tdata(abfd)->n_local_got_entries++;
1620 }
1621 }
1622
1623 /* Remember how this literal is used from its LITUSEs.
1624 This will be important when it comes to decide if we can
1625 create a .plt entry for a function symbol. */
1626 if (rel+1 < relend
1627 && ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
1628 {
1629 do
1630 {
1631 ++rel;
1632 if (rel->r_addend >= 1 && rel->r_addend <= 3)
1633 flags |= 1 << rel->r_addend;
1634 }
1635 while (rel+1 < relend &&
1636 ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE);
1637 }
1638 else
1639 {
1640 /* No LITUSEs -- presumably the address is not being
1641 loaded for nothing. */
1642 flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
1643 }
1644
1645 gotent->flags |= flags;
1646 if (h)
1647 {
1648 /* Make a guess as to whether a .plt entry will be needed. */
1649 if ((h->flags |= flags) == ALPHA_ELF_LINK_HASH_LU_FUNC)
1650 h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1651 else
1652 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1653 }
1654 }
1655 /* FALLTHRU */
1656
1657 case R_ALPHA_GPDISP:
1658 case R_ALPHA_GPREL32:
1659 /* We don't actually use the .got here, but the sections must
1660 be created before the linker maps input sections to output
1661 sections. */
1662 if (!got_created)
297a4f1a 1663 {
ff12f303
ILT
1664 if (!elf64_alpha_create_got_section (abfd, info))
1665 return false;
1666
1667 /* Make sure the object's gotobj is set to itself so
1668 that we default to every object with its own .got.
1669 We'll merge .gots later once we've collected each
1670 object's info. */
1671 alpha_elf_tdata(abfd)->gotobj = abfd;
1672
1673 got_created = 1;
1674 }
1675 break;
1676
1677 case R_ALPHA_SREL16:
1678 case R_ALPHA_SREL32:
1679 case R_ALPHA_SREL64:
1680 if (h == NULL)
1681 break;
1682 /* FALLTHRU */
1683
1684 case R_ALPHA_REFLONG:
1685 case R_ALPHA_REFQUAD:
1686 if (rel_sec_name == NULL)
1687 {
1688 rel_sec_name = (bfd_elf_string_from_elf_section
1689 (abfd, elf_elfheader(abfd)->e_shstrndx,
1690 elf_section_data(sec)->rel_hdr.sh_name));
1691 if (rel_sec_name == NULL)
1692 return false;
1693
1694 BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
1695 && strcmp (bfd_get_section_name (abfd, sec),
1696 rel_sec_name+5) == 0);
1697 }
1698
1699 /* We need to create the section here now whether we eventually
1700 use it or not so that it gets mapped to an output section by
1701 the linker. If not used, we'll kill it in
1702 size_dynamic_sections. */
1703 if (sreloc == NULL)
1704 {
1705 sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
1706 if (sreloc == NULL)
37d7888d 1707 {
ff12f303
ILT
1708 sreloc = bfd_make_section (dynobj, rel_sec_name);
1709 if (sreloc == NULL
1710 || !bfd_set_section_flags (dynobj, sreloc,
1711 (SEC_ALLOC|SEC_LOAD
1712 | SEC_HAS_CONTENTS
1713 | SEC_IN_MEMORY
1714 | SEC_LINKER_CREATED
1715 | SEC_READONLY))
1716 || !bfd_set_section_alignment (dynobj, sreloc, 3))
1717 return false;
37d7888d 1718 }
297a4f1a
ILT
1719 }
1720
ff12f303 1721 if (h)
297a4f1a 1722 {
ff12f303
ILT
1723 /* Since we havn't seen all of the input symbols yet, we
1724 don't know whether we'll actually need a dynamic relocation
1725 entry for this reloc. So make a record of it. Once we
1726 find out if this thing needs dynamic relocation we'll
1727 expand the relocation sections by the appropriate amount. */
297a4f1a 1728
ff12f303
ILT
1729 struct alpha_elf_reloc_entry *rent;
1730
1731 for (rent = h->reloc_entries; rent; rent = rent->next)
1732 if (rent->rtype == r_type && rent->srel == sreloc)
1733 break;
1734
1735 if (!rent)
1736 {
1737 rent = ((struct alpha_elf_reloc_entry *)
1738 bfd_alloc (abfd,
1739 sizeof (struct alpha_elf_reloc_entry)));
1740 if (!rent)
1741 return false;
1742
1743 rent->srel = sreloc;
1744 rent->rtype = r_type;
1745 rent->count = 1;
297a4f1a 1746
ff12f303
ILT
1747 rent->next = h->reloc_entries;
1748 h->reloc_entries = rent;
1749 }
1750 else
1751 rent->count++;
297a4f1a 1752 }
ff12f303
ILT
1753 else if (info->shared)
1754 {
1755 /* If this is a shared library, we need a RELATIVE reloc. */
1756 sreloc->_raw_size += sizeof (Elf64_External_Rela);
1757 }
1758 break;
1759 }
1760 }
1761
1762 return true;
1763}
1764
1765/* Adjust a symbol defined by a dynamic object and referenced by a
1766 regular object. The current definition is in some section of the
1767 dynamic object, but we're not including those sections. We have to
1768 change the definition to something the rest of the link can
1769 understand. */
1770
1771static boolean
1772elf64_alpha_adjust_dynamic_symbol (info, h)
1773 struct bfd_link_info *info;
1774 struct elf_link_hash_entry *h;
1775{
1776 bfd *dynobj;
1777 asection *s;
1778 struct alpha_elf_link_hash_entry *ah;
1779
1780 dynobj = elf_hash_table(info)->dynobj;
1781 ah = (struct alpha_elf_link_hash_entry *)h;
1782
1783 /* Now that we've seen all of the input symbols, finalize our decision
1784 about whether this symbol should get a .plt entry. */
1785
1786 if (h->root.type != bfd_link_hash_undefweak
1787 && alpha_elf_dynamic_symbol_p (h, info)
1788 && (h->type == STT_FUNC
1789 || (h->type == STT_NOTYPE
1790 && ah->flags == ALPHA_ELF_LINK_HASH_LU_FUNC))
1791 /* Don't prevent otherwise valid programs from linking by attempting
1792 to create a new .got entry somewhere. A Correct Solution would be
1793 to add a new .got section to a new object file and let it be merged
1794 somewhere later. But for now don't bother. */
1795 && ah->got_entries)
1796 {
1797 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1798
1799 s = bfd_get_section_by_name(dynobj, ".plt");
1800 if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
1801 return false;
1802
1803 /* The first bit of the .plt is reserved. */
1804 if (s->_raw_size == 0)
1805 s->_raw_size = PLT_HEADER_SIZE;
1806
1807 h->plt_offset = s->_raw_size;
1808 s->_raw_size += PLT_ENTRY_SIZE;
1809
1810 /* If this symbol is not defined in a regular file, and we are not
1811 generating a shared library, then set the symbol to the location
1812 in the .plt. This is required to make function pointers compare
1813 equal between the normal executable and the shared library. */
1814 if (!info->shared)
1815 {
1816 h->root.u.def.section = s;
1817 h->root.u.def.value = h->plt_offset;
1818 }
1819
1820 /* We also need a JMP_SLOT entry in the .rela.plt section. */
1821 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1822 BFD_ASSERT (s != NULL);
1823 s->_raw_size += sizeof (Elf64_External_Rela);
1824
1825 return true;
1826 }
1827 else
1828 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1829
1830 /* If this is a weak symbol, and there is a real definition, the
1831 processor independent code will have arranged for us to see the
1832 real definition first, and we can just use the same value. */
1833 if (h->weakdef != NULL)
1834 {
1835 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1836 || h->weakdef->root.type == bfd_link_hash_defweak);
1837 h->root.u.def.section = h->weakdef->root.u.def.section;
1838 h->root.u.def.value = h->weakdef->root.u.def.value;
1839 return true;
1840 }
1841
1842 /* This is a reference to a symbol defined by a dynamic object which
1843 is not a function. The Alpha, since it uses .got entries for all
1844 symbols even in regular objects, does not need the hackery of a
1845 .dynbss section and COPY dynamic relocations. */
1846
1847 return true;
1848}
1849
1850/* Is it possible to merge two object file's .got tables? */
1851
1852static boolean
1853elf64_alpha_can_merge_gots (a, b)
1854 bfd *a, *b;
1855{
1856 int total = alpha_elf_tdata (a)->total_got_entries;
1857
1858 /* Trivial quick fallout test. */
1859 if (total + alpha_elf_tdata (b)->total_got_entries <= MAX_GOT_ENTRIES)
1860 return true;
1861
1862 /* By their nature, local .got entries cannot be merged. */
1863 if ((total += alpha_elf_tdata (b)->n_local_got_entries) > MAX_GOT_ENTRIES)
1864 return false;
1865
1866 /* Failing the common trivial comparison, we must effectively
1867 perform the merge. Not actually performing the merge means that
1868 we don't have to store undo information in case we fail. */
1869 {
1870 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
1871 Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
1872 int i, n;
1873
1874 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
1875 for (i = 0; i < n; ++i)
1876 {
1877 struct alpha_elf_got_entry *ae, *be;
1878 for (be = hashes[i]->got_entries; be ; be = be->next)
1879 {
1880 if (be->gotobj != b)
1881 continue;
1882
1883 for (ae = hashes[i]->got_entries; ae ; ae = ae->next)
1884 if (ae->gotobj == a && ae->addend == be->addend)
1885 goto global_found;
1886
1887 if (++total > MAX_GOT_ENTRIES)
1888 return false;
1889 global_found:;
1890 }
1891 }
1892 }
1893
1894 return true;
1895}
1896
1897/* Actually merge two .got tables. */
1898
1899static void
1900elf64_alpha_merge_gots (a, b)
1901 bfd *a, *b;
1902{
1903 int total = alpha_elf_tdata(a)->total_got_entries;
1904
1905 /* Remember local expansion. */
1906 {
1907 int e = alpha_elf_tdata(b)->n_local_got_entries;
1908 total += e;
1909 alpha_elf_tdata(a)->n_local_got_entries += e;
1910 }
1911
1912 /* Let the local .got entries know they are part of a new subsegment. */
1913 {
1914 struct alpha_elf_got_entry **local_got_entries;
1915 local_got_entries = alpha_elf_tdata(b)->local_got_entries;
1916 if (local_got_entries)
1917 {
1918 int i, n;
1919
1920 n = elf_tdata(b)->symtab_hdr.sh_info;
1921 for (i = 0; i < n; ++i)
1922 {
1923 struct alpha_elf_got_entry *gotent;
1924 for (gotent = local_got_entries[i]; gotent; gotent = gotent->next)
1925 gotent->gotobj = a;
1926 }
1927 }
1928 }
1929
1930 /* Merge the global .got entries. */
1931 {
1932 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
1933 Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
1934 int i, n;
1935
1936 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
1937 for (i = 0; i < n; ++i)
1938 {
1939 struct alpha_elf_got_entry *ae, *be, **pbe, **start;
1940 start = &hashes[i]->got_entries;
1941 for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
1942 {
1943 if (be->gotobj != b)
1944 continue;
297a4f1a 1945
ff12f303
ILT
1946 for (ae = *start; ae ; ae = ae->next)
1947 if (ae->gotobj == a && ae->addend == be->addend)
297a4f1a 1948 {
ff12f303
ILT
1949 *pbe = be->next;
1950 goto global_found;
297a4f1a 1951 }
ff12f303
ILT
1952 be->gotobj = a;
1953 total += 1;
297a4f1a 1954
ff12f303
ILT
1955 global_found:;
1956 }
1957 }
1958 }
297a4f1a 1959
ff12f303
ILT
1960 alpha_elf_tdata(a)->total_got_entries = total;
1961 alpha_elf_tdata(b)->gotobj = a;
1962}
297a4f1a 1963
ff12f303 1964/* Calculate the offsets for the got entries. */
297a4f1a 1965
ff12f303
ILT
1966static boolean
1967elf64_alpha_calc_got_offsets_for_symbol (h, arg)
1968 struct alpha_elf_link_hash_entry *h;
1969 PTR arg;
1970{
1971 struct alpha_elf_got_entry *gotent;
297a4f1a 1972
ff12f303
ILT
1973 for (gotent = h->got_entries; gotent; gotent = gotent->next)
1974 {
1975 bfd_size_type *plge = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
1976 gotent->got_offset = *plge;
1977 *plge += 8;
1978 }
297a4f1a 1979
ff12f303
ILT
1980 return true;
1981}
297a4f1a 1982
ff12f303
ILT
1983static void
1984elf64_alpha_calc_got_offsets (info)
1985 struct bfd_link_info *info;
1986{
1987 bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
297a4f1a 1988
ff12f303
ILT
1989 /* First, zero out the .got sizes, as we may be recalculating the
1990 .got after optimizing it. */
1991 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
1992 alpha_elf_tdata(i)->got->_raw_size = 0;
297a4f1a 1993
ff12f303
ILT
1994 /* Next, fill in the offsets for all the global entries. */
1995 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
1996 elf64_alpha_calc_got_offsets_for_symbol,
1997 NULL);
297a4f1a 1998
ff12f303
ILT
1999 /* Finally, fill in the offsets for the local entries. */
2000 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2001 {
2002 bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
2003 bfd *j;
297a4f1a 2004
ff12f303
ILT
2005 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2006 {
2007 struct alpha_elf_got_entry **local_got_entries, *gotent;
2008 int k, n;
297a4f1a 2009
ff12f303
ILT
2010 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2011 if (!local_got_entries)
2012 continue;
2013
2014 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
2015 for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
2016 {
2017 gotent->got_offset = got_offset;
2018 got_offset += 8;
2019 }
297a4f1a 2020 }
ff12f303
ILT
2021
2022 alpha_elf_tdata(i)->got->_raw_size = got_offset;
297a4f1a 2023 }
ff12f303 2024}
297a4f1a 2025
ff12f303
ILT
2026/* Remove a section from the output BFD. */
2027
2028static void
2029elf64_alpha_strip_section_from_output (s)
2030 asection *s;
2031{
2032 asection **spp;
2033
2034 for (spp = &s->output_section->owner->sections;
2035 *spp != s->output_section;
2036 spp = &(*spp)->next)
2037 continue;
2038 *spp = s->output_section->next;
2039 --s->output_section->owner->section_count;
297a4f1a
ILT
2040}
2041
ff12f303 2042/* Constructs the gots. */
297a4f1a
ILT
2043
2044static boolean
ff12f303
ILT
2045elf64_alpha_always_size_sections (output_bfd, info)
2046 bfd *output_bfd;
297a4f1a 2047 struct bfd_link_info *info;
297a4f1a 2048{
ff12f303
ILT
2049 bfd *i, *got_list, *cur_got_obj, **cur_got_tail;
2050 int ngots;
297a4f1a 2051
ff12f303
ILT
2052 if (info->relocateable)
2053 return true;
297a4f1a 2054
ff12f303
ILT
2055 ngots = 0;
2056 got_list = NULL;
2057 cur_got_obj = NULL;
2058 cur_got_tail = NULL;
2059 for (i = info->input_bfds; i ; i = i->link_next)
297a4f1a 2060 {
ff12f303
ILT
2061 bfd *this_got = alpha_elf_tdata (i)->gotobj;
2062
2063 /* Don't play if there is no .got for this input file. */
2064 if (this_got == NULL)
2065 continue;
2066
2067 if (alpha_elf_tdata (this_got)->total_got_entries > MAX_GOT_ENTRIES)
297a4f1a 2068 {
ff12f303
ILT
2069 /* Yikes! A single object file has too many entries. */
2070 (*_bfd_error_handler)
2071 ("%s: .got subsegment exceeds 64K (size %d)",
2072 bfd_get_filename(i),
2073 alpha_elf_tdata(this_got)->total_got_entries * 8);
2074 return false;
297a4f1a
ILT
2075 }
2076
ff12f303
ILT
2077 if (cur_got_obj)
2078 {
2079 if (elf64_alpha_can_merge_gots (cur_got_obj, i))
2080 {
2081 elf64_alpha_merge_gots (cur_got_obj, i);
2082 *cur_got_tail = i;
2083 }
2084 else
2085 {
2086 if (++ngots == 2)
2087 {
2088 (*info->callbacks->warning)
2089 (info, "using multiple gp values", (char *) NULL,
2090 output_bfd, (asection *) NULL, (bfd_vma) 0);
2091 }
2092 *cur_got_tail = NULL;
2093 alpha_elf_tdata(cur_got_obj)->got_link_next = got_list;
2094 got_list = cur_got_obj;
2095 cur_got_obj = i;
2096 }
2097 }
2098 else
2099 {
2100 ++ngots;
2101 cur_got_obj = i;
2102 }
2103 cur_got_tail = &alpha_elf_tdata(i)->in_got_link_next;
2104 }
297a4f1a 2105
ff12f303
ILT
2106 if (cur_got_obj)
2107 alpha_elf_tdata (cur_got_obj)->got_link_next = got_list;
2108 alpha_elf_hash_table (info)->got_list = got_list = cur_got_obj;
297a4f1a 2109
ff12f303
ILT
2110 /* Once the gots have been merged, fill in the got offsets for everything
2111 therein. */
2112 elf64_alpha_calc_got_offsets (info);
297a4f1a 2113
ff12f303
ILT
2114 /* Allocate space for all of the .got subsections. */
2115 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2116 {
2117 asection *s = alpha_elf_tdata(i)->got;
2118 if (s->_raw_size > 0)
297a4f1a 2119 {
ff12f303
ILT
2120 s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
2121 if (s->contents == NULL)
2122 return false;
297a4f1a 2123 }
ff12f303 2124 }
297a4f1a 2125
ff12f303
ILT
2126 return true;
2127}
297a4f1a 2128
ff12f303 2129/* Work out the sizes of the dynamic relocation entries. */
297a4f1a 2130
ff12f303
ILT
2131static boolean
2132elf64_alpha_calc_dynrel_sizes (h, info)
2133 struct alpha_elf_link_hash_entry *h;
2134 struct bfd_link_info *info;
2135{
2136 /* If the symbol was defined as a common symbol in a regular object
2137 file, and there was no definition in any dynamic object, then the
2138 linker will have allocated space for the symbol in a common
2139 section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
2140 set. This is done for dynamic symbols in
2141 elf_adjust_dynamic_symbol but this is not done for non-dynamic
2142 symbols, somehow. */
2143 if (((h->root.elf_link_hash_flags
2144 & (ELF_LINK_HASH_DEF_REGULAR
2145 | ELF_LINK_HASH_REF_REGULAR
2146 | ELF_LINK_HASH_DEF_DYNAMIC))
2147 == ELF_LINK_HASH_REF_REGULAR)
2148 && (h->root.root.type == bfd_link_hash_defined
2149 || h->root.root.type == bfd_link_hash_defweak)
2150 && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
2151 {
2152 h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
297a4f1a
ILT
2153 }
2154
ff12f303
ILT
2155 /* If the symbol is dynamic, we'll need all the relocations in their
2156 natural form. */
2157 if (alpha_elf_dynamic_symbol_p (&h->root, info))
297a4f1a 2158 {
ff12f303
ILT
2159 struct alpha_elf_reloc_entry *relent;
2160
2161 for (relent = h->reloc_entries; relent; relent = relent->next)
2162 {
2163 relent->srel->_raw_size +=
2164 sizeof (Elf64_External_Rela) * relent->count;
2165 }
2166
2167 /* Only add a .rela.got entry if we're not using a .plt entry. */
2168 if (h->root.plt_offset == MINUS_ONE)
2169 {
2170 bfd *dynobj = elf_hash_table(info)->dynobj;
2171 struct alpha_elf_got_entry *gotent;
2172 bfd_size_type count = 0;
2173 asection *srel;
2174
2175 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2176 count++;
2177 if (count > 0)
2178 {
2179 srel = bfd_get_section_by_name (dynobj, ".rela.got");
2180 BFD_ASSERT (srel != NULL);
2181 srel->_raw_size += sizeof (Elf64_External_Rela) * count;
2182 }
2183 }
297a4f1a 2184 }
ff12f303
ILT
2185 /* Otherwise, shared objects require RELATIVE relocs for all REFQUAD
2186 and REFLONG relocations. */
2187 else if (info->shared)
2188 {
2189 struct alpha_elf_reloc_entry *relent;
297a4f1a 2190
ff12f303
ILT
2191 for (relent = h->reloc_entries; relent; relent = relent->next)
2192 if (relent->rtype == R_ALPHA_REFLONG
2193 || relent->rtype == R_ALPHA_REFQUAD)
2194 {
2195 relent->srel->_raw_size +=
2196 sizeof(Elf64_External_Rela) * relent->count;
2197 }
2198 }
297a4f1a
ILT
2199
2200 return true;
2201}
2202
2203/* Set the sizes of the dynamic sections. */
2204
2205static boolean
2206elf64_alpha_size_dynamic_sections (output_bfd, info)
2207 bfd *output_bfd;
2208 struct bfd_link_info *info;
2209{
2210 bfd *dynobj;
2211 asection *s;
2212 boolean reltext;
2213 boolean relplt;
2214
2215 dynobj = elf_hash_table(info)->dynobj;
2216 BFD_ASSERT(dynobj != NULL);
2217
ff12f303 2218 if (elf_hash_table (info)->dynamic_sections_created)
297a4f1a
ILT
2219 {
2220 /* Set the contents of the .interp section to the interpreter. */
2221 if (!info->shared)
2222 {
ff12f303
ILT
2223 s = bfd_get_section_by_name (dynobj, ".interp");
2224 BFD_ASSERT (s != NULL);
297a4f1a 2225 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
ff12f303
ILT
2226 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2227 }
2228
2229 /* Now that we've seen all of the input files, we can decide which
2230 symbols need dynamic relocation entries and which don't. We've
2231 collected information in check_relocs that we can now apply to
2232 size the dynamic relocation sections. */
2233 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2234 elf64_alpha_calc_dynrel_sizes,
2235 info);
2236
2237 /* When building shared libraries, each local .got entry needs a
2238 RELATIVE reloc. */
2239 if (info->shared)
2240 {
2241 bfd *i;
2242 asection *srel;
2243 bfd_size_type count;
2244
2245 srel = bfd_get_section_by_name (dynobj, ".rela.got");
2246 BFD_ASSERT (srel != NULL);
2247
2248 for (i = alpha_elf_hash_table(info)->got_list, count = 0;
2249 i != NULL;
2250 i = alpha_elf_tdata(i)->got_link_next)
2251 count += alpha_elf_tdata(i)->n_local_got_entries;
2252
2253 srel->_raw_size += count * sizeof(Elf64_External_Rela);
297a4f1a
ILT
2254 }
2255 }
ff12f303 2256 /* else we're not dynamic and by definition we don't need such things. */
297a4f1a
ILT
2257
2258 /* The check_relocs and adjust_dynamic_symbol entry points have
2259 determined the sizes of the various dynamic sections. Allocate
2260 memory for them. */
2261 reltext = false;
2262 relplt = false;
2263 for (s = dynobj->sections; s != NULL; s = s->next)
2264 {
2265 const char *name;
2266 boolean strip;
2267
ff12f303 2268 if (!(s->flags & SEC_LINKER_CREATED))
297a4f1a
ILT
2269 continue;
2270
2271 /* It's OK to base decisions on the section name, because none
2272 of the dynobj section names depend upon the input files. */
ff12f303 2273 name = bfd_get_section_name (dynobj, s);
297a4f1a
ILT
2274
2275 /* If we don't need this section, strip it from the output file.
2276 This is to handle .rela.bss and .rela.plt. We must create it
2277 in create_dynamic_sections, because it must be created before
2278 the linker maps input sections to output sections. The
2279 linker does that before adjust_dynamic_symbol is called, and
2280 it is that function which decides whether anything needs to
2281 go into these sections. */
2282
2283 strip = false;
2284
ff12f303 2285 if (strncmp (name, ".rela", 5) == 0)
297a4f1a
ILT
2286 {
2287 strip = (s->_raw_size == 0);
2288
2289 if (!strip)
2290 {
303e7257 2291 const char *outname;
297a4f1a
ILT
2292 asection *target;
2293
2294 /* If this relocation section applies to a read only
2295 section, then we probably need a DT_TEXTREL entry. */
303e7257
ILT
2296 outname = bfd_get_section_name (output_bfd,
2297 s->output_section);
2298 target = bfd_get_section_by_name (output_bfd, outname + 5);
297a4f1a
ILT
2299 if (target != NULL
2300 && (target->flags & SEC_READONLY) != 0)
2301 reltext = true;
2302
2303 if (strcmp(name, ".rela.plt") == 0)
2304 relplt = true;
2305
2306 /* We use the reloc_count field as a counter if we need
2307 to copy relocs into the output file. */
2308 s->reloc_count = 0;
2309 }
2310 }
297a4f1a
ILT
2311 else if (strcmp (name, ".plt") != 0)
2312 {
ff12f303 2313 /* It's not one of our dynamic sections, so don't allocate space. */
297a4f1a
ILT
2314 continue;
2315 }
2316
2317 if (strip)
ff12f303
ILT
2318 elf64_alpha_strip_section_from_output (s);
2319 else
297a4f1a 2320 {
ff12f303
ILT
2321 /* Allocate memory for the section contents. */
2322 s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
2323 if (s->contents == NULL && s->_raw_size != 0)
2324 return false;
2325 }
2326 }
297a4f1a 2327
ff12f303
ILT
2328 /* If we are generating a shared library, we generate a section
2329 symbol for each output section. These are local symbols, which
2330 means that they must come first in the dynamic symbol table.
2331 That means we must increment the dynamic symbol index of every
2332 other dynamic symbol. */
2333 if (info->shared)
2334 {
2335 long c[2], i;
2336 asection *p;
297a4f1a 2337
ff12f303
ILT
2338 c[0] = 0;
2339 c[1] = bfd_count_sections (output_bfd);
297a4f1a 2340
ff12f303
ILT
2341 elf_hash_table (info)->dynsymcount += c[1];
2342 elf_link_hash_traverse (elf_hash_table(info),
2343 elf64_alpha_adjust_dynindx,
2344 (PTR) c);
2345
2346 for (i = 1, p = output_bfd->sections;
2347 p != NULL;
2348 p = p->next, i++)
2349 {
2350 elf_section_data (p)->dynindx = i;
2351 /* These symbols will have no names, so we don't need to
2352 fiddle with dynstr_index. */
2353 }
297a4f1a
ILT
2354 }
2355
2356 if (elf_hash_table (info)->dynamic_sections_created)
2357 {
2358 /* Add some entries to the .dynamic section. We fill in the
2359 values later, in elf64_alpha_finish_dynamic_sections, but we
2360 must add the entries now so that we get the correct size for
2361 the .dynamic section. The DT_DEBUG entry is filled in by the
2362 dynamic linker and used by the debugger. */
2363 if (!info->shared)
2364 {
2365 if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
2366 return false;
2367 }
2368
2369 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
2370 return false;
2371
2372 if (relplt)
2373 {
2374 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2375 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2376 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
2377 return false;
2378 }
2379
2380 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
2381 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
2382 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
2383 sizeof(Elf64_External_Rela)))
2384 return false;
2385
2386 if (reltext)
2387 {
2388 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
2389 return false;
2390 }
2391 }
2392
2393 return true;
2394}
2395
2396/* Increment the index of a dynamic symbol by a given amount. Called
2397 via elf_link_hash_traverse. */
2398
2399static boolean
2400elf64_alpha_adjust_dynindx (h, cparg)
2401 struct elf_link_hash_entry *h;
2402 PTR cparg;
2403{
2404 long *cp = (long *)cparg;
2405
2406 if (h->dynindx >= cp[0])
2407 h->dynindx += cp[1];
2408
2409 return true;
2410}
2411
2412/* Relocate an Alpha ELF section. */
2413
2414static boolean
2415elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
2416 contents, relocs, local_syms, local_sections)
2417 bfd *output_bfd;
2418 struct bfd_link_info *info;
2419 bfd *input_bfd;
2420 asection *input_section;
2421 bfd_byte *contents;
2422 Elf_Internal_Rela *relocs;
2423 Elf_Internal_Sym *local_syms;
2424 asection **local_sections;
2425{
2426 Elf_Internal_Shdr *symtab_hdr;
2427 Elf_Internal_Rela *rel;
2428 Elf_Internal_Rela *relend;
ff12f303
ILT
2429 asection *sec, *sgot, *srel, *srelgot;
2430 bfd *dynobj, *gotobj;
297a4f1a
ILT
2431 bfd_vma gp;
2432
ff12f303
ILT
2433 srelgot = srel = NULL;
2434 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2435 dynobj = elf_hash_table (info)->dynobj;
2436 if (dynobj)
2437 {
2438 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2439 }
297a4f1a
ILT
2440
2441 /* Find the gp value for this input bfd. */
2442 sgot = NULL;
2443 gp = 0;
ff12f303
ILT
2444 gotobj = alpha_elf_tdata (input_bfd)->gotobj;
2445 if (gotobj)
297a4f1a 2446 {
ff12f303
ILT
2447 sgot = alpha_elf_tdata (gotobj)->got;
2448 gp = _bfd_get_gp_value (gotobj);
297a4f1a
ILT
2449 if (gp == 0)
2450 {
2451 gp = (sgot->output_section->vma
2452 + sgot->output_offset
2453 + 0x8000);
ff12f303 2454 _bfd_set_gp_value (gotobj, gp);
297a4f1a
ILT
2455 }
2456 }
2457
2458 rel = relocs;
2459 relend = relocs + input_section->reloc_count;
2460 for (; rel < relend; rel++)
2461 {
2462 int r_type;
2463 reloc_howto_type *howto;
2464 unsigned long r_symndx;
ff12f303 2465 struct alpha_elf_link_hash_entry *h;
297a4f1a
ILT
2466 Elf_Internal_Sym *sym;
2467 bfd_vma relocation;
2468 bfd_vma addend;
2469 bfd_reloc_status_type r;
2470
2471 r_type = ELF64_R_TYPE(rel->r_info);
2472 if (r_type < 0 || r_type >= (int) R_ALPHA_max)
2473 {
2474 bfd_set_error (bfd_error_bad_value);
2475 return false;
2476 }
2477 howto = elf64_alpha_howto_table + r_type;
2478
2479 r_symndx = ELF64_R_SYM(rel->r_info);
2480
2481 if (info->relocateable)
2482 {
2483 /* This is a relocateable link. We don't have to change
2484 anything, unless the reloc is against a section symbol,
2485 in which case we have to adjust according to where the
2486 section symbol winds up in the output section. */
2487 if (r_symndx < symtab_hdr->sh_info)
2488 {
2489 sym = local_syms + r_symndx;
2490 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
2491 {
2492 sec = local_sections[r_symndx];
2493 rel->r_addend += sec->output_offset + sym->st_value;
2494 }
2495 }
2496
2497 continue;
2498 }
2499
2500 /* This is a final link. */
2501
2502 h = NULL;
2503 sym = NULL;
2504 sec = NULL;
2505
2506 if (r_symndx < symtab_hdr->sh_info)
2507 {
2508 sym = local_syms + r_symndx;
2509 sec = local_sections[r_symndx];
2510 relocation = (sec->output_section->vma
2511 + sec->output_offset
2512 + sym->st_value);
2513 }
2514 else
2515 {
ff12f303 2516 h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
297a4f1a 2517
ff12f303
ILT
2518 while (h->root.root.type == bfd_link_hash_indirect
2519 || h->root.root.type == bfd_link_hash_warning)
2520 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
297a4f1a 2521
ff12f303
ILT
2522 if (h->root.root.type == bfd_link_hash_defined
2523 || h->root.root.type == bfd_link_hash_defweak)
297a4f1a 2524 {
ff12f303 2525 sec = h->root.root.u.def.section;
297a4f1a
ILT
2526
2527#if rth_notdef
2528 if ((r_type == R_ALPHA_LITERAL
2529 && elf_hash_table(info)->dynamic_sections_created
2530 && (!info->shared
2531 || !info->symbolic
ff12f303 2532 || !(h->root.elf_link_hash_flags
297a4f1a
ILT
2533 & ELF_LINK_HASH_DEF_REGULAR)))
2534 || (info->shared
2535 && (!info->symbolic
ff12f303 2536 || !(h->root.elf_link_hash_flags
297a4f1a
ILT
2537 & ELF_LINK_HASH_DEF_REGULAR))
2538 && (input_section->flags & SEC_ALLOC)
2539 && (r_type == R_ALPHA_REFLONG
2540 || r_type == R_ALPHA_REFQUAD
2541 || r_type == R_ALPHA_LITERAL)))
2542 {
2543 /* In these cases, we don't need the relocation value.
2544 We check specially because in some obscure cases
2545 sec->output_section will be NULL. */
2546 relocation = 0;
2547 }
2548#else
2549 /* FIXME: Are not these obscure cases simply bugs? Let's
2550 get something working and come back to this. */
2551 if (sec->output_section == NULL)
2552 relocation = 0;
2553#endif /* rth_notdef */
2554 else
2555 {
ff12f303 2556 relocation = (h->root.root.u.def.value
297a4f1a
ILT
2557 + sec->output_section->vma
2558 + sec->output_offset);
2559 }
2560 }
ff12f303 2561 else if (h->root.root.type == bfd_link_hash_undefweak)
297a4f1a
ILT
2562 relocation = 0;
2563 else if (info->shared && !info->symbolic)
2564 relocation = 0;
2565 else
2566 {
2567 if (!((*info->callbacks->undefined_symbol)
ff12f303 2568 (info, h->root.root.root.string, input_bfd,
297a4f1a
ILT
2569 input_section, rel->r_offset)))
2570 return false;
2571 relocation = 0;
2572 }
2573 }
2574 addend = rel->r_addend;
2575
2576 switch (r_type)
2577 {
2578 case R_ALPHA_GPDISP:
2579 {
2580 bfd_byte *p_ldah, *p_lda;
2581
ff12f303
ILT
2582 BFD_ASSERT(gp != 0);
2583
297a4f1a
ILT
2584 relocation = (input_section->output_section->vma
2585 + input_section->output_offset
2586 + rel->r_offset);
2587
2588 p_ldah = contents + rel->r_offset - input_section->vma;
2589 p_lda = p_ldah + rel->r_addend;
2590
2591 r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
2592 p_ldah, p_lda);
2593 }
2594 break;
2595
2596 case R_ALPHA_OP_PUSH:
2597 case R_ALPHA_OP_STORE:
2598 case R_ALPHA_OP_PSUB:
2599 case R_ALPHA_OP_PRSHIFT:
ff12f303 2600 /* We hate these silly beasts. */
297a4f1a
ILT
2601 abort();
2602
2603 case R_ALPHA_LITERAL:
2604 {
ff12f303 2605 struct alpha_elf_got_entry *gotent;
297a4f1a 2606
297a4f1a 2607 BFD_ASSERT(sgot != NULL);
ff12f303
ILT
2608 BFD_ASSERT(gp != 0);
2609
297a4f1a
ILT
2610 if (h != NULL)
2611 {
ff12f303
ILT
2612 gotent = h->got_entries;
2613 while (gotent->gotobj != gotobj || gotent->addend != addend)
2614 gotent = gotent->next;
297a4f1a 2615
ff12f303
ILT
2616 /* Initialize the .got entry's value. */
2617 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
2618 {
2619 bfd_put_64 (output_bfd, relocation+addend,
2620 sgot->contents + gotent->got_offset);
297a4f1a 2621
ff12f303
ILT
2622 /* The dynamic relocations for the .got entries are
2623 done in finish_dynamic_symbol. */
297a4f1a 2624
ff12f303
ILT
2625 gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
2626 }
2627 }
2628 else
2629 {
2630 gotent = (alpha_elf_tdata(input_bfd)->
2631 local_got_entries[r_symndx]);
2632 while (gotent->addend != addend)
2633 gotent = gotent->next;
2634
2635 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
2636 {
2637 bfd_put_64 (output_bfd, relocation+addend,
2638 sgot->contents + gotent->got_offset);
2639
2640 /* Local got entries need RELATIVE relocs in shared
2641 libraries. */
2642 if (info->shared)
2643 {
2644 Elf_Internal_Rela outrel;
2645
2646 BFD_ASSERT(srelgot != NULL);
2647
2648 outrel.r_offset = (sgot->output_section->vma
2649 + sgot->output_offset
2650 + gotent->got_offset);
2651 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
2652 outrel.r_addend = 0;
2653
2654 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2655 ((Elf64_External_Rela *)
2656 srelgot->contents)
2657 + srelgot->reloc_count++);
2658 }
2659
2660 gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
2661 }
297a4f1a 2662 }
297a4f1a
ILT
2663
2664 /* Figure the gprel relocation. */
2665 addend = 0;
2666 relocation = (sgot->output_section->vma
2667 + sgot->output_offset
ff12f303 2668 + gotent->got_offset);
297a4f1a
ILT
2669 relocation -= gp;
2670 }
2671 /* overflow handled by _bfd_final_link_relocate */
2672 goto default_reloc;
ff12f303 2673
297a4f1a
ILT
2674 case R_ALPHA_GPREL32:
2675 BFD_ASSERT(gp != 0);
2676 relocation -= gp;
2677 goto default_reloc;
2678
2679 case R_ALPHA_BRADDR:
2680 case R_ALPHA_HINT:
2681 /* The regular PC-relative stuff measures from the start of
2682 the instruction rather than the end. */
2683 addend -= 4;
2684 goto default_reloc;
ff12f303 2685
297a4f1a
ILT
2686 case R_ALPHA_REFLONG:
2687 case R_ALPHA_REFQUAD:
ff12f303
ILT
2688 {
2689 Elf_Internal_Rela outrel;
297a4f1a 2690
ff12f303
ILT
2691 /* Careful here to remember RELATIVE relocations for global
2692 variables for symbolic shared objects. */
2693
2694 if (h && alpha_elf_dynamic_symbol_p (&h->root, info))
2695 {
2696 BFD_ASSERT(h->root.dynindx != -1);
2697 outrel.r_info = ELF64_R_INFO(h->root.dynindx, r_type);
2698 outrel.r_addend = addend;
2699 addend = 0, relocation = 0;
2700 }
2701 else if (info->shared)
2702 {
2703 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
2704 outrel.r_addend = 0;
2705 }
2706 else
2707 goto default_reloc;
2708
2709 if (!srel)
2710 {
2711 const char *name;
2712
2713 name = (bfd_elf_string_from_elf_section
2714 (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
2715 elf_section_data(input_section)->rel_hdr.sh_name));
2716 BFD_ASSERT(name != NULL);
2717
2718 srel = bfd_get_section_by_name (dynobj, name);
2719 BFD_ASSERT(srel != NULL);
2720 }
2721
2722 outrel.r_offset = (input_section->output_section->vma
2723 + input_section->output_offset
2724 + rel->r_offset);
2725
2726 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2727 ((Elf64_External_Rela *)
2728 srel->contents)
2729 + srel->reloc_count++);
2730 }
297a4f1a
ILT
2731 goto default_reloc;
2732
2733 default:
2734 default_reloc:
2735 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2736 contents, rel->r_offset, relocation,
2737 addend);
2738 break;
2739 }
2740
2741 switch (r)
2742 {
2743 case bfd_reloc_ok:
2744 break;
2745
2746 case bfd_reloc_overflow:
2747 {
2748 const char *name;
2749
2750 if (h != NULL)
ff12f303 2751 name = h->root.root.root.string;
297a4f1a
ILT
2752 else
2753 {
2754 name = (bfd_elf_string_from_elf_section
2755 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2756 if (name == NULL)
2757 return false;
2758 if (*name == '\0')
2759 name = bfd_section_name (input_bfd, sec);
2760 }
2761 if (! ((*info->callbacks->reloc_overflow)
2762 (info, name, howto->name, (bfd_vma) 0,
2763 input_bfd, input_section, rel->r_offset)))
2764 return false;
2765 }
2766 break;
2767
2768 default:
2769 case bfd_reloc_outofrange:
2770 abort ();
2771 }
2772 }
2773
2774 return true;
2775}
2776
2777/* Finish up dynamic symbol handling. We set the contents of various
2778 dynamic sections here. */
2779
2780static boolean
2781elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
2782 bfd *output_bfd;
2783 struct bfd_link_info *info;
2784 struct elf_link_hash_entry *h;
2785 Elf_Internal_Sym *sym;
2786{
2787 bfd *dynobj = elf_hash_table(info)->dynobj;
2788
2789 if (h->plt_offset != MINUS_ONE)
2790 {
ff12f303 2791 /* Fill in the .plt entry for this symbol. */
297a4f1a
ILT
2792 asection *splt, *sgot, *srel;
2793 Elf_Internal_Rela outrel;
2794 bfd_vma got_addr, plt_addr;
2795 bfd_vma plt_index;
ff12f303 2796 struct alpha_elf_got_entry *gotent;
297a4f1a 2797
ff12f303 2798 BFD_ASSERT (h->dynindx != -1);
297a4f1a 2799
ff12f303
ILT
2800 /* The first .got entry will be updated by the .plt with the
2801 address of the target function. */
2802 gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
2803 BFD_ASSERT (gotent && gotent->addend == 0);
297a4f1a 2804
ff12f303
ILT
2805 splt = bfd_get_section_by_name (dynobj, ".plt");
2806 BFD_ASSERT (splt != NULL);
2807 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
2808 BFD_ASSERT (srel != NULL);
2809 sgot = alpha_elf_tdata (gotent->gotobj)->got;
2810 BFD_ASSERT (sgot != NULL);
297a4f1a
ILT
2811
2812 got_addr = (sgot->output_section->vma
ff12f303
ILT
2813 + sgot->output_offset
2814 + gotent->got_offset);
297a4f1a
ILT
2815 plt_addr = (splt->output_section->vma
2816 + splt->output_offset
2817 + h->plt_offset);
2818
2819 plt_index = (h->plt_offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2820
2821 /* Fill in the entry in the procedure linkage table. */
2822 {
2823 unsigned insn1, insn2, insn3;
2824 long hi, lo;
2825
2826 /* decompose the reloc offset for the plt for ldah+lda */
2827 hi = plt_index * sizeof(Elf64_External_Rela);
2828 lo = ((hi & 0xffff) ^ 0x8000) - 0x8000;
2829 hi = (hi - lo) >> 16;
2830
2831 insn1 = PLT_ENTRY_WORD1 | (hi & 0xffff);
2832 insn2 = PLT_ENTRY_WORD2 | (lo & 0xffff);
2833 insn3 = PLT_ENTRY_WORD3 | ((-(h->plt_offset + 12) >> 2) & 0x1fffff);
ff12f303 2834
297a4f1a
ILT
2835 bfd_put_32 (output_bfd, insn1, splt->contents + h->plt_offset);
2836 bfd_put_32 (output_bfd, insn2, splt->contents + h->plt_offset + 4);
2837 bfd_put_32 (output_bfd, insn3, splt->contents + h->plt_offset + 8);
2838 }
2839
2840 /* Fill in the entry in the .rela.plt section. */
2841 outrel.r_offset = got_addr;
2842 outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
2843 outrel.r_addend = 0;
2844
2845 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2846 ((Elf64_External_Rela *)srel->contents
2847 + plt_index));
2848
2849 if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2850 {
2851 /* Mark the symbol as undefined, rather than as defined in the
2852 .plt section. Leave the value alone. */
2853 sym->st_shndx = SHN_UNDEF;
2854 }
2855
ff12f303
ILT
2856 /* Fill in the entries in the .got. */
2857 bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
2858
2859 /* Subsequent .got entries will continue to bounce through the .plt. */
2860 while ((gotent = gotent->next) != NULL)
2861 {
2862 sgot = alpha_elf_tdata(gotent->gotobj)->got;
2863 BFD_ASSERT(sgot != NULL);
2864 BFD_ASSERT(gotent->addend == 0);
2865
2866 bfd_put_64 (output_bfd, plt_addr,
2867 sgot->contents + gotent->got_offset);
2868 }
297a4f1a 2869 }
ff12f303 2870 else if (alpha_elf_dynamic_symbol_p (h, info))
297a4f1a 2871 {
ff12f303
ILT
2872 /* Fill in the dynamic relocations for this symbol's .got entries. */
2873 asection *srel;
297a4f1a 2874 Elf_Internal_Rela outrel;
ff12f303 2875 struct alpha_elf_got_entry *gotent;
297a4f1a 2876
297a4f1a
ILT
2877 srel = bfd_get_section_by_name (dynobj, ".rela.got");
2878 BFD_ASSERT (srel != NULL);
2879
ff12f303
ILT
2880 outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_GLOB_DAT);
2881 for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
2882 gotent != NULL;
2883 gotent = gotent->next)
297a4f1a 2884 {
ff12f303
ILT
2885 asection *sgot = alpha_elf_tdata (gotent->gotobj)->got;
2886 outrel.r_offset = (sgot->output_section->vma
2887 + sgot->output_offset
2888 + gotent->got_offset);
2889 outrel.r_addend = gotent->addend;
2890
2891 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
2892 ((Elf64_External_Rela *)srel->contents
2893 + srel->reloc_count++));
297a4f1a 2894 }
297a4f1a
ILT
2895 }
2896
2897 /* Mark some specially defined symbols as absolute. */
2898 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2899 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2900 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2901 sym->st_shndx = SHN_ABS;
2902
2903 return true;
2904}
2905
2906/* Finish up the dynamic sections. */
2907
2908static boolean
2909elf64_alpha_finish_dynamic_sections (output_bfd, info)
2910 bfd *output_bfd;
2911 struct bfd_link_info *info;
2912{
2913 bfd *dynobj;
2914 asection *sdyn;
297a4f1a
ILT
2915
2916 dynobj = elf_hash_table (info)->dynobj;
2917 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2918
2919 if (elf_hash_table (info)->dynamic_sections_created)
2920 {
2921 asection *splt;
2922 Elf64_External_Dyn *dyncon, *dynconend;
2923
2924 splt = bfd_get_section_by_name (dynobj, ".plt");
2925 BFD_ASSERT (splt != NULL && sdyn != NULL);
2926
2927 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2928 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2929 for (; dyncon < dynconend; dyncon++)
2930 {
2931 Elf_Internal_Dyn dyn;
2932 const char *name;
2933 asection *s;
2934
2935 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2936
2937 switch (dyn.d_tag)
2938 {
2939 case DT_PLTGOT:
2940 name = ".plt";
2941 goto get_vma;
2942 case DT_PLTRELSZ:
2943 name = ".rela.plt";
2944 goto get_size;
2945 case DT_JMPREL:
2946 name = ".rela.plt";
2947 goto get_vma;
2948
2949 case DT_RELASZ:
2950 /* My interpretation of the TIS v1.1 ELF document indicates
2951 that RELASZ should not include JMPREL. This is not what
ff12f303
ILT
2952 the rest of the BFD does. It is, however, what the
2953 glibc ld.so wants. Do this fixup here until we found
297a4f1a
ILT
2954 out who is right. */
2955 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2956 if (s)
2957 {
2958 dyn.d_un.d_val -=
2959 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2960 }
2961 break;
2962
2963 get_vma:
2964 s = bfd_get_section_by_name (output_bfd, name);
2965 dyn.d_un.d_ptr = (s ? s->vma : 0);
2966 break;
2967
2968 get_size:
2969 s = bfd_get_section_by_name (output_bfd, name);
2970 dyn.d_un.d_val =
2971 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
2972 break;
2973 }
2974
2975 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2976 }
2977
2978 /* Initialize the PLT0 entry */
2979 if (splt->_raw_size > 0)
2980 {
2981 bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
2982 bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
2983 bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
2984 bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
ff12f303 2985
297a4f1a
ILT
2986 /* The next two words will be filled in by ld.so */
2987 bfd_put_64 (output_bfd, 0, splt->contents + 16);
2988 bfd_put_64 (output_bfd, 0, splt->contents + 24);
2989
ff12f303 2990 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
297a4f1a
ILT
2991 PLT_HEADER_SIZE;
2992 }
2993 }
2994
297a4f1a
ILT
2995 if (info->shared)
2996 {
2997 asection *sdynsym;
2998 asection *s;
2999 Elf_Internal_Sym sym;
3000
3001 /* Set up the section symbols for the output sections. */
3002
3003 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
3004 BFD_ASSERT (sdynsym != NULL);
3005
3006 sym.st_size = 0;
3007 sym.st_name = 0;
3008 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3009 sym.st_other = 0;
3010
3011 for (s = output_bfd->sections; s != NULL; s = s->next)
3012 {
3013 int indx;
3014
3015 sym.st_value = s->vma;
3016
3017 indx = elf_section_data (s)->this_idx;
3018 BFD_ASSERT (indx > 0);
3019 sym.st_shndx = indx;
3020
3021 bfd_elf64_swap_symbol_out (output_bfd, &sym,
3022 (PTR) (((Elf64_External_Sym *)
3023 sdynsym->contents)
3024 + elf_section_data (s)->dynindx));
3025 }
3026
3027 /* Set the sh_info field of the output .dynsym section to the
3028 index of the first global symbol. */
3029 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
3030 bfd_count_sections (output_bfd) + 1;
3031 }
3032
3033 return true;
3034}
3035
3036/* We need to use a special link routine to handle the .reginfo and
3037 the .mdebug sections. We need to merge all instances of these
3038 sections together, not write them all out sequentially. */
3039
3040static boolean
3041elf64_alpha_final_link (abfd, info)
3042 bfd *abfd;
3043 struct bfd_link_info *info;
3044{
3045 asection *o;
3046 struct bfd_link_order *p;
3047 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3048 struct ecoff_debug_info debug;
3049 const struct ecoff_debug_swap *swap
3050 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3051 HDRR *symhdr = &debug.symbolic_header;
3052 PTR mdebug_handle = NULL;
3053
3054 /* Go through the sections and collect the .reginfo and .mdebug
3055 information. */
3056 reginfo_sec = NULL;
3057 mdebug_sec = NULL;
3058 gptab_data_sec = NULL;
3059 gptab_bss_sec = NULL;
3060 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3061 {
3062#ifdef ERIC_neverdef
3063 if (strcmp (o->name, ".reginfo") == 0)
3064 {
3065 memset (&reginfo, 0, sizeof reginfo);
3066
3067 /* We have found the .reginfo section in the output file.
3068 Look through all the link_orders comprising it and merge
3069 the information together. */
3070 for (p = o->link_order_head;
3071 p != (struct bfd_link_order *) NULL;
3072 p = p->next)
3073 {
3074 asection *input_section;
3075 bfd *input_bfd;
3076 Elf64_External_RegInfo ext;
3077 Elf64_RegInfo sub;
3078
3079 if (p->type != bfd_indirect_link_order)
3080 {
3081 if (p->type == bfd_fill_link_order)
3082 continue;
3083 abort ();
3084 }
3085
3086 input_section = p->u.indirect.section;
3087 input_bfd = input_section->owner;
3088
3089 /* The linker emulation code has probably clobbered the
3090 size to be zero bytes. */
3091 if (input_section->_raw_size == 0)
3092 input_section->_raw_size = sizeof (Elf64_External_RegInfo);
3093
3094 if (! bfd_get_section_contents (input_bfd, input_section,
3095 (PTR) &ext,
3096 (file_ptr) 0,
3097 sizeof ext))
3098 return false;
3099
3100 bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
3101
3102 reginfo.ri_gprmask |= sub.ri_gprmask;
3103 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3104 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3105 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3106 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3107
3108 /* ri_gp_value is set by the function
3109 alpha_elf_section_processing when the section is
3110 finally written out. */
3111
3112 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3113 elf_link_input_bfd ignores this section. */
3114 input_section->flags &=~ SEC_HAS_CONTENTS;
3115 }
3116
3117 /* Force the section size to the value we want. */
3118 o->_raw_size = sizeof (Elf64_External_RegInfo);
3119
3120 /* Skip this section later on (I don't think this currently
3121 matters, but someday it might). */
3122 o->link_order_head = (struct bfd_link_order *) NULL;
3123
3124 reginfo_sec = o;
3125 }
3126#endif
3127
3128 if (strcmp (o->name, ".mdebug") == 0)
3129 {
3130 struct extsym_info einfo;
3131
3132 /* We have found the .mdebug section in the output file.
3133 Look through all the link_orders comprising it and merge
3134 the information together. */
3135 symhdr->magic = swap->sym_magic;
3136 /* FIXME: What should the version stamp be? */
3137 symhdr->vstamp = 0;
3138 symhdr->ilineMax = 0;
3139 symhdr->cbLine = 0;
3140 symhdr->idnMax = 0;
3141 symhdr->ipdMax = 0;
3142 symhdr->isymMax = 0;
3143 symhdr->ioptMax = 0;
3144 symhdr->iauxMax = 0;
3145 symhdr->issMax = 0;
3146 symhdr->issExtMax = 0;
3147 symhdr->ifdMax = 0;
3148 symhdr->crfd = 0;
3149 symhdr->iextMax = 0;
3150
3151 /* We accumulate the debugging information itself in the
3152 debug_info structure. */
3153 debug.line = NULL;
3154 debug.external_dnr = NULL;
3155 debug.external_pdr = NULL;
3156 debug.external_sym = NULL;
3157 debug.external_opt = NULL;
3158 debug.external_aux = NULL;
3159 debug.ss = NULL;
3160 debug.ssext = debug.ssext_end = NULL;
3161 debug.external_fdr = NULL;
3162 debug.external_rfd = NULL;
3163 debug.external_ext = debug.external_ext_end = NULL;
3164
3165 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3166 if (mdebug_handle == (PTR) NULL)
3167 return false;
3168
3169 if (1)
3170 {
3171 asection *s;
3172 EXTR esym;
3173 bfd_vma last;
3174 unsigned int i;
3175 static const char * const name[] =
3176 {
3177 ".text", ".init", ".fini", ".data",
3178 ".rodata", ".sdata", ".sbss", ".bss"
3179 };
3180 static const int sc[] = { scText, scInit, scFini, scData,
3181 scRData, scSData, scSBss, scBss };
3182
3183 esym.jmptbl = 0;
3184 esym.cobol_main = 0;
3185 esym.weakext = 0;
3186 esym.reserved = 0;
3187 esym.ifd = ifdNil;
3188 esym.asym.iss = issNil;
3189 esym.asym.st = stLocal;
3190 esym.asym.reserved = 0;
3191 esym.asym.index = indexNil;
3192 for (i = 0; i < 8; i++)
3193 {
3194 esym.asym.sc = sc[i];
3195 s = bfd_get_section_by_name (abfd, name[i]);
3196 if (s != NULL)
3197 {
3198 esym.asym.value = s->vma;
3199 last = s->vma + s->_raw_size;
3200 }
3201 else
3202 esym.asym.value = last;
ff12f303 3203
297a4f1a
ILT
3204 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3205 name[i], &esym))
3206 return false;
3207 }
3208 }
3209
3210 for (p = o->link_order_head;
3211 p != (struct bfd_link_order *) NULL;
3212 p = p->next)
3213 {
3214 asection *input_section;
3215 bfd *input_bfd;
3216 const struct ecoff_debug_swap *input_swap;
3217 struct ecoff_debug_info input_debug;
3218 char *eraw_src;
3219 char *eraw_end;
3220
3221 if (p->type != bfd_indirect_link_order)
3222 {
3223 if (p->type == bfd_fill_link_order)
3224 continue;
3225 abort ();
3226 }
3227
3228 input_section = p->u.indirect.section;
3229 input_bfd = input_section->owner;
3230
3231 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
3232 || (get_elf_backend_data (input_bfd)
3233 ->elf_backend_ecoff_debug_swap) == NULL)
3234 {
3235 /* I don't know what a non ALPHA ELF bfd would be
3236 doing with a .mdebug section, but I don't really
3237 want to deal with it. */
3238 continue;
3239 }
3240
3241 input_swap = (get_elf_backend_data (input_bfd)
3242 ->elf_backend_ecoff_debug_swap);
3243
3244 BFD_ASSERT (p->size == input_section->_raw_size);
3245
3246 /* The ECOFF linking code expects that we have already
3247 read in the debugging information and set up an
3248 ecoff_debug_info structure, so we do that now. */
3249 if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
3250 &input_debug))
3251 return false;
3252
3253 if (! (bfd_ecoff_debug_accumulate
3254 (mdebug_handle, abfd, &debug, swap, input_bfd,
3255 &input_debug, input_swap, info)))
3256 return false;
3257
3258 /* Loop through the external symbols. For each one with
3259 interesting information, try to find the symbol in
3260 the linker global hash table and save the information
3261 for the output external symbols. */
3262 eraw_src = input_debug.external_ext;
3263 eraw_end = (eraw_src
3264 + (input_debug.symbolic_header.iextMax
3265 * input_swap->external_ext_size));
3266 for (;
3267 eraw_src < eraw_end;
3268 eraw_src += input_swap->external_ext_size)
3269 {
3270 EXTR ext;
3271 const char *name;
3272 struct alpha_elf_link_hash_entry *h;
3273
3274 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
3275 if (ext.asym.sc == scNil
3276 || ext.asym.sc == scUndefined
3277 || ext.asym.sc == scSUndefined)
3278 continue;
3279
3280 name = input_debug.ssext + ext.asym.iss;
3281 h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
3282 name, false, false, true);
3283 if (h == NULL || h->esym.ifd != -2)
3284 continue;
3285
3286 if (ext.ifd != -1)
3287 {
3288 BFD_ASSERT (ext.ifd
3289 < input_debug.symbolic_header.ifdMax);
3290 ext.ifd = input_debug.ifdmap[ext.ifd];
3291 }
3292
3293 h->esym = ext;
3294 }
3295
3296 /* Free up the information we just read. */
3297 free (input_debug.line);
3298 free (input_debug.external_dnr);
3299 free (input_debug.external_pdr);
3300 free (input_debug.external_sym);
3301 free (input_debug.external_opt);
3302 free (input_debug.external_aux);
3303 free (input_debug.ss);
3304 free (input_debug.ssext);
3305 free (input_debug.external_fdr);
3306 free (input_debug.external_rfd);
3307 free (input_debug.external_ext);
3308
3309 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3310 elf_link_input_bfd ignores this section. */
3311 input_section->flags &=~ SEC_HAS_CONTENTS;
3312 }
3313
3314#ifdef ERIC_neverdef
3315 if (info->shared)
3316 {
3317 /* Create .rtproc section. */
3318 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3319 if (rtproc_sec == NULL)
3320 {
ff12f303
ILT
3321 flagword flags = (SEC_HAS_CONTENTS
3322 | SEC_IN_MEMORY
3323 | SEC_LINKER_CREATED
297a4f1a
ILT
3324 | SEC_READONLY);
3325
3326 rtproc_sec = bfd_make_section (abfd, ".rtproc");
3327 if (rtproc_sec == NULL
3328 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
3329 || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
3330 return false;
3331 }
3332
3333 if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
3334 info, rtproc_sec, &debug))
3335 return false;
3336 }
3337#endif
3338
3339
3340 /* Build the external symbol information. */
3341 einfo.abfd = abfd;
3342 einfo.info = info;
3343 einfo.debug = &debug;
3344 einfo.swap = swap;
3345 einfo.failed = false;
3346 elf_link_hash_traverse (elf_hash_table (info),
3347 elf64_alpha_output_extsym,
3348 (PTR) &einfo);
3349 if (einfo.failed)
3350 return false;
3351
3352 /* Set the size of the .mdebug section. */
3353 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
3354
3355 /* Skip this section later on (I don't think this currently
3356 matters, but someday it might). */
3357 o->link_order_head = (struct bfd_link_order *) NULL;
3358
3359 mdebug_sec = o;
3360 }
3361
3362#ifdef ERIC_neverdef
3363 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
3364 {
3365 const char *subname;
3366 unsigned int c;
3367 Elf64_gptab *tab;
3368 Elf64_External_gptab *ext_tab;
3369 unsigned int i;
3370
3371 /* The .gptab.sdata and .gptab.sbss sections hold
3372 information describing how the small data area would
3373 change depending upon the -G switch. These sections
3374 not used in executables files. */
3375 if (! info->relocateable)
3376 {
3377 asection **secpp;
3378
3379 for (p = o->link_order_head;
3380 p != (struct bfd_link_order *) NULL;
3381 p = p->next)
3382 {
3383 asection *input_section;
3384
3385 if (p->type != bfd_indirect_link_order)
3386 {
3387 if (p->type == bfd_fill_link_order)
3388 continue;
3389 abort ();
3390 }
3391
3392 input_section = p->u.indirect.section;
3393
3394 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3395 elf_link_input_bfd ignores this section. */
3396 input_section->flags &=~ SEC_HAS_CONTENTS;
3397 }
3398
3399 /* Skip this section later on (I don't think this
3400 currently matters, but someday it might). */
3401 o->link_order_head = (struct bfd_link_order *) NULL;
3402
3403 /* Really remove the section. */
3404 for (secpp = &abfd->sections;
3405 *secpp != o;
3406 secpp = &(*secpp)->next)
3407 ;
3408 *secpp = (*secpp)->next;
3409 --abfd->section_count;
3410
3411 continue;
3412 }
3413
3414 /* There is one gptab for initialized data, and one for
3415 uninitialized data. */
3416 if (strcmp (o->name, ".gptab.sdata") == 0)
3417 gptab_data_sec = o;
3418 else if (strcmp (o->name, ".gptab.sbss") == 0)
3419 gptab_bss_sec = o;
3420 else
3421 {
3422 (*_bfd_error_handler)
3423 ("%s: illegal section name `%s'",
3424 bfd_get_filename (abfd), o->name);
3425 bfd_set_error (bfd_error_nonrepresentable_section);
3426 return false;
3427 }
3428
3429 /* The linker script always combines .gptab.data and
3430 .gptab.sdata into .gptab.sdata, and likewise for
3431 .gptab.bss and .gptab.sbss. It is possible that there is
3432 no .sdata or .sbss section in the output file, in which
3433 case we must change the name of the output section. */
3434 subname = o->name + sizeof ".gptab" - 1;
3435 if (bfd_get_section_by_name (abfd, subname) == NULL)
3436 {
3437 if (o == gptab_data_sec)
3438 o->name = ".gptab.data";
3439 else
3440 o->name = ".gptab.bss";
3441 subname = o->name + sizeof ".gptab" - 1;
3442 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
3443 }
3444
3445 /* Set up the first entry. */
3446 c = 1;
3447 tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
3448 if (tab == NULL)
3449 return false;
3450 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
3451 tab[0].gt_header.gt_unused = 0;
3452
3453 /* Combine the input sections. */
3454 for (p = o->link_order_head;
3455 p != (struct bfd_link_order *) NULL;
3456 p = p->next)
3457 {
3458 asection *input_section;
3459 bfd *input_bfd;
3460 bfd_size_type size;
3461 unsigned long last;
3462 bfd_size_type gpentry;
3463
3464 if (p->type != bfd_indirect_link_order)
3465 {
3466 if (p->type == bfd_fill_link_order)
3467 continue;
3468 abort ();
3469 }
3470
3471 input_section = p->u.indirect.section;
3472 input_bfd = input_section->owner;
3473
3474 /* Combine the gptab entries for this input section one
3475 by one. We know that the input gptab entries are
3476 sorted by ascending -G value. */
3477 size = bfd_section_size (input_bfd, input_section);
3478 last = 0;
3479 for (gpentry = sizeof (Elf64_External_gptab);
3480 gpentry < size;
3481 gpentry += sizeof (Elf64_External_gptab))
3482 {
3483 Elf64_External_gptab ext_gptab;
3484 Elf64_gptab int_gptab;
3485 unsigned long val;
3486 unsigned long add;
3487 boolean exact;
3488 unsigned int look;
3489
3490 if (! (bfd_get_section_contents
3491 (input_bfd, input_section, (PTR) &ext_gptab,
3492 gpentry, sizeof (Elf64_External_gptab))))
3493 {
3494 free (tab);
3495 return false;
3496 }
3497
3498 bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
3499 &int_gptab);
3500 val = int_gptab.gt_entry.gt_g_value;
3501 add = int_gptab.gt_entry.gt_bytes - last;
3502
3503 exact = false;
3504 for (look = 1; look < c; look++)
3505 {
3506 if (tab[look].gt_entry.gt_g_value >= val)
3507 tab[look].gt_entry.gt_bytes += add;
3508
3509 if (tab[look].gt_entry.gt_g_value == val)
3510 exact = true;
3511 }
3512
3513 if (! exact)
3514 {
3515 Elf64_gptab *new_tab;
3516 unsigned int max;
3517
3518 /* We need a new table entry. */
3519 new_tab = ((Elf64_gptab *)
3520 bfd_realloc ((PTR) tab,
3521 (c + 1) * sizeof (Elf64_gptab)));
3522 if (new_tab == NULL)
3523 {
3524 free (tab);
3525 return false;
3526 }
3527 tab = new_tab;
3528 tab[c].gt_entry.gt_g_value = val;
3529 tab[c].gt_entry.gt_bytes = add;
3530
3531 /* Merge in the size for the next smallest -G
3532 value, since that will be implied by this new
3533 value. */
3534 max = 0;
3535 for (look = 1; look < c; look++)
3536 {
3537 if (tab[look].gt_entry.gt_g_value < val
3538 && (max == 0
3539 || (tab[look].gt_entry.gt_g_value
3540 > tab[max].gt_entry.gt_g_value)))
3541 max = look;
3542 }
3543 if (max != 0)
3544 tab[c].gt_entry.gt_bytes +=
3545 tab[max].gt_entry.gt_bytes;
3546
3547 ++c;
3548 }
3549
3550 last = int_gptab.gt_entry.gt_bytes;
3551 }
3552
3553 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3554 elf_link_input_bfd ignores this section. */
3555 input_section->flags &=~ SEC_HAS_CONTENTS;
3556 }
3557
3558 /* The table must be sorted by -G value. */
3559 if (c > 2)
3560 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
3561
3562 /* Swap out the table. */
3563 ext_tab = ((Elf64_External_gptab *)
3564 bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
3565 if (ext_tab == NULL)
3566 {
3567 free (tab);
3568 return false;
3569 }
3570
3571 for (i = 0; i < c; i++)
3572 bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
3573 free (tab);
3574
3575 o->_raw_size = c * sizeof (Elf64_External_gptab);
3576 o->contents = (bfd_byte *) ext_tab;
3577
3578 /* Skip this section later on (I don't think this currently
3579 matters, but someday it might). */
3580 o->link_order_head = (struct bfd_link_order *) NULL;
3581 }
3582#endif
3583
3584 }
3585
3586 /* Invoke the regular ELF backend linker to do all the work. */
3587 if (! bfd_elf64_bfd_final_link (abfd, info))
3588 return false;
3589
3590 /* Now write out the computed sections. */
3591
ff12f303
ILT
3592 /* The .got subsections... */
3593 {
3594 bfd *i, *dynobj = elf_hash_table(info)->dynobj;
3595 for (i = alpha_elf_hash_table(info)->got_list;
3596 i != NULL;
3597 i = alpha_elf_tdata(i)->got_link_next)
3598 {
3599 asection *sgot;
3600
3601 /* elf_bfd_final_link already did everything in dynobj. */
3602 if (i == dynobj)
3603 continue;
3604
3605 sgot = alpha_elf_tdata(i)->got;
3606 if (! bfd_set_section_contents (abfd, sgot->output_section,
3607 sgot->contents, sgot->output_offset,
3608 sgot->_raw_size))
3609 return false;
3610 }
3611 }
3612
297a4f1a
ILT
3613#ifdef ERIC_neverdef
3614 if (reginfo_sec != (asection *) NULL)
3615 {
3616 Elf64_External_RegInfo ext;
3617
3618 bfd_alpha_elf64_swap_reginfo_out (abfd, &reginfo, &ext);
3619 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
3620 (file_ptr) 0, sizeof ext))
3621 return false;
3622 }
3623#endif
3624
3625 if (mdebug_sec != (asection *) NULL)
3626 {
3627 BFD_ASSERT (abfd->output_has_begun);
3628 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
3629 swap, info,
3630 mdebug_sec->filepos))
3631 return false;
3632
3633 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
3634 }
3635
3636 if (gptab_data_sec != (asection *) NULL)
3637 {
3638 if (! bfd_set_section_contents (abfd, gptab_data_sec,
3639 gptab_data_sec->contents,
3640 (file_ptr) 0,
3641 gptab_data_sec->_raw_size))
3642 return false;
3643 }
3644
3645 if (gptab_bss_sec != (asection *) NULL)
3646 {
3647 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
3648 gptab_bss_sec->contents,
3649 (file_ptr) 0,
3650 gptab_bss_sec->_raw_size))
3651 return false;
3652 }
3653
3654 return true;
3655}
3656\f
3657/* ECOFF swapping routines. These are used when dealing with the
3658 .mdebug section, which is in the ECOFF debugging format. Copied
3659 from elf32-mips.c. */
3660static const struct ecoff_debug_swap
3661elf64_alpha_ecoff_debug_swap =
3662{
3663 /* Symbol table magic number. */
3664 magicSym2,
3665 /* Alignment of debugging information. E.g., 4. */
3666 8,
3667 /* Sizes of external symbolic information. */
3668 sizeof (struct hdr_ext),
3669 sizeof (struct dnr_ext),
3670 sizeof (struct pdr_ext),
3671 sizeof (struct sym_ext),
3672 sizeof (struct opt_ext),
3673 sizeof (struct fdr_ext),
3674 sizeof (struct rfd_ext),
3675 sizeof (struct ext_ext),
3676 /* Functions to swap in external symbolic data. */
3677 ecoff_swap_hdr_in,
3678 ecoff_swap_dnr_in,
3679 ecoff_swap_pdr_in,
3680 ecoff_swap_sym_in,
3681 ecoff_swap_opt_in,
3682 ecoff_swap_fdr_in,
3683 ecoff_swap_rfd_in,
3684 ecoff_swap_ext_in,
3685 _bfd_ecoff_swap_tir_in,
3686 _bfd_ecoff_swap_rndx_in,
3687 /* Functions to swap out external symbolic data. */
3688 ecoff_swap_hdr_out,
3689 ecoff_swap_dnr_out,
3690 ecoff_swap_pdr_out,
3691 ecoff_swap_sym_out,
3692 ecoff_swap_opt_out,
3693 ecoff_swap_fdr_out,
3694 ecoff_swap_rfd_out,
3695 ecoff_swap_ext_out,
3696 _bfd_ecoff_swap_tir_out,
3697 _bfd_ecoff_swap_rndx_out,
3698 /* Function to read in symbolic data. */
3699 elf64_alpha_read_ecoff_info
3700};
3701\f
3702#define TARGET_LITTLE_SYM bfd_elf64_alpha_vec
3703#define TARGET_LITTLE_NAME "elf64-alpha"
3704#define ELF_ARCH bfd_arch_alpha
3705#define ELF_MACHINE_CODE EM_ALPHA
3706#define ELF_MAXPAGESIZE 0x100000
3707
3708#define bfd_elf64_bfd_link_hash_table_create \
3709 elf64_alpha_bfd_link_hash_table_create
3710
3711#define bfd_elf64_bfd_reloc_type_lookup \
3712 elf64_alpha_bfd_reloc_type_lookup
3713#define elf_info_to_howto \
3714 elf64_alpha_info_to_howto
3715
ff12f303
ILT
3716#define bfd_elf64_mkobject \
3717 elf64_alpha_mkobject
3718
297a4f1a 3719#define elf_backend_object_p \
ff12f303 3720 elf64_alpha_object_p
297a4f1a
ILT
3721#define elf_backend_section_from_shdr \
3722 elf64_alpha_section_from_shdr
3723#define elf_backend_fake_sections \
3724 elf64_alpha_fake_sections
3725#define elf_backend_additional_program_headers \
3726 elf64_alpha_additional_program_headers
3727
303e7257
ILT
3728#define bfd_elf64_bfd_is_local_label_name \
3729 elf64_alpha_is_local_label_name
297a4f1a
ILT
3730#define bfd_elf64_find_nearest_line \
3731 elf64_alpha_find_nearest_line
3732
3733#define elf_backend_check_relocs \
3734 elf64_alpha_check_relocs
3735#define elf_backend_create_dynamic_sections \
3736 elf64_alpha_create_dynamic_sections
3737#define elf_backend_adjust_dynamic_symbol \
3738 elf64_alpha_adjust_dynamic_symbol
ff12f303
ILT
3739#define elf_backend_always_size_sections \
3740 elf64_alpha_always_size_sections
297a4f1a
ILT
3741#define elf_backend_size_dynamic_sections \
3742 elf64_alpha_size_dynamic_sections
3743#define elf_backend_relocate_section \
3744 elf64_alpha_relocate_section
3745#define elf_backend_finish_dynamic_symbol \
3746 elf64_alpha_finish_dynamic_symbol
3747#define elf_backend_finish_dynamic_sections \
3748 elf64_alpha_finish_dynamic_sections
3749#define bfd_elf64_bfd_final_link \
3750 elf64_alpha_final_link
3751
3752#define elf_backend_ecoff_debug_swap \
3753 &elf64_alpha_ecoff_debug_swap
3754
3755/*
3756 * A few constants that determine how the .plt section is set up.
3757 */
3758#define elf_backend_want_got_plt 0
3759#define elf_backend_plt_readonly 0
3760#define elf_backend_want_plt_sym 1
3761
3762#include "elf64-target.h"
This page took 0.285995 seconds and 4 git commands to generate.