MCCodeView.h 9.1 KB

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