diff options
Diffstat (limited to 'lld/COFF/DLL.cpp')
-rw-r--r-- | lld/COFF/DLL.cpp | 564 |
1 files changed, 282 insertions, 282 deletions
diff --git a/lld/COFF/DLL.cpp b/lld/COFF/DLL.cpp index 769f02e7d0d..bdc5bcb1c83 100644 --- a/lld/COFF/DLL.cpp +++ b/lld/COFF/DLL.cpp @@ -37,41 +37,41 @@ namespace { // A chunk for the import descriptor table. class HintNameChunk : public NonSectionChunk { public: - HintNameChunk(StringRef N, uint16_t H) : Name(N), Hint(H) {} + HintNameChunk(StringRef n, uint16_t h) : name(n), hint(h) {} size_t getSize() const override { // Starts with 2 byte Hint field, followed by a null-terminated string, // ends with 0 or 1 byte padding. - return alignTo(Name.size() + 3, 2); + 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()); + 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; + StringRef name; + uint16_t hint; }; // A chunk for the import descriptor table. class LookupChunk : public NonSectionChunk { public: - explicit LookupChunk(Chunk *C) : HintName(C) { - setAlignment(Config->Wordsize); + explicit LookupChunk(Chunk *c) : hintName(c) { + setAlignment(config->wordsize); } - size_t getSize() const override { return Config->Wordsize; } + size_t getSize() const override { return config->wordsize; } - void writeTo(uint8_t *Buf) const override { - if (Config->is64()) - write64le(Buf, HintName->getRVA()); + void writeTo(uint8_t *buf) const override { + if (config->is64()) + write64le(buf, hintName->getRVA()); else - write32le(Buf, HintName->getRVA()); + write32le(buf, hintName->getRVA()); } - Chunk *HintName; + Chunk *hintName; }; // A chunk for the import descriptor table. @@ -79,82 +79,82 @@ public: // See Microsoft PE/COFF spec 7.1. Import Header for details. class OrdinalOnlyChunk : public NonSectionChunk { public: - explicit OrdinalOnlyChunk(uint16_t V) : Ordinal(V) { - setAlignment(Config->Wordsize); + explicit OrdinalOnlyChunk(uint16_t v) : ordinal(v) { + setAlignment(config->wordsize); } - size_t getSize() const override { return Config->Wordsize; } + size_t getSize() const override { return config->wordsize; } - void writeTo(uint8_t *Buf) const override { + void writeTo(uint8_t *buf) const override { // An import-by-ordinal slot has MSB 1 to indicate that // this is import-by-ordinal (and not import-by-name). - if (Config->is64()) { - write64le(Buf, (1ULL << 63) | Ordinal); + if (config->is64()) { + write64le(buf, (1ULL << 63) | ordinal); } else { - write32le(Buf, (1ULL << 31) | Ordinal); + write32le(buf, (1ULL << 31) | ordinal); } } - uint16_t Ordinal; + uint16_t ordinal; }; // A chunk for the import descriptor table. class ImportDirectoryChunk : public NonSectionChunk { public: - explicit ImportDirectoryChunk(Chunk *N) : DLLName(N) {} + explicit ImportDirectoryChunk(Chunk *n) : dllName(n) {} size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); } - void writeTo(uint8_t *Buf) const override { - memset(Buf, 0, getSize()); + 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(); + 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; + Chunk *dllName; + Chunk *lookupTab; + Chunk *addressTab; }; // A chunk representing null terminator in the import table. // Contents of this chunk is always null bytes. class NullChunk : public NonSectionChunk { public: - explicit NullChunk(size_t N) : Size(N) { HasData = false; } - size_t getSize() const override { return Size; } + 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); + void writeTo(uint8_t *buf) const override { + memset(buf, 0, size); } private: - size_t Size; + size_t size; }; static std::vector<std::vector<DefinedImportData *>> -binImports(const std::vector<DefinedImportData *> &Imports) { +binImports(const std::vector<DefinedImportData *> &imports) { // Group DLL-imported symbols by DLL name because that's how // symbols are layed out in the import descriptor table. - auto Less = [](const std::string &A, const std::string &B) { - return Config->DLLOrder[A] < Config->DLLOrder[B]; + auto less = [](const std::string &a, const std::string &b) { + return config->dllOrder[a] < config->dllOrder[b]; }; std::map<std::string, std::vector<DefinedImportData *>, - bool(*)(const std::string &, const std::string &)> M(Less); - for (DefinedImportData *Sym : Imports) - M[Sym->getDLLName().lower()].push_back(Sym); + bool(*)(const std::string &, const std::string &)> 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<std::vector<DefinedImportData *>> v; + for (auto &kv : m) { // Sort symbols by name for each group. - std::vector<DefinedImportData *> &Syms = KV.second; - std::sort(Syms.begin(), Syms.end(), - [](DefinedImportData *A, DefinedImportData *B) { - return A->getName() < B->getName(); + std::vector<DefinedImportData *> &syms = kv.second; + std::sort(syms.begin(), syms.end(), + [](DefinedImportData *a, DefinedImportData *b) { + return a->getName() < b->getName(); }); - V.push_back(std::move(Syms)); + v.push_back(std::move(syms)); } - return V; + return v; } // Export table @@ -163,34 +163,34 @@ binImports(const std::vector<DefinedImportData *> &Imports) { // A chunk for the delay import descriptor table etnry. class DelayDirectoryChunk : public NonSectionChunk { public: - explicit DelayDirectoryChunk(Chunk *N) : DLLName(N) {} + explicit DelayDirectoryChunk(Chunk *n) : dllName(n) {} size_t getSize() const override { return sizeof(delay_import_directory_table_entry); } - void writeTo(uint8_t *Buf) const override { - memset(Buf, 0, getSize()); + 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(); + 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; + Chunk *dllName; + Chunk *moduleHandle; + Chunk *addressTab; + Chunk *nameTab; }; // Initial contents for delay-loaded functions. // This code calls __delayLoadHelper2 function to resolve a symbol // and then overwrites its jump table slot with the result // for subsequent function calls. -static const uint8_t ThunkX64[] = { +static const uint8_t thunkX64[] = { 0x51, // push rcx 0x52, // push rdx 0x41, 0x50, // push r8 @@ -215,7 +215,7 @@ static const uint8_t ThunkX64[] = { 0xFF, 0xE0, // jmp rax }; -static const uint8_t ThunkX86[] = { +static const uint8_t thunkX86[] = { 0x51, // push ecx 0x52, // push edx 0x68, 0, 0, 0, 0, // push offset ___imp__<FUNCNAME> @@ -226,7 +226,7 @@ static const uint8_t ThunkX86[] = { 0xFF, 0xE0, // jmp eax }; -static const uint8_t ThunkARM[] = { +static const uint8_t thunkARM[] = { 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 __imp_<FUNCNAME> 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 __imp_<FUNCNAME> 0x2d, 0xe9, 0x0f, 0x48, // push.w {r0, r1, r2, r3, r11, lr} @@ -242,7 +242,7 @@ static const uint8_t ThunkARM[] = { 0x60, 0x47, // bx ip }; -static const uint8_t ThunkARM64[] = { +static const uint8_t thunkARM64[] = { 0x11, 0x00, 0x00, 0x90, // adrp x17, #0 __imp_<FUNCNAME> 0x31, 0x02, 0x00, 0x91, // add x17, x17, #0 :lo12:__imp_<FUNCNAME> 0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]! @@ -275,117 +275,117 @@ static const uint8_t ThunkARM64[] = { // A chunk for the delay import thunk. class ThunkChunkX64 : public NonSectionChunk { public: - ThunkChunkX64(Defined *I, Chunk *D, Defined *H) - : Imp(I), Desc(D), Helper(H) {} + ThunkChunkX64(Defined *i, Chunk *d, Defined *h) + : imp(i), desc(d), helper(h) {} - size_t getSize() const override { return sizeof(ThunkX64); } + size_t getSize() const override { return sizeof(thunkX64); } - void writeTo(uint8_t *Buf) const override { - memcpy(Buf, ThunkX64, sizeof(ThunkX64)); - write32le(Buf + 36, Imp->getRVA() - RVA - 40); - write32le(Buf + 43, Desc->getRVA() - RVA - 47); - write32le(Buf + 48, Helper->getRVA() - RVA - 52); + void writeTo(uint8_t *buf) const override { + memcpy(buf, thunkX64, sizeof(thunkX64)); + write32le(buf + 36, imp->getRVA() - rva - 40); + write32le(buf + 43, desc->getRVA() - rva - 47); + write32le(buf + 48, helper->getRVA() - rva - 52); } - Defined *Imp = nullptr; - Chunk *Desc = nullptr; - Defined *Helper = nullptr; + Defined *imp = nullptr; + Chunk *desc = nullptr; + Defined *helper = nullptr; }; class ThunkChunkX86 : public NonSectionChunk { public: - ThunkChunkX86(Defined *I, Chunk *D, Defined *H) - : Imp(I), Desc(D), Helper(H) {} + ThunkChunkX86(Defined *i, Chunk *d, Defined *h) + : imp(i), desc(d), helper(h) {} - size_t getSize() const override { return sizeof(ThunkX86); } + size_t getSize() const override { return sizeof(thunkX86); } - void writeTo(uint8_t *Buf) const override { - memcpy(Buf, ThunkX86, sizeof(ThunkX86)); - write32le(Buf + 3, Imp->getRVA() + Config->ImageBase); - write32le(Buf + 8, Desc->getRVA() + Config->ImageBase); - write32le(Buf + 13, Helper->getRVA() - RVA - 17); + void writeTo(uint8_t *buf) const override { + memcpy(buf, thunkX86, sizeof(thunkX86)); + write32le(buf + 3, imp->getRVA() + config->imageBase); + write32le(buf + 8, desc->getRVA() + config->imageBase); + write32le(buf + 13, helper->getRVA() - rva - 17); } - void getBaserels(std::vector<Baserel> *Res) override { - Res->emplace_back(RVA + 3); - Res->emplace_back(RVA + 8); + void getBaserels(std::vector<Baserel> *res) override { + res->emplace_back(rva + 3); + res->emplace_back(rva + 8); } - Defined *Imp = nullptr; - Chunk *Desc = nullptr; - Defined *Helper = nullptr; + Defined *imp = nullptr; + Chunk *desc = nullptr; + Defined *helper = nullptr; }; class ThunkChunkARM : public NonSectionChunk { public: - ThunkChunkARM(Defined *I, Chunk *D, Defined *H) - : Imp(I), Desc(D), Helper(H) {} + ThunkChunkARM(Defined *i, Chunk *d, Defined *h) + : imp(i), desc(d), helper(h) {} - size_t getSize() const override { return sizeof(ThunkARM); } + size_t getSize() const override { return sizeof(thunkARM); } - void writeTo(uint8_t *Buf) const override { - memcpy(Buf, ThunkARM, sizeof(ThunkARM)); - applyMOV32T(Buf + 0, Imp->getRVA() + Config->ImageBase); - applyMOV32T(Buf + 22, Desc->getRVA() + Config->ImageBase); - applyBranch24T(Buf + 30, Helper->getRVA() - RVA - 34); + void writeTo(uint8_t *buf) const override { + memcpy(buf, thunkARM, sizeof(thunkARM)); + applyMOV32T(buf + 0, imp->getRVA() + config->imageBase); + applyMOV32T(buf + 22, desc->getRVA() + config->imageBase); + applyBranch24T(buf + 30, helper->getRVA() - rva - 34); } - void getBaserels(std::vector<Baserel> *Res) override { - Res->emplace_back(RVA + 0, IMAGE_REL_BASED_ARM_MOV32T); - Res->emplace_back(RVA + 22, IMAGE_REL_BASED_ARM_MOV32T); + void getBaserels(std::vector<Baserel> *res) override { + res->emplace_back(rva + 0, IMAGE_REL_BASED_ARM_MOV32T); + res->emplace_back(rva + 22, IMAGE_REL_BASED_ARM_MOV32T); } - Defined *Imp = nullptr; - Chunk *Desc = nullptr; - Defined *Helper = nullptr; + Defined *imp = nullptr; + Chunk *desc = nullptr; + Defined *helper = nullptr; }; class ThunkChunkARM64 : public NonSectionChunk { public: - ThunkChunkARM64(Defined *I, Chunk *D, Defined *H) - : Imp(I), Desc(D), Helper(H) {} - - size_t getSize() const override { return sizeof(ThunkARM64); } - - 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); - applyArm64Addr(Buf + 52, Desc->getRVA(), RVA + 52, 12); - applyArm64Imm(Buf + 56, Desc->getRVA() & 0xfff, 0); - applyArm64Branch26(Buf + 60, Helper->getRVA() - RVA - 60); + ThunkChunkARM64(Defined *i, Chunk *d, Defined *h) + : imp(i), desc(d), helper(h) {} + + size_t getSize() const override { return sizeof(thunkARM64); } + + 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); + applyArm64Addr(buf + 52, desc->getRVA(), rva + 52, 12); + applyArm64Imm(buf + 56, desc->getRVA() & 0xfff, 0); + applyArm64Branch26(buf + 60, helper->getRVA() - rva - 60); } - Defined *Imp = nullptr; - Chunk *Desc = nullptr; - Defined *Helper = nullptr; + Defined *imp = nullptr; + Chunk *desc = nullptr; + Defined *helper = nullptr; }; // A chunk for the import descriptor table. class DelayAddressChunk : public NonSectionChunk { public: - explicit DelayAddressChunk(Chunk *C) : Thunk(C) { - setAlignment(Config->Wordsize); + explicit DelayAddressChunk(Chunk *c) : thunk(c) { + setAlignment(config->wordsize); } - size_t getSize() const override { return Config->Wordsize; } + size_t getSize() const override { return config->wordsize; } - void writeTo(uint8_t *Buf) const override { - if (Config->is64()) { - write64le(Buf, Thunk->getRVA() + Config->ImageBase); + void writeTo(uint8_t *buf) const override { + if (config->is64()) { + write64le(buf, thunk->getRVA() + config->imageBase); } else { - uint32_t Bit = 0; + uint32_t bit = 0; // Pointer to thumb code must have the LSB set, so adjust it. - if (Config->Machine == ARMNT) - Bit = 1; - write32le(Buf, (Thunk->getRVA() + Config->ImageBase) | Bit); + if (config->machine == ARMNT) + bit = 1; + write32le(buf, (thunk->getRVA() + config->imageBase) | bit); } } - void getBaserels(std::vector<Baserel> *Res) override { - Res->emplace_back(RVA); + void getBaserels(std::vector<Baserel> *res) override { + res->emplace_back(rva); } - Chunk *Thunk; + Chunk *thunk; }; // Export table @@ -394,248 +394,248 @@ public: // A chunk for the export descriptor table. class ExportDirectoryChunk : public NonSectionChunk { public: - ExportDirectoryChunk(int I, int J, Chunk *D, Chunk *A, Chunk *N, Chunk *O) - : MaxOrdinal(I), NameTabSize(J), DLLName(D), AddressTab(A), NameTab(N), - OrdinalTab(O) {} + ExportDirectoryChunk(int i, int j, Chunk *d, Chunk *a, Chunk *n, Chunk *o) + : maxOrdinal(i), nameTabSize(j), 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 = 0; - E->AddressTableEntries = MaxOrdinal + 1; - E->NumberOfNamePointers = NameTabSize; - E->ExportAddressTableRVA = AddressTab->getRVA(); - E->NamePointerRVA = NameTab->getRVA(); - E->OrdinalTableRVA = OrdinalTab->getRVA(); + void writeTo(uint8_t *buf) const override { + memset(buf, 0, getSize()); + + auto *e = (export_directory_table_entry *)(buf); + e->NameRVA = dllName->getRVA(); + e->OrdinalBase = 0; + e->AddressTableEntries = maxOrdinal + 1; + e->NumberOfNamePointers = nameTabSize; + e->ExportAddressTableRVA = addressTab->getRVA(); + e->NamePointerRVA = nameTab->getRVA(); + e->OrdinalTableRVA = ordinalTab->getRVA(); } - uint16_t MaxOrdinal; - uint16_t NameTabSize; - Chunk *DLLName; - Chunk *AddressTab; - Chunk *NameTab; - Chunk *OrdinalTab; + uint16_t maxOrdinal; + uint16_t nameTabSize; + Chunk *dllName; + Chunk *addressTab; + Chunk *nameTab; + Chunk *ordinalTab; }; class AddressTableChunk : public NonSectionChunk { public: - explicit AddressTableChunk(size_t MaxOrdinal) : Size(MaxOrdinal + 1) {} - size_t getSize() const override { return Size * 4; } + explicit AddressTableChunk(size_t maxOrdinal) : size(maxOrdinal + 1) {} + size_t getSize() const override { return size * 4; } - void writeTo(uint8_t *Buf) const override { - memset(Buf, 0, getSize()); + void writeTo(uint8_t *buf) const override { + memset(buf, 0, getSize()); - for (const Export &E : Config->Exports) { - uint8_t *P = Buf + E.Ordinal * 4; - uint32_t Bit = 0; + for (const Export &e : config->exports) { + uint8_t *p = buf + e.ordinal * 4; + uint32_t bit = 0; // Pointer to thumb code must have the LSB set, so adjust it. - if (Config->Machine == ARMNT && !E.Data) - Bit = 1; - if (E.ForwardChunk) { - write32le(P, E.ForwardChunk->getRVA() | Bit); + if (config->machine == ARMNT && !e.data) + bit = 1; + if (e.forwardChunk) { + write32le(p, e.forwardChunk->getRVA() | bit); } else { - write32le(P, cast<Defined>(E.Sym)->getRVA() | Bit); + write32le(p, cast<Defined>(e.sym)->getRVA() | bit); } } } private: - size_t Size; + size_t size; }; class NamePointersChunk : public NonSectionChunk { public: - explicit NamePointersChunk(std::vector<Chunk *> &V) : Chunks(V) {} - size_t getSize() const override { return Chunks.size() * 4; } + 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; + void writeTo(uint8_t *buf) const override { + for (Chunk *c : chunks) { + write32le(buf, c->getRVA()); + buf += 4; } } private: - std::vector<Chunk *> Chunks; + std::vector<Chunk *> chunks; }; class ExportOrdinalChunk : public NonSectionChunk { public: - explicit ExportOrdinalChunk(size_t I) : Size(I) {} - size_t getSize() const override { return Size * 2; } + explicit ExportOrdinalChunk(size_t i) : size(i) {} + size_t getSize() const override { return size * 2; } - void writeTo(uint8_t *Buf) const override { - for (Export &E : Config->Exports) { - if (E.Noname) + void writeTo(uint8_t *buf) const override { + for (Export &e : config->exports) { + if (e.noname) continue; - write16le(Buf, E.Ordinal); - Buf += 2; + write16le(buf, e.ordinal); + buf += 2; } } private: - size_t Size; + size_t size; }; } // anonymous namespace void IdataContents::create() { - std::vector<std::vector<DefinedImportData *>> V = binImports(Imports); + std::vector<std::vector<DefinedImportData *>> v = binImports(imports); // Create .idata contents for each DLL. - for (std::vector<DefinedImportData *> &Syms : V) { + for (std::vector<DefinedImportData *> &syms : v) { // Create lookup and address tables. If they have external names, // we need to create HintName chunks to store the names. // If they don't (if they are import-by-ordinals), we store only // ordinal values to the table. - size_t Base = Lookups.size(); - for (DefinedImportData *S : Syms) { - uint16_t Ord = S->getOrdinal(); - if (S->getExternalName().empty()) { - Lookups.push_back(make<OrdinalOnlyChunk>(Ord)); - Addresses.push_back(make<OrdinalOnlyChunk>(Ord)); + size_t base = lookups.size(); + for (DefinedImportData *s : syms) { + uint16_t ord = s->getOrdinal(); + if (s->getExternalName().empty()) { + lookups.push_back(make<OrdinalOnlyChunk>(ord)); + addresses.push_back(make<OrdinalOnlyChunk>(ord)); continue; } - auto *C = make<HintNameChunk>(S->getExternalName(), Ord); - Lookups.push_back(make<LookupChunk>(C)); - Addresses.push_back(make<LookupChunk>(C)); - Hints.push_back(C); + auto *c = make<HintNameChunk>(s->getExternalName(), ord); + lookups.push_back(make<LookupChunk>(c)); + addresses.push_back(make<LookupChunk>(c)); + hints.push_back(c); } // Terminate with null values. - Lookups.push_back(make<NullChunk>(Config->Wordsize)); - Addresses.push_back(make<NullChunk>(Config->Wordsize)); + lookups.push_back(make<NullChunk>(config->wordsize)); + addresses.push_back(make<NullChunk>(config->wordsize)); - for (int I = 0, E = Syms.size(); I < E; ++I) - Syms[I]->setLocation(Addresses[Base + I]); + for (int i = 0, e = syms.size(); i < e; ++i) + syms[i]->setLocation(addresses[base + i]); // Create the import table header. - 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); + 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); } // Add null terminator. - Dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry))); + 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 *> 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; + 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); + return dirs.size() * sizeof(delay_import_directory_table_entry); } -void DelayLoadContents::create(Defined *H) { - Helper = H; - std::vector<std::vector<DefinedImportData *>> V = binImports(Imports); +void DelayLoadContents::create(Defined *h) { + helper = h; + std::vector<std::vector<DefinedImportData *>> v = binImports(imports); // Create .didat contents for each DLL. - for (std::vector<DefinedImportData *> &Syms : V) { + for (std::vector<DefinedImportData *> &syms : v) { // Create the delay import table header. - DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName())); - auto *Dir = make<DelayDirectoryChunk>(DLLNames.back()); - - size_t Base = Addresses.size(); - for (DefinedImportData *S : Syms) { - Chunk *T = newThunkChunk(S, Dir); - auto *A = make<DelayAddressChunk>(T); - Addresses.push_back(A); - Thunks.push_back(T); - StringRef ExtName = S->getExternalName(); - if (ExtName.empty()) { - Names.push_back(make<OrdinalOnlyChunk>(S->getOrdinal())); + dllNames.push_back(make<StringChunk>(syms[0]->getDLLName())); + auto *dir = make<DelayDirectoryChunk>(dllNames.back()); + + size_t base = addresses.size(); + for (DefinedImportData *s : syms) { + Chunk *t = newThunkChunk(s, dir); + auto *a = make<DelayAddressChunk>(t); + addresses.push_back(a); + thunks.push_back(t); + StringRef extName = s->getExternalName(); + if (extName.empty()) { + names.push_back(make<OrdinalOnlyChunk>(s->getOrdinal())); } else { - auto *C = make<HintNameChunk>(ExtName, 0); - Names.push_back(make<LookupChunk>(C)); - HintNames.push_back(C); + auto *c = make<HintNameChunk>(extName, 0); + names.push_back(make<LookupChunk>(c)); + hintNames.push_back(c); } } // Terminate with null values. - Addresses.push_back(make<NullChunk>(8)); - Names.push_back(make<NullChunk>(8)); + 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); + 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); // Fill the delay import table header fields. - Dir->ModuleHandle = MH; - Dir->AddressTab = Addresses[Base]; - Dir->NameTab = Names[Base]; - Dirs.push_back(Dir); + dir->moduleHandle = mh; + dir->addressTab = addresses[base]; + dir->nameTab = names[base]; + dirs.push_back(dir); } // Add null terminator. - Dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry))); + dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry))); } -Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *S, Chunk *Dir) { - switch (Config->Machine) { +Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s, Chunk *dir) { + switch (config->machine) { case AMD64: - return make<ThunkChunkX64>(S, Dir, Helper); + return make<ThunkChunkX64>(s, dir, helper); case I386: - return make<ThunkChunkX86>(S, Dir, Helper); + return make<ThunkChunkX86>(s, dir, helper); case ARMNT: - return make<ThunkChunkARM>(S, Dir, Helper); + return make<ThunkChunkARM>(s, dir, helper); case ARM64: - return make<ThunkChunkARM64>(S, Dir, Helper); + return make<ThunkChunkARM64>(s, dir, helper); default: llvm_unreachable("unsupported machine type"); } } EdataContents::EdataContents() { - uint16_t MaxOrdinal = 0; - for (Export &E : Config->Exports) - MaxOrdinal = std::max(MaxOrdinal, E.Ordinal); - - auto *DLLName = make<StringChunk>(sys::path::filename(Config->OutputFile)); - auto *AddressTab = make<AddressTableChunk>(MaxOrdinal); - std::vector<Chunk *> Names; - for (Export &E : Config->Exports) - if (!E.Noname) - Names.push_back(make<StringChunk>(E.ExportName)); - - std::vector<Chunk *> Forwards; - for (Export &E : Config->Exports) { - if (E.ForwardTo.empty()) + uint16_t maxOrdinal = 0; + for (Export &e : config->exports) + maxOrdinal = std::max(maxOrdinal, e.ordinal); + + auto *dllName = make<StringChunk>(sys::path::filename(config->outputFile)); + auto *addressTab = make<AddressTableChunk>(maxOrdinal); + std::vector<Chunk *> names; + for (Export &e : config->exports) + if (!e.noname) + names.push_back(make<StringChunk>(e.exportName)); + + std::vector<Chunk *> forwards; + for (Export &e : config->exports) { + if (e.forwardTo.empty()) continue; - E.ForwardChunk = make<StringChunk>(E.ForwardTo); - Forwards.push_back(E.ForwardChunk); + e.forwardChunk = make<StringChunk>(e.forwardTo); + forwards.push_back(e.forwardChunk); } - auto *NameTab = make<NamePointersChunk>(Names); - auto *OrdinalTab = make<ExportOrdinalChunk>(Names.size()); - auto *Dir = make<ExportDirectoryChunk>(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()); + auto *nameTab = make<NamePointersChunk>(names); + auto *ordinalTab = make<ExportOrdinalChunk>(names.size()); + auto *dir = make<ExportDirectoryChunk>(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()); } } // namespace coff |