“Port GNU Compiler Collection (gcc) to HelenOS” by Vivek Prakash, HelenOS, 2012
GNU Compiler Collection has frontends to C, C++, Objective-C, Fortran, Java, Ada and includes libraries for these languages. The goal of this project is to port at least the C compiler part of GNU CC i.e. gcc-core to HelenOS. Porting gcc will be a major step towards HelenOS to become a self-hosting operating system, where all development work can be done inside HelenOS itself.
The port of GNU binutils and the implementation of libposix library last year has already created a good platform for porting gcc to HelenOS, as this project is heavily dependent on both GNU binutils and libposix.
After preliminary examination, i found that gcc-core has following dependencies:
Internal libraries: libcpp, libdecnumber, libgcc, libgomp, libiberty, libmudflap, libquadmath, libssp, libintl and zlib. These libraries are all part of the gcc-core source tree.
External libraries: libgmp, libmpfr, libmpc. These are used for arbitrary precision arithmetic, multiple-precision floating point computations, and complex numbers’ arithmetic respectively.
userspace utilities: bison, byacc, flex, m4, texinfo, fixincludes, mkdep, etc. some of which are part of the gcc source tree itself.
So, i will first get the external dependencies and try to port them. The external libraries libgmp, libmpfr and libmpc are pretty much self contained and easy to port.
2. Building gcc
I believe the toughest and trickiest part would be to hijack the gcc build process. The bazaar log for binutils port gives very good insights, which can be applied to some extent in the gcc port too. The hardest part will be to gain insights into gcc build process and identify the parts that have dependencies and the ones that need to modified and have to be patched. This has been done excellently for binutils, and i believe same can be done here without reading thousands of lines of configure and make scripts line by line. It is evident that following files need to be written for gcc port too:
A toolchain.sh script to hijack gcc build process.
Makefile and Makefile.common that defines all the flags to be passed to the build system and calls or executes initialization scripts.
One or more patch files for gcc source tree, configure scripts and Makefile. The patch will probably simply consists of changing include and library paths in gcc Makefile and configure, where we may have to alter or add -I (include directories), -L (library paths) , -D (preprocessor flags), –nostdinc (standard directories exclusion), preprocessor directives, standard libc exclusion, etc.
Now, building the native gcc, which will run on HelenOS (host) and target HelenOS, on a different build system e.g. Linux can done by leveraging the canadian cross. A canadian cross is used to first build a cross-compiler for host on build-system, and then that cross-compiler is used by setting –build=<where the compiler runs>, –host=<where the compiler being built will run>, and –target=<where the executables the compiler being built will build shall run>. Here, host and target are same i.e. HelenOS. Since, we already have the HelenOS toolchain for all architectures, it can be used in the first stage of canadian cross, and then simple cross-compilation of gcc could be done in the second stage. If everything goes well, we can then use that native gcc to compile gcc source inside HelenOS itself to be sure there are no issues left after cross-compilation on the build-system. Whether or not this is feasible and possible to do is yet to be discussed and deeply analyzed.
Update: Using native gcc to compile gcc source inside HelenOS itself also requires GNU Make, which i will port after porting gcc. The dependencies of GNU Make e.g. libintl, m4 will be met while porting gcc itself. So, porting GNU Make will not much difficult. It can be easily cross-compiled as there are options to set `–target’ and `–host’ in Make configure script itself. For building GNU Make, one can use `build.sh’ shell script provided in the source, if there is no other `make’ program to use. This also eliminates the need to setup `make’ as the part of toolchain on the build-system.
3. Missing C/POSIX functions
I applied ldd, objdump & grep on an existing gcc binary to get the list of functions calls, and found that many of them are either implemented or have a stub in HelenOS libposix. But, i need to analyze it in more detail to get a good overview of what to implement and improve in HelenOS libposix and libc.
The missing functions required by gcc should be resolved in the following order:
If the functions are already present in HelenOS native libc, these functions has to be examined and a compatibility wrapper has to be written in libposix to call these functions.
If the functions are not present, they have to be written from scratch and implement them in libposix. Simultaneously, appropriate care needs to be taken so that these new functions can fit into native libc very smoothly.
If it’s not possible, may be because the dependency is fork(), mmap(), signals. etc. which are not supported, a stub may be created which calls not_implemented(), or gcc source has to be patched with a workaround depending on the situation.
I have found these online resources very useful in getting a good idea of my project:
The helenos/binutils and helenos/libposix branches are good source to examine the progress over time, which gives insights into design decisions taken during the binutils port and libposix implementation. These two branches were merged into helenos/devel from time to time, and helenos/devel was finally merged into helenos/mainline.
I have also gone through bazaar tutorials so that i don’t have any difficulty in using bazaar and launchpad in future. I have found following resources very useful while getting acquainted with bazaar and launchpad:
Note: Many parts of the proposal have been snipped which are related to personal information.
I may be wrong in my analysis at few places after preliminary examination. But that’s what I will rectify over the summer and hopefully finish the project! I intend to post my progress on the project over the summer here. Watch out for some interesting developments in HelenOS over this summer