CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2005
    Posts
    8

    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?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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

  4. #4
    Join Date
    Jul 2005
    Location
    Krosno, Poland
    Posts
    51

    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.

  5. #5
    Join Date
    Feb 2005
    Posts
    8

    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?

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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()!

  7. #7
    Join Date
    Feb 2005
    Posts
    8

    Re: getting code length and begin pointer

    Quote 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.

  8. #8
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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
  •  





Click Here to Expand Forum to Full Width

Featured