mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-01 16:58:30 +00:00
Mint APE Loader v1.5
This change ports APE Loader to Linux AARCH64, so that Raspberry Pi users can run programs like redbean, without the executable needing to modify itself. Progress has also slipped into this change on the issue of making progress better conforming to user expectations and industry standards regarding which symbols we're allowed to declare
This commit is contained in:
parent
6843150e0c
commit
7e0a09feec
510 changed files with 1783 additions and 1483 deletions
|
@ -45,7 +45,7 @@
|
|||
* @param st is open symbol table for current executable
|
||||
* @return -1 w/ errno if error happened
|
||||
*/
|
||||
dontinstrument noasan int PrintBacktraceUsingSymbols(
|
||||
dontinstrument dontasan int PrintBacktraceUsingSymbols(
|
||||
int fd, const struct StackFrame *bp, struct SymbolTable *st) {
|
||||
bool ok;
|
||||
size_t gi;
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
||||
STATIC_YOINK("strerror_wr");
|
||||
__static_yoink("strerror_wr");
|
||||
|
||||
/**
|
||||
* Handles failure of CHECK_xx() macros.
|
||||
|
|
|
@ -53,7 +53,7 @@ static void PrintHistogram(const long *h, size_t n, long t) {
|
|||
unsigned long logos;
|
||||
for (i = 0; i < n; ++i) {
|
||||
p = (h[i] * 10000 + (t >> 1)) / t;
|
||||
_unassert(0 <= p && p <= 10000);
|
||||
unassert(0 <= p && p <= 10000);
|
||||
if (p) {
|
||||
for (j = 0; j < p / 100; ++j) s[j] = '#';
|
||||
s[j] = 0;
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
#include "libc/thread/thread.h"
|
||||
|
||||
#if SupportsMetal()
|
||||
STATIC_YOINK("_idt");
|
||||
__static_yoink("_idt");
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
|
|
@ -28,14 +28,14 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
STATIC_YOINK("GetSymbolByAddr");
|
||||
__static_yoink("GetSymbolByAddr");
|
||||
|
||||
#define MAXLEAKS 1000
|
||||
|
||||
static bool once;
|
||||
static bool hasleaks;
|
||||
|
||||
static noasan void CheckLeak(void *x, void *y, size_t n, void *a) {
|
||||
static dontasan void CheckLeak(void *x, void *y, size_t n, void *a) {
|
||||
if (n) {
|
||||
if (IsAsan()) {
|
||||
if (__asan_get_heap_size(x) && !__asan_is_leaky(x)) {
|
||||
|
@ -47,7 +47,7 @@ static noasan void CheckLeak(void *x, void *y, size_t n, void *a) {
|
|||
}
|
||||
}
|
||||
|
||||
static noasan void OnMemory(void *x, void *y, size_t n, void *a) {
|
||||
static dontasan void OnMemory(void *x, void *y, size_t n, void *a) {
|
||||
static int i;
|
||||
if (n) {
|
||||
if (MAXLEAKS) {
|
||||
|
@ -67,7 +67,7 @@ static noasan void OnMemory(void *x, void *y, size_t n, void *a) {
|
|||
}
|
||||
}
|
||||
|
||||
static noasan bool HasLeaks(void) {
|
||||
static dontasan bool HasLeaks(void) {
|
||||
malloc_inspect_all(CheckLeak, 0);
|
||||
return hasleaks;
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ static noasan bool HasLeaks(void) {
|
|||
* services that depend on malloc() cannot be used, after calling this
|
||||
* function.
|
||||
*/
|
||||
noasan void CheckForMemoryLeaks(void) {
|
||||
dontasan void CheckForMemoryLeaks(void) {
|
||||
struct mallinfo mi;
|
||||
if (!IsAsan()) return; // we need traces to exclude leaky
|
||||
if (!_cmpxchg(&once, false, true)) {
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
*
|
||||
* If you put the following in your main file:
|
||||
*
|
||||
* STATIC_YOINK("enable_memory_log");
|
||||
* __static_yoink("enable_memory_log");
|
||||
*
|
||||
* Then memory allocations with constant backtraces will be logged to
|
||||
* standard error. The columns printed are
|
||||
|
@ -146,7 +146,7 @@ static void __memlog_update(void *p2, void *p) {
|
|||
__memlog.usage += n - __memlog.allocs.p[i].size;
|
||||
__memlog.allocs.p[i].addr = p2;
|
||||
__memlog.allocs.p[i].size = n;
|
||||
_unassert(__memlog.usage >= 0);
|
||||
unassert(__memlog.usage >= 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -170,20 +170,20 @@ static void __memlog_free(void *p) {
|
|||
__memlog.allocs.p[i].addr = 0;
|
||||
__memlog.usage -= __memlog.allocs.p[i].size;
|
||||
__memlog.allocs.f = MIN(__memlog.allocs.f, i);
|
||||
_unassert(__memlog.usage >= 0);
|
||||
unassert(__memlog.usage >= 0);
|
||||
} else {
|
||||
kprintf("memlog could not find %p\n", p);
|
||||
notpossible;
|
||||
}
|
||||
__memlog_unlock();
|
||||
_unassert(__memlog.free);
|
||||
unassert(__memlog.free);
|
||||
__memlog.free(p);
|
||||
__memlog_log(__builtin_frame_address(0), "free", 0, p, n);
|
||||
}
|
||||
|
||||
static void *__memlog_malloc(size_t n) {
|
||||
void *res;
|
||||
_unassert(__memlog.malloc);
|
||||
unassert(__memlog.malloc);
|
||||
if ((res = __memlog.malloc(n))) {
|
||||
__memlog_lock();
|
||||
__memlog_insert(res);
|
||||
|
@ -195,7 +195,7 @@ static void *__memlog_malloc(size_t n) {
|
|||
|
||||
static void *__memlog_calloc(size_t n, size_t z) {
|
||||
void *res;
|
||||
_unassert(__memlog.calloc);
|
||||
unassert(__memlog.calloc);
|
||||
if ((res = __memlog.calloc(n, z))) {
|
||||
__memlog_lock();
|
||||
__memlog_insert(res);
|
||||
|
@ -207,7 +207,7 @@ static void *__memlog_calloc(size_t n, size_t z) {
|
|||
|
||||
static void *__memlog_memalign(size_t l, size_t n) {
|
||||
void *res;
|
||||
_unassert(__memlog.memalign);
|
||||
unassert(__memlog.memalign);
|
||||
if ((res = __memlog.memalign(l, n))) {
|
||||
__memlog_lock();
|
||||
__memlog_insert(res);
|
||||
|
@ -221,7 +221,7 @@ static void *__memlog_realloc_impl(void *p, size_t n,
|
|||
void *(*f)(void *, size_t),
|
||||
struct StackFrame *frame) {
|
||||
void *res;
|
||||
_unassert(f);
|
||||
unassert(f);
|
||||
if ((res = f(p, n))) {
|
||||
__memlog_lock();
|
||||
if (p) {
|
||||
|
|
|
@ -45,13 +45,14 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
STATIC_YOINK("strerror_wr"); // for kprintf %m
|
||||
STATIC_YOINK("strsignal_r"); // for kprintf %G
|
||||
__static_yoink("strerror_wr"); // for kprintf %m
|
||||
__static_yoink("strsignal_r"); // for kprintf %G
|
||||
|
||||
static const char kGregOrder[17] forcealign(1) = {
|
||||
13, 11, 8, 14, 12, 9, 10, 15, 16, 0, 1, 2, 3, 4, 5, 6, 7,
|
||||
|
@ -219,7 +220,7 @@ relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
|||
uname(&names);
|
||||
errno = err;
|
||||
// TODO(jart): Buffer the WHOLE crash report with backtrace for atomic write.
|
||||
_npassert((p = buf = kmalloc((n = 1024 * 1024))));
|
||||
npassert((p = buf = kmalloc((n = 1024 * 1024))));
|
||||
p += ksnprintf(
|
||||
p, n,
|
||||
"\n%serror%s: Uncaught %G (%s) on %s pid %d tid %d\n"
|
||||
|
@ -227,8 +228,9 @@ relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
|
|||
" %s\n"
|
||||
" %s %s %s %s\n",
|
||||
!__nocolor ? "\e[30;101m" : "", !__nocolor ? "\e[0m" : "", sig,
|
||||
(ctx && (ctx->uc_mcontext.rsp >= GetStaticStackAddr(0) &&
|
||||
ctx->uc_mcontext.rsp <= GetStaticStackAddr(0) + APE_GUARDSIZE))
|
||||
(ctx &&
|
||||
(ctx->uc_mcontext.rsp >= GetStaticStackAddr(0) &&
|
||||
ctx->uc_mcontext.rsp <= GetStaticStackAddr(0) + getauxval(AT_PAGESZ)))
|
||||
? "Stack Overflow"
|
||||
: GetSiCodeName(sig, si->si_code),
|
||||
host, getpid(), gettid(), program_invocation_name, strerror(err),
|
||||
|
|
|
@ -39,12 +39,13 @@
|
|||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#ifdef __aarch64__
|
||||
|
||||
STATIC_YOINK("strerror_wr"); // for kprintf %m
|
||||
STATIC_YOINK("strsignal_r"); // for kprintf %G
|
||||
__static_yoink("strerror_wr"); // for kprintf %m
|
||||
__static_yoink("strsignal_r"); // for kprintf %G
|
||||
|
||||
#define RESET "\e[0m"
|
||||
#define BOLD "\e[1m"
|
||||
|
@ -197,7 +198,8 @@ relegated void __oncrash_arm64(int sig, struct siginfo *si, void *arg) {
|
|||
gethostname(host, sizeof(host));
|
||||
reset = !__nocolor ? RESET : "";
|
||||
strong = !__nocolor ? STRONG : "";
|
||||
if (ctx && (ctx->uc_mcontext.sp & (GetStackSize() - 1)) <= APE_GUARDSIZE) {
|
||||
if (ctx &&
|
||||
(ctx->uc_mcontext.sp & (GetStackSize() - 1)) <= getauxval(AT_PAGESZ)) {
|
||||
kind = "Stack Overflow";
|
||||
} else {
|
||||
kind = GetSiCodeName(sig, si->si_code);
|
||||
|
|
|
@ -37,13 +37,13 @@
|
|||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/ss.h"
|
||||
|
||||
STATIC_YOINK("zipos"); // for symtab
|
||||
STATIC_YOINK("__die"); // for backtracing
|
||||
STATIC_YOINK("ShowBacktrace"); // for backtracing
|
||||
STATIC_YOINK("GetSymbolTable"); // for backtracing
|
||||
STATIC_YOINK("PrintBacktraceUsingSymbols"); // for backtracing
|
||||
STATIC_YOINK("malloc_inspect_all"); // for asan memory origin
|
||||
STATIC_YOINK("GetSymbolByAddr"); // for asan memory origin
|
||||
__static_yoink("zipos"); // for symtab
|
||||
__static_yoink("__die"); // for backtracing
|
||||
__static_yoink("ShowBacktrace"); // for backtracing
|
||||
__static_yoink("GetSymbolTable"); // for backtracing
|
||||
__static_yoink("PrintBacktraceUsingSymbols"); // for backtracing
|
||||
__static_yoink("malloc_inspect_all"); // for asan memory origin
|
||||
__static_yoink("GetSymbolByAddr"); // for asan memory origin
|
||||
|
||||
struct CrashHandler {
|
||||
int sig;
|
||||
|
@ -147,8 +147,8 @@ void ShowCrashReports(void) {
|
|||
ss.ss_size = GetStackSize();
|
||||
// FreeBSD sigaltstack() will EFAULT if we use MAP_STACK here
|
||||
// OpenBSD sigaltstack() auto-applies MAP_STACK to the memory
|
||||
_npassert((ss.ss_sp = _mapanon(GetStackSize())));
|
||||
_npassert(!sigaltstack(&ss, 0));
|
||||
npassert((ss.ss_sp = _mapanon(GetStackSize())));
|
||||
npassert(!sigaltstack(&ss, 0));
|
||||
}
|
||||
InstallCrashHandler(SIGQUIT, __got_sigquit, ef); // ctrl+\ aka ctrl+break
|
||||
InstallCrashHandler(SIGFPE, __got_sigfpe, ef); // 1 / 0
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
// there's a crash in a constructor, this will help with
|
||||
// troubleshooting it. You need to add:
|
||||
//
|
||||
// STATIC_YOINK("ShowCrashReportsEarly");
|
||||
// __static_yoink("ShowCrashReportsEarly");
|
||||
//
|
||||
// To the top of your main module to use this.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue