Introduce new linker for fat ape binaries

This commit is contained in:
Justine Tunney 2023-08-11 04:37:23 -07:00
parent e3c456d23a
commit 0105e3e2b6
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
44 changed files with 3140 additions and 867 deletions

View file

@ -32,10 +32,12 @@ kNtSystemDirectory:
.init.start 300,_init_kNtSystemDirectory
#if SupportsWindows()
testb IsWindows()
jz 1f
pushpop BYTES,%rdx
mov __imp_GetSystemDirectoryA(%rip),%rax
call __getntsyspath
#else
add $BYTES,%rdi
jmp 2f
#endif
.init.end 300,_init_kNtSystemDirectory
1: add $BYTES,%rdi
2: .init.end 300,_init_kNtSystemDirectory

View file

@ -32,10 +32,12 @@ kNtWindowsDirectory:
.init.start 300,_init_kNtWindowsDirectory
#if SupportsWindows()
testb IsWindows()
jz 1f
pushpop BYTES,%rdx
mov __imp_GetWindowsDirectoryA(%rip),%rax
call __getntsyspath
#else
add $BYTES,%rdi
jmp 2f
#endif
.init.end 300,_init_kNtWindowsDirectory
1: add $BYTES,%rdi
2: .init.end 300,_init_kNtWindowsDirectory

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
.text.windows
@ -25,7 +26,9 @@ __onntconsoleevent_nt:
.endfn __onntconsoleevent_nt,globl,hidden
.init.start 300,_init_onntconsoleevent
testb IsWindows()
jz 1f
ezlea __onntconsoleevent_nt,cx
pushpop 1,%rdx
ntcall __imp_SetConsoleCtrlHandler
.init.end 300,_init_onntconsoleevent,globl,hidden
1: .init.end 300,_init_onntconsoleevent,globl,hidden

View file

@ -20,6 +20,8 @@
#include "libc/macros.internal.h"
.init.start 300,_init_wincrash
testb IsWindows()
jz 1f
#if !IsTiny()
mov __wincrashearly(%rip),%rcx
ntcall __imp_RemoveVectoredExceptionHandler
@ -27,4 +29,4 @@
pushpop 1,%rcx
ezlea __wincrash_nt,dx
ntcall __imp_AddVectoredExceptionHandler
.init.end 300,_init_wincrash,globl,hidden
1: .init.end 300,_init_wincrash,globl,hidden

View file

@ -83,7 +83,7 @@ _start:
// make win32 imps noop
.weak ape_idata_iat
.weak ape_idata_iatend
ezlea _missingno,ax
ezlea __win32_oops,ax
ezlea ape_idata_iat,di
ezlea ape_idata_iatend,cx
sub %rdi,%rcx

View file

@ -66,6 +66,18 @@
#define IsXnuSilicon() 0
#endif
#if defined(__x86_64__)
#define _ARCH_NAME "amd64"
#elif defined(__aarch64__)
#define _ARCH_NAME "arm64"
#elif defined(__powerpc64__)
#define _ARCH_NAME "ppc64"
#elif defined(__s390x__)
#define _ARCH_NAME "s390x"
#elif defined(__riscv)
#define _ARCH_NAME "riscv"
#endif
#define SupportsLinux() ((SUPPORT_VECTOR & _HOSTLINUX) == _HOSTLINUX)
#define SupportsMetal() ((SUPPORT_VECTOR & _HOSTMETAL) == _HOSTMETAL)
#define SupportsWindows() ((SUPPORT_VECTOR & _HOSTWINDOWS) == _HOSTWINDOWS)

View file

@ -611,8 +611,8 @@ void abort(void) wontreturn;
} while (0)
#ifndef __STRICT_ANSI__
#define textstartup _Section(".text.startup") dontinstrument
#define textexit _Section(".text.exit") dontinstrument
#define textstartup _Section(".text.startup")
#define textexit _Section(".text.exit")
#define textreal _Section(".text.real")
#define texthead _Section(".text.head")
#define textwindows _Section(".text.windows")

View file

@ -50,6 +50,7 @@ char *GetSymbolByAddr(int64_t);
void PrintGarbage(void);
void PrintGarbageNumeric(FILE *);
void CheckForMemoryLeaks(void);
void PrintWindowsMemory(const char *, size_t);
#ifndef __STRICT_ANSI__

View file

@ -0,0 +1,83 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/memflags.h"
#include "libc/nt/memory.h"
#include "libc/nt/struct/memorybasicinformation.h"
#include "libc/str/str.h"
static const struct DescribeFlags kNtMemState[] = {
{kNtMemCommit, "Commit"}, //
{kNtMemFree, "Free"}, //
{kNtMemReserve, "Reserve"}, //
};
static const char *DescribeNtMemState(char buf[64], uint32_t x) {
return DescribeFlags(buf, 64, kNtMemState, ARRAYLEN(kNtMemState), "kNtMem",
x);
}
static const struct DescribeFlags kNtMemType[] = {
{kNtMemImage, "Image"}, //
{kNtMemMapped, "Mapped"}, //
{kNtMemPrivate, "Private"}, //
};
static const char *DescribeNtMemType(char buf[64], uint32_t x) {
return DescribeFlags(buf, 64, kNtMemType, ARRAYLEN(kNtMemType), "kNtMem", x);
}
/**
* Prints to stderr all memory mappings that exist according to WIN32.
*
* The `high` and `size` parameters may optionally be specified so that
* memory mappings which overlap `[high,high+size)` will get printed in
* ANSI bold red text.
*/
void PrintWindowsMemory(const char *high, size_t size) {
char *p, b[5][64];
const char *start, *stop;
struct NtMemoryBasicInformation mi;
kprintf("%-12s %-12s %10s %16s %16s %32s %32s\n", "Allocation", "BaseAddress",
"RegionSize", "State", "Type", "AllocationProtect", "Protect");
for (p = 0;; p = (char *)mi.BaseAddress + mi.RegionSize) {
const char *start, *stop;
bzero(&mi, sizeof(mi));
if (!VirtualQuery(p, &mi, sizeof(mi))) break;
sizefmt(b[0], mi.RegionSize, 1024);
if (MAX(high, (char *)mi.BaseAddress) <
MIN(high + size, (char *)mi.BaseAddress + mi.RegionSize)) {
start = "\e[1;31m";
stop = "\e[0m";
} else {
start = "";
stop = "";
}
kprintf("%s%.12lx %.12lx %10s %16s %16s %32s %32s%s\n", start,
mi.AllocationBase, mi.BaseAddress, b[0],
DescribeNtMemState(b[1], mi.State),
DescribeNtMemType(b[2], mi.Type),
(DescribeNtPageFlags)(b[3], mi.AllocationProtect),
(DescribeNtPageFlags)(b[4], mi.Protect), stop);
}
}

View file

@ -347,14 +347,6 @@
pop \dest
.endm
// Declares optional function.
.macro .optfn fn:req
.globl "\fn"
.weak "\fn"
.equ "\fn",_missingno
.type "\fn",@function
.endm
// Embeds fixed-width zero-filled string table.
// @note zero-padded ≠ nul-terminated
.macro .fxstr width head rest:vararg

View file

@ -1,30 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 sw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
.real
// Optional function stub.
_missingno:
#ifdef __x86__
xor %eax,%eax
#elif defined(__aarch64__)
mov x0,#0
#endif
ret
.endfn _missingno,globl,hidden

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/nt/enum/status.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#ifdef __x86_64__
@ -40,19 +41,25 @@ kNtdllProcRvas:
.init.start 202,_init_ntdll
push %r12
push %r13
lea _ntdllmissingno(%rip),%r13
lea __ntdll_not_found(%rip),%r13
sub $32,%rsp
xor %eax,%eax
testb IsWindows()
jz 7f
loadstr "ntdll.dll",cx
call *__imp_GetModuleHandleA(%rip)
mov %rax,%r12
7: mov %rax,%r12
0: lodsq
test %rax,%rax
jz 1f
.weak __executable_start
lea __executable_start(%rax),%rdx
xor %eax,%eax
testb IsWindows()
jz 7f
mov %r12,%rcx
call *__imp_GetProcAddress(%rip)
test %rax,%rax
7: test %rax,%rax
cmovz %r13,%rax
stosq
jmp 0b
@ -62,9 +69,13 @@ kNtdllProcRvas:
.init.end 202,_init_ntdll,globl,hidden
.text.windows
_ntdllmissingno:
.ftrace1
__ntdll_not_found:
.ftrace2
mov $kNtStatusDllNotFound,%eax
ret
.previous
.weak __hostos
#endif /* __x86_64__ */

View file

@ -32,10 +32,10 @@
#define kNtPe32bit 0x010b
#define kNtPe64bit 0x020b
#define kNtPeSectionCntCode 0x000000020
#define kNtPeSectionCntInitializedData 0x000000040
#define kNtPeSectionCntUninitializedData 0x000000080
#define kNtPeSectionGprel 0x000008000
#define kNtPeSectionCntCode 0x00000020
#define kNtPeSectionCntInitializedData 0x00000040
#define kNtPeSectionCntUninitializedData 0x00000080
#define kNtPeSectionGprel 0x00008000
#define kNtPeSectionMemDiscardable 0x02000000
#define kNtPeSectionMemNotCached 0x04000000
#define kNtPeSectionMemNotPaged 0x08000000

View file

@ -34,16 +34,6 @@
__static_yoink("__get_symbol");
#ifdef __x86_64__
#define SYMTAB_ARCH ".symtab.x86"
#elif defined(__aarch64__)
#define SYMTAB_ARCH ".symtab.aarch64"
#elif defined(__powerpc64__)
#define SYMTAB_ARCH ".symtab.powerpc64"
#else
#error "unsupported architecture"
#endif
static pthread_spinlock_t g_lock;
struct SymbolTable *__symtab; // for kprintf
@ -69,7 +59,7 @@ static struct SymbolTable *GetSymbolTableFromZip(struct Zipos *zipos) {
size_t size, size2;
ssize_t rc, cf, lf;
struct SymbolTable *res = 0;
if ((cf = GetZipFile(zipos, SYMTAB_ARCH)) != -1 ||
if ((cf = GetZipFile(zipos, ".symtab." _ARCH_NAME)) != -1 ||
(cf = GetZipFile(zipos, ".symtab")) != -1) {
lf = GetZipCfileOffset(zipos->map + cf);
size = GetZipLfileUncompressedSize(zipos->map + lf);

View file

@ -113,7 +113,6 @@ void _Exitr(int) libcesque wontreturn;
void _Exit1(int) libcesque wontreturn;
void _restorewintty(void);
void __paginate(int, const char *);
long _missingno();
/* memory management */
void _weakfree(void *);
void *_mapanon(size_t) attributeallocsize((1)) mallocesque;

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/dce.h"
@ -95,6 +96,12 @@ static const short kConsoleModes[3] = {
kNtEnableVirtualTerminalProcessing,
};
// implements all win32 apis on non-windows hosts
__msabi long __win32_oops(void) {
assert(!"win32 api called on non-windows host");
return 0;
}
// https://nullprogram.com/blog/2022/02/18/
__msabi static inline char16_t *MyCommandLine(void) {
void *cmd;

View file

@ -705,6 +705,10 @@ haveinc:
return prec;
}
static int __fmt_noop(const char *, void *, size_t) {
return 0;
}
/**
* Implements {,v}{,s{,n},{,{,x}as},f,d}printf domain-specific language.
*
@ -797,7 +801,7 @@ int __fmt(void *fn, void *arg, const char *format, va_list va) {
x = 0;
lasterr = errno;
out = fn ? fn : (void *)_missingno;
out = fn ? fn : __fmt_noop;
while (*format) {
if (*format != '%') {