Skip to content

Commit

Permalink
Merge branch 'topjohnwu:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
deep-soft authored Feb 29, 2024
2 parents a01a775 + 715284b commit 03b45be
Show file tree
Hide file tree
Showing 22 changed files with 340 additions and 171 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
[submodule "system_properties"]
path = native/src/external/system_properties
url = https://github.com/topjohnwu/system_properties.git
[submodule "crt0"]
path = native/src/external/crt0
url = https://github.com/topjohnwu/crt0.git
[submodule "termux-elf-cleaner"]
path = tools/termux-elf-cleaner
url = https://github.com/termux/termux-elf-cleaner.git
22 changes: 1 addition & 21 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -555,27 +556,6 @@ def setup_ndk(args):
rm_rf(ndk_path)
mv(ondk_path, ndk_path)

header("* Patching static libs")
for target in ["arm-linux-androideabi", "i686-linux-android"]:
arch = target.split("-")[0]
lib_dir = op.join(
ndk_path,
"toolchains",
"llvm",
"prebuilt",
f"{os_name}-x86_64",
"sysroot",
"usr",
"lib",
f"{target}",
"23",
)
if not op.exists(lib_dir):
continue
src_dir = op.join("tools", "ndk-bins", arch)
rm(op.join(src_dir, ".DS_Store"))
shutil.copytree(src_dir, lib_dir, copy_function=cp, dirs_exist_ok=True)


def push_files(args, script):
abi = cmd_out([adb_path, "shell", "getprop", "ro.product.cpu.abi"])
Expand Down
11 changes: 9 additions & 2 deletions native/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ include $(CLEAR_VARS)
LOCAL_MODULE := magiskinit
LOCAL_STATIC_LIBRARIES := \
libbase \
libcompat \
libpolicy \
libxz \
libinit-rs
Expand All @@ -77,6 +76,13 @@ LOCAL_SRC_FILES := \
init/selinux.cpp \
init/init-rs.cpp

LOCAL_LDFLAGS := -static -T src/lto_fix.lds

ifdef B_CRT0
LOCAL_STATIC_LIBRARIES += crt0
LOCAL_LDFLAGS :=
endif

include $(BUILD_EXECUTABLE)

endif
Expand All @@ -87,7 +93,6 @@ include $(CLEAR_VARS)
LOCAL_MODULE := magiskboot
LOCAL_STATIC_LIBRARIES := \
libbase \
libcompat \
liblzma \
liblz4 \
libbz2 \
Expand All @@ -102,6 +107,8 @@ LOCAL_SRC_FILES := \
boot/format.cpp \
boot/boot-rs.cpp

LOCAL_LDFLAGS := -static -T src/lto_fix.lds

include $(BUILD_EXECUTABLE)

endif
Expand Down
9 changes: 9 additions & 0 deletions native/src/Application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ APP_PLATFORM := android-23
APP_THIN_ARCHIVE := true
APP_STRIP_MODE := none

ifdef B_CRT0

# Disable all security and debugging features
APP_CFLAGS += -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-stack-protector -U_FORTIFY_SOURCE
# Override output folder to make sure all dependencies are rebuilt with new CFLAGS
NDK_APP_OUT := ./obj/nolibc

endif

# Busybox should use stock libc.a
ifdef B_BB
APP_PLATFORM := android-26
Expand Down
48 changes: 22 additions & 26 deletions native/src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions native/src/base/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,3 @@ LOCAL_SRC_FILES := \
base-rs.cpp \
../external/cxx-rs/src/cxx.cc
include $(BUILD_STATIC_LIBRARY)

# All static executables should link with libcompat

include $(CLEAR_VARS)
LOCAL_MODULE := libcompat
LOCAL_SRC_FILES := compat/compat.cpp
# Fix static variables' ctor/dtor when using LTO
# See: https://github.com/android/ndk/issues/1461
LOCAL_EXPORT_LDFLAGS := -static -T src/lto_fix.lds -Wl,--wrap=rename -Wl,--wrap=renameat
# For some reason, using the hacky libc.a with x86 will trigger stack protection violation
# when mixing Rust and C++ code. Disable stack protector to bypass this issue.
ifeq ($(TARGET_ARCH), x86)
LOCAL_EXPORT_CFLAGS := -fno-stack-protector
endif
include $(BUILD_STATIC_LIBRARY)
20 changes: 18 additions & 2 deletions native/src/base/cstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::cmp::min;
use std::ffi::{CStr, FromBytesWithNulError, OsStr};
use std::fmt::{Arguments, Debug, Display, Formatter, Write};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str::{Utf8Chunks, Utf8Error};
use std::{fmt, mem, slice, str};
use std::os::unix::ffi::OsStrExt;

use cxx::{type_id, ExternType};
use libc::c_char;
Expand Down Expand Up @@ -105,7 +106,7 @@ trait AsUtf8CStr {

// Implementation for Utf8CString

trait StringExt {
pub trait StringExt {
fn nul_terminate(&mut self) -> &mut [u8];
}

Expand All @@ -122,6 +123,21 @@ impl StringExt for String {
}
}

impl StringExt for PathBuf {
#[allow(mutable_transmutes)]
fn nul_terminate(&mut self) -> &mut [u8] {
self.reserve(1);
// SAFETY: the PathBuf is reserved to have enough capacity to fit in the null byte
// SAFETY: the null byte is explicitly added outside of the PathBuf's length
unsafe {
let bytes: &mut [u8] = mem::transmute(self.as_mut_os_str().as_bytes());
let buf = slice::from_raw_parts_mut(bytes.as_mut_ptr(), bytes.len() + 1);
*buf.get_unchecked_mut(bytes.len()) = b'\0';
buf
}
}
}

#[derive(Default)]
pub struct Utf8CString(String);

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

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

std::vector<mount_info> parse_mount_info(const char *pid) {
Expand Down
Loading

0 comments on commit 03b45be

Please sign in to comment.