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