diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2016-09-11 22:54:50 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2016-09-11 22:54:50 +0200 |
| commit | 0828d0bf368d7e0bdc835062aad62bc336047350 (patch) | |
| tree | e876d5cbf008c750e7c34abb36b406b67a2ae156 | |
| parent | 0ce4a59418937e562b4830b1b0654d0a4887addb (diff) | |
| download | openwar-0828d0bf368d7e0bdc835062aad62bc336047350.tar.gz openwar-0828d0bf368d7e0bdc835062aad62bc336047350.tar.bz2 openwar-0828d0bf368d7e0bdc835062aad62bc336047350.zip | |
Writes function assembly to files.
| -rw-r--r-- | disasm/extractfunction.cpp | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/disasm/extractfunction.cpp b/disasm/extractfunction.cpp index ce54240..3fc12e1 100644 --- a/disasm/extractfunction.cpp +++ b/disasm/extractfunction.cpp @@ -6,11 +6,21 @@ #include <distorm.h> +#include <boost/filesystem/operations.hpp> + #include <iomanip> #include <fstream> void extract_function(std::string file_path, binparse::Value32 object_id, binparse::Offset32 function_offset) { + std::stringstream ss; + ss << "f_" << function_offset << ".a"; + std::string o_file = ss.str(); + if(boost::filesystem::exists(o_file)) { + return; + } + std::ofstream output(o_file); + std::ifstream ifs(file_path, std::ios::binary); ifs.unsetf(std::ios::skipws); @@ -32,8 +42,6 @@ void extract_function(std::string file_path, binparse::Value32 object_id, binpar ci.dt = dt; ci.features = DF_RETURN_FC_ONLY | DF_STOP_ON_FLOW_CONTROL; - std::cout << "Function starts at: " << function_offset << std::endl; - binparse::Offset32 furthestjmp; bool done = false; @@ -46,7 +54,9 @@ void extract_function(std::string file_path, binparse::Value32 object_id, binpar switch(META_GET_FC(decinst.meta)) { case FC_CALL: { - //std::cout << "Call:\t"; + binparse::Offset32 target = binparse::Offset32(INSTRUCTION_GET_TARGET(&decinst)); + std::cout << binparse::Offset32(ci.codeOffset) << " " << target << std::endl; + extract_function(file_path, object_id, target); break; } case FC_CMOV: { @@ -104,26 +114,32 @@ void extract_function(std::string file_path, binparse::Value32 object_id, binpar ci.codeOffset += distance; } } + std::cout << "Function starts at: " << function_offset << std::endl; std::cout << "Function ends at: " << binparse::Offset32(ci.nextOffset) << std::endl; - std::vector<_DecodedInst> instructions; - instructions.resize(ci.nextOffset - function_offset); - unsigned int read_inst; + auto end = ci.nextOffset; - auto result = distorm_decode64( - file.object_table.entries[object_id].reloc_base_address + buffer_offset, - code_buf.data() + buffer_offset, - ci.nextOffset - function_offset, - Decode32Bits, - instructions.data(), - instructions.size(), - &read_inst - ); + ci.code = code_buf.data() + buffer_offset; + ci.nextOffset = 0; + ci.codeLen = code_buf.size() - buffer_offset; + ci.codeOffset = base_reloc_offset + buffer_offset; + ci.dt = dt; + ci.features = DF_NONE; - instructions.resize(read_inst); - instructions.shrink_to_fit(); + while(ci.nextOffset < end) { + distorm_decompose(&ci, &decinst, 1, &decodedInstructionsCount); + + _DecodedInst inst; + distorm_format64(&ci, &decinst, &inst); - for(auto&& inst : instructions) { std::cout << std::hex << std::setw(8) << std::setfill('0') << inst.offset << ":\t" << inst.mnemonic.p << " " << inst.operands.p << std::endl; + output << inst.mnemonic.p << " " << inst.operands.p << std::endl; + + auto distance = ci.nextOffset - ci.codeOffset; + + ci.code += distance; + ci.codeLen -= distance; + ci.codeOffset += distance; } + } |
