Skip to content

Commit c490d34

Browse files
committed
[ELF] Pass Ctx & to Relocations
1 parent 079b832 commit c490d34

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

lld/ELF/Arch/LoongArch.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
598598
// immediate fields, the relocation range is [-128G - 0x20000, +128G -
599599
// 0x20000) (of course must be 4-byte aligned).
600600
if (((int64_t)val + 0x20000) != llvm::SignExtend64(val + 0x20000, 38))
601-
reportRangeError(loc, rel, Twine(val), llvm::minIntN(38) - 0x20000,
601+
reportRangeError(ctx, loc, rel, Twine(val), llvm::minIntN(38) - 0x20000,
602602
llvm::maxIntN(38) - 0x20000);
603603
checkAlignment(loc, val, 4, rel);
604604
// Since jirl performs sign extension on the offset immediate, adds (1<<17)

lld/ELF/Relocations.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ static std::string getLocation(Ctx &ctx, InputSectionBase &s, const Symbol &sym,
9797
return msg + s.getObjMsg(off);
9898
}
9999

100-
void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
101-
int64_t min, uint64_t max) {
100+
void elf::reportRangeError(Ctx &, uint8_t *loc, const Relocation &rel,
101+
const Twine &v, int64_t min, uint64_t max) {
102102
ErrorPlace errPlace = getErrorPlace(ctx, loc);
103103
std::string hint;
104104
if (rel.sym) {
@@ -285,7 +285,7 @@ template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
285285
// them are copied by a copy relocation, all of them need to be copied.
286286
// Otherwise, they would refer to different places at runtime.
287287
template <class ELFT>
288-
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
288+
static SmallSet<SharedSymbol *, 4> getSymbolsAt(Ctx &ctx, SharedSymbol &ss) {
289289
using Elf_Sym = typename ELFT::Sym;
290290

291291
const auto &file = cast<SharedFile>(*ss.file);
@@ -372,7 +372,7 @@ static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value,
372372
// to the variable in .bss. This kind of issue is sometimes very hard to
373373
// debug. What's a solution? Instead of exporting a variable V from a DSO,
374374
// define an accessor getV().
375-
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
375+
template <class ELFT> static void addCopyRelSymbol(Ctx &ctx, SharedSymbol &ss) {
376376
// Copy relocation against zero-sized symbol doesn't make sense.
377377
uint64_t symSize = ss.getSize();
378378
if (symSize == 0 || ss.alignment == 0)
@@ -397,7 +397,7 @@ template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
397397
// Look through the DSO's dynamic symbol table for aliases and create a
398398
// dynamic symbol for each one. This causes the copy relocation to correctly
399399
// interpose any aliases.
400-
for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
400+
for (SharedSymbol *sym : getSymbolsAt<ELFT>(ctx, ss))
401401
replaceWithDefined(*sym, *sec, 0, sym->size);
402402

403403
ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->copyRel, *sec, 0, ss);
@@ -526,7 +526,7 @@ int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
526526

527527
// Custom error message if Sym is defined in a discarded section.
528528
template <class ELFT>
529-
static std::string maybeReportDiscarded(Undefined &sym) {
529+
static std::string maybeReportDiscarded(Ctx &ctx, Undefined &sym) {
530530
auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
531531
if (!file || !sym.discardedSecIdx)
532532
return "";
@@ -714,7 +714,7 @@ static const Symbol *getAlternativeSpelling(const Undefined &sym,
714714
return nullptr;
715715
}
716716

717-
static void reportUndefinedSymbol(const UndefinedDiag &undef,
717+
static void reportUndefinedSymbol(Ctx &ctx, const UndefinedDiag &undef,
718718
bool correctSpelling) {
719719
Undefined &sym = *undef.sym;
720720

@@ -734,16 +734,16 @@ static void reportUndefinedSymbol(const UndefinedDiag &undef,
734734
std::string msg;
735735
switch (ctx.arg.ekind) {
736736
case ELF32LEKind:
737-
msg = maybeReportDiscarded<ELF32LE>(sym);
737+
msg = maybeReportDiscarded<ELF32LE>(ctx, sym);
738738
break;
739739
case ELF32BEKind:
740-
msg = maybeReportDiscarded<ELF32BE>(sym);
740+
msg = maybeReportDiscarded<ELF32BE>(ctx, sym);
741741
break;
742742
case ELF64LEKind:
743-
msg = maybeReportDiscarded<ELF64LE>(sym);
743+
msg = maybeReportDiscarded<ELF64LE>(ctx, sym);
744744
break;
745745
case ELF64BEKind:
746-
msg = maybeReportDiscarded<ELF64BE>(sym);
746+
msg = maybeReportDiscarded<ELF64BE>(ctx, sym);
747747
break;
748748
default:
749749
llvm_unreachable("");
@@ -801,7 +801,7 @@ static void reportUndefinedSymbol(const UndefinedDiag &undef,
801801
error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
802802
}
803803

804-
void elf::reportUndefinedSymbols() {
804+
void elf::reportUndefinedSymbols(Ctx &ctx) {
805805
// Find the first "undefined symbol" diagnostic for each diagnostic, and
806806
// collect all "referenced from" lines at the first diagnostic.
807807
DenseMap<Symbol *, UndefinedDiag *> firstRef;
@@ -817,14 +817,14 @@ void elf::reportUndefinedSymbols() {
817817
// Enable spell corrector for the first 2 diagnostics.
818818
for (const auto &[i, undef] : llvm::enumerate(undefs))
819819
if (!undef.locs.empty())
820-
reportUndefinedSymbol(undef, i < 2);
820+
reportUndefinedSymbol(ctx, undef, i < 2);
821821
undefs.clear();
822822
}
823823

824824
// Report an undefined symbol if necessary.
825825
// Returns true if the undefined symbol will produce an error message.
826-
static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec,
827-
uint64_t offset) {
826+
static bool maybeReportUndefined(Ctx &ctx, Undefined &sym,
827+
InputSectionBase &sec, uint64_t offset) {
828828
std::lock_guard<std::mutex> lock(relocMutex);
829829
// If versioned, issue an error (even if the symbol is weak) because we don't
830830
// know the defining filename which is required to construct a Verneed entry.
@@ -947,7 +947,7 @@ void elf::addGotEntry(Ctx &ctx, Symbol &sym) {
947947
addRelativeReloc(*ctx.in.got, off, sym, 0, R_ABS, ctx.target->symbolicRel);
948948
}
949949

950-
static void addTpOffsetGotEntry(Symbol &sym) {
950+
static void addTpOffsetGotEntry(Ctx &ctx, Symbol &sym) {
951951
ctx.in.got->addEntry(sym);
952952
uint64_t off = sym.getGotOffset();
953953
if (!sym.isPreemptible && !ctx.arg.shared) {
@@ -1277,7 +1277,7 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
12771277
// pollute other `handleTlsRelocation` by MIPS `ifs` statements.
12781278
// Mips has a custom MipsGotSection that handles the writing of GOT entries
12791279
// without dynamic relocations.
1280-
static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym,
1280+
static unsigned handleMipsTlsRelocation(Ctx &ctx, RelType type, Symbol &sym,
12811281
InputSectionBase &c, uint64_t offset,
12821282
int64_t addend, RelExpr expr) {
12831283
if (expr == R_MIPS_TLSLD) {
@@ -1314,7 +1314,7 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type,
13141314
}
13151315

13161316
if (ctx.arg.emachine == EM_MIPS)
1317-
return handleMipsTlsRelocation(type, sym, *sec, offset, addend, expr);
1317+
return handleMipsTlsRelocation(ctx, type, sym, *sec, offset, addend, expr);
13181318

13191319
// LoongArch does not yet implement transition from TLSDESC to LE/IE, so
13201320
// generate TLSDESC dynamic relocation for the dynamic linker to handle.
@@ -1488,7 +1488,7 @@ void RelocationScanner::scanOne(typename Relocs<RelTy>::const_iterator &i) {
14881488
// Error if the target symbol is undefined. Symbol index 0 may be used by
14891489
// marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
14901490
if (sym.isUndefined() && symIndex != 0 &&
1491-
maybeReportUndefined(cast<Undefined>(sym), *sec, offset))
1491+
maybeReportUndefined(ctx, cast<Undefined>(sym), *sec, offset))
14921492
return;
14931493

14941494
if (ctx.arg.emachine == EM_PPC64) {
@@ -1799,7 +1799,7 @@ void elf::postScanRelocations(Ctx &ctx) {
17991799
ctx.target->pltRel, sym);
18001800
if (flags & NEEDS_COPY) {
18011801
if (sym.isObject()) {
1802-
invokeELFT(addCopyRelSymbol, cast<SharedSymbol>(sym));
1802+
invokeELFT(addCopyRelSymbol, ctx, cast<SharedSymbol>(sym));
18031803
// NEEDS_COPY is cleared for sym and its aliases so that in
18041804
// later iterations aliases won't cause redundant copies.
18051805
assert(!sym.hasFlag(NEEDS_COPY));
@@ -1863,7 +1863,7 @@ void elf::postScanRelocations(Ctx &ctx) {
18631863
}
18641864

18651865
if ((flags & NEEDS_TLSIE) && !(flags & NEEDS_TLSGD_TO_IE))
1866-
addTpOffsetGotEntry(sym);
1866+
addTpOffsetGotEntry(ctx, sym);
18671867
};
18681868

18691869
GotSection *got = ctx.in.got.get();

lld/ELF/Relocations.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct JumpInstrMod {
144144
// the diagnostics.
145145
template <class ELFT> void scanRelocations(Ctx &ctx);
146146
template <class ELFT> void checkNoCrossRefs(Ctx &ctx);
147-
void reportUndefinedSymbols();
147+
void reportUndefinedSymbols(Ctx &);
148148
void postScanRelocations(Ctx &ctx);
149149
void addGotEntry(Ctx &ctx, Symbol &sym);
150150

lld/ELF/Target.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -247,21 +247,22 @@ TargetInfo *getTarget(Ctx &);
247247

248248
template <class ELFT> bool isMipsPIC(const Defined *sym);
249249

250-
void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
251-
int64_t min, uint64_t max);
250+
void reportRangeError(Ctx &, uint8_t *loc, const Relocation &rel,
251+
const Twine &v, int64_t min, uint64_t max);
252252
void reportRangeError(Ctx &ctx, uint8_t *loc, int64_t v, int n,
253253
const Symbol &sym, const Twine &msg);
254254

255255
// Make sure that V can be represented as an N bit signed integer.
256256
inline void checkInt(uint8_t *loc, int64_t v, int n, const Relocation &rel) {
257257
if (v != llvm::SignExtend64(v, n))
258-
reportRangeError(loc, rel, Twine(v), llvm::minIntN(n), llvm::maxIntN(n));
258+
reportRangeError(ctx, loc, rel, Twine(v), llvm::minIntN(n),
259+
llvm::maxIntN(n));
259260
}
260261

261262
// Make sure that V can be represented as an N bit unsigned integer.
262263
inline void checkUInt(uint8_t *loc, uint64_t v, int n, const Relocation &rel) {
263264
if ((v >> n) != 0)
264-
reportRangeError(loc, rel, Twine(v), 0, llvm::maxUIntN(n));
265+
reportRangeError(ctx, loc, rel, Twine(v), 0, llvm::maxUIntN(n));
265266
}
266267

267268
// Make sure that V can be represented as an N bit signed or unsigned integer.
@@ -270,7 +271,7 @@ inline void checkIntUInt(uint8_t *loc, uint64_t v, int n,
270271
// For the error message we should cast V to a signed integer so that error
271272
// messages show a small negative value rather than an extremely large one
272273
if (v != (uint64_t)llvm::SignExtend64(v, n) && (v >> n) != 0)
273-
reportRangeError(loc, rel, Twine((int64_t)v), llvm::minIntN(n),
274+
reportRangeError(ctx, loc, rel, Twine((int64_t)v), llvm::minIntN(n),
274275
llvm::maxUIntN(n));
275276
}
276277

lld/ELF/Writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
18071807
// called after processSymbolAssignments() because it needs to know whether
18081808
// a linker-script-defined symbol is absolute.
18091809
scanRelocations<ELFT>(ctx);
1810-
reportUndefinedSymbols();
1810+
reportUndefinedSymbols(ctx);
18111811
postScanRelocations(ctx);
18121812

18131813
if (ctx.in.plt && ctx.in.plt->isNeeded())

0 commit comments

Comments
 (0)