Trying to be a bit less platform-specific.

This has only been tested on my amd64 gnu/linux, so take it with
the due suspicions.
This commit is contained in:
King_DuckZ 2020-04-26 22:06:53 +02:00
commit 0545f26990
3 changed files with 51 additions and 11 deletions

10
src/config.h.in Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#define ASM_PTR_SIZE @POINTER_SIZE@
#define ASM_FUNC_PTR_SIZE @FUNC_POINTER_SIZE@
#if defined(__cplusplus)
static_assert(sizeof(void*) == ASM_PTR_SIZE, "Build system reports an unexpected pointer size, please ensure assembly code is correct");
static_assert(sizeof(void(*)(int)) == ASM_FUNC_PTR_SIZE, "Build system reports an unexpected function pointer size, please ensure assembly code is correct");
#endif

View file

@ -4,6 +4,7 @@
#endif
#include "dynafunc_maker.hpp"
#include "config.h"
#include <algorithm>
#include <cassert>
#include <unistd.h> //for sysconf()
@ -19,14 +20,12 @@ namespace wren {
extern "C" const char g_dynafunc_end[];
const unsigned int g_dynafunc_len = g_dynafunc_end - g_dynafunc;
#if defined(__amd64__) and defined(__gnu_linux__)
const constexpr unsigned int g_dynafunc_ptr1_size = 8;
const constexpr unsigned int g_dynafunc_ptr2_size = 8;
const constexpr unsigned char g_dynafunc_ptr1[g_dynafunc_ptr1_size] = {0xef, 0xbe, 0xad, 0xde, 0xef, 0xbe, 0xad, 0xde};
const constexpr unsigned char g_dynafunc_ptr2[g_dynafunc_ptr2_size] = {0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba};
#else
# error "Not implemented on this platform"
#endif
const constexpr unsigned int g_dynafunc_ptr1_size = ASM_PTR_SIZE;
const constexpr unsigned int g_dynafunc_ptr2_size = ASM_FUNC_PTR_SIZE;
const constexpr unsigned char g_dynafunc_ptr1[] = {0xef, 0xbe, 0xad, 0xde, 0xef, 0xbe, 0xad, 0xde};
const constexpr unsigned char g_dynafunc_ptr2[] = {0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba};
static_assert(sizeof(g_dynafunc_ptr1) / sizeof(g_dynafunc_ptr1[0]) >= g_dynafunc_ptr1_size);
static_assert(sizeof(g_dynafunc_ptr2) / sizeof(g_dynafunc_ptr2[0]) >= g_dynafunc_ptr2_size);
template <typename T, unsigned int S>
void replace_ptr(
@ -35,8 +34,8 @@ namespace wren {
const unsigned char (&search)[S],
T replace
) {
static_assert(sizeof(T) == S, "Unexpected pointer size");
const auto subseq = std::search(beg, end, search, search + S);
static_assert(sizeof(T) <= S, "Unexpected pointer size");
const auto subseq = std::search(beg, end, search, search + sizeof(T));
assert(subseq != end);
const auto ptr = reinterpret_cast<unsigned char*>(&replace);
std::copy(ptr, ptr + sizeof(T), subseq);