CodeViewYAMLDebugSections.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. //===- CodeViewYAMLDebugSections.cpp - CodeView YAMLIO debug sections -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines classes for handling the YAML representation of CodeView
  11. // Debug Info.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
  15. #include "llvm/ADT/StringExtras.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/DebugInfo/CodeView/CodeViewError.h"
  18. #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
  19. #include "llvm/DebugInfo/CodeView/DebugCrossExSubsection.h"
  20. #include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h"
  21. #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
  22. #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
  23. #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
  24. #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
  25. #include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
  26. #include "llvm/DebugInfo/CodeView/DebugSymbolRVASubsection.h"
  27. #include "llvm/DebugInfo/CodeView/DebugSymbolsSubsection.h"
  28. #include "llvm/DebugInfo/CodeView/EnumTables.h"
  29. #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
  30. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  31. #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
  32. #include "llvm/ObjectYAML/CodeViewYAMLSymbols.h"
  33. #include "llvm/Support/BinaryStreamWriter.h"
  34. using namespace llvm;
  35. using namespace llvm::codeview;
  36. using namespace llvm::CodeViewYAML;
  37. using namespace llvm::CodeViewYAML::detail;
  38. using namespace llvm::yaml;
  39. LLVM_YAML_IS_SEQUENCE_VECTOR(SourceFileChecksumEntry)
  40. LLVM_YAML_IS_SEQUENCE_VECTOR(SourceLineEntry)
  41. LLVM_YAML_IS_SEQUENCE_VECTOR(SourceColumnEntry)
  42. LLVM_YAML_IS_SEQUENCE_VECTOR(SourceLineBlock)
  43. LLVM_YAML_IS_SEQUENCE_VECTOR(SourceLineInfo)
  44. LLVM_YAML_IS_SEQUENCE_VECTOR(InlineeSite)
  45. LLVM_YAML_IS_SEQUENCE_VECTOR(InlineeInfo)
  46. LLVM_YAML_IS_SEQUENCE_VECTOR(CrossModuleExport)
  47. LLVM_YAML_IS_SEQUENCE_VECTOR(YAMLCrossModuleImport)
  48. LLVM_YAML_IS_SEQUENCE_VECTOR(YAMLFrameData)
  49. LLVM_YAML_DECLARE_SCALAR_TRAITS(HexFormattedString, false)
  50. LLVM_YAML_DECLARE_ENUM_TRAITS(DebugSubsectionKind)
  51. LLVM_YAML_DECLARE_ENUM_TRAITS(FileChecksumKind)
  52. LLVM_YAML_DECLARE_BITSET_TRAITS(LineFlags)
  53. LLVM_YAML_DECLARE_MAPPING_TRAITS(CrossModuleExport)
  54. LLVM_YAML_DECLARE_MAPPING_TRAITS(YAMLFrameData)
  55. LLVM_YAML_DECLARE_MAPPING_TRAITS(YAMLCrossModuleImport)
  56. LLVM_YAML_DECLARE_MAPPING_TRAITS(CrossModuleImportItem)
  57. LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceLineEntry)
  58. LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceColumnEntry)
  59. LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceFileChecksumEntry)
  60. LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceLineBlock)
  61. LLVM_YAML_DECLARE_MAPPING_TRAITS(InlineeSite)
  62. namespace llvm {
  63. namespace CodeViewYAML {
  64. namespace detail {
  65. struct YAMLSubsectionBase {
  66. explicit YAMLSubsectionBase(DebugSubsectionKind Kind) : Kind(Kind) {}
  67. DebugSubsectionKind Kind;
  68. virtual ~YAMLSubsectionBase() {}
  69. virtual void map(IO &IO) = 0;
  70. virtual std::shared_ptr<DebugSubsection>
  71. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  72. const codeview::StringsAndChecksums &SC) const = 0;
  73. };
  74. }
  75. }
  76. }
  77. namespace {
  78. struct YAMLChecksumsSubsection : public YAMLSubsectionBase {
  79. YAMLChecksumsSubsection()
  80. : YAMLSubsectionBase(DebugSubsectionKind::FileChecksums) {}
  81. void map(IO &IO) override;
  82. std::shared_ptr<DebugSubsection>
  83. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  84. const codeview::StringsAndChecksums &SC) const override;
  85. static Expected<std::shared_ptr<YAMLChecksumsSubsection>>
  86. fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
  87. const DebugChecksumsSubsectionRef &FC);
  88. std::vector<SourceFileChecksumEntry> Checksums;
  89. };
  90. struct YAMLLinesSubsection : public YAMLSubsectionBase {
  91. YAMLLinesSubsection() : YAMLSubsectionBase(DebugSubsectionKind::Lines) {}
  92. void map(IO &IO) override;
  93. std::shared_ptr<DebugSubsection>
  94. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  95. const codeview::StringsAndChecksums &SC) const override;
  96. static Expected<std::shared_ptr<YAMLLinesSubsection>>
  97. fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
  98. const DebugChecksumsSubsectionRef &Checksums,
  99. const DebugLinesSubsectionRef &Lines);
  100. SourceLineInfo Lines;
  101. };
  102. struct YAMLInlineeLinesSubsection : public YAMLSubsectionBase {
  103. YAMLInlineeLinesSubsection()
  104. : YAMLSubsectionBase(DebugSubsectionKind::InlineeLines) {}
  105. void map(IO &IO) override;
  106. std::shared_ptr<DebugSubsection>
  107. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  108. const codeview::StringsAndChecksums &SC) const override;
  109. static Expected<std::shared_ptr<YAMLInlineeLinesSubsection>>
  110. fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
  111. const DebugChecksumsSubsectionRef &Checksums,
  112. const DebugInlineeLinesSubsectionRef &Lines);
  113. InlineeInfo InlineeLines;
  114. };
  115. struct YAMLCrossModuleExportsSubsection : public YAMLSubsectionBase {
  116. YAMLCrossModuleExportsSubsection()
  117. : YAMLSubsectionBase(DebugSubsectionKind::CrossScopeExports) {}
  118. void map(IO &IO) override;
  119. std::shared_ptr<DebugSubsection>
  120. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  121. const codeview::StringsAndChecksums &SC) const override;
  122. static Expected<std::shared_ptr<YAMLCrossModuleExportsSubsection>>
  123. fromCodeViewSubsection(const DebugCrossModuleExportsSubsectionRef &Exports);
  124. std::vector<CrossModuleExport> Exports;
  125. };
  126. struct YAMLCrossModuleImportsSubsection : public YAMLSubsectionBase {
  127. YAMLCrossModuleImportsSubsection()
  128. : YAMLSubsectionBase(DebugSubsectionKind::CrossScopeImports) {}
  129. void map(IO &IO) override;
  130. std::shared_ptr<DebugSubsection>
  131. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  132. const codeview::StringsAndChecksums &SC) const override;
  133. static Expected<std::shared_ptr<YAMLCrossModuleImportsSubsection>>
  134. fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
  135. const DebugCrossModuleImportsSubsectionRef &Imports);
  136. std::vector<YAMLCrossModuleImport> Imports;
  137. };
  138. struct YAMLSymbolsSubsection : public YAMLSubsectionBase {
  139. YAMLSymbolsSubsection() : YAMLSubsectionBase(DebugSubsectionKind::Symbols) {}
  140. void map(IO &IO) override;
  141. std::shared_ptr<DebugSubsection>
  142. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  143. const codeview::StringsAndChecksums &SC) const override;
  144. static Expected<std::shared_ptr<YAMLSymbolsSubsection>>
  145. fromCodeViewSubsection(const DebugSymbolsSubsectionRef &Symbols);
  146. std::vector<CodeViewYAML::SymbolRecord> Symbols;
  147. };
  148. struct YAMLStringTableSubsection : public YAMLSubsectionBase {
  149. YAMLStringTableSubsection()
  150. : YAMLSubsectionBase(DebugSubsectionKind::StringTable) {}
  151. void map(IO &IO) override;
  152. std::shared_ptr<DebugSubsection>
  153. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  154. const codeview::StringsAndChecksums &SC) const override;
  155. static Expected<std::shared_ptr<YAMLStringTableSubsection>>
  156. fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings);
  157. std::vector<StringRef> Strings;
  158. };
  159. struct YAMLFrameDataSubsection : public YAMLSubsectionBase {
  160. YAMLFrameDataSubsection()
  161. : YAMLSubsectionBase(DebugSubsectionKind::FrameData) {}
  162. void map(IO &IO) override;
  163. std::shared_ptr<DebugSubsection>
  164. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  165. const codeview::StringsAndChecksums &SC) const override;
  166. static Expected<std::shared_ptr<YAMLFrameDataSubsection>>
  167. fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
  168. const DebugFrameDataSubsectionRef &Frames);
  169. std::vector<YAMLFrameData> Frames;
  170. };
  171. struct YAMLCoffSymbolRVASubsection : public YAMLSubsectionBase {
  172. YAMLCoffSymbolRVASubsection()
  173. : YAMLSubsectionBase(DebugSubsectionKind::CoffSymbolRVA) {}
  174. void map(IO &IO) override;
  175. std::shared_ptr<DebugSubsection>
  176. toCodeViewSubsection(BumpPtrAllocator &Allocator,
  177. const codeview::StringsAndChecksums &SC) const override;
  178. static Expected<std::shared_ptr<YAMLCoffSymbolRVASubsection>>
  179. fromCodeViewSubsection(const DebugSymbolRVASubsectionRef &RVAs);
  180. std::vector<uint32_t> RVAs;
  181. };
  182. }
  183. void ScalarBitSetTraits<LineFlags>::bitset(IO &io, LineFlags &Flags) {
  184. io.bitSetCase(Flags, "HasColumnInfo", LF_HaveColumns);
  185. io.enumFallback<Hex16>(Flags);
  186. }
  187. void ScalarEnumerationTraits<FileChecksumKind>::enumeration(
  188. IO &io, FileChecksumKind &Kind) {
  189. io.enumCase(Kind, "None", FileChecksumKind::None);
  190. io.enumCase(Kind, "MD5", FileChecksumKind::MD5);
  191. io.enumCase(Kind, "SHA1", FileChecksumKind::SHA1);
  192. io.enumCase(Kind, "SHA256", FileChecksumKind::SHA256);
  193. }
  194. void ScalarTraits<HexFormattedString>::output(const HexFormattedString &Value,
  195. void *ctx, raw_ostream &Out) {
  196. StringRef Bytes(reinterpret_cast<const char *>(Value.Bytes.data()),
  197. Value.Bytes.size());
  198. Out << toHex(Bytes);
  199. }
  200. StringRef ScalarTraits<HexFormattedString>::input(StringRef Scalar, void *ctxt,
  201. HexFormattedString &Value) {
  202. std::string H = fromHex(Scalar);
  203. Value.Bytes.assign(H.begin(), H.end());
  204. return StringRef();
  205. }
  206. void MappingTraits<SourceLineEntry>::mapping(IO &IO, SourceLineEntry &Obj) {
  207. IO.mapRequired("Offset", Obj.Offset);
  208. IO.mapRequired("LineStart", Obj.LineStart);
  209. IO.mapRequired("IsStatement", Obj.IsStatement);
  210. IO.mapRequired("EndDelta", Obj.EndDelta);
  211. }
  212. void MappingTraits<SourceColumnEntry>::mapping(IO &IO, SourceColumnEntry &Obj) {
  213. IO.mapRequired("StartColumn", Obj.StartColumn);
  214. IO.mapRequired("EndColumn", Obj.EndColumn);
  215. }
  216. void MappingTraits<SourceLineBlock>::mapping(IO &IO, SourceLineBlock &Obj) {
  217. IO.mapRequired("FileName", Obj.FileName);
  218. IO.mapRequired("Lines", Obj.Lines);
  219. IO.mapRequired("Columns", Obj.Columns);
  220. }
  221. void MappingTraits<CrossModuleExport>::mapping(IO &IO, CrossModuleExport &Obj) {
  222. IO.mapRequired("LocalId", Obj.Local);
  223. IO.mapRequired("GlobalId", Obj.Global);
  224. }
  225. void MappingTraits<YAMLCrossModuleImport>::mapping(IO &IO,
  226. YAMLCrossModuleImport &Obj) {
  227. IO.mapRequired("Module", Obj.ModuleName);
  228. IO.mapRequired("Imports", Obj.ImportIds);
  229. }
  230. void MappingTraits<SourceFileChecksumEntry>::mapping(
  231. IO &IO, SourceFileChecksumEntry &Obj) {
  232. IO.mapRequired("FileName", Obj.FileName);
  233. IO.mapRequired("Kind", Obj.Kind);
  234. IO.mapRequired("Checksum", Obj.ChecksumBytes);
  235. }
  236. void MappingTraits<InlineeSite>::mapping(IO &IO, InlineeSite &Obj) {
  237. IO.mapRequired("FileName", Obj.FileName);
  238. IO.mapRequired("LineNum", Obj.SourceLineNum);
  239. IO.mapRequired("Inlinee", Obj.Inlinee);
  240. IO.mapOptional("ExtraFiles", Obj.ExtraFiles);
  241. }
  242. void MappingTraits<YAMLFrameData>::mapping(IO &IO, YAMLFrameData &Obj) {
  243. IO.mapRequired("CodeSize", Obj.CodeSize);
  244. IO.mapRequired("FrameFunc", Obj.FrameFunc);
  245. IO.mapRequired("LocalSize", Obj.LocalSize);
  246. IO.mapOptional("MaxStackSize", Obj.MaxStackSize);
  247. IO.mapOptional("ParamsSize", Obj.ParamsSize);
  248. IO.mapOptional("PrologSize", Obj.PrologSize);
  249. IO.mapOptional("RvaStart", Obj.RvaStart);
  250. IO.mapOptional("SavedRegsSize", Obj.SavedRegsSize);
  251. }
  252. void YAMLChecksumsSubsection::map(IO &IO) {
  253. IO.mapTag("!FileChecksums", true);
  254. IO.mapRequired("Checksums", Checksums);
  255. }
  256. void YAMLLinesSubsection::map(IO &IO) {
  257. IO.mapTag("!Lines", true);
  258. IO.mapRequired("CodeSize", Lines.CodeSize);
  259. IO.mapRequired("Flags", Lines.Flags);
  260. IO.mapRequired("RelocOffset", Lines.RelocOffset);
  261. IO.mapRequired("RelocSegment", Lines.RelocSegment);
  262. IO.mapRequired("Blocks", Lines.Blocks);
  263. }
  264. void YAMLInlineeLinesSubsection::map(IO &IO) {
  265. IO.mapTag("!InlineeLines", true);
  266. IO.mapRequired("HasExtraFiles", InlineeLines.HasExtraFiles);
  267. IO.mapRequired("Sites", InlineeLines.Sites);
  268. }
  269. void YAMLCrossModuleExportsSubsection::map(IO &IO) {
  270. IO.mapTag("!CrossModuleExports", true);
  271. IO.mapOptional("Exports", Exports);
  272. }
  273. void YAMLCrossModuleImportsSubsection::map(IO &IO) {
  274. IO.mapTag("!CrossModuleImports", true);
  275. IO.mapOptional("Imports", Imports);
  276. }
  277. void YAMLSymbolsSubsection::map(IO &IO) {
  278. IO.mapTag("!Symbols", true);
  279. IO.mapRequired("Records", Symbols);
  280. }
  281. void YAMLStringTableSubsection::map(IO &IO) {
  282. IO.mapTag("!StringTable", true);
  283. IO.mapRequired("Strings", Strings);
  284. }
  285. void YAMLFrameDataSubsection::map(IO &IO) {
  286. IO.mapTag("!FrameData", true);
  287. IO.mapRequired("Frames", Frames);
  288. }
  289. void YAMLCoffSymbolRVASubsection::map(IO &IO) {
  290. IO.mapTag("!COFFSymbolRVAs", true);
  291. IO.mapRequired("RVAs", RVAs);
  292. }
  293. void MappingTraits<YAMLDebugSubsection>::mapping(
  294. IO &IO, YAMLDebugSubsection &Subsection) {
  295. if (!IO.outputting()) {
  296. if (IO.mapTag("!FileChecksums")) {
  297. auto SS = std::make_shared<YAMLChecksumsSubsection>();
  298. Subsection.Subsection = SS;
  299. } else if (IO.mapTag("!Lines")) {
  300. Subsection.Subsection = std::make_shared<YAMLLinesSubsection>();
  301. } else if (IO.mapTag("!InlineeLines")) {
  302. Subsection.Subsection = std::make_shared<YAMLInlineeLinesSubsection>();
  303. } else if (IO.mapTag("!CrossModuleExports")) {
  304. Subsection.Subsection =
  305. std::make_shared<YAMLCrossModuleExportsSubsection>();
  306. } else if (IO.mapTag("!CrossModuleImports")) {
  307. Subsection.Subsection =
  308. std::make_shared<YAMLCrossModuleImportsSubsection>();
  309. } else if (IO.mapTag("!Symbols")) {
  310. Subsection.Subsection = std::make_shared<YAMLSymbolsSubsection>();
  311. } else if (IO.mapTag("!StringTable")) {
  312. Subsection.Subsection = std::make_shared<YAMLStringTableSubsection>();
  313. } else if (IO.mapTag("!FrameData")) {
  314. Subsection.Subsection = std::make_shared<YAMLFrameDataSubsection>();
  315. } else if (IO.mapTag("!COFFSymbolRVAs")) {
  316. Subsection.Subsection = std::make_shared<YAMLCoffSymbolRVASubsection>();
  317. } else {
  318. llvm_unreachable("Unexpected subsection tag!");
  319. }
  320. }
  321. Subsection.Subsection->map(IO);
  322. }
  323. std::shared_ptr<DebugSubsection> YAMLChecksumsSubsection::toCodeViewSubsection(
  324. BumpPtrAllocator &Allocator,
  325. const codeview::StringsAndChecksums &SC) const {
  326. assert(SC.hasStrings());
  327. auto Result = std::make_shared<DebugChecksumsSubsection>(*SC.strings());
  328. for (const auto &CS : Checksums) {
  329. Result->addChecksum(CS.FileName, CS.Kind, CS.ChecksumBytes.Bytes);
  330. }
  331. return Result;
  332. }
  333. std::shared_ptr<DebugSubsection> YAMLLinesSubsection::toCodeViewSubsection(
  334. BumpPtrAllocator &Allocator,
  335. const codeview::StringsAndChecksums &SC) const {
  336. assert(SC.hasStrings() && SC.hasChecksums());
  337. auto Result =
  338. std::make_shared<DebugLinesSubsection>(*SC.checksums(), *SC.strings());
  339. Result->setCodeSize(Lines.CodeSize);
  340. Result->setRelocationAddress(Lines.RelocSegment, Lines.RelocOffset);
  341. Result->setFlags(Lines.Flags);
  342. for (const auto &LC : Lines.Blocks) {
  343. Result->createBlock(LC.FileName);
  344. if (Result->hasColumnInfo()) {
  345. for (const auto &Item : zip(LC.Lines, LC.Columns)) {
  346. auto &L = std::get<0>(Item);
  347. auto &C = std::get<1>(Item);
  348. uint32_t LE = L.LineStart + L.EndDelta;
  349. Result->addLineAndColumnInfo(L.Offset,
  350. LineInfo(L.LineStart, LE, L.IsStatement),
  351. C.StartColumn, C.EndColumn);
  352. }
  353. } else {
  354. for (const auto &L : LC.Lines) {
  355. uint32_t LE = L.LineStart + L.EndDelta;
  356. Result->addLineInfo(L.Offset, LineInfo(L.LineStart, LE, L.IsStatement));
  357. }
  358. }
  359. }
  360. return Result;
  361. }
  362. std::shared_ptr<DebugSubsection>
  363. YAMLInlineeLinesSubsection::toCodeViewSubsection(
  364. BumpPtrAllocator &Allocator,
  365. const codeview::StringsAndChecksums &SC) const {
  366. assert(SC.hasChecksums());
  367. auto Result = std::make_shared<DebugInlineeLinesSubsection>(
  368. *SC.checksums(), InlineeLines.HasExtraFiles);
  369. for (const auto &Site : InlineeLines.Sites) {
  370. Result->addInlineSite(TypeIndex(Site.Inlinee), Site.FileName,
  371. Site.SourceLineNum);
  372. if (!InlineeLines.HasExtraFiles)
  373. continue;
  374. for (auto EF : Site.ExtraFiles) {
  375. Result->addExtraFile(EF);
  376. }
  377. }
  378. return Result;
  379. }
  380. std::shared_ptr<DebugSubsection>
  381. YAMLCrossModuleExportsSubsection::toCodeViewSubsection(
  382. BumpPtrAllocator &Allocator,
  383. const codeview::StringsAndChecksums &SC) const {
  384. auto Result = std::make_shared<DebugCrossModuleExportsSubsection>();
  385. for (const auto &M : Exports)
  386. Result->addMapping(M.Local, M.Global);
  387. return Result;
  388. }
  389. std::shared_ptr<DebugSubsection>
  390. YAMLCrossModuleImportsSubsection::toCodeViewSubsection(
  391. BumpPtrAllocator &Allocator,
  392. const codeview::StringsAndChecksums &SC) const {
  393. assert(SC.hasStrings());
  394. auto Result =
  395. std::make_shared<DebugCrossModuleImportsSubsection>(*SC.strings());
  396. for (const auto &M : Imports) {
  397. for (const auto Id : M.ImportIds)
  398. Result->addImport(M.ModuleName, Id);
  399. }
  400. return Result;
  401. }
  402. std::shared_ptr<DebugSubsection> YAMLSymbolsSubsection::toCodeViewSubsection(
  403. BumpPtrAllocator &Allocator,
  404. const codeview::StringsAndChecksums &SC) const {
  405. auto Result = std::make_shared<DebugSymbolsSubsection>();
  406. for (const auto &Sym : Symbols)
  407. Result->addSymbol(
  408. Sym.toCodeViewSymbol(Allocator, CodeViewContainer::ObjectFile));
  409. return Result;
  410. }
  411. std::shared_ptr<DebugSubsection>
  412. YAMLStringTableSubsection::toCodeViewSubsection(
  413. BumpPtrAllocator &Allocator,
  414. const codeview::StringsAndChecksums &SC) const {
  415. auto Result = std::make_shared<DebugStringTableSubsection>();
  416. for (const auto &Str : this->Strings)
  417. Result->insert(Str);
  418. return Result;
  419. }
  420. std::shared_ptr<DebugSubsection> YAMLFrameDataSubsection::toCodeViewSubsection(
  421. BumpPtrAllocator &Allocator,
  422. const codeview::StringsAndChecksums &SC) const {
  423. assert(SC.hasStrings());
  424. auto Result = std::make_shared<DebugFrameDataSubsection>();
  425. for (const auto &YF : Frames) {
  426. codeview::FrameData F;
  427. F.CodeSize = YF.CodeSize;
  428. F.Flags = YF.Flags;
  429. F.LocalSize = YF.LocalSize;
  430. F.MaxStackSize = YF.MaxStackSize;
  431. F.ParamsSize = YF.ParamsSize;
  432. F.PrologSize = YF.PrologSize;
  433. F.RvaStart = YF.RvaStart;
  434. F.SavedRegsSize = YF.SavedRegsSize;
  435. F.FrameFunc = SC.strings()->insert(YF.FrameFunc);
  436. Result->addFrameData(F);
  437. }
  438. return Result;
  439. }
  440. std::shared_ptr<DebugSubsection>
  441. YAMLCoffSymbolRVASubsection::toCodeViewSubsection(
  442. BumpPtrAllocator &Allocator,
  443. const codeview::StringsAndChecksums &SC) const {
  444. auto Result = std::make_shared<DebugSymbolRVASubsection>();
  445. for (const auto &RVA : RVAs)
  446. Result->addRVA(RVA);
  447. return Result;
  448. }
  449. static Expected<SourceFileChecksumEntry>
  450. convertOneChecksum(const DebugStringTableSubsectionRef &Strings,
  451. const FileChecksumEntry &CS) {
  452. auto ExpectedString = Strings.getString(CS.FileNameOffset);
  453. if (!ExpectedString)
  454. return ExpectedString.takeError();
  455. SourceFileChecksumEntry Result;
  456. Result.ChecksumBytes.Bytes = CS.Checksum;
  457. Result.Kind = CS.Kind;
  458. Result.FileName = *ExpectedString;
  459. return Result;
  460. }
  461. static Expected<StringRef>
  462. getFileName(const DebugStringTableSubsectionRef &Strings,
  463. const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID) {
  464. auto Iter = Checksums.getArray().at(FileID);
  465. if (Iter == Checksums.getArray().end())
  466. return make_error<CodeViewError>(cv_error_code::no_records);
  467. uint32_t Offset = Iter->FileNameOffset;
  468. return Strings.getString(Offset);
  469. }
  470. Expected<std::shared_ptr<YAMLChecksumsSubsection>>
  471. YAMLChecksumsSubsection::fromCodeViewSubsection(
  472. const DebugStringTableSubsectionRef &Strings,
  473. const DebugChecksumsSubsectionRef &FC) {
  474. auto Result = std::make_shared<YAMLChecksumsSubsection>();
  475. for (const auto &CS : FC) {
  476. auto ConvertedCS = convertOneChecksum(Strings, CS);
  477. if (!ConvertedCS)
  478. return ConvertedCS.takeError();
  479. Result->Checksums.push_back(*ConvertedCS);
  480. }
  481. return Result;
  482. }
  483. Expected<std::shared_ptr<YAMLLinesSubsection>>
  484. YAMLLinesSubsection::fromCodeViewSubsection(
  485. const DebugStringTableSubsectionRef &Strings,
  486. const DebugChecksumsSubsectionRef &Checksums,
  487. const DebugLinesSubsectionRef &Lines) {
  488. auto Result = std::make_shared<YAMLLinesSubsection>();
  489. Result->Lines.CodeSize = Lines.header()->CodeSize;
  490. Result->Lines.RelocOffset = Lines.header()->RelocOffset;
  491. Result->Lines.RelocSegment = Lines.header()->RelocSegment;
  492. Result->Lines.Flags = static_cast<LineFlags>(uint16_t(Lines.header()->Flags));
  493. for (const auto &L : Lines) {
  494. SourceLineBlock Block;
  495. auto EF = getFileName(Strings, Checksums, L.NameIndex);
  496. if (!EF)
  497. return EF.takeError();
  498. Block.FileName = *EF;
  499. if (Lines.hasColumnInfo()) {
  500. for (const auto &C : L.Columns) {
  501. SourceColumnEntry SCE;
  502. SCE.EndColumn = C.EndColumn;
  503. SCE.StartColumn = C.StartColumn;
  504. Block.Columns.push_back(SCE);
  505. }
  506. }
  507. for (const auto &LN : L.LineNumbers) {
  508. SourceLineEntry SLE;
  509. LineInfo LI(LN.Flags);
  510. SLE.Offset = LN.Offset;
  511. SLE.LineStart = LI.getStartLine();
  512. SLE.EndDelta = LI.getLineDelta();
  513. SLE.IsStatement = LI.isStatement();
  514. Block.Lines.push_back(SLE);
  515. }
  516. Result->Lines.Blocks.push_back(Block);
  517. }
  518. return Result;
  519. }
  520. Expected<std::shared_ptr<YAMLInlineeLinesSubsection>>
  521. YAMLInlineeLinesSubsection::fromCodeViewSubsection(
  522. const DebugStringTableSubsectionRef &Strings,
  523. const DebugChecksumsSubsectionRef &Checksums,
  524. const DebugInlineeLinesSubsectionRef &Lines) {
  525. auto Result = std::make_shared<YAMLInlineeLinesSubsection>();
  526. Result->InlineeLines.HasExtraFiles = Lines.hasExtraFiles();
  527. for (const auto &IL : Lines) {
  528. InlineeSite Site;
  529. auto ExpF = getFileName(Strings, Checksums, IL.Header->FileID);
  530. if (!ExpF)
  531. return ExpF.takeError();
  532. Site.FileName = *ExpF;
  533. Site.Inlinee = IL.Header->Inlinee.getIndex();
  534. Site.SourceLineNum = IL.Header->SourceLineNum;
  535. if (Lines.hasExtraFiles()) {
  536. for (const auto EF : IL.ExtraFiles) {
  537. auto ExpF2 = getFileName(Strings, Checksums, EF);
  538. if (!ExpF2)
  539. return ExpF2.takeError();
  540. Site.ExtraFiles.push_back(*ExpF2);
  541. }
  542. }
  543. Result->InlineeLines.Sites.push_back(Site);
  544. }
  545. return Result;
  546. }
  547. Expected<std::shared_ptr<YAMLCrossModuleExportsSubsection>>
  548. YAMLCrossModuleExportsSubsection::fromCodeViewSubsection(
  549. const DebugCrossModuleExportsSubsectionRef &Exports) {
  550. auto Result = std::make_shared<YAMLCrossModuleExportsSubsection>();
  551. Result->Exports.assign(Exports.begin(), Exports.end());
  552. return Result;
  553. }
  554. Expected<std::shared_ptr<YAMLCrossModuleImportsSubsection>>
  555. YAMLCrossModuleImportsSubsection::fromCodeViewSubsection(
  556. const DebugStringTableSubsectionRef &Strings,
  557. const DebugCrossModuleImportsSubsectionRef &Imports) {
  558. auto Result = std::make_shared<YAMLCrossModuleImportsSubsection>();
  559. for (const auto &CMI : Imports) {
  560. YAMLCrossModuleImport YCMI;
  561. auto ExpectedStr = Strings.getString(CMI.Header->ModuleNameOffset);
  562. if (!ExpectedStr)
  563. return ExpectedStr.takeError();
  564. YCMI.ModuleName = *ExpectedStr;
  565. YCMI.ImportIds.assign(CMI.Imports.begin(), CMI.Imports.end());
  566. Result->Imports.push_back(YCMI);
  567. }
  568. return Result;
  569. }
  570. Expected<std::shared_ptr<YAMLSymbolsSubsection>>
  571. YAMLSymbolsSubsection::fromCodeViewSubsection(
  572. const DebugSymbolsSubsectionRef &Symbols) {
  573. auto Result = std::make_shared<YAMLSymbolsSubsection>();
  574. for (const auto &Sym : Symbols) {
  575. auto S = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
  576. if (!S)
  577. return joinErrors(make_error<CodeViewError>(
  578. cv_error_code::corrupt_record,
  579. "Invalid CodeView Symbol Record in SymbolRecord "
  580. "subsection of .debug$S while converting to YAML!"),
  581. S.takeError());
  582. Result->Symbols.push_back(*S);
  583. }
  584. return Result;
  585. }
  586. Expected<std::shared_ptr<YAMLStringTableSubsection>>
  587. YAMLStringTableSubsection::fromCodeViewSubsection(
  588. const DebugStringTableSubsectionRef &Strings) {
  589. auto Result = std::make_shared<YAMLStringTableSubsection>();
  590. BinaryStreamReader Reader(Strings.getBuffer());
  591. StringRef S;
  592. // First item is a single null string, skip it.
  593. if (auto EC = Reader.readCString(S))
  594. return std::move(EC);
  595. assert(S.empty());
  596. while (Reader.bytesRemaining() > 0) {
  597. if (auto EC = Reader.readCString(S))
  598. return std::move(EC);
  599. Result->Strings.push_back(S);
  600. }
  601. return Result;
  602. }
  603. Expected<std::shared_ptr<YAMLFrameDataSubsection>>
  604. YAMLFrameDataSubsection::fromCodeViewSubsection(
  605. const DebugStringTableSubsectionRef &Strings,
  606. const DebugFrameDataSubsectionRef &Frames) {
  607. auto Result = std::make_shared<YAMLFrameDataSubsection>();
  608. for (const auto &F : Frames) {
  609. YAMLFrameData YF;
  610. YF.CodeSize = F.CodeSize;
  611. YF.Flags = F.Flags;
  612. YF.LocalSize = F.LocalSize;
  613. YF.MaxStackSize = F.MaxStackSize;
  614. YF.ParamsSize = F.ParamsSize;
  615. YF.PrologSize = F.PrologSize;
  616. YF.RvaStart = F.RvaStart;
  617. YF.SavedRegsSize = F.SavedRegsSize;
  618. auto ES = Strings.getString(F.FrameFunc);
  619. if (!ES)
  620. return joinErrors(
  621. make_error<CodeViewError>(
  622. cv_error_code::no_records,
  623. "Could not find string for string id while mapping FrameData!"),
  624. ES.takeError());
  625. YF.FrameFunc = *ES;
  626. Result->Frames.push_back(YF);
  627. }
  628. return Result;
  629. }
  630. Expected<std::shared_ptr<YAMLCoffSymbolRVASubsection>>
  631. YAMLCoffSymbolRVASubsection::fromCodeViewSubsection(
  632. const DebugSymbolRVASubsectionRef &Section) {
  633. auto Result = std::make_shared<YAMLCoffSymbolRVASubsection>();
  634. for (const auto &RVA : Section) {
  635. Result->RVAs.push_back(RVA);
  636. }
  637. return Result;
  638. }
  639. Expected<std::vector<std::shared_ptr<DebugSubsection>>>
  640. llvm::CodeViewYAML::toCodeViewSubsectionList(
  641. BumpPtrAllocator &Allocator, ArrayRef<YAMLDebugSubsection> Subsections,
  642. const codeview::StringsAndChecksums &SC) {
  643. std::vector<std::shared_ptr<DebugSubsection>> Result;
  644. if (Subsections.empty())
  645. return std::move(Result);
  646. for (const auto &SS : Subsections) {
  647. std::shared_ptr<DebugSubsection> CVS;
  648. CVS = SS.Subsection->toCodeViewSubsection(Allocator, SC);
  649. assert(CVS != nullptr);
  650. Result.push_back(std::move(CVS));
  651. }
  652. return std::move(Result);
  653. }
  654. namespace {
  655. struct SubsectionConversionVisitor : public DebugSubsectionVisitor {
  656. SubsectionConversionVisitor() {}
  657. Error visitUnknown(DebugUnknownSubsectionRef &Unknown) override;
  658. Error visitLines(DebugLinesSubsectionRef &Lines,
  659. const StringsAndChecksumsRef &State) override;
  660. Error visitFileChecksums(DebugChecksumsSubsectionRef &Checksums,
  661. const StringsAndChecksumsRef &State) override;
  662. Error visitInlineeLines(DebugInlineeLinesSubsectionRef &Inlinees,
  663. const StringsAndChecksumsRef &State) override;
  664. Error visitCrossModuleExports(DebugCrossModuleExportsSubsectionRef &Checksums,
  665. const StringsAndChecksumsRef &State) override;
  666. Error visitCrossModuleImports(DebugCrossModuleImportsSubsectionRef &Inlinees,
  667. const StringsAndChecksumsRef &State) override;
  668. Error visitStringTable(DebugStringTableSubsectionRef &ST,
  669. const StringsAndChecksumsRef &State) override;
  670. Error visitSymbols(DebugSymbolsSubsectionRef &Symbols,
  671. const StringsAndChecksumsRef &State) override;
  672. Error visitFrameData(DebugFrameDataSubsectionRef &Symbols,
  673. const StringsAndChecksumsRef &State) override;
  674. Error visitCOFFSymbolRVAs(DebugSymbolRVASubsectionRef &Symbols,
  675. const StringsAndChecksumsRef &State) override;
  676. YAMLDebugSubsection Subsection;
  677. };
  678. Error SubsectionConversionVisitor::visitUnknown(
  679. DebugUnknownSubsectionRef &Unknown) {
  680. return make_error<CodeViewError>(cv_error_code::operation_unsupported);
  681. }
  682. Error SubsectionConversionVisitor::visitLines(
  683. DebugLinesSubsectionRef &Lines, const StringsAndChecksumsRef &State) {
  684. auto Result = YAMLLinesSubsection::fromCodeViewSubsection(
  685. State.strings(), State.checksums(), Lines);
  686. if (!Result)
  687. return Result.takeError();
  688. Subsection.Subsection = *Result;
  689. return Error::success();
  690. }
  691. Error SubsectionConversionVisitor::visitFileChecksums(
  692. DebugChecksumsSubsectionRef &Checksums,
  693. const StringsAndChecksumsRef &State) {
  694. auto Result = YAMLChecksumsSubsection::fromCodeViewSubsection(State.strings(),
  695. Checksums);
  696. if (!Result)
  697. return Result.takeError();
  698. Subsection.Subsection = *Result;
  699. return Error::success();
  700. }
  701. Error SubsectionConversionVisitor::visitInlineeLines(
  702. DebugInlineeLinesSubsectionRef &Inlinees,
  703. const StringsAndChecksumsRef &State) {
  704. auto Result = YAMLInlineeLinesSubsection::fromCodeViewSubsection(
  705. State.strings(), State.checksums(), Inlinees);
  706. if (!Result)
  707. return Result.takeError();
  708. Subsection.Subsection = *Result;
  709. return Error::success();
  710. }
  711. Error SubsectionConversionVisitor::visitCrossModuleExports(
  712. DebugCrossModuleExportsSubsectionRef &Exports,
  713. const StringsAndChecksumsRef &State) {
  714. auto Result =
  715. YAMLCrossModuleExportsSubsection::fromCodeViewSubsection(Exports);
  716. if (!Result)
  717. return Result.takeError();
  718. Subsection.Subsection = *Result;
  719. return Error::success();
  720. }
  721. Error SubsectionConversionVisitor::visitCrossModuleImports(
  722. DebugCrossModuleImportsSubsectionRef &Imports,
  723. const StringsAndChecksumsRef &State) {
  724. auto Result = YAMLCrossModuleImportsSubsection::fromCodeViewSubsection(
  725. State.strings(), Imports);
  726. if (!Result)
  727. return Result.takeError();
  728. Subsection.Subsection = *Result;
  729. return Error::success();
  730. }
  731. Error SubsectionConversionVisitor::visitStringTable(
  732. DebugStringTableSubsectionRef &Strings,
  733. const StringsAndChecksumsRef &State) {
  734. auto Result = YAMLStringTableSubsection::fromCodeViewSubsection(Strings);
  735. if (!Result)
  736. return Result.takeError();
  737. Subsection.Subsection = *Result;
  738. return Error::success();
  739. }
  740. Error SubsectionConversionVisitor::visitSymbols(
  741. DebugSymbolsSubsectionRef &Symbols, const StringsAndChecksumsRef &State) {
  742. auto Result = YAMLSymbolsSubsection::fromCodeViewSubsection(Symbols);
  743. if (!Result)
  744. return Result.takeError();
  745. Subsection.Subsection = *Result;
  746. return Error::success();
  747. }
  748. Error SubsectionConversionVisitor::visitFrameData(
  749. DebugFrameDataSubsectionRef &Frames, const StringsAndChecksumsRef &State) {
  750. auto Result =
  751. YAMLFrameDataSubsection::fromCodeViewSubsection(State.strings(), Frames);
  752. if (!Result)
  753. return Result.takeError();
  754. Subsection.Subsection = *Result;
  755. return Error::success();
  756. }
  757. Error SubsectionConversionVisitor::visitCOFFSymbolRVAs(
  758. DebugSymbolRVASubsectionRef &RVAs, const StringsAndChecksumsRef &State) {
  759. auto Result = YAMLCoffSymbolRVASubsection::fromCodeViewSubsection(RVAs);
  760. if (!Result)
  761. return Result.takeError();
  762. Subsection.Subsection = *Result;
  763. return Error::success();
  764. }
  765. }
  766. Expected<YAMLDebugSubsection>
  767. YAMLDebugSubsection::fromCodeViewSubection(const StringsAndChecksumsRef &SC,
  768. const DebugSubsectionRecord &SS) {
  769. SubsectionConversionVisitor V;
  770. if (auto EC = visitDebugSubsection(SS, V, SC))
  771. return std::move(EC);
  772. return V.Subsection;
  773. }
  774. std::vector<YAMLDebugSubsection>
  775. llvm::CodeViewYAML::fromDebugS(ArrayRef<uint8_t> Data,
  776. const StringsAndChecksumsRef &SC) {
  777. BinaryStreamReader Reader(Data, support::little);
  778. uint32_t Magic;
  779. ExitOnError Err("Invalid .debug$S section!");
  780. Err(Reader.readInteger(Magic));
  781. assert(Magic == COFF::DEBUG_SECTION_MAGIC && "Invalid .debug$S section!");
  782. DebugSubsectionArray Subsections;
  783. Err(Reader.readArray(Subsections, Reader.bytesRemaining()));
  784. std::vector<YAMLDebugSubsection> Result;
  785. for (const auto &SS : Subsections) {
  786. auto YamlSS = Err(YAMLDebugSubsection::fromCodeViewSubection(SC, SS));
  787. Result.push_back(YamlSS);
  788. }
  789. return Result;
  790. }
  791. void llvm::CodeViewYAML::initializeStringsAndChecksums(
  792. ArrayRef<YAMLDebugSubsection> Sections, codeview::StringsAndChecksums &SC) {
  793. // String Table and Checksums subsections don't use the allocator.
  794. BumpPtrAllocator Allocator;
  795. // It's possible for checksums and strings to even appear in different debug$S
  796. // sections, so we have to make this a stateful function that can build up
  797. // the strings and checksums field over multiple iterations.
  798. // File Checksums require the string table, but may become before it, so we
  799. // have to scan for strings first, then scan for checksums again from the
  800. // beginning.
  801. if (!SC.hasStrings()) {
  802. for (const auto &SS : Sections) {
  803. if (SS.Subsection->Kind != DebugSubsectionKind::StringTable)
  804. continue;
  805. auto Result = SS.Subsection->toCodeViewSubsection(Allocator, SC);
  806. SC.setStrings(
  807. std::static_pointer_cast<DebugStringTableSubsection>(Result));
  808. break;
  809. }
  810. }
  811. if (SC.hasStrings() && !SC.hasChecksums()) {
  812. for (const auto &SS : Sections) {
  813. if (SS.Subsection->Kind != DebugSubsectionKind::FileChecksums)
  814. continue;
  815. auto Result = SS.Subsection->toCodeViewSubsection(Allocator, SC);
  816. SC.setChecksums(
  817. std::static_pointer_cast<DebugChecksumsSubsection>(Result));
  818. break;
  819. }
  820. }
  821. }