Commit 770b28ca authored by topjohnwu's avatar topjohnwu

Build on API 21 headers

parent 62e464f7
...@@ -330,6 +330,12 @@ def build_binary(args): ...@@ -330,6 +330,12 @@ def build_binary(args):
if 'test' in args.target: if 'test' in args.target:
flag += ' B_TEST=1' flag += ' B_TEST=1'
if flag:
run_ndk_build(flag + ' B_SHARED=1')
if 'magisk' in args.target:
clean_elf()
if 'magiskinit' in args.target: if 'magiskinit' in args.target:
dump_bin_header() dump_bin_header()
flag += ' B_INIT=1' flag += ' B_INIT=1'
...@@ -346,9 +352,6 @@ def build_binary(args): ...@@ -346,9 +352,6 @@ def build_binary(args):
if flag: if flag:
run_ndk_build(flag) run_ndk_build(flag)
if 'magisk' in args.target:
clean_elf()
if 'busybox' in args.target: if 'busybox' in args.target:
run_ndk_build('B_BB=1') run_ndk_build('B_BB=1')
...@@ -445,19 +448,18 @@ def setup_ndk(args): ...@@ -445,19 +448,18 @@ def setup_ndk(args):
mv(op.join(ndk_root, f'android-ndk-r{ndk_ver}'), ndk_path) mv(op.join(ndk_root, f'android-ndk-r{ndk_ver}'), ndk_path)
header('* Patching static libs') header('* Patching static libs')
for api in ['16', '21']: for target in ['aarch64-linux-android', 'arm-linux-androideabi',
for target in ['aarch64-linux-android', 'arm-linux-androideabi', 'i686-linux-android', 'x86_64-linux-android']:
'i686-linux-android', 'x86_64-linux-android']: arch = target.split('-')[0]
arch = target.split('-')[0] lib_dir = op.join(
lib_dir = op.join( ndk_path, 'toolchains', 'llvm', 'prebuilt', f'{os_name}-x86_64',
ndk_path, 'toolchains', 'llvm', 'prebuilt', f'{os_name}-x86_64', 'sysroot', 'usr', 'lib', f'{target}', '21')
'sysroot', 'usr', 'lib', f'{target}', api) if not op.exists(lib_dir):
if not op.exists(lib_dir): continue
continue src_dir = op.join('tools', 'ndk-bins', '21', arch)
src_dir = op.join('tools', 'ndk-bins', api, arch) rm(op.join(src_dir, '.DS_Store'))
rm(op.join(src_dir, '.DS_Store')) for path in copy_tree(src_dir, lib_dir):
for path in copy_tree(src_dir, lib_dir): vprint(f'Replaced {path}')
vprint(f'Replaced {path}')
def setup_avd(args): def setup_avd(args):
......
...@@ -8,7 +8,7 @@ ifdef B_MAGISK ...@@ -8,7 +8,7 @@ ifdef B_MAGISK
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := magisk LOCAL_MODULE := magisk
LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils libphmap libxhook LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils-shared libphmap libxhook
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
core/applets.cpp \ core/applets.cpp \
...@@ -134,7 +134,7 @@ ifneq (,$(wildcard jni/test.cpp)) ...@@ -134,7 +134,7 @@ ifneq (,$(wildcard jni/test.cpp))
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_MODULE := test LOCAL_MODULE := test
LOCAL_STATIC_LIBRARIES := libutils libphmap LOCAL_STATIC_LIBRARIES := libutils-shared libphmap
LOCAL_SRC_FILES := test.cpp LOCAL_SRC_FILES := test.cpp
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)
......
...@@ -3,10 +3,19 @@ APP_CFLAGS := -Wall -Oz -fomit-frame-pointer -flto ...@@ -3,10 +3,19 @@ APP_CFLAGS := -Wall -Oz -fomit-frame-pointer -flto
APP_LDFLAGS := -flto APP_LDFLAGS := -flto
APP_CPPFLAGS := -std=c++17 APP_CPPFLAGS := -std=c++17
APP_STL := none APP_STL := none
APP_PLATFORM := android-16 APP_PLATFORM := android-21
APP_THIN_ARCHIVE := true APP_THIN_ARCHIVE := true
APP_STRIP_MODE := --strip-all APP_STRIP_MODE := --strip-all
ifneq ($(TARGET_ARCH),arm64)
ifneq ($(TARGET_ARCH),x86_64)
ifndef B_SHARED
# Disable fortify on static 32-bit targets
APP_CFLAGS += -D_FORTIFY_SOURCE=0 -Wno-macro-redefined
endif
endif
endif
# Busybox should use stock libc.a # Busybox should use stock libc.a
ifdef B_BB ifdef B_BB
APP_PLATFORM := android-22 APP_PLATFORM := android-22
......
...@@ -11,10 +11,6 @@ ...@@ -11,10 +11,6 @@
#define PR_SET_VMA_ANON_NAME 0 #define PR_SET_VMA_ANON_NAME 0
#endif #endif
// Missing functions
#define getline compat_getline
ssize_t compat_getline(char **, size_t *, FILE *);
// Rename symbols // Rename symbols
#pragma redefine_extname __system_property_set _system_property_set2 #pragma redefine_extname __system_property_set _system_property_set2
#pragma redefine_extname __system_property_find _system_property_find2 #pragma redefine_extname __system_property_find _system_property_find2
......
// Force using legacy_signal_inlines.h
#define __ANDROID_API_BACKUP__ __ANDROID_API__
#undef __ANDROID_API__
#define __ANDROID_API__ 20
#include <android/legacy_signal_inlines.h>
#undef __ANDROID_API__
#define __ANDROID_API__ __ANDROID_API_BACKUP__
#include <sys/mount.h> #include <sys/mount.h>
#include <map> #include <map>
......
...@@ -2,14 +2,7 @@ LOCAL_PATH := $(call my-dir) ...@@ -2,14 +2,7 @@ LOCAL_PATH := $(call my-dir)
# All Magisk common code lives here # All Magisk common code lives here
include $(CLEAR_VARS) UTILS_SRC_FILES := \
LOCAL_MODULE:= libutils
LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include out/generated
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_EXPORT_STATIC_LIBRARIES := libcxx
LOCAL_STATIC_LIBRARIES := libcxx
LOCAL_SRC_FILES := \
missing.cpp \
new.cpp \ new.cpp \
files.cpp \ files.cpp \
misc.cpp \ misc.cpp \
...@@ -18,4 +11,20 @@ LOCAL_SRC_FILES := \ ...@@ -18,4 +11,20 @@ LOCAL_SRC_FILES := \
xwrap.cpp \ xwrap.cpp \
stream.cpp stream.cpp
include $(CLEAR_VARS)
LOCAL_MODULE:= libutils
LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include out/generated
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_EXPORT_STATIC_LIBRARIES := libcxx
LOCAL_STATIC_LIBRARIES := libcxx
LOCAL_SRC_FILES := $(UTILS_SRC_FILES) missing.cpp
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libutils-shared
LOCAL_C_INCLUDES := jni/include $(LOCAL_PATH)/include out/generated
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_EXPORT_STATIC_LIBRARIES := libcxx
LOCAL_STATIC_LIBRARIES := libcxx
LOCAL_SRC_FILES := $(UTILS_SRC_FILES)
include $(BUILD_STATIC_LIBRARY) include $(BUILD_STATIC_LIBRARY)
/* // This file implements all missing symbols that should exist in normal API 21
* Host all missing/incomplete implementation in bionic // libc.a but missing in our extremely lean libc.a replacements.
* Copied from various sources
* */ #include <cstdlib>
#include <cstring>
#include <stdlib.h> #include <cerrno>
#include <string.h>
#include <errno.h>
#include <mntent.h> #include <mntent.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "missing.hpp" extern "C" {
/* Original source: https://github.com/freebsd/freebsd/blob/master/contrib/file/src/getline.c /* Original source: https://github.com/freebsd/freebsd/blob/master/contrib/file/src/getline.c
* License: BSD, full copyright notice please check original source */ * License: BSD, full copyright notice please check original source */
ssize_t compat_getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) { ssize_t getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) {
char *ptr, *eptr; char *ptr, *eptr;
if (*buf == nullptr || *bufsiz == 0) { if (*buf == nullptr || *bufsiz == 0) {
...@@ -49,14 +49,14 @@ ssize_t compat_getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) { ...@@ -49,14 +49,14 @@ ssize_t compat_getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) {
} }
} }
ssize_t compat_getline(char **buf, size_t *bufsiz, FILE *fp) { ssize_t getline(char **buf, size_t *bufsiz, FILE *fp) {
return getdelim(buf, bufsiz, '\n', fp); return getdelim(buf, bufsiz, '\n', fp);
} }
/* Original source: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/mntent.cpp /* Original source: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/mntent.cpp
* License: AOSP, full copyright notice please check original source */ * License: AOSP, full copyright notice please check original source */
struct mntent *compat_getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len) { struct mntent *getmntent_r(FILE *fp, struct mntent *e, char *buf, int buf_len) {
memset(e, 0, sizeof(*e)); memset(e, 0, sizeof(*e));
while (fgets(buf, buf_len, fp) != nullptr) { while (fgets(buf, buf_len, fp) != nullptr) {
// Entries look like "proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0". // Entries look like "proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0".
...@@ -79,31 +79,58 @@ struct mntent *compat_getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf ...@@ -79,31 +79,58 @@ struct mntent *compat_getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf
return nullptr; return nullptr;
} }
FILE *compat_setmntent(const char* path, const char* mode) { FILE *setmntent(const char *path, const char *mode) {
return fopen(path, mode); return fopen(path, mode);
} }
int compat_endmntent(FILE* fp) { int endmntent(FILE *fp) {
if (fp != nullptr) { if (fp != nullptr) {
fclose(fp); fclose(fp);
} }
return 1; return 1;
} }
char *compat_hasmntopt(const struct mntent* mnt, const char* opt) { // Missing system call wrappers
char* token = mnt->mnt_opts;
char* const end = mnt->mnt_opts + strlen(mnt->mnt_opts); int setns(int fd, int nstype) {
const size_t optLen = strlen(opt); return syscall(__NR_setns, fd, nstype);
while (token) { }
char* const tokenEnd = token + optLen;
if (tokenEnd > end) break; int unshare(int flags) {
if (memcmp(token, opt, optLen) == 0 && return syscall(__NR_unshare, flags);
(*tokenEnd == '\0' || *tokenEnd == ',' || *tokenEnd == '=')) { }
return token;
} int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
token = strchr(token, ','); return syscall(__NR_accept4, sockfd, addr, addrlen, flags);
if (token) token++; }
}
return nullptr; int dup3(int oldfd, int newfd, int flags) {
return syscall(__NR_dup3, oldfd, newfd, flags);
} }
ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz);
}
int symlinkat(const char *target, int newdirfd, const char *linkpath) {
return syscall(__NR_symlinkat, target, newdirfd, linkpath);
}
int linkat(int olddirfd, const char *oldpath,
int newdirfd, const char *newpath, int flags) {
return syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
}
int inotify_init1(int flags) {
return syscall(__NR_inotify_init1, flags);
}
int faccessat(int dirfd, const char *pathname, int mode, int flags) {
return syscall(__NR_faccessat, dirfd, pathname, mode, flags);
}
#if !defined(__LP64__)
void android_set_abort_message(const char *msg) {}
#endif
} // extern "C"
...@@ -2,73 +2,8 @@ ...@@ -2,73 +2,8 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
#include <mntent.h>
// Missing libc functions static inline int sigtimedwait(const sigset_t* set, siginfo_t* info, const timespec* timeout) {
#define getline compat_getline
#define getdelim compat_getdelim
#define getmntent_r compat_getmntent_r
#define setmntent compat_setmntent
#define endmntent compat_endmntent
#define hasmntopt compat_hasmntopt
// Missing syscall wrappers
#define setns compat_setns
#define unshare compat_unshare
#define accept4 compat_accept4
#define dup3 compat_dup3
#define readlinkat compat_readlinkat
#define symlinkat compat_symlinkat
#define linkat compat_linkat
#define inotify_init1 compat_inotify_init1
#define faccessat compat_faccessat
#define sigtimedwait compat_sigtimedwait
ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream);
ssize_t compat_getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
struct mntent *compat_getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len);
FILE *compat_setmntent(const char* path, const char* mode);
int compat_endmntent(FILE* fp);
char *compat_hasmntopt(const struct mntent* mnt, const char* opt);
static inline int compat_setns(int fd, int nstype) {
return syscall(__NR_setns, fd, nstype);
}
static inline int compat_unshare(int flags) {
return syscall(__NR_unshare, flags);
}
static inline int compat_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
return syscall(__NR_accept4, sockfd, addr, addrlen, flags);
}
static inline int compat_dup3(int oldfd, int newfd, int flags) {
return syscall(__NR_dup3, oldfd, newfd, flags);
}
static inline ssize_t compat_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz);
}
static inline int compat_symlinkat(const char *target, int newdirfd, const char *linkpath) {
return syscall(__NR_symlinkat, target, newdirfd, linkpath);
}
static inline int compat_linkat(int olddirfd, const char *oldpath,
int newdirfd, const char *newpath, int flags) {
return syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
}
static inline int compat_inotify_init1(int flags) {
return syscall(__NR_inotify_init1, flags);
}
static inline int compat_faccessat(int dirfd, const char *pathname, int mode, int flags) {
return syscall(__NR_faccessat, dirfd, pathname, mode, flags);
}
static inline int compat_sigtimedwait(const sigset_t* set, siginfo_t* info, const timespec* timeout) {
union { union {
sigset_t set; sigset_t set;
sigset_t set64; sigset_t set64;
......
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/xattr.h> #include <sys/xattr.h>
#include <utils.hpp> #include <utils.hpp>
......
#include <unistd.h>
#include <cstddef> #include <cstddef>
#include <utils.hpp> #include <utils.hpp>
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/ptrace.h> #include <sys/ptrace.h>
#include <sys/inotify.h>
#include <utils.hpp> #include <utils.hpp>
...@@ -198,26 +199,9 @@ int xlisten(int sockfd, int backlog) { ...@@ -198,26 +199,9 @@ int xlisten(int sockfd, int backlog) {
return ret; return ret;
} }
static int accept4_compat(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
int fd = accept(sockfd, addr, addrlen);
if (fd < 0) {
PLOGE("accept");
} else {
if (flags & SOCK_CLOEXEC)
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (flags & SOCK_NONBLOCK) {
int i = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, i | O_NONBLOCK);
}
}
return fd;
}
int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
int fd = accept4(sockfd, addr, addrlen, flags); int fd = accept4(sockfd, addr, addrlen, flags);
if (fd < 0) { if (fd < 0) {
if (errno == ENOSYS)
return accept4_compat(sockfd, addr, addrlen, flags);
PLOGE("accept4"); PLOGE("accept4");
} }
return fd; return fd;
......
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment