Closed dutor closed 2 weeks ago
Thank you for your report. This is a very difficult issue and arguably a bug in glibc rather than the linker. At least the code is very fragile as it depends on when a weak symbol is resolved.
Even with GNU ld, if you compile your main executable with g++ -pthread main.cpp ./librun.so -o main -fno-PIC -no-pie -Wl,-allow-shlib-undefined
, the output is broken just like mold's output. Or, if you use LLVM lld and compile with g++ -pthread main.cpp ./librun.so -o main -fno-PIC -no-pie
, the result is the same.
Let me think more about how to fix this. By the way, how did you find this problem?
Please try again with git head.
Thanks for the reply and fix!
By the way, how did you find this problem?
We have experienced several occasional memory issues, like heap-use-after-free on the control block of shared_ptr
and memory leaks on resources managed by shared_ptr
. So we tracked down each reference counting operation of shared_ptr
and found out that the atomicity was compromised. Then the runtime atomic dispatch and pthread weak symbol things, etc.
Interestingly, at the beginning we fixed this bug by linking against libpthread
explicitly with --no-as-needed
for every binary(just like the fix https://github.com/rui314/mold/commit/06b592683c150a18d7808e6a91387c0393fa849b). But that seams not encouraged as per the -pthread
option. Then we discovered that other linkers like ld
and gold
dont have these issue(for our build options).
This is a very difficult issue and arguably a bug in glibc rather than the linker
I'm not a toolchain guy. Is there any related discussion on this problem? Maybe I can have more understanding on this.
I wrote the explanation of the issue as the commit message, so you may want to read it first if you want to understand it more. Feel free to ask any questions!
I think this is worth making a new release. I'll be releasing mold 2.32.1 soon.
First of all, thanks for this great linker. It's blazing fast and saves me a lot of build time.
My environment:
Simplified code to reproduce this issue:
Build & run
Some facts:
shared_ptr
is atomic only if it's in a multiple threaded process. Refer to here__gthread_active_p
, which utilizes a weak reference to__pthread_key_create
fromlibpthread
. Refer to here__gthread_active_p
returns false.ld.bfd
,ld.gold
andmold
2.1.0 is OK.Thanks in advance.