Introduction
- This page describes how to build Keystone eapp using the musl libc
 - As described the following issue, Keystone has some issues related to glibc initialization as of Jun. 30, 2022.
    
- Runtime Page faults when running hello.ke
 - Other libc implementation does not have this issue, such as musl libc
 
 
How to build an eapp with musl libc
- This section describes how to build hello example in keystone-sdk.
    
- It prints a message in an eapp without ocall
 
 
1. Setup musl C compiler for RISC-V
- Download pre-built toolchain
    
- [NOTICE] AS LONG AS WE TESTED, 11.2.1 (latest stable as of Jun. 30, 2022) DOES NOT WORK AS EXPECTED
        
- Specifically, 
-staticoption does not generate completely a static-linked binary 
 - Specifically, 
 
 - [NOTICE] AS LONG AS WE TESTED, 11.2.1 (latest stable as of Jun. 30, 2022) DOES NOT WORK AS EXPECTED
        
 - You can find musl-toolchains at 
<riscv64-linux-musl-cross>/bin/ 
curl -S https://more.musl.cc/10.2.1/x86_64-linux-musl/riscv64-linux-musl-cross.tgz
tar -zxf riscv64-linux-musl-cross.tgz
2. Configure keystone-sdk repository to use the musl toolchains
- You must configure the keystone-sdk to use the musl toolchains
 <keystone-sdk>/macros.cmake- We recommend you to use the absolute path in 
cross_compileor setPATHcorrectly in advance 
- We recommend you to use the absolute path in 
 
diff --git a/macros.cmake b/macros.cmake
index 34a53cd..a29a05f 100644
--- a/macros.cmake
+++ b/macros.cmake
@@ -7,7 +7,8 @@ macro(global_set Name Value)
 endmacro()
 
 macro(use_riscv_toolchain bits)
-  set(cross_compile riscv${bits}-unknown-linux-gnu-)
+  #set(cross_compile riscv${bits}-unknown-linux-gnu-)
+  set(cross_compile <riscv64-linux-musl-cross>/bin/riscv64-linux-musl-)
   execute_process(
     COMMAND which ${cross_compile}gcc
     OUTPUT_VARIABLE CROSSCOMPILE
3. Configure eapp compile settings
- As default, musl toolchains generate a dynamic-linked binariy to musl libc
    
- When you execute the binary on a platform without musl libc, you may get a 
No such file or directoryerror - We can avoid the error by static linking
 
 - When you execute the binary on a platform without musl libc, you may get a 
 <keystone-sdk>/macros.cmake
diff --git a/macros.cmake b/macros.cmake
index 34a53cd..a29a05f 100644
--- a/macros.cmake
+++ b/macros.cmake
@@ -28,7 +29,7 @@ macro(use_riscv_toolchain bits)
   set(AR              ${CROSSCOMPILE}ar)
   set(OBJCOPY         ${CROSSCOMPILE}objcopy)
   set(OBJDUMP         ${CROSSCOMPILE}objdump)
-  set(CFLAGS          "-Wall -Werror")
+  set(CFLAGS          "-Wall -Werror -static")
 
   global_set(CMAKE_C_COMPILER        ${CC}${EXT})
   global_set(CMAKE_ASM_COMPILER        ${CC}${EXT})
<keystone-sdk>/examples/hello/CMakeLists.txt
diff --git a/examples/hello/CMakeLists.txt b/examples/hello/CMakeLists.txt
index 9f5ee48..9e4c1b8 100644
--- a/examples/hello/CMakeLists.txt
+++ b/examples/hello/CMakeLists.txt
@@ -19,7 +19,7 @@ target_link_libraries(${eapp_bin} "-static")
 # host
 
 add_executable(${host_bin} ${host_src})
-target_link_libraries(${host_bin} ${KEYSTONE_LIB_HOST} ${KEYSTONE_LIB_EDGE})
+target_link_libraries(${host_bin} ${KEYSTONE_LIB_HOST} ${KEYSTONE_LIB_EDGE} "-static")
 
 # add target for Eyrie runtime (see keystone.cmake)
<keystone-sdk>/examples/tests/CMakeLists.txt(Optional)
diff --git a/examples/tests/CMakeLists.txt b/examples/tests/CMakeLists.txt
index 837013f..3b22e8d 100644
--- a/examples/tests/CMakeLists.txt
+++ b/examples/tests/CMakeLists.txt
@@ -98,7 +98,7 @@ set_target_properties(${all_test_bins}
 # host
 
 add_executable(${host_bin} ${host_src})
-target_link_libraries(${host_bin} ${KEYSTONE_LIB_HOST} ${KEYSTONE_LIB_EDGE} ${KEYSTONE_LIB_VERIFIER})
+target_link_libraries(${host_bin} ${KEYSTONE_LIB_HOST} ${KEYSTONE_LIB_EDGE} ${KEYSTONE_LIB_VERIFIER} "-static")
 
 # add target for Eyrie runtime (see keystone.cmake)
4. Build keystone-sdk and examples
- You must compile whole keystone-sdk with musl toolchains
 - Follow the keystone-sdk instructions:
 
cd `keystone-sdk`
mkdir build
cd build
export KEYSTONE_SDK_DIR=<install_directory>
cmake ..
make
make install
make examples
5. Run the hello example
- Copy the built examples into your platform, and test it
    
- On QEMU, 
overlay/root, thenmake image 
 - On QEMU,