DLL.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. //===- DLL.cpp ------------------------------------------------------------===//
  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 various types of chunks for the DLL import or export
  10. // descriptor tables. They are inherently Windows-specific.
  11. // You need to read Microsoft PE/COFF spec to understand details
  12. // about the data structures.
  13. //
  14. // If you are not particularly interested in linking against Windows
  15. // DLL, you can skip this file, and you should still be able to
  16. // understand the rest of the linker.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "DLL.h"
  20. #include "Chunks.h"
  21. #include "llvm/Object/COFF.h"
  22. #include "llvm/Support/Endian.h"
  23. #include "llvm/Support/Path.h"
  24. using namespace llvm;
  25. using namespace llvm::object;
  26. using namespace llvm::support::endian;
  27. using namespace llvm::COFF;
  28. namespace lld {
  29. namespace coff {
  30. namespace {
  31. // Import table
  32. // A chunk for the import descriptor table.
  33. class HintNameChunk : public NonSectionChunk {
  34. public:
  35. HintNameChunk(StringRef n, uint16_t h) : name(n), hint(h) {}
  36. size_t getSize() const override {
  37. // Starts with 2 byte Hint field, followed by a null-terminated string,
  38. // ends with 0 or 1 byte padding.
  39. return alignTo(name.size() + 3, 2);
  40. }
  41. void writeTo(uint8_t *buf) const override {
  42. memset(buf, 0, getSize());
  43. write16le(buf, hint);
  44. memcpy(buf + 2, name.data(), name.size());
  45. }
  46. private:
  47. StringRef name;
  48. uint16_t hint;
  49. };
  50. // A chunk for the import descriptor table.
  51. class LookupChunk : public NonSectionChunk {
  52. public:
  53. explicit LookupChunk(Chunk *c) : hintName(c) {
  54. setAlignment(config->wordsize);
  55. }
  56. size_t getSize() const override { return config->wordsize; }
  57. void writeTo(uint8_t *buf) const override {
  58. if (config->is64())
  59. write64le(buf, hintName->getRVA());
  60. else
  61. write32le(buf, hintName->getRVA());
  62. }
  63. Chunk *hintName;
  64. };
  65. // A chunk for the import descriptor table.
  66. // This chunk represent import-by-ordinal symbols.
  67. // See Microsoft PE/COFF spec 7.1. Import Header for details.
  68. class OrdinalOnlyChunk : public NonSectionChunk {
  69. public:
  70. explicit OrdinalOnlyChunk(uint16_t v) : ordinal(v) {
  71. setAlignment(config->wordsize);
  72. }
  73. size_t getSize() const override { return config->wordsize; }
  74. void writeTo(uint8_t *buf) const override {
  75. // An import-by-ordinal slot has MSB 1 to indicate that
  76. // this is import-by-ordinal (and not import-by-name).
  77. if (config->is64()) {
  78. write64le(buf, (1ULL << 63) | ordinal);
  79. } else {
  80. write32le(buf, (1ULL << 31) | ordinal);
  81. }
  82. }
  83. uint16_t ordinal;
  84. };
  85. // A chunk for the import descriptor table.
  86. class ImportDirectoryChunk : public NonSectionChunk {
  87. public:
  88. explicit ImportDirectoryChunk(Chunk *n) : dllName(n) {}
  89. size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
  90. void writeTo(uint8_t *buf) const override {
  91. memset(buf, 0, getSize());
  92. auto *e = (coff_import_directory_table_entry *)(buf);
  93. e->ImportLookupTableRVA = lookupTab->getRVA();
  94. e->NameRVA = dllName->getRVA();
  95. e->ImportAddressTableRVA = addressTab->getRVA();
  96. }
  97. Chunk *dllName;
  98. Chunk *lookupTab;
  99. Chunk *addressTab;
  100. };
  101. // A chunk representing null terminator in the import table.
  102. // Contents of this chunk is always null bytes.
  103. class NullChunk : public NonSectionChunk {
  104. public:
  105. explicit NullChunk(size_t n) : size(n) { hasData = false; }
  106. size_t getSize() const override { return size; }
  107. void writeTo(uint8_t *buf) const override {
  108. memset(buf, 0, size);
  109. }
  110. private:
  111. size_t size;
  112. };
  113. static std::vector<std::vector<DefinedImportData *>>
  114. binImports(const std::vector<DefinedImportData *> &imports) {
  115. // Group DLL-imported symbols by DLL name because that's how
  116. // symbols are laid out in the import descriptor table.
  117. auto less = [](const std::string &a, const std::string &b) {
  118. return config->dllOrder[a] < config->dllOrder[b];
  119. };
  120. std::map<std::string, std::vector<DefinedImportData *>,
  121. bool(*)(const std::string &, const std::string &)> m(less);
  122. for (DefinedImportData *sym : imports)
  123. m[sym->getDLLName().lower()].push_back(sym);
  124. std::vector<std::vector<DefinedImportData *>> v;
  125. for (auto &kv : m) {
  126. // Sort symbols by name for each group.
  127. std::vector<DefinedImportData *> &syms = kv.second;
  128. std::sort(syms.begin(), syms.end(),
  129. [](DefinedImportData *a, DefinedImportData *b) {
  130. return a->getName() < b->getName();
  131. });
  132. v.push_back(std::move(syms));
  133. }
  134. return v;
  135. }
  136. // Export table
  137. // See Microsoft PE/COFF spec 4.3 for details.
  138. // A chunk for the delay import descriptor table etnry.
  139. class DelayDirectoryChunk : public NonSectionChunk {
  140. public:
  141. explicit DelayDirectoryChunk(Chunk *n) : dllName(n) {}
  142. size_t getSize() const override {
  143. return sizeof(delay_import_directory_table_entry);
  144. }
  145. void writeTo(uint8_t *buf) const override {
  146. memset(buf, 0, getSize());
  147. auto *e = (delay_import_directory_table_entry *)(buf);
  148. e->Attributes = 1;
  149. e->Name = dllName->getRVA();
  150. e->ModuleHandle = moduleHandle->getRVA();
  151. e->DelayImportAddressTable = addressTab->getRVA();
  152. e->DelayImportNameTable = nameTab->getRVA();
  153. }
  154. Chunk *dllName;
  155. Chunk *moduleHandle;
  156. Chunk *addressTab;
  157. Chunk *nameTab;
  158. };
  159. // Initial contents for delay-loaded functions.
  160. // This code calls __delayLoadHelper2 function to resolve a symbol
  161. // which then overwrites its jump table slot with the result
  162. // for subsequent function calls.
  163. static const uint8_t thunkX64[] = {
  164. 0x48, 0x8D, 0x05, 0, 0, 0, 0, // lea rax, [__imp_<FUNCNAME>]
  165. 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
  166. };
  167. static const uint8_t tailMergeX64[] = {
  168. 0x51, // push rcx
  169. 0x52, // push rdx
  170. 0x41, 0x50, // push r8
  171. 0x41, 0x51, // push r9
  172. 0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h
  173. 0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0
  174. 0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1
  175. 0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2
  176. 0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3
  177. 0x48, 0x8B, 0xD0, // mov rdx, rax
  178. 0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...]
  179. 0xE8, 0, 0, 0, 0, // call __delayLoadHelper2
  180. 0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp]
  181. 0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h]
  182. 0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h]
  183. 0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h]
  184. 0x48, 0x83, 0xC4, 0x48, // add rsp, 48h
  185. 0x41, 0x59, // pop r9
  186. 0x41, 0x58, // pop r8
  187. 0x5A, // pop rdx
  188. 0x59, // pop rcx
  189. 0xFF, 0xE0, // jmp rax
  190. };
  191. static const uint8_t thunkX86[] = {
  192. 0xB8, 0, 0, 0, 0, // mov eax, offset ___imp__<FUNCNAME>
  193. 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
  194. };
  195. static const uint8_t tailMergeX86[] = {
  196. 0x51, // push ecx
  197. 0x52, // push edx
  198. 0x50, // push eax
  199. 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
  200. 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8
  201. 0x5A, // pop edx
  202. 0x59, // pop ecx
  203. 0xFF, 0xE0, // jmp eax
  204. };
  205. static const uint8_t thunkARM[] = {
  206. 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 __imp_<FUNCNAME>
  207. 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 __imp_<FUNCNAME>
  208. 0x00, 0xf0, 0x00, 0xb8, // b.w __tailMerge_<lib>
  209. };
  210. static const uint8_t tailMergeARM[] = {
  211. 0x2d, 0xe9, 0x0f, 0x48, // push.w {r0, r1, r2, r3, r11, lr}
  212. 0x0d, 0xf2, 0x10, 0x0b, // addw r11, sp, #16
  213. 0x2d, 0xed, 0x10, 0x0b, // vpush {d0, d1, d2, d3, d4, d5, d6, d7}
  214. 0x61, 0x46, // mov r1, ip
  215. 0x40, 0xf2, 0x00, 0x00, // mov.w r0, #0 DELAY_IMPORT_DESCRIPTOR
  216. 0xc0, 0xf2, 0x00, 0x00, // mov.t r0, #0 DELAY_IMPORT_DESCRIPTOR
  217. 0x00, 0xf0, 0x00, 0xd0, // bl #0 __delayLoadHelper2
  218. 0x84, 0x46, // mov ip, r0
  219. 0xbd, 0xec, 0x10, 0x0b, // vpop {d0, d1, d2, d3, d4, d5, d6, d7}
  220. 0xbd, 0xe8, 0x0f, 0x48, // pop.w {r0, r1, r2, r3, r11, lr}
  221. 0x60, 0x47, // bx ip
  222. };
  223. static const uint8_t thunkARM64[] = {
  224. 0x11, 0x00, 0x00, 0x90, // adrp x17, #0 __imp_<FUNCNAME>
  225. 0x31, 0x02, 0x00, 0x91, // add x17, x17, #0 :lo12:__imp_<FUNCNAME>
  226. 0x00, 0x00, 0x00, 0x14, // b __tailMerge_<lib>
  227. };
  228. static const uint8_t tailMergeARM64[] = {
  229. 0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]!
  230. 0xfd, 0x03, 0x00, 0x91, // mov x29, sp
  231. 0xe0, 0x07, 0x01, 0xa9, // stp x0, x1, [sp, #16]
  232. 0xe2, 0x0f, 0x02, 0xa9, // stp x2, x3, [sp, #32]
  233. 0xe4, 0x17, 0x03, 0xa9, // stp x4, x5, [sp, #48]
  234. 0xe6, 0x1f, 0x04, 0xa9, // stp x6, x7, [sp, #64]
  235. 0xe0, 0x87, 0x02, 0xad, // stp q0, q1, [sp, #80]
  236. 0xe2, 0x8f, 0x03, 0xad, // stp q2, q3, [sp, #112]
  237. 0xe4, 0x97, 0x04, 0xad, // stp q4, q5, [sp, #144]
  238. 0xe6, 0x9f, 0x05, 0xad, // stp q6, q7, [sp, #176]
  239. 0xe1, 0x03, 0x11, 0xaa, // mov x1, x17
  240. 0x00, 0x00, 0x00, 0x90, // adrp x0, #0 DELAY_IMPORT_DESCRIPTOR
  241. 0x00, 0x00, 0x00, 0x91, // add x0, x0, #0 :lo12:DELAY_IMPORT_DESCRIPTOR
  242. 0x00, 0x00, 0x00, 0x94, // bl #0 __delayLoadHelper2
  243. 0xf0, 0x03, 0x00, 0xaa, // mov x16, x0
  244. 0xe6, 0x9f, 0x45, 0xad, // ldp q6, q7, [sp, #176]
  245. 0xe4, 0x97, 0x44, 0xad, // ldp q4, q5, [sp, #144]
  246. 0xe2, 0x8f, 0x43, 0xad, // ldp q2, q3, [sp, #112]
  247. 0xe0, 0x87, 0x42, 0xad, // ldp q0, q1, [sp, #80]
  248. 0xe6, 0x1f, 0x44, 0xa9, // ldp x6, x7, [sp, #64]
  249. 0xe4, 0x17, 0x43, 0xa9, // ldp x4, x5, [sp, #48]
  250. 0xe2, 0x0f, 0x42, 0xa9, // ldp x2, x3, [sp, #32]
  251. 0xe0, 0x07, 0x41, 0xa9, // ldp x0, x1, [sp, #16]
  252. 0xfd, 0x7b, 0xcd, 0xa8, // ldp x29, x30, [sp], #208
  253. 0x00, 0x02, 0x1f, 0xd6, // br x16
  254. };
  255. // A chunk for the delay import thunk.
  256. class ThunkChunkX64 : public NonSectionChunk {
  257. public:
  258. ThunkChunkX64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
  259. size_t getSize() const override { return sizeof(thunkX64); }
  260. void writeTo(uint8_t *buf) const override {
  261. memcpy(buf, thunkX64, sizeof(thunkX64));
  262. write32le(buf + 3, imp->getRVA() - rva - 7);
  263. write32le(buf + 8, tailMerge->getRVA() - rva - 12);
  264. }
  265. Defined *imp = nullptr;
  266. Chunk *tailMerge = nullptr;
  267. };
  268. class TailMergeChunkX64 : public NonSectionChunk {
  269. public:
  270. TailMergeChunkX64(Chunk *d, Defined *h) : desc(d), helper(h) {}
  271. size_t getSize() const override { return sizeof(tailMergeX64); }
  272. void writeTo(uint8_t *buf) const override {
  273. memcpy(buf, tailMergeX64, sizeof(tailMergeX64));
  274. write32le(buf + 39, desc->getRVA() - rva - 43);
  275. write32le(buf + 44, helper->getRVA() - rva - 48);
  276. }
  277. Chunk *desc = nullptr;
  278. Defined *helper = nullptr;
  279. };
  280. class ThunkChunkX86 : public NonSectionChunk {
  281. public:
  282. ThunkChunkX86(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
  283. size_t getSize() const override { return sizeof(thunkX86); }
  284. void writeTo(uint8_t *buf) const override {
  285. memcpy(buf, thunkX86, sizeof(thunkX86));
  286. write32le(buf + 1, imp->getRVA() + config->imageBase);
  287. write32le(buf + 6, tailMerge->getRVA() - rva - 10);
  288. }
  289. void getBaserels(std::vector<Baserel> *res) override {
  290. res->emplace_back(rva + 1);
  291. }
  292. Defined *imp = nullptr;
  293. Chunk *tailMerge = nullptr;
  294. };
  295. class TailMergeChunkX86 : public NonSectionChunk {
  296. public:
  297. TailMergeChunkX86(Chunk *d, Defined *h) : desc(d), helper(h) {}
  298. size_t getSize() const override { return sizeof(tailMergeX86); }
  299. void writeTo(uint8_t *buf) const override {
  300. memcpy(buf, tailMergeX86, sizeof(tailMergeX86));
  301. write32le(buf + 4, desc->getRVA() + config->imageBase);
  302. write32le(buf + 9, helper->getRVA() - rva - 13);
  303. }
  304. void getBaserels(std::vector<Baserel> *res) override {
  305. res->emplace_back(rva + 4);
  306. }
  307. Chunk *desc = nullptr;
  308. Defined *helper = nullptr;
  309. };
  310. class ThunkChunkARM : public NonSectionChunk {
  311. public:
  312. ThunkChunkARM(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
  313. size_t getSize() const override { return sizeof(thunkARM); }
  314. void writeTo(uint8_t *buf) const override {
  315. memcpy(buf, thunkARM, sizeof(thunkARM));
  316. applyMOV32T(buf + 0, imp->getRVA() + config->imageBase);
  317. applyBranch24T(buf + 8, tailMerge->getRVA() - rva - 12);
  318. }
  319. void getBaserels(std::vector<Baserel> *res) override {
  320. res->emplace_back(rva + 0, IMAGE_REL_BASED_ARM_MOV32T);
  321. }
  322. Defined *imp = nullptr;
  323. Chunk *tailMerge = nullptr;
  324. };
  325. class TailMergeChunkARM : public NonSectionChunk {
  326. public:
  327. TailMergeChunkARM(Chunk *d, Defined *h) : desc(d), helper(h) {}
  328. size_t getSize() const override { return sizeof(tailMergeARM); }
  329. void writeTo(uint8_t *buf) const override {
  330. memcpy(buf, tailMergeARM, sizeof(tailMergeARM));
  331. applyMOV32T(buf + 14, desc->getRVA() + config->imageBase);
  332. applyBranch24T(buf + 22, helper->getRVA() - rva - 26);
  333. }
  334. void getBaserels(std::vector<Baserel> *res) override {
  335. res->emplace_back(rva + 14, IMAGE_REL_BASED_ARM_MOV32T);
  336. }
  337. Chunk *desc = nullptr;
  338. Defined *helper = nullptr;
  339. };
  340. class ThunkChunkARM64 : public NonSectionChunk {
  341. public:
  342. ThunkChunkARM64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
  343. size_t getSize() const override { return sizeof(thunkARM64); }
  344. void writeTo(uint8_t *buf) const override {
  345. memcpy(buf, thunkARM64, sizeof(thunkARM64));
  346. applyArm64Addr(buf + 0, imp->getRVA(), rva + 0, 12);
  347. applyArm64Imm(buf + 4, imp->getRVA() & 0xfff, 0);
  348. applyArm64Branch26(buf + 8, tailMerge->getRVA() - rva - 8);
  349. }
  350. Defined *imp = nullptr;
  351. Chunk *tailMerge = nullptr;
  352. };
  353. class TailMergeChunkARM64 : public NonSectionChunk {
  354. public:
  355. TailMergeChunkARM64(Chunk *d, Defined *h) : desc(d), helper(h) {}
  356. size_t getSize() const override { return sizeof(tailMergeARM64); }
  357. void writeTo(uint8_t *buf) const override {
  358. memcpy(buf, tailMergeARM64, sizeof(tailMergeARM64));
  359. applyArm64Addr(buf + 44, desc->getRVA(), rva + 44, 12);
  360. applyArm64Imm(buf + 48, desc->getRVA() & 0xfff, 0);
  361. applyArm64Branch26(buf + 52, helper->getRVA() - rva - 52);
  362. }
  363. Chunk *desc = nullptr;
  364. Defined *helper = nullptr;
  365. };
  366. // A chunk for the import descriptor table.
  367. class DelayAddressChunk : public NonSectionChunk {
  368. public:
  369. explicit DelayAddressChunk(Chunk *c) : thunk(c) {
  370. setAlignment(config->wordsize);
  371. }
  372. size_t getSize() const override { return config->wordsize; }
  373. void writeTo(uint8_t *buf) const override {
  374. if (config->is64()) {
  375. write64le(buf, thunk->getRVA() + config->imageBase);
  376. } else {
  377. uint32_t bit = 0;
  378. // Pointer to thumb code must have the LSB set, so adjust it.
  379. if (config->machine == ARMNT)
  380. bit = 1;
  381. write32le(buf, (thunk->getRVA() + config->imageBase) | bit);
  382. }
  383. }
  384. void getBaserels(std::vector<Baserel> *res) override {
  385. res->emplace_back(rva);
  386. }
  387. Chunk *thunk;
  388. };
  389. // Export table
  390. // Read Microsoft PE/COFF spec 5.3 for details.
  391. // A chunk for the export descriptor table.
  392. class ExportDirectoryChunk : public NonSectionChunk {
  393. public:
  394. ExportDirectoryChunk(int i, int j, Chunk *d, Chunk *a, Chunk *n, Chunk *o)
  395. : maxOrdinal(i), nameTabSize(j), dllName(d), addressTab(a), nameTab(n),
  396. ordinalTab(o) {}
  397. size_t getSize() const override {
  398. return sizeof(export_directory_table_entry);
  399. }
  400. void writeTo(uint8_t *buf) const override {
  401. memset(buf, 0, getSize());
  402. auto *e = (export_directory_table_entry *)(buf);
  403. e->NameRVA = dllName->getRVA();
  404. e->OrdinalBase = 0;
  405. e->AddressTableEntries = maxOrdinal + 1;
  406. e->NumberOfNamePointers = nameTabSize;
  407. e->ExportAddressTableRVA = addressTab->getRVA();
  408. e->NamePointerRVA = nameTab->getRVA();
  409. e->OrdinalTableRVA = ordinalTab->getRVA();
  410. }
  411. uint16_t maxOrdinal;
  412. uint16_t nameTabSize;
  413. Chunk *dllName;
  414. Chunk *addressTab;
  415. Chunk *nameTab;
  416. Chunk *ordinalTab;
  417. };
  418. class AddressTableChunk : public NonSectionChunk {
  419. public:
  420. explicit AddressTableChunk(size_t maxOrdinal) : size(maxOrdinal + 1) {}
  421. size_t getSize() const override { return size * 4; }
  422. void writeTo(uint8_t *buf) const override {
  423. memset(buf, 0, getSize());
  424. for (const Export &e : config->exports) {
  425. uint8_t *p = buf + e.ordinal * 4;
  426. uint32_t bit = 0;
  427. // Pointer to thumb code must have the LSB set, so adjust it.
  428. if (config->machine == ARMNT && !e.data)
  429. bit = 1;
  430. if (e.forwardChunk) {
  431. write32le(p, e.forwardChunk->getRVA() | bit);
  432. } else {
  433. write32le(p, cast<Defined>(e.sym)->getRVA() | bit);
  434. }
  435. }
  436. }
  437. private:
  438. size_t size;
  439. };
  440. class NamePointersChunk : public NonSectionChunk {
  441. public:
  442. explicit NamePointersChunk(std::vector<Chunk *> &v) : chunks(v) {}
  443. size_t getSize() const override { return chunks.size() * 4; }
  444. void writeTo(uint8_t *buf) const override {
  445. for (Chunk *c : chunks) {
  446. write32le(buf, c->getRVA());
  447. buf += 4;
  448. }
  449. }
  450. private:
  451. std::vector<Chunk *> chunks;
  452. };
  453. class ExportOrdinalChunk : public NonSectionChunk {
  454. public:
  455. explicit ExportOrdinalChunk(size_t i) : size(i) {}
  456. size_t getSize() const override { return size * 2; }
  457. void writeTo(uint8_t *buf) const override {
  458. for (Export &e : config->exports) {
  459. if (e.noname)
  460. continue;
  461. write16le(buf, e.ordinal);
  462. buf += 2;
  463. }
  464. }
  465. private:
  466. size_t size;
  467. };
  468. } // anonymous namespace
  469. void IdataContents::create() {
  470. std::vector<std::vector<DefinedImportData *>> v = binImports(imports);
  471. // Create .idata contents for each DLL.
  472. for (std::vector<DefinedImportData *> &syms : v) {
  473. // Create lookup and address tables. If they have external names,
  474. // we need to create hintName chunks to store the names.
  475. // If they don't (if they are import-by-ordinals), we store only
  476. // ordinal values to the table.
  477. size_t base = lookups.size();
  478. for (DefinedImportData *s : syms) {
  479. uint16_t ord = s->getOrdinal();
  480. if (s->getExternalName().empty()) {
  481. lookups.push_back(make<OrdinalOnlyChunk>(ord));
  482. addresses.push_back(make<OrdinalOnlyChunk>(ord));
  483. continue;
  484. }
  485. auto *c = make<HintNameChunk>(s->getExternalName(), ord);
  486. lookups.push_back(make<LookupChunk>(c));
  487. addresses.push_back(make<LookupChunk>(c));
  488. hints.push_back(c);
  489. }
  490. // Terminate with null values.
  491. lookups.push_back(make<NullChunk>(config->wordsize));
  492. addresses.push_back(make<NullChunk>(config->wordsize));
  493. for (int i = 0, e = syms.size(); i < e; ++i)
  494. syms[i]->setLocation(addresses[base + i]);
  495. // Create the import table header.
  496. dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
  497. auto *dir = make<ImportDirectoryChunk>(dllNames.back());
  498. dir->lookupTab = lookups[base];
  499. dir->addressTab = addresses[base];
  500. dirs.push_back(dir);
  501. }
  502. // Add null terminator.
  503. dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
  504. }
  505. std::vector<Chunk *> DelayLoadContents::getChunks() {
  506. std::vector<Chunk *> v;
  507. v.insert(v.end(), dirs.begin(), dirs.end());
  508. v.insert(v.end(), names.begin(), names.end());
  509. v.insert(v.end(), hintNames.begin(), hintNames.end());
  510. v.insert(v.end(), dllNames.begin(), dllNames.end());
  511. return v;
  512. }
  513. std::vector<Chunk *> DelayLoadContents::getDataChunks() {
  514. std::vector<Chunk *> v;
  515. v.insert(v.end(), moduleHandles.begin(), moduleHandles.end());
  516. v.insert(v.end(), addresses.begin(), addresses.end());
  517. return v;
  518. }
  519. uint64_t DelayLoadContents::getDirSize() {
  520. return dirs.size() * sizeof(delay_import_directory_table_entry);
  521. }
  522. void DelayLoadContents::create(Defined *h) {
  523. helper = h;
  524. std::vector<std::vector<DefinedImportData *>> v = binImports(imports);
  525. // Create .didat contents for each DLL.
  526. for (std::vector<DefinedImportData *> &syms : v) {
  527. // Create the delay import table header.
  528. dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
  529. auto *dir = make<DelayDirectoryChunk>(dllNames.back());
  530. size_t base = addresses.size();
  531. Chunk *tm = newTailMergeChunk(dir);
  532. for (DefinedImportData *s : syms) {
  533. Chunk *t = newThunkChunk(s, tm);
  534. auto *a = make<DelayAddressChunk>(t);
  535. addresses.push_back(a);
  536. thunks.push_back(t);
  537. StringRef extName = s->getExternalName();
  538. if (extName.empty()) {
  539. names.push_back(make<OrdinalOnlyChunk>(s->getOrdinal()));
  540. } else {
  541. auto *c = make<HintNameChunk>(extName, 0);
  542. names.push_back(make<LookupChunk>(c));
  543. hintNames.push_back(c);
  544. }
  545. }
  546. thunks.push_back(tm);
  547. // Terminate with null values.
  548. addresses.push_back(make<NullChunk>(8));
  549. names.push_back(make<NullChunk>(8));
  550. for (int i = 0, e = syms.size(); i < e; ++i)
  551. syms[i]->setLocation(addresses[base + i]);
  552. auto *mh = make<NullChunk>(8);
  553. mh->setAlignment(8);
  554. moduleHandles.push_back(mh);
  555. // Fill the delay import table header fields.
  556. dir->moduleHandle = mh;
  557. dir->addressTab = addresses[base];
  558. dir->nameTab = names[base];
  559. dirs.push_back(dir);
  560. }
  561. // Add null terminator.
  562. dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
  563. }
  564. Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {
  565. switch (config->machine) {
  566. case AMD64:
  567. return make<TailMergeChunkX64>(dir, helper);
  568. case I386:
  569. return make<TailMergeChunkX86>(dir, helper);
  570. case ARMNT:
  571. return make<TailMergeChunkARM>(dir, helper);
  572. case ARM64:
  573. return make<TailMergeChunkARM64>(dir, helper);
  574. default:
  575. llvm_unreachable("unsupported machine type");
  576. }
  577. }
  578. Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s,
  579. Chunk *tailMerge) {
  580. switch (config->machine) {
  581. case AMD64:
  582. return make<ThunkChunkX64>(s, tailMerge);
  583. case I386:
  584. return make<ThunkChunkX86>(s, tailMerge);
  585. case ARMNT:
  586. return make<ThunkChunkARM>(s, tailMerge);
  587. case ARM64:
  588. return make<ThunkChunkARM64>(s, tailMerge);
  589. default:
  590. llvm_unreachable("unsupported machine type");
  591. }
  592. }
  593. EdataContents::EdataContents() {
  594. uint16_t maxOrdinal = 0;
  595. for (Export &e : config->exports)
  596. maxOrdinal = std::max(maxOrdinal, e.ordinal);
  597. auto *dllName = make<StringChunk>(sys::path::filename(config->outputFile));
  598. auto *addressTab = make<AddressTableChunk>(maxOrdinal);
  599. std::vector<Chunk *> names;
  600. for (Export &e : config->exports)
  601. if (!e.noname)
  602. names.push_back(make<StringChunk>(e.exportName));
  603. std::vector<Chunk *> forwards;
  604. for (Export &e : config->exports) {
  605. if (e.forwardTo.empty())
  606. continue;
  607. e.forwardChunk = make<StringChunk>(e.forwardTo);
  608. forwards.push_back(e.forwardChunk);
  609. }
  610. auto *nameTab = make<NamePointersChunk>(names);
  611. auto *ordinalTab = make<ExportOrdinalChunk>(names.size());
  612. auto *dir = make<ExportDirectoryChunk>(maxOrdinal, names.size(), dllName,
  613. addressTab, nameTab, ordinalTab);
  614. chunks.push_back(dir);
  615. chunks.push_back(dllName);
  616. chunks.push_back(addressTab);
  617. chunks.push_back(nameTab);
  618. chunks.push_back(ordinalTab);
  619. chunks.insert(chunks.end(), names.begin(), names.end());
  620. chunks.insert(chunks.end(), forwards.begin(), forwards.end());
  621. }
  622. } // namespace coff
  623. } // namespace lld