Skip to content

Commit

Permalink
Merge pull request #31 from yomaytk/release
Browse files Browse the repository at this point in the history
Organize the process of system calls emulation.
  • Loading branch information
yomaytk authored Apr 4, 2024
2 parents 4453df6 + 4572121 commit 9bd6460
Show file tree
Hide file tree
Showing 13 changed files with 727 additions and 147 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ obj-intel64/*
VERSION

examples/w2c
examples/tests
examples/tests

# release
SHA*
elfconv-v*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and elfconv uses [emscripten](https://github.com/emscripten-core/emscripten) (fo
- Furthermore, a part of aarch64 instructions are not supported. if the instruction of your ELF binary is not supported, elfconv outputs the message (\[WARNING\] Unsupported instruction at 0x...)
- No support of stripped binaries
- No support of shared objects
- a lot of Linux system calls are unimplemented(ref: [`runtime/Syscall.cpp`](https://github.com/yomaytk/elfconv/blob/main/runtime/Syscall.cpp))
- a lot of Linux system calls are unimplemented(ref: [`runtime/syscalls`](https://github.com/yomaytk/elfconv/blob/main/runtime/syscalls))
## Quick Start
You can try elfconv using docker container (amd64 and arm64) by executing the commands as follows.
In default settings, both `elflift` (used for generating LLVM bitcode file) and `libelfconv.a` (used for executing generated LLVM bitcode) are installed to `~/.elfconv`.
Expand Down
6 changes: 4 additions & 2 deletions bin/elfconv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ setting() {
EMAR=emar
OPTFLAGS="-O3"
EMCCFLAGS="${OPTFLAGS} -I${ROOT_DIR}/backend/remill/include -I${ROOT_DIR}"
ELFCONV_MACROS="-DELFCONV_BROWSER_ENV=1"
ELFCONV_MACROS="-DELFC_BROWSER_ENV=1"
ELFCONV_DEBUG_MACROS=
ELFPATH=$( realpath "$1" )
WASMCC=$EMCC
Expand All @@ -39,17 +39,19 @@ main() {
fi

# build runtime
SYSCALLCPP="syscall/SyscallBrowser.cpp"
echo -e "[\033[32mINFO\033[0m] Building elfconv-Runtime ..."
if [ "$TARGET" = "wasm-host" ]; then
WASMCC=$WASISDKCXX
WASMCCFLAGS=$WASISDKFLAGS
WASMAR=$WASISDKAR
SYSCALLCPP="syscall/SyscallWasi.cpp"
fi
cd "${RUNTIME_DIR}" || { echo "cd Failure"; exit 1; }
# shellcheck disable=SC2086
$WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Entry.o -c Entry.cpp && \
$WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Memory.o -c Memory.cpp && \
$WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Syscall.o -c Syscall.cpp && \
$WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Syscall.o -c $SYSCALLCPP && \
$WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o VmIntrinsics.o -c VmIntrinsics.cpp && \
$WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o Util.o -c "${UTILS_DIR}"/Util.cpp && \
$WASMCC $WASMCCFLAGS $ELFCONV_MACROS $ELFCONV_DEBUG_MACROS -o elfconv.o -c "${UTILS_DIR}"/elfconv.cpp && \
Expand Down
2 changes: 1 addition & 1 deletion release/elfconv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ setting() {
WASMCC=emcc
WASISDKCXX=${WASI_SDK_PATH}/bin/clang++
OPTFLAGS="-O3"
ELFCONV_MACROS="-DELFCONV_BROWSER_ENV=1"
ELFCONV_MACROS="-DELFC_BROWSER_ENV=1"
ELFPATH=$( realpath "$1" )
BITCODEPATH=$( realpath "$2" )

Expand Down
6 changes: 3 additions & 3 deletions release/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ setting() {
EMCXX=emcc
EMAR=emar
EMCCFLAGS="${OPTFLAGS} -I${ELFCONV_DIR}/backend/remill/include -I${ELFCONV_DIR}"
EMCC_ELFCONV_MACROS="-DELFCONV_BROWSER_ENV=1"
EMCC_ELFCONV_MACROS="-DELFC_BROWSER_ENV=1"

# wasi-sdk
WASISDKCXX=${WASI_SDK_PATH}/bin/clang++
Expand Down Expand Up @@ -62,7 +62,7 @@ main() {
# shellcheck disable=SC2086
$EMCXX $EMCCFLAGS $EMCC_ELFCONV_MACROS -o Entry.o -c Entry.cpp && \
$EMCXX $EMCCFLAGS $EMCC_ELFCONV_MACROS -o Memory.o -c Memory.cpp && \
$EMCXX $EMCCFLAGS $EMCC_ELFCONV_MACROS -o Syscall.o -c Syscall.cpp && \
$EMCXX $EMCCFLAGS $EMCC_ELFCONV_MACROS -o Syscall.o -c syscalls/SyscallBrowser.cpp && \
$EMCXX $EMCCFLAGS $EMCC_ELFCONV_MACROS -o VmIntrinsics.o -c VmIntrinsics.cpp && \
$EMCXX $EMCCFLAGS $EMCC_ELFCONV_MACROS -o Util.o -c "${UTILS_DIR}"/Util.cpp && \
$EMCXX $EMCCFLAGS $EMCC_ELFCONV_MACROS -o elfconv.o -c "${UTILS_DIR}"/elfconv.cpp && \
Expand All @@ -80,7 +80,7 @@ main() {
# shellcheck disable=SC2086
$WASISDKCXX $WASISDKFLAGS $WASI_ELFCONV_MACROS -o Entry.o -c Entry.cpp && \
$WASISDKCXX $WASISDKFLAGS $WASI_ELFCONV_MACROS -o Memory.o -c Memory.cpp && \
$WASISDKCXX $WASISDKFLAGS $WASI_ELFCONV_MACROS -o Syscall.o -c Syscall.cpp && \
$WASISDKCXX $WASISDKFLAGS $WASI_ELFCONV_MACROS -o Syscall.o -c syscalls/SyscallWasi.cpp && \
$WASISDKCXX $WASISDKFLAGS $WASI_ELFCONV_MACROS -o VmIntrinsics.o -c VmIntrinsics.cpp && \
$WASISDKCXX $WASISDKFLAGS $WASI_ELFCONV_MACROS -o Util.o -c "${UTILS_DIR}"/Util.cpp && \
$WASISDKCXX $WASISDKFLAGS $WASI_ELFCONV_MACROS -o elfconv.o -c "${UTILS_DIR}"/elfconv.cpp && \
Expand Down
4 changes: 3 additions & 1 deletion runtime/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class MappedMemory;
class RuntimeManager;

/* own implementation of syscall emulation */
extern void __svc_call();
extern void __svc_native_call();
extern void __svc_browser_call();
extern void __svc_wasi_call();
/* translate the address of the original ELF to the actual address of mapped space */
extern void *_ecv_translate_ptr(addr_t vma_addr);
extern "C" uint64_t *__g_get_indirectbr_block_address(uint64_t fun_vma, uint64_t bb_vma);
Expand Down
8 changes: 7 additions & 1 deletion runtime/VmIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ Memory *__remill_write_memory_f128(Memory *, addr_t, float128_t) {
*/
Memory *__remill_syscall_tranpoline_call(State &state, Memory *memory) {
/* TODO: We should select one syscall emulate process (own implementation, WASI, LKL, etc...) */
__svc_call();
#if defined(ELFC_WASI_ENV)
__svc_wasi_call();
#elif defined(ELFC_BROWSER_ENV)
__svc_browser_call();
#else
__svc_native_call();
#endif
return nullptr;
}

Expand Down
41 changes: 41 additions & 0 deletions runtime/syscalls/SysTable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#define _ECV_EACCESS 13
#define _ECV_ENOSYS 38
/*
syscall number table
*/
#define AARCH64_SYS_IOCTL 29
#define AARCH64_SYS_FACCESSAT 48
#define AARCH64_SYS_OPENAT 56
#define AARCH64_SYS_CLOSE 57
#define AARCH64_SYS_READ 63
#define AARCH64_SYS_WRITE 64
#define AARCH64_SYS_WRITEV 66
#define AARCH64_SYS_READLINKAT 78
#define AARCH64_SYS_NEWFSTATAT 79
#define AARCH64_SYS_EXIT 93
#define AARCH64_SYS_EXITGROUP 94
#define AARCH64_SYS_SET_TID_ADDRESS 96
#define AARCH64_SYS_FUTEX 98
#define AARCH64_SYS_SET_ROBUST_LIST 99
#define AARCH64_SYS_CLOCK_GETTIME 113
#define AARCH64_SYS_TGKILL 131
#define AARCH64_SYS_RT_SIGACTION 134
#define AARCH64_SYS_RT_SIGPROCMASK 135
#define AARCH64_SYS_UNAME 160
#define AARCH64_SYS_GETPID 172
#define AARCH64_SYS_GETPPID 173
#define AARCH64_SYS_GETTUID 174
#define AARCH64_SYS_GETEUID 175
#define AARCH64_SYS_GETGID 176
#define AARCH64_SYS_GETEGID 177
#define AARCH64_SYS_GETTID 178
#define AARCH64_SYS_BRK 214
#define AARCH64_SYS_MUNMAP 215
#define AARCH64_SYS_MMAP 222
#define AARCH64_SYS_MPROTECT 226
#define AARCH64_SYS_PRLIMIT64 261
#define AARCH64_SYS_GETRANDOM 278
#define AARCH64_SYS_STATX 291
#define AARCH64_SYS_RSEQ 293
Loading

0 comments on commit 9bd6460

Please sign in to comment.