[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC v6 2/9] build: Implement libnative library and the build machinery
From: |
Yeqi Fu |
Subject: |
[RFC v6 2/9] build: Implement libnative library and the build machinery for libnative |
Date: |
Wed, 13 Sep 2023 05:28:35 +0800 |
This commit implements a shared library, where native functions are
rewritten as special instructions. At runtime, user programs load
the shared library, and special instructions are executed when
native functions are called.
Signed-off-by: Yeqi Fu <fufuyqqqqqq@gmail.com>
---
Makefile | 2 ++
common-user/native/Makefile.include | 8 +++++
common-user/native/Makefile.target | 22 +++++++++++++
common-user/native/libnative.S | 51 +++++++++++++++++++++++++++++
configure | 39 ++++++++++++++++++++++
5 files changed, 122 insertions(+)
create mode 100644 common-user/native/Makefile.include
create mode 100644 common-user/native/Makefile.target
create mode 100644 common-user/native/libnative.S
diff --git a/Makefile b/Makefile
index 5d48dfac18..6f6147b40f 100644
--- a/Makefile
+++ b/Makefile
@@ -182,6 +182,8 @@ SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory --quiet)
include $(SRC_PATH)/tests/Makefile.include
+include $(SRC_PATH)/common-user/native/Makefile.include
+
all: recurse-all
ROMS_RULES=$(foreach t, all clean distclean, $(addsuffix /$(t), $(ROMS)))
diff --git a/common-user/native/Makefile.include
b/common-user/native/Makefile.include
new file mode 100644
index 0000000000..65b10fddac
--- /dev/null
+++ b/common-user/native/Makefile.include
@@ -0,0 +1,8 @@
+.PHONY: build-native
+build-native: $(NATIVE_TARGETS:%=build-native-library-%)
+$(NATIVE_TARGETS:%=build-native-library-%): build-native-library-%:
+ $(call quiet-command, \
+ $(MAKE) -C common-user/native/$* $(SUBDIR_MAKEFLAGS), \
+ "BUILD","$* native library")
+
+all: build-native
diff --git a/common-user/native/Makefile.target
b/common-user/native/Makefile.target
new file mode 100644
index 0000000000..65d90102e2
--- /dev/null
+++ b/common-user/native/Makefile.target
@@ -0,0 +1,22 @@
+# -*- Mode: makefile -*-
+#
+# Library for native calls
+#
+
+all:
+-include ../../../config-host.mak
+-include config-target.mak
+
+CFLAGS+=-shared -D $(TARGET_NAME)
+LDFLAGS+=
+
+SRC = $(SRC_PATH)/common-user/native/libnative.S
+LIBNATIVE = libnative.so
+
+all: $(LIBNATIVE)
+
+$(LIBNATIVE): $(SRC)
+ $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(EXTRA_NATIVE_CALL_FLAGS) $< -o $@
$(LDFLAGS)
+
+clean:
+ rm -f $(LIBNATIVE)
diff --git a/common-user/native/libnative.S b/common-user/native/libnative.S
new file mode 100644
index 0000000000..bc51dabedf
--- /dev/null
+++ b/common-user/native/libnative.S
@@ -0,0 +1,51 @@
+.macro special_instr sym
+#if defined(__i386__)
+ ud0 \sym-1f, %eax; 1:
+#elif defined(__x86_64__)
+ ud0 \sym(%rip), %eax
+#elif defined(__arm__) || defined(__aarch64__)
+ hlt 0xffff
+1: .word \sym - 1b
+#elif defined(__mips__)
+ syscall 0xffff
+1: .word \sym - 1b
+#else
+# error
+#endif
+.endm
+
+.macro ret_instr
+#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
+ ret
+#elif defined(__arm__)
+ bx lr
+#elif defined(__mips__)
+ jr $ra
+#else
+# error
+#endif
+.endm
+
+/* Symbols of native functions */
+
+.macro define_function name
+ .text
+\name:
+ special_instr 9f
+ ret_instr
+ .globl \name
+ .type \name, %function
+ .size \name, . - \name
+
+ .section .rodata
+9: .asciz "\name"
+.endm
+
+define_function memcmp
+define_function memcpy
+define_function memset
+define_function strcat
+define_function strcmp
+define_function strcpy
+define_function strncmp
+define_function strncpy
diff --git a/configure b/configure
index 7a1e463d9c..de533b27a2 100755
--- a/configure
+++ b/configure
@@ -1826,6 +1826,45 @@ if test "$tcg" = "enabled"; then
fi
)
+# common-user/native configuration
+(mkdir -p common-user/native
+
+native_targets=
+for target in $target_list; do
+ case $target in
+ *-softmmu)
+ continue
+ ;;
+ esac
+
+ # native call is only supported on these architectures
+ arch=${target%%-*}
+ config_target_mak=common-user/native/${target}/config-target.mak
+ case $arch in
+ i386|x86_64|arm|aarch64|mips|mips64)
+ if test -f cross-build/${target}/config-target.mak; then
+ mkdir -p "common-user/native/${target}"
+ ln -srf cross-build/${target}/config-target.mak "$config_target_mak"
+ if test $arch = arm; then
+ echo "EXTRA_NATIVE_CALL_FLAGS=-marm" >> "$config_target_mak"
+ fi
+ if test $arch = $cpu || \
+ { test $arch = i386 && test $cpu = x86_64; } || \
+ { test $arch = arm && test $cpu = aarch64; } || \
+ { test $arch = mips && test $cpu = mips64; }; then
+ echo "LD_PREFIX=/" >> "$config_target_mak"
+ fi
+ echo "LIBNATIVE=$PWD/common-user/native/${target}/libnative.so" >>
"$config_target_mak"
+ ln -sf ${source_path}/common-user/native/Makefile.target
common-user/native/${target}/Makefile
+ native_targets="$native_targets ${target}"
+ fi
+ ;;
+ esac
+done
+
+echo "NATIVE_TARGETS=$native_targets" >> config-host.mak
+)
+
if test "$skip_meson" = no; then
cross="config-meson.cross.new"
meson_quote() {
--
2.34.1
- [RFC v6 0/9] Native Library Calls, Yeqi Fu, 2023/09/12
- [RFC v6 1/9] build: Implement logic for sharing cross-building config files, Yeqi Fu, 2023/09/12
- [RFC v6 2/9] build: Implement libnative library and the build machinery for libnative,
Yeqi Fu <=
- [RFC v6 3/9] linux-user: Implement native-bypass option support, Yeqi Fu, 2023/09/12
- [RFC v6 4/9] tcg: Add tcg opcodes and helpers for native library calls, Yeqi Fu, 2023/09/12
- [RFC v6 7/9] target/arm: Add support for native library calls, Yeqi Fu, 2023/09/12
- [RFC v6 8/9] tests/tcg/multiarch: Add nativecall.c test, Yeqi Fu, 2023/09/12
- [RFC v6 5/9] target/i386: Add support for native library calls, Yeqi Fu, 2023/09/12
- [RFC v6 6/9] target/mips: Add support for native library calls, Yeqi Fu, 2023/09/12
- [RFC v6 9/9] docs/user: Add doc for native library calls, Yeqi Fu, 2023/09/12