State of the art
After you implement a program, you may wonder how much
memory does your program consume. In particular, for embedded programs, the memory
resource in embedded device is limited. The memory consumption of your programs
is crucial.
I. State of the art
The application memory is generally composed of some
following elements:
·
Read only memory
o
Code
o
Constant data
·
Random-access memory
o
Global or static writable variable data
o
Stack memory
o
Heap memory
Except stack memory and heap memory, all of these details can be found
in the map file generated while linking your programs.
To analyse programs data and code one can undergo the following steps:
Create a map file
Well first of all, you'll need a map file. If you don't have
one, it will be very difficult to disclose your program’s code and static data
size.
So first, I'll show you how to create a good map file. For
this, I will create a new project. You can do the same, or adjust your own
project. I create a new project using the Win32 Application option in VC++,
selecting the 'typical "Hello Word!" application' to keep the size of
the map file reasonable for explanation.
Now, you're ready to compile and link your project. After
linking, you will find a .map file in your intermediate directory (together
with your exe).
Read the map file
Now, it's time to open the map file with text editor(notepad or something similar). You map file will look like this:
App
Timestamp is 4495cecc (Sun Jun 18 15:08:12 2006)
Preferred load address is 10000000
Start Length Name Class
0001:00000000 00006ff2H .text CODE
0002:00000000 000000f4H .idata$5 DATA
0002:000000f8 000038e9H .rdata DATA
0002:000039e4 00000028H .idata$2 DATA
0002:00003a0c 00000014H .idata$3 DATA
0002:00003a20 000000f4H .idata$4 DATA
0002:00003b14 00000442H .idata$6 DATA
0002:00003f60 0000005bH .edata DATA
0003:00000000 00000004H .CRT$XCA DATA
0003:00000004 00000004H .CRT$XCZ DATA
0003:00000008 00000004H .CRT$XIA DATA
0003:0000000c 00000004H .CRT$XIC DATA
0003:00000010 00000004H .CRT$XIZ DATA
0003:00000014 00000004H .CRT$XPA DATA
0003:00000018
00000004H .CRT$XPZ DATA
0003:0000001c 00000004H .CRT$XTA DATA
0003:00000020 00000004H .CRT$XTZ DATA
0003:00000030 00000e20H .data DATA
0003:00000e50 000038ccH .bss DATA
0004:00000000 00000208H .rsrc$01 DATA
0004:00000210 00003e00H .rsrc$02 DATA
The top of the map file contains the module name, the
timestamp indicating the link of the project, and the preferred load address
(which will probably be 0x00400000 unless you're
using a dll). After the header comes the section information that shows which
sections the linker brought in from the various OBJ and LIB files.
Address Publics by
Value Rva+Base Lib:Object
0001:00000000
_DllMain@12
10001000 f App.obj
0001:00000010
_DialogCtrlEntry
10001010 f DialogCtrl.obj
0001:00000040
_DialogCtrlExit
10001040 f DialogCtrl.obj
0001:00000060 _NormalEntry 10001060 f DialogCtrl.obj
0001:00000080
_AbnormalEntry
10001080 f DialogCtrl.obj
0001:000000a0
_CallingEntry
100010a0 f DialogCtrl.obj
0001:00000110
_StopExit
10001110 f DialogCtrl.obj
0001:00000110
_PlayExit
10001110 f DialogCtrl.obj
…
0003:00000090 _ImageListApp
1000c090 ImageList.obj
0003:000000b0 _image_rc
1000c0b0 ImageList.obj
…
After the section information, you get the public function
information. Notice the "public" part. If you have static-declared C
functions, they won't show up in the map file. The important parts of the
public function information are the function names and the information in the Rva+Base column, which is
the starting address of the function.