32 and 64 bit code with gcc,g++

Programming, for all ages and all languages.
Post Reply
sebihepp
Member
Member
Posts: 184
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

32 and 64 bit code with gcc,g++

Post 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
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: 32 and 64 bit code with gcc,g++

Post 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.
If a trainstation is where trains stop, what is a workstation ?
sebihepp
Member
Member
Posts: 184
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: 32 and 64 bit code with gcc,g++

Post 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. :)
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: 32 and 64 bit code with gcc,g++

Post 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.
If a trainstation is where trains stop, what is a workstation ?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: 32 and 64 bit code with gcc,g++

Post 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)
sebihepp
Member
Member
Posts: 184
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: 32 and 64 bit code with gcc,g++

Post by sebihepp »

Hmm, That's what I will choose secondly, because I would need to parse ELF files. :D
But okay, I will need a elf-loader for user programms later, so I could also do it now...

Thanks for your replies
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: 32 and 64 bit code with gcc,g++

Post by Candy »

sebihepp wrote:Hmm, That's what I will choose secondly, because I would need to parse ELF files. :D
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 :-)).
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: 32 and 64 bit code with gcc,g++

Post 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.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
sebihepp
Member
Member
Posts: 184
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: 32 and 64 bit code with gcc,g++

Post 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?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: 32 and 64 bit code with gcc,g++

Post 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.
Post Reply