Skip to content

Commit

Permalink
[ELF] ScriptParser: pass Ctx to ScriptParser and ScriptLexer. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Sep 21, 2024
1 parent f86050d commit cf57a67
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 66 deletions.
12 changes: 6 additions & 6 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void LinkerDriver::addFile(StringRef path, bool withLOption) {

switch (identify_magic(mbref.getBuffer())) {
case file_magic::unknown:
readLinkerScript(mbref);
readLinkerScript(ctx, mbref);
return;
case file_magic::archive: {
auto members = getArchiveMembers(mbref);
Expand Down Expand Up @@ -1810,12 +1810,12 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
for (auto *arg :
args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list))
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
readDynamicList(*buffer);
readDynamicList(ctx, *buffer);

for (auto *arg : args.filtered(OPT_version_script))
if (std::optional<std::string> path = searchScript(arg->getValue())) {
if (std::optional<MemoryBufferRef> buffer = readFile(*path))
readVersionScript(*buffer);
readVersionScript(ctx, *buffer);
} else {
error(Twine("cannot find version script ") + arg->getValue());
}
Expand Down Expand Up @@ -1943,7 +1943,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
hasInput = true;
break;
case OPT_defsym: {
readDefsym(MemoryBufferRef(arg->getValue(), "--defsym"));
readDefsym(ctx, MemoryBufferRef(arg->getValue(), "--defsym"));
break;
}
case OPT_script:
Expand All @@ -1953,7 +1953,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
if (arg->getOption().matches(OPT_default_script)) {
defaultScript = mb;
} else {
readLinkerScript(*mb);
readLinkerScript(ctx, *mb);
hasScript = true;
}
}
Expand Down Expand Up @@ -2039,7 +2039,7 @@ void LinkerDriver::createFiles(opt::InputArgList &args) {
}

if (defaultScript && !hasScript)
readLinkerScript(*defaultScript);
readLinkerScript(ctx, *defaultScript);
if (files.empty() && !hasInput && errorCount() == 0)
error("no input files");
}
Expand Down
9 changes: 5 additions & 4 deletions lld/ELF/ScriptLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@ using namespace llvm;
using namespace lld;
using namespace lld::elf;

ScriptLexer::Buffer::Buffer(MemoryBufferRef mb)
ScriptLexer::Buffer::Buffer(Ctx &ctx, MemoryBufferRef mb)
: s(mb.getBuffer()), filename(mb.getBufferIdentifier()),
begin(mb.getBufferStart()) {
if (config->sysroot == "")
if (ctx.arg.sysroot == "")
return;
StringRef path = filename;
for (; !path.empty(); path = sys::path::parent_path(path)) {
if (!sys::fs::equivalent(config->sysroot, path))
if (!sys::fs::equivalent(ctx.arg.sysroot, path))
continue;
isUnderSysroot = true;
return;
}
}

ScriptLexer::ScriptLexer(MemoryBufferRef mb) : curBuf(mb), mbs(1, mb) {
ScriptLexer::ScriptLexer(Ctx &ctx, MemoryBufferRef mb)
: curBuf(ctx, mb), mbs(1, mb) {
activeFilenames.insert(mb.getBufferIdentifier());
}

Expand Down
5 changes: 3 additions & 2 deletions lld/ELF/ScriptLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <vector>

namespace lld::elf {
struct Ctx;

class ScriptLexer {
protected:
Expand All @@ -30,7 +31,7 @@ class ScriptLexer {
bool isUnderSysroot = false;

Buffer() = default;
Buffer(MemoryBufferRef mb);
Buffer(Ctx &ctx, MemoryBufferRef mb);
};
// The current buffer and parent buffers due to INCLUDE.
Buffer curBuf;
Expand All @@ -57,7 +58,7 @@ class ScriptLexer {
bool eof = false;

public:
explicit ScriptLexer(MemoryBufferRef mb);
explicit ScriptLexer(Ctx &ctx, MemoryBufferRef mb);

void setError(const Twine &msg);
void lex();
Expand Down
101 changes: 51 additions & 50 deletions lld/ELF/ScriptParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ using namespace lld::elf;
namespace {
class ScriptParser final : ScriptLexer {
public:
ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
}
ScriptParser(Ctx &ctx, MemoryBufferRef mb) : ScriptLexer(ctx, mb) {}

void readLinkerScript();
void readVersionScript();
Expand Down Expand Up @@ -197,7 +196,7 @@ void ScriptParser::readDynamicList() {
}

for (SymbolVersion v : globals)
config->dynamicList.push_back(v);
ctx.arg.dynamicList.push_back(v);
}

void ScriptParser::readVersionScript() {
Expand Down Expand Up @@ -313,11 +312,11 @@ void ScriptParser::readNoCrossRefs(bool to) {
void ScriptParser::addFile(StringRef s) {
if (curBuf.isUnderSysroot && s.starts_with("/")) {
SmallString<128> pathData;
StringRef path = (config->sysroot + s).toStringRef(pathData);
StringRef path = (ctx.arg.sysroot + s).toStringRef(pathData);
if (sys::fs::exists(path))
ctx.driver.addFile(saver().save(path), /*withLOption=*/false);
else
setError("cannot find " + s + " inside " + config->sysroot);
setError("cannot find " + s + " inside " + ctx.arg.sysroot);
return;
}

Expand All @@ -326,10 +325,10 @@ void ScriptParser::addFile(StringRef s) {
ctx.driver.addFile(s, /*withLOption=*/false);
} else if (s.starts_with("=")) {
// Case 2: relative to the sysroot.
if (config->sysroot.empty())
if (ctx.arg.sysroot.empty())
ctx.driver.addFile(s.substr(1), /*withLOption=*/false);
else
ctx.driver.addFile(saver().save(config->sysroot + "/" + s.substr(1)),
ctx.driver.addFile(saver().save(ctx.arg.sysroot + "/" + s.substr(1)),
/*withLOption=*/false);
} else if (s.starts_with("-l")) {
// Case 3: search in the list of library paths.
Expand Down Expand Up @@ -361,26 +360,26 @@ void ScriptParser::addFile(StringRef s) {

void ScriptParser::readAsNeeded() {
expect("(");
bool orig = config->asNeeded;
config->asNeeded = true;
bool orig = ctx.arg.asNeeded;
ctx.arg.asNeeded = true;
while (auto tok = till(")"))
addFile(unquote(tok));
config->asNeeded = orig;
ctx.arg.asNeeded = orig;
}

void ScriptParser::readEntry() {
// -e <symbol> takes predecence over ENTRY(<symbol>).
expect("(");
StringRef name = readName();
if (config->entry.empty())
config->entry = name;
if (ctx.arg.entry.empty())
ctx.arg.entry = name;
expect(")");
}

void ScriptParser::readExtern() {
expect("(");
while (auto tok = till(")"))
config->undefined.push_back(unquote(tok));
ctx.arg.undefined.push_back(unquote(tok));
}

void ScriptParser::readGroup() {
Expand All @@ -402,7 +401,7 @@ void ScriptParser::readInclude() {
if (std::optional<std::string> path = searchScript(name)) {
if (std::optional<MemoryBufferRef> mb = readFile(*path)) {
buffers.push_back(curBuf);
curBuf = Buffer(*mb);
curBuf = Buffer(ctx, *mb);
mbs.push_back(*mb);
}
return;
Expand All @@ -424,8 +423,8 @@ void ScriptParser::readOutput() {
// -o <file> takes predecence over OUTPUT(<file>).
expect("(");
StringRef name = readName();
if (config->outputFile.empty())
config->outputFile = name;
if (ctx.arg.outputFile.empty())
ctx.arg.outputFile = name;
expect(")");
}

Expand Down Expand Up @@ -479,34 +478,34 @@ void ScriptParser::readOutputFormat() {
if (!consume(")")) {
expect(",");
StringRef tmp = readName();
if (config->optEB)
if (ctx.arg.optEB)
s = tmp;
expect(",");
tmp = readName();
if (config->optEL)
if (ctx.arg.optEL)
s = tmp;
consume(")");
}
// If more than one OUTPUT_FORMAT is specified, only the first is checked.
if (!config->bfdname.empty())
if (!ctx.arg.bfdname.empty())
return;
config->bfdname = s;
ctx.arg.bfdname = s;

if (s == "binary") {
config->oFormatBinary = true;
ctx.arg.oFormatBinary = true;
return;
}

if (s.consume_back("-freebsd"))
config->osabi = ELFOSABI_FREEBSD;
ctx.arg.osabi = ELFOSABI_FREEBSD;

std::tie(config->ekind, config->emachine) = parseBfdName(s);
if (config->emachine == EM_NONE)
setError("unknown output format name: " + config->bfdname);
std::tie(ctx.arg.ekind, ctx.arg.emachine) = parseBfdName(s);
if (ctx.arg.emachine == EM_NONE)
setError("unknown output format name: " + ctx.arg.bfdname);
if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips")
config->mipsN32Abi = true;
if (config->emachine == EM_MSP430)
config->osabi = ELFOSABI_STANDALONE;
ctx.arg.mipsN32Abi = true;
if (ctx.arg.emachine == EM_MSP430)
ctx.arg.osabi = ELFOSABI_STANDALONE;
}

void ScriptParser::readPhdrs() {
Expand Down Expand Up @@ -550,8 +549,8 @@ void ScriptParser::readRegionAlias() {
void ScriptParser::readSearchDir() {
expect("(");
StringRef name = readName();
if (!config->nostdlib)
config->searchPaths.push_back(name);
if (!ctx.arg.nostdlib)
ctx.arg.searchPaths.push_back(name);
expect(")");
}

Expand All @@ -562,15 +561,15 @@ void ScriptParser::readSearchDir() {
SmallVector<SectionCommand *, 0> ScriptParser::readOverlay() {
Expr addrExpr;
if (consume(":")) {
addrExpr = [] { return ctx.script->getDot(); };
addrExpr = [&] { return ctx.script->getDot(); };
} else {
addrExpr = readExpr();
expect(":");
}
// When AT is omitted, LMA should equal VMA. script->getDot() when evaluating
// lmaExpr will ensure this, even if the start address is specified.
Expr lmaExpr =
consume("AT") ? readParenExpr() : [] { return ctx.script->getDot(); };
consume("AT") ? readParenExpr() : [&] { return ctx.script->getDot(); };
expect("{");

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

if (tok.starts_with("elf"))
config->formatBinary = false;
ctx.arg.formatBinary = false;
else if (tok == "binary")
config->formatBinary = true;
ctx.arg.formatBinary = true;
else
setError("unknown target: " + tok);
}
Expand Down Expand Up @@ -1327,7 +1326,7 @@ Expr ScriptParser::getPageSize() {
std::string location = getCurrentLocation();
return [=]() -> uint64_t {
if (ctx.target)
return config->commonPageSize;
return ctx.arg.commonPageSize;
error(location + ": unable to calculate page size");
return 4096; // Return a dummy value.
};
Expand All @@ -1338,7 +1337,7 @@ Expr ScriptParser::readConstant() {
if (s == "COMMONPAGESIZE")
return getPageSize();
if (s == "MAXPAGESIZE")
return [] { return config->maxPageSize; };
return [&] { return ctx.arg.maxPageSize; };
setError("unknown constant: " + s);
return [] { return 0; };
}
Expand Down Expand Up @@ -1556,7 +1555,7 @@ Expr ScriptParser::readPrimary() {
expect("(");
expect(".");
expect(")");
return [] { return ctx.script->getDot(); };
return [&] { return ctx.script->getDot(); };
}
if (tok == "DATA_SEGMENT_RELRO_END") {
// GNU linkers implements more complicated logic to handle
Expand All @@ -1568,8 +1567,8 @@ Expr ScriptParser::readPrimary() {
readExpr();
expect(")");
ctx.script->seenRelroEnd = true;
return [=] {
return alignToPowerOf2(ctx.script->getDot(), config->maxPageSize);
return [&] {
return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize);
};
}
if (tok == "DEFINED") {
Expand Down Expand Up @@ -1728,9 +1727,9 @@ void ScriptParser::readAnonymousDeclaration() {
SmallVector<SymbolVersion, 0> globals;
std::tie(locals, globals) = readSymbols();
for (const SymbolVersion &pat : locals)
config->versionDefinitions[VER_NDX_LOCAL].localPatterns.push_back(pat);
ctx.arg.versionDefinitions[VER_NDX_LOCAL].localPatterns.push_back(pat);
for (const SymbolVersion &pat : globals)
config->versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(pat);
ctx.arg.versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(pat);

expect(";");
}
Expand All @@ -1748,8 +1747,8 @@ void ScriptParser::readVersionDeclaration(StringRef verStr) {
ver.name = verStr;
ver.nonLocalPatterns = std::move(globals);
ver.localPatterns = std::move(locals);
ver.id = config->versionDefinitions.size();
config->versionDefinitions.push_back(ver);
ver.id = ctx.arg.versionDefinitions.size();
ctx.arg.versionDefinitions.push_back(ver);

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

void elf::readLinkerScript(MemoryBufferRef mb) {
void elf::readLinkerScript(Ctx &ctx, MemoryBufferRef mb) {
llvm::TimeTraceScope timeScope("Read linker script",
mb.getBufferIdentifier());
ScriptParser(mb).readLinkerScript();
ScriptParser(ctx, mb).readLinkerScript();
}

void elf::readVersionScript(MemoryBufferRef mb) {
void elf::readVersionScript(Ctx &ctx, MemoryBufferRef mb) {
llvm::TimeTraceScope timeScope("Read version script",
mb.getBufferIdentifier());
ScriptParser(mb).readVersionScript();
ScriptParser(ctx, mb).readVersionScript();
}

void elf::readDynamicList(MemoryBufferRef mb) {
void elf::readDynamicList(Ctx &ctx, MemoryBufferRef mb) {
llvm::TimeTraceScope timeScope("Read dynamic list", mb.getBufferIdentifier());
ScriptParser(mb).readDynamicList();
ScriptParser(ctx, mb).readDynamicList();
}

void elf::readDefsym(MemoryBufferRef mb) { ScriptParser(mb).readDefsym(); }
void elf::readDefsym(Ctx &ctx, MemoryBufferRef mb) {
ScriptParser(ctx, mb).readDefsym();
}
Loading

0 comments on commit cf57a67

Please sign in to comment.