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 = alignTo(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 = alignTo(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 = LastSection->getFileOff() + 451 alignTo(LastSection->getRawSize(), SectorSize); 452 if (!OutputSymtab.empty()) { 453 PointerToSymbolTable = FileOff; 454 FileOff += OutputSymtab.size() * sizeof(coff_symbol16); 455 } 456 if (!Strtab.empty()) 457 FileOff += Strtab.size() + 4; 458 FileSize = alignTo(FileOff, SectorSize); 459 } 460 461 // Visits all sections to assign incremental, non-overlapping RVAs and 462 // file offsets. 463 void Writer::assignAddresses() { 464 SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) + 465 sizeof(data_directory) * NumberfOfDataDirectory + 466 sizeof(coff_section) * OutputSections.size(); 467 SizeOfHeaders += 468 Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header); 469 SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize); 470 uint64_t RVA = 0x1000; // The first page is kept unmapped. 471 FileSize = SizeOfHeaders; 472 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because 473 // the loader cannot handle holes. 474 std::stable_partition( 475 OutputSections.begin(), OutputSections.end(), [](OutputSection *S) { 476 return (S->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0; 477 }); 478 for (OutputSection *Sec : OutputSections) { 479 if (Sec->getName() == ".reloc") 480 addBaserels(Sec); 481 Sec->setRVA(RVA); 482 Sec->setFileOffset(FileSize); 483 RVA += alignTo(Sec->getVirtualSize(), PageSize); 484 FileSize += alignTo(Sec->getRawSize(), SectorSize); 485 } 486 SizeOfImage = SizeOfHeaders + alignTo(RVA - 0x1000, PageSize); 487 } 488 489 template <typename PEHeaderTy> void Writer::writeHeader() { 490 // Write DOS stub 491 uint8_t *Buf = Buffer->getBufferStart(); 492 auto *DOS = reinterpret_cast<dos_header *>(Buf); 493 Buf += DOSStubSize; 494 DOS->Magic[0] = 'M'; 495 DOS->Magic[1] = 'Z'; 496 DOS->AddressOfRelocationTable = sizeof(dos_header); 497 DOS->AddressOfNewExeHeader = DOSStubSize; 498 499 // Write PE magic 500 memcpy(Buf, PEMagic, sizeof(PEMagic)); 501 Buf += sizeof(PEMagic); 502 503 // Write COFF header 504 auto *COFF = reinterpret_cast<coff_file_header *>(Buf); 505 Buf += sizeof(*COFF); 506 COFF->Machine = Config->Machine; 507 COFF->NumberOfSections = OutputSections.size(); 508 COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE; 509 if (Config->LargeAddressAware) 510 COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE; 511 if (!Config->is64()) 512 COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE; 513 if (Config->DLL) 514 COFF->Characteristics |= IMAGE_FILE_DLL; 515 if (!Config->Relocatable) 516 COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; 517 COFF->SizeOfOptionalHeader = 518 sizeof(PEHeaderTy) + sizeof(data_directory) * NumberfOfDataDirectory; 519 520 // Write PE header 521 auto *PE = reinterpret_cast<PEHeaderTy *>(Buf); 522 Buf += sizeof(*PE); 523 PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32; 524 PE->ImageBase = Config->ImageBase; 525 PE->SectionAlignment = PageSize; 526 PE->FileAlignment = SectorSize; 527 PE->MajorImageVersion = Config->MajorImageVersion; 528 PE->MinorImageVersion = Config->MinorImageVersion; 529 PE->MajorOperatingSystemVersion = Config->MajorOSVersion; 530 PE->MinorOperatingSystemVersion = Config->MinorOSVersion; 531 PE->MajorSubsystemVersion = Config->MajorOSVersion; 532 PE->MinorSubsystemVersion = Config->MinorOSVersion; 533 PE->Subsystem = Config->Subsystem; 534 PE->SizeOfImage = SizeOfImage; 535 PE->SizeOfHeaders = SizeOfHeaders; 536 if (!Config->NoEntry) { 537 Defined *Entry = cast<Defined>(Config->Entry->repl()); 538 PE->AddressOfEntryPoint = Entry->getRVA(); 539 // Pointer to thumb code must have the LSB set, so adjust it. 540 if (Config->Machine == ARMNT) 541 PE->AddressOfEntryPoint |= 1; 542 } 543 PE->SizeOfStackReserve = Config->StackReserve; 544 PE->SizeOfStackCommit = Config->StackCommit; 545 PE->SizeOfHeapReserve = Config->HeapReserve; 546 PE->SizeOfHeapCommit = Config->HeapCommit; 547 if (Config->DynamicBase) 548 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE; 549 if (Config->HighEntropyVA) 550 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA; 551 if (!Config->AllowBind) 552 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND; 553 if (Config->NxCompat) 554 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT; 555 if (!Config->AllowIsolation) 556 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION; 557 if (Config->TerminalServerAware) 558 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE; 559 PE->NumberOfRvaAndSize = NumberfOfDataDirectory; 560 if (OutputSection *Text = findSection(".text")) { 561 PE->BaseOfCode = Text->getRVA(); 562 PE->SizeOfCode = Text->getRawSize(); 563 } 564 PE->SizeOfInitializedData = getSizeOfInitializedData(); 565 566 // Write data directory 567 auto *Dir = reinterpret_cast<data_directory *>(Buf); 568 Buf += sizeof(*Dir) * NumberfOfDataDirectory; 569 if (OutputSection *Sec = findSection(".edata")) { 570 Dir[EXPORT_TABLE].RelativeVirtualAddress = Sec->getRVA(); 571 Dir[EXPORT_TABLE].Size = Sec->getVirtualSize(); 572 } 573 if (!Idata.empty()) { 574 Dir[IMPORT_TABLE].RelativeVirtualAddress = Idata.getDirRVA(); 575 Dir[IMPORT_TABLE].Size = Idata.getDirSize(); 576 Dir[IAT].RelativeVirtualAddress = Idata.getIATRVA(); 577 Dir[IAT].Size = Idata.getIATSize(); 578 } 579 if (!DelayIdata.empty()) { 580 Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress = 581 DelayIdata.getDirRVA(); 582 Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize(); 583 } 584 if (OutputSection *Sec = findSection(".rsrc")) { 585 Dir[RESOURCE_TABLE].RelativeVirtualAddress = Sec->getRVA(); 586 Dir[RESOURCE_TABLE].Size = Sec->getVirtualSize(); 587 } 588 if (OutputSection *Sec = findSection(".reloc")) { 589 Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = Sec->getRVA(); 590 Dir[BASE_RELOCATION_TABLE].Size = Sec->getVirtualSize(); 591 } 592 if (OutputSection *Sec = findSection(".pdata")) { 593 Dir[EXCEPTION_TABLE].RelativeVirtualAddress = Sec->getRVA(); 594 Dir[EXCEPTION_TABLE].Size = Sec->getVirtualSize(); 595 } 596 if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) { 597 if (Defined *B = dyn_cast<Defined>(Sym->Body)) { 598 Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA(); 599 Dir[TLS_TABLE].Size = Config->is64() 600 ? sizeof(object::coff_tls_directory64) 601 : sizeof(object::coff_tls_directory32); 602 } 603 } 604 if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) { 605 if (auto *B = dyn_cast<DefinedRegular>(Sym->Body)) { 606 SectionChunk *SC = B->getChunk(); 607 assert(B->getRVA() >= SC->getRVA()); 608 uint64_t OffsetInChunk = B->getRVA() - SC->getRVA(); 609 if (!SC->hasData() || OffsetInChunk + 4 > SC->getSize()) 610 error("_load_config_used is malformed"); 611 612 ArrayRef<uint8_t> SecContents = SC->getContents(); 613 uint32_t LoadConfigSize = 614 *reinterpret_cast<const ulittle32_t *>(&SecContents[OffsetInChunk]); 615 if (OffsetInChunk + LoadConfigSize > SC->getSize()) 616 error("_load_config_used is too large"); 617 Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA(); 618 Dir[LOAD_CONFIG_TABLE].Size = LoadConfigSize; 619 } 620 } 621 622 // Write section table 623 for (OutputSection *Sec : OutputSections) { 624 Sec->writeHeaderTo(Buf); 625 Buf += sizeof(coff_section); 626 } 627 628 if (OutputSymtab.empty()) 629 return; 630 631 COFF->PointerToSymbolTable = PointerToSymbolTable; 632 uint32_t NumberOfSymbols = OutputSymtab.size(); 633 COFF->NumberOfSymbols = NumberOfSymbols; 634 auto *SymbolTable = reinterpret_cast<coff_symbol16 *>( 635 Buffer->getBufferStart() + COFF->PointerToSymbolTable); 636 for (size_t I = 0; I != NumberOfSymbols; ++I) 637 SymbolTable[I] = OutputSymtab[I]; 638 // Create the string table, it follows immediately after the symbol table. 639 // The first 4 bytes is length including itself. 640 Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]); 641 write32le(Buf, Strtab.size() + 4); 642 if (!Strtab.empty()) 643 memcpy(Buf + 4, Strtab.data(), Strtab.size()); 644 } 645 646 void Writer::openFile(StringRef Path) { 647 ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 648 FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable); 649 error(BufferOrErr, Twine("failed to open ") + Path); 650 Buffer = std::move(*BufferOrErr); 651 } 652 653 void Writer::fixSafeSEHSymbols() { 654 if (!SEHTable) 655 return; 656 Config->SEHTable->setRVA(SEHTable->getRVA()); 657 Config->SEHCount->setVA(SEHTable->getSize() / 4); 658 } 659 660 // Write section contents to a mmap'ed file. 661 void Writer::writeSections() { 662 uint8_t *Buf = Buffer->getBufferStart(); 663 for (OutputSection *Sec : OutputSections) { 664 uint8_t *SecBuf = Buf + Sec->getFileOff(); 665 // Fill gaps between functions in .text with INT3 instructions 666 // instead of leaving as NUL bytes (which can be interpreted as 667 // ADD instructions). 668 if (Sec->getPermissions() & IMAGE_SCN_CNT_CODE) 669 memset(SecBuf, 0xCC, Sec->getRawSize()); 670 parallel_for_each(Sec->getChunks().begin(), Sec->getChunks().end(), 671 [&](Chunk *C) { C->writeTo(SecBuf); }); 672 } 673 } 674 675 // Sort .pdata section contents according to PE/COFF spec 5.5. 676 void Writer::sortExceptionTable() { 677 OutputSection *Sec = findSection(".pdata"); 678 if (!Sec) 679 return; 680 // We assume .pdata contains function table entries only. 681 uint8_t *Begin = Buffer->getBufferStart() + Sec->getFileOff(); 682 uint8_t *End = Begin + Sec->getVirtualSize(); 683 if (Config->Machine == AMD64) { 684 struct Entry { ulittle32_t Begin, End, Unwind; }; 685 parallel_sort( 686 (Entry *)Begin, (Entry *)End, 687 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); 688 return; 689 } 690 if (Config->Machine == ARMNT) { 691 struct Entry { ulittle32_t Begin, Unwind; }; 692 parallel_sort( 693 (Entry *)Begin, (Entry *)End, 694 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); 695 return; 696 } 697 errs() << "warning: don't know how to handle .pdata.\n"; 698 } 699 700 OutputSection *Writer::findSection(StringRef Name) { 701 for (OutputSection *Sec : OutputSections) 702 if (Sec->getName() == Name) 703 return Sec; 704 return nullptr; 705 } 706 707 uint32_t Writer::getSizeOfInitializedData() { 708 uint32_t Res = 0; 709 for (OutputSection *S : OutputSections) 710 if (S->getPermissions() & IMAGE_SCN_CNT_INITIALIZED_DATA) 711 Res += S->getRawSize(); 712 return Res; 713 } 714 715 // Returns an existing section or create a new one if not found. 716 OutputSection *Writer::createSection(StringRef Name) { 717 if (auto *Sec = findSection(Name)) 718 return Sec; 719 const auto DATA = IMAGE_SCN_CNT_INITIALIZED_DATA; 720 const auto BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA; 721 const auto CODE = IMAGE_SCN_CNT_CODE; 722 const auto DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE; 723 const auto R = IMAGE_SCN_MEM_READ; 724 const auto W = IMAGE_SCN_MEM_WRITE; 725 const auto X = IMAGE_SCN_MEM_EXECUTE; 726 uint32_t Perms = StringSwitch<uint32_t>(Name) 727 .Case(".bss", BSS | R | W) 728 .Case(".data", DATA | R | W) 729 .Case(".didat", DATA | R) 730 .Case(".edata", DATA | R) 731 .Case(".idata", DATA | R) 732 .Case(".rdata", DATA | R) 733 .Case(".reloc", DATA | DISCARDABLE | R) 734 .Case(".text", CODE | R | X) 735 .Default(0); 736 if (!Perms) 737 llvm_unreachable("unknown section name"); 738 auto Sec = new (CAlloc.Allocate()) OutputSection(Name); 739 Sec->addPermissions(Perms); 740 OutputSections.push_back(Sec); 741 return Sec; 742 } 743 744 // Dest is .reloc section. Add contents to that section. 745 void Writer::addBaserels(OutputSection *Dest) { 746 std::vector<Baserel> V; 747 for (OutputSection *Sec : OutputSections) { 748 if (Sec == Dest) 749 continue; 750 // Collect all locations for base relocations. 751 for (Chunk *C : Sec->getChunks()) 752 C->getBaserels(&V); 753 // Add the addresses to .reloc section. 754 if (!V.empty()) 755 addBaserelBlocks(Dest, V); 756 V.clear(); 757 } 758 } 759 760 // Add addresses to .reloc section. Note that addresses are grouped by page. 761 void Writer::addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V) { 762 const uint32_t Mask = ~uint32_t(PageSize - 1); 763 uint32_t Page = V[0].RVA & Mask; 764 size_t I = 0, J = 1; 765 for (size_t E = V.size(); J < E; ++J) { 766 uint32_t P = V[J].RVA & Mask; 767 if (P == Page) 768 continue; 769 BaserelChunk *Buf = BAlloc.Allocate(); 770 Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J)); 771 I = J; 772 Page = P; 773 } 774 if (I == J) 775 return; 776 BaserelChunk *Buf = BAlloc.Allocate(); 777 Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J)); 778 } 779