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 RelocOverflow = 65535;
24d60d7d69Sjasonliu static const uint16_t NoRelMask = 0x0001;
257c72e82bSJason Liu 
26837ae69fSSean Fertile // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer
27837ae69fSSean Fertile // 'M'. Returns a pointer to the underlying object on success.
28ab2eb2bfSHubert Tong template <typename T>
29837ae69fSSean Fertile static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
30ab2eb2bfSHubert Tong                                      const uint64_t Size = sizeof(T)) {
31ab2eb2bfSHubert Tong   uintptr_t Addr = uintptr_t(Ptr);
32*e03a135bSReid Kleckner   if (Error E = Binary::checkOffset(M, Addr, Size))
33*e03a135bSReid Kleckner     return std::move(E);
34837ae69fSSean Fertile   return reinterpret_cast<const T *>(Addr);
35837ae69fSSean Fertile }
36837ae69fSSean Fertile 
37837ae69fSSean Fertile static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) {
38837ae69fSSean Fertile   return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) +
39837ae69fSSean Fertile                                      Offset);
40ab2eb2bfSHubert Tong }
41ab2eb2bfSHubert Tong 
42a93a33cbSSean Fertile template <typename T> static const T *viewAs(uintptr_t in) {
43a93a33cbSSean Fertile   return reinterpret_cast<const T *>(in);
44a93a33cbSSean Fertile }
45a93a33cbSSean Fertile 
467c72e82bSJason Liu static StringRef generateXCOFFFixedNameStringRef(const char *Name) {
4729141da7SSean Fertile   auto NulCharPtr =
4829141da7SSean Fertile       static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize));
499212206dSJason Liu   return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
5029141da7SSean Fertile                     : StringRef(Name, XCOFF::NameSize);
519212206dSJason Liu }
529212206dSJason Liu 
53c63c1a72Sdiggerlin template <typename T> StringRef XCOFFSectionHeader<T>::getName() const {
54c63c1a72Sdiggerlin   const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
55c63c1a72Sdiggerlin   return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name);
56c63c1a72Sdiggerlin }
57c63c1a72Sdiggerlin 
58c63c1a72Sdiggerlin template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const {
59c63c1a72Sdiggerlin   const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
60c63c1a72Sdiggerlin   return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask;
61c63c1a72Sdiggerlin }
62c63c1a72Sdiggerlin 
63c63c1a72Sdiggerlin template <typename T>
64c63c1a72Sdiggerlin bool XCOFFSectionHeader<T>::isReservedSectionType() const {
65c63c1a72Sdiggerlin   return getSectionType() & SectionFlagsReservedMask;
66c63c1a72Sdiggerlin }
67c63c1a72Sdiggerlin 
6834d4bff3SDigger Lin bool XCOFFRelocation32::isRelocationSigned() const {
6934d4bff3SDigger Lin   return Info & XR_SIGN_INDICATOR_MASK;
7034d4bff3SDigger Lin }
7134d4bff3SDigger Lin 
7234d4bff3SDigger Lin bool XCOFFRelocation32::isFixupIndicated() const {
7334d4bff3SDigger Lin   return Info & XR_FIXUP_INDICATOR_MASK;
7434d4bff3SDigger Lin }
7534d4bff3SDigger Lin 
7634d4bff3SDigger Lin uint8_t XCOFFRelocation32::getRelocatedLength() const {
7734d4bff3SDigger Lin   // The relocation encodes the bit length being relocated minus 1. Add back
7834d4bff3SDigger Lin   // the 1 to get the actual length being relocated.
7934d4bff3SDigger Lin   return (Info & XR_BIASED_LENGTH_MASK) + 1;
8034d4bff3SDigger Lin }
8134d4bff3SDigger Lin 
82837ae69fSSean Fertile void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr,
83837ae69fSSean Fertile                                           uintptr_t TableAddress) const {
84837ae69fSSean Fertile   if (Addr < TableAddress)
85a93a33cbSSean Fertile     report_fatal_error("Section header outside of section header table.");
86a93a33cbSSean Fertile 
87837ae69fSSean Fertile   uintptr_t Offset = Addr - TableAddress;
88837ae69fSSean Fertile   if (Offset >= getSectionHeaderSize() * getNumberOfSections())
89837ae69fSSean Fertile     report_fatal_error("Section header outside of section header table.");
90837ae69fSSean Fertile 
91a93a33cbSSean Fertile   if (Offset % getSectionHeaderSize() != 0)
92a93a33cbSSean Fertile     report_fatal_error(
93a93a33cbSSean Fertile         "Section header pointer does not point to a valid section header.");
94837ae69fSSean Fertile }
95837ae69fSSean Fertile 
96837ae69fSSean Fertile const XCOFFSectionHeader32 *
97837ae69fSSean Fertile XCOFFObjectFile::toSection32(DataRefImpl Ref) const {
98837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
99837ae69fSSean Fertile #ifndef NDEBUG
100837ae69fSSean Fertile   checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
101a93a33cbSSean Fertile #endif
102837ae69fSSean Fertile   return viewAs<XCOFFSectionHeader32>(Ref.p);
103837ae69fSSean Fertile }
104837ae69fSSean Fertile 
105837ae69fSSean Fertile const XCOFFSectionHeader64 *
106837ae69fSSean Fertile XCOFFObjectFile::toSection64(DataRefImpl Ref) const {
107837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
108837ae69fSSean Fertile #ifndef NDEBUG
109837ae69fSSean Fertile   checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
110837ae69fSSean Fertile #endif
111837ae69fSSean Fertile   return viewAs<XCOFFSectionHeader64>(Ref.p);
112a93a33cbSSean Fertile }
113a93a33cbSSean Fertile 
1149212206dSJason Liu const XCOFFSymbolEntry *XCOFFObjectFile::toSymbolEntry(DataRefImpl Ref) const {
115837ae69fSSean Fertile   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
1169212206dSJason Liu   assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!");
1177c72e82bSJason Liu #ifndef NDEBUG
1187c72e82bSJason Liu   checkSymbolEntryPointer(Ref.p);
1197c72e82bSJason Liu #endif
1209212206dSJason Liu   auto SymEntPtr = viewAs<XCOFFSymbolEntry>(Ref.p);
1219212206dSJason Liu   return SymEntPtr;
1229212206dSJason Liu }
1239212206dSJason Liu 
124837ae69fSSean Fertile const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const {
125837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
126837ae69fSSean Fertile   return static_cast<const XCOFFFileHeader32 *>(FileHeader);
127a93a33cbSSean Fertile }
128a93a33cbSSean Fertile 
129837ae69fSSean Fertile const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const {
130837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
131837ae69fSSean Fertile   return static_cast<const XCOFFFileHeader64 *>(FileHeader);
132a93a33cbSSean Fertile }
133a93a33cbSSean Fertile 
134837ae69fSSean Fertile const XCOFFSectionHeader32 *
135837ae69fSSean Fertile XCOFFObjectFile::sectionHeaderTable32() const {
136837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
137837ae69fSSean Fertile   return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable);
138837ae69fSSean Fertile }
139837ae69fSSean Fertile 
140837ae69fSSean Fertile const XCOFFSectionHeader64 *
141837ae69fSSean Fertile XCOFFObjectFile::sectionHeaderTable64() const {
142837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
143837ae69fSSean Fertile   return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable);
144837ae69fSSean Fertile }
1459212206dSJason Liu 
146ab2eb2bfSHubert Tong void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
1479212206dSJason Liu   const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
1489212206dSJason Liu   SymEntPtr += SymEntPtr->NumberOfAuxEntries + 1;
1497c72e82bSJason Liu #ifndef NDEBUG
1507c72e82bSJason Liu   // This function is used by basic_symbol_iterator, which allows to
1517c72e82bSJason Liu   // point to the end-of-symbol-table address.
1527c72e82bSJason Liu   if (reinterpret_cast<uintptr_t>(SymEntPtr) != getEndOfSymbolTableAddress())
1537c72e82bSJason Liu     checkSymbolEntryPointer(reinterpret_cast<uintptr_t>(SymEntPtr));
1547c72e82bSJason Liu #endif
1559212206dSJason Liu   Symb.p = reinterpret_cast<uintptr_t>(SymEntPtr);
156ab2eb2bfSHubert Tong }
157ab2eb2bfSHubert Tong 
1587c72e82bSJason Liu Expected<StringRef>
1597c72e82bSJason Liu XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
1607c72e82bSJason Liu   // The byte offset is relative to the start of the string table.
1617c72e82bSJason Liu   // A byte offset value of 0 is a null or zero-length symbol
1629212206dSJason Liu   // name. A byte offset in the range 1 to 3 (inclusive) points into the length
1639212206dSJason Liu   // field; as a soft-error recovery mechanism, we treat such cases as having an
1649212206dSJason Liu   // offset of 0.
1659212206dSJason Liu   if (Offset < 4)
1669212206dSJason Liu     return StringRef(nullptr, 0);
1679212206dSJason Liu 
1689212206dSJason Liu   if (StringTable.Data != nullptr && StringTable.Size > Offset)
1699212206dSJason Liu     return (StringTable.Data + Offset);
1709212206dSJason Liu 
1717c72e82bSJason Liu   return make_error<GenericBinaryError>("Bad offset for string table entry",
1729212206dSJason Liu                                         object_error::parse_failed);
173ab2eb2bfSHubert Tong }
174ab2eb2bfSHubert Tong 
1757c72e82bSJason Liu Expected<StringRef>
1767c72e82bSJason Liu XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const {
1777c72e82bSJason Liu   if (CFileEntPtr->NameInStrTbl.Magic !=
1787c72e82bSJason Liu       XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC)
1797c72e82bSJason Liu     return generateXCOFFFixedNameStringRef(CFileEntPtr->Name);
1807c72e82bSJason Liu   return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset);
1817c72e82bSJason Liu }
1827c72e82bSJason Liu 
1837c72e82bSJason Liu Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const {
1847c72e82bSJason Liu   const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
1857c72e82bSJason Liu 
1867c72e82bSJason Liu   // A storage class value with the high-order bit on indicates that the name is
1877c72e82bSJason Liu   // a symbolic debugger stabstring.
1887c72e82bSJason Liu   if (SymEntPtr->StorageClass & 0x80)
1897c72e82bSJason Liu     return StringRef("Unimplemented Debug Name");
1907c72e82bSJason Liu 
1917c72e82bSJason Liu   if (SymEntPtr->NameInStrTbl.Magic != XCOFFSymbolEntry::NAME_IN_STR_TBL_MAGIC)
1927c72e82bSJason Liu     return generateXCOFFFixedNameStringRef(SymEntPtr->SymbolName);
1937c72e82bSJason Liu 
1947c72e82bSJason Liu   return getStringTableEntry(SymEntPtr->NameInStrTbl.Offset);
1957c72e82bSJason Liu }
1967c72e82bSJason Liu 
197ab2eb2bfSHubert Tong Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
198b91f798fSdiggerlin   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
199b91f798fSdiggerlin   return toSymbolEntry(Symb)->Value;
200ab2eb2bfSHubert Tong }
201ab2eb2bfSHubert Tong 
202ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
2037c72e82bSJason Liu   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
2049212206dSJason Liu   return toSymbolEntry(Symb)->Value;
205ab2eb2bfSHubert Tong }
206ab2eb2bfSHubert Tong 
207ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
208ab2eb2bfSHubert Tong   uint64_t Result = 0;
209ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
210ab2eb2bfSHubert Tong   return Result;
211ab2eb2bfSHubert Tong }
212ab2eb2bfSHubert Tong 
213ab2eb2bfSHubert Tong Expected<SymbolRef::Type>
214ab2eb2bfSHubert Tong XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const {
215ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
216ab2eb2bfSHubert Tong   return SymbolRef::ST_Other;
217ab2eb2bfSHubert Tong }
218ab2eb2bfSHubert Tong 
219ab2eb2bfSHubert Tong Expected<section_iterator>
220ab2eb2bfSHubert Tong XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const {
2219212206dSJason Liu   const XCOFFSymbolEntry *SymEntPtr = toSymbolEntry(Symb);
2229212206dSJason Liu   int16_t SectNum = SymEntPtr->SectionNumber;
2239212206dSJason Liu 
2249212206dSJason Liu   if (isReservedSectionNumber(SectNum))
2259212206dSJason Liu     return section_end();
2269212206dSJason Liu 
227837ae69fSSean Fertile   Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum);
228837ae69fSSean Fertile   if (!ExpSec)
229837ae69fSSean Fertile     return ExpSec.takeError();
2309212206dSJason Liu 
231837ae69fSSean Fertile   return section_iterator(SectionRef(ExpSec.get(), this));
232ab2eb2bfSHubert Tong }
233ab2eb2bfSHubert Tong 
234ab2eb2bfSHubert Tong void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const {
235a93a33cbSSean Fertile   const char *Ptr = reinterpret_cast<const char *>(Sec.p);
236a93a33cbSSean Fertile   Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
237ab2eb2bfSHubert Tong }
238ab2eb2bfSHubert Tong 
2398be28cdcSFangrui Song Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
2407c72e82bSJason Liu   return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec));
241ab2eb2bfSHubert Tong }
242ab2eb2bfSHubert Tong 
243ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
244210314aeSSean Fertile   // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
245210314aeSSean Fertile   // with MSVC.
246210314aeSSean Fertile   if (is64Bit())
247210314aeSSean Fertile     return toSection64(Sec)->VirtualAddress;
248210314aeSSean Fertile 
249210314aeSSean Fertile   return toSection32(Sec)->VirtualAddress;
250ab2eb2bfSHubert Tong }
251ab2eb2bfSHubert Tong 
252ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
253a93a33cbSSean Fertile   // Section numbers in XCOFF are numbered beginning at 1. A section number of
254a93a33cbSSean Fertile   // zero is used to indicate that a symbol is being imported or is undefined.
255837ae69fSSean Fertile   if (is64Bit())
256837ae69fSSean Fertile     return toSection64(Sec) - sectionHeaderTable64() + 1;
257837ae69fSSean Fertile   else
258837ae69fSSean Fertile     return toSection32(Sec) - sectionHeaderTable32() + 1;
259ab2eb2bfSHubert Tong }
260ab2eb2bfSHubert Tong 
261ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const {
262210314aeSSean Fertile   // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
263210314aeSSean Fertile   // with MSVC.
264210314aeSSean Fertile   if (is64Bit())
265210314aeSSean Fertile     return toSection64(Sec)->SectionSize;
266210314aeSSean Fertile 
267210314aeSSean Fertile   return toSection32(Sec)->SectionSize;
268ab2eb2bfSHubert Tong }
269ab2eb2bfSHubert Tong 
270e1cb2c0fSFangrui Song Expected<ArrayRef<uint8_t>>
271e1cb2c0fSFangrui Song XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
272b91f798fSdiggerlin   if (isSectionVirtual(Sec))
273b91f798fSdiggerlin     return ArrayRef<uint8_t>();
274b91f798fSdiggerlin 
275ea13683fSdiggerlin   uint64_t OffsetToRaw;
276ea13683fSdiggerlin   if (is64Bit())
277ea13683fSdiggerlin     OffsetToRaw = toSection64(Sec)->FileOffsetToRawData;
278ea13683fSdiggerlin   else
279ea13683fSdiggerlin     OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
280b91f798fSdiggerlin 
281b91f798fSdiggerlin   const uint8_t * ContentStart = base() + OffsetToRaw;
282b91f798fSdiggerlin   uint64_t SectionSize = getSectionSize(Sec);
283b91f798fSdiggerlin   if (checkOffset(Data, uintptr_t(ContentStart), SectionSize))
284b91f798fSdiggerlin     return make_error<BinaryError>();
285b91f798fSdiggerlin 
286b91f798fSdiggerlin   return makeArrayRef(ContentStart,SectionSize);
287ab2eb2bfSHubert Tong }
288ab2eb2bfSHubert Tong 
289ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
290ab2eb2bfSHubert Tong   uint64_t Result = 0;
291ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
292ab2eb2bfSHubert Tong   return Result;
293ab2eb2bfSHubert Tong }
294ab2eb2bfSHubert Tong 
295ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
296ab2eb2bfSHubert Tong   bool Result = false;
297ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
298ab2eb2bfSHubert Tong   return Result;
299ab2eb2bfSHubert Tong }
300ab2eb2bfSHubert Tong 
301ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const {
302837ae69fSSean Fertile   return getSectionFlags(Sec) & XCOFF::STYP_TEXT;
303ab2eb2bfSHubert Tong }
304ab2eb2bfSHubert Tong 
305ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const {
306837ae69fSSean Fertile   uint32_t Flags = getSectionFlags(Sec);
307a93a33cbSSean Fertile   return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA);
308ab2eb2bfSHubert Tong }
309ab2eb2bfSHubert Tong 
310ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const {
311837ae69fSSean Fertile   uint32_t Flags = getSectionFlags(Sec);
312a93a33cbSSean Fertile   return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS);
313ab2eb2bfSHubert Tong }
314ab2eb2bfSHubert Tong 
315ab2eb2bfSHubert Tong bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
316b91f798fSdiggerlin   return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0
317b91f798fSdiggerlin                    : toSection32(Sec)->FileOffsetToRawData == 0;
318ab2eb2bfSHubert Tong }
319ab2eb2bfSHubert Tong 
320ab2eb2bfSHubert Tong relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
321d60d7d69Sjasonliu   if (is64Bit())
322d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
323d60d7d69Sjasonliu   const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
324d60d7d69Sjasonliu   auto RelocationsOrErr = relocations(*SectionEntPtr);
325d60d7d69Sjasonliu   if (Error E = RelocationsOrErr.takeError())
326ab2eb2bfSHubert Tong     return relocation_iterator(RelocationRef());
327d60d7d69Sjasonliu   DataRefImpl Ret;
328d60d7d69Sjasonliu   Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
329d60d7d69Sjasonliu   return relocation_iterator(RelocationRef(Ret, this));
330ab2eb2bfSHubert Tong }
331ab2eb2bfSHubert Tong 
332ab2eb2bfSHubert Tong relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const {
333d60d7d69Sjasonliu   if (is64Bit())
334d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
335d60d7d69Sjasonliu   const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
336d60d7d69Sjasonliu   auto RelocationsOrErr = relocations(*SectionEntPtr);
337d60d7d69Sjasonliu   if (Error E = RelocationsOrErr.takeError())
338ab2eb2bfSHubert Tong     return relocation_iterator(RelocationRef());
339d60d7d69Sjasonliu   DataRefImpl Ret;
340d60d7d69Sjasonliu   Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
341d60d7d69Sjasonliu   return relocation_iterator(RelocationRef(Ret, this));
342ab2eb2bfSHubert Tong }
343ab2eb2bfSHubert Tong 
344ab2eb2bfSHubert Tong void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
345d60d7d69Sjasonliu   Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1);
346ab2eb2bfSHubert Tong }
347ab2eb2bfSHubert Tong 
348ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
349d60d7d69Sjasonliu   if (is64Bit())
350d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
351d60d7d69Sjasonliu   const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
352d60d7d69Sjasonliu   const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32();
353d60d7d69Sjasonliu   const uint32_t RelocAddress = Reloc->VirtualAddress;
354d60d7d69Sjasonliu   const uint16_t NumberOfSections = getNumberOfSections();
355d60d7d69Sjasonliu   for (uint16_t i = 0; i < NumberOfSections; ++i) {
356d60d7d69Sjasonliu     // Find which section this relocation is belonging to, and get the
357d60d7d69Sjasonliu     // relocation offset relative to the start of the section.
358d60d7d69Sjasonliu     if (Sec32->VirtualAddress <= RelocAddress &&
359d60d7d69Sjasonliu         RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) {
360d60d7d69Sjasonliu       return RelocAddress - Sec32->VirtualAddress;
361d60d7d69Sjasonliu     }
362d60d7d69Sjasonliu     ++Sec32;
363d60d7d69Sjasonliu   }
364d60d7d69Sjasonliu   return InvalidRelocOffset;
365ab2eb2bfSHubert Tong }
366ab2eb2bfSHubert Tong 
367ab2eb2bfSHubert Tong symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
368d60d7d69Sjasonliu   if (is64Bit())
369d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
370d60d7d69Sjasonliu   const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
371d60d7d69Sjasonliu   const uint32_t Index = Reloc->SymbolIndex;
372d60d7d69Sjasonliu 
373d60d7d69Sjasonliu   if (Index >= getLogicalNumberOfSymbolTableEntries32())
374d60d7d69Sjasonliu     return symbol_end();
375d60d7d69Sjasonliu 
376d60d7d69Sjasonliu   DataRefImpl SymDRI;
377d60d7d69Sjasonliu   SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index);
378d60d7d69Sjasonliu   return symbol_iterator(SymbolRef(SymDRI, this));
379ab2eb2bfSHubert Tong }
380ab2eb2bfSHubert Tong 
381ab2eb2bfSHubert Tong uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const {
382d60d7d69Sjasonliu   if (is64Bit())
383d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
384d60d7d69Sjasonliu   return viewAs<XCOFFRelocation32>(Rel.p)->Type;
385ab2eb2bfSHubert Tong }
386ab2eb2bfSHubert Tong 
387ab2eb2bfSHubert Tong void XCOFFObjectFile::getRelocationTypeName(
388ab2eb2bfSHubert Tong     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
389d60d7d69Sjasonliu   if (is64Bit())
390d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
391d60d7d69Sjasonliu   const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
392d60d7d69Sjasonliu   StringRef Res = XCOFF::getRelocationTypeString(Reloc->Type);
393d60d7d69Sjasonliu   Result.append(Res.begin(), Res.end());
394ab2eb2bfSHubert Tong }
395ab2eb2bfSHubert Tong 
396ac00376aSvgxbj Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const {
397ab2eb2bfSHubert Tong   uint32_t Result = 0;
398ab2eb2bfSHubert Tong   llvm_unreachable("Not yet implemented!");
399ab2eb2bfSHubert Tong   return Result;
400ab2eb2bfSHubert Tong }
401ab2eb2bfSHubert Tong 
402ab2eb2bfSHubert Tong basic_symbol_iterator XCOFFObjectFile::symbol_begin() const {
403d60d7d69Sjasonliu   if (is64Bit())
404d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
4059212206dSJason Liu   DataRefImpl SymDRI;
4069212206dSJason Liu   SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr);
4079212206dSJason Liu   return basic_symbol_iterator(SymbolRef(SymDRI, this));
408ab2eb2bfSHubert Tong }
409ab2eb2bfSHubert Tong 
410ab2eb2bfSHubert Tong basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
411d60d7d69Sjasonliu   if (is64Bit())
412d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
4139212206dSJason Liu   DataRefImpl SymDRI;
4149212206dSJason Liu   SymDRI.p = reinterpret_cast<uintptr_t>(
415837ae69fSSean Fertile       SymbolTblPtr + getLogicalNumberOfSymbolTableEntries32());
4169212206dSJason Liu   return basic_symbol_iterator(SymbolRef(SymDRI, this));
417ab2eb2bfSHubert Tong }
418ab2eb2bfSHubert Tong 
419ab2eb2bfSHubert Tong section_iterator XCOFFObjectFile::section_begin() const {
420a93a33cbSSean Fertile   DataRefImpl DRI;
421837ae69fSSean Fertile   DRI.p = getSectionHeaderTableAddress();
422a93a33cbSSean Fertile   return section_iterator(SectionRef(DRI, this));
423ab2eb2bfSHubert Tong }
424ab2eb2bfSHubert Tong 
425ab2eb2bfSHubert Tong section_iterator XCOFFObjectFile::section_end() const {
426a93a33cbSSean Fertile   DataRefImpl DRI;
427837ae69fSSean Fertile   DRI.p = getWithOffset(getSectionHeaderTableAddress(),
428837ae69fSSean Fertile                         getNumberOfSections() * getSectionHeaderSize());
429a93a33cbSSean Fertile   return section_iterator(SectionRef(DRI, this));
430ab2eb2bfSHubert Tong }
431ab2eb2bfSHubert Tong 
432837ae69fSSean Fertile uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; }
433ab2eb2bfSHubert Tong 
434ab2eb2bfSHubert Tong StringRef XCOFFObjectFile::getFileFormatName() const {
435837ae69fSSean Fertile   return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000";
436ab2eb2bfSHubert Tong }
437ab2eb2bfSHubert Tong 
438ab2eb2bfSHubert Tong Triple::ArchType XCOFFObjectFile::getArch() const {
439837ae69fSSean Fertile   return is64Bit() ? Triple::ppc64 : Triple::ppc;
440ab2eb2bfSHubert Tong }
441ab2eb2bfSHubert Tong 
442ab2eb2bfSHubert Tong SubtargetFeatures XCOFFObjectFile::getFeatures() const {
443ab2eb2bfSHubert Tong   return SubtargetFeatures();
444ab2eb2bfSHubert Tong }
445ab2eb2bfSHubert Tong 
446ab2eb2bfSHubert Tong bool XCOFFObjectFile::isRelocatableObject() const {
447d60d7d69Sjasonliu   if (is64Bit())
448d60d7d69Sjasonliu     report_fatal_error("64-bit support not implemented yet");
449d60d7d69Sjasonliu   return !(fileHeader32()->Flags & NoRelMask);
450ab2eb2bfSHubert Tong }
451ab2eb2bfSHubert Tong 
452a93a33cbSSean Fertile Expected<uint64_t> XCOFFObjectFile::getStartAddress() const {
453a93a33cbSSean Fertile   // TODO FIXME Should get from auxiliary_header->o_entry when support for the
454a93a33cbSSean Fertile   // auxiliary_header is added.
455a93a33cbSSean Fertile   return 0;
456a93a33cbSSean Fertile }
457a93a33cbSSean Fertile 
458837ae69fSSean Fertile size_t XCOFFObjectFile::getFileHeaderSize() const {
459837ae69fSSean Fertile   return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32);
4609212206dSJason Liu }
4619212206dSJason Liu 
462837ae69fSSean Fertile size_t XCOFFObjectFile::getSectionHeaderSize() const {
463837ae69fSSean Fertile   return is64Bit() ? sizeof(XCOFFSectionHeader64) :
464837ae69fSSean Fertile                      sizeof(XCOFFSectionHeader32);
465837ae69fSSean Fertile }
466837ae69fSSean Fertile 
467837ae69fSSean Fertile bool XCOFFObjectFile::is64Bit() const {
468837ae69fSSean Fertile   return Binary::ID_XCOFF64 == getType();
469837ae69fSSean Fertile }
470837ae69fSSean Fertile 
471837ae69fSSean Fertile uint16_t XCOFFObjectFile::getMagic() const {
472837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic;
473837ae69fSSean Fertile }
474837ae69fSSean Fertile 
475837ae69fSSean Fertile Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const {
476837ae69fSSean Fertile   if (Num <= 0 || Num > getNumberOfSections())
477837ae69fSSean Fertile     return errorCodeToError(object_error::invalid_section_index);
478837ae69fSSean Fertile 
479837ae69fSSean Fertile   DataRefImpl DRI;
480837ae69fSSean Fertile   DRI.p = getWithOffset(getSectionHeaderTableAddress(),
481837ae69fSSean Fertile                         getSectionHeaderSize() * (Num - 1));
482837ae69fSSean Fertile   return DRI;
4839212206dSJason Liu }
4849212206dSJason Liu 
4859212206dSJason Liu Expected<StringRef>
4869212206dSJason Liu XCOFFObjectFile::getSymbolSectionName(const XCOFFSymbolEntry *SymEntPtr) const {
487837ae69fSSean Fertile   assert(!is64Bit() && "Symbol table support not implemented for 64-bit.");
4889212206dSJason Liu   int16_t SectionNum = SymEntPtr->SectionNumber;
4899212206dSJason Liu 
4909212206dSJason Liu   switch (SectionNum) {
4919212206dSJason Liu   case XCOFF::N_DEBUG:
4929212206dSJason Liu     return "N_DEBUG";
4939212206dSJason Liu   case XCOFF::N_ABS:
4949212206dSJason Liu     return "N_ABS";
4959212206dSJason Liu   case XCOFF::N_UNDEF:
4969212206dSJason Liu     return "N_UNDEF";
497837ae69fSSean Fertile   default:
498837ae69fSSean Fertile     Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum);
499837ae69fSSean Fertile     if (SecRef)
5007c72e82bSJason Liu       return generateXCOFFFixedNameStringRef(
5017c72e82bSJason Liu           getSectionNameInternal(SecRef.get()));
502837ae69fSSean Fertile     return SecRef.takeError();
5039212206dSJason Liu   }
5049212206dSJason Liu }
5059212206dSJason Liu 
5069212206dSJason Liu bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
5079212206dSJason Liu   return (SectionNumber <= 0 && SectionNumber >= -2);
5089212206dSJason Liu }
5099212206dSJason Liu 
5109212206dSJason Liu uint16_t XCOFFObjectFile::getNumberOfSections() const {
511837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->NumberOfSections
512837ae69fSSean Fertile                    : fileHeader32()->NumberOfSections;
5139212206dSJason Liu }
5149212206dSJason Liu 
515837ae69fSSean Fertile int32_t XCOFFObjectFile::getTimeStamp() const {
516837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp;
5179212206dSJason Liu }
5189212206dSJason Liu 
5199212206dSJason Liu uint16_t XCOFFObjectFile::getOptionalHeaderSize() const {
520837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->AuxHeaderSize
521837ae69fSSean Fertile                    : fileHeader32()->AuxHeaderSize;
5229212206dSJason Liu }
5239212206dSJason Liu 
524837ae69fSSean Fertile uint32_t XCOFFObjectFile::getSymbolTableOffset32() const {
525837ae69fSSean Fertile   return fileHeader32()->SymbolTableOffset;
526837ae69fSSean Fertile }
527ab2eb2bfSHubert Tong 
528837ae69fSSean Fertile int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const {
529837ae69fSSean Fertile   // As far as symbol table size is concerned, if this field is negative it is
530837ae69fSSean Fertile   // to be treated as a 0. However since this field is also used for printing we
531837ae69fSSean Fertile   // don't want to truncate any negative values.
532837ae69fSSean Fertile   return fileHeader32()->NumberOfSymTableEntries;
533837ae69fSSean Fertile }
534ab2eb2bfSHubert Tong 
535837ae69fSSean Fertile uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const {
536837ae69fSSean Fertile   return (fileHeader32()->NumberOfSymTableEntries >= 0
537837ae69fSSean Fertile               ? fileHeader32()->NumberOfSymTableEntries
538837ae69fSSean Fertile               : 0);
539837ae69fSSean Fertile }
540a93a33cbSSean Fertile 
541837ae69fSSean Fertile uint64_t XCOFFObjectFile::getSymbolTableOffset64() const {
542837ae69fSSean Fertile   return fileHeader64()->SymbolTableOffset;
543837ae69fSSean Fertile }
544837ae69fSSean Fertile 
545837ae69fSSean Fertile uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const {
546837ae69fSSean Fertile   return fileHeader64()->NumberOfSymTableEntries;
547837ae69fSSean Fertile }
548837ae69fSSean Fertile 
5497c72e82bSJason Liu uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const {
5507c72e82bSJason Liu   uint32_t NumberOfSymTableEntries =
5517c72e82bSJason Liu       is64Bit() ? getNumberOfSymbolTableEntries64()
5527c72e82bSJason Liu                 : getLogicalNumberOfSymbolTableEntries32();
5537c72e82bSJason Liu   return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr),
5547c72e82bSJason Liu                        XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries);
5557c72e82bSJason Liu }
5567c72e82bSJason Liu 
5577c72e82bSJason Liu void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const {
5587c72e82bSJason Liu   if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr))
5597c72e82bSJason Liu     report_fatal_error("Symbol table entry is outside of symbol table.");
5607c72e82bSJason Liu 
5617c72e82bSJason Liu   if (SymbolEntPtr >= getEndOfSymbolTableAddress())
5627c72e82bSJason Liu     report_fatal_error("Symbol table entry is outside of symbol table.");
5637c72e82bSJason Liu 
5647c72e82bSJason Liu   ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) -
5657c72e82bSJason Liu                      reinterpret_cast<const char *>(SymbolTblPtr);
5667c72e82bSJason Liu 
5677c72e82bSJason Liu   if (Offset % XCOFF::SymbolTableEntrySize != 0)
5687c72e82bSJason Liu     report_fatal_error(
5697c72e82bSJason Liu         "Symbol table entry position is not valid inside of symbol table.");
5707c72e82bSJason Liu }
5717c72e82bSJason Liu 
5727c72e82bSJason Liu uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const {
5737c72e82bSJason Liu   return (reinterpret_cast<const char *>(SymbolEntPtr) -
5747c72e82bSJason Liu           reinterpret_cast<const char *>(SymbolTblPtr)) /
5757c72e82bSJason Liu          XCOFF::SymbolTableEntrySize;
5767c72e82bSJason Liu }
5777c72e82bSJason Liu 
57834d4bff3SDigger Lin Expected<StringRef>
57934d4bff3SDigger Lin XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const {
58034d4bff3SDigger Lin   if (is64Bit())
58134d4bff3SDigger Lin     report_fatal_error("64-bit symbol table support not implemented yet.");
58234d4bff3SDigger Lin 
58334d4bff3SDigger Lin   if (Index >= getLogicalNumberOfSymbolTableEntries32())
58434d4bff3SDigger Lin     return errorCodeToError(object_error::invalid_symbol_index);
58534d4bff3SDigger Lin 
58634d4bff3SDigger Lin   DataRefImpl SymDRI;
58734d4bff3SDigger Lin   SymDRI.p = reinterpret_cast<uintptr_t>(getPointerToSymbolTable() + Index);
58834d4bff3SDigger Lin   return getSymbolName(SymDRI);
58934d4bff3SDigger Lin }
59034d4bff3SDigger Lin 
591837ae69fSSean Fertile uint16_t XCOFFObjectFile::getFlags() const {
592837ae69fSSean Fertile   return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags;
593837ae69fSSean Fertile }
594837ae69fSSean Fertile 
595837ae69fSSean Fertile const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const {
596837ae69fSSean Fertile   return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name;
597837ae69fSSean Fertile }
598837ae69fSSean Fertile 
599837ae69fSSean Fertile uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const {
600837ae69fSSean Fertile   return reinterpret_cast<uintptr_t>(SectionHeaderTable);
601837ae69fSSean Fertile }
602837ae69fSSean Fertile 
603837ae69fSSean Fertile int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const {
604837ae69fSSean Fertile   return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags;
605837ae69fSSean Fertile }
606837ae69fSSean Fertile 
607837ae69fSSean Fertile XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object)
608837ae69fSSean Fertile     : ObjectFile(Type, Object) {
609837ae69fSSean Fertile   assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64);
610837ae69fSSean Fertile }
611837ae69fSSean Fertile 
612837ae69fSSean Fertile ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const {
613837ae69fSSean Fertile   assert(is64Bit() && "64-bit interface called for non 64-bit file.");
614837ae69fSSean Fertile   const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64();
615837ae69fSSean Fertile   return ArrayRef<XCOFFSectionHeader64>(TablePtr,
616837ae69fSSean Fertile                                         TablePtr + getNumberOfSections());
617837ae69fSSean Fertile }
618837ae69fSSean Fertile 
619837ae69fSSean Fertile ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const {
620837ae69fSSean Fertile   assert(!is64Bit() && "32-bit interface called for non 32-bit file.");
621837ae69fSSean Fertile   const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32();
622837ae69fSSean Fertile   return ArrayRef<XCOFFSectionHeader32>(TablePtr,
623837ae69fSSean Fertile                                         TablePtr + getNumberOfSections());
624837ae69fSSean Fertile }
625837ae69fSSean Fertile 
62634d4bff3SDigger Lin // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO
62734d4bff3SDigger Lin // section header contains the actual count of relocation entries in the s_paddr
62834d4bff3SDigger Lin // field. STYP_OVRFLO headers contain the section index of their corresponding
62934d4bff3SDigger Lin // sections as their raw "NumberOfRelocations" field value.
63034d4bff3SDigger Lin Expected<uint32_t> XCOFFObjectFile::getLogicalNumberOfRelocationEntries(
63134d4bff3SDigger Lin     const XCOFFSectionHeader32 &Sec) const {
63234d4bff3SDigger Lin 
63334d4bff3SDigger Lin   uint16_t SectionIndex = &Sec - sectionHeaderTable32() + 1;
63434d4bff3SDigger Lin 
635d60d7d69Sjasonliu   if (Sec.NumberOfRelocations < RelocOverflow)
63634d4bff3SDigger Lin     return Sec.NumberOfRelocations;
63734d4bff3SDigger Lin   for (const auto &Sec : sections32()) {
63834d4bff3SDigger Lin     if (Sec.Flags == XCOFF::STYP_OVRFLO &&
63934d4bff3SDigger Lin         Sec.NumberOfRelocations == SectionIndex)
64034d4bff3SDigger Lin       return Sec.PhysicalAddress;
64134d4bff3SDigger Lin   }
64234d4bff3SDigger Lin   return errorCodeToError(object_error::parse_failed);
64334d4bff3SDigger Lin }
64434d4bff3SDigger Lin 
64534d4bff3SDigger Lin Expected<ArrayRef<XCOFFRelocation32>>
64634d4bff3SDigger Lin XCOFFObjectFile::relocations(const XCOFFSectionHeader32 &Sec) const {
64734d4bff3SDigger Lin   uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader),
64834d4bff3SDigger Lin                                       Sec.FileOffsetToRelocationInfo);
64934d4bff3SDigger Lin   auto NumRelocEntriesOrErr = getLogicalNumberOfRelocationEntries(Sec);
65034d4bff3SDigger Lin   if (Error E = NumRelocEntriesOrErr.takeError())
651c55cf4afSBill Wendling     return std::move(E);
65234d4bff3SDigger Lin 
65334d4bff3SDigger Lin   uint32_t NumRelocEntries = NumRelocEntriesOrErr.get();
65434d4bff3SDigger Lin 
6553bbe7a68Sjasonliu   assert(sizeof(XCOFFRelocation32) == XCOFF::RelocationSerializationSize32);
65634d4bff3SDigger Lin   auto RelocationOrErr =
65734d4bff3SDigger Lin       getObject<XCOFFRelocation32>(Data, reinterpret_cast<void *>(RelocAddr),
65834d4bff3SDigger Lin                                    NumRelocEntries * sizeof(XCOFFRelocation32));
65934d4bff3SDigger Lin   if (Error E = RelocationOrErr.takeError())
660c55cf4afSBill Wendling     return std::move(E);
66134d4bff3SDigger Lin 
66234d4bff3SDigger Lin   const XCOFFRelocation32 *StartReloc = RelocationOrErr.get();
66334d4bff3SDigger Lin 
66434d4bff3SDigger Lin   return ArrayRef<XCOFFRelocation32>(StartReloc, StartReloc + NumRelocEntries);
66534d4bff3SDigger Lin }
66634d4bff3SDigger Lin 
667837ae69fSSean Fertile Expected<XCOFFStringTable>
668837ae69fSSean Fertile XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) {
669837ae69fSSean Fertile   // If there is a string table, then the buffer must contain at least 4 bytes
670837ae69fSSean Fertile   // for the string table's size. Not having a string table is not an error.
671*e03a135bSReid Kleckner   if (Error E = Binary::checkOffset(
672*e03a135bSReid Kleckner           Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) {
673*e03a135bSReid Kleckner     consumeError(std::move(E));
674837ae69fSSean Fertile     return XCOFFStringTable{0, nullptr};
675*e03a135bSReid Kleckner   }
676837ae69fSSean Fertile 
677837ae69fSSean Fertile   // Read the size out of the buffer.
678837ae69fSSean Fertile   uint32_t Size = support::endian::read32be(Obj->base() + Offset);
679837ae69fSSean Fertile 
680837ae69fSSean Fertile   // If the size is less then 4, then the string table is just a size and no
681837ae69fSSean Fertile   // string data.
682837ae69fSSean Fertile   if (Size <= 4)
683837ae69fSSean Fertile     return XCOFFStringTable{4, nullptr};
684837ae69fSSean Fertile 
685837ae69fSSean Fertile   auto StringTableOrErr =
686837ae69fSSean Fertile       getObject<char>(Obj->Data, Obj->base() + Offset, Size);
687837ae69fSSean Fertile   if (Error E = StringTableOrErr.takeError())
688c55cf4afSBill Wendling     return std::move(E);
689837ae69fSSean Fertile 
690837ae69fSSean Fertile   const char *StringTablePtr = StringTableOrErr.get();
691837ae69fSSean Fertile   if (StringTablePtr[Size - 1] != '\0')
692837ae69fSSean Fertile     return errorCodeToError(object_error::string_table_non_null_end);
693837ae69fSSean Fertile 
694837ae69fSSean Fertile   return XCOFFStringTable{Size, StringTablePtr};
695837ae69fSSean Fertile }
696837ae69fSSean Fertile 
697837ae69fSSean Fertile Expected<std::unique_ptr<XCOFFObjectFile>>
698837ae69fSSean Fertile XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) {
6990eaee545SJonas Devlieghere   // Can't use std::make_unique because of the private constructor.
700837ae69fSSean Fertile   std::unique_ptr<XCOFFObjectFile> Obj;
701837ae69fSSean Fertile   Obj.reset(new XCOFFObjectFile(Type, MBR));
702837ae69fSSean Fertile 
703837ae69fSSean Fertile   uint64_t CurOffset = 0;
704837ae69fSSean Fertile   const auto *Base = Obj->base();
705837ae69fSSean Fertile   MemoryBufferRef Data = Obj->Data;
706837ae69fSSean Fertile 
707837ae69fSSean Fertile   // Parse file header.
708837ae69fSSean Fertile   auto FileHeaderOrErr =
709837ae69fSSean Fertile       getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize());
710837ae69fSSean Fertile   if (Error E = FileHeaderOrErr.takeError())
711c55cf4afSBill Wendling     return std::move(E);
712837ae69fSSean Fertile   Obj->FileHeader = FileHeaderOrErr.get();
713837ae69fSSean Fertile 
714837ae69fSSean Fertile   CurOffset += Obj->getFileHeaderSize();
715a93a33cbSSean Fertile   // TODO FIXME we don't have support for an optional header yet, so just skip
716a93a33cbSSean Fertile   // past it.
717837ae69fSSean Fertile   CurOffset += Obj->getOptionalHeaderSize();
718a93a33cbSSean Fertile 
719837ae69fSSean Fertile   // Parse the section header table if it is present.
720837ae69fSSean Fertile   if (Obj->getNumberOfSections()) {
721837ae69fSSean Fertile     auto SecHeadersOrErr = getObject<void>(Data, Base + CurOffset,
722837ae69fSSean Fertile                                            Obj->getNumberOfSections() *
723837ae69fSSean Fertile                                                Obj->getSectionHeaderSize());
724837ae69fSSean Fertile     if (Error E = SecHeadersOrErr.takeError())
725c55cf4afSBill Wendling       return std::move(E);
726837ae69fSSean Fertile     Obj->SectionHeaderTable = SecHeadersOrErr.get();
727a93a33cbSSean Fertile   }
728ab2eb2bfSHubert Tong 
729837ae69fSSean Fertile   // 64-bit object supports only file header and section headers for now.
730837ae69fSSean Fertile   if (Obj->is64Bit())
731c55cf4afSBill Wendling     return std::move(Obj);
732fd75ee91SSean Fertile 
733837ae69fSSean Fertile   // If there is no symbol table we are done parsing the memory buffer.
734837ae69fSSean Fertile   if (Obj->getLogicalNumberOfSymbolTableEntries32() == 0)
735c55cf4afSBill Wendling     return std::move(Obj);
736837ae69fSSean Fertile 
737837ae69fSSean Fertile   // Parse symbol table.
738837ae69fSSean Fertile   CurOffset = Obj->fileHeader32()->SymbolTableOffset;
7399212206dSJason Liu   uint64_t SymbolTableSize = (uint64_t)(sizeof(XCOFFSymbolEntry)) *
740837ae69fSSean Fertile                              Obj->getLogicalNumberOfSymbolTableEntries32();
741837ae69fSSean Fertile   auto SymTableOrErr =
742837ae69fSSean Fertile       getObject<XCOFFSymbolEntry>(Data, Base + CurOffset, SymbolTableSize);
743837ae69fSSean Fertile   if (Error E = SymTableOrErr.takeError())
744c55cf4afSBill Wendling     return std::move(E);
745837ae69fSSean Fertile   Obj->SymbolTblPtr = SymTableOrErr.get();
746837ae69fSSean Fertile   CurOffset += SymbolTableSize;
747fd75ee91SSean Fertile 
748837ae69fSSean Fertile   // Parse String table.
749837ae69fSSean Fertile   Expected<XCOFFStringTable> StringTableOrErr =
750837ae69fSSean Fertile       parseStringTable(Obj.get(), CurOffset);
751837ae69fSSean Fertile   if (Error E = StringTableOrErr.takeError())
752c55cf4afSBill Wendling     return std::move(E);
753837ae69fSSean Fertile   Obj->StringTable = StringTableOrErr.get();
754fd75ee91SSean Fertile 
755c55cf4afSBill Wendling   return std::move(Obj);
756fd75ee91SSean Fertile }
757fd75ee91SSean Fertile 
758ab2eb2bfSHubert Tong Expected<std::unique_ptr<ObjectFile>>
759837ae69fSSean Fertile ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef,
760837ae69fSSean Fertile                                   unsigned FileType) {
761837ae69fSSean Fertile   return XCOFFObjectFile::create(FileType, MemBufRef);
762ab2eb2bfSHubert Tong }
763ab2eb2bfSHubert Tong 
7647c72e82bSJason Liu XCOFF::StorageClass XCOFFSymbolRef::getStorageClass() const {
7657c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->StorageClass;
7667c72e82bSJason Liu }
7677c72e82bSJason Liu 
7687c72e82bSJason Liu uint8_t XCOFFSymbolRef::getNumberOfAuxEntries() const {
7697c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->NumberOfAuxEntries;
7707c72e82bSJason Liu }
7717c72e82bSJason Liu 
772a26a441bSdiggerlin // TODO: The function needs to return an error if there is no csect auxiliary
773a26a441bSdiggerlin // entry.
7747c72e82bSJason Liu const XCOFFCsectAuxEnt32 *XCOFFSymbolRef::getXCOFFCsectAuxEnt32() const {
7757c72e82bSJason Liu   assert(!OwningObjectPtr->is64Bit() &&
7767c72e82bSJason Liu          "32-bit interface called on 64-bit object file.");
7777c72e82bSJason Liu   assert(hasCsectAuxEnt() && "No Csect Auxiliary Entry is found.");
7787c72e82bSJason Liu 
7797c72e82bSJason Liu   // In XCOFF32, the csect auxilliary entry is always the last auxiliary
7807c72e82bSJason Liu   // entry for the symbol.
7817c72e82bSJason Liu   uintptr_t AuxAddr = getWithOffset(
7827c72e82bSJason Liu       SymEntDataRef.p, XCOFF::SymbolTableEntrySize * getNumberOfAuxEntries());
7837c72e82bSJason Liu 
7847c72e82bSJason Liu #ifndef NDEBUG
7857c72e82bSJason Liu   OwningObjectPtr->checkSymbolEntryPointer(AuxAddr);
7867c72e82bSJason Liu #endif
7877c72e82bSJason Liu 
7887c72e82bSJason Liu   return reinterpret_cast<const XCOFFCsectAuxEnt32 *>(AuxAddr);
7897c72e82bSJason Liu }
7907c72e82bSJason Liu 
7917c72e82bSJason Liu uint16_t XCOFFSymbolRef::getType() const {
7927c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SymbolType;
7937c72e82bSJason Liu }
7947c72e82bSJason Liu 
7957c72e82bSJason Liu int16_t XCOFFSymbolRef::getSectionNumber() const {
7967c72e82bSJason Liu   return OwningObjectPtr->toSymbolEntry(SymEntDataRef)->SectionNumber;
7977c72e82bSJason Liu }
7987c72e82bSJason Liu 
799a26a441bSdiggerlin // TODO: The function name needs to be changed to express the purpose of the
800a26a441bSdiggerlin // function.
8017c72e82bSJason Liu bool XCOFFSymbolRef::hasCsectAuxEnt() const {
8027c72e82bSJason Liu   XCOFF::StorageClass SC = getStorageClass();
8037c72e82bSJason Liu   return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT ||
8047c72e82bSJason Liu           SC == XCOFF::C_HIDEXT);
8057c72e82bSJason Liu }
8067c72e82bSJason Liu 
8077c72e82bSJason Liu bool XCOFFSymbolRef::isFunction() const {
8087c72e82bSJason Liu   if (OwningObjectPtr->is64Bit())
8097c72e82bSJason Liu     report_fatal_error("64-bit support is unimplemented yet.");
8107c72e82bSJason Liu 
811d60d7d69Sjasonliu   if (getType() & FunctionSym)
8127c72e82bSJason Liu     return true;
8137c72e82bSJason Liu 
8147c72e82bSJason Liu   if (!hasCsectAuxEnt())
8157c72e82bSJason Liu     return false;
8167c72e82bSJason Liu 
8177c72e82bSJason Liu   const XCOFFCsectAuxEnt32 *CsectAuxEnt = getXCOFFCsectAuxEnt32();
8187c72e82bSJason Liu 
8197c72e82bSJason Liu   // A function definition should be a label definition.
820d60d7d69Sjasonliu   if ((CsectAuxEnt->SymbolAlignmentAndType & SymTypeMask) != XCOFF::XTY_LD)
8217c72e82bSJason Liu     return false;
8227c72e82bSJason Liu 
8237c72e82bSJason Liu   if (CsectAuxEnt->StorageMappingClass != XCOFF::XMC_PR)
8247c72e82bSJason Liu     return false;
8257c72e82bSJason Liu 
8267c72e82bSJason Liu   int16_t SectNum = getSectionNumber();
8277c72e82bSJason Liu   Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum);
8287c72e82bSJason Liu   if (!SI)
8297c72e82bSJason Liu     return false;
8307c72e82bSJason Liu 
8317c72e82bSJason Liu   return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT);
832ab2eb2bfSHubert Tong }
833ab2eb2bfSHubert Tong 
834f8622543SFangrui Song // Explictly instantiate template classes.
835f8622543SFangrui Song template struct XCOFFSectionHeader<XCOFFSectionHeader32>;
836f8622543SFangrui Song template struct XCOFFSectionHeader<XCOFFSectionHeader64>;
837f8622543SFangrui Song 
838ab2eb2bfSHubert Tong } // namespace object
839ab2eb2bfSHubert Tong } // namespace llvm
840