Home | History | Annotate | Download | only in Object
      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 
     14 #include "llvm/Object/COFFYAML.h"
     15 
     16 #define ECase(X) IO.enumCase(Value, #X, COFF::X);
     17 namespace llvm {
     18 
     19 namespace COFFYAML {
     20 Section::Section() { memset(&Header, 0, sizeof(COFF::section)); }
     21 Symbol::Symbol() { memset(&Header, 0, sizeof(COFF::symbol)); }
     22 Object::Object() { memset(&Header, 0, sizeof(COFF::header)); }
     23 }
     24 
     25 namespace yaml {
     26 void ScalarEnumerationTraits<COFF::MachineTypes>::enumeration(
     27     IO &IO, COFF::MachineTypes &Value) {
     28   ECase(IMAGE_FILE_MACHINE_UNKNOWN);
     29   ECase(IMAGE_FILE_MACHINE_AM33);
     30   ECase(IMAGE_FILE_MACHINE_AMD64);
     31   ECase(IMAGE_FILE_MACHINE_ARM);
     32   ECase(IMAGE_FILE_MACHINE_ARMV7);
     33   ECase(IMAGE_FILE_MACHINE_EBC);
     34   ECase(IMAGE_FILE_MACHINE_I386);
     35   ECase(IMAGE_FILE_MACHINE_IA64);
     36   ECase(IMAGE_FILE_MACHINE_M32R);
     37   ECase(IMAGE_FILE_MACHINE_MIPS16);
     38   ECase(IMAGE_FILE_MACHINE_MIPSFPU);
     39   ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
     40   ECase(IMAGE_FILE_MACHINE_POWERPC);
     41   ECase(IMAGE_FILE_MACHINE_POWERPCFP);
     42   ECase(IMAGE_FILE_MACHINE_R4000);
     43   ECase(IMAGE_FILE_MACHINE_SH3);
     44   ECase(IMAGE_FILE_MACHINE_SH3DSP);
     45   ECase(IMAGE_FILE_MACHINE_SH4);
     46   ECase(IMAGE_FILE_MACHINE_SH5);
     47   ECase(IMAGE_FILE_MACHINE_THUMB);
     48   ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
     49 }
     50 
     51 void ScalarEnumerationTraits<COFF::SymbolBaseType>::enumeration(
     52     IO &IO, COFF::SymbolBaseType &Value) {
     53   ECase(IMAGE_SYM_TYPE_NULL);
     54   ECase(IMAGE_SYM_TYPE_VOID);
     55   ECase(IMAGE_SYM_TYPE_CHAR);
     56   ECase(IMAGE_SYM_TYPE_SHORT);
     57   ECase(IMAGE_SYM_TYPE_INT);
     58   ECase(IMAGE_SYM_TYPE_LONG);
     59   ECase(IMAGE_SYM_TYPE_FLOAT);
     60   ECase(IMAGE_SYM_TYPE_DOUBLE);
     61   ECase(IMAGE_SYM_TYPE_STRUCT);
     62   ECase(IMAGE_SYM_TYPE_UNION);
     63   ECase(IMAGE_SYM_TYPE_ENUM);
     64   ECase(IMAGE_SYM_TYPE_MOE);
     65   ECase(IMAGE_SYM_TYPE_BYTE);
     66   ECase(IMAGE_SYM_TYPE_WORD);
     67   ECase(IMAGE_SYM_TYPE_UINT);
     68   ECase(IMAGE_SYM_TYPE_DWORD);
     69 }
     70 
     71 void ScalarEnumerationTraits<COFF::SymbolStorageClass>::enumeration(
     72     IO &IO, COFF::SymbolStorageClass &Value) {
     73   ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
     74   ECase(IMAGE_SYM_CLASS_NULL);
     75   ECase(IMAGE_SYM_CLASS_AUTOMATIC);
     76   ECase(IMAGE_SYM_CLASS_EXTERNAL);
     77   ECase(IMAGE_SYM_CLASS_STATIC);
     78   ECase(IMAGE_SYM_CLASS_REGISTER);
     79   ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
     80   ECase(IMAGE_SYM_CLASS_LABEL);
     81   ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
     82   ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
     83   ECase(IMAGE_SYM_CLASS_ARGUMENT);
     84   ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
     85   ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
     86   ECase(IMAGE_SYM_CLASS_UNION_TAG);
     87   ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
     88   ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
     89   ECase(IMAGE_SYM_CLASS_ENUM_TAG);
     90   ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
     91   ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
     92   ECase(IMAGE_SYM_CLASS_BIT_FIELD);
     93   ECase(IMAGE_SYM_CLASS_BLOCK);
     94   ECase(IMAGE_SYM_CLASS_FUNCTION);
     95   ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
     96   ECase(IMAGE_SYM_CLASS_FILE);
     97   ECase(IMAGE_SYM_CLASS_SECTION);
     98   ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
     99   ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
    100 }
    101 
    102 void ScalarEnumerationTraits<COFF::SymbolComplexType>::enumeration(
    103     IO &IO, COFF::SymbolComplexType &Value) {
    104   ECase(IMAGE_SYM_DTYPE_NULL);
    105   ECase(IMAGE_SYM_DTYPE_POINTER);
    106   ECase(IMAGE_SYM_DTYPE_FUNCTION);
    107   ECase(IMAGE_SYM_DTYPE_ARRAY);
    108 }
    109 
    110 void ScalarEnumerationTraits<COFF::RelocationTypeX86>::enumeration(
    111     IO &IO, COFF::RelocationTypeX86 &Value) {
    112   ECase(IMAGE_REL_I386_ABSOLUTE);
    113   ECase(IMAGE_REL_I386_DIR16);
    114   ECase(IMAGE_REL_I386_REL16);
    115   ECase(IMAGE_REL_I386_DIR32);
    116   ECase(IMAGE_REL_I386_DIR32NB);
    117   ECase(IMAGE_REL_I386_SEG12);
    118   ECase(IMAGE_REL_I386_SECTION);
    119   ECase(IMAGE_REL_I386_SECREL);
    120   ECase(IMAGE_REL_I386_TOKEN);
    121   ECase(IMAGE_REL_I386_SECREL7);
    122   ECase(IMAGE_REL_I386_REL32);
    123   ECase(IMAGE_REL_AMD64_ABSOLUTE);
    124   ECase(IMAGE_REL_AMD64_ADDR64);
    125   ECase(IMAGE_REL_AMD64_ADDR32);
    126   ECase(IMAGE_REL_AMD64_ADDR32NB);
    127   ECase(IMAGE_REL_AMD64_REL32);
    128   ECase(IMAGE_REL_AMD64_REL32_1);
    129   ECase(IMAGE_REL_AMD64_REL32_2);
    130   ECase(IMAGE_REL_AMD64_REL32_3);
    131   ECase(IMAGE_REL_AMD64_REL32_4);
    132   ECase(IMAGE_REL_AMD64_REL32_5);
    133   ECase(IMAGE_REL_AMD64_SECTION);
    134   ECase(IMAGE_REL_AMD64_SECREL);
    135   ECase(IMAGE_REL_AMD64_SECREL7);
    136   ECase(IMAGE_REL_AMD64_TOKEN);
    137   ECase(IMAGE_REL_AMD64_SREL32);
    138   ECase(IMAGE_REL_AMD64_PAIR);
    139   ECase(IMAGE_REL_AMD64_SSPAN32);
    140 }
    141 #undef ECase
    142 
    143 #define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
    144 void ScalarBitSetTraits<COFF::Characteristics>::bitset(
    145     IO &IO, COFF::Characteristics &Value) {
    146   BCase(IMAGE_FILE_RELOCS_STRIPPED);
    147   BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
    148   BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
    149   BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
    150   BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
    151   BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
    152   BCase(IMAGE_FILE_BYTES_REVERSED_LO);
    153   BCase(IMAGE_FILE_32BIT_MACHINE);
    154   BCase(IMAGE_FILE_DEBUG_STRIPPED);
    155   BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
    156   BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
    157   BCase(IMAGE_FILE_SYSTEM);
    158   BCase(IMAGE_FILE_DLL);
    159   BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
    160   BCase(IMAGE_FILE_BYTES_REVERSED_HI);
    161 }
    162 
    163 void ScalarBitSetTraits<COFF::SectionCharacteristics>::bitset(
    164     IO &IO, COFF::SectionCharacteristics &Value) {
    165   BCase(IMAGE_SCN_TYPE_NO_PAD);
    166   BCase(IMAGE_SCN_CNT_CODE);
    167   BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
    168   BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
    169   BCase(IMAGE_SCN_LNK_OTHER);
    170   BCase(IMAGE_SCN_LNK_INFO);
    171   BCase(IMAGE_SCN_LNK_REMOVE);
    172   BCase(IMAGE_SCN_LNK_COMDAT);
    173   BCase(IMAGE_SCN_GPREL);
    174   BCase(IMAGE_SCN_MEM_PURGEABLE);
    175   BCase(IMAGE_SCN_MEM_16BIT);
    176   BCase(IMAGE_SCN_MEM_LOCKED);
    177   BCase(IMAGE_SCN_MEM_PRELOAD);
    178   BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
    179   BCase(IMAGE_SCN_MEM_DISCARDABLE);
    180   BCase(IMAGE_SCN_MEM_NOT_CACHED);
    181   BCase(IMAGE_SCN_MEM_NOT_PAGED);
    182   BCase(IMAGE_SCN_MEM_SHARED);
    183   BCase(IMAGE_SCN_MEM_EXECUTE);
    184   BCase(IMAGE_SCN_MEM_READ);
    185   BCase(IMAGE_SCN_MEM_WRITE);
    186 }
    187 #undef BCase
    188 
    189 namespace {
    190 struct NSectionCharacteristics {
    191   NSectionCharacteristics(IO &)
    192       : Characteristics(COFF::SectionCharacteristics(0)) {}
    193   NSectionCharacteristics(IO &, uint32_t C)
    194       : Characteristics(COFF::SectionCharacteristics(C)) {}
    195   uint32_t denormalize(IO &) { return Characteristics; }
    196   COFF::SectionCharacteristics Characteristics;
    197 };
    198 
    199 struct NStorageClass {
    200   NStorageClass(IO &) : StorageClass(COFF::SymbolStorageClass(0)) {}
    201   NStorageClass(IO &, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {}
    202   uint8_t denormalize(IO &) { return StorageClass; }
    203 
    204   COFF::SymbolStorageClass StorageClass;
    205 };
    206 
    207 struct NMachine {
    208   NMachine(IO &) : Machine(COFF::MachineTypes(0)) {}
    209   NMachine(IO &, uint16_t M) : Machine(COFF::MachineTypes(M)) {}
    210   uint16_t denormalize(IO &) { return Machine; }
    211   COFF::MachineTypes Machine;
    212 };
    213 
    214 struct NHeaderCharacteristics {
    215   NHeaderCharacteristics(IO &) : Characteristics(COFF::Characteristics(0)) {}
    216   NHeaderCharacteristics(IO &, uint16_t C)
    217       : Characteristics(COFF::Characteristics(C)) {}
    218   uint16_t denormalize(IO &) { return Characteristics; }
    219 
    220   COFF::Characteristics Characteristics;
    221 };
    222 
    223 struct NType {
    224   NType(IO &) : Type(COFF::RelocationTypeX86(0)) {}
    225   NType(IO &, uint16_t T) : Type(COFF::RelocationTypeX86(T)) {}
    226   uint16_t denormalize(IO &) { return Type; }
    227   COFF::RelocationTypeX86 Type;
    228 };
    229 
    230 }
    231 
    232 void MappingTraits<COFFYAML::Relocation>::mapping(IO &IO,
    233                                                   COFFYAML::Relocation &Rel) {
    234   MappingNormalization<NType, uint16_t> NT(IO, Rel.Type);
    235 
    236   IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
    237   IO.mapRequired("SymbolName", Rel.SymbolName);
    238   IO.mapRequired("Type", NT->Type);
    239 }
    240 
    241 void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) {
    242   MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine);
    243   MappingNormalization<NHeaderCharacteristics, uint16_t> NC(IO,
    244                                                             H.Characteristics);
    245 
    246   IO.mapRequired("Machine", NM->Machine);
    247   IO.mapOptional("Characteristics", NC->Characteristics);
    248 }
    249 
    250 void MappingTraits<COFFYAML::Symbol>::mapping(IO &IO, COFFYAML::Symbol &S) {
    251   MappingNormalization<NStorageClass, uint8_t> NS(IO, S.Header.StorageClass);
    252 
    253   IO.mapRequired("Name", S.Name);
    254   IO.mapRequired("Value", S.Header.Value);
    255   IO.mapRequired("SectionNumber", S.Header.SectionNumber);
    256   IO.mapRequired("SimpleType", S.SimpleType);
    257   IO.mapRequired("ComplexType", S.ComplexType);
    258   IO.mapRequired("StorageClass", NS->StorageClass);
    259   IO.mapOptional("NumberOfAuxSymbols", S.Header.NumberOfAuxSymbols,
    260                  (uint8_t) 0);
    261   IO.mapOptional("AuxiliaryData", S.AuxiliaryData, object::yaml::BinaryRef());
    262 }
    263 
    264 void MappingTraits<COFFYAML::Section>::mapping(IO &IO, COFFYAML::Section &Sec) {
    265   MappingNormalization<NSectionCharacteristics, uint32_t> NC(
    266       IO, Sec.Header.Characteristics);
    267   IO.mapRequired("Name", Sec.Name);
    268   IO.mapRequired("Characteristics", NC->Characteristics);
    269   IO.mapOptional("Alignment", Sec.Alignment);
    270   IO.mapRequired("SectionData", Sec.SectionData);
    271   IO.mapOptional("Relocations", Sec.Relocations);
    272 }
    273 
    274 void MappingTraits<COFFYAML::Object>::mapping(IO &IO, COFFYAML::Object &Obj) {
    275   IO.mapRequired("header", Obj.Header);
    276   IO.mapRequired("sections", Obj.Sections);
    277   IO.mapRequired("symbols", Obj.Symbols);
    278 }
    279 
    280 }
    281 }
    282