From 33197faf9b626acb6c2815786e6caa316df64a33 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sun, 11 Feb 2024 23:29:09 -0700 Subject: Move Main.hs to Config.hs and override _start() Now if one tries to execute the built binary with 'stack run' or by just executing it, a helpful message prints to the screen instead of the segmentation fault that normally happens. This technically makes things not portable to other architectures, but it's all just window dressing and can be taken out if need be for other architectures. --- src/Config.hs | 8 ++++++++ src/Main.hs | 17 ----------------- src/harness_adapter.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 src/Config.hs delete mode 100644 src/Main.hs (limited to 'src') diff --git a/src/Config.hs b/src/Config.hs new file mode 100644 index 0000000..14326cb --- /dev/null +++ b/src/Config.hs @@ -0,0 +1,8 @@ +module Config () where + +import Wetterhorn.Core + +foreign export ccall wetterhorn :: IO Wetterhorn + +wetterhorn :: IO Wetterhorn +wetterhorn = initWetterhorn defaultConfig diff --git a/src/Main.hs b/src/Main.hs deleted file mode 100644 index 03b5018..0000000 --- a/src/Main.hs +++ /dev/null @@ -1,17 +0,0 @@ -{-# HLINT ignore "Use camelCase" #-} - -module Main (main) where - -import Control.Monad.Writer (MonadWriter (tell), execWriter) -import Text.Printf -import Wetterhorn.Core - -foreign export ccall wetterhorn :: IO Wetterhorn - -wetterhorn :: IO Wetterhorn -wetterhorn = - initWetterhorn - defaultConfig - -main :: IO () -main = putStrLn "This should be dynamically linked!\n" diff --git a/src/harness_adapter.c b/src/harness_adapter.c index 5a9b9a4..6ae8c66 100644 --- a/src/harness_adapter.c +++ b/src/harness_adapter.c @@ -5,8 +5,43 @@ #include "HsFFI.h" #include "plugin_interface.h" +#include const char *plugin_name = "Wetterhorn"; void plugin_load(int argc, char **argv) { hs_init(&argc, &argv); } void plugin_teardown(opqst_t st) { hs_exit(); } + +const char msg[] = + "Wetterhorn Plugin v 0.01\n" + " This is a mere shared object file, meant to be a plugin for the " + "Wetterhorn" + " Compositor. To use this, please run 'wtr_harness -p [this_file]' to" + " use this plugin.\n"; +const int msg_sz = sizeof(msg); + + +__attribute__((naked)) void _start() { + + // Make system call to print the message + asm( + // Load the address of the string into rsi + "mov %0, %%rsi\n" + // Load the string length into edx + "mov %1, %%edx\n" + // Load the file descriptor for stdout into edi + "mov $1, %%edi\n" + // Load the syscall number for sys_write into eax + "mov $1, %%eax\n" + // Make the syscall + "syscall\n" + + // Exit the program. + "mov $0, %%rdi\n" + "mov $60, %%rax\n" + "syscall\n" + : + : "r"(msg), "r"(msg_sz) // Input: address of msg + : "%rsi", "%edx", "%edi" // Clobbered registers + ); +} -- cgit