Skip to content

Commit 3320400

Browse files
committed
[ELF] ScriptParser: make Ctx & a member variable. NFC
Lambda captures need adjusting.
1 parent 8ec4067 commit 3320400

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

lld/ELF/ScriptParser.cpp

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

5252
void readLinkerScript();
5353
void readVersionScript();
@@ -126,6 +126,8 @@ class ScriptParser final : ScriptLexer {
126126
std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>>
127127
readSymbols();
128128

129+
Ctx &ctx;
130+
129131
// If we are currently parsing a PROVIDE|PROVIDE_HIDDEN command,
130132
// then this member is set to the PROVIDE symbol name.
131133
std::optional<llvm::StringRef> activeProvideSym;
@@ -140,16 +142,16 @@ static StringRef unquote(StringRef s) {
140142

141143
// Some operations only support one non absolute value. Move the
142144
// absolute one to the right hand side for convenience.
143-
static void moveAbsRight(ExprValue &a, ExprValue &b) {
145+
static void moveAbsRight(LinkerScript &s, ExprValue &a, ExprValue &b) {
144146
if (a.sec == nullptr || (a.forceAbsolute && !b.isAbsolute()))
145147
std::swap(a, b);
146148
if (!b.isAbsolute())
147-
ctx.script->recordError(
148-
a.loc + ": at least one side of the expression must be absolute");
149+
s.recordError(a.loc +
150+
": at least one side of the expression must be absolute");
149151
}
150152

151-
static ExprValue add(ExprValue a, ExprValue b) {
152-
moveAbsRight(a, b);
153+
static ExprValue add(LinkerScript &s, ExprValue a, ExprValue b) {
154+
moveAbsRight(s, a, b);
153155
return {a.sec, a.forceAbsolute, a.getSectionOffset() + b.getValue(), a.loc};
154156
}
155157

@@ -160,20 +162,20 @@ static ExprValue sub(ExprValue a, ExprValue b) {
160162
return {a.sec, false, a.getSectionOffset() - b.getValue(), a.loc};
161163
}
162164

163-
static ExprValue bitAnd(ExprValue a, ExprValue b) {
164-
moveAbsRight(a, b);
165+
static ExprValue bitAnd(LinkerScript &s, ExprValue a, ExprValue b) {
166+
moveAbsRight(s, a, b);
165167
return {a.sec, a.forceAbsolute,
166168
(a.getValue() & b.getValue()) - a.getSecAddr(), a.loc};
167169
}
168170

169-
static ExprValue bitXor(ExprValue a, ExprValue b) {
170-
moveAbsRight(a, b);
171+
static ExprValue bitXor(LinkerScript &s, ExprValue a, ExprValue b) {
172+
moveAbsRight(s, a, b);
171173
return {a.sec, a.forceAbsolute,
172174
(a.getValue() ^ b.getValue()) - a.getSecAddr(), a.loc};
173175
}
174176

175-
static ExprValue bitOr(ExprValue a, ExprValue b) {
176-
moveAbsRight(a, b);
177+
static ExprValue bitOr(LinkerScript &s, ExprValue a, ExprValue b) {
178+
moveAbsRight(s, a, b);
177179
return {a.sec, a.forceAbsolute,
178180
(a.getValue() | b.getValue()) - a.getSecAddr(), a.loc};
179181
}
@@ -561,15 +563,15 @@ void ScriptParser::readSearchDir() {
561563
SmallVector<SectionCommand *, 0> ScriptParser::readOverlay() {
562564
Expr addrExpr;
563565
if (consume(":")) {
564-
addrExpr = [&] { return ctx.script->getDot(); };
566+
addrExpr = [s = ctx.script] { return s->getDot(); };
565567
} else {
566568
addrExpr = readExpr();
567569
expect(":");
568570
}
569571
// When AT is omitted, LMA should equal VMA. script->getDot() when evaluating
570572
// lmaExpr will ensure this, even if the start address is specified.
571-
Expr lmaExpr =
572-
consume("AT") ? readParenExpr() : [&] { return ctx.script->getDot(); };
573+
Expr lmaExpr = consume("AT") ? readParenExpr()
574+
: [s = ctx.script] { return s->getDot(); };
573575
expect("{");
574576

575577
SmallVector<SectionCommand *, 0> v;
@@ -891,10 +893,10 @@ Expr ScriptParser::readAssert() {
891893
StringRef msg = readName();
892894
expect(")");
893895

894-
return [=] {
896+
return [=, s = ctx.script]() -> ExprValue {
895897
if (!e().getValue())
896898
errorOrWarn(msg);
897-
return ctx.script->getDot();
899+
return s->getDot();
898900
};
899901
}
900902

@@ -1196,8 +1198,8 @@ SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
11961198
Expr e = readExpr();
11971199
if (op != "=") {
11981200
std::string loc = getCurrentLocation();
1199-
e = [=, c = op[0]]() -> ExprValue {
1200-
ExprValue lhs = ctx.script->getSymbolValue(name, loc);
1201+
e = [=, s = ctx.script, c = op[0]]() -> ExprValue {
1202+
ExprValue lhs = s->getSymbolValue(name, loc);
12011203
switch (c) {
12021204
case '*':
12031205
return lhs.getValue() * e().getValue();
@@ -1207,7 +1209,7 @@ SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
12071209
error(loc + ": division by zero");
12081210
return 0;
12091211
case '+':
1210-
return add(lhs, e());
1212+
return add(*s, lhs, e());
12111213
case '-':
12121214
return sub(lhs, e());
12131215
case '<':
@@ -1241,7 +1243,7 @@ Expr ScriptParser::readExpr() {
12411243

12421244
Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
12431245
if (op == "+")
1244-
return [=] { return add(l(), r()); };
1246+
return [=, s = ctx.script] { return add(*s, l(), r()); };
12451247
if (op == "-")
12461248
return [=] { return sub(l(), r()); };
12471249
if (op == "*")
@@ -1285,11 +1287,11 @@ Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
12851287
if (op == "&&")
12861288
return [=] { return l().getValue() && r().getValue(); };
12871289
if (op == "&")
1288-
return [=] { return bitAnd(l(), r()); };
1290+
return [=, s = ctx.script] { return bitAnd(*s, l(), r()); };
12891291
if (op == "^")
1290-
return [=] { return bitXor(l(), r()); };
1292+
return [=, s = ctx.script] { return bitXor(*s, l(), r()); };
12911293
if (op == "|")
1292-
return [=] { return bitOr(l(), r()); };
1294+
return [=, s = ctx.script] { return bitOr(*s, l(), r()); };
12931295
llvm_unreachable("invalid operator");
12941296
}
12951297

@@ -1324,7 +1326,7 @@ Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
13241326

13251327
Expr ScriptParser::getPageSize() {
13261328
std::string location = getCurrentLocation();
1327-
return [=]() -> uint64_t {
1329+
return [=, &ctx = this->ctx]() -> uint64_t {
13281330
if (ctx.target)
13291331
return ctx.arg.commonPageSize;
13301332
error(location + ": unable to calculate page size");
@@ -1337,7 +1339,7 @@ Expr ScriptParser::readConstant() {
13371339
if (s == "COMMONPAGESIZE")
13381340
return getPageSize();
13391341
if (s == "MAXPAGESIZE")
1340-
return [&] { return ctx.arg.maxPageSize; };
1342+
return [&ctx = this->ctx] { return ctx.arg.maxPageSize; };
13411343
setError("unknown constant: " + s);
13421344
return [] { return 0; };
13431345
}
@@ -1459,9 +1461,10 @@ StringRef ScriptParser::readParenName() {
14591461
return tok;
14601462
}
14611463

1462-
static void checkIfExists(const OutputSection &osec, StringRef location) {
1463-
if (osec.location.empty() && ctx.script->errorOnMissingSection)
1464-
ctx.script->recordError(location + ": undefined section " + osec.name);
1464+
static void checkIfExists(LinkerScript &script, const OutputSection &osec,
1465+
StringRef location) {
1466+
if (osec.location.empty() && script.errorOnMissingSection)
1467+
script.recordError(location + ": undefined section " + osec.name);
14651468
}
14661469

14671470
static bool isValidSymbolName(StringRef s) {
@@ -1505,8 +1508,8 @@ Expr ScriptParser::readPrimary() {
15051508
StringRef name = readParenName();
15061509
OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec;
15071510
osec->usedInExpression = true;
1508-
return [=]() -> ExprValue {
1509-
checkIfExists(*osec, location);
1511+
return [=, s = ctx.script]() -> ExprValue {
1512+
checkIfExists(*s, *osec, location);
15101513
return {osec, false, 0, location};
15111514
};
15121515
}
@@ -1515,8 +1518,9 @@ Expr ScriptParser::readPrimary() {
15151518
Expr e = readExpr();
15161519
if (consume(")")) {
15171520
e = checkAlignment(e, location);
1518-
return
1519-
[=] { return alignToPowerOf2(ctx.script->getDot(), e().getValue()); };
1521+
return [=, s = ctx.script] {
1522+
return alignToPowerOf2(s->getDot(), e().getValue());
1523+
};
15201524
}
15211525
expect(",");
15221526
Expr e2 = checkAlignment(readExpr(), location);
@@ -1530,8 +1534,8 @@ Expr ScriptParser::readPrimary() {
15301534
if (tok == "ALIGNOF") {
15311535
StringRef name = readParenName();
15321536
OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec;
1533-
return [=] {
1534-
checkIfExists(*osec, location);
1537+
return [=, s = ctx.script] {
1538+
checkIfExists(*s, *osec, location);
15351539
return osec->addralign;
15361540
};
15371541
}
@@ -1546,16 +1550,16 @@ Expr ScriptParser::readPrimary() {
15461550
readExpr();
15471551
expect(")");
15481552
ctx.script->seenDataAlign = true;
1549-
return [=] {
1553+
return [=, s = ctx.script] {
15501554
uint64_t align = std::max(uint64_t(1), e().getValue());
1551-
return (ctx.script->getDot() + align - 1) & -align;
1555+
return (s->getDot() + align - 1) & -align;
15521556
};
15531557
}
15541558
if (tok == "DATA_SEGMENT_END") {
15551559
expect("(");
15561560
expect(".");
15571561
expect(")");
1558-
return [&] { return ctx.script->getDot(); };
1562+
return [s = ctx.script] { return s->getDot(); };
15591563
}
15601564
if (tok == "DATA_SEGMENT_RELRO_END") {
15611565
// GNU linkers implements more complicated logic to handle
@@ -1567,7 +1571,7 @@ Expr ScriptParser::readPrimary() {
15671571
readExpr();
15681572
expect(")");
15691573
ctx.script->seenRelroEnd = true;
1570-
return [&] {
1574+
return [&ctx = this->ctx] {
15711575
return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize);
15721576
};
15731577
}
@@ -1576,7 +1580,7 @@ Expr ScriptParser::readPrimary() {
15761580
// Return 1 if s is defined. If the definition is only found in a linker
15771581
// script, it must happen before this DEFINED.
15781582
auto order = ctx.scriptSymOrderCounter++;
1579-
return [=] {
1583+
return [=, &ctx = this->ctx] {
15801584
Symbol *s = symtab.find(name);
15811585
return s && s->isDefined() && ctx.scriptSymOrder.lookup(s) < order ? 1
15821586
: 0;
@@ -1594,8 +1598,8 @@ Expr ScriptParser::readPrimary() {
15941598
StringRef name = readParenName();
15951599
OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec;
15961600
osec->usedInExpression = true;
1597-
return [=] {
1598-
checkIfExists(*osec, location);
1601+
return [=, s = ctx.script] {
1602+
checkIfExists(*s, *osec, location);
15991603
return osec->getLMA();
16001604
};
16011605
}
@@ -1647,7 +1651,7 @@ Expr ScriptParser::readPrimary() {
16471651

16481652
// Tok is the dot.
16491653
if (tok == ".")
1650-
return [=] { return ctx.script->getSymbolValue(tok, location); };
1654+
return [=, s = ctx.script] { return s->getSymbolValue(tok, location); };
16511655

16521656
// Tok is a literal number.
16531657
if (std::optional<uint64_t> val = parseInt(tok))
@@ -1662,7 +1666,7 @@ Expr ScriptParser::readPrimary() {
16621666
ctx.script->provideMap[*activeProvideSym].push_back(tok);
16631667
else
16641668
ctx.script->referencedSymbols.push_back(tok);
1665-
return [=] { return ctx.script->getSymbolValue(tok, location); };
1669+
return [=, s = ctx.script] { return s->getSymbolValue(tok, location); };
16661670
}
16671671

16681672
Expr ScriptParser::readTernary(Expr cond) {

0 commit comments

Comments
 (0)