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