Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
added support for native Linux builds
Browse files Browse the repository at this point in the history
  • Loading branch information
larryk85 committed Nov 13, 2018
1 parent 1e9555b commit 7214963
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
[submodule "tools/native_tester/Catch2"]
path = tools/native_tester/Catch2
url = https://github.com/catchorg/Catch2
[submodule "libraries/catch2/upstream"]
path = libraries/catch2/upstream
url = https://github.com/catchorg/Catch2.git
2 changes: 2 additions & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ add_subdirectory(libc)
add_subdirectory(libc++)
add_subdirectory(eosiolib)
add_subdirectory(boost)
add_subdirectory(crt)
add_subdirectory(catch2)
1 change: 1 addition & 0 deletions libraries/catch2/upstream
Submodule upstream added at 489a41
14 changes: 14 additions & 0 deletions libraries/crt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
file(GLOB SOURCES _crt.s crt.cpp)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-everything")

add_native_library(crt
${SOURCES})

add_dependencies(crt EosioTools)

install(TARGETS crt EXPORT EosioLib
LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR})

add_custom_command( TARGET crt POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:crt> ${CMAKE_BINARY_DIR}/lib )
75 changes: 75 additions & 0 deletions libraries/crt/_crt.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
.global _start
.global ___putc
.global _mmap
.global setjmp
.global longjmp
.type _start,@function
.type ___putc,@function
.type _mmap,@function
.type setjmp,@function
.type longjmp,@function

_start:
mov %rsp, %rbp
mov 0(%rbp), %rdi
lea 8(%rbp), %rsi
call __wrap_main
mov %rax, %rdi
mov $60, %rax
syscall

___putc:
dec %rsp
mov %rbx, %r8
mov %rdi, %rax
mov %al, 0(%rsp)
mov $1, %edi
mov %rsp, %rsi
mov $1, %edx
mov $1, %eax
syscall
inc %rsp
mov %r8, %rbx
ret

_mmap:
mov $9, %eax
mov $0, %rdi
mov $0x6400000, %rsi # 100Mb
mov $3, %rdx
mov $0x22, %r10
mov $-1, %r8
mov $0, %r9
syscall
ret

setjmp:
mov %rbx, 0(%rdi)
mov %rbp, 8(%rdi)
mov %r12, 16(%rdi)
mov %r13, 24(%rdi)
mov %r14, 32(%rdi)
mov %r15, 40(%rdi)
lea 8(%rsp), %rdx
mov %rdx, 48(%rdi)
mov (%rsp), %rdx
mov %rdx, 56(%rdi)
xor %rax, %rax
ret

longjmp:
mov %rsi, %rax
test %rax, %rax
jnz 1f
inc %rax
1:
mov 0(%rdi), %rbx
mov 8(%rdi), %rbp
mov 16(%rdi), %r12
mov 24(%rdi), %r13
mov 32(%rdi), %r14
mov 40(%rdi), %r15
mov 48(%rdi), %rdx
mov %rdx, %rsp
mov 56(%rdi), %rdx
jmp *%rdx
183 changes: 183 additions & 0 deletions libraries/crt/crt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#include <eosiolib/name.hpp>
#include <cstdint>
#include <stdio.h>
#include <setjmp.h>

extern "C" {
int main(int, char**);
char* _mmap();

static jmp_buf ___env;
static int ___jmp_ret;
char* ___heap;
char* ___heap_ptr;
void ___putc(char c);

void prints_l(const char* cstr, uint32_t len) {
for (int i=0; i < len; i++)
___putc(cstr[i]);
}

void prints(const char* cstr) {
for (int i=0; cstr[i] != '\0'; i++)
___putc(cstr[i]);
}

void printi(int64_t value) {
printf("%lli\n", value);
}

void printui(uint64_t value) {
printf("%llu\n", value);
}

void printi128(const int128_t* value) {
int* tmp = (int*)value;
printf("0x%04x%04x%04x%04x\n", tmp[0], tmp[1], tmp[2], tmp[3]);
}

void printui128(const int128_t* value) {
int* tmp = (int*)value;
printf("0x%04x%04x%04x%04x\n", tmp[0], tmp[1], tmp[2], tmp[3]);
}

void printsf(float value) {
printf("%f\n", value);
}

void printdf(double value) {
printf("%f\n", value);
}

void printqf(const long double* value) {
int* tmp = (int*)value;
printf("0x%04x%04x%04x%04x\n", tmp[0], tmp[1], tmp[2], tmp[3]);
}

void printn(uint64_t nm) {
std::string s = eosio::name(nm).to_string();
prints_l(s.c_str(), s.length());
}

void printhex(const void* data, uint32_t len) {
char* tmp = (char*)data;
for (int i=0; i < len; i++) {
}
}

void* memset ( void* ptr, int value, size_t num ) {
unsigned char v = value;
char* _ptr = (char*)ptr;
for (int i=0; i < num; i++) {
_ptr[i] = v;
}
return (void*)_ptr;
}

void* memcpy ( void* destination, const void* source, size_t num ) {
char* dest = (char*)destination;
char* src = (char*)source;
for (int i=0; i < num; i++) {
dest[i] = src[i];
}
return (void*)dest;
}

void eosio_assert(uint32_t test, const char* msg) {
if (test == 0) {
prints("asserted with message [");
prints(msg);
prints_l("]\n", 2);
longjmp(___env, 1);
}
}

void eosio_assert_message(uint32_t test, const char* msg, uint32_t len) {
if (test == 0) {
prints("asserted with message [");
prints_l(msg, len);
prints_l("]\n", 2);
longjmp(___env, 1);
}
}

void eosio_assert_code(uint32_t test, uint64_t code) {
if (test == 0) {
prints("asserted with code [");
printui(code);
prints_l("]\n", 2);
longjmp(___env, 1);
}
}

void abort() {
eosio_assert(false, "abort");
}

void set_blockchain_parameters_packed( char* data, uint32_t datalen ){}
void get_blockchain_parameters_packed( char* data, uint32_t datalen ){}

size_t __builtin_wasm_current_memory() {
return (size_t)___heap_ptr;
}

size_t __builtin_wasm_grow_memory(size_t size) {
if ((___heap_ptr + (size*64*1024)) > (___heap_ptr + 100*1024*1024))
eosio_assert(false, "__builtin_wasm_grow_memory");
___heap_ptr += (size*64*1024);
return (size_t)___heap_ptr;
}

int __wrap_main(int argc, char** argv) {
int ret_val = 0;
___heap = _mmap();
___heap_ptr = ___heap;
___jmp_ret = setjmp(___env);
if (___jmp_ret == 0) {
ret_val = main(argc, argv);
} else {
ret_val = -1;
}
return ret_val;
}

// action.h
uint32_t read_action_data( void* msg, uint32_t len ) {
eosio_assert(false, "unsupported");
}
uint32_t action_data_size() {
eosio_assert(false, "unsupported");
}
void require_recipient( capi_name name ) {
eosio_assert(false, "unsupported");
}
void require_auth( capi_name name ) {
eosio_assert(false, "unsupported");
}
bool has_auth( capi_name name ) {
eosio_assert(false, "unsupported");
}
void require_auth2( capi_name name, capi_name permission ) {
eosio_assert(false, "unsupported");
}
bool is_account( capi_name name ) {
eosio_assert(false, "unsupported");
}
void send_inline(char *serialized_action, size_t size) {
eosio_assert(false, "unsupported");
}
void send_context_free_inline(char *serialized_action, size_t size) {
eosio_assert(false, "unsupported");
}
uint64_t publication_time() {
eosio_assert(false, "unsupported");
}
capi_name current_receiver() {
eosio_assert(false, "unsupported");
}

// chain.h
uint32_t get_active_producers( capi_name* producers, uint32_t datalen ) {
eosio_assert(false, "unsupported");
}
}
Binary file added libraries/crt/tt
Binary file not shown.
1 change: 1 addition & 0 deletions libraries/eosiolib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ install(TARGETS native_eosio EXPORT EosioLib

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../eosiolib DESTINATION ${CMAKE_BINARY_DIR}/include FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
add_custom_command( TARGET eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:eosio> ${CMAKE_BINARY_DIR}/lib )
add_custom_command( TARGET native_eosio POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:native_eosio> ${CMAKE_BINARY_DIR}/lib )
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../eosiolib DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
4 changes: 4 additions & 0 deletions libraries/eosiolib/eosio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <eosiolib/dispatcher.hpp>
#include <eosiolib/contract.hpp>

#ifndef EOSIO_NATIVE
static_assert( sizeof(long) == sizeof(int), "unexpected size difference" );
#endif

/**
* Helper macros to reduce the verbosity for common contracts
*/
Expand Down
7 changes: 7 additions & 0 deletions libraries/eosiolib/eosiolib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#include "memory.hpp"
#include "privileged.hpp"

#ifdef EOSIO_NATIVE
extern "C" {
size_t __builtin_wasm_current_memory();
size_t __builtin_wasm_grow_memory(size_t);
}
#endif

void* sbrk(size_t num_bytes) {
constexpr uint32_t NBPPL2 = 16U;
constexpr uint32_t NBBP = 65536U;
Expand Down
2 changes: 1 addition & 1 deletion libraries/eosiolib/name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace eosio {
eosio_assert( false, "string is too long to be a valid name" );
}

auto n = std::min( str.size(), 12u );
auto n = std::min( (uint32_t)str.size(), (uint32_t)12u );
for( decltype(n) i = 0; i < n; ++i ) {
value <<= 5;
value |= char_to_value( str[i] );
Expand Down
2 changes: 0 additions & 2 deletions libraries/eosiolib/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace eosio {

static_assert( sizeof(long) == sizeof(int), "unexpected size difference" );

/**
* Prints string
*
Expand Down
2 changes: 1 addition & 1 deletion libraries/libc++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ install(TARGETS native_c++ EXPORT EosioLib

add_custom_command( TARGET c++ POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:c++> ${CMAKE_BINARY_DIR}/lib )

add_custom_command( TARGET native_c++ POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:c++> ${CMAKE_BINARY_DIR}/native/lib )
add_custom_command( TARGET native_c++ POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:native_c++> ${CMAKE_BINARY_DIR}/lib )

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/libcxx/include/ DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/libcxx)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/libcxx/include/ DESTINATION ${CMAKE_BINARY_DIR}/include/libcxx)
Expand Down
2 changes: 1 addition & 1 deletion libraries/libc++/libcxx
2 changes: 1 addition & 1 deletion libraries/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ install(TARGETS native_c EXPORT EosioLib

add_custom_command( TARGET c POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:c> ${CMAKE_BINARY_DIR}/lib )

add_custom_command( TARGET native_c POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:c> ${CMAKE_BINARY_DIR}/native/lib )
add_custom_command( TARGET native_c POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:native_c> ${CMAKE_BINARY_DIR}/lib )

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/musl/include/ DESTINATION ${CMAKE_BINARY_DIR}/include/libc/)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/musl/src/internal/ DESTINATION ${CMAKE_BINARY_DIR}/include/libc/)
Expand Down
2 changes: 1 addition & 1 deletion libraries/libc/musl
Submodule musl updated 107 files
21 changes: 0 additions & 21 deletions libraries/native/CMakeLists.txt

This file was deleted.

1 change: 1 addition & 0 deletions modules/EosioCDTNative.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ endmacro()
macro (add_native_executable TARGET)
add_executable( ${TARGET} ${ARGN} )
target_compile_options( ${TARGET} PUBLIC -fnative )
set_target_properties( ${TARGET} PROPERTIES LINK_FLAGS "-fnative" )
endmacro()

0 comments on commit 7214963

Please sign in to comment.