Skip to content

Commit

Permalink
Enable capstone build for Win.
Browse files Browse the repository at this point in the history
Also enable keystone build for win, and disassembler wrapper. Update
capstone to 5.0 version.
  • Loading branch information
Vipon committed Oct 16, 2023
1 parent 4cfc7ec commit 3c9a382
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 62 deletions.
4 changes: 1 addition & 3 deletions cTools/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ if (NOT APPLE)
add_subdirectory(perfAnalysis)
endif (NOT APPLE)
add_subdirectory(test)
if (NOT WIN32)
add_subdirectory(x86_64)
endif (NOT WIN32)
add_subdirectory(x86_64)

26 changes: 24 additions & 2 deletions cTools/libs/x86_64/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# MIT License
#
# Copyright (c) 2023 Konychev Valerii
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

set(X86_64_HEADERS
x86_64.h
x86_64_DisassemblerWrap.h
Expand All @@ -10,10 +32,10 @@ set(X86_64_SOURCES

add_vipon_library(
NAME asmWrap
TYPE SHARED
TYPE ${VIPON_TOOLS_LIB_TYPE}
HEADERS ${X86_64_HEADERS}
SOURCES ${X86_64_SOURCES}
LINK_LIBS capstone keystone mem
LINK_LIBS capstone mem
INSTALL ON
)

Expand Down
5 changes: 3 additions & 2 deletions cTools/libs/x86_64/x86_64.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/***
* MIT License
*
* Copyright (c) 2020-2021 Konychev Valera
* Copyright (c) 2020-2023 Konychev Valera
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -343,7 +343,8 @@ extern const uint8_t NOP_7_BYTES[];
#define SIZE_NOP_7_BYTES 7
extern const uint8_t NOP_8_BYTES[];
#define SIZE_NOP_8_BYTES 8

extern const uint8_t NOP_9_BYTES[];
#define SIZE_NOP_9_BYTES 9

/*
* Description:
Expand Down
37 changes: 14 additions & 23 deletions cTools/libs/x86_64/x86_64_CapstoneWrap.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/***
* MIT License
*
* Copyright (c) 2020-2021 Konychev Valera
* Copyright (c) 2020-2023 Konychev Valera
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,23 +24,16 @@

#include "bits.h"
#include "comdef.h"
#include "string.h"
#include "x86_64.h"
#include "x86_64_DisassemblerWrap.h"

#include <capstone/capstone.h>

#ifdef __STDC__
#if __STDC__ == 1
#include <string.h>
#else
#error "*** ERROR: Need standard C library or equivalent. ***"
#endif /* __STDC__ == 1 */
#endif /* __STDC__ */

typedef csh x86_64_disasm;

static uint8_t init = 0;
x86_64_disasm disasm;
static x86_64_disasm disasm;


ERR_DISASM_WRAP init_x86_64_disassembler(void)
Expand Down Expand Up @@ -147,8 +140,7 @@ const char *get_disasmwrap_error_string(ERR_DISASM_WRAP err)
}


#ifdef __STDC__
#if __STDC__ == 1
#if (__STDC__ == 1) || (__STDC_HOSTED__ == 1)
void print_x86_instr(FILE *f, const x86_64_instr *insn)
{
LOG("start print_x86_instr\n");
Expand Down Expand Up @@ -208,7 +200,7 @@ void print_x86_instr(FILE *f, const x86_64_instr *insn)

case X86_OP_REG:
fprintf(f, "X86_OP_REG\n");
fprintf(f, "\t\treg:\n\t\t\t%s\n", cs_reg_name(disasm, x86_op[i].reg));
fprintf(f, "\t\treg:\n\t\t\t%s\n", cs_reg_name(disasm, (unsigned)x86_op[i].reg));
break;

case X86_OP_IMM:
Expand All @@ -219,20 +211,20 @@ void print_x86_instr(FILE *f, const x86_64_instr *insn)
case X86_OP_MEM:
fprintf(f, "X86_OP_MEM\n");
if (x86_op[i].mem.base != X86_REG_INVALID)
fprintf(f, "\t\tbase:\n\t\t\t%s\n", cs_reg_name(disasm, x86_op[i].mem.base));
fprintf(f, "\t\tbase:\n\t\t\t%s\n", cs_reg_name(disasm, (unsigned)x86_op[i].mem.base));
if (x86_op[i].mem.index != X86_REG_INVALID) {
fprintf(f, "\t\tindex:\n\t\t\t%s\n", cs_reg_name(disasm, x86_op[i].mem.index));
fprintf(f, "\t\tindex:\n\t\t\t%s\n", cs_reg_name(disasm, (unsigned)x86_op[i].mem.index));
fprintf(f, "\t\tscale:\n\t\t\t%d\n", x86_op[i].mem.scale);
}

fprintf(f, "\t\tdisp:\n\t\t\t%"PRIx64"\n", x86_op[i].mem.disp);
break;

/*
case X86_OP_FP:
fprintf(f, "X86_OP_FP\n");
fprintf(f, "\t\tfp_val:\n\t\t\t%lf\n", x86_op[i].fp);
break;

*/
default:
break;
}
Expand All @@ -241,8 +233,7 @@ void print_x86_instr(FILE *f, const x86_64_instr *insn)

LOG("end print_x86_instr\n");
}
#endif /* __STDC__ == 1 */
#endif /* __STDC__ */
#endif /* __STDC__ == 1 */


void get_instr_mnemonic(char *com, const x86_64_instr *insn)
Expand Down Expand Up @@ -410,7 +401,7 @@ uint8_t get_modrm_size(const x86_64_instr *insn)
uint8_t reg_op_count = 0;
for (i = 0; i < x86->op_count; ++i) {
if (x86->operands[i].type == X86_OP_MEM
|| x86->operands[i].type == X86_OP_FP) {
/*|| x86->operands[i].type == X86_OP_FP*/) {
modrm_size = 1;
break;
}
Expand Down Expand Up @@ -584,7 +575,7 @@ uint32_t get_vex_prefix(const x86_64_instr *insn)
uint8_t get_rex_prefix(const x86_64_instr *insn)
{
LOG("start get_rex_prefix\n");
uint32_t rex_prefix = 0;
uint8_t rex_prefix = 0;
cs_x86 *x86 = &(insn->detail->x86);

if (get_rex_prefix_size(insn))
Expand Down Expand Up @@ -656,13 +647,13 @@ int32_t get_disp(const x86_64_instr *insn)
/* Capstone bugs: Disasm in these cases make insn->disp = 0. */
/* call/jmp rel8/32 */
if (get_modrm_size(insn))
disp = x86->disp;
disp = (int32_t)x86->disp;
else
disp = (int32_t)((uint64_t)get_imm(insn) - insn->address - insn->size);
break;

default:
disp = x86->disp;
disp = (int32_t)x86->disp;
break;
}

Expand Down
18 changes: 8 additions & 10 deletions cTools/libs/x86_64/x86_64_DisassemblerWrap.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/***
* MIT License
*
* Copyright (c) 2020-2021 Konychev Valera
* Copyright (c) 2020-2023 Konychev Valera
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -49,11 +49,13 @@
#error "*** ERROR: Unknown disassembler. ***"
#endif /* CAPSTONE_DISASSEMBLER */

#ifdef __STDC__
#if __STDC__ == 1
#ifndef __STDC__
# define __STDC__ 0
#endif /* __STDC__ */

#if (__STDC__ == 1) || (__STDC_HOSTED__ == 1)
#include <stdio.h>
#include <stdint.h>
#endif /* __STDC__ == 1 */
#else
#error "*** ERROR: Need standard C library or equivalent. ***"
#endif /* __STDC__ */
Expand Down Expand Up @@ -116,16 +118,15 @@ int get_instr(x86_64_instr *insn, const uint8_t *addr, uint64_t ip);
const char *get_disasmwrap_error_string(ERR_DISASM_WRAP err);


#ifdef __STDC__
#if __STDC__ == 1
#if (__STDC__ == 1) || (__STDC_HOSTED__ == 1)
/*
* Description:
* Function inits global structure disasm.
* Input:
* @insn - pointer to descriptor of instruction should be printed.
*/
void print_x86_instr(FILE *f, const x86_64_instr *insn);

#endif /* (__STDC__ == 1) || (__STDC_HOSTED__ == 1) */

/*
* Description:
Expand All @@ -136,9 +137,6 @@ void print_x86_instr(FILE *f, const x86_64_instr *insn);
* @com - pointer to array of chars.
*/
void get_instr_mnemonic(char *com, const x86_64_instr *insn);
#endif /* __STDC__ == 1 */
#endif /* __STDC__ */


/*
* Description:
Expand Down
13 changes: 11 additions & 2 deletions cmake/toolchain/cflags/warnings.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License
#
# Copyright (c) 2021 Konychev Valerii
# Copyright (c) 2021-2023 Konychev Valerii
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -58,13 +58,22 @@ if (MSVC)
-Wno-old-style-cast
-Wno-float-equal
-Wno-c++98-compat-pedantic
-Wno-unsafe-buffer-usage

)

message(STATUS "msvc_version: ${MSVC_VERSION}")
if (MSVC_VERSION GREATER_EQUAL "1930") # VS17
append_cflags(WARNING_FLAGS
-Wno-reserved-identifier
-Wno-cast-function-type
-Wno-documentation
-Wno-microsoft-enum-value
-Wno-duplicate-enum
)
endif ()
if (MSVC_VERSION GREATER_EQUAL "1934") # VS17.7.2
append_cflags(WARNING_FLAGS
-Wno-unsafe-buffer-usage
)
endif ()
endif (MSVC)
Expand Down
3 changes: 2 additions & 1 deletion external/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include
lib
lib
bin
6 changes: 5 additions & 1 deletion python/setupEnv/depsTests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ tools:
- cmake
- ninja
- capstone:
ver: 3.0.5
ver: "5.0"
deps: cmake
dir:
win: ./../../external
- keystone:
ver: 0.9.2
deps: cmake
dir:
win: ./../../external

libs:
argp:
Expand Down
19 changes: 12 additions & 7 deletions python/setupEnv/tools/capstone/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from vpy.installArgs import parseInstallArgs
import vpy.installArgs as vpy

vpy.INSTALL_VERSION = '3.0.5'
vpy.INSTALL_VERSION = '5.0'

CAPSTONE_SRC_URL = None
CAPSTONE_SRC_ROOT = None
Expand Down Expand Up @@ -78,21 +78,26 @@ def buildAndInstallCapstone():
, f'-DCMAKE_INSTALL_PREFIX={vpy.INSTALL_PREFIX}'
, '..'
]
execCmd(cmd)
env = {'CFLAGS': '-fpic'}
execCmd(cmd, env)

cmd = [ 'make', '-j' ]
if isWin():
cmd = [ 'cmake', '--build', '.', '--config', 'Release']
else:
cmd = [ 'make', '-j' ]
execCmd(cmd)

cmd = [ 'make', 'install' ]
if isWin():
cmd = [ 'cmake', '--install', '.', '--config', 'Release']
else:
cmd = [ 'make', 'install' ]
execCmd(cmd)

os.chdir(cwd)

def main():
if isWin():
return

args = parseArgs()

if not args.default:
downloadSrcCapstone()
extractSrcCapstone()
Expand Down
20 changes: 12 additions & 8 deletions python/setupEnv/tools/keystone/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
from vpy.installArgs import parseInstallArgs
import vpy.installArgs as vpy

vpy.INSTALL_VERSION = '3.0.5'
vpy.INSTALL_VERSION = '0.9.2'

KEYSTONE_SRC_URL = None
KEYSTONE_SRC_ROOT = None
KEYSTONE_BUILD_DIR = None
KEYSTONE_SRC_ARCHIVE = None

def parseArgs():
args = parseInstallArgs('Install capstone.')
args = parseInstallArgs('Install keystone.')

global KEYSTONE_SRC_URL
KEYSTONE_SRC_URL = f'https://github.com/keystone-engine/keystone/archive/refs/tags/{vpy.INSTALL_VERSION}.zip'
Expand Down Expand Up @@ -80,25 +80,29 @@ def buildAndInstallKeystone():
]
execCmd(cmd)

cmd = [ 'make', '-j' ]
if isWin():
cmd = [ 'cmake', '--build', '.', '--config', 'Release']
else:
cmd = [ 'make', '-j' ]
execCmd(cmd)

cmd = [ 'make', 'install' ]
if isWin():
cmd = [ 'cmake', '--install', '.', '--config', 'Release']
else:
cmd = [ 'make', 'install' ]
execCmd(cmd)

os.chdir(cwd)

def main():
if isWin():
return

args = parseArgs()

if not args.default:
downloadSrcKeystone()
extractSrcKeystone()
buildAndInstallKeystone()
else:
installPkg('capstone')
installPkg('keystone')

if __name__ == '__main__':
main()
Expand Down
Loading

0 comments on commit 3c9a382

Please sign in to comment.