diff options
| author | Rui Ueyama <ruiu@google.com> | 2016-11-25 18:51:53 +0000 |
|---|---|---|
| committer | Rui Ueyama <ruiu@google.com> | 2016-11-25 18:51:53 +0000 |
| commit | da06bfb7945afd242276f409368c52da683f5e9b (patch) | |
| tree | 0c06ab103d8ac30f65b2303ea7930dfc8ce277a9 | |
| parent | 79c05871a28872a01e5e75394c5c6382d5c434a5 (diff) | |
| download | bcm5719-llvm-da06bfb7945afd242276f409368c52da683f5e9b.tar.gz bcm5719-llvm-da06bfb7945afd242276f409368c52da683f5e9b.zip | |
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
| -rw-r--r-- | lld/ELF/EhFrame.cpp | 3 | ||||
| -rw-r--r-- | lld/ELF/InputSection.cpp | 27 | ||||
| -rw-r--r-- | lld/ELF/InputSection.h | 3 | ||||
| -rw-r--r-- | lld/ELF/Relocations.cpp | 58 | ||||
| -rw-r--r-- | lld/ELF/Relocations.h | 3 | ||||
| -rw-r--r-- | 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 e59acda7a82..2428473d901 100644 --- a/lld/ELF/EhFrame.cpp +++ b/lld/ELF/EhFrame.cpp @@ -44,8 +44,7 @@ public: private: template <class P> 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 9dc769ff2e8..3d8c236d493 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -198,6 +198,31 @@ InputSectionBase<ELFT> *InputSectionBase<ELFT>::getLinkOrderDep() const { return nullptr; } +// Returns a source location string. Used to construct an error message. +template <class ELFT> +std::string InputSectionBase<ELFT>::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<DefinedRegular<ELFT>>(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 <class ELFT> InputSection<ELFT>::InputSection() : InputSectionBase<ELFT>() {} @@ -467,7 +492,7 @@ void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> 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 028f9671273..78c217f6557 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 79a0c047802..66fe4c6c20a 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<ELFT> &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<ELFT> &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<ELFT> &File, return Addend; } -// Find symbol that encloses given offset. Used for error reporting. -template <class ELFT> -static DefinedRegular<ELFT> *getSymbolAt(InputSectionBase<ELFT> *S, - typename ELFT::uint Offset) { - for (SymbolBody *B : S->getFile()->getSymbols()) - if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B)) - if (D->Value <= Offset && D->Value + D->Size > Offset && D->Section == S) - return D; - - return nullptr; -} - -template <class ELFT> -std::string getLocation(InputSectionBase<ELFT> &S, typename ELFT::uint Offset) { - ObjectFile<ELFT> *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<ELFT> *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 <class ELFT> static void reportUndefined(SymbolBody &Sym, InputSectionBase<ELFT> &S, typename ELFT::uint Offset) { @@ -569,7 +532,7 @@ static void reportUndefined(SymbolBody &Sym, InputSectionBase<ELFT> &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<ELFT> &C, ArrayRef<RelTy> 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<ELF32LE>(InputSectionBase<ELF32LE> &); template void createThunks<ELF32BE>(InputSectionBase<ELF32BE> &); template void createThunks<ELF64LE>(InputSectionBase<ELF64LE> &); template void createThunks<ELF64BE>(InputSectionBase<ELF64BE> &); - -template std::string getLocation<ELF32LE>(InputSectionBase<ELF32LE> &S, - uint32_t Offset); -template std::string getLocation<ELF32BE>(InputSectionBase<ELF32BE> &S, - uint32_t Offset); -template std::string getLocation<ELF64LE>(InputSectionBase<ELF64LE> &S, - uint64_t Offset); -template std::string getLocation<ELF64BE>(InputSectionBase<ELF64BE> &S, - uint64_t Offset); } } diff --git a/lld/ELF/Relocations.h b/lld/ELF/Relocations.h index 801f4555422..3eb4e5c9d4e 100644 --- a/lld/ELF/Relocations.h +++ b/lld/ELF/Relocations.h @@ -87,9 +87,6 @@ template <class ELFT> void scanRelocations(InputSectionBase<ELFT> &); template <class ELFT> void createThunks(InputSectionBase<ELFT> &); template <class ELFT> -std::string getLocation(InputSectionBase<ELFT> &S, typename ELFT::uint Offset); - -template <class ELFT> 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 31e7f770b21..bbeb4f6bde8 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"); |

