Suppose I am having this code:
int main() {
int var1;
char *ptr = malloc(5 * sizeof(char));
//...........
do_something();
//...........
return 0;
}
We know that the actual memory layout will be divided into segments like: .text
, .bss
, .data
, .heap
, .stack
.
I know how to use objdump
, readelf
, etc. But, I want to get a better view of the memory stack, where I can see things like:
.heap ptr
.stack do_something()
.text main()
.bss var1
The main point is: The actual variable names are missing from the output of objdump
, readelf
etc.
I am compiling this code with -g
, thus retaining the symbol table.
Then, why am I not able to see the memory layout with local/global variable names included?
objdump -x
shows the names of the variables if type is static
otherwise not. Why?
ptr
is a local variable ofmain
, just likevar1
. The object you'd like to see in .heap is a nameless block allocated by a call tomalloc
, and that block doesn't know that its address was stored in a variable namedptr
. This is just one of the reasons you do not really want to see these things in that level of detail. – Jinn