Page 1 of 5

article on C compiler and standard lib C related to osdev

Posted: Thu Sep 12, 2013 12:18 pm
by h0bby1
i started a little wiki page about some of the issue one can encounter with C compiler and standard lib C when programming an OS, using an alternate way from building a cross compiler, but more to use specific implementation of the standard C library and header that can be used to write compiler independent code

http://wiki.osdev.org/User:H0bby1

i just started the page today, i'm trying to figure out how the wiki works, there are probably some mistake, i'll correct them latter, and i will complete it as well to write code that is compiler independent, eventually how to make C functions using specific assembler like mmx or optimization consideration, but more generally how to design a C library and application that use it in a way that doesn't depend on compiler specifics

not sure if it's a good idea, or how far i want to go with it, but i think it might be helpful to some people who don't want to follow the cross compiler way

Re: article on C compiler and standard lib C related to osde

Posted: Thu Sep 12, 2013 5:42 pm
by sortie
Hi, this looks like something I'd like to have a closer look at tomorrow when I've had some sleep.

Could you *please* use proper capitalization? I can tolerate you don't do it in your forum posts (which makes me not want to read your posts), but a wiki article must absolutely use all the proper grammar and formatting rules.

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 12:33 am
by sortie
Additionally, please consider not saving your WIP article everytime you make a minor draft change. It clutters the wiki activity log considerably - I like to read it to help out moderating the wiki. Instead, just save a copy of your draft as a text file on your desktop and then paste the current version into the "Create this page" (or "Edit this page") textbox and hit preview. This way your WIP article doesn't get published/updated at undesirable times, but your document is still preserved in case of a browser crash or similar. :-)

(Just woke up, I'll have a look at your work a little later)

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 4:32 am
by h0bby1
sortie wrote:Additionally, please consider not saving your WIP article everytime you make a minor draft change. It clutters the wiki activity log considerably - I like to read it to help out moderating the wiki. Instead, just save a copy of your draft as a text file on your desktop and then paste the current version into the "Create this page" (or "Edit this page") textbox and hit preview. This way your WIP article doesn't get published/updated at undesirable times, but your document is still preserved in case of a browser crash or similar. :-)

(Just woke up, I'll have a look at your work a little later)
yes but i needed to also see how it looks in the wiki, and to get familiar with the tags, and how wiki page is organised, but i'll try to avoid that :)

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 4:32 am
by h0bby1
sortie wrote:Hi, this looks like something I'd like to have a closer look at tomorrow when I've had some sleep.

Could you *please* use proper capitalization? I can tolerate you don't do it in your forum posts (which makes me not want to read your posts), but a wiki article must absolutely use all the proper grammar and formatting rules.
ok :)

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 5:19 am
by dozniak
h0bby1 wrote:
sortie wrote:Additionally, please consider not saving your WIP article everytime you make a minor draft change....
yes but i needed to also see how it looks in the wiki, and to get familiar with the tags, and how wiki page is organised, but i'll try to avoid that :)
Use "[x] This is a minor edit"

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 5:24 am
by Combuster
And the preview button :wink:

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 5:27 am
by h0bby1
ok if i make preview or check the minor modification box, it won't flood the log ?

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 5:31 am
by Combuster
Preview doesn't log because it doesn't save anything, minor edits are just a hint to other readers that there are no changes in the actual content - they will still show up.

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 5:33 am
by sortie
The minor modification box will still make an entry at RecentChanges - it's a polite way of saying "This edit is small and doesn't really change anything". The goal is to make it easy for wiki users to skip unimportant changes when reviewing the log.

The preview button lets you view the page without saving it, which means there won't be any log entries. The goal is to keep draft versions private and not clutter the log with temporay entries, when all the reviewing users care about is the article going from non-existent to finished.

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 11:05 am
by sortie
Hi, I finally had a chance to read through your draft article. I realize it's not finished, but I figured I'd comment anyway. I don't have that much time right now, but I'll try to get some points across and continue later.

The C standard doesn't mandate any calling conventions. It's all entirely implementation defined. C could in theory be interpreted rather than translated. When you hear of the 'C calling convention', it refers to some default standard calling convention that everybody happened to agree upon (at least in the i386) world - it's called this because Windows has a heap of other calling conventions and the 'C calling convention' happens to the default and it's rather simple.

Note that libgcc is part of GCC, it doesn't have anything to do with GNU/Linux. There is a libgcc on Windows as well.

You appear to be confused about the relationship between a compiler and the libc. You appear to have a Windows background, where the official compiler suite is Visual Studio where they ship a libc and compiler together, they are tightly coupled there. You suggest furthermore that programs should ship with their own copy of the libc as a shared library. This is really, really stupid. It's how Windows work, sure, but the C library is meant to be just another core system library. There is a completely staggering excess of code duplication in Windows - this is largely attributed to that programs can't assume anything is installed and sharing shared libraries is out of the question because few people care about binary compatibility. My point is that the compiler and libc are separate projects and there is no such thing as a "compiler libc" universally (although, some systems do use this botched scheme).

You don't appear to understand cross-compilation and why it's so vitally important for osdev work. Note that the 'build' platform is the one where the compiler runs, and the 'host' platform is where the program will run, and the target platform is what kind of machine code the program emits (if the program is a compiler, otherwise irrelevant). Your compiler must use the headers of the host platform libc and the host platform libc itself when linking. You cannot use the headers of your build platform and link with the host platform libc. Your little charade of prefixing everything with os_ is just working around that your compiler is using the wrong headers by default. Your compiler should never even consider using the wrong headers. A cross-compiler knows it doesn't target the build platform, so there never is any risk of using the wrong headers or libraries. If your OS has a libc, I strongly recommend setting up a 'OS Specific Toolchain' (see wiki article) which means you modify gcc to make it know about your OS and its libc.

[Okay, out of time, think about this, and I'll continue later]

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 12:03 pm
by h0bby1
sortie wrote:Hi, I finally had a chance to read through your draft article. I realize it's not finished, but I figured I'd comment anyway. I don't have that much time right now, but I'll try to get some points across and continue later.

The C standard doesn't mandate any calling conventions. It's all entirely implementation defined. C could in theory be interpreted rather than translated. When you hear of the 'C calling convention', it refers to some default standard calling convention that everybody happened to agree upon (at least in the i386) world - it's called this because Windows has a heap of other calling conventions and the 'C calling convention' happens to the default and it's rather simple.
there are still calling convention that are recognized by many if not all compilers, like __cdecl, __stdcall, __fastcall, and the cpu also has standard way to make calls
sortie wrote: Note that libgcc is part of GCC, it doesn't have anything to do with GNU/Linux. There is a libgcc on Windows as well.
well gcc is the compiler used to build gnu linux, and shipped with it as well, the libc used in linux system is also the one of gcc, so they are still tightly related to each other
sortie wrote: You appear to be confused about the relationship between a compiler and the libc. You appear to have a Windows background, where the official compiler suite is Visual Studio where they ship a libc and compiler together, they are tightly coupled there. You suggest furthermore that programs should ship with their own copy of the libc as a shared library. This is really, really stupid. It's how Windows work, sure, but the C library is meant to be just another core system library. There is a completely staggering excess of code duplication in Windows - this is largely attributed to that programs can't assume anything is installed and sharing shared libraries is out of the question because few people care about binary compatibility.
i don't have only a window background, i programmed on both windows and linux, and i issue the two different configurations, the goal is to be able to have C code that can be compiled both under windows and linux, with any compiler, and used in the same over all project.
sortie wrote: My point is that the compiler and libc are separate projects and there is no such thing as a "compiler libc" universally (although, some systems do use this botched scheme).
all big compilers use their own version of the C library, including gcc and visual studio, and most other C compilers. actually i don't think there is a even a single implementation of the C library that can be called a standard implementation that would work with every executable compiled with any compiler. this just doesn't exist. and most if not all major implementation of C library are shipped with a compiler and specific to it.
sortie wrote: You don't appear to understand cross-compilation and why it's so vitally important for osdev work. Note that the 'build' platform is the one where the compiler runs, and the 'host' platform is where the program will run, and the target platform is what kind of machine code the program emits (if the program is a compiler, otherwise irrelevant). Your compiler must use the headers of the host platform libc and the host platform libc itself when linking. You cannot use the headers of your build platform and link with the host platform libc. Your little charade of prefixing everything with os_ is just working around that your compiler is using the wrong headers by default. Your compiler should never even consider using the wrong headers. A cross-compiler knows it doesn't target the build platform, so there never is any risk of using the wrong headers or libraries. If your OS has a libc, I strongly recommend setting up a 'OS Specific Toolchain' (see wiki article) which means you modify gcc to make it know about your OS and its libc.
the tricks are just to avoid problems, and to get rid of using any header that a compiler 'should include', but here you just contradict yourself when you say a compiler should never consider using the wrong header, and saying the lib C is totally independent of the compiler, maybe in theory that's supposed to be the case, but it's not how it works in most case.

this system is specially to avoid to use any 'wrong header' , because it doesn't use any header of any C library, and so the compiler always know what to do and how to declare and use the functions, that's the whole point of the system, that you can have a program compiled with visual studio and a program compiled with gcc that doesn't require to have the two version of the libc installed to run depending on which header it included and which compiler it has been compiled with

basically it get rid of many of the 'host' vs 'target' problem, at least for the problems specific to the use of C library and runtime, because it doesn't use the host C library header nor the host C library at all. so the build is made independent on the host for everything related to the C library, and the executable produced will be compatible no matter what host it is compiled on, and which compiler is used to compile it, it's the point of doing this


linux can have many advantages, but linux doesn't provide binary compatibility between distribution or versions, even a little variation in version of library and the program can't run, and it's a serious disadvantage for development of commercial non open source applications.

with this system, any compiler can be used to create the executable file on any type of host, because it doesn't use anything on the host system or the C library header and implementation specific to the 'host' building environment

if people want to develop application in C under windows with visual studio, borland , code warrior, or other compiler, with this system they can without problem, or use any gcc they have installed on the host to compile the file, but using definition of C functions from headers that are not the one present 'by default' on the host, and not the one that the compiler use by default that are either from the host system or from the local building environment, but instead use custom declarations of the functions that are compatible with every compilers but independent from the host configuration or specific building environment

provided that the compiler can generate valid machine code for the target architecture, and can respect calling convention specified explicitly in the host/compiler independent headers, with this system the host platform doesn't matter at all, neither even the compiler normally


i tend to make difference already between 'host system' and 'building environment', because even without having cross compiler, the same host can run different compilers with each their own version of the C headers and C library that are not entirely compatible with each other, or different compiler can be configured as well on the same host to compile different applications without it to be cross compiling for another target as well, it's the case with the linux version of the intel compiler for example. and much more common under windows.

with this system, any compiler from any host can generate the executable in a manner compatible with the target, without needing a cross compiler

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 12:48 pm
by Combuster
I'm for once not going to TLDR, mostly because it became somewhat apparent you are seriously misinformed.
there are still calling convention that are recognized by many if not all compilers, like __cdecl, __stdcall, __fastcall
Just tried GCC. It laughed at me for attempting something stupid:

Code: Select all

arm-linux-eabi-gcc -c -o test.o test.c
test.c:3:1: warning: 'cdecl' attribute directive ignored [-Wattributes]
(...)
test.c:3:1: warning: 'fastcall' attribute directive ignored [-Wattributes]
(...)
test.c:3:1: warning: 'stdcall' attribute directive ignored [-Wattributes]
well gcc is the compiler used to build gnu linux, and shipped with it as well
I tried a fresh Ubuntu last week. Said gcc couldn't be found (and if you think that's cheating, have fun trying it out on pretty much any Android device), so it doesn't get shipped with it. Also, quite important to note, Linux is not GNU.
all big compilers use their own version of the C library, including gcc
So a gcc cross-compiler is what? It's still the standard for compilers, and it lacks a C library.

Also, I can choose pretty much any combination of the following - I can even change one without needing to do anything with the other:

Code: Select all

$ ls /usr/portage/sys-devel/gcc
ChangeLog-2006         gcc-4.3.4.ebuild     gcc-4.6.0.ebuild
Manifest               gcc-4.3.5.ebuild     gcc-4.6.1-r1.ebuild
files                  gcc-4.3.6-r1.ebuild  gcc-4.6.2.ebuild
gcc-2.95.3-r10.ebuild  gcc-4.4.2.ebuild     gcc-4.6.3.ebuild
gcc-3.1.1-r2.ebuild    gcc-4.4.3-r3.ebuild  gcc-4.6.4.ebuild
gcc-3.2.2.ebuild       gcc-4.4.4-r2.ebuild  gcc-4.7.0.ebuild
gcc-3.2.3-r4.ebuild    gcc-4.4.5.ebuild     gcc-4.7.1.ebuild
gcc-3.3.6-r1.ebuild    gcc-4.4.6-r1.ebuild  gcc-4.7.2-r1.ebuild
gcc-3.4.6-r2.ebuild    gcc-4.4.7.ebuild     gcc-4.7.3.ebuild
gcc-4.0.4.ebuild       gcc-4.5.1-r1.ebuild  gcc-4.8.0.ebuild
gcc-4.1.2.ebuild       gcc-4.5.2.ebuild     gcc-4.8.1.ebuild
gcc-4.2.4-r1.ebuild    gcc-4.5.3-r2.ebuild  metadata.xml

$ ls /usr/portage/sys-libs/glibc
ChangeLog               glibc-2.13-r2.ebuild    glibc-2.16.0.ebuild
ChangeLog-2007          glibc-2.13-r4.ebuild    glibc-2.17.ebuild
Manifest                glibc-2.14.1-r2.ebuild  glibc-2.18.ebuild
files                   glibc-2.14.1-r3.ebuild  glibc-2.9_p20081201-r3.ebuild
glibc-2.10.1-r1.ebuild  glibc-2.14.ebuild       glibc-9999.ebuild
glibc-2.11.3.ebuild     glibc-2.15-r1.ebuild    metadata.xml
glibc-2.12.1-r3.ebuild  glibc-2.15-r2.ebuild
glibc-2.12.2.ebuild     glibc-2.15-r3.ebuild
And then I haven't even started about newlib. So much for bundling compilers with C libraries...
linux can have many advantages, but linux doesn't provide binary compatibility between distribution or versions, even a little variation in version of library and the program can't run, and it's a serious disadvantage for development of commercial non open source applications.
What happened to all the precompiled linux binaries on the web that are magically able to run on any version and distribution? It must be a lie! :-({|=

(Actually, windows is worse because it doesn't even attempt versioning. And the smarter people know that so they bundle their own DLLs to waste disk space and hide the problem.)

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 12:59 pm
by h0bby1
you still can't install gcc on a plateform and using it to compile application that use the C library without installing those headers, and you can't take those header from a borland compiler under windows, and compile program with gcc under linux using those headers, or compile program with visual studio using C header file from mac os, there is a certain degree of flexibity as compiler can still work with different version of the libc, but many compiler also ship their C library with them, intel compiler also have it's specific C library with optimized code path, and there many preprocessor variable that compiler will set that the libc depend on, they are not either completely independent from each other, it's actually nearly impossible to make a libC that doesn't contain any compiler specific thing, but at least with this system, you can control what the compiler will do no matter on which host it is run and no matter which compiler is used


precompiled linux binaries depend on a whole dependency chain, that depend on each package being compiled specifically with a specific compiler and building environment, they don't appear magically on the web by the operation of the holy spirit, even if it might look like it for linux user who just need to make apt get and all dependency are resolved to choose the good version package among the zillioooooons and zilllliooons of other version of the same package compiled with other version of the libC or other version of a library in one of the dependencies

application might be presented in a binary form to end users, but it's not the form distribution maintainers generally use, and most distribution build all application that are part of the official repository in a controlled environment for each target they release binary for

i'd really want to see how an application that is not open source, or even open source but is not built with gcc (no makefile/configure), would be magically installable without problem as binaries on all distributions

i have seen applications like that under linux, like maybe quake3, they require a 2000 line sh script to be run, including directive to /bin/ld to load good version of this or that lib and make checks on the system, but it's nothing trivial easy and straightforward under linux

the goal of the system here is to be able to develop things for the os from any other os with any version of any compiler, provided it support x86 ,respect specified calling convention ,support all the data type used, and can be made to pack structures with the proper layout, all those require compiler specific directive, and will impact compatibility if not dealt with properly, using default C headers of the host, or the one of the default building environment doesn't garantee that it will work on the target platform, which is the goal here, being able to use any compiler from any host to create executable that will work on the target os, without having to recompile the whole os if you want to use a different compiler for a specific application or library, and without having to ship any 'build specific' (if you prefer build specific to compiler specific, even if the compiler is still concerned by the version of the lib header used) C Library with the application

otherwise, it mean you limit the applications that can be run on the system to the ones that are compiled with a specific compiler using specific C headers and library


you don't need a cross compiler under windows to program an application for win2K under windows 7, and cross compiling is made much easier with project configurations specific to a particular target in visual studio if you need to enable some options or having target specific configurations, you don't need to have X versions of the compiler installed to compile for different target, but the compiler can use preprocessor definition to change how the libc function are defined in the C header for a particular configuration/target, gcc also uses it's lot of preprocessor definition that are used in the glibc headers, but linux is not made to run easily program compiled on other machines, i guess it's why sun developed java

if you say you managed to compile successfully a linux kernel with borland under windows that work with any linux distribution, i'd need a proof or i'd call it a blatant lie =) i'm not even sure the kernel can be safely compiled with the intel compiler, i remember trying and there was some issues, so it's not like linux is entirely independent from gcc and from at least some specific set of implementations of the C library

or replacing the glibc.so file by the msvc.dll converted to a .so,or using a version of glibc compiled as a dll instead of msvc.dll for a program compiled under visual studio, , i'm not sure it would work just fine

otherwise it mean you need to have a different version of the os in binary form for each compiler you want to use, to have all the dependencies to the C library and header files of all compiler resolved to a system wide C library, or then either having a specific version of the C library for each build made with a different compiler than the one installed on the system

whereas by forcing all compilers to use a unique version of the C library on all host , it can have a true system wide c library that can be used safely by application built from any host or any build environment, or compiler, because the compiler is forced to respect some standard for calling conventions, structure packing, and basic data type definitions, that you can control, and so binary will be automatically compatible with the target os no matter on which host it's built on, with minimum work to port or develop applications who need C library compatibility

Re: article on C compiler and standard lib C related to osde

Posted: Fri Sep 13, 2013 5:00 pm
by gerryg400
you still can't install gcc on a plateform and using it to compile application that use the C library without installing those headers
h0bby1, which headers do you mean by 'those headers' ?