diff options
Diffstat (limited to 'tools/llvm-objdump')
| -rw-r--r-- | tools/llvm-objdump/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tools/llvm-objdump/COFFDump.cpp | 355 | ||||
| -rw-r--r-- | tools/llvm-objdump/ELFDump.cpp | 100 | ||||
| -rw-r--r-- | tools/llvm-objdump/MachODump.cpp | 59 | ||||
| -rw-r--r-- | tools/llvm-objdump/llvm-objdump.cpp | 97 | ||||
| -rw-r--r-- | tools/llvm-objdump/llvm-objdump.h | 11 | 
6 files changed, 563 insertions, 61 deletions
| diff --git a/tools/llvm-objdump/CMakeLists.txt b/tools/llvm-objdump/CMakeLists.txt index f3b2e1fe4196..0c49d0b457f2 100644 --- a/tools/llvm-objdump/CMakeLists.txt +++ b/tools/llvm-objdump/CMakeLists.txt @@ -9,6 +9,8 @@ set(LLVM_LINK_COMPONENTS  add_llvm_tool(llvm-objdump    llvm-objdump.cpp +  COFFDump.cpp +  ELFDump.cpp    MachODump.cpp    MCFunction.cpp    ) diff --git a/tools/llvm-objdump/COFFDump.cpp b/tools/llvm-objdump/COFFDump.cpp new file mode 100644 index 000000000000..2ada683f2df1 --- /dev/null +++ b/tools/llvm-objdump/COFFDump.cpp @@ -0,0 +1,355 @@ +//===-- COFFDump.cpp - COFF-specific dumper ---------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file implements the COFF-specific dumper for llvm-objdump. +/// It outputs the Win64 EH data structures as plain text. +/// The encoding of the unwind codes is decribed in MSDN: +/// http://msdn.microsoft.com/en-us/library/ck9asaa9.aspx +/// +//===----------------------------------------------------------------------===// + +#include "llvm-objdump.h" +#include "llvm/Object/COFF.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/Win64EH.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/system_error.h" +#include <algorithm> +#include <cstring> + +using namespace llvm; +using namespace object; +using namespace llvm::Win64EH; + +// Returns the name of the unwind code. +static StringRef getUnwindCodeTypeName(uint8_t Code) { +  switch(Code) { +  default: llvm_unreachable("Invalid unwind code"); +  case UOP_PushNonVol: return "UOP_PushNonVol"; +  case UOP_AllocLarge: return "UOP_AllocLarge"; +  case UOP_AllocSmall: return "UOP_AllocSmall"; +  case UOP_SetFPReg: return "UOP_SetFPReg"; +  case UOP_SaveNonVol: return "UOP_SaveNonVol"; +  case UOP_SaveNonVolBig: return "UOP_SaveNonVolBig"; +  case UOP_SaveXMM128: return "UOP_SaveXMM128"; +  case UOP_SaveXMM128Big: return "UOP_SaveXMM128Big"; +  case UOP_PushMachFrame: return "UOP_PushMachFrame"; +  } +} + +// Returns the name of a referenced register. +static StringRef getUnwindRegisterName(uint8_t Reg) { +  switch(Reg) { +  default: llvm_unreachable("Invalid register"); +  case 0: return "RAX"; +  case 1: return "RCX"; +  case 2: return "RDX"; +  case 3: return "RBX"; +  case 4: return "RSP"; +  case 5: return "RBP"; +  case 6: return "RSI"; +  case 7: return "RDI"; +  case 8: return "R8"; +  case 9: return "R9"; +  case 10: return "R10"; +  case 11: return "R11"; +  case 12: return "R12"; +  case 13: return "R13"; +  case 14: return "R14"; +  case 15: return "R15"; +  } +} + +// Calculates the number of array slots required for the unwind code. +static unsigned getNumUsedSlots(const UnwindCode &UnwindCode) { +  switch (UnwindCode.getUnwindOp()) { +  default: llvm_unreachable("Invalid unwind code"); +  case UOP_PushNonVol: +  case UOP_AllocSmall: +  case UOP_SetFPReg: +  case UOP_PushMachFrame: +    return 1; +  case UOP_SaveNonVol: +  case UOP_SaveXMM128: +    return 2; +  case UOP_SaveNonVolBig: +  case UOP_SaveXMM128Big: +    return 3; +  case UOP_AllocLarge: +    return (UnwindCode.getOpInfo() == 0) ? 2 : 3; +  } +} + +// Prints one unwind code. Because an unwind code can occupy up to 3 slots in +// the unwind codes array, this function requires that the correct number of +// slots is provided. +static void printUnwindCode(ArrayRef<UnwindCode> UCs) { +  assert(UCs.size() >= getNumUsedSlots(UCs[0])); +  outs() <<  format("    0x%02x: ", unsigned(UCs[0].u.CodeOffset)) +         << getUnwindCodeTypeName(UCs[0].getUnwindOp()); +  switch (UCs[0].getUnwindOp()) { +  case UOP_PushNonVol: +    outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()); +    break; +  case UOP_AllocLarge: +    if (UCs[0].getOpInfo() == 0) { +      outs() << " " << UCs[1].FrameOffset; +    } else { +      outs() << " " << UCs[1].FrameOffset +                       + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16); +    } +    break; +  case UOP_AllocSmall: +    outs() << " " << ((UCs[0].getOpInfo() + 1) * 8); +    break; +  case UOP_SetFPReg: +    outs() << " "; +    break; +  case UOP_SaveNonVol: +    outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) +           << format(" [0x%04x]", 8 * UCs[1].FrameOffset); +    break; +  case UOP_SaveNonVolBig: +    outs() << " " << getUnwindRegisterName(UCs[0].getOpInfo()) +           << format(" [0x%08x]", UCs[1].FrameOffset +                    + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); +    break; +  case UOP_SaveXMM128: +    outs() << " XMM" << static_cast<uint32_t>(UCs[0].getOpInfo()) +           << format(" [0x%04x]", 16 * UCs[1].FrameOffset); +    break; +  case UOP_SaveXMM128Big: +    outs() << " XMM" << UCs[0].getOpInfo() +           << format(" [0x%08x]", UCs[1].FrameOffset +                           + (static_cast<uint32_t>(UCs[2].FrameOffset) << 16)); +    break; +  case UOP_PushMachFrame: +    outs() << " " << (UCs[0].getOpInfo() ? "w/o" : "w") +           << " error code"; +    break; +  } +  outs() << "\n"; +} + +static void printAllUnwindCodes(ArrayRef<UnwindCode> UCs) { +  for (const UnwindCode *I = UCs.begin(), *E = UCs.end(); I < E; ) { +    unsigned UsedSlots = getNumUsedSlots(*I); +    if (UsedSlots > UCs.size()) { +      outs() << "Unwind data corrupted: Encountered unwind op " +             << getUnwindCodeTypeName((*I).getUnwindOp()) +             << " which requires " << UsedSlots +             << " slots, but only " << UCs.size() +             << " remaining in buffer"; +      return ; +    } +    printUnwindCode(ArrayRef<UnwindCode>(I, E)); +    I += UsedSlots; +  } +} + +// Given a symbol sym this functions returns the address and section of it. +static error_code resolveSectionAndAddress(const COFFObjectFile *Obj, +                                           const SymbolRef &Sym, +                                           const coff_section *&ResolvedSection, +                                           uint64_t &ResolvedAddr) { +  if (error_code ec = Sym.getAddress(ResolvedAddr)) return ec; +  section_iterator iter(Obj->begin_sections()); +  if (error_code ec = Sym.getSection(iter)) return ec; +  ResolvedSection = Obj->getCOFFSection(iter); +  return object_error::success; +} + +// Given a vector of relocations for a section and an offset into this section +// the function returns the symbol used for the relocation at the offset. +static error_code resolveSymbol(const std::vector<RelocationRef> &Rels, +                                uint64_t Offset, SymbolRef &Sym) { +  for (std::vector<RelocationRef>::const_iterator I = Rels.begin(), +                                                  E = Rels.end(); +                                                  I != E; ++I) { +    uint64_t Ofs; +    if (error_code ec = I->getOffset(Ofs)) return ec; +    if (Ofs == Offset) { +      if (error_code ec = I->getSymbol(Sym)) return ec; +      break; +    } +  } +  return object_error::success; +} + +// Given a vector of relocations for a section and an offset into this section +// the function resolves the symbol used for the relocation at the offset and +// returns the section content and the address inside the content pointed to +// by the symbol. +static error_code getSectionContents(const COFFObjectFile *Obj, +                                     const std::vector<RelocationRef> &Rels, +                                     uint64_t Offset, +                                     ArrayRef<uint8_t> &Contents, +                                     uint64_t &Addr) { +  SymbolRef Sym; +  if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec; +  const coff_section *Section; +  if (error_code ec = resolveSectionAndAddress(Obj, Sym, Section, Addr)) +    return ec; +  if (error_code ec = Obj->getSectionContents(Section, Contents)) return ec; +  return object_error::success; +} + +// Given a vector of relocations for a section and an offset into this section +// the function returns the name of the symbol used for the relocation at the +// offset. +static error_code resolveSymbolName(const std::vector<RelocationRef> &Rels, +                                    uint64_t Offset, StringRef &Name) { +  SymbolRef Sym; +  if (error_code ec = resolveSymbol(Rels, Offset, Sym)) return ec; +  if (error_code ec = Sym.getName(Name)) return ec; +  return object_error::success; +} + +static void printCOFFSymbolAddress(llvm::raw_ostream &Out, +                                   const std::vector<RelocationRef> &Rels, +                                   uint64_t Offset, uint32_t Disp) { +  StringRef Sym; +  if (error_code ec = resolveSymbolName(Rels, Offset, Sym)) { +    error(ec); +    return ; +  } +  Out << Sym; +  if (Disp > 0) +    Out << format(" + 0x%04x", Disp); +} + +void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) { +  const coff_file_header *Header; +  if (error(Obj->getHeader(Header))) return; + +  if (Header->Machine != COFF::IMAGE_FILE_MACHINE_AMD64) { +    errs() << "Unsupported image machine type " +              "(currently only AMD64 is supported).\n"; +    return; +  } + +  const coff_section *Pdata = 0; + +  error_code ec; +  for (section_iterator SI = Obj->begin_sections(), +                        SE = Obj->end_sections(); +                        SI != SE; SI.increment(ec)) { +    if (error(ec)) return; + +    StringRef Name; +    if (error(SI->getName(Name))) continue; + +    if (Name != ".pdata") continue; + +    Pdata = Obj->getCOFFSection(SI); +    std::vector<RelocationRef> Rels; +    for (relocation_iterator RI = SI->begin_relocations(), +                             RE = SI->end_relocations(); +                             RI != RE; RI.increment(ec)) { +      if (error(ec)) break; +      Rels.push_back(*RI); +    } + +    // Sort relocations by address. +    std::sort(Rels.begin(), Rels.end(), RelocAddressLess); + +    ArrayRef<uint8_t> Contents; +    if (error(Obj->getSectionContents(Pdata, Contents))) continue; +    if (Contents.empty()) continue; + +    ArrayRef<RuntimeFunction> RFs( +                  reinterpret_cast<const RuntimeFunction *>(Contents.data()), +                                  Contents.size() / sizeof(RuntimeFunction)); +    for (const RuntimeFunction *I = RFs.begin(), *E = RFs.end(); I < E; ++I) { +      const uint64_t SectionOffset = std::distance(RFs.begin(), I) +                                     * sizeof(RuntimeFunction); + +      outs() << "Function Table:\n"; + +      outs() << "  Start Address: "; +      printCOFFSymbolAddress(outs(), Rels, SectionOffset + +                             /*offsetof(RuntimeFunction, StartAddress)*/ 0, +                             I->StartAddress); +      outs() << "\n"; + +      outs() << "  End Address: "; +      printCOFFSymbolAddress(outs(), Rels, SectionOffset + +                             /*offsetof(RuntimeFunction, EndAddress)*/ 4, +                             I->EndAddress); +      outs() << "\n"; + +      outs() << "  Unwind Info Address: "; +      printCOFFSymbolAddress(outs(), Rels, SectionOffset + +                             /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, +                             I->UnwindInfoOffset); +      outs() << "\n"; + +      ArrayRef<uint8_t> XContents; +      uint64_t UnwindInfoOffset = 0; +      if (error(getSectionContents(Obj, Rels, SectionOffset + +                              /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8, +                                   XContents, UnwindInfoOffset))) continue; +      if (XContents.empty()) continue; + +      UnwindInfoOffset += I->UnwindInfoOffset; +      if (UnwindInfoOffset > XContents.size()) continue; + +      const Win64EH::UnwindInfo *UI = +                            reinterpret_cast<const Win64EH::UnwindInfo *> +                              (XContents.data() + UnwindInfoOffset); + +      // The casts to int are required in order to output the value as number. +      // Without the casts the value would be interpreted as char data (which +      // results in garbage output). +      outs() << "  Version: " << static_cast<int>(UI->getVersion()) << "\n"; +      outs() << "  Flags: " << static_cast<int>(UI->getFlags()); +      if (UI->getFlags()) { +          if (UI->getFlags() & UNW_ExceptionHandler) +            outs() << " UNW_ExceptionHandler"; +          if (UI->getFlags() & UNW_TerminateHandler) +            outs() << " UNW_TerminateHandler"; +          if (UI->getFlags() & UNW_ChainInfo) +            outs() << " UNW_ChainInfo"; +      } +      outs() << "\n"; +      outs() << "  Size of prolog: " +             << static_cast<int>(UI->PrologSize) << "\n"; +      outs() << "  Number of Codes: " +             << static_cast<int>(UI->NumCodes) << "\n"; +      // Maybe this should move to output of UOP_SetFPReg? +      if (UI->getFrameRegister()) { +        outs() << "  Frame register: " +                << getUnwindRegisterName(UI->getFrameRegister()) +                << "\n"; +        outs() << "  Frame offset: " +                << 16 * UI->getFrameOffset() +                << "\n"; +      } else { +        outs() << "  No frame pointer used\n"; +      } +      if (UI->getFlags() & (UNW_ExceptionHandler | UNW_TerminateHandler)) { +        // FIXME: Output exception handler data +      } else if (UI->getFlags() & UNW_ChainInfo) { +        // FIXME: Output chained unwind info +      } + +      if (UI->NumCodes) +        outs() << "  Unwind Codes:\n"; + +      printAllUnwindCodes(ArrayRef<UnwindCode>(&UI->UnwindCodes[0], +                          UI->NumCodes)); + +      outs() << "\n\n"; +      outs().flush(); +    } +  } +} diff --git a/tools/llvm-objdump/ELFDump.cpp b/tools/llvm-objdump/ELFDump.cpp new file mode 100644 index 000000000000..bd1523133f55 --- /dev/null +++ b/tools/llvm-objdump/ELFDump.cpp @@ -0,0 +1,100 @@ +//===-- ELFDump.cpp - ELF-specific dumper -----------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file implements the ELF-specific dumper for llvm-objdump. +/// +//===----------------------------------------------------------------------===// + +#include "llvm-objdump.h" +#include "llvm/Object/ELF.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; +using namespace llvm::object; + +template<class ELFT> +void printProgramHeaders( +    const ELFObjectFile<ELFT> *o) { +  typedef ELFObjectFile<ELFT> ELFO; +  outs() << "Program Header:\n"; +  for (typename ELFO::Elf_Phdr_Iter pi = o->begin_program_headers(), +                                    pe = o->end_program_headers(); +                                    pi != pe; ++pi) { +    switch (pi->p_type) { +    case ELF::PT_LOAD: +      outs() << "    LOAD "; +      break; +    case ELF::PT_GNU_STACK: +      outs() << "   STACK "; +      break; +    case ELF::PT_GNU_EH_FRAME: +      outs() << "EH_FRAME "; +      break; +    case ELF::PT_INTERP: +      outs() << "  INTERP "; +      break; +    case ELF::PT_DYNAMIC: +      outs() << " DYNAMIC "; +      break; +    case ELF::PT_PHDR: +      outs() << "    PHDR "; +      break; +    case ELF::PT_TLS: +      outs() << "    TLS "; +      break; +    default: +      outs() << " UNKNOWN "; +    } + +    const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " "; + +    outs() << "off    " +           << format(Fmt, (uint64_t)pi->p_offset) +           << "vaddr " +           << format(Fmt, (uint64_t)pi->p_vaddr) +           << "paddr " +           << format(Fmt, (uint64_t)pi->p_paddr) +           << format("align 2**%u\n", CountTrailingZeros_64(pi->p_align)) +           << "         filesz " +           << format(Fmt, (uint64_t)pi->p_filesz) +           << "memsz " +           << format(Fmt, (uint64_t)pi->p_memsz) +           << "flags " +           << ((pi->p_flags & ELF::PF_R) ? "r" : "-") +           << ((pi->p_flags & ELF::PF_W) ? "w" : "-") +           << ((pi->p_flags & ELF::PF_X) ? "x" : "-") +           << "\n"; +  } +  outs() << "\n"; +} + +void llvm::printELFFileHeader(const object::ObjectFile *Obj) { +  // Little-endian 32-bit +  if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj = +          dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj)) +    printProgramHeaders(ELFObj); + +  // Big-endian 32-bit +  if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj = +          dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj)) +    printProgramHeaders(ELFObj); + +  // Little-endian 64-bit +  if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj = +          dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj)) +    printProgramHeaders(ELFObj); + +  // Big-endian 64-bit +  if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj = +          dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj)) +    printProgramHeaders(ELFObj); +} diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 1feea421f23a..c324ff13a6b8 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -13,11 +13,9 @@  #include "llvm-objdump.h"  #include "MCFunction.h" -#include "llvm/Support/MachO.h" -#include "llvm/Object/MachO.h"  #include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/Triple.h"  #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Triple.h"  #include "llvm/DebugInfo/DIContext.h"  #include "llvm/MC/MCAsmInfo.h"  #include "llvm/MC/MCDisassembler.h" @@ -28,10 +26,12 @@  #include "llvm/MC/MCInstrInfo.h"  #include "llvm/MC/MCRegisterInfo.h"  #include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Object/MachO.h"  #include "llvm/Support/CommandLine.h"  #include "llvm/Support/Debug.h"  #include "llvm/Support/Format.h"  #include "llvm/Support/GraphWriter.h" +#include "llvm/Support/MachO.h"  #include "llvm/Support/MemoryBuffer.h"  #include "llvm/Support/TargetRegistry.h"  #include "llvm/Support/TargetSelect.h" @@ -309,16 +309,10 @@ void llvm::DisassembleInputMachO(StringRef Filename) {    raw_ostream &DebugOut = nulls();  #endif -  StringRef DebugAbbrevSection, DebugInfoSection, DebugArangesSection, -            DebugLineSection, DebugStrSection;    OwningPtr<DIContext> diContext; -  OwningPtr<MachOObjectFile> DSYMObj; -  MachOObject *DbgInfoObj = MachOObj; +  ObjectFile *DbgObj = MachOOF.get();    // Try to find debug info and set up the DIContext for it.    if (UseDbg) { -    ArrayRef<SectionRef> DebugSections = Sections; -    std::vector<SectionRef> DSYMSections; -      // A separate DSym file path was specified, parse it as a macho file,      // get the sections and supply it to the section name parsing machinery.      if (!DSYMFile.empty()) { @@ -327,42 +321,11 @@ void llvm::DisassembleInputMachO(StringRef Filename) {          errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';          return;        } -      DSYMObj.reset(static_cast<MachOObjectFile*>( -            ObjectFile::createMachOObjectFile(Buf.take()))); -      const macho::Header &Header = DSYMObj->getObject()->getHeader(); - -      std::vector<SymbolRef> Symbols; -      SmallVector<uint64_t, 8> FoundFns; -      getSectionsAndSymbols(Header, DSYMObj.get(), 0, DSYMSections, Symbols, -                            FoundFns); -      DebugSections = DSYMSections; -      DbgInfoObj = DSYMObj.get()->getObject(); -    } - -    // Find the named debug info sections. -    for (unsigned SectIdx = 0; SectIdx != DebugSections.size(); SectIdx++) { -      StringRef SectName; -      if (!DebugSections[SectIdx].getName(SectName)) { -        if (SectName.equals("__DWARF,__debug_abbrev")) -          DebugSections[SectIdx].getContents(DebugAbbrevSection); -        else if (SectName.equals("__DWARF,__debug_info")) -          DebugSections[SectIdx].getContents(DebugInfoSection); -        else if (SectName.equals("__DWARF,__debug_aranges")) -          DebugSections[SectIdx].getContents(DebugArangesSection); -        else if (SectName.equals("__DWARF,__debug_line")) -          DebugSections[SectIdx].getContents(DebugLineSection); -        else if (SectName.equals("__DWARF,__debug_str")) -          DebugSections[SectIdx].getContents(DebugStrSection); -      } +      DbgObj = ObjectFile::createMachOObjectFile(Buf.take());      } -    // Setup the DIContext. -    diContext.reset(DIContext::getDWARFContext(DbgInfoObj->isLittleEndian(), -                                               DebugInfoSection, -                                               DebugAbbrevSection, -                                               DebugArangesSection, -                                               DebugLineSection, -                                               DebugStrSection)); +    // Setup the DIContext +    diContext.reset(DIContext::getDWARFContext(DbgObj));    }    FunctionMapTy FunctionMap; @@ -371,9 +334,15 @@ void llvm::DisassembleInputMachO(StringRef Filename) {    for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {      StringRef SectName;      if (Sections[SectIdx].getName(SectName) || -        SectName.compare("__TEXT,__text")) +        SectName != "__text")        continue; // Skip non-text sections +    StringRef SegmentName; +    DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl(); +    if (MachOOF->getSectionFinalSegmentName(DR, SegmentName) || +        SegmentName != "__TEXT") +      continue; +      // Insert the functions from the function starts segment into our map.      uint64_t VMAddr;      Sections[SectIdx].getAddress(VMAddr); diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 13ea4e32958a..7832cf0dff4c 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -11,17 +11,17 @@  // dumps out a plethora of information about an object file depending on the  // flags.  // +// The flags and output of this program should be near identical to those of +// binutils objdump. +//  //===----------------------------------------------------------------------===//  #include "llvm-objdump.h"  #include "MCFunction.h" -#include "llvm/Object/Archive.h" -#include "llvm/Object/COFF.h" -#include "llvm/Object/ObjectFile.h"  #include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/STLExtras.h"  #include "llvm/ADT/StringExtras.h"  #include "llvm/ADT/Triple.h" -#include "llvm/ADT/STLExtras.h"  #include "llvm/MC/MCAsmInfo.h"  #include "llvm/MC/MCDisassembler.h"  #include "llvm/MC/MCInst.h" @@ -29,6 +29,10 @@  #include "llvm/MC/MCInstrInfo.h"  #include "llvm/MC/MCRegisterInfo.h"  #include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Object/Archive.h" +#include "llvm/Object/COFF.h" +#include "llvm/Object/MachO.h" +#include "llvm/Object/ObjectFile.h"  #include "llvm/Support/Casting.h"  #include "llvm/Support/CommandLine.h"  #include "llvm/Support/Debug.h" @@ -72,9 +76,9 @@ static cl::opt<bool>  SymbolTable("t", cl::desc("Display the symbol table"));  static cl::opt<bool> -MachO("macho", cl::desc("Use MachO specific object file parser")); +MachOOpt("macho", cl::desc("Use MachO specific object file parser"));  static cl::alias -MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO)); +MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));  cl::opt<std::string>  llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " @@ -100,9 +104,28 @@ MAttrs("mattr",    cl::desc("Target specific attributes"),    cl::value_desc("a1,+a2,-a3,...")); +static cl::opt<bool> +NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, " +                                           "do not print the instruction bytes.")); + +static cl::opt<bool> +UnwindInfo("unwind-info", cl::desc("Display unwind information")); + +static cl::alias +UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), +                cl::aliasopt(UnwindInfo)); + +static cl::opt<bool> +PrivateHeaders("private-headers", +               cl::desc("Display format specific file headers")); + +static cl::alias +PrivateHeadersShort("p", cl::desc("Alias for --private-headers"), +                    cl::aliasopt(PrivateHeaders)); +  static StringRef ToolName; -static bool error(error_code ec) { +bool llvm::error(error_code ec) {    if (!ec) return false;    outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; @@ -161,7 +184,7 @@ void llvm::DumpBytes(StringRef bytes) {    outs() << output;  } -static bool RelocAddressLess(RelocationRef a, RelocationRef b) { +bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {    uint64_t a_addr, b_addr;    if (error(a.getAddress(a_addr))) return false;    if (error(b.getAddress(b_addr))) return false; @@ -205,6 +228,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {        if (!error(i->containsSymbol(*si, contains)) && contains) {          uint64_t Address;          if (error(si->getAddress(Address))) break; +        if (Address == UnknownAddressOrSize) continue;          Address -= SectionAddr;          StringRef Name; @@ -230,9 +254,18 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {      // Sort relocations by address.      std::sort(Rels.begin(), Rels.end(), RelocAddressLess); +    StringRef SegmentName = ""; +    if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { +      DataRefImpl DR = i->getRawDataRefImpl(); +      if (error(MachO->getSectionFinalSegmentName(DR, SegmentName))) +        break; +    }      StringRef name;      if (error(i->getName(name))) break; -    outs() << "Disassembly of section " << name << ':'; +    outs() << "Disassembly of section "; +    if (!SegmentName.empty()) +      outs() << SegmentName << ","; +    outs() << name << ':';      // If the section has no symbols just insert a dummy one and disassemble      // the whole section. @@ -321,8 +354,11 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {          if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,                                     DebugOut, nulls())) { -          outs() << format("%8" PRIx64 ":\t", SectionAddr + Index); -          DumpBytes(StringRef(Bytes.data() + Index, Size)); +          outs() << format("%8" PRIx64 ":", SectionAddr + Index); +          if (!NoShowRawInsn) { +            outs() << "\t"; +            DumpBytes(StringRef(Bytes.data() + Index, Size)); +          }            IP->printInst(&Inst, outs(), "");            outs() << "\n";          } else { @@ -409,7 +445,7 @@ static void PrintSectionHeaders(const ObjectFile *o) {      if (error(si->isBSS(BSS))) return;      std::string Type = (std::string(Text ? "TEXT " : "") +                          (Data ? "DATA " : "") + (BSS ? "BSS" : "")); -    outs() << format("%3d %-13s %09" PRIx64 " %017" PRIx64 " %s\n", +    outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n",                       i, Name.str().c_str(), Size, Address, Type.c_str());      ++i;    } @@ -446,7 +482,7 @@ static void PrintSectionContents(const ObjectFile *o) {        // Print ascii.        outs() << "  ";        for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { -        if (std::isprint(Contents[addr + i] & 0xFF)) +        if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))            outs() << Contents[addr + i];          else            outs() << "."; @@ -539,7 +575,10 @@ static void PrintSymbolTable(const ObjectFile *o) {        else if (Type == SymbolRef::ST_Function)          FileFunc = 'F'; -      outs() << format("%08" PRIx64, Address) << " " +      const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 : +                                                     "%08" PRIx64; + +      outs() << format(Fmt, Address) << " "               << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '               << (Weak ? 'w' : ' ') // Weak?               << ' ' // Constructor. Not supported yet. @@ -553,6 +592,13 @@ static void PrintSymbolTable(const ObjectFile *o) {        else if (Section == o->end_sections())          outs() << "*UND*";        else { +        if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(o)) { +          StringRef SegmentName; +          DataRefImpl DR = Section->getRawDataRefImpl(); +          if (error(MachO->getSectionFinalSegmentName(DR, SegmentName))) +            SegmentName = ""; +          outs() << SegmentName << ","; +        }          StringRef SectionName;          if (error(Section->getName(SectionName)))            SectionName = ""; @@ -566,6 +612,19 @@ static void PrintSymbolTable(const ObjectFile *o) {    }  } +static void PrintUnwindInfo(const ObjectFile *o) { +  outs() << "Unwind info:\n\n"; + +  if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) { +    printCOFFUnwindInfo(coff); +  } else { +    // TODO: Extract DWARF dump tool to objdump. +    errs() << "This operation is only currently supported " +              "for COFF object files.\n"; +    return; +  } +} +  static void DumpObject(const ObjectFile *o) {    outs() << '\n';    outs() << o->getFileName() @@ -581,6 +640,10 @@ static void DumpObject(const ObjectFile *o) {      PrintSectionContents(o);    if (SymbolTable)      PrintSymbolTable(o); +  if (UnwindInfo) +    PrintUnwindInfo(o); +  if (PrivateHeaders && o->isELF()) +    printELFFileHeader(o);  }  /// @brief Dump each object file in \a a; @@ -611,7 +674,7 @@ static void DumpInput(StringRef file) {      return;    } -  if (MachO && Disassemble) { +  if (MachOOpt && Disassemble) {      DisassembleInputMachO(file);      return;    } @@ -659,7 +722,9 @@ int main(int argc, char **argv) {        && !Relocations        && !SectionHeaders        && !SectionContents -      && !SymbolTable) { +      && !SymbolTable +      && !UnwindInfo +      && !PrivateHeaders) {      cl::PrintHelpMessage();      return 2;    } diff --git a/tools/llvm-objdump/llvm-objdump.h b/tools/llvm-objdump/llvm-objdump.h index aa71b77c8abf..ca7bced635f8 100644 --- a/tools/llvm-objdump/llvm-objdump.h +++ b/tools/llvm-objdump/llvm-objdump.h @@ -17,12 +17,23 @@  namespace llvm { +namespace object { +  class COFFObjectFile; +  class ObjectFile; +  class RelocationRef; +} +class error_code; +  extern cl::opt<std::string> TripleName;  extern cl::opt<std::string> ArchName;  // Various helper functions. +bool error(error_code ec); +bool RelocAddressLess(object::RelocationRef a, object::RelocationRef b);  void DumpBytes(StringRef bytes);  void DisassembleInputMachO(StringRef Filename); +void printCOFFUnwindInfo(const object::COFFObjectFile* o); +void printELFFileHeader(const object::ObjectFile *o);  class StringRefMemoryObject : public MemoryObject {    virtual void anchor(); | 
