2016-01-29 01:49:42 +01:00
|
|
|
//===- MCCodeView.h - Machine Code CodeView support -------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-01-29 01:49:42 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Holds state from .cv_file and .cv_loc directives for later emission.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_MC_MCCODEVIEW_H
|
|
|
|
#define LLVM_MC_MCCODEVIEW_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-01-29 01:49:42 +01:00
|
|
|
#include "llvm/MC/MCFragment.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
2016-01-29 01:49:42 +01:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class MCContext;
|
|
|
|
class MCObjectStreamer;
|
|
|
|
class MCStreamer;
|
2016-08-26 19:58:37 +02:00
|
|
|
class CodeViewContext;
|
2016-01-29 01:49:42 +01:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Instances of this class represent the information from a
|
2016-01-29 01:49:42 +01:00
|
|
|
/// .cv_loc directive.
|
|
|
|
class MCCVLoc {
|
2018-08-29 01:25:59 +02:00
|
|
|
const MCSymbol *Label = nullptr;
|
2016-01-29 01:49:42 +01:00
|
|
|
uint32_t FunctionId;
|
|
|
|
uint32_t FileNum;
|
|
|
|
uint32_t Line;
|
|
|
|
uint16_t Column;
|
|
|
|
uint16_t PrologueEnd : 1;
|
|
|
|
uint16_t IsStmt : 1;
|
|
|
|
|
2016-08-26 19:58:37 +02:00
|
|
|
private: // CodeViewContext manages these
|
|
|
|
friend class CodeViewContext;
|
2018-08-29 01:25:59 +02:00
|
|
|
MCCVLoc(const MCSymbol *Label, unsigned functionid, unsigned fileNum,
|
|
|
|
unsigned line, unsigned column, bool prologueend, bool isstmt)
|
|
|
|
: Label(Label), FunctionId(functionid), FileNum(fileNum), Line(line),
|
|
|
|
Column(column), PrologueEnd(prologueend), IsStmt(isstmt) {}
|
2016-01-29 01:49:42 +01:00
|
|
|
|
|
|
|
// Allow the default copy constructor and assignment operator to be used
|
|
|
|
// for an MCCVLoc object.
|
|
|
|
|
|
|
|
public:
|
2018-08-29 01:25:59 +02:00
|
|
|
const MCSymbol *getLabel() const { return Label; }
|
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
unsigned getFunctionId() const { return FunctionId; }
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the FileNum of this MCCVLoc.
|
2016-01-29 01:49:42 +01:00
|
|
|
unsigned getFileNum() const { return FileNum; }
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the Line of this MCCVLoc.
|
2016-01-29 01:49:42 +01:00
|
|
|
unsigned getLine() const { return Line; }
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the Column of this MCCVLoc.
|
2016-01-29 01:49:42 +01:00
|
|
|
unsigned getColumn() const { return Column; }
|
|
|
|
|
|
|
|
bool isPrologueEnd() const { return PrologueEnd; }
|
|
|
|
bool isStmt() const { return IsStmt; }
|
|
|
|
|
2018-08-29 01:25:59 +02:00
|
|
|
void setLabel(const MCSymbol *L) { Label = L; }
|
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
void setFunctionId(unsigned FID) { FunctionId = FID; }
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Set the FileNum of this MCCVLoc.
|
2016-01-29 01:49:42 +01:00
|
|
|
void setFileNum(unsigned fileNum) { FileNum = fileNum; }
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Set the Line of this MCCVLoc.
|
2016-01-29 01:49:42 +01:00
|
|
|
void setLine(unsigned line) { Line = line; }
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Set the Column of this MCCVLoc.
|
2016-01-29 01:49:42 +01:00
|
|
|
void setColumn(unsigned column) {
|
|
|
|
assert(column <= UINT16_MAX);
|
|
|
|
Column = column;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPrologueEnd(bool PE) { PrologueEnd = PE; }
|
|
|
|
void setIsStmt(bool IS) { IsStmt = IS; }
|
|
|
|
};
|
|
|
|
|
2016-09-07 18:15:31 +02:00
|
|
|
/// Information describing a function or inlined call site introduced by
|
|
|
|
/// .cv_func_id or .cv_inline_site_id. Accumulates information from .cv_loc
|
|
|
|
/// directives used with this function's id or the id of an inlined call site
|
|
|
|
/// within this function or inlined call site.
|
|
|
|
struct MCCVFunctionInfo {
|
|
|
|
/// If this represents an inlined call site, then ParentFuncIdPlusOne will be
|
|
|
|
/// the parent function id plus one. If this represents a normal function,
|
|
|
|
/// then there is no parent, and ParentFuncIdPlusOne will be FunctionSentinel.
|
|
|
|
/// If this struct is an unallocated slot in the function info vector, then
|
|
|
|
/// ParentFuncIdPlusOne will be zero.
|
|
|
|
unsigned ParentFuncIdPlusOne = 0;
|
|
|
|
|
|
|
|
enum : unsigned { FunctionSentinel = ~0U };
|
|
|
|
|
|
|
|
struct LineInfo {
|
|
|
|
unsigned File;
|
|
|
|
unsigned Line;
|
|
|
|
unsigned Col;
|
|
|
|
};
|
|
|
|
|
|
|
|
LineInfo InlinedAt;
|
|
|
|
|
|
|
|
/// The section of the first .cv_loc directive used for this function, or null
|
|
|
|
/// if none has been seen yet.
|
|
|
|
MCSection *Section = nullptr;
|
|
|
|
|
|
|
|
/// Map from inlined call site id to the inlined at location to use for that
|
|
|
|
/// call site. Call chains are collapsed, so for the call chain 'f -> g -> h',
|
|
|
|
/// the InlinedAtMap of 'f' will contain entries for 'g' and 'h' that both
|
|
|
|
/// list the line info for the 'g' call site.
|
|
|
|
DenseMap<unsigned, LineInfo> InlinedAtMap;
|
|
|
|
|
|
|
|
/// Returns true if this is function info has not yet been used in a
|
|
|
|
/// .cv_func_id or .cv_inline_site_id directive.
|
|
|
|
bool isUnallocatedFunctionInfo() const { return ParentFuncIdPlusOne == 0; }
|
|
|
|
|
|
|
|
/// Returns true if this represents an inlined call site, meaning
|
|
|
|
/// ParentFuncIdPlusOne is neither zero nor ~0U.
|
|
|
|
bool isInlinedCallSite() const {
|
|
|
|
return !isUnallocatedFunctionInfo() &&
|
|
|
|
ParentFuncIdPlusOne != FunctionSentinel;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getParentFuncId() const {
|
|
|
|
assert(isInlinedCallSite());
|
|
|
|
return ParentFuncIdPlusOne - 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
/// Holds state from .cv_file and .cv_loc directives for later emission.
|
|
|
|
class CodeViewContext {
|
|
|
|
public:
|
|
|
|
CodeViewContext();
|
|
|
|
~CodeViewContext();
|
|
|
|
|
|
|
|
bool isValidFileNumber(unsigned FileNumber) const;
|
2017-09-19 20:14:45 +02:00
|
|
|
bool addFile(MCStreamer &OS, unsigned FileNumber, StringRef Filename,
|
|
|
|
ArrayRef<uint8_t> ChecksumBytes, uint8_t ChecksumKind);
|
2016-01-29 01:49:42 +01:00
|
|
|
|
2016-09-07 18:15:31 +02:00
|
|
|
/// Records the function id of a normal function. Returns false if the
|
|
|
|
/// function id has already been used, and true otherwise.
|
|
|
|
bool recordFunctionId(unsigned FuncId);
|
|
|
|
|
|
|
|
/// Records the function id of an inlined call site. Records the "inlined at"
|
|
|
|
/// location info of the call site, including what function or inlined call
|
|
|
|
/// site it was inlined into. Returns false if the function id has already
|
|
|
|
/// been used, and true otherwise.
|
|
|
|
bool recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc,
|
|
|
|
unsigned IAFile, unsigned IALine,
|
|
|
|
unsigned IACol);
|
|
|
|
|
|
|
|
/// Retreive the function info if this is a valid function id, or nullptr.
|
2018-01-18 23:55:14 +01:00
|
|
|
MCCVFunctionInfo *getCVFunctionInfo(unsigned FuncId);
|
2016-09-07 18:15:31 +02:00
|
|
|
|
2016-08-26 19:58:37 +02:00
|
|
|
/// Saves the information from the currently parsed .cv_loc directive
|
|
|
|
/// and sets CVLocSeen. When the next instruction is assembled an entry
|
|
|
|
/// in the line number table with this information and the address of the
|
|
|
|
/// instruction will be created.
|
2018-08-29 01:25:59 +02:00
|
|
|
void recordCVLoc(MCContext &Ctx, const MCSymbol *Label, unsigned FunctionId,
|
|
|
|
unsigned FileNo, unsigned Line, unsigned Column,
|
|
|
|
bool PrologueEnd, bool IsStmt);
|
2016-08-26 19:58:37 +02:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Add a line entry.
|
2018-08-29 01:25:59 +02:00
|
|
|
void addLineEntry(const MCCVLoc &LineEntry);
|
2016-01-29 01:49:42 +01:00
|
|
|
|
2018-08-29 01:25:59 +02:00
|
|
|
std::vector<MCCVLoc> getFunctionLineEntries(unsigned FuncId);
|
2016-01-30 01:36:09 +01:00
|
|
|
|
2018-01-18 23:55:14 +01:00
|
|
|
std::pair<size_t, size_t> getLineExtent(unsigned FuncId);
|
2016-02-02 18:41:18 +01:00
|
|
|
|
2018-08-29 01:25:59 +02:00
|
|
|
ArrayRef<MCCVLoc> getLinesForExtent(size_t L, size_t R);
|
2016-02-02 18:41:18 +01:00
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
/// Emits a line table substream.
|
|
|
|
void emitLineTableForFunction(MCObjectStreamer &OS, unsigned FuncId,
|
|
|
|
const MCSymbol *FuncBegin,
|
|
|
|
const MCSymbol *FuncEnd);
|
|
|
|
|
2016-09-07 18:15:31 +02:00
|
|
|
void emitInlineLineTableForFunction(MCObjectStreamer &OS,
|
|
|
|
unsigned PrimaryFunctionId,
|
|
|
|
unsigned SourceFileId,
|
|
|
|
unsigned SourceLineNum,
|
|
|
|
const MCSymbol *FnStartSym,
|
|
|
|
const MCSymbol *FnEndSym);
|
2016-02-02 18:41:18 +01:00
|
|
|
|
|
|
|
/// Encodes the binary annotations once we have a layout.
|
|
|
|
void encodeInlineLineTable(MCAsmLayout &Layout,
|
|
|
|
MCCVInlineLineTableFragment &F);
|
2016-02-05 02:55:49 +01:00
|
|
|
|
2018-12-17 22:49:35 +01:00
|
|
|
MCFragment *
|
2016-02-05 02:55:49 +01:00
|
|
|
emitDefRange(MCObjectStreamer &OS,
|
|
|
|
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
|
|
|
|
StringRef FixedSizePortion);
|
|
|
|
|
|
|
|
void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F);
|
2016-01-29 20:24:12 +01:00
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
/// Emits the string table substream.
|
|
|
|
void emitStringTable(MCObjectStreamer &OS);
|
|
|
|
|
|
|
|
/// Emits the file checksum substream.
|
|
|
|
void emitFileChecksums(MCObjectStreamer &OS);
|
|
|
|
|
2017-09-19 20:14:45 +02:00
|
|
|
/// Emits the offset into the checksum table of the given file number.
|
|
|
|
void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo);
|
|
|
|
|
[codeview] Implement FPO data assembler directives
Summary:
This adds a set of new directives that describe 32-bit x86 prologues.
The directives are limited and do not expose the full complexity of
codeview FPO data. They are merely a convenience for the compiler to
generate more readable assembly so we don't need to generate tons of
labels in CodeGen. If our prologue emission changes in the future, we
can change the set of available directives to suit our needs. These are
modelled after the .seh_ directives, which use a different format that
interacts with exception handling.
The directives are:
.cv_fpo_proc _foo
.cv_fpo_pushreg ebp/ebx/etc
.cv_fpo_setframe ebp/esi/etc
.cv_fpo_stackalloc 200
.cv_fpo_endprologue
.cv_fpo_endproc
.cv_fpo_data _foo
I tried to follow the implementation of ARM EHABI CFI directives by
sinking most directives out of MCStreamer and into X86TargetStreamer.
This helps avoid polluting non-X86 code with WinCOFF specific logic.
I used cdb to confirm that this can show locals in parent CSRs in a few
cases, most importantly the one where we use ESI as a frame pointer,
i.e. the one in http://crbug.com/756153#c28
Once we have cdb integration in debuginfo-tests, we can add integration
tests there.
Reviewers: majnemer, hans
Subscribers: aemerson, mgorny, kristof.beyls, llvm-commits, hiraditya
Differential Revision: https://reviews.llvm.org/D38776
llvm-svn: 315513
2017-10-11 23:24:33 +02:00
|
|
|
/// Add something to the string table. Returns the final string as well as
|
|
|
|
/// offset into the string table.
|
|
|
|
std::pair<StringRef, unsigned> addToStringTable(StringRef S);
|
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
private:
|
|
|
|
/// Map from string to string table offset.
|
|
|
|
StringMap<unsigned> StringTable;
|
|
|
|
|
|
|
|
/// The fragment that ultimately holds our strings.
|
|
|
|
MCDataFragment *StrTabFragment = nullptr;
|
|
|
|
bool InsertedStrTabFragment = false;
|
|
|
|
|
|
|
|
MCDataFragment *getStringTableFragment();
|
|
|
|
|
|
|
|
/// Get a string table offset.
|
|
|
|
unsigned getStringTableOffset(StringRef S);
|
|
|
|
|
2017-09-19 20:14:45 +02:00
|
|
|
struct FileInfo {
|
|
|
|
unsigned StringTableOffset;
|
|
|
|
|
|
|
|
// Indicates if this FileInfo corresponds to an actual file, or hasn't been
|
|
|
|
// set yet.
|
|
|
|
bool Assigned = false;
|
|
|
|
|
|
|
|
uint8_t ChecksumKind;
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> Checksum;
|
|
|
|
|
|
|
|
// Checksum offset stored as a symbol because it might be requested
|
|
|
|
// before it has been calculated, so a fixup may be needed.
|
|
|
|
MCSymbol *ChecksumTableOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Array storing added file information.
|
|
|
|
SmallVector<FileInfo, 4> Files;
|
2016-01-29 01:49:42 +01:00
|
|
|
|
2016-01-29 20:24:12 +01:00
|
|
|
/// The offset of the first and last .cv_loc directive for a given function
|
|
|
|
/// id.
|
2016-01-29 20:38:03 +01:00
|
|
|
std::map<unsigned, std::pair<size_t, size_t>> MCCVLineStartStop;
|
2016-01-29 20:24:12 +01:00
|
|
|
|
2018-08-29 01:25:59 +02:00
|
|
|
/// A collection of MCCVLoc for each section.
|
|
|
|
std::vector<MCCVLoc> MCCVLines;
|
2016-09-07 18:15:31 +02:00
|
|
|
|
|
|
|
/// All known functions and inlined call sites, indexed by function id.
|
|
|
|
std::vector<MCCVFunctionInfo> Functions;
|
2017-09-19 20:14:45 +02:00
|
|
|
|
|
|
|
/// Indicate whether we have already laid out the checksum table addresses or
|
|
|
|
/// not.
|
|
|
|
bool ChecksumOffsetsAssigned = false;
|
2016-01-29 01:49:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
#endif
|