Skip to content

Commit 7556f6b

Browse files
committedNov 2, 2016
Reduce number of classes by merging DIHelper with ObjectFile.
DIHelper is a class having only one member, and ObjectFile has a unique pointer to a DIHelper. So we can directly have ObjectFile have the member. Differential Revision: https://reviews.llvm.org/D26223 llvm-svn: 285850
1 parent 757d317 commit 7556f6b

File tree

3 files changed

+31
-57
lines changed

3 files changed

+31
-57
lines changed
 

‎lld/ELF/InputFiles.cpp

+21-35
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ using namespace llvm::sys::fs;
3535
using namespace lld;
3636
using namespace lld::elf;
3737

38-
3938
namespace {
4039
// In ELF object file all section addresses are zero. If we have multiple
4140
// .text sections (when using -ffunction-section or comdat group) then
@@ -53,47 +52,46 @@ class ObjectInfo : public LoadedObjectInfo {
5352
};
5453
}
5554

56-
template <class ELFT> DIHelper<ELFT>::DIHelper(InputFile *F) {
57-
Expected<std::unique_ptr<object::ObjectFile>> Obj =
58-
object::ObjectFile::createObjectFile(F->MB);
59-
if (!Obj)
60-
return;
55+
template <class ELFT> void ObjectFile<ELFT>::initializeDwarfLine() {
56+
std::unique_ptr<object::ObjectFile> Obj =
57+
check(object::ObjectFile::createObjectFile(this->MB),
58+
"createObjectFile failed");
6159

6260
ObjectInfo ObjInfo;
6361
DWARFContextInMemory Dwarf(*Obj.get(), &ObjInfo);
6462
DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
6563
DataExtractor LineData(Dwarf.getLineSection().Data,
6664
ELFT::TargetEndianness == support::little,
6765
ELFT::Is64Bits ? 8 : 4);
66+
6867
// The second parameter is offset in .debug_line section
6968
// for compilation unit (CU) of interest. We have only one
7069
// CU (object file), so offset is always 0.
7170
DwarfLine->getOrParseLineTable(LineData, 0);
7271
}
7372

74-
template <class ELFT> DIHelper<ELFT>::~DIHelper() {}
75-
73+
// Returns source line information for a given offset
74+
// using DWARF debug info.
7675
template <class ELFT>
77-
std::string DIHelper<ELFT>::getLineInfo(InputSectionBase<ELFT> *S,
78-
uintX_t Offset) {
76+
std::string ObjectFile<ELFT>::getLineInfo(InputSectionBase<ELFT> *S,
77+
uintX_t Offset) {
7978
if (!DwarfLine)
80-
return "";
79+
initializeDwarfLine();
8180

82-
DILineInfo LineInfo;
83-
DILineInfoSpecifier Spec;
84-
// The offset to CU is 0 (see DIHelper constructor).
85-
const DWARFDebugLine::LineTable *LineTbl = DwarfLine->getLineTable(0);
86-
if (!LineTbl)
81+
// The offset to CU is 0.
82+
const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
83+
if (!Tbl)
8784
return "";
8885

8986
// Use fake address calcuated by adding section file offset and offset in
90-
// section.
91-
// See comments for ObjectInfo class
92-
LineTbl->getFileLineInfoForAddress(S->Offset + Offset, nullptr, Spec.FLIKind,
93-
LineInfo);
94-
return LineInfo.Line != 0
95-
? LineInfo.FileName + " (" + std::to_string(LineInfo.Line) + ")"
96-
: "";
87+
// section. See comments for ObjectInfo class.
88+
DILineInfo Info;
89+
DILineInfoSpecifier Spec;
90+
Tbl->getFileLineInfoForAddress(S->Offset + Offset, nullptr, Spec.FLIKind,
91+
Info);
92+
if (Info.Line == 0)
93+
return "";
94+
return Info.FileName + " (" + std::to_string(Info.Line) + ")";
9795
}
9896

9997
// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
@@ -185,13 +183,6 @@ ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
185183
return makeArrayRef(this->SymbolBodies).slice(1);
186184
}
187185

188-
template <class ELFT> DIHelper<ELFT> *elf::ObjectFile<ELFT>::getDIHelper() {
189-
if (!DIH)
190-
DIH.reset(new DIHelper<ELFT>(this));
191-
192-
return DIH.get();
193-
}
194-
195186
template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
196187
if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo)
197188
return MipsOptions->Reginfo->ri_gp_value;
@@ -968,8 +959,3 @@ template void BinaryFile::parse<ELF32LE>();
968959
template void BinaryFile::parse<ELF32BE>();
969960
template void BinaryFile::parse<ELF64LE>();
970961
template void BinaryFile::parse<ELF64BE>();
971-
972-
template class elf::DIHelper<ELF32LE>;
973-
template class elf::DIHelper<ELF32BE>;
974-
template class elf::DIHelper<ELF64LE>;
975-
template class elf::DIHelper<ELF64BE>;

‎lld/ELF/InputFiles.h

+9-21
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,6 @@ class InputFile;
4343
class Lazy;
4444
class SymbolBody;
4545

46-
// Debugging information helper class. The main purpose is to
47-
// retrieve source file and line for error reporting. Linker may
48-
// find reasonable number of errors in a single object file, so
49-
// we cache debugging information in order to parse it only once
50-
// for each object file we link.
51-
template <class ELFT> class DIHelper {
52-
typedef typename ELFT::uint uintX_t;
53-
54-
public:
55-
DIHelper(InputFile *F);
56-
~DIHelper();
57-
std::string getLineInfo(InputSectionBase<ELFT> *S, uintX_t Offset);
58-
59-
private:
60-
std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
61-
};
62-
6346
// The root class of input files.
6447
class InputFile {
6548
public:
@@ -175,9 +158,9 @@ template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
175158

176159
const Elf_Shdr *getSymbolTable() const { return this->Symtab; };
177160

178-
// DI helper allows manipilating debugging information for this
179-
// object file. Used for error reporting.
180-
DIHelper<ELFT> *getDIHelper();
161+
// Returns source line information for a given offset.
162+
// If no information is available, returns "".
163+
std::string getLineInfo(InputSectionBase<ELFT> *S, uintX_t Offset);
181164

182165
// Get MIPS GP0 value defined by this file. This value represents the gp value
183166
// used to create the relocatable object and required to support
@@ -198,6 +181,7 @@ template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
198181
initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
199182
void initializeSymbols();
200183
void initializeReverseDependencies();
184+
void initializeDwarfLine();
201185
InputSectionBase<ELFT> *getRelocTarget(const Elf_Shdr &Sec);
202186
InputSectionBase<ELFT> *createInputSection(const Elf_Shdr &Sec,
203187
StringRef SectionStringTable);
@@ -218,7 +202,11 @@ template <class ELFT> class ObjectFile : public ELFFileBase<ELFT> {
218202
// MIPS .MIPS.abiflags section defined by this file.
219203
std::unique_ptr<MipsAbiFlagsInputSection<ELFT>> MipsAbiFlags;
220204

221-
std::unique_ptr<DIHelper<ELFT>> DIH;
205+
// Debugging information to retrieve source file and line for error
206+
// reporting. Linker may find reasonable number of errors in a
207+
// single object file, so we cache debugging information in order to
208+
// parse it only once for each object file we link.
209+
std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
222210
};
223211

224212
// LazyObjectFile is analogous to ArchiveFile in the sense that

‎lld/ELF/Relocations.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ static std::string getLocation(SymbolBody &Sym, InputSectionBase<ELFT> &S,
544544
ObjectFile<ELFT> *File = S.getFile();
545545

546546
// First check if we can get desired values from debugging information.
547-
std::string LineInfo = File->getDIHelper()->getLineInfo(&S, Offset);
547+
std::string LineInfo = File->getLineInfo(&S, Offset);
548548
if (!LineInfo.empty())
549549
return LineInfo;
550550

0 commit comments

Comments
 (0)
Please sign in to comment.