Skip to content

Commit

Permalink
native library shared object support
Browse files Browse the repository at this point in the history
  • Loading branch information
dimas1185 committed Feb 9, 2023
1 parent 226f497 commit 23659dc
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 134 deletions.
11 changes: 3 additions & 8 deletions libraries/native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ project(native LANGUAGES CXX ASM)

if (NOT __APPLE)
set(CRT_ASM elf_crt.s)
set(UTILS_ASM utils.s)
else()
set(CRT_ASM macho_crt.s)
set(UTILS_ASM mac_utils.s)
endif()

if (NOT ${PARENT_PROJECT_NAME} MATCHES "cdt_tools")
Expand Down Expand Up @@ -339,12 +337,9 @@ add_library ( sf STATIC ${softfloat_sources} )
target_include_directories( sf PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR})

if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools")
# TODO: we need crt.cpp and assembly only for _prints_l and _prints
# we unlikely need those implementations for antler-run so
# we may need to add some code (macroses, etc) to crt.cpp and/or intrinsics.cpp so
# we can revert assembly changes and build only *.cpp without ${UTILS_ASM}
add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${UTILS_ASM})
add_library(${PROJECT_NAME} STATIC ${softfloat_sources} intrinsics.cpp crt_lib.cpp)

target_compile_definitions(${PROJECT_NAME} PUBLIC NATIVELIB_ENABLE_EXCEPTIONS)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../libraries/eosiolib/core
${CMAKE_SOURCE_DIR}/../libraries/eosiolib/contracts
${CMAKE_SOURCE_DIR}/../libraries/eosiolib/capi
Expand All @@ -353,7 +348,7 @@ if (${PARENT_PROJECT_NAME} STREQUAL "cdt_tools")
# suppress executable stack warning. this is due to absence of .note.GNU-stack
target_link_libraries(${PROJECT_NAME} PUBLIC "-Wl,-z,noexecstack")
else()
add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM} ${UTILS_ASM})
add_native_library ( native STATIC ${softfloat_sources} intrinsics.cpp crt.cpp ${CRT_ASM})
endif()

target_include_directories( native PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/include" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/source/8086-SSE" "${CMAKE_CURRENT_SOURCE_DIR}/softfloat/build/Linux-x86_64-GCC" ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/eosiolib/capi ${CMAKE_SOURCE_DIR}/eosiolib/contracts ${CMAKE_SOURCE_DIR}/eosiolib/core)
Expand Down
32 changes: 32 additions & 0 deletions libraries/native/crt_lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "native/eosio/crt.hpp"

#include <cstdio>

eosio::cdt::output_stream std_out;
eosio::cdt::output_stream std_err;

bool ___disable_output = false;
bool ___has_failed = false;
bool ___earlier_unit_test_has_failed = false;

void _prints_l(const char* cstr, uint32_t len, uint8_t which) {
for (int i=0; i < len; i++) {
if (which == eosio::cdt::output_stream_kind::std_out)
std_out.push(cstr[i]);
else if (which == eosio::cdt::output_stream_kind::std_err)
std_err.push(cstr[i]);
if (!___disable_output) {
std::putc(cstr[i], which == eosio::cdt::output_stream_kind::std_out ? stdout : stderr);
}
}
}
void _prints(const char* cstr, uint8_t which) {
for (int i=0; cstr[i] != '\0'; i++) {
if (which == eosio::cdt::output_stream_kind::std_out)
std_out.push(cstr[i]);
else if (which == eosio::cdt::output_stream_kind::std_err)
std_err.push(cstr[i]);
if (!___disable_output)
std::putc(cstr[i], which == eosio::cdt::output_stream_kind::std_out ? stdout : stderr);
}
}
66 changes: 65 additions & 1 deletion libraries/native/elf_crt.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
.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
Expand All @@ -8,4 +16,60 @@ _start:
call _wrap_main
mov %rax, %rdi
mov $60, %rax
syscall
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
60 changes: 0 additions & 60 deletions libraries/native/mac_utils.s

This file was deleted.

62 changes: 61 additions & 1 deletion libraries/native/macho_crt.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.global start
.global ____putc
.global __mmap
.global _setjmp
.global _longjmp

start:
mov %rsp, %rbp
Expand All @@ -7,4 +11,60 @@ start:
call __wrap_main
mov %rax, %rdi
mov $0x2000001, %rax
syscall
syscall

____putc:
dec %rsp
mov %rbx, %r8
mov %rdi, %rax
mov %al, 0(%rsp)
mov $1, %edi # using stdout
mov %rsp, %rsi # point to the buffer
mov $1, %edx # buffer is only 1 char
mov $0x2000004, %eax # write syscall 0x4
syscall
inc %rsp
mov %r8, %rbx
ret

__mmap:
mov $0x20000C5, %eax # mmap syscall 0xC5 or 197
mov $0, %rdi # don't map
mov $0x640000000, %rsi # size 100Mb
mov $3, %rdx
mov $0x1002, %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
12 changes: 12 additions & 0 deletions libraries/native/native/eosio/crt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@

#include <string>


#ifdef NATIVELIB_ENABLE_EXCEPTIONS
#include <stdexcept>
namespace eosio { namespace cdt {
using assert_exception = std::runtime_error;
}} //ns eosio::cdt
#endif

namespace eosio { namespace cdt {

enum output_stream_kind {
std_out,
std_err,
Expand All @@ -27,6 +36,9 @@ extern eosio::cdt::output_stream std_out;
extern eosio::cdt::output_stream std_err;
extern "C" jmp_buf* ___env_ptr;
extern "C" char* ___heap_ptr;
extern "C" bool ___disable_output;
extern "C" bool ___has_failed;
extern "C" bool ___earlier_unit_test_has_failed;

extern "C" {
void __set_env_test();
Expand Down
64 changes: 0 additions & 64 deletions libraries/native/utils.s

This file was deleted.

0 comments on commit 23659dc

Please sign in to comment.