WinCodeViewLineTables.cpp [plain text]
#include "WinCodeViewLineTables.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/COFF.h"
namespace llvm {
StringRef WinCodeViewLineTables::getFullFilepath(const MDNode *S) {
assert(S);
DIDescriptor D(S);
assert((D.isCompileUnit() || D.isFile() || D.isSubprogram() ||
D.isLexicalBlockFile() || D.isLexicalBlock()) &&
"Unexpected scope info");
DIScope Scope(S);
StringRef Dir = Scope.getDirectory(),
Filename = Scope.getFilename();
char *&Result = DirAndFilenameToFilepathMap[std::make_pair(Dir, Filename)];
if (Result)
return Result;
std::string Filepath;
if (Filename.find(':') == 1)
Filepath = Filename;
else
Filepath = (Dir + Twine("\\") + Filename).str();
std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
size_t Cursor = 0;
while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
Filepath.erase(Cursor, 2);
Cursor = 0;
while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
if (Cursor == 0)
break;
size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
if (PrevSlash == std::string::npos)
break;
Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
Cursor = PrevSlash;
}
Cursor = 0;
while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
Filepath.erase(Cursor, 1);
Result = strdup(Filepath.c_str());
return StringRef(Result);
}
void WinCodeViewLineTables::maybeRecordLocation(DebugLoc DL,
const MachineFunction *MF) {
const MDNode *Scope = DL.getScope(MF->getFunction()->getContext());
if (!Scope)
return;
StringRef Filename = getFullFilepath(Scope);
assert(CurFn);
if (!CurFn->Instrs.empty()) {
const InstrInfoTy &LastInstr = InstrInfo[CurFn->Instrs.back()];
if (LastInstr.Filename == Filename && LastInstr.LineNumber == DL.getLine())
return;
}
FileNameRegistry.add(Filename);
MCSymbol *MCL = Asm->MMI->getContext().CreateTempSymbol();
Asm->OutStreamer.EmitLabel(MCL);
CurFn->Instrs.push_back(MCL);
InstrInfo[MCL] = InstrInfoTy(Filename, DL.getLine());
}
WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *AP)
: Asm(nullptr), CurFn(nullptr) {
MachineModuleInfo *MMI = AP->MMI;
if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
!AP->getObjFileLowering().getCOFFDebugSymbolsSection())
return;
MMI->setDebugInfoAvailability(true);
Asm = AP;
}
static void EmitLabelDiff(MCStreamer &Streamer,
const MCSymbol *From, const MCSymbol *To) {
MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
MCContext &Context = Streamer.getContext();
const MCExpr *FromRef = MCSymbolRefExpr::Create(From, Variant, Context),
*ToRef = MCSymbolRefExpr::Create(To, Variant, Context);
const MCExpr *AddrDelta =
MCBinaryExpr::Create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
Streamer.EmitValue(AddrDelta, 4);
}
void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) {
const MCSymbol *Fn = Asm->getSymbol(GV);
assert(Fn);
const FunctionInfo &FI = FnDebugInfo[GV];
if (FI.Instrs.empty())
return;
assert(FI.End && "Don't know where the function ends?");
DenseMap<size_t, size_t> FilenameSegmentLengths;
size_t LastSegmentEnd = 0;
StringRef PrevFilename = InstrInfo[FI.Instrs[0]].Filename;
for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) {
if (PrevFilename == InstrInfo[FI.Instrs[J]].Filename)
continue;
FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd;
LastSegmentEnd = J;
PrevFilename = InstrInfo[FI.Instrs[J]].Filename;
}
FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd;
Asm->OutStreamer.AddComment(
"Linetable subsection for " + Twine(Fn->getName()));
Asm->EmitInt32(COFF::DEBUG_LINE_TABLE_SUBSECTION);
MCSymbol *SubsectionBegin = Asm->MMI->getContext().CreateTempSymbol(),
*SubsectionEnd = Asm->MMI->getContext().CreateTempSymbol();
EmitLabelDiff(Asm->OutStreamer, SubsectionBegin, SubsectionEnd);
Asm->OutStreamer.EmitLabel(SubsectionBegin);
Asm->OutStreamer.EmitCOFFSecRel32(Fn);
Asm->OutStreamer.EmitCOFFSectionIndex(Fn);
EmitLabelDiff(Asm->OutStreamer, Fn, FI.End);
MCSymbol *FileSegmentEnd = nullptr;
for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) {
MCSymbol *Instr = FI.Instrs[J];
assert(InstrInfo.count(Instr));
if (FilenameSegmentLengths.count(J)) {
if (FileSegmentEnd)
Asm->OutStreamer.EmitLabel(FileSegmentEnd);
StringRef CurFilename = InstrInfo[FI.Instrs[J]].Filename;
assert(FileNameRegistry.Infos.count(CurFilename));
size_t IndexInStringTable =
FileNameRegistry.Infos[CurFilename].FilenameID;
Asm->OutStreamer.AddComment(
"Segment for file '" + Twine(CurFilename) + "' begins");
MCSymbol *FileSegmentBegin = Asm->MMI->getContext().CreateTempSymbol();
Asm->OutStreamer.EmitLabel(FileSegmentBegin);
Asm->EmitInt32(8 * IndexInStringTable);
size_t SegmentLength = FilenameSegmentLengths[J];
Asm->EmitInt32(SegmentLength);
FileSegmentEnd = Asm->MMI->getContext().CreateTempSymbol();
EmitLabelDiff(Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd);
}
EmitLabelDiff(Asm->OutStreamer, Fn, Instr);
Asm->EmitInt32(InstrInfo[Instr].LineNumber);
}
if (FileSegmentEnd)
Asm->OutStreamer.EmitLabel(FileSegmentEnd);
Asm->OutStreamer.EmitLabel(SubsectionEnd);
}
void WinCodeViewLineTables::endModule() {
if (FnDebugInfo.empty())
return;
assert(Asm != nullptr);
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I)
emitDebugInfoForFunction(VisitedFunctions[I]);
Asm->OutStreamer.AddComment("File index to string table offset subsection");
Asm->EmitInt32(COFF::DEBUG_INDEX_SUBSECTION);
size_t NumFilenames = FileNameRegistry.Infos.size();
Asm->EmitInt32(8 * NumFilenames);
for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
StringRef Filename = FileNameRegistry.Filenames[I];
Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset);
Asm->EmitInt32(0);
}
Asm->OutStreamer.AddComment("String table");
Asm->EmitInt32(COFF::DEBUG_STRING_TABLE_SUBSECTION);
Asm->EmitInt32(FileNameRegistry.LastOffset);
Asm->EmitInt8(0);
for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
Asm->OutStreamer.EmitBytes(FileNameRegistry.Filenames[I]);
Asm->EmitInt8(0);
}
Asm->OutStreamer.EmitFill((-FileNameRegistry.LastOffset) % 4, 0);
clear();
}
void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
assert(!CurFn && "Can't process two functions at once!");
if (!Asm || !Asm->MMI->hasDebugInfo())
return;
const Function *GV = MF->getFunction();
assert(FnDebugInfo.count(GV) == false);
VisitedFunctions.push_back(GV);
CurFn = &FnDebugInfo[GV];
DebugLoc PrologEndLoc;
bool EmptyPrologue = true;
for (const auto &MBB : *MF) {
if (!PrologEndLoc.isUnknown())
break;
for (const auto &MI : MBB) {
if (MI.isDebugValue())
continue;
if (!MI.getFlag(MachineInstr::FrameSetup) &&
(!MI.getDebugLoc().isUnknown())) {
PrologEndLoc = MI.getDebugLoc();
break;
}
EmptyPrologue = false;
}
}
if (!PrologEndLoc.isUnknown() && !EmptyPrologue) {
DebugLoc FnStartDL =
PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
maybeRecordLocation(FnStartDL, MF);
}
}
void WinCodeViewLineTables::endFunction(const MachineFunction *MF) {
if (!Asm || !CurFn) return;
const Function *GV = MF->getFunction();
assert(FnDebugInfo.count(GV));
assert(CurFn == &FnDebugInfo[GV]);
if (CurFn->Instrs.empty()) {
FnDebugInfo.erase(GV);
VisitedFunctions.pop_back();
} else {
MCSymbol *FunctionEndSym = Asm->OutStreamer.getContext().CreateTempSymbol();
Asm->OutStreamer.EmitLabel(FunctionEndSym);
CurFn->End = FunctionEndSym;
}
CurFn = nullptr;
}
void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) {
if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
return;
DebugLoc DL = MI->getDebugLoc();
if (DL == PrevInstLoc || DL.isUnknown())
return;
maybeRecordLocation(DL, Asm->MF);
}
}