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"
140865d414SSimon Pilgrim #include "llvm/MC/SubtargetFeature.h"
15ab2eb2bfSHubert Tong #include <cstddef>
16ab2eb2bfSHubert Tong #include <cstring>
17ab2eb2bfSHubert Tong 
18ab2eb2bfSHubert Tong namespace llvm {
19ab2eb2bfSHubert Tong namespace object {
20ab2eb2bfSHubert Tong 
21d60d7d69Sjasonliu static const uint8_t FunctionSym = 0x20;
22d60d7d69Sjasonliu static const uint8_t SymTypeMask = 0x07;
23d60d7d69Sjasonliu static const uint16_t NoRelMask = 0x0001;
247c72e82bSJason Liu 
25837ae69fSSean Fertile // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer
26837ae69fSSean Fertile // 'M'. Returns a pointer to the underlying object on success.
27ab2eb2bfSHubert Tong template <typename T>
28837ae69fSSean Fertile static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
29ab2eb2bfSHubert Tong                                      const uint64_t Size = sizeof(T)) {
30ab2eb2bfSHubert Tong   uintptr_t Addr = uintptr_t(Ptr);
31e03a135bSReid Kleckner   if (Error E = Binary::checkOffset(M, Addr, Size))
32e03a135bSReid Kleckner     return std::move(E);
33837ae69fSSean Fertile   return reinterpret_cast<const T *>(Addr);
34837ae69fSSean Fertile }
35837ae69fSSean Fertile 
36837ae69fSSean Fertile static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) {
37837ae69fSSean Fertile   return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) +
38837ae69fSSean Fertile                                      Offset);
39ab2eb2bfSHubert Tong }
40ab2eb2bfSHubert Tong 
41a93a33cbSSean Fertile template <typename T> static const T *viewAs(uintptr_t in) {
42a93a33cbSSean Fertile   return reinterpret_cast<const T *>(in);
43a93a33cbSSean Fertile }
44a93a33cbSSean Fertile 
457c72e82bSJason Liu static StringRef generateXCOFFFixedNameStringRef(const char *Name) {
4629141da7SSean Fertile   auto NulCharPtr =
4729141da7SSean Fertile       static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize));
489212206dSJason Liu   return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
4929141da7SSean Fertile                     : StringRef(Name, XCOFF::NameSize);
509212206dSJason Liu }
519212206dSJason Liu 
52c63c1a72Sdiggerlin template <typename T> StringRef XCOFFSectionHeader<T>::getName() const {
53c63c1a72Sdiggerlin   const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
54c63c1a72Sdiggerlin   return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name);
55c63c1a72Sdiggerlin }
56c63c1a72Sdiggerlin 
57c63c1a72Sdiggerlin template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const {
58c63c1a72Sdiggerlin   const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
59c63c1a72Sdiggerlin   return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask;
60c63c1a72Sdiggerlin }
61c63c1a72Sdiggerlin 
62c63c1a72Sdiggerlin template <typename T>
63c63c1a72Sdiggerlin bool XCOFFSectionHeader<T>::isReservedSectionType() const {
64c63c1a72Sdiggerlin   return getSectionType() & SectionFlagsReservedMask;
65c63c1a72Sdiggerlin }
66c63c1a72Sdiggerlin 
6734d4bff3SDigger Lin bool XCOFFRelocation32::isRelocationSigned() const {
6834d4bff3SDigger Lin   return Info & XR_SIGN_INDICATOR_MASK;
6934d4bff3SDigger Lin }
7034d4bff3SDigger Lin 
7134d4bff3SDigger Lin bool XCOFFRelocation32::isFixupIndicated() const {
7234d4bff3SDigger Lin   return Info & XR_FIXUP_INDICATOR_MASK;
7334d4bff3SDigger Lin }
7434d4bff3SDigger Lin 
7534d4bff3SDigger Lin uint8_t XCOFFRelocation32::getRelocatedLength() const {
7634d4bff3SDigger Lin   // The relocation encodes the bit length being relocated minus 1. Add back
7734d4bff3SDigger Lin   // the 1 to get the actual length being relocated.
7834d4bff3SDigger Lin   return (Info & XR_BIASED_LENGTH_MASK) + 1;
7934d4bff3SDigger Lin }
8034d4bff3SDigger Lin 
81837ae69fSSean Fertile void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr,
82837ae69fSSean Fertile                                           uintptr_t TableAddress) const {
83837ae69fSSean Fertile   if (Addr < TableAddress)
84a93a33cbSSean Fertile     report_fatal_error("Section header outside of section header table.");
85a93a33cbSSean Fertile 
86837ae69fSSean Fertile   uintptr_t Offset = Addr - TableAddress;
87837ae69fSSean Fertile   if (Offset >= getSectionHeaderSize() * getNumberOfSections())
88837ae69fSSean Fertile     report_fatal_error("Section header outside of section header table.");
89837ae69fSSean Fertile 
90a93a33cbSSean Fertile   if (Offset % getSectionHeaderSize() != 0)
91a93a33cbSSean Fertile     report_fatal_error(
92a93a33cbSSean Fertile         "Section header pointer does not point to a valid section header.");
93837ae69fSSean Fertile }
94837ae69fSSean Fertile 
95837ae69fSSean Fertile const XCOFFSectionHeader32 *
96837ae69fSSean Fertile XCOFFObjectFile::toSection32(DataRefImpl Ref) const {
97837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
98837ae69fSSean Fertile #ifndef NDEBUG
99837ae69fSSean Fertile   checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
100a93a33cbSSean Fertile #endif
101837ae69fSSean Fertile   return viewAs<XCOFFSectionHeader32>(Ref.p);
102837ae69fSSean Fertile }
103837ae69fSSean Fertile 
104837ae69fSSean Fertile const XCOFFSectionHeader64 *
105837ae69fSSean Fertile XCOFFObjectFile::toSection64(DataRefImpl Ref) const {
106837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
107837ae69fSSean Fertile #ifndef NDEBUG
108837ae69fSSean Fertile   checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
109837ae69fSSean Fertile #endif
110837ae69fSSean Fertile   return viewAs<XCOFFSectionHeader64>(Ref.p);
111a93a33cbSSean Fertile }
112a93a33cbSSean Fertile 
1139212206dSJason Liu const XCOFFSymbolEntry *XCOFFObjectFile::toSymbolEntry(DataRefImpl Ref) const {
114837ae69fSSean Fertile   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
1159212206dSJason Liu   assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!");
1167c72e82bSJason Liu #ifndef NDEBUG
1177c72e82bSJason Liu   checkSymbolEntryPointer(Ref.p);
1187c72e82bSJason Liu #endif
1199212206dSJason Liu   auto SymEntPtr = viewAs<XCOFFSymbolEntry>(Ref.p);
1209212206dSJason Liu   return SymEntPtr;
1219212206dSJason Liu }
1229212206dSJason Liu 
123837ae69fSSean Fertile const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const {
124837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
125837ae69fSSean Fertile   return static_cast<const XCOFFFileHeader32 *>(FileHeader);
126a93a33cbSSean Fertile }
127a93a33cbSSean Fertile 
128837ae69fSSean Fertile const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const {
129837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
130837ae69fSSean Fertile   return static_cast<const XCOFFFileHeader64 *>(FileHeader);
131a93a33cbSSean Fertile }
132a93a33cbSSean Fertile 
133837ae69fSSean Fertile const XCOFFSectionHeader32 *
134837ae69fSSean Fertile XCOFFObjectFile::sectionHeaderTable32() const {
135837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
136837ae69fSSean Fertile   return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable);
137837ae69fSSean Fertile }
138837ae69fSSean Fertile 
139837ae69fSSean Fertile const XCOFFSectionHeader64 *
140837ae69fSSean Fertile XCOFFObjectFile::sectionHeaderTable64() const {
141837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
142837ae69fSSean Fertile   return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable);
143837ae69fSSean Fertile }
1449212206dSJason Liu 
145ab2eb2bfSHubert Tong void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
1469212206dSJason Liu   const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
1479212206dSJason Liu   SymEntPtr += SymEntPtr->NumberOfAuxEntries + 1;
1487c72e82bSJason Liu #ifndef NDEBUG
1497c72e82bSJason Liu   // This function is used by basic_symbol_iterator, which allows to
1507c72e82bSJason Liu   // point to the end-of-symbol-table address.
1517c72e82bSJason Liu   if (reinterpret_cast<uintptr_t>(SymEntPtr) != getEndOfSymbolTableAddress())
1527c72e82bSJason Liu     checkSymbolEntryPointer(reinterpret_cast<uintptr_t>(SymEntPtr));
1537c72e82bSJason Liu #endif
1549212206dSJason Liu   Symb.p = reinterpret_cast<uintptr_t>(SymEntPtr);
155ab2eb2bfSHubert Tong }
156ab2eb2bfSHubert Tong 
1577c72e82bSJason Liu Expected<StringRef>
1587c72e82bSJason Liu XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
1597c72e82bSJason Liu   // The byte offset is relative to the start of the string table.
1607c72e82bSJason Liu   // A byte offset value of 0 is a null or zero-length symbol
1619212206dSJason Liu   // name. A byte offset in the range 1 to 3 (inclusive) points into the length
1629212206dSJason Liu   // field; as a soft-error recovery mechanism, we treat such cases as having an
1639212206dSJason Liu   // offset of 0.
1649212206dSJason Liu   if (Offset < 4)
1659212206dSJason Liu     return StringRef(nullptr, 0);
1669212206dSJason Liu 
1679212206dSJason Liu   if (StringTable.Data != nullptr && StringTable.Size > Offset)
1689212206dSJason Liu     return (StringTable.Data + Offset);
1699212206dSJason Liu 
1707c72e82bSJason Liu   return make_error<GenericBinaryError>("Bad offset for string table entry",
1719212206dSJason Liu                                         object_error::parse_failed);
172ab2eb2bfSHubert Tong }
173ab2eb2bfSHubert Tong 
1747c72e82bSJason Liu Expected<StringRef>
1757c72e82bSJason Liu XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const {
1767c72e82bSJason Liu   if (CFileEntPtr->NameInStrTbl.Magic !=
1777c72e82bSJason Liu       XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC)
1787c72e82bSJason Liu     return generateXCOFFFixedNameStringRef(CFileEntPtr->Name);
1797c72e82bSJason Liu   return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset);
1807c72e82bSJason Liu }
1817c72e82bSJason Liu 
1827c72e82bSJason Liu Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const {
1837c72e82bSJason Liu   const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
1847c72e82bSJason Liu 
1857c72e82bSJason Liu   // A storage class value with the high-order bit on indicates that the name is
1867c72e82bSJason Liu   // a symbolic debugger stabstring.
1877c72e82bSJason Liu   if (SymEntPtr->StorageClass & 0x80)
1887c72e82bSJason Liu     return StringRef("Unimplemented Debug Name");
1897c72e82bSJason Liu 
1907c72e82bSJason Liu   if (SymEntPtr->NameInStrTbl.Magic != XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC)
1917c72e82bSJason Liu     return generateXCOFFFixedNameStringRef(SymEntPtr->SymbolName);
1927c72e82bSJason Liu 
1937c72e82bSJason Liu   return getStringTableEntry(SymEntPtr->NameInStrTbl.Offset);
1947c72e82bSJason Liu }
1957c72e82bSJason Liu 
196ab2eb2bfSHubert Tong Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
197b91f798fSdiggerlin   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
198b91f798fSdiggerlin   return toSymbolEntry(Symb)->Value;
199ab2eb2bfSHubert Tong }
200ab2eb2bfSHubert Tong 
201ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
2027c72e82bSJason Liu   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
2039212206dSJason Liu   return toSymbolEntry(Symb)->Value;
204ab2eb2bfSHubert Tong }
205ab2eb2bfSHubert Tong 
206ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
207ab2eb2bfSHubert Tong   uint64_t Result = 0;
208ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
209ab2eb2bfSHubert Tong   return Result;
210ab2eb2bfSHubert Tong }
211ab2eb2bfSHubert Tong 
212ab2eb2bfSHubert Tong Expected<SymbolRef::Type>
213ab2eb2bfSHubert Tong XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const {
214ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
215ab2eb2bfSHubert Tong   return SymbolRef::ST_Other;
216ab2eb2bfSHubert Tong }
217ab2eb2bfSHubert Tong 
218ab2eb2bfSHubert Tong Expected<section_iterator>
219ab2eb2bfSHubert Tong XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const {
2209212206dSJason Liu   const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
2219212206dSJason Liu   int16_t SectNum = SymEntPtr->SectionNumber;
2229212206dSJason Liu 
2239212206dSJason Liu   if (isReservedSectionNumber(SectNum))
2249212206dSJason Liu     return section_end();
2259212206dSJason Liu 
226837ae69fSSean Fertile   Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum);
227837ae69fSSean Fertile   if (!ExpSec)
228837ae69fSSean Fertile     return ExpSec.takeError();
2299212206dSJason Liu 
230837ae69fSSean Fertile   return section_iterator(SectionRef(ExpSec.get(), this));
231ab2eb2bfSHubert Tong }
232ab2eb2bfSHubert Tong 
233ab2eb2bfSHubert Tong void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const {
234a93a33cbSSean Fertile   const char *Ptr = reinterpret_cast<const char *>(Sec.p);
235a93a33cbSSean Fertile   Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
236ab2eb2bfSHubert Tong }
237ab2eb2bfSHubert Tong 
2388be28cdcSFangrui Song Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
2397c72e82bSJason Liu   return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec));
240ab2eb2bfSHubert Tong }
241ab2eb2bfSHubert Tong 
242ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
243210314aeSSean Fertile   // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
244210314aeSSean Fertile   // with MSVC.
245210314aeSSean Fertile   if (is64Bit())
246210314aeSSean Fertile     return toSection64(Sec)->VirtualAddress;
247210314aeSSean Fertile 
248210314aeSSean Fertile   return toSection32(Sec)->VirtualAddress;
249ab2eb2bfSHubert Tong }
250ab2eb2bfSHubert Tong 
251ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
252a93a33cbSSean Fertile   // Section numbers in XCOFF are numbered beginning at 1. A section number of
253a93a33cbSSean Fertile   // zero is used to indicate that a symbol is being imported or is undefined.
254837ae69fSSean Fertile   if (is64Bit())
255837ae69fSSean Fertile     return toSection64(Sec) - sectionHeaderTable64() + 1;
256837ae69fSSean Fertile   else
257837ae69fSSean Fertile     return toSection32(Sec) - sectionHeaderTable32() + 1;
258ab2eb2bfSHubert Tong }
259ab2eb2bfSHubert Tong 
260ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const {
261210314aeSSean Fertile   // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
262210314aeSSean Fertile   // with MSVC.
263210314aeSSean Fertile   if (is64Bit())
264210314aeSSean Fertile     return toSection64(Sec)->SectionSize;
265210314aeSSean Fertile 
266210314aeSSean Fertile   return toSection32(Sec)->SectionSize;
267ab2eb2bfSHubert Tong }
268ab2eb2bfSHubert Tong 
269e1cb2c0fSFangrui Song Expected<ArrayRef<uint8_t>>
270e1cb2c0fSFangrui Song XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
271b91f798fSdiggerlin   if (isSectionVirtual(Sec))
272b91f798fSdiggerlin     return ArrayRef<uint8_t>();
273b91f798fSdiggerlin 
274ea13683fSdiggerlin   uint64_t OffsetToRaw;
275ea13683fSdiggerlin   if (is64Bit())
276ea13683fSdiggerlin     OffsetToRaw = toSection64(Sec)->FileOffsetToRawData;
277ea13683fSdiggerlin   else
278ea13683fSdiggerlin     OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
279b91f798fSdiggerlin 
280b91f798fSdiggerlin   const uint8_t * ContentStart = base() + OffsetToRaw;
281b91f798fSdiggerlin   uint64_t SectionSize = getSectionSize(Sec);
282b91f798fSdiggerlin   if (checkOffset(Data, uintptr_t(ContentStart), SectionSize))
283b91f798fSdiggerlin     return make_error<BinaryError>();
284b91f798fSdiggerlin 
285b91f798fSdiggerlin   return makeArrayRef(ContentStart,SectionSize);
286ab2eb2bfSHubert Tong }
287ab2eb2bfSHubert Tong 
288ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
289ab2eb2bfSHubert Tong   uint64_t Result = 0;
290ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
291ab2eb2bfSHubert Tong   return Result;
292ab2eb2bfSHubert Tong }
293ab2eb2bfSHubert Tong 
294ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
295ab2eb2bfSHubert Tong   bool Result = false;
296ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
297ab2eb2bfSHubert Tong   return Result;
298ab2eb2bfSHubert Tong }
299ab2eb2bfSHubert Tong 
300ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const {
301837ae69fSSean Fertile   return getSectionFlags(Sec) & XCOFF::STYP_TEXT;
302ab2eb2bfSHubert Tong }
303ab2eb2bfSHubert Tong 
304ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const {
305837ae69fSSean Fertile   uint32_t Flags = getSectionFlags(Sec);
306a93a33cbSSean Fertile   return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA);
307ab2eb2bfSHubert Tong }
308ab2eb2bfSHubert Tong 
309ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const {
310837ae69fSSean Fertile   uint32_t Flags = getSectionFlags(Sec);
311a93a33cbSSean Fertile   return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS);
312ab2eb2bfSHubert Tong }
313ab2eb2bfSHubert Tong 
314ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
315b91f798fSdiggerlin   return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0
316b91f798fSdiggerlin                    : toSection32(Sec)->FileOffsetToRawData == 0;
317ab2eb2bfSHubert Tong }
318ab2eb2bfSHubert Tong 
319ab2eb2bfSHubert Tong relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
320d60d7d69Sjasonliu   if (is64Bit())
321d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
322d60d7d69Sjasonliu   const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
323d60d7d69Sjasonliu   auto RelocationsOrErr = relocations(*SectionEntPtr);
324d60d7d69Sjasonliu   if (Error E = RelocationsOrErr.takeError())
325ab2eb2bfSHubert Tong     return relocation_iterator(RelocationRef());
326d60d7d69Sjasonliu   DataRefImpl Ret;
327d60d7d69Sjasonliu   Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
328d60d7d69Sjasonliu   return relocation_iterator(RelocationRef(Ret, this));
329ab2eb2bfSHubert Tong }
330ab2eb2bfSHubert Tong 
331ab2eb2bfSHubert Tong relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const {
332d60d7d69Sjasonliu   if (is64Bit())
333d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
334d60d7d69Sjasonliu   const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
335d60d7d69Sjasonliu   auto RelocationsOrErr = relocations(*SectionEntPtr);
336d60d7d69Sjasonliu   if (Error E = RelocationsOrErr.takeError())
337ab2eb2bfSHubert Tong     return relocation_iterator(RelocationRef());
338d60d7d69Sjasonliu   DataRefImpl Ret;
339d60d7d69Sjasonliu   Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
340d60d7d69Sjasonliu   return relocation_iterator(RelocationRef(Ret, this));
341ab2eb2bfSHubert Tong }
342ab2eb2bfSHubert Tong 
343ab2eb2bfSHubert Tong void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
344d60d7d69Sjasonliu   Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1);
345ab2eb2bfSHubert Tong }
346ab2eb2bfSHubert Tong 
347ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
348d60d7d69Sjasonliu   if (is64Bit())
349d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
350d60d7d69Sjasonliu   const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
351d60d7d69Sjasonliu   const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32();
352d60d7d69Sjasonliu   const uint32_t RelocAddress = Reloc->VirtualAddress;
353d60d7d69Sjasonliu   const uint16_t NumberOfSections = getNumberOfSections();
354d60d7d69Sjasonliu   for (uint16_t i = 0; i < NumberOfSections; ++i) {
355d60d7d69Sjasonliu     // Find which section this relocation is belonging to, and get the
356d60d7d69Sjasonliu     // relocation offset relative to the start of the section.
357d60d7d69Sjasonliu     if (Sec32->VirtualAddress <= RelocAddress &&
358d60d7d69Sjasonliu         RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) {
359d60d7d69Sjasonliu       return RelocAddress - Sec32->VirtualAddress;
360d60d7d69Sjasonliu     }
361d60d7d69Sjasonliu     ++Sec32;
362d60d7d69Sjasonliu   }
363d60d7d69Sjasonliu   return InvalidRelocOffset;
364ab2eb2bfSHubert Tong }
365ab2eb2bfSHubert Tong 
366ab2eb2bfSHubert Tong symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
367d60d7d69Sjasonliu   if (is64Bit())
368d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
369d60d7d69Sjasonliu   const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
370d60d7d69Sjasonliu   const uint32_t Index = Reloc->SymbolIndex;
371d60d7d69Sjasonliu 
372d60d7d69Sjasonliu   if (Index >= getLogicalNumberOfSymbolTableEntries32())
373d60d7d69Sjasonliu     return symbol_end();
374d60d7d69Sjasonliu 
375d60d7d69Sjasonliu   DataRefImpl SymDRI;
376d60d7d69Sjasonliu   SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index);
377d60d7d69Sjasonliu   return symbol_iterator(SymbolRef(SymDRI, this));
378ab2eb2bfSHubert Tong }
379ab2eb2bfSHubert Tong 
380ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const {
381d60d7d69Sjasonliu   if (is64Bit())
382d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
383d60d7d69Sjasonliu   return viewAs<XCOFFRelocation32>(Rel.p)->Type;
384ab2eb2bfSHubert Tong }
385ab2eb2bfSHubert Tong 
386ab2eb2bfSHubert Tong void XCOFFObjectFile::getRelocationTypeName(
387ab2eb2bfSHubert Tong     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
388d60d7d69Sjasonliu   if (is64Bit())
389d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
390d60d7d69Sjasonliu   const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
391d60d7d69Sjasonliu   StringRef Res = XCOFF::getRelocationTypeString(Reloc->Type);
392d60d7d69Sjasonliu   Result.append(Res.begin(), Res.end());
393ab2eb2bfSHubert Tong }
394ab2eb2bfSHubert Tong 
395ac00376aSvgxbj Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const {
396ab2eb2bfSHubert Tong   uint32_t Result = 0;
397ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
398ab2eb2bfSHubert Tong   return Result;
399ab2eb2bfSHubert Tong }
400ab2eb2bfSHubert Tong 
401ab2eb2bfSHubert Tong basic_symbol_iterator XCOFFObjectFile::symbol_begin() const {
402d60d7d69Sjasonliu   if (is64Bit())
403d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
4049212206dSJason Liu   DataRefImpl SymDRI;
4059212206dSJason Liu   SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr);
4069212206dSJason Liu   return basic_symbol_iterator(SymbolRef(SymDRI, this));
407ab2eb2bfSHubert Tong }
408ab2eb2bfSHubert Tong 
409ab2eb2bfSHubert Tong basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
410d60d7d69Sjasonliu   if (is64Bit())
411d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
4129212206dSJason Liu   DataRefImpl SymDRI;
4139212206dSJason Liu   SymDRI.p = reinterpret_cast<uintptr_t>(
414837ae69fSSean Fertile       SymbolTblPtr + getLogicalNumberOfSymbolTableEntries32());
4159212206dSJason Liu   return basic_symbol_iterator(SymbolRef(SymDRI, this));
416ab2eb2bfSHubert Tong }
417ab2eb2bfSHubert Tong 
418ab2eb2bfSHubert Tong section_iterator XCOFFObjectFile::section_begin() const {
419a93a33cbSSean Fertile   DataRefImpl DRI;
420837ae69fSSean Fertile   DRI.p = getSectionHeaderTableAddress();
421a93a33cbSSean Fertile   return section_iterator(SectionRef(DRI, this));
422ab2eb2bfSHubert Tong }
423ab2eb2bfSHubert Tong 
424ab2eb2bfSHubert Tong section_iterator XCOFFObjectFile::section_end() const {
425a93a33cbSSean Fertile   DataRefImpl DRI;
426837ae69fSSean Fertile   DRI.p = getWithOffset(getSectionHeaderTableAddress(),
427837ae69fSSean Fertile                         getNumberOfSections() * getSectionHeaderSize());
428a93a33cbSSean Fertile   return section_iterator(SectionRef(DRI, this));
429ab2eb2bfSHubert Tong }
430ab2eb2bfSHubert Tong 
431837ae69fSSean Fertile uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; }
432ab2eb2bfSHubert Tong 
433ab2eb2bfSHubert Tong StringRef XCOFFObjectFile::getFileFormatName() const {
434837ae69fSSean Fertile   return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000";
435ab2eb2bfSHubert Tong }
436ab2eb2bfSHubert Tong 
437ab2eb2bfSHubert Tong Triple::ArchType XCOFFObjectFile::getArch() const {
438837ae69fSSean Fertile   return is64Bit() ? Triple::ppc64 : Triple::ppc;
439ab2eb2bfSHubert Tong }
440ab2eb2bfSHubert Tong 
441ab2eb2bfSHubert Tong SubtargetFeatures XCOFFObjectFile::getFeatures() const {
442ab2eb2bfSHubert Tong   return SubtargetFeatures();
443ab2eb2bfSHubert Tong }
444ab2eb2bfSHubert Tong 
445ab2eb2bfSHubert Tong bool XCOFFObjectFile::isRelocatableObject() const {
446d60d7d69Sjasonliu   if (is64Bit())
447d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
448d60d7d69Sjasonliu   return !(fileHeader32()->Flags & NoRelMask);
449ab2eb2bfSHubert Tong }
450ab2eb2bfSHubert Tong 
451a93a33cbSSean Fertile Expected<uint64_t> XCOFFObjectFile::getStartAddress() const {
452a93a33cbSSean Fertile   // TODO FIXME Should get from auxiliary_header->o_entry when support for the
453a93a33cbSSean Fertile   // auxiliary_header is added.
454a93a33cbSSean Fertile   return 0;
455a93a33cbSSean Fertile }
456a93a33cbSSean Fertile 
457837ae69fSSean Fertile size_t XCOFFObjectFile::getFileHeaderSize() const {
458837ae69fSSean Fertile   return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32);
4599212206dSJason Liu }
4609212206dSJason Liu 
461837ae69fSSean Fertile size_t XCOFFObjectFile::getSectionHeaderSize() const {
462837ae69fSSean Fertile   return is64Bit() ? sizeof(XCOFFSectionHeader64) :
463837ae69fSSean Fertile                      sizeof(XCOFFSectionHeader32);
464837ae69fSSean Fertile }
465837ae69fSSean Fertile 
466837ae69fSSean Fertile bool XCOFFObjectFile::is64Bit() const {
467837ae69fSSean Fertile   return Binary::ID_XCOFF64 == getType();
468837ae69fSSean Fertile }
469837ae69fSSean Fertile 
470837ae69fSSean Fertile uint16_t XCOFFObjectFile::getMagic() const {
471837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic;
472837ae69fSSean Fertile }
473837ae69fSSean Fertile 
474837ae69fSSean Fertile Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const {
475837ae69fSSean Fertile   if (Num <= 0 || Num > getNumberOfSections())
476837ae69fSSean Fertile     return errorCodeToError(object_error::invalid_section_index);
477837ae69fSSean Fertile 
478837ae69fSSean Fertile   DataRefImpl DRI;
479837ae69fSSean Fertile   DRI.p = getWithOffset(getSectionHeaderTableAddress(),
480837ae69fSSean Fertile                         getSectionHeaderSize() * (Num - 1));
481837ae69fSSean Fertile   return DRI;
4829212206dSJason Liu }
4839212206dSJason Liu 
4849212206dSJason Liu Expected<StringRef>
4859212206dSJason Liu XCOFFObjectFile::getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const {
486837ae69fSSean Fertile   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
4879212206dSJason Liu   int16_t SectionNum = SymEntPtr->SectionNumber;
4889212206dSJason Liu 
4899212206dSJason Liu   switch (SectionNum) {
4909212206dSJason Liu   case XCOFF::N_DEBUG:
4919212206dSJason Liu     return "N_DEBUG";
4929212206dSJason Liu   case XCOFF::N_ABS:
4939212206dSJason Liu     return "N_ABS";
4949212206dSJason Liu   case XCOFF::N_UNDEF:
4959212206dSJason Liu     return "N_UNDEF";
496837ae69fSSean Fertile   default:
497837ae69fSSean Fertile     Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum);
498837ae69fSSean Fertile     if (SecRef)
4997c72e82bSJason Liu       return generateXCOFFFixedNameStringRef(
5007c72e82bSJason Liu           getSectionNameInternal(SecRef.get()));
501837ae69fSSean Fertile     return SecRef.takeError();
5029212206dSJason Liu   }
5039212206dSJason Liu }
5049212206dSJason Liu 
5059212206dSJason Liu bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
5069212206dSJason Liu   return (SectionNumber <= 0 && SectionNumber >= -2);
5079212206dSJason Liu }
5089212206dSJason Liu 
5099212206dSJason Liu uint16_t XCOFFObjectFile::getNumberOfSections() const {
510837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->NumberOfSections
511837ae69fSSean Fertile                    : fileHeader32()->NumberOfSections;
5129212206dSJason Liu }
5139212206dSJason Liu 
514837ae69fSSean Fertile int32_t XCOFFObjectFile::getTimeStamp() const {
515837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp;
5169212206dSJason Liu }
5179212206dSJason Liu 
5189212206dSJason Liu uint16_t XCOFFObjectFile::getOptionalHeaderSize() const {
519837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->AuxHeaderSize
520837ae69fSSean Fertile                    : fileHeader32()->AuxHeaderSize;
5219212206dSJason Liu }
5229212206dSJason Liu 
523837ae69fSSean Fertile uint32_t XCOFFObjectFile::getSymbolTableOffset32() const {
524837ae69fSSean Fertile   return fileHeader32()->SymbolTableOffset;
525837ae69fSSean Fertile }
526ab2eb2bfSHubert Tong 
527837ae69fSSean Fertile int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const {
528837ae69fSSean Fertile   // As far as symbol table size is concerned, if this field is negative it is
529837ae69fSSean Fertile   // to be treated as a 0. However since this field is also used for printing we
530837ae69fSSean Fertile   // don't want to truncate any negative values.
531837ae69fSSean Fertile   return fileHeader32()->NumberOfSymTableEntries;
532837ae69fSSean Fertile }
533ab2eb2bfSHubert Tong 
534837ae69fSSean Fertile uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const {
535837ae69fSSean Fertile   return (fileHeader32()->NumberOfSymTableEntries >= 0
536837ae69fSSean Fertile               ? fileHeader32()->NumberOfSymTableEntries
537837ae69fSSean Fertile               : 0);
538837ae69fSSean Fertile }
539a93a33cbSSean Fertile 
540837ae69fSSean Fertile uint64_t XCOFFObjectFile::getSymbolTableOffset64() const {
541837ae69fSSean Fertile   return fileHeader64()->SymbolTableOffset;
542837ae69fSSean Fertile }
543837ae69fSSean Fertile 
544837ae69fSSean Fertile uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const {
545837ae69fSSean Fertile   return fileHeader64()->NumberOfSymTableEntries;
546837ae69fSSean Fertile }
547837ae69fSSean Fertile 
5487c72e82bSJason Liu uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const {
5497c72e82bSJason Liu   uint32_t NumberOfSymTableEntries =
5507c72e82bSJason Liu       is64Bit() ? getNumberOfSymbolTableEntries64()
5517c72e82bSJason Liu                 : getLogicalNumberOfSymbolTableEntries32();
5527c72e82bSJason Liu   return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr),
5537c72e82bSJason Liu                        XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries);
5547c72e82bSJason Liu }
5557c72e82bSJason Liu 
5567c72e82bSJason Liu void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const {
5577c72e82bSJason Liu   if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr))
5587c72e82bSJason Liu     report_fatal_error("Symbol table entry is outside of symbol table.");
5597c72e82bSJason Liu 
5607c72e82bSJason Liu   if (SymbolEntPtr >= getEndOfSymbolTableAddress())
5617c72e82bSJason Liu     report_fatal_error("Symbol table entry is outside of symbol table.");
5627c72e82bSJason Liu 
5637c72e82bSJason Liu   ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) -
5647c72e82bSJason Liu                      reinterpret_cast<const char *>(SymbolTblPtr);
5657c72e82bSJason Liu 
5667c72e82bSJason Liu   if (Offset % XCOFF::SymbolTableEntrySize != 0)
5677c72e82bSJason Liu     report_fatal_error(
5687c72e82bSJason Liu         "Symbol table entry position is not valid inside of symbol table.");
5697c72e82bSJason Liu }
5707c72e82bSJason Liu 
5717c72e82bSJason Liu uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const {
5727c72e82bSJason Liu   return (reinterpret_cast<const char *>(SymbolEntPtr) -
5737c72e82bSJason Liu           reinterpret_cast<const char *>(SymbolTblPtr)) /
5747c72e82bSJason Liu          XCOFF::SymbolTableEntrySize;
5757c72e82bSJason Liu }
5767c72e82bSJason Liu 
57734d4bff3SDigger Lin Expected<StringRef>
57834d4bff3SDigger Lin XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const {
57934d4bff3SDigger Lin   if (is64Bit())
58034d4bff3SDigger Lin     report_fatal_error("64-bit symbol table support not implemented yet.");
58134d4bff3SDigger Lin 
58234d4bff3SDigger Lin   if (Index >= getLogicalNumberOfSymbolTableEntries32())
58334d4bff3SDigger Lin     return errorCodeToError(object_error::invalid_symbol_index);
58434d4bff3SDigger Lin 
58534d4bff3SDigger Lin   DataRefImpl SymDRI;
58634d4bff3SDigger Lin   SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index);
58734d4bff3SDigger Lin   return getSymbolName(SymDRI);
58834d4bff3SDigger Lin }
58934d4bff3SDigger Lin 
590837ae69fSSean Fertile uint16_t XCOFFObjectFile::getFlags() const {
591837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags;
592837ae69fSSean Fertile }
593837ae69fSSean Fertile 
594837ae69fSSean Fertile const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const {
595837ae69fSSean Fertile   return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name;
596837ae69fSSean Fertile }
597837ae69fSSean Fertile 
598837ae69fSSean Fertile uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const {
599837ae69fSSean Fertile   return reinterpret_cast<uintptr_t>(SectionHeaderTable);
600837ae69fSSean Fertile }
601837ae69fSSean Fertile 
602837ae69fSSean Fertile int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const {
603837ae69fSSean Fertile   return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags;
604837ae69fSSean Fertile }
605837ae69fSSean Fertile 
606837ae69fSSean Fertile XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object)
607837ae69fSSean Fertile     : ObjectFile(Type, Object) {
608837ae69fSSean Fertile   assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64);
609837ae69fSSean Fertile }
610837ae69fSSean Fertile 
611837ae69fSSean Fertile ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const {
612837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called for non 64-bit file.");
613837ae69fSSean Fertile   const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64();
614837ae69fSSean Fertile   return ArrayRef<XCOFFSectionHeader64>(TablePtr,
615837ae69fSSean Fertile                                         TablePtr + getNumberOfSections());
616837ae69fSSean Fertile }
617837ae69fSSean Fertile 
618837ae69fSSean Fertile ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const {
619837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called for non 32-bit file.");
620837ae69fSSean Fertile   const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32();
621837ae69fSSean Fertile   return ArrayRef<XCOFFSectionHeader32>(TablePtr,
622837ae69fSSean Fertile                                         TablePtr + getNumberOfSections());
623837ae69fSSean Fertile }
624837ae69fSSean Fertile 
62534d4bff3SDigger Lin // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO
62634d4bff3SDigger Lin // section header contains the actual count of relocation entries in the s_paddr
62734d4bff3SDigger Lin // field. STYP_OVRFLO headers contain the section index of their corresponding
62834d4bff3SDigger Lin // sections as their raw "NumberOfRelocations" field value.
62934d4bff3SDigger Lin Expected<uint32_t> XCOFFObjectFile::getLogicalNumberOfRelocationEntries(
63034d4bff3SDigger Lin     const XCOFFSectionHeader32 &Sec) const {
63134d4bff3SDigger Lin 
63234d4bff3SDigger Lin   uint16_t SectionIndex = &Sec - sectionHeaderTable32() + 1;
63334d4bff3SDigger Lin 
634*775ef445Sjasonliu   if (Sec.NumberOfRelocations < XCOFF::RelocOverflow)
63534d4bff3SDigger Lin     return Sec.NumberOfRelocations;
63634d4bff3SDigger Lin   for (const auto &Sec : sections32()) {
63734d4bff3SDigger Lin     if (Sec.Flags == XCOFF::STYP_OVRFLO &&
63834d4bff3SDigger Lin         Sec.NumberOfRelocations == SectionIndex)
63934d4bff3SDigger Lin       return Sec.PhysicalAddress;
64034d4bff3SDigger Lin   }
64134d4bff3SDigger Lin   return errorCodeToError(object_error::parse_failed);
64234d4bff3SDigger Lin }
64334d4bff3SDigger Lin 
64434d4bff3SDigger Lin Expected<ArrayRef<XCOFFRelocation32>>
64534d4bff3SDigger Lin XCOFFObjectFile::relocations(const XCOFFSectionHeader32 &Sec) const {
64634d4bff3SDigger Lin   uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader),
64734d4bff3SDigger Lin                                       Sec.FileOffsetToRelocationInfo);
64834d4bff3SDigger Lin   auto NumRelocEntriesOrErr = getLogicalNumberOfRelocationEntries(Sec);
64934d4bff3SDigger Lin   if (Error E = NumRelocEntriesOrErr.takeError())
650c55cf4afSBill Wendling     return std::move(E);
65134d4bff3SDigger Lin 
65234d4bff3SDigger Lin   uint32_t NumRelocEntries = NumRelocEntriesOrErr.get();
65334d4bff3SDigger Lin 
6543bbe7a68Sjasonliu   assert(sizeof(XCOFFRelocation32) == XCOFF::RelocationSerializationSize32);
65534d4bff3SDigger Lin   auto RelocationOrErr =
65634d4bff3SDigger Lin       getObject<XCOFFRelocation32>(Data, reinterpret_cast<void *>(RelocAddr),
65734d4bff3SDigger Lin                                    NumRelocEntries * sizeof(XCOFFRelocation32));
65834d4bff3SDigger Lin   if (Error E = RelocationOrErr.takeError())
659c55cf4afSBill Wendling     return std::move(E);
66034d4bff3SDigger Lin 
66134d4bff3SDigger Lin   const XCOFFRelocation32 *StartReloc = RelocationOrErr.get();
66234d4bff3SDigger Lin 
66334d4bff3SDigger Lin   return ArrayRef<XCOFFRelocation32>(StartReloc, StartReloc + NumRelocEntries);
66434d4bff3SDigger Lin }
66534d4bff3SDigger Lin 
666837ae69fSSean Fertile Expected<XCOFFStringTable>
667837ae69fSSean Fertile XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) {
668837ae69fSSean Fertile   // If there is a string table, then the buffer must contain at least 4 bytes
669837ae69fSSean Fertile   // for the string table's size. Not having a string table is not an error.
670e03a135bSReid Kleckner   if (Error E = Binary::checkOffset(
671e03a135bSReid Kleckner           Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) {
672e03a135bSReid Kleckner     consumeError(std::move(E));
673837ae69fSSean Fertile     return XCOFFStringTable{0, nullptr};
674e03a135bSReid Kleckner   }
675837ae69fSSean Fertile 
676837ae69fSSean Fertile   // Read the size out of the buffer.
677837ae69fSSean Fertile   uint32_t Size = support::endian::read32be(Obj->base() + Offset);
678837ae69fSSean Fertile 
679837ae69fSSean Fertile   // If the size is less then 4, then the string table is just a size and no
680837ae69fSSean Fertile   // string data.
681837ae69fSSean Fertile   if (Size <= 4)
682837ae69fSSean Fertile     return XCOFFStringTable{4, nullptr};
683837ae69fSSean Fertile 
684837ae69fSSean Fertile   auto StringTableOrErr =
685837ae69fSSean Fertile       getObject<char>(Obj->Data, Obj->base() + Offset, Size);
686837ae69fSSean Fertile   if (Error E = StringTableOrErr.takeError())
687c55cf4afSBill Wendling     return std::move(E);
688837ae69fSSean Fertile 
689837ae69fSSean Fertile   const char *StringTablePtr = StringTableOrErr.get();
690837ae69fSSean Fertile   if (StringTablePtr[Size - 1] != '\0')
691837ae69fSSean Fertile     return errorCodeToError(object_error::string_table_non_null_end);
692837ae69fSSean Fertile 
693837ae69fSSean Fertile   return XCOFFStringTable{Size, StringTablePtr};
694837ae69fSSean Fertile }
695837ae69fSSean Fertile 
696837ae69fSSean Fertile Expected<std::unique_ptr<XCOFFObjectFile>>
697837ae69fSSean Fertile XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) {
6980eaee545SJonas Devlieghere   // Can't use std::make_unique because of the private constructor.
699837ae69fSSean Fertile   std::unique_ptr<XCOFFObjectFile> Obj;
700837ae69fSSean Fertile   Obj.reset(new XCOFFObjectFile(Type, MBR));
701837ae69fSSean Fertile 
702837ae69fSSean Fertile   uint64_t CurOffset = 0;
703837ae69fSSean Fertile   const auto *Base = Obj->base();
704837ae69fSSean Fertile   MemoryBufferRef Data = Obj->Data;
705837ae69fSSean Fertile 
706837ae69fSSean Fertile   // Parse file header.
707837ae69fSSean Fertile   auto FileHeaderOrErr =
708837ae69fSSean Fertile       getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize());
709837ae69fSSean Fertile   if (Error E = FileHeaderOrErr.takeError())
710c55cf4afSBill Wendling     return std::move(E);
711837ae69fSSean Fertile   Obj->FileHeader = FileHeaderOrErr.get();
712837ae69fSSean Fertile 
713837ae69fSSean Fertile   CurOffset += Obj->getFileHeaderSize();
714a93a33cbSSean Fertile   // TODO FIXME we don't have support for an optional header yet, so just skip
715a93a33cbSSean Fertile   // past it.
716837ae69fSSean Fertile   CurOffset += Obj->getOptionalHeaderSize();
717a93a33cbSSean Fertile 
718837ae69fSSean Fertile   // Parse the section header table if it is present.
719837ae69fSSean Fertile   if (Obj->getNumberOfSections()) {
720837ae69fSSean Fertile     auto SecHeadersOrErr = getObject<void>(Data, Base + CurOffset,
721837ae69fSSean Fertile                                            Obj->getNumberOfSections() *
722837ae69fSSean Fertile                                                Obj->getSectionHeaderSize());
723837ae69fSSean Fertile     if (Error E = SecHeadersOrErr.takeError())
724c55cf4afSBill Wendling       return std::move(E);
725837ae69fSSean Fertile     Obj->SectionHeaderTable = SecHeadersOrErr.get();
726a93a33cbSSean Fertile   }
727ab2eb2bfSHubert Tong 
728837ae69fSSean Fertile   // 64-bit object supports only file header and section headers for now.
729837ae69fSSean Fertile   if (Obj->is64Bit())
730c55cf4afSBill Wendling     return std::move(Obj);
731fd75ee91SSean Fertile 
732837ae69fSSean Fertile   // If there is no symbol table we are done parsing the memory buffer.
733837ae69fSSean Fertile   if (Obj->getLogicalNumberOfSymbolTableEntries32() == 0)
734c55cf4afSBill Wendling     return std::move(Obj);
735837ae69fSSean Fertile 
736837ae69fSSean Fertile   // Parse symbol table.
737837ae69fSSean Fertile   CurOffset = Obj->fileHeader32()->SymbolTableOffset;
7389212206dSJason Liu   uint64_t SymbolTableSize = (uint64_t)(sizeof(XCOFFSymbolEntry)) *
739837ae69fSSean Fertile                              Obj->getLogicalNumberOfSymbolTableEntries32();
740837ae69fSSean Fertile   auto SymTableOrErr =
741837ae69fSSean Fertile       getObject<XCOFFSymbolEntry>(Data, Base + CurOffset, SymbolTableSize);
742837ae69fSSean Fertile   if (Error E = SymTableOrErr.takeError())
743c55cf4afSBill Wendling     return std::move(E);
744837ae69fSSean Fertile   Obj->SymbolTblPtr = SymTableOrErr.get();
745837ae69fSSean Fertile   CurOffset += SymbolTableSize;
746fd75ee91SSean Fertile 
747837ae69fSSean Fertile   // Parse String table.
748837ae69fSSean Fertile   Expected<XCOFFStringTable> StringTableOrErr =
749837ae69fSSean Fertile       parseStringTable(Obj.get(), CurOffset);
750837ae69fSSean Fertile   if (Error E = StringTableOrErr.takeError())
751c55cf4afSBill Wendling     return std::move(E);
752837ae69fSSean Fertile   Obj->StringTable = StringTableOrErr.get();
753fd75ee91SSean Fertile 
754c55cf4afSBill Wendling   return std::move(Obj);
755fd75ee91SSean Fertile }
756fd75ee91SSean Fertile 
757ab2eb2bfSHubert Tong Expected<std::unique_ptr<ObjectFile>>
758837ae69fSSean Fertile ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef,
759837ae69fSSean Fertile                                   unsigned FileType) {
760837ae69fSSean Fertile   return XCOFFObjectFile::create(FileType, MemBufRef);
761ab2eb2bfSHubert Tong }
762ab2eb2bfSHubert Tong 
7637c72e82bSJason Liu XCOFF::StorageClass XCOFFSymbolRef::getStorageClass() const {
7647c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->StorageClass;
7657c72e82bSJason Liu }
7667c72e82bSJason Liu 
7677c72e82bSJason Liu uint8_t XCOFFSymbolRef::getNumberOfAuxEntries() const {
7687c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->NumberOfAuxEntries;
7697c72e82bSJason Liu }
7707c72e82bSJason Liu 
771a26a441bSdiggerlin // TODO: The function needs to return an error if there is no csect auxiliary
772a26a441bSdiggerlin // entry.
7737c72e82bSJason Liu const XCOFFCsectAuxEnt32 *XCOFFSymbolRef::getXCOFFCsectAuxEnt32() const {
7747c72e82bSJason Liu   assert(!OwningObjectPtr->is64Bit() &&
7757c72e82bSJason Liu          "32-bit interface called on 64-bit object file.");
7767c72e82bSJason Liu   assert(hasCsectAuxEnt() && "No Csect Auxiliary Entry is found.");
7777c72e82bSJason Liu 
7787c72e82bSJason Liu   // In XCOFF32, the csect auxilliary entry is always the last auxiliary
7797c72e82bSJason Liu   // entry for the symbol.
7807c72e82bSJason Liu   uintptr_t AuxAddr = getWithOffset(
7817c72e82bSJason Liu       SymEntDataRef.p, XCOFF::SymbolTableEntrySize * getNumberOfAuxEntries());
7827c72e82bSJason Liu 
7837c72e82bSJason Liu #ifndef NDEBUG
7847c72e82bSJason Liu   OwningObjectPtr->checkSymbolEntryPointer(AuxAddr);
7857c72e82bSJason Liu #endif
7867c72e82bSJason Liu 
7877c72e82bSJason Liu   return reinterpret_cast<const XCOFFCsectAuxEnt32 *>(AuxAddr);
7887c72e82bSJason Liu }
7897c72e82bSJason Liu 
7907c72e82bSJason Liu uint16_t XCOFFSymbolRef::getType() const {
7917c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SymbolType;
7927c72e82bSJason Liu }
7937c72e82bSJason Liu 
7947c72e82bSJason Liu int16_t XCOFFSymbolRef::getSectionNumber() const {
7957c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SectionNumber;
7967c72e82bSJason Liu }
7977c72e82bSJason Liu 
798a26a441bSdiggerlin // TODO: The function name needs to be changed to express the purpose of the
799a26a441bSdiggerlin // function.
8007c72e82bSJason Liu bool XCOFFSymbolRef::hasCsectAuxEnt() const {
8017c72e82bSJason Liu   XCOFF::StorageClass SC = getStorageClass();
8027c72e82bSJason Liu   return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT ||
8037c72e82bSJason Liu           SC == XCOFF::C_HIDEXT);
8047c72e82bSJason Liu }
8057c72e82bSJason Liu 
8067c72e82bSJason Liu bool XCOFFSymbolRef::isFunction() const {
8077c72e82bSJason Liu   if (OwningObjectPtr->is64Bit())
8087c72e82bSJason Liu     report_fatal_error("64-bit support is unimplemented yet.");
8097c72e82bSJason Liu 
810d60d7d69Sjasonliu   if (getType() & FunctionSym)
8117c72e82bSJason Liu     return true;
8127c72e82bSJason Liu 
8137c72e82bSJason Liu   if (!hasCsectAuxEnt())
8147c72e82bSJason Liu     return false;
8157c72e82bSJason Liu 
8167c72e82bSJason Liu   const XCOFFCsectAuxEnt32 *CsectAuxEnt = getXCOFFCsectAuxEnt32();
8177c72e82bSJason Liu 
8187c72e82bSJason Liu   // A function definition should be a label definition.
819d60d7d69Sjasonliu   if ((CsectAuxEnt->SymbolAlignmentAndType & SymTypeMask) != XCOFF::XTY_LD)
8207c72e82bSJason Liu     return false;
8217c72e82bSJason Liu 
8227c72e82bSJason Liu   if (CsectAuxEnt->StorageMappingClass != XCOFF::XMC_PR)
8237c72e82bSJason Liu     return false;
8247c72e82bSJason Liu 
8257c72e82bSJason Liu   int16_t SectNum = getSectionNumber();
8267c72e82bSJason Liu   Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum);
8277c72e82bSJason Liu   if (!SI)
8287c72e82bSJason Liu     return false;
8297c72e82bSJason Liu 
8307c72e82bSJason Liu   return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT);
831ab2eb2bfSHubert Tong }
832ab2eb2bfSHubert Tong 
833f8622543SFangrui Song // Explictly instantiate template classes.
834f8622543SFangrui Song template struct XCOFFSectionHeader<XCOFFSectionHeader32>;
835f8622543SFangrui Song template struct XCOFFSectionHeader<XCOFFSectionHeader64>;
836f8622543SFangrui Song 
837ab2eb2bfSHubert Tong } // namespace object
838ab2eb2bfSHubert Tong } // namespace llvm
839