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