[lld] bb0a6f2 - [ELF] Pass Ctx to LinkerScript. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 21 10:22:16 PDT 2024


Author: Fangrui Song
Date: 2024-09-21T10:22:11-07:00
New Revision: bb0a6f252fe8cb2e5e4f9c26ec2eeda5a8b514fb

URL: https://github.com/llvm/llvm-project/commit/bb0a6f252fe8cb2e5e4f9c26ec2eeda5a8b514fb
DIFF: https://github.com/llvm/llvm-project/commit/bb0a6f252fe8cb2e5e4f9c26ec2eeda5a8b514fb.diff

LOG: [ELF] Pass Ctx to LinkerScript. NFC

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/ELF/LinkerScript.cpp
    lld/ELF/LinkerScript.h

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 189e56507e16b6..2d8752cea03c69 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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;
 }

diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 3e8f375fa17031..20c63c3d10275d 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -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;

diff  --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index dee558722299f7..db6a8c8e147ac6 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -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);
 


        


More information about the llvm-commits mailing list