From 7240505bd1158f6633e4e9dac4af2fe07a27ab11 Mon Sep 17 00:00:00 2001 From: interfiberschool <117294755+interfiberschool@users.noreply.github.com> Date: Thu, 16 Mar 2023 18:04:29 +0000 Subject: [PATCH] init --- .devcontainer/devcontainer.json | 19 +++++++++++++++++ .gitignore | 1 + Makefile | 17 +++++++++++++++ README.md | 1 - examples/unix/lib.c | 5 +++++ examples/unix/main.c | 6 ++++++ examples/win/lib.c | 5 +++++ examples/win/main.c | 6 ++++++ src/dlopen.c | 32 ++++++++++++++++++++++++++++ src/dlopen.h | 37 +++++++++++++++++++++++++++++++++ 10 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitignore create mode 100644 Makefile delete mode 100644 README.md create mode 100644 examples/unix/lib.c create mode 100644 examples/unix/main.c create mode 100644 examples/win/lib.c create mode 100644 examples/win/main.c create mode 100644 src/dlopen.c create mode 100644 src/dlopen.h diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..009a80f --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,19 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/debian +{ + "name": "Debian", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/base:bullseye" + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5e82d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f631e83 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +exampleUnix: examples/unix/main.c examples/unix/lib.c src/dlopen.c + ## BUILD UNIX EXAMPLES ## + + mkdir -p bin + + gcc examples/unix/main.c src/dlopen.c -o bin/exampleUnix -ldl + gcc examples/unix/lib.c -shared -o bin/exampleUnixLib.dll + +exampleWin: examples/win/main.c examples/win/lib.c src/dlopen.c + ## BUILD WINDOWS EXAMPLES ## + + mkdir -p bin + + x86_64-w64-mingw32-gcc examples/win/main.c src/dlopen.c -o bin/exampleWin + x86_64-w64-mingw32-gcc examples/win/lib.c -shared -o bin/exampleWinLib.dll + +all: exampleUnix exampleWin \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 8b13789..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/examples/unix/lib.c b/examples/unix/lib.c new file mode 100644 index 0000000..e3f6573 --- /dev/null +++ b/examples/unix/lib.c @@ -0,0 +1,5 @@ +#include + +void test(){ + printf("testing 123\n"); +} \ No newline at end of file diff --git a/examples/unix/main.c b/examples/unix/main.c new file mode 100644 index 0000000..d2f98d9 --- /dev/null +++ b/examples/unix/main.c @@ -0,0 +1,6 @@ +#include "../../src/dlopen.h" + +int main() +{ + struct hotwire_dll_t dll = hw_dlopen("./bin/exampleUnixLib.dll", RTLD_NOW); +} \ No newline at end of file diff --git a/examples/win/lib.c b/examples/win/lib.c new file mode 100644 index 0000000..b03eff9 --- /dev/null +++ b/examples/win/lib.c @@ -0,0 +1,5 @@ +#include + +void test(){ + printf("testing 123(windows)\n"); +} \ No newline at end of file diff --git a/examples/win/main.c b/examples/win/main.c new file mode 100644 index 0000000..d709de1 --- /dev/null +++ b/examples/win/main.c @@ -0,0 +1,6 @@ +#include "../../src/dlopen.h" + +int main() +{ + struct hotwire_dll_t dll = hw_dlopen("./bin/exampleWinLib.dll", 0); +} \ No newline at end of file diff --git a/src/dlopen.c b/src/dlopen.c new file mode 100644 index 0000000..a22c98e --- /dev/null +++ b/src/dlopen.c @@ -0,0 +1,32 @@ +#include "dlopen.h" +#include + +struct hotwire_dll_t hw_dlopen(const char *file, int flags) { + + struct hotwire_dll_t dll; + +// UNIX specific code(MacOS uses the same API as linux for dlopen) +#if defined(__linux__) || defined(__APPLE__) + dll.dll_handle = dlopen(file, flags); + + if (dll.dll_handle == NULL){ + printf("could not load dll(UNIX api call returned error)\n"); + printf("error message: %s\n", dlerror()); + + exit(-1); + } +#endif + +// Windows specific code +#if defined(_WIN32) + dll.dll_handle = LoadLibrary(file); + + if (!dll.dll_handle){ + printf("could not load dll(windows api call returned error)\n"); + + exit(-1); + } +#endif + + return dll; +} \ No newline at end of file diff --git a/src/dlopen.h b/src/dlopen.h new file mode 100644 index 0000000..0b0fdd5 --- /dev/null +++ b/src/dlopen.h @@ -0,0 +1,37 @@ +#pragma once +#include + +#if defined(__APPLE__) +#define HW_MODE 0 +#include +#endif + +#if defined(__linux__) +#define HW_MODE 1 +#include +#endif + +#if defined(_WIN32) +#define HW_MODE 2 +#include +#endif + + +struct hotwire_dll_t { + #if defined(__linux__) || defined(__APPLE__) + void* dll_handle; + #endif + + #if defined(_WIN32) + HINSTANCE dll_handle; + #endif +}; + +// Function arguments are based off the dlopen() api on POSIX based operating systems +// Loads dll from library +struct hotwire_dll_t hw_dlopen(const char* file, int flags); + +// Loads symbol from DLL +inline void* hw_dlsym(struct hotwire_dll_t dll, const char *__restrict symbol); + +inline int hw_dlclose(void* handle);