1 //===- Writer.cpp ---------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 #include "InputFiles.h"
14 #include "SymbolTable.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"
32 using namespace llvm::COFF;
33 using namespace llvm::object;
34 using namespace llvm::support;
35 using namespace llvm::support::endian;
37 using namespace lld::coff;
39 static const int PageSize = 4096;
40 static const int SectorSize = 512;
41 static const int DOSStubSize = 64;
42 static const int NumberfOfDataDirectory = 16;
45 // The writer writes a SymbolTable result to a file.
48 Writer(SymbolTable *T) : Symtab(T) {}
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();
64 void sortExceptionTable();
65 void applyRelocations();
67 llvm::Optional<coff_symbol16> createSymbol(Defined *D);
68 size_t addEntryToStringTable(StringRef Str);
70 OutputSection *findSection(StringRef Name);
71 OutputSection *createSection(StringRef Name);
72 void addBaserels(OutputSection *Dest);
73 void addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V);
75 uint32_t getSizeOfInitializedData();
76 std::map<StringRef, std::vector<DefinedImportData *>> binImports();
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;
86 DelayLoadContents DelayIdata;
88 std::unique_ptr<SEHTableChunk> SEHTable;
91 uint32_t PointerToSymbolTable = 0;
93 uint64_t SizeOfHeaders;
95 std::vector<std::unique_ptr<Chunk>> Chunks;
97 } // anonymous namespace
102 void writeResult(SymbolTable *T) { Writer(T).run(); }
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 {
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);
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
130 uint64_t getVirtualSize() { return Header.VirtualSize; }
132 // Returns the size of the section in the output file.
133 uint64_t getRawSize() { return Header.SizeOfRawData; }
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; }
139 // N.B. The section index is one based.
140 uint32_t SectionIndex = 0;
145 uint32_t StringTableOff = 0;
146 std::vector<Chunk *> Chunks;
149 void OutputSection::setRVA(uint64_t RVA) {
150 Header.VirtualAddress = RVA;
151 for (Chunk *C : Chunks)
152 C->setRVA(C->getRVA() + RVA);
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
159 if (Header.SizeOfRawData == 0)
161 Header.PointerToRawData = Off;
164 void OutputSection::addChunk(Chunk *C) {
166 C->setOutputSection(this);
167 uint64_t Off = Header.VirtualSize;
168 Off = alignTo(Off, C->getAlign());
170 C->setOutputSectionOff(Off);
172 Header.VirtualSize = Off;
174 Header.SizeOfRawData = alignTo(Off, SectorSize);
177 void OutputSection::addPermissions(uint32_t C) {
178 Header.Characteristics |= C & PermMask;
181 void OutputSection::setPermissions(uint32_t C) {
182 Header.Characteristics = C & PermMask;
185 // Write the section header to a given buffer.
186 void OutputSection::writeHeaderTo(uint8_t *Buf) {
187 auto *Hdr = reinterpret_cast<coff_section *>(Buf);
189 if (StringTableOff) {
190 // If name is too long, write offset into the string table as a name.
191 sprintf(Hdr->Name, "/%d", StringTableOff);
193 assert(!Config->Debug || Name.size() <= COFF::NameSize);
194 strncpy(Hdr->Name, Name.data(),
195 std::min(Name.size(), (size_t)COFF::NameSize));
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");
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");
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);
221 // The main function of the writer.
225 createImportTables();
227 if (Config->Relocatable)
228 createSection(".reloc");
230 removeEmptySections();
231 setSectionPermissions();
232 createSymbolAndStringTable();
233 openFile(Config->OutputFile);
234 if (Config->is64()) {
235 writeHeader<pe32plus_header>();
237 writeHeader<pe32_header>();
241 sortExceptionTable();
242 if (auto EC = Buffer->commit())
243 fatal(EC, "failed to write the output file");
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())
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()) {
262 SC->printDiscardedMessage();
265 Map[C->getSectionName()].push_back(C);
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];
277 Sec = new (CAlloc.Allocate()) OutputSection(Name);
278 OutputSections.push_back(Sec);
280 std::vector<Chunk *> &Chunks = Pair.second;
281 for (Chunk *C : Chunks) {
283 Sec->addPermissions(C->getPermissions());
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)
296 // Create SEH table. x86-only.
297 if (Config->Machine != I386)
299 std::set<Defined *> Handlers;
300 for (lld::coff::ObjectFile *File : Symtab->ObjectFiles) {
301 if (!File->SEHCompat)
303 for (SymbolBody *B : File->SEHandlers)
304 Handlers.insert(cast<Defined>(B->repl()));
306 SEHTable.reset(new SEHTableChunk(Handlers));
307 createSection(".rdata")->addChunk(SEHTable.get());
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())
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();
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);
334 Idata.add(File->ImpSym);
337 if (!Idata.empty()) {
338 OutputSection *Sec = createSection(".idata");
339 for (Chunk *C : Idata.getChunks())
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())
348 Sec = createSection(".data");
349 for (Chunk *C : DelayIdata.getDataChunks())
351 Sec = createSection(".text");
352 for (std::unique_ptr<Chunk> &C : DelayIdata.getCodeChunks())
353 Sec->addChunk(C.get());
357 void Writer::createExportTable() {
358 if (Config->Exports.empty())
360 OutputSection *Sec = createSection(".edata");
361 for (std::unique_ptr<Chunk> &C : Edata.Chunks)
362 Sec->addChunk(C.get());
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());
373 for (OutputSection *Sec : OutputSections)
374 Sec->SectionIndex = Idx++;
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;
385 Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
386 if (auto *D = dyn_cast<DefinedRegular>(Def))
387 if (!D->getChunk()->isLive())
391 StringRef Name = Def->getName();
392 if (Name.size() > COFF::NameSize) {
393 Sym.Name.Offset.Zeroes = 0;
394 Sym.Name.Offset.Offset = addEntryToStringTable(Name);
396 memset(Sym.Name.ShortName, 0, COFF::NameSize);
397 memcpy(Sym.Name.ShortName, Name.data(), Name.size());
400 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
401 COFFSymbolRef Ref = D->getCOFFSymbol();
402 Sym.Type = Ref.getType();
403 Sym.StorageClass = Ref.getStorageClass();
405 Sym.Type = IMAGE_SYM_TYPE_NULL;
406 Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
408 Sym.NumberOfAuxSymbols = 0;
410 switch (Def->kind()) {
411 case SymbolBody::DefinedAbsoluteKind:
412 case SymbolBody::DefinedRelativeKind:
413 Sym.Value = Def->getRVA();
414 Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
417 uint64_t RVA = Def->getRVA();
418 OutputSection *Sec = nullptr;
419 for (OutputSection *S : OutputSections) {
420 if (S->getRVA() > RVA)
424 Sym.Value = RVA - Sec->getRVA();
425 Sym.SectionNumber = Sec->SectionIndex;
432 void Writer::createSymbolAndStringTable() {
433 if (!Config->Debug || !Config->WriteSymtab)
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)
442 Sec->setStringTableOff(addEntryToStringTable(Name));
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);
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);
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);
465 FileOff += Strtab.size() + 4;
466 FileSize = alignTo(FileOff, SectorSize);
469 // Visits all sections to assign incremental, non-overlapping RVAs and
471 void Writer::assignAddresses() {
472 SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
473 sizeof(data_directory) * NumberfOfDataDirectory +
474 sizeof(coff_section) * OutputSections.size();
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;
486 for (OutputSection *Sec : OutputSections) {
487 if (Sec->getName() == ".reloc")
490 Sec->setFileOffset(FileSize);
491 RVA += alignTo(Sec->getVirtualSize(), PageSize);
492 FileSize += alignTo(Sec->getRawSize(), SectorSize);
494 SizeOfImage = SizeOfHeaders + alignTo(RVA - 0x1000, PageSize);
497 template <typename PEHeaderTy> void Writer::writeHeader() {
499 uint8_t *Buf = Buffer->getBufferStart();
500 auto *DOS = reinterpret_cast<dos_header *>(Buf);
504 DOS->AddressOfRelocationTable = sizeof(dos_header);
505 DOS->AddressOfNewExeHeader = DOSStubSize;
508 memcpy(Buf, PEMagic, sizeof(PEMagic));
509 Buf += sizeof(PEMagic);
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;
520 COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
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;
529 auto *PE = reinterpret_cast<PEHeaderTy *>(Buf);
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;
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();
572 PE->SizeOfInitializedData = getSizeOfInitializedData();
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();
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();
587 if (!DelayIdata.empty()) {
588 Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
589 DelayIdata.getDirRVA();
590 Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize();
592 if (OutputSection *Sec = findSection(".rsrc")) {
593 Dir[RESOURCE_TABLE].RelativeVirtualAddress = Sec->getRVA();
594 Dir[RESOURCE_TABLE].Size = Sec->getVirtualSize();
596 if (OutputSection *Sec = findSection(".reloc")) {
597 Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = Sec->getRVA();
598 Dir[BASE_RELOCATION_TABLE].Size = Sec->getVirtualSize();
600 if (OutputSection *Sec = findSection(".pdata")) {
601 Dir[EXCEPTION_TABLE].RelativeVirtualAddress = Sec->getRVA();
602 Dir[EXCEPTION_TABLE].Size = Sec->getVirtualSize();
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);
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");
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;
630 // Write section table
631 for (OutputSection *Sec : OutputSections) {
632 Sec->writeHeaderTo(Buf);
633 Buf += sizeof(coff_section);
636 if (OutputSymtab.empty())
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);
651 memcpy(Buf + 4, Strtab.data(), Strtab.size());
654 void Writer::openFile(StringRef Path) {
656 FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable),
657 "failed to open " + Path);
660 void Writer::fixSafeSEHSymbols() {
663 Config->SEHTable->setRVA(SEHTable->getRVA());
664 Config->SEHCount->setVA(SEHTable->getSize() / 4);
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);
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); });
693 // Sort .pdata section contents according to PE/COFF spec 5.5.
694 void Writer::sortExceptionTable() {
695 OutputSection *Sec = findSection(".pdata");
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; };
704 (Entry *)Begin, (Entry *)End,
705 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
708 if (Config->Machine == ARMNT) {
709 struct Entry { ulittle32_t Begin, Unwind; };
711 (Entry *)Begin, (Entry *)End,
712 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
715 errs() << "warning: don't know how to handle .pdata.\n";
718 OutputSection *Writer::findSection(StringRef Name) {
719 for (OutputSection *Sec : OutputSections)
720 if (Sec->getName() == Name)
725 uint32_t Writer::getSizeOfInitializedData() {
727 for (OutputSection *S : OutputSections)
728 if (S->getPermissions() & IMAGE_SCN_CNT_INITIALIZED_DATA)
729 Res += S->getRawSize();
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))
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)
755 llvm_unreachable("unknown section name");
756 auto Sec = new (CAlloc.Allocate()) OutputSection(Name);
757 Sec->addPermissions(Perms);
758 OutputSections.push_back(Sec);
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) {
768 // Collect all locations for base relocations.
769 for (Chunk *C : Sec->getChunks())
771 // Add the addresses to .reloc section.
773 addBaserelBlocks(Dest, V);
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;
783 for (size_t E = V.size(); J < E; ++J) {
784 uint32_t P = V[J].RVA & Mask;
787 BaserelChunk *Buf = BAlloc.Allocate();
788 Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J));
794 BaserelChunk *Buf = BAlloc.Allocate();
795 Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J));