Calling a bare function from one to the other?
When I try to call the declspec (naked) function of another function, I get an error that according to the function prototype, it should return a value, but when I try to return a value, I get another error: says the naked function could not return a value ...
__declspec(naked) void bar() {
__asm {
nop
ret
}
}
__declspec(naked) NTSTATUS WINAPI foo(int a, int b) {
bar();
return NTSTATUS(1);
}
This is all reasonable because bare functions do not create a stack frame for a function, hence calling another function is an error unless the programmer explicitly creates a stack frame. However, when I try to create a stack frame and align the stacks correctly, I get an error.
How can I correctly call from a bare function to another?
source to share
Naked functions do not support return statements
The following rules and restrictions apply to the bare function:
- The return statement is not allowed.
you have to deal with the stack frame and the return itself, for example.
__declspec( naked ) void bar() {
__asm {
nop
ret
}
}
__declspec(naked) bool foo(int a, int b) {
bar();
__asm {
mov al,1
ret
}
}
int main() {
bool return_value = foo(2, 2);
std::cout << return_value; // 1
}
source to share
The bare function means the compiler is not going to write the prologue (setting esp and ebp) and epilogue (resetting esp and ebp) for you. Since the compiler does not write an epilogue, so you cannot ask it to return some value for you using the "return" keyword. // prologue
// simple prologue
push ebp
mov ebp, esp
// simple epilogue
mov eax, ret_var //by default eax holds return value
mov esp, ebp
pop ebp
source to share