Is gcc cross compiler needed in this case?[ SOLVED ]

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.
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Is gcc cross compiler needed in this case?[ SOLVED ]

Post by Raven »

Hi

I have designed my own BootLoader that loads my SecondStage BootLoader present on my hard disk partition.
Both are written in pure 16 bit assembly using nasm 0.97. The second stage bootloader then sets up protected mode by filling GDT, IDT and installing default handlers for exceptions and finally loads kernel at 0x10000 address.

What I wanna know is that i want my kernel to be pure flat binary which i obtain on Fedora using :

Code: Select all

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o kernel.o kernel.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o scrn.o scrn.c
ld -o kernel -T Linker.ld -N kernel.o scrn.o
objcopy -R .note -R .comment -S -O binary kernel kernel.bin
To do the same on windows xp i downloaded DJGPP that has gcc-4.3.2 and binutils-2.19.
It shows no compilation error but when tested it generates exception in my Second Stage Bootloader

I am not able to understand whether i need GCC Cross Compiler or not?
Please help, thanks in advance!
Last edited by Raven on Sun Mar 29, 2009 12:58 am, edited 1 time in total.
User avatar
Tomaka17
Member
Member
Posts: 67
Joined: Thu Oct 02, 2008 8:20 am

Re: Is gcc cross compiler needed in this case?

Post by Tomaka17 »

*stupid things*

*never post when you're half alseep*
Last edited by Tomaka17 on Sat Mar 07, 2009 11:51 am, edited 1 time in total.
MysteriOS
Currently working on: TCP/IP
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is gcc cross compiler needed in this case?

Post by Combuster »

The reason for a crosscompiler is already obvious here:
A DOS compiler produces something different than a linux compiler, which produces something different from a crosscompiler. A crosscompiler can be built for any platform, but will give you the same results. And it has the advantage of not being tainted by host-specific issues.

That and DJGPP sucks IMNSHO.

What this nonsense about C++-like exception handling comes from when dealing with x86 exceptions is beyond me. Please ignore the post directly above :roll:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Is gcc cross compiler needed in this case?

Post by Raven »

Thanks for your reply

I want to clear the way i perceive it :

Code: Select all

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o kernel.o kernel.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o scrn.o scrn.c
This will produce COFF files using DJGPP, am i right?

Code: Select all

ld -o kernel -T Linker.ld -N kernel.o scrn.o
This will resolve the unsolved references and will again create a COFF file using DJGPP, am i right?

Code: Select all

objcopy -R .note -R .comment -S -O binary kernel kernel.bin
This will produce a flat binary out of COFF, am i right?

If i am so far correct, then doing this using Linux native will produce elf's and finally binary.
Why should these two binaries differ as i am not targeting for any specific OS?

Please comment.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is gcc cross compiler needed in this case?

Post by Combuster »

Because both compilers still assume they are compiling for their specific OSes, even though you are not using the host's features.

Also, if you have different versions of GCC, the actual code generated will also be different (even if you run on the same OS)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Is gcc cross compiler needed in this case?

Post by Raven »

Thanks again :)

So it means Flat-Plain binaries created on Windows and Linux using the procedure i mentioned are totally different, but why is it so that Linux binary is loaded properly by Second Stage Bootloader (may Linux binaries are perfect one!)?

Also, thank for mentioning that DJGPP is not a good choice, i downloaded Cygwin on windows but am not able to understand how to make it cross compiler :roll:

Can you give some steps to do that or point me to some url. I tried to google it but you know either am not able to understand them or the actual content is something else!

Also what u mentioned about gcc is perfect i have used ndisasm on two binaries compiled from same source code using different versions of gcc on RH and FC =D>

Once more thanks
User avatar
Steve the Pirate
Member
Member
Posts: 152
Joined: Fri Dec 15, 2006 7:01 am
Location: Brisbane, Australia
Contact:

Re: Is gcc cross compiler needed in this case?

Post by Steve the Pirate »

Raven wrote:Also, thank for mentioning that DJGPP is not a good choice, i downloaded Cygwin on windows but am not able to understand how to make it cross compiler :roll:

Can you give some steps to do that or point me to some url. I tried to google it but you know either am not able to understand them or the actual content is something else!
http://wiki.osdev.org/GCC_Cross-Compiler
My Site | My Blog
Symmetry - My operating system.
User avatar
AndrewAPrice
Member
Member
Posts: 2306
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Is gcc cross compiler needed in this case?

Post by AndrewAPrice »

A cross compiler isn't scary, and if you're going to write your own OS then it's going to be WAY more difficult when you try to port libraries and programs to your OS.

To put it simply, if you've used Linux, chances are you've built something from source. You go into the directory and run:

Code: Select all

./configure
make
make install
It's standard procedure and becomes second nature after a while.

Creating a cross compiler follows the exact same steps, only this time you are passing extra parameters.. e.g. the steps to compile GCC copied and pasted from the wiki:

Code: Select all

../gcc-x.x.x/configure --target=$TARGET --prefix=$PREFIX --disable-nls \
    --enable-languages=c,c++ --without-headers
make all-gcc
make install-gcc
and do the same thing for binutils. The "export PREFIX=/usr/cross" and "export TARGET=i586-elf" lines at the beginning of the guide are merely setting a few environment parameters to save you time when you type $TARGET and $PREFIX as parameters to 'configure'.

A lot of users give endless excuses and try to find all the ways they can out of building a cross compiler, when it's so ridiculously simple. And if you ever run in to trouble we're always here for you to ask.
My OS is Perception.
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Is gcc cross compiler needed in this case?

Post by Raven »

I followed this
http://wiki.osdev.org/GCC_Cross-Compiler

But after I downloaded binutils-2.9 and did what i was supposed to do many yes and no appeared like
whether the C compiler(cc -g) is cross compiler...no
I haven't yet configured gcc. Any problem with it?
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Is gcc cross compiler needed in this case?

Post by Raven »

It is not ending, i mean it is repeating same messages again and again.
I think something i have done wrong.
Please help
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Is gcc cross compiler needed in this case?

Post by Raven »

Well..........................I failed :oops:
I tried binutils first and then gcc and other way too, but all in vain.
I tried for i586-elf but got i686-pc-cygwin and i686-pc-mingw
I tried to create elf i got pe-i386
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is gcc cross compiler needed in this case?

Post by Combuster »

binutils-2.9
You're 11 releases behind.
The horror.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Is gcc cross compiler needed in this case?

Post by Raven »

So is the reason i failed or there is something else?
I wanna make clear before attempting again, i have core2duo, xp and cygwin.
Please suggest which binutil and gcc version ( i hav bnu-2.w and gcc-3.3.2) to use?
Please mention any specific changes i need to do in wiki tutorial like order of compilation, etc.
I am in big trouble, please help
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is gcc cross compiler needed in this case?

Post by Combuster »

You got yourself in trouble, for violating forum rules

The answer: RTFM really.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Raven
Member
Member
Posts: 41
Joined: Sun Feb 01, 2009 12:20 am

Re: Is gcc cross compiler needed in this case?

Post by Raven »

I really apologize for violating rules, it might have happened by chance not by intention.

I wanted to make it clear that whether i have to download specific binutil-x.x.x and gcc-x.x.x package or not because wiki didn't mention about any specific combination.
Preparation

export PREFIX=/usr/cross
export TARGET=i586-elf
cd /usr/src
mkdir build-binutils build-gcc

The prefix will configure the build process so that all the files of your cross-compiler environment end up in /usr/cross, without disturbing your "normal" compiler setup. This tutorial has been shown to work in the same way for the x86_64-elf target, for building 64 bit executables (GCC 4.3.x). In order to do this, simply export TARGET as x86_64-elf instead of i586-elf.
binutils

cd /usr/src/build-binutils
../binutils-x.xx/configure --target=$TARGET --prefix=$PREFIX --disable-nls
make all
make install

This compiles the binutils (assembler, disassembler, and various other useful stuff), runable on your system but handling code in the format specified by $TARGET.

--disable-nls tells binutils not to include native language support. This is basically optional, but reduces dependencies and compile time. It will also result in English-language diagnostics, which the people on the Forum understand when you ask your questions. ;-)
gcc

Now, you can build GCC. (Use v3.3 or later - v3.2.x has a bug with internal __malloc declarations resulting in an error during compilation. This could be fixed by patching four occurrences in three different source files, but I lost the diff output and am not in a mind of re-checking. ;-) )

cd /usr/src/build-gcc
export PATH=$PATH:$PREFIX/bin
../gcc-x.x.x/configure --target=$TARGET --prefix=$PREFIX --disable-nls \
--enable-languages=c,c++ --without-headers
make all-gcc
make install-gcc

The path has to be extended since GCC needs the binutils we built earlier at some point of the build process. You might want to add these extensions to your $PATH permanently, so you won't have to use fully qualified path names every time you call your cross-compiler.

--disable-nls is the same as for binutils above.

--without-headers tells GCC not to rely on any C library (standard or runtime) being present for the target.

--enable-languages tells GCC not to compile all the other language frontends it supports, but only C (and optionally C++).

If you are compiling GCC <= 3.3.x, you need --with-newlib as additional parameter. Those older versions have a known bug that keeps --without-headers from working correctly. Additionally setting --with-newlib is a workaround for that bug.
I tried to compile that way last night but failed, that is way I asked you guys for help.
Post Reply