class COFFT {
};
+
+ class MACHOT {
+
+ };
}
/*
if (COFFObjectFile * object = dyn_cast<COFFObjectFile>(op)) {
return new LLVMDisassembler<COFFT>(filename, manager, object);
}
+ if (MachOObjectFile * object = dyn_cast<MachOObjectFile>(op)) {
+ return new LLVMDisassembler<MACHOT>(filename, manager, object);
+ }
return NULL;
}
InformationManager* manager,
ObjectFile* file)
: Disassembler()
- , logger(log4cxx::Logger::getLogger("LLVMDisassembler"))
+ , logger(log4cxx::Logger::getLogger("disassembler.LLVMDisassembler"))
, triple("unknown-unknown-unknown")
, manager(manager)
{
template <typename ELFT>
Function* LLVMDisassembler<ELFT>::disassembleFunctionAt(uint64_t address, const std::string& name) {
Function * function;
- SectionRef text_section = sections[".text"];
+ SectionRef text_section = getTextSection();
uint64_t base_address, size;
text_section.getAddress(base_address);
text_section.getSize(size);
template <typename ELFT>
void LLVMDisassembler<ELFT>::disassembleFunction(Function* function) {
+ std::vector<uint64_t> called_functions;
std::stack<BasicBlock*> remaining_blocks;
/* TODO:
* Do all blocks get added properly? We should take care to remove
* the other ones at the end of the function!
*/
std::map<uint64_t, BasicBlock*> new_blocks;
- SectionRef text_section = sections[".text"];
+ SectionRef text_section = getTextSection();
StringRef bytes;
text_section.getContents(bytes);
StringRefMemoryObject ref(bytes);
new_blocks.insert(std::make_pair(block->getStartAddress(), block));
function->addBasicBlock(block);
+ uint64_t base_address, size;
+ text_section.getAddress(base_address);
+ text_section.getSize(size);
+ LOG4CXX_DEBUG(logger, "Text section at " << std::hex << base_address << " with size " << size);
+
while (remaining_blocks.size()) {
BasicBlock * current_block = remaining_blocks.top();
remaining_blocks.pop();
<< current_block->getStartAddress());
uint64_t inst_size;
- uint64_t base_address;
- text_section.getAddress(base_address);
uint64_t current_address = current_block->getStartAddress() - base_address;
while(true) {
MCInst inst;
if (!MIA->isIndirectBranch(inst)) {
if (MIA->isCall(inst)) {
if (NULL == manager->getFunction(jmptarget))
- disassembleFunctionAt(jmptarget);
+ called_functions.push_back(jmptarget);
} else {
current_block->setNextBlock(0, jmptarget);
if (new_blocks.find(jmptarget) == new_blocks.end()) {
splitBlocks(function);
LOG4CXX_DEBUG(logger, "Finished function " << function->getName());
manager->finishFunction(function);
+ for (uint64_t address : called_functions)
+ disassembleFunctionAt(address);
}
template <typename ELFT>
void LLVMDisassembler<ELFT>::disassemble() {
- SectionRef text_section = sections[".text"];
+ SectionRef text_section = getTextSection();
std::vector<Function*> remaining_functions;
// Assume all function symbols actually start a real function
if (!x->second.getAddress(result)) {
Function * fun = manager->newFunction(result);
- fun->setName(x->first);
- remaining_functions.push_back(fun);
- LOG4CXX_DEBUG(logger, "Disasembling " << x->first);
+ if (fun) {
+ fun->setName(x->first);
+ remaining_functions.push_back(fun);
+ LOG4CXX_DEBUG(logger, "Disasembling " << x->first);
+ } else {
+ LOG4CXX_DEBUG(logger, "Function at " << std::hex << result
+ << " already disassembled as " << manager->getFunction(result)->getName());
+ }
}
}
}
}
+template<>
+uint64_t LLVMDisassembler<MACHOT>::entryAddress() {
+ // TODO
+ return 0;
+}
+
template <typename ELFT>
uint64_t LLVMDisassembler<ELFT>::entryAddress() {
const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
template <typename ELFT>
void LLVMDisassembler<ELFT>::splitBlocks(Function* function) {
- SectionRef text_section = sections[".text"];
+ SectionRef text_section = getTextSection();
StringRef bytes;
text_section.getContents(bytes);
StringRefMemoryObject ref(bytes);
+ LOG4CXX_DEBUG(logger, "Splitting Blocks in Function " << function->getName());
// Split blocks where jumps are going inside the block
for (auto it = function->blocks().begin();
it != function->blocks().end();
++it) {
BasicBlock * current_block = it->second;
+ if (current_block->getEndAddress() == 0) {
+ LOG4CXX_ERROR(logger, "UNFINISHED BLOCK " << std::hex << current_block->getStartAddress());
+ break;
+ }
uint64_t inst_size;
uint64_t base_address;
text_section.getAddress(base_address);
//TODO
}
+template<>
+void LLVMDisassembler<MACHOT>::readDynamicSymbols() {
+ //TODO
+}
+
template <typename ELFT>
void LLVMDisassembler<ELFT>::readDynamicSymbols() {
const auto elffile = dyn_cast<ELFObjectFile<ELFT>>(o)->getELFFile();
std::function<void (uint8_t*, size_t,
const std::string&,
const std::string&)> fun) {
- SectionRef text_section = sections[".text"];
+ SectionRef text_section = getTextSection();
uint64_t base_address;
text_section.getAddress(base_address);
uint64_t current_address = start - base_address;
current_address += inst_size;
}
}
+
+template <typename ELFT>
+SectionRef LLVMDisassembler<ELFT>::getTextSection() {
+ return sections[".text"];
+}
+
+template <>
+SectionRef LLVMDisassembler<MACHOT>::getTextSection() {
+ return sections["__text"];
+}