diff --git a/project/arch.mk b/project/arch.mk index e69de29..82310ab 100644 --- a/project/arch.mk +++ b/project/arch.mk @@ -0,0 +1,12 @@ +# nt32/nt64 +ARCH = nt$(CC_BITS) + +src/internal/nolibc/toksvc_compiler.lo: $(SOURCE_DIR)/src/internal/nolibc/$(ARCH)/toksvc_compiler.s host.tag tree.tag + $(CC) -c -o $@ $< + +src/internal/nolibc/toksvc_compiler.o: $(SOURCE_DIR)/src/internal/nolibc/$(ARCH)/toksvc_compiler.s host.tag tree.tag + $(CC) -c -o $@ $< + +shared-objs-asm: src/internal/nolibc/toksvc_compiler.lo + +static-objs-asm: src/internal/nolibc/toksvc_compiler.o diff --git a/project/common.mk b/project/common.mk index 4b72987..453b6bf 100644 --- a/project/common.mk +++ b/project/common.mk @@ -1,6 +1,9 @@ API_SRCS = \ INTERNAL_SRCS = \ + src/internal/nolibc/toksvc_compiler.c \ + src/internal/toksvc_nolibc_impl.c \ + src/internal/toksvc_ntaio_impl.c \ APP_SRCS = \ diff --git a/project/extras.mk b/project/extras.mk index e69de29..5cf78f7 100644 --- a/project/extras.mk +++ b/project/extras.mk @@ -0,0 +1,21 @@ +CFLAGS_SHARED_ATTR += -DTOKS_PRE_ALPHA -DTOKS_EXPORT +CFLAGS_STATIC_ATTR += -DTOKS_PRE_ALPHA -DTOKS_STATIC -mstatic +CFLAGS_STATIC_ATTR += -DPE_STATIC -DDALIST_STATIC -DNTAPI_STATIC +CFLAGS_APP_ATTR += -DTOKS_APP + +ifeq ($(ALL_STATIC),yes) +CFLAGS_APP_ATTR += $(CFLAGS_STATIC_ATTR) +else +CFLAGS_APP_ATTR += -fvisibility=protetcted +endif + + +CFLAGS_CONFIG += -ffreestanding +CFLAGS_CONFIG += -D_MIDIPIX_FREESTANDING -D__NT$(HOST_BITS) +CFLAGS_CONFIG += -UWIN32 -U_WIN32 -U__WIN32 -U__WIN32__ +CFLAGS_CONFIG += -UWIN64 -U_WIN64 -U__WIN64 -U__WIN64__ + +LDFLAGS_COMMON += -nostdlib -lntapi -lpemagine -ldalist +LDFLAGS_COMMON += -Wl,--entry -Wl,$(HOST_UNDERSCORE)$(PACKAGE)_entry_point + +LDFLAGS_SHARED += -Wl,--exclude-all-symbols diff --git a/project/headers.mk b/project/headers.mk index e7c0af7..61f548e 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -2,5 +2,13 @@ API_HEADERS = \ INTERNAL_HEADERS = \ $(SOURCE_DIR)/src/internal/argv/argv.h \ + $(SOURCE_DIR)/src/internal/nolibc/stdbool.h \ + $(SOURCE_DIR)/src/internal/nolibc/stddef.h \ + $(SOURCE_DIR)/src/internal/nolibc/stdint.h \ + $(SOURCE_DIR)/src/internal/nolibc/stdio.h \ + $(SOURCE_DIR)/src/internal/nolibc/stdlib.h \ + $(SOURCE_DIR)/src/internal/nolibc/string.h \ + $(SOURCE_DIR)/src/internal/nolibc/unistd.h \ + $(SOURCE_DIR)/src/internal/toksvc_nolibc_impl.h \ ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/project/tree.mk b/project/tree.mk index ed9e1f8..e593d53 100644 --- a/project/tree.mk +++ b/project/tree.mk @@ -1,4 +1,6 @@ -TREE_DIRS = bin src lib +TREE_DIRS = bin src lib \ + src/internal \ + src/internal/nolibc \ tree.tag: mkdir -p $(TREE_DIRS) diff --git a/src/internal/nolibc/nt32/toksvc_compiler.asm b/src/internal/nolibc/nt32/toksvc_compiler.asm new file mode 100644 index 0000000..71c8080 --- /dev/null +++ b/src/internal/nolibc/nt32/toksvc_compiler.asm @@ -0,0 +1,20 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; toksvc: a framework-native token broker service ;; +;; Copyright (C) 2020 Z. Gilboa ;; +;; Released under GPLv2 and GPLv3; see COPYING.TOKSVC. ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework +; and are provided here in the form of no-op functions in order to satisfy +; compiler-generated dependencies. + +TITLE __psx_compiler + +.model flat +.code + +___chkstk PROC EXPORT + ret +___chkstk ENDP + +END diff --git a/src/internal/nolibc/nt32/toksvc_compiler.s b/src/internal/nolibc/nt32/toksvc_compiler.s new file mode 100644 index 0000000..65869a0 --- /dev/null +++ b/src/internal/nolibc/nt32/toksvc_compiler.s @@ -0,0 +1,20 @@ +########################################################### +## toksvc: a framework-native token broker service ## +## Copyright (C) 2020 Z. Gilboa ## +## Released under GPLv2 and GPLv3; see COPYING.TOKSVC. ## +########################################################### + +# ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework +# and are provided here in the form of no-op functions in order to satisfy +# compiler-generated dependencies. + +.section .text + +.global _pei386_runtime_relocator +.global ___chkstk_ms + +_pei386_runtime_relocatorr: + ret + +___chkstk_ms: + ret diff --git a/src/internal/nolibc/nt64/toksvc_compiler.asm b/src/internal/nolibc/nt64/toksvc_compiler.asm new file mode 100644 index 0000000..5f084ef --- /dev/null +++ b/src/internal/nolibc/nt64/toksvc_compiler.asm @@ -0,0 +1,19 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; toksvc: a framework-native token broker service ;; +;; Copyright (C) 2020 Z. Gilboa ;; +;; Released under GPLv2 and GPLv3; see COPYING.TOKSVC. ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework +; and are provided here in the form of no-op functions in order to satisfy +; compiler-generated dependencies. + +TITLE __psx_compiler + +.code + +__chkstk PROC EXPORT + ret +__chkstk ENDP + +END diff --git a/src/internal/nolibc/nt64/toksvc_compiler.s b/src/internal/nolibc/nt64/toksvc_compiler.s new file mode 100644 index 0000000..65869a0 --- /dev/null +++ b/src/internal/nolibc/nt64/toksvc_compiler.s @@ -0,0 +1,20 @@ +########################################################### +## toksvc: a framework-native token broker service ## +## Copyright (C) 2020 Z. Gilboa ## +## Released under GPLv2 and GPLv3; see COPYING.TOKSVC. ## +########################################################### + +# ___chkstk_ms and _pei386_runtime_relocator are not needed by the framework +# and are provided here in the form of no-op functions in order to satisfy +# compiler-generated dependencies. + +.section .text + +.global _pei386_runtime_relocator +.global ___chkstk_ms + +_pei386_runtime_relocatorr: + ret + +___chkstk_ms: + ret diff --git a/src/internal/nolibc/stdbool.h b/src/internal/nolibc/stdbool.h new file mode 100644 index 0000000..45fc6f9 --- /dev/null +++ b/src/internal/nolibc/stdbool.h @@ -0,0 +1,11 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +#ifndef __cplusplus + +#define true 1 +#define false 0 +#define bool _Bool + +#endif +#endif diff --git a/src/internal/nolibc/stddef.h b/src/internal/nolibc/stddef.h new file mode 100644 index 0000000..52ef014 --- /dev/null +++ b/src/internal/nolibc/stddef.h @@ -0,0 +1,8 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +#include + +#define offsetof(type,member) __offsetof(type,member) + +#endif diff --git a/src/internal/nolibc/stdint.h b/src/internal/nolibc/stdint.h new file mode 100644 index 0000000..831d865 --- /dev/null +++ b/src/internal/nolibc/stdint.h @@ -0,0 +1,6 @@ +#ifndef _STDINT_H +#define _STDINT_H + +#include + +#endif diff --git a/src/internal/nolibc/stdio.h b/src/internal/nolibc/stdio.h new file mode 100644 index 0000000..610533e --- /dev/null +++ b/src/internal/nolibc/stdio.h @@ -0,0 +1,8 @@ +#ifndef _STDIO_H +#define _STDIO_H + +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + +#endif diff --git a/src/internal/nolibc/stdlib.h b/src/internal/nolibc/stdlib.h new file mode 100644 index 0000000..8a24bec --- /dev/null +++ b/src/internal/nolibc/stdlib.h @@ -0,0 +1,4 @@ +#ifndef _STDLIB_H +#define _STDLIB_H + +#endif diff --git a/src/internal/nolibc/string.h b/src/internal/nolibc/string.h new file mode 100644 index 0000000..f8e3b8e --- /dev/null +++ b/src/internal/nolibc/string.h @@ -0,0 +1,4 @@ +#ifndef _STRING_H +#define _STRING_H + +#endif diff --git a/src/internal/nolibc/unistd.h b/src/internal/nolibc/unistd.h new file mode 100644 index 0000000..2e39dc8 --- /dev/null +++ b/src/internal/nolibc/unistd.h @@ -0,0 +1,4 @@ +#ifndef _UNISTD_H +#define _UNISTD_H + +#endif diff --git a/src/internal/toksvc_nolibc_impl.c b/src/internal/toksvc_nolibc_impl.c new file mode 100644 index 0000000..afc2286 --- /dev/null +++ b/src/internal/toksvc_nolibc_impl.c @@ -0,0 +1,73 @@ +/*********************************************************/ +/* toksvc: a framework-native token broker service */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TOKSVC. */ +/*********************************************************/ + +#include + +extern const ntapi_vtbl * toks_ntapi; + +void * toks_memcpy(void * dst, const void * src, size_t n) +{ + return toks_ntapi->tt_generic_memcpy(dst,src,n); +} + +void * toks_memset(void * ch, int c, size_t n) +{ + return toks_ntapi->tt_generic_memset(ch,c,n); +} + +char * toks_strcpy(char * dst, const char * src) +{ + return toks_ntapi->tt_generic_memcpy( + dst,src, + toks_ntapi->tt_string_null_offset_multibyte(src)); +} + +size_t toks_strlen(const char * ch) +{ + return toks_ntapi->tt_string_null_offset_multibyte(ch); +} + +int toks_strcmp(const char * a, const char * b) +{ + return toks_ntapi->tt_strcmp_multibyte(a,b); +} + +int toks_strncmp(const char * a, const char * b, size_t n) +{ + return toks_ntapi->tt_strncmp_multibyte(a,b,n); +} + +char * toks_strchr(const char * ch, int c) +{ + for (; *ch; ch++) + if (*ch == c) + return (char *)ch; + return 0; +} + +char * toks_strrchr(const char * ch, int c) +{ + const char * base; + + base = ch; + ch += toks_ntapi->tt_string_null_offset_multibyte(ch); + + for (; ch >= base; ch--) + if (*ch == c) + return (char *)ch; + return 0; +} + +#ifdef TOKS_EXPORT +int __stdcall toksvc_entry_point(void * hinstance, uint32_t reason, void * reserved) +{ + (void)hinstance; + (void)reason; + (void)reserved; + + return 1; +} +#endif diff --git a/src/internal/toksvc_nolibc_impl.h b/src/internal/toksvc_nolibc_impl.h new file mode 100644 index 0000000..5d21612 --- /dev/null +++ b/src/internal/toksvc_nolibc_impl.h @@ -0,0 +1,41 @@ +#ifndef TOKSVC_NOLIBC_IMPL_H +#define TOKSVC_NOLIBC_IMPL_H + +#define isatty toks_isatty + +#define sprintf toks_sprintf +#define snprintf toks_snprintf + +#define memcpy toks_memcpy +#define memset toks_memset + +#define strcpy toks_strcpy +#define strlen toks_strlen +#define strcmp toks_strcmp +#define strncmp toks_strncmp +#define strchr toks_strchr +#define strrchr toks_strrchr + +#define calloc toks_calloc +#define free toks_free + +int toks_isatty(int fildes); +int toks_write(int, const void *, size_t); + +int toks_sprintf(char * str, const char * fmt, ...); +int toks_snprintf(char * str, size_t n, const char * fmt, ...); + +void * toks_memcpy(void * dst, const void * src, size_t n); +void * memset(void * ch, int c, size_t n); + +char * toks_strcpy(char * dst, const char * src); +size_t toks_strlen(const char * ch); +int toks_strcmp(const char * a, const char * b); +int toks_strncmp(const char * a, const char * b, size_t n); +char * toks_strchr(const char * ch, int c); +char * toks_strrchr(const char * ch, int c); + +void * toks_calloc(size_t n, size_t size); +void toks_free(void *); + +#endif diff --git a/src/internal/toksvc_ntaio_impl.c b/src/internal/toksvc_ntaio_impl.c new file mode 100644 index 0000000..b515494 --- /dev/null +++ b/src/internal/toksvc_ntaio_impl.c @@ -0,0 +1,129 @@ +/*********************************************************/ +/* toksvc: a framework-native token broker service */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TOKSVC. */ +/*********************************************************/ + +#include +#include + +extern const ntapi_vtbl * toks_ntapi; + +int toks_sprintf(char * str, const char * fmt, ...) +{ + int ret; + va_list ap; + + va_start(ap, fmt); + ret = toks_ntapi->vsprintf(str, fmt, ap); + va_end(ap); + + return ret; +} + +int toks_snprintf(char * str, size_t n, const char * fmt, ...) +{ + int ret; + va_list ap; + + va_start(ap, fmt); + ret = toks_ntapi->vsnprintf(str, n, fmt, ap); + va_end(ap); + + return ret; +} + +int toks_isatty(int fildes) +{ + nt_runtime_data * rtdata; + + if ((toks_ntapi->tt_get_runtime_data(&rtdata,0))) + return 0; + + if (fildes == 0) + return (rtdata->stdin_type == NT_FILE_TYPE_PTY); + + else if (fildes == 1) + return (rtdata->stdout_type == NT_FILE_TYPE_PTY); + + else if (fildes == 2) + return (rtdata->stderr_type == NT_FILE_TYPE_PTY); + + else + return 0; +} + +int toks_write(int fd, const void * buf, size_t size) +{ + int32_t status; + nt_runtime_data * rtdata; + ntapi_zw_write_file * iofn; + void * hio; + void * hevent; + int fdtype; + nt_iosb iosb; + + /* size */ + if (size >= 0x80000000) + return NT_STATUS_INVALID_PARAMETER; + + /* rtdata */ + if (toks_ntapi->tt_get_runtime_data(&rtdata,0)) + return NT_STATUS_REINITIALIZATION_NEEDED; + + /* hio, io type */ + if (fd == STDIN_FILENO) { + hio = rtdata->hstdin; + fdtype = rtdata->stdin_type; + + } else if (fd == STDOUT_FILENO) { + hio = rtdata->hstdout; + fdtype = rtdata->stdout_type; + + } else if (fd == STDERR_FILENO) { + hio = rtdata->hstderr; + fdtype = rtdata->stderr_type; + } else { + return NT_STATUS_INVALID_PARAMETER; + } + + if (!hio) + return NT_STATUS_INVALID_HANDLE; + + /* iofn */ + iofn = (fdtype == NT_FILE_TYPE_PTY) + ? (ntapi_zw_write_file *)toks_ntapi->pty_write + : toks_ntapi->zw_write_file; + + /* hevent */ + if ((status = toks_ntapi->tt_create_private_event( + &hevent, + NT_NOTIFICATION_EVENT, + NT_EVENT_NOT_SIGNALED))) + return status; + + /* iowrite */ + status = iofn( + hio,hevent, + 0,0,&iosb, + (void *)buf,size, + 0,0); + + /* wait */ + switch (status) { + case NT_STATUS_PENDING: + status = toks_ntapi->zw_wait_for_single_object( + hevent,0,0); + break; + + default: + iosb.status = status; + break; + } + + /* hevent */ + toks_ntapi->zw_close(hevent); + + /* ret */ + return iosb.status ? iosb.status : iosb.info; +}