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"
|
2015-08-06 07:43:53 +08:00
|
|
|
#include "DLL.h"
|
2015-08-06 22:58:50 +08:00
|
|
|
#include "Error.h"
|
2015-08-06 07:43:53 +08:00
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "Writer.h"
|
2015-09-19 06:07:10 +08:00
|
|
|
#include "lld/Core/Parallel.h"
|
2015-07-28 08:17:25 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-07-28 08:17:25 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#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 <map>
|
2015-08-06 07:43:53 +08:00
|
|
|
#include <memory>
|
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-08-06 07:43:53 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::coff;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
static const int PageSize = 4096;
|
2015-08-12 07:09:00 +08:00
|
|
|
static const int SectorSize = 512;
|
2015-05-29 03:09:30 +08:00
|
|
|
static const int DOSStubSize = 64;
|
|
|
|
static const int NumberfOfDataDirectory = 16;
|
|
|
|
|
2015-08-06 07:43:53 +08:00
|
|
|
namespace {
|
|
|
|
// The writer writes a SymbolTable result to a file.
|
|
|
|
class Writer {
|
|
|
|
public:
|
2015-08-06 07:51:50 +08:00
|
|
|
Writer(SymbolTable *T) : Symtab(T) {}
|
2015-08-06 22:58:50 +08:00
|
|
|
void run();
|
2015-08-06 07:43:53 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void createSections();
|
|
|
|
void createMiscChunks();
|
|
|
|
void createImportTables();
|
|
|
|
void createExportTable();
|
|
|
|
void assignAddresses();
|
|
|
|
void removeEmptySections();
|
|
|
|
void createSymbolAndStringTable();
|
2015-08-06 22:58:50 +08:00
|
|
|
void openFile(StringRef OutputPath);
|
2015-08-06 07:43:53 +08:00
|
|
|
template <typename PEHeaderTy> void writeHeader();
|
|
|
|
void fixSafeSEHSymbols();
|
|
|
|
void writeSections();
|
|
|
|
void sortExceptionTable();
|
|
|
|
void applyRelocations();
|
|
|
|
|
|
|
|
llvm::Optional<coff_symbol16> createSymbol(Defined *D);
|
|
|
|
size_t addEntryToStringTable(StringRef Str);
|
|
|
|
|
|
|
|
OutputSection *findSection(StringRef Name);
|
|
|
|
OutputSection *createSection(StringRef Name);
|
|
|
|
void addBaserels(OutputSection *Dest);
|
|
|
|
void addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V);
|
|
|
|
|
|
|
|
uint32_t getSizeOfInitializedData();
|
|
|
|
std::map<StringRef, std::vector<DefinedImportData *>> binImports();
|
|
|
|
|
|
|
|
SymbolTable *Symtab;
|
|
|
|
std::unique_ptr<llvm::FileOutputBuffer> Buffer;
|
|
|
|
llvm::SpecificBumpPtrAllocator<OutputSection> CAlloc;
|
|
|
|
llvm::SpecificBumpPtrAllocator<BaserelChunk> BAlloc;
|
|
|
|
std::vector<OutputSection *> OutputSections;
|
|
|
|
std::vector<char> Strtab;
|
|
|
|
std::vector<llvm::object::coff_symbol16> OutputSymtab;
|
|
|
|
IdataContents Idata;
|
|
|
|
DelayLoadContents DelayIdata;
|
|
|
|
EdataContents Edata;
|
|
|
|
std::unique_ptr<SEHTableChunk> SEHTable;
|
|
|
|
|
|
|
|
uint64_t FileSize;
|
|
|
|
uint32_t PointerToSymbolTable = 0;
|
|
|
|
uint64_t SizeOfImage;
|
|
|
|
uint64_t SizeOfHeaders;
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<Chunk>> Chunks;
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void writeResult(SymbolTable *T) { Writer(T).run(); }
|
2015-06-07 07:32:08 +08:00
|
|
|
|
2015-08-06 07:43:53 +08:00
|
|
|
// OutputSection represents a section in an output file. It's a
|
|
|
|
// container of chunks. OutputSection and Chunk are 1:N relationship.
|
|
|
|
// Chunks cannot belong to more than one OutputSections. The writer
|
|
|
|
// creates multiple OutputSections and assign them unique,
|
|
|
|
// non-overlapping file offsets and RVAs.
|
|
|
|
class OutputSection {
|
|
|
|
public:
|
|
|
|
OutputSection(StringRef N) : Name(N), Header({}) {}
|
|
|
|
void setRVA(uint64_t);
|
|
|
|
void setFileOffset(uint64_t);
|
|
|
|
void addChunk(Chunk *C);
|
|
|
|
StringRef getName() { return Name; }
|
|
|
|
std::vector<Chunk *> &getChunks() { return Chunks; }
|
|
|
|
void addPermissions(uint32_t C);
|
|
|
|
uint32_t getPermissions() { return Header.Characteristics & PermMask; }
|
|
|
|
uint32_t getCharacteristics() { return Header.Characteristics; }
|
|
|
|
uint64_t getRVA() { return Header.VirtualAddress; }
|
|
|
|
uint64_t getFileOff() { return Header.PointerToRawData; }
|
|
|
|
void writeHeaderTo(uint8_t *Buf);
|
|
|
|
|
|
|
|
// Returns the size of this section in an executable memory image.
|
|
|
|
// This may be smaller than the raw size (the raw size is multiple
|
|
|
|
// of disk sector size, so there may be padding at end), or may be
|
|
|
|
// larger (if that's the case, the loader reserves spaces after end
|
|
|
|
// of raw data).
|
|
|
|
uint64_t getVirtualSize() { return Header.VirtualSize; }
|
|
|
|
|
|
|
|
// Returns the size of the section in the output file.
|
|
|
|
uint64_t getRawSize() { return Header.SizeOfRawData; }
|
|
|
|
|
|
|
|
// Set offset into the string table storing this section name.
|
|
|
|
// Used only when the name is longer than 8 bytes.
|
|
|
|
void setStringTableOff(uint32_t V) { StringTableOff = V; }
|
|
|
|
|
|
|
|
// N.B. The section index is one based.
|
|
|
|
uint32_t SectionIndex = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef Name;
|
|
|
|
coff_section Header;
|
|
|
|
uint32_t StringTableOff = 0;
|
|
|
|
std::vector<Chunk *> Chunks;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-01-15 04:53:50 +08:00
|
|
|
Off = alignTo(Off, C->getAlign());
|
2015-05-29 03:09:30 +08:00
|
|
|
C->setRVA(Off);
|
2015-08-14 11:30:59 +08:00
|
|
|
C->setOutputSectionOff(Off);
|
2015-05-29 03:09:30 +08:00
|
|
|
Off += C->getSize();
|
|
|
|
Header.VirtualSize = Off;
|
|
|
|
if (C->hasData())
|
2016-01-15 04:53:50 +08:00
|
|
|
Header.SizeOfRawData = alignTo(Off, SectorSize);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-07-09 00:37:50 +08:00
|
|
|
assert(!Config->Debug || Name.size() <= COFF::NameSize);
|
|
|
|
strncpy(Hdr->Name, Name.data(),
|
|
|
|
std::min(Name.size(), (size_t)COFF::NameSize));
|
2015-05-31 03:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 07:43:53 +08:00
|
|
|
uint64_t Defined::getSecrel() {
|
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(this))
|
|
|
|
return getRVA() - D->getChunk()->getOutputSection()->getRVA();
|
2015-08-06 22:58:50 +08:00
|
|
|
error("SECREL relocation points to a non-regular symbol");
|
2015-08-06 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Defined::getSectionIndex() {
|
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(this))
|
|
|
|
return D->getChunk()->getOutputSection()->SectionIndex;
|
2015-08-06 22:58:50 +08:00
|
|
|
error("SECTION relocation points to a non-regular symbol");
|
2015-08-06 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Defined::isExecutable() {
|
|
|
|
const auto X = IMAGE_SCN_MEM_EXECUTE;
|
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(this))
|
|
|
|
return D->getChunk()->getOutputSection()->getPermissions() & X;
|
|
|
|
return isa<DefinedImportThunk>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
// The main function of the writer.
|
2015-08-06 22:58:50 +08:00
|
|
|
void Writer::run() {
|
2015-08-06 07:43:53 +08:00
|
|
|
createSections();
|
|
|
|
createMiscChunks();
|
|
|
|
createImportTables();
|
|
|
|
createExportTable();
|
|
|
|
if (Config->Relocatable)
|
|
|
|
createSection(".reloc");
|
|
|
|
assignAddresses();
|
|
|
|
removeEmptySections();
|
|
|
|
createSymbolAndStringTable();
|
2015-08-06 22:58:50 +08:00
|
|
|
openFile(Config->OutputFile);
|
2015-08-06 07:43:53 +08:00
|
|
|
if (Config->is64()) {
|
|
|
|
writeHeader<pe32plus_header>();
|
|
|
|
} else {
|
|
|
|
writeHeader<pe32_header>();
|
|
|
|
}
|
|
|
|
fixSafeSEHSymbols();
|
|
|
|
writeSections();
|
|
|
|
sortExceptionTable();
|
2015-08-06 22:58:50 +08:00
|
|
|
error(Buffer->commit(), "Failed to write the output file");
|
2015-08-06 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
2015-07-05 07:37:32 +08:00
|
|
|
static StringRef getOutputSection(StringRef Name) {
|
|
|
|
StringRef S = Name.split('$').first;
|
|
|
|
auto It = Config->Merge.find(S);
|
|
|
|
if (It == Config->Merge.end())
|
|
|
|
return S;
|
|
|
|
return It->second;
|
|
|
|
}
|
|
|
|
|
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-09-17 05:40:47 +08:00
|
|
|
auto *SC = dyn_cast<SectionChunk>(C);
|
|
|
|
if (SC && !SC->isLive()) {
|
|
|
|
if (Config->Verbose)
|
|
|
|
SC->printDiscardedMessage();
|
|
|
|
continue;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
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.
|
2015-07-28 08:17:25 +08:00
|
|
|
SmallDenseMap<StringRef, OutputSection *> Sections;
|
2015-07-05 07:37:32 +08:00
|
|
|
for (auto Pair : Map) {
|
|
|
|
StringRef Name = getOutputSection(Pair.first);
|
|
|
|
OutputSection *&Sec = Sections[Name];
|
|
|
|
if (!Sec) {
|
2015-07-09 00:37:50 +08:00
|
|
|
Sec = new (CAlloc.Allocate()) OutputSection(Name);
|
2015-06-08 16:26:28 +08:00
|
|
|
OutputSections.push_back(Sec);
|
|
|
|
}
|
2015-07-05 07:37:32 +08:00
|
|
|
std::vector<Chunk *> &Chunks = Pair.second;
|
2015-05-29 03:09:30 +08:00
|
|
|
for (Chunk *C : Chunks) {
|
|
|
|
Sec->addChunk(C);
|
|
|
|
Sec->addPermissions(C->getPermissions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 11:31:47 +08:00
|
|
|
void Writer::createMiscChunks() {
|
2015-07-25 07:51:14 +08:00
|
|
|
// Create thunks for locally-dllimported symbols.
|
|
|
|
if (!Symtab->LocalImportChunks.empty()) {
|
|
|
|
OutputSection *Sec = createSection(".rdata");
|
|
|
|
for (Chunk *C : Symtab->LocalImportChunks)
|
|
|
|
Sec->addChunk(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create SEH table. x86-only.
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine != I386)
|
2015-06-25 11:31:47 +08:00
|
|
|
return;
|
2015-07-25 07:51:14 +08:00
|
|
|
std::set<Defined *> Handlers;
|
2015-08-06 07:43:53 +08:00
|
|
|
for (lld::coff::ObjectFile *File : Symtab->ObjectFiles) {
|
2015-07-25 07:51:14 +08:00
|
|
|
if (!File->SEHCompat)
|
|
|
|
return;
|
|
|
|
for (SymbolBody *B : File->SEHandlers)
|
|
|
|
Handlers.insert(cast<Defined>(B->repl()));
|
|
|
|
}
|
|
|
|
SEHTable.reset(new SEHTableChunk(Handlers));
|
|
|
|
createSection(".rdata")->addChunk(SEHTable.get());
|
2015-06-25 11:31:47 +08:00
|
|
|
}
|
|
|
|
|
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-08-17 16:30:31 +08:00
|
|
|
|
|
|
|
// Initialize DLLOrder so that import entries are ordered in
|
|
|
|
// the same order as in the command line. (That affects DLL
|
|
|
|
// initialization order, and this ordering is MSVC-compatible.)
|
2015-09-02 15:27:31 +08:00
|
|
|
for (ImportFile *File : Symtab->ImportFiles) {
|
|
|
|
std::string DLL = StringRef(File->DLLName).lower();
|
|
|
|
if (Config->DLLOrder.count(DLL) == 0)
|
|
|
|
Config->DLLOrder[DLL] = Config->DLLOrder.size();
|
|
|
|
}
|
2015-08-17 16:30:31 +08:00
|
|
|
|
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-08-17 15:27:45 +08:00
|
|
|
if (DefinedImportThunk *Thunk = File->ThunkSym)
|
|
|
|
Text->addChunk(Thunk->getChunk());
|
2015-09-03 22:49:47 +08:00
|
|
|
if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
|
2015-08-17 16:30:31 +08:00
|
|
|
DelayIdata.add(File->ImpSym);
|
2015-08-17 15:27:45 +08:00
|
|
|
} else {
|
2015-08-17 16:30:31 +08:00
|
|
|
Idata.add(File->ImpSym);
|
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()) {
|
2015-07-14 06:31:45 +08:00
|
|
|
Defined *Helper = cast<Defined>(Config->DelayLoadHelper->repl());
|
2015-07-02 11:59:04 +08:00
|
|
|
DelayIdata.create(Helper);
|
2015-06-22 06:31:52 +08:00
|
|
|
OutputSection *Sec = createSection(".didat");
|
2015-06-27 05:40:15 +08:00
|
|
|
for (Chunk *C : DelayIdata.getChunks())
|
|
|
|
Sec->addChunk(C);
|
|
|
|
Sec = createSection(".data");
|
|
|
|
for (Chunk *C : DelayIdata.getDataChunks())
|
2015-06-22 06:31:52 +08:00
|
|
|
Sec->addChunk(C);
|
|
|
|
Sec = createSection(".text");
|
|
|
|
for (std::unique_ptr<Chunk> &C : DelayIdata.getCodeChunks())
|
|
|
|
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());
|
2015-07-09 00:37:50 +08:00
|
|
|
uint32_t Idx = 1;
|
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
Sec->SectionIndex = Idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Writer::addEntryToStringTable(StringRef Str) {
|
|
|
|
assert(Str.size() > COFF::NameSize);
|
|
|
|
size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field
|
|
|
|
Strtab.insert(Strtab.end(), Str.begin(), Str.end());
|
|
|
|
Strtab.push_back('\0');
|
|
|
|
return OffsetOfEntry;
|
|
|
|
}
|
|
|
|
|
2015-07-28 07:40:20 +08:00
|
|
|
Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
|
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(Def))
|
2015-09-17 07:55:52 +08:00
|
|
|
if (!D->getChunk()->isLive())
|
2015-07-28 07:40:20 +08:00
|
|
|
return None;
|
2015-07-14 04:56:31 +08:00
|
|
|
|
|
|
|
coff_symbol16 Sym;
|
2015-07-28 07:40:20 +08:00
|
|
|
StringRef Name = Def->getName();
|
2015-07-14 04:56:31 +08:00
|
|
|
if (Name.size() > COFF::NameSize) {
|
|
|
|
Sym.Name.Offset.Zeroes = 0;
|
|
|
|
Sym.Name.Offset.Offset = addEntryToStringTable(Name);
|
|
|
|
} else {
|
|
|
|
memset(Sym.Name.ShortName, 0, COFF::NameSize);
|
|
|
|
memcpy(Sym.Name.ShortName, Name.data(), Name.size());
|
|
|
|
}
|
2015-07-28 07:40:20 +08:00
|
|
|
|
|
|
|
if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
|
|
|
|
COFFSymbolRef Ref = D->getCOFFSymbol();
|
|
|
|
Sym.Type = Ref.getType();
|
|
|
|
Sym.StorageClass = Ref.getStorageClass();
|
|
|
|
} else {
|
|
|
|
Sym.Type = IMAGE_SYM_TYPE_NULL;
|
|
|
|
Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
|
|
|
|
}
|
2015-07-14 04:56:31 +08:00
|
|
|
Sym.NumberOfAuxSymbols = 0;
|
2015-07-28 07:40:20 +08:00
|
|
|
|
|
|
|
switch (Def->kind()) {
|
|
|
|
case SymbolBody::DefinedAbsoluteKind:
|
|
|
|
case SymbolBody::DefinedRelativeKind:
|
|
|
|
Sym.Value = Def->getRVA();
|
|
|
|
Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
uint64_t RVA = Def->getRVA();
|
|
|
|
OutputSection *Sec = nullptr;
|
|
|
|
for (OutputSection *S : OutputSections) {
|
|
|
|
if (S->getRVA() > RVA)
|
|
|
|
break;
|
|
|
|
Sec = S;
|
|
|
|
}
|
|
|
|
Sym.Value = RVA - Sec->getRVA();
|
|
|
|
Sym.SectionNumber = Sec->SectionIndex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 04:56:31 +08:00
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-07-09 00:37:50 +08:00
|
|
|
void Writer::createSymbolAndStringTable() {
|
2015-09-22 07:43:31 +08:00
|
|
|
if (!Config->Debug || !Config->WriteSymtab)
|
2015-07-09 00:37:50 +08:00
|
|
|
return;
|
2015-09-22 07:43:31 +08:00
|
|
|
|
2015-07-09 00:37:50 +08:00
|
|
|
// Name field in the section table is 8 byte long. Longer names need
|
|
|
|
// to be written to the string table. First, construct string table.
|
|
|
|
for (OutputSection *Sec : OutputSections) {
|
|
|
|
StringRef Name = Sec->getName();
|
|
|
|
if (Name.size() <= COFF::NameSize)
|
|
|
|
continue;
|
|
|
|
Sec->setStringTableOff(addEntryToStringTable(Name));
|
|
|
|
}
|
|
|
|
|
2015-08-06 07:43:53 +08:00
|
|
|
for (lld::coff::ObjectFile *File : Symtab->ObjectFiles)
|
2015-07-14 04:56:31 +08:00
|
|
|
for (SymbolBody *B : File->getSymbols())
|
2015-07-28 07:40:20 +08:00
|
|
|
if (auto *D = dyn_cast<Defined>(B))
|
|
|
|
if (Optional<coff_symbol16> Sym = createSymbol(D))
|
|
|
|
OutputSymtab.push_back(*Sym);
|
|
|
|
|
|
|
|
for (ImportFile *File : Symtab->ImportFiles)
|
|
|
|
for (SymbolBody *B : File->getSymbols())
|
|
|
|
if (Optional<coff_symbol16> Sym = createSymbol(cast<Defined>(B)))
|
|
|
|
OutputSymtab.push_back(*Sym);
|
2015-07-09 00:37:50 +08:00
|
|
|
|
|
|
|
OutputSection *LastSection = OutputSections.back();
|
|
|
|
// We position the symbol table to be adjacent to the end of the last section.
|
2016-01-15 04:53:50 +08:00
|
|
|
uint64_t FileOff = LastSection->getFileOff() +
|
|
|
|
alignTo(LastSection->getRawSize(), SectorSize);
|
2015-07-09 00:37:50 +08:00
|
|
|
if (!OutputSymtab.empty()) {
|
|
|
|
PointerToSymbolTable = FileOff;
|
|
|
|
FileOff += OutputSymtab.size() * sizeof(coff_symbol16);
|
|
|
|
}
|
|
|
|
if (!Strtab.empty())
|
|
|
|
FileOff += Strtab.size() + 4;
|
2016-01-15 04:53:50 +08:00
|
|
|
FileSize = alignTo(FileOff, SectorSize);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Visits all sections to assign incremental, non-overlapping RVAs and
|
|
|
|
// file offsets.
|
|
|
|
void Writer::assignAddresses() {
|
2015-07-08 09:45:29 +08:00
|
|
|
SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
|
|
|
|
sizeof(data_directory) * NumberfOfDataDirectory +
|
|
|
|
sizeof(coff_section) * OutputSections.size();
|
2015-07-10 00:40:39 +08:00
|
|
|
SizeOfHeaders +=
|
|
|
|
Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
|
2016-01-15 04:53:50 +08:00
|
|
|
SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize);
|
2015-05-29 03:09:30 +08:00
|
|
|
uint64_t RVA = 0x1000; // The first page is kept unmapped.
|
2015-08-12 07:09:00 +08:00
|
|
|
FileSize = SizeOfHeaders;
|
2015-07-31 04:26:55 +08:00
|
|
|
// Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
|
|
|
|
// the loader cannot handle holes.
|
|
|
|
std::stable_partition(
|
|
|
|
OutputSections.begin(), OutputSections.end(), [](OutputSection *S) {
|
|
|
|
return (S->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0;
|
|
|
|
});
|
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);
|
2015-08-12 07:09:00 +08:00
|
|
|
Sec->setFileOffset(FileSize);
|
2016-01-15 04:53:50 +08:00
|
|
|
RVA += alignTo(Sec->getVirtualSize(), PageSize);
|
|
|
|
FileSize += alignTo(Sec->getRawSize(), SectorSize);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2016-01-15 04:53:50 +08:00
|
|
|
SizeOfImage = SizeOfHeaders + alignTo(RVA - 0x1000, PageSize);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 09:45:29 +08:00
|
|
|
template <typename PEHeaderTy> void Writer::writeHeader() {
|
2015-05-29 03:09:30 +08:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Write COFF header
|
|
|
|
auto *COFF = reinterpret_cast<coff_file_header *>(Buf);
|
|
|
|
Buf += sizeof(*COFF);
|
2015-07-26 05:54:50 +08:00
|
|
|
COFF->Machine = Config->Machine;
|
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;
|
2015-07-28 11:12:00 +08:00
|
|
|
if (Config->LargeAddressAware)
|
2015-07-08 09:45:29 +08:00
|
|
|
COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
|
2015-07-28 11:12:00 +08:00
|
|
|
if (!Config->is64())
|
2015-07-09 09:25:49 +08:00
|
|
|
COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
|
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 =
|
2015-07-08 09:45:29 +08:00
|
|
|
sizeof(PEHeaderTy) + sizeof(data_directory) * NumberfOfDataDirectory;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Write PE header
|
2015-07-08 09:45:29 +08:00
|
|
|
auto *PE = reinterpret_cast<PEHeaderTy *>(Buf);
|
2015-05-29 03:09:30 +08:00
|
|
|
Buf += sizeof(*PE);
|
2015-07-10 00:40:39 +08:00
|
|
|
PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
|
2015-05-29 03:09:30 +08:00
|
|
|
PE->ImageBase = Config->ImageBase;
|
2015-08-12 07:09:00 +08:00
|
|
|
PE->SectionAlignment = PageSize;
|
|
|
|
PE->FileAlignment = SectorSize;
|
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-29 03:56:30 +08:00
|
|
|
if (!Config->NoEntry) {
|
2015-07-02 08:21:11 +08:00
|
|
|
Defined *Entry = cast<Defined>(Config->Entry->repl());
|
2015-06-29 03:56:30 +08:00
|
|
|
PE->AddressOfEntryPoint = Entry->getRVA();
|
2015-07-25 10:25:14 +08:00
|
|
|
// Pointer to thumb code must have the LSB set, so adjust it.
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine == ARMNT)
|
2015-07-25 10:25:14 +08:00
|
|
|
PE->AddressOfEntryPoint |= 1;
|
2015-06-29 03:56:30 +08:00
|
|
|
}
|
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-07-29 06:56:02 +08:00
|
|
|
if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) {
|
2015-07-07 01:45:22 +08:00
|
|
|
if (Defined *B = dyn_cast<Defined>(Sym->Body)) {
|
2015-07-06 09:48:01 +08:00
|
|
|
Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA();
|
|
|
|
Dir[TLS_TABLE].Size = 40;
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 06:43:53 +08:00
|
|
|
if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) {
|
2015-07-17 02:30:35 +08:00
|
|
|
if (Defined *B = dyn_cast<Defined>(Sym->Body)) {
|
|
|
|
Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA();
|
2015-07-30 00:30:31 +08:00
|
|
|
Dir[LOAD_CONFIG_TABLE].Size = Config->is64() ? 112 : 64;
|
2015-07-17 02:30:35 +08:00
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2015-07-09 00:37:50 +08:00
|
|
|
if (OutputSymtab.empty())
|
2015-05-31 03:09:50 +08:00
|
|
|
return;
|
2015-07-09 00:37:50 +08:00
|
|
|
|
|
|
|
COFF->PointerToSymbolTable = PointerToSymbolTable;
|
|
|
|
uint32_t NumberOfSymbols = OutputSymtab.size();
|
|
|
|
COFF->NumberOfSymbols = NumberOfSymbols;
|
|
|
|
auto *SymbolTable = reinterpret_cast<coff_symbol16 *>(
|
|
|
|
Buffer->getBufferStart() + COFF->PointerToSymbolTable);
|
|
|
|
for (size_t I = 0; I != NumberOfSymbols; ++I)
|
|
|
|
SymbolTable[I] = OutputSymtab[I];
|
|
|
|
// Create the string table, it follows immediately after the symbol table.
|
|
|
|
// The first 4 bytes is length including itself.
|
|
|
|
Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]);
|
2015-05-31 03:09:50 +08:00
|
|
|
write32le(Buf, Strtab.size() + 4);
|
|
|
|
memcpy(Buf + 4, Strtab.data(), Strtab.size());
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void Writer::openFile(StringRef Path) {
|
2015-08-13 08:31:46 +08:00
|
|
|
ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
|
|
|
FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable);
|
|
|
|
error(BufferOrErr, Twine("failed to open ") + Path);
|
|
|
|
Buffer = std::move(*BufferOrErr);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 07:51:14 +08:00
|
|
|
void Writer::fixSafeSEHSymbols() {
|
|
|
|
if (!SEHTable)
|
|
|
|
return;
|
|
|
|
Config->SEHTable->setRVA(SEHTable->getRVA());
|
|
|
|
Config->SEHCount->setVA(SEHTable->getSize() / 4);
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// 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-08-14 11:30:59 +08:00
|
|
|
uint8_t *SecBuf = Buf + Sec->getFileOff();
|
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)
|
2015-08-14 11:30:59 +08:00
|
|
|
memset(SecBuf, 0xCC, Sec->getRawSize());
|
2015-09-19 06:07:10 +08:00
|
|
|
parallel_for_each(Sec->getChunks().begin(), Sec->getChunks().end(),
|
|
|
|
[&](Chunk *C) { C->writeTo(SecBuf); });
|
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() {
|
2015-08-06 11:45:27 +08:00
|
|
|
OutputSection *Sec = findSection(".pdata");
|
|
|
|
if (!Sec)
|
|
|
|
return;
|
|
|
|
// We assume .pdata contains function table entries only.
|
|
|
|
uint8_t *Begin = Buffer->getBufferStart() + Sec->getFileOff();
|
|
|
|
uint8_t *End = Begin + Sec->getVirtualSize();
|
|
|
|
if (Config->Machine == AMD64) {
|
2015-06-21 12:00:54 +08:00
|
|
|
struct Entry { ulittle32_t Begin, End, Unwind; };
|
2015-09-19 07:17:34 +08:00
|
|
|
parallel_sort(
|
|
|
|
(Entry *)Begin, (Entry *)End,
|
|
|
|
[](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
|
2015-08-06 11:45:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Config->Machine == ARMNT) {
|
|
|
|
struct Entry { ulittle32_t Begin, Unwind; };
|
2015-09-19 07:17:34 +08:00
|
|
|
parallel_sort(
|
|
|
|
(Entry *)Begin, (Entry *)End,
|
|
|
|
[](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
|
2015-08-06 11:45:27 +08:00
|
|
|
return;
|
2015-06-21 12:00:54 +08:00
|
|
|
}
|
2015-08-06 11:45:27 +08:00
|
|
|
errs() << "warning: don't know how to handle .pdata.\n";
|
2015-06-21 12:00:54 +08:00
|
|
|
}
|
|
|
|
|
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-07-09 00:37:50 +08:00
|
|
|
auto Sec = new (CAlloc.Allocate()) OutputSection(Name);
|
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) {
|
2015-07-25 09:44:32 +08:00
|
|
|
std::vector<Baserel> V;
|
2015-06-15 09:23:58 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
|
|
|
if (Sec == Dest)
|
|
|
|
continue;
|
|
|
|
// Collect all locations for base relocations.
|
|
|
|
for (Chunk *C : Sec->getChunks())
|
2015-07-25 06:58:44 +08:00
|
|
|
C->getBaserels(&V);
|
2015-06-15 09:23:58 +08:00
|
|
|
// 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.
|
2015-07-25 09:44:32 +08:00
|
|
|
void Writer::addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V) {
|
2015-06-15 09:23:58 +08:00
|
|
|
const uint32_t Mask = ~uint32_t(PageSize - 1);
|
2015-07-25 09:44:32 +08:00
|
|
|
uint32_t Page = V[0].RVA & Mask;
|
2015-06-15 09:23:58 +08:00
|
|
|
size_t I = 0, J = 1;
|
|
|
|
for (size_t E = V.size(); J < E; ++J) {
|
2015-07-25 09:44:32 +08:00
|
|
|
uint32_t P = V[J].RVA & Mask;
|
2015-06-15 09:23:58 +08:00
|
|
|
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));
|
|
|
|
}
|