blob: 8a258cb0909720a91a67333912cdedfe5d335361 [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"
Ahmed Bougachaef993562013-05-24 01:07:04 +000011#include "llvm/ADT/SetVector.h"
Ahmed Bougacha05a81022013-08-21 07:28:51 +000012#include "llvm/ADT/SmallPtrSet.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000013#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"
Ahmed Bougacha0f4a5ba2013-08-21 07:28:48 +000021#include "llvm/MC/MCObjectSymbolizer.h"
Ahmed Bougacha0e83b902013-08-21 07:28:44 +000022#include "llvm/Object/MachO.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000023#include "llvm/Object/ObjectFile.h"
Ahmed Bougacha0e83b902013-08-21 07:28:44 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/MachO.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000026#include "llvm/Support/MemoryObject.h"
27#include "llvm/Support/StringRefMemoryObject.h"
28#include "llvm/Support/raw_ostream.h"
29#include <map>
Ahmed Bougachaef993562013-05-24 01:07:04 +000030
31using namespace llvm;
32using namespace object;
33
Stephen Hinesdce4a402014-05-29 02:49:00 -070034#define DEBUG_TYPE "mc"
35
Ahmed Bougachaef993562013-05-24 01:07:04 +000036MCObjectDisassembler::MCObjectDisassembler(const ObjectFile &Obj,
37 const MCDisassembler &Dis,
38 const MCInstrAnalysis &MIA)
Stephen Hinesdce4a402014-05-29 02:49:00 -070039 : Obj(Obj), Dis(Dis), MIA(MIA), MOS(nullptr) {}
Ahmed Bougachaef993562013-05-24 01:07:04 +000040
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000041uint64_t MCObjectDisassembler::getEntrypoint() {
Stephen Hines36b56882014-04-23 16:57:46 -070042 for (const SymbolRef &Symbol : Obj.symbols()) {
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000043 StringRef Name;
Stephen Hines36b56882014-04-23 16:57:46 -070044 Symbol.getName(Name);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000045 if (Name == "main" || Name == "_main") {
46 uint64_t Entrypoint;
Stephen Hines36b56882014-04-23 16:57:46 -070047 Symbol.getAddress(Entrypoint);
Ahmed Bougacha484a6eb2013-08-21 07:28:37 +000048 return getEffectiveLoadAddr(Entrypoint);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000049 }
50 }
51 return 0;
52}
53
54ArrayRef<uint64_t> MCObjectDisassembler::getStaticInitFunctions() {
55 return ArrayRef<uint64_t>();
56}
57
58ArrayRef<uint64_t> MCObjectDisassembler::getStaticExitFunctions() {
59 return ArrayRef<uint64_t>();
60}
61
Ahmed Bougachaf1764822013-08-21 07:28:55 +000062MemoryObject *MCObjectDisassembler::getRegionFor(uint64_t Addr) {
63 // FIXME: Keep track of object sections.
64 return FallbackRegion.get();
65}
66
Ahmed Bougacha484a6eb2013-08-21 07:28:37 +000067uint64_t MCObjectDisassembler::getEffectiveLoadAddr(uint64_t Addr) {
68 return Addr;
69}
70
71uint64_t MCObjectDisassembler::getOriginalLoadAddr(uint64_t Addr) {
72 return Addr;
73}
74
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000075MCModule *MCObjectDisassembler::buildEmptyModule() {
Ahmed Bougachaef993562013-05-24 01:07:04 +000076 MCModule *Module = new MCModule;
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +000077 Module->Entrypoint = getEntrypoint();
78 return Module;
79}
80
81MCModule *MCObjectDisassembler::buildModule(bool withCFG) {
82 MCModule *Module = buildEmptyModule();
83
Ahmed Bougachaef993562013-05-24 01:07:04 +000084 buildSectionAtoms(Module);
85 if (withCFG)
86 buildCFG(Module);
87 return Module;
88}
89
90void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
Stephen Hines36b56882014-04-23 16:57:46 -070091 for (const SectionRef &Section : Obj.sections()) {
92 bool isText;
93 Section.isText(isText);
94 bool isData;
95 Section.isData(isData);
Ahmed Bougachaef993562013-05-24 01:07:04 +000096 if (!isData && !isText)
97 continue;
98
Stephen Hines36b56882014-04-23 16:57:46 -070099 uint64_t StartAddr;
100 Section.getAddress(StartAddr);
101 uint64_t SecSize;
102 Section.getSize(SecSize);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000103 if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize)
104 continue;
Ahmed Bougacha484a6eb2013-08-21 07:28:37 +0000105 StartAddr = getEffectiveLoadAddr(StartAddr);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000106
Stephen Hines36b56882014-04-23 16:57:46 -0700107 StringRef Contents;
108 Section.getContents(Contents);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +0000109 StringRefMemoryObject memoryObject(Contents, StartAddr);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000110
111 // We don't care about things like non-file-backed sections yet.
112 if (Contents.size() != SecSize || !SecSize)
113 continue;
114 uint64_t EndAddr = StartAddr + SecSize - 1;
115
Stephen Hines36b56882014-04-23 16:57:46 -0700116 StringRef SecName;
117 Section.getName(SecName);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000118
119 if (isText) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700120 MCTextAtom *Text = nullptr;
121 MCDataAtom *InvalidData = nullptr;
Ahmed Bougacha46937272013-08-21 07:28:32 +0000122
Ahmed Bougachaef993562013-05-24 01:07:04 +0000123 uint64_t InstSize;
124 for (uint64_t Index = 0; Index < SecSize; Index += InstSize) {
Ahmed Bougacha46937272013-08-21 07:28:32 +0000125 const uint64_t CurAddr = StartAddr + Index;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000126 MCInst Inst;
Ahmed Bougacha46937272013-08-21 07:28:32 +0000127 if (Dis.getInstruction(Inst, InstSize, memoryObject, CurAddr, nulls(),
128 nulls())) {
129 if (!Text) {
130 Text = Module->createTextAtom(CurAddr, CurAddr);
131 Text->setName(SecName);
132 }
Ahmed Bougachaef993562013-05-24 01:07:04 +0000133 Text->addInst(Inst, InstSize);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700134 InvalidData = nullptr;
Ahmed Bougacha46937272013-08-21 07:28:32 +0000135 } else {
Rafael Espindola88e1e102013-10-16 19:03:14 +0000136 assert(InstSize && "getInstruction() consumed no bytes");
Ahmed Bougacha46937272013-08-21 07:28:32 +0000137 if (!InvalidData) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700138 Text = nullptr;
Rafael Espindola88e1e102013-10-16 19:03:14 +0000139 InvalidData = Module->createDataAtom(CurAddr, CurAddr+InstSize - 1);
Ahmed Bougacha46937272013-08-21 07:28:32 +0000140 }
Rafael Espindola88e1e102013-10-16 19:03:14 +0000141 for (uint64_t I = 0; I < InstSize; ++I)
142 InvalidData->addData(Contents[Index+I]);
Ahmed Bougacha46937272013-08-21 07:28:32 +0000143 }
Ahmed Bougachaef993562013-05-24 01:07:04 +0000144 }
Ahmed Bougachaef993562013-05-24 01:07:04 +0000145 } else {
146 MCDataAtom *Data = Module->createDataAtom(StartAddr, EndAddr);
147 Data->setName(SecName);
148 for (uint64_t Index = 0; Index < SecSize; ++Index)
149 Data->addData(Contents[Index]);
150 }
151 }
152}
153
154namespace {
155 struct BBInfo;
Ahmed Bougacha05a81022013-08-21 07:28:51 +0000156 typedef SmallPtrSet<BBInfo*, 2> BBInfoSetTy;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000157
158 struct BBInfo {
159 MCTextAtom *Atom;
160 MCBasicBlock *BB;
161 BBInfoSetTy Succs;
162 BBInfoSetTy Preds;
Ahmed Bougachaf1764822013-08-21 07:28:55 +0000163 MCObjectDisassembler::AddressSetTy SuccAddrs;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000164
Stephen Hinesdce4a402014-05-29 02:49:00 -0700165 BBInfo() : Atom(nullptr), BB(nullptr) {}
Ahmed Bougacha46937272013-08-21 07:28:32 +0000166
Ahmed Bougachaef993562013-05-24 01:07:04 +0000167 void addSucc(BBInfo &Succ) {
168 Succs.insert(&Succ);
169 Succ.Preds.insert(this);
170 }
171 };
172}
173
Ahmed Bougachaf1764822013-08-21 07:28:55 +0000174static void RemoveDupsFromAddressVector(MCObjectDisassembler::AddressSetTy &V) {
175 std::sort(V.begin(), V.end());
176 V.erase(std::unique(V.begin(), V.end()), V.end());
177}
178
Ahmed Bougachaef993562013-05-24 01:07:04 +0000179void MCObjectDisassembler::buildCFG(MCModule *Module) {
180 typedef std::map<uint64_t, BBInfo> BBInfoByAddrTy;
181 BBInfoByAddrTy BBInfos;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000182 AddressSetTy Splits;
183 AddressSetTy Calls;
184
Stephen Hines36b56882014-04-23 16:57:46 -0700185 for (const SymbolRef &Symbol : Obj.symbols()) {
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +0000186 SymbolRef::Type SymType;
Stephen Hines36b56882014-04-23 16:57:46 -0700187 Symbol.getType(SymType);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +0000188 if (SymType == SymbolRef::ST_Function) {
189 uint64_t SymAddr;
Stephen Hines36b56882014-04-23 16:57:46 -0700190 Symbol.getAddress(SymAddr);
Ahmed Bougacha484a6eb2013-08-21 07:28:37 +0000191 SymAddr = getEffectiveLoadAddr(SymAddr);
Ahmed Bougacha05a81022013-08-21 07:28:51 +0000192 Calls.push_back(SymAddr);
193 Splits.push_back(SymAddr);
Ahmed Bougacha0a30ccc2013-08-21 07:28:29 +0000194 }
195 }
196
Ahmed Bougachaef993562013-05-24 01:07:04 +0000197 assert(Module->func_begin() == Module->func_end()
198 && "Module already has a CFG!");
199
200 // First, determine the basic block boundaries and call targets.
201 for (MCModule::atom_iterator AI = Module->atom_begin(),
202 AE = Module->atom_end();
203 AI != AE; ++AI) {
204 MCTextAtom *TA = dyn_cast<MCTextAtom>(*AI);
205 if (!TA) continue;
Ahmed Bougacha05a81022013-08-21 07:28:51 +0000206 Calls.push_back(TA->getBeginAddr());
Ahmed Bougacha7ab184a2013-06-19 20:18:59 +0000207 BBInfos[TA->getBeginAddr()].Atom = TA;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000208 for (MCTextAtom::const_iterator II = TA->begin(), IE = TA->end();
209 II != IE; ++II) {
210 if (MIA.isTerminator(II->Inst))
Ahmed Bougacha05a81022013-08-21 07:28:51 +0000211 Splits.push_back(II->Address + II->Size);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000212 uint64_t Target;
213 if (MIA.evaluateBranch(II->Inst, II->Address, II->Size, Target)) {
214 if (MIA.isCall(II->Inst))
Ahmed Bougacha05a81022013-08-21 07:28:51 +0000215 Calls.push_back(Target);
216 Splits.push_back(Target);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000217 }
218 }
219 }
220
Ahmed Bougachaf1764822013-08-21 07:28:55 +0000221 RemoveDupsFromAddressVector(Splits);
222 RemoveDupsFromAddressVector(Calls);
Ahmed Bougacha05a81022013-08-21 07:28:51 +0000223
Ahmed Bougachaef993562013-05-24 01:07:04 +0000224 // Split text atoms into basic block atoms.
225 for (AddressSetTy::const_iterator SI = Splits.begin(), SE = Splits.end();
226 SI != SE; ++SI) {
227 MCAtom *A = Module->findAtomContaining(*SI);
228 if (!A) continue;
229 MCTextAtom *TA = cast<MCTextAtom>(A);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000230 if (TA->getBeginAddr() == *SI)
231 continue;
232 MCTextAtom *NewAtom = TA->split(*SI);
233 BBInfos[NewAtom->getBeginAddr()].Atom = NewAtom;
234 StringRef BBName = TA->getName();
235 BBName = BBName.substr(0, BBName.find_last_of(':'));
236 NewAtom->setName((BBName + ":" + utohexstr(*SI)).str());
237 }
238
239 // Compute succs/preds.
240 for (MCModule::atom_iterator AI = Module->atom_begin(),
241 AE = Module->atom_end();
242 AI != AE; ++AI) {
243 MCTextAtom *TA = dyn_cast<MCTextAtom>(*AI);
244 if (!TA) continue;
245 BBInfo &CurBB = BBInfos[TA->getBeginAddr()];
246 const MCDecodedInst &LI = TA->back();
247 if (MIA.isBranch(LI.Inst)) {
248 uint64_t Target;
249 if (MIA.evaluateBranch(LI.Inst, LI.Address, LI.Size, Target))
250 CurBB.addSucc(BBInfos[Target]);
251 if (MIA.isConditionalBranch(LI.Inst))
252 CurBB.addSucc(BBInfos[LI.Address + LI.Size]);
253 } else if (!MIA.isTerminator(LI.Inst))
254 CurBB.addSucc(BBInfos[LI.Address + LI.Size]);
255 }
256
257
258 // Create functions and basic blocks.
259 for (AddressSetTy::const_iterator CI = Calls.begin(), CE = Calls.end();
260 CI != CE; ++CI) {
261 BBInfo &BBI = BBInfos[*CI];
262 if (!BBI.Atom) continue;
263
264 MCFunction &MCFN = *Module->createFunction(BBI.Atom->getName());
265
266 // Create MCBBs.
267 SmallSetVector<BBInfo*, 16> Worklist;
268 Worklist.insert(&BBI);
Ahmed Bougacha46937272013-08-21 07:28:32 +0000269 for (size_t wi = 0; wi < Worklist.size(); ++wi) {
270 BBInfo *BBI = Worklist[wi];
Ahmed Bougachaef993562013-05-24 01:07:04 +0000271 if (!BBI->Atom)
272 continue;
273 BBI->BB = &MCFN.createBlock(*BBI->Atom);
274 // Add all predecessors and successors to the worklist.
275 for (BBInfoSetTy::iterator SI = BBI->Succs.begin(), SE = BBI->Succs.end();
276 SI != SE; ++SI)
277 Worklist.insert(*SI);
278 for (BBInfoSetTy::iterator PI = BBI->Preds.begin(), PE = BBI->Preds.end();
279 PI != PE; ++PI)
280 Worklist.insert(*PI);
281 }
282
283 // Set preds/succs.
Ahmed Bougacha46937272013-08-21 07:28:32 +0000284 for (size_t wi = 0; wi < Worklist.size(); ++wi) {
285 BBInfo *BBI = Worklist[wi];
Ahmed Bougachaef993562013-05-24 01:07:04 +0000286 MCBasicBlock *MCBB = BBI->BB;
287 if (!MCBB)
288 continue;
289 for (BBInfoSetTy::iterator SI = BBI->Succs.begin(), SE = BBI->Succs.end();
Ahmed Bougacha46937272013-08-21 07:28:32 +0000290 SI != SE; ++SI)
291 if ((*SI)->BB)
292 MCBB->addSuccessor((*SI)->BB);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000293 for (BBInfoSetTy::iterator PI = BBI->Preds.begin(), PE = BBI->Preds.end();
Ahmed Bougacha46937272013-08-21 07:28:32 +0000294 PI != PE; ++PI)
295 if ((*PI)->BB)
296 MCBB->addPredecessor((*PI)->BB);
Ahmed Bougachaef993562013-05-24 01:07:04 +0000297 }
298 }
299}
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000300
Ahmed Bougachaf1764822013-08-21 07:28:55 +0000301// Basic idea of the disassembly + discovery:
302//
303// start with the wanted address, insert it in the worklist
304// while worklist not empty, take next address in the worklist:
305// - check if atom exists there
306// - if middle of atom:
307// - split basic blocks referencing the atom
308// - look for an already encountered BBInfo (using a map<atom, bbinfo>)
309// - if there is, split it (new one, fallthrough, move succs, etc..)
310// - if start of atom: nothing else to do
311// - if no atom: create new atom and new bbinfo
312// - look at the last instruction in the atom, add succs to worklist
313// for all elements in the worklist:
314// - create basic block, update preds/succs, etc..
315//
316MCBasicBlock *MCObjectDisassembler::getBBAt(MCModule *Module, MCFunction *MCFN,
317 uint64_t BBBeginAddr,
318 AddressSetTy &CallTargets,
319 AddressSetTy &TailCallTargets) {
320 typedef std::map<uint64_t, BBInfo> BBInfoByAddrTy;
321 typedef SmallSetVector<uint64_t, 16> AddrWorklistTy;
322 BBInfoByAddrTy BBInfos;
323 AddrWorklistTy Worklist;
324
325 Worklist.insert(BBBeginAddr);
326 for (size_t wi = 0; wi < Worklist.size(); ++wi) {
327 const uint64_t BeginAddr = Worklist[wi];
328 BBInfo *BBI = &BBInfos[BeginAddr];
329
330 MCTextAtom *&TA = BBI->Atom;
331 assert(!TA && "Discovered basic block already has an associated atom!");
332
333 // Look for an atom at BeginAddr.
334 if (MCAtom *A = Module->findAtomContaining(BeginAddr)) {
335 // FIXME: We don't care about mixed atoms, see above.
336 TA = cast<MCTextAtom>(A);
337
338 // The found atom doesn't begin at BeginAddr, we have to split it.
339 if (TA->getBeginAddr() != BeginAddr) {
340 // FIXME: Handle overlapping atoms: middle-starting instructions, etc..
341 MCTextAtom *NewTA = TA->split(BeginAddr);
342
343 // Look for an already encountered basic block that needs splitting
344 BBInfoByAddrTy::iterator It = BBInfos.find(TA->getBeginAddr());
345 if (It != BBInfos.end() && It->second.Atom) {
346 BBI->SuccAddrs = It->second.SuccAddrs;
347 It->second.SuccAddrs.clear();
348 It->second.SuccAddrs.push_back(BeginAddr);
349 }
350 TA = NewTA;
351 }
352 BBI->Atom = TA;
353 } else {
354 // If we didn't find an atom, then we have to disassemble to create one!
355
356 MemoryObject *Region = getRegionFor(BeginAddr);
357 if (!Region)
358 llvm_unreachable(("Couldn't find suitable region for disassembly at " +
359 utostr(BeginAddr)).c_str());
360
361 uint64_t InstSize;
362 uint64_t EndAddr = Region->getBase() + Region->getExtent();
363
364 // We want to stop before the next atom and have a fallthrough to it.
365 if (MCTextAtom *NextAtom =
366 cast_or_null<MCTextAtom>(Module->findFirstAtomAfter(BeginAddr)))
367 EndAddr = std::min(EndAddr, NextAtom->getBeginAddr());
368
369 for (uint64_t Addr = BeginAddr; Addr < EndAddr; Addr += InstSize) {
370 MCInst Inst;
371 if (Dis.getInstruction(Inst, InstSize, *Region, Addr, nulls(),
372 nulls())) {
373 if (!TA)
374 TA = Module->createTextAtom(Addr, Addr);
375 TA->addInst(Inst, InstSize);
376 } else {
377 // We don't care about splitting mixed atoms either.
378 llvm_unreachable("Couldn't disassemble instruction in atom.");
379 }
380
381 uint64_t BranchTarget;
382 if (MIA.evaluateBranch(Inst, Addr, InstSize, BranchTarget)) {
383 if (MIA.isCall(Inst))
384 CallTargets.push_back(BranchTarget);
385 }
386
387 if (MIA.isTerminator(Inst))
388 break;
389 }
390 BBI->Atom = TA;
391 }
392
393 assert(TA && "Couldn't disassemble atom, none was created!");
394 assert(TA->begin() != TA->end() && "Empty atom!");
395
396 MemoryObject *Region = getRegionFor(TA->getBeginAddr());
397 assert(Region && "Couldn't find region for already disassembled code!");
398 uint64_t EndRegion = Region->getBase() + Region->getExtent();
399
400 // Now we have a basic block atom, add successors.
401 // Add the fallthrough block.
402 if ((MIA.isConditionalBranch(TA->back().Inst) ||
403 !MIA.isTerminator(TA->back().Inst)) &&
404 (TA->getEndAddr() + 1 < EndRegion)) {
405 BBI->SuccAddrs.push_back(TA->getEndAddr() + 1);
406 Worklist.insert(TA->getEndAddr() + 1);
407 }
408
409 // If the terminator is a branch, add the target block.
410 if (MIA.isBranch(TA->back().Inst)) {
411 uint64_t BranchTarget;
412 if (MIA.evaluateBranch(TA->back().Inst, TA->back().Address,
413 TA->back().Size, BranchTarget)) {
414 StringRef ExtFnName;
415 if (MOS)
416 ExtFnName =
417 MOS->findExternalFunctionAt(getOriginalLoadAddr(BranchTarget));
418 if (!ExtFnName.empty()) {
419 TailCallTargets.push_back(BranchTarget);
420 CallTargets.push_back(BranchTarget);
421 } else {
422 BBI->SuccAddrs.push_back(BranchTarget);
423 Worklist.insert(BranchTarget);
424 }
425 }
426 }
427 }
428
429 for (size_t wi = 0, we = Worklist.size(); wi != we; ++wi) {
430 const uint64_t BeginAddr = Worklist[wi];
431 BBInfo *BBI = &BBInfos[BeginAddr];
432
433 assert(BBI->Atom && "Found a basic block without an associated atom!");
434
435 // Look for a basic block at BeginAddr.
436 BBI->BB = MCFN->find(BeginAddr);
437 if (BBI->BB) {
438 // FIXME: check that the succs/preds are the same
439 continue;
440 }
441 // If there was none, we have to create one from the atom.
442 BBI->BB = &MCFN->createBlock(*BBI->Atom);
443 }
444
445 for (size_t wi = 0, we = Worklist.size(); wi != we; ++wi) {
446 const uint64_t BeginAddr = Worklist[wi];
447 BBInfo *BBI = &BBInfos[BeginAddr];
448 MCBasicBlock *BB = BBI->BB;
449
450 RemoveDupsFromAddressVector(BBI->SuccAddrs);
451 for (AddressSetTy::const_iterator SI = BBI->SuccAddrs.begin(),
452 SE = BBI->SuccAddrs.end();
453 SE != SE; ++SI) {
454 MCBasicBlock *Succ = BBInfos[*SI].BB;
455 BB->addSuccessor(Succ);
456 Succ->addPredecessor(BB);
457 }
458 }
459
460 assert(BBInfos[Worklist[0]].BB &&
461 "No basic block created at requested address?");
462
463 return BBInfos[Worklist[0]].BB;
464}
465
466MCFunction *
467MCObjectDisassembler::createFunction(MCModule *Module, uint64_t BeginAddr,
468 AddressSetTy &CallTargets,
469 AddressSetTy &TailCallTargets) {
470 // First, check if this is an external function.
471 StringRef ExtFnName;
472 if (MOS)
473 ExtFnName = MOS->findExternalFunctionAt(getOriginalLoadAddr(BeginAddr));
474 if (!ExtFnName.empty())
475 return Module->createFunction(ExtFnName);
476
477 // If it's not, look for an existing function.
478 for (MCModule::func_iterator FI = Module->func_begin(),
479 FE = Module->func_end();
480 FI != FE; ++FI) {
481 if ((*FI)->empty())
482 continue;
483 // FIXME: MCModule should provide a findFunctionByAddr()
484 if ((*FI)->getEntryBlock()->getInsts()->getBeginAddr() == BeginAddr)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700485 return FI->get();
Ahmed Bougachaf1764822013-08-21 07:28:55 +0000486 }
487
488 // Finally, just create a new one.
489 MCFunction *MCFN = Module->createFunction("");
490 getBBAt(Module, MCFN, BeginAddr, CallTargets, TailCallTargets);
491 return MCFN;
492}
493
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000494// MachO MCObjectDisassembler implementation.
495
496MCMachOObjectDisassembler::MCMachOObjectDisassembler(
497 const MachOObjectFile &MOOF, const MCDisassembler &Dis,
498 const MCInstrAnalysis &MIA, uint64_t VMAddrSlide,
499 uint64_t HeaderLoadAddress)
500 : MCObjectDisassembler(MOOF, Dis, MIA), MOOF(MOOF),
501 VMAddrSlide(VMAddrSlide), HeaderLoadAddress(HeaderLoadAddress) {
502
Stephen Hines36b56882014-04-23 16:57:46 -0700503 for (const SectionRef &Section : MOOF.sections()) {
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000504 StringRef Name;
Stephen Hines36b56882014-04-23 16:57:46 -0700505 Section.getName(Name);
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000506 // FIXME: We should use the S_ section type instead of the name.
507 if (Name == "__mod_init_func") {
508 DEBUG(dbgs() << "Found __mod_init_func section!\n");
Stephen Hines36b56882014-04-23 16:57:46 -0700509 Section.getContents(ModInitContents);
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000510 } else if (Name == "__mod_exit_func") {
511 DEBUG(dbgs() << "Found __mod_exit_func section!\n");
Stephen Hines36b56882014-04-23 16:57:46 -0700512 Section.getContents(ModExitContents);
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000513 }
514 }
515}
516
517// FIXME: Only do the translations for addresses actually inside the object.
518uint64_t MCMachOObjectDisassembler::getEffectiveLoadAddr(uint64_t Addr) {
519 return Addr + VMAddrSlide;
520}
521
522uint64_t
523MCMachOObjectDisassembler::getOriginalLoadAddr(uint64_t EffectiveAddr) {
524 return EffectiveAddr - VMAddrSlide;
525}
526
527uint64_t MCMachOObjectDisassembler::getEntrypoint() {
528 uint64_t EntryFileOffset = 0;
529
530 // Look for LC_MAIN.
531 {
Charles Davis55107282013-09-01 04:28:48 +0000532 uint32_t LoadCommandCount = MOOF.getHeader().ncmds;
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000533 MachOObjectFile::LoadCommandInfo Load = MOOF.getFirstLoadCommandInfo();
534 for (unsigned I = 0;; ++I) {
Charles Davis55107282013-09-01 04:28:48 +0000535 if (Load.C.cmd == MachO::LC_MAIN) {
Ahmed Bougacha0e83b902013-08-21 07:28:44 +0000536 EntryFileOffset =
537 ((const MachO::entry_point_command *)Load.Ptr)->entryoff;
538 break;
539 }
540
541 if (I == LoadCommandCount - 1)
542 break;
543 else
544 Load = MOOF.getNextLoadCommandInfo(Load);
545 }
546 }
547
548 // If we didn't find anything, default to the common implementation.
549 // FIXME: Maybe we could also look at LC_UNIXTHREAD and friends?
550 if (EntryFileOffset)
551 return MCObjectDisassembler::getEntrypoint();
552
553 return EntryFileOffset + HeaderLoadAddress;
554}
555
556ArrayRef<uint64_t> MCMachOObjectDisassembler::getStaticInitFunctions() {
557 // FIXME: We only handle 64bit mach-o
558 assert(MOOF.is64Bit());
559
560 size_t EntrySize = 8;
561 size_t EntryCount = ModInitContents.size() / EntrySize;
562 return ArrayRef<uint64_t>(
563 reinterpret_cast<const uint64_t *>(ModInitContents.data()), EntryCount);
564}
565
566ArrayRef<uint64_t> MCMachOObjectDisassembler::getStaticExitFunctions() {
567 // FIXME: We only handle 64bit mach-o
568 assert(MOOF.is64Bit());
569
570 size_t EntrySize = 8;
571 size_t EntryCount = ModExitContents.size() / EntrySize;
572 return ArrayRef<uint64_t>(
573 reinterpret_cast<const uint64_t *>(ModExitContents.data()), EntryCount);
574}