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