[lld] 942928f - [ELF] Migrate away from global ctx

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 14 23:04:23 PST 2024


Author: Fangrui Song
Date: 2024-11-14T23:04:18-08:00
New Revision: 942928f3df16c01ea2b905f441d72cca138032e9

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

LOG: [ELF] Migrate away from global ctx

Added: 
    

Modified: 
    lld/ELF/Arch/X86_64.cpp
    lld/ELF/DriverUtils.cpp
    lld/ELF/InputFiles.cpp
    lld/ELF/InputSection.cpp
    lld/ELF/OutputSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp
index b4caf99e881a76..8c183c021cd8af 100644
--- a/lld/ELF/Arch/X86_64.cpp
+++ b/lld/ELF/Arch/X86_64.cpp
@@ -1002,7 +1002,7 @@ static void relaxGot(uint8_t *loc, const Relocation &rel, uint64_t val) {
   if (op != 0xff) {
     // We are relaxing a rip relative to an absolute, so compensate
     // for the old -4 addend.
-    assert(!ctx.arg.isPic);
+    assert(!rel.sym->file || !rel.sym->file->ctx.arg.isPic);
     relaxGotNoPic(loc, val + 4, op, modRm, rel.type == R_X86_64_REX2_GOTPCRELX);
     return;
   }

diff  --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index d41dc2e0524764..6e58545da8951d 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -52,7 +52,7 @@ ELFOptTable::ELFOptTable() : GenericOptTable(optInfo) {}
 
 // Set color diagnostics according to --color-diagnostics={auto,always,never}
 // or --no-color-diagnostics flags.
-static void handleColorDiagnostics(opt::InputArgList &args) {
+static void handleColorDiagnostics(Ctx &ctx, opt::InputArgList &args) {
   auto *arg = args.getLastArg(OPT_color_diagnostics);
   if (!arg)
     return;
@@ -121,7 +121,7 @@ opt::InputArgList ELFOptTable::parse(Ctx &ctx, ArrayRef<const char *> argv) {
   concatLTOPluginOptions(vec);
   args = this->ParseArgs(vec, missingIndex, missingCount);
 
-  handleColorDiagnostics(args);
+  handleColorDiagnostics(ctx, args);
   if (missingCount)
     ErrAlways(ctx) << Twine(args.getArgString(missingIndex))
                    << ": missing argument";

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index b28fcfd7900d85..769081fa71a449 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -71,7 +71,7 @@ const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
   return s << toString(f);
 }
 
-static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) {
+static ELFKind getELFKind(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName) {
   unsigned char size;
   unsigned char endian;
   std::tie(size, endian) = getElfArchType(mb.getBuffer());
@@ -1350,7 +1350,7 @@ static bool isNonCommonDef(Ctx &ctx, ELFKind ekind, MemoryBufferRef mb,
 
 static bool isNonCommonDef(Ctx &ctx, MemoryBufferRef mb, StringRef symName,
                            StringRef archiveName) {
-  switch (getELFKind(mb, archiveName)) {
+  switch (getELFKind(ctx, mb, archiveName)) {
   case ELF32LEKind:
     return isNonCommonDef<ELF32LE>(ctx, ELF32LEKind, mb, symName, archiveName);
   case ELF32BEKind:
@@ -1367,8 +1367,8 @@ static bool isNonCommonDef(Ctx &ctx, MemoryBufferRef mb, StringRef symName,
 unsigned SharedFile::vernauxNum;
 
 SharedFile::SharedFile(Ctx &ctx, MemoryBufferRef m, StringRef defaultSoName)
-    : ELFFileBase(ctx, SharedKind, getELFKind(m, ""), m), soName(defaultSoName),
-      isNeeded(!ctx.arg.asNeeded) {}
+    : ELFFileBase(ctx, SharedKind, getELFKind(ctx, m, ""), m),
+      soName(defaultSoName), isNeeded(!ctx.arg.asNeeded) {}
 
 // Parse the version definitions in the object file if present, and return a
 // vector whose nth element contains a pointer to the Elf_Verdef for version
@@ -1641,7 +1641,8 @@ static ELFKind getBitcodeELFKind(const Triple &t) {
   return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
 }
 
-static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) {
+static uint16_t getBitcodeMachineKind(Ctx &ctx, StringRef path,
+                                      const Triple &t) {
   switch (t.getArch()) {
   case Triple::aarch64:
   case Triple::aarch64_be:
@@ -1732,7 +1733,7 @@ BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
 
   Triple t(obj->getTargetTriple());
   ekind = getBitcodeELFKind(t);
-  emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t);
+  emachine = getBitcodeMachineKind(ctx, mb.getBufferIdentifier(), t);
   osabi = getOsAbi(t);
 }
 
@@ -1883,7 +1884,7 @@ InputFile *elf::createInternalFile(Ctx &ctx, StringRef name) {
 ELFFileBase *elf::createObjFile(Ctx &ctx, MemoryBufferRef mb,
                                 StringRef archiveName, bool lazy) {
   ELFFileBase *f;
-  switch (getELFKind(mb, archiveName)) {
+  switch (getELFKind(ctx, mb, archiveName)) {
   case ELF32LEKind:
     f = make<ObjFile<ELF32LE>>(ctx, ELF32LEKind, mb, archiveName);
     break;

diff  --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 932c327388102d..5c6a6c2a4bf182 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -64,13 +64,13 @@ InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
   // sections are smaller than 4 GiB, which is not an unreasonable
   // assumption as of 2017.
   if (sectionKind == SectionBase::Merge && content().size() > UINT32_MAX)
-    ErrAlways(ctx) << this << ": section too large";
+    ErrAlways(getCtx()) << this << ": section too large";
 
   // The ELF spec states that a value of 0 means the section has
   // no alignment constraints.
   uint32_t v = std::max<uint32_t>(addralign, 1);
   if (!isPowerOf2_64(v))
-    Fatal(ctx) << this << ": sh_addralign is not a power of 2";
+    Fatal(getCtx()) << this << ": sh_addralign is not a power of 2";
   this->addralign = v;
 
   // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no
@@ -103,7 +103,7 @@ InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
   // they are allowed by the spec. I think 4GB is a reasonable limitation.
   // We might want to relax this in the future.
   if (hdr.sh_addralign > UINT32_MAX)
-    Fatal(ctx) << &file << ": section sh_addralign is too large";
+    Fatal(getCtx()) << &file << ": section sh_addralign is too large";
 }
 
 size_t InputSectionBase::getSize() const {
@@ -629,8 +629,8 @@ static uint64_t getRISCVUndefinedRelativeWeakVA(uint64_t type, uint64_t p) {
 static uint64_t getARMStaticBase(const Symbol &sym) {
   OutputSection *os = sym.getOutputSection();
   if (!os || !os->ptLoad || !os->ptLoad->firstSec)
-    Fatal(ctx) << "SBREL relocation to " << sym.getName()
-               << " without static base";
+    Fatal(os->ctx) << "SBREL relocation to " << sym.getName()
+                   << " without static base";
   return os->ptLoad->firstSec->addr;
 }
 
@@ -1405,7 +1405,7 @@ void MergeInputSection::splitStrings(StringRef s, size_t entSize) {
   const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections;
   const char *p = s.data(), *end = s.data() + s.size();
   if (!std::all_of(end - entSize, end, [](char c) { return c == 0; }))
-    Fatal(ctx) << this << ": string is not null terminated";
+    Fatal(getCtx()) << this << ": string is not null terminated";
   if (entSize == 1) {
     // Optimize the common case.
     do {
@@ -1465,7 +1465,7 @@ void MergeInputSection::splitIntoPieces() {
 
 SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) {
   if (content().size() <= offset)
-    Fatal(ctx) << this << ": offset is outside the section";
+    Fatal(getCtx()) << this << ": offset is outside the section";
   return partition_point(
       pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1];
 }

diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 6e8a055922b83c..9eff2a42ccc285 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -308,8 +308,8 @@ static void fill(uint8_t *buf, size_t size,
 }
 
 #if LLVM_ENABLE_ZLIB
-static SmallVector<uint8_t, 0> deflateShard(ArrayRef<uint8_t> in, int level,
-                                            int flush) {
+static SmallVector<uint8_t, 0> deflateShard(Ctx &ctx, ArrayRef<uint8_t> in,
+                                            int level, int flush) {
   // 15 and 8 are default. windowBits=-15 is negative to generate raw deflate
   // data with no zlib header or trailer.
   z_stream s = {};
@@ -432,7 +432,7 @@ template <class ELFT> void OutputSection::maybeCompress(Ctx &ctx) {
     // concatenated with the next shard.
     auto shardsAdler = std::make_unique<uint32_t[]>(numShards);
     parallelFor(0, numShards, [&](size_t i) {
-      shardsOut[i] = deflateShard(shardsIn[i], level,
+      shardsOut[i] = deflateShard(ctx, shardsIn[i], level,
                                   i != numShards - 1 ? Z_SYNC_FLUSH : Z_FINISH);
       shardsAdler[i] = adler32(1, shardsIn[i].data(), shardsIn[i].size());
     });


        


More information about the llvm-commits mailing list