Linking 32bit bootstrap with 64bit kernel

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.
Post Reply
TryHarder
Member
Member
Posts: 28
Joined: Mon Aug 22, 2011 12:45 am

Linking 32bit bootstrap with 64bit kernel

Post 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?
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Linking 32bit bootstrap with 64bit kernel

Post 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
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
TryHarder
Member
Member
Posts: 28
Joined: Mon Aug 22, 2011 12:45 am

Re: Linking 32bit bootstrap with 64bit kernel

Post 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?
aod
Member
Member
Posts: 26
Joined: Fri Sep 30, 2011 2:36 am

Re: Linking 32bit bootstrap with 64bit kernel

Post 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
TryHarder
Member
Member
Posts: 28
Joined: Mon Aug 22, 2011 12:45 am

Re: Linking 32bit bootstrap with 64bit kernel

Post 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.
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

Re: Linking 32bit bootstrap with 64bit kernel

Post by AndrewBuckley »

I don't think aod links together the 32bit and 64 bit code.
TryHarder
Member
Member
Posts: 28
Joined: Mon Aug 22, 2011 12:45 am

Re: Linking 32bit bootstrap with 64bit kernel

Post by TryHarder »

I don't think this includes some magic tricks, yet can't find any examples or usable info...
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Linking 32bit bootstrap with 64bit kernel

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
TryHarder
Member
Member
Posts: 28
Joined: Mon Aug 22, 2011 12:45 am

Re: Linking 32bit bootstrap with 64bit kernel

Post by TryHarder »

Yes, after setting up things to compile for 64bit again it suddenly worked. Thanks guys!
Post Reply