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