Skip to content

Commit

Permalink
Introduce dlopen() support
Browse files Browse the repository at this point in the history
Every program built using Cosmopolitan is statically-linked. However
there are some cases, e.g. GUIs and video drivers, where linking the
host platform libraries is desirable. So what we do in such cases is
launch a stub executable using the host platform's libc, and longjmp
back into this executable. The stub executable passes back to us the
platform-specific dlopen() implementation, which we shall then wrap.

Here's the list of platforms that are supported so far:

- x86-64 Linux w/ Glibc
- x86-64 Linux w/ Musl Libc
- x86-64 FreeBSD
- x86-64 Windows
- aarch64 Linux w/ Glibc
- aarch64 MacOS

What this means is your Cosmo programs can call foreign functions on
your host operating system. However, it's important to note that any
foreign library you link won't have the ability to call functions in
your Cosmopolitan program. For example it's now technically possible
that Lua can load a module, however that almost certainly won't work
since the Lua module won't have access to Cosmo's Lua API.

Kudos to @jacereda for figuring out how to do this.
  • Loading branch information
jart committed Nov 3, 2023
1 parent 1eb6484 commit 5e8c928
Show file tree
Hide file tree
Showing 27 changed files with 753 additions and 53 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ include libc/calls/calls.mk #─┐
include libc/irq/irq.mk # ├──SYSTEMS RUNTIME
include third_party/nsync/nsync.mk # │ You can issue system calls
include libc/runtime/runtime.mk #
include libc/dlopen/dlopen.mk #
include third_party/double-conversion/dc.mk #
include libc/crt/crt.mk #
include third_party/dlmalloc/dlmalloc.mk #─┘
Expand Down
12 changes: 11 additions & 1 deletion ape/ape-m1.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
#include <dlfcn.h>

#define pagesz 16384
#define SYSLIB_MAGIC ('s' | 'l' << 8 | 'i' << 16 | 'b' << 24)
#define SYSLIB_VERSION 5
#define SYSLIB_VERSION 6

struct Syslib {
int magic;
Expand Down Expand Up @@ -91,6 +92,11 @@ struct Syslib {
long (*sem_trywait)(int *);
long (*getrlimit)(int, struct rlimit *);
long (*setrlimit)(int, const struct rlimit *);
// v6 (2023-11-03)
void *(*dlopen)(const char *, int);
void *(*dlsym)(void *, const char *);
int (*dlclose)(void *);
char *(*dlerror)(void);
};

#define ELFCLASS32 1
Expand Down Expand Up @@ -943,6 +949,10 @@ int main(int argc, char **argv, char **envp) {
M->lib.sem_trywait = sys_sem_trywait;
M->lib.getrlimit = sys_getrlimit;
M->lib.setrlimit = sys_setrlimit;
M->lib.dlopen = dlopen;
M->lib.dlsym = dlsym;
M->lib.dlclose = dlclose;
M->lib.dlerror = dlerror;

/* getenv("_") is close enough to at_execfn */
execfn = argc > 0 ? argv[0] : 0;
Expand Down
55 changes: 55 additions & 0 deletions examples/dlopen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
│ To the extent possible under law, Justine Tunney has waived │
│ all copyright and related or neighboring rights to this file, │
│ as it is written in the following disclaimers: │
│ • http://unlicense.org/ │
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
╚─────────────────────────────────────────────────────────────────*/
#endif
#include "libc/calls/calls.h"
#include "libc/dlopen/dlfcn.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/runtime/runtime.h"

/**
* @fileoverview cosmopolitan dynamic runtime linking demo
*
* Our dlopen() interface currently supports:
*
* - x86-64 Linux w/ Glibc
* - x86-64 Linux w/ Musl Libc
* - x86-64 FreeBSD
* - x86-64 Windows
* - aarch64 Linux w/ Glibc
* - aarch64 MacOS
*
*/

int main(int argc, char **argv, char **envp) {

// open the host system's zlib library
void *libc = dlopen("libz.so", RTLD_LAZY);
if (!libc) {
tinyprint(2, dlerror(), "\n", NULL);
exit(1);
}

// load crc() function address
unsigned (*crc32)(unsigned, void *, int) = dlsym(libc, "crc32");
if (!crc32) {
tinyprint(2, dlerror(), "\n", NULL);
exit(1);
}

// compute a checksum and print the result
char ibuf[12];
FormatInt32(ibuf, crc32(0, "hi", 2));
tinyprint(1, "crc(hi) = ", ibuf, "\n", NULL);

// mop up
dlclose(libc);
exit(0);
}
1 change: 1 addition & 0 deletions examples/examples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ EXAMPLES_DIRECTDEPS = \
DSP_SCALE \
DSP_TTY \
LIBC_CALLS \
LIBC_DLOPEN \
LIBC_DNS \
LIBC_FMT \
LIBC_INTRIN \
Expand Down
Binary file added libc/dlopen/.dlopen.aarch64.glibc.elf
Binary file not shown.
Binary file added libc/dlopen/.dlopen.x86_64.freebsd.elf
Binary file not shown.
Binary file added libc/dlopen/.dlopen.x86_64.glibc.elf
Binary file not shown.
Binary file added libc/dlopen/.dlopen.x86_64.musl.elf
Binary file not shown.
6 changes: 3 additions & 3 deletions libc/runtime/dlfcn.h → libc/dlopen/dlfcn.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_
#define COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_
#ifndef COSMOPOLITAN_LIBC_DLFCN_H_
#define COSMOPOLITAN_LIBC_DLFCN_H_

#define RTLD_LOCAL 0
#define RTLD_LAZY 1
Expand All @@ -20,4 +20,4 @@ int dl_iterate_phdr(int (*)(void *, size_t, void *), void *);

COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_ */
#endif /* COSMOPOLITAN_LIBC_DLFCN_H_ */
Loading

0 comments on commit 5e8c928

Please sign in to comment.