Skip to content

Commit cf57a67

Browse files
committed
[ELF] ScriptParser: pass Ctx to ScriptParser and ScriptLexer. NFC
1 parent f86050d commit cf57a67

File tree

5 files changed

+70
-66
lines changed

5 files changed

+70
-66
lines changed

lld/ELF/Driver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {
293293

294294
switch (identify_magic(mbref.getBuffer())) {
295295
case file_magic::unknown:
296-
readLinkerScript(mbref);
296+
readLinkerScript(ctx, mbref);
297297
return;
298298
case file_magic::archive: {
299299
auto members = getArchiveMembers(mbref);
@@ -1810,12 +1810,12 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
18101810
for (auto *arg :
18111811
args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list))
18121812
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
1813-
readDynamicList(*buffer);
1813+
readDynamicList(ctx, *buffer);
18141814

18151815
for (auto *arg : args.filtered(OPT_version_script))
18161816
if (std::optional<std::string> path = searchScript(arg->getValue())) {
18171817
if (std::optional<MemoryBufferRef> buffer = readFile(*path))
1818-
readVersionScript(*buffer);
1818+
readVersionScript(ctx, *buffer);
18191819
} else {
18201820
error(Twine("cannot find version script ") + arg->getValue());
18211821
}
@@ -1943,7 +1943,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
19431943
hasInput = true;
19441944
break;
19451945
case OPT_defsym: {
1946-
readDefsym(MemoryBufferRef(arg->getValue(), "--defsym"));
1946+
readDefsym(ctx, MemoryBufferRef(arg->getValue(), "--defsym"));
19471947
break;
19481948
}
19491949
case OPT_script:
@@ -1953,7 +1953,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
19531953
if (arg->getOption().matches(OPT_default_script)) {
19541954
defaultScript = mb;
19551955
} else {
1956-
readLinkerScript(*mb);
1956+
readLinkerScript(ctx, *mb);
19571957
hasScript = true;
19581958
}
19591959
}
@@ -2039,7 +2039,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
20392039
}
20402040

20412041
if (defaultScript && !hasScript)
2042-
readLinkerScript(*defaultScript);
2042+
readLinkerScript(ctx, *defaultScript);
20432043
if (files.empty() && !hasInput && errorCount() == 0)
20442044
error("no input files");
20452045
}

lld/ELF/ScriptLexer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,22 @@ using namespace llvm;
3939
using namespace lld;
4040
using namespace lld::elf;
4141

42-
ScriptLexer::Buffer::Buffer(MemoryBufferRef mb)
42+
ScriptLexer::Buffer::Buffer(Ctx &ctx, MemoryBufferRef mb)
4343
: s(mb.getBuffer()), filename(mb.getBufferIdentifier()),
4444
begin(mb.getBufferStart()) {
45-
if (config->sysroot == "")
45+
if (ctx.arg.sysroot == "")
4646
return;
4747
StringRef path = filename;
4848
for (; !path.empty(); path = sys::path::parent_path(path)) {
49-
if (!sys::fs::equivalent(config->sysroot, path))
49+
if (!sys::fs::equivalent(ctx.arg.sysroot, path))
5050
continue;
5151
isUnderSysroot = true;
5252
return;
5353
}
5454
}
5555

56-
ScriptLexer::ScriptLexer(MemoryBufferRef mb) : curBuf(mb), mbs(1, mb) {
56+
ScriptLexer::ScriptLexer(Ctx &ctx, MemoryBufferRef mb)
57+
: curBuf(ctx, mb), mbs(1, mb) {
5758
activeFilenames.insert(mb.getBufferIdentifier());
5859
}
5960

lld/ELF/ScriptLexer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <vector>
1818

1919
namespace lld::elf {
20+
struct Ctx;
2021

2122
class ScriptLexer {
2223
protected:
@@ -30,7 +31,7 @@ class ScriptLexer {
3031
bool isUnderSysroot = false;
3132

3233
Buffer() = default;
33-
Buffer(MemoryBufferRef mb);
34+
Buffer(Ctx &ctx, MemoryBufferRef mb);
3435
};
3536
// The current buffer and parent buffers due to INCLUDE.
3637
Buffer curBuf;
@@ -57,7 +58,7 @@ class ScriptLexer {
5758
bool eof = false;
5859

5960
public:
60-
explicit ScriptLexer(MemoryBufferRef mb);
61+
explicit ScriptLexer(Ctx &ctx, MemoryBufferRef mb);
6162

6263
void setError(const Twine &msg);
6364
void lex();

lld/ELF/ScriptParser.cpp

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ using namespace lld::elf;
4747
namespace {
4848
class ScriptParser final : ScriptLexer {
4949
public:
50-
ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
51-
}
50+
ScriptParser(Ctx &ctx, MemoryBufferRef mb) : ScriptLexer(ctx, mb) {}
5251

5352
void readLinkerScript();
5453
void readVersionScript();
@@ -197,7 +196,7 @@ void ScriptParser::readDynamicList() {
197196
}
198197

199198
for (SymbolVersion v : globals)
200-
config->dynamicList.push_back(v);
199+
ctx.arg.dynamicList.push_back(v);
201200
}
202201

203202
void ScriptParser::readVersionScript() {
@@ -313,11 +312,11 @@ void ScriptParser::readNoCrossRefs(bool to) {
313312
void ScriptParser::addFile(StringRef s) {
314313
if (curBuf.isUnderSysroot && s.starts_with("/")) {
315314
SmallString<128> pathData;
316-
StringRef path = (config->sysroot + s).toStringRef(pathData);
315+
StringRef path = (ctx.arg.sysroot + s).toStringRef(pathData);
317316
if (sys::fs::exists(path))
318317
ctx.driver.addFile(saver().save(path), /*withLOption=*/false);
319318
else
320-
setError("cannot find " + s + " inside " + config->sysroot);
319+
setError("cannot find " + s + " inside " + ctx.arg.sysroot);
321320
return;
322321
}
323322

@@ -326,10 +325,10 @@ void ScriptParser::addFile(StringRef s) {
326325
ctx.driver.addFile(s, /*withLOption=*/false);
327326
} else if (s.starts_with("=")) {
328327
// Case 2: relative to the sysroot.
329-
if (config->sysroot.empty())
328+
if (ctx.arg.sysroot.empty())
330329
ctx.driver.addFile(s.substr(1), /*withLOption=*/false);
331330
else
332-
ctx.driver.addFile(saver().save(config->sysroot + "/" + s.substr(1)),
331+
ctx.driver.addFile(saver().save(ctx.arg.sysroot + "/" + s.substr(1)),
333332
/*withLOption=*/false);
334333
} else if (s.starts_with("-l")) {
335334
// Case 3: search in the list of library paths.
@@ -361,26 +360,26 @@ void ScriptParser::addFile(StringRef s) {
361360

362361
void ScriptParser::readAsNeeded() {
363362
expect("(");
364-
bool orig = config->asNeeded;
365-
config->asNeeded = true;
363+
bool orig = ctx.arg.asNeeded;
364+
ctx.arg.asNeeded = true;
366365
while (auto tok = till(")"))
367366
addFile(unquote(tok));
368-
config->asNeeded = orig;
367+
ctx.arg.asNeeded = orig;
369368
}
370369

371370
void ScriptParser::readEntry() {
372371
// -e <symbol> takes predecence over ENTRY(<symbol>).
373372
expect("(");
374373
StringRef name = readName();
375-
if (config->entry.empty())
376-
config->entry = name;
374+
if (ctx.arg.entry.empty())
375+
ctx.arg.entry = name;
377376
expect(")");
378377
}
379378

380379
void ScriptParser::readExtern() {
381380
expect("(");
382381
while (auto tok = till(")"))
383-
config->undefined.push_back(unquote(tok));
382+
ctx.arg.undefined.push_back(unquote(tok));
384383
}
385384

386385
void ScriptParser::readGroup() {
@@ -402,7 +401,7 @@ void ScriptParser::readInclude() {
402401
if (std::optional<std::string> path = searchScript(name)) {
403402
if (std::optional<MemoryBufferRef> mb = readFile(*path)) {
404403
buffers.push_back(curBuf);
405-
curBuf = Buffer(*mb);
404+
curBuf = Buffer(ctx, *mb);
406405
mbs.push_back(*mb);
407406
}
408407
return;
@@ -424,8 +423,8 @@ void ScriptParser::readOutput() {
424423
// -o <file> takes predecence over OUTPUT(<file>).
425424
expect("(");
426425
StringRef name = readName();
427-
if (config->outputFile.empty())
428-
config->outputFile = name;
426+
if (ctx.arg.outputFile.empty())
427+
ctx.arg.outputFile = name;
429428
expect(")");
430429
}
431430

@@ -479,34 +478,34 @@ void ScriptParser::readOutputFormat() {
479478
if (!consume(")")) {
480479
expect(",");
481480
StringRef tmp = readName();
482-
if (config->optEB)
481+
if (ctx.arg.optEB)
483482
s = tmp;
484483
expect(",");
485484
tmp = readName();
486-
if (config->optEL)
485+
if (ctx.arg.optEL)
487486
s = tmp;
488487
consume(")");
489488
}
490489
// If more than one OUTPUT_FORMAT is specified, only the first is checked.
491-
if (!config->bfdname.empty())
490+
if (!ctx.arg.bfdname.empty())
492491
return;
493-
config->bfdname = s;
492+
ctx.arg.bfdname = s;
494493

495494
if (s == "binary") {
496-
config->oFormatBinary = true;
495+
ctx.arg.oFormatBinary = true;
497496
return;
498497
}
499498

500499
if (s.consume_back("-freebsd"))
501-
config->osabi = ELFOSABI_FREEBSD;
500+
ctx.arg.osabi = ELFOSABI_FREEBSD;
502501

503-
std::tie(config->ekind, config->emachine) = parseBfdName(s);
504-
if (config->emachine == EM_NONE)
505-
setError("unknown output format name: " + config->bfdname);
502+
std::tie(ctx.arg.ekind, ctx.arg.emachine) = parseBfdName(s);
503+
if (ctx.arg.emachine == EM_NONE)
504+
setError("unknown output format name: " + ctx.arg.bfdname);
506505
if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips")
507-
config->mipsN32Abi = true;
508-
if (config->emachine == EM_MSP430)
509-
config->osabi = ELFOSABI_STANDALONE;
506+
ctx.arg.mipsN32Abi = true;
507+
if (ctx.arg.emachine == EM_MSP430)
508+
ctx.arg.osabi = ELFOSABI_STANDALONE;
510509
}
511510

512511
void ScriptParser::readPhdrs() {
@@ -550,8 +549,8 @@ void ScriptParser::readRegionAlias() {
550549
void ScriptParser::readSearchDir() {
551550
expect("(");
552551
StringRef name = readName();
553-
if (!config->nostdlib)
554-
config->searchPaths.push_back(name);
552+
if (!ctx.arg.nostdlib)
553+
ctx.arg.searchPaths.push_back(name);
555554
expect(")");
556555
}
557556

@@ -562,15 +561,15 @@ void ScriptParser::readSearchDir() {
562561
SmallVector<SectionCommand *, 0> ScriptParser::readOverlay() {
563562
Expr addrExpr;
564563
if (consume(":")) {
565-
addrExpr = [] { return ctx.script->getDot(); };
564+
addrExpr = [&] { return ctx.script->getDot(); };
566565
} else {
567566
addrExpr = readExpr();
568567
expect(":");
569568
}
570569
// When AT is omitted, LMA should equal VMA. script->getDot() when evaluating
571570
// lmaExpr will ensure this, even if the start address is specified.
572571
Expr lmaExpr =
573-
consume("AT") ? readParenExpr() : [] { return ctx.script->getDot(); };
572+
consume("AT") ? readParenExpr() : [&] { return ctx.script->getDot(); };
574573
expect("{");
575574

576575
SmallVector<SectionCommand *, 0> v;
@@ -704,9 +703,9 @@ void ScriptParser::readTarget() {
704703
expect(")");
705704

706705
if (tok.starts_with("elf"))
707-
config->formatBinary = false;
706+
ctx.arg.formatBinary = false;
708707
else if (tok == "binary")
709-
config->formatBinary = true;
708+
ctx.arg.formatBinary = true;
710709
else
711710
setError("unknown target: " + tok);
712711
}
@@ -1327,7 +1326,7 @@ Expr ScriptParser::getPageSize() {
13271326
std::string location = getCurrentLocation();
13281327
return [=]() -> uint64_t {
13291328
if (ctx.target)
1330-
return config->commonPageSize;
1329+
return ctx.arg.commonPageSize;
13311330
error(location + ": unable to calculate page size");
13321331
return 4096; // Return a dummy value.
13331332
};
@@ -1338,7 +1337,7 @@ Expr ScriptParser::readConstant() {
13381337
if (s == "COMMONPAGESIZE")
13391338
return getPageSize();
13401339
if (s == "MAXPAGESIZE")
1341-
return [] { return config->maxPageSize; };
1340+
return [&] { return ctx.arg.maxPageSize; };
13421341
setError("unknown constant: " + s);
13431342
return [] { return 0; };
13441343
}
@@ -1556,7 +1555,7 @@ Expr ScriptParser::readPrimary() {
15561555
expect("(");
15571556
expect(".");
15581557
expect(")");
1559-
return [] { return ctx.script->getDot(); };
1558+
return [&] { return ctx.script->getDot(); };
15601559
}
15611560
if (tok == "DATA_SEGMENT_RELRO_END") {
15621561
// GNU linkers implements more complicated logic to handle
@@ -1568,8 +1567,8 @@ Expr ScriptParser::readPrimary() {
15681567
readExpr();
15691568
expect(")");
15701569
ctx.script->seenRelroEnd = true;
1571-
return [=] {
1572-
return alignToPowerOf2(ctx.script->getDot(), config->maxPageSize);
1570+
return [&] {
1571+
return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize);
15731572
};
15741573
}
15751574
if (tok == "DEFINED") {
@@ -1728,9 +1727,9 @@ void ScriptParser::readAnonymousDeclaration() {
17281727
SmallVector<SymbolVersion, 0> globals;
17291728
std::tie(locals, globals) = readSymbols();
17301729
for (const SymbolVersion &pat : locals)
1731-
config->versionDefinitions[VER_NDX_LOCAL].localPatterns.push_back(pat);
1730+
ctx.arg.versionDefinitions[VER_NDX_LOCAL].localPatterns.push_back(pat);
17321731
for (const SymbolVersion &pat : globals)
1733-
config->versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(pat);
1732+
ctx.arg.versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(pat);
17341733

17351734
expect(";");
17361735
}
@@ -1748,8 +1747,8 @@ void ScriptParser::readVersionDeclaration(StringRef verStr) {
17481747
ver.name = verStr;
17491748
ver.nonLocalPatterns = std::move(globals);
17501749
ver.localPatterns = std::move(locals);
1751-
ver.id = config->versionDefinitions.size();
1752-
config->versionDefinitions.push_back(ver);
1750+
ver.id = ctx.arg.versionDefinitions.size();
1751+
ctx.arg.versionDefinitions.push_back(ver);
17531752

17541753
// Each version may have a parent version. For example, "Ver2"
17551754
// defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
@@ -1891,21 +1890,23 @@ void ScriptParser::readMemoryAttributes(uint32_t &flags, uint32_t &invFlags,
18911890
}
18921891
}
18931892

1894-
void elf::readLinkerScript(MemoryBufferRef mb) {
1893+
void elf::readLinkerScript(Ctx &ctx, MemoryBufferRef mb) {
18951894
llvm::TimeTraceScope timeScope("Read linker script",
18961895
mb.getBufferIdentifier());
1897-
ScriptParser(mb).readLinkerScript();
1896+
ScriptParser(ctx, mb).readLinkerScript();
18981897
}
18991898

1900-
void elf::readVersionScript(MemoryBufferRef mb) {
1899+
void elf::readVersionScript(Ctx &ctx, MemoryBufferRef mb) {
19011900
llvm::TimeTraceScope timeScope("Read version script",
19021901
mb.getBufferIdentifier());
1903-
ScriptParser(mb).readVersionScript();
1902+
ScriptParser(ctx, mb).readVersionScript();
19041903
}
19051904

1906-
void elf::readDynamicList(MemoryBufferRef mb) {
1905+
void elf::readDynamicList(Ctx &ctx, MemoryBufferRef mb) {
19071906
llvm::TimeTraceScope timeScope("Read dynamic list", mb.getBufferIdentifier());
1908-
ScriptParser(mb).readDynamicList();
1907+
ScriptParser(ctx, mb).readDynamicList();
19091908
}
19101909

1911-
void elf::readDefsym(MemoryBufferRef mb) { ScriptParser(mb).readDefsym(); }
1910+
void elf::readDefsym(Ctx &ctx, MemoryBufferRef mb) {
1911+
ScriptParser(ctx, mb).readDefsym();
1912+
}

0 commit comments

Comments
 (0)