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/Object/COFF.h" 25 #include "llvm/Support/Endian.h" 26 #include "llvm/Support/Path.h" 27 28 using namespace llvm; 29 using namespace llvm::object; 30 using namespace llvm::support::endian; 31 using namespace llvm::COFF; 32 33 namespace lld { 34 namespace 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(Chunk *c) : hintName(c) { 65 setAlignment(config->wordsize); 66 } 67 size_t getSize() const override { return config->wordsize; } 68 69 void writeTo(uint8_t *buf) const override { 70 if (config->is64()) 71 write64le(buf, hintName->getRVA()); 72 else 73 write32le(buf, hintName->getRVA()); 74 } 75 76 Chunk *hintName; 77 }; 78 79 // A chunk for the import descriptor table. 80 // This chunk represent import-by-ordinal symbols. 81 // See Microsoft PE/COFF spec 7.1. Import Header for details. 82 class OrdinalOnlyChunk : public NonSectionChunk { 83 public: 84 explicit OrdinalOnlyChunk(uint16_t v) : ordinal(v) { 85 setAlignment(config->wordsize); 86 } 87 size_t getSize() const override { return config->wordsize; } 88 89 void writeTo(uint8_t *buf) const override { 90 // An import-by-ordinal slot has MSB 1 to indicate that 91 // this is import-by-ordinal (and not import-by-name). 92 if (config->is64()) { 93 write64le(buf, (1ULL << 63) | ordinal); 94 } else { 95 write32le(buf, (1ULL << 31) | ordinal); 96 } 97 } 98 99 uint16_t ordinal; 100 }; 101 102 // A chunk for the import descriptor table. 103 class ImportDirectoryChunk : public NonSectionChunk { 104 public: 105 explicit ImportDirectoryChunk(Chunk *n) : dllName(n) {} 106 size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); } 107 108 void writeTo(uint8_t *buf) const override { 109 memset(buf, 0, getSize()); 110 111 auto *e = (coff_import_directory_table_entry *)(buf); 112 e->ImportLookupTableRVA = lookupTab->getRVA(); 113 e->NameRVA = dllName->getRVA(); 114 e->ImportAddressTableRVA = addressTab->getRVA(); 115 } 116 117 Chunk *dllName; 118 Chunk *lookupTab; 119 Chunk *addressTab; 120 }; 121 122 // A chunk representing null terminator in the import table. 123 // Contents of this chunk is always null bytes. 124 class NullChunk : public NonSectionChunk { 125 public: 126 explicit NullChunk(size_t n) : size(n) { hasData = false; } 127 size_t getSize() const override { return size; } 128 129 void writeTo(uint8_t *buf) const override { 130 memset(buf, 0, size); 131 } 132 133 private: 134 size_t size; 135 }; 136 137 static std::vector<std::vector<DefinedImportData *>> 138 binImports(const std::vector<DefinedImportData *> &imports) { 139 // Group DLL-imported symbols by DLL name because that's how 140 // symbols are laid out in the import descriptor table. 141 auto less = [](const std::string &a, const std::string &b) { 142 return config->dllOrder[a] < config->dllOrder[b]; 143 }; 144 std::map<std::string, std::vector<DefinedImportData *>, 145 bool(*)(const std::string &, const std::string &)> m(less); 146 for (DefinedImportData *sym : imports) 147 m[sym->getDLLName().lower()].push_back(sym); 148 149 std::vector<std::vector<DefinedImportData *>> v; 150 for (auto &kv : m) { 151 // Sort symbols by name for each group. 152 std::vector<DefinedImportData *> &syms = kv.second; 153 std::sort(syms.begin(), syms.end(), 154 [](DefinedImportData *a, DefinedImportData *b) { 155 return a->getName() < b->getName(); 156 }); 157 v.push_back(std::move(syms)); 158 } 159 return v; 160 } 161 162 // See Microsoft PE/COFF spec 4.3 for details. 163 164 // A chunk for the delay import descriptor table etnry. 165 class DelayDirectoryChunk : public NonSectionChunk { 166 public: 167 explicit DelayDirectoryChunk(Chunk *n) : dllName(n) {} 168 169 size_t getSize() const override { 170 return sizeof(delay_import_directory_table_entry); 171 } 172 173 void writeTo(uint8_t *buf) const override { 174 memset(buf, 0, getSize()); 175 176 auto *e = (delay_import_directory_table_entry *)(buf); 177 e->Attributes = 1; 178 e->Name = dllName->getRVA(); 179 e->ModuleHandle = moduleHandle->getRVA(); 180 e->DelayImportAddressTable = addressTab->getRVA(); 181 e->DelayImportNameTable = nameTab->getRVA(); 182 } 183 184 Chunk *dllName; 185 Chunk *moduleHandle; 186 Chunk *addressTab; 187 Chunk *nameTab; 188 }; 189 190 // Initial contents for delay-loaded functions. 191 // This code calls __delayLoadHelper2 function to resolve a symbol 192 // which then overwrites its jump table slot with the result 193 // for subsequent function calls. 194 static const uint8_t thunkX64[] = { 195 0x48, 0x8D, 0x05, 0, 0, 0, 0, // lea rax, [__imp_<FUNCNAME>] 196 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib> 197 }; 198 199 static const uint8_t tailMergeX64[] = { 200 0x51, // push rcx 201 0x52, // push rdx 202 0x41, 0x50, // push r8 203 0x41, 0x51, // push r9 204 0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h 205 0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0 206 0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1 207 0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2 208 0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3 209 0x48, 0x8B, 0xD0, // mov rdx, rax 210 0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...] 211 0xE8, 0, 0, 0, 0, // call __delayLoadHelper2 212 0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp] 213 0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h] 214 0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h] 215 0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h] 216 0x48, 0x83, 0xC4, 0x48, // add rsp, 48h 217 0x41, 0x59, // pop r9 218 0x41, 0x58, // pop r8 219 0x5A, // pop rdx 220 0x59, // pop rcx 221 0xFF, 0xE0, // jmp rax 222 }; 223 224 static const uint8_t thunkX86[] = { 225 0xB8, 0, 0, 0, 0, // mov eax, offset ___imp__<FUNCNAME> 226 0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib> 227 }; 228 229 static const uint8_t tailMergeX86[] = { 230 0x51, // push ecx 231 0x52, // push edx 232 0x50, // push eax 233 0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll 234 0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8 235 0x5A, // pop edx 236 0x59, // pop ecx 237 0xFF, 0xE0, // jmp eax 238 }; 239 240 static const uint8_t thunkARM[] = { 241 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 __imp_<FUNCNAME> 242 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 __imp_<FUNCNAME> 243 0x00, 0xf0, 0x00, 0xb8, // b.w __tailMerge_<lib> 244 }; 245 246 static const uint8_t tailMergeARM[] = { 247 0x2d, 0xe9, 0x0f, 0x48, // push.w {r0, r1, r2, r3, r11, lr} 248 0x0d, 0xf2, 0x10, 0x0b, // addw r11, sp, #16 249 0x2d, 0xed, 0x10, 0x0b, // vpush {d0, d1, d2, d3, d4, d5, d6, d7} 250 0x61, 0x46, // mov r1, ip 251 0x40, 0xf2, 0x00, 0x00, // mov.w r0, #0 DELAY_IMPORT_DESCRIPTOR 252 0xc0, 0xf2, 0x00, 0x00, // mov.t r0, #0 DELAY_IMPORT_DESCRIPTOR 253 0x00, 0xf0, 0x00, 0xd0, // bl #0 __delayLoadHelper2 254 0x84, 0x46, // mov ip, r0 255 0xbd, 0xec, 0x10, 0x0b, // vpop {d0, d1, d2, d3, d4, d5, d6, d7} 256 0xbd, 0xe8, 0x0f, 0x48, // pop.w {r0, r1, r2, r3, r11, lr} 257 0x60, 0x47, // bx ip 258 }; 259 260 static const uint8_t thunkARM64[] = { 261 0x11, 0x00, 0x00, 0x90, // adrp x17, #0 __imp_<FUNCNAME> 262 0x31, 0x02, 0x00, 0x91, // add x17, x17, #0 :lo12:__imp_<FUNCNAME> 263 0x00, 0x00, 0x00, 0x14, // b __tailMerge_<lib> 264 }; 265 266 static const uint8_t tailMergeARM64[] = { 267 0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]! 268 0xfd, 0x03, 0x00, 0x91, // mov x29, sp 269 0xe0, 0x07, 0x01, 0xa9, // stp x0, x1, [sp, #16] 270 0xe2, 0x0f, 0x02, 0xa9, // stp x2, x3, [sp, #32] 271 0xe4, 0x17, 0x03, 0xa9, // stp x4, x5, [sp, #48] 272 0xe6, 0x1f, 0x04, 0xa9, // stp x6, x7, [sp, #64] 273 0xe0, 0x87, 0x02, 0xad, // stp q0, q1, [sp, #80] 274 0xe2, 0x8f, 0x03, 0xad, // stp q2, q3, [sp, #112] 275 0xe4, 0x97, 0x04, 0xad, // stp q4, q5, [sp, #144] 276 0xe6, 0x9f, 0x05, 0xad, // stp q6, q7, [sp, #176] 277 0xe1, 0x03, 0x11, 0xaa, // mov x1, x17 278 0x00, 0x00, 0x00, 0x90, // adrp x0, #0 DELAY_IMPORT_DESCRIPTOR 279 0x00, 0x00, 0x00, 0x91, // add x0, x0, #0 :lo12:DELAY_IMPORT_DESCRIPTOR 280 0x00, 0x00, 0x00, 0x94, // bl #0 __delayLoadHelper2 281 0xf0, 0x03, 0x00, 0xaa, // mov x16, x0 282 0xe6, 0x9f, 0x45, 0xad, // ldp q6, q7, [sp, #176] 283 0xe4, 0x97, 0x44, 0xad, // ldp q4, q5, [sp, #144] 284 0xe2, 0x8f, 0x43, 0xad, // ldp q2, q3, [sp, #112] 285 0xe0, 0x87, 0x42, 0xad, // ldp q0, q1, [sp, #80] 286 0xe6, 0x1f, 0x44, 0xa9, // ldp x6, x7, [sp, #64] 287 0xe4, 0x17, 0x43, 0xa9, // ldp x4, x5, [sp, #48] 288 0xe2, 0x0f, 0x42, 0xa9, // ldp x2, x3, [sp, #32] 289 0xe0, 0x07, 0x41, 0xa9, // ldp x0, x1, [sp, #16] 290 0xfd, 0x7b, 0xcd, 0xa8, // ldp x29, x30, [sp], #208 291 0x00, 0x02, 0x1f, 0xd6, // br x16 292 }; 293 294 // A chunk for the delay import thunk. 295 class ThunkChunkX64 : public NonSectionChunk { 296 public: 297 ThunkChunkX64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {} 298 299 size_t getSize() const override { return sizeof(thunkX64); } 300 301 void writeTo(uint8_t *buf) const override { 302 memcpy(buf, thunkX64, sizeof(thunkX64)); 303 write32le(buf + 3, imp->getRVA() - rva - 7); 304 write32le(buf + 8, tailMerge->getRVA() - rva - 12); 305 } 306 307 Defined *imp = nullptr; 308 Chunk *tailMerge = nullptr; 309 }; 310 311 class TailMergeChunkX64 : public NonSectionChunk { 312 public: 313 TailMergeChunkX64(Chunk *d, Defined *h) : desc(d), helper(h) {} 314 315 size_t getSize() const override { return sizeof(tailMergeX64); } 316 317 void writeTo(uint8_t *buf) const override { 318 memcpy(buf, tailMergeX64, sizeof(tailMergeX64)); 319 write32le(buf + 39, desc->getRVA() - rva - 43); 320 write32le(buf + 44, helper->getRVA() - rva - 48); 321 } 322 323 Chunk *desc = nullptr; 324 Defined *helper = nullptr; 325 }; 326 327 class ThunkChunkX86 : public NonSectionChunk { 328 public: 329 ThunkChunkX86(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {} 330 331 size_t getSize() const override { return sizeof(thunkX86); } 332 333 void writeTo(uint8_t *buf) const override { 334 memcpy(buf, thunkX86, sizeof(thunkX86)); 335 write32le(buf + 1, imp->getRVA() + config->imageBase); 336 write32le(buf + 6, tailMerge->getRVA() - rva - 10); 337 } 338 339 void getBaserels(std::vector<Baserel> *res) override { 340 res->emplace_back(rva + 1); 341 } 342 343 Defined *imp = nullptr; 344 Chunk *tailMerge = nullptr; 345 }; 346 347 class TailMergeChunkX86 : public NonSectionChunk { 348 public: 349 TailMergeChunkX86(Chunk *d, Defined *h) : desc(d), helper(h) {} 350 351 size_t getSize() const override { return sizeof(tailMergeX86); } 352 353 void writeTo(uint8_t *buf) const override { 354 memcpy(buf, tailMergeX86, sizeof(tailMergeX86)); 355 write32le(buf + 4, desc->getRVA() + config->imageBase); 356 write32le(buf + 9, helper->getRVA() - rva - 13); 357 } 358 359 void getBaserels(std::vector<Baserel> *res) override { 360 res->emplace_back(rva + 4); 361 } 362 363 Chunk *desc = nullptr; 364 Defined *helper = nullptr; 365 }; 366 367 class ThunkChunkARM : public NonSectionChunk { 368 public: 369 ThunkChunkARM(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) { 370 setAlignment(2); 371 } 372 373 size_t getSize() const override { return sizeof(thunkARM); } 374 375 void writeTo(uint8_t *buf) const override { 376 memcpy(buf, thunkARM, sizeof(thunkARM)); 377 applyMOV32T(buf + 0, imp->getRVA() + config->imageBase); 378 applyBranch24T(buf + 8, tailMerge->getRVA() - rva - 12); 379 } 380 381 void getBaserels(std::vector<Baserel> *res) override { 382 res->emplace_back(rva + 0, IMAGE_REL_BASED_ARM_MOV32T); 383 } 384 385 Defined *imp = nullptr; 386 Chunk *tailMerge = nullptr; 387 }; 388 389 class TailMergeChunkARM : public NonSectionChunk { 390 public: 391 TailMergeChunkARM(Chunk *d, Defined *h) : desc(d), helper(h) { 392 setAlignment(2); 393 } 394 395 size_t getSize() const override { return sizeof(tailMergeARM); } 396 397 void writeTo(uint8_t *buf) const override { 398 memcpy(buf, tailMergeARM, sizeof(tailMergeARM)); 399 applyMOV32T(buf + 14, desc->getRVA() + config->imageBase); 400 applyBranch24T(buf + 22, helper->getRVA() - rva - 26); 401 } 402 403 void getBaserels(std::vector<Baserel> *res) override { 404 res->emplace_back(rva + 14, IMAGE_REL_BASED_ARM_MOV32T); 405 } 406 407 Chunk *desc = nullptr; 408 Defined *helper = nullptr; 409 }; 410 411 class ThunkChunkARM64 : public NonSectionChunk { 412 public: 413 ThunkChunkARM64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) { 414 setAlignment(4); 415 } 416 417 size_t getSize() const override { return sizeof(thunkARM64); } 418 419 void writeTo(uint8_t *buf) const override { 420 memcpy(buf, thunkARM64, sizeof(thunkARM64)); 421 applyArm64Addr(buf + 0, imp->getRVA(), rva + 0, 12); 422 applyArm64Imm(buf + 4, imp->getRVA() & 0xfff, 0); 423 applyArm64Branch26(buf + 8, tailMerge->getRVA() - rva - 8); 424 } 425 426 Defined *imp = nullptr; 427 Chunk *tailMerge = nullptr; 428 }; 429 430 class TailMergeChunkARM64 : public NonSectionChunk { 431 public: 432 TailMergeChunkARM64(Chunk *d, Defined *h) : desc(d), helper(h) { 433 setAlignment(4); 434 } 435 436 size_t getSize() const override { return sizeof(tailMergeARM64); } 437 438 void writeTo(uint8_t *buf) const override { 439 memcpy(buf, tailMergeARM64, sizeof(tailMergeARM64)); 440 applyArm64Addr(buf + 44, desc->getRVA(), rva + 44, 12); 441 applyArm64Imm(buf + 48, desc->getRVA() & 0xfff, 0); 442 applyArm64Branch26(buf + 52, helper->getRVA() - rva - 52); 443 } 444 445 Chunk *desc = nullptr; 446 Defined *helper = nullptr; 447 }; 448 449 // A chunk for the import descriptor table. 450 class DelayAddressChunk : public NonSectionChunk { 451 public: 452 explicit DelayAddressChunk(Chunk *c) : thunk(c) { 453 setAlignment(config->wordsize); 454 } 455 size_t getSize() const override { return config->wordsize; } 456 457 void writeTo(uint8_t *buf) const override { 458 if (config->is64()) { 459 write64le(buf, thunk->getRVA() + config->imageBase); 460 } else { 461 uint32_t bit = 0; 462 // Pointer to thumb code must have the LSB set, so adjust it. 463 if (config->machine == ARMNT) 464 bit = 1; 465 write32le(buf, (thunk->getRVA() + config->imageBase) | bit); 466 } 467 } 468 469 void getBaserels(std::vector<Baserel> *res) override { 470 res->emplace_back(rva); 471 } 472 473 Chunk *thunk; 474 }; 475 476 // Export table 477 // Read Microsoft PE/COFF spec 5.3 for details. 478 479 // A chunk for the export descriptor table. 480 class ExportDirectoryChunk : public NonSectionChunk { 481 public: 482 ExportDirectoryChunk(int i, int j, Chunk *d, Chunk *a, Chunk *n, Chunk *o) 483 : maxOrdinal(i), nameTabSize(j), dllName(d), addressTab(a), nameTab(n), 484 ordinalTab(o) {} 485 486 size_t getSize() const override { 487 return sizeof(export_directory_table_entry); 488 } 489 490 void writeTo(uint8_t *buf) const override { 491 memset(buf, 0, getSize()); 492 493 auto *e = (export_directory_table_entry *)(buf); 494 e->NameRVA = dllName->getRVA(); 495 e->OrdinalBase = 0; 496 e->AddressTableEntries = maxOrdinal + 1; 497 e->NumberOfNamePointers = nameTabSize; 498 e->ExportAddressTableRVA = addressTab->getRVA(); 499 e->NamePointerRVA = nameTab->getRVA(); 500 e->OrdinalTableRVA = ordinalTab->getRVA(); 501 } 502 503 uint16_t maxOrdinal; 504 uint16_t nameTabSize; 505 Chunk *dllName; 506 Chunk *addressTab; 507 Chunk *nameTab; 508 Chunk *ordinalTab; 509 }; 510 511 class AddressTableChunk : public NonSectionChunk { 512 public: 513 explicit AddressTableChunk(size_t maxOrdinal) : size(maxOrdinal + 1) {} 514 size_t getSize() const override { return size * 4; } 515 516 void writeTo(uint8_t *buf) const override { 517 memset(buf, 0, getSize()); 518 519 for (const Export &e : config->exports) { 520 uint8_t *p = buf + e.ordinal * 4; 521 uint32_t bit = 0; 522 // Pointer to thumb code must have the LSB set, so adjust it. 523 if (config->machine == ARMNT && !e.data) 524 bit = 1; 525 if (e.forwardChunk) { 526 write32le(p, e.forwardChunk->getRVA() | bit); 527 } else { 528 assert(cast<Defined>(e.sym)->getRVA() != 0 && 529 "Exported symbol unmapped"); 530 write32le(p, cast<Defined>(e.sym)->getRVA() | bit); 531 } 532 } 533 } 534 535 private: 536 size_t size; 537 }; 538 539 class NamePointersChunk : public NonSectionChunk { 540 public: 541 explicit NamePointersChunk(std::vector<Chunk *> &v) : chunks(v) {} 542 size_t getSize() const override { return chunks.size() * 4; } 543 544 void writeTo(uint8_t *buf) const override { 545 for (Chunk *c : chunks) { 546 write32le(buf, c->getRVA()); 547 buf += 4; 548 } 549 } 550 551 private: 552 std::vector<Chunk *> chunks; 553 }; 554 555 class ExportOrdinalChunk : public NonSectionChunk { 556 public: 557 explicit ExportOrdinalChunk(size_t i) : size(i) {} 558 size_t getSize() const override { return size * 2; } 559 560 void writeTo(uint8_t *buf) const override { 561 for (Export &e : config->exports) { 562 if (e.noname) 563 continue; 564 write16le(buf, e.ordinal); 565 buf += 2; 566 } 567 } 568 569 private: 570 size_t size; 571 }; 572 573 } // anonymous namespace 574 575 void IdataContents::create() { 576 std::vector<std::vector<DefinedImportData *>> v = binImports(imports); 577 578 // Create .idata contents for each DLL. 579 for (std::vector<DefinedImportData *> &syms : v) { 580 // Create lookup and address tables. If they have external names, 581 // we need to create hintName chunks to store the names. 582 // If they don't (if they are import-by-ordinals), we store only 583 // ordinal values to the table. 584 size_t base = lookups.size(); 585 for (DefinedImportData *s : syms) { 586 uint16_t ord = s->getOrdinal(); 587 if (s->getExternalName().empty()) { 588 lookups.push_back(make<OrdinalOnlyChunk>(ord)); 589 addresses.push_back(make<OrdinalOnlyChunk>(ord)); 590 continue; 591 } 592 auto *c = make<HintNameChunk>(s->getExternalName(), ord); 593 lookups.push_back(make<LookupChunk>(c)); 594 addresses.push_back(make<LookupChunk>(c)); 595 hints.push_back(c); 596 } 597 // Terminate with null values. 598 lookups.push_back(make<NullChunk>(config->wordsize)); 599 addresses.push_back(make<NullChunk>(config->wordsize)); 600 601 for (int i = 0, e = syms.size(); i < e; ++i) 602 syms[i]->setLocation(addresses[base + i]); 603 604 // Create the import table header. 605 dllNames.push_back(make<StringChunk>(syms[0]->getDLLName())); 606 auto *dir = make<ImportDirectoryChunk>(dllNames.back()); 607 dir->lookupTab = lookups[base]; 608 dir->addressTab = addresses[base]; 609 dirs.push_back(dir); 610 } 611 // Add null terminator. 612 dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry))); 613 } 614 615 std::vector<Chunk *> DelayLoadContents::getChunks() { 616 std::vector<Chunk *> v; 617 v.insert(v.end(), dirs.begin(), dirs.end()); 618 v.insert(v.end(), names.begin(), names.end()); 619 v.insert(v.end(), hintNames.begin(), hintNames.end()); 620 v.insert(v.end(), dllNames.begin(), dllNames.end()); 621 return v; 622 } 623 624 std::vector<Chunk *> DelayLoadContents::getDataChunks() { 625 std::vector<Chunk *> v; 626 v.insert(v.end(), moduleHandles.begin(), moduleHandles.end()); 627 v.insert(v.end(), addresses.begin(), addresses.end()); 628 return v; 629 } 630 631 uint64_t DelayLoadContents::getDirSize() { 632 return dirs.size() * sizeof(delay_import_directory_table_entry); 633 } 634 635 void DelayLoadContents::create(COFFLinkerContext &ctx, Defined *h) { 636 helper = h; 637 std::vector<std::vector<DefinedImportData *>> v = binImports(imports); 638 639 // Create .didat contents for each DLL. 640 for (std::vector<DefinedImportData *> &syms : v) { 641 // Create the delay import table header. 642 dllNames.push_back(make<StringChunk>(syms[0]->getDLLName())); 643 auto *dir = make<DelayDirectoryChunk>(dllNames.back()); 644 645 size_t base = addresses.size(); 646 Chunk *tm = newTailMergeChunk(dir); 647 for (DefinedImportData *s : syms) { 648 Chunk *t = newThunkChunk(s, tm); 649 auto *a = make<DelayAddressChunk>(t); 650 addresses.push_back(a); 651 thunks.push_back(t); 652 StringRef extName = s->getExternalName(); 653 if (extName.empty()) { 654 names.push_back(make<OrdinalOnlyChunk>(s->getOrdinal())); 655 } else { 656 auto *c = make<HintNameChunk>(extName, 0); 657 names.push_back(make<LookupChunk>(c)); 658 hintNames.push_back(c); 659 // Add a syntentic symbol for this load thunk, using the "__imp_load" 660 // prefix, in case this thunk needs to be added to the list of valid 661 // call targets for Control Flow Guard. 662 StringRef symName = saver().save("__imp_load_" + extName); 663 s->loadThunkSym = 664 cast<DefinedSynthetic>(ctx.symtab.addSynthetic(symName, t)); 665 } 666 } 667 thunks.push_back(tm); 668 StringRef tmName = 669 saver().save("__tailMerge_" + syms[0]->getDLLName().lower()); 670 ctx.symtab.addSynthetic(tmName, tm); 671 // Terminate with null values. 672 addresses.push_back(make<NullChunk>(8)); 673 names.push_back(make<NullChunk>(8)); 674 675 for (int i = 0, e = syms.size(); i < e; ++i) 676 syms[i]->setLocation(addresses[base + i]); 677 auto *mh = make<NullChunk>(8); 678 mh->setAlignment(8); 679 moduleHandles.push_back(mh); 680 681 // Fill the delay import table header fields. 682 dir->moduleHandle = mh; 683 dir->addressTab = addresses[base]; 684 dir->nameTab = names[base]; 685 dirs.push_back(dir); 686 } 687 // Add null terminator. 688 dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry))); 689 } 690 691 Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) { 692 switch (config->machine) { 693 case AMD64: 694 return make<TailMergeChunkX64>(dir, helper); 695 case I386: 696 return make<TailMergeChunkX86>(dir, helper); 697 case ARMNT: 698 return make<TailMergeChunkARM>(dir, helper); 699 case ARM64: 700 return make<TailMergeChunkARM64>(dir, helper); 701 default: 702 llvm_unreachable("unsupported machine type"); 703 } 704 } 705 706 Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s, 707 Chunk *tailMerge) { 708 switch (config->machine) { 709 case AMD64: 710 return make<ThunkChunkX64>(s, tailMerge); 711 case I386: 712 return make<ThunkChunkX86>(s, tailMerge); 713 case ARMNT: 714 return make<ThunkChunkARM>(s, tailMerge); 715 case ARM64: 716 return make<ThunkChunkARM64>(s, tailMerge); 717 default: 718 llvm_unreachable("unsupported machine type"); 719 } 720 } 721 722 EdataContents::EdataContents() { 723 uint16_t maxOrdinal = 0; 724 for (Export &e : config->exports) 725 maxOrdinal = std::max(maxOrdinal, e.ordinal); 726 727 auto *dllName = make<StringChunk>(sys::path::filename(config->outputFile)); 728 auto *addressTab = make<AddressTableChunk>(maxOrdinal); 729 std::vector<Chunk *> names; 730 for (Export &e : config->exports) 731 if (!e.noname) 732 names.push_back(make<StringChunk>(e.exportName)); 733 734 std::vector<Chunk *> forwards; 735 for (Export &e : config->exports) { 736 if (e.forwardTo.empty()) 737 continue; 738 e.forwardChunk = make<StringChunk>(e.forwardTo); 739 forwards.push_back(e.forwardChunk); 740 } 741 742 auto *nameTab = make<NamePointersChunk>(names); 743 auto *ordinalTab = make<ExportOrdinalChunk>(names.size()); 744 auto *dir = make<ExportDirectoryChunk>(maxOrdinal, names.size(), dllName, 745 addressTab, nameTab, ordinalTab); 746 chunks.push_back(dir); 747 chunks.push_back(dllName); 748 chunks.push_back(addressTab); 749 chunks.push_back(nameTab); 750 chunks.push_back(ordinalTab); 751 chunks.insert(chunks.end(), names.begin(), names.end()); 752 chunks.insert(chunks.end(), forwards.begin(), forwards.end()); 753 } 754 755 } // namespace coff 756 } // namespace lld 757