Page 1 of 2

Porting GCC to my OS

Posted: Tue Nov 06, 2007 4:02 pm
by giszo
Hi

I just tried to port GCC to my OS, so first read the article about it on the wiki page and i have some questions about it.

My first and biggest question is that when i compile the native compiler with the cross comiler (compiled before) i specify the --with-headers option to the configure with the directory of the headers of my C library (that works on my OS) but how do i tell to the compile progress to link the new GCC with my C library that runs on my OS?

Thanks for the answers,
giszo

Posted: Tue Nov 06, 2007 4:10 pm
by jnc100
Try OS Specific Toolchain. It'll tell you how to generate a gcc cross-compiler for your os that automatically links with your os's libc. You could then use that (e.g. CC=i586-pc-myos-gcc ./configure ...) to build a native gcc for your os.

Regards,
John.

Posted: Tue Nov 06, 2007 4:24 pm
by giszo
I'm not planning to use newlibc because i have my own implementation. I'm expecting a configure option or something similar for the libc option to link with it but i can't find it, that's why i don't really understand how the whole stuff will work...

giszo

Posted: Tue Nov 06, 2007 7:56 pm
by Speewave
I'm not planning to use newlibc because i have my own implementation
what do you mean. like a modified gcc? or your own compiler.

to figure out how gcc works wikipedia gcc

Posted: Wed Nov 07, 2007 12:45 am
by AndrewAPrice
Speewave wrote:
I'm not planning to use newlibc because i have my own implementation
what do you mean. like a modified gcc? or your own compiler.

to figure out how gcc works wikipedia gcc
GCC != newlib

Newlib is only an implementation of the C Standard Library.

GCC is a compiler.

A GCC package may come with a standard library, but it would be effectively useless in OSDeving as you'd have to provide your own anyway.

@giszo: You should be able to follow the instructions and just skip the newlib section. I'm not porting any C or C+ library yet (only my API).

Posted: Wed Nov 07, 2007 1:12 am
by jnc100
As long as your cross-compiler is configured without the option --without-headers, it should expect a c library to be present on the target system, usually in $PREFIX/$TARGET/lib/libc.a, and any attempt to link a program with your cross compiler should include libc by default. If you can get that working, e.g. by compiling a few simple test cases that use the c library, you can then compile gcc to run on your os with similar steps, by using the --host=$TARGET --target=$TARGET options to configure. Note that specifying --without-headers implies that you have a full c library, with all the required header files.

Regards,
John.

Posted: Wed Nov 07, 2007 1:42 am
by giszo
As i run througt on the page again that jnc100 told me, i got an idea...
I may change the linker scripts in the cross compiler and add my libc to the libraries that has to be linked to the application, so when i compile my native compiler it will be linked with my own libc?

It this is true one more question: what should i do if i'd use dynamic linking?

Posted: Wed Nov 07, 2007 1:52 am
by giszo
jnc100 wrote:Note that specifying --without-headers implies that you have a full c library, with all the required header files.
You mean --with-headers ?

Posted: Wed Nov 07, 2007 2:50 am
by JamesM
Gizo: The way to do this is make two compilers.

1. Build a cross-compiler toolchain for your OS. What I mean by this is: Build a cross-compile toolchain that when run on your HOST or DEVELOPMENT system, produces a binary which can be run on your OWN os.. To do this you need to do several things:

- Obviously build the toolchain. Put it in somewhere out of the way, like /usr/myos-cross.
- cd into /usr/myos-cross/lib/gcc-4.0.1/ (that is an approximate directory name: go into /usr/myos-cross/lib/ and follow the trail of directories until you find a load of .o files: crt0.o etc).
- Copy your own libc.a, libm.a, libg.a, crt0.o, crtbegin.o/crtend.o (you can just make these blank files)
- Test it! Get it compiling a test program, copy the resulting binary across to your OS and run it.

2. This is the next stage. You already have a compiler that will product native binaries and link with your own libraries, now you want one which will do that self-hosted.

- Build another toolchain! Put it somewhere like /usr/myos-native, and this time before calling ../gcc-4.0.1/configure, do this:

Code: Select all

export CC=/usr/myos-cross/bin/gcc
export CFLAGS=<any extra flags you may need to build on your OS>
export LDFAGS=<any extra flags you may need to link on your OS: extra libraries etc>
- When that's compiled, you're done! You can take /usr/myos-native/bin/gcc and /usr/myos-native/bin/ld and copy them over to your own OS (don't forget cc1 and collect2 and all the other executables in that directory) and run them! hey presto!

Any questions, just ask :D

JamesM

Posted: Wed Nov 07, 2007 6:17 am
by giszo
Ok.. So the answer for me is to drop my libc.a to .../my_cross_dir/lib and recompile gcc/binutils with the cross compiler.
Thanks! I'll try that and come back with the results!

giszo

Posted: Wed Nov 07, 2007 10:57 am
by giszo
I'm just finished compiling the cross compiler toolchain, so now i have i586-pc-giszOS-gcc, i586-pc-giszOS-ld and so on...
I also added the bin directory of my cross dir to path and tried to compile a really simple .c file with an empty main() and i got the following:

/home/giszo/gcc_giszOS_cross/bin/../lib/gcc/i586-pc-giszOS/4.2.2/../../../../i586-pc-giszOS/bin/ld: crt0.o: No such file: No such file or directory
collect2: ld returned 1 exit status

Where should crt0.o be? I haven't found it in the subtree of my cross dir. I found crtbegin and crtend only.

Posted: Wed Nov 07, 2007 11:13 am
by AJ
Hi,

crt0.o is the runtime support for programs written in C. On a cross compiler (or new native compiler), you need to write that yourself.

Cheers,
Adam

Posted: Wed Nov 07, 2007 11:34 am
by giszo
Ok, i created a crt0.o file and put it to the lib directory and now it looks working and i can create an ELF executable that runs well on my OS with the cross compiler.

Now if i understand the process well i should compile the binutils and gcc again with the cross compiler, right?

Posted: Wed Nov 07, 2007 12:40 pm
by giszo
What parameters should i use to configure and compile binutils with the cross compiler toolchain?

I tried several commands:

#1: CC=i586-pc-giszOS-gcc ./configure --target=i586-pc-giszOS --prefix=/home/giszo/gcc_giszOS_native --disable-nls
This fails with the following message: checking whether the C compiler works... configure: error: cannot run C compiled programs.

#2: CC=i586-pc-giszOS-gcc ./configure --host=i686-pc-linux --target=i586-pc-giszOS --prefix=/home/giszo/gcc_giszOS_native --disable-nls
Configure finished without error but i'm not sure if it's correct to add the --host parameters but this one also fails during make.

giszo

Posted: Wed Nov 07, 2007 1:15 pm
by jnc100

Code: Select all

./configure --target=i586-pc-gizOS --host=i586-pc-gizOS
Basically means compile something that will run on your os (host) by using the cross compiler, and what you're compiling will also target your os (i.e. be a native toolchain).
giszo wrote:
jnc100 wrote:Note that specifying --without-headers implies that you have a full c library, with all the required header files.
You mean --with-headers ?
Indeed :oops: Acutally I meant "Note that _not_ specifying --without-headers..."

Regards,
John.