#include "DLL.h"
#include "COFFLinkerContext.h"
#include "Chunks.h"
#include "SymbolTable.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Object/COFF.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Path.h"
using namespace llvm;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace llvm::COFF;
namespace lld::coff {
namespace {
class HintNameChunk : public NonSectionChunk {
public:
HintNameChunk(StringRef n, uint16_t h) : name(n), hint(h) {}
size_t getSize() const override {
return alignTo(name.size() + 3, 2);
}
void writeTo(uint8_t *buf) const override {
memset(buf, 0, getSize());
write16le(buf, hint);
memcpy(buf + 2, name.data(), name.size());
}
private:
StringRef name;
uint16_t hint;
};
class LookupChunk : public NonSectionChunk {
public:
explicit LookupChunk(COFFLinkerContext &ctx, Chunk *c)
: hintName(c), ctx(ctx) {
setAlignment(ctx.config.wordsize);
}
size_t getSize() const override { return ctx.config.wordsize; }
void writeTo(uint8_t *buf) const override {
if (ctx.config.is64())
write64le(buf, hintName->getRVA());
else
write32le(buf, hintName->getRVA());
}
Chunk *hintName;
private:
COFFLinkerContext &ctx;
};
class OrdinalOnlyChunk : public NonSectionChunk {
public:
explicit OrdinalOnlyChunk(COFFLinkerContext &c, uint16_t v)
: ordinal(v), ctx(c) {
setAlignment(ctx.config.wordsize);
}
size_t getSize() const override { return ctx.config.wordsize; }
void writeTo(uint8_t *buf) const override {
if (ctx.config.is64()) {
write64le(buf, (1ULL << 63) | ordinal);
} else {
write32le(buf, (1ULL << 31) | ordinal);
}
}
uint16_t ordinal;
private:
COFFLinkerContext &ctx;
};
class ImportDirectoryChunk : public NonSectionChunk {
public:
explicit ImportDirectoryChunk(Chunk *n) : dllName(n) { setAlignment(4); }
size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
void writeTo(uint8_t *buf) const override {
memset(buf, 0, getSize());
auto *e = (coff_import_directory_table_entry *)(buf);
e->ImportLookupTableRVA = lookupTab->getRVA();
e->NameRVA = dllName->getRVA();
e->ImportAddressTableRVA = addressTab->getRVA();
}
Chunk *dllName;
Chunk *lookupTab;
Chunk *addressTab;
};
class NullChunk : public NonSectionChunk {
public:
explicit NullChunk(size_t n) : size(n) { hasData = false; }
size_t getSize() const override { return size; }
void writeTo(uint8_t *buf) const override {
memset(buf, 0, size);
}
private:
size_t size;
};
static std::vector<std::vector<DefinedImportData *>>
binImports(COFFLinkerContext &ctx,
const std::vector<DefinedImportData *> &imports) {
auto less = [&ctx](const std::string &a, const std::string &b) {
return ctx.config.dllOrder[a] < ctx.config.dllOrder[b];
};
std::map<std::string, std::vector<DefinedImportData *>, decltype(less)> m(
less);
for (DefinedImportData *sym : imports)
m[sym->getDLLName().lower()].push_back(sym);
std::vector<std::vector<DefinedImportData *>> v;
for (auto &kv : m) {
std::vector<DefinedImportData *> &syms = kv.second;
llvm::sort(syms, [](DefinedImportData *a, DefinedImportData *b) {
return a->getName() < b->getName();
});
v.push_back(std::move(syms));
}
return v;
}
class DelayDirectoryChunk : public NonSectionChunk {
public:
explicit DelayDirectoryChunk(Chunk *n) : dllName(n) { setAlignment(4); }
size_t getSize() const override {
return sizeof(delay_import_directory_table_entry);
}
void writeTo(uint8_t *buf) const override {
memset(buf, 0, getSize());
auto *e = (delay_import_directory_table_entry *)(buf);
e->Attributes = 1;
e->Name = dllName->getRVA();
e->ModuleHandle = moduleHandle->getRVA();
e->DelayImportAddressTable = addressTab->getRVA();
e->DelayImportNameTable = nameTab->getRVA();
}
Chunk *dllName;
Chunk *moduleHandle;
Chunk *addressTab;
Chunk *nameTab;
};
static const uint8_t thunkX64[] = {
0x48, 0x8D, 0x05, 0, 0, 0, 0,
0xE9, 0, 0, 0, 0,
};
static const uint8_t tailMergeX64[] = {
0x51,
0x52,
0x41, 0x50,
0x41, 0x51,
0x48, 0x83, 0xEC, 0x48,
0x66, 0x0F, 0x7F, 0x04, 0x24,
0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10,
0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20,
0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30,
0x48, 0x8B, 0xD0,
0x48, 0x8D, 0x0D, 0, 0, 0, 0,
0xE8, 0, 0, 0, 0,
0x66, 0x0F, 0x6F, 0x04, 0x24,
0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10,
0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20,
0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30,
0x48, 0x83, 0xC4, 0x48,
0x41, 0x59,
0x41, 0x58,
0x5A,
0x59,
0xFF, 0xE0,
};
static const uint8_t tailMergeUnwindInfoX64[] = {
0x01,
0x0a,
0x05,
0x00,
0x0a, 0x82,
0x06, 0x02,
0x04, 0x02,
0x02, 0x02,
0x01, 0x02,
0x00, 0x00
};
static const uint8_t thunkX86[] = {
0xB8, 0, 0, 0, 0,
0xE9, 0, 0, 0, 0,
};
static const uint8_t tailMergeX86[] = {
0x51,
0x52,
0x50,
0x68, 0, 0, 0, 0,
0xE8, 0, 0, 0, 0,
0x5A,
0x59,
0xFF, 0xE0,
};
static const uint8_t thunkARM[] = {
0x40, 0xf2, 0x00, 0x0c,
0xc0, 0xf2, 0x00, 0x0c,
0x00, 0xf0, 0x00, 0xb8,
};
static const uint8_t tailMergeARM[] = {
0x2d, 0xe9, 0x0f, 0x48,
0x0d, 0xf2, 0x10, 0x0b,
0x2d, 0xed, 0x10, 0x0b,
0x61, 0x46,
0x40, 0xf2, 0x00, 0x00,
0xc0, 0xf2, 0x00, 0x00,
0x00, 0xf0, 0x00, 0xd0,
0x84, 0x46,
0xbd, 0xec, 0x10, 0x0b,
0xbd, 0xe8, 0x0f, 0x48,
0x60, 0x47,
};
static const uint8_t thunkARM64[] = {
0x11, 0x00, 0x00, 0x90,
0x31, 0x02, 0x00, 0x91,
0x00, 0x00, 0x00, 0x14,
};
static const uint8_t tailMergeARM64[] = {
0xfd, 0x7b, 0xb3, 0xa9,
0xfd, 0x03, 0x00, 0x91,
0xe0, 0x07, 0x01, 0xa9,
0xe2, 0x0f, 0x02, 0xa9,
0xe4, 0x17, 0x03, 0xa9,
0xe6, 0x1f, 0x04, 0xa9,
0xe0, 0x87, 0x02, 0xad,
0xe2, 0x8f, 0x03, 0xad,
0xe4, 0x97, 0x04, 0xad,
0xe6, 0x9f, 0x05, 0xad,
0xe1, 0x03, 0x11, 0xaa,
0x00, 0x00, 0x00, 0x90,
0x00, 0x00, 0x00, 0x91,
0x00, 0x00, 0x00, 0x94,
0xf0, 0x03, 0x00, 0xaa,
0xe6, 0x9f, 0x45, 0xad,
0xe4, 0x97, 0x44, 0xad,
0xe2, 0x8f, 0x43, 0xad,
0xe0, 0x87, 0x42, 0xad,
0xe6, 0x1f, 0x44, 0xa9,
0xe4, 0x17, 0x43, 0xa9,
0xe2, 0x0f, 0x42, 0xa9,
0xe0, 0x07, 0x41, 0xa9,
0xfd, 0x7b, 0xcd, 0xa8,
0x00, 0x02, 0x1f, 0xd6,
};
class ThunkChunkX64 : public NonSectionCodeChunk {
public:
ThunkChunkX64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
size_t getSize() const override { return sizeof(thunkX64); }
MachineTypes getMachine() const override { return AMD64; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, thunkX64, sizeof(thunkX64));
write32le(buf + 3, imp->getRVA() - rva - 7);
write32le(buf + 8, tailMerge->getRVA() - rva - 12);
}
Defined *imp = nullptr;
Chunk *tailMerge = nullptr;
};
class TailMergeChunkX64 : public NonSectionCodeChunk {
public:
TailMergeChunkX64(Chunk *d, Defined *h) : desc(d), helper(h) {}
size_t getSize() const override { return sizeof(tailMergeX64); }
MachineTypes getMachine() const override { return AMD64; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, tailMergeX64, sizeof(tailMergeX64));
write32le(buf + 39, desc->getRVA() - rva - 43);
write32le(buf + 44, helper->getRVA() - rva - 48);
}
Chunk *desc = nullptr;
Defined *helper = nullptr;
};
class TailMergePDataChunkX64 : public NonSectionChunk {
public:
TailMergePDataChunkX64(Chunk *tm, Chunk *unwind) : tm(tm), unwind(unwind) {
setAlignment(4);
}
size_t getSize() const override { return 3 * sizeof(uint32_t); }
void writeTo(uint8_t *buf) const override {
write32le(buf + 0, tm->getRVA());
write32le(buf + 4, tm->getRVA() + tm->getSize());
write32le(buf + 8, unwind->getRVA());
}
Chunk *tm = nullptr;
Chunk *unwind = nullptr;
};
class TailMergeUnwindInfoX64 : public NonSectionChunk {
public:
TailMergeUnwindInfoX64() {
setAlignment(4);
}
size_t getSize() const override { return sizeof(tailMergeUnwindInfoX64); }
void writeTo(uint8_t *buf) const override {
memcpy(buf, tailMergeUnwindInfoX64, sizeof(tailMergeUnwindInfoX64));
}
};
class ThunkChunkX86 : public NonSectionCodeChunk {
public:
ThunkChunkX86(COFFLinkerContext &ctx, Defined *i, Chunk *tm)
: imp(i), tailMerge(tm), ctx(ctx) {}
size_t getSize() const override { return sizeof(thunkX86); }
MachineTypes getMachine() const override { return I386; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, thunkX86, sizeof(thunkX86));
write32le(buf + 1, imp->getRVA() + ctx.config.imageBase);
write32le(buf + 6, tailMerge->getRVA() - rva - 10);
}
void getBaserels(std::vector<Baserel> *res) override {
res->emplace_back(rva + 1, ctx.config.machine);
}
Defined *imp = nullptr;
Chunk *tailMerge = nullptr;
private:
const COFFLinkerContext &ctx;
};
class TailMergeChunkX86 : public NonSectionCodeChunk {
public:
TailMergeChunkX86(COFFLinkerContext &ctx, Chunk *d, Defined *h)
: desc(d), helper(h), ctx(ctx) {}
size_t getSize() const override { return sizeof(tailMergeX86); }
MachineTypes getMachine() const override { return I386; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, tailMergeX86, sizeof(tailMergeX86));
write32le(buf + 4, desc->getRVA() + ctx.config.imageBase);
write32le(buf + 9, helper->getRVA() - rva - 13);
}
void getBaserels(std::vector<Baserel> *res) override {
res->emplace_back(rva + 4, ctx.config.machine);
}
Chunk *desc = nullptr;
Defined *helper = nullptr;
private:
const COFFLinkerContext &ctx;
};
class ThunkChunkARM : public NonSectionCodeChunk {
public:
ThunkChunkARM(COFFLinkerContext &ctx, Defined *i, Chunk *tm)
: imp(i), tailMerge(tm), ctx(ctx) {
setAlignment(2);
}
size_t getSize() const override { return sizeof(thunkARM); }
MachineTypes getMachine() const override { return ARMNT; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, thunkARM, sizeof(thunkARM));
applyMOV32T(buf + 0, imp->getRVA() + ctx.config.imageBase);
applyBranch24T(buf + 8, tailMerge->getRVA() - rva - 12);
}
void getBaserels(std::vector<Baserel> *res) override {
res->emplace_back(rva + 0, IMAGE_REL_BASED_ARM_MOV32T);
}
Defined *imp = nullptr;
Chunk *tailMerge = nullptr;
private:
const COFFLinkerContext &ctx;
};
class TailMergeChunkARM : public NonSectionCodeChunk {
public:
TailMergeChunkARM(COFFLinkerContext &ctx, Chunk *d, Defined *h)
: desc(d), helper(h), ctx(ctx) {
setAlignment(2);
}
size_t getSize() const override { return sizeof(tailMergeARM); }
MachineTypes getMachine() const override { return ARMNT; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, tailMergeARM, sizeof(tailMergeARM));
applyMOV32T(buf + 14, desc->getRVA() + ctx.config.imageBase);
applyBranch24T(buf + 22, helper->getRVA() - rva - 26);
}
void getBaserels(std::vector<Baserel> *res) override {
res->emplace_back(rva + 14, IMAGE_REL_BASED_ARM_MOV32T);
}
Chunk *desc = nullptr;
Defined *helper = nullptr;
private:
const COFFLinkerContext &ctx;
};
class ThunkChunkARM64 : public NonSectionCodeChunk {
public:
ThunkChunkARM64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {
setAlignment(4);
}
size_t getSize() const override { return sizeof(thunkARM64); }
MachineTypes getMachine() const override { return ARM64; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, thunkARM64, sizeof(thunkARM64));
applyArm64Addr(buf + 0, imp->getRVA(), rva + 0, 12);
applyArm64Imm(buf + 4, imp->getRVA() & 0xfff, 0);
applyArm64Branch26(buf + 8, tailMerge->getRVA() - rva - 8);
}
Defined *imp = nullptr;
Chunk *tailMerge = nullptr;
};
class TailMergeChunkARM64 : public NonSectionCodeChunk {
public:
TailMergeChunkARM64(Chunk *d, Defined *h) : desc(d), helper(h) {
setAlignment(4);
}
size_t getSize() const override { return sizeof(tailMergeARM64); }
MachineTypes getMachine() const override { return ARM64; }
void writeTo(uint8_t *buf) const override {
memcpy(buf, tailMergeARM64, sizeof(tailMergeARM64));
applyArm64Addr(buf + 44, desc->getRVA(), rva + 44, 12);
applyArm64Imm(buf + 48, desc->getRVA() & 0xfff, 0);
applyArm64Branch26(buf + 52, helper->getRVA() - rva - 52);
}
Chunk *desc = nullptr;
Defined *helper = nullptr;
};
class DelayAddressChunk : public NonSectionChunk {
public:
explicit DelayAddressChunk(COFFLinkerContext &ctx, Chunk *c)
: thunk(c), ctx(ctx) {
setAlignment(ctx.config.wordsize);
}
size_t getSize() const override { return ctx.config.wordsize; }
void writeTo(uint8_t *buf) const override {
if (ctx.config.is64()) {
write64le(buf, thunk->getRVA() + ctx.config.imageBase);
} else {
uint32_t bit = 0;
if (ctx.config.machine == ARMNT)
bit = 1;
write32le(buf, (thunk->getRVA() + ctx.config.imageBase) | bit);
}
}
void getBaserels(std::vector<Baserel> *res) override {
res->emplace_back(rva, ctx.config.machine);
}
Chunk *thunk;
private:
const COFFLinkerContext &ctx;
};
class ExportDirectoryChunk : public NonSectionChunk {
public:
ExportDirectoryChunk(int baseOrdinal, int maxOrdinal, int nameTabSize,
Chunk *d, Chunk *a, Chunk *n, Chunk *o)
: baseOrdinal(baseOrdinal), maxOrdinal(maxOrdinal),
nameTabSize(nameTabSize), dllName(d), addressTab(a), nameTab(n),
ordinalTab(o) {}
size_t getSize() const override {
return sizeof(export_directory_table_entry);
}
void writeTo(uint8_t *buf) const override {
memset(buf, 0, getSize());
auto *e = (export_directory_table_entry *)(buf);
e->NameRVA = dllName->getRVA();
e->OrdinalBase = baseOrdinal;
e->AddressTableEntries = (maxOrdinal - baseOrdinal) + 1;
e->NumberOfNamePointers = nameTabSize;
e->ExportAddressTableRVA = addressTab->getRVA();
e->NamePointerRVA = nameTab->getRVA();
e->OrdinalTableRVA = ordinalTab->getRVA();
}
uint16_t baseOrdinal;
uint16_t maxOrdinal;
uint16_t nameTabSize;
Chunk *dllName;
Chunk *addressTab;
Chunk *nameTab;
Chunk *ordinalTab;
};
class AddressTableChunk : public NonSectionChunk {
public:
explicit AddressTableChunk(COFFLinkerContext &ctx, size_t baseOrdinal,
size_t maxOrdinal)
: baseOrdinal(baseOrdinal), size((maxOrdinal - baseOrdinal) + 1),
ctx(ctx) {}
size_t getSize() const override { return size * 4; }
void writeTo(uint8_t *buf) const override {
memset(buf, 0, getSize());
for (const Export &e : ctx.config.exports) {
assert(e.ordinal >= baseOrdinal && "Export symbol has invalid ordinal");
uint8_t *p = buf + (e.ordinal - baseOrdinal) * 4;
uint32_t bit = 0;
if (ctx.config.machine == ARMNT && !e.data)
bit = 1;
if (e.forwardChunk) {
write32le(p, e.forwardChunk->getRVA() | bit);
} else {
assert(cast<Defined>(e.sym)->getRVA() != 0 &&
"Exported symbol unmapped");
write32le(p, cast<Defined>(e.sym)->getRVA() | bit);
}
}
}
private:
size_t baseOrdinal;
size_t size;
const COFFLinkerContext &ctx;
};
class NamePointersChunk : public NonSectionChunk {
public:
explicit NamePointersChunk(std::vector<Chunk *> &v) : chunks(v) {}
size_t getSize() const override { return chunks.size() * 4; }
void writeTo(uint8_t *buf) const override {
for (Chunk *c : chunks) {
write32le(buf, c->getRVA());
buf += 4;
}
}
private:
std::vector<Chunk *> chunks;
};
class ExportOrdinalChunk : public NonSectionChunk {
public:
explicit ExportOrdinalChunk(const COFFLinkerContext &ctx, size_t baseOrdinal,
size_t tableSize)
: baseOrdinal(baseOrdinal), size(tableSize), ctx(ctx) {}
size_t getSize() const override { return size * 2; }
void writeTo(uint8_t *buf) const override {
for (const Export &e : ctx.config.exports) {
if (e.noname)
continue;
assert(e.ordinal >= baseOrdinal && "Export symbol has invalid ordinal");
write16le(buf, e.ordinal - baseOrdinal);
buf += 2;
}
}
private:
size_t baseOrdinal;
size_t size;
const COFFLinkerContext &ctx;
};
}
void IdataContents::create(COFFLinkerContext &ctx) {
std::vector<std::vector<DefinedImportData *>> v = binImports(ctx, imports);
for (std::vector<DefinedImportData *> &syms : v) {
size_t base = lookups.size();
for (DefinedImportData *s : syms) {
uint16_t ord = s->getOrdinal();
if (s->getExternalName().empty()) {
lookups.push_back(make<OrdinalOnlyChunk>(ctx, ord));
addresses.push_back(make<OrdinalOnlyChunk>(ctx, ord));
continue;
}
auto *c = make<HintNameChunk>(s->getExternalName(), ord);
lookups.push_back(make<LookupChunk>(ctx, c));
addresses.push_back(make<LookupChunk>(ctx, c));
hints.push_back(c);
}
lookups.push_back(make<NullChunk>(ctx.config.wordsize));
addresses.push_back(make<NullChunk>(ctx.config.wordsize));
for (int i = 0, e = syms.size(); i < e; ++i)
syms[i]->setLocation(addresses[base + i]);
dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
auto *dir = make<ImportDirectoryChunk>(dllNames.back());
dir->lookupTab = lookups[base];
dir->addressTab = addresses[base];
dirs.push_back(dir);
}
dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
}
std::vector<Chunk *> DelayLoadContents::getChunks() {
std::vector<Chunk *> v;
v.insert(v.end(), dirs.begin(), dirs.end());
v.insert(v.end(), names.begin(), names.end());
v.insert(v.end(), hintNames.begin(), hintNames.end());
v.insert(v.end(), dllNames.begin(), dllNames.end());
return v;
}
std::vector<Chunk *> DelayLoadContents::getDataChunks() {
std::vector<Chunk *> v;
v.insert(v.end(), moduleHandles.begin(), moduleHandles.end());
v.insert(v.end(), addresses.begin(), addresses.end());
return v;
}
uint64_t DelayLoadContents::getDirSize() {
return dirs.size() * sizeof(delay_import_directory_table_entry);
}
void DelayLoadContents::create(Defined *h) {
helper = h;
std::vector<std::vector<DefinedImportData *>> v = binImports(ctx, imports);
Chunk *unwind = newTailMergeUnwindInfoChunk();
for (std::vector<DefinedImportData *> &syms : v) {
dllNames.push_back(make<StringChunk>(syms[0]->getDLLName()));
auto *dir = make<DelayDirectoryChunk>(dllNames.back());
size_t base = addresses.size();
Chunk *tm = newTailMergeChunk(dir);
Chunk *pdataChunk = unwind ? newTailMergePDataChunk(tm, unwind) : nullptr;
for (DefinedImportData *s : syms) {
Chunk *t = newThunkChunk(s, tm);
auto *a = make<DelayAddressChunk>(ctx, t);
addresses.push_back(a);
thunks.push_back(t);
StringRef extName = s->getExternalName();
if (extName.empty()) {
names.push_back(make<OrdinalOnlyChunk>(ctx, s->getOrdinal()));
} else {
auto *c = make<HintNameChunk>(extName, 0);
names.push_back(make<LookupChunk>(ctx, c));
hintNames.push_back(c);
StringRef symName = saver().save("__imp___load_" + extName);
s->loadThunkSym =
cast<DefinedSynthetic>(ctx.symtab.addSynthetic(symName, t));
}
}
thunks.push_back(tm);
if (pdataChunk)
pdata.push_back(pdataChunk);
StringRef tmName =
saver().save("__tailMerge_" + syms[0]->getDLLName().lower());
ctx.symtab.addSynthetic(tmName, tm);
addresses.push_back(make<NullChunk>(8));
names.push_back(make<NullChunk>(8));
for (int i = 0, e = syms.size(); i < e; ++i)
syms[i]->setLocation(addresses[base + i]);
auto *mh = make<NullChunk>(8);
mh->setAlignment(8);
moduleHandles.push_back(mh);
dir->moduleHandle = mh;
dir->addressTab = addresses[base];
dir->nameTab = names[base];
dirs.push_back(dir);
}
if (unwind)
unwindinfo.push_back(unwind);
dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
}
Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {
switch (ctx.config.machine) {
case AMD64:
return make<TailMergeChunkX64>(dir, helper);
case I386:
return make<TailMergeChunkX86>(ctx, dir, helper);
case ARMNT:
return make<TailMergeChunkARM>(ctx, dir, helper);
case ARM64:
return make<TailMergeChunkARM64>(dir, helper);
default:
llvm_unreachable("unsupported machine type");
}
}
Chunk *DelayLoadContents::newTailMergeUnwindInfoChunk() {
switch (ctx.config.machine) {
case AMD64:
return make<TailMergeUnwindInfoX64>();
default:
return nullptr;
}
}
Chunk *DelayLoadContents::newTailMergePDataChunk(Chunk *tm, Chunk *unwind) {
switch (ctx.config.machine) {
case AMD64:
return make<TailMergePDataChunkX64>(tm, unwind);
default:
return nullptr;
}
}
Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s,
Chunk *tailMerge) {
switch (ctx.config.machine) {
case AMD64:
return make<ThunkChunkX64>(s, tailMerge);
case I386:
return make<ThunkChunkX86>(ctx, s, tailMerge);
case ARMNT:
return make<ThunkChunkARM>(ctx, s, tailMerge);
case ARM64:
return make<ThunkChunkARM64>(s, tailMerge);
default:
llvm_unreachable("unsupported machine type");
}
}
EdataContents::EdataContents(COFFLinkerContext &ctx) : ctx(ctx) {
unsigned baseOrdinal = 1 << 16, maxOrdinal = 0;
for (Export &e : ctx.config.exports) {
baseOrdinal = std::min(baseOrdinal, (unsigned)e.ordinal);
maxOrdinal = std::max(maxOrdinal, (unsigned)e.ordinal);
}
assert(baseOrdinal >= 1);
auto *dllName = make<StringChunk>(sys::path::filename(ctx.config.outputFile));
auto *addressTab = make<AddressTableChunk>(ctx, baseOrdinal, maxOrdinal);
std::vector<Chunk *> names;
for (Export &e : ctx.config.exports)
if (!e.noname)
names.push_back(make<StringChunk>(e.exportName));
std::vector<Chunk *> forwards;
for (Export &e : ctx.config.exports) {
if (e.forwardTo.empty())
continue;
e.forwardChunk = make<StringChunk>(e.forwardTo);
forwards.push_back(e.forwardChunk);
}
auto *nameTab = make<NamePointersChunk>(names);
auto *ordinalTab = make<ExportOrdinalChunk>(ctx, baseOrdinal, names.size());
auto *dir =
make<ExportDirectoryChunk>(baseOrdinal, maxOrdinal, names.size(), dllName,
addressTab, nameTab, ordinalTab);
chunks.push_back(dir);
chunks.push_back(dllName);
chunks.push_back(addressTab);
chunks.push_back(nameTab);
chunks.push_back(ordinalTab);
chunks.insert(chunks.end(), names.begin(), names.end());
chunks.insert(chunks.end(), forwards.begin(), forwards.end());
}
}