blob: 55fd2b50d06e5f4f6cd553b4104837168c8d0c3d [file] [log] [blame]
Rui Ueyama4b22fa72015-06-07 01:15:04 +00001//===- DLL.cpp ------------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Rui Ueyamaa77336b2015-06-21 22:31:52 +000010// This file defines various types of chunks for the DLL import or export
11// descriptor tables. They are inherently Windows-specific.
Rui Ueyama4b22fa72015-06-07 01:15:04 +000012// You need to read Microsoft PE/COFF spec to understand details
13// about the data structures.
14//
15// If you are not particularly interested in linking against Windows
16// DLL, you can skip this file, and you should still be able to
17// understand the rest of the linker.
18//
19//===----------------------------------------------------------------------===//
20
21#include "Chunks.h"
22#include "DLL.h"
Rui Ueyama4b22fa72015-06-07 01:15:04 +000023#include "llvm/Object/COFF.h"
24#include "llvm/Support/Endian.h"
Rui Ueyama97dff9e2015-06-17 00:16:33 +000025#include "llvm/Support/Path.h"
Rui Ueyama4b22fa72015-06-07 01:15:04 +000026
27using namespace llvm;
28using namespace llvm::object;
29using namespace llvm::support::endian;
30using namespace llvm::COFF;
Rui Ueyama4b22fa72015-06-07 01:15:04 +000031
Rui Ueyama4b22fa72015-06-07 01:15:04 +000032namespace lld {
33namespace coff {
Rui Ueyama857b3032015-08-10 23:02:57 +000034namespace {
Rui Ueyama4b22fa72015-06-07 01:15:04 +000035
Rui Ueyama97dff9e2015-06-17 00:16:33 +000036// Import table
37
Rui Ueyama25522f52015-07-09 00:45:50 +000038static int ptrSize() { return Config->is64() ? 8 : 4; }
39
Rui Ueyama4b22fa72015-06-07 01:15:04 +000040// A chunk for the import descriptor table.
41class HintNameChunk : public Chunk {
42public:
43 HintNameChunk(StringRef N, uint16_t H) : Name(N), Hint(H) {}
44
45 size_t getSize() const override {
46 // Starts with 2 byte Hint field, followed by a null-terminated string,
47 // ends with 0 or 1 byte padding.
Rui Ueyama489a8062016-01-14 20:53:50 +000048 return alignTo(Name.size() + 3, 2);
Rui Ueyama4b22fa72015-06-07 01:15:04 +000049 }
50
Rui Ueyama63bbe842015-09-19 23:28:57 +000051 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +000052 write16le(Buf + OutputSectionOff, Hint);
53 memcpy(Buf + OutputSectionOff + 2, Name.data(), Name.size());
Rui Ueyama4b22fa72015-06-07 01:15:04 +000054 }
55
56private:
57 StringRef Name;
58 uint16_t Hint;
59};
60
61// A chunk for the import descriptor table.
62class LookupChunk : public Chunk {
63public:
Martin Storsjob3c97b92017-07-20 05:49:54 +000064 explicit LookupChunk(Chunk *C) : HintName(C) { Align = ptrSize(); }
Rui Ueyama25522f52015-07-09 00:45:50 +000065 size_t getSize() const override { return ptrSize(); }
Rui Ueyama4b22fa72015-06-07 01:15:04 +000066
Rui Ueyama63bbe842015-09-19 23:28:57 +000067 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +000068 write32le(Buf + OutputSectionOff, HintName->getRVA());
Rui Ueyama4b22fa72015-06-07 01:15:04 +000069 }
70
71 Chunk *HintName;
72};
73
74// A chunk for the import descriptor table.
75// This chunk represent import-by-ordinal symbols.
76// See Microsoft PE/COFF spec 7.1. Import Header for details.
77class OrdinalOnlyChunk : public Chunk {
78public:
Martin Storsjob3c97b92017-07-20 05:49:54 +000079 explicit OrdinalOnlyChunk(uint16_t V) : Ordinal(V) { Align = ptrSize(); }
Rui Ueyama25522f52015-07-09 00:45:50 +000080 size_t getSize() const override { return ptrSize(); }
Rui Ueyama4b22fa72015-06-07 01:15:04 +000081
Rui Ueyama63bbe842015-09-19 23:28:57 +000082 void writeTo(uint8_t *Buf) const override {
Rui Ueyama4b22fa72015-06-07 01:15:04 +000083 // An import-by-ordinal slot has MSB 1 to indicate that
84 // this is import-by-ordinal (and not import-by-name).
Rui Ueyama25522f52015-07-09 00:45:50 +000085 if (Config->is64()) {
Rafael Espindola5c546a12015-08-14 03:30:59 +000086 write64le(Buf + OutputSectionOff, (1ULL << 63) | Ordinal);
Rui Ueyama25522f52015-07-09 00:45:50 +000087 } else {
Rafael Espindola5c546a12015-08-14 03:30:59 +000088 write32le(Buf + OutputSectionOff, (1ULL << 31) | Ordinal);
Rui Ueyama25522f52015-07-09 00:45:50 +000089 }
Rui Ueyama4b22fa72015-06-07 01:15:04 +000090 }
91
92 uint16_t Ordinal;
93};
94
95// A chunk for the import descriptor table.
Rui Ueyama97dff9e2015-06-17 00:16:33 +000096class ImportDirectoryChunk : public Chunk {
Rui Ueyama4b22fa72015-06-07 01:15:04 +000097public:
Rui Ueyama97dff9e2015-06-17 00:16:33 +000098 explicit ImportDirectoryChunk(Chunk *N) : DLLName(N) {}
99 size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
Rui Ueyama4b22fa72015-06-07 01:15:04 +0000100
Rui Ueyama63bbe842015-09-19 23:28:57 +0000101 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000102 auto *E = (coff_import_directory_table_entry *)(Buf + OutputSectionOff);
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000103 E->ImportLookupTableRVA = LookupTab->getRVA();
Rui Ueyama4b22fa72015-06-07 01:15:04 +0000104 E->NameRVA = DLLName->getRVA();
105 E->ImportAddressTableRVA = AddressTab->getRVA();
106 }
107
108 Chunk *DLLName;
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000109 Chunk *LookupTab;
Rui Ueyama4b22fa72015-06-07 01:15:04 +0000110 Chunk *AddressTab;
111};
112
113// A chunk representing null terminator in the import table.
114// Contents of this chunk is always null bytes.
115class NullChunk : public Chunk {
116public:
117 explicit NullChunk(size_t N) : Size(N) {}
118 bool hasData() const override { return false; }
119 size_t getSize() const override { return Size; }
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000120 void setAlign(size_t N) { Align = N; }
Rui Ueyama4b22fa72015-06-07 01:15:04 +0000121
122private:
123 size_t Size;
124};
125
Rui Ueyamad1570882015-08-17 08:30:31 +0000126static std::vector<std::vector<DefinedImportData *>>
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000127binImports(const std::vector<DefinedImportData *> &Imports) {
Rui Ueyama4b22fa72015-06-07 01:15:04 +0000128 // Group DLL-imported symbols by DLL name because that's how
129 // symbols are layed out in the import descriptor table.
Rui Ueyamabfbd2772015-09-02 07:27:31 +0000130 auto Less = [](const std::string &A, const std::string &B) {
Rui Ueyamad1570882015-08-17 08:30:31 +0000131 return Config->DLLOrder[A] < Config->DLLOrder[B];
132 };
Rui Ueyamabfbd2772015-09-02 07:27:31 +0000133 std::map<std::string, std::vector<DefinedImportData *>,
134 bool(*)(const std::string &, const std::string &)> M(Less);
Rui Ueyama4b22fa72015-06-07 01:15:04 +0000135 for (DefinedImportData *Sym : Imports)
Rui Ueyamabfbd2772015-09-02 07:27:31 +0000136 M[Sym->getDLLName().lower()].push_back(Sym);
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000137
Rui Ueyamad1570882015-08-17 08:30:31 +0000138 std::vector<std::vector<DefinedImportData *>> V;
Rui Ueyama01f93332017-05-18 17:03:49 +0000139 for (auto &KV : M) {
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000140 // Sort symbols by name for each group.
Rui Ueyama01f93332017-05-18 17:03:49 +0000141 std::vector<DefinedImportData *> &Syms = KV.second;
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000142 std::sort(Syms.begin(), Syms.end(),
143 [](DefinedImportData *A, DefinedImportData *B) {
144 return A->getName() < B->getName();
145 });
Rui Ueyamad1570882015-08-17 08:30:31 +0000146 V.push_back(std::move(Syms));
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000147 }
Rui Ueyamad1570882015-08-17 08:30:31 +0000148 return V;
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000149}
150
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000151// Export table
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000152// See Microsoft PE/COFF spec 4.3 for details.
153
154// A chunk for the delay import descriptor table etnry.
155class DelayDirectoryChunk : public Chunk {
156public:
157 explicit DelayDirectoryChunk(Chunk *N) : DLLName(N) {}
158
159 size_t getSize() const override {
160 return sizeof(delay_import_directory_table_entry);
161 }
162
Rui Ueyama63bbe842015-09-19 23:28:57 +0000163 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000164 auto *E = (delay_import_directory_table_entry *)(Buf + OutputSectionOff);
Rui Ueyama382dc962015-06-26 21:40:15 +0000165 E->Attributes = 1;
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000166 E->Name = DLLName->getRVA();
167 E->ModuleHandle = ModuleHandle->getRVA();
168 E->DelayImportAddressTable = AddressTab->getRVA();
169 E->DelayImportNameTable = NameTab->getRVA();
170 }
171
172 Chunk *DLLName;
173 Chunk *ModuleHandle;
174 Chunk *AddressTab;
175 Chunk *NameTab;
176};
177
178// Initial contents for delay-loaded functions.
Rui Ueyama61096202015-06-22 17:26:27 +0000179// This code calls __delayLoadHelper2 function to resolve a symbol
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000180// and then overwrites its jump table slot with the result
181// for subsequent function calls.
Rui Ueyamaef0e6472015-07-15 22:26:57 +0000182static const uint8_t ThunkX64[] = {
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000183 0x51, // push rcx
184 0x52, // push rdx
185 0x41, 0x50, // push r8
186 0x41, 0x51, // push r9
187 0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h
188 0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0
189 0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1
190 0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2
191 0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3
192 0x48, 0x8D, 0x15, 0, 0, 0, 0, // lea rdx, [__imp_<FUNCNAME>]
193 0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...]
194 0xE8, 0, 0, 0, 0, // call __delayLoadHelper2
195 0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp]
196 0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h]
197 0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h]
198 0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h]
199 0x48, 0x83, 0xC4, 0x48, // add rsp, 48h
200 0x41, 0x59, // pop r9
201 0x41, 0x58, // pop r8
202 0x5A, // pop rdx
203 0x59, // pop rcx
204 0xFF, 0xE0, // jmp rax
205};
206
Rui Ueyamaef0e6472015-07-15 22:26:57 +0000207static const uint8_t ThunkX86[] = {
208 0x51, // push ecx
209 0x52, // push edx
210 0x68, 0, 0, 0, 0, // push offset ___imp__<FUNCNAME>
211 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
212 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8
213 0x5A, // pop edx
214 0x59, // pop ecx
215 0xFF, 0xE0, // jmp eax
216};
217
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000218// A chunk for the delay import thunk.
Rui Ueyamaef0e6472015-07-15 22:26:57 +0000219class ThunkChunkX64 : public Chunk {
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000220public:
Rui Ueyamaef0e6472015-07-15 22:26:57 +0000221 ThunkChunkX64(Defined *I, Chunk *D, Defined *H)
222 : Imp(I), Desc(D), Helper(H) {}
223
224 size_t getSize() const override { return sizeof(ThunkX64); }
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000225
Rui Ueyama63bbe842015-09-19 23:28:57 +0000226 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000227 memcpy(Buf + OutputSectionOff, ThunkX64, sizeof(ThunkX64));
228 write32le(Buf + OutputSectionOff + 36, Imp->getRVA() - RVA - 40);
229 write32le(Buf + OutputSectionOff + 43, Desc->getRVA() - RVA - 47);
230 write32le(Buf + OutputSectionOff + 48, Helper->getRVA() - RVA - 52);
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000231 }
232
233 Defined *Imp = nullptr;
234 Chunk *Desc = nullptr;
235 Defined *Helper = nullptr;
236};
237
Rui Ueyamaef0e6472015-07-15 22:26:57 +0000238class ThunkChunkX86 : public Chunk {
239public:
240 ThunkChunkX86(Defined *I, Chunk *D, Defined *H)
241 : Imp(I), Desc(D), Helper(H) {}
242
243 size_t getSize() const override { return sizeof(ThunkX86); }
244
Rui Ueyama63bbe842015-09-19 23:28:57 +0000245 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000246 memcpy(Buf + OutputSectionOff, ThunkX86, sizeof(ThunkX86));
247 write32le(Buf + OutputSectionOff + 3, Imp->getRVA() + Config->ImageBase);
248 write32le(Buf + OutputSectionOff + 8, Desc->getRVA() + Config->ImageBase);
249 write32le(Buf + OutputSectionOff + 13, Helper->getRVA() - RVA - 17);
Rui Ueyamaef0e6472015-07-15 22:26:57 +0000250 }
251
Rui Ueyama3afd5bf2015-07-25 01:44:32 +0000252 void getBaserels(std::vector<Baserel> *Res) override {
253 Res->emplace_back(RVA + 3);
254 Res->emplace_back(RVA + 8);
Rui Ueyamaef0e6472015-07-15 22:26:57 +0000255 }
256
257 Defined *Imp = nullptr;
258 Chunk *Desc = nullptr;
259 Defined *Helper = nullptr;
260};
261
Rui Ueyama382dc962015-06-26 21:40:15 +0000262// A chunk for the import descriptor table.
263class DelayAddressChunk : public Chunk {
264public:
Martin Storsjob3c97b92017-07-20 05:49:54 +0000265 explicit DelayAddressChunk(Chunk *C) : Thunk(C) { Align = ptrSize(); }
Rui Ueyama7e387a62015-07-28 02:54:18 +0000266 size_t getSize() const override { return ptrSize(); }
Rui Ueyama382dc962015-06-26 21:40:15 +0000267
Rui Ueyama63bbe842015-09-19 23:28:57 +0000268 void writeTo(uint8_t *Buf) const override {
Rui Ueyama7e387a62015-07-28 02:54:18 +0000269 if (Config->is64()) {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000270 write64le(Buf + OutputSectionOff, Thunk->getRVA() + Config->ImageBase);
Rui Ueyama7e387a62015-07-28 02:54:18 +0000271 } else {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000272 write32le(Buf + OutputSectionOff, Thunk->getRVA() + Config->ImageBase);
Rui Ueyama7e387a62015-07-28 02:54:18 +0000273 }
Rui Ueyama382dc962015-06-26 21:40:15 +0000274 }
275
Rui Ueyama3afd5bf2015-07-25 01:44:32 +0000276 void getBaserels(std::vector<Baserel> *Res) override {
277 Res->emplace_back(RVA);
Rui Ueyama810551a2015-06-26 22:05:32 +0000278 }
279
Rui Ueyama382dc962015-06-26 21:40:15 +0000280 Chunk *Thunk;
281};
282
Rui Ueyamaa77336b2015-06-21 22:31:52 +0000283// Export table
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000284// Read Microsoft PE/COFF spec 5.3 for details.
285
286// A chunk for the export descriptor table.
287class ExportDirectoryChunk : public Chunk {
288public:
289 ExportDirectoryChunk(int I, int J, Chunk *D, Chunk *A, Chunk *N, Chunk *O)
290 : MaxOrdinal(I), NameTabSize(J), DLLName(D), AddressTab(A), NameTab(N),
291 OrdinalTab(O) {}
292
293 size_t getSize() const override {
294 return sizeof(export_directory_table_entry);
295 }
296
Rui Ueyama63bbe842015-09-19 23:28:57 +0000297 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000298 auto *E = (export_directory_table_entry *)(Buf + OutputSectionOff);
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000299 E->NameRVA = DLLName->getRVA();
300 E->OrdinalBase = 0;
301 E->AddressTableEntries = MaxOrdinal + 1;
302 E->NumberOfNamePointers = NameTabSize;
303 E->ExportAddressTableRVA = AddressTab->getRVA();
304 E->NamePointerRVA = NameTab->getRVA();
305 E->OrdinalTableRVA = OrdinalTab->getRVA();
306 }
307
308 uint16_t MaxOrdinal;
309 uint16_t NameTabSize;
310 Chunk *DLLName;
311 Chunk *AddressTab;
312 Chunk *NameTab;
313 Chunk *OrdinalTab;
314};
315
316class AddressTableChunk : public Chunk {
317public:
318 explicit AddressTableChunk(size_t MaxOrdinal) : Size(MaxOrdinal + 1) {}
319 size_t getSize() const override { return Size * 4; }
320
Rui Ueyama63bbe842015-09-19 23:28:57 +0000321 void writeTo(uint8_t *Buf) const override {
Martin Storsjo405b5bc2017-07-25 06:10:44 +0000322 uint32_t Bit = 0;
323 // Pointer to thumb code must have the LSB set, so adjust it.
324 if (Config->Machine == ARMNT)
325 Bit = 1;
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000326 for (Export &E : Config->Exports) {
Rui Ueyama84425d72016-01-09 01:22:00 +0000327 uint8_t *P = Buf + OutputSectionOff + E.Ordinal * 4;
328 if (E.ForwardChunk) {
Martin Storsjo405b5bc2017-07-25 06:10:44 +0000329 write32le(P, E.ForwardChunk->getRVA() | Bit);
Rui Ueyama84425d72016-01-09 01:22:00 +0000330 } else {
Martin Storsjo405b5bc2017-07-25 06:10:44 +0000331 write32le(P, cast<Defined>(E.Sym)->getRVA() | Bit);
Rui Ueyama84425d72016-01-09 01:22:00 +0000332 }
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000333 }
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000334 }
335
336private:
337 size_t Size;
338};
339
340class NamePointersChunk : public Chunk {
341public:
342 explicit NamePointersChunk(std::vector<Chunk *> &V) : Chunks(V) {}
343 size_t getSize() const override { return Chunks.size() * 4; }
344
Rui Ueyama63bbe842015-09-19 23:28:57 +0000345 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000346 uint8_t *P = Buf + OutputSectionOff;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000347 for (Chunk *C : Chunks) {
348 write32le(P, C->getRVA());
349 P += 4;
350 }
351 }
352
353private:
354 std::vector<Chunk *> Chunks;
355};
356
357class ExportOrdinalChunk : public Chunk {
358public:
359 explicit ExportOrdinalChunk(size_t I) : Size(I) {}
360 size_t getSize() const override { return Size * 2; }
361
Rui Ueyama63bbe842015-09-19 23:28:57 +0000362 void writeTo(uint8_t *Buf) const override {
Rafael Espindola5c546a12015-08-14 03:30:59 +0000363 uint8_t *P = Buf + OutputSectionOff;
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000364 for (Export &E : Config->Exports) {
365 if (E.Noname)
366 continue;
367 write16le(P, E.Ordinal);
368 P += 2;
369 }
370 }
371
372private:
373 size_t Size;
374};
375
Rui Ueyama857b3032015-08-10 23:02:57 +0000376} // anonymous namespace
377
378uint64_t IdataContents::getDirSize() {
379 return Dirs.size() * sizeof(ImportDirectoryTableEntry);
380}
381
382uint64_t IdataContents::getIATSize() {
383 return Addresses.size() * ptrSize();
384}
385
386// Returns a list of .idata contents.
387// See Microsoft PE/COFF spec 5.4 for details.
388std::vector<Chunk *> IdataContents::getChunks() {
389 create();
Rui Ueyama01f93332017-05-18 17:03:49 +0000390
Rui Ueyama857b3032015-08-10 23:02:57 +0000391 // The loader assumes a specific order of data.
392 // Add each type in the correct order.
Rui Ueyama01f93332017-05-18 17:03:49 +0000393 std::vector<Chunk *> V;
394 V.insert(V.end(), Dirs.begin(), Dirs.end());
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000395 V.insert(V.end(), Lookups.begin(), Lookups.end());
Rui Ueyama01f93332017-05-18 17:03:49 +0000396 V.insert(V.end(), Addresses.begin(), Addresses.end());
397 V.insert(V.end(), Hints.begin(), Hints.end());
Peter Collingbourne03638d02017-05-18 18:53:39 +0000398 V.insert(V.end(), DLLNames.begin(), DLLNames.end());
Rui Ueyama857b3032015-08-10 23:02:57 +0000399 return V;
400}
401
402void IdataContents::create() {
Rui Ueyamad1570882015-08-17 08:30:31 +0000403 std::vector<std::vector<DefinedImportData *>> V = binImports(Imports);
Rui Ueyama857b3032015-08-10 23:02:57 +0000404
405 // Create .idata contents for each DLL.
Rui Ueyamad1570882015-08-17 08:30:31 +0000406 for (std::vector<DefinedImportData *> &Syms : V) {
Rui Ueyama857b3032015-08-10 23:02:57 +0000407 // Create lookup and address tables. If they have external names,
408 // we need to create HintName chunks to store the names.
409 // If they don't (if they are import-by-ordinals), we store only
410 // ordinal values to the table.
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000411 size_t Base = Lookups.size();
Rui Ueyama857b3032015-08-10 23:02:57 +0000412 for (DefinedImportData *S : Syms) {
413 uint16_t Ord = S->getOrdinal();
414 if (S->getExternalName().empty()) {
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000415 Lookups.push_back(make<OrdinalOnlyChunk>(Ord));
Rui Ueyama01f93332017-05-18 17:03:49 +0000416 Addresses.push_back(make<OrdinalOnlyChunk>(Ord));
Rui Ueyama857b3032015-08-10 23:02:57 +0000417 continue;
418 }
Rui Ueyama01f93332017-05-18 17:03:49 +0000419 auto *C = make<HintNameChunk>(S->getExternalName(), Ord);
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000420 Lookups.push_back(make<LookupChunk>(C));
Rui Ueyama01f93332017-05-18 17:03:49 +0000421 Addresses.push_back(make<LookupChunk>(C));
422 Hints.push_back(C);
Rui Ueyama857b3032015-08-10 23:02:57 +0000423 }
424 // Terminate with null values.
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000425 Lookups.push_back(make<NullChunk>(ptrSize()));
Rui Ueyama01f93332017-05-18 17:03:49 +0000426 Addresses.push_back(make<NullChunk>(ptrSize()));
Rui Ueyama857b3032015-08-10 23:02:57 +0000427
428 for (int I = 0, E = Syms.size(); I < E; ++I)
Rui Ueyama01f93332017-05-18 17:03:49 +0000429 Syms[I]->setLocation(Addresses[Base + I]);
Rui Ueyama857b3032015-08-10 23:02:57 +0000430
431 // Create the import table header.
Peter Collingbourne03638d02017-05-18 18:53:39 +0000432 DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName()));
433 auto *Dir = make<ImportDirectoryChunk>(DLLNames.back());
Reid Klecknerc2eaccb02017-06-02 18:49:38 +0000434 Dir->LookupTab = Lookups[Base];
Rui Ueyama01f93332017-05-18 17:03:49 +0000435 Dir->AddressTab = Addresses[Base];
436 Dirs.push_back(Dir);
Rui Ueyama857b3032015-08-10 23:02:57 +0000437 }
438 // Add null terminator.
Rui Ueyama01f93332017-05-18 17:03:49 +0000439 Dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
Rui Ueyama857b3032015-08-10 23:02:57 +0000440}
441
442std::vector<Chunk *> DelayLoadContents::getChunks() {
443 std::vector<Chunk *> V;
Rui Ueyama01f93332017-05-18 17:03:49 +0000444 V.insert(V.end(), Dirs.begin(), Dirs.end());
445 V.insert(V.end(), Names.begin(), Names.end());
446 V.insert(V.end(), HintNames.begin(), HintNames.end());
Peter Collingbourne03638d02017-05-18 18:53:39 +0000447 V.insert(V.end(), DLLNames.begin(), DLLNames.end());
Rui Ueyama857b3032015-08-10 23:02:57 +0000448 return V;
449}
450
451std::vector<Chunk *> DelayLoadContents::getDataChunks() {
452 std::vector<Chunk *> V;
Rui Ueyama01f93332017-05-18 17:03:49 +0000453 V.insert(V.end(), ModuleHandles.begin(), ModuleHandles.end());
454 V.insert(V.end(), Addresses.begin(), Addresses.end());
Rui Ueyama857b3032015-08-10 23:02:57 +0000455 return V;
456}
457
458uint64_t DelayLoadContents::getDirSize() {
459 return Dirs.size() * sizeof(delay_import_directory_table_entry);
460}
461
462void DelayLoadContents::create(Defined *H) {
463 Helper = H;
Rui Ueyamad1570882015-08-17 08:30:31 +0000464 std::vector<std::vector<DefinedImportData *>> V = binImports(Imports);
Rui Ueyama857b3032015-08-10 23:02:57 +0000465
466 // Create .didat contents for each DLL.
Rui Ueyamad1570882015-08-17 08:30:31 +0000467 for (std::vector<DefinedImportData *> &Syms : V) {
Rui Ueyama857b3032015-08-10 23:02:57 +0000468 // Create the delay import table header.
Peter Collingbourne03638d02017-05-18 18:53:39 +0000469 DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName()));
470 auto *Dir = make<DelayDirectoryChunk>(DLLNames.back());
Rui Ueyama857b3032015-08-10 23:02:57 +0000471
472 size_t Base = Addresses.size();
473 for (DefinedImportData *S : Syms) {
Rui Ueyama01f93332017-05-18 17:03:49 +0000474 Chunk *T = newThunkChunk(S, Dir);
475 auto *A = make<DelayAddressChunk>(T);
476 Addresses.push_back(A);
477 Thunks.push_back(T);
Rui Ueyama857b3032015-08-10 23:02:57 +0000478 StringRef ExtName = S->getExternalName();
479 if (ExtName.empty()) {
Rui Ueyama01f93332017-05-18 17:03:49 +0000480 Names.push_back(make<OrdinalOnlyChunk>(S->getOrdinal()));
Rui Ueyama857b3032015-08-10 23:02:57 +0000481 } else {
Rui Ueyama01f93332017-05-18 17:03:49 +0000482 auto *C = make<HintNameChunk>(ExtName, 0);
483 Names.push_back(make<LookupChunk>(C));
484 HintNames.push_back(C);
Rui Ueyama857b3032015-08-10 23:02:57 +0000485 }
486 }
487 // Terminate with null values.
Rui Ueyama01f93332017-05-18 17:03:49 +0000488 Addresses.push_back(make<NullChunk>(8));
489 Names.push_back(make<NullChunk>(8));
Rui Ueyama857b3032015-08-10 23:02:57 +0000490
491 for (int I = 0, E = Syms.size(); I < E; ++I)
Rui Ueyama01f93332017-05-18 17:03:49 +0000492 Syms[I]->setLocation(Addresses[Base + I]);
493 auto *MH = make<NullChunk>(8);
Rui Ueyama857b3032015-08-10 23:02:57 +0000494 MH->setAlign(8);
Rui Ueyama01f93332017-05-18 17:03:49 +0000495 ModuleHandles.push_back(MH);
Rui Ueyama857b3032015-08-10 23:02:57 +0000496
497 // Fill the delay import table header fields.
498 Dir->ModuleHandle = MH;
Rui Ueyama01f93332017-05-18 17:03:49 +0000499 Dir->AddressTab = Addresses[Base];
500 Dir->NameTab = Names[Base];
501 Dirs.push_back(Dir);
Rui Ueyama857b3032015-08-10 23:02:57 +0000502 }
503 // Add null terminator.
Rui Ueyama01f93332017-05-18 17:03:49 +0000504 Dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
Rui Ueyama857b3032015-08-10 23:02:57 +0000505}
506
507Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *S, Chunk *Dir) {
508 switch (Config->Machine) {
509 case AMD64:
Rui Ueyama01f93332017-05-18 17:03:49 +0000510 return make<ThunkChunkX64>(S, Dir, Helper);
Rui Ueyama857b3032015-08-10 23:02:57 +0000511 case I386:
Rui Ueyama01f93332017-05-18 17:03:49 +0000512 return make<ThunkChunkX86>(S, Dir, Helper);
Rui Ueyama857b3032015-08-10 23:02:57 +0000513 default:
514 llvm_unreachable("unsupported machine type");
515 }
516}
517
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000518EdataContents::EdataContents() {
519 uint16_t MaxOrdinal = 0;
520 for (Export &E : Config->Exports)
521 MaxOrdinal = std::max(MaxOrdinal, E.Ordinal);
522
Rui Ueyama01f93332017-05-18 17:03:49 +0000523 auto *DLLName = make<StringChunk>(sys::path::filename(Config->OutputFile));
524 auto *AddressTab = make<AddressTableChunk>(MaxOrdinal);
Rui Ueyama97dff9e2015-06-17 00:16:33 +0000525 std::vector<Chunk *> Names;
526 for (Export &E : Config->Exports)
527 if (!E.Noname)
Rui Ueyama01f93332017-05-18 17:03:49 +0000528 Names.push_back(make<StringChunk>(E.ExportName));
Rui Ueyama84425d72016-01-09 01:22:00 +0000529
530 std::vector<Chunk *> Forwards;
531 for (Export &E : Config->Exports) {
532 if (E.ForwardTo.empty())
533 continue;
Rui Ueyama01f93332017-05-18 17:03:49 +0000534 E.ForwardChunk = make<StringChunk>(E.ForwardTo);
Rui Ueyama84425d72016-01-09 01:22:00 +0000535 Forwards.push_back(E.ForwardChunk);
536 }
537
Rui Ueyama01f93332017-05-18 17:03:49 +0000538 auto *NameTab = make<NamePointersChunk>(Names);
539 auto *OrdinalTab = make<ExportOrdinalChunk>(Names.size());
540 auto *Dir = make<ExportDirectoryChunk>(MaxOrdinal, Names.size(), DLLName,
541 AddressTab, NameTab, OrdinalTab);
542 Chunks.push_back(Dir);
543 Chunks.push_back(DLLName);
544 Chunks.push_back(AddressTab);
545 Chunks.push_back(NameTab);
546 Chunks.push_back(OrdinalTab);
547 Chunks.insert(Chunks.end(), Names.begin(), Names.end());
548 Chunks.insert(Chunks.end(), Forwards.begin(), Forwards.end());
Rui Ueyama4b22fa72015-06-07 01:15:04 +0000549}
550
551} // namespace coff
552} // namespace lld