-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments for "https://os.phil-opp.com/freestanding-rust-binary/" #386
Comments
There is a broken link which isn't actually formatted as a link: [name mangling][mangling] |
Thanks! Fixed in 8bd66bf. |
Both the first and second editions of these series are invaluable. Thank you for every bit of clearly written information you’ve shared ✨ |
@memoryruins Thanks so much! |
The Windows subsystem information link is outdated. The page behind it is pretty much unreadable and suggests to link to here instead: https://docs.microsoft.com/en-us/cpp/build/reference/entry-entry-point-symbol |
@CornedBee Thanks for the hint! Fixed in 6599a69. |
In the windows part of the post, the CONSOLE subsystem entry points are used as an example, then immediately the Windows subsystems entry points are referenced, i got a feeling that some explication is missing there. |
Is the final code sample is supposed to build wihtout errors ? Compilator ask me to import PanicInfo |
@sepiropht Thanks for reporting. Seems like we forgot to add the imports in the latest update. I opened #440 to fix this. |
@phil-opp , thanks for a great series. Really well written and well explained (so much beginner-friendly). What do you think about adding the platform annotations for the trampoline functions? #[cfg(target_os = "windows")]
#[no_mangle]
pub extern "C" fn mainCRTStartup() -> ! {
main();
} |
Thanks a lot!
These functions are just used to get a compiling no_std executable on existing operating systems in the first post. It wouldn't make sense to keep them for the subsequent posts because our OS kernel won't ever run on top of Windows or macOS. Only adding the conditional compilation for the first post would be possible, but I don't think that it's worth it because it makes things more complicated and the functions are removed in the second post anyway. |
But you have two main() functions, one for Windows and one for MacOS. |
@AregevDev You should only use one of the three variants, depending on your host OS. So if you're on Windows, only add the Windows-style entry point and not the Linux and macOS entry points. |
@AregevDev An alternative approach to having os-specific entrypoints would be the introduction of a custom linker script. There, you can define the entry-point function via I think the first edition of the blog talked about it. One way or the other, you have a little piece of inconvenience. When writing a kernel, though, in the long run you will probably end up with a custom linker script at some point anyways. |
panic_implementation : this attribute was renamed to |
In addition to the panic_implementation being renamed, using the new name means you don't need the #![feature(panic_implementation)] anymore (or I believe the nightly?). |
@jaypatelani @jessejhein Thanks for reporting. I updated the blog in #468. |
As of rustc 1.29.2 stable it is still necessary to use the nightly toolchain on Linux (and probably Mac), as the '-Z' option works only in nightly. |
Hi, playing around I'm getting the following linker error with Rust nightly build 1.31.0. rustc 1.31.0-nightly (3e6f30ec3 2018-10-26)
error: linking with `cc` failed: exit code: 1
|
= note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-nostartfiles" "-L" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526.1lwzcx53g884pkrj.rcgu.o" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526.1mxek59mmosxp1gr.rcgu.o" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526.3vwhvtx17707okjv.rcgu.o" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526.4k2fyypy62ab6aaa.rcgu.o" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526.pmfm3pkes9s56eu.rcgu.o" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526.u78q3renarfctfb.rcgu.o" "-o" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526" "/home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526.36u6sdy3tifj0l3t.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/devop/workspace/tactical_os/target/debug/deps" "-L" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-749cdbe450dd7140.rlib" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_abort-b02d3fe29f436127.rlib" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-cf548d84ef70130f.rlib" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-1d3e911ab051fda4.rlib" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_system-b9cd5a1dfd88584e.rlib" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-8f294fb62aa82289.rlib" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-b6501604988320ff.rlib" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-56f5587bc5216ac9.rlib" "-Wl,--end-group" "/home/devop/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-e578c298dc2b65d9.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil"
= note: /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000003370
/usr/lib/x86_64-linux-gnu/libpthread_nonshared.a(pthread_atfork.oS): In function `__pthread_atfork':
/build/glibc-OTsEL5/glibc-2.27/nptl/pthread_atfork.c:51: undefined reference to `__dso_handle'
/usr/lib/x86_64-linux-gnu/libc_nonshared.a(atexit.oS): In function `atexit':
(.text+0x3): undefined reference to `__dso_handle'
/usr/bin/ld: /home/devop/workspace/tactical_os/target/debug/deps/tactical_os-091a9ff38ca58526: hidden symbol `__dso_handle' isn't defined
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
error: aborting due to previous error
error: Could not compile `tactical_os`.
To learn more, run the command again with --verbose. |
@fcogama I never seen this one before. Are you using Linux? If yes, are you using a |
With current rustc nightly, the errors printed with no panic handler defined no longer references
|
This was super useful to get me started. Once I did, I ran into some seemingly arbitrary segfaults. Turns out the libc initialization does something useful after all! It aligns the stack to 16 bytes which is necessary for SSE instructions. So if you find that you're reliably getting SIGSEGV when executing #[no_mangle]
fn main() {
// the actual main
}
#[no_mangle]
pub unsafe extern "C" fn _start() {
// Align the stack to the 16 byte boundary as the stdlib C preamble would
asm!(
"
and $$0xfffffffffffffff0, %rsp;
call main;
"
);
} |
@guncha Yeah, the x86_64 architecture requires that the stack is aligned on each function entry. The bootloader that we add in the next post should do this for our kernel. |
Adding the compiler flags in a [build]
rustflags = ["-C", "link-arg=-nostartfiles"] keeps the build commands consistent accross OSes ( |
@mfournial Sorry for the late reply!
So we don't need to pass |
@reesmanp I also thought about adding conditional compilation and a |
After rereading all the feedback in this thread, I decided to rewrite the section about overwriting the entry point. It now uses a different approach to fixing the linker errors: compile for a bare metal target. The existing section about the linker arguments still exists, but is now optional and hidden by default. I updated the arguments so that now Thank you all for your feedback and helping in improving this post! |
@phil-opp Thank you for all the hard work you've put into this blog. It's an amazing resource. If anyone has issues completing the Linker Arguments portion on a Mac the following .cargo/config file worked for me (not tested on Linux and Windows yet).
The first change I made was separating the link-args into multiple link-arg arguments as it didn't seem to be parsing the spaces correctly. Then I added |
@cstenger Thanks a lot :). Interesting. I updated this section quite recently. I don't have a mac myself, so I was only able to test it on CI, where the command |
Well, guess I'm going crazy. To ensure it wasn't due to any changes I had in my existing project, I started from scratch today. Everything worked as it should have. When calling rustc directly as well as from a config file, no "-lSystem" or multiple link-arg statements were required. Then I went back to the retrofitted project (where I encountered the issues), and it all worked there too. At this moment I don't know why the behavior doesn't match from the previous evening, if I'm able to reproduce it I'll be sure to document the circumstances. Hopefully my experiences haven't lead anyone astray. |
@cstenger Thanks for the update! Let me know if you can reproduce the failure. It's not that there's something wrong with |
@phil-opp I followed each instruction here but I this happened:
How can I fix this? System Info:OS Name Microsoft Windows 10 Home Version 10.0.17134 Build 17134 Other OS Description Not Available OS Manufacturer Microsoft Corporation System Name LAPTOP-FHA479JD System Manufacturer HP System Model OMEN by HP Laptop 15-ce0xx System Type x64-based PC System SKU 1KV79UA#ABA Processor Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz, 2808 Mhz, 4 Core(s), 8 Logical Processor(s) BIOS Version/Date American Megatrends Inc. F.10, 11/1/2017 SMBIOS Version 3.0 Embedded Controller Version 40.24 BIOS Mode UEFI BaseBoard Manufacturer HP BaseBoard Model Not Available BaseBoard Name Base Board Platform Role Mobile Secure Boot State On PCR7 Configuration Binding Possible Windows Directory C:\WINDOWS System Directory C:\WINDOWS\system32 Boot Device \Device\HarddiskVolume1 Locale United States Hardware Abstraction Layer Version = "10.0.17134.765" User Name LAPTOP-FHA479JD\Administrator Time Zone Eastern Daylight Time Installed Physical Memory (RAM) 8.00 GB Total Physical Memory 7.88 GB Available Physical Memory 2.66 GB Total Virtual Memory 12.9 GB Available Virtual Memory 5.42 GB Page File Space 5.00 GB Page File C:\pagefile.sys Kernel DMA Protection Off Virtualization-based security Not enabled Device Encryption Support Reasons for failed automatic device encryption: Hardware Security Test Interface failed and device is not InstantGo, Un-allowed DMA capable bus/device(s) detected Hyper-V - VM Monitor Mode Extensions Yes Hyper-V - Second Level Address Translation Extensions Yes Hyper-V - Virtualization Enabled in Firmware Yes Hyper-V - Data Execution Protection YesEdit(phil-opp): Cleaned up formatting. |
but this happened* |
@LiamTheProgrammer Is there a reason why you're using The problem seems to be that
To fix this, you can either add a nightly override for the |
I am doing translation.And I have some doubts. |
@thebegining Normally there is a |
@phil-opp Thanks for the tutorial. I have been enjoying following it so far. I am stuck at I am receiving the following error at the end of
Cargo now uses a more informative error message. Issue here. I assume that xbuild is still using an older version of cargo. I could not get any further than that. Any insight you have would be great. |
Either way, the old error message contains the same number of information, it just uses a different formulation. The error indicates that your target does not support the Line 9 in fea4fa9
Note that needs to be executables (plural), not executable. Unfortunately, cargo just ignores invalid target configuration keys instead of throwing an error for them. I hope this solves your issue! |
Infinite there is a -Z flag to fix this, rust-lang/rust#59546 Or you can just volatile read a local variable over and over (or any other side effect of of choice). |
Thank you. |
@Lokathor Thanks for your comment! I'm aware of that issue. Everytime I look at it it seems like it is close to being solved, so I always thinks that it's not worth it to mention it on the blog. However, since the issue now persists for multiple years, you're probably right that we should fix it on the blog too. I opened #793 to track this. |
@jerryajay You're welcome! |
What system or systems are responsible for calling the _start entrypoint? |
since this booted to, i presume the linker is configured by default to arrange the binary for the _start to be at the initial address that the bootloader jumps to. |
Thank you! |
Thanks for the work & sponsor the blog already! |
@dy9-spec Thanks a lot for your support! |
Nice! |
I just pass the build without the errors... |
Do you have |
What nightly version is this. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
This is a general purpose comment thread for the “A Freestanding Rust Binary” post.
The text was updated successfully, but these errors were encountered: