Bug 31482 - The first definition in shared object and archive is ignored
Summary: The first definition in shared object and archive is ignored
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.43 (HEAD)
: P2 normal
Target Milestone: ---
Assignee: H.J. Lu
URL:
Keywords:
: 31489 (view as bug list)
Depends on:
Blocks: 31489
  Show dependency treegraph
 
Reported: 2024-03-14 06:54 UTC by xionghuluo
Modified: 2024-03-15 19:44 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description xionghuluo 2024-03-14 06:54:12 UTC
For even quite simple test case, the binary fails to link static jemalloc functions as below when source file is built with -flto either link with bfd.ld or gold.ld but NOT with mold:

test.cpp:

#include <jemalloc/jemalloc.h>
#include <stdlib.h>

int *ptr;
void test();

void do_something(size_t i) {
   ptr =  (int *)malloc(i * 100);
}

int main(int argc, char **argv) {
  for (size_t i = 0; i < 1000; i++) {
    do_something(i);
  }

  return 0;
}

1) no lto:
g++ test.cpp -c 
g++ test.o /data/home/xionghuluo/install/lib/libjemalloc.a -o test -pthread -ldl
nm test | grep jemalloc
0000000000414600 T je_jemalloc_postfork_child
00000000004143d0 T je_jemalloc_postfork_parent
0000000000414260 T je_jemalloc_prefork
000000000040e770 t jemalloc_constructor

2) source file -flto:
g++ test.cpp -c -flto
g++ test.o /data/home/xionghuluo/install/lib/libjemalloc.a -o test -pthread -ldl
nm test | grep jemalloc

3) both source file and libjemalloc are built with LTO, note the difference of install_s with install for libjemalloc.a:
g++ test.cpp -c -flto
g++ test.o /data/home/xionghuluo/install_s/lib/libjemalloc.a -pthread -ldl -o test -flto
nm test | grep jemalloc

4) source file is built without flto and libjemalloc is built with LTO:
g++ test.cpp -c
g++ test.o -L/data/home/xionghuluo/install_s/lib -l:libjemalloc.a -pthread -ldl -o test
nm test | grep jemalloc
0000000000419d10 t je_jemalloc_postfork_child
0000000000419990 t je_jemalloc_postfork_parent
0000000000418f00 t je_jemalloc_prefork
00000000000f6427 N jemalloc.c.50456a62
000000000040e4d0 t jemalloc_constructor


It shows that when the source file test.cpp is built with -flto, no matter libjemalloc.a is built with or without -flto, the final binary fallback to link glibc's malloc, is this a valid bug for bfd or golder?  Please take a look, thanks.
Comment 1 H.J. Lu 2024-03-14 13:43:44 UTC
This is a GCC bug:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114337
Comment 2 H.J. Lu 2024-03-14 13:51:47 UTC
[hjl@gnu-cfl-3 pr31482-a]$ cat x.c 
#include <stdlib.h>

int
main()
{
  abort ();
  return 0;
}
[hjl@gnu-cfl-3 pr31482-a]$ cat foo.c
#include <unistd.h>

void
abort (void)
{
  _exit (0);
}
[hjl@gnu-cfl-3 pr31482-a]$ make
gcc -B./ -g -flto   -c -o x.o x.c
gcc -B./ -g -o x x.o libfoo.a
./x
make: *** [Makefile:8: all] Aborted (core dumped)
[hjl@gnu-cfl-3 pr31482-a]$
Comment 3 H.J. Lu 2024-03-14 17:07:38 UTC
A patch is posted at

https://sourceware.org/pipermail/binutils/2024-March/133020.html
Comment 4 H.J. Lu 2024-03-14 17:18:39 UTC
The v2 patch is at

https://sourceware.org/pipermail/binutils/2024-March/133022.html

with some test fixes.
Comment 5 H.J. Lu 2024-03-14 23:20:03 UTC
*** Bug 31489 has been marked as a duplicate of this bug. ***
Comment 7 H.J. Lu 2024-03-15 10:38:45 UTC
The v2 patch:

https://sourceware.org/pipermail/binutils/2024-March/133036.html
Comment 8 H.J. Lu 2024-03-15 19:44:43 UTC
The v3 patch fixed a bug:

https://sourceware.org/pipermail/binutils/2024-March/133055.html