Page 1 of 1

Linking 32bit bootstrap with 64bit kernel

Posted: Wed Oct 12, 2011 7:47 am
by TryHarder
Hey everybody,
I'm trying to run my kernel in long mode. I have my own simple bootloader that initializes Pmode and load kernel.
The file (entry.S) is responsible to setup Lmode structures, and drop to 64bit kernel code. However I have some problems with linking entry.S with rest of the files. Attempts that was made so far:
1) Compile entry.S with -m64 flag. Problem that it contains instructions like "movl %eax, %cr4" which is mandatory in Pmode, but fails to compile with -m64 flag.
2) Compile entry.S with -m32 flag and 'objcopy -O elf64-x86-64' it. Then 'ld' fails with

Code: Select all

ld: obj/kern/entry.o: bad relocation section name `.rel.text'
obj/kern/entry.o: could not read symbols: File format not recognized
I feel that there are something small that i'm missing (probably some flag), but can't figure it out. How it should be done?

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Wed Oct 12, 2011 8:37 am
by xenos
I guess you are missing the .code32 directive:

http://sourceware.org/binutils/docs/as/ ... 16bit.html

It allows you to put 32 bit code in an assembler file which is assembled to a 64 bit binary, like this (which is the startup code of my 64 bit kernel):

http://xenos.svn.sourceforge.net/viewvc ... iew=markup

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Wed Oct 12, 2011 8:49 am
by TryHarder
No, it's there. Tell me please how do you compile and link your OS with your Entry.S? Just making an elf64 file with .code32 inside?

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Wed Oct 12, 2011 9:03 am
by aod
I use GRUB bootloader booting 32-bit code and 64-bit kernel as a module. It requires simple menu.lst:

Code: Select all

title Your OS
kernel /boot/kernel32
module=/boot/kernel64
Multiboot info block contains fields necessary to load your module as specified in http://www.gnu.org/software/grub/manual ... iboot.html

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Wed Oct 12, 2011 11:57 am
by TryHarder
It doesn't solve the problem that a small piece of 32bit code should lay at the kernel and setup long mode isn't it. I have a problem of linking that code among rest of 64bit kernel.

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Wed Oct 12, 2011 12:17 pm
by AndrewBuckley
I don't think aod links together the 32bit and 64 bit code.

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Wed Oct 12, 2011 12:50 pm
by TryHarder
I don't think this includes some magic tricks, yet can't find any examples or usable info...

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Thu Oct 13, 2011 12:59 am
by xenos
TryHarder wrote:No, it's there. Tell me please how do you compile and link your OS with your Entry.S? Just making an elf64 file with .code32 inside?
Yes, exactly. I compile / assemble all source files to ELF64 object files. The only thing that is special about Entry.S is that it contains some 32 bit code, prefixed with .code32, but that does not matter to the linker.

Re: Linking 32bit bootstrap with 64bit kernel

Posted: Thu Oct 13, 2011 2:08 am
by TryHarder
Yes, after setting up things to compile for 64bit again it suddenly worked. Thanks guys!