Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ set(BOX64_ROOT ${CMAKE_SOURCE_DIR})
add_definitions(-std=gnu11 -funwind-tables -fvisibility=hidden)
add_definitions(-D_GNU_SOURCE=1)
add_definitions(-D_FILE_OFFSET_BITS=64)
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)

if(USE_CCACHE)
find_program(CCACHE_FOUND ccache)
Expand Down Expand Up @@ -418,6 +419,193 @@ add_custom_target(generate_dynacache_hashes
VERBATIM
)

include(CheckIncludeFile)

set(PROJECT_HEADERS

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe all those checks should go in another file and just include it here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

second this. also is it necessary to enable these checks for non-musl targets?

execinfo.h
)

foreach(header IN LISTS PROJECT_HEADERS)
string(MAKE_C_IDENTIFIER "${header}" header_ident)
string(TOUPPER "${header_ident}" header_ident)

set(var "HAVE_${header_ident}")

check_include_file("${header}" "${var}")

if(${var})
add_compile_definitions("${var}=1")
endif()
endforeach()

if(HAVE_EXECINFO_H AND NOT ANDROID)
add_compile_definitions(USE_BACKTRACE)
endif()

include(CheckSymbolExists)

set(PROJECT_SYMBOL_CHECKS
mallopt malloc.h
malloc_trim malloc.h
mallinfo malloc.h
mallinfo2 malloc.h
_pthread_cleanup_push pthread.h
_pthread_cleanup_pop pthread.h
pthread_attr_getaffinity_np pthread.h
pthread_attr_setaffinity_np pthread.h
pthread_mutexattr_setprioceiling pthread.h
dlvsym dlfcn.h
obstack_vprintf stdio.h
getprotobyname_r netdb.h
getprotobynumber_r netdb.h
scandirat dirent.h
)

list(LENGTH PROJECT_SYMBOL_CHECKS checks_len)
math(EXPR checks_last "${checks_len} - 1")

foreach(i RANGE 0 ${checks_last} 2)
math(EXPR j "${i} + 1")

list(GET PROJECT_SYMBOL_CHECKS ${i} symbol)
list(GET PROJECT_SYMBOL_CHECKS ${j} header)

string(MAKE_C_IDENTIFIER "${symbol}" symbol_ident)
string(TOUPPER "${symbol_ident}" symbol_ident)

set(var "HAVE_${symbol_ident}")

set(headers "${header}")
check_symbol_exists("${symbol}" "${headers}" "${var}")

if(${var})
add_compile_definitions("${var}=1")
endif()
endforeach()

include(CheckFunctionExists)

set(PROJECT_FUNCTION_CHECKS
__chk_fail
)

foreach(function IN LISTS PROJECT_FUNCTION_CHECKS)
string(MAKE_C_IDENTIFIER "${function}" function_ident)
string(TOUPPER "${function_ident}" function_ident)

set(var "HAVE_${function_ident}")

check_function_exists("${function}" "${var}")

if(${var})
add_compile_definitions("${var}=1")
endif()
endforeach()

include(CheckCSourceCompiles)

check_c_source_compiles("#include <obstack.h>
int main(void){ struct obstack o = { .temp.tempptr = NULL }; return 0; }" OBSTACK_TEMP_TEMPPTR)
check_c_source_compiles("#include <obstack.h>
int main(void){ struct obstack o = { .temp.p = NULL }; return 0; }" OBSTACK_TEMP_P)

if(OBSTACK_TEMP_TEMPPTR) # glibc
add_compile_definitions("STRUCT_OBSTACK_TEMP=tempptr")
add_compile_definitions("STRUCT_OBSTACK_CHUNKFUN=chunkfun")
add_compile_definitions("STRUCT_OBSTACK_FREEFUN=freefun")
elseif(OBSTACK_TEMP_P) # musl-obstack
add_compile_definitions("STRUCT_OBSTACK_TEMP=p")
add_compile_definitions("STRUCT_OBSTACK_CHUNKFUN=chunkfun.extra")
add_compile_definitions("STRUCT_OBSTACK_FREEFUN=freefun.extra")
elseif(NOT ANDROID)
message(FATAL_ERROR "Unknown struct obstack layout!")
endif()

check_c_source_compiles("#include <signal.h>
int main(void){ siginfo_t s = { ._sifields = { 0 } }; return 0; }" SIGINFO_S__SIFIELDS)
check_c_source_compiles("#include <signal.h>
int main(void){ siginfo_t s = { .__si_fields = { 0 } }; return 0; }" SIGINFO_S___SI_FIELDS)

if(SIGINFO_S__SIFIELDS) # glibc
add_compile_definitions("SIGINFO_S_SIFIELDS=_sifields")
elseif(SIGINFO_S___SI_FIELDS) # musl
add_compile_definitions("SIGINFO_S_SIFIELDS=__si_fields")
elseif(NOT ANDROID)
message(FATAL_ERROR "Unknown siginfo_t layout!")
endif()

check_c_source_compiles("#include <resolv.h>
int main(void){ struct __res_state r = { .__glibc_unused_qhook = NULL }; return 0; }" RES_STATE_GLIBC_UNUSED_QHOOK)
check_c_source_compiles("#include <resolv.h>
int main(void){ struct __res_state r = { .qhook = NULL }; return 0; }" RES_STATE_QHOOK)

if(RES_STATE_GLIBC_UNUSED_QHOOK) # glibc
add_compile_definitions("RES_STATE_QHOOK=__glibc_unused_qhook")
add_compile_definitions("RES_STATE_RHOOK=__glibc_unused_rhook")
elseif(RES_STATE_QHOOK) # musl
add_compile_definitions("RES_STATE_QHOOK=qhook")
add_compile_definitions("RES_STATE_RHOOK=rhook")
elseif(NOT ANDROID)
message(FATAL_ERROR "Unknown res_state layout!")
endif()

check_c_source_compiles("#include <ctype.h>
int main(void){ return &__ctype_b_loc == 0; }" CTYPE_B_LOC)
check_c_source_compiles("#include <ctype.h>
extern const unsigned short int **__ctype_b_loc(void);
int main(void){ __ctype_b_loc(); return 0; }" CTYPE_B_LOC_EXT)

if(CTYPE_B_LOC) # glibc
add_compile_definitions("HAVE_CTYPE_B_LOC")
elseif(CTYPE_B_LOC_EXT) # musl
add_compile_definitions("HAVE_CTYPE_B_LOC")
add_compile_definitions("HAVE_CTYPE_B_LOC_EXT")
endif()

check_c_source_compiles("#include <glob.h>
int main(void){ glob_t g = { .gl_flags = 0 }; return 0; }" GLOB_T_GL_FLAGS)
check_c_source_compiles("#include <glob.h>
int main(void){ glob_t g = { .__dummy1 = 0 }; return 0; }" GLOB_T_DUMMY)

if(GLOB_T_GL_FLAGS) # glibc
add_compile_definitions("GLOB_T_GL_FLAGS=gl_flags")
add_compile_definitions("GLOB_T_GL_CLOSEDIR=gl_closedir")
add_compile_definitions("GLOB_T_GL_READDIR=gl_readdir")
add_compile_definitions("GLOB_T_GL_OPENDIR=gl_opendir")
add_compile_definitions("GLOB_T_GL_LSTAT=gl_lstat")
add_compile_definitions("GLOB_T_GL_STAT=gl_stat")
elseif(GLOB_T_DUMMY) # musl
add_compile_definitions("GLOB_T_GL_FLAGS=__dummy1")
add_compile_definitions("GLOB_T_GL_CLOSEDIR=__dummy2[0]")
add_compile_definitions("GLOB_T_GL_READDIR=__dummy2[1]")
add_compile_definitions("GLOB_T_GL_OPENDIR=__dummy2[2]")
add_compile_definitions("GLOB_T_GL_LSTAT=__dummy2[3]")
add_compile_definitions("GLOB_T_GL_STAT=__dummy2[4]")
elseif(NOT ANDROID)
message(FATAL_ERROR "Unknown glob_t layout!")
endif()

check_c_source_compiles("#include <spawn.h>
int main(void){ posix_spawn_file_actions_t a = { .__allocated = 0 }; return 0; }"
POSIX_SPAWN_FILE_ACTIONS_T__ALLOCATED)
check_c_source_compiles("#include <spawn.h>
int main(void){ posix_spawn_file_actions_t a = { .__pad0 = {0} }; return 0; }"
POSIX_SPAWN_FILE_ACTIONS_T__PAD0)

if(POSIX_SPAWN_FILE_ACTIONS_T__ALLOCATED) # glibc
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__ALLOCATED=__allocated")
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__USED=__used")
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__ACTIONS=__actions")
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__PAD=__pad")
elseif(POSIX_SPAWN_FILE_ACTIONS_T__PAD0) # musl
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__ALLOCATED=__pad0[0]")
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__USED=__pad0[1]")
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__ACTIONS=__actions")
add_compile_definitions("POSIX_SPAWN_FILE_ACTIONS_T__PAD=__pad")
elseif(NOT ANDROID)
message(FATAL_ERROR "Unknown posix_spawn_file_actions_t layout!")
endif()

# generated files
set_source_files_properties(
"${BOX64_ROOT}/src/git_head.h"
Expand Down Expand Up @@ -477,6 +665,7 @@ set(ELFLOADER_SRC
"${BOX64_ROOT}/src/libtools/syscall_user_dispatch.c"
"${BOX64_ROOT}/src/libtools/decopcode.c"
"${BOX64_ROOT}/src/libtools/threads.c"
"${BOX64_ROOT}/src/libtools/libc.c"
"${BOX64_ROOT}/src/steamwebhelper.c"
"${BOX64_ROOT}/src/tools/bitutils.c"
"${BOX64_ROOT}/src/tools/box64stack.c"
Expand Down
14 changes: 14 additions & 0 deletions src/include/libc_ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
#ifndef _BOX64_LIBC_CTYPE_H_
#define _BOX64_LIBC_CTYPE_H_

#ifdef HAVE_CTYPE_B_LOC_EXT

#include <stdint.h>

const unsigned short **__ctype_b_loc(void);
const int32_t **__ctype_tolower_loc(void);
const int32_t **__ctype_toupper_loc(void);
#endif

#endif
10 changes: 10 additions & 0 deletions src/include/libc_dirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
#ifndef _BOX64_LIBC_DIRENT_H_
#define _BOX64_LIBC_DIRENT_H_

#ifndef HAVE_SCANDIRAT
int scandirat(int dirfd, const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),int (*compar)(const struct dirent**, const struct dirent **));
#endif

#endif
13 changes: 13 additions & 0 deletions src/include/libc_glob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
#ifndef _BOX64_LIBC_GLOB_H_
#define _BOX64_LIBC_GLOB_H_

#ifndef GLOB_APPEND
#define GLOB_APPEND (1 << 5)
#endif

#ifndef GLOB_ALTDIRFUNC
# define GLOB_ALTDIRFUNC (1 << 9)
#endif

#endif
35 changes: 35 additions & 0 deletions src/include/libc_malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
#ifndef _BOX64_LIBC_MALLOC_H_
#define _BOX64_LIBC_MALLOC_H_

#ifndef HAVE_MALLINFO
struct mallinfo {
int arena;
int ordblks;
int smblks;
int hblks;
int hblkhd;
int usmblks;
int fsmblks;
int uordblks;
int fordblks;
int keepcost;
};
#endif

#ifndef HAVE_MALLINFO2
struct mallinfo2 {
size_t arena;
size_t ordblks;
size_t smblks;
size_t hblks;
size_t hblkhd;
size_t usmblks;
size_t fsmblks;
size_t uordblks;
size_t fordblks;
size_t keepcost;
};
#endif

#endif
13 changes: 13 additions & 0 deletions src/include/libc_netdb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
#ifndef _BOX64_LIBC_NETDB_H_
#define _BOX64_LIBC_NETDB_H_

#ifndef HAVE_GETPROTOBYNAME_R
int getprotobyname_r(const char *name, struct protoent *result_buf, char *buf, size_t buflen, struct protoent **result);
#endif

#ifndef HAVE_GETPROTOBYNUMBER_R
int getprotobynumber_r(int proto, struct protoent *result_buf, char *buf, size_t buflen, struct protoent **result);
#endif

#endif
9 changes: 9 additions & 0 deletions src/include/libc_obstack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
#ifndef _BOX64_LIBC_OBSTACK_H_
#define _BOX64_LIBC_OBSTACK_H_

#ifndef HAVE_OBSTACK_VPRINTF
int obstack_vprintf(struct obstack *obstack, const char *fmt, va_list args);
#endif

#endif
13 changes: 13 additions & 0 deletions src/include/libc_select.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
#ifndef _BOX64_LIBC_SELECT_H_
#define _BOX64_LIBC_SELECT_H_

#ifndef __NFDBITS
#ifdef NFDBITS
#define __NFDBITS NFDBITS
#else
#error NFDBITS not defined by libc
#endif
#endif

#endif
14 changes: 14 additions & 0 deletions src/include/mydlfcn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
#ifndef __MYDLFCN_H_
#define __MYDLFCN_H_

#include <dlfcn.h>

#ifndef RTLD_DL_SYMENT
#define RTLD_DL_SYMENT 1
#endif
#ifndef RTLD_DL_LINKMAP
#define RTLD_DL_LINKMAP 2
#endif

#endif // __MYDLFCN_H_
4 changes: 4 additions & 0 deletions src/include/signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "x64_signals.h"
#include "box64context.h"

#ifndef __SI_SIGFAULT_ADDL
#define __SI_SIGFAULT_ADDL
#endif

typedef void (*sighandler_t)(int);

#ifdef ANDROID
Expand Down
Loading
Loading