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