From 17473182f584c8eac09fe915256dcbd761b25286 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 29 Sep 2024 15:39:52 -0700 Subject: [PATCH] [ELF] Pass Ctx & to MapFile --- lld/ELF/MapFile.cpp | 35 ++++++++++++++++++----------------- lld/ELF/MapFile.h | 3 ++- lld/ELF/Writer.cpp | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp index 17c694d410a6..3495cdb0bc66 100644 --- a/lld/ELF/MapFile.cpp +++ b/lld/ELF/MapFile.cpp @@ -44,7 +44,7 @@ static constexpr char indent8[] = " "; // 8 spaces static constexpr char indent16[] = " "; // 16 spaces // Print out the first three columns of a line. -static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma, +static void writeHeader(Ctx &ctx, raw_ostream &os, uint64_t vma, uint64_t lma, uint64_t size, uint64_t align) { if (ctx.arg.is64) os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align); @@ -90,14 +90,14 @@ static SymbolMapTy getSectionSyms(ArrayRef syms) { // Demangling symbols (which is what toString() does) is slow, so // we do that in batch using parallel-for. static DenseMap -getSymbolStrings(ArrayRef syms) { +getSymbolStrings(Ctx &ctx, ArrayRef syms) { auto strs = std::make_unique(syms.size()); parallelFor(0, syms.size(), [&](size_t i) { raw_string_ostream os(strs[i]); OutputSection *osec = syms[i]->getOutputSection(); uint64_t vma = syms[i]->getVA(); uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0; - writeHeader(os, vma, lma, syms[i]->getSize(), 1); + writeHeader(ctx, os, vma, lma, syms[i]->getSize(), 1); os << indent16 << toString(*syms[i]); }); @@ -113,7 +113,7 @@ getSymbolStrings(ArrayRef syms) { // .eh_frame tend to contain a lot of section pieces that are contiguous // both in input file and output file. Such pieces are squashed before // being displayed to make output compact. -static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) { +static void printEhFrame(Ctx &ctx, raw_ostream &os, const EhFrameSection *sec) { std::vector pieces; auto add = [&](const EhSectionPiece &p) { @@ -139,18 +139,18 @@ static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) { // Print out section pieces. const OutputSection *osec = sec->getOutputSection(); for (EhSectionPiece &p : pieces) { - writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff, + writeHeader(ctx, os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff, p.size, 1); os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x" << Twine::utohexstr(p.inputOff) + ")\n"; } } -static void writeMapFile(raw_fd_ostream &os) { +static void writeMapFile(Ctx &ctx, raw_fd_ostream &os) { // Collect symbol info that we want to print out. std::vector syms = getSymbols(); SymbolMapTy sectionSyms = getSectionSyms(syms); - DenseMap symStr = getSymbolStrings(syms); + DenseMap symStr = getSymbolStrings(ctx, syms); // Print out the header line. int w = ctx.arg.is64 ? 16 : 8; @@ -163,7 +163,7 @@ static void writeMapFile(raw_fd_ostream &os) { if (assign->provide && !assign->sym) continue; uint64_t lma = osec ? osec->getLMA() + assign->addr - osec->getVA(0) : 0; - writeHeader(os, assign->addr, lma, assign->size, 1); + writeHeader(ctx, os, assign->addr, lma, assign->size, 1); os << assign->commandString << '\n'; continue; } @@ -171,7 +171,8 @@ static void writeMapFile(raw_fd_ostream &os) { continue; osec = &cast(cmd)->osec; - writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->addralign); + writeHeader(ctx, os, osec->addr, osec->getLMA(), osec->size, + osec->addralign); os << osec->name << '\n'; // Dump symbols for each input section. @@ -179,11 +180,11 @@ static void writeMapFile(raw_fd_ostream &os) { if (auto *isd = dyn_cast(subCmd)) { for (InputSection *isec : isd->sections) { if (auto *ehSec = dyn_cast(isec)) { - printEhFrame(os, ehSec); + printEhFrame(ctx, os, ehSec); continue; } - writeHeader(os, isec->getVA(), osec->getLMA() + isec->outSecOff, + writeHeader(ctx, os, isec->getVA(), osec->getLMA() + isec->outSecOff, isec->getSize(), isec->addralign); os << indent8 << toString(isec) << '\n'; for (Symbol *sym : llvm::make_first_range(sectionSyms[isec])) @@ -193,7 +194,7 @@ static void writeMapFile(raw_fd_ostream &os) { } if (auto *data = dyn_cast(subCmd)) { - writeHeader(os, osec->addr + data->offset, + writeHeader(ctx, os, osec->addr + data->offset, osec->getLMA() + data->offset, data->size, 1); os << indent8 << data->commandString << '\n'; continue; @@ -202,7 +203,7 @@ static void writeMapFile(raw_fd_ostream &os) { if (auto *assign = dyn_cast(subCmd)) { if (assign->provide && !assign->sym) continue; - writeHeader(os, assign->addr, + writeHeader(ctx, os, assign->addr, osec->getLMA() + assign->addr - osec->getVA(0), assign->size, 1); os << indent8 << assign->commandString << '\n'; @@ -223,7 +224,7 @@ static void writeMapFile(raw_fd_ostream &os) { // // In this case, strlen is defined by libc.so.6 and used by other two // files. -static void writeCref(raw_fd_ostream &os) { +static void writeCref(Ctx &ctx, raw_fd_ostream &os) { // Collect symbols and files. MapVector> map; for (ELFFileBase *file : ctx.objectFiles) { @@ -256,7 +257,7 @@ static void writeCref(raw_fd_ostream &os) { } } -void elf::writeMapAndCref() { +void elf::writeMapAndCref(Ctx &ctx) { if (ctx.arg.mapFile.empty() && !ctx.arg.cref) return; @@ -272,7 +273,7 @@ void elf::writeMapAndCref() { } if (!ctx.arg.mapFile.empty()) - writeMapFile(os); + writeMapFile(ctx, os); if (ctx.arg.cref) - writeCref(os); + writeCref(ctx, os); } diff --git a/lld/ELF/MapFile.h b/lld/ELF/MapFile.h index b271f627df57..c4efd33a3095 100644 --- a/lld/ELF/MapFile.h +++ b/lld/ELF/MapFile.h @@ -10,7 +10,8 @@ #define LLD_ELF_MAPFILE_H namespace lld::elf { -void writeMapAndCref(); +struct Ctx; +void writeMapAndCref(Ctx &); } #endif diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 27b3cf64a43a..7c928a80a10d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -336,7 +336,7 @@ template void Writer::run() { // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections() // because the files may be useful in case checkSections() or openFile() // fails, for example, due to an erroneous file size. - writeMapAndCref(); + writeMapAndCref(ctx); // Handle --print-memory-usage option. if (ctx.arg.printMemoryUsage)