Skip to content

Commit 7081585

Browse files
authored
[lld][COFF][NFC] Store pdata range as ChunkRange. (#74024)
1 parent e9c6f3f commit 7081585

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

lld/COFF/Writer.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ class PartialSectionKey {
197197
}
198198
};
199199

200+
struct ChunkRange {
201+
Chunk *first = nullptr, *last;
202+
};
203+
200204
// The writer writes a SymbolTable result to a file.
201205
class Writer {
202206
public:
@@ -247,7 +251,7 @@ class Writer {
247251
void writeBuildId();
248252
void writePEChecksum();
249253
void sortSections();
250-
template <typename T> void sortExceptionTable(Chunk *first, Chunk *last);
254+
template <typename T> void sortExceptionTable(ChunkRange &exceptionTable);
251255
void sortExceptionTables();
252256
void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
253257
void addSyntheticIdata();
@@ -311,7 +315,7 @@ class Writer {
311315
OutputSection *ctorsSec;
312316
OutputSection *dtorsSec;
313317

314-
// The first and last .pdata sections in the output file.
318+
// The range of .pdata sections in the output file.
315319
//
316320
// We need to keep track of the location of .pdata in whichever section it
317321
// gets merged into so that we can sort its contents and emit a correct data
@@ -320,8 +324,7 @@ class Writer {
320324
// are entirely linker-generated we can keep track of their locations using
321325
// the chunks that the linker creates. All .pdata chunks come from input
322326
// files, so we need to keep track of them separately.
323-
Chunk *firstPdata = nullptr;
324-
Chunk *lastPdata;
327+
ChunkRange pdata;
325328

326329
COFFLinkerContext &ctx;
327330
};
@@ -1408,8 +1411,8 @@ void Writer::createSymbolAndStringTable() {
14081411
void Writer::mergeSections() {
14091412
llvm::TimeTraceScope timeScope("Merge sections");
14101413
if (!pdataSec->chunks.empty()) {
1411-
firstPdata = pdataSec->chunks.front();
1412-
lastPdata = pdataSec->chunks.back();
1414+
pdata.first = pdataSec->chunks.front();
1415+
pdata.last = pdataSec->chunks.back();
14131416
}
14141417

14151418
for (auto &p : ctx.config.merge) {
@@ -1665,10 +1668,10 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
16651668
dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
16661669
dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
16671670
}
1668-
if (firstPdata) {
1669-
dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA();
1671+
if (pdata.first) {
1672+
dir[EXCEPTION_TABLE].RelativeVirtualAddress = pdata.first->getRVA();
16701673
dir[EXCEPTION_TABLE].Size =
1671-
lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
1674+
pdata.last->getRVA() + pdata.last->getSize() - pdata.first->getRVA();
16721675
}
16731676
if (relocSec->getVirtualSize()) {
16741677
dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
@@ -2166,15 +2169,18 @@ void Writer::writeBuildId() {
21662169

21672170
// Sort .pdata section contents according to PE/COFF spec 5.5.
21682171
template <typename T>
2169-
void Writer::sortExceptionTable(Chunk *first, Chunk *last) {
2172+
void Writer::sortExceptionTable(ChunkRange &exceptionTable) {
2173+
if (!exceptionTable.first)
2174+
return;
2175+
21702176
// We assume .pdata contains function table entries only.
21712177
auto bufAddr = [&](Chunk *c) {
21722178
OutputSection *os = ctx.getOutputSection(c);
21732179
return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
21742180
os->getRVA();
21752181
};
2176-
uint8_t *begin = bufAddr(first);
2177-
uint8_t *end = bufAddr(last) + last->getSize();
2182+
uint8_t *begin = bufAddr(exceptionTable.first);
2183+
uint8_t *end = bufAddr(exceptionTable.last) + exceptionTable.last->getSize();
21782184
if ((end - begin) % sizeof(T) != 0) {
21792185
fatal("unexpected .pdata size: " + Twine(end - begin) +
21802186
" is not a multiple of " + Twine(sizeof(T)));
@@ -2198,16 +2204,14 @@ void Writer::sortExceptionTables() {
21982204

21992205
switch (ctx.config.machine) {
22002206
case AMD64:
2201-
if (firstPdata)
2202-
sortExceptionTable<EntryX64>(firstPdata, lastPdata);
2207+
sortExceptionTable<EntryX64>(pdata);
22032208
break;
22042209
case ARMNT:
22052210
case ARM64:
2206-
if (firstPdata)
2207-
sortExceptionTable<EntryArm>(firstPdata, lastPdata);
2211+
sortExceptionTable<EntryArm>(pdata);
22082212
break;
22092213
default:
2210-
if (firstPdata)
2214+
if (pdata.first)
22112215
lld::errs() << "warning: don't know how to handle .pdata.\n";
22122216
break;
22132217
}

0 commit comments

Comments
 (0)