-
-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
27 changed files
with
753 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.