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 | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 33 | raw_ostream &DebugOut) { |
| 34 | std::set<uint64_t> Splits; |
| 35 | Splits.insert(Start); |
| 36 | std::vector<MCDecodedInst> Instructions; |
| 37 | uint64_t Size; |
| 38 | |
| 39 | // Disassemble code and gather basic block split points. |
| 40 | for (uint64_t Index = Start; Index < End; Index += Size) { |
| 41 | MCInst Inst; |
| 42 | |
| 43 | if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut)) { |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame^] | 44 | if (Ana->isBranch(Inst)) { |
| 45 | uint64_t targ = Ana->evaluateBranch(Inst, Index, Size); |
| 46 | // FIXME: Distinguish relocations from nop jumps. |
| 47 | if (targ != -1ULL && (targ == Index+Size || targ >= End)) { |
| 48 | Instructions.push_back(MCDecodedInst(Index, Size, Inst)); |
| 49 | continue; // Skip branches that leave the function. |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 50 | } |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame^] | 51 | if (targ != -1ULL) |
| 52 | Splits.insert(targ); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 53 | Splits.insert(Index+Size); |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame^] | 54 | } else if (Ana->isReturn(Inst)) { |
Benjamin Kramer | 853b0fd | 2011-07-25 23:04:36 +0000 | [diff] [blame] | 55 | Splits.insert(Index+Size); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | Instructions.push_back(MCDecodedInst(Index, Size, Inst)); |
| 59 | } else { |
| 60 | errs() << "warning: invalid instruction encoding\n"; |
| 61 | if (Size == 0) |
| 62 | Size = 1; // skip illegible bytes |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | MCFunction f(Name); |
| 68 | |
| 69 | // Create basic blocks. |
| 70 | unsigned ii = 0, ie = Instructions.size(); |
| 71 | for (std::set<uint64_t>::iterator spi = Splits.begin(), |
| 72 | spe = Splits.end(); spi != spe; ++spi) { |
| 73 | MCBasicBlock BB; |
| 74 | uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi); |
| 75 | // Add instructions to the BB. |
| 76 | for (; ii != ie; ++ii) { |
| 77 | if (Instructions[ii].Address < *spi || |
| 78 | Instructions[ii].Address >= BlockEnd) |
| 79 | break; |
| 80 | BB.addInst(Instructions[ii]); |
| 81 | } |
| 82 | f.addBlock(*spi, BB); |
| 83 | } |
| 84 | |
| 85 | // Calculate successors of each block. |
| 86 | for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) { |
| 87 | MCBasicBlock &BB = i->second; |
| 88 | if (BB.getInsts().empty()) continue; |
| 89 | const MCDecodedInst &Inst = BB.getInsts().back(); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 90 | |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame^] | 91 | if (Ana->isBranch(Inst.Inst)) { |
| 92 | uint64_t targ = Ana->evaluateBranch(Inst.Inst, Inst.Address, Inst.Size); |
| 93 | if (targ == -1ULL) { |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 94 | // Indirect branch. Bail and add all blocks of the function as a |
| 95 | // successor. |
| 96 | for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) |
| 97 | BB.addSucc(&i->second); |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame^] | 98 | } else if (targ != Inst.Address+Inst.Size) |
| 99 | BB.addSucc(&f.getBlockAtAddress(targ)); |
| 100 | // Conditional branches can also fall through to the next block. |
| 101 | if (Ana->isConditionalBranch(Inst.Inst) && llvm::next(i) != e) |
| 102 | BB.addSucc(&llvm::next(i)->second); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 103 | } else { |
| 104 | // No branch. Fall through to the next block. |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame^] | 105 | if (!Ana->isReturn(Inst.Inst) && llvm::next(i) != e) |
Francois Pichet | c3d48ce | 2011-07-20 21:35:29 +0000 | [diff] [blame] | 106 | BB.addSucc(&llvm::next(i)->second); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | return f; |
| 111 | } |