Introduce dlopen() support

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.
This commit is contained in:
Justine Tunney 2023-11-03 06:04:13 -07:00
parent 1eb6484c9c
commit 5e8c928f1a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
27 changed files with 753 additions and 53 deletions

View file

@ -1,23 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_
#define COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_
#define RTLD_LOCAL 0
#define RTLD_LAZY 1
#define RTLD_NOW 2
#define RTLD_GLOBAL 256
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define RTLD_NEXT ((void *)-1)
#define RTLD_DEFAULT ((void *)0)
char *dlerror(void);
void *dlopen(const char *, int);
void *dlsym(void *, const char *);
int dlclose(void *);
int dl_iterate_phdr(int (*)(void *, size_t, void *), void *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_DLFCN_H_ */

0
libc/runtime/elf_loader.h Executable file
View file

View file

@ -1,40 +0,0 @@
/*-*- 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 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
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/runtime/dlfcn.h"
char *dlerror(void) {
return "cosmopolitan doesn't support dsos";
}
void *dlopen(const char *file, int mode) {
return NULL;
}
void *dlsym(void *handle, const char *name) {
return NULL;
}
int dlclose(void *handle) {
return -1;
}
int dl_iterate_phdr(int callback(void *info, size_t size, void *data),
void *data) {
return -1;
}

View file

@ -12,7 +12,7 @@ COSMOPOLITAN_C_START_
*/
#define SYSLIB_MAGIC ('s' | 'l' << 8 | 'i' << 16 | 'b' << 24)
#define SYSLIB_VERSION 4
#define SYSLIB_VERSION 6
typedef uint64_t dispatch_time_t;
typedef uint64_t dispatch_semaphore_t;
@ -70,6 +70,11 @@ struct Syslib {
long (*__sem_trywait)(int *);
long (*__getrlimit)(int, void *);
long (*__setrlimit)(int, const void *);
// v6 (2023-11-03)
void *(*__dlopen)(const char *, int);
void *(*__dlsym)(void *, const char *);
int (*__dlclose)(void *);
char *(*__dlerror)(void);
};
extern struct Syslib *__syslib;