[ELF] Pass Ctx to LinkerScript. NFC

This commit is contained in:
Fangrui Song
2024-09-21 10:22:11 -07:00
parent 6b56a27349
commit bb0a6f252f
3 changed files with 20 additions and 17 deletions

View File

@@ -148,32 +148,33 @@ namespace elf {
bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
// This driver-specific context will be freed later by unsafeLldMain().
auto *ctx = new CommonLinkerContext;
auto *context = new CommonLinkerContext;
ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
ctx->e.cleanupCallback = []() {
context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
context->e.cleanupCallback = []() {
elf::ctx.reset();
elf::ctx.partitions.emplace_back();
symtab = SymbolTable();
SharedFile::vernauxNum = 0;
};
ctx->e.logName = args::getFilenameWithoutExe(args[0]);
ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use "
"--error-limit=0 to see all errors)";
context->e.logName = args::getFilenameWithoutExe(args[0]);
context->e.errorLimitExceededMsg =
"too many errors emitted, stopping now (use "
"--error-limit=0 to see all errors)";
config = ConfigWrapper();
LinkerScript script;
elf::ctx.script = &script;
elf::ctx.symAux.emplace_back();
LinkerScript script(ctx);
ctx.script = &script;
ctx.symAux.emplace_back();
elf::ctx.partitions.clear();
elf::ctx.partitions.emplace_back();
ctx.partitions.clear();
ctx.partitions.emplace_back();
config->progName = args[0];
elf::ctx.driver.linkerMain(args);
ctx.driver.linkerMain(args);
return errorCount() == 0;
}

View File

@@ -834,7 +834,7 @@ void LinkerScript::processSymbolAssignments() {
// `st` captures the local AddressState and makes it accessible deliberately.
// This is needed as there are some cases where we cannot just thread the
// current state through to a lambda function created by the script parser.
AddressState st;
AddressState st(*this);
state = &st;
st.outSec = aether;
@@ -1468,8 +1468,8 @@ void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
[](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
}
LinkerScript::AddressState::AddressState() {
for (auto &mri : ctx.script->memoryRegions) {
LinkerScript::AddressState::AddressState(const LinkerScript &script) {
for (auto &mri : script.memoryRegions) {
MemoryRegion *mr = mri.second;
mr->curPos = (mr->origin)().getValue();
}
@@ -1495,7 +1495,7 @@ LinkerScript::assignAddresses() {
}
OutputSection *changedOsec = nullptr;
AddressState st;
AddressState st(*this);
state = &st;
errorOnMissingSection = true;
st.outSec = aether;

View File

@@ -290,7 +290,7 @@ class LinkerScript final {
// that must be reinitialized for each call to the above functions, and must
// not be used outside of the scope of a call to the above functions.
struct AddressState {
AddressState();
AddressState(const LinkerScript &);
OutputSection *outSec = nullptr;
MemoryRegion *memRegion = nullptr;
MemoryRegion *lmaRegion = nullptr;
@@ -298,6 +298,7 @@ class LinkerScript final {
uint64_t tbssAddr = 0;
};
Ctx &ctx;
llvm::DenseMap<llvm::CachedHashStringRef, OutputDesc *> nameToOutputSection;
StringRef getOutputSectionName(const InputSectionBase *s) const;
@@ -335,6 +336,7 @@ class LinkerScript final {
uint64_t dot = 0;
public:
LinkerScript(Ctx &ctx) : ctx(ctx) {}
OutputDesc *createOutputSection(StringRef name, StringRef location);
OutputDesc *getOrCreateOutputSection(StringRef name);