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