1e6d15924SDimitry Andric //===--- XCOFFObjectFile.cpp - XCOFF object file implementation -----------===//
2e6d15924SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6d15924SDimitry Andric //
7e6d15924SDimitry Andric //===----------------------------------------------------------------------===//
8e6d15924SDimitry Andric //
9e6d15924SDimitry Andric // This file defines the XCOFFObjectFile class.
10e6d15924SDimitry Andric //
11e6d15924SDimitry Andric //===----------------------------------------------------------------------===//
12e6d15924SDimitry Andric
13e6d15924SDimitry Andric #include "llvm/Object/XCOFFObjectFile.h"
14344a3780SDimitry Andric #include "llvm/ADT/StringSwitch.h"
15b60736ecSDimitry Andric #include "llvm/Support/DataExtractor.h"
167fa27ce4SDimitry Andric #include "llvm/TargetParser/SubtargetFeature.h"
17e6d15924SDimitry Andric #include <cstddef>
18e6d15924SDimitry Andric #include <cstring>
19e6d15924SDimitry Andric
20e6d15924SDimitry Andric namespace llvm {
21b60736ecSDimitry Andric
22b60736ecSDimitry Andric using namespace XCOFF;
23b60736ecSDimitry Andric
24e6d15924SDimitry Andric namespace object {
25e6d15924SDimitry Andric
26cfca06d7SDimitry Andric static const uint8_t FunctionSym = 0x20;
27cfca06d7SDimitry Andric static const uint16_t NoRelMask = 0x0001;
28344a3780SDimitry Andric static const size_t SymbolAuxTypeOffset = 17;
291d5ae102SDimitry Andric
30e6d15924SDimitry Andric // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer
31e6d15924SDimitry Andric // 'M'. Returns a pointer to the underlying object on success.
32e6d15924SDimitry Andric template <typename T>
getObject(MemoryBufferRef M,const void * Ptr,const uint64_t Size=sizeof (T))33e6d15924SDimitry Andric static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
34e6d15924SDimitry Andric const uint64_t Size = sizeof(T)) {
35b60736ecSDimitry Andric uintptr_t Addr = reinterpret_cast<uintptr_t>(Ptr);
36cfca06d7SDimitry Andric if (Error E = Binary::checkOffset(M, Addr, Size))
37cfca06d7SDimitry Andric return std::move(E);
38e6d15924SDimitry Andric return reinterpret_cast<const T *>(Addr);
39e6d15924SDimitry Andric }
40e6d15924SDimitry Andric
getWithOffset(uintptr_t Base,ptrdiff_t Offset)41e6d15924SDimitry Andric static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) {
42e6d15924SDimitry Andric return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) +
43e6d15924SDimitry Andric Offset);
44e6d15924SDimitry Andric }
45e6d15924SDimitry Andric
viewAs(uintptr_t in)46e6d15924SDimitry Andric template <typename T> static const T *viewAs(uintptr_t in) {
47e6d15924SDimitry Andric return reinterpret_cast<const T *>(in);
48e6d15924SDimitry Andric }
49e6d15924SDimitry Andric
generateXCOFFFixedNameStringRef(const char * Name)501d5ae102SDimitry Andric static StringRef generateXCOFFFixedNameStringRef(const char *Name) {
511d5ae102SDimitry Andric auto NulCharPtr =
521d5ae102SDimitry Andric static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize));
53e6d15924SDimitry Andric return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
541d5ae102SDimitry Andric : StringRef(Name, XCOFF::NameSize);
551d5ae102SDimitry Andric }
561d5ae102SDimitry Andric
getName() const57706b4fc4SDimitry Andric template <typename T> StringRef XCOFFSectionHeader<T>::getName() const {
58706b4fc4SDimitry Andric const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
59706b4fc4SDimitry Andric return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name);
60706b4fc4SDimitry Andric }
61706b4fc4SDimitry Andric
getSectionType() const62706b4fc4SDimitry Andric template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const {
63706b4fc4SDimitry Andric const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
64706b4fc4SDimitry Andric return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask;
65706b4fc4SDimitry Andric }
66706b4fc4SDimitry Andric
67706b4fc4SDimitry Andric template <typename T>
getSectionSubtype() const68ac9a064cSDimitry Andric uint32_t XCOFFSectionHeader<T>::getSectionSubtype() const {
69ac9a064cSDimitry Andric const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
70ac9a064cSDimitry Andric return DerivedXCOFFSectionHeader.Flags & ~SectionFlagsTypeMask;
71ac9a064cSDimitry Andric }
72ac9a064cSDimitry Andric
73ac9a064cSDimitry Andric template <typename T>
isReservedSectionType() const74706b4fc4SDimitry Andric bool XCOFFSectionHeader<T>::isReservedSectionType() const {
75706b4fc4SDimitry Andric return getSectionType() & SectionFlagsReservedMask;
76706b4fc4SDimitry Andric }
77706b4fc4SDimitry Andric
78c0981da4SDimitry Andric template <typename AddressType>
isRelocationSigned() const79c0981da4SDimitry Andric bool XCOFFRelocation<AddressType>::isRelocationSigned() const {
801d5ae102SDimitry Andric return Info & XR_SIGN_INDICATOR_MASK;
811d5ae102SDimitry Andric }
821d5ae102SDimitry Andric
83c0981da4SDimitry Andric template <typename AddressType>
isFixupIndicated() const84c0981da4SDimitry Andric bool XCOFFRelocation<AddressType>::isFixupIndicated() const {
851d5ae102SDimitry Andric return Info & XR_FIXUP_INDICATOR_MASK;
861d5ae102SDimitry Andric }
871d5ae102SDimitry Andric
88c0981da4SDimitry Andric template <typename AddressType>
getRelocatedLength() const89c0981da4SDimitry Andric uint8_t XCOFFRelocation<AddressType>::getRelocatedLength() const {
901d5ae102SDimitry Andric // The relocation encodes the bit length being relocated minus 1. Add back
911d5ae102SDimitry Andric // the 1 to get the actual length being relocated.
921d5ae102SDimitry Andric return (Info & XR_BIASED_LENGTH_MASK) + 1;
93e6d15924SDimitry Andric }
94e6d15924SDimitry Andric
95e3b55780SDimitry Andric template struct ExceptionSectionEntry<support::ubig32_t>;
96e3b55780SDimitry Andric template struct ExceptionSectionEntry<support::ubig64_t>;
97e3b55780SDimitry Andric
98e3b55780SDimitry Andric template <typename T>
getLoaderSecSymNameInStrTbl(const T * LoaderSecHeader,uint64_t Offset)99e3b55780SDimitry Andric Expected<StringRef> getLoaderSecSymNameInStrTbl(const T *LoaderSecHeader,
100e3b55780SDimitry Andric uint64_t Offset) {
101e3b55780SDimitry Andric if (LoaderSecHeader->LengthOfStrTbl > Offset)
102e3b55780SDimitry Andric return (reinterpret_cast<const char *>(LoaderSecHeader) +
103e3b55780SDimitry Andric LoaderSecHeader->OffsetToStrTbl + Offset);
104e3b55780SDimitry Andric
105e3b55780SDimitry Andric return createError("entry with offset 0x" + Twine::utohexstr(Offset) +
106e3b55780SDimitry Andric " in the loader section's string table with size 0x" +
107e3b55780SDimitry Andric Twine::utohexstr(LoaderSecHeader->LengthOfStrTbl) +
108e3b55780SDimitry Andric " is invalid");
109e3b55780SDimitry Andric }
110e3b55780SDimitry Andric
getSymbolName(const LoaderSectionHeader32 * LoaderSecHeader32) const111e3b55780SDimitry Andric Expected<StringRef> LoaderSectionSymbolEntry32::getSymbolName(
112e3b55780SDimitry Andric const LoaderSectionHeader32 *LoaderSecHeader32) const {
113e3b55780SDimitry Andric const NameOffsetInStrTbl *NameInStrTbl =
114e3b55780SDimitry Andric reinterpret_cast<const NameOffsetInStrTbl *>(SymbolName);
115e3b55780SDimitry Andric if (NameInStrTbl->IsNameInStrTbl != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
116e3b55780SDimitry Andric return generateXCOFFFixedNameStringRef(SymbolName);
117e3b55780SDimitry Andric
118e3b55780SDimitry Andric return getLoaderSecSymNameInStrTbl(LoaderSecHeader32, NameInStrTbl->Offset);
119e3b55780SDimitry Andric }
120e3b55780SDimitry Andric
getSymbolName(const LoaderSectionHeader64 * LoaderSecHeader64) const121e3b55780SDimitry Andric Expected<StringRef> LoaderSectionSymbolEntry64::getSymbolName(
122e3b55780SDimitry Andric const LoaderSectionHeader64 *LoaderSecHeader64) const {
123e3b55780SDimitry Andric return getLoaderSecSymNameInStrTbl(LoaderSecHeader64, Offset);
124e3b55780SDimitry Andric }
125e3b55780SDimitry Andric
126344a3780SDimitry Andric uintptr_t
getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress,uint32_t Distance)127344a3780SDimitry Andric XCOFFObjectFile::getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress,
128344a3780SDimitry Andric uint32_t Distance) {
129344a3780SDimitry Andric return getWithOffset(CurrentAddress, Distance * XCOFF::SymbolTableEntrySize);
130344a3780SDimitry Andric }
131344a3780SDimitry Andric
132344a3780SDimitry Andric const XCOFF::SymbolAuxType *
getSymbolAuxType(uintptr_t AuxEntryAddress) const133344a3780SDimitry Andric XCOFFObjectFile::getSymbolAuxType(uintptr_t AuxEntryAddress) const {
134344a3780SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
135344a3780SDimitry Andric return viewAs<XCOFF::SymbolAuxType>(
136344a3780SDimitry Andric getWithOffset(AuxEntryAddress, SymbolAuxTypeOffset));
137344a3780SDimitry Andric }
138344a3780SDimitry Andric
checkSectionAddress(uintptr_t Addr,uintptr_t TableAddress) const139e6d15924SDimitry Andric void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr,
140e6d15924SDimitry Andric uintptr_t TableAddress) const {
141e6d15924SDimitry Andric if (Addr < TableAddress)
142e6d15924SDimitry Andric report_fatal_error("Section header outside of section header table.");
143e6d15924SDimitry Andric
144e6d15924SDimitry Andric uintptr_t Offset = Addr - TableAddress;
145e6d15924SDimitry Andric if (Offset >= getSectionHeaderSize() * getNumberOfSections())
146e6d15924SDimitry Andric report_fatal_error("Section header outside of section header table.");
147e6d15924SDimitry Andric
148e6d15924SDimitry Andric if (Offset % getSectionHeaderSize() != 0)
149e6d15924SDimitry Andric report_fatal_error(
150e6d15924SDimitry Andric "Section header pointer does not point to a valid section header.");
151e6d15924SDimitry Andric }
152e6d15924SDimitry Andric
153e6d15924SDimitry Andric const XCOFFSectionHeader32 *
toSection32(DataRefImpl Ref) const154e6d15924SDimitry Andric XCOFFObjectFile::toSection32(DataRefImpl Ref) const {
155e6d15924SDimitry Andric assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
156e6d15924SDimitry Andric #ifndef NDEBUG
157e6d15924SDimitry Andric checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
158e6d15924SDimitry Andric #endif
159e6d15924SDimitry Andric return viewAs<XCOFFSectionHeader32>(Ref.p);
160e6d15924SDimitry Andric }
161e6d15924SDimitry Andric
162e6d15924SDimitry Andric const XCOFFSectionHeader64 *
toSection64(DataRefImpl Ref) const163e6d15924SDimitry Andric XCOFFObjectFile::toSection64(DataRefImpl Ref) const {
164e6d15924SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
165e6d15924SDimitry Andric #ifndef NDEBUG
166e6d15924SDimitry Andric checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
167e6d15924SDimitry Andric #endif
168e6d15924SDimitry Andric return viewAs<XCOFFSectionHeader64>(Ref.p);
169e6d15924SDimitry Andric }
170e6d15924SDimitry Andric
toSymbolRef(DataRefImpl Ref) const171344a3780SDimitry Andric XCOFFSymbolRef XCOFFObjectFile::toSymbolRef(DataRefImpl Ref) const {
172e6d15924SDimitry Andric assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!");
1731d5ae102SDimitry Andric #ifndef NDEBUG
1741d5ae102SDimitry Andric checkSymbolEntryPointer(Ref.p);
1751d5ae102SDimitry Andric #endif
176344a3780SDimitry Andric return XCOFFSymbolRef(Ref, this);
177e6d15924SDimitry Andric }
178e6d15924SDimitry Andric
fileHeader32() const179e6d15924SDimitry Andric const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const {
180e6d15924SDimitry Andric assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
181e6d15924SDimitry Andric return static_cast<const XCOFFFileHeader32 *>(FileHeader);
182e6d15924SDimitry Andric }
183e6d15924SDimitry Andric
fileHeader64() const184e6d15924SDimitry Andric const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const {
185e6d15924SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
186e6d15924SDimitry Andric return static_cast<const XCOFFFileHeader64 *>(FileHeader);
187e6d15924SDimitry Andric }
188e6d15924SDimitry Andric
auxiliaryHeader32() const189c0981da4SDimitry Andric const XCOFFAuxiliaryHeader32 *XCOFFObjectFile::auxiliaryHeader32() const {
190c0981da4SDimitry Andric assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
191c0981da4SDimitry Andric return static_cast<const XCOFFAuxiliaryHeader32 *>(AuxiliaryHeader);
192c0981da4SDimitry Andric }
193c0981da4SDimitry Andric
auxiliaryHeader64() const194c0981da4SDimitry Andric const XCOFFAuxiliaryHeader64 *XCOFFObjectFile::auxiliaryHeader64() const {
195c0981da4SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
196c0981da4SDimitry Andric return static_cast<const XCOFFAuxiliaryHeader64 *>(AuxiliaryHeader);
197c0981da4SDimitry Andric }
198c0981da4SDimitry Andric
sectionHeaderTable() const199c0981da4SDimitry Andric template <typename T> const T *XCOFFObjectFile::sectionHeaderTable() const {
200c0981da4SDimitry Andric return static_cast<const T *>(SectionHeaderTable);
201c0981da4SDimitry Andric }
202c0981da4SDimitry Andric
203e6d15924SDimitry Andric const XCOFFSectionHeader32 *
sectionHeaderTable32() const204e6d15924SDimitry Andric XCOFFObjectFile::sectionHeaderTable32() const {
205e6d15924SDimitry Andric assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
206e6d15924SDimitry Andric return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable);
207e6d15924SDimitry Andric }
208e6d15924SDimitry Andric
209e6d15924SDimitry Andric const XCOFFSectionHeader64 *
sectionHeaderTable64() const210e6d15924SDimitry Andric XCOFFObjectFile::sectionHeaderTable64() const {
211e6d15924SDimitry Andric assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
212e6d15924SDimitry Andric return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable);
213e6d15924SDimitry Andric }
214e6d15924SDimitry Andric
moveSymbolNext(DataRefImpl & Symb) const215e6d15924SDimitry Andric void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
216344a3780SDimitry Andric uintptr_t NextSymbolAddr = getAdvancedSymbolEntryAddress(
217344a3780SDimitry Andric Symb.p, toSymbolRef(Symb).getNumberOfAuxEntries() + 1);
2181d5ae102SDimitry Andric #ifndef NDEBUG
2191d5ae102SDimitry Andric // This function is used by basic_symbol_iterator, which allows to
2201d5ae102SDimitry Andric // point to the end-of-symbol-table address.
221344a3780SDimitry Andric if (NextSymbolAddr != getEndOfSymbolTableAddress())
222344a3780SDimitry Andric checkSymbolEntryPointer(NextSymbolAddr);
2231d5ae102SDimitry Andric #endif
224344a3780SDimitry Andric Symb.p = NextSymbolAddr;
225e6d15924SDimitry Andric }
226e6d15924SDimitry Andric
2271d5ae102SDimitry Andric Expected<StringRef>
getStringTableEntry(uint32_t Offset) const2281d5ae102SDimitry Andric XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
2291d5ae102SDimitry Andric // The byte offset is relative to the start of the string table.
2301d5ae102SDimitry Andric // A byte offset value of 0 is a null or zero-length symbol
231e6d15924SDimitry Andric // name. A byte offset in the range 1 to 3 (inclusive) points into the length
232e6d15924SDimitry Andric // field; as a soft-error recovery mechanism, we treat such cases as having an
233e6d15924SDimitry Andric // offset of 0.
234e6d15924SDimitry Andric if (Offset < 4)
235e6d15924SDimitry Andric return StringRef(nullptr, 0);
236e6d15924SDimitry Andric
237e6d15924SDimitry Andric if (StringTable.Data != nullptr && StringTable.Size > Offset)
238e6d15924SDimitry Andric return (StringTable.Data + Offset);
239e6d15924SDimitry Andric
240c0981da4SDimitry Andric return createError("entry with offset 0x" + Twine::utohexstr(Offset) +
241c0981da4SDimitry Andric " in a string table with size 0x" +
242c0981da4SDimitry Andric Twine::utohexstr(StringTable.Size) + " is invalid");
243e6d15924SDimitry Andric }
244e6d15924SDimitry Andric
getStringTable() const245344a3780SDimitry Andric StringRef XCOFFObjectFile::getStringTable() const {
246c0981da4SDimitry Andric // If the size is less than or equal to 4, then the string table contains no
247c0981da4SDimitry Andric // string data.
248c0981da4SDimitry Andric return StringRef(StringTable.Data,
249c0981da4SDimitry Andric StringTable.Size <= 4 ? 0 : StringTable.Size);
250344a3780SDimitry Andric }
251344a3780SDimitry Andric
2521d5ae102SDimitry Andric Expected<StringRef>
getCFileName(const XCOFFFileAuxEnt * CFileEntPtr) const2531d5ae102SDimitry Andric XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const {
254344a3780SDimitry Andric if (CFileEntPtr->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
2551d5ae102SDimitry Andric return generateXCOFFFixedNameStringRef(CFileEntPtr->Name);
2561d5ae102SDimitry Andric return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset);
2571d5ae102SDimitry Andric }
2581d5ae102SDimitry Andric
getSymbolName(DataRefImpl Symb) const2591d5ae102SDimitry Andric Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const {
260344a3780SDimitry Andric return toSymbolRef(Symb).getName();
2611d5ae102SDimitry Andric }
2621d5ae102SDimitry Andric
getSymbolAddress(DataRefImpl Symb) const263e6d15924SDimitry Andric Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
264344a3780SDimitry Andric return toSymbolRef(Symb).getValue();
265e6d15924SDimitry Andric }
266e6d15924SDimitry Andric
getSymbolValueImpl(DataRefImpl Symb) const267e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
268344a3780SDimitry Andric return toSymbolRef(Symb).getValue();
269e6d15924SDimitry Andric }
270e6d15924SDimitry Andric
getSymbolAlignment(DataRefImpl Symb) const271c0981da4SDimitry Andric uint32_t XCOFFObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
272c0981da4SDimitry Andric uint64_t Result = 0;
273c0981da4SDimitry Andric XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
274c0981da4SDimitry Andric if (XCOFFSym.isCsectSymbol()) {
275c0981da4SDimitry Andric Expected<XCOFFCsectAuxRef> CsectAuxRefOrError =
276c0981da4SDimitry Andric XCOFFSym.getXCOFFCsectAuxRef();
277c0981da4SDimitry Andric if (!CsectAuxRefOrError)
278c0981da4SDimitry Andric // TODO: report the error up the stack.
279c0981da4SDimitry Andric consumeError(CsectAuxRefOrError.takeError());
280c0981da4SDimitry Andric else
281c0981da4SDimitry Andric Result = 1ULL << CsectAuxRefOrError.get().getAlignmentLog2();
282c0981da4SDimitry Andric }
283c0981da4SDimitry Andric return Result;
284c0981da4SDimitry Andric }
285c0981da4SDimitry Andric
getCommonSymbolSizeImpl(DataRefImpl Symb) const286e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
287e6d15924SDimitry Andric uint64_t Result = 0;
288c0981da4SDimitry Andric XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
289c0981da4SDimitry Andric if (XCOFFSym.isCsectSymbol()) {
290c0981da4SDimitry Andric Expected<XCOFFCsectAuxRef> CsectAuxRefOrError =
291c0981da4SDimitry Andric XCOFFSym.getXCOFFCsectAuxRef();
292c0981da4SDimitry Andric if (!CsectAuxRefOrError)
293c0981da4SDimitry Andric // TODO: report the error up the stack.
294c0981da4SDimitry Andric consumeError(CsectAuxRefOrError.takeError());
295c0981da4SDimitry Andric else {
296c0981da4SDimitry Andric XCOFFCsectAuxRef CsectAuxRef = CsectAuxRefOrError.get();
297c0981da4SDimitry Andric assert(CsectAuxRef.getSymbolType() == XCOFF::XTY_CM);
298c0981da4SDimitry Andric Result = CsectAuxRef.getSectionOrLength();
299c0981da4SDimitry Andric }
300c0981da4SDimitry Andric }
301e6d15924SDimitry Andric return Result;
302e6d15924SDimitry Andric }
303e6d15924SDimitry Andric
304e6d15924SDimitry Andric Expected<SymbolRef::Type>
getSymbolType(DataRefImpl Symb) const305e6d15924SDimitry Andric XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const {
306c0981da4SDimitry Andric XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
307c0981da4SDimitry Andric
308b1c73532SDimitry Andric Expected<bool> IsFunction = XCOFFSym.isFunction();
309b1c73532SDimitry Andric if (!IsFunction)
310b1c73532SDimitry Andric return IsFunction.takeError();
311b1c73532SDimitry Andric
312b1c73532SDimitry Andric if (*IsFunction)
313c0981da4SDimitry Andric return SymbolRef::ST_Function;
314c0981da4SDimitry Andric
315c0981da4SDimitry Andric if (XCOFF::C_FILE == XCOFFSym.getStorageClass())
316c0981da4SDimitry Andric return SymbolRef::ST_File;
317c0981da4SDimitry Andric
318c0981da4SDimitry Andric int16_t SecNum = XCOFFSym.getSectionNumber();
319c0981da4SDimitry Andric if (SecNum <= 0)
320c0981da4SDimitry Andric return SymbolRef::ST_Other;
321c0981da4SDimitry Andric
322c0981da4SDimitry Andric Expected<DataRefImpl> SecDRIOrErr =
323c0981da4SDimitry Andric getSectionByNum(XCOFFSym.getSectionNumber());
324c0981da4SDimitry Andric
325c0981da4SDimitry Andric if (!SecDRIOrErr)
326c0981da4SDimitry Andric return SecDRIOrErr.takeError();
327c0981da4SDimitry Andric
328c0981da4SDimitry Andric DataRefImpl SecDRI = SecDRIOrErr.get();
329c0981da4SDimitry Andric
330c0981da4SDimitry Andric Expected<StringRef> SymNameOrError = XCOFFSym.getName();
331c0981da4SDimitry Andric if (SymNameOrError) {
332c0981da4SDimitry Andric // The "TOC" symbol is treated as SymbolRef::ST_Other.
333c0981da4SDimitry Andric if (SymNameOrError.get() == "TOC")
334c0981da4SDimitry Andric return SymbolRef::ST_Other;
335c0981da4SDimitry Andric
336c0981da4SDimitry Andric // The symbol for a section name is treated as SymbolRef::ST_Other.
337c0981da4SDimitry Andric StringRef SecName;
338c0981da4SDimitry Andric if (is64Bit())
339c0981da4SDimitry Andric SecName = XCOFFObjectFile::toSection64(SecDRIOrErr.get())->getName();
340c0981da4SDimitry Andric else
341c0981da4SDimitry Andric SecName = XCOFFObjectFile::toSection32(SecDRIOrErr.get())->getName();
342c0981da4SDimitry Andric
343c0981da4SDimitry Andric if (SecName == SymNameOrError.get())
344c0981da4SDimitry Andric return SymbolRef::ST_Other;
345c0981da4SDimitry Andric } else
346c0981da4SDimitry Andric return SymNameOrError.takeError();
347c0981da4SDimitry Andric
348c0981da4SDimitry Andric if (isSectionData(SecDRI) || isSectionBSS(SecDRI))
349c0981da4SDimitry Andric return SymbolRef::ST_Data;
350c0981da4SDimitry Andric
351c0981da4SDimitry Andric if (isDebugSection(SecDRI))
352c0981da4SDimitry Andric return SymbolRef::ST_Debug;
353c0981da4SDimitry Andric
354e6d15924SDimitry Andric return SymbolRef::ST_Other;
355e6d15924SDimitry Andric }
356e6d15924SDimitry Andric
357e6d15924SDimitry Andric Expected<section_iterator>
getSymbolSection(DataRefImpl Symb) const358e6d15924SDimitry Andric XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const {
359344a3780SDimitry Andric const int16_t SectNum = toSymbolRef(Symb).getSectionNumber();
360e6d15924SDimitry Andric
361e6d15924SDimitry Andric if (isReservedSectionNumber(SectNum))
362e6d15924SDimitry Andric return section_end();
363e6d15924SDimitry Andric
364e6d15924SDimitry Andric Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum);
365e6d15924SDimitry Andric if (!ExpSec)
366e6d15924SDimitry Andric return ExpSec.takeError();
367e6d15924SDimitry Andric
368e6d15924SDimitry Andric return section_iterator(SectionRef(ExpSec.get(), this));
369e6d15924SDimitry Andric }
370e6d15924SDimitry Andric
moveSectionNext(DataRefImpl & Sec) const371e6d15924SDimitry Andric void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const {
372e6d15924SDimitry Andric const char *Ptr = reinterpret_cast<const char *>(Sec.p);
373e6d15924SDimitry Andric Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
374e6d15924SDimitry Andric }
375e6d15924SDimitry Andric
getSectionName(DataRefImpl Sec) const376e6d15924SDimitry Andric Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
3771d5ae102SDimitry Andric return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec));
378e6d15924SDimitry Andric }
379e6d15924SDimitry Andric
getSectionAddress(DataRefImpl Sec) const380e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
381e6d15924SDimitry Andric // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
382e6d15924SDimitry Andric // with MSVC.
383e6d15924SDimitry Andric if (is64Bit())
384e6d15924SDimitry Andric return toSection64(Sec)->VirtualAddress;
385e6d15924SDimitry Andric
386e6d15924SDimitry Andric return toSection32(Sec)->VirtualAddress;
387e6d15924SDimitry Andric }
388e6d15924SDimitry Andric
getSectionIndex(DataRefImpl Sec) const389e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
390e6d15924SDimitry Andric // Section numbers in XCOFF are numbered beginning at 1. A section number of
391e6d15924SDimitry Andric // zero is used to indicate that a symbol is being imported or is undefined.
392e6d15924SDimitry Andric if (is64Bit())
393e6d15924SDimitry Andric return toSection64(Sec) - sectionHeaderTable64() + 1;
394e6d15924SDimitry Andric else
395e6d15924SDimitry Andric return toSection32(Sec) - sectionHeaderTable32() + 1;
396e6d15924SDimitry Andric }
397e6d15924SDimitry Andric
getSectionSize(DataRefImpl Sec) const398e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const {
399e6d15924SDimitry Andric // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
400e6d15924SDimitry Andric // with MSVC.
401e6d15924SDimitry Andric if (is64Bit())
402e6d15924SDimitry Andric return toSection64(Sec)->SectionSize;
403e6d15924SDimitry Andric
404e6d15924SDimitry Andric return toSection32(Sec)->SectionSize;
405e6d15924SDimitry Andric }
406e6d15924SDimitry Andric
407e6d15924SDimitry Andric Expected<ArrayRef<uint8_t>>
getSectionContents(DataRefImpl Sec) const408e6d15924SDimitry Andric XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
409706b4fc4SDimitry Andric if (isSectionVirtual(Sec))
410706b4fc4SDimitry Andric return ArrayRef<uint8_t>();
411706b4fc4SDimitry Andric
412706b4fc4SDimitry Andric uint64_t OffsetToRaw;
413706b4fc4SDimitry Andric if (is64Bit())
414706b4fc4SDimitry Andric OffsetToRaw = toSection64(Sec)->FileOffsetToRawData;
415706b4fc4SDimitry Andric else
416706b4fc4SDimitry Andric OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
417706b4fc4SDimitry Andric
418706b4fc4SDimitry Andric const uint8_t * ContentStart = base() + OffsetToRaw;
419706b4fc4SDimitry Andric uint64_t SectionSize = getSectionSize(Sec);
420c0981da4SDimitry Andric if (Error E = Binary::checkOffset(
421c0981da4SDimitry Andric Data, reinterpret_cast<uintptr_t>(ContentStart), SectionSize))
422c0981da4SDimitry Andric return createError(
423c0981da4SDimitry Andric toString(std::move(E)) + ": section data with offset 0x" +
424c0981da4SDimitry Andric Twine::utohexstr(OffsetToRaw) + " and size 0x" +
425c0981da4SDimitry Andric Twine::utohexstr(SectionSize) + " goes past the end of the file");
426706b4fc4SDimitry Andric
427e3b55780SDimitry Andric return ArrayRef(ContentStart, SectionSize);
428e6d15924SDimitry Andric }
429e6d15924SDimitry Andric
getSectionAlignment(DataRefImpl Sec) const430e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
431e6d15924SDimitry Andric uint64_t Result = 0;
432e6d15924SDimitry Andric llvm_unreachable("Not yet implemented!");
433e6d15924SDimitry Andric return Result;
434e6d15924SDimitry Andric }
435e6d15924SDimitry Andric
getSectionFileOffsetToRawData(DataRefImpl Sec) const436e3b55780SDimitry Andric uint64_t XCOFFObjectFile::getSectionFileOffsetToRawData(DataRefImpl Sec) const {
437e3b55780SDimitry Andric if (is64Bit())
438e3b55780SDimitry Andric return toSection64(Sec)->FileOffsetToRawData;
439c0981da4SDimitry Andric
440e3b55780SDimitry Andric return toSection32(Sec)->FileOffsetToRawData;
441c0981da4SDimitry Andric }
442c0981da4SDimitry Andric
getSectionFileOffsetToRawData(XCOFF::SectionTypeFlags SectType) const443e3b55780SDimitry Andric Expected<uintptr_t> XCOFFObjectFile::getSectionFileOffsetToRawData(
444e3b55780SDimitry Andric XCOFF::SectionTypeFlags SectType) const {
445e3b55780SDimitry Andric DataRefImpl DRI = getSectionByType(SectType);
446e3b55780SDimitry Andric
447e3b55780SDimitry Andric if (DRI.p == 0) // No section is not an error.
448c0981da4SDimitry Andric return 0;
449c0981da4SDimitry Andric
450e3b55780SDimitry Andric uint64_t SectionOffset = getSectionFileOffsetToRawData(DRI);
451e3b55780SDimitry Andric uint64_t SizeOfSection = getSectionSize(DRI);
452c0981da4SDimitry Andric
453e3b55780SDimitry Andric uintptr_t SectionStart = reinterpret_cast<uintptr_t>(base() + SectionOffset);
454e3b55780SDimitry Andric if (Error E = Binary::checkOffset(Data, SectionStart, SizeOfSection)) {
455e3b55780SDimitry Andric SmallString<32> UnknownType;
456e3b55780SDimitry Andric Twine(("<Unknown:") + Twine::utohexstr(SectType) + ">")
457e3b55780SDimitry Andric .toVector(UnknownType);
458e3b55780SDimitry Andric const char *SectionName = UnknownType.c_str();
459e3b55780SDimitry Andric
460e3b55780SDimitry Andric switch (SectType) {
461e3b55780SDimitry Andric #define ECASE(Value, String) \
462e3b55780SDimitry Andric case XCOFF::Value: \
463e3b55780SDimitry Andric SectionName = String; \
464e3b55780SDimitry Andric break
465e3b55780SDimitry Andric
466e3b55780SDimitry Andric ECASE(STYP_PAD, "pad");
467e3b55780SDimitry Andric ECASE(STYP_DWARF, "dwarf");
468e3b55780SDimitry Andric ECASE(STYP_TEXT, "text");
469e3b55780SDimitry Andric ECASE(STYP_DATA, "data");
470e3b55780SDimitry Andric ECASE(STYP_BSS, "bss");
471e3b55780SDimitry Andric ECASE(STYP_EXCEPT, "expect");
472e3b55780SDimitry Andric ECASE(STYP_INFO, "info");
473e3b55780SDimitry Andric ECASE(STYP_TDATA, "tdata");
474e3b55780SDimitry Andric ECASE(STYP_TBSS, "tbss");
475e3b55780SDimitry Andric ECASE(STYP_LOADER, "loader");
476e3b55780SDimitry Andric ECASE(STYP_DEBUG, "debug");
477e3b55780SDimitry Andric ECASE(STYP_TYPCHK, "typchk");
478e3b55780SDimitry Andric ECASE(STYP_OVRFLO, "ovrflo");
479e3b55780SDimitry Andric #undef ECASE
480e3b55780SDimitry Andric }
481e3b55780SDimitry Andric return createError(toString(std::move(E)) + ": " + SectionName +
482e3b55780SDimitry Andric " section with offset 0x" +
483e3b55780SDimitry Andric Twine::utohexstr(SectionOffset) + " and size 0x" +
484e3b55780SDimitry Andric Twine::utohexstr(SizeOfSection) +
485e3b55780SDimitry Andric " goes past the end of the file");
486e3b55780SDimitry Andric }
487e3b55780SDimitry Andric return SectionStart;
488c0981da4SDimitry Andric }
489c0981da4SDimitry Andric
isSectionCompressed(DataRefImpl Sec) const490e6d15924SDimitry Andric bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
491344a3780SDimitry Andric return false;
492e6d15924SDimitry Andric }
493e6d15924SDimitry Andric
isSectionText(DataRefImpl Sec) const494e6d15924SDimitry Andric bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const {
495e6d15924SDimitry Andric return getSectionFlags(Sec) & XCOFF::STYP_TEXT;
496e6d15924SDimitry Andric }
497e6d15924SDimitry Andric
isSectionData(DataRefImpl Sec) const498e6d15924SDimitry Andric bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const {
499e6d15924SDimitry Andric uint32_t Flags = getSectionFlags(Sec);
500e6d15924SDimitry Andric return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA);
501e6d15924SDimitry Andric }
502e6d15924SDimitry Andric
isSectionBSS(DataRefImpl Sec) const503e6d15924SDimitry Andric bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const {
504e6d15924SDimitry Andric uint32_t Flags = getSectionFlags(Sec);
505e6d15924SDimitry Andric return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS);
506e6d15924SDimitry Andric }
507e6d15924SDimitry Andric
isDebugSection(DataRefImpl Sec) const508344a3780SDimitry Andric bool XCOFFObjectFile::isDebugSection(DataRefImpl Sec) const {
509344a3780SDimitry Andric uint32_t Flags = getSectionFlags(Sec);
510344a3780SDimitry Andric return Flags & (XCOFF::STYP_DEBUG | XCOFF::STYP_DWARF);
511344a3780SDimitry Andric }
512344a3780SDimitry Andric
isSectionVirtual(DataRefImpl Sec) const513e6d15924SDimitry Andric bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
514706b4fc4SDimitry Andric return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0
515706b4fc4SDimitry Andric : toSection32(Sec)->FileOffsetToRawData == 0;
516e6d15924SDimitry Andric }
517e6d15924SDimitry Andric
section_rel_begin(DataRefImpl Sec) const518e6d15924SDimitry Andric relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
519cfca06d7SDimitry Andric DataRefImpl Ret;
520c0981da4SDimitry Andric if (is64Bit()) {
521c0981da4SDimitry Andric const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Sec);
522c0981da4SDimitry Andric auto RelocationsOrErr =
523c0981da4SDimitry Andric relocations<XCOFFSectionHeader64, XCOFFRelocation64>(*SectionEntPtr);
524c0981da4SDimitry Andric if (Error E = RelocationsOrErr.takeError()) {
525c0981da4SDimitry Andric // TODO: report the error up the stack.
526c0981da4SDimitry Andric consumeError(std::move(E));
527c0981da4SDimitry Andric return relocation_iterator(RelocationRef());
528c0981da4SDimitry Andric }
529cfca06d7SDimitry Andric Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
530c0981da4SDimitry Andric } else {
531c0981da4SDimitry Andric const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
532c0981da4SDimitry Andric auto RelocationsOrErr =
533c0981da4SDimitry Andric relocations<XCOFFSectionHeader32, XCOFFRelocation32>(*SectionEntPtr);
534c0981da4SDimitry Andric if (Error E = RelocationsOrErr.takeError()) {
535c0981da4SDimitry Andric // TODO: report the error up the stack.
536c0981da4SDimitry Andric consumeError(std::move(E));
537c0981da4SDimitry Andric return relocation_iterator(RelocationRef());
538c0981da4SDimitry Andric }
539c0981da4SDimitry Andric Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
540c0981da4SDimitry Andric }
541cfca06d7SDimitry Andric return relocation_iterator(RelocationRef(Ret, this));
542e6d15924SDimitry Andric }
543e6d15924SDimitry Andric
section_rel_end(DataRefImpl Sec) const544e6d15924SDimitry Andric relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const {
545cfca06d7SDimitry Andric DataRefImpl Ret;
546c0981da4SDimitry Andric if (is64Bit()) {
547c0981da4SDimitry Andric const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Sec);
548c0981da4SDimitry Andric auto RelocationsOrErr =
549c0981da4SDimitry Andric relocations<XCOFFSectionHeader64, XCOFFRelocation64>(*SectionEntPtr);
550c0981da4SDimitry Andric if (Error E = RelocationsOrErr.takeError()) {
551c0981da4SDimitry Andric // TODO: report the error up the stack.
552c0981da4SDimitry Andric consumeError(std::move(E));
553c0981da4SDimitry Andric return relocation_iterator(RelocationRef());
554c0981da4SDimitry Andric }
555cfca06d7SDimitry Andric Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
556c0981da4SDimitry Andric } else {
557c0981da4SDimitry Andric const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
558c0981da4SDimitry Andric auto RelocationsOrErr =
559c0981da4SDimitry Andric relocations<XCOFFSectionHeader32, XCOFFRelocation32>(*SectionEntPtr);
560c0981da4SDimitry Andric if (Error E = RelocationsOrErr.takeError()) {
561c0981da4SDimitry Andric // TODO: report the error up the stack.
562c0981da4SDimitry Andric consumeError(std::move(E));
563c0981da4SDimitry Andric return relocation_iterator(RelocationRef());
564c0981da4SDimitry Andric }
565c0981da4SDimitry Andric Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
566c0981da4SDimitry Andric }
567cfca06d7SDimitry Andric return relocation_iterator(RelocationRef(Ret, this));
568e6d15924SDimitry Andric }
569e6d15924SDimitry Andric
moveRelocationNext(DataRefImpl & Rel) const570e6d15924SDimitry Andric void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
571c0981da4SDimitry Andric if (is64Bit())
572c0981da4SDimitry Andric Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation64>(Rel.p) + 1);
573c0981da4SDimitry Andric else
574cfca06d7SDimitry Andric Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1);
575e6d15924SDimitry Andric }
576e6d15924SDimitry Andric
getRelocationOffset(DataRefImpl Rel) const577e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
578c0981da4SDimitry Andric if (is64Bit()) {
579c0981da4SDimitry Andric const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p);
580c0981da4SDimitry Andric const XCOFFSectionHeader64 *Sec64 = sectionHeaderTable64();
581c0981da4SDimitry Andric const uint64_t RelocAddress = Reloc->VirtualAddress;
582c0981da4SDimitry Andric const uint16_t NumberOfSections = getNumberOfSections();
583c0981da4SDimitry Andric for (uint16_t I = 0; I < NumberOfSections; ++I) {
584c0981da4SDimitry Andric // Find which section this relocation belongs to, and get the
585c0981da4SDimitry Andric // relocation offset relative to the start of the section.
586c0981da4SDimitry Andric if (Sec64->VirtualAddress <= RelocAddress &&
587c0981da4SDimitry Andric RelocAddress < Sec64->VirtualAddress + Sec64->SectionSize) {
588c0981da4SDimitry Andric return RelocAddress - Sec64->VirtualAddress;
589c0981da4SDimitry Andric }
590c0981da4SDimitry Andric ++Sec64;
591c0981da4SDimitry Andric }
592c0981da4SDimitry Andric } else {
593cfca06d7SDimitry Andric const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
594cfca06d7SDimitry Andric const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32();
595cfca06d7SDimitry Andric const uint32_t RelocAddress = Reloc->VirtualAddress;
596cfca06d7SDimitry Andric const uint16_t NumberOfSections = getNumberOfSections();
597c0981da4SDimitry Andric for (uint16_t I = 0; I < NumberOfSections; ++I) {
598c0981da4SDimitry Andric // Find which section this relocation belongs to, and get the
599cfca06d7SDimitry Andric // relocation offset relative to the start of the section.
600cfca06d7SDimitry Andric if (Sec32->VirtualAddress <= RelocAddress &&
601cfca06d7SDimitry Andric RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) {
602cfca06d7SDimitry Andric return RelocAddress - Sec32->VirtualAddress;
603cfca06d7SDimitry Andric }
604cfca06d7SDimitry Andric ++Sec32;
605cfca06d7SDimitry Andric }
606c0981da4SDimitry Andric }
607cfca06d7SDimitry Andric return InvalidRelocOffset;
608e6d15924SDimitry Andric }
609e6d15924SDimitry Andric
getRelocationSymbol(DataRefImpl Rel) const610e6d15924SDimitry Andric symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
611c0981da4SDimitry Andric uint32_t Index;
612c0981da4SDimitry Andric if (is64Bit()) {
613c0981da4SDimitry Andric const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p);
614c0981da4SDimitry Andric Index = Reloc->SymbolIndex;
615c0981da4SDimitry Andric
616c0981da4SDimitry Andric if (Index >= getNumberOfSymbolTableEntries64())
617c0981da4SDimitry Andric return symbol_end();
618c0981da4SDimitry Andric } else {
619cfca06d7SDimitry Andric const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
620c0981da4SDimitry Andric Index = Reloc->SymbolIndex;
621cfca06d7SDimitry Andric
622cfca06d7SDimitry Andric if (Index >= getLogicalNumberOfSymbolTableEntries32())
623cfca06d7SDimitry Andric return symbol_end();
624c0981da4SDimitry Andric }
625cfca06d7SDimitry Andric DataRefImpl SymDRI;
626344a3780SDimitry Andric SymDRI.p = getSymbolEntryAddressByIndex(Index);
627cfca06d7SDimitry Andric return symbol_iterator(SymbolRef(SymDRI, this));
628e6d15924SDimitry Andric }
629e6d15924SDimitry Andric
getRelocationType(DataRefImpl Rel) const630e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const {
631cfca06d7SDimitry Andric if (is64Bit())
632c0981da4SDimitry Andric return viewAs<XCOFFRelocation64>(Rel.p)->Type;
633cfca06d7SDimitry Andric return viewAs<XCOFFRelocation32>(Rel.p)->Type;
634e6d15924SDimitry Andric }
635e6d15924SDimitry Andric
getRelocationTypeName(DataRefImpl Rel,SmallVectorImpl<char> & Result) const636e6d15924SDimitry Andric void XCOFFObjectFile::getRelocationTypeName(
637e6d15924SDimitry Andric DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
638c0981da4SDimitry Andric StringRef Res;
639c0981da4SDimitry Andric if (is64Bit()) {
640c0981da4SDimitry Andric const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p);
641c0981da4SDimitry Andric Res = XCOFF::getRelocationTypeString(Reloc->Type);
642c0981da4SDimitry Andric } else {
643cfca06d7SDimitry Andric const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
644c0981da4SDimitry Andric Res = XCOFF::getRelocationTypeString(Reloc->Type);
645c0981da4SDimitry Andric }
646cfca06d7SDimitry Andric Result.append(Res.begin(), Res.end());
647e6d15924SDimitry Andric }
648e6d15924SDimitry Andric
getSymbolFlags(DataRefImpl Symb) const649cfca06d7SDimitry Andric Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const {
650c0981da4SDimitry Andric XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
651c0981da4SDimitry Andric uint32_t Result = SymbolRef::SF_None;
652c0981da4SDimitry Andric
653c0981da4SDimitry Andric if (XCOFFSym.getSectionNumber() == XCOFF::N_ABS)
654c0981da4SDimitry Andric Result |= SymbolRef::SF_Absolute;
655c0981da4SDimitry Andric
656c0981da4SDimitry Andric XCOFF::StorageClass SC = XCOFFSym.getStorageClass();
657c0981da4SDimitry Andric if (XCOFF::C_EXT == SC || XCOFF::C_WEAKEXT == SC)
658c0981da4SDimitry Andric Result |= SymbolRef::SF_Global;
659c0981da4SDimitry Andric
660c0981da4SDimitry Andric if (XCOFF::C_WEAKEXT == SC)
661c0981da4SDimitry Andric Result |= SymbolRef::SF_Weak;
662c0981da4SDimitry Andric
663c0981da4SDimitry Andric if (XCOFFSym.isCsectSymbol()) {
664c0981da4SDimitry Andric Expected<XCOFFCsectAuxRef> CsectAuxEntOrErr =
665c0981da4SDimitry Andric XCOFFSym.getXCOFFCsectAuxRef();
666c0981da4SDimitry Andric if (CsectAuxEntOrErr) {
667c0981da4SDimitry Andric if (CsectAuxEntOrErr.get().getSymbolType() == XCOFF::XTY_CM)
668c0981da4SDimitry Andric Result |= SymbolRef::SF_Common;
669c0981da4SDimitry Andric } else
670c0981da4SDimitry Andric return CsectAuxEntOrErr.takeError();
671c0981da4SDimitry Andric }
672c0981da4SDimitry Andric
673c0981da4SDimitry Andric if (XCOFFSym.getSectionNumber() == XCOFF::N_UNDEF)
674c0981da4SDimitry Andric Result |= SymbolRef::SF_Undefined;
675c0981da4SDimitry Andric
676145449b1SDimitry Andric // There is no visibility in old 32 bit XCOFF object file interpret.
677145449b1SDimitry Andric if (is64Bit() || (auxiliaryHeader32() && (auxiliaryHeader32()->getVersion() ==
678145449b1SDimitry Andric NEW_XCOFF_INTERPRET))) {
679145449b1SDimitry Andric uint16_t SymType = XCOFFSym.getSymbolType();
680145449b1SDimitry Andric if ((SymType & VISIBILITY_MASK) == SYM_V_HIDDEN)
681145449b1SDimitry Andric Result |= SymbolRef::SF_Hidden;
682145449b1SDimitry Andric
683145449b1SDimitry Andric if ((SymType & VISIBILITY_MASK) == SYM_V_EXPORTED)
684145449b1SDimitry Andric Result |= SymbolRef::SF_Exported;
685145449b1SDimitry Andric }
686e6d15924SDimitry Andric return Result;
687e6d15924SDimitry Andric }
688e6d15924SDimitry Andric
symbol_begin() const689e6d15924SDimitry Andric basic_symbol_iterator XCOFFObjectFile::symbol_begin() const {
690e6d15924SDimitry Andric DataRefImpl SymDRI;
691e6d15924SDimitry Andric SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr);
692e6d15924SDimitry Andric return basic_symbol_iterator(SymbolRef(SymDRI, this));
693e6d15924SDimitry Andric }
694e6d15924SDimitry Andric
symbol_end() const695e6d15924SDimitry Andric basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
696e6d15924SDimitry Andric DataRefImpl SymDRI;
697344a3780SDimitry Andric const uint32_t NumberOfSymbolTableEntries = getNumberOfSymbolTableEntries();
698344a3780SDimitry Andric SymDRI.p = getSymbolEntryAddressByIndex(NumberOfSymbolTableEntries);
699e6d15924SDimitry Andric return basic_symbol_iterator(SymbolRef(SymDRI, this));
700e6d15924SDimitry Andric }
701e6d15924SDimitry Andric
symbols() const702b1c73532SDimitry Andric XCOFFObjectFile::xcoff_symbol_iterator_range XCOFFObjectFile::symbols() const {
703b1c73532SDimitry Andric return xcoff_symbol_iterator_range(symbol_begin(), symbol_end());
704b1c73532SDimitry Andric }
705b1c73532SDimitry Andric
section_begin() const706e6d15924SDimitry Andric section_iterator XCOFFObjectFile::section_begin() const {
707e6d15924SDimitry Andric DataRefImpl DRI;
708e6d15924SDimitry Andric DRI.p = getSectionHeaderTableAddress();
709e6d15924SDimitry Andric return section_iterator(SectionRef(DRI, this));
710e6d15924SDimitry Andric }
711e6d15924SDimitry Andric
section_end() const712e6d15924SDimitry Andric section_iterator XCOFFObjectFile::section_end() const {
713e6d15924SDimitry Andric DataRefImpl DRI;
714e6d15924SDimitry Andric DRI.p = getWithOffset(getSectionHeaderTableAddress(),
715e6d15924SDimitry Andric getNumberOfSections() * getSectionHeaderSize());
716e6d15924SDimitry Andric return section_iterator(SectionRef(DRI, this));
717e6d15924SDimitry Andric }
718e6d15924SDimitry Andric
getBytesInAddress() const719e6d15924SDimitry Andric uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; }
720e6d15924SDimitry Andric
getFileFormatName() const721e6d15924SDimitry Andric StringRef XCOFFObjectFile::getFileFormatName() const {
722e6d15924SDimitry Andric return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000";
723e6d15924SDimitry Andric }
724e6d15924SDimitry Andric
getArch() const725e6d15924SDimitry Andric Triple::ArchType XCOFFObjectFile::getArch() const {
726e6d15924SDimitry Andric return is64Bit() ? Triple::ppc64 : Triple::ppc;
727e6d15924SDimitry Andric }
728e6d15924SDimitry Andric
getFeatures() const729e3b55780SDimitry Andric Expected<SubtargetFeatures> XCOFFObjectFile::getFeatures() const {
730e6d15924SDimitry Andric return SubtargetFeatures();
731e6d15924SDimitry Andric }
732e6d15924SDimitry Andric
isRelocatableObject() const733e6d15924SDimitry Andric bool XCOFFObjectFile::isRelocatableObject() const {
734cfca06d7SDimitry Andric if (is64Bit())
735344a3780SDimitry Andric return !(fileHeader64()->Flags & NoRelMask);
736cfca06d7SDimitry Andric return !(fileHeader32()->Flags & NoRelMask);
737e6d15924SDimitry Andric }
738e6d15924SDimitry Andric
getStartAddress() const739e6d15924SDimitry Andric Expected<uint64_t> XCOFFObjectFile::getStartAddress() const {
740ac9a064cSDimitry Andric if (AuxiliaryHeader == nullptr)
741e6d15924SDimitry Andric return 0;
742ac9a064cSDimitry Andric
743ac9a064cSDimitry Andric return is64Bit() ? auxiliaryHeader64()->getEntryPointAddr()
744ac9a064cSDimitry Andric : auxiliaryHeader32()->getEntryPointAddr();
745e6d15924SDimitry Andric }
746e6d15924SDimitry Andric
mapDebugSectionName(StringRef Name) const747344a3780SDimitry Andric StringRef XCOFFObjectFile::mapDebugSectionName(StringRef Name) const {
748344a3780SDimitry Andric return StringSwitch<StringRef>(Name)
749344a3780SDimitry Andric .Case("dwinfo", "debug_info")
750344a3780SDimitry Andric .Case("dwline", "debug_line")
751344a3780SDimitry Andric .Case("dwpbnms", "debug_pubnames")
752344a3780SDimitry Andric .Case("dwpbtyp", "debug_pubtypes")
753344a3780SDimitry Andric .Case("dwarnge", "debug_aranges")
754344a3780SDimitry Andric .Case("dwabrev", "debug_abbrev")
755344a3780SDimitry Andric .Case("dwstr", "debug_str")
756344a3780SDimitry Andric .Case("dwrnges", "debug_ranges")
757344a3780SDimitry Andric .Case("dwloc", "debug_loc")
758344a3780SDimitry Andric .Case("dwframe", "debug_frame")
759344a3780SDimitry Andric .Case("dwmac", "debug_macinfo")
760344a3780SDimitry Andric .Default(Name);
761344a3780SDimitry Andric }
762344a3780SDimitry Andric
getFileHeaderSize() const763e6d15924SDimitry Andric size_t XCOFFObjectFile::getFileHeaderSize() const {
764e6d15924SDimitry Andric return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32);
765e6d15924SDimitry Andric }
766e6d15924SDimitry Andric
getSectionHeaderSize() const767e6d15924SDimitry Andric size_t XCOFFObjectFile::getSectionHeaderSize() const {
768e6d15924SDimitry Andric return is64Bit() ? sizeof(XCOFFSectionHeader64) :
769e6d15924SDimitry Andric sizeof(XCOFFSectionHeader32);
770e6d15924SDimitry Andric }
771e6d15924SDimitry Andric
is64Bit() const772e6d15924SDimitry Andric bool XCOFFObjectFile::is64Bit() const {
773e6d15924SDimitry Andric return Binary::ID_XCOFF64 == getType();
774e6d15924SDimitry Andric }
775e6d15924SDimitry Andric
getRawData(const char * Start,uint64_t Size,StringRef Name) const776145449b1SDimitry Andric Expected<StringRef> XCOFFObjectFile::getRawData(const char *Start,
777145449b1SDimitry Andric uint64_t Size,
778145449b1SDimitry Andric StringRef Name) const {
779145449b1SDimitry Andric uintptr_t StartPtr = reinterpret_cast<uintptr_t>(Start);
780145449b1SDimitry Andric // TODO: this path is untested.
781145449b1SDimitry Andric if (Error E = Binary::checkOffset(Data, StartPtr, Size))
782145449b1SDimitry Andric return createError(toString(std::move(E)) + ": " + Name.data() +
783145449b1SDimitry Andric " data with offset 0x" + Twine::utohexstr(StartPtr) +
784145449b1SDimitry Andric " and size 0x" + Twine::utohexstr(Size) +
785145449b1SDimitry Andric " goes past the end of the file");
786145449b1SDimitry Andric return StringRef(Start, Size);
787145449b1SDimitry Andric }
788145449b1SDimitry Andric
getMagic() const789e6d15924SDimitry Andric uint16_t XCOFFObjectFile::getMagic() const {
790e6d15924SDimitry Andric return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic;
791e6d15924SDimitry Andric }
792e6d15924SDimitry Andric
getSectionByNum(int16_t Num) const793e6d15924SDimitry Andric Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const {
794e6d15924SDimitry Andric if (Num <= 0 || Num > getNumberOfSections())
795c0981da4SDimitry Andric return createStringError(object_error::invalid_section_index,
796c0981da4SDimitry Andric "the section index (" + Twine(Num) +
797c0981da4SDimitry Andric ") is invalid");
798e6d15924SDimitry Andric
799e6d15924SDimitry Andric DataRefImpl DRI;
800e6d15924SDimitry Andric DRI.p = getWithOffset(getSectionHeaderTableAddress(),
801e6d15924SDimitry Andric getSectionHeaderSize() * (Num - 1));
802e6d15924SDimitry Andric return DRI;
803e6d15924SDimitry Andric }
804e6d15924SDimitry Andric
805e3b55780SDimitry Andric DataRefImpl
getSectionByType(XCOFF::SectionTypeFlags SectType) const806e3b55780SDimitry Andric XCOFFObjectFile::getSectionByType(XCOFF::SectionTypeFlags SectType) const {
807e3b55780SDimitry Andric DataRefImpl DRI;
808e3b55780SDimitry Andric auto GetSectionAddr = [&](const auto &Sections) -> uintptr_t {
809e3b55780SDimitry Andric for (const auto &Sec : Sections)
810e3b55780SDimitry Andric if (Sec.getSectionType() == SectType)
811e3b55780SDimitry Andric return reinterpret_cast<uintptr_t>(&Sec);
812e3b55780SDimitry Andric return uintptr_t(0);
813e3b55780SDimitry Andric };
814e3b55780SDimitry Andric if (is64Bit())
815e3b55780SDimitry Andric DRI.p = GetSectionAddr(sections64());
816e3b55780SDimitry Andric else
817e3b55780SDimitry Andric DRI.p = GetSectionAddr(sections32());
818e3b55780SDimitry Andric return DRI;
819e3b55780SDimitry Andric }
820e3b55780SDimitry Andric
821e6d15924SDimitry Andric Expected<StringRef>
getSymbolSectionName(XCOFFSymbolRef SymEntPtr) const822344a3780SDimitry Andric XCOFFObjectFile::getSymbolSectionName(XCOFFSymbolRef SymEntPtr) const {
823344a3780SDimitry Andric const int16_t SectionNum = SymEntPtr.getSectionNumber();
824e6d15924SDimitry Andric
825e6d15924SDimitry Andric switch (SectionNum) {
826e6d15924SDimitry Andric case XCOFF::N_DEBUG:
827e6d15924SDimitry Andric return "N_DEBUG";
828e6d15924SDimitry Andric case XCOFF::N_ABS:
829e6d15924SDimitry Andric return "N_ABS";
830e6d15924SDimitry Andric case XCOFF::N_UNDEF:
831e6d15924SDimitry Andric return "N_UNDEF";
832e6d15924SDimitry Andric default:
833e6d15924SDimitry Andric Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum);
834e6d15924SDimitry Andric if (SecRef)
8351d5ae102SDimitry Andric return generateXCOFFFixedNameStringRef(
8361d5ae102SDimitry Andric getSectionNameInternal(SecRef.get()));
837e6d15924SDimitry Andric return SecRef.takeError();
838e6d15924SDimitry Andric }
839e6d15924SDimitry Andric }
840e6d15924SDimitry Andric
getSymbolSectionID(SymbolRef Sym) const841344a3780SDimitry Andric unsigned XCOFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
842344a3780SDimitry Andric XCOFFSymbolRef XCOFFSymRef(Sym.getRawDataRefImpl(), this);
843344a3780SDimitry Andric return XCOFFSymRef.getSectionNumber();
844344a3780SDimitry Andric }
845344a3780SDimitry Andric
isReservedSectionNumber(int16_t SectionNumber)846e6d15924SDimitry Andric bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
847e6d15924SDimitry Andric return (SectionNumber <= 0 && SectionNumber >= -2);
848e6d15924SDimitry Andric }
849e6d15924SDimitry Andric
getNumberOfSections() const850e6d15924SDimitry Andric uint16_t XCOFFObjectFile::getNumberOfSections() const {
851e6d15924SDimitry Andric return is64Bit() ? fileHeader64()->NumberOfSections
852e6d15924SDimitry Andric : fileHeader32()->NumberOfSections;
853e6d15924SDimitry Andric }
854e6d15924SDimitry Andric
getTimeStamp() const855e6d15924SDimitry Andric int32_t XCOFFObjectFile::getTimeStamp() const {
856e6d15924SDimitry Andric return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp;
857e6d15924SDimitry Andric }
858e6d15924SDimitry Andric
getOptionalHeaderSize() const859e6d15924SDimitry Andric uint16_t XCOFFObjectFile::getOptionalHeaderSize() const {
860e6d15924SDimitry Andric return is64Bit() ? fileHeader64()->AuxHeaderSize
861e6d15924SDimitry Andric : fileHeader32()->AuxHeaderSize;
862e6d15924SDimitry Andric }
863e6d15924SDimitry Andric
getSymbolTableOffset32() const864e6d15924SDimitry Andric uint32_t XCOFFObjectFile::getSymbolTableOffset32() const {
865e6d15924SDimitry Andric return fileHeader32()->SymbolTableOffset;
866e6d15924SDimitry Andric }
867e6d15924SDimitry Andric
getRawNumberOfSymbolTableEntries32() const868e6d15924SDimitry Andric int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const {
869e6d15924SDimitry Andric // As far as symbol table size is concerned, if this field is negative it is
870e6d15924SDimitry Andric // to be treated as a 0. However since this field is also used for printing we
871e6d15924SDimitry Andric // don't want to truncate any negative values.
872e6d15924SDimitry Andric return fileHeader32()->NumberOfSymTableEntries;
873e6d15924SDimitry Andric }
874e6d15924SDimitry Andric
getLogicalNumberOfSymbolTableEntries32() const875e6d15924SDimitry Andric uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const {
876e6d15924SDimitry Andric return (fileHeader32()->NumberOfSymTableEntries >= 0
877e6d15924SDimitry Andric ? fileHeader32()->NumberOfSymTableEntries
878e6d15924SDimitry Andric : 0);
879e6d15924SDimitry Andric }
880e6d15924SDimitry Andric
getSymbolTableOffset64() const881e6d15924SDimitry Andric uint64_t XCOFFObjectFile::getSymbolTableOffset64() const {
882e6d15924SDimitry Andric return fileHeader64()->SymbolTableOffset;
883e6d15924SDimitry Andric }
884e6d15924SDimitry Andric
getNumberOfSymbolTableEntries64() const885e6d15924SDimitry Andric uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const {
886e6d15924SDimitry Andric return fileHeader64()->NumberOfSymTableEntries;
887e6d15924SDimitry Andric }
888e6d15924SDimitry Andric
getNumberOfSymbolTableEntries() const889344a3780SDimitry Andric uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries() const {
890344a3780SDimitry Andric return is64Bit() ? getNumberOfSymbolTableEntries64()
8911d5ae102SDimitry Andric : getLogicalNumberOfSymbolTableEntries32();
892344a3780SDimitry Andric }
893344a3780SDimitry Andric
getEndOfSymbolTableAddress() const894344a3780SDimitry Andric uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const {
895344a3780SDimitry Andric const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries();
8961d5ae102SDimitry Andric return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr),
8971d5ae102SDimitry Andric XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries);
8981d5ae102SDimitry Andric }
8991d5ae102SDimitry Andric
checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const9001d5ae102SDimitry Andric void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const {
9011d5ae102SDimitry Andric if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr))
9021d5ae102SDimitry Andric report_fatal_error("Symbol table entry is outside of symbol table.");
9031d5ae102SDimitry Andric
9041d5ae102SDimitry Andric if (SymbolEntPtr >= getEndOfSymbolTableAddress())
9051d5ae102SDimitry Andric report_fatal_error("Symbol table entry is outside of symbol table.");
9061d5ae102SDimitry Andric
9071d5ae102SDimitry Andric ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) -
9081d5ae102SDimitry Andric reinterpret_cast<const char *>(SymbolTblPtr);
9091d5ae102SDimitry Andric
9101d5ae102SDimitry Andric if (Offset % XCOFF::SymbolTableEntrySize != 0)
9111d5ae102SDimitry Andric report_fatal_error(
9121d5ae102SDimitry Andric "Symbol table entry position is not valid inside of symbol table.");
9131d5ae102SDimitry Andric }
9141d5ae102SDimitry Andric
getSymbolIndex(uintptr_t SymbolEntPtr) const9151d5ae102SDimitry Andric uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const {
9161d5ae102SDimitry Andric return (reinterpret_cast<const char *>(SymbolEntPtr) -
9171d5ae102SDimitry Andric reinterpret_cast<const char *>(SymbolTblPtr)) /
9181d5ae102SDimitry Andric XCOFF::SymbolTableEntrySize;
9191d5ae102SDimitry Andric }
9201d5ae102SDimitry Andric
getSymbolSize(DataRefImpl Symb) const921c0981da4SDimitry Andric uint64_t XCOFFObjectFile::getSymbolSize(DataRefImpl Symb) const {
922c0981da4SDimitry Andric uint64_t Result = 0;
923c0981da4SDimitry Andric XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
924c0981da4SDimitry Andric if (XCOFFSym.isCsectSymbol()) {
925c0981da4SDimitry Andric Expected<XCOFFCsectAuxRef> CsectAuxRefOrError =
926c0981da4SDimitry Andric XCOFFSym.getXCOFFCsectAuxRef();
927c0981da4SDimitry Andric if (!CsectAuxRefOrError)
928c0981da4SDimitry Andric // TODO: report the error up the stack.
929c0981da4SDimitry Andric consumeError(CsectAuxRefOrError.takeError());
930c0981da4SDimitry Andric else {
931c0981da4SDimitry Andric XCOFFCsectAuxRef CsectAuxRef = CsectAuxRefOrError.get();
932c0981da4SDimitry Andric uint8_t SymType = CsectAuxRef.getSymbolType();
933c0981da4SDimitry Andric if (SymType == XCOFF::XTY_SD || SymType == XCOFF::XTY_CM)
934c0981da4SDimitry Andric Result = CsectAuxRef.getSectionOrLength();
935c0981da4SDimitry Andric }
936c0981da4SDimitry Andric }
937c0981da4SDimitry Andric return Result;
938c0981da4SDimitry Andric }
939c0981da4SDimitry Andric
getSymbolEntryAddressByIndex(uint32_t Index) const940344a3780SDimitry Andric uintptr_t XCOFFObjectFile::getSymbolEntryAddressByIndex(uint32_t Index) const {
941344a3780SDimitry Andric return getAdvancedSymbolEntryAddress(
942344a3780SDimitry Andric reinterpret_cast<uintptr_t>(getPointerToSymbolTable()), Index);
943344a3780SDimitry Andric }
944344a3780SDimitry Andric
9451d5ae102SDimitry Andric Expected<StringRef>
getSymbolNameByIndex(uint32_t Index) const9461d5ae102SDimitry Andric XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const {
947344a3780SDimitry Andric const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries();
9481d5ae102SDimitry Andric
949344a3780SDimitry Andric if (Index >= NumberOfSymTableEntries)
950c0981da4SDimitry Andric return createError("symbol index " + Twine(Index) +
951c0981da4SDimitry Andric " exceeds symbol count " +
952c0981da4SDimitry Andric Twine(NumberOfSymTableEntries));
9531d5ae102SDimitry Andric
9541d5ae102SDimitry Andric DataRefImpl SymDRI;
955344a3780SDimitry Andric SymDRI.p = getSymbolEntryAddressByIndex(Index);
9561d5ae102SDimitry Andric return getSymbolName(SymDRI);
9571d5ae102SDimitry Andric }
9581d5ae102SDimitry Andric
getFlags() const959e6d15924SDimitry Andric uint16_t XCOFFObjectFile::getFlags() const {
960e6d15924SDimitry Andric return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags;
961e6d15924SDimitry Andric }
962e6d15924SDimitry Andric
getSectionNameInternal(DataRefImpl Sec) const963e6d15924SDimitry Andric const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const {
964e6d15924SDimitry Andric return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name;
965e6d15924SDimitry Andric }
966e6d15924SDimitry Andric
getSectionHeaderTableAddress() const967e6d15924SDimitry Andric uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const {
968e6d15924SDimitry Andric return reinterpret_cast<uintptr_t>(SectionHeaderTable);
969e6d15924SDimitry Andric }
970e6d15924SDimitry Andric
getSectionFlags(DataRefImpl Sec) const971e6d15924SDimitry Andric int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const {
972e6d15924SDimitry Andric return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags;
973e6d15924SDimitry Andric }
974e6d15924SDimitry Andric
XCOFFObjectFile(unsigned int Type,MemoryBufferRef Object)975e6d15924SDimitry Andric XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object)
976e6d15924SDimitry Andric : ObjectFile(Type, Object) {
977e6d15924SDimitry Andric assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64);
978e6d15924SDimitry Andric }
979e6d15924SDimitry Andric
sections64() const980e6d15924SDimitry Andric ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const {
981e6d15924SDimitry Andric assert(is64Bit() && "64-bit interface called for non 64-bit file.");
982e6d15924SDimitry Andric const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64();
983e6d15924SDimitry Andric return ArrayRef<XCOFFSectionHeader64>(TablePtr,
984e6d15924SDimitry Andric TablePtr + getNumberOfSections());
985e6d15924SDimitry Andric }
986e6d15924SDimitry Andric
sections32() const987e6d15924SDimitry Andric ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const {
988e6d15924SDimitry Andric assert(!is64Bit() && "32-bit interface called for non 32-bit file.");
989e6d15924SDimitry Andric const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32();
990e6d15924SDimitry Andric return ArrayRef<XCOFFSectionHeader32>(TablePtr,
991e6d15924SDimitry Andric TablePtr + getNumberOfSections());
992e6d15924SDimitry Andric }
993e6d15924SDimitry Andric
9941d5ae102SDimitry Andric // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO
9951d5ae102SDimitry Andric // section header contains the actual count of relocation entries in the s_paddr
9961d5ae102SDimitry Andric // field. STYP_OVRFLO headers contain the section index of their corresponding
9971d5ae102SDimitry Andric // sections as their raw "NumberOfRelocations" field value.
998c0981da4SDimitry Andric template <typename T>
getNumberOfRelocationEntries(const XCOFFSectionHeader<T> & Sec) const999c0981da4SDimitry Andric Expected<uint32_t> XCOFFObjectFile::getNumberOfRelocationEntries(
1000c0981da4SDimitry Andric const XCOFFSectionHeader<T> &Sec) const {
1001c0981da4SDimitry Andric const T &Section = static_cast<const T &>(Sec);
1002c0981da4SDimitry Andric if (is64Bit())
1003c0981da4SDimitry Andric return Section.NumberOfRelocations;
10041d5ae102SDimitry Andric
1005c0981da4SDimitry Andric uint16_t SectionIndex = &Section - sectionHeaderTable<T>() + 1;
1006c0981da4SDimitry Andric if (Section.NumberOfRelocations < XCOFF::RelocOverflow)
1007c0981da4SDimitry Andric return Section.NumberOfRelocations;
10081d5ae102SDimitry Andric for (const auto &Sec : sections32()) {
10091d5ae102SDimitry Andric if (Sec.Flags == XCOFF::STYP_OVRFLO &&
10101d5ae102SDimitry Andric Sec.NumberOfRelocations == SectionIndex)
10111d5ae102SDimitry Andric return Sec.PhysicalAddress;
10121d5ae102SDimitry Andric }
10131d5ae102SDimitry Andric return errorCodeToError(object_error::parse_failed);
10141d5ae102SDimitry Andric }
10151d5ae102SDimitry Andric
1016c0981da4SDimitry Andric template <typename Shdr, typename Reloc>
relocations(const Shdr & Sec) const1017c0981da4SDimitry Andric Expected<ArrayRef<Reloc>> XCOFFObjectFile::relocations(const Shdr &Sec) const {
10181d5ae102SDimitry Andric uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader),
10191d5ae102SDimitry Andric Sec.FileOffsetToRelocationInfo);
1020c0981da4SDimitry Andric auto NumRelocEntriesOrErr = getNumberOfRelocationEntries(Sec);
10211d5ae102SDimitry Andric if (Error E = NumRelocEntriesOrErr.takeError())
10221d5ae102SDimitry Andric return std::move(E);
10231d5ae102SDimitry Andric
10241d5ae102SDimitry Andric uint32_t NumRelocEntries = NumRelocEntriesOrErr.get();
1025c0981da4SDimitry Andric static_assert((sizeof(Reloc) == XCOFF::RelocationSerializationSize64 ||
1026c0981da4SDimitry Andric sizeof(Reloc) == XCOFF::RelocationSerializationSize32),
1027c0981da4SDimitry Andric "Relocation structure is incorrect");
10281d5ae102SDimitry Andric auto RelocationOrErr =
1029c0981da4SDimitry Andric getObject<Reloc>(Data, reinterpret_cast<void *>(RelocAddr),
1030c0981da4SDimitry Andric NumRelocEntries * sizeof(Reloc));
1031c0981da4SDimitry Andric if (!RelocationOrErr)
1032c0981da4SDimitry Andric return createError(
1033c0981da4SDimitry Andric toString(RelocationOrErr.takeError()) + ": relocations with offset 0x" +
1034c0981da4SDimitry Andric Twine::utohexstr(Sec.FileOffsetToRelocationInfo) + " and size 0x" +
1035c0981da4SDimitry Andric Twine::utohexstr(NumRelocEntries * sizeof(Reloc)) +
1036c0981da4SDimitry Andric " go past the end of the file");
10371d5ae102SDimitry Andric
1038c0981da4SDimitry Andric const Reloc *StartReloc = RelocationOrErr.get();
10391d5ae102SDimitry Andric
1040c0981da4SDimitry Andric return ArrayRef<Reloc>(StartReloc, StartReloc + NumRelocEntries);
10411d5ae102SDimitry Andric }
10421d5ae102SDimitry Andric
1043e3b55780SDimitry Andric template <typename ExceptEnt>
getExceptionEntries() const1044e3b55780SDimitry Andric Expected<ArrayRef<ExceptEnt>> XCOFFObjectFile::getExceptionEntries() const {
1045e3b55780SDimitry Andric assert((is64Bit() && sizeof(ExceptEnt) == sizeof(ExceptionSectionEntry64)) ||
1046e3b55780SDimitry Andric (!is64Bit() && sizeof(ExceptEnt) == sizeof(ExceptionSectionEntry32)));
1047e3b55780SDimitry Andric
1048e3b55780SDimitry Andric Expected<uintptr_t> ExceptionSectOrErr =
1049e3b55780SDimitry Andric getSectionFileOffsetToRawData(XCOFF::STYP_EXCEPT);
1050e3b55780SDimitry Andric if (!ExceptionSectOrErr)
1051e3b55780SDimitry Andric return ExceptionSectOrErr.takeError();
1052e3b55780SDimitry Andric
1053e3b55780SDimitry Andric DataRefImpl DRI = getSectionByType(XCOFF::STYP_EXCEPT);
1054e3b55780SDimitry Andric if (DRI.p == 0)
1055e3b55780SDimitry Andric return ArrayRef<ExceptEnt>();
1056e3b55780SDimitry Andric
1057e3b55780SDimitry Andric ExceptEnt *ExceptEntStart =
1058e3b55780SDimitry Andric reinterpret_cast<ExceptEnt *>(*ExceptionSectOrErr);
1059e3b55780SDimitry Andric return ArrayRef<ExceptEnt>(
1060e3b55780SDimitry Andric ExceptEntStart, ExceptEntStart + getSectionSize(DRI) / sizeof(ExceptEnt));
1061e3b55780SDimitry Andric }
1062e3b55780SDimitry Andric
1063e3b55780SDimitry Andric template Expected<ArrayRef<ExceptionSectionEntry32>>
1064e3b55780SDimitry Andric XCOFFObjectFile::getExceptionEntries() const;
1065e3b55780SDimitry Andric template Expected<ArrayRef<ExceptionSectionEntry64>>
1066e3b55780SDimitry Andric XCOFFObjectFile::getExceptionEntries() const;
1067e3b55780SDimitry Andric
1068e6d15924SDimitry Andric Expected<XCOFFStringTable>
parseStringTable(const XCOFFObjectFile * Obj,uint64_t Offset)1069e6d15924SDimitry Andric XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) {
1070e6d15924SDimitry Andric // If there is a string table, then the buffer must contain at least 4 bytes
1071e6d15924SDimitry Andric // for the string table's size. Not having a string table is not an error.
1072cfca06d7SDimitry Andric if (Error E = Binary::checkOffset(
1073cfca06d7SDimitry Andric Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) {
1074cfca06d7SDimitry Andric consumeError(std::move(E));
1075e6d15924SDimitry Andric return XCOFFStringTable{0, nullptr};
1076cfca06d7SDimitry Andric }
1077e6d15924SDimitry Andric
1078e6d15924SDimitry Andric // Read the size out of the buffer.
1079e6d15924SDimitry Andric uint32_t Size = support::endian::read32be(Obj->base() + Offset);
1080e6d15924SDimitry Andric
1081e6d15924SDimitry Andric // If the size is less then 4, then the string table is just a size and no
1082e6d15924SDimitry Andric // string data.
1083e6d15924SDimitry Andric if (Size <= 4)
1084e6d15924SDimitry Andric return XCOFFStringTable{4, nullptr};
1085e6d15924SDimitry Andric
1086e6d15924SDimitry Andric auto StringTableOrErr =
1087e6d15924SDimitry Andric getObject<char>(Obj->Data, Obj->base() + Offset, Size);
1088c0981da4SDimitry Andric if (!StringTableOrErr)
1089c0981da4SDimitry Andric return createError(toString(StringTableOrErr.takeError()) +
1090c0981da4SDimitry Andric ": string table with offset 0x" +
1091c0981da4SDimitry Andric Twine::utohexstr(Offset) + " and size 0x" +
1092c0981da4SDimitry Andric Twine::utohexstr(Size) +
1093c0981da4SDimitry Andric " goes past the end of the file");
1094e6d15924SDimitry Andric
1095e6d15924SDimitry Andric const char *StringTablePtr = StringTableOrErr.get();
1096e6d15924SDimitry Andric if (StringTablePtr[Size - 1] != '\0')
1097e6d15924SDimitry Andric return errorCodeToError(object_error::string_table_non_null_end);
1098e6d15924SDimitry Andric
1099e6d15924SDimitry Andric return XCOFFStringTable{Size, StringTablePtr};
1100e6d15924SDimitry Andric }
1101e6d15924SDimitry Andric
1102c0981da4SDimitry Andric // This function returns the import file table. Each entry in the import file
1103c0981da4SDimitry Andric // table consists of: "path_name\0base_name\0archive_member_name\0".
getImportFileTable() const1104c0981da4SDimitry Andric Expected<StringRef> XCOFFObjectFile::getImportFileTable() const {
1105e3b55780SDimitry Andric Expected<uintptr_t> LoaderSectionAddrOrError =
1106e3b55780SDimitry Andric getSectionFileOffsetToRawData(XCOFF::STYP_LOADER);
1107c0981da4SDimitry Andric if (!LoaderSectionAddrOrError)
1108c0981da4SDimitry Andric return LoaderSectionAddrOrError.takeError();
1109c0981da4SDimitry Andric
1110c0981da4SDimitry Andric uintptr_t LoaderSectionAddr = LoaderSectionAddrOrError.get();
1111c0981da4SDimitry Andric if (!LoaderSectionAddr)
1112c0981da4SDimitry Andric return StringRef();
1113c0981da4SDimitry Andric
1114c0981da4SDimitry Andric uint64_t OffsetToImportFileTable = 0;
1115c0981da4SDimitry Andric uint64_t LengthOfImportFileTable = 0;
1116c0981da4SDimitry Andric if (is64Bit()) {
1117c0981da4SDimitry Andric const LoaderSectionHeader64 *LoaderSec64 =
1118c0981da4SDimitry Andric viewAs<LoaderSectionHeader64>(LoaderSectionAddr);
1119c0981da4SDimitry Andric OffsetToImportFileTable = LoaderSec64->OffsetToImpid;
1120c0981da4SDimitry Andric LengthOfImportFileTable = LoaderSec64->LengthOfImpidStrTbl;
1121c0981da4SDimitry Andric } else {
1122c0981da4SDimitry Andric const LoaderSectionHeader32 *LoaderSec32 =
1123c0981da4SDimitry Andric viewAs<LoaderSectionHeader32>(LoaderSectionAddr);
1124c0981da4SDimitry Andric OffsetToImportFileTable = LoaderSec32->OffsetToImpid;
1125c0981da4SDimitry Andric LengthOfImportFileTable = LoaderSec32->LengthOfImpidStrTbl;
1126c0981da4SDimitry Andric }
1127c0981da4SDimitry Andric
1128c0981da4SDimitry Andric auto ImportTableOrErr = getObject<char>(
1129c0981da4SDimitry Andric Data,
1130c0981da4SDimitry Andric reinterpret_cast<void *>(LoaderSectionAddr + OffsetToImportFileTable),
1131c0981da4SDimitry Andric LengthOfImportFileTable);
1132c0981da4SDimitry Andric if (!ImportTableOrErr)
1133c0981da4SDimitry Andric return createError(
1134c0981da4SDimitry Andric toString(ImportTableOrErr.takeError()) +
1135c0981da4SDimitry Andric ": import file table with offset 0x" +
1136c0981da4SDimitry Andric Twine::utohexstr(LoaderSectionAddr + OffsetToImportFileTable) +
1137c0981da4SDimitry Andric " and size 0x" + Twine::utohexstr(LengthOfImportFileTable) +
1138c0981da4SDimitry Andric " goes past the end of the file");
1139c0981da4SDimitry Andric
1140c0981da4SDimitry Andric const char *ImportTablePtr = ImportTableOrErr.get();
1141c0981da4SDimitry Andric if (ImportTablePtr[LengthOfImportFileTable - 1] != '\0')
1142c0981da4SDimitry Andric return createError(
1143c0981da4SDimitry Andric ": import file name table with offset 0x" +
1144c0981da4SDimitry Andric Twine::utohexstr(LoaderSectionAddr + OffsetToImportFileTable) +
1145c0981da4SDimitry Andric " and size 0x" + Twine::utohexstr(LengthOfImportFileTable) +
1146c0981da4SDimitry Andric " must end with a null terminator");
1147c0981da4SDimitry Andric
1148c0981da4SDimitry Andric return StringRef(ImportTablePtr, LengthOfImportFileTable);
1149c0981da4SDimitry Andric }
1150c0981da4SDimitry Andric
1151e6d15924SDimitry Andric Expected<std::unique_ptr<XCOFFObjectFile>>
create(unsigned Type,MemoryBufferRef MBR)1152e6d15924SDimitry Andric XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) {
11531d5ae102SDimitry Andric // Can't use std::make_unique because of the private constructor.
1154e6d15924SDimitry Andric std::unique_ptr<XCOFFObjectFile> Obj;
1155e6d15924SDimitry Andric Obj.reset(new XCOFFObjectFile(Type, MBR));
1156e6d15924SDimitry Andric
1157e6d15924SDimitry Andric uint64_t CurOffset = 0;
1158e6d15924SDimitry Andric const auto *Base = Obj->base();
1159e6d15924SDimitry Andric MemoryBufferRef Data = Obj->Data;
1160e6d15924SDimitry Andric
1161e6d15924SDimitry Andric // Parse file header.
1162e6d15924SDimitry Andric auto FileHeaderOrErr =
1163e6d15924SDimitry Andric getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize());
1164e6d15924SDimitry Andric if (Error E = FileHeaderOrErr.takeError())
1165e6d15924SDimitry Andric return std::move(E);
1166e6d15924SDimitry Andric Obj->FileHeader = FileHeaderOrErr.get();
1167e6d15924SDimitry Andric
1168e6d15924SDimitry Andric CurOffset += Obj->getFileHeaderSize();
1169c0981da4SDimitry Andric
1170c0981da4SDimitry Andric if (Obj->getOptionalHeaderSize()) {
1171c0981da4SDimitry Andric auto AuxiliaryHeaderOrErr =
1172c0981da4SDimitry Andric getObject<void>(Data, Base + CurOffset, Obj->getOptionalHeaderSize());
1173c0981da4SDimitry Andric if (Error E = AuxiliaryHeaderOrErr.takeError())
1174c0981da4SDimitry Andric return std::move(E);
1175c0981da4SDimitry Andric Obj->AuxiliaryHeader = AuxiliaryHeaderOrErr.get();
1176c0981da4SDimitry Andric }
1177c0981da4SDimitry Andric
1178e6d15924SDimitry Andric CurOffset += Obj->getOptionalHeaderSize();
1179e6d15924SDimitry Andric
1180e6d15924SDimitry Andric // Parse the section header table if it is present.
1181e6d15924SDimitry Andric if (Obj->getNumberOfSections()) {
1182c0981da4SDimitry Andric uint64_t SectionHeadersSize =
1183c0981da4SDimitry Andric Obj->getNumberOfSections() * Obj->getSectionHeaderSize();
1184c0981da4SDimitry Andric auto SecHeadersOrErr =
1185c0981da4SDimitry Andric getObject<void>(Data, Base + CurOffset, SectionHeadersSize);
1186c0981da4SDimitry Andric if (!SecHeadersOrErr)
1187c0981da4SDimitry Andric return createError(toString(SecHeadersOrErr.takeError()) +
1188c0981da4SDimitry Andric ": section headers with offset 0x" +
1189c0981da4SDimitry Andric Twine::utohexstr(CurOffset) + " and size 0x" +
1190c0981da4SDimitry Andric Twine::utohexstr(SectionHeadersSize) +
1191c0981da4SDimitry Andric " go past the end of the file");
1192c0981da4SDimitry Andric
1193e6d15924SDimitry Andric Obj->SectionHeaderTable = SecHeadersOrErr.get();
1194e6d15924SDimitry Andric }
1195e6d15924SDimitry Andric
1196344a3780SDimitry Andric const uint32_t NumberOfSymbolTableEntries =
1197344a3780SDimitry Andric Obj->getNumberOfSymbolTableEntries();
1198e6d15924SDimitry Andric
1199e6d15924SDimitry Andric // If there is no symbol table we are done parsing the memory buffer.
1200344a3780SDimitry Andric if (NumberOfSymbolTableEntries == 0)
1201e6d15924SDimitry Andric return std::move(Obj);
1202e6d15924SDimitry Andric
1203e6d15924SDimitry Andric // Parse symbol table.
1204344a3780SDimitry Andric CurOffset = Obj->is64Bit() ? Obj->getSymbolTableOffset64()
1205344a3780SDimitry Andric : Obj->getSymbolTableOffset32();
1206344a3780SDimitry Andric const uint64_t SymbolTableSize =
1207344a3780SDimitry Andric static_cast<uint64_t>(XCOFF::SymbolTableEntrySize) *
1208344a3780SDimitry Andric NumberOfSymbolTableEntries;
1209e6d15924SDimitry Andric auto SymTableOrErr =
1210344a3780SDimitry Andric getObject<void *>(Data, Base + CurOffset, SymbolTableSize);
1211c0981da4SDimitry Andric if (!SymTableOrErr)
1212c0981da4SDimitry Andric return createError(
1213c0981da4SDimitry Andric toString(SymTableOrErr.takeError()) + ": symbol table with offset 0x" +
1214c0981da4SDimitry Andric Twine::utohexstr(CurOffset) + " and size 0x" +
1215c0981da4SDimitry Andric Twine::utohexstr(SymbolTableSize) + " goes past the end of the file");
1216c0981da4SDimitry Andric
1217e6d15924SDimitry Andric Obj->SymbolTblPtr = SymTableOrErr.get();
1218e6d15924SDimitry Andric CurOffset += SymbolTableSize;
1219e6d15924SDimitry Andric
1220e6d15924SDimitry Andric // Parse String table.
1221e6d15924SDimitry Andric Expected<XCOFFStringTable> StringTableOrErr =
1222e6d15924SDimitry Andric parseStringTable(Obj.get(), CurOffset);
1223e6d15924SDimitry Andric if (Error E = StringTableOrErr.takeError())
1224e6d15924SDimitry Andric return std::move(E);
1225e6d15924SDimitry Andric Obj->StringTable = StringTableOrErr.get();
1226e6d15924SDimitry Andric
1227e6d15924SDimitry Andric return std::move(Obj);
1228e6d15924SDimitry Andric }
1229e6d15924SDimitry Andric
1230e6d15924SDimitry Andric Expected<std::unique_ptr<ObjectFile>>
createXCOFFObjectFile(MemoryBufferRef MemBufRef,unsigned FileType)1231e6d15924SDimitry Andric ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef,
1232e6d15924SDimitry Andric unsigned FileType) {
1233e6d15924SDimitry Andric return XCOFFObjectFile::create(FileType, MemBufRef);
1234e6d15924SDimitry Andric }
1235e6d15924SDimitry Andric
tryGetCPUName() const12367fa27ce4SDimitry Andric std::optional<StringRef> XCOFFObjectFile::tryGetCPUName() const {
12377fa27ce4SDimitry Andric return StringRef("future");
12387fa27ce4SDimitry Andric }
12397fa27ce4SDimitry Andric
isFunction() const1240b1c73532SDimitry Andric Expected<bool> XCOFFSymbolRef::isFunction() const {
1241344a3780SDimitry Andric if (!isCsectSymbol())
1242344a3780SDimitry Andric return false;
1243344a3780SDimitry Andric
1244344a3780SDimitry Andric if (getSymbolType() & FunctionSym)
1245344a3780SDimitry Andric return true;
1246344a3780SDimitry Andric
1247344a3780SDimitry Andric Expected<XCOFFCsectAuxRef> ExpCsectAuxEnt = getXCOFFCsectAuxRef();
1248b1c73532SDimitry Andric if (!ExpCsectAuxEnt)
1249b1c73532SDimitry Andric return ExpCsectAuxEnt.takeError();
1250344a3780SDimitry Andric
1251344a3780SDimitry Andric const XCOFFCsectAuxRef CsectAuxRef = ExpCsectAuxEnt.get();
1252344a3780SDimitry Andric
1253b1c73532SDimitry Andric if (CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_PR &&
1254b1c73532SDimitry Andric CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_GL)
1255344a3780SDimitry Andric return false;
1256344a3780SDimitry Andric
1257b1c73532SDimitry Andric // A function definition should not be a common type symbol or an external
1258b1c73532SDimitry Andric // symbol.
1259b1c73532SDimitry Andric if (CsectAuxRef.getSymbolType() == XCOFF::XTY_CM ||
1260b1c73532SDimitry Andric CsectAuxRef.getSymbolType() == XCOFF::XTY_ER)
1261344a3780SDimitry Andric return false;
1262344a3780SDimitry Andric
1263b1c73532SDimitry Andric // If the next symbol is an XTY_LD type symbol with the same address, this
1264b1c73532SDimitry Andric // XTY_SD symbol is not a function. Otherwise this is a function symbol for
1265b1c73532SDimitry Andric // -ffunction-sections.
1266b1c73532SDimitry Andric if (CsectAuxRef.getSymbolType() == XCOFF::XTY_SD) {
1267b1c73532SDimitry Andric // If this is a csect with size 0, it won't be a function definition.
1268b1c73532SDimitry Andric // This is used to work around the fact that LLVM always generates below
1269b1c73532SDimitry Andric // symbol for -ffunction-sections:
1270b1c73532SDimitry Andric // m 0x00000000 .text 1 unamex **No Symbol**
1271b1c73532SDimitry Andric // a4 0x00000000 0 0 SD PR 0 0
1272b1c73532SDimitry Andric // FIXME: remove or replace this meaningless symbol.
1273b1c73532SDimitry Andric if (getSize() == 0)
1274344a3780SDimitry Andric return false;
1275b1c73532SDimitry Andric
1276b1c73532SDimitry Andric xcoff_symbol_iterator NextIt(this);
1277b1c73532SDimitry Andric // If this is the last main symbol table entry, there won't be an XTY_LD
1278b1c73532SDimitry Andric // type symbol below.
1279b1c73532SDimitry Andric if (++NextIt == getObject()->symbol_end())
1280b1c73532SDimitry Andric return true;
1281b1c73532SDimitry Andric
1282b1c73532SDimitry Andric if (cantFail(getAddress()) != cantFail(NextIt->getAddress()))
1283b1c73532SDimitry Andric return true;
1284b1c73532SDimitry Andric
1285b1c73532SDimitry Andric // Check next symbol is XTY_LD. If so, this symbol is not a function.
1286b1c73532SDimitry Andric Expected<XCOFFCsectAuxRef> NextCsectAuxEnt = NextIt->getXCOFFCsectAuxRef();
1287b1c73532SDimitry Andric if (!NextCsectAuxEnt)
1288b1c73532SDimitry Andric return NextCsectAuxEnt.takeError();
1289b1c73532SDimitry Andric
1290b1c73532SDimitry Andric if (NextCsectAuxEnt.get().getSymbolType() == XCOFF::XTY_LD)
1291b1c73532SDimitry Andric return false;
1292b1c73532SDimitry Andric
1293b1c73532SDimitry Andric return true;
12941d5ae102SDimitry Andric }
12951d5ae102SDimitry Andric
1296b1c73532SDimitry Andric if (CsectAuxRef.getSymbolType() == XCOFF::XTY_LD)
1297b1c73532SDimitry Andric return true;
1298b1c73532SDimitry Andric
1299b1c73532SDimitry Andric return createError(
1300b1c73532SDimitry Andric "symbol csect aux entry with index " +
1301b1c73532SDimitry Andric Twine(getObject()->getSymbolIndex(CsectAuxRef.getEntryAddress())) +
1302b1c73532SDimitry Andric " has invalid symbol type " +
1303b1c73532SDimitry Andric Twine::utohexstr(CsectAuxRef.getSymbolType()));
13041d5ae102SDimitry Andric }
13051d5ae102SDimitry Andric
isCsectSymbol() const1306344a3780SDimitry Andric bool XCOFFSymbolRef::isCsectSymbol() const {
13071d5ae102SDimitry Andric XCOFF::StorageClass SC = getStorageClass();
13081d5ae102SDimitry Andric return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT ||
13091d5ae102SDimitry Andric SC == XCOFF::C_HIDEXT);
13101d5ae102SDimitry Andric }
13111d5ae102SDimitry Andric
getXCOFFCsectAuxRef() const1312344a3780SDimitry Andric Expected<XCOFFCsectAuxRef> XCOFFSymbolRef::getXCOFFCsectAuxRef() const {
1313344a3780SDimitry Andric assert(isCsectSymbol() &&
1314344a3780SDimitry Andric "Calling csect symbol interface with a non-csect symbol.");
13151d5ae102SDimitry Andric
1316344a3780SDimitry Andric uint8_t NumberOfAuxEntries = getNumberOfAuxEntries();
13171d5ae102SDimitry Andric
1318344a3780SDimitry Andric Expected<StringRef> NameOrErr = getName();
1319344a3780SDimitry Andric if (auto Err = NameOrErr.takeError())
1320344a3780SDimitry Andric return std::move(Err);
13211d5ae102SDimitry Andric
1322b1c73532SDimitry Andric uint32_t SymbolIdx = getObject()->getSymbolIndex(getEntryAddress());
1323344a3780SDimitry Andric if (!NumberOfAuxEntries) {
1324c0981da4SDimitry Andric return createError("csect symbol \"" + *NameOrErr + "\" with index " +
1325c0981da4SDimitry Andric Twine(SymbolIdx) + " contains no auxiliary entry");
1326344a3780SDimitry Andric }
13271d5ae102SDimitry Andric
1328b1c73532SDimitry Andric if (!getObject()->is64Bit()) {
1329344a3780SDimitry Andric // In XCOFF32, the csect auxilliary entry is always the last auxiliary
1330344a3780SDimitry Andric // entry for the symbol.
1331344a3780SDimitry Andric uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
1332344a3780SDimitry Andric getEntryAddress(), NumberOfAuxEntries);
1333344a3780SDimitry Andric return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt32>(AuxAddr));
1334344a3780SDimitry Andric }
13351d5ae102SDimitry Andric
1336344a3780SDimitry Andric // XCOFF64 uses SymbolAuxType to identify the auxiliary entry type.
1337344a3780SDimitry Andric // We need to iterate through all the auxiliary entries to find it.
1338344a3780SDimitry Andric for (uint8_t Index = NumberOfAuxEntries; Index > 0; --Index) {
1339344a3780SDimitry Andric uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
1340344a3780SDimitry Andric getEntryAddress(), Index);
1341b1c73532SDimitry Andric if (*getObject()->getSymbolAuxType(AuxAddr) ==
1342344a3780SDimitry Andric XCOFF::SymbolAuxType::AUX_CSECT) {
1343344a3780SDimitry Andric #ifndef NDEBUG
1344b1c73532SDimitry Andric getObject()->checkSymbolEntryPointer(AuxAddr);
1345344a3780SDimitry Andric #endif
1346344a3780SDimitry Andric return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt64>(AuxAddr));
1347344a3780SDimitry Andric }
1348344a3780SDimitry Andric }
13491d5ae102SDimitry Andric
1350c0981da4SDimitry Andric return createError(
1351c0981da4SDimitry Andric "a csect auxiliary entry has not been found for symbol \"" + *NameOrErr +
1352c0981da4SDimitry Andric "\" with index " + Twine(SymbolIdx));
1353344a3780SDimitry Andric }
13541d5ae102SDimitry Andric
getName() const1355344a3780SDimitry Andric Expected<StringRef> XCOFFSymbolRef::getName() const {
1356344a3780SDimitry Andric // A storage class value with the high-order bit on indicates that the name is
1357344a3780SDimitry Andric // a symbolic debugger stabstring.
1358344a3780SDimitry Andric if (getStorageClass() & 0x80)
1359344a3780SDimitry Andric return StringRef("Unimplemented Debug Name");
1360344a3780SDimitry Andric
1361b1c73532SDimitry Andric if (!getObject()->is64Bit()) {
1362b1c73532SDimitry Andric if (getSymbol32()->NameInStrTbl.Magic !=
1363b1c73532SDimitry Andric XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
1364b1c73532SDimitry Andric return generateXCOFFFixedNameStringRef(getSymbol32()->SymbolName);
1365344a3780SDimitry Andric
1366b1c73532SDimitry Andric return getObject()->getStringTableEntry(getSymbol32()->NameInStrTbl.Offset);
1367344a3780SDimitry Andric }
1368344a3780SDimitry Andric
1369b1c73532SDimitry Andric return getObject()->getStringTableEntry(getSymbol64()->Offset);
1370e6d15924SDimitry Andric }
1371e6d15924SDimitry Andric
1372ac9a064cSDimitry Andric // Explicitly instantiate template classes.
1373706b4fc4SDimitry Andric template struct XCOFFSectionHeader<XCOFFSectionHeader32>;
1374706b4fc4SDimitry Andric template struct XCOFFSectionHeader<XCOFFSectionHeader64>;
1375706b4fc4SDimitry Andric
1376c0981da4SDimitry Andric template struct XCOFFRelocation<llvm::support::ubig32_t>;
1377c0981da4SDimitry Andric template struct XCOFFRelocation<llvm::support::ubig64_t>;
1378c0981da4SDimitry Andric
1379c0981da4SDimitry Andric template llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation64>>
1380c0981da4SDimitry Andric llvm::object::XCOFFObjectFile::relocations<llvm::object::XCOFFSectionHeader64,
1381c0981da4SDimitry Andric llvm::object::XCOFFRelocation64>(
1382c0981da4SDimitry Andric llvm::object::XCOFFSectionHeader64 const &) const;
1383c0981da4SDimitry Andric template llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation32>>
1384c0981da4SDimitry Andric llvm::object::XCOFFObjectFile::relocations<llvm::object::XCOFFSectionHeader32,
1385c0981da4SDimitry Andric llvm::object::XCOFFRelocation32>(
1386c0981da4SDimitry Andric llvm::object::XCOFFSectionHeader32 const &) const;
1387c0981da4SDimitry Andric
doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes)1388b60736ecSDimitry Andric bool doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes) {
1389b60736ecSDimitry Andric if (Bytes.size() < 4)
1390b60736ecSDimitry Andric return false;
1391b60736ecSDimitry Andric
1392b60736ecSDimitry Andric return support::endian::read32be(Bytes.data()) == 0;
1393b60736ecSDimitry Andric }
1394b60736ecSDimitry Andric
1395b60736ecSDimitry Andric #define GETVALUEWITHMASK(X) (Data & (TracebackTable::X))
1396b60736ecSDimitry Andric #define GETVALUEWITHMASKSHIFT(X, S) \
1397b60736ecSDimitry Andric ((Data & (TracebackTable::X)) >> (TracebackTable::S))
1398344a3780SDimitry Andric
create(StringRef TBvectorStrRef)1399344a3780SDimitry Andric Expected<TBVectorExt> TBVectorExt::create(StringRef TBvectorStrRef) {
1400344a3780SDimitry Andric Error Err = Error::success();
1401344a3780SDimitry Andric TBVectorExt TBTVecExt(TBvectorStrRef, Err);
1402344a3780SDimitry Andric if (Err)
1403344a3780SDimitry Andric return std::move(Err);
1404344a3780SDimitry Andric return TBTVecExt;
1405344a3780SDimitry Andric }
1406344a3780SDimitry Andric
TBVectorExt(StringRef TBvectorStrRef,Error & Err)1407344a3780SDimitry Andric TBVectorExt::TBVectorExt(StringRef TBvectorStrRef, Error &Err) {
1408344a3780SDimitry Andric const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(TBvectorStrRef.data());
1409344a3780SDimitry Andric Data = support::endian::read16be(Ptr);
1410344a3780SDimitry Andric uint32_t VecParmsTypeValue = support::endian::read32be(Ptr + 2);
1411344a3780SDimitry Andric unsigned ParmsNum =
1412344a3780SDimitry Andric GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask, NumberOfVectorParmsShift);
1413344a3780SDimitry Andric
1414344a3780SDimitry Andric ErrorAsOutParameter EAO(&Err);
1415344a3780SDimitry Andric Expected<SmallString<32>> VecParmsTypeOrError =
1416344a3780SDimitry Andric parseVectorParmsType(VecParmsTypeValue, ParmsNum);
1417344a3780SDimitry Andric if (!VecParmsTypeOrError)
1418344a3780SDimitry Andric Err = VecParmsTypeOrError.takeError();
1419344a3780SDimitry Andric else
1420344a3780SDimitry Andric VecParmsInfo = VecParmsTypeOrError.get();
1421344a3780SDimitry Andric }
1422344a3780SDimitry Andric
getNumberOfVRSaved() const1423b60736ecSDimitry Andric uint8_t TBVectorExt::getNumberOfVRSaved() const {
1424b60736ecSDimitry Andric return GETVALUEWITHMASKSHIFT(NumberOfVRSavedMask, NumberOfVRSavedShift);
1425b60736ecSDimitry Andric }
1426b60736ecSDimitry Andric
isVRSavedOnStack() const1427b60736ecSDimitry Andric bool TBVectorExt::isVRSavedOnStack() const {
1428b60736ecSDimitry Andric return GETVALUEWITHMASK(IsVRSavedOnStackMask);
1429b60736ecSDimitry Andric }
1430b60736ecSDimitry Andric
hasVarArgs() const1431b60736ecSDimitry Andric bool TBVectorExt::hasVarArgs() const {
1432b60736ecSDimitry Andric return GETVALUEWITHMASK(HasVarArgsMask);
1433b60736ecSDimitry Andric }
1434344a3780SDimitry Andric
getNumberOfVectorParms() const1435b60736ecSDimitry Andric uint8_t TBVectorExt::getNumberOfVectorParms() const {
1436b60736ecSDimitry Andric return GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask,
1437b60736ecSDimitry Andric NumberOfVectorParmsShift);
1438b60736ecSDimitry Andric }
1439b60736ecSDimitry Andric
hasVMXInstruction() const1440b60736ecSDimitry Andric bool TBVectorExt::hasVMXInstruction() const {
1441b60736ecSDimitry Andric return GETVALUEWITHMASK(HasVMXInstructionMask);
1442b60736ecSDimitry Andric }
1443b60736ecSDimitry Andric #undef GETVALUEWITHMASK
1444b60736ecSDimitry Andric #undef GETVALUEWITHMASKSHIFT
1445b60736ecSDimitry Andric
14467fa27ce4SDimitry Andric Expected<XCOFFTracebackTable>
create(const uint8_t * Ptr,uint64_t & Size,bool Is64Bit)14477fa27ce4SDimitry Andric XCOFFTracebackTable::create(const uint8_t *Ptr, uint64_t &Size, bool Is64Bit) {
1448b60736ecSDimitry Andric Error Err = Error::success();
14497fa27ce4SDimitry Andric XCOFFTracebackTable TBT(Ptr, Size, Err, Is64Bit);
1450b60736ecSDimitry Andric if (Err)
1451b60736ecSDimitry Andric return std::move(Err);
1452b60736ecSDimitry Andric return TBT;
1453b60736ecSDimitry Andric }
1454b60736ecSDimitry Andric
XCOFFTracebackTable(const uint8_t * Ptr,uint64_t & Size,Error & Err,bool Is64Bit)1455b60736ecSDimitry Andric XCOFFTracebackTable::XCOFFTracebackTable(const uint8_t *Ptr, uint64_t &Size,
14567fa27ce4SDimitry Andric Error &Err, bool Is64Bit)
14577fa27ce4SDimitry Andric : TBPtr(Ptr), Is64BitObj(Is64Bit) {
1458b60736ecSDimitry Andric ErrorAsOutParameter EAO(&Err);
1459b60736ecSDimitry Andric DataExtractor DE(ArrayRef<uint8_t>(Ptr, Size), /*IsLittleEndian=*/false,
1460b60736ecSDimitry Andric /*AddressSize=*/0);
1461b60736ecSDimitry Andric DataExtractor::Cursor Cur(/*Offset=*/0);
1462b60736ecSDimitry Andric
1463b60736ecSDimitry Andric // Skip 8 bytes of mandatory fields.
1464b60736ecSDimitry Andric DE.getU64(Cur);
1465b60736ecSDimitry Andric
1466344a3780SDimitry Andric unsigned FixedParmsNum = getNumberOfFixedParms();
1467344a3780SDimitry Andric unsigned FloatingParmsNum = getNumberOfFPParms();
1468344a3780SDimitry Andric uint32_t ParamsTypeValue = 0;
1469b60736ecSDimitry Andric
1470344a3780SDimitry Andric // Begin to parse optional fields.
1471344a3780SDimitry Andric if (Cur && (FixedParmsNum + FloatingParmsNum) > 0)
1472344a3780SDimitry Andric ParamsTypeValue = DE.getU32(Cur);
1473b60736ecSDimitry Andric
1474b60736ecSDimitry Andric if (Cur && hasTraceBackTableOffset())
1475b60736ecSDimitry Andric TraceBackTableOffset = DE.getU32(Cur);
1476b60736ecSDimitry Andric
1477b60736ecSDimitry Andric if (Cur && isInterruptHandler())
1478b60736ecSDimitry Andric HandlerMask = DE.getU32(Cur);
1479b60736ecSDimitry Andric
1480b60736ecSDimitry Andric if (Cur && hasControlledStorage()) {
1481b60736ecSDimitry Andric NumOfCtlAnchors = DE.getU32(Cur);
1482b60736ecSDimitry Andric if (Cur && NumOfCtlAnchors) {
1483b60736ecSDimitry Andric SmallVector<uint32_t, 8> Disp;
1484145449b1SDimitry Andric Disp.reserve(*NumOfCtlAnchors);
1485b60736ecSDimitry Andric for (uint32_t I = 0; I < NumOfCtlAnchors && Cur; ++I)
1486b60736ecSDimitry Andric Disp.push_back(DE.getU32(Cur));
1487b60736ecSDimitry Andric if (Cur)
1488b60736ecSDimitry Andric ControlledStorageInfoDisp = std::move(Disp);
1489b60736ecSDimitry Andric }
1490b60736ecSDimitry Andric }
1491b60736ecSDimitry Andric
1492b60736ecSDimitry Andric if (Cur && isFuncNamePresent()) {
1493b60736ecSDimitry Andric uint16_t FunctionNameLen = DE.getU16(Cur);
1494b60736ecSDimitry Andric if (Cur)
1495b60736ecSDimitry Andric FunctionName = DE.getBytes(Cur, FunctionNameLen);
1496b60736ecSDimitry Andric }
1497b60736ecSDimitry Andric
1498b60736ecSDimitry Andric if (Cur && isAllocaUsed())
1499b60736ecSDimitry Andric AllocaRegister = DE.getU8(Cur);
1500b60736ecSDimitry Andric
1501344a3780SDimitry Andric unsigned VectorParmsNum = 0;
1502b60736ecSDimitry Andric if (Cur && hasVectorInfo()) {
1503b60736ecSDimitry Andric StringRef VectorExtRef = DE.getBytes(Cur, 6);
1504344a3780SDimitry Andric if (Cur) {
1505344a3780SDimitry Andric Expected<TBVectorExt> TBVecExtOrErr = TBVectorExt::create(VectorExtRef);
1506344a3780SDimitry Andric if (!TBVecExtOrErr) {
1507344a3780SDimitry Andric Err = TBVecExtOrErr.takeError();
1508344a3780SDimitry Andric return;
1509344a3780SDimitry Andric }
1510344a3780SDimitry Andric VecExt = TBVecExtOrErr.get();
1511145449b1SDimitry Andric VectorParmsNum = VecExt->getNumberOfVectorParms();
15127fa27ce4SDimitry Andric // Skip two bytes of padding after vector info.
15137fa27ce4SDimitry Andric DE.skip(Cur, 2);
1514344a3780SDimitry Andric }
1515344a3780SDimitry Andric }
1516344a3780SDimitry Andric
1517344a3780SDimitry Andric // As long as there is no fixed-point or floating-point parameter, this
1518344a3780SDimitry Andric // field remains not present even when hasVectorInfo gives true and
1519344a3780SDimitry Andric // indicates the presence of vector parameters.
1520344a3780SDimitry Andric if (Cur && (FixedParmsNum + FloatingParmsNum) > 0) {
1521344a3780SDimitry Andric Expected<SmallString<32>> ParmsTypeOrError =
1522344a3780SDimitry Andric hasVectorInfo()
1523344a3780SDimitry Andric ? parseParmsTypeWithVecInfo(ParamsTypeValue, FixedParmsNum,
1524344a3780SDimitry Andric FloatingParmsNum, VectorParmsNum)
1525344a3780SDimitry Andric : parseParmsType(ParamsTypeValue, FixedParmsNum, FloatingParmsNum);
1526344a3780SDimitry Andric
1527344a3780SDimitry Andric if (!ParmsTypeOrError) {
1528344a3780SDimitry Andric Err = ParmsTypeOrError.takeError();
1529344a3780SDimitry Andric return;
1530344a3780SDimitry Andric }
1531344a3780SDimitry Andric ParmsType = ParmsTypeOrError.get();
1532b60736ecSDimitry Andric }
1533b60736ecSDimitry Andric
15347fa27ce4SDimitry Andric if (Cur && hasExtensionTable()) {
1535b60736ecSDimitry Andric ExtensionTable = DE.getU8(Cur);
1536b60736ecSDimitry Andric
15377fa27ce4SDimitry Andric if (*ExtensionTable & ExtendedTBTableFlag::TB_EH_INFO) {
15387fa27ce4SDimitry Andric // eh_info displacement must be 4-byte aligned.
15397fa27ce4SDimitry Andric Cur.seek(alignTo(Cur.tell(), 4));
15407fa27ce4SDimitry Andric EhInfoDisp = Is64BitObj ? DE.getU64(Cur) : DE.getU32(Cur);
15417fa27ce4SDimitry Andric }
15427fa27ce4SDimitry Andric }
1543b60736ecSDimitry Andric if (!Cur)
1544b60736ecSDimitry Andric Err = Cur.takeError();
1545344a3780SDimitry Andric
1546b60736ecSDimitry Andric Size = Cur.tell();
1547b60736ecSDimitry Andric }
1548b60736ecSDimitry Andric
1549b60736ecSDimitry Andric #define GETBITWITHMASK(P, X) \
1550b60736ecSDimitry Andric (support::endian::read32be(TBPtr + (P)) & (TracebackTable::X))
1551b60736ecSDimitry Andric #define GETBITWITHMASKSHIFT(P, X, S) \
1552b60736ecSDimitry Andric ((support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) >> \
1553b60736ecSDimitry Andric (TracebackTable::S))
1554b60736ecSDimitry Andric
getVersion() const1555b60736ecSDimitry Andric uint8_t XCOFFTracebackTable::getVersion() const {
1556b60736ecSDimitry Andric return GETBITWITHMASKSHIFT(0, VersionMask, VersionShift);
1557b60736ecSDimitry Andric }
1558b60736ecSDimitry Andric
getLanguageID() const1559b60736ecSDimitry Andric uint8_t XCOFFTracebackTable::getLanguageID() const {
1560b60736ecSDimitry Andric return GETBITWITHMASKSHIFT(0, LanguageIdMask, LanguageIdShift);
1561b60736ecSDimitry Andric }
1562b60736ecSDimitry Andric
isGlobalLinkage() const1563b60736ecSDimitry Andric bool XCOFFTracebackTable::isGlobalLinkage() const {
1564b60736ecSDimitry Andric return GETBITWITHMASK(0, IsGlobaLinkageMask);
1565b60736ecSDimitry Andric }
1566b60736ecSDimitry Andric
isOutOfLineEpilogOrPrologue() const1567b60736ecSDimitry Andric bool XCOFFTracebackTable::isOutOfLineEpilogOrPrologue() const {
1568b60736ecSDimitry Andric return GETBITWITHMASK(0, IsOutOfLineEpilogOrPrologueMask);
1569b60736ecSDimitry Andric }
1570b60736ecSDimitry Andric
hasTraceBackTableOffset() const1571b60736ecSDimitry Andric bool XCOFFTracebackTable::hasTraceBackTableOffset() const {
1572b60736ecSDimitry Andric return GETBITWITHMASK(0, HasTraceBackTableOffsetMask);
1573b60736ecSDimitry Andric }
1574b60736ecSDimitry Andric
isInternalProcedure() const1575b60736ecSDimitry Andric bool XCOFFTracebackTable::isInternalProcedure() const {
1576b60736ecSDimitry Andric return GETBITWITHMASK(0, IsInternalProcedureMask);
1577b60736ecSDimitry Andric }
1578b60736ecSDimitry Andric
hasControlledStorage() const1579b60736ecSDimitry Andric bool XCOFFTracebackTable::hasControlledStorage() const {
1580b60736ecSDimitry Andric return GETBITWITHMASK(0, HasControlledStorageMask);
1581b60736ecSDimitry Andric }
1582b60736ecSDimitry Andric
isTOCless() const1583b60736ecSDimitry Andric bool XCOFFTracebackTable::isTOCless() const {
1584b60736ecSDimitry Andric return GETBITWITHMASK(0, IsTOClessMask);
1585b60736ecSDimitry Andric }
1586b60736ecSDimitry Andric
isFloatingPointPresent() const1587b60736ecSDimitry Andric bool XCOFFTracebackTable::isFloatingPointPresent() const {
1588b60736ecSDimitry Andric return GETBITWITHMASK(0, IsFloatingPointPresentMask);
1589b60736ecSDimitry Andric }
1590b60736ecSDimitry Andric
isFloatingPointOperationLogOrAbortEnabled() const1591b60736ecSDimitry Andric bool XCOFFTracebackTable::isFloatingPointOperationLogOrAbortEnabled() const {
1592b60736ecSDimitry Andric return GETBITWITHMASK(0, IsFloatingPointOperationLogOrAbortEnabledMask);
1593b60736ecSDimitry Andric }
1594b60736ecSDimitry Andric
isInterruptHandler() const1595b60736ecSDimitry Andric bool XCOFFTracebackTable::isInterruptHandler() const {
1596b60736ecSDimitry Andric return GETBITWITHMASK(0, IsInterruptHandlerMask);
1597b60736ecSDimitry Andric }
1598b60736ecSDimitry Andric
isFuncNamePresent() const1599b60736ecSDimitry Andric bool XCOFFTracebackTable::isFuncNamePresent() const {
1600b60736ecSDimitry Andric return GETBITWITHMASK(0, IsFunctionNamePresentMask);
1601b60736ecSDimitry Andric }
1602b60736ecSDimitry Andric
isAllocaUsed() const1603b60736ecSDimitry Andric bool XCOFFTracebackTable::isAllocaUsed() const {
1604b60736ecSDimitry Andric return GETBITWITHMASK(0, IsAllocaUsedMask);
1605b60736ecSDimitry Andric }
1606b60736ecSDimitry Andric
getOnConditionDirective() const1607b60736ecSDimitry Andric uint8_t XCOFFTracebackTable::getOnConditionDirective() const {
1608b60736ecSDimitry Andric return GETBITWITHMASKSHIFT(0, OnConditionDirectiveMask,
1609b60736ecSDimitry Andric OnConditionDirectiveShift);
1610b60736ecSDimitry Andric }
1611b60736ecSDimitry Andric
isCRSaved() const1612b60736ecSDimitry Andric bool XCOFFTracebackTable::isCRSaved() const {
1613b60736ecSDimitry Andric return GETBITWITHMASK(0, IsCRSavedMask);
1614b60736ecSDimitry Andric }
1615b60736ecSDimitry Andric
isLRSaved() const1616b60736ecSDimitry Andric bool XCOFFTracebackTable::isLRSaved() const {
1617b60736ecSDimitry Andric return GETBITWITHMASK(0, IsLRSavedMask);
1618b60736ecSDimitry Andric }
1619b60736ecSDimitry Andric
isBackChainStored() const1620b60736ecSDimitry Andric bool XCOFFTracebackTable::isBackChainStored() const {
1621b60736ecSDimitry Andric return GETBITWITHMASK(4, IsBackChainStoredMask);
1622b60736ecSDimitry Andric }
1623b60736ecSDimitry Andric
isFixup() const1624b60736ecSDimitry Andric bool XCOFFTracebackTable::isFixup() const {
1625b60736ecSDimitry Andric return GETBITWITHMASK(4, IsFixupMask);
1626b60736ecSDimitry Andric }
1627b60736ecSDimitry Andric
getNumOfFPRsSaved() const1628b60736ecSDimitry Andric uint8_t XCOFFTracebackTable::getNumOfFPRsSaved() const {
1629b60736ecSDimitry Andric return GETBITWITHMASKSHIFT(4, FPRSavedMask, FPRSavedShift);
1630b60736ecSDimitry Andric }
1631b60736ecSDimitry Andric
hasExtensionTable() const1632b60736ecSDimitry Andric bool XCOFFTracebackTable::hasExtensionTable() const {
1633b60736ecSDimitry Andric return GETBITWITHMASK(4, HasExtensionTableMask);
1634b60736ecSDimitry Andric }
1635b60736ecSDimitry Andric
hasVectorInfo() const1636b60736ecSDimitry Andric bool XCOFFTracebackTable::hasVectorInfo() const {
1637b60736ecSDimitry Andric return GETBITWITHMASK(4, HasVectorInfoMask);
1638b60736ecSDimitry Andric }
1639b60736ecSDimitry Andric
getNumOfGPRsSaved() const1640b60736ecSDimitry Andric uint8_t XCOFFTracebackTable::getNumOfGPRsSaved() const {
1641b60736ecSDimitry Andric return GETBITWITHMASKSHIFT(4, GPRSavedMask, GPRSavedShift);
1642b60736ecSDimitry Andric }
1643b60736ecSDimitry Andric
getNumberOfFixedParms() const1644b60736ecSDimitry Andric uint8_t XCOFFTracebackTable::getNumberOfFixedParms() const {
1645b60736ecSDimitry Andric return GETBITWITHMASKSHIFT(4, NumberOfFixedParmsMask,
1646b60736ecSDimitry Andric NumberOfFixedParmsShift);
1647b60736ecSDimitry Andric }
1648b60736ecSDimitry Andric
getNumberOfFPParms() const1649b60736ecSDimitry Andric uint8_t XCOFFTracebackTable::getNumberOfFPParms() const {
1650b60736ecSDimitry Andric return GETBITWITHMASKSHIFT(4, NumberOfFloatingPointParmsMask,
1651b60736ecSDimitry Andric NumberOfFloatingPointParmsShift);
1652b60736ecSDimitry Andric }
1653b60736ecSDimitry Andric
hasParmsOnStack() const1654b60736ecSDimitry Andric bool XCOFFTracebackTable::hasParmsOnStack() const {
1655b60736ecSDimitry Andric return GETBITWITHMASK(4, HasParmsOnStackMask);
1656b60736ecSDimitry Andric }
1657b60736ecSDimitry Andric
1658b60736ecSDimitry Andric #undef GETBITWITHMASK
1659b60736ecSDimitry Andric #undef GETBITWITHMASKSHIFT
1660e6d15924SDimitry Andric } // namespace object
1661e6d15924SDimitry Andric } // namespace llvm
1662