2015-06-07 09:15:04 +08:00
|
|
|
//===- DLL.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-06-22 06:31:52 +08:00
|
|
|
// This file defines various types of chunks for the DLL import or export
|
|
|
|
// descriptor tables. They are inherently Windows-specific.
|
2015-06-07 09:15:04 +08:00
|
|
|
// You need to read Microsoft PE/COFF spec to understand details
|
|
|
|
// about the data structures.
|
|
|
|
//
|
|
|
|
// If you are not particularly interested in linking against Windows
|
|
|
|
// DLL, you can skip this file, and you should still be able to
|
|
|
|
// understand the rest of the linker.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DLL.h"
|
2018-02-21 05:53:18 +08:00
|
|
|
#include "Chunks.h"
|
2015-06-07 09:15:04 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
2015-06-17 08:16:33 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-06-07 09:15:04 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
|
|
|
using namespace llvm::COFF;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
2015-08-11 07:02:57 +08:00
|
|
|
namespace {
|
2015-06-07 09:15:04 +08:00
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Import table
|
|
|
|
|
2015-07-09 08:45:50 +08:00
|
|
|
static int ptrSize() { return Config->is64() ? 8 : 4; }
|
|
|
|
|
2015-06-07 09:15:04 +08:00
|
|
|
// A chunk for the import descriptor table.
|
|
|
|
class HintNameChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
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.
|
2016-01-15 04:53:50 +08:00
|
|
|
return alignTo(Name.size() + 3, 2);
|
2015-06-07 09:15:04 +08:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
write16le(Buf + OutputSectionOff, Hint);
|
|
|
|
memcpy(Buf + OutputSectionOff + 2, Name.data(), Name.size());
|
2015-06-07 09:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef Name;
|
|
|
|
uint16_t Hint;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A chunk for the import descriptor table.
|
|
|
|
class LookupChunk : public Chunk {
|
|
|
|
public:
|
2017-09-14 05:54:55 +08:00
|
|
|
explicit LookupChunk(Chunk *C) : HintName(C) { Alignment = ptrSize(); }
|
2015-07-09 08:45:50 +08:00
|
|
|
size_t getSize() const override { return ptrSize(); }
|
2015-06-07 09:15:04 +08:00
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
write32le(Buf + OutputSectionOff, HintName->getRVA());
|
2015-06-07 09:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk *HintName;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A chunk for the import descriptor table.
|
|
|
|
// This chunk represent import-by-ordinal symbols.
|
|
|
|
// See Microsoft PE/COFF spec 7.1. Import Header for details.
|
|
|
|
class OrdinalOnlyChunk : public Chunk {
|
|
|
|
public:
|
2017-09-14 05:54:55 +08:00
|
|
|
explicit OrdinalOnlyChunk(uint16_t V) : Ordinal(V) { Alignment = ptrSize(); }
|
2015-07-09 08:45:50 +08:00
|
|
|
size_t getSize() const override { return ptrSize(); }
|
2015-06-07 09:15:04 +08:00
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-06-07 09:15:04 +08:00
|
|
|
// An import-by-ordinal slot has MSB 1 to indicate that
|
|
|
|
// this is import-by-ordinal (and not import-by-name).
|
2015-07-09 08:45:50 +08:00
|
|
|
if (Config->is64()) {
|
2015-08-14 11:30:59 +08:00
|
|
|
write64le(Buf + OutputSectionOff, (1ULL << 63) | Ordinal);
|
2015-07-09 08:45:50 +08:00
|
|
|
} else {
|
2015-08-14 11:30:59 +08:00
|
|
|
write32le(Buf + OutputSectionOff, (1ULL << 31) | Ordinal);
|
2015-07-09 08:45:50 +08:00
|
|
|
}
|
2015-06-07 09:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Ordinal;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A chunk for the import descriptor table.
|
2015-06-17 08:16:33 +08:00
|
|
|
class ImportDirectoryChunk : public Chunk {
|
2015-06-07 09:15:04 +08:00
|
|
|
public:
|
2015-06-17 08:16:33 +08:00
|
|
|
explicit ImportDirectoryChunk(Chunk *N) : DLLName(N) {}
|
|
|
|
size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
|
2015-06-07 09:15:04 +08:00
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
auto *E = (coff_import_directory_table_entry *)(Buf + OutputSectionOff);
|
2017-06-03 02:49:38 +08:00
|
|
|
E->ImportLookupTableRVA = LookupTab->getRVA();
|
2015-06-07 09:15:04 +08:00
|
|
|
E->NameRVA = DLLName->getRVA();
|
|
|
|
E->ImportAddressTableRVA = AddressTab->getRVA();
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk *DLLName;
|
2017-06-03 02:49:38 +08:00
|
|
|
Chunk *LookupTab;
|
2015-06-07 09:15:04 +08:00
|
|
|
Chunk *AddressTab;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A chunk representing null terminator in the import table.
|
|
|
|
// Contents of this chunk is always null bytes.
|
|
|
|
class NullChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
explicit NullChunk(size_t N) : Size(N) {}
|
|
|
|
bool hasData() const override { return false; }
|
|
|
|
size_t getSize() const override { return Size; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t Size;
|
|
|
|
};
|
|
|
|
|
2015-08-17 16:30:31 +08:00
|
|
|
static std::vector<std::vector<DefinedImportData *>>
|
2015-06-22 06:31:52 +08:00
|
|
|
binImports(const std::vector<DefinedImportData *> &Imports) {
|
2015-06-07 09:15:04 +08:00
|
|
|
// Group DLL-imported symbols by DLL name because that's how
|
|
|
|
// symbols are layed out in the import descriptor table.
|
2015-09-02 15:27:31 +08:00
|
|
|
auto Less = [](const std::string &A, const std::string &B) {
|
2015-08-17 16:30:31 +08:00
|
|
|
return Config->DLLOrder[A] < Config->DLLOrder[B];
|
|
|
|
};
|
2015-09-02 15:27:31 +08:00
|
|
|
std::map<std::string, std::vector<DefinedImportData *>,
|
|
|
|
bool(*)(const std::string &, const std::string &)> M(Less);
|
2015-06-07 09:15:04 +08:00
|
|
|
for (DefinedImportData *Sym : Imports)
|
2015-09-02 15:27:31 +08:00
|
|
|
M[Sym->getDLLName().lower()].push_back(Sym);
|
2015-06-07 09:15:04 +08:00
|
|
|
|
2015-08-17 16:30:31 +08:00
|
|
|
std::vector<std::vector<DefinedImportData *>> V;
|
2017-05-19 01:03:49 +08:00
|
|
|
for (auto &KV : M) {
|
2015-06-07 09:15:04 +08:00
|
|
|
// Sort symbols by name for each group.
|
2017-05-19 01:03:49 +08:00
|
|
|
std::vector<DefinedImportData *> &Syms = KV.second;
|
2015-06-07 09:15:04 +08:00
|
|
|
std::sort(Syms.begin(), Syms.end(),
|
|
|
|
[](DefinedImportData *A, DefinedImportData *B) {
|
|
|
|
return A->getName() < B->getName();
|
|
|
|
});
|
2015-08-17 16:30:31 +08:00
|
|
|
V.push_back(std::move(Syms));
|
2015-06-22 06:31:52 +08:00
|
|
|
}
|
2015-08-17 16:30:31 +08:00
|
|
|
return V;
|
2015-06-22 06:31:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Export table
|
|
|
|
// See Microsoft PE/COFF spec 4.3 for details.
|
|
|
|
|
|
|
|
// A chunk for the delay import descriptor table etnry.
|
|
|
|
class DelayDirectoryChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
explicit DelayDirectoryChunk(Chunk *N) : DLLName(N) {}
|
|
|
|
|
|
|
|
size_t getSize() const override {
|
|
|
|
return sizeof(delay_import_directory_table_entry);
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
auto *E = (delay_import_directory_table_entry *)(Buf + OutputSectionOff);
|
2015-06-27 05:40:15 +08:00
|
|
|
E->Attributes = 1;
|
2015-06-22 06:31:52 +08:00
|
|
|
E->Name = DLLName->getRVA();
|
|
|
|
E->ModuleHandle = ModuleHandle->getRVA();
|
|
|
|
E->DelayImportAddressTable = AddressTab->getRVA();
|
|
|
|
E->DelayImportNameTable = NameTab->getRVA();
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk *DLLName;
|
|
|
|
Chunk *ModuleHandle;
|
|
|
|
Chunk *AddressTab;
|
|
|
|
Chunk *NameTab;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initial contents for delay-loaded functions.
|
2015-06-23 01:26:27 +08:00
|
|
|
// This code calls __delayLoadHelper2 function to resolve a symbol
|
2015-06-22 06:31:52 +08:00
|
|
|
// and then overwrites its jump table slot with the result
|
|
|
|
// for subsequent function calls.
|
2015-07-16 06:26:57 +08:00
|
|
|
static const uint8_t ThunkX64[] = {
|
2015-06-22 06:31:52 +08:00
|
|
|
0x51, // push rcx
|
|
|
|
0x52, // push rdx
|
|
|
|
0x41, 0x50, // push r8
|
|
|
|
0x41, 0x51, // push r9
|
|
|
|
0x48, 0x83, 0xEC, 0x48, // sub rsp, 48h
|
|
|
|
0x66, 0x0F, 0x7F, 0x04, 0x24, // movdqa xmmword ptr [rsp], xmm0
|
|
|
|
0x66, 0x0F, 0x7F, 0x4C, 0x24, 0x10, // movdqa xmmword ptr [rsp+10h], xmm1
|
|
|
|
0x66, 0x0F, 0x7F, 0x54, 0x24, 0x20, // movdqa xmmword ptr [rsp+20h], xmm2
|
|
|
|
0x66, 0x0F, 0x7F, 0x5C, 0x24, 0x30, // movdqa xmmword ptr [rsp+30h], xmm3
|
|
|
|
0x48, 0x8D, 0x15, 0, 0, 0, 0, // lea rdx, [__imp_<FUNCNAME>]
|
|
|
|
0x48, 0x8D, 0x0D, 0, 0, 0, 0, // lea rcx, [___DELAY_IMPORT_...]
|
|
|
|
0xE8, 0, 0, 0, 0, // call __delayLoadHelper2
|
|
|
|
0x66, 0x0F, 0x6F, 0x04, 0x24, // movdqa xmm0, xmmword ptr [rsp]
|
|
|
|
0x66, 0x0F, 0x6F, 0x4C, 0x24, 0x10, // movdqa xmm1, xmmword ptr [rsp+10h]
|
|
|
|
0x66, 0x0F, 0x6F, 0x54, 0x24, 0x20, // movdqa xmm2, xmmword ptr [rsp+20h]
|
|
|
|
0x66, 0x0F, 0x6F, 0x5C, 0x24, 0x30, // movdqa xmm3, xmmword ptr [rsp+30h]
|
|
|
|
0x48, 0x83, 0xC4, 0x48, // add rsp, 48h
|
|
|
|
0x41, 0x59, // pop r9
|
|
|
|
0x41, 0x58, // pop r8
|
|
|
|
0x5A, // pop rdx
|
|
|
|
0x59, // pop rcx
|
|
|
|
0xFF, 0xE0, // jmp rax
|
|
|
|
};
|
|
|
|
|
2015-07-16 06:26:57 +08:00
|
|
|
static const uint8_t ThunkX86[] = {
|
|
|
|
0x51, // push ecx
|
|
|
|
0x52, // push edx
|
|
|
|
0x68, 0, 0, 0, 0, // push offset ___imp__<FUNCNAME>
|
|
|
|
0x68, 0, 0, 0, 0, // push offset ___DELAY_IMPORT_DESCRIPTOR_<DLLNAME>_dll
|
|
|
|
0xE8, 0, 0, 0, 0, // call ___delayLoadHelper2@8
|
|
|
|
0x5A, // pop edx
|
|
|
|
0x59, // pop ecx
|
|
|
|
0xFF, 0xE0, // jmp eax
|
|
|
|
};
|
|
|
|
|
2017-07-26 04:00:37 +08:00
|
|
|
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}
|
|
|
|
0x0d, 0xf2, 0x10, 0x0b, // addw r11, sp, #16
|
|
|
|
0x2d, 0xed, 0x10, 0x0b, // vpush {d0, d1, d2, d3, d4, d5, d6, d7}
|
|
|
|
0x61, 0x46, // mov r1, ip
|
|
|
|
0x40, 0xf2, 0x00, 0x00, // mov.w r0, #0 DELAY_IMPORT_DESCRIPTOR
|
|
|
|
0xc0, 0xf2, 0x00, 0x00, // mov.t r0, #0 DELAY_IMPORT_DESCRIPTOR
|
|
|
|
0x00, 0xf0, 0x00, 0xd0, // bl #0 __delayLoadHelper2
|
|
|
|
0x84, 0x46, // mov ip, r0
|
|
|
|
0xbd, 0xec, 0x10, 0x0b, // vpop {d0, d1, d2, d3, d4, d5, d6, d7}
|
|
|
|
0xbd, 0xe8, 0x0f, 0x48, // pop.w {r0, r1, r2, r3, r11, lr}
|
|
|
|
0x60, 0x47, // bx ip
|
|
|
|
};
|
|
|
|
|
2015-06-22 06:31:52 +08:00
|
|
|
// A chunk for the delay import thunk.
|
2015-07-16 06:26:57 +08:00
|
|
|
class ThunkChunkX64 : public Chunk {
|
2015-06-22 06:31:52 +08:00
|
|
|
public:
|
2015-07-16 06:26:57 +08:00
|
|
|
ThunkChunkX64(Defined *I, Chunk *D, Defined *H)
|
|
|
|
: Imp(I), Desc(D), Helper(H) {}
|
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(ThunkX64); }
|
2015-06-22 06:31:52 +08:00
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, ThunkX64, sizeof(ThunkX64));
|
|
|
|
write32le(Buf + OutputSectionOff + 36, Imp->getRVA() - RVA - 40);
|
|
|
|
write32le(Buf + OutputSectionOff + 43, Desc->getRVA() - RVA - 47);
|
|
|
|
write32le(Buf + OutputSectionOff + 48, Helper->getRVA() - RVA - 52);
|
2015-06-22 06:31:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Defined *Imp = nullptr;
|
|
|
|
Chunk *Desc = nullptr;
|
|
|
|
Defined *Helper = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-07-16 06:26:57 +08:00
|
|
|
class ThunkChunkX86 : public Chunk {
|
|
|
|
public:
|
|
|
|
ThunkChunkX86(Defined *I, Chunk *D, Defined *H)
|
|
|
|
: Imp(I), Desc(D), Helper(H) {}
|
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(ThunkX86); }
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
memcpy(Buf + OutputSectionOff, ThunkX86, sizeof(ThunkX86));
|
|
|
|
write32le(Buf + OutputSectionOff + 3, Imp->getRVA() + Config->ImageBase);
|
|
|
|
write32le(Buf + OutputSectionOff + 8, Desc->getRVA() + Config->ImageBase);
|
|
|
|
write32le(Buf + OutputSectionOff + 13, Helper->getRVA() - RVA - 17);
|
2015-07-16 06:26:57 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:44:32 +08:00
|
|
|
void getBaserels(std::vector<Baserel> *Res) override {
|
|
|
|
Res->emplace_back(RVA + 3);
|
|
|
|
Res->emplace_back(RVA + 8);
|
2015-07-16 06:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Defined *Imp = nullptr;
|
|
|
|
Chunk *Desc = nullptr;
|
|
|
|
Defined *Helper = nullptr;
|
|
|
|
};
|
|
|
|
|
2017-07-26 04:00:37 +08:00
|
|
|
class ThunkChunkARM : public Chunk {
|
|
|
|
public:
|
|
|
|
ThunkChunkARM(Defined *I, Chunk *D, Defined *H)
|
|
|
|
: Imp(I), Desc(D), Helper(H) {}
|
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(ThunkARM); }
|
|
|
|
|
|
|
|
void writeTo(uint8_t *Buf) const override {
|
|
|
|
memcpy(Buf + OutputSectionOff, ThunkARM, sizeof(ThunkARM));
|
|
|
|
applyMOV32T(Buf + OutputSectionOff + 0, Imp->getRVA() + Config->ImageBase);
|
|
|
|
applyMOV32T(Buf + OutputSectionOff + 22, Desc->getRVA() + Config->ImageBase);
|
|
|
|
applyBranch24T(Buf + OutputSectionOff + 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Defined *Imp = nullptr;
|
|
|
|
Chunk *Desc = nullptr;
|
|
|
|
Defined *Helper = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-06-27 05:40:15 +08:00
|
|
|
// A chunk for the import descriptor table.
|
|
|
|
class DelayAddressChunk : public Chunk {
|
|
|
|
public:
|
2017-09-14 05:54:55 +08:00
|
|
|
explicit DelayAddressChunk(Chunk *C) : Thunk(C) { Alignment = ptrSize(); }
|
2015-07-28 10:54:18 +08:00
|
|
|
size_t getSize() const override { return ptrSize(); }
|
2015-06-27 05:40:15 +08:00
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-07-28 10:54:18 +08:00
|
|
|
if (Config->is64()) {
|
2015-08-14 11:30:59 +08:00
|
|
|
write64le(Buf + OutputSectionOff, Thunk->getRVA() + Config->ImageBase);
|
2015-07-28 10:54:18 +08:00
|
|
|
} else {
|
2017-07-26 04:00:37 +08:00
|
|
|
uint32_t Bit = 0;
|
|
|
|
// Pointer to thumb code must have the LSB set, so adjust it.
|
|
|
|
if (Config->Machine == ARMNT)
|
|
|
|
Bit = 1;
|
|
|
|
write32le(Buf + OutputSectionOff, (Thunk->getRVA() + Config->ImageBase) | Bit);
|
2015-07-28 10:54:18 +08:00
|
|
|
}
|
2015-06-27 05:40:15 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:44:32 +08:00
|
|
|
void getBaserels(std::vector<Baserel> *Res) override {
|
|
|
|
Res->emplace_back(RVA);
|
2015-06-27 06:05:32 +08:00
|
|
|
}
|
|
|
|
|
2015-06-27 05:40:15 +08:00
|
|
|
Chunk *Thunk;
|
|
|
|
};
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Export table
|
|
|
|
// Read Microsoft PE/COFF spec 5.3 for details.
|
|
|
|
|
|
|
|
// A chunk for the export descriptor table.
|
|
|
|
class ExportDirectoryChunk : public Chunk {
|
|
|
|
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) {}
|
|
|
|
|
|
|
|
size_t getSize() const override {
|
|
|
|
return sizeof(export_directory_table_entry);
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
auto *E = (export_directory_table_entry *)(Buf + OutputSectionOff);
|
2015-06-17 08:16:33 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AddressTableChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
explicit AddressTableChunk(size_t MaxOrdinal) : Size(MaxOrdinal + 1) {}
|
|
|
|
size_t getSize() const override { return Size * 4; }
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2017-12-20 14:50:45 +08:00
|
|
|
for (const Export &E : Config->Exports) {
|
2016-01-09 09:22:00 +08:00
|
|
|
uint8_t *P = Buf + OutputSectionOff + E.Ordinal * 4;
|
2017-12-20 14:50:45 +08:00
|
|
|
uint32_t Bit = 0;
|
|
|
|
// Pointer to thumb code must have the LSB set, so adjust it.
|
|
|
|
if (Config->Machine == ARMNT && !E.Data)
|
|
|
|
Bit = 1;
|
2016-01-09 09:22:00 +08:00
|
|
|
if (E.ForwardChunk) {
|
2017-07-25 14:10:44 +08:00
|
|
|
write32le(P, E.ForwardChunk->getRVA() | Bit);
|
2016-01-09 09:22:00 +08:00
|
|
|
} else {
|
2017-07-25 14:10:44 +08:00
|
|
|
write32le(P, cast<Defined>(E.Sym)->getRVA() | Bit);
|
2016-01-09 09:22:00 +08:00
|
|
|
}
|
2015-06-29 06:16:41 +08:00
|
|
|
}
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t Size;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NamePointersChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
explicit NamePointersChunk(std::vector<Chunk *> &V) : Chunks(V) {}
|
|
|
|
size_t getSize() const override { return Chunks.size() * 4; }
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
uint8_t *P = Buf + OutputSectionOff;
|
2015-06-17 08:16:33 +08:00
|
|
|
for (Chunk *C : Chunks) {
|
|
|
|
write32le(P, C->getRVA());
|
|
|
|
P += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Chunk *> Chunks;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExportOrdinalChunk : public Chunk {
|
|
|
|
public:
|
|
|
|
explicit ExportOrdinalChunk(size_t I) : Size(I) {}
|
|
|
|
size_t getSize() const override { return Size * 2; }
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *Buf) const override {
|
2015-08-14 11:30:59 +08:00
|
|
|
uint8_t *P = Buf + OutputSectionOff;
|
2015-06-17 08:16:33 +08:00
|
|
|
for (Export &E : Config->Exports) {
|
|
|
|
if (E.Noname)
|
|
|
|
continue;
|
|
|
|
write16le(P, E.Ordinal);
|
|
|
|
P += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t Size;
|
|
|
|
};
|
|
|
|
|
2015-08-11 07:02:57 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
uint64_t IdataContents::getDirSize() {
|
|
|
|
return Dirs.size() * sizeof(ImportDirectoryTableEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t IdataContents::getIATSize() {
|
|
|
|
return Addresses.size() * ptrSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a list of .idata contents.
|
|
|
|
// See Microsoft PE/COFF spec 5.4 for details.
|
|
|
|
std::vector<Chunk *> IdataContents::getChunks() {
|
|
|
|
create();
|
2017-05-19 01:03:49 +08:00
|
|
|
|
2015-08-11 07:02:57 +08:00
|
|
|
// The loader assumes a specific order of data.
|
|
|
|
// Add each type in the correct order.
|
2017-05-19 01:03:49 +08:00
|
|
|
std::vector<Chunk *> V;
|
|
|
|
V.insert(V.end(), Dirs.begin(), Dirs.end());
|
2017-06-03 02:49:38 +08:00
|
|
|
V.insert(V.end(), Lookups.begin(), Lookups.end());
|
2017-05-19 01:03:49 +08:00
|
|
|
V.insert(V.end(), Addresses.begin(), Addresses.end());
|
|
|
|
V.insert(V.end(), Hints.begin(), Hints.end());
|
2017-05-19 02:53:39 +08:00
|
|
|
V.insert(V.end(), DLLNames.begin(), DLLNames.end());
|
2015-08-11 07:02:57 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdataContents::create() {
|
2015-08-17 16:30:31 +08:00
|
|
|
std::vector<std::vector<DefinedImportData *>> V = binImports(Imports);
|
2015-08-11 07:02:57 +08:00
|
|
|
|
|
|
|
// Create .idata contents for each DLL.
|
2015-08-17 16:30:31 +08:00
|
|
|
for (std::vector<DefinedImportData *> &Syms : V) {
|
2015-08-11 07:02:57 +08:00
|
|
|
// 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.
|
2017-06-03 02:49:38 +08:00
|
|
|
size_t Base = Lookups.size();
|
2015-08-11 07:02:57 +08:00
|
|
|
for (DefinedImportData *S : Syms) {
|
|
|
|
uint16_t Ord = S->getOrdinal();
|
|
|
|
if (S->getExternalName().empty()) {
|
2017-06-03 02:49:38 +08:00
|
|
|
Lookups.push_back(make<OrdinalOnlyChunk>(Ord));
|
2017-05-19 01:03:49 +08:00
|
|
|
Addresses.push_back(make<OrdinalOnlyChunk>(Ord));
|
2015-08-11 07:02:57 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-05-19 01:03:49 +08:00
|
|
|
auto *C = make<HintNameChunk>(S->getExternalName(), Ord);
|
2017-06-03 02:49:38 +08:00
|
|
|
Lookups.push_back(make<LookupChunk>(C));
|
2017-05-19 01:03:49 +08:00
|
|
|
Addresses.push_back(make<LookupChunk>(C));
|
|
|
|
Hints.push_back(C);
|
2015-08-11 07:02:57 +08:00
|
|
|
}
|
|
|
|
// Terminate with null values.
|
2017-06-03 02:49:38 +08:00
|
|
|
Lookups.push_back(make<NullChunk>(ptrSize()));
|
2017-05-19 01:03:49 +08:00
|
|
|
Addresses.push_back(make<NullChunk>(ptrSize()));
|
2015-08-11 07:02:57 +08:00
|
|
|
|
|
|
|
for (int I = 0, E = Syms.size(); I < E; ++I)
|
2017-05-19 01:03:49 +08:00
|
|
|
Syms[I]->setLocation(Addresses[Base + I]);
|
2015-08-11 07:02:57 +08:00
|
|
|
|
|
|
|
// Create the import table header.
|
2017-05-19 02:53:39 +08:00
|
|
|
DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName()));
|
|
|
|
auto *Dir = make<ImportDirectoryChunk>(DLLNames.back());
|
2017-06-03 02:49:38 +08:00
|
|
|
Dir->LookupTab = Lookups[Base];
|
2017-05-19 01:03:49 +08:00
|
|
|
Dir->AddressTab = Addresses[Base];
|
|
|
|
Dirs.push_back(Dir);
|
2015-08-11 07:02:57 +08:00
|
|
|
}
|
|
|
|
// Add null terminator.
|
2017-05-19 01:03:49 +08:00
|
|
|
Dirs.push_back(make<NullChunk>(sizeof(ImportDirectoryTableEntry)));
|
2015-08-11 07:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Chunk *> DelayLoadContents::getChunks() {
|
|
|
|
std::vector<Chunk *> V;
|
2017-05-19 01:03:49 +08:00
|
|
|
V.insert(V.end(), Dirs.begin(), Dirs.end());
|
|
|
|
V.insert(V.end(), Names.begin(), Names.end());
|
|
|
|
V.insert(V.end(), HintNames.begin(), HintNames.end());
|
2017-05-19 02:53:39 +08:00
|
|
|
V.insert(V.end(), DLLNames.begin(), DLLNames.end());
|
2015-08-11 07:02:57 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Chunk *> DelayLoadContents::getDataChunks() {
|
|
|
|
std::vector<Chunk *> V;
|
2017-05-19 01:03:49 +08:00
|
|
|
V.insert(V.end(), ModuleHandles.begin(), ModuleHandles.end());
|
|
|
|
V.insert(V.end(), Addresses.begin(), Addresses.end());
|
2015-08-11 07:02:57 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t DelayLoadContents::getDirSize() {
|
|
|
|
return Dirs.size() * sizeof(delay_import_directory_table_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayLoadContents::create(Defined *H) {
|
|
|
|
Helper = H;
|
2015-08-17 16:30:31 +08:00
|
|
|
std::vector<std::vector<DefinedImportData *>> V = binImports(Imports);
|
2015-08-11 07:02:57 +08:00
|
|
|
|
|
|
|
// Create .didat contents for each DLL.
|
2015-08-17 16:30:31 +08:00
|
|
|
for (std::vector<DefinedImportData *> &Syms : V) {
|
2015-08-11 07:02:57 +08:00
|
|
|
// Create the delay import table header.
|
2017-05-19 02:53:39 +08:00
|
|
|
DLLNames.push_back(make<StringChunk>(Syms[0]->getDLLName()));
|
|
|
|
auto *Dir = make<DelayDirectoryChunk>(DLLNames.back());
|
2015-08-11 07:02:57 +08:00
|
|
|
|
|
|
|
size_t Base = Addresses.size();
|
|
|
|
for (DefinedImportData *S : Syms) {
|
2017-05-19 01:03:49 +08:00
|
|
|
Chunk *T = newThunkChunk(S, Dir);
|
|
|
|
auto *A = make<DelayAddressChunk>(T);
|
|
|
|
Addresses.push_back(A);
|
|
|
|
Thunks.push_back(T);
|
2015-08-11 07:02:57 +08:00
|
|
|
StringRef ExtName = S->getExternalName();
|
|
|
|
if (ExtName.empty()) {
|
2017-05-19 01:03:49 +08:00
|
|
|
Names.push_back(make<OrdinalOnlyChunk>(S->getOrdinal()));
|
2015-08-11 07:02:57 +08:00
|
|
|
} else {
|
2017-05-19 01:03:49 +08:00
|
|
|
auto *C = make<HintNameChunk>(ExtName, 0);
|
|
|
|
Names.push_back(make<LookupChunk>(C));
|
|
|
|
HintNames.push_back(C);
|
2015-08-11 07:02:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Terminate with null values.
|
2017-05-19 01:03:49 +08:00
|
|
|
Addresses.push_back(make<NullChunk>(8));
|
|
|
|
Names.push_back(make<NullChunk>(8));
|
2015-08-11 07:02:57 +08:00
|
|
|
|
|
|
|
for (int I = 0, E = Syms.size(); I < E; ++I)
|
2017-05-19 01:03:49 +08:00
|
|
|
Syms[I]->setLocation(Addresses[Base + I]);
|
|
|
|
auto *MH = make<NullChunk>(8);
|
2017-09-14 05:54:55 +08:00
|
|
|
MH->Alignment = 8;
|
2017-05-19 01:03:49 +08:00
|
|
|
ModuleHandles.push_back(MH);
|
2015-08-11 07:02:57 +08:00
|
|
|
|
|
|
|
// Fill the delay import table header fields.
|
|
|
|
Dir->ModuleHandle = MH;
|
2017-05-19 01:03:49 +08:00
|
|
|
Dir->AddressTab = Addresses[Base];
|
|
|
|
Dir->NameTab = Names[Base];
|
|
|
|
Dirs.push_back(Dir);
|
2015-08-11 07:02:57 +08:00
|
|
|
}
|
|
|
|
// Add null terminator.
|
2017-05-19 01:03:49 +08:00
|
|
|
Dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
|
2015-08-11 07:02:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *S, Chunk *Dir) {
|
|
|
|
switch (Config->Machine) {
|
|
|
|
case AMD64:
|
2017-05-19 01:03:49 +08:00
|
|
|
return make<ThunkChunkX64>(S, Dir, Helper);
|
2015-08-11 07:02:57 +08:00
|
|
|
case I386:
|
2017-05-19 01:03:49 +08:00
|
|
|
return make<ThunkChunkX86>(S, Dir, Helper);
|
2017-07-26 04:00:37 +08:00
|
|
|
case ARMNT:
|
|
|
|
return make<ThunkChunkARM>(S, Dir, Helper);
|
2015-08-11 07:02:57 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported machine type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
EdataContents::EdataContents() {
|
|
|
|
uint16_t MaxOrdinal = 0;
|
|
|
|
for (Export &E : Config->Exports)
|
|
|
|
MaxOrdinal = std::max(MaxOrdinal, E.Ordinal);
|
|
|
|
|
2017-05-19 01:03:49 +08:00
|
|
|
auto *DLLName = make<StringChunk>(sys::path::filename(Config->OutputFile));
|
|
|
|
auto *AddressTab = make<AddressTableChunk>(MaxOrdinal);
|
2015-06-17 08:16:33 +08:00
|
|
|
std::vector<Chunk *> Names;
|
|
|
|
for (Export &E : Config->Exports)
|
|
|
|
if (!E.Noname)
|
2017-05-19 01:03:49 +08:00
|
|
|
Names.push_back(make<StringChunk>(E.ExportName));
|
2016-01-09 09:22:00 +08:00
|
|
|
|
|
|
|
std::vector<Chunk *> Forwards;
|
|
|
|
for (Export &E : Config->Exports) {
|
|
|
|
if (E.ForwardTo.empty())
|
|
|
|
continue;
|
2017-05-19 01:03:49 +08:00
|
|
|
E.ForwardChunk = make<StringChunk>(E.ForwardTo);
|
2016-01-09 09:22:00 +08:00
|
|
|
Forwards.push_back(E.ForwardChunk);
|
|
|
|
}
|
|
|
|
|
2017-05-19 01:03:49 +08:00
|
|
|
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());
|
2015-06-07 09:15:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|