2

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

1
  • I am working on linux platform and gcc compiler Commented Nov 18, 2009 at 15:27

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.