'GDB similar address output
Can someone explain why all four print statements below print out the same instruction pointer even though dereferencing is going on for two of them. Moreover why is $rip void (*)()? I'm just struggling to see the subtle differences.
(gdb) print *$rip
$6 = {void ()} 0x55555555460e <main+20>
(gdb) print $rip
$7 = (void (*)()) 0x55555555460e <main+20>
(gdb) x/i $rip
=> 0x55555555460e <main+20>: callq 0x55555555461a <foo>
(gdb) x/i *$rip
=> 0x55555555460e <main+20>: callq 0x55555555461a <foo>
Solution 1:[1]
GDB uses the symbols in the elf file to know the type of what you are trying to print. For example, if you try to print an array of characters, it would print it as a string .. etc.
Now, this address, is inside main() by an offset that is 20 bytes from the beginning of main(). It's code. A generic function pointer type void (*)() is assumed. I think as in C, &main is the same as main, both are pointers to functions, this is why print *$rip prints the same as print $rip but with a difference in the type. But nothing to print other than the contents of the instruction at this location.
x/i on the other hand, prints the disassemble of the contents of that address.
If you want to print the contents, for example, as array of ints, you'd need to cast the pointer and use something like:
print *(unsigned *) $rip
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hesham_EE |
