Hi!
I'll firstly describe my problem really quick. I am trying to make a really low level linux app in an environment without any libc and also don't link against musl, so I use x86_64-unknown-linux-none
. Now in the _start
function, I want to retrieve the argc and argv, which should be laying on the stack at calling. The only problem is that I have function prologue in front of me, and there doesn't seem to be any calling convention in rust which loads parameters like given :C. So I tried writing that myself with global_asm and then naked functions, but both come to the same point: I have the data from the stack, but don't want to call the main method with all specific argument call conventions in pure C. Is there a way to remove the prologue of a function without allowing just naked_asm
in it's body?
Thanks
Thanks for including a high level problem! This makes it much easier to answer your request.
It's worse than that: such calling convention simply doesn't exist. You can read about how parameters are passed to ELF file in this article… but they they not passed in accordance to any known calling convention for any high-level language.
That's why crt0.o
in any libc[1] is written in assembler. What you see passed into main
is produced by a tiny piece of assembler code in crt0.S
file, it doesn't come directly from kernel. You would have to do the same, I'm afraid.
Not yet. The need for that is well-understood, that's called “naked function” and RFC is there… but it's unstable.
Any libs that I saw, anyway. ↩︎
Isn't that the RFC for the normal #[naked]
attribute? My problem is that when I do that, I also have to call my main manually with the needed convention. I basically want that to compile:
#[naked]
#[no_mangle]
fn _start() -> ! {
let argc: ffi::c_int;
unsafe {
naked_asm! {
"pop {0}",
out(reg) argc,
}
}
main(argc)
}
but it seems like this is impossible? :c
Of course it's not possible! Where would that argc
variable live? In a thin ether? Either rust is responsible for the allocation of variables (like in normal function) or it's not responsible (as in naked function). Describing semantic of what you want would be so complex that there's absolutely zero chance that anyone would want to even read about it. Just look on what you wrote:
Now your stack pointer is not aligned properly… but how may Rust even know that and accomodate that?
Well, that RFC is really outdated and doesn't agree with the current functionality any more!
The useful links for naked function are these:
- Stabilize `naked_functions` by folkertdev · Pull Request #134213 · rust-lang/rust · GitHub which proposes to stablize it and is in FCP since last month
- naked functions by folkertdev · Pull Request #1689 · rust-lang/reference · GitHub which updates the Reference as a draft
Maybe the origin
crate can help you?
wow that is great!