atv
atv

Reputation: 2000

Determine Stack bottom, start and end of data segment of C program

I am trying to understand how memory space is allocated for a C program. For that , I want to determine stack and data segment boundaries. Is there any library call or system call which does this job ? I found that stack bottom can be determined by reading /proc/self/stat. However, I could not find how to do it. Please help. :)

Upvotes: 2

Views: 1663

Answers (3)

Andy Ross
Andy Ross

Reputation: 12043

Processes don't have a single "data segment" anymore. They have a bunch of mappings of memory into their address space. Common cases are:

  • Shared library or executable code or rodata, mapped shared, without write access.
  • Glibc heap segments, anonymous segments mapped with rw permissions.
  • Thread stack areas. They look a lot like heap segments, but are usually separated from each other with some unmapped guard pages.

As Nikolai points out, you can look at the list of these with the pmap tool.

Upvotes: 2

wallyk
wallyk

Reputation: 57784

There is no general method for doing this. In fact, some of the secure computing environments randomize the exact address space allocations and order so that code injection attacks are more challenging to engineer.

However, every C runtime library has to arrange the contributions of data and stack segments so the program works correctly. Reading the runtime startup code is the most direct way of finding the answer.

Which C compiler are you interested in?

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84189

Look into /proc/<pid>/maps and /proc/<pid>/smaps (assuming Linux). Also pmap <pid>.

Upvotes: 0

Related Questions