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