...
 
Commits (3)
...@@ -30,8 +30,13 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/d3d9 ${PROJECT_NAME}_LIST) ...@@ -30,8 +30,13 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/d3d9 ${PROJECT_NAME}_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/llmo ${PROJECT_NAME}_LIST) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/llmo ${PROJECT_NAME}_LIST)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/sys ${PROJECT_NAME}_LIST) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/sys ${PROJECT_NAME}_LIST)
add_definitions("-ffunction-sections -fexec-charset=CP1251 -pipe -w") if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--gc-sections -ffast-math") add_definitions("-ffunction-sections -pipe -w")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--gc-sections -ffast-math -fno-stack-protector")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_definitions("-ffunction-sections -fexec-charset=CP1251 -pipe -w")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--gc-sections -ffast-math")
endif()
add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_LIST}) add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_LIST})
...@@ -41,4 +46,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES ...@@ -41,4 +46,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_EXTENSIONS NO CXX_EXTENSIONS NO
) )
target_link_libraries(${PROJECT_NAME} d3d9 d3dx9 dxerr9 mman ${Boost_LIBRARIES}) target_link_libraries(${PROJECT_NAME} d3d9 d3dx9 dxerr9 ${Boost_LIBRARIES})
Subproject commit 60f35fb446c11a6dee78efd3c09e9fdea753a915 Subproject commit d7109b81bf73db2d438f8e283de2f0429b868663
#include "cshortasm.h" #include "cshortasm.h"
#ifdef WIN32
# include <windows.h>
#else
# include <sys/mman.h>
#endif
CShortAsm::CShortAsm( uint32_t pages ) { CShortAsm::CShortAsm( uint32_t pages ) {
_size = pages * PAGE_SIZE; _size = pages * PAGE_SIZE;
_code = static_cast<uint8_t *>( _code = static_cast<uint8_t *>( myalloc( _size ) );
mmap( NULL, _size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0 ) );
} }
CShortAsm::~CShortAsm() CShortAsm::~CShortAsm() {
{ myfree( _code, _size );
munmap(_code, _size);
} }
void CShortAsm::insert( uint8_t *array, uint32_t size ) { void CShortAsm::insert( uint8_t *array, uint32_t size ) {
...@@ -802,12 +806,10 @@ uint32_t CShortAsm::getSize() { ...@@ -802,12 +806,10 @@ uint32_t CShortAsm::getSize() {
} }
void CShortAsm::resize( uint32_t pages ) { void CShortAsm::resize( uint32_t pages ) {
uint8_t *new_code = static_cast<uint8_t *>( mmap( NULL, pages * PAGE_SIZE, uint8_t *new_code = static_cast<uint8_t *>( myalloc( pages * PAGE_SIZE ) );
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE, 0, 0 ) );
memcpy(new_code, _code, _size); memcpy(new_code, _code, _size);
_size = pages * PAGE_SIZE; _size = pages * PAGE_SIZE;
munmap(_code, _size); myfree( _code, _size );
_code = new_code; _code = new_code;
} }
...@@ -819,6 +821,40 @@ void CShortAsm::write( uint8_t v ) { ...@@ -819,6 +821,40 @@ void CShortAsm::write( uint8_t v ) {
if (_offset + 1 >= _size) if (_offset + 1 >= _size)
resize(arrayToPages(_offset * 2)); resize(arrayToPages(_offset * 2));
_code[_offset++] = v; _code[_offset++] = v;
if (_offset > _peak) if ( _offset > _peak ) _peak = _offset;
_peak = _offset; }
void *CShortAsm::myalloc( size_t size ) {
#ifdef WIN32
auto Unprotect = []( size_t address, size_t size ) // Allow execute code. By FYP
{
do {
MEMORY_BASIC_INFORMATION mbi;
if ( !VirtualQuery( reinterpret_cast<PVOID>( address ), &mbi, sizeof( mbi ) ) )
throw "virtual query error";
if ( size > mbi.RegionSize )
size -= mbi.RegionSize;
else
size = 0;
DWORD oldp;
if ( !VirtualProtect( mbi.BaseAddress, mbi.RegionSize, PAGE_EXECUTE_READWRITE, &oldp ) )
throw "virtual protect error";
if ( reinterpret_cast<size_t>( mbi.BaseAddress ) + mbi.RegionSize < address + size )
address = reinterpret_cast<size_t>( mbi.BaseAddress ) + mbi.RegionSize;
} while ( size );
};
auto addr = malloc( size );
Unprotect( reinterpret_cast<size_t>( addr ), size );
return addr;
#else
return mmap( NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0 );
#endif
}
void *CShortAsm::myfree( void *ptr, size_t size ) {
#ifdef WIN32
VirtualFree( ptr, size, 0 );
#else
munmap( ptr, size );
#endif
} }
#ifndef CSHORTASM_H #ifndef CSHORTASM_H
#define CSHORTASM_H #define CSHORTASM_H
#include <sys/mman.h>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <map> #include <map>
...@@ -103,6 +102,8 @@ protected: ...@@ -103,6 +102,8 @@ protected:
void resize( uint32_t pages ); void resize( uint32_t pages );
uint32_t getRelativeAddress( uint32_t addr ); uint32_t getRelativeAddress( uint32_t addr );
void write( uint8_t v ); void write( uint8_t v );
void *myalloc( size_t size );
void *myfree( void *ptr, size_t size );
uint8_t *_code = nullptr; uint8_t *_code = nullptr;
uint32_t _size = 0; uint32_t _size = 0;
......