From e531c0179299de7fbaa84dda39ab1bd289cecf88 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 25 Apr 2025 15:11:33 -0400 Subject: Speculative fix for Mac: use llvm-objcopy instead of patchelf for Mach-O shared library files --- Makefile | 11 ++++++++--- src/tomo.c | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 75ed5ef7..fd51f8d3 100644 --- a/Makefile +++ b/Makefile @@ -155,11 +155,16 @@ examples: deps: bash ./install_dependencies.sh -check-utilities: +check-utilities: check-c-compiler @which objcopy 2>/dev/null >/dev/null \ || { printf '\033[31;1m%s\033[m\n' "I couldn't find 'objcopy' on your system! Try installing the package 'binutils' with your package manager."; exit 1; } - @which patchelf 2>/dev/null >/dev/null \ - || { printf '\033[31;1m%s\033[m\n' "I couldn't find 'patchelf' on your system! Try installing the package 'binutils' with your package manager."; exit 1; } + @if echo | $(DEFAULT_C_COMPILER) -dM -E - | grep -q '__ELF__'; then \ + which patchelf 2>/dev/null >/dev/null \ + || { printf '\033[31;1m%s\033[m\n' "I couldn't find 'patchelf' on your system! Try installing the package 'binutils' with your package manager."; exit 1; }; \ + else \ + which llvm-objcopy 2>/dev/null >/dev/null \ + || { printf '\033[31;1m%s\033[m\n' "I couldn't find 'llvm-objcopy' on your system! Try installing the package 'llvm' with your package manager."; exit 1; }; \ + fi @which nm 2>/dev/null >/dev/null \ || { printf '\033[31;1m%s\033[m\n' "I couldn't find 'nm' on your system! Try installing the package 'binutils' with your package manager."; exit 1; } @which awk 2>/dev/null >/dev/null \ diff --git a/src/tomo.c b/src/tomo.c index 1c4f39f9..9f273ab3 100644 --- a/src/tomo.c +++ b/src/tomo.c @@ -495,10 +495,19 @@ void build_library(Text_t lib_dir_name) if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) errx(WEXITSTATUS(status), "Failed to run `objcopy` to add library prefix to symbols"); +#if defined(__ELF__) prog = run_cmd("patchelf --rename-dynamic-symbols .build/symbol_renames.txt 'lib", lib_dir_name, SHARED_SUFFIX, "'"); status = pclose(prog); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) errx(WEXITSTATUS(status), "Failed to run `patchelf` to rename dynamic symbols with library prefix"); +#elif defined(__MACH__) + prog = run_cmd("llvm-objcopy --redefine-syms=.build/symbol_renames.txt 'lib", lib_dir_name, SHARED_SUFFIX, "'"); + status = pclose(prog); + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) + errx(WEXITSTATUS(status), "Failed to run `llvm-objcopy` to rename dynamic symbols with library prefix"); +#else +#error "Unknown platform (not ELF or MACH)" +#endif if (verbose) print("Successfully renamed symbols with library prefix!"); -- cgit v1.2.3