1 /* BFD back-end for ALPHA Extended-Coff files. 2 Copyright (C) 1993-2020 Free Software Foundation, Inc. 3 Modified from coff-mips.c by Steve Chamberlain <sac@cygnus.com> and 4 Ian Lance Taylor <ian@cygnus.com>. 5 6 This file is part of BFD, the Binary File Descriptor library. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 MA 02110-1301, USA. */ 22 23 #include "sysdep.h" 24 #include "bfd.h" 25 #include "bfdlink.h" 26 #include "libbfd.h" 27 #include "coff/internal.h" 28 #include "coff/sym.h" 29 #include "coff/symconst.h" 30 #include "coff/ecoff.h" 31 #include "coff/alpha.h" 32 #include "aout/ar.h" 33 #include "libcoff.h" 34 #include "libecoff.h" 35 36 /* Prototypes for static functions. */ 37 38 39 40 /* ECOFF has COFF sections, but the debugging information is stored in 41 a completely different format. ECOFF targets use some of the 42 swapping routines from coffswap.h, and some of the generic COFF 43 routines in coffgen.c, but, unlike the real COFF targets, do not 44 use coffcode.h itself. 45 46 Get the generic COFF swapping routines, except for the reloc, 47 symbol, and lineno ones. Give them ecoff names. Define some 48 accessor macros for the large sizes used for Alpha ECOFF. */ 49 50 #define GET_FILEHDR_SYMPTR H_GET_64 51 #define PUT_FILEHDR_SYMPTR H_PUT_64 52 #define GET_AOUTHDR_TSIZE H_GET_64 53 #define PUT_AOUTHDR_TSIZE H_PUT_64 54 #define GET_AOUTHDR_DSIZE H_GET_64 55 #define PUT_AOUTHDR_DSIZE H_PUT_64 56 #define GET_AOUTHDR_BSIZE H_GET_64 57 #define PUT_AOUTHDR_BSIZE H_PUT_64 58 #define GET_AOUTHDR_ENTRY H_GET_64 59 #define PUT_AOUTHDR_ENTRY H_PUT_64 60 #define GET_AOUTHDR_TEXT_START H_GET_64 61 #define PUT_AOUTHDR_TEXT_START H_PUT_64 62 #define GET_AOUTHDR_DATA_START H_GET_64 63 #define PUT_AOUTHDR_DATA_START H_PUT_64 64 #define GET_SCNHDR_PADDR H_GET_64 65 #define PUT_SCNHDR_PADDR H_PUT_64 66 #define GET_SCNHDR_VADDR H_GET_64 67 #define PUT_SCNHDR_VADDR H_PUT_64 68 #define GET_SCNHDR_SIZE H_GET_64 69 #define PUT_SCNHDR_SIZE H_PUT_64 70 #define GET_SCNHDR_SCNPTR H_GET_64 71 #define PUT_SCNHDR_SCNPTR H_PUT_64 72 #define GET_SCNHDR_RELPTR H_GET_64 73 #define PUT_SCNHDR_RELPTR H_PUT_64 74 #define GET_SCNHDR_LNNOPTR H_GET_64 75 #define PUT_SCNHDR_LNNOPTR H_PUT_64 76 77 #define ALPHAECOFF 78 79 #define NO_COFF_RELOCS 80 #define NO_COFF_SYMBOLS 81 #define NO_COFF_LINENOS 82 #define coff_swap_filehdr_in alpha_ecoff_swap_filehdr_in 83 #define coff_swap_filehdr_out alpha_ecoff_swap_filehdr_out 84 #define coff_swap_aouthdr_in alpha_ecoff_swap_aouthdr_in 85 #define coff_swap_aouthdr_out alpha_ecoff_swap_aouthdr_out 86 #define coff_swap_scnhdr_in alpha_ecoff_swap_scnhdr_in 87 #define coff_swap_scnhdr_out alpha_ecoff_swap_scnhdr_out 88 #include "coffswap.h" 89 90 /* Get the ECOFF swapping routines. */ 91 #define ECOFF_64 92 #include "ecoffswap.h" 93 94 /* How to process the various reloc types. */ 95 96 static bfd_reloc_status_type 97 reloc_nil (bfd *abfd ATTRIBUTE_UNUSED, 98 arelent *reloc ATTRIBUTE_UNUSED, 99 asymbol *sym ATTRIBUTE_UNUSED, 100 void * data ATTRIBUTE_UNUSED, 101 asection *sec ATTRIBUTE_UNUSED, 102 bfd *output_bfd ATTRIBUTE_UNUSED, 103 char **error_message ATTRIBUTE_UNUSED) 104 { 105 return bfd_reloc_ok; 106 } 107 108 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value 109 from smaller values. Start with zero, widen, *then* decrement. */ 110 #define MINUS_ONE (((bfd_vma)0) - 1) 111 112 static reloc_howto_type alpha_howto_table[] = 113 { 114 /* Reloc type 0 is ignored by itself. However, it appears after a 115 GPDISP reloc to identify the location where the low order 16 bits 116 of the gp register are loaded. */ 117 HOWTO (ALPHA_R_IGNORE, /* type */ 118 0, /* rightshift */ 119 0, /* size (0 = byte, 1 = short, 2 = long) */ 120 8, /* bitsize */ 121 TRUE, /* pc_relative */ 122 0, /* bitpos */ 123 complain_overflow_dont, /* complain_on_overflow */ 124 reloc_nil, /* special_function */ 125 "IGNORE", /* name */ 126 TRUE, /* partial_inplace */ 127 0, /* src_mask */ 128 0, /* dst_mask */ 129 TRUE), /* pcrel_offset */ 130 131 /* A 32 bit reference to a symbol. */ 132 HOWTO (ALPHA_R_REFLONG, /* type */ 133 0, /* rightshift */ 134 2, /* size (0 = byte, 1 = short, 2 = long) */ 135 32, /* bitsize */ 136 FALSE, /* pc_relative */ 137 0, /* bitpos */ 138 complain_overflow_bitfield, /* complain_on_overflow */ 139 0, /* special_function */ 140 "REFLONG", /* name */ 141 TRUE, /* partial_inplace */ 142 0xffffffff, /* src_mask */ 143 0xffffffff, /* dst_mask */ 144 FALSE), /* pcrel_offset */ 145 146 /* A 64 bit reference to a symbol. */ 147 HOWTO (ALPHA_R_REFQUAD, /* type */ 148 0, /* rightshift */ 149 4, /* size (0 = byte, 1 = short, 2 = long) */ 150 64, /* bitsize */ 151 FALSE, /* pc_relative */ 152 0, /* bitpos */ 153 complain_overflow_bitfield, /* complain_on_overflow */ 154 0, /* special_function */ 155 "REFQUAD", /* name */ 156 TRUE, /* partial_inplace */ 157 MINUS_ONE, /* src_mask */ 158 MINUS_ONE, /* dst_mask */ 159 FALSE), /* pcrel_offset */ 160 161 /* A 32 bit GP relative offset. This is just like REFLONG except 162 that when the value is used the value of the gp register will be 163 added in. */ 164 HOWTO (ALPHA_R_GPREL32, /* type */ 165 0, /* rightshift */ 166 2, /* size (0 = byte, 1 = short, 2 = long) */ 167 32, /* bitsize */ 168 FALSE, /* pc_relative */ 169 0, /* bitpos */ 170 complain_overflow_bitfield, /* complain_on_overflow */ 171 0, /* special_function */ 172 "GPREL32", /* name */ 173 TRUE, /* partial_inplace */ 174 0xffffffff, /* src_mask */ 175 0xffffffff, /* dst_mask */ 176 FALSE), /* pcrel_offset */ 177 178 /* Used for an instruction that refers to memory off the GP 179 register. The offset is 16 bits of the 32 bit instruction. This 180 reloc always seems to be against the .lita section. */ 181 HOWTO (ALPHA_R_LITERAL, /* type */ 182 0, /* rightshift */ 183 2, /* size (0 = byte, 1 = short, 2 = long) */ 184 16, /* bitsize */ 185 FALSE, /* pc_relative */ 186 0, /* bitpos */ 187 complain_overflow_signed, /* complain_on_overflow */ 188 0, /* special_function */ 189 "LITERAL", /* name */ 190 TRUE, /* partial_inplace */ 191 0xffff, /* src_mask */ 192 0xffff, /* dst_mask */ 193 FALSE), /* pcrel_offset */ 194 195 /* This reloc only appears immediately following a LITERAL reloc. 196 It identifies a use of the literal. It seems that the linker can 197 use this to eliminate a portion of the .lita section. The symbol 198 index is special: 1 means the literal address is in the base 199 register of a memory format instruction; 2 means the literal 200 address is in the byte offset register of a byte-manipulation 201 instruction; 3 means the literal address is in the target 202 register of a jsr instruction. This does not actually do any 203 relocation. */ 204 HOWTO (ALPHA_R_LITUSE, /* type */ 205 0, /* rightshift */ 206 2, /* size (0 = byte, 1 = short, 2 = long) */ 207 32, /* bitsize */ 208 FALSE, /* pc_relative */ 209 0, /* bitpos */ 210 complain_overflow_dont, /* complain_on_overflow */ 211 reloc_nil, /* special_function */ 212 "LITUSE", /* name */ 213 FALSE, /* partial_inplace */ 214 0, /* src_mask */ 215 0, /* dst_mask */ 216 FALSE), /* pcrel_offset */ 217 218 /* Load the gp register. This is always used for a ldah instruction 219 which loads the upper 16 bits of the gp register. The next reloc 220 will be an IGNORE reloc which identifies the location of the lda 221 instruction which loads the lower 16 bits. The symbol index of 222 the GPDISP instruction appears to actually be the number of bytes 223 between the ldah and lda instructions. This gives two different 224 ways to determine where the lda instruction is; I don't know why 225 both are used. The value to use for the relocation is the 226 difference between the GP value and the current location; the 227 load will always be done against a register holding the current 228 address. */ 229 HOWTO (ALPHA_R_GPDISP, /* type */ 230 16, /* rightshift */ 231 2, /* size (0 = byte, 1 = short, 2 = long) */ 232 16, /* bitsize */ 233 TRUE, /* pc_relative */ 234 0, /* bitpos */ 235 complain_overflow_dont, /* complain_on_overflow */ 236 reloc_nil, /* special_function */ 237 "GPDISP", /* name */ 238 TRUE, /* partial_inplace */ 239 0xffff, /* src_mask */ 240 0xffff, /* dst_mask */ 241 TRUE), /* pcrel_offset */ 242 243 /* A 21 bit branch. The native assembler generates these for 244 branches within the text segment, and also fills in the PC 245 relative offset in the instruction. */ 246 HOWTO (ALPHA_R_BRADDR, /* type */ 247 2, /* rightshift */ 248 2, /* size (0 = byte, 1 = short, 2 = long) */ 249 21, /* bitsize */ 250 TRUE, /* pc_relative */ 251 0, /* bitpos */ 252 complain_overflow_signed, /* complain_on_overflow */ 253 0, /* special_function */ 254 "BRADDR", /* name */ 255 TRUE, /* partial_inplace */ 256 0x1fffff, /* src_mask */ 257 0x1fffff, /* dst_mask */ 258 FALSE), /* pcrel_offset */ 259 260 /* A hint for a jump to a register. */ 261 HOWTO (ALPHA_R_HINT, /* type */ 262 2, /* rightshift */ 263 2, /* size (0 = byte, 1 = short, 2 = long) */ 264 14, /* bitsize */ 265 TRUE, /* pc_relative */ 266 0, /* bitpos */ 267 complain_overflow_dont, /* complain_on_overflow */ 268 0, /* special_function */ 269 "HINT", /* name */ 270 TRUE, /* partial_inplace */ 271 0x3fff, /* src_mask */ 272 0x3fff, /* dst_mask */ 273 FALSE), /* pcrel_offset */ 274 275 /* 16 bit PC relative offset. */ 276 HOWTO (ALPHA_R_SREL16, /* type */ 277 0, /* rightshift */ 278 1, /* size (0 = byte, 1 = short, 2 = long) */ 279 16, /* bitsize */ 280 TRUE, /* pc_relative */ 281 0, /* bitpos */ 282 complain_overflow_signed, /* complain_on_overflow */ 283 0, /* special_function */ 284 "SREL16", /* name */ 285 TRUE, /* partial_inplace */ 286 0xffff, /* src_mask */ 287 0xffff, /* dst_mask */ 288 FALSE), /* pcrel_offset */ 289 290 /* 32 bit PC relative offset. */ 291 HOWTO (ALPHA_R_SREL32, /* type */ 292 0, /* rightshift */ 293 2, /* size (0 = byte, 1 = short, 2 = long) */ 294 32, /* bitsize */ 295 TRUE, /* pc_relative */ 296 0, /* bitpos */ 297 complain_overflow_signed, /* complain_on_overflow */ 298 0, /* special_function */ 299 "SREL32", /* name */ 300 TRUE, /* partial_inplace */ 301 0xffffffff, /* src_mask */ 302 0xffffffff, /* dst_mask */ 303 FALSE), /* pcrel_offset */ 304 305 /* A 64 bit PC relative offset. */ 306 HOWTO (ALPHA_R_SREL64, /* type */ 307 0, /* rightshift */ 308 4, /* size (0 = byte, 1 = short, 2 = long) */ 309 64, /* bitsize */ 310 TRUE, /* pc_relative */ 311 0, /* bitpos */ 312 complain_overflow_signed, /* complain_on_overflow */ 313 0, /* special_function */ 314 "SREL64", /* name */ 315 TRUE, /* partial_inplace */ 316 MINUS_ONE, /* src_mask */ 317 MINUS_ONE, /* dst_mask */ 318 FALSE), /* pcrel_offset */ 319 320 /* Push a value on the reloc evaluation stack. */ 321 HOWTO (ALPHA_R_OP_PUSH, /* type */ 322 0, /* rightshift */ 323 0, /* size (0 = byte, 1 = short, 2 = long) */ 324 0, /* bitsize */ 325 FALSE, /* pc_relative */ 326 0, /* bitpos */ 327 complain_overflow_dont, /* complain_on_overflow */ 328 0, /* special_function */ 329 "OP_PUSH", /* name */ 330 FALSE, /* partial_inplace */ 331 0, /* src_mask */ 332 0, /* dst_mask */ 333 FALSE), /* pcrel_offset */ 334 335 /* Store the value from the stack at the given address. Store it in 336 a bitfield of size r_size starting at bit position r_offset. */ 337 HOWTO (ALPHA_R_OP_STORE, /* type */ 338 0, /* rightshift */ 339 4, /* size (0 = byte, 1 = short, 2 = long) */ 340 64, /* bitsize */ 341 FALSE, /* pc_relative */ 342 0, /* bitpos */ 343 complain_overflow_dont, /* complain_on_overflow */ 344 0, /* special_function */ 345 "OP_STORE", /* name */ 346 FALSE, /* partial_inplace */ 347 0, /* src_mask */ 348 MINUS_ONE, /* dst_mask */ 349 FALSE), /* pcrel_offset */ 350 351 /* Subtract the reloc address from the value on the top of the 352 relocation stack. */ 353 HOWTO (ALPHA_R_OP_PSUB, /* type */ 354 0, /* rightshift */ 355 0, /* size (0 = byte, 1 = short, 2 = long) */ 356 0, /* bitsize */ 357 FALSE, /* pc_relative */ 358 0, /* bitpos */ 359 complain_overflow_dont, /* complain_on_overflow */ 360 0, /* special_function */ 361 "OP_PSUB", /* name */ 362 FALSE, /* partial_inplace */ 363 0, /* src_mask */ 364 0, /* dst_mask */ 365 FALSE), /* pcrel_offset */ 366 367 /* Shift the value on the top of the relocation stack right by the 368 given value. */ 369 HOWTO (ALPHA_R_OP_PRSHIFT, /* type */ 370 0, /* rightshift */ 371 0, /* size (0 = byte, 1 = short, 2 = long) */ 372 0, /* bitsize */ 373 FALSE, /* pc_relative */ 374 0, /* bitpos */ 375 complain_overflow_dont, /* complain_on_overflow */ 376 0, /* special_function */ 377 "OP_PRSHIFT", /* name */ 378 FALSE, /* partial_inplace */ 379 0, /* src_mask */ 380 0, /* dst_mask */ 381 FALSE), /* pcrel_offset */ 382 383 /* Adjust the GP value for a new range in the object file. */ 384 HOWTO (ALPHA_R_GPVALUE, /* type */ 385 0, /* rightshift */ 386 0, /* size (0 = byte, 1 = short, 2 = long) */ 387 0, /* bitsize */ 388 FALSE, /* pc_relative */ 389 0, /* bitpos */ 390 complain_overflow_dont, /* complain_on_overflow */ 391 0, /* special_function */ 392 "GPVALUE", /* name */ 393 FALSE, /* partial_inplace */ 394 0, /* src_mask */ 395 0, /* dst_mask */ 396 FALSE) /* pcrel_offset */ 397 }; 398 399 /* Recognize an Alpha ECOFF file. */ 400 401 static const bfd_target * 402 alpha_ecoff_object_p (bfd *abfd) 403 { 404 static const bfd_target *ret; 405 406 ret = coff_object_p (abfd); 407 408 if (ret != NULL) 409 { 410 asection *sec; 411 412 /* Alpha ECOFF has a .pdata section. The lnnoptr field of the 413 .pdata section is the number of entries it contains. Each 414 entry takes up 8 bytes. The number of entries is required 415 since the section is aligned to a 16 byte boundary. When we 416 link .pdata sections together, we do not want to include the 417 alignment bytes. We handle this on input by faking the size 418 of the .pdata section to remove the unwanted alignment bytes. 419 On output we will set the lnnoptr field and force the 420 alignment. */ 421 sec = bfd_get_section_by_name (abfd, _PDATA); 422 if (sec != (asection *) NULL) 423 { 424 bfd_size_type size; 425 426 size = (bfd_size_type) sec->line_filepos * 8; 427 BFD_ASSERT (size == sec->size 428 || size + 8 == sec->size); 429 if (!bfd_set_section_size (sec, size)) 430 return NULL; 431 } 432 } 433 434 return ret; 435 } 436 437 /* See whether the magic number matches. */ 438 439 static bfd_boolean 440 alpha_ecoff_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, 441 void * filehdr) 442 { 443 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr; 444 445 if (! ALPHA_ECOFF_BADMAG (*internal_f)) 446 return TRUE; 447 448 if (ALPHA_ECOFF_COMPRESSEDMAG (*internal_f)) 449 _bfd_error_handler 450 (_("%pB: cannot handle compressed Alpha binaries; " 451 "use compiler flags, or objZ, to generate uncompressed binaries"), 452 abfd); 453 454 return FALSE; 455 } 456 457 /* This is a hook called by coff_real_object_p to create any backend 458 specific information. */ 459 460 static void * 461 alpha_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr) 462 { 463 void * ecoff; 464 465 ecoff = _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr); 466 467 if (ecoff != NULL) 468 { 469 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr; 470 471 /* Set additional BFD flags according to the object type from the 472 machine specific file header flags. */ 473 switch (internal_f->f_flags & F_ALPHA_OBJECT_TYPE_MASK) 474 { 475 case F_ALPHA_SHARABLE: 476 abfd->flags |= DYNAMIC; 477 break; 478 case F_ALPHA_CALL_SHARED: 479 /* Always executable if using shared libraries as the run time 480 loader might resolve undefined references. */ 481 abfd->flags |= (DYNAMIC | EXEC_P); 482 break; 483 } 484 } 485 return ecoff; 486 } 487 488 /* Reloc handling. */ 489 490 /* Swap a reloc in. */ 491 492 static void 493 alpha_ecoff_swap_reloc_in (bfd *abfd, 494 void * ext_ptr, 495 struct internal_reloc *intern) 496 { 497 const RELOC *ext = (RELOC *) ext_ptr; 498 499 intern->r_vaddr = H_GET_64 (abfd, ext->r_vaddr); 500 intern->r_symndx = H_GET_32 (abfd, ext->r_symndx); 501 502 BFD_ASSERT (bfd_header_little_endian (abfd)); 503 504 intern->r_type = ((ext->r_bits[0] & RELOC_BITS0_TYPE_LITTLE) 505 >> RELOC_BITS0_TYPE_SH_LITTLE); 506 intern->r_extern = (ext->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0; 507 intern->r_offset = ((ext->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE) 508 >> RELOC_BITS1_OFFSET_SH_LITTLE); 509 /* Ignored the reserved bits. */ 510 intern->r_size = ((ext->r_bits[3] & RELOC_BITS3_SIZE_LITTLE) 511 >> RELOC_BITS3_SIZE_SH_LITTLE); 512 513 if (intern->r_type == ALPHA_R_LITUSE 514 || intern->r_type == ALPHA_R_GPDISP) 515 { 516 /* Handle the LITUSE and GPDISP relocs specially. Its symndx 517 value is not actually a symbol index, but is instead a 518 special code. We put the code in the r_size field, and 519 clobber the symndx. */ 520 if (intern->r_size != 0) 521 abort (); 522 intern->r_size = intern->r_symndx; 523 intern->r_symndx = RELOC_SECTION_NONE; 524 } 525 else if (intern->r_type == ALPHA_R_IGNORE) 526 { 527 /* The IGNORE reloc generally follows a GPDISP reloc, and is 528 against the .lita section. The section is irrelevant. */ 529 if (! intern->r_extern && 530 intern->r_symndx == RELOC_SECTION_ABS) 531 abort (); 532 if (! intern->r_extern && intern->r_symndx == RELOC_SECTION_LITA) 533 intern->r_symndx = RELOC_SECTION_ABS; 534 } 535 } 536 537 /* Swap a reloc out. */ 538 539 static void 540 alpha_ecoff_swap_reloc_out (bfd *abfd, 541 const struct internal_reloc *intern, 542 void * dst) 543 { 544 RELOC *ext = (RELOC *) dst; 545 long symndx; 546 unsigned char size; 547 548 /* Undo the hackery done in swap_reloc_in. */ 549 if (intern->r_type == ALPHA_R_LITUSE 550 || intern->r_type == ALPHA_R_GPDISP) 551 { 552 symndx = intern->r_size; 553 size = 0; 554 } 555 else if (intern->r_type == ALPHA_R_IGNORE 556 && ! intern->r_extern 557 && intern->r_symndx == RELOC_SECTION_ABS) 558 { 559 symndx = RELOC_SECTION_LITA; 560 size = intern->r_size; 561 } 562 else 563 { 564 symndx = intern->r_symndx; 565 size = intern->r_size; 566 } 567 568 /* XXX FIXME: The maximum symndx value used to be 14 but this 569 fails with object files produced by DEC's C++ compiler. 570 Where does the value 14 (or 15) come from anyway ? */ 571 BFD_ASSERT (intern->r_extern 572 || (intern->r_symndx >= 0 && intern->r_symndx <= 15)); 573 574 H_PUT_64 (abfd, intern->r_vaddr, ext->r_vaddr); 575 H_PUT_32 (abfd, symndx, ext->r_symndx); 576 577 BFD_ASSERT (bfd_header_little_endian (abfd)); 578 579 ext->r_bits[0] = ((intern->r_type << RELOC_BITS0_TYPE_SH_LITTLE) 580 & RELOC_BITS0_TYPE_LITTLE); 581 ext->r_bits[1] = ((intern->r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0) 582 | ((intern->r_offset << RELOC_BITS1_OFFSET_SH_LITTLE) 583 & RELOC_BITS1_OFFSET_LITTLE)); 584 ext->r_bits[2] = 0; 585 ext->r_bits[3] = ((size << RELOC_BITS3_SIZE_SH_LITTLE) 586 & RELOC_BITS3_SIZE_LITTLE); 587 } 588 589 /* Finish canonicalizing a reloc. Part of this is generic to all 590 ECOFF targets, and that part is in ecoff.c. The rest is done in 591 this backend routine. It must fill in the howto field. */ 592 593 static void 594 alpha_adjust_reloc_in (bfd *abfd, 595 const struct internal_reloc *intern, 596 arelent *rptr) 597 { 598 if (intern->r_type > ALPHA_R_GPVALUE) 599 { 600 /* xgettext:c-format */ 601 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), 602 abfd, intern->r_type); 603 bfd_set_error (bfd_error_bad_value); 604 rptr->addend = 0; 605 rptr->howto = NULL; 606 return; 607 } 608 609 switch (intern->r_type) 610 { 611 case ALPHA_R_BRADDR: 612 case ALPHA_R_SREL16: 613 case ALPHA_R_SREL32: 614 case ALPHA_R_SREL64: 615 /* This relocs appear to be fully resolved when they are against 616 internal symbols. Against external symbols, BRADDR at least 617 appears to be resolved against the next instruction. */ 618 if (! intern->r_extern) 619 rptr->addend = 0; 620 else 621 rptr->addend = - (intern->r_vaddr + 4); 622 break; 623 624 case ALPHA_R_GPREL32: 625 case ALPHA_R_LITERAL: 626 /* Copy the gp value for this object file into the addend, to 627 ensure that we are not confused by the linker. */ 628 if (! intern->r_extern) 629 rptr->addend += ecoff_data (abfd)->gp; 630 break; 631 632 case ALPHA_R_LITUSE: 633 case ALPHA_R_GPDISP: 634 /* The LITUSE and GPDISP relocs do not use a symbol, or an 635 addend, but they do use a special code. Put this code in the 636 addend field. */ 637 rptr->addend = intern->r_size; 638 break; 639 640 case ALPHA_R_OP_STORE: 641 /* The STORE reloc needs the size and offset fields. We store 642 them in the addend. */ 643 #if 0 644 BFD_ASSERT (intern->r_offset <= 256); 645 #endif 646 rptr->addend = (intern->r_offset << 8) + intern->r_size; 647 break; 648 649 case ALPHA_R_OP_PUSH: 650 case ALPHA_R_OP_PSUB: 651 case ALPHA_R_OP_PRSHIFT: 652 /* The PUSH, PSUB and PRSHIFT relocs do not actually use an 653 address. I believe that the address supplied is really an 654 addend. */ 655 rptr->addend = intern->r_vaddr; 656 break; 657 658 case ALPHA_R_GPVALUE: 659 /* Set the addend field to the new GP value. */ 660 rptr->addend = intern->r_symndx + ecoff_data (abfd)->gp; 661 break; 662 663 case ALPHA_R_IGNORE: 664 /* If the type is ALPHA_R_IGNORE, make sure this is a reference 665 to the absolute section so that the reloc is ignored. For 666 some reason the address of this reloc type is not adjusted by 667 the section vma. We record the gp value for this object file 668 here, for convenience when doing the GPDISP relocation. */ 669 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; 670 rptr->address = intern->r_vaddr; 671 rptr->addend = ecoff_data (abfd)->gp; 672 break; 673 674 default: 675 break; 676 } 677 678 rptr->howto = &alpha_howto_table[intern->r_type]; 679 } 680 681 /* When writing out a reloc we need to pull some values back out of 682 the addend field into the reloc. This is roughly the reverse of 683 alpha_adjust_reloc_in, except that there are several changes we do 684 not need to undo. */ 685 686 static void 687 alpha_adjust_reloc_out (bfd *abfd ATTRIBUTE_UNUSED, 688 const arelent *rel, 689 struct internal_reloc *intern) 690 { 691 switch (intern->r_type) 692 { 693 case ALPHA_R_LITUSE: 694 case ALPHA_R_GPDISP: 695 intern->r_size = rel->addend; 696 break; 697 698 case ALPHA_R_OP_STORE: 699 intern->r_size = rel->addend & 0xff; 700 intern->r_offset = (rel->addend >> 8) & 0xff; 701 break; 702 703 case ALPHA_R_OP_PUSH: 704 case ALPHA_R_OP_PSUB: 705 case ALPHA_R_OP_PRSHIFT: 706 intern->r_vaddr = rel->addend; 707 break; 708 709 case ALPHA_R_IGNORE: 710 intern->r_vaddr = rel->address; 711 break; 712 713 default: 714 break; 715 } 716 } 717 718 /* The size of the stack for the relocation evaluator. */ 719 #define RELOC_STACKSIZE (10) 720 721 /* Alpha ECOFF relocs have a built in expression evaluator as well as 722 other interdependencies. Rather than use a bunch of special 723 functions and global variables, we use a single routine to do all 724 the relocation for a section. I haven't yet worked out how the 725 assembler is going to handle this. */ 726 727 static bfd_byte * 728 alpha_ecoff_get_relocated_section_contents (bfd *abfd, 729 struct bfd_link_info *link_info, 730 struct bfd_link_order *link_order, 731 bfd_byte *data, 732 bfd_boolean relocatable, 733 asymbol **symbols) 734 { 735 bfd *input_bfd = link_order->u.indirect.section->owner; 736 asection *input_section = link_order->u.indirect.section; 737 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section); 738 arelent **reloc_vector = NULL; 739 long reloc_count; 740 bfd *output_bfd = relocatable ? abfd : (bfd *) NULL; 741 bfd_vma gp; 742 bfd_size_type sz; 743 bfd_boolean gp_undefined; 744 bfd_vma stack[RELOC_STACKSIZE]; 745 int tos = 0; 746 747 if (reloc_size < 0) 748 goto error_return; 749 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size); 750 if (reloc_vector == NULL && reloc_size != 0) 751 goto error_return; 752 753 sz = input_section->rawsize ? input_section->rawsize : input_section->size; 754 if (! bfd_get_section_contents (input_bfd, input_section, data, 0, sz)) 755 goto error_return; 756 757 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section, 758 reloc_vector, symbols); 759 if (reloc_count < 0) 760 goto error_return; 761 if (reloc_count == 0) 762 goto successful_return; 763 764 /* Get the GP value for the output BFD. */ 765 gp_undefined = FALSE; 766 gp = _bfd_get_gp_value (abfd); 767 if (gp == 0) 768 { 769 if (relocatable) 770 { 771 asection *sec; 772 bfd_vma lo; 773 774 /* Make up a value. */ 775 lo = (bfd_vma) -1; 776 for (sec = abfd->sections; sec != NULL; sec = sec->next) 777 { 778 if (sec->vma < lo 779 && (strcmp (sec->name, ".sbss") == 0 780 || strcmp (sec->name, ".sdata") == 0 781 || strcmp (sec->name, ".lit4") == 0 782 || strcmp (sec->name, ".lit8") == 0 783 || strcmp (sec->name, ".lita") == 0)) 784 lo = sec->vma; 785 } 786 gp = lo + 0x8000; 787 _bfd_set_gp_value (abfd, gp); 788 } 789 else 790 { 791 struct bfd_link_hash_entry *h; 792 793 h = bfd_link_hash_lookup (link_info->hash, "_gp", FALSE, FALSE, 794 TRUE); 795 if (h == (struct bfd_link_hash_entry *) NULL 796 || h->type != bfd_link_hash_defined) 797 gp_undefined = TRUE; 798 else 799 { 800 gp = (h->u.def.value 801 + h->u.def.section->output_section->vma 802 + h->u.def.section->output_offset); 803 _bfd_set_gp_value (abfd, gp); 804 } 805 } 806 } 807 808 for (; *reloc_vector != (arelent *) NULL; reloc_vector++) 809 { 810 arelent *rel; 811 bfd_reloc_status_type r; 812 char *err; 813 814 rel = *reloc_vector; 815 r = bfd_reloc_ok; 816 switch (rel->howto->type) 817 { 818 case ALPHA_R_IGNORE: 819 rel->address += input_section->output_offset; 820 break; 821 822 case ALPHA_R_REFLONG: 823 case ALPHA_R_REFQUAD: 824 case ALPHA_R_BRADDR: 825 case ALPHA_R_HINT: 826 case ALPHA_R_SREL16: 827 case ALPHA_R_SREL32: 828 case ALPHA_R_SREL64: 829 if (relocatable 830 && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0) 831 { 832 rel->address += input_section->output_offset; 833 break; 834 } 835 r = bfd_perform_relocation (input_bfd, rel, data, input_section, 836 output_bfd, &err); 837 break; 838 839 case ALPHA_R_GPREL32: 840 /* This relocation is used in a switch table. It is a 32 841 bit offset from the current GP value. We must adjust it 842 by the different between the original GP value and the 843 current GP value. The original GP value is stored in the 844 addend. We adjust the addend and let 845 bfd_perform_relocation finish the job. */ 846 rel->addend -= gp; 847 r = bfd_perform_relocation (input_bfd, rel, data, input_section, 848 output_bfd, &err); 849 if (r == bfd_reloc_ok && gp_undefined) 850 { 851 r = bfd_reloc_dangerous; 852 err = (char *) _("GP relative relocation used when GP not defined"); 853 } 854 break; 855 856 case ALPHA_R_LITERAL: 857 /* This is a reference to a literal value, generally 858 (always?) in the .lita section. This is a 16 bit GP 859 relative relocation. Sometimes the subsequent reloc is a 860 LITUSE reloc, which indicates how this reloc is used. 861 This sometimes permits rewriting the two instructions 862 referred to by the LITERAL and the LITUSE into different 863 instructions which do not refer to .lita. This can save 864 a memory reference, and permits removing a value from 865 .lita thus saving GP relative space. 866 867 We do not these optimizations. To do them we would need 868 to arrange to link the .lita section first, so that by 869 the time we got here we would know the final values to 870 use. This would not be particularly difficult, but it is 871 not currently implemented. */ 872 873 { 874 unsigned long insn; 875 876 /* I believe that the LITERAL reloc will only apply to a 877 ldq or ldl instruction, so check my assumption. */ 878 insn = bfd_get_32 (input_bfd, data + rel->address); 879 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29 880 || ((insn >> 26) & 0x3f) == 0x28); 881 882 rel->addend -= gp; 883 r = bfd_perform_relocation (input_bfd, rel, data, input_section, 884 output_bfd, &err); 885 if (r == bfd_reloc_ok && gp_undefined) 886 { 887 r = bfd_reloc_dangerous; 888 err = 889 (char *) _("GP relative relocation used when GP not defined"); 890 } 891 } 892 break; 893 894 case ALPHA_R_LITUSE: 895 /* See ALPHA_R_LITERAL above for the uses of this reloc. It 896 does not cause anything to happen, itself. */ 897 rel->address += input_section->output_offset; 898 break; 899 900 case ALPHA_R_GPDISP: 901 /* This marks the ldah of an ldah/lda pair which loads the 902 gp register with the difference of the gp value and the 903 current location. The second of the pair is r_size bytes 904 ahead; it used to be marked with an ALPHA_R_IGNORE reloc, 905 but that no longer happens in OSF/1 3.2. */ 906 { 907 unsigned long insn1, insn2; 908 bfd_vma addend; 909 910 /* Get the two instructions. */ 911 insn1 = bfd_get_32 (input_bfd, data + rel->address); 912 insn2 = bfd_get_32 (input_bfd, data + rel->address + rel->addend); 913 914 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */ 915 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */ 916 917 /* Get the existing addend. We must account for the sign 918 extension done by lda and ldah. */ 919 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff); 920 if (insn1 & 0x8000) 921 { 922 addend -= 0x80000000; 923 addend -= 0x80000000; 924 } 925 if (insn2 & 0x8000) 926 addend -= 0x10000; 927 928 /* The existing addend includes the different between the 929 gp of the input BFD and the address in the input BFD. 930 Subtract this out. */ 931 addend -= (ecoff_data (input_bfd)->gp 932 - (input_section->vma + rel->address)); 933 934 /* Now add in the final gp value, and subtract out the 935 final address. */ 936 addend += (gp 937 - (input_section->output_section->vma 938 + input_section->output_offset 939 + rel->address)); 940 941 /* Change the instructions, accounting for the sign 942 extension, and write them out. */ 943 if (addend & 0x8000) 944 addend += 0x10000; 945 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff); 946 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff); 947 948 bfd_put_32 (input_bfd, (bfd_vma) insn1, data + rel->address); 949 bfd_put_32 (input_bfd, (bfd_vma) insn2, 950 data + rel->address + rel->addend); 951 952 rel->address += input_section->output_offset; 953 } 954 break; 955 956 case ALPHA_R_OP_PUSH: 957 /* Push a value on the reloc evaluation stack. */ 958 { 959 asymbol *symbol; 960 bfd_vma relocation; 961 962 if (relocatable) 963 { 964 rel->address += input_section->output_offset; 965 break; 966 } 967 968 /* Figure out the relocation of this symbol. */ 969 symbol = *rel->sym_ptr_ptr; 970 971 if (bfd_is_und_section (symbol->section)) 972 r = bfd_reloc_undefined; 973 974 if (bfd_is_com_section (symbol->section)) 975 relocation = 0; 976 else 977 relocation = symbol->value; 978 relocation += symbol->section->output_section->vma; 979 relocation += symbol->section->output_offset; 980 relocation += rel->addend; 981 982 if (tos >= RELOC_STACKSIZE) 983 abort (); 984 985 stack[tos++] = relocation; 986 } 987 break; 988 989 case ALPHA_R_OP_STORE: 990 /* Store a value from the reloc stack into a bitfield. */ 991 { 992 bfd_vma val; 993 int offset, size; 994 995 if (relocatable) 996 { 997 rel->address += input_section->output_offset; 998 break; 999 } 1000 1001 if (tos == 0) 1002 abort (); 1003 1004 /* The offset and size for this reloc are encoded into the 1005 addend field by alpha_adjust_reloc_in. */ 1006 offset = (rel->addend >> 8) & 0xff; 1007 size = rel->addend & 0xff; 1008 1009 val = bfd_get_64 (abfd, data + rel->address); 1010 val &=~ (((1 << size) - 1) << offset); 1011 val |= (stack[--tos] & ((1 << size) - 1)) << offset; 1012 bfd_put_64 (abfd, val, data + rel->address); 1013 } 1014 break; 1015 1016 case ALPHA_R_OP_PSUB: 1017 /* Subtract a value from the top of the stack. */ 1018 { 1019 asymbol *symbol; 1020 bfd_vma relocation; 1021 1022 if (relocatable) 1023 { 1024 rel->address += input_section->output_offset; 1025 break; 1026 } 1027 1028 /* Figure out the relocation of this symbol. */ 1029 symbol = *rel->sym_ptr_ptr; 1030 1031 if (bfd_is_und_section (symbol->section)) 1032 r = bfd_reloc_undefined; 1033 1034 if (bfd_is_com_section (symbol->section)) 1035 relocation = 0; 1036 else 1037 relocation = symbol->value; 1038 relocation += symbol->section->output_section->vma; 1039 relocation += symbol->section->output_offset; 1040 relocation += rel->addend; 1041 1042 if (tos == 0) 1043 abort (); 1044 1045 stack[tos - 1] -= relocation; 1046 } 1047 break; 1048 1049 case ALPHA_R_OP_PRSHIFT: 1050 /* Shift the value on the top of the stack. */ 1051 { 1052 asymbol *symbol; 1053 bfd_vma relocation; 1054 1055 if (relocatable) 1056 { 1057 rel->address += input_section->output_offset; 1058 break; 1059 } 1060 1061 /* Figure out the relocation of this symbol. */ 1062 symbol = *rel->sym_ptr_ptr; 1063 1064 if (bfd_is_und_section (symbol->section)) 1065 r = bfd_reloc_undefined; 1066 1067 if (bfd_is_com_section (symbol->section)) 1068 relocation = 0; 1069 else 1070 relocation = symbol->value; 1071 relocation += symbol->section->output_section->vma; 1072 relocation += symbol->section->output_offset; 1073 relocation += rel->addend; 1074 1075 if (tos == 0) 1076 abort (); 1077 1078 stack[tos - 1] >>= relocation; 1079 } 1080 break; 1081 1082 case ALPHA_R_GPVALUE: 1083 /* I really don't know if this does the right thing. */ 1084 gp = rel->addend; 1085 gp_undefined = FALSE; 1086 break; 1087 1088 default: 1089 abort (); 1090 } 1091 1092 if (relocatable) 1093 { 1094 asection *os = input_section->output_section; 1095 1096 /* A partial link, so keep the relocs. */ 1097 os->orelocation[os->reloc_count] = rel; 1098 os->reloc_count++; 1099 } 1100 1101 if (r != bfd_reloc_ok) 1102 { 1103 switch (r) 1104 { 1105 case bfd_reloc_undefined: 1106 (*link_info->callbacks->undefined_symbol) 1107 (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr), 1108 input_bfd, input_section, rel->address, TRUE); 1109 break; 1110 case bfd_reloc_dangerous: 1111 (*link_info->callbacks->reloc_dangerous) 1112 (link_info, err, input_bfd, input_section, rel->address); 1113 break; 1114 case bfd_reloc_overflow: 1115 (*link_info->callbacks->reloc_overflow) 1116 (link_info, NULL, bfd_asymbol_name (*rel->sym_ptr_ptr), 1117 rel->howto->name, rel->addend, input_bfd, 1118 input_section, rel->address); 1119 break; 1120 case bfd_reloc_outofrange: 1121 default: 1122 abort (); 1123 break; 1124 } 1125 } 1126 } 1127 1128 if (tos != 0) 1129 abort (); 1130 1131 successful_return: 1132 if (reloc_vector != NULL) 1133 free (reloc_vector); 1134 return data; 1135 1136 error_return: 1137 if (reloc_vector != NULL) 1138 free (reloc_vector); 1139 return NULL; 1140 } 1141 1142 /* Get the howto structure for a generic reloc type. */ 1143 1144 static reloc_howto_type * 1145 alpha_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 1146 bfd_reloc_code_real_type code) 1147 { 1148 int alpha_type; 1149 1150 switch (code) 1151 { 1152 case BFD_RELOC_32: 1153 alpha_type = ALPHA_R_REFLONG; 1154 break; 1155 case BFD_RELOC_64: 1156 case BFD_RELOC_CTOR: 1157 alpha_type = ALPHA_R_REFQUAD; 1158 break; 1159 case BFD_RELOC_GPREL32: 1160 alpha_type = ALPHA_R_GPREL32; 1161 break; 1162 case BFD_RELOC_ALPHA_LITERAL: 1163 alpha_type = ALPHA_R_LITERAL; 1164 break; 1165 case BFD_RELOC_ALPHA_LITUSE: 1166 alpha_type = ALPHA_R_LITUSE; 1167 break; 1168 case BFD_RELOC_ALPHA_GPDISP_HI16: 1169 alpha_type = ALPHA_R_GPDISP; 1170 break; 1171 case BFD_RELOC_ALPHA_GPDISP_LO16: 1172 alpha_type = ALPHA_R_IGNORE; 1173 break; 1174 case BFD_RELOC_23_PCREL_S2: 1175 alpha_type = ALPHA_R_BRADDR; 1176 break; 1177 case BFD_RELOC_ALPHA_HINT: 1178 alpha_type = ALPHA_R_HINT; 1179 break; 1180 case BFD_RELOC_16_PCREL: 1181 alpha_type = ALPHA_R_SREL16; 1182 break; 1183 case BFD_RELOC_32_PCREL: 1184 alpha_type = ALPHA_R_SREL32; 1185 break; 1186 case BFD_RELOC_64_PCREL: 1187 alpha_type = ALPHA_R_SREL64; 1188 break; 1189 default: 1190 return (reloc_howto_type *) NULL; 1191 } 1192 1193 return &alpha_howto_table[alpha_type]; 1194 } 1195 1196 static reloc_howto_type * 1197 alpha_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 1198 const char *r_name) 1199 { 1200 unsigned int i; 1201 1202 for (i = 0; 1203 i < sizeof (alpha_howto_table) / sizeof (alpha_howto_table[0]); 1204 i++) 1205 if (alpha_howto_table[i].name != NULL 1206 && strcasecmp (alpha_howto_table[i].name, r_name) == 0) 1207 return &alpha_howto_table[i]; 1208 1209 return NULL; 1210 } 1211 1212 /* A helper routine for alpha_relocate_section which converts an 1213 external reloc when generating relocatable output. Returns the 1214 relocation amount. */ 1215 1216 static bfd_vma 1217 alpha_convert_external_reloc (bfd *output_bfd ATTRIBUTE_UNUSED, 1218 struct bfd_link_info *info, 1219 bfd *input_bfd, 1220 struct external_reloc *ext_rel, 1221 struct ecoff_link_hash_entry *h) 1222 { 1223 unsigned long r_symndx; 1224 bfd_vma relocation; 1225 1226 BFD_ASSERT (bfd_link_relocatable (info)); 1227 1228 if (h->root.type == bfd_link_hash_defined 1229 || h->root.type == bfd_link_hash_defweak) 1230 { 1231 asection *hsec; 1232 const char *name; 1233 1234 /* This symbol is defined in the output. Convert the reloc from 1235 being against the symbol to being against the section. */ 1236 1237 /* Clear the r_extern bit. */ 1238 ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE; 1239 1240 /* Compute a new r_symndx value. */ 1241 hsec = h->root.u.def.section; 1242 name = bfd_section_name (hsec->output_section); 1243 1244 r_symndx = (unsigned long) -1; 1245 switch (name[1]) 1246 { 1247 case 'A': 1248 if (strcmp (name, "*ABS*") == 0) 1249 r_symndx = RELOC_SECTION_ABS; 1250 break; 1251 case 'b': 1252 if (strcmp (name, ".bss") == 0) 1253 r_symndx = RELOC_SECTION_BSS; 1254 break; 1255 case 'd': 1256 if (strcmp (name, ".data") == 0) 1257 r_symndx = RELOC_SECTION_DATA; 1258 break; 1259 case 'f': 1260 if (strcmp (name, ".fini") == 0) 1261 r_symndx = RELOC_SECTION_FINI; 1262 break; 1263 case 'i': 1264 if (strcmp (name, ".init") == 0) 1265 r_symndx = RELOC_SECTION_INIT; 1266 break; 1267 case 'l': 1268 if (strcmp (name, ".lita") == 0) 1269 r_symndx = RELOC_SECTION_LITA; 1270 else if (strcmp (name, ".lit8") == 0) 1271 r_symndx = RELOC_SECTION_LIT8; 1272 else if (strcmp (name, ".lit4") == 0) 1273 r_symndx = RELOC_SECTION_LIT4; 1274 break; 1275 case 'p': 1276 if (strcmp (name, ".pdata") == 0) 1277 r_symndx = RELOC_SECTION_PDATA; 1278 break; 1279 case 'r': 1280 if (strcmp (name, ".rdata") == 0) 1281 r_symndx = RELOC_SECTION_RDATA; 1282 else if (strcmp (name, ".rconst") == 0) 1283 r_symndx = RELOC_SECTION_RCONST; 1284 break; 1285 case 's': 1286 if (strcmp (name, ".sdata") == 0) 1287 r_symndx = RELOC_SECTION_SDATA; 1288 else if (strcmp (name, ".sbss") == 0) 1289 r_symndx = RELOC_SECTION_SBSS; 1290 break; 1291 case 't': 1292 if (strcmp (name, ".text") == 0) 1293 r_symndx = RELOC_SECTION_TEXT; 1294 break; 1295 case 'x': 1296 if (strcmp (name, ".xdata") == 0) 1297 r_symndx = RELOC_SECTION_XDATA; 1298 break; 1299 } 1300 1301 if (r_symndx == (unsigned long) -1) 1302 abort (); 1303 1304 /* Add the section VMA and the symbol value. */ 1305 relocation = (h->root.u.def.value 1306 + hsec->output_section->vma 1307 + hsec->output_offset); 1308 } 1309 else 1310 { 1311 /* Change the symndx value to the right one for 1312 the output BFD. */ 1313 r_symndx = h->indx; 1314 if (r_symndx == (unsigned long) -1) 1315 { 1316 /* Caller must give an error. */ 1317 r_symndx = 0; 1318 } 1319 relocation = 0; 1320 } 1321 1322 /* Write out the new r_symndx value. */ 1323 H_PUT_32 (input_bfd, r_symndx, ext_rel->r_symndx); 1324 1325 return relocation; 1326 } 1327 1328 /* Relocate a section while linking an Alpha ECOFF file. This is 1329 quite similar to get_relocated_section_contents. Perhaps they 1330 could be combined somehow. */ 1331 1332 static bfd_boolean 1333 alpha_relocate_section (bfd *output_bfd, 1334 struct bfd_link_info *info, 1335 bfd *input_bfd, 1336 asection *input_section, 1337 bfd_byte *contents, 1338 void * external_relocs) 1339 { 1340 asection **symndx_to_section, *lita_sec; 1341 struct ecoff_link_hash_entry **sym_hashes; 1342 bfd_vma gp; 1343 bfd_boolean gp_undefined; 1344 bfd_vma stack[RELOC_STACKSIZE]; 1345 int tos = 0; 1346 struct external_reloc *ext_rel; 1347 struct external_reloc *ext_rel_end; 1348 bfd_size_type amt; 1349 1350 /* We keep a table mapping the symndx found in an internal reloc to 1351 the appropriate section. This is faster than looking up the 1352 section by name each time. */ 1353 symndx_to_section = ecoff_data (input_bfd)->symndx_to_section; 1354 if (symndx_to_section == (asection **) NULL) 1355 { 1356 amt = NUM_RELOC_SECTIONS * sizeof (asection *); 1357 symndx_to_section = (asection **) bfd_alloc (input_bfd, amt); 1358 if (!symndx_to_section) 1359 return FALSE; 1360 1361 symndx_to_section[RELOC_SECTION_NONE] = NULL; 1362 symndx_to_section[RELOC_SECTION_TEXT] = 1363 bfd_get_section_by_name (input_bfd, ".text"); 1364 symndx_to_section[RELOC_SECTION_RDATA] = 1365 bfd_get_section_by_name (input_bfd, ".rdata"); 1366 symndx_to_section[RELOC_SECTION_DATA] = 1367 bfd_get_section_by_name (input_bfd, ".data"); 1368 symndx_to_section[RELOC_SECTION_SDATA] = 1369 bfd_get_section_by_name (input_bfd, ".sdata"); 1370 symndx_to_section[RELOC_SECTION_SBSS] = 1371 bfd_get_section_by_name (input_bfd, ".sbss"); 1372 symndx_to_section[RELOC_SECTION_BSS] = 1373 bfd_get_section_by_name (input_bfd, ".bss"); 1374 symndx_to_section[RELOC_SECTION_INIT] = 1375 bfd_get_section_by_name (input_bfd, ".init"); 1376 symndx_to_section[RELOC_SECTION_LIT8] = 1377 bfd_get_section_by_name (input_bfd, ".lit8"); 1378 symndx_to_section[RELOC_SECTION_LIT4] = 1379 bfd_get_section_by_name (input_bfd, ".lit4"); 1380 symndx_to_section[RELOC_SECTION_XDATA] = 1381 bfd_get_section_by_name (input_bfd, ".xdata"); 1382 symndx_to_section[RELOC_SECTION_PDATA] = 1383 bfd_get_section_by_name (input_bfd, ".pdata"); 1384 symndx_to_section[RELOC_SECTION_FINI] = 1385 bfd_get_section_by_name (input_bfd, ".fini"); 1386 symndx_to_section[RELOC_SECTION_LITA] = 1387 bfd_get_section_by_name (input_bfd, ".lita"); 1388 symndx_to_section[RELOC_SECTION_ABS] = bfd_abs_section_ptr; 1389 symndx_to_section[RELOC_SECTION_RCONST] = 1390 bfd_get_section_by_name (input_bfd, ".rconst"); 1391 1392 ecoff_data (input_bfd)->symndx_to_section = symndx_to_section; 1393 } 1394 1395 sym_hashes = ecoff_data (input_bfd)->sym_hashes; 1396 1397 /* On the Alpha, the .lita section must be addressable by the global 1398 pointer. To support large programs, we need to allow multiple 1399 global pointers. This works as long as each input .lita section 1400 is <64KB big. This implies that when producing relocatable 1401 output, the .lita section is limited to 64KB. . */ 1402 1403 lita_sec = symndx_to_section[RELOC_SECTION_LITA]; 1404 gp = _bfd_get_gp_value (output_bfd); 1405 if (! bfd_link_relocatable (info) && lita_sec != NULL) 1406 { 1407 struct ecoff_section_tdata *lita_sec_data; 1408 1409 /* Make sure we have a section data structure to which we can 1410 hang on to the gp value we pick for the section. */ 1411 lita_sec_data = ecoff_section_data (input_bfd, lita_sec); 1412 if (lita_sec_data == NULL) 1413 { 1414 amt = sizeof (struct ecoff_section_tdata); 1415 lita_sec_data = ((struct ecoff_section_tdata *) 1416 bfd_zalloc (input_bfd, amt)); 1417 lita_sec->used_by_bfd = lita_sec_data; 1418 } 1419 1420 if (lita_sec_data->gp != 0) 1421 { 1422 /* If we already assigned a gp to this section, we better 1423 stick with that value. */ 1424 gp = lita_sec_data->gp; 1425 } 1426 else 1427 { 1428 bfd_vma lita_vma; 1429 bfd_size_type lita_size; 1430 1431 lita_vma = lita_sec->output_offset + lita_sec->output_section->vma; 1432 lita_size = lita_sec->size; 1433 1434 if (gp == 0 1435 || lita_vma < gp - 0x8000 1436 || lita_vma + lita_size >= gp + 0x8000) 1437 { 1438 /* Either gp hasn't been set at all or the current gp 1439 cannot address this .lita section. In both cases we 1440 reset the gp to point into the "middle" of the 1441 current input .lita section. */ 1442 if (gp && !ecoff_data (output_bfd)->issued_multiple_gp_warning) 1443 { 1444 (*info->callbacks->warning) (info, 1445 _("using multiple gp values"), 1446 (char *) NULL, output_bfd, 1447 (asection *) NULL, (bfd_vma) 0); 1448 ecoff_data (output_bfd)->issued_multiple_gp_warning = TRUE; 1449 } 1450 if (lita_vma < gp - 0x8000) 1451 gp = lita_vma + lita_size - 0x8000; 1452 else 1453 gp = lita_vma + 0x8000; 1454 1455 } 1456 1457 lita_sec_data->gp = gp; 1458 } 1459 1460 _bfd_set_gp_value (output_bfd, gp); 1461 } 1462 1463 gp_undefined = (gp == 0); 1464 1465 BFD_ASSERT (bfd_header_little_endian (output_bfd)); 1466 BFD_ASSERT (bfd_header_little_endian (input_bfd)); 1467 1468 ext_rel = (struct external_reloc *) external_relocs; 1469 ext_rel_end = ext_rel + input_section->reloc_count; 1470 for (; ext_rel < ext_rel_end; ext_rel++) 1471 { 1472 bfd_vma r_vaddr; 1473 unsigned long r_symndx; 1474 int r_type; 1475 int r_extern; 1476 int r_offset; 1477 int r_size; 1478 bfd_boolean relocatep; 1479 bfd_boolean adjust_addrp; 1480 bfd_boolean gp_usedp; 1481 bfd_vma addend; 1482 1483 r_vaddr = H_GET_64 (input_bfd, ext_rel->r_vaddr); 1484 r_symndx = H_GET_32 (input_bfd, ext_rel->r_symndx); 1485 1486 r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE) 1487 >> RELOC_BITS0_TYPE_SH_LITTLE); 1488 r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0; 1489 r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE) 1490 >> RELOC_BITS1_OFFSET_SH_LITTLE); 1491 /* Ignored the reserved bits. */ 1492 r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE) 1493 >> RELOC_BITS3_SIZE_SH_LITTLE); 1494 1495 relocatep = FALSE; 1496 adjust_addrp = TRUE; 1497 gp_usedp = FALSE; 1498 addend = 0; 1499 1500 switch (r_type) 1501 { 1502 case ALPHA_R_GPRELHIGH: 1503 _bfd_error_handler (_("%pB: %s unsupported"), 1504 input_bfd, "ALPHA_R_GPRELHIGH"); 1505 bfd_set_error (bfd_error_bad_value); 1506 continue; 1507 1508 case ALPHA_R_GPRELLOW: 1509 _bfd_error_handler (_("%pB: %s unsupported"), 1510 input_bfd, "ALPHA_R_GPRELLOW"); 1511 bfd_set_error (bfd_error_bad_value); 1512 continue; 1513 1514 default: 1515 /* xgettext:c-format */ 1516 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), 1517 input_bfd, (int) r_type); 1518 bfd_set_error (bfd_error_bad_value); 1519 continue; 1520 1521 case ALPHA_R_IGNORE: 1522 /* This reloc appears after a GPDISP reloc. On earlier 1523 versions of OSF/1, It marked the position of the second 1524 instruction to be altered by the GPDISP reloc, but it is 1525 not otherwise used for anything. For some reason, the 1526 address of the relocation does not appear to include the 1527 section VMA, unlike the other relocation types. */ 1528 if (bfd_link_relocatable (info)) 1529 H_PUT_64 (input_bfd, input_section->output_offset + r_vaddr, 1530 ext_rel->r_vaddr); 1531 adjust_addrp = FALSE; 1532 break; 1533 1534 case ALPHA_R_REFLONG: 1535 case ALPHA_R_REFQUAD: 1536 case ALPHA_R_HINT: 1537 relocatep = TRUE; 1538 break; 1539 1540 case ALPHA_R_BRADDR: 1541 case ALPHA_R_SREL16: 1542 case ALPHA_R_SREL32: 1543 case ALPHA_R_SREL64: 1544 if (r_extern) 1545 addend += - (r_vaddr + 4); 1546 relocatep = TRUE; 1547 break; 1548 1549 case ALPHA_R_GPREL32: 1550 /* This relocation is used in a switch table. It is a 32 1551 bit offset from the current GP value. We must adjust it 1552 by the different between the original GP value and the 1553 current GP value. */ 1554 relocatep = TRUE; 1555 addend = ecoff_data (input_bfd)->gp - gp; 1556 gp_usedp = TRUE; 1557 break; 1558 1559 case ALPHA_R_LITERAL: 1560 /* This is a reference to a literal value, generally 1561 (always?) in the .lita section. This is a 16 bit GP 1562 relative relocation. Sometimes the subsequent reloc is a 1563 LITUSE reloc, which indicates how this reloc is used. 1564 This sometimes permits rewriting the two instructions 1565 referred to by the LITERAL and the LITUSE into different 1566 instructions which do not refer to .lita. This can save 1567 a memory reference, and permits removing a value from 1568 .lita thus saving GP relative space. 1569 1570 We do not these optimizations. To do them we would need 1571 to arrange to link the .lita section first, so that by 1572 the time we got here we would know the final values to 1573 use. This would not be particularly difficult, but it is 1574 not currently implemented. */ 1575 1576 /* I believe that the LITERAL reloc will only apply to a ldq 1577 or ldl instruction, so check my assumption. */ 1578 { 1579 unsigned long insn; 1580 1581 insn = bfd_get_32 (input_bfd, 1582 contents + r_vaddr - input_section->vma); 1583 BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29 1584 || ((insn >> 26) & 0x3f) == 0x28); 1585 } 1586 1587 relocatep = TRUE; 1588 addend = ecoff_data (input_bfd)->gp - gp; 1589 gp_usedp = TRUE; 1590 break; 1591 1592 case ALPHA_R_LITUSE: 1593 /* See ALPHA_R_LITERAL above for the uses of this reloc. It 1594 does not cause anything to happen, itself. */ 1595 break; 1596 1597 case ALPHA_R_GPDISP: 1598 /* This marks the ldah of an ldah/lda pair which loads the 1599 gp register with the difference of the gp value and the 1600 current location. The second of the pair is r_symndx 1601 bytes ahead. It used to be marked with an ALPHA_R_IGNORE 1602 reloc, but OSF/1 3.2 no longer does that. */ 1603 { 1604 unsigned long insn1, insn2; 1605 1606 /* Get the two instructions. */ 1607 insn1 = bfd_get_32 (input_bfd, 1608 contents + r_vaddr - input_section->vma); 1609 insn2 = bfd_get_32 (input_bfd, 1610 (contents 1611 + r_vaddr 1612 - input_section->vma 1613 + r_symndx)); 1614 1615 BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */ 1616 BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */ 1617 1618 /* Get the existing addend. We must account for the sign 1619 extension done by lda and ldah. */ 1620 addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff); 1621 if (insn1 & 0x8000) 1622 { 1623 /* This is addend -= 0x100000000 without causing an 1624 integer overflow on a 32 bit host. */ 1625 addend -= 0x80000000; 1626 addend -= 0x80000000; 1627 } 1628 if (insn2 & 0x8000) 1629 addend -= 0x10000; 1630 1631 /* The existing addend includes the difference between the 1632 gp of the input BFD and the address in the input BFD. 1633 We want to change this to the difference between the 1634 final GP and the final address. */ 1635 addend += (gp 1636 - ecoff_data (input_bfd)->gp 1637 + input_section->vma 1638 - (input_section->output_section->vma 1639 + input_section->output_offset)); 1640 1641 /* Change the instructions, accounting for the sign 1642 extension, and write them out. */ 1643 if (addend & 0x8000) 1644 addend += 0x10000; 1645 insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff); 1646 insn2 = (insn2 & 0xffff0000) | (addend & 0xffff); 1647 1648 bfd_put_32 (input_bfd, (bfd_vma) insn1, 1649 contents + r_vaddr - input_section->vma); 1650 bfd_put_32 (input_bfd, (bfd_vma) insn2, 1651 contents + r_vaddr - input_section->vma + r_symndx); 1652 1653 gp_usedp = TRUE; 1654 } 1655 break; 1656 1657 case ALPHA_R_OP_PUSH: 1658 case ALPHA_R_OP_PSUB: 1659 case ALPHA_R_OP_PRSHIFT: 1660 /* Manipulate values on the reloc evaluation stack. The 1661 r_vaddr field is not an address in input_section, it is 1662 the current value (including any addend) of the object 1663 being used. */ 1664 if (! r_extern) 1665 { 1666 asection *s; 1667 1668 s = symndx_to_section[r_symndx]; 1669 if (s == (asection *) NULL) 1670 abort (); 1671 addend = s->output_section->vma + s->output_offset - s->vma; 1672 } 1673 else 1674 { 1675 struct ecoff_link_hash_entry *h; 1676 1677 h = sym_hashes[r_symndx]; 1678 if (h == (struct ecoff_link_hash_entry *) NULL) 1679 abort (); 1680 1681 if (! bfd_link_relocatable (info)) 1682 { 1683 if (h->root.type == bfd_link_hash_defined 1684 || h->root.type == bfd_link_hash_defweak) 1685 addend = (h->root.u.def.value 1686 + h->root.u.def.section->output_section->vma 1687 + h->root.u.def.section->output_offset); 1688 else 1689 { 1690 /* Note that we pass the address as 0, since we 1691 do not have a meaningful number for the 1692 location within the section that is being 1693 relocated. */ 1694 (*info->callbacks->undefined_symbol) 1695 (info, h->root.root.string, input_bfd, 1696 input_section, (bfd_vma) 0, TRUE); 1697 addend = 0; 1698 } 1699 } 1700 else 1701 { 1702 if (h->root.type != bfd_link_hash_defined 1703 && h->root.type != bfd_link_hash_defweak 1704 && h->indx == -1) 1705 { 1706 /* This symbol is not being written out. Pass 1707 the address as 0, as with undefined_symbol, 1708 above. */ 1709 (*info->callbacks->unattached_reloc) 1710 (info, h->root.root.string, 1711 input_bfd, input_section, (bfd_vma) 0); 1712 } 1713 1714 addend = alpha_convert_external_reloc (output_bfd, info, 1715 input_bfd, 1716 ext_rel, h); 1717 } 1718 } 1719 1720 addend += r_vaddr; 1721 1722 if (bfd_link_relocatable (info)) 1723 { 1724 /* Adjust r_vaddr by the addend. */ 1725 H_PUT_64 (input_bfd, addend, ext_rel->r_vaddr); 1726 } 1727 else 1728 { 1729 switch (r_type) 1730 { 1731 case ALPHA_R_OP_PUSH: 1732 if (tos >= RELOC_STACKSIZE) 1733 abort (); 1734 stack[tos++] = addend; 1735 break; 1736 1737 case ALPHA_R_OP_PSUB: 1738 if (tos == 0) 1739 abort (); 1740 stack[tos - 1] -= addend; 1741 break; 1742 1743 case ALPHA_R_OP_PRSHIFT: 1744 if (tos == 0) 1745 abort (); 1746 stack[tos - 1] >>= addend; 1747 break; 1748 } 1749 } 1750 1751 adjust_addrp = FALSE; 1752 break; 1753 1754 case ALPHA_R_OP_STORE: 1755 /* Store a value from the reloc stack into a bitfield. If 1756 we are generating relocatable output, all we do is 1757 adjust the address of the reloc. */ 1758 if (! bfd_link_relocatable (info)) 1759 { 1760 bfd_vma mask; 1761 bfd_vma val; 1762 1763 if (tos == 0) 1764 abort (); 1765 1766 /* Get the relocation mask. The separate steps and the 1767 casts to bfd_vma are attempts to avoid a bug in the 1768 Alpha OSF 1.3 C compiler. See reloc.c for more 1769 details. */ 1770 mask = 1; 1771 mask <<= (bfd_vma) r_size; 1772 mask -= 1; 1773 1774 /* FIXME: I don't know what kind of overflow checking, 1775 if any, should be done here. */ 1776 val = bfd_get_64 (input_bfd, 1777 contents + r_vaddr - input_section->vma); 1778 val &=~ mask << (bfd_vma) r_offset; 1779 val |= (stack[--tos] & mask) << (bfd_vma) r_offset; 1780 bfd_put_64 (input_bfd, val, 1781 contents + r_vaddr - input_section->vma); 1782 } 1783 break; 1784 1785 case ALPHA_R_GPVALUE: 1786 /* I really don't know if this does the right thing. */ 1787 gp = ecoff_data (input_bfd)->gp + r_symndx; 1788 gp_undefined = FALSE; 1789 break; 1790 } 1791 1792 if (relocatep) 1793 { 1794 reloc_howto_type *howto; 1795 struct ecoff_link_hash_entry *h = NULL; 1796 asection *s = NULL; 1797 bfd_vma relocation; 1798 bfd_reloc_status_type r; 1799 1800 /* Perform a relocation. */ 1801 1802 howto = &alpha_howto_table[r_type]; 1803 1804 if (r_extern) 1805 { 1806 h = sym_hashes[r_symndx]; 1807 /* If h is NULL, that means that there is a reloc 1808 against an external symbol which we thought was just 1809 a debugging symbol. This should not happen. */ 1810 if (h == (struct ecoff_link_hash_entry *) NULL) 1811 abort (); 1812 } 1813 else 1814 { 1815 if (r_symndx >= NUM_RELOC_SECTIONS) 1816 s = NULL; 1817 else 1818 s = symndx_to_section[r_symndx]; 1819 1820 if (s == (asection *) NULL) 1821 abort (); 1822 } 1823 1824 if (bfd_link_relocatable (info)) 1825 { 1826 /* We are generating relocatable output, and must 1827 convert the existing reloc. */ 1828 if (r_extern) 1829 { 1830 if (h->root.type != bfd_link_hash_defined 1831 && h->root.type != bfd_link_hash_defweak 1832 && h->indx == -1) 1833 { 1834 /* This symbol is not being written out. */ 1835 (*info->callbacks->unattached_reloc) 1836 (info, h->root.root.string, input_bfd, 1837 input_section, r_vaddr - input_section->vma); 1838 } 1839 1840 relocation = alpha_convert_external_reloc (output_bfd, 1841 info, 1842 input_bfd, 1843 ext_rel, 1844 h); 1845 } 1846 else 1847 { 1848 /* This is a relocation against a section. Adjust 1849 the value by the amount the section moved. */ 1850 relocation = (s->output_section->vma 1851 + s->output_offset 1852 - s->vma); 1853 } 1854 1855 /* If this is PC relative, the existing object file 1856 appears to already have the reloc worked out. We 1857 must subtract out the old value and add in the new 1858 one. */ 1859 if (howto->pc_relative) 1860 relocation -= (input_section->output_section->vma 1861 + input_section->output_offset 1862 - input_section->vma); 1863 1864 /* Put in any addend. */ 1865 relocation += addend; 1866 1867 /* Adjust the contents. */ 1868 r = _bfd_relocate_contents (howto, input_bfd, relocation, 1869 (contents 1870 + r_vaddr 1871 - input_section->vma)); 1872 } 1873 else 1874 { 1875 /* We are producing a final executable. */ 1876 if (r_extern) 1877 { 1878 /* This is a reloc against a symbol. */ 1879 if (h->root.type == bfd_link_hash_defined 1880 || h->root.type == bfd_link_hash_defweak) 1881 { 1882 asection *hsec; 1883 1884 hsec = h->root.u.def.section; 1885 relocation = (h->root.u.def.value 1886 + hsec->output_section->vma 1887 + hsec->output_offset); 1888 } 1889 else 1890 { 1891 (*info->callbacks->undefined_symbol) 1892 (info, h->root.root.string, input_bfd, input_section, 1893 r_vaddr - input_section->vma, TRUE); 1894 relocation = 0; 1895 } 1896 } 1897 else 1898 { 1899 /* This is a reloc against a section. */ 1900 relocation = (s->output_section->vma 1901 + s->output_offset 1902 - s->vma); 1903 1904 /* Adjust a PC relative relocation by removing the 1905 reference to the original source section. */ 1906 if (howto->pc_relative) 1907 relocation += input_section->vma; 1908 } 1909 1910 r = _bfd_final_link_relocate (howto, 1911 input_bfd, 1912 input_section, 1913 contents, 1914 r_vaddr - input_section->vma, 1915 relocation, 1916 addend); 1917 } 1918 1919 if (r != bfd_reloc_ok) 1920 { 1921 switch (r) 1922 { 1923 default: 1924 case bfd_reloc_outofrange: 1925 abort (); 1926 case bfd_reloc_overflow: 1927 { 1928 const char *name; 1929 1930 if (r_extern) 1931 name = sym_hashes[r_symndx]->root.root.string; 1932 else 1933 name = bfd_section_name (symndx_to_section[r_symndx]); 1934 (*info->callbacks->reloc_overflow) 1935 (info, NULL, name, alpha_howto_table[r_type].name, 1936 (bfd_vma) 0, input_bfd, input_section, 1937 r_vaddr - input_section->vma); 1938 } 1939 break; 1940 } 1941 } 1942 } 1943 1944 if (bfd_link_relocatable (info) && adjust_addrp) 1945 { 1946 /* Change the address of the relocation. */ 1947 H_PUT_64 (input_bfd, 1948 (input_section->output_section->vma 1949 + input_section->output_offset 1950 - input_section->vma 1951 + r_vaddr), 1952 ext_rel->r_vaddr); 1953 } 1954 1955 if (gp_usedp && gp_undefined) 1956 { 1957 (*info->callbacks->reloc_dangerous) 1958 (info, _("GP relative relocation used when GP not defined"), 1959 input_bfd, input_section, r_vaddr - input_section->vma); 1960 /* Only give the error once per link. */ 1961 gp = 4; 1962 _bfd_set_gp_value (output_bfd, gp); 1963 gp_undefined = FALSE; 1964 } 1965 } 1966 1967 if (tos != 0) 1968 abort (); 1969 1970 return TRUE; 1971 } 1972 1973 /* Do final adjustments to the filehdr and the aouthdr. This routine 1974 sets the dynamic bits in the file header. */ 1975 1976 static bfd_boolean 1977 alpha_adjust_headers (bfd *abfd, 1978 struct internal_filehdr *fhdr, 1979 struct internal_aouthdr *ahdr ATTRIBUTE_UNUSED) 1980 { 1981 if ((abfd->flags & (DYNAMIC | EXEC_P)) == (DYNAMIC | EXEC_P)) 1982 fhdr->f_flags |= F_ALPHA_CALL_SHARED; 1983 else if ((abfd->flags & DYNAMIC) != 0) 1984 fhdr->f_flags |= F_ALPHA_SHARABLE; 1985 return TRUE; 1986 } 1987 1988 /* Archive handling. In OSF/1 (or Digital Unix) v3.2, Digital 1989 introduced archive packing, in which the elements in an archive are 1990 optionally compressed using a simple dictionary scheme. We know 1991 how to read such archives, but we don't write them. */ 1992 1993 #define alpha_ecoff_slurp_armap _bfd_ecoff_slurp_armap 1994 #define alpha_ecoff_slurp_extended_name_table \ 1995 _bfd_ecoff_slurp_extended_name_table 1996 #define alpha_ecoff_construct_extended_name_table \ 1997 _bfd_ecoff_construct_extended_name_table 1998 #define alpha_ecoff_truncate_arname _bfd_ecoff_truncate_arname 1999 #define alpha_ecoff_write_armap _bfd_ecoff_write_armap 2000 #define alpha_ecoff_write_ar_hdr _bfd_generic_write_ar_hdr 2001 #define alpha_ecoff_generic_stat_arch_elt _bfd_ecoff_generic_stat_arch_elt 2002 #define alpha_ecoff_update_armap_timestamp _bfd_ecoff_update_armap_timestamp 2003 2004 /* A compressed file uses this instead of ARFMAG. */ 2005 2006 #define ARFZMAG "Z\012" 2007 2008 /* Read an archive header. This is like the standard routine, but it 2009 also accepts ARFZMAG. */ 2010 2011 static void * 2012 alpha_ecoff_read_ar_hdr (bfd *abfd) 2013 { 2014 struct areltdata *ret; 2015 struct ar_hdr *h; 2016 2017 ret = (struct areltdata *) _bfd_generic_read_ar_hdr_mag (abfd, ARFZMAG); 2018 if (ret == NULL) 2019 return NULL; 2020 2021 h = (struct ar_hdr *) ret->arch_header; 2022 if (strncmp (h->ar_fmag, ARFZMAG, 2) == 0) 2023 { 2024 bfd_byte ab[8]; 2025 2026 /* This is a compressed file. We must set the size correctly. 2027 The size is the eight bytes after the dummy file header. */ 2028 if (bfd_seek (abfd, (file_ptr) FILHSZ, SEEK_CUR) != 0 2029 || bfd_bread (ab, (bfd_size_type) 8, abfd) != 8 2030 || bfd_seek (abfd, (file_ptr) (- (FILHSZ + 8)), SEEK_CUR) != 0) 2031 { 2032 free (ret); 2033 return NULL; 2034 } 2035 2036 ret->parsed_size = H_GET_64 (abfd, ab); 2037 } 2038 2039 return ret; 2040 } 2041 2042 /* Get an archive element at a specified file position. This is where 2043 we uncompress the archive element if necessary. */ 2044 2045 static bfd * 2046 alpha_ecoff_get_elt_at_filepos (bfd *archive, file_ptr filepos) 2047 { 2048 bfd *nbfd = NULL; 2049 struct areltdata *tdata; 2050 struct ar_hdr *hdr; 2051 bfd_byte ab[8]; 2052 bfd_size_type size; 2053 bfd_byte *buf, *p; 2054 struct bfd_in_memory *bim; 2055 2056 buf = NULL; 2057 nbfd = _bfd_get_elt_at_filepos (archive, filepos); 2058 if (nbfd == NULL) 2059 goto error_return; 2060 2061 if ((nbfd->flags & BFD_IN_MEMORY) != 0) 2062 { 2063 /* We have already expanded this BFD. */ 2064 return nbfd; 2065 } 2066 2067 tdata = (struct areltdata *) nbfd->arelt_data; 2068 hdr = (struct ar_hdr *) tdata->arch_header; 2069 if (strncmp (hdr->ar_fmag, ARFZMAG, 2) != 0) 2070 return nbfd; 2071 2072 /* We must uncompress this element. We do this by copying it into a 2073 memory buffer, and making bfd_bread and bfd_seek use that buffer. 2074 This can use a lot of memory, but it's simpler than getting a 2075 temporary file, making that work with the file descriptor caching 2076 code, and making sure that it is deleted at all appropriate 2077 times. It can be changed if it ever becomes important. */ 2078 2079 /* The compressed file starts with a dummy ECOFF file header. */ 2080 if (bfd_seek (nbfd, (file_ptr) FILHSZ, SEEK_SET) != 0) 2081 goto error_return; 2082 2083 /* The next eight bytes are the real file size. */ 2084 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8) 2085 goto error_return; 2086 size = H_GET_64 (nbfd, ab); 2087 2088 if (size != 0) 2089 { 2090 bfd_size_type left; 2091 bfd_byte dict[4096]; 2092 unsigned int h; 2093 bfd_byte b; 2094 2095 buf = (bfd_byte *) bfd_malloc (size); 2096 if (buf == NULL) 2097 goto error_return; 2098 p = buf; 2099 2100 left = size; 2101 2102 /* I don't know what the next eight bytes are for. */ 2103 if (bfd_bread (ab, (bfd_size_type) 8, nbfd) != 8) 2104 goto error_return; 2105 2106 /* This is the uncompression algorithm. It's a simple 2107 dictionary based scheme in which each character is predicted 2108 by a hash of the previous three characters. A control byte 2109 indicates whether the character is predicted or whether it 2110 appears in the input stream; each control byte manages the 2111 next eight bytes in the output stream. */ 2112 memset (dict, 0, sizeof dict); 2113 h = 0; 2114 while (bfd_bread (&b, (bfd_size_type) 1, nbfd) == 1) 2115 { 2116 unsigned int i; 2117 2118 for (i = 0; i < 8; i++, b >>= 1) 2119 { 2120 bfd_byte n; 2121 2122 if ((b & 1) == 0) 2123 n = dict[h]; 2124 else 2125 { 2126 if (! bfd_bread (&n, (bfd_size_type) 1, nbfd)) 2127 goto error_return; 2128 dict[h] = n; 2129 } 2130 2131 *p++ = n; 2132 2133 --left; 2134 if (left == 0) 2135 break; 2136 2137 h <<= 4; 2138 h ^= n; 2139 h &= sizeof dict - 1; 2140 } 2141 2142 if (left == 0) 2143 break; 2144 } 2145 } 2146 2147 /* Now the uncompressed file contents are in buf. */ 2148 bim = ((struct bfd_in_memory *) 2149 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory))); 2150 if (bim == NULL) 2151 goto error_return; 2152 bim->size = size; 2153 bim->buffer = buf; 2154 2155 nbfd->mtime_set = TRUE; 2156 nbfd->mtime = strtol (hdr->ar_date, (char **) NULL, 10); 2157 2158 nbfd->flags |= BFD_IN_MEMORY; 2159 nbfd->iostream = bim; 2160 nbfd->iovec = &_bfd_memory_iovec; 2161 nbfd->origin = 0; 2162 BFD_ASSERT (! nbfd->cacheable); 2163 2164 return nbfd; 2165 2166 error_return: 2167 if (buf != NULL) 2168 free (buf); 2169 if (nbfd != NULL) 2170 bfd_close (nbfd); 2171 return NULL; 2172 } 2173 2174 /* Open the next archived file. */ 2175 2176 static bfd * 2177 alpha_ecoff_openr_next_archived_file (bfd *archive, bfd *last_file) 2178 { 2179 ufile_ptr filestart; 2180 2181 if (last_file == NULL) 2182 filestart = bfd_ardata (archive)->first_file_filepos; 2183 else 2184 { 2185 struct areltdata *t; 2186 struct ar_hdr *h; 2187 bfd_size_type size; 2188 2189 /* We can't use arelt_size here, because that uses parsed_size, 2190 which is the uncompressed size. We need the compressed size. */ 2191 t = (struct areltdata *) last_file->arelt_data; 2192 h = (struct ar_hdr *) t->arch_header; 2193 size = strtol (h->ar_size, (char **) NULL, 10); 2194 2195 /* Pad to an even boundary... 2196 Note that last_file->origin can be odd in the case of 2197 BSD-4.4-style element with a long odd size. */ 2198 filestart = last_file->proxy_origin + size; 2199 filestart += filestart % 2; 2200 if (filestart < last_file->proxy_origin) 2201 { 2202 /* Prevent looping. See PR19256. */ 2203 bfd_set_error (bfd_error_malformed_archive); 2204 return NULL; 2205 } 2206 } 2207 2208 return alpha_ecoff_get_elt_at_filepos (archive, filestart); 2209 } 2210 2211 /* Open the archive file given an index into the armap. */ 2212 2213 static bfd * 2214 alpha_ecoff_get_elt_at_index (bfd *abfd, symindex sym_index) 2215 { 2216 carsym *entry; 2217 2218 entry = bfd_ardata (abfd)->symdefs + sym_index; 2219 return alpha_ecoff_get_elt_at_filepos (abfd, entry->file_offset); 2220 } 2221 2222 static void 2223 alpha_ecoff_swap_coff_aux_in (bfd *abfd ATTRIBUTE_UNUSED, 2224 void *ext1 ATTRIBUTE_UNUSED, 2225 int type ATTRIBUTE_UNUSED, 2226 int in_class ATTRIBUTE_UNUSED, 2227 int indx ATTRIBUTE_UNUSED, 2228 int numaux ATTRIBUTE_UNUSED, 2229 void *in1 ATTRIBUTE_UNUSED) 2230 { 2231 } 2232 2233 static void 2234 alpha_ecoff_swap_coff_sym_in (bfd *abfd ATTRIBUTE_UNUSED, 2235 void *ext1 ATTRIBUTE_UNUSED, 2236 void *in1 ATTRIBUTE_UNUSED) 2237 { 2238 } 2239 2240 static void 2241 alpha_ecoff_swap_coff_lineno_in (bfd *abfd ATTRIBUTE_UNUSED, 2242 void *ext1 ATTRIBUTE_UNUSED, 2243 void *in1 ATTRIBUTE_UNUSED) 2244 { 2245 } 2246 2247 static unsigned int 2248 alpha_ecoff_swap_coff_aux_out (bfd *abfd ATTRIBUTE_UNUSED, 2249 void *inp ATTRIBUTE_UNUSED, 2250 int type ATTRIBUTE_UNUSED, 2251 int in_class ATTRIBUTE_UNUSED, 2252 int indx ATTRIBUTE_UNUSED, 2253 int numaux ATTRIBUTE_UNUSED, 2254 void *extp ATTRIBUTE_UNUSED) 2255 { 2256 return 0; 2257 } 2258 2259 static unsigned int 2260 alpha_ecoff_swap_coff_sym_out (bfd *abfd ATTRIBUTE_UNUSED, 2261 void *inp ATTRIBUTE_UNUSED, 2262 void *extp ATTRIBUTE_UNUSED) 2263 { 2264 return 0; 2265 } 2266 2267 static unsigned int 2268 alpha_ecoff_swap_coff_lineno_out (bfd *abfd ATTRIBUTE_UNUSED, 2269 void *inp ATTRIBUTE_UNUSED, 2270 void *extp ATTRIBUTE_UNUSED) 2271 { 2272 return 0; 2273 } 2274 2275 static unsigned int 2276 alpha_ecoff_swap_coff_reloc_out (bfd *abfd ATTRIBUTE_UNUSED, 2277 void *inp ATTRIBUTE_UNUSED, 2278 void *extp ATTRIBUTE_UNUSED) 2279 { 2280 return 0; 2281 } 2282 2283 /* This is the ECOFF backend structure. The backend field of the 2284 target vector points to this. */ 2285 2286 static const struct ecoff_backend_data alpha_ecoff_backend_data = 2287 { 2288 /* COFF backend structure. */ 2289 { 2290 alpha_ecoff_swap_coff_aux_in, alpha_ecoff_swap_coff_sym_in, 2291 alpha_ecoff_swap_coff_lineno_in, alpha_ecoff_swap_coff_aux_out, 2292 alpha_ecoff_swap_coff_sym_out, alpha_ecoff_swap_coff_lineno_out, 2293 alpha_ecoff_swap_coff_reloc_out, 2294 alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out, 2295 alpha_ecoff_swap_scnhdr_out, 2296 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, TRUE, 2297 ECOFF_NO_LONG_SECTION_NAMES, 4, FALSE, 2, 32768, 2298 alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in, 2299 alpha_ecoff_swap_scnhdr_in, NULL, 2300 alpha_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook, 2301 alpha_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags, 2302 _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table, 2303 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2304 NULL, NULL, NULL, NULL 2305 }, 2306 /* Supported architecture. */ 2307 bfd_arch_alpha, 2308 /* Initial portion of armap string. */ 2309 "________64", 2310 /* The page boundary used to align sections in a demand-paged 2311 executable file. E.g., 0x1000. */ 2312 0x2000, 2313 /* TRUE if the .rdata section is part of the text segment, as on the 2314 Alpha. FALSE if .rdata is part of the data segment, as on the 2315 MIPS. */ 2316 TRUE, 2317 /* Bitsize of constructor entries. */ 2318 64, 2319 /* Reloc to use for constructor entries. */ 2320 &alpha_howto_table[ALPHA_R_REFQUAD], 2321 { 2322 /* Symbol table magic number. */ 2323 magicSym2, 2324 /* Alignment of debugging information. E.g., 4. */ 2325 8, 2326 /* Sizes of external symbolic information. */ 2327 sizeof (struct hdr_ext), 2328 sizeof (struct dnr_ext), 2329 sizeof (struct pdr_ext), 2330 sizeof (struct sym_ext), 2331 sizeof (struct opt_ext), 2332 sizeof (struct fdr_ext), 2333 sizeof (struct rfd_ext), 2334 sizeof (struct ext_ext), 2335 /* Functions to swap in external symbolic data. */ 2336 ecoff_swap_hdr_in, 2337 ecoff_swap_dnr_in, 2338 ecoff_swap_pdr_in, 2339 ecoff_swap_sym_in, 2340 ecoff_swap_opt_in, 2341 ecoff_swap_fdr_in, 2342 ecoff_swap_rfd_in, 2343 ecoff_swap_ext_in, 2344 _bfd_ecoff_swap_tir_in, 2345 _bfd_ecoff_swap_rndx_in, 2346 /* Functions to swap out external symbolic data. */ 2347 ecoff_swap_hdr_out, 2348 ecoff_swap_dnr_out, 2349 ecoff_swap_pdr_out, 2350 ecoff_swap_sym_out, 2351 ecoff_swap_opt_out, 2352 ecoff_swap_fdr_out, 2353 ecoff_swap_rfd_out, 2354 ecoff_swap_ext_out, 2355 _bfd_ecoff_swap_tir_out, 2356 _bfd_ecoff_swap_rndx_out, 2357 /* Function to read in symbolic data. */ 2358 _bfd_ecoff_slurp_symbolic_info 2359 }, 2360 /* External reloc size. */ 2361 RELSZ, 2362 /* Reloc swapping functions. */ 2363 alpha_ecoff_swap_reloc_in, 2364 alpha_ecoff_swap_reloc_out, 2365 /* Backend reloc tweaking. */ 2366 alpha_adjust_reloc_in, 2367 alpha_adjust_reloc_out, 2368 /* Relocate section contents while linking. */ 2369 alpha_relocate_section, 2370 /* Do final adjustments to filehdr and aouthdr. */ 2371 alpha_adjust_headers, 2372 /* Read an element from an archive at a given file position. */ 2373 alpha_ecoff_get_elt_at_filepos 2374 }; 2375 2376 /* Looking up a reloc type is Alpha specific. */ 2377 #define _bfd_ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup 2378 #define _bfd_ecoff_bfd_reloc_name_lookup \ 2379 alpha_bfd_reloc_name_lookup 2380 2381 /* So is getting relocated section contents. */ 2382 #define _bfd_ecoff_bfd_get_relocated_section_contents \ 2383 alpha_ecoff_get_relocated_section_contents 2384 2385 /* Handling file windows is generic. */ 2386 #define _bfd_ecoff_get_section_contents_in_window \ 2387 _bfd_generic_get_section_contents_in_window 2388 2389 /* Input section flag lookup is generic. */ 2390 #define _bfd_ecoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags 2391 2392 /* Relaxing sections is generic. */ 2393 #define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section 2394 #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections 2395 #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections 2396 #define _bfd_ecoff_bfd_is_group_section bfd_generic_is_group_section 2397 #define _bfd_ecoff_bfd_group_name bfd_generic_group_name 2398 #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group 2399 #define _bfd_ecoff_section_already_linked \ 2400 _bfd_coff_section_already_linked 2401 #define _bfd_ecoff_bfd_define_common_symbol bfd_generic_define_common_symbol 2402 #define _bfd_ecoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol 2403 #define _bfd_ecoff_bfd_define_start_stop bfd_generic_define_start_stop 2404 #define _bfd_ecoff_bfd_link_check_relocs _bfd_generic_link_check_relocs 2405 2406 /* Installing internal relocations in a section is also generic. */ 2407 #define _bfd_ecoff_set_reloc _bfd_generic_set_reloc 2408 2409 const bfd_target alpha_ecoff_le_vec = 2410 { 2411 "ecoff-littlealpha", /* name */ 2412 bfd_target_ecoff_flavour, 2413 BFD_ENDIAN_LITTLE, /* data byte order is little */ 2414 BFD_ENDIAN_LITTLE, /* header byte order is little */ 2415 2416 (HAS_RELOC | EXEC_P /* object flags */ 2417 | HAS_LINENO | HAS_DEBUG 2418 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED), 2419 2420 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA), 2421 0, /* leading underscore */ 2422 ' ', /* ar_pad_char */ 2423 15, /* ar_max_namelen */ 2424 0, /* match priority. */ 2425 bfd_getl64, bfd_getl_signed_64, bfd_putl64, 2426 bfd_getl32, bfd_getl_signed_32, bfd_putl32, 2427 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ 2428 bfd_getl64, bfd_getl_signed_64, bfd_putl64, 2429 bfd_getl32, bfd_getl_signed_32, bfd_putl32, 2430 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ 2431 2432 { /* bfd_check_format */ 2433 _bfd_dummy_target, 2434 alpha_ecoff_object_p, 2435 bfd_generic_archive_p, 2436 _bfd_dummy_target 2437 }, 2438 { /* bfd_set_format */ 2439 _bfd_bool_bfd_false_error, 2440 _bfd_ecoff_mkobject, 2441 _bfd_generic_mkarchive, 2442 _bfd_bool_bfd_false_error 2443 }, 2444 { /* bfd_write_contents */ 2445 _bfd_bool_bfd_false_error, 2446 _bfd_ecoff_write_object_contents, 2447 _bfd_write_archive_contents, 2448 _bfd_bool_bfd_false_error 2449 }, 2450 2451 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff), 2452 BFD_JUMP_TABLE_COPY (_bfd_ecoff), 2453 BFD_JUMP_TABLE_CORE (_bfd_nocore), 2454 BFD_JUMP_TABLE_ARCHIVE (alpha_ecoff), 2455 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff), 2456 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff), 2457 BFD_JUMP_TABLE_WRITE (_bfd_ecoff), 2458 BFD_JUMP_TABLE_LINK (_bfd_ecoff), 2459 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), 2460 2461 NULL, 2462 2463 &alpha_ecoff_backend_data 2464 }; 2465