Skip to content

Commit 8f238f6

Browse files
committed
[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.
1 parent b3230dd commit 8f238f6

File tree

6 files changed

+37
-46
lines changed

6 files changed

+37
-46
lines changed

lld/ELF/Config.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,12 @@ struct InStruct {
551551
std::unique_ptr<SymtabShndxSection> symTabShndx;
552552
};
553553

554-
struct Ctx {
554+
struct Ctx : CommonLinkerContext {
555555
Config arg;
556556
LinkerDriver driver;
557557
LinkerScript *script;
558558
std::unique_ptr<TargetInfo> target;
559559

560-
CommonLinkerContext *commonCtx;
561-
ErrorHandler *errHandler;
562-
563560
// These variables are initialized by Writer and should not be used before
564561
// Writer is initialized.
565562
uint8_t *bufferStart = nullptr;
@@ -689,18 +686,16 @@ static inline ArrayRef<VersionDefinition> namedVersionDefs(Ctx &ctx) {
689686
return llvm::ArrayRef(ctx.arg.versionDefinitions).slice(2);
690687
}
691688

692-
inline llvm::BumpPtrAllocator &bAlloc(Ctx &ctx) {
693-
return ctx.commonCtx->bAlloc;
694-
}
695-
inline llvm::StringSaver &saver(Ctx &ctx) { return ctx.commonCtx->saver; }
689+
inline llvm::BumpPtrAllocator &bAlloc(Ctx &ctx) { return ctx.bAlloc; }
690+
inline llvm::StringSaver &saver(Ctx &ctx) { return ctx.saver; }
696691
inline llvm::UniqueStringSaver &uniqueSaver(Ctx &ctx) {
697-
return ctx.commonCtx->uniqueSaver;
692+
return ctx.uniqueSaver;
698693
}
699694

700695
struct ELFSyncStream : SyncStream {
701696
Ctx &ctx;
702697
ELFSyncStream(Ctx &ctx, DiagLevel level)
703-
: SyncStream(*ctx.errHandler, level), ctx(ctx) {}
698+
: SyncStream(ctx.e, level), ctx(ctx) {}
704699
};
705700

706701
template <typename T>

lld/ELF/Driver.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ELFSyncStream elf::Err(Ctx &ctx) {
9090
}
9191
ELFSyncStream elf::ErrAlways(Ctx &ctx) { return {ctx, DiagLevel::Err}; }
9292
ELFSyncStream elf::Fatal(Ctx &ctx) { return {ctx, DiagLevel::Fatal}; }
93-
uint64_t elf::errCount(Ctx &ctx) { return ctx.errHandler->errorCount; }
93+
uint64_t elf::errCount(Ctx &ctx) { return ctx.e.errorCount; }
9494

9595
ELFSyncStream elf::InternalErr(Ctx &ctx, const uint8_t *buf) {
9696
ELFSyncStream s(ctx, DiagLevel::Err);
@@ -112,9 +112,9 @@ namespace lld {
112112
namespace elf {
113113
bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
114114
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
115-
Ctx ctx;
116115
// This driver-specific context will be freed later by unsafeLldMain().
117-
auto *context = new CommonLinkerContext;
116+
auto *context = new Ctx;
117+
Ctx &ctx = *context;
118118

119119
context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
120120
context->e.logName = args::getFilenameWithoutExe(args[0]);
@@ -124,8 +124,6 @@ bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
124124

125125
LinkerScript script(ctx);
126126
ctx.script = &script;
127-
ctx.commonCtx = context;
128-
ctx.errHandler = &context->e;
129127
ctx.symAux.emplace_back();
130128
ctx.symtab = std::make_unique<SymbolTable>(ctx);
131129

@@ -334,8 +332,8 @@ void LinkerDriver::addLibrary(StringRef name) {
334332
if (std::optional<std::string> path = searchLibrary(ctx, name))
335333
addFile(saver(ctx).save(*path), /*withLOption=*/true);
336334
else
337-
ctx.errHandler->error("unable to find library -l" + name,
338-
ErrorTag::LibNotFound, {name});
335+
ctx.e.error("unable to find library -l" + name, ErrorTag::LibNotFound,
336+
{name});
339337
}
340338

341339
// This function is called on startup. We need this for LTO since
@@ -589,11 +587,11 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
589587
opt::InputArgList args = parser.parse(ctx, argsArr.slice(1));
590588

591589
// Interpret these flags early because Err/Warn depend on them.
592-
ctx.errHandler->errorLimit = args::getInteger(args, OPT_error_limit, 20);
593-
ctx.errHandler->fatalWarnings =
590+
ctx.e.errorLimit = args::getInteger(args, OPT_error_limit, 20);
591+
ctx.e.fatalWarnings =
594592
args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) &&
595593
!args.hasArg(OPT_no_warnings);
596-
ctx.errHandler->suppressWarnings = args.hasArg(OPT_no_warnings);
594+
ctx.e.suppressWarnings = args.hasArg(OPT_no_warnings);
597595

598596
// Handle -help
599597
if (args.hasArg(OPT_help)) {
@@ -667,10 +665,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
667665
}
668666

669667
if (ctx.arg.timeTraceEnabled) {
670-
checkError(
671-
*ctx.errHandler,
672-
timeTraceProfilerWrite(args.getLastArgValue(OPT_time_trace_eq).str(),
673-
ctx.arg.outputFile));
668+
checkError(ctx.e, timeTraceProfilerWrite(
669+
args.getLastArgValue(OPT_time_trace_eq).str(),
670+
ctx.arg.outputFile));
674671
timeTraceProfilerCleanup();
675672
}
676673
}
@@ -1227,8 +1224,8 @@ static bool remapInputs(Ctx &ctx, StringRef line, const Twine &location) {
12271224

12281225
// Initializes Config members by the command line options.
12291226
static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
1230-
ctx.errHandler->verbose = args.hasArg(OPT_verbose);
1231-
ctx.errHandler->vsDiagnostics =
1227+
ctx.e.verbose = args.hasArg(OPT_verbose);
1228+
ctx.e.vsDiagnostics =
12321229
args.hasArg(OPT_visual_studio_diagnostics_format, false);
12331230

12341231
ctx.arg.allowMultipleDefinition =
@@ -1286,8 +1283,7 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
12861283
args.hasArg(OPT_enable_non_contiguous_regions);
12871284
ctx.arg.entry = args.getLastArgValue(OPT_entry);
12881285

1289-
ctx.errHandler->errorHandlingScript =
1290-
args.getLastArgValue(OPT_error_handling_script);
1286+
ctx.e.errorHandlingScript = args.getLastArgValue(OPT_error_handling_script);
12911287

12921288
ctx.arg.executeOnly =
12931289
args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);

lld/ELF/DriverUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ static void handleColorDiagnostics(Ctx &ctx, opt::InputArgList &args) {
5858
return;
5959
StringRef s = arg->getValue();
6060
if (s == "always")
61-
ctx.errHandler->errs().enable_colors(true);
61+
ctx.e.errs().enable_colors(true);
6262
else if (s == "never")
63-
ctx.errHandler->errs().enable_colors(false);
63+
ctx.e.errs().enable_colors(false);
6464
else if (s != "auto")
6565
ErrAlways(ctx) << "unknown option: --color-diagnostics=" << s;
6666
}
@@ -138,7 +138,7 @@ opt::InputArgList ELFOptTable::parse(Ctx &ctx, ArrayRef<const char *> argv) {
138138
}
139139

140140
void elf::printHelp(Ctx &ctx) {
141-
auto &outs = ctx.errHandler->outs();
141+
auto &outs = ctx.e.outs();
142142
ELFOptTable().printHelp(
143143
outs, (ctx.arg.progName + " [options] file...").str().c_str(), "lld",
144144
false /*ShowHidden*/, true /*ShowAllAliases*/);

lld/ELF/LTO.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ static lto::Config createConfig(Ctx &ctx) {
165165
}
166166

167167
if (!ctx.arg.saveTempsArgs.empty())
168-
checkError(*ctx.errHandler, c.addSaveTemps(ctx.arg.outputFile.str() + ".",
169-
/*UseInputModulePath*/ true,
170-
ctx.arg.saveTempsArgs));
168+
checkError(ctx.e, c.addSaveTemps(ctx.arg.outputFile.str() + ".",
169+
/*UseInputModulePath*/ true,
170+
ctx.arg.saveTempsArgs));
171171
return c;
172172
}
173173

@@ -278,7 +278,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
278278
// their values are still not final.
279279
r.LinkerRedefined = sym->scriptDefined;
280280
}
281-
checkError(*ctx.errHandler, ltoObj->add(std::move(f.obj), resols));
281+
checkError(ctx.e, ltoObj->add(std::move(f.obj), resols));
282282
}
283283

284284
// If LazyObjFile has not been added to link, emit empty index files.
@@ -329,14 +329,14 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
329329
}));
330330

331331
if (!ctx.bitcodeFiles.empty())
332-
checkError(*ctx.errHandler, ltoObj->run(
333-
[&](size_t task, const Twine &moduleName) {
334-
buf[task].first = moduleName.str();
335-
return std::make_unique<CachedFileStream>(
336-
std::make_unique<raw_svector_ostream>(
337-
buf[task].second));
338-
},
339-
cache));
332+
checkError(ctx.e, ltoObj->run(
333+
[&](size_t task, const Twine &moduleName) {
334+
buf[task].first = moduleName.str();
335+
return std::make_unique<CachedFileStream>(
336+
std::make_unique<raw_svector_ostream>(
337+
buf[task].second));
338+
},
339+
cache));
340340

341341
// Emit empty index files for non-indexed files but not in single-module mode.
342342
if (ctx.arg.thinLTOModulesToCompile.empty()) {

lld/ELF/Relocations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ static void reportUndefinedSymbol(Ctx &ctx, const UndefinedDiag &undef,
777777
if (undef.isWarning)
778778
Warn(ctx) << msg;
779779
else
780-
ctx.errHandler->error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
780+
ctx.e.error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
781781
}
782782

783783
void elf::reportUndefinedSymbols(Ctx &ctx) {

lld/ELF/Writer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ template <class ELFT> class Writer {
4949
public:
5050
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
5151

52-
Writer(Ctx &ctx) : ctx(ctx), buffer(ctx.errHandler->outputBuffer) {}
52+
Writer(Ctx &ctx) : ctx(ctx), buffer(ctx.e.outputBuffer) {}
5353

5454
void run();
5555

@@ -342,7 +342,7 @@ template <class ELFT> void Writer<ELFT>::run() {
342342

343343
// Handle --print-memory-usage option.
344344
if (ctx.arg.printMemoryUsage)
345-
ctx.script->printMemoryUsage(ctx.errHandler->outs());
345+
ctx.script->printMemoryUsage(ctx.e.outs());
346346

347347
if (ctx.arg.checkSections)
348348
checkSections();

0 commit comments

Comments
 (0)