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