Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 1 | //===-- MCFunction.cpp ----------------------------------------------------===// |
| 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 the algorithm to break down a region of machine code |
| 11 | // into basic blocks and try to reconstruct a CFG from it. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "MCFunction.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/MC/MCDisassembler.h" |
| 18 | #include "llvm/MC/MCInst.h" |
| 19 | #include "llvm/MC/MCInstPrinter.h" |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInstrAnalysis.h" |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInstrDesc.h" |
| 22 | #include "llvm/MC/MCInstrInfo.h" |
| 23 | #include "llvm/Support/MemoryObject.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Support/system_error.h" |
| 26 | #include <set> |
| 27 | using namespace llvm; |
| 28 | |
| 29 | MCFunction |
| 30 | MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm, |
| 31 | const MemoryObject &Region, uint64_t Start, |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 32 | uint64_t End, const MCInstrAnalysis *Ana, |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 33 | raw_ostream &DebugOut, |
| 34 | SmallVectorImpl<uint64_t> &Calls) { |
| 35 | std::vector<MCDecodedInst> Instructions; |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 36 | std::set<uint64_t> Splits; |
| 37 | Splits.insert(Start); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 38 | uint64_t Size; |
| 39 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 40 | MCFunction f(Name); |
| 41 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 42 | { |
| 43 | DenseSet<uint64_t> VisitedInsts; |
| 44 | SmallVector<uint64_t, 16> WorkList; |
| 45 | WorkList.push_back(Start); |
| 46 | // Disassemble code and gather basic block split points. |
| 47 | while (!WorkList.empty()) { |
| 48 | uint64_t Index = WorkList.pop_back_val(); |
| 49 | if (VisitedInsts.find(Index) != VisitedInsts.end()) |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 50 | continue; // Already visited this location. |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 51 | |
| 52 | for (;Index < End; Index += Size) { |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 53 | VisitedInsts.insert(Index); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 54 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 55 | MCInst Inst; |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 56 | if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut, nulls())){ |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 57 | Instructions.push_back(MCDecodedInst(Index, Size, Inst)); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 58 | if (Ana->isBranch(Inst)) { |
| 59 | uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 60 | if (targ != -1ULL && targ == Index+Size) |
| 61 | continue; // Skip nop jumps. |
| 62 | |
| 63 | // If we could determine the branch target, make a note to start a |
| 64 | // new basic block there and add the target to the worklist. |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 65 | if (targ != -1ULL) { |
| 66 | Splits.insert(targ); |
| 67 | WorkList.push_back(targ); |
| 68 | WorkList.push_back(Index+Size); |
| 69 | } |
| 70 | Splits.insert(Index+Size); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 71 | break; |
| 72 | } else if (Ana->isReturn(Inst)) { |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 73 | // Return instruction. This basic block ends here. |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 74 | Splits.insert(Index+Size); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 75 | break; |
| 76 | } else if (Ana->isCall(Inst)) { |
| 77 | uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 78 | // Add the call to the call list if the destination is known. |
| 79 | if (targ != -1ULL && targ != Index+Size) |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 80 | Calls.push_back(targ); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 81 | } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 82 | } else { |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 83 | errs().write_hex(Index) << ": warning: invalid instruction encoding\n"; |
| 84 | if (Size == 0) |
| 85 | Size = 1; // skip illegible bytes |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 91 | // Make sure the instruction list is sorted. |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 92 | std::sort(Instructions.begin(), Instructions.end()); |
| 93 | |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 94 | // Create basic blocks. |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 95 | unsigned ii = 0, ie = Instructions.size(); |
| 96 | for (std::set<uint64_t>::iterator spi = Splits.begin(), |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 97 | spe = llvm::prior(Splits.end()); spi != spe; ++spi) { |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 98 | MCBasicBlock BB; |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 99 | uint64_t BlockEnd = *llvm::next(spi); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 100 | // Add instructions to the BB. |
| 101 | for (; ii != ie; ++ii) { |
| 102 | if (Instructions[ii].Address < *spi || |
| 103 | Instructions[ii].Address >= BlockEnd) |
| 104 | break; |
| 105 | BB.addInst(Instructions[ii]); |
| 106 | } |
| 107 | f.addBlock(*spi, BB); |
| 108 | } |
| 109 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 110 | std::sort(f.Blocks.begin(), f.Blocks.end()); |
| 111 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 112 | // Calculate successors of each block. |
| 113 | for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) { |
Benjamin Kramer | a894c8e | 2011-09-20 17:53:01 +0000 | [diff] [blame^] | 114 | MCBasicBlock &BB = const_cast<MCBasicBlock&>(i->second); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 115 | if (BB.getInsts().empty()) continue; |
| 116 | const MCDecodedInst &Inst = BB.getInsts().back(); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 117 | |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 118 | if (Ana->isBranch(Inst.Inst)) { |
| 119 | uint64_t targ = Ana->evaluateBranch(Inst.Inst, Inst.Address, Inst.Size); |
| 120 | if (targ == -1ULL) { |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 121 | // Indirect branch. Bail and add all blocks of the function as a |
| 122 | // successor. |
| 123 | for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 124 | BB.addSucc(i->first); |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 125 | } else if (targ != Inst.Address+Inst.Size) |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 126 | BB.addSucc(targ); |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 127 | // Conditional branches can also fall through to the next block. |
| 128 | if (Ana->isConditionalBranch(Inst.Inst) && llvm::next(i) != e) |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 129 | BB.addSucc(llvm::next(i)->first); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 130 | } else { |
| 131 | // No branch. Fall through to the next block. |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 132 | if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e) |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 133 | BB.addSucc(llvm::next(i)->first); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | return f; |
| 138 | } |