1 //===- DLL.cpp ------------------------------------------------------------===//
2 //
3 // The LLVM Linker
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 various types of chunks for the DLL import or export
11 // descriptor tables. They are inherently Windows-specific.
12 // You need to read Microsoft PE/COFF spec to understand details
13 // about the data structures.
14 //
15 // If you are not particularly interested in linking against Windows
16 // DLL, you can skip this file, and you should still be able to
17 // understand the rest of the linker.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "DLL.h"
22 #include "Chunks.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 Chunk {
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 + OutputSectionOff, 0, getSize());
51 write16le(Buf + OutputSectionOff, Hint);
52 memcpy(Buf + OutputSectionOff + 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 Chunk {
62 public:
LookupChunk(Chunk * C)63 explicit LookupChunk(Chunk *C) : HintName(C) { Alignment = Config->Wordsize; }
getSize() const64 size_t getSize() const override { return Config->Wordsize; }
65
writeTo(uint8_t * Buf) const66 void writeTo(uint8_t *Buf) const override {
67 if (Config->is64())
68 write64le(Buf + OutputSectionOff, HintName->getRVA());
69 else
70 write32le(Buf + OutputSectionOff, HintName->getRVA());
71 }
72
73 Chunk *HintName;
74 };
75
76 // A chunk for the import descriptor table.
77 // This chunk represent import-by-ordinal symbols.
78 // See Microsoft PE/COFF spec 7.1. Import Header for details.
79 class OrdinalOnlyChunk : public Chunk {
80 public:
OrdinalOnlyChunk(uint16_t V)81 explicit OrdinalOnlyChunk(uint16_t V) : Ordinal(V) {
82 Alignment = Config->Wordsize;
83 }
getSize() const84 size_t getSize() const override { return Config->Wordsize; }
85
writeTo(uint8_t * Buf) const86 void writeTo(uint8_t *Buf) const override {
87 // An import-by-ordinal slot has MSB 1 to indicate that
88 // this is import-by-ordinal (and not import-by-name).
89 if (Config->is64()) {
90 write64le(Buf + OutputSectionOff, (1ULL << 63) | Ordinal);
91 } else {
92 write32le(Buf + OutputSectionOff, (1ULL << 31) | Ordinal);
93 }
94 }
95
96 uint16_t Ordinal;
97 };
98
99 // A chunk for the import descriptor table.
100 class ImportDirectoryChunk : public Chunk {
101 public:
ImportDirectoryChunk(Chunk * N)102 explicit ImportDirectoryChunk(Chunk *N) : DLLName(N) {}
getSize() const103 size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
104
writeTo(uint8_t * Buf) const105 void writeTo(uint8_t *Buf) const override {
106 memset(Buf + OutputSectionOff, 0, getSize());
107
108 auto *E = (coff_import_directory_table_entry *)(Buf + OutputSectionOff);
109 E->ImportLookupTableRVA = LookupTab->getRVA();
110 E->NameRVA = DLLName->getRVA();
111 E->ImportAddressTableRVA = AddressTab->getRVA();
112 }
113
114 Chunk *DLLName;
115 Chunk *LookupTab;
116 Chunk *AddressTab;
117 };
118
119 // A chunk representing null terminator in the import table.
120 // Contents of this chunk is always null bytes.
121 class NullChunk : public Chunk {
122 public:
NullChunk(size_t N)123 explicit NullChunk(size_t N) : Size(N) {}
hasData() const124 bool hasData() const override { return false; }
getSize() const125 size_t getSize() const override { return Size; }
126
writeTo(uint8_t * Buf) const127 void writeTo(uint8_t *Buf) const override {
128 memset(Buf + OutputSectionOff, 0, Size);
129 }
130
131 private:
132 size_t Size;
133 };
134
135 static std::vector<std::vector<DefinedImportData *>>
binImports(const std::vector<DefinedImportData * > & Imports)136 binImports(const std::vector<DefinedImportData *> &Imports) {
137 // Group DLL-imported symbols by DLL name because that's how
138 // symbols are layed out in the import descriptor table.
139 auto Less = [](const std::string &A, const std::string &B) {
140 return Config->DLLOrder[A] < Config->DLLOrder[B];
141 };
142 std::map<std::string, std::vector<DefinedImportData *>,
143 bool(*)(const std::string &, const std::string &)> M(Less);
144 for (DefinedImportData *Sym : Imports)
145 M[Sym->getDLLName().lower()].push_back(Sym);
146
147 std::vector<std::vector<DefinedImportData *>> V;
148 for (auto &KV : M) {
149 // Sort symbols by name for each group.
150 std::vector<DefinedImportData *> &Syms = KV.second;
151 std::sort(Syms.begin(), Syms.end(),
152 [](DefinedImportData *A, DefinedImportData *B) {
153 return A->getName() < B->getName();
154 });
155 V.push_back(std::move(Syms));
156 }
157 return V;
158 }
159
160 // Export table
161 // See Microsoft PE/COFF spec 4.3 for details.
162
163 // A chunk for the delay import descriptor table etnry.
164 class DelayDirectoryChunk : public Chunk {
165 public:
DelayDirectoryChunk(Chunk * N)166 explicit DelayDirectoryChunk(Chunk *N) : DLLName(N) {}
167
getSize() const168 size_t getSize() const override {
169 return sizeof(delay_import_directory_table_entry);
170 }
171
writeTo(uint8_t * Buf) const172 void writeTo(uint8_t *Buf) const override {
173 memset(Buf + OutputSectionOff, 0, getSize());
174
175 auto *E = (delay_import_directory_table_entry *)(Buf + OutputSectionOff);
176 E->Attributes = 1;
177 E->Name = DLLName->getRVA();
178 E->ModuleHandle = ModuleHandle->getRVA();
179 E->DelayImportAddressTable = AddressTab->getRVA();
180 E->DelayImportNameTable = NameTab->getRVA();
181 }
182
183 Chunk *DLLName;
184 Chunk *ModuleHandle;
185 Chunk *AddressTab;
186 Chunk *NameTab;
187 };
188
189 // Initial contents for delay-loaded functions.
190 // This code calls __delayLoadHelper2 function to resolve a symbol
191 // and then overwrites its jump table slot with the result
192 // for subsequent function calls.
193 static const uint8_t ThunkX64[] = {
194 0x51, // push rcx
195 0x52, // push rdx
196 0x41, 0x50, // push r8
197 0x41, 0x51, // push r9
198 0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h
199 0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0
200 0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1
201 0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2
202 0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3
203 0x48, 0x8D, 0x15, 0, 0, 0, 0, // lea rdx, [__imp_<FUNCNAME>]
204 0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...]
205 0xE8, 0, 0, 0, 0, // call __delayLoadHelper2
206 0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp]
207 0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h]
208 0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h]
209 0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h]
210 0x48, 0x83, 0xC4, 0x48, // add rsp, 48h
211 0x41, 0x59, // pop r9
212 0x41, 0x58, // pop r8
213 0x5A, // pop rdx
214 0x59, // pop rcx
215 0xFF, 0xE0, // jmp rax
216 };
217
218 static const uint8_t ThunkX86[] = {
219 0x51, // push ecx
220 0x52, // push edx
221 0x68, 0, 0, 0, 0, // push offset ___imp__<FUNCNAME>
222 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
223 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8
224 0x5A, // pop edx
225 0x59, // pop ecx
226 0xFF, 0xE0, // jmp eax
227 };
228
229 static const uint8_t ThunkARM[] = {
230 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 __imp_<FUNCNAME>
231 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 __imp_<FUNCNAME>
232 0x2d, 0xe9, 0x0f, 0x48, // push.w {r0, r1, r2, r3, r11, lr}
233 0x0d, 0xf2, 0x10, 0x0b, // addw r11, sp, #16
234 0x2d, 0xed, 0x10, 0x0b, // vpush {d0, d1, d2, d3, d4, d5, d6, d7}
235 0x61, 0x46, // mov r1, ip
236 0x40, 0xf2, 0x00, 0x00, // mov.w r0, #0 DELAY_IMPORT_DESCRIPTOR
237 0xc0, 0xf2, 0x00, 0x00, // mov.t r0, #0 DELAY_IMPORT_DESCRIPTOR
238 0x00, 0xf0, 0x00, 0xd0, // bl #0 __delayLoadHelper2
239 0x84, 0x46, // mov ip, r0
240 0xbd, 0xec, 0x10, 0x0b, // vpop {d0, d1, d2, d3, d4, d5, d6, d7}
241 0xbd, 0xe8, 0x0f, 0x48, // pop.w {r0, r1, r2, r3, r11, lr}
242 0x60, 0x47, // bx ip
243 };
244
245 static const uint8_t ThunkARM64[] = {
246 0x11, 0x00, 0x00, 0x90, // adrp x17, #0 __imp_<FUNCNAME>
247 0x31, 0x02, 0x00, 0x91, // add x17, x17, #0 :lo12:__imp_<FUNCNAME>
248 0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]!
249 0xfd, 0x03, 0x00, 0x91, // mov x29, sp
250 0xe0, 0x07, 0x01, 0xa9, // stp x0, x1, [sp, #16]
251 0xe2, 0x0f, 0x02, 0xa9, // stp x2, x3, [sp, #32]
252 0xe4, 0x17, 0x03, 0xa9, // stp x4, x5, [sp, #48]
253 0xe6, 0x1f, 0x04, 0xa9, // stp x6, x7, [sp, #64]
254 0xe0, 0x87, 0x02, 0xad, // stp q0, q1, [sp, #80]
255 0xe2, 0x8f, 0x03, 0xad, // stp q2, q3, [sp, #112]
256 0xe4, 0x97, 0x04, 0xad, // stp q4, q5, [sp, #144]
257 0xe6, 0x9f, 0x05, 0xad, // stp q6, q7, [sp, #176]
258 0xe1, 0x03, 0x11, 0xaa, // mov x1, x17
259 0x00, 0x00, 0x00, 0x90, // adrp x0, #0 DELAY_IMPORT_DESCRIPTOR
260 0x00, 0x00, 0x00, 0x91, // add x0, x0, #0 :lo12:DELAY_IMPORT_DESCRIPTOR
261 0x00, 0x00, 0x00, 0x94, // bl #0 __delayLoadHelper2
262 0xf0, 0x03, 0x00, 0xaa, // mov x16, x0
263 0xe6, 0x9f, 0x45, 0xad, // ldp q6, q7, [sp, #176]
264 0xe4, 0x97, 0x44, 0xad, // ldp q4, q5, [sp, #144]
265 0xe2, 0x8f, 0x43, 0xad, // ldp q2, q3, [sp, #112]
266 0xe0, 0x87, 0x42, 0xad, // ldp q0, q1, [sp, #80]
267 0xe6, 0x1f, 0x44, 0xa9, // ldp x6, x7, [sp, #64]
268 0xe4, 0x17, 0x43, 0xa9, // ldp x4, x5, [sp, #48]
269 0xe2, 0x0f, 0x42, 0xa9, // ldp x2, x3, [sp, #32]
270 0xe0, 0x07, 0x41, 0xa9, // ldp x0, x1, [sp, #16]
271 0xfd, 0x7b, 0xcd, 0xa8, // ldp x29, x30, [sp], #208
272 0x00, 0x02, 0x1f, 0xd6, // br x16
273 };
274
275 // A chunk for the delay import thunk.
276 class ThunkChunkX64 : public Chunk {
277 public:
ThunkChunkX64(Defined * I,Chunk * D,Defined * H)278 ThunkChunkX64(Defined *I, Chunk *D, Defined *H)
279 : Imp(I), Desc(D), Helper(H) {}
280
getSize() const281 size_t getSize() const override { return sizeof(ThunkX64); }
282
writeTo(uint8_t * Buf) const283 void writeTo(uint8_t *Buf) const override {
284 memcpy(Buf + OutputSectionOff, ThunkX64, sizeof(ThunkX64));
285 write32le(Buf + OutputSectionOff + 36, Imp->getRVA() - RVA - 40);
286 write32le(Buf + OutputSectionOff + 43, Desc->getRVA() - RVA - 47);
287 write32le(Buf + OutputSectionOff + 48, Helper->getRVA() - RVA - 52);
288 }
289
290 Defined *Imp = nullptr;
291 Chunk *Desc = nullptr;
292 Defined *Helper = nullptr;
293 };
294
295 class ThunkChunkX86 : public Chunk {
296 public:
ThunkChunkX86(Defined * I,Chunk * D,Defined * H)297 ThunkChunkX86(Defined *I, Chunk *D, Defined *H)
298 : Imp(I), Desc(D), Helper(H) {}
299
getSize() const300 size_t getSize() const override { return sizeof(ThunkX86); }
301
writeTo(uint8_t * Buf) const302 void writeTo(uint8_t *Buf) const override {
303 memcpy(Buf + OutputSectionOff, ThunkX86, sizeof(ThunkX86));
304 write32le(Buf + OutputSectionOff + 3, Imp->getRVA() + Config->ImageBase);
305 write32le(Buf + OutputSectionOff + 8, Desc->getRVA() + Config->ImageBase);
306 write32le(Buf + OutputSectionOff + 13, Helper->getRVA() - RVA - 17);
307 }
308
getBaserels(std::vector<Baserel> * Res)309 void getBaserels(std::vector<Baserel> *Res) override {
310 Res->emplace_back(RVA + 3);
311 Res->emplace_back(RVA + 8);
312 }
313
314 Defined *Imp = nullptr;
315 Chunk *Desc = nullptr;
316 Defined *Helper = nullptr;
317 };
318
319 class ThunkChunkARM : public Chunk {
320 public:
ThunkChunkARM(Defined * I,Chunk * D,Defined * H)321 ThunkChunkARM(Defined *I, Chunk *D, Defined *H)
322 : Imp(I), Desc(D), Helper(H) {}
323
getSize() const324 size_t getSize() const override { return sizeof(ThunkARM); }
325
writeTo(uint8_t * Buf) const326 void writeTo(uint8_t *Buf) const override {
327 memcpy(Buf + OutputSectionOff, ThunkARM, sizeof(ThunkARM));
328 applyMOV32T(Buf + OutputSectionOff + 0, Imp->getRVA() + Config->ImageBase);
329 applyMOV32T(Buf + OutputSectionOff + 22, Desc->getRVA() + Config->ImageBase);
330 applyBranch24T(Buf + OutputSectionOff + 30, Helper->getRVA() - RVA - 34);
331 }
332
getBaserels(std::vector<Baserel> * Res)333 void getBaserels(std::vector<Baserel> *Res) override {
334 Res->emplace_back(RVA + 0, IMAGE_REL_BASED_ARM_MOV32T);
335 Res->emplace_back(RVA + 22, IMAGE_REL_BASED_ARM_MOV32T);
336 }
337
338 Defined *Imp = nullptr;
339 Chunk *Desc = nullptr;
340 Defined *Helper = nullptr;
341 };
342
343 class ThunkChunkARM64 : public Chunk {
344 public:
ThunkChunkARM64(Defined * I,Chunk * D,Defined * H)345 ThunkChunkARM64(Defined *I, Chunk *D, Defined *H)
346 : Imp(I), Desc(D), Helper(H) {}
347
getSize() const348 size_t getSize() const override { return sizeof(ThunkARM64); }
349
writeTo(uint8_t * Buf) const350 void writeTo(uint8_t *Buf) const override {
351 memcpy(Buf + OutputSectionOff, ThunkARM64, sizeof(ThunkARM64));
352 applyArm64Addr(Buf + OutputSectionOff + 0, Imp->getRVA(), RVA + 0, 12);
353 applyArm64Imm(Buf + OutputSectionOff + 4, Imp->getRVA() & 0xfff, 0);
354 applyArm64Addr(Buf + OutputSectionOff + 52, Desc->getRVA(), RVA + 52, 12);
355 applyArm64Imm(Buf + OutputSectionOff + 56, Desc->getRVA() & 0xfff, 0);
356 applyArm64Branch26(Buf + OutputSectionOff + 60,
357 Helper->getRVA() - RVA - 60);
358 }
359
360 Defined *Imp = nullptr;
361 Chunk *Desc = nullptr;
362 Defined *Helper = nullptr;
363 };
364
365 // A chunk for the import descriptor table.
366 class DelayAddressChunk : public Chunk {
367 public:
DelayAddressChunk(Chunk * C)368 explicit DelayAddressChunk(Chunk *C) : Thunk(C) {
369 Alignment = Config->Wordsize;
370 }
getSize() const371 size_t getSize() const override { return Config->Wordsize; }
372
writeTo(uint8_t * Buf) const373 void writeTo(uint8_t *Buf) const override {
374 if (Config->is64()) {
375 write64le(Buf + OutputSectionOff, 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 + OutputSectionOff, (Thunk->getRVA() + Config->ImageBase) | Bit);
382 }
383 }
384
getBaserels(std::vector<Baserel> * Res)385 void getBaserels(std::vector<Baserel> *Res) override {
386 Res->emplace_back(RVA);
387 }
388
389 Chunk *Thunk;
390 };
391
392 // Export table
393 // Read Microsoft PE/COFF spec 5.3 for details.
394
395 // A chunk for the export descriptor table.
396 class ExportDirectoryChunk : public Chunk {
397 public:
ExportDirectoryChunk(int I,int J,Chunk * D,Chunk * A,Chunk * N,Chunk * O)398 ExportDirectoryChunk(int I, int J, Chunk *D, Chunk *A, Chunk *N, Chunk *O)
399 : MaxOrdinal(I), NameTabSize(J), DLLName(D), AddressTab(A), NameTab(N),
400 OrdinalTab(O) {}
401
getSize() const402 size_t getSize() const override {
403 return sizeof(export_directory_table_entry);
404 }
405
writeTo(uint8_t * Buf) const406 void writeTo(uint8_t *Buf) const override {
407 memset(Buf + OutputSectionOff, 0, getSize());
408
409 auto *E = (export_directory_table_entry *)(Buf + OutputSectionOff);
410 E->NameRVA = DLLName->getRVA();
411 E->OrdinalBase = 0;
412 E->AddressTableEntries = MaxOrdinal + 1;
413 E->NumberOfNamePointers = NameTabSize;
414 E->ExportAddressTableRVA = AddressTab->getRVA();
415 E->NamePointerRVA = NameTab->getRVA();
416 E->OrdinalTableRVA = OrdinalTab->getRVA();
417 }
418
419 uint16_t MaxOrdinal;
420 uint16_t NameTabSize;
421 Chunk *DLLName;
422 Chunk *AddressTab;
423 Chunk *NameTab;
424 Chunk *OrdinalTab;
425 };
426
427 class AddressTableChunk : public Chunk {
428 public:
AddressTableChunk(size_t MaxOrdinal)429 explicit AddressTableChunk(size_t MaxOrdinal) : Size(MaxOrdinal + 1) {}
getSize() const430 size_t getSize() const override { return Size * 4; }
431
writeTo(uint8_t * Buf) const432 void writeTo(uint8_t *Buf) const override {
433 memset(Buf + OutputSectionOff, 0, getSize());
434
435 for (const Export &E : Config->Exports) {
436 uint8_t *P = Buf + OutputSectionOff + E.Ordinal * 4;
437 uint32_t Bit = 0;
438 // Pointer to thumb code must have the LSB set, so adjust it.
439 if (Config->Machine == ARMNT && !E.Data)
440 Bit = 1;
441 if (E.ForwardChunk) {
442 write32le(P, E.ForwardChunk->getRVA() | Bit);
443 } else {
444 write32le(P, cast<Defined>(E.Sym)->getRVA() | Bit);
445 }
446 }
447 }
448
449 private:
450 size_t Size;
451 };
452
453 class NamePointersChunk : public Chunk {
454 public:
NamePointersChunk(std::vector<Chunk * > & V)455 explicit NamePointersChunk(std::vector<Chunk *> &V) : Chunks(V) {}
getSize() const456 size_t getSize() const override { return Chunks.size() * 4; }
457
writeTo(uint8_t * Buf) const458 void writeTo(uint8_t *Buf) const override {
459 uint8_t *P = Buf + OutputSectionOff;
460 for (Chunk *C : Chunks) {
461 write32le(P, C->getRVA());
462 P += 4;
463 }
464 }
465
466 private:
467 std::vector<Chunk *> Chunks;
468 };
469
470 class ExportOrdinalChunk : public Chunk {
471 public:
ExportOrdinalChunk(size_t I)472 explicit ExportOrdinalChunk(size_t I) : Size(I) {}
getSize() const473 size_t getSize() const override { return Size * 2; }
474
writeTo(uint8_t * Buf) const475 void writeTo(uint8_t *Buf) const override {
476 uint8_t *P = Buf + OutputSectionOff;
477 for (Export &E : Config->Exports) {
478 if (E.Noname)
479 continue;
480 write16le(P, E.Ordinal);
481 P += 2;
482 }
483 }
484
485 private:
486 size_t Size;
487 };
488
489 } // anonymous namespace
490
create()491 void IdataContents::create() {
492 std::vector<std::vector<DefinedImportData *>> V = binImports(Imports);
493
494 // Create .idata contents for each DLL.
495 for (std::vector<DefinedImportData *> &Syms : V) {
496 // Create lookup and address tables. If they have external names,
497 // we need to create HintName chunks to store the names.
498 // If they don't (if they are import-by-ordinals), we store only
499 // ordinal values to the table.
500 size_t Base = Lookups.size();
501 for (DefinedImportData *S : Syms) {
502 uint16_t Ord = S->getOrdinal();
503 if (S->getExternalName().empty()) {
504 Lookups.push_back(make<OrdinalOnlyChunk>(Ord));
505 Addresses.push_back(make<OrdinalOnlyChunk>(Ord));
506 continue;
507 }
508 auto *C = make<HintNameChunk>(S->getExternalName(), Ord);
509 Lookups.push_back(make<LookupChunk>(C));
510 Addresses.push_back(make<LookupChunk>(C));
511 Hints.push_back(C);
512 }
513 // Terminate with null values.
514 Lookups.push_back(make<NullChunk>(Config->Wordsize));
515 Addresses.push_back(make<NullChunk>(Config->Wordsize));
516
517 for (int I = 0, E = Syms.size(); I < E; ++I)
518 Syms[I]->setLocation(Addresses[Base + I]);
519
520 // Create the import table header.
521 DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName()));
522 auto *Dir = make<ImportDirectoryChunk>(DLLNames.back());
523 Dir->LookupTab = Lookups[Base];
524 Dir->AddressTab = Addresses[Base];
525 Dirs.push_back(Dir);
526 }
527 // Add null terminator.
528 Dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
529 }
530
getChunks()531 std::vector<Chunk *> DelayLoadContents::getChunks() {
532 std::vector<Chunk *> V;
533 V.insert(V.end(), Dirs.begin(), Dirs.end());
534 V.insert(V.end(), Names.begin(), Names.end());
535 V.insert(V.end(), HintNames.begin(), HintNames.end());
536 V.insert(V.end(), DLLNames.begin(), DLLNames.end());
537 return V;
538 }
539
getDataChunks()540 std::vector<Chunk *> DelayLoadContents::getDataChunks() {
541 std::vector<Chunk *> V;
542 V.insert(V.end(), ModuleHandles.begin(), ModuleHandles.end());
543 V.insert(V.end(), Addresses.begin(), Addresses.end());
544 return V;
545 }
546
getDirSize()547 uint64_t DelayLoadContents::getDirSize() {
548 return Dirs.size() * sizeof(delay_import_directory_table_entry);
549 }
550
create(Defined * H)551 void DelayLoadContents::create(Defined *H) {
552 Helper = H;
553 std::vector<std::vector<DefinedImportData *>> V = binImports(Imports);
554
555 // Create .didat contents for each DLL.
556 for (std::vector<DefinedImportData *> &Syms : V) {
557 // Create the delay import table header.
558 DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName()));
559 auto *Dir = make<DelayDirectoryChunk>(DLLNames.back());
560
561 size_t Base = Addresses.size();
562 for (DefinedImportData *S : Syms) {
563 Chunk *T = newThunkChunk(S, Dir);
564 auto *A = make<DelayAddressChunk>(T);
565 Addresses.push_back(A);
566 Thunks.push_back(T);
567 StringRef ExtName = S->getExternalName();
568 if (ExtName.empty()) {
569 Names.push_back(make<OrdinalOnlyChunk>(S->getOrdinal()));
570 } else {
571 auto *C = make<HintNameChunk>(ExtName, 0);
572 Names.push_back(make<LookupChunk>(C));
573 HintNames.push_back(C);
574 }
575 }
576 // Terminate with null values.
577 Addresses.push_back(make<NullChunk>(8));
578 Names.push_back(make<NullChunk>(8));
579
580 for (int I = 0, E = Syms.size(); I < E; ++I)
581 Syms[I]->setLocation(Addresses[Base + I]);
582 auto *MH = make<NullChunk>(8);
583 MH->Alignment = 8;
584 ModuleHandles.push_back(MH);
585
586 // Fill the delay import table header fields.
587 Dir->ModuleHandle = MH;
588 Dir->AddressTab = Addresses[Base];
589 Dir->NameTab = Names[Base];
590 Dirs.push_back(Dir);
591 }
592 // Add null terminator.
593 Dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
594 }
595
newThunkChunk(DefinedImportData * S,Chunk * Dir)596 Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *S, Chunk *Dir) {
597 switch (Config->Machine) {
598 case AMD64:
599 return make<ThunkChunkX64>(S, Dir, Helper);
600 case I386:
601 return make<ThunkChunkX86>(S, Dir, Helper);
602 case ARMNT:
603 return make<ThunkChunkARM>(S, Dir, Helper);
604 case ARM64:
605 return make<ThunkChunkARM64>(S, Dir, Helper);
606 default:
607 llvm_unreachable("unsupported machine type");
608 }
609 }
610
EdataContents()611 EdataContents::EdataContents() {
612 uint16_t MaxOrdinal = 0;
613 for (Export &E : Config->Exports)
614 MaxOrdinal = std::max(MaxOrdinal, E.Ordinal);
615
616 auto *DLLName = make<StringChunk>(sys::path::filename(Config->OutputFile));
617 auto *AddressTab = make<AddressTableChunk>(MaxOrdinal);
618 std::vector<Chunk *> Names;
619 for (Export &E : Config->Exports)
620 if (!E.Noname)
621 Names.push_back(make<StringChunk>(E.ExportName));
622
623 std::vector<Chunk *> Forwards;
624 for (Export &E : Config->Exports) {
625 if (E.ForwardTo.empty())
626 continue;
627 E.ForwardChunk = make<StringChunk>(E.ForwardTo);
628 Forwards.push_back(E.ForwardChunk);
629 }
630
631 auto *NameTab = make<NamePointersChunk>(Names);
632 auto *OrdinalTab = make<ExportOrdinalChunk>(Names.size());
633 auto *Dir = make<ExportDirectoryChunk>(MaxOrdinal, Names.size(), DLLName,
634 AddressTab, NameTab, OrdinalTab);
635 Chunks.push_back(Dir);
636 Chunks.push_back(DLLName);
637 Chunks.push_back(AddressTab);
638 Chunks.push_back(NameTab);
639 Chunks.push_back(OrdinalTab);
640 Chunks.insert(Chunks.end(), Names.begin(), Names.end());
641 Chunks.insert(Chunks.end(), Forwards.begin(), Forwards.end());
642 }
643
644 } // namespace coff
645 } // namespace lld
646