0

This question somehow relates to my previous question, but now I have a binary which is not stripped.

Let's say I have the following code snippet

QString my_variable("almafa");
qDebug() << my_variable;

Before calling the << operator I expect that either rsi or rdi to contain the address of my_variable. Correct me if I am wrong.

   0x0000000000400aca <+100>:    lea    -0x60(%rbp),%rdx
   0x0000000000400ace <+104>:    lea    -0x50(%rbp),%rax
   0x0000000000400ad2 <+108>:    mov    %rdx,%rsi
   0x0000000000400ad5 <+111>:    mov    %rax,%rdi
   0x0000000000400ad8 <+114>:    callq  0x400d40 <QDebug::operator<<(QString const&)>

strings a.out | grep my_variable returns my_variable, so it is present in the executable. Is it somehow possible to ask gdb (or any other linux debugger) what is the variable name which belongs to a given address? my_variable is recognized by gdb since command print my_variable is working.

robert
  • 887
  • 2
  • 12
  • 28

2 Answers2

1

I was trying amritanshu's answer using the following code:

int z;
int main() 
{
    int x;
    x = 5;
    z = 6;
    return 0; 
}

Please note that z is a global variable and x is a local variable. I ran the program in gdb, paused execution of the program at the line x = 5. When I used the command

info address z

I got the following response:

Symbol "z" is static storage at address 0x601030.

And when I typed:

info symbol 0x601030

GDB displayed:

z in section .bss

So, the info symbol address command worked for me when examining a global variable. But, I wasn't able to do the same thing for the local variable x. I determined the address of x using the following command:

print &x

When I used the address that was displayed in the info symbol command, GDB displayed

No symbol matches ...

So, it may depend on whether the address your examining is used for a global or local variable. As a workaround, you can try using the command info locals, and then use the print &variable command on each local variable that is displayed.

GDB documentation on the info commands related to examining the symbol table can be found at Examining the Symbol Table.

ashleydc
  • 19
  • 1
0

To identify the nearest symbol for a given address you can try "info" command:

info symbol address

amritanshu
  • 101
  • 1