xref: /freebsd/contrib/llvm-project/lld/COFF/DLL.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 //===- DLL.cpp ------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines various types of chunks for the DLL import or export
10 // descriptor tables. They are inherently Windows-specific.
11 // You need to read Microsoft PE/COFF spec to understand details
12 // about the data structures.
13 //
14 // If you are not particularly interested in linking against Windows
15 // DLL, you can skip this file, and you should still be able to
16 // understand the rest of the linker.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "DLL.h"
21 #include "COFFLinkerContext.h"
22 #include "Chunks.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"
28 
29 using namespace llvm;
30 using namespace llvm::object;
31 using namespace llvm::support::endian;
32 using namespace llvm::COFF;
33 
34 namespace lld::coff {
35 namespace {
36 
37 // Import table
38 
39 // A chunk for the import descriptor table.
40 class HintNameChunk : public NonSectionChunk {
41 public:
42   HintNameChunk(StringRef n, uint16_t h) : name(n), hint(h) {}
43 
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);
48   }
49 
50   void writeTo(uint8_t *buf) const override {
51     memset(buf, 0, getSize());
52     write16le(buf, hint);
53     memcpy(buf + 2, name.data(), name.size());
54   }
55 
56 private:
57   StringRef name;
58   uint16_t hint;
59 };
60 
61 // A chunk for the import descriptor table.
62 class LookupChunk : public NonSectionChunk {
63 public:
64   explicit LookupChunk(COFFLinkerContext &ctx, Chunk *c)
65       : hintName(c), ctx(ctx) {
66     setAlignment(ctx.config.wordsize);
67   }
68   size_t getSize() const override { return ctx.config.wordsize; }
69 
70   void writeTo(uint8_t *buf) const override {
71     if (ctx.config.is64())
72       write64le(buf, hintName->getRVA());
73     else
74       write32le(buf, hintName->getRVA());
75   }
76 
77   Chunk *hintName;
78 
79 private:
80   COFFLinkerContext &ctx;
81 };
82 
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 {
87 public:
88   explicit OrdinalOnlyChunk(COFFLinkerContext &c, uint16_t v)
89       : ordinal(v), ctx(c) {
90     setAlignment(ctx.config.wordsize);
91   }
92   size_t getSize() const override { return ctx.config.wordsize; }
93 
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);
99     } else {
100       write32le(buf, (1ULL << 31) | ordinal);
101     }
102   }
103 
104   uint16_t ordinal;
105 
106 private:
107   COFFLinkerContext &ctx;
108 };
109 
110 // A chunk for the import descriptor table.
111 class ImportDirectoryChunk : public NonSectionChunk {
112 public:
113   explicit ImportDirectoryChunk(Chunk *n) : dllName(n) {}
114   size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
115 
116   void writeTo(uint8_t *buf) const override {
117     memset(buf, 0, getSize());
118 
119     auto *e = (coff_import_directory_table_entry *)(buf);
120     e->ImportLookupTableRVA = lookupTab->getRVA();
121     e->NameRVA = dllName->getRVA();
122     e->ImportAddressTableRVA = addressTab->getRVA();
123   }
124 
125   Chunk *dllName;
126   Chunk *lookupTab;
127   Chunk *addressTab;
128 };
129 
130 // A chunk representing null terminator in the import table.
131 // Contents of this chunk is always null bytes.
132 class NullChunk : public NonSectionChunk {
133 public:
134   explicit NullChunk(size_t n) : size(n) { hasData = false; }
135   size_t getSize() const override { return size; }
136 
137   void writeTo(uint8_t *buf) const override {
138     memset(buf, 0, size);
139   }
140 
141 private:
142   size_t size;
143 };
144 
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];
152   };
153   std::map<std::string, std::vector<DefinedImportData *>, decltype(less)> m(
154       less);
155   for (DefinedImportData *sym : imports)
156     m[sym->getDLLName().lower()].push_back(sym);
157 
158   std::vector<std::vector<DefinedImportData *>> v;
159   for (auto &kv : m) {
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();
164     });
165     v.push_back(std::move(syms));
166   }
167   return v;
168 }
169 
170 // See Microsoft PE/COFF spec 4.3 for details.
171 
172 // A chunk for the delay import descriptor table etnry.
173 class DelayDirectoryChunk : public NonSectionChunk {
174 public:
175   explicit DelayDirectoryChunk(Chunk *n) : dllName(n) {}
176 
177   size_t getSize() const override {
178     return sizeof(delay_import_directory_table_entry);
179   }
180 
181   void writeTo(uint8_t *buf) const override {
182     memset(buf, 0, getSize());
183 
184     auto *e = (delay_import_directory_table_entry *)(buf);
185     e->Attributes = 1;
186     e->Name = dllName->getRVA();
187     e->ModuleHandle = moduleHandle->getRVA();
188     e->DelayImportAddressTable = addressTab->getRVA();
189     e->DelayImportNameTable = nameTab->getRVA();
190   }
191 
192   Chunk *dllName;
193   Chunk *moduleHandle;
194   Chunk *addressTab;
195   Chunk *nameTab;
196 };
197 
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>
205 };
206 
207 static const uint8_t tailMergeX64[] = {
208     0x51,                               // push    rcx
209     0x52,                               // push    rdx
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
227     0x5A,                               // pop     rdx
228     0x59,                               // pop     rcx
229     0xFF, 0xE0,                         // jmp     rax
230 };
231 
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
243 };
244 
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>
248 };
249 
250 static const uint8_t tailMergeX86[] = {
251     0x51,              // push  ecx
252     0x52,              // push  edx
253     0x50,              // push  eax
254     0x68, 0, 0, 0, 0,  // push  offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
255     0xE8, 0, 0, 0, 0,  // call  ___delayLoadHelper2@8
256     0x5A,              // pop   edx
257     0x59,              // pop   ecx
258     0xFF, 0xE0,        // jmp   eax
259 };
260 
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>
265 };
266 
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}
278     0x60, 0x47,             // bx      ip
279 };
280 
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>
285 };
286 
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
313 };
314 
315 // A chunk for the delay import thunk.
316 class ThunkChunkX64 : public NonSectionChunk {
317 public:
318   ThunkChunkX64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
319 
320   size_t getSize() const override { return sizeof(thunkX64); }
321 
322   void writeTo(uint8_t *buf) const override {
323     memcpy(buf, thunkX64, sizeof(thunkX64));
324     write32le(buf + 3, imp->getRVA() - rva - 7);
325     write32le(buf + 8, tailMerge->getRVA() - rva - 12);
326   }
327 
328   Defined *imp = nullptr;
329   Chunk *tailMerge = nullptr;
330 };
331 
332 class TailMergeChunkX64 : public NonSectionChunk {
333 public:
334   TailMergeChunkX64(Chunk *d, Defined *h) : desc(d), helper(h) {}
335 
336   size_t getSize() const override { return sizeof(tailMergeX64); }
337 
338   void writeTo(uint8_t *buf) const override {
339     memcpy(buf, tailMergeX64, sizeof(tailMergeX64));
340     write32le(buf + 39, desc->getRVA() - rva - 43);
341     write32le(buf + 44, helper->getRVA() - rva - 48);
342   }
343 
344   Chunk *desc = nullptr;
345   Defined *helper = nullptr;
346 };
347 
348 class TailMergePDataChunkX64 : public NonSectionChunk {
349 public:
350   TailMergePDataChunkX64(Chunk *tm, Chunk *unwind) : tm(tm), unwind(unwind) {
351     // See
352     // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-runtime_function
353     setAlignment(4);
354   }
355 
356   size_t getSize() const override { return 3 * sizeof(uint32_t); }
357 
358   void writeTo(uint8_t *buf) const override {
359     write32le(buf + 0, tm->getRVA()); // TailMergeChunk start RVA
360     write32le(buf + 4, tm->getRVA() + tm->getSize()); // TailMergeChunk stop RVA
361     write32le(buf + 8, unwind->getRVA());             // UnwindInfo RVA
362   }
363 
364   Chunk *tm = nullptr;
365   Chunk *unwind = nullptr;
366 };
367 
368 class TailMergeUnwindInfoX64 : public NonSectionChunk {
369 public:
370   TailMergeUnwindInfoX64() {
371     // See
372     // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-unwind_info
373     setAlignment(4);
374   }
375 
376   size_t getSize() const override { return sizeof(tailMergeUnwindInfoX64); }
377 
378   void writeTo(uint8_t *buf) const override {
379     memcpy(buf, tailMergeUnwindInfoX64, sizeof(tailMergeUnwindInfoX64));
380   }
381 };
382 
383 class ThunkChunkX86 : public NonSectionChunk {
384 public:
385   ThunkChunkX86(COFFLinkerContext &ctx, Defined *i, Chunk *tm)
386       : imp(i), tailMerge(tm), ctx(ctx) {}
387 
388   size_t getSize() const override { return sizeof(thunkX86); }
389 
390   void writeTo(uint8_t *buf) const override {
391     memcpy(buf, thunkX86, sizeof(thunkX86));
392     write32le(buf + 1, imp->getRVA() + ctx.config.imageBase);
393     write32le(buf + 6, tailMerge->getRVA() - rva - 10);
394   }
395 
396   void getBaserels(std::vector<Baserel> *res) override {
397     res->emplace_back(rva + 1, ctx.config.machine);
398   }
399 
400   Defined *imp = nullptr;
401   Chunk *tailMerge = nullptr;
402 
403 private:
404   const COFFLinkerContext &ctx;
405 };
406 
407 class TailMergeChunkX86 : public NonSectionChunk {
408 public:
409   TailMergeChunkX86(COFFLinkerContext &ctx, Chunk *d, Defined *h)
410       : desc(d), helper(h), ctx(ctx) {}
411 
412   size_t getSize() const override { return sizeof(tailMergeX86); }
413 
414   void writeTo(uint8_t *buf) const override {
415     memcpy(buf, tailMergeX86, sizeof(tailMergeX86));
416     write32le(buf + 4, desc->getRVA() + ctx.config.imageBase);
417     write32le(buf + 9, helper->getRVA() - rva - 13);
418   }
419 
420   void getBaserels(std::vector<Baserel> *res) override {
421     res->emplace_back(rva + 4, ctx.config.machine);
422   }
423 
424   Chunk *desc = nullptr;
425   Defined *helper = nullptr;
426 
427 private:
428   const COFFLinkerContext &ctx;
429 };
430 
431 class ThunkChunkARM : public NonSectionChunk {
432 public:
433   ThunkChunkARM(COFFLinkerContext &ctx, Defined *i, Chunk *tm)
434       : imp(i), tailMerge(tm), ctx(ctx) {
435     setAlignment(2);
436   }
437 
438   size_t getSize() const override { return sizeof(thunkARM); }
439 
440   void writeTo(uint8_t *buf) const override {
441     memcpy(buf, thunkARM, sizeof(thunkARM));
442     applyMOV32T(buf + 0, imp->getRVA() + ctx.config.imageBase);
443     applyBranch24T(buf + 8, tailMerge->getRVA() - rva - 12);
444   }
445 
446   void getBaserels(std::vector<Baserel> *res) override {
447     res->emplace_back(rva + 0, IMAGE_REL_BASED_ARM_MOV32T);
448   }
449 
450   Defined *imp = nullptr;
451   Chunk *tailMerge = nullptr;
452 
453 private:
454   const COFFLinkerContext &ctx;
455 };
456 
457 class TailMergeChunkARM : public NonSectionChunk {
458 public:
459   TailMergeChunkARM(COFFLinkerContext &ctx, Chunk *d, Defined *h)
460       : desc(d), helper(h), ctx(ctx) {
461     setAlignment(2);
462   }
463 
464   size_t getSize() const override { return sizeof(tailMergeARM); }
465 
466   void writeTo(uint8_t *buf) const override {
467     memcpy(buf, tailMergeARM, sizeof(tailMergeARM));
468     applyMOV32T(buf + 14, desc->getRVA() + ctx.config.imageBase);
469     applyBranch24T(buf + 22, helper->getRVA() - rva - 26);
470   }
471 
472   void getBaserels(std::vector<Baserel> *res) override {
473     res->emplace_back(rva + 14, IMAGE_REL_BASED_ARM_MOV32T);
474   }
475 
476   Chunk *desc = nullptr;
477   Defined *helper = nullptr;
478 
479 private:
480   const COFFLinkerContext &ctx;
481 };
482 
483 class ThunkChunkARM64 : public NonSectionChunk {
484 public:
485   ThunkChunkARM64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {
486     setAlignment(4);
487   }
488 
489   size_t getSize() const override { return sizeof(thunkARM64); }
490 
491   void writeTo(uint8_t *buf) const override {
492     memcpy(buf, thunkARM64, sizeof(thunkARM64));
493     applyArm64Addr(buf + 0, imp->getRVA(), rva + 0, 12);
494     applyArm64Imm(buf + 4, imp->getRVA() & 0xfff, 0);
495     applyArm64Branch26(buf + 8, tailMerge->getRVA() - rva - 8);
496   }
497 
498   Defined *imp = nullptr;
499   Chunk *tailMerge = nullptr;
500 };
501 
502 class TailMergeChunkARM64 : public NonSectionChunk {
503 public:
504   TailMergeChunkARM64(Chunk *d, Defined *h) : desc(d), helper(h) {
505     setAlignment(4);
506   }
507 
508   size_t getSize() const override { return sizeof(tailMergeARM64); }
509 
510   void writeTo(uint8_t *buf) const override {
511     memcpy(buf, tailMergeARM64, sizeof(tailMergeARM64));
512     applyArm64Addr(buf + 44, desc->getRVA(), rva + 44, 12);
513     applyArm64Imm(buf + 48, desc->getRVA() & 0xfff, 0);
514     applyArm64Branch26(buf + 52, helper->getRVA() - rva - 52);
515   }
516 
517   Chunk *desc = nullptr;
518   Defined *helper = nullptr;
519 };
520 
521 // A chunk for the import descriptor table.
522 class DelayAddressChunk : public NonSectionChunk {
523 public:
524   explicit DelayAddressChunk(COFFLinkerContext &ctx, Chunk *c)
525       : thunk(c), ctx(ctx) {
526     setAlignment(ctx.config.wordsize);
527   }
528   size_t getSize() const override { return ctx.config.wordsize; }
529 
530   void writeTo(uint8_t *buf) const override {
531     if (ctx.config.is64()) {
532       write64le(buf, thunk->getRVA() + ctx.config.imageBase);
533     } else {
534       uint32_t bit = 0;
535       // Pointer to thumb code must have the LSB set, so adjust it.
536       if (ctx.config.machine == ARMNT)
537         bit = 1;
538       write32le(buf, (thunk->getRVA() + ctx.config.imageBase) | bit);
539     }
540   }
541 
542   void getBaserels(std::vector<Baserel> *res) override {
543     res->emplace_back(rva, ctx.config.machine);
544   }
545 
546   Chunk *thunk;
547 
548 private:
549   const COFFLinkerContext &ctx;
550 };
551 
552 // Export table
553 // Read Microsoft PE/COFF spec 5.3 for details.
554 
555 // A chunk for the export descriptor table.
556 class ExportDirectoryChunk : public NonSectionChunk {
557 public:
558   ExportDirectoryChunk(int baseOrdinal, int maxOrdinal, int nameTabSize,
559                        Chunk *d, Chunk *a, Chunk *n, Chunk *o)
560       : baseOrdinal(baseOrdinal), maxOrdinal(maxOrdinal),
561         nameTabSize(nameTabSize), dllName(d), addressTab(a), nameTab(n),
562         ordinalTab(o) {}
563 
564   size_t getSize() const override {
565     return sizeof(export_directory_table_entry);
566   }
567 
568   void writeTo(uint8_t *buf) const override {
569     memset(buf, 0, getSize());
570 
571     auto *e = (export_directory_table_entry *)(buf);
572     e->NameRVA = dllName->getRVA();
573     e->OrdinalBase = baseOrdinal;
574     e->AddressTableEntries = (maxOrdinal - baseOrdinal) + 1;
575     e->NumberOfNamePointers = nameTabSize;
576     e->ExportAddressTableRVA = addressTab->getRVA();
577     e->NamePointerRVA = nameTab->getRVA();
578     e->OrdinalTableRVA = ordinalTab->getRVA();
579   }
580 
581   uint16_t baseOrdinal;
582   uint16_t maxOrdinal;
583   uint16_t nameTabSize;
584   Chunk *dllName;
585   Chunk *addressTab;
586   Chunk *nameTab;
587   Chunk *ordinalTab;
588 };
589 
590 class AddressTableChunk : public NonSectionChunk {
591 public:
592   explicit AddressTableChunk(COFFLinkerContext &ctx, size_t baseOrdinal,
593                              size_t maxOrdinal)
594       : baseOrdinal(baseOrdinal), size((maxOrdinal - baseOrdinal) + 1),
595         ctx(ctx) {}
596   size_t getSize() const override { return size * 4; }
597 
598   void writeTo(uint8_t *buf) const override {
599     memset(buf, 0, getSize());
600 
601     for (const Export &e : ctx.config.exports) {
602       assert(e.ordinal >= baseOrdinal && "Export symbol has invalid ordinal");
603       // Subtract the OrdinalBase to get the index.
604       uint8_t *p = buf + (e.ordinal - baseOrdinal) * 4;
605       uint32_t bit = 0;
606       // Pointer to thumb code must have the LSB set, so adjust it.
607       if (ctx.config.machine == ARMNT && !e.data)
608         bit = 1;
609       if (e.forwardChunk) {
610         write32le(p, e.forwardChunk->getRVA() | bit);
611       } else {
612         assert(cast<Defined>(e.sym)->getRVA() != 0 &&
613                "Exported symbol unmapped");
614         write32le(p, cast<Defined>(e.sym)->getRVA() | bit);
615       }
616     }
617   }
618 
619 private:
620   size_t baseOrdinal;
621   size_t size;
622   const COFFLinkerContext &ctx;
623 };
624 
625 class NamePointersChunk : public NonSectionChunk {
626 public:
627   explicit NamePointersChunk(std::vector<Chunk *> &v) : chunks(v) {}
628   size_t getSize() const override { return chunks.size() * 4; }
629 
630   void writeTo(uint8_t *buf) const override {
631     for (Chunk *c : chunks) {
632       write32le(buf, c->getRVA());
633       buf += 4;
634     }
635   }
636 
637 private:
638   std::vector<Chunk *> chunks;
639 };
640 
641 class ExportOrdinalChunk : public NonSectionChunk {
642 public:
643   explicit ExportOrdinalChunk(const COFFLinkerContext &ctx, size_t baseOrdinal,
644                               size_t tableSize)
645       : baseOrdinal(baseOrdinal), size(tableSize), ctx(ctx) {}
646   size_t getSize() const override { return size * 2; }
647 
648   void writeTo(uint8_t *buf) const override {
649     for (const Export &e : ctx.config.exports) {
650       if (e.noname)
651         continue;
652       assert(e.ordinal >= baseOrdinal && "Export symbol has invalid ordinal");
653       // This table stores unbiased indices, so subtract OrdinalBase.
654       write16le(buf, e.ordinal - baseOrdinal);
655       buf += 2;
656     }
657   }
658 
659 private:
660   size_t baseOrdinal;
661   size_t size;
662   const COFFLinkerContext &ctx;
663 };
664 
665 } // anonymous namespace
666 
667 void IdataContents::create(COFFLinkerContext &ctx) {
668   std::vector<std::vector<DefinedImportData *>> v = binImports(ctx, imports);
669 
670   // Create .idata contents for each DLL.
671   for (std::vector<DefinedImportData *> &syms : v) {
672     // Create lookup and address tables. If they have external names,
673     // we need to create hintName chunks to store the names.
674     // If they don't (if they are import-by-ordinals), we store only
675     // ordinal values to the table.
676     size_t base = lookups.size();
677     for (DefinedImportData *s : syms) {
678       uint16_t ord = s->getOrdinal();
679       if (s->getExternalName().empty()) {
680         lookups.push_back(make<OrdinalOnlyChunk>(ctx, ord));
681         addresses.push_back(make<OrdinalOnlyChunk>(ctx, ord));
682         continue;
683       }
684       auto *c = make<HintNameChunk>(s->getExternalName(), ord);
685       lookups.push_back(make<LookupChunk>(ctx, c));
686       addresses.push_back(make<LookupChunk>(ctx, c));
687       hints.push_back(c);
688     }
689     // Terminate with null values.
690     lookups.push_back(make<NullChunk>(ctx.config.wordsize));
691     addresses.push_back(make<NullChunk>(ctx.config.wordsize));
692 
693     for (int i = 0, e = syms.size(); i < e; ++i)
694       syms[i]->setLocation(addresses[base + i]);
695 
696     // Create the import table header.
697     dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
698     auto *dir = make<ImportDirectoryChunk>(dllNames.back());
699     dir->lookupTab = lookups[base];
700     dir->addressTab = addresses[base];
701     dirs.push_back(dir);
702   }
703   // Add null terminator.
704   dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
705 }
706 
707 std::vector<Chunk *> DelayLoadContents::getChunks() {
708   std::vector<Chunk *> v;
709   v.insert(v.end(), dirs.begin(), dirs.end());
710   v.insert(v.end(), names.begin(), names.end());
711   v.insert(v.end(), hintNames.begin(), hintNames.end());
712   v.insert(v.end(), dllNames.begin(), dllNames.end());
713   return v;
714 }
715 
716 std::vector<Chunk *> DelayLoadContents::getDataChunks() {
717   std::vector<Chunk *> v;
718   v.insert(v.end(), moduleHandles.begin(), moduleHandles.end());
719   v.insert(v.end(), addresses.begin(), addresses.end());
720   return v;
721 }
722 
723 uint64_t DelayLoadContents::getDirSize() {
724   return dirs.size() * sizeof(delay_import_directory_table_entry);
725 }
726 
727 void DelayLoadContents::create(Defined *h) {
728   helper = h;
729   std::vector<std::vector<DefinedImportData *>> v = binImports(ctx, imports);
730 
731   Chunk *unwind = newTailMergeUnwindInfoChunk();
732 
733   // Create .didat contents for each DLL.
734   for (std::vector<DefinedImportData *> &syms : v) {
735     // Create the delay import table header.
736     dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
737     auto *dir = make<DelayDirectoryChunk>(dllNames.back());
738 
739     size_t base = addresses.size();
740     Chunk *tm = newTailMergeChunk(dir);
741     Chunk *pdataChunk = unwind ? newTailMergePDataChunk(tm, unwind) : nullptr;
742     for (DefinedImportData *s : syms) {
743       Chunk *t = newThunkChunk(s, tm);
744       auto *a = make<DelayAddressChunk>(ctx, t);
745       addresses.push_back(a);
746       thunks.push_back(t);
747       StringRef extName = s->getExternalName();
748       if (extName.empty()) {
749         names.push_back(make<OrdinalOnlyChunk>(ctx, s->getOrdinal()));
750       } else {
751         auto *c = make<HintNameChunk>(extName, 0);
752         names.push_back(make<LookupChunk>(ctx, c));
753         hintNames.push_back(c);
754         // Add a synthetic symbol for this load thunk, using the "__imp___load"
755         // prefix, in case this thunk needs to be added to the list of valid
756         // call targets for Control Flow Guard.
757         StringRef symName = saver().save("__imp___load_" + extName);
758         s->loadThunkSym =
759             cast<DefinedSynthetic>(ctx.symtab.addSynthetic(symName, t));
760       }
761     }
762     thunks.push_back(tm);
763     if (pdataChunk)
764       pdata.push_back(pdataChunk);
765     StringRef tmName =
766         saver().save("__tailMerge_" + syms[0]->getDLLName().lower());
767     ctx.symtab.addSynthetic(tmName, tm);
768     // Terminate with null values.
769     addresses.push_back(make<NullChunk>(8));
770     names.push_back(make<NullChunk>(8));
771 
772     for (int i = 0, e = syms.size(); i < e; ++i)
773       syms[i]->setLocation(addresses[base + i]);
774     auto *mh = make<NullChunk>(8);
775     mh->setAlignment(8);
776     moduleHandles.push_back(mh);
777 
778     // Fill the delay import table header fields.
779     dir->moduleHandle = mh;
780     dir->addressTab = addresses[base];
781     dir->nameTab = names[base];
782     dirs.push_back(dir);
783   }
784 
785   if (unwind)
786     unwindinfo.push_back(unwind);
787   // Add null terminator.
788   dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
789 }
790 
791 Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {
792   switch (ctx.config.machine) {
793   case AMD64:
794     return make<TailMergeChunkX64>(dir, helper);
795   case I386:
796     return make<TailMergeChunkX86>(ctx, dir, helper);
797   case ARMNT:
798     return make<TailMergeChunkARM>(ctx, dir, helper);
799   case ARM64:
800     return make<TailMergeChunkARM64>(dir, helper);
801   default:
802     llvm_unreachable("unsupported machine type");
803   }
804 }
805 
806 Chunk *DelayLoadContents::newTailMergeUnwindInfoChunk() {
807   switch (ctx.config.machine) {
808   case AMD64:
809     return make<TailMergeUnwindInfoX64>();
810     // FIXME: Add support for other architectures.
811   default:
812     return nullptr; // Just don't generate unwind info.
813   }
814 }
815 Chunk *DelayLoadContents::newTailMergePDataChunk(Chunk *tm, Chunk *unwind) {
816   switch (ctx.config.machine) {
817   case AMD64:
818     return make<TailMergePDataChunkX64>(tm, unwind);
819     // FIXME: Add support for other architectures.
820   default:
821     return nullptr; // Just don't generate unwind info.
822   }
823 }
824 
825 Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s,
826                                         Chunk *tailMerge) {
827   switch (ctx.config.machine) {
828   case AMD64:
829     return make<ThunkChunkX64>(s, tailMerge);
830   case I386:
831     return make<ThunkChunkX86>(ctx, s, tailMerge);
832   case ARMNT:
833     return make<ThunkChunkARM>(ctx, s, tailMerge);
834   case ARM64:
835     return make<ThunkChunkARM64>(s, tailMerge);
836   default:
837     llvm_unreachable("unsupported machine type");
838   }
839 }
840 
841 EdataContents::EdataContents(COFFLinkerContext &ctx) : ctx(ctx) {
842   unsigned baseOrdinal = 1 << 16, maxOrdinal = 0;
843   for (Export &e : ctx.config.exports) {
844     baseOrdinal = std::min(baseOrdinal, (unsigned)e.ordinal);
845     maxOrdinal = std::max(maxOrdinal, (unsigned)e.ordinal);
846   }
847   // Ordinals must start at 1 as suggested in:
848   // https://learn.microsoft.com/en-us/cpp/build/reference/export-exports-a-function?view=msvc-170
849   assert(baseOrdinal >= 1);
850 
851   auto *dllName = make<StringChunk>(sys::path::filename(ctx.config.outputFile));
852   auto *addressTab = make<AddressTableChunk>(ctx, baseOrdinal, maxOrdinal);
853   std::vector<Chunk *> names;
854   for (Export &e : ctx.config.exports)
855     if (!e.noname)
856       names.push_back(make<StringChunk>(e.exportName));
857 
858   std::vector<Chunk *> forwards;
859   for (Export &e : ctx.config.exports) {
860     if (e.forwardTo.empty())
861       continue;
862     e.forwardChunk = make<StringChunk>(e.forwardTo);
863     forwards.push_back(e.forwardChunk);
864   }
865 
866   auto *nameTab = make<NamePointersChunk>(names);
867   auto *ordinalTab = make<ExportOrdinalChunk>(ctx, baseOrdinal, names.size());
868   auto *dir =
869       make<ExportDirectoryChunk>(baseOrdinal, maxOrdinal, names.size(), dllName,
870                                  addressTab, nameTab, ordinalTab);
871   chunks.push_back(dir);
872   chunks.push_back(dllName);
873   chunks.push_back(addressTab);
874   chunks.push_back(nameTab);
875   chunks.push_back(ordinalTab);
876   chunks.insert(chunks.end(), names.begin(), names.end());
877   chunks.insert(chunks.end(), forwards.begin(), forwards.end());
878 }
879 
880 } // namespace lld::coff
881