1 //===- COFFYAML.cpp - COFF YAMLIO 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 COFF. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ObjectYAML/COFFYAML.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/Support/YAMLTraits.h" 16 #include <cstdint> 17 #include <cstring> 18 19 #define ECase(X) IO.enumCase(Value, #X, COFF::X); 20 21 namespace llvm { 22 23 namespace COFFYAML { 24 25 Section::Section() { memset(&Header, 0, sizeof(COFF::section)); } 26 Symbol::Symbol() { memset(&Header, 0, sizeof(COFF::symbol)); } 27 Object::Object() { memset(&Header, 0, sizeof(COFF::header)); } 28 29 } // end namespace COFFYAML 30 31 namespace yaml { 32 33 void ScalarEnumerationTraits<COFFYAML::COMDATType>::enumeration( 34 IO &IO, COFFYAML::COMDATType &Value) { 35 IO.enumCase(Value, "0", 0); 36 ECase(IMAGE_COMDAT_SELECT_NODUPLICATES); 37 ECase(IMAGE_COMDAT_SELECT_ANY); 38 ECase(IMAGE_COMDAT_SELECT_SAME_SIZE); 39 ECase(IMAGE_COMDAT_SELECT_EXACT_MATCH); 40 ECase(IMAGE_COMDAT_SELECT_ASSOCIATIVE); 41 ECase(IMAGE_COMDAT_SELECT_LARGEST); 42 ECase(IMAGE_COMDAT_SELECT_NEWEST); 43 } 44 45 void 46 ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics>::enumeration( 47 IO &IO, COFFYAML::WeakExternalCharacteristics &Value) { 48 IO.enumCase(Value, "0", 0); 49 ECase(IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY); 50 ECase(IMAGE_WEAK_EXTERN_SEARCH_LIBRARY); 51 ECase(IMAGE_WEAK_EXTERN_SEARCH_ALIAS); 52 ECase(IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY); 53 } 54 55 void ScalarEnumerationTraits<COFFYAML::AuxSymbolType>::enumeration( 56 IO &IO, COFFYAML::AuxSymbolType &Value) { 57 ECase(IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF); 58 } 59 60 void ScalarEnumerationTraits<COFF::MachineTypes>::enumeration( 61 IO &IO, COFF::MachineTypes &Value) { 62 ECase(IMAGE_FILE_MACHINE_UNKNOWN); 63 ECase(IMAGE_FILE_MACHINE_AM33); 64 ECase(IMAGE_FILE_MACHINE_AMD64); 65 ECase(IMAGE_FILE_MACHINE_ARM); 66 ECase(IMAGE_FILE_MACHINE_ARMNT); 67 ECase(IMAGE_FILE_MACHINE_ARM64); 68 ECase(IMAGE_FILE_MACHINE_ARM64EC); 69 ECase(IMAGE_FILE_MACHINE_EBC); 70 ECase(IMAGE_FILE_MACHINE_I386); 71 ECase(IMAGE_FILE_MACHINE_IA64); 72 ECase(IMAGE_FILE_MACHINE_M32R); 73 ECase(IMAGE_FILE_MACHINE_MIPS16); 74 ECase(IMAGE_FILE_MACHINE_MIPSFPU); 75 ECase(IMAGE_FILE_MACHINE_MIPSFPU16); 76 ECase(IMAGE_FILE_MACHINE_POWERPC); 77 ECase(IMAGE_FILE_MACHINE_POWERPCFP); 78 ECase(IMAGE_FILE_MACHINE_R4000); 79 ECase(IMAGE_FILE_MACHINE_RISCV32); 80 ECase(IMAGE_FILE_MACHINE_RISCV64); 81 ECase(IMAGE_FILE_MACHINE_RISCV128); 82 ECase(IMAGE_FILE_MACHINE_SH3); 83 ECase(IMAGE_FILE_MACHINE_SH3DSP); 84 ECase(IMAGE_FILE_MACHINE_SH4); 85 ECase(IMAGE_FILE_MACHINE_SH5); 86 ECase(IMAGE_FILE_MACHINE_THUMB); 87 ECase(IMAGE_FILE_MACHINE_WCEMIPSV2); 88 } 89 90 void ScalarEnumerationTraits<COFF::SymbolBaseType>::enumeration( 91 IO &IO, COFF::SymbolBaseType &Value) { 92 ECase(IMAGE_SYM_TYPE_NULL); 93 ECase(IMAGE_SYM_TYPE_VOID); 94 ECase(IMAGE_SYM_TYPE_CHAR); 95 ECase(IMAGE_SYM_TYPE_SHORT); 96 ECase(IMAGE_SYM_TYPE_INT); 97 ECase(IMAGE_SYM_TYPE_LONG); 98 ECase(IMAGE_SYM_TYPE_FLOAT); 99 ECase(IMAGE_SYM_TYPE_DOUBLE); 100 ECase(IMAGE_SYM_TYPE_STRUCT); 101 ECase(IMAGE_SYM_TYPE_UNION); 102 ECase(IMAGE_SYM_TYPE_ENUM); 103 ECase(IMAGE_SYM_TYPE_MOE); 104 ECase(IMAGE_SYM_TYPE_BYTE); 105 ECase(IMAGE_SYM_TYPE_WORD); 106 ECase(IMAGE_SYM_TYPE_UINT); 107 ECase(IMAGE_SYM_TYPE_DWORD); 108 } 109 110 void ScalarEnumerationTraits<COFF::SymbolStorageClass>::enumeration( 111 IO &IO, COFF::SymbolStorageClass &Value) { 112 ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION); 113 ECase(IMAGE_SYM_CLASS_NULL); 114 ECase(IMAGE_SYM_CLASS_AUTOMATIC); 115 ECase(IMAGE_SYM_CLASS_EXTERNAL); 116 ECase(IMAGE_SYM_CLASS_STATIC); 117 ECase(IMAGE_SYM_CLASS_REGISTER); 118 ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF); 119 ECase(IMAGE_SYM_CLASS_LABEL); 120 ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL); 121 ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT); 122 ECase(IMAGE_SYM_CLASS_ARGUMENT); 123 ECase(IMAGE_SYM_CLASS_STRUCT_TAG); 124 ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION); 125 ECase(IMAGE_SYM_CLASS_UNION_TAG); 126 ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION); 127 ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC); 128 ECase(IMAGE_SYM_CLASS_ENUM_TAG); 129 ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM); 130 ECase(IMAGE_SYM_CLASS_REGISTER_PARAM); 131 ECase(IMAGE_SYM_CLASS_BIT_FIELD); 132 ECase(IMAGE_SYM_CLASS_BLOCK); 133 ECase(IMAGE_SYM_CLASS_FUNCTION); 134 ECase(IMAGE_SYM_CLASS_END_OF_STRUCT); 135 ECase(IMAGE_SYM_CLASS_FILE); 136 ECase(IMAGE_SYM_CLASS_SECTION); 137 ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL); 138 ECase(IMAGE_SYM_CLASS_CLR_TOKEN); 139 } 140 141 void ScalarEnumerationTraits<COFF::SymbolComplexType>::enumeration( 142 IO &IO, COFF::SymbolComplexType &Value) { 143 ECase(IMAGE_SYM_DTYPE_NULL); 144 ECase(IMAGE_SYM_DTYPE_POINTER); 145 ECase(IMAGE_SYM_DTYPE_FUNCTION); 146 ECase(IMAGE_SYM_DTYPE_ARRAY); 147 } 148 149 void ScalarEnumerationTraits<COFF::RelocationTypeI386>::enumeration( 150 IO &IO, COFF::RelocationTypeI386 &Value) { 151 ECase(IMAGE_REL_I386_ABSOLUTE); 152 ECase(IMAGE_REL_I386_DIR16); 153 ECase(IMAGE_REL_I386_REL16); 154 ECase(IMAGE_REL_I386_DIR32); 155 ECase(IMAGE_REL_I386_DIR32NB); 156 ECase(IMAGE_REL_I386_SEG12); 157 ECase(IMAGE_REL_I386_SECTION); 158 ECase(IMAGE_REL_I386_SECREL); 159 ECase(IMAGE_REL_I386_TOKEN); 160 ECase(IMAGE_REL_I386_SECREL7); 161 ECase(IMAGE_REL_I386_REL32); 162 } 163 164 void ScalarEnumerationTraits<COFF::RelocationTypeAMD64>::enumeration( 165 IO &IO, COFF::RelocationTypeAMD64 &Value) { 166 ECase(IMAGE_REL_AMD64_ABSOLUTE); 167 ECase(IMAGE_REL_AMD64_ADDR64); 168 ECase(IMAGE_REL_AMD64_ADDR32); 169 ECase(IMAGE_REL_AMD64_ADDR32NB); 170 ECase(IMAGE_REL_AMD64_REL32); 171 ECase(IMAGE_REL_AMD64_REL32_1); 172 ECase(IMAGE_REL_AMD64_REL32_2); 173 ECase(IMAGE_REL_AMD64_REL32_3); 174 ECase(IMAGE_REL_AMD64_REL32_4); 175 ECase(IMAGE_REL_AMD64_REL32_5); 176 ECase(IMAGE_REL_AMD64_SECTION); 177 ECase(IMAGE_REL_AMD64_SECREL); 178 ECase(IMAGE_REL_AMD64_SECREL7); 179 ECase(IMAGE_REL_AMD64_TOKEN); 180 ECase(IMAGE_REL_AMD64_SREL32); 181 ECase(IMAGE_REL_AMD64_PAIR); 182 ECase(IMAGE_REL_AMD64_SSPAN32); 183 } 184 185 void ScalarEnumerationTraits<COFF::RelocationTypesARM>::enumeration( 186 IO &IO, COFF::RelocationTypesARM &Value) { 187 ECase(IMAGE_REL_ARM_ABSOLUTE); 188 ECase(IMAGE_REL_ARM_ADDR32); 189 ECase(IMAGE_REL_ARM_ADDR32NB); 190 ECase(IMAGE_REL_ARM_BRANCH24); 191 ECase(IMAGE_REL_ARM_BRANCH11); 192 ECase(IMAGE_REL_ARM_TOKEN); 193 ECase(IMAGE_REL_ARM_BLX24); 194 ECase(IMAGE_REL_ARM_BLX11); 195 ECase(IMAGE_REL_ARM_REL32); 196 ECase(IMAGE_REL_ARM_SECTION); 197 ECase(IMAGE_REL_ARM_SECREL); 198 ECase(IMAGE_REL_ARM_MOV32A); 199 ECase(IMAGE_REL_ARM_MOV32T); 200 ECase(IMAGE_REL_ARM_BRANCH20T); 201 ECase(IMAGE_REL_ARM_BRANCH24T); 202 ECase(IMAGE_REL_ARM_BLX23T); 203 ECase(IMAGE_REL_ARM_PAIR); 204 } 205 206 void ScalarEnumerationTraits<COFF::RelocationTypesARM64>::enumeration( 207 IO &IO, COFF::RelocationTypesARM64 &Value) { 208 ECase(IMAGE_REL_ARM64_ABSOLUTE); 209 ECase(IMAGE_REL_ARM64_ADDR32); 210 ECase(IMAGE_REL_ARM64_ADDR32NB); 211 ECase(IMAGE_REL_ARM64_BRANCH26); 212 ECase(IMAGE_REL_ARM64_PAGEBASE_REL21); 213 ECase(IMAGE_REL_ARM64_REL21); 214 ECase(IMAGE_REL_ARM64_PAGEOFFSET_12A); 215 ECase(IMAGE_REL_ARM64_PAGEOFFSET_12L); 216 ECase(IMAGE_REL_ARM64_SECREL); 217 ECase(IMAGE_REL_ARM64_SECREL_LOW12A); 218 ECase(IMAGE_REL_ARM64_SECREL_HIGH12A); 219 ECase(IMAGE_REL_ARM64_SECREL_LOW12L); 220 ECase(IMAGE_REL_ARM64_TOKEN); 221 ECase(IMAGE_REL_ARM64_SECTION); 222 ECase(IMAGE_REL_ARM64_ADDR64); 223 ECase(IMAGE_REL_ARM64_BRANCH19); 224 ECase(IMAGE_REL_ARM64_BRANCH14); 225 ECase(IMAGE_REL_ARM64_REL32); 226 } 227 228 void ScalarEnumerationTraits<COFF::WindowsSubsystem>::enumeration( 229 IO &IO, COFF::WindowsSubsystem &Value) { 230 ECase(IMAGE_SUBSYSTEM_UNKNOWN); 231 ECase(IMAGE_SUBSYSTEM_NATIVE); 232 ECase(IMAGE_SUBSYSTEM_WINDOWS_GUI); 233 ECase(IMAGE_SUBSYSTEM_WINDOWS_CUI); 234 ECase(IMAGE_SUBSYSTEM_OS2_CUI); 235 ECase(IMAGE_SUBSYSTEM_POSIX_CUI); 236 ECase(IMAGE_SUBSYSTEM_NATIVE_WINDOWS); 237 ECase(IMAGE_SUBSYSTEM_WINDOWS_CE_GUI); 238 ECase(IMAGE_SUBSYSTEM_EFI_APPLICATION); 239 ECase(IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER); 240 ECase(IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER); 241 ECase(IMAGE_SUBSYSTEM_EFI_ROM); 242 ECase(IMAGE_SUBSYSTEM_XBOX); 243 ECase(IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION); 244 } 245 #undef ECase 246 247 #define BCase(X) IO.bitSetCase(Value, #X, COFF::X); 248 void ScalarBitSetTraits<COFF::Characteristics>::bitset( 249 IO &IO, COFF::Characteristics &Value) { 250 BCase(IMAGE_FILE_RELOCS_STRIPPED); 251 BCase(IMAGE_FILE_EXECUTABLE_IMAGE); 252 BCase(IMAGE_FILE_LINE_NUMS_STRIPPED); 253 BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED); 254 BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM); 255 BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE); 256 BCase(IMAGE_FILE_BYTES_REVERSED_LO); 257 BCase(IMAGE_FILE_32BIT_MACHINE); 258 BCase(IMAGE_FILE_DEBUG_STRIPPED); 259 BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP); 260 BCase(IMAGE_FILE_NET_RUN_FROM_SWAP); 261 BCase(IMAGE_FILE_SYSTEM); 262 BCase(IMAGE_FILE_DLL); 263 BCase(IMAGE_FILE_UP_SYSTEM_ONLY); 264 BCase(IMAGE_FILE_BYTES_REVERSED_HI); 265 } 266 267 void ScalarBitSetTraits<COFF::SectionCharacteristics>::bitset( 268 IO &IO, COFF::SectionCharacteristics &Value) { 269 BCase(IMAGE_SCN_TYPE_NOLOAD); 270 BCase(IMAGE_SCN_TYPE_NO_PAD); 271 BCase(IMAGE_SCN_CNT_CODE); 272 BCase(IMAGE_SCN_CNT_INITIALIZED_DATA); 273 BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA); 274 BCase(IMAGE_SCN_LNK_OTHER); 275 BCase(IMAGE_SCN_LNK_INFO); 276 BCase(IMAGE_SCN_LNK_REMOVE); 277 BCase(IMAGE_SCN_LNK_COMDAT); 278 BCase(IMAGE_SCN_GPREL); 279 BCase(IMAGE_SCN_MEM_PURGEABLE); 280 BCase(IMAGE_SCN_MEM_16BIT); 281 BCase(IMAGE_SCN_MEM_LOCKED); 282 BCase(IMAGE_SCN_MEM_PRELOAD); 283 BCase(IMAGE_SCN_LNK_NRELOC_OVFL); 284 BCase(IMAGE_SCN_MEM_DISCARDABLE); 285 BCase(IMAGE_SCN_MEM_NOT_CACHED); 286 BCase(IMAGE_SCN_MEM_NOT_PAGED); 287 BCase(IMAGE_SCN_MEM_SHARED); 288 BCase(IMAGE_SCN_MEM_EXECUTE); 289 BCase(IMAGE_SCN_MEM_READ); 290 BCase(IMAGE_SCN_MEM_WRITE); 291 } 292 293 void ScalarBitSetTraits<COFF::DLLCharacteristics>::bitset( 294 IO &IO, COFF::DLLCharacteristics &Value) { 295 BCase(IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA); 296 BCase(IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE); 297 BCase(IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY); 298 BCase(IMAGE_DLL_CHARACTERISTICS_NX_COMPAT); 299 BCase(IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION); 300 BCase(IMAGE_DLL_CHARACTERISTICS_NO_SEH); 301 BCase(IMAGE_DLL_CHARACTERISTICS_NO_BIND); 302 BCase(IMAGE_DLL_CHARACTERISTICS_APPCONTAINER); 303 BCase(IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER); 304 BCase(IMAGE_DLL_CHARACTERISTICS_GUARD_CF); 305 BCase(IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE); 306 } 307 #undef BCase 308 309 namespace { 310 311 struct NSectionSelectionType { 312 NSectionSelectionType(IO &) 313 : SelectionType(COFFYAML::COMDATType(0)) {} 314 NSectionSelectionType(IO &, uint8_t C) 315 : SelectionType(COFFYAML::COMDATType(C)) {} 316 317 uint8_t denormalize(IO &) { return SelectionType; } 318 319 COFFYAML::COMDATType SelectionType; 320 }; 321 322 struct NWeakExternalCharacteristics { 323 NWeakExternalCharacteristics(IO &) 324 : Characteristics(COFFYAML::WeakExternalCharacteristics(0)) {} 325 NWeakExternalCharacteristics(IO &, uint32_t C) 326 : Characteristics(COFFYAML::WeakExternalCharacteristics(C)) {} 327 328 uint32_t denormalize(IO &) { return Characteristics; } 329 330 COFFYAML::WeakExternalCharacteristics Characteristics; 331 }; 332 333 struct NSectionCharacteristics { 334 NSectionCharacteristics(IO &) 335 : Characteristics(COFF::SectionCharacteristics(0)) {} 336 NSectionCharacteristics(IO &, uint32_t C) 337 : Characteristics(COFF::SectionCharacteristics(C)) {} 338 339 uint32_t denormalize(IO &) { return Characteristics; } 340 341 COFF::SectionCharacteristics Characteristics; 342 }; 343 344 struct NAuxTokenType { 345 NAuxTokenType(IO &) 346 : AuxType(COFFYAML::AuxSymbolType(0)) {} 347 NAuxTokenType(IO &, uint8_t C) 348 : AuxType(COFFYAML::AuxSymbolType(C)) {} 349 350 uint32_t denormalize(IO &) { return AuxType; } 351 352 COFFYAML::AuxSymbolType AuxType; 353 }; 354 355 struct NStorageClass { 356 NStorageClass(IO &) : StorageClass(COFF::SymbolStorageClass(0)) {} 357 NStorageClass(IO &, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {} 358 359 uint8_t denormalize(IO &) { return StorageClass; } 360 361 COFF::SymbolStorageClass StorageClass; 362 }; 363 364 struct NMachine { 365 NMachine(IO &) : Machine(COFF::MachineTypes(0)) {} 366 NMachine(IO &, uint16_t M) : Machine(COFF::MachineTypes(M)) {} 367 368 uint16_t denormalize(IO &) { return Machine; } 369 370 COFF::MachineTypes Machine; 371 }; 372 373 struct NHeaderCharacteristics { 374 NHeaderCharacteristics(IO &) : Characteristics(COFF::Characteristics(0)) {} 375 NHeaderCharacteristics(IO &, uint16_t C) 376 : Characteristics(COFF::Characteristics(C)) {} 377 378 uint16_t denormalize(IO &) { return Characteristics; } 379 380 COFF::Characteristics Characteristics; 381 }; 382 383 template <typename RelocType> 384 struct NType { 385 NType(IO &) : Type(RelocType(0)) {} 386 NType(IO &, uint16_t T) : Type(RelocType(T)) {} 387 388 uint16_t denormalize(IO &) { return Type; } 389 390 RelocType Type; 391 }; 392 393 struct NWindowsSubsystem { 394 NWindowsSubsystem(IO &) : Subsystem(COFF::WindowsSubsystem(0)) {} 395 NWindowsSubsystem(IO &, uint16_t C) : Subsystem(COFF::WindowsSubsystem(C)) {} 396 397 uint16_t denormalize(IO &) { return Subsystem; } 398 399 COFF::WindowsSubsystem Subsystem; 400 }; 401 402 struct NDLLCharacteristics { 403 NDLLCharacteristics(IO &) : Characteristics(COFF::DLLCharacteristics(0)) {} 404 NDLLCharacteristics(IO &, uint16_t C) 405 : Characteristics(COFF::DLLCharacteristics(C)) {} 406 407 uint16_t denormalize(IO &) { return Characteristics; } 408 409 COFF::DLLCharacteristics Characteristics; 410 }; 411 412 } // end anonymous namespace 413 414 void MappingTraits<COFFYAML::Relocation>::mapping(IO &IO, 415 COFFYAML::Relocation &Rel) { 416 IO.mapRequired("VirtualAddress", Rel.VirtualAddress); 417 IO.mapOptional("SymbolName", Rel.SymbolName, StringRef()); 418 IO.mapOptional("SymbolTableIndex", Rel.SymbolTableIndex); 419 420 COFF::header &H = *static_cast<COFF::header *>(IO.getContext()); 421 if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) { 422 MappingNormalization<NType<COFF::RelocationTypeI386>, uint16_t> NT( 423 IO, Rel.Type); 424 IO.mapRequired("Type", NT->Type); 425 } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) { 426 MappingNormalization<NType<COFF::RelocationTypeAMD64>, uint16_t> NT( 427 IO, Rel.Type); 428 IO.mapRequired("Type", NT->Type); 429 } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) { 430 MappingNormalization<NType<COFF::RelocationTypesARM>, uint16_t> NT( 431 IO, Rel.Type); 432 IO.mapRequired("Type", NT->Type); 433 } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARM64 || 434 H.Machine == COFF::IMAGE_FILE_MACHINE_ARM64EC) { 435 MappingNormalization<NType<COFF::RelocationTypesARM64>, uint16_t> NT( 436 IO, Rel.Type); 437 IO.mapRequired("Type", NT->Type); 438 } else { 439 IO.mapRequired("Type", Rel.Type); 440 } 441 } 442 443 void MappingTraits<COFF::DataDirectory>::mapping(IO &IO, 444 COFF::DataDirectory &DD) { 445 IO.mapRequired("RelativeVirtualAddress", DD.RelativeVirtualAddress); 446 IO.mapRequired("Size", DD.Size); 447 } 448 449 void MappingTraits<COFFYAML::PEHeader>::mapping(IO &IO, 450 COFFYAML::PEHeader &PH) { 451 MappingNormalization<NWindowsSubsystem, uint16_t> NWS(IO, 452 PH.Header.Subsystem); 453 MappingNormalization<NDLLCharacteristics, uint16_t> NDC( 454 IO, PH.Header.DLLCharacteristics); 455 456 IO.mapOptional("AddressOfEntryPoint", PH.Header.AddressOfEntryPoint); 457 IO.mapOptional("ImageBase", PH.Header.ImageBase); 458 IO.mapOptional("SectionAlignment", PH.Header.SectionAlignment, 1); 459 IO.mapOptional("FileAlignment", PH.Header.FileAlignment, 1); 460 IO.mapOptional("MajorOperatingSystemVersion", 461 PH.Header.MajorOperatingSystemVersion); 462 IO.mapOptional("MinorOperatingSystemVersion", 463 PH.Header.MinorOperatingSystemVersion); 464 IO.mapOptional("MajorImageVersion", PH.Header.MajorImageVersion); 465 IO.mapOptional("MinorImageVersion", PH.Header.MinorImageVersion); 466 IO.mapOptional("MajorSubsystemVersion", PH.Header.MajorSubsystemVersion); 467 IO.mapOptional("MinorSubsystemVersion", PH.Header.MinorSubsystemVersion); 468 IO.mapOptional("Subsystem", NWS->Subsystem); 469 IO.mapOptional("DLLCharacteristics", NDC->Characteristics); 470 IO.mapOptional("SizeOfStackReserve", PH.Header.SizeOfStackReserve); 471 IO.mapOptional("SizeOfStackCommit", PH.Header.SizeOfStackCommit); 472 IO.mapOptional("SizeOfHeapReserve", PH.Header.SizeOfHeapReserve); 473 IO.mapOptional("SizeOfHeapCommit", PH.Header.SizeOfHeapCommit); 474 475 IO.mapOptional("NumberOfRvaAndSize", PH.Header.NumberOfRvaAndSize, 476 COFF::NUM_DATA_DIRECTORIES + 1); 477 IO.mapOptional("ExportTable", PH.DataDirectories[COFF::EXPORT_TABLE]); 478 IO.mapOptional("ImportTable", PH.DataDirectories[COFF::IMPORT_TABLE]); 479 IO.mapOptional("ResourceTable", PH.DataDirectories[COFF::RESOURCE_TABLE]); 480 IO.mapOptional("ExceptionTable", PH.DataDirectories[COFF::EXCEPTION_TABLE]); 481 IO.mapOptional("CertificateTable", PH.DataDirectories[COFF::CERTIFICATE_TABLE]); 482 IO.mapOptional("BaseRelocationTable", 483 PH.DataDirectories[COFF::BASE_RELOCATION_TABLE]); 484 IO.mapOptional("Debug", PH.DataDirectories[COFF::DEBUG_DIRECTORY]); 485 IO.mapOptional("Architecture", PH.DataDirectories[COFF::ARCHITECTURE]); 486 IO.mapOptional("GlobalPtr", PH.DataDirectories[COFF::GLOBAL_PTR]); 487 IO.mapOptional("TlsTable", PH.DataDirectories[COFF::TLS_TABLE]); 488 IO.mapOptional("LoadConfigTable", 489 PH.DataDirectories[COFF::LOAD_CONFIG_TABLE]); 490 IO.mapOptional("BoundImport", PH.DataDirectories[COFF::BOUND_IMPORT]); 491 IO.mapOptional("IAT", PH.DataDirectories[COFF::IAT]); 492 IO.mapOptional("DelayImportDescriptor", 493 PH.DataDirectories[COFF::DELAY_IMPORT_DESCRIPTOR]); 494 IO.mapOptional("ClrRuntimeHeader", 495 PH.DataDirectories[COFF::CLR_RUNTIME_HEADER]); 496 } 497 498 void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) { 499 MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine); 500 MappingNormalization<NHeaderCharacteristics, uint16_t> NC(IO, 501 H.Characteristics); 502 503 IO.mapRequired("Machine", NM->Machine); 504 IO.mapOptional("Characteristics", NC->Characteristics); 505 IO.setContext(static_cast<void *>(&H)); 506 } 507 508 void MappingTraits<COFF::AuxiliaryFunctionDefinition>::mapping( 509 IO &IO, COFF::AuxiliaryFunctionDefinition &AFD) { 510 IO.mapRequired("TagIndex", AFD.TagIndex); 511 IO.mapRequired("TotalSize", AFD.TotalSize); 512 IO.mapRequired("PointerToLinenumber", AFD.PointerToLinenumber); 513 IO.mapRequired("PointerToNextFunction", AFD.PointerToNextFunction); 514 } 515 516 void MappingTraits<COFF::AuxiliarybfAndefSymbol>::mapping( 517 IO &IO, COFF::AuxiliarybfAndefSymbol &AAS) { 518 IO.mapRequired("Linenumber", AAS.Linenumber); 519 IO.mapRequired("PointerToNextFunction", AAS.PointerToNextFunction); 520 } 521 522 void MappingTraits<COFF::AuxiliaryWeakExternal>::mapping( 523 IO &IO, COFF::AuxiliaryWeakExternal &AWE) { 524 MappingNormalization<NWeakExternalCharacteristics, uint32_t> NWEC( 525 IO, AWE.Characteristics); 526 IO.mapRequired("TagIndex", AWE.TagIndex); 527 IO.mapRequired("Characteristics", NWEC->Characteristics); 528 } 529 530 void MappingTraits<COFF::AuxiliarySectionDefinition>::mapping( 531 IO &IO, COFF::AuxiliarySectionDefinition &ASD) { 532 MappingNormalization<NSectionSelectionType, uint8_t> NSST( 533 IO, ASD.Selection); 534 535 IO.mapRequired("Length", ASD.Length); 536 IO.mapRequired("NumberOfRelocations", ASD.NumberOfRelocations); 537 IO.mapRequired("NumberOfLinenumbers", ASD.NumberOfLinenumbers); 538 IO.mapRequired("CheckSum", ASD.CheckSum); 539 IO.mapRequired("Number", ASD.Number); 540 IO.mapOptional("Selection", NSST->SelectionType, COFFYAML::COMDATType(0)); 541 } 542 543 void MappingTraits<COFF::AuxiliaryCLRToken>::mapping( 544 IO &IO, COFF::AuxiliaryCLRToken &ACT) { 545 MappingNormalization<NAuxTokenType, uint8_t> NATT(IO, ACT.AuxType); 546 IO.mapRequired("AuxType", NATT->AuxType); 547 IO.mapRequired("SymbolTableIndex", ACT.SymbolTableIndex); 548 } 549 550 void MappingTraits<COFFYAML::Symbol>::mapping(IO &IO, COFFYAML::Symbol &S) { 551 MappingNormalization<NStorageClass, uint8_t> NS(IO, S.Header.StorageClass); 552 553 IO.mapRequired("Name", S.Name); 554 IO.mapRequired("Value", S.Header.Value); 555 IO.mapRequired("SectionNumber", S.Header.SectionNumber); 556 IO.mapRequired("SimpleType", S.SimpleType); 557 IO.mapRequired("ComplexType", S.ComplexType); 558 IO.mapRequired("StorageClass", NS->StorageClass); 559 IO.mapOptional("FunctionDefinition", S.FunctionDefinition); 560 IO.mapOptional("bfAndefSymbol", S.bfAndefSymbol); 561 IO.mapOptional("WeakExternal", S.WeakExternal); 562 IO.mapOptional("File", S.File, StringRef()); 563 IO.mapOptional("SectionDefinition", S.SectionDefinition); 564 IO.mapOptional("CLRToken", S.CLRToken); 565 } 566 567 void MappingTraits<COFFYAML::Section>::mapping(IO &IO, COFFYAML::Section &Sec) { 568 MappingNormalization<NSectionCharacteristics, uint32_t> NC( 569 IO, Sec.Header.Characteristics); 570 IO.mapRequired("Name", Sec.Name); 571 IO.mapRequired("Characteristics", NC->Characteristics); 572 IO.mapOptional("VirtualAddress", Sec.Header.VirtualAddress, 0U); 573 IO.mapOptional("VirtualSize", Sec.Header.VirtualSize, 0U); 574 IO.mapOptional("Alignment", Sec.Alignment, 0U); 575 576 // If this is a .debug$S .debug$T .debug$P, or .debug$H section parse the 577 // semantic representation of the symbols/types. If it is any other kind 578 // of section, just deal in raw bytes. 579 IO.mapOptional("SectionData", Sec.SectionData); 580 if (Sec.Name == ".debug$S") 581 IO.mapOptional("Subsections", Sec.DebugS); 582 else if (Sec.Name == ".debug$T") 583 IO.mapOptional("Types", Sec.DebugT); 584 else if (Sec.Name == ".debug$P") 585 IO.mapOptional("PrecompTypes", Sec.DebugP); 586 else if (Sec.Name == ".debug$H") 587 IO.mapOptional("GlobalHashes", Sec.DebugH); 588 589 // Uninitialized sections, such as .bss, typically have no data, but the size 590 // is carried in SizeOfRawData, even though PointerToRawData is zero. 591 if (Sec.SectionData.binary_size() == 0 && 592 NC->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) 593 IO.mapOptional("SizeOfRawData", Sec.Header.SizeOfRawData); 594 595 IO.mapOptional("Relocations", Sec.Relocations); 596 } 597 598 void MappingTraits<COFFYAML::Object>::mapping(IO &IO, COFFYAML::Object &Obj) { 599 IO.mapTag("!COFF", true); 600 IO.mapOptional("OptionalHeader", Obj.OptionalHeader); 601 IO.mapRequired("header", Obj.Header); 602 IO.mapRequired("sections", Obj.Sections); 603 IO.mapRequired("symbols", Obj.Symbols); 604 } 605 606 } // end namespace yaml 607 608 } // end namespace llvm 609