[lld] A primitive program linked by lld core dumps


I've encountered a newly introduced bug in lld today.

I am building my program with clang and lld on an X86-64 Ubuntu Linux machine.
Command line:
     clang main.c

The compiled program invokes the "main" function multiple times until it closes with a segmentation fault.
This affects even the most primitive programs like "int main() { return 0; }".

The problem was introduced at the revision 218259 - [ELF] Fix linking when a regular object defines a symbol that is used in a DSO.

Would you be able to take a look at this please?

Thank you.

Kind regards,

Thanks for reporting, I’ll look into this.

Could you send me in a private message the binary attached?


Hi Oleg,

The problem is that lld incorrectly exports a dummy definition of __tls_get_addr saying that this TLS runtime function resides at the first address of the text section (which coincides with your main function). Your libc (and mine doesn’t, that’s why this went unnoticed) requests this symbol at link time, and the linker promptly puts this into the dynamic symbol table.

At runtime, your program runs normally, but afterwards, when libc is running its finalization hooks, it uses TLS storage, a mechanism that needs the __tls_get_addr function to be correctly set up. Since it has been incorrectly linked to your main function, it loops calling your main function several times until it crashes.

Therefore, while the patch introduced in r218259 is correct, the problem is that lld is incorrectly exporting __tls_get_addr. I spent this week working on this issue (TLS relocations) and I will upload today a patch that fixes the __tls_get_addr symbol on ELF targets, please wait a little more until this new patch gets reviewed. Thanks for your report.

Rafael Auler