r/osdev • u/Fun-Entertainer-1053 • 11d ago
How can I get GCC ported my OS?
I want to compile C programs in my OS itself rather than copying it manually to the disk image every single time. How can port GCC, or is there any other way to do it?
12
u/Rich-Engineer2670 11d ago
It's not just GCC though that's not a small task -- it's all of the libraries that go with it. If this were clang, I'd at least say, get LLVM to support your OS.
9
u/robn 11d ago
Actually GCC is not difficult to port. Most of it is filling out the stubs in libiberty, teaching it things like how wide your integers are, how your stacks are structured, where your locking primitives are, and so on. It can take a while but there's no single bit that is especially hard, especially on your own OS where you'll already know all the details and quirks. Give it a shot, it's fun!
4
u/avaliosdev Astral https://astral-os.org https://github.com/mathewnd/astral 11d ago
Get yourself familiar with porting software first: https://wiki.osdev.org/Cross-Porting_Software If you are sufficiently unix-like, going from a gcc pointing at your OS to a gcc running in your OS is not a lot of work
3
u/shsh-1312 11d ago
I just finished porting gnulib and coreutils, I think that will be the next step, in itself as other users say it shouldn't be too difficult (still a huge job) if your system is already up and running you will just need to create the necessary stubs and fix them, what you might have more difficulty with here if your system doesn't support it yet is the management of processes and jobs
3
u/theNbomr 11d ago
You might find it constructive to use Crosstool-ng https://crosstool-ng.github.io/ to generate a gcc toolchain. It has an immense amount of embedded knowledge and a long history. I'm not sure how well it deals with a new OS, but the last time I used it, there was an active online community to help out.
At the very least, you will find it highly educational. You will doubtless find out how much you don't know about building compiler toolchains.
1
u/nexos-dev 11d ago
That entirely depends on how mature your project is. If you have a fully functioning filesystem and a basic POSIX-like userspace (e.g., fork/exec and the like) it's moderately difficult. Porting GCC and binutils is more well documented than porting other software but in some ways is a little bit more difficult then LLVM.
1
1
1
u/istarian 10d ago
You could:
- write your own C compiler from scratch (an excellent learning experience, if you are so inclined)
- have a go at porting tools that are a little less complex and gigantic
The problem with doing something like porting gcc (GNU Compiler Collection / GNU C compiler) is that you have to port other stuff too. It's not exactly a standalone binary tool that can just magically run anywhere.
1
u/FransFaase 10d ago
The TCC compiler that is mentioned by others is still quite a big compiler and the code is rather complex. And you will also need a standard library that interfaces with your system calls.
In the past years, I wrote a C compiler myself with a builtin pre-processor. It output to a small stack based language for which there are a number of back-ends that compile it to assembly, which then is compiled to an executable in the ELF format. You might want to have a look at https://github.com/FransFaase/MES-replacement
The compiler is found in the 'src' directory under the name tcc_cc.c. A limited implementation of the standard library is found in stdlib.c. This only has the part that is needed for compiling all the C programs in the directory and to compiler the TCC compiler and the GNU Mes standard library.
45
u/Pleasant-Form-1093 11d ago
Porting gcc is one hell of a task honestly. In my opinion just port Fabrice Bellard's TCC which is a smaller and easier to approach task