rui314 / mold

Mold: A Modern Linker 🦠
MIT License
14.25k stars 469 forks source link

mold-linked DSO's destructors not called upon dlclose unless compiled with -fno-use-cxa-atexit #589

Open yosefk opened 2 years ago

yosefk commented 2 years ago

First of all, thanks for the awesome and blindingly fast linker!

In my test, I run into a segfault upon exiting a process after dlclosing a DSO linked with mold, because an exit handler gets called from this already-unloaded DSO (note that no exit handlers are called upon the call to dlclose). When compiling all of the code in the DSO with -fno-use-cxa-atexit, everything works (no segfault, exit handlers called upon dlclose.) When compiling normally (without -fno-use-cxa-atexit) and overriding __cxa_atexit with a do-nothing function using LD_PRELOAD, no segfault (and no exit handlers ever called, ofc.)

I think that using -fno-use-cxa-atexit is a fine workaround (at least it seems that the exit handlers work fine so not sure if this has any downside), but I might be wrong. Anyway, a reproducer is attached:

mold-atexit-report.zip

To reproduce:

$ unzip mold-atexit-report.zip $ ./mold-1.3.1/dlopen-test/test.sh

(Note that this unzips a mold binary with all of its .so dependencies, specifically version "mold 1.3.1 (ff087b26a2f0035a11c761bed59965eab161a0c7)" built using the Dockerfile in this repo. test.sh uses this binary.)

yosefk commented 2 years ago

Ahem, this does not reproduce with https://github.com/rui314/mold/releases/download/v1.3.1/mold-1.3.1-x86_64-linux.tar.gz (version "mold 1.3.1 (7f7d10cc896e0c7513d09723d5ca3d8346bdea49").

Somehow it seems likely that the problem is with my build and not the actual linker source code, but leaving it here just in case. Also found that the file ./mold-1.3.1/bin/ld points outside the unzipped directory - the ld file can be replaced with:

#!/bin/bash SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" env LD_LIBRARY_PATH=$SCRIPTPATH $SCRIPTPATH/mold "$@"