Support 40 cosmo_dlopen() function parameters

Our dynamic linking implementation is now able to support functions with
dozens of parameters. In addition to having extra integral arguments you
can now pass vector registers using intrinsic types. Lastly, you can now
return multiple values, which is useful for functions returning structs.
This commit is contained in:
Justine Tunney 2024-01-04 13:41:26 -08:00
parent a3deef70c2
commit 0e49bed660
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 550 additions and 135 deletions

View file

@ -8,8 +8,13 @@ LIBC_DLOPEN = $(LIBC_DLOPEN_A_DEPS) $(LIBC_DLOPEN_A)
LIBC_DLOPEN_A = o/$(MODE)/libc/dlopen/dlopen.a
LIBC_DLOPEN_A_FILES := $(wildcard libc/dlopen/*)
LIBC_DLOPEN_A_HDRS = $(filter %.h,$(LIBC_DLOPEN_A_FILES))
LIBC_DLOPEN_A_SRCS = $(filter %.c,$(LIBC_DLOPEN_A_FILES))
LIBC_DLOPEN_A_OBJS = $(LIBC_DLOPEN_A_SRCS:%.c=o/$(MODE)/%.o)
LIBC_DLOPEN_A_SRCS_C = $(filter %.c,$(LIBC_DLOPEN_A_FILES))
LIBC_DLOPEN_A_SRCS_S = $(filter %.S,$(LIBC_DLOPEN_A_FILES))
LIBC_DLOPEN_A_SRCS = $(LIBC_DLOPEN_A_SRCS_C) $(LIBC_DLOPEN_A_SRCS_S)
LIBC_DLOPEN_A_OBJS = \
$(LIBC_DLOPEN_A_SRCS_C:%.c=o/$(MODE)/%.o) \
$(LIBC_DLOPEN_A_SRCS_S:%.S=o/$(MODE)/%.o)
LIBC_DLOPEN_A_CHECKS = \
$(LIBC_DLOPEN_A).pkg \
@ -45,6 +50,10 @@ $(LIBC_DLOPEN_A_OBJS): private \
-Wframe-larger-than=4096 \
-Walloca-larger-than=4096
# these assembly files are safe to build on aarch64
o/$(MODE)/libc/dlopen/foreign_tramp.o: libc/dlopen/foreign_tramp.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
LIBC_DLOPEN_LIBS = $(foreach x,$(LIBC_DLOPEN_ARTIFACTS),$($(x)))
LIBC_DLOPEN_SRCS = $(foreach x,$(LIBC_DLOPEN_ARTIFACTS),$($(x)_SRCS))
LIBC_DLOPEN_HDRS = $(foreach x,$(LIBC_DLOPEN_ARTIFACTS),$($(x)_HDRS))

View file

@ -116,7 +116,7 @@ struct Loaded {
Elf64_Phdr ph[25];
};
static struct {
struct {
atomic_uint once;
bool is_supported;
struct CosmoTib *tib;
@ -125,9 +125,10 @@ static struct {
int (*dlclose)(void *);
char *(*dlerror)(void);
jmp_buf jb;
} foreign;
} __foreign;
long __sysv2nt14();
long foreign_tramp();
static _Thread_local char dlerror_buf[128];
@ -156,28 +157,6 @@ static int is_file_newer_than(const char *path, const char *other) {
return timespec_cmp(st1.st_mtim, st2.st_mtim) > 0;
}
// on system five we sadly need this brutal trampoline
// todo(jart): add tls trampoline to sigaction() handlers
// todo(jart): morph binary to get tls from host c library
static long foreign_tramp(long a, long b, long c, long d, long e,
long func(long, long, long, long, long, double,
double, double, double, double, double),
double A, double B, double C, double D, double E,
double F) {
long res;
BLOCK_SIGNALS;
#ifdef __x86_64__
struct CosmoTib *tib = __get_tls();
__set_tls(foreign.tib);
#endif
res = func(a, b, c, d, e, A, B, C, D, E, F);
#ifdef __x86_64__
__set_tls(tib);
#endif
ALLOW_SIGNALS;
return res;
}
static unsigned elf2prot(unsigned x) {
unsigned r = 0;
if (x & PF_R) r += PROT_READ;
@ -308,11 +287,11 @@ static long *push_strs(long *sp, char **list, int count) {
}
static wontreturn dontinstrument void foreign_helper(void **p) {
foreign.dlopen = p[0];
foreign.dlsym = p[1];
foreign.dlclose = p[2];
foreign.dlerror = p[3];
longjmp(foreign.jb, 1);
__foreign.dlopen = p[0];
__foreign.dlsym = p[1];
__foreign.dlclose = p[2];
__foreign.dlerror = p[3];
longjmp(__foreign.jb, 1);
}
static dontinline void elf_exec(const char *file, char **envp) {
@ -515,11 +494,11 @@ static uint8_t *movimm(uint8_t p[static 16], int reg, uint64_t val) {
static void *foreign_thunk_sysv(void *func) {
uint8_t *code, *p;
#ifdef __x86_64__
// movabs $func,%r9
// movabs $func,%rax
// movabs $foreign_tramp,%r10
// jmp *%r10
if (!(p = code = foreign_alloc(23))) return 0; // 10 + 10 + 3 = 23
p = movimm(p, 9, (uintptr_t)func);
p = movimm(p, 0, (uintptr_t)func);
p = movimm(p, 10, (uintptr_t)foreign_tramp);
*p++ = 0x41;
*p++ = 0xff;
@ -527,7 +506,7 @@ static void *foreign_thunk_sysv(void *func) {
#elif defined(__aarch64__)
__jit_begin();
if ((p = code = foreign_alloc(36))) {
p = movimm(p, 5, (uintptr_t)func);
p = movimm(p, 8, (uintptr_t)func);
p = movimm(p, 10, (uintptr_t)foreign_tramp);
*(uint32_t *)p = 0xd61f0140; // br x10
__clear_cache(code, p + 4);
@ -671,19 +650,19 @@ static bool foreign_setup(void) {
#ifdef __x86_64__
struct CosmoTib *cosmo_tib = __get_tls();
#endif
if (!setjmp(foreign.jb)) {
if (!setjmp(__foreign.jb)) {
elf_exec(exe, environ);
return false; // if elf_exec() returns, it failed
}
#ifdef __x86_64__
foreign.tib = __get_tls();
__foreign.tib = __get_tls();
__set_tls(cosmo_tib);
#endif
foreign.dlopen = foreign_thunk_sysv(foreign.dlopen);
foreign.dlsym = foreign_thunk_sysv(foreign.dlsym);
foreign.dlclose = foreign_thunk_sysv(foreign.dlclose);
foreign.dlerror = foreign_thunk_sysv(foreign.dlerror);
foreign.is_supported = true;
__foreign.dlopen = foreign_thunk_sysv(__foreign.dlopen);
__foreign.dlsym = foreign_thunk_sysv(__foreign.dlsym);
__foreign.dlclose = foreign_thunk_sysv(__foreign.dlclose);
__foreign.dlerror = foreign_thunk_sysv(__foreign.dlerror);
__foreign.is_supported = true;
return true;
}
@ -693,8 +672,8 @@ static void foreign_once(void) {
static bool foreign_init(void) {
bool res;
cosmo_once(&foreign.once, foreign_once);
if (!(res = foreign.is_supported)) {
cosmo_once(&__foreign.once, foreign_once);
if (!(res = __foreign.is_supported)) {
dlerror_set("dlopen() isn't supported on this platform");
}
return res;
@ -824,7 +803,7 @@ void *cosmo_dlopen(const char *path, int mode) {
dlerror_set("dlopen() isn't supported on OpenBSD yet");
res = 0;
} else if (foreign_init()) {
res = foreign.dlopen(path, mode);
res = __foreign.dlopen(path, mode);
} else {
res = 0;
}
@ -854,7 +833,7 @@ void *cosmo_dlsym(void *handle, const char *name) {
dlerror_set("dlopen() isn't supported on x86-64 MacOS");
func = 0;
} else if (foreign_init()) {
if ((func = foreign.dlsym(handle, name))) {
if ((func = __foreign.dlsym(handle, name))) {
func = foreign_thunk_sysv(func);
}
} else {
@ -880,7 +859,7 @@ int cosmo_dlclose(void *handle) {
dlerror_set("dlopen() isn't supported on x86-64 MacOS");
res = -1;
} else if (foreign_init()) {
res = foreign.dlclose(handle);
res = __foreign.dlclose(handle);
} else {
res = -1;
}
@ -898,7 +877,7 @@ char *cosmo_dlerror(void) {
} else if (IsWindows() || IsXnu()) {
res = dlerror_buf;
} else if (foreign_init()) {
res = foreign.dlerror();
res = __foreign.dlerror();
res = dlerror_set(res);
} else {
res = dlerror_buf;

236
libc/dlopen/foreign_tramp.S Normal file
View file

@ -0,0 +1,236 @@
/*-*- 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 2024 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"
#define SIZE 0x0200
#define SKEW 0x10
foreign_tramp:
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
sub $SIZE,%rsp
// save vector arguments
movdqu %xmm0,-0x10(%rbp)
movdqu %xmm1,-0x20(%rbp)
movdqu %xmm2,-0x30(%rbp)
movdqu %xmm3,-0x40(%rbp)
movdqu %xmm4,-0x50(%rbp)
movdqu %xmm5,-0x60(%rbp)
movdqu %xmm6,-0x70(%rbp)
movdqu %xmm7,-0x80(%rbp)
// save register arguments
mov %rdi,-0x88(%rbp)
mov %rsi,-0x90(%rbp)
mov %rdx,-0x98(%rbp)
mov %rcx,-0xa0(%rbp)
mov %r8,-0xa8(%rbp)
mov %r9,-0xb0(%rbp)
// save function pointer
mov %rax,-0xb8(%rbp)
// block signals
call __sig_block
mov %rax,-0xc0(%rbp)
// switch to foreign tls
mov %fs:0,%rax
mov %rax,-0xc8(%rbp)
mov __foreign+8(%rip),%rdi
call __set_tls
// move stack arguments
movdqu SKEW+0x00(%rbp),%xmm0
movdqu SKEW+0x10(%rbp),%xmm1
movdqu SKEW+0x20(%rbp),%xmm2
movdqu SKEW+0x30(%rbp),%xmm3
movdqu SKEW+0x40(%rbp),%xmm4
movdqu SKEW+0x50(%rbp),%xmm5
movdqu SKEW+0x60(%rbp),%xmm6
movdqu SKEW+0x70(%rbp),%xmm7
movdqu %xmm0,-SIZE+0x00(%rbp)
movdqu %xmm1,-SIZE+0x10(%rbp)
movdqu %xmm2,-SIZE+0x20(%rbp)
movdqu %xmm3,-SIZE+0x30(%rbp)
movdqu %xmm4,-SIZE+0x40(%rbp)
movdqu %xmm5,-SIZE+0x50(%rbp)
movdqu %xmm6,-SIZE+0x60(%rbp)
movdqu %xmm7,-SIZE+0x70(%rbp)
movdqu SKEW+0x80(%rbp),%xmm0
movdqu SKEW+0x90(%rbp),%xmm1
movdqu SKEW+0xa0(%rbp),%xmm2
movdqu SKEW+0xb0(%rbp),%xmm3
movdqu SKEW+0xc0(%rbp),%xmm4
movdqu SKEW+0xd0(%rbp),%xmm5
movdqu SKEW+0xe0(%rbp),%xmm6
movdqu SKEW+0xf0(%rbp),%xmm7
movdqu %xmm0,-SIZE+0x80(%rbp)
movdqu %xmm1,-SIZE+0x90(%rbp)
movdqu %xmm2,-SIZE+0xa0(%rbp)
movdqu %xmm3,-SIZE+0xb0(%rbp)
movdqu %xmm4,-SIZE+0xc0(%rbp)
movdqu %xmm5,-SIZE+0xd0(%rbp)
movdqu %xmm6,-SIZE+0xe0(%rbp)
movdqu %xmm7,-SIZE+0xf0(%rbp)
// restore vector arguments
movdqu -0x10(%rbp),%xmm0
movdqu -0x20(%rbp),%xmm1
movdqu -0x30(%rbp),%xmm2
movdqu -0x40(%rbp),%xmm3
movdqu -0x50(%rbp),%xmm4
movdqu -0x60(%rbp),%xmm5
movdqu -0x70(%rbp),%xmm6
movdqu -0x80(%rbp),%xmm7
// restore register arguments
mov -0x88(%rbp),%rdi
mov -0x90(%rbp),%rsi
mov -0x98(%rbp),%rdx
mov -0xa0(%rbp),%rcx
mov -0xa8(%rbp),%r8
mov -0xb0(%rbp),%r9
// call function
mov -0xb8(%rbp),%rax
call *%rax
// save function result
movdqu %xmm0,-0x10(%rbp)
movdqu %xmm1,-0x20(%rbp)
mov %rax,-0x28(%rbp)
mov %rdx,-0x30(%rbp)
// restore tls
mov -0xc8(%rbp),%rdi
call __set_tls
// unblock signals
mov -0xc0(%rbp),%rdi
call __sig_unblock
// restore function result
mov -0x28(%rbp),%rax
mov -0x30(%rbp),%rdx
movdqu -0x10(%rbp),%xmm0
movdqu -0x20(%rbp),%xmm1
add $SIZE,%rsp
pop %rbp
ret
#elif defined(__aarch64__)
stp x29,x30,[sp,-0x100]!
mov x29,sp
// save vector arguments
stp q0,q1,[sp,0x10]
stp q2,q3,[sp,0x30]
stp q4,q5,[sp,0x50]
stp q6,q7,[sp,0x70]
// save register arguments
stp x0,x1,[sp,0x90]
stp x2,x3,[sp,0xa0]
stp x4,x5,[sp,0xb0]
stp x6,x7,[sp,0xc0]
// save function pointer
str x8,[sp,0xd0]
// block signals
bl __sig_block
str x0,[sp,0xd8]
// move stack arguments
sub sp,sp,#0x100
ldp q0,q1,[sp,SIZE+0x00]
ldp q2,q3,[sp,SIZE+0x20]
ldp q4,q5,[sp,SIZE+0x40]
ldp q6,q7,[sp,SIZE+0x60]
stp q0,q1,[sp,0x00]
stp q2,q3,[sp,0x20]
stp q4,q5,[sp,0x40]
stp q6,q7,[sp,0x60]
ldp q0,q1,[sp,SIZE+0x80]
ldp q2,q3,[sp,SIZE+0xa0]
ldp q4,q5,[sp,SIZE+0xc0]
ldp q6,q7,[sp,SIZE+0xe0]
stp q0,q1,[sp,0x80]
stp q2,q3,[sp,0xa0]
stp q4,q5,[sp,0xc0]
stp q6,q7,[sp,0xe0]
// restore vector arguments
ldp q0,q1,[sp,0x100+0x10]
ldp q2,q3,[sp,0x100+0x30]
ldp q4,q5,[sp,0x100+0x50]
ldp q6,q7,[sp,0x100+0x70]
// restore register arguments
ldp x0,x1,[sp,0x100+0x90]
ldp x2,x3,[sp,0x100+0xa0]
ldp x4,x5,[sp,0x100+0xb0]
ldp x6,x7,[sp,0x100+0xc0]
// call function
ldr x8,[sp,0x100+0xd0]
blr x8
add sp,sp,#0x100
// save vector results
stp q0,q1,[sp,0x10]
stp q2,q3,[sp,0x30]
stp q4,q5,[sp,0x50]
stp q6,q7,[sp,0x70]
// save register results
stp x0,x1,[sp,0x90]
stp x2,x3,[sp,0xa0]
stp x4,x5,[sp,0xb0]
stp x6,x7,[sp,0xc0]
// unblock signals
ldr x0,[sp,0xd8]
bl __sig_unblock
// restore vector results
ldp q0,q1,[sp,0x10]
ldp q2,q3,[sp,0x30]
ldp q4,q5,[sp,0x50]
ldp q6,q7,[sp,0x70]
// restore register results
ldp x0,x1,[sp,0x90]
ldp x2,x3,[sp,0xa0]
ldp x4,x5,[sp,0xb0]
ldp x6,x7,[sp,0xc0]
ldp x29,x30,[sp],0x100
ret
#endif // __x86_64__
.endfn foreign_tramp,globl

View file

@ -7,124 +7,141 @@ TOOL_BUILD_FILES := $(wildcard tool/build/*)
TOOL_BUILD_SRCS = $(filter %.c,$(TOOL_BUILD_FILES))
TOOL_BUILD_HDRS = $(filter %.h,$(TOOL_BUILD_FILES))
TOOL_BUILD_BINS = \
$(TOOL_BUILD_COMS) \
TOOL_BUILD_BINS = \
$(TOOL_BUILD_COMS) \
$(TOOL_BUILD_COMS:%=%.dbg)
TOOL_BUILD_OBJS = \
TOOL_BUILD_OBJS = \
$(TOOL_BUILD_SRCS:%.c=o/$(MODE)/%.o)
TOOL_BUILD_COMS = \
TOOL_BUILD_COMS = \
$(TOOL_BUILD_SRCS:%.c=o/$(MODE)/%.com)
TOOL_BUILD_CHECKS = \
$(TOOL_BUILD).pkg \
TOOL_BUILD_CHECKS = \
$(TOOL_BUILD).pkg \
$(TOOL_BUILD_HDRS:%=o/$(MODE)/%.ok)
TOOL_BUILD_DIRECTDEPS = \
DSP_CORE \
DSP_SCALE \
DSP_TTY \
LIBC_CALLS \
LIBC_ELF \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_NT_PSAPI \
LIBC_NT_USER32 \
LIBC_NT_WS2_32 \
LIBC_PROC \
LIBC_RUNTIME \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_X \
NET_HTTP \
NET_HTTPS \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GDTOA \
THIRD_PARTY_GETOPT \
THIRD_PARTY_MBEDTLS \
THIRD_PARTY_MUSL \
THIRD_PARTY_REGEX \
THIRD_PARTY_STB \
THIRD_PARTY_XED \
THIRD_PARTY_ZLIB \
THIRD_PARTY_ZLIB_GZ \
TOOL_BUILD_DIRECTDEPS = \
DSP_CORE \
DSP_SCALE \
DSP_TTY \
LIBC_CALLS \
LIBC_DLOPEN \
LIBC_ELF \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \
LIBC_NT_PSAPI \
LIBC_NT_USER32 \
LIBC_NT_WS2_32 \
LIBC_PROC \
LIBC_RUNTIME \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \
LIBC_TIME \
LIBC_TINYMATH \
LIBC_X \
NET_HTTP \
NET_HTTPS \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GDTOA \
THIRD_PARTY_GETOPT \
THIRD_PARTY_MBEDTLS \
THIRD_PARTY_MUSL \
THIRD_PARTY_REGEX \
THIRD_PARTY_STB \
THIRD_PARTY_XED \
THIRD_PARTY_ZLIB \
THIRD_PARTY_ZLIB_GZ \
TOOL_BUILD_LIB
TOOL_BUILD_DEPS := \
TOOL_BUILD_DEPS := \
$(call uniq,$(foreach x,$(TOOL_BUILD_DIRECTDEPS),$($(x))))
o/$(MODE)/tool/build/build.pkg: \
$(TOOL_BUILD_OBJS) \
o/$(MODE)/tool/build/build.pkg: \
$(TOOL_BUILD_OBJS) \
$(foreach x,$(TOOL_BUILD_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/tool/build/%.com.dbg: \
$(TOOL_BUILD_DEPS) \
o/$(MODE)/tool/build/build.pkg \
o/$(MODE)/tool/build/%.o \
$(CRT) \
o/$(MODE)/tool/build/%.com.dbg: \
$(TOOL_BUILD_DEPS) \
o/$(MODE)/tool/build/build.pkg \
o/$(MODE)/tool/build/%.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/tool/build/dso/sandbox-$(ARCH).so.zip.o \
o/$(MODE)/tool/build/false.com.zip.o \
o/$(MODE)/tool/build/echo.com.zip.o \
o/$(MODE)/tool/build/cocmd.com.zip.o: private \
ZIPOBJ_FLAGS += \
o/$(MODE)/tool/build/dso/sandbox-$(ARCH).so.zip.o \
o/$(MODE)/tool/build/false.com.zip.o \
o/$(MODE)/tool/build/echo.com.zip.o \
o/$(MODE)/tool/build/cocmd.com.zip.o: private \
ZIPOBJ_FLAGS += \
-B
# we need pic because:
# so it can be an LD_PRELOAD payload
o/$(MODE)/tool/build/dso/sandbox.o: private \
CFLAGS += \
o/$(MODE)/tool/build/dso/sandbox.o: private \
CFLAGS += \
-fPIC
o/$(MODE)/tool/build/dso/sandbox.o: \
libc/calls/calls.h \
tool/build/dso/sandbox.c \
libc/calls/pledge.h \
libc/runtime/runtime.h \
libc/calls/pledge.internal.h \
libc/intrin/promises.internal.h \
o/$(MODE)/tool/build/dso/sandbox.o: \
libc/calls/calls.h \
tool/build/dso/sandbox.c \
libc/calls/pledge.h \
libc/runtime/runtime.h \
libc/calls/pledge.internal.h \
libc/intrin/promises.internal.h \
tool/build/BUILD.mk
o/$(MODE)/tool/build/dso/sandbox-$(ARCH).so: \
o/$(MODE)/tool/build/dso/sandbox.o \
o/$(MODE)/libc/calls/pledge-linux.o \
o/$(MODE)/tool/build/dso/sandbox-$(ARCH).so: \
o/$(MODE)/tool/build/dso/sandbox.o \
o/$(MODE)/libc/calls/pledge-linux.o \
o/$(MODE)/libc/sysv/restorert.o
@$(COMPILE) -ALINK.so \
$(CC) \
-s \
-shared \
-nostdlib \
-fuse-ld=bfd \
-Wl,--gc-sections \
o/$(MODE)/tool/build/dso/sandbox.o \
o/$(MODE)/libc/calls/pledge-linux.o \
o/$(MODE)/libc/sysv/restorert.o \
@$(COMPILE) -ALINK.so \
$(CC) \
-s \
-shared \
-nostdlib \
-fuse-ld=bfd \
-Wl,--gc-sections \
o/$(MODE)/tool/build/dso/sandbox.o \
o/$(MODE)/libc/calls/pledge-linux.o \
o/$(MODE)/libc/sysv/restorert.o \
$(OUTPUT_OPTION)
o/$(MODE)/tool/build/pledge.com.dbg: \
$(TOOL_BUILD_DEPS) \
o/$(MODE)/tool/build/build.pkg \
o/$(MODE)/tool/build/dso/sandbox-$(ARCH).so.zip.o \
o/$(MODE)/tool/build/pledge.o \
$(CRT) \
o/$(MODE)/tool/build/pledge.com.dbg: \
$(TOOL_BUILD_DEPS) \
o/$(MODE)/tool/build/build.pkg \
o/$(MODE)/tool/build/dso/sandbox-$(ARCH).so.zip.o \
o/$(MODE)/tool/build/pledge.o \
$(CRT) \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/tool/build/dso/dlopen_helper.so: \
o/$(MODE)/tool/build/dso/dlopen_helper.o
@$(COMPILE) -ALINK.so \
$(CC) \
-s \
-shared \
-nostdlib \
-fuse-ld=bfd \
o/$(MODE)/tool/build/dso/dlopen_helper.o \
$(OUTPUT_OPTION)
o/$(MODE)/tool/build/dlopen_test.com.runs: \
o/$(MODE)/tool/build/dlopen_test.com \
o/$(MODE)/tool/build/dso/dlopen_helper.so
$< o/$(MODE)/tool/build/dso/dlopen_helper.so
.PHONY: o/$(MODE)/tool/build
o/$(MODE)/tool/build: \
o/$(MODE)/tool/build/lib \
$(TOOL_BUILD_BINS) \
o/$(MODE)/tool/build: \
o/$(MODE)/tool/build/lib \
$(TOOL_BUILD_BINS) \
$(TOOL_BUILD_CHECKS)

126
tool/build/dlopen_test.c Normal file
View file

@ -0,0 +1,126 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 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/calls/calls.h"
#include "libc/dlopen/dlfcn.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
struct big {
long x1;
long x2;
long x3;
long x4;
long x5;
long x6;
long x7;
long x8;
long x9;
double f0;
double f1;
};
int main(int argc, char *argv[]) {
void *lib = cosmo_dlopen(argv[1], RTLD_LAZY);
if (!lib) {
tinyprint(2, "cosmo_dlopen() failed: ", cosmo_dlerror(), "\n", NULL);
exit(1);
}
fprintf(stderr, "opened library!\n");
long (*big)(struct big *, long, long, long, long, long, long, long, long,
long, double, double) = cosmo_dlsym(lib, "big");
if (!big) {
tinyprint(2, "cosmo_dlsym() failed: ", cosmo_dlerror(), "\n", NULL);
exit(1);
}
fprintf(stderr, "imported symbol!\n");
struct big args = {0};
long res = big(&args, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3.14, 2.3);
int fails = 0;
if (res != 31337) {
fprintf(stderr, "error: bad result: %016lx\n", res);
++fails;
}
if (args.x1 != 1) {
fprintf(stderr, "error: bad x1: %016lx\n", args.x1);
++fails;
}
if (args.x2 != 2) {
fprintf(stderr, "error: bad x2: %016lx\n", args.x2);
++fails;
}
if (args.x3 != 3) {
fprintf(stderr, "error: bad x3: %016lx\n", args.x3);
++fails;
}
if (args.x4 != 4) {
fprintf(stderr, "error: bad x4: %016lx\n", args.x4);
++fails;
}
if (args.x5 != 5) {
fprintf(stderr, "error: bad x5: %016lx\n", args.x5);
++fails;
}
if (args.x6 != 6) {
fprintf(stderr, "error: bad x6: %016lx\n", args.x6);
++fails;
}
if (args.x7 != 7) {
fprintf(stderr, "error: bad x7: %016lx\n", args.x7);
++fails;
}
if (args.x8 != 8) {
fprintf(stderr, "error: bad x8: %016lx\n", args.x8);
++fails;
}
if (args.x9 != 9) {
fprintf(stderr, "error: bad x9: %016lx\n", args.x9);
++fails;
}
if (args.f0 != 3.14) {
fprintf(stderr, "error: bad f0: %g\n", args.f0);
++fails;
}
if (args.f1 != 2.3) {
fprintf(stderr, "error: bad f1: %g\n", args.f1);
++fails;
}
cosmo_dlclose(lib);
return fails;
}

View file

@ -0,0 +1,48 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 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.
*/
struct big {
long x1;
long x2;
long x3;
long x4;
long x5;
long x6;
long x7;
long x8;
long x9;
double f0;
double f1;
};
long big(struct big *x0, long x1, long x2, long x3, long x4, long x5, long x6,
long x7, long x8, long x9, double f0, double f1) {
x0->x1 = x1;
x0->x2 = x2;
x0->x3 = x3;
x0->x4 = x4;
x0->x5 = x5;
x0->x6 = x6;
x0->x7 = x7;
x0->x8 = x8;
x0->x9 = x9;
x0->f0 = f0;
x0->f1 = f1;
return 31337;
}