32 and 64 bit code with gcc,g++
-
- Member
- Posts: 184
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
32 and 64 bit code with gcc,g++
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
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++
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 ?
-
- 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++
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++
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 ?
Re: 32 and 64 bit code with gcc,g++
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.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
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)
-
- 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++
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
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++
Grub parses & loads the elf file for you. There's nothing left until you're in user space.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
(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++
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.
I believe ArcticOS does something similar.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
-
- 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++
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:
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?
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
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++
Yes.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:I have to allocate 0xEBE4 Bytes in memory, wich will be mapped to 0x100000. Then loading bytes at file offset 0x60 length 0xD56CCode: Select all
LOAD off 0x00000060 vaddr 0x00100000 paddr 0x00100000 align 2**5 filesz 0x0000d56c memsz 0x0000ebe4 flags rwx
to the begin of the allocated memory and set the rest until 0xEBE4 to zero, then jump to "EntryPoint - vaddr", right?