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.
This commit is contained in:
Landon Curt Noll
2023-08-17 14:23:29 -07:00
parent 3cd8fd7053
commit 4d32b138ed
7 changed files with 58 additions and 51 deletions

11
CHANGES
View File

@@ -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.

View File

@@ -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}

View File

@@ -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
#

32
calc.c
View File

@@ -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);
}
/*

View File

@@ -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" : ""));

View File

@@ -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 */

View File

@@ -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 <unistd.h>
#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.
*/