Skip to content

Commit e57331e

Browse files
committed
[ELF] Move global relocMutex/undefs into Ctx
1 parent 9664ce6 commit e57331e

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

lld/ELF/Config.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "llvm/Support/TarWriter.h"
3131
#include <atomic>
3232
#include <memory>
33+
#include <mutex>
3334
#include <optional>
3435
#include <vector>
3536

@@ -43,6 +44,7 @@ class SharedFile;
4344
class InputSectionBase;
4445
class EhInputSection;
4546
class Defined;
47+
class Undefined;
4648
class Symbol;
4749
class SymbolTable;
4850
class BitcodeCompiler;
@@ -505,6 +507,16 @@ struct DuplicateSymbol {
505507
uint64_t value;
506508
};
507509

510+
struct UndefinedDiag {
511+
Undefined *sym;
512+
struct Loc {
513+
InputSectionBase *sec;
514+
uint64_t offset;
515+
};
516+
SmallVector<Loc, 0> locs;
517+
bool isWarning;
518+
};
519+
508520
// Linker generated sections which can be used as inputs and are not specific to
509521
// a partition.
510522
struct InStruct {
@@ -619,6 +631,11 @@ struct Ctx {
619631
SmallVector<SymbolAux, 0> symAux;
620632
// Duplicate symbol candidates.
621633
SmallVector<DuplicateSymbol, 0> duplicates;
634+
// Undefined diagnostics are collected in a vector and emitted once all of
635+
// them are known, so that some postprocessing on the list of undefined
636+
// symbols can happen before lld emits diagnostics.
637+
std::mutex relocMutex;
638+
SmallVector<UndefinedDiag, 0> undefErrs;
622639
// Symbols in a non-prevailing COMDAT group which should be changed to an
623640
// Undefined.
624641
SmallVector<std::pair<Symbol *, unsigned>, 0> nonPrevailingSyms;

lld/ELF/Relocations.cpp

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -560,24 +560,6 @@ static std::string maybeReportDiscarded(Ctx &ctx, Undefined &sym) {
560560
return msg;
561561
}
562562

563-
namespace {
564-
// Undefined diagnostics are collected in a vector and emitted once all of
565-
// them are known, so that some postprocessing on the list of undefined symbols
566-
// can happen before lld emits diagnostics.
567-
struct UndefinedDiag {
568-
Undefined *sym;
569-
struct Loc {
570-
InputSectionBase *sec;
571-
uint64_t offset;
572-
};
573-
std::vector<Loc> locs;
574-
bool isWarning;
575-
};
576-
577-
std::vector<UndefinedDiag> undefs;
578-
std::mutex relocMutex;
579-
}
580-
581563
// Check whether the definition name def is a mangled function name that matches
582564
// the reference name ref.
583565
static bool canSuggestExternCForCXX(StringRef ref, StringRef def) {
@@ -802,7 +784,7 @@ void elf::reportUndefinedSymbols(Ctx &ctx) {
802784
// Find the first "undefined symbol" diagnostic for each diagnostic, and
803785
// collect all "referenced from" lines at the first diagnostic.
804786
DenseMap<Symbol *, UndefinedDiag *> firstRef;
805-
for (UndefinedDiag &undef : undefs) {
787+
for (UndefinedDiag &undef : ctx.undefErrs) {
806788
assert(undef.locs.size() == 1);
807789
if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
808790
canon->locs.push_back(undef.locs[0]);
@@ -812,21 +794,20 @@ void elf::reportUndefinedSymbols(Ctx &ctx) {
812794
}
813795

814796
// Enable spell corrector for the first 2 diagnostics.
815-
for (const auto &[i, undef] : llvm::enumerate(undefs))
797+
for (auto [i, undef] : llvm::enumerate(ctx.undefErrs))
816798
if (!undef.locs.empty())
817799
reportUndefinedSymbol(ctx, undef, i < 2);
818-
undefs.clear();
819800
}
820801

821802
// Report an undefined symbol if necessary.
822803
// Returns true if the undefined symbol will produce an error message.
823804
static bool maybeReportUndefined(Ctx &ctx, Undefined &sym,
824805
InputSectionBase &sec, uint64_t offset) {
825-
std::lock_guard<std::mutex> lock(relocMutex);
806+
std::lock_guard<std::mutex> lock(ctx.relocMutex);
826807
// If versioned, issue an error (even if the symbol is weak) because we don't
827808
// know the defining filename which is required to construct a Verneed entry.
828809
if (sym.hasVersionSuffix) {
829-
undefs.push_back({&sym, {{&sec, offset}}, false});
810+
ctx.undefErrs.push_back({&sym, {{&sec, offset}}, false});
830811
return true;
831812
}
832813
if (sym.isWeak())
@@ -851,7 +832,7 @@ static bool maybeReportUndefined(Ctx &ctx, Undefined &sym,
851832
bool isWarning =
852833
(ctx.arg.unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||
853834
ctx.arg.noinhibitExec;
854-
undefs.push_back({&sym, {{&sec, offset}}, isWarning});
835+
ctx.undefErrs.push_back({&sym, {{&sec, offset}}, isWarning});
855836
return !isWarning;
856837
}
857838

@@ -878,7 +859,7 @@ static void addRelativeReloc(Ctx &ctx, InputSectionBase &isec,
878859
Partition &part = isec.getPartition(ctx);
879860

880861
if (sym.isTagged()) {
881-
std::lock_guard<std::mutex> lock(relocMutex);
862+
std::lock_guard<std::mutex> lock(ctx.relocMutex);
882863
part.relaDyn->addRelativeReloc(ctx.target->relativeRel, isec, offsetInSec,
883864
sym, addend, type, expr);
884865
// 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,
10891070
// We were asked not to generate PLT entries for ifuncs. Instead, pass the
10901071
// direct relocation on through.
10911072
if (LLVM_UNLIKELY(isIfunc) && ctx.arg.zIfuncNoplt) {
1092-
std::lock_guard<std::mutex> lock(relocMutex);
1073+
std::lock_guard<std::mutex> lock(ctx.relocMutex);
10931074
sym.exportDynamic = true;
10941075
ctx.mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend,
10951076
type);
@@ -1156,7 +1137,7 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
11561137
if (rel != 0) {
11571138
if (ctx.arg.emachine == EM_MIPS && rel == ctx.target->symbolicRel)
11581139
rel = ctx.target->relativeRel;
1159-
std::lock_guard<std::mutex> lock(relocMutex);
1140+
std::lock_guard<std::mutex> lock(ctx.relocMutex);
11601141
Partition &part = sec->getPartition(ctx);
11611142
if (ctx.arg.emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64) {
11621143
// For a preemptible symbol, we can't use a relative relocation. For an

0 commit comments

Comments
 (0)