summaryrefslogtreecommitdiff
path: root/disasm
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-09-10 16:04:23 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-09-10 16:04:23 +0200
commit4f65043e40dbaf8e4cc290327e34270645dedce3 (patch)
tree8fe2979b77d0edfd99d64db77ae43a437b888fca /disasm
parentcec56db9d4c84de13796ce7cd7dcd2631c443cec (diff)
downloadopenwar-4f65043e40dbaf8e4cc290327e34270645dedce3.tar.gz
openwar-4f65043e40dbaf8e4cc290327e34270645dedce3.tar.bz2
openwar-4f65043e40dbaf8e4cc290327e34270645dedce3.zip
Fixes differences in between input buffers in different parts.
Now opens files in binary mode, and added some conveniance functions to read parts of the binary file.
Diffstat (limited to 'disasm')
-rw-r--r--disasm/CMakeLists.txt2
-rw-r--r--disasm/disasm.cpp8
-rw-r--r--disasm/dumpobject.cpp26
-rw-r--r--disasm/dumpobject.hpp2
-rw-r--r--disasm/extractfunction.cpp50
-rw-r--r--disasm/extractfunction.hpp2
-rw-r--r--disasm/leparseutil.cpp31
-rw-r--r--disasm/leparseutil.hpp11
-rw-r--r--disasm/parsefileandrewind.cpp11
-rw-r--r--disasm/parsefileandrewind.hpp7
10 files changed, 88 insertions, 62 deletions
diff --git a/disasm/CMakeLists.txt b/disasm/CMakeLists.txt
index 7c21409..cd5113c 100644
--- a/disasm/CMakeLists.txt
+++ b/disasm/CMakeLists.txt
@@ -1,7 +1,7 @@
add_executable(disasm
disasm.cpp
- parsefileandrewind.hpp parsefileandrewind.cpp
+ leparseutil.hpp leparseutil.cpp
dumpobject.hpp dumpobject.cpp
extractfunction.hpp extractfunction.cpp
)
diff --git a/disasm/disasm.cpp b/disasm/disasm.cpp
index d5b1333..a38a71d 100644
--- a/disasm/disasm.cpp
+++ b/disasm/disasm.cpp
@@ -155,9 +155,7 @@ int parse_dumpobject_options(std::vector<std::string> arguments) {
return ret;
}
- std::ifstream file_stream(file_path.string());
-
- dump_object(file_stream, object_id);
+ dump_object(file_path.string(), object_id);
}
int parse_extractfunction_options(std::vector<std::string> arguments) {
@@ -199,7 +197,5 @@ int parse_extractfunction_options(std::vector<std::string> arguments) {
return ret;
}
- std::ifstream file_stream(file_path.string());
-
- extract_function(file_stream, object_id, function_offset);
+ extract_function(file_path.string(), object_id, function_offset);
}
diff --git a/disasm/dumpobject.cpp b/disasm/dumpobject.cpp
index 4f1a5cc..ae09b12 100644
--- a/disasm/dumpobject.cpp
+++ b/disasm/dumpobject.cpp
@@ -1,31 +1,27 @@
#include "dumpobject.hpp"
-#include "parsefileandrewind.hpp"
-
-#include "le_file.hpp"
+#include "leparseutil.hpp"
#include <distorm.h>
#include <iomanip>
-void dump_object(std::ifstream& ifs, binparse::Value32 object_id)
-{
- auto file = parse_file_and_rewind(ifs);
-
- std::vector<uint8_t> code(std::istreambuf_iterator<char>(ifs), {});
+void dump_object(std::string file_name, binparse::Value32 object_id)
+{
+ std::ifstream ifs(file_name, std::ios::binary);
+ ifs.unsetf(std::ios::skipws);
+ auto file = parse_file(ifs);
+
std::vector<_DecodedInst> instructions;
instructions.resize(100000);
unsigned int read_inst;
-
- auto object = file.object_table.entries[object_id];
- auto index = object.page_table_index;
- binparse::Offset32 offset = file.le_header.data_page_offset;
+ std::vector<uint8_t> code = read_object(ifs, file, object_id);
auto result = distorm_decode64(
- object.reloc_base_address,
- code.data() + offset + (index - 1) * file.le_header.page_size,
- file.object_table.entries[1].nr_page_table_entries * file.le_header.page_size,
+ file.object_table.entries[object_id].reloc_base_address,
+ code.data(),
+ code.size(),
Decode32Bits,
instructions.data(),
instructions.size(),
diff --git a/disasm/dumpobject.hpp b/disasm/dumpobject.hpp
index 8685588..94c7e34 100644
--- a/disasm/dumpobject.hpp
+++ b/disasm/dumpobject.hpp
@@ -5,4 +5,4 @@
#include <fstream>
#include <cstdint>
-void dump_object(std::ifstream& ifs, binparse::Value32 object_id); \ No newline at end of file
+void dump_object(std::string file_name, binparse::Value32 object_id); \ No newline at end of file
diff --git a/disasm/extractfunction.cpp b/disasm/extractfunction.cpp
index 6d6f7bc..4fb675c 100644
--- a/disasm/extractfunction.cpp
+++ b/disasm/extractfunction.cpp
@@ -1,39 +1,49 @@
#include "extractfunction.hpp"
-#include "parsefileandrewind.hpp"
+#include "leparseutil.hpp"
#include <distorm.h>
#include <iomanip>
+#include <fstream>
-void extract_function(std::istream& is, binparse::Value32 object_id, binparse::Offset32 function_offset) {
-
- auto file = parse_file_and_rewind(is);
+void extract_function(std::string file_path, binparse::Value32 object_id, binparse::Offset32 function_offset) {
+
+ std::ifstream ifs(file_path, std::ios::binary);
+ ifs.unsetf(std::ios::skipws);
- is.ignore(file.le_header.data_page_offset);
+ auto file = parse_file(ifs);
- _DInst decodedInstructions[1];
+ _DInst decinst;
_DecodeType dt = Decode32Bits;
unsigned int decodedInstructionsCount = 0;
- auto object = file.object_table.entries[object_id];
- unsigned int object_size = object.nr_page_table_entries * file.le_header.page_size;
+ std::vector<uint8_t> code_buf = read_object(ifs, file, object_id);
- std::vector<uint8_t> code_buf;
- code_buf.reserve(object_size);
- std::copy_n(std::istream_iterator<uint8_t>(is), object_size, std::back_inserter(code_buf));
+ binparse::Offset32 base_reloc_offset = file.object_table.entries[object_id].reloc_base_address;
+ binparse::Offset32 buffer_offset = binparse::Offset32(function_offset - base_reloc_offset);
_CodeInfo ci;
- ci.code = code_buf.data() + function_offset;
- ci.codeLen = code_buf.size() - function_offset;
- ci.codeOffset = object.reloc_base_address + function_offset;
+ ci.code = code_buf.data() + buffer_offset;
+ ci.codeLen = code_buf.size() - buffer_offset;
+ ci.codeOffset = base_reloc_offset + buffer_offset;
ci.dt = dt;
ci.features = DF_NONE;
- distorm_decompose64(&ci, decodedInstructions, 1, &decodedInstructionsCount);
-
- _DecodedInst inst;
- distorm_format64(&ci, &decodedInstructions[0], &inst);
-
- std::cout << std::hex << std::setw(8) << std::setfill('0') << inst.offset << ":\t" << inst.mnemonic.p << " " << inst.operands.p << std::endl;
+ while(true) {
+ distorm_decompose64(&ci, &decinst, 1, &decodedInstructionsCount);
+
+ if(decinst.flags == FLAG_NOT_DECODABLE) {
+ break;
+ }
+
+ _DecodedInst inst;
+ distorm_format64(&ci, &decinst, &inst);
+
+ std::cout << std::hex << std::setw(8) << std::setfill('0') << inst.offset << ":\t" << inst.mnemonic.p << " " << inst.operands.p << std::endl;
+
+ ci.code += decinst.size;
+ ci.codeLen -= decinst.size;
+ ci.codeOffset += decinst.size;
+ }
}
diff --git a/disasm/extractfunction.hpp b/disasm/extractfunction.hpp
index 57e0597..0b3099b 100644
--- a/disasm/extractfunction.hpp
+++ b/disasm/extractfunction.hpp
@@ -3,4 +3,4 @@
#include "le_file.hpp"
#include "types.hpp"
-void extract_function(std::istream& is, binparse::Value32 object_id, binparse::Offset32 function_offset);
+void extract_function(std::string file_path, binparse::Value32 object_id, binparse::Offset32 function_offset);
diff --git a/disasm/leparseutil.cpp b/disasm/leparseutil.cpp
new file mode 100644
index 0000000..9c4e790
--- /dev/null
+++ b/disasm/leparseutil.cpp
@@ -0,0 +1,31 @@
+#include "leparseutil.hpp"
+
+std::vector<uint8_t> read_file_part(std::istream& is, std::streamsize offset, size_t length) {
+ is.clear();
+ is.seekg(0, std::ios::beg);
+
+ is.ignore(offset);
+
+ std::vector<uint8_t> vec;
+ vec.reserve(length);
+
+ std::copy_n(std::istream_iterator<uint8_t>(is), length, std::back_inserter(vec));
+ return vec;
+}
+
+std::vector<uint8_t> read_object(std::istream& is, le::File file, binparse::Value32 object_id) {
+ auto object = file.object_table.entries[object_id];
+ auto index = object.page_table_index;
+
+ return read_file_part(is, file.le_header.data_page_offset, file.le_header.page_size * (object.nr_page_table_entries -(index -1)));
+}
+
+le::File parse_file(std::istream& is)
+{
+ is.clear();
+ is.seekg(0, std::ios::beg);
+
+ auto file = le::parse_file(is);
+
+ return file;
+}
diff --git a/disasm/leparseutil.hpp b/disasm/leparseutil.hpp
new file mode 100644
index 0000000..d44bc18
--- /dev/null
+++ b/disasm/leparseutil.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "le_file.hpp"
+
+#include <istream>
+
+le::File parse_file(std::istream& is);
+
+std::vector<uint8_t> read_file_part(std::istream& is, std::streamsize offset, size_t length);
+
+std::vector<uint8_t> read_object(std::istream& is, le::File file, binparse::Value32 object_id); \ No newline at end of file
diff --git a/disasm/parsefileandrewind.cpp b/disasm/parsefileandrewind.cpp
deleted file mode 100644
index 704e9da..0000000
--- a/disasm/parsefileandrewind.cpp
+++ /dev/null
@@ -1,11 +0,0 @@
-#include "parsefileandrewind.hpp"
-
-le::File parse_file_and_rewind(std::istream& is)
-{
- auto file = le::parse_file(is);
-
- is.clear();
- is.seekg(0, std::ios::beg);
-
- return file;
-}
diff --git a/disasm/parsefileandrewind.hpp b/disasm/parsefileandrewind.hpp
deleted file mode 100644
index 4d564ac..0000000
--- a/disasm/parsefileandrewind.hpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-
-#include "le_file.hpp"
-
-#include <istream>
-
-le::File parse_file_and_rewind(std::istream& is);