www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - stack frame optimization problem

reply sprucely <timberdig gmail.com> writes:
This works with g++ and inline ATT assembly, but I have had no such luck in D.
I have many simple functions that need to be executed sequentially and have
identical stack frames. To avoid the overhead of setting up and tearing down
the stack frames I want to jmp from the body of one function to the body of the
next. A simplified example...

extern(C) byte jumpHere;

byte* jumpTo = &jumpHere;

void f1()
{
	asm
	{
		//jmp dword ptr jumpTo;
		mov EAX, jumpTo;
		jmp EAX;
		//jmp [EAX]
	}
}

void f2()
{
	asm{jumpHere:;}
}

No matter what I try I get a segfault. My assembly skills are very limited. I'm
not using the naked keyword yet, because I want to get a proof-of-concept
working first. Anyone see anything wrong with this? Any suggestions?
Oct 20 2009
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
sprucely:

This works with g++ and inline ATT assembly, but I have had no such luck in D.<
What compiler are you using? I think LDC isn't yet able to do this (it's LLVM limit, that may get lifted in future). Bye, bearophile
Oct 20 2009
parent sprucely <timberdig gmail.com> writes:
bearophile,

DMD 1.0.43 I think. But I'll have to check to make sure, because I was
experimenting with LDC at one point.

So does this mean there's nothing inherently wrong with my snippet?

My C++ code was also modifying the this pointer as it jumped from a member
function of one class to a member function of another. But I decided not to
even try that until I got the jumps working.

Thanks,
sprucely


bearophile Wrote:

 sprucely:
 
This works with g++ and inline ATT assembly, but I have had no such luck in D.<
What compiler are you using? I think LDC isn't yet able to do this (it's LLVM limit, that may get lifted in future). Bye, bearophile
Oct 20 2009
prev sibling next sibling parent reply "Vladimir Panteleev" <thecybershadow gmail.com> writes:
On Tue, 20 Oct 2009 18:45:50 +0300, sprucely <timberdig gmail.com> wrote:

 This works with g++ and inline ATT assembly, but I have had no such luck  
 in D. I have many simple functions that need to be executed sequentially  
 and have identical stack frames. To avoid the overhead of setting up and  
 tearing down the stack frames I want to jmp from the body of one  
 function to the body of the next. A simplified example...

 extern(C) byte jumpHere;

 byte* jumpTo = &jumpHere;

 void f1()
 {
 	asm
 	{
 		//jmp dword ptr jumpTo;
 		mov EAX, jumpTo;
 		jmp EAX;
 		//jmp [EAX]
 	}
 }

 void f2()
 {
 	asm{jumpHere:;}
 }

 No matter what I try I get a segfault. My assembly skills are very  
 limited. I'm not using the naked keyword yet, because I want to get a  
 proof-of-concept working first. Anyone see anything wrong with this? Any  
 suggestions?
Just disassemble the resulting machine code and look at what's going on. -- Best regards, Vladimir mailto:thecybershadow gmail.com
Oct 20 2009
parent reply sprucely <timberdig gmail.com> writes:
To try to be sure I had the correct syntax I tried the -S option of g++ along
with a switch for intel syntax to output the assembly. However the portion
corresponding to the inline assembly was still in ATT syntax.

For my resulting D executable I tried using hte, but it would abort after
mentioning something about a nonexistent htcfg file. I didn't find much info
after a cursory search. I gave up easily because I wasn't sure if I would be
able to make proper use of it. Maybe I should take an x86 assembly course.

Vladimir Panteleev Wrote:

 On Tue, 20 Oct 2009 18:45:50 +0300, sprucely <timberdig gmail.com> wrote:
 
 This works with g++ and inline ATT assembly, but I have had no such luck  
 in D. I have many simple functions that need to be executed sequentially  
 and have identical stack frames. To avoid the overhead of setting up and  
 tearing down the stack frames I want to jmp from the body of one  
 function to the body of the next. A simplified example...

 extern(C) byte jumpHere;

 byte* jumpTo = &jumpHere;

 void f1()
 {
 	asm
 	{
 		//jmp dword ptr jumpTo;
 		mov EAX, jumpTo;
 		jmp EAX;
 		//jmp [EAX]
 	}
 }

 void f2()
 {
 	asm{jumpHere:;}
 }

 No matter what I try I get a segfault. My assembly skills are very  
 limited. I'm not using the naked keyword yet, because I want to get a  
 proof-of-concept working first. Anyone see anything wrong with this? Any  
 suggestions?
Just disassemble the resulting machine code and look at what's going on. -- Best regards, Vladimir mailto:thecybershadow gmail.com
Oct 20 2009
next sibling parent downs <default_357-line yahoo.de> writes:
sprucely wrote:
 To try to be sure I had the correct syntax I tried the -S option of g++ along
with a switch for intel syntax to output the assembly. However the portion
corresponding to the inline assembly was still in ATT syntax.
 
 For my resulting D executable I tried using hte, but it would abort after
mentioning something about a nonexistent htcfg file. I didn't find much info
after a cursory search. I gave up easily because I wasn't sure if I would be
able to make proper use of it. Maybe I should take an x86 assembly course.
 
 Vladimir Panteleev Wrote:
 
 On Tue, 20 Oct 2009 18:45:50 +0300, sprucely <timberdig gmail.com> wrote:

 This works with g++ and inline ATT assembly, but I have had no such luck  
 in D. I have many simple functions that need to be executed sequentially  
 and have identical stack frames. To avoid the overhead of setting up and  
 tearing down the stack frames I want to jmp from the body of one  
 function to the body of the next. A simplified example...

 extern(C) byte jumpHere;

 byte* jumpTo = &jumpHere;

 void f1()
 {
 	asm
 	{
 		//jmp dword ptr jumpTo;
 		mov EAX, jumpTo;
 		jmp EAX;
 		//jmp [EAX]
 	}
 }

 void f2()
 {
 	asm{jumpHere:;}
 }

 No matter what I try I get a segfault. My assembly skills are very  
 limited. I'm not using the naked keyword yet, because I want to get a  
 proof-of-concept working first. Anyone see anything wrong with this? Any  
 suggestions?
Just disassemble the resulting machine code and look at what's going on. -- Best regards, Vladimir mailto:thecybershadow gmail.com
Try dropping an "int 3" before and after, then running it in gdb and using the "disassemble" and "info registers" commands.
Oct 20 2009
prev sibling parent "Vladimir Panteleev" <thecybershadow gmail.com> writes:
On Wed, 21 Oct 2009 00:55:26 +0300, sprucely <timberdig gmail.com> wrote:

 To try to be sure I had the correct syntax I tried the -S option of g++  
 along with a switch for intel syntax to output the assembly. However the  
 portion corresponding to the inline assembly was still in ATT syntax.

 For my resulting D executable I tried using hte, but it would abort  
 after mentioning something about a nonexistent htcfg file. I didn't find  
 much info after a cursory search. I gave up easily because I wasn't sure  
 if I would be able to make proper use of it. Maybe I should take an x86  
 assembly course.
I believe DMD comes with a Linux binary of obj2asm. For Windows you can use the free version of IDA. -- Best regards, Vladimir mailto:thecybershadow gmail.com
Oct 21 2009
prev sibling parent reply sprucely <timberdig gmail.com> writes:
Okay, between gdb and obj2asm I've been able to figure out that the address
placed in EAX in the following instructions...

		mov EAX, jumpTo;
		jmp EAX;

is actually the address of the initialization function for jumpTo... 

byte* jumpTo = &jumpHere;

which is named _D9conductor6jumpToPg. Execution then ends up segfaulting on a
(bad) instruction in function _moduleinfo_array.

So I'm not sure if the wrong address is being stored in jumpTo or if I'm simply
not correctly dereferencing it. Ultimately I need to know the offset from
function pointers to the point at which the prolog has completed and execution
of the main function body begins. I'm attempting to discover the offset by
using a dummy function containing a "jumpHere:" label and subtracting the
function address from the label address. Is there a better way to do this?

Thanks for the suggestions I've received. I'm learning a lot!


sprucely Wrote:

 This works with g++ and inline ATT assembly, but I have had no such luck in D.
I have many simple functions that need to be executed sequentially and have
identical stack frames. To avoid the overhead of setting up and tearing down
the stack frames I want to jmp from the body of one function to the body of the
next. A simplified example...
 
 extern(C) byte jumpHere;
 
 byte* jumpTo = &jumpHere;
 
 void f1()
 {
 	asm
 	{
 		//jmp dword ptr jumpTo;
 		mov EAX, jumpTo;
 		jmp EAX;
 		//jmp [EAX]
 	}
 }
 
 void f2()
 {
 	asm{jumpHere:;}
 }
 
 No matter what I try I get a segfault. My assembly skills are very limited.
I'm not using the naked keyword yet, because I want to get a proof-of-concept
working first. Anyone see anything wrong with this? Any suggestions?
Oct 23 2009
parent sprucely <timberdig gmail.com> writes:
I've been able to determine that the trick, extern(C) byte jumpHere, is not
providing the correct address. Since all my functions will have an identical
stack frame, it will be easy enough to just hard code the proper offset.

sprucely Wrote:

 Okay, between gdb and obj2asm I've been able to figure out that the address
placed in EAX in the following instructions...
 
 		mov EAX, jumpTo;
 		jmp EAX;
 
 is actually the address of the initialization function for jumpTo... 
 
 byte* jumpTo = &jumpHere;
 
 which is named _D9conductor6jumpToPg. Execution then ends up segfaulting on a
(bad) instruction in function _moduleinfo_array.
 
 So I'm not sure if the wrong address is being stored in jumpTo or if I'm
simply not correctly dereferencing it. Ultimately I need to know the offset
from function pointers to the point at which the prolog has completed and
execution of the main function body begins. I'm attempting to discover the
offset by using a dummy function containing a "jumpHere:" label and subtracting
the function address from the label address. Is there a better way to do this?
 
 Thanks for the suggestions I've received. I'm learning a lot!
 
 
 sprucely Wrote:
 
 This works with g++ and inline ATT assembly, but I have had no such luck in D.
I have many simple functions that need to be executed sequentially and have
identical stack frames. To avoid the overhead of setting up and tearing down
the stack frames I want to jmp from the body of one function to the body of the
next. A simplified example...
 
 extern(C) byte jumpHere;
 
 byte* jumpTo = &jumpHere;
 
 void f1()
 {
 	asm
 	{
 		//jmp dword ptr jumpTo;
 		mov EAX, jumpTo;
 		jmp EAX;
 		//jmp [EAX]
 	}
 }
 
 void f2()
 {
 	asm{jumpHere:;}
 }
 
 No matter what I try I get a segfault. My assembly skills are very limited.
I'm not using the naked keyword yet, because I want to get a proof-of-concept
working first. Anyone see anything wrong with this? Any suggestions?
Oct 24 2009