Skip to content

[lld][COFF][NFC] Store pdata range as ChunkRange. #74024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions lld/COFF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class PartialSectionKey {
}
};

struct ChunkRange {
Chunk *first = nullptr, *last;
};

// The writer writes a SymbolTable result to a file.
class Writer {
public:
Expand Down Expand Up @@ -247,7 +251,7 @@ class Writer {
void writeBuildId();
void writePEChecksum();
void sortSections();
template <typename T> void sortExceptionTable(Chunk *first, Chunk *last);
template <typename T> void sortExceptionTable(ChunkRange &exceptionTable);
void sortExceptionTables();
void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
void addSyntheticIdata();
Expand Down Expand Up @@ -311,7 +315,7 @@ class Writer {
OutputSection *ctorsSec;
OutputSection *dtorsSec;

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

COFFLinkerContext &ctx;
};
Expand Down Expand Up @@ -1408,8 +1411,8 @@ void Writer::createSymbolAndStringTable() {
void Writer::mergeSections() {
llvm::TimeTraceScope timeScope("Merge sections");
if (!pdataSec->chunks.empty()) {
firstPdata = pdataSec->chunks.front();
lastPdata = pdataSec->chunks.back();
pdata.first = pdataSec->chunks.front();
pdata.last = pdataSec->chunks.back();
}

for (auto &p : ctx.config.merge) {
Expand Down Expand Up @@ -1665,10 +1668,10 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
}
if (firstPdata) {
dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA();
if (pdata.first) {
dir[EXCEPTION_TABLE].RelativeVirtualAddress = pdata.first->getRVA();
dir[EXCEPTION_TABLE].Size =
lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
pdata.last->getRVA() + pdata.last->getSize() - pdata.first->getRVA();
}
if (relocSec->getVirtualSize()) {
dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
Expand Down Expand Up @@ -2166,15 +2169,18 @@ void Writer::writeBuildId() {

// Sort .pdata section contents according to PE/COFF spec 5.5.
template <typename T>
void Writer::sortExceptionTable(Chunk *first, Chunk *last) {
void Writer::sortExceptionTable(ChunkRange &exceptionTable) {
if (!exceptionTable.first)
return;

// 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(first);
uint8_t *end = bufAddr(last) + last->getSize();
uint8_t *begin = bufAddr(exceptionTable.first);
uint8_t *end = bufAddr(exceptionTable.last) + exceptionTable.last->getSize();
if ((end - begin) % sizeof(T) != 0) {
fatal("unexpected .pdata size: " + Twine(end - begin) +
" is not a multiple of " + Twine(sizeof(T)));
Expand All @@ -2198,16 +2204,14 @@ void Writer::sortExceptionTables() {

switch (ctx.config.machine) {
case AMD64:
if (firstPdata)
sortExceptionTable<EntryX64>(firstPdata, lastPdata);
sortExceptionTable<EntryX64>(pdata);
break;
case ARMNT:
case ARM64:
if (firstPdata)
sortExceptionTable<EntryArm>(firstPdata, lastPdata);
sortExceptionTable<EntryArm>(pdata);
break;
default:
if (firstPdata)
if (pdata.first)
lld::errs() << "warning: don't know how to handle .pdata.\n";
break;
}
Expand Down