From adfd238ee23ca31d9e69832b9193c5a3b79ad932 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sat, 21 Jan 2023 20:11:42 -0800 Subject: [PATCH] Updating build to hopefully allow OSX builds. dlsym opcode modified so that 0 for library is converted to RTLD_DEFAULT as the two don't match on Darwin. Made errno an opcode, as previous method depended on Linux internals. [1] https://opensource.apple.com/source/xnu/xnu-201/osfmk/libsa/errno.h.auto.html [2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html Placed errno in internals and then alias inside posix vocabulary, as it seemed a waste to have another builtin stub for one symbol. Arguably maybe dlsym should be that way too, but leaving for now. Added OS make variable to conditionally set flags. Set stripping / minimization options based on it. Based off of https://github.com/flagxor/ueforth/pull/3 Thanks Ulrich! --- Makefile | 32 ++++++++++++++++++++++++-------- posix/main.c | 6 +++++- posix/posix.fs | 5 +++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 8be2f6e..b90be2b 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,8 @@ ESP32 = $(OUT)/esp32 ESP32_SIM = $(OUT)/esp32-sim DEPLOY = $(OUT)/deploy +OS = $(shell uname -s) + CFLAGS_COMMON = -O2 -I ./ -I $(OUT) CFLAGS_MINIMIZE = \ @@ -37,7 +39,7 @@ CFLAGS_MINIMIZE = \ -ffreestanding \ -fno-stack-protector \ -fomit-frame-pointer \ - -fno-ident -Wl,--build-id=none \ + -fno-ident \ -ffunction-sections -fdata-sections \ -fmerge-all-constants CFLAGS = $(CFLAGS_COMMON) \ @@ -47,13 +49,27 @@ CFLAGS = $(CFLAGS_COMMON) \ -Werror \ -no-pie \ -Wl,--gc-sections -STRIP_ARGS = -S \ - --strip-unneeded \ - --remove-section=.note.gnu.gold-version \ - --remove-section=.comment \ - --remove-section=.note \ - --remove-section=.note.gnu.build-id \ - --remove-section=.note.ABI-tag +ifeq ($(OS),Darwin) + CFLAGS += -Wl,-dead_strip -D_GNU_SOURCE +endif +ifeq ($(OS),Linux) + CFLAGS_MINIMIZE += -Wl,--build-id=none + CFLAGS += -s -Wl,--gc-sections -no-pie -Wl,--build-id=none +endif + +STRIP_ARGS = -S +ifeq ($(OS),Darwin) + STRIP_ARGS += -x +endif +ifeq ($(OS),Linux) + STRIP_ARGS += --strip-unneeded \ + --remove-section=.note.gnu.gold-version \ + --remove-section=.comment \ + --remove-section=.note \ + --remove-section=.note.gnu.build-id \ + --remove-section=.note.ABI-tag +endif + LIBS=-ldl WIN_CFLAGS = $(CFLAGS_COMMON) \ diff --git a/posix/main.c b/posix/main.c index 9c5cfce..ee36170 100644 --- a/posix/main.c +++ b/posix/main.c @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include "common/tier0_opcodes.h" @@ -24,8 +25,11 @@ #define HEAP_SIZE (10 * 1024 * 1024) #define STACK_CELLS (8 * 1024) +// NOTE: errno implemented as opcode to avoid a Linux platform dependency. + #define PLATFORM_OPCODE_LIST \ - Y(DLSYM, tos = (cell_t) dlsym(a1, c0); --sp) \ + Y(DLSYM, tos = (cell_t) dlsym(a1 ? a1 : RTLD_DEFAULT, c0); NIP) \ + XV(internals, "errno", ERRNO_INTERNAL, DUP; tos = (cell_t) errno) \ CALLING_OPCODE_LIST \ FLOATING_POINT_LIST diff --git a/posix/posix.fs b/posix/posix.fs index 57cc2ac..fd01cfd 100644 --- a/posix/posix.fs +++ b/posix/posix.fs @@ -69,8 +69,9 @@ z" readdir" 1 sysfunc readdir : .d_name ( a -- z ) 19 + ; ( Errno ) -z" __errno_location" 0 sysfunc __errno_location -: errno ( -- n ) __errno_location sl@ ; +also internals +: errno ( -- n ) errno ; +previous ( Default Pipes ) 0 constant stdin