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