2015-05-29 03:09:30 +08:00
|
|
|
//===- Writer.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Config.h"
|
|
|
|
#include "Writer.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
2015-05-31 03:09:50 +08:00
|
|
|
#include <cstdio>
|
2015-05-29 03:09:30 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
2015-06-24 12:36:52 +08:00
|
|
|
#include <unordered_set>
|
2015-05-29 03:09:30 +08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::COFF;
|
2015-05-31 03:09:50 +08:00
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support;
|
|
|
|
using namespace llvm::support::endian;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
static const int PageSize = 4096;
|
|
|
|
static const int FileAlignment = 512;
|
|
|
|
static const int SectionAlignment = 4096;
|
|
|
|
static const int DOSStubSize = 64;
|
|
|
|
static const int NumberfOfDataDirectory = 16;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2015-06-07 07:32:08 +08:00
|
|
|
// The main function of the writer.
|
|
|
|
std::error_code Writer::write(StringRef OutputPath) {
|
|
|
|
markLive();
|
2015-06-25 04:40:03 +08:00
|
|
|
dedupCOMDATs();
|
2015-06-07 07:32:08 +08:00
|
|
|
createSections();
|
2015-06-25 11:31:47 +08:00
|
|
|
createMiscChunks();
|
2015-06-07 07:32:08 +08:00
|
|
|
createImportTables();
|
2015-06-17 08:16:33 +08:00
|
|
|
createExportTable();
|
2015-06-15 09:23:58 +08:00
|
|
|
if (Config->Relocatable)
|
|
|
|
createSection(".reloc");
|
2015-06-07 07:32:08 +08:00
|
|
|
assignAddresses();
|
|
|
|
removeEmptySections();
|
|
|
|
if (auto EC = openFile(OutputPath))
|
|
|
|
return EC;
|
|
|
|
writeHeader();
|
|
|
|
writeSections();
|
2015-06-21 12:00:54 +08:00
|
|
|
sortExceptionTable();
|
2015-06-07 07:32:08 +08:00
|
|
|
return Buffer->commit();
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
void OutputSection::setRVA(uint64_t RVA) {
|
|
|
|
Header.VirtualAddress = RVA;
|
|
|
|
for (Chunk *C : Chunks)
|
|
|
|
C->setRVA(C->getRVA() + RVA);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSection::setFileOffset(uint64_t Off) {
|
|
|
|
// If a section has no actual data (i.e. BSS section), we want to
|
|
|
|
// set 0 to its PointerToRawData. Otherwise the output is rejected
|
|
|
|
// by the loader.
|
|
|
|
if (Header.SizeOfRawData == 0)
|
|
|
|
return;
|
|
|
|
Header.PointerToRawData = Off;
|
|
|
|
for (Chunk *C : Chunks)
|
|
|
|
C->setFileOff(C->getFileOff() + Off);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSection::addChunk(Chunk *C) {
|
|
|
|
Chunks.push_back(C);
|
2015-06-15 06:16:47 +08:00
|
|
|
C->setOutputSection(this);
|
2015-05-29 03:09:30 +08:00
|
|
|
uint64_t Off = Header.VirtualSize;
|
|
|
|
Off = RoundUpToAlignment(Off, C->getAlign());
|
|
|
|
C->setRVA(Off);
|
|
|
|
C->setFileOff(Off);
|
|
|
|
Off += C->getSize();
|
|
|
|
Header.VirtualSize = Off;
|
|
|
|
if (C->hasData())
|
|
|
|
Header.SizeOfRawData = RoundUpToAlignment(Off, FileAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputSection::addPermissions(uint32_t C) {
|
2015-06-15 11:03:23 +08:00
|
|
|
Header.Characteristics |= C & PermMask;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-05-31 03:09:50 +08:00
|
|
|
// Write the section header to a given buffer.
|
2015-06-07 07:19:38 +08:00
|
|
|
void OutputSection::writeHeaderTo(uint8_t *Buf) {
|
2015-05-31 03:09:50 +08:00
|
|
|
auto *Hdr = reinterpret_cast<coff_section *>(Buf);
|
|
|
|
*Hdr = Header;
|
|
|
|
if (StringTableOff) {
|
|
|
|
// If name is too long, write offset into the string table as a name.
|
|
|
|
sprintf(Hdr->Name, "/%d", StringTableOff);
|
|
|
|
} else {
|
|
|
|
assert(Name.size() <= COFF::NameSize);
|
|
|
|
strncpy(Hdr->Name, Name.data(), Name.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-07 07:32:08 +08:00
|
|
|
// Set live bit on for each reachable chunk. Unmarked (unreachable)
|
|
|
|
// COMDAT chunks will be ignored in the next step, so that they don't
|
|
|
|
// come to the final output file.
|
2015-05-29 03:09:30 +08:00
|
|
|
void Writer::markLive() {
|
2015-06-07 11:17:42 +08:00
|
|
|
if (!Config->DoGC)
|
|
|
|
return;
|
2015-06-04 10:12:16 +08:00
|
|
|
for (StringRef Name : Config->GCRoots)
|
|
|
|
cast<Defined>(Symtab->find(Name))->markLive();
|
2015-05-29 03:09:30 +08:00
|
|
|
for (Chunk *C : Symtab->getChunks())
|
|
|
|
if (C->isRoot())
|
|
|
|
C->markLive();
|
|
|
|
}
|
|
|
|
|
2015-06-24 12:36:52 +08:00
|
|
|
struct Hasher {
|
|
|
|
size_t operator()(const SectionChunk *C) const { return C->getHash(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Equals {
|
|
|
|
bool operator()(const SectionChunk *A, const SectionChunk *B) const {
|
|
|
|
return A->equals(B);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Merge identical COMDAT sections.
|
|
|
|
void Writer::dedupCOMDATs() {
|
2015-06-25 04:40:03 +08:00
|
|
|
if (Config->ICF)
|
|
|
|
doICF(Symtab->getChunks());
|
2015-06-24 12:36:52 +08:00
|
|
|
}
|
|
|
|
|
2015-06-07 07:32:08 +08:00
|
|
|
// Create output section objects and add them to OutputSections.
|
2015-05-29 03:09:30 +08:00
|
|
|
void Writer::createSections() {
|
2015-06-07 07:32:08 +08:00
|
|
|
// First, bin chunks by name.
|
2015-05-29 03:09:30 +08:00
|
|
|
std::map<StringRef, std::vector<Chunk *>> Map;
|
|
|
|
for (Chunk *C : Symtab->getChunks()) {
|
2015-06-07 11:17:42 +08:00
|
|
|
if (Config->DoGC && !C->isLive()) {
|
2015-05-29 03:09:30 +08:00
|
|
|
if (Config->Verbose)
|
|
|
|
C->printDiscardedMessage();
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-08 16:26:28 +08:00
|
|
|
Map[C->getSectionName()].push_back(C);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-07 07:32:08 +08:00
|
|
|
// Then create an OutputSection for each section.
|
2015-06-08 16:26:28 +08:00
|
|
|
// '$' and all following characters in input section names are
|
|
|
|
// discarded when determining output section. So, .text$foo
|
|
|
|
// contributes to .text, for example. See PE/COFF spec 3.2.
|
|
|
|
StringRef Name = Map.begin()->first.split('$').first;
|
|
|
|
auto Sec = new (CAlloc.Allocate()) OutputSection(Name, 0);
|
|
|
|
OutputSections.push_back(Sec);
|
2015-05-29 03:09:30 +08:00
|
|
|
for (auto &P : Map) {
|
|
|
|
StringRef SectionName = P.first;
|
2015-06-08 16:26:28 +08:00
|
|
|
StringRef Base = SectionName.split('$').first;
|
|
|
|
if (Base != Sec->getName()) {
|
|
|
|
size_t SectIdx = OutputSections.size();
|
|
|
|
Sec = new (CAlloc.Allocate()) OutputSection(Base, SectIdx);
|
|
|
|
OutputSections.push_back(Sec);
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
std::vector<Chunk *> &Chunks = P.second;
|
|
|
|
for (Chunk *C : Chunks) {
|
|
|
|
Sec->addChunk(C);
|
|
|
|
Sec->addPermissions(C->getPermissions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 11:31:47 +08:00
|
|
|
void Writer::createMiscChunks() {
|
|
|
|
if (Symtab->LocalImportChunks.empty())
|
|
|
|
return;
|
|
|
|
OutputSection *Sec = createSection(".rdata");
|
|
|
|
for (Chunk *C : Symtab->LocalImportChunks)
|
|
|
|
Sec->addChunk(C);
|
|
|
|
}
|
|
|
|
|
2015-06-07 06:56:55 +08:00
|
|
|
// Create .idata section for the DLL-imported symbol table.
|
|
|
|
// The format of this section is inherently Windows-specific.
|
|
|
|
// IdataContents class abstracted away the details for us,
|
|
|
|
// so we just let it create chunks and add them to the section.
|
2015-06-07 06:46:15 +08:00
|
|
|
void Writer::createImportTables() {
|
|
|
|
if (Symtab->ImportFiles.empty())
|
|
|
|
return;
|
2015-05-29 03:09:30 +08:00
|
|
|
OutputSection *Text = createSection(".text");
|
2015-06-24 07:56:39 +08:00
|
|
|
for (ImportFile *File : Symtab->ImportFiles) {
|
2015-06-22 06:31:52 +08:00
|
|
|
for (SymbolBody *B : File->getSymbols()) {
|
|
|
|
auto *Import = dyn_cast<DefinedImportData>(B);
|
|
|
|
if (!Import) {
|
|
|
|
// Linker-created function thunks for DLL symbols are added to
|
|
|
|
// .text section.
|
|
|
|
Text->addChunk(cast<DefinedImportThunk>(B)->getChunk());
|
2015-05-29 03:09:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-22 06:31:52 +08:00
|
|
|
if (Config->DelayLoads.count(Import->getDLLName())) {
|
|
|
|
DelayIdata.add(Import);
|
|
|
|
} else {
|
|
|
|
Idata.add(Import);
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-22 06:31:52 +08:00
|
|
|
if (!Idata.empty()) {
|
|
|
|
OutputSection *Sec = createSection(".idata");
|
|
|
|
for (Chunk *C : Idata.getChunks())
|
|
|
|
Sec->addChunk(C);
|
|
|
|
}
|
|
|
|
if (!DelayIdata.empty()) {
|
|
|
|
OutputSection *Sec = createSection(".didat");
|
|
|
|
for (Chunk *C : DelayIdata.getChunks(Symtab->find("__delayLoadHelper2")))
|
|
|
|
Sec->addChunk(C);
|
|
|
|
Sec = createSection(".text");
|
|
|
|
for (std::unique_ptr<Chunk> &C : DelayIdata.getCodeChunks())
|
|
|
|
Sec->addChunk(C.get());
|
|
|
|
Sec = createSection(".data");
|
|
|
|
for (std::unique_ptr<Chunk> &C : DelayIdata.getDataChunks())
|
|
|
|
Sec->addChunk(C.get());
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
void Writer::createExportTable() {
|
|
|
|
if (Config->Exports.empty())
|
|
|
|
return;
|
|
|
|
OutputSection *Sec = createSection(".edata");
|
2015-06-22 06:31:52 +08:00
|
|
|
for (std::unique_ptr<Chunk> &C : Edata.Chunks)
|
2015-06-17 08:16:33 +08:00
|
|
|
Sec->addChunk(C.get());
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// The Windows loader doesn't seem to like empty sections,
|
|
|
|
// so we remove them if any.
|
|
|
|
void Writer::removeEmptySections() {
|
2015-06-04 00:44:00 +08:00
|
|
|
auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; };
|
2015-05-29 03:09:30 +08:00
|
|
|
OutputSections.erase(
|
|
|
|
std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
|
|
|
|
OutputSections.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visits all sections to assign incremental, non-overlapping RVAs and
|
|
|
|
// file offsets.
|
|
|
|
void Writer::assignAddresses() {
|
|
|
|
SizeOfHeaders = RoundUpToAlignment(
|
2015-06-07 07:19:36 +08:00
|
|
|
DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
|
|
|
|
sizeof(pe32plus_header) +
|
|
|
|
sizeof(data_directory) * NumberfOfDataDirectory +
|
|
|
|
sizeof(coff_section) * OutputSections.size(), PageSize);
|
2015-05-29 03:09:30 +08:00
|
|
|
uint64_t RVA = 0x1000; // The first page is kept unmapped.
|
|
|
|
uint64_t FileOff = SizeOfHeaders;
|
2015-06-04 00:44:00 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2015-06-15 09:23:58 +08:00
|
|
|
if (Sec->getName() == ".reloc")
|
|
|
|
addBaserels(Sec);
|
2015-05-29 03:09:30 +08:00
|
|
|
Sec->setRVA(RVA);
|
|
|
|
Sec->setFileOffset(FileOff);
|
|
|
|
RVA += RoundUpToAlignment(Sec->getVirtualSize(), PageSize);
|
|
|
|
FileOff += RoundUpToAlignment(Sec->getRawSize(), FileAlignment);
|
|
|
|
}
|
|
|
|
SizeOfImage = SizeOfHeaders + RoundUpToAlignment(RVA - 0x1000, PageSize);
|
|
|
|
FileSize = SizeOfHeaders +
|
|
|
|
RoundUpToAlignment(FileOff - SizeOfHeaders, FileAlignment);
|
|
|
|
}
|
|
|
|
|
2015-05-30 05:47:36 +08:00
|
|
|
static MachineTypes
|
2015-06-24 07:56:39 +08:00
|
|
|
inferMachineType(const std::vector<ObjectFile *> &Files) {
|
|
|
|
for (ObjectFile *F : Files) {
|
2015-05-30 05:47:36 +08:00
|
|
|
// Try to infer machine type from the magic byte of the object file.
|
2015-06-24 07:56:39 +08:00
|
|
|
auto MT = static_cast<MachineTypes>(F->getCOFFObj()->getMachine());
|
2015-05-30 05:47:36 +08:00
|
|
|
if (MT != IMAGE_FILE_MACHINE_UNKNOWN)
|
|
|
|
return MT;
|
|
|
|
}
|
|
|
|
return IMAGE_FILE_MACHINE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
void Writer::writeHeader() {
|
|
|
|
// Write DOS stub
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
|
|
|
auto *DOS = reinterpret_cast<dos_header *>(Buf);
|
|
|
|
Buf += DOSStubSize;
|
|
|
|
DOS->Magic[0] = 'M';
|
|
|
|
DOS->Magic[1] = 'Z';
|
|
|
|
DOS->AddressOfRelocationTable = sizeof(dos_header);
|
|
|
|
DOS->AddressOfNewExeHeader = DOSStubSize;
|
|
|
|
|
|
|
|
// Write PE magic
|
|
|
|
memcpy(Buf, PEMagic, sizeof(PEMagic));
|
|
|
|
Buf += sizeof(PEMagic);
|
|
|
|
|
2015-05-30 05:47:36 +08:00
|
|
|
// Determine machine type, infer if needed. TODO: diagnose conflicts.
|
|
|
|
MachineTypes MachineType = Config->MachineType;
|
|
|
|
if (MachineType == IMAGE_FILE_MACHINE_UNKNOWN)
|
|
|
|
MachineType = inferMachineType(Symtab->ObjectFiles);
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Write COFF header
|
|
|
|
auto *COFF = reinterpret_cast<coff_file_header *>(Buf);
|
|
|
|
Buf += sizeof(*COFF);
|
2015-05-30 05:47:36 +08:00
|
|
|
COFF->Machine = MachineType;
|
2015-05-29 03:09:30 +08:00
|
|
|
COFF->NumberOfSections = OutputSections.size();
|
2015-06-15 11:03:23 +08:00
|
|
|
COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
|
|
|
|
COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
|
2015-06-17 08:16:33 +08:00
|
|
|
if (Config->DLL)
|
|
|
|
COFF->Characteristics |= IMAGE_FILE_DLL;
|
2015-06-15 09:23:58 +08:00
|
|
|
if (!Config->Relocatable)
|
2015-06-15 11:03:23 +08:00
|
|
|
COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
|
2015-05-29 03:09:30 +08:00
|
|
|
COFF->SizeOfOptionalHeader =
|
|
|
|
sizeof(pe32plus_header) + sizeof(data_directory) * NumberfOfDataDirectory;
|
|
|
|
|
|
|
|
// Write PE header
|
|
|
|
auto *PE = reinterpret_cast<pe32plus_header *>(Buf);
|
|
|
|
Buf += sizeof(*PE);
|
|
|
|
PE->Magic = PE32Header::PE32_PLUS;
|
|
|
|
PE->ImageBase = Config->ImageBase;
|
|
|
|
PE->SectionAlignment = SectionAlignment;
|
|
|
|
PE->FileAlignment = FileAlignment;
|
2015-05-30 00:28:29 +08:00
|
|
|
PE->MajorImageVersion = Config->MajorImageVersion;
|
|
|
|
PE->MinorImageVersion = Config->MinorImageVersion;
|
2015-05-30 00:34:31 +08:00
|
|
|
PE->MajorOperatingSystemVersion = Config->MajorOSVersion;
|
|
|
|
PE->MinorOperatingSystemVersion = Config->MinorOSVersion;
|
|
|
|
PE->MajorSubsystemVersion = Config->MajorOSVersion;
|
|
|
|
PE->MinorSubsystemVersion = Config->MinorOSVersion;
|
|
|
|
PE->Subsystem = Config->Subsystem;
|
2015-05-29 03:09:30 +08:00
|
|
|
PE->SizeOfImage = SizeOfImage;
|
|
|
|
PE->SizeOfHeaders = SizeOfHeaders;
|
2015-06-04 10:12:16 +08:00
|
|
|
Defined *Entry = cast<Defined>(Symtab->find(Config->EntryName));
|
2015-05-29 03:09:30 +08:00
|
|
|
PE->AddressOfEntryPoint = Entry->getRVA();
|
2015-05-30 00:21:11 +08:00
|
|
|
PE->SizeOfStackReserve = Config->StackReserve;
|
|
|
|
PE->SizeOfStackCommit = Config->StackCommit;
|
2015-05-30 00:23:40 +08:00
|
|
|
PE->SizeOfHeapReserve = Config->HeapReserve;
|
|
|
|
PE->SizeOfHeapCommit = Config->HeapCommit;
|
2015-06-17 07:13:00 +08:00
|
|
|
if (Config->DynamicBase)
|
|
|
|
PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
|
|
|
|
if (Config->HighEntropyVA)
|
|
|
|
PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
|
|
|
|
if (!Config->AllowBind)
|
|
|
|
PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
|
|
|
|
if (Config->NxCompat)
|
|
|
|
PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
|
|
|
|
if (!Config->AllowIsolation)
|
|
|
|
PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
|
|
|
|
if (Config->TerminalServerAware)
|
|
|
|
PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
|
2015-05-29 03:09:30 +08:00
|
|
|
PE->NumberOfRvaAndSize = NumberfOfDataDirectory;
|
|
|
|
if (OutputSection *Text = findSection(".text")) {
|
|
|
|
PE->BaseOfCode = Text->getRVA();
|
|
|
|
PE->SizeOfCode = Text->getRawSize();
|
|
|
|
}
|
|
|
|
PE->SizeOfInitializedData = getSizeOfInitializedData();
|
|
|
|
|
|
|
|
// Write data directory
|
2015-06-21 12:10:54 +08:00
|
|
|
auto *Dir = reinterpret_cast<data_directory *>(Buf);
|
|
|
|
Buf += sizeof(*Dir) * NumberfOfDataDirectory;
|
2015-06-17 08:16:33 +08:00
|
|
|
if (OutputSection *Sec = findSection(".edata")) {
|
2015-06-21 12:10:54 +08:00
|
|
|
Dir[EXPORT_TABLE].RelativeVirtualAddress = Sec->getRVA();
|
|
|
|
Dir[EXPORT_TABLE].Size = Sec->getVirtualSize();
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
2015-06-22 06:31:52 +08:00
|
|
|
if (!Idata.empty()) {
|
|
|
|
Dir[IMPORT_TABLE].RelativeVirtualAddress = Idata.getDirRVA();
|
|
|
|
Dir[IMPORT_TABLE].Size = Idata.getDirSize();
|
|
|
|
Dir[IAT].RelativeVirtualAddress = Idata.getIATRVA();
|
|
|
|
Dir[IAT].Size = Idata.getIATSize();
|
|
|
|
}
|
|
|
|
if (!DelayIdata.empty()) {
|
|
|
|
Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
|
|
|
|
DelayIdata.getDirRVA();
|
|
|
|
Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2015-06-15 05:50:50 +08:00
|
|
|
if (OutputSection *Sec = findSection(".rsrc")) {
|
2015-06-21 12:10:54 +08:00
|
|
|
Dir[RESOURCE_TABLE].RelativeVirtualAddress = Sec->getRVA();
|
|
|
|
Dir[RESOURCE_TABLE].Size = Sec->getVirtualSize();
|
2015-06-15 05:50:50 +08:00
|
|
|
}
|
2015-06-15 09:23:58 +08:00
|
|
|
if (OutputSection *Sec = findSection(".reloc")) {
|
2015-06-21 12:10:54 +08:00
|
|
|
Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = Sec->getRVA();
|
|
|
|
Dir[BASE_RELOCATION_TABLE].Size = Sec->getVirtualSize();
|
2015-06-15 09:23:58 +08:00
|
|
|
}
|
2015-06-21 12:00:54 +08:00
|
|
|
if (OutputSection *Sec = findSection(".pdata")) {
|
2015-06-21 12:10:54 +08:00
|
|
|
Dir[EXCEPTION_TABLE].RelativeVirtualAddress = Sec->getRVA();
|
|
|
|
Dir[EXCEPTION_TABLE].Size = Sec->getVirtualSize();
|
2015-06-21 12:00:54 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-05-31 03:09:50 +08:00
|
|
|
// Section table
|
2015-06-01 11:55:02 +08:00
|
|
|
// Name field in the section table is 8 byte long. Longer names need
|
2015-05-31 03:09:50 +08:00
|
|
|
// to be written to the string table. First, construct string table.
|
|
|
|
std::vector<char> Strtab;
|
2015-06-04 00:44:00 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2015-05-31 03:09:50 +08:00
|
|
|
StringRef Name = Sec->getName();
|
|
|
|
if (Name.size() <= COFF::NameSize)
|
|
|
|
continue;
|
|
|
|
Sec->setStringTableOff(Strtab.size() + 4); // +4 for the size field
|
|
|
|
Strtab.insert(Strtab.end(), Name.begin(), Name.end());
|
|
|
|
Strtab.push_back('\0');
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Write section table
|
2015-06-04 00:44:00 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2015-06-07 07:19:38 +08:00
|
|
|
Sec->writeHeaderTo(Buf);
|
2015-05-31 03:09:50 +08:00
|
|
|
Buf += sizeof(coff_section);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write string table if we need to. The string table immediately
|
|
|
|
// follows the symbol table, so we create a dummy symbol table
|
|
|
|
// first. The symbol table contains one dummy symbol.
|
|
|
|
if (Strtab.empty())
|
|
|
|
return;
|
|
|
|
COFF->PointerToSymbolTable = Buf - Buffer->getBufferStart();
|
|
|
|
COFF->NumberOfSymbols = 1;
|
|
|
|
auto *SymbolTable = reinterpret_cast<coff_symbol16 *>(Buf);
|
|
|
|
Buf += sizeof(*SymbolTable);
|
|
|
|
// (Set 4 to make the dummy symbol point to the first string table
|
|
|
|
// entry, so that tools to print out symbols don't read NUL bytes.)
|
|
|
|
SymbolTable->Name.Offset.Offset = 4;
|
|
|
|
// Then create the symbol table. The first 4 bytes is length
|
|
|
|
// including itself.
|
|
|
|
write32le(Buf, Strtab.size() + 4);
|
|
|
|
memcpy(Buf + 4, Strtab.data(), Strtab.size());
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code Writer::openFile(StringRef Path) {
|
|
|
|
if (auto EC = FileOutputBuffer::create(Path, FileSize, Buffer,
|
2015-06-01 10:58:15 +08:00
|
|
|
FileOutputBuffer::F_executable)) {
|
|
|
|
llvm::errs() << "failed to open " << Path << ": " << EC.message() << "\n";
|
|
|
|
return EC;
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write section contents to a mmap'ed file.
|
|
|
|
void Writer::writeSections() {
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
2015-06-04 00:44:00 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2015-05-29 03:09:30 +08:00
|
|
|
// Fill gaps between functions in .text with INT3 instructions
|
|
|
|
// instead of leaving as NUL bytes (which can be interpreted as
|
|
|
|
// ADD instructions).
|
|
|
|
if (Sec->getPermissions() & IMAGE_SCN_CNT_CODE)
|
|
|
|
memset(Buf + Sec->getFileOff(), 0xCC, Sec->getRawSize());
|
|
|
|
for (Chunk *C : Sec->getChunks())
|
2015-05-29 03:45:43 +08:00
|
|
|
C->writeTo(Buf);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 12:00:54 +08:00
|
|
|
// Sort .pdata section contents according to PE/COFF spec 5.5.
|
|
|
|
void Writer::sortExceptionTable() {
|
|
|
|
if (auto *Sec = findSection(".pdata")) {
|
|
|
|
// We assume .pdata contains function table entries only.
|
|
|
|
struct Entry { ulittle32_t Begin, End, Unwind; };
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart() + Sec->getFileOff();
|
|
|
|
std::sort(reinterpret_cast<Entry *>(Buf),
|
|
|
|
reinterpret_cast<Entry *>(Buf + Sec->getVirtualSize()),
|
|
|
|
[](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
OutputSection *Writer::findSection(StringRef Name) {
|
2015-06-04 00:44:00 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2015-05-29 03:09:30 +08:00
|
|
|
if (Sec->getName() == Name)
|
2015-06-04 00:44:00 +08:00
|
|
|
return Sec;
|
2015-05-29 03:09:30 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Writer::getSizeOfInitializedData() {
|
|
|
|
uint32_t Res = 0;
|
2015-06-04 00:44:00 +08:00
|
|
|
for (OutputSection *S : OutputSections)
|
2015-05-29 03:09:30 +08:00
|
|
|
if (S->getPermissions() & IMAGE_SCN_CNT_INITIALIZED_DATA)
|
|
|
|
Res += S->getRawSize();
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an existing section or create a new one if not found.
|
|
|
|
OutputSection *Writer::createSection(StringRef Name) {
|
|
|
|
if (auto *Sec = findSection(Name))
|
|
|
|
return Sec;
|
2015-06-07 07:07:01 +08:00
|
|
|
const auto DATA = IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
|
|
const auto BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
|
|
|
|
const auto CODE = IMAGE_SCN_CNT_CODE;
|
2015-06-15 09:23:58 +08:00
|
|
|
const auto DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE;
|
2015-05-29 03:09:30 +08:00
|
|
|
const auto R = IMAGE_SCN_MEM_READ;
|
|
|
|
const auto W = IMAGE_SCN_MEM_WRITE;
|
2015-06-15 06:21:29 +08:00
|
|
|
const auto X = IMAGE_SCN_MEM_EXECUTE;
|
2015-06-07 07:07:01 +08:00
|
|
|
uint32_t Perms = StringSwitch<uint32_t>(Name)
|
|
|
|
.Case(".bss", BSS | R | W)
|
|
|
|
.Case(".data", DATA | R | W)
|
|
|
|
.Case(".didat", DATA | R)
|
2015-06-17 08:16:33 +08:00
|
|
|
.Case(".edata", DATA | R)
|
2015-06-07 07:07:01 +08:00
|
|
|
.Case(".idata", DATA | R)
|
|
|
|
.Case(".rdata", DATA | R)
|
2015-06-15 09:23:58 +08:00
|
|
|
.Case(".reloc", DATA | DISCARDABLE | R)
|
2015-06-15 06:21:29 +08:00
|
|
|
.Case(".text", CODE | R | X)
|
2015-06-07 07:07:01 +08:00
|
|
|
.Default(0);
|
|
|
|
if (!Perms)
|
2015-05-29 03:09:30 +08:00
|
|
|
llvm_unreachable("unknown section name");
|
2015-06-04 00:44:00 +08:00
|
|
|
size_t SectIdx = OutputSections.size();
|
|
|
|
auto Sec = new (CAlloc.Allocate()) OutputSection(Name, SectIdx);
|
2015-06-07 07:07:01 +08:00
|
|
|
Sec->addPermissions(Perms);
|
2015-06-04 00:44:00 +08:00
|
|
|
OutputSections.push_back(Sec);
|
2015-05-29 03:09:30 +08:00
|
|
|
return Sec;
|
|
|
|
}
|
|
|
|
|
2015-06-15 09:23:58 +08:00
|
|
|
// Dest is .reloc section. Add contents to that section.
|
|
|
|
void Writer::addBaserels(OutputSection *Dest) {
|
|
|
|
std::vector<uint32_t> V;
|
|
|
|
Defined *ImageBase = cast<Defined>(Symtab->find("__ImageBase"));
|
|
|
|
for (OutputSection *Sec : OutputSections) {
|
|
|
|
if (Sec == Dest)
|
|
|
|
continue;
|
|
|
|
// Collect all locations for base relocations.
|
|
|
|
for (Chunk *C : Sec->getChunks())
|
|
|
|
C->getBaserels(&V, ImageBase);
|
|
|
|
// Add the addresses to .reloc section.
|
|
|
|
if (!V.empty())
|
|
|
|
addBaserelBlocks(Dest, V);
|
|
|
|
V.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add addresses to .reloc section. Note that addresses are grouped by page.
|
|
|
|
void Writer::addBaserelBlocks(OutputSection *Dest, std::vector<uint32_t> &V) {
|
|
|
|
const uint32_t Mask = ~uint32_t(PageSize - 1);
|
|
|
|
uint32_t Page = V[0] & Mask;
|
|
|
|
size_t I = 0, J = 1;
|
|
|
|
for (size_t E = V.size(); J < E; ++J) {
|
|
|
|
uint32_t P = V[J] & Mask;
|
|
|
|
if (P == Page)
|
|
|
|
continue;
|
|
|
|
BaserelChunk *Buf = BAlloc.Allocate();
|
|
|
|
Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J));
|
|
|
|
I = J;
|
|
|
|
Page = P;
|
|
|
|
}
|
|
|
|
if (I == J)
|
|
|
|
return;
|
|
|
|
BaserelChunk *Buf = BAlloc.Allocate();
|
|
|
|
Dest->addChunk(new (Buf) BaserelChunk(Page, &V[I], &V[0] + J));
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|