xref: /llvm-project/llvm/lib/Object/XCOFFObjectFile.cpp (revision d02c1676d75a6bab1252b48da9a955fc7dc1251f)
1 //===--- XCOFFObjectFile.cpp - XCOFF object file implementation -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the XCOFFObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/XCOFFObjectFile.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/DataExtractor.h"
17 #include "llvm/TargetParser/SubtargetFeature.h"
18 #include <cstddef>
19 #include <cstring>
20 
21 namespace llvm {
22 
23 using namespace XCOFF;
24 
25 namespace object {
26 
27 static const uint8_t FunctionSym = 0x20;
28 static const uint16_t NoRelMask = 0x0001;
29 static const size_t SymbolAuxTypeOffset = 17;
30 
31 // Checks that [Ptr, Ptr + Size) bytes fall inside the memory buffer
32 // 'M'. Returns a pointer to the underlying object on success.
33 template <typename T>
34 static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
35                                      const uint64_t Size = sizeof(T)) {
36   uintptr_t Addr = reinterpret_cast<uintptr_t>(Ptr);
37   if (Error E = Binary::checkOffset(M, Addr, Size))
38     return std::move(E);
39   return reinterpret_cast<const T *>(Addr);
40 }
41 
42 static uintptr_t getWithOffset(uintptr_t Base, ptrdiff_t Offset) {
43   return reinterpret_cast<uintptr_t>(reinterpret_cast<const char *>(Base) +
44                                      Offset);
45 }
46 
47 template <typename T> static const T *viewAs(uintptr_t in) {
48   return reinterpret_cast<const T *>(in);
49 }
50 
51 static StringRef generateXCOFFFixedNameStringRef(const char *Name) {
52   auto NulCharPtr =
53       static_cast<const char *>(memchr(Name, '\0', XCOFF::NameSize));
54   return NulCharPtr ? StringRef(Name, NulCharPtr - Name)
55                     : StringRef(Name, XCOFF::NameSize);
56 }
57 
58 template <typename T> StringRef XCOFFSectionHeader<T>::getName() const {
59   const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
60   return generateXCOFFFixedNameStringRef(DerivedXCOFFSectionHeader.Name);
61 }
62 
63 template <typename T> uint16_t XCOFFSectionHeader<T>::getSectionType() const {
64   const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
65   return DerivedXCOFFSectionHeader.Flags & SectionFlagsTypeMask;
66 }
67 
68 template <typename T>
69 uint32_t XCOFFSectionHeader<T>::getSectionSubtype() const {
70   const T &DerivedXCOFFSectionHeader = static_cast<const T &>(*this);
71   return DerivedXCOFFSectionHeader.Flags & ~SectionFlagsTypeMask;
72 }
73 
74 template <typename T>
75 bool XCOFFSectionHeader<T>::isReservedSectionType() const {
76   return getSectionType() & SectionFlagsReservedMask;
77 }
78 
79 template <typename AddressType>
80 bool XCOFFRelocation<AddressType>::isRelocationSigned() const {
81   return Info & XR_SIGN_INDICATOR_MASK;
82 }
83 
84 template <typename AddressType>
85 bool XCOFFRelocation<AddressType>::isFixupIndicated() const {
86   return Info & XR_FIXUP_INDICATOR_MASK;
87 }
88 
89 template <typename AddressType>
90 uint8_t XCOFFRelocation<AddressType>::getRelocatedLength() const {
91   // The relocation encodes the bit length being relocated minus 1. Add back
92   // the 1 to get the actual length being relocated.
93   return (Info & XR_BIASED_LENGTH_MASK) + 1;
94 }
95 
96 template struct ExceptionSectionEntry<support::ubig32_t>;
97 template struct ExceptionSectionEntry<support::ubig64_t>;
98 
99 template <typename T>
100 Expected<StringRef> getLoaderSecSymNameInStrTbl(const T *LoaderSecHeader,
101                                                 uint64_t Offset) {
102   if (LoaderSecHeader->LengthOfStrTbl > Offset)
103     return (reinterpret_cast<const char *>(LoaderSecHeader) +
104             LoaderSecHeader->OffsetToStrTbl + Offset);
105 
106   return createError("entry with offset 0x" + Twine::utohexstr(Offset) +
107                      " in the loader section's string table with size 0x" +
108                      Twine::utohexstr(LoaderSecHeader->LengthOfStrTbl) +
109                      " is invalid");
110 }
111 
112 Expected<StringRef> LoaderSectionSymbolEntry32::getSymbolName(
113     const LoaderSectionHeader32 *LoaderSecHeader32) const {
114   const NameOffsetInStrTbl *NameInStrTbl =
115       reinterpret_cast<const NameOffsetInStrTbl *>(SymbolName);
116   if (NameInStrTbl->IsNameInStrTbl != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
117     return generateXCOFFFixedNameStringRef(SymbolName);
118 
119   return getLoaderSecSymNameInStrTbl(LoaderSecHeader32, NameInStrTbl->Offset);
120 }
121 
122 Expected<StringRef> LoaderSectionSymbolEntry64::getSymbolName(
123     const LoaderSectionHeader64 *LoaderSecHeader64) const {
124   return getLoaderSecSymNameInStrTbl(LoaderSecHeader64, Offset);
125 }
126 
127 uintptr_t
128 XCOFFObjectFile::getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress,
129                                                uint32_t Distance) {
130   return getWithOffset(CurrentAddress, Distance * XCOFF::SymbolTableEntrySize);
131 }
132 
133 const XCOFF::SymbolAuxType *
134 XCOFFObjectFile::getSymbolAuxType(uintptr_t AuxEntryAddress) const {
135   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
136   return viewAs<XCOFF::SymbolAuxType>(
137       getWithOffset(AuxEntryAddress, SymbolAuxTypeOffset));
138 }
139 
140 void XCOFFObjectFile::checkSectionAddress(uintptr_t Addr,
141                                           uintptr_t TableAddress) const {
142   if (Addr < TableAddress)
143     report_fatal_error("Section header outside of section header table.");
144 
145   uintptr_t Offset = Addr - TableAddress;
146   if (Offset >= getSectionHeaderSize() * getNumberOfSections())
147     report_fatal_error("Section header outside of section header table.");
148 
149   if (Offset % getSectionHeaderSize() != 0)
150     report_fatal_error(
151         "Section header pointer does not point to a valid section header.");
152 }
153 
154 const XCOFFSectionHeader32 *
155 XCOFFObjectFile::toSection32(DataRefImpl Ref) const {
156   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
157 #ifndef NDEBUG
158   checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
159 #endif
160   return viewAs<XCOFFSectionHeader32>(Ref.p);
161 }
162 
163 const XCOFFSectionHeader64 *
164 XCOFFObjectFile::toSection64(DataRefImpl Ref) const {
165   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
166 #ifndef NDEBUG
167   checkSectionAddress(Ref.p, getSectionHeaderTableAddress());
168 #endif
169   return viewAs<XCOFFSectionHeader64>(Ref.p);
170 }
171 
172 XCOFFSymbolRef XCOFFObjectFile::toSymbolRef(DataRefImpl Ref) const {
173   assert(Ref.p != 0 && "Symbol table pointer can not be nullptr!");
174 #ifndef NDEBUG
175   checkSymbolEntryPointer(Ref.p);
176 #endif
177   return XCOFFSymbolRef(Ref, this);
178 }
179 
180 const XCOFFFileHeader32 *XCOFFObjectFile::fileHeader32() const {
181   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
182   return static_cast<const XCOFFFileHeader32 *>(FileHeader);
183 }
184 
185 const XCOFFFileHeader64 *XCOFFObjectFile::fileHeader64() const {
186   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
187   return static_cast<const XCOFFFileHeader64 *>(FileHeader);
188 }
189 
190 const XCOFFAuxiliaryHeader32 *XCOFFObjectFile::auxiliaryHeader32() const {
191   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
192   return static_cast<const XCOFFAuxiliaryHeader32 *>(AuxiliaryHeader);
193 }
194 
195 const XCOFFAuxiliaryHeader64 *XCOFFObjectFile::auxiliaryHeader64() const {
196   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
197   return static_cast<const XCOFFAuxiliaryHeader64 *>(AuxiliaryHeader);
198 }
199 
200 template <typename T> const T *XCOFFObjectFile::sectionHeaderTable() const {
201   return static_cast<const T *>(SectionHeaderTable);
202 }
203 
204 const XCOFFSectionHeader32 *
205 XCOFFObjectFile::sectionHeaderTable32() const {
206   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
207   return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable);
208 }
209 
210 const XCOFFSectionHeader64 *
211 XCOFFObjectFile::sectionHeaderTable64() const {
212   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
213   return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable);
214 }
215 
216 void XCOFFObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
217   uintptr_t NextSymbolAddr = getAdvancedSymbolEntryAddress(
218       Symb.p, toSymbolRef(Symb).getNumberOfAuxEntries() + 1);
219 #ifndef NDEBUG
220   // This function is used by basic_symbol_iterator, which allows to
221   // point to the end-of-symbol-table address.
222   if (NextSymbolAddr != getEndOfSymbolTableAddress())
223     checkSymbolEntryPointer(NextSymbolAddr);
224 #endif
225   Symb.p = NextSymbolAddr;
226 }
227 
228 Expected<StringRef>
229 XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
230   // The byte offset is relative to the start of the string table.
231   // A byte offset value of 0 is a null or zero-length symbol
232   // name. A byte offset in the range 1 to 3 (inclusive) points into the length
233   // field; as a soft-error recovery mechanism, we treat such cases as having an
234   // offset of 0.
235   if (Offset < 4)
236     return StringRef(nullptr, 0);
237 
238   if (StringTable.Data != nullptr && StringTable.Size > Offset)
239     return (StringTable.Data + Offset);
240 
241   return createError("entry with offset 0x" + Twine::utohexstr(Offset) +
242                      " in a string table with size 0x" +
243                      Twine::utohexstr(StringTable.Size) + " is invalid");
244 }
245 
246 StringRef XCOFFObjectFile::getStringTable() const {
247   // If the size is less than or equal to 4, then the string table contains no
248   // string data.
249   return StringRef(StringTable.Data,
250                    StringTable.Size <= 4 ? 0 : StringTable.Size);
251 }
252 
253 Expected<StringRef>
254 XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const {
255   if (CFileEntPtr->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
256     return generateXCOFFFixedNameStringRef(CFileEntPtr->Name);
257   return getStringTableEntry(CFileEntPtr->NameInStrTbl.Offset);
258 }
259 
260 Expected<StringRef> XCOFFObjectFile::getSymbolName(DataRefImpl Symb) const {
261   return toSymbolRef(Symb).getName();
262 }
263 
264 Expected<uint64_t> XCOFFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
265   return toSymbolRef(Symb).getValue();
266 }
267 
268 uint64_t XCOFFObjectFile::getSymbolValueImpl(DataRefImpl Symb) const {
269   return toSymbolRef(Symb).getValue();
270 }
271 
272 uint32_t XCOFFObjectFile::getSymbolAlignment(DataRefImpl Symb) const {
273   uint64_t Result = 0;
274   XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
275   if (XCOFFSym.isCsectSymbol()) {
276     Expected<XCOFFCsectAuxRef> CsectAuxRefOrError =
277         XCOFFSym.getXCOFFCsectAuxRef();
278     if (!CsectAuxRefOrError)
279       // TODO: report the error up the stack.
280       consumeError(CsectAuxRefOrError.takeError());
281     else
282       Result = 1ULL << CsectAuxRefOrError.get().getAlignmentLog2();
283   }
284   return Result;
285 }
286 
287 uint64_t XCOFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
288   uint64_t Result = 0;
289   XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
290   if (XCOFFSym.isCsectSymbol()) {
291     Expected<XCOFFCsectAuxRef> CsectAuxRefOrError =
292         XCOFFSym.getXCOFFCsectAuxRef();
293     if (!CsectAuxRefOrError)
294       // TODO: report the error up the stack.
295       consumeError(CsectAuxRefOrError.takeError());
296     else {
297       XCOFFCsectAuxRef CsectAuxRef = CsectAuxRefOrError.get();
298       assert(CsectAuxRef.getSymbolType() == XCOFF::XTY_CM);
299       Result = CsectAuxRef.getSectionOrLength();
300     }
301   }
302   return Result;
303 }
304 
305 Expected<SymbolRef::Type>
306 XCOFFObjectFile::getSymbolType(DataRefImpl Symb) const {
307   XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
308 
309   Expected<bool> IsFunction = XCOFFSym.isFunction();
310   if (!IsFunction)
311     return IsFunction.takeError();
312 
313   if (*IsFunction)
314     return SymbolRef::ST_Function;
315 
316   if (XCOFF::C_FILE == XCOFFSym.getStorageClass())
317     return SymbolRef::ST_File;
318 
319   int16_t SecNum = XCOFFSym.getSectionNumber();
320   if (SecNum <= 0)
321     return SymbolRef::ST_Other;
322 
323   Expected<DataRefImpl> SecDRIOrErr =
324       getSectionByNum(XCOFFSym.getSectionNumber());
325 
326   if (!SecDRIOrErr)
327     return SecDRIOrErr.takeError();
328 
329   DataRefImpl SecDRI = SecDRIOrErr.get();
330 
331   Expected<StringRef> SymNameOrError = XCOFFSym.getName();
332   if (SymNameOrError) {
333     // The "TOC" symbol is treated as SymbolRef::ST_Other.
334     if (SymNameOrError.get() == "TOC")
335       return SymbolRef::ST_Other;
336 
337     // The symbol for a section name is treated as SymbolRef::ST_Other.
338     StringRef SecName;
339     if (is64Bit())
340       SecName = XCOFFObjectFile::toSection64(SecDRIOrErr.get())->getName();
341     else
342       SecName = XCOFFObjectFile::toSection32(SecDRIOrErr.get())->getName();
343 
344     if (SecName == SymNameOrError.get())
345       return SymbolRef::ST_Other;
346   } else
347     return SymNameOrError.takeError();
348 
349   if (isSectionData(SecDRI) || isSectionBSS(SecDRI))
350     return SymbolRef::ST_Data;
351 
352   if (isDebugSection(SecDRI))
353     return SymbolRef::ST_Debug;
354 
355   return SymbolRef::ST_Other;
356 }
357 
358 Expected<section_iterator>
359 XCOFFObjectFile::getSymbolSection(DataRefImpl Symb) const {
360   const int16_t SectNum = toSymbolRef(Symb).getSectionNumber();
361 
362   if (isReservedSectionNumber(SectNum))
363     return section_end();
364 
365   Expected<DataRefImpl> ExpSec = getSectionByNum(SectNum);
366   if (!ExpSec)
367     return ExpSec.takeError();
368 
369   return section_iterator(SectionRef(ExpSec.get(), this));
370 }
371 
372 void XCOFFObjectFile::moveSectionNext(DataRefImpl &Sec) const {
373   const char *Ptr = reinterpret_cast<const char *>(Sec.p);
374   Sec.p = reinterpret_cast<uintptr_t>(Ptr + getSectionHeaderSize());
375 }
376 
377 Expected<StringRef> XCOFFObjectFile::getSectionName(DataRefImpl Sec) const {
378   return generateXCOFFFixedNameStringRef(getSectionNameInternal(Sec));
379 }
380 
381 uint64_t XCOFFObjectFile::getSectionAddress(DataRefImpl Sec) const {
382   // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
383   // with MSVC.
384   if (is64Bit())
385     return toSection64(Sec)->VirtualAddress;
386 
387   return toSection32(Sec)->VirtualAddress;
388 }
389 
390 uint64_t XCOFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
391   // Section numbers in XCOFF are numbered beginning at 1. A section number of
392   // zero is used to indicate that a symbol is being imported or is undefined.
393   if (is64Bit())
394     return toSection64(Sec) - sectionHeaderTable64() + 1;
395   else
396     return toSection32(Sec) - sectionHeaderTable32() + 1;
397 }
398 
399 uint64_t XCOFFObjectFile::getSectionSize(DataRefImpl Sec) const {
400   // Avoid ternary due to failure to convert the ubig32_t value to a unit64_t
401   // with MSVC.
402   if (is64Bit())
403     return toSection64(Sec)->SectionSize;
404 
405   return toSection32(Sec)->SectionSize;
406 }
407 
408 Expected<ArrayRef<uint8_t>>
409 XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
410   if (isSectionVirtual(Sec))
411     return ArrayRef<uint8_t>();
412 
413   uint64_t OffsetToRaw;
414   if (is64Bit())
415     OffsetToRaw = toSection64(Sec)->FileOffsetToRawData;
416   else
417     OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
418 
419   const uint8_t * ContentStart = base() + OffsetToRaw;
420   uint64_t SectionSize = getSectionSize(Sec);
421   if (Error E = Binary::checkOffset(
422           Data, reinterpret_cast<uintptr_t>(ContentStart), SectionSize))
423     return createError(
424         toString(std::move(E)) + ": section data with offset 0x" +
425         Twine::utohexstr(OffsetToRaw) + " and size 0x" +
426         Twine::utohexstr(SectionSize) + " goes past the end of the file");
427 
428   return ArrayRef(ContentStart, SectionSize);
429 }
430 
431 uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
432   uint64_t Result = 0;
433   llvm_unreachable("Not yet implemented!");
434   return Result;
435 }
436 
437 uint64_t XCOFFObjectFile::getSectionFileOffsetToRawData(DataRefImpl Sec) const {
438   if (is64Bit())
439     return toSection64(Sec)->FileOffsetToRawData;
440 
441   return toSection32(Sec)->FileOffsetToRawData;
442 }
443 
444 Expected<uintptr_t> XCOFFObjectFile::getSectionFileOffsetToRawData(
445     XCOFF::SectionTypeFlags SectType) const {
446   DataRefImpl DRI = getSectionByType(SectType);
447 
448   if (DRI.p == 0) // No section is not an error.
449     return 0;
450 
451   uint64_t SectionOffset = getSectionFileOffsetToRawData(DRI);
452   uint64_t SizeOfSection = getSectionSize(DRI);
453 
454   uintptr_t SectionStart = reinterpret_cast<uintptr_t>(base() + SectionOffset);
455   if (Error E = Binary::checkOffset(Data, SectionStart, SizeOfSection)) {
456     SmallString<32> UnknownType;
457     Twine(("<Unknown:") + Twine::utohexstr(SectType) + ">")
458         .toVector(UnknownType);
459     const char *SectionName = UnknownType.c_str();
460 
461     switch (SectType) {
462 #define ECASE(Value, String)                                                   \
463   case XCOFF::Value:                                                           \
464     SectionName = String;                                                      \
465     break
466 
467       ECASE(STYP_PAD, "pad");
468       ECASE(STYP_DWARF, "dwarf");
469       ECASE(STYP_TEXT, "text");
470       ECASE(STYP_DATA, "data");
471       ECASE(STYP_BSS, "bss");
472       ECASE(STYP_EXCEPT, "expect");
473       ECASE(STYP_INFO, "info");
474       ECASE(STYP_TDATA, "tdata");
475       ECASE(STYP_TBSS, "tbss");
476       ECASE(STYP_LOADER, "loader");
477       ECASE(STYP_DEBUG, "debug");
478       ECASE(STYP_TYPCHK, "typchk");
479       ECASE(STYP_OVRFLO, "ovrflo");
480 #undef ECASE
481     }
482     return createError(toString(std::move(E)) + ": " + SectionName +
483                        " section with offset 0x" +
484                        Twine::utohexstr(SectionOffset) + " and size 0x" +
485                        Twine::utohexstr(SizeOfSection) +
486                        " goes past the end of the file");
487   }
488   return SectionStart;
489 }
490 
491 bool XCOFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
492   return false;
493 }
494 
495 bool XCOFFObjectFile::isSectionText(DataRefImpl Sec) const {
496   return getSectionFlags(Sec) & XCOFF::STYP_TEXT;
497 }
498 
499 bool XCOFFObjectFile::isSectionData(DataRefImpl Sec) const {
500   uint32_t Flags = getSectionFlags(Sec);
501   return Flags & (XCOFF::STYP_DATA | XCOFF::STYP_TDATA);
502 }
503 
504 bool XCOFFObjectFile::isSectionBSS(DataRefImpl Sec) const {
505   uint32_t Flags = getSectionFlags(Sec);
506   return Flags & (XCOFF::STYP_BSS | XCOFF::STYP_TBSS);
507 }
508 
509 bool XCOFFObjectFile::isDebugSection(DataRefImpl Sec) const {
510   uint32_t Flags = getSectionFlags(Sec);
511   return Flags & (XCOFF::STYP_DEBUG | XCOFF::STYP_DWARF);
512 }
513 
514 bool XCOFFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
515   return is64Bit() ? toSection64(Sec)->FileOffsetToRawData == 0
516                    : toSection32(Sec)->FileOffsetToRawData == 0;
517 }
518 
519 relocation_iterator XCOFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
520   DataRefImpl Ret;
521   if (is64Bit()) {
522     const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Sec);
523     auto RelocationsOrErr =
524         relocations<XCOFFSectionHeader64, XCOFFRelocation64>(*SectionEntPtr);
525     if (Error E = RelocationsOrErr.takeError()) {
526       // TODO: report the error up the stack.
527       consumeError(std::move(E));
528       return relocation_iterator(RelocationRef());
529     }
530     Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
531   } else {
532     const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
533     auto RelocationsOrErr =
534         relocations<XCOFFSectionHeader32, XCOFFRelocation32>(*SectionEntPtr);
535     if (Error E = RelocationsOrErr.takeError()) {
536       // TODO: report the error up the stack.
537       consumeError(std::move(E));
538       return relocation_iterator(RelocationRef());
539     }
540     Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().begin());
541   }
542   return relocation_iterator(RelocationRef(Ret, this));
543 }
544 
545 relocation_iterator XCOFFObjectFile::section_rel_end(DataRefImpl Sec) const {
546   DataRefImpl Ret;
547   if (is64Bit()) {
548     const XCOFFSectionHeader64 *SectionEntPtr = toSection64(Sec);
549     auto RelocationsOrErr =
550         relocations<XCOFFSectionHeader64, XCOFFRelocation64>(*SectionEntPtr);
551     if (Error E = RelocationsOrErr.takeError()) {
552       // TODO: report the error up the stack.
553       consumeError(std::move(E));
554       return relocation_iterator(RelocationRef());
555     }
556     Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
557   } else {
558     const XCOFFSectionHeader32 *SectionEntPtr = toSection32(Sec);
559     auto RelocationsOrErr =
560         relocations<XCOFFSectionHeader32, XCOFFRelocation32>(*SectionEntPtr);
561     if (Error E = RelocationsOrErr.takeError()) {
562       // TODO: report the error up the stack.
563       consumeError(std::move(E));
564       return relocation_iterator(RelocationRef());
565     }
566     Ret.p = reinterpret_cast<uintptr_t>(&*RelocationsOrErr.get().end());
567   }
568   return relocation_iterator(RelocationRef(Ret, this));
569 }
570 
571 void XCOFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
572   if (is64Bit())
573     Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation64>(Rel.p) + 1);
574   else
575     Rel.p = reinterpret_cast<uintptr_t>(viewAs<XCOFFRelocation32>(Rel.p) + 1);
576 }
577 
578 uint64_t XCOFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
579   if (is64Bit()) {
580     const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p);
581     const XCOFFSectionHeader64 *Sec64 = sectionHeaderTable64();
582     const uint64_t RelocAddress = Reloc->VirtualAddress;
583     const uint16_t NumberOfSections = getNumberOfSections();
584     for (uint16_t I = 0; I < NumberOfSections; ++I) {
585       // Find which section this relocation belongs to, and get the
586       // relocation offset relative to the start of the section.
587       if (Sec64->VirtualAddress <= RelocAddress &&
588           RelocAddress < Sec64->VirtualAddress + Sec64->SectionSize) {
589         return RelocAddress - Sec64->VirtualAddress;
590       }
591       ++Sec64;
592     }
593   } else {
594     const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
595     const XCOFFSectionHeader32 *Sec32 = sectionHeaderTable32();
596     const uint32_t RelocAddress = Reloc->VirtualAddress;
597     const uint16_t NumberOfSections = getNumberOfSections();
598     for (uint16_t I = 0; I < NumberOfSections; ++I) {
599       // Find which section this relocation belongs to, and get the
600       // relocation offset relative to the start of the section.
601       if (Sec32->VirtualAddress <= RelocAddress &&
602           RelocAddress < Sec32->VirtualAddress + Sec32->SectionSize) {
603         return RelocAddress - Sec32->VirtualAddress;
604       }
605       ++Sec32;
606     }
607   }
608   return InvalidRelocOffset;
609 }
610 
611 symbol_iterator XCOFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
612   uint32_t Index;
613   if (is64Bit()) {
614     const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p);
615     Index = Reloc->SymbolIndex;
616 
617     if (Index >= getNumberOfSymbolTableEntries64())
618       return symbol_end();
619   } else {
620     const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
621     Index = Reloc->SymbolIndex;
622 
623     if (Index >= getLogicalNumberOfSymbolTableEntries32())
624       return symbol_end();
625   }
626   DataRefImpl SymDRI;
627   SymDRI.p = getSymbolEntryAddressByIndex(Index);
628   return symbol_iterator(SymbolRef(SymDRI, this));
629 }
630 
631 uint64_t XCOFFObjectFile::getRelocationType(DataRefImpl Rel) const {
632   if (is64Bit())
633     return viewAs<XCOFFRelocation64>(Rel.p)->Type;
634   return viewAs<XCOFFRelocation32>(Rel.p)->Type;
635 }
636 
637 void XCOFFObjectFile::getRelocationTypeName(
638     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
639   StringRef Res;
640   if (is64Bit()) {
641     const XCOFFRelocation64 *Reloc = viewAs<XCOFFRelocation64>(Rel.p);
642     Res = XCOFF::getRelocationTypeString(Reloc->Type);
643   } else {
644     const XCOFFRelocation32 *Reloc = viewAs<XCOFFRelocation32>(Rel.p);
645     Res = XCOFF::getRelocationTypeString(Reloc->Type);
646   }
647   Result.append(Res.begin(), Res.end());
648 }
649 
650 Expected<uint32_t> XCOFFObjectFile::getSymbolFlags(DataRefImpl Symb) const {
651   XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
652   uint32_t Result = SymbolRef::SF_None;
653 
654   if (XCOFFSym.getSectionNumber() == XCOFF::N_ABS)
655     Result |= SymbolRef::SF_Absolute;
656 
657   XCOFF::StorageClass SC = XCOFFSym.getStorageClass();
658   if (XCOFF::C_EXT == SC || XCOFF::C_WEAKEXT == SC)
659     Result |= SymbolRef::SF_Global;
660 
661   if (XCOFF::C_WEAKEXT == SC)
662     Result |= SymbolRef::SF_Weak;
663 
664   if (XCOFFSym.isCsectSymbol()) {
665     Expected<XCOFFCsectAuxRef> CsectAuxEntOrErr =
666         XCOFFSym.getXCOFFCsectAuxRef();
667     if (CsectAuxEntOrErr) {
668       if (CsectAuxEntOrErr.get().getSymbolType() == XCOFF::XTY_CM)
669         Result |= SymbolRef::SF_Common;
670     } else
671       return CsectAuxEntOrErr.takeError();
672   }
673 
674   if (XCOFFSym.getSectionNumber() == XCOFF::N_UNDEF)
675     Result |= SymbolRef::SF_Undefined;
676 
677   // There is no visibility in old 32 bit XCOFF object file interpret.
678   if (is64Bit() || (auxiliaryHeader32() && (auxiliaryHeader32()->getVersion() ==
679                                             NEW_XCOFF_INTERPRET))) {
680     uint16_t SymType = XCOFFSym.getSymbolType();
681     if ((SymType & VISIBILITY_MASK) == SYM_V_HIDDEN)
682       Result |= SymbolRef::SF_Hidden;
683 
684     if ((SymType & VISIBILITY_MASK) == SYM_V_EXPORTED)
685       Result |= SymbolRef::SF_Exported;
686   }
687   return Result;
688 }
689 
690 basic_symbol_iterator XCOFFObjectFile::symbol_begin() const {
691   DataRefImpl SymDRI;
692   SymDRI.p = reinterpret_cast<uintptr_t>(SymbolTblPtr);
693   return basic_symbol_iterator(SymbolRef(SymDRI, this));
694 }
695 
696 basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
697   DataRefImpl SymDRI;
698   const uint32_t NumberOfSymbolTableEntries = getNumberOfSymbolTableEntries();
699   SymDRI.p = getSymbolEntryAddressByIndex(NumberOfSymbolTableEntries);
700   return basic_symbol_iterator(SymbolRef(SymDRI, this));
701 }
702 
703 XCOFFObjectFile::xcoff_symbol_iterator_range XCOFFObjectFile::symbols() const {
704   return xcoff_symbol_iterator_range(symbol_begin(), symbol_end());
705 }
706 
707 section_iterator XCOFFObjectFile::section_begin() const {
708   DataRefImpl DRI;
709   DRI.p = getSectionHeaderTableAddress();
710   return section_iterator(SectionRef(DRI, this));
711 }
712 
713 section_iterator XCOFFObjectFile::section_end() const {
714   DataRefImpl DRI;
715   DRI.p = getWithOffset(getSectionHeaderTableAddress(),
716                         getNumberOfSections() * getSectionHeaderSize());
717   return section_iterator(SectionRef(DRI, this));
718 }
719 
720 uint8_t XCOFFObjectFile::getBytesInAddress() const { return is64Bit() ? 8 : 4; }
721 
722 StringRef XCOFFObjectFile::getFileFormatName() const {
723   return is64Bit() ? "aix5coff64-rs6000" : "aixcoff-rs6000";
724 }
725 
726 Triple::ArchType XCOFFObjectFile::getArch() const {
727   return is64Bit() ? Triple::ppc64 : Triple::ppc;
728 }
729 
730 Expected<SubtargetFeatures> XCOFFObjectFile::getFeatures() const {
731   return SubtargetFeatures();
732 }
733 
734 bool XCOFFObjectFile::isRelocatableObject() const {
735   if (is64Bit())
736     return !(fileHeader64()->Flags & NoRelMask);
737   return !(fileHeader32()->Flags & NoRelMask);
738 }
739 
740 Expected<uint64_t> XCOFFObjectFile::getStartAddress() const {
741   if (AuxiliaryHeader == nullptr)
742     return 0;
743 
744   return is64Bit() ? auxiliaryHeader64()->getEntryPointAddr()
745                    : auxiliaryHeader32()->getEntryPointAddr();
746 }
747 
748 StringRef XCOFFObjectFile::mapDebugSectionName(StringRef Name) const {
749   return StringSwitch<StringRef>(Name)
750       .Case("dwinfo", "debug_info")
751       .Case("dwline", "debug_line")
752       .Case("dwpbnms", "debug_pubnames")
753       .Case("dwpbtyp", "debug_pubtypes")
754       .Case("dwarnge", "debug_aranges")
755       .Case("dwabrev", "debug_abbrev")
756       .Case("dwstr", "debug_str")
757       .Case("dwrnges", "debug_ranges")
758       .Case("dwloc", "debug_loc")
759       .Case("dwframe", "debug_frame")
760       .Case("dwmac", "debug_macinfo")
761       .Default(Name);
762 }
763 
764 size_t XCOFFObjectFile::getFileHeaderSize() const {
765   return is64Bit() ? sizeof(XCOFFFileHeader64) : sizeof(XCOFFFileHeader32);
766 }
767 
768 size_t XCOFFObjectFile::getSectionHeaderSize() const {
769   return is64Bit() ? sizeof(XCOFFSectionHeader64) :
770                      sizeof(XCOFFSectionHeader32);
771 }
772 
773 bool XCOFFObjectFile::is64Bit() const {
774   return Binary::ID_XCOFF64 == getType();
775 }
776 
777 Expected<StringRef> XCOFFObjectFile::getRawData(const char *Start,
778                                                 uint64_t Size,
779                                                 StringRef Name) const {
780   uintptr_t StartPtr = reinterpret_cast<uintptr_t>(Start);
781   // TODO: this path is untested.
782   if (Error E = Binary::checkOffset(Data, StartPtr, Size))
783     return createError(toString(std::move(E)) + ": " + Name.data() +
784                        " data with offset 0x" + Twine::utohexstr(StartPtr) +
785                        " and size 0x" + Twine::utohexstr(Size) +
786                        " goes past the end of the file");
787   return StringRef(Start, Size);
788 }
789 
790 uint16_t XCOFFObjectFile::getMagic() const {
791   return is64Bit() ? fileHeader64()->Magic : fileHeader32()->Magic;
792 }
793 
794 Expected<DataRefImpl> XCOFFObjectFile::getSectionByNum(int16_t Num) const {
795   if (Num <= 0 || Num > getNumberOfSections())
796     return createStringError(object_error::invalid_section_index,
797                              "the section index (" + Twine(Num) +
798                                  ") is invalid");
799 
800   DataRefImpl DRI;
801   DRI.p = getWithOffset(getSectionHeaderTableAddress(),
802                         getSectionHeaderSize() * (Num - 1));
803   return DRI;
804 }
805 
806 DataRefImpl
807 XCOFFObjectFile::getSectionByType(XCOFF::SectionTypeFlags SectType) const {
808   DataRefImpl DRI;
809   auto GetSectionAddr = [&](const auto &Sections) -> uintptr_t {
810     for (const auto &Sec : Sections)
811       if (Sec.getSectionType() == SectType)
812         return reinterpret_cast<uintptr_t>(&Sec);
813     return uintptr_t(0);
814   };
815   if (is64Bit())
816     DRI.p = GetSectionAddr(sections64());
817   else
818     DRI.p = GetSectionAddr(sections32());
819   return DRI;
820 }
821 
822 Expected<StringRef>
823 XCOFFObjectFile::getSymbolSectionName(XCOFFSymbolRef SymEntPtr) const {
824   const int16_t SectionNum = SymEntPtr.getSectionNumber();
825 
826   switch (SectionNum) {
827   case XCOFF::N_DEBUG:
828     return "N_DEBUG";
829   case XCOFF::N_ABS:
830     return "N_ABS";
831   case XCOFF::N_UNDEF:
832     return "N_UNDEF";
833   default:
834     Expected<DataRefImpl> SecRef = getSectionByNum(SectionNum);
835     if (SecRef)
836       return generateXCOFFFixedNameStringRef(
837           getSectionNameInternal(SecRef.get()));
838     return SecRef.takeError();
839   }
840 }
841 
842 unsigned XCOFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
843   XCOFFSymbolRef XCOFFSymRef(Sym.getRawDataRefImpl(), this);
844   return XCOFFSymRef.getSectionNumber();
845 }
846 
847 bool XCOFFObjectFile::isReservedSectionNumber(int16_t SectionNumber) {
848   return (SectionNumber <= 0 && SectionNumber >= -2);
849 }
850 
851 uint16_t XCOFFObjectFile::getNumberOfSections() const {
852   return is64Bit() ? fileHeader64()->NumberOfSections
853                    : fileHeader32()->NumberOfSections;
854 }
855 
856 int32_t XCOFFObjectFile::getTimeStamp() const {
857   return is64Bit() ? fileHeader64()->TimeStamp : fileHeader32()->TimeStamp;
858 }
859 
860 uint16_t XCOFFObjectFile::getOptionalHeaderSize() const {
861   return is64Bit() ? fileHeader64()->AuxHeaderSize
862                    : fileHeader32()->AuxHeaderSize;
863 }
864 
865 uint32_t XCOFFObjectFile::getSymbolTableOffset32() const {
866   return fileHeader32()->SymbolTableOffset;
867 }
868 
869 int32_t XCOFFObjectFile::getRawNumberOfSymbolTableEntries32() const {
870   // As far as symbol table size is concerned, if this field is negative it is
871   // to be treated as a 0. However since this field is also used for printing we
872   // don't want to truncate any negative values.
873   return fileHeader32()->NumberOfSymTableEntries;
874 }
875 
876 uint32_t XCOFFObjectFile::getLogicalNumberOfSymbolTableEntries32() const {
877   return (fileHeader32()->NumberOfSymTableEntries >= 0
878               ? fileHeader32()->NumberOfSymTableEntries
879               : 0);
880 }
881 
882 uint64_t XCOFFObjectFile::getSymbolTableOffset64() const {
883   return fileHeader64()->SymbolTableOffset;
884 }
885 
886 uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries64() const {
887   return fileHeader64()->NumberOfSymTableEntries;
888 }
889 
890 uint32_t XCOFFObjectFile::getNumberOfSymbolTableEntries() const {
891   return is64Bit() ? getNumberOfSymbolTableEntries64()
892                    : getLogicalNumberOfSymbolTableEntries32();
893 }
894 
895 uintptr_t XCOFFObjectFile::getEndOfSymbolTableAddress() const {
896   const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries();
897   return getWithOffset(reinterpret_cast<uintptr_t>(SymbolTblPtr),
898                        XCOFF::SymbolTableEntrySize * NumberOfSymTableEntries);
899 }
900 
901 void XCOFFObjectFile::checkSymbolEntryPointer(uintptr_t SymbolEntPtr) const {
902   if (SymbolEntPtr < reinterpret_cast<uintptr_t>(SymbolTblPtr))
903     report_fatal_error("Symbol table entry is outside of symbol table.");
904 
905   if (SymbolEntPtr >= getEndOfSymbolTableAddress())
906     report_fatal_error("Symbol table entry is outside of symbol table.");
907 
908   ptrdiff_t Offset = reinterpret_cast<const char *>(SymbolEntPtr) -
909                      reinterpret_cast<const char *>(SymbolTblPtr);
910 
911   if (Offset % XCOFF::SymbolTableEntrySize != 0)
912     report_fatal_error(
913         "Symbol table entry position is not valid inside of symbol table.");
914 }
915 
916 uint32_t XCOFFObjectFile::getSymbolIndex(uintptr_t SymbolEntPtr) const {
917   return (reinterpret_cast<const char *>(SymbolEntPtr) -
918           reinterpret_cast<const char *>(SymbolTblPtr)) /
919          XCOFF::SymbolTableEntrySize;
920 }
921 
922 uint64_t XCOFFObjectFile::getSymbolSize(DataRefImpl Symb) const {
923   uint64_t Result = 0;
924   XCOFFSymbolRef XCOFFSym = toSymbolRef(Symb);
925   if (XCOFFSym.isCsectSymbol()) {
926     Expected<XCOFFCsectAuxRef> CsectAuxRefOrError =
927         XCOFFSym.getXCOFFCsectAuxRef();
928     if (!CsectAuxRefOrError)
929       // TODO: report the error up the stack.
930       consumeError(CsectAuxRefOrError.takeError());
931     else {
932       XCOFFCsectAuxRef CsectAuxRef = CsectAuxRefOrError.get();
933       uint8_t SymType = CsectAuxRef.getSymbolType();
934       if (SymType == XCOFF::XTY_SD || SymType == XCOFF::XTY_CM)
935         Result = CsectAuxRef.getSectionOrLength();
936     }
937   }
938   return Result;
939 }
940 
941 uintptr_t XCOFFObjectFile::getSymbolEntryAddressByIndex(uint32_t Index) const {
942   return getAdvancedSymbolEntryAddress(
943       reinterpret_cast<uintptr_t>(getPointerToSymbolTable()), Index);
944 }
945 
946 Expected<StringRef>
947 XCOFFObjectFile::getSymbolNameByIndex(uint32_t Index) const {
948   const uint32_t NumberOfSymTableEntries = getNumberOfSymbolTableEntries();
949 
950   if (Index >= NumberOfSymTableEntries)
951     return createError("symbol index " + Twine(Index) +
952                        " exceeds symbol count " +
953                        Twine(NumberOfSymTableEntries));
954 
955   DataRefImpl SymDRI;
956   SymDRI.p = getSymbolEntryAddressByIndex(Index);
957   return getSymbolName(SymDRI);
958 }
959 
960 uint16_t XCOFFObjectFile::getFlags() const {
961   return is64Bit() ? fileHeader64()->Flags : fileHeader32()->Flags;
962 }
963 
964 const char *XCOFFObjectFile::getSectionNameInternal(DataRefImpl Sec) const {
965   return is64Bit() ? toSection64(Sec)->Name : toSection32(Sec)->Name;
966 }
967 
968 uintptr_t XCOFFObjectFile::getSectionHeaderTableAddress() const {
969   return reinterpret_cast<uintptr_t>(SectionHeaderTable);
970 }
971 
972 int32_t XCOFFObjectFile::getSectionFlags(DataRefImpl Sec) const {
973   return is64Bit() ? toSection64(Sec)->Flags : toSection32(Sec)->Flags;
974 }
975 
976 XCOFFObjectFile::XCOFFObjectFile(unsigned int Type, MemoryBufferRef Object)
977     : ObjectFile(Type, Object) {
978   assert(Type == Binary::ID_XCOFF32 || Type == Binary::ID_XCOFF64);
979 }
980 
981 ArrayRef<XCOFFSectionHeader64> XCOFFObjectFile::sections64() const {
982   assert(is64Bit() && "64-bit interface called for non 64-bit file.");
983   const XCOFFSectionHeader64 *TablePtr = sectionHeaderTable64();
984   return ArrayRef<XCOFFSectionHeader64>(TablePtr,
985                                         TablePtr + getNumberOfSections());
986 }
987 
988 ArrayRef<XCOFFSectionHeader32> XCOFFObjectFile::sections32() const {
989   assert(!is64Bit() && "32-bit interface called for non 32-bit file.");
990   const XCOFFSectionHeader32 *TablePtr = sectionHeaderTable32();
991   return ArrayRef<XCOFFSectionHeader32>(TablePtr,
992                                         TablePtr + getNumberOfSections());
993 }
994 
995 // In an XCOFF32 file, when the field value is 65535, then an STYP_OVRFLO
996 // section header contains the actual count of relocation entries in the s_paddr
997 // field. STYP_OVRFLO headers contain the section index of their corresponding
998 // sections as their raw "NumberOfRelocations" field value.
999 template <typename T>
1000 Expected<uint32_t> XCOFFObjectFile::getNumberOfRelocationEntries(
1001     const XCOFFSectionHeader<T> &Sec) const {
1002   const T &Section = static_cast<const T &>(Sec);
1003   if (is64Bit())
1004     return Section.NumberOfRelocations;
1005 
1006   uint16_t SectionIndex = &Section - sectionHeaderTable<T>() + 1;
1007   if (Section.NumberOfRelocations < XCOFF::RelocOverflow)
1008     return Section.NumberOfRelocations;
1009   for (const auto &Sec : sections32()) {
1010     if (Sec.Flags == XCOFF::STYP_OVRFLO &&
1011         Sec.NumberOfRelocations == SectionIndex)
1012       return Sec.PhysicalAddress;
1013   }
1014   return errorCodeToError(object_error::parse_failed);
1015 }
1016 
1017 template <typename Shdr, typename Reloc>
1018 Expected<ArrayRef<Reloc>> XCOFFObjectFile::relocations(const Shdr &Sec) const {
1019   uintptr_t RelocAddr = getWithOffset(reinterpret_cast<uintptr_t>(FileHeader),
1020                                       Sec.FileOffsetToRelocationInfo);
1021   auto NumRelocEntriesOrErr = getNumberOfRelocationEntries(Sec);
1022   if (Error E = NumRelocEntriesOrErr.takeError())
1023     return std::move(E);
1024 
1025   uint32_t NumRelocEntries = NumRelocEntriesOrErr.get();
1026   static_assert((sizeof(Reloc) == XCOFF::RelocationSerializationSize64 ||
1027                  sizeof(Reloc) == XCOFF::RelocationSerializationSize32),
1028                 "Relocation structure is incorrect");
1029   auto RelocationOrErr =
1030       getObject<Reloc>(Data, reinterpret_cast<void *>(RelocAddr),
1031                        NumRelocEntries * sizeof(Reloc));
1032   if (!RelocationOrErr)
1033     return createError(
1034         toString(RelocationOrErr.takeError()) + ": relocations with offset 0x" +
1035         Twine::utohexstr(Sec.FileOffsetToRelocationInfo) + " and size 0x" +
1036         Twine::utohexstr(NumRelocEntries * sizeof(Reloc)) +
1037         " go past the end of the file");
1038 
1039   const Reloc *StartReloc = RelocationOrErr.get();
1040 
1041   return ArrayRef<Reloc>(StartReloc, StartReloc + NumRelocEntries);
1042 }
1043 
1044 template <typename ExceptEnt>
1045 Expected<ArrayRef<ExceptEnt>> XCOFFObjectFile::getExceptionEntries() const {
1046   assert((is64Bit() && sizeof(ExceptEnt) == sizeof(ExceptionSectionEntry64)) ||
1047          (!is64Bit() && sizeof(ExceptEnt) == sizeof(ExceptionSectionEntry32)));
1048 
1049   Expected<uintptr_t> ExceptionSectOrErr =
1050       getSectionFileOffsetToRawData(XCOFF::STYP_EXCEPT);
1051   if (!ExceptionSectOrErr)
1052     return ExceptionSectOrErr.takeError();
1053 
1054   DataRefImpl DRI = getSectionByType(XCOFF::STYP_EXCEPT);
1055   if (DRI.p == 0)
1056     return ArrayRef<ExceptEnt>();
1057 
1058   ExceptEnt *ExceptEntStart =
1059       reinterpret_cast<ExceptEnt *>(*ExceptionSectOrErr);
1060   return ArrayRef<ExceptEnt>(
1061       ExceptEntStart, ExceptEntStart + getSectionSize(DRI) / sizeof(ExceptEnt));
1062 }
1063 
1064 template LLVM_EXPORT_TEMPLATE Expected<ArrayRef<ExceptionSectionEntry32>>
1065 XCOFFObjectFile::getExceptionEntries() const;
1066 template LLVM_EXPORT_TEMPLATE Expected<ArrayRef<ExceptionSectionEntry64>>
1067 XCOFFObjectFile::getExceptionEntries() const;
1068 
1069 Expected<XCOFFStringTable>
1070 XCOFFObjectFile::parseStringTable(const XCOFFObjectFile *Obj, uint64_t Offset) {
1071   // If there is a string table, then the buffer must contain at least 4 bytes
1072   // for the string table's size. Not having a string table is not an error.
1073   if (Error E = Binary::checkOffset(
1074           Obj->Data, reinterpret_cast<uintptr_t>(Obj->base() + Offset), 4)) {
1075     consumeError(std::move(E));
1076     return XCOFFStringTable{0, nullptr};
1077   }
1078 
1079   // Read the size out of the buffer.
1080   uint32_t Size = support::endian::read32be(Obj->base() + Offset);
1081 
1082   // If the size is less then 4, then the string table is just a size and no
1083   // string data.
1084   if (Size <= 4)
1085     return XCOFFStringTable{4, nullptr};
1086 
1087   auto StringTableOrErr =
1088       getObject<char>(Obj->Data, Obj->base() + Offset, Size);
1089   if (!StringTableOrErr)
1090     return createError(toString(StringTableOrErr.takeError()) +
1091                        ": string table with offset 0x" +
1092                        Twine::utohexstr(Offset) + " and size 0x" +
1093                        Twine::utohexstr(Size) +
1094                        " goes past the end of the file");
1095 
1096   const char *StringTablePtr = StringTableOrErr.get();
1097   if (StringTablePtr[Size - 1] != '\0')
1098     return errorCodeToError(object_error::string_table_non_null_end);
1099 
1100   return XCOFFStringTable{Size, StringTablePtr};
1101 }
1102 
1103 // This function returns the import file table. Each entry in the import file
1104 // table consists of: "path_name\0base_name\0archive_member_name\0".
1105 Expected<StringRef> XCOFFObjectFile::getImportFileTable() const {
1106   Expected<uintptr_t> LoaderSectionAddrOrError =
1107       getSectionFileOffsetToRawData(XCOFF::STYP_LOADER);
1108   if (!LoaderSectionAddrOrError)
1109     return LoaderSectionAddrOrError.takeError();
1110 
1111   uintptr_t LoaderSectionAddr = LoaderSectionAddrOrError.get();
1112   if (!LoaderSectionAddr)
1113     return StringRef();
1114 
1115   uint64_t OffsetToImportFileTable = 0;
1116   uint64_t LengthOfImportFileTable = 0;
1117   if (is64Bit()) {
1118     const LoaderSectionHeader64 *LoaderSec64 =
1119         viewAs<LoaderSectionHeader64>(LoaderSectionAddr);
1120     OffsetToImportFileTable = LoaderSec64->OffsetToImpid;
1121     LengthOfImportFileTable = LoaderSec64->LengthOfImpidStrTbl;
1122   } else {
1123     const LoaderSectionHeader32 *LoaderSec32 =
1124         viewAs<LoaderSectionHeader32>(LoaderSectionAddr);
1125     OffsetToImportFileTable = LoaderSec32->OffsetToImpid;
1126     LengthOfImportFileTable = LoaderSec32->LengthOfImpidStrTbl;
1127   }
1128 
1129   auto ImportTableOrErr = getObject<char>(
1130       Data,
1131       reinterpret_cast<void *>(LoaderSectionAddr + OffsetToImportFileTable),
1132       LengthOfImportFileTable);
1133   if (!ImportTableOrErr)
1134     return createError(
1135         toString(ImportTableOrErr.takeError()) +
1136         ": import file table with offset 0x" +
1137         Twine::utohexstr(LoaderSectionAddr + OffsetToImportFileTable) +
1138         " and size 0x" + Twine::utohexstr(LengthOfImportFileTable) +
1139         " goes past the end of the file");
1140 
1141   const char *ImportTablePtr = ImportTableOrErr.get();
1142   if (ImportTablePtr[LengthOfImportFileTable - 1] != '\0')
1143     return createError(
1144         ": import file name table with offset 0x" +
1145         Twine::utohexstr(LoaderSectionAddr + OffsetToImportFileTable) +
1146         " and size 0x" + Twine::utohexstr(LengthOfImportFileTable) +
1147         " must end with a null terminator");
1148 
1149   return StringRef(ImportTablePtr, LengthOfImportFileTable);
1150 }
1151 
1152 Expected<std::unique_ptr<XCOFFObjectFile>>
1153 XCOFFObjectFile::create(unsigned Type, MemoryBufferRef MBR) {
1154   // Can't use std::make_unique because of the private constructor.
1155   std::unique_ptr<XCOFFObjectFile> Obj;
1156   Obj.reset(new XCOFFObjectFile(Type, MBR));
1157 
1158   uint64_t CurOffset = 0;
1159   const auto *Base = Obj->base();
1160   MemoryBufferRef Data = Obj->Data;
1161 
1162   // Parse file header.
1163   auto FileHeaderOrErr =
1164       getObject<void>(Data, Base + CurOffset, Obj->getFileHeaderSize());
1165   if (Error E = FileHeaderOrErr.takeError())
1166     return std::move(E);
1167   Obj->FileHeader = FileHeaderOrErr.get();
1168 
1169   CurOffset += Obj->getFileHeaderSize();
1170 
1171   if (Obj->getOptionalHeaderSize()) {
1172     auto AuxiliaryHeaderOrErr =
1173         getObject<void>(Data, Base + CurOffset, Obj->getOptionalHeaderSize());
1174     if (Error E = AuxiliaryHeaderOrErr.takeError())
1175       return std::move(E);
1176     Obj->AuxiliaryHeader = AuxiliaryHeaderOrErr.get();
1177   }
1178 
1179   CurOffset += Obj->getOptionalHeaderSize();
1180 
1181   // Parse the section header table if it is present.
1182   if (Obj->getNumberOfSections()) {
1183     uint64_t SectionHeadersSize =
1184         Obj->getNumberOfSections() * Obj->getSectionHeaderSize();
1185     auto SecHeadersOrErr =
1186         getObject<void>(Data, Base + CurOffset, SectionHeadersSize);
1187     if (!SecHeadersOrErr)
1188       return createError(toString(SecHeadersOrErr.takeError()) +
1189                          ": section headers with offset 0x" +
1190                          Twine::utohexstr(CurOffset) + " and size 0x" +
1191                          Twine::utohexstr(SectionHeadersSize) +
1192                          " go past the end of the file");
1193 
1194     Obj->SectionHeaderTable = SecHeadersOrErr.get();
1195   }
1196 
1197   const uint32_t NumberOfSymbolTableEntries =
1198       Obj->getNumberOfSymbolTableEntries();
1199 
1200   // If there is no symbol table we are done parsing the memory buffer.
1201   if (NumberOfSymbolTableEntries == 0)
1202     return std::move(Obj);
1203 
1204   // Parse symbol table.
1205   CurOffset = Obj->is64Bit() ? Obj->getSymbolTableOffset64()
1206                              : Obj->getSymbolTableOffset32();
1207   const uint64_t SymbolTableSize =
1208       static_cast<uint64_t>(XCOFF::SymbolTableEntrySize) *
1209       NumberOfSymbolTableEntries;
1210   auto SymTableOrErr =
1211       getObject<void *>(Data, Base + CurOffset, SymbolTableSize);
1212   if (!SymTableOrErr)
1213     return createError(
1214         toString(SymTableOrErr.takeError()) + ": symbol table with offset 0x" +
1215         Twine::utohexstr(CurOffset) + " and size 0x" +
1216         Twine::utohexstr(SymbolTableSize) + " goes past the end of the file");
1217 
1218   Obj->SymbolTblPtr = SymTableOrErr.get();
1219   CurOffset += SymbolTableSize;
1220 
1221   // Parse String table.
1222   Expected<XCOFFStringTable> StringTableOrErr =
1223       parseStringTable(Obj.get(), CurOffset);
1224   if (Error E = StringTableOrErr.takeError())
1225     return std::move(E);
1226   Obj->StringTable = StringTableOrErr.get();
1227 
1228   return std::move(Obj);
1229 }
1230 
1231 Expected<std::unique_ptr<ObjectFile>>
1232 ObjectFile::createXCOFFObjectFile(MemoryBufferRef MemBufRef,
1233                                   unsigned FileType) {
1234   return XCOFFObjectFile::create(FileType, MemBufRef);
1235 }
1236 
1237 std::optional<StringRef> XCOFFObjectFile::tryGetCPUName() const {
1238   return StringRef("future");
1239 }
1240 
1241 Expected<bool> XCOFFSymbolRef::isFunction() const {
1242   if (!isCsectSymbol())
1243     return false;
1244 
1245   if (getSymbolType() & FunctionSym)
1246     return true;
1247 
1248   Expected<XCOFFCsectAuxRef> ExpCsectAuxEnt = getXCOFFCsectAuxRef();
1249   if (!ExpCsectAuxEnt)
1250     return ExpCsectAuxEnt.takeError();
1251 
1252   const XCOFFCsectAuxRef CsectAuxRef = ExpCsectAuxEnt.get();
1253 
1254   if (CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_PR &&
1255       CsectAuxRef.getStorageMappingClass() != XCOFF::XMC_GL)
1256     return false;
1257 
1258   // A function definition should not be a common type symbol or an external
1259   // symbol.
1260   if (CsectAuxRef.getSymbolType() == XCOFF::XTY_CM ||
1261       CsectAuxRef.getSymbolType() == XCOFF::XTY_ER)
1262     return false;
1263 
1264   // If the next symbol is an XTY_LD type symbol with the same address, this
1265   // XTY_SD symbol is not a function. Otherwise this is a function symbol for
1266   // -ffunction-sections.
1267   if (CsectAuxRef.getSymbolType() == XCOFF::XTY_SD) {
1268     // If this is a csect with size 0, it won't be a function definition.
1269     // This is used to work around the fact that LLVM always generates below
1270     // symbol for -ffunction-sections:
1271     // m   0x00000000     .text     1  unamex                    **No Symbol**
1272     // a4  0x00000000       0    0     SD       PR    0    0
1273     // FIXME: remove or replace this meaningless symbol.
1274     if (getSize() == 0)
1275       return false;
1276 
1277     xcoff_symbol_iterator NextIt(this);
1278     // If this is the last main symbol table entry, there won't be an XTY_LD
1279     // type symbol below.
1280     if (++NextIt == getObject()->symbol_end())
1281       return true;
1282 
1283     if (cantFail(getAddress()) != cantFail(NextIt->getAddress()))
1284       return true;
1285 
1286     // Check next symbol is XTY_LD. If so, this symbol is not a function.
1287     Expected<XCOFFCsectAuxRef> NextCsectAuxEnt = NextIt->getXCOFFCsectAuxRef();
1288     if (!NextCsectAuxEnt)
1289       return NextCsectAuxEnt.takeError();
1290 
1291     if (NextCsectAuxEnt.get().getSymbolType() == XCOFF::XTY_LD)
1292       return false;
1293 
1294     return true;
1295   }
1296 
1297   if (CsectAuxRef.getSymbolType() == XCOFF::XTY_LD)
1298     return true;
1299 
1300   return createError(
1301       "symbol csect aux entry with index " +
1302       Twine(getObject()->getSymbolIndex(CsectAuxRef.getEntryAddress())) +
1303       " has invalid symbol type " +
1304       Twine::utohexstr(CsectAuxRef.getSymbolType()));
1305 }
1306 
1307 bool XCOFFSymbolRef::isCsectSymbol() const {
1308   XCOFF::StorageClass SC = getStorageClass();
1309   return (SC == XCOFF::C_EXT || SC == XCOFF::C_WEAKEXT ||
1310           SC == XCOFF::C_HIDEXT);
1311 }
1312 
1313 Expected<XCOFFCsectAuxRef> XCOFFSymbolRef::getXCOFFCsectAuxRef() const {
1314   assert(isCsectSymbol() &&
1315          "Calling csect symbol interface with a non-csect symbol.");
1316 
1317   uint8_t NumberOfAuxEntries = getNumberOfAuxEntries();
1318 
1319   Expected<StringRef> NameOrErr = getName();
1320   if (auto Err = NameOrErr.takeError())
1321     return std::move(Err);
1322 
1323   uint32_t SymbolIdx = getObject()->getSymbolIndex(getEntryAddress());
1324   if (!NumberOfAuxEntries) {
1325     return createError("csect symbol \"" + *NameOrErr + "\" with index " +
1326                        Twine(SymbolIdx) + " contains no auxiliary entry");
1327   }
1328 
1329   if (!getObject()->is64Bit()) {
1330     // In XCOFF32, the csect auxilliary entry is always the last auxiliary
1331     // entry for the symbol.
1332     uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
1333         getEntryAddress(), NumberOfAuxEntries);
1334     return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt32>(AuxAddr));
1335   }
1336 
1337   // XCOFF64 uses SymbolAuxType to identify the auxiliary entry type.
1338   // We need to iterate through all the auxiliary entries to find it.
1339   for (uint8_t Index = NumberOfAuxEntries; Index > 0; --Index) {
1340     uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
1341         getEntryAddress(), Index);
1342     if (*getObject()->getSymbolAuxType(AuxAddr) ==
1343         XCOFF::SymbolAuxType::AUX_CSECT) {
1344 #ifndef NDEBUG
1345       getObject()->checkSymbolEntryPointer(AuxAddr);
1346 #endif
1347       return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt64>(AuxAddr));
1348     }
1349   }
1350 
1351   return createError(
1352       "a csect auxiliary entry has not been found for symbol \"" + *NameOrErr +
1353       "\" with index " + Twine(SymbolIdx));
1354 }
1355 
1356 Expected<StringRef> XCOFFSymbolRef::getName() const {
1357   // A storage class value with the high-order bit on indicates that the name is
1358   // a symbolic debugger stabstring.
1359   if (getStorageClass() & 0x80)
1360     return StringRef("Unimplemented Debug Name");
1361 
1362   if (!getObject()->is64Bit()) {
1363     if (getSymbol32()->NameInStrTbl.Magic !=
1364         XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
1365       return generateXCOFFFixedNameStringRef(getSymbol32()->SymbolName);
1366 
1367     return getObject()->getStringTableEntry(getSymbol32()->NameInStrTbl.Offset);
1368   }
1369 
1370   return getObject()->getStringTableEntry(getSymbol64()->Offset);
1371 }
1372 
1373 // Explicitly instantiate template classes.
1374 template struct XCOFFSectionHeader<XCOFFSectionHeader32>;
1375 template struct XCOFFSectionHeader<XCOFFSectionHeader64>;
1376 
1377 template struct XCOFFRelocation<llvm::support::ubig32_t>;
1378 template struct XCOFFRelocation<llvm::support::ubig64_t>;
1379 
1380 template LLVM_EXPORT_TEMPLATE
1381     llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation64>>
1382     llvm::object::XCOFFObjectFile::relocations<
1383         llvm::object::XCOFFSectionHeader64, llvm::object::XCOFFRelocation64>(
1384         llvm::object::XCOFFSectionHeader64 const &) const;
1385 template LLVM_EXPORT_TEMPLATE
1386     llvm::Expected<llvm::ArrayRef<llvm::object::XCOFFRelocation32>>
1387     llvm::object::XCOFFObjectFile::relocations<
1388         llvm::object::XCOFFSectionHeader32, llvm::object::XCOFFRelocation32>(
1389         llvm::object::XCOFFSectionHeader32 const &) const;
1390 
1391 bool doesXCOFFTracebackTableBegin(ArrayRef<uint8_t> Bytes) {
1392   if (Bytes.size() < 4)
1393     return false;
1394 
1395   return support::endian::read32be(Bytes.data()) == 0;
1396 }
1397 
1398 #define GETVALUEWITHMASK(X) (Data & (TracebackTable::X))
1399 #define GETVALUEWITHMASKSHIFT(X, S)                                            \
1400   ((Data & (TracebackTable::X)) >> (TracebackTable::S))
1401 
1402 Expected<TBVectorExt> TBVectorExt::create(StringRef TBvectorStrRef) {
1403   Error Err = Error::success();
1404   TBVectorExt TBTVecExt(TBvectorStrRef, Err);
1405   if (Err)
1406     return std::move(Err);
1407   return TBTVecExt;
1408 }
1409 
1410 TBVectorExt::TBVectorExt(StringRef TBvectorStrRef, Error &Err) {
1411   const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(TBvectorStrRef.data());
1412   Data = support::endian::read16be(Ptr);
1413   uint32_t VecParmsTypeValue = support::endian::read32be(Ptr + 2);
1414   unsigned ParmsNum =
1415       GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask, NumberOfVectorParmsShift);
1416 
1417   ErrorAsOutParameter EAO(Err);
1418   Expected<SmallString<32>> VecParmsTypeOrError =
1419       parseVectorParmsType(VecParmsTypeValue, ParmsNum);
1420   if (!VecParmsTypeOrError)
1421     Err = VecParmsTypeOrError.takeError();
1422   else
1423     VecParmsInfo = VecParmsTypeOrError.get();
1424 }
1425 
1426 uint8_t TBVectorExt::getNumberOfVRSaved() const {
1427   return GETVALUEWITHMASKSHIFT(NumberOfVRSavedMask, NumberOfVRSavedShift);
1428 }
1429 
1430 bool TBVectorExt::isVRSavedOnStack() const {
1431   return GETVALUEWITHMASK(IsVRSavedOnStackMask);
1432 }
1433 
1434 bool TBVectorExt::hasVarArgs() const {
1435   return GETVALUEWITHMASK(HasVarArgsMask);
1436 }
1437 
1438 uint8_t TBVectorExt::getNumberOfVectorParms() const {
1439   return GETVALUEWITHMASKSHIFT(NumberOfVectorParmsMask,
1440                                NumberOfVectorParmsShift);
1441 }
1442 
1443 bool TBVectorExt::hasVMXInstruction() const {
1444   return GETVALUEWITHMASK(HasVMXInstructionMask);
1445 }
1446 #undef GETVALUEWITHMASK
1447 #undef GETVALUEWITHMASKSHIFT
1448 
1449 Expected<XCOFFTracebackTable>
1450 XCOFFTracebackTable::create(const uint8_t *Ptr, uint64_t &Size, bool Is64Bit) {
1451   Error Err = Error::success();
1452   XCOFFTracebackTable TBT(Ptr, Size, Err, Is64Bit);
1453   if (Err)
1454     return std::move(Err);
1455   return TBT;
1456 }
1457 
1458 XCOFFTracebackTable::XCOFFTracebackTable(const uint8_t *Ptr, uint64_t &Size,
1459                                          Error &Err, bool Is64Bit)
1460     : TBPtr(Ptr), Is64BitObj(Is64Bit) {
1461   ErrorAsOutParameter EAO(Err);
1462   DataExtractor DE(ArrayRef<uint8_t>(Ptr, Size), /*IsLittleEndian=*/false,
1463                    /*AddressSize=*/0);
1464   DataExtractor::Cursor Cur(/*Offset=*/0);
1465 
1466   // Skip 8 bytes of mandatory fields.
1467   DE.getU64(Cur);
1468 
1469   unsigned FixedParmsNum = getNumberOfFixedParms();
1470   unsigned FloatingParmsNum = getNumberOfFPParms();
1471   uint32_t ParamsTypeValue = 0;
1472 
1473   // Begin to parse optional fields.
1474   if (Cur && (FixedParmsNum + FloatingParmsNum) > 0)
1475     ParamsTypeValue = DE.getU32(Cur);
1476 
1477   if (Cur && hasTraceBackTableOffset())
1478     TraceBackTableOffset = DE.getU32(Cur);
1479 
1480   if (Cur && isInterruptHandler())
1481     HandlerMask = DE.getU32(Cur);
1482 
1483   if (Cur && hasControlledStorage()) {
1484     NumOfCtlAnchors = DE.getU32(Cur);
1485     if (Cur && NumOfCtlAnchors) {
1486       SmallVector<uint32_t, 8> Disp;
1487       Disp.reserve(*NumOfCtlAnchors);
1488       for (uint32_t I = 0; I < NumOfCtlAnchors && Cur; ++I)
1489         Disp.push_back(DE.getU32(Cur));
1490       if (Cur)
1491         ControlledStorageInfoDisp = std::move(Disp);
1492     }
1493   }
1494 
1495   if (Cur && isFuncNamePresent()) {
1496     uint16_t FunctionNameLen = DE.getU16(Cur);
1497     if (Cur)
1498       FunctionName = DE.getBytes(Cur, FunctionNameLen);
1499   }
1500 
1501   if (Cur && isAllocaUsed())
1502     AllocaRegister = DE.getU8(Cur);
1503 
1504   unsigned VectorParmsNum = 0;
1505   if (Cur && hasVectorInfo()) {
1506     StringRef VectorExtRef = DE.getBytes(Cur, 6);
1507     if (Cur) {
1508       Expected<TBVectorExt> TBVecExtOrErr = TBVectorExt::create(VectorExtRef);
1509       if (!TBVecExtOrErr) {
1510         Err = TBVecExtOrErr.takeError();
1511         return;
1512       }
1513       VecExt = TBVecExtOrErr.get();
1514       VectorParmsNum = VecExt->getNumberOfVectorParms();
1515       // Skip two bytes of padding after vector info.
1516       DE.skip(Cur, 2);
1517     }
1518   }
1519 
1520   // As long as there is no fixed-point or floating-point parameter, this
1521   // field remains not present even when hasVectorInfo gives true and
1522   // indicates the presence of vector parameters.
1523   if (Cur && (FixedParmsNum + FloatingParmsNum) > 0) {
1524     Expected<SmallString<32>> ParmsTypeOrError =
1525         hasVectorInfo()
1526             ? parseParmsTypeWithVecInfo(ParamsTypeValue, FixedParmsNum,
1527                                         FloatingParmsNum, VectorParmsNum)
1528             : parseParmsType(ParamsTypeValue, FixedParmsNum, FloatingParmsNum);
1529 
1530     if (!ParmsTypeOrError) {
1531       Err = ParmsTypeOrError.takeError();
1532       return;
1533     }
1534     ParmsType = ParmsTypeOrError.get();
1535   }
1536 
1537   if (Cur && hasExtensionTable()) {
1538     ExtensionTable = DE.getU8(Cur);
1539 
1540     if (*ExtensionTable & ExtendedTBTableFlag::TB_EH_INFO) {
1541       // eh_info displacement must be 4-byte aligned.
1542       Cur.seek(alignTo(Cur.tell(), 4));
1543       EhInfoDisp = Is64BitObj ? DE.getU64(Cur) : DE.getU32(Cur);
1544     }
1545   }
1546   if (!Cur)
1547     Err = Cur.takeError();
1548 
1549   Size = Cur.tell();
1550 }
1551 
1552 #define GETBITWITHMASK(P, X)                                                   \
1553   (support::endian::read32be(TBPtr + (P)) & (TracebackTable::X))
1554 #define GETBITWITHMASKSHIFT(P, X, S)                                           \
1555   ((support::endian::read32be(TBPtr + (P)) & (TracebackTable::X)) >>           \
1556    (TracebackTable::S))
1557 
1558 uint8_t XCOFFTracebackTable::getVersion() const {
1559   return GETBITWITHMASKSHIFT(0, VersionMask, VersionShift);
1560 }
1561 
1562 uint8_t XCOFFTracebackTable::getLanguageID() const {
1563   return GETBITWITHMASKSHIFT(0, LanguageIdMask, LanguageIdShift);
1564 }
1565 
1566 bool XCOFFTracebackTable::isGlobalLinkage() const {
1567   return GETBITWITHMASK(0, IsGlobaLinkageMask);
1568 }
1569 
1570 bool XCOFFTracebackTable::isOutOfLineEpilogOrPrologue() const {
1571   return GETBITWITHMASK(0, IsOutOfLineEpilogOrPrologueMask);
1572 }
1573 
1574 bool XCOFFTracebackTable::hasTraceBackTableOffset() const {
1575   return GETBITWITHMASK(0, HasTraceBackTableOffsetMask);
1576 }
1577 
1578 bool XCOFFTracebackTable::isInternalProcedure() const {
1579   return GETBITWITHMASK(0, IsInternalProcedureMask);
1580 }
1581 
1582 bool XCOFFTracebackTable::hasControlledStorage() const {
1583   return GETBITWITHMASK(0, HasControlledStorageMask);
1584 }
1585 
1586 bool XCOFFTracebackTable::isTOCless() const {
1587   return GETBITWITHMASK(0, IsTOClessMask);
1588 }
1589 
1590 bool XCOFFTracebackTable::isFloatingPointPresent() const {
1591   return GETBITWITHMASK(0, IsFloatingPointPresentMask);
1592 }
1593 
1594 bool XCOFFTracebackTable::isFloatingPointOperationLogOrAbortEnabled() const {
1595   return GETBITWITHMASK(0, IsFloatingPointOperationLogOrAbortEnabledMask);
1596 }
1597 
1598 bool XCOFFTracebackTable::isInterruptHandler() const {
1599   return GETBITWITHMASK(0, IsInterruptHandlerMask);
1600 }
1601 
1602 bool XCOFFTracebackTable::isFuncNamePresent() const {
1603   return GETBITWITHMASK(0, IsFunctionNamePresentMask);
1604 }
1605 
1606 bool XCOFFTracebackTable::isAllocaUsed() const {
1607   return GETBITWITHMASK(0, IsAllocaUsedMask);
1608 }
1609 
1610 uint8_t XCOFFTracebackTable::getOnConditionDirective() const {
1611   return GETBITWITHMASKSHIFT(0, OnConditionDirectiveMask,
1612                              OnConditionDirectiveShift);
1613 }
1614 
1615 bool XCOFFTracebackTable::isCRSaved() const {
1616   return GETBITWITHMASK(0, IsCRSavedMask);
1617 }
1618 
1619 bool XCOFFTracebackTable::isLRSaved() const {
1620   return GETBITWITHMASK(0, IsLRSavedMask);
1621 }
1622 
1623 bool XCOFFTracebackTable::isBackChainStored() const {
1624   return GETBITWITHMASK(4, IsBackChainStoredMask);
1625 }
1626 
1627 bool XCOFFTracebackTable::isFixup() const {
1628   return GETBITWITHMASK(4, IsFixupMask);
1629 }
1630 
1631 uint8_t XCOFFTracebackTable::getNumOfFPRsSaved() const {
1632   return GETBITWITHMASKSHIFT(4, FPRSavedMask, FPRSavedShift);
1633 }
1634 
1635 bool XCOFFTracebackTable::hasExtensionTable() const {
1636   return GETBITWITHMASK(4, HasExtensionTableMask);
1637 }
1638 
1639 bool XCOFFTracebackTable::hasVectorInfo() const {
1640   return GETBITWITHMASK(4, HasVectorInfoMask);
1641 }
1642 
1643 uint8_t XCOFFTracebackTable::getNumOfGPRsSaved() const {
1644   return GETBITWITHMASKSHIFT(4, GPRSavedMask, GPRSavedShift);
1645 }
1646 
1647 uint8_t XCOFFTracebackTable::getNumberOfFixedParms() const {
1648   return GETBITWITHMASKSHIFT(4, NumberOfFixedParmsMask,
1649                              NumberOfFixedParmsShift);
1650 }
1651 
1652 uint8_t XCOFFTracebackTable::getNumberOfFPParms() const {
1653   return GETBITWITHMASKSHIFT(4, NumberOfFloatingPointParmsMask,
1654                              NumberOfFloatingPointParmsShift);
1655 }
1656 
1657 bool XCOFFTracebackTable::hasParmsOnStack() const {
1658   return GETBITWITHMASK(4, HasParmsOnStackMask);
1659 }
1660 
1661 #undef GETBITWITHMASK
1662 #undef GETBITWITHMASKSHIFT
1663 } // namespace object
1664 } // namespace llvm
1665