SymbolRecordMapping.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. //===- SymbolRecordMapping.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/SymbolRecordMapping.h"
  9. using namespace llvm;
  10. using namespace llvm::codeview;
  11. #define error(X) \
  12. if (auto EC = X) \
  13. return EC;
  14. namespace {
  15. struct MapGap {
  16. Error operator()(CodeViewRecordIO &IO, LocalVariableAddrGap &Gap) const {
  17. error(IO.mapInteger(Gap.GapStartOffset));
  18. error(IO.mapInteger(Gap.Range));
  19. return Error::success();
  20. }
  21. };
  22. }
  23. static Error mapLocalVariableAddrRange(CodeViewRecordIO &IO,
  24. LocalVariableAddrRange &Range) {
  25. error(IO.mapInteger(Range.OffsetStart));
  26. error(IO.mapInteger(Range.ISectStart));
  27. error(IO.mapInteger(Range.Range));
  28. return Error::success();
  29. }
  30. Error SymbolRecordMapping::visitSymbolBegin(CVSymbol &Record) {
  31. error(IO.beginRecord(MaxRecordLength - sizeof(RecordPrefix)));
  32. return Error::success();
  33. }
  34. Error SymbolRecordMapping::visitSymbolEnd(CVSymbol &Record) {
  35. error(IO.padToAlignment(alignOf(Container)));
  36. error(IO.endRecord());
  37. return Error::success();
  38. }
  39. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, BlockSym &Block) {
  40. error(IO.mapInteger(Block.Parent));
  41. error(IO.mapInteger(Block.End));
  42. error(IO.mapInteger(Block.CodeSize));
  43. error(IO.mapInteger(Block.CodeOffset));
  44. error(IO.mapInteger(Block.Segment));
  45. error(IO.mapStringZ(Block.Name));
  46. return Error::success();
  47. }
  48. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, Thunk32Sym &Thunk) {
  49. error(IO.mapInteger(Thunk.Parent));
  50. error(IO.mapInteger(Thunk.End));
  51. error(IO.mapInteger(Thunk.Next));
  52. error(IO.mapInteger(Thunk.Offset));
  53. error(IO.mapInteger(Thunk.Segment));
  54. error(IO.mapInteger(Thunk.Length));
  55. error(IO.mapEnum(Thunk.Thunk));
  56. error(IO.mapStringZ(Thunk.Name));
  57. error(IO.mapByteVectorTail(Thunk.VariantData));
  58. return Error::success();
  59. }
  60. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  61. TrampolineSym &Tramp) {
  62. error(IO.mapEnum(Tramp.Type));
  63. error(IO.mapInteger(Tramp.Size));
  64. error(IO.mapInteger(Tramp.ThunkOffset));
  65. error(IO.mapInteger(Tramp.TargetOffset));
  66. error(IO.mapInteger(Tramp.ThunkSection));
  67. error(IO.mapInteger(Tramp.TargetSection));
  68. return Error::success();
  69. }
  70. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  71. SectionSym &Section) {
  72. uint8_t Padding = 0;
  73. error(IO.mapInteger(Section.SectionNumber));
  74. error(IO.mapInteger(Section.Alignment));
  75. error(IO.mapInteger(Padding));
  76. error(IO.mapInteger(Section.Rva));
  77. error(IO.mapInteger(Section.Length));
  78. error(IO.mapInteger(Section.Characteristics));
  79. error(IO.mapStringZ(Section.Name));
  80. return Error::success();
  81. }
  82. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  83. CoffGroupSym &CoffGroup) {
  84. error(IO.mapInteger(CoffGroup.Size));
  85. error(IO.mapInteger(CoffGroup.Characteristics));
  86. error(IO.mapInteger(CoffGroup.Offset));
  87. error(IO.mapInteger(CoffGroup.Segment));
  88. error(IO.mapStringZ(CoffGroup.Name));
  89. return Error::success();
  90. }
  91. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  92. BPRelativeSym &BPRel) {
  93. error(IO.mapInteger(BPRel.Offset));
  94. error(IO.mapInteger(BPRel.Type));
  95. error(IO.mapStringZ(BPRel.Name));
  96. return Error::success();
  97. }
  98. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  99. BuildInfoSym &BuildInfo) {
  100. error(IO.mapInteger(BuildInfo.BuildId));
  101. return Error::success();
  102. }
  103. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  104. CallSiteInfoSym &CallSiteInfo) {
  105. uint16_t Padding = 0;
  106. error(IO.mapInteger(CallSiteInfo.CodeOffset));
  107. error(IO.mapInteger(CallSiteInfo.Segment));
  108. error(IO.mapInteger(Padding));
  109. error(IO.mapInteger(CallSiteInfo.Type));
  110. return Error::success();
  111. }
  112. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  113. EnvBlockSym &EnvBlock) {
  114. uint8_t Reserved = 0;
  115. error(IO.mapInteger(Reserved));
  116. error(IO.mapStringZVectorZ(EnvBlock.Fields));
  117. return Error::success();
  118. }
  119. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  120. FileStaticSym &FileStatic) {
  121. error(IO.mapInteger(FileStatic.Index));
  122. error(IO.mapInteger(FileStatic.ModFilenameOffset));
  123. error(IO.mapEnum(FileStatic.Flags));
  124. error(IO.mapStringZ(FileStatic.Name));
  125. return Error::success();
  126. }
  127. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, ExportSym &Export) {
  128. error(IO.mapInteger(Export.Ordinal));
  129. error(IO.mapEnum(Export.Flags));
  130. error(IO.mapStringZ(Export.Name));
  131. return Error::success();
  132. }
  133. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  134. Compile2Sym &Compile2) {
  135. error(IO.mapEnum(Compile2.Flags));
  136. error(IO.mapEnum(Compile2.Machine));
  137. error(IO.mapInteger(Compile2.VersionFrontendMajor));
  138. error(IO.mapInteger(Compile2.VersionFrontendMinor));
  139. error(IO.mapInteger(Compile2.VersionFrontendBuild));
  140. error(IO.mapInteger(Compile2.VersionBackendMajor));
  141. error(IO.mapInteger(Compile2.VersionBackendMinor));
  142. error(IO.mapInteger(Compile2.VersionBackendBuild));
  143. error(IO.mapStringZ(Compile2.Version));
  144. error(IO.mapStringZVectorZ(Compile2.ExtraStrings));
  145. return Error::success();
  146. }
  147. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  148. Compile3Sym &Compile3) {
  149. error(IO.mapEnum(Compile3.Flags));
  150. error(IO.mapEnum(Compile3.Machine));
  151. error(IO.mapInteger(Compile3.VersionFrontendMajor));
  152. error(IO.mapInteger(Compile3.VersionFrontendMinor));
  153. error(IO.mapInteger(Compile3.VersionFrontendBuild));
  154. error(IO.mapInteger(Compile3.VersionFrontendQFE));
  155. error(IO.mapInteger(Compile3.VersionBackendMajor));
  156. error(IO.mapInteger(Compile3.VersionBackendMinor));
  157. error(IO.mapInteger(Compile3.VersionBackendBuild));
  158. error(IO.mapInteger(Compile3.VersionBackendQFE));
  159. error(IO.mapStringZ(Compile3.Version));
  160. return Error::success();
  161. }
  162. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  163. ConstantSym &Constant) {
  164. error(IO.mapInteger(Constant.Type));
  165. error(IO.mapEncodedInteger(Constant.Value));
  166. error(IO.mapStringZ(Constant.Name));
  167. return Error::success();
  168. }
  169. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, DataSym &Data) {
  170. error(IO.mapInteger(Data.Type));
  171. error(IO.mapInteger(Data.DataOffset));
  172. error(IO.mapInteger(Data.Segment));
  173. error(IO.mapStringZ(Data.Name));
  174. return Error::success();
  175. }
  176. Error SymbolRecordMapping::visitKnownRecord(
  177. CVSymbol &CVR, DefRangeFramePointerRelSym &DefRangeFramePointerRel) {
  178. error(IO.mapObject(DefRangeFramePointerRel.Hdr.Offset));
  179. error(mapLocalVariableAddrRange(IO, DefRangeFramePointerRel.Range));
  180. error(IO.mapVectorTail(DefRangeFramePointerRel.Gaps, MapGap()));
  181. return Error::success();
  182. }
  183. Error SymbolRecordMapping::visitKnownRecord(
  184. CVSymbol &CVR,
  185. DefRangeFramePointerRelFullScopeSym &DefRangeFramePointerRelFullScope) {
  186. error(IO.mapInteger(DefRangeFramePointerRelFullScope.Offset));
  187. return Error::success();
  188. }
  189. Error SymbolRecordMapping::visitKnownRecord(
  190. CVSymbol &CVR, DefRangeRegisterRelSym &DefRangeRegisterRel) {
  191. error(IO.mapObject(DefRangeRegisterRel.Hdr.Register));
  192. error(IO.mapObject(DefRangeRegisterRel.Hdr.Flags));
  193. error(IO.mapObject(DefRangeRegisterRel.Hdr.BasePointerOffset));
  194. error(mapLocalVariableAddrRange(IO, DefRangeRegisterRel.Range));
  195. error(IO.mapVectorTail(DefRangeRegisterRel.Gaps, MapGap()));
  196. return Error::success();
  197. }
  198. Error SymbolRecordMapping::visitKnownRecord(
  199. CVSymbol &CVR, DefRangeRegisterSym &DefRangeRegister) {
  200. error(IO.mapObject(DefRangeRegister.Hdr.Register));
  201. error(IO.mapObject(DefRangeRegister.Hdr.MayHaveNoName));
  202. error(mapLocalVariableAddrRange(IO, DefRangeRegister.Range));
  203. error(IO.mapVectorTail(DefRangeRegister.Gaps, MapGap()));
  204. return Error::success();
  205. }
  206. Error SymbolRecordMapping::visitKnownRecord(
  207. CVSymbol &CVR, DefRangeSubfieldRegisterSym &DefRangeSubfieldRegister) {
  208. error(IO.mapObject(DefRangeSubfieldRegister.Hdr.Register));
  209. error(IO.mapObject(DefRangeSubfieldRegister.Hdr.MayHaveNoName));
  210. error(IO.mapObject(DefRangeSubfieldRegister.Hdr.OffsetInParent));
  211. error(mapLocalVariableAddrRange(IO, DefRangeSubfieldRegister.Range));
  212. error(IO.mapVectorTail(DefRangeSubfieldRegister.Gaps, MapGap()));
  213. return Error::success();
  214. }
  215. Error SymbolRecordMapping::visitKnownRecord(
  216. CVSymbol &CVR, DefRangeSubfieldSym &DefRangeSubfield) {
  217. error(IO.mapInteger(DefRangeSubfield.Program));
  218. error(IO.mapInteger(DefRangeSubfield.OffsetInParent));
  219. error(mapLocalVariableAddrRange(IO, DefRangeSubfield.Range));
  220. error(IO.mapVectorTail(DefRangeSubfield.Gaps, MapGap()));
  221. return Error::success();
  222. }
  223. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  224. DefRangeSym &DefRange) {
  225. error(IO.mapInteger(DefRange.Program));
  226. error(mapLocalVariableAddrRange(IO, DefRange.Range));
  227. error(IO.mapVectorTail(DefRange.Gaps, MapGap()));
  228. return Error::success();
  229. }
  230. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  231. FrameCookieSym &FrameCookie) {
  232. error(IO.mapInteger(FrameCookie.CodeOffset));
  233. error(IO.mapInteger(FrameCookie.Register));
  234. error(IO.mapEnum(FrameCookie.CookieKind));
  235. error(IO.mapInteger(FrameCookie.Flags));
  236. return Error::success();
  237. }
  238. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  239. FrameProcSym &FrameProc) {
  240. error(IO.mapInteger(FrameProc.TotalFrameBytes));
  241. error(IO.mapInteger(FrameProc.PaddingFrameBytes));
  242. error(IO.mapInteger(FrameProc.OffsetToPadding));
  243. error(IO.mapInteger(FrameProc.BytesOfCalleeSavedRegisters));
  244. error(IO.mapInteger(FrameProc.OffsetOfExceptionHandler));
  245. error(IO.mapInteger(FrameProc.SectionIdOfExceptionHandler));
  246. error(IO.mapEnum(FrameProc.Flags));
  247. return Error::success();
  248. }
  249. Error SymbolRecordMapping::visitKnownRecord(
  250. CVSymbol &CVR, HeapAllocationSiteSym &HeapAllocSite) {
  251. error(IO.mapInteger(HeapAllocSite.CodeOffset));
  252. error(IO.mapInteger(HeapAllocSite.Segment));
  253. error(IO.mapInteger(HeapAllocSite.CallInstructionSize));
  254. error(IO.mapInteger(HeapAllocSite.Type));
  255. return Error::success();
  256. }
  257. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  258. InlineSiteSym &InlineSite) {
  259. error(IO.mapInteger(InlineSite.Parent));
  260. error(IO.mapInteger(InlineSite.End));
  261. error(IO.mapInteger(InlineSite.Inlinee));
  262. error(IO.mapByteVectorTail(InlineSite.AnnotationData));
  263. return Error::success();
  264. }
  265. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  266. RegisterSym &Register) {
  267. error(IO.mapInteger(Register.Index));
  268. error(IO.mapEnum(Register.Register));
  269. error(IO.mapStringZ(Register.Name));
  270. return Error::success();
  271. }
  272. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  273. PublicSym32 &Public) {
  274. error(IO.mapEnum(Public.Flags));
  275. error(IO.mapInteger(Public.Offset));
  276. error(IO.mapInteger(Public.Segment));
  277. error(IO.mapStringZ(Public.Name));
  278. return Error::success();
  279. }
  280. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  281. ProcRefSym &ProcRef) {
  282. error(IO.mapInteger(ProcRef.SumName));
  283. error(IO.mapInteger(ProcRef.SymOffset));
  284. error(IO.mapInteger(ProcRef.Module));
  285. error(IO.mapStringZ(ProcRef.Name));
  286. return Error::success();
  287. }
  288. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, LabelSym &Label) {
  289. error(IO.mapInteger(Label.CodeOffset));
  290. error(IO.mapInteger(Label.Segment));
  291. error(IO.mapEnum(Label.Flags));
  292. error(IO.mapStringZ(Label.Name));
  293. return Error::success();
  294. }
  295. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, LocalSym &Local) {
  296. error(IO.mapInteger(Local.Type));
  297. error(IO.mapEnum(Local.Flags));
  298. error(IO.mapStringZ(Local.Name));
  299. return Error::success();
  300. }
  301. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  302. ObjNameSym &ObjName) {
  303. error(IO.mapInteger(ObjName.Signature));
  304. error(IO.mapStringZ(ObjName.Name));
  305. return Error::success();
  306. }
  307. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, ProcSym &Proc) {
  308. error(IO.mapInteger(Proc.Parent));
  309. error(IO.mapInteger(Proc.End));
  310. error(IO.mapInteger(Proc.Next));
  311. error(IO.mapInteger(Proc.CodeSize));
  312. error(IO.mapInteger(Proc.DbgStart));
  313. error(IO.mapInteger(Proc.DbgEnd));
  314. error(IO.mapInteger(Proc.FunctionType));
  315. error(IO.mapInteger(Proc.CodeOffset));
  316. error(IO.mapInteger(Proc.Segment));
  317. error(IO.mapEnum(Proc.Flags));
  318. error(IO.mapStringZ(Proc.Name));
  319. return Error::success();
  320. }
  321. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  322. ScopeEndSym &ScopeEnd) {
  323. return Error::success();
  324. }
  325. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, CallerSym &Caller) {
  326. error(IO.mapVectorN<uint32_t>(
  327. Caller.Indices,
  328. [](CodeViewRecordIO &IO, TypeIndex &N) { return IO.mapInteger(N); }));
  329. return Error::success();
  330. }
  331. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  332. RegRelativeSym &RegRel) {
  333. error(IO.mapInteger(RegRel.Offset));
  334. error(IO.mapInteger(RegRel.Type));
  335. error(IO.mapEnum(RegRel.Register));
  336. error(IO.mapStringZ(RegRel.Name));
  337. return Error::success();
  338. }
  339. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  340. ThreadLocalDataSym &Data) {
  341. error(IO.mapInteger(Data.Type));
  342. error(IO.mapInteger(Data.DataOffset));
  343. error(IO.mapInteger(Data.Segment));
  344. error(IO.mapStringZ(Data.Name));
  345. return Error::success();
  346. }
  347. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR, UDTSym &UDT) {
  348. error(IO.mapInteger(UDT.Type));
  349. error(IO.mapStringZ(UDT.Name));
  350. return Error::success();
  351. }
  352. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  353. UsingNamespaceSym &UN) {
  354. error(IO.mapStringZ(UN.Name));
  355. return Error::success();
  356. }
  357. Error SymbolRecordMapping::visitKnownRecord(CVSymbol &CVR,
  358. AnnotationSym &Annot) {
  359. error(IO.mapInteger(Annot.CodeOffset));
  360. error(IO.mapInteger(Annot.Segment));
  361. error(IO.mapVectorN<uint16_t>(
  362. Annot.Strings,
  363. [](CodeViewRecordIO &IO, StringRef &S) { return IO.mapStringZ(S); }));
  364. return Error::success();
  365. }
  366. RegisterId codeview::decodeFramePtrReg(EncodedFramePtrReg EncodedReg,
  367. CPUType CPU) {
  368. assert(unsigned(EncodedReg) < 4);
  369. switch (CPU) {
  370. // FIXME: Add ARM and AArch64 variants here.
  371. default:
  372. break;
  373. case CPUType::Intel8080:
  374. case CPUType::Intel8086:
  375. case CPUType::Intel80286:
  376. case CPUType::Intel80386:
  377. case CPUType::Intel80486:
  378. case CPUType::Pentium:
  379. case CPUType::PentiumPro:
  380. case CPUType::Pentium3:
  381. switch (EncodedReg) {
  382. case EncodedFramePtrReg::None: return RegisterId::NONE;
  383. case EncodedFramePtrReg::StackPtr: return RegisterId::VFRAME;
  384. case EncodedFramePtrReg::FramePtr: return RegisterId::EBP;
  385. case EncodedFramePtrReg::BasePtr: return RegisterId::EBX;
  386. }
  387. llvm_unreachable("bad encoding");
  388. case CPUType::X64:
  389. switch (EncodedReg) {
  390. case EncodedFramePtrReg::None: return RegisterId::NONE;
  391. case EncodedFramePtrReg::StackPtr: return RegisterId::RSP;
  392. case EncodedFramePtrReg::FramePtr: return RegisterId::RBP;
  393. case EncodedFramePtrReg::BasePtr: return RegisterId::R13;
  394. }
  395. llvm_unreachable("bad encoding");
  396. }
  397. return RegisterId::NONE;
  398. }
  399. EncodedFramePtrReg codeview::encodeFramePtrReg(RegisterId Reg, CPUType CPU) {
  400. switch (CPU) {
  401. // FIXME: Add ARM and AArch64 variants here.
  402. default:
  403. break;
  404. case CPUType::Intel8080:
  405. case CPUType::Intel8086:
  406. case CPUType::Intel80286:
  407. case CPUType::Intel80386:
  408. case CPUType::Intel80486:
  409. case CPUType::Pentium:
  410. case CPUType::PentiumPro:
  411. case CPUType::Pentium3:
  412. switch (Reg) {
  413. case RegisterId::VFRAME:
  414. return EncodedFramePtrReg::StackPtr;
  415. case RegisterId::EBP:
  416. return EncodedFramePtrReg::FramePtr;
  417. case RegisterId::EBX:
  418. return EncodedFramePtrReg::BasePtr;
  419. default:
  420. break;
  421. }
  422. break;
  423. case CPUType::X64:
  424. switch (Reg) {
  425. case RegisterId::RSP:
  426. return EncodedFramePtrReg::StackPtr;
  427. case RegisterId::RBP:
  428. return EncodedFramePtrReg::FramePtr;
  429. case RegisterId::R13:
  430. return EncodedFramePtrReg::BasePtr;
  431. default:
  432. break;
  433. }
  434. break;
  435. }
  436. return EncodedFramePtrReg::None;
  437. }