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 //===----------------------------------------------------------------------===//
21 #include "COFFLinkerContext.h"
23 #include "SymbolTable.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/Path.h"
29 using namespace llvm::object
;
30 using namespace llvm::support::endian
;
31 using namespace llvm::COFF
;
39 // A chunk for the import descriptor table.
40 class HintNameChunk
: public NonSectionChunk
{
42 HintNameChunk(StringRef n
, uint16_t h
) : name(n
), hint(h
) {}
44 size_t getSize() const override
{
45 // Starts with 2 byte Hint field, followed by a null-terminated string,
46 // ends with 0 or 1 byte padding.
47 return alignTo(name
.size() + 3, 2);
50 void writeTo(uint8_t *buf
) const override
{
51 memset(buf
, 0, getSize());
53 memcpy(buf
+ 2, name
.data(), name
.size());
61 // A chunk for the import descriptor table.
62 class LookupChunk
: public NonSectionChunk
{
64 explicit LookupChunk(Chunk
*c
) : hintName(c
) {
65 setAlignment(config
->wordsize
);
67 size_t getSize() const override
{ return config
->wordsize
; }
69 void writeTo(uint8_t *buf
) const override
{
71 write64le(buf
, hintName
->getRVA());
73 write32le(buf
, hintName
->getRVA());
79 // A chunk for the import descriptor table.
80 // This chunk represent import-by-ordinal symbols.
81 // See Microsoft PE/COFF spec 7.1. Import Header for details.
82 class OrdinalOnlyChunk
: public NonSectionChunk
{
84 explicit OrdinalOnlyChunk(uint16_t v
) : ordinal(v
) {
85 setAlignment(config
->wordsize
);
87 size_t getSize() const override
{ return config
->wordsize
; }
89 void writeTo(uint8_t *buf
) const override
{
90 // An import-by-ordinal slot has MSB 1 to indicate that
91 // this is import-by-ordinal (and not import-by-name).
93 write64le(buf
, (1ULL << 63) | ordinal
);
95 write32le(buf
, (1ULL << 31) | ordinal
);
102 // A chunk for the import descriptor table.
103 class ImportDirectoryChunk
: public NonSectionChunk
{
105 explicit ImportDirectoryChunk(Chunk
*n
) : dllName(n
) {}
106 size_t getSize() const override
{ return sizeof(ImportDirectoryTableEntry
); }
108 void writeTo(uint8_t *buf
) const override
{
109 memset(buf
, 0, getSize());
111 auto *e
= (coff_import_directory_table_entry
*)(buf
);
112 e
->ImportLookupTableRVA
= lookupTab
->getRVA();
113 e
->NameRVA
= dllName
->getRVA();
114 e
->ImportAddressTableRVA
= addressTab
->getRVA();
122 // A chunk representing null terminator in the import table.
123 // Contents of this chunk is always null bytes.
124 class NullChunk
: public NonSectionChunk
{
126 explicit NullChunk(size_t n
) : size(n
) { hasData
= false; }
127 size_t getSize() const override
{ return size
; }
129 void writeTo(uint8_t *buf
) const override
{
130 memset(buf
, 0, size
);
137 static std::vector
<std::vector
<DefinedImportData
*>>
138 binImports(const std::vector
<DefinedImportData
*> &imports
) {
139 // Group DLL-imported symbols by DLL name because that's how
140 // symbols are laid out in the import descriptor table.
141 auto less
= [](const std::string
&a
, const std::string
&b
) {
142 return config
->dllOrder
[a
] < config
->dllOrder
[b
];
144 std::map
<std::string
, std::vector
<DefinedImportData
*>,
145 bool(*)(const std::string
&, const std::string
&)> m(less
);
146 for (DefinedImportData
*sym
: imports
)
147 m
[sym
->getDLLName().lower()].push_back(sym
);
149 std::vector
<std::vector
<DefinedImportData
*>> v
;
151 // Sort symbols by name for each group.
152 std::vector
<DefinedImportData
*> &syms
= kv
.second
;
153 std::sort(syms
.begin(), syms
.end(),
154 [](DefinedImportData
*a
, DefinedImportData
*b
) {
155 return a
->getName() < b
->getName();
157 v
.push_back(std::move(syms
));
162 // See Microsoft PE/COFF spec 4.3 for details.
164 // A chunk for the delay import descriptor table etnry.
165 class DelayDirectoryChunk
: public NonSectionChunk
{
167 explicit DelayDirectoryChunk(Chunk
*n
) : dllName(n
) {}
169 size_t getSize() const override
{
170 return sizeof(delay_import_directory_table_entry
);
173 void writeTo(uint8_t *buf
) const override
{
174 memset(buf
, 0, getSize());
176 auto *e
= (delay_import_directory_table_entry
*)(buf
);
178 e
->Name
= dllName
->getRVA();
179 e
->ModuleHandle
= moduleHandle
->getRVA();
180 e
->DelayImportAddressTable
= addressTab
->getRVA();
181 e
->DelayImportNameTable
= nameTab
->getRVA();
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>
199 static const uint8_t tailMergeX64
[] = {
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
221 0xFF, 0xE0, // jmp rax
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>
229 static const uint8_t tailMergeX86
[] = {
233 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
234 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8
237 0xFF, 0xE0, // jmp eax
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>
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}
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>
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
294 // A chunk for the delay import thunk.
295 class ThunkChunkX64
: public NonSectionChunk
{
297 ThunkChunkX64(Defined
*i
, Chunk
*tm
) : imp(i
), tailMerge(tm
) {}
299 size_t getSize() const override
{ return sizeof(thunkX64
); }
301 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);
307 Defined
*imp
= nullptr;
308 Chunk
*tailMerge
= nullptr;
311 class TailMergeChunkX64
: public NonSectionChunk
{
313 TailMergeChunkX64(Chunk
*d
, Defined
*h
) : desc(d
), helper(h
) {}
315 size_t getSize() const override
{ return sizeof(tailMergeX64
); }
317 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);
323 Chunk
*desc
= nullptr;
324 Defined
*helper
= nullptr;
327 class ThunkChunkX86
: public NonSectionChunk
{
329 ThunkChunkX86(Defined
*i
, Chunk
*tm
) : imp(i
), tailMerge(tm
) {}
331 size_t getSize() const override
{ return sizeof(thunkX86
); }
333 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);
339 void getBaserels(std::vector
<Baserel
> *res
) override
{
340 res
->emplace_back(rva
+ 1);
343 Defined
*imp
= nullptr;
344 Chunk
*tailMerge
= nullptr;
347 class TailMergeChunkX86
: public NonSectionChunk
{
349 TailMergeChunkX86(Chunk
*d
, Defined
*h
) : desc(d
), helper(h
) {}
351 size_t getSize() const override
{ return sizeof(tailMergeX86
); }
353 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);
359 void getBaserels(std::vector
<Baserel
> *res
) override
{
360 res
->emplace_back(rva
+ 4);
363 Chunk
*desc
= nullptr;
364 Defined
*helper
= nullptr;
367 class ThunkChunkARM
: public NonSectionChunk
{
369 ThunkChunkARM(Defined
*i
, Chunk
*tm
) : imp(i
), tailMerge(tm
) {
373 size_t getSize() const override
{ return sizeof(thunkARM
); }
375 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);
381 void getBaserels(std::vector
<Baserel
> *res
) override
{
382 res
->emplace_back(rva
+ 0, IMAGE_REL_BASED_ARM_MOV32T
);
385 Defined
*imp
= nullptr;
386 Chunk
*tailMerge
= nullptr;
389 class TailMergeChunkARM
: public NonSectionChunk
{
391 TailMergeChunkARM(Chunk
*d
, Defined
*h
) : desc(d
), helper(h
) {
395 size_t getSize() const override
{ return sizeof(tailMergeARM
); }
397 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);
403 void getBaserels(std::vector
<Baserel
> *res
) override
{
404 res
->emplace_back(rva
+ 14, IMAGE_REL_BASED_ARM_MOV32T
);
407 Chunk
*desc
= nullptr;
408 Defined
*helper
= nullptr;
411 class ThunkChunkARM64
: public NonSectionChunk
{
413 ThunkChunkARM64(Defined
*i
, Chunk
*tm
) : imp(i
), tailMerge(tm
) {
417 size_t getSize() const override
{ return sizeof(thunkARM64
); }
419 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);
426 Defined
*imp
= nullptr;
427 Chunk
*tailMerge
= nullptr;
430 class TailMergeChunkARM64
: public NonSectionChunk
{
432 TailMergeChunkARM64(Chunk
*d
, Defined
*h
) : desc(d
), helper(h
) {
436 size_t getSize() const override
{ return sizeof(tailMergeARM64
); }
438 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);
445 Chunk
*desc
= nullptr;
446 Defined
*helper
= nullptr;
449 // A chunk for the import descriptor table.
450 class DelayAddressChunk
: public NonSectionChunk
{
452 explicit DelayAddressChunk(Chunk
*c
) : thunk(c
) {
453 setAlignment(config
->wordsize
);
455 size_t getSize() const override
{ return config
->wordsize
; }
457 void writeTo(uint8_t *buf
) const override
{
458 if (config
->is64()) {
459 write64le(buf
, thunk
->getRVA() + config
->imageBase
);
462 // Pointer to thumb code must have the LSB set, so adjust it.
463 if (config
->machine
== ARMNT
)
465 write32le(buf
, (thunk
->getRVA() + config
->imageBase
) | bit
);
469 void getBaserels(std::vector
<Baserel
> *res
) override
{
470 res
->emplace_back(rva
);
477 // Read Microsoft PE/COFF spec 5.3 for details.
479 // A chunk for the export descriptor table.
480 class ExportDirectoryChunk
: public NonSectionChunk
{
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
),
486 size_t getSize() const override
{
487 return sizeof(export_directory_table_entry
);
490 void writeTo(uint8_t *buf
) const override
{
491 memset(buf
, 0, getSize());
493 auto *e
= (export_directory_table_entry
*)(buf
);
494 e
->NameRVA
= dllName
->getRVA();
496 e
->AddressTableEntries
= maxOrdinal
+ 1;
497 e
->NumberOfNamePointers
= nameTabSize
;
498 e
->ExportAddressTableRVA
= addressTab
->getRVA();
499 e
->NamePointerRVA
= nameTab
->getRVA();
500 e
->OrdinalTableRVA
= ordinalTab
->getRVA();
504 uint16_t nameTabSize
;
511 class AddressTableChunk
: public NonSectionChunk
{
513 explicit AddressTableChunk(size_t maxOrdinal
) : size(maxOrdinal
+ 1) {}
514 size_t getSize() const override
{ return size
* 4; }
516 void writeTo(uint8_t *buf
) const override
{
517 memset(buf
, 0, getSize());
519 for (const Export
&e
: config
->exports
) {
520 uint8_t *p
= buf
+ e
.ordinal
* 4;
522 // Pointer to thumb code must have the LSB set, so adjust it.
523 if (config
->machine
== ARMNT
&& !e
.data
)
525 if (e
.forwardChunk
) {
526 write32le(p
, e
.forwardChunk
->getRVA() | bit
);
528 assert(cast
<Defined
>(e
.sym
)->getRVA() != 0 &&
529 "Exported symbol unmapped");
530 write32le(p
, cast
<Defined
>(e
.sym
)->getRVA() | bit
);
539 class NamePointersChunk
: public NonSectionChunk
{
541 explicit NamePointersChunk(std::vector
<Chunk
*> &v
) : chunks(v
) {}
542 size_t getSize() const override
{ return chunks
.size() * 4; }
544 void writeTo(uint8_t *buf
) const override
{
545 for (Chunk
*c
: chunks
) {
546 write32le(buf
, c
->getRVA());
552 std::vector
<Chunk
*> chunks
;
555 class ExportOrdinalChunk
: public NonSectionChunk
{
557 explicit ExportOrdinalChunk(size_t i
) : size(i
) {}
558 size_t getSize() const override
{ return size
* 2; }
560 void writeTo(uint8_t *buf
) const override
{
561 for (Export
&e
: config
->exports
) {
564 write16le(buf
, e
.ordinal
);
573 } // anonymous namespace
575 void IdataContents::create() {
576 std::vector
<std::vector
<DefinedImportData
*>> v
= binImports(imports
);
578 // Create .idata contents for each DLL.
579 for (std::vector
<DefinedImportData
*> &syms
: v
) {
580 // Create lookup and address tables. If they have external names,
581 // we need to create hintName chunks to store the names.
582 // If they don't (if they are import-by-ordinals), we store only
583 // ordinal values to the table.
584 size_t base
= lookups
.size();
585 for (DefinedImportData
*s
: syms
) {
586 uint16_t ord
= s
->getOrdinal();
587 if (s
->getExternalName().empty()) {
588 lookups
.push_back(make
<OrdinalOnlyChunk
>(ord
));
589 addresses
.push_back(make
<OrdinalOnlyChunk
>(ord
));
592 auto *c
= make
<HintNameChunk
>(s
->getExternalName(), ord
);
593 lookups
.push_back(make
<LookupChunk
>(c
));
594 addresses
.push_back(make
<LookupChunk
>(c
));
597 // Terminate with null values.
598 lookups
.push_back(make
<NullChunk
>(config
->wordsize
));
599 addresses
.push_back(make
<NullChunk
>(config
->wordsize
));
601 for (int i
= 0, e
= syms
.size(); i
< e
; ++i
)
602 syms
[i
]->setLocation(addresses
[base
+ i
]);
604 // Create the import table header.
605 dllNames
.push_back(make
<StringChunk
>(syms
[0]->getDLLName()));
606 auto *dir
= make
<ImportDirectoryChunk
>(dllNames
.back());
607 dir
->lookupTab
= lookups
[base
];
608 dir
->addressTab
= addresses
[base
];
611 // Add null terminator.
612 dirs
.push_back(make
<NullChunk
>(sizeof(ImportDirectoryTableEntry
)));
615 std::vector
<Chunk
*> DelayLoadContents::getChunks() {
616 std::vector
<Chunk
*> v
;
617 v
.insert(v
.end(), dirs
.begin(), dirs
.end());
618 v
.insert(v
.end(), names
.begin(), names
.end());
619 v
.insert(v
.end(), hintNames
.begin(), hintNames
.end());
620 v
.insert(v
.end(), dllNames
.begin(), dllNames
.end());
624 std::vector
<Chunk
*> DelayLoadContents::getDataChunks() {
625 std::vector
<Chunk
*> v
;
626 v
.insert(v
.end(), moduleHandles
.begin(), moduleHandles
.end());
627 v
.insert(v
.end(), addresses
.begin(), addresses
.end());
631 uint64_t DelayLoadContents::getDirSize() {
632 return dirs
.size() * sizeof(delay_import_directory_table_entry
);
635 void DelayLoadContents::create(COFFLinkerContext
&ctx
, Defined
*h
) {
637 std::vector
<std::vector
<DefinedImportData
*>> v
= binImports(imports
);
639 // Create .didat contents for each DLL.
640 for (std::vector
<DefinedImportData
*> &syms
: v
) {
641 // Create the delay import table header.
642 dllNames
.push_back(make
<StringChunk
>(syms
[0]->getDLLName()));
643 auto *dir
= make
<DelayDirectoryChunk
>(dllNames
.back());
645 size_t base
= addresses
.size();
646 Chunk
*tm
= newTailMergeChunk(dir
);
647 for (DefinedImportData
*s
: syms
) {
648 Chunk
*t
= newThunkChunk(s
, tm
);
649 auto *a
= make
<DelayAddressChunk
>(t
);
650 addresses
.push_back(a
);
652 StringRef extName
= s
->getExternalName();
653 if (extName
.empty()) {
654 names
.push_back(make
<OrdinalOnlyChunk
>(s
->getOrdinal()));
656 auto *c
= make
<HintNameChunk
>(extName
, 0);
657 names
.push_back(make
<LookupChunk
>(c
));
658 hintNames
.push_back(c
);
659 // Add a syntentic symbol for this load thunk, using the "__imp_load"
660 // prefix, in case this thunk needs to be added to the list of valid
661 // call targets for Control Flow Guard.
662 StringRef symName
= saver().save("__imp_load_" + extName
);
664 cast
<DefinedSynthetic
>(ctx
.symtab
.addSynthetic(symName
, t
));
667 thunks
.push_back(tm
);
669 saver().save("__tailMerge_" + syms
[0]->getDLLName().lower());
670 ctx
.symtab
.addSynthetic(tmName
, tm
);
671 // Terminate with null values.
672 addresses
.push_back(make
<NullChunk
>(8));
673 names
.push_back(make
<NullChunk
>(8));
675 for (int i
= 0, e
= syms
.size(); i
< e
; ++i
)
676 syms
[i
]->setLocation(addresses
[base
+ i
]);
677 auto *mh
= make
<NullChunk
>(8);
679 moduleHandles
.push_back(mh
);
681 // Fill the delay import table header fields.
682 dir
->moduleHandle
= mh
;
683 dir
->addressTab
= addresses
[base
];
684 dir
->nameTab
= names
[base
];
687 // Add null terminator.
688 dirs
.push_back(make
<NullChunk
>(sizeof(delay_import_directory_table_entry
)));
691 Chunk
*DelayLoadContents::newTailMergeChunk(Chunk
*dir
) {
692 switch (config
->machine
) {
694 return make
<TailMergeChunkX64
>(dir
, helper
);
696 return make
<TailMergeChunkX86
>(dir
, helper
);
698 return make
<TailMergeChunkARM
>(dir
, helper
);
700 return make
<TailMergeChunkARM64
>(dir
, helper
);
702 llvm_unreachable("unsupported machine type");
706 Chunk
*DelayLoadContents::newThunkChunk(DefinedImportData
*s
,
708 switch (config
->machine
) {
710 return make
<ThunkChunkX64
>(s
, tailMerge
);
712 return make
<ThunkChunkX86
>(s
, tailMerge
);
714 return make
<ThunkChunkARM
>(s
, tailMerge
);
716 return make
<ThunkChunkARM64
>(s
, tailMerge
);
718 llvm_unreachable("unsupported machine type");
722 EdataContents::EdataContents() {
723 uint16_t maxOrdinal
= 0;
724 for (Export
&e
: config
->exports
)
725 maxOrdinal
= std::max(maxOrdinal
, e
.ordinal
);
727 auto *dllName
= make
<StringChunk
>(sys::path::filename(config
->outputFile
));
728 auto *addressTab
= make
<AddressTableChunk
>(maxOrdinal
);
729 std::vector
<Chunk
*> names
;
730 for (Export
&e
: config
->exports
)
732 names
.push_back(make
<StringChunk
>(e
.exportName
));
734 std::vector
<Chunk
*> forwards
;
735 for (Export
&e
: config
->exports
) {
736 if (e
.forwardTo
.empty())
738 e
.forwardChunk
= make
<StringChunk
>(e
.forwardTo
);
739 forwards
.push_back(e
.forwardChunk
);
742 auto *nameTab
= make
<NamePointersChunk
>(names
);
743 auto *ordinalTab
= make
<ExportOrdinalChunk
>(names
.size());
744 auto *dir
= make
<ExportDirectoryChunk
>(maxOrdinal
, names
.size(), dllName
,
745 addressTab
, nameTab
, ordinalTab
);
746 chunks
.push_back(dir
);
747 chunks
.push_back(dllName
);
748 chunks
.push_back(addressTab
);
749 chunks
.push_back(nameTab
);
750 chunks
.push_back(ordinalTab
);
751 chunks
.insert(chunks
.end(), names
.begin(), names
.end());
752 chunks
.insert(chunks
.end(), forwards
.begin(), forwards
.end());