From 2777d226588028b52336a534e958ec00fb686f89 Mon Sep 17 00:00:00 2001 From: yomaytk Date: Thu, 19 Sep 2024 17:02:21 +0900 Subject: [PATCH] Set wasm32 target triple and data layout for the generated LLVM IR. --- lifter/Lift.cpp | 14 ++++++++++++++ scripts/dev.sh | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lifter/Lift.cpp b/lifter/Lift.cpp index f5400ee..42e0943 100644 --- a/lifter/Lift.cpp +++ b/lifter/Lift.cpp @@ -42,6 +42,7 @@ DEFINE_string(arch, REMILL_ARCH, DEFINE_string(target_elf, "DUMMY_ELF", "Name of the target ELF binary"); DEFINE_string(dbg_fun_cfg, "", "Function Name of the debug target"); DEFINE_string(bitcode_path, "", "Function Name of the debug target"); +DEFINE_string(target_arch, "", "Target Architecture for conversion"); extern "C" void debug_stream_out_sigaction(int sig, siginfo_t *info, void *ctx) { std::cout << remill::ECV_DEBUG_STREAM.str(); @@ -148,6 +149,19 @@ int main(int argc, char *argv[]) { /* generate LLVM bitcode file */ auto host_arch = remill::Arch::Build(&context, os_name, remill::GetArchName(REMILL_ARCH)); host_arch->PrepareModule(module.get()); + + // Set wasm32-unknown-wasi and wasm32 data layout if necessary. + if (FLAGS_target_arch == "wasm32") { + auto wasm32_dl = + llvm::DataLayout("e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"); + module->setDataLayout(wasm32_dl.getStringRepresentation()); + llvm::Triple wasm32_triple; + wasm32_triple.setArch(llvm::Triple::wasm32); + wasm32_triple.setVendor(llvm::Triple::UnknownVendor); + wasm32_triple.setOS(llvm::Triple::WASI); + module->setTargetTriple(wasm32_triple.str()); + } + remill::StoreModuleToFile(module.get(), FLAGS_bc_out); printf("[\033[32mINFO\033[0m] Lift Done.\n"); diff --git a/scripts/dev.sh b/scripts/dev.sh index a91af7f..e0ad738 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -58,13 +58,20 @@ lifting() { # ELF -> LLVM bc echo -e "[\033[32mINFO\033[0m] ELF Converting Start." elf_path=$( realpath "$1" ) + + wasm32_target_arch='' + if [ "${TARGET}" == "wasi" ]; then + wasm32_target_arch='wasm32' + fi + cd ${BUILD_LIFTER_DIR} && \ ./elflift \ --arch aarch64 \ --bc_out ./lift.bc \ --target_elf "$elf_path" \ --dbg_fun_cfg "$2" \ - --bitcode_path "$3" && \ + --bitcode_path "$3" \ + --target_arch "$wasm32_target_arch" && \ llvm-dis-${LLVM_VERSION} lift.bc -o lift.ll echo -e "[\033[32mINFO\033[0m] Generate lift.bc."