Skip to content

Commit

Permalink
working x86
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio committed Aug 8, 2018
1 parent df2538f commit fb71f58
Show file tree
Hide file tree
Showing 14 changed files with 1,822 additions and 445 deletions.
1,064 changes: 719 additions & 345 deletions libdec/arch/x86.js

Large diffs are not rendered by default.

697 changes: 697 additions & 0 deletions libdec/arch/x86.old.js

Large diffs are not rendered by default.

33 changes: 23 additions & 10 deletions libdec/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,38 @@
*/

module.exports = (function() {
var Printer = require('libdec/printer');

return function() {
// theme (requires to be initialized after evars)
this.printer = new Printer();
// ident for print
this.identAsm = '';
this.identAsmSet = function(size) {
// size = 0x + addr + space + asm + space
size += 12;
while (this.identAsm.length < size) {
this.identAsm += ' ';
}
};
this.ident = '';
this.identIn = function() {
this.ident += ' ';
};
this.identOut = function(force) {
this.ident = this.ident.substr(4, this.ident.lenght);
this.ident = this.ident.substr(4, this.ident.length);
};
this.identfy = function(s, p) {
var h = Global.printer.html;
if (Global.evars.honor.assembly) {
p = p || '';
s = s || 0;
return h(' ') + p + this.identAsm.substring(s, this.identAsm.length) + h(' | ') + h(this.ident)
}
return h(this.ident)
};

// stack for instructions..
this.scope = [];
this.stack = [];
this.local = function() {
var n = this.scope[this.scope.lenght - 1];
var n = this.scope[this.scope.length - 1];
return this.stack.slice(this.stack.length - n, this.stack.length);
};
this.pushLocal = function() {
Expand All @@ -47,17 +60,17 @@ module.exports = (function() {
}
};
this.push = function(x) {
if (this.scope.lenght < 1) {
if (this.scope.length < 1) {
throw new Error("Bad context stack (push with zero)")
}
this.scope[this.scope.lenght - 1]++;
this.scope[this.scope.length - 1]++;
this.stack.push(x);
};
this.pop = function() {
if (this.scope.lenght < 1 || this.scope[this.scope.lenght - 1] == 0) {
if (this.scope.length < 1 || this.scope[this.scope.length - 1] == 0) {
throw new Error("Bad context stack (pop with zero)")
}
this.scope[this.scope.lenght - 1]--;
this.scope[this.scope.length - 1]--;
return this.stack.pop();
};
};
Expand Down
47 changes: 30 additions & 17 deletions libdec/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@ module.exports = (function() {
var Base = require('libdec/core/base');
var Block = require('libdec/core/block');
var Scope = require('libdec/core/scope');
var Strings = require('libdec/core/strings');
var Instruction = require('libdec/core/instruction');

var _post_analysis = function(data, arch) {};
var _pre_analysis = function(data, arch) {};
var _decompile = function(data, arch) {
var _post_analysis = function(data, arch, arch_context) {
if (arch.custom_end) {
arch.custom_end(data.instructions, arch_context)
}
};

var _pre_analysis = function(data, arch, arch_context) {
if (arch.custom_start) {
arch.custom_start(data.instructions, arch_context)
}
};
var _decompile = function(data, arch, arch_context) {
var instructions = data.blocks[0].instructions;
for (var i = 0; i < instructions.length; i++) {
instr = instructions[i];
fcn = arch.instructions[instr.parsed.memn];
if (fcn) {
instr.code = fcn(instr, instructions);
} else {
instr.code = new Base.unknown(instr.simplified)
}
var instr = instructions[i];
var fcn = arch.instructions[instr.parsed.mnem];
instr.code = fcn ? fcn(instr, arch_context, instructions) : new Base.unknown(instr.assembly)
}
};
var _print = function(data) {
Expand All @@ -41,22 +47,29 @@ module.exports = (function() {

var _prepare = function(data, arch) {
this.blocks = [new Block()];
data.graph[i]
var instructions = [];
this.instructions = [];
var strings = new Strings(data.xrefs.strings)
var max_length = 0;
for (var i = 0; i < data.graph[0].blocks.length; i++) {
var block = data.graph[0].blocks[i];
instructions = instructions.concat(block.ops.map(function(b) {
return new Instruction(b, arch);
this.instructions = this.instructions.concat(block.ops.map(function(b) {
if (max_length < b.opcode.length) {
max_length = b.opcode.length
}
var ins = new Instruction(b, arch);
ins.strings = strings.search(ins.pointer);
return ins;
}));
}
this.blocks[0].extra.push(new Scope.routine(instructions[0].location, {
Global.context.identAsmSet(max_length);
this.blocks[0].extra.push(new Scope.routine(this.instructions[0].location, {
returns: 'void',
name: data.graph[0].name,
args: [],
locals: []
}));
this.blocks[0].extra.push(new Scope.brace(instructions[instructions.length - 1].location));
this.blocks[0].instructions = instructions;
this.blocks[0].extra.push(new Scope.brace(this.instructions[this.instructions.length - 1].location));
this.blocks[0].instructions = this.instructions.splice();

this.print = function() {
for (var i = 0; i < this.blocks.length; i++) {
Expand Down
93 changes: 85 additions & 8 deletions libdec/core/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
*/

module.exports = (function() {
var _internal_variable_cnt = 0;
var Variable = require('libdec/core/variable');
var Extra = require('libdec/core/extra');

var _generic_asm = function(asm) {
this.asm = asm;
this.toString = function() {
var t = context.printer.theme;
var a = context.printer.auto;
var t = Global.printer.theme;
var a = Global.printer.auto;
return t.callname('__asm') + ' (' + a(this.asm) + ')';
}
};

var _assignment = function(destination, source) {
var _generic_assignment = function(destination, source) {
this.destination = destination;
this.source = source;
this.toString = function() {
Expand All @@ -43,7 +44,7 @@ module.exports = (function() {
this.source = source;
this.cast = cast;
this.toString = function() {
var t = context.printer.theme;
var t = Global.printer.theme;
return this.destination + ' = (' + t.types(this.cast) + ') ' + this.source;
};
};
Expand All @@ -69,7 +70,74 @@ module.exports = (function() {
};
};

var _generic_memory = function(bits, is_signed, pointer, register, is_write) {
if (is_write) {
this.toString = function() {
return this.pointer + ' = ' + this.reg;
};
} else {
this.toString = function() {
return this.reg + ' = ' + this.pointer;
};
}
};

var _generic_call = function(function_name, arguments) {
this.function_name = function_name;
this.arguments = arguments || [];
this.toString = function() {
var fcn = this.function_name;
if (Extra.is.string(fcn)) {
fcn = Global.printer.theme.callname(fcn);
}
return fcn + ' (' + this.arguments.join(', ') + ')';
};
};

var _genric_return = function(value) {
this.value = value;
this.toString = function(options) {
var r = Global.printer.theme.flow('return');
if (this.value) {
r += ' ' + this.value;
}
return r;
};
};

var _base = {
/* COMMON */
assign: function(destination, source) {
return new _generic_assignment(destination, source);
},
cast: function(destination, source, cast) {
return new _cast_register(destination, source, cast);
},
nop: function(asm) {
return null;
},
/* JUMPS */
call: function(function_name, function_arguments) {
return new _generic_call(function_name, function_arguments);
},
return: function(value) {
return new _genric_return(value);
},
/* BRANCHES */

/* MATH */
increase: function(destination, source) {
if (source == '1') {
return new _generic_inc_dec(destination, '++');
}
return new _generic_math(destination, destination, source, '+');
},
decrease: function(destination, source) {
if (source == '1') {
return new _generic_inc_dec(destination, '--');
}
return new _generic_math(destination, destination, source, '-');
},
add: function(destination, source_a, source_b) {
if (destination == source_a && source_b == '1') {
return new _generic_inc_dec(destination, '++');
Expand All @@ -82,9 +150,6 @@ module.exports = (function() {
}
return new _generic_math(destination, source_a, source_b, '^');
},
assign: function(destination, source) {
return new _assignment(destination, source);
},
subtract: function(destination, source_a, source_b) {
if (destination == source_a && source_b == '1') {
return new _generic_inc_dec(destination, '++');
Expand All @@ -97,6 +162,18 @@ module.exports = (function() {
}
return new _generic_math(destination, source_a, source_b, '^');
},
/* MEMORY */
read_memory: function(pointer, register, bits, is_signed) {
var value = (Extra.is.string(register) || Extra.is.number(register)) ? register : Variable.variable(register, Extra.to.type(bits, is_signed));
var pointer = (Extra.is.string(pointer) || Extra.is.number(pointer)) ? pointer : Variable.memory(pointer, Extra.to.type(bits, is_signed));
return new _generic_assignment(value, pointer);
},
write_memory: function(pointer, register, bits, is_signed) {
var value = (Extra.is.string(register) || Extra.is.number(register)) ? register : Variable.variable(register, Extra.to.type(bits, is_signed));
var pointer = (Extra.is.string(pointer) || Extra.is.number(pointer)) ? pointer : Variable.memory(pointer, Extra.to.type(bits, is_signed));
return new _generic_assignment(pointer, value);
},
/* UNKNOWN */
unknown: function(asm) {
return new _generic_asm(asm);
}
Expand Down
74 changes: 74 additions & 0 deletions libdec/core/extra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2018, Giovanni Dante Grazioli <deroad@libero.it>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/


module.exports = (function() {
var _standard_types = {
'void': 0,
'char': 8,
'short': 16,
'long': 32,
'float': 32,
'long long': 64,
'double': 64,
};

var _is = {
string: function(s) {
return typeof s == 'string';
},
number: function(s) {
return typeof s == 'number';
},
array: function(s) {
return Array.isArray(s);
},
};

var _to = {
type: function(bits, signed) {
if (bits == 0) {
return 'void';
}
return (signed ? 'int' : 'uint') + bits + '_t';
},
bits: function(type) {
if (_standard_types[type]) {
return _standard_types[type];
}
if (type == 'int') {
var bits = Global.evars.archbits;
return bits < 32 ? 16 : 32;
}
return parseInt(type.replace(/[intu_]/g, ''));
}
};

return {
is: _is,
to: _to
};
})();

0 comments on commit fb71f58

Please sign in to comment.