Click to See Complete Forum and Search --> : getting code length and begin pointer
McGreg
October 10th, 2005, 09:33 AM
I got some functions defined like this:
#pragma code_seg(push, r1, ".mycode")
void function1(....)
{
...
}
void function2 .....
#pragma code_seg(push, r1, ".mycode")
i want to know where in the memory .mycode begins, and where it ends/how long it is. how can i get that information from?
some kind of pointer and sizeof to the r1 identifier?
cilu
October 10th, 2005, 10:27 AM
What you can do is go to Project, Settings, C/C++, File Listing Category and select to output the assembly files. That way you can see the generated code for the above function. As for the memory location of your function, you can use GetProcAddress if this a function exported by a DLL.
exterminator
October 10th, 2005, 10:35 AM
For memory location of a function, you could also simply use the function pointer as done in here:#include <iostream>
void myfunc(){
//do something
}
int main(){
void (*fptr)() = NULL; //function pointer variable fptr for a function taking nothing and returning nothing
fptr = myfunc; //let fptr point to myfunc
std::cout << fptr << std::endl; //print the address as in fptr
return 0;
}Regards.
ikk
October 10th, 2005, 08:22 PM
(explanations below)
DWORD dwEnd = 0, dwFuncEnd = 0, dwBegin = 0;
VOID func1()
{
__asm{
lbl1:
mov eax, lbl1 //EIP = 0x00401B28 (Line #1)
mov dwBegin, eax
}
// <--- this position ll be recieved as begin into dwBegin
DWORD blabla = 1; //EIP = 0x00401B32 (Line #2)
DWORD bleh;
// ...
bleh = 0xBADC0DE;
// <--- this position ll be recieved as end into dwEnd
__asm{
lbl2:
mov eax, lbl2
mov dwEnd, eax
}
}
VOID funcHi()
{
func1();
// add size of what is betwen Line #1 and Line #2
dwBegin += 0xA;
// copute end of function by adding address of lbl2 plus size of
// these two instructions, we know this is same as above, that is 0xA
dwFuncEnd = dwEnd + 0xA;
char bfr1k[1024];
wsprintf( bfr1k,
"func1=0x%X, dwBegin=0x%X dwEnd=0x%X, dwFuncEnd=0x%X",
func1, dwBegin, dwEnd, dwFuncEnd );
MessageBox( 0, bfr1k, "", 0 );
}
u can use assembler instructions. by taking value of labels im copying
address of insstruction that follows that label.
but i cant take value of label which is below particular instruction that why i cant place label which directly points to line where begins C-code (ie. line: 'blabla = 1').
but i can get size of particular instructions by entering debugger and writting down value of EIP register, in this example, at Line #1 and at Line #2, later by taking difference i know that size of those two assembly instructions is 0xA.
At the end i have:
dwBegin - address of line 'DWORD blabla = 1;',
dwEnd - address of line 'lbl2:', whitch is address just after 'bleh = 0xBADC0DE;' line,
dwFuncEnd - points at end of func1() function.
all displayed in messagebox.
McGreg
October 11th, 2005, 02:18 AM
thank you a lot for the answers, but i think i didnt explain well.
i have a lot of functions in the code_seg, and i want to know the size of the whole code_seg, not of every function.
is there a possibility to do this?
SuperKoko
October 11th, 2005, 06:56 AM
You can define all these functions in one (or more) modules.
The, you can define an asm file containing something like that (i am not sure of the exact code):
public StartOfMyCODE
.mycode segment
StartOfMyCODE label byte
.mycode ends
And another:
public EndOfMyCODE
.mycode segment
EndOfMyCODE label byte
.mycode ends
And, then you must link your project by specifying these two modules respecitvely before, and after the C++ modules containing the code put in the .mycode section.
And, then you can import the two labels in the C++ program and compute the difference of the labels to get the size of the section.
McGreg
October 11th, 2005, 07:13 AM
And, then you can import the two labels in the C++ program and compute the difference of the labels to get the size of the section.
That was an idea i has before. But can i specify the link order in Visual studio?
I can't find such settings.
SuperKoko
October 11th, 2005, 09:35 AM
If there is a project file list, i suppose that linking is done in the order of the list.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.