summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-07-14 10:54:38 +0200
committerJo-Philipp Wich <jo@mein.io>2023-08-09 01:03:21 +0200
commitbe071072115059726846163c6f28f62dc01573ec (patch)
tree1bbf6fe1140e6305c86b621d580b9999706e82b2 /include
parenta38315454add264a41094524e1fcf435acb85fe8 (diff)
treewide: consolidate platform specific code in platform.c
Get rid of most __APPLE__ guards by introducing a central platform.c unit providing drop-in replacements for missing APIs. Also move system signal definitions into the new platform file to be able to share them with the upcoming debug library. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'include')
-rw-r--r--include/endian.h23
-rw-r--r--include/ucode/platform.h79
-rw-r--r--include/ucode/types.h3
3 files changed, 81 insertions, 24 deletions
diff --git a/include/endian.h b/include/endian.h
deleted file mode 100644
index 198cf7c..0000000
--- a/include/endian.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifdef __APPLE__
-
-# include <machine/endian.h>
-# include <libkern/OSByteOrder.h>
-
-# define htobe16(x) OSSwapHostToBigInt16(x)
-# define htole16(x) OSSwapHostToLittleInt16(x)
-# define be16toh(x) OSSwapBigToHostInt16(x)
-# define le16toh(x) OSSwapLittleToHostInt16(x)
-
-# define htobe32(x) OSSwapHostToBigInt32(x)
-# define htole32(x) OSSwapHostToLittleInt32(x)
-# define be32toh(x) OSSwapBigToHostInt32(x)
-# define le32toh(x) OSSwapLittleToHostInt32(x)
-
-# define htobe64(x) OSSwapHostToBigInt64(x)
-# define htole64(x) OSSwapHostToLittleInt64(x)
-# define be64toh(x) OSSwapBigToHostInt64(x)
-# define le64toh(x) OSSwapLittleToHostInt64(x)
-
-#else
-# include_next <endian.h>
-#endif
diff --git a/include/ucode/platform.h b/include/ucode/platform.h
new file mode 100644
index 0000000..52eb391
--- /dev/null
+++ b/include/ucode/platform.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2023 Jo-Philipp Wich <jo@mein.io>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef UCODE_PLATFORM_H
+#define UCODE_PLATFORM_H
+
+#include <signal.h>
+#include <fcntl.h>
+#include <time.h>
+
+#include "ucode/util.h"
+
+#ifdef NSIG
+# define UC_SYSTEM_SIGNAL_COUNT NSIG
+#else
+# define UC_SYSTEM_SIGNAL_COUNT (_SIGMAX + 1)
+#endif
+
+extern const char *uc_system_signal_names[];
+
+#if defined(__linux__)
+# include <endian.h>
+# include <sys/sysmacros.h>
+#elif defined(__APPLE__)
+# include <unistd.h>
+# include <crt_externs.h>
+# include <machine/endian.h>
+# include <libkern/OSByteOrder.h>
+
+# define htobe16(x) OSSwapHostToBigInt16(x)
+# define htole16(x) OSSwapHostToLittleInt16(x)
+# define be16toh(x) OSSwapBigToHostInt16(x)
+# define le16toh(x) OSSwapLittleToHostInt16(x)
+
+# define htobe32(x) OSSwapHostToBigInt32(x)
+# define htole32(x) OSSwapHostToLittleInt32(x)
+# define be32toh(x) OSSwapBigToHostInt32(x)
+# define le32toh(x) OSSwapLittleToHostInt32(x)
+
+# define htobe64(x) OSSwapHostToBigInt64(x)
+# define htole64(x) OSSwapHostToLittleInt64(x)
+# define be64toh(x) OSSwapBigToHostInt64(x)
+# define le64toh(x) OSSwapLittleToHostInt64(x)
+
+# define environ (*_NSGetEnviron())
+
+__hidden int pipe2(int[2], int);
+__hidden int sigtimedwait(const sigset_t *, siginfo_t *, const struct timespec *);
+
+static inline int
+execvpe(const char *program, char **argv, char **envp)
+{
+ char **saved = environ;
+ int rc;
+
+ environ = envp;
+ rc = execvp(program, argv);
+ environ = saved;
+
+ return rc;
+}
+#else
+# error Unsupported platform
+#endif
+
+#endif /* UCODE_PLATFORM_H */
diff --git a/include/ucode/types.h b/include/ucode/types.h
index 641c469..7fcc7f1 100644
--- a/include/ucode/types.h
+++ b/include/ucode/types.h
@@ -24,6 +24,7 @@
#include <json-c/json.h>
#include "util.h"
+#include "platform.h"
/* Value types and generic value header */
@@ -314,7 +315,7 @@ struct uc_vm {
uc_exception_handler_t *exhandler;
FILE *output;
struct {
- uint64_t raised[((NSIG + 63) & ~63) / 64];
+ uint64_t raised[((UC_SYSTEM_SIGNAL_COUNT + 63) & ~63) / 64];
uc_value_t *handler;
struct sigaction sa;
int sigpipe[2];