COFFImportFile.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. //===- COFFImportFile.cpp - COFF short import file implementation ---------===//
  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 defines the writeImportLibrary function.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Object/COFFImportFile.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/Object/Archive.h"
  16. #include "llvm/Object/ArchiveWriter.h"
  17. #include "llvm/Object/COFF.h"
  18. #include "llvm/Support/Error.h"
  19. #include "llvm/Support/Path.h"
  20. #include <cstdint>
  21. #include <string>
  22. #include <vector>
  23. using namespace llvm::COFF;
  24. using namespace llvm::object;
  25. using namespace llvm;
  26. namespace llvm {
  27. namespace object {
  28. static bool is32bit(MachineTypes Machine) {
  29. switch (Machine) {
  30. default:
  31. llvm_unreachable("unsupported machine");
  32. case IMAGE_FILE_MACHINE_ARM64:
  33. case IMAGE_FILE_MACHINE_AMD64:
  34. return false;
  35. case IMAGE_FILE_MACHINE_ARMNT:
  36. case IMAGE_FILE_MACHINE_I386:
  37. return true;
  38. }
  39. }
  40. static uint16_t getImgRelRelocation(MachineTypes Machine) {
  41. switch (Machine) {
  42. default:
  43. llvm_unreachable("unsupported machine");
  44. case IMAGE_FILE_MACHINE_AMD64:
  45. return IMAGE_REL_AMD64_ADDR32NB;
  46. case IMAGE_FILE_MACHINE_ARMNT:
  47. return IMAGE_REL_ARM_ADDR32NB;
  48. case IMAGE_FILE_MACHINE_ARM64:
  49. return IMAGE_REL_ARM64_ADDR32NB;
  50. case IMAGE_FILE_MACHINE_I386:
  51. return IMAGE_REL_I386_DIR32NB;
  52. }
  53. }
  54. template <class T> static void append(std::vector<uint8_t> &B, const T &Data) {
  55. size_t S = B.size();
  56. B.resize(S + sizeof(T));
  57. memcpy(&B[S], &Data, sizeof(T));
  58. }
  59. static void writeStringTable(std::vector<uint8_t> &B,
  60. ArrayRef<const std::string> Strings) {
  61. // The COFF string table consists of a 4-byte value which is the size of the
  62. // table, including the length field itself. This value is followed by the
  63. // string content itself, which is an array of null-terminated C-style
  64. // strings. The termination is important as they are referenced to by offset
  65. // by the symbol entity in the file format.
  66. size_t Pos = B.size();
  67. size_t Offset = B.size();
  68. // Skip over the length field, we will fill it in later as we will have
  69. // computed the length while emitting the string content itself.
  70. Pos += sizeof(uint32_t);
  71. for (const auto &S : Strings) {
  72. B.resize(Pos + S.length() + 1);
  73. strcpy(reinterpret_cast<char *>(&B[Pos]), S.c_str());
  74. Pos += S.length() + 1;
  75. }
  76. // Backfill the length of the table now that it has been computed.
  77. support::ulittle32_t Length(B.size() - Offset);
  78. support::endian::write32le(&B[Offset], Length);
  79. }
  80. static ImportNameType getNameType(StringRef Sym, StringRef ExtName,
  81. MachineTypes Machine) {
  82. if (Sym != ExtName)
  83. return IMPORT_NAME_UNDECORATE;
  84. if (Machine == IMAGE_FILE_MACHINE_I386 && Sym.startswith("_"))
  85. return IMPORT_NAME_NOPREFIX;
  86. return IMPORT_NAME;
  87. }
  88. static Expected<std::string> replace(StringRef S, StringRef From,
  89. StringRef To) {
  90. size_t Pos = S.find(From);
  91. // From and To may be mangled, but substrings in S may not.
  92. if (Pos == StringRef::npos && From.startswith("_") && To.startswith("_")) {
  93. From = From.substr(1);
  94. To = To.substr(1);
  95. Pos = S.find(From);
  96. }
  97. if (Pos == StringRef::npos) {
  98. return make_error<StringError>(
  99. StringRef(Twine(S + ": replacing '" + From +
  100. "' with '" + To + "' failed").str()), object_error::parse_failed);
  101. }
  102. return (Twine(S.substr(0, Pos)) + To + S.substr(Pos + From.size())).str();
  103. }
  104. static const std::string NullImportDescriptorSymbolName =
  105. "__NULL_IMPORT_DESCRIPTOR";
  106. namespace {
  107. // This class constructs various small object files necessary to support linking
  108. // symbols imported from a DLL. The contents are pretty strictly defined and
  109. // nearly entirely static. The details of the structures files are defined in
  110. // WINNT.h and the PE/COFF specification.
  111. class ObjectFactory {
  112. using u16 = support::ulittle16_t;
  113. using u32 = support::ulittle32_t;
  114. MachineTypes Machine;
  115. BumpPtrAllocator Alloc;
  116. StringRef ImportName;
  117. StringRef Library;
  118. std::string ImportDescriptorSymbolName;
  119. std::string NullThunkSymbolName;
  120. public:
  121. ObjectFactory(StringRef S, MachineTypes M)
  122. : Machine(M), ImportName(S), Library(S.drop_back(4)),
  123. ImportDescriptorSymbolName(("__IMPORT_DESCRIPTOR_" + Library).str()),
  124. NullThunkSymbolName(("\x7f" + Library + "_NULL_THUNK_DATA").str()) {}
  125. // Creates an Import Descriptor. This is a small object file which contains a
  126. // reference to the terminators and contains the library name (entry) for the
  127. // import name table. It will force the linker to construct the necessary
  128. // structure to import symbols from the DLL.
  129. NewArchiveMember createImportDescriptor(std::vector<uint8_t> &Buffer);
  130. // Creates a NULL import descriptor. This is a small object file whcih
  131. // contains a NULL import descriptor. It is used to terminate the imports
  132. // from a specific DLL.
  133. NewArchiveMember createNullImportDescriptor(std::vector<uint8_t> &Buffer);
  134. // Create a NULL Thunk Entry. This is a small object file which contains a
  135. // NULL Import Address Table entry and a NULL Import Lookup Table Entry. It
  136. // is used to terminate the IAT and ILT.
  137. NewArchiveMember createNullThunk(std::vector<uint8_t> &Buffer);
  138. // Create a short import file which is described in PE/COFF spec 7. Import
  139. // Library Format.
  140. NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal,
  141. ImportType Type, ImportNameType NameType);
  142. // Create a weak external file which is described in PE/COFF Aux Format 3.
  143. NewArchiveMember createWeakExternal(StringRef Sym, StringRef Weak, bool Imp);
  144. };
  145. } // namespace
  146. NewArchiveMember
  147. ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
  148. const uint32_t NumberOfSections = 2;
  149. const uint32_t NumberOfSymbols = 7;
  150. const uint32_t NumberOfRelocations = 3;
  151. // COFF Header
  152. coff_file_header Header{
  153. u16(Machine),
  154. u16(NumberOfSections),
  155. u32(0),
  156. u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
  157. // .idata$2
  158. sizeof(coff_import_directory_table_entry) +
  159. NumberOfRelocations * sizeof(coff_relocation) +
  160. // .idata$4
  161. (ImportName.size() + 1)),
  162. u32(NumberOfSymbols),
  163. u16(0),
  164. u16(is32bit(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0),
  165. };
  166. append(Buffer, Header);
  167. // Section Header Table
  168. const coff_section SectionTable[NumberOfSections] = {
  169. {{'.', 'i', 'd', 'a', 't', 'a', '$', '2'},
  170. u32(0),
  171. u32(0),
  172. u32(sizeof(coff_import_directory_table_entry)),
  173. u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
  174. u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
  175. sizeof(coff_import_directory_table_entry)),
  176. u32(0),
  177. u16(NumberOfRelocations),
  178. u16(0),
  179. u32(IMAGE_SCN_ALIGN_4BYTES | IMAGE_SCN_CNT_INITIALIZED_DATA |
  180. IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)},
  181. {{'.', 'i', 'd', 'a', 't', 'a', '$', '6'},
  182. u32(0),
  183. u32(0),
  184. u32(ImportName.size() + 1),
  185. u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
  186. sizeof(coff_import_directory_table_entry) +
  187. NumberOfRelocations * sizeof(coff_relocation)),
  188. u32(0),
  189. u32(0),
  190. u16(0),
  191. u16(0),
  192. u32(IMAGE_SCN_ALIGN_2BYTES | IMAGE_SCN_CNT_INITIALIZED_DATA |
  193. IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)},
  194. };
  195. append(Buffer, SectionTable);
  196. // .idata$2
  197. const coff_import_directory_table_entry ImportDescriptor{
  198. u32(0), u32(0), u32(0), u32(0), u32(0),
  199. };
  200. append(Buffer, ImportDescriptor);
  201. const coff_relocation RelocationTable[NumberOfRelocations] = {
  202. {u32(offsetof(coff_import_directory_table_entry, NameRVA)), u32(2),
  203. u16(getImgRelRelocation(Machine))},
  204. {u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)),
  205. u32(3), u16(getImgRelRelocation(Machine))},
  206. {u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)),
  207. u32(4), u16(getImgRelRelocation(Machine))},
  208. };
  209. append(Buffer, RelocationTable);
  210. // .idata$6
  211. auto S = Buffer.size();
  212. Buffer.resize(S + ImportName.size() + 1);
  213. memcpy(&Buffer[S], ImportName.data(), ImportName.size());
  214. Buffer[S + ImportName.size()] = '\0';
  215. // Symbol Table
  216. coff_symbol16 SymbolTable[NumberOfSymbols] = {
  217. {{{0, 0, 0, 0, 0, 0, 0, 0}},
  218. u32(0),
  219. u16(1),
  220. u16(0),
  221. IMAGE_SYM_CLASS_EXTERNAL,
  222. 0},
  223. {{{'.', 'i', 'd', 'a', 't', 'a', '$', '2'}},
  224. u32(0),
  225. u16(1),
  226. u16(0),
  227. IMAGE_SYM_CLASS_SECTION,
  228. 0},
  229. {{{'.', 'i', 'd', 'a', 't', 'a', '$', '6'}},
  230. u32(0),
  231. u16(2),
  232. u16(0),
  233. IMAGE_SYM_CLASS_STATIC,
  234. 0},
  235. {{{'.', 'i', 'd', 'a', 't', 'a', '$', '4'}},
  236. u32(0),
  237. u16(0),
  238. u16(0),
  239. IMAGE_SYM_CLASS_SECTION,
  240. 0},
  241. {{{'.', 'i', 'd', 'a', 't', 'a', '$', '5'}},
  242. u32(0),
  243. u16(0),
  244. u16(0),
  245. IMAGE_SYM_CLASS_SECTION,
  246. 0},
  247. {{{0, 0, 0, 0, 0, 0, 0, 0}},
  248. u32(0),
  249. u16(0),
  250. u16(0),
  251. IMAGE_SYM_CLASS_EXTERNAL,
  252. 0},
  253. {{{0, 0, 0, 0, 0, 0, 0, 0}},
  254. u32(0),
  255. u16(0),
  256. u16(0),
  257. IMAGE_SYM_CLASS_EXTERNAL,
  258. 0},
  259. };
  260. // TODO: Name.Offset.Offset here and in the all similar places below
  261. // suggests a names refactoring. Maybe StringTableOffset.Value?
  262. SymbolTable[0].Name.Offset.Offset =
  263. sizeof(uint32_t);
  264. SymbolTable[5].Name.Offset.Offset =
  265. sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1;
  266. SymbolTable[6].Name.Offset.Offset =
  267. sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1 +
  268. NullImportDescriptorSymbolName.length() + 1;
  269. append(Buffer, SymbolTable);
  270. // String Table
  271. writeStringTable(Buffer,
  272. {ImportDescriptorSymbolName, NullImportDescriptorSymbolName,
  273. NullThunkSymbolName});
  274. StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
  275. return {MemoryBufferRef(F, ImportName)};
  276. }
  277. NewArchiveMember
  278. ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
  279. const uint32_t NumberOfSections = 1;
  280. const uint32_t NumberOfSymbols = 1;
  281. // COFF Header
  282. coff_file_header Header{
  283. u16(Machine),
  284. u16(NumberOfSections),
  285. u32(0),
  286. u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
  287. // .idata$3
  288. sizeof(coff_import_directory_table_entry)),
  289. u32(NumberOfSymbols),
  290. u16(0),
  291. u16(is32bit(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0),
  292. };
  293. append(Buffer, Header);
  294. // Section Header Table
  295. const coff_section SectionTable[NumberOfSections] = {
  296. {{'.', 'i', 'd', 'a', 't', 'a', '$', '3'},
  297. u32(0),
  298. u32(0),
  299. u32(sizeof(coff_import_directory_table_entry)),
  300. u32(sizeof(coff_file_header) +
  301. (NumberOfSections * sizeof(coff_section))),
  302. u32(0),
  303. u32(0),
  304. u16(0),
  305. u16(0),
  306. u32(IMAGE_SCN_ALIGN_4BYTES | IMAGE_SCN_CNT_INITIALIZED_DATA |
  307. IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)},
  308. };
  309. append(Buffer, SectionTable);
  310. // .idata$3
  311. const coff_import_directory_table_entry ImportDescriptor{
  312. u32(0), u32(0), u32(0), u32(0), u32(0),
  313. };
  314. append(Buffer, ImportDescriptor);
  315. // Symbol Table
  316. coff_symbol16 SymbolTable[NumberOfSymbols] = {
  317. {{{0, 0, 0, 0, 0, 0, 0, 0}},
  318. u32(0),
  319. u16(1),
  320. u16(0),
  321. IMAGE_SYM_CLASS_EXTERNAL,
  322. 0},
  323. };
  324. SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
  325. append(Buffer, SymbolTable);
  326. // String Table
  327. writeStringTable(Buffer, {NullImportDescriptorSymbolName});
  328. StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
  329. return {MemoryBufferRef(F, ImportName)};
  330. }
  331. NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
  332. const uint32_t NumberOfSections = 2;
  333. const uint32_t NumberOfSymbols = 1;
  334. uint32_t VASize = is32bit(Machine) ? 4 : 8;
  335. // COFF Header
  336. coff_file_header Header{
  337. u16(Machine),
  338. u16(NumberOfSections),
  339. u32(0),
  340. u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
  341. // .idata$5
  342. VASize +
  343. // .idata$4
  344. VASize),
  345. u32(NumberOfSymbols),
  346. u16(0),
  347. u16(is32bit(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0),
  348. };
  349. append(Buffer, Header);
  350. // Section Header Table
  351. const coff_section SectionTable[NumberOfSections] = {
  352. {{'.', 'i', 'd', 'a', 't', 'a', '$', '5'},
  353. u32(0),
  354. u32(0),
  355. u32(VASize),
  356. u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
  357. u32(0),
  358. u32(0),
  359. u16(0),
  360. u16(0),
  361. u32((is32bit(Machine) ? IMAGE_SCN_ALIGN_4BYTES
  362. : IMAGE_SCN_ALIGN_8BYTES) |
  363. IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
  364. IMAGE_SCN_MEM_WRITE)},
  365. {{'.', 'i', 'd', 'a', 't', 'a', '$', '4'},
  366. u32(0),
  367. u32(0),
  368. u32(VASize),
  369. u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
  370. VASize),
  371. u32(0),
  372. u32(0),
  373. u16(0),
  374. u16(0),
  375. u32((is32bit(Machine) ? IMAGE_SCN_ALIGN_4BYTES
  376. : IMAGE_SCN_ALIGN_8BYTES) |
  377. IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
  378. IMAGE_SCN_MEM_WRITE)},
  379. };
  380. append(Buffer, SectionTable);
  381. // .idata$5, ILT
  382. append(Buffer, u32(0));
  383. if (!is32bit(Machine))
  384. append(Buffer, u32(0));
  385. // .idata$4, IAT
  386. append(Buffer, u32(0));
  387. if (!is32bit(Machine))
  388. append(Buffer, u32(0));
  389. // Symbol Table
  390. coff_symbol16 SymbolTable[NumberOfSymbols] = {
  391. {{{0, 0, 0, 0, 0, 0, 0, 0}},
  392. u32(0),
  393. u16(1),
  394. u16(0),
  395. IMAGE_SYM_CLASS_EXTERNAL,
  396. 0},
  397. };
  398. SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
  399. append(Buffer, SymbolTable);
  400. // String Table
  401. writeStringTable(Buffer, {NullThunkSymbolName});
  402. StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
  403. return {MemoryBufferRef{F, ImportName}};
  404. }
  405. NewArchiveMember ObjectFactory::createShortImport(StringRef Sym,
  406. uint16_t Ordinal,
  407. ImportType ImportType,
  408. ImportNameType NameType) {
  409. size_t ImpSize = ImportName.size() + Sym.size() + 2; // +2 for NULs
  410. size_t Size = sizeof(coff_import_header) + ImpSize;
  411. char *Buf = Alloc.Allocate<char>(Size);
  412. memset(Buf, 0, Size);
  413. char *P = Buf;
  414. // Write short import library.
  415. auto *Imp = reinterpret_cast<coff_import_header *>(P);
  416. P += sizeof(*Imp);
  417. Imp->Sig2 = 0xFFFF;
  418. Imp->Machine = Machine;
  419. Imp->SizeOfData = ImpSize;
  420. if (Ordinal > 0)
  421. Imp->OrdinalHint = Ordinal;
  422. Imp->TypeInfo = (NameType << 2) | ImportType;
  423. // Write symbol name and DLL name.
  424. memcpy(P, Sym.data(), Sym.size());
  425. P += Sym.size() + 1;
  426. memcpy(P, ImportName.data(), ImportName.size());
  427. return {MemoryBufferRef(StringRef(Buf, Size), ImportName)};
  428. }
  429. NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
  430. StringRef Weak, bool Imp) {
  431. std::vector<uint8_t> Buffer;
  432. const uint32_t NumberOfSections = 1;
  433. const uint32_t NumberOfSymbols = 5;
  434. // COFF Header
  435. coff_file_header Header{
  436. u16(0),
  437. u16(NumberOfSections),
  438. u32(0),
  439. u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section))),
  440. u32(NumberOfSymbols),
  441. u16(0),
  442. u16(0),
  443. };
  444. append(Buffer, Header);
  445. // Section Header Table
  446. const coff_section SectionTable[NumberOfSections] = {
  447. {{'.', 'd', 'r', 'e', 'c', 't', 'v', 'e'},
  448. u32(0),
  449. u32(0),
  450. u32(0),
  451. u32(0),
  452. u32(0),
  453. u32(0),
  454. u16(0),
  455. u16(0),
  456. u32(IMAGE_SCN_LNK_INFO | IMAGE_SCN_LNK_REMOVE)}};
  457. append(Buffer, SectionTable);
  458. // Symbol Table
  459. coff_symbol16 SymbolTable[NumberOfSymbols] = {
  460. {{{'@', 'c', 'o', 'm', 'p', '.', 'i', 'd'}},
  461. u32(0),
  462. u16(0xFFFF),
  463. u16(0),
  464. IMAGE_SYM_CLASS_STATIC,
  465. 0},
  466. {{{'@', 'f', 'e', 'a', 't', '.', '0', '0'}},
  467. u32(0),
  468. u16(0xFFFF),
  469. u16(0),
  470. IMAGE_SYM_CLASS_STATIC,
  471. 0},
  472. {{{0, 0, 0, 0, 0, 0, 0, 0}},
  473. u32(0),
  474. u16(0),
  475. u16(0),
  476. IMAGE_SYM_CLASS_EXTERNAL,
  477. 0},
  478. {{{0, 0, 0, 0, 0, 0, 0, 0}},
  479. u32(0),
  480. u16(0),
  481. u16(0),
  482. IMAGE_SYM_CLASS_WEAK_EXTERNAL,
  483. 1},
  484. {{{2, 0, 0, 0, 3, 0, 0, 0}}, u32(0), u16(0), u16(0), uint8_t(0), 0},
  485. };
  486. SymbolTable[2].Name.Offset.Offset = sizeof(uint32_t);
  487. //__imp_ String Table
  488. StringRef Prefix = Imp ? "__imp_" : "";
  489. SymbolTable[3].Name.Offset.Offset =
  490. sizeof(uint32_t) + Sym.size() + Prefix.size() + 1;
  491. append(Buffer, SymbolTable);
  492. writeStringTable(Buffer, {(Prefix + Sym).str(),
  493. (Prefix + Weak).str()});
  494. // Copied here so we can still use writeStringTable
  495. char *Buf = Alloc.Allocate<char>(Buffer.size());
  496. memcpy(Buf, Buffer.data(), Buffer.size());
  497. return {MemoryBufferRef(StringRef(Buf, Buffer.size()), ImportName)};
  498. }
  499. Error writeImportLibrary(StringRef ImportName, StringRef Path,
  500. ArrayRef<COFFShortExport> Exports,
  501. MachineTypes Machine, bool MakeWeakAliases) {
  502. std::vector<NewArchiveMember> Members;
  503. ObjectFactory OF(llvm::sys::path::filename(ImportName), Machine);
  504. std::vector<uint8_t> ImportDescriptor;
  505. Members.push_back(OF.createImportDescriptor(ImportDescriptor));
  506. std::vector<uint8_t> NullImportDescriptor;
  507. Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
  508. std::vector<uint8_t> NullThunk;
  509. Members.push_back(OF.createNullThunk(NullThunk));
  510. for (COFFShortExport E : Exports) {
  511. if (E.Private)
  512. continue;
  513. if (E.isWeak() && MakeWeakAliases) {
  514. Members.push_back(OF.createWeakExternal(E.Name, E.ExtName, false));
  515. Members.push_back(OF.createWeakExternal(E.Name, E.ExtName, true));
  516. continue;
  517. }
  518. ImportType ImportType = IMPORT_CODE;
  519. if (E.Data)
  520. ImportType = IMPORT_DATA;
  521. if (E.Constant)
  522. ImportType = IMPORT_CONST;
  523. StringRef SymbolName = E.SymbolName.empty() ? E.Name : E.SymbolName;
  524. ImportNameType NameType = getNameType(SymbolName, E.Name, Machine);
  525. Expected<std::string> Name = E.ExtName.empty()
  526. ? SymbolName
  527. : replace(SymbolName, E.Name, E.ExtName);
  528. if (!Name)
  529. return Name.takeError();
  530. Members.push_back(
  531. OF.createShortImport(*Name, E.Ordinal, ImportType, NameType));
  532. }
  533. return writeArchive(Path, Members, /*WriteSymtab*/ true,
  534. object::Archive::K_GNU,
  535. /*Deterministic*/ true, /*Thin*/ false);
  536. }
  537. } // namespace object
  538. } // namespace llvm