MCCodeView.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //===- MCCodeView.h - Machine Code CodeView support -------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Holds state from .cv_file and .cv_loc directives for later emission.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_MC_MCCODEVIEW_H
  13. #define LLVM_MC_MCCODEVIEW_H
  14. #include "llvm/ADT/StringMap.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/MC/MCFragment.h"
  17. #include "llvm/MC/MCObjectStreamer.h"
  18. #include <map>
  19. #include <vector>
  20. namespace llvm {
  21. class MCContext;
  22. class MCObjectStreamer;
  23. class MCStreamer;
  24. class CodeViewContext;
  25. /// Instances of this class represent the information from a
  26. /// .cv_loc directive.
  27. class MCCVLoc {
  28. const MCSymbol *Label = nullptr;
  29. uint32_t FunctionId;
  30. uint32_t FileNum;
  31. uint32_t Line;
  32. uint16_t Column;
  33. uint16_t PrologueEnd : 1;
  34. uint16_t IsStmt : 1;
  35. private: // CodeViewContext manages these
  36. friend class CodeViewContext;
  37. MCCVLoc(const MCSymbol *Label, unsigned functionid, unsigned fileNum,
  38. unsigned line, unsigned column, bool prologueend, bool isstmt)
  39. : Label(Label), FunctionId(functionid), FileNum(fileNum), Line(line),
  40. Column(column), PrologueEnd(prologueend), IsStmt(isstmt) {}
  41. // Allow the default copy constructor and assignment operator to be used
  42. // for an MCCVLoc object.
  43. public:
  44. const MCSymbol *getLabel() const { return Label; }
  45. unsigned getFunctionId() const { return FunctionId; }
  46. /// Get the FileNum of this MCCVLoc.
  47. unsigned getFileNum() const { return FileNum; }
  48. /// Get the Line of this MCCVLoc.
  49. unsigned getLine() const { return Line; }
  50. /// Get the Column of this MCCVLoc.
  51. unsigned getColumn() const { return Column; }
  52. bool isPrologueEnd() const { return PrologueEnd; }
  53. bool isStmt() const { return IsStmt; }
  54. void setLabel(const MCSymbol *L) { Label = L; }
  55. void setFunctionId(unsigned FID) { FunctionId = FID; }
  56. /// Set the FileNum of this MCCVLoc.
  57. void setFileNum(unsigned fileNum) { FileNum = fileNum; }
  58. /// Set the Line of this MCCVLoc.
  59. void setLine(unsigned line) { Line = line; }
  60. /// Set the Column of this MCCVLoc.
  61. void setColumn(unsigned column) {
  62. assert(column <= UINT16_MAX);
  63. Column = column;
  64. }
  65. void setPrologueEnd(bool PE) { PrologueEnd = PE; }
  66. void setIsStmt(bool IS) { IsStmt = IS; }
  67. };
  68. /// Information describing a function or inlined call site introduced by
  69. /// .cv_func_id or .cv_inline_site_id. Accumulates information from .cv_loc
  70. /// directives used with this function's id or the id of an inlined call site
  71. /// within this function or inlined call site.
  72. struct MCCVFunctionInfo {
  73. /// If this represents an inlined call site, then ParentFuncIdPlusOne will be
  74. /// the parent function id plus one. If this represents a normal function,
  75. /// then there is no parent, and ParentFuncIdPlusOne will be FunctionSentinel.
  76. /// If this struct is an unallocated slot in the function info vector, then
  77. /// ParentFuncIdPlusOne will be zero.
  78. unsigned ParentFuncIdPlusOne = 0;
  79. enum : unsigned { FunctionSentinel = ~0U };
  80. struct LineInfo {
  81. unsigned File;
  82. unsigned Line;
  83. unsigned Col;
  84. };
  85. LineInfo InlinedAt;
  86. /// The section of the first .cv_loc directive used for this function, or null
  87. /// if none has been seen yet.
  88. MCSection *Section = nullptr;
  89. /// Map from inlined call site id to the inlined at location to use for that
  90. /// call site. Call chains are collapsed, so for the call chain 'f -> g -> h',
  91. /// the InlinedAtMap of 'f' will contain entries for 'g' and 'h' that both
  92. /// list the line info for the 'g' call site.
  93. DenseMap<unsigned, LineInfo> InlinedAtMap;
  94. /// Returns true if this is function info has not yet been used in a
  95. /// .cv_func_id or .cv_inline_site_id directive.
  96. bool isUnallocatedFunctionInfo() const { return ParentFuncIdPlusOne == 0; }
  97. /// Returns true if this represents an inlined call site, meaning
  98. /// ParentFuncIdPlusOne is neither zero nor ~0U.
  99. bool isInlinedCallSite() const {
  100. return !isUnallocatedFunctionInfo() &&
  101. ParentFuncIdPlusOne != FunctionSentinel;
  102. }
  103. unsigned getParentFuncId() const {
  104. assert(isInlinedCallSite());
  105. return ParentFuncIdPlusOne - 1;
  106. }
  107. };
  108. /// Holds state from .cv_file and .cv_loc directives for later emission.
  109. class CodeViewContext {
  110. public:
  111. CodeViewContext();
  112. ~CodeViewContext();
  113. bool isValidFileNumber(unsigned FileNumber) const;
  114. bool addFile(MCStreamer &OS, unsigned FileNumber, StringRef Filename,
  115. ArrayRef<uint8_t> ChecksumBytes, uint8_t ChecksumKind);
  116. /// Records the function id of a normal function. Returns false if the
  117. /// function id has already been used, and true otherwise.
  118. bool recordFunctionId(unsigned FuncId);
  119. /// Records the function id of an inlined call site. Records the "inlined at"
  120. /// location info of the call site, including what function or inlined call
  121. /// site it was inlined into. Returns false if the function id has already
  122. /// been used, and true otherwise.
  123. bool recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc,
  124. unsigned IAFile, unsigned IALine,
  125. unsigned IACol);
  126. /// Retreive the function info if this is a valid function id, or nullptr.
  127. MCCVFunctionInfo *getCVFunctionInfo(unsigned FuncId);
  128. /// Saves the information from the currently parsed .cv_loc directive
  129. /// and sets CVLocSeen. When the next instruction is assembled an entry
  130. /// in the line number table with this information and the address of the
  131. /// instruction will be created.
  132. void recordCVLoc(MCContext &Ctx, const MCSymbol *Label, unsigned FunctionId,
  133. unsigned FileNo, unsigned Line, unsigned Column,
  134. bool PrologueEnd, bool IsStmt);
  135. /// Add a line entry.
  136. void addLineEntry(const MCCVLoc &LineEntry);
  137. std::vector<MCCVLoc> getFunctionLineEntries(unsigned FuncId);
  138. std::pair<size_t, size_t> getLineExtent(unsigned FuncId);
  139. ArrayRef<MCCVLoc> getLinesForExtent(size_t L, size_t R);
  140. /// Emits a line table substream.
  141. void emitLineTableForFunction(MCObjectStreamer &OS, unsigned FuncId,
  142. const MCSymbol *FuncBegin,
  143. const MCSymbol *FuncEnd);
  144. void emitInlineLineTableForFunction(MCObjectStreamer &OS,
  145. unsigned PrimaryFunctionId,
  146. unsigned SourceFileId,
  147. unsigned SourceLineNum,
  148. const MCSymbol *FnStartSym,
  149. const MCSymbol *FnEndSym);
  150. /// Encodes the binary annotations once we have a layout.
  151. void encodeInlineLineTable(MCAsmLayout &Layout,
  152. MCCVInlineLineTableFragment &F);
  153. MCFragment *
  154. emitDefRange(MCObjectStreamer &OS,
  155. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  156. StringRef FixedSizePortion);
  157. void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F);
  158. /// Emits the string table substream.
  159. void emitStringTable(MCObjectStreamer &OS);
  160. /// Emits the file checksum substream.
  161. void emitFileChecksums(MCObjectStreamer &OS);
  162. /// Emits the offset into the checksum table of the given file number.
  163. void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo);
  164. /// Add something to the string table. Returns the final string as well as
  165. /// offset into the string table.
  166. std::pair<StringRef, unsigned> addToStringTable(StringRef S);
  167. private:
  168. /// Map from string to string table offset.
  169. StringMap<unsigned> StringTable;
  170. /// The fragment that ultimately holds our strings.
  171. MCDataFragment *StrTabFragment = nullptr;
  172. bool InsertedStrTabFragment = false;
  173. MCDataFragment *getStringTableFragment();
  174. /// Get a string table offset.
  175. unsigned getStringTableOffset(StringRef S);
  176. struct FileInfo {
  177. unsigned StringTableOffset;
  178. // Indicates if this FileInfo corresponds to an actual file, or hasn't been
  179. // set yet.
  180. bool Assigned = false;
  181. uint8_t ChecksumKind;
  182. ArrayRef<uint8_t> Checksum;
  183. // Checksum offset stored as a symbol because it might be requested
  184. // before it has been calculated, so a fixup may be needed.
  185. MCSymbol *ChecksumTableOffset;
  186. };
  187. /// Array storing added file information.
  188. SmallVector<FileInfo, 4> Files;
  189. /// The offset of the first and last .cv_loc directive for a given function
  190. /// id.
  191. std::map<unsigned, std::pair<size_t, size_t>> MCCVLineStartStop;
  192. /// A collection of MCCVLoc for each section.
  193. std::vector<MCCVLoc> MCCVLines;
  194. /// All known functions and inlined call sites, indexed by function id.
  195. std::vector<MCCVFunctionInfo> Functions;
  196. /// Indicate whether we have already laid out the checksum table addresses or
  197. /// not.
  198. bool ChecksumOffsetsAssigned = false;
  199. };
  200. } // end namespace llvm
  201. #endif