COFFObjectFile.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the COFFObjectFile class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Object/COFF.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/Support/COFF.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <cctype>
  22. #include <limits>
  23. using namespace llvm;
  24. using namespace object;
  25. using support::ulittle8_t;
  26. using support::ulittle16_t;
  27. using support::ulittle32_t;
  28. using support::little16_t;
  29. // Returns false if size is greater than the buffer size. And sets ec.
  30. static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
  31. if (M->getBufferSize() < Size) {
  32. EC = object_error::unexpected_eof;
  33. return false;
  34. }
  35. return true;
  36. }
  37. // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
  38. // Returns unexpected_eof if error.
  39. template<typename T>
  40. static error_code getObject(const T *&Obj, const MemoryBuffer *M,
  41. const uint8_t *Ptr, const size_t Size = sizeof(T)) {
  42. uintptr_t Addr = uintptr_t(Ptr);
  43. if (Addr + Size < Addr ||
  44. Addr + Size < Size ||
  45. Addr + Size > uintptr_t(M->getBufferEnd())) {
  46. return object_error::unexpected_eof;
  47. }
  48. Obj = reinterpret_cast<const T *>(Addr);
  49. return object_error::success;
  50. }
  51. // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
  52. // prefixed slashes.
  53. static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
  54. assert(Str.size() <= 6 && "String too long, possible overflow.");
  55. if (Str.size() > 6)
  56. return true;
  57. uint64_t Value = 0;
  58. while (!Str.empty()) {
  59. unsigned CharVal;
  60. if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
  61. CharVal = Str[0] - 'A';
  62. else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
  63. CharVal = Str[0] - 'a' + 26;
  64. else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
  65. CharVal = Str[0] - '0' + 52;
  66. else if (Str[0] == '+') // 62
  67. CharVal = 62;
  68. else if (Str[0] == '/') // 63
  69. CharVal = 63;
  70. else
  71. return true;
  72. Value = (Value * 64) + CharVal;
  73. Str = Str.substr(1);
  74. }
  75. if (Value > std::numeric_limits<uint32_t>::max())
  76. return true;
  77. Result = static_cast<uint32_t>(Value);
  78. return false;
  79. }
  80. const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
  81. const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
  82. # ifndef NDEBUG
  83. // Verify that the symbol points to a valid entry in the symbol table.
  84. uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
  85. if (Offset < COFFHeader->PointerToSymbolTable
  86. || Offset >= COFFHeader->PointerToSymbolTable
  87. + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
  88. report_fatal_error("Symbol was outside of symbol table.");
  89. assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
  90. == 0 && "Symbol did not point to the beginning of a symbol");
  91. # endif
  92. return Addr;
  93. }
  94. const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
  95. const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
  96. # ifndef NDEBUG
  97. // Verify that the section points to a valid entry in the section table.
  98. if (Addr < SectionTable
  99. || Addr >= (SectionTable + COFFHeader->NumberOfSections))
  100. report_fatal_error("Section was outside of section table.");
  101. uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
  102. assert(Offset % sizeof(coff_section) == 0 &&
  103. "Section did not point to the beginning of a section");
  104. # endif
  105. return Addr;
  106. }
  107. void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
  108. const coff_symbol *Symb = toSymb(Ref);
  109. Symb += 1 + Symb->NumberOfAuxSymbols;
  110. Ref.p = reinterpret_cast<uintptr_t>(Symb);
  111. }
  112. error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
  113. StringRef &Result) const {
  114. const coff_symbol *Symb = toSymb(Ref);
  115. return getSymbolName(Symb, Result);
  116. }
  117. error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
  118. uint64_t &Result) const {
  119. const coff_symbol *Symb = toSymb(Ref);
  120. const coff_section *Section = nullptr;
  121. if (error_code EC = getSection(Symb->SectionNumber, Section))
  122. return EC;
  123. if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
  124. Result = UnknownAddressOrSize;
  125. else if (Section)
  126. Result = Section->VirtualAddress + Symb->Value;
  127. else
  128. Result = Symb->Value;
  129. return object_error::success;
  130. }
  131. error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
  132. SymbolRef::Type &Result) const {
  133. const coff_symbol *Symb = toSymb(Ref);
  134. Result = SymbolRef::ST_Other;
  135. if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
  136. Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
  137. Result = SymbolRef::ST_Unknown;
  138. } else if (Symb->isFunctionDefinition()) {
  139. Result = SymbolRef::ST_Function;
  140. } else {
  141. uint32_t Characteristics = 0;
  142. if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
  143. const coff_section *Section = nullptr;
  144. if (error_code EC = getSection(Symb->SectionNumber, Section))
  145. return EC;
  146. Characteristics = Section->Characteristics;
  147. }
  148. if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
  149. ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
  150. Result = SymbolRef::ST_Data;
  151. }
  152. return object_error::success;
  153. }
  154. uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
  155. const coff_symbol *Symb = toSymb(Ref);
  156. uint32_t Result = SymbolRef::SF_None;
  157. // TODO: Correctly set SF_FormatSpecific, SF_Common
  158. if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
  159. if (Symb->Value == 0)
  160. Result |= SymbolRef::SF_Undefined;
  161. else
  162. Result |= SymbolRef::SF_Common;
  163. }
  164. // TODO: This are certainly too restrictive.
  165. if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
  166. Result |= SymbolRef::SF_Global;
  167. if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
  168. Result |= SymbolRef::SF_Weak;
  169. if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
  170. Result |= SymbolRef::SF_Absolute;
  171. return Result;
  172. }
  173. error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
  174. uint64_t &Result) const {
  175. // FIXME: Return the correct size. This requires looking at all the symbols
  176. // in the same section as this symbol, and looking for either the next
  177. // symbol, or the end of the section.
  178. const coff_symbol *Symb = toSymb(Ref);
  179. const coff_section *Section = nullptr;
  180. if (error_code EC = getSection(Symb->SectionNumber, Section))
  181. return EC;
  182. if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
  183. Result = UnknownAddressOrSize;
  184. else if (Section)
  185. Result = Section->SizeOfRawData - Symb->Value;
  186. else
  187. Result = 0;
  188. return object_error::success;
  189. }
  190. error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
  191. section_iterator &Result) const {
  192. const coff_symbol *Symb = toSymb(Ref);
  193. if (COFF::isReservedSectionNumber(Symb->SectionNumber)) {
  194. Result = section_end();
  195. } else {
  196. const coff_section *Sec = nullptr;
  197. if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
  198. DataRefImpl Ref;
  199. Ref.p = reinterpret_cast<uintptr_t>(Sec);
  200. Result = section_iterator(SectionRef(Ref, this));
  201. }
  202. return object_error::success;
  203. }
  204. void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
  205. const coff_section *Sec = toSec(Ref);
  206. Sec += 1;
  207. Ref.p = reinterpret_cast<uintptr_t>(Sec);
  208. }
  209. error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
  210. StringRef &Result) const {
  211. const coff_section *Sec = toSec(Ref);
  212. return getSectionName(Sec, Result);
  213. }
  214. error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
  215. uint64_t &Result) const {
  216. const coff_section *Sec = toSec(Ref);
  217. Result = Sec->VirtualAddress;
  218. return object_error::success;
  219. }
  220. error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
  221. uint64_t &Result) const {
  222. const coff_section *Sec = toSec(Ref);
  223. Result = Sec->SizeOfRawData;
  224. return object_error::success;
  225. }
  226. error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
  227. StringRef &Result) const {
  228. const coff_section *Sec = toSec(Ref);
  229. ArrayRef<uint8_t> Res;
  230. error_code EC = getSectionContents(Sec, Res);
  231. Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
  232. return EC;
  233. }
  234. error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
  235. uint64_t &Res) const {
  236. const coff_section *Sec = toSec(Ref);
  237. if (!Sec)
  238. return object_error::parse_failed;
  239. Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
  240. return object_error::success;
  241. }
  242. error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
  243. bool &Result) const {
  244. const coff_section *Sec = toSec(Ref);
  245. Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
  246. return object_error::success;
  247. }
  248. error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
  249. bool &Result) const {
  250. const coff_section *Sec = toSec(Ref);
  251. Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
  252. return object_error::success;
  253. }
  254. error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
  255. bool &Result) const {
  256. const coff_section *Sec = toSec(Ref);
  257. Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
  258. return object_error::success;
  259. }
  260. error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
  261. bool &Result) const {
  262. // FIXME: Unimplemented
  263. Result = true;
  264. return object_error::success;
  265. }
  266. error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
  267. bool &Result) const {
  268. const coff_section *Sec = toSec(Ref);
  269. Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
  270. return object_error::success;
  271. }
  272. error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
  273. bool &Result) const {
  274. // FIXME: Unimplemented.
  275. Result = false;
  276. return object_error::success;
  277. }
  278. error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
  279. bool &Result) const {
  280. // FIXME: Unimplemented.
  281. Result = false;
  282. return object_error::success;
  283. }
  284. error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
  285. DataRefImpl SymbRef,
  286. bool &Result) const {
  287. const coff_section *Sec = toSec(SecRef);
  288. const coff_symbol *Symb = toSymb(SymbRef);
  289. const coff_section *SymbSec = nullptr;
  290. if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
  291. if (SymbSec == Sec)
  292. Result = true;
  293. else
  294. Result = false;
  295. return object_error::success;
  296. }
  297. relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
  298. const coff_section *Sec = toSec(Ref);
  299. DataRefImpl Ret;
  300. if (Sec->NumberOfRelocations == 0) {
  301. Ret.p = 0;
  302. } else {
  303. auto begin = reinterpret_cast<const coff_relocation*>(
  304. base() + Sec->PointerToRelocations);
  305. if (Sec->hasExtendedRelocations()) {
  306. // Skip the first relocation entry repurposed to store the number of
  307. // relocations.
  308. begin++;
  309. }
  310. Ret.p = reinterpret_cast<uintptr_t>(begin);
  311. }
  312. return relocation_iterator(RelocationRef(Ret, this));
  313. }
  314. static uint32_t getNumberOfRelocations(const coff_section *Sec,
  315. const uint8_t *base) {
  316. // The field for the number of relocations in COFF section table is only
  317. // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
  318. // NumberOfRelocations field, and the actual relocation count is stored in the
  319. // VirtualAddress field in the first relocation entry.
  320. if (Sec->hasExtendedRelocations()) {
  321. auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
  322. base + Sec->PointerToRelocations);
  323. return FirstReloc->VirtualAddress;
  324. }
  325. return Sec->NumberOfRelocations;
  326. }
  327. relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
  328. const coff_section *Sec = toSec(Ref);
  329. DataRefImpl Ret;
  330. if (Sec->NumberOfRelocations == 0) {
  331. Ret.p = 0;
  332. } else {
  333. auto begin = reinterpret_cast<const coff_relocation*>(
  334. base() + Sec->PointerToRelocations);
  335. uint32_t NumReloc = getNumberOfRelocations(Sec, base());
  336. Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
  337. }
  338. return relocation_iterator(RelocationRef(Ret, this));
  339. }
  340. // Initialize the pointer to the symbol table.
  341. error_code COFFObjectFile::initSymbolTablePtr() {
  342. if (error_code EC = getObject(
  343. SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
  344. COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
  345. return EC;
  346. // Find string table. The first four byte of the string table contains the
  347. // total size of the string table, including the size field itself. If the
  348. // string table is empty, the value of the first four byte would be 4.
  349. const uint8_t *StringTableAddr =
  350. base() + COFFHeader->PointerToSymbolTable +
  351. COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
  352. const ulittle32_t *StringTableSizePtr;
  353. if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
  354. return EC;
  355. StringTableSize = *StringTableSizePtr;
  356. if (error_code EC =
  357. getObject(StringTable, Data, StringTableAddr, StringTableSize))
  358. return EC;
  359. // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
  360. // tools like cvtres write a size of 0 for an empty table instead of 4.
  361. if (StringTableSize < 4)
  362. StringTableSize = 4;
  363. // Check that the string table is null terminated if has any in it.
  364. if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
  365. return object_error::parse_failed;
  366. return object_error::success;
  367. }
  368. // Returns the file offset for the given VA.
  369. error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
  370. uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
  371. : (uint64_t)PE32PlusHeader->ImageBase;
  372. uint64_t Rva = Addr - ImageBase;
  373. assert(Rva <= UINT32_MAX);
  374. return getRvaPtr((uint32_t)Rva, Res);
  375. }
  376. // Returns the file offset for the given RVA.
  377. error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
  378. for (const SectionRef &S : sections()) {
  379. const coff_section *Section = getCOFFSection(S);
  380. uint32_t SectionStart = Section->VirtualAddress;
  381. uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
  382. if (SectionStart <= Addr && Addr < SectionEnd) {
  383. uint32_t Offset = Addr - SectionStart;
  384. Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
  385. return object_error::success;
  386. }
  387. }
  388. return object_error::parse_failed;
  389. }
  390. // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
  391. // table entry.
  392. error_code COFFObjectFile::
  393. getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
  394. uintptr_t IntPtr = 0;
  395. if (error_code EC = getRvaPtr(Rva, IntPtr))
  396. return EC;
  397. const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
  398. Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
  399. Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
  400. return object_error::success;
  401. }
  402. // Find the import table.
  403. error_code COFFObjectFile::initImportTablePtr() {
  404. // First, we get the RVA of the import table. If the file lacks a pointer to
  405. // the import table, do nothing.
  406. const data_directory *DataEntry;
  407. if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
  408. return object_error::success;
  409. // Do nothing if the pointer to import table is NULL.
  410. if (DataEntry->RelativeVirtualAddress == 0)
  411. return object_error::success;
  412. uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
  413. NumberOfImportDirectory = DataEntry->Size /
  414. sizeof(import_directory_table_entry);
  415. // Find the section that contains the RVA. This is needed because the RVA is
  416. // the import table's memory address which is different from its file offset.
  417. uintptr_t IntPtr = 0;
  418. if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
  419. return EC;
  420. ImportDirectory = reinterpret_cast<
  421. const import_directory_table_entry *>(IntPtr);
  422. return object_error::success;
  423. }
  424. // Find the export table.
  425. error_code COFFObjectFile::initExportTablePtr() {
  426. // First, we get the RVA of the export table. If the file lacks a pointer to
  427. // the export table, do nothing.
  428. const data_directory *DataEntry;
  429. if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
  430. return object_error::success;
  431. // Do nothing if the pointer to export table is NULL.
  432. if (DataEntry->RelativeVirtualAddress == 0)
  433. return object_error::success;
  434. uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
  435. uintptr_t IntPtr = 0;
  436. if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
  437. return EC;
  438. ExportDirectory =
  439. reinterpret_cast<const export_directory_table_entry *>(IntPtr);
  440. return object_error::success;
  441. }
  442. COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
  443. bool BufferOwned)
  444. : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(nullptr),
  445. PE32Header(nullptr), PE32PlusHeader(nullptr), DataDirectory(nullptr),
  446. SectionTable(nullptr), SymbolTable(nullptr), StringTable(nullptr),
  447. StringTableSize(0), ImportDirectory(nullptr), NumberOfImportDirectory(0),
  448. ExportDirectory(nullptr) {
  449. // Check that we at least have enough room for a header.
  450. if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
  451. // The current location in the file where we are looking at.
  452. uint64_t CurPtr = 0;
  453. // PE header is optional and is present only in executables. If it exists,
  454. // it is placed right after COFF header.
  455. bool HasPEHeader = false;
  456. // Check if this is a PE/COFF file.
  457. if (base()[0] == 0x4d && base()[1] == 0x5a) {
  458. // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
  459. // PE signature to find 'normal' COFF header.
  460. if (!checkSize(Data, EC, 0x3c + 8)) return;
  461. CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
  462. // Check the PE magic bytes. ("PE\0\0")
  463. if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
  464. EC = object_error::parse_failed;
  465. return;
  466. }
  467. CurPtr += 4; // Skip the PE magic bytes.
  468. HasPEHeader = true;
  469. }
  470. if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
  471. return;
  472. CurPtr += sizeof(coff_file_header);
  473. if (HasPEHeader) {
  474. const pe32_header *Header;
  475. if ((EC = getObject(Header, Data, base() + CurPtr)))
  476. return;
  477. const uint8_t *DataDirAddr;
  478. uint64_t DataDirSize;
  479. if (Header->Magic == 0x10b) {
  480. PE32Header = Header;
  481. DataDirAddr = base() + CurPtr + sizeof(pe32_header);
  482. DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
  483. } else if (Header->Magic == 0x20b) {
  484. PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
  485. DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
  486. DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
  487. } else {
  488. // It's neither PE32 nor PE32+.
  489. EC = object_error::parse_failed;
  490. return;
  491. }
  492. if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
  493. return;
  494. CurPtr += COFFHeader->SizeOfOptionalHeader;
  495. }
  496. if (COFFHeader->isImportLibrary())
  497. return;
  498. if ((EC = getObject(SectionTable, Data, base() + CurPtr,
  499. COFFHeader->NumberOfSections * sizeof(coff_section))))
  500. return;
  501. // Initialize the pointer to the symbol table.
  502. if (COFFHeader->PointerToSymbolTable != 0)
  503. if ((EC = initSymbolTablePtr()))
  504. return;
  505. // Initialize the pointer to the beginning of the import table.
  506. if ((EC = initImportTablePtr()))
  507. return;
  508. // Initialize the pointer to the export table.
  509. if ((EC = initExportTablePtr()))
  510. return;
  511. EC = object_error::success;
  512. }
  513. basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
  514. DataRefImpl Ret;
  515. Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
  516. return basic_symbol_iterator(SymbolRef(Ret, this));
  517. }
  518. basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
  519. // The symbol table ends where the string table begins.
  520. DataRefImpl Ret;
  521. Ret.p = reinterpret_cast<uintptr_t>(StringTable);
  522. return basic_symbol_iterator(SymbolRef(Ret, this));
  523. }
  524. library_iterator COFFObjectFile::needed_library_begin() const {
  525. // TODO: implement
  526. report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
  527. }
  528. library_iterator COFFObjectFile::needed_library_end() const {
  529. // TODO: implement
  530. report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
  531. }
  532. StringRef COFFObjectFile::getLoadName() const {
  533. // COFF does not have this field.
  534. return "";
  535. }
  536. import_directory_iterator COFFObjectFile::import_directory_begin() const {
  537. return import_directory_iterator(
  538. ImportDirectoryEntryRef(ImportDirectory, 0, this));
  539. }
  540. import_directory_iterator COFFObjectFile::import_directory_end() const {
  541. return import_directory_iterator(
  542. ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
  543. }
  544. export_directory_iterator COFFObjectFile::export_directory_begin() const {
  545. return export_directory_iterator(
  546. ExportDirectoryEntryRef(ExportDirectory, 0, this));
  547. }
  548. export_directory_iterator COFFObjectFile::export_directory_end() const {
  549. if (!ExportDirectory)
  550. return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
  551. ExportDirectoryEntryRef Ref(ExportDirectory,
  552. ExportDirectory->AddressTableEntries, this);
  553. return export_directory_iterator(Ref);
  554. }
  555. section_iterator COFFObjectFile::section_begin() const {
  556. DataRefImpl Ret;
  557. Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
  558. return section_iterator(SectionRef(Ret, this));
  559. }
  560. section_iterator COFFObjectFile::section_end() const {
  561. DataRefImpl Ret;
  562. int NumSections = COFFHeader->isImportLibrary()
  563. ? 0 : COFFHeader->NumberOfSections;
  564. Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
  565. return section_iterator(SectionRef(Ret, this));
  566. }
  567. uint8_t COFFObjectFile::getBytesInAddress() const {
  568. return getArch() == Triple::x86_64 ? 8 : 4;
  569. }
  570. StringRef COFFObjectFile::getFileFormatName() const {
  571. switch(COFFHeader->Machine) {
  572. case COFF::IMAGE_FILE_MACHINE_I386:
  573. return "COFF-i386";
  574. case COFF::IMAGE_FILE_MACHINE_AMD64:
  575. return "COFF-x86-64";
  576. case COFF::IMAGE_FILE_MACHINE_ARMNT:
  577. return "COFF-ARM";
  578. default:
  579. return "COFF-<unknown arch>";
  580. }
  581. }
  582. unsigned COFFObjectFile::getArch() const {
  583. switch(COFFHeader->Machine) {
  584. case COFF::IMAGE_FILE_MACHINE_I386:
  585. return Triple::x86;
  586. case COFF::IMAGE_FILE_MACHINE_AMD64:
  587. return Triple::x86_64;
  588. case COFF::IMAGE_FILE_MACHINE_ARMNT:
  589. return Triple::thumb;
  590. default:
  591. return Triple::UnknownArch;
  592. }
  593. }
  594. // This method is kept here because lld uses this. As soon as we make
  595. // lld to use getCOFFHeader, this method will be removed.
  596. error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
  597. return getCOFFHeader(Res);
  598. }
  599. error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
  600. Res = COFFHeader;
  601. return object_error::success;
  602. }
  603. error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
  604. Res = PE32Header;
  605. return object_error::success;
  606. }
  607. error_code
  608. COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
  609. Res = PE32PlusHeader;
  610. return object_error::success;
  611. }
  612. error_code COFFObjectFile::getDataDirectory(uint32_t Index,
  613. const data_directory *&Res) const {
  614. // Error if if there's no data directory or the index is out of range.
  615. if (!DataDirectory)
  616. return object_error::parse_failed;
  617. assert(PE32Header || PE32PlusHeader);
  618. uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
  619. : PE32PlusHeader->NumberOfRvaAndSize;
  620. if (Index > NumEnt)
  621. return object_error::parse_failed;
  622. Res = &DataDirectory[Index];
  623. return object_error::success;
  624. }
  625. error_code COFFObjectFile::getSection(int32_t Index,
  626. const coff_section *&Result) const {
  627. // Check for special index values.
  628. if (COFF::isReservedSectionNumber(Index))
  629. Result = nullptr;
  630. else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
  631. // We already verified the section table data, so no need to check again.
  632. Result = SectionTable + (Index - 1);
  633. else
  634. return object_error::parse_failed;
  635. return object_error::success;
  636. }
  637. error_code COFFObjectFile::getString(uint32_t Offset,
  638. StringRef &Result) const {
  639. if (StringTableSize <= 4)
  640. // Tried to get a string from an empty string table.
  641. return object_error::parse_failed;
  642. if (Offset >= StringTableSize)
  643. return object_error::unexpected_eof;
  644. Result = StringRef(StringTable + Offset);
  645. return object_error::success;
  646. }
  647. error_code COFFObjectFile::getSymbol(uint32_t Index,
  648. const coff_symbol *&Result) const {
  649. if (Index < COFFHeader->NumberOfSymbols)
  650. Result = SymbolTable + Index;
  651. else
  652. return object_error::parse_failed;
  653. return object_error::success;
  654. }
  655. error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
  656. StringRef &Res) const {
  657. // Check for string table entry. First 4 bytes are 0.
  658. if (Symbol->Name.Offset.Zeroes == 0) {
  659. uint32_t Offset = Symbol->Name.Offset.Offset;
  660. if (error_code EC = getString(Offset, Res))
  661. return EC;
  662. return object_error::success;
  663. }
  664. if (Symbol->Name.ShortName[7] == 0)
  665. // Null terminated, let ::strlen figure out the length.
  666. Res = StringRef(Symbol->Name.ShortName);
  667. else
  668. // Not null terminated, use all 8 bytes.
  669. Res = StringRef(Symbol->Name.ShortName, 8);
  670. return object_error::success;
  671. }
  672. ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
  673. const coff_symbol *Symbol) const {
  674. const uint8_t *Aux = nullptr;
  675. if (Symbol->NumberOfAuxSymbols > 0) {
  676. // AUX data comes immediately after the symbol in COFF
  677. Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
  678. # ifndef NDEBUG
  679. // Verify that the Aux symbol points to a valid entry in the symbol table.
  680. uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
  681. if (Offset < COFFHeader->PointerToSymbolTable
  682. || Offset >= COFFHeader->PointerToSymbolTable
  683. + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
  684. report_fatal_error("Aux Symbol data was outside of symbol table.");
  685. assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
  686. == 0 && "Aux Symbol data did not point to the beginning of a symbol");
  687. # endif
  688. }
  689. return ArrayRef<uint8_t>(Aux,
  690. Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
  691. }
  692. error_code COFFObjectFile::getSectionName(const coff_section *Sec,
  693. StringRef &Res) const {
  694. StringRef Name;
  695. if (Sec->Name[7] == 0)
  696. // Null terminated, let ::strlen figure out the length.
  697. Name = Sec->Name;
  698. else
  699. // Not null terminated, use all 8 bytes.
  700. Name = StringRef(Sec->Name, 8);
  701. // Check for string table entry. First byte is '/'.
  702. if (Name[0] == '/') {
  703. uint32_t Offset;
  704. if (Name[1] == '/') {
  705. if (decodeBase64StringEntry(Name.substr(2), Offset))
  706. return object_error::parse_failed;
  707. } else {
  708. if (Name.substr(1).getAsInteger(10, Offset))
  709. return object_error::parse_failed;
  710. }
  711. if (error_code EC = getString(Offset, Name))
  712. return EC;
  713. }
  714. Res = Name;
  715. return object_error::success;
  716. }
  717. error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
  718. ArrayRef<uint8_t> &Res) const {
  719. // The only thing that we need to verify is that the contents is contained
  720. // within the file bounds. We don't need to make sure it doesn't cover other
  721. // data, as there's nothing that says that is not allowed.
  722. uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
  723. uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
  724. if (ConEnd > uintptr_t(Data->getBufferEnd()))
  725. return object_error::parse_failed;
  726. Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
  727. Sec->SizeOfRawData);
  728. return object_error::success;
  729. }
  730. const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
  731. return reinterpret_cast<const coff_relocation*>(Rel.p);
  732. }
  733. void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
  734. Rel.p = reinterpret_cast<uintptr_t>(
  735. reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
  736. }
  737. error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
  738. uint64_t &Res) const {
  739. report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
  740. }
  741. error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
  742. uint64_t &Res) const {
  743. Res = toRel(Rel)->VirtualAddress;
  744. return object_error::success;
  745. }
  746. symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
  747. const coff_relocation* R = toRel(Rel);
  748. DataRefImpl Ref;
  749. Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
  750. return symbol_iterator(SymbolRef(Ref, this));
  751. }
  752. error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
  753. uint64_t &Res) const {
  754. const coff_relocation* R = toRel(Rel);
  755. Res = R->Type;
  756. return object_error::success;
  757. }
  758. const coff_section *
  759. COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
  760. return toSec(Section.getRawDataRefImpl());
  761. }
  762. const coff_symbol *
  763. COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
  764. return toSymb(Symbol.getRawDataRefImpl());
  765. }
  766. const coff_relocation *
  767. COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
  768. return toRel(Reloc.getRawDataRefImpl());
  769. }
  770. #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
  771. case COFF::reloc_type: \
  772. Res = #reloc_type; \
  773. break;
  774. error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
  775. SmallVectorImpl<char> &Result) const {
  776. const coff_relocation *Reloc = toRel(Rel);
  777. StringRef Res;
  778. switch (COFFHeader->Machine) {
  779. case COFF::IMAGE_FILE_MACHINE_AMD64:
  780. switch (Reloc->Type) {
  781. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
  782. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
  783. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
  784. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
  785. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
  786. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
  787. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
  788. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
  789. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
  790. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
  791. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
  792. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
  793. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
  794. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
  795. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
  796. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
  797. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
  798. default:
  799. Res = "Unknown";
  800. }
  801. break;
  802. case COFF::IMAGE_FILE_MACHINE_ARMNT:
  803. switch (Reloc->Type) {
  804. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
  805. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
  806. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
  807. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
  808. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
  809. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
  810. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
  811. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
  812. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
  813. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
  814. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
  815. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
  816. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
  817. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
  818. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
  819. default:
  820. Res = "Unknown";
  821. }
  822. break;
  823. case COFF::IMAGE_FILE_MACHINE_I386:
  824. switch (Reloc->Type) {
  825. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
  826. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
  827. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
  828. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
  829. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
  830. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
  831. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
  832. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
  833. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
  834. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
  835. LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
  836. default:
  837. Res = "Unknown";
  838. }
  839. break;
  840. default:
  841. Res = "Unknown";
  842. }
  843. Result.append(Res.begin(), Res.end());
  844. return object_error::success;
  845. }
  846. #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
  847. error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
  848. SmallVectorImpl<char> &Result) const {
  849. const coff_relocation *Reloc = toRel(Rel);
  850. const coff_symbol *Symb = nullptr;
  851. if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
  852. DataRefImpl Sym;
  853. Sym.p = reinterpret_cast<uintptr_t>(Symb);
  854. StringRef SymName;
  855. if (error_code EC = getSymbolName(Sym, SymName)) return EC;
  856. Result.append(SymName.begin(), SymName.end());
  857. return object_error::success;
  858. }
  859. error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
  860. LibraryRef &Result) const {
  861. report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
  862. }
  863. error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
  864. StringRef &Result) const {
  865. report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
  866. }
  867. bool ImportDirectoryEntryRef::
  868. operator==(const ImportDirectoryEntryRef &Other) const {
  869. return ImportTable == Other.ImportTable && Index == Other.Index;
  870. }
  871. void ImportDirectoryEntryRef::moveNext() {
  872. ++Index;
  873. }
  874. error_code ImportDirectoryEntryRef::
  875. getImportTableEntry(const import_directory_table_entry *&Result) const {
  876. Result = ImportTable;
  877. return object_error::success;
  878. }
  879. error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
  880. uintptr_t IntPtr = 0;
  881. if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
  882. return EC;
  883. Result = StringRef(reinterpret_cast<const char *>(IntPtr));
  884. return object_error::success;
  885. }
  886. error_code ImportDirectoryEntryRef::getImportLookupEntry(
  887. const import_lookup_table_entry32 *&Result) const {
  888. uintptr_t IntPtr = 0;
  889. if (error_code EC =
  890. OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
  891. return EC;
  892. Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
  893. return object_error::success;
  894. }
  895. bool ExportDirectoryEntryRef::
  896. operator==(const ExportDirectoryEntryRef &Other) const {
  897. return ExportTable == Other.ExportTable && Index == Other.Index;
  898. }
  899. void ExportDirectoryEntryRef::moveNext() {
  900. ++Index;
  901. }
  902. // Returns the name of the current export symbol. If the symbol is exported only
  903. // by ordinal, the empty string is set as a result.
  904. error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
  905. uintptr_t IntPtr = 0;
  906. if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
  907. return EC;
  908. Result = StringRef(reinterpret_cast<const char *>(IntPtr));
  909. return object_error::success;
  910. }
  911. // Returns the starting ordinal number.
  912. error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
  913. Result = ExportTable->OrdinalBase;
  914. return object_error::success;
  915. }
  916. // Returns the export ordinal of the current export symbol.
  917. error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
  918. Result = ExportTable->OrdinalBase + Index;
  919. return object_error::success;
  920. }
  921. // Returns the address of the current export symbol.
  922. error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
  923. uintptr_t IntPtr = 0;
  924. if (error_code EC = OwningObject->getRvaPtr(
  925. ExportTable->ExportAddressTableRVA, IntPtr))
  926. return EC;
  927. const export_address_table_entry *entry =
  928. reinterpret_cast<const export_address_table_entry *>(IntPtr);
  929. Result = entry[Index].ExportRVA;
  930. return object_error::success;
  931. }
  932. // Returns the name of the current export symbol. If the symbol is exported only
  933. // by ordinal, the empty string is set as a result.
  934. error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
  935. uintptr_t IntPtr = 0;
  936. if (error_code EC = OwningObject->getRvaPtr(
  937. ExportTable->OrdinalTableRVA, IntPtr))
  938. return EC;
  939. const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
  940. uint32_t NumEntries = ExportTable->NumberOfNamePointers;
  941. int Offset = 0;
  942. for (const ulittle16_t *I = Start, *E = Start + NumEntries;
  943. I < E; ++I, ++Offset) {
  944. if (*I != Index)
  945. continue;
  946. if (error_code EC = OwningObject->getRvaPtr(
  947. ExportTable->NamePointerRVA, IntPtr))
  948. return EC;
  949. const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
  950. if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
  951. return EC;
  952. Result = StringRef(reinterpret_cast<const char *>(IntPtr));
  953. return object_error::success;
  954. }
  955. Result = "";
  956. return object_error::success;
  957. }
  958. ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
  959. bool BufferOwned) {
  960. error_code EC;
  961. std::unique_ptr<COFFObjectFile> Ret(
  962. new COFFObjectFile(Object, EC, BufferOwned));
  963. if (EC)
  964. return EC;
  965. return Ret.release();
  966. }