2013-12-13 14:58:27 +08:00
|
|
|
//===- lib/ReaderWriter/PECOFF/EdataPass.cpp ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Pass.h"
|
|
|
|
#include "EdataPass.h"
|
|
|
|
|
|
|
|
#include "lld/Core/File.h"
|
|
|
|
#include "lld/Core/Pass.h"
|
|
|
|
#include "lld/ReaderWriter/Simple.h"
|
2013-12-16 13:46:07 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-12-13 14:58:27 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
2013-12-16 15:14:40 +08:00
|
|
|
#include <climits>
|
2013-12-13 14:58:27 +08:00
|
|
|
#include <ctime>
|
2013-12-16 15:14:40 +08:00
|
|
|
#include <utility>
|
2013-12-13 14:58:27 +08:00
|
|
|
|
|
|
|
using lld::pecoff::edata::EdataAtom;
|
2013-12-16 13:46:07 +08:00
|
|
|
using lld::pecoff::edata::TableEntry;
|
2013-12-13 14:58:27 +08:00
|
|
|
using llvm::object::export_address_table_entry;
|
|
|
|
using llvm::object::export_directory_table_entry;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace pecoff {
|
|
|
|
|
2013-12-16 13:46:07 +08:00
|
|
|
static bool compare(const TableEntry &a, const TableEntry &b) {
|
|
|
|
return a.exportName.compare(b.exportName) < 0;
|
2013-12-14 11:54:15 +08:00
|
|
|
}
|
|
|
|
|
2013-12-28 16:40:23 +08:00
|
|
|
static void assignOrdinals(PECOFFLinkingContext &ctx) {
|
|
|
|
std::set<PECOFFLinkingContext::ExportDesc> exports;
|
|
|
|
int maxOrdinal = -1;
|
|
|
|
for (const PECOFFLinkingContext::ExportDesc &desc : ctx.getDllExports())
|
|
|
|
maxOrdinal = std::max(maxOrdinal, desc.ordinal);
|
|
|
|
int nextOrdinal = (maxOrdinal == -1) ? 1 : (maxOrdinal + 1);
|
|
|
|
for (PECOFFLinkingContext::ExportDesc desc : ctx.getDllExports()) {
|
|
|
|
if (desc.ordinal == -1)
|
|
|
|
desc.ordinal = nextOrdinal++;
|
|
|
|
exports.insert(desc);
|
|
|
|
}
|
|
|
|
ctx.getDllExports().swap(exports);
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:37:52 +08:00
|
|
|
static bool getExportedAtoms(const PECOFFLinkingContext &ctx, MutableFile *file,
|
2013-12-16 13:46:07 +08:00
|
|
|
std::vector<TableEntry> &ret) {
|
2013-12-13 14:58:27 +08:00
|
|
|
std::map<StringRef, const DefinedAtom *> definedAtoms;
|
|
|
|
for (const DefinedAtom *atom : file->defined())
|
|
|
|
definedAtoms[atom->name()] = atom;
|
|
|
|
|
2013-12-13 16:42:52 +08:00
|
|
|
for (const PECOFFLinkingContext::ExportDesc &desc : ctx.getDllExports()) {
|
2013-12-24 17:15:57 +08:00
|
|
|
auto it = definedAtoms.find(desc.name);
|
2013-12-13 14:58:27 +08:00
|
|
|
if (it == definedAtoms.end()) {
|
2013-12-13 16:42:52 +08:00
|
|
|
llvm::errs() << "Symbol <" << desc.name
|
2013-12-13 14:58:27 +08:00
|
|
|
<< "> is exported but not defined.\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const DefinedAtom *atom = it->second;
|
2013-12-16 17:02:43 +08:00
|
|
|
ret.push_back(TableEntry(desc.name, desc.ordinal, atom, desc.noname));
|
2013-12-13 14:58:27 +08:00
|
|
|
}
|
2013-12-14 11:54:15 +08:00
|
|
|
std::sort(ret.begin(), ret.end(), compare);
|
2013-12-13 14:58:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-20 18:02:59 +08:00
|
|
|
static std::pair<int, int> getOrdinalBase(std::vector<TableEntry> &entries) {
|
2013-12-16 15:14:40 +08:00
|
|
|
int ordinalBase = INT_MAX;
|
2013-12-16 13:46:07 +08:00
|
|
|
int maxOrdinal = -1;
|
2013-12-16 15:14:40 +08:00
|
|
|
for (TableEntry &e : entries) {
|
2013-12-20 18:02:59 +08:00
|
|
|
ordinalBase = std::min(ordinalBase, e.ordinal);
|
2013-12-16 13:46:07 +08:00
|
|
|
maxOrdinal = std::max(maxOrdinal, e.ordinal);
|
2013-12-16 15:14:40 +08:00
|
|
|
}
|
|
|
|
return std::pair<int, int>(ordinalBase, maxOrdinal);
|
2013-12-16 13:46:07 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 14:58:27 +08:00
|
|
|
edata::EdataAtom *
|
2013-12-16 13:46:07 +08:00
|
|
|
EdataPass::createAddressTable(const std::vector<TableEntry> &entries,
|
2013-12-16 15:14:40 +08:00
|
|
|
int ordinalBase, int maxOrdinal) {
|
|
|
|
EdataAtom *addressTable =
|
|
|
|
new (_alloc) EdataAtom(_file, sizeof(export_address_table_entry) *
|
|
|
|
(maxOrdinal - ordinalBase + 1));
|
2013-12-13 14:58:27 +08:00
|
|
|
|
2013-12-16 13:46:07 +08:00
|
|
|
for (const TableEntry &e : entries) {
|
2013-12-16 15:14:40 +08:00
|
|
|
int index = e.ordinal - ordinalBase;
|
2013-12-16 13:46:07 +08:00
|
|
|
size_t offset = index * sizeof(export_address_table_entry);
|
|
|
|
addDir32NBReloc(addressTable, e.atom, offset);
|
2013-12-13 14:58:27 +08:00
|
|
|
}
|
|
|
|
return addressTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
edata::EdataAtom *
|
2013-12-14 12:32:29 +08:00
|
|
|
EdataPass::createNamePointerTable(const PECOFFLinkingContext &ctx,
|
2013-12-16 13:46:07 +08:00
|
|
|
const std::vector<TableEntry> &entries,
|
2013-12-13 14:58:27 +08:00
|
|
|
MutableFile *file) {
|
2013-12-13 15:37:52 +08:00
|
|
|
EdataAtom *table =
|
2013-12-16 13:46:07 +08:00
|
|
|
new (_alloc) EdataAtom(_file, sizeof(uint32_t) * entries.size());
|
2013-12-13 14:58:27 +08:00
|
|
|
|
|
|
|
size_t offset = 0;
|
2013-12-16 13:46:07 +08:00
|
|
|
for (const TableEntry &e : entries) {
|
2013-12-24 17:15:57 +08:00
|
|
|
auto *stringAtom = new (_alloc) COFFStringAtom(
|
|
|
|
_file, _stringOrdinal++, ".edata", ctx.undecorateSymbol(e.exportName));
|
2013-12-13 14:58:27 +08:00
|
|
|
file->addAtom(*stringAtom);
|
|
|
|
addDir32NBReloc(table, stringAtom, offset);
|
|
|
|
offset += sizeof(uint32_t);
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:46:07 +08:00
|
|
|
edata::EdataAtom *EdataPass::createExportDirectoryTable(
|
2013-12-16 17:02:43 +08:00
|
|
|
const std::vector<edata::TableEntry> &namedEntries, int ordinalBase,
|
2013-12-16 15:14:40 +08:00
|
|
|
int maxOrdinal) {
|
2013-12-13 15:37:52 +08:00
|
|
|
EdataAtom *ret =
|
|
|
|
new (_alloc) EdataAtom(_file, sizeof(export_directory_table_entry));
|
2013-12-13 14:58:27 +08:00
|
|
|
auto *data = ret->getContents<export_directory_table_entry>();
|
|
|
|
data->TimeDateStamp = time(nullptr);
|
2013-12-16 15:14:40 +08:00
|
|
|
data->OrdinalBase = ordinalBase;
|
|
|
|
data->AddressTableEntries = maxOrdinal - ordinalBase + 1;
|
2013-12-16 17:02:43 +08:00
|
|
|
data->NumberOfNamePointers = namedEntries.size();
|
2013-12-13 14:58:27 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:46:07 +08:00
|
|
|
edata::EdataAtom *
|
2013-12-16 15:14:40 +08:00
|
|
|
EdataPass::createOrdinalTable(const std::vector<TableEntry> &entries,
|
|
|
|
int ordinalBase) {
|
2013-12-13 15:37:52 +08:00
|
|
|
EdataAtom *ret =
|
2013-12-16 13:46:07 +08:00
|
|
|
new (_alloc) EdataAtom(_file, sizeof(uint16_t) * entries.size());
|
2013-12-13 14:58:27 +08:00
|
|
|
uint16_t *data = ret->getContents<uint16_t>();
|
2013-12-16 13:46:07 +08:00
|
|
|
int i = 0;
|
|
|
|
for (const TableEntry &e : entries)
|
2013-12-16 15:14:40 +08:00
|
|
|
data[i++] = e.ordinal - ordinalBase;
|
2013-12-13 14:58:27 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EdataPass::perform(std::unique_ptr<MutableFile> &file) {
|
2013-12-28 16:40:23 +08:00
|
|
|
assignOrdinals(_ctx);
|
|
|
|
|
2013-12-16 13:46:07 +08:00
|
|
|
std::vector<TableEntry> entries;
|
|
|
|
if (!getExportedAtoms(_ctx, file.get(), entries))
|
2013-12-13 14:58:27 +08:00
|
|
|
return;
|
2013-12-16 13:46:07 +08:00
|
|
|
if (entries.empty())
|
2013-12-13 14:58:27 +08:00
|
|
|
return;
|
|
|
|
|
2013-12-16 15:14:40 +08:00
|
|
|
int ordinalBase, maxOrdinal;
|
2014-03-02 21:45:18 +08:00
|
|
|
std::tie(ordinalBase, maxOrdinal) = getOrdinalBase(entries);
|
2013-12-16 15:14:40 +08:00
|
|
|
|
2013-12-16 17:02:43 +08:00
|
|
|
std::vector<TableEntry> namedEntries;
|
|
|
|
for (TableEntry &e : entries)
|
|
|
|
if (!e.noname)
|
|
|
|
namedEntries.push_back(e);
|
|
|
|
|
2013-12-16 15:14:40 +08:00
|
|
|
EdataAtom *table =
|
2013-12-16 17:02:43 +08:00
|
|
|
createExportDirectoryTable(namedEntries, ordinalBase, maxOrdinal);
|
2013-12-13 14:58:27 +08:00
|
|
|
file->addAtom(*table);
|
|
|
|
|
2013-12-13 15:34:40 +08:00
|
|
|
COFFStringAtom *dllName =
|
|
|
|
new (_alloc) COFFStringAtom(_file, _stringOrdinal++, ".edata",
|
|
|
|
llvm::sys::path::filename(_ctx.outputPath()));
|
2013-12-13 14:58:27 +08:00
|
|
|
file->addAtom(*dllName);
|
2013-12-13 15:37:52 +08:00
|
|
|
addDir32NBReloc(table, dllName,
|
|
|
|
offsetof(export_directory_table_entry, NameRVA));
|
2013-12-13 14:58:27 +08:00
|
|
|
|
2013-12-16 15:14:40 +08:00
|
|
|
EdataAtom *addressTable =
|
|
|
|
createAddressTable(entries, ordinalBase, maxOrdinal);
|
2013-12-13 14:58:27 +08:00
|
|
|
file->addAtom(*addressTable);
|
2013-12-13 15:37:52 +08:00
|
|
|
addDir32NBReloc(table, addressTable, offsetof(export_directory_table_entry,
|
|
|
|
ExportAddressTableRVA));
|
2013-12-13 14:58:27 +08:00
|
|
|
|
2013-12-16 13:46:07 +08:00
|
|
|
EdataAtom *namePointerTable =
|
2013-12-16 17:02:43 +08:00
|
|
|
createNamePointerTable(_ctx, namedEntries, file.get());
|
2013-12-13 14:58:27 +08:00
|
|
|
file->addAtom(*namePointerTable);
|
|
|
|
addDir32NBReloc(table, namePointerTable,
|
|
|
|
offsetof(export_directory_table_entry, NamePointerRVA));
|
|
|
|
|
2013-12-16 17:02:43 +08:00
|
|
|
EdataAtom *ordinalTable = createOrdinalTable(namedEntries, ordinalBase);
|
2013-12-13 14:58:27 +08:00
|
|
|
file->addAtom(*ordinalTable);
|
|
|
|
addDir32NBReloc(table, ordinalTable,
|
|
|
|
offsetof(export_directory_table_entry, OrdinalTableRVA));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pecoff
|
|
|
|
} // namespace lld
|