Hey all;

I need to know the memory layout in detail of a c program. Since I have been unable to find out in google the exact things that I want to know I am going to write them here.

Please correct whichever is wrong.

Thanxxx

Assuming a stack going from high to low (From unix book)

----------------         high address 
| Environment  |
|  Variables   |
|              |
----------------
|              |
|    Stack     |
|      |       |
|      |       |
|              |
|              |
|      |       |
|      |       |
|    Heap      |
----------------
|    bss       |
|              |
|              |
----------------     end
| initialised  |
|              |
|              |
----------------     edata
|    text      |
|              |
|              |
----------------     low address (etext)

The end , etext and edata are the variables in unix. Now for my questions :)

1) The global variables can lie anywhere after heap ie from end of heap to etext ??

2) What is initialised data ?? Is it the same as int i = 0 in global ??

3) Is there any way to know the start of bss ??

Thanks a lot !! :)

Recommended Answers

All 5 Replies

Try creating a small exe with the gcc compiler then use the binutil

readelf -a exename

It should display all the information you need..

Hey

Could you help me a little more.

I did this by compiling a gcc file and then giving its exe in the command

readelf -a a // where a is the name of the exe

The error that I got was

" NOT an elf file - it has wrong magic bytes at the start "

What should i do

Ooops I assumed your were running a Linux based system with Executable Linkable Format(elf)..Are you compiling this on Windows or Linux/Unix and what binutils are available?

I am running my program on cygwin environment on Windows.

I am hoping you are aware of it but just incase http://www.cygwin.com/ .

The standard GNU assembler and linker 2.20.51.2 available inside cygwin have been installed.

Thanks a lot for your answers....

I'm not certain which binutils(binary utilities) are valid/available with that version but I would Google around and find out...

You could try

nm filename

Which should display all the symbols from the object file

Here's a link for a list of the GNU GCC compilers binutils

http://www.gnu.org/software/binutils/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.