From 4d32b138ed7379263e0b90f6421a17d513859c10 Mon Sep 17 00:00:00 2001 From: Landon Curt Noll Date: Thu, 17 Aug 2023 14:23:29 -0700 Subject: [PATCH] change libcalc dynamic library to link with libcustcalc Under macOS, to reduce dependency chains, we remove functions and data that are unreachable by the entry point or exported symbols. In particular, the macOS linker is used with both "-dead_strip" and "-dead_strip_dylibs". The libcalc shared library is now linked with libcustcalc. --- CHANGES | 11 +++++++++++ Makefile | 4 ++-- Makefile.target | 15 --------------- calc.c | 32 ++++++++++---------------------- custom.c | 16 ++++++++++++++++ custom.h | 2 +- custom/custtbl.c | 29 ++++++++++++++++++----------- 7 files changed, 58 insertions(+), 51 deletions(-) diff --git a/CHANGES b/CHANGES index c48fe8e..631b4b9 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,14 @@ +The following are the changes from calc version 2.14.3.4 to date: + + Under macOS, to reduce dependency chains, we remove functions + and data that are unreachable by the entry point or exported + symbols. In particular, the macOS linker is used with both + "-dead_strip" and "-dead_strip_dylibs". + + The libcalc shared library is now linked with libcustcalc. + + + The following are the changes from calc version 2.14.3.1 to 2.14.3.4: Fix typo in the `make debug` Makefile rule. diff --git a/Makefile b/Makefile index b29a4fb..8889f21 100644 --- a/Makefile +++ b/Makefile @@ -475,8 +475,8 @@ calc${EXT}: .hsrc ${CALCOBJS} ${CALC_DYNAMIC_LIBS} ${MK_SET} ${CC} ${CALCOBJS} ${LDFLAGS} ${LD_SHARE} ${CALC_DYNAMIC_LIBS} \ ${READLINE_LIB} ${READLINE_EXTRAS} -o $@ -libcalc${LIB_EXT_VERSION}: ${LIBOBJS} ver_calc${EXT} ${MK_SET} - ${CC} ${LIBCALC_SHLIB} ${LIBOBJS} \ +libcalc${LIB_EXT_VERSION}: ${LIBOBJS} ver_calc${EXT} ${MK_SET} libcustcalc${LIB_EXT_VERSION} + ${CC} ${LIBCALC_SHLIB} ${LIBOBJS} libcustcalc${LIB_EXT_VERSION} \ ${READLINE_LIB} ${READLINE_EXTRAS} -o libcalc${LIB_EXT_VERSION} libcalc${LIB_EXT}: libcalc${LIB_EXT_VERSION} diff --git a/Makefile.target b/Makefile.target index 51dfc2d..e4dd24e 100644 --- a/Makefile.target +++ b/Makefile.target @@ -177,22 +177,7 @@ LIBCUSTCALC_SHLIB= -dynamiclib -undefined dynamic_lookup \ # data that are unreachable by the entry point or exported symbols. # COMMON_LDFLAGS+= -Wl,-dead_strip - -# Do not link with "-dead_strip_dylibs" if CUSTOM is defined -# (i.e., ${ALLOW_CUSTOM} is "-DCUSTOM). This is because unless -# the application makes an explicit symbol reference to some symbol -# in libcustcalc, the application will fail to execute due to -# a missing symbol. -# -# The calc application will run with CUSTOM defined under -# "-dead_strip_dylibs" because calc.o (in main()) makes an explicit -# reference to custtbl_allowed (see custom/custtbl.c) in libcustcalc. -# However applications such as sample_many and sample_rand will -# fail to load when CUSTOM is defined and "-dead_strip_dylibs" is used.. -# -ifneq ($(ALLOW_CUSTOM),-DCUSTOM) COMMON_LDFLAGS+= -Wl,-dead_strip_dylibs -endif # ($(ALLOW_CUSTOM),-DCUSTOM) # static library option # diff --git a/calc.c b/calc.c index dd371cc..742c20c 100644 --- a/calc.c +++ b/calc.c @@ -164,19 +164,13 @@ main(int argc, char **argv) switch (c) { case 'C': #if defined(CUSTOM) + /* - * validate custtbl_allowed value - * - * We make an explicit reference to the custtbl_allowed symbol - * in libcustcalc (see custom/custtbl.c) so that the use - * of "-dead_strip_dylibs" with the macOS linker won't cause - * the calc to fail to load due to a missing symbol. + * error if libcustcalc was compiled with CUSTOM undefined */ - if (custtbl_allowed == 0) { - fprintf(stderr, "%s: calc was built with " - "custom functions enabled, " - "but custtbl_allowed: %d == 0\n", - program, custtbl_allowed); + if (custom_compiled() == 0) { + fprintf(stderr, "%s: calc was built with custom functions enabled, " + "however custom_compiled() retuned 0", program); exit(1); } @@ -186,19 +180,13 @@ main(int argc, char **argv) allow_custom = TRUE; break; #else /* CUSTOM */ + /* - * validate custtbl_allowed value - * - * We make an explicit reference to the custtbl_allowed symbol - * in libcustcalc (see custom/custtbl.c) so that the use - * of "-dead_strip_dylibs" with the macOS linker won't cause - * the calc to fail to load due to a missing symbol. + * error if libcustcalc was compiled with CUSTOM defined */ - if (custtbl_allowed != 0) { - fprintf(stderr, "%s: calc was built with " - "custom functions disabled, " - "but custtbl_allowed: %d != 0\n", - program, custtbl_allowed); + if (custom_compiled() == 1) { + fprintf(stderr, "%s: calc was built with custom functions disabled, " + "however custom_compiled() retuned 1", program); } /* diff --git a/custom.c b/custom.c index 1638db2..f9feedf 100644 --- a/custom.c +++ b/custom.c @@ -68,6 +68,14 @@ custom(char *name, int count, VALUE **vals) CONST struct custom *p; /* current function */ + /* + * error if libcustcalc was compiled with CUSTOM undefined + */ + if (custom_compiled() == 0) { + math_error("libcustcalc was compiled CUSTOM undefined"); + not_reached(); + } + /* * search the custom interface table for a function */ @@ -100,6 +108,14 @@ custom(char *name, int count, VALUE **vals) #else /* CUSTOM */ + /* + * error if libcustcalc was compiled with CUSTOM defined + */ + if (custom_compiled() == 1) { + math_error("libcustcalc was compiled with CUSTOM defined"); + not_reached(); + } + fprintf(stderr, "%sCalc was built with custom functions disabled\n", (conf->tab_ok ? "\t" : "")); diff --git a/custom.h b/custom.h index 94f1707..d5aa9ad 100644 --- a/custom.h +++ b/custom.h @@ -73,7 +73,7 @@ struct custom { * * These are the required interfaces. The dummy.c stubs these interfaces too. */ -EXTERN int custtbl_allowed; /* 1 ==> CUSTOM defined, 0 ==> CUSTOM undefined */ +E_FUNC int custom_compiled(void); /* return 1 ==> libcustcalc compiled w/CUSTOM defined, else return 0 */ E_FUNC VALUE custom(char*, int, VALUE**); /* master custom interface */ EXTERN BOOL allow_custom; /* TRUE => custom builtins allowed */ E_FUNC void showcustom(void); /* print custom functions */ diff --git a/custom/custtbl.c b/custom/custtbl.c index 71c6b25..e2c16ba 100644 --- a/custom/custtbl.c +++ b/custom/custtbl.c @@ -25,17 +25,6 @@ */ -/* - * ISO C requires a translation unit to contain at least one declaration, - * so we declare a global variable whose value is based on if CUSTOM is defined. - */ -#if defined(CUSTOM) -int custtbl_allowed = 1; /* CUSTOM defined */ -#else /* CUSTOM */ -int custtbl_allowed = 0; /* CUSTOM undefined */ -#endif /* CUSTOM */ - - #include #include "../have_const.h" @@ -46,6 +35,24 @@ int custtbl_allowed = 0; /* CUSTOM undefined */ #include "../banned.h" /* include after system header <> includes */ +/* + * custom_compiled - determine if custom functions are compiled into libcustcalc + * + * returns: +s* 1 ==> libcustcalc was compiled with CUSTOM defined + * 0 ==> libcustcalc was compiled with CUSTOM undefined + */ +E_FUNC int +custom_compiled(void) +{ +#if defined(CUSTOM) + return 1; +#else /* CUSTOM */ + return 0; +#endif /* CUSTOM */ +} + + /* * NOTE: See the file HOW_TO_ADD for instructions on how to add custom functions. */