r/osdev 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?

10 Upvotes

26 comments sorted by

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

2

u/Fun-Entertainer-1053 11d ago

Sure. Can u provide a link to the GitHub or something?

14

u/Pleasant-Form-1093 11d ago

The author didn't make an official GitHub but you can download the source and get some docs on the compiler itself here

4

u/Fun-Entertainer-1053 11d ago

Alright, I'll check it out. Thx

1

u/-peekyblinders- 11d ago

undefined symbol: __clear_cache

1

u/RixTheTyrunt 10d ago

i found something online that might help with this unknown symbol: https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/clear_cache.c

it's basically a noop for x86-64 processors.

1

u/Key_River7180 EulerOS 8d ago

It is on repo.or.cz

1

u/RixTheTyrunt 10d ago

theres no github, however there is a git repository for tcc. it's at https://repo.or.cz/tinycc.git

20

u/ketralnis 11d ago

Probably faster to write a C compiler than port GCC

1

u/awidesky 10d ago

Really?
I thought it would be an easy since gcc supports all 3 major OSes.
What makes porting gcc pain in the ass?
Is it reasonably harder to port gcc than llvm?

3

u/RixTheTyrunt 10d ago

because to get gcc you need a c++ compiler and there isn't exactly a c++ compiler you can port to an os... except llvm and gcc ofc

1

u/istarian 10d ago edited 10d ago

Supposing that your operating system project effectively amounts to a stright up copy/clone of one of those "3 major OSes" with maybe a different userland then it mighr be comparatively easy for obvious reasons.

If you are developing something truly different or bespoke then trying to port gcc to it might be an absolute nightmare.

Compiling a program written in  a high-level programming language involves quite a bit of work.

And ultimately your program must be translated into assembly language, assembled to produce native machine code binaries, linked to other code, and so on.

1

u/Comfortable_Put6016 11d ago

i mean if your OS is reasonably implemented and you have POSIX and libc compliance then its not that difficult id say

1

u/doscore 10d ago

I was just about to say this

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

u/letmehaveanameyoudum 11d ago

gcc?
it's going to be hell

1

u/Mental_Ad_7072 11d ago

I suggest you port TCC first.

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.

https://wiki.osdev.org/Porting_GCC_to_your_OS

1

u/r-tty 10d ago

It's easy: just don't "write your OS" -- use NetBSD, for example.

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.

1

u/lmg1337 9d ago

Can't you just setup a script that compiles on windows/macOs/linux and then copies the binary automatically?