Reputation: 2000
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
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:
As Nikolai points out, you can look at the list of these with the pmap tool.
Upvotes: 2
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
Reputation: 84189
Look into /proc/<pid>/maps
and /proc/<pid>/smaps
(assuming Linux). Also pmap <pid>
.
Upvotes: 0