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 "Chunks.h" 22 #include "llvm/Object/COFF.h" 23 #include "llvm/Support/Endian.h" 24 #include "llvm/Support/Path.h" 25 26 using namespace llvm; 27 using namespace llvm::object; 28 using namespace llvm::support::endian; 29 using namespace llvm::COFF; 30 31 namespace lld { 32 namespace coff { 33 namespace { 34 35 // Import table 36 37 // A chunk for the import descriptor table. 38 class HintNameChunk : public NonSectionChunk { 39 public: 40 HintNameChunk(StringRef N, uint16_t H) : Name(N), Hint(H) {} 41 42 size_t getSize() const override { 43 // Starts with 2 byte Hint field, followed by a null-terminated string, 44 // ends with 0 or 1 byte padding. 45 return alignTo(Name.size() + 3, 2); 46 } 47 48 void writeTo(uint8_t *Buf) const override { 49 memset(Buf, 0, getSize()); 50 write16le(Buf, Hint); 51 memcpy(Buf + 2, Name.data(), Name.size()); 52 } 53 54 private: 55 StringRef Name; 56 uint16_t Hint; 57 }; 58 59 // A chunk for the import descriptor table. 60 class LookupChunk : public NonSectionChunk { 61 public: 62 explicit LookupChunk(Chunk *C) : HintName(C) { 63 setAlignment(Config->Wordsize); 64 } 65 size_t getSize() const override { return Config->Wordsize; } 66 67 void writeTo(uint8_t *Buf) const override { 68 if (Config->is64()) 69 write64le(Buf, HintName->getRVA()); 70 else 71 write32le(Buf, HintName->getRVA()); 72 } 73 74 Chunk *HintName; 75 }; 76 77 // A chunk for the import descriptor table. 78 // This chunk represent import-by-ordinal symbols. 79 // See Microsoft PE/COFF spec 7.1. Import Header for details. 80 class OrdinalOnlyChunk : public NonSectionChunk { 81 public: 82 explicit OrdinalOnlyChunk(uint16_t V) : Ordinal(V) { 83 setAlignment(Config->Wordsize); 84 } 85 size_t getSize() const override { return Config->Wordsize; } 86 87 void writeTo(uint8_t *Buf) const override { 88 // An import-by-ordinal slot has MSB 1 to indicate that 89 // this is import-by-ordinal (and not import-by-name). 90 if (Config->is64()) { 91 write64le(Buf, (1ULL << 63) | Ordinal); 92 } else { 93 write32le(Buf, (1ULL << 31) | Ordinal); 94 } 95 } 96 97 uint16_t Ordinal; 98 }; 99 100 // A chunk for the import descriptor table. 101 class ImportDirectoryChunk : public NonSectionChunk { 102 public: 103 explicit ImportDirectoryChunk(Chunk *N) : DLLName(N) {} 104 size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); } 105 106 void writeTo(uint8_t *Buf) const override { 107 memset(Buf, 0, getSize()); 108 109 auto *E = (coff_import_directory_table_entry *)(Buf); 110 E->ImportLookupTableRVA = LookupTab->getRVA(); 111 E->NameRVA = DLLName->getRVA(); 112 E->ImportAddressTableRVA = AddressTab->getRVA(); 113 } 114 115 Chunk *DLLName; 116 Chunk *LookupTab; 117 Chunk *AddressTab; 118 }; 119 120 // A chunk representing null terminator in the import table. 121 // Contents of this chunk is always null bytes. 122 class NullChunk : public NonSectionChunk { 123 public: 124 explicit NullChunk(size_t N) : Size(N) { HasData = false; } 125 size_t getSize() const override { return Size; } 126 127 void writeTo(uint8_t *Buf) const override { 128 memset(Buf, 0, Size); 129 } 130 131 private: 132 size_t Size; 133 }; 134 135 static std::vector<std::vector<DefinedImportData *>> 136 binImports(const std::vector<DefinedImportData *> &Imports) { 137 // Group DLL-imported symbols by DLL name because that's how 138 // symbols are layed out in the import descriptor table. 139 auto Less = [](const std::string &A, const std::string &B) { 140 return Config->DLLOrder[A] < Config->DLLOrder[B]; 141 }; 142 std::map<std::string, std::vector<DefinedImportData *>, 143 bool(*)(const std::string &, const std::string &)> M(Less); 144 for (DefinedImportData *Sym : Imports) 145 M[Sym->getDLLName().lower()].push_back(Sym); 146 147 std::vector<std::vector<DefinedImportData *>> V; 148 for (auto &KV : M) { 149 // Sort symbols by name for each group. 150 std::vector<DefinedImportData *> &Syms = KV.second; 151 std::sort(Syms.begin(), Syms.end(), 152 [](DefinedImportData *A, DefinedImportData *B) { 153 return A->getName() < B->getName(); 154 }); 155 V.push_back(std::move(Syms)); 156 } 157 return V; 158 } 159 160 // Export table 161 // See Microsoft PE/COFF spec 4.3 for details. 162 163 // A chunk for the delay import descriptor table etnry. 164 class DelayDirectoryChunk : public NonSectionChunk { 165 public: 166 explicit DelayDirectoryChunk(Chunk *N) : DLLName(N) {} 167 168 size_t getSize() const override { 169 return sizeof(delay_import_directory_table_entry); 170 } 171 172 void writeTo(uint8_t *Buf) const override { 173 memset(Buf, 0, getSize()); 174 175 auto *E = (delay_import_directory_table_entry *)(Buf); 176 E->Attributes = 1; 177 E->Name = DLLName->getRVA(); 178 E->ModuleHandle = ModuleHandle->getRVA(); 179 E->DelayImportAddressTable = AddressTab->getRVA(); 180 E->DelayImportNameTable = NameTab->getRVA(); 181 } 182 183 Chunk *DLLName; 184 Chunk *ModuleHandle; 185 Chunk *AddressTab; 186 Chunk *NameTab; 187 }; 188 189 // Initial contents for delay-loaded functions. 190 // This code calls __delayLoadHelper2 function to resolve a symbol 191 // and then overwrites its jump table slot with the result 192 // for subsequent function calls. 193 static const uint8_t ThunkX64[] = { 194 0x51, // push rcx 195 0x52, // push rdx 196 0x41, 0x50, // push r8 197 0x41, 0x51, // push r9 198 0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h 199 0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0 200 0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1 201 0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2 202 0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3 203 0x48, 0x8D, 0x15, 0, 0, 0, 0, // lea rdx, [__imp_<FUNCNAME>] 204 0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...] 205 0xE8, 0, 0, 0, 0, // call __delayLoadHelper2 206 0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp] 207 0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h] 208 0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h] 209 0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h] 210 0x48, 0x83, 0xC4, 0x48, // add rsp, 48h 211 0x41, 0x59, // pop r9 212 0x41, 0x58, // pop r8 213 0x5A, // pop rdx 214 0x59, // pop rcx 215 0xFF, 0xE0, // jmp rax 216 }; 217 218 static const uint8_t ThunkX86[] = { 219 0x51, // push ecx 220 0x52, // push edx 221 0x68, 0, 0, 0, 0, // push offset ___imp__<FUNCNAME> 222 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll 223 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8 224 0x5A, // pop edx 225 0x59, // pop ecx 226 0xFF, 0xE0, // jmp eax 227 }; 228 229 static const uint8_t ThunkARM[] = { 230 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 __imp_<FUNCNAME> 231 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 __imp_<FUNCNAME> 232 0x2d, 0xe9, 0x0f, 0x48, // push.w {r0, r1, r2, r3, r11, lr} 233 0x0d, 0xf2, 0x10, 0x0b, // addw r11, sp, #16 234 0x2d, 0xed, 0x10, 0x0b, // vpush {d0, d1, d2, d3, d4, d5, d6, d7} 235 0x61, 0x46, // mov r1, ip 236 0x40, 0xf2, 0x00, 0x00, // mov.w r0, #0 DELAY_IMPORT_DESCRIPTOR 237 0xc0, 0xf2, 0x00, 0x00, // mov.t r0, #0 DELAY_IMPORT_DESCRIPTOR 238 0x00, 0xf0, 0x00, 0xd0, // bl #0 __delayLoadHelper2 239 0x84, 0x46, // mov ip, r0 240 0xbd, 0xec, 0x10, 0x0b, // vpop {d0, d1, d2, d3, d4, d5, d6, d7} 241 0xbd, 0xe8, 0x0f, 0x48, // pop.w {r0, r1, r2, r3, r11, lr} 242 0x60, 0x47, // bx ip 243 }; 244 245 static const uint8_t ThunkARM64[] = { 246 0x11, 0x00, 0x00, 0x90, // adrp x17, #0 __imp_<FUNCNAME> 247 0x31, 0x02, 0x00, 0x91, // add x17, x17, #0 :lo12:__imp_<FUNCNAME> 248 0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]! 249 0xfd, 0x03, 0x00, 0x91, // mov x29, sp 250 0xe0, 0x07, 0x01, 0xa9, // stp x0, x1, [sp, #16] 251 0xe2, 0x0f, 0x02, 0xa9, // stp x2, x3, [sp, #32] 252 0xe4, 0x17, 0x03, 0xa9, // stp x4, x5, [sp, #48] 253 0xe6, 0x1f, 0x04, 0xa9, // stp x6, x7, [sp, #64] 254 0xe0, 0x87, 0x02, 0xad, // stp q0, q1, [sp, #80] 255 0xe2, 0x8f, 0x03, 0xad, // stp q2, q3, [sp, #112] 256 0xe4, 0x97, 0x04, 0xad, // stp q4, q5, [sp, #144] 257 0xe6, 0x9f, 0x05, 0xad, // stp q6, q7, [sp, #176] 258 0xe1, 0x03, 0x11, 0xaa, // mov x1, x17 259 0x00, 0x00, 0x00, 0x90, // adrp x0, #0 DELAY_IMPORT_DESCRIPTOR 260 0x00, 0x00, 0x00, 0x91, // add x0, x0, #0 :lo12:DELAY_IMPORT_DESCRIPTOR 261 0x00, 0x00, 0x00, 0x94, // bl #0 __delayLoadHelper2 262 0xf0, 0x03, 0x00, 0xaa, // mov x16, x0 263 0xe6, 0x9f, 0x45, 0xad, // ldp q6, q7, [sp, #176] 264 0xe4, 0x97, 0x44, 0xad, // ldp q4, q5, [sp, #144] 265 0xe2, 0x8f, 0x43, 0xad, // ldp q2, q3, [sp, #112] 266 0xe0, 0x87, 0x42, 0xad, // ldp q0, q1, [sp, #80] 267 0xe6, 0x1f, 0x44, 0xa9, // ldp x6, x7, [sp, #64] 268 0xe4, 0x17, 0x43, 0xa9, // ldp x4, x5, [sp, #48] 269 0xe2, 0x0f, 0x42, 0xa9, // ldp x2, x3, [sp, #32] 270 0xe0, 0x07, 0x41, 0xa9, // ldp x0, x1, [sp, #16] 271 0xfd, 0x7b, 0xcd, 0xa8, // ldp x29, x30, [sp], #208 272 0x00, 0x02, 0x1f, 0xd6, // br x16 273 }; 274 275 // A chunk for the delay import thunk. 276 class ThunkChunkX64 : public NonSectionChunk { 277 public: 278 ThunkChunkX64(Defined *I, Chunk *D, Defined *H) 279 : Imp(I), Desc(D), Helper(H) {} 280 281 size_t getSize() const override { return sizeof(ThunkX64); } 282 283 void writeTo(uint8_t *Buf) const override { 284 memcpy(Buf, ThunkX64, sizeof(ThunkX64)); 285 write32le(Buf + 36, Imp->getRVA() - RVA - 40); 286 write32le(Buf + 43, Desc->getRVA() - RVA - 47); 287 write32le(Buf + 48, Helper->getRVA() - RVA - 52); 288 } 289 290 Defined *Imp = nullptr; 291 Chunk *Desc = nullptr; 292 Defined *Helper = nullptr; 293 }; 294 295 class ThunkChunkX86 : public NonSectionChunk { 296 public: 297 ThunkChunkX86(Defined *I, Chunk *D, Defined *H) 298 : Imp(I), Desc(D), Helper(H) {} 299 300 size_t getSize() const override { return sizeof(ThunkX86); } 301 302 void writeTo(uint8_t *Buf) const override { 303 memcpy(Buf, ThunkX86, sizeof(ThunkX86)); 304 write32le(Buf + 3, Imp->getRVA() + Config->ImageBase); 305 write32le(Buf + 8, Desc->getRVA() + Config->ImageBase); 306 write32le(Buf + 13, Helper->getRVA() - RVA - 17); 307 } 308 309 void getBaserels(std::vector<Baserel> *Res) override { 310 Res->emplace_back(RVA + 3); 311 Res->emplace_back(RVA + 8); 312 } 313 314 Defined *Imp = nullptr; 315 Chunk *Desc = nullptr; 316 Defined *Helper = nullptr; 317 }; 318 319 class ThunkChunkARM : public NonSectionChunk { 320 public: 321 ThunkChunkARM(Defined *I, Chunk *D, Defined *H) 322 : Imp(I), Desc(D), Helper(H) {} 323 324 size_t getSize() const override { return sizeof(ThunkARM); } 325 326 void writeTo(uint8_t *Buf) const override { 327 memcpy(Buf, ThunkARM, sizeof(ThunkARM)); 328 applyMOV32T(Buf + 0, Imp->getRVA() + Config->ImageBase); 329 applyMOV32T(Buf + 22, Desc->getRVA() + Config->ImageBase); 330 applyBranch24T(Buf + 30, Helper->getRVA() - RVA - 34); 331 } 332 333 void getBaserels(std::vector<Baserel> *Res) override { 334 Res->emplace_back(RVA + 0, IMAGE_REL_BASED_ARM_MOV32T); 335 Res->emplace_back(RVA + 22, IMAGE_REL_BASED_ARM_MOV32T); 336 } 337 338 Defined *Imp = nullptr; 339 Chunk *Desc = nullptr; 340 Defined *Helper = nullptr; 341 }; 342 343 class ThunkChunkARM64 : public NonSectionChunk { 344 public: 345 ThunkChunkARM64(Defined *I, Chunk *D, Defined *H) 346 : Imp(I), Desc(D), Helper(H) {} 347 348 size_t getSize() const override { return sizeof(ThunkARM64); } 349 350 void writeTo(uint8_t *Buf) const override { 351 memcpy(Buf, ThunkARM64, sizeof(ThunkARM64)); 352 applyArm64Addr(Buf + 0, Imp->getRVA(), RVA + 0, 12); 353 applyArm64Imm(Buf + 4, Imp->getRVA() & 0xfff, 0); 354 applyArm64Addr(Buf + 52, Desc->getRVA(), RVA + 52, 12); 355 applyArm64Imm(Buf + 56, Desc->getRVA() & 0xfff, 0); 356 applyArm64Branch26(Buf + 60, Helper->getRVA() - RVA - 60); 357 } 358 359 Defined *Imp = nullptr; 360 Chunk *Desc = nullptr; 361 Defined *Helper = nullptr; 362 }; 363 364 // A chunk for the import descriptor table. 365 class DelayAddressChunk : public NonSectionChunk { 366 public: 367 explicit DelayAddressChunk(Chunk *C) : Thunk(C) { 368 setAlignment(Config->Wordsize); 369 } 370 size_t getSize() const override { return Config->Wordsize; } 371 372 void writeTo(uint8_t *Buf) const override { 373 if (Config->is64()) { 374 write64le(Buf, Thunk->getRVA() + Config->ImageBase); 375 } else { 376 uint32_t Bit = 0; 377 // Pointer to thumb code must have the LSB set, so adjust it. 378 if (Config->Machine == ARMNT) 379 Bit = 1; 380 write32le(Buf, (Thunk->getRVA() + Config->ImageBase) | Bit); 381 } 382 } 383 384 void getBaserels(std::vector<Baserel> *Res) override { 385 Res->emplace_back(RVA); 386 } 387 388 Chunk *Thunk; 389 }; 390 391 // Export table 392 // Read Microsoft PE/COFF spec 5.3 for details. 393 394 // A chunk for the export descriptor table. 395 class ExportDirectoryChunk : public NonSectionChunk { 396 public: 397 ExportDirectoryChunk(int I, int J, Chunk *D, Chunk *A, Chunk *N, Chunk *O) 398 : MaxOrdinal(I), NameTabSize(J), DLLName(D), AddressTab(A), NameTab(N), 399 OrdinalTab(O) {} 400 401 size_t getSize() const override { 402 return sizeof(export_directory_table_entry); 403 } 404 405 void writeTo(uint8_t *Buf) const override { 406 memset(Buf, 0, getSize()); 407 408 auto *E = (export_directory_table_entry *)(Buf); 409 E->NameRVA = DLLName->getRVA(); 410 E->OrdinalBase = 0; 411 E->AddressTableEntries = MaxOrdinal + 1; 412 E->NumberOfNamePointers = NameTabSize; 413 E->ExportAddressTableRVA = AddressTab->getRVA(); 414 E->NamePointerRVA = NameTab->getRVA(); 415 E->OrdinalTableRVA = OrdinalTab->getRVA(); 416 } 417 418 uint16_t MaxOrdinal; 419 uint16_t NameTabSize; 420 Chunk *DLLName; 421 Chunk *AddressTab; 422 Chunk *NameTab; 423 Chunk *OrdinalTab; 424 }; 425 426 class AddressTableChunk : public NonSectionChunk { 427 public: 428 explicit AddressTableChunk(size_t MaxOrdinal) : Size(MaxOrdinal + 1) {} 429 size_t getSize() const override { return Size * 4; } 430 431 void writeTo(uint8_t *Buf) const override { 432 memset(Buf, 0, getSize()); 433 434 for (const Export &E : Config->Exports) { 435 uint8_t *P = Buf + E.Ordinal * 4; 436 uint32_t Bit = 0; 437 // Pointer to thumb code must have the LSB set, so adjust it. 438 if (Config->Machine == ARMNT && !E.Data) 439 Bit = 1; 440 if (E.ForwardChunk) { 441 write32le(P, E.ForwardChunk->getRVA() | Bit); 442 } else { 443 write32le(P, cast<Defined>(E.Sym)->getRVA() | Bit); 444 } 445 } 446 } 447 448 private: 449 size_t Size; 450 }; 451 452 class NamePointersChunk : public NonSectionChunk { 453 public: 454 explicit NamePointersChunk(std::vector<Chunk *> &V) : Chunks(V) {} 455 size_t getSize() const override { return Chunks.size() * 4; } 456 457 void writeTo(uint8_t *Buf) const override { 458 for (Chunk *C : Chunks) { 459 write32le(Buf, C->getRVA()); 460 Buf += 4; 461 } 462 } 463 464 private: 465 std::vector<Chunk *> Chunks; 466 }; 467 468 class ExportOrdinalChunk : public NonSectionChunk { 469 public: 470 explicit ExportOrdinalChunk(size_t I) : Size(I) {} 471 size_t getSize() const override { return Size * 2; } 472 473 void writeTo(uint8_t *Buf) const override { 474 for (Export &E : Config->Exports) { 475 if (E.Noname) 476 continue; 477 write16le(Buf, E.Ordinal); 478 Buf += 2; 479 } 480 } 481 482 private: 483 size_t Size; 484 }; 485 486 } // anonymous namespace 487 488 void IdataContents::create() { 489 std::vector<std::vector<DefinedImportData *>> V = binImports(Imports); 490 491 // Create .idata contents for each DLL. 492 for (std::vector<DefinedImportData *> &Syms : V) { 493 // Create lookup and address tables. If they have external names, 494 // we need to create HintName chunks to store the names. 495 // If they don't (if they are import-by-ordinals), we store only 496 // ordinal values to the table. 497 size_t Base = Lookups.size(); 498 for (DefinedImportData *S : Syms) { 499 uint16_t Ord = S->getOrdinal(); 500 if (S->getExternalName().empty()) { 501 Lookups.push_back(make<OrdinalOnlyChunk>(Ord)); 502 Addresses.push_back(make<OrdinalOnlyChunk>(Ord)); 503 continue; 504 } 505 auto *C = make<HintNameChunk>(S->getExternalName(), Ord); 506 Lookups.push_back(make<LookupChunk>(C)); 507 Addresses.push_back(make<LookupChunk>(C)); 508 Hints.push_back(C); 509 } 510 // Terminate with null values. 511 Lookups.push_back(make<NullChunk>(Config->Wordsize)); 512 Addresses.push_back(make<NullChunk>(Config->Wordsize)); 513 514 for (int I = 0, E = Syms.size(); I < E; ++I) 515 Syms[I]->setLocation(Addresses[Base + I]); 516 517 // Create the import table header. 518 DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName())); 519 auto *Dir = make<ImportDirectoryChunk>(DLLNames.back()); 520 Dir->LookupTab = Lookups[Base]; 521 Dir->AddressTab = Addresses[Base]; 522 Dirs.push_back(Dir); 523 } 524 // Add null terminator. 525 Dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry))); 526 } 527 528 std::vector<Chunk *> DelayLoadContents::getChunks() { 529 std::vector<Chunk *> V; 530 V.insert(V.end(), Dirs.begin(), Dirs.end()); 531 V.insert(V.end(), Names.begin(), Names.end()); 532 V.insert(V.end(), HintNames.begin(), HintNames.end()); 533 V.insert(V.end(), DLLNames.begin(), DLLNames.end()); 534 return V; 535 } 536 537 std::vector<Chunk *> DelayLoadContents::getDataChunks() { 538 std::vector<Chunk *> V; 539 V.insert(V.end(), ModuleHandles.begin(), ModuleHandles.end()); 540 V.insert(V.end(), Addresses.begin(), Addresses.end()); 541 return V; 542 } 543 544 uint64_t DelayLoadContents::getDirSize() { 545 return Dirs.size() * sizeof(delay_import_directory_table_entry); 546 } 547 548 void DelayLoadContents::create(Defined *H) { 549 Helper = H; 550 std::vector<std::vector<DefinedImportData *>> V = binImports(Imports); 551 552 // Create .didat contents for each DLL. 553 for (std::vector<DefinedImportData *> &Syms : V) { 554 // Create the delay import table header. 555 DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName())); 556 auto *Dir = make<DelayDirectoryChunk>(DLLNames.back()); 557 558 size_t Base = Addresses.size(); 559 for (DefinedImportData *S : Syms) { 560 Chunk *T = newThunkChunk(S, Dir); 561 auto *A = make<DelayAddressChunk>(T); 562 Addresses.push_back(A); 563 Thunks.push_back(T); 564 StringRef ExtName = S->getExternalName(); 565 if (ExtName.empty()) { 566 Names.push_back(make<OrdinalOnlyChunk>(S->getOrdinal())); 567 } else { 568 auto *C = make<HintNameChunk>(ExtName, 0); 569 Names.push_back(make<LookupChunk>(C)); 570 HintNames.push_back(C); 571 } 572 } 573 // Terminate with null values. 574 Addresses.push_back(make<NullChunk>(8)); 575 Names.push_back(make<NullChunk>(8)); 576 577 for (int I = 0, E = Syms.size(); I < E; ++I) 578 Syms[I]->setLocation(Addresses[Base + I]); 579 auto *MH = make<NullChunk>(8); 580 MH->setAlignment(8); 581 ModuleHandles.push_back(MH); 582 583 // Fill the delay import table header fields. 584 Dir->ModuleHandle = MH; 585 Dir->AddressTab = Addresses[Base]; 586 Dir->NameTab = Names[Base]; 587 Dirs.push_back(Dir); 588 } 589 // Add null terminator. 590 Dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry))); 591 } 592 593 Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *S, Chunk *Dir) { 594 switch (Config->Machine) { 595 case AMD64: 596 return make<ThunkChunkX64>(S, Dir, Helper); 597 case I386: 598 return make<ThunkChunkX86>(S, Dir, Helper); 599 case ARMNT: 600 return make<ThunkChunkARM>(S, Dir, Helper); 601 case ARM64: 602 return make<ThunkChunkARM64>(S, Dir, Helper); 603 default: 604 llvm_unreachable("unsupported machine type"); 605 } 606 } 607 608 EdataContents::EdataContents() { 609 uint16_t MaxOrdinal = 0; 610 for (Export &E : Config->Exports) 611 MaxOrdinal = std::max(MaxOrdinal, E.Ordinal); 612 613 auto *DLLName = make<StringChunk>(sys::path::filename(Config->OutputFile)); 614 auto *AddressTab = make<AddressTableChunk>(MaxOrdinal); 615 std::vector<Chunk *> Names; 616 for (Export &E : Config->Exports) 617 if (!E.Noname) 618 Names.push_back(make<StringChunk>(E.ExportName)); 619 620 std::vector<Chunk *> Forwards; 621 for (Export &E : Config->Exports) { 622 if (E.ForwardTo.empty()) 623 continue; 624 E.ForwardChunk = make<StringChunk>(E.ForwardTo); 625 Forwards.push_back(E.ForwardChunk); 626 } 627 628 auto *NameTab = make<NamePointersChunk>(Names); 629 auto *OrdinalTab = make<ExportOrdinalChunk>(Names.size()); 630 auto *Dir = make<ExportDirectoryChunk>(MaxOrdinal, Names.size(), DLLName, 631 AddressTab, NameTab, OrdinalTab); 632 Chunks.push_back(Dir); 633 Chunks.push_back(DLLName); 634 Chunks.push_back(AddressTab); 635 Chunks.push_back(NameTab); 636 Chunks.push_back(OrdinalTab); 637 Chunks.insert(Chunks.end(), Names.begin(), Names.end()); 638 Chunks.insert(Chunks.end(), Forwards.begin(), Forwards.end()); 639 } 640 641 } // namespace coff 642 } // namespace lld 643