2016-01-15 03:25:04 +08:00
|
|
|
//===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===//
|
2014-01-30 09:39:17 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-01-15 03:25:04 +08:00
|
|
|
// This file contains support for writing Microsoft CodeView debug info.
|
2014-01-30 09:39:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
#include "CodeViewDebug.h"
|
2016-01-14 07:44:57 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/CodeView.h"
|
2016-01-29 08:49:42 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/Line.h"
|
2016-01-14 07:44:57 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
|
2014-01-30 09:39:17 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
|
2016-01-14 07:44:57 +08:00
|
|
|
using namespace llvm::codeview;
|
|
|
|
|
2014-01-30 09:39:17 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2016-01-16 08:09:09 +08:00
|
|
|
StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
|
|
|
|
std::string &Filepath = FileToFilepathMap[File];
|
2015-12-03 06:34:30 +08:00
|
|
|
if (!Filepath.empty())
|
|
|
|
return Filepath;
|
2014-01-30 09:39:17 +08:00
|
|
|
|
2016-01-16 08:09:09 +08:00
|
|
|
StringRef Dir = File->getDirectory(), Filename = File->getFilename();
|
|
|
|
|
2014-01-30 09:39:17 +08:00
|
|
|
// Clang emits directory and relative filename info into the IR, but CodeView
|
|
|
|
// operates on full paths. We could change Clang to emit full paths too, but
|
|
|
|
// that would increase the IR size and probably not needed for other users.
|
|
|
|
// For now, just concatenate and canonicalize the path here.
|
|
|
|
if (Filename.find(':') == 1)
|
|
|
|
Filepath = Filename;
|
|
|
|
else
|
2015-03-28 01:51:30 +08:00
|
|
|
Filepath = (Dir + "\\" + Filename).str();
|
2014-01-30 09:39:17 +08:00
|
|
|
|
|
|
|
// Canonicalize the path. We have to do it textually because we may no longer
|
|
|
|
// have access the file in the filesystem.
|
|
|
|
// First, replace all slashes with backslashes.
|
|
|
|
std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
|
|
|
|
|
|
|
|
// Remove all "\.\" with "\".
|
|
|
|
size_t Cursor = 0;
|
|
|
|
while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
|
|
|
|
Filepath.erase(Cursor, 2);
|
|
|
|
|
|
|
|
// Replace all "\XXX\..\" with "\". Don't try too hard though as the original
|
|
|
|
// path should be well-formatted, e.g. start with a drive letter, etc.
|
|
|
|
Cursor = 0;
|
|
|
|
while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
|
|
|
|
// Something's wrong if the path starts with "\..\", abort.
|
|
|
|
if (Cursor == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
|
|
|
|
if (PrevSlash == std::string::npos)
|
|
|
|
// Something's wrong, abort.
|
|
|
|
break;
|
|
|
|
|
|
|
|
Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
|
|
|
|
// The next ".." might be following the one we've just erased.
|
|
|
|
Cursor = PrevSlash;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all duplicate backslashes.
|
|
|
|
Cursor = 0;
|
|
|
|
while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
|
|
|
|
Filepath.erase(Cursor, 1);
|
|
|
|
|
2015-12-03 06:34:30 +08:00
|
|
|
return Filepath;
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
|
|
|
|
unsigned NextId = FileIdMap.size() + 1;
|
|
|
|
auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
|
|
|
|
if (Insertion.second) {
|
|
|
|
// We have to compute the full filepath and emit a .cv_file directive.
|
|
|
|
StringRef FullPath = getFullFilepath(F);
|
|
|
|
NextId = Asm->OutStreamer->EmitCVFileDirective(NextId, FullPath);
|
|
|
|
assert(NextId == FileIdMap.size() && ".cv_file directive failed");
|
|
|
|
}
|
|
|
|
return Insertion.first->second;
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
|
2016-01-16 08:09:09 +08:00
|
|
|
const MachineFunction *MF) {
|
|
|
|
// Skip this instruction if it has the same location as the previous one.
|
|
|
|
if (DL == CurFn->LastLoc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const DIScope *Scope = DL.get()->getScope();
|
2014-01-30 09:39:17 +08:00
|
|
|
if (!Scope)
|
|
|
|
return;
|
2016-01-16 08:09:09 +08:00
|
|
|
|
2016-01-13 09:05:23 +08:00
|
|
|
// Skip this line if it is longer than the maximum we can record.
|
2016-01-29 08:49:42 +08:00
|
|
|
LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
|
|
|
|
if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
|
|
|
|
LI.isNeverStepInto())
|
2016-01-13 09:05:23 +08:00
|
|
|
return;
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
|
|
|
|
if (CI.getStartColumn() != DL.getCol())
|
|
|
|
return;
|
2016-01-29 08:13:28 +08:00
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
if (!CurFn->HaveLineInfo)
|
|
|
|
CurFn->HaveLineInfo = true;
|
|
|
|
unsigned FileId = 0;
|
|
|
|
if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
|
|
|
|
FileId = CurFn->LastFileId;
|
|
|
|
else
|
|
|
|
FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
|
|
|
|
CurFn->LastLoc = DL;
|
|
|
|
Asm->OutStreamer->EmitCVLocDirective(CurFn->FuncId, FileId, DL.getLine(),
|
|
|
|
DL.getCol(), /*PrologueEnd=*/false,
|
|
|
|
/*IsStmt=*/false, DL->getFilename());
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
|
2014-04-24 14:44:33 +08:00
|
|
|
: Asm(nullptr), CurFn(nullptr) {
|
2014-01-30 09:39:17 +08:00
|
|
|
MachineModuleInfo *MMI = AP->MMI;
|
|
|
|
|
|
|
|
// If module doesn't have named metadata anchors or COFF debug section
|
|
|
|
// is not available, skip any debug info related stuff.
|
|
|
|
if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
|
|
|
|
!AP->getObjFileLowering().getCOFFDebugSymbolsSection())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Tell MMI that we have debug info.
|
|
|
|
MMI->setDebugInfoAvailability(true);
|
|
|
|
Asm = AP;
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::endModule() {
|
2014-10-11 00:05:32 +08:00
|
|
|
if (FnDebugInfo.empty())
|
|
|
|
return;
|
|
|
|
|
2016-01-14 07:44:57 +08:00
|
|
|
// FIXME: For functions that are comdat, we should emit separate .debug$S
|
|
|
|
// sections that are comdat associative with the main function instead of
|
|
|
|
// having one big .debug$S section.
|
2014-10-11 00:05:32 +08:00
|
|
|
assert(Asm != nullptr);
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->SwitchSection(
|
2014-10-11 00:05:32 +08:00
|
|
|
Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
|
|
|
|
Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
|
|
|
|
|
|
|
|
// The COFF .debug$S section consists of several subsections, each starting
|
|
|
|
// with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
|
|
|
|
// of the payload followed by the payload itself. The subsections are 4-byte
|
|
|
|
// aligned.
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
// Emit per-function debug information.
|
|
|
|
for (auto &P : FnDebugInfo)
|
|
|
|
emitDebugInfoForFunction(P.first, P.second);
|
2014-10-11 00:05:32 +08:00
|
|
|
|
|
|
|
// This subsection holds a file index to offset in string table table.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->AddComment("File index to string table offset subsection");
|
2016-01-29 08:49:42 +08:00
|
|
|
Asm->OutStreamer->EmitCVFileChecksumsDirective();
|
2014-10-11 00:05:32 +08:00
|
|
|
|
|
|
|
// This subsection holds the string table.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->AddComment("String table");
|
2016-01-29 08:49:42 +08:00
|
|
|
Asm->OutStreamer->EmitCVStringTableDirective();
|
2014-10-11 00:05:32 +08:00
|
|
|
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2014-01-30 09:39:17 +08:00
|
|
|
static void EmitLabelDiff(MCStreamer &Streamer,
|
2014-10-24 09:27:45 +08:00
|
|
|
const MCSymbol *From, const MCSymbol *To,
|
|
|
|
unsigned int Size = 4) {
|
2014-01-30 09:39:17 +08:00
|
|
|
MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
|
|
|
|
MCContext &Context = Streamer.getContext();
|
2015-05-30 09:25:56 +08:00
|
|
|
const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context),
|
|
|
|
*ToRef = MCSymbolRefExpr::create(To, Variant, Context);
|
2014-01-30 09:39:17 +08:00
|
|
|
const MCExpr *AddrDelta =
|
2015-05-30 09:25:56 +08:00
|
|
|
MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
|
2014-10-24 09:27:45 +08:00
|
|
|
Streamer.EmitValue(AddrDelta, Size);
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
|
|
|
|
FunctionInfo &FI) {
|
2014-01-30 09:39:17 +08:00
|
|
|
// For each function there is a separate subsection
|
|
|
|
// which holds the PC to file:line table.
|
|
|
|
const MCSymbol *Fn = Asm->getSymbol(GV);
|
|
|
|
assert(Fn);
|
2014-03-26 17:50:36 +08:00
|
|
|
|
2015-03-21 03:50:00 +08:00
|
|
|
StringRef FuncName;
|
2015-04-21 06:10:08 +08:00
|
|
|
if (auto *SP = getDISubprogram(GV))
|
2015-04-14 11:40:37 +08:00
|
|
|
FuncName = SP->getDisplayName();
|
2015-03-21 03:50:00 +08:00
|
|
|
|
2016-01-14 08:12:54 +08:00
|
|
|
// If our DISubprogram name is empty, use the mangled name.
|
2016-01-14 03:32:35 +08:00
|
|
|
if (FuncName.empty())
|
|
|
|
FuncName = GlobalValue::getRealLinkageName(GV->getName());
|
2016-01-14 08:12:54 +08:00
|
|
|
|
2014-10-24 09:27:45 +08:00
|
|
|
// Emit a symbol subsection, required by VS2012+ to find function boundaries.
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(),
|
|
|
|
*SymbolsEnd = Asm->MMI->getContext().createTempSymbol();
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName));
|
2016-01-14 07:44:57 +08:00
|
|
|
Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols));
|
2015-04-25 03:11:51 +08:00
|
|
|
EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd);
|
|
|
|
Asm->OutStreamer->EmitLabel(SymbolsBegin);
|
2014-10-24 09:27:45 +08:00
|
|
|
{
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(),
|
|
|
|
*ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol();
|
2015-04-25 03:11:51 +08:00
|
|
|
EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2);
|
|
|
|
Asm->OutStreamer->EmitLabel(ProcSegmentBegin);
|
2014-10-24 09:27:45 +08:00
|
|
|
|
2016-01-14 07:44:57 +08:00
|
|
|
Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID));
|
|
|
|
|
2014-10-24 09:27:45 +08:00
|
|
|
// Some bytes of this segment don't seem to be required for basic debugging,
|
|
|
|
// so just fill them with zeroes.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitFill(12, 0);
|
2014-10-24 09:27:45 +08:00
|
|
|
// This is the important bit that tells the debugger where the function
|
|
|
|
// code is located and what's its size:
|
2015-04-25 03:11:51 +08:00
|
|
|
EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
|
|
|
|
Asm->OutStreamer->EmitFill(12, 0);
|
|
|
|
Asm->OutStreamer->EmitCOFFSecRel32(Fn);
|
|
|
|
Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
|
2014-10-24 09:27:45 +08:00
|
|
|
Asm->EmitInt8(0);
|
2014-11-13 04:10:09 +08:00
|
|
|
// Emit the function display name as a null-terminated string.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitBytes(FuncName);
|
2014-10-24 09:27:45 +08:00
|
|
|
Asm->EmitInt8(0);
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitLabel(ProcSegmentEnd);
|
2014-10-24 09:27:45 +08:00
|
|
|
|
|
|
|
// We're done with this function.
|
|
|
|
Asm->EmitInt16(0x0002);
|
2016-01-14 07:44:57 +08:00
|
|
|
Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END));
|
2014-10-24 09:27:45 +08:00
|
|
|
}
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitLabel(SymbolsEnd);
|
2014-10-24 09:27:45 +08:00
|
|
|
// Every subsection must be aligned to a 4-byte boundary.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0);
|
2014-10-24 09:27:45 +08:00
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
// We have an assembler directive that takes care of the whole line table.
|
|
|
|
Asm->OutStreamer->EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::beginFunction(const MachineFunction *MF) {
|
2014-01-30 09:39:17 +08:00
|
|
|
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);
|
|
|
|
CurFn = &FnDebugInfo[GV];
|
2016-01-29 08:49:42 +08:00
|
|
|
CurFn->FuncId = NextFuncId++;
|
2014-01-30 09:39:17 +08:00
|
|
|
|
|
|
|
// Find the end of the function prolog.
|
|
|
|
// FIXME: is there a simpler a way to do this? Can we just search
|
|
|
|
// for the first instruction of the function, not the last of the prolog?
|
|
|
|
DebugLoc PrologEndLoc;
|
|
|
|
bool EmptyPrologue = true;
|
2014-05-01 06:17:38 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
2015-03-31 03:14:47 +08:00
|
|
|
if (PrologEndLoc)
|
2014-05-01 06:17:38 +08:00
|
|
|
break;
|
|
|
|
for (const auto &MI : MBB) {
|
|
|
|
if (MI.isDebugValue())
|
2014-01-30 09:39:17 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// First known non-DBG_VALUE and non-frame setup location marks
|
|
|
|
// the beginning of the function body.
|
|
|
|
// FIXME: do we need the first subcondition?
|
2015-03-31 03:14:47 +08:00
|
|
|
if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
|
2014-05-01 06:17:38 +08:00
|
|
|
PrologEndLoc = MI.getDebugLoc();
|
2014-01-30 09:39:17 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
EmptyPrologue = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Record beginning of function if we have a non-empty prologue.
|
2015-03-31 03:14:47 +08:00
|
|
|
if (PrologEndLoc && !EmptyPrologue) {
|
|
|
|
DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
|
2014-01-30 09:39:17 +08:00
|
|
|
maybeRecordLocation(FnStartDL, MF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::endFunction(const MachineFunction *MF) {
|
2014-01-30 09:39:17 +08:00
|
|
|
if (!Asm || !CurFn) // We haven't created any debug info for this function.
|
|
|
|
return;
|
|
|
|
|
2014-03-26 19:24:36 +08:00
|
|
|
const Function *GV = MF->getFunction();
|
2014-06-20 18:26:56 +08:00
|
|
|
assert(FnDebugInfo.count(GV));
|
2014-03-26 19:24:36 +08:00
|
|
|
assert(CurFn == &FnDebugInfo[GV]);
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
// Don't emit anything if we don't have any line tables.
|
|
|
|
if (!CurFn->HaveLineInfo) {
|
2014-03-26 19:24:36 +08:00
|
|
|
FnDebugInfo.erase(GV);
|
|
|
|
} else {
|
2015-03-05 10:05:42 +08:00
|
|
|
CurFn->End = Asm->getFunctionEnd();
|
2014-03-26 17:50:36 +08:00
|
|
|
}
|
2014-04-24 14:44:33 +08:00
|
|
|
CurFn = nullptr;
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
|
2014-01-30 09:39:17 +08:00
|
|
|
// Ignore DBG_VALUE locations and function prologue.
|
|
|
|
if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
|
|
|
|
return;
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
2015-03-31 03:14:47 +08:00
|
|
|
if (DL == PrevInstLoc || !DL)
|
2014-01-30 09:39:17 +08:00
|
|
|
return;
|
|
|
|
maybeRecordLocation(DL, Asm->MF);
|
|
|
|
}
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|