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