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