From 5347510e990d410512ff0730c89e4fdaf6c9bdd2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 13 Jul 2021 21:00:31 +0000 Subject: [PATCH] Don't explicitly override __cxa_thread_atexit_impl, rely on LLVM to optimize it out --- genbindings.sh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/genbindings.sh b/genbindings.sh index 15a574da..741f9de1 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -87,13 +87,13 @@ else LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a if [ "$IS_MAC" = "false" ]; then COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld" - echo "// __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd." >> src/main/jni/bindings.c - echo "// However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and fallback in case it is missing." >> src/main/jni/bindings.c - echo "// Because it is weak-linked on the rust side, we should be able to simply define it explicitly here, forcing rust to use the fallback." >> src/main/jni/bindings.c - echo "void *__cxa_thread_atexit_impl = NULL;" >> src/main/jni/bindings.c - # Note that the above is not sufficient. For some reason involving ancient dark magic and - # haunted code segments, overriding the weak symbol only impacts sites which *call* the - # symbol in question, not sites which *compare with* the symbol in question. + # __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd. + # However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and + # fallback in case it is missing. + # Because it is weak-linked on the rust side, we should be able to simply define it + # explicitly, forcing rust to use the fallback. However, for some reason involving ancient + # dark magic and haunted code segments, overriding the weak symbol only impacts sites which + # *call* the symbol in question, not sites which *compare with* the symbol in question. # This means that the NULL check in rust's libstd will always think the function is # callable while the function which is called ends up being NULL (leading to a jmp to the # zero page and a quick SEGFAULT). @@ -104,6 +104,9 @@ else # After exhausting nearly every flag documented in lld, the only reliable method appears # to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can # disassemble it into very readable code, edit it, and then reassemble it. + # Note that if we do so we don't have to bother overriding the actual call, LLVM should + # optimize it away, which also provides a good check that there isn't anything actually + # relying on it elsewhere. [ ! -f "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a ] && exit 1 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep -v "\.o$" || echo)" != "" ]; then echo "Archive contained non-object files!" -- 2.30.2