Build magiskboot with crt0

This commit is contained in:
topjohnwu 2024-02-29 02:36:05 -08:00
parent b1297c4192
commit 24e46a5971
8 changed files with 61 additions and 31 deletions

View File

@ -400,31 +400,27 @@ def build_binary(args):
flag += " B_POLICY=1" flag += " B_POLICY=1"
clean = True clean = True
if "test" in args.target:
flag += " B_TEST=1"
if "magiskinit" in args.target: if "magiskinit" in args.target:
flag += " B_PRELOAD=1" flag += " B_PRELOAD=1"
if "resetprop" in args.target: if "resetprop" in args.target:
flag += " B_PROP=1" flag += " B_PROP=1"
if "magiskboot" in args.target:
flag += " B_BOOT=1"
if flag: if flag:
run_ndk_build(flag) run_ndk_build(flag)
# magiskinit embeds preload.so
flag = "" flag = ""
if "magiskinit" in args.target: if "magiskinit" in args.target:
# magiskinit embeds preload.so
dump_bin_header(args)
flag += " B_INIT=1" flag += " B_INIT=1"
flag += " B_CRT0=1"
if "magiskboot" in args.target:
flag += " B_BOOT=1"
if flag: if flag:
dump_bin_header(args) flag += " B_CRT0=1"
run_ndk_build(flag) run_ndk_build(flag)
if clean: if clean:

View File

@ -22,7 +22,8 @@ android {
ndkBuild { ndkBuild {
// Pass arguments to ndk-build. // Pass arguments to ndk-build.
arguments( arguments(
"B_MAGISK=1", "B_INIT=1", "B_BOOT=1", "B_TEST=1", "B_POLICY=1", "B_PRELOAD=1", "B_PROP=1" "B_MAGISK=1", "B_INIT=1", "B_BOOT=1", "B_POLICY=1",
"B_PRELOAD=1", "B_PROP=1", "B_CRT0=1"
) )
} }
} }

View File

@ -109,6 +109,12 @@ LOCAL_SRC_FILES := \
LOCAL_LDFLAGS := -static -T src/lto_fix.lds LOCAL_LDFLAGS := -static -T src/lto_fix.lds
ifdef B_CRT0
LOCAL_STATIC_LIBRARIES += crt0
LOCAL_CFLAGS += -DUSE_MUSL_PRINTF
LOCAL_LDFLAGS := -lm -Wl,--wrap=qsort
endif
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)
endif endif

View File

@ -75,12 +75,8 @@ void file_readline(bool trim, FILE *fp, const function<bool(string_view)> &fn) {
} }
void file_readline(bool trim, const char *file, const function<bool(string_view)> &fn) { void file_readline(bool trim, const char *file, const function<bool(string_view)> &fn) {
int fd = xopen(file, O_RDONLY | O_CLOEXEC); if (auto fp = open_file(file, "re"))
if (fd >= 0) { file_readline(trim, fp.get(), fn);
auto fp = fdopen(fd, "re");
file_readline(trim, fp, fn);
fclose(fp);
}
} }
void file_readline(const char *file, const function<bool(string_view)> &fn) { void file_readline(const char *file, const function<bool(string_view)> &fn) {
@ -101,12 +97,8 @@ void parse_prop_file(FILE *fp, const function<bool(string_view, string_view)> &f
} }
void parse_prop_file(const char *file, const function<bool(string_view, string_view)> &fn) { void parse_prop_file(const char *file, const function<bool(string_view, string_view)> &fn) {
int fd = xopen(file, O_RDONLY | O_CLOEXEC); if (auto fp = open_file(file, "re"))
if (fd >= 0) { parse_prop_file(fp.get(), fn);
auto fp = fdopen(fd, "re");
parse_prop_file(fp, fn);
fclose(fp);
}
} }
std::vector<mount_info> parse_mount_info(const char *pid) { std::vector<mount_info> parse_mount_info(const char *pid) {

View File

@ -290,3 +290,37 @@ const char *rust::Utf8CStr::data() const {
size_t rust::Utf8CStr::length() const { size_t rust::Utf8CStr::length() const {
return cxx$utf8str$len(this); return cxx$utf8str$len(this);
} }
#define elm(i) (p + (i * size))
// An alternative qsort implementation. Only used when linking with crt0
extern "C"
void __wrap_qsort(void *ptr, size_t count, size_t size, int (*comp)(const void*, const void*)) {
// Create the index array
uint8_t *p = (uint8_t *) ptr;
vector<int> v(count);
std::iota(v.begin(), v.end(), 0);
// Sort the index array
std::sort(v.begin(), v.end(), [=](int a, int b) {
return comp(elm(a), elm(b)) < 0;
});
// Reorganize the array with index array
void *t = malloc(size);
for (int i = 0; i < count; ++i) {
if (v[i] != i) {
memcpy(t, elm(i), size);
int j = i;
int k;
while (i != (k = v[j])) {
memcpy(elm(j), elm(k), size);
v[j] = j;
j = k;
}
memcpy(elm(j), t, size);
v[j] = j;
}
}
free(t);
}

View File

@ -6,6 +6,11 @@
using namespace std; using namespace std;
#ifdef USE_MUSL_PRINTF
// Switch to use the musl vfprintf
__asm__(".global vfprintf \n vfprintf = musl_vfprintf");
#endif
static void print_formats() { static void print_formats() {
for (int fmt = GZIP; fmt < LZOP; ++fmt) { for (int fmt = GZIP; fmt < LZOP; ++fmt) {
fprintf(stderr, "%s ", fmt2name[(format_t) fmt]); fprintf(stderr, "%s ", fmt2name[(format_t) fmt]);

@ -1 +1 @@
Subproject commit 9790b7ee48d3c396c94ef69e164004f50a1f5587 Subproject commit f800c65e1c088240c8336a0a5f0d9346dfea0697

View File

@ -103,8 +103,7 @@ sepolicy *sepolicy::from_file(const char *file) {
policy_file_t pf; policy_file_t pf;
policy_file_init(&pf); policy_file_init(&pf);
int fd = xopen(file, O_RDONLY | O_CLOEXEC); auto fp = xopen_file(file, "re");
auto fp = make_file(fdopen(fd, "re"));
pf.fp = fp.get(); pf.fp = fp.get();
pf.type = PF_USE_STDIO; pf.type = PF_USE_STDIO;
@ -124,7 +123,6 @@ sepolicy *sepolicy::compile_split() {
cil_db_t *db = nullptr; cil_db_t *db = nullptr;
sepol_policydb_t *pdb = nullptr; sepol_policydb_t *pdb = nullptr;
FILE *f; FILE *f;
int fd;
int policy_ver; int policy_ver;
const char *cil_file; const char *cil_file;
#if MAGISK_DEBUG #if MAGISK_DEBUG
@ -150,15 +148,13 @@ sepolicy *sepolicy::compile_split() {
cil_set_target_platform(db, SEPOL_TARGET_SELINUX); cil_set_target_platform(db, SEPOL_TARGET_SELINUX);
cil_set_attrs_expand_generated(db, 1); cil_set_attrs_expand_generated(db, 1);
fd = xopen(SELINUX_VERSION, O_RDONLY | O_CLOEXEC); f = xfopen(SELINUX_VERSION, "re");
f = fdopen(fd, "re");
fscanf(f, "%d", &policy_ver); fscanf(f, "%d", &policy_ver);
fclose(f); fclose(f);
cil_set_policy_version(db, policy_ver); cil_set_policy_version(db, policy_ver);
// Get mapping version // Get mapping version
fd = xopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", O_RDONLY | O_CLOEXEC); f = xfopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", "re");
f = fdopen(fd, "re");
fscanf(f, "%s", plat_ver); fscanf(f, "%s", plat_ver);
fclose(f); fclose(f);