1 //===- DLL.cpp ------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
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.
18 //===----------------------------------------------------------------------===//
22 #include "llvm/Object/COFF.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Path.h"
27 using namespace llvm::object;
28 using namespace llvm::support::endian;
29 using namespace llvm::COFF;
37 // A chunk for the import descriptor table.
38 class HintNameChunk : public NonSectionChunk {
40 HintNameChunk(StringRef n, uint16_t h) : name(n), hint(h) {}
42 size_t getSize() const override {
43 // Starts with 2 byte Hint field, followed by a null-terminated string,
44 // ends with 0 or 1 byte padding.
45 return alignTo(name.size() + 3, 2);
48 void writeTo(uint8_t *buf) const override {
49 memset(buf, 0, getSize());
51 memcpy(buf + 2, name.data(), name.size());
59 // A chunk for the import descriptor table.
60 class LookupChunk : public NonSectionChunk {
62 explicit LookupChunk(Chunk *c) : hintName(c) {
63 setAlignment(config->wordsize);
65 size_t getSize() const override { return config->wordsize; }
67 void writeTo(uint8_t *buf) const override {
69 write64le(buf, hintName->getRVA());
71 write32le(buf, hintName->getRVA());
77 // A chunk for the import descriptor table.
78 // This chunk represent import-by-ordinal symbols.
79 // See Microsoft PE/COFF spec 7.1. Import Header for details.
80 class OrdinalOnlyChunk : public NonSectionChunk {
82 explicit OrdinalOnlyChunk(uint16_t v) : ordinal(v) {
83 setAlignment(config->wordsize);
85 size_t getSize() const override { return config->wordsize; }
87 void writeTo(uint8_t *buf) const override {
88 // An import-by-ordinal slot has MSB 1 to indicate that
89 // this is import-by-ordinal (and not import-by-name).
91 write64le(buf, (1ULL << 63) | ordinal);
93 write32le(buf, (1ULL << 31) | ordinal);
100 // A chunk for the import descriptor table.
101 class ImportDirectoryChunk : public NonSectionChunk {
103 explicit ImportDirectoryChunk(Chunk *n) : dllName(n) {}
104 size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
106 void writeTo(uint8_t *buf) const override {
107 memset(buf, 0, getSize());
109 auto *e = (coff_import_directory_table_entry *)(buf);
110 e->ImportLookupTableRVA = lookupTab->getRVA();
111 e->NameRVA = dllName->getRVA();
112 e->ImportAddressTableRVA = addressTab->getRVA();
120 // A chunk representing null terminator in the import table.
121 // Contents of this chunk is always null bytes.
122 class NullChunk : public NonSectionChunk {
124 explicit NullChunk(size_t n) : size(n) { hasData = false; }
125 size_t getSize() const override { return size; }
127 void writeTo(uint8_t *buf) const override {
128 memset(buf, 0, size);
135 static std::vector<std::vector<DefinedImportData *>>
136 binImports(const std::vector<DefinedImportData *> &imports) {
137 // Group DLL-imported symbols by DLL name because that's how
138 // symbols are laid 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];
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);
147 std::vector<std::vector<DefinedImportData *>> v;
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();
155 v.push_back(std::move(syms));
161 // See Microsoft PE/COFF spec 4.3 for details.
163 // A chunk for the delay import descriptor table etnry.
164 class DelayDirectoryChunk : public NonSectionChunk {
166 explicit DelayDirectoryChunk(Chunk *n) : dllName(n) {}
168 size_t getSize() const override {
169 return sizeof(delay_import_directory_table_entry);
172 void writeTo(uint8_t *buf) const override {
173 memset(buf, 0, getSize());
175 auto *e = (delay_import_directory_table_entry *)(buf);
177 e->Name = dllName->getRVA();
178 e->ModuleHandle = moduleHandle->getRVA();
179 e->DelayImportAddressTable = addressTab->getRVA();
180 e->DelayImportNameTable = nameTab->getRVA();
189 // Initial contents for delay-loaded functions.
190 // This code calls __delayLoadHelper2 function to resolve a symbol
191 // which then overwrites its jump table slot with the result
192 // for subsequent function calls.
193 static const uint8_t thunkX64[] = {
194 0x48, 0x8D, 0x05, 0, 0, 0, 0, // lea rax, [__imp_<FUNCNAME>]
195 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
198 static const uint8_t tailMergeX64[] = {
201 0x41, 0x50, // push r8
202 0x41, 0x51, // push r9
203 0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h
204 0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0
205 0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1
206 0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2
207 0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3
208 0x48, 0x8B, 0xD0, // mov rdx, rax
209 0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...]
210 0xE8, 0, 0, 0, 0, // call __delayLoadHelper2
211 0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp]
212 0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h]
213 0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h]
214 0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h]
215 0x48, 0x83, 0xC4, 0x48, // add rsp, 48h
216 0x41, 0x59, // pop r9
217 0x41, 0x58, // pop r8
220 0xFF, 0xE0, // jmp rax
223 static const uint8_t thunkX86[] = {
224 0xB8, 0, 0, 0, 0, // mov eax, offset ___imp__<FUNCNAME>
225 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
228 static const uint8_t tailMergeX86[] = {
232 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
233 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8
236 0xFF, 0xE0, // jmp eax
239 static const uint8_t thunkARM[] = {
240 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 __imp_<FUNCNAME>
241 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 __imp_<FUNCNAME>
242 0x00, 0xf0, 0x00, 0xb8, // b.w __tailMerge_<lib>
245 static const uint8_t tailMergeARM[] = {
246 0x2d, 0xe9, 0x0f, 0x48, // push.w {r0, r1, r2, r3, r11, lr}
247 0x0d, 0xf2, 0x10, 0x0b, // addw r11, sp, #16
248 0x2d, 0xed, 0x10, 0x0b, // vpush {d0, d1, d2, d3, d4, d5, d6, d7}
249 0x61, 0x46, // mov r1, ip
250 0x40, 0xf2, 0x00, 0x00, // mov.w r0, #0 DELAY_IMPORT_DESCRIPTOR
251 0xc0, 0xf2, 0x00, 0x00, // mov.t r0, #0 DELAY_IMPORT_DESCRIPTOR
252 0x00, 0xf0, 0x00, 0xd0, // bl #0 __delayLoadHelper2
253 0x84, 0x46, // mov ip, r0
254 0xbd, 0xec, 0x10, 0x0b, // vpop {d0, d1, d2, d3, d4, d5, d6, d7}
255 0xbd, 0xe8, 0x0f, 0x48, // pop.w {r0, r1, r2, r3, r11, lr}
259 static const uint8_t thunkARM64[] = {
260 0x11, 0x00, 0x00, 0x90, // adrp x17, #0 __imp_<FUNCNAME>
261 0x31, 0x02, 0x00, 0x91, // add x17, x17, #0 :lo12:__imp_<FUNCNAME>
262 0x00, 0x00, 0x00, 0x14, // b __tailMerge_<lib>
265 static const uint8_t tailMergeARM64[] = {
266 0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]!
267 0xfd, 0x03, 0x00, 0x91, // mov x29, sp
268 0xe0, 0x07, 0x01, 0xa9, // stp x0, x1, [sp, #16]
269 0xe2, 0x0f, 0x02, 0xa9, // stp x2, x3, [sp, #32]
270 0xe4, 0x17, 0x03, 0xa9, // stp x4, x5, [sp, #48]
271 0xe6, 0x1f, 0x04, 0xa9, // stp x6, x7, [sp, #64]
272 0xe0, 0x87, 0x02, 0xad, // stp q0, q1, [sp, #80]
273 0xe2, 0x8f, 0x03, 0xad, // stp q2, q3, [sp, #112]
274 0xe4, 0x97, 0x04, 0xad, // stp q4, q5, [sp, #144]
275 0xe6, 0x9f, 0x05, 0xad, // stp q6, q7, [sp, #176]
276 0xe1, 0x03, 0x11, 0xaa, // mov x1, x17
277 0x00, 0x00, 0x00, 0x90, // adrp x0, #0 DELAY_IMPORT_DESCRIPTOR
278 0x00, 0x00, 0x00, 0x91, // add x0, x0, #0 :lo12:DELAY_IMPORT_DESCRIPTOR
279 0x00, 0x00, 0x00, 0x94, // bl #0 __delayLoadHelper2
280 0xf0, 0x03, 0x00, 0xaa, // mov x16, x0
281 0xe6, 0x9f, 0x45, 0xad, // ldp q6, q7, [sp, #176]
282 0xe4, 0x97, 0x44, 0xad, // ldp q4, q5, [sp, #144]
283 0xe2, 0x8f, 0x43, 0xad, // ldp q2, q3, [sp, #112]
284 0xe0, 0x87, 0x42, 0xad, // ldp q0, q1, [sp, #80]
285 0xe6, 0x1f, 0x44, 0xa9, // ldp x6, x7, [sp, #64]
286 0xe4, 0x17, 0x43, 0xa9, // ldp x4, x5, [sp, #48]
287 0xe2, 0x0f, 0x42, 0xa9, // ldp x2, x3, [sp, #32]
288 0xe0, 0x07, 0x41, 0xa9, // ldp x0, x1, [sp, #16]
289 0xfd, 0x7b, 0xcd, 0xa8, // ldp x29, x30, [sp], #208
290 0x00, 0x02, 0x1f, 0xd6, // br x16
293 // A chunk for the delay import thunk.
294 class ThunkChunkX64 : public NonSectionChunk {
296 ThunkChunkX64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
298 size_t getSize() const override { return sizeof(thunkX64); }
300 void writeTo(uint8_t *buf) const override {
301 memcpy(buf, thunkX64, sizeof(thunkX64));
302 write32le(buf + 3, imp->getRVA() - rva - 7);
303 write32le(buf + 8, tailMerge->getRVA() - rva - 12);
306 Defined *imp = nullptr;
307 Chunk *tailMerge = nullptr;
310 class TailMergeChunkX64 : public NonSectionChunk {
312 TailMergeChunkX64(Chunk *d, Defined *h) : desc(d), helper(h) {}
314 size_t getSize() const override { return sizeof(tailMergeX64); }
316 void writeTo(uint8_t *buf) const override {
317 memcpy(buf, tailMergeX64, sizeof(tailMergeX64));
318 write32le(buf + 39, desc->getRVA() - rva - 43);
319 write32le(buf + 44, helper->getRVA() - rva - 48);
322 Chunk *desc = nullptr;
323 Defined *helper = nullptr;
326 class ThunkChunkX86 : public NonSectionChunk {
328 ThunkChunkX86(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
330 size_t getSize() const override { return sizeof(thunkX86); }
332 void writeTo(uint8_t *buf) const override {
333 memcpy(buf, thunkX86, sizeof(thunkX86));
334 write32le(buf + 1, imp->getRVA() + config->imageBase);
335 write32le(buf + 6, tailMerge->getRVA() - rva - 10);
338 void getBaserels(std::vector<Baserel> *res) override {
339 res->emplace_back(rva + 1);
342 Defined *imp = nullptr;
343 Chunk *tailMerge = nullptr;
346 class TailMergeChunkX86 : public NonSectionChunk {
348 TailMergeChunkX86(Chunk *d, Defined *h) : desc(d), helper(h) {}
350 size_t getSize() const override { return sizeof(tailMergeX86); }
352 void writeTo(uint8_t *buf) const override {
353 memcpy(buf, tailMergeX86, sizeof(tailMergeX86));
354 write32le(buf + 4, desc->getRVA() + config->imageBase);
355 write32le(buf + 9, helper->getRVA() - rva - 13);
358 void getBaserels(std::vector<Baserel> *res) override {
359 res->emplace_back(rva + 4);
362 Chunk *desc = nullptr;
363 Defined *helper = nullptr;
366 class ThunkChunkARM : public NonSectionChunk {
368 ThunkChunkARM(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
370 size_t getSize() const override { return sizeof(thunkARM); }
372 void writeTo(uint8_t *buf) const override {
373 memcpy(buf, thunkARM, sizeof(thunkARM));
374 applyMOV32T(buf + 0, imp->getRVA() + config->imageBase);
375 applyBranch24T(buf + 8, tailMerge->getRVA() - rva - 12);
378 void getBaserels(std::vector<Baserel> *res) override {
379 res->emplace_back(rva + 0, IMAGE_REL_BASED_ARM_MOV32T);
382 Defined *imp = nullptr;
383 Chunk *tailMerge = nullptr;
386 class TailMergeChunkARM : public NonSectionChunk {
388 TailMergeChunkARM(Chunk *d, Defined *h) : desc(d), helper(h) {}
390 size_t getSize() const override { return sizeof(tailMergeARM); }
392 void writeTo(uint8_t *buf) const override {
393 memcpy(buf, tailMergeARM, sizeof(tailMergeARM));
394 applyMOV32T(buf + 14, desc->getRVA() + config->imageBase);
395 applyBranch24T(buf + 22, helper->getRVA() - rva - 26);
398 void getBaserels(std::vector<Baserel> *res) override {
399 res->emplace_back(rva + 14, IMAGE_REL_BASED_ARM_MOV32T);
402 Chunk *desc = nullptr;
403 Defined *helper = nullptr;
406 class ThunkChunkARM64 : public NonSectionChunk {
408 ThunkChunkARM64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
410 size_t getSize() const override { return sizeof(thunkARM64); }
412 void writeTo(uint8_t *buf) const override {
413 memcpy(buf, thunkARM64, sizeof(thunkARM64));
414 applyArm64Addr(buf + 0, imp->getRVA(), rva + 0, 12);
415 applyArm64Imm(buf + 4, imp->getRVA() & 0xfff, 0);
416 applyArm64Branch26(buf + 8, tailMerge->getRVA() - rva - 8);
419 Defined *imp = nullptr;
420 Chunk *tailMerge = nullptr;
423 class TailMergeChunkARM64 : public NonSectionChunk {
425 TailMergeChunkARM64(Chunk *d, Defined *h) : desc(d), helper(h) {}
427 size_t getSize() const override { return sizeof(tailMergeARM64); }
429 void writeTo(uint8_t *buf) const override {
430 memcpy(buf, tailMergeARM64, sizeof(tailMergeARM64));
431 applyArm64Addr(buf + 44, desc->getRVA(), rva + 44, 12);
432 applyArm64Imm(buf + 48, desc->getRVA() & 0xfff, 0);
433 applyArm64Branch26(buf + 52, helper->getRVA() - rva - 52);
436 Chunk *desc = nullptr;
437 Defined *helper = nullptr;
440 // A chunk for the import descriptor table.
441 class DelayAddressChunk : public NonSectionChunk {
443 explicit DelayAddressChunk(Chunk *c) : thunk(c) {
444 setAlignment(config->wordsize);
446 size_t getSize() const override { return config->wordsize; }
448 void writeTo(uint8_t *buf) const override {
449 if (config->is64()) {
450 write64le(buf, thunk->getRVA() + config->imageBase);
453 // Pointer to thumb code must have the LSB set, so adjust it.
454 if (config->machine == ARMNT)
456 write32le(buf, (thunk->getRVA() + config->imageBase) | bit);
460 void getBaserels(std::vector<Baserel> *res) override {
461 res->emplace_back(rva);
468 // Read Microsoft PE/COFF spec 5.3 for details.
470 // A chunk for the export descriptor table.
471 class ExportDirectoryChunk : public NonSectionChunk {
473 ExportDirectoryChunk(int i, int j, Chunk *d, Chunk *a, Chunk *n, Chunk *o)
474 : maxOrdinal(i), nameTabSize(j), dllName(d), addressTab(a), nameTab(n),
477 size_t getSize() const override {
478 return sizeof(export_directory_table_entry);
481 void writeTo(uint8_t *buf) const override {
482 memset(buf, 0, getSize());
484 auto *e = (export_directory_table_entry *)(buf);
485 e->NameRVA = dllName->getRVA();
487 e->AddressTableEntries = maxOrdinal + 1;
488 e->NumberOfNamePointers = nameTabSize;
489 e->ExportAddressTableRVA = addressTab->getRVA();
490 e->NamePointerRVA = nameTab->getRVA();
491 e->OrdinalTableRVA = ordinalTab->getRVA();
495 uint16_t nameTabSize;
502 class AddressTableChunk : public NonSectionChunk {
504 explicit AddressTableChunk(size_t maxOrdinal) : size(maxOrdinal + 1) {}
505 size_t getSize() const override { return size * 4; }
507 void writeTo(uint8_t *buf) const override {
508 memset(buf, 0, getSize());
510 for (const Export &e : config->exports) {
511 uint8_t *p = buf + e.ordinal * 4;
513 // Pointer to thumb code must have the LSB set, so adjust it.
514 if (config->machine == ARMNT && !e.data)
516 if (e.forwardChunk) {
517 write32le(p, e.forwardChunk->getRVA() | bit);
519 write32le(p, cast<Defined>(e.sym)->getRVA() | bit);
528 class NamePointersChunk : public NonSectionChunk {
530 explicit NamePointersChunk(std::vector<Chunk *> &v) : chunks(v) {}
531 size_t getSize() const override { return chunks.size() * 4; }
533 void writeTo(uint8_t *buf) const override {
534 for (Chunk *c : chunks) {
535 write32le(buf, c->getRVA());
541 std::vector<Chunk *> chunks;
544 class ExportOrdinalChunk : public NonSectionChunk {
546 explicit ExportOrdinalChunk(size_t i) : size(i) {}
547 size_t getSize() const override { return size * 2; }
549 void writeTo(uint8_t *buf) const override {
550 for (Export &e : config->exports) {
553 write16le(buf, e.ordinal);
562 } // anonymous namespace
564 void IdataContents::create() {
565 std::vector<std::vector<DefinedImportData *>> v = binImports(imports);
567 // Create .idata contents for each DLL.
568 for (std::vector<DefinedImportData *> &syms : v) {
569 // Create lookup and address tables. If they have external names,
570 // we need to create hintName chunks to store the names.
571 // If they don't (if they are import-by-ordinals), we store only
572 // ordinal values to the table.
573 size_t base = lookups.size();
574 for (DefinedImportData *s : syms) {
575 uint16_t ord = s->getOrdinal();
576 if (s->getExternalName().empty()) {
577 lookups.push_back(make<OrdinalOnlyChunk>(ord));
578 addresses.push_back(make<OrdinalOnlyChunk>(ord));
581 auto *c = make<HintNameChunk>(s->getExternalName(), ord);
582 lookups.push_back(make<LookupChunk>(c));
583 addresses.push_back(make<LookupChunk>(c));
586 // Terminate with null values.
587 lookups.push_back(make<NullChunk>(config->wordsize));
588 addresses.push_back(make<NullChunk>(config->wordsize));
590 for (int i = 0, e = syms.size(); i < e; ++i)
591 syms[i]->setLocation(addresses[base + i]);
593 // Create the import table header.
594 dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
595 auto *dir = make<ImportDirectoryChunk>(dllNames.back());
596 dir->lookupTab = lookups[base];
597 dir->addressTab = addresses[base];
600 // Add null terminator.
601 dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
604 std::vector<Chunk *> DelayLoadContents::getChunks() {
605 std::vector<Chunk *> v;
606 v.insert(v.end(), dirs.begin(), dirs.end());
607 v.insert(v.end(), names.begin(), names.end());
608 v.insert(v.end(), hintNames.begin(), hintNames.end());
609 v.insert(v.end(), dllNames.begin(), dllNames.end());
613 std::vector<Chunk *> DelayLoadContents::getDataChunks() {
614 std::vector<Chunk *> v;
615 v.insert(v.end(), moduleHandles.begin(), moduleHandles.end());
616 v.insert(v.end(), addresses.begin(), addresses.end());
620 uint64_t DelayLoadContents::getDirSize() {
621 return dirs.size() * sizeof(delay_import_directory_table_entry);
624 void DelayLoadContents::create(Defined *h) {
626 std::vector<std::vector<DefinedImportData *>> v = binImports(imports);
628 // Create .didat contents for each DLL.
629 for (std::vector<DefinedImportData *> &syms : v) {
630 // Create the delay import table header.
631 dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
632 auto *dir = make<DelayDirectoryChunk>(dllNames.back());
634 size_t base = addresses.size();
635 Chunk *tm = newTailMergeChunk(dir);
636 for (DefinedImportData *s : syms) {
637 Chunk *t = newThunkChunk(s, tm);
638 auto *a = make<DelayAddressChunk>(t);
639 addresses.push_back(a);
641 StringRef extName = s->getExternalName();
642 if (extName.empty()) {
643 names.push_back(make<OrdinalOnlyChunk>(s->getOrdinal()));
645 auto *c = make<HintNameChunk>(extName, 0);
646 names.push_back(make<LookupChunk>(c));
647 hintNames.push_back(c);
650 thunks.push_back(tm);
651 // Terminate with null values.
652 addresses.push_back(make<NullChunk>(8));
653 names.push_back(make<NullChunk>(8));
655 for (int i = 0, e = syms.size(); i < e; ++i)
656 syms[i]->setLocation(addresses[base + i]);
657 auto *mh = make<NullChunk>(8);
659 moduleHandles.push_back(mh);
661 // Fill the delay import table header fields.
662 dir->moduleHandle = mh;
663 dir->addressTab = addresses[base];
664 dir->nameTab = names[base];
667 // Add null terminator.
668 dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
671 Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {
672 switch (config->machine) {
674 return make<TailMergeChunkX64>(dir, helper);
676 return make<TailMergeChunkX86>(dir, helper);
678 return make<TailMergeChunkARM>(dir, helper);
680 return make<TailMergeChunkARM64>(dir, helper);
682 llvm_unreachable("unsupported machine type");
686 Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s,
688 switch (config->machine) {
690 return make<ThunkChunkX64>(s, tailMerge);
692 return make<ThunkChunkX86>(s, tailMerge);
694 return make<ThunkChunkARM>(s, tailMerge);
696 return make<ThunkChunkARM64>(s, tailMerge);
698 llvm_unreachable("unsupported machine type");
702 EdataContents::EdataContents() {
703 uint16_t maxOrdinal = 0;
704 for (Export &e : config->exports)
705 maxOrdinal = std::max(maxOrdinal, e.ordinal);
707 auto *dllName = make<StringChunk>(sys::path::filename(config->outputFile));
708 auto *addressTab = make<AddressTableChunk>(maxOrdinal);
709 std::vector<Chunk *> names;
710 for (Export &e : config->exports)
712 names.push_back(make<StringChunk>(e.exportName));
714 std::vector<Chunk *> forwards;
715 for (Export &e : config->exports) {
716 if (e.forwardTo.empty())
718 e.forwardChunk = make<StringChunk>(e.forwardTo);
719 forwards.push_back(e.forwardChunk);
722 auto *nameTab = make<NamePointersChunk>(names);
723 auto *ordinalTab = make<ExportOrdinalChunk>(names.size());
724 auto *dir = make<ExportDirectoryChunk>(maxOrdinal, names.size(), dllName,
725 addressTab, nameTab, ordinalTab);
726 chunks.push_back(dir);
727 chunks.push_back(dllName);
728 chunks.push_back(addressTab);
729 chunks.push_back(nameTab);
730 chunks.push_back(ordinalTab);
731 chunks.insert(chunks.end(), names.begin(), names.end());
732 chunks.insert(chunks.end(), forwards.begin(), forwards.end());