[PECOFF] Simplify EdataPass by sorting atoms from the beginning.

llvm-svn: 197306
This commit is contained in:
Rui Ueyama 2013-12-14 03:54:15 +00:00
parent 3298368de9
commit 1293106fdb
4 changed files with 26 additions and 28 deletions

View File

@ -24,6 +24,10 @@ using llvm::object::export_directory_table_entry;
namespace lld {
namespace pecoff {
static bool compare(const DefinedAtom *a, const DefinedAtom *b) {
return a->name().compare(b->name()) < 0;
}
static bool getExportedAtoms(const PECOFFLinkingContext &ctx, MutableFile *file,
std::vector<const DefinedAtom *> &ret) {
std::map<StringRef, const DefinedAtom *> definedAtoms;
@ -40,13 +44,10 @@ static bool getExportedAtoms(const PECOFFLinkingContext &ctx, MutableFile *file,
const DefinedAtom *atom = it->second;
ret.push_back(atom);
}
std::sort(ret.begin(), ret.end(), compare);
return true;
}
static bool compare(const DefinedAtom *a, const DefinedAtom *b) {
return a->name().compare(b->name()) < 0;
}
edata::EdataAtom *
EdataPass::createAddressTable(const std::vector<const DefinedAtom *> &atoms) {
EdataAtom *addressTable = new (_alloc)
@ -89,20 +90,12 @@ edata::EdataAtom *EdataPass::createExportDirectoryTable(size_t numEntries) {
}
edata::EdataAtom *EdataPass::createOrdinalTable(
const std::vector<const DefinedAtom *> &atoms,
const std::vector<const DefinedAtom *> &sortedAtoms) {
const std::vector<const DefinedAtom *> &atoms) {
EdataAtom *ret =
new (_alloc) EdataAtom(_file, sizeof(uint16_t) * atoms.size());
uint16_t *data = ret->getContents<uint16_t>();
std::map<const DefinedAtom *, size_t> ordinals;
size_t ordinal = 0;
for (const DefinedAtom *atom : atoms)
ordinals[atom] = ordinal++;
size_t index = 0;
for (const DefinedAtom *atom : sortedAtoms)
data[index++] = ordinals[atom];
for (size_t i = 0, e = atoms.size(); i < e; ++i)
data[i] = i;
return ret;
}
@ -128,14 +121,12 @@ void EdataPass::perform(std::unique_ptr<MutableFile> &file) {
addDir32NBReloc(table, addressTable, offsetof(export_directory_table_entry,
ExportAddressTableRVA));
std::vector<const DefinedAtom *> sortedAtoms(atoms);
std::sort(sortedAtoms.begin(), sortedAtoms.end(), compare);
EdataAtom *namePointerTable = createNamePointerTable(sortedAtoms, file.get());
EdataAtom *namePointerTable = createNamePointerTable(atoms, file.get());
file->addAtom(*namePointerTable);
addDir32NBReloc(table, namePointerTable,
offsetof(export_directory_table_entry, NamePointerRVA));
EdataAtom *ordinalTable = createOrdinalTable(atoms, sortedAtoms);
EdataAtom *ordinalTable = createOrdinalTable(atoms);
file->addAtom(*ordinalTable);
addDir32NBReloc(table, ordinalTable,
offsetof(export_directory_table_entry, OrdinalTableRVA));

View File

@ -69,8 +69,7 @@ private:
createNamePointerTable(const std::vector<const DefinedAtom *> &atoms,
MutableFile *file);
edata::EdataAtom *
createOrdinalTable(const std::vector<const DefinedAtom *> &atoms,
const std::vector<const DefinedAtom *> &sortedAtoms);
createOrdinalTable(const std::vector<const DefinedAtom *> &atoms);
const PECOFFLinkingContext &_ctx;
VirtualFile _file;

View File

@ -22,10 +22,16 @@ symbols:
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: _exportfn
- Name: _exportfn1
Value: 8
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: _exportfn2
Value: 16
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...

View File

@ -1,12 +1,14 @@
# RUN: yaml2obj %p/Inputs/export.obj.yaml > %t.obj
#
# RUN: lld -flavor link /out:%t.dll /dll /subsystem:console /entry:_init \
# RUN: /export:exportfn -- %t.obj
# RUN: /export:exportfn1 /export:exportfn2 -- %t.obj
# RUN: llvm-objdump -s %t.dll | FileCheck %s
CHECK: Contents of section .edata:
CHECK-NEXT: 1000 00000000 {{........}} 00000000 32100000
CHECK-NEXT: 1010 01000000 01000000 01000000 28100000
CHECK-NEXT: 1020 2c100000 30100000 08200000 46100000
CHECK-NEXT: 1030 00006578 706f7274 2e746573 742e746d
CHECK-NEXT: 1040 702e646c 6c005f65 78706f72 74666e00
CHECK-NEXT: 1000 00000000 {{........}} 00000000 3c100000
CHECK-NEXT: 1010 01000000 02000000 02000000 28100000
CHECK-NEXT: 1020 30100000 38100000 08200000 10200000
CHECK-NEXT: 1030 50100000 5b100000 00000100 6578706f
CHECK-NEXT: 1040 72742e74 6573742e 746d702e 646c6c00
CHECK-NEXT: 1050 5f657870 6f727466 6e31005f 6578706f
CHECK-NEXT: 1060 7274666e 3200