Re: stack frame optimization problem

2009-10-24 Thread sprucely
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?
> 



Re: stack frame optimization problem

2009-10-23 Thread sprucely
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?



Re: stack frame optimization problem

2009-10-21 Thread Vladimir Panteleev

On Wed, 21 Oct 2009 00:55:26 +0300, 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.


I believe DMD comes with a Linux binary of obj2asm. For Windows you can  
use the free version of IDA.


--
Best regards,
 Vladimir  mailto:thecybersha...@gmail.com


Re: stack frame optimization problem

2009-10-20 Thread downs
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  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:thecybersha...@gmail.com
> 

Try dropping an "int 3" before and after, then running it in gdb and using the 
"disassemble" and "info registers" commands.


Re: stack frame optimization problem

2009-10-20 Thread sprucely
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  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:thecybersha...@gmail.com



Re: stack frame optimization problem

2009-10-20 Thread sprucely
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



Re: stack frame optimization problem

2009-10-20 Thread Vladimir Panteleev

On Tue, 20 Oct 2009 18:45:50 +0300, 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?


Just disassemble the resulting machine code and look at what's going on.

--
Best regards,
 Vladimir  mailto:thecybersha...@gmail.com


Re: stack frame optimization problem

2009-10-20 Thread bearophile
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


stack frame optimization problem

2009-10-20 Thread sprucely
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?