Stream: cranelift

Topic: Module Documentation or Examples


view this post on Zulip Chris Clark (Sep 06 2023 at 02:35):

Does anyone have any documentation on using cranelift-module or thorough examples?

view this post on Zulip Afonso Bordado (Sep 06 2023 at 09:45):

Hey, we don't have great documentation on this. My understanding is that you shouldn't really use cranelift-module directly, it mostly exposes a trait that other crates implement.

One of them is cranelift-object that provides a module that allows you to produce object files. The other one is cranelift-jit that provides a module that can be run with the JIT.

If you are interested in the JIT part, cranelift-jit-demo has a good example of how it's used. If you are interested in the the object emission part we have an example in clif-util but it's mostly the same thing.

view this post on Zulip Afonso Bordado (Sep 06 2023 at 09:47):

There's also this example on declaring data sections

view this post on Zulip Chris Clark (Sep 06 2023 at 18:04):

Afonso Bordado said:

Ahh, okay. I am looking at producing my first executable with a main function in CLIR. So, I need to use cranelift-object to generate the .o file, and then use a linker to make it an executable. I will look at the data section link you provided. That is the biggest gap in my understanding, as well as how to pass the function to object, and how to manipulate the global data, and use it within the function. But I just want to set up the main.o file first with a simple return of 42 :) I will review the code and examples tonight. Thank you!

view this post on Zulip Afonso Bordado (Sep 06 2023 at 19:01):

Oh, right you might also want to look at this message, since I also have some other examples there (the linking part):

https://bytecodealliance.zulipchat.com/#narrow/stream/217117-cranelift/topic/generate.20binary/near/388828156

view this post on Zulip Chris Clark (Sep 06 2023 at 21:55):

Afonso Bordado said:

Oh, right you might also want to look at this message, since I also have some other examples there (the linking part):

https://bytecodealliance.zulipchat.com/#narrow/stream/217117-cranelift/topic/generate.20binary/near/388828156

So, saltwater is your project which can do linking outside of the need for gcc or llvm's linker? That's nice. Any plans to include it in the cranelift project?

view this post on Zulip Afonso Bordado (Sep 06 2023 at 21:59):

I'm sort of just maintaining it, Unfortunately I don't really have time to do much work on it. But yeah, its a C compiler that is fully based on cranelift. It doesent do linking itself, it calls into the system linker to do that.

I'm not sure its worth merging with the cranelift project, It's not a very active project, and I think it makes sense to keep them separate

view this post on Zulip Juan Bono (Sep 07 2023 at 00:10):

@Chris Clark I'm trying to do the same hahaha. I'm going to check the links that Afonso shared next weekend. I'll tell you if I come up with a simple way of making a simple object file that returns a number. I already have the generated clif. But I'm missing the object and linking part

view this post on Zulip Chris Clark (Sep 08 2023 at 15:21):

@Juan Bono I have been sick these past few days, and now we have a huge install at work this evening. I'm not sure when I'm going to be able to look at this. Everywhere I go, i can't find a linker as a library. I must call out to an external linker. Zig is getting closer every day. I might require an external linker for now, and then use zigs later.

view this post on Zulip Juan Bono (Sep 09 2023 at 01:07):

I was able to generate the main.o file and then link it with gcc. I pasted an example main function with that https://gist.github.com/juanbono/9ad4d22e99ea73579691bcf60c765d42 . I wasn't able to find linker lib. I saw that saltwater just calls cc linker using std::process::Command. That's another option so you don't have to call another program after you generate the object file.

cranelift binary. GitHub Gist: instantly share code, notes, and snippets.

view this post on Zulip Chris Clark (Sep 09 2023 at 18:20):

@Juan Bono this is great thank you. I'll take a look

view this post on Zulip Chris Clark (Sep 09 2023 at 18:53):

Finally got to sit in front of the computer for a bit, you did all the hard work there. Great job. I got the objdump of main looking good. but when trying to link it complains of no _start, and then I get a segfault when running it. Possibly because push %rbp isn't allowed. I'll have to look at it again later.

objdump -d main.o
...
0000000000000000 <main>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   b8 05 00 00 00          mov    $0x5,%eax
   9:   48 89 ec                mov    %rbp,%rsp
   c:   5d                      pop    %rbp
   d:   c3                      retq

view this post on Zulip Chris Clark (Sep 09 2023 at 20:38):

I used gcc, and that worked. it was ld by itself that had the problem.

view this post on Zulip Juan Bono (Sep 10 2023 at 13:35):

Great! I found there is a cc crate that can be used to programmatically link the object file https://github.com/rust-lang/cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library - GitHub - rust-lang/cc-rs: Rust library for build scripts to compile C/C++ code into a Rust library

view this post on Zulip bjorn3 (Sep 10 2023 at 17:04):

Chris Clark said:

I used gcc, and that worked. it was ld by itself that had the problem.

Yeah, gcc and clang wrap ld and add a ton of extra flags for it to for example find the C standard library and the crt object which defines _start.


Last updated: Oct 23 2024 at 20:03 UTC