blob: 4ce8e927933d693538d13c6b7a237b4d785e961c [file] [log] [blame]
Ahmed Bougachaef993562013-05-24 01:07:04 +00001//===- 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
28using namespace llvm;
29using namespace object;
30
31MCObjectDisassembler::MCObjectDisassembler(const ObjectFile &Obj,
32 const MCDisassembler &Dis,
33 const MCInstrAnalysis &MIA)
34 : Obj(Obj), Dis(Dis), MIA(MIA) {}
35
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000036uint64_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 Bougacha484a6eb2013-08-21 07:28:37 +000047 return getEffectiveLoadAddr(Entrypoint);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000048 }
49 }
50 return 0;
51}
52
53ArrayRef<uint64_t> MCObjectDisassembler::getStaticInitFunctions() {
54 return ArrayRef<uint64_t>();
55}
56
57ArrayRef<uint64_t> MCObjectDisassembler::getStaticExitFunctions() {
58 return ArrayRef<uint64_t>();
59}
60
Ahmed Bougacha484a6eb2013-08-21 07:28:37 +000061uint64_t MCObjectDisassembler::getEffectiveLoadAddr(uint64_t Addr) {
62 return Addr;
63}
64
65uint64_t MCObjectDisassembler::getOriginalLoadAddr(uint64_t Addr) {
66 return Addr;
67}
68
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000069MCModule *MCObjectDisassembler::buildEmptyModule() {
Ahmed Bougachaef993562013-05-24 01:07:04 +000070 MCModule *Module = new MCModule;
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000071 Module->Entrypoint = getEntrypoint();
72 return Module;
73}
74
75MCModule *MCObjectDisassembler::buildModule(bool withCFG) {
76 MCModule *Module = buildEmptyModule();
77
Ahmed Bougachaef993562013-05-24 01:07:04 +000078 buildSectionAtoms(Module);
79 if (withCFG)
80 buildCFG(Module);
81 return Module;
82}
83
84void 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 Bougacha484a6eb2013-08-21 07:28:37 +0000101 StartAddr = getEffectiveLoadAddr(StartAddr);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000102
103 StringRef Contents; SI->getContents(Contents);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +0000104 StringRefMemoryObject memoryObject(Contents, StartAddr);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000105
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 Bougacha46937272013-08-21 07:28:32 +0000114 MCTextAtom *Text = 0;
115 MCDataAtom *InvalidData = 0;
116
Ahmed Bougachaef993562013-05-24 01:07:04 +0000117 uint64_t InstSize;
118 for (uint64_t Index = 0; Index < SecSize; Index += InstSize) {
Ahmed Bougacha46937272013-08-21 07:28:32 +0000119 const uint64_t CurAddr = StartAddr + Index;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000120 MCInst Inst;
Ahmed Bougacha46937272013-08-21 07:28:32 +0000121 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 Bougachaef993562013-05-24 01:07:04 +0000127 Text->addInst(Inst, InstSize);
Ahmed Bougacha46937272013-08-21 07:28:32 +0000128 InvalidData = 0;
129 } else {
130 if (!InvalidData) {
131 Text = 0;
132 InvalidData = Module->createDataAtom(CurAddr, EndAddr);
133 }
134 InvalidData->addData(Contents[Index]);
135 }
Ahmed Bougachaef993562013-05-24 01:07:04 +0000136 }
Ahmed Bougachaef993562013-05-24 01:07:04 +0000137 } 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
146namespace {
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 Bougacha46937272013-08-21 07:28:32 +0000156 BBInfo() : Atom(0), BB(0) {}
157
Ahmed Bougachaef993562013-05-24 01:07:04 +0000158 void addSucc(BBInfo &Succ) {
159 Succs.insert(&Succ);
160 Succ.Preds.insert(this);
161 }
162 };
163}
164
165void 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 Bougacha0a30ccc2013-08-21 07:28:29 +0000172 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 Bougacha484a6eb2013-08-21 07:28:37 +0000182 SymAddr = getEffectiveLoadAddr(SymAddr);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +0000183 Calls.insert(SymAddr);
184 Splits.insert(SymAddr);
185 }
186 }
187
Ahmed Bougachaef993562013-05-24 01:07:04 +0000188 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 Bougacha7ab184a2013-06-19 20:18:59 +0000198 BBInfos[TA->getBeginAddr()].Atom = TA;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000199 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 Bougachaef993562013-05-24 01:07:04 +0000218 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 Bougacha46937272013-08-21 07:28:32 +0000257 for (size_t wi = 0; wi < Worklist.size(); ++wi) {
258 BBInfo *BBI = Worklist[wi];
Ahmed Bougachaef993562013-05-24 01:07:04 +0000259 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 Bougacha46937272013-08-21 07:28:32 +0000272 for (size_t wi = 0; wi < Worklist.size(); ++wi) {
273 BBInfo *BBI = Worklist[wi];
Ahmed Bougachaef993562013-05-24 01:07:04 +0000274 MCBasicBlock *MCBB = BBI->BB;
275 if (!MCBB)
276 continue;
277 for (BBInfoSetTy::iterator SI = BBI->Succs.begin(), SE = BBI->Succs.end();
Ahmed Bougacha46937272013-08-21 07:28:32 +0000278 SI != SE; ++SI)
279 if ((*SI)->BB)
280 MCBB->addSuccessor((*SI)->BB);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000281 for (BBInfoSetTy::iterator PI = BBI->Preds.begin(), PE = BBI->Preds.end();
Ahmed Bougacha46937272013-08-21 07:28:32 +0000282 PI != PE; ++PI)
283 if ((*PI)->BB)
284 MCBB->addPredecessor((*PI)->BB);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000285 }
286 }
287}