XCOFFObjectFile.cpp 55 KB

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