2015-06-07 09:15:04 +08:00
|
|
|
//===- DLL.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-06-07 09:15:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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-06-07 09:15:04 +08:00
|
|
|
// A chunk for the import descriptor table.
|
2019-05-25 04:25:40 +08:00
|
|
|
class HintNameChunk : public NonSectionChunk {
|
2015-06-07 09:15:04 +08:00
|
|
|
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 {
|
2019-05-10 05:21:22 +08:00
|
|
|
memset(buf, 0, getSize());
|
|
|
|
write16le(buf, hint);
|
|
|
|
memcpy(buf + 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.
|
2019-05-25 04:25:40 +08:00
|
|
|
class LookupChunk : public NonSectionChunk {
|
2015-06-07 09:15:04 +08:00
|
|
|
public:
|
2019-05-23 04:21:52 +08:00
|
|
|
explicit LookupChunk(Chunk *c) : hintName(c) {
|
|
|
|
setAlignment(config->wordsize);
|
|
|
|
}
|
2018-10-12 01:45:58 +08:00
|
|
|
size_t getSize() const override { return config->wordsize; }
|
2015-06-07 09:15:04 +08:00
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *buf) const override {
|
2019-01-25 03:02:31 +08:00
|
|
|
if (config->is64())
|
2019-05-10 05:21:22 +08:00
|
|
|
write64le(buf, hintName->getRVA());
|
2019-01-25 03:02:31 +08:00
|
|
|
else
|
2019-05-10 05:21:22 +08:00
|
|
|
write32le(buf, 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.
|
2019-05-25 04:25:40 +08:00
|
|
|
class OrdinalOnlyChunk : public NonSectionChunk {
|
2015-06-07 09:15:04 +08:00
|
|
|
public:
|
2018-10-12 01:45:58 +08:00
|
|
|
explicit OrdinalOnlyChunk(uint16_t v) : ordinal(v) {
|
2019-05-23 04:21:52 +08:00
|
|
|
setAlignment(config->wordsize);
|
2018-10-12 01:45:58 +08:00
|
|
|
}
|
|
|
|
size_t getSize() const override { return config->wordsize; }
|
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()) {
|
2019-05-10 05:21:22 +08:00
|
|
|
write64le(buf, (1ULL << 63) | ordinal);
|
2015-07-09 08:45:50 +08:00
|
|
|
} else {
|
2019-05-10 05:21:22 +08:00
|
|
|
write32le(buf, (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.
|
2019-05-25 04:25:40 +08:00
|
|
|
class ImportDirectoryChunk : public NonSectionChunk {
|
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 {
|
2019-05-10 05:21:22 +08:00
|
|
|
memset(buf, 0, getSize());
|
2019-01-25 03:02:31 +08:00
|
|
|
|
2019-05-10 05:21:22 +08:00
|
|
|
auto *e = (coff_import_directory_table_entry *)(buf);
|
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.
|
2019-05-25 04:25:40 +08:00
|
|
|
class NullChunk : public NonSectionChunk {
|
2015-06-07 09:15:04 +08:00
|
|
|
public:
|
2019-05-25 04:25:40 +08:00
|
|
|
explicit NullChunk(size_t n) : size(n) { hasData = false; }
|
2015-06-07 09:15:04 +08:00
|
|
|
size_t getSize() const override { return size; }
|
|
|
|
|
2019-01-25 03:02:31 +08:00
|
|
|
void writeTo(uint8_t *buf) const override {
|
2019-05-10 05:21:22 +08:00
|
|
|
memset(buf, 0, size);
|
2019-01-25 03:02:31 +08:00
|
|
|
}
|
|
|
|
|
2015-06-07 09:15:04 +08:00
|
|
|
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
|
2019-10-10 10:04:56 +08:00
|
|
|
// symbols are laid 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.
|
2019-05-25 04:25:40 +08:00
|
|
|
class DelayDirectoryChunk : public NonSectionChunk {
|
2015-06-22 06:31:52 +08:00
|
|
|
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 {
|
2019-05-10 05:21:22 +08:00
|
|
|
memset(buf, 0, getSize());
|
2019-01-25 03:02:31 +08:00
|
|
|
|
2019-05-10 05:21:22 +08:00
|
|
|
auto *e = (delay_import_directory_table_entry *)(buf);
|
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
|
2019-08-02 19:08:15 +08:00
|
|
|
// which then overwrites its jump table slot with the result
|
2015-06-22 06:31:52 +08:00
|
|
|
// for subsequent function calls.
|
2015-07-16 06:26:57 +08:00
|
|
|
static const uint8_t thunkX64[] = {
|
2019-07-12 05:19:11 +08:00
|
|
|
0x48, 0x8D, 0x05, 0, 0, 0, 0, // lea rax, [__imp_<FUNCNAME>]
|
|
|
|
0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t tailMergeX64[] = {
|
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
|
2019-07-12 05:19:11 +08:00
|
|
|
0x48, 0x8B, 0xD0, // mov rdx, rax
|
2015-06-22 06:31:52 +08:00
|
|
|
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[] = {
|
2019-07-12 05:19:11 +08:00
|
|
|
0xB8, 0, 0, 0, 0, // mov eax, offset ___imp__<FUNCNAME>
|
|
|
|
0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t tailMergeX86[] = {
|
2015-07-16 06:26:57 +08:00
|
|
|
0x51, // push ecx
|
|
|
|
0x52, // push edx
|
2019-07-12 05:19:11 +08:00
|
|
|
0x50, // push eax
|
2015-07-16 06:26:57 +08:00
|
|
|
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>
|
2019-07-12 05:19:11 +08:00
|
|
|
0x00, 0xf0, 0x00, 0xb8, // b.w __tailMerge_<lib>
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t tailMergeARM[] = {
|
2017-07-26 04:00:37 +08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-09-18 15:22:01 +08:00
|
|
|
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>
|
2019-07-12 05:19:11 +08:00
|
|
|
0x00, 0x00, 0x00, 0x14, // b __tailMerge_<lib>
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t tailMergeARM64[] = {
|
2018-09-18 15:22:01 +08:00
|
|
|
0xfd, 0x7b, 0xb3, 0xa9, // stp x29, x30, [sp, #-208]!
|
|
|
|
0xfd, 0x03, 0x00, 0x91, // mov x29, sp
|
|
|
|
0xe0, 0x07, 0x01, 0xa9, // stp x0, x1, [sp, #16]
|
|
|
|
0xe2, 0x0f, 0x02, 0xa9, // stp x2, x3, [sp, #32]
|
|
|
|
0xe4, 0x17, 0x03, 0xa9, // stp x4, x5, [sp, #48]
|
|
|
|
0xe6, 0x1f, 0x04, 0xa9, // stp x6, x7, [sp, #64]
|
|
|
|
0xe0, 0x87, 0x02, 0xad, // stp q0, q1, [sp, #80]
|
|
|
|
0xe2, 0x8f, 0x03, 0xad, // stp q2, q3, [sp, #112]
|
|
|
|
0xe4, 0x97, 0x04, 0xad, // stp q4, q5, [sp, #144]
|
|
|
|
0xe6, 0x9f, 0x05, 0xad, // stp q6, q7, [sp, #176]
|
|
|
|
0xe1, 0x03, 0x11, 0xaa, // mov x1, x17
|
|
|
|
0x00, 0x00, 0x00, 0x90, // adrp x0, #0 DELAY_IMPORT_DESCRIPTOR
|
|
|
|
0x00, 0x00, 0x00, 0x91, // add x0, x0, #0 :lo12:DELAY_IMPORT_DESCRIPTOR
|
|
|
|
0x00, 0x00, 0x00, 0x94, // bl #0 __delayLoadHelper2
|
|
|
|
0xf0, 0x03, 0x00, 0xaa, // mov x16, x0
|
|
|
|
0xe6, 0x9f, 0x45, 0xad, // ldp q6, q7, [sp, #176]
|
|
|
|
0xe4, 0x97, 0x44, 0xad, // ldp q4, q5, [sp, #144]
|
|
|
|
0xe2, 0x8f, 0x43, 0xad, // ldp q2, q3, [sp, #112]
|
|
|
|
0xe0, 0x87, 0x42, 0xad, // ldp q0, q1, [sp, #80]
|
|
|
|
0xe6, 0x1f, 0x44, 0xa9, // ldp x6, x7, [sp, #64]
|
|
|
|
0xe4, 0x17, 0x43, 0xa9, // ldp x4, x5, [sp, #48]
|
|
|
|
0xe2, 0x0f, 0x42, 0xa9, // ldp x2, x3, [sp, #32]
|
|
|
|
0xe0, 0x07, 0x41, 0xa9, // ldp x0, x1, [sp, #16]
|
|
|
|
0xfd, 0x7b, 0xcd, 0xa8, // ldp x29, x30, [sp], #208
|
|
|
|
0x00, 0x02, 0x1f, 0xd6, // br x16
|
|
|
|
};
|
|
|
|
|
2015-06-22 06:31:52 +08:00
|
|
|
// A chunk for the delay import thunk.
|
2019-05-25 04:25:40 +08:00
|
|
|
class ThunkChunkX64 : public NonSectionChunk {
|
2015-06-22 06:31:52 +08:00
|
|
|
public:
|
2019-07-12 05:19:11 +08:00
|
|
|
ThunkChunkX64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
|
2015-07-16 06:26:57 +08:00
|
|
|
|
|
|
|
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 {
|
2019-05-10 05:21:22 +08:00
|
|
|
memcpy(buf, thunkX64, sizeof(thunkX64));
|
2019-07-12 05:19:11 +08:00
|
|
|
write32le(buf + 3, imp->getRVA() - rva - 7);
|
|
|
|
write32le(buf + 8, tailMerge->getRVA() - rva - 12);
|
2015-06-22 06:31:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Defined *imp = nullptr;
|
2019-07-12 05:19:11 +08:00
|
|
|
Chunk *tailMerge = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TailMergeChunkX64 : public NonSectionChunk {
|
|
|
|
public:
|
|
|
|
TailMergeChunkX64(Chunk *d, Defined *h) : desc(d), helper(h) {}
|
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(tailMergeX64); }
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-06-22 06:31:52 +08:00
|
|
|
Chunk *desc = nullptr;
|
|
|
|
Defined *helper = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-05-25 04:25:40 +08:00
|
|
|
class ThunkChunkX86 : public NonSectionChunk {
|
2015-07-16 06:26:57 +08:00
|
|
|
public:
|
2019-07-12 05:19:11 +08:00
|
|
|
ThunkChunkX86(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
|
2015-07-16 06:26:57 +08:00
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(thunkX86); }
|
|
|
|
|
2015-09-20 07:28:57 +08:00
|
|
|
void writeTo(uint8_t *buf) const override {
|
2019-05-10 05:21:22 +08:00
|
|
|
memcpy(buf, thunkX86, sizeof(thunkX86));
|
2019-07-12 05:19:11 +08:00
|
|
|
write32le(buf + 1, imp->getRVA() + config->imageBase);
|
|
|
|
write32le(buf + 6, tailMerge->getRVA() - rva - 10);
|
2015-07-16 06:26:57 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:44:32 +08:00
|
|
|
void getBaserels(std::vector<Baserel> *res) override {
|
2019-07-12 05:19:11 +08:00
|
|
|
res->emplace_back(rva + 1);
|
2015-07-16 06:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Defined *imp = nullptr;
|
2019-07-12 05:19:11 +08:00
|
|
|
Chunk *tailMerge = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TailMergeChunkX86 : public NonSectionChunk {
|
|
|
|
public:
|
|
|
|
TailMergeChunkX86(Chunk *d, Defined *h) : desc(d), helper(h) {}
|
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(tailMergeX86); }
|
|
|
|
|
|
|
|
void writeTo(uint8_t *buf) const override {
|
|
|
|
memcpy(buf, tailMergeX86, sizeof(tailMergeX86));
|
|
|
|
write32le(buf + 4, desc->getRVA() + config->imageBase);
|
|
|
|
write32le(buf + 9, helper->getRVA() - rva - 13);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getBaserels(std::vector<Baserel> *res) override {
|
|
|
|
res->emplace_back(rva + 4);
|
|
|
|
}
|
|
|
|
|
2015-07-16 06:26:57 +08:00
|
|
|
Chunk *desc = nullptr;
|
|
|
|
Defined *helper = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-05-25 04:25:40 +08:00
|
|
|
class ThunkChunkARM : public NonSectionChunk {
|
2017-07-26 04:00:37 +08:00
|
|
|
public:
|
2020-04-09 05:42:50 +08:00
|
|
|
ThunkChunkARM(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {
|
|
|
|
setAlignment(2);
|
|
|
|
}
|
2017-07-26 04:00:37 +08:00
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(thunkARM); }
|
|
|
|
|
|
|
|
void writeTo(uint8_t *buf) const override {
|
2019-05-10 05:21:22 +08:00
|
|
|
memcpy(buf, thunkARM, sizeof(thunkARM));
|
|
|
|
applyMOV32T(buf + 0, imp->getRVA() + config->imageBase);
|
2019-07-12 05:19:11 +08:00
|
|
|
applyBranch24T(buf + 8, tailMerge->getRVA() - rva - 12);
|
2017-07-26 04:00:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void getBaserels(std::vector<Baserel> *res) override {
|
|
|
|
res->emplace_back(rva + 0, IMAGE_REL_BASED_ARM_MOV32T);
|
|
|
|
}
|
|
|
|
|
|
|
|
Defined *imp = nullptr;
|
2019-07-12 05:19:11 +08:00
|
|
|
Chunk *tailMerge = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TailMergeChunkARM : public NonSectionChunk {
|
|
|
|
public:
|
2020-04-09 05:42:50 +08:00
|
|
|
TailMergeChunkARM(Chunk *d, Defined *h) : desc(d), helper(h) {
|
|
|
|
setAlignment(2);
|
|
|
|
}
|
2019-07-12 05:19:11 +08:00
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(tailMergeARM); }
|
|
|
|
|
|
|
|
void writeTo(uint8_t *buf) const override {
|
|
|
|
memcpy(buf, tailMergeARM, sizeof(tailMergeARM));
|
|
|
|
applyMOV32T(buf + 14, desc->getRVA() + 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);
|
|
|
|
}
|
|
|
|
|
2017-07-26 04:00:37 +08:00
|
|
|
Chunk *desc = nullptr;
|
|
|
|
Defined *helper = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-05-25 04:25:40 +08:00
|
|
|
class ThunkChunkARM64 : public NonSectionChunk {
|
2018-09-18 15:22:01 +08:00
|
|
|
public:
|
2020-04-09 05:42:50 +08:00
|
|
|
ThunkChunkARM64(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {
|
|
|
|
setAlignment(4);
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-09-18 15:22:01 +08:00
|
|
|
size_t getSize() const override { return sizeof(thunkARM64); }
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-09-18 15:22:01 +08:00
|
|
|
void writeTo(uint8_t *buf) const override {
|
2019-05-10 05:21:22 +08:00
|
|
|
memcpy(buf, thunkARM64, sizeof(thunkARM64));
|
|
|
|
applyArm64Addr(buf + 0, imp->getRVA(), rva + 0, 12);
|
|
|
|
applyArm64Imm(buf + 4, imp->getRVA() & 0xfff, 0);
|
2019-07-12 05:19:11 +08:00
|
|
|
applyArm64Branch26(buf + 8, tailMerge->getRVA() - rva - 8);
|
2018-09-18 15:22:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Defined *imp = nullptr;
|
2019-07-12 05:19:11 +08:00
|
|
|
Chunk *tailMerge = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TailMergeChunkARM64 : public NonSectionChunk {
|
|
|
|
public:
|
2020-04-09 05:42:50 +08:00
|
|
|
TailMergeChunkARM64(Chunk *d, Defined *h) : desc(d), helper(h) {
|
|
|
|
setAlignment(4);
|
|
|
|
}
|
2019-07-12 05:19:11 +08:00
|
|
|
|
|
|
|
size_t getSize() const override { return sizeof(tailMergeARM64); }
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:22:01 +08:00
|
|
|
Chunk *desc = nullptr;
|
|
|
|
Defined *helper = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-06-27 05:40:15 +08:00
|
|
|
// A chunk for the import descriptor table.
|
2019-05-25 04:25:40 +08:00
|
|
|
class DelayAddressChunk : public NonSectionChunk {
|
2015-06-27 05:40:15 +08:00
|
|
|
public:
|
2018-10-12 01:45:58 +08:00
|
|
|
explicit DelayAddressChunk(Chunk *c) : thunk(c) {
|
2019-05-23 04:21:52 +08:00
|
|
|
setAlignment(config->wordsize);
|
2018-10-12 01:45:58 +08:00
|
|
|
}
|
|
|
|
size_t getSize() const override { return config->wordsize; }
|
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()) {
|
2019-05-10 05:21:22 +08:00
|
|
|
write64le(buf, 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;
|
2019-05-10 05:21:22 +08:00
|
|
|
write32le(buf, (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.
|
2019-05-25 04:25:40 +08:00
|
|
|
class ExportDirectoryChunk : public NonSectionChunk {
|
2015-06-17 08:16:33 +08:00
|
|
|
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 {
|
2019-05-10 05:21:22 +08:00
|
|
|
memset(buf, 0, getSize());
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-10 05:21:22 +08:00
|
|
|
auto *e = (export_directory_table_entry *)(buf);
|
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;
|
|
|
|
};
|
|
|
|
|
2019-05-25 04:25:40 +08:00
|
|
|
class AddressTableChunk : public NonSectionChunk {
|
2015-06-17 08:16:33 +08:00
|
|
|
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 {
|
2019-05-10 05:21:22 +08:00
|
|
|
memset(buf, 0, getSize());
|
2018-12-01 00:34:56 +08:00
|
|
|
|
2017-12-20 14:50:45 +08:00
|
|
|
for (const Export &e : config->exports) {
|
2019-05-10 05:21:22 +08:00
|
|
|
uint8_t *p = buf + 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;
|
|
|
|
};
|
|
|
|
|
2019-05-25 04:25:40 +08:00
|
|
|
class NamePointersChunk : public NonSectionChunk {
|
2015-06-17 08:16:33 +08:00
|
|
|
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-06-17 08:16:33 +08:00
|
|
|
for (Chunk *c : chunks) {
|
2019-05-10 05:21:22 +08:00
|
|
|
write32le(buf, c->getRVA());
|
|
|
|
buf += 4;
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Chunk *> chunks;
|
|
|
|
};
|
|
|
|
|
2019-05-25 04:25:40 +08:00
|
|
|
class ExportOrdinalChunk : public NonSectionChunk {
|
2015-06-17 08:16:33 +08:00
|
|
|
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-06-17 08:16:33 +08:00
|
|
|
for (Export &e : config->exports) {
|
|
|
|
if (e.noname)
|
|
|
|
continue;
|
2019-05-10 05:21:22 +08:00
|
|
|
write16le(buf, e.ordinal);
|
|
|
|
buf += 2;
|
2015-06-17 08:16:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2015-08-11 07:02:57 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
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,
|
2019-07-16 16:26:38 +08:00
|
|
|
// we need to create hintName chunks to store the names.
|
2015-08-11 07:02:57 +08:00
|
|
|
// 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.
|
2018-10-12 01:45:58 +08:00
|
|
|
lookups.push_back(make<NullChunk>(config->wordsize));
|
|
|
|
addresses.push_back(make<NullChunk>(config->wordsize));
|
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());
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2015-08-11 07:02:57 +08:00
|
|
|
size_t base = addresses.size();
|
2019-07-12 05:19:11 +08:00
|
|
|
Chunk *tm = newTailMergeChunk(dir);
|
2015-08-11 07:02:57 +08:00
|
|
|
for (DefinedImportData *s : syms) {
|
2019-07-12 05:19:11 +08:00
|
|
|
Chunk *t = newThunkChunk(s, tm);
|
2017-05-19 01:03:49 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2019-07-12 05:19:11 +08:00
|
|
|
thunks.push_back(tm);
|
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);
|
2019-05-23 04:21:52 +08:00
|
|
|
mh->setAlignment(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
|
|
|
}
|
|
|
|
|
2019-07-12 05:19:11 +08:00
|
|
|
Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {
|
|
|
|
switch (config->machine) {
|
|
|
|
case AMD64:
|
|
|
|
return make<TailMergeChunkX64>(dir, helper);
|
|
|
|
case I386:
|
|
|
|
return make<TailMergeChunkX86>(dir, helper);
|
|
|
|
case ARMNT:
|
|
|
|
return make<TailMergeChunkARM>(dir, helper);
|
|
|
|
case ARM64:
|
|
|
|
return make<TailMergeChunkARM64>(dir, helper);
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported machine type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s,
|
|
|
|
Chunk *tailMerge) {
|
2015-08-11 07:02:57 +08:00
|
|
|
switch (config->machine) {
|
|
|
|
case AMD64:
|
2019-07-12 05:19:11 +08:00
|
|
|
return make<ThunkChunkX64>(s, tailMerge);
|
2015-08-11 07:02:57 +08:00
|
|
|
case I386:
|
2019-07-12 05:19:11 +08:00
|
|
|
return make<ThunkChunkX86>(s, tailMerge);
|
2017-07-26 04:00:37 +08:00
|
|
|
case ARMNT:
|
2019-07-12 05:19:11 +08:00
|
|
|
return make<ThunkChunkARM>(s, tailMerge);
|
2018-09-18 15:22:01 +08:00
|
|
|
case ARM64:
|
2019-07-12 05:19:11 +08:00
|
|
|
return make<ThunkChunkARM64>(s, tailMerge);
|
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);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
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));
|
2019-07-11 13:40:30 +08:00
|
|
|
|
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
|