Trim down executable sizes

This commit is contained in:
Justine Tunney 2021-02-11 08:37:18 -08:00
parent ec69413f83
commit bfa8581537
19 changed files with 1176 additions and 131 deletions

View file

@ -27,7 +27,7 @@
#include "libc/runtime/runtime.h"
textwindows int sys_fstatat_nt(int dirfd, const char *path, struct stat *st,
uint32_t flags) {
uint32_t flags) {
int rc;
int64_t fh;
uint16_t path16[PATH_MAX];

View file

@ -1,44 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=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.h"
.text.exit
.source __FILE__
// Calls linker registered finalization functions.
// @note functions are called in reverse order
_destruct:
push %rbp
mov %rsp,%rbp
ezlea __fini_array_start,cx
.weak __fini_array_start
ezlea __fini_array_end,ax
.weak __fini_array_end
cmp %rax,%rcx
je 2f
1: sub $8,%rax
push %rax
push %rcx
call *(%rax)
pop %rcx
pop %rax
cmp %rax,%rcx
jne 1b
2: pop %rbp
ret
.endfn _destruct,globl

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- 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 2020 Justine Alexandra Roberts Tunney
Copyright 2021 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
@ -16,24 +16,31 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.text.exit
.source __FILE__
#include "libc/bits/weaken.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
// Exits program with grace.
//
// @param %dil has exit code
// @noreturn
exit: push %rbp
mov %rsp,%rbp
push %rdi
push %rdi
xor %edi,%edi
call __cxa_finalize
call _destruct
pop %rdi
pop %rdi
call _Exit
.endfn exit,globl
extern const uintptr_t __fini_array_start[];
extern const uintptr_t __fini_array_end[];
ud2
/**
* Exits process with grace.
*
* This calls functions registered by atexit() before terminating
* the current process, and any associated threads. It also calls
* all the legacy linker registered destructors in reeverse order
*
* @param exitcode is masked with 255
* @see _Exit()
* @noreturn
*/
wontreturn void exit(int exitcode) {
const uintptr_t *p;
if (weaken(__cxa_finalize)) {
weaken(__cxa_finalize)(NULL);
}
for (p = *weaken(__fini_array_end); p-- > *weaken(__fini_array_start);) {
((void (*)(void))p)();
}
_Exit(exitcode);
}

View file

@ -20,7 +20,6 @@
#include "libc/runtime/internal.h"
#include "libc/macros.h"
.privileged
.source __FILE__
// Terminates process, ignoring destructors and atexit() handlers.
//
@ -34,17 +33,13 @@ _Exit: push %rbp
testb IsWindows()
jz 1f
sub $32,%rsp
movzbl %dil,%ecx # %ERRORLEVEL% is limitless
4: call *__imp_ExitProcess(%rip)
jmp 4b
0: int3 # @see setjmp() in WinMain()
movzbl %dil,%ecx # %ERRORLEVEL% is limitless
call *__imp_ExitProcess(%rip)
#endif
1: mov __NR_exit_group(%rip),%eax
syscall
cli
lidt 3f
2: hlt
jmp 2b
3: .quad 0
#if SupportsMetal()
call triplf
#endif
.endfn _Exit,globl,protected
.hidden __NR_exit_group

View file

@ -47,8 +47,10 @@ vfork:
#endif
syscall
push %rsi # note it happens twice in same page
#if SupportsLinux()
cmp $-4095,%eax
jae systemfive_error
#endif
0: ezlea __vforked,di
test %eax,%eax
jz 1f

View file

@ -35,6 +35,7 @@
#include "libc/nt/pedef.internal.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/directmap.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.h"
@ -64,11 +65,11 @@ static noasan textwindows void MakeLongDoubleLongAgain(void) {
asm volatile("fldcw\t%0" : /* no outputs */ : "m"(x87cw));
}
static noasan textwindows void NormalizeCmdExe(void) {
static noasan textwindows void NormalizeCmdExe(int version) {
uint32_t mode;
int64_t handle, hstdin, hstdout, hstderr;
if ((int)weakaddr("v_ntsubsystem") == kNtImageSubsystemWindowsCui &&
NtGetVersion() >= kNtVersionWindows10) {
version >= 10) {
hstdin = GetStdHandle(pushpop(kNtStdInputHandle));
hstdout = GetStdHandle(pushpop(kNtStdOutputHandle));
hstderr = GetStdHandle(pushpop(kNtStdErrorHandle));
@ -86,17 +87,16 @@ static noasan textwindows void NormalizeCmdExe(void) {
SetTrueColor();
SetConsoleOutputCP(kNtCpUtf8);
GetConsoleMode(handle, &mode);
SetConsoleMode(handle, mode | kNtEnableProcessedOutput |
kNtEnableWrapAtEolOutput |
(NtGetVersion() >= kNtVersionWindows10
? kNtEnableVirtualTerminalProcessing
: 0));
SetConsoleMode(
handle, mode | kNtEnableProcessedOutput | kNtEnableWrapAtEolOutput |
(version >= 10 ? kNtEnableVirtualTerminalProcessing : 0));
}
}
}
static noasan textwindows wontreturn void WinMainNew(void) {
int64_t h;
int version;
size_t size;
int i, count;
uint64_t addr;
@ -105,13 +105,15 @@ static noasan textwindows wontreturn void WinMainNew(void) {
const char16_t *env16;
extern char os asm("__hostos");
os = WINDOWS; /* madness https://news.ycombinator.com/item?id=21019722 */
NormalizeCmdExe();
addr = NtGetVersion() < kNtVersionWindows10 ? 0xff00000 : 0x777000000000;
version = NtGetPeb()->OSMajorVersion;
NormalizeCmdExe(version);
addr = version < 10 ? 0xff00000 : 0x777000000000;
size = ROUNDUP(STACKSIZE + sizeof(struct WinArgs), FRAMESIZE);
_mmi.p[0].h =
sys_mmap_nt((char *)addr, size, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE, -1, 0)
.maphandle;
MapViewOfFileExNuma((_mmi.p[0].h = CreateFileMappingNuma(
-1, &kNtIsInheritable, kNtPageExecuteReadwrite,
size >> 32, size, NULL, kNtNumaNoPreferredNode)),
kNtFileMapWrite | kNtFileMapExecute, 0, 0, size,
(void *)addr, kNtNumaNoPreferredNode);
_mmi.p[0].x = addr >> 16;
_mmi.p[0].y = (addr >> 16) + ((size >> 16) - 1);
_mmi.p[0].prot = PROT_READ | PROT_WRITE | PROT_EXEC;

View file

@ -27,7 +27,7 @@
.endobj \name,globl
.previous
#if SupportsLinux()
#if SupportsLinux() || SupportsMetal()
.section .sort.rodata.syscon.linux.2.\group\().\name,"a",@progbits
.sleb128 \linux
.previous

View file

@ -104,7 +104,7 @@ __systemfive:
.privileged
.Lanchorpoint:
#if SupportsLinux()
#if SupportsLinux() || SupportsMetal()
systemfive_linux:
and $0xfff,%eax
cmp $0xfff,%eax
@ -137,7 +137,7 @@ systemfive_enosys:
systemfive_netbsd:
shr $4*13,%rax
jmp systemfive_bsdscrub
.endfn systemfive_openbsd,globl,hidden
.endfn systemfive_netbsd,globl,hidden
#endif
#if SupportsOpenbsd()
systemfive_openbsd:
@ -222,7 +222,7 @@ systemfive_xnu:
lea 2(%eax),%eax
jnz 0b
#endif
#if SupportsLinux()
#if SupportsLinux() || SupportsMetal()
_init_systemfive_linux:
pushb systemfive_linux-.Lanchorpoint
push $LINUX
@ -403,7 +403,7 @@ syscon_end:
.type syscon_end,@object
.globl syscon_start
.globl syscon_end
#if SupportsLinux()
#if SupportsLinux() || SupportsMetal()
.section .sort.rodata.syscon.linux.1,"a",@progbits
.align 1
syscon_linux:/*