1 //===- Writer.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 10 #include "Config.h" 11 #include "DLL.h" 12 #include "Error.h" 13 #include "InputFiles.h" 14 #include "SymbolTable.h" 15 #include "Symbols.h" 16 #include "Writer.h" 17 #include "lld/Core/Parallel.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Support/Endian.h" 23 #include "llvm/Support/FileOutputBuffer.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include <algorithm> 26 #include <cstdio> 27 #include <map> 28 #include <memory> 29 #include <utility> 30 31 using namespace llvm; 32 using namespace llvm::COFF; 33 using namespace llvm::object; 34 using namespace llvm::support; 35 using namespace llvm::support::endian; 36 using namespace lld; 37 using namespace lld::coff; 38 39 static const int PageSize = 4096; 40 static const int SectorSize = 512; 41 static const int DOSStubSize = 64; 42 static const int NumberfOfDataDirectory = 16; 43 44 namespace { 45 // The writer writes a SymbolTable result to a file. 46 class Writer { 47 public: 48 Writer(SymbolTable *T) : Symtab(T) {} 49 void run(); 50 51 private: 52 void createSections(); 53 void createMiscChunks(); 54 void createImportTables(); 55 void createExportTable(); 56 void assignAddresses(); 57 void removeEmptySections(); 58 void createSymbolAndStringTable(); 59 void openFile(StringRef OutputPath); 60 template <typename PEHeaderTy> void writeHeader(); 61 void fixSafeSEHSymbols(); 62 void writeSections(); 63 void sortExceptionTable(); 64 void applyRelocations(); 65 66 llvm::Optional<coff_symbol16> createSymbol(Defined *D); 67 size_t addEntryToStringTable(StringRef Str); 68 69 OutputSection *findSection(StringRef Name); 70 OutputSection *createSection(StringRef Name); 71 void addBaserels(OutputSection *Dest); 72 void addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V); 73 74 uint32_t getSizeOfInitializedData(); 75 std::map<StringRef, std::vector<DefinedImportData *>> binImports(); 76 77 SymbolTable *Symtab; 78 std::unique_ptr<llvm::FileOutputBuffer> Buffer; 79 llvm::SpecificBumpPtrAllocator<OutputSection> CAlloc; 80 llvm::SpecificBumpPtrAllocator<BaserelChunk> BAlloc; 81 std::vector<OutputSection *> OutputSections; 82 std::vector<char> Strtab; 83 std::vector<llvm::object::coff_symbol16> OutputSymtab; 84 IdataContents Idata; 85 DelayLoadContents DelayIdata; 86 EdataContents Edata; 87 std::unique_ptr<SEHTableChunk> SEHTable; 88 89 uint64_t FileSize; 90 uint32_t PointerToSymbolTable = 0; 91 uint64_t SizeOfImage; 92 uint64_t SizeOfHeaders; 93 94 std::vector<std::unique_ptr<Chunk>> Chunks; 95 }; 96 } // anonymous namespace 97 98 namespace lld { 99 namespace coff { 100 101 void writeResult(SymbolTable *T) { Writer(T).run(); } 102 103 // OutputSection represents a section in an output file. It's a 104 // container of chunks. OutputSection and Chunk are 1:N relationship. 105 // Chunks cannot belong to more than one OutputSections. The writer 106 // creates multiple OutputSections and assign them unique, 107 // non-overlapping file offsets and RVAs. 108 class OutputSection { 109 public: 110 OutputSection(StringRef N) : Name(N), Header({}) {} 111 void setRVA(uint64_t); 112 void setFileOffset(uint64_t); 113 void addChunk(Chunk *C); 114 StringRef getName() { return Name; } 115 std::vector<Chunk *> &getChunks() { return Chunks; } 116 void addPermissions(uint32_t C); 117 uint32_t getPermissions() { return Header.Characteristics & PermMask; } 118 uint32_t getCharacteristics() { return Header.Characteristics; } 119 uint64_t getRVA() { return Header.VirtualAddress; } 120 uint64_t getFileOff() { return Header.PointerToRawData; } 121 void writeHeaderTo(uint8_t *Buf); 122 123 // Returns the size of this section in an executable memory image. 124 // This may be smaller than the raw size (the raw size is multiple 125 // of disk sector size, so there may be padding at end), or may be 126 // larger (if that's the case, the loader reserves spaces after end 127 // of raw data). 128 uint64_t getVirtualSize() { return Header.VirtualSize; } 129 130 // Returns the size of the section in the output file. 131 uint64_t getRawSize() { return Header.SizeOfRawData; } 132 133 // Set offset into the string table storing this section name. 134 // Used only when the name is longer than 8 bytes. 135 void setStringTableOff(uint32_t V) { StringTableOff = V; } 136 137 // N.B. The section index is one based. 138 uint32_t SectionIndex = 0; 139 140 private: 141 StringRef Name; 142 coff_section Header; 143 uint32_t StringTableOff = 0; 144 std::vector<Chunk *> Chunks; 145 }; 146 147 void OutputSection::setRVA(uint64_t RVA) { 148 Header.VirtualAddress = RVA; 149 for (Chunk *C : Chunks) 150 C->setRVA(C->getRVA() + RVA); 151 } 152 153 void OutputSection::setFileOffset(uint64_t Off) { 154 // If a section has no actual data (i.e. BSS section), we want to 155 // set 0 to its PointerToRawData. Otherwise the output is rejected 156 // by the loader. 157 if (Header.SizeOfRawData == 0) 158 return; 159 Header.PointerToRawData = Off; 160 } 161 162 void OutputSection::addChunk(Chunk *C) { 163 Chunks.push_back(C); 164 C->setOutputSection(this); 165 uint64_t Off = Header.VirtualSize; 166 Off = RoundUpToAlignment(Off, C->getAlign()); 167 C->setRVA(Off); 168 C->setOutputSectionOff(Off); 169 Off += C->getSize(); 170 Header.VirtualSize = Off; 171 if (C->hasData()) 172 Header.SizeOfRawData = RoundUpToAlignment(Off, SectorSize); 173 } 174 175 void OutputSection::addPermissions(uint32_t C) { 176 Header.Characteristics |= C & PermMask; 177 } 178 179 // Write the section header to a given buffer. 180 void OutputSection::writeHeaderTo(uint8_t *Buf) { 181 auto *Hdr = reinterpret_cast<coff_section *>(Buf); 182 *Hdr = Header; 183 if (StringTableOff) { 184 // If name is too long, write offset into the string table as a name. 185 sprintf(Hdr->Name, "/%d", StringTableOff); 186 } else { 187 assert(!Config->Debug || Name.size() <= COFF::NameSize); 188 strncpy(Hdr->Name, Name.data(), 189 std::min(Name.size(), (size_t)COFF::NameSize)); 190 } 191 } 192 193 uint64_t Defined::getSecrel() { 194 if (auto *D = dyn_cast<DefinedRegular>(this)) 195 return getRVA() - D->getChunk()->getOutputSection()->getRVA(); 196 error("SECREL relocation points to a non-regular symbol"); 197 } 198 199 uint64_t Defined::getSectionIndex() { 200 if (auto *D = dyn_cast<DefinedRegular>(this)) 201 return D->getChunk()->getOutputSection()->SectionIndex; 202 error("SECTION relocation points to a non-regular symbol"); 203 } 204 205 bool Defined::isExecutable() { 206 const auto X = IMAGE_SCN_MEM_EXECUTE; 207 if (auto *D = dyn_cast<DefinedRegular>(this)) 208 return D->getChunk()->getOutputSection()->getPermissions() & X; 209 return isa<DefinedImportThunk>(this); 210 } 211 212 } // namespace coff 213 } // namespace lld 214 215 // The main function of the writer. 216 void Writer::run() { 217 createSections(); 218 createMiscChunks(); 219 createImportTables(); 220 createExportTable(); 221 if (Config->Relocatable) 222 createSection(".reloc"); 223 assignAddresses(); 224 removeEmptySections(); 225 createSymbolAndStringTable(); 226 openFile(Config->OutputFile); 227 if (Config->is64()) { 228 writeHeader<pe32plus_header>(); 229 } else { 230 writeHeader<pe32_header>(); 231 } 232 fixSafeSEHSymbols(); 233 writeSections(); 234 sortExceptionTable(); 235 error(Buffer->commit(), "Failed to write the output file"); 236 } 237 238 static StringRef getOutputSection(StringRef Name) { 239 StringRef S = Name.split('$').first; 240 auto It = Config->Merge.find(S); 241 if (It == Config->Merge.end()) 242 return S; 243 return It->second; 244 } 245 246 // Create output section objects and add them to OutputSections. 247 void Writer::createSections() { 248 // First, bin chunks by name. 249 std::map<StringRef, std::vector<Chunk *>> Map; 250 for (Chunk *C : Symtab->getChunks()) { 251 auto *SC = dyn_cast<SectionChunk>(C); 252 if (SC && !SC->isLive()) { 253 if (Config->Verbose) 254 SC->printDiscardedMessage(); 255 continue; 256 } 257 Map[C->getSectionName()].push_back(C); 258 } 259 260 // Then create an OutputSection for each section. 261 // '$' and all following characters in input section names are 262 // discarded when determining output section. So, .text$foo 263 // contributes to .text, for example. See PE/COFF spec 3.2. 264 SmallDenseMap<StringRef, OutputSection *> Sections; 265 for (auto Pair : Map) { 266 StringRef Name = getOutputSection(Pair.first); 267 OutputSection *&Sec = Sections[Name]; 268 if (!Sec) { 269 Sec = new (CAlloc.Allocate()) OutputSection(Name); 270 OutputSections.push_back(Sec); 271 } 272 std::vector<Chunk *> &Chunks = Pair.second; 273 for (Chunk *C : Chunks) { 274 Sec->addChunk(C); 275 Sec->addPermissions(C->getPermissions()); 276 } 277 } 278 } 279 280 void Writer::createMiscChunks() { 281 // Create thunks for locally-dllimported symbols. 282 if (!Symtab->LocalImportChunks.empty()) { 283 OutputSection *Sec = createSection(".rdata"); 284 for (Chunk *C : Symtab->LocalImportChunks) 285 Sec->addChunk(C); 286 } 287 288 // Create SEH table. x86-only. 289 if (Config->Machine != I386) 290 return; 291 std::set<Defined *> Handlers; 292 for (lld::coff::ObjectFile *File : Symtab->ObjectFiles) { 293 if (!File->SEHCompat) 294 return; 295 for (SymbolBody *B : File->SEHandlers) 296 Handlers.insert(cast<Defined>(B->repl())); 297 } 298 SEHTable.reset(new SEHTableChunk(Handlers)); 299 createSection(".rdata")->addChunk(SEHTable.get()); 300 } 301 302 // Create .idata section for the DLL-imported symbol table. 303 // The format of this section is inherently Windows-specific. 304 // IdataContents class abstracted away the details for us, 305 // so we just let it create chunks and add them to the section. 306 void Writer::createImportTables() { 307 if (Symtab->ImportFiles.empty()) 308 return; 309 310 // Initialize DLLOrder so that import entries are ordered in 311 // the same order as in the command line. (That affects DLL 312 // initialization order, and this ordering is MSVC-compatible.) 313 for (ImportFile *File : Symtab->ImportFiles) { 314 std::string DLL = StringRef(File->DLLName).lower(); 315 if (Config->DLLOrder.count(DLL) == 0) 316 Config->DLLOrder[DLL] = Config->DLLOrder.size(); 317 } 318 319 OutputSection *Text = createSection(".text"); 320 for (ImportFile *File : Symtab->ImportFiles) { 321 if (DefinedImportThunk *Thunk = File->ThunkSym) 322 Text->addChunk(Thunk->getChunk()); 323 if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) { 324 DelayIdata.add(File->ImpSym); 325 } else { 326 Idata.add(File->ImpSym); 327 } 328 } 329 if (!Idata.empty()) { 330 OutputSection *Sec = createSection(".idata"); 331 for (Chunk *C : Idata.getChunks()) 332 Sec->addChunk(C); 333 } 334 if (!DelayIdata.empty()) { 335 Defined *Helper = cast<Defined>(Config->DelayLoadHelper->repl()); 336 DelayIdata.create(Helper); 337 OutputSection *Sec = createSection(".didat"); 338 for (Chunk *C : DelayIdata.getChunks()) 339 Sec->addChunk(C); 340 Sec = createSection(".data"); 341 for (Chunk *C : DelayIdata.getDataChunks()) 342 Sec->addChunk(C); 343 Sec = createSection(".text"); 344 for (std::unique_ptr<Chunk> &C : DelayIdata.getCodeChunks()) 345 Sec->addChunk(C.get()); 346 } 347 } 348 349 void Writer::createExportTable() { 350 if (Config->Exports.empty()) 351 return; 352 OutputSection *Sec = createSection(".edata"); 353 for (std::unique_ptr<Chunk> &C : Edata.Chunks) 354 Sec->addChunk(C.get()); 355 } 356 357 // The Windows loader doesn't seem to like empty sections, 358 // so we remove them if any. 359 void Writer::removeEmptySections() { 360 auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; }; 361 OutputSections.erase( 362 std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty), 363 OutputSections.end()); 364 uint32_t Idx = 1; 365 for (OutputSection *Sec : OutputSections) 366 Sec->SectionIndex = Idx++; 367 } 368 369 size_t Writer::addEntryToStringTable(StringRef Str) { 370 assert(Str.size() > COFF::NameSize); 371 size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field 372 Strtab.insert(Strtab.end(), Str.begin(), Str.end()); 373 Strtab.push_back('\0'); 374 return OffsetOfEntry; 375 } 376 377 Optional<coff_symbol16> Writer::createSymbol(Defined *Def) { 378 if (auto *D = dyn_cast<DefinedRegular>(Def)) 379 if (!D->getChunk()->isLive()) 380 return None; 381 382 coff_symbol16 Sym; 383 StringRef Name = Def->getName(); 384 if (Name.size() > COFF::NameSize) { 385 Sym.Name.Offset.Zeroes = 0; 386 Sym.Name.Offset.Offset = addEntryToStringTable(Name); 387 } else { 388 memset(Sym.Name.ShortName, 0, COFF::NameSize); 389 memcpy(Sym.Name.ShortName, Name.data(), Name.size()); 390 } 391 392 if (auto *D = dyn_cast<DefinedCOFF>(Def)) { 393 COFFSymbolRef Ref = D->getCOFFSymbol(); 394 Sym.Type = Ref.getType(); 395 Sym.StorageClass = Ref.getStorageClass(); 396 } else { 397 Sym.Type = IMAGE_SYM_TYPE_NULL; 398 Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL; 399 } 400 Sym.NumberOfAuxSymbols = 0; 401 402 switch (Def->kind()) { 403 case SymbolBody::DefinedAbsoluteKind: 404 case SymbolBody::DefinedRelativeKind: 405 Sym.Value = Def->getRVA(); 406 Sym.SectionNumber = IMAGE_SYM_ABSOLUTE; 407 break; 408 default: { 409 uint64_t RVA = Def->getRVA(); 410 OutputSection *Sec = nullptr; 411 for (OutputSection *S : OutputSections) { 412 if (S->getRVA() > RVA) 413 break; 414 Sec = S; 415 } 416 Sym.Value = RVA - Sec->getRVA(); 417 Sym.SectionNumber = Sec->SectionIndex; 418 break; 419 } 420 } 421 return Sym; 422 } 423 424 void Writer::createSymbolAndStringTable() { 425 if (!Config->Debug || !Config->WriteSymtab) 426 return; 427 428 // Name field in the section table is 8 byte long. Longer names need 429 // to be written to the string table. First, construct string table. 430 for (OutputSection *Sec : OutputSections) { 431 StringRef Name = Sec->getName(); 432 if (Name.size() <= COFF::NameSize) 433 continue; 434 Sec->setStringTableOff(addEntryToStringTable(Name)); 435 } 436 437 for (lld::coff::ObjectFile *File : Symtab->ObjectFiles) 438 for (SymbolBody *B : File->getSymbols()) 439 if (auto *D = dyn_cast<Defined>(B)) 440 if (Optional<coff_symbol16> Sym = createSymbol(D)) 441 OutputSymtab.push_back(*Sym); 442 443 for (ImportFile *File : Symtab->ImportFiles) 444 for (SymbolBody *B : File->getSymbols()) 445 if (Optional<coff_symbol16> Sym = createSymbol(cast<Defined>(B))) 446 OutputSymtab.push_back(*Sym); 447 448 OutputSection *LastSection = OutputSections.back(); 449 // We position the symbol table to be adjacent to the end of the last section. 450 uint64_t FileOff = 451 LastSection->getFileOff() + 452 RoundUpToAlignment(LastSection->getRawSize(), SectorSize); 453 if (!OutputSymtab.empty()) { 454 PointerToSymbolTable = FileOff; 455 FileOff += OutputSymtab.size() * sizeof(coff_symbol16); 456 } 457 if (!Strtab.empty()) 458 FileOff += Strtab.size() + 4; 459 FileSize = RoundUpToAlignment(FileOff, SectorSize); 460 } 461 462 // Visits all sections to assign incremental, non-overlapping RVAs and 463 // file offsets. 464 void Writer::assignAddresses() { 465 SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) + 466 sizeof(data_directory) * NumberfOfDataDirectory + 467 sizeof(coff_section) * OutputSections.size(); 468 SizeOfHeaders += 469 Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header); 470 SizeOfHeaders = RoundUpToAlignment(SizeOfHeaders, SectorSize); 471 uint64_t RVA = 0x1000; // The first page is kept unmapped. 472 FileSize = SizeOfHeaders; 473 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because 474 // the loader cannot handle holes. 475 std::stable_partition( 476 OutputSections.begin(), OutputSections.end(), [](OutputSection *S) { 477 return (S->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0; 478 }); 479 for (OutputSection *Sec : OutputSections) { 480 if (Sec->getName() == ".reloc") 481 addBaserels(Sec); 482 Sec->setRVA(RVA); 483 Sec->setFileOffset(FileSize); 484 RVA += RoundUpToAlignment(Sec->getVirtualSize(), PageSize); 485 FileSize += RoundUpToAlignment(Sec->getRawSize(), SectorSize); 486 } 487 SizeOfImage = SizeOfHeaders + RoundUpToAlignment(RVA - 0x1000, PageSize); 488 } 489 490 template <typename PEHeaderTy> void Writer::writeHeader() { 491 // Write DOS stub 492 uint8_t *Buf = Buffer->getBufferStart(); 493 auto *DOS = reinterpret_cast<dos_header *>(Buf); 494 Buf += DOSStubSize; 495 DOS->Magic[0] = 'M'; 496 DOS->Magic[1] = 'Z'; 497 DOS->AddressOfRelocationTable = sizeof(dos_header); 498 DOS->AddressOfNewExeHeader = DOSStubSize; 499 500 // Write PE magic 501 memcpy(Buf, PEMagic, sizeof(PEMagic)); 502 Buf += sizeof(PEMagic); 503 504 // Write COFF header 505 auto *COFF = reinterpret_cast<coff_file_header *>(Buf); 506 Buf += sizeof(*COFF); 507 COFF->Machine = Config->Machine; 508 COFF->NumberOfSections = OutputSections.size(); 509 COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE; 510 if (Config->LargeAddressAware) 511 COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE; 512 if (!Config->is64()) 513 COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE; 514 if (Config->DLL) 515 COFF->Characteristics |= IMAGE_FILE_DLL; 516 if (!Config->Relocatable) 517 COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; 518 COFF->SizeOfOptionalHeader = 519 sizeof(PEHeaderTy) + sizeof(data_directory) * NumberfOfDataDirectory; 520 521 // Write PE header 522 auto *PE = reinterpret_cast<PEHeaderTy *>(Buf); 523 Buf += sizeof(*PE); 524 PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32; 525 PE->ImageBase = Config->ImageBase; 526 PE->SectionAlignment = PageSize; 527 PE->FileAlignment = SectorSize; 528 PE->MajorImageVersion = Config->MajorImageVersion; 529 PE->MinorImageVersion = Config->MinorImageVersion; 530 PE->MajorOperatingSystemVersion = Config->MajorOSVersion; 531 PE->MinorOperatingSystemVersion = Config->MinorOSVersion; 532 PE->MajorSubsystemVersion = Config->MajorOSVersion; 533 PE->MinorSubsystemVersion = Config->MinorOSVersion; 534 PE->Subsystem = Config->Subsystem; 535 PE->SizeOfImage = SizeOfImage; 536 PE->SizeOfHeaders = SizeOfHeaders; 537 if (!Config->NoEntry) { 538 Defined *Entry = cast<Defined>(Config->Entry->repl()); 539 PE->AddressOfEntryPoint = Entry->getRVA(); 540 // Pointer to thumb code must have the LSB set, so adjust it. 541 if (Config->Machine == ARMNT) 542 PE->AddressOfEntryPoint |= 1; 543 } 544 PE->SizeOfStackReserve = Config->StackReserve; 545 PE->SizeOfStackCommit = Config->StackCommit; 546 PE->SizeOfHeapReserve = Config->HeapReserve; 547 PE->SizeOfHeapCommit = Config->HeapCommit; 548 if (Config->DynamicBase) 549 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE; 550 if (Config->HighEntropyVA) 551 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA; 552 if (!Config->AllowBind) 553 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND; 554 if (Config->NxCompat) 555 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT; 556 if (!Config->AllowIsolation) 557 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION; 558 if (Config->TerminalServerAware) 559 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE; 560 PE->NumberOfRvaAndSize = NumberfOfDataDirectory; 561 if (OutputSection *Text = findSection(".text")) { 562 PE->BaseOfCode = Text->getRVA(); 563 PE->SizeOfCode = Text->getRawSize(); 564 } 565 PE->SizeOfInitializedData = getSizeOfInitializedData(); 566 567 // Write data directory 568 auto *Dir = reinterpret_cast<data_directory *>(Buf); 569 Buf += sizeof(*Dir) * NumberfOfDataDirectory; 570 if (OutputSection *Sec = findSection(".edata")) { 571 Dir[EXPORT_TABLE].RelativeVirtualAddress = Sec->getRVA(); 572 Dir[EXPORT_TABLE].Size = Sec->getVirtualSize(); 573 } 574 if (!Idata.empty()) { 575 Dir[IMPORT_TABLE].RelativeVirtualAddress = Idata.getDirRVA(); 576 Dir[IMPORT_TABLE].Size = Idata.getDirSize(); 577 Dir[IAT].RelativeVirtualAddress = Idata.getIATRVA(); 578 Dir[IAT].Size = Idata.getIATSize(); 579 } 580 if (!DelayIdata.empty()) { 581 Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress = 582 DelayIdata.getDirRVA(); 583 Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize(); 584 } 585 if (OutputSection *Sec = findSection(".rsrc")) { 586 Dir[RESOURCE_TABLE].RelativeVirtualAddress = Sec->getRVA(); 587 Dir[RESOURCE_TABLE].Size = Sec->getVirtualSize(); 588 } 589 if (OutputSection *Sec = findSection(".reloc")) { 590 Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = Sec->getRVA(); 591 Dir[BASE_RELOCATION_TABLE].Size = Sec->getVirtualSize(); 592 } 593 if (OutputSection *Sec = findSection(".pdata")) { 594 Dir[EXCEPTION_TABLE].RelativeVirtualAddress = Sec->getRVA(); 595 Dir[EXCEPTION_TABLE].Size = Sec->getVirtualSize(); 596 } 597 if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) { 598 if (Defined *B = dyn_cast<Defined>(Sym->Body)) { 599 Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA(); 600 Dir[TLS_TABLE].Size = 40; 601 } 602 } 603 if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) { 604 if (Defined *B = dyn_cast<Defined>(Sym->Body)) { 605 Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA(); 606 Dir[LOAD_CONFIG_TABLE].Size = Config->is64() ? 112 : 64; 607 } 608 } 609 610 // Write section table 611 for (OutputSection *Sec : OutputSections) { 612 Sec->writeHeaderTo(Buf); 613 Buf += sizeof(coff_section); 614 } 615 616 if (OutputSymtab.empty()) 617 return; 618 619 COFF->PointerToSymbolTable = PointerToSymbolTable; 620 uint32_t NumberOfSymbols = OutputSymtab.size(); 621 COFF->NumberOfSymbols = NumberOfSymbols; 622 auto *SymbolTable = reinterpret_cast<coff_symbol16 *>( 623 Buffer->getBufferStart() + COFF->PointerToSymbolTable); 624 for (size_t I = 0; I != NumberOfSymbols; ++I) 625 SymbolTable[I] = OutputSymtab[I]; 626 // Create the string table, it follows immediately after the symbol table. 627 // The first 4 bytes is length including itself. 628 Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]); 629 write32le(Buf, Strtab.size() + 4); 630 memcpy(Buf + 4, Strtab.data(), Strtab.size()); 631 } 632 633 void Writer::openFile(StringRef Path) { 634 ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 635 FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable); 636 error(BufferOrErr, Twine("failed to open ") + Path); 637 Buffer = std::move(*BufferOrErr); 638 } 639 640 void Writer::fixSafeSEHSymbols() { 641 if (!SEHTable) 642 return; 643 Config->SEHTable->setRVA(SEHTable->getRVA()); 644 Config->SEHCount->setVA(SEHTable->getSize() / 4); 645 } 646 647 // Write section contents to a mmap'ed file. 648 void Writer::writeSections() { 649 uint8_t *Buf = Buffer->getBufferStart(); 650 for (OutputSection *Sec : OutputSections) { 651 uint8_t *SecBuf = Buf + Sec->getFileOff(); 652 // Fill gaps between functions in .text with INT3 instructions 653 // instead of leaving as NUL bytes (which can be interpreted as 654 // ADD instructions). 655 if (Sec->getPermissions() & IMAGE_SCN_CNT_CODE) 656 memset(SecBuf, 0xCC, Sec->getRawSize()); 657 parallel_for_each(Sec->getChunks().begin(), Sec->getChunks().end(), 658 [&](Chunk *C) { C->writeTo(SecBuf); }); 659 } 660 } 661 662 // Sort .pdata section contents according to PE/COFF spec 5.5. 663 void Writer::sortExceptionTable() { 664 OutputSection *Sec = findSection(".pdata"); 665 if (!Sec) 666 return; 667 // We assume .pdata contains function table entries only. 668 uint8_t *Begin = Buffer->getBufferStart() + Sec->getFileOff(); 669 uint8_t *End = Begin + Sec->getVirtualSize(); 670 if (Config->Machine == AMD64) { 671 struct Entry { ulittle32_t Begin, End, Unwind; }; 672 parallel_sort( 673 (Entry *)Begin, (Entry *)End, 674 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); 675 return; 676 } 677 if (Config->Machine == ARMNT) { 678 struct Entry { ulittle32_t Begin, Unwind; }; 679 parallel_sort( 680 (Entry *)Begin, (Entry *)End, 681 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); 682 return; 683 } 684 errs() << "warning: don't know how to handle .pdata.\n"; 685 } 686 687 OutputSection *Writer::findSection(StringRef Name) { 688 for (OutputSection *Sec : OutputSections) 689 if (Sec->getName() == Name) 690 return Sec; 691 return nullptr; 692 } 693 694 uint32_t Writer::getSizeOfInitializedData() { 695 uint32_t Res = 0; 696 for (OutputSection *S : OutputSections) 697 if (S->getPermissions() & IMAGE_SCN_CNT_INITIALIZED_DATA) 698 Res += S->getRawSize(); 699 return Res; 700 } 701 702 // Returns an existing section or create a new one if not found. 703 OutputSection *Writer::createSection(StringRef Name) { 704 if (auto *Sec = findSection(Name)) 705 return Sec; 706 const auto DATA = IMAGE_SCN_CNT_INITIALIZED_DATA; 707 const auto BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA; 708 const auto CODE = IMAGE_SCN_CNT_CODE; 709 const auto DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE; 710 const auto R = IMAGE_SCN_MEM_READ; 711 const auto W = IMAGE_SCN_MEM_WRITE; 712 const auto X = IMAGE_SCN_MEM_EXECUTE; 713 uint32_t Perms = StringSwitch<uint32_t>(Name) 714 .Case(".bss", BSS | R | W) 715 .Case(".data", DATA | R | W) 716 .Case(".didat", DATA | R) 717 .Case(".edata", DATA | R) 718 .Case(".idata", DATA | R) 719 .Case(".rdata", DATA | R) 720 .Case(".reloc", DATA | DISCARDABLE | R) 721 .Case(".text", CODE | R | X) 722 .Default(0); 723 if (!Perms) 724 llvm_unreachable("unknown section name"); 725 auto Sec = new (CAlloc.Allocate()) OutputSection(Name); 726 Sec->addPermissions(Perms); 727 OutputSections.push_back(Sec); 728 return Sec; 729 } 730 731 // Dest is .reloc section. Add contents to that section. 732 void Writer::addBaserels(OutputSection *Dest) { 733 std::vector<Baserel> V; 734 for (OutputSection *Sec : OutputSections) { 735 if (Sec == Dest) 736 continue; 737 // Collect all locations for base relocations. 738 for (Chunk *C : Sec->getChunks()) 739 C->getBaserels(&V); 740 // Add the addresses to .reloc section. 741 if (!V.empty()) 742 addBaserelBlocks(Dest, V); 743 V.clear(); 744 } 745 } 746 747 // Add addresses to .reloc section. Note that addresses are grouped by page. 748 void Writer::addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V) { 749 const uint32_t Mask = ~uint32_t(PageSize - 1); 750 uint32_t Page = V[0].RVA & Mask; 751 size_t I = 0, J = 1; 752 for (size_t E = V.size(); J < E; ++J) { 753 uint32_t P = V[J].RVA & Mask; 754 if (P == Page) 755 continue; 756 BaserelChunk *Buf = BAlloc.Allocate(); 757 Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J)); 758 I = J; 759 Page = P; 760 } 761 if (I == J) 762 return; 763 BaserelChunk *Buf = BAlloc.Allocate(); 764 Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J)); 765 } 766