Skip to content

Commit

Permalink
Allow cross-compiling from Linux with MinGW (Putnam3145#5)
Browse files Browse the repository at this point in the history
* Fix miscapitalized headers

* Remove extra qualifiers from method declarations

* Move urmem calling convention specializations to a namespace

Their current position is supported by MSVC but not GCC:
https://stackoverflow.com/questions/2097811/

* Add CMake files for cross-compiling

* Statically link libgcc and libstdc++

Co-Authored-By: steamport <[email protected]>
  • Loading branch information
2 people authored and MCHSL committed Nov 20, 2019
1 parent dc6f55d commit f449f86
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 43 deletions.
6 changes: 5 additions & 1 deletion byond-extools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ add_library(byond-extools SHARED ${SRC_FILES})
source_group(TREE ${SRC_DIR} FILES ${SRC_FILES})
if (WIN32)
set_property(TARGET byond-extools PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
target_compile_options(byond-extools PRIVATE "/MP")
if (MSVC)
target_compile_options(byond-extools PRIVATE "/MP")
else()
target_link_libraries(byond-extools PRIVATE "ws2_32" "psapi" "-static-libgcc" "-static-libstdc++" "-static")
endif()
target_compile_definitions(byond-extools PRIVATE WIN32_LEAN_AND_MEAN)
target_compile_definitions(byond-extools PRIVATE _WINSOCKAPI_)
else()
Expand Down
18 changes: 18 additions & 0 deletions byond-extools/cmake/i686-w64-mingw32.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set(GNU_HOST i686-w64-mingw32)
set(CMAKE_SYSTEM_PROCESSOR "i686")

set(COMPILER_PREFIX "${GNU_HOST}-")
set(COMPILER_SUFFIX "-posix")

set(CMAKE_SYSTEM_NAME "Windows")
set(CMAKE_CROSSCOMPILING TRUE)
set(WIN32 TRUE)
set(MINGW TRUE)

set(CMAKE_C_COMPILER ${COMPILER_PREFIX}gcc${COMPILER_SUFFIX})
set(CMAKE_CXX_COMPILER ${COMPILER_PREFIX}g++${COMPILER_SUFFIX})
set(CMAKE_RC_COMPILER ${COMPILER_PREFIX}windres)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
2 changes: 1 addition & 1 deletion byond-extools/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#ifdef _WIN32
#include <Windows.h>
#include <windows.h>

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
Expand Down
4 changes: 2 additions & 2 deletions byond-extools/src/core/sigscan/sigscan.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "sigscan.h"

#ifdef _WIN32
#include <Windows.h>
#include <Psapi.h>
#include <windows.h>
#include <psapi.h>
#else
// lol
#include <dlfcn.h>
Expand Down
4 changes: 2 additions & 2 deletions byond-extools/src/core/socket/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#include <windows.h>
#pragma comment (lib, "Ws2_32")
#endif

Expand All @@ -26,6 +26,6 @@ class SocketServer
bool listen_for_client();

bool send(std::string type, nlohmann::json content);
bool SocketServer::send(nlohmann::json j);
bool send(nlohmann::json j);
nlohmann::json recv_message();
};
75 changes: 39 additions & 36 deletions byond-extools/src/core/urmem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,55 @@
#include <memory>
#include <mutex>

class urmem {
public:
namespace urmem_invoker {
using address_t = unsigned long;
using byte_t = unsigned char;
using bytearray_t = std::vector<byte_t>;

enum class calling_convention {
cdeclcall,
stdcall,
thiscall
};

#ifdef _WIN32
template<calling_convention>
struct invoker;

template<>
struct invoker<calling_convention::cdeclcall> {
template<typename Ret, typename ... Args>
static inline Ret call(address_t address, Args ... args) {
return (reinterpret_cast<Ret(__cdecl *)(Args...)>(address))(args...);
}
};

template<>
struct invoker<calling_convention::stdcall> {
template<typename Ret, typename ... Args>
static inline Ret call(address_t address, Args ... args) {
return (reinterpret_cast<Ret(__stdcall *)(Args...)>(address))(args...);
}
};

template<>
struct invoker<calling_convention::thiscall> {
template<typename Ret, typename ... Args>
static inline Ret call(address_t address, Args ... args) {
return (reinterpret_cast<Ret(__thiscall *)(Args...)>(address))(args...);
}
};
#endif
}

class urmem {
public:
using address_t = unsigned long;
using byte_t = unsigned char;
using bytearray_t = std::vector<byte_t>;
using calling_convention = urmem_invoker::calling_convention;

template<calling_convention CConv = calling_convention::cdeclcall, typename Ret = void, typename ... Args>
static Ret call_function(address_t address, Args ... args) {
#ifdef _WIN32
return invoker<CConv>::template call<Ret, Args...>(address, args...);
return urmem_invoker::invoker<CConv>::template call<Ret, Args...>(address, args...);
#else
return (reinterpret_cast<Ret(*)(Args...)>(address))(args...);
#endif
Expand Down Expand Up @@ -320,36 +353,6 @@ class urmem {
address_t _original_addr{};
std::shared_ptr<patch> _patch;
};

private:
#ifdef _WIN32
template<calling_convention>
struct invoker;

template<>
struct invoker<calling_convention::cdeclcall> {
template<typename Ret, typename ... Args>
static inline Ret call(address_t address, Args ... args) {
return (reinterpret_cast<Ret(__cdecl *)(Args...)>(address))(args...);
}
};

template<>
struct invoker<calling_convention::stdcall> {
template<typename Ret, typename ... Args>
static inline Ret call(address_t address, Args ... args) {
return (reinterpret_cast<Ret(__stdcall *)(Args...)>(address))(args...);
}
};

template<>
struct invoker<calling_convention::thiscall> {
template<typename Ret, typename ... Args>
static inline Ret call(address_t address, Args ... args) {
return (reinterpret_cast<Ret(__thiscall *)(Args...)>(address))(args...);
}
};
#endif
};

#endif // URMEM_H_
2 changes: 1 addition & 1 deletion byond-extools/src/dmdism/disassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Disassembly
std::vector<int>* assemble();
Instruction& at(unsigned int i);
Instruction& at_offset(unsigned int offset);
Instruction* Disassembly::next_from_offset(unsigned int offset);
Instruction* next_from_offset(unsigned int offset);
int op_at(unsigned int i);
std::vector<Instruction>::iterator begin() noexcept;
std::vector<Instruction>::iterator end() noexcept;
Expand Down

0 comments on commit f449f86

Please sign in to comment.