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/ADT/STLExtras.h"
25 #include "llvm/Object/COFF.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/Path.h"
30 using namespace llvm::object
;
31 using namespace llvm::support::endian
;
32 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(COFFLinkerContext
&ctx
, Chunk
*c
)
65 : hintName(c
), ctx(ctx
) {
66 setAlignment(ctx
.config
.wordsize
);
68 size_t getSize() const override
{ return ctx
.config
.wordsize
; }
70 void writeTo(uint8_t *buf
) const override
{
71 if (ctx
.config
.is64())
72 write64le(buf
, hintName
->getRVA());
74 write32le(buf
, hintName
->getRVA());
80 COFFLinkerContext
&ctx
;
83 // A chunk for the import descriptor table.
84 // This chunk represent import-by-ordinal symbols.
85 // See Microsoft PE/COFF spec 7.1. Import Header for details.
86 class OrdinalOnlyChunk
: public NonSectionChunk
{
88 explicit OrdinalOnlyChunk(COFFLinkerContext
&c
, uint16_t v
)
89 : ordinal(v
), ctx(c
) {
90 setAlignment(ctx
.config
.wordsize
);
92 size_t getSize() const override
{ return ctx
.config
.wordsize
; }
94 void writeTo(uint8_t *buf
) const override
{
95 // An import-by-ordinal slot has MSB 1 to indicate that
96 // this is import-by-ordinal (and not import-by-name).
97 if (ctx
.config
.is64()) {
98 write64le(buf
, (1ULL << 63) | ordinal
);
100 write32le(buf
, (1ULL << 31) | ordinal
);
107 COFFLinkerContext
&ctx
;
110 // A chunk for the import descriptor table.
111 class ImportDirectoryChunk
: public NonSectionChunk
{
113 explicit ImportDirectoryChunk(Chunk
*n
) : dllName(n
) {}
114 size_t getSize() const override
{ return sizeof(ImportDirectoryTableEntry
); }
116 void writeTo(uint8_t *buf
) const override
{
117 memset(buf
, 0, getSize());
119 auto *e
= (coff_import_directory_table_entry
*)(buf
);
120 e
->ImportLookupTableRVA
= lookupTab
->getRVA();
121 e
->NameRVA
= dllName
->getRVA();
122 e
->ImportAddressTableRVA
= addressTab
->getRVA();
130 // A chunk representing null terminator in the import table.
131 // Contents of this chunk is always null bytes.
132 class NullChunk
: public NonSectionChunk
{
134 explicit NullChunk(size_t n
) : size(n
) { hasData
= false; }
135 size_t getSize() const override
{ return size
; }
137 void writeTo(uint8_t *buf
) const override
{
138 memset(buf
, 0, size
);
145 static std::vector
<std::vector
<DefinedImportData
*>>
146 binImports(COFFLinkerContext
&ctx
,
147 const std::vector
<DefinedImportData
*> &imports
) {
148 // Group DLL-imported symbols by DLL name because that's how
149 // symbols are laid out in the import descriptor table.
150 auto less
= [&ctx
](const std::string
&a
, const std::string
&b
) {
151 return ctx
.config
.dllOrder
[a
] < ctx
.config
.dllOrder
[b
];
153 std::map
<std::string
, std::vector
<DefinedImportData
*>, decltype(less
)> m(
155 for (DefinedImportData
*sym
: imports
)
156 m
[sym
->getDLLName().lower()].push_back(sym
);
158 std::vector
<std::vector
<DefinedImportData
*>> v
;
160 // Sort symbols by name for each group.
161 std::vector
<DefinedImportData
*> &syms
= kv
.second
;
162 llvm::sort(syms
, [](DefinedImportData
*a
, DefinedImportData
*b
) {
163 return a
->getName() < b
->getName();
165 v
.push_back(std::move(syms
));
170 // See Microsoft PE/COFF spec 4.3 for details.
172 // A chunk for the delay import descriptor table etnry.
173 class DelayDirectoryChunk
: public NonSectionChunk
{
175 explicit DelayDirectoryChunk(Chunk
*n
) : dllName(n
) {}
177 size_t getSize() const override
{
178 return sizeof(delay_import_directory_table_entry
);
181 void writeTo(uint8_t *buf
) const override
{
182 memset(buf
, 0, getSize());
184 auto *e
= (delay_import_directory_table_entry
*)(buf
);
186 e
->Name
= dllName
->getRVA();
187 e
->ModuleHandle
= moduleHandle
->getRVA();
188 e
->DelayImportAddressTable
= addressTab
->getRVA();
189 e
->DelayImportNameTable
= nameTab
->getRVA();
198 // Initial contents for delay-loaded functions.
199 // This code calls __delayLoadHelper2 function to resolve a symbol
200 // which then overwrites its jump table slot with the result
201 // for subsequent function calls.
202 static const uint8_t thunkX64
[] = {
203 0x48, 0x8D, 0x05, 0, 0, 0, 0, // lea rax, [__imp_<FUNCNAME>]
204 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
207 static const uint8_t tailMergeX64
[] = {
210 0x41, 0x50, // push r8
211 0x41, 0x51, // push r9
212 0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h
213 0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0
214 0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1
215 0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2
216 0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3
217 0x48, 0x8B, 0xD0, // mov rdx, rax
218 0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...]
219 0xE8, 0, 0, 0, 0, // call __delayLoadHelper2
220 0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp]
221 0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h]
222 0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h]
223 0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h]
224 0x48, 0x83, 0xC4, 0x48, // add rsp, 48h
225 0x41, 0x59, // pop r9
226 0x41, 0x58, // pop r8
229 0xFF, 0xE0, // jmp rax
232 static const uint8_t tailMergeUnwindInfoX64
[] = {
233 0x01, // Version=1, Flags=UNW_FLAG_NHANDLER
234 0x0a, // Size of prolog
235 0x05, // Count of unwind codes
236 0x00, // No frame register
237 0x0a, 0x82, // Offset 0xa: UWOP_ALLOC_SMALL(0x48)
238 0x06, 0x02, // Offset 6: UWOP_ALLOC_SMALL(8)
239 0x04, 0x02, // Offset 4: UWOP_ALLOC_SMALL(8)
240 0x02, 0x02, // Offset 2: UWOP_ALLOC_SMALL(8)
241 0x01, 0x02, // Offset 1: UWOP_ALLOC_SMALL(8)
242 0x00, 0x00 // Padding to align on 32-bits
245 static const uint8_t thunkX86
[] = {
246 0xB8, 0, 0, 0, 0, // mov eax, offset ___imp__<FUNCNAME>
247 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
250 static const uint8_t tailMergeX86
[] = {
254 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
255 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8
258 0xFF, 0xE0, // jmp eax
261 static const uint8_t thunkARM
[] = {
262 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 __imp_<FUNCNAME>
263 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 __imp_<FUNCNAME>
264 0x00, 0xf0, 0x00, 0xb8, // b.w __tailMerge_<lib>
267 static const uint8_t tailMergeARM
[] = {
268 0x2d, 0xe9, 0x0f, 0x48, // push.w {r0, r1, r2, r3, r11, lr}
269 0x0d, 0xf2, 0x10, 0x0b, // addw r11, sp, #16
270 0x2d, 0xed, 0x10, 0x0b, // vpush {d0, d1, d2, d3, d4, d5, d6, d7}
271 0x61, 0x46, // mov r1, ip
272 0x40, 0xf2, 0x00, 0x00, // mov.w r0, #0 DELAY_IMPORT_DESCRIPTOR
273 0xc0, 0xf2, 0x00, 0x00, // mov.t r0, #0 DELAY_IMPORT_DESCRIPTOR
274 0x00, 0xf0, 0x00, 0xd0, // bl #0 __delayLoadHelper2
275 0x84, 0x46, // mov ip, r0
276 0xbd, 0xec, 0x10, 0x0b, // vpop {d0, d1, d2, d3, d4, d5, d6, d7}
277 0xbd, 0xe8, 0x0f, 0x48, // pop.w {r0, r1, r2, r3, r11, lr}
281 static const uint8_t thunkARM64
[] = {
282 0x11, 0x00, 0x00, 0x90, // adrp x17, #0 __imp_<FUNCNAME>
283 0x31, 0x02, 0x00, 0x91, // add x17, x17, #0 :lo12:__imp_<FUNCNAME>
284 0x00, 0x00, 0x00, 0x14, // b __tailMerge_<lib>
287 static const uint8_t tailMergeARM64
[] = {
288 0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]!
289 0xfd, 0x03, 0x00, 0x91, // mov x29, sp
290 0xe0, 0x07, 0x01, 0xa9, // stp x0, x1, [sp, #16]
291 0xe2, 0x0f, 0x02, 0xa9, // stp x2, x3, [sp, #32]
292 0xe4, 0x17, 0x03, 0xa9, // stp x4, x5, [sp, #48]
293 0xe6, 0x1f, 0x04, 0xa9, // stp x6, x7, [sp, #64]
294 0xe0, 0x87, 0x02, 0xad, // stp q0, q1, [sp, #80]
295 0xe2, 0x8f, 0x03, 0xad, // stp q2, q3, [sp, #112]
296 0xe4, 0x97, 0x04, 0xad, // stp q4, q5, [sp, #144]
297 0xe6, 0x9f, 0x05, 0xad, // stp q6, q7, [sp, #176]
298 0xe1, 0x03, 0x11, 0xaa, // mov x1, x17
299 0x00, 0x00, 0x00, 0x90, // adrp x0, #0 DELAY_IMPORT_DESCRIPTOR
300 0x00, 0x00, 0x00, 0x91, // add x0, x0, #0 :lo12:DELAY_IMPORT_DESCRIPTOR
301 0x00, 0x00, 0x00, 0x94, // bl #0 __delayLoadHelper2
302 0xf0, 0x03, 0x00, 0xaa, // mov x16, x0
303 0xe6, 0x9f, 0x45, 0xad, // ldp q6, q7, [sp, #176]
304 0xe4, 0x97, 0x44, 0xad, // ldp q4, q5, [sp, #144]
305 0xe2, 0x8f, 0x43, 0xad, // ldp q2, q3, [sp, #112]
306 0xe0, 0x87, 0x42, 0xad, // ldp q0, q1, [sp, #80]
307 0xe6, 0x1f, 0x44, 0xa9, // ldp x6, x7, [sp, #64]
308 0xe4, 0x17, 0x43, 0xa9, // ldp x4, x5, [sp, #48]
309 0xe2, 0x0f, 0x42, 0xa9, // ldp x2, x3, [sp, #32]
310 0xe0, 0x07, 0x41, 0xa9, // ldp x0, x1, [sp, #16]
311 0xfd, 0x7b, 0xcd, 0xa8, // ldp x29, x30, [sp], #208
312 0x00, 0x02, 0x1f, 0xd6, // br x16
315 // A chunk for the delay import thunk.
316 class ThunkChunkX64
: public NonSectionCodeChunk
{
318 ThunkChunkX64(Defined
*i
, Chunk
*tm
) : imp(i
), tailMerge(tm
) {}
320 size_t getSize() const override
{ return sizeof(thunkX64
); }
321 MachineTypes
getMachine() const override
{ return AMD64
; }
323 void writeTo(uint8_t *buf
) const override
{
324 memcpy(buf
, thunkX64
, sizeof(thunkX64
));
325 write32le(buf
+ 3, imp
->getRVA() - rva
- 7);
326 write32le(buf
+ 8, tailMerge
->getRVA() - rva
- 12);
329 Defined
*imp
= nullptr;
330 Chunk
*tailMerge
= nullptr;
333 class TailMergeChunkX64
: public NonSectionCodeChunk
{
335 TailMergeChunkX64(Chunk
*d
, Defined
*h
) : desc(d
), helper(h
) {}
337 size_t getSize() const override
{ return sizeof(tailMergeX64
); }
338 MachineTypes
getMachine() const override
{ return AMD64
; }
340 void writeTo(uint8_t *buf
) const override
{
341 memcpy(buf
, tailMergeX64
, sizeof(tailMergeX64
));
342 write32le(buf
+ 39, desc
->getRVA() - rva
- 43);
343 write32le(buf
+ 44, helper
->getRVA() - rva
- 48);
346 Chunk
*desc
= nullptr;
347 Defined
*helper
= nullptr;
350 class TailMergePDataChunkX64
: public NonSectionChunk
{
352 TailMergePDataChunkX64(Chunk
*tm
, Chunk
*unwind
) : tm(tm
), unwind(unwind
) {
354 // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-runtime_function
358 size_t getSize() const override
{ return 3 * sizeof(uint32_t); }
360 void writeTo(uint8_t *buf
) const override
{
361 write32le(buf
+ 0, tm
->getRVA()); // TailMergeChunk start RVA
362 write32le(buf
+ 4, tm
->getRVA() + tm
->getSize()); // TailMergeChunk stop RVA
363 write32le(buf
+ 8, unwind
->getRVA()); // UnwindInfo RVA
367 Chunk
*unwind
= nullptr;
370 class TailMergeUnwindInfoX64
: public NonSectionChunk
{
372 TailMergeUnwindInfoX64() {
374 // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-unwind_info
378 size_t getSize() const override
{ return sizeof(tailMergeUnwindInfoX64
); }
380 void writeTo(uint8_t *buf
) const override
{
381 memcpy(buf
, tailMergeUnwindInfoX64
, sizeof(tailMergeUnwindInfoX64
));
385 class ThunkChunkX86
: public NonSectionCodeChunk
{
387 ThunkChunkX86(COFFLinkerContext
&ctx
, Defined
*i
, Chunk
*tm
)
388 : imp(i
), tailMerge(tm
), ctx(ctx
) {}
390 size_t getSize() const override
{ return sizeof(thunkX86
); }
391 MachineTypes
getMachine() const override
{ return I386
; }
393 void writeTo(uint8_t *buf
) const override
{
394 memcpy(buf
, thunkX86
, sizeof(thunkX86
));
395 write32le(buf
+ 1, imp
->getRVA() + ctx
.config
.imageBase
);
396 write32le(buf
+ 6, tailMerge
->getRVA() - rva
- 10);
399 void getBaserels(std::vector
<Baserel
> *res
) override
{
400 res
->emplace_back(rva
+ 1, ctx
.config
.machine
);
403 Defined
*imp
= nullptr;
404 Chunk
*tailMerge
= nullptr;
407 const COFFLinkerContext
&ctx
;
410 class TailMergeChunkX86
: public NonSectionCodeChunk
{
412 TailMergeChunkX86(COFFLinkerContext
&ctx
, Chunk
*d
, Defined
*h
)
413 : desc(d
), helper(h
), ctx(ctx
) {}
415 size_t getSize() const override
{ return sizeof(tailMergeX86
); }
416 MachineTypes
getMachine() const override
{ return I386
; }
418 void writeTo(uint8_t *buf
) const override
{
419 memcpy(buf
, tailMergeX86
, sizeof(tailMergeX86
));
420 write32le(buf
+ 4, desc
->getRVA() + ctx
.config
.imageBase
);
421 write32le(buf
+ 9, helper
->getRVA() - rva
- 13);
424 void getBaserels(std::vector
<Baserel
> *res
) override
{
425 res
->emplace_back(rva
+ 4, ctx
.config
.machine
);
428 Chunk
*desc
= nullptr;
429 Defined
*helper
= nullptr;
432 const COFFLinkerContext
&ctx
;
435 class ThunkChunkARM
: public NonSectionCodeChunk
{
437 ThunkChunkARM(COFFLinkerContext
&ctx
, Defined
*i
, Chunk
*tm
)
438 : imp(i
), tailMerge(tm
), ctx(ctx
) {
442 size_t getSize() const override
{ return sizeof(thunkARM
); }
443 MachineTypes
getMachine() const override
{ return ARMNT
; }
445 void writeTo(uint8_t *buf
) const override
{
446 memcpy(buf
, thunkARM
, sizeof(thunkARM
));
447 applyMOV32T(buf
+ 0, imp
->getRVA() + ctx
.config
.imageBase
);
448 applyBranch24T(buf
+ 8, tailMerge
->getRVA() - rva
- 12);
451 void getBaserels(std::vector
<Baserel
> *res
) override
{
452 res
->emplace_back(rva
+ 0, IMAGE_REL_BASED_ARM_MOV32T
);
455 Defined
*imp
= nullptr;
456 Chunk
*tailMerge
= nullptr;
459 const COFFLinkerContext
&ctx
;
462 class TailMergeChunkARM
: public NonSectionCodeChunk
{
464 TailMergeChunkARM(COFFLinkerContext
&ctx
, Chunk
*d
, Defined
*h
)
465 : desc(d
), helper(h
), ctx(ctx
) {
469 size_t getSize() const override
{ return sizeof(tailMergeARM
); }
470 MachineTypes
getMachine() const override
{ return ARMNT
; }
472 void writeTo(uint8_t *buf
) const override
{
473 memcpy(buf
, tailMergeARM
, sizeof(tailMergeARM
));
474 applyMOV32T(buf
+ 14, desc
->getRVA() + ctx
.config
.imageBase
);
475 applyBranch24T(buf
+ 22, helper
->getRVA() - rva
- 26);
478 void getBaserels(std::vector
<Baserel
> *res
) override
{
479 res
->emplace_back(rva
+ 14, IMAGE_REL_BASED_ARM_MOV32T
);
482 Chunk
*desc
= nullptr;
483 Defined
*helper
= nullptr;
486 const COFFLinkerContext
&ctx
;
489 class ThunkChunkARM64
: public NonSectionCodeChunk
{
491 ThunkChunkARM64(Defined
*i
, Chunk
*tm
) : imp(i
), tailMerge(tm
) {
495 size_t getSize() const override
{ return sizeof(thunkARM64
); }
496 MachineTypes
getMachine() const override
{ return ARM64
; }
498 void writeTo(uint8_t *buf
) const override
{
499 memcpy(buf
, thunkARM64
, sizeof(thunkARM64
));
500 applyArm64Addr(buf
+ 0, imp
->getRVA(), rva
+ 0, 12);
501 applyArm64Imm(buf
+ 4, imp
->getRVA() & 0xfff, 0);
502 applyArm64Branch26(buf
+ 8, tailMerge
->getRVA() - rva
- 8);
505 Defined
*imp
= nullptr;
506 Chunk
*tailMerge
= nullptr;
509 class TailMergeChunkARM64
: public NonSectionCodeChunk
{
511 TailMergeChunkARM64(Chunk
*d
, Defined
*h
) : desc(d
), helper(h
) {
515 size_t getSize() const override
{ return sizeof(tailMergeARM64
); }
516 MachineTypes
getMachine() const override
{ return ARM64
; }
518 void writeTo(uint8_t *buf
) const override
{
519 memcpy(buf
, tailMergeARM64
, sizeof(tailMergeARM64
));
520 applyArm64Addr(buf
+ 44, desc
->getRVA(), rva
+ 44, 12);
521 applyArm64Imm(buf
+ 48, desc
->getRVA() & 0xfff, 0);
522 applyArm64Branch26(buf
+ 52, helper
->getRVA() - rva
- 52);
525 Chunk
*desc
= nullptr;
526 Defined
*helper
= nullptr;
529 // A chunk for the import descriptor table.
530 class DelayAddressChunk
: public NonSectionChunk
{
532 explicit DelayAddressChunk(COFFLinkerContext
&ctx
, Chunk
*c
)
533 : thunk(c
), ctx(ctx
) {
534 setAlignment(ctx
.config
.wordsize
);
536 size_t getSize() const override
{ return ctx
.config
.wordsize
; }
538 void writeTo(uint8_t *buf
) const override
{
539 if (ctx
.config
.is64()) {
540 write64le(buf
, thunk
->getRVA() + ctx
.config
.imageBase
);
543 // Pointer to thumb code must have the LSB set, so adjust it.
544 if (ctx
.config
.machine
== ARMNT
)
546 write32le(buf
, (thunk
->getRVA() + ctx
.config
.imageBase
) | bit
);
550 void getBaserels(std::vector
<Baserel
> *res
) override
{
551 res
->emplace_back(rva
, ctx
.config
.machine
);
557 const COFFLinkerContext
&ctx
;
561 // Read Microsoft PE/COFF spec 5.3 for details.
563 // A chunk for the export descriptor table.
564 class ExportDirectoryChunk
: public NonSectionChunk
{
566 ExportDirectoryChunk(int baseOrdinal
, int maxOrdinal
, int nameTabSize
,
567 Chunk
*d
, Chunk
*a
, Chunk
*n
, Chunk
*o
)
568 : baseOrdinal(baseOrdinal
), maxOrdinal(maxOrdinal
),
569 nameTabSize(nameTabSize
), dllName(d
), addressTab(a
), nameTab(n
),
572 size_t getSize() const override
{
573 return sizeof(export_directory_table_entry
);
576 void writeTo(uint8_t *buf
) const override
{
577 memset(buf
, 0, getSize());
579 auto *e
= (export_directory_table_entry
*)(buf
);
580 e
->NameRVA
= dllName
->getRVA();
581 e
->OrdinalBase
= baseOrdinal
;
582 e
->AddressTableEntries
= (maxOrdinal
- baseOrdinal
) + 1;
583 e
->NumberOfNamePointers
= nameTabSize
;
584 e
->ExportAddressTableRVA
= addressTab
->getRVA();
585 e
->NamePointerRVA
= nameTab
->getRVA();
586 e
->OrdinalTableRVA
= ordinalTab
->getRVA();
589 uint16_t baseOrdinal
;
591 uint16_t nameTabSize
;
598 class AddressTableChunk
: public NonSectionChunk
{
600 explicit AddressTableChunk(COFFLinkerContext
&ctx
, size_t baseOrdinal
,
602 : baseOrdinal(baseOrdinal
), size((maxOrdinal
- baseOrdinal
) + 1),
604 size_t getSize() const override
{ return size
* 4; }
606 void writeTo(uint8_t *buf
) const override
{
607 memset(buf
, 0, getSize());
609 for (const Export
&e
: ctx
.config
.exports
) {
610 assert(e
.ordinal
>= baseOrdinal
&& "Export symbol has invalid ordinal");
611 // Subtract the OrdinalBase to get the index.
612 uint8_t *p
= buf
+ (e
.ordinal
- baseOrdinal
) * 4;
614 // Pointer to thumb code must have the LSB set, so adjust it.
615 if (ctx
.config
.machine
== ARMNT
&& !e
.data
)
617 if (e
.forwardChunk
) {
618 write32le(p
, e
.forwardChunk
->getRVA() | bit
);
620 assert(cast
<Defined
>(e
.sym
)->getRVA() != 0 &&
621 "Exported symbol unmapped");
622 write32le(p
, cast
<Defined
>(e
.sym
)->getRVA() | bit
);
630 const COFFLinkerContext
&ctx
;
633 class NamePointersChunk
: public NonSectionChunk
{
635 explicit NamePointersChunk(std::vector
<Chunk
*> &v
) : chunks(v
) {}
636 size_t getSize() const override
{ return chunks
.size() * 4; }
638 void writeTo(uint8_t *buf
) const override
{
639 for (Chunk
*c
: chunks
) {
640 write32le(buf
, c
->getRVA());
646 std::vector
<Chunk
*> chunks
;
649 class ExportOrdinalChunk
: public NonSectionChunk
{
651 explicit ExportOrdinalChunk(const COFFLinkerContext
&ctx
, size_t baseOrdinal
,
653 : baseOrdinal(baseOrdinal
), size(tableSize
), ctx(ctx
) {}
654 size_t getSize() const override
{ return size
* 2; }
656 void writeTo(uint8_t *buf
) const override
{
657 for (const Export
&e
: ctx
.config
.exports
) {
660 assert(e
.ordinal
>= baseOrdinal
&& "Export symbol has invalid ordinal");
661 // This table stores unbiased indices, so subtract OrdinalBase.
662 write16le(buf
, e
.ordinal
- baseOrdinal
);
670 const COFFLinkerContext
&ctx
;
673 } // anonymous namespace
675 void IdataContents::create(COFFLinkerContext
&ctx
) {
676 std::vector
<std::vector
<DefinedImportData
*>> v
= binImports(ctx
, imports
);
678 // Create .idata contents for each DLL.
679 for (std::vector
<DefinedImportData
*> &syms
: v
) {
680 // Create lookup and address tables. If they have external names,
681 // we need to create hintName chunks to store the names.
682 // If they don't (if they are import-by-ordinals), we store only
683 // ordinal values to the table.
684 size_t base
= lookups
.size();
685 for (DefinedImportData
*s
: syms
) {
686 uint16_t ord
= s
->getOrdinal();
687 if (s
->getExternalName().empty()) {
688 lookups
.push_back(make
<OrdinalOnlyChunk
>(ctx
, ord
));
689 addresses
.push_back(make
<OrdinalOnlyChunk
>(ctx
, ord
));
692 auto *c
= make
<HintNameChunk
>(s
->getExternalName(), ord
);
693 lookups
.push_back(make
<LookupChunk
>(ctx
, c
));
694 addresses
.push_back(make
<LookupChunk
>(ctx
, c
));
697 // Terminate with null values.
698 lookups
.push_back(make
<NullChunk
>(ctx
.config
.wordsize
));
699 addresses
.push_back(make
<NullChunk
>(ctx
.config
.wordsize
));
701 for (int i
= 0, e
= syms
.size(); i
< e
; ++i
)
702 syms
[i
]->setLocation(addresses
[base
+ i
]);
704 // Create the import table header.
705 dllNames
.push_back(make
<StringChunk
>(syms
[0]->getDLLName()));
706 auto *dir
= make
<ImportDirectoryChunk
>(dllNames
.back());
707 dir
->lookupTab
= lookups
[base
];
708 dir
->addressTab
= addresses
[base
];
711 // Add null terminator.
712 dirs
.push_back(make
<NullChunk
>(sizeof(ImportDirectoryTableEntry
)));
715 std::vector
<Chunk
*> DelayLoadContents::getChunks() {
716 std::vector
<Chunk
*> v
;
717 v
.insert(v
.end(), dirs
.begin(), dirs
.end());
718 v
.insert(v
.end(), names
.begin(), names
.end());
719 v
.insert(v
.end(), hintNames
.begin(), hintNames
.end());
720 v
.insert(v
.end(), dllNames
.begin(), dllNames
.end());
724 std::vector
<Chunk
*> DelayLoadContents::getDataChunks() {
725 std::vector
<Chunk
*> v
;
726 v
.insert(v
.end(), moduleHandles
.begin(), moduleHandles
.end());
727 v
.insert(v
.end(), addresses
.begin(), addresses
.end());
731 uint64_t DelayLoadContents::getDirSize() {
732 return dirs
.size() * sizeof(delay_import_directory_table_entry
);
735 void DelayLoadContents::create(Defined
*h
) {
737 std::vector
<std::vector
<DefinedImportData
*>> v
= binImports(ctx
, imports
);
739 Chunk
*unwind
= newTailMergeUnwindInfoChunk();
741 // Create .didat contents for each DLL.
742 for (std::vector
<DefinedImportData
*> &syms
: v
) {
743 // Create the delay import table header.
744 dllNames
.push_back(make
<StringChunk
>(syms
[0]->getDLLName()));
745 auto *dir
= make
<DelayDirectoryChunk
>(dllNames
.back());
747 size_t base
= addresses
.size();
748 Chunk
*tm
= newTailMergeChunk(dir
);
749 Chunk
*pdataChunk
= unwind
? newTailMergePDataChunk(tm
, unwind
) : nullptr;
750 for (DefinedImportData
*s
: syms
) {
751 Chunk
*t
= newThunkChunk(s
, tm
);
752 auto *a
= make
<DelayAddressChunk
>(ctx
, t
);
753 addresses
.push_back(a
);
755 StringRef extName
= s
->getExternalName();
756 if (extName
.empty()) {
757 names
.push_back(make
<OrdinalOnlyChunk
>(ctx
, s
->getOrdinal()));
759 auto *c
= make
<HintNameChunk
>(extName
, 0);
760 names
.push_back(make
<LookupChunk
>(ctx
, c
));
761 hintNames
.push_back(c
);
762 // Add a synthetic symbol for this load thunk, using the "__imp___load"
763 // prefix, in case this thunk needs to be added to the list of valid
764 // call targets for Control Flow Guard.
765 StringRef symName
= saver().save("__imp___load_" + extName
);
767 cast
<DefinedSynthetic
>(ctx
.symtab
.addSynthetic(symName
, t
));
770 thunks
.push_back(tm
);
772 pdata
.push_back(pdataChunk
);
774 saver().save("__tailMerge_" + syms
[0]->getDLLName().lower());
775 ctx
.symtab
.addSynthetic(tmName
, tm
);
776 // Terminate with null values.
777 addresses
.push_back(make
<NullChunk
>(8));
778 names
.push_back(make
<NullChunk
>(8));
780 for (int i
= 0, e
= syms
.size(); i
< e
; ++i
)
781 syms
[i
]->setLocation(addresses
[base
+ i
]);
782 auto *mh
= make
<NullChunk
>(8);
784 moduleHandles
.push_back(mh
);
786 // Fill the delay import table header fields.
787 dir
->moduleHandle
= mh
;
788 dir
->addressTab
= addresses
[base
];
789 dir
->nameTab
= names
[base
];
794 unwindinfo
.push_back(unwind
);
795 // Add null terminator.
796 dirs
.push_back(make
<NullChunk
>(sizeof(delay_import_directory_table_entry
)));
799 Chunk
*DelayLoadContents::newTailMergeChunk(Chunk
*dir
) {
800 switch (ctx
.config
.machine
) {
802 return make
<TailMergeChunkX64
>(dir
, helper
);
804 return make
<TailMergeChunkX86
>(ctx
, dir
, helper
);
806 return make
<TailMergeChunkARM
>(ctx
, dir
, helper
);
808 return make
<TailMergeChunkARM64
>(dir
, helper
);
810 llvm_unreachable("unsupported machine type");
814 Chunk
*DelayLoadContents::newTailMergeUnwindInfoChunk() {
815 switch (ctx
.config
.machine
) {
817 return make
<TailMergeUnwindInfoX64
>();
818 // FIXME: Add support for other architectures.
820 return nullptr; // Just don't generate unwind info.
823 Chunk
*DelayLoadContents::newTailMergePDataChunk(Chunk
*tm
, Chunk
*unwind
) {
824 switch (ctx
.config
.machine
) {
826 return make
<TailMergePDataChunkX64
>(tm
, unwind
);
827 // FIXME: Add support for other architectures.
829 return nullptr; // Just don't generate unwind info.
833 Chunk
*DelayLoadContents::newThunkChunk(DefinedImportData
*s
,
835 switch (ctx
.config
.machine
) {
837 return make
<ThunkChunkX64
>(s
, tailMerge
);
839 return make
<ThunkChunkX86
>(ctx
, s
, tailMerge
);
841 return make
<ThunkChunkARM
>(ctx
, s
, tailMerge
);
843 return make
<ThunkChunkARM64
>(s
, tailMerge
);
845 llvm_unreachable("unsupported machine type");
849 EdataContents::EdataContents(COFFLinkerContext
&ctx
) : ctx(ctx
) {
850 unsigned baseOrdinal
= 1 << 16, maxOrdinal
= 0;
851 for (Export
&e
: ctx
.config
.exports
) {
852 baseOrdinal
= std::min(baseOrdinal
, (unsigned)e
.ordinal
);
853 maxOrdinal
= std::max(maxOrdinal
, (unsigned)e
.ordinal
);
855 // Ordinals must start at 1 as suggested in:
856 // https://learn.microsoft.com/en-us/cpp/build/reference/export-exports-a-function?view=msvc-170
857 assert(baseOrdinal
>= 1);
859 auto *dllName
= make
<StringChunk
>(sys::path::filename(ctx
.config
.outputFile
));
860 auto *addressTab
= make
<AddressTableChunk
>(ctx
, baseOrdinal
, maxOrdinal
);
861 std::vector
<Chunk
*> names
;
862 for (Export
&e
: ctx
.config
.exports
)
864 names
.push_back(make
<StringChunk
>(e
.exportName
));
866 std::vector
<Chunk
*> forwards
;
867 for (Export
&e
: ctx
.config
.exports
) {
868 if (e
.forwardTo
.empty())
870 e
.forwardChunk
= make
<StringChunk
>(e
.forwardTo
);
871 forwards
.push_back(e
.forwardChunk
);
874 auto *nameTab
= make
<NamePointersChunk
>(names
);
875 auto *ordinalTab
= make
<ExportOrdinalChunk
>(ctx
, baseOrdinal
, names
.size());
877 make
<ExportDirectoryChunk
>(baseOrdinal
, maxOrdinal
, names
.size(), dllName
,
878 addressTab
, nameTab
, ordinalTab
);
879 chunks
.push_back(dir
);
880 chunks
.push_back(dllName
);
881 chunks
.push_back(addressTab
);
882 chunks
.push_back(nameTab
);
883 chunks
.push_back(ordinalTab
);
884 chunks
.insert(chunks
.end(), names
.begin(), names
.end());
885 chunks
.insert(chunks
.end(), forwards
.begin(), forwards
.end());
888 } // namespace lld::coff