Home | History | Annotate | Download | only in llvm-readobj
      1 //===-- ELFDumper.cpp - ELF-specific dumper ---------------------*- 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 /// \file
     11 /// \brief This file implements the ELF-specific dumper for llvm-readobj.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "llvm-readobj.h"
     16 #include "Error.h"
     17 #include "ObjDumper.h"
     18 #include "StreamWriter.h"
     19 
     20 #include "llvm/ADT/SmallString.h"
     21 #include "llvm/Object/ELF.h"
     22 #include "llvm/Support/Compiler.h"
     23 #include "llvm/Support/Format.h"
     24 #include "llvm/Support/MathExtras.h"
     25 #include "llvm/Support/raw_ostream.h"
     26 
     27 using namespace llvm;
     28 using namespace llvm::object;
     29 using namespace ELF;
     30 
     31 
     32 #define LLVM_READOBJ_ENUM_CASE(ns, enum) \
     33   case ns::enum: return #enum;
     34 
     35 namespace {
     36 
     37 template<typename ELFT>
     38 class ELFDumper : public ObjDumper {
     39 public:
     40   ELFDumper(const ELFObjectFile<ELFT> *Obj, StreamWriter& Writer)
     41     : ObjDumper(Writer)
     42     , Obj(Obj) { }
     43 
     44   virtual void printFileHeaders() LLVM_OVERRIDE;
     45   virtual void printSections() LLVM_OVERRIDE;
     46   virtual void printRelocations() LLVM_OVERRIDE;
     47   virtual void printSymbols() LLVM_OVERRIDE;
     48   virtual void printDynamicSymbols() LLVM_OVERRIDE;
     49   virtual void printUnwindInfo() LLVM_OVERRIDE;
     50 
     51   virtual void printDynamicTable() LLVM_OVERRIDE;
     52   virtual void printNeededLibraries() LLVM_OVERRIDE;
     53   virtual void printProgramHeaders() LLVM_OVERRIDE;
     54 
     55 private:
     56   typedef ELFObjectFile<ELFT> ELFO;
     57   typedef typename ELFO::Elf_Shdr Elf_Shdr;
     58   typedef typename ELFO::Elf_Sym Elf_Sym;
     59 
     60   void printSymbol(symbol_iterator SymI, bool IsDynamic = false);
     61 
     62   void printRelocation(section_iterator SecI, relocation_iterator RelI);
     63 
     64   const ELFO *Obj;
     65 };
     66 
     67 } // namespace
     68 
     69 
     70 namespace llvm {
     71 
     72 template <class ELFT>
     73 static error_code createELFDumper(const ELFObjectFile<ELFT> *Obj,
     74                                   StreamWriter &Writer,
     75                                   OwningPtr<ObjDumper> &Result) {
     76   Result.reset(new ELFDumper<ELFT>(Obj, Writer));
     77   return readobj_error::success;
     78 }
     79 
     80 error_code createELFDumper(const object::ObjectFile *Obj,
     81                            StreamWriter& Writer,
     82                            OwningPtr<ObjDumper> &Result) {
     83   // Little-endian 32-bit
     84   if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
     85     return createELFDumper(ELFObj, Writer, Result);
     86 
     87   // Big-endian 32-bit
     88   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
     89     return createELFDumper(ELFObj, Writer, Result);
     90 
     91   // Little-endian 64-bit
     92   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
     93     return createELFDumper(ELFObj, Writer, Result);
     94 
     95   // Big-endian 64-bit
     96   if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
     97     return createELFDumper(ELFObj, Writer, Result);
     98 
     99   return readobj_error::unsupported_obj_file_format;
    100 }
    101 
    102 } // namespace llvm
    103 
    104 
    105 static const EnumEntry<unsigned> ElfClass[] = {
    106   { "None",   ELF::ELFCLASSNONE },
    107   { "32-bit", ELF::ELFCLASS32   },
    108   { "64-bit", ELF::ELFCLASS64   },
    109 };
    110 
    111 static const EnumEntry<unsigned> ElfDataEncoding[] = {
    112   { "None",         ELF::ELFDATANONE },
    113   { "LittleEndian", ELF::ELFDATA2LSB },
    114   { "BigEndian",    ELF::ELFDATA2MSB },
    115 };
    116 
    117 static const EnumEntry<unsigned> ElfObjectFileType[] = {
    118   { "None",         ELF::ET_NONE },
    119   { "Relocatable",  ELF::ET_REL  },
    120   { "Executable",   ELF::ET_EXEC },
    121   { "SharedObject", ELF::ET_DYN  },
    122   { "Core",         ELF::ET_CORE },
    123 };
    124 
    125 static const EnumEntry<unsigned> ElfOSABI[] = {
    126   { "SystemV",      ELF::ELFOSABI_NONE         },
    127   { "HPUX",         ELF::ELFOSABI_HPUX         },
    128   { "NetBSD",       ELF::ELFOSABI_NETBSD       },
    129   { "GNU/Linux",    ELF::ELFOSABI_LINUX        },
    130   { "GNU/Hurd",     ELF::ELFOSABI_HURD         },
    131   { "Solaris",      ELF::ELFOSABI_SOLARIS      },
    132   { "AIX",          ELF::ELFOSABI_AIX          },
    133   { "IRIX",         ELF::ELFOSABI_IRIX         },
    134   { "FreeBSD",      ELF::ELFOSABI_FREEBSD      },
    135   { "TRU64",        ELF::ELFOSABI_TRU64        },
    136   { "Modesto",      ELF::ELFOSABI_MODESTO      },
    137   { "OpenBSD",      ELF::ELFOSABI_OPENBSD      },
    138   { "OpenVMS",      ELF::ELFOSABI_OPENVMS      },
    139   { "NSK",          ELF::ELFOSABI_NSK          },
    140   { "AROS",         ELF::ELFOSABI_AROS         },
    141   { "FenixOS",      ELF::ELFOSABI_FENIXOS      },
    142   { "C6000_ELFABI", ELF::ELFOSABI_C6000_ELFABI },
    143   { "C6000_LINUX" , ELF::ELFOSABI_C6000_LINUX  },
    144   { "ARM",          ELF::ELFOSABI_ARM          },
    145   { "Standalone"  , ELF::ELFOSABI_STANDALONE   }
    146 };
    147 
    148 static const EnumEntry<unsigned> ElfMachineType[] = {
    149   LLVM_READOBJ_ENUM_ENT(ELF, EM_NONE         ),
    150   LLVM_READOBJ_ENUM_ENT(ELF, EM_M32          ),
    151   LLVM_READOBJ_ENUM_ENT(ELF, EM_SPARC        ),
    152   LLVM_READOBJ_ENUM_ENT(ELF, EM_386          ),
    153   LLVM_READOBJ_ENUM_ENT(ELF, EM_68K          ),
    154   LLVM_READOBJ_ENUM_ENT(ELF, EM_88K          ),
    155   LLVM_READOBJ_ENUM_ENT(ELF, EM_486          ),
    156   LLVM_READOBJ_ENUM_ENT(ELF, EM_860          ),
    157   LLVM_READOBJ_ENUM_ENT(ELF, EM_MIPS         ),
    158   LLVM_READOBJ_ENUM_ENT(ELF, EM_S370         ),
    159   LLVM_READOBJ_ENUM_ENT(ELF, EM_MIPS_RS3_LE  ),
    160   LLVM_READOBJ_ENUM_ENT(ELF, EM_PARISC       ),
    161   LLVM_READOBJ_ENUM_ENT(ELF, EM_VPP500       ),
    162   LLVM_READOBJ_ENUM_ENT(ELF, EM_SPARC32PLUS  ),
    163   LLVM_READOBJ_ENUM_ENT(ELF, EM_960          ),
    164   LLVM_READOBJ_ENUM_ENT(ELF, EM_PPC          ),
    165   LLVM_READOBJ_ENUM_ENT(ELF, EM_PPC64        ),
    166   LLVM_READOBJ_ENUM_ENT(ELF, EM_S390         ),
    167   LLVM_READOBJ_ENUM_ENT(ELF, EM_SPU          ),
    168   LLVM_READOBJ_ENUM_ENT(ELF, EM_V800         ),
    169   LLVM_READOBJ_ENUM_ENT(ELF, EM_FR20         ),
    170   LLVM_READOBJ_ENUM_ENT(ELF, EM_RH32         ),
    171   LLVM_READOBJ_ENUM_ENT(ELF, EM_RCE          ),
    172   LLVM_READOBJ_ENUM_ENT(ELF, EM_ARM          ),
    173   LLVM_READOBJ_ENUM_ENT(ELF, EM_ALPHA        ),
    174   LLVM_READOBJ_ENUM_ENT(ELF, EM_SH           ),
    175   LLVM_READOBJ_ENUM_ENT(ELF, EM_SPARCV9      ),
    176   LLVM_READOBJ_ENUM_ENT(ELF, EM_TRICORE      ),
    177   LLVM_READOBJ_ENUM_ENT(ELF, EM_ARC          ),
    178   LLVM_READOBJ_ENUM_ENT(ELF, EM_H8_300       ),
    179   LLVM_READOBJ_ENUM_ENT(ELF, EM_H8_300H      ),
    180   LLVM_READOBJ_ENUM_ENT(ELF, EM_H8S          ),
    181   LLVM_READOBJ_ENUM_ENT(ELF, EM_H8_500       ),
    182   LLVM_READOBJ_ENUM_ENT(ELF, EM_IA_64        ),
    183   LLVM_READOBJ_ENUM_ENT(ELF, EM_MIPS_X       ),
    184   LLVM_READOBJ_ENUM_ENT(ELF, EM_COLDFIRE     ),
    185   LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC12       ),
    186   LLVM_READOBJ_ENUM_ENT(ELF, EM_MMA          ),
    187   LLVM_READOBJ_ENUM_ENT(ELF, EM_PCP          ),
    188   LLVM_READOBJ_ENUM_ENT(ELF, EM_NCPU         ),
    189   LLVM_READOBJ_ENUM_ENT(ELF, EM_NDR1         ),
    190   LLVM_READOBJ_ENUM_ENT(ELF, EM_STARCORE     ),
    191   LLVM_READOBJ_ENUM_ENT(ELF, EM_ME16         ),
    192   LLVM_READOBJ_ENUM_ENT(ELF, EM_ST100        ),
    193   LLVM_READOBJ_ENUM_ENT(ELF, EM_TINYJ        ),
    194   LLVM_READOBJ_ENUM_ENT(ELF, EM_X86_64       ),
    195   LLVM_READOBJ_ENUM_ENT(ELF, EM_PDSP         ),
    196   LLVM_READOBJ_ENUM_ENT(ELF, EM_PDP10        ),
    197   LLVM_READOBJ_ENUM_ENT(ELF, EM_PDP11        ),
    198   LLVM_READOBJ_ENUM_ENT(ELF, EM_FX66         ),
    199   LLVM_READOBJ_ENUM_ENT(ELF, EM_ST9PLUS      ),
    200   LLVM_READOBJ_ENUM_ENT(ELF, EM_ST7          ),
    201   LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC16       ),
    202   LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC11       ),
    203   LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC08       ),
    204   LLVM_READOBJ_ENUM_ENT(ELF, EM_68HC05       ),
    205   LLVM_READOBJ_ENUM_ENT(ELF, EM_SVX          ),
    206   LLVM_READOBJ_ENUM_ENT(ELF, EM_ST19         ),
    207   LLVM_READOBJ_ENUM_ENT(ELF, EM_VAX          ),
    208   LLVM_READOBJ_ENUM_ENT(ELF, EM_CRIS         ),
    209   LLVM_READOBJ_ENUM_ENT(ELF, EM_JAVELIN      ),
    210   LLVM_READOBJ_ENUM_ENT(ELF, EM_FIREPATH     ),
    211   LLVM_READOBJ_ENUM_ENT(ELF, EM_ZSP          ),
    212   LLVM_READOBJ_ENUM_ENT(ELF, EM_MMIX         ),
    213   LLVM_READOBJ_ENUM_ENT(ELF, EM_HUANY        ),
    214   LLVM_READOBJ_ENUM_ENT(ELF, EM_PRISM        ),
    215   LLVM_READOBJ_ENUM_ENT(ELF, EM_AVR          ),
    216   LLVM_READOBJ_ENUM_ENT(ELF, EM_FR30         ),
    217   LLVM_READOBJ_ENUM_ENT(ELF, EM_D10V         ),
    218   LLVM_READOBJ_ENUM_ENT(ELF, EM_D30V         ),
    219   LLVM_READOBJ_ENUM_ENT(ELF, EM_V850         ),
    220   LLVM_READOBJ_ENUM_ENT(ELF, EM_M32R         ),
    221   LLVM_READOBJ_ENUM_ENT(ELF, EM_MN10300      ),
    222   LLVM_READOBJ_ENUM_ENT(ELF, EM_MN10200      ),
    223   LLVM_READOBJ_ENUM_ENT(ELF, EM_PJ           ),
    224   LLVM_READOBJ_ENUM_ENT(ELF, EM_OPENRISC     ),
    225   LLVM_READOBJ_ENUM_ENT(ELF, EM_ARC_COMPACT  ),
    226   LLVM_READOBJ_ENUM_ENT(ELF, EM_XTENSA       ),
    227   LLVM_READOBJ_ENUM_ENT(ELF, EM_VIDEOCORE    ),
    228   LLVM_READOBJ_ENUM_ENT(ELF, EM_TMM_GPP      ),
    229   LLVM_READOBJ_ENUM_ENT(ELF, EM_NS32K        ),
    230   LLVM_READOBJ_ENUM_ENT(ELF, EM_TPC          ),
    231   LLVM_READOBJ_ENUM_ENT(ELF, EM_SNP1K        ),
    232   LLVM_READOBJ_ENUM_ENT(ELF, EM_ST200        ),
    233   LLVM_READOBJ_ENUM_ENT(ELF, EM_IP2K         ),
    234   LLVM_READOBJ_ENUM_ENT(ELF, EM_MAX          ),
    235   LLVM_READOBJ_ENUM_ENT(ELF, EM_CR           ),
    236   LLVM_READOBJ_ENUM_ENT(ELF, EM_F2MC16       ),
    237   LLVM_READOBJ_ENUM_ENT(ELF, EM_MSP430       ),
    238   LLVM_READOBJ_ENUM_ENT(ELF, EM_BLACKFIN     ),
    239   LLVM_READOBJ_ENUM_ENT(ELF, EM_SE_C33       ),
    240   LLVM_READOBJ_ENUM_ENT(ELF, EM_SEP          ),
    241   LLVM_READOBJ_ENUM_ENT(ELF, EM_ARCA         ),
    242   LLVM_READOBJ_ENUM_ENT(ELF, EM_UNICORE      ),
    243   LLVM_READOBJ_ENUM_ENT(ELF, EM_EXCESS       ),
    244   LLVM_READOBJ_ENUM_ENT(ELF, EM_DXP          ),
    245   LLVM_READOBJ_ENUM_ENT(ELF, EM_ALTERA_NIOS2 ),
    246   LLVM_READOBJ_ENUM_ENT(ELF, EM_CRX          ),
    247   LLVM_READOBJ_ENUM_ENT(ELF, EM_XGATE        ),
    248   LLVM_READOBJ_ENUM_ENT(ELF, EM_C166         ),
    249   LLVM_READOBJ_ENUM_ENT(ELF, EM_M16C         ),
    250   LLVM_READOBJ_ENUM_ENT(ELF, EM_DSPIC30F     ),
    251   LLVM_READOBJ_ENUM_ENT(ELF, EM_CE           ),
    252   LLVM_READOBJ_ENUM_ENT(ELF, EM_M32C         ),
    253   LLVM_READOBJ_ENUM_ENT(ELF, EM_TSK3000      ),
    254   LLVM_READOBJ_ENUM_ENT(ELF, EM_RS08         ),
    255   LLVM_READOBJ_ENUM_ENT(ELF, EM_SHARC        ),
    256   LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG2        ),
    257   LLVM_READOBJ_ENUM_ENT(ELF, EM_SCORE7       ),
    258   LLVM_READOBJ_ENUM_ENT(ELF, EM_DSP24        ),
    259   LLVM_READOBJ_ENUM_ENT(ELF, EM_VIDEOCORE3   ),
    260   LLVM_READOBJ_ENUM_ENT(ELF, EM_LATTICEMICO32),
    261   LLVM_READOBJ_ENUM_ENT(ELF, EM_SE_C17       ),
    262   LLVM_READOBJ_ENUM_ENT(ELF, EM_TI_C6000     ),
    263   LLVM_READOBJ_ENUM_ENT(ELF, EM_TI_C2000     ),
    264   LLVM_READOBJ_ENUM_ENT(ELF, EM_TI_C5500     ),
    265   LLVM_READOBJ_ENUM_ENT(ELF, EM_MMDSP_PLUS   ),
    266   LLVM_READOBJ_ENUM_ENT(ELF, EM_CYPRESS_M8C  ),
    267   LLVM_READOBJ_ENUM_ENT(ELF, EM_R32C         ),
    268   LLVM_READOBJ_ENUM_ENT(ELF, EM_TRIMEDIA     ),
    269   LLVM_READOBJ_ENUM_ENT(ELF, EM_HEXAGON      ),
    270   LLVM_READOBJ_ENUM_ENT(ELF, EM_8051         ),
    271   LLVM_READOBJ_ENUM_ENT(ELF, EM_STXP7X       ),
    272   LLVM_READOBJ_ENUM_ENT(ELF, EM_NDS32        ),
    273   LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG1        ),
    274   LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG1X       ),
    275   LLVM_READOBJ_ENUM_ENT(ELF, EM_MAXQ30       ),
    276   LLVM_READOBJ_ENUM_ENT(ELF, EM_XIMO16       ),
    277   LLVM_READOBJ_ENUM_ENT(ELF, EM_MANIK        ),
    278   LLVM_READOBJ_ENUM_ENT(ELF, EM_CRAYNV2      ),
    279   LLVM_READOBJ_ENUM_ENT(ELF, EM_RX           ),
    280   LLVM_READOBJ_ENUM_ENT(ELF, EM_METAG        ),
    281   LLVM_READOBJ_ENUM_ENT(ELF, EM_MCST_ELBRUS  ),
    282   LLVM_READOBJ_ENUM_ENT(ELF, EM_ECOG16       ),
    283   LLVM_READOBJ_ENUM_ENT(ELF, EM_CR16         ),
    284   LLVM_READOBJ_ENUM_ENT(ELF, EM_ETPU         ),
    285   LLVM_READOBJ_ENUM_ENT(ELF, EM_SLE9X        ),
    286   LLVM_READOBJ_ENUM_ENT(ELF, EM_L10M         ),
    287   LLVM_READOBJ_ENUM_ENT(ELF, EM_K10M         ),
    288   LLVM_READOBJ_ENUM_ENT(ELF, EM_AARCH64      ),
    289   LLVM_READOBJ_ENUM_ENT(ELF, EM_AVR32        ),
    290   LLVM_READOBJ_ENUM_ENT(ELF, EM_STM8         ),
    291   LLVM_READOBJ_ENUM_ENT(ELF, EM_TILE64       ),
    292   LLVM_READOBJ_ENUM_ENT(ELF, EM_TILEPRO      ),
    293   LLVM_READOBJ_ENUM_ENT(ELF, EM_CUDA         ),
    294   LLVM_READOBJ_ENUM_ENT(ELF, EM_TILEGX       ),
    295   LLVM_READOBJ_ENUM_ENT(ELF, EM_CLOUDSHIELD  ),
    296   LLVM_READOBJ_ENUM_ENT(ELF, EM_COREA_1ST    ),
    297   LLVM_READOBJ_ENUM_ENT(ELF, EM_COREA_2ND    ),
    298   LLVM_READOBJ_ENUM_ENT(ELF, EM_ARC_COMPACT2 ),
    299   LLVM_READOBJ_ENUM_ENT(ELF, EM_OPEN8        ),
    300   LLVM_READOBJ_ENUM_ENT(ELF, EM_RL78         ),
    301   LLVM_READOBJ_ENUM_ENT(ELF, EM_VIDEOCORE5   ),
    302   LLVM_READOBJ_ENUM_ENT(ELF, EM_78KOR        ),
    303   LLVM_READOBJ_ENUM_ENT(ELF, EM_56800EX      )
    304 };
    305 
    306 static const EnumEntry<unsigned> ElfSymbolBindings[] = {
    307   { "Local",  ELF::STB_LOCAL  },
    308   { "Global", ELF::STB_GLOBAL },
    309   { "Weak",   ELF::STB_WEAK   }
    310 };
    311 
    312 static const EnumEntry<unsigned> ElfSymbolTypes[] = {
    313   { "None",      ELF::STT_NOTYPE    },
    314   { "Object",    ELF::STT_OBJECT    },
    315   { "Function",  ELF::STT_FUNC      },
    316   { "Section",   ELF::STT_SECTION   },
    317   { "File",      ELF::STT_FILE      },
    318   { "Common",    ELF::STT_COMMON    },
    319   { "TLS",       ELF::STT_TLS       },
    320   { "GNU_IFunc", ELF::STT_GNU_IFUNC }
    321 };
    322 
    323 static const char *getElfSectionType(unsigned Arch, unsigned Type) {
    324   switch (Arch) {
    325   case Triple::arm:
    326     switch (Type) {
    327     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_EXIDX);
    328     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
    329     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
    330     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
    331     LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
    332     }
    333   case Triple::hexagon:
    334     switch (Type) {
    335     LLVM_READOBJ_ENUM_CASE(ELF, SHT_HEX_ORDERED);
    336     }
    337   case Triple::x86_64:
    338     switch (Type) {
    339     LLVM_READOBJ_ENUM_CASE(ELF, SHT_X86_64_UNWIND);
    340     }
    341   case Triple::mips:
    342   case Triple::mipsel:
    343     switch (Type) {
    344     LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
    345     LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
    346     }
    347   }
    348 
    349   switch (Type) {
    350   LLVM_READOBJ_ENUM_CASE(ELF, SHT_NULL              );
    351   LLVM_READOBJ_ENUM_CASE(ELF, SHT_PROGBITS          );
    352   LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB            );
    353   LLVM_READOBJ_ENUM_CASE(ELF, SHT_STRTAB            );
    354   LLVM_READOBJ_ENUM_CASE(ELF, SHT_RELA              );
    355   LLVM_READOBJ_ENUM_CASE(ELF, SHT_HASH              );
    356   LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNAMIC           );
    357   LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOTE              );
    358   LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOBITS            );
    359   LLVM_READOBJ_ENUM_CASE(ELF, SHT_REL               );
    360   LLVM_READOBJ_ENUM_CASE(ELF, SHT_SHLIB             );
    361   LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNSYM            );
    362   LLVM_READOBJ_ENUM_CASE(ELF, SHT_INIT_ARRAY        );
    363   LLVM_READOBJ_ENUM_CASE(ELF, SHT_FINI_ARRAY        );
    364   LLVM_READOBJ_ENUM_CASE(ELF, SHT_PREINIT_ARRAY     );
    365   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GROUP             );
    366   LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX      );
    367   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES    );
    368   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_HASH          );
    369   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verdef        );
    370   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verneed       );
    371   LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_versym        );
    372   default: return "";
    373   }
    374 }
    375 
    376 static const EnumEntry<unsigned> ElfSectionFlags[] = {
    377   LLVM_READOBJ_ENUM_ENT(ELF, SHF_WRITE           ),
    378   LLVM_READOBJ_ENUM_ENT(ELF, SHF_ALLOC           ),
    379   LLVM_READOBJ_ENUM_ENT(ELF, SHF_EXECINSTR       ),
    380   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MERGE           ),
    381   LLVM_READOBJ_ENUM_ENT(ELF, SHF_STRINGS         ),
    382   LLVM_READOBJ_ENUM_ENT(ELF, SHF_INFO_LINK       ),
    383   LLVM_READOBJ_ENUM_ENT(ELF, SHF_LINK_ORDER      ),
    384   LLVM_READOBJ_ENUM_ENT(ELF, SHF_OS_NONCONFORMING),
    385   LLVM_READOBJ_ENUM_ENT(ELF, SHF_GROUP           ),
    386   LLVM_READOBJ_ENUM_ENT(ELF, SHF_TLS             ),
    387   LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION),
    388   LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION),
    389   LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NOSTRIP    )
    390 };
    391 
    392 static const EnumEntry<unsigned> ElfSegmentTypes[] = {
    393   LLVM_READOBJ_ENUM_ENT(ELF, PT_NULL   ),
    394   LLVM_READOBJ_ENUM_ENT(ELF, PT_LOAD   ),
    395   LLVM_READOBJ_ENUM_ENT(ELF, PT_DYNAMIC),
    396   LLVM_READOBJ_ENUM_ENT(ELF, PT_INTERP ),
    397   LLVM_READOBJ_ENUM_ENT(ELF, PT_NOTE   ),
    398   LLVM_READOBJ_ENUM_ENT(ELF, PT_SHLIB  ),
    399   LLVM_READOBJ_ENUM_ENT(ELF, PT_PHDR   ),
    400   LLVM_READOBJ_ENUM_ENT(ELF, PT_TLS    ),
    401 
    402   LLVM_READOBJ_ENUM_ENT(ELF, PT_GNU_EH_FRAME),
    403   LLVM_READOBJ_ENUM_ENT(ELF, PT_SUNW_EH_FRAME),
    404   LLVM_READOBJ_ENUM_ENT(ELF, PT_SUNW_UNWIND),
    405 
    406   LLVM_READOBJ_ENUM_ENT(ELF, PT_GNU_STACK),
    407   LLVM_READOBJ_ENUM_ENT(ELF, PT_GNU_RELRO),
    408 
    409   LLVM_READOBJ_ENUM_ENT(ELF, PT_ARM_EXIDX),
    410   LLVM_READOBJ_ENUM_ENT(ELF, PT_ARM_UNWIND)
    411 };
    412 
    413 static const EnumEntry<unsigned> ElfSegmentFlags[] = {
    414   LLVM_READOBJ_ENUM_ENT(ELF, PF_X),
    415   LLVM_READOBJ_ENUM_ENT(ELF, PF_W),
    416   LLVM_READOBJ_ENUM_ENT(ELF, PF_R)
    417 };
    418 
    419 
    420 template<class ELFT>
    421 void ELFDumper<ELFT>::printFileHeaders() {
    422   error_code EC;
    423 
    424   const typename ELFO::Elf_Ehdr *Header = Obj->getElfHeader();
    425 
    426   {
    427     DictScope D(W, "ElfHeader");
    428     {
    429       DictScope D(W, "Ident");
    430       W.printBinary("Magic", makeArrayRef(Header->e_ident).slice(ELF::EI_MAG0,
    431                                                                  4));
    432       W.printEnum  ("Class", Header->e_ident[ELF::EI_CLASS],
    433                       makeArrayRef(ElfClass));
    434       W.printEnum  ("DataEncoding", Header->e_ident[ELF::EI_DATA],
    435                       makeArrayRef(ElfDataEncoding));
    436       W.printNumber("FileVersion", Header->e_ident[ELF::EI_VERSION]);
    437       W.printEnum  ("OS/ABI", Header->e_ident[ELF::EI_OSABI],
    438                       makeArrayRef(ElfOSABI));
    439       W.printNumber("ABIVersion", Header->e_ident[ELF::EI_ABIVERSION]);
    440       W.printBinary("Unused", makeArrayRef(Header->e_ident).slice(ELF::EI_PAD));
    441     }
    442 
    443     W.printEnum  ("Type", Header->e_type, makeArrayRef(ElfObjectFileType));
    444     W.printEnum  ("Machine", Header->e_machine, makeArrayRef(ElfMachineType));
    445     W.printNumber("Version", Header->e_version);
    446     W.printHex   ("Entry", Header->e_entry);
    447     W.printHex   ("ProgramHeaderOffset", Header->e_phoff);
    448     W.printHex   ("SectionHeaderOffset", Header->e_shoff);
    449     W.printFlags ("Flags", Header->e_flags);
    450     W.printNumber("HeaderSize", Header->e_ehsize);
    451     W.printNumber("ProgramHeaderEntrySize", Header->e_phentsize);
    452     W.printNumber("ProgramHeaderCount", Header->e_phnum);
    453     W.printNumber("SectionHeaderEntrySize", Header->e_shentsize);
    454     W.printNumber("SectionHeaderCount", Header->e_shnum);
    455     W.printNumber("StringTableSectionIndex", Header->e_shstrndx);
    456   }
    457 }
    458 
    459 template<class ELFT>
    460 void ELFDumper<ELFT>::printSections() {
    461   ListScope SectionsD(W, "Sections");
    462 
    463   int SectionIndex = -1;
    464   error_code EC;
    465   for (section_iterator SecI = Obj->begin_sections(),
    466                         SecE = Obj->end_sections();
    467                         SecI != SecE; SecI.increment(EC)) {
    468     if (error(EC)) break;
    469 
    470     ++SectionIndex;
    471 
    472     const Elf_Shdr *Section = Obj->getElfSection(SecI);
    473     StringRef Name;
    474     if (error(SecI->getName(Name)))
    475         Name = "";
    476 
    477     DictScope SectionD(W, "Section");
    478     W.printNumber("Index", SectionIndex);
    479     W.printNumber("Name", Name, Section->sh_name);
    480     W.printHex   ("Type", getElfSectionType(Obj->getArch(), Section->sh_type),
    481                     Section->sh_type);
    482     W.printFlags ("Flags", Section->sh_flags, makeArrayRef(ElfSectionFlags));
    483     W.printHex   ("Address", Section->sh_addr);
    484     W.printHex   ("Offset", Section->sh_offset);
    485     W.printNumber("Size", Section->sh_size);
    486     W.printNumber("Link", Section->sh_link);
    487     W.printNumber("Info", Section->sh_info);
    488     W.printNumber("AddressAlignment", Section->sh_addralign);
    489     W.printNumber("EntrySize", Section->sh_entsize);
    490 
    491     if (opts::SectionRelocations) {
    492       ListScope D(W, "Relocations");
    493       for (relocation_iterator RelI = SecI->begin_relocations(),
    494                                RelE = SecI->end_relocations();
    495                                RelI != RelE; RelI.increment(EC)) {
    496         if (error(EC)) break;
    497 
    498         printRelocation(SecI, RelI);
    499       }
    500     }
    501 
    502     if (opts::SectionSymbols) {
    503       ListScope D(W, "Symbols");
    504       for (symbol_iterator SymI = Obj->begin_symbols(),
    505                            SymE = Obj->end_symbols();
    506                            SymI != SymE; SymI.increment(EC)) {
    507         if (error(EC)) break;
    508 
    509         bool Contained = false;
    510         if (SecI->containsSymbol(*SymI, Contained) || !Contained)
    511           continue;
    512 
    513         printSymbol(SymI);
    514       }
    515     }
    516 
    517     if (opts::SectionData) {
    518       StringRef Data;
    519       if (error(SecI->getContents(Data))) break;
    520 
    521       W.printBinaryBlock("SectionData", Data);
    522     }
    523   }
    524 }
    525 
    526 template<class ELFT>
    527 void ELFDumper<ELFT>::printRelocations() {
    528   ListScope D(W, "Relocations");
    529 
    530   error_code EC;
    531   int SectionNumber = -1;
    532   for (section_iterator SecI = Obj->begin_sections(),
    533                         SecE = Obj->end_sections();
    534                         SecI != SecE; SecI.increment(EC)) {
    535     if (error(EC)) break;
    536 
    537     ++SectionNumber;
    538     StringRef Name;
    539     if (error(SecI->getName(Name)))
    540       continue;
    541 
    542     bool PrintedGroup = false;
    543     for (relocation_iterator RelI = SecI->begin_relocations(),
    544                              RelE = SecI->end_relocations();
    545                              RelI != RelE; RelI.increment(EC)) {
    546       if (error(EC)) break;
    547 
    548       if (!PrintedGroup) {
    549         W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
    550         W.indent();
    551         PrintedGroup = true;
    552       }
    553 
    554       printRelocation(SecI, RelI);
    555     }
    556 
    557     if (PrintedGroup) {
    558       W.unindent();
    559       W.startLine() << "}\n";
    560     }
    561   }
    562 }
    563 
    564 template<class ELFT>
    565 void ELFDumper<ELFT>::printRelocation(section_iterator Sec,
    566                                       relocation_iterator RelI) {
    567   uint64_t Offset;
    568   uint64_t RelocType;
    569   SmallString<32> RelocName;
    570   int64_t Addend;
    571   StringRef SymbolName;
    572   if (Obj->getElfHeader()->e_type == ELF::ET_REL){
    573     if (error(RelI->getOffset(Offset))) return;
    574   } else {
    575     if (error(RelI->getAddress(Offset))) return;
    576   }
    577   if (error(RelI->getType(RelocType))) return;
    578   if (error(RelI->getTypeName(RelocName))) return;
    579   if (error(getELFRelocationAddend(*RelI, Addend))) return;
    580   symbol_iterator Symbol = RelI->getSymbol();
    581   if (Symbol != Obj->end_symbols() && error(Symbol->getName(SymbolName)))
    582     return;
    583 
    584   if (opts::ExpandRelocs) {
    585     DictScope Group(W, "Relocation");
    586     W.printHex("Offset", Offset);
    587     W.printNumber("Type", RelocName, RelocType);
    588     W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
    589     W.printHex("Addend", Addend);
    590   } else {
    591     raw_ostream& OS = W.startLine();
    592     OS << W.hex(Offset)
    593        << " " << RelocName
    594        << " " << (SymbolName.size() > 0 ? SymbolName : "-")
    595        << " " << W.hex(Addend)
    596        << "\n";
    597   }
    598 }
    599 
    600 template<class ELFT>
    601 void ELFDumper<ELFT>::printSymbols() {
    602   ListScope Group(W, "Symbols");
    603 
    604   error_code EC;
    605   for (symbol_iterator SymI = Obj->begin_symbols(), SymE = Obj->end_symbols();
    606                        SymI != SymE; SymI.increment(EC)) {
    607     if (error(EC)) break;
    608 
    609     printSymbol(SymI);
    610   }
    611 }
    612 
    613 template<class ELFT>
    614 void ELFDumper<ELFT>::printDynamicSymbols() {
    615   ListScope Group(W, "DynamicSymbols");
    616 
    617   error_code EC;
    618   for (symbol_iterator SymI = Obj->begin_dynamic_symbols(),
    619                        SymE = Obj->end_dynamic_symbols();
    620                        SymI != SymE; SymI.increment(EC)) {
    621     if (error(EC)) break;
    622 
    623     printSymbol(SymI, true);
    624   }
    625 }
    626 
    627 template<class ELFT>
    628 void ELFDumper<ELFT>::printSymbol(symbol_iterator SymI, bool IsDynamic) {
    629   error_code EC;
    630 
    631   const Elf_Sym *Symbol = Obj->getElfSymbol(SymI);
    632   const Elf_Shdr *Section = Obj->getSection(Symbol);
    633 
    634   StringRef SymbolName;
    635   if (SymI->getName(SymbolName))
    636     SymbolName = "";
    637 
    638   StringRef SectionName = "";
    639   if (Section)
    640     Obj->getSectionName(Section, SectionName);
    641 
    642   std::string FullSymbolName(SymbolName);
    643   if (IsDynamic) {
    644     StringRef Version;
    645     bool IsDefault;
    646     if (error(Obj->getSymbolVersion(*SymI, Version, IsDefault)))
    647       return;
    648     if (!Version.empty()) {
    649       FullSymbolName += (IsDefault ? "@@" : "@");
    650       FullSymbolName += Version;
    651     }
    652   }
    653 
    654   DictScope D(W, "Symbol");
    655   W.printNumber("Name", FullSymbolName, Symbol->st_name);
    656   W.printHex   ("Value", Symbol->st_value);
    657   W.printNumber("Size", Symbol->st_size);
    658   W.printEnum  ("Binding", Symbol->getBinding(),
    659                   makeArrayRef(ElfSymbolBindings));
    660   W.printEnum  ("Type", Symbol->getType(), makeArrayRef(ElfSymbolTypes));
    661   W.printNumber("Other", Symbol->st_other);
    662   W.printHex   ("Section", SectionName, Symbol->st_shndx);
    663 }
    664 
    665 #define LLVM_READOBJ_TYPE_CASE(name) \
    666   case DT_##name: return #name
    667 
    668 static const char *getTypeString(uint64_t Type) {
    669   switch (Type) {
    670   LLVM_READOBJ_TYPE_CASE(BIND_NOW);
    671   LLVM_READOBJ_TYPE_CASE(DEBUG);
    672   LLVM_READOBJ_TYPE_CASE(FINI);
    673   LLVM_READOBJ_TYPE_CASE(FINI_ARRAY);
    674   LLVM_READOBJ_TYPE_CASE(FINI_ARRAYSZ);
    675   LLVM_READOBJ_TYPE_CASE(FLAGS);
    676   LLVM_READOBJ_TYPE_CASE(HASH);
    677   LLVM_READOBJ_TYPE_CASE(INIT);
    678   LLVM_READOBJ_TYPE_CASE(INIT_ARRAY);
    679   LLVM_READOBJ_TYPE_CASE(INIT_ARRAYSZ);
    680   LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAY);
    681   LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAYSZ);
    682   LLVM_READOBJ_TYPE_CASE(JMPREL);
    683   LLVM_READOBJ_TYPE_CASE(NEEDED);
    684   LLVM_READOBJ_TYPE_CASE(NULL);
    685   LLVM_READOBJ_TYPE_CASE(PLTGOT);
    686   LLVM_READOBJ_TYPE_CASE(PLTREL);
    687   LLVM_READOBJ_TYPE_CASE(PLTRELSZ);
    688   LLVM_READOBJ_TYPE_CASE(REL);
    689   LLVM_READOBJ_TYPE_CASE(RELA);
    690   LLVM_READOBJ_TYPE_CASE(RELENT);
    691   LLVM_READOBJ_TYPE_CASE(RELSZ);
    692   LLVM_READOBJ_TYPE_CASE(RELAENT);
    693   LLVM_READOBJ_TYPE_CASE(RELASZ);
    694   LLVM_READOBJ_TYPE_CASE(RPATH);
    695   LLVM_READOBJ_TYPE_CASE(RUNPATH);
    696   LLVM_READOBJ_TYPE_CASE(SONAME);
    697   LLVM_READOBJ_TYPE_CASE(STRSZ);
    698   LLVM_READOBJ_TYPE_CASE(STRTAB);
    699   LLVM_READOBJ_TYPE_CASE(SYMBOLIC);
    700   LLVM_READOBJ_TYPE_CASE(SYMENT);
    701   LLVM_READOBJ_TYPE_CASE(SYMTAB);
    702   LLVM_READOBJ_TYPE_CASE(TEXTREL);
    703   default: return "unknown";
    704   }
    705 }
    706 
    707 #undef LLVM_READOBJ_TYPE_CASE
    708 
    709 template<class ELFT>
    710 static void printValue(const ELFObjectFile<ELFT> *O, uint64_t Type,
    711                        uint64_t Value, bool Is64, raw_ostream &OS) {
    712   switch (Type) {
    713   case DT_PLTREL:
    714     if (Value == DT_REL) {
    715       OS << "REL";
    716       break;
    717     } else if (Value == DT_RELA) {
    718       OS << "RELA";
    719       break;
    720     }
    721   // Fallthrough.
    722   case DT_PLTGOT:
    723   case DT_HASH:
    724   case DT_STRTAB:
    725   case DT_SYMTAB:
    726   case DT_RELA:
    727   case DT_INIT:
    728   case DT_FINI:
    729   case DT_REL:
    730   case DT_JMPREL:
    731   case DT_INIT_ARRAY:
    732   case DT_FINI_ARRAY:
    733   case DT_PREINIT_ARRAY:
    734   case DT_DEBUG:
    735   case DT_NULL:
    736     OS << format("0x%" PRIX64, Value);
    737     break;
    738   case DT_PLTRELSZ:
    739   case DT_RELASZ:
    740   case DT_RELAENT:
    741   case DT_STRSZ:
    742   case DT_SYMENT:
    743   case DT_RELSZ:
    744   case DT_RELENT:
    745   case DT_INIT_ARRAYSZ:
    746   case DT_FINI_ARRAYSZ:
    747   case DT_PREINIT_ARRAYSZ:
    748     OS << Value << " (bytes)";
    749     break;
    750   case DT_NEEDED:
    751     OS << "SharedLibrary ("
    752        << O->getString(O->getDynamicStringTableSectionHeader(), Value) << ")";
    753     break;
    754   case DT_SONAME:
    755     OS << "LibrarySoname ("
    756        << O->getString(O->getDynamicStringTableSectionHeader(), Value) << ")";
    757     break;
    758   }
    759 }
    760 
    761 template<class ELFT>
    762 void ELFDumper<ELFT>::printUnwindInfo() {
    763   W.startLine() << "UnwindInfo not implemented.\n";
    764 }
    765 
    766 template<class ELFT>
    767 void ELFDumper<ELFT>::printDynamicTable() {
    768   typedef typename ELFO::Elf_Dyn_iterator EDI;
    769   EDI Start = Obj->begin_dynamic_table(),
    770       End = Obj->end_dynamic_table(true);
    771 
    772   if (Start == End)
    773     return;
    774 
    775   ptrdiff_t Total = std::distance(Start, End);
    776   raw_ostream &OS = W.getOStream();
    777   W.startLine() << "DynamicSection [ (" << Total << " entries)\n";
    778 
    779   bool Is64 = Obj->getBytesInAddress() == 8;
    780 
    781   W.startLine()
    782      << "  Tag" << (Is64 ? "                " : "        ") << "Type"
    783      << "                 " << "Name/Value\n";
    784   for (; Start != End; ++Start) {
    785     W.startLine()
    786        << "  "
    787        << format(Is64 ? "0x%016" PRIX64 : "0x%08" PRIX64, Start->getTag())
    788        << " " << format("%-21s", getTypeString(Start->getTag()));
    789     printValue(Obj, Start->getTag(), Start->getVal(), Is64, OS);
    790     OS << "\n";
    791   }
    792 
    793   W.startLine() << "]\n";
    794 }
    795 
    796 static bool compareLibraryName(const LibraryRef &L, const LibraryRef &R) {
    797   StringRef LPath, RPath;
    798   L.getPath(LPath);
    799   R.getPath(RPath);
    800   return LPath < RPath;
    801 }
    802 
    803 template<class ELFT>
    804 void ELFDumper<ELFT>::printNeededLibraries() {
    805   ListScope D(W, "NeededLibraries");
    806 
    807   error_code EC;
    808 
    809   typedef std::vector<LibraryRef> LibsTy;
    810   LibsTy Libs;
    811 
    812   for (library_iterator I = Obj->begin_libraries_needed(),
    813                         E = Obj->end_libraries_needed();
    814                         I != E; I.increment(EC)) {
    815     if (EC)
    816       report_fatal_error("Needed libraries iteration failed");
    817 
    818     Libs.push_back(*I);
    819   }
    820 
    821   std::sort(Libs.begin(), Libs.end(), &compareLibraryName);
    822 
    823   for (LibsTy::const_iterator I = Libs.begin(), E = Libs.end();
    824                                   I != E; ++I) {
    825     StringRef Path;
    826     I->getPath(Path);
    827     outs() << "  " << Path << "\n";
    828   }
    829 }
    830 
    831 template<class ELFT>
    832 void ELFDumper<ELFT>::printProgramHeaders() {
    833   ListScope L(W, "ProgramHeaders");
    834 
    835   for (typename ELFO::Elf_Phdr_Iter PI = Obj->begin_program_headers(),
    836                                     PE = Obj->end_program_headers();
    837                                     PI != PE; ++PI) {
    838     DictScope P(W, "ProgramHeader");
    839     W.printEnum  ("Type", PI->p_type, makeArrayRef(ElfSegmentTypes));
    840     W.printHex   ("Offset", PI->p_offset);
    841     W.printHex   ("VirtualAddress", PI->p_vaddr);
    842     W.printHex   ("PhysicalAddress", PI->p_paddr);
    843     W.printNumber("FileSize", PI->p_filesz);
    844     W.printNumber("MemSize", PI->p_memsz);
    845     W.printFlags ("Flags", PI->p_flags, makeArrayRef(ElfSegmentFlags));
    846     W.printNumber("Alignment", PI->p_align);
    847   }
    848 }
    849