[lld] e57331e - [ELF] Move global relocMutex/undefs into Ctx

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 16 19:22:16 PST 2024


Author: Fangrui Song
Date: 2024-11-16T19:22:11-08:00
New Revision: e57331ec6344fd0a5cd04e2f2da38d139cbf2417

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

LOG: [ELF] Move global relocMutex/undefs into Ctx

Added: 
    

Modified: 
    lld/ELF/Config.h
    lld/ELF/Relocations.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index a9b7a98e5b544a..06c54283f7ea40 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -30,6 +30,7 @@
 #include "llvm/Support/TarWriter.h"
 #include <atomic>
 #include <memory>
+#include <mutex>
 #include <optional>
 #include <vector>
 
@@ -43,6 +44,7 @@ class SharedFile;
 class InputSectionBase;
 class EhInputSection;
 class Defined;
+class Undefined;
 class Symbol;
 class SymbolTable;
 class BitcodeCompiler;
@@ -505,6 +507,16 @@ struct DuplicateSymbol {
   uint64_t value;
 };
 
+struct UndefinedDiag {
+  Undefined *sym;
+  struct Loc {
+    InputSectionBase *sec;
+    uint64_t offset;
+  };
+  SmallVector<Loc, 0> locs;
+  bool isWarning;
+};
+
 // Linker generated sections which can be used as inputs and are not specific to
 // a partition.
 struct InStruct {
@@ -619,6 +631,11 @@ struct Ctx {
   SmallVector<SymbolAux, 0> symAux;
   // Duplicate symbol candidates.
   SmallVector<DuplicateSymbol, 0> duplicates;
+  // Undefined diagnostics are collected in a vector and emitted once all of
+  // them are known, so that some postprocessing on the list of undefined
+  // symbols can happen before lld emits diagnostics.
+  std::mutex relocMutex;
+  SmallVector<UndefinedDiag, 0> undefErrs;
   // Symbols in a non-prevailing COMDAT group which should be changed to an
   // Undefined.
   SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;

diff  --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 67e951e2c6b8f8..4ca22de2a87d17 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -560,24 +560,6 @@ static std::string maybeReportDiscarded(Ctx &ctx, Undefined &sym) {
   return msg;
 }
 
-namespace {
-// Undefined diagnostics are collected in a vector and emitted once all of
-// them are known, so that some postprocessing on the list of undefined symbols
-// can happen before lld emits diagnostics.
-struct UndefinedDiag {
-  Undefined *sym;
-  struct Loc {
-    InputSectionBase *sec;
-    uint64_t offset;
-  };
-  std::vector<Loc> locs;
-  bool isWarning;
-};
-
-std::vector<UndefinedDiag> undefs;
-std::mutex relocMutex;
-}
-
 // Check whether the definition name def is a mangled function name that matches
 // the reference name ref.
 static bool canSuggestExternCForCXX(StringRef ref, StringRef def) {
@@ -802,7 +784,7 @@ void elf::reportUndefinedSymbols(Ctx &ctx) {
   // Find the first "undefined symbol" diagnostic for each diagnostic, and
   // collect all "referenced from" lines at the first diagnostic.
   DenseMap<Symbol *, UndefinedDiag *> firstRef;
-  for (UndefinedDiag &undef : undefs) {
+  for (UndefinedDiag &undef : ctx.undefErrs) {
     assert(undef.locs.size() == 1);
     if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
       canon->locs.push_back(undef.locs[0]);
@@ -812,21 +794,20 @@ void elf::reportUndefinedSymbols(Ctx &ctx) {
   }
 
   // Enable spell corrector for the first 2 diagnostics.
-  for (const auto &[i, undef] : llvm::enumerate(undefs))
+  for (auto [i, undef] : llvm::enumerate(ctx.undefErrs))
     if (!undef.locs.empty())
       reportUndefinedSymbol(ctx, undef, i < 2);
-  undefs.clear();
 }
 
 // Report an undefined symbol if necessary.
 // Returns true if the undefined symbol will produce an error message.
 static bool maybeReportUndefined(Ctx &ctx, Undefined &sym,
                                  InputSectionBase &sec, uint64_t offset) {
-  std::lock_guard<std::mutex> lock(relocMutex);
+  std::lock_guard<std::mutex> lock(ctx.relocMutex);
   // If versioned, issue an error (even if the symbol is weak) because we don't
   // know the defining filename which is required to construct a Verneed entry.
   if (sym.hasVersionSuffix) {
-    undefs.push_back({&sym, {{&sec, offset}}, false});
+    ctx.undefErrs.push_back({&sym, {{&sec, offset}}, false});
     return true;
   }
   if (sym.isWeak())
@@ -851,7 +832,7 @@ static bool maybeReportUndefined(Ctx &ctx, Undefined &sym,
   bool isWarning =
       (ctx.arg.unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||
       ctx.arg.noinhibitExec;
-  undefs.push_back({&sym, {{&sec, offset}}, isWarning});
+  ctx.undefErrs.push_back({&sym, {{&sec, offset}}, isWarning});
   return !isWarning;
 }
 
@@ -878,7 +859,7 @@ static void addRelativeReloc(Ctx &ctx, InputSectionBase &isec,
   Partition &part = isec.getPartition(ctx);
 
   if (sym.isTagged()) {
-    std::lock_guard<std::mutex> lock(relocMutex);
+    std::lock_guard<std::mutex> lock(ctx.relocMutex);
     part.relaDyn->addRelativeReloc(ctx.target->relativeRel, isec, offsetInSec,
                                    sym, addend, type, expr);
     // With MTE globals, we always want to derive the address tag by `ldg`-ing
@@ -1089,7 +1070,7 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
   // We were asked not to generate PLT entries for ifuncs. Instead, pass the
   // direct relocation on through.
   if (LLVM_UNLIKELY(isIfunc) && ctx.arg.zIfuncNoplt) {
-    std::lock_guard<std::mutex> lock(relocMutex);
+    std::lock_guard<std::mutex> lock(ctx.relocMutex);
     sym.exportDynamic = true;
     ctx.mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend,
                                           type);
@@ -1156,7 +1137,7 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
     if (rel != 0) {
       if (ctx.arg.emachine == EM_MIPS && rel == ctx.target->symbolicRel)
         rel = ctx.target->relativeRel;
-      std::lock_guard<std::mutex> lock(relocMutex);
+      std::lock_guard<std::mutex> lock(ctx.relocMutex);
       Partition &part = sec->getPartition(ctx);
       if (ctx.arg.emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64) {
         // For a preemptible symbol, we can't use a relative relocation. For an


        


More information about the llvm-commits mailing list