Question

For example in the following code "justatest" and the format "%s" is defined in heap:

char str[15]="justatest";
int main(){
    printf("%s",str);
    return 0;
}

in GDB,i got the assembly code before call to printf as:

=> 0x0804841f <+14>:    movl   $0x804a020,0x4(%esp)
   0x08048427 <+22>:    movl   $0x80484d8,(%esp)
   0x0804842e <+29>:    call   0x80482f0 <printf@plt>

Do i have to examine the parameter 1by1 using "x/s 0x804a020" and "x/s 0x80484d8"

or is there a Table of constants defined in heap that i can directly refer to?

thanks!

Was it helpful?

Solution

Your understanding about str reside on heap is not correct. Its global variable which gets stored into the data segment. Regarding your print global variable, you can do as follows on my GNU/Linux terminal.

$ gcc -g -Wall hello.c
$ gdb -q ./a.out 
Reading symbols from /home/mantosh/practice/a.out...done.
(gdb) break main
Breakpoint 1 at 0x400524: file hello.c, line 6.
(gdb) run
Starting program: /home/mantosh/practice/a.out 

Breakpoint 1, main () at bakwas.c:6
6       printf("%s",str);
(gdb) disassemble main
Dump of assembler code for function main:
   0x0000000000400520 <+0>: push   %rbp
   0x0000000000400521 <+1>: mov    %rsp,%rbp
=> 0x0000000000400524 <+4>: mov    $0x601020,%esi
   0x0000000000400529 <+9>: mov    $0x4005e4,%edi
   0x000000000040052e <+14>:    mov    $0x0,%eax
   0x0000000000400533 <+19>:    callq  0x4003f0 <printf@plt>
   0x0000000000400538 <+24>:    mov    $0x0,%eax
   0x000000000040053d <+29>:    pop    %rbp
   0x000000000040053e <+30>:    retq   
End of assembler dump.

(gdb) p str
$1 = "justatest\000\000\000\000\000"
(gdb) p &str
$2 = (char (*)[15]) 0x601020

// These are addresses of two arguments which would be passed in printf.
// From assembly instruction we can verify that before calling the printf
// these are getting stored into the registers.
(gdb) x/s 0x4005e4
0x4005e4:    "%s"
(gdb) x/s 0x601020
0x601020 <str>:  "justatest

OTHER TIPS

later i found that for object files without a debugging symbols table

objdump -t obj

would contains most of the symbols of global variables/functions and their address ,and

objdump -D obj     instead of -d

would include all sections such as .text/.data/.rodata instead of .text only

these two combined provided sufficient access to what i mentioned aboved, such as switch tables/const strings/global variables

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top