1.5. HELLO, WORLD!
int main()
{
printf(0x400238);
return 0;
}
It’s hard to believe, but this code prints the aforementioned string.
If you would change the address to0x400260, the “GNU” string would be printed. This address is true
for my specific GCC version, GNU toolset, etc. On your system, the executable may be slightly different,
and all addresses will also be different. Also, adding/removing code to/from this source code will probably
shift all addresses back or forward.
1.5.3 GCC—one more thing.
The fact that ananonymousC-string hasconsttype (1.5.1 on page 9), and that C-strings allocated in
constants segment are guaranteed to be immutable, has an interesting consequence: the compiler may
use a specific part of the string.
Let’s try this example:
#include <stdio.h>
int f1()
{
printf ("world\n");
}
int f2()
{
printf ("hello world\n");
}
int main()
{
f1();
f2();
}
Common C/C++-compilers (including MSVC) allocate two strings, but let’s see what GCC 4.8.1 does:
Listing 1.24: GCC 4.8.1 + IDA listing
f1 proc near
s = dword ptr -1Ch
sub esp, 1Ch
mov [esp+1Ch+s], offset s ; "world\n"
call _puts
add esp, 1Ch
retn
f1 endp
f2 proc near
s = dword ptr -1Ch
sub esp, 1Ch
mov [esp+1Ch+s], offset aHello ; "hello "
call _puts
add esp, 1Ch
retn
f2 endp
aHello db 'hello '
s db 'world',0xa,0