-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linking with C libraries #2
Comments
Can't it just pass the real main to libc? If you aren't using libc, c-scape could provide it's own |
Great, thanks for finding that!
That would work, but I'd prefer |
It's currently not safe to link with C libraries, because Mustang doesn't initialize the C runtime. I've now added a point about this to the README.md. But is there a way Mustang could make this work?
__libc_start_main
appears to be a relatively stable interface, that looks like it might work in both glibc and musl. Mustang could call it and pass a no-opmain
to let libc initialize itself, but the tricky part is, it callsexit
when it's done.Since
mustang
defines its ownexit
, one option here would be formustang
to call__libc_start_main
and "catch" theexit
. But then, what should it do? GLIBC's__libc_start_main
is annotated with__attribute__ ((noreturn)
, and the C interface toexit
uses__attribute__((noreturn))
, so we can't haveexit
return. Another option would belongjmp
, but we can't currently dosetjmp
in Rust. Another would be to panic and usecatch_unwind
, but a panic across an FFI boundary is UB.Another idea is to create a new thread, call
__libc_start_main
on it, and then terminate the thread once it callsexit
. But, Mustang doesn't support threads yet. It's tempting to just call libc'spthread_create
et al in that case, however we can't safely call C code until the C runtime is initialized, which is what we're trying to do :-}.I'm thinking
longjmp
may be the best option for now. A C-library compatibility mode would be all about having C code in the process, so having a small amount of extra C code to callsetjmp
would be fine.The text was updated successfully, but these errors were encountered: