smallworld-re 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. smallworld/__init__.py +35 -0
  2. smallworld/analyses/__init__.py +14 -0
  3. smallworld/analyses/analysis.py +88 -0
  4. smallworld/analyses/code_coverage.py +31 -0
  5. smallworld/analyses/colorizer.py +682 -0
  6. smallworld/analyses/colorizer_summary.py +100 -0
  7. smallworld/analyses/field_detection/__init__.py +14 -0
  8. smallworld/analyses/field_detection/field_analysis.py +536 -0
  9. smallworld/analyses/field_detection/guards.py +26 -0
  10. smallworld/analyses/field_detection/hints.py +133 -0
  11. smallworld/analyses/field_detection/malloc.py +211 -0
  12. smallworld/analyses/forced_exec/__init__.py +3 -0
  13. smallworld/analyses/forced_exec/forced_exec.py +87 -0
  14. smallworld/analyses/underlays/__init__.py +4 -0
  15. smallworld/analyses/underlays/basic.py +13 -0
  16. smallworld/analyses/underlays/underlay.py +31 -0
  17. smallworld/analyses/unstable/__init__.py +4 -0
  18. smallworld/analyses/unstable/angr/__init__.py +0 -0
  19. smallworld/analyses/unstable/angr/base.py +12 -0
  20. smallworld/analyses/unstable/angr/divergence.py +274 -0
  21. smallworld/analyses/unstable/angr/model.py +383 -0
  22. smallworld/analyses/unstable/angr/nwbt.py +63 -0
  23. smallworld/analyses/unstable/angr/typedefs.py +170 -0
  24. smallworld/analyses/unstable/angr/utils.py +25 -0
  25. smallworld/analyses/unstable/angr/visitor.py +315 -0
  26. smallworld/analyses/unstable/angr_nwbt.py +106 -0
  27. smallworld/analyses/unstable/code_coverage.py +54 -0
  28. smallworld/analyses/unstable/code_reachable.py +44 -0
  29. smallworld/analyses/unstable/control_flow_tracer.py +71 -0
  30. smallworld/analyses/unstable/pointer_finder.py +90 -0
  31. smallworld/arch/__init__.py +0 -0
  32. smallworld/arch/aarch64_arch.py +286 -0
  33. smallworld/arch/amd64_arch.py +86 -0
  34. smallworld/arch/i386_arch.py +44 -0
  35. smallworld/emulators/__init__.py +14 -0
  36. smallworld/emulators/angr/__init__.py +7 -0
  37. smallworld/emulators/angr/angr.py +1652 -0
  38. smallworld/emulators/angr/default.py +15 -0
  39. smallworld/emulators/angr/exceptions.py +7 -0
  40. smallworld/emulators/angr/exploration/__init__.py +9 -0
  41. smallworld/emulators/angr/exploration/bounds.py +27 -0
  42. smallworld/emulators/angr/exploration/default.py +17 -0
  43. smallworld/emulators/angr/exploration/terminate.py +22 -0
  44. smallworld/emulators/angr/factory.py +55 -0
  45. smallworld/emulators/angr/machdefs/__init__.py +35 -0
  46. smallworld/emulators/angr/machdefs/aarch64.py +292 -0
  47. smallworld/emulators/angr/machdefs/amd64.py +192 -0
  48. smallworld/emulators/angr/machdefs/arm.py +387 -0
  49. smallworld/emulators/angr/machdefs/i386.py +221 -0
  50. smallworld/emulators/angr/machdefs/machdef.py +138 -0
  51. smallworld/emulators/angr/machdefs/mips.py +184 -0
  52. smallworld/emulators/angr/machdefs/mips64.py +189 -0
  53. smallworld/emulators/angr/machdefs/ppc.py +101 -0
  54. smallworld/emulators/angr/machdefs/riscv.py +261 -0
  55. smallworld/emulators/angr/machdefs/xtensa.py +255 -0
  56. smallworld/emulators/angr/memory/__init__.py +7 -0
  57. smallworld/emulators/angr/memory/default.py +10 -0
  58. smallworld/emulators/angr/memory/fixups.py +43 -0
  59. smallworld/emulators/angr/memory/memtrack.py +105 -0
  60. smallworld/emulators/angr/scratch.py +43 -0
  61. smallworld/emulators/angr/simos.py +53 -0
  62. smallworld/emulators/angr/utils.py +70 -0
  63. smallworld/emulators/emulator.py +1013 -0
  64. smallworld/emulators/hookable.py +252 -0
  65. smallworld/emulators/panda/__init__.py +5 -0
  66. smallworld/emulators/panda/machdefs/__init__.py +28 -0
  67. smallworld/emulators/panda/machdefs/aarch64.py +93 -0
  68. smallworld/emulators/panda/machdefs/amd64.py +71 -0
  69. smallworld/emulators/panda/machdefs/arm.py +89 -0
  70. smallworld/emulators/panda/machdefs/i386.py +36 -0
  71. smallworld/emulators/panda/machdefs/machdef.py +86 -0
  72. smallworld/emulators/panda/machdefs/mips.py +94 -0
  73. smallworld/emulators/panda/machdefs/mips64.py +91 -0
  74. smallworld/emulators/panda/machdefs/ppc.py +79 -0
  75. smallworld/emulators/panda/panda.py +575 -0
  76. smallworld/emulators/unicorn/__init__.py +13 -0
  77. smallworld/emulators/unicorn/machdefs/__init__.py +28 -0
  78. smallworld/emulators/unicorn/machdefs/aarch64.py +310 -0
  79. smallworld/emulators/unicorn/machdefs/amd64.py +326 -0
  80. smallworld/emulators/unicorn/machdefs/arm.py +321 -0
  81. smallworld/emulators/unicorn/machdefs/i386.py +137 -0
  82. smallworld/emulators/unicorn/machdefs/machdef.py +117 -0
  83. smallworld/emulators/unicorn/machdefs/mips.py +202 -0
  84. smallworld/emulators/unicorn/unicorn.py +684 -0
  85. smallworld/exceptions/__init__.py +5 -0
  86. smallworld/exceptions/exceptions.py +85 -0
  87. smallworld/exceptions/unstable/__init__.py +1 -0
  88. smallworld/exceptions/unstable/exceptions.py +25 -0
  89. smallworld/extern/__init__.py +4 -0
  90. smallworld/extern/ctypes.py +94 -0
  91. smallworld/extern/unstable/__init__.py +1 -0
  92. smallworld/extern/unstable/ghidra.py +129 -0
  93. smallworld/helpers.py +107 -0
  94. smallworld/hinting/__init__.py +8 -0
  95. smallworld/hinting/hinting.py +214 -0
  96. smallworld/hinting/hints.py +427 -0
  97. smallworld/hinting/unstable/__init__.py +2 -0
  98. smallworld/hinting/utils.py +19 -0
  99. smallworld/instructions/__init__.py +18 -0
  100. smallworld/instructions/aarch64.py +20 -0
  101. smallworld/instructions/arm.py +18 -0
  102. smallworld/instructions/bsid.py +67 -0
  103. smallworld/instructions/instructions.py +258 -0
  104. smallworld/instructions/mips.py +21 -0
  105. smallworld/instructions/x86.py +100 -0
  106. smallworld/logging.py +90 -0
  107. smallworld/platforms.py +95 -0
  108. smallworld/py.typed +0 -0
  109. smallworld/state/__init__.py +6 -0
  110. smallworld/state/cpus/__init__.py +32 -0
  111. smallworld/state/cpus/aarch64.py +563 -0
  112. smallworld/state/cpus/amd64.py +676 -0
  113. smallworld/state/cpus/arm.py +630 -0
  114. smallworld/state/cpus/cpu.py +71 -0
  115. smallworld/state/cpus/i386.py +239 -0
  116. smallworld/state/cpus/mips.py +374 -0
  117. smallworld/state/cpus/mips64.py +372 -0
  118. smallworld/state/cpus/powerpc.py +229 -0
  119. smallworld/state/cpus/riscv.py +357 -0
  120. smallworld/state/cpus/xtensa.py +80 -0
  121. smallworld/state/memory/__init__.py +7 -0
  122. smallworld/state/memory/code.py +70 -0
  123. smallworld/state/memory/elf/__init__.py +3 -0
  124. smallworld/state/memory/elf/elf.py +564 -0
  125. smallworld/state/memory/elf/rela/__init__.py +32 -0
  126. smallworld/state/memory/elf/rela/aarch64.py +27 -0
  127. smallworld/state/memory/elf/rela/amd64.py +32 -0
  128. smallworld/state/memory/elf/rela/arm.py +51 -0
  129. smallworld/state/memory/elf/rela/i386.py +32 -0
  130. smallworld/state/memory/elf/rela/mips.py +45 -0
  131. smallworld/state/memory/elf/rela/ppc.py +45 -0
  132. smallworld/state/memory/elf/rela/rela.py +63 -0
  133. smallworld/state/memory/elf/rela/riscv64.py +27 -0
  134. smallworld/state/memory/elf/rela/xtensa.py +15 -0
  135. smallworld/state/memory/elf/structs.py +55 -0
  136. smallworld/state/memory/heap.py +85 -0
  137. smallworld/state/memory/memory.py +181 -0
  138. smallworld/state/memory/stack/__init__.py +31 -0
  139. smallworld/state/memory/stack/aarch64.py +22 -0
  140. smallworld/state/memory/stack/amd64.py +42 -0
  141. smallworld/state/memory/stack/arm.py +66 -0
  142. smallworld/state/memory/stack/i386.py +22 -0
  143. smallworld/state/memory/stack/mips.py +34 -0
  144. smallworld/state/memory/stack/mips64.py +34 -0
  145. smallworld/state/memory/stack/ppc.py +34 -0
  146. smallworld/state/memory/stack/riscv.py +22 -0
  147. smallworld/state/memory/stack/stack.py +127 -0
  148. smallworld/state/memory/stack/xtensa.py +34 -0
  149. smallworld/state/models/__init__.py +6 -0
  150. smallworld/state/models/mmio.py +186 -0
  151. smallworld/state/models/model.py +163 -0
  152. smallworld/state/models/posix.py +455 -0
  153. smallworld/state/models/x86/__init__.py +2 -0
  154. smallworld/state/models/x86/microsoftcdecl.py +35 -0
  155. smallworld/state/models/x86/systemv.py +240 -0
  156. smallworld/state/state.py +962 -0
  157. smallworld/state/unstable/__init__.py +0 -0
  158. smallworld/state/unstable/elf.py +393 -0
  159. smallworld/state/x86_registers.py +30 -0
  160. smallworld/utils.py +935 -0
  161. smallworld_re-1.0.0.dist-info/LICENSE.txt +21 -0
  162. smallworld_re-1.0.0.dist-info/METADATA +189 -0
  163. smallworld_re-1.0.0.dist-info/RECORD +166 -0
  164. smallworld_re-1.0.0.dist-info/WHEEL +5 -0
  165. smallworld_re-1.0.0.dist-info/entry_points.txt +2 -0
  166. smallworld_re-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,321 @@
1
+ import capstone
2
+ import unicorn
3
+
4
+ from ....platforms import Architecture, Byteorder
5
+ from .machdef import UnicornMachineDef
6
+
7
+
8
+ class ARMMachineDef(UnicornMachineDef):
9
+ """Base Unicorn machine definition for 32-bit ARM"""
10
+
11
+ uc_arch = unicorn.UC_ARCH_ARM
12
+ uc_mode = unicorn.UC_MODE_ARM
13
+
14
+ cs_arch = capstone.CS_ARCH_ARM
15
+ cs_mode = capstone.CS_MODE_ARM
16
+
17
+ pc_reg = "pc"
18
+
19
+ def __init__(self):
20
+ self._registers = {
21
+ "r0": (unicorn.arm_const.UC_ARM_REG_R0, "r0", 0, 4),
22
+ "r1": (unicorn.arm_const.UC_ARM_REG_R1, "r1", 0, 4),
23
+ "r2": (unicorn.arm_const.UC_ARM_REG_R2, "r2", 0, 4),
24
+ "r3": (unicorn.arm_const.UC_ARM_REG_R3, "r3", 0, 4),
25
+ "r4": (unicorn.arm_const.UC_ARM_REG_R4, "r4", 0, 4),
26
+ "r5": (unicorn.arm_const.UC_ARM_REG_R5, "r5", 0, 4),
27
+ "r6": (unicorn.arm_const.UC_ARM_REG_R6, "r6", 0, 4),
28
+ "r7": (unicorn.arm_const.UC_ARM_REG_R7, "r7", 0, 4),
29
+ "r8": (unicorn.arm_const.UC_ARM_REG_R8, "r8", 0, 4),
30
+ # r9 doubles as the Static base pointer
31
+ "r9": (unicorn.arm_const.UC_ARM_REG_R9, "r9", 0, 4),
32
+ "sb": (unicorn.arm_const.UC_ARM_REG_SB, "r9", 0, 4),
33
+ # r10 doubles as the Stack Limit pointer
34
+ "r10": (unicorn.arm_const.UC_ARM_REG_R10, "r10", 0, 4),
35
+ "sl": (unicorn.arm_const.UC_ARM_REG_SL, "r10", 0, 4),
36
+ # r11 doubles as the Frame Pointer, if desired.
37
+ "r11": (unicorn.arm_const.UC_ARM_REG_R11, "r11", 0, 4),
38
+ "fp": (unicorn.arm_const.UC_ARM_REG_FP, "r11", 0, 4),
39
+ # r12 doubles as the Intra-call scratch register
40
+ "r12": (unicorn.arm_const.UC_ARM_REG_R12, "r12", 0, 4),
41
+ "ip": (unicorn.arm_const.UC_ARM_REG_IP, "r12", 0, 4),
42
+ "sp": (unicorn.arm_const.UC_ARM_REG_SP, "sp", 0, 4),
43
+ "lr": (unicorn.arm_const.UC_ARM_REG_LR, "lr", 0, 4),
44
+ "pc": (unicorn.arm_const.UC_ARM_REG_PC, "pc", 0, 4),
45
+ }
46
+
47
+
48
+ class ARMMachineMixinM:
49
+ """Mixin for ARM M-series machine models"""
50
+
51
+ def __init__(self):
52
+ super().__init__()
53
+ self._registers.update(
54
+ {
55
+ # NOTE: PSR is aliased to CPSR
56
+ # This is an artifact of the fact that Unicorn
57
+ # seems to emulate a mash-up of M- and A-series arm.
58
+ "psr": (unicorn.arm_const.UC_ARM_REG_CPSR, "psr", 0, 4),
59
+ "primask": (unicorn.arm_const.UC_ARM_REG_PRIMASK, "primask", 0, 4),
60
+ "basepri": (unicorn.arm_const.UC_ARM_REG_BASEPRI, "basepri", 0, 4),
61
+ "faultmask": (
62
+ unicorn.arm_const.UC_ARM_REG_FAULTMASK,
63
+ "faultmask",
64
+ 0,
65
+ 4,
66
+ ),
67
+ "control": (unicorn.arm_const.UC_ARM_REG_CONTROL, "control", 0, 4),
68
+ "msp": (unicorn.arm_const.UC_ARM_REG_MSP, "msp", 0, 4),
69
+ "psp": (unicorn.arm_const.UC_ARM_REG_PSP, "psp", 0, 4),
70
+ }
71
+ )
72
+
73
+
74
+ class ARMMachineMixinRA:
75
+ """Mixin for ARM R- or A- series machine models"""
76
+
77
+ def __init__(self):
78
+ super().__init__()
79
+ self._registers.update(
80
+ {
81
+ "cpsr": (unicorn.arm_const.UC_ARM_REG_CPSR, "cpsr", 0, 4),
82
+ "spsr": (unicorn.arm_const.UC_ARM_REG_SPSR, "spsr", 0, 4),
83
+ # NOTE: None of the banked registers have Unicorn IDs
84
+ "sp_usr": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_usr", 0, 4),
85
+ "lr_usr": (unicorn.arm_const.UC_ARM_REG_INVALID, "lr_usr", 0, 4),
86
+ "r8_usr": (unicorn.arm_const.UC_ARM_REG_INVALID, "r8_usr", 0, 4),
87
+ "r9_usr": (unicorn.arm_const.UC_ARM_REG_INVALID, "r9_usr", 0, 4),
88
+ "r10_usr": (unicorn.arm_const.UC_ARM_REG_INVALID, "r10_usr", 0, 4),
89
+ "r11_usr": (unicorn.arm_const.UC_ARM_REG_INVALID, "r11_usr", 0, 4),
90
+ "r12_usr": (unicorn.arm_const.UC_ARM_REG_INVALID, "r12_usr", 0, 4),
91
+ "sp_hyp": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_hyp", 0, 4),
92
+ "spsr_hyp": (unicorn.arm_const.UC_ARM_REG_INVALID, "spsr_hyp", 0, 4),
93
+ "elr_hyp": (unicorn.arm_const.UC_ARM_REG_INVALID, "elr_hyp", 0, 4),
94
+ "sp_svc": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_svc", 0, 4),
95
+ "lr_svc": (unicorn.arm_const.UC_ARM_REG_INVALID, "lr_svc", 0, 4),
96
+ "spsr_svc": (unicorn.arm_const.UC_ARM_REG_INVALID, "spsr_svc", 0, 4),
97
+ "sp_abt": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_abt", 0, 4),
98
+ "lr_abt": (unicorn.arm_const.UC_ARM_REG_INVALID, "lr_abt", 0, 4),
99
+ "spsr_abt": (unicorn.arm_const.UC_ARM_REG_INVALID, "spsr_abt", 0, 4),
100
+ "sp_und": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_und", 0, 4),
101
+ "lr_und": (unicorn.arm_const.UC_ARM_REG_INVALID, "lr_und", 0, 4),
102
+ "spsr_und": (unicorn.arm_const.UC_ARM_REG_INVALID, "spsr_und", 0, 4),
103
+ "sp_mon": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_mon", 0, 4),
104
+ "lr_mon": (unicorn.arm_const.UC_ARM_REG_INVALID, "lr_mon", 0, 4),
105
+ "spsr_mon": (unicorn.arm_const.UC_ARM_REG_INVALID, "spsr_mon", 0, 4),
106
+ "sp_irq": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_irq", 0, 4),
107
+ "lr_irq": (unicorn.arm_const.UC_ARM_REG_INVALID, "lr_irq", 0, 4),
108
+ "spsr_irq": (unicorn.arm_const.UC_ARM_REG_INVALID, "spsr_irq", 0, 4),
109
+ "sp_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "sp_fiq", 0, 4),
110
+ "lr_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "lr_fiq", 0, 4),
111
+ "spsr_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "spsr_fiq", 0, 4),
112
+ "r8_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "r8_fiq", 0, 4),
113
+ "r9_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "r9_fiq", 0, 4),
114
+ "r10_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "r10_fiq", 0, 4),
115
+ "r11_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "r11_fiq", 0, 4),
116
+ "r12_fiq": (unicorn.arm_const.UC_ARM_REG_INVALID, "r12_fiq", 0, 4),
117
+ }
118
+ )
119
+
120
+
121
+ class ARMMachineMixinFP:
122
+ """Mixin for ARM machine models with basic FPUs"""
123
+
124
+ def __init__(self):
125
+ super().__init__()
126
+ self._registers.update(
127
+ {
128
+ "fpscr": (unicorn.arm_const.UC_ARM_REG_FPSCR, "fpscr", 0, 4),
129
+ "fpexc": (unicorn.arm_const.UC_ARM_REG_FPEXC, "fpexc", 0, 4),
130
+ "fpsid": (unicorn.arm_const.UC_ARM_REG_FPSID, "fpsid", 0, 4),
131
+ "mvfr0": (unicorn.arm_const.UC_ARM_REG_INVALID, "mvfr0", 0, 4),
132
+ "mvfr1": (unicorn.arm_const.UC_ARM_REG_INVALID, "mvfr1", 0, 4),
133
+ "d0": (unicorn.arm_const.UC_ARM_REG_D0, "d0", 0, 8),
134
+ "s0": (unicorn.arm_const.UC_ARM_REG_S0, "d0", 0, 4),
135
+ "s1": (unicorn.arm_const.UC_ARM_REG_S1, "d0", 4, 4),
136
+ "d1": (unicorn.arm_const.UC_ARM_REG_D1, "d1", 0, 8),
137
+ "s2": (unicorn.arm_const.UC_ARM_REG_S2, "d1", 0, 4),
138
+ "s3": (unicorn.arm_const.UC_ARM_REG_S3, "d1", 4, 4),
139
+ "d2": (unicorn.arm_const.UC_ARM_REG_D2, "d2", 0, 8),
140
+ "s4": (unicorn.arm_const.UC_ARM_REG_S4, "d2", 0, 4),
141
+ "s5": (unicorn.arm_const.UC_ARM_REG_S5, "d2", 4, 4),
142
+ "d3": (unicorn.arm_const.UC_ARM_REG_D3, "d3", 0, 8),
143
+ "s6": (unicorn.arm_const.UC_ARM_REG_S6, "d3", 0, 4),
144
+ "s7": (unicorn.arm_const.UC_ARM_REG_S7, "d3", 4, 4),
145
+ "d4": (unicorn.arm_const.UC_ARM_REG_D4, "d4", 0, 8),
146
+ "s8": (unicorn.arm_const.UC_ARM_REG_S8, "d4", 0, 4),
147
+ "s9": (unicorn.arm_const.UC_ARM_REG_S9, "d4", 4, 4),
148
+ "d5": (unicorn.arm_const.UC_ARM_REG_D5, "d5", 0, 8),
149
+ "s10": (unicorn.arm_const.UC_ARM_REG_S10, "d5", 0, 4),
150
+ "s11": (unicorn.arm_const.UC_ARM_REG_S11, "d5", 4, 4),
151
+ "d6": (unicorn.arm_const.UC_ARM_REG_D6, "d6", 0, 8),
152
+ "s12": (unicorn.arm_const.UC_ARM_REG_S12, "d6", 0, 4),
153
+ "s13": (unicorn.arm_const.UC_ARM_REG_S13, "d6", 4, 4),
154
+ "d7": (unicorn.arm_const.UC_ARM_REG_D7, "d7", 0, 8),
155
+ "s14": (unicorn.arm_const.UC_ARM_REG_S14, "d7", 0, 4),
156
+ "s15": (unicorn.arm_const.UC_ARM_REG_S15, "d7", 4, 4),
157
+ "d8": (unicorn.arm_const.UC_ARM_REG_D8, "d8", 0, 8),
158
+ "s16": (unicorn.arm_const.UC_ARM_REG_S16, "d8", 0, 4),
159
+ "s17": (unicorn.arm_const.UC_ARM_REG_S17, "d8", 4, 4),
160
+ "d9": (unicorn.arm_const.UC_ARM_REG_D9, "d9", 0, 8),
161
+ "s18": (unicorn.arm_const.UC_ARM_REG_S18, "d9", 0, 4),
162
+ "s19": (unicorn.arm_const.UC_ARM_REG_S19, "d9", 4, 4),
163
+ "d10": (unicorn.arm_const.UC_ARM_REG_D10, "d10", 0, 8),
164
+ "s20": (unicorn.arm_const.UC_ARM_REG_S20, "d10", 0, 4),
165
+ "s21": (unicorn.arm_const.UC_ARM_REG_S21, "d10", 4, 4),
166
+ "d11": (unicorn.arm_const.UC_ARM_REG_D11, "d11", 0, 8),
167
+ "s22": (unicorn.arm_const.UC_ARM_REG_S22, "d11", 0, 4),
168
+ "s23": (unicorn.arm_const.UC_ARM_REG_S23, "d11", 4, 4),
169
+ "d12": (unicorn.arm_const.UC_ARM_REG_D12, "d12", 0, 8),
170
+ "s24": (unicorn.arm_const.UC_ARM_REG_S24, "d12", 0, 4),
171
+ "s25": (unicorn.arm_const.UC_ARM_REG_S25, "d12", 4, 4),
172
+ "d13": (unicorn.arm_const.UC_ARM_REG_D13, "d13", 0, 8),
173
+ "s26": (unicorn.arm_const.UC_ARM_REG_S26, "d13", 0, 4),
174
+ "s27": (unicorn.arm_const.UC_ARM_REG_S27, "d13", 4, 4),
175
+ "d14": (unicorn.arm_const.UC_ARM_REG_D14, "d14", 0, 8),
176
+ "s28": (unicorn.arm_const.UC_ARM_REG_S28, "d14", 0, 4),
177
+ "s29": (unicorn.arm_const.UC_ARM_REG_S29, "d14", 4, 4),
178
+ "d15": (unicorn.arm_const.UC_ARM_REG_D15, "d15", 0, 8),
179
+ "s30": (unicorn.arm_const.UC_ARM_REG_S30, "d15", 0, 4),
180
+ "s31": (unicorn.arm_const.UC_ARM_REG_S31, "d15", 4, 4),
181
+ }
182
+ )
183
+
184
+
185
+ class ARMMachineMixinVFP:
186
+ """Mixin for ARM machine models with VFP/NEON support"""
187
+
188
+ def __init__(self):
189
+ super().__init__()
190
+ self._registers.update(
191
+ {
192
+ "fpscr": (unicorn.arm_const.UC_ARM_REG_FPSCR, "fpscr", 0, 4),
193
+ "fpexc": (unicorn.arm_const.UC_ARM_REG_FPEXC, "fpexc", 0, 4),
194
+ "fpsid": (unicorn.arm_const.UC_ARM_REG_FPSID, "fpsid", 0, 4),
195
+ "mvfr0": (unicorn.arm_const.UC_ARM_REG_INVALID, "mvfr0", 0, 4),
196
+ "mvfr1": (unicorn.arm_const.UC_ARM_REG_INVALID, "mvfr1", 0, 4),
197
+ "q0": (unicorn.arm_const.UC_ARM_REG_Q0, "q0", 0, 16),
198
+ "d0": (unicorn.arm_const.UC_ARM_REG_D0, "q0", 0, 8),
199
+ "s0": (unicorn.arm_const.UC_ARM_REG_S0, "q0", 0, 4),
200
+ "s1": (unicorn.arm_const.UC_ARM_REG_S1, "q0", 4, 4),
201
+ "d1": (unicorn.arm_const.UC_ARM_REG_D1, "q0", 8, 8),
202
+ "s2": (unicorn.arm_const.UC_ARM_REG_S2, "q0", 8, 4),
203
+ "s3": (unicorn.arm_const.UC_ARM_REG_S3, "q0", 12, 4),
204
+ "q1": (unicorn.arm_const.UC_ARM_REG_Q1, "q1", 0, 16),
205
+ "d2": (unicorn.arm_const.UC_ARM_REG_D2, "q1", 0, 8),
206
+ "s4": (unicorn.arm_const.UC_ARM_REG_S4, "q1", 0, 4),
207
+ "s5": (unicorn.arm_const.UC_ARM_REG_S5, "q1", 4, 4),
208
+ "d3": (unicorn.arm_const.UC_ARM_REG_D3, "q1", 8, 8),
209
+ "s6": (unicorn.arm_const.UC_ARM_REG_S6, "q1", 8, 4),
210
+ "s7": (unicorn.arm_const.UC_ARM_REG_S7, "q1", 12, 4),
211
+ "q2": (unicorn.arm_const.UC_ARM_REG_Q2, "q2", 0, 16),
212
+ "d4": (unicorn.arm_const.UC_ARM_REG_D4, "q2", 0, 8),
213
+ "s8": (unicorn.arm_const.UC_ARM_REG_S8, "q2", 0, 4),
214
+ "s9": (unicorn.arm_const.UC_ARM_REG_S9, "q2", 4, 4),
215
+ "d5": (unicorn.arm_const.UC_ARM_REG_D5, "q2", 8, 8),
216
+ "s10": (unicorn.arm_const.UC_ARM_REG_S10, "q2", 8, 4),
217
+ "s11": (unicorn.arm_const.UC_ARM_REG_S11, "q2", 12, 4),
218
+ "q3": (unicorn.arm_const.UC_ARM_REG_Q3, "q3", 0, 16),
219
+ "d6": (unicorn.arm_const.UC_ARM_REG_D6, "q3", 0, 8),
220
+ "s12": (unicorn.arm_const.UC_ARM_REG_S12, "q3", 0, 4),
221
+ "s13": (unicorn.arm_const.UC_ARM_REG_S13, "q3", 4, 4),
222
+ "d7": (unicorn.arm_const.UC_ARM_REG_D7, "q3", 8, 8),
223
+ "s14": (unicorn.arm_const.UC_ARM_REG_S14, "q3", 8, 4),
224
+ "s15": (unicorn.arm_const.UC_ARM_REG_S15, "q3", 12, 4),
225
+ "q4": (unicorn.arm_const.UC_ARM_REG_Q4, "q4", 0, 16),
226
+ "d8": (unicorn.arm_const.UC_ARM_REG_D8, "q4", 0, 8),
227
+ "s16": (unicorn.arm_const.UC_ARM_REG_S16, "q4", 0, 4),
228
+ "s17": (unicorn.arm_const.UC_ARM_REG_S17, "q4", 4, 4),
229
+ "d9": (unicorn.arm_const.UC_ARM_REG_D9, "q4", 8, 8),
230
+ "s18": (unicorn.arm_const.UC_ARM_REG_S18, "q4", 8, 4),
231
+ "s19": (unicorn.arm_const.UC_ARM_REG_S19, "q4", 12, 4),
232
+ "q5": (unicorn.arm_const.UC_ARM_REG_Q5, "q5", 0, 16),
233
+ "d10": (unicorn.arm_const.UC_ARM_REG_D10, "q5", 0, 8),
234
+ "s20": (unicorn.arm_const.UC_ARM_REG_S20, "q5", 0, 4),
235
+ "s21": (unicorn.arm_const.UC_ARM_REG_S21, "q5", 4, 4),
236
+ "d11": (unicorn.arm_const.UC_ARM_REG_D11, "q5", 8, 8),
237
+ "s22": (unicorn.arm_const.UC_ARM_REG_S22, "q5", 8, 4),
238
+ "s23": (unicorn.arm_const.UC_ARM_REG_S23, "q5", 12, 4),
239
+ "q6": (unicorn.arm_const.UC_ARM_REG_Q6, "q6", 0, 16),
240
+ "d12": (unicorn.arm_const.UC_ARM_REG_D12, "q6", 0, 8),
241
+ "s24": (unicorn.arm_const.UC_ARM_REG_S24, "q6", 0, 4),
242
+ "s25": (unicorn.arm_const.UC_ARM_REG_S25, "q6", 4, 4),
243
+ "d13": (unicorn.arm_const.UC_ARM_REG_D13, "q6", 8, 8),
244
+ "s26": (unicorn.arm_const.UC_ARM_REG_S26, "q6", 8, 4),
245
+ "s27": (unicorn.arm_const.UC_ARM_REG_S27, "q6", 12, 4),
246
+ "q7": (unicorn.arm_const.UC_ARM_REG_Q7, "q7", 0, 16),
247
+ "d14": (unicorn.arm_const.UC_ARM_REG_D14, "q7", 0, 8),
248
+ "s28": (unicorn.arm_const.UC_ARM_REG_S28, "q7", 0, 4),
249
+ "s29": (unicorn.arm_const.UC_ARM_REG_S29, "q7", 4, 4),
250
+ "d15": (unicorn.arm_const.UC_ARM_REG_D15, "q7", 8, 8),
251
+ "s30": (unicorn.arm_const.UC_ARM_REG_S30, "q7", 8, 4),
252
+ "s31": (unicorn.arm_const.UC_ARM_REG_S31, "q7", 12, 4),
253
+ "q8": (unicorn.arm_const.UC_ARM_REG_Q8, "q8", 0, 16),
254
+ "d16": (unicorn.arm_const.UC_ARM_REG_D16, "q8", 0, 8),
255
+ "d17": (unicorn.arm_const.UC_ARM_REG_D17, "q8", 8, 8),
256
+ "q9": (unicorn.arm_const.UC_ARM_REG_Q9, "q9", 0, 16),
257
+ "d18": (unicorn.arm_const.UC_ARM_REG_D18, "q9", 0, 8),
258
+ "d19": (unicorn.arm_const.UC_ARM_REG_D19, "q9", 8, 8),
259
+ "q10": (unicorn.arm_const.UC_ARM_REG_Q10, "q10", 0, 16),
260
+ "d20": (unicorn.arm_const.UC_ARM_REG_D20, "q10", 0, 8),
261
+ "d21": (unicorn.arm_const.UC_ARM_REG_D21, "q10", 8, 8),
262
+ "q11": (unicorn.arm_const.UC_ARM_REG_Q11, "q11", 0, 16),
263
+ "d22": (unicorn.arm_const.UC_ARM_REG_D22, "q11", 0, 8),
264
+ "d23": (unicorn.arm_const.UC_ARM_REG_D23, "q11", 8, 8),
265
+ "q12": (unicorn.arm_const.UC_ARM_REG_Q12, "q12", 0, 16),
266
+ "d24": (unicorn.arm_const.UC_ARM_REG_D24, "q12", 0, 8),
267
+ "d25": (unicorn.arm_const.UC_ARM_REG_D25, "q12", 8, 8),
268
+ "q13": (unicorn.arm_const.UC_ARM_REG_Q13, "q13", 0, 16),
269
+ "d26": (unicorn.arm_const.UC_ARM_REG_D26, "q13", 0, 8),
270
+ "d27": (unicorn.arm_const.UC_ARM_REG_D27, "q13", 8, 8),
271
+ "q14": (unicorn.arm_const.UC_ARM_REG_Q14, "q14", 0, 16),
272
+ "d28": (unicorn.arm_const.UC_ARM_REG_D28, "q14", 0, 8),
273
+ "d29": (unicorn.arm_const.UC_ARM_REG_D29, "q14", 8, 8),
274
+ "q15": (unicorn.arm_const.UC_ARM_REG_Q15, "q15", 0, 16),
275
+ "d30": (unicorn.arm_const.UC_ARM_REG_D30, "q15", 0, 8),
276
+ "d31": (unicorn.arm_const.UC_ARM_REG_D31, "q15", 8, 8),
277
+ }
278
+ )
279
+
280
+
281
+ class ARMv5TMachineDef(ARMMachineMixinM, ARMMachineDef):
282
+ """Unicorn machine definition for ARMv5T little-endian"""
283
+
284
+ arch = Architecture.ARM_V5T
285
+ byteorder = Byteorder.LITTLE
286
+
287
+
288
+ class ARMv6MMachineDef(ARMMachineMixinFP, ARMMachineMixinM, ARMMachineDef):
289
+ """Unicorn machine definition for ARMv6-M little-endian"""
290
+
291
+ arch = Architecture.ARM_V6M
292
+ byteorder = Byteorder.LITTLE
293
+
294
+
295
+ class ARMv6MThumbMachineDef(ARMv6MMachineDef):
296
+ """Unicorn machine definition for ARMv6-M little-endian, THUMB ISA"""
297
+
298
+ arch = Architecture.ARM_V6M_THUMB
299
+ uc_mode = unicorn.UC_MODE_THUMB
300
+ cs_mode = capstone.CS_MODE_THUMB
301
+
302
+
303
+ class ARMv7MMachineDef(ARMMachineMixinFP, ARMMachineMixinM, ARMMachineDef):
304
+ """Unicorn machine definition for ARMv7-M little-endian"""
305
+
306
+ arch = Architecture.ARM_V7M
307
+ byteorder = Byteorder.LITTLE
308
+
309
+
310
+ class ARMv7RMachineDef(ARMMachineMixinVFP, ARMMachineMixinRA, ARMMachineDef):
311
+ """Unicorn machine definition for ARMv7-R little-endian"""
312
+
313
+ arch = Architecture.ARM_V7R
314
+ byteorder = Byteorder.LITTLE
315
+
316
+
317
+ class ARMv7AMachineDef(ARMMachineMixinVFP, ARMMachineMixinRA, ARMMachineDef):
318
+ """Unicorn machine definition for ARMv7-A little-endian"""
319
+
320
+ arch = Architecture.ARM_V7A
321
+ byteorder = Byteorder.LITTLE
@@ -0,0 +1,137 @@
1
+ import capstone
2
+ import unicorn
3
+
4
+ from ....platforms import Architecture, Byteorder
5
+ from .machdef import UnicornMachineDef
6
+
7
+
8
+ class i386MachineDef(UnicornMachineDef):
9
+ """Unicorn machine definition for i386"""
10
+
11
+ arch = Architecture.X86_32
12
+ byteorder = Byteorder.LITTLE
13
+
14
+ uc_arch = unicorn.UC_ARCH_X86
15
+ uc_mode = unicorn.UC_MODE_32
16
+
17
+ cs_arch = capstone.CS_ARCH_X86
18
+ cs_mode = capstone.CS_MODE_32
19
+
20
+ pc_reg = "eip"
21
+
22
+ _registers = {
23
+ # *** General Purpose Registers ***
24
+ "eax": (unicorn.x86_const.UC_X86_REG_EAX, "eax", 4, 0),
25
+ "ax": (unicorn.x86_const.UC_X86_REG_AX, "eax", 2, 0),
26
+ "al": (unicorn.x86_const.UC_X86_REG_AL, "eax", 1, 0),
27
+ "ah": (unicorn.x86_const.UC_X86_REG_AH, "eax", 1, 1),
28
+ "ebx": (unicorn.x86_const.UC_X86_REG_EBX, "ebx", 4, 0),
29
+ "bx": (unicorn.x86_const.UC_X86_REG_BX, "ebx", 2, 0),
30
+ "bl": (unicorn.x86_const.UC_X86_REG_BL, "ebx", 1, 0),
31
+ "bh": (unicorn.x86_const.UC_X86_REG_BH, "ebx", 1, 1),
32
+ "ecx": (unicorn.x86_const.UC_X86_REG_ECX, "ecx", 4, 0),
33
+ "cx": (unicorn.x86_const.UC_X86_REG_CX, "ecx", 2, 0),
34
+ "cl": (unicorn.x86_const.UC_X86_REG_CL, "ecx", 1, 0),
35
+ "ch": (unicorn.x86_const.UC_X86_REG_CH, "ecx", 1, 1),
36
+ "edx": (unicorn.x86_const.UC_X86_REG_EDX, "edx", 4, 0),
37
+ "dx": (unicorn.x86_const.UC_X86_REG_DX, "edx", 2, 0),
38
+ "dl": (unicorn.x86_const.UC_X86_REG_DL, "edx", 1, 0),
39
+ "dh": (unicorn.x86_const.UC_X86_REG_DH, "edx", 1, 1),
40
+ "esi": (unicorn.x86_const.UC_X86_REG_ESI, "esi", 4, 0),
41
+ "si": (unicorn.x86_const.UC_X86_REG_SI, "esi", 2, 0),
42
+ "sil": (unicorn.x86_const.UC_X86_REG_SIL, "esi", 1, 0),
43
+ "edi": (unicorn.x86_const.UC_X86_REG_EDI, "edi", 4, 0),
44
+ "di": (unicorn.x86_const.UC_X86_REG_DI, "edi", 2, 0),
45
+ "dil": (unicorn.x86_const.UC_X86_REG_DIL, "edi", 1, 0),
46
+ "ebp": (unicorn.x86_const.UC_X86_REG_EBP, "ebp", 4, 0),
47
+ "bp": (unicorn.x86_const.UC_X86_REG_BP, "ebp", 2, 0),
48
+ "bpl": (unicorn.x86_const.UC_X86_REG_BPL, "ebp", 1, 0),
49
+ "esp": (unicorn.x86_const.UC_X86_REG_ESP, "esp", 4, 0),
50
+ "sp": (unicorn.x86_const.UC_X86_REG_SP, "esp", 2, 0),
51
+ "spl": (unicorn.x86_const.UC_X86_REG_SPL, "esp", 1, 0),
52
+ # *** Instruction Pointer ***
53
+ "eip": (unicorn.x86_const.UC_X86_REG_EIP, "eip", 4, 0),
54
+ "ip": (unicorn.x86_const.UC_X86_REG_IP, "eip", 2, 0),
55
+ # *** Segment Registers ***
56
+ "cs": (unicorn.x86_const.UC_X86_REG_CS, "cs", 2, 0),
57
+ "ss": (unicorn.x86_const.UC_X86_REG_SS, "ss", 2, 0),
58
+ "ds": (unicorn.x86_const.UC_X86_REG_DS, "ds", 2, 0),
59
+ "es": (unicorn.x86_const.UC_X86_REG_ES, "es", 2, 0),
60
+ "fs": (unicorn.x86_const.UC_X86_REG_FS, "fs", 2, 0),
61
+ "gs": (unicorn.x86_const.UC_X86_REG_GS, "gs", 2, 0),
62
+ # *** Flags Registers ***
63
+ "eflags": (unicorn.x86_const.UC_X86_REG_EFLAGS, "eflags", 4, 0),
64
+ "flags": (unicorn.x86_const.UC_X86_REG_FLAGS, "eflags", 2, 0),
65
+ # *** Control Registers ***
66
+ "cr0": (unicorn.x86_const.UC_X86_REG_CR0, "cr0", 4, 0),
67
+ "cr1": (unicorn.x86_const.UC_X86_REG_CR1, "cr1", 4, 0),
68
+ "cr2": (unicorn.x86_const.UC_X86_REG_CR2, "cr2", 4, 0),
69
+ "cr3": (unicorn.x86_const.UC_X86_REG_CR3, "cr3", 4, 0),
70
+ "cr4": (unicorn.x86_const.UC_X86_REG_CR4, "cr4", 4, 0),
71
+ # NOTE: I've got conflicting reports whether cr8 exists in i386.
72
+ "cr8": (unicorn.x86_const.UC_X86_REG_INVALID, "cr8", 4, 0),
73
+ # *** Debug Registers ***
74
+ "dr0": (unicorn.x86_const.UC_X86_REG_DR0, "dr0", 4, 0),
75
+ "dr1": (unicorn.x86_const.UC_X86_REG_DR1, "dr1", 4, 0),
76
+ "dr2": (unicorn.x86_const.UC_X86_REG_DR2, "dr2", 4, 0),
77
+ "dr3": (unicorn.x86_const.UC_X86_REG_DR3, "dr3", 4, 0),
78
+ "dr6": (unicorn.x86_const.UC_X86_REG_DR6, "dr6", 4, 0),
79
+ "dr7": (unicorn.x86_const.UC_X86_REG_DR7, "dr7", 4, 0),
80
+ # *** Descriptor Table Registers
81
+ # NOTE: Yes, this is 6 bytes; 2 byte segment selector plus 4 byte offset
82
+ "gdtr": (unicorn.x86_const.UC_X86_REG_GDTR, "gdtr", 6, 0),
83
+ "idtr": (unicorn.x86_const.UC_X86_REG_IDTR, "idtr", 6, 0),
84
+ "ldtr": (unicorn.x86_const.UC_X86_REG_LDTR, "ldtr", 6, 0),
85
+ # *** Task Register ***
86
+ # NOTE: Yes, this is 6 bytes; 2 byte segment selector plus 4 byte offset
87
+ "tr": (unicorn.x86_const.UC_X86_REG_TR, "tr", 6, 0),
88
+ # *** x87 registers ***
89
+ # NOTE: x87 is supported by Unicorn, but not by SmallWorld.
90
+ # Values are represented as tuples (exponent: int, mantissa: int).
91
+ # If you need x87 support, open a ticket.
92
+ "fpr0": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr0", 10, 0),
93
+ "fpr1": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr1", 10, 0),
94
+ "fpr2": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr2", 10, 0),
95
+ "fpr3": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr3", 10, 0),
96
+ "fpr4": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr4", 10, 0),
97
+ "fpr5": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr5", 10, 0),
98
+ "fpr6": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr6", 10, 0),
99
+ "fpr7": (unicorn.x86_const.UC_X86_REG_INVALID, "fpr7", 10, 0),
100
+ # x87 Control Register
101
+ "fctrl": (unicorn.x86_const.UC_X86_REG_FPCW, "fctrl", 2, 0),
102
+ # x87 Status Register
103
+ "fstat": (unicorn.x86_const.UC_X86_REG_FPSW, "fstat", 2, 0),
104
+ # x87 Tag Register
105
+ "ftag": (unicorn.x86_const.UC_X86_REG_FPTAG, "ftag", 2, 0),
106
+ # x87 Last Instruction Register
107
+ "fip": (unicorn.x86_const.UC_X86_REG_FIP, "fip", 8, 0),
108
+ # x87 Last Operand Pointer
109
+ "fdp": (unicorn.x86_const.UC_X86_REG_FDP, "fdp", 8, 0),
110
+ # x87 Last Opcode
111
+ "fop": (unicorn.x86_const.UC_X86_REG_FOP, "fop", 2, 0),
112
+ # NOTE: Docs disagree on the format of fip and fdp.
113
+ # One source describes them as 48-bit offset-plus-segment,
114
+ # the other describes them as 64-bit.
115
+ # There may also be separate segment registers.
116
+ # If you care about the x87 debug info, please feel free to update.
117
+ # *** MMX Registers ***
118
+ # NOTE: The MMX registers are aliases for the low 8 bytes of the x87 registers.
119
+ # The two subsystems cannot be used simultaneously.
120
+ "mm0": (unicorn.x86_const.UC_X86_REG_MM0, "fpr0", 8, 0),
121
+ "mm1": (unicorn.x86_const.UC_X86_REG_MM1, "fpr1", 8, 0),
122
+ "mm2": (unicorn.x86_const.UC_X86_REG_MM2, "fpr2", 8, 0),
123
+ "mm3": (unicorn.x86_const.UC_X86_REG_MM3, "fpr3", 8, 0),
124
+ "mm4": (unicorn.x86_const.UC_X86_REG_MM4, "fpr4", 8, 0),
125
+ "mm5": (unicorn.x86_const.UC_X86_REG_MM5, "fpr5", 8, 0),
126
+ "mm6": (unicorn.x86_const.UC_X86_REG_MM6, "fpr6", 8, 0),
127
+ "mm7": (unicorn.x86_const.UC_X86_REG_MM7, "fpr7", 8, 0),
128
+ # *** SSE Registers ***
129
+ "xmm0": (unicorn.x86_const.UC_X86_REG_XMM0, "xmm0", 16, 0),
130
+ "xmm1": (unicorn.x86_const.UC_X86_REG_XMM1, "xmm1", 16, 0),
131
+ "xmm2": (unicorn.x86_const.UC_X86_REG_XMM2, "xmm2", 16, 0),
132
+ "xmm3": (unicorn.x86_const.UC_X86_REG_XMM3, "xmm3", 16, 0),
133
+ "xmm4": (unicorn.x86_const.UC_X86_REG_XMM4, "xmm4", 16, 0),
134
+ "xmm5": (unicorn.x86_const.UC_X86_REG_XMM5, "xmm5", 16, 0),
135
+ "xmm6": (unicorn.x86_const.UC_X86_REG_XMM6, "xmm6", 16, 0),
136
+ "xmm7": (unicorn.x86_const.UC_X86_REG_XMM7, "xmm7", 16, 0),
137
+ }
@@ -0,0 +1,117 @@
1
+ import abc
2
+ import inspect
3
+ import typing
4
+
5
+ from .... import platforms, utils
6
+
7
+ # from ....platforms import Architecture
8
+
9
+
10
+ class UnicornMachineDef(metaclass=abc.ABCMeta):
11
+ """Container class for Unicorn architecture-specific definitions"""
12
+
13
+ @property
14
+ @abc.abstractmethod
15
+ def arch(self) -> platforms.Architecture:
16
+ """The architecture ID"""
17
+ raise NotImplementedError("Abstract unicorn machine def has no architecture")
18
+
19
+ @property
20
+ @abc.abstractmethod
21
+ def byteorder(self) -> platforms.Byteorder:
22
+ """The byte order"""
23
+ raise NotImplementedError("Abstract unicorn machine def has no byteorder")
24
+
25
+ @property
26
+ @abc.abstractmethod
27
+ def uc_arch(self) -> int:
28
+ """The Unicorn architecture ID"""
29
+ return 0
30
+
31
+ @property
32
+ @abc.abstractmethod
33
+ def uc_mode(self) -> int:
34
+ """The unicorn mode ID
35
+
36
+ This must include an byteorder flag
37
+ """
38
+ return 0
39
+
40
+ @property
41
+ @abc.abstractmethod
42
+ def cs_arch(self) -> int:
43
+ """The capstone arch ID"""
44
+ return 0
45
+
46
+ @property
47
+ @abc.abstractmethod
48
+ def cs_mode(self) -> int:
49
+ """The capstone mode ID
50
+
51
+ This must include an byteorder flag
52
+ """
53
+ return 0
54
+
55
+ @property
56
+ @abc.abstractmethod
57
+ def pc_reg(self) -> str:
58
+ """The name of the Program Counter register for this machine"""
59
+ return ""
60
+
61
+ _registers: typing.Dict[str, typing.Tuple[typing.Any, str, int, int]] = {}
62
+
63
+ def uc_reg(self, name: str) -> typing.Tuple[typing.Any, str, int, int]:
64
+ """Convert a register name to unicorn constant
65
+
66
+ This must cover all names defined in the CPU state model
67
+ for this arch/mode/byteorder, or return 0,
68
+ which always indicates an invalid register
69
+ """
70
+ if name in self._registers:
71
+ return self._registers[name]
72
+ else:
73
+ raise ValueError(
74
+ f"Unknown register for {self.arch}:{self.byteorder}: {name}"
75
+ )
76
+
77
+ @classmethod
78
+ def for_platform(cls, platform: platforms.Platform):
79
+ """Find the appropriate MachineDef for your architecture
80
+
81
+ Arguments:
82
+ platform: platform metadata
83
+
84
+ Returns:
85
+ An instance of the appropriate MachineDef
86
+
87
+ Raises:
88
+ ValueError: If no MachineDef subclass matches your request
89
+ """
90
+
91
+ try:
92
+ return utils.find_subclass(
93
+ cls,
94
+ lambda x: x.arch == platform.architecture
95
+ and x.byteorder == platform.byteorder,
96
+ )
97
+ except:
98
+ raise ValueError(
99
+ f"No machine model for {platform.architecture}:{platform.byteorder}"
100
+ )
101
+
102
+
103
+ def populate_registers(arch_info, unicorn_consts):
104
+ def find_uc_const(reg_name):
105
+ ew = f"_{reg_name.upper()}"
106
+ for name, num in inspect.getmembers(unicorn_consts):
107
+ if name.endswith(ew) and "REG" in name:
108
+ return (name, num)
109
+ return None
110
+
111
+ registers = {}
112
+ for reg_name, info in arch_info.items():
113
+ (base_reg_name, (start, end)) = info
114
+ (ucstr, ucnum) = find_uc_const(reg_name)
115
+ registers[reg_name] = (ucnum, base_reg_name, start, end)
116
+
117
+ return registers