Home | History | Annotate | Download | only in ObjectYAML
      1 //===- COFFYAML.h - COFF YAMLIO implementation ------------------*- C++ -*-===//
      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 declares classes for handling the YAML representation of COFF.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_OBJECTYAML_COFFYAML_H
     15 #define LLVM_OBJECTYAML_COFFYAML_H
     16 
     17 #include "llvm/ADT/Optional.h"
     18 #include "llvm/BinaryFormat/COFF.h"
     19 #include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
     20 #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
     21 #include "llvm/ObjectYAML/YAML.h"
     22 
     23 namespace llvm {
     24 
     25 namespace COFF {
     26 inline Characteristics operator|(Characteristics a, Characteristics b) {
     27   uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
     28   return static_cast<Characteristics>(Ret);
     29 }
     30 
     31 inline SectionCharacteristics operator|(SectionCharacteristics a,
     32                                         SectionCharacteristics b) {
     33   uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
     34   return static_cast<SectionCharacteristics>(Ret);
     35 }
     36 
     37 inline DLLCharacteristics operator|(DLLCharacteristics a,
     38                                     DLLCharacteristics b) {
     39   uint16_t Ret = static_cast<uint16_t>(a) | static_cast<uint16_t>(b);
     40   return static_cast<DLLCharacteristics>(Ret);
     41 }
     42 }
     43 
     44 // The structure of the yaml files is not an exact 1:1 match to COFF. In order
     45 // to use yaml::IO, we use these structures which are closer to the source.
     46 namespace COFFYAML {
     47   LLVM_YAML_STRONG_TYPEDEF(uint8_t, COMDATType)
     48   LLVM_YAML_STRONG_TYPEDEF(uint32_t, WeakExternalCharacteristics)
     49   LLVM_YAML_STRONG_TYPEDEF(uint8_t, AuxSymbolType)
     50 
     51   struct Relocation {
     52     uint32_t VirtualAddress;
     53     uint16_t Type;
     54     StringRef SymbolName;
     55   };
     56 
     57   struct Section {
     58     COFF::section Header;
     59     unsigned Alignment = 0;
     60     yaml::BinaryRef SectionData;
     61     std::vector<CodeViewYAML::YAMLDebugSubsection> DebugS;
     62     std::vector<CodeViewYAML::LeafRecord> DebugT;
     63     std::vector<Relocation> Relocations;
     64     StringRef Name;
     65     Section();
     66   };
     67 
     68   struct Symbol {
     69     COFF::symbol Header;
     70     COFF::SymbolBaseType SimpleType = COFF::IMAGE_SYM_TYPE_NULL;
     71     COFF::SymbolComplexType ComplexType = COFF::IMAGE_SYM_DTYPE_NULL;
     72     Optional<COFF::AuxiliaryFunctionDefinition> FunctionDefinition;
     73     Optional<COFF::AuxiliarybfAndefSymbol> bfAndefSymbol;
     74     Optional<COFF::AuxiliaryWeakExternal> WeakExternal;
     75     StringRef File;
     76     Optional<COFF::AuxiliarySectionDefinition> SectionDefinition;
     77     Optional<COFF::AuxiliaryCLRToken> CLRToken;
     78     StringRef Name;
     79     Symbol();
     80   };
     81 
     82   struct PEHeader {
     83     COFF::PE32Header Header;
     84     Optional<COFF::DataDirectory> DataDirectories[COFF::NUM_DATA_DIRECTORIES];
     85   };
     86 
     87   struct Object {
     88     Optional<PEHeader> OptionalHeader;
     89     COFF::header Header;
     90     std::vector<Section> Sections;
     91     std::vector<Symbol> Symbols;
     92     Object();
     93   };
     94 }
     95 }
     96 
     97 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
     98 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
     99 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
    100 
    101 namespace llvm {
    102 namespace yaml {
    103 
    104 template <>
    105 struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
    106   static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
    107 };
    108 
    109 template <>
    110 struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
    111   static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
    112 };
    113 
    114 template <>
    115 struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
    116   static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
    117 };
    118 
    119 template <>
    120 struct ScalarEnumerationTraits<COFF::MachineTypes> {
    121   static void enumeration(IO &IO, COFF::MachineTypes &Value);
    122 };
    123 
    124 template <>
    125 struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
    126   static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
    127 };
    128 
    129 template <>
    130 struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
    131   static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
    132 };
    133 
    134 template <>
    135 struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
    136   static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
    137 };
    138 
    139 template <>
    140 struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
    141   static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
    142 };
    143 
    144 template <>
    145 struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
    146   static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
    147 };
    148 
    149 template <>
    150 struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
    151   static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
    152 };
    153 
    154 template <>
    155 struct ScalarBitSetTraits<COFF::Characteristics> {
    156   static void bitset(IO &IO, COFF::Characteristics &Value);
    157 };
    158 
    159 template <>
    160 struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
    161   static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
    162 };
    163 
    164 template <>
    165 struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
    166   static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
    167 };
    168 
    169 template <>
    170 struct MappingTraits<COFFYAML::Relocation> {
    171   static void mapping(IO &IO, COFFYAML::Relocation &Rel);
    172 };
    173 
    174 template <>
    175 struct MappingTraits<COFFYAML::PEHeader> {
    176   static void mapping(IO &IO, COFFYAML::PEHeader &PH);
    177 };
    178 
    179 template <>
    180 struct MappingTraits<COFF::DataDirectory> {
    181   static void mapping(IO &IO, COFF::DataDirectory &DD);
    182 };
    183 
    184 template <>
    185 struct MappingTraits<COFF::header> {
    186   static void mapping(IO &IO, COFF::header &H);
    187 };
    188 
    189 template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
    190   static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
    191 };
    192 
    193 template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
    194   static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
    195 };
    196 
    197 template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
    198   static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
    199 };
    200 
    201 template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
    202   static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
    203 };
    204 
    205 template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
    206   static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
    207 };
    208 
    209 template <>
    210 struct MappingTraits<COFFYAML::Symbol> {
    211   static void mapping(IO &IO, COFFYAML::Symbol &S);
    212 };
    213 
    214 template <>
    215 struct MappingTraits<COFFYAML::Section> {
    216   static void mapping(IO &IO, COFFYAML::Section &Sec);
    217 };
    218 
    219 template <>
    220 struct MappingTraits<COFFYAML::Object> {
    221   static void mapping(IO &IO, COFFYAML::Object &Obj);
    222 };
    223 
    224 } // end namespace yaml
    225 } // end namespace llvm
    226 
    227 #endif
    228