Page 1 of 2
Is gcc cross compiler needed in this case?[ SOLVED ]
Posted: Fri Mar 06, 2009 10:03 pm
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!
Re: Is gcc cross compiler needed in this case?
Posted: Sat Mar 07, 2009 4:25 am
by Tomaka17
*stupid things*
*never post when you're half alseep*
Re: Is gcc cross compiler needed in this case?
Posted: Sat Mar 07, 2009 8:29 am
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
Re: Is gcc cross compiler needed in this case?
Posted: Sat Mar 07, 2009 9:54 am
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.
Re: Is gcc cross compiler needed in this case?
Posted: Sat Mar 07, 2009 12:03 pm
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)
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 1:51 am
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
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
Once more thanks
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 2:59 am
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
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
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 4:30 am
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:
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.
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 12:54 pm
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?
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 1:28 pm
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
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 2:29 pm
by Raven
Well..........................I failed
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
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 3:16 pm
by Combuster
binutils-2.9
You're 11 releases behind.
The horror.
Re: Is gcc cross compiler needed in this case?
Posted: Mon Mar 09, 2009 11:06 pm
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
Re: Is gcc cross compiler needed in this case?
Posted: Tue Mar 10, 2009 1:42 am
by Combuster
You got yourself in trouble, for violating
forum rules
The answer: RTFM really.
Re: Is gcc cross compiler needed in this case?
Posted: Tue Mar 10, 2009 3:10 am
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.