RecordName.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //===- RecordName.cpp ----------------------------------------- *- 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. #include "llvm/DebugInfo/CodeView/RecordName.h"
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h"
  12. #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
  13. #include "llvm/DebugInfo/CodeView/SymbolRecordMapping.h"
  14. #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
  15. #include "llvm/Support/FormatVariadic.h"
  16. using namespace llvm;
  17. using namespace llvm::codeview;
  18. namespace {
  19. class TypeNameComputer : public TypeVisitorCallbacks {
  20. /// The type collection. Used to calculate names of nested types.
  21. TypeCollection &Types;
  22. TypeIndex CurrentTypeIndex = TypeIndex::None();
  23. /// Name of the current type. Only valid before visitTypeEnd.
  24. SmallString<256> Name;
  25. public:
  26. explicit TypeNameComputer(TypeCollection &Types) : Types(Types) {}
  27. StringRef name() const { return Name; }
  28. /// Paired begin/end actions for all types. Receives all record data,
  29. /// including the fixed-length record prefix.
  30. Error visitTypeBegin(CVType &Record) override;
  31. Error visitTypeBegin(CVType &Record, TypeIndex Index) override;
  32. Error visitTypeEnd(CVType &Record) override;
  33. #define TYPE_RECORD(EnumName, EnumVal, Name) \
  34. Error visitKnownRecord(CVType &CVR, Name##Record &Record) override;
  35. #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
  36. #define MEMBER_RECORD(EnumName, EnumVal, Name)
  37. #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
  38. };
  39. } // namespace
  40. Error TypeNameComputer::visitTypeBegin(CVType &Record) {
  41. llvm_unreachable("Must call visitTypeBegin with a TypeIndex!");
  42. return Error::success();
  43. }
  44. Error TypeNameComputer::visitTypeBegin(CVType &Record, TypeIndex Index) {
  45. // Reset Name to the empty string. If the visitor sets it, we know it.
  46. Name = "";
  47. CurrentTypeIndex = Index;
  48. return Error::success();
  49. }
  50. Error TypeNameComputer::visitTypeEnd(CVType &CVR) { return Error::success(); }
  51. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  52. FieldListRecord &FieldList) {
  53. Name = "<field list>";
  54. return Error::success();
  55. }
  56. Error TypeNameComputer::visitKnownRecord(CVRecord<TypeLeafKind> &CVR,
  57. StringIdRecord &String) {
  58. Name = String.getString();
  59. return Error::success();
  60. }
  61. Error TypeNameComputer::visitKnownRecord(CVType &CVR, ArgListRecord &Args) {
  62. auto Indices = Args.getIndices();
  63. uint32_t Size = Indices.size();
  64. Name = "(";
  65. for (uint32_t I = 0; I < Size; ++I) {
  66. if (Indices[I] < CurrentTypeIndex)
  67. Name.append(Types.getTypeName(Indices[I]));
  68. else
  69. Name.append("<unknown 0x" + utohexstr(Indices[I].getIndex()) + ">");
  70. if (I + 1 != Size)
  71. Name.append(", ");
  72. }
  73. Name.push_back(')');
  74. return Error::success();
  75. }
  76. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  77. StringListRecord &Strings) {
  78. auto Indices = Strings.getIndices();
  79. uint32_t Size = Indices.size();
  80. Name = "\"";
  81. for (uint32_t I = 0; I < Size; ++I) {
  82. Name.append(Types.getTypeName(Indices[I]));
  83. if (I + 1 != Size)
  84. Name.append("\" \"");
  85. }
  86. Name.push_back('\"');
  87. return Error::success();
  88. }
  89. Error TypeNameComputer::visitKnownRecord(CVType &CVR, ClassRecord &Class) {
  90. Name = Class.getName();
  91. return Error::success();
  92. }
  93. Error TypeNameComputer::visitKnownRecord(CVType &CVR, UnionRecord &Union) {
  94. Name = Union.getName();
  95. return Error::success();
  96. }
  97. Error TypeNameComputer::visitKnownRecord(CVType &CVR, EnumRecord &Enum) {
  98. Name = Enum.getName();
  99. return Error::success();
  100. }
  101. Error TypeNameComputer::visitKnownRecord(CVType &CVR, ArrayRecord &AT) {
  102. Name = AT.getName();
  103. return Error::success();
  104. }
  105. Error TypeNameComputer::visitKnownRecord(CVType &CVR, VFTableRecord &VFT) {
  106. Name = VFT.getName();
  107. return Error::success();
  108. }
  109. Error TypeNameComputer::visitKnownRecord(CVType &CVR, MemberFuncIdRecord &Id) {
  110. Name = Id.getName();
  111. return Error::success();
  112. }
  113. Error TypeNameComputer::visitKnownRecord(CVType &CVR, ProcedureRecord &Proc) {
  114. StringRef Ret = Types.getTypeName(Proc.getReturnType());
  115. StringRef Params = Types.getTypeName(Proc.getArgumentList());
  116. Name = formatv("{0} {1}", Ret, Params).sstr<256>();
  117. return Error::success();
  118. }
  119. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  120. MemberFunctionRecord &MF) {
  121. StringRef Ret = Types.getTypeName(MF.getReturnType());
  122. StringRef Class = Types.getTypeName(MF.getClassType());
  123. StringRef Params = Types.getTypeName(MF.getArgumentList());
  124. Name = formatv("{0} {1}::{2}", Ret, Class, Params).sstr<256>();
  125. return Error::success();
  126. }
  127. Error TypeNameComputer::visitKnownRecord(CVType &CVR, FuncIdRecord &Func) {
  128. Name = Func.getName();
  129. return Error::success();
  130. }
  131. Error TypeNameComputer::visitKnownRecord(CVType &CVR, TypeServer2Record &TS) {
  132. Name = TS.getName();
  133. return Error::success();
  134. }
  135. Error TypeNameComputer::visitKnownRecord(CVType &CVR, PointerRecord &Ptr) {
  136. if (Ptr.isPointerToMember()) {
  137. const MemberPointerInfo &MI = Ptr.getMemberInfo();
  138. StringRef Pointee = Types.getTypeName(Ptr.getReferentType());
  139. StringRef Class = Types.getTypeName(MI.getContainingType());
  140. Name = formatv("{0} {1}::*", Pointee, Class);
  141. } else {
  142. Name.append(Types.getTypeName(Ptr.getReferentType()));
  143. if (Ptr.getMode() == PointerMode::LValueReference)
  144. Name.append("&");
  145. else if (Ptr.getMode() == PointerMode::RValueReference)
  146. Name.append("&&");
  147. else if (Ptr.getMode() == PointerMode::Pointer)
  148. Name.append("*");
  149. // Qualifiers in pointer records apply to the pointer, not the pointee, so
  150. // they go on the right.
  151. if (Ptr.isConst())
  152. Name.append(" const");
  153. if (Ptr.isVolatile())
  154. Name.append(" volatile");
  155. if (Ptr.isUnaligned())
  156. Name.append(" __unaligned");
  157. if (Ptr.isRestrict())
  158. Name.append(" __restrict");
  159. }
  160. return Error::success();
  161. }
  162. Error TypeNameComputer::visitKnownRecord(CVType &CVR, ModifierRecord &Mod) {
  163. uint16_t Mods = static_cast<uint16_t>(Mod.getModifiers());
  164. if (Mods & uint16_t(ModifierOptions::Const))
  165. Name.append("const ");
  166. if (Mods & uint16_t(ModifierOptions::Volatile))
  167. Name.append("volatile ");
  168. if (Mods & uint16_t(ModifierOptions::Unaligned))
  169. Name.append("__unaligned ");
  170. Name.append(Types.getTypeName(Mod.getModifiedType()));
  171. return Error::success();
  172. }
  173. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  174. VFTableShapeRecord &Shape) {
  175. Name = formatv("<vftable {0} methods>", Shape.getEntryCount());
  176. return Error::success();
  177. }
  178. Error TypeNameComputer::visitKnownRecord(
  179. CVType &CVR, UdtModSourceLineRecord &ModSourceLine) {
  180. return Error::success();
  181. }
  182. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  183. UdtSourceLineRecord &SourceLine) {
  184. return Error::success();
  185. }
  186. Error TypeNameComputer::visitKnownRecord(CVType &CVR, BitFieldRecord &BF) {
  187. return Error::success();
  188. }
  189. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  190. MethodOverloadListRecord &Overloads) {
  191. return Error::success();
  192. }
  193. Error TypeNameComputer::visitKnownRecord(CVType &CVR, BuildInfoRecord &BI) {
  194. return Error::success();
  195. }
  196. Error TypeNameComputer::visitKnownRecord(CVType &CVR, LabelRecord &R) {
  197. return Error::success();
  198. }
  199. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  200. PrecompRecord &Precomp) {
  201. return Error::success();
  202. }
  203. Error TypeNameComputer::visitKnownRecord(CVType &CVR,
  204. EndPrecompRecord &EndPrecomp) {
  205. return Error::success();
  206. }
  207. std::string llvm::codeview::computeTypeName(TypeCollection &Types,
  208. TypeIndex Index) {
  209. TypeNameComputer Computer(Types);
  210. CVType Record = Types.getType(Index);
  211. if (auto EC = visitTypeRecord(Record, Index, Computer)) {
  212. consumeError(std::move(EC));
  213. return "<unknown UDT>";
  214. }
  215. return std::string(Computer.name());
  216. }
  217. static int getSymbolNameOffset(CVSymbol Sym) {
  218. switch (Sym.kind()) {
  219. // See ProcSym
  220. case SymbolKind::S_GPROC32:
  221. case SymbolKind::S_LPROC32:
  222. case SymbolKind::S_GPROC32_ID:
  223. case SymbolKind::S_LPROC32_ID:
  224. case SymbolKind::S_LPROC32_DPC:
  225. case SymbolKind::S_LPROC32_DPC_ID:
  226. return 35;
  227. // See Thunk32Sym
  228. case SymbolKind::S_THUNK32:
  229. return 21;
  230. // See SectionSym
  231. case SymbolKind::S_SECTION:
  232. return 16;
  233. // See CoffGroupSym
  234. case SymbolKind::S_COFFGROUP:
  235. return 14;
  236. // See PublicSym32, FileStaticSym, RegRelativeSym, DataSym, ThreadLocalDataSym
  237. case SymbolKind::S_PUB32:
  238. case SymbolKind::S_FILESTATIC:
  239. case SymbolKind::S_REGREL32:
  240. case SymbolKind::S_GDATA32:
  241. case SymbolKind::S_LDATA32:
  242. case SymbolKind::S_LMANDATA:
  243. case SymbolKind::S_GMANDATA:
  244. case SymbolKind::S_LTHREAD32:
  245. case SymbolKind::S_GTHREAD32:
  246. case SymbolKind::S_PROCREF:
  247. case SymbolKind::S_LPROCREF:
  248. return 10;
  249. // See RegisterSym and LocalSym
  250. case SymbolKind::S_REGISTER:
  251. case SymbolKind::S_LOCAL:
  252. return 6;
  253. // See BlockSym
  254. case SymbolKind::S_BLOCK32:
  255. return 18;
  256. // See LabelSym
  257. case SymbolKind::S_LABEL32:
  258. return 7;
  259. // See ObjNameSym, ExportSym, and UDTSym
  260. case SymbolKind::S_OBJNAME:
  261. case SymbolKind::S_EXPORT:
  262. case SymbolKind::S_UDT:
  263. return 4;
  264. // See BPRelativeSym
  265. case SymbolKind::S_BPREL32:
  266. return 8;
  267. // See UsingNamespaceSym
  268. case SymbolKind::S_UNAMESPACE:
  269. return 0;
  270. default:
  271. return -1;
  272. }
  273. }
  274. StringRef llvm::codeview::getSymbolName(CVSymbol Sym) {
  275. if (Sym.kind() == SymbolKind::S_CONSTANT) {
  276. // S_CONSTANT is preceded by an APSInt, which has a variable length. So we
  277. // have to do a full deserialization.
  278. BinaryStreamReader Reader(Sym.content(), llvm::support::little);
  279. // The container doesn't matter for single records.
  280. SymbolRecordMapping Mapping(Reader, CodeViewContainer::ObjectFile);
  281. ConstantSym Const(SymbolKind::S_CONSTANT);
  282. cantFail(Mapping.visitSymbolBegin(Sym));
  283. cantFail(Mapping.visitKnownRecord(Sym, Const));
  284. cantFail(Mapping.visitSymbolEnd(Sym));
  285. return Const.Name;
  286. }
  287. int Offset = getSymbolNameOffset(Sym);
  288. if (Offset == -1)
  289. return StringRef();
  290. StringRef StringData = toStringRef(Sym.content()).drop_front(Offset);
  291. return StringData.split('\0').first;
  292. }