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
This commit is contained in:
Rui Ueyama 2016-11-25 18:51:53 +00:00
parent 79c05871a2
commit da06bfb794
6 changed files with 38 additions and 60 deletions

View File

@ -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();

View File

@ -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;
}

View File

@ -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:

View File

@ -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);
}
}

View File

@ -86,9 +86,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;

View File

@ -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");