6 ms·
Bare metal printf – C standard library without OS
- o11c 2y ago[flagged]
- alxmdev 2y agoWhat's the state of the art for cross-compiling in $CURRENTYEAR?
- forrestthewoods 2y ago> What's the state of the art for cross-compiling in $CURRENTYEAR? Poopy garbage dog poop. glibc is a dumpster fire of bad design. If you want to cross-compile for an arbitrarily old version of glibc then... good luck. It can be done. But it's nightmare fuel.
- zahlman 2y agoIs the cross-compilation story any better with musl?
- forrestthewoods 2y agotbh I haven't gone too deep because glibc is such a standard :( but I can answer with reasonable confidence "musl surely has other problems, but not this one". It's a nice, clean, simple, single set of headers and source files. Very nice.
- sylware 2y agogcc and glibc SDKs are hell beyond anything sane. Like it is done on purpose.
- forrestthewoods 2y agoYou're getting downvoted but you're not wrong. Well, it's not on purpose per se. It's just really really bad design from the 80s when we didn't know better. And unfortunately its design hasn't been fixed. So we have to deal with almost 40 years of accumulated garbage.
- doublepg23 2y agoAren't there some ideologically driven decisions to make proprietary extensions harder/impossible?
- sylware 2y agoThey would be illegal everywhere the GNU GPL can be enforced.
- sylware 2y agoIndeed, that why HN comments are kind of dead (AIs, zealots with one billion accounts behind VPNs, etc). Sometimes I do still try to give an honest, but unpleasant, opinion/fact though. I should stick to raw neutral communication and information publishing.
- achierius 2y agoWhat sort of roadblocks do you run into? Just to get an idea for the flavor of problems.
- forrestthewoods 2y agoWell there's two flavors to this. Building glibc and building a program that links against glibc. They're not entirely the same. And you'd think the latter is easier. But you'd be wrong! It should be trivial to compile glibc with an arbitrary build system for any target Linux platform from any OS. For example if I'm on Windows and I want to build a program that targets glibc 2.23 for Ubuntu on x86_64 target that should be easy peasy. It is not. glibc should have ONE set of .c and .h files for the entire universe. There should be a small number of #define macros that users need to specify to build whatever weird ass flavor they need. These macros should be plainly defined in a single header file that anyone can look at. But glibc is a pile of garbage and has generated files for every damn permutation in the universe. This is not necessary. It's a function of bad design. Code should NEVER EVER EVER have a ./configure step that generates files for the local platform. EVER. Read this blog post to understand the mountains that Zig moved to enable cross-compilation. It's insane. https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html https://andrewkelley.me/post/zig-cc-powerful-drop-in-replace...
- rcxdude 2y ago>glibc should have ONE set of .c and .h files for the entire universe. There should be a small number of #define macros that users need to specify to build whatever weird ass flavor they need. These macros should be plainly defined in a single header file that anyone can look at. Code generation aside, this is not really a great way to do it either. The build should include target-specific files, as opposed to creating a maze of ifdefs inside the code.
- forrestthewoods 2y ago> The build should include target-specific files, as opposed to creating a maze of ifdefs inside the code. Hard disagree. What makes you think it's a maze of ifdefs? Compiling a library should be as simple as "compile every .c/.cpp file and link them into a static/shared lib". The nightmare maze is when you don't do that and you need to painfully figure out which files you should and shouldn't include in this particular build. It's horrible. Far far far simpler is to stick with the rule "always compile all files". It's very simple to ifdef out an entire file with a single, easily understood ifdef at the top. I do agree you don't want the middle of a file to be a fully of 10 different ifdef cases. There's an art to whether to switch within a file or produce different files. No hard and fast rule there. Fundamentally you put either your branching logic in the code or in the build system. And given that source files should be compatible with an unbounded array of potential build systems it is therefore superior to solve the problem once in the code itself. I am currently trying to get the Zig glibc source/headers to compile directly via clang and trying to figure out which files to include or not include is infuriatingly unclear and difficult. So no, I strongly and passionately disagree that build-system is where this logic should occur. It's fucking awful.
- LorenzoGood 2y agoHonesty probably zig cc.
- o11c 2y agoWell, I know it's pretty easy in Debian. (It's not completely pain-free if you need unpackaged third-party libraries and/or if you are cross-compiling from one uncommon architecture to another.)
- genewitch 2y agoI just run gentoo and follow their cross-compile guide, as well as set up distcc; but that's for system packages. TBH I'd use qemu if I had to make something work for arbitrary code. https://wiki.gentoo.org/wiki/Crossdev https://wiki.gentoo.org/wiki/Crossdev But there are others.
- deleted 2y ago[deleted]
- eqvinox 2y agoI was very confused by the title, expected someone writing their own printf — i.e. the part that parses the format string, grabs varargs, converts numbers, lines up strings, etc. I'd have called it "Bare metal puts()" or "Bare metal write()" or something along those lines instead. (FWIW, FreeBSD's printf() is quite easy to pluck out of its surrounding libc infrastructure and adapt/customize.)
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]
- anyfoo 2y agoFreeBSD’s printf is my goto, too! It’s indeed enormously simple to pluck out, instantly gives you full-featured printf, and has added features such as dumping memory as hex.
- eqvinox 2y agoFunnily enough we're not even referring to the same one, the hexdump thing is in FreeBSD's kernel printf, I was looking at the userspace one :). Haven't looked at the kernel one myself but nice to hear it's also well-engineered. (The problem with '%D' hexdumps is that it breaks compiler format checking… and also 'D' is a length modifier for _Decimal64 starting in ISO C23… that's why our hexdump is hooked in as '%.*pHX' instead [which still gives a warning because %p is not supposed to have a precision, but at least it's not entirely broken.])
- einpoklum 2y agoIs it? Could you elaborate/provide links to examples of this? What customization would it support? Say, compared to these options: https://github.com/eyalroz/printf?tab=readme-ov-file#cmake-options-and-preprocessor-definitions https://github.com/eyalroz/printf?tab=readme-ov-file#cmake-o...
- 2y ago
- sylware 2y agoI am coding RISC-V assembly (which I run on x86_64 with a mini-interpreter) but I am careful to avoid the usage of the pseudo-instructions and the registers aliases (no compressed instruction ofc). I have a little tool to generate constant loading code, one-liner (semi-colon separated instructions). And as a pre-processor I use a simple C preprocessor (I don't want to tie the code to the pre-processor of a specific assembler): I did that for x86_64 assembly, and I could assemble with gas, nasm and fasmng(fasm2) transparently.
- 0x000xca0xfe 2y agoWhat's wrong with compressed instructions?
- sylware 2y agoI don't feel comfy using duplicate instructions for a 'R'educed instruction set. That said, I know in some cases it could increase performance since the code would use less memory (and certainly more things which I don't know because I am not into modern advanced hardware CPU micro-architecture design).
- 0x000xca0xfe 2y agoIt's just an alternative encoding for exactly the same instructions, it does not make the ISA more complex. If you are writing assembly you probably are using compressed instructions already since your assembler can do the substitions transparently, e.g. addi a0,a0,10 -> c.addi a0,10 Example: https://godbolt.org/z/MG3v3jx7P https://godbolt.org/z/MG3v3jx7P (the disassembly shows addi but the instruction is only two bytes). They offer a nice reduction in code size with basically no downsides :)
- sylware 2y agoI explicitely do disable register ABI alias names, pseudo-instructions and transparent "optimizations", because I run the RISC-V binary in my own x86_64 assembly written little RISC-V machine code interpreter which does support only the core instructions (and a linux syscall translation layer). I may start to add the compressed instructions someday though.
- ChuckMcM 2y agoI was feeling a bit like the Petunia and thought "Oh no, not again." :-) One of the annoyances of embedded programming can be having the wheel re-invented a zillion times. I was pleased to see that the author was just describing good software architecture that creates portable code on top of an environment specific library. For doing 'bare metal' embedded work in C you need the crt0 which is the weirdly named C startup code that satisfies the assumption the C compiler made when it compiled your code. And a set of primitives to do what the i/o drivers of an operating system would have been doing for you. And voila, your C program runs on 'bare metal.' Another good topic associated with this is setting up hooks to make STDIN and STDOUT work for your particular setup, so that when you type printf() it just automagically works. This will also then introduce you to the concept of a basic input/output system or BIOS which exports those primitives. Then you can take that code in flash/eprom and load a binary compilation into memory and start it and now you've got a monitor or a primitive one application at a time OS like CP/M or DOS. Its a fun road for students who really want to understand computer systems to go down.
- marssaxman 2y agoThis was my attempt at a minimal bare-metal C environment: https://github.com/marssaxman/startc https://github.com/marssaxman/startc
- ChuckMcM 2y agoThat's awesome. Back in the day this was the strong point of eCOS which was a bare metal "platform" for running essentially one application on x86 hardware. The x86 ecosystem has gotten so complicated that being able to do this can get you better performance for an "embedded" app than running on top of Linux or another embedded OS. That translates into your appliance type device using lower cost chips which is a win. When I was playing around with eCos a lot of the digital signage market was using it.
- guestbest 2y agoDoes anyone still do it that way?
- smackeyacky 2y agoHas anybody played with newlib, but grown the complexity as the system came together? It seems like one thing to get a bare-bones printf() working to get you started on a bit of hardware, but as the complexity of the system grows you might want to move on from (say) pushing characters out of a serial interface onto pushing them onto a bitmapped display. Does newlib allow you to put different hooks in there as the complexity of the system increases?
- Gibbon1 2y agoYou can always write a printf replacement that takes a minimal control block that provides put, get, control, and a context. That way you can print to a serial port, an LCD Display, or a log. Meaning seriously the standard printf is late 1970's hot garbage and no one should use it.
- adrian_b 2y agoNewlib provides both a standard printf, which is necessarily big, and a printf that does not support any of the floating-point format specifiers. The latter is small enough so that I have used it in the past with various small microcontrollers, from ancient types based on PowerPC or ARM7TDMI to more recent MCUs with Cortex-M0+. You just need to make the right configuration choice.
- MuffinFlavored 2y agoI always felt with these kinds of things you strip out `stdio.h` and your new API/ABI/blackbox becomes `syscall` for `write()`, etc.
- Neywiny 2y agoIn school we were taught that the OS does the printf. I think the professors were just trying to generalize to not go on tangents. But, once I learned that no embedded libc variants had printf just no output path, it got a lot easier to figure out how to get it working. I wish I knew about SWO and the magic of semihosting back then. I don't think those would be hard to explain and interestingly it's one of the few things students asked about that in the field I'm also asked how to do by coworkers (the setting up _write).
- wrasee 2y ago> But, once I learned that no embedded libc variants had printf just no output path Did you mean "once I learned that no, embedded libc variants have printf"? To clarify as I had to check, embedded libc variants do indeed have some (possibly stripped-down) implementation of printf and as you say they just lack the output path (hence custom output backends like UART, etc).
- Neywiny 2y agoSorry yes I had a comma there and then deleted it. I don't remember why but I remember doing it.
- saagarjha 2y agochar buffer[100]; printf("Type something: "); scanf("%s", buffer); Come on, it’s 2025, there’s no need to write trivial buffer overflows anymore.
- Rochus 2y agoNewlib is huge and complex (even including old K&R syntax) and adapting the build process to a new system is not trivial. I spent a lot of time with it when I re-targeted chibicc and cparser to EiGen, and finally switched to PDCLib for libc and a part of uClibc for libm; see https://github.com/rochus-keller/EiGen/tree/master/ecc/lib https://github.com/rochus-keller/EiGen/tree/master/ecc/lib. The result is platform independent besides esentially one file.
- adrian_b 2y agoFor a static library it does not matter whether it is huge and complex, because you will typically link into your embedded application only a small number of functions from it. I have used a part of newlib with many different kinds of microcontrollers and its build process has always been essentially the same as a quarter of century ago, so that the script that I have written the first time, before 2000, has always worked without problems, regardless of the target CPU. The only tricky part that I had to figure the first time was how to split the compilation of the gcc cross-compiler into a part that is built before newlib and a part that is built after newlib. However that is not specific to newlib, but is the method that must be used when compiling a cross-gcc with any standard C library and it has been simplified over the years, so that now there is little more to it than choosing the appropriate make targets when executing the make commands. I have never needed to change the build process of newlib for a new system, I had just needed to replace a few functions, for things like I/O peripherals or memory allocation. However, I have never used much of newlib, mostly only stdio and memory/string functions.
- Rochus 2y ago> it does not matter whether it is huge and complex I was talking about the migration effort and usage complexity, not what the compiler or linker actually sees. It may well be that Newlib can be configured for every conceivable application, but it was more important to me not to have a such a behemoth and bag full of surprises in the project with preprocessor rules and dependencies that a single developer can hardly understand or keep track of. My solution is lean, complete, and works with standard-conforming compilers on each platform I need it.
- rurban 2y ago220k just to include studio? That's insane. I have 12k and still do IO. Just without the overblown stdio and sbrk, uart_puts is enough. And only in DEBUG mode.
- tails4e 2y agoI thought this was going to talk about how printf is implemented. I worked with a tiny embedded processor that had 8k imem, and printf is about 100k alone. Crazy. Switched to a more basic implementation that was around 2k, and ran much,much faster. It seems printf is pretty bloated, though I guess typical people don't care.
- adrian_b 2y agoIn most C standard libraries intended for embedded applications, including newlib, there is some configuration option to provide a printf that does not support any of the floating-point format specifiers. That is normally enough to reduce the footprint of printf by more than an order of magnitude, making it compatible with small microcontrollers.
- rurban 2y agoI implemented a secure printf_s and its API is the problem. You cannot dead-code eliminate all the unused methods. And it's type unsafe. There are much better API's to implement a safe printer with all the formatting options still. format is not one of them
- bobmcnamara 2y agoThe only hack I could think of is having the compiler front end generate calls to different functions based on the content of the format string, similar to how some compilers replace memset with a 32-bit memset based on the type of the destination pointer And it all falls apart as soon as a format string cannot be known at compile time.
- Someone 2y ago
- gitroom 2y agohonestly love reading about this stuff - always makes me realize how much gets glossed over in school. you think modern cpus and all the abstraction layers help or just make things messier for folks trying to learn the real basics?
- einpoklum 2y agoWhile "newlib" is an interesting idea - the approach taken here is, in many cases, the wrong one. You see, actually, the printf() family of functions don't actually require _any_ metal, bare or otherwise, beyond the ability to print individual characters. For this reason, a popular approach for the case of not having a full-fledged standard library is to have a fully cross-platform implementation of the family which "exposes" a symbol dependency on a character printing function, e.g.: void putchar_(char c); and variants of the printf functions which take the character-printing function as a runtime parameter: int fctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, ...); int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, va_list arg); this is the approach taken in the standalone printf implementation I maintain, originally by Marco Paland: https://github.com/eyalroz/printf https://github.com/eyalroz/printf
- eqvinox 2y agoAs replied on your other comment, when you introduce a custom printf for an embedded platform it makes more sense to just edit in support for your local I/O backend rather than having the complexity of a putch() callback function pointer. cf. https://news.ycombinator.com/item?id=43811191 https://news.ycombinator.com/item?id=43811191 for other notes.
- dailykoder 2y ago// QEMU UART registers - these addresses are for QEMU's 16550A UART #define UART_BASE 0x10000000 #define UART_THR (*(volatile char *)(UART_BASE + 0x00)) // Transmit Holding Register #define UART_RBR (*(volatile char *)(UART_BASE + 0x00)) // Receive Buffer Register #define UART_LSR (*(volatile char *)(UART_BASE + 0x05)) // Line Status Register This looks odd. Why are receive and transmit buffer the same and why would you use such a weird offset? Iirc RISC-V allows that, but my gut says I'd still align this to the word size.
- eqvinox 2y agoMy sweet summer child… this is backwards compatibility to the I/O register set of NatSemi/Intel's 8250 UART chip… …from 1978. https://en.m.wikipedia.org/wiki/8250_UART https://en.m.wikipedia.org/wiki/8250_UART The definitions are correct, look up an 16550 datasheet if you want to lose some sanity :)
- dailykoder 2y agoOh damn, thanks!
- deleted 2y ago[deleted]
- bobmcnamara 2y ago> Why are receive and transmit buffer the same? Backwards compatibility aside, why bother implementing additional register address decoding? Since the host already doesn't need to read THR or write RBR they can be safely combined. Some UARTs call this a DATA register instead.
- p0w3n3d 2y agoBare metal printf is usually faster but (surprise surprise) platform dependent. I remember I was trying to program Atari 8-bit using C compiler, and writing directly characters to Antic memory range WITH charcode translation was 100x faster than using printf. However I'm not sharing this code because it won't work on UART... laughs nervously
- up2isomorphism 2y agoIf you already are doing bare metal, you probably should think twice if you need a printf, may you just need inb and outb.
- adiabatichottub 2y agoI've used avrlibc's printf quite a lot. I always thought it was silly that Arduino never did the simple task of plumbing up to the UART