【Hacker News搬运】Hello World
-
Title: Hello World
Hello World
Text:
Url: https://thecoder08.github.io/hello-world.html
这篇文章详细分析了用 C 语言编写的 Hello World 程序,考察了高级语言与运行在处理器上的机器代码之间的抽象。文章解释了编译过程,生成的 ELF 可执行文件,以及系统的标准 C 库。它还讨论了动态链接器的作用,栈帧的功能,以及为什么使用 puts() 函数而不是 printf() 函数来简化问题。作者反汇编程序以显示机器代码及其回到原始 C 代码的翻译,强调了 C 字符串中的 NULL 终止符的重要性以及标准库在处理输出方面的作用。 文本描述了从用 C 编写的代码到向内核发起系统调用的整个 "Hello World" 程序工作过程。它解释了使用的方法和库,例如 musl libc 和 Glibc,以及它们如何通过系统调用来与硬件交互。作者得出结论,现代软件系统复杂而精致,使得很难完全理解程序执行时发生的所有事情。
Post by: fbrusch
Comments:
MuffinFlavored: I got bored the other day and tried to achieve something similar on MacOS with Rust:<p><pre><code> #![no_std]
#![no_main]use core::panic::PanicInfo; #[panic_handler] fn panic_handler(_panic: &PanicInfo<'_>) -> ! { // TODO: write panic message to stderr write(2, "Panic occured\n".as_bytes()); // TODO: panic location + message unsafe { sc::syscall!(EXIT, 255 as u32) }; loop {} } fn write(fd: usize, buf: &[u8]) { unsafe { sc::syscall!(WRITE, fd, buf.as_ptr(), buf.len()); } } #[no_mangle] pub extern "C" fn main() -> u32 { write(1, "Hello, world!\n".as_bytes()); return 0; }
</code></pre>
Then I inspected the ELF output in Ghidra. No matter what it was about ~16kb. I'm sure some code golf could be done to get it done (which has obviously been done + written about + documented before)MuffinFlavored: 前几天我很无聊,试图用Rust在MacOS上实现类似的功能:<p><pre><code>#![no_std]#![no_main]使用core::panic::PanicInfo;#[panic_handler]fn panic_handler(panic:&;PanicInfo<;;>;)->!{//;TODO:将紧急消息写入stderrwrite(2,“发生了恐慌”.as_bytes())//;TODO:紧急定位+消息不安全{sc::syscall!(EXIT,255作为u32)};环}fn写入(fd:usize,buf:&;[u8]){不安全的sc::syscall!(写入,fd,buf.as_ptr(),buf.len());}}#[no_mangle]pub-extern”;C”;fn main()->;u32{write(1,“你好,世界!”.as_bytes());返回0;}</code></pre>然后我检查了Ghidra的ELF输出。不管它大约是16kb。I-;我确信可以做一些代码golf来完成它(这显然已经完成+写过+以前记录过)
bkallus: This almost entirely skips the role of the dynamic linker, which is arguably the true entry point of the program.<p>If you are interested in that argument, see <a href="https://gist.github.com/kenballus/c7eff5db56aa8e4810d39021b23d8a8f" rel="nofollow">https://gist.github.com/kenballus/c7eff5db56aa8e4810d39021b2...</a>.
bkallus: 这几乎完全跳过了动态链接器的角色,动态链接器可以说是程序的真正入口点<p> 如果您对该论点感兴趣,请参阅<a href=“https://;/;gist.github.com/!kenballus/:c7eff5db56aa8e4810d39021b23d8a8f”rel=“nofollow”>https:///;gist.github.com/;肯巴勒斯;c7有效5db56a8e4810d39021b2…</a>。
cancerhacker: I liked and appreciated this, two points I'd like to make: you should disable optimizations and whatever inlining caused printf to become puts (or, alternatively, write the hello world to use puts directly), second would be to break your compile step into the 4 real parts: preprocess, compile, assemble, link. Or add --save-temps to the cc line and describe the various files created. There's a lot less magic involved if you can see the pipeline.
cancerhacker: 我喜欢并欣赏这一点;我想说的是:你应该禁用优化和任何导致printf变成put的内联(或者,直接编写hello世界来使用put),第二个是将编译步骤分解为4个实际部分:预处理、编译、组装和链接。或者将--save temps添加到cc行,并描述所创建的各种文件。有;如果你能看到管道的话,这就少了很多魔法。
Syntaf: This reminds me of one of my favorite CS assignments in college for a systems programming class:<p><pre><code> > Given a hello world C++ snippet, submit the smallest possible compiled binary
</code></pre>
I remember using tools like readelf and objdump to inspect the program and slowly rip away layers and compiler optimizations until I ended up with the smallest possible binary that still outputted "hello world". I googled around and of course found someone who did it likely much better than any of us students could have ever managed [1]<p>[1]: <a href="https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html" rel="nofollow">https://www.muppetlabs.com/~breadbox/software/tiny/teensy....</a>Syntaf: 这让我想起了我在大学里最喜欢的系统编程课CS作业之一:<p><pre><code>>;给定一个helloworld C++代码片段,提交尽可能小的已编译二进制文件</code></pre>我记得我使用readelf和objdump等工具来检查程序,并慢慢地剥离层和编译器优化,直到我最终得到了仍然输出“”的尽可能小的二进制文件;你好世界”;。我在谷歌上搜索了一下,当然发现有人做得可能比我们任何学生都要好[1]<p>[1]:<a href=“https://;/;www.muppetlabs.com/!%7Ebreadbox/:软件/“微小/”teensy.html”rel=“nofollow”>https:///;www.muppetlabs.com/;%7Ebreadbox;软件;微小的;青少年</一
norir: > I’m sorry the ending maybe wasn’t as satisfying as you hoped. I’m happy someone found this interesting. I’m not quite sure why I wrote this, but it’s now after midnight so I should get some sleep.<p>This was actually a perfect ending to this piece.
norir: >;很抱歉,结局可能没有你希望的那么令人满意。我很高兴有人发现这很有趣。我不太清楚我为什么写这篇文章,但现在已经过了午夜,所以我应该睡一觉<p> 这实际上是这部作品的完美结局。