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