From 365b373480a769ba6cbb7793d48472f262ebac8e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Feb 2024 22:32:22 -0800 Subject: [PATCH] Make it easy to build without crt0 --- build.py | 1 + native/src/Android.mk | 12 ++++++++---- native/src/Application.mk | 5 ++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/build.py b/build.py index ccbc9bee2..6ffcb3c62 100755 --- a/build.py +++ b/build.py @@ -421,6 +421,7 @@ def build_binary(args): if "magiskinit" in args.target: flag += " B_INIT=1" + flag += " B_CRT0=1" if flag: dump_bin_header(args) diff --git a/native/src/Android.mk b/native/src/Android.mk index 9aa178a96..47c204955 100644 --- a/native/src/Android.mk +++ b/native/src/Android.mk @@ -59,13 +59,9 @@ endif ifdef B_INIT -# -lc -lm is hardcoded in this variable, disable it -TARGET_LDLIBS := - include $(CLEAR_VARS) LOCAL_MODULE := magiskinit LOCAL_STATIC_LIBRARIES := \ - crt0 \ libbase \ libpolicy \ libxz \ @@ -80,6 +76,14 @@ LOCAL_SRC_FILES := \ init/selinux.cpp \ init/init-rs.cpp +LOCAL_LDFLAGS := -static + +ifdef B_CRT0 +# -lc -lm is hardcoded in this variable, disable it +TARGET_LDLIBS := +LOCAL_STATIC_LIBRARIES += crt0 +endif + include $(BUILD_EXECUTABLE) endif diff --git a/native/src/Application.mk b/native/src/Application.mk index 7a140e9a3..0e78996ef 100644 --- a/native/src/Application.mk +++ b/native/src/Application.mk @@ -8,14 +8,13 @@ APP_PLATFORM := android-26 APP_THIN_ARCHIVE := true APP_STRIP_MODE := none -# magiskinit uses crt0 -ifdef B_INIT +ifdef B_CRT0 # Disable all security and debugging features APP_CFLAGS += -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-stack-protector -U_FORTIFY_SOURCE APP_LDFLAGS += -Wl,-wrap,abort_message # Override output folder to make sure all dependencies are rebuilt with new CFLAGS -NDK_APP_OUT := ./obj/init +NDK_APP_OUT := ./obj/nolibc endif