Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCObjectDisassembler.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 | #include "llvm/MC/MCObjectDisassembler.h" |
| 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/ADT/SetVector.h" |
| 13 | #include "llvm/ADT/StringExtras.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | #include "llvm/MC/MCAtom.h" |
| 17 | #include "llvm/MC/MCDisassembler.h" |
| 18 | #include "llvm/MC/MCFunction.h" |
| 19 | #include "llvm/MC/MCInstrAnalysis.h" |
| 20 | #include "llvm/MC/MCModule.h" |
| 21 | #include "llvm/Object/ObjectFile.h" |
| 22 | #include "llvm/Support/MemoryObject.h" |
| 23 | #include "llvm/Support/StringRefMemoryObject.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <map> |
| 26 | #include <set> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace object; |
| 30 | |
| 31 | MCObjectDisassembler::MCObjectDisassembler(const ObjectFile &Obj, |
| 32 | const MCDisassembler &Dis, |
| 33 | const MCInstrAnalysis &MIA) |
| 34 | : Obj(Obj), Dis(Dis), MIA(MIA) {} |
| 35 | |
Ahmed Bougacha | 0a30ccc | 2013-08-21 07:28:29 +0000 | [diff] [blame] | 36 | uint64_t MCObjectDisassembler::getEntrypoint() { |
| 37 | error_code ec; |
| 38 | for (symbol_iterator SI = Obj.begin_symbols(), SE = Obj.end_symbols(); |
| 39 | SI != SE; SI.increment(ec)) { |
| 40 | if (ec) |
| 41 | break; |
| 42 | StringRef Name; |
| 43 | SI->getName(Name); |
| 44 | if (Name == "main" || Name == "_main") { |
| 45 | uint64_t Entrypoint; |
| 46 | SI->getAddress(Entrypoint); |
Ahmed Bougacha | 484a6eb | 2013-08-21 07:28:37 +0000 | [diff] [blame] | 47 | return getEffectiveLoadAddr(Entrypoint); |
Ahmed Bougacha | 0a30ccc | 2013-08-21 07:28:29 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | ArrayRef<uint64_t> MCObjectDisassembler::getStaticInitFunctions() { |
| 54 | return ArrayRef<uint64_t>(); |
| 55 | } |
| 56 | |
| 57 | ArrayRef<uint64_t> MCObjectDisassembler::getStaticExitFunctions() { |
| 58 | return ArrayRef<uint64_t>(); |
| 59 | } |
| 60 | |
Ahmed Bougacha | 484a6eb | 2013-08-21 07:28:37 +0000 | [diff] [blame] | 61 | uint64_t MCObjectDisassembler::getEffectiveLoadAddr(uint64_t Addr) { |
| 62 | return Addr; |
| 63 | } |
| 64 | |
| 65 | uint64_t MCObjectDisassembler::getOriginalLoadAddr(uint64_t Addr) { |
| 66 | return Addr; |
| 67 | } |
| 68 | |
Ahmed Bougacha | 0a30ccc | 2013-08-21 07:28:29 +0000 | [diff] [blame] | 69 | MCModule *MCObjectDisassembler::buildEmptyModule() { |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 70 | MCModule *Module = new MCModule; |
Ahmed Bougacha | 0a30ccc | 2013-08-21 07:28:29 +0000 | [diff] [blame] | 71 | Module->Entrypoint = getEntrypoint(); |
| 72 | return Module; |
| 73 | } |
| 74 | |
| 75 | MCModule *MCObjectDisassembler::buildModule(bool withCFG) { |
| 76 | MCModule *Module = buildEmptyModule(); |
| 77 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 78 | buildSectionAtoms(Module); |
| 79 | if (withCFG) |
| 80 | buildCFG(Module); |
| 81 | return Module; |
| 82 | } |
| 83 | |
| 84 | void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) { |
| 85 | error_code ec; |
| 86 | for (section_iterator SI = Obj.begin_sections(), |
| 87 | SE = Obj.end_sections(); |
| 88 | SI != SE; |
| 89 | SI.increment(ec)) { |
| 90 | if (ec) break; |
| 91 | |
| 92 | bool isText; SI->isText(isText); |
| 93 | bool isData; SI->isData(isData); |
| 94 | if (!isData && !isText) |
| 95 | continue; |
| 96 | |
| 97 | uint64_t StartAddr; SI->getAddress(StartAddr); |
| 98 | uint64_t SecSize; SI->getSize(SecSize); |
| 99 | if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize) |
| 100 | continue; |
Ahmed Bougacha | 484a6eb | 2013-08-21 07:28:37 +0000 | [diff] [blame] | 101 | StartAddr = getEffectiveLoadAddr(StartAddr); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 102 | |
| 103 | StringRef Contents; SI->getContents(Contents); |
Ahmed Bougacha | 0a30ccc | 2013-08-21 07:28:29 +0000 | [diff] [blame] | 104 | StringRefMemoryObject memoryObject(Contents, StartAddr); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 105 | |
| 106 | // We don't care about things like non-file-backed sections yet. |
| 107 | if (Contents.size() != SecSize || !SecSize) |
| 108 | continue; |
| 109 | uint64_t EndAddr = StartAddr + SecSize - 1; |
| 110 | |
| 111 | StringRef SecName; SI->getName(SecName); |
| 112 | |
| 113 | if (isText) { |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 114 | MCTextAtom *Text = 0; |
| 115 | MCDataAtom *InvalidData = 0; |
| 116 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 117 | uint64_t InstSize; |
| 118 | for (uint64_t Index = 0; Index < SecSize; Index += InstSize) { |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 119 | const uint64_t CurAddr = StartAddr + Index; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 120 | MCInst Inst; |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 121 | if (Dis.getInstruction(Inst, InstSize, memoryObject, CurAddr, nulls(), |
| 122 | nulls())) { |
| 123 | if (!Text) { |
| 124 | Text = Module->createTextAtom(CurAddr, CurAddr); |
| 125 | Text->setName(SecName); |
| 126 | } |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 127 | Text->addInst(Inst, InstSize); |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 128 | InvalidData = 0; |
| 129 | } else { |
| 130 | if (!InvalidData) { |
| 131 | Text = 0; |
| 132 | InvalidData = Module->createDataAtom(CurAddr, EndAddr); |
| 133 | } |
| 134 | InvalidData->addData(Contents[Index]); |
| 135 | } |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 136 | } |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 137 | } else { |
| 138 | MCDataAtom *Data = Module->createDataAtom(StartAddr, EndAddr); |
| 139 | Data->setName(SecName); |
| 140 | for (uint64_t Index = 0; Index < SecSize; ++Index) |
| 141 | Data->addData(Contents[Index]); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | namespace { |
| 147 | struct BBInfo; |
| 148 | typedef std::set<BBInfo*> BBInfoSetTy; |
| 149 | |
| 150 | struct BBInfo { |
| 151 | MCTextAtom *Atom; |
| 152 | MCBasicBlock *BB; |
| 153 | BBInfoSetTy Succs; |
| 154 | BBInfoSetTy Preds; |
| 155 | |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 156 | BBInfo() : Atom(0), BB(0) {} |
| 157 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 158 | void addSucc(BBInfo &Succ) { |
| 159 | Succs.insert(&Succ); |
| 160 | Succ.Preds.insert(this); |
| 161 | } |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | void MCObjectDisassembler::buildCFG(MCModule *Module) { |
| 166 | typedef std::map<uint64_t, BBInfo> BBInfoByAddrTy; |
| 167 | BBInfoByAddrTy BBInfos; |
| 168 | typedef std::set<uint64_t> AddressSetTy; |
| 169 | AddressSetTy Splits; |
| 170 | AddressSetTy Calls; |
| 171 | |
Ahmed Bougacha | 0a30ccc | 2013-08-21 07:28:29 +0000 | [diff] [blame] | 172 | error_code ec; |
| 173 | for (symbol_iterator SI = Obj.begin_symbols(), SE = Obj.end_symbols(); |
| 174 | SI != SE; SI.increment(ec)) { |
| 175 | if (ec) |
| 176 | break; |
| 177 | SymbolRef::Type SymType; |
| 178 | SI->getType(SymType); |
| 179 | if (SymType == SymbolRef::ST_Function) { |
| 180 | uint64_t SymAddr; |
| 181 | SI->getAddress(SymAddr); |
Ahmed Bougacha | 484a6eb | 2013-08-21 07:28:37 +0000 | [diff] [blame] | 182 | SymAddr = getEffectiveLoadAddr(SymAddr); |
Ahmed Bougacha | 0a30ccc | 2013-08-21 07:28:29 +0000 | [diff] [blame] | 183 | Calls.insert(SymAddr); |
| 184 | Splits.insert(SymAddr); |
| 185 | } |
| 186 | } |
| 187 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 188 | assert(Module->func_begin() == Module->func_end() |
| 189 | && "Module already has a CFG!"); |
| 190 | |
| 191 | // First, determine the basic block boundaries and call targets. |
| 192 | for (MCModule::atom_iterator AI = Module->atom_begin(), |
| 193 | AE = Module->atom_end(); |
| 194 | AI != AE; ++AI) { |
| 195 | MCTextAtom *TA = dyn_cast<MCTextAtom>(*AI); |
| 196 | if (!TA) continue; |
| 197 | Calls.insert(TA->getBeginAddr()); |
Ahmed Bougacha | 7ab184a | 2013-06-19 20:18:59 +0000 | [diff] [blame] | 198 | BBInfos[TA->getBeginAddr()].Atom = TA; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 199 | for (MCTextAtom::const_iterator II = TA->begin(), IE = TA->end(); |
| 200 | II != IE; ++II) { |
| 201 | if (MIA.isTerminator(II->Inst)) |
| 202 | Splits.insert(II->Address + II->Size); |
| 203 | uint64_t Target; |
| 204 | if (MIA.evaluateBranch(II->Inst, II->Address, II->Size, Target)) { |
| 205 | if (MIA.isCall(II->Inst)) |
| 206 | Calls.insert(Target); |
| 207 | Splits.insert(Target); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Split text atoms into basic block atoms. |
| 213 | for (AddressSetTy::const_iterator SI = Splits.begin(), SE = Splits.end(); |
| 214 | SI != SE; ++SI) { |
| 215 | MCAtom *A = Module->findAtomContaining(*SI); |
| 216 | if (!A) continue; |
| 217 | MCTextAtom *TA = cast<MCTextAtom>(A); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 218 | if (TA->getBeginAddr() == *SI) |
| 219 | continue; |
| 220 | MCTextAtom *NewAtom = TA->split(*SI); |
| 221 | BBInfos[NewAtom->getBeginAddr()].Atom = NewAtom; |
| 222 | StringRef BBName = TA->getName(); |
| 223 | BBName = BBName.substr(0, BBName.find_last_of(':')); |
| 224 | NewAtom->setName((BBName + ":" + utohexstr(*SI)).str()); |
| 225 | } |
| 226 | |
| 227 | // Compute succs/preds. |
| 228 | for (MCModule::atom_iterator AI = Module->atom_begin(), |
| 229 | AE = Module->atom_end(); |
| 230 | AI != AE; ++AI) { |
| 231 | MCTextAtom *TA = dyn_cast<MCTextAtom>(*AI); |
| 232 | if (!TA) continue; |
| 233 | BBInfo &CurBB = BBInfos[TA->getBeginAddr()]; |
| 234 | const MCDecodedInst &LI = TA->back(); |
| 235 | if (MIA.isBranch(LI.Inst)) { |
| 236 | uint64_t Target; |
| 237 | if (MIA.evaluateBranch(LI.Inst, LI.Address, LI.Size, Target)) |
| 238 | CurBB.addSucc(BBInfos[Target]); |
| 239 | if (MIA.isConditionalBranch(LI.Inst)) |
| 240 | CurBB.addSucc(BBInfos[LI.Address + LI.Size]); |
| 241 | } else if (!MIA.isTerminator(LI.Inst)) |
| 242 | CurBB.addSucc(BBInfos[LI.Address + LI.Size]); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | // Create functions and basic blocks. |
| 247 | for (AddressSetTy::const_iterator CI = Calls.begin(), CE = Calls.end(); |
| 248 | CI != CE; ++CI) { |
| 249 | BBInfo &BBI = BBInfos[*CI]; |
| 250 | if (!BBI.Atom) continue; |
| 251 | |
| 252 | MCFunction &MCFN = *Module->createFunction(BBI.Atom->getName()); |
| 253 | |
| 254 | // Create MCBBs. |
| 255 | SmallSetVector<BBInfo*, 16> Worklist; |
| 256 | Worklist.insert(&BBI); |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 257 | for (size_t wi = 0; wi < Worklist.size(); ++wi) { |
| 258 | BBInfo *BBI = Worklist[wi]; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 259 | if (!BBI->Atom) |
| 260 | continue; |
| 261 | BBI->BB = &MCFN.createBlock(*BBI->Atom); |
| 262 | // Add all predecessors and successors to the worklist. |
| 263 | for (BBInfoSetTy::iterator SI = BBI->Succs.begin(), SE = BBI->Succs.end(); |
| 264 | SI != SE; ++SI) |
| 265 | Worklist.insert(*SI); |
| 266 | for (BBInfoSetTy::iterator PI = BBI->Preds.begin(), PE = BBI->Preds.end(); |
| 267 | PI != PE; ++PI) |
| 268 | Worklist.insert(*PI); |
| 269 | } |
| 270 | |
| 271 | // Set preds/succs. |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 272 | for (size_t wi = 0; wi < Worklist.size(); ++wi) { |
| 273 | BBInfo *BBI = Worklist[wi]; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 274 | MCBasicBlock *MCBB = BBI->BB; |
| 275 | if (!MCBB) |
| 276 | continue; |
| 277 | for (BBInfoSetTy::iterator SI = BBI->Succs.begin(), SE = BBI->Succs.end(); |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 278 | SI != SE; ++SI) |
| 279 | if ((*SI)->BB) |
| 280 | MCBB->addSuccessor((*SI)->BB); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 281 | for (BBInfoSetTy::iterator PI = BBI->Preds.begin(), PE = BBI->Preds.end(); |
Ahmed Bougacha | 4693727 | 2013-08-21 07:28:32 +0000 | [diff] [blame] | 282 | PI != PE; ++PI) |
| 283 | if ((*PI)->BB) |
| 284 | MCBB->addPredecessor((*PI)->BB); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |