Linking 32-bit object file and 64-bit object file
Posted: Wed Feb 16, 2011 11:42 am
I am not currently at home so I cannot test this in regards to kernel development, but I was experimenting with linking a 32-bit object file (such as a boot loader) with a 64-bit object file (64-bit entry/kernel).
I came up with a solution that was workable in a test:
linker.ld:
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386)
32.cpp:
int f32 ()
{
return sizeof(void *);
}
64.cpp
#include <stdio>
extern int f32 ();
int main ()
{
printf("64: %i\n", (int)sizeof(void *));
printf("32, %i\n", f32());
}
To build:
g++ -m32 32.cpp -c -nostdlib -fno-exceptions -fno-rtti
g++ -m64 64.cpp -c -fno-exceptions -fno-rtti
ld -T linker.ld 32.o -o 32a.o
g++ -o out 32a.o 64.o
running it results in:
./out:
64: 8
32: 4
If you alter 32.cpp to include actual operations, such as:
32.cpp:
int f32 ()
{
int a = sizeof(void *);
a *= 2;
a /= 2;
return a;
}
it then segfaults.
Haven't disassembled anything yet, but I'm guessing a simple function that just returns is the same in 32 and 64-bit. Since I am not at home and unable to fully test this, could anyone confirm for me that this works?
I came up with a solution that was workable in a test:
linker.ld:
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386)
32.cpp:
int f32 ()
{
return sizeof(void *);
}
64.cpp
#include <stdio>
extern int f32 ();
int main ()
{
printf("64: %i\n", (int)sizeof(void *));
printf("32, %i\n", f32());
}
To build:
g++ -m32 32.cpp -c -nostdlib -fno-exceptions -fno-rtti
g++ -m64 64.cpp -c -fno-exceptions -fno-rtti
ld -T linker.ld 32.o -o 32a.o
g++ -o out 32a.o 64.o
running it results in:
./out:
64: 8
32: 4
If you alter 32.cpp to include actual operations, such as:
32.cpp:
int f32 ()
{
int a = sizeof(void *);
a *= 2;
a /= 2;
return a;
}
it then segfaults.
Haven't disassembled anything yet, but I'm guessing a simple function that just returns is the same in 32 and 64-bit. Since I am not at home and unable to fully test this, could anyone confirm for me that this works?