Skip to content

Commit 55fe515

Browse files
Merge pull request #100 from GH0st3rs/master
Add registers to condition output for MIPS
2 parents d691e98 + 808c825 commit 55fe515

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

plasma/lib/arch/mips/output.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
MIPS_INS_SUBU, MIPS_INS_BGTZ, MIPS_INS_LH, MIPS_INS_LHU,
2727
MIPS_INS_SH, MIPS_INS_SD, MIPS_INS_LD, MIPS_GRP_MIPS64,
2828
MIPS_INS_BGEZ, MIPS_INS_BNEZ, MIPS_INS_BEQZ, MIPS_INS_BLEZ,
29-
MIPS_INS_BLTZ, MIPS_REG_ZERO, MIPS_REG_GP, MIPS_INS_NEG)
29+
MIPS_INS_BLTZ, MIPS_REG_ZERO, MIPS_REG_GP, MIPS_INS_NEG,
30+
MIPS_INS_BEQ, MIPS_INS_BNE)
3031

3132
from plasma.lib.output import OutputAbs
3233
from plasma.lib.arch.mips.utils import (inst_symbol, is_call, is_jump, is_ret,
3334
is_uncond_jump, cond_symbol)
3435
from capstone.mips import (MIPS_INS_SLT, MIPS_INS_SLTI, MIPS_INS_SLTIU, MIPS_INS_SLTU,
3536
MIPS_INS_ANDI, MIPS_INS_OR, MIPS_INS_ORI)
3637

37-
# ASSIGNMENT_OPS = {ARM_INS_EOR, ARM_INS_AND, ARM_INS_ORR}
38-
ASSIGNMENT_OPS = {MIPS_INS_SLT, MIPS_INS_SLTI, MIPS_INS_SLTIU, MIPS_INS_SLTU}
38+
ASSIGNMENT_OPS = {MIPS_INS_SLT, MIPS_INS_SLTI, MIPS_INS_SLTIU, MIPS_INS_SLTU, MIPS_INS_BEQ, MIPS_INS_BNE}
3939

4040
LD_TYPE = {
4141
MIPS_INS_LH: "halfword",
@@ -148,16 +148,18 @@ def _if_cond(self, cond, fused_inst):
148148
self._add(" 0")
149149
return
150150

151-
assignment = fused_inst.id in ASSIGNMENT_OPS
151+
assignment = fused_inst.id in ASSIGNMENT_OPS or fused_inst.id in COND_ADD_ZERO
152152

153153
if assignment:
154154
self._add("(")
155-
self._operand(fused_inst, 1)
156-
if cond == MIPS_INS_BNEZ:
157-
self._add(" < ")
155+
self._operand(fused_inst, 0)
156+
self._add(" ")
157+
self._add(cond_symbol(cond))
158+
if cond in COND_ADD_ZERO:
159+
self._add(" 0")
158160
else:
159-
self._add(" >= ")
160-
self._operand(fused_inst, 2)
161+
self._add(" ")
162+
self._operand(fused_inst, 1)
161163
self._add(")")
162164

163165
def _sub_asm_inst(self, i, tab=0):
@@ -265,8 +267,11 @@ def _sub_asm_inst(self, i, tab=0):
265267
if i.id == MIPS_INS_LUI:
266268
self._operand(i, 0)
267269
self._add(" = ")
268-
self._operand(i, 1)
269-
self._add(" << 16")
270+
if str(i.operands[1].value.reg).isdigit:
271+
self._add(" 0x%x" % (i.operands[1].value.reg << 16))
272+
else:
273+
self._operand(i, 1)
274+
self._add(" << 16")
270275

271276
elif i.id == MIPS_INS_MOVE:
272277
self._operand(i, 0)

plasma/lib/arch/mips/process_ast.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@
2121
MIPS_INS_LUI, MIPS_OP_REG, MIPS_REG_ZERO, MipsOpValue)
2222

2323
from plasma.lib.ast import (Ast_Branch, Ast_Loop, Ast_IfGoto, Ast_Ifelse,
24-
Ast_AndIf)
24+
Ast_AndIf, Ast_If_cond)
2525
from plasma.lib.arch.mips.output import ASSIGNMENT_OPS
2626

2727

2828
FUSE_OPS = set(ASSIGNMENT_OPS)
29-
# FUSE_OPS.add(ARM_INS_CMP)
30-
# FUSE_OPS.add(ARM_INS_TST)
3129

3230

3331
def fuse_inst_with_if(ctx, ast):
3432
if isinstance(ast, Ast_Branch):
35-
types_ast = (Ast_Ifelse, Ast_IfGoto, Ast_AndIf)
33+
types_ast = (Ast_Ifelse, Ast_IfGoto, Ast_AndIf, Ast_If_cond)
3634
for i, n in enumerate(ast.nodes):
3735
if isinstance(n, list):
3836
if n[-1].id in FUSE_OPS and i + 1 < len(ast.nodes) \
@@ -43,8 +41,10 @@ def fuse_inst_with_if(ctx, ast):
4341
fuse_inst_with_if(ctx, n)
4442

4543
elif isinstance(ast, Ast_Ifelse):
44+
ast.fused_inst = ast.jump_inst
4645
fuse_inst_with_if(ctx, ast.br_next)
4746
fuse_inst_with_if(ctx, ast.br_next_jump)
4847

4948
elif isinstance(ast, Ast_Loop):
5049
fuse_inst_with_if(ctx, ast.branch)
50+

plasma/lib/fileformat/elf.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,10 @@ def load_dyn_sym(self):
215215
# pyreadelf's assumptions make our own string table
216216
fakestrtabheader = {
217217
"sh_offset": self.__get_offset(self.dtags["DT_STRTAB"]),
218+
"sh_flags": 2048,
218219
}
219220
strtab = StringTableSection(
220-
fakestrtabheader, "strtab_plasma", self.elf.stream)
221+
fakestrtabheader, "strtab_plasma", self.elf)
221222

222223
# ...
223224
# Here in CLE was checked the DT_SONAME
@@ -232,16 +233,16 @@ def load_dyn_sym(self):
232233
fakesymtabheader = {
233234
"sh_offset": self.__get_offset(self.dtags["DT_SYMTAB"]),
234235
"sh_entsize": self.dtags["DT_SYMENT"],
235-
"sh_size": 0
236+
"sh_size": 0,
237+
"sh_flags": 2048,
236238
} # bogus size: no iteration allowed
237239

238240
# ...
239241
# Here in CLE : creation of hash section
240242
# ...
241243

242244
self.dynsym = SymbolTableSection(
243-
fakesymtabheader, "symtab_plasma", self.elf.stream,
244-
self.elf, strtab)
245+
fakesymtabheader, "symtab_plasma", self.elf, strtab)
245246

246247
# mips' relocations are absolutely screwed up, handle some of them here.
247248
self.__relocate_mips()
@@ -276,11 +277,11 @@ def load_dyn_sym(self):
276277
"sh_offset": self.__get_offset(reloffset),
277278
"sh_type": "SHT_" + rela_type,
278279
"sh_entsize": relentsz,
279-
"sh_size": relsz
280+
"sh_size": relsz,
281+
"sh_flags": 2048,
280282
}
281283
reloc_sec = RelocationSection(
282-
fakerelheader, "reloc_plasma",
283-
self.elf.stream, self.elf)
284+
fakerelheader, "reloc_plasma", self.elf)
284285
self.__register_relocs(reloc_sec)
285286

286287
# try to parse relocations out of a table of type DT_JMPREL

0 commit comments

Comments
 (0)