LLVM API Documentation

COFFYAML.cpp
Go to the documentation of this file.
00001 //===- COFFYAML.cpp - COFF YAMLIO implementation --------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines classes for handling the YAML representation of COFF.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Object/COFFYAML.h"
00015 
00016 #define ECase(X) IO.enumCase(Value, #X, COFF::X);
00017 namespace llvm {
00018 
00019 namespace COFFYAML {
00020 Section::Section() { memset(&Header, 0, sizeof(COFF::section)); }
00021 Symbol::Symbol() { memset(&Header, 0, sizeof(COFF::symbol)); }
00022 Object::Object() { memset(&Header, 0, sizeof(COFF::header)); }
00023 }
00024 
00025 namespace yaml {
00026 void ScalarEnumerationTraits<COFFYAML::COMDATType>::enumeration(
00027     IO &IO, COFFYAML::COMDATType &Value) {
00028   IO.enumCase(Value, "0", 0);
00029   ECase(IMAGE_COMDAT_SELECT_NODUPLICATES);
00030   ECase(IMAGE_COMDAT_SELECT_ANY);
00031   ECase(IMAGE_COMDAT_SELECT_SAME_SIZE);
00032   ECase(IMAGE_COMDAT_SELECT_EXACT_MATCH);
00033   ECase(IMAGE_COMDAT_SELECT_ASSOCIATIVE);
00034   ECase(IMAGE_COMDAT_SELECT_LARGEST);
00035   ECase(IMAGE_COMDAT_SELECT_NEWEST);
00036 }
00037 
00038 void
00039 ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics>::enumeration(
00040     IO &IO, COFFYAML::WeakExternalCharacteristics &Value) {
00041   IO.enumCase(Value, "0", 0);
00042   ECase(IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY);
00043   ECase(IMAGE_WEAK_EXTERN_SEARCH_LIBRARY);
00044   ECase(IMAGE_WEAK_EXTERN_SEARCH_ALIAS);
00045 }
00046 
00047 void ScalarEnumerationTraits<COFFYAML::AuxSymbolType>::enumeration(
00048     IO &IO, COFFYAML::AuxSymbolType &Value) {
00049   ECase(IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF);
00050 }
00051 
00052 void ScalarEnumerationTraits<COFF::MachineTypes>::enumeration(
00053     IO &IO, COFF::MachineTypes &Value) {
00054   ECase(IMAGE_FILE_MACHINE_UNKNOWN);
00055   ECase(IMAGE_FILE_MACHINE_AM33);
00056   ECase(IMAGE_FILE_MACHINE_AMD64);
00057   ECase(IMAGE_FILE_MACHINE_ARM);
00058   ECase(IMAGE_FILE_MACHINE_ARMNT);
00059   ECase(IMAGE_FILE_MACHINE_EBC);
00060   ECase(IMAGE_FILE_MACHINE_I386);
00061   ECase(IMAGE_FILE_MACHINE_IA64);
00062   ECase(IMAGE_FILE_MACHINE_M32R);
00063   ECase(IMAGE_FILE_MACHINE_MIPS16);
00064   ECase(IMAGE_FILE_MACHINE_MIPSFPU);
00065   ECase(IMAGE_FILE_MACHINE_MIPSFPU16);
00066   ECase(IMAGE_FILE_MACHINE_POWERPC);
00067   ECase(IMAGE_FILE_MACHINE_POWERPCFP);
00068   ECase(IMAGE_FILE_MACHINE_R4000);
00069   ECase(IMAGE_FILE_MACHINE_SH3);
00070   ECase(IMAGE_FILE_MACHINE_SH3DSP);
00071   ECase(IMAGE_FILE_MACHINE_SH4);
00072   ECase(IMAGE_FILE_MACHINE_SH5);
00073   ECase(IMAGE_FILE_MACHINE_THUMB);
00074   ECase(IMAGE_FILE_MACHINE_WCEMIPSV2);
00075 }
00076 
00077 void ScalarEnumerationTraits<COFF::SymbolBaseType>::enumeration(
00078     IO &IO, COFF::SymbolBaseType &Value) {
00079   ECase(IMAGE_SYM_TYPE_NULL);
00080   ECase(IMAGE_SYM_TYPE_VOID);
00081   ECase(IMAGE_SYM_TYPE_CHAR);
00082   ECase(IMAGE_SYM_TYPE_SHORT);
00083   ECase(IMAGE_SYM_TYPE_INT);
00084   ECase(IMAGE_SYM_TYPE_LONG);
00085   ECase(IMAGE_SYM_TYPE_FLOAT);
00086   ECase(IMAGE_SYM_TYPE_DOUBLE);
00087   ECase(IMAGE_SYM_TYPE_STRUCT);
00088   ECase(IMAGE_SYM_TYPE_UNION);
00089   ECase(IMAGE_SYM_TYPE_ENUM);
00090   ECase(IMAGE_SYM_TYPE_MOE);
00091   ECase(IMAGE_SYM_TYPE_BYTE);
00092   ECase(IMAGE_SYM_TYPE_WORD);
00093   ECase(IMAGE_SYM_TYPE_UINT);
00094   ECase(IMAGE_SYM_TYPE_DWORD);
00095 }
00096 
00097 void ScalarEnumerationTraits<COFF::SymbolStorageClass>::enumeration(
00098     IO &IO, COFF::SymbolStorageClass &Value) {
00099   ECase(IMAGE_SYM_CLASS_END_OF_FUNCTION);
00100   ECase(IMAGE_SYM_CLASS_NULL);
00101   ECase(IMAGE_SYM_CLASS_AUTOMATIC);
00102   ECase(IMAGE_SYM_CLASS_EXTERNAL);
00103   ECase(IMAGE_SYM_CLASS_STATIC);
00104   ECase(IMAGE_SYM_CLASS_REGISTER);
00105   ECase(IMAGE_SYM_CLASS_EXTERNAL_DEF);
00106   ECase(IMAGE_SYM_CLASS_LABEL);
00107   ECase(IMAGE_SYM_CLASS_UNDEFINED_LABEL);
00108   ECase(IMAGE_SYM_CLASS_MEMBER_OF_STRUCT);
00109   ECase(IMAGE_SYM_CLASS_ARGUMENT);
00110   ECase(IMAGE_SYM_CLASS_STRUCT_TAG);
00111   ECase(IMAGE_SYM_CLASS_MEMBER_OF_UNION);
00112   ECase(IMAGE_SYM_CLASS_UNION_TAG);
00113   ECase(IMAGE_SYM_CLASS_TYPE_DEFINITION);
00114   ECase(IMAGE_SYM_CLASS_UNDEFINED_STATIC);
00115   ECase(IMAGE_SYM_CLASS_ENUM_TAG);
00116   ECase(IMAGE_SYM_CLASS_MEMBER_OF_ENUM);
00117   ECase(IMAGE_SYM_CLASS_REGISTER_PARAM);
00118   ECase(IMAGE_SYM_CLASS_BIT_FIELD);
00119   ECase(IMAGE_SYM_CLASS_BLOCK);
00120   ECase(IMAGE_SYM_CLASS_FUNCTION);
00121   ECase(IMAGE_SYM_CLASS_END_OF_STRUCT);
00122   ECase(IMAGE_SYM_CLASS_FILE);
00123   ECase(IMAGE_SYM_CLASS_SECTION);
00124   ECase(IMAGE_SYM_CLASS_WEAK_EXTERNAL);
00125   ECase(IMAGE_SYM_CLASS_CLR_TOKEN);
00126 }
00127 
00128 void ScalarEnumerationTraits<COFF::SymbolComplexType>::enumeration(
00129     IO &IO, COFF::SymbolComplexType &Value) {
00130   ECase(IMAGE_SYM_DTYPE_NULL);
00131   ECase(IMAGE_SYM_DTYPE_POINTER);
00132   ECase(IMAGE_SYM_DTYPE_FUNCTION);
00133   ECase(IMAGE_SYM_DTYPE_ARRAY);
00134 }
00135 
00136 void ScalarEnumerationTraits<COFF::RelocationTypeI386>::enumeration(
00137     IO &IO, COFF::RelocationTypeI386 &Value) {
00138   ECase(IMAGE_REL_I386_ABSOLUTE);
00139   ECase(IMAGE_REL_I386_DIR16);
00140   ECase(IMAGE_REL_I386_REL16);
00141   ECase(IMAGE_REL_I386_DIR32);
00142   ECase(IMAGE_REL_I386_DIR32NB);
00143   ECase(IMAGE_REL_I386_SEG12);
00144   ECase(IMAGE_REL_I386_SECTION);
00145   ECase(IMAGE_REL_I386_SECREL);
00146   ECase(IMAGE_REL_I386_TOKEN);
00147   ECase(IMAGE_REL_I386_SECREL7);
00148   ECase(IMAGE_REL_I386_REL32);
00149 }
00150 
00151 void ScalarEnumerationTraits<COFF::RelocationTypeAMD64>::enumeration(
00152     IO &IO, COFF::RelocationTypeAMD64 &Value) {
00153   ECase(IMAGE_REL_AMD64_ABSOLUTE);
00154   ECase(IMAGE_REL_AMD64_ADDR64);
00155   ECase(IMAGE_REL_AMD64_ADDR32);
00156   ECase(IMAGE_REL_AMD64_ADDR32NB);
00157   ECase(IMAGE_REL_AMD64_REL32);
00158   ECase(IMAGE_REL_AMD64_REL32_1);
00159   ECase(IMAGE_REL_AMD64_REL32_2);
00160   ECase(IMAGE_REL_AMD64_REL32_3);
00161   ECase(IMAGE_REL_AMD64_REL32_4);
00162   ECase(IMAGE_REL_AMD64_REL32_5);
00163   ECase(IMAGE_REL_AMD64_SECTION);
00164   ECase(IMAGE_REL_AMD64_SECREL);
00165   ECase(IMAGE_REL_AMD64_SECREL7);
00166   ECase(IMAGE_REL_AMD64_TOKEN);
00167   ECase(IMAGE_REL_AMD64_SREL32);
00168   ECase(IMAGE_REL_AMD64_PAIR);
00169   ECase(IMAGE_REL_AMD64_SSPAN32);
00170 }
00171 #undef ECase
00172 
00173 #define BCase(X) IO.bitSetCase(Value, #X, COFF::X);
00174 void ScalarBitSetTraits<COFF::Characteristics>::bitset(
00175     IO &IO, COFF::Characteristics &Value) {
00176   BCase(IMAGE_FILE_RELOCS_STRIPPED);
00177   BCase(IMAGE_FILE_EXECUTABLE_IMAGE);
00178   BCase(IMAGE_FILE_LINE_NUMS_STRIPPED);
00179   BCase(IMAGE_FILE_LOCAL_SYMS_STRIPPED);
00180   BCase(IMAGE_FILE_AGGRESSIVE_WS_TRIM);
00181   BCase(IMAGE_FILE_LARGE_ADDRESS_AWARE);
00182   BCase(IMAGE_FILE_BYTES_REVERSED_LO);
00183   BCase(IMAGE_FILE_32BIT_MACHINE);
00184   BCase(IMAGE_FILE_DEBUG_STRIPPED);
00185   BCase(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP);
00186   BCase(IMAGE_FILE_NET_RUN_FROM_SWAP);
00187   BCase(IMAGE_FILE_SYSTEM);
00188   BCase(IMAGE_FILE_DLL);
00189   BCase(IMAGE_FILE_UP_SYSTEM_ONLY);
00190   BCase(IMAGE_FILE_BYTES_REVERSED_HI);
00191 }
00192 
00193 void ScalarBitSetTraits<COFF::SectionCharacteristics>::bitset(
00194     IO &IO, COFF::SectionCharacteristics &Value) {
00195   BCase(IMAGE_SCN_TYPE_NO_PAD);
00196   BCase(IMAGE_SCN_CNT_CODE);
00197   BCase(IMAGE_SCN_CNT_INITIALIZED_DATA);
00198   BCase(IMAGE_SCN_CNT_UNINITIALIZED_DATA);
00199   BCase(IMAGE_SCN_LNK_OTHER);
00200   BCase(IMAGE_SCN_LNK_INFO);
00201   BCase(IMAGE_SCN_LNK_REMOVE);
00202   BCase(IMAGE_SCN_LNK_COMDAT);
00203   BCase(IMAGE_SCN_GPREL);
00204   BCase(IMAGE_SCN_MEM_PURGEABLE);
00205   BCase(IMAGE_SCN_MEM_16BIT);
00206   BCase(IMAGE_SCN_MEM_LOCKED);
00207   BCase(IMAGE_SCN_MEM_PRELOAD);
00208   BCase(IMAGE_SCN_LNK_NRELOC_OVFL);
00209   BCase(IMAGE_SCN_MEM_DISCARDABLE);
00210   BCase(IMAGE_SCN_MEM_NOT_CACHED);
00211   BCase(IMAGE_SCN_MEM_NOT_PAGED);
00212   BCase(IMAGE_SCN_MEM_SHARED);
00213   BCase(IMAGE_SCN_MEM_EXECUTE);
00214   BCase(IMAGE_SCN_MEM_READ);
00215   BCase(IMAGE_SCN_MEM_WRITE);
00216 }
00217 #undef BCase
00218 
00219 namespace {
00220 struct NSectionSelectionType {
00221   NSectionSelectionType(IO &)
00222       : SelectionType(COFFYAML::COMDATType(0)) {}
00223   NSectionSelectionType(IO &, uint8_t C)
00224       : SelectionType(COFFYAML::COMDATType(C)) {}
00225   uint8_t denormalize(IO &) { return SelectionType; }
00226   COFFYAML::COMDATType SelectionType;
00227 };
00228 
00229 struct NWeakExternalCharacteristics {
00230   NWeakExternalCharacteristics(IO &)
00231       : Characteristics(COFFYAML::WeakExternalCharacteristics(0)) {}
00232   NWeakExternalCharacteristics(IO &, uint32_t C)
00233       : Characteristics(COFFYAML::WeakExternalCharacteristics(C)) {}
00234   uint32_t denormalize(IO &) { return Characteristics; }
00235   COFFYAML::WeakExternalCharacteristics Characteristics;
00236 };
00237 
00238 struct NSectionCharacteristics {
00239   NSectionCharacteristics(IO &)
00240       : Characteristics(COFF::SectionCharacteristics(0)) {}
00241   NSectionCharacteristics(IO &, uint32_t C)
00242       : Characteristics(COFF::SectionCharacteristics(C)) {}
00243   uint32_t denormalize(IO &) { return Characteristics; }
00244   COFF::SectionCharacteristics Characteristics;
00245 };
00246 
00247 struct NAuxTokenType {
00248   NAuxTokenType(IO &)
00249       : AuxType(COFFYAML::AuxSymbolType(0)) {}
00250   NAuxTokenType(IO &, uint8_t C)
00251       : AuxType(COFFYAML::AuxSymbolType(C)) {}
00252   uint32_t denormalize(IO &) { return AuxType; }
00253   COFFYAML::AuxSymbolType AuxType;
00254 };
00255 
00256 struct NStorageClass {
00257   NStorageClass(IO &) : StorageClass(COFF::SymbolStorageClass(0)) {}
00258   NStorageClass(IO &, uint8_t S) : StorageClass(COFF::SymbolStorageClass(S)) {}
00259   uint8_t denormalize(IO &) { return StorageClass; }
00260 
00261   COFF::SymbolStorageClass StorageClass;
00262 };
00263 
00264 struct NMachine {
00265   NMachine(IO &) : Machine(COFF::MachineTypes(0)) {}
00266   NMachine(IO &, uint16_t M) : Machine(COFF::MachineTypes(M)) {}
00267   uint16_t denormalize(IO &) { return Machine; }
00268   COFF::MachineTypes Machine;
00269 };
00270 
00271 struct NHeaderCharacteristics {
00272   NHeaderCharacteristics(IO &) : Characteristics(COFF::Characteristics(0)) {}
00273   NHeaderCharacteristics(IO &, uint16_t C)
00274       : Characteristics(COFF::Characteristics(C)) {}
00275   uint16_t denormalize(IO &) { return Characteristics; }
00276 
00277   COFF::Characteristics Characteristics;
00278 };
00279 
00280 template <typename RelocType>
00281 struct NType {
00282   NType(IO &) : Type(RelocType(0)) {}
00283   NType(IO &, uint16_t T) : Type(RelocType(T)) {}
00284   uint16_t denormalize(IO &) { return Type; }
00285   RelocType Type;
00286 };
00287 
00288 }
00289 
00290 void MappingTraits<COFFYAML::Relocation>::mapping(IO &IO,
00291                                                   COFFYAML::Relocation &Rel) {
00292   IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
00293   IO.mapRequired("SymbolName", Rel.SymbolName);
00294 
00295   COFF::header &H = *static_cast<COFF::header *>(IO.getContext());
00296   if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) {
00297     MappingNormalization<NType<COFF::RelocationTypeI386>, uint16_t> NT(
00298         IO, Rel.Type);
00299     IO.mapRequired("Type", NT->Type);
00300   } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) {
00301     MappingNormalization<NType<COFF::RelocationTypeAMD64>, uint16_t> NT(
00302         IO, Rel.Type);
00303     IO.mapRequired("Type", NT->Type);
00304   } else {
00305     IO.mapRequired("Type", Rel.Type);
00306   }
00307 }
00308 
00309 void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) {
00310   MappingNormalization<NMachine, uint16_t> NM(IO, H.Machine);
00311   MappingNormalization<NHeaderCharacteristics, uint16_t> NC(IO,
00312                                                             H.Characteristics);
00313 
00314   IO.mapRequired("Machine", NM->Machine);
00315   IO.mapOptional("Characteristics", NC->Characteristics);
00316   IO.setContext(static_cast<void *>(&H));
00317 }
00318 
00319 void MappingTraits<COFF::AuxiliaryFunctionDefinition>::mapping(
00320     IO &IO, COFF::AuxiliaryFunctionDefinition &AFD) {
00321   IO.mapRequired("TagIndex", AFD.TagIndex);
00322   IO.mapRequired("TotalSize", AFD.TotalSize);
00323   IO.mapRequired("PointerToLinenumber", AFD.PointerToLinenumber);
00324   IO.mapRequired("PointerToNextFunction", AFD.PointerToNextFunction);
00325 }
00326 
00327 void MappingTraits<COFF::AuxiliarybfAndefSymbol>::mapping(
00328     IO &IO, COFF::AuxiliarybfAndefSymbol &AAS) {
00329   IO.mapRequired("Linenumber", AAS.Linenumber);
00330   IO.mapRequired("PointerToNextFunction", AAS.PointerToNextFunction);
00331 }
00332 
00333 void MappingTraits<COFF::AuxiliaryWeakExternal>::mapping(
00334     IO &IO, COFF::AuxiliaryWeakExternal &AWE) {
00335   MappingNormalization<NWeakExternalCharacteristics, uint32_t> NWEC(
00336       IO, AWE.Characteristics);
00337   IO.mapRequired("TagIndex", AWE.TagIndex);
00338   IO.mapRequired("Characteristics", NWEC->Characteristics);
00339 }
00340 
00341 void MappingTraits<COFF::AuxiliarySectionDefinition>::mapping(
00342     IO &IO, COFF::AuxiliarySectionDefinition &ASD) {
00343   MappingNormalization<NSectionSelectionType, uint8_t> NSST(
00344       IO, ASD.Selection);
00345 
00346   IO.mapRequired("Length", ASD.Length);
00347   IO.mapRequired("NumberOfRelocations", ASD.NumberOfRelocations);
00348   IO.mapRequired("NumberOfLinenumbers", ASD.NumberOfLinenumbers);
00349   IO.mapRequired("CheckSum", ASD.CheckSum);
00350   IO.mapRequired("Number", ASD.Number);
00351   IO.mapOptional("Selection", NSST->SelectionType, COFFYAML::COMDATType(0));
00352 }
00353 
00354 void MappingTraits<COFF::AuxiliaryCLRToken>::mapping(
00355     IO &IO, COFF::AuxiliaryCLRToken &ACT) {
00356   MappingNormalization<NAuxTokenType, uint8_t> NATT(IO, ACT.AuxType);
00357   IO.mapRequired("AuxType", NATT->AuxType);
00358   IO.mapRequired("SymbolTableIndex", ACT.SymbolTableIndex);
00359 }
00360 
00361 void MappingTraits<COFFYAML::Symbol>::mapping(IO &IO, COFFYAML::Symbol &S) {
00362   MappingNormalization<NStorageClass, uint8_t> NS(IO, S.Header.StorageClass);
00363 
00364   IO.mapRequired("Name", S.Name);
00365   IO.mapRequired("Value", S.Header.Value);
00366   IO.mapRequired("SectionNumber", S.Header.SectionNumber);
00367   IO.mapRequired("SimpleType", S.SimpleType);
00368   IO.mapRequired("ComplexType", S.ComplexType);
00369   IO.mapRequired("StorageClass", NS->StorageClass);
00370   IO.mapOptional("FunctionDefinition", S.FunctionDefinition);
00371   IO.mapOptional("bfAndefSymbol", S.bfAndefSymbol);
00372   IO.mapOptional("WeakExternal", S.WeakExternal);
00373   IO.mapOptional("File", S.File, StringRef());
00374   IO.mapOptional("SectionDefinition", S.SectionDefinition);
00375   IO.mapOptional("CLRToken", S.CLRToken);
00376 }
00377 
00378 void MappingTraits<COFFYAML::Section>::mapping(IO &IO, COFFYAML::Section &Sec) {
00379   MappingNormalization<NSectionCharacteristics, uint32_t> NC(
00380       IO, Sec.Header.Characteristics);
00381   IO.mapRequired("Name", Sec.Name);
00382   IO.mapRequired("Characteristics", NC->Characteristics);
00383   IO.mapOptional("Alignment", Sec.Alignment);
00384   IO.mapRequired("SectionData", Sec.SectionData);
00385   IO.mapOptional("Relocations", Sec.Relocations);
00386 }
00387 
00388 void MappingTraits<COFFYAML::Object>::mapping(IO &IO, COFFYAML::Object &Obj) {
00389   IO.mapRequired("header", Obj.Header);
00390   IO.mapRequired("sections", Obj.Sections);
00391   IO.mapRequired("symbols", Obj.Symbols);
00392 }
00393 
00394 }
00395 }