1ab2eb2bfSHubert Tong //===--- XCOFFObjectFile.cpp - XCOFF object file implementation -----------===// 2ab2eb2bfSHubert Tong // 3ab2eb2bfSHubert Tong // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4ab2eb2bfSHubert Tong // See https://llvm.org/LICENSE.txt for license information. 5ab2eb2bfSHubert Tong // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ab2eb2bfSHubert Tong // 7ab2eb2bfSHubert Tong //===----------------------------------------------------------------------===// 8ab2eb2bfSHubert Tong // 9ab2eb2bfSHubert Tong // This file defines the XCOFFObjectFile class. 10ab2eb2bfSHubert Tong // 11ab2eb2bfSHubert Tong //===----------------------------------------------------------------------===// 12ab2eb2bfSHubert Tong 13ab2eb2bfSHubert Tong #include "llvm/Object/XCOFFObjectFile.h" 14ab2eb2bfSHubert Tong #include <cstddef> 15ab2eb2bfSHubert Tong #include <cstring> 16ab2eb2bfSHubert Tong 17ab2eb2bfSHubert Tong namespace llvm { 18ab2eb2bfSHubert Tong namespace object { 19ab2eb2bfSHubert Tong 2034d4bff3SDigger Lin enum { FUNCTION_SYM = 0x20, SYM_TYPE_MASK = 0x07, RELOC_OVERFLOW = 65535 }; 217c72e82bSJason Liu 22837ae69fSSean Fertile // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer 23837ae69fSSean Fertile // 'M'. Returns a pointer to the underlying object on success. 24ab2eb2bfSHubert Tong template <typename T> 25837ae69fSSean Fertile static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr, 26ab2eb2bfSHubert Tong const uint64_t Size = sizeof(T)) { 27ab2eb2bfSHubert Tong uintptr_t Addr = uintptr_t(Ptr); 28ab2eb2bfSHubert Tong if (std::error_code EC = Binary::checkOffset(M, Addr, Size)) 29837ae69fSSean Fertile return errorCodeToError(EC); 30837ae69fSSean Fertile return reinterpret_cast<const T *>(Addr); 31837ae69fSSean Fertile } 32837ae69fSSean Fertile 33837ae69fSSean Fertile static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) { 34837ae69fSSean Fertile return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) + 35837ae69fSSean Fertile Offset); 36ab2eb2bfSHubert Tong } 37ab2eb2bfSHubert Tong 38a93a33cbSSean Fertile template <typename T> static const T *viewAs(uintptr_t in) { 39a93a33cbSSean Fertile return reinterpret_cast<const T *>(in); 40a93a33cbSSean Fertile } 41a93a33cbSSean Fertile 427c72e82bSJason Liu static StringRef generateXCOFFFixedNameStringRef(const char *Name) { 4329141da7SSean Fertile auto NulCharPtr = 4429141da7SSean Fertile static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize)); 459212206dSJason Liu return NulCharPtr ? StringRef(Name, NulCharPtr - Name) 4629141da7SSean Fertile : StringRef(Name, XCOFF::NameSize); 479212206dSJason Liu } 489212206dSJason Liu 49c63c1a72Sdiggerlin template <typename T> StringRef XCOFFSectionHeader<T>::getName() const { 50c63c1a72Sdiggerlin const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this); 51c63c1a72Sdiggerlin return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name); 52c63c1a72Sdiggerlin } 53c63c1a72Sdiggerlin 54c63c1a72Sdiggerlin template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const { 55c63c1a72Sdiggerlin const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this); 56c63c1a72Sdiggerlin return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask; 57c63c1a72Sdiggerlin } 58c63c1a72Sdiggerlin 59c63c1a72Sdiggerlin template <typename T> 60c63c1a72Sdiggerlin bool XCOFFSectionHeader<T>::isReservedSectionType() const { 61c63c1a72Sdiggerlin return getSectionType() & SectionFlagsReservedMask; 62c63c1a72Sdiggerlin } 63c63c1a72Sdiggerlin 6434d4bff3SDigger Lin bool XCOFFRelocation32::isRelocationSigned() const { 6534d4bff3SDigger Lin return Info & XR_SIGN_INDICATOR_MASK; 6634d4bff3SDigger Lin } 6734d4bff3SDigger Lin 6834d4bff3SDigger Lin bool XCOFFRelocation32::isFixupIndicated() const { 6934d4bff3SDigger Lin return Info & XR_FIXUP_INDICATOR_MASK; 7034d4bff3SDigger Lin } 7134d4bff3SDigger Lin 7234d4bff3SDigger Lin uint8_t XCOFFRelocation32::getRelocatedLength() const { 7334d4bff3SDigger Lin // The relocation encodes the bit length being relocated minus 1. Add back 7434d4bff3SDigger Lin // the 1 to get the actual length being relocated. 7534d4bff3SDigger Lin return (Info & XR_BIASED_LENGTH_MASK) + 1; 7634d4bff3SDigger Lin } 7734d4bff3SDigger Lin 78837ae69fSSean Fertile void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr, 79837ae69fSSean Fertile uintptr_t TableAddress) const { 80837ae69fSSean Fertile if (Addr < TableAddress) 81a93a33cbSSean Fertile report_fatal_error("Section header outside of section header table."); 82a93a33cbSSean Fertile 83837ae69fSSean Fertile uintptr_t Offset = Addr - TableAddress; 84837ae69fSSean Fertile if (Offset >= getSectionHeaderSize() * getNumberOfSections()) 85837ae69fSSean Fertile report_fatal_error("Section header outside of section header table."); 86837ae69fSSean Fertile 87a93a33cbSSean Fertile if (Offset % getSectionHeaderSize() != 0) 88a93a33cbSSean Fertile report_fatal_error( 89a93a33cbSSean Fertile "Section header pointer does not point to a valid section header."); 90837ae69fSSean Fertile } 91837ae69fSSean Fertile 92837ae69fSSean Fertile const XCOFFSectionHeader32 * 93837ae69fSSean Fertile XCOFFObjectFile::toSection32(DataRefImpl Ref) const { 94837ae69fSSean Fertile assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 95837ae69fSSean Fertile #ifndef NDEBUG 96837ae69fSSean Fertile checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); 97a93a33cbSSean Fertile #endif 98837ae69fSSean Fertile return viewAs<XCOFFSectionHeader32>(Ref.p); 99837ae69fSSean Fertile } 100837ae69fSSean Fertile 101837ae69fSSean Fertile const XCOFFSectionHeader64 * 102837ae69fSSean Fertile XCOFFObjectFile::toSection64(DataRefImpl Ref) const { 103837ae69fSSean Fertile assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 104837ae69fSSean Fertile #ifndef NDEBUG 105837ae69fSSean Fertile checkSectionAddress(Ref.p, getSectionHeaderTableAddress()); 106837ae69fSSean Fertile #endif 107837ae69fSSean Fertile return viewAs<XCOFFSectionHeader64>(Ref.p); 108a93a33cbSSean Fertile } 109a93a33cbSSean Fertile 1109212206dSJason Liu const XCOFFSymbolEntry *XCOFFObjectFile::toSymbolEntry(DataRefImpl Ref) const { 111837ae69fSSean Fertile assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 1129212206dSJason Liu assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!"); 1137c72e82bSJason Liu #ifndef NDEBUG 1147c72e82bSJason Liu checkSymbolEntryPointer(Ref.p); 1157c72e82bSJason Liu #endif 1169212206dSJason Liu auto SymEntPtr = viewAs<XCOFFSymbolEntry>(Ref.p); 1179212206dSJason Liu return SymEntPtr; 1189212206dSJason Liu } 1199212206dSJason Liu 120837ae69fSSean Fertile const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const { 121837ae69fSSean Fertile assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 122837ae69fSSean Fertile return static_cast<const XCOFFFileHeader32 *>(FileHeader); 123a93a33cbSSean Fertile } 124a93a33cbSSean Fertile 125837ae69fSSean Fertile const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const { 126837ae69fSSean Fertile assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 127837ae69fSSean Fertile return static_cast<const XCOFFFileHeader64 *>(FileHeader); 128a93a33cbSSean Fertile } 129a93a33cbSSean Fertile 130837ae69fSSean Fertile const XCOFFSectionHeader32 * 131837ae69fSSean Fertile XCOFFObjectFile::sectionHeaderTable32() const { 132837ae69fSSean Fertile assert(!is64Bit() && "32-bit interface called on 64-bit object file."); 133837ae69fSSean Fertile return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable); 134837ae69fSSean Fertile } 135837ae69fSSean Fertile 136837ae69fSSean Fertile const XCOFFSectionHeader64 * 137837ae69fSSean Fertile XCOFFObjectFile::sectionHeaderTable64() const { 138837ae69fSSean Fertile assert(is64Bit() && "64-bit interface called on a 32-bit object file."); 139837ae69fSSean Fertile return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable); 140837ae69fSSean Fertile } 1419212206dSJason Liu 142ab2eb2bfSHubert Tong void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const { 1439212206dSJason Liu const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb); 1449212206dSJason Liu SymEntPtr += SymEntPtr->NumberOfAuxEntries + 1; 1457c72e82bSJason Liu #ifndef NDEBUG 1467c72e82bSJason Liu // This function is used by basic_symbol_iterator, which allows to 1477c72e82bSJason Liu // point to the end-of-symbol-table address. 1487c72e82bSJason Liu if (reinterpret_cast<uintptr_t>(SymEntPtr) != getEndOfSymbolTableAddress()) 1497c72e82bSJason Liu checkSymbolEntryPointer(reinterpret_cast<uintptr_t>(SymEntPtr)); 1507c72e82bSJason Liu #endif 1519212206dSJason Liu Symb.p = reinterpret_cast<uintptr_t>(SymEntPtr); 152ab2eb2bfSHubert Tong } 153ab2eb2bfSHubert Tong 1547c72e82bSJason Liu Expected<StringRef> 1557c72e82bSJason Liu XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const { 1567c72e82bSJason Liu // The byte offset is relative to the start of the string table. 1577c72e82bSJason Liu // A byte offset value of 0 is a null or zero-length symbol 1589212206dSJason Liu // name. A byte offset in the range 1 to 3 (inclusive) points into the length 1599212206dSJason Liu // field; as a soft-error recovery mechanism, we treat such cases as having an 1609212206dSJason Liu // offset of 0. 1619212206dSJason Liu if (Offset < 4) 1629212206dSJason Liu return StringRef(nullptr, 0); 1639212206dSJason Liu 1649212206dSJason Liu if (StringTable.Data != nullptr && StringTable.Size > Offset) 1659212206dSJason Liu return (StringTable.Data + Offset); 1669212206dSJason Liu 1677c72e82bSJason Liu return make_error<GenericBinaryError>("Bad offset for string table entry", 1689212206dSJason Liu object_error::parse_failed); 169ab2eb2bfSHubert Tong } 170ab2eb2bfSHubert Tong 1717c72e82bSJason Liu Expected<StringRef> 1727c72e82bSJason Liu XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const { 1737c72e82bSJason Liu if (CFileEntPtr->NameInStrTbl.Magic != 1747c72e82bSJason Liu XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC) 1757c72e82bSJason Liu return generateXCOFFFixedNameStringRef(CFileEntPtr->Name); 1767c72e82bSJason Liu return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset); 1777c72e82bSJason Liu } 1787c72e82bSJason Liu 1797c72e82bSJason Liu Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const { 1807c72e82bSJason Liu const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb); 1817c72e82bSJason Liu 1827c72e82bSJason Liu // A storage class value with the high-order bit on indicates that the name is 1837c72e82bSJason Liu // a symbolic debugger stabstring. 1847c72e82bSJason Liu if (SymEntPtr->StorageClass & 0x80) 1857c72e82bSJason Liu return StringRef("Unimplemented Debug Name"); 1867c72e82bSJason Liu 1877c72e82bSJason Liu if (SymEntPtr->NameInStrTbl.Magic != XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC) 1887c72e82bSJason Liu return generateXCOFFFixedNameStringRef(SymEntPtr->SymbolName); 1897c72e82bSJason Liu 1907c72e82bSJason Liu return getStringTableEntry(SymEntPtr->NameInStrTbl.Offset); 1917c72e82bSJason Liu } 1927c72e82bSJason Liu 193ab2eb2bfSHubert Tong Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const { 194*b91f798fSdiggerlin assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 195*b91f798fSdiggerlin return toSymbolEntry(Symb)->Value; 196ab2eb2bfSHubert Tong } 197ab2eb2bfSHubert Tong 198ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const { 1997c72e82bSJason Liu assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 2009212206dSJason Liu return toSymbolEntry(Symb)->Value; 201ab2eb2bfSHubert Tong } 202ab2eb2bfSHubert Tong 203ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const { 204ab2eb2bfSHubert Tong uint64_t Result = 0; 205ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 206ab2eb2bfSHubert Tong return Result; 207ab2eb2bfSHubert Tong } 208ab2eb2bfSHubert Tong 209ab2eb2bfSHubert Tong Expected<SymbolRef::Type> 210ab2eb2bfSHubert Tong XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const { 211ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 212ab2eb2bfSHubert Tong return SymbolRef::ST_Other; 213ab2eb2bfSHubert Tong } 214ab2eb2bfSHubert Tong 215ab2eb2bfSHubert Tong Expected<section_iterator> 216ab2eb2bfSHubert Tong XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const { 2179212206dSJason Liu const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb); 2189212206dSJason Liu int16_t SectNum = SymEntPtr->SectionNumber; 2199212206dSJason Liu 2209212206dSJason Liu if (isReservedSectionNumber(SectNum)) 2219212206dSJason Liu return section_end(); 2229212206dSJason Liu 223837ae69fSSean Fertile Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum); 224837ae69fSSean Fertile if (!ExpSec) 225837ae69fSSean Fertile return ExpSec.takeError(); 2269212206dSJason Liu 227837ae69fSSean Fertile return section_iterator(SectionRef(ExpSec.get(), this)); 228ab2eb2bfSHubert Tong } 229ab2eb2bfSHubert Tong 230ab2eb2bfSHubert Tong void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const { 231a93a33cbSSean Fertile const char *Ptr = reinterpret_cast<const char *>(Sec.p); 232a93a33cbSSean Fertile Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize()); 233ab2eb2bfSHubert Tong } 234ab2eb2bfSHubert Tong 2358be28cdcSFangrui Song Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const { 2367c72e82bSJason Liu return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec)); 237ab2eb2bfSHubert Tong } 238ab2eb2bfSHubert Tong 239ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const { 240210314aeSSean Fertile // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t 241210314aeSSean Fertile // with MSVC. 242210314aeSSean Fertile if (is64Bit()) 243210314aeSSean Fertile return toSection64(Sec)->VirtualAddress; 244210314aeSSean Fertile 245210314aeSSean Fertile return toSection32(Sec)->VirtualAddress; 246ab2eb2bfSHubert Tong } 247ab2eb2bfSHubert Tong 248ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const { 249a93a33cbSSean Fertile // Section numbers in XCOFF are numbered beginning at 1. A section number of 250a93a33cbSSean Fertile // zero is used to indicate that a symbol is being imported or is undefined. 251837ae69fSSean Fertile if (is64Bit()) 252837ae69fSSean Fertile return toSection64(Sec) - sectionHeaderTable64() + 1; 253837ae69fSSean Fertile else 254837ae69fSSean Fertile return toSection32(Sec) - sectionHeaderTable32() + 1; 255ab2eb2bfSHubert Tong } 256ab2eb2bfSHubert Tong 257ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const { 258210314aeSSean Fertile // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t 259210314aeSSean Fertile // with MSVC. 260210314aeSSean Fertile if (is64Bit()) 261210314aeSSean Fertile return toSection64(Sec)->SectionSize; 262210314aeSSean Fertile 263210314aeSSean Fertile return toSection32(Sec)->SectionSize; 264ab2eb2bfSHubert Tong } 265ab2eb2bfSHubert Tong 266e1cb2c0fSFangrui Song Expected<ArrayRef<uint8_t>> 267e1cb2c0fSFangrui Song XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const { 268*b91f798fSdiggerlin if (isSectionVirtual(Sec)) 269*b91f798fSdiggerlin return ArrayRef<uint8_t>(); 270*b91f798fSdiggerlin 271*b91f798fSdiggerlin const uint64_t OffsetToRaw = is64Bit() 272*b91f798fSdiggerlin ? toSection64(Sec)->FileOffsetToRawData 273*b91f798fSdiggerlin : toSection32(Sec)->FileOffsetToRawData; 274*b91f798fSdiggerlin 275*b91f798fSdiggerlin const uint8_t * ContentStart = base() + OffsetToRaw; 276*b91f798fSdiggerlin uint64_t SectionSize = getSectionSize(Sec); 277*b91f798fSdiggerlin if (checkOffset(Data, uintptr_t(ContentStart), SectionSize)) 278*b91f798fSdiggerlin return make_error<BinaryError>(); 279*b91f798fSdiggerlin 280*b91f798fSdiggerlin return makeArrayRef(ContentStart,SectionSize); 281ab2eb2bfSHubert Tong } 282ab2eb2bfSHubert Tong 283ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const { 284ab2eb2bfSHubert Tong uint64_t Result = 0; 285ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 286ab2eb2bfSHubert Tong return Result; 287ab2eb2bfSHubert Tong } 288ab2eb2bfSHubert Tong 289ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { 290ab2eb2bfSHubert Tong bool Result = false; 291ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 292ab2eb2bfSHubert Tong return Result; 293ab2eb2bfSHubert Tong } 294ab2eb2bfSHubert Tong 295ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const { 296837ae69fSSean Fertile return getSectionFlags(Sec) & XCOFF::STYP_TEXT; 297ab2eb2bfSHubert Tong } 298ab2eb2bfSHubert Tong 299ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const { 300837ae69fSSean Fertile uint32_t Flags = getSectionFlags(Sec); 301a93a33cbSSean Fertile return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA); 302ab2eb2bfSHubert Tong } 303ab2eb2bfSHubert Tong 304ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const { 305837ae69fSSean Fertile uint32_t Flags = getSectionFlags(Sec); 306a93a33cbSSean Fertile return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS); 307ab2eb2bfSHubert Tong } 308ab2eb2bfSHubert Tong 309ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const { 310*b91f798fSdiggerlin return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0 311*b91f798fSdiggerlin : toSection32(Sec)->FileOffsetToRawData == 0; 312ab2eb2bfSHubert Tong } 313ab2eb2bfSHubert Tong 314ab2eb2bfSHubert Tong relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const { 315ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 316ab2eb2bfSHubert Tong return relocation_iterator(RelocationRef()); 317ab2eb2bfSHubert Tong } 318ab2eb2bfSHubert Tong 319ab2eb2bfSHubert Tong relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const { 320ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 321ab2eb2bfSHubert Tong return relocation_iterator(RelocationRef()); 322ab2eb2bfSHubert Tong } 323ab2eb2bfSHubert Tong 324ab2eb2bfSHubert Tong void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 325ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 326ab2eb2bfSHubert Tong return; 327ab2eb2bfSHubert Tong } 328ab2eb2bfSHubert Tong 329ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const { 330ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 331ab2eb2bfSHubert Tong uint64_t Result = 0; 332ab2eb2bfSHubert Tong return Result; 333ab2eb2bfSHubert Tong } 334ab2eb2bfSHubert Tong 335ab2eb2bfSHubert Tong symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 336ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 337ab2eb2bfSHubert Tong return symbol_iterator(SymbolRef()); 338ab2eb2bfSHubert Tong } 339ab2eb2bfSHubert Tong 340ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const { 341ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 342ab2eb2bfSHubert Tong uint64_t Result = 0; 343ab2eb2bfSHubert Tong return Result; 344ab2eb2bfSHubert Tong } 345ab2eb2bfSHubert Tong 346ab2eb2bfSHubert Tong void XCOFFObjectFile::getRelocationTypeName( 347ab2eb2bfSHubert Tong DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 348ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 349ab2eb2bfSHubert Tong return; 350ab2eb2bfSHubert Tong } 351ab2eb2bfSHubert Tong 352ab2eb2bfSHubert Tong uint32_t XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const { 353ab2eb2bfSHubert Tong uint32_t Result = 0; 354ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 355ab2eb2bfSHubert Tong return Result; 356ab2eb2bfSHubert Tong } 357ab2eb2bfSHubert Tong 358ab2eb2bfSHubert Tong basic_symbol_iterator XCOFFObjectFile::symbol_begin() const { 359837ae69fSSean Fertile assert(!is64Bit() && "64-bit support not implemented yet."); 3609212206dSJason Liu DataRefImpl SymDRI; 3619212206dSJason Liu SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr); 3629212206dSJason Liu return basic_symbol_iterator(SymbolRef(SymDRI, this)); 363ab2eb2bfSHubert Tong } 364ab2eb2bfSHubert Tong 365ab2eb2bfSHubert Tong basic_symbol_iterator XCOFFObjectFile::symbol_end() const { 366837ae69fSSean Fertile assert(!is64Bit() && "64-bit support not implemented yet."); 3679212206dSJason Liu DataRefImpl SymDRI; 3689212206dSJason Liu SymDRI.p = reinterpret_cast<uintptr_t>( 369837ae69fSSean Fertile SymbolTblPtr + getLogicalNumberOfSymbolTableEntries32()); 3709212206dSJason Liu return basic_symbol_iterator(SymbolRef(SymDRI, this)); 371ab2eb2bfSHubert Tong } 372ab2eb2bfSHubert Tong 373ab2eb2bfSHubert Tong section_iterator XCOFFObjectFile::section_begin() const { 374a93a33cbSSean Fertile DataRefImpl DRI; 375837ae69fSSean Fertile DRI.p = getSectionHeaderTableAddress(); 376a93a33cbSSean Fertile return section_iterator(SectionRef(DRI, this)); 377ab2eb2bfSHubert Tong } 378ab2eb2bfSHubert Tong 379ab2eb2bfSHubert Tong section_iterator XCOFFObjectFile::section_end() const { 380a93a33cbSSean Fertile DataRefImpl DRI; 381837ae69fSSean Fertile DRI.p = getWithOffset(getSectionHeaderTableAddress(), 382837ae69fSSean Fertile getNumberOfSections() * getSectionHeaderSize()); 383a93a33cbSSean Fertile return section_iterator(SectionRef(DRI, this)); 384ab2eb2bfSHubert Tong } 385ab2eb2bfSHubert Tong 386837ae69fSSean Fertile uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; } 387ab2eb2bfSHubert Tong 388ab2eb2bfSHubert Tong StringRef XCOFFObjectFile::getFileFormatName() const { 389837ae69fSSean Fertile return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000"; 390ab2eb2bfSHubert Tong } 391ab2eb2bfSHubert Tong 392ab2eb2bfSHubert Tong Triple::ArchType XCOFFObjectFile::getArch() const { 393837ae69fSSean Fertile return is64Bit() ? Triple::ppc64 : Triple::ppc; 394ab2eb2bfSHubert Tong } 395ab2eb2bfSHubert Tong 396ab2eb2bfSHubert Tong SubtargetFeatures XCOFFObjectFile::getFeatures() const { 397ab2eb2bfSHubert Tong return SubtargetFeatures(); 398ab2eb2bfSHubert Tong } 399ab2eb2bfSHubert Tong 400ab2eb2bfSHubert Tong bool XCOFFObjectFile::isRelocatableObject() const { 401ab2eb2bfSHubert Tong bool Result = false; 402ab2eb2bfSHubert Tong llvm_unreachable("Not yet implemented!"); 403ab2eb2bfSHubert Tong return Result; 404ab2eb2bfSHubert Tong } 405ab2eb2bfSHubert Tong 406a93a33cbSSean Fertile Expected<uint64_t> XCOFFObjectFile::getStartAddress() const { 407a93a33cbSSean Fertile // TODO FIXME Should get from auxiliary_header->o_entry when support for the 408a93a33cbSSean Fertile // auxiliary_header is added. 409a93a33cbSSean Fertile return 0; 410a93a33cbSSean Fertile } 411a93a33cbSSean Fertile 412837ae69fSSean Fertile size_t XCOFFObjectFile::getFileHeaderSize() const { 413837ae69fSSean Fertile return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32); 4149212206dSJason Liu } 4159212206dSJason Liu 416837ae69fSSean Fertile size_t XCOFFObjectFile::getSectionHeaderSize() const { 417837ae69fSSean Fertile return is64Bit() ? sizeof(XCOFFSectionHeader64) : 418837ae69fSSean Fertile sizeof(XCOFFSectionHeader32); 419837ae69fSSean Fertile } 420837ae69fSSean Fertile 421837ae69fSSean Fertile bool XCOFFObjectFile::is64Bit() const { 422837ae69fSSean Fertile return Binary::ID_XCOFF64 == getType(); 423837ae69fSSean Fertile } 424837ae69fSSean Fertile 425837ae69fSSean Fertile uint16_t XCOFFObjectFile::getMagic() const { 426837ae69fSSean Fertile return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic; 427837ae69fSSean Fertile } 428837ae69fSSean Fertile 429837ae69fSSean Fertile Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const { 430837ae69fSSean Fertile if (Num <= 0 || Num > getNumberOfSections()) 431837ae69fSSean Fertile return errorCodeToError(object_error::invalid_section_index); 432837ae69fSSean Fertile 433837ae69fSSean Fertile DataRefImpl DRI; 434837ae69fSSean Fertile DRI.p = getWithOffset(getSectionHeaderTableAddress(), 435837ae69fSSean Fertile getSectionHeaderSize() * (Num - 1)); 436837ae69fSSean Fertile return DRI; 4379212206dSJason Liu } 4389212206dSJason Liu 4399212206dSJason Liu Expected<StringRef> 4409212206dSJason Liu XCOFFObjectFile::getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const { 441837ae69fSSean Fertile assert(!is64Bit() && "Symbol table support not implemented for 64-bit."); 4429212206dSJason Liu int16_t SectionNum = SymEntPtr->SectionNumber; 4439212206dSJason Liu 4449212206dSJason Liu switch (SectionNum) { 4459212206dSJason Liu case XCOFF::N_DEBUG: 4469212206dSJason Liu return "N_DEBUG"; 4479212206dSJason Liu case XCOFF::N_ABS: 4489212206dSJason Liu return "N_ABS"; 4499212206dSJason Liu case XCOFF::N_UNDEF: 4509212206dSJason Liu return "N_UNDEF"; 451837ae69fSSean Fertile default: 452837ae69fSSean Fertile Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum); 453837ae69fSSean Fertile if (SecRef) 4547c72e82bSJason Liu return generateXCOFFFixedNameStringRef( 4557c72e82bSJason Liu getSectionNameInternal(SecRef.get())); 456837ae69fSSean Fertile return SecRef.takeError(); 4579212206dSJason Liu } 4589212206dSJason Liu } 4599212206dSJason Liu 4609212206dSJason Liu bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) { 4619212206dSJason Liu return (SectionNumber <= 0 && SectionNumber >= -2); 4629212206dSJason Liu } 4639212206dSJason Liu 4649212206dSJason Liu uint16_t XCOFFObjectFile::getNumberOfSections() const { 465837ae69fSSean Fertile return is64Bit() ? fileHeader64()->NumberOfSections 466837ae69fSSean Fertile : fileHeader32()->NumberOfSections; 4679212206dSJason Liu } 4689212206dSJason Liu 469837ae69fSSean Fertile int32_t XCOFFObjectFile::getTimeStamp() const { 470837ae69fSSean Fertile return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp; 4719212206dSJason Liu } 4729212206dSJason Liu 4739212206dSJason Liu uint16_t XCOFFObjectFile::getOptionalHeaderSize() const { 474837ae69fSSean Fertile return is64Bit() ? fileHeader64()->AuxHeaderSize 475837ae69fSSean Fertile : fileHeader32()->AuxHeaderSize; 4769212206dSJason Liu } 4779212206dSJason Liu 478837ae69fSSean Fertile uint32_t XCOFFObjectFile::getSymbolTableOffset32() const { 479837ae69fSSean Fertile return fileHeader32()->SymbolTableOffset; 480837ae69fSSean Fertile } 481ab2eb2bfSHubert Tong 482837ae69fSSean Fertile int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const { 483837ae69fSSean Fertile // As far as symbol table size is concerned, if this field is negative it is 484837ae69fSSean Fertile // to be treated as a 0. However since this field is also used for printing we 485837ae69fSSean Fertile // don't want to truncate any negative values. 486837ae69fSSean Fertile return fileHeader32()->NumberOfSymTableEntries; 487837ae69fSSean Fertile } 488ab2eb2bfSHubert Tong 489837ae69fSSean Fertile uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const { 490837ae69fSSean Fertile return (fileHeader32()->NumberOfSymTableEntries >= 0 491837ae69fSSean Fertile ? fileHeader32()->NumberOfSymTableEntries 492837ae69fSSean Fertile : 0); 493837ae69fSSean Fertile } 494a93a33cbSSean Fertile 495837ae69fSSean Fertile uint64_t XCOFFObjectFile::getSymbolTableOffset64() const { 496837ae69fSSean Fertile return fileHeader64()->SymbolTableOffset; 497837ae69fSSean Fertile } 498837ae69fSSean Fertile 499837ae69fSSean Fertile uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const { 500837ae69fSSean Fertile return fileHeader64()->NumberOfSymTableEntries; 501837ae69fSSean Fertile } 502837ae69fSSean Fertile 5037c72e82bSJason Liu uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const { 5047c72e82bSJason Liu uint32_t NumberOfSymTableEntries = 5057c72e82bSJason Liu is64Bit() ? getNumberOfSymbolTableEntries64() 5067c72e82bSJason Liu : getLogicalNumberOfSymbolTableEntries32(); 5077c72e82bSJason Liu return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr), 5087c72e82bSJason Liu XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries); 5097c72e82bSJason Liu } 5107c72e82bSJason Liu 5117c72e82bSJason Liu void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const { 5127c72e82bSJason Liu if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr)) 5137c72e82bSJason Liu report_fatal_error("Symbol table entry is outside of symbol table."); 5147c72e82bSJason Liu 5157c72e82bSJason Liu if (SymbolEntPtr >= getEndOfSymbolTableAddress()) 5167c72e82bSJason Liu report_fatal_error("Symbol table entry is outside of symbol table."); 5177c72e82bSJason Liu 5187c72e82bSJason Liu ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) - 5197c72e82bSJason Liu reinterpret_cast<const char *>(SymbolTblPtr); 5207c72e82bSJason Liu 5217c72e82bSJason Liu if (Offset % XCOFF::SymbolTableEntrySize != 0) 5227c72e82bSJason Liu report_fatal_error( 5237c72e82bSJason Liu "Symbol table entry position is not valid inside of symbol table."); 5247c72e82bSJason Liu } 5257c72e82bSJason Liu 5267c72e82bSJason Liu uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const { 5277c72e82bSJason Liu return (reinterpret_cast<const char *>(SymbolEntPtr) - 5287c72e82bSJason Liu reinterpret_cast<const char *>(SymbolTblPtr)) / 5297c72e82bSJason Liu XCOFF::SymbolTableEntrySize; 5307c72e82bSJason Liu } 5317c72e82bSJason Liu 53234d4bff3SDigger Lin Expected<StringRef> 53334d4bff3SDigger Lin XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const { 53434d4bff3SDigger Lin if (is64Bit()) 53534d4bff3SDigger Lin report_fatal_error("64-bit symbol table support not implemented yet."); 53634d4bff3SDigger Lin 53734d4bff3SDigger Lin if (Index >= getLogicalNumberOfSymbolTableEntries32()) 53834d4bff3SDigger Lin return errorCodeToError(object_error::invalid_symbol_index); 53934d4bff3SDigger Lin 54034d4bff3SDigger Lin DataRefImpl SymDRI; 54134d4bff3SDigger Lin SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index); 54234d4bff3SDigger Lin return getSymbolName(SymDRI); 54334d4bff3SDigger Lin } 54434d4bff3SDigger Lin 545837ae69fSSean Fertile uint16_t XCOFFObjectFile::getFlags() const { 546837ae69fSSean Fertile return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags; 547837ae69fSSean Fertile } 548837ae69fSSean Fertile 549837ae69fSSean Fertile const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const { 550837ae69fSSean Fertile return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name; 551837ae69fSSean Fertile } 552837ae69fSSean Fertile 553837ae69fSSean Fertile uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const { 554837ae69fSSean Fertile return reinterpret_cast<uintptr_t>(SectionHeaderTable); 555837ae69fSSean Fertile } 556837ae69fSSean Fertile 557837ae69fSSean Fertile int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const { 558837ae69fSSean Fertile return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags; 559837ae69fSSean Fertile } 560837ae69fSSean Fertile 561837ae69fSSean Fertile XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object) 562837ae69fSSean Fertile : ObjectFile(Type, Object) { 563837ae69fSSean Fertile assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64); 564837ae69fSSean Fertile } 565837ae69fSSean Fertile 566837ae69fSSean Fertile ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const { 567837ae69fSSean Fertile assert(is64Bit() && "64-bit interface called for non 64-bit file."); 568837ae69fSSean Fertile const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64(); 569837ae69fSSean Fertile return ArrayRef<XCOFFSectionHeader64>(TablePtr, 570837ae69fSSean Fertile TablePtr + getNumberOfSections()); 571837ae69fSSean Fertile } 572837ae69fSSean Fertile 573837ae69fSSean Fertile ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const { 574837ae69fSSean Fertile assert(!is64Bit() && "32-bit interface called for non 32-bit file."); 575837ae69fSSean Fertile const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32(); 576837ae69fSSean Fertile return ArrayRef<XCOFFSectionHeader32>(TablePtr, 577837ae69fSSean Fertile TablePtr + getNumberOfSections()); 578837ae69fSSean Fertile } 579837ae69fSSean Fertile 58034d4bff3SDigger Lin // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO 58134d4bff3SDigger Lin // section header contains the actual count of relocation entries in the s_paddr 58234d4bff3SDigger Lin // field. STYP_OVRFLO headers contain the section index of their corresponding 58334d4bff3SDigger Lin // sections as their raw "NumberOfRelocations" field value. 58434d4bff3SDigger Lin Expected<uint32_t> XCOFFObjectFile::getLogicalNumberOfRelocationEntries( 58534d4bff3SDigger Lin const XCOFFSectionHeader32 &Sec) const { 58634d4bff3SDigger Lin 58734d4bff3SDigger Lin uint16_t SectionIndex = &Sec - sectionHeaderTable32() + 1; 58834d4bff3SDigger Lin 58934d4bff3SDigger Lin if (Sec.NumberOfRelocations < RELOC_OVERFLOW) 59034d4bff3SDigger Lin return Sec.NumberOfRelocations; 59134d4bff3SDigger Lin for (const auto &Sec : sections32()) { 59234d4bff3SDigger Lin if (Sec.Flags == XCOFF::STYP_OVRFLO && 59334d4bff3SDigger Lin Sec.NumberOfRelocations == SectionIndex) 59434d4bff3SDigger Lin return Sec.PhysicalAddress; 59534d4bff3SDigger Lin } 59634d4bff3SDigger Lin return errorCodeToError(object_error::parse_failed); 59734d4bff3SDigger Lin } 59834d4bff3SDigger Lin 59934d4bff3SDigger Lin Expected<ArrayRef<XCOFFRelocation32>> 60034d4bff3SDigger Lin XCOFFObjectFile::relocations(const XCOFFSectionHeader32 &Sec) const { 60134d4bff3SDigger Lin uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader), 60234d4bff3SDigger Lin Sec.FileOffsetToRelocationInfo); 60334d4bff3SDigger Lin auto NumRelocEntriesOrErr = getLogicalNumberOfRelocationEntries(Sec); 60434d4bff3SDigger Lin if (Error E = NumRelocEntriesOrErr.takeError()) 60534d4bff3SDigger Lin return std::move(E); 60634d4bff3SDigger Lin 60734d4bff3SDigger Lin uint32_t NumRelocEntries = NumRelocEntriesOrErr.get(); 60834d4bff3SDigger Lin 60934d4bff3SDigger Lin auto RelocationOrErr = 61034d4bff3SDigger Lin getObject<XCOFFRelocation32>(Data, reinterpret_cast<void *>(RelocAddr), 61134d4bff3SDigger Lin NumRelocEntries * sizeof(XCOFFRelocation32)); 61234d4bff3SDigger Lin if (Error E = RelocationOrErr.takeError()) 61334d4bff3SDigger Lin return std::move(E); 61434d4bff3SDigger Lin 61534d4bff3SDigger Lin const XCOFFRelocation32 *StartReloc = RelocationOrErr.get(); 61634d4bff3SDigger Lin 61734d4bff3SDigger Lin return ArrayRef<XCOFFRelocation32>(StartReloc, StartReloc + NumRelocEntries); 61834d4bff3SDigger Lin } 61934d4bff3SDigger Lin 620837ae69fSSean Fertile Expected<XCOFFStringTable> 621837ae69fSSean Fertile XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) { 622837ae69fSSean Fertile // If there is a string table, then the buffer must contain at least 4 bytes 623837ae69fSSean Fertile // for the string table's size. Not having a string table is not an error. 624837ae69fSSean Fertile if (auto EC = Binary::checkOffset( 625837ae69fSSean Fertile Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) 626837ae69fSSean Fertile return XCOFFStringTable{0, nullptr}; 627837ae69fSSean Fertile 628837ae69fSSean Fertile // Read the size out of the buffer. 629837ae69fSSean Fertile uint32_t Size = support::endian::read32be(Obj->base() + Offset); 630837ae69fSSean Fertile 631837ae69fSSean Fertile // If the size is less then 4, then the string table is just a size and no 632837ae69fSSean Fertile // string data. 633837ae69fSSean Fertile if (Size <= 4) 634837ae69fSSean Fertile return XCOFFStringTable{4, nullptr}; 635837ae69fSSean Fertile 636837ae69fSSean Fertile auto StringTableOrErr = 637837ae69fSSean Fertile getObject<char>(Obj->Data, Obj->base() + Offset, Size); 638837ae69fSSean Fertile if (Error E = StringTableOrErr.takeError()) 639837ae69fSSean Fertile return std::move(E); 640837ae69fSSean Fertile 641837ae69fSSean Fertile const char *StringTablePtr = StringTableOrErr.get(); 642837ae69fSSean Fertile if (StringTablePtr[Size - 1] != '\0') 643837ae69fSSean Fertile return errorCodeToError(object_error::string_table_non_null_end); 644837ae69fSSean Fertile 645837ae69fSSean Fertile return XCOFFStringTable{Size, StringTablePtr}; 646837ae69fSSean Fertile } 647837ae69fSSean Fertile 648837ae69fSSean Fertile Expected<std::unique_ptr<XCOFFObjectFile>> 649837ae69fSSean Fertile XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) { 6500eaee545SJonas Devlieghere // Can't use std::make_unique because of the private constructor. 651837ae69fSSean Fertile std::unique_ptr<XCOFFObjectFile> Obj; 652837ae69fSSean Fertile Obj.reset(new XCOFFObjectFile(Type, MBR)); 653837ae69fSSean Fertile 654837ae69fSSean Fertile uint64_t CurOffset = 0; 655837ae69fSSean Fertile const auto *Base = Obj->base(); 656837ae69fSSean Fertile MemoryBufferRef Data = Obj->Data; 657837ae69fSSean Fertile 658837ae69fSSean Fertile // Parse file header. 659837ae69fSSean Fertile auto FileHeaderOrErr = 660837ae69fSSean Fertile getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize()); 661837ae69fSSean Fertile if (Error E = FileHeaderOrErr.takeError()) 662837ae69fSSean Fertile return std::move(E); 663837ae69fSSean Fertile Obj->FileHeader = FileHeaderOrErr.get(); 664837ae69fSSean Fertile 665837ae69fSSean Fertile CurOffset += Obj->getFileHeaderSize(); 666a93a33cbSSean Fertile // TODO FIXME we don't have support for an optional header yet, so just skip 667a93a33cbSSean Fertile // past it. 668837ae69fSSean Fertile CurOffset += Obj->getOptionalHeaderSize(); 669a93a33cbSSean Fertile 670837ae69fSSean Fertile // Parse the section header table if it is present. 671837ae69fSSean Fertile if (Obj->getNumberOfSections()) { 672837ae69fSSean Fertile auto SecHeadersOrErr = getObject<void>(Data, Base + CurOffset, 673837ae69fSSean Fertile Obj->getNumberOfSections() * 674837ae69fSSean Fertile Obj->getSectionHeaderSize()); 675837ae69fSSean Fertile if (Error E = SecHeadersOrErr.takeError()) 676837ae69fSSean Fertile return std::move(E); 677837ae69fSSean Fertile Obj->SectionHeaderTable = SecHeadersOrErr.get(); 678a93a33cbSSean Fertile } 679ab2eb2bfSHubert Tong 680837ae69fSSean Fertile // 64-bit object supports only file header and section headers for now. 681837ae69fSSean Fertile if (Obj->is64Bit()) 682837ae69fSSean Fertile return std::move(Obj); 683fd75ee91SSean Fertile 684837ae69fSSean Fertile // If there is no symbol table we are done parsing the memory buffer. 685837ae69fSSean Fertile if (Obj->getLogicalNumberOfSymbolTableEntries32() == 0) 686837ae69fSSean Fertile return std::move(Obj); 687837ae69fSSean Fertile 688837ae69fSSean Fertile // Parse symbol table. 689837ae69fSSean Fertile CurOffset = Obj->fileHeader32()->SymbolTableOffset; 6909212206dSJason Liu uint64_t SymbolTableSize = (uint64_t)(sizeof(XCOFFSymbolEntry)) * 691837ae69fSSean Fertile Obj->getLogicalNumberOfSymbolTableEntries32(); 692837ae69fSSean Fertile auto SymTableOrErr = 693837ae69fSSean Fertile getObject<XCOFFSymbolEntry>(Data, Base + CurOffset, SymbolTableSize); 694837ae69fSSean Fertile if (Error E = SymTableOrErr.takeError()) 695837ae69fSSean Fertile return std::move(E); 696837ae69fSSean Fertile Obj->SymbolTblPtr = SymTableOrErr.get(); 697837ae69fSSean Fertile CurOffset += SymbolTableSize; 698fd75ee91SSean Fertile 699837ae69fSSean Fertile // Parse String table. 700837ae69fSSean Fertile Expected<XCOFFStringTable> StringTableOrErr = 701837ae69fSSean Fertile parseStringTable(Obj.get(), CurOffset); 702837ae69fSSean Fertile if (Error E = StringTableOrErr.takeError()) 703837ae69fSSean Fertile return std::move(E); 704837ae69fSSean Fertile Obj->StringTable = StringTableOrErr.get(); 705fd75ee91SSean Fertile 706837ae69fSSean Fertile return std::move(Obj); 707fd75ee91SSean Fertile } 708fd75ee91SSean Fertile 709ab2eb2bfSHubert Tong Expected<std::unique_ptr<ObjectFile>> 710837ae69fSSean Fertile ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef, 711837ae69fSSean Fertile unsigned FileType) { 712837ae69fSSean Fertile return XCOFFObjectFile::create(FileType, MemBufRef); 713ab2eb2bfSHubert Tong } 714ab2eb2bfSHubert Tong 7157c72e82bSJason Liu XCOFF::StorageClass XCOFFSymbolRef::getStorageClass() const { 7167c72e82bSJason Liu return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->StorageClass; 7177c72e82bSJason Liu } 7187c72e82bSJason Liu 7197c72e82bSJason Liu uint8_t XCOFFSymbolRef::getNumberOfAuxEntries() const { 7207c72e82bSJason Liu return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->NumberOfAuxEntries; 7217c72e82bSJason Liu } 7227c72e82bSJason Liu 7237c72e82bSJason Liu const XCOFFCsectAuxEnt32 *XCOFFSymbolRef::getXCOFFCsectAuxEnt32() const { 7247c72e82bSJason Liu assert(!OwningObjectPtr->is64Bit() && 7257c72e82bSJason Liu "32-bit interface called on 64-bit object file."); 7267c72e82bSJason Liu assert(hasCsectAuxEnt() && "No Csect Auxiliary Entry is found."); 7277c72e82bSJason Liu 7287c72e82bSJason Liu // In XCOFF32, the csect auxilliary entry is always the last auxiliary 7297c72e82bSJason Liu // entry for the symbol. 7307c72e82bSJason Liu uintptr_t AuxAddr = getWithOffset( 7317c72e82bSJason Liu SymEntDataRef.p, XCOFF::SymbolTableEntrySize * getNumberOfAuxEntries()); 7327c72e82bSJason Liu 7337c72e82bSJason Liu #ifndef NDEBUG 7347c72e82bSJason Liu OwningObjectPtr->checkSymbolEntryPointer(AuxAddr); 7357c72e82bSJason Liu #endif 7367c72e82bSJason Liu 7377c72e82bSJason Liu return reinterpret_cast<const XCOFFCsectAuxEnt32 *>(AuxAddr); 7387c72e82bSJason Liu } 7397c72e82bSJason Liu 7407c72e82bSJason Liu uint16_t XCOFFSymbolRef::getType() const { 7417c72e82bSJason Liu return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SymbolType; 7427c72e82bSJason Liu } 7437c72e82bSJason Liu 7447c72e82bSJason Liu int16_t XCOFFSymbolRef::getSectionNumber() const { 7457c72e82bSJason Liu return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SectionNumber; 7467c72e82bSJason Liu } 7477c72e82bSJason Liu 7487c72e82bSJason Liu bool XCOFFSymbolRef::hasCsectAuxEnt() const { 7497c72e82bSJason Liu XCOFF::StorageClass SC = getStorageClass(); 7507c72e82bSJason Liu return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT || 7517c72e82bSJason Liu SC == XCOFF::C_HIDEXT); 7527c72e82bSJason Liu } 7537c72e82bSJason Liu 7547c72e82bSJason Liu bool XCOFFSymbolRef::isFunction() const { 7557c72e82bSJason Liu if (OwningObjectPtr->is64Bit()) 7567c72e82bSJason Liu report_fatal_error("64-bit support is unimplemented yet."); 7577c72e82bSJason Liu 7587c72e82bSJason Liu if (getType() & FUNCTION_SYM) 7597c72e82bSJason Liu return true; 7607c72e82bSJason Liu 7617c72e82bSJason Liu if (!hasCsectAuxEnt()) 7627c72e82bSJason Liu return false; 7637c72e82bSJason Liu 7647c72e82bSJason Liu const XCOFFCsectAuxEnt32 *CsectAuxEnt = getXCOFFCsectAuxEnt32(); 7657c72e82bSJason Liu 7667c72e82bSJason Liu // A function definition should be a label definition. 7677c72e82bSJason Liu if ((CsectAuxEnt->SymbolAlignmentAndType & SYM_TYPE_MASK) != XCOFF::XTY_LD) 7687c72e82bSJason Liu return false; 7697c72e82bSJason Liu 7707c72e82bSJason Liu if (CsectAuxEnt->StorageMappingClass != XCOFF::XMC_PR) 7717c72e82bSJason Liu return false; 7727c72e82bSJason Liu 7737c72e82bSJason Liu int16_t SectNum = getSectionNumber(); 7747c72e82bSJason Liu Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum); 7757c72e82bSJason Liu if (!SI) 7767c72e82bSJason Liu return false; 7777c72e82bSJason Liu 7787c72e82bSJason Liu return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT); 779ab2eb2bfSHubert Tong } 780ab2eb2bfSHubert Tong 781f8622543SFangrui Song // Explictly instantiate template classes. 782f8622543SFangrui Song template struct XCOFFSectionHeader<XCOFFSectionHeader32>; 783f8622543SFangrui Song template struct XCOFFSectionHeader<XCOFFSectionHeader64>; 784f8622543SFangrui Song 785ab2eb2bfSHubert Tong } // namespace object 786ab2eb2bfSHubert Tong } // namespace llvm 787