From 99c5af7a76a5dc9710a4815a02c7b27717404897 Mon Sep 17 00:00:00 2001 From: m4b Date: Tue, 25 Apr 2017 21:52:15 -0700 Subject: [PATCH] build, asm: add libasm for compiling asm functions in one place and with only rust :tada: instead of using gcc + build.rs --- Cargo.toml | 5 +- Makefile | 6 ++- asm/Cargo.toml | 8 +++ asm/src/lib.rs | 129 +++++++++++++++++++++++++++++++++++++++++++++++++ build.rs | 38 --------------- 5 files changed, 146 insertions(+), 40 deletions(-) create mode 100644 asm/Cargo.toml create mode 100644 asm/src/lib.rs delete mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 32a4735..b4ec9e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,6 @@ name = "dryad" version = "0.1.2" authors = ["m4b "] -build = "build.rs" [lib] crate-type = ["staticlib"] @@ -24,3 +23,7 @@ color = ["colorify"] version = "0.0.9" default-features = false features = ["elf64", "elf32", "std"] + +[dependencies.asm] +version = "0.1" +path = "asm" diff --git a/Makefile b/Makefile index 4acd302..2ef60ad 100644 --- a/Makefile +++ b/Makefile @@ -60,16 +60,18 @@ LIBDRYAD=libdryad.a LINK_ARGS := -pie --gc-sections -Bsymbolic --dynamic-list=${ETC}/dynamic-list.txt -I${PT_INTERP} -soname ${SONAME} -L$(USRLIB) -nostdlib -e _start +LIBSTART=$(wildcard $(OUT_DIR)/deps/libasm-*) dryad.so.1: $(OUT_DIR)/$(LIBDRYAD) @printf "\33[0;4;33mlinking:\33[0m \33[0;32m$(SONAME)\33[0m with $(HASH)\n" $(LD) ${LINK_ARGS}\ -o ${SONAME}\ + $(LIBSTART)\ ${OUT_DIR}/$(LIBDRYAD)\ ${RUSTLIBS}\ ${CARGO_DEPS} cp ${SONAME} /tmp -$(OUT_DIR)/$(LIBDRYAD): $(SRC) +$(OUT_DIR)/$(LIBDRYAD): $(SRC) asm/src/lib.rs @printf "\33[0;4;33mcompiling:\33[0m \33[1;32mdryad\33[0m\n" CC=$(CC) AR=$(AR) $(CARGO) rustc $(COLOR) -vv --verbose --target=$(TRIPLE) --lib -j 4 -- -C panic=abort #-C lto # uncomment this when lto is important @@ -103,11 +105,13 @@ tests: ${TESTS} # for testing, debugging, etc. +LIBSTART=$(wildcard $(OUT_DIR)/deps/libasm-*) link: @printf "\33[0;4;33mlinking:\33[0m \33[0;32m$(SONAME)\33[0m with $(HASH)\n" $(LD) -Map=${ETC}/dryad.map\ ${LINK_ARGS}\ -o ${SONAME}\ + $(LIBSTART)\ ${OUT_DIR}/$(LIBDRYAD)\ ${RUSTLIBS}\ ${CARGO_DEPS} diff --git a/asm/Cargo.toml b/asm/Cargo.toml new file mode 100644 index 0000000..c9f992e --- /dev/null +++ b/asm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "asm" +version = "0.1.0" +authors = ["m4b "] +#workspace = ".." + +[lib] +crate_type = ["staticlib"] diff --git a/asm/src/lib.rs b/asm/src/lib.rs new file mode 100644 index 0000000..4d046ac --- /dev/null +++ b/asm/src/lib.rs @@ -0,0 +1,129 @@ +#![feature(asm)] +#![feature(naked_functions)] + +#[no_mangle] +// pub extern fn _dryad_fini() { +// leaq (%rip), %rax +// retq + +#[no_mangle] +#[naked] +pub extern fn _start() { + // TODO: do stupid i386 thunk asm nonsense + #[cfg(target_arch = "x86")] + unsafe { + asm!(" + mov %esp, %edi + and $$~15, %esp + call dryad_init + mov $0, %edx + jmp *%eax + " + ); + } + #[cfg(target_arch = "x86_64")] + unsafe { + asm!(" + mov %rsp, %rdi + andq $$~15, %rsp + callq dryad_init + movq $$0, %rdx + jmpq *%rax + " + ); + } + #[cfg(target_arch = "arm")] + unsafe { + asm!(" + mov r0, sp + bl dryad_init + mov pc, r0 + bx pc + " + ); + } + #[cfg(target_arch = "arm64")] + unsafe { + asm!(" + mov x0, sp + bl dryad_init + br x0 + "); + } +} + +#[no_mangle] +pub extern fn _dryad_resolve_symbol () { + #[cfg(target_arch = "x86")] + unsafe { + asm!(" + "); + } + #[cfg(target_arch = "x86_64")] + unsafe { + asm!(" + sub $$0x180,%rsp + mov %rax,0x140(%rsp) + mov %rcx,0x148(%rsp) + mov %rdx,0x150(%rsp) + mov %rsi,0x158(%rsp) + mov %rdi,0x160(%rsp) + mov %r8,0x168(%rsp) + mov %r9,0x170(%rsp) + vmovdqa %ymm0,(%rsp) + vmovdqa %ymm1,0x20(%rsp) + vmovdqa %ymm2,0x40(%rsp) + vmovdqa %ymm3,0x60(%rsp) + vmovdqa %ymm4,0x80(%rsp) + vmovdqa %ymm5,0xa0(%rsp) + vmovdqa %ymm6,0xc0(%rsp) + vmovdqa %ymm7,0xe0(%rsp) + bndmov %bnd0,0x100(%rsp) + bndmov %bnd1,0x110(%rsp) + bndmov %bnd2,0x120(%rsp) + bndmov %bnd3,0x130(%rsp) + mov 0x10(%rbx),%rsi + mov 0x8(%rbx),%rdi + callq dryad_resolve_symbol + mov %rax,%r11 + bndmov 0x130(%rsp),%bnd3 + bndmov 0x120(%rsp),%bnd2 + bndmov 0x110(%rsp),%bnd1 + bndmov 0x100(%rsp),%bnd0 + mov 0x170(%rsp),%r9 + mov 0x168(%rsp),%r8 + mov 0x160(%rsp),%rdi + mov 0x158(%rsp),%rsi + mov 0x150(%rsp),%rdx + mov 0x148(%rsp),%rcx + mov 0x140(%rsp),%rax + vmovdqa (%rsp),%ymm0 + vmovdqa 0x20(%rsp),%ymm1 + vmovdqa 0x40(%rsp),%ymm2 + vmovdqa 0x60(%rsp),%ymm3 + vmovdqa 0x80(%rsp),%ymm4 + vmovdqa 0xa0(%rsp),%ymm5 + vmovdqa 0xc0(%rsp),%ymm6 + vmovdqa 0xe0(%rsp),%ymm7 + mov %rbx,%rsp + mov (%rsp),%rbx + add $$0x18,%rsp + jmpq *%r11 + " + ); + } + #[cfg(target_arch = "arm")] + unsafe { + asm!(" + and sp, #-15 + bl dryad_resolve_symbol + "); + } + #[cfg(target_arch = "arm64")] + unsafe { + asm!(" + bl dryad_resolve_symbol + "); + } +} + diff --git a/build.rs b/build.rs deleted file mode 100644 index 8f637df..0000000 --- a/build.rs +++ /dev/null @@ -1,38 +0,0 @@ -extern crate gcc; - -use std::env; - -fn main () { - - let target = env::var("TARGET").unwrap(); - println!("target: {}", target); - - let (target, arch) = match target.as_str() { - "i686-unknown-linux-gnu" => { - ("i686-unknown-linux-gnu", "x86") - }, - "x86_64-unknown-linux-gnu" => { - ("x86_64-unknown-linux-gnu", "x86_64") - }, - "x86_64-unknown-linux-musl" => { - ("x86_64-unknown-linux-gnu", "x86_64") - }, - "i686-unknown-linux-musl" => { - ("i686-unknown-linux-gnu", "x86") - }, - target => { - if target.contains("aarch64") { - (target, "arm64") - } else if target.contains("arm"){ - (target, "arm") - } else { - panic!(format!("Unsupported target architecture: {}", target)) - } - }, - }; - - gcc::Config::new() - .file(format!("src/arch/{}/asm.s", arch)) - .target(target) - .compile("libstart.a"); -}