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