Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

assembly - Show segment addresses in gdb

This youtube video shows the layout of the virtual memory of a program, which includes the following segments from a high memory address to a low memory address.

  • kernel
  • stack (grows from high addresses to low addresses)
  • heap (grows from low addresses to high addresses)
  • data
  • text

Is the arrangement of these segments always so? Are they independent of what computer architecture a computer uses?

To inspect the address of each segment in gdb, could anybody show me how to do it?
Take the following C program as an example:

#include <stdio.h>

int main() {
    printf("Hello World!
");
}
question from:https://stackoverflow.com/questions/65877953/show-segment-addresses-in-gdb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is the arrangement of these segments always so?

No. For one thing, modern programs use multiple threads, which means there are multiple stacks. For another, modern malloc implementations use mmap, so "the heap" is not a single contiguous space, but a collection of disjoint arenas. Shared libraries' .text and .data are also sprinkled randomly, and may be between heap arenas.

Are they independent of what computer architecture a computer uses?

No. Certain architectures use stack which grows up (towards higher addresses), though these architectures are currently rare.

To inspect the address of each segment in gdb, could anybody show me how to do it?

GDB doesn't have any special support for examining segments. On Linux, /proc/$pid/maps will show you current mappings. Once you know the base address of any given segment, you can examine memory at that address using the "normal" GDB x command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...