Bizarre GCC Optimization

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
AR

Bizarre GCC Optimization

Post by AR »

I've written an entry point for call gates but every time I run it, Bochs gives me some rubbish about there not being an LDT, I traced the cause back to the stack being messed up by the handler:

Code: Select all

/* Entry point: */
.extern SysCallEntry
.globl x86CallGateEntry
x86CallGateEntry:
   pusha
   pushl %ds
   pushl %es
   
   movl %ss, %eax
   movl %eax, %ds
   movl %eax, %es
   
   call SysCallEntry
   
   popl %es
   popl %ds
   popa
   iret

/* Cpp Function: */
ASMLINKABLE void x86::SysCall::SysCallEntry(unsigned int ES, unsigned int DS, x86::ISRs::GeneralRegisters r, unsigned int EIP, unsigned int CS, unsigned int Flags)
{
   Display::printf("Unimplemented...\n");
}
The Cpp code is built using -O2 -fomit-frame-pointer and generates this:

Code: Select all

movl   $0xc000325f,%eax
>> movl   %eax,0x4(%esp,1) <<
jmp    c0001680
The problem line is marked, it "pushes" a value without bothering to move ESP which corrupts the value from "push %es" causing a crash when it's poped. The problem only goes away if I manually manipulate the stack, add dummy code (i += i), or set optimizations as none, this is just weird...

Does anyone know the convention for optimized calls?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Bizarre GCC Optimization

Post by Pype.Clicker »

hey, who has ever told you the content of arguments will be preserved on the stack ?

I mean i can write a function like

Code: Select all

void foo(int bar)
{
   while (bar<5) bar++;
}
That's the reason why the stack is discarded with something like [tt]add esp,<cste>[/tt] rather than popped back after a C function has been called.

That being said, that "mov [esp+4],0xc000325f" thing still sounds weird to me. isn't [esp+4] supposed to be the return address ? why is the gcc-generated code toying with that ?
User avatar
Solar
Member
Member
Posts: 7635
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Bizarre GCC Optimization

Post by Solar »

Just a wild guess: Exception handling? (As I don't see any options to the contrary...)
Every good solution is obvious once you've found it.
AR

Re:Bizarre GCC Optimization

Post by AR »

I forgot about that, well that is just annoying so I'll have to use every parameter meaning it will go away when I code the actual handling stuff but the i+=i hack will be sufficent for testing anyway.
isn't [esp+4] supposed to be the return address ? why is the gcc-generated code toying with that ?
The stack is usually:
ESP+8 = Parameter
ESP+4 = return address
ESP = Old EBP
With framepointers disabled and code optimizations however, the EBP isn't pushed so the return address ends up directly at ESP.
wacco

Re:Bizarre GCC Optimization

Post by wacco »

Pype.Clicker wrote: That being said, that "mov [esp+4],0xc000325f" thing still sounds weird to me. isn't [esp+4] supposed to be the return address ? why is the gcc-generated code toying with that ?
Sounds like a good optimalisation to me, the last thing to do in the function is calling printf, and that address is known. So instead of calling printf, then returning to this function, and then returning to whoever called this in the first place is kind of stupid. It would be faster to just edit the return address, call printf, and when printf returns we 'skip' this function.

Atleast, that's what I think is happening. There might be a bit more to that, since it's a static value that's placed on the stack and the 'function that called us in the first place' isn't known (unless it's only called from /one/ other location?)
To figure that out, we need more disassembled code ;)

HtH & HH
AR

Re:Bizarre GCC Optimization

Post by AR »

The problem is resolved already, GCC optimised by overriding the stack parameters since they weren't used in the function (most annoying in this case but would be good otherwise...).
Sounds like a good optimalisation to me, the last thing to do in the function is calling printf, and that address is known. So instead of calling printf, then returning to this function, and then returning to whoever called this in the first place is kind of stupid. It would be faster to just edit the return address, call printf, and when printf returns we 'skip' this function.
See my above post. The return address points to after the CALL instruction in the assembly handler, it does not need to be manipulated, it is simply jumping to printf instead of calling so the when printf executes RET, it'll return directly to the assembly function. The MOV is actually moving the pointer to the string being printed to [ESP+4] as a parameter for printf.
wacco

Re:Bizarre GCC Optimization

Post by wacco »

Argh I really need to learn how to read... :-X

But now I'd really like to know how you fixed it. No longer the O2 flag, or did you change your syscall entry? Hope you didn't add some dummy code, because that sounds more like an ugly hack then a proper solution...
AR

Re:Bizarre GCC Optimization

Post by AR »

From Pype's post I've determined that if I actually use the parameters then GCC will stop overriding them, to resolve it I've placed some dummy code but when I code the switch() statement to actually do the syscall I can remove the dummy code.
proxy

Re:Bizarre GCC Optimization

Post by proxy »

best way to force gcc to think a param is used is to do this:

Code: Select all

int f(int arg) {
    (void)arg;
    return 0;
}
or better yet, i tend to make an "UNUSED" macro like this:

Code: Select all

#define UNUSED(x) ((void)(x))
int f(int arg) {
    UNUSED(arg);
    return 0;
}
proxy
Post Reply