What year is it! This file has no reason to be written in C, and has doubly no
[oota-llvm.git] / lib / MC / MCObjectDisassembler.cpp
index fa4663740d3488bd3567fdfc15cca7d304a4948e..1487563e715e53aceed998ddf86d81c38ab4abd1 100644 (file)
@@ -34,19 +34,15 @@ using namespace object;
 MCObjectDisassembler::MCObjectDisassembler(const ObjectFile &Obj,
                                            const MCDisassembler &Dis,
                                            const MCInstrAnalysis &MIA)
-    : Obj(Obj), Dis(Dis), MIA(MIA), MOS(0) {}
+    : Obj(Obj), Dis(Dis), MIA(MIA), MOS(nullptr) {}
 
 uint64_t MCObjectDisassembler::getEntrypoint() {
-  error_code ec;
-  for (symbol_iterator SI = Obj.begin_symbols(), SE = Obj.end_symbols();
-       SI != SE; SI.increment(ec)) {
-    if (ec)
-      break;
+  for (const SymbolRef &Symbol : Obj.symbols()) {
     StringRef Name;
-    SI->getName(Name);
+    Symbol.getName(Name);
     if (Name == "main" || Name == "_main") {
       uint64_t Entrypoint;
-      SI->getAddress(Entrypoint);
+      Symbol.getAddress(Entrypoint);
       return getEffectiveLoadAddr(Entrypoint);
     }
   }
@@ -90,25 +86,24 @@ MCModule *MCObjectDisassembler::buildModule(bool withCFG) {
 }
 
 void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
-  error_code ec;
-  for (section_iterator SI = Obj.begin_sections(),
-                        SE = Obj.end_sections();
-                        SI != SE;
-                        SI.increment(ec)) {
-    if (ec) break;
-
-    bool isText; SI->isText(isText);
-    bool isData; SI->isData(isData);
+  for (const SectionRef &Section : Obj.sections()) {
+    bool isText;
+    Section.isText(isText);
+    bool isData;
+    Section.isData(isData);
     if (!isData && !isText)
       continue;
 
-    uint64_t StartAddr; SI->getAddress(StartAddr);
-    uint64_t SecSize; SI->getSize(SecSize);
+    uint64_t StartAddr;
+    Section.getAddress(StartAddr);
+    uint64_t SecSize;
+    Section.getSize(SecSize);
     if (StartAddr == UnknownAddressOrSize || SecSize == UnknownAddressOrSize)
       continue;
     StartAddr = getEffectiveLoadAddr(StartAddr);
 
-    StringRef Contents; SI->getContents(Contents);
+    StringRef Contents;
+    Section.getContents(Contents);
     StringRefMemoryObject memoryObject(Contents, StartAddr);
 
     // We don't care about things like non-file-backed sections yet.
@@ -116,11 +111,12 @@ void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
       continue;
     uint64_t EndAddr = StartAddr + SecSize - 1;
 
-    StringRef SecName; SI->getName(SecName);
+    StringRef SecName;
+    Section.getName(SecName);
 
     if (isText) {
-      MCTextAtom *Text = 0;
-      MCDataAtom *InvalidData = 0;
+      MCTextAtom *Text = nullptr;
+      MCDataAtom *InvalidData = nullptr;
 
       uint64_t InstSize;
       for (uint64_t Index = 0; Index < SecSize; Index += InstSize) {
@@ -133,13 +129,15 @@ void MCObjectDisassembler::buildSectionAtoms(MCModule *Module) {
             Text->setName(SecName);
           }
           Text->addInst(Inst, InstSize);
-          InvalidData = 0;
+          InvalidData = nullptr;
         } else {
+          assert(InstSize && "getInstruction() consumed no bytes");
           if (!InvalidData) {
-            Text = 0;
-            InvalidData = Module->createDataAtom(CurAddr, EndAddr);
+            Text = nullptr;
+            InvalidData = Module->createDataAtom(CurAddr, CurAddr+InstSize - 1);
           }
-          InvalidData->addData(Contents[Index]);
+          for (uint64_t I = 0; I < InstSize; ++I)
+            InvalidData->addData(Contents[Index+I]);
         }
       }
     } else {
@@ -162,7 +160,7 @@ namespace {
     BBInfoSetTy Preds;
     MCObjectDisassembler::AddressSetTy SuccAddrs;
 
-    BBInfo() : Atom(0), BB(0) {}
+    BBInfo() : Atom(nullptr), BB(nullptr) {}
 
     void addSucc(BBInfo &Succ) {
       Succs.insert(&Succ);
@@ -182,16 +180,12 @@ void MCObjectDisassembler::buildCFG(MCModule *Module) {
   AddressSetTy Splits;
   AddressSetTy Calls;
 
-  error_code ec;
-  for (symbol_iterator SI = Obj.begin_symbols(), SE = Obj.end_symbols();
-       SI != SE; SI.increment(ec)) {
-    if (ec)
-      break;
+  for (const SymbolRef &Symbol : Obj.symbols()) {
     SymbolRef::Type SymType;
-    SI->getType(SymType);
+    Symbol.getType(SymType);
     if (SymType == SymbolRef::ST_Function) {
       uint64_t SymAddr;
-      SI->getAddress(SymAddr);
+      Symbol.getAddress(SymAddr);
       SymAddr = getEffectiveLoadAddr(SymAddr);
       Calls.push_back(SymAddr);
       Splits.push_back(SymAddr);
@@ -486,7 +480,7 @@ MCObjectDisassembler::createFunction(MCModule *Module, uint64_t BeginAddr,
       continue;
     // FIXME: MCModule should provide a findFunctionByAddr()
     if ((*FI)->getEntryBlock()->getInsts()->getBeginAddr() == BeginAddr)
-      return *FI;
+      return FI->get();
   }
 
   // Finally, just create a new one.
@@ -504,20 +498,16 @@ MCMachOObjectDisassembler::MCMachOObjectDisassembler(
     : MCObjectDisassembler(MOOF, Dis, MIA), MOOF(MOOF),
       VMAddrSlide(VMAddrSlide), HeaderLoadAddress(HeaderLoadAddress) {
 
-  error_code ec;
-  for (section_iterator SI = MOOF.begin_sections(), SE = MOOF.end_sections();
-       SI != SE; SI.increment(ec)) {
-    if (ec)
-      break;
+  for (const SectionRef &Section : MOOF.sections()) {
     StringRef Name;
-    SI->getName(Name);
+    Section.getName(Name);
     // FIXME: We should use the S_ section type instead of the name.
     if (Name == "__mod_init_func") {
       DEBUG(dbgs() << "Found __mod_init_func section!\n");
-      SI->getContents(ModInitContents);
+      Section.getContents(ModInitContents);
     } else if (Name == "__mod_exit_func") {
       DEBUG(dbgs() << "Found __mod_exit_func section!\n");
-      SI->getContents(ModExitContents);
+      Section.getContents(ModExitContents);
     }
   }
 }