|
-
October 10th, 2005, 09:33 AM
#1
getting code length and begin pointer
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?
-
October 10th, 2005, 10:27 AM
#2
Re: getting code length and begin pointer
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.
-
October 10th, 2005, 10:35 AM
#3
Re: getting code length and begin pointer
For memory location of a function, you could also simply use the function pointer as done in here:
Code:
#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.
Last edited by exterminator; October 10th, 2005 at 12:15 PM.
Reason: changed code a little bit
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 10th, 2005, 08:22 PM
#4
Re: getting code length and begin pointer
(explanations below)
Code:
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.
-
October 11th, 2005, 02:18 AM
#5
Re: getting code length and begin pointer
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?
-
October 11th, 2005, 06:56 AM
#6
Re: getting code length and begin pointer
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):
Code:
public StartOfMyCODE
.mycode segment
StartOfMyCODE label byte
.mycode ends
And another:
Code:
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.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
October 11th, 2005, 07:13 AM
#7
Re: getting code length and begin pointer
 Originally Posted by SuperKoko
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.
-
October 11th, 2005, 09:35 AM
#8
Re: getting code length and begin pointer
If there is a project file list, i suppose that linking is done in the order of the list.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|