Porting GCC to my OS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Porting GCC to my OS

Post 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
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Post 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
Speewave
Member
Member
Posts: 40
Joined: Fri Jun 22, 2007 1:11 pm
Location: The USA
Contact:

Post 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
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post 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).
My OS is Perception.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Post 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?
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Post 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 ?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Post 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
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Post 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?
giszo
Member
Member
Posts: 124
Joined: Tue Nov 06, 2007 2:37 pm
Location: Hungary

Post 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
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
Post Reply