C Library: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
m Fix typo
more targets have been added since last update
 
(22 intermediate revisions by 15 users not shown)
Line 13: Line 13:
There are several open-source C library packages available, and using one may be a viable solution for you. All of them will require some degree of modification to suit your needs.
There are several open-source C library packages available, and using one may be a viable solution for you. All of them will require some degree of modification to suit your needs.


There is a comparison table of some of these at: [http://www.etalabs.net/compare_libcs.html]
There is a [http://www.etalabs.net/compare_libcs.html comparison table] of some of these.


=== MyOS libc ===
=== MyOS libc ===
{{Main|Creating a C Library}}
{{Main|Creating a C Library}}


One of the more obvious solutions is to roll your own. You can get seamless integration with your kernel and the rest of the operating system, without portability layers. You also get the ability to progress with function implementations in step with the hardware functionality you add to your kernel. You can effectively write the standard and append custom interfaces to be available to all apps. It is also the one and only option if you happen to be one of the purists out there. It also serves as quite the learning experience.
The best option, in terms of code quality and integration with your operating system, is to write your own C library. You can aim for making a clean and high-quality implementation that follows standards well. It will integrate cleanly with your kernel as no portability layer is needed. You can be secure and robust. You can surpass the limits of what you can do with existing implementations. You can add vendor-extensions that replace bad interfaces and do things better. You can break a lot of code because you followed the standards and applications didn't, then fix the applications as well. It can be better than the competition if you make that your goal.


On the flipside, the C library requires a significant amount of time, and using an existing version allows you to spend more effort on your own kernel. In addition, there are a great many caveats hidden in the C language specification, and it is exceedingly easy to write a non-compliant implementation that will later on cause unexpected issues when porting other third part software - or it so happens that your version is actually compliant and whatever you try to port has a hard dependency on glibc quirks.
This is the idealist path and most in the osdev spirit. It has a lot of advantages, but has the steep price of requiring a lot of effort. This may very well be worth it if your goal is to make a ''good'' operating system, not just ''a'' operating system. You don't have to be a libc expert when starting out, but you'll be one when you're done. You might just realize much of the competition out there isn't very good.


Here are some existing C libraries for you to pursue:
Creating your own minimal C library is relatively easy. Providing all the primitives needed by real program is a much larger task, but straightforward as you can attempt to cross-compile the software and implement all the missing features causing compilation errors.


=== [https://www.gnu.org/software/libc/ Glibc] ===
=== Newlib ===
* The license is unrestricted (not GPL or LGPL), but each file likely has a different copyright notice.
* Requires threading, so is more appropriate for a runtime library
* About 400 functions supported
* [http://sourceware.org/newlib/ newlib website]

=== GlibC ===
* GPL license
* GPL license
* Should be absolutely complete (even has all the bloat)
* Should be absolutely complete (even has all the bloat)
* Supports almost every architecture
* [http://www.gnu.org/software/libc/ glibc website]
* Generates large programs
* Is not written with anything other than Linux in mind, making it a hard port.


=== Musl ===
=== [http://musl.libc.org/ Musl] ===
* MIT or BSD licenses
* MIT license
* No kernel portability layer, uses the Linux system calls directly.
* No kernel portability layer, uses the Linux system calls directly. You can add your own layer between musl and the kernel to translate Linux system calls into native system calls, which is the method used by [http://midipix.org midipix].
* A full set of math and printf functions
* A full set of math and printf functions
* Support for about 1200 functions
* Support for about 1200 functions
* Many system calls needs to be implemented as it assumes you are a full Linux
* Many system calls needs to be implemented as it assumes you are a full Linux
* [http://www.etalabs.net/musl/ musl website]


=== [https://sourceware.org/newlib/ Newlib] ===
=== PDCLib ===
* The license is unrestricted (not GPL or LGPL), but each file likely has a different copyright notice.
* Requires threading, so is more appropriate for a runtime library
* About 400 functions supported

=== [https://rootdirectory.de/doku.php?id=pdclib:start PDCLib] ===
* Creative Commons Zero license (public domain)
* Creative Commons Zero license (public domain)
* Under active development, and not at full working release 1.0 yet
* Under active development, and not at full working release 1.0 yet
Line 50: Line 50:
* 10 (plus one optional) required syscalls need to be implemented
* 10 (plus one optional) required syscalls need to be implemented
* No ASM -- should be fully portable
* No ASM -- should be fully portable
* [http://pdclib.e43.eu/ PDCLib website]


=== uClibc ===
=== [https://www.uclibc.org/ uClibc] ===
* LGPL license
* LGPL license
* [http://www.uclibc.org/ uclibc website]


=== [https://www.fefe.de/dietlibc/ Diet Libc] ===
=== diet libc ===
* GPL license
* GPL 2 license
* Optimized for small size
* [http://www.fefe.de/dietlibc/ dietlibc website]
* Many features missing


=== Google's Bionic ===
=== [https://android.googlesource.com/platform/bionic/ Google's Bionic] ===
* BSD license
* BSD license
* No support for locales
* No support for locales
Line 66: Line 65:
* Its own smallish implementation of pthreads based on Linux futexes
* Its own smallish implementation of pthreads based on Linux futexes
* Support for x86, ARM and ARM thumb CPU instruction sets and kernel interfaces
* Support for x86, ARM and ARM thumb CPU instruction sets and kernel interfaces

* [https://github.com/android/platform_bionic/tree/master/libc bionic website]
=== [https://sortix.org/source/sortix/tree/master/libc Sortix Libc] ===
* ISC license.
* Implements large parts of the C and POSIX standards.
* Subset can be built as the kernel standard library libk.
* Supports over [https://gitlab.com/sortix/sortix/wikis/Ports 70 pieces of third party software].
* The source code is well organized and fairly straightforward.
* Static linking only at this time.
* Part of [https://sortix.org Sortix] and assumes the Sortix system call ABI, which makes it cleaner.
* Some modification is required to support other system call ABIs depending on how similar the ABI is.

=== [http://libc11.org/ Libc11] ===
* Public domain
* Written for the new C11 standards.
* Under development since November 2014. Currently still severely lacking in functionality.

=== [https://github.com/managarm/mlibc mlibc] ===
* MIT license
* Supports C11, POSIX and various linux and glibc extensions
* Highly modular (can turn off extensions if not wanted)
* Extremely easy to port to your OS
* Supports a large variety of ports (bash, gcc, wayland, xorg and many more)
* Supports dynamic and static linking

=== [http://pdos.org PDPCLIB] ===
* Public domain
* Under active development
* C90 only, no extensions whatsoever
* 16-bit, 32-bit and 64-bit
* Supports Win32 (x86 and ARM), Win64 (x64) MSDOS, AmigaOS, OS/2, Linux (x86, x64, ARM32, ARM64), MacOS, MVS, CMS, VSE, PDOS/386, z/PDOS, PDOS-generic, UEFI
* Other platforms can probably be added easily, including new platforms
* Not to be confused with PDCLIB (predates and inspired PDCLIB)


== Standards ==
== Standards ==
Line 72: Line 102:
Especially if you want to roll your own C lib, you may want to buy the ISO/IEC 9899 specification to work from.
Especially if you want to roll your own C lib, you may want to buy the ISO/IEC 9899 specification to work from.
It is not free. Expect a PDF to cost somewhere around $250 (US) or 250 Swiss Francs, depending on currency conversions.
It is not free. Expect a PDF to cost somewhere around $250 (US) or 250 Swiss Francs, depending on currency conversions.

On the other hand, the INCITS republishes these standards for a lot less: INCITS/ISO/IEC 9899-2011 can be purchased for about $60 (US) from the ANSI web store or from TechStreet.


The older standards (C89/C90, C99) are not commercially available anymore. To find the current standard, go to one of the
The older standards (C89/C90, C99) are not commercially available anymore. To find the current standard, go to one of the
following sites and search for document "ISO/IEC 9899".
following sites and search for document "ISO/IEC 9899".


By the way, you can use latest draft of standard, it's publicly available for free and exactly the same as approved version. This is a path chosen by most open-source software, especially GCC and GLIBC relies on drafts published by ISO/IEC. Latest version of C2011 draft has the name ISO/IEC 9899 N1570 and can be downloaded [http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf here (PDF)]
Specification Stores:

=== Specification Stores ===
* [http://webstore.ansi.org From ANSI]
* [http://webstore.ansi.org From ANSI]
* [http://infostore.saiglobal.com/store From SAI Global]
* [http://infostore.saiglobal.com/store From SAI Global]
* [http://www.iso.org/iso/home/store From ISO Store]
* [http://www.iso.org/iso/home/store From ISO Store]

=== Specifications ===
* [http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+9899-2012 INCITS/ISO/IEC 9899-2011 at ANSI's eStandards Store]
* [http://www.techstreet.com/products/1852924 INCITS/ISO/IEC 9899-2011 at Thomson Reuters TechStreet]

== Literature ==
* [https://www.amazon.com/dp/0131315099/ The Standard C Library by P.J. Plauger]

[[Category:C]]
[[Category:Standard Libraries]]

Latest revision as of 09:22, 10 May 2024

The C standard library provides string manipulation (string.h), basic I/O (stdio.h), memory allocation (stdlib.h), and other basic functionality to C programs. The interface is described in the C standard, with further additions described in POSIX as well as vendor extensions. On Unix platforms, the library is named libc and is linked automatically into every executable.

You need a C standard library implementation with the necessary features to run C programs on your operating system. C++ programs can usually use the C standard library as well and the C++ implementation is normally built on top of libc. It is possible to use the C standard interface in a kernel if the library implementation supports this.

Freestanding and Hosted

There are two flavors of the C compilation environment: Hosted, where the standard library is available; and freestanding, where only a few headers are usable that contains only defines and types. The hosted environment is meant for user-space programming while freestanding is meant for kernel programming. The hosted environment is default, but you can switch to the freestanding by passing -ffreestanding to your compiler.

The __STDC_HOSTED__ macro expands to 1 on hosted implementations, or 0 on freestanding ones. The freestanding headers are: <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>. You should be familiar with these headers as they contain useful declarations you shouldn't do yourself. GCC also comes with additional freestanding headers for CPUID, SSE and such.

Implementations

There are several open-source C library packages available, and using one may be a viable solution for you. All of them will require some degree of modification to suit your needs.

There is a comparison table of some of these.

MyOS libc

Main article: Creating a C Library

One of the more obvious solutions is to roll your own. You can get seamless integration with your kernel and the rest of the operating system, without portability layers. You also get the ability to progress with function implementations in step with the hardware functionality you add to your kernel. You can effectively write the standard and append custom interfaces to be available to all apps. It is also the one and only option if you happen to be one of the purists out there. It also serves as quite the learning experience.

On the flipside, the C library requires a significant amount of time, and using an existing version allows you to spend more effort on your own kernel. In addition, there are a great many caveats hidden in the C language specification, and it is exceedingly easy to write a non-compliant implementation that will later on cause unexpected issues when porting other third part software - or it so happens that your version is actually compliant and whatever you try to port has a hard dependency on glibc quirks.

Here are some existing C libraries for you to pursue:

Glibc

  • GPL license
  • Should be absolutely complete (even has all the bloat)
  • Supports almost every architecture
  • Generates large programs
  • Is not written with anything other than Linux in mind, making it a hard port.

Musl

  • MIT license
  • No kernel portability layer, uses the Linux system calls directly. You can add your own layer between musl and the kernel to translate Linux system calls into native system calls, which is the method used by midipix.
  • A full set of math and printf functions
  • Support for about 1200 functions
  • Many system calls needs to be implemented as it assumes you are a full Linux

Newlib

  • The license is unrestricted (not GPL or LGPL), but each file likely has a different copyright notice.
  • Requires threading, so is more appropriate for a runtime library
  • About 400 functions supported

PDCLib

  • Creative Commons Zero license (public domain)
  • Under active development, and not at full working release 1.0 yet
  • Good for linking into kernels
  • Support for about 120 functions, currently
  • 10 (plus one optional) required syscalls need to be implemented
  • No ASM -- should be fully portable

uClibc

  • LGPL license

Diet Libc

  • GPL 2 license
  • Optimized for small size
  • Many features missing

Google's Bionic

  • BSD license
  • No support for locales
  • No libthread_db or libm implementation
  • Its own smallish implementation of pthreads based on Linux futexes
  • Support for x86, ARM and ARM thumb CPU instruction sets and kernel interfaces

Sortix Libc

  • ISC license.
  • Implements large parts of the C and POSIX standards.
  • Subset can be built as the kernel standard library libk.
  • Supports over 70 pieces of third party software.
  • The source code is well organized and fairly straightforward.
  • Static linking only at this time.
  • Part of Sortix and assumes the Sortix system call ABI, which makes it cleaner.
  • Some modification is required to support other system call ABIs depending on how similar the ABI is.

Libc11

  • Public domain
  • Written for the new C11 standards.
  • Under development since November 2014. Currently still severely lacking in functionality.

mlibc

  • MIT license
  • Supports C11, POSIX and various linux and glibc extensions
  • Highly modular (can turn off extensions if not wanted)
  • Extremely easy to port to your OS
  • Supports a large variety of ports (bash, gcc, wayland, xorg and many more)
  • Supports dynamic and static linking

PDPCLIB

  • Public domain
  • Under active development
  • C90 only, no extensions whatsoever
  • 16-bit, 32-bit and 64-bit
  • Supports Win32 (x86 and ARM), Win64 (x64) MSDOS, AmigaOS, OS/2, Linux (x86, x64, ARM32, ARM64), MacOS, MVS, CMS, VSE, PDOS/386, z/PDOS, PDOS-generic, UEFI
  • Other platforms can probably be added easily, including new platforms
  • Not to be confused with PDCLIB (predates and inspired PDCLIB)

Standards

Especially if you want to roll your own C lib, you may want to buy the ISO/IEC 9899 specification to work from. It is not free. Expect a PDF to cost somewhere around $250 (US) or 250 Swiss Francs, depending on currency conversions.

On the other hand, the INCITS republishes these standards for a lot less: INCITS/ISO/IEC 9899-2011 can be purchased for about $60 (US) from the ANSI web store or from TechStreet.

The older standards (C89/C90, C99) are not commercially available anymore. To find the current standard, go to one of the following sites and search for document "ISO/IEC 9899".

By the way, you can use latest draft of standard, it's publicly available for free and exactly the same as approved version. This is a path chosen by most open-source software, especially GCC and GLIBC relies on drafts published by ISO/IEC. Latest version of C2011 draft has the name ISO/IEC 9899 N1570 and can be downloaded here (PDF)

Specification Stores

Specifications

Literature