GCC create flat binary with correctly linked data section

1k views Asked by At

I try to create a flat binary file with gcc using objcopy. My C code looks like:

char* str = "Hello world!";
printString(str,12);

When I create my binary with

C:\MinGW\bin\gcc -c cfile.c -o cfile.o
C:\MinGW\bin\gcc -nodefaultlibs -nostdlib -o comfile.o afile.o cfile.o
C:\MinGW\bin\objcopy -O binary comfile.o kernel.bin

The code section (.text) is linked correct, but the data section (.rdata) is just put behind my code section and not linked with my code section. If I access anything in the data section, it will load something from 0x0x401000 instead from the correct location and will crash.

Is there a way to solve this?

PS: Working C code is:

char* str = "Hello world!"-0x401000;
printString(str,12);
0

There are 0 answers