Пример #1
0
static void DumpInput(const StringRef &Filename) {
  OwningPtr<MemoryBuffer> Buff;

  if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
    errs() << Filename << ": " << ec.message() << "\n";
    return;
  }

  OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));

  StringRef DebugInfoSection;
  StringRef DebugAbbrevSection;
  StringRef DebugLineSection;
  StringRef DebugArangesSection;
  StringRef DebugStringSection;

  error_code ec;
  for (ObjectFile::section_iterator i = Obj->begin_sections(),
                                    e = Obj->end_sections();
                                    i != e; i.increment(ec)) {
    StringRef name;
    i->getName(name);
    StringRef data;
    i->getContents(data);

    if (name.startswith("__DWARF,"))
      name = name.substr(8); // Skip "__DWARF," prefix.
    name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
    if (name == "debug_info")
      DebugInfoSection = data;
    else if (name == "debug_abbrev")
      DebugAbbrevSection = data;
    else if (name == "debug_line")
      DebugLineSection = data;
    else if (name == "debug_aranges")
      DebugArangesSection = data;
    else if (name == "debug_str")
      DebugStringSection = data;
  }

  OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true,
                                                        DebugInfoSection,
                                                        DebugAbbrevSection,
                                                        DebugArangesSection,
                                                        DebugLineSection,
                                                        DebugStringSection));
  if (Address == -1ULL) {
    outs() << Filename
           << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
    // Dump the complete DWARF structure.
    dictx->dump(outs());
  } else {
    // Print line info for the specified address.
    DILineInfo dli = dictx->getLineInfoForAddress(Address);
    outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':'
           << dli.getLine() << ':' << dli.getColumn() << '\n';
  }
}
Пример #2
0
static void DisassembleInput(const StringRef &Filename) {
    OwningPtr<MemoryBuffer> Buff;

    if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
        errs() << ToolName << ": " << Filename << ": " << ec.message() << "\n";
        return;
    }

    OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));

    const Target *TheTarget = GetTarget(Obj.get());
    if (!TheTarget) {
        // GetTarget prints out stuff.
        return;
    }

    outs() << '\n';
    outs() << Filename
           << ":\tfile format " << Obj->getFileFormatName() << "\n\n\n";

    for (ObjectFile::section_iterator i = Obj->begin_sections(),
            e = Obj->end_sections();
            i != e; ++i) {
        if (!i->isText())
            continue;
        outs() << "Disassembly of section " << i->getName() << ":\n\n";

        // Set up disassembler.
        OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createAsmInfo(TripleName));

        if (!AsmInfo) {
            errs() << "error: no assembly info for target " << TripleName << "\n";
            return;
        }

        OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler());
        if (!DisAsm) {
            errs() << "error: no disassembler for target " << TripleName << "\n";
            return;
        }

        int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
        OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
                                        AsmPrinterVariant, *AsmInfo));
        if (!IP) {
            errs() << "error: no instruction printer for target " << TripleName << '\n';
            return;
        }

        StringRef Bytes = i->getContents();
        StringRefMemoryObject memoryObject(Bytes);
        uint64_t Size;
        uint64_t Index;

        for (Index = 0; Index < Bytes.size(); Index += Size) {
            MCInst Inst;

#     ifndef NDEBUG
            raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
#     else
            raw_ostream &DebugOut = nulls();
#     endif

            if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) {
                outs() << format("%8x:\t", i->getAddress() + Index);
                DumpBytes(StringRef(Bytes.data() + Index, Size));
                IP->printInst(&Inst, outs());
                outs() << "\n";
            } else {
                errs() << ToolName << ": warning: invalid instruction encoding\n";
                if (Size == 0)
                    Size = 1; // skip illegible bytes
            }
        }
    }
}