LLVM  6.0.0svn
COFFYAML.cpp
Go to the documentation of this file.
1 //===- COFFYAML.cpp - COFF YAMLIO implementation --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines classes for handling the YAML representation of COFF.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/StringRef.h"
17 #include <cstdint>
18 #include <cstring>
19 
20 #define ECase(X) IO.enumCase(Value, #X, COFF::X);
21 
22 namespace llvm {
23 
24 namespace COFFYAML {
25 
26 Section::Section() { memset(&Header, 0, sizeof(COFF::section)); }
27 Symbol::Symbol() { memset(&Header, 0, sizeof(COFF::symbol)); }
28 Object::Object() { memset(&Header, 0, sizeof(COFF::header)); }
29 
30 } // end namespace COFFYAML
31 
32 namespace yaml {
33 
36  IO.enumCase(Value, "0", 0);
44 }
45 
46 void
49  IO.enumCase(Value, "0", 0);
53 }
54 
58 }
59 
84 }
85 
104 }
105 
135 }
136 
143 }
144 
158 }
159 
179 }
180 
198 }
199 
219 }
220 
237 }
238 #undef ECase
239 
240 #define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
258 }
259 
284 }
285 
299 }
300 #undef BCase
301 
302 namespace {
303 
304 struct NSectionSelectionType {
305  NSectionSelectionType(IO &)
307  NSectionSelectionType(IO &, uint8_t C)
309 
310  uint8_t denormalize(IO &) { return SelectionType; }
311 
313 };
314 
315 struct NWeakExternalCharacteristics {
316  NWeakExternalCharacteristics(IO &)
318  NWeakExternalCharacteristics(IO &, uint32_t C)
320 
321  uint32_t denormalize(IO &) { return Characteristics; }
322 
324 };
325 
326 struct NSectionCharacteristics {
327  NSectionCharacteristics(IO &)
329  NSectionCharacteristics(IO &, uint32_t C)
331 
332  uint32_t denormalize(IO &) { return Characteristics; }
333 
335 };
336 
337 struct NAuxTokenType {
338  NAuxTokenType(IO &)
340  NAuxTokenType(IO &, uint8_t C)
342 
343  uint32_t denormalize(IO &) { return AuxType; }
344 
346 };
347 
348 struct NStorageClass {
349  NStorageClass(IO &) : StorageClass(COFF::SymbolStorageClass(0)) {}
350  NStorageClass(IO &, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {}
351 
352  uint8_t denormalize(IO &) { return StorageClass; }
353 
355 };
356 
357 struct NMachine {
358  NMachine(IO &) : Machine(COFF::MachineTypes(0)) {}
359  NMachine(IO &, uint16_t M) : Machine(COFF::MachineTypes(M)) {}
360 
361  uint16_t denormalize(IO &) { return Machine; }
362 
364 };
365 
366 struct NHeaderCharacteristics {
367  NHeaderCharacteristics(IO &) : Characteristics(COFF::Characteristics(0)) {}
368  NHeaderCharacteristics(IO &, uint16_t C)
370 
371  uint16_t denormalize(IO &) { return Characteristics; }
372 
374 };
375 
376 template <typename RelocType>
377 struct NType {
378  NType(IO &) : Type(RelocType(0)) {}
379  NType(IO &, uint16_t T) : Type(RelocType(T)) {}
380 
381  uint16_t denormalize(IO &) { return Type; }
382 
383  RelocType Type;
384 };
385 
386 struct NWindowsSubsystem {
387  NWindowsSubsystem(IO &) : Subsystem(COFF::WindowsSubsystem(0)) {}
388  NWindowsSubsystem(IO &, uint16_t C) : Subsystem(COFF::WindowsSubsystem(C)) {}
389 
390  uint16_t denormalize(IO &) { return Subsystem; }
391 
393 };
394 
395 struct NDLLCharacteristics {
396  NDLLCharacteristics(IO &) : Characteristics(COFF::DLLCharacteristics(0)) {}
397  NDLLCharacteristics(IO &, uint16_t C)
399 
400  uint16_t denormalize(IO &) { return Characteristics; }
401 
403 };
404 
405 } // end anonymous namespace
406 
408  COFFYAML::Relocation &Rel) {
409  IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
410  IO.mapRequired("SymbolName", Rel.SymbolName);
411 
412  COFF::header &H = *static_cast<COFF::header *>(IO.getContext());
415  IO, Rel.Type);
416  IO.mapRequired("Type", NT->Type);
417  } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) {
419  IO, Rel.Type);
420  IO.mapRequired("Type", NT->Type);
421  } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
423  IO, Rel.Type);
424  IO.mapRequired("Type", NT->Type);
425  } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_ARM64) {
427  IO, Rel.Type);
428  IO.mapRequired("Type", NT->Type);
429  } else {
430  IO.mapRequired("Type", Rel.Type);
431  }
432 }
433 
435  COFF::DataDirectory &DD) {
436  IO.mapRequired("RelativeVirtualAddress", DD.RelativeVirtualAddress);
437  IO.mapRequired("Size", DD.Size);
438 }
439 
441  COFFYAML::PEHeader &PH) {
443  PH.Header.Subsystem);
445  IO, PH.Header.DLLCharacteristics);
446 
447  IO.mapRequired("AddressOfEntryPoint", PH.Header.AddressOfEntryPoint);
448  IO.mapRequired("ImageBase", PH.Header.ImageBase);
449  IO.mapRequired("SectionAlignment", PH.Header.SectionAlignment);
450  IO.mapRequired("FileAlignment", PH.Header.FileAlignment);
451  IO.mapRequired("MajorOperatingSystemVersion",
453  IO.mapRequired("MinorOperatingSystemVersion",
455  IO.mapRequired("MajorImageVersion", PH.Header.MajorImageVersion);
456  IO.mapRequired("MinorImageVersion", PH.Header.MinorImageVersion);
457  IO.mapRequired("MajorSubsystemVersion", PH.Header.MajorSubsystemVersion);
458  IO.mapRequired("MinorSubsystemVersion", PH.Header.MinorSubsystemVersion);
459  IO.mapRequired("Subsystem", NWS->Subsystem);
460  IO.mapRequired("DLLCharacteristics", NDC->Characteristics);
461  IO.mapRequired("SizeOfStackReserve", PH.Header.SizeOfStackReserve);
462  IO.mapRequired("SizeOfStackCommit", PH.Header.SizeOfStackCommit);
463  IO.mapRequired("SizeOfHeapReserve", PH.Header.SizeOfHeapReserve);
464  IO.mapRequired("SizeOfHeapCommit", PH.Header.SizeOfHeapCommit);
465 
466  IO.mapOptional("ExportTable", PH.DataDirectories[COFF::EXPORT_TABLE]);
467  IO.mapOptional("ImportTable", PH.DataDirectories[COFF::IMPORT_TABLE]);
468  IO.mapOptional("ResourceTable", PH.DataDirectories[COFF::RESOURCE_TABLE]);
469  IO.mapOptional("ExceptionTable", PH.DataDirectories[COFF::EXCEPTION_TABLE]);
470  IO.mapOptional("CertificateTable", PH.DataDirectories[COFF::CERTIFICATE_TABLE]);
471  IO.mapOptional("BaseRelocationTable",
474  IO.mapOptional("Architecture", PH.DataDirectories[COFF::ARCHITECTURE]);
475  IO.mapOptional("GlobalPtr", PH.DataDirectories[COFF::GLOBAL_PTR]);
476  IO.mapOptional("TlsTable", PH.DataDirectories[COFF::TLS_TABLE]);
477  IO.mapOptional("LoadConfigTable",
479  IO.mapOptional("BoundImport", PH.DataDirectories[COFF::BOUND_IMPORT]);
480  IO.mapOptional("IAT", PH.DataDirectories[COFF::IAT]);
481  IO.mapOptional("DelayImportDescriptor",
483  IO.mapOptional("ClrRuntimeHeader",
485 }
486 
490  H.Characteristics);
491 
492  IO.mapRequired("Machine", NM->Machine);
493  IO.mapOptional("Characteristics", NC->Characteristics);
494  IO.setContext(static_cast<void *>(&H));
495 }
496 
499  IO.mapRequired("TagIndex", AFD.TagIndex);
500  IO.mapRequired("TotalSize", AFD.TotalSize);
501  IO.mapRequired("PointerToLinenumber", AFD.PointerToLinenumber);
502  IO.mapRequired("PointerToNextFunction", AFD.PointerToNextFunction);
503 }
504 
507  IO.mapRequired("Linenumber", AAS.Linenumber);
508  IO.mapRequired("PointerToNextFunction", AAS.PointerToNextFunction);
509 }
510 
514  IO, AWE.Characteristics);
515  IO.mapRequired("TagIndex", AWE.TagIndex);
516  IO.mapRequired("Characteristics", NWEC->Characteristics);
517 }
518 
522  IO, ASD.Selection);
523 
524  IO.mapRequired("Length", ASD.Length);
525  IO.mapRequired("NumberOfRelocations", ASD.NumberOfRelocations);
526  IO.mapRequired("NumberOfLinenumbers", ASD.NumberOfLinenumbers);
527  IO.mapRequired("CheckSum", ASD.CheckSum);
528  IO.mapRequired("Number", ASD.Number);
529  IO.mapOptional("Selection", NSST->SelectionType, COFFYAML::COMDATType(0));
530 }
531 
533  IO &IO, COFF::AuxiliaryCLRToken &ACT) {
535  IO.mapRequired("AuxType", NATT->AuxType);
536  IO.mapRequired("SymbolTableIndex", ACT.SymbolTableIndex);
537 }
538 
541 
542  IO.mapRequired("Name", S.Name);
543  IO.mapRequired("Value", S.Header.Value);
544  IO.mapRequired("SectionNumber", S.Header.SectionNumber);
545  IO.mapRequired("SimpleType", S.SimpleType);
546  IO.mapRequired("ComplexType", S.ComplexType);
547  IO.mapRequired("StorageClass", NS->StorageClass);
548  IO.mapOptional("FunctionDefinition", S.FunctionDefinition);
549  IO.mapOptional("bfAndefSymbol", S.bfAndefSymbol);
550  IO.mapOptional("WeakExternal", S.WeakExternal);
551  IO.mapOptional("File", S.File, StringRef());
552  IO.mapOptional("SectionDefinition", S.SectionDefinition);
553  IO.mapOptional("CLRToken", S.CLRToken);
554 }
555 
558  IO, Sec.Header.Characteristics);
559  IO.mapRequired("Name", Sec.Name);
560  IO.mapRequired("Characteristics", NC->Characteristics);
561  IO.mapOptional("VirtualAddress", Sec.Header.VirtualAddress, 0U);
562  IO.mapOptional("VirtualSize", Sec.Header.VirtualSize, 0U);
563  IO.mapOptional("Alignment", Sec.Alignment, 0U);
564 
565  // If this is a .debug$S or .debug$T section parse the semantic representation
566  // of the symbols/types. If it is any other kind of section, just deal in raw
567  // bytes.
568  IO.mapOptional("SectionData", Sec.SectionData);
569  if (Sec.Name == ".debug$S")
570  IO.mapOptional("Subsections", Sec.DebugS);
571  else if (Sec.Name == ".debug$T")
572  IO.mapOptional("Types", Sec.DebugT);
573 
574  IO.mapOptional("Relocations", Sec.Relocations);
575 }
576 
578  IO.mapTag("!COFF", true);
579  IO.mapOptional("OptionalHeader", Obj.OptionalHeader);
580  IO.mapRequired("header", Obj.Header);
581  IO.mapRequired("sections", Obj.Sections);
582  IO.mapRequired("symbols", Obj.Symbols);
583 }
584 
585 } // end namespace yaml
586 
587 } // end namespace llvm
Characteristics
Definition: COFF.h:121
uint64_t CallInst * C
COFF::section Header
Definition: COFFYAML.h:64
A byte; unsigned 1-byte integer.
Definition: COFF.h:250
void mapOptional(const char *Key, T &Val)
Definition: YAMLTraits.h:656
COFF::PE32Header Header
Definition: COFFYAML.h:91
COFF::SymbolBaseType SimpleType
Definition: COFFYAML.h:77
The Windows character subsystem.
Definition: COFF.h:599
RelocationTypesARM64
Definition: COFF.h:380
RelocationTypesARM
Definition: COFF.h:362
Image can handle > 2GiB addresses.
Definition: COFF.h:138
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
int32_t SectionNumber
Definition: COFF.h:188
A pointer to base type.
Definition: COFF.h:258
uint32_t Value
Definition: COFF.h:187
Physical end of function.
Definition: COFF.h:204
Device drivers and native Windows processes.
Definition: COFF.h:597
Optional< COFF::AuxiliarybfAndefSymbol > bfAndefSymbol
Definition: COFFYAML.h:80
A 4-byte signed integer.
Definition: COFF.h:243
uint32_t SectionAlignment
Definition: COFF.h:546
COFF::symbol Header
Definition: COFFYAML.h:76
uint32_t SizeOfHeapReserve
Definition: COFF.h:563
void * getContext()
Definition: YAMLTraits.cpp:43
Aggressively trim working set. This is deprecated and must be 0.
Definition: COFF.h:136
AuxSymbolType
Definition: COFF.h:266
Image supports Control Flow Guard.
Definition: COFF.h:633
Isolation aware, but do not isolate the image.
Definition: COFF.h:624
COFF::SymbolStorageClass StorageClass
Definition: COFFYAML.cpp:354
uint16_t Machine
Definition: COFF.h:67
The Windows GUI subsystem.
Definition: COFF.h:598
uint32_t ImageBase
Definition: COFF.h:545
uint16_t MinorSubsystemVersion
Definition: COFF.h:553
COFF::header Header
Definition: COFFYAML.h:97
COFF symbol table entries for local symbols have been removed.
Definition: COFF.h:134
uint16_t Characteristics
Definition: COFF.h:73
".bf" or ".ef" - beginning or end of function
Definition: COFF.h:227
SymbolComplexType
Definition: COFF.h:256
An enumerated type.
Definition: COFF.h:248
No complex type; simple scalar variable.
Definition: COFF.h:257
uint32_t SizeOfStackReserve
Definition: COFF.h:561
ASLR with 64 bit address space.
Definition: COFF.h:616
std::vector< Relocation > Relocations
Definition: COFFYAML.h:69
A 2-byte signed integer.
Definition: COFF.h:241
uint32_t FileAlignment
Definition: COFF.h:547
uint16_t MajorOperatingSystemVersion
Definition: COFF.h:548
External symbol in dmert public lib.
Definition: COFF.h:234
std::vector< Section > Sections
Definition: COFFYAML.h:98
An unsigned integer of natural size.
Definition: COFF.h:252
SymbolBaseType
Definition: COFF.h:237
An unknown subsystem.
Definition: COFF.h:596
A 4-byte floating-point number.
Definition: COFF.h:244
WindowsSubsystem
Definition: COFF.h:595
This class should be specialized by any type that needs to be converted to/from a YAML mapping...
COFFYAML::COMDATType SelectionType
Definition: COFFYAML.cpp:312
An array of base type.
Definition: COFF.h:260
An EFI ROM image.
Definition: COFF.h:609
uint32_t AddressOfEntryPoint
Definition: COFF.h:542
The OS/2 character subsytem.
Definition: COFF.h:600
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:363
uint32_t SizeOfHeapCommit
Definition: COFF.h:564
virtual bool mapTag(StringRef Tag, bool Default=false)=0
Used with void pointers and functions.
Definition: COFF.h:239
SectionCharacteristics
Definition: COFF.h:281
void enumCase(T &Val, const char *Str, const T ConstVal)
Definition: YAMLTraits.h:590
Machine is based on a 32bit word architecture.
Definition: COFF.h:143
An EFI driver with run-time services.
Definition: COFF.h:607
An EFI driver with boot services.
Definition: COFF.h:605
No type information or unknown base type.
Definition: COFF.h:238
uint16_t MinorOperatingSystemVersion
Definition: COFF.h:549
COFF::WindowsSubsystem Subsystem
Definition: COFFYAML.cpp:392
Line number, reformatted as symbol.
Definition: COFF.h:231
#define ECase(X)
Definition: COFFYAML.cpp:20
An 8-byte floating-point number.
Definition: COFF.h:245
The image file is a DLL.
Definition: COFF.h:153
#define H(x, y, z)
Definition: MD5.cpp:57
Member of enumeration.
Definition: COFF.h:221
Register variable.
Definition: COFF.h:209
A function that returns a base type.
Definition: COFF.h:259
uint16_t Subsystem
Definition: COFF.h:558
uint16_t DLLCharacteristics
Definition: COFF.h:560
If the image is on removable media, fully load it and copy it to swap.
Definition: COFF.h:147
uint32_t VirtualAddress
Definition: COFF.h:271
RelocationTypeI386
Definition: COFF.h:328
A word; unsigned 2-byte integer.
Definition: COFF.h:251
uint16_t MajorSubsystemVersion
Definition: COFF.h:552
yaml::BinaryRef SectionData
Definition: COFFYAML.h:66
Little endian: the LSB precedes the MSB in memory.
Definition: COFF.h:141
An unsigned 4-byte integer.
Definition: COFF.h:253
Optional< COFF::AuxiliaryFunctionDefinition > FunctionDefinition
Definition: COFFYAML.h:79
COFFYAML::AuxSymbolType AuxType
Definition: COFFYAML.cpp:345
This file should only be run on a uniprocessor machine.
Definition: COFF.h:155
DLL can be relocated at load time.
Definition: COFF.h:618
std::vector< CodeViewYAML::LeafRecord > DebugT
Definition: COFFYAML.h:68
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition: YAMLTraits.h:98
Optional< COFF::AuxiliaryCLRToken > CLRToken
Definition: COFFYAML.h:84
If the image is on network media, fully load it and copy it to swap.
Definition: COFF.h:149
Debugging info has been removed.
Definition: COFF.h:145
RelocationTypeAMD64
Definition: COFF.h:342
#define NC
Definition: regutils.h:42
uint32_t RelativeVirtualAddress
Definition: COFF.h:571
uint32_t VirtualSize
Definition: COFF.h:270
SymbolStorageClass
Storage class tells where and what the symbol represents.
Definition: COFF.h:201
".bb" or ".eb" - beginning or end of block
Definition: COFF.h:225
COMDATType
Definition: COFF.h:400
void setContext(void *)
Definition: YAMLTraits.cpp:47
COFF line numbers have been stripped.
Definition: COFF.h:131
Function argument.
Definition: COFF.h:214
Optional< COFF::DataDirectory > DataDirectories[COFF::NUM_DATA_DIRECTORIES]
Definition: COFFYAML.h:92
DLLCharacteristics
Definition: COFF.h:614
Big endian: the MSB precedes the LSB in memory.
Definition: COFF.h:158
A character (signed byte).
Definition: COFF.h:240
The POSIX character subsystem.
Definition: COFF.h:601
Code integrity checks are enforced.
Definition: COFF.h:620
Optional< COFF::AuxiliaryWeakExternal > WeakExternal
Definition: COFFYAML.h:81
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:323
Does not use structured exception handling (SEH).
Definition: COFF.h:627
uint32_t SizeOfStackCommit
Definition: COFF.h:562
The image file is a system file, not a user program.
Definition: COFF.h:151
MachineTypes
Definition: COFF.h:94
This class should be specialized by any integer type that is a union of bit values and the YAML repre...
Definition: YAMLTraits.h:115
uint8_t StorageClass
Definition: COFF.h:190
std::vector< CodeViewYAML::YAMLDebugSubsection > DebugS
Definition: COFFYAML.h:67
uint16_t MinorImageVersion
Definition: COFF.h:551
LLVM Value Representation.
Definition: Value.h:73
std::vector< Symbol > Symbols
Definition: COFFYAML.h:99
RelocType Type
Definition: COFFYAML.cpp:383
uint16_t MajorImageVersion
Definition: COFF.h:550
A member of enumeration (a specific value).
Definition: COFF.h:249
#define BCase(X)
Definition: COFFYAML.cpp:240
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
The file does not contain base relocations and must be loaded at its preferred base.
Definition: COFF.h:126
void mapRequired(const char *Key, T &Val)
Definition: YAMLTraits.h:646
COFF::SymbolComplexType ComplexType
Definition: COFFYAML.h:78
Optional< PEHeader > OptionalHeader
Definition: COFFYAML.h:96
WeakExternalCharacteristics
Definition: COFF.h:433
uint32_t Characteristics
Definition: COFF.h:278
The file is valid and can be run.
Definition: COFF.h:128
Optional< COFF::AuxiliarySectionDefinition > SectionDefinition
Definition: COFFYAML.h:83
Native Windows 9x driver.
Definition: COFF.h:602
A natural integer type on the target.
Definition: COFF.h:242