+ void Binary::disassemble_functions() {
+ error_code ec;
+ for (section_iterator i = o->begin_sections(),
+ e = o->end_sections();
+ i != e; i.increment(ec)) {
+ if (error(ec)) break;
+ bool text;
+ if (error(i->isText(text))) break;
+ if (!text) continue;
+
+ uint64_t SectionAddr;
+ if (error(i->getAddress(SectionAddr))) break;
+
+ // Make a list of all the symbols in this section.
+ std::vector<std::pair<uint64_t, StringRef> > Symbols;
+ for (symbol_iterator si = o->begin_symbols(),
+ se = o->end_symbols();
+ si != se; si.increment(ec)) {
+ bool contains;
+ if (!error(i->containsSymbol(*si, contains)) && contains) {
+ uint64_t Address;
+ if (error(si->getAddress(Address))) break;
+ if (Address == UnknownAddressOrSize) continue;
+ Address -= SectionAddr;
+
+ StringRef Name;
+ if (error(si->getName(Name))) break;
+
+ outs() << "\nXXX " << Name << "\n";
+
+ Symbols.push_back(std::make_pair(Address, Name));
+ }
+ }
+
+ // Sort the symbols by address, just in case they didn't come in that way.
+ array_pod_sort(Symbols.begin(), Symbols.end());
+
+ // Make a list of all the relocations for this section.
+ std::vector<RelocationRef> Rels;
+ // if (InlineRelocs) {
+ // for (relocation_iterator ri = i->begin_relocations(),
+ // re = i->end_relocations();
+ // ri != re; ri.increment(ec)) {
+ // if (error(ec)) break;
+ // Rels.push_back(*ri);
+ // }
+ // }
+
+ // Sort relocations by address.
+ std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
+
+ StringRef SegmentName = "";
+ // if (const MachOObjectFile *MachO =
+ // dyn_cast<const MachOObjectFile>(o)) {
+ // DataRefImpl DR = i->getRawDataRefImpl();
+ // SegmentName = MachO->getSectionFinalSegmentName(DR);
+ // }
+ StringRef name;
+ if (error(i->getName(name))) break;
+ outs() << "Disassembly of section ";
+ if (!SegmentName.empty())
+ outs() << SegmentName << ",";
+ outs() << name << ':';
+
+ // If the section has no symbols just insert a dummy one and disassemble
+ // the whole section.
+ if (Symbols.empty())
+ Symbols.push_back(std::make_pair(0, name));
+
+
+ StringRef Bytes;
+ if (error(i->getContents(Bytes))) break;
+ StringRefMemoryObject memoryObject(Bytes);
+ uint64_t Size;
+ uint64_t Index;
+ uint64_t SectSize;
+ if (error(i->getSize(SectSize))) break;
+
+ std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
+ std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
+ // Disassemble symbol by symbol.
+ for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
+ uint64_t Start = Symbols[si].first;
+ uint64_t End;
+ // The end is either the size of the section or the beginning of the next
+ // symbol.
+ if (si == se - 1)
+ End = SectSize;
+ // Make sure this symbol takes up space.
+ else if (Symbols[si + 1].first != Start)
+ End = Symbols[si + 1].first - 1;
+ else
+ // This symbol has the same address as the next symbol. Skip it.
+ continue;
+
+ outs() << '\n' << Symbols[si].second << ":\n";