cgccx86osdevfreestanding

Subtleties dealing with cross compilation, freestanding libgcc, etc


I have a few questions about https://wiki.osdev.org/Meaty_Skeleton, which says:

The GCC documentation explicitly states that libgcc requires the freestanding environment to supply the memcmp, memcpy, memmove, and memset functions, as well as abort on some platforms. We will satisfy this requirement by creating a special kernel C library (libk) that contains the parts of the user-space libc that are freestanding (doesn't require any kernel features) as opposed to hosted libc features that need to do system calls

Alright, I understand that libgcc is a 'private library' that is used by gcc, i.e. It has significance only during the compilation process, when gcc is in use, kind of like a helper for gcc. Is this right? I understand that the machine I run gcc on is called the build machine, and the host machine is my own OS. What is the freestanding environment specified here? Since gcc runs on the build machine, libgcc must use the build machine libgcc I guess? Where does freestanding come into the picture?

Also, what is the libk? I think that I really don't understand freestanding and hosted environments yet :(


Solution

  • libgcc

    This library must be linked in some cases when you compile with GCC. Because GCC produces calls to functions in this library, if the target architecture doesn't support a specific feature.

    For example, if you use 64-Bit arithmetic in your kernel and you want compile for i386, then GCC make a call to the specific function which resides in libgcc:

    uint64_t div64(uint64_t dividend, uint64_t divisor)
    {
        return (dividend / divisor);
    }
    

    Here you get an undefined reference to __udivdi3 error from the linker when you try to link your kernel without libgcc. On the other side, libgcc also make calls to standard C library functions. So these specific functions must be implemented.

    Here is another IMHO good explanation. It's called bare-metal programming here for the ARM architecture, but also valid for x86.