[lld] 8f238f6 - [ELF] Make Ctx inherit from CommonLinkerContext
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 16 22:29:00 PST 2024
Author: Fangrui Song
Date: 2024-11-16T22:28:55-08:00
New Revision: 8f238f662c8237b88392f8a94469cd50d86636d6
URL: https://github.com/llvm/llvm-project/commit/8f238f662c8237b88392f8a94469cd50d86636d6
DIFF: https://github.com/llvm/llvm-project/commit/8f238f662c8237b88392f8a94469cd50d86636d6.diff
LOG: [ELF] Make Ctx inherit from CommonLinkerContext
link calls `new CommonLinkerContext`. Now that `Ctx ctx` is a local
variable, we can make it inherit from CommonLinkerContext.
Added:
Modified:
lld/ELF/Config.h
lld/ELF/Driver.cpp
lld/ELF/DriverUtils.cpp
lld/ELF/LTO.cpp
lld/ELF/Relocations.cpp
lld/ELF/Writer.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 06c54283f7ea40..431d675b41258c 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -551,15 +551,12 @@ struct InStruct {
std::unique_ptr<SymtabShndxSection> symTabShndx;
};
-struct Ctx {
+struct Ctx : CommonLinkerContext {
Config arg;
LinkerDriver driver;
LinkerScript *script;
std::unique_ptr<TargetInfo> target;
- CommonLinkerContext *commonCtx;
- ErrorHandler *errHandler;
-
// These variables are initialized by Writer and should not be used before
// Writer is initialized.
uint8_t *bufferStart = nullptr;
@@ -689,18 +686,16 @@ static inline ArrayRef<VersionDefinition> namedVersionDefs(Ctx &ctx) {
return llvm::ArrayRef(ctx.arg.versionDefinitions).slice(2);
}
-inline llvm::BumpPtrAllocator &bAlloc(Ctx &ctx) {
- return ctx.commonCtx->bAlloc;
-}
-inline llvm::StringSaver &saver(Ctx &ctx) { return ctx.commonCtx->saver; }
+inline llvm::BumpPtrAllocator &bAlloc(Ctx &ctx) { return ctx.bAlloc; }
+inline llvm::StringSaver &saver(Ctx &ctx) { return ctx.saver; }
inline llvm::UniqueStringSaver &uniqueSaver(Ctx &ctx) {
- return ctx.commonCtx->uniqueSaver;
+ return ctx.uniqueSaver;
}
struct ELFSyncStream : SyncStream {
Ctx &ctx;
ELFSyncStream(Ctx &ctx, DiagLevel level)
- : SyncStream(*ctx.errHandler, level), ctx(ctx) {}
+ : SyncStream(ctx.e, level), ctx(ctx) {}
};
template <typename T>
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 9d784040aadf28..927fa9da65fdea 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -90,7 +90,7 @@ ELFSyncStream elf::Err(Ctx &ctx) {
}
ELFSyncStream elf::ErrAlways(Ctx &ctx) { return {ctx, DiagLevel::Err}; }
ELFSyncStream elf::Fatal(Ctx &ctx) { return {ctx, DiagLevel::Fatal}; }
-uint64_t elf::errCount(Ctx &ctx) { return ctx.errHandler->errorCount; }
+uint64_t elf::errCount(Ctx &ctx) { return ctx.e.errorCount; }
ELFSyncStream elf::InternalErr(Ctx &ctx, const uint8_t *buf) {
ELFSyncStream s(ctx, DiagLevel::Err);
@@ -112,9 +112,9 @@ namespace lld {
namespace elf {
bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
- Ctx ctx;
// This driver-specific context will be freed later by unsafeLldMain().
- auto *context = new CommonLinkerContext;
+ auto *context = new Ctx;
+ Ctx &ctx = *context;
context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
context->e.logName = args::getFilenameWithoutExe(args[0]);
@@ -124,8 +124,6 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
LinkerScript script(ctx);
ctx.script = &script;
- ctx.commonCtx = context;
- ctx.errHandler = &context->e;
ctx.symAux.emplace_back();
ctx.symtab = std::make_unique<SymbolTable>(ctx);
@@ -334,8 +332,8 @@ void LinkerDriver::addLibrary(StringRef name) {
if (std::optional<std::string> path = searchLibrary(ctx, name))
addFile(saver(ctx).save(*path), /*withLOption=*/true);
else
- ctx.errHandler->error("unable to find library -l" + name,
- ErrorTag::LibNotFound, {name});
+ ctx.e.error("unable to find library -l" + name, ErrorTag::LibNotFound,
+ {name});
}
// This function is called on startup. We need this for LTO since
@@ -589,11 +587,11 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
opt::InputArgList args = parser.parse(ctx, argsArr.slice(1));
// Interpret these flags early because Err/Warn depend on them.
- ctx.errHandler->errorLimit = args::getInteger(args, OPT_error_limit, 20);
- ctx.errHandler->fatalWarnings =
+ ctx.e.errorLimit = args::getInteger(args, OPT_error_limit, 20);
+ ctx.e.fatalWarnings =
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) &&
!args.hasArg(OPT_no_warnings);
- ctx.errHandler->suppressWarnings = args.hasArg(OPT_no_warnings);
+ ctx.e.suppressWarnings = args.hasArg(OPT_no_warnings);
// Handle -help
if (args.hasArg(OPT_help)) {
@@ -667,10 +665,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
}
if (ctx.arg.timeTraceEnabled) {
- checkError(
- *ctx.errHandler,
- timeTraceProfilerWrite(args.getLastArgValue(OPT_time_trace_eq).str(),
- ctx.arg.outputFile));
+ checkError(ctx.e, timeTraceProfilerWrite(
+ args.getLastArgValue(OPT_time_trace_eq).str(),
+ ctx.arg.outputFile));
timeTraceProfilerCleanup();
}
}
@@ -1227,8 +1224,8 @@ static bool remapInputs(Ctx &ctx, StringRef line, const Twine &location) {
// Initializes Config members by the command line options.
static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
- ctx.errHandler->verbose = args.hasArg(OPT_verbose);
- ctx.errHandler->vsDiagnostics =
+ ctx.e.verbose = args.hasArg(OPT_verbose);
+ ctx.e.vsDiagnostics =
args.hasArg(OPT_visual_studio_diagnostics_format, false);
ctx.arg.allowMultipleDefinition =
@@ -1286,8 +1283,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
args.hasArg(OPT_enable_non_contiguous_regions);
ctx.arg.entry = args.getLastArgValue(OPT_entry);
- ctx.errHandler->errorHandlingScript =
- args.getLastArgValue(OPT_error_handling_script);
+ ctx.e.errorHandlingScript = args.getLastArgValue(OPT_error_handling_script);
ctx.arg.executeOnly =
args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index 33006c1d322ca0..97ede74ef736bd 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -58,9 +58,9 @@ static void handleColorDiagnostics(Ctx &ctx, opt::InputArgList &args) {
return;
StringRef s = arg->getValue();
if (s == "always")
- ctx.errHandler->errs().enable_colors(true);
+ ctx.e.errs().enable_colors(true);
else if (s == "never")
- ctx.errHandler->errs().enable_colors(false);
+ ctx.e.errs().enable_colors(false);
else if (s != "auto")
ErrAlways(ctx) << "unknown option: --color-diagnostics=" << s;
}
@@ -138,7 +138,7 @@ opt::InputArgList ELFOptTable::parse(Ctx &ctx, ArrayRef<const char *> argv) {
}
void elf::printHelp(Ctx &ctx) {
- auto &outs = ctx.errHandler->outs();
+ auto &outs = ctx.e.outs();
ELFOptTable().printHelp(
outs, (ctx.arg.progName + " [options] file...").str().c_str(), "lld",
false /*ShowHidden*/, true /*ShowAllAliases*/);
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 36161c3d648708..7379f97f59ca19 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -165,9 +165,9 @@ static lto::Config createConfig(Ctx &ctx) {
}
if (!ctx.arg.saveTempsArgs.empty())
- checkError(*ctx.errHandler, c.addSaveTemps(ctx.arg.outputFile.str() + ".",
- /*UseInputModulePath*/ true,
- ctx.arg.saveTempsArgs));
+ checkError(ctx.e, c.addSaveTemps(ctx.arg.outputFile.str() + ".",
+ /*UseInputModulePath*/ true,
+ ctx.arg.saveTempsArgs));
return c;
}
@@ -278,7 +278,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
// their values are still not final.
r.LinkerRedefined = sym->scriptDefined;
}
- checkError(*ctx.errHandler, ltoObj->add(std::move(f.obj), resols));
+ checkError(ctx.e, ltoObj->add(std::move(f.obj), resols));
}
// If LazyObjFile has not been added to link, emit empty index files.
@@ -329,14 +329,14 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
}));
if (!ctx.bitcodeFiles.empty())
- checkError(*ctx.errHandler, ltoObj->run(
- [&](size_t task, const Twine &moduleName) {
- buf[task].first = moduleName.str();
- return std::make_unique<CachedFileStream>(
- std::make_unique<raw_svector_ostream>(
- buf[task].second));
- },
- cache));
+ checkError(ctx.e, ltoObj->run(
+ [&](size_t task, const Twine &moduleName) {
+ buf[task].first = moduleName.str();
+ return std::make_unique<CachedFileStream>(
+ std::make_unique<raw_svector_ostream>(
+ buf[task].second));
+ },
+ cache));
// Emit empty index files for non-indexed files but not in single-module mode.
if (ctx.arg.thinLTOModulesToCompile.empty()) {
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index a000e79d5f4216..1f3c5708f92535 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -777,7 +777,7 @@ static void reportUndefinedSymbol(Ctx &ctx, const UndefinedDiag &undef,
if (undef.isWarning)
Warn(ctx) << msg;
else
- ctx.errHandler->error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
+ ctx.e.error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
}
void elf::reportUndefinedSymbols(Ctx &ctx) {
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 32bb05f83d15b1..0ec0da2a4af2c8 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -49,7 +49,7 @@ template <class ELFT> class Writer {
public:
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
- Writer(Ctx &ctx) : ctx(ctx), buffer(ctx.errHandler->outputBuffer) {}
+ Writer(Ctx &ctx) : ctx(ctx), buffer(ctx.e.outputBuffer) {}
void run();
@@ -342,7 +342,7 @@ template <class ELFT> void Writer<ELFT>::run() {
// Handle --print-memory-usage option.
if (ctx.arg.printMemoryUsage)
- ctx.script->printMemoryUsage(ctx.errHandler->outs());
+ ctx.script->printMemoryUsage(ctx.e.outs());
if (ctx.arg.checkSections)
checkSections();
More information about the llvm-commits
mailing list