From 21f40e30832d91c3d4e004e2415624d56bf6f9e0 Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Sat, 15 Jul 2023 19:52:58 +0200 Subject: [PATCH] Initial source push! --- .gitignore | 82 +++++++++++++++++++++ CMakeLists.txt | 60 ++++++++++++++++ doc/CMakeLists.txt | 45 ++++++++++++ src/CMakeLists.txt | 5 ++ src/based/CMakeLists.txt | 16 +++++ src/based/base.c | 8 +++ src/based/include/CMakeLists.txt | 6 ++ src/based/include/base.h | 39 ++++++++++ src/based/include/env/CMakeLists.txt | 3 + src/based/include/env/envb.h | 94 +++++++++++++++++++++++++ src/based/system/CMakeLists.txt | 10 +++ src/based/system/bootstrap.c | 24 +++++++ src/based/system/unix/CMakeLists.txt | 2 + src/based/system/win32/CMakeLists.txt | 3 + src/based/system/win32/win32sym.c | 21 ++++++ src/cli/CMakeLists.txt | 5 ++ src/cli/main.c | 6 ++ src/runtime/CMakeLists.txt | 15 ++++ src/runtime/rtinternal.h | 3 + src/runtime/runtime.c | 9 +++ src/runtime/system/CMakeLists.txt | 8 +++ src/runtime/system/unix/CMakeLists.txt | 3 + src/runtime/system/unix/crt.S | 17 +++++ src/runtime/system/unix/unixrt.c | 6 ++ src/runtime/system/win32/CMakeLists.txt | 3 + src/runtime/system/win32/winrt.c | 59 ++++++++++++++++ 26 files changed, 552 insertions(+) create mode 100755 .gitignore create mode 100755 CMakeLists.txt create mode 100755 doc/CMakeLists.txt create mode 100755 src/CMakeLists.txt create mode 100755 src/based/CMakeLists.txt create mode 100755 src/based/base.c create mode 100755 src/based/include/CMakeLists.txt create mode 100755 src/based/include/base.h create mode 100755 src/based/include/env/CMakeLists.txt create mode 100755 src/based/include/env/envb.h create mode 100755 src/based/system/CMakeLists.txt create mode 100755 src/based/system/bootstrap.c create mode 100755 src/based/system/unix/CMakeLists.txt create mode 100755 src/based/system/win32/CMakeLists.txt create mode 100755 src/based/system/win32/win32sym.c create mode 100755 src/cli/CMakeLists.txt create mode 100755 src/cli/main.c create mode 100755 src/runtime/CMakeLists.txt create mode 100755 src/runtime/rtinternal.h create mode 100755 src/runtime/runtime.c create mode 100755 src/runtime/system/CMakeLists.txt create mode 100755 src/runtime/system/unix/CMakeLists.txt create mode 100755 src/runtime/system/unix/crt.S create mode 100755 src/runtime/system/unix/unixrt.c create mode 100755 src/runtime/system/win32/CMakeLists.txt create mode 100755 src/runtime/system/win32/winrt.c diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..5f143dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,82 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# CMAKE +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +cmake_install.cmake +CTestTestfile.cmake + +# Make +Makefile + +# Build +Build/ +build/ +BUILD/ +bin/ +BIN/ +lib/ +LIB/ +out/ + +# IDE +.vs/ +.vscode/ +.idea/ +.kdev/ +.kdev4/ +*.kdev +*.kdev4 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..33a2b9b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,60 @@ +cmake_minimum_required(VERSION 3.5) + +project("Based" LANGUAGES C ASM) + +# ============================================= +# Options +# ============================================= +#unset(DOCUMENTATION CACHE) +#unset(INTERNAL_DOC, CACHE) +option(DOCUMENTATION "Enable to generate ducumentation." OFF) +option(INTERNAL_DOC "Enable to generate internal & public documentaion. Disable to only generate public documentation" OFF) + +# ============================================= +# Policies +# ============================================= +cmake_policy(SET CMP0076 NEW) + +# ============================================= +# Compiler +# ============================================= +if(CMAKE_C_COMPILER_ID STREQUAL "GNU") # GNUCC +# Add linker options. +add_link_options(-nostdlib -nodefaultlibs -nostdinc -lgcc) # -z noexecstack +add_compile_options( + -Wall -Wextra -Wshadow -Wfloat-equal -Wpointer-arith -fPIC -fno-builtin + "$<$:-s;-O3;-DNDEBUG>" + "$<$:-O0;-g>" + "$<$:-O2;-g;-DNDEBUG>") + +elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC") # MSVC +#We use a stack of 1MiB, it is cleaner to implement the chkstk function to check if we need to init more pages. +add_link_options(/NODEFAULTLIB /STACK:0x100000,0x100000) +add_compile_options( + /DWIN32 /D_WINDOWS /W3 /Zl /GS- /Gs9999999 /c + "$<$:/MDd;/Zi;/Ob0;/Od>" +) +endif() + +# ============================================= +# Directories +# ============================================= +set(PROJ_BUILD_DIR ${CMAKE_BINARY_DIR}) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJ_BUILD_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJ_BUILD_DIR}/bin) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJ_BUILD_DIR}/bin) + +set(PROJ_REL_BUILD_DIR ${CMAKE_BINARY_DIR}/release) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJ_REL_BUILD_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJ_REL_BUILD_DIR}/bin) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJ_REL_BUILD_DIR}/bin) + +set(PROJ_DEB_BUILD_DIR ${CMAKE_BINARY_DIR}/debug) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJ_DEB_BUILD_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJ_DEB_BUILD_DIR}/bin) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJ_DEB_BUILD_DIR}/bin) + +if (DOCUMENTATION) +add_subdirectory(doc) +endif(DOCUMENTATION) +add_subdirectory(src) \ No newline at end of file diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt new file mode 100755 index 0000000..b12be5e --- /dev/null +++ b/doc/CMakeLists.txt @@ -0,0 +1,45 @@ +#Cmake: Documentation + +find_package(Doxygen REQUIRED doxygen) +find_program(HAS_GRAPHVIZ NAMES dot) + +if(HAS_GRAPHVIZ) +message("Graphviz found!") +set(DOXYGEN_HAVE_DOT YES) # Needs graphviz +endif() +set(DOXYGEN_CREATE_SUBDIRS YES) +set(DOXYGEN_CREATE_SUBDIRS_LEVEL 8) +set(DOXYGEN_HTML_COLORSTYLE DARK) +set(DOXYGEN_FULL_PATH_NAMES NO) +set(DOXYGEN_EXTRACT_ALL YES) +set(DOXYGEN_EXTRACT_PRIVATE YES) +set(DOXYGEN_EXTRACT_STATIC YES) +set(DOXYGEN_CALL_GRAPH YES) +set(DOXYGEN_CALLER_GRAPH YES) +set(DOXYGEN_DISABLE_INDEX YES) +set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES) +set(DOXYGEN_ALPHABETICAL_INDEX NO) +set(DOXYGEN_EXCLUDE build) +set(DOXYGEN_FILE_PATTERNS *.c *.h) +set(DOXYGEN_GENERATE_TREEVIEW YES) +set(DOXYGEN_QUIET YES) +set(DOXYGEN_RECURSIVE YES) +set(DOXYGEN_REFERENCED_BY_RELATION YES) +set(DOXYGEN_REFERENCES_RELATION YES) +set(DOXYGEN_SORT_BY_SCOPE_NAME YES) +set(DOXYGEN_SORT_MEMBER_DOCS NO) +set(DOXYGEN_SOURCE_BROWSER YES) +set(DOXYGEN_STRIP_CODE_COMMENTS NO) + +if(INTERNAL_DOC) +set(DOC_PATHS "${CMAKE_SOURCE_DIR}/src") +set(DOXYGEN_PROJECT_NAME "${CMAKE_PROJECT_NAME} Internal") +message("Internal doc enabled!") +else() +set(DOC_PATHS "${CMAKE_SOURCE_DIR}/src/rtbase/include") +message("Public doc enabled!") +endif(INTERNAL_DOC) + +doxygen_add_docs(doxygen ${DOC_PATHS} + ALL + COMMENT "Generating HTML documentation") \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100755 index 0000000..b484c25 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,5 @@ +# src directory + +add_subdirectory(based) +add_subdirectory(runtime) +add_subdirectory(cli) \ No newline at end of file diff --git a/src/based/CMakeLists.txt b/src/based/CMakeLists.txt new file mode 100755 index 0000000..4989986 --- /dev/null +++ b/src/based/CMakeLists.txt @@ -0,0 +1,16 @@ +# Source based library + +add_library(based SHARED "base.c") + +target_compile_features(based PRIVATE c_std_99) + +target_include_directories(based + PUBLIC + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}/include + PRIVATE + ${CMAKE_SOURCE_DIR}/src) + + +add_subdirectory(include) +add_subdirectory(system) \ No newline at end of file diff --git a/src/based/base.c b/src/based/base.c new file mode 100755 index 0000000..4a44aeb --- /dev/null +++ b/src/based/base.c @@ -0,0 +1,8 @@ +#include "base.h" + +// Test export function, else there will be no symbols exported on the build. +EXPORT int test() +{ + return 69; +} + diff --git a/src/based/include/CMakeLists.txt b/src/based/include/CMakeLists.txt new file mode 100755 index 0000000..c4ce907 --- /dev/null +++ b/src/based/include/CMakeLists.txt @@ -0,0 +1,6 @@ +# based => include + +target_sources(based PUBLIC + "base.h") + +add_subdirectory(env) \ No newline at end of file diff --git a/src/based/include/base.h b/src/based/include/base.h new file mode 100755 index 0000000..d676594 --- /dev/null +++ b/src/based/include/base.h @@ -0,0 +1,39 @@ +/** + * @file base_defs.h + * @brief The library general header. + * @version 0.1 + * @date 2023-01-11 + * @details Contains utilities like memory management, evironment and more. + */ + +#ifndef BASE_H +#define BASE_H +#include "env/envb.h" + +// ======================================== +// Definitions +// ======================================== + +/// Exit on success. +#define EXIT_OK 0 +/// Exit on fail. +#define EXIT_FAIL -1 +/// Exit on error. +#define EXIT_ERROR 1 + +#if !defined(bool) || !defined(BOOL) +#define bool uint8 +#define true 1 +#define false 0 +#endif + +#ifndef NULL +#define NULL ((void*)0) +#endif // NULL +#define INVALID_PTR ((void*)-1) +#define NULL_BYTE_PTR ((BYTE*)0) +#define INVALID_BYTE_PTR ((BYTE*)-1) +#define NULL_BYTE '\0' + +#define IGNORE_ARG(x) (void)x; +#endif //BASE_H \ No newline at end of file diff --git a/src/based/include/env/CMakeLists.txt b/src/based/include/env/CMakeLists.txt new file mode 100755 index 0000000..a7ae083 --- /dev/null +++ b/src/based/include/env/CMakeLists.txt @@ -0,0 +1,3 @@ +# based => include => env + +target_sources(based PRIVATE "envb.h") \ No newline at end of file diff --git a/src/based/include/env/envb.h b/src/based/include/env/envb.h new file mode 100755 index 0000000..0076167 --- /dev/null +++ b/src/based/include/env/envb.h @@ -0,0 +1,94 @@ +/** + * @file envb.h + * @brief Environment base header. + * @version 0.1 + * @date 2023-01-11 + */ + +#ifndef ENVB_H +#define ENVB_H + +// ======================================== +// Linux +// ======================================== +#ifdef __gnu_linux__ +#define OS_LINUX + +#define EXPORT __attribute__((visibility("default"))) +#define CDECL_ATT __attribute__((cdecl)) +#define NO_RETURN_ATT __attribute__((noreturn)) + +#ifdef __x86_64__ + +#if defined(__i386__) || defined(_M_IX86) +#define OS_ENV 32 +#else // defined(__i386__) || defined(_M_IX86) +#define OS_ENV 64 +#endif // defined(__i386__) || defined(_M_IX86) + +#endif // __x86_64__ +#endif // __gnu_linux__ +// ======================================== +// Windows +// ======================================== +#ifdef _WIN32 +#define OS_WINDOWS 1 +#define EXPORT __declspec(dllexport) + +#ifdef _WIN64 // _WIN64 +#define OS_ENV 64 +#else // x32 +#define OS_ENV 32 +#endif // _WIN64 +#endif // _WIN32 + +// =================================================================== +// +// Lib types +// +// =================================================================== + +typedef unsigned char uint8; +typedef short int16; +typedef unsigned short uint16; +typedef int int32; +typedef unsigned int uint32; +typedef long long int64; +typedef unsigned long long uint64; + +#if OS_ENV == 64 +typedef uint64 uintptr; +#else +typedef uint32 uintptr; +#endif + +/** + * @brief Typedef for process identifier. + */ +typedef int32 p_id; +/** + * @brief Typedef for wide char. + */ +typedef uint16 w_char; +/** + * @brief Typedef for byte. + */ +typedef uint8 BYTE; +/** + * @brief Typedef for bit field. + */ +typedef uint32 bit_field; +/** + * @brief Typedef for offset. + */ +typedef long int off_set; +/** + * @brief Typedef for usize. + */ +typedef long unsigned int usize; +/** + * @brief Typedef for ssize. + */ +typedef long signed int ssize; + +#endif //ENVB_H \ No newline at end of file diff --git a/src/based/system/CMakeLists.txt b/src/based/system/CMakeLists.txt new file mode 100755 index 0000000..228188d --- /dev/null +++ b/src/based/system/CMakeLists.txt @@ -0,0 +1,10 @@ +# based => system + +target_sources(based PRIVATE "bootstrap.c") + +if(UNIX) +add_subdirectory(unix) +endif(UNIX) +if(WIN32) +add_subdirectory(win32) +endif(WIN32) \ No newline at end of file diff --git a/src/based/system/bootstrap.c b/src/based/system/bootstrap.c new file mode 100755 index 0000000..b44a672 --- /dev/null +++ b/src/based/system/bootstrap.c @@ -0,0 +1,24 @@ +#include "base.h" +//TODO(Implement): Handle the boostrapping of the application. + +enum bootstrap_sys +{ + sys_unix = 0, + sys_win32 = 1 +}; + +/// @brief Executes the bootstrapping process. +int base_bootstrap(enum bootstrap_sys system) +{ + switch (system) + { + case sys_unix: + break; + case sys_win32: + break; + default: + return -1; + break; + } + return 0; +} \ No newline at end of file diff --git a/src/based/system/unix/CMakeLists.txt b/src/based/system/unix/CMakeLists.txt new file mode 100755 index 0000000..acaef32 --- /dev/null +++ b/src/based/system/unix/CMakeLists.txt @@ -0,0 +1,2 @@ +# based => system => unix + diff --git a/src/based/system/win32/CMakeLists.txt b/src/based/system/win32/CMakeLists.txt new file mode 100755 index 0000000..5066532 --- /dev/null +++ b/src/based/system/win32/CMakeLists.txt @@ -0,0 +1,3 @@ +# based => system => win32 + +target_sources(based PRIVATE "win32sym.c") \ No newline at end of file diff --git a/src/based/system/win32/win32sym.c b/src/based/system/win32/win32sym.c new file mode 100755 index 0000000..1e087ee --- /dev/null +++ b/src/based/system/win32/win32sym.c @@ -0,0 +1,21 @@ +#include "base.h" +#include "windows.h" + +BOOL WINAPI _DllMainCRTStartup(HINSTANCE const instance, DWORD const reason, LPVOID const reserved) // => DllMain +{ + (void)instance; + (void)reason; + (void)reserved; + //return DllMain(instance, reason, reserved); + return TRUE; +} + +BOOL WINAPI _RTC_InitBase() +{ + return TRUE; +} + +BOOL WINAPI _RTC_Shutdown() +{ + return TRUE; +} \ No newline at end of file diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt new file mode 100755 index 0000000..c997610 --- /dev/null +++ b/src/cli/CMakeLists.txt @@ -0,0 +1,5 @@ +# cli project + +add_executable(basedcli "main.c") + +target_link_libraries(basedcli runtime) \ No newline at end of file diff --git a/src/cli/main.c b/src/cli/main.c new file mode 100755 index 0000000..c15bfea --- /dev/null +++ b/src/cli/main.c @@ -0,0 +1,6 @@ +#include "base.h" + +int main(int argc, char* argv[], char* envp[]) +{ + return 0; +} \ No newline at end of file diff --git a/src/runtime/CMakeLists.txt b/src/runtime/CMakeLists.txt new file mode 100755 index 0000000..0e6e4db --- /dev/null +++ b/src/runtime/CMakeLists.txt @@ -0,0 +1,15 @@ +# runtime library + +add_library(runtime STATIC + "rtinternal.h" + "runtime.c") + +target_link_libraries(runtime PUBLIC based) + +target_include_directories(runtime + PUBLIC + ${CMAKE_SOURCE_DIR}/include + PRIVATE + ${CMAKE_SOURCE_DIR}/src) + +add_subdirectory(system) \ No newline at end of file diff --git a/src/runtime/rtinternal.h b/src/runtime/rtinternal.h new file mode 100755 index 0000000..010f733 --- /dev/null +++ b/src/runtime/rtinternal.h @@ -0,0 +1,3 @@ +#include "base.h" + +extern int main(int argc, BYTE** argv, BYTE** envp); diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c new file mode 100755 index 0000000..39912b8 --- /dev/null +++ b/src/runtime/runtime.c @@ -0,0 +1,9 @@ +#include "rtinternal.h" + +int rt_bootstrap(void) +{ + // Bootstraping => based. + // Invoke main. + // Return exit code to caller. + return 420; +} \ No newline at end of file diff --git a/src/runtime/system/CMakeLists.txt b/src/runtime/system/CMakeLists.txt new file mode 100755 index 0000000..29a40bc --- /dev/null +++ b/src/runtime/system/CMakeLists.txt @@ -0,0 +1,8 @@ +# runtime => system + +if(UNIX) +add_subdirectory(unix) +endif() +if(WIN32) +add_subdirectory(win32) +endif() \ No newline at end of file diff --git a/src/runtime/system/unix/CMakeLists.txt b/src/runtime/system/unix/CMakeLists.txt new file mode 100755 index 0000000..b986b88 --- /dev/null +++ b/src/runtime/system/unix/CMakeLists.txt @@ -0,0 +1,3 @@ +# runtime => system => unix + +target_sources(runtime PRIVATE "unixrt.c" "crt.S") \ No newline at end of file diff --git a/src/runtime/system/unix/crt.S b/src/runtime/system/unix/crt.S new file mode 100755 index 0000000..77109ab --- /dev/null +++ b/src/runtime/system/unix/crt.S @@ -0,0 +1,17 @@ +//#include "../syscalls.h" + +.globl _start +.globl rt_unix_start + +.text + +// Start point unix. +_start: + xor %rbp, %rbp // Clear stack frame pointer + + mov (%rsp), %rdi // Movq argc to rdi + lea 0x8(%rsp), %rsi // Get the argv address + lea 16(%rsp,%rdi,8), %rdx // Get the envp address + + call rt_unix_start // Start c function + hlt // Halt if the lib or app does not call the exit function. \ No newline at end of file diff --git a/src/runtime/system/unix/unixrt.c b/src/runtime/system/unix/unixrt.c new file mode 100755 index 0000000..b75485f --- /dev/null +++ b/src/runtime/system/unix/unixrt.c @@ -0,0 +1,6 @@ +#include "base.h" + +void rt_unix_start(int argc, BYTE** argv, BYTE** envp) +{ + //TODO: Callback => main & syscall to exit group +} \ No newline at end of file diff --git a/src/runtime/system/win32/CMakeLists.txt b/src/runtime/system/win32/CMakeLists.txt new file mode 100755 index 0000000..e846948 --- /dev/null +++ b/src/runtime/system/win32/CMakeLists.txt @@ -0,0 +1,3 @@ +# runtime => system => win32 + +target_sources(runtime PRIVATE "winrt.c") \ No newline at end of file diff --git a/src/runtime/system/win32/winrt.c b/src/runtime/system/win32/winrt.c new file mode 100755 index 0000000..7c974ae --- /dev/null +++ b/src/runtime/system/win32/winrt.c @@ -0,0 +1,59 @@ +#include "base.h" +#include "windows.h" + +// Windows(Win32) of course needs to be different... + +// Use the wide entry +#pragma comment(linker,"/ENTRY:wmainCRTStartup") + +// Main +//extern int WINAPI mainCRTStartup(); +int WINAPI mainCRTStartup() // => main +{ + return 0; +} +//extern int __CRTDECL wmain(int argc, wchar_t** argv, wchar_t** envp); +int WINAPI wmainCRTStartup() // => wmain +{ + return 0; +} + +// WinMain +//extern int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd); +int WINAPI WinMainCRTStartup() // => WinMain +{ + return 0; +} + +//extern int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd); +int wWinMainCRTStartup() // => wWinMain +{ + return 0; +} + +// RTC stuff +BOOL WINAPI _RTC_InitBase() +{ + return TRUE; +} + +BOOL WINAPI _RTC_Shutdown() +{ + return TRUE; +} + +// Library +/* #ifdef MRTDLL +extern BOOL __clrcall DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved); +#else +extern BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved); +#endif // MRTDLL */ + +BOOL WINAPI _DllMainCRTStartup(HINSTANCE const instance, DWORD const reason, LPVOID const reserved) // => DllMain +{ + (void)instance; + (void)reason; + (void)reserved; + //TODO: Handle dllmain + return TRUE; +} \ No newline at end of file