TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Tearing apart printf() (2018)

152 pointsby dreampeppers99over 5 years ago

7 comments

emmelaichover 5 years ago
Excellent stuff. My couple of notes..<p>1. printf may malloc so don&#x27;t use it in an out of memory situation. Though I think this problem is vanishingly unlikely these days.<p>2. printf of floats used to require linking to the math library on some platforms (-lm)<p>3. I am pathetically grateful for nice diagnostics you get when you use the wrong % formatter in more recent C compilers. This used to be a <i>rich</i> source of errors and non-portability.
评论 #21519472 未加载
评论 #21519499 未加载
评论 #21523090 未加载
评论 #21519419 未加载
评论 #21521671 未加载
评论 #21519797 未加载
评论 #21519955 未加载
matheusmoreiraover 5 years ago
The registers rdi, rsi and rdx are acknowledged in the article but they don&#x27;t appear in the disassembly:<p><pre><code> 000000000040f9c0 &lt;__libc_write&gt;: 40f9c0: 83 3d c5 bb 2a 00 00 cmpl $0x0,0x2abbc5(%rip) # 6bb58c &lt;__libc_multiple_threads&gt; 40f9c7: 75 14 jne 40f9dd &lt;__write_nocancel+0x14&gt; 000000000040f9c9 &lt;__write_nocancel&gt;: 40f9c9: b8 01 00 00 00 mov $0x1,%eax 40f9ce: 0f 05 syscall </code></pre> This is because they are set when the write function is called. The System V AMD64 ABI specifies that rdi, rsi and rdx are used for the 1st, 2nd and 3rd arguments of a function, perfectly matching the Linux system call ABI. The 5th and 6th also match: r8 and r9, respectively. However, the 4th argument doesn&#x27;t match: system calls use r10 while System V uses rcx. I wish I knew why.
评论 #21518513 未加载
评论 #21519307 未加载
porknubbinsover 5 years ago
I wish there was a book length guide with this kind of tutorial style writing but haven’t found much. Everything I’ve found on Linux is more like a reference resource
Annatarover 5 years ago
This is a good overview of how it all works. On some systems like AmigaOS, there is no libc at all, for example standard input &#x2F; output is handled by the dos.library in ROM. compiler-specific implementation is delivered as libc.a, which means that only static linking with libc is possible. UNIX®️ software cannot be compiled at all without modification and without having the 3rd party ixemul.library downloaded from &quot;AmiNet&quot; and installed in the LIBS: assign (which usually resolves to SYS:LIBS, which in turn usually resolves to either DF0:LIBS or DH0:LIBS).
mwfunkover 5 years ago
“Linux&#x2F;GNU” is a new one. Not sure if it’s a typo or a troll, but if the latter, it’s a pretty decent troll if such a thing exists? If it’s a typo, geez dude. You’re going to give RMS an aneurysm.
评论 #21520561 未加载
eqvinoxover 5 years ago
As far as the formatting part is concerned, if you just want to look at an implementation, FreeBSD&#x27;s is extremely nice and readable:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;lattera&#x2F;freebsd&#x2F;blob&#x2F;master&#x2F;lib&#x2F;libc&#x2F;stdio&#x2F;vfprintf.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lattera&#x2F;freebsd&#x2F;blob&#x2F;master&#x2F;lib&#x2F;libc&#x2F;stdi...</a><p>For FRRouting, we decided we want to use Linux kernel style extensions (like &quot;%pI4&quot;), so to get this in a portable way we imported FreeBSD&#x27;s printf into our code. Since printf() isn&#x27;t exactly &quot;hot&quot; code getting changed a lot, we considered the maintenance &#x2F; duplication cost acceptable.<p>You can see the result here:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;FRRouting&#x2F;frr&#x2F;tree&#x2F;master&#x2F;lib&#x2F;printf" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;FRRouting&#x2F;frr&#x2F;tree&#x2F;master&#x2F;lib&#x2F;printf</a><p><a href="http:&#x2F;&#x2F;docs.frrouting.org&#x2F;projects&#x2F;dev-guide&#x2F;en&#x2F;latest&#x2F;logging.html" rel="nofollow">http:&#x2F;&#x2F;docs.frrouting.org&#x2F;projects&#x2F;dev-guide&#x2F;en&#x2F;latest&#x2F;loggi...</a><p>stdio support is completely gone in our copy, WCHAR_SUPPORT is disabled at compile time, locale support is stubbed out&#x2F;hardcoded to C locale. None of these matter to us. As a result, we can use this printf even in the SEGV handler. (That&#x27;s only a bonus though, the main reason was extensibility on the format specifiers.)
dangover 5 years ago
Discussed at the time: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17114919" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17114919</a>