tags:

views:

203

answers:

3

Hi,

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. :)

A: 

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?

wallyk
i am workin on linux platform and gcc compiler
atv
A: 

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

Nikolai N Fetissov
+2  A: 

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.

Andy Ross