1 //===- CodeViewYAMLTypes.cpp - CodeView YAMLIO types implementation -------===// 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 // This file defines classes for handling the YAML representation of CodeView 10 // Debug Info. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ObjectYAML/CodeViewYAMLTypes.h" 15 #include "llvm/ADT/APSInt.h" 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/BinaryFormat/COFF.h" 19 #include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h" 20 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" 21 #include "llvm/DebugInfo/CodeView/CodeView.h" 22 #include "llvm/DebugInfo/CodeView/CodeViewError.h" 23 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h" 24 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 25 #include "llvm/DebugInfo/CodeView/TypeIndex.h" 26 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" 27 #include "llvm/Support/Allocator.h" 28 #include "llvm/Support/BinaryStreamReader.h" 29 #include "llvm/Support/BinaryStreamWriter.h" 30 #include "llvm/Support/Endian.h" 31 #include "llvm/Support/Error.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/YAMLTraits.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include <algorithm> 36 #include <cassert> 37 #include <cstdint> 38 #include <vector> 39 40 using namespace llvm; 41 using namespace llvm::codeview; 42 using namespace llvm::CodeViewYAML; 43 using namespace llvm::CodeViewYAML::detail; 44 using namespace llvm::yaml; 45 46 LLVM_YAML_IS_SEQUENCE_VECTOR(OneMethodRecord) 47 LLVM_YAML_IS_SEQUENCE_VECTOR(VFTableSlotKind) 48 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(TypeIndex) 49 50 LLVM_YAML_DECLARE_SCALAR_TRAITS(TypeIndex, QuotingType::None) 51 LLVM_YAML_DECLARE_SCALAR_TRAITS(APSInt, QuotingType::None) 52 53 LLVM_YAML_DECLARE_ENUM_TRAITS(TypeLeafKind) 54 LLVM_YAML_DECLARE_ENUM_TRAITS(PointerToMemberRepresentation) 55 LLVM_YAML_DECLARE_ENUM_TRAITS(VFTableSlotKind) 56 LLVM_YAML_DECLARE_ENUM_TRAITS(CallingConvention) 57 LLVM_YAML_DECLARE_ENUM_TRAITS(PointerKind) 58 LLVM_YAML_DECLARE_ENUM_TRAITS(PointerMode) 59 LLVM_YAML_DECLARE_ENUM_TRAITS(HfaKind) 60 LLVM_YAML_DECLARE_ENUM_TRAITS(MemberAccess) 61 LLVM_YAML_DECLARE_ENUM_TRAITS(MethodKind) 62 LLVM_YAML_DECLARE_ENUM_TRAITS(WindowsRTClassKind) 63 LLVM_YAML_DECLARE_ENUM_TRAITS(LabelType) 64 65 LLVM_YAML_DECLARE_BITSET_TRAITS(PointerOptions) 66 LLVM_YAML_DECLARE_BITSET_TRAITS(ModifierOptions) 67 LLVM_YAML_DECLARE_BITSET_TRAITS(FunctionOptions) 68 LLVM_YAML_DECLARE_BITSET_TRAITS(ClassOptions) 69 LLVM_YAML_DECLARE_BITSET_TRAITS(MethodOptions) 70 71 LLVM_YAML_DECLARE_MAPPING_TRAITS(OneMethodRecord) 72 LLVM_YAML_DECLARE_MAPPING_TRAITS(MemberPointerInfo) 73 74 namespace llvm { 75 namespace CodeViewYAML { 76 namespace detail { 77 78 struct LeafRecordBase { 79 TypeLeafKind Kind; 80 81 explicit LeafRecordBase(TypeLeafKind K) : Kind(K) {} 82 virtual ~LeafRecordBase() = default; 83 84 virtual void map(yaml::IO &io) = 0; 85 virtual CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const = 0; 86 virtual Error fromCodeViewRecord(CVType Type) = 0; 87 }; 88 89 template <typename T> struct LeafRecordImpl : public LeafRecordBase { 90 explicit LeafRecordImpl(TypeLeafKind K) 91 : LeafRecordBase(K), Record(static_cast<TypeRecordKind>(K)) {} 92 93 void map(yaml::IO &io) override; 94 95 Error fromCodeViewRecord(CVType Type) override { 96 return TypeDeserializer::deserializeAs<T>(Type, Record); 97 } 98 99 CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const override { 100 TS.writeLeafType(Record); 101 return CVType(TS.records().back()); 102 } 103 104 mutable T Record; 105 }; 106 107 template <> struct LeafRecordImpl<FieldListRecord> : public LeafRecordBase { 108 explicit LeafRecordImpl(TypeLeafKind K) : LeafRecordBase(K) {} 109 110 void map(yaml::IO &io) override; 111 CVType toCodeViewRecord(AppendingTypeTableBuilder &TS) const override; 112 Error fromCodeViewRecord(CVType Type) override; 113 114 std::vector<MemberRecord> Members; 115 }; 116 117 struct MemberRecordBase { 118 TypeLeafKind Kind; 119 120 explicit MemberRecordBase(TypeLeafKind K) : Kind(K) {} 121 virtual ~MemberRecordBase() = default; 122 123 virtual void map(yaml::IO &io) = 0; 124 virtual void writeTo(ContinuationRecordBuilder &CRB) = 0; 125 }; 126 127 template <typename T> struct MemberRecordImpl : public MemberRecordBase { 128 explicit MemberRecordImpl(TypeLeafKind K) 129 : MemberRecordBase(K), Record(static_cast<TypeRecordKind>(K)) {} 130 131 void map(yaml::IO &io) override; 132 133 void writeTo(ContinuationRecordBuilder &CRB) override { 134 CRB.writeMemberType(Record); 135 } 136 137 mutable T Record; 138 }; 139 140 } // end namespace detail 141 } // end namespace CodeViewYAML 142 } // end namespace llvm 143 144 void ScalarTraits<GUID>::output(const GUID &G, void *, llvm::raw_ostream &OS) { 145 OS << G; 146 } 147 148 StringRef ScalarTraits<GUID>::input(StringRef Scalar, void *Ctx, GUID &S) { 149 if (Scalar.size() != 38) 150 return "GUID strings are 38 characters long"; 151 if (Scalar.front() != '{' || Scalar.back() != '}') 152 return "GUID is not enclosed in {}"; 153 Scalar = Scalar.substr(1, Scalar.size() - 2); 154 SmallVector<StringRef, 6> A; 155 Scalar.split(A, '-', 5); 156 if (A.size() != 5 || Scalar[8] != '-' || Scalar[13] != '-' || 157 Scalar[18] != '-' || Scalar[23] != '-') 158 return "GUID sections are not properly delineated with dashes"; 159 struct MSGuid { 160 support::ulittle32_t Data1; 161 support::ulittle16_t Data2; 162 support::ulittle16_t Data3; 163 support::ubig64_t Data4; 164 }; 165 MSGuid G = {}; 166 uint64_t D41{}, D42{}; 167 if (!to_integer(A[0], G.Data1, 16) || !to_integer(A[1], G.Data2, 16) || 168 !to_integer(A[2], G.Data3, 16) || !to_integer(A[3], D41, 16) || 169 !to_integer(A[4], D42, 16)) 170 return "GUID contains non hex digits"; 171 G.Data4 = (D41 << 48) | D42; 172 ::memcpy(&S, &G, sizeof(GUID)); 173 return ""; 174 } 175 176 void ScalarTraits<TypeIndex>::output(const TypeIndex &S, void *, 177 raw_ostream &OS) { 178 OS << S.getIndex(); 179 } 180 181 StringRef ScalarTraits<TypeIndex>::input(StringRef Scalar, void *Ctx, 182 TypeIndex &S) { 183 uint32_t I; 184 StringRef Result = ScalarTraits<uint32_t>::input(Scalar, Ctx, I); 185 S.setIndex(I); 186 return Result; 187 } 188 189 void ScalarTraits<APSInt>::output(const APSInt &S, void *, raw_ostream &OS) { 190 S.print(OS, S.isSigned()); 191 } 192 193 StringRef ScalarTraits<APSInt>::input(StringRef Scalar, void *Ctx, APSInt &S) { 194 S = APSInt(Scalar); 195 return ""; 196 } 197 198 void ScalarEnumerationTraits<TypeLeafKind>::enumeration(IO &io, 199 TypeLeafKind &Value) { 200 #define CV_TYPE(name, val) io.enumCase(Value, #name, name); 201 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def" 202 #undef CV_TYPE 203 } 204 205 void ScalarEnumerationTraits<PointerToMemberRepresentation>::enumeration( 206 IO &IO, PointerToMemberRepresentation &Value) { 207 IO.enumCase(Value, "Unknown", PointerToMemberRepresentation::Unknown); 208 IO.enumCase(Value, "SingleInheritanceData", 209 PointerToMemberRepresentation::SingleInheritanceData); 210 IO.enumCase(Value, "MultipleInheritanceData", 211 PointerToMemberRepresentation::MultipleInheritanceData); 212 IO.enumCase(Value, "VirtualInheritanceData", 213 PointerToMemberRepresentation::VirtualInheritanceData); 214 IO.enumCase(Value, "GeneralData", PointerToMemberRepresentation::GeneralData); 215 IO.enumCase(Value, "SingleInheritanceFunction", 216 PointerToMemberRepresentation::SingleInheritanceFunction); 217 IO.enumCase(Value, "MultipleInheritanceFunction", 218 PointerToMemberRepresentation::MultipleInheritanceFunction); 219 IO.enumCase(Value, "VirtualInheritanceFunction", 220 PointerToMemberRepresentation::VirtualInheritanceFunction); 221 IO.enumCase(Value, "GeneralFunction", 222 PointerToMemberRepresentation::GeneralFunction); 223 } 224 225 void ScalarEnumerationTraits<VFTableSlotKind>::enumeration( 226 IO &IO, VFTableSlotKind &Kind) { 227 IO.enumCase(Kind, "Near16", VFTableSlotKind::Near16); 228 IO.enumCase(Kind, "Far16", VFTableSlotKind::Far16); 229 IO.enumCase(Kind, "This", VFTableSlotKind::This); 230 IO.enumCase(Kind, "Outer", VFTableSlotKind::Outer); 231 IO.enumCase(Kind, "Meta", VFTableSlotKind::Meta); 232 IO.enumCase(Kind, "Near", VFTableSlotKind::Near); 233 IO.enumCase(Kind, "Far", VFTableSlotKind::Far); 234 } 235 236 void ScalarEnumerationTraits<CallingConvention>::enumeration( 237 IO &IO, CallingConvention &Value) { 238 IO.enumCase(Value, "NearC", CallingConvention::NearC); 239 IO.enumCase(Value, "FarC", CallingConvention::FarC); 240 IO.enumCase(Value, "NearPascal", CallingConvention::NearPascal); 241 IO.enumCase(Value, "FarPascal", CallingConvention::FarPascal); 242 IO.enumCase(Value, "NearFast", CallingConvention::NearFast); 243 IO.enumCase(Value, "FarFast", CallingConvention::FarFast); 244 IO.enumCase(Value, "NearStdCall", CallingConvention::NearStdCall); 245 IO.enumCase(Value, "FarStdCall", CallingConvention::FarStdCall); 246 IO.enumCase(Value, "NearSysCall", CallingConvention::NearSysCall); 247 IO.enumCase(Value, "FarSysCall", CallingConvention::FarSysCall); 248 IO.enumCase(Value, "ThisCall", CallingConvention::ThisCall); 249 IO.enumCase(Value, "MipsCall", CallingConvention::MipsCall); 250 IO.enumCase(Value, "Generic", CallingConvention::Generic); 251 IO.enumCase(Value, "AlphaCall", CallingConvention::AlphaCall); 252 IO.enumCase(Value, "PpcCall", CallingConvention::PpcCall); 253 IO.enumCase(Value, "SHCall", CallingConvention::SHCall); 254 IO.enumCase(Value, "ArmCall", CallingConvention::ArmCall); 255 IO.enumCase(Value, "AM33Call", CallingConvention::AM33Call); 256 IO.enumCase(Value, "TriCall", CallingConvention::TriCall); 257 IO.enumCase(Value, "SH5Call", CallingConvention::SH5Call); 258 IO.enumCase(Value, "M32RCall", CallingConvention::M32RCall); 259 IO.enumCase(Value, "ClrCall", CallingConvention::ClrCall); 260 IO.enumCase(Value, "Inline", CallingConvention::Inline); 261 IO.enumCase(Value, "NearVector", CallingConvention::NearVector); 262 } 263 264 void ScalarEnumerationTraits<PointerKind>::enumeration(IO &IO, 265 PointerKind &Kind) { 266 IO.enumCase(Kind, "Near16", PointerKind::Near16); 267 IO.enumCase(Kind, "Far16", PointerKind::Far16); 268 IO.enumCase(Kind, "Huge16", PointerKind::Huge16); 269 IO.enumCase(Kind, "BasedOnSegment", PointerKind::BasedOnSegment); 270 IO.enumCase(Kind, "BasedOnValue", PointerKind::BasedOnValue); 271 IO.enumCase(Kind, "BasedOnSegmentValue", PointerKind::BasedOnSegmentValue); 272 IO.enumCase(Kind, "BasedOnAddress", PointerKind::BasedOnAddress); 273 IO.enumCase(Kind, "BasedOnSegmentAddress", 274 PointerKind::BasedOnSegmentAddress); 275 IO.enumCase(Kind, "BasedOnType", PointerKind::BasedOnType); 276 IO.enumCase(Kind, "BasedOnSelf", PointerKind::BasedOnSelf); 277 IO.enumCase(Kind, "Near32", PointerKind::Near32); 278 IO.enumCase(Kind, "Far32", PointerKind::Far32); 279 IO.enumCase(Kind, "Near64", PointerKind::Near64); 280 } 281 282 void ScalarEnumerationTraits<PointerMode>::enumeration(IO &IO, 283 PointerMode &Mode) { 284 IO.enumCase(Mode, "Pointer", PointerMode::Pointer); 285 IO.enumCase(Mode, "LValueReference", PointerMode::LValueReference); 286 IO.enumCase(Mode, "PointerToDataMember", PointerMode::PointerToDataMember); 287 IO.enumCase(Mode, "PointerToMemberFunction", 288 PointerMode::PointerToMemberFunction); 289 IO.enumCase(Mode, "RValueReference", PointerMode::RValueReference); 290 } 291 292 void ScalarEnumerationTraits<HfaKind>::enumeration(IO &IO, HfaKind &Value) { 293 IO.enumCase(Value, "None", HfaKind::None); 294 IO.enumCase(Value, "Float", HfaKind::Float); 295 IO.enumCase(Value, "Double", HfaKind::Double); 296 IO.enumCase(Value, "Other", HfaKind::Other); 297 } 298 299 void ScalarEnumerationTraits<MemberAccess>::enumeration(IO &IO, 300 MemberAccess &Access) { 301 IO.enumCase(Access, "None", MemberAccess::None); 302 IO.enumCase(Access, "Private", MemberAccess::Private); 303 IO.enumCase(Access, "Protected", MemberAccess::Protected); 304 IO.enumCase(Access, "Public", MemberAccess::Public); 305 } 306 307 void ScalarEnumerationTraits<MethodKind>::enumeration(IO &IO, 308 MethodKind &Kind) { 309 IO.enumCase(Kind, "Vanilla", MethodKind::Vanilla); 310 IO.enumCase(Kind, "Virtual", MethodKind::Virtual); 311 IO.enumCase(Kind, "Static", MethodKind::Static); 312 IO.enumCase(Kind, "Friend", MethodKind::Friend); 313 IO.enumCase(Kind, "IntroducingVirtual", MethodKind::IntroducingVirtual); 314 IO.enumCase(Kind, "PureVirtual", MethodKind::PureVirtual); 315 IO.enumCase(Kind, "PureIntroducingVirtual", 316 MethodKind::PureIntroducingVirtual); 317 } 318 319 void ScalarEnumerationTraits<WindowsRTClassKind>::enumeration( 320 IO &IO, WindowsRTClassKind &Value) { 321 IO.enumCase(Value, "None", WindowsRTClassKind::None); 322 IO.enumCase(Value, "Ref", WindowsRTClassKind::RefClass); 323 IO.enumCase(Value, "Value", WindowsRTClassKind::ValueClass); 324 IO.enumCase(Value, "Interface", WindowsRTClassKind::Interface); 325 } 326 327 void ScalarEnumerationTraits<LabelType>::enumeration(IO &IO, LabelType &Value) { 328 IO.enumCase(Value, "Near", LabelType::Near); 329 IO.enumCase(Value, "Far", LabelType::Far); 330 } 331 332 void ScalarBitSetTraits<PointerOptions>::bitset(IO &IO, 333 PointerOptions &Options) { 334 IO.bitSetCase(Options, "None", PointerOptions::None); 335 IO.bitSetCase(Options, "Flat32", PointerOptions::Flat32); 336 IO.bitSetCase(Options, "Volatile", PointerOptions::Volatile); 337 IO.bitSetCase(Options, "Const", PointerOptions::Const); 338 IO.bitSetCase(Options, "Unaligned", PointerOptions::Unaligned); 339 IO.bitSetCase(Options, "Restrict", PointerOptions::Restrict); 340 IO.bitSetCase(Options, "WinRTSmartPointer", 341 PointerOptions::WinRTSmartPointer); 342 } 343 344 void ScalarBitSetTraits<ModifierOptions>::bitset(IO &IO, 345 ModifierOptions &Options) { 346 IO.bitSetCase(Options, "None", ModifierOptions::None); 347 IO.bitSetCase(Options, "Const", ModifierOptions::Const); 348 IO.bitSetCase(Options, "Volatile", ModifierOptions::Volatile); 349 IO.bitSetCase(Options, "Unaligned", ModifierOptions::Unaligned); 350 } 351 352 void ScalarBitSetTraits<FunctionOptions>::bitset(IO &IO, 353 FunctionOptions &Options) { 354 IO.bitSetCase(Options, "None", FunctionOptions::None); 355 IO.bitSetCase(Options, "CxxReturnUdt", FunctionOptions::CxxReturnUdt); 356 IO.bitSetCase(Options, "Constructor", FunctionOptions::Constructor); 357 IO.bitSetCase(Options, "ConstructorWithVirtualBases", 358 FunctionOptions::ConstructorWithVirtualBases); 359 } 360 361 void ScalarBitSetTraits<ClassOptions>::bitset(IO &IO, ClassOptions &Options) { 362 IO.bitSetCase(Options, "None", ClassOptions::None); 363 IO.bitSetCase(Options, "HasConstructorOrDestructor", 364 ClassOptions::HasConstructorOrDestructor); 365 IO.bitSetCase(Options, "HasOverloadedOperator", 366 ClassOptions::HasOverloadedOperator); 367 IO.bitSetCase(Options, "Nested", ClassOptions::Nested); 368 IO.bitSetCase(Options, "ContainsNestedClass", 369 ClassOptions::ContainsNestedClass); 370 IO.bitSetCase(Options, "HasOverloadedAssignmentOperator", 371 ClassOptions::HasOverloadedAssignmentOperator); 372 IO.bitSetCase(Options, "HasConversionOperator", 373 ClassOptions::HasConversionOperator); 374 IO.bitSetCase(Options, "ForwardReference", ClassOptions::ForwardReference); 375 IO.bitSetCase(Options, "Scoped", ClassOptions::Scoped); 376 IO.bitSetCase(Options, "HasUniqueName", ClassOptions::HasUniqueName); 377 IO.bitSetCase(Options, "Sealed", ClassOptions::Sealed); 378 IO.bitSetCase(Options, "Intrinsic", ClassOptions::Intrinsic); 379 } 380 381 void ScalarBitSetTraits<MethodOptions>::bitset(IO &IO, MethodOptions &Options) { 382 IO.bitSetCase(Options, "None", MethodOptions::None); 383 IO.bitSetCase(Options, "Pseudo", MethodOptions::Pseudo); 384 IO.bitSetCase(Options, "NoInherit", MethodOptions::NoInherit); 385 IO.bitSetCase(Options, "NoConstruct", MethodOptions::NoConstruct); 386 IO.bitSetCase(Options, "CompilerGenerated", MethodOptions::CompilerGenerated); 387 IO.bitSetCase(Options, "Sealed", MethodOptions::Sealed); 388 } 389 390 void MappingTraits<MemberPointerInfo>::mapping(IO &IO, MemberPointerInfo &MPI) { 391 IO.mapRequired("ContainingType", MPI.ContainingType); 392 IO.mapRequired("Representation", MPI.Representation); 393 } 394 395 namespace llvm { 396 namespace CodeViewYAML { 397 namespace detail { 398 399 template <> void LeafRecordImpl<ModifierRecord>::map(IO &IO) { 400 IO.mapRequired("ModifiedType", Record.ModifiedType); 401 IO.mapRequired("Modifiers", Record.Modifiers); 402 } 403 404 template <> void LeafRecordImpl<ProcedureRecord>::map(IO &IO) { 405 IO.mapRequired("ReturnType", Record.ReturnType); 406 IO.mapRequired("CallConv", Record.CallConv); 407 IO.mapRequired("Options", Record.Options); 408 IO.mapRequired("ParameterCount", Record.ParameterCount); 409 IO.mapRequired("ArgumentList", Record.ArgumentList); 410 } 411 412 template <> void LeafRecordImpl<MemberFunctionRecord>::map(IO &IO) { 413 IO.mapRequired("ReturnType", Record.ReturnType); 414 IO.mapRequired("ClassType", Record.ClassType); 415 IO.mapRequired("ThisType", Record.ThisType); 416 IO.mapRequired("CallConv", Record.CallConv); 417 IO.mapRequired("Options", Record.Options); 418 IO.mapRequired("ParameterCount", Record.ParameterCount); 419 IO.mapRequired("ArgumentList", Record.ArgumentList); 420 IO.mapRequired("ThisPointerAdjustment", Record.ThisPointerAdjustment); 421 } 422 423 template <> void LeafRecordImpl<LabelRecord>::map(IO &IO) { 424 IO.mapRequired("Mode", Record.Mode); 425 } 426 427 template <> void LeafRecordImpl<MemberFuncIdRecord>::map(IO &IO) { 428 IO.mapRequired("ClassType", Record.ClassType); 429 IO.mapRequired("FunctionType", Record.FunctionType); 430 IO.mapRequired("Name", Record.Name); 431 } 432 433 template <> void LeafRecordImpl<ArgListRecord>::map(IO &IO) { 434 IO.mapRequired("ArgIndices", Record.ArgIndices); 435 } 436 437 template <> void LeafRecordImpl<StringListRecord>::map(IO &IO) { 438 IO.mapRequired("StringIndices", Record.StringIndices); 439 } 440 441 template <> void LeafRecordImpl<PointerRecord>::map(IO &IO) { 442 IO.mapRequired("ReferentType", Record.ReferentType); 443 IO.mapRequired("Attrs", Record.Attrs); 444 IO.mapOptional("MemberInfo", Record.MemberInfo); 445 } 446 447 template <> void LeafRecordImpl<ArrayRecord>::map(IO &IO) { 448 IO.mapRequired("ElementType", Record.ElementType); 449 IO.mapRequired("IndexType", Record.IndexType); 450 IO.mapRequired("Size", Record.Size); 451 IO.mapRequired("Name", Record.Name); 452 } 453 454 void LeafRecordImpl<FieldListRecord>::map(IO &IO) { 455 IO.mapRequired("FieldList", Members); 456 } 457 458 } // end namespace detail 459 } // end namespace CodeViewYAML 460 } // end namespace llvm 461 462 namespace { 463 464 class MemberRecordConversionVisitor : public TypeVisitorCallbacks { 465 public: 466 explicit MemberRecordConversionVisitor(std::vector<MemberRecord> &Records) 467 : Records(Records) {} 468 469 #define TYPE_RECORD(EnumName, EnumVal, Name) 470 #define MEMBER_RECORD(EnumName, EnumVal, Name) \ 471 Error visitKnownMember(CVMemberRecord &CVR, Name##Record &Record) override { \ 472 return visitKnownMemberImpl(Record); \ 473 } 474 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) 475 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) 476 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def" 477 private: 478 template <typename T> Error visitKnownMemberImpl(T &Record) { 479 TypeLeafKind K = static_cast<TypeLeafKind>(Record.getKind()); 480 auto Impl = std::make_shared<MemberRecordImpl<T>>(K); 481 Impl->Record = Record; 482 Records.push_back(MemberRecord{Impl}); 483 return Error::success(); 484 } 485 486 std::vector<MemberRecord> &Records; 487 }; 488 489 } // end anonymous namespace 490 491 Error LeafRecordImpl<FieldListRecord>::fromCodeViewRecord(CVType Type) { 492 MemberRecordConversionVisitor V(Members); 493 FieldListRecord FieldList; 494 cantFail(TypeDeserializer::deserializeAs<FieldListRecord>(Type, 495 FieldList)); 496 return visitMemberRecordStream(FieldList.Data, V); 497 } 498 499 CVType LeafRecordImpl<FieldListRecord>::toCodeViewRecord( 500 AppendingTypeTableBuilder &TS) const { 501 ContinuationRecordBuilder CRB; 502 CRB.begin(ContinuationRecordKind::FieldList); 503 for (const auto &Member : Members) { 504 Member.Member->writeTo(CRB); 505 } 506 TS.insertRecord(CRB); 507 return CVType(TS.records().back()); 508 } 509 510 void MappingTraits<OneMethodRecord>::mapping(IO &io, OneMethodRecord &Record) { 511 io.mapRequired("Type", Record.Type); 512 io.mapRequired("Attrs", Record.Attrs.Attrs); 513 io.mapRequired("VFTableOffset", Record.VFTableOffset); 514 io.mapRequired("Name", Record.Name); 515 } 516 517 namespace llvm { 518 namespace CodeViewYAML { 519 namespace detail { 520 521 template <> void LeafRecordImpl<ClassRecord>::map(IO &IO) { 522 IO.mapRequired("MemberCount", Record.MemberCount); 523 IO.mapRequired("Options", Record.Options); 524 IO.mapRequired("FieldList", Record.FieldList); 525 IO.mapRequired("Name", Record.Name); 526 IO.mapRequired("UniqueName", Record.UniqueName); 527 IO.mapRequired("DerivationList", Record.DerivationList); 528 IO.mapRequired("VTableShape", Record.VTableShape); 529 IO.mapRequired("Size", Record.Size); 530 } 531 532 template <> void LeafRecordImpl<UnionRecord>::map(IO &IO) { 533 IO.mapRequired("MemberCount", Record.MemberCount); 534 IO.mapRequired("Options", Record.Options); 535 IO.mapRequired("FieldList", Record.FieldList); 536 IO.mapRequired("Name", Record.Name); 537 IO.mapRequired("UniqueName", Record.UniqueName); 538 IO.mapRequired("Size", Record.Size); 539 } 540 541 template <> void LeafRecordImpl<EnumRecord>::map(IO &IO) { 542 IO.mapRequired("NumEnumerators", Record.MemberCount); 543 IO.mapRequired("Options", Record.Options); 544 IO.mapRequired("FieldList", Record.FieldList); 545 IO.mapRequired("Name", Record.Name); 546 IO.mapRequired("UniqueName", Record.UniqueName); 547 IO.mapRequired("UnderlyingType", Record.UnderlyingType); 548 } 549 550 template <> void LeafRecordImpl<BitFieldRecord>::map(IO &IO) { 551 IO.mapRequired("Type", Record.Type); 552 IO.mapRequired("BitSize", Record.BitSize); 553 IO.mapRequired("BitOffset", Record.BitOffset); 554 } 555 556 template <> void LeafRecordImpl<VFTableShapeRecord>::map(IO &IO) { 557 IO.mapRequired("Slots", Record.Slots); 558 } 559 560 template <> void LeafRecordImpl<TypeServer2Record>::map(IO &IO) { 561 IO.mapRequired("Guid", Record.Guid); 562 IO.mapRequired("Age", Record.Age); 563 IO.mapRequired("Name", Record.Name); 564 } 565 566 template <> void LeafRecordImpl<StringIdRecord>::map(IO &IO) { 567 IO.mapRequired("Id", Record.Id); 568 IO.mapRequired("String", Record.String); 569 } 570 571 template <> void LeafRecordImpl<FuncIdRecord>::map(IO &IO) { 572 IO.mapRequired("ParentScope", Record.ParentScope); 573 IO.mapRequired("FunctionType", Record.FunctionType); 574 IO.mapRequired("Name", Record.Name); 575 } 576 577 template <> void LeafRecordImpl<UdtSourceLineRecord>::map(IO &IO) { 578 IO.mapRequired("UDT", Record.UDT); 579 IO.mapRequired("SourceFile", Record.SourceFile); 580 IO.mapRequired("LineNumber", Record.LineNumber); 581 } 582 583 template <> void LeafRecordImpl<UdtModSourceLineRecord>::map(IO &IO) { 584 IO.mapRequired("UDT", Record.UDT); 585 IO.mapRequired("SourceFile", Record.SourceFile); 586 IO.mapRequired("LineNumber", Record.LineNumber); 587 IO.mapRequired("Module", Record.Module); 588 } 589 590 template <> void LeafRecordImpl<BuildInfoRecord>::map(IO &IO) { 591 IO.mapRequired("ArgIndices", Record.ArgIndices); 592 } 593 594 template <> void LeafRecordImpl<VFTableRecord>::map(IO &IO) { 595 IO.mapRequired("CompleteClass", Record.CompleteClass); 596 IO.mapRequired("OverriddenVFTable", Record.OverriddenVFTable); 597 IO.mapRequired("VFPtrOffset", Record.VFPtrOffset); 598 IO.mapRequired("MethodNames", Record.MethodNames); 599 } 600 601 template <> void LeafRecordImpl<MethodOverloadListRecord>::map(IO &IO) { 602 IO.mapRequired("Methods", Record.Methods); 603 } 604 605 template <> void LeafRecordImpl<PrecompRecord>::map(IO &IO) { 606 IO.mapRequired("StartTypeIndex", Record.StartTypeIndex); 607 IO.mapRequired("TypesCount", Record.TypesCount); 608 IO.mapRequired("Signature", Record.Signature); 609 IO.mapRequired("PrecompFilePath", Record.PrecompFilePath); 610 } 611 612 template <> void LeafRecordImpl<EndPrecompRecord>::map(IO &IO) { 613 IO.mapRequired("Signature", Record.Signature); 614 } 615 616 template <> void MemberRecordImpl<OneMethodRecord>::map(IO &IO) { 617 MappingTraits<OneMethodRecord>::mapping(IO, Record); 618 } 619 620 template <> void MemberRecordImpl<OverloadedMethodRecord>::map(IO &IO) { 621 IO.mapRequired("NumOverloads", Record.NumOverloads); 622 IO.mapRequired("MethodList", Record.MethodList); 623 IO.mapRequired("Name", Record.Name); 624 } 625 626 template <> void MemberRecordImpl<NestedTypeRecord>::map(IO &IO) { 627 IO.mapRequired("Type", Record.Type); 628 IO.mapRequired("Name", Record.Name); 629 } 630 631 template <> void MemberRecordImpl<DataMemberRecord>::map(IO &IO) { 632 IO.mapRequired("Attrs", Record.Attrs.Attrs); 633 IO.mapRequired("Type", Record.Type); 634 IO.mapRequired("FieldOffset", Record.FieldOffset); 635 IO.mapRequired("Name", Record.Name); 636 } 637 638 template <> void MemberRecordImpl<StaticDataMemberRecord>::map(IO &IO) { 639 IO.mapRequired("Attrs", Record.Attrs.Attrs); 640 IO.mapRequired("Type", Record.Type); 641 IO.mapRequired("Name", Record.Name); 642 } 643 644 template <> void MemberRecordImpl<EnumeratorRecord>::map(IO &IO) { 645 IO.mapRequired("Attrs", Record.Attrs.Attrs); 646 IO.mapRequired("Value", Record.Value); 647 IO.mapRequired("Name", Record.Name); 648 } 649 650 template <> void MemberRecordImpl<VFPtrRecord>::map(IO &IO) { 651 IO.mapRequired("Type", Record.Type); 652 } 653 654 template <> void MemberRecordImpl<BaseClassRecord>::map(IO &IO) { 655 IO.mapRequired("Attrs", Record.Attrs.Attrs); 656 IO.mapRequired("Type", Record.Type); 657 IO.mapRequired("Offset", Record.Offset); 658 } 659 660 template <> void MemberRecordImpl<VirtualBaseClassRecord>::map(IO &IO) { 661 IO.mapRequired("Attrs", Record.Attrs.Attrs); 662 IO.mapRequired("BaseType", Record.BaseType); 663 IO.mapRequired("VBPtrType", Record.VBPtrType); 664 IO.mapRequired("VBPtrOffset", Record.VBPtrOffset); 665 IO.mapRequired("VTableIndex", Record.VTableIndex); 666 } 667 668 template <> void MemberRecordImpl<ListContinuationRecord>::map(IO &IO) { 669 IO.mapRequired("ContinuationIndex", Record.ContinuationIndex); 670 } 671 672 } // end namespace detail 673 } // end namespace CodeViewYAML 674 } // end namespace llvm 675 676 template <typename T> 677 static inline Expected<LeafRecord> fromCodeViewRecordImpl(CVType Type) { 678 LeafRecord Result; 679 680 auto Impl = std::make_shared<LeafRecordImpl<T>>(Type.kind()); 681 if (auto EC = Impl->fromCodeViewRecord(Type)) 682 return std::move(EC); 683 Result.Leaf = Impl; 684 return Result; 685 } 686 687 Expected<LeafRecord> LeafRecord::fromCodeViewRecord(CVType Type) { 688 #define TYPE_RECORD(EnumName, EnumVal, ClassName) \ 689 case EnumName: \ 690 return fromCodeViewRecordImpl<ClassName##Record>(Type); 691 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) \ 692 TYPE_RECORD(EnumName, EnumVal, ClassName) 693 #define MEMBER_RECORD(EnumName, EnumVal, ClassName) 694 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) 695 switch (Type.kind()) { 696 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def" 697 default: 698 llvm_unreachable("Unknown leaf kind!"); 699 } 700 return make_error<CodeViewError>(cv_error_code::corrupt_record); 701 } 702 703 CVType 704 LeafRecord::toCodeViewRecord(AppendingTypeTableBuilder &Serializer) const { 705 return Leaf->toCodeViewRecord(Serializer); 706 } 707 708 namespace llvm { 709 namespace yaml { 710 711 template <> struct MappingTraits<LeafRecordBase> { 712 static void mapping(IO &io, LeafRecordBase &Record) { Record.map(io); } 713 }; 714 715 template <> struct MappingTraits<MemberRecordBase> { 716 static void mapping(IO &io, MemberRecordBase &Record) { Record.map(io); } 717 }; 718 719 } // end namespace yaml 720 } // end namespace llvm 721 722 template <typename ConcreteType> 723 static void mapLeafRecordImpl(IO &IO, const char *Class, TypeLeafKind Kind, 724 LeafRecord &Obj) { 725 if (!IO.outputting()) 726 Obj.Leaf = std::make_shared<LeafRecordImpl<ConcreteType>>(Kind); 727 728 if (Kind == LF_FIELDLIST) 729 Obj.Leaf->map(IO); 730 else 731 IO.mapRequired(Class, *Obj.Leaf); 732 } 733 734 void MappingTraits<LeafRecord>::mapping(IO &IO, LeafRecord &Obj) { 735 TypeLeafKind Kind; 736 if (IO.outputting()) 737 Kind = Obj.Leaf->Kind; 738 IO.mapRequired("Kind", Kind); 739 740 #define TYPE_RECORD(EnumName, EnumVal, ClassName) \ 741 case EnumName: \ 742 mapLeafRecordImpl<ClassName##Record>(IO, #ClassName, Kind, Obj); \ 743 break; 744 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) \ 745 TYPE_RECORD(EnumName, EnumVal, ClassName) 746 #define MEMBER_RECORD(EnumName, EnumVal, ClassName) 747 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) 748 switch (Kind) { 749 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def" 750 default: { llvm_unreachable("Unknown leaf kind!"); } 751 } 752 } 753 754 template <typename ConcreteType> 755 static void mapMemberRecordImpl(IO &IO, const char *Class, TypeLeafKind Kind, 756 MemberRecord &Obj) { 757 if (!IO.outputting()) 758 Obj.Member = std::make_shared<MemberRecordImpl<ConcreteType>>(Kind); 759 760 IO.mapRequired(Class, *Obj.Member); 761 } 762 763 void MappingTraits<MemberRecord>::mapping(IO &IO, MemberRecord &Obj) { 764 TypeLeafKind Kind; 765 if (IO.outputting()) 766 Kind = Obj.Member->Kind; 767 IO.mapRequired("Kind", Kind); 768 769 #define MEMBER_RECORD(EnumName, EnumVal, ClassName) \ 770 case EnumName: \ 771 mapMemberRecordImpl<ClassName##Record>(IO, #ClassName, Kind, Obj); \ 772 break; 773 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) \ 774 MEMBER_RECORD(EnumName, EnumVal, ClassName) 775 #define TYPE_RECORD(EnumName, EnumVal, ClassName) 776 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, AliasName, ClassName) 777 switch (Kind) { 778 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def" 779 default: { llvm_unreachable("Unknown member kind!"); } 780 } 781 } 782 783 std::vector<LeafRecord> 784 llvm::CodeViewYAML::fromDebugT(ArrayRef<uint8_t> DebugTorP, 785 StringRef SectionName) { 786 ExitOnError Err("Invalid " + std::string(SectionName) + " section!"); 787 BinaryStreamReader Reader(DebugTorP, llvm::endianness::little); 788 CVTypeArray Types; 789 uint32_t Magic; 790 791 Err(Reader.readInteger(Magic)); 792 assert(Magic == COFF::DEBUG_SECTION_MAGIC && 793 "Invalid .debug$T or .debug$P section!"); 794 795 std::vector<LeafRecord> Result; 796 Err(Reader.readArray(Types, Reader.bytesRemaining())); 797 for (const auto &T : Types) { 798 auto CVT = Err(LeafRecord::fromCodeViewRecord(T)); 799 Result.push_back(CVT); 800 } 801 return Result; 802 } 803 804 ArrayRef<uint8_t> llvm::CodeViewYAML::toDebugT(ArrayRef<LeafRecord> Leafs, 805 BumpPtrAllocator &Alloc, 806 StringRef SectionName) { 807 AppendingTypeTableBuilder TS(Alloc); 808 uint32_t Size = sizeof(uint32_t); 809 for (const auto &Leaf : Leafs) { 810 CVType T = Leaf.Leaf->toCodeViewRecord(TS); 811 Size += T.length(); 812 assert(T.length() % 4 == 0 && "Improper type record alignment!"); 813 } 814 uint8_t *ResultBuffer = Alloc.Allocate<uint8_t>(Size); 815 MutableArrayRef<uint8_t> Output(ResultBuffer, Size); 816 BinaryStreamWriter Writer(Output, llvm::endianness::little); 817 ExitOnError Err("Error writing type record to " + std::string(SectionName) + 818 " section"); 819 Err(Writer.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC)); 820 for (const auto &R : TS.records()) { 821 Err(Writer.writeBytes(R)); 822 } 823 assert(Writer.bytesRemaining() == 0 && "Didn't write all type record bytes!"); 824 return Output; 825 } 826