How to link 32- and 64-bit code together?
Posted: Wed Mar 18, 2020 7:47 am
So, I'm compiling my 32-bit bootstrap code with the following commands:
Then compiling my 64-bit kernel with:
Finally, I link everything with:
At which point I get an error message:
And then, a couple undefined references in boot32.o to symbols defined in the 64-bit code. Now, what is the best way to link this together? I know I could manually parse the 64-bit symbol table, or even use objcopy somehow to move the 64-bit sections into the 32-bit object file by hand, with the two exported symbols at defined addresses. But this looks extremely ugly to do, and I think I'm missing something about 32- and 64-bit executable formats.
Also, I'm using Multiboot, so the bootstrap code *must* be 32-bit, and the entire image loadable by the bootloader.
Code: Select all
i686-elf-gcc -c $(C_FILE) -o $(O_FILE) -std=gnu99 -ffreestanding -O2 -Wall -Wextra -DKERNEL_MODE=64 #for C source files
i686-elf-gcc $(S_FILE) -o $(O_FILE) -ffreestanding -nostdlib -r -DKERNEL_MODE=64 #for .S assembly files
i686-elf-as $(S_FILE) -o $(O_FILE) #for .s assembly files
i686-elf-gcc -T boot32.ld -o boot32.o -ffreestanding -O2 -nostdlib -r $(OBJ32) -lgcc
Code: Select all
x86_64-elf-gcc -c $(C_FILE) -o $(O_FILE) -std=gnu99 -ffreestanding -O2 -Wall -Wextra -DKERNEL_MODE=64 #for C source files
x86_64-elf-gcc $(S_FILE) -o $(O_FILE) -ffreestanding -nostdlib -r -DKERNEL_MODE=64 #for .S assembly files
x86_64-elf-objcopy -O elf64-x86-64 -B i386 -I binary $(BINARY_DATA) $(BINARY_OBJECT) #for binary data
Code: Select all
x86_64-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot32.o $(OBJ64) -lgcc
Code: Select all
[...]x86_64-elf/bin/ld: i386 architecture of input file `boot32.o' is incompatible with i386:x86-64 output
Also, I'm using Multiboot, so the bootstrap code *must* be 32-bit, and the entire image loadable by the bootloader.