Home | History | Annotate | Download | only in Object
      1 //===- ELFYAML.cpp - ELF 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 ELF.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Object/ELFYAML.h"
     15 #include "llvm/Support/Casting.h"
     16 #include "llvm/Support/MipsABIFlags.h"
     17 
     18 namespace llvm {
     19 
     20 ELFYAML::Section::~Section() {}
     21 
     22 namespace yaml {
     23 
     24 void
     25 ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(IO &IO,
     26                                                       ELFYAML::ELF_ET &Value) {
     27 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
     28   ECase(ET_NONE)
     29   ECase(ET_REL)
     30   ECase(ET_EXEC)
     31   ECase(ET_DYN)
     32   ECase(ET_CORE)
     33 #undef ECase
     34   IO.enumFallback<Hex16>(Value);
     35 }
     36 
     37 void
     38 ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(IO &IO,
     39                                                       ELFYAML::ELF_EM &Value) {
     40 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
     41   ECase(EM_NONE)
     42   ECase(EM_M32)
     43   ECase(EM_SPARC)
     44   ECase(EM_386)
     45   ECase(EM_68K)
     46   ECase(EM_88K)
     47   ECase(EM_IAMCU)
     48   ECase(EM_860)
     49   ECase(EM_MIPS)
     50   ECase(EM_S370)
     51   ECase(EM_MIPS_RS3_LE)
     52   ECase(EM_PARISC)
     53   ECase(EM_VPP500)
     54   ECase(EM_SPARC32PLUS)
     55   ECase(EM_960)
     56   ECase(EM_PPC)
     57   ECase(EM_PPC64)
     58   ECase(EM_S390)
     59   ECase(EM_SPU)
     60   ECase(EM_V800)
     61   ECase(EM_FR20)
     62   ECase(EM_RH32)
     63   ECase(EM_RCE)
     64   ECase(EM_ARM)
     65   ECase(EM_ALPHA)
     66   ECase(EM_SH)
     67   ECase(EM_SPARCV9)
     68   ECase(EM_TRICORE)
     69   ECase(EM_ARC)
     70   ECase(EM_H8_300)
     71   ECase(EM_H8_300H)
     72   ECase(EM_H8S)
     73   ECase(EM_H8_500)
     74   ECase(EM_IA_64)
     75   ECase(EM_MIPS_X)
     76   ECase(EM_COLDFIRE)
     77   ECase(EM_68HC12)
     78   ECase(EM_MMA)
     79   ECase(EM_PCP)
     80   ECase(EM_NCPU)
     81   ECase(EM_NDR1)
     82   ECase(EM_STARCORE)
     83   ECase(EM_ME16)
     84   ECase(EM_ST100)
     85   ECase(EM_TINYJ)
     86   ECase(EM_X86_64)
     87   ECase(EM_PDSP)
     88   ECase(EM_PDP10)
     89   ECase(EM_PDP11)
     90   ECase(EM_FX66)
     91   ECase(EM_ST9PLUS)
     92   ECase(EM_ST7)
     93   ECase(EM_68HC16)
     94   ECase(EM_68HC11)
     95   ECase(EM_68HC08)
     96   ECase(EM_68HC05)
     97   ECase(EM_SVX)
     98   ECase(EM_ST19)
     99   ECase(EM_VAX)
    100   ECase(EM_CRIS)
    101   ECase(EM_JAVELIN)
    102   ECase(EM_FIREPATH)
    103   ECase(EM_ZSP)
    104   ECase(EM_MMIX)
    105   ECase(EM_HUANY)
    106   ECase(EM_PRISM)
    107   ECase(EM_AVR)
    108   ECase(EM_FR30)
    109   ECase(EM_D10V)
    110   ECase(EM_D30V)
    111   ECase(EM_V850)
    112   ECase(EM_M32R)
    113   ECase(EM_MN10300)
    114   ECase(EM_MN10200)
    115   ECase(EM_PJ)
    116   ECase(EM_OPENRISC)
    117   ECase(EM_ARC_COMPACT)
    118   ECase(EM_XTENSA)
    119   ECase(EM_VIDEOCORE)
    120   ECase(EM_TMM_GPP)
    121   ECase(EM_NS32K)
    122   ECase(EM_TPC)
    123   ECase(EM_SNP1K)
    124   ECase(EM_ST200)
    125   ECase(EM_IP2K)
    126   ECase(EM_MAX)
    127   ECase(EM_CR)
    128   ECase(EM_F2MC16)
    129   ECase(EM_MSP430)
    130   ECase(EM_BLACKFIN)
    131   ECase(EM_SE_C33)
    132   ECase(EM_SEP)
    133   ECase(EM_ARCA)
    134   ECase(EM_UNICORE)
    135   ECase(EM_EXCESS)
    136   ECase(EM_DXP)
    137   ECase(EM_ALTERA_NIOS2)
    138   ECase(EM_CRX)
    139   ECase(EM_XGATE)
    140   ECase(EM_C166)
    141   ECase(EM_M16C)
    142   ECase(EM_DSPIC30F)
    143   ECase(EM_CE)
    144   ECase(EM_M32C)
    145   ECase(EM_TSK3000)
    146   ECase(EM_RS08)
    147   ECase(EM_SHARC)
    148   ECase(EM_ECOG2)
    149   ECase(EM_SCORE7)
    150   ECase(EM_DSP24)
    151   ECase(EM_VIDEOCORE3)
    152   ECase(EM_LATTICEMICO32)
    153   ECase(EM_SE_C17)
    154   ECase(EM_TI_C6000)
    155   ECase(EM_TI_C2000)
    156   ECase(EM_TI_C5500)
    157   ECase(EM_MMDSP_PLUS)
    158   ECase(EM_CYPRESS_M8C)
    159   ECase(EM_R32C)
    160   ECase(EM_TRIMEDIA)
    161   ECase(EM_HEXAGON)
    162   ECase(EM_8051)
    163   ECase(EM_STXP7X)
    164   ECase(EM_NDS32)
    165   ECase(EM_ECOG1)
    166   ECase(EM_ECOG1X)
    167   ECase(EM_MAXQ30)
    168   ECase(EM_XIMO16)
    169   ECase(EM_MANIK)
    170   ECase(EM_CRAYNV2)
    171   ECase(EM_RX)
    172   ECase(EM_METAG)
    173   ECase(EM_MCST_ELBRUS)
    174   ECase(EM_ECOG16)
    175   ECase(EM_CR16)
    176   ECase(EM_ETPU)
    177   ECase(EM_SLE9X)
    178   ECase(EM_L10M)
    179   ECase(EM_K10M)
    180   ECase(EM_AARCH64)
    181   ECase(EM_AVR32)
    182   ECase(EM_STM8)
    183   ECase(EM_TILE64)
    184   ECase(EM_TILEPRO)
    185   ECase(EM_CUDA)
    186   ECase(EM_TILEGX)
    187   ECase(EM_CLOUDSHIELD)
    188   ECase(EM_COREA_1ST)
    189   ECase(EM_COREA_2ND)
    190   ECase(EM_ARC_COMPACT2)
    191   ECase(EM_OPEN8)
    192   ECase(EM_RL78)
    193   ECase(EM_VIDEOCORE5)
    194   ECase(EM_78KOR)
    195   ECase(EM_56800EX)
    196   ECase(EM_AMDGPU)
    197 #undef ECase
    198 }
    199 
    200 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
    201     IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
    202 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
    203   // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
    204   // here.
    205   ECase(ELFCLASS32)
    206   ECase(ELFCLASS64)
    207 #undef ECase
    208 }
    209 
    210 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
    211     IO &IO, ELFYAML::ELF_ELFDATA &Value) {
    212 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
    213   // Since the semantics of ELFDATANONE is "invalid", just don't accept it
    214   // here.
    215   ECase(ELFDATA2LSB)
    216   ECase(ELFDATA2MSB)
    217 #undef ECase
    218 }
    219 
    220 void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
    221     IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
    222 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
    223   ECase(ELFOSABI_NONE)
    224   ECase(ELFOSABI_HPUX)
    225   ECase(ELFOSABI_NETBSD)
    226   ECase(ELFOSABI_GNU)
    227   ECase(ELFOSABI_GNU)
    228   ECase(ELFOSABI_HURD)
    229   ECase(ELFOSABI_SOLARIS)
    230   ECase(ELFOSABI_AIX)
    231   ECase(ELFOSABI_IRIX)
    232   ECase(ELFOSABI_FREEBSD)
    233   ECase(ELFOSABI_TRU64)
    234   ECase(ELFOSABI_MODESTO)
    235   ECase(ELFOSABI_OPENBSD)
    236   ECase(ELFOSABI_OPENVMS)
    237   ECase(ELFOSABI_NSK)
    238   ECase(ELFOSABI_AROS)
    239   ECase(ELFOSABI_FENIXOS)
    240   ECase(ELFOSABI_CLOUDABI)
    241   ECase(ELFOSABI_C6000_ELFABI)
    242   ECase(ELFOSABI_C6000_LINUX)
    243   ECase(ELFOSABI_ARM)
    244   ECase(ELFOSABI_STANDALONE)
    245 #undef ECase
    246 }
    247 
    248 void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
    249                                                  ELFYAML::ELF_EF &Value) {
    250   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
    251   assert(Object && "The IO context is not initialized");
    252 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
    253 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M);
    254   switch (Object->Header.Machine) {
    255   case ELF::EM_ARM:
    256     BCase(EF_ARM_SOFT_FLOAT)
    257     BCase(EF_ARM_VFP_FLOAT)
    258     BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK)
    259     BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK)
    260     BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK)
    261     BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK)
    262     BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK)
    263     BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK)
    264     break;
    265   case ELF::EM_MIPS:
    266     BCase(EF_MIPS_NOREORDER)
    267     BCase(EF_MIPS_PIC)
    268     BCase(EF_MIPS_CPIC)
    269     BCase(EF_MIPS_ABI2)
    270     BCase(EF_MIPS_32BITMODE)
    271     BCase(EF_MIPS_FP64)
    272     BCase(EF_MIPS_NAN2008)
    273     BCase(EF_MIPS_MICROMIPS)
    274     BCase(EF_MIPS_ARCH_ASE_M16)
    275     BCase(EF_MIPS_ARCH_ASE_MDMX)
    276     BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI)
    277     BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI)
    278     BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI)
    279     BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI)
    280     BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH)
    281     BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH)
    282     BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH)
    283     BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH)
    284     BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH)
    285     BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH)
    286     BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH)
    287     BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH)
    288     BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH)
    289     BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH)
    290     BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH)
    291     BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH)
    292     BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH)
    293     BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH)
    294     BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH)
    295     BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH)
    296     BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH)
    297     BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH)
    298     BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH)
    299     BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH)
    300     BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH)
    301     BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH)
    302     BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH)
    303     BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH)
    304     BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH)
    305     BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH)
    306     BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH)
    307     BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH)
    308     BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH)
    309     break;
    310   case ELF::EM_HEXAGON:
    311     BCase(EF_HEXAGON_MACH_V2)
    312     BCase(EF_HEXAGON_MACH_V3)
    313     BCase(EF_HEXAGON_MACH_V4)
    314     BCase(EF_HEXAGON_MACH_V5)
    315     BCase(EF_HEXAGON_ISA_V2)
    316     BCase(EF_HEXAGON_ISA_V3)
    317     BCase(EF_HEXAGON_ISA_V4)
    318     BCase(EF_HEXAGON_ISA_V5)
    319     break;
    320   case ELF::EM_AVR:
    321     BCase(EF_AVR_ARCH_AVR1)
    322     BCase(EF_AVR_ARCH_AVR2)
    323     BCase(EF_AVR_ARCH_AVR25)
    324     BCase(EF_AVR_ARCH_AVR3)
    325     BCase(EF_AVR_ARCH_AVR31)
    326     BCase(EF_AVR_ARCH_AVR35)
    327     BCase(EF_AVR_ARCH_AVR4)
    328     BCase(EF_AVR_ARCH_AVR51)
    329     BCase(EF_AVR_ARCH_AVR6)
    330     BCase(EF_AVR_ARCH_AVRTINY)
    331     BCase(EF_AVR_ARCH_XMEGA1)
    332     BCase(EF_AVR_ARCH_XMEGA2)
    333     BCase(EF_AVR_ARCH_XMEGA3)
    334     BCase(EF_AVR_ARCH_XMEGA4)
    335     BCase(EF_AVR_ARCH_XMEGA5)
    336     BCase(EF_AVR_ARCH_XMEGA6)
    337     BCase(EF_AVR_ARCH_XMEGA7)
    338     break;
    339   default:
    340     llvm_unreachable("Unsupported architecture");
    341   }
    342 #undef BCase
    343 #undef BCaseMask
    344 }
    345 
    346 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
    347     IO &IO, ELFYAML::ELF_SHT &Value) {
    348   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
    349   assert(Object && "The IO context is not initialized");
    350 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
    351   ECase(SHT_NULL)
    352   ECase(SHT_PROGBITS)
    353   // No SHT_SYMTAB. Use the top-level `Symbols` key instead.
    354   // FIXME: Issue a diagnostic with this information.
    355   ECase(SHT_STRTAB)
    356   ECase(SHT_RELA)
    357   ECase(SHT_HASH)
    358   ECase(SHT_DYNAMIC)
    359   ECase(SHT_NOTE)
    360   ECase(SHT_NOBITS)
    361   ECase(SHT_REL)
    362   ECase(SHT_SHLIB)
    363   ECase(SHT_DYNSYM)
    364   ECase(SHT_INIT_ARRAY)
    365   ECase(SHT_FINI_ARRAY)
    366   ECase(SHT_PREINIT_ARRAY)
    367   ECase(SHT_GROUP)
    368   ECase(SHT_SYMTAB_SHNDX)
    369   ECase(SHT_LOOS)
    370   ECase(SHT_GNU_ATTRIBUTES)
    371   ECase(SHT_GNU_HASH)
    372   ECase(SHT_GNU_verdef)
    373   ECase(SHT_GNU_verneed)
    374   ECase(SHT_GNU_versym)
    375   ECase(SHT_HIOS)
    376   ECase(SHT_LOPROC)
    377   switch (Object->Header.Machine) {
    378   case ELF::EM_ARM:
    379     ECase(SHT_ARM_EXIDX)
    380     ECase(SHT_ARM_PREEMPTMAP)
    381     ECase(SHT_ARM_ATTRIBUTES)
    382     ECase(SHT_ARM_DEBUGOVERLAY)
    383     ECase(SHT_ARM_OVERLAYSECTION)
    384     break;
    385   case ELF::EM_HEXAGON:
    386     ECase(SHT_HEX_ORDERED)
    387     break;
    388   case ELF::EM_X86_64:
    389     ECase(SHT_X86_64_UNWIND)
    390     break;
    391   case ELF::EM_MIPS:
    392     ECase(SHT_MIPS_REGINFO)
    393     ECase(SHT_MIPS_OPTIONS)
    394     ECase(SHT_MIPS_ABIFLAGS)
    395     break;
    396   default:
    397     // Nothing to do.
    398     break;
    399   }
    400 #undef ECase
    401 }
    402 
    403 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
    404                                                   ELFYAML::ELF_SHF &Value) {
    405   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
    406 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
    407   BCase(SHF_WRITE)
    408   BCase(SHF_ALLOC)
    409   BCase(SHF_EXCLUDE)
    410   BCase(SHF_EXECINSTR)
    411   BCase(SHF_MERGE)
    412   BCase(SHF_STRINGS)
    413   BCase(SHF_INFO_LINK)
    414   BCase(SHF_LINK_ORDER)
    415   BCase(SHF_OS_NONCONFORMING)
    416   BCase(SHF_GROUP)
    417   BCase(SHF_TLS)
    418   switch(Object->Header.Machine) {
    419   case ELF::EM_AMDGPU:
    420     BCase(SHF_AMDGPU_HSA_GLOBAL)
    421     BCase(SHF_AMDGPU_HSA_READONLY)
    422     BCase(SHF_AMDGPU_HSA_CODE)
    423     BCase(SHF_AMDGPU_HSA_AGENT)
    424     break;
    425   default:
    426     // Nothing to do.
    427     break;
    428   }
    429 #undef BCase
    430 }
    431 
    432 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
    433     IO &IO, ELFYAML::ELF_STT &Value) {
    434 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
    435   ECase(STT_NOTYPE)
    436   ECase(STT_OBJECT)
    437   ECase(STT_FUNC)
    438   ECase(STT_SECTION)
    439   ECase(STT_FILE)
    440   ECase(STT_COMMON)
    441   ECase(STT_TLS)
    442   ECase(STT_GNU_IFUNC)
    443 #undef ECase
    444 }
    445 
    446 void ScalarEnumerationTraits<ELFYAML::ELF_STV>::enumeration(
    447     IO &IO, ELFYAML::ELF_STV &Value) {
    448 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
    449   ECase(STV_DEFAULT)
    450   ECase(STV_INTERNAL)
    451   ECase(STV_HIDDEN)
    452   ECase(STV_PROTECTED)
    453 #undef ECase
    454 }
    455 
    456 void ScalarBitSetTraits<ELFYAML::ELF_STO>::bitset(IO &IO,
    457                                                   ELFYAML::ELF_STO &Value) {
    458   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
    459   assert(Object && "The IO context is not initialized");
    460 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
    461   switch (Object->Header.Machine) {
    462   case ELF::EM_MIPS:
    463     BCase(STO_MIPS_OPTIONAL)
    464     BCase(STO_MIPS_PLT)
    465     BCase(STO_MIPS_PIC)
    466     BCase(STO_MIPS_MICROMIPS)
    467     break;
    468   default:
    469     break; // Nothing to do
    470   }
    471 #undef BCase
    472 #undef BCaseMask
    473 }
    474 
    475 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
    476     IO &IO, ELFYAML::ELF_RSS &Value) {
    477 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
    478   ECase(RSS_UNDEF)
    479   ECase(RSS_GP)
    480   ECase(RSS_GP0)
    481   ECase(RSS_LOC)
    482 #undef ECase
    483 }
    484 
    485 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
    486     IO &IO, ELFYAML::ELF_REL &Value) {
    487   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
    488   assert(Object && "The IO context is not initialized");
    489 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
    490   switch (Object->Header.Machine) {
    491   case ELF::EM_X86_64:
    492 #include "llvm/Support/ELFRelocs/x86_64.def"
    493     break;
    494   case ELF::EM_MIPS:
    495 #include "llvm/Support/ELFRelocs/Mips.def"
    496     break;
    497   case ELF::EM_HEXAGON:
    498 #include "llvm/Support/ELFRelocs/Hexagon.def"
    499     break;
    500   case ELF::EM_386:
    501   case ELF::EM_IAMCU:
    502 #include "llvm/Support/ELFRelocs/i386.def"
    503     break;
    504   case ELF::EM_AARCH64:
    505 #include "llvm/Support/ELFRelocs/AArch64.def"
    506     break;
    507   case ELF::EM_ARM:
    508 #include "llvm/Support/ELFRelocs/ARM.def"
    509     break;
    510   default:
    511     llvm_unreachable("Unsupported architecture");
    512   }
    513 #undef ELF_RELOC
    514 }
    515 
    516 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
    517     IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
    518 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X);
    519   ECase(REG_NONE)
    520   ECase(REG_32)
    521   ECase(REG_64)
    522   ECase(REG_128)
    523 #undef ECase
    524 }
    525 
    526 void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration(
    527     IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
    528 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X);
    529   ECase(FP_ANY)
    530   ECase(FP_DOUBLE)
    531   ECase(FP_SINGLE)
    532   ECase(FP_SOFT)
    533   ECase(FP_OLD_64)
    534   ECase(FP_XX)
    535   ECase(FP_64)
    536   ECase(FP_64A)
    537 #undef ECase
    538 }
    539 
    540 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration(
    541     IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
    542 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X);
    543   ECase(EXT_NONE)
    544   ECase(EXT_XLR)
    545   ECase(EXT_OCTEON2)
    546   ECase(EXT_OCTEONP)
    547   ECase(EXT_LOONGSON_3A)
    548   ECase(EXT_OCTEON)
    549   ECase(EXT_5900)
    550   ECase(EXT_4650)
    551   ECase(EXT_4010)
    552   ECase(EXT_4100)
    553   ECase(EXT_3900)
    554   ECase(EXT_10000)
    555   ECase(EXT_SB1)
    556   ECase(EXT_4111)
    557   ECase(EXT_4120)
    558   ECase(EXT_5400)
    559   ECase(EXT_5500)
    560   ECase(EXT_LOONGSON_2E)
    561   ECase(EXT_LOONGSON_2F)
    562   ECase(EXT_OCTEON3)
    563 #undef ECase
    564 }
    565 
    566 void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
    567     IO &IO, ELFYAML::MIPS_ISA &Value) {
    568   IO.enumCase(Value, "MIPS1", 1);
    569   IO.enumCase(Value, "MIPS2", 2);
    570   IO.enumCase(Value, "MIPS3", 3);
    571   IO.enumCase(Value, "MIPS4", 4);
    572   IO.enumCase(Value, "MIPS5", 5);
    573   IO.enumCase(Value, "MIPS32", 32);
    574   IO.enumCase(Value, "MIPS64", 64);
    575 }
    576 
    577 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
    578     IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
    579 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X);
    580   BCase(DSP)
    581   BCase(DSPR2)
    582   BCase(EVA)
    583   BCase(MCU)
    584   BCase(MDMX)
    585   BCase(MIPS3D)
    586   BCase(MT)
    587   BCase(SMARTMIPS)
    588   BCase(VIRT)
    589   BCase(MSA)
    590   BCase(MIPS16)
    591   BCase(MICROMIPS)
    592   BCase(XPA)
    593 #undef BCase
    594 }
    595 
    596 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
    597     IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
    598 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X);
    599   BCase(ODDSPREG)
    600 #undef BCase
    601 }
    602 
    603 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
    604                                                  ELFYAML::FileHeader &FileHdr) {
    605   IO.mapRequired("Class", FileHdr.Class);
    606   IO.mapRequired("Data", FileHdr.Data);
    607   IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
    608   IO.mapRequired("Type", FileHdr.Type);
    609   IO.mapRequired("Machine", FileHdr.Machine);
    610   IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
    611   IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
    612 }
    613 
    614 namespace {
    615 struct NormalizedOther {
    616   NormalizedOther(IO &)
    617       : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {}
    618   NormalizedOther(IO &, uint8_t Original)
    619       : Visibility(Original & 0x3), Other(Original & ~0x3) {}
    620 
    621   uint8_t denormalize(IO &) { return Visibility | Other; }
    622 
    623   ELFYAML::ELF_STV Visibility;
    624   ELFYAML::ELF_STO Other;
    625 };
    626 }
    627 
    628 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
    629   IO.mapOptional("Name", Symbol.Name, StringRef());
    630   IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
    631   IO.mapOptional("Section", Symbol.Section, StringRef());
    632   IO.mapOptional("Value", Symbol.Value, Hex64(0));
    633   IO.mapOptional("Size", Symbol.Size, Hex64(0));
    634 
    635   MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other);
    636   IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0));
    637   IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0));
    638 }
    639 
    640 void MappingTraits<ELFYAML::LocalGlobalWeakSymbols>::mapping(
    641     IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols) {
    642   IO.mapOptional("Local", Symbols.Local);
    643   IO.mapOptional("Global", Symbols.Global);
    644   IO.mapOptional("Weak", Symbols.Weak);
    645 }
    646 
    647 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
    648   IO.mapOptional("Name", Section.Name, StringRef());
    649   IO.mapRequired("Type", Section.Type);
    650   IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
    651   IO.mapOptional("Address", Section.Address, Hex64(0));
    652   IO.mapOptional("Link", Section.Link, StringRef());
    653   IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
    654   IO.mapOptional("Info", Section.Info, StringRef());
    655 }
    656 
    657 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
    658   commonSectionMapping(IO, Section);
    659   IO.mapOptional("Content", Section.Content);
    660   IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
    661 }
    662 
    663 static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
    664   commonSectionMapping(IO, Section);
    665   IO.mapOptional("Size", Section.Size, Hex64(0));
    666 }
    667 
    668 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
    669   commonSectionMapping(IO, Section);
    670   IO.mapOptional("Relocations", Section.Relocations);
    671 }
    672 
    673 static void groupSectionMapping(IO &IO, ELFYAML::Group &group) {
    674   commonSectionMapping(IO, group);
    675   IO.mapRequired("Members", group.Members);
    676 }
    677 
    678 void MappingTraits<ELFYAML::SectionOrType>::mapping(
    679     IO &IO, ELFYAML::SectionOrType &sectionOrType) {
    680   IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
    681 }
    682 
    683 static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) {
    684   commonSectionMapping(IO, Section);
    685   IO.mapOptional("Version", Section.Version, Hex16(0));
    686   IO.mapRequired("ISA", Section.ISALevel);
    687   IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
    688   IO.mapOptional("ISAExtension", Section.ISAExtension,
    689                  ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
    690   IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
    691   IO.mapOptional("FpABI", Section.FpABI,
    692                  ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
    693   IO.mapOptional("GPRSize", Section.GPRSize,
    694                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
    695   IO.mapOptional("CPR1Size", Section.CPR1Size,
    696                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
    697   IO.mapOptional("CPR2Size", Section.CPR2Size,
    698                  ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
    699   IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
    700   IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
    701 }
    702 
    703 void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
    704     IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
    705   ELFYAML::ELF_SHT sectionType;
    706   if (IO.outputting())
    707     sectionType = Section->Type;
    708   else
    709     IO.mapRequired("Type", sectionType);
    710 
    711   switch (sectionType) {
    712   case ELF::SHT_REL:
    713   case ELF::SHT_RELA:
    714     if (!IO.outputting())
    715       Section.reset(new ELFYAML::RelocationSection());
    716     sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
    717     break;
    718   case ELF::SHT_GROUP:
    719     if (!IO.outputting())
    720       Section.reset(new ELFYAML::Group());
    721     groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
    722     break;
    723   case ELF::SHT_NOBITS:
    724     if (!IO.outputting())
    725       Section.reset(new ELFYAML::NoBitsSection());
    726     sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
    727     break;
    728   case ELF::SHT_MIPS_ABIFLAGS:
    729     if (!IO.outputting())
    730       Section.reset(new ELFYAML::MipsABIFlags());
    731     sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
    732     break;
    733   default:
    734     if (!IO.outputting())
    735       Section.reset(new ELFYAML::RawContentSection());
    736     sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
    737   }
    738 }
    739 
    740 StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
    741     IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
    742   const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
    743   if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
    744     return StringRef();
    745   return "Section size must be greater or equal to the content size";
    746 }
    747 
    748 namespace {
    749 struct NormalizedMips64RelType {
    750   NormalizedMips64RelType(IO &)
    751       : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
    752         Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
    753         Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
    754         SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
    755   NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
    756       : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
    757         Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
    758 
    759   ELFYAML::ELF_REL denormalize(IO &) {
    760     ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
    761     return Res;
    762   }
    763 
    764   ELFYAML::ELF_REL Type;
    765   ELFYAML::ELF_REL Type2;
    766   ELFYAML::ELF_REL Type3;
    767   ELFYAML::ELF_RSS SpecSym;
    768 };
    769 }
    770 
    771 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
    772                                                  ELFYAML::Relocation &Rel) {
    773   const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
    774   assert(Object && "The IO context is not initialized");
    775 
    776   IO.mapRequired("Offset", Rel.Offset);
    777   IO.mapRequired("Symbol", Rel.Symbol);
    778 
    779   if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
    780       Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
    781     MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
    782         IO, Rel.Type);
    783     IO.mapRequired("Type", Key->Type);
    784     IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
    785     IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
    786     IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
    787   } else
    788     IO.mapRequired("Type", Rel.Type);
    789 
    790   IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
    791 }
    792 
    793 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
    794   assert(!IO.getContext() && "The IO context is initialized already");
    795   IO.setContext(&Object);
    796   IO.mapRequired("FileHeader", Object.Header);
    797   IO.mapOptional("Sections", Object.Sections);
    798   IO.mapOptional("Symbols", Object.Symbols);
    799   IO.setContext(nullptr);
    800 }
    801 
    802 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
    803 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
    804 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
    805 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
    806 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
    807 
    808 } // end namespace yaml
    809 } // end namespace llvm
    810