1 //===--- XCOFFObjectFile.cpp - XCOFF object file implementation -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the XCOFFObjectFile class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Object/XCOFFObjectFile.h" 14 #include "llvm/MC/SubtargetFeature.h" 15 #include <cstddef> 16 #include <cstring> 17 18 namespace llvm { 19 namespace object { 20 21 static const uint8_t FunctionSym = 0x20; 22 static const uint8_t SymTypeMask = 0x07; 23 static const uint16_t RelocOverflow = 65535; 24 static const uint16_t NoRelMask = 0x0001; 25 26 // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer 27 // 'M'. Returns a pointer to the underlying object on success. 28 template <typename T> 29 static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr, 30 const uint64_t Size = sizeof(T)) { 31 uintptr_t Addr = uintptr_t(Ptr); 32 if (Error E = Binary::checkOffset(M, Addr, Size)) 33 return std::move(E); 34 return reinterpret_cast<const T *>(Addr); 35 } 36 37 static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) { 38 return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) + 39 Offset); 40 } 41 42 template <typename T> static const T *viewAs(uintptr_t in) { 43 return reinterpret_cast<const T *>(in); 44 } 45 46 static StringRef generateXCOFFFixedNameStringRef(const char *Name) { 47 auto NulCharPtr = 48 static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize)); 49 return NulCharPtr ? StringRef(Name, NulCharPtr - Name) 50 : StringRef(Name, XCOFF::NameSize); 51 } 52 53 template <typename T> StringRef XCOFFSectionHeader<T>::getName() const { 54 const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this); 55 return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name); 56 } 57 58 template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const { 59 const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this); 60 return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask; 61 } 62 63 template <typename T> 64 bool XCOFFSectionHeader<T>::isReservedSectionType() const { 65 return getSectionType() & SectionFlagsReservedMask; 66 } 67 68 bool XCOFFRelocation32::isRelocationSigned() const { 69 return Info & XR_SIGN_INDICATOR_MASK; 70 } 71 72 bool XCOFFRelocation32::isFixupIndicated() const { 73 return Info & XR_FIXUP_INDICATOR_MASK; 74 } 75 76 uint8_t XCOFFRelocation32::getRelocatedLength() const { 77 // The relocation encodes the bit length being relocated minus 1. Add back 78 // the 1 to get the actual length being relocated. 79 return (Info & XR_BIASED_LENGTH_MASK) + 1; 80 } 81 82 void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr, 83 uintptr_t TableAddress) const { 84 if (Addr < TableAddress) 85 report_fatal_error("Section header outside of section header table."); 86 87 uintptr_t Offset = Addr - TableAddress; 88 if (Offset >= getSectionHeaderSize() * getNumberOfSections()) 89 report_fatal_error("Section header outside of section header table."); 90 91 if (Offset % getSectionHeaderSize() != 0) 92 report_fatal_error( 93 "Section header pointer does not point to a valid section header."); 94 } 95 96 const XCOFFSectionHeader32 * 97 XCOFFObjectFile::toSection32(DataRefImpl Ref) const { 98 assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 99 #ifndef NDEBUG 100 checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); 101 #endif 102 return viewAs<XCOFFSectionHeader32>(Ref.p); 103 } 104 105 const XCOFFSectionHeader64 * 106 XCOFFObjectFile::toSection64(DataRefImpl Ref) const { 107 assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 108 #ifndef NDEBUG 109 checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); 110 #endif 111 return viewAs<XCOFFSectionHeader64>(Ref.p); 112 } 113 114 const XCOFFSymbolEntry *XCOFFObjectFile::toSymbolEntry(DataRefImpl Ref) const { 115 assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 116 assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!"); 117 #ifndef NDEBUG 118 checkSymbolEntryPointer(Ref.p); 119 #endif 120 auto SymEntPtr = viewAs<XCOFFSymbolEntry>(Ref.p); 121 return SymEntPtr; 122 } 123 124 const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const { 125 assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 126 return static_cast<const XCOFFFileHeader32 *>(FileHeader); 127 } 128 129 const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const { 130 assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 131 return static_cast<const XCOFFFileHeader64 *>(FileHeader); 132 } 133 134 const XCOFFSectionHeader32 * 135 XCOFFObjectFile::sectionHeaderTable32() const { 136 assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 137 return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable); 138 } 139 140 const XCOFFSectionHeader64 * 141 XCOFFObjectFile::sectionHeaderTable64() const { 142 assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 143 return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable); 144 } 145 146 void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 147 const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb); 148 SymEntPtr += SymEntPtr->NumberOfAuxEntries + 1; 149 #ifndef NDEBUG 150 // This function is used by basic_symbol_iterator, which allows to 151 // point to the end-of-symbol-table address. 152 if (reinterpret_cast<uintptr_t>(SymEntPtr) != getEndOfSymbolTableAddress()) 153 checkSymbolEntryPointer(reinterpret_cast<uintptr_t>(SymEntPtr)); 154 #endif 155 Symb.p = reinterpret_cast<uintptr_t>(SymEntPtr); 156 } 157 158 Expected<StringRef> 159 XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const { 160 // The byte offset is relative to the start of the string table. 161 // A byte offset value of 0 is a null or zero-length symbol 162 // name. A byte offset in the range 1 to 3 (inclusive) points into the length 163 // field; as a soft-error recovery mechanism, we treat such cases as having an 164 // offset of 0. 165 if (Offset < 4) 166 return StringRef(nullptr, 0); 167 168 if (StringTable.Data != nullptr && StringTable.Size > Offset) 169 return (StringTable.Data + Offset); 170 171 return make_error<GenericBinaryError>("Bad offset for string table entry", 172 object_error::parse_failed); 173 } 174 175 Expected<StringRef> 176 XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const { 177 if (CFileEntPtr->NameInStrTbl.Magic != 178 XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC) 179 return generateXCOFFFixedNameStringRef(CFileEntPtr->Name); 180 return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset); 181 } 182 183 Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const { 184 const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb); 185 186 // A storage class value with the high-order bit on indicates that the name is 187 // a symbolic debugger stabstring. 188 if (SymEntPtr->StorageClass & 0x80) 189 return StringRef("Unimplemented Debug Name"); 190 191 if (SymEntPtr->NameInStrTbl.Magic != XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC) 192 return generateXCOFFFixedNameStringRef(SymEntPtr->SymbolName); 193 194 return getStringTableEntry(SymEntPtr->NameInStrTbl.Offset); 195 } 196 197 Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const { 198 assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 199 return toSymbolEntry(Symb)->Value; 200 } 201 202 uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 203 assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 204 return toSymbolEntry(Symb)->Value; 205 } 206 207 uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 208 uint64_t Result = 0; 209 llvm_unreachable("Not yet implemented!"); 210 return Result; 211 } 212 213 Expected<SymbolRef::Type> 214 XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const { 215 llvm_unreachable("Not yet implemented!"); 216 return SymbolRef::ST_Other; 217 } 218 219 Expected<section_iterator> 220 XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const { 221 const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb); 222 int16_t SectNum = SymEntPtr->SectionNumber; 223 224 if (isReservedSectionNumber(SectNum)) 225 return section_end(); 226 227 Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum); 228 if (!ExpSec) 229 return ExpSec.takeError(); 230 231 return section_iterator(SectionRef(ExpSec.get(), this)); 232 } 233 234 void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const { 235 const char *Ptr = reinterpret_cast<const char *>(Sec.p); 236 Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize()); 237 } 238 239 Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const { 240 return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec)); 241 } 242 243 uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const { 244 // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t 245 // with MSVC. 246 if (is64Bit()) 247 return toSection64(Sec)->VirtualAddress; 248 249 return toSection32(Sec)->VirtualAddress; 250 } 251 252 uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const { 253 // Section numbers in XCOFF are numbered beginning at 1. A section number of 254 // zero is used to indicate that a symbol is being imported or is undefined. 255 if (is64Bit()) 256 return toSection64(Sec) - sectionHeaderTable64() + 1; 257 else 258 return toSection32(Sec) - sectionHeaderTable32() + 1; 259 } 260 261 uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const { 262 // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t 263 // with MSVC. 264 if (is64Bit()) 265 return toSection64(Sec)->SectionSize; 266 267 return toSection32(Sec)->SectionSize; 268 } 269 270 Expected<ArrayRef<uint8_t>> 271 XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const { 272 if (isSectionVirtual(Sec)) 273 return ArrayRef<uint8_t>(); 274 275 uint64_t OffsetToRaw; 276 if (is64Bit()) 277 OffsetToRaw = toSection64(Sec)->FileOffsetToRawData; 278 else 279 OffsetToRaw = toSection32(Sec)->FileOffsetToRawData; 280 281 const uint8_t * ContentStart = base() + OffsetToRaw; 282 uint64_t SectionSize = getSectionSize(Sec); 283 if (checkOffset(Data, uintptr_t(ContentStart), SectionSize)) 284 return make_error<BinaryError>(); 285 286 return makeArrayRef(ContentStart,SectionSize); 287 } 288 289 uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { 290 uint64_t Result = 0; 291 llvm_unreachable("Not yet implemented!"); 292 return Result; 293 } 294 295 bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { 296 bool Result = false; 297 llvm_unreachable("Not yet implemented!"); 298 return Result; 299 } 300 301 bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const { 302 return getSectionFlags(Sec) & XCOFF::STYP_TEXT; 303 } 304 305 bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const { 306 uint32_t Flags = getSectionFlags(Sec); 307 return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA); 308 } 309 310 bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const { 311 uint32_t Flags = getSectionFlags(Sec); 312 return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS); 313 } 314 315 bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const { 316 return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0 317 : toSection32(Sec)->FileOffsetToRawData == 0; 318 } 319 320 relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const { 321 if (is64Bit()) 322 report_fatal_error("64-bit support not implemented yet"); 323 const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec); 324 auto RelocationsOrErr = relocations(*SectionEntPtr); 325 if (Error E = RelocationsOrErr.takeError()) 326 return relocation_iterator(RelocationRef()); 327 DataRefImpl Ret; 328 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin()); 329 return relocation_iterator(RelocationRef(Ret, this)); 330 } 331 332 relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const { 333 if (is64Bit()) 334 report_fatal_error("64-bit support not implemented yet"); 335 const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec); 336 auto RelocationsOrErr = relocations(*SectionEntPtr); 337 if (Error E = RelocationsOrErr.takeError()) 338 return relocation_iterator(RelocationRef()); 339 DataRefImpl Ret; 340 Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end()); 341 return relocation_iterator(RelocationRef(Ret, this)); 342 } 343 344 void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 345 Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1); 346 } 347 348 uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const { 349 if (is64Bit()) 350 report_fatal_error("64-bit support not implemented yet"); 351 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p); 352 const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32(); 353 const uint32_t RelocAddress = Reloc->VirtualAddress; 354 const uint16_t NumberOfSections = getNumberOfSections(); 355 for (uint16_t i = 0; i < NumberOfSections; ++i) { 356 // Find which section this relocation is belonging to, and get the 357 // relocation offset relative to the start of the section. 358 if (Sec32->VirtualAddress <= RelocAddress && 359 RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) { 360 return RelocAddress - Sec32->VirtualAddress; 361 } 362 ++Sec32; 363 } 364 return InvalidRelocOffset; 365 } 366 367 symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 368 if (is64Bit()) 369 report_fatal_error("64-bit support not implemented yet"); 370 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p); 371 const uint32_t Index = Reloc->SymbolIndex; 372 373 if (Index >= getLogicalNumberOfSymbolTableEntries32()) 374 return symbol_end(); 375 376 DataRefImpl SymDRI; 377 SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index); 378 return symbol_iterator(SymbolRef(SymDRI, this)); 379 } 380 381 uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const { 382 if (is64Bit()) 383 report_fatal_error("64-bit support not implemented yet"); 384 return viewAs<XCOFFRelocation32>(Rel.p)->Type; 385 } 386 387 void XCOFFObjectFile::getRelocationTypeName( 388 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 389 if (is64Bit()) 390 report_fatal_error("64-bit support not implemented yet"); 391 const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p); 392 StringRef Res = XCOFF::getRelocationTypeString(Reloc->Type); 393 Result.append(Res.begin(), Res.end()); 394 } 395 396 Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const { 397 uint32_t Result = 0; 398 llvm_unreachable("Not yet implemented!"); 399 return Result; 400 } 401 402 basic_symbol_iterator XCOFFObjectFile::symbol_begin() const { 403 if (is64Bit()) 404 report_fatal_error("64-bit support not implemented yet"); 405 DataRefImpl SymDRI; 406 SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr); 407 return basic_symbol_iterator(SymbolRef(SymDRI, this)); 408 } 409 410 basic_symbol_iterator XCOFFObjectFile::symbol_end() const { 411 if (is64Bit()) 412 report_fatal_error("64-bit support not implemented yet"); 413 DataRefImpl SymDRI; 414 SymDRI.p = reinterpret_cast<uintptr_t>( 415 SymbolTblPtr + getLogicalNumberOfSymbolTableEntries32()); 416 return basic_symbol_iterator(SymbolRef(SymDRI, this)); 417 } 418 419 section_iterator XCOFFObjectFile::section_begin() const { 420 DataRefImpl DRI; 421 DRI.p = getSectionHeaderTableAddress(); 422 return section_iterator(SectionRef(DRI, this)); 423 } 424 425 section_iterator XCOFFObjectFile::section_end() const { 426 DataRefImpl DRI; 427 DRI.p = getWithOffset(getSectionHeaderTableAddress(), 428 getNumberOfSections() * getSectionHeaderSize()); 429 return section_iterator(SectionRef(DRI, this)); 430 } 431 432 uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; } 433 434 StringRef XCOFFObjectFile::getFileFormatName() const { 435 return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000"; 436 } 437 438 Triple::ArchType XCOFFObjectFile::getArch() const { 439 return is64Bit() ? Triple::ppc64 : Triple::ppc; 440 } 441 442 SubtargetFeatures XCOFFObjectFile::getFeatures() const { 443 return SubtargetFeatures(); 444 } 445 446 bool XCOFFObjectFile::isRelocatableObject() const { 447 if (is64Bit()) 448 report_fatal_error("64-bit support not implemented yet"); 449 return !(fileHeader32()->Flags & NoRelMask); 450 } 451 452 Expected<uint64_t> XCOFFObjectFile::getStartAddress() const { 453 // TODO FIXME Should get from auxiliary_header->o_entry when support for the 454 // auxiliary_header is added. 455 return 0; 456 } 457 458 size_t XCOFFObjectFile::getFileHeaderSize() const { 459 return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32); 460 } 461 462 size_t XCOFFObjectFile::getSectionHeaderSize() const { 463 return is64Bit() ? sizeof(XCOFFSectionHeader64) : 464 sizeof(XCOFFSectionHeader32); 465 } 466 467 bool XCOFFObjectFile::is64Bit() const { 468 return Binary::ID_XCOFF64 == getType(); 469 } 470 471 uint16_t XCOFFObjectFile::getMagic() const { 472 return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic; 473 } 474 475 Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const { 476 if (Num <= 0 || Num > getNumberOfSections()) 477 return errorCodeToError(object_error::invalid_section_index); 478 479 DataRefImpl DRI; 480 DRI.p = getWithOffset(getSectionHeaderTableAddress(), 481 getSectionHeaderSize() * (Num - 1)); 482 return DRI; 483 } 484 485 Expected<StringRef> 486 XCOFFObjectFile::getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const { 487 assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 488 int16_t SectionNum = SymEntPtr->SectionNumber; 489 490 switch (SectionNum) { 491 case XCOFF::N_DEBUG: 492 return "N_DEBUG"; 493 case XCOFF::N_ABS: 494 return "N_ABS"; 495 case XCOFF::N_UNDEF: 496 return "N_UNDEF"; 497 default: 498 Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum); 499 if (SecRef) 500 return generateXCOFFFixedNameStringRef( 501 getSectionNameInternal(SecRef.get())); 502 return SecRef.takeError(); 503 } 504 } 505 506 bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) { 507 return (SectionNumber <= 0 && SectionNumber >= -2); 508 } 509 510 uint16_t XCOFFObjectFile::getNumberOfSections() const { 511 return is64Bit() ? fileHeader64()->NumberOfSections 512 : fileHeader32()->NumberOfSections; 513 } 514 515 int32_t XCOFFObjectFile::getTimeStamp() const { 516 return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp; 517 } 518 519 uint16_t XCOFFObjectFile::getOptionalHeaderSize() const { 520 return is64Bit() ? fileHeader64()->AuxHeaderSize 521 : fileHeader32()->AuxHeaderSize; 522 } 523 524 uint32_t XCOFFObjectFile::getSymbolTableOffset32() const { 525 return fileHeader32()->SymbolTableOffset; 526 } 527 528 int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const { 529 // As far as symbol table size is concerned, if this field is negative it is 530 // to be treated as a 0. However since this field is also used for printing we 531 // don't want to truncate any negative values. 532 return fileHeader32()->NumberOfSymTableEntries; 533 } 534 535 uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const { 536 return (fileHeader32()->NumberOfSymTableEntries >= 0 537 ? fileHeader32()->NumberOfSymTableEntries 538 : 0); 539 } 540 541 uint64_t XCOFFObjectFile::getSymbolTableOffset64() const { 542 return fileHeader64()->SymbolTableOffset; 543 } 544 545 uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const { 546 return fileHeader64()->NumberOfSymTableEntries; 547 } 548 549 uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const { 550 uint32_t NumberOfSymTableEntries = 551 is64Bit() ? getNumberOfSymbolTableEntries64() 552 : getLogicalNumberOfSymbolTableEntries32(); 553 return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr), 554 XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries); 555 } 556 557 void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const { 558 if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr)) 559 report_fatal_error("Symbol table entry is outside of symbol table."); 560 561 if (SymbolEntPtr >= getEndOfSymbolTableAddress()) 562 report_fatal_error("Symbol table entry is outside of symbol table."); 563 564 ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) - 565 reinterpret_cast<const char *>(SymbolTblPtr); 566 567 if (Offset % XCOFF::SymbolTableEntrySize != 0) 568 report_fatal_error( 569 "Symbol table entry position is not valid inside of symbol table."); 570 } 571 572 uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const { 573 return (reinterpret_cast<const char *>(SymbolEntPtr) - 574 reinterpret_cast<const char *>(SymbolTblPtr)) / 575 XCOFF::SymbolTableEntrySize; 576 } 577 578 Expected<StringRef> 579 XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const { 580 if (is64Bit()) 581 report_fatal_error("64-bit symbol table support not implemented yet."); 582 583 if (Index >= getLogicalNumberOfSymbolTableEntries32()) 584 return errorCodeToError(object_error::invalid_symbol_index); 585 586 DataRefImpl SymDRI; 587 SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index); 588 return getSymbolName(SymDRI); 589 } 590 591 uint16_t XCOFFObjectFile::getFlags() const { 592 return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags; 593 } 594 595 const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const { 596 return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name; 597 } 598 599 uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const { 600 return reinterpret_cast<uintptr_t>(SectionHeaderTable); 601 } 602 603 int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const { 604 return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags; 605 } 606 607 XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object) 608 : ObjectFile(Type, Object) { 609 assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64); 610 } 611 612 ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const { 613 assert(is64Bit() && "64-bit interface called for non 64-bit file."); 614 const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64(); 615 return ArrayRef<XCOFFSectionHeader64>(TablePtr, 616 TablePtr + getNumberOfSections()); 617 } 618 619 ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const { 620 assert(!is64Bit() && "32-bit interface called for non 32-bit file."); 621 const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32(); 622 return ArrayRef<XCOFFSectionHeader32>(TablePtr, 623 TablePtr + getNumberOfSections()); 624 } 625 626 // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO 627 // section header contains the actual count of relocation entries in the s_paddr 628 // field. STYP_OVRFLO headers contain the section index of their corresponding 629 // sections as their raw "NumberOfRelocations" field value. 630 Expected<uint32_t> XCOFFObjectFile::getLogicalNumberOfRelocationEntries( 631 const XCOFFSectionHeader32 &Sec) const { 632 633 uint16_t SectionIndex = &Sec - sectionHeaderTable32() + 1; 634 635 if (Sec.NumberOfRelocations < RelocOverflow) 636 return Sec.NumberOfRelocations; 637 for (const auto &Sec : sections32()) { 638 if (Sec.Flags == XCOFF::STYP_OVRFLO && 639 Sec.NumberOfRelocations == SectionIndex) 640 return Sec.PhysicalAddress; 641 } 642 return errorCodeToError(object_error::parse_failed); 643 } 644 645 Expected<ArrayRef<XCOFFRelocation32>> 646 XCOFFObjectFile::relocations(const XCOFFSectionHeader32 &Sec) const { 647 uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader), 648 Sec.FileOffsetToRelocationInfo); 649 auto NumRelocEntriesOrErr = getLogicalNumberOfRelocationEntries(Sec); 650 if (Error E = NumRelocEntriesOrErr.takeError()) 651 return std::move(E); 652 653 uint32_t NumRelocEntries = NumRelocEntriesOrErr.get(); 654 655 assert(sizeof(XCOFFRelocation32) == XCOFF::RelocationSerializationSize32); 656 auto RelocationOrErr = 657 getObject<XCOFFRelocation32>(Data, reinterpret_cast<void *>(RelocAddr), 658 NumRelocEntries * sizeof(XCOFFRelocation32)); 659 if (Error E = RelocationOrErr.takeError()) 660 return std::move(E); 661 662 const XCOFFRelocation32 *StartReloc = RelocationOrErr.get(); 663 664 return ArrayRef<XCOFFRelocation32>(StartReloc, StartReloc + NumRelocEntries); 665 } 666 667 Expected<XCOFFStringTable> 668 XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) { 669 // If there is a string table, then the buffer must contain at least 4 bytes 670 // for the string table's size. Not having a string table is not an error. 671 if (auto EC = Binary::checkOffset( 672 Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) 673 return XCOFFStringTable{0, nullptr}; 674 675 // Read the size out of the buffer. 676 uint32_t Size = support::endian::read32be(Obj->base() + Offset); 677 678 // If the size is less then 4, then the string table is just a size and no 679 // string data. 680 if (Size <= 4) 681 return XCOFFStringTable{4, nullptr}; 682 683 auto StringTableOrErr = 684 getObject<char>(Obj->Data, Obj->base() + Offset, Size); 685 if (Error E = StringTableOrErr.takeError()) 686 return std::move(E); 687 688 const char *StringTablePtr = StringTableOrErr.get(); 689 if (StringTablePtr[Size - 1] != '\0') 690 return errorCodeToError(object_error::string_table_non_null_end); 691 692 return XCOFFStringTable{Size, StringTablePtr}; 693 } 694 695 Expected<std::unique_ptr<XCOFFObjectFile>> 696 XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) { 697 // Can't use std::make_unique because of the private constructor. 698 std::unique_ptr<XCOFFObjectFile> Obj; 699 Obj.reset(new XCOFFObjectFile(Type, MBR)); 700 701 uint64_t CurOffset = 0; 702 const auto *Base = Obj->base(); 703 MemoryBufferRef Data = Obj->Data; 704 705 // Parse file header. 706 auto FileHeaderOrErr = 707 getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize()); 708 if (Error E = FileHeaderOrErr.takeError()) 709 return std::move(E); 710 Obj->FileHeader = FileHeaderOrErr.get(); 711 712 CurOffset += Obj->getFileHeaderSize(); 713 // TODO FIXME we don't have support for an optional header yet, so just skip 714 // past it. 715 CurOffset += Obj->getOptionalHeaderSize(); 716 717 // Parse the section header table if it is present. 718 if (Obj->getNumberOfSections()) { 719 auto SecHeadersOrErr = getObject<void>(Data, Base + CurOffset, 720 Obj->getNumberOfSections() * 721 Obj->getSectionHeaderSize()); 722 if (Error E = SecHeadersOrErr.takeError()) 723 return std::move(E); 724 Obj->SectionHeaderTable = SecHeadersOrErr.get(); 725 } 726 727 // 64-bit object supports only file header and section headers for now. 728 if (Obj->is64Bit()) 729 return std::move(Obj); 730 731 // If there is no symbol table we are done parsing the memory buffer. 732 if (Obj->getLogicalNumberOfSymbolTableEntries32() == 0) 733 return std::move(Obj); 734 735 // Parse symbol table. 736 CurOffset = Obj->fileHeader32()->SymbolTableOffset; 737 uint64_t SymbolTableSize = (uint64_t)(sizeof(XCOFFSymbolEntry)) * 738 Obj->getLogicalNumberOfSymbolTableEntries32(); 739 auto SymTableOrErr = 740 getObject<XCOFFSymbolEntry>(Data, Base + CurOffset, SymbolTableSize); 741 if (Error E = SymTableOrErr.takeError()) 742 return std::move(E); 743 Obj->SymbolTblPtr = SymTableOrErr.get(); 744 CurOffset += SymbolTableSize; 745 746 // Parse String table. 747 Expected<XCOFFStringTable> StringTableOrErr = 748 parseStringTable(Obj.get(), CurOffset); 749 if (Error E = StringTableOrErr.takeError()) 750 return std::move(E); 751 Obj->StringTable = StringTableOrErr.get(); 752 753 return std::move(Obj); 754 } 755 756 Expected<std::unique_ptr<ObjectFile>> 757 ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef, 758 unsigned FileType) { 759 return XCOFFObjectFile::create(FileType, MemBufRef); 760 } 761 762 XCOFF::StorageClass XCOFFSymbolRef::getStorageClass() const { 763 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->StorageClass; 764 } 765 766 uint8_t XCOFFSymbolRef::getNumberOfAuxEntries() const { 767 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->NumberOfAuxEntries; 768 } 769 770 // TODO: The function needs to return an error if there is no csect auxiliary 771 // entry. 772 const XCOFFCsectAuxEnt32 *XCOFFSymbolRef::getXCOFFCsectAuxEnt32() const { 773 assert(!OwningObjectPtr->is64Bit() && 774 "32-bit interface called on 64-bit object file."); 775 assert(hasCsectAuxEnt() && "No Csect Auxiliary Entry is found."); 776 777 // In XCOFF32, the csect auxilliary entry is always the last auxiliary 778 // entry for the symbol. 779 uintptr_t AuxAddr = getWithOffset( 780 SymEntDataRef.p, XCOFF::SymbolTableEntrySize * getNumberOfAuxEntries()); 781 782 #ifndef NDEBUG 783 OwningObjectPtr->checkSymbolEntryPointer(AuxAddr); 784 #endif 785 786 return reinterpret_cast<const XCOFFCsectAuxEnt32 *>(AuxAddr); 787 } 788 789 uint16_t XCOFFSymbolRef::getType() const { 790 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SymbolType; 791 } 792 793 int16_t XCOFFSymbolRef::getSectionNumber() const { 794 return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SectionNumber; 795 } 796 797 // TODO: The function name needs to be changed to express the purpose of the 798 // function. 799 bool XCOFFSymbolRef::hasCsectAuxEnt() const { 800 XCOFF::StorageClass SC = getStorageClass(); 801 return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT || 802 SC == XCOFF::C_HIDEXT); 803 } 804 805 bool XCOFFSymbolRef::isFunction() const { 806 if (OwningObjectPtr->is64Bit()) 807 report_fatal_error("64-bit support is unimplemented yet."); 808 809 if (getType() & FunctionSym) 810 return true; 811 812 if (!hasCsectAuxEnt()) 813 return false; 814 815 const XCOFFCsectAuxEnt32 *CsectAuxEnt = getXCOFFCsectAuxEnt32(); 816 817 // A function definition should be a label definition. 818 if ((CsectAuxEnt->SymbolAlignmentAndType & SymTypeMask) != XCOFF::XTY_LD) 819 return false; 820 821 if (CsectAuxEnt->StorageMappingClass != XCOFF::XMC_PR) 822 return false; 823 824 int16_t SectNum = getSectionNumber(); 825 Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum); 826 if (!SI) 827 return false; 828 829 return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT); 830 } 831 832 // Explictly instantiate template classes. 833 template struct XCOFFSectionHeader<XCOFFSectionHeader32>; 834 template struct XCOFFSectionHeader<XCOFFSectionHeader64>; 835 836 } // namespace object 837 } // namespace llvm 838