This is t.c: ------------ #include <stdio.h> #include <unistd.h> ssize_t __wrap_read(int fd, void *buffer, size_t count) { printf("%s\n", (char*)buffer); return fd + count; } int main() { int i = read(1, "abc", 5); printf("%i\n", i); } ------------ I have gcc 8.3.1 20190311, ld.bfd 2.32.51.20190319, ld.gold 1.16 2.32.51.20190319 and clang 8.0.0. This works clang -flto -fuse-ld=gold -Wl,--wrap=read t.c gcc -fuse-ld=bfd -Wl,--wrap=read t.c gcc -fuse-ld=gold -Wl,--wrap=read t.c clang -fuse-ld=bfd -Wl,--wrap=read t.c clang -fuse-ld=gold -Wl,--wrap=read t.c This fails clang -flto -fuse-ld=bfd -Wl,--wrap=read t.c gcc -flto -fuse-ld=gold -Wl,--wrap=read t.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read t.c
Reported the same at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89930 for GCC with the note “since clang+gold works, gcc+gold should also work.”
This works: clang -flto -fuse-ld=lld -Wl,--wrap=read t.c with ld.LLD 8.0.0.
A patch is posted at https://sourceware.org/ml/binutils/2019-04/msg00011.html
With the patch applied “clang -flto -fuse-ld=bfd -Wl,--wrap=read t.c” does work.
With the patch applied this works: clang -flto -fuse-ld=bfd -Wl,--wrap=read -O3 t.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read -O3 t.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read -O2 t.c gcc -flto -fuse-ld=bfd -Wl,--wrap=read -O1 t.c This does not work: gcc -flto -fuse-ld=gold -Wl,--wrap=read -O3 t.c gcc -flto -fuse-ld=gold -Wl,--wrap=read -O2 t.c gcc -flto -fuse-ld=gold -Wl,--wrap=read -O1 t.c
Reported for gold at https://sourceware.org/bugzilla/show_bug.cgi?id=24415 .
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6fe014bcd33686cb75e6355f9c36ce483a64ec62 commit 6fe014bcd33686cb75e6355f9c36ce483a64ec62 Author: H.J. Lu <hjl.tools@gmail.com> Date: Thu Apr 25 07:53:46 2019 -0700 LTO: Properly handle wrapper symbols in IR When a wrapper symbol, __wrap_FOO, is defined in IR, its resolution should be LDPR_PREVAILING_DEF, not PREVAILING_DEF_IRONLY, since LTO doesn't know that __wrap_FOO provides definition of FOO. And resolution of FOO should be LDPR_RESOLVED_IR since it is resolved by __wrap_FOO in IR. PR ld/24406 * ld.texi: Remove LTO warning from --wrap. * plugin.c (get_symbols): Update resolution for wrapper and wrapped symbols. * testsuite/ld-plugin/lto.exp: Run ld/24406 tests. * testsuite/ld-plugin/pr24406-1.c: New file. * testsuite/ld-plugin/pr24406-2a.c: Likewise. * testsuite/ld-plugin/pr24406-2b.c: Likewise.
Fixed for 2.33.
The master branch has been updated by Alan Modra <amodra@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a78fca7b57c5444ac23110c6d0f80abd966f0e90 commit a78fca7b57c5444ac23110c6d0f80abd966f0e90 Author: Alan Modra <amodra@gmail.com> Date: Thu Aug 29 00:20:46 2019 +0930 Segfault in ld building SPEC CPU2017 527.cam4_r with -flto Caused by the PR24406 fix. unwrap_hash_lookup shouldn't be called with link_info.wrap_hash NULL. PR ld/24406 * plugin.c (get_symbols): Test link_info.wrap_hash before calling unwrap_hash_lookup.