Does anyone have any documentation on using cranelift-module or thorough examples?
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.
There's also this example on declaring data sections
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!
Oh, right you might also want to look at this message, since I also have some other examples there (the linking part):
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):
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?
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
@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
@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.
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.
@Juan Bono this is great thank you. I'll take a look
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
I used gcc, and that worked. it was ld by itself that had the problem.
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
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: Nov 22 2024 at 16:03 UTC