+ # 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.
+ # 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).
+ # This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
+ # and --defsym arguments.
+ # In smaller programs, it appears to be possible to work around this with -Bsymbolic and
+ # -nostdlib, however when applied the full-sized JNI library here it no longer works.
+ # 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.
+ [ ! -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!"
+ exit 1
+ fi
+ if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
+ echo "Archive contained more than one LDK object file"
+ exit 1
+ fi
+ mkdir -p tmp
+ rm -f tmp/*
+ ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
+ pushd tmp
+ llvm-dis ldk.ldk.*-cgu.*.rcgu.o
+ 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
+ llvm-as ldk.ldk.*-cgu.*.rcgu.o.ll -o ./libldk.bc
+ ar q libldk.a *.o
+ popd
+ LDK_LIB="tmp/libldk.bc tmp/libldk.a"