10b57cec5SDimitry Andric //===--- XCOFFObjectFile.cpp - XCOFF object file implementation -----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the XCOFFObjectFile class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/Object/XCOFFObjectFile.h"
14*5f7ddb14SDimitry Andric #include "llvm/ADT/StringSwitch.h"
155ffd83dbSDimitry Andric #include "llvm/MC/SubtargetFeature.h"
16af732203SDimitry Andric #include "llvm/Support/DataExtractor.h"
170b57cec5SDimitry Andric #include <cstddef>
180b57cec5SDimitry Andric #include <cstring>
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric namespace llvm {
21af732203SDimitry Andric
22af732203SDimitry Andric using namespace XCOFF;
23af732203SDimitry Andric
240b57cec5SDimitry Andric namespace object {
250b57cec5SDimitry Andric
265ffd83dbSDimitry Andric static const uint8_t FunctionSym = 0x20;
275ffd83dbSDimitry Andric static const uint16_t NoRelMask = 0x0001;
28*5f7ddb14SDimitry Andric static const size_t SymbolAuxTypeOffset = 17;
298bcb0991SDimitry Andric
300b57cec5SDimitry Andric // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer
310b57cec5SDimitry Andric // 'M'. Returns a pointer to the underlying object on success.
320b57cec5SDimitry Andric template <typename T>
getObject(MemoryBufferRef M,const void * Ptr,const uint64_t Size=sizeof (T))330b57cec5SDimitry Andric static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
340b57cec5SDimitry Andric const uint64_t Size = sizeof(T)) {
35af732203SDimitry Andric uintptr_t Addr = reinterpret_cast<uintptr_t>(Ptr);
365ffd83dbSDimitry Andric if (Error E = Binary::checkOffset(M, Addr, Size))
375ffd83dbSDimitry Andric return std::move(E);
380b57cec5SDimitry Andric return reinterpret_cast<const T *>(Addr);
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric
getWithOffset(uintptr_t Base,ptrdiff_t Offset)410b57cec5SDimitry Andric static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) {
420b57cec5SDimitry Andric return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) +
430b57cec5SDimitry Andric Offset);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric
viewAs(uintptr_t in)460b57cec5SDimitry Andric template <typename T> static const T *viewAs(uintptr_t in) {
470b57cec5SDimitry Andric return reinterpret_cast<const T *>(in);
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric
generateXCOFFFixedNameStringRef(const char * Name)508bcb0991SDimitry Andric static StringRef generateXCOFFFixedNameStringRef(const char *Name) {
518bcb0991SDimitry Andric auto NulCharPtr =
528bcb0991SDimitry Andric static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize));
530b57cec5SDimitry Andric return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
548bcb0991SDimitry Andric : StringRef(Name, XCOFF::NameSize);
558bcb0991SDimitry Andric }
568bcb0991SDimitry Andric
getName() const57480093f4SDimitry Andric template <typename T> StringRef XCOFFSectionHeader<T>::getName() const {
58480093f4SDimitry Andric const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
59480093f4SDimitry Andric return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name);
60480093f4SDimitry Andric }
61480093f4SDimitry Andric
getSectionType() const62480093f4SDimitry Andric template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const {
63480093f4SDimitry Andric const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
64480093f4SDimitry Andric return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask;
65480093f4SDimitry Andric }
66480093f4SDimitry Andric
67480093f4SDimitry Andric template <typename T>
isReservedSectionType() const68480093f4SDimitry Andric bool XCOFFSectionHeader<T>::isReservedSectionType() const {
69480093f4SDimitry Andric return getSectionType() & SectionFlagsReservedMask;
70480093f4SDimitry Andric }
71480093f4SDimitry Andric
isRelocationSigned() const728bcb0991SDimitry Andric bool XCOFFRelocation32::isRelocationSigned() const {
738bcb0991SDimitry Andric return Info & XR_SIGN_INDICATOR_MASK;
748bcb0991SDimitry Andric }
758bcb0991SDimitry Andric
isFixupIndicated() const768bcb0991SDimitry Andric bool XCOFFRelocation32::isFixupIndicated() const {
778bcb0991SDimitry Andric return Info & XR_FIXUP_INDICATOR_MASK;
788bcb0991SDimitry Andric }
798bcb0991SDimitry Andric
getRelocatedLength() const808bcb0991SDimitry Andric uint8_t XCOFFRelocation32::getRelocatedLength() const {
818bcb0991SDimitry Andric // The relocation encodes the bit length being relocated minus 1. Add back
828bcb0991SDimitry Andric // the 1 to get the actual length being relocated.
838bcb0991SDimitry Andric return (Info & XR_BIASED_LENGTH_MASK) + 1;
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric
86*5f7ddb14SDimitry Andric uintptr_t
getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress,uint32_t Distance)87*5f7ddb14SDimitry Andric XCOFFObjectFile::getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress,
88*5f7ddb14SDimitry Andric uint32_t Distance) {
89*5f7ddb14SDimitry Andric return getWithOffset(CurrentAddress, Distance * XCOFF::SymbolTableEntrySize);
90*5f7ddb14SDimitry Andric }
91*5f7ddb14SDimitry Andric
92*5f7ddb14SDimitry Andric const XCOFF::SymbolAuxType *
getSymbolAuxType(uintptr_t AuxEntryAddress) const93*5f7ddb14SDimitry Andric XCOFFObjectFile::getSymbolAuxType(uintptr_t AuxEntryAddress) const {
94*5f7ddb14SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
95*5f7ddb14SDimitry Andric return viewAs<XCOFF::SymbolAuxType>(
96*5f7ddb14SDimitry Andric getWithOffset(AuxEntryAddress, SymbolAuxTypeOffset));
97*5f7ddb14SDimitry Andric }
98*5f7ddb14SDimitry Andric
checkSectionAddress(uintptr_t Addr,uintptr_t TableAddress) const990b57cec5SDimitry Andric void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr,
1000b57cec5SDimitry Andric uintptr_t TableAddress) const {
1010b57cec5SDimitry Andric if (Addr < TableAddress)
1020b57cec5SDimitry Andric report_fatal_error("Section header outside of section header table.");
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric uintptr_t Offset = Addr - TableAddress;
1050b57cec5SDimitry Andric if (Offset >= getSectionHeaderSize() * getNumberOfSections())
1060b57cec5SDimitry Andric report_fatal_error("Section header outside of section header table.");
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric if (Offset % getSectionHeaderSize() != 0)
1090b57cec5SDimitry Andric report_fatal_error(
1100b57cec5SDimitry Andric "Section header pointer does not point to a valid section header.");
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric const XCOFFSectionHeader32 *
toSection32(DataRefImpl Ref) const1140b57cec5SDimitry Andric XCOFFObjectFile::toSection32(DataRefImpl Ref) const {
1150b57cec5SDimitry Andric assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
1160b57cec5SDimitry Andric #ifndef NDEBUG
1170b57cec5SDimitry Andric checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
1180b57cec5SDimitry Andric #endif
1190b57cec5SDimitry Andric return viewAs<XCOFFSectionHeader32>(Ref.p);
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric const XCOFFSectionHeader64 *
toSection64(DataRefImpl Ref) const1230b57cec5SDimitry Andric XCOFFObjectFile::toSection64(DataRefImpl Ref) const {
1240b57cec5SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
1250b57cec5SDimitry Andric #ifndef NDEBUG
1260b57cec5SDimitry Andric checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
1270b57cec5SDimitry Andric #endif
1280b57cec5SDimitry Andric return viewAs<XCOFFSectionHeader64>(Ref.p);
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric
toSymbolRef(DataRefImpl Ref) const131*5f7ddb14SDimitry Andric XCOFFSymbolRef XCOFFObjectFile::toSymbolRef(DataRefImpl Ref) const {
1320b57cec5SDimitry Andric assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!");
1338bcb0991SDimitry Andric #ifndef NDEBUG
1348bcb0991SDimitry Andric checkSymbolEntryPointer(Ref.p);
1358bcb0991SDimitry Andric #endif
136*5f7ddb14SDimitry Andric return XCOFFSymbolRef(Ref, this);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric
fileHeader32() const1390b57cec5SDimitry Andric const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const {
1400b57cec5SDimitry Andric assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
1410b57cec5SDimitry Andric return static_cast<const XCOFFFileHeader32 *>(FileHeader);
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric
fileHeader64() const1440b57cec5SDimitry Andric const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const {
1450b57cec5SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
1460b57cec5SDimitry Andric return static_cast<const XCOFFFileHeader64 *>(FileHeader);
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric const XCOFFSectionHeader32 *
sectionHeaderTable32() const1500b57cec5SDimitry Andric XCOFFObjectFile::sectionHeaderTable32() const {
1510b57cec5SDimitry Andric assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
1520b57cec5SDimitry Andric return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable);
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric const XCOFFSectionHeader64 *
sectionHeaderTable64() const1560b57cec5SDimitry Andric XCOFFObjectFile::sectionHeaderTable64() const {
1570b57cec5SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
1580b57cec5SDimitry Andric return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable);
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric
moveSymbolNext(DataRefImpl & Symb) const1610b57cec5SDimitry Andric void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
162*5f7ddb14SDimitry Andric uintptr_t NextSymbolAddr = getAdvancedSymbolEntryAddress(
163*5f7ddb14SDimitry Andric Symb.p, toSymbolRef(Symb).getNumberOfAuxEntries() + 1);
1648bcb0991SDimitry Andric #ifndef NDEBUG
1658bcb0991SDimitry Andric // This function is used by basic_symbol_iterator, which allows to
1668bcb0991SDimitry Andric // point to the end-of-symbol-table address.
167*5f7ddb14SDimitry Andric if (NextSymbolAddr != getEndOfSymbolTableAddress())
168*5f7ddb14SDimitry Andric checkSymbolEntryPointer(NextSymbolAddr);
1698bcb0991SDimitry Andric #endif
170*5f7ddb14SDimitry Andric Symb.p = NextSymbolAddr;
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric
1738bcb0991SDimitry Andric Expected<StringRef>
getStringTableEntry(uint32_t Offset) const1748bcb0991SDimitry Andric XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
1758bcb0991SDimitry Andric // The byte offset is relative to the start of the string table.
1768bcb0991SDimitry Andric // A byte offset value of 0 is a null or zero-length symbol
1770b57cec5SDimitry Andric // name. A byte offset in the range 1 to 3 (inclusive) points into the length
1780b57cec5SDimitry Andric // field; as a soft-error recovery mechanism, we treat such cases as having an
1790b57cec5SDimitry Andric // offset of 0.
1800b57cec5SDimitry Andric if (Offset < 4)
1810b57cec5SDimitry Andric return StringRef(nullptr, 0);
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric if (StringTable.Data != nullptr && StringTable.Size > Offset)
1840b57cec5SDimitry Andric return (StringTable.Data + Offset);
1850b57cec5SDimitry Andric
1868bcb0991SDimitry Andric return make_error<GenericBinaryError>("Bad offset for string table entry",
1870b57cec5SDimitry Andric object_error::parse_failed);
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric
getStringTable() const190*5f7ddb14SDimitry Andric StringRef XCOFFObjectFile::getStringTable() const {
191*5f7ddb14SDimitry Andric return StringRef(StringTable.Data, StringTable.Size);
192*5f7ddb14SDimitry Andric }
193*5f7ddb14SDimitry Andric
1948bcb0991SDimitry Andric Expected<StringRef>
getCFileName(const XCOFFFileAuxEnt * CFileEntPtr) const1958bcb0991SDimitry Andric XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const {
196*5f7ddb14SDimitry Andric if (CFileEntPtr->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
1978bcb0991SDimitry Andric return generateXCOFFFixedNameStringRef(CFileEntPtr->Name);
1988bcb0991SDimitry Andric return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset);
1998bcb0991SDimitry Andric }
2008bcb0991SDimitry Andric
getSymbolName(DataRefImpl Symb) const2018bcb0991SDimitry Andric Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const {
202*5f7ddb14SDimitry Andric return toSymbolRef(Symb).getName();
2038bcb0991SDimitry Andric }
2048bcb0991SDimitry Andric
getSymbolAddress(DataRefImpl Symb) const2050b57cec5SDimitry Andric Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
206*5f7ddb14SDimitry Andric return toSymbolRef(Symb).getValue();
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric
getSymbolValueImpl(DataRefImpl Symb) const2090b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
210*5f7ddb14SDimitry Andric return toSymbolRef(Symb).getValue();
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric
getCommonSymbolSizeImpl(DataRefImpl Symb) const2130b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
2140b57cec5SDimitry Andric uint64_t Result = 0;
2150b57cec5SDimitry Andric llvm_unreachable("Not yet implemented!");
2160b57cec5SDimitry Andric return Result;
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric Expected<SymbolRef::Type>
getSymbolType(DataRefImpl Symb) const2200b57cec5SDimitry Andric XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const {
221*5f7ddb14SDimitry Andric // TODO: Return the correct symbol type.
2220b57cec5SDimitry Andric return SymbolRef::ST_Other;
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric Expected<section_iterator>
getSymbolSection(DataRefImpl Symb) const2260b57cec5SDimitry Andric XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const {
227*5f7ddb14SDimitry Andric const int16_t SectNum = toSymbolRef(Symb).getSectionNumber();
2280b57cec5SDimitry Andric
2290b57cec5SDimitry Andric if (isReservedSectionNumber(SectNum))
2300b57cec5SDimitry Andric return section_end();
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum);
2330b57cec5SDimitry Andric if (!ExpSec)
2340b57cec5SDimitry Andric return ExpSec.takeError();
2350b57cec5SDimitry Andric
2360b57cec5SDimitry Andric return section_iterator(SectionRef(ExpSec.get(), this));
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric
moveSectionNext(DataRefImpl & Sec) const2390b57cec5SDimitry Andric void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const {
2400b57cec5SDimitry Andric const char *Ptr = reinterpret_cast<const char *>(Sec.p);
2410b57cec5SDimitry Andric Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric
getSectionName(DataRefImpl Sec) const2440b57cec5SDimitry Andric Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
2458bcb0991SDimitry Andric return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec));
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric
getSectionAddress(DataRefImpl Sec) const2480b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
2490b57cec5SDimitry Andric // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
2500b57cec5SDimitry Andric // with MSVC.
2510b57cec5SDimitry Andric if (is64Bit())
2520b57cec5SDimitry Andric return toSection64(Sec)->VirtualAddress;
2530b57cec5SDimitry Andric
2540b57cec5SDimitry Andric return toSection32(Sec)->VirtualAddress;
2550b57cec5SDimitry Andric }
2560b57cec5SDimitry Andric
getSectionIndex(DataRefImpl Sec) const2570b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
2580b57cec5SDimitry Andric // Section numbers in XCOFF are numbered beginning at 1. A section number of
2590b57cec5SDimitry Andric // zero is used to indicate that a symbol is being imported or is undefined.
2600b57cec5SDimitry Andric if (is64Bit())
2610b57cec5SDimitry Andric return toSection64(Sec) - sectionHeaderTable64() + 1;
2620b57cec5SDimitry Andric else
2630b57cec5SDimitry Andric return toSection32(Sec) - sectionHeaderTable32() + 1;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
getSectionSize(DataRefImpl Sec) const2660b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const {
2670b57cec5SDimitry Andric // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
2680b57cec5SDimitry Andric // with MSVC.
2690b57cec5SDimitry Andric if (is64Bit())
2700b57cec5SDimitry Andric return toSection64(Sec)->SectionSize;
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric return toSection32(Sec)->SectionSize;
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric
2750b57cec5SDimitry Andric Expected<ArrayRef<uint8_t>>
getSectionContents(DataRefImpl Sec) const2760b57cec5SDimitry Andric XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
277480093f4SDimitry Andric if (isSectionVirtual(Sec))
278480093f4SDimitry Andric return ArrayRef<uint8_t>();
279480093f4SDimitry Andric
280480093f4SDimitry Andric uint64_t OffsetToRaw;
281480093f4SDimitry Andric if (is64Bit())
282480093f4SDimitry Andric OffsetToRaw = toSection64(Sec)->FileOffsetToRawData;
283480093f4SDimitry Andric else
284480093f4SDimitry Andric OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
285480093f4SDimitry Andric
286480093f4SDimitry Andric const uint8_t * ContentStart = base() + OffsetToRaw;
287480093f4SDimitry Andric uint64_t SectionSize = getSectionSize(Sec);
288af732203SDimitry Andric if (checkOffset(Data, reinterpret_cast<uintptr_t>(ContentStart), SectionSize))
289480093f4SDimitry Andric return make_error<BinaryError>();
290480093f4SDimitry Andric
291480093f4SDimitry Andric return makeArrayRef(ContentStart,SectionSize);
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric
getSectionAlignment(DataRefImpl Sec) const2940b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
2950b57cec5SDimitry Andric uint64_t Result = 0;
2960b57cec5SDimitry Andric llvm_unreachable("Not yet implemented!");
2970b57cec5SDimitry Andric return Result;
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric
isSectionCompressed(DataRefImpl Sec) const3000b57cec5SDimitry Andric bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
301*5f7ddb14SDimitry Andric return false;
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric
isSectionText(DataRefImpl Sec) const3040b57cec5SDimitry Andric bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const {
3050b57cec5SDimitry Andric return getSectionFlags(Sec) & XCOFF::STYP_TEXT;
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric
isSectionData(DataRefImpl Sec) const3080b57cec5SDimitry Andric bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const {
3090b57cec5SDimitry Andric uint32_t Flags = getSectionFlags(Sec);
3100b57cec5SDimitry Andric return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA);
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric
isSectionBSS(DataRefImpl Sec) const3130b57cec5SDimitry Andric bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const {
3140b57cec5SDimitry Andric uint32_t Flags = getSectionFlags(Sec);
3150b57cec5SDimitry Andric return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS);
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric
isDebugSection(DataRefImpl Sec) const318*5f7ddb14SDimitry Andric bool XCOFFObjectFile::isDebugSection(DataRefImpl Sec) const {
319*5f7ddb14SDimitry Andric uint32_t Flags = getSectionFlags(Sec);
320*5f7ddb14SDimitry Andric return Flags & (XCOFF::STYP_DEBUG | XCOFF::STYP_DWARF);
321*5f7ddb14SDimitry Andric }
322*5f7ddb14SDimitry Andric
isSectionVirtual(DataRefImpl Sec) const3230b57cec5SDimitry Andric bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
324480093f4SDimitry Andric return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0
325480093f4SDimitry Andric : toSection32(Sec)->FileOffsetToRawData == 0;
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric
section_rel_begin(DataRefImpl Sec) const3280b57cec5SDimitry Andric relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
3295ffd83dbSDimitry Andric if (is64Bit())
3305ffd83dbSDimitry Andric report_fatal_error("64-bit support not implemented yet");
3315ffd83dbSDimitry Andric const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
3325ffd83dbSDimitry Andric auto RelocationsOrErr = relocations(*SectionEntPtr);
3335ffd83dbSDimitry Andric if (Error E = RelocationsOrErr.takeError())
3340b57cec5SDimitry Andric return relocation_iterator(RelocationRef());
3355ffd83dbSDimitry Andric DataRefImpl Ret;
3365ffd83dbSDimitry Andric Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
3375ffd83dbSDimitry Andric return relocation_iterator(RelocationRef(Ret, this));
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric
section_rel_end(DataRefImpl Sec) const3400b57cec5SDimitry Andric relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const {
3415ffd83dbSDimitry Andric if (is64Bit())
3425ffd83dbSDimitry Andric report_fatal_error("64-bit support not implemented yet");
3435ffd83dbSDimitry Andric const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
3445ffd83dbSDimitry Andric auto RelocationsOrErr = relocations(*SectionEntPtr);
3455ffd83dbSDimitry Andric if (Error E = RelocationsOrErr.takeError())
3460b57cec5SDimitry Andric return relocation_iterator(RelocationRef());
3475ffd83dbSDimitry Andric DataRefImpl Ret;
3485ffd83dbSDimitry Andric Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
3495ffd83dbSDimitry Andric return relocation_iterator(RelocationRef(Ret, this));
3500b57cec5SDimitry Andric }
3510b57cec5SDimitry Andric
moveRelocationNext(DataRefImpl & Rel) const3520b57cec5SDimitry Andric void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
3535ffd83dbSDimitry Andric Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1);
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
getRelocationOffset(DataRefImpl Rel) const3560b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
3575ffd83dbSDimitry Andric if (is64Bit())
3585ffd83dbSDimitry Andric report_fatal_error("64-bit support not implemented yet");
3595ffd83dbSDimitry Andric const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
3605ffd83dbSDimitry Andric const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32();
3615ffd83dbSDimitry Andric const uint32_t RelocAddress = Reloc->VirtualAddress;
3625ffd83dbSDimitry Andric const uint16_t NumberOfSections = getNumberOfSections();
3635ffd83dbSDimitry Andric for (uint16_t i = 0; i < NumberOfSections; ++i) {
3645ffd83dbSDimitry Andric // Find which section this relocation is belonging to, and get the
3655ffd83dbSDimitry Andric // relocation offset relative to the start of the section.
3665ffd83dbSDimitry Andric if (Sec32->VirtualAddress <= RelocAddress &&
3675ffd83dbSDimitry Andric RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) {
3685ffd83dbSDimitry Andric return RelocAddress - Sec32->VirtualAddress;
3695ffd83dbSDimitry Andric }
3705ffd83dbSDimitry Andric ++Sec32;
3715ffd83dbSDimitry Andric }
3725ffd83dbSDimitry Andric return InvalidRelocOffset;
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric
getRelocationSymbol(DataRefImpl Rel) const3750b57cec5SDimitry Andric symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
3765ffd83dbSDimitry Andric if (is64Bit())
3775ffd83dbSDimitry Andric report_fatal_error("64-bit support not implemented yet");
3785ffd83dbSDimitry Andric const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
3795ffd83dbSDimitry Andric const uint32_t Index = Reloc->SymbolIndex;
3805ffd83dbSDimitry Andric
3815ffd83dbSDimitry Andric if (Index >= getLogicalNumberOfSymbolTableEntries32())
3825ffd83dbSDimitry Andric return symbol_end();
3835ffd83dbSDimitry Andric
3845ffd83dbSDimitry Andric DataRefImpl SymDRI;
385*5f7ddb14SDimitry Andric SymDRI.p = getSymbolEntryAddressByIndex(Index);
3865ffd83dbSDimitry Andric return symbol_iterator(SymbolRef(SymDRI, this));
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric
getRelocationType(DataRefImpl Rel) const3890b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const {
3905ffd83dbSDimitry Andric if (is64Bit())
3915ffd83dbSDimitry Andric report_fatal_error("64-bit support not implemented yet");
3925ffd83dbSDimitry Andric return viewAs<XCOFFRelocation32>(Rel.p)->Type;
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric
getRelocationTypeName(DataRefImpl Rel,SmallVectorImpl<char> & Result) const3950b57cec5SDimitry Andric void XCOFFObjectFile::getRelocationTypeName(
3960b57cec5SDimitry Andric DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
3975ffd83dbSDimitry Andric if (is64Bit())
3985ffd83dbSDimitry Andric report_fatal_error("64-bit support not implemented yet");
3995ffd83dbSDimitry Andric const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
4005ffd83dbSDimitry Andric StringRef Res = XCOFF::getRelocationTypeString(Reloc->Type);
4015ffd83dbSDimitry Andric Result.append(Res.begin(), Res.end());
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric
getSymbolFlags(DataRefImpl Symb) const4045ffd83dbSDimitry Andric Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const {
4050b57cec5SDimitry Andric uint32_t Result = 0;
406*5f7ddb14SDimitry Andric // TODO: Return correct symbol flags.
4070b57cec5SDimitry Andric return Result;
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric
symbol_begin() const4100b57cec5SDimitry Andric basic_symbol_iterator XCOFFObjectFile::symbol_begin() const {
4110b57cec5SDimitry Andric DataRefImpl SymDRI;
4120b57cec5SDimitry Andric SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr);
4130b57cec5SDimitry Andric return basic_symbol_iterator(SymbolRef(SymDRI, this));
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric
symbol_end() const4160b57cec5SDimitry Andric basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
4170b57cec5SDimitry Andric DataRefImpl SymDRI;
418*5f7ddb14SDimitry Andric const uint32_t NumberOfSymbolTableEntries = getNumberOfSymbolTableEntries();
419*5f7ddb14SDimitry Andric SymDRI.p = getSymbolEntryAddressByIndex(NumberOfSymbolTableEntries);
4200b57cec5SDimitry Andric return basic_symbol_iterator(SymbolRef(SymDRI, this));
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
section_begin() const4230b57cec5SDimitry Andric section_iterator XCOFFObjectFile::section_begin() const {
4240b57cec5SDimitry Andric DataRefImpl DRI;
4250b57cec5SDimitry Andric DRI.p = getSectionHeaderTableAddress();
4260b57cec5SDimitry Andric return section_iterator(SectionRef(DRI, this));
4270b57cec5SDimitry Andric }
4280b57cec5SDimitry Andric
section_end() const4290b57cec5SDimitry Andric section_iterator XCOFFObjectFile::section_end() const {
4300b57cec5SDimitry Andric DataRefImpl DRI;
4310b57cec5SDimitry Andric DRI.p = getWithOffset(getSectionHeaderTableAddress(),
4320b57cec5SDimitry Andric getNumberOfSections() * getSectionHeaderSize());
4330b57cec5SDimitry Andric return section_iterator(SectionRef(DRI, this));
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric
getBytesInAddress() const4360b57cec5SDimitry Andric uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; }
4370b57cec5SDimitry Andric
getFileFormatName() const4380b57cec5SDimitry Andric StringRef XCOFFObjectFile::getFileFormatName() const {
4390b57cec5SDimitry Andric return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000";
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric
getArch() const4420b57cec5SDimitry Andric Triple::ArchType XCOFFObjectFile::getArch() const {
4430b57cec5SDimitry Andric return is64Bit() ? Triple::ppc64 : Triple::ppc;
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric
getFeatures() const4460b57cec5SDimitry Andric SubtargetFeatures XCOFFObjectFile::getFeatures() const {
4470b57cec5SDimitry Andric return SubtargetFeatures();
4480b57cec5SDimitry Andric }
4490b57cec5SDimitry Andric
isRelocatableObject() const4500b57cec5SDimitry Andric bool XCOFFObjectFile::isRelocatableObject() const {
4515ffd83dbSDimitry Andric if (is64Bit())
452*5f7ddb14SDimitry Andric return !(fileHeader64()->Flags & NoRelMask);
4535ffd83dbSDimitry Andric return !(fileHeader32()->Flags & NoRelMask);
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric
getStartAddress() const4560b57cec5SDimitry Andric Expected<uint64_t> XCOFFObjectFile::getStartAddress() const {
4570b57cec5SDimitry Andric // TODO FIXME Should get from auxiliary_header->o_entry when support for the
4580b57cec5SDimitry Andric // auxiliary_header is added.
4590b57cec5SDimitry Andric return 0;
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric
mapDebugSectionName(StringRef Name) const462*5f7ddb14SDimitry Andric StringRef XCOFFObjectFile::mapDebugSectionName(StringRef Name) const {
463*5f7ddb14SDimitry Andric return StringSwitch<StringRef>(Name)
464*5f7ddb14SDimitry Andric .Case("dwinfo", "debug_info")
465*5f7ddb14SDimitry Andric .Case("dwline", "debug_line")
466*5f7ddb14SDimitry Andric .Case("dwpbnms", "debug_pubnames")
467*5f7ddb14SDimitry Andric .Case("dwpbtyp", "debug_pubtypes")
468*5f7ddb14SDimitry Andric .Case("dwarnge", "debug_aranges")
469*5f7ddb14SDimitry Andric .Case("dwabrev", "debug_abbrev")
470*5f7ddb14SDimitry Andric .Case("dwstr", "debug_str")
471*5f7ddb14SDimitry Andric .Case("dwrnges", "debug_ranges")
472*5f7ddb14SDimitry Andric .Case("dwloc", "debug_loc")
473*5f7ddb14SDimitry Andric .Case("dwframe", "debug_frame")
474*5f7ddb14SDimitry Andric .Case("dwmac", "debug_macinfo")
475*5f7ddb14SDimitry Andric .Default(Name);
476*5f7ddb14SDimitry Andric }
477*5f7ddb14SDimitry Andric
getFileHeaderSize() const4780b57cec5SDimitry Andric size_t XCOFFObjectFile::getFileHeaderSize() const {
4790b57cec5SDimitry Andric return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32);
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric
getSectionHeaderSize() const4820b57cec5SDimitry Andric size_t XCOFFObjectFile::getSectionHeaderSize() const {
4830b57cec5SDimitry Andric return is64Bit() ? sizeof(XCOFFSectionHeader64) :
4840b57cec5SDimitry Andric sizeof(XCOFFSectionHeader32);
4850b57cec5SDimitry Andric }
4860b57cec5SDimitry Andric
is64Bit() const4870b57cec5SDimitry Andric bool XCOFFObjectFile::is64Bit() const {
4880b57cec5SDimitry Andric return Binary::ID_XCOFF64 == getType();
4890b57cec5SDimitry Andric }
4900b57cec5SDimitry Andric
getMagic() const4910b57cec5SDimitry Andric uint16_t XCOFFObjectFile::getMagic() const {
4920b57cec5SDimitry Andric return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic;
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric
getSectionByNum(int16_t Num) const4950b57cec5SDimitry Andric Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const {
4960b57cec5SDimitry Andric if (Num <= 0 || Num > getNumberOfSections())
4970b57cec5SDimitry Andric return errorCodeToError(object_error::invalid_section_index);
4980b57cec5SDimitry Andric
4990b57cec5SDimitry Andric DataRefImpl DRI;
5000b57cec5SDimitry Andric DRI.p = getWithOffset(getSectionHeaderTableAddress(),
5010b57cec5SDimitry Andric getSectionHeaderSize() * (Num - 1));
5020b57cec5SDimitry Andric return DRI;
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric
5050b57cec5SDimitry Andric Expected<StringRef>
getSymbolSectionName(XCOFFSymbolRef SymEntPtr) const506*5f7ddb14SDimitry Andric XCOFFObjectFile::getSymbolSectionName(XCOFFSymbolRef SymEntPtr) const {
507*5f7ddb14SDimitry Andric const int16_t SectionNum = SymEntPtr.getSectionNumber();
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric switch (SectionNum) {
5100b57cec5SDimitry Andric case XCOFF::N_DEBUG:
5110b57cec5SDimitry Andric return "N_DEBUG";
5120b57cec5SDimitry Andric case XCOFF::N_ABS:
5130b57cec5SDimitry Andric return "N_ABS";
5140b57cec5SDimitry Andric case XCOFF::N_UNDEF:
5150b57cec5SDimitry Andric return "N_UNDEF";
5160b57cec5SDimitry Andric default:
5170b57cec5SDimitry Andric Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum);
5180b57cec5SDimitry Andric if (SecRef)
5198bcb0991SDimitry Andric return generateXCOFFFixedNameStringRef(
5208bcb0991SDimitry Andric getSectionNameInternal(SecRef.get()));
5210b57cec5SDimitry Andric return SecRef.takeError();
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric
getSymbolSectionID(SymbolRef Sym) const525*5f7ddb14SDimitry Andric unsigned XCOFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
526*5f7ddb14SDimitry Andric XCOFFSymbolRef XCOFFSymRef(Sym.getRawDataRefImpl(), this);
527*5f7ddb14SDimitry Andric return XCOFFSymRef.getSectionNumber();
528*5f7ddb14SDimitry Andric }
529*5f7ddb14SDimitry Andric
isReservedSectionNumber(int16_t SectionNumber)5300b57cec5SDimitry Andric bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
5310b57cec5SDimitry Andric return (SectionNumber <= 0 && SectionNumber >= -2);
5320b57cec5SDimitry Andric }
5330b57cec5SDimitry Andric
getNumberOfSections() const5340b57cec5SDimitry Andric uint16_t XCOFFObjectFile::getNumberOfSections() const {
5350b57cec5SDimitry Andric return is64Bit() ? fileHeader64()->NumberOfSections
5360b57cec5SDimitry Andric : fileHeader32()->NumberOfSections;
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric
getTimeStamp() const5390b57cec5SDimitry Andric int32_t XCOFFObjectFile::getTimeStamp() const {
5400b57cec5SDimitry Andric return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp;
5410b57cec5SDimitry Andric }
5420b57cec5SDimitry Andric
getOptionalHeaderSize() const5430b57cec5SDimitry Andric uint16_t XCOFFObjectFile::getOptionalHeaderSize() const {
5440b57cec5SDimitry Andric return is64Bit() ? fileHeader64()->AuxHeaderSize
5450b57cec5SDimitry Andric : fileHeader32()->AuxHeaderSize;
5460b57cec5SDimitry Andric }
5470b57cec5SDimitry Andric
getSymbolTableOffset32() const5480b57cec5SDimitry Andric uint32_t XCOFFObjectFile::getSymbolTableOffset32() const {
5490b57cec5SDimitry Andric return fileHeader32()->SymbolTableOffset;
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric
getRawNumberOfSymbolTableEntries32() const5520b57cec5SDimitry Andric int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const {
5530b57cec5SDimitry Andric // As far as symbol table size is concerned, if this field is negative it is
5540b57cec5SDimitry Andric // to be treated as a 0. However since this field is also used for printing we
5550b57cec5SDimitry Andric // don't want to truncate any negative values.
5560b57cec5SDimitry Andric return fileHeader32()->NumberOfSymTableEntries;
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric
getLogicalNumberOfSymbolTableEntries32() const5590b57cec5SDimitry Andric uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const {
5600b57cec5SDimitry Andric return (fileHeader32()->NumberOfSymTableEntries >= 0
5610b57cec5SDimitry Andric ? fileHeader32()->NumberOfSymTableEntries
5620b57cec5SDimitry Andric : 0);
5630b57cec5SDimitry Andric }
5640b57cec5SDimitry Andric
getSymbolTableOffset64() const5650b57cec5SDimitry Andric uint64_t XCOFFObjectFile::getSymbolTableOffset64() const {
5660b57cec5SDimitry Andric return fileHeader64()->SymbolTableOffset;
5670b57cec5SDimitry Andric }
5680b57cec5SDimitry Andric
getNumberOfSymbolTableEntries64() const5690b57cec5SDimitry Andric uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const {
5700b57cec5SDimitry Andric return fileHeader64()->NumberOfSymTableEntries;
5710b57cec5SDimitry Andric }
5720b57cec5SDimitry Andric
getNumberOfSymbolTableEntries() const573*5f7ddb14SDimitry Andric uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries() const {
574*5f7ddb14SDimitry Andric return is64Bit() ? getNumberOfSymbolTableEntries64()
5758bcb0991SDimitry Andric : getLogicalNumberOfSymbolTableEntries32();
576*5f7ddb14SDimitry Andric }
577*5f7ddb14SDimitry Andric
getEndOfSymbolTableAddress() const578*5f7ddb14SDimitry Andric uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const {
579*5f7ddb14SDimitry Andric const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries();
5808bcb0991SDimitry Andric return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr),
5818bcb0991SDimitry Andric XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries);
5828bcb0991SDimitry Andric }
5838bcb0991SDimitry Andric
checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const5848bcb0991SDimitry Andric void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const {
5858bcb0991SDimitry Andric if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr))
5868bcb0991SDimitry Andric report_fatal_error("Symbol table entry is outside of symbol table.");
5878bcb0991SDimitry Andric
5888bcb0991SDimitry Andric if (SymbolEntPtr >= getEndOfSymbolTableAddress())
5898bcb0991SDimitry Andric report_fatal_error("Symbol table entry is outside of symbol table.");
5908bcb0991SDimitry Andric
5918bcb0991SDimitry Andric ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) -
5928bcb0991SDimitry Andric reinterpret_cast<const char *>(SymbolTblPtr);
5938bcb0991SDimitry Andric
5948bcb0991SDimitry Andric if (Offset % XCOFF::SymbolTableEntrySize != 0)
5958bcb0991SDimitry Andric report_fatal_error(
5968bcb0991SDimitry Andric "Symbol table entry position is not valid inside of symbol table.");
5978bcb0991SDimitry Andric }
5988bcb0991SDimitry Andric
getSymbolIndex(uintptr_t SymbolEntPtr) const5998bcb0991SDimitry Andric uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const {
6008bcb0991SDimitry Andric return (reinterpret_cast<const char *>(SymbolEntPtr) -
6018bcb0991SDimitry Andric reinterpret_cast<const char *>(SymbolTblPtr)) /
6028bcb0991SDimitry Andric XCOFF::SymbolTableEntrySize;
6038bcb0991SDimitry Andric }
6048bcb0991SDimitry Andric
getSymbolEntryAddressByIndex(uint32_t Index) const605*5f7ddb14SDimitry Andric uintptr_t XCOFFObjectFile::getSymbolEntryAddressByIndex(uint32_t Index) const {
606*5f7ddb14SDimitry Andric return getAdvancedSymbolEntryAddress(
607*5f7ddb14SDimitry Andric reinterpret_cast<uintptr_t>(getPointerToSymbolTable()), Index);
608*5f7ddb14SDimitry Andric }
609*5f7ddb14SDimitry Andric
6108bcb0991SDimitry Andric Expected<StringRef>
getSymbolNameByIndex(uint32_t Index) const6118bcb0991SDimitry Andric XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const {
612*5f7ddb14SDimitry Andric const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries();
6138bcb0991SDimitry Andric
614*5f7ddb14SDimitry Andric if (Index >= NumberOfSymTableEntries)
6158bcb0991SDimitry Andric return errorCodeToError(object_error::invalid_symbol_index);
6168bcb0991SDimitry Andric
6178bcb0991SDimitry Andric DataRefImpl SymDRI;
618*5f7ddb14SDimitry Andric SymDRI.p = getSymbolEntryAddressByIndex(Index);
6198bcb0991SDimitry Andric return getSymbolName(SymDRI);
6208bcb0991SDimitry Andric }
6218bcb0991SDimitry Andric
getFlags() const6220b57cec5SDimitry Andric uint16_t XCOFFObjectFile::getFlags() const {
6230b57cec5SDimitry Andric return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags;
6240b57cec5SDimitry Andric }
6250b57cec5SDimitry Andric
getSectionNameInternal(DataRefImpl Sec) const6260b57cec5SDimitry Andric const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const {
6270b57cec5SDimitry Andric return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name;
6280b57cec5SDimitry Andric }
6290b57cec5SDimitry Andric
getSectionHeaderTableAddress() const6300b57cec5SDimitry Andric uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const {
6310b57cec5SDimitry Andric return reinterpret_cast<uintptr_t>(SectionHeaderTable);
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric
getSectionFlags(DataRefImpl Sec) const6340b57cec5SDimitry Andric int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const {
6350b57cec5SDimitry Andric return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags;
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric
XCOFFObjectFile(unsigned int Type,MemoryBufferRef Object)6380b57cec5SDimitry Andric XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object)
6390b57cec5SDimitry Andric : ObjectFile(Type, Object) {
6400b57cec5SDimitry Andric assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64);
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric
sections64() const6430b57cec5SDimitry Andric ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const {
6440b57cec5SDimitry Andric assert(is64Bit() && "64-bit interface called for non 64-bit file.");
6450b57cec5SDimitry Andric const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64();
6460b57cec5SDimitry Andric return ArrayRef<XCOFFSectionHeader64>(TablePtr,
6470b57cec5SDimitry Andric TablePtr + getNumberOfSections());
6480b57cec5SDimitry Andric }
6490b57cec5SDimitry Andric
sections32() const6500b57cec5SDimitry Andric ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const {
6510b57cec5SDimitry Andric assert(!is64Bit() && "32-bit interface called for non 32-bit file.");
6520b57cec5SDimitry Andric const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32();
6530b57cec5SDimitry Andric return ArrayRef<XCOFFSectionHeader32>(TablePtr,
6540b57cec5SDimitry Andric TablePtr + getNumberOfSections());
6550b57cec5SDimitry Andric }
6560b57cec5SDimitry Andric
6578bcb0991SDimitry Andric // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO
6588bcb0991SDimitry Andric // section header contains the actual count of relocation entries in the s_paddr
6598bcb0991SDimitry Andric // field. STYP_OVRFLO headers contain the section index of their corresponding
6608bcb0991SDimitry Andric // sections as their raw "NumberOfRelocations" field value.
getLogicalNumberOfRelocationEntries(const XCOFFSectionHeader32 & Sec) const6618bcb0991SDimitry Andric Expected<uint32_t> XCOFFObjectFile::getLogicalNumberOfRelocationEntries(
6628bcb0991SDimitry Andric const XCOFFSectionHeader32 &Sec) const {
6638bcb0991SDimitry Andric
6648bcb0991SDimitry Andric uint16_t SectionIndex = &Sec - sectionHeaderTable32() + 1;
6658bcb0991SDimitry Andric
6665ffd83dbSDimitry Andric if (Sec.NumberOfRelocations < XCOFF::RelocOverflow)
6678bcb0991SDimitry Andric return Sec.NumberOfRelocations;
6688bcb0991SDimitry Andric for (const auto &Sec : sections32()) {
6698bcb0991SDimitry Andric if (Sec.Flags == XCOFF::STYP_OVRFLO &&
6708bcb0991SDimitry Andric Sec.NumberOfRelocations == SectionIndex)
6718bcb0991SDimitry Andric return Sec.PhysicalAddress;
6728bcb0991SDimitry Andric }
6738bcb0991SDimitry Andric return errorCodeToError(object_error::parse_failed);
6748bcb0991SDimitry Andric }
6758bcb0991SDimitry Andric
6768bcb0991SDimitry Andric Expected<ArrayRef<XCOFFRelocation32>>
relocations(const XCOFFSectionHeader32 & Sec) const6778bcb0991SDimitry Andric XCOFFObjectFile::relocations(const XCOFFSectionHeader32 &Sec) const {
6788bcb0991SDimitry Andric uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader),
6798bcb0991SDimitry Andric Sec.FileOffsetToRelocationInfo);
6808bcb0991SDimitry Andric auto NumRelocEntriesOrErr = getLogicalNumberOfRelocationEntries(Sec);
6818bcb0991SDimitry Andric if (Error E = NumRelocEntriesOrErr.takeError())
6828bcb0991SDimitry Andric return std::move(E);
6838bcb0991SDimitry Andric
6848bcb0991SDimitry Andric uint32_t NumRelocEntries = NumRelocEntriesOrErr.get();
6858bcb0991SDimitry Andric
686af732203SDimitry Andric static_assert(
687af732203SDimitry Andric sizeof(XCOFFRelocation32) == XCOFF::RelocationSerializationSize32, "");
6888bcb0991SDimitry Andric auto RelocationOrErr =
6898bcb0991SDimitry Andric getObject<XCOFFRelocation32>(Data, reinterpret_cast<void *>(RelocAddr),
6908bcb0991SDimitry Andric NumRelocEntries * sizeof(XCOFFRelocation32));
6918bcb0991SDimitry Andric if (Error E = RelocationOrErr.takeError())
6928bcb0991SDimitry Andric return std::move(E);
6938bcb0991SDimitry Andric
6948bcb0991SDimitry Andric const XCOFFRelocation32 *StartReloc = RelocationOrErr.get();
6958bcb0991SDimitry Andric
6968bcb0991SDimitry Andric return ArrayRef<XCOFFRelocation32>(StartReloc, StartReloc + NumRelocEntries);
6978bcb0991SDimitry Andric }
6988bcb0991SDimitry Andric
6990b57cec5SDimitry Andric Expected<XCOFFStringTable>
parseStringTable(const XCOFFObjectFile * Obj,uint64_t Offset)7000b57cec5SDimitry Andric XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) {
7010b57cec5SDimitry Andric // If there is a string table, then the buffer must contain at least 4 bytes
7020b57cec5SDimitry Andric // for the string table's size. Not having a string table is not an error.
7035ffd83dbSDimitry Andric if (Error E = Binary::checkOffset(
7045ffd83dbSDimitry Andric Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) {
7055ffd83dbSDimitry Andric consumeError(std::move(E));
7060b57cec5SDimitry Andric return XCOFFStringTable{0, nullptr};
7075ffd83dbSDimitry Andric }
7080b57cec5SDimitry Andric
7090b57cec5SDimitry Andric // Read the size out of the buffer.
7100b57cec5SDimitry Andric uint32_t Size = support::endian::read32be(Obj->base() + Offset);
7110b57cec5SDimitry Andric
7120b57cec5SDimitry Andric // If the size is less then 4, then the string table is just a size and no
7130b57cec5SDimitry Andric // string data.
7140b57cec5SDimitry Andric if (Size <= 4)
7150b57cec5SDimitry Andric return XCOFFStringTable{4, nullptr};
7160b57cec5SDimitry Andric
7170b57cec5SDimitry Andric auto StringTableOrErr =
7180b57cec5SDimitry Andric getObject<char>(Obj->Data, Obj->base() + Offset, Size);
7190b57cec5SDimitry Andric if (Error E = StringTableOrErr.takeError())
7200b57cec5SDimitry Andric return std::move(E);
7210b57cec5SDimitry Andric
7220b57cec5SDimitry Andric const char *StringTablePtr = StringTableOrErr.get();
7230b57cec5SDimitry Andric if (StringTablePtr[Size - 1] != '\0')
7240b57cec5SDimitry Andric return errorCodeToError(object_error::string_table_non_null_end);
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric return XCOFFStringTable{Size, StringTablePtr};
7270b57cec5SDimitry Andric }
7280b57cec5SDimitry Andric
7290b57cec5SDimitry Andric Expected<std::unique_ptr<XCOFFObjectFile>>
create(unsigned Type,MemoryBufferRef MBR)7300b57cec5SDimitry Andric XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) {
7318bcb0991SDimitry Andric // Can't use std::make_unique because of the private constructor.
7320b57cec5SDimitry Andric std::unique_ptr<XCOFFObjectFile> Obj;
7330b57cec5SDimitry Andric Obj.reset(new XCOFFObjectFile(Type, MBR));
7340b57cec5SDimitry Andric
7350b57cec5SDimitry Andric uint64_t CurOffset = 0;
7360b57cec5SDimitry Andric const auto *Base = Obj->base();
7370b57cec5SDimitry Andric MemoryBufferRef Data = Obj->Data;
7380b57cec5SDimitry Andric
7390b57cec5SDimitry Andric // Parse file header.
7400b57cec5SDimitry Andric auto FileHeaderOrErr =
7410b57cec5SDimitry Andric getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize());
7420b57cec5SDimitry Andric if (Error E = FileHeaderOrErr.takeError())
7430b57cec5SDimitry Andric return std::move(E);
7440b57cec5SDimitry Andric Obj->FileHeader = FileHeaderOrErr.get();
7450b57cec5SDimitry Andric
7460b57cec5SDimitry Andric CurOffset += Obj->getFileHeaderSize();
7470b57cec5SDimitry Andric // TODO FIXME we don't have support for an optional header yet, so just skip
7480b57cec5SDimitry Andric // past it.
7490b57cec5SDimitry Andric CurOffset += Obj->getOptionalHeaderSize();
7500b57cec5SDimitry Andric
7510b57cec5SDimitry Andric // Parse the section header table if it is present.
7520b57cec5SDimitry Andric if (Obj->getNumberOfSections()) {
7530b57cec5SDimitry Andric auto SecHeadersOrErr = getObject<void>(Data, Base + CurOffset,
7540b57cec5SDimitry Andric Obj->getNumberOfSections() *
7550b57cec5SDimitry Andric Obj->getSectionHeaderSize());
7560b57cec5SDimitry Andric if (Error E = SecHeadersOrErr.takeError())
7570b57cec5SDimitry Andric return std::move(E);
7580b57cec5SDimitry Andric Obj->SectionHeaderTable = SecHeadersOrErr.get();
7590b57cec5SDimitry Andric }
7600b57cec5SDimitry Andric
761*5f7ddb14SDimitry Andric const uint32_t NumberOfSymbolTableEntries =
762*5f7ddb14SDimitry Andric Obj->getNumberOfSymbolTableEntries();
7630b57cec5SDimitry Andric
7640b57cec5SDimitry Andric // If there is no symbol table we are done parsing the memory buffer.
765*5f7ddb14SDimitry Andric if (NumberOfSymbolTableEntries == 0)
7660b57cec5SDimitry Andric return std::move(Obj);
7670b57cec5SDimitry Andric
7680b57cec5SDimitry Andric // Parse symbol table.
769*5f7ddb14SDimitry Andric CurOffset = Obj->is64Bit() ? Obj->getSymbolTableOffset64()
770*5f7ddb14SDimitry Andric : Obj->getSymbolTableOffset32();
771*5f7ddb14SDimitry Andric const uint64_t SymbolTableSize =
772*5f7ddb14SDimitry Andric static_cast<uint64_t>(XCOFF::SymbolTableEntrySize) *
773*5f7ddb14SDimitry Andric NumberOfSymbolTableEntries;
7740b57cec5SDimitry Andric auto SymTableOrErr =
775*5f7ddb14SDimitry Andric getObject<void *>(Data, Base + CurOffset, SymbolTableSize);
7760b57cec5SDimitry Andric if (Error E = SymTableOrErr.takeError())
7770b57cec5SDimitry Andric return std::move(E);
7780b57cec5SDimitry Andric Obj->SymbolTblPtr = SymTableOrErr.get();
7790b57cec5SDimitry Andric CurOffset += SymbolTableSize;
7800b57cec5SDimitry Andric
7810b57cec5SDimitry Andric // Parse String table.
7820b57cec5SDimitry Andric Expected<XCOFFStringTable> StringTableOrErr =
7830b57cec5SDimitry Andric parseStringTable(Obj.get(), CurOffset);
7840b57cec5SDimitry Andric if (Error E = StringTableOrErr.takeError())
7850b57cec5SDimitry Andric return std::move(E);
7860b57cec5SDimitry Andric Obj->StringTable = StringTableOrErr.get();
7870b57cec5SDimitry Andric
7880b57cec5SDimitry Andric return std::move(Obj);
7890b57cec5SDimitry Andric }
7900b57cec5SDimitry Andric
7910b57cec5SDimitry Andric Expected<std::unique_ptr<ObjectFile>>
createXCOFFObjectFile(MemoryBufferRef MemBufRef,unsigned FileType)7920b57cec5SDimitry Andric ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef,
7930b57cec5SDimitry Andric unsigned FileType) {
7940b57cec5SDimitry Andric return XCOFFObjectFile::create(FileType, MemBufRef);
7950b57cec5SDimitry Andric }
7960b57cec5SDimitry Andric
isFunction() const797*5f7ddb14SDimitry Andric bool XCOFFSymbolRef::isFunction() const {
798*5f7ddb14SDimitry Andric if (!isCsectSymbol())
799*5f7ddb14SDimitry Andric return false;
800*5f7ddb14SDimitry Andric
801*5f7ddb14SDimitry Andric if (getSymbolType() & FunctionSym)
802*5f7ddb14SDimitry Andric return true;
803*5f7ddb14SDimitry Andric
804*5f7ddb14SDimitry Andric Expected<XCOFFCsectAuxRef> ExpCsectAuxEnt = getXCOFFCsectAuxRef();
805*5f7ddb14SDimitry Andric if (!ExpCsectAuxEnt)
806*5f7ddb14SDimitry Andric return false;
807*5f7ddb14SDimitry Andric
808*5f7ddb14SDimitry Andric const XCOFFCsectAuxRef CsectAuxRef = ExpCsectAuxEnt.get();
809*5f7ddb14SDimitry Andric
810*5f7ddb14SDimitry Andric // A function definition should be a label definition.
811*5f7ddb14SDimitry Andric // FIXME: This is not necessarily the case when -ffunction-sections is
812*5f7ddb14SDimitry Andric // enabled.
813*5f7ddb14SDimitry Andric if (!CsectAuxRef.isLabel())
814*5f7ddb14SDimitry Andric return false;
815*5f7ddb14SDimitry Andric
816*5f7ddb14SDimitry Andric if (CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_PR)
817*5f7ddb14SDimitry Andric return false;
818*5f7ddb14SDimitry Andric
819*5f7ddb14SDimitry Andric const int16_t SectNum = getSectionNumber();
820*5f7ddb14SDimitry Andric Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum);
821*5f7ddb14SDimitry Andric if (!SI) {
822*5f7ddb14SDimitry Andric // If we could not get the section, then this symbol should not be
823*5f7ddb14SDimitry Andric // a function. So consume the error and return `false` to move on.
824*5f7ddb14SDimitry Andric consumeError(SI.takeError());
825*5f7ddb14SDimitry Andric return false;
8268bcb0991SDimitry Andric }
8278bcb0991SDimitry Andric
828*5f7ddb14SDimitry Andric return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT);
8298bcb0991SDimitry Andric }
8308bcb0991SDimitry Andric
isCsectSymbol() const831*5f7ddb14SDimitry Andric bool XCOFFSymbolRef::isCsectSymbol() const {
8328bcb0991SDimitry Andric XCOFF::StorageClass SC = getStorageClass();
8338bcb0991SDimitry Andric return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT ||
8348bcb0991SDimitry Andric SC == XCOFF::C_HIDEXT);
8358bcb0991SDimitry Andric }
8368bcb0991SDimitry Andric
getXCOFFCsectAuxRef() const837*5f7ddb14SDimitry Andric Expected<XCOFFCsectAuxRef> XCOFFSymbolRef::getXCOFFCsectAuxRef() const {
838*5f7ddb14SDimitry Andric assert(isCsectSymbol() &&
839*5f7ddb14SDimitry Andric "Calling csect symbol interface with a non-csect symbol.");
8408bcb0991SDimitry Andric
841*5f7ddb14SDimitry Andric uint8_t NumberOfAuxEntries = getNumberOfAuxEntries();
8428bcb0991SDimitry Andric
843*5f7ddb14SDimitry Andric Expected<StringRef> NameOrErr = getName();
844*5f7ddb14SDimitry Andric if (auto Err = NameOrErr.takeError())
845*5f7ddb14SDimitry Andric return std::move(Err);
8468bcb0991SDimitry Andric
847*5f7ddb14SDimitry Andric if (!NumberOfAuxEntries) {
848*5f7ddb14SDimitry Andric return createStringError(object_error::parse_failed,
849*5f7ddb14SDimitry Andric "csect symbol \"" + *NameOrErr +
850*5f7ddb14SDimitry Andric "\" contains no auxiliary entry");
851*5f7ddb14SDimitry Andric }
8528bcb0991SDimitry Andric
853*5f7ddb14SDimitry Andric if (!OwningObjectPtr->is64Bit()) {
854*5f7ddb14SDimitry Andric // In XCOFF32, the csect auxilliary entry is always the last auxiliary
855*5f7ddb14SDimitry Andric // entry for the symbol.
856*5f7ddb14SDimitry Andric uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
857*5f7ddb14SDimitry Andric getEntryAddress(), NumberOfAuxEntries);
858*5f7ddb14SDimitry Andric return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt32>(AuxAddr));
859*5f7ddb14SDimitry Andric }
8608bcb0991SDimitry Andric
861*5f7ddb14SDimitry Andric // XCOFF64 uses SymbolAuxType to identify the auxiliary entry type.
862*5f7ddb14SDimitry Andric // We need to iterate through all the auxiliary entries to find it.
863*5f7ddb14SDimitry Andric for (uint8_t Index = NumberOfAuxEntries; Index > 0; --Index) {
864*5f7ddb14SDimitry Andric uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
865*5f7ddb14SDimitry Andric getEntryAddress(), Index);
866*5f7ddb14SDimitry Andric if (*OwningObjectPtr->getSymbolAuxType(AuxAddr) ==
867*5f7ddb14SDimitry Andric XCOFF::SymbolAuxType::AUX_CSECT) {
868*5f7ddb14SDimitry Andric #ifndef NDEBUG
869*5f7ddb14SDimitry Andric OwningObjectPtr->checkSymbolEntryPointer(AuxAddr);
870*5f7ddb14SDimitry Andric #endif
871*5f7ddb14SDimitry Andric return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt64>(AuxAddr));
872*5f7ddb14SDimitry Andric }
873*5f7ddb14SDimitry Andric }
8748bcb0991SDimitry Andric
875*5f7ddb14SDimitry Andric return createStringError(
876*5f7ddb14SDimitry Andric object_error::parse_failed,
877*5f7ddb14SDimitry Andric "a csect auxiliary entry is not found for symbol \"" + *NameOrErr + "\"");
878*5f7ddb14SDimitry Andric }
8798bcb0991SDimitry Andric
getName() const880*5f7ddb14SDimitry Andric Expected<StringRef> XCOFFSymbolRef::getName() const {
881*5f7ddb14SDimitry Andric // A storage class value with the high-order bit on indicates that the name is
882*5f7ddb14SDimitry Andric // a symbolic debugger stabstring.
883*5f7ddb14SDimitry Andric if (getStorageClass() & 0x80)
884*5f7ddb14SDimitry Andric return StringRef("Unimplemented Debug Name");
885*5f7ddb14SDimitry Andric
886*5f7ddb14SDimitry Andric if (Entry32) {
887*5f7ddb14SDimitry Andric if (Entry32->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
888*5f7ddb14SDimitry Andric return generateXCOFFFixedNameStringRef(Entry32->SymbolName);
889*5f7ddb14SDimitry Andric
890*5f7ddb14SDimitry Andric return OwningObjectPtr->getStringTableEntry(Entry32->NameInStrTbl.Offset);
891*5f7ddb14SDimitry Andric }
892*5f7ddb14SDimitry Andric
893*5f7ddb14SDimitry Andric return OwningObjectPtr->getStringTableEntry(Entry64->Offset);
8940b57cec5SDimitry Andric }
8950b57cec5SDimitry Andric
896480093f4SDimitry Andric // Explictly instantiate template classes.
897480093f4SDimitry Andric template struct XCOFFSectionHeader<XCOFFSectionHeader32>;
898480093f4SDimitry Andric template struct XCOFFSectionHeader<XCOFFSectionHeader64>;
899480093f4SDimitry Andric
doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes)900af732203SDimitry Andric bool doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes) {
901af732203SDimitry Andric if (Bytes.size() < 4)
902af732203SDimitry Andric return false;
903af732203SDimitry Andric
904af732203SDimitry Andric return support::endian::read32be(Bytes.data()) == 0;
905af732203SDimitry Andric }
906af732203SDimitry Andric
907af732203SDimitry Andric #define GETVALUEWITHMASK(X) (Data & (TracebackTable::X))
908af732203SDimitry Andric #define GETVALUEWITHMASKSHIFT(X, S) \
909af732203SDimitry Andric ((Data & (TracebackTable::X)) >> (TracebackTable::S))
910*5f7ddb14SDimitry Andric
create(StringRef TBvectorStrRef)911*5f7ddb14SDimitry Andric Expected<TBVectorExt> TBVectorExt::create(StringRef TBvectorStrRef) {
912*5f7ddb14SDimitry Andric Error Err = Error::success();
913*5f7ddb14SDimitry Andric TBVectorExt TBTVecExt(TBvectorStrRef, Err);
914*5f7ddb14SDimitry Andric if (Err)
915*5f7ddb14SDimitry Andric return std::move(Err);
916*5f7ddb14SDimitry Andric return TBTVecExt;
917*5f7ddb14SDimitry Andric }
918*5f7ddb14SDimitry Andric
TBVectorExt(StringRef TBvectorStrRef,Error & Err)919*5f7ddb14SDimitry Andric TBVectorExt::TBVectorExt(StringRef TBvectorStrRef, Error &Err) {
920*5f7ddb14SDimitry Andric const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(TBvectorStrRef.data());
921*5f7ddb14SDimitry Andric Data = support::endian::read16be(Ptr);
922*5f7ddb14SDimitry Andric uint32_t VecParmsTypeValue = support::endian::read32be(Ptr + 2);
923*5f7ddb14SDimitry Andric unsigned ParmsNum =
924*5f7ddb14SDimitry Andric GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask, NumberOfVectorParmsShift);
925*5f7ddb14SDimitry Andric
926*5f7ddb14SDimitry Andric ErrorAsOutParameter EAO(&Err);
927*5f7ddb14SDimitry Andric Expected<SmallString<32>> VecParmsTypeOrError =
928*5f7ddb14SDimitry Andric parseVectorParmsType(VecParmsTypeValue, ParmsNum);
929*5f7ddb14SDimitry Andric if (!VecParmsTypeOrError)
930*5f7ddb14SDimitry Andric Err = VecParmsTypeOrError.takeError();
931*5f7ddb14SDimitry Andric else
932*5f7ddb14SDimitry Andric VecParmsInfo = VecParmsTypeOrError.get();
933*5f7ddb14SDimitry Andric }
934*5f7ddb14SDimitry Andric
getNumberOfVRSaved() const935af732203SDimitry Andric uint8_t TBVectorExt::getNumberOfVRSaved() const {
936af732203SDimitry Andric return GETVALUEWITHMASKSHIFT(NumberOfVRSavedMask, NumberOfVRSavedShift);
937af732203SDimitry Andric }
938af732203SDimitry Andric
isVRSavedOnStack() const939af732203SDimitry Andric bool TBVectorExt::isVRSavedOnStack() const {
940af732203SDimitry Andric return GETVALUEWITHMASK(IsVRSavedOnStackMask);
941af732203SDimitry Andric }
942af732203SDimitry Andric
hasVarArgs() const943af732203SDimitry Andric bool TBVectorExt::hasVarArgs() const {
944af732203SDimitry Andric return GETVALUEWITHMASK(HasVarArgsMask);
945af732203SDimitry Andric }
946*5f7ddb14SDimitry Andric
getNumberOfVectorParms() const947af732203SDimitry Andric uint8_t TBVectorExt::getNumberOfVectorParms() const {
948af732203SDimitry Andric return GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask,
949af732203SDimitry Andric NumberOfVectorParmsShift);
950af732203SDimitry Andric }
951af732203SDimitry Andric
hasVMXInstruction() const952af732203SDimitry Andric bool TBVectorExt::hasVMXInstruction() const {
953af732203SDimitry Andric return GETVALUEWITHMASK(HasVMXInstructionMask);
954af732203SDimitry Andric }
955af732203SDimitry Andric #undef GETVALUEWITHMASK
956af732203SDimitry Andric #undef GETVALUEWITHMASKSHIFT
957af732203SDimitry Andric
create(const uint8_t * Ptr,uint64_t & Size)958af732203SDimitry Andric Expected<XCOFFTracebackTable> XCOFFTracebackTable::create(const uint8_t *Ptr,
959af732203SDimitry Andric uint64_t &Size) {
960af732203SDimitry Andric Error Err = Error::success();
961af732203SDimitry Andric XCOFFTracebackTable TBT(Ptr, Size, Err);
962af732203SDimitry Andric if (Err)
963af732203SDimitry Andric return std::move(Err);
964af732203SDimitry Andric return TBT;
965af732203SDimitry Andric }
966af732203SDimitry Andric
XCOFFTracebackTable(const uint8_t * Ptr,uint64_t & Size,Error & Err)967af732203SDimitry Andric XCOFFTracebackTable::XCOFFTracebackTable(const uint8_t *Ptr, uint64_t &Size,
968af732203SDimitry Andric Error &Err)
969af732203SDimitry Andric : TBPtr(Ptr) {
970af732203SDimitry Andric ErrorAsOutParameter EAO(&Err);
971af732203SDimitry Andric DataExtractor DE(ArrayRef<uint8_t>(Ptr, Size), /*IsLittleEndian=*/false,
972af732203SDimitry Andric /*AddressSize=*/0);
973af732203SDimitry Andric DataExtractor::Cursor Cur(/*Offset=*/0);
974af732203SDimitry Andric
975af732203SDimitry Andric // Skip 8 bytes of mandatory fields.
976af732203SDimitry Andric DE.getU64(Cur);
977af732203SDimitry Andric
978*5f7ddb14SDimitry Andric unsigned FixedParmsNum = getNumberOfFixedParms();
979*5f7ddb14SDimitry Andric unsigned FloatingParmsNum = getNumberOfFPParms();
980*5f7ddb14SDimitry Andric uint32_t ParamsTypeValue = 0;
981af732203SDimitry Andric
982*5f7ddb14SDimitry Andric // Begin to parse optional fields.
983*5f7ddb14SDimitry Andric if (Cur && (FixedParmsNum + FloatingParmsNum) > 0)
984*5f7ddb14SDimitry Andric ParamsTypeValue = DE.getU32(Cur);
985af732203SDimitry Andric
986af732203SDimitry Andric if (Cur && hasTraceBackTableOffset())
987af732203SDimitry Andric TraceBackTableOffset = DE.getU32(Cur);
988af732203SDimitry Andric
989af732203SDimitry Andric if (Cur && isInterruptHandler())
990af732203SDimitry Andric HandlerMask = DE.getU32(Cur);
991af732203SDimitry Andric
992af732203SDimitry Andric if (Cur && hasControlledStorage()) {
993af732203SDimitry Andric NumOfCtlAnchors = DE.getU32(Cur);
994af732203SDimitry Andric if (Cur && NumOfCtlAnchors) {
995af732203SDimitry Andric SmallVector<uint32_t, 8> Disp;
996af732203SDimitry Andric Disp.reserve(NumOfCtlAnchors.getValue());
997af732203SDimitry Andric for (uint32_t I = 0; I < NumOfCtlAnchors && Cur; ++I)
998af732203SDimitry Andric Disp.push_back(DE.getU32(Cur));
999af732203SDimitry Andric if (Cur)
1000af732203SDimitry Andric ControlledStorageInfoDisp = std::move(Disp);
1001af732203SDimitry Andric }
1002af732203SDimitry Andric }
1003af732203SDimitry Andric
1004af732203SDimitry Andric if (Cur && isFuncNamePresent()) {
1005af732203SDimitry Andric uint16_t FunctionNameLen = DE.getU16(Cur);
1006af732203SDimitry Andric if (Cur)
1007af732203SDimitry Andric FunctionName = DE.getBytes(Cur, FunctionNameLen);
1008af732203SDimitry Andric }
1009af732203SDimitry Andric
1010af732203SDimitry Andric if (Cur && isAllocaUsed())
1011af732203SDimitry Andric AllocaRegister = DE.getU8(Cur);
1012af732203SDimitry Andric
1013*5f7ddb14SDimitry Andric unsigned VectorParmsNum = 0;
1014af732203SDimitry Andric if (Cur && hasVectorInfo()) {
1015af732203SDimitry Andric StringRef VectorExtRef = DE.getBytes(Cur, 6);
1016*5f7ddb14SDimitry Andric if (Cur) {
1017*5f7ddb14SDimitry Andric Expected<TBVectorExt> TBVecExtOrErr = TBVectorExt::create(VectorExtRef);
1018*5f7ddb14SDimitry Andric if (!TBVecExtOrErr) {
1019*5f7ddb14SDimitry Andric Err = TBVecExtOrErr.takeError();
1020*5f7ddb14SDimitry Andric return;
1021*5f7ddb14SDimitry Andric }
1022*5f7ddb14SDimitry Andric VecExt = TBVecExtOrErr.get();
1023*5f7ddb14SDimitry Andric VectorParmsNum = VecExt.getValue().getNumberOfVectorParms();
1024*5f7ddb14SDimitry Andric }
1025*5f7ddb14SDimitry Andric }
1026*5f7ddb14SDimitry Andric
1027*5f7ddb14SDimitry Andric // As long as there is no fixed-point or floating-point parameter, this
1028*5f7ddb14SDimitry Andric // field remains not present even when hasVectorInfo gives true and
1029*5f7ddb14SDimitry Andric // indicates the presence of vector parameters.
1030*5f7ddb14SDimitry Andric if (Cur && (FixedParmsNum + FloatingParmsNum) > 0) {
1031*5f7ddb14SDimitry Andric Expected<SmallString<32>> ParmsTypeOrError =
1032*5f7ddb14SDimitry Andric hasVectorInfo()
1033*5f7ddb14SDimitry Andric ? parseParmsTypeWithVecInfo(ParamsTypeValue, FixedParmsNum,
1034*5f7ddb14SDimitry Andric FloatingParmsNum, VectorParmsNum)
1035*5f7ddb14SDimitry Andric : parseParmsType(ParamsTypeValue, FixedParmsNum, FloatingParmsNum);
1036*5f7ddb14SDimitry Andric
1037*5f7ddb14SDimitry Andric if (!ParmsTypeOrError) {
1038*5f7ddb14SDimitry Andric Err = ParmsTypeOrError.takeError();
1039*5f7ddb14SDimitry Andric return;
1040*5f7ddb14SDimitry Andric }
1041*5f7ddb14SDimitry Andric ParmsType = ParmsTypeOrError.get();
1042af732203SDimitry Andric }
1043af732203SDimitry Andric
1044af732203SDimitry Andric if (Cur && hasExtensionTable())
1045af732203SDimitry Andric ExtensionTable = DE.getU8(Cur);
1046af732203SDimitry Andric
1047af732203SDimitry Andric if (!Cur)
1048af732203SDimitry Andric Err = Cur.takeError();
1049*5f7ddb14SDimitry Andric
1050af732203SDimitry Andric Size = Cur.tell();
1051af732203SDimitry Andric }
1052af732203SDimitry Andric
1053af732203SDimitry Andric #define GETBITWITHMASK(P, X) \
1054af732203SDimitry Andric (support::endian::read32be(TBPtr + (P)) & (TracebackTable::X))
1055af732203SDimitry Andric #define GETBITWITHMASKSHIFT(P, X, S) \
1056af732203SDimitry Andric ((support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) >> \
1057af732203SDimitry Andric (TracebackTable::S))
1058af732203SDimitry Andric
getVersion() const1059af732203SDimitry Andric uint8_t XCOFFTracebackTable::getVersion() const {
1060af732203SDimitry Andric return GETBITWITHMASKSHIFT(0, VersionMask, VersionShift);
1061af732203SDimitry Andric }
1062af732203SDimitry Andric
getLanguageID() const1063af732203SDimitry Andric uint8_t XCOFFTracebackTable::getLanguageID() const {
1064af732203SDimitry Andric return GETBITWITHMASKSHIFT(0, LanguageIdMask, LanguageIdShift);
1065af732203SDimitry Andric }
1066af732203SDimitry Andric
isGlobalLinkage() const1067af732203SDimitry Andric bool XCOFFTracebackTable::isGlobalLinkage() const {
1068af732203SDimitry Andric return GETBITWITHMASK(0, IsGlobaLinkageMask);
1069af732203SDimitry Andric }
1070af732203SDimitry Andric
isOutOfLineEpilogOrPrologue() const1071af732203SDimitry Andric bool XCOFFTracebackTable::isOutOfLineEpilogOrPrologue() const {
1072af732203SDimitry Andric return GETBITWITHMASK(0, IsOutOfLineEpilogOrPrologueMask);
1073af732203SDimitry Andric }
1074af732203SDimitry Andric
hasTraceBackTableOffset() const1075af732203SDimitry Andric bool XCOFFTracebackTable::hasTraceBackTableOffset() const {
1076af732203SDimitry Andric return GETBITWITHMASK(0, HasTraceBackTableOffsetMask);
1077af732203SDimitry Andric }
1078af732203SDimitry Andric
isInternalProcedure() const1079af732203SDimitry Andric bool XCOFFTracebackTable::isInternalProcedure() const {
1080af732203SDimitry Andric return GETBITWITHMASK(0, IsInternalProcedureMask);
1081af732203SDimitry Andric }
1082af732203SDimitry Andric
hasControlledStorage() const1083af732203SDimitry Andric bool XCOFFTracebackTable::hasControlledStorage() const {
1084af732203SDimitry Andric return GETBITWITHMASK(0, HasControlledStorageMask);
1085af732203SDimitry Andric }
1086af732203SDimitry Andric
isTOCless() const1087af732203SDimitry Andric bool XCOFFTracebackTable::isTOCless() const {
1088af732203SDimitry Andric return GETBITWITHMASK(0, IsTOClessMask);
1089af732203SDimitry Andric }
1090af732203SDimitry Andric
isFloatingPointPresent() const1091af732203SDimitry Andric bool XCOFFTracebackTable::isFloatingPointPresent() const {
1092af732203SDimitry Andric return GETBITWITHMASK(0, IsFloatingPointPresentMask);
1093af732203SDimitry Andric }
1094af732203SDimitry Andric
isFloatingPointOperationLogOrAbortEnabled() const1095af732203SDimitry Andric bool XCOFFTracebackTable::isFloatingPointOperationLogOrAbortEnabled() const {
1096af732203SDimitry Andric return GETBITWITHMASK(0, IsFloatingPointOperationLogOrAbortEnabledMask);
1097af732203SDimitry Andric }
1098af732203SDimitry Andric
isInterruptHandler() const1099af732203SDimitry Andric bool XCOFFTracebackTable::isInterruptHandler() const {
1100af732203SDimitry Andric return GETBITWITHMASK(0, IsInterruptHandlerMask);
1101af732203SDimitry Andric }
1102af732203SDimitry Andric
isFuncNamePresent() const1103af732203SDimitry Andric bool XCOFFTracebackTable::isFuncNamePresent() const {
1104af732203SDimitry Andric return GETBITWITHMASK(0, IsFunctionNamePresentMask);
1105af732203SDimitry Andric }
1106af732203SDimitry Andric
isAllocaUsed() const1107af732203SDimitry Andric bool XCOFFTracebackTable::isAllocaUsed() const {
1108af732203SDimitry Andric return GETBITWITHMASK(0, IsAllocaUsedMask);
1109af732203SDimitry Andric }
1110af732203SDimitry Andric
getOnConditionDirective() const1111af732203SDimitry Andric uint8_t XCOFFTracebackTable::getOnConditionDirective() const {
1112af732203SDimitry Andric return GETBITWITHMASKSHIFT(0, OnConditionDirectiveMask,
1113af732203SDimitry Andric OnConditionDirectiveShift);
1114af732203SDimitry Andric }
1115af732203SDimitry Andric
isCRSaved() const1116af732203SDimitry Andric bool XCOFFTracebackTable::isCRSaved() const {
1117af732203SDimitry Andric return GETBITWITHMASK(0, IsCRSavedMask);
1118af732203SDimitry Andric }
1119af732203SDimitry Andric
isLRSaved() const1120af732203SDimitry Andric bool XCOFFTracebackTable::isLRSaved() const {
1121af732203SDimitry Andric return GETBITWITHMASK(0, IsLRSavedMask);
1122af732203SDimitry Andric }
1123af732203SDimitry Andric
isBackChainStored() const1124af732203SDimitry Andric bool XCOFFTracebackTable::isBackChainStored() const {
1125af732203SDimitry Andric return GETBITWITHMASK(4, IsBackChainStoredMask);
1126af732203SDimitry Andric }
1127af732203SDimitry Andric
isFixup() const1128af732203SDimitry Andric bool XCOFFTracebackTable::isFixup() const {
1129af732203SDimitry Andric return GETBITWITHMASK(4, IsFixupMask);
1130af732203SDimitry Andric }
1131af732203SDimitry Andric
getNumOfFPRsSaved() const1132af732203SDimitry Andric uint8_t XCOFFTracebackTable::getNumOfFPRsSaved() const {
1133af732203SDimitry Andric return GETBITWITHMASKSHIFT(4, FPRSavedMask, FPRSavedShift);
1134af732203SDimitry Andric }
1135af732203SDimitry Andric
hasExtensionTable() const1136af732203SDimitry Andric bool XCOFFTracebackTable::hasExtensionTable() const {
1137af732203SDimitry Andric return GETBITWITHMASK(4, HasExtensionTableMask);
1138af732203SDimitry Andric }
1139af732203SDimitry Andric
hasVectorInfo() const1140af732203SDimitry Andric bool XCOFFTracebackTable::hasVectorInfo() const {
1141af732203SDimitry Andric return GETBITWITHMASK(4, HasVectorInfoMask);
1142af732203SDimitry Andric }
1143af732203SDimitry Andric
getNumOfGPRsSaved() const1144af732203SDimitry Andric uint8_t XCOFFTracebackTable::getNumOfGPRsSaved() const {
1145af732203SDimitry Andric return GETBITWITHMASKSHIFT(4, GPRSavedMask, GPRSavedShift);
1146af732203SDimitry Andric }
1147af732203SDimitry Andric
getNumberOfFixedParms() const1148af732203SDimitry Andric uint8_t XCOFFTracebackTable::getNumberOfFixedParms() const {
1149af732203SDimitry Andric return GETBITWITHMASKSHIFT(4, NumberOfFixedParmsMask,
1150af732203SDimitry Andric NumberOfFixedParmsShift);
1151af732203SDimitry Andric }
1152af732203SDimitry Andric
getNumberOfFPParms() const1153af732203SDimitry Andric uint8_t XCOFFTracebackTable::getNumberOfFPParms() const {
1154af732203SDimitry Andric return GETBITWITHMASKSHIFT(4, NumberOfFloatingPointParmsMask,
1155af732203SDimitry Andric NumberOfFloatingPointParmsShift);
1156af732203SDimitry Andric }
1157af732203SDimitry Andric
hasParmsOnStack() const1158af732203SDimitry Andric bool XCOFFTracebackTable::hasParmsOnStack() const {
1159af732203SDimitry Andric return GETBITWITHMASK(4, HasParmsOnStackMask);
1160af732203SDimitry Andric }
1161af732203SDimitry Andric
1162af732203SDimitry Andric #undef GETBITWITHMASK
1163af732203SDimitry Andric #undef GETBITWITHMASKSHIFT
11640b57cec5SDimitry Andric } // namespace object
11650b57cec5SDimitry Andric } // namespace llvm
1166