Mixing 32 Bit and 64 Bit Assembler

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
nilres
Posts: 6
Joined: Sun Nov 08, 2015 1:20 pm

Mixing 32 Bit and 64 Bit Assembler

Post by nilres »

Hey,

I'm working on porting my operating system to the x64 architecture. I'm following the articles in the wiki for switching to long mode. I use gas for assembly code. The cross compile tool chain is set-up correctly. I compile the 32 bit bootstrap with the following command:
x86_64-elf-as --32 -o CMakeFiles/myOS.elf.dir/src/arch/x64/pc/boot.S.o /home/nils/code/osdev/myOS/src/arch/x64/pc/boot.S
But then the linkage fails with the following error:
/usr/bin/cc -T ../././src//arch/x64/pc/layout.ld -N -ffreestanding -nostdlib CMakeFiles/schlemmiOS.elf.dir/src/arch/x64/pc/boot.S.o -o schlemmiOS.elf
/usr/bin/ld: i386 architecture of input file `CMakeFiles/schlemmiOS.elf.dir/src/arch/x64/pc/boot.S.o' is incompatible with i386:x86-64 output
Obviously the linker complains that it can't build 32bit code into my the binary... How can I fix this? And why is this a problem?

Thanks.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Mixing 32 Bit and 64 Bit Assembler

Post by Combuster »

/usr/bin/cc (...)
/usr/bin/ld (...)
You might want to start with making sure you use the cross-compiler all the way
it can't build 32bit code into my the binary
Actually, it can't mix architectures. Instead you should tell the assembler that certain chunks are to be assembled in 32-bits (and some as 64-bits) using .code32 and .code64, instead of passing the 32-bit architecture flag on the command line
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nilres
Posts: 6
Joined: Sun Nov 08, 2015 1:20 pm

Re: Mixing 32 Bit and 64 Bit Assembler

Post by nilres »

Combuster wrote:You might want to start with making sure you use the cross-compiler all the way
Actually these are symlinks to the cross compiler. Ubuntu is doing some magic with the update-alternatives there. Probably not the greatest way for an operating system. But it's working for me and I haven't figured out a better way to say cmake which linker it should use.
Combuster wrote:Actually, it can't mix architectures. Instead you should tell the assembler that certain chunks are to be assembled in 32-bits (and some as 64-bits) using .code32 and .code64, instead of passing the 32-bit architecture flag on the command line
Ah ok. Is working now.Thanks.
Post Reply