Search code examples
rustformattingx86-64elfosdev

Problems with rust formatting (x86-unknown-none target)


I'm writing an x86_64 Os in rust and testing it on qemu pc. I've implemented ELF file loading and I'm 100% sure the file gets properly loaded and mapped, as I have manually checked memory.

My issue is that when I try to use rust formatting in println!, the program jumps to a random address in page 0, which should be part of a read only segment. After this, some data gets interpreted as instructions and as a result I either get a page fault or an invalid opcode fault. Printing strings with no formatting works fine, but as soon as I try to print a string with a formatted integer in the middle I get this issue. The ELF executable is compiled with the x86_64-unknown-none target.

Here is the github repository for my project: https://github.com/gillo04/alba/tree/main/kernel/src

In the user1/ directory you can find the source for the program I'm trying to execute. In the kernel/src/main.rs file, at the bottom of the main function you can find the code I use to load the executable and jump to it. Thanks for the help!


Solution

  • Apparently the generated ELF needed some relocations to work properly. Adding rusflags=["-C", "relocation-model=static"] to my .cargo/config.toml file fixed the issue, removing the relocations