2016-01-29 08:49:42 +08:00
|
|
|
//===- MCCodeView.h - Machine Code CodeView support -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Holds state from .cv_file and .cv_loc directives for later emission.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCCodeView.h"
|
2016-02-03 01:41:18 +08:00
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
2016-01-29 08:49:42 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/CodeView.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/Line.h"
|
2016-01-30 03:24:12 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
|
2016-01-29 08:49:42 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
2016-02-05 09:55:49 +08:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2016-01-29 08:49:42 +08:00
|
|
|
#include "llvm/Support/COFF.h"
|
2016-06-23 07:23:08 +08:00
|
|
|
#include "llvm/Support/EndianStream.h"
|
2016-01-29 08:49:42 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::codeview;
|
|
|
|
|
|
|
|
CodeViewContext::CodeViewContext() {}
|
|
|
|
|
|
|
|
CodeViewContext::~CodeViewContext() {
|
|
|
|
// If someone inserted strings into the string table but never actually
|
|
|
|
// emitted them somewhere, clean up the fragment.
|
|
|
|
if (!InsertedStrTabFragment)
|
|
|
|
delete StrTabFragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a valid number for use with .cv_loc if we've already seen a .cv_file
|
|
|
|
/// for it.
|
|
|
|
bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const {
|
|
|
|
unsigned Idx = FileNumber - 1;
|
|
|
|
if (Idx < Filenames.size())
|
|
|
|
return !Filenames[Idx].empty();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeViewContext::addFile(unsigned FileNumber, StringRef Filename) {
|
|
|
|
assert(FileNumber > 0);
|
|
|
|
Filename = addToStringTable(Filename);
|
|
|
|
unsigned Idx = FileNumber - 1;
|
|
|
|
if (Idx >= Filenames.size())
|
|
|
|
Filenames.resize(Idx + 1);
|
|
|
|
|
|
|
|
if (Filename.empty())
|
|
|
|
Filename = "<stdin>";
|
|
|
|
|
|
|
|
if (!Filenames[Idx].empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: We should store the string table offset of the filename, rather than
|
|
|
|
// the filename itself for efficiency.
|
|
|
|
Filename = addToStringTable(Filename);
|
|
|
|
|
|
|
|
Filenames[Idx] = Filename;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
bool CodeViewContext::recordFunctionId(unsigned FuncId) {
|
|
|
|
if (FuncId >= Functions.size())
|
|
|
|
Functions.resize(FuncId + 1);
|
|
|
|
|
|
|
|
// Return false if this function info was already allocated.
|
|
|
|
if (!Functions[FuncId].isUnallocatedFunctionInfo())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Mark this as an allocated normal function, and leave the rest alone.
|
|
|
|
Functions[FuncId].ParentFuncIdPlusOne = MCCVFunctionInfo::FunctionSentinel;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeViewContext::recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc,
|
|
|
|
unsigned IAFile, unsigned IALine,
|
|
|
|
unsigned IACol) {
|
|
|
|
if (FuncId >= Functions.size())
|
|
|
|
Functions.resize(FuncId + 1);
|
|
|
|
|
|
|
|
// Return false if this function info was already allocated.
|
|
|
|
if (!Functions[FuncId].isUnallocatedFunctionInfo())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MCCVFunctionInfo::LineInfo InlinedAt;
|
|
|
|
InlinedAt.File = IAFile;
|
|
|
|
InlinedAt.Line = IALine;
|
|
|
|
InlinedAt.Col = IACol;
|
|
|
|
|
|
|
|
// Mark this as an inlined call site and record call site line info.
|
|
|
|
MCCVFunctionInfo *Info = &Functions[FuncId];
|
|
|
|
Info->ParentFuncIdPlusOne = IAFunc + 1;
|
|
|
|
Info->InlinedAt = InlinedAt;
|
|
|
|
|
|
|
|
// Walk up the call chain adding this function id to the InlinedAtMap of all
|
|
|
|
// transitive callers until we hit a real function.
|
|
|
|
while (Info->isInlinedCallSite()) {
|
|
|
|
InlinedAt = Info->InlinedAt;
|
|
|
|
Info = getCVFunctionInfo(Info->getParentFuncId());
|
|
|
|
Info->InlinedAtMap[FuncId] = InlinedAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
MCDataFragment *CodeViewContext::getStringTableFragment() {
|
|
|
|
if (!StrTabFragment) {
|
|
|
|
StrTabFragment = new MCDataFragment();
|
|
|
|
// Start a new string table out with a null byte.
|
|
|
|
StrTabFragment->getContents().push_back('\0');
|
|
|
|
}
|
|
|
|
return StrTabFragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef CodeViewContext::addToStringTable(StringRef S) {
|
|
|
|
SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents();
|
|
|
|
auto Insertion =
|
|
|
|
StringTable.insert(std::make_pair(S, unsigned(Contents.size())));
|
|
|
|
// Return the string from the table, since it is stable.
|
|
|
|
S = Insertion.first->first();
|
|
|
|
if (Insertion.second) {
|
|
|
|
// The string map key is always null terminated.
|
|
|
|
Contents.append(S.begin(), S.end() + 1);
|
|
|
|
}
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned CodeViewContext::getStringTableOffset(StringRef S) {
|
|
|
|
// A string table offset of zero is always the empty string.
|
|
|
|
if (S.empty())
|
|
|
|
return 0;
|
|
|
|
auto I = StringTable.find(S);
|
|
|
|
assert(I != StringTable.end());
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeViewContext::emitStringTable(MCObjectStreamer &OS) {
|
|
|
|
MCContext &Ctx = OS.getContext();
|
2016-01-29 10:23:13 +08:00
|
|
|
MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false),
|
|
|
|
*StringEnd = Ctx.createTempSymbol("strtab_end", false);
|
2016-01-29 08:49:42 +08:00
|
|
|
|
|
|
|
OS.EmitIntValue(unsigned(ModuleSubstreamKind::StringTable), 4);
|
|
|
|
OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4);
|
|
|
|
OS.EmitLabel(StringBegin);
|
|
|
|
|
|
|
|
// Put the string table data fragment here, if we haven't already put it
|
|
|
|
// somewhere else. If somebody wants two string tables in their .s file, one
|
|
|
|
// will just be empty.
|
|
|
|
if (!InsertedStrTabFragment) {
|
|
|
|
OS.insert(getStringTableFragment());
|
|
|
|
InsertedStrTabFragment = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS.EmitValueToAlignment(4, 0);
|
|
|
|
|
|
|
|
OS.EmitLabel(StringEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeViewContext::emitFileChecksums(MCObjectStreamer &OS) {
|
2016-06-09 01:50:29 +08:00
|
|
|
// Do nothing if there are no file checksums. Microsoft's linker rejects empty
|
|
|
|
// CodeView substreams.
|
|
|
|
if (Filenames.empty())
|
|
|
|
return;
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
MCContext &Ctx = OS.getContext();
|
2016-01-29 10:23:13 +08:00
|
|
|
MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false),
|
|
|
|
*FileEnd = Ctx.createTempSymbol("filechecksums_end", false);
|
2016-01-29 08:49:42 +08:00
|
|
|
|
|
|
|
OS.EmitIntValue(unsigned(ModuleSubstreamKind::FileChecksums), 4);
|
|
|
|
OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4);
|
|
|
|
OS.EmitLabel(FileBegin);
|
|
|
|
|
|
|
|
// Emit an array of FileChecksum entries. We index into this table using the
|
|
|
|
// user-provided file number. Each entry is currently 8 bytes, as we don't
|
|
|
|
// emit checksums.
|
|
|
|
for (StringRef Filename : Filenames) {
|
|
|
|
OS.EmitIntValue(getStringTableOffset(Filename), 4);
|
|
|
|
// Zero the next two fields and align back to 4 bytes. This indicates that
|
|
|
|
// no checksum is present.
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
OS.EmitLabel(FileEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS,
|
|
|
|
unsigned FuncId,
|
|
|
|
const MCSymbol *FuncBegin,
|
|
|
|
const MCSymbol *FuncEnd) {
|
|
|
|
MCContext &Ctx = OS.getContext();
|
2016-01-29 10:23:13 +08:00
|
|
|
MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false),
|
|
|
|
*LineEnd = Ctx.createTempSymbol("linetable_end", false);
|
2016-01-29 08:49:42 +08:00
|
|
|
|
|
|
|
OS.EmitIntValue(unsigned(ModuleSubstreamKind::Lines), 4);
|
|
|
|
OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4);
|
|
|
|
OS.EmitLabel(LineBegin);
|
2016-12-08 09:56:23 +08:00
|
|
|
OS.EmitCOFFSecRel32(FuncBegin);
|
2016-01-29 08:49:42 +08:00
|
|
|
OS.EmitCOFFSectionIndex(FuncBegin);
|
|
|
|
|
|
|
|
// Actual line info.
|
2016-01-30 03:24:12 +08:00
|
|
|
std::vector<MCCVLineEntry> Locs = getFunctionLineEntries(FuncId);
|
2016-01-29 08:49:42 +08:00
|
|
|
bool HaveColumns = any_of(Locs, [](const MCCVLineEntry &LineEntry) {
|
|
|
|
return LineEntry.getColumn() != 0;
|
|
|
|
});
|
2016-01-30 03:24:12 +08:00
|
|
|
OS.EmitIntValue(HaveColumns ? int(LineFlags::HaveColumns) : 0, 2);
|
2016-01-29 08:49:42 +08:00
|
|
|
OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4);
|
|
|
|
|
|
|
|
for (auto I = Locs.begin(), E = Locs.end(); I != E;) {
|
|
|
|
// Emit a file segment for the run of locations that share a file id.
|
|
|
|
unsigned CurFileNum = I->getFileNum();
|
|
|
|
auto FileSegEnd =
|
|
|
|
std::find_if(I, E, [CurFileNum](const MCCVLineEntry &Loc) {
|
|
|
|
return Loc.getFileNum() != CurFileNum;
|
|
|
|
});
|
|
|
|
unsigned EntryCount = FileSegEnd - I;
|
|
|
|
OS.AddComment("Segment for file '" + Twine(Filenames[CurFileNum - 1]) +
|
|
|
|
"' begins");
|
|
|
|
OS.EmitIntValue(8 * (CurFileNum - 1), 4);
|
|
|
|
OS.EmitIntValue(EntryCount, 4);
|
|
|
|
uint32_t SegmentSize = 12;
|
|
|
|
SegmentSize += 8 * EntryCount;
|
|
|
|
if (HaveColumns)
|
|
|
|
SegmentSize += 4 * EntryCount;
|
|
|
|
OS.EmitIntValue(SegmentSize, 4);
|
|
|
|
|
|
|
|
for (auto J = I; J != FileSegEnd; ++J) {
|
|
|
|
OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4);
|
|
|
|
unsigned LineData = J->getLine();
|
|
|
|
if (J->isStmt())
|
2016-01-30 03:24:12 +08:00
|
|
|
LineData |= LineInfo::StatementFlag;
|
2016-01-29 08:49:42 +08:00
|
|
|
OS.EmitIntValue(LineData, 4);
|
|
|
|
}
|
|
|
|
if (HaveColumns) {
|
|
|
|
for (auto J = I; J != FileSegEnd; ++J) {
|
|
|
|
OS.EmitIntValue(J->getColumn(), 2);
|
|
|
|
OS.EmitIntValue(0, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
I = FileSegEnd;
|
|
|
|
}
|
|
|
|
OS.EmitLabel(LineEnd);
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:24:12 +08:00
|
|
|
static bool compressAnnotation(uint32_t Data, SmallVectorImpl<char> &Buffer) {
|
|
|
|
if (isUInt<7>(Data)) {
|
|
|
|
Buffer.push_back(Data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUInt<14>(Data)) {
|
|
|
|
Buffer.push_back((Data >> 8) | 0x80);
|
|
|
|
Buffer.push_back(Data & 0xff);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUInt<29>(Data)) {
|
|
|
|
Buffer.push_back((Data >> 24) | 0xC0);
|
|
|
|
Buffer.push_back((Data >> 16) & 0xff);
|
|
|
|
Buffer.push_back((Data >> 8) & 0xff);
|
|
|
|
Buffer.push_back(Data & 0xff);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:50:21 +08:00
|
|
|
static bool compressAnnotation(BinaryAnnotationsOpCode Annotation,
|
|
|
|
SmallVectorImpl<char> &Buffer) {
|
|
|
|
return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer);
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:24:12 +08:00
|
|
|
static uint32_t encodeSignedNumber(uint32_t Data) {
|
|
|
|
if (Data >> 31)
|
|
|
|
return ((-Data) << 1) | 1;
|
|
|
|
return Data << 1;
|
|
|
|
}
|
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS,
|
|
|
|
unsigned PrimaryFunctionId,
|
|
|
|
unsigned SourceFileId,
|
|
|
|
unsigned SourceLineNum,
|
|
|
|
const MCSymbol *FnStartSym,
|
|
|
|
const MCSymbol *FnEndSym) {
|
2016-02-03 01:41:18 +08:00
|
|
|
// Create and insert a fragment into the current section that will be encoded
|
|
|
|
// later.
|
2016-09-08 00:15:31 +08:00
|
|
|
new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId,
|
|
|
|
SourceLineNum, FnStartSym, FnEndSym,
|
|
|
|
OS.getCurrentSectionOnly());
|
2016-02-03 01:41:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-05 09:55:49 +08:00
|
|
|
void CodeViewContext::emitDefRange(
|
|
|
|
MCObjectStreamer &OS,
|
|
|
|
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
|
|
|
|
StringRef FixedSizePortion) {
|
|
|
|
// Create and insert a fragment into the current section that will be encoded
|
|
|
|
// later.
|
|
|
|
new MCCVDefRangeFragment(Ranges, FixedSizePortion,
|
|
|
|
OS.getCurrentSectionOnly());
|
|
|
|
}
|
|
|
|
|
2016-02-04 08:21:42 +08:00
|
|
|
static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin,
|
|
|
|
const MCSymbol *End) {
|
2016-02-03 01:41:18 +08:00
|
|
|
MCContext &Ctx = Layout.getAssembler().getContext();
|
|
|
|
MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
|
|
|
|
const MCExpr *BeginRef = MCSymbolRefExpr::create(Begin, Variant, Ctx),
|
|
|
|
*EndRef = MCSymbolRefExpr::create(End, Variant, Ctx);
|
|
|
|
const MCExpr *AddrDelta =
|
|
|
|
MCBinaryExpr::create(MCBinaryExpr::Sub, EndRef, BeginRef, Ctx);
|
|
|
|
int64_t Result;
|
|
|
|
bool Success = AddrDelta->evaluateKnownAbsolute(Result, Layout);
|
|
|
|
assert(Success && "failed to evaluate label difference as absolute");
|
|
|
|
(void)Success;
|
|
|
|
assert(Result >= 0 && "negative label difference requested");
|
|
|
|
assert(Result < UINT_MAX && "label difference greater than 2GB");
|
|
|
|
return unsigned(Result);
|
|
|
|
}
|
2016-01-30 03:24:12 +08:00
|
|
|
|
2016-02-03 01:41:18 +08:00
|
|
|
void CodeViewContext::encodeInlineLineTable(MCAsmLayout &Layout,
|
|
|
|
MCCVInlineLineTableFragment &Frag) {
|
|
|
|
size_t LocBegin;
|
|
|
|
size_t LocEnd;
|
|
|
|
std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId);
|
2016-09-08 00:15:31 +08:00
|
|
|
|
|
|
|
// Include all child inline call sites in our .cv_loc extent.
|
|
|
|
MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(Frag.SiteFuncId);
|
|
|
|
for (auto &KV : SiteInfo->InlinedAtMap) {
|
|
|
|
unsigned ChildId = KV.first;
|
|
|
|
auto Extent = getLineExtent(ChildId);
|
2016-02-03 01:41:18 +08:00
|
|
|
LocBegin = std::min(LocBegin, Extent.first);
|
|
|
|
LocEnd = std::max(LocEnd, Extent.second);
|
|
|
|
}
|
2016-09-08 00:15:31 +08:00
|
|
|
|
2016-02-03 01:41:18 +08:00
|
|
|
if (LocBegin >= LocEnd)
|
|
|
|
return;
|
2016-02-03 03:22:34 +08:00
|
|
|
ArrayRef<MCCVLineEntry> Locs = getLinesForExtent(LocBegin, LocEnd);
|
2016-02-03 01:41:18 +08:00
|
|
|
if (Locs.empty())
|
|
|
|
return;
|
2016-01-30 03:24:12 +08:00
|
|
|
|
2016-02-03 01:41:18 +08:00
|
|
|
// Make an artificial start location using the function start and the inlinee
|
|
|
|
// lines start location information. All deltas start relative to this
|
|
|
|
// location.
|
|
|
|
MCCVLineEntry StartLoc(Frag.getFnStartSym(), MCCVLoc(Locs.front()));
|
|
|
|
StartLoc.setFileNum(Frag.StartFileId);
|
|
|
|
StartLoc.setLine(Frag.StartLineNum);
|
2016-07-15 07:47:15 +08:00
|
|
|
bool HaveOpenRange = false;
|
2016-02-03 01:41:18 +08:00
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
const MCSymbol *LastLabel = Frag.getFnStartSym();
|
|
|
|
MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc;
|
|
|
|
LastSourceLoc.File = Frag.StartFileId;
|
|
|
|
LastSourceLoc.Line = Frag.StartLineNum;
|
|
|
|
|
2016-02-03 01:41:18 +08:00
|
|
|
SmallVectorImpl<char> &Buffer = Frag.getContents();
|
|
|
|
Buffer.clear(); // Clear old contents if we went through relaxation.
|
2016-01-30 03:24:12 +08:00
|
|
|
for (const MCCVLineEntry &Loc : Locs) {
|
2016-10-06 06:36:07 +08:00
|
|
|
// Exit early if our line table would produce an oversized InlineSiteSym
|
|
|
|
// record. Account for the ChangeCodeLength annotation emitted after the
|
|
|
|
// loop ends.
|
|
|
|
size_t MaxBufferSize = MaxRecordLength - sizeof(InlineSiteSym::Hdr) - 8;
|
|
|
|
if (Buffer.size() >= MaxBufferSize)
|
|
|
|
break;
|
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
if (Loc.getFunctionId() == Frag.SiteFuncId) {
|
|
|
|
CurSourceLoc.File = Loc.getFileNum();
|
|
|
|
CurSourceLoc.Line = Loc.getLine();
|
|
|
|
} else {
|
|
|
|
auto I = SiteInfo->InlinedAtMap.find(Loc.getFunctionId());
|
|
|
|
if (I != SiteInfo->InlinedAtMap.end()) {
|
|
|
|
// This .cv_loc is from a child inline call site. Use the source
|
|
|
|
// location of the inlined call site instead of the .cv_loc directive
|
|
|
|
// source location.
|
|
|
|
CurSourceLoc = I->second;
|
|
|
|
} else {
|
|
|
|
// We've hit a cv_loc not attributed to this inline call site. Use this
|
|
|
|
// label to end the PC range.
|
|
|
|
if (HaveOpenRange) {
|
|
|
|
unsigned Length = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
|
|
|
|
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
|
|
|
|
compressAnnotation(Length, Buffer);
|
|
|
|
LastLabel = Loc.getLabel();
|
|
|
|
}
|
|
|
|
HaveOpenRange = false;
|
|
|
|
continue;
|
2016-02-03 01:41:18 +08:00
|
|
|
}
|
2016-01-30 03:24:12 +08:00
|
|
|
}
|
2016-07-15 07:47:15 +08:00
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
// Skip this .cv_loc if we have an open range and this isn't a meaningful
|
|
|
|
// source location update. The current table format does not support column
|
|
|
|
// info, so we can skip updates for those.
|
|
|
|
if (HaveOpenRange && CurSourceLoc.File == LastSourceLoc.File &&
|
|
|
|
CurSourceLoc.Line == LastSourceLoc.Line)
|
2016-07-15 07:47:15 +08:00
|
|
|
continue;
|
2016-09-08 00:15:31 +08:00
|
|
|
|
2016-07-15 07:47:15 +08:00
|
|
|
HaveOpenRange = true;
|
2016-01-30 03:24:12 +08:00
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
if (CurSourceLoc.File != LastSourceLoc.File) {
|
2016-02-20 07:55:38 +08:00
|
|
|
// File ids are 1 based, and each file checksum table entry is 8 bytes
|
|
|
|
// long. See emitFileChecksums above.
|
2016-09-08 00:15:31 +08:00
|
|
|
unsigned FileOffset = 8 * (CurSourceLoc.File - 1);
|
2016-05-18 07:50:21 +08:00
|
|
|
compressAnnotation(BinaryAnnotationsOpCode::ChangeFile, Buffer);
|
2016-02-20 07:55:38 +08:00
|
|
|
compressAnnotation(FileOffset, Buffer);
|
2016-02-03 01:41:18 +08:00
|
|
|
}
|
2016-01-30 03:24:12 +08:00
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line;
|
2016-02-03 01:41:18 +08:00
|
|
|
unsigned EncodedLineDelta = encodeSignedNumber(LineDelta);
|
2016-09-08 00:15:31 +08:00
|
|
|
unsigned CodeDelta = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
|
|
|
|
if (CodeDelta == 0 && LineDelta != 0) {
|
2016-05-18 07:50:21 +08:00
|
|
|
compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);
|
2016-02-03 01:41:18 +08:00
|
|
|
compressAnnotation(EncodedLineDelta, Buffer);
|
|
|
|
} else if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) {
|
|
|
|
// The ChangeCodeOffsetAndLineOffset combination opcode is used when the
|
|
|
|
// encoded line delta uses 3 or fewer set bits and the code offset fits
|
|
|
|
// in one nibble.
|
|
|
|
unsigned Operand = (EncodedLineDelta << 4) | CodeDelta;
|
2016-05-18 07:50:21 +08:00
|
|
|
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset,
|
|
|
|
Buffer);
|
2016-02-03 01:41:18 +08:00
|
|
|
compressAnnotation(Operand, Buffer);
|
|
|
|
} else {
|
|
|
|
// Otherwise use the separate line and code deltas.
|
2016-09-08 00:15:31 +08:00
|
|
|
if (LineDelta != 0) {
|
|
|
|
compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);
|
|
|
|
compressAnnotation(EncodedLineDelta, Buffer);
|
|
|
|
}
|
2016-05-18 07:50:21 +08:00
|
|
|
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffset, Buffer);
|
2016-02-03 01:41:18 +08:00
|
|
|
compressAnnotation(CodeDelta, Buffer);
|
|
|
|
}
|
2016-01-30 03:24:12 +08:00
|
|
|
|
2016-09-08 00:15:31 +08:00
|
|
|
LastLabel = Loc.getLabel();
|
|
|
|
LastSourceLoc = CurSourceLoc;
|
2016-01-30 03:24:12 +08:00
|
|
|
}
|
2016-02-03 03:22:34 +08:00
|
|
|
|
2016-07-15 07:47:15 +08:00
|
|
|
assert(HaveOpenRange);
|
2016-02-03 03:22:34 +08:00
|
|
|
|
|
|
|
unsigned EndSymLength =
|
2016-09-08 00:15:31 +08:00
|
|
|
computeLabelDiff(Layout, LastLabel, Frag.getFnEndSym());
|
2016-02-03 03:22:34 +08:00
|
|
|
unsigned LocAfterLength = ~0U;
|
|
|
|
ArrayRef<MCCVLineEntry> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1);
|
2016-02-04 08:21:42 +08:00
|
|
|
if (!LocAfter.empty()) {
|
|
|
|
// Only try to compute this difference if we're in the same section.
|
|
|
|
const MCCVLineEntry &Loc = LocAfter[0];
|
2016-09-08 00:15:31 +08:00
|
|
|
if (&Loc.getLabel()->getSection(false) == &LastLabel->getSection(false))
|
|
|
|
LocAfterLength = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
|
2016-02-04 08:21:42 +08:00
|
|
|
}
|
2016-02-03 03:22:34 +08:00
|
|
|
|
2016-05-18 07:50:21 +08:00
|
|
|
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
|
2016-02-03 03:22:34 +08:00
|
|
|
compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer);
|
2016-01-30 03:24:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-05 09:55:49 +08:00
|
|
|
void CodeViewContext::encodeDefRange(MCAsmLayout &Layout,
|
|
|
|
MCCVDefRangeFragment &Frag) {
|
|
|
|
MCContext &Ctx = Layout.getAssembler().getContext();
|
|
|
|
SmallVectorImpl<char> &Contents = Frag.getContents();
|
|
|
|
Contents.clear();
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups = Frag.getFixups();
|
|
|
|
Fixups.clear();
|
|
|
|
raw_svector_ostream OS(Contents);
|
|
|
|
|
2016-09-16 06:05:08 +08:00
|
|
|
// Compute all the sizes up front.
|
|
|
|
SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes;
|
|
|
|
const MCSymbol *LastLabel = nullptr;
|
2016-02-05 09:55:49 +08:00
|
|
|
for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) {
|
2016-09-16 06:05:08 +08:00
|
|
|
unsigned GapSize =
|
|
|
|
LastLabel ? computeLabelDiff(Layout, LastLabel, Range.first) : 0;
|
2016-02-05 09:55:49 +08:00
|
|
|
unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second);
|
2016-09-16 06:05:08 +08:00
|
|
|
GapAndRangeSizes.push_back({GapSize, RangeSize});
|
|
|
|
LastLabel = Range.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write down each range where the variable is defined.
|
|
|
|
for (size_t I = 0, E = Frag.getRanges().size(); I != E;) {
|
|
|
|
// If the range size of multiple consecutive ranges is under the max,
|
|
|
|
// combine the ranges and emit some gaps.
|
|
|
|
const MCSymbol *RangeBegin = Frag.getRanges()[I].first;
|
|
|
|
unsigned RangeSize = GapAndRangeSizes[I].second;
|
|
|
|
size_t J = I + 1;
|
|
|
|
for (; J != E; ++J) {
|
|
|
|
unsigned GapAndRangeSize = GapAndRangeSizes[J].first + GapAndRangeSizes[J].second;
|
|
|
|
if (RangeSize + GapAndRangeSize > MaxDefRange)
|
|
|
|
break;
|
|
|
|
RangeSize += GapAndRangeSize;
|
|
|
|
}
|
|
|
|
unsigned NumGaps = J - I - 1;
|
|
|
|
|
|
|
|
support::endian::Writer<support::little> LEWriter(OS);
|
|
|
|
|
2016-02-05 09:55:49 +08:00
|
|
|
unsigned Bias = 0;
|
|
|
|
// We must split the range into chunks of MaxDefRange, this is a fundamental
|
|
|
|
// limitation of the file format.
|
|
|
|
do {
|
|
|
|
uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize);
|
|
|
|
|
2016-09-16 06:05:08 +08:00
|
|
|
const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx);
|
2016-02-05 09:55:49 +08:00
|
|
|
const MCBinaryExpr *BE =
|
|
|
|
MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx);
|
|
|
|
MCValue Res;
|
|
|
|
BE->evaluateAsRelocatable(Res, &Layout, /*Fixup=*/nullptr);
|
|
|
|
|
|
|
|
// Each record begins with a 2-byte number indicating how large the record
|
|
|
|
// is.
|
|
|
|
StringRef FixedSizePortion = Frag.getFixedSizePortion();
|
|
|
|
// Our record is a fixed sized prefix and a LocalVariableAddrRange that we
|
|
|
|
// are artificially constructing.
|
2016-09-16 06:05:08 +08:00
|
|
|
size_t RecordSize = FixedSizePortion.size() +
|
|
|
|
sizeof(LocalVariableAddrRange) + 4 * NumGaps;
|
2016-02-05 09:55:49 +08:00
|
|
|
// Write out the recrod size.
|
|
|
|
support::endian::Writer<support::little>(OS).write<uint16_t>(RecordSize);
|
|
|
|
// Write out the fixed size prefix.
|
|
|
|
OS << FixedSizePortion;
|
|
|
|
// Make space for a fixup that will eventually have a section relative
|
|
|
|
// relocation pointing at the offset where the variable becomes live.
|
|
|
|
Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4));
|
|
|
|
Contents.resize(Contents.size() + 4); // Fixup for code start.
|
|
|
|
// Make space for a fixup that will record the section index for the code.
|
|
|
|
Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2));
|
|
|
|
Contents.resize(Contents.size() + 2); // Fixup for section index.
|
|
|
|
// Write down the range's extent.
|
2016-09-16 06:05:08 +08:00
|
|
|
LEWriter.write<uint16_t>(Chunk);
|
2016-02-05 09:55:49 +08:00
|
|
|
|
|
|
|
// Move on to the next range.
|
|
|
|
Bias += Chunk;
|
|
|
|
RangeSize -= Chunk;
|
|
|
|
} while (RangeSize > 0);
|
2016-09-16 06:05:08 +08:00
|
|
|
|
|
|
|
// Emit the gaps afterwards.
|
|
|
|
assert((NumGaps == 0 || Bias < MaxDefRange) &&
|
|
|
|
"large ranges should not have gaps");
|
|
|
|
unsigned GapStartOffset = GapAndRangeSizes[I].second;
|
|
|
|
for (++I; I != J; ++I) {
|
|
|
|
unsigned GapSize, RangeSize;
|
|
|
|
assert(I < GapAndRangeSizes.size());
|
|
|
|
std::tie(GapSize, RangeSize) = GapAndRangeSizes[I];
|
|
|
|
LEWriter.write<uint16_t>(GapStartOffset);
|
|
|
|
LEWriter.write<uint16_t>(RangeSize);
|
|
|
|
GapStartOffset += GapSize + RangeSize;
|
|
|
|
}
|
2016-02-05 09:55:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
//
|
|
|
|
// This is called when an instruction is assembled into the specified section
|
|
|
|
// and if there is information from the last .cv_loc directive that has yet to have
|
|
|
|
// a line entry made for it is made.
|
|
|
|
//
|
|
|
|
void MCCVLineEntry::Make(MCObjectStreamer *MCOS) {
|
2016-08-27 01:58:37 +08:00
|
|
|
CodeViewContext &CVC = MCOS->getContext().getCVContext();
|
|
|
|
if (!CVC.getCVLocSeen())
|
2016-01-29 08:49:42 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a symbol at in the current section for use in the line entry.
|
|
|
|
MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
|
|
|
|
// Set the value of the symbol to use for the MCCVLineEntry.
|
|
|
|
MCOS->EmitLabel(LineSym);
|
|
|
|
|
|
|
|
// Get the current .loc info saved in the context.
|
2016-08-27 01:58:37 +08:00
|
|
|
const MCCVLoc &CVLoc = CVC.getCurrentCVLoc();
|
2016-01-29 08:49:42 +08:00
|
|
|
|
|
|
|
// Create a (local) line entry with the symbol and the current .loc info.
|
|
|
|
MCCVLineEntry LineEntry(LineSym, CVLoc);
|
|
|
|
|
|
|
|
// clear CVLocSeen saying the current .loc info is now used.
|
2016-08-27 01:58:37 +08:00
|
|
|
CVC.clearCVLocSeen();
|
2016-01-29 08:49:42 +08:00
|
|
|
|
|
|
|
// Add the line entry to this section's entries.
|
2016-08-27 01:58:37 +08:00
|
|
|
CVC.addLineEntry(LineEntry);
|
2016-01-29 08:49:42 +08:00
|
|
|
}
|