Question

So I have a minimal OS that doesn't do much. There's a bootloader, that loads a basic C kernel in 32-bit protected mode. How do I port in a C library so I can use things like printf? I'm looking to use the GNU C Library. Are there any tutorials anywhere?

Was it helpful?

Solution

Ok, porting in a C library isn't that hard, i'm using Newlib in my kernel. Here is a tutorial to start: http://wiki.osdev.org/Porting_Newlib.

You basically need to:

  • Compile the library (for example Newlib) using your cross compiler
  • Provide stub-implementations for a list of system functions (like fork, fstat, etc.) in your kernel
  • Link the library and your kernel together

If you want to use functions like malloc or printf (which uses malloc internally), you need some kind of memory management and simplest working implementation of sbrk.

OTHER TIPS

I strongly recommend against glibc. It is a beast.

Try newlib instead. Porting it to a new kernel is easy. You just need to write a few support functions, as explained here.

Another new kid on the block is musl which specifically aims to improve the situation in embedded space.

It's probably not the best choice for a beginner, though, since it's still pretty much work in progress.

Better look for a small libc, like uClibc. The GNU C library is huge. And as the comments tell, the first step is to get a C compiler going.

What are you trying to do? Building a full operating system is a job for a group of people lasting a few years... better start with something that already works, and hack on the parts that most interest you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top