From a52251675f001115b225f57362d37e92b7355ef9 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 28 Sep 2024 19:23:56 -0700 Subject: [PATCH] [ELF] Pass Ctx & to Target.cpp --- lld/ELF/Arch/ARM.cpp | 9 +++++---- lld/ELF/Driver.cpp | 2 +- lld/ELF/Relocations.cpp | 4 ++-- lld/ELF/Target.cpp | 4 ++-- lld/ELF/Target.h | 6 +++--- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp index 1bbd2e1f21d7c3..29f0b7c71d43c1 100644 --- a/lld/ELF/Arch/ARM.cpp +++ b/lld/ELF/Arch/ARM.cpp @@ -491,9 +491,10 @@ bool ARM::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { // Helper to produce message text when LLD detects that a CALL relocation to // a non STT_FUNC symbol that may result in incorrect interworking between ARM // or Thumb. -static void stateChangeWarning(uint8_t *loc, RelType relt, const Symbol &s) { +static void stateChangeWarning(Ctx &ctx, uint8_t *loc, RelType relt, + const Symbol &s) { assert(!s.isFunc()); - const ErrorPlace place = getErrorPlace(loc); + const ErrorPlace place = getErrorPlace(ctx, loc); std::string hint; if (!place.srcLoc.empty()) hint = "; " + place.srcLoc; @@ -630,7 +631,7 @@ void ARM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { // lld 10.0 and before always used bit0Thumb when deciding to write a BLX // even when type not STT_FUNC. if (!rel.sym->isFunc() && isBlx != bit0Thumb) - stateChangeWarning(loc, rel.type, *rel.sym); + stateChangeWarning(ctx, loc, rel.type, *rel.sym); if (rel.sym->isFunc() ? bit0Thumb : isBlx) { // The BLX encoding is 0xfa:H:imm24 where Val = imm24:H:'1' checkInt(loc, val, 26, rel); @@ -687,7 +688,7 @@ void ARM::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { // lld 10.0 and before always used bit0Thumb when deciding to write a BLX // even when type not STT_FUNC. if (!rel.sym->isFunc() && !rel.sym->isInPlt() && isBlx == useThumb) - stateChangeWarning(loc, rel.type, *rel.sym); + stateChangeWarning(ctx, loc, rel.type, *rel.sym); if ((rel.sym->isFunc() || rel.sym->isInPlt()) ? !useThumb : isBlx) { // We are writing a BLX. Ensure BLX destination is 4-byte aligned. As // the BLX instruction may only be two byte aligned. This must be done diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 8f34b156c9c4e8..64f4489987b007 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -3122,7 +3122,7 @@ template void LinkerDriver::link(opt::InputArgList &args) { // The Target instance handles target-specific stuff, such as applying // relocations or writing a PLT section. It also contains target-dependent // values such as a default image base address. - ctx.target = getTarget(); + ctx.target = getTarget(ctx); ctx.arg.eflags = ctx.target->calcEFlags(); // maxPageSize (sometimes called abi page size) is the maximum page size that diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 58344b75accdce..5ce2df22c22859 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -99,7 +99,7 @@ static std::string getLocation(Ctx &ctx, InputSectionBase &s, const Symbol &sym, void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, int64_t min, uint64_t max) { - ErrorPlace errPlace = getErrorPlace(loc); + ErrorPlace errPlace = getErrorPlace(ctx, loc); std::string hint; if (rel.sym) { if (!rel.sym->isSection()) @@ -130,7 +130,7 @@ void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, void elf::reportRangeError(Ctx &ctx, uint8_t *loc, int64_t v, int n, const Symbol &sym, const Twine &msg) { - ErrorPlace errPlace = getErrorPlace(loc); + ErrorPlace errPlace = getErrorPlace(ctx, loc); std::string hint; if (!sym.getName().empty()) hint = "; references '" + lld::toString(sym) + '\'' + diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp index d895757ad4e49f..ea8dc98e9a2e73 100644 --- a/lld/ELF/Target.cpp +++ b/lld/ELF/Target.cpp @@ -45,7 +45,7 @@ std::string lld::toString(RelType type) { return std::string(s); } -TargetInfo *elf::getTarget() { +TargetInfo *elf::getTarget(Ctx &ctx) { switch (ctx.arg.emachine) { case EM_386: case EM_IAMCU: @@ -94,7 +94,7 @@ TargetInfo *elf::getTarget() { } } -ErrorPlace elf::getErrorPlace(const uint8_t *loc) { +ErrorPlace elf::getErrorPlace(Ctx &ctx, const uint8_t *loc) { assert(loc != nullptr); for (InputSectionBase *d : ctx.inputSections) { auto *isec = dyn_cast(d); diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h index 1c3c293be2f329..951b2f36fdda93 100644 --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -200,10 +200,10 @@ struct ErrorPlace { }; // Returns input section and corresponding source string for the given location. -ErrorPlace getErrorPlace(const uint8_t *loc); +ErrorPlace getErrorPlace(Ctx &ctx, const uint8_t *loc); static inline std::string getErrorLocation(const uint8_t *loc) { - return getErrorPlace(loc).loc; + return getErrorPlace(ctx, loc).loc; } void processArmCmseSymbols(); @@ -241,7 +241,7 @@ void convertArmInstructionstoBE8(InputSection *sec, uint8_t *buf); void createTaggedSymbols(const SmallVector &files); void initSymbolAnchors(); -TargetInfo *getTarget(); +TargetInfo *getTarget(Ctx &ctx); template bool isMipsPIC(const Defined *sym);