Skip to content

Commit

Permalink
Merge pull request #2060 from Sonicadvance1/clang_thunks
Browse files Browse the repository at this point in the history
Thunks: Add support for building with clang
  • Loading branch information
Sonicadvance1 committed Oct 10, 2022
2 parents eaddf7f + b75e8f2 commit 2b1ef97
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ CHECK_INCLUDE_FILES ("gdb/jit-reader.h" HAVE_GDB_JIT_READER_H)
option(BUILD_TESTS "Build unit tests to ensure sanity" TRUE)
option(BUILD_FEX_LINUX_TESTS "Build FEXLinuxTests, requires x86 compiler" FALSE)
option(BUILD_THUNKS "Build thunks" FALSE)
option(BUILD_CLANG_THUNKS "Build thunks with clang" FALSE)
option(ENABLE_CLANG_FORMAT "Run clang format over the source" FALSE)
option(ENABLE_IWYU "Enables include what you use program" FALSE)
option(ENABLE_LTO "Enable LTO with compilation" TRUE)
Expand Down Expand Up @@ -415,6 +416,7 @@ if (BUILD_THUNKS)
CMAKE_ARGS
"-DBITNESS=64"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DENABLE_CLANG_THUNKS=${ENABLE_CLANG_THUNKS}"
"-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${X86_64_TOOLCHAIN_FILE}"
"-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}"
"-DSTRUCT_VERIFIER=${CMAKE_SOURCE_DIR}/Scripts/StructPackVerifier.py"
Expand All @@ -432,6 +434,7 @@ if (BUILD_THUNKS)
CMAKE_ARGS
"-DBITNESS=32"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DENABLE_CLANG_THUNKS=${ENABLE_CLANG_THUNKS}"
"-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${X86_32_TOOLCHAIN_FILE}"
"-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}"
"-DSTRUCT_VERIFIER=${CMAKE_SOURCE_DIR}/Scripts/StructPackVerifier.py"
Expand Down
6 changes: 6 additions & 0 deletions ThunkLibs/GuestLibs/CMakeLists.txt
Expand Up @@ -2,6 +2,12 @@ cmake_minimum_required(VERSION 3.14)
project(guest-thunks)

option(BITNESS "Which bitness the thunks are building for" 64)
option(ENABLE_CLANG_THUNKS "Enable building thunks with clang" FALSE)

if (ENABLE_CLANG_THUNKS)
set (LD_OVERRIDE "-fuse-ld=lld")
add_link_options(${LD_OVERRIDE})
endif()

if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# We've been included using ExternalProject_add, so set up the actual thunk libraries to be cross-compiled
Expand Down
6 changes: 6 additions & 0 deletions ThunkLibs/HostLibs/CMakeLists.txt
Expand Up @@ -3,6 +3,12 @@ project(host-thunks)

set(CMAKE_CXX_STANDARD 17)
set (HOSTLIBS_DATA_DIRECTORY "${CMAKE_INSTALL_PREFIX}/lib/fex-emu" CACHE PATH "global data directory")
option(ENABLE_CLANG_THUNKS "Enable building thunks with clang" FALSE)

if (ENABLE_CLANG_THUNKS)
set (LD_OVERRIDE "-fuse-ld=lld")
add_link_options(${LD_OVERRIDE})
endif()

# Syntax: generate(libxyz libxyz-interface.cpp)
# This defines two targets and a custom command:
Expand Down
4 changes: 4 additions & 0 deletions ThunkLibs/include/common/Guest.h
Expand Up @@ -7,8 +7,12 @@
#if __SIZEOF_POINTER__ == 8
#define THUNK_ABI
#else
#ifdef __clang__
#define THUNK_ABI __fastcall
#else
#define THUNK_ABI [[gnu::fastcall]]
#endif
#endif

template<typename signature>
THUNK_ABI
Expand Down
6 changes: 3 additions & 3 deletions ThunkLibs/libVDSO/libVDSO_Guest.lds
Expand Up @@ -29,9 +29,9 @@ SECTIONS {
}

PHDRS {
text PT_LOAD FLAGS(PF_R | PF_X) FILEHDR PHDRS;
dynamic PT_DYNAMIC FLAGS(PF_R);
note PT_NOTE FLAGS(PF_R);
text PT_LOAD FLAGS(4 | 1) FILEHDR PHDRS;
dynamic PT_DYNAMIC FLAGS(4);
note PT_NOTE FLAGS(4);
}

VERSION {
Expand Down
6 changes: 3 additions & 3 deletions ThunkLibs/libVDSO/libVDSO_Guest_32.lds
Expand Up @@ -29,9 +29,9 @@ SECTIONS {
}

PHDRS {
text PT_LOAD FLAGS(PF_R | PF_X) FILEHDR PHDRS;
dynamic PT_DYNAMIC FLAGS(PF_R);
note PT_NOTE FLAGS(PF_R);
text PT_LOAD FLAGS(4 | 1) FILEHDR PHDRS;
dynamic PT_DYNAMIC FLAGS(4);
note PT_NOTE FLAGS(4);
}

VERSION {
Expand Down
20 changes: 18 additions & 2 deletions toolchain_x86_32.cmake
@@ -1,4 +1,20 @@
option(ENABLE_CLANG_THUNKS "Enable building thunks with clang" FALSE)

set(CMAKE_SYSTEM_PROCESSOR i686)

set(CMAKE_C_COMPILER x86_64-linux-gnu-gcc -m32)
set(CMAKE_CXX_COMPILER x86_64-linux-gnu-g++ -m32)
if (ENABLE_CLANG_THUNKS)
message(STATUS "Enabling thunk clang building. Force enabling LLD as well")

set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CLANG_FLAGS "-target i686-linux-gnu -msse2 -mfpmath=sse")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CLANG_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_FLAGS}")
else()
set(CMAKE_C_COMPILER x86_64-linux-gnu-gcc -m32)
set(CMAKE_CXX_COMPILER x86_64-linux-gnu-g++ -m32)
endif()
20 changes: 18 additions & 2 deletions toolchain_x86_64.cmake
@@ -1,4 +1,20 @@
option(ENABLE_CLANG_THUNKS "Enable building thunks with clang" FALSE)

set(CMAKE_SYSTEM_PROCESSOR x86_64)

set(CMAKE_C_COMPILER x86_64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER x86_64-linux-gnu-g++)
if (ENABLE_CLANG_THUNKS)
message(STATUS "Enabling thunk clang building. Force enabling LLD as well")

set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CLANG_FLAGS "-target x86_64-linux-gnu")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CLANG_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_FLAGS}")
else()
set(CMAKE_C_COMPILER x86_64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER x86_64-linux-gnu-g++)
endif()

0 comments on commit 2b1ef97

Please sign in to comment.