digitalmars.D - stack frame optimization problem
- sprucely (18/18) Oct 20 2009 This works with g++ and inline ATT assembly, but I have had no such luck...
- bearophile (4/5) Oct 20 2009 What compiler are you using? I think LDC isn't yet able to do this (it's...
- sprucely (7/15) Oct 20 2009 bearophile,
- Vladimir Panteleev (5/30) Oct 20 2009 Just disassemble the resulting machine code and look at what's going on.
- sprucely (3/41) Oct 20 2009 To try to be sure I had the correct syntax I tried the -S option of g++ ...
- downs (2/46) Oct 20 2009 Try dropping an "int 3" before and after, then running it in gdb and usi...
- Vladimir Panteleev (6/14) Oct 21 2009 I believe DMD comes with a Linux binary of obj2asm. For Windows you can ...
- sprucely (9/32) Oct 23 2009 Okay, between gdb and obj2asm I've been able to figure out that the addr...
- sprucely (2/44) Oct 24 2009
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
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
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
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
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
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:Try dropping an "int 3" before and after, then running it in gdb and using the "disassemble" and "info registers" commands.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
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
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
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