#Compiling to raw function + data
12 messages · Page 1 of 1 (latest)
So if you want to call printf or something, you have to pass it as a function pointer into the loaded function.
Hmm, it looks like this is a bit trickier for Rust than for C, since it wants to run static initializers and destructors
Here's a starting point that might help you:
lib.rs:
#![no_std]
use core::panic::PanicInfo;
#[panic_handler]
fn panic(_: &PanicInfo) -> ! {
loop {}
}
#[no_mangle]
pub extern "C" fn entry(print: extern "C" fn(*const u8)) {
print(b"Hello, world!\0".as_ptr());
}
Cargo.toml:
[lib]
crate-type = ["cdylib"]
[profile.release]
panic = "abort"
build.rs:
fn main() {
println!("cargo:rustc-link-arg=-Tflat.ld");
}
flat.ld:
OUTPUT_FORMAT(elf64-x86-64)
SECTIONS
{
.data : { *(.text.entry) *(*) }
}
Good luck trying to make the output relocatable, it's definitely easier as a static binary
For whatever reason it keeps wanting to create a GOT if I make it position-independent
(oh, and you can get a raw binary with objcopy -O binary target/release/libexample.so example.bin, but always check to make sure it applied the relocations correctly)
swap the bytes of each word; objdump is showing you each word in big endian