Skip to content

Commit

Permalink
build, asm: add libasm for compiling asm functions in one place and w…
Browse files Browse the repository at this point in the history
…ith only rust 🎉 instead of using gcc + build.rs
  • Loading branch information
m4b committed Apr 26, 2017
1 parent f14d375 commit 99c5af7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 40 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
name = "dryad"
version = "0.1.2"
authors = ["m4b <[email protected]>"]
build = "build.rs"

[lib]
crate-type = ["staticlib"]
Expand All @@ -24,3 +23,7 @@ color = ["colorify"]
version = "0.0.9"
default-features = false
features = ["elf64", "elf32", "std"]

[dependencies.asm]
version = "0.1"
path = "asm"
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}
Expand Down
8 changes: 8 additions & 0 deletions asm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "asm"
version = "0.1.0"
authors = ["m4b <[email protected]>"]
#workspace = ".."

[lib]
crate_type = ["staticlib"]
129 changes: 129 additions & 0 deletions asm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
");
}
}

38 changes: 0 additions & 38 deletions build.rs

This file was deleted.

0 comments on commit 99c5af7

Please sign in to comment.