Skip to content

Commit 102c3b8

Browse files
committed
Add flag role for carry flag when subtraction is implemented with addition
1 parent 3eb83fb commit 102c3b8

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
lines changed

arch/arm64/arch_arm64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ class Arm64Architecture : public Architecture
15111511
case IL_FLAG_Z:
15121512
return ZeroFlagRole;
15131513
case IL_FLAG_C:
1514-
return CarryFlagRole;
1514+
return CarryFlagWithInvertedSubtractRole;
15151515
case IL_FLAG_V:
15161516
return OverflowFlagRole;
15171517
default:

arch/armv7/arch_armv7.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ BNFlagRole ArmCommonArchitecture::GetFlagRole(uint32_t flag, uint32_t)
17341734
case IL_FLAG_Z:
17351735
return ZeroFlagRole;
17361736
case IL_FLAG_C:
1737-
return CarryFlagRole;
1737+
return CarryFlagWithInvertedSubtractRole;
17381738
case IL_FLAG_V:
17391739
return OverflowFlagRole;
17401740
default:

binaryninjacore.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 72
40+
#define BN_CURRENT_CORE_ABI_VERSION 73
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
@@ -671,7 +671,8 @@ extern "C"
671671
EvenParityFlagRole = 7,
672672
OddParityFlagRole = 8,
673673
OrderedFlagRole = 9,
674-
UnorderedFlagRole = 10
674+
UnorderedFlagRole = 10,
675+
CarryFlagWithInvertedSubtractRole = 11,
675676
} BNFlagRole;
676677

677678
typedef enum BNFunctionGraphType

python/examples/nes.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class M6502(Architecture):
8787
flags = [c_flag, z_flag, i_flag, d_flag, b_flag, v_flag, s_flag]
8888
flag_write_types = [FlagWriteTypeName("*"), FlagWriteTypeName("czs"), FlagWriteTypeName("zvs"),FlagWriteTypeName( "zs")]
8989
flag_roles = {
90-
c_flag: FlagRole.SpecialFlagRole, # Not a normal carry flag, subtract result is inverted
90+
c_flag: FlagRole.CarryFlagWithInvertedSubtractRole,
9191
z_flag: FlagRole.ZeroFlagRole, v_flag: FlagRole.OverflowFlagRole, s_flag: FlagRole.NegativeSignFlagRole
9292
}
9393
flags_required_for_flag_condition = {
@@ -104,8 +104,8 @@ class M6502(Architecture):
104104
Mnemonic("asl"):lambda il, operand: il.store(1, operand, il.shift_left(1, il.load(1, operand), il.const(1, 1), flags=FlagName("czs"))),
105105
Mnemonic("asl@"):lambda il, operand: il.set_reg(1, a_reg, il.shift_left(1, operand, il.const(1, 1), flags=FlagName("czs"))),
106106
Mnemonic("and"):lambda il, operand: il.set_reg(1, a_reg, il.and_expr(1, il.reg(1, a_reg), operand, flags=FlagName("zs"))),
107-
Mnemonic("bcc"):lambda il, operand: M6502.cond_branch(il, il.flag_condition(LowLevelILFlagCondition.LLFC_UGE), operand),
108-
Mnemonic("bcs"):lambda il, operand: M6502.cond_branch(il, il.flag_condition(LowLevelILFlagCondition.LLFC_ULT), operand),
107+
Mnemonic("bcc"):lambda il, operand: M6502.cond_branch(il, il.flag_condition(LowLevelILFlagCondition.LLFC_ULT), operand),
108+
Mnemonic("bcs"):lambda il, operand: M6502.cond_branch(il, il.flag_condition(LowLevelILFlagCondition.LLFC_UGE), operand),
109109
Mnemonic("beq"):lambda il, operand: M6502.cond_branch(il, il.flag_condition(LowLevelILFlagCondition.LLFC_E), operand),
110110
Mnemonic("bit"):lambda il, operand: il.and_expr(1, il.reg(1, a_reg), operand, flags=FlagName("czs")),
111111
Mnemonic("bmi"):lambda il, operand: M6502.cond_branch(il, il.flag_condition(LowLevelILFlagCondition.LLFC_NEG), operand),
@@ -528,15 +528,6 @@ def get_instruction_low_level_il(self, data: bytes, addr: int, il: LowLevelILFun
528528
il.append(i)
529529
return length
530530

531-
def get_flag_write_low_level_il(self, op: LowLevelILOperation, size: int, write_type: Optional[FlagWriteTypeName], flag: FlagType, operands: List[ILRegisterType], il: LowLevelILFunction) -> ExpressionIndex:
532-
if flag == 'c':
533-
if (op == LowLevelILOperation.LLIL_SUB) or (op == LowLevelILOperation.LLIL_SBB):
534-
# Subtraction carry flag is inverted from the commom implementation
535-
return il.not_expr(0, self.get_default_flag_write_low_level_il(op, size, FlagRole.CarryFlagRole, operands, il))
536-
# Other operations use a normal carry flag
537-
return self.get_default_flag_write_low_level_il(op, size, FlagRole.CarryFlagRole, operands, il)
538-
return Architecture.get_flag_write_low_level_il(self, op, size, write_type, flag, operands, il)
539-
540531
def is_never_branch_patch_available(self, data:bytes, addr:int) -> bool:
541532
if (data[0:1] == b"\x10") or (data[0:1] == b"\x30") or (data[0:1] == b"\x50") or (data[0:1] == b"\x70") or (
542533
data[0:1] == b"\x90"
@@ -708,7 +699,7 @@ def perform_is_executable(self) -> bool:
708699
return True
709700

710701
def perform_get_address_size(self) -> int:
711-
return self.address_size
702+
return 2
712703

713704
def perform_get_entry_point(self) -> int:
714705
return struct.unpack("<H", self.read(0xfffc, 2))[0]

0 commit comments

Comments
 (0)