1
votes

i got some simple code file

mangen.c:

///////////// begin of the file 
void mangen(int* data) 
{ 
  for(int j=0; j<100; j++) 
   for(int i=0; i<100; i++) 
      data[j*100+i] = 111; 
} 
//////// end of the file 

I compile it with mingw (on win32)

c:\mingw\bin\gcc -std=c99 -c mangen.c -fno-exceptions -march=core2 -mtune=generic -mfpmath=both -msse2

it yeilds to mangen.o file which is 400 bytes

00000000  4C 01 03 00 00 00 00 00-D8 00 00 00 0A 00 00 00  L............... 
00000010  00 00 05 01 2E 74 65 78-74 00 00 00 00 00 00 00  .....text....... 
00000020  00 00 00 00 4C 00 00 00-8C 00 00 00 00 00 00 00  ....L........... 
00000030  00 00 00 00 00 00 00 00-20 00 30 60 2E 64 61 74  ........ .0`.dat 
00000040  61 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  a............... 
00000050  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................ 
00000060  40 00 30 C0 2E 62 73 73-00 00 00 00 00 00 00 00  @.0..bss........ 
00000070  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................ 
00000080  00 00 00 00 00 00 00 00-80 00 30 C0 55 89 E5 83  ..........0.U... 
00000090  EC 10 C7 45 FC 00 00 00-00 EB 34 C7 45 F8 00 00  ...E......4.E... 
000000A0  00 00 EB 21 8B 45 FC 6B-D0 64 8B 45 F8 01 D0 8D  ...!.E.k.d.E.... 
000000B0  14 85 00 00 00 00 8B 45-08 01 D0 C7 00 6F 00 00  .......E.....o.. 
000000C0  00 83 45 F8 01 83 7D F8-63 7E D9 83 45 FC 01 83  ..E...}.c~..E... 
000000D0  7D FC 63 7E C6 C9 C3 90-2E 66 69 6C 65 00 00 00  }.c~.....file... 
000000E0  00 00 00 00 FE FF 00 00-67 01 6D 61 6E 67 65 6E  ........g.mangen 
000000F0  2E 63 00 00 00 00 00 00-00 00 00 00 5F 6D 61 6E  .c.........._man 
00000100  67 65 6E 00 00 00 00 00-01 00 20 00 02 01 00 00  gen....... ..... 
00000110  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00  ................ 
00000120  2E 74 65 78 74 00 00 00-00 00 00 00 01 00 00 00  .text........... 
00000130  03 01 4B 00 00 00 00 00-00 00 00 00 00 00 00 00  ..K............. 
00000140  00 00 00 00 2E 64 61 74-61 00 00 00 00 00 00 00  .....data....... 
00000150  02 00 00 00 03 01 00 00-00 00 00 00 00 00 00 00  ................ 
00000160  00 00 00 00 00 00 00 00-2E 62 73 73 00 00 00 00  .........bss.... 
00000170  00 00 00 00 03 00 00 00-03 01 00 00 00 00 00 00  ................ 
00000180  00 00 00 00 00 00 00 00-00 00 00 00 04 00 00 00  ................ 

Now I need to know where is the binary chunk containing above function body in here

Could someone provide some simple code that will allow me to retrive this boundaries ?

(assume that function body may be shorter or longer and also there may be other functions or data in source fite added so it will move in chunk but I suspect procedure to localise it should be not very complex.

3
you can use objdump -t <binary file name> to print out the symbol table which contains the address of the function within your binary. - SSC
This is why I like midnight commander. Along with many other things, it even has this built-in for F3 view (well, it is in a config file, but it comes appropriately prepared). - Jubatian
tnx for hint, but i need it as recipe from the c code to work in general, few lines in code to find a procedure body chunk (for any given symbol -procedure name preferebly) - user2214913

3 Answers

2
votes

You can use objdump -Fd mangen.o to find out file offset and lenght of a function.
Alternatively, you can use readelf -s mangen.o to find out size of a function.

1
votes

You may define something like int abc = 0x11223344; in the beginning and end of function and use the constants to locate the function body.

1
votes

You can use objdump or nm.

For instance, try:

 nm mangen.o

Or

 objdump -t mangen.o

If you need to use your own code, have a look here:

http://www.rohitab.com/discuss/topic/38591-c-import-table-parser/

It will give you something to start with. You can find much more information about the format in MSDN.

If you are into Python, there is nice tool/library (including source code) that can be helpful:

https://code.google.com/p/pefile/