Skip to content

Commit

Permalink
Merge pull request keystone-engine#1 from null-cell/RISCV
Browse files Browse the repository at this point in the history
Adds RISCV support
  • Loading branch information
null-cell authored Mar 28, 2023
2 parents 1856935 + bc980f0 commit db034e5
Show file tree
Hide file tree
Showing 82 changed files with 15,122 additions and 48 deletions.
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include",
"${workspaceFolder}/llvm/lib/Target/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
59 changes: 59 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) MIPS Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/kali/keystone/build/kstool/kstool",
"args": ["mips", "'nop'"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) RISCVLaunch",
"type": "cppdbg",
"request": "launch",
"program": "/home/kali/keystone/build/kstool/kstool",
"args": ["riscv64", "'mul a2, a1, a1'"],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}

]
}
66 changes: 66 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"files.associations": {
"*.inc": "c",
"*.tcc": "cpp",
"bitset": "cpp",
"lanebitmask.h": "c",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeinfo": "cpp"
},
"C_Cpp.dimInactiveRegions": false
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Keystone Engine
Keystone is a lightweight multi-platform, multi-architecture assembler framework.
It offers some unparalleled features:

- Multi-architecture, with support for Arm, Arm64 (AArch64/Armv8), Ethereum Virtual Machine, Hexagon, Mips, PowerPC, Sparc, SystemZ & X86 (include 16/32/64bit).
- Multi-architecture, with support for Arm, Arm64 (AArch64/Armv8), Ethereum Virtual Machine, Hexagon, Mips, PowerPC, RISC-V, Sparc, SystemZ & X86 (include 16/32/64bit).
- Clean/simple/lightweight/intuitive architecture-neutral API.
- Implemented in C/C++ languages, with bindings for Java, Masm, C#, PowerShell, Perl, Python, NodeJS, Ruby, Go, Rust, Haskell, VB6 & OCaml available.
- Native support for Windows & \*nix (with Mac OSX, Linux, \*BSD & Solaris confirmed).
Expand Down
4 changes: 4 additions & 0 deletions bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SAMPLE_MIPS = $(TMPDIR)/sample_mips
SAMPLE_M68K = $(TMPDIR)/sample_m68k
SAMPLE_SPARC = $(TMPDIR)/sample_sparc
SAMPLE_X86 = $(TMPDIR)/sample_x86
SAMPLE_RISCV = $(TMPDIR)/sample_riscv

.PHONY: all expected python rust go nodejs ruby

Expand All @@ -34,6 +35,7 @@ expected:
../samples/sample_sparc > $(SAMPLE_SPARC)_e
../samples/sample_m68k > $(SAMPLE_M68K)_e
../samples/sample_x86 > $(SAMPLE_X86)_e
../samples/sample_riscv > $(SAMPLE_RISCV)_e

python: FORCE
cd python && $(MAKE)
Expand All @@ -43,6 +45,7 @@ python: FORCE
python python/sample_sparc.py > $(SAMPLE_SPARC)_o
python python/sample_m68k.py > $(SAMPLE_M68K)_o
python python/sample_x86.py > $(SAMPLE_X86)_o
python python/sample_riscv.py > $(SAMPLE_RISCV)_o
$(MAKE) sample_diff

ruby: FORCE
Expand All @@ -67,6 +70,7 @@ sample_diff: FORCE
$(DIFF) $(SAMPLE_SPARC)_e $(SAMPLE_SPARC)_o
$(DIFF) $(SAMPLE_M68K)_e $(SAMPLE_M68K)_o
$(DIFF) $(SAMPLE_X86)_e $(SAMPLE_X86)_o
$(DIFF) $(SAMPLE_RISCV)_e $(SAMPLE_RISCV)_o

clean:
rm -rf $(TMPDIR)
Expand Down
18 changes: 12 additions & 6 deletions bindings/const_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# NOTE: this reflects the value of KS_ERR_ASM_xxx in keystone.h
ks_err_val = { 'KS_ERR_ASM': '128', 'KS_ERR_ASM_ARCH': '512' }

include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'ppc.h', 'systemz.h', 'hexagon.h', 'evm.h', 'keystone.h' ]
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'riscv.h', 'ppc.h', 'systemz.h', 'hexagon.h', 'evm.h', 'keystone.h' ]

def CamelCase(s):
# return re.sub(r'(\w)+\_?', lambda m:m.group(0).capitalize(), s)
Expand Down Expand Up @@ -40,6 +40,7 @@ def CamelCase(s):
'mips.h': 'keystone',
'x86.h': 'keystone',
'sparc.h': 'keystone',
'riscv.h': 'keystone',
'systemz.h': 'keystone',
'ppc.h': 'keystone',
'hexagon.h': 'keystone',
Expand Down Expand Up @@ -112,6 +113,7 @@ def CamelCase(s):
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
'riscv.h': 'riscv',
'systemz.h': 'systemz',
'ppc.h': 'ppc',
'hexagon.h': 'hexagon',
Expand Down Expand Up @@ -174,6 +176,7 @@ def CamelCase(s):
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
'riscv.h': 'riscv',
'systemz.h': 'systemz',
'ppc.h': 'ppc',
'hexagon.h': 'hexagon',
Expand All @@ -199,6 +202,7 @@ def CamelCase(s):
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
'riscv.h': 'riscv',
'systemz.h': 'systemz',
'ppc.h': 'ppc',
'hexagon.h': 'hexagon',
Expand All @@ -224,6 +228,7 @@ def CamelCase(s):
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
'riscv.h': 'riscv.h',
'systemz.h': 'systemz',
'ppc.h': 'ppc',
'hexagon.h': 'hexagon',
Expand All @@ -250,6 +255,7 @@ def CamelCase(s):
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
'riscv.h': 'riscv',
'systemz.h': 'systemz',
'ppc.h': 'ppc',
'hexagon.h': 'hexagon',
Expand Down Expand Up @@ -387,7 +393,7 @@ def gen(lang):

for prefix in consts.keys():
outfile = open(templ['out_file'] % prefix, 'wb') # open as binary prevents windows newlines
outfile.write (templ['header'] % prefix)
outfile.write (str.encode(templ['header'] % prefix))

for rule in rules:
regex = rule['regex']
Expand All @@ -403,18 +409,18 @@ def gen(lang):
continue

if rule.get('pre'):
outfile.write(rule.get('pre').format(CamelCase(prefix)))
outfile.write(str.encode(rule.get('pre').format(CamelCase(prefix))))

for const in consts2:
lhs_strip = const[0]
rhs = const[1]
outfile.write(rule['line_format'].format(rule['fn'](lhs_strip), rhs, lhs_strip).encode("utf-8"))

if rule.get('post'):
outfile.write (rule.get('post'))
outfile.write ('\n')
outfile.write(str.encode (rule.get('post')))
outfile.write(str.encode ('\n'))

outfile.write (templ['footer'])
outfile.write(str.encode(templ['footer']))
outfile.close()

def main():
Expand Down
13 changes: 8 additions & 5 deletions bindings/go/keystone/keystone_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
MODE_PPC32 Mode = 4
MODE_PPC64 Mode = 8
MODE_QPX Mode = 16
MODE_RISCV32 Mode = 4
MODE_RISCV64 Mode = 8
MODE_SPARC32 Mode = 4
MODE_SPARC64 Mode = 8
MODE_V9 Mode = 16
Expand All @@ -34,11 +36,12 @@ const (
ARCH_MIPS Architecture = 3
ARCH_X86 Architecture = 4
ARCH_PPC Architecture = 5
ARCH_SPARC Architecture = 6
ARCH_SYSTEMZ Architecture = 7
ARCH_HEXAGON Architecture = 8
ARCH_EVM Architecture = 9
ARCH_MAX Architecture = 10
ARCH_RISCV Architecture = 6
ARCH_SPARC Architecture = 7
ARCH_SYSTEMZ Architecture = 8
ARCH_HEXAGON Architecture = 9
ARCH_EVM Architecture = 10
ARCH_MAX Architecture = 11
)

const (
Expand Down
9 changes: 9 additions & 0 deletions bindings/go/keystone/riscv_const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package keystone
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [riscv_const.go]

const (
ERR_ASM_RISCV_INVALIDOPERAND Error = 512
ERR_ASM_RISCV_MISSINGFEATURE Error = 513
ERR_ASM_RISCV_MNEMONICFAIL Error = 514
)

15 changes: 9 additions & 6 deletions bindings/nodejs/consts/keystone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ module.exports.API_MAJOR = 0
module.exports.API_MINOR = 9
module.exports.VERSION_MAJOR = 0
module.exports.VERSION_MINOR = 9
module.exports.VERSION_EXTRA = 2
module.exports.VERSION_EXTRA = 3
module.exports.ARCH_ARM = 1
module.exports.ARCH_ARM64 = 2
module.exports.ARCH_MIPS = 3
module.exports.ARCH_X86 = 4
module.exports.ARCH_PPC = 5
module.exports.ARCH_SPARC = 6
module.exports.ARCH_SYSTEMZ = 7
module.exports.ARCH_HEXAGON = 8
module.exports.ARCH_EVM = 9
module.exports.ARCH_MAX = 10
module.exports.ARCH_RISCV = 6
module.exports.ARCH_SPARC = 7
module.exports.ARCH_SYSTEMZ = 8
module.exports.ARCH_HEXAGON = 9
module.exports.ARCH_EVM = 10
module.exports.ARCH_MAX = 11
module.exports.MODE_LITTLE_ENDIAN = 0
module.exports.MODE_BIG_ENDIAN = 1073741824
module.exports.MODE_ARM = 1
Expand All @@ -30,6 +31,8 @@ module.exports.MODE_64 = 8
module.exports.MODE_PPC32 = 4
module.exports.MODE_PPC64 = 8
module.exports.MODE_QPX = 16
module.exports.MODE_RISCV32 = 4
module.exports.MODE_RISCV64 = 8
module.exports.MODE_SPARC32 = 4
module.exports.MODE_SPARC64 = 8
module.exports.MODE_V9 = 16
Expand Down
4 changes: 4 additions & 0 deletions bindings/nodejs/consts/riscv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [riscv_const.js]
module.exports.ERR_ASM_RISCV_INVALIDOPERAND = 512
module.exports.ERR_ASM_RISCV_MISSINGFEATURE = 513
module.exports.ERR_ASM_RISCV_MNEMONICFAIL = 514
15 changes: 9 additions & 6 deletions bindings/powershell/Keystone/Const/keystone_h.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
KS_API_MINOR = 9,
KS_VERSION_MAJOR = 0,
KS_VERSION_MINOR = 9,
KS_VERSION_EXTRA = 2,
KS_VERSION_EXTRA = 3,
KS_ARCH_ARM = 1,
KS_ARCH_ARM64 = 2,
KS_ARCH_MIPS = 3,
KS_ARCH_X86 = 4,
KS_ARCH_PPC = 5,
KS_ARCH_SPARC = 6,
KS_ARCH_SYSTEMZ = 7,
KS_ARCH_HEXAGON = 8,
KS_ARCH_EVM = 9,
KS_ARCH_MAX = 10,
KS_ARCH_RISCV = 6,
KS_ARCH_SPARC = 7,
KS_ARCH_SYSTEMZ = 8,
KS_ARCH_HEXAGON = 9,
KS_ARCH_EVM = 10,
KS_ARCH_MAX = 11,
KS_MODE_LITTLE_ENDIAN = 0,
KS_MODE_BIG_ENDIAN = 1073741824,
KS_MODE_ARM = 1,
Expand All @@ -30,6 +31,8 @@
KS_MODE_PPC32 = 4,
KS_MODE_PPC64 = 8,
KS_MODE_QPX = 16,
KS_MODE_RISCV32 = 4,
KS_MODE_RISCV64 = 8,
KS_MODE_SPARC32 = 4,
KS_MODE_SPARC64 = 8,
KS_MODE_V9 = 16,
Expand Down
Loading

0 comments on commit db034e5

Please sign in to comment.