#include "llvm/MC/MCCodeView.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/DebugInfo/CodeView/CodeView.h"
#include "llvm/DebugInfo/CodeView/Line.h"
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/EndianStream.h"
using namespace llvm;
using namespace llvm::codeview;
CodeViewContext::CodeViewContext() = default;
CodeViewContext::~CodeViewContext() {
if (!InsertedStrTabFragment)
delete StrTabFragment;
}
bool CodeViewContext::isValidFileNumber(unsigned FileNumber) const {
unsigned Idx = FileNumber - 1;
if (Idx < Files.size())
return Files[Idx].Assigned;
return false;
}
bool CodeViewContext::addFile(MCStreamer &OS, unsigned FileNumber,
StringRef Filename,
ArrayRef<uint8_t> ChecksumBytes,
uint8_t ChecksumKind) {
assert(FileNumber > 0);
auto FilenameOffset = addToStringTable(Filename);
Filename = FilenameOffset.first;
unsigned Idx = FileNumber - 1;
if (Idx >= Files.size())
Files.resize(Idx + 1);
if (Filename.empty())
Filename = "<stdin>";
if (Files[Idx].Assigned)
return false;
FilenameOffset = addToStringTable(Filename);
Filename = FilenameOffset.first;
unsigned Offset = FilenameOffset.second;
auto ChecksumOffsetSymbol =
OS.getContext().createTempSymbol("checksum_offset", false);
Files[Idx].StringTableOffset = Offset;
Files[Idx].ChecksumTableOffset = ChecksumOffsetSymbol;
Files[Idx].Assigned = true;
Files[Idx].Checksum = ChecksumBytes;
Files[Idx].ChecksumKind = ChecksumKind;
return true;
}
MCCVFunctionInfo *CodeViewContext::getCVFunctionInfo(unsigned FuncId) {
if (FuncId >= Functions.size())
return nullptr;
if (Functions[FuncId].isUnallocatedFunctionInfo())
return nullptr;
return &Functions[FuncId];
}
bool CodeViewContext::recordFunctionId(unsigned FuncId) {
if (FuncId >= Functions.size())
Functions.resize(FuncId + 1);
if (!Functions[FuncId].isUnallocatedFunctionInfo())
return false;
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);
if (!Functions[FuncId].isUnallocatedFunctionInfo())
return false;
MCCVFunctionInfo::LineInfo InlinedAt;
InlinedAt.File = IAFile;
InlinedAt.Line = IALine;
InlinedAt.Col = IACol;
MCCVFunctionInfo *Info = &Functions[FuncId];
Info->ParentFuncIdPlusOne = IAFunc + 1;
Info->InlinedAt = InlinedAt;
while (Info->isInlinedCallSite()) {
InlinedAt = Info->InlinedAt;
Info = getCVFunctionInfo(Info->getParentFuncId());
Info->InlinedAtMap[FuncId] = InlinedAt;
}
return true;
}
void CodeViewContext::recordCVLoc(MCContext &Ctx, const MCSymbol *Label,
unsigned FunctionId, unsigned FileNo,
unsigned Line, unsigned Column,
bool PrologueEnd, bool IsStmt) {
addLineEntry(MCCVLoc{
Label, FunctionId, FileNo, Line, Column, PrologueEnd, IsStmt});
}
MCDataFragment *CodeViewContext::getStringTableFragment() {
if (!StrTabFragment) {
StrTabFragment = new MCDataFragment();
StrTabFragment->getContents().push_back('\0');
}
return StrTabFragment;
}
std::pair<StringRef, unsigned> CodeViewContext::addToStringTable(StringRef S) {
SmallVectorImpl<char> &Contents = getStringTableFragment()->getContents();
auto Insertion =
StringTable.insert(std::make_pair(S, unsigned(Contents.size())));
std::pair<StringRef, unsigned> Ret =
std::make_pair(Insertion.first->first(), Insertion.first->second);
if (Insertion.second) {
Contents.append(Ret.first.begin(), Ret.first.end() + 1);
}
return Ret;
}
unsigned CodeViewContext::getStringTableOffset(StringRef S) {
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();
MCSymbol *StringBegin = Ctx.createTempSymbol("strtab_begin", false),
*StringEnd = Ctx.createTempSymbol("strtab_end", false);
OS.emitInt32(uint32_t(DebugSubsectionKind::StringTable));
OS.emitAbsoluteSymbolDiff(StringEnd, StringBegin, 4);
OS.emitLabel(StringBegin);
if (!InsertedStrTabFragment) {
OS.insert(getStringTableFragment());
InsertedStrTabFragment = true;
}
OS.emitValueToAlignment(4, 0);
OS.emitLabel(StringEnd);
}
void CodeViewContext::emitFileChecksums(MCObjectStreamer &OS) {
if (Files.empty())
return;
MCContext &Ctx = OS.getContext();
MCSymbol *FileBegin = Ctx.createTempSymbol("filechecksums_begin", false),
*FileEnd = Ctx.createTempSymbol("filechecksums_end", false);
OS.emitInt32(uint32_t(DebugSubsectionKind::FileChecksums));
OS.emitAbsoluteSymbolDiff(FileEnd, FileBegin, 4);
OS.emitLabel(FileBegin);
unsigned CurrentOffset = 0;
for (auto File : Files) {
OS.emitAssignment(File.ChecksumTableOffset,
MCConstantExpr::create(CurrentOffset, Ctx));
CurrentOffset += 4; if (!File.ChecksumKind) {
CurrentOffset +=
4; } else {
CurrentOffset += 2; CurrentOffset += File.Checksum.size();
CurrentOffset = alignTo(CurrentOffset, 4);
}
OS.emitInt32(File.StringTableOffset);
if (!File.ChecksumKind) {
OS.emitInt32(0);
continue;
}
OS.emitInt8(static_cast<uint8_t>(File.Checksum.size()));
OS.emitInt8(File.ChecksumKind);
OS.emitBytes(toStringRef(File.Checksum));
OS.emitValueToAlignment(4);
}
OS.emitLabel(FileEnd);
ChecksumOffsetsAssigned = true;
}
void CodeViewContext::emitFileChecksumOffset(MCObjectStreamer &OS,
unsigned FileNo) {
unsigned Idx = FileNo - 1;
if (Idx >= Files.size())
Files.resize(Idx + 1);
if (ChecksumOffsetsAssigned) {
OS.emitSymbolValue(Files[Idx].ChecksumTableOffset, 4);
return;
}
const MCSymbolRefExpr *SRE =
MCSymbolRefExpr::create(Files[Idx].ChecksumTableOffset, OS.getContext());
OS.emitValueImpl(SRE, 4);
}
void CodeViewContext::addLineEntry(const MCCVLoc &LineEntry) {
size_t Offset = MCCVLines.size();
auto I = MCCVLineStartStop.insert(
{LineEntry.getFunctionId(), {Offset, Offset + 1}});
if (!I.second)
I.first->second.second = Offset + 1;
MCCVLines.push_back(LineEntry);
}
std::vector<MCCVLoc>
CodeViewContext::getFunctionLineEntries(unsigned FuncId) {
std::vector<MCCVLoc> FilteredLines;
auto I = MCCVLineStartStop.find(FuncId);
if (I != MCCVLineStartStop.end()) {
MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(FuncId);
for (size_t Idx = I->second.first, End = I->second.second; Idx != End;
++Idx) {
unsigned LocationFuncId = MCCVLines[Idx].getFunctionId();
if (LocationFuncId == FuncId) {
FilteredLines.push_back(MCCVLines[Idx]);
} else {
auto I = SiteInfo->InlinedAtMap.find(LocationFuncId);
if (I != SiteInfo->InlinedAtMap.end()) {
MCCVFunctionInfo::LineInfo &IA = I->second;
if (FilteredLines.empty() ||
FilteredLines.back().getFileNum() != IA.File ||
FilteredLines.back().getLine() != IA.Line ||
FilteredLines.back().getColumn() != IA.Col) {
FilteredLines.push_back(MCCVLoc(
MCCVLines[Idx].getLabel(),
FuncId, IA.File, IA.Line, IA.Col, false, false));
}
}
}
}
}
return FilteredLines;
}
std::pair<size_t, size_t> CodeViewContext::getLineExtent(unsigned FuncId) {
auto I = MCCVLineStartStop.find(FuncId);
if (I == MCCVLineStartStop.end())
return {~0ULL, 0};
return I->second;
}
ArrayRef<MCCVLoc> CodeViewContext::getLinesForExtent(size_t L, size_t R) {
if (R <= L)
return None;
if (L >= MCCVLines.size())
return None;
return makeArrayRef(&MCCVLines[L], R - L);
}
void CodeViewContext::emitLineTableForFunction(MCObjectStreamer &OS,
unsigned FuncId,
const MCSymbol *FuncBegin,
const MCSymbol *FuncEnd) {
MCContext &Ctx = OS.getContext();
MCSymbol *LineBegin = Ctx.createTempSymbol("linetable_begin", false),
*LineEnd = Ctx.createTempSymbol("linetable_end", false);
OS.emitInt32(uint32_t(DebugSubsectionKind::Lines));
OS.emitAbsoluteSymbolDiff(LineEnd, LineBegin, 4);
OS.emitLabel(LineBegin);
OS.emitCOFFSecRel32(FuncBegin, 0);
OS.emitCOFFSectionIndex(FuncBegin);
std::vector<MCCVLoc> Locs = getFunctionLineEntries(FuncId);
bool HaveColumns = any_of(Locs, [](const MCCVLoc &LineEntry) {
return LineEntry.getColumn() != 0;
});
OS.emitInt16(HaveColumns ? int(LF_HaveColumns) : 0);
OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 4);
for (auto I = Locs.begin(), E = Locs.end(); I != E;) {
unsigned CurFileNum = I->getFileNum();
auto FileSegEnd =
std::find_if(I, E, [CurFileNum](const MCCVLoc &Loc) {
return Loc.getFileNum() != CurFileNum;
});
unsigned EntryCount = FileSegEnd - I;
OS.AddComment(
"Segment for file '" +
Twine(getStringTableFragment()
->getContents()[Files[CurFileNum - 1].StringTableOffset]) +
"' begins");
OS.emitCVFileChecksumOffsetDirective(CurFileNum);
OS.emitInt32(EntryCount);
uint32_t SegmentSize = 12;
SegmentSize += 8 * EntryCount;
if (HaveColumns)
SegmentSize += 4 * EntryCount;
OS.emitInt32(SegmentSize);
for (auto J = I; J != FileSegEnd; ++J) {
OS.emitAbsoluteSymbolDiff(J->getLabel(), FuncBegin, 4);
unsigned LineData = J->getLine();
if (J->isStmt())
LineData |= LineInfo::StatementFlag;
OS.emitInt32(LineData);
}
if (HaveColumns) {
for (auto J = I; J != FileSegEnd; ++J) {
OS.emitInt16(J->getColumn());
OS.emitInt16(0);
}
}
I = FileSegEnd;
}
OS.emitLabel(LineEnd);
}
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;
}
static bool compressAnnotation(BinaryAnnotationsOpCode Annotation,
SmallVectorImpl<char> &Buffer) {
return compressAnnotation(static_cast<uint32_t>(Annotation), Buffer);
}
static uint32_t encodeSignedNumber(uint32_t Data) {
if (Data >> 31)
return ((-Data) << 1) | 1;
return Data << 1;
}
void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS,
unsigned PrimaryFunctionId,
unsigned SourceFileId,
unsigned SourceLineNum,
const MCSymbol *FnStartSym,
const MCSymbol *FnEndSym) {
new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId,
SourceLineNum, FnStartSym, FnEndSym,
OS.getCurrentSectionOnly());
}
MCFragment *CodeViewContext::emitDefRange(
MCObjectStreamer &OS,
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
StringRef FixedSizePortion) {
return new MCCVDefRangeFragment(Ranges, FixedSizePortion,
OS.getCurrentSectionOnly());
}
static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin,
const MCSymbol *End) {
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);
}
void CodeViewContext::encodeInlineLineTable(MCAsmLayout &Layout,
MCCVInlineLineTableFragment &Frag) {
size_t LocBegin;
size_t LocEnd;
std::tie(LocBegin, LocEnd) = getLineExtent(Frag.SiteFuncId);
MCCVFunctionInfo *SiteInfo = getCVFunctionInfo(Frag.SiteFuncId);
for (auto &KV : SiteInfo->InlinedAtMap) {
unsigned ChildId = KV.first;
auto Extent = getLineExtent(ChildId);
LocBegin = std::min(LocBegin, Extent.first);
LocEnd = std::max(LocEnd, Extent.second);
}
if (LocBegin >= LocEnd)
return;
ArrayRef<MCCVLoc> Locs = getLinesForExtent(LocBegin, LocEnd);
if (Locs.empty())
return;
#ifndef NDEBUG
const MCSection *FirstSec = &Locs.front().getLabel()->getSection();
for (const MCCVLoc &Loc : Locs) {
if (&Loc.getLabel()->getSection() != FirstSec) {
errs() << ".cv_loc " << Loc.getFunctionId() << ' ' << Loc.getFileNum()
<< ' ' << Loc.getLine() << ' ' << Loc.getColumn()
<< " is in the wrong section\n";
llvm_unreachable(".cv_loc crosses sections");
}
}
#endif
MCCVLoc StartLoc = Locs.front();
StartLoc.setLabel(Frag.getFnStartSym());
StartLoc.setFileNum(Frag.StartFileId);
StartLoc.setLine(Frag.StartLineNum);
bool HaveOpenRange = false;
const MCSymbol *LastLabel = Frag.getFnStartSym();
MCCVFunctionInfo::LineInfo LastSourceLoc, CurSourceLoc;
LastSourceLoc.File = Frag.StartFileId;
LastSourceLoc.Line = Frag.StartLineNum;
SmallVectorImpl<char> &Buffer = Frag.getContents();
Buffer.clear(); for (const MCCVLoc &Loc : Locs) {
constexpr uint32_t InlineSiteSize = 12;
constexpr uint32_t AnnotationSize = 8;
size_t MaxBufferSize = MaxRecordLength - InlineSiteSize - AnnotationSize;
if (Buffer.size() >= MaxBufferSize)
break;
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()) {
CurSourceLoc = I->second;
} else {
if (HaveOpenRange) {
unsigned Length = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
compressAnnotation(Length, Buffer);
LastLabel = Loc.getLabel();
}
HaveOpenRange = false;
continue;
}
}
if (HaveOpenRange && CurSourceLoc.File == LastSourceLoc.File &&
CurSourceLoc.Line == LastSourceLoc.Line)
continue;
HaveOpenRange = true;
if (CurSourceLoc.File != LastSourceLoc.File) {
unsigned FileOffset = static_cast<const MCConstantExpr *>(
Files[CurSourceLoc.File - 1]
.ChecksumTableOffset->getVariableValue())
->getValue();
compressAnnotation(BinaryAnnotationsOpCode::ChangeFile, Buffer);
compressAnnotation(FileOffset, Buffer);
}
int LineDelta = CurSourceLoc.Line - LastSourceLoc.Line;
unsigned EncodedLineDelta = encodeSignedNumber(LineDelta);
unsigned CodeDelta = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
if (EncodedLineDelta < 0x8 && CodeDelta <= 0xf) {
unsigned Operand = (EncodedLineDelta << 4) | CodeDelta;
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset,
Buffer);
compressAnnotation(Operand, Buffer);
} else {
if (LineDelta != 0) {
compressAnnotation(BinaryAnnotationsOpCode::ChangeLineOffset, Buffer);
compressAnnotation(EncodedLineDelta, Buffer);
}
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeOffset, Buffer);
compressAnnotation(CodeDelta, Buffer);
}
LastLabel = Loc.getLabel();
LastSourceLoc = CurSourceLoc;
}
assert(HaveOpenRange);
unsigned EndSymLength =
computeLabelDiff(Layout, LastLabel, Frag.getFnEndSym());
unsigned LocAfterLength = ~0U;
ArrayRef<MCCVLoc> LocAfter = getLinesForExtent(LocEnd, LocEnd + 1);
if (!LocAfter.empty()) {
const MCCVLoc &Loc = LocAfter[0];
if (&Loc.getLabel()->getSection() == &LastLabel->getSection())
LocAfterLength = computeLabelDiff(Layout, LastLabel, Loc.getLabel());
}
compressAnnotation(BinaryAnnotationsOpCode::ChangeCodeLength, Buffer);
compressAnnotation(std::min(EndSymLength, LocAfterLength), Buffer);
}
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);
SmallVector<std::pair<unsigned, unsigned>, 4> GapAndRangeSizes;
const MCSymbol *LastLabel = nullptr;
for (std::pair<const MCSymbol *, const MCSymbol *> Range : Frag.getRanges()) {
unsigned GapSize =
LastLabel ? computeLabelDiff(Layout, LastLabel, Range.first) : 0;
unsigned RangeSize = computeLabelDiff(Layout, Range.first, Range.second);
GapAndRangeSizes.push_back({GapSize, RangeSize});
LastLabel = Range.second;
}
for (size_t I = 0, E = Frag.getRanges().size(); I != E;) {
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 LEWriter(OS, support::little);
unsigned Bias = 0;
do {
uint16_t Chunk = std::min((uint32_t)MaxDefRange, RangeSize);
const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(RangeBegin, Ctx);
const MCBinaryExpr *BE =
MCBinaryExpr::createAdd(SRE, MCConstantExpr::create(Bias, Ctx), Ctx);
MCValue Res;
BE->evaluateAsRelocatable(Res, &Layout, nullptr);
StringRef FixedSizePortion = Frag.getFixedSizePortion();
size_t RecordSize = FixedSizePortion.size() +
sizeof(LocalVariableAddrRange) + 4 * NumGaps;
LEWriter.write<uint16_t>(RecordSize);
OS << FixedSizePortion;
Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_4));
LEWriter.write<uint32_t>(0); Fixups.push_back(MCFixup::create(Contents.size(), BE, FK_SecRel_2));
LEWriter.write<uint16_t>(0); LEWriter.write<uint16_t>(Chunk);
Bias += Chunk;
RangeSize -= Chunk;
} while (RangeSize > 0);
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>(GapSize);
GapStartOffset += GapSize + RangeSize;
}
}
}