[lld] [lld][COFF][NFC] Factor out exception table sorting. (PR #72518)
Jacek Caban via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 16 07:59:19 PST 2023
https://github.com/cjacek updated https://github.com/llvm/llvm-project/pull/72518
>From a36acf7c8db4d9a06d2e5d9ca743e8929fc0b1d3 Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Wed, 15 Nov 2023 23:56:20 +0100
Subject: [PATCH 1/2] [lld][COFF][NFC] Factor out exception table sorting.
---
lld/COFF/Writer.cpp | 57 ++++++++++++++++++++++++++-------------------
1 file changed, 33 insertions(+), 24 deletions(-)
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 0477c094c0ac82c..76c9b7604021f17 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -247,7 +247,8 @@ class Writer {
void writeBuildId();
void writePEChecksum();
void sortSections();
- void sortExceptionTable();
+ template <typename T> void sortExceptionTable(Chunk *first, Chunk *last);
+ void sortExceptionTables();
void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
void addSyntheticIdata();
void sortBySectionOrder(std::vector<Chunk *> &chunks);
@@ -751,7 +752,7 @@ void Writer::run() {
}
writeSections();
prepareLoadConfig();
- sortExceptionTable();
+ sortExceptionTables();
// Fix up the alignment in the TLS Directory's characteristic field,
// if a specific alignment value is needed
@@ -2164,41 +2165,49 @@ void Writer::writeBuildId() {
}
// Sort .pdata section contents according to PE/COFF spec 5.5.
-void Writer::sortExceptionTable() {
- if (!firstPdata)
+template <typename T>
+void Writer::sortExceptionTable(Chunk *first, Chunk *last) {
+ if (!first)
return;
- llvm::TimeTraceScope timeScope("Sort exception table");
// We assume .pdata contains function table entries only.
+
auto bufAddr = [&](Chunk *c) {
OutputSection *os = ctx.getOutputSection(c);
return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
os->getRVA();
};
- uint8_t *begin = bufAddr(firstPdata);
- uint8_t *end = bufAddr(lastPdata) + lastPdata->getSize();
+ uint8_t *begin = bufAddr(first);
+ uint8_t *end = bufAddr(last) + last->getSize();
+ if ((end - begin) % sizeof(T) != 0) {
+ fatal("unexpected .pdata size: " + Twine(end - begin) +
+ " is not a multiple of " + Twine(sizeof(T)));
+ }
+
+ parallelSort(MutableArrayRef<T>((T *)begin, (T *)end),
+ [](const T &a, const T &b) { return a.begin < b.begin; });
+}
+
+// Sort .pdata section contents according to PE/COFF spec 5.5.
+void Writer::sortExceptionTables() {
+ llvm::TimeTraceScope timeScope("Sort exception table");
+
+ struct EntryX64 {
+ ulittle32_t begin, end, unwind;
+ };
+ struct EntryArm {
+ ulittle32_t begin, unwind;
+ };
+
if (ctx.config.machine == AMD64) {
- struct Entry { ulittle32_t begin, end, unwind; };
- if ((end - begin) % sizeof(Entry) != 0) {
- fatal("unexpected .pdata size: " + Twine(end - begin) +
- " is not a multiple of " + Twine(sizeof(Entry)));
- }
- parallelSort(
- MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
- [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
+ sortExceptionTable<EntryX64>(firstPdata, lastPdata);
return;
}
if (ctx.config.machine == ARMNT || ctx.config.machine == ARM64) {
- struct Entry { ulittle32_t begin, unwind; };
- if ((end - begin) % sizeof(Entry) != 0) {
- fatal("unexpected .pdata size: " + Twine(end - begin) +
- " is not a multiple of " + Twine(sizeof(Entry)));
- }
- parallelSort(
- MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
- [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
+ sortExceptionTable<EntryArm>(firstPdata, lastPdata);
return;
}
- lld::errs() << "warning: don't know how to handle .pdata.\n";
+ if (firstPdata)
+ lld::errs() << "warning: don't know how to handle .pdata.\n";
}
// The CRT section contains, among other things, the array of function
>From 53b51ac3c5f489184505ae352a8e07309b486aa6 Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Thu, 16 Nov 2023 16:55:06 +0100
Subject: [PATCH 2/2] use reinterpret_cast and switch
---
lld/COFF/Writer.cpp | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 76c9b7604021f17..553e421f36ef965 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -2183,7 +2183,8 @@ void Writer::sortExceptionTable(Chunk *first, Chunk *last) {
" is not a multiple of " + Twine(sizeof(T)));
}
- parallelSort(MutableArrayRef<T>((T *)begin, (T *)end),
+ parallelSort(MutableArrayRef<T>(reinterpret_cast<T *>(begin),
+ reinterpret_cast<T *>(end)),
[](const T &a, const T &b) { return a.begin < b.begin; });
}
@@ -2198,16 +2199,19 @@ void Writer::sortExceptionTables() {
ulittle32_t begin, unwind;
};
- if (ctx.config.machine == AMD64) {
+ switch (ctx.config.machine) {
+ case AMD64:
sortExceptionTable<EntryX64>(firstPdata, lastPdata);
- return;
- }
- if (ctx.config.machine == ARMNT || ctx.config.machine == ARM64) {
+ break;
+ case ARMNT:
+ case ARM64:
sortExceptionTable<EntryArm>(firstPdata, lastPdata);
- return;
+ break;
+ default:
+ if (firstPdata)
+ lld::errs() << "warning: don't know how to handle .pdata.\n";
+ break;
}
- if (firstPdata)
- lld::errs() << "warning: don't know how to handle .pdata.\n";
}
// The CRT section contains, among other things, the array of function
More information about the llvm-commits
mailing list