From da06bfb7945afd242276f409368c52da683f5e9b Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 25 Nov 2016 18:51:53 +0000 Subject: [PATCH] Move getLocation from Relocations.cpp to InputSection.cpp. The function was used only within Relocations.cpp, but now we are using it in many places, so this patch moves it to a file that fits to the functionality. llvm-svn: 287943 --- lld/ELF/EhFrame.cpp | 3 +-- lld/ELF/InputSection.cpp | 27 +++++++++++++++++++++- lld/ELF/InputSection.h | 3 +++ lld/ELF/Relocations.cpp | 58 +++++------------------------------------------- lld/ELF/Relocations.h | 3 --- lld/ELF/SymbolTable.cpp | 4 ++-- 6 files changed, 38 insertions(+), 60 deletions(-) diff --git a/lld/ELF/EhFrame.cpp b/lld/ELF/EhFrame.cpp index e59acda..2428473 100644 --- a/lld/ELF/EhFrame.cpp +++ b/lld/ELF/EhFrame.cpp @@ -44,8 +44,7 @@ public: private: template void failOn(const P *Loc, const Twine &Msg) { - fatal(getLocation(*IS, (const uint8_t *)Loc - IS->Data.data()) + ": " + - Msg); + fatal(IS->getLocation((const uint8_t *)Loc - IS->Data.data()) + ": " + Msg); } uint8_t readByte(); diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 9dc769f..3d8c236 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -198,6 +198,31 @@ InputSectionBase *InputSectionBase::getLinkOrderDep() const { return nullptr; } +// Returns a source location string. Used to construct an error message. +template +std::string InputSectionBase::getLocation(typename ELFT::uint Offset) { + // First check if we can get desired values from debugging information. + std::string LineInfo = File->getLineInfo(this, Offset); + if (!LineInfo.empty()) + return LineInfo; + + // File->SourceFile contains STT_FILE symbol that contains a + // source file name. If it's missing, we use an object file name. + std::string SrcFile = File->SourceFile; + if (SrcFile.empty()) + SrcFile = toString(File); + + // Find a function symbol that encloses a given location. + for (SymbolBody *B : File->getSymbols()) + if (auto *D = dyn_cast>(B)) + if (D->Section == this && D->Type == STT_FUNC) + if (D->Value <= Offset && Offset < D->Value + D->Size) + return SrcFile + ":(function " + toString(*D) + ")"; + + // If there's no symbol, print out the offset in the section. + return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str(); +} + template InputSection::InputSection() : InputSectionBase() {} @@ -467,7 +492,7 @@ void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef Rels) { SymbolBody &Sym = this->File->getRelocTargetSym(Rel); if (Target->getRelExpr(Type, Sym) != R_ABS) { - error(getLocation(*this, Offset) + ": has non-ABS reloc"); + error(this->getLocation(Offset) + ": has non-ABS reloc"); return; } diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index 028f967..78c217f 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -141,6 +141,9 @@ public: void uncompress(); + // Returns a source location string. Used to construct an error message. + std::string getLocation(uintX_t Offset); + void relocate(uint8_t *Buf, uint8_t *BufEnd); private: diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 79a0c04..66fe4c6 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -343,7 +343,7 @@ static bool isStaticLinkTimeConstant(RelExpr E, uint32_t Type, if (AbsVal && RelE) { if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) return true; - error(getLocation(S, RelOff) + ": relocation " + toString(Type) + + error(S.getLocation(RelOff) + ": relocation " + toString(Type) + " cannot refer to absolute symbol '" + toString(Body) + "' defined in " + toString(Body.File)); return true; @@ -443,7 +443,7 @@ static RelExpr adjustExpr(const elf::ObjectFile &File, SymbolBody &Body, // only memory. We can hack around it if we are producing an executable and // the refered symbol can be preemepted to refer to the executable. if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) { - error(getLocation(S, RelOff) + ": can't create dynamic relocation " + + error(S.getLocation(RelOff) + ": can't create dynamic relocation " + toString(Type) + " against " + (Body.getName().empty() ? "local symbol in readonly segment" : "symbol '" + toString(Body) + "'") + @@ -451,8 +451,8 @@ static RelExpr adjustExpr(const elf::ObjectFile &File, SymbolBody &Body, return Expr; } if (Body.getVisibility() != STV_DEFAULT) { - error(getLocation(S, RelOff) + ": cannot preempt symbol '" + - toString(Body) + "' defined in " + toString(Body.File)); + error(S.getLocation(RelOff) + ": cannot preempt symbol '" + toString(Body) + + "' defined in " + toString(Body.File)); return Expr; } if (Body.isObject()) { @@ -521,43 +521,6 @@ static typename ELFT::uint computeAddend(const elf::ObjectFile &File, return Addend; } -// Find symbol that encloses given offset. Used for error reporting. -template -static DefinedRegular *getSymbolAt(InputSectionBase *S, - typename ELFT::uint Offset) { - for (SymbolBody *B : S->getFile()->getSymbols()) - if (auto *D = dyn_cast>(B)) - if (D->Value <= Offset && D->Value + D->Size > Offset && D->Section == S) - return D; - - return nullptr; -} - -template -std::string getLocation(InputSectionBase &S, typename ELFT::uint Offset) { - ObjectFile *File = S.getFile(); - - // First check if we can get desired values from debugging information. - std::string LineInfo = File->getLineInfo(&S, Offset); - if (!LineInfo.empty()) - return LineInfo; - - // File->SourceFile contains STT_FILE symbol contents which is a - // filename. Compilers usually create STT_FILE symbols. If it's - // missing, we use an actual filename. - std::string SrcFile = File->SourceFile; - if (SrcFile.empty()) - SrcFile = toString(File); - - // Find a symbol at a given location. - DefinedRegular *Encl = getSymbolAt(&S, Offset); - if (Encl && Encl->Type == STT_FUNC) - return SrcFile + ":(function " + toString(*Encl) + ")"; - - // If there's no symbol, print out the offset instead of a symbol name. - return (SrcFile + ":(" + S.Name + "+0x" + utohexstr(Offset) + ")").str(); -} - template static void reportUndefined(SymbolBody &Sym, InputSectionBase &S, typename ELFT::uint Offset) { @@ -569,7 +532,7 @@ static void reportUndefined(SymbolBody &Sym, InputSectionBase &S, return; std::string Msg = - getLocation(S, Offset) + ": undefined symbol '" + toString(Sym) + "'"; + S.getLocation(Offset) + ": undefined symbol '" + toString(Sym) + "'"; if (Config->UnresolvedSymbols == UnresolvedPolicy::Warn) warn(Msg); @@ -712,7 +675,7 @@ static void scanRelocs(InputSectionBase &C, ArrayRef Rels) { // We don't know anything about the finaly symbol. Just ask the dynamic // linker to handle the relocation for us. if (!Target->isPicRel(Type)) - error(getLocation(C, Offset) + ": relocation " + toString(Type) + + error(C.getLocation(Offset) + ": relocation " + toString(Type) + " cannot be used against shared object; recompile with -fPIC."); AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend}); @@ -837,14 +800,5 @@ template void createThunks(InputSectionBase &); template void createThunks(InputSectionBase &); template void createThunks(InputSectionBase &); template void createThunks(InputSectionBase &); - -template std::string getLocation(InputSectionBase &S, - uint32_t Offset); -template std::string getLocation(InputSectionBase &S, - uint32_t Offset); -template std::string getLocation(InputSectionBase &S, - uint64_t Offset); -template std::string getLocation(InputSectionBase &S, - uint64_t Offset); } } diff --git a/lld/ELF/Relocations.h b/lld/ELF/Relocations.h index 801f455..3eb4e5c 100644 --- a/lld/ELF/Relocations.h +++ b/lld/ELF/Relocations.h @@ -87,9 +87,6 @@ template void scanRelocations(InputSectionBase &); template void createThunks(InputSectionBase &); template -std::string getLocation(InputSectionBase &S, typename ELFT::uint Offset); - -template static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) { return 0; } diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp index 31e7f77..bbeb4f6 100644 --- a/lld/ELF/SymbolTable.cpp +++ b/lld/ELF/SymbolTable.cpp @@ -359,8 +359,8 @@ static void reportDuplicate(SymbolBody *Existing, return; } - std::string OldLoc = getLocation(*D->Section, D->Value); - std::string NewLoc = getLocation(*ErrSec, ErrOffset); + std::string OldLoc = D->Section->getLocation(D->Value); + std::string NewLoc = ErrSec->getLocation(ErrOffset); print(NewLoc + ": duplicate symbol '" + toString(*Existing) + "'"); print(OldLoc + ": previous definition was here"); -- 2.7.4