3

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?

user2151887
  • 457
  • 1
  • 6
  • 9
  • 1
    How much of a "custom" kernel is it? – Ryan Mar 22 '13 at 19:40
  • 7
    First you have to port a C compiler, which is hard (if it isn't GCC then that's why, if it is GCC, then that's why). Then you have to make that compiler eat the source code of glibc, which is even harder. –  Mar 22 '13 at 19:41
  • 4
    @H2CO3 +1 for _if it isn't GCC then that's why, if it is GCC, then that's why_ – mah Mar 22 '13 at 19:45
  • 3
    @user21somethingorother... there's _far_ more to porting a C library and/or compiler in order to perform a proper printf. Does your hobby OS have a device IO structure? If printf is your primary motivation, I would suggest you forget about the gcc library and instead look at http://www.efgh.com/software/gprintf.txt (for which you will need to provide a character output function compatible with your OS/hardware). – mah Mar 22 '13 at 19:48
  • @H2CO3 You don't need to port a compiler. I didn't have to port 3 compilers (Borland/Turbo C/C++, Open Watcom C/C++ and gcc/DJGPP) to compile standalone code with them that went into bootloaders and kernels and just worked. You need to figure out how to replace startup code and the standard library and compile and use your own replacements. Then you need to write an image loader for whatever format the compiler outputs. Not a big deal (except for minimal usable standard library, there's quite some coding needed). – Alexey Frunze Mar 22 '13 at 20:02
  • @AlexeyFrunze Actually, I didn't mean "make a C compiler run on your platform". I meant "make a C compiler generate code for your platform". –  Mar 22 '13 at 20:04
  • 1
    @H2CO3 That would be a more appropriate wording/meaning and less intimidating at the same time. :) – Alexey Frunze Mar 22 '13 at 20:04
  • [newlib](http://sourceware.org/newlib/) – Carl Norum Mar 22 '13 at 20:14

4 Answers4

7

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.

iefserge
  • 1,136
  • 1
  • 10
  • 10
  • 1
    +1. I had it up and running in EFI in no time. Pretty much awesome. – Carl Norum Mar 22 '13 at 20:14
  • 1
    Thank you. Apparently you're one of the people who can actually give a simple answer here. Everyone else just argues over how bad I am at programming. – user2151887 Mar 30 '13 at 15:02
5

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.

  • Could you elaborate a bit on why "porting glibc" is painful? I have a firmware equipped with glibc. I am trying to understand the glibc portability issue and hence "debloating" the glibc with respect to my specific embedded device. – lllllllllllll Sep 25 '18 at 10:09
  • Sorry for my late reply. The build system is a monstrosity in itself that few human beings truly understand. You would need to add support for a new kernel which is a massive undertaking. It would take days, and that's only for the build system. –  Nov 12 '18 at 10:08
2

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.

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109
1

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.

vonbrand
  • 11,412
  • 8
  • 32
  • 52