Delay computation of IsAbsolute.
We parse linker scripts very early, but whether an expression is
absolute or not can depend on a symbol defined in a .o. Given that, we
have to delay the computation of IsAbsolute. We can do that by storing
an AST when parsing or by also making IsAbsolute a function like we do
for the expression value. This patch implements the second option.
llvm-svn: 285628
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index ae52ed9..9061dac 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -64,7 +64,7 @@
}
template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
- if (Cmd->Expression.IsAbsolute)
+ if (Cmd->Expression.IsAbsolute())
addRegular<ELFT>(Cmd);
else
addSynthetic<ELFT>(Cmd);
@@ -867,6 +867,12 @@
return Symtab<ELFT>::X->find(S) != nullptr;
}
+template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) {
+ SymbolBody *Sym = Symtab<ELFT>::X->find(S);
+ auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym);
+ return DR && !DR->Section;
+}
+
// Returns indices of ELF headers containing specific section, identified
// by Name. Each index is a zero based number of ELF header listed within
// PHDRS {} script block.
@@ -1424,7 +1430,7 @@
Cmd = readProvideHidden(true, true);
}
if (Cmd && MakeAbsolute)
- Cmd->Expression.IsAbsolute = true;
+ Cmd->Expression.IsAbsolute = []() { return true; };
return Cmd;
}
@@ -1434,6 +1440,12 @@
return ScriptBase->getSymbolValue(S);
}
+static bool isAbsolute(StringRef S) {
+ if (S == ".")
+ return false;
+ return ScriptBase->isAbsolute(S);
+}
+
SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
StringRef Op = next();
Expr E;
@@ -1442,7 +1454,7 @@
// The RHS may be something like "ABSOLUTE(.) & 0xff".
// Call readExpr1 to read the whole expression.
E = readExpr1(readParenExpr(), 0);
- E.IsAbsolute = true;
+ E.IsAbsolute = []() { return true; };
} else {
E = readExpr();
}
@@ -1469,7 +1481,8 @@
};
}
if (Op == "+")
- return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
+ return {[=](uint64_t Dot) { return L(Dot) + R(Dot); },
+ [=]() { return L.IsAbsolute() && R.IsAbsolute(); }};
if (Op == "-")
return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
if (Op == "<<")
@@ -1687,7 +1700,8 @@
// Tok is a symbol name.
if (Tok != "." && !isValidCIdentifier(Tok))
setError("malformed number: " + Tok);
- return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
+ return {[=](uint64_t Dot) { return getSymbolValue(Tok, Dot); },
+ [=]() { return isAbsolute(Tok); }};
}
Expr ScriptParser::readTernary(Expr Cond) {