Fix C++ test crash because of misaligned stack

This commit is contained in:
Zeex 2018-09-07 18:53:56 +06:00
parent f348fb52a8
commit 3a61f8f270
7 changed files with 20 additions and 15 deletions

View File

@ -24,8 +24,7 @@ else()
endif()
set(options "-f" "${asm_format}")
if(APPLE
OR (WIN32 AND (CMAKE_SIZEOF_VOID_P EQUAL 4 OR SUBHOOK_FORCE_32BIT)))
if(APPLE OR (WIN32 AND (CMAKE_SIZEOF_VOID_P EQUAL 4 OR SUBHOOK_FORCE_32BIT)))
list(APPEND options "--prefix=_")
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")

5
tests/foo.cpp Normal file
View File

@ -0,0 +1,5 @@
#include <stdio.h>
extern "C" void foo() {
puts("foo() called");
}

View File

@ -1,7 +0,0 @@
#!/bin/sh
yasm -f elf64 foo_64_unix.asm
gcc -c foo_main.c -o foo_main.o
gcc foo_64_unix.o foo_main.o -o foo
./foo

View File

@ -1,7 +1,7 @@
extern puts
global foo
section .data
section .rodata
message:
db 'foo() called', 0
@ -21,11 +21,11 @@ section .text
%define nop9 db 0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 ; 66 NOP DWORD ptr [EAX + EAX*1 + 00000000H]
foo:
nop
push ebp
mov ebp, esp
sub esp, 4 ; align the stack to a 16-byte boundary
push message
call puts
add esp, 4
mov esp, ebp
pop ebp
xor eax, eax
ret

6
tests/foo_main.cpp Normal file
View File

@ -0,0 +1,6 @@
extern "C" void foo(void);
int main() {
foo();
return 0;
}

View File

@ -11,7 +11,8 @@ typedef void (*foo_func_t)(void);
#elif defined SUBHOOK_UNIX
#define FOO_CALL __attribute__((cdecl))
#endif
#else
#endif
#ifndef FOO_CALL
#define FOO_CALL
#endif

View File

@ -9,7 +9,8 @@ typedef void (*foo_func_t)();
#elif defined SUBHOOK_UNIX
#define FOO_CALL __attribute__((cdecl))
#endif
#else
#endif
#ifndef FOO_CALL
#define FOO_CALL
#endif