From 0b8b771e9f2f251460a6f200c45efe9d55640d60 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 19 Sep 2011 17:56:04 +0000 Subject: Add a MachO-specific "mode" to llvm-objdump, that, if enabled, gathers additional information that are only available on MachO. - It can take FunctionStarts from a binary to find entry points more accurately. - Symbol offsets in executables are correct now. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140028 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/llvm-objdump/MCFunction.cpp | 91 ++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 30 deletions(-) (limited to 'tools/llvm-objdump/MCFunction.cpp') diff --git a/tools/llvm-objdump/MCFunction.cpp b/tools/llvm-objdump/MCFunction.cpp index 5f1649694d..03390c2b58 100644 --- a/tools/llvm-objdump/MCFunction.cpp +++ b/tools/llvm-objdump/MCFunction.cpp @@ -30,48 +30,77 @@ MCFunction MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, const MemoryObject &Region, uint64_t Start, uint64_t End, const MCInstrAnalysis *Ana, - raw_ostream &DebugOut) { + raw_ostream &DebugOut, + SmallVectorImpl &Calls) { + std::vector Instructions; std::set Splits; Splits.insert(Start); - std::vector Instructions; uint64_t Size; + MCFunction f(Name); + + { + DenseSet VisitedInsts; + SmallVector WorkList; + WorkList.push_back(Start); // Disassemble code and gather basic block split points. - for (uint64_t Index = Start; Index < End; Index += Size) { - MCInst Inst; + while (!WorkList.empty()) { + uint64_t Index = WorkList.pop_back_val(); + if (VisitedInsts.find(Index) != VisitedInsts.end()) + continue; - if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut, nulls())) { - if (Ana->isBranch(Inst)) { - uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); - // FIXME: Distinguish relocations from nop jumps. - if (targ != -1ULL && (targ == Index+Size || targ >= End)) { + for (;Index < End; Index += Size) { + MCInst Inst; + + if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut, nulls())){ + if (Ana->isBranch(Inst)) { + uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); + if (targ != -1ULL && targ == Index+Size) { + Instructions.push_back(MCDecodedInst(Index, Size, Inst)); + VisitedInsts.insert(Index); + continue; + } + if (targ != -1ULL) { + Splits.insert(targ); + WorkList.push_back(targ); + WorkList.push_back(Index+Size); + } + Splits.insert(Index+Size); + Instructions.push_back(MCDecodedInst(Index, Size, Inst)); + VisitedInsts.insert(Index); + break; + } else if (Ana->isReturn(Inst)) { + Splits.insert(Index+Size); Instructions.push_back(MCDecodedInst(Index, Size, Inst)); - continue; // Skip branches that leave the function. + VisitedInsts.insert(Index); + break; + } else if (Ana->isCall(Inst)) { + uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); + if (targ != -1ULL && targ != Index+Size) { + Calls.push_back(targ); + } } - if (targ != -1ULL) - Splits.insert(targ); - Splits.insert(Index+Size); - } else if (Ana->isReturn(Inst)) { - Splits.insert(Index+Size); - } - Instructions.push_back(MCDecodedInst(Index, Size, Inst)); - } else { - errs() << "warning: invalid instruction encoding\n"; - if (Size == 0) - Size = 1; // skip illegible bytes + Instructions.push_back(MCDecodedInst(Index, Size, Inst)); + VisitedInsts.insert(Index); + } else { + VisitedInsts.insert(Index); + errs().write_hex(Index) << ": warning: invalid instruction encoding\n"; + if (Size == 0) + Size = 1; // skip illegible bytes + } } - + } } - MCFunction f(Name); + std::sort(Instructions.begin(), Instructions.end()); - // Create basic blocks. + // Create basic blocks. unsigned ii = 0, ie = Instructions.size(); for (std::set::iterator spi = Splits.begin(), - spe = Splits.end(); spi != spe; ++spi) { + spe = llvm::prior(Splits.end()); spi != spe; ++spi) { MCBasicBlock BB; - uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi); + uint64_t BlockEnd = *llvm::next(spi); // Add instructions to the BB. for (; ii != ie; ++ii) { if (Instructions[ii].Address < *spi || @@ -82,6 +111,8 @@ MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, f.addBlock(*spi, BB); } + std::sort(f.Blocks.begin(), f.Blocks.end()); + // Calculate successors of each block. for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) { MCBasicBlock &BB = i->second; @@ -94,16 +125,16 @@ MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, // Indirect branch. Bail and add all blocks of the function as a // successor. for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) - BB.addSucc(&i->second); + BB.addSucc(i->first); } else if (targ != Inst.Address+Inst.Size) - BB.addSucc(&f.getBlockAtAddress(targ)); + BB.addSucc(targ); // Conditional branches can also fall through to the next block. if (Ana->isConditionalBranch(Inst.Inst) && llvm::next(i) != e) - BB.addSucc(&llvm::next(i)->second); + BB.addSucc(llvm::next(i)->first); } else { // No branch. Fall through to the next block. if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e) - BB.addSucc(&llvm::next(i)->second); + BB.addSucc(llvm::next(i)->first); } } -- cgit v1.2.3