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