Skip to content
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

Generating executable entrypoints with cranelift-object #5996

Closed
a-nick-fischer opened this issue Mar 11, 2023 · 2 comments
Closed

Generating executable entrypoints with cranelift-object #5996

a-nick-fischer opened this issue Mar 11, 2023 · 2 comments

Comments

@a-nick-fischer
Copy link

Feature

As far as I can tell, there's no build-in way to generate an entrypoint (_start) when generating an object file using cranelift-object. Please correct me if I'm mistaken, but I think this makes it impossible to actually produce a properly executable file using only cranelift and a linker.

Two solutions came to my mind:

  • A function like define_entrypoint, which let's us define an entrypoint directly (_start). One could define a custom entrypoint similar to how one defines a function now.
  • cranelift-object defines a default entrypoint, calling a main-function (similar to what C does)

Note: I tried defining _start as a function which obviously didn't work out.

Benefit

Being able to actually generate runnable executables with cranelift-object.

Implementation

I'm definitely not qualified to fill this section in :P

Alternatives

Some alternative approaches come to mind:

  • Compile the object file to a dynamic library and link it with a "boot"-program written in C/Rust, which calls a custom entrypoint in our "library". Requires an extra toolchain to be installed.
  • Do what saltwater does and link it using cc, which apperantely insert a valid entrypoint calling main. This is also the only project I came across which uses cranelift-object. Requires an extra toolchain to be installed.
  • Using cranelift-faerie, which is deprecated. Not sure if it has the same problem, through.
  • I'm yet to reverse-engineer how rustc-codegen-cranelift does it
@bjorn3
Copy link
Contributor

bjorn3 commented Mar 11, 2023

cranelift-object only produces object files. You need to link them yourself to get an executable or dynamic library. If you link you will either have to provide the right crt.o to define the _start function or you have gcc/clang provide it for you. cranelift-object can't know what the right contents are. On some systems it calls __libc_start_main, on others it calls the constrictors and directly jumps to main and on yet other systems it does low level hardware initialization and sets up a stack before calling main.

I did just recommend defining main and then using gcc or clang to link the executable. This is what rustc_codegen_cranelift does too.

@a-nick-fischer
Copy link
Author

Alright I see. I'll be going with the gcc solution then too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants