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