* elf64-alpha.c (elf64_alpha_relax_section): Use the proper section
[deliverable/binutils-gdb.git] / bfd / elf64-alpha.c
CommitLineData
ff12f303 1/* Alpha specific support for 64-bit ELF
69842d08 2 Copyright 1996, 1997, 1998 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 *));
69842d08
RH
105static boolean elf64_alpha_size_got_sections
106 PARAMS ((bfd *, struct bfd_link_info *));
303e7257
ILT
107static boolean elf64_alpha_always_size_sections
108 PARAMS ((bfd *, struct bfd_link_info *));
109static boolean elf64_alpha_calc_dynrel_sizes
110 PARAMS ((struct alpha_elf_link_hash_entry *, struct bfd_link_info *));
69842d08
RH
111static boolean elf64_alpha_add_symbol_hook
112 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
113 const char **, flagword *, asection **, bfd_vma *));
297a4f1a
ILT
114static boolean elf64_alpha_check_relocs
115 PARAMS((bfd *, struct bfd_link_info *, asection *sec,
116 const Elf_Internal_Rela *));
117static boolean elf64_alpha_adjust_dynamic_symbol
118 PARAMS((struct bfd_link_info *, struct elf_link_hash_entry *));
119static boolean elf64_alpha_size_dynamic_sections
120 PARAMS((bfd *, struct bfd_link_info *));
121static boolean elf64_alpha_adjust_dynindx
122 PARAMS((struct elf_link_hash_entry *, PTR));
123static boolean elf64_alpha_relocate_section
124 PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
125 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
126static boolean elf64_alpha_finish_dynamic_symbol
127 PARAMS((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
128 Elf_Internal_Sym *));
129static boolean elf64_alpha_finish_dynamic_sections
130 PARAMS((bfd *, struct bfd_link_info *));
131static boolean elf64_alpha_final_link
132 PARAMS((bfd *, struct bfd_link_info *));
133
134\f
297a4f1a
ILT
135struct alpha_elf_link_hash_entry
136{
137 struct elf_link_hash_entry root;
138
139 /* External symbol information. */
140 EXTR esym;
37d7888d 141
ff12f303
ILT
142 /* Cumulative flags for all the .got entries. */
143 int flags;
144
37d7888d 145 /* Contexts (LITUSE) in which a literal was referenced. */
ff12f303
ILT
146#define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
147#define ALPHA_ELF_LINK_HASH_LU_MEM 0x02
148#define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
149#define ALPHA_ELF_LINK_HASH_LU_FUNC 0x08
150
151 /* Used to implement multiple .got subsections. */
152 struct alpha_elf_got_entry
153 {
154 struct alpha_elf_got_entry *next;
155
156 /* which .got subsection? */
157 bfd *gotobj;
158
159 /* the addend in effect for this entry. */
160 bfd_vma addend;
161
162 /* the .got offset for this entry. */
163 int got_offset;
164
165 int flags;
166
167 /* An additional flag. */
168#define ALPHA_ELF_GOT_ENTRY_RELOCS_DONE 0x10
69842d08
RH
169
170 int use_count;
ff12f303
ILT
171 } *got_entries;
172
173 /* used to count non-got, non-plt relocations for delayed sizing
174 of relocation sections. */
175 struct alpha_elf_reloc_entry
176 {
177 struct alpha_elf_reloc_entry *next;
178
179 /* which .reloc section? */
180 asection *srel;
181
182 /* what kind of relocation? */
183 unsigned long rtype;
184
185 /* how many did we find? */
186 unsigned long count;
187 } *reloc_entries;
297a4f1a
ILT
188};
189
190/* Alpha ELF linker hash table. */
191
192struct alpha_elf_link_hash_table
193{
194 struct elf_link_hash_table root;
ff12f303
ILT
195
196 /* The head of a list of .got subsections linked through
197 alpha_elf_tdata(abfd)->got_link_next. */
198 bfd *got_list;
297a4f1a
ILT
199};
200
201/* Look up an entry in a Alpha ELF linker hash table. */
202
203#define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
204 ((struct alpha_elf_link_hash_entry *) \
205 elf_link_hash_lookup (&(table)->root, (string), (create), \
206 (copy), (follow)))
207
208/* Traverse a Alpha ELF linker hash table. */
209
210#define alpha_elf_link_hash_traverse(table, func, info) \
211 (elf_link_hash_traverse \
212 (&(table)->root, \
213 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
214 (info)))
215
216/* Get the Alpha ELF linker hash table from a link_info structure. */
217
218#define alpha_elf_hash_table(p) \
219 ((struct alpha_elf_link_hash_table *) ((p)->hash))
220
ff12f303
ILT
221/* Get the object's symbols as our own entry type. */
222
223#define alpha_elf_sym_hashes(abfd) \
224 ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
225
226/* Should we do dynamic things to this symbol? */
227
228#define alpha_elf_dynamic_symbol_p(h, info) \
303e7257 229 (((info)->shared && !(info)->symbolic && (h)->dynindx != -1) \
ff12f303
ILT
230 || (((h)->elf_link_hash_flags \
231 & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)) \
232 == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
233
297a4f1a
ILT
234/* Create an entry in a Alpha ELF linker hash table. */
235
236static struct bfd_hash_entry *
237elf64_alpha_link_hash_newfunc (entry, table, string)
238 struct bfd_hash_entry *entry;
239 struct bfd_hash_table *table;
240 const char *string;
241{
242 struct alpha_elf_link_hash_entry *ret =
243 (struct alpha_elf_link_hash_entry *) entry;
244
245 /* Allocate the structure if it has not already been allocated by a
246 subclass. */
247 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
248 ret = ((struct alpha_elf_link_hash_entry *)
249 bfd_hash_allocate (table,
250 sizeof (struct alpha_elf_link_hash_entry)));
251 if (ret == (struct alpha_elf_link_hash_entry *) NULL)
252 return (struct bfd_hash_entry *) ret;
253
254 /* Call the allocation method of the superclass. */
255 ret = ((struct alpha_elf_link_hash_entry *)
256 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
257 table, string));
258 if (ret != (struct alpha_elf_link_hash_entry *) NULL)
259 {
260 /* Set local fields. */
261 memset (&ret->esym, 0, sizeof (EXTR));
262 /* We use -2 as a marker to indicate that the information has
263 not been set. -1 means there is no associated ifd. */
264 ret->esym.ifd = -2;
37d7888d 265 ret->flags = 0;
ff12f303
ILT
266 ret->got_entries = NULL;
267 ret->reloc_entries = NULL;
297a4f1a
ILT
268 }
269
270 return (struct bfd_hash_entry *) ret;
271}
272
273/* Create a Alpha ELF linker hash table. */
274
275static struct bfd_link_hash_table *
276elf64_alpha_bfd_link_hash_table_create (abfd)
277 bfd *abfd;
278{
279 struct alpha_elf_link_hash_table *ret;
280
281 ret = ((struct alpha_elf_link_hash_table *)
282 bfd_zalloc (abfd, sizeof (struct alpha_elf_link_hash_table)));
283 if (ret == (struct alpha_elf_link_hash_table *) NULL)
284 return NULL;
285
286 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
287 elf64_alpha_link_hash_newfunc))
288 {
289 bfd_release (abfd, ret);
290 return NULL;
291 }
292
293 return &ret->root.root;
294}
295\f
ff12f303
ILT
296/* We have some private fields hanging off of the elf_tdata structure. */
297
298struct alpha_elf_obj_tdata
299{
300 struct elf_obj_tdata root;
301
302 /* For every input file, these are the got entries for that object's
303 local symbols. */
304 struct alpha_elf_got_entry ** local_got_entries;
297a4f1a 305
ff12f303
ILT
306 /* For every input file, this is the object that owns the got that
307 this input file uses. */
308 bfd *gotobj;
309
310 /* For every got, this is a linked list through the objects using this got */
311 bfd *in_got_link_next;
312
313 /* For every got, this is a link to the next got subsegment. */
314 bfd *got_link_next;
315
316 /* For every got, this is the section. */
317 asection *got;
318
319 /* For every got, this is it's total number of *entries*. */
320 int total_got_entries;
321
322 /* For every got, this is the sum of the number of *entries* required
323 to hold all of the member object's local got. */
324 int n_local_got_entries;
325};
326
327#define alpha_elf_tdata(abfd) \
328 ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
329
330static boolean
331elf64_alpha_mkobject (abfd)
332 bfd *abfd;
333{
334 abfd->tdata.any = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
335 if (abfd->tdata.any == NULL)
336 return false;
337 return true;
338}
3a13dd8f
RH
339
340static boolean
341elf64_alpha_object_p (abfd)
342 bfd *abfd;
343{
344 /* Allocate our special target data. */
345 struct alpha_elf_obj_tdata *new_tdata;
346 new_tdata = bfd_zalloc (abfd, sizeof (struct alpha_elf_obj_tdata));
347 if (new_tdata == NULL)
348 return false;
349 new_tdata->root = *abfd->tdata.elf_obj_data;
350 abfd->tdata.any = new_tdata;
351
352 /* Set the right machine number for an Alpha ELF file. */
353 return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
354}
ff12f303 355\f
297a4f1a
ILT
356/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
357 from smaller values. Start with zero, widen, *then* decrement. */
358#define MINUS_ONE (((bfd_vma)0) - 1)
359
360static reloc_howto_type elf64_alpha_howto_table[] =
361{
362 HOWTO (R_ALPHA_NONE, /* type */
363 0, /* rightshift */
364 0, /* size (0 = byte, 1 = short, 2 = long) */
365 8, /* bitsize */
366 true, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_dont, /* complain_on_overflow */
369 elf64_alpha_reloc_nil, /* special_function */
370 "NONE", /* name */
371 false, /* partial_inplace */
372 0, /* src_mask */
373 0, /* dst_mask */
374 true), /* pcrel_offset */
375
376 /* A 32 bit reference to a symbol. */
377 HOWTO (R_ALPHA_REFLONG, /* type */
378 0, /* rightshift */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
380 32, /* bitsize */
381 false, /* pc_relative */
382 0, /* bitpos */
383 complain_overflow_bitfield, /* complain_on_overflow */
384 0, /* special_function */
385 "REFLONG", /* name */
386 false, /* partial_inplace */
387 0xffffffff, /* src_mask */
388 0xffffffff, /* dst_mask */
389 false), /* pcrel_offset */
390
391 /* A 64 bit reference to a symbol. */
392 HOWTO (R_ALPHA_REFQUAD, /* type */
393 0, /* rightshift */
394 4, /* size (0 = byte, 1 = short, 2 = long) */
395 64, /* bitsize */
396 false, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_bitfield, /* complain_on_overflow */
399 0, /* special_function */
400 "REFQUAD", /* name */
401 false, /* partial_inplace */
402 MINUS_ONE, /* src_mask */
403 MINUS_ONE, /* dst_mask */
404 false), /* pcrel_offset */
405
406 /* A 32 bit GP relative offset. This is just like REFLONG except
407 that when the value is used the value of the gp register will be
408 added in. */
409 HOWTO (R_ALPHA_GPREL32, /* type */
410 0, /* rightshift */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
412 32, /* bitsize */
413 false, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_bitfield, /* complain_on_overflow */
416 0, /* special_function */
417 "GPREL32", /* name */
418 false, /* partial_inplace */
419 0xffffffff, /* src_mask */
420 0xffffffff, /* dst_mask */
421 false), /* pcrel_offset */
422
423 /* Used for an instruction that refers to memory off the GP register. */
424 HOWTO (R_ALPHA_LITERAL, /* type */
425 0, /* rightshift */
426 2, /* size (0 = byte, 1 = short, 2 = long) */
427 16, /* bitsize */
428 false, /* pc_relative */
429 0, /* bitpos */
430 complain_overflow_signed, /* complain_on_overflow */
431 0, /* special_function */
ff12f303 432 "ELF_LITERAL", /* name */
297a4f1a
ILT
433 false, /* partial_inplace */
434 0xffff, /* src_mask */
435 0xffff, /* dst_mask */
436 false), /* pcrel_offset */
437
ff12f303 438 /* This reloc only appears immediately following an ELF_LITERAL reloc.
297a4f1a
ILT
439 It identifies a use of the literal. The symbol index is special:
440 1 means the literal address is in the base register of a memory
441 format instruction; 2 means the literal address is in the byte
442 offset register of a byte-manipulation instruction; 3 means the
443 literal address is in the target register of a jsr instruction.
444 This does not actually do any relocation. */
445 HOWTO (R_ALPHA_LITUSE, /* type */
446 0, /* rightshift */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
448 32, /* bitsize */
449 false, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_dont, /* complain_on_overflow */
452 elf64_alpha_reloc_nil, /* special_function */
453 "LITUSE", /* name */
454 false, /* partial_inplace */
455 0, /* src_mask */
456 0, /* dst_mask */
457 false), /* pcrel_offset */
458
459 /* Load the gp register. This is always used for a ldah instruction
460 which loads the upper 16 bits of the gp register. The symbol
461 index of the GPDISP instruction is an offset in bytes to the lda
462 instruction that loads the lower 16 bits. The value to use for
463 the relocation is the difference between the GP value and the
464 current location; the load will always be done against a register
465 holding the current address.
466
ff12f303 467 NOTE: Unlike ECOFF, partial in-place relocation is not done. If
297a4f1a
ILT
468 any offset is present in the instructions, it is an offset from
469 the register to the ldah instruction. This lets us avoid any
470 stupid hackery like inventing a gp value to do partial relocation
471 against. Also unlike ECOFF, we do the whole relocation off of
472 the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
473 space consuming bit, that, since all the information was present
474 in the GPDISP_HI16 reloc. */
475 HOWTO (R_ALPHA_GPDISP, /* type */
476 16, /* rightshift */
477 2, /* size (0 = byte, 1 = short, 2 = long) */
478 16, /* bitsize */
479 false, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont, /* complain_on_overflow */
482 elf64_alpha_reloc_gpdisp, /* special_function */
483 "GPDISP", /* name */
484 false, /* partial_inplace */
485 0xffff, /* src_mask */
486 0xffff, /* dst_mask */
487 true), /* pcrel_offset */
488
489 /* A 21 bit branch. */
490 HOWTO (R_ALPHA_BRADDR, /* type */
491 2, /* rightshift */
492 2, /* size (0 = byte, 1 = short, 2 = long) */
493 21, /* bitsize */
494 true, /* pc_relative */
495 0, /* bitpos */
496 complain_overflow_signed, /* complain_on_overflow */
497 0, /* special_function */
498 "BRADDR", /* name */
499 false, /* partial_inplace */
500 0x1fffff, /* src_mask */
501 0x1fffff, /* dst_mask */
502 true), /* pcrel_offset */
503
504 /* A hint for a jump to a register. */
505 HOWTO (R_ALPHA_HINT, /* type */
506 2, /* rightshift */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
508 14, /* bitsize */
509 true, /* pc_relative */
510 0, /* bitpos */
511 complain_overflow_dont, /* complain_on_overflow */
512 0, /* special_function */
513 "HINT", /* name */
514 false, /* partial_inplace */
515 0x3fff, /* src_mask */
516 0x3fff, /* dst_mask */
517 true), /* pcrel_offset */
518
519 /* 16 bit PC relative offset. */
520 HOWTO (R_ALPHA_SREL16, /* type */
521 0, /* rightshift */
522 1, /* size (0 = byte, 1 = short, 2 = long) */
523 16, /* bitsize */
524 true, /* pc_relative */
525 0, /* bitpos */
526 complain_overflow_signed, /* complain_on_overflow */
527 0, /* special_function */
528 "SREL16", /* name */
529 false, /* partial_inplace */
530 0xffff, /* src_mask */
531 0xffff, /* dst_mask */
532 false), /* pcrel_offset */
533
534 /* 32 bit PC relative offset. */
535 HOWTO (R_ALPHA_SREL32, /* type */
536 0, /* rightshift */
537 2, /* size (0 = byte, 1 = short, 2 = long) */
538 32, /* bitsize */
539 true, /* pc_relative */
540 0, /* bitpos */
541 complain_overflow_signed, /* complain_on_overflow */
542 0, /* special_function */
543 "SREL32", /* name */
544 false, /* partial_inplace */
545 0xffffffff, /* src_mask */
546 0xffffffff, /* dst_mask */
547 false), /* pcrel_offset */
548
549 /* A 64 bit PC relative offset. */
550 HOWTO (R_ALPHA_SREL64, /* type */
551 0, /* rightshift */
552 4, /* size (0 = byte, 1 = short, 2 = long) */
553 64, /* bitsize */
554 true, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_signed, /* complain_on_overflow */
557 0, /* special_function */
558 "SREL64", /* name */
559 false, /* partial_inplace */
560 MINUS_ONE, /* src_mask */
561 MINUS_ONE, /* dst_mask */
562 false), /* pcrel_offset */
563
564 /* Push a value on the reloc evaluation stack. */
3a13dd8f
RH
565 /* Not implemented -- it's dumb. */
566 HOWTO (R_ALPHA_OP_PUSH, /* type */
297a4f1a
ILT
567 0, /* rightshift */
568 0, /* size (0 = byte, 1 = short, 2 = long) */
569 0, /* bitsize */
570 false, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_dont, /* complain_on_overflow */
ff12f303 573 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
574 "OP_PUSH", /* name */
575 false, /* partial_inplace */
576 0, /* src_mask */
577 0, /* dst_mask */
578 false), /* pcrel_offset */
579
580 /* Store the value from the stack at the given address. Store it in
581 a bitfield of size r_size starting at bit position r_offset. */
3a13dd8f
RH
582 /* Not implemented -- it's dumb. */
583 HOWTO (R_ALPHA_OP_STORE, /* type */
297a4f1a
ILT
584 0, /* rightshift */
585 4, /* size (0 = byte, 1 = short, 2 = long) */
586 64, /* bitsize */
587 false, /* pc_relative */
588 0, /* bitpos */
589 complain_overflow_dont, /* complain_on_overflow */
ff12f303 590 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
591 "OP_STORE", /* name */
592 false, /* partial_inplace */
593 0, /* src_mask */
594 MINUS_ONE, /* dst_mask */
595 false), /* pcrel_offset */
596
597 /* Subtract the reloc address from the value on the top of the
598 relocation stack. */
3a13dd8f
RH
599 /* Not implemented -- it's dumb. */
600 HOWTO (R_ALPHA_OP_PSUB, /* type */
297a4f1a
ILT
601 0, /* rightshift */
602 0, /* size (0 = byte, 1 = short, 2 = long) */
603 0, /* bitsize */
604 false, /* pc_relative */
605 0, /* bitpos */
606 complain_overflow_dont, /* complain_on_overflow */
ff12f303 607 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
608 "OP_PSUB", /* name */
609 false, /* partial_inplace */
610 0, /* src_mask */
611 0, /* dst_mask */
612 false), /* pcrel_offset */
613
614 /* Shift the value on the top of the relocation stack right by the
615 given value. */
3a13dd8f
RH
616 /* Not implemented -- it's dumb. */
617 HOWTO (R_ALPHA_OP_PRSHIFT, /* type */
297a4f1a
ILT
618 0, /* rightshift */
619 0, /* size (0 = byte, 1 = short, 2 = long) */
620 0, /* bitsize */
621 false, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_dont, /* complain_on_overflow */
ff12f303 624 elf64_alpha_reloc_bad, /* special_function */
297a4f1a
ILT
625 "OP_PRSHIFT", /* name */
626 false, /* partial_inplace */
627 0, /* src_mask */
628 0, /* dst_mask */
629 false), /* pcrel_offset */
630
3a13dd8f
RH
631 /* Change the value of GP used by +r_addend until the next GPVALUE or the
632 end of the input bfd. */
633 /* Not implemented -- it's dumb. */
634 HOWTO (R_ALPHA_GPVALUE,
635 0, /* rightshift */
636 0, /* size (0 = byte, 1 = short, 2 = long) */
637 0, /* bitsize */
638 false, /* pc_relative */
639 0, /* bitpos */
640 complain_overflow_dont, /* complain_on_overflow */
641 elf64_alpha_reloc_bad, /* special_function */
642 "GPVALUE", /* name */
643 false, /* partial_inplace */
644 0, /* src_mask */
645 0, /* dst_mask */
646 false), /* pcrel_offset */
647
648 /* The high 16 bits of the displacement from GP to the target. */
3a13dd8f
RH
649 HOWTO (R_ALPHA_GPRELHIGH,
650 0, /* rightshift */
69842d08
RH
651 2, /* size (0 = byte, 1 = short, 2 = long) */
652 16, /* bitsize */
3a13dd8f
RH
653 false, /* pc_relative */
654 0, /* bitpos */
69842d08 655 complain_overflow_signed, /* complain_on_overflow */
3a13dd8f
RH
656 elf64_alpha_reloc_bad, /* special_function */
657 "GPRELHIGH", /* name */
658 false, /* partial_inplace */
69842d08
RH
659 0xffff, /* src_mask */
660 0xffff, /* dst_mask */
3a13dd8f
RH
661 false), /* pcrel_offset */
662
663 /* The low 16 bits of the displacement from GP to the target. */
3a13dd8f
RH
664 HOWTO (R_ALPHA_GPRELLOW,
665 0, /* rightshift */
69842d08
RH
666 2, /* size (0 = byte, 1 = short, 2 = long) */
667 16, /* bitsize */
3a13dd8f
RH
668 false, /* pc_relative */
669 0, /* bitpos */
670 complain_overflow_dont, /* complain_on_overflow */
671 elf64_alpha_reloc_bad, /* special_function */
672 "GPRELLOW", /* name */
673 false, /* partial_inplace */
69842d08
RH
674 0xffff, /* src_mask */
675 0xffff, /* dst_mask */
3a13dd8f
RH
676 false), /* pcrel_offset */
677
678 /* A 16-bit displacement from the GP to the target. */
679 /* XXX: Not implemented. */
680 HOWTO (R_ALPHA_IMMED_GP_16,
681 0, /* rightshift */
69842d08
RH
682 2, /* size (0 = byte, 1 = short, 2 = long) */
683 16, /* bitsize */
3a13dd8f
RH
684 false, /* pc_relative */
685 0, /* bitpos */
69842d08
RH
686 complain_overflow_signed, /* complain_on_overflow */
687 0, /* special_function */
3a13dd8f
RH
688 "IMMED_GP_16", /* name */
689 false, /* partial_inplace */
69842d08
RH
690 0xffff, /* src_mask */
691 0xffff, /* dst_mask */
3a13dd8f
RH
692 false), /* pcrel_offset */
693
694 /* The high bits of a 32-bit displacement from the GP to the target; the
695 low bits are supplied in the subsequent R_ALPHA_IMMED_LO32 relocs. */
696 /* XXX: Not implemented. */
697 HOWTO (R_ALPHA_IMMED_GP_HI32,
698 0, /* rightshift */
699 0, /* size (0 = byte, 1 = short, 2 = long) */
700 0, /* bitsize */
701 false, /* pc_relative */
702 0, /* bitpos */
703 complain_overflow_dont, /* complain_on_overflow */
704 elf64_alpha_reloc_bad, /* special_function */
705 "IMMED_GP_HI32", /* name */
706 false, /* partial_inplace */
707 0, /* src_mask */
708 0, /* dst_mask */
709 false), /* pcrel_offset */
710
711 /* The high bits of a 32-bit displacement to the starting address of the
712 current section (the relocation target is ignored); the low bits are
713 supplied in the subsequent R_ALPHA_IMMED_LO32 relocs. */
714 /* XXX: Not implemented. */
715 HOWTO (R_ALPHA_IMMED_SCN_HI32,
716 0, /* rightshift */
717 0, /* size (0 = byte, 1 = short, 2 = long) */
718 0, /* bitsize */
719 false, /* pc_relative */
720 0, /* bitpos */
721 complain_overflow_dont, /* complain_on_overflow */
722 elf64_alpha_reloc_bad, /* special_function */
723 "IMMED_SCN_HI32", /* name */
724 false, /* partial_inplace */
725 0, /* src_mask */
726 0, /* dst_mask */
727 false), /* pcrel_offset */
728
729 /* The high bits of a 32-bit displacement from the previous br, bsr, jsr
730 or jmp insn (as tagged by a BRADDR or HINT reloc) to the target; the
731 low bits are supplied by subsequent R_ALPHA_IMMED_LO32 relocs. */
732 /* XXX: Not implemented. */
733 HOWTO (R_ALPHA_IMMED_BR_HI32,
734 0, /* rightshift */
735 0, /* size (0 = byte, 1 = short, 2 = long) */
736 0, /* bitsize */
737 false, /* pc_relative */
738 0, /* bitpos */
739 complain_overflow_dont, /* complain_on_overflow */
740 elf64_alpha_reloc_bad, /* special_function */
741 "IMMED_BR_HI32", /* name */
742 false, /* partial_inplace */
743 0, /* src_mask */
744 0, /* dst_mask */
745 false), /* pcrel_offset */
746
747 /* The low 16 bits of a displacement calculated in a previous HI32 reloc. */
748 /* XXX: Not implemented. */
749 HOWTO (R_ALPHA_IMMED_LO32,
750 0, /* rightshift */
751 0, /* size (0 = byte, 1 = short, 2 = long) */
752 0, /* bitsize */
753 false, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_dont, /* complain_on_overflow */
756 elf64_alpha_reloc_bad, /* special_function */
757 "IMMED_LO32", /* name */
758 false, /* partial_inplace */
759 0, /* src_mask */
760 0, /* dst_mask */
761 false), /* pcrel_offset */
762
297a4f1a 763 /* Misc ELF relocations. */
3a13dd8f
RH
764
765 /* A dynamic relocation to copy the target into our .dynbss section. */
766 /* Not generated, as all Alpha objects use PIC, so it is not needed. It
767 is present because every other ELF has one, but should not be used
768 because .dynbss is an ugly thing. */
297a4f1a
ILT
769 HOWTO (R_ALPHA_COPY,
770 0,
771 0,
772 0,
773 false,
774 0,
775 complain_overflow_dont,
776 bfd_elf_generic_reloc,
777 "COPY",
778 false,
779 0,
ff12f303 780 0,
297a4f1a
ILT
781 true),
782
3a13dd8f 783 /* A dynamic relocation for a .got entry. */
297a4f1a
ILT
784 HOWTO (R_ALPHA_GLOB_DAT,
785 0,
786 0,
787 0,
788 false,
789 0,
790 complain_overflow_dont,
791 bfd_elf_generic_reloc,
792 "GLOB_DAT",
793 false,
794 0,
ff12f303 795 0,
297a4f1a
ILT
796 true),
797
3a13dd8f 798 /* A dynamic relocation for a .plt entry. */
297a4f1a
ILT
799 HOWTO (R_ALPHA_JMP_SLOT,
800 0,
801 0,
802 0,
803 false,
804 0,
805 complain_overflow_dont,
806 bfd_elf_generic_reloc,
807 "JMP_SLOT",
808 false,
809 0,
810 0,
811 true),
812
3a13dd8f 813 /* A dynamic relocation to add the base of the DSO to a 64-bit field. */
297a4f1a
ILT
814 HOWTO (R_ALPHA_RELATIVE,
815 0,
816 0,
817 0,
818 false,
819 0,
820 complain_overflow_dont,
821 bfd_elf_generic_reloc,
ff12f303 822 "RELATIVE",
297a4f1a
ILT
823 false,
824 0,
825 0,
826 true)
827};
828
ff12f303
ILT
829/* A relocation function which doesn't do anything. */
830
297a4f1a
ILT
831static bfd_reloc_status_type
832elf64_alpha_reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
833 bfd *abfd;
834 arelent *reloc;
835 asymbol *sym;
836 PTR data;
837 asection *sec;
838 bfd *output_bfd;
839 char **error_message;
840{
841 if (output_bfd)
842 reloc->address += sec->output_offset;
843 return bfd_reloc_ok;
844}
845
ff12f303
ILT
846/* A relocation function used for an unsupported reloc. */
847
848static bfd_reloc_status_type
849elf64_alpha_reloc_bad (abfd, reloc, sym, data, sec, output_bfd, error_message)
850 bfd *abfd;
851 arelent *reloc;
852 asymbol *sym;
853 PTR data;
854 asection *sec;
855 bfd *output_bfd;
856 char **error_message;
857{
858 if (output_bfd)
859 reloc->address += sec->output_offset;
860 return bfd_reloc_notsupported;
861}
862
863/* Do the work of the GPDISP relocation. */
864
297a4f1a
ILT
865static bfd_reloc_status_type
866elf64_alpha_do_reloc_gpdisp (abfd, gpdisp, p_ldah, p_lda)
867 bfd *abfd;
868 bfd_vma gpdisp;
ff12f303
ILT
869 bfd_byte *p_ldah;
870 bfd_byte *p_lda;
297a4f1a
ILT
871{
872 bfd_reloc_status_type ret = bfd_reloc_ok;
873 bfd_vma addend;
874 unsigned long i_ldah, i_lda;
875
ff12f303
ILT
876 i_ldah = bfd_get_32 (abfd, p_ldah);
877 i_lda = bfd_get_32 (abfd, p_lda);
297a4f1a
ILT
878
879 /* Complain if the instructions are not correct. */
880 if (((i_ldah >> 26) & 0x3f) != 0x09
881 || ((i_lda >> 26) & 0x3f) != 0x08)
882 ret = bfd_reloc_dangerous;
883
884 /* Extract the user-supplied offset, mirroring the sign extensions
885 that the instructions perform. */
886 addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
887 addend = (addend ^ 0x80008000) - 0x80008000;
888
889 gpdisp += addend;
890
211b0be8
RH
891 if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
892 || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
297a4f1a
ILT
893 ret = bfd_reloc_overflow;
894
895 /* compensate for the sign extension again. */
896 i_ldah = ((i_ldah & 0xffff0000)
897 | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
898 i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
899
900 bfd_put_32 (abfd, i_ldah, p_ldah);
901 bfd_put_32 (abfd, i_lda, p_lda);
902
903 return ret;
904}
905
ff12f303
ILT
906/* The special function for the GPDISP reloc. */
907
297a4f1a
ILT
908static bfd_reloc_status_type
909elf64_alpha_reloc_gpdisp (abfd, reloc_entry, sym, data, input_section,
910 output_bfd, err_msg)
911 bfd *abfd;
912 arelent *reloc_entry;
913 asymbol *sym;
914 PTR data;
915 asection *input_section;
916 bfd *output_bfd;
917 char **err_msg;
918{
919 bfd_reloc_status_type ret;
920 bfd_vma gp, relocation;
921 bfd_byte *p_ldah, *p_lda;
922
923 /* Don't do anything if we're not doing a final link. */
924 if (output_bfd)
925 {
926 reloc_entry->address += input_section->output_offset;
927 return bfd_reloc_ok;
928 }
929
930 if (reloc_entry->address > input_section->_cooked_size ||
931 reloc_entry->address + reloc_entry->addend > input_section->_cooked_size)
932 return bfd_reloc_outofrange;
933
ff12f303 934 /* The gp used in the portion of the output object to which this
297a4f1a
ILT
935 input object belongs is cached on the input bfd. */
936 gp = _bfd_get_gp_value (abfd);
937
938 relocation = (input_section->output_section->vma
939 + input_section->output_offset
940 + reloc_entry->address);
941
ff12f303 942 p_ldah = (bfd_byte *) data + reloc_entry->address;
297a4f1a
ILT
943 p_lda = p_ldah + reloc_entry->addend;
944
945 ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
946
947 /* Complain if the instructions are not correct. */
948 if (ret == bfd_reloc_dangerous)
53d3ce37 949 *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
297a4f1a
ILT
950
951 return ret;
952}
953
297a4f1a
ILT
954/* A mapping from BFD reloc types to Alpha ELF reloc types. */
955
956struct elf_reloc_map
957{
958 bfd_reloc_code_real_type bfd_reloc_val;
959 int elf_reloc_val;
960};
961
962static const struct elf_reloc_map elf64_alpha_reloc_map[] =
963{
964 {BFD_RELOC_NONE, R_ALPHA_NONE},
965 {BFD_RELOC_32, R_ALPHA_REFLONG},
966 {BFD_RELOC_64, R_ALPHA_REFQUAD},
967 {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
968 {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
ff12f303 969 {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
297a4f1a
ILT
970 {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
971 {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
ff12f303
ILT
972 {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
973 {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
974 {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
975 {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
976 {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
297a4f1a
ILT
977};
978
979/* Given a BFD reloc type, return a HOWTO structure. */
980
981static reloc_howto_type *
982elf64_alpha_bfd_reloc_type_lookup (abfd, code)
983 bfd *abfd;
984 bfd_reloc_code_real_type code;
985{
986 const struct elf_reloc_map *i, *e;
987 i = e = elf64_alpha_reloc_map;
988 e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
989 for (; i != e; ++i)
990 {
991 if (i->bfd_reloc_val == code)
992 return &elf64_alpha_howto_table[i->elf_reloc_val];
993 }
994 return 0;
995}
996
997/* Given an Alpha ELF reloc type, fill in an arelent structure. */
998
999static void
1000elf64_alpha_info_to_howto (abfd, cache_ptr, dst)
1001 bfd *abfd;
1002 arelent *cache_ptr;
1003 Elf64_Internal_Rela *dst;
1004{
1005 unsigned r_type;
1006
1007 r_type = ELF64_R_TYPE(dst->r_info);
1008 BFD_ASSERT (r_type < (unsigned int) R_ALPHA_max);
1009 cache_ptr->howto = &elf64_alpha_howto_table[r_type];
1010}
1011\f
69842d08
RH
1012/* These functions do relaxation for Alpha ELF.
1013
1014 Currently I'm only handling what I can do with existing compiler
1015 and assembler support, which means no instructions are removed,
1016 though some may be nopped. At this time GCC does not emit enough
1017 information to do all of the relaxing that is possible. It will
1018 take some not small amount of work for that to happen.
1019
1020 There are a couple of interesting papers that I once read on this
1021 subject, that I cannot find references to at the moment, that
1022 related to Alpha in particular. They are by David Wall, then of
1023 DEC WRL. */
1024
1025#define OP_LDA 0x08
1026#define OP_LDAH 0x09
1027#define INSN_JSR 0x68004000
1028#define INSN_JSR_MASK 0xfc00c000
1029#define OP_LDQ 0x29
1030#define OP_BR 0x30
1031#define OP_BSR 0x34
1032#define INSN_UNOP 0x2fe00000
1033
1034struct alpha_relax_info
1035{
1036 bfd *abfd;
1037 asection *sec;
1038 bfd_byte *contents;
1039 Elf_Internal_Rela *relocs, *relend;
1040 struct bfd_link_info *link_info;
1041 boolean changed_contents;
1042 boolean changed_relocs;
1043 bfd_vma gp;
1044 bfd *gotobj;
1045 struct alpha_elf_link_hash_entry *h;
1046 struct alpha_elf_got_entry *gotent;
1047};
1048
1049static Elf_Internal_Rela * elf64_alpha_relax_with_lituse
1050 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1051 Elf_Internal_Rela *irel, Elf_Internal_Rela *irelend));
1052
1053static boolean elf64_alpha_relax_without_lituse
1054 PARAMS((struct alpha_relax_info *info, bfd_vma symval,
1055 Elf_Internal_Rela *irel));
1056
1057static bfd_vma elf64_alpha_relax_opt_call
1058 PARAMS((struct alpha_relax_info *info, bfd_vma symval));
1059
1060static boolean elf64_alpha_relax_section
1061 PARAMS((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
1062 boolean *again));
1063
1064static Elf_Internal_Rela *
211b0be8 1065elf64_alpha_find_reloc_at_ofs (rel, relend, offset, type)
69842d08
RH
1066 Elf_Internal_Rela *rel, *relend;
1067 bfd_vma offset;
1068 int type;
1069{
1070 while (rel < relend)
1071 {
1072 if (rel->r_offset == offset && ELF64_R_TYPE (rel->r_info) == type)
1073 return rel;
1074 ++rel;
1075 }
1076 return NULL;
1077}
1078
1079static Elf_Internal_Rela *
1080elf64_alpha_relax_with_lituse (info, symval, irel, irelend)
1081 struct alpha_relax_info *info;
1082 bfd_vma symval;
1083 Elf_Internal_Rela *irel, *irelend;
1084{
1085 Elf_Internal_Rela *urel;
1086 int flags, count, i;
1087 bfd_signed_vma disp;
1088 boolean fits16;
1089 boolean fits32;
1090 boolean lit_reused = false;
1091 boolean all_optimized = true;
1092 unsigned int lit_insn;
1093
1094 lit_insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1095 if (lit_insn >> 26 != OP_LDQ)
1096 {
1097 ((*_bfd_error_handler)
1098 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1099 bfd_get_filename (info->abfd), info->sec->name,
1100 (unsigned long)irel->r_offset));
1101 return irel;
1102 }
1103
1104 /* Summarize how this particular LITERAL is used. */
1105 for (urel = irel+1, flags = count = 0; urel < irelend; ++urel, ++count)
1106 {
1107 if (ELF64_R_TYPE (urel->r_info) != R_ALPHA_LITUSE)
1108 break;
1109 if (urel->r_addend >= 0 && urel->r_addend <= 3)
1110 flags |= 1 << urel->r_addend;
1111 }
1112
1113 /* A little preparation for the loop... */
1114 disp = symval - info->gp;
1115 fits16 = (disp >= -(bfd_signed_vma)0x8000 && disp < 0x8000);
1116 fits32 = (disp >= -(bfd_signed_vma)0x80000000 && disp < 0x7fff8000);
1117
1118 for (urel = irel+1, i = 0; i < count; ++i, ++urel)
1119 {
1120 unsigned int insn;
1121 insn = bfd_get_32 (info->abfd, info->contents + urel->r_offset);
1122
1123 switch (urel->r_addend)
1124 {
1125 default: /* 0 = ADDRESS FORMAT */
1126 /* This type is really just a placeholder to note that all
1127 uses cannot be optimized, but to still allow some. */
1128 all_optimized = false;
1129 break;
1130
1131 case 1: /* MEM FORMAT */
1132 /* We can always optimize 16-bit displacements. */
1133 if (fits16)
1134 {
1135 /* FIXME: sanity check the insn for mem format with
1136 zero addend. */
1137
1138 /* Take the op code and dest from this insn, take the base
1139 register from the literal insn. Leave the offset alone. */
1140 insn = (insn & 0xffe00000) | (lit_insn & 0x001f0000);
1141 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1142 R_ALPHA_GPRELLOW);
1143 urel->r_addend = irel->r_addend;
1144 info->changed_relocs = true;
1145
1146 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1147 info->changed_contents = true;
1148 }
1149
1150 /* If all mem+byte, we can optimize 32-bit mem displacements. */
1151 else if (fits32 && !(flags & ~6))
1152 {
1153 /* FIXME: sanity check that lit insn Ra is mem insn Rb, and
1154 that mem_insn disp is zero. */
1155
1156 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1157 R_ALPHA_GPRELHIGH);
1158 lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
1159 bfd_put_32 (info->abfd, lit_insn,
1160 info->contents + irel->r_offset);
1161 lit_reused = true;
1162 info->changed_contents = true;
1163
1164 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1165 R_ALPHA_GPRELLOW);
1166 urel->r_addend = irel->r_addend;
1167 info->changed_relocs = true;
1168 }
1169 else
1170 all_optimized = false;
1171 break;
1172
1173 case 2: /* BYTE OFFSET FORMAT */
1174 /* We can always optimize byte instructions. */
1175
1176 /* FIXME: sanity check the insn for byte op. Check that the
1177 literal dest reg is indeed Rb in the byte insn. */
1178
1179 insn = (insn & ~0x001ff000) | ((symval & 7) << 13) | 0x1000;
1180
1181 urel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1182 urel->r_addend = 0;
1183 info->changed_relocs = true;
1184
1185 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1186 info->changed_contents = true;
1187 break;
1188
1189 case 3: /* CALL FORMAT */
1190 {
1191 /* If not zero, place to jump without needing pv. */
1192 bfd_vma optdest = elf64_alpha_relax_opt_call (info, symval);
1193 bfd_vma org = (info->sec->output_section->vma
1194 + info->sec->output_offset
1195 + urel->r_offset + 4);
1196 bfd_signed_vma odisp;
1197
1198 odisp = (optdest ? optdest : symval) - org;
1199 if (odisp >= -0x400000 && odisp < 0x400000)
1200 {
1201 Elf_Internal_Rela *xrel;
1202
1203 /* Preserve branch prediction call stack when possible. */
1204 if ((insn & INSN_JSR_MASK) == INSN_JSR)
1205 insn = (OP_BSR << 26) | (insn & 0x03e00000);
1206 else
1207 insn = (OP_BR << 26) | (insn & 0x03e00000);
1208
1209 urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
1210 R_ALPHA_BRADDR);
1211 urel->r_addend = irel->r_addend;
1212 info->changed_relocs = true;
1213
1214 /* Kill any HINT reloc that might exist for this insn. */
211b0be8 1215 xrel = (elf64_alpha_find_reloc_at_ofs
69842d08
RH
1216 (info->relocs, info->relend, urel->r_offset,
1217 R_ALPHA_HINT));
1218 if (xrel)
1219 xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1220
1221 if (optdest)
1222 urel->r_addend += optdest - symval;
1223 else
1224 all_optimized = false;
1225
1226 bfd_put_32 (info->abfd, insn, info->contents + urel->r_offset);
1227 info->changed_contents = true;
1228 }
1229 else
1230 all_optimized = false;
1231
1232 /* ??? If target gp == current gp we can eliminate the gp reload.
1233 This does depend on every place a gp could be reloaded will
1234 be, which currently happens for all code produced by gcc, but
1235 not necessarily by hand-coded assembly, or if sibling calls
1236 are enabled in gcc.
1237
1238 Perhaps conditionalize this on a flag being set in the target
1239 object file's header, and have gcc set it? */
1240 }
1241 break;
1242 }
1243 }
1244
1245 /* If all cases were optimized, we can reduce the use count on this
1246 got entry by one, possibly eliminating it. */
1247 if (all_optimized)
1248 {
1249 info->gotent->use_count -= 1;
1250 alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1251 if (!info->h)
1252 alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1253
1254 /* If the literal instruction is no longer needed (it may have been
1255 reused. We can eliminate it.
1256 ??? For now, I don't want to deal with compacting the section,
1257 so just nop it out. */
1258 if (!lit_reused)
1259 {
1260 irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
1261 info->changed_relocs = true;
1262
1263 bfd_put_32 (info->abfd, INSN_UNOP, info->contents + irel->r_offset);
1264 info->changed_contents = true;
1265 }
1266 }
1267
1268 return irel + count;
1269}
1270
1271static bfd_vma
1272elf64_alpha_relax_opt_call (info, symval)
1273 struct alpha_relax_info *info;
1274 bfd_vma symval;
1275{
1276 /* If the function has the same gp, and we can identify that the
1277 function does not use its function pointer, we can eliminate the
1278 address load.
1279
1280 ??? The .prologue [0,1] information is what we need. How do we
1281 get it out of the mdebug uglyness? What shall we do when we drop
1282 that crap for dwarf2?
1283
1284 For now, only consider the case in which there is an identifyable
1285 GP load in the first two words. We can then skip over that load. */
1286
1287 return 0;
1288}
1289
1290static boolean
1291elf64_alpha_relax_without_lituse (info, symval, irel)
1292 struct alpha_relax_info *info;
1293 bfd_vma symval;
1294 Elf_Internal_Rela *irel;
1295{
1296 unsigned int insn;
1297 bfd_signed_vma disp;
1298
1299 /* Get the instruction. */
1300 insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
1301
1302 if (insn >> 26 != OP_LDQ)
1303 {
1304 ((*_bfd_error_handler)
1305 ("%s: %s+0x%lx: warning: LITERAL relocation against unexpected insn",
1306 bfd_get_filename (info->abfd), info->sec->name,
1307 (unsigned long) irel->r_offset));
1308 return true;
1309 }
1310
1311 /* So we aren't told much. Do what we can with the address load and
1312 fake the rest. All of the optimizations here require that the
1313 offset from the GP fit in 16 bits. */
1314
1315 disp = symval - info->gp;
1316 if (disp < -0x8000 || disp >= 0x8000)
1317 return true;
1318
1319 /* On the LITERAL instruction itself, consider exchanging
1320 `ldq R,X(gp)' for `lda R,Y(gp)'. */
1321
1322 insn = (OP_LDA << 26) | (insn & 0x03ff0000);
1323 bfd_put_32 (info->abfd, insn, info->contents + irel->r_offset);
1324 info->changed_contents = true;
1325
1326 irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), R_ALPHA_GPRELLOW);
1327 info->changed_relocs = true;
1328
1329 /* Reduce the use count on this got entry by one, possibly
1330 eliminating it. */
1331 info->gotent->use_count -= 1;
1332 alpha_elf_tdata (info->gotent->gotobj)->total_got_entries -= 1;
1333 if (!info->h)
1334 alpha_elf_tdata (info->gotent->gotobj)->n_local_got_entries -= 1;
1335
1336 /* ??? Search forward through this basic block looking for insns
1337 that use the target register. Stop after an insn modifying the
1338 register is seen, or after a branch or call.
1339
1340 Any such memory load insn may be substituted by a load directly
1341 off the GP. This allows the memory load insn to be issued before
1342 the calculated GP register would otherwise be ready.
1343
1344 Any such jsr insn can be replaced by a bsr if it is in range.
1345
1346 This would mean that we'd have to _add_ relocations, the pain of
1347 which gives one pause. */
1348
1349 return true;
1350}
1351
1352static boolean
1353elf64_alpha_relax_section (abfd, sec, link_info, again)
1354 bfd *abfd;
1355 asection *sec;
1356 struct bfd_link_info *link_info;
1357 boolean *again;
1358{
1359 Elf_Internal_Shdr *symtab_hdr;
1360 Elf_Internal_Rela *internal_relocs;
1361 Elf_Internal_Rela *free_relocs = NULL;
1362 Elf_Internal_Rela *irel, *irelend;
1363 bfd_byte *free_contents = NULL;
1364 Elf64_External_Sym *extsyms = NULL;
1365 Elf64_External_Sym *free_extsyms = NULL;
1366 struct alpha_elf_got_entry **local_got_entries;
1367 struct alpha_relax_info info;
1368
1369 /* We are not currently changing any sizes, so only one pass. */
1370 *again = false;
1371
1372 if (link_info->relocateable
1373 || (sec->flags & SEC_RELOC) == 0
1374 || sec->reloc_count == 0)
1375 return true;
1376
1377 /* If this is the first time we have been called for this section,
1378 initialize the cooked size. */
1379 if (sec->_cooked_size == 0)
1380 sec->_cooked_size = sec->_raw_size;
1381
1382 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1383 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
1384
1385 /* Load the relocations for this section. */
1386 internal_relocs = (_bfd_elf64_link_read_relocs
1387 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1388 link_info->keep_memory));
1389 if (internal_relocs == NULL)
1390 goto error_return;
1391 if (! link_info->keep_memory)
1392 free_relocs = internal_relocs;
1393
1394 memset(&info, 0, sizeof(info));
1395 info.abfd = abfd;
1396 info.sec = sec;
1397 info.link_info = link_info;
1398 info.relocs = internal_relocs;
1399 info.relend = irelend = internal_relocs + sec->reloc_count;
1400
1401 /* Find the GP for this object. */
1402 info.gotobj = alpha_elf_tdata (abfd)->gotobj;
1403 if (info.gotobj)
1404 {
1405 asection *sgot = alpha_elf_tdata (info.gotobj)->got;
1406 info.gp = _bfd_get_gp_value (info.gotobj);
1407 if (info.gp == 0)
1408 {
1409 info.gp = (sgot->output_section->vma
1410 + sgot->output_offset
1411 + 0x8000);
1412 _bfd_set_gp_value (info.gotobj, info.gp);
1413 }
1414 }
1415
1416 for (irel = internal_relocs; irel < irelend; irel++)
1417 {
1418 bfd_vma symval;
1419 unsigned int insn;
1420
1421 if (ELF64_R_TYPE (irel->r_info) != (int) R_ALPHA_LITERAL)
1422 continue;
1423
1424 /* Get the section contents. */
1425 if (info.contents == NULL)
1426 {
1427 if (elf_section_data (sec)->this_hdr.contents != NULL)
1428 info.contents = elf_section_data (sec)->this_hdr.contents;
1429 else
1430 {
1431 info.contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1432 if (info.contents == NULL)
1433 goto error_return;
1434 free_contents = info.contents;
1435
1436 if (! bfd_get_section_contents (abfd, sec, info.contents,
1437 (file_ptr) 0, sec->_raw_size))
1438 goto error_return;
1439 }
1440 }
1441
1442 /* Read this BFD's symbols if we haven't done so already. */
1443 if (extsyms == NULL)
1444 {
1445 if (symtab_hdr->contents != NULL)
1446 extsyms = (Elf64_External_Sym *) symtab_hdr->contents;
1447 else
1448 {
1449 extsyms = ((Elf64_External_Sym *)
1450 bfd_malloc (symtab_hdr->sh_size));
1451 if (extsyms == NULL)
1452 goto error_return;
1453 free_extsyms = extsyms;
1454 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1455 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1456 != symtab_hdr->sh_size))
1457 goto error_return;
1458 }
1459 }
1460
1461 /* Get the value of the symbol referred to by the reloc. */
1462 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1463 {
1464 Elf_Internal_Sym isym;
986c3691 1465 asection *lsec;
69842d08
RH
1466
1467 /* A local symbol. */
1468 bfd_elf64_swap_symbol_in (abfd,
1469 extsyms + ELF64_R_SYM (irel->r_info),
1470 &isym);
986c3691
RH
1471 if (isym.st_shndx == SHN_UNDEF)
1472 lsec = bfd_und_section_ptr;
1473 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1474 lsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1475 else if (isym.st_shndx == SHN_ABS)
1476 lsec = bfd_abs_section_ptr;
1477 else if (isym.st_shndx == SHN_COMMON)
1478 lsec = bfd_com_section_ptr;
1479 else
1480 continue; /* who knows. */
69842d08
RH
1481
1482 info.h = NULL;
1483 info.gotent = local_got_entries[ELF64_R_SYM(irel->r_info)];
1484 symval = (isym.st_value
986c3691
RH
1485 + lsec->output_section->vma
1486 + lsec->output_offset);
69842d08
RH
1487 }
1488 else
1489 {
1490 unsigned long indx;
1491 struct alpha_elf_link_hash_entry *h;
1492 struct alpha_elf_got_entry *gotent;
1493
1494 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1495 h = alpha_elf_sym_hashes (abfd)[indx];
1496 BFD_ASSERT (h != NULL);
1497
1498 /* We can't do anthing with undefined or dynamic symbols. */
1499 if (h->root.root.type == bfd_link_hash_undefined
1500 || h->root.root.type == bfd_link_hash_undefweak
1501 || alpha_elf_dynamic_symbol_p (&h->root, link_info))
1502 continue;
1503
1504 /* Search for the got entry to be used by this relocation. */
1505 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
1506 if (gotent->gotobj == info.gotobj
1507 && gotent->addend == irel->r_addend)
1508 break;
1509
1510 info.h = h;
1511 info.gotent = gotent;
1512 symval = (h->root.root.u.def.value
1513 + h->root.root.u.def.section->output_section->vma
1514 + h->root.root.u.def.section->output_offset);
1515 }
1516 symval += irel->r_addend;
1517
1518 BFD_ASSERT(info.gotent != NULL);
1519
1520 /* If there exist LITUSE relocations immediately following, this
1521 opens up all sorts of interesting optimizations, because we
1522 now know every location that this address load is used. */
1523
1524 if (irel+1 < irelend && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
1525 {
1526 irel = elf64_alpha_relax_with_lituse (&info, symval, irel, irelend);
1527 if (irel == NULL)
1528 goto error_return;
1529 }
1530 else
1531 {
1532 if (!elf64_alpha_relax_without_lituse (&info, symval, irel))
1533 goto error_return;
1534 }
1535 }
1536
1537 if (!elf64_alpha_size_got_sections (abfd, link_info))
1538 return false;
1539
1540 if (info.changed_relocs)
1541 {
1542 elf_section_data (sec)->relocs = internal_relocs;
1543 }
1544 else if (free_relocs != NULL)
1545 {
1546 free (free_relocs);
1547 }
1548
1549 if (info.changed_contents)
1550 {
1551 elf_section_data (sec)->this_hdr.contents = info.contents;
1552 }
1553 else if (free_contents != NULL)
1554 {
1555 if (! link_info->keep_memory)
1556 free (free_contents);
1557 else
1558 {
1559 /* Cache the section contents for elf_link_input_bfd. */
1560 elf_section_data (sec)->this_hdr.contents = info.contents;
1561 }
1562 }
1563
1564 if (free_extsyms != NULL)
1565 {
1566 if (! link_info->keep_memory)
1567 free (free_extsyms);
1568 else
1569 {
1570 /* Cache the symbols for elf_link_input_bfd. */
1571 symtab_hdr->contents = extsyms;
1572 }
1573 }
1574
1575 return true;
1576
1577 error_return:
1578 if (free_relocs != NULL)
1579 free (free_relocs);
1580 if (free_contents != NULL)
1581 free (free_contents);
1582 if (free_extsyms != NULL)
1583 free (free_extsyms);
1584 return false;
1585}
1586\f
297a4f1a
ILT
1587/* PLT/GOT Stuff */
1588#define PLT_HEADER_SIZE 32
1589#define PLT_HEADER_WORD1 0xc3600000 /* br $27,.+4 */
1590#define PLT_HEADER_WORD2 0xa77b000c /* ldq $27,12($27) */
1591#define PLT_HEADER_WORD3 0x47ff041f /* nop */
1592#define PLT_HEADER_WORD4 0x6b7b0000 /* jmp $27,($27) */
1593
1594#define PLT_ENTRY_SIZE 12
3a13dd8f
RH
1595#define PLT_ENTRY_WORD1 0xc3800000 /* br $28, plt0 */
1596#define PLT_ENTRY_WORD2 0
1597#define PLT_ENTRY_WORD3 0
297a4f1a 1598
ff12f303 1599#define MAX_GOT_ENTRIES (64*1024 / 8)
297a4f1a
ILT
1600
1601#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
1602\f
ff12f303 1603/* Handle an Alpha specific section when reading an object file. This
297a4f1a 1604 is called when elfcode.h finds a section with an unknown type.
69842d08 1605 FIXME: We need to handle the SHF_ALPHA_GPREL flag, but I'm not sure
297a4f1a
ILT
1606 how to. */
1607
1608static boolean
1609elf64_alpha_section_from_shdr (abfd, hdr, name)
1610 bfd *abfd;
1611 Elf64_Internal_Shdr *hdr;
1612 char *name;
1613{
1614 asection *newsect;
1615
1616 /* There ought to be a place to keep ELF backend specific flags, but
1617 at the moment there isn't one. We just keep track of the
1618 sections by their name, instead. Fortunately, the ABI gives
1619 suggested names for all the MIPS specific sections, so we will
1620 probably get away with this. */
1621 switch (hdr->sh_type)
1622 {
1623 case SHT_ALPHA_DEBUG:
1624 if (strcmp (name, ".mdebug") != 0)
1625 return false;
1626 break;
1627#ifdef ERIC_neverdef
1628 case SHT_ALPHA_REGINFO:
1629 if (strcmp (name, ".reginfo") != 0
1630 || hdr->sh_size != sizeof (Elf64_External_RegInfo))
1631 return false;
1632 break;
1633#endif
1634 default:
1635 return false;
1636 }
1637
1638 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1639 return false;
1640 newsect = hdr->bfd_section;
1641
1642 if (hdr->sh_type == SHT_ALPHA_DEBUG)
1643 {
1644 if (! bfd_set_section_flags (abfd, newsect,
1645 (bfd_get_section_flags (abfd, newsect)
1646 | SEC_DEBUGGING)))
1647 return false;
1648 }
1649
1650#ifdef ERIC_neverdef
1651 /* For a .reginfo section, set the gp value in the tdata information
1652 from the contents of this section. We need the gp value while
1653 processing relocs, so we just get it now. */
1654 if (hdr->sh_type == SHT_ALPHA_REGINFO)
1655 {
1656 Elf64_External_RegInfo ext;
1657 Elf64_RegInfo s;
1658
1659 if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1660 (file_ptr) 0, sizeof ext))
1661 return false;
1662 bfd_alpha_elf64_swap_reginfo_in (abfd, &ext, &s);
1663 elf_gp (abfd) = s.ri_gp_value;
1664 }
1665#endif
1666
1667 return true;
1668}
1669
1670/* Set the correct type for an Alpha ELF section. We do this by the
1671 section name, which is a hack, but ought to work. */
1672
1673static boolean
1674elf64_alpha_fake_sections (abfd, hdr, sec)
1675 bfd *abfd;
1676 Elf64_Internal_Shdr *hdr;
1677 asection *sec;
1678{
1679 register const char *name;
1680
1681 name = bfd_get_section_name (abfd, sec);
1682
1683 if (strcmp (name, ".mdebug") == 0)
1684 {
1685 hdr->sh_type = SHT_ALPHA_DEBUG;
1686 /* In a shared object on Irix 5.3, the .mdebug section has an
1687 entsize of 0. FIXME: Does this matter? */
1688 if ((abfd->flags & DYNAMIC) != 0 )
1689 hdr->sh_entsize = 0;
1690 else
1691 hdr->sh_entsize = 1;
1692 }
1693#ifdef ERIC_neverdef
1694 else if (strcmp (name, ".reginfo") == 0)
1695 {
1696 hdr->sh_type = SHT_ALPHA_REGINFO;
1697 /* In a shared object on Irix 5.3, the .reginfo section has an
1698 entsize of 0x18. FIXME: Does this matter? */
1699 if ((abfd->flags & DYNAMIC) != 0)
1700 hdr->sh_entsize = sizeof (Elf64_External_RegInfo);
1701 else
1702 hdr->sh_entsize = 1;
1703
1704 /* Force the section size to the correct value, even if the
1705 linker thinks it is larger. The link routine below will only
1706 write out this much data for .reginfo. */
1707 hdr->sh_size = sec->_raw_size = sizeof (Elf64_External_RegInfo);
1708 }
1709 else if (strcmp (name, ".hash") == 0
1710 || strcmp (name, ".dynamic") == 0
1711 || strcmp (name, ".dynstr") == 0)
1712 {
1713 hdr->sh_entsize = 0;
1714 hdr->sh_info = SIZEOF_ALPHA_DYNSYM_SECNAMES;
1715 }
ff12f303 1716#endif
297a4f1a
ILT
1717 else if (strcmp (name, ".sdata") == 0
1718 || strcmp (name, ".sbss") == 0
1719 || strcmp (name, ".lit4") == 0
1720 || strcmp (name, ".lit8") == 0)
1721 hdr->sh_flags |= SHF_ALPHA_GPREL;
297a4f1a
ILT
1722
1723 return true;
1724}
1725
69842d08
RH
1726/* Hook called by the linker routine which adds symbols from an object
1727 file. We use it to put .comm items in .sbss, and not .bss. */
1728
1729static boolean
1730elf64_alpha_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1731 bfd *abfd;
1732 struct bfd_link_info *info;
1733 const Elf_Internal_Sym *sym;
1734 const char **namep;
1735 flagword *flagsp;
1736 asection **secp;
1737 bfd_vma *valp;
1738{
1739 if (sym->st_shndx == SHN_COMMON
1740 && !info->relocateable
1741 && sym->st_size <= bfd_get_gp_size (abfd))
1742 {
1743 /* Common symbols less than or equal to -G nn bytes are
1744 automatically put into .sbss. */
1745
211b0be8 1746 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
69842d08 1747
211b0be8 1748 if (scomm == NULL)
69842d08 1749 {
211b0be8
RH
1750 scomm = bfd_make_section (abfd, ".scommon");
1751 if (scomm == NULL
1752 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC | SEC_LOAD
1753 | SEC_IS_COMMON
1754 | SEC_LINKER_CREATED)))
69842d08
RH
1755 return false;
1756 }
1757
211b0be8 1758 if (bfd_get_section_alignment (abfd, scomm) < sym->st_value)
69842d08 1759 {
211b0be8 1760 if (!bfd_set_section_alignment (abfd, scomm, sym->st_value))
69842d08
RH
1761 return false;
1762 }
1763
211b0be8 1764 *secp = scomm;
69842d08
RH
1765 *valp = sym->st_size;
1766 }
1767
1768 return true;
1769}
1770
ff12f303
ILT
1771/* Return the number of additional phdrs we will need. */
1772
297a4f1a
ILT
1773static int
1774elf64_alpha_additional_program_headers (abfd)
1775 bfd *abfd;
1776{
1777 asection *s;
1778 int ret;
1779
1780 ret = 0;
1781
1782 s = bfd_get_section_by_name (abfd, ".reginfo");
1783 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1784 {
1785 /* We need a PT_ALPHA_REGINFO segment. */
1786 ++ret;
1787 }
1788
1789 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
1790 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
1791 {
1792 /* We need a PT_ALPHA_RTPROC segment. */
1793 ++ret;
1794 }
1795
1796 return ret;
1797}
1798
ff12f303
ILT
1799/* Create the .got section. */
1800
297a4f1a
ILT
1801static boolean
1802elf64_alpha_create_got_section(abfd, info)
1803 bfd *abfd;
1804 struct bfd_link_info *info;
1805{
1806 asection *s;
297a4f1a
ILT
1807
1808 if (bfd_get_section_by_name (abfd, ".got"))
1809 return true;
1810
ff12f303 1811 s = bfd_make_section (abfd, ".got");
297a4f1a
ILT
1812 if (s == NULL
1813 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
ff12f303
ILT
1814 | SEC_HAS_CONTENTS
1815 | SEC_IN_MEMORY
1816 | SEC_LINKER_CREATED))
297a4f1a
ILT
1817 || !bfd_set_section_alignment (abfd, s, 3))
1818 return false;
1819
ff12f303 1820 alpha_elf_tdata (abfd)->got = s;
297a4f1a
ILT
1821
1822 return true;
1823}
1824
ff12f303
ILT
1825/* Create all the dynamic sections. */
1826
297a4f1a
ILT
1827static boolean
1828elf64_alpha_create_dynamic_sections (abfd, info)
1829 bfd *abfd;
1830 struct bfd_link_info *info;
1831{
ff12f303 1832 asection *s;
297a4f1a
ILT
1833 struct elf_link_hash_entry *h;
1834
1835 /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
1836
1837 s = bfd_make_section (abfd, ".plt");
1838 if (s == NULL
1839 || ! bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
ff12f303
ILT
1840 | SEC_HAS_CONTENTS
1841 | SEC_IN_MEMORY
1842 | SEC_LINKER_CREATED
297a4f1a
ILT
1843 | SEC_CODE))
1844 || ! bfd_set_section_alignment (abfd, s, 3))
1845 return false;
1846
1847 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1848 .plt section. */
1849 h = NULL;
1850 if (! (_bfd_generic_link_add_one_symbol
1851 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1852 (bfd_vma) 0, (const char *) NULL, false,
1853 get_elf_backend_data (abfd)->collect,
1854 (struct bfd_link_hash_entry **) &h)))
1855 return false;
1856 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1857 h->type = STT_OBJECT;
1858
1859 if (info->shared
1860 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1861 return false;
1862
1863 s = bfd_make_section (abfd, ".rela.plt");
1864 if (s == NULL
1865 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
ff12f303
ILT
1866 | SEC_HAS_CONTENTS
1867 | SEC_IN_MEMORY
1868 | SEC_LINKER_CREATED
1869 | SEC_READONLY))
297a4f1a
ILT
1870 || ! bfd_set_section_alignment (abfd, s, 3))
1871 return false;
1872
ff12f303
ILT
1873 /* We may or may not have created a .got section for this object, but
1874 we definitely havn't done the rest of the work. */
1875
297a4f1a
ILT
1876 if (!elf64_alpha_create_got_section (abfd, info))
1877 return false;
1878
ff12f303
ILT
1879 s = bfd_make_section(abfd, ".rela.got");
1880 if (s == NULL
1881 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1882 | SEC_HAS_CONTENTS
1883 | SEC_IN_MEMORY
1884 | SEC_LINKER_CREATED
1885 | SEC_READONLY))
1886 || !bfd_set_section_alignment (abfd, s, 3))
1887 return false;
297a4f1a 1888
ff12f303
ILT
1889 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
1890 dynobj's .got section. We don't do this in the linker script
1891 because we don't want to define the symbol if we are not creating
1892 a global offset table. */
1893 h = NULL;
1894 if (!(_bfd_generic_link_add_one_symbol
1895 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL,
1896 alpha_elf_tdata(abfd)->got, (bfd_vma) 0, (const char *) NULL,
1897 false, get_elf_backend_data (abfd)->collect,
1898 (struct bfd_link_hash_entry **) &h)))
1899 return false;
1900 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1901 h->type = STT_OBJECT;
297a4f1a 1902
ff12f303
ILT
1903 if (info->shared
1904 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1905 return false;
1906
1907 elf_hash_table (info)->hgot = h;
1908
1909 return true;
1910}
297a4f1a
ILT
1911\f
1912/* Read ECOFF debugging information from a .mdebug section into a
1913 ecoff_debug_info structure. */
1914
1915static boolean
1916elf64_alpha_read_ecoff_info (abfd, section, debug)
1917 bfd *abfd;
1918 asection *section;
1919 struct ecoff_debug_info *debug;
1920{
1921 HDRR *symhdr;
1922 const struct ecoff_debug_swap *swap;
1923 char *ext_hdr = NULL;
1924
1925 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1926
1927 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
1928 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1929 goto error_return;
1930
1931 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1932 swap->external_hdr_size)
1933 == false)
1934 goto error_return;
1935
1936 symhdr = &debug->symbolic_header;
1937 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1938
1939 /* The symbolic header contains absolute file offsets and sizes to
1940 read. */
1941#define READ(ptr, offset, count, size, type) \
1942 if (symhdr->count == 0) \
1943 debug->ptr = NULL; \
1944 else \
1945 { \
1946 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
1947 if (debug->ptr == NULL) \
1948 goto error_return; \
1949 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1950 || (bfd_read (debug->ptr, size, symhdr->count, \
1951 abfd) != size * symhdr->count)) \
1952 goto error_return; \
1953 }
1954
1955 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1956 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1957 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1958 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1959 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1960 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1961 union aux_ext *);
1962 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1963 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1964 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1965 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1966 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1967#undef READ
1968
1969 debug->fdr = NULL;
1970 debug->adjust = NULL;
1971
1972 return true;
1973
1974 error_return:
1975 if (ext_hdr != NULL)
1976 free (ext_hdr);
1977 if (debug->line != NULL)
1978 free (debug->line);
1979 if (debug->external_dnr != NULL)
1980 free (debug->external_dnr);
1981 if (debug->external_pdr != NULL)
1982 free (debug->external_pdr);
1983 if (debug->external_sym != NULL)
1984 free (debug->external_sym);
1985 if (debug->external_opt != NULL)
1986 free (debug->external_opt);
1987 if (debug->external_aux != NULL)
1988 free (debug->external_aux);
1989 if (debug->ss != NULL)
1990 free (debug->ss);
1991 if (debug->ssext != NULL)
1992 free (debug->ssext);
1993 if (debug->external_fdr != NULL)
1994 free (debug->external_fdr);
1995 if (debug->external_rfd != NULL)
1996 free (debug->external_rfd);
1997 if (debug->external_ext != NULL)
1998 free (debug->external_ext);
1999 return false;
2000}
2001
2002/* Alpha ELF local labels start with '$'. */
2003
2004static boolean
303e7257 2005elf64_alpha_is_local_label_name (abfd, name)
297a4f1a 2006 bfd *abfd;
303e7257 2007 const char *name;
297a4f1a 2008{
303e7257 2009 return name[0] == '$';
297a4f1a
ILT
2010}
2011
2012/* Alpha ELF follows MIPS ELF in using a special find_nearest_line
2013 routine in order to handle the ECOFF debugging information. We
2014 still call this mips_elf_find_line because of the slot
2015 find_line_info in elf_obj_tdata is declared that way. */
2016
2017struct mips_elf_find_line
2018{
2019 struct ecoff_debug_info d;
2020 struct ecoff_find_line i;
2021};
2022
2023static boolean
2024elf64_alpha_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2025 functionname_ptr, line_ptr)
2026 bfd *abfd;
2027 asection *section;
2028 asymbol **symbols;
2029 bfd_vma offset;
2030 const char **filename_ptr;
2031 const char **functionname_ptr;
2032 unsigned int *line_ptr;
2033{
2034 asection *msec;
2035
2036 msec = bfd_get_section_by_name (abfd, ".mdebug");
2037 if (msec != NULL)
2038 {
2039 flagword origflags;
2040 struct mips_elf_find_line *fi;
2041 const struct ecoff_debug_swap * const swap =
2042 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2043
2044 /* If we are called during a link, alpha_elf_final_link may have
2045 cleared the SEC_HAS_CONTENTS field. We force it back on here
2046 if appropriate (which it normally will be). */
2047 origflags = msec->flags;
2048 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2049 msec->flags |= SEC_HAS_CONTENTS;
2050
2051 fi = elf_tdata (abfd)->find_line_info;
2052 if (fi == NULL)
2053 {
2054 bfd_size_type external_fdr_size;
2055 char *fraw_src;
2056 char *fraw_end;
2057 struct fdr *fdr_ptr;
2058
2059 fi = ((struct mips_elf_find_line *)
2060 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
2061 if (fi == NULL)
2062 {
2063 msec->flags = origflags;
2064 return false;
2065 }
2066
2067 if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
2068 {
2069 msec->flags = origflags;
2070 return false;
2071 }
2072
2073 /* Swap in the FDR information. */
2074 fi->d.fdr = ((struct fdr *)
2075 bfd_alloc (abfd,
2076 (fi->d.symbolic_header.ifdMax *
2077 sizeof (struct fdr))));
2078 if (fi->d.fdr == NULL)
2079 {
2080 msec->flags = origflags;
2081 return false;
2082 }
2083 external_fdr_size = swap->external_fdr_size;
2084 fdr_ptr = fi->d.fdr;
2085 fraw_src = (char *) fi->d.external_fdr;
2086 fraw_end = (fraw_src
2087 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2088 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2089 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2090
2091 elf_tdata (abfd)->find_line_info = fi;
2092
2093 /* Note that we don't bother to ever free this information.
2094 find_nearest_line is either called all the time, as in
2095 objdump -l, so the information should be saved, or it is
2096 rarely called, as in ld error messages, so the memory
2097 wasted is unimportant. Still, it would probably be a
2098 good idea for free_cached_info to throw it away. */
2099 }
2100
2101 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2102 &fi->i, filename_ptr, functionname_ptr,
2103 line_ptr))
2104 {
2105 msec->flags = origflags;
2106 return true;
2107 }
2108
2109 msec->flags = origflags;
2110 }
2111
2112 /* Fall back on the generic ELF find_nearest_line routine. */
2113
2114 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2115 filename_ptr, functionname_ptr,
2116 line_ptr);
2117}
2118\f
2119/* Structure used to pass information to alpha_elf_output_extsym. */
2120
2121struct extsym_info
2122{
2123 bfd *abfd;
2124 struct bfd_link_info *info;
2125 struct ecoff_debug_info *debug;
2126 const struct ecoff_debug_swap *swap;
2127 boolean failed;
2128};
2129
2130static boolean
2131elf64_alpha_output_extsym (h, data)
2132 struct alpha_elf_link_hash_entry *h;
2133 PTR data;
2134{
2135 struct extsym_info *einfo = (struct extsym_info *) data;
2136 boolean strip;
2137 asection *sec, *output_section;
2138
2139 if (h->root.indx == -2)
2140 strip = false;
2141 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2142 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2143 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2144 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2145 strip = true;
2146 else if (einfo->info->strip == strip_all
2147 || (einfo->info->strip == strip_some
2148 && bfd_hash_lookup (einfo->info->keep_hash,
2149 h->root.root.root.string,
2150 false, false) == NULL))
2151 strip = true;
2152 else
2153 strip = false;
2154
2155 if (strip)
2156 return true;
2157
2158 if (h->esym.ifd == -2)
2159 {
2160 h->esym.jmptbl = 0;
2161 h->esym.cobol_main = 0;
2162 h->esym.weakext = 0;
2163 h->esym.reserved = 0;
2164 h->esym.ifd = ifdNil;
2165 h->esym.asym.value = 0;
2166 h->esym.asym.st = stGlobal;
2167
2168 if (h->root.root.type != bfd_link_hash_defined
2169 && h->root.root.type != bfd_link_hash_defweak)
2170 h->esym.asym.sc = scAbs;
2171 else
2172 {
2173 const char *name;
2174
2175 sec = h->root.root.u.def.section;
2176 output_section = sec->output_section;
2177
2178 /* When making a shared library and symbol h is the one from
2179 the another shared library, OUTPUT_SECTION may be null. */
2180 if (output_section == NULL)
2181 h->esym.asym.sc = scUndefined;
2182 else
2183 {
2184 name = bfd_section_name (output_section->owner, output_section);
ff12f303 2185
297a4f1a
ILT
2186 if (strcmp (name, ".text") == 0)
2187 h->esym.asym.sc = scText;
2188 else if (strcmp (name, ".data") == 0)
2189 h->esym.asym.sc = scData;
2190 else if (strcmp (name, ".sdata") == 0)
2191 h->esym.asym.sc = scSData;
2192 else if (strcmp (name, ".rodata") == 0
2193 || strcmp (name, ".rdata") == 0)
2194 h->esym.asym.sc = scRData;
2195 else if (strcmp (name, ".bss") == 0)
2196 h->esym.asym.sc = scBss;
2197 else if (strcmp (name, ".sbss") == 0)
2198 h->esym.asym.sc = scSBss;
2199 else if (strcmp (name, ".init") == 0)
2200 h->esym.asym.sc = scInit;
2201 else if (strcmp (name, ".fini") == 0)
2202 h->esym.asym.sc = scFini;
2203 else
2204 h->esym.asym.sc = scAbs;
2205 }
2206 }
2207
2208 h->esym.asym.reserved = 0;
2209 h->esym.asym.index = indexNil;
2210 }
2211
2212 if (h->root.root.type == bfd_link_hash_common)
2213 h->esym.asym.value = h->root.root.u.c.size;
2214 else if (h->root.root.type == bfd_link_hash_defined
2215 || h->root.root.type == bfd_link_hash_defweak)
2216 {
2217 if (h->esym.asym.sc == scCommon)
2218 h->esym.asym.sc = scBss;
2219 else if (h->esym.asym.sc == scSCommon)
2220 h->esym.asym.sc = scSBss;
2221
2222 sec = h->root.root.u.def.section;
2223 output_section = sec->output_section;
2224 if (output_section != NULL)
2225 h->esym.asym.value = (h->root.root.u.def.value
2226 + sec->output_offset
2227 + output_section->vma);
2228 else
2229 h->esym.asym.value = 0;
2230 }
2231 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2232 {
2233 /* Set type and value for a symbol with a function stub. */
2234 h->esym.asym.st = stProc;
ff12f303 2235 sec = bfd_get_section_by_name (einfo->abfd, ".plt");
297a4f1a 2236 if (sec == NULL)
ff12f303 2237 h->esym.asym.value = 0;
297a4f1a 2238 else
ff12f303
ILT
2239 {
2240 output_section = sec->output_section;
2241 if (output_section != NULL)
2242 h->esym.asym.value = (h->root.plt_offset
2243 + sec->output_offset
2244 + output_section->vma);
2245 else
2246 h->esym.asym.value = 0;
2247 }
297a4f1a
ILT
2248#if 0 /* FIXME? */
2249 h->esym.ifd = 0;
2250#endif
ff12f303 2251 }
297a4f1a
ILT
2252
2253 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2254 h->root.root.root.string,
2255 &h->esym))
2256 {
2257 einfo->failed = true;
2258 return false;
2259 }
2260
2261 return true;
2262}
2263
2264/* FIXME: Create a runtime procedure table from the .mdebug section.
2265
2266static boolean
2267mips_elf_create_procedure_table (handle, abfd, info, s, debug)
2268 PTR handle;
2269 bfd *abfd;
2270 struct bfd_link_info *info;
2271 asection *s;
2272 struct ecoff_debug_info *debug;
ff12f303 2273*/
297a4f1a 2274\f
ff12f303
ILT
2275/* Handle dynamic relocations when doing an Alpha ELF link. */
2276
297a4f1a
ILT
2277static boolean
2278elf64_alpha_check_relocs (abfd, info, sec, relocs)
2279 bfd *abfd;
2280 struct bfd_link_info *info;
2281 asection *sec;
2282 const Elf_Internal_Rela *relocs;
2283{
2284 bfd *dynobj;
297a4f1a 2285 asection *sreloc;
ff12f303 2286 const char *rel_sec_name;
297a4f1a 2287 Elf_Internal_Shdr *symtab_hdr;
ff12f303
ILT
2288 struct alpha_elf_link_hash_entry **sym_hashes;
2289 struct alpha_elf_got_entry **local_got_entries;
297a4f1a 2290 const Elf_Internal_Rela *rel, *relend;
ff12f303 2291 int got_created;
297a4f1a
ILT
2292
2293 if (info->relocateable)
2294 return true;
2295
297a4f1a 2296 dynobj = elf_hash_table(info)->dynobj;
ff12f303
ILT
2297 if (dynobj == NULL)
2298 elf_hash_table(info)->dynobj = dynobj = abfd;
2299
2300 sreloc = NULL;
2301 rel_sec_name = NULL;
2302 symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
2303 sym_hashes = alpha_elf_sym_hashes(abfd);
2304 local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
2305 got_created = 0;
297a4f1a
ILT
2306
2307 relend = relocs + sec->reloc_count;
2308 for (rel = relocs; rel < relend; ++rel)
2309 {
ff12f303 2310 unsigned long r_symndx, r_type;
37d7888d 2311 struct alpha_elf_link_hash_entry *h;
297a4f1a 2312
37d7888d 2313 r_symndx = ELF64_R_SYM (rel->r_info);
297a4f1a
ILT
2314 if (r_symndx < symtab_hdr->sh_info)
2315 h = NULL;
2316 else
ff12f303
ILT
2317 {
2318 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
69842d08
RH
2319
2320 while (h->root.root.type == bfd_link_hash_indirect
2321 || h->root.root.type == bfd_link_hash_warning)
2322 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2323
ff12f303
ILT
2324 h->root.elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2325 }
2326 r_type = ELF64_R_TYPE (rel->r_info);
297a4f1a 2327
ff12f303 2328 switch (r_type)
297a4f1a
ILT
2329 {
2330 case R_ALPHA_LITERAL:
ff12f303
ILT
2331 {
2332 struct alpha_elf_got_entry *gotent;
2333 int flags = 0;
297a4f1a 2334
ff12f303
ILT
2335 if (h)
2336 {
2337 /* Search for and possibly create a got entry. */
2338 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
2339 if (gotent->gotobj == abfd &&
2340 gotent->addend == rel->r_addend)
2341 break;
2342
2343 if (!gotent)
2344 {
2345 gotent = ((struct alpha_elf_got_entry *)
2346 bfd_alloc (abfd,
2347 sizeof (struct alpha_elf_got_entry)));
2348 if (!gotent)
2349 return false;
297a4f1a 2350
ff12f303
ILT
2351 gotent->gotobj = abfd;
2352 gotent->addend = rel->r_addend;
2353 gotent->got_offset = -1;
2354 gotent->flags = 0;
69842d08 2355 gotent->use_count = 1;
297a4f1a 2356
ff12f303
ILT
2357 gotent->next = h->got_entries;
2358 h->got_entries = gotent;
37d7888d 2359
ff12f303
ILT
2360 alpha_elf_tdata (abfd)->total_got_entries++;
2361 }
69842d08
RH
2362 else
2363 gotent->use_count += 1;
ff12f303
ILT
2364 }
2365 else
2366 {
2367 /* This is a local .got entry -- record for merge. */
2368 if (!local_got_entries)
2369 {
2370 size_t size;
2371 size = (symtab_hdr->sh_info
2372 * sizeof (struct alpha_elf_got_entry *));
2373
2374 local_got_entries = ((struct alpha_elf_got_entry **)
2375 bfd_alloc (abfd, size));
2376 if (!local_got_entries)
2377 return false;
2378
2379 memset (local_got_entries, 0, size);
2380 alpha_elf_tdata (abfd)->local_got_entries =
2381 local_got_entries;
2382 }
2383
2384 for (gotent = local_got_entries[ELF64_R_SYM(rel->r_info)];
2385 gotent != NULL && gotent->addend != rel->r_addend;
2386 gotent = gotent->next)
2387 continue;
2388 if (!gotent)
2389 {
2390 gotent = ((struct alpha_elf_got_entry *)
2391 bfd_alloc (abfd,
2392 sizeof (struct alpha_elf_got_entry)));
2393 if (!gotent)
2394 return false;
2395
2396 gotent->gotobj = abfd;
2397 gotent->addend = rel->r_addend;
2398 gotent->got_offset = -1;
2399 gotent->flags = 0;
69842d08 2400 gotent->use_count = 1;
ff12f303
ILT
2401
2402 gotent->next = local_got_entries[ELF64_R_SYM(rel->r_info)];
2403 local_got_entries[ELF64_R_SYM(rel->r_info)] = gotent;
2404
2405 alpha_elf_tdata(abfd)->total_got_entries++;
2406 alpha_elf_tdata(abfd)->n_local_got_entries++;
2407 }
69842d08
RH
2408 else
2409 gotent->use_count += 1;
ff12f303
ILT
2410 }
2411
2412 /* Remember how this literal is used from its LITUSEs.
2413 This will be important when it comes to decide if we can
2414 create a .plt entry for a function symbol. */
2415 if (rel+1 < relend
2416 && ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE)
2417 {
2418 do
2419 {
2420 ++rel;
2421 if (rel->r_addend >= 1 && rel->r_addend <= 3)
2422 flags |= 1 << rel->r_addend;
2423 }
2424 while (rel+1 < relend &&
2425 ELF64_R_TYPE (rel[1].r_info) == R_ALPHA_LITUSE);
2426 }
2427 else
2428 {
2429 /* No LITUSEs -- presumably the address is not being
2430 loaded for nothing. */
2431 flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
2432 }
2433
2434 gotent->flags |= flags;
2435 if (h)
2436 {
2437 /* Make a guess as to whether a .plt entry will be needed. */
2438 if ((h->flags |= flags) == ALPHA_ELF_LINK_HASH_LU_FUNC)
2439 h->root.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2440 else
2441 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2442 }
2443 }
2444 /* FALLTHRU */
2445
2446 case R_ALPHA_GPDISP:
2447 case R_ALPHA_GPREL32:
69842d08
RH
2448 case R_ALPHA_GPRELHIGH:
2449 case R_ALPHA_GPRELLOW:
ff12f303
ILT
2450 /* We don't actually use the .got here, but the sections must
2451 be created before the linker maps input sections to output
2452 sections. */
2453 if (!got_created)
297a4f1a 2454 {
ff12f303
ILT
2455 if (!elf64_alpha_create_got_section (abfd, info))
2456 return false;
2457
2458 /* Make sure the object's gotobj is set to itself so
2459 that we default to every object with its own .got.
2460 We'll merge .gots later once we've collected each
2461 object's info. */
2462 alpha_elf_tdata(abfd)->gotobj = abfd;
2463
2464 got_created = 1;
2465 }
2466 break;
2467
2468 case R_ALPHA_SREL16:
2469 case R_ALPHA_SREL32:
2470 case R_ALPHA_SREL64:
2471 if (h == NULL)
2472 break;
2473 /* FALLTHRU */
2474
2475 case R_ALPHA_REFLONG:
2476 case R_ALPHA_REFQUAD:
2477 if (rel_sec_name == NULL)
2478 {
2479 rel_sec_name = (bfd_elf_string_from_elf_section
2480 (abfd, elf_elfheader(abfd)->e_shstrndx,
2481 elf_section_data(sec)->rel_hdr.sh_name));
2482 if (rel_sec_name == NULL)
2483 return false;
2484
2485 BFD_ASSERT (strncmp (rel_sec_name, ".rela", 5) == 0
2486 && strcmp (bfd_get_section_name (abfd, sec),
2487 rel_sec_name+5) == 0);
2488 }
2489
2490 /* We need to create the section here now whether we eventually
2491 use it or not so that it gets mapped to an output section by
2492 the linker. If not used, we'll kill it in
2493 size_dynamic_sections. */
2494 if (sreloc == NULL)
2495 {
2496 sreloc = bfd_get_section_by_name (dynobj, rel_sec_name);
2497 if (sreloc == NULL)
37d7888d 2498 {
ff12f303
ILT
2499 sreloc = bfd_make_section (dynobj, rel_sec_name);
2500 if (sreloc == NULL
2501 || !bfd_set_section_flags (dynobj, sreloc,
2502 (SEC_ALLOC|SEC_LOAD
2503 | SEC_HAS_CONTENTS
2504 | SEC_IN_MEMORY
2505 | SEC_LINKER_CREATED
2506 | SEC_READONLY))
2507 || !bfd_set_section_alignment (dynobj, sreloc, 3))
2508 return false;
37d7888d 2509 }
297a4f1a
ILT
2510 }
2511
ff12f303 2512 if (h)
297a4f1a 2513 {
ff12f303
ILT
2514 /* Since we havn't seen all of the input symbols yet, we
2515 don't know whether we'll actually need a dynamic relocation
2516 entry for this reloc. So make a record of it. Once we
2517 find out if this thing needs dynamic relocation we'll
2518 expand the relocation sections by the appropriate amount. */
297a4f1a 2519
ff12f303
ILT
2520 struct alpha_elf_reloc_entry *rent;
2521
2522 for (rent = h->reloc_entries; rent; rent = rent->next)
2523 if (rent->rtype == r_type && rent->srel == sreloc)
2524 break;
2525
2526 if (!rent)
2527 {
2528 rent = ((struct alpha_elf_reloc_entry *)
2529 bfd_alloc (abfd,
2530 sizeof (struct alpha_elf_reloc_entry)));
2531 if (!rent)
2532 return false;
2533
2534 rent->srel = sreloc;
2535 rent->rtype = r_type;
2536 rent->count = 1;
297a4f1a 2537
ff12f303
ILT
2538 rent->next = h->reloc_entries;
2539 h->reloc_entries = rent;
2540 }
2541 else
2542 rent->count++;
297a4f1a 2543 }
ff12f303
ILT
2544 else if (info->shared)
2545 {
2546 /* If this is a shared library, we need a RELATIVE reloc. */
2547 sreloc->_raw_size += sizeof (Elf64_External_Rela);
2548 }
2549 break;
2550 }
2551 }
2552
2553 return true;
2554}
2555
2556/* Adjust a symbol defined by a dynamic object and referenced by a
2557 regular object. The current definition is in some section of the
2558 dynamic object, but we're not including those sections. We have to
2559 change the definition to something the rest of the link can
2560 understand. */
2561
2562static boolean
2563elf64_alpha_adjust_dynamic_symbol (info, h)
2564 struct bfd_link_info *info;
2565 struct elf_link_hash_entry *h;
2566{
2567 bfd *dynobj;
2568 asection *s;
2569 struct alpha_elf_link_hash_entry *ah;
2570
2571 dynobj = elf_hash_table(info)->dynobj;
2572 ah = (struct alpha_elf_link_hash_entry *)h;
2573
2574 /* Now that we've seen all of the input symbols, finalize our decision
2575 about whether this symbol should get a .plt entry. */
2576
2577 if (h->root.type != bfd_link_hash_undefweak
2578 && alpha_elf_dynamic_symbol_p (h, info)
3a13dd8f
RH
2579 && ((h->type == STT_FUNC
2580 && !(ah->flags & ALPHA_ELF_LINK_HASH_LU_ADDR))
ff12f303
ILT
2581 || (h->type == STT_NOTYPE
2582 && ah->flags == ALPHA_ELF_LINK_HASH_LU_FUNC))
2583 /* Don't prevent otherwise valid programs from linking by attempting
2584 to create a new .got entry somewhere. A Correct Solution would be
2585 to add a new .got section to a new object file and let it be merged
2586 somewhere later. But for now don't bother. */
2587 && ah->got_entries)
2588 {
2589 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2590
2591 s = bfd_get_section_by_name(dynobj, ".plt");
2592 if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
2593 return false;
2594
2595 /* The first bit of the .plt is reserved. */
2596 if (s->_raw_size == 0)
2597 s->_raw_size = PLT_HEADER_SIZE;
2598
2599 h->plt_offset = s->_raw_size;
2600 s->_raw_size += PLT_ENTRY_SIZE;
2601
2602 /* If this symbol is not defined in a regular file, and we are not
2603 generating a shared library, then set the symbol to the location
2604 in the .plt. This is required to make function pointers compare
2605 equal between the normal executable and the shared library. */
2606 if (!info->shared)
2607 {
2608 h->root.u.def.section = s;
2609 h->root.u.def.value = h->plt_offset;
2610 }
2611
2612 /* We also need a JMP_SLOT entry in the .rela.plt section. */
2613 s = bfd_get_section_by_name (dynobj, ".rela.plt");
2614 BFD_ASSERT (s != NULL);
2615 s->_raw_size += sizeof (Elf64_External_Rela);
2616
2617 return true;
2618 }
2619 else
2620 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2621
2622 /* If this is a weak symbol, and there is a real definition, the
2623 processor independent code will have arranged for us to see the
2624 real definition first, and we can just use the same value. */
2625 if (h->weakdef != NULL)
2626 {
2627 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2628 || h->weakdef->root.type == bfd_link_hash_defweak);
2629 h->root.u.def.section = h->weakdef->root.u.def.section;
2630 h->root.u.def.value = h->weakdef->root.u.def.value;
2631 return true;
2632 }
2633
2634 /* This is a reference to a symbol defined by a dynamic object which
2635 is not a function. The Alpha, since it uses .got entries for all
2636 symbols even in regular objects, does not need the hackery of a
2637 .dynbss section and COPY dynamic relocations. */
2638
2639 return true;
2640}
2641
69842d08
RH
2642/* Symbol versioning can create new symbols, and make our old symbols
2643 indirect to the new ones. Consolidate the got and reloc information
2644 in these situations. */
2645
2646static boolean
2647elf64_alpha_merge_ind_symbols (hi, dummy)
2648 struct alpha_elf_link_hash_entry *hi;
2649 PTR dummy;
2650{
2651 struct alpha_elf_link_hash_entry *hs;
2652
2653 if (hi->root.root.type != bfd_link_hash_indirect)
2654 return true;
2655 hs = hi;
2656 do {
2657 hs = (struct alpha_elf_link_hash_entry *)hs->root.root.u.i.link;
2658 } while (hs->root.root.type == bfd_link_hash_indirect);
2659
2660 /* Merge the flags. Whee. */
2661
2662 hs->flags |= hi->flags;
2663
2664 /* Merge the .got entries. Cannibalize the old symbol's list in
2665 doing so, since we don't need it anymore. */
2666
2667 if (hs->got_entries == NULL)
2668 hs->got_entries = hi->got_entries;
2669 else
2670 {
2671 struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
2672
2673 gsh = hs->got_entries;
2674 for (gi = hi->got_entries; gi ; gi = gin)
2675 {
2676 gin = gi->next;
2677 for (gs = gsh; gs ; gs = gs->next)
2678 if (gi->gotobj == gs->gotobj && gi->addend == gs->addend)
2679 goto got_found;
2680 gi->next = hs->got_entries;
2681 hs->got_entries = gi;
2682 got_found:;
2683 }
2684 }
2685 hi->got_entries = NULL;
2686
2687 /* And similar for the reloc entries. */
2688
2689 if (hs->reloc_entries == NULL)
2690 hs->reloc_entries = hi->reloc_entries;
2691 else
2692 {
2693 struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
2694
2695 rsh = hs->reloc_entries;
2696 for (ri = hi->reloc_entries; ri ; ri = rin)
2697 {
2698 rin = ri->next;
2699 for (rs = rsh; rs ; rs = rs->next)
2700 if (ri->rtype == rs->rtype)
2701 {
2702 rs->count += ri->count;
2703 goto found_reloc;
2704 }
2705 ri->next = hs->reloc_entries;
2706 hs->reloc_entries = ri;
2707 found_reloc:;
2708 }
2709 }
2710 hi->reloc_entries = NULL;
2711
2712 return true;
2713}
2714
ff12f303
ILT
2715/* Is it possible to merge two object file's .got tables? */
2716
2717static boolean
2718elf64_alpha_can_merge_gots (a, b)
2719 bfd *a, *b;
2720{
2721 int total = alpha_elf_tdata (a)->total_got_entries;
2722
2723 /* Trivial quick fallout test. */
2724 if (total + alpha_elf_tdata (b)->total_got_entries <= MAX_GOT_ENTRIES)
2725 return true;
2726
2727 /* By their nature, local .got entries cannot be merged. */
2728 if ((total += alpha_elf_tdata (b)->n_local_got_entries) > MAX_GOT_ENTRIES)
2729 return false;
2730
2731 /* Failing the common trivial comparison, we must effectively
2732 perform the merge. Not actually performing the merge means that
2733 we don't have to store undo information in case we fail. */
2734 {
2735 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
2736 Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
2737 int i, n;
2738
2739 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2740 for (i = 0; i < n; ++i)
2741 {
2742 struct alpha_elf_got_entry *ae, *be;
69842d08
RH
2743 struct alpha_elf_link_hash_entry *h;
2744
2745 h = hashes[i];
2746 while (h->root.root.type == bfd_link_hash_indirect
2747 || h->root.root.type == bfd_link_hash_warning)
2748 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2749
2750 for (be = h->got_entries; be ; be = be->next)
ff12f303 2751 {
69842d08
RH
2752 if (be->use_count == 0)
2753 continue;
ff12f303
ILT
2754 if (be->gotobj != b)
2755 continue;
2756
69842d08 2757 for (ae = h->got_entries; ae ; ae = ae->next)
ff12f303
ILT
2758 if (ae->gotobj == a && ae->addend == be->addend)
2759 goto global_found;
2760
2761 if (++total > MAX_GOT_ENTRIES)
2762 return false;
2763 global_found:;
2764 }
2765 }
2766 }
2767
2768 return true;
2769}
2770
2771/* Actually merge two .got tables. */
2772
2773static void
2774elf64_alpha_merge_gots (a, b)
2775 bfd *a, *b;
2776{
2777 int total = alpha_elf_tdata(a)->total_got_entries;
2778
2779 /* Remember local expansion. */
2780 {
2781 int e = alpha_elf_tdata(b)->n_local_got_entries;
2782 total += e;
2783 alpha_elf_tdata(a)->n_local_got_entries += e;
2784 }
2785
2786 /* Let the local .got entries know they are part of a new subsegment. */
2787 {
2788 struct alpha_elf_got_entry **local_got_entries;
2789 local_got_entries = alpha_elf_tdata(b)->local_got_entries;
2790 if (local_got_entries)
2791 {
2792 int i, n;
2793
2794 n = elf_tdata(b)->symtab_hdr.sh_info;
2795 for (i = 0; i < n; ++i)
2796 {
2797 struct alpha_elf_got_entry *gotent;
2798 for (gotent = local_got_entries[i]; gotent; gotent = gotent->next)
2799 gotent->gotobj = a;
2800 }
2801 }
2802 }
2803
2804 /* Merge the global .got entries. */
2805 {
2806 struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes(b);
2807 Elf_Internal_Shdr *symtab_hdr = &elf_tdata(b)->symtab_hdr;
2808 int i, n;
2809
2810 n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
2811 for (i = 0; i < n; ++i)
2812 {
2813 struct alpha_elf_got_entry *ae, *be, **pbe, **start;
69842d08
RH
2814 struct alpha_elf_link_hash_entry *h;
2815
2816 h = hashes[i];
2817 while (h->root.root.type == bfd_link_hash_indirect
2818 || h->root.root.type == bfd_link_hash_warning)
2819 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
2820
2821 start = &h->got_entries;
ff12f303
ILT
2822 for (pbe = start, be = *start; be ; pbe = &be->next, be = be->next)
2823 {
69842d08
RH
2824 if (be->use_count == 0)
2825 {
2826 *pbe = be->next;
2827 continue;
2828 }
ff12f303
ILT
2829 if (be->gotobj != b)
2830 continue;
297a4f1a 2831
ff12f303
ILT
2832 for (ae = *start; ae ; ae = ae->next)
2833 if (ae->gotobj == a && ae->addend == be->addend)
297a4f1a 2834 {
3a13dd8f 2835 ae->flags |= be->flags;
ff12f303
ILT
2836 *pbe = be->next;
2837 goto global_found;
297a4f1a 2838 }
ff12f303
ILT
2839 be->gotobj = a;
2840 total += 1;
297a4f1a 2841
ff12f303
ILT
2842 global_found:;
2843 }
2844 }
2845 }
297a4f1a 2846
ff12f303
ILT
2847 alpha_elf_tdata(a)->total_got_entries = total;
2848 alpha_elf_tdata(b)->gotobj = a;
2849}
297a4f1a 2850
ff12f303 2851/* Calculate the offsets for the got entries. */
297a4f1a 2852
ff12f303
ILT
2853static boolean
2854elf64_alpha_calc_got_offsets_for_symbol (h, arg)
2855 struct alpha_elf_link_hash_entry *h;
2856 PTR arg;
2857{
2858 struct alpha_elf_got_entry *gotent;
297a4f1a 2859
ff12f303 2860 for (gotent = h->got_entries; gotent; gotent = gotent->next)
69842d08
RH
2861 if (gotent->use_count > 0)
2862 {
2863 bfd_size_type *plge
2864 = &alpha_elf_tdata (gotent->gotobj)->got->_raw_size;
2865
2866 gotent->got_offset = *plge;
2867 *plge += 8;
2868 }
297a4f1a 2869
ff12f303
ILT
2870 return true;
2871}
297a4f1a 2872
ff12f303
ILT
2873static void
2874elf64_alpha_calc_got_offsets (info)
2875 struct bfd_link_info *info;
2876{
2877 bfd *i, *got_list = alpha_elf_hash_table(info)->got_list;
297a4f1a 2878
ff12f303
ILT
2879 /* First, zero out the .got sizes, as we may be recalculating the
2880 .got after optimizing it. */
2881 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2882 alpha_elf_tdata(i)->got->_raw_size = 0;
297a4f1a 2883
ff12f303
ILT
2884 /* Next, fill in the offsets for all the global entries. */
2885 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
2886 elf64_alpha_calc_got_offsets_for_symbol,
2887 NULL);
297a4f1a 2888
ff12f303
ILT
2889 /* Finally, fill in the offsets for the local entries. */
2890 for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
2891 {
2892 bfd_size_type got_offset = alpha_elf_tdata(i)->got->_raw_size;
2893 bfd *j;
297a4f1a 2894
ff12f303
ILT
2895 for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
2896 {
2897 struct alpha_elf_got_entry **local_got_entries, *gotent;
2898 int k, n;
297a4f1a 2899
ff12f303
ILT
2900 local_got_entries = alpha_elf_tdata(j)->local_got_entries;
2901 if (!local_got_entries)
2902 continue;
2903
2904 for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
2905 for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
986c3691
RH
2906 if (gotent->use_count > 0)
2907 {
2908 gotent->got_offset = got_offset;
2909 got_offset += 8;
2910 }
297a4f1a 2911 }
ff12f303
ILT
2912
2913 alpha_elf_tdata(i)->got->_raw_size = got_offset;
986c3691 2914 alpha_elf_tdata(i)->got->_cooked_size = got_offset;
297a4f1a 2915 }
ff12f303 2916}
297a4f1a 2917
ff12f303
ILT
2918/* Remove a section from the output BFD. */
2919
2920static void
2921elf64_alpha_strip_section_from_output (s)
2922 asection *s;
2923{
2924 asection **spp;
2925
2926 for (spp = &s->output_section->owner->sections;
2927 *spp != s->output_section;
2928 spp = &(*spp)->next)
2929 continue;
2930 *spp = s->output_section->next;
2931 --s->output_section->owner->section_count;
297a4f1a
ILT
2932}
2933
ff12f303 2934/* Constructs the gots. */
297a4f1a
ILT
2935
2936static boolean
69842d08 2937elf64_alpha_size_got_sections (output_bfd, info)
ff12f303 2938 bfd *output_bfd;
297a4f1a 2939 struct bfd_link_info *info;
297a4f1a 2940{
ff12f303
ILT
2941 bfd *i, *got_list, *cur_got_obj, **cur_got_tail;
2942 int ngots;
297a4f1a 2943
ff12f303
ILT
2944 ngots = 0;
2945 got_list = NULL;
2946 cur_got_obj = NULL;
2947 cur_got_tail = NULL;
2948 for (i = info->input_bfds; i ; i = i->link_next)
297a4f1a 2949 {
ff12f303
ILT
2950 bfd *this_got = alpha_elf_tdata (i)->gotobj;
2951
2952 /* Don't play if there is no .got for this input file. */
2953 if (this_got == NULL)
2954 continue;
2955
2956 if (alpha_elf_tdata (this_got)->total_got_entries > MAX_GOT_ENTRIES)
297a4f1a 2957 {
ff12f303
ILT
2958 /* Yikes! A single object file has too many entries. */
2959 (*_bfd_error_handler)
53d3ce37 2960 (_("%s: .got subsegment exceeds 64K (size %d)"),
ff12f303
ILT
2961 bfd_get_filename(i),
2962 alpha_elf_tdata(this_got)->total_got_entries * 8);
2963 return false;
297a4f1a
ILT
2964 }
2965
ff12f303
ILT
2966 if (cur_got_obj)
2967 {
211b0be8
RH
2968 if (this_got == cur_got_obj)
2969 ; /* Some previous pass merged us already. */
2970 else if (elf64_alpha_can_merge_gots (cur_got_obj, i))
ff12f303
ILT
2971 {
2972 elf64_alpha_merge_gots (cur_got_obj, i);
2973 *cur_got_tail = i;
2974 }
2975 else
2976 {
2977 if (++ngots == 2)
2978 {
2979 (*info->callbacks->warning)
53d3ce37 2980 (info, _("using multiple gp values"), (char *) NULL,
ff12f303
ILT
2981 output_bfd, (asection *) NULL, (bfd_vma) 0);
2982 }
2983 *cur_got_tail = NULL;
2984 alpha_elf_tdata(cur_got_obj)->got_link_next = got_list;
2985 got_list = cur_got_obj;
2986 cur_got_obj = i;
2987 }
2988 }
2989 else
2990 {
2991 ++ngots;
2992 cur_got_obj = i;
2993 }
2994 cur_got_tail = &alpha_elf_tdata(i)->in_got_link_next;
2995 }
297a4f1a 2996
ff12f303
ILT
2997 if (cur_got_obj)
2998 alpha_elf_tdata (cur_got_obj)->got_link_next = got_list;
69842d08 2999 alpha_elf_hash_table (info)->got_list = cur_got_obj;
297a4f1a 3000
ff12f303
ILT
3001 /* Once the gots have been merged, fill in the got offsets for everything
3002 therein. */
3003 elf64_alpha_calc_got_offsets (info);
297a4f1a 3004
69842d08
RH
3005 return true;
3006}
3007
3008static boolean
3009elf64_alpha_always_size_sections (output_bfd, info)
3010 bfd *output_bfd;
3011 struct bfd_link_info *info;
3012{
3013 bfd *i;
3014
3015 if (info->relocateable)
3016 return true;
3017
3018 /* First, take care of the indirect symbols created by versioning. */
3019 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3020 elf64_alpha_merge_ind_symbols,
3021 NULL);
3022
3023 if (!elf64_alpha_size_got_sections (output_bfd, info))
3024 return false;
3025
ff12f303 3026 /* Allocate space for all of the .got subsections. */
69842d08
RH
3027 i = alpha_elf_hash_table (info)->got_list;
3028 for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
ff12f303
ILT
3029 {
3030 asection *s = alpha_elf_tdata(i)->got;
3031 if (s->_raw_size > 0)
297a4f1a 3032 {
ff12f303
ILT
3033 s->contents = (bfd_byte *) bfd_zalloc (i, s->_raw_size);
3034 if (s->contents == NULL)
3035 return false;
297a4f1a 3036 }
ff12f303 3037 }
297a4f1a 3038
ff12f303
ILT
3039 return true;
3040}
297a4f1a 3041
ff12f303 3042/* Work out the sizes of the dynamic relocation entries. */
297a4f1a 3043
ff12f303
ILT
3044static boolean
3045elf64_alpha_calc_dynrel_sizes (h, info)
3046 struct alpha_elf_link_hash_entry *h;
3047 struct bfd_link_info *info;
3048{
3049 /* If the symbol was defined as a common symbol in a regular object
3050 file, and there was no definition in any dynamic object, then the
3051 linker will have allocated space for the symbol in a common
3052 section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
3053 set. This is done for dynamic symbols in
3054 elf_adjust_dynamic_symbol but this is not done for non-dynamic
3055 symbols, somehow. */
3056 if (((h->root.elf_link_hash_flags
3057 & (ELF_LINK_HASH_DEF_REGULAR
3058 | ELF_LINK_HASH_REF_REGULAR
3059 | ELF_LINK_HASH_DEF_DYNAMIC))
3060 == ELF_LINK_HASH_REF_REGULAR)
3061 && (h->root.root.type == bfd_link_hash_defined
3062 || h->root.root.type == bfd_link_hash_defweak)
3063 && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
3064 {
3065 h->root.elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
297a4f1a
ILT
3066 }
3067
ff12f303 3068 /* If the symbol is dynamic, we'll need all the relocations in their
6a587c4a
RH
3069 natural form. If it has been forced local, we'll need the same
3070 number of RELATIVE relocations. */
69842d08 3071 if (alpha_elf_dynamic_symbol_p (&h->root, info)
6a587c4a 3072 || (info->shared && h->root.dynindx == -1))
297a4f1a 3073 {
ff12f303
ILT
3074 struct alpha_elf_reloc_entry *relent;
3075
3076 for (relent = h->reloc_entries; relent; relent = relent->next)
3077 {
3078 relent->srel->_raw_size +=
3079 sizeof (Elf64_External_Rela) * relent->count;
3080 }
3081
3082 /* Only add a .rela.got entry if we're not using a .plt entry. */
3083 if (h->root.plt_offset == MINUS_ONE)
3084 {
3085 bfd *dynobj = elf_hash_table(info)->dynobj;
3086 struct alpha_elf_got_entry *gotent;
3087 bfd_size_type count = 0;
3088 asection *srel;
3089
3090 for (gotent = h->got_entries; gotent ; gotent = gotent->next)
3091 count++;
3092 if (count > 0)
3093 {
3094 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3095 BFD_ASSERT (srel != NULL);
3096 srel->_raw_size += sizeof (Elf64_External_Rela) * count;
3097 }
3098 }
297a4f1a 3099 }
ff12f303
ILT
3100 /* Otherwise, shared objects require RELATIVE relocs for all REFQUAD
3101 and REFLONG relocations. */
3102 else if (info->shared)
3103 {
3104 struct alpha_elf_reloc_entry *relent;
297a4f1a 3105
ff12f303
ILT
3106 for (relent = h->reloc_entries; relent; relent = relent->next)
3107 if (relent->rtype == R_ALPHA_REFLONG
3108 || relent->rtype == R_ALPHA_REFQUAD)
3109 {
3110 relent->srel->_raw_size +=
3111 sizeof(Elf64_External_Rela) * relent->count;
3112 }
3113 }
297a4f1a
ILT
3114
3115 return true;
3116}
3117
3118/* Set the sizes of the dynamic sections. */
3119
3120static boolean
3121elf64_alpha_size_dynamic_sections (output_bfd, info)
3122 bfd *output_bfd;
3123 struct bfd_link_info *info;
3124{
3125 bfd *dynobj;
3126 asection *s;
3127 boolean reltext;
3128 boolean relplt;
3129
3130 dynobj = elf_hash_table(info)->dynobj;
3131 BFD_ASSERT(dynobj != NULL);
3132
ff12f303 3133 if (elf_hash_table (info)->dynamic_sections_created)
297a4f1a
ILT
3134 {
3135 /* Set the contents of the .interp section to the interpreter. */
3136 if (!info->shared)
3137 {
ff12f303
ILT
3138 s = bfd_get_section_by_name (dynobj, ".interp");
3139 BFD_ASSERT (s != NULL);
297a4f1a 3140 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
ff12f303
ILT
3141 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3142 }
3143
3144 /* Now that we've seen all of the input files, we can decide which
3145 symbols need dynamic relocation entries and which don't. We've
3146 collected information in check_relocs that we can now apply to
3147 size the dynamic relocation sections. */
3148 alpha_elf_link_hash_traverse (alpha_elf_hash_table (info),
3149 elf64_alpha_calc_dynrel_sizes,
3150 info);
3151
3152 /* When building shared libraries, each local .got entry needs a
3153 RELATIVE reloc. */
3154 if (info->shared)
3155 {
3156 bfd *i;
3157 asection *srel;
3158 bfd_size_type count;
3159
3160 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3161 BFD_ASSERT (srel != NULL);
3162
3163 for (i = alpha_elf_hash_table(info)->got_list, count = 0;
3164 i != NULL;
3165 i = alpha_elf_tdata(i)->got_link_next)
3166 count += alpha_elf_tdata(i)->n_local_got_entries;
3167
3168 srel->_raw_size += count * sizeof(Elf64_External_Rela);
297a4f1a
ILT
3169 }
3170 }
ff12f303 3171 /* else we're not dynamic and by definition we don't need such things. */
297a4f1a
ILT
3172
3173 /* The check_relocs and adjust_dynamic_symbol entry points have
3174 determined the sizes of the various dynamic sections. Allocate
3175 memory for them. */
3176 reltext = false;
3177 relplt = false;
3178 for (s = dynobj->sections; s != NULL; s = s->next)
3179 {
3180 const char *name;
3181 boolean strip;
3182
ff12f303 3183 if (!(s->flags & SEC_LINKER_CREATED))
297a4f1a
ILT
3184 continue;
3185
3186 /* It's OK to base decisions on the section name, because none
3187 of the dynobj section names depend upon the input files. */
ff12f303 3188 name = bfd_get_section_name (dynobj, s);
297a4f1a
ILT
3189
3190 /* If we don't need this section, strip it from the output file.
3191 This is to handle .rela.bss and .rela.plt. We must create it
3192 in create_dynamic_sections, because it must be created before
3193 the linker maps input sections to output sections. The
3194 linker does that before adjust_dynamic_symbol is called, and
3195 it is that function which decides whether anything needs to
3196 go into these sections. */
3197
3198 strip = false;
3199
ff12f303 3200 if (strncmp (name, ".rela", 5) == 0)
297a4f1a
ILT
3201 {
3202 strip = (s->_raw_size == 0);
3203
3204 if (!strip)
3205 {
303e7257 3206 const char *outname;
297a4f1a
ILT
3207 asection *target;
3208
3209 /* If this relocation section applies to a read only
3210 section, then we probably need a DT_TEXTREL entry. */
303e7257
ILT
3211 outname = bfd_get_section_name (output_bfd,
3212 s->output_section);
3213 target = bfd_get_section_by_name (output_bfd, outname + 5);
297a4f1a 3214 if (target != NULL
3a13dd8f
RH
3215 && (target->flags & SEC_READONLY) != 0
3216 && (target->flags & SEC_ALLOC) != 0)
297a4f1a
ILT
3217 reltext = true;
3218
3219 if (strcmp(name, ".rela.plt") == 0)
3220 relplt = true;
3221
3222 /* We use the reloc_count field as a counter if we need
3223 to copy relocs into the output file. */
3224 s->reloc_count = 0;
3225 }
3226 }
297a4f1a
ILT
3227 else if (strcmp (name, ".plt") != 0)
3228 {
ff12f303 3229 /* It's not one of our dynamic sections, so don't allocate space. */
297a4f1a
ILT
3230 continue;
3231 }
3232
3233 if (strip)
ff12f303
ILT
3234 elf64_alpha_strip_section_from_output (s);
3235 else
297a4f1a 3236 {
ff12f303
ILT
3237 /* Allocate memory for the section contents. */
3238 s->contents = (bfd_byte *) bfd_zalloc(dynobj, s->_raw_size);
3239 if (s->contents == NULL && s->_raw_size != 0)
3240 return false;
3241 }
3242 }
297a4f1a 3243
ff12f303
ILT
3244 /* If we are generating a shared library, we generate a section
3245 symbol for each output section. These are local symbols, which
3246 means that they must come first in the dynamic symbol table.
3247 That means we must increment the dynamic symbol index of every
3248 other dynamic symbol. */
3249 if (info->shared)
3250 {
3251 long c[2], i;
3252 asection *p;
297a4f1a 3253
ff12f303
ILT
3254 c[0] = 0;
3255 c[1] = bfd_count_sections (output_bfd);
297a4f1a 3256
ff12f303
ILT
3257 elf_hash_table (info)->dynsymcount += c[1];
3258 elf_link_hash_traverse (elf_hash_table(info),
3259 elf64_alpha_adjust_dynindx,
3260 (PTR) c);
3261
3262 for (i = 1, p = output_bfd->sections;
3263 p != NULL;
3264 p = p->next, i++)
3265 {
3266 elf_section_data (p)->dynindx = i;
3267 /* These symbols will have no names, so we don't need to
3268 fiddle with dynstr_index. */
3269 }
297a4f1a
ILT
3270 }
3271
3272 if (elf_hash_table (info)->dynamic_sections_created)
3273 {
3274 /* Add some entries to the .dynamic section. We fill in the
3275 values later, in elf64_alpha_finish_dynamic_sections, but we
3276 must add the entries now so that we get the correct size for
3277 the .dynamic section. The DT_DEBUG entry is filled in by the
3278 dynamic linker and used by the debugger. */
3279 if (!info->shared)
3280 {
3281 if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
3282 return false;
3283 }
3284
3285 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
3286 return false;
3287
3288 if (relplt)
3289 {
3290 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3291 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
3292 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
3293 return false;
3294 }
3295
3296 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
3297 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
3298 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
3299 sizeof(Elf64_External_Rela)))
3300 return false;
3301
3302 if (reltext)
3303 {
3304 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
3305 return false;
3306 }
3307 }
3308
3309 return true;
3310}
3311
3312/* Increment the index of a dynamic symbol by a given amount. Called
3313 via elf_link_hash_traverse. */
3314
3315static boolean
3316elf64_alpha_adjust_dynindx (h, cparg)
3317 struct elf_link_hash_entry *h;
3318 PTR cparg;
3319{
3320 long *cp = (long *)cparg;
3321
3322 if (h->dynindx >= cp[0])
3323 h->dynindx += cp[1];
3324
3325 return true;
3326}
3327
3328/* Relocate an Alpha ELF section. */
3329
3330static boolean
3331elf64_alpha_relocate_section (output_bfd, info, input_bfd, input_section,
3332 contents, relocs, local_syms, local_sections)
3333 bfd *output_bfd;
3334 struct bfd_link_info *info;
3335 bfd *input_bfd;
3336 asection *input_section;
3337 bfd_byte *contents;
3338 Elf_Internal_Rela *relocs;
3339 Elf_Internal_Sym *local_syms;
3340 asection **local_sections;
3341{
3342 Elf_Internal_Shdr *symtab_hdr;
3343 Elf_Internal_Rela *rel;
3344 Elf_Internal_Rela *relend;
ff12f303
ILT
3345 asection *sec, *sgot, *srel, *srelgot;
3346 bfd *dynobj, *gotobj;
297a4f1a
ILT
3347 bfd_vma gp;
3348
ff12f303
ILT
3349 srelgot = srel = NULL;
3350 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3351 dynobj = elf_hash_table (info)->dynobj;
3352 if (dynobj)
3353 {
3354 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3355 }
297a4f1a
ILT
3356
3357 /* Find the gp value for this input bfd. */
3358 sgot = NULL;
3359 gp = 0;
ff12f303
ILT
3360 gotobj = alpha_elf_tdata (input_bfd)->gotobj;
3361 if (gotobj)
297a4f1a 3362 {
ff12f303
ILT
3363 sgot = alpha_elf_tdata (gotobj)->got;
3364 gp = _bfd_get_gp_value (gotobj);
297a4f1a
ILT
3365 if (gp == 0)
3366 {
3367 gp = (sgot->output_section->vma
3368 + sgot->output_offset
3369 + 0x8000);
ff12f303 3370 _bfd_set_gp_value (gotobj, gp);
297a4f1a
ILT
3371 }
3372 }
3373
3374 rel = relocs;
3375 relend = relocs + input_section->reloc_count;
3376 for (; rel < relend; rel++)
3377 {
3378 int r_type;
3379 reloc_howto_type *howto;
3380 unsigned long r_symndx;
ff12f303 3381 struct alpha_elf_link_hash_entry *h;
297a4f1a
ILT
3382 Elf_Internal_Sym *sym;
3383 bfd_vma relocation;
3384 bfd_vma addend;
3385 bfd_reloc_status_type r;
3386
3387 r_type = ELF64_R_TYPE(rel->r_info);
3388 if (r_type < 0 || r_type >= (int) R_ALPHA_max)
3389 {
3390 bfd_set_error (bfd_error_bad_value);
3391 return false;
3392 }
3393 howto = elf64_alpha_howto_table + r_type;
3394
3395 r_symndx = ELF64_R_SYM(rel->r_info);
3396
3397 if (info->relocateable)
3398 {
3399 /* This is a relocateable link. We don't have to change
3400 anything, unless the reloc is against a section symbol,
3401 in which case we have to adjust according to where the
3402 section symbol winds up in the output section. */
3403 if (r_symndx < symtab_hdr->sh_info)
3404 {
3405 sym = local_syms + r_symndx;
3406 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
3407 {
3408 sec = local_sections[r_symndx];
3409 rel->r_addend += sec->output_offset + sym->st_value;
3410 }
3411 }
3412
3413 continue;
3414 }
3415
3416 /* This is a final link. */
3417
3418 h = NULL;
3419 sym = NULL;
3420 sec = NULL;
3421
3422 if (r_symndx < symtab_hdr->sh_info)
3423 {
3424 sym = local_syms + r_symndx;
3425 sec = local_sections[r_symndx];
3426 relocation = (sec->output_section->vma
3427 + sec->output_offset
3428 + sym->st_value);
3429 }
3430 else
3431 {
ff12f303 3432 h = alpha_elf_sym_hashes (input_bfd)[r_symndx - symtab_hdr->sh_info];
297a4f1a 3433
ff12f303
ILT
3434 while (h->root.root.type == bfd_link_hash_indirect
3435 || h->root.root.type == bfd_link_hash_warning)
3436 h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
297a4f1a 3437
ff12f303
ILT
3438 if (h->root.root.type == bfd_link_hash_defined
3439 || h->root.root.type == bfd_link_hash_defweak)
297a4f1a 3440 {
ff12f303 3441 sec = h->root.root.u.def.section;
297a4f1a
ILT
3442
3443#if rth_notdef
3444 if ((r_type == R_ALPHA_LITERAL
3445 && elf_hash_table(info)->dynamic_sections_created
3446 && (!info->shared
3447 || !info->symbolic
ff12f303 3448 || !(h->root.elf_link_hash_flags
297a4f1a
ILT
3449 & ELF_LINK_HASH_DEF_REGULAR)))
3450 || (info->shared
3451 && (!info->symbolic
ff12f303 3452 || !(h->root.elf_link_hash_flags
297a4f1a
ILT
3453 & ELF_LINK_HASH_DEF_REGULAR))
3454 && (input_section->flags & SEC_ALLOC)
3455 && (r_type == R_ALPHA_REFLONG
3456 || r_type == R_ALPHA_REFQUAD
3457 || r_type == R_ALPHA_LITERAL)))
3458 {
3459 /* In these cases, we don't need the relocation value.
3460 We check specially because in some obscure cases
3461 sec->output_section will be NULL. */
3462 relocation = 0;
3463 }
3464#else
3465 /* FIXME: Are not these obscure cases simply bugs? Let's
3466 get something working and come back to this. */
3467 if (sec->output_section == NULL)
3468 relocation = 0;
3469#endif /* rth_notdef */
3470 else
3471 {
ff12f303 3472 relocation = (h->root.root.u.def.value
297a4f1a
ILT
3473 + sec->output_section->vma
3474 + sec->output_offset);
3475 }
3476 }
ff12f303 3477 else if (h->root.root.type == bfd_link_hash_undefweak)
297a4f1a
ILT
3478 relocation = 0;
3479 else if (info->shared && !info->symbolic)
3480 relocation = 0;
3481 else
3482 {
3483 if (!((*info->callbacks->undefined_symbol)
ff12f303 3484 (info, h->root.root.root.string, input_bfd,
297a4f1a
ILT
3485 input_section, rel->r_offset)))
3486 return false;
3487 relocation = 0;
3488 }
3489 }
3490 addend = rel->r_addend;
3491
3492 switch (r_type)
3493 {
3494 case R_ALPHA_GPDISP:
3495 {
3496 bfd_byte *p_ldah, *p_lda;
3497
ff12f303
ILT
3498 BFD_ASSERT(gp != 0);
3499
297a4f1a
ILT
3500 relocation = (input_section->output_section->vma
3501 + input_section->output_offset
3502 + rel->r_offset);
3503
3504 p_ldah = contents + rel->r_offset - input_section->vma;
3505 p_lda = p_ldah + rel->r_addend;
3506
3507 r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - relocation,
3508 p_ldah, p_lda);
3509 }
3510 break;
3511
3512 case R_ALPHA_OP_PUSH:
3513 case R_ALPHA_OP_STORE:
3514 case R_ALPHA_OP_PSUB:
3515 case R_ALPHA_OP_PRSHIFT:
ff12f303 3516 /* We hate these silly beasts. */
297a4f1a
ILT
3517 abort();
3518
3519 case R_ALPHA_LITERAL:
3520 {
ff12f303 3521 struct alpha_elf_got_entry *gotent;
297a4f1a 3522
297a4f1a 3523 BFD_ASSERT(sgot != NULL);
ff12f303
ILT
3524 BFD_ASSERT(gp != 0);
3525
297a4f1a
ILT
3526 if (h != NULL)
3527 {
ff12f303 3528 gotent = h->got_entries;
69842d08 3529 BFD_ASSERT(gotent != NULL);
3a13dd8f 3530
ff12f303
ILT
3531 while (gotent->gotobj != gotobj || gotent->addend != addend)
3532 gotent = gotent->next;
297a4f1a 3533
ff12f303
ILT
3534 /* Initialize the .got entry's value. */
3535 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
3536 {
3537 bfd_put_64 (output_bfd, relocation+addend,
3538 sgot->contents + gotent->got_offset);
297a4f1a 3539
6a587c4a
RH
3540 /* If the symbol has been forced local, output a
3541 RELATIVE reloc, otherwise it will be handled in
3542 finish_dynamic_symbol. */
3543 if (info->shared && h->root.dynindx == -1)
3544 {
3545 Elf_Internal_Rela outrel;
3546
3547 BFD_ASSERT(srelgot != NULL);
3548
3549 outrel.r_offset = (sgot->output_section->vma
3550 + sgot->output_offset
3551 + gotent->got_offset);
3552 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3553 outrel.r_addend = 0;
3554
3555 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3556 ((Elf64_External_Rela *)
3557 srelgot->contents)
3558 + srelgot->reloc_count++);
3559 }
297a4f1a 3560
ff12f303
ILT
3561 gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
3562 }
3563 }
3564 else
3565 {
3566 gotent = (alpha_elf_tdata(input_bfd)->
3567 local_got_entries[r_symndx]);
3568 while (gotent->addend != addend)
3569 gotent = gotent->next;
3570
3571 if (!(gotent->flags & ALPHA_ELF_GOT_ENTRY_RELOCS_DONE))
3572 {
3573 bfd_put_64 (output_bfd, relocation+addend,
3574 sgot->contents + gotent->got_offset);
3575
3576 /* Local got entries need RELATIVE relocs in shared
3577 libraries. */
3578 if (info->shared)
3579 {
3580 Elf_Internal_Rela outrel;
3581
3582 BFD_ASSERT(srelgot != NULL);
3583
3584 outrel.r_offset = (sgot->output_section->vma
3585 + sgot->output_offset
3586 + gotent->got_offset);
3587 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3588 outrel.r_addend = 0;
3589
3590 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3591 ((Elf64_External_Rela *)
3592 srelgot->contents)
3593 + srelgot->reloc_count++);
3594 }
3595
3596 gotent->flags |= ALPHA_ELF_GOT_ENTRY_RELOCS_DONE;
3597 }
297a4f1a 3598 }
297a4f1a
ILT
3599
3600 /* Figure the gprel relocation. */
3601 addend = 0;
3602 relocation = (sgot->output_section->vma
3603 + sgot->output_offset
ff12f303 3604 + gotent->got_offset);
297a4f1a
ILT
3605 relocation -= gp;
3606 }
3607 /* overflow handled by _bfd_final_link_relocate */
3608 goto default_reloc;
ff12f303 3609
297a4f1a 3610 case R_ALPHA_GPREL32:
69842d08
RH
3611 case R_ALPHA_GPRELLOW:
3612 BFD_ASSERT(gp != 0);
3613 relocation -= gp;
3614 goto default_reloc;
3615
3616 case R_ALPHA_GPRELHIGH:
297a4f1a
ILT
3617 BFD_ASSERT(gp != 0);
3618 relocation -= gp;
69842d08
RH
3619 relocation += addend;
3620 addend = 0;
3621 relocation = (((bfd_signed_vma) relocation >> 16)
3622 + ((relocation >> 15) & 1));
297a4f1a
ILT
3623 goto default_reloc;
3624
3625 case R_ALPHA_BRADDR:
3626 case R_ALPHA_HINT:
3627 /* The regular PC-relative stuff measures from the start of
3628 the instruction rather than the end. */
3629 addend -= 4;
3630 goto default_reloc;
ff12f303 3631
297a4f1a
ILT
3632 case R_ALPHA_REFLONG:
3633 case R_ALPHA_REFQUAD:
ff12f303
ILT
3634 {
3635 Elf_Internal_Rela outrel;
3a13dd8f 3636 boolean skip;
297a4f1a 3637
ff12f303
ILT
3638 /* Careful here to remember RELATIVE relocations for global
3639 variables for symbolic shared objects. */
3640
3641 if (h && alpha_elf_dynamic_symbol_p (&h->root, info))
3642 {
3643 BFD_ASSERT(h->root.dynindx != -1);
3644 outrel.r_info = ELF64_R_INFO(h->root.dynindx, r_type);
3645 outrel.r_addend = addend;
3646 addend = 0, relocation = 0;
3647 }
3648 else if (info->shared)
3649 {
3650 outrel.r_info = ELF64_R_INFO(0, R_ALPHA_RELATIVE);
3651 outrel.r_addend = 0;
3652 }
3653 else
3654 goto default_reloc;
3655
3656 if (!srel)
3657 {
3658 const char *name;
3659
3660 name = (bfd_elf_string_from_elf_section
3661 (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
3662 elf_section_data(input_section)->rel_hdr.sh_name));
3663 BFD_ASSERT(name != NULL);
3664
3665 srel = bfd_get_section_by_name (dynobj, name);
3666 BFD_ASSERT(srel != NULL);
3667 }
3668
3a13dd8f
RH
3669 skip = false;
3670
3671 if (elf_section_data (input_section)->stab_info == NULL)
3672 outrel.r_offset = rel->r_offset;
3673 else
3674 {
3675 bfd_vma off;
3676
3677 off = (_bfd_stab_section_offset
3678 (output_bfd, &elf_hash_table (info)->stab_info,
3679 input_section,
3680 &elf_section_data (input_section)->stab_info,
3681 rel->r_offset));
3682 if (off == (bfd_vma) -1)
3683 skip = true;
3684 outrel.r_offset = off;
3685 }
3686
3687 if (! skip)
3688 outrel.r_offset += (input_section->output_section->vma
3689 + input_section->output_offset);
3690 else
3691 memset (&outrel, 0, sizeof outrel);
ff12f303
ILT
3692
3693 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3694 ((Elf64_External_Rela *)
3695 srel->contents)
3696 + srel->reloc_count++);
3697 }
297a4f1a
ILT
3698 goto default_reloc;
3699
3700 default:
3701 default_reloc:
3702 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3703 contents, rel->r_offset, relocation,
3704 addend);
3705 break;
3706 }
3707
3708 switch (r)
3709 {
3710 case bfd_reloc_ok:
3711 break;
3712
3713 case bfd_reloc_overflow:
3714 {
3715 const char *name;
3716
3717 if (h != NULL)
ff12f303 3718 name = h->root.root.root.string;
297a4f1a
ILT
3719 else
3720 {
3721 name = (bfd_elf_string_from_elf_section
3722 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3723 if (name == NULL)
3724 return false;
3725 if (*name == '\0')
3726 name = bfd_section_name (input_bfd, sec);
3727 }
3728 if (! ((*info->callbacks->reloc_overflow)
3729 (info, name, howto->name, (bfd_vma) 0,
3730 input_bfd, input_section, rel->r_offset)))
3731 return false;
3732 }
3733 break;
3734
3735 default:
3736 case bfd_reloc_outofrange:
3737 abort ();
3738 }
3739 }
3740
3741 return true;
3742}
3743
3744/* Finish up dynamic symbol handling. We set the contents of various
3745 dynamic sections here. */
3746
3747static boolean
3748elf64_alpha_finish_dynamic_symbol (output_bfd, info, h, sym)
3749 bfd *output_bfd;
3750 struct bfd_link_info *info;
3751 struct elf_link_hash_entry *h;
3752 Elf_Internal_Sym *sym;
3753{
3754 bfd *dynobj = elf_hash_table(info)->dynobj;
3755
3756 if (h->plt_offset != MINUS_ONE)
3757 {
ff12f303 3758 /* Fill in the .plt entry for this symbol. */
297a4f1a
ILT
3759 asection *splt, *sgot, *srel;
3760 Elf_Internal_Rela outrel;
3761 bfd_vma got_addr, plt_addr;
3762 bfd_vma plt_index;
ff12f303 3763 struct alpha_elf_got_entry *gotent;
297a4f1a 3764
ff12f303 3765 BFD_ASSERT (h->dynindx != -1);
297a4f1a 3766
ff12f303
ILT
3767 /* The first .got entry will be updated by the .plt with the
3768 address of the target function. */
3769 gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3770 BFD_ASSERT (gotent && gotent->addend == 0);
297a4f1a 3771
ff12f303
ILT
3772 splt = bfd_get_section_by_name (dynobj, ".plt");
3773 BFD_ASSERT (splt != NULL);
3774 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
3775 BFD_ASSERT (srel != NULL);
3776 sgot = alpha_elf_tdata (gotent->gotobj)->got;
3777 BFD_ASSERT (sgot != NULL);
297a4f1a
ILT
3778
3779 got_addr = (sgot->output_section->vma
ff12f303
ILT
3780 + sgot->output_offset
3781 + gotent->got_offset);
297a4f1a
ILT
3782 plt_addr = (splt->output_section->vma
3783 + splt->output_offset
3784 + h->plt_offset);
3785
3786 plt_index = (h->plt_offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
3787
3788 /* Fill in the entry in the procedure linkage table. */
3789 {
3790 unsigned insn1, insn2, insn3;
297a4f1a 3791
3a13dd8f
RH
3792 insn1 = PLT_ENTRY_WORD1 | ((-(h->plt_offset + 4) >> 2) & 0x1fffff);
3793 insn2 = PLT_ENTRY_WORD2;
3794 insn3 = PLT_ENTRY_WORD3;
ff12f303 3795
297a4f1a
ILT
3796 bfd_put_32 (output_bfd, insn1, splt->contents + h->plt_offset);
3797 bfd_put_32 (output_bfd, insn2, splt->contents + h->plt_offset + 4);
3798 bfd_put_32 (output_bfd, insn3, splt->contents + h->plt_offset + 8);
3799 }
3800
3801 /* Fill in the entry in the .rela.plt section. */
3802 outrel.r_offset = got_addr;
3803 outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
3804 outrel.r_addend = 0;
3805
3806 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3807 ((Elf64_External_Rela *)srel->contents
3808 + plt_index));
3809
3810 if (!(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3811 {
3812 /* Mark the symbol as undefined, rather than as defined in the
3813 .plt section. Leave the value alone. */
3814 sym->st_shndx = SHN_UNDEF;
3815 }
3816
ff12f303
ILT
3817 /* Fill in the entries in the .got. */
3818 bfd_put_64 (output_bfd, plt_addr, sgot->contents + gotent->got_offset);
3819
3820 /* Subsequent .got entries will continue to bounce through the .plt. */
3821 while ((gotent = gotent->next) != NULL)
3822 {
3823 sgot = alpha_elf_tdata(gotent->gotobj)->got;
3824 BFD_ASSERT(sgot != NULL);
3825 BFD_ASSERT(gotent->addend == 0);
3826
3827 bfd_put_64 (output_bfd, plt_addr,
3828 sgot->contents + gotent->got_offset);
3829 }
297a4f1a 3830 }
ff12f303 3831 else if (alpha_elf_dynamic_symbol_p (h, info))
297a4f1a 3832 {
ff12f303
ILT
3833 /* Fill in the dynamic relocations for this symbol's .got entries. */
3834 asection *srel;
297a4f1a 3835 Elf_Internal_Rela outrel;
ff12f303 3836 struct alpha_elf_got_entry *gotent;
297a4f1a 3837
297a4f1a
ILT
3838 srel = bfd_get_section_by_name (dynobj, ".rela.got");
3839 BFD_ASSERT (srel != NULL);
3840
ff12f303
ILT
3841 outrel.r_info = ELF64_R_INFO (h->dynindx, R_ALPHA_GLOB_DAT);
3842 for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
3843 gotent != NULL;
3844 gotent = gotent->next)
297a4f1a 3845 {
ff12f303
ILT
3846 asection *sgot = alpha_elf_tdata (gotent->gotobj)->got;
3847 outrel.r_offset = (sgot->output_section->vma
3848 + sgot->output_offset
3849 + gotent->got_offset);
3850 outrel.r_addend = gotent->addend;
3851
3852 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
3853 ((Elf64_External_Rela *)srel->contents
3854 + srel->reloc_count++));
297a4f1a 3855 }
297a4f1a
ILT
3856 }
3857
3858 /* Mark some specially defined symbols as absolute. */
3859 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3860 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3861 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3862 sym->st_shndx = SHN_ABS;
3863
3864 return true;
3865}
3866
3867/* Finish up the dynamic sections. */
3868
3869static boolean
3870elf64_alpha_finish_dynamic_sections (output_bfd, info)
3871 bfd *output_bfd;
3872 struct bfd_link_info *info;
3873{
3874 bfd *dynobj;
3875 asection *sdyn;
297a4f1a
ILT
3876
3877 dynobj = elf_hash_table (info)->dynobj;
3878 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3879
3880 if (elf_hash_table (info)->dynamic_sections_created)
3881 {
3882 asection *splt;
3883 Elf64_External_Dyn *dyncon, *dynconend;
3884
3885 splt = bfd_get_section_by_name (dynobj, ".plt");
3886 BFD_ASSERT (splt != NULL && sdyn != NULL);
3887
3888 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3889 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3890 for (; dyncon < dynconend; dyncon++)
3891 {
3892 Elf_Internal_Dyn dyn;
3893 const char *name;
3894 asection *s;
3895
3896 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3897
3898 switch (dyn.d_tag)
3899 {
3900 case DT_PLTGOT:
3901 name = ".plt";
3902 goto get_vma;
3903 case DT_PLTRELSZ:
3904 name = ".rela.plt";
3905 goto get_size;
3906 case DT_JMPREL:
3907 name = ".rela.plt";
3908 goto get_vma;
3909
3910 case DT_RELASZ:
3911 /* My interpretation of the TIS v1.1 ELF document indicates
3912 that RELASZ should not include JMPREL. This is not what
ff12f303
ILT
3913 the rest of the BFD does. It is, however, what the
3914 glibc ld.so wants. Do this fixup here until we found
297a4f1a
ILT
3915 out who is right. */
3916 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
3917 if (s)
3918 {
3919 dyn.d_un.d_val -=
3920 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3921 }
3922 break;
3923
3924 get_vma:
3925 s = bfd_get_section_by_name (output_bfd, name);
3926 dyn.d_un.d_ptr = (s ? s->vma : 0);
3927 break;
3928
3929 get_size:
3930 s = bfd_get_section_by_name (output_bfd, name);
3931 dyn.d_un.d_val =
3932 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
3933 break;
3934 }
3935
3936 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3937 }
3938
3939 /* Initialize the PLT0 entry */
3940 if (splt->_raw_size > 0)
3941 {
3942 bfd_put_32 (output_bfd, PLT_HEADER_WORD1, splt->contents);
3943 bfd_put_32 (output_bfd, PLT_HEADER_WORD2, splt->contents + 4);
3944 bfd_put_32 (output_bfd, PLT_HEADER_WORD3, splt->contents + 8);
3945 bfd_put_32 (output_bfd, PLT_HEADER_WORD4, splt->contents + 12);
ff12f303 3946
297a4f1a
ILT
3947 /* The next two words will be filled in by ld.so */
3948 bfd_put_64 (output_bfd, 0, splt->contents + 16);
3949 bfd_put_64 (output_bfd, 0, splt->contents + 24);
3950
ff12f303 3951 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
297a4f1a
ILT
3952 PLT_HEADER_SIZE;
3953 }
3954 }
3955
297a4f1a
ILT
3956 if (info->shared)
3957 {
3958 asection *sdynsym;
3959 asection *s;
3960 Elf_Internal_Sym sym;
3961
3962 /* Set up the section symbols for the output sections. */
3963
3964 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
3965 BFD_ASSERT (sdynsym != NULL);
3966
3967 sym.st_size = 0;
3968 sym.st_name = 0;
3969 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3970 sym.st_other = 0;
3971
3972 for (s = output_bfd->sections; s != NULL; s = s->next)
3973 {
3974 int indx;
3975
3976 sym.st_value = s->vma;
3977
3978 indx = elf_section_data (s)->this_idx;
3979 BFD_ASSERT (indx > 0);
3980 sym.st_shndx = indx;
3981
3982 bfd_elf64_swap_symbol_out (output_bfd, &sym,
3983 (PTR) (((Elf64_External_Sym *)
3984 sdynsym->contents)
3985 + elf_section_data (s)->dynindx));
3986 }
3987
3988 /* Set the sh_info field of the output .dynsym section to the
3989 index of the first global symbol. */
3990 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
3991 bfd_count_sections (output_bfd) + 1;
3992 }
3993
3994 return true;
3995}
3996
3997/* We need to use a special link routine to handle the .reginfo and
3998 the .mdebug sections. We need to merge all instances of these
3999 sections together, not write them all out sequentially. */
4000
4001static boolean
4002elf64_alpha_final_link (abfd, info)
4003 bfd *abfd;
4004 struct bfd_link_info *info;
4005{
4006 asection *o;
4007 struct bfd_link_order *p;
4008 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4009 struct ecoff_debug_info debug;
4010 const struct ecoff_debug_swap *swap
4011 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4012 HDRR *symhdr = &debug.symbolic_header;
4013 PTR mdebug_handle = NULL;
4014
4015 /* Go through the sections and collect the .reginfo and .mdebug
4016 information. */
4017 reginfo_sec = NULL;
4018 mdebug_sec = NULL;
4019 gptab_data_sec = NULL;
4020 gptab_bss_sec = NULL;
4021 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4022 {
4023#ifdef ERIC_neverdef
4024 if (strcmp (o->name, ".reginfo") == 0)
4025 {
4026 memset (&reginfo, 0, sizeof reginfo);
4027
4028 /* We have found the .reginfo section in the output file.
4029 Look through all the link_orders comprising it and merge
4030 the information together. */
4031 for (p = o->link_order_head;
4032 p != (struct bfd_link_order *) NULL;
4033 p = p->next)
4034 {
4035 asection *input_section;
4036 bfd *input_bfd;
4037 Elf64_External_RegInfo ext;
4038 Elf64_RegInfo sub;
4039
4040 if (p->type != bfd_indirect_link_order)
4041 {
4042 if (p->type == bfd_fill_link_order)
4043 continue;
4044 abort ();
4045 }
4046
4047 input_section = p->u.indirect.section;
4048 input_bfd = input_section->owner;
4049
4050 /* The linker emulation code has probably clobbered the
4051 size to be zero bytes. */
4052 if (input_section->_raw_size == 0)
4053 input_section->_raw_size = sizeof (Elf64_External_RegInfo);
4054
4055 if (! bfd_get_section_contents (input_bfd, input_section,
4056 (PTR) &ext,
4057 (file_ptr) 0,
4058 sizeof ext))
4059 return false;
4060
4061 bfd_alpha_elf64_swap_reginfo_in (input_bfd, &ext, &sub);
4062
4063 reginfo.ri_gprmask |= sub.ri_gprmask;
4064 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4065 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4066 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4067 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4068
4069 /* ri_gp_value is set by the function
4070 alpha_elf_section_processing when the section is
4071 finally written out. */
4072
4073 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4074 elf_link_input_bfd ignores this section. */
4075 input_section->flags &=~ SEC_HAS_CONTENTS;
4076 }
4077
4078 /* Force the section size to the value we want. */
4079 o->_raw_size = sizeof (Elf64_External_RegInfo);
4080
4081 /* Skip this section later on (I don't think this currently
4082 matters, but someday it might). */
4083 o->link_order_head = (struct bfd_link_order *) NULL;
4084
4085 reginfo_sec = o;
4086 }
4087#endif
4088
4089 if (strcmp (o->name, ".mdebug") == 0)
4090 {
4091 struct extsym_info einfo;
4092
4093 /* We have found the .mdebug section in the output file.
4094 Look through all the link_orders comprising it and merge
4095 the information together. */
4096 symhdr->magic = swap->sym_magic;
4097 /* FIXME: What should the version stamp be? */
4098 symhdr->vstamp = 0;
4099 symhdr->ilineMax = 0;
4100 symhdr->cbLine = 0;
4101 symhdr->idnMax = 0;
4102 symhdr->ipdMax = 0;
4103 symhdr->isymMax = 0;
4104 symhdr->ioptMax = 0;
4105 symhdr->iauxMax = 0;
4106 symhdr->issMax = 0;
4107 symhdr->issExtMax = 0;
4108 symhdr->ifdMax = 0;
4109 symhdr->crfd = 0;
4110 symhdr->iextMax = 0;
4111
4112 /* We accumulate the debugging information itself in the
4113 debug_info structure. */
4114 debug.line = NULL;
4115 debug.external_dnr = NULL;
4116 debug.external_pdr = NULL;
4117 debug.external_sym = NULL;
4118 debug.external_opt = NULL;
4119 debug.external_aux = NULL;
4120 debug.ss = NULL;
4121 debug.ssext = debug.ssext_end = NULL;
4122 debug.external_fdr = NULL;
4123 debug.external_rfd = NULL;
4124 debug.external_ext = debug.external_ext_end = NULL;
4125
4126 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4127 if (mdebug_handle == (PTR) NULL)
4128 return false;
4129
4130 if (1)
4131 {
4132 asection *s;
4133 EXTR esym;
4134 bfd_vma last;
4135 unsigned int i;
4136 static const char * const name[] =
4137 {
4138 ".text", ".init", ".fini", ".data",
4139 ".rodata", ".sdata", ".sbss", ".bss"
4140 };
4141 static const int sc[] = { scText, scInit, scFini, scData,
4142 scRData, scSData, scSBss, scBss };
4143
4144 esym.jmptbl = 0;
4145 esym.cobol_main = 0;
4146 esym.weakext = 0;
4147 esym.reserved = 0;
4148 esym.ifd = ifdNil;
4149 esym.asym.iss = issNil;
4150 esym.asym.st = stLocal;
4151 esym.asym.reserved = 0;
4152 esym.asym.index = indexNil;
4153 for (i = 0; i < 8; i++)
4154 {
4155 esym.asym.sc = sc[i];
4156 s = bfd_get_section_by_name (abfd, name[i]);
4157 if (s != NULL)
4158 {
4159 esym.asym.value = s->vma;
4160 last = s->vma + s->_raw_size;
4161 }
4162 else
4163 esym.asym.value = last;
ff12f303 4164
297a4f1a
ILT
4165 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4166 name[i], &esym))
4167 return false;
4168 }
4169 }
4170
4171 for (p = o->link_order_head;
4172 p != (struct bfd_link_order *) NULL;
4173 p = p->next)
4174 {
4175 asection *input_section;
4176 bfd *input_bfd;
4177 const struct ecoff_debug_swap *input_swap;
4178 struct ecoff_debug_info input_debug;
4179 char *eraw_src;
4180 char *eraw_end;
4181
4182 if (p->type != bfd_indirect_link_order)
4183 {
4184 if (p->type == bfd_fill_link_order)
4185 continue;
4186 abort ();
4187 }
4188
4189 input_section = p->u.indirect.section;
4190 input_bfd = input_section->owner;
4191
4192 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4193 || (get_elf_backend_data (input_bfd)
4194 ->elf_backend_ecoff_debug_swap) == NULL)
4195 {
4196 /* I don't know what a non ALPHA ELF bfd would be
4197 doing with a .mdebug section, but I don't really
4198 want to deal with it. */
4199 continue;
4200 }
4201
4202 input_swap = (get_elf_backend_data (input_bfd)
4203 ->elf_backend_ecoff_debug_swap);
4204
4205 BFD_ASSERT (p->size == input_section->_raw_size);
4206
4207 /* The ECOFF linking code expects that we have already
4208 read in the debugging information and set up an
4209 ecoff_debug_info structure, so we do that now. */
4210 if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
4211 &input_debug))
4212 return false;
4213
4214 if (! (bfd_ecoff_debug_accumulate
4215 (mdebug_handle, abfd, &debug, swap, input_bfd,
4216 &input_debug, input_swap, info)))
4217 return false;
4218
4219 /* Loop through the external symbols. For each one with
4220 interesting information, try to find the symbol in
4221 the linker global hash table and save the information
4222 for the output external symbols. */
4223 eraw_src = input_debug.external_ext;
4224 eraw_end = (eraw_src
4225 + (input_debug.symbolic_header.iextMax
4226 * input_swap->external_ext_size));
4227 for (;
4228 eraw_src < eraw_end;
4229 eraw_src += input_swap->external_ext_size)
4230 {
4231 EXTR ext;
4232 const char *name;
4233 struct alpha_elf_link_hash_entry *h;
4234
4235 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4236 if (ext.asym.sc == scNil
4237 || ext.asym.sc == scUndefined
4238 || ext.asym.sc == scSUndefined)
4239 continue;
4240
4241 name = input_debug.ssext + ext.asym.iss;
4242 h = alpha_elf_link_hash_lookup (alpha_elf_hash_table (info),
4243 name, false, false, true);
4244 if (h == NULL || h->esym.ifd != -2)
4245 continue;
4246
4247 if (ext.ifd != -1)
4248 {
4249 BFD_ASSERT (ext.ifd
4250 < input_debug.symbolic_header.ifdMax);
4251 ext.ifd = input_debug.ifdmap[ext.ifd];
4252 }
4253
4254 h->esym = ext;
4255 }
4256
4257 /* Free up the information we just read. */
4258 free (input_debug.line);
4259 free (input_debug.external_dnr);
4260 free (input_debug.external_pdr);
4261 free (input_debug.external_sym);
4262 free (input_debug.external_opt);
4263 free (input_debug.external_aux);
4264 free (input_debug.ss);
4265 free (input_debug.ssext);
4266 free (input_debug.external_fdr);
4267 free (input_debug.external_rfd);
4268 free (input_debug.external_ext);
4269
4270 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4271 elf_link_input_bfd ignores this section. */
4272 input_section->flags &=~ SEC_HAS_CONTENTS;
4273 }
4274
4275#ifdef ERIC_neverdef
4276 if (info->shared)
4277 {
4278 /* Create .rtproc section. */
4279 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4280 if (rtproc_sec == NULL)
4281 {
ff12f303
ILT
4282 flagword flags = (SEC_HAS_CONTENTS
4283 | SEC_IN_MEMORY
4284 | SEC_LINKER_CREATED
297a4f1a
ILT
4285 | SEC_READONLY);
4286
4287 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4288 if (rtproc_sec == NULL
4289 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4290 || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
4291 return false;
4292 }
4293
4294 if (! alpha_elf_create_procedure_table (mdebug_handle, abfd,
4295 info, rtproc_sec, &debug))
4296 return false;
4297 }
4298#endif
4299
4300
4301 /* Build the external symbol information. */
4302 einfo.abfd = abfd;
4303 einfo.info = info;
4304 einfo.debug = &debug;
4305 einfo.swap = swap;
4306 einfo.failed = false;
4307 elf_link_hash_traverse (elf_hash_table (info),
4308 elf64_alpha_output_extsym,
4309 (PTR) &einfo);
4310 if (einfo.failed)
4311 return false;
4312
4313 /* Set the size of the .mdebug section. */
4314 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4315
4316 /* Skip this section later on (I don't think this currently
4317 matters, but someday it might). */
4318 o->link_order_head = (struct bfd_link_order *) NULL;
4319
4320 mdebug_sec = o;
4321 }
4322
4323#ifdef ERIC_neverdef
4324 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4325 {
4326 const char *subname;
4327 unsigned int c;
4328 Elf64_gptab *tab;
4329 Elf64_External_gptab *ext_tab;
4330 unsigned int i;
4331
4332 /* The .gptab.sdata and .gptab.sbss sections hold
4333 information describing how the small data area would
4334 change depending upon the -G switch. These sections
4335 not used in executables files. */
4336 if (! info->relocateable)
4337 {
4338 asection **secpp;
4339
4340 for (p = o->link_order_head;
4341 p != (struct bfd_link_order *) NULL;
4342 p = p->next)
4343 {
4344 asection *input_section;
4345
4346 if (p->type != bfd_indirect_link_order)
4347 {
4348 if (p->type == bfd_fill_link_order)
4349 continue;
4350 abort ();
4351 }
4352
4353 input_section = p->u.indirect.section;
4354
4355 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4356 elf_link_input_bfd ignores this section. */
4357 input_section->flags &=~ SEC_HAS_CONTENTS;
4358 }
4359
4360 /* Skip this section later on (I don't think this
4361 currently matters, but someday it might). */
4362 o->link_order_head = (struct bfd_link_order *) NULL;
4363
4364 /* Really remove the section. */
4365 for (secpp = &abfd->sections;
4366 *secpp != o;
4367 secpp = &(*secpp)->next)
4368 ;
4369 *secpp = (*secpp)->next;
4370 --abfd->section_count;
4371
4372 continue;
4373 }
4374
4375 /* There is one gptab for initialized data, and one for
4376 uninitialized data. */
4377 if (strcmp (o->name, ".gptab.sdata") == 0)
4378 gptab_data_sec = o;
4379 else if (strcmp (o->name, ".gptab.sbss") == 0)
4380 gptab_bss_sec = o;
4381 else
4382 {
4383 (*_bfd_error_handler)
53d3ce37 4384 (_("%s: illegal section name `%s'"),
297a4f1a
ILT
4385 bfd_get_filename (abfd), o->name);
4386 bfd_set_error (bfd_error_nonrepresentable_section);
4387 return false;
4388 }
4389
4390 /* The linker script always combines .gptab.data and
4391 .gptab.sdata into .gptab.sdata, and likewise for
4392 .gptab.bss and .gptab.sbss. It is possible that there is
4393 no .sdata or .sbss section in the output file, in which
4394 case we must change the name of the output section. */
4395 subname = o->name + sizeof ".gptab" - 1;
4396 if (bfd_get_section_by_name (abfd, subname) == NULL)
4397 {
4398 if (o == gptab_data_sec)
4399 o->name = ".gptab.data";
4400 else
4401 o->name = ".gptab.bss";
4402 subname = o->name + sizeof ".gptab" - 1;
4403 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4404 }
4405
4406 /* Set up the first entry. */
4407 c = 1;
4408 tab = (Elf64_gptab *) bfd_malloc (c * sizeof (Elf64_gptab));
4409 if (tab == NULL)
4410 return false;
4411 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4412 tab[0].gt_header.gt_unused = 0;
4413
4414 /* Combine the input sections. */
4415 for (p = o->link_order_head;
4416 p != (struct bfd_link_order *) NULL;
4417 p = p->next)
4418 {
4419 asection *input_section;
4420 bfd *input_bfd;
4421 bfd_size_type size;
4422 unsigned long last;
4423 bfd_size_type gpentry;
4424
4425 if (p->type != bfd_indirect_link_order)
4426 {
4427 if (p->type == bfd_fill_link_order)
4428 continue;
4429 abort ();
4430 }
4431
4432 input_section = p->u.indirect.section;
4433 input_bfd = input_section->owner;
4434
4435 /* Combine the gptab entries for this input section one
4436 by one. We know that the input gptab entries are
4437 sorted by ascending -G value. */
4438 size = bfd_section_size (input_bfd, input_section);
4439 last = 0;
4440 for (gpentry = sizeof (Elf64_External_gptab);
4441 gpentry < size;
4442 gpentry += sizeof (Elf64_External_gptab))
4443 {
4444 Elf64_External_gptab ext_gptab;
4445 Elf64_gptab int_gptab;
4446 unsigned long val;
4447 unsigned long add;
4448 boolean exact;
4449 unsigned int look;
4450
4451 if (! (bfd_get_section_contents
4452 (input_bfd, input_section, (PTR) &ext_gptab,
4453 gpentry, sizeof (Elf64_External_gptab))))
4454 {
4455 free (tab);
4456 return false;
4457 }
4458
4459 bfd_alpha_elf64_swap_gptab_in (input_bfd, &ext_gptab,
4460 &int_gptab);
4461 val = int_gptab.gt_entry.gt_g_value;
4462 add = int_gptab.gt_entry.gt_bytes - last;
4463
4464 exact = false;
4465 for (look = 1; look < c; look++)
4466 {
4467 if (tab[look].gt_entry.gt_g_value >= val)
4468 tab[look].gt_entry.gt_bytes += add;
4469
4470 if (tab[look].gt_entry.gt_g_value == val)
4471 exact = true;
4472 }
4473
4474 if (! exact)
4475 {
4476 Elf64_gptab *new_tab;
4477 unsigned int max;
4478
4479 /* We need a new table entry. */
4480 new_tab = ((Elf64_gptab *)
4481 bfd_realloc ((PTR) tab,
4482 (c + 1) * sizeof (Elf64_gptab)));
4483 if (new_tab == NULL)
4484 {
4485 free (tab);
4486 return false;
4487 }
4488 tab = new_tab;
4489 tab[c].gt_entry.gt_g_value = val;
4490 tab[c].gt_entry.gt_bytes = add;
4491
4492 /* Merge in the size for the next smallest -G
4493 value, since that will be implied by this new
4494 value. */
4495 max = 0;
4496 for (look = 1; look < c; look++)
4497 {
4498 if (tab[look].gt_entry.gt_g_value < val
4499 && (max == 0
4500 || (tab[look].gt_entry.gt_g_value
4501 > tab[max].gt_entry.gt_g_value)))
4502 max = look;
4503 }
4504 if (max != 0)
4505 tab[c].gt_entry.gt_bytes +=
4506 tab[max].gt_entry.gt_bytes;
4507
4508 ++c;
4509 }
4510
4511 last = int_gptab.gt_entry.gt_bytes;
4512 }
4513
4514 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4515 elf_link_input_bfd ignores this section. */
4516 input_section->flags &=~ SEC_HAS_CONTENTS;
4517 }
4518
4519 /* The table must be sorted by -G value. */
4520 if (c > 2)
4521 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4522
4523 /* Swap out the table. */
4524 ext_tab = ((Elf64_External_gptab *)
4525 bfd_alloc (abfd, c * sizeof (Elf64_External_gptab)));
4526 if (ext_tab == NULL)
4527 {
4528 free (tab);
4529 return false;
4530 }
4531
4532 for (i = 0; i < c; i++)
4533 bfd_alpha_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
4534 free (tab);
4535
4536 o->_raw_size = c * sizeof (Elf64_External_gptab);
4537 o->contents = (bfd_byte *) ext_tab;
4538
4539 /* Skip this section later on (I don't think this currently
4540 matters, but someday it might). */
4541 o->link_order_head = (struct bfd_link_order *) NULL;
4542 }
4543#endif
4544
4545 }
4546
4547 /* Invoke the regular ELF backend linker to do all the work. */
4548 if (! bfd_elf64_bfd_final_link (abfd, info))
4549 return false;
4550
4551 /* Now write out the computed sections. */
4552
ff12f303
ILT
4553 /* The .got subsections... */
4554 {
4555 bfd *i, *dynobj = elf_hash_table(info)->dynobj;
4556 for (i = alpha_elf_hash_table(info)->got_list;
4557 i != NULL;
4558 i = alpha_elf_tdata(i)->got_link_next)
4559 {
4560 asection *sgot;
4561
4562 /* elf_bfd_final_link already did everything in dynobj. */
4563 if (i == dynobj)
4564 continue;
4565
4566 sgot = alpha_elf_tdata(i)->got;
4567 if (! bfd_set_section_contents (abfd, sgot->output_section,
4568 sgot->contents, sgot->output_offset,
4569 sgot->_raw_size))
4570 return false;
4571 }
4572 }
4573
297a4f1a
ILT
4574#ifdef ERIC_neverdef
4575 if (reginfo_sec != (asection *) NULL)
4576 {
4577 Elf64_External_RegInfo ext;
4578
4579 bfd_alpha_elf64_swap_reginfo_out (abfd, &reginfo, &ext);
4580 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4581 (file_ptr) 0, sizeof ext))
4582 return false;
4583 }
4584#endif
4585
4586 if (mdebug_sec != (asection *) NULL)
4587 {
4588 BFD_ASSERT (abfd->output_has_begun);
4589 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4590 swap, info,
4591 mdebug_sec->filepos))
4592 return false;
4593
4594 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4595 }
4596
4597 if (gptab_data_sec != (asection *) NULL)
4598 {
4599 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4600 gptab_data_sec->contents,
4601 (file_ptr) 0,
4602 gptab_data_sec->_raw_size))
4603 return false;
4604 }
4605
4606 if (gptab_bss_sec != (asection *) NULL)
4607 {
4608 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4609 gptab_bss_sec->contents,
4610 (file_ptr) 0,
4611 gptab_bss_sec->_raw_size))
4612 return false;
4613 }
4614
4615 return true;
4616}
4617\f
4618/* ECOFF swapping routines. These are used when dealing with the
4619 .mdebug section, which is in the ECOFF debugging format. Copied
4620 from elf32-mips.c. */
4621static const struct ecoff_debug_swap
4622elf64_alpha_ecoff_debug_swap =
4623{
4624 /* Symbol table magic number. */
4625 magicSym2,
4626 /* Alignment of debugging information. E.g., 4. */
4627 8,
4628 /* Sizes of external symbolic information. */
4629 sizeof (struct hdr_ext),
4630 sizeof (struct dnr_ext),
4631 sizeof (struct pdr_ext),
4632 sizeof (struct sym_ext),
4633 sizeof (struct opt_ext),
4634 sizeof (struct fdr_ext),
4635 sizeof (struct rfd_ext),
4636 sizeof (struct ext_ext),
4637 /* Functions to swap in external symbolic data. */
4638 ecoff_swap_hdr_in,
4639 ecoff_swap_dnr_in,
4640 ecoff_swap_pdr_in,
4641 ecoff_swap_sym_in,
4642 ecoff_swap_opt_in,
4643 ecoff_swap_fdr_in,
4644 ecoff_swap_rfd_in,
4645 ecoff_swap_ext_in,
4646 _bfd_ecoff_swap_tir_in,
4647 _bfd_ecoff_swap_rndx_in,
4648 /* Functions to swap out external symbolic data. */
4649 ecoff_swap_hdr_out,
4650 ecoff_swap_dnr_out,
4651 ecoff_swap_pdr_out,
4652 ecoff_swap_sym_out,
4653 ecoff_swap_opt_out,
4654 ecoff_swap_fdr_out,
4655 ecoff_swap_rfd_out,
4656 ecoff_swap_ext_out,
4657 _bfd_ecoff_swap_tir_out,
4658 _bfd_ecoff_swap_rndx_out,
4659 /* Function to read in symbolic data. */
4660 elf64_alpha_read_ecoff_info
4661};
4662\f
4663#define TARGET_LITTLE_SYM bfd_elf64_alpha_vec
4664#define TARGET_LITTLE_NAME "elf64-alpha"
4665#define ELF_ARCH bfd_arch_alpha
4666#define ELF_MACHINE_CODE EM_ALPHA
69842d08 4667#define ELF_MAXPAGESIZE 0x10000
297a4f1a
ILT
4668
4669#define bfd_elf64_bfd_link_hash_table_create \
4670 elf64_alpha_bfd_link_hash_table_create
4671
4672#define bfd_elf64_bfd_reloc_type_lookup \
4673 elf64_alpha_bfd_reloc_type_lookup
4674#define elf_info_to_howto \
4675 elf64_alpha_info_to_howto
4676
ff12f303
ILT
4677#define bfd_elf64_mkobject \
4678 elf64_alpha_mkobject
297a4f1a 4679#define elf_backend_object_p \
ff12f303 4680 elf64_alpha_object_p
3a13dd8f 4681
297a4f1a
ILT
4682#define elf_backend_section_from_shdr \
4683 elf64_alpha_section_from_shdr
4684#define elf_backend_fake_sections \
4685 elf64_alpha_fake_sections
4686#define elf_backend_additional_program_headers \
4687 elf64_alpha_additional_program_headers
4688
303e7257
ILT
4689#define bfd_elf64_bfd_is_local_label_name \
4690 elf64_alpha_is_local_label_name
297a4f1a
ILT
4691#define bfd_elf64_find_nearest_line \
4692 elf64_alpha_find_nearest_line
69842d08
RH
4693#define bfd_elf64_bfd_relax_section \
4694 elf64_alpha_relax_section
297a4f1a 4695
69842d08
RH
4696#define elf_backend_add_symbol_hook \
4697 elf64_alpha_add_symbol_hook
297a4f1a
ILT
4698#define elf_backend_check_relocs \
4699 elf64_alpha_check_relocs
4700#define elf_backend_create_dynamic_sections \
4701 elf64_alpha_create_dynamic_sections
4702#define elf_backend_adjust_dynamic_symbol \
4703 elf64_alpha_adjust_dynamic_symbol
ff12f303
ILT
4704#define elf_backend_always_size_sections \
4705 elf64_alpha_always_size_sections
297a4f1a
ILT
4706#define elf_backend_size_dynamic_sections \
4707 elf64_alpha_size_dynamic_sections
4708#define elf_backend_relocate_section \
4709 elf64_alpha_relocate_section
4710#define elf_backend_finish_dynamic_symbol \
4711 elf64_alpha_finish_dynamic_symbol
4712#define elf_backend_finish_dynamic_sections \
4713 elf64_alpha_finish_dynamic_sections
4714#define bfd_elf64_bfd_final_link \
4715 elf64_alpha_final_link
4716
4717#define elf_backend_ecoff_debug_swap \
4718 &elf64_alpha_ecoff_debug_swap
4719
4720/*
4721 * A few constants that determine how the .plt section is set up.
4722 */
4723#define elf_backend_want_got_plt 0
4724#define elf_backend_plt_readonly 0
4725#define elf_backend_want_plt_sym 1
4726
4727#include "elf64-target.h"
This page took 0.307376 seconds and 4 git commands to generate.