views:

152

answers:

3

I have a couple of functions in a namespace called stub. I have to determine the exact start address of the namespace and the end address, of at least the size of the namespace in memory (to copy these functions into another process). While this worked perfectly in Visual C++ 2008 by adding a

void stub_end() { }

at the end of the namespace and using

size_t size = reinterpret_cast<ULONG_PTR>(stub_end) - reinterpret_cast<ULONG_PTR>(stub_start);

to determine the size of the stub.

This worked because Visual C++ preserved the function order as it is in the .cpp file, however that does not seem to be the case in Visual C++ 2010 anymore.

How can I find out the size of the functions or the whole namespace/stub by using pragma directives, compiler/linker facilities or similar?

+1  A: 

With the new push in security these days (heap randomization, layout randomization, etc..) I think this is going to be much more difficult. You may end up having to just copy each function individually.

joshperry
A: 

You can try and place each function in a different section, using the VC++ equivalent GCC's attribute ((section ("name"))) http://www.delorie.com/gnu/docs/gcc/gcc%5F62.html and then use your technique, or you could place each function in a different source file.

anijhaw
A: 

The C++ language provides no guarantees for finding addresses or sizes of namespaces. That said, venture into assembly language and linker instructions.

Many assembly languages have an opcode or mnemonic for placing code at specific addresses. This allows a label to be set up to indicate the start of a memory area. Some linkers have variables for obtaining segment starting addresses and sizes. These would be user defined logical addresses.

In summary, use your assembly and linker tools to define public symbols for the namespace start and length or optionally the end of the segment. In your C++ program, access these labels as extern.

Thomas Matthews
Can you give examples for the directives in Visual C++?
Patrick Daryll Glandien