Skip to content

Commit

Permalink
Build magiskboot with crt0
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Feb 29, 2024
1 parent b1297c4 commit 24e46a5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 31 deletions.
16 changes: 6 additions & 10 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,31 +400,27 @@ def build_binary(args):
flag += " B_POLICY=1"
clean = True

if "test" in args.target:
flag += " B_TEST=1"

if "magiskinit" in args.target:
flag += " B_PRELOAD=1"

if "resetprop" in args.target:
flag += " B_PROP=1"

if "magiskboot" in args.target:
flag += " B_BOOT=1"

if flag:
run_ndk_build(flag)

# magiskinit embeds preload.so

flag = ""

if "magiskinit" in args.target:
# magiskinit embeds preload.so
dump_bin_header(args)
flag += " B_INIT=1"
flag += " B_CRT0=1"

if "magiskboot" in args.target:
flag += " B_BOOT=1"

if flag:
dump_bin_header(args)
flag += " B_CRT0=1"
run_ndk_build(flag)

if clean:
Expand Down
3 changes: 2 additions & 1 deletion native/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ android {
ndkBuild {
// Pass arguments to ndk-build.
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"
)
}
}
Expand Down
6 changes: 6 additions & 0 deletions native/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ LOCAL_SRC_FILES := \

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)

endif
Expand Down
16 changes: 4 additions & 12 deletions native/src/base/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
int fd = xopen(file, O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
auto fp = fdopen(fd, "re");
file_readline(trim, fp, fn);
fclose(fp);
}
if (auto fp = open_file(file, "re"))
file_readline(trim, fp.get(), fn);
}

void file_readline(const char *file, const function<bool(string_view)> &fn) {
Expand All @@ -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) {
int fd = xopen(file, O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
auto fp = fdopen(fd, "re");
parse_prop_file(fp, fn);
fclose(fp);
}
if (auto fp = open_file(file, "re"))
parse_prop_file(fp.get(), fn);
}

std::vector<mount_info> parse_mount_info(const char *pid) {
Expand Down
34 changes: 34 additions & 0 deletions native/src/base/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,37 @@ const char *rust::Utf8CStr::data() const {
size_t rust::Utf8CStr::length() const {
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);
}
5 changes: 5 additions & 0 deletions native/src/boot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

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() {
for (int fmt = GZIP; fmt < LZOP; ++fmt) {
fprintf(stderr, "%s ", fmt2name[(format_t) fmt]);
Expand Down
2 changes: 1 addition & 1 deletion native/src/external/crt0
Submodule crt0 updated 7 files
+4 −2 Android.mk
+27 −2 mem.c
+54 −9 misc.c
+659 −0 musl/vfprintf.c
+161 −23 stdio.c
+49 −2 syscall.c
+2 −2 tinystdio/tinystdio.h
10 changes: 3 additions & 7 deletions native/src/sepolicy/policydb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ sepolicy *sepolicy::from_file(const char *file) {

policy_file_t pf;
policy_file_init(&pf);
int fd = xopen(file, O_RDONLY | O_CLOEXEC);
auto fp = make_file(fdopen(fd, "re"));
auto fp = xopen_file(file, "re");
pf.fp = fp.get();
pf.type = PF_USE_STDIO;

Expand All @@ -124,7 +123,6 @@ sepolicy *sepolicy::compile_split() {
cil_db_t *db = nullptr;
sepol_policydb_t *pdb = nullptr;
FILE *f;
int fd;
int policy_ver;
const char *cil_file;
#if MAGISK_DEBUG
Expand All @@ -150,15 +148,13 @@ sepolicy *sepolicy::compile_split() {
cil_set_target_platform(db, SEPOL_TARGET_SELINUX);
cil_set_attrs_expand_generated(db, 1);

fd = xopen(SELINUX_VERSION, O_RDONLY | O_CLOEXEC);
f = fdopen(fd, "re");
f = xfopen(SELINUX_VERSION, "re");
fscanf(f, "%d", &policy_ver);
fclose(f);
cil_set_policy_version(db, policy_ver);

// Get mapping version
fd = xopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", O_RDONLY | O_CLOEXEC);
f = fdopen(fd, "re");
f = xfopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", "re");
fscanf(f, "%s", plat_ver);
fclose(f);

Expand Down

0 comments on commit 24e46a5

Please sign in to comment.