Cleanup libc hacks

This commit is contained in:
topjohnwu 2023-05-19 03:23:43 -07:00
parent 6ca2a3d841
commit 582cad1b8d
22 changed files with 17 additions and 82 deletions

View File

@ -554,7 +554,7 @@ def setup_ndk(args):
)
if not op.exists(lib_dir):
continue
src_dir = op.join("tools", "ndk-bins", "21", arch)
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)

View File

@ -22,8 +22,8 @@ include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libcompat
# Workaround "hacky" libc.a missing symbols
# To build Magisk with vanilla NDK, comment out the next line
# Add "hacky" libc.a missing symbols back
# All symbols in this library are weak, so a vanilla NDK should still link properly
LOCAL_SRC_FILES := compat/compat.cpp
# Fix static variables' ctor/dtor when using LTO
# See: https://github.com/android/ndk/issues/1461

View File

@ -1,18 +1,19 @@
// This file implements all missing symbols that should exist in normal API 21
// This file implements all missing symbols that should exist in normal API 23
// libc.a but missing in our extremely lean libc.a replacements.
#if !defined(__LP64__)
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <mntent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#if !defined(__LP64__)
extern "C" {
#include "fortify.hpp"
// Original source: https://github.com/freebsd/freebsd/blob/master/contrib/file/src/getline.c
// License: BSD, full copyright notice please check original source
@ -58,41 +59,8 @@ ssize_t getline(char **buf, size_t *bufsiz, FILE *fp) {
return getdelim(buf, bufsiz, '\n', fp);
}
[[gnu::weak]]
FILE *setmntent(const char *path, const char *mode) {
return fopen(path, mode);
}
[[gnu::weak]]
int endmntent(FILE *fp) {
if (fp != nullptr) {
fclose(fp);
}
return 1;
}
// Missing system call wrappers
[[gnu::weak]]
int setns(int fd, int nstype) {
return syscall(__NR_setns, fd, nstype);
}
[[gnu::weak]]
int unshare(int flags) {
return syscall(__NR_unshare, flags);
}
[[gnu::weak]]
int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
return syscall(__NR_accept4, sockfd, addr, addrlen, flags);
}
[[gnu::weak]]
int dup3(int oldfd, int newfd, int flags) {
return syscall(__NR_dup3, oldfd, newfd, flags);
}
[[gnu::weak]]
ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
return syscall(__NR_readlinkat, dirfd, pathname, buf, bufsiz);
@ -109,11 +77,6 @@ int linkat(int olddirfd, const char *oldpath,
return syscall(__NR_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
}
[[gnu::weak]]
int inotify_init1(int flags) {
return syscall(__NR_inotify_init1, flags);
}
[[gnu::weak]]
int faccessat(int dirfd, const char *pathname, int mode, int flags) {
return syscall(__NR_faccessat, dirfd, pathname, mode, flags);
@ -142,34 +105,6 @@ int ftruncate64(int fd, off64_t length) {
[[gnu::weak]]
void android_set_abort_message(const char *) {}
// Original source: <android/legacy_signal_inlines.h>
[[gnu::weak]]
int sigaddset(sigset_t *set, int signum) {
/* Signal numbers start at 1, but bit positions start at 0. */
int bit = signum - 1;
auto *local_set = (unsigned long *)set;
if (set == nullptr || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
errno = EINVAL;
return -1;
}
local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
return 0;
}
[[gnu::weak]]
int sigemptyset(sigset_t *set) {
if (set == nullptr) {
errno = EINVAL;
return -1;
}
memset(set, 0, sizeof(sigset_t));
return 0;
}
#undef vsnprintf
#undef snprintf
#include "fortify.hpp"
extern FILE __sF[];
[[gnu::weak]] FILE* stdin = &__sF[0];

View File

@ -1,14 +1,14 @@
#include <new>
#include <stdlib.h>
#include <cstdlib>
/* Override libc++ new implementation
* to optimize final build size */
void* operator new(std::size_t s) { return malloc(s); }
void* operator new[](std::size_t s) { return malloc(s); }
void operator delete(void *p) { free(p); }
void operator delete[](void *p) { free(p); }
void* operator new(std::size_t s, const std::nothrow_t&) noexcept { return malloc(s); }
void* operator new[](std::size_t s, const std::nothrow_t&) noexcept { return malloc(s); }
void operator delete(void *p, const std::nothrow_t&) noexcept { free(p); }
void operator delete[](void *p, const std::nothrow_t&) noexcept { free(p); }
void* operator new(std::size_t s) { return std::malloc(s); }
void* operator new[](std::size_t s) { return std::malloc(s); }
void operator delete(void *p) { std::free(p); }
void operator delete[](void *p) { std::free(p); }
void* operator new(std::size_t s, const std::nothrow_t&) noexcept { return std::malloc(s); }
void* operator new[](std::size_t s, const std::nothrow_t&) noexcept { return std::malloc(s); }
void operator delete(void *p, const std::nothrow_t&) noexcept { std::free(p); }
void operator delete[](void *p, const std::nothrow_t&) noexcept { std::free(p); }