Skip to content

Commit ec42d54

Browse files
authored
[lld][COFF][NFC] Factor out exception table sorting. (#72518)
This is a preparation for ARM64EC support, which needs to sort both ARM and x86_64 tables separately.
1 parent a67b85e commit ec42d54

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

lld/COFF/Writer.cpp

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ class Writer {
247247
void writeBuildId();
248248
void writePEChecksum();
249249
void sortSections();
250-
void sortExceptionTable();
250+
template <typename T> void sortExceptionTable(Chunk *first, Chunk *last);
251+
void sortExceptionTables();
251252
void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
252253
void addSyntheticIdata();
253254
void sortBySectionOrder(std::vector<Chunk *> &chunks);
@@ -751,7 +752,7 @@ void Writer::run() {
751752
}
752753
writeSections();
753754
prepareLoadConfig();
754-
sortExceptionTable();
755+
sortExceptionTables();
755756

756757
// Fix up the alignment in the TLS Directory's characteristic field,
757758
// if a specific alignment value is needed
@@ -2164,41 +2165,52 @@ void Writer::writeBuildId() {
21642165
}
21652166

21662167
// Sort .pdata section contents according to PE/COFF spec 5.5.
2167-
void Writer::sortExceptionTable() {
2168-
if (!firstPdata)
2169-
return;
2170-
llvm::TimeTraceScope timeScope("Sort exception table");
2168+
template <typename T>
2169+
void Writer::sortExceptionTable(Chunk *first, Chunk *last) {
21712170
// We assume .pdata contains function table entries only.
21722171
auto bufAddr = [&](Chunk *c) {
21732172
OutputSection *os = ctx.getOutputSection(c);
21742173
return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
21752174
os->getRVA();
21762175
};
2177-
uint8_t *begin = bufAddr(firstPdata);
2178-
uint8_t *end = bufAddr(lastPdata) + lastPdata->getSize();
2179-
if (ctx.config.machine == AMD64) {
2180-
struct Entry { ulittle32_t begin, end, unwind; };
2181-
if ((end - begin) % sizeof(Entry) != 0) {
2182-
fatal("unexpected .pdata size: " + Twine(end - begin) +
2183-
" is not a multiple of " + Twine(sizeof(Entry)));
2184-
}
2185-
parallelSort(
2186-
MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
2187-
[](const Entry &a, const Entry &b) { return a.begin < b.begin; });
2188-
return;
2176+
uint8_t *begin = bufAddr(first);
2177+
uint8_t *end = bufAddr(last) + last->getSize();
2178+
if ((end - begin) % sizeof(T) != 0) {
2179+
fatal("unexpected .pdata size: " + Twine(end - begin) +
2180+
" is not a multiple of " + Twine(sizeof(T)));
21892181
}
2190-
if (ctx.config.machine == ARMNT || ctx.config.machine == ARM64) {
2191-
struct Entry { ulittle32_t begin, unwind; };
2192-
if ((end - begin) % sizeof(Entry) != 0) {
2193-
fatal("unexpected .pdata size: " + Twine(end - begin) +
2194-
" is not a multiple of " + Twine(sizeof(Entry)));
2195-
}
2196-
parallelSort(
2197-
MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
2198-
[](const Entry &a, const Entry &b) { return a.begin < b.begin; });
2199-
return;
2182+
2183+
parallelSort(MutableArrayRef<T>(reinterpret_cast<T *>(begin),
2184+
reinterpret_cast<T *>(end)),
2185+
[](const T &a, const T &b) { return a.begin < b.begin; });
2186+
}
2187+
2188+
// Sort .pdata section contents according to PE/COFF spec 5.5.
2189+
void Writer::sortExceptionTables() {
2190+
llvm::TimeTraceScope timeScope("Sort exception table");
2191+
2192+
struct EntryX64 {
2193+
ulittle32_t begin, end, unwind;
2194+
};
2195+
struct EntryArm {
2196+
ulittle32_t begin, unwind;
2197+
};
2198+
2199+
switch (ctx.config.machine) {
2200+
case AMD64:
2201+
if (firstPdata)
2202+
sortExceptionTable<EntryX64>(firstPdata, lastPdata);
2203+
break;
2204+
case ARMNT:
2205+
case ARM64:
2206+
if (firstPdata)
2207+
sortExceptionTable<EntryArm>(firstPdata, lastPdata);
2208+
break;
2209+
default:
2210+
if (firstPdata)
2211+
lld::errs() << "warning: don't know how to handle .pdata.\n";
2212+
break;
22002213
}
2201-
lld::errs() << "warning: don't know how to handle .pdata.\n";
22022214
}
22032215

22042216
// The CRT section contains, among other things, the array of function

0 commit comments

Comments
 (0)