Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include contents of char array in flat binary file

I am using flat binary files as external programs for my OS. When I compile them, like so:

gcc -Wall ctest.c -o ctest.bin -nostdlib -Wl,-Ttext=0x8000,-nostdlib -masm=intel
objcopy -O binary -j .text ctest.bin ctest

But doing this, the contents of the character array aren't put in the file. This is my code:

static volatile char string[4] = "Hi!\0";
static volatile char string2[15] = "Hello, World!\n\0";

int _start()
{
    asm("mov eax, [string]");
    asm("mov ebx, 0x00");
    asm("int 0x01");
    asm("mov eax, [string2]");
    asm("mov ebx, 0x00");
    asm("int 0x01");
    return 0;
}

and when I run objdump (I ran it on the elf, but I verified it had the same code as this):

00008000 <_start>:
8000:   55                      push   ebp
8001:   89 e5                   mov    ebp,esp
8003:   a1 70 90 00 00          mov    eax,ds:0x9070
8008:   bb 00 00 00 00          mov    ebx,0x0
800d:   cd 01                   int    0x1
800f:   a1 74 90 00 00          mov    eax,ds:0x9074
8014:   bb 00 00 00 00          mov    ebx,0x0
8019:   cd 01                   int    0x1
801b:   b8 00 00 00 00          mov    eax,0x0
8020:   5d                      pop    ebp
8021:   c3                      ret    

As you can see, the text is nowhere to be found. I was hoping it would do something like this: string db "Hi!", 0 which I would do with nasm.

What should I do so it includes the characters in the output bin file without coding this in assembly?
Thanks in advance.

like image 669
farlepet Avatar asked Jan 25 '26 08:01

farlepet


1 Answers

A binary executable file is typically divided into sections. Your strings have simply been placed into a different section than the code. This makes sense, since the code should be read-only but the strings have been declared non-const and volatile.

like image 66
Mark Ransom Avatar answered Jan 27 '26 23:01

Mark Ransom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!