Page 1 of 1
32 and 64 bit code with gcc,g++
Posted: Tue Aug 03, 2010 5:25 am
by sebihepp
Hello,
I want to make an elf64 kernel and therefore need 32 bit code to make it bootable by grub2.
With NASM there is no problem, but I have some c++ code wich I need to compile for 32 bit.
My linker (ld) always complains that the (32 bit) file is incompatible with target architecture.
I found out that the compiler always produces an elf32 file. How do I get g++ to produce 32 bit
code into an elf64 file?
best regards
sebihepp
Re: 32 and 64 bit code with gcc,g++
Posted: Tue Aug 03, 2010 6:11 am
by gerryg400
I don't think you can mix 32bit and 64bit object files easily in one ELF executable file.
This link shows several other ways to load a 64 bit kernel.
Re: 32 and 64 bit code with gcc,g++
Posted: Tue Aug 03, 2010 6:17 am
by sebihepp
Damn. I thought I could compile the 32 and 64 bit kernel together and at startup a small routine decides to wich it jumps.
Re: 32 and 64 bit code with gcc,g++
Posted: Tue Aug 03, 2010 6:24 am
by gerryg400
Well there are several things you can do. Remember Grub will load more than one file using its 'module' feature. You could have a 32bit loader that decides which of those to use.
Re: 32 and 64 bit code with gcc,g++
Posted: Tue Aug 03, 2010 6:29 am
by Candy
sebihepp wrote:Hello,
I want to make an elf64 kernel and therefore need 32 bit code to make it bootable by grub2.
With NASM there is no problem, but I have some c++ code wich I need to compile for 32 bit.
My linker (ld) always complains that the (32 bit) file is incompatible with target architecture.
I found out that the compiler always produces an elf32 file. How do I get g++ to produce 32 bit
code into an elf64 file?
best regards
sebihepp
You can use a very ugly hack to tell LD to link your 64-bit objects to 32-bit output. Then tell nasm to make an elf64 file and link it to a 32-bit file. Grub will happily load & jump to your entry point, your entry point then has to be 32-bits code until you've got long mode running. The rest is as it was before.
I'm sure this hack is on the wiki - I got it from there a few weeks ago. Basically, put in your linker.ld:
Code: Select all
OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386:x86-64)
Re: 32 and 64 bit code with gcc,g++
Posted: Tue Aug 03, 2010 6:30 am
by sebihepp
Hmm, That's what I will choose secondly, because I would need to parse ELF files.
But okay, I will need a elf-loader for user programms later, so I could also do it now...
Thanks for your replies
Re: 32 and 64 bit code with gcc,g++
Posted: Tue Aug 03, 2010 8:13 am
by Candy
sebihepp wrote:Hmm, That's what I will choose secondly, because I would need to parse ELF files.
But okay, I will need a elf-loader for user programms later, so I could also do it now...
Thanks for your replies
Grub parses & loads the elf file for you. There's nothing left until you're in user space.
(and loading a 64-bit userspace program is about 140 lines of definitions and 40 lines of elf loading code - have it working right here
).
Re: 32 and 64 bit code with gcc,g++
Posted: Wed Aug 04, 2010 3:48 am
by Creature
When using GRUB Legacy, you can use 64-bit C/C++ code only and put the 32-bit code in your multiboot assembly file for GRUB. As soon as you get command from GRUB, you can setup a 64-bit GDT, the PML4, etc., activate long mode (in short, everything needed to be in long mode). Then when you're in long mode, you switch to 64-bit instructions and call your Main C/C++ entry point (which is compiled as 64-bits).
I believe
ArcticOS does something similar.
Re: 32 and 64 bit code with gcc,g++
Posted: Wed Aug 04, 2010 7:10 am
by sebihepp
I have just read something about elf32 format.
If I understand correctly, I just have to read all programm-headers and do, what theay tell me. (besides check for integrity)
If there is:
Code: Select all
LOAD off 0x00000060 vaddr 0x00100000 paddr 0x00100000 align 2**5
filesz 0x0000d56c memsz 0x0000ebe4 flags rwx
I have to allocate 0xEBE4 Bytes in memory, wich will be mapped to 0x100000. Then loading bytes at file offset 0x60 length 0xD56C
to the begin of the allocated memory and set the rest until 0xEBE4 to zero, then jump to "EntryPoint - vaddr", right?
Re: 32 and 64 bit code with gcc,g++
Posted: Wed Aug 04, 2010 7:16 am
by Candy
sebihepp wrote:I have just read something about elf32 format.
If I understand correctly, I just have to read all programm-headers and do, what theay tell me. (besides check for integrity)
If there is:
Code: Select all
LOAD off 0x00000060 vaddr 0x00100000 paddr 0x00100000 align 2**5
filesz 0x0000d56c memsz 0x0000ebe4 flags rwx
I have to allocate 0xEBE4 Bytes in memory, wich will be mapped to 0x100000. Then loading bytes at file offset 0x60 length 0xD56C
to the begin of the allocated memory and set the rest until 0xEBE4 to zero, then jump to "EntryPoint - vaddr", right?
Yes.