@@ -84,18 +84,49 @@ if [ "$3" = "true" ]; then
8484 [ " $IS_MAC " = " false" ] && COMPILE=" $COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free"
8585 $COMPILE -o liblightningjni_debug$LDK_TARGET_SUFFIX .so -g -fsanitize=address -shared-libasan -rdynamic -I" $1 " /lightning-c-bindings/include/ $2 src/main/jni/bindings.c " $1 " /lightning-c-bindings/target/$LDK_TARGET /debug/libldk.a -lm
8686else
87+ LDK_LIB=" $1 " /lightning-c-bindings/target/$LDK_TARGET /release/libldk.a
8788 if [ " $IS_MAC " = " false" ]; then
8889 COMPILE=" $COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
8990 echo " // __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd." >> src/main/jni/bindings.c
9091 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
91- echo " // Because it is weak-linked on the rust side, we can simply define it explicitly here, forcing rust to use the fallback." >> src/main/jni/bindings.c
92+ 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
9293 echo " void *__cxa_thread_atexit_impl = NULL;" >> src/main/jni/bindings.c
94+ # Note that the above is not sufficient. For some reason involving ancient dark magic and
95+ # haunted code segments, overriding the weak symbol only impacts sites which *call* the
96+ # symbol in question, not sites which *compare with* the symbol in question.
97+ # This means that the NULL check in rust's libstd will always think the function is
98+ # callable while the function which is called ends up being NULL (leading to a jmp to the
99+ # zero page and a quick SEGFAULT).
100+ # This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
101+ # and --defsym arguments.
102+ # In smaller programs, it appears to be possible to work around this with -Bsymbolic and
103+ # -nostdlib, however when applied the full-sized JNI library here it no longer works.
104+ # After exhausting nearly every flag documented in lld, the only reliable method appears
105+ # to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can
106+ # disassemble it into very readable code, edit it, and then reassemble it.
107+ [ ! -f " $1 " /lightning-c-bindings/target/$LDK_TARGET /release/libldk.a ] && exit 1
108+ if [ " $( ar t " $1 " /lightning-c-bindings/target/$LDK_TARGET /release/libldk.a | grep -v " \.o$" || echo) " != " " ]; then
109+ echo " Archive contained non-object files!"
110+ exit 1
111+ fi
112+ if [ " $( ar t " $1 " /lightning-c-bindings/target/$LDK_TARGET /release/libldk.a | grep ldk.ldk.* -cgu.* .rcgu.o | wc -l) " != " 1" ]; then
113+ echo " Archive contained more than one LDK object file"
114+ exit 1
115+ fi
116+ mkdir -p tmp
117+ rm -f tmp/*
118+ ar x --output=tmp " $1 " /lightning-c-bindings/target/$LDK_TARGET /release/libldk.a
119+ pushd tmp
120+ llvm-dis ldk.ldk.* -cgu.* .rcgu.o
121+ sed -i ' s/br i1 icmp eq (i8\* @__cxa_thread_atexit_impl, i8\* null)/br i1 icmp eq (i8* null, i8* null)/g' ldk.ldk.* -cgu.* .rcgu.o.ll
122+ llvm-as ldk.ldk.* -cgu.* .rcgu.o.ll -o ./libldk.bc
123+ ar q libldk.a * .o
124+ popd
125+ LDK_LIB=" tmp/libldk.bc tmp/libldk.a"
93126 fi
94- $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX .so -flto -O3 -I" $1 " /lightning-c-bindings/include/ $2 src/main/jni/bindings.c " $1 " /lightning-c-bindings/target/ $LDK_TARGET /release/libldk.a
127+ $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX .so -flto -O3 -I" $1 " /lightning-c-bindings/include/ $2 src/main/jni/bindings.c $LDK_LIB
95128 if [ " $IS_MAC " = " false" ]; then
96- set +e # grep exits with 1 if no lines were left, which is our success condition
97- GLIBC_SYMBS=" $( objdump -T liblightningjni_release$LDK_TARGET_SUFFIX .so | grep GLIBC_ | grep -v " GLIBC_2\.2\." | grep -v " GLIBC_2\.3\(\.\| \)" | grep -v " GLIBC_2.\(14\|17\) " ) "
98- set -e
129+ GLIBC_SYMBS=" $( objdump -T liblightningjni_release$LDK_TARGET_SUFFIX .so | grep GLIBC_ | grep -v " GLIBC_2\.2\." | grep -v " GLIBC_2\.3\(\.\| \)" | grep -v " GLIBC_2.\(14\|17\) " || echo) "
99130 if [ " $GLIBC_SYMBS " != " " ]; then
100131 echo " Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
101132 echo " $GLIBC_SYMBS "
0 commit comments