Bug 24406 - -Wl,--wrap= incompatible with -flto
Summary: -Wl,--wrap= incompatible with -flto
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.32
: P2 normal
Target Milestone: 2.33
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: 25389
  Show dependency treegraph
 
Reported: 2019-04-01 06:18 UTC by dilyan.palauzov@aegee.org
Modified: 2020-01-17 00:27 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2019-04-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description dilyan.palauzov@aegee.org 2019-04-01 06:18:15 UTC
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
Comment 1 dilyan.palauzov@aegee.org 2019-04-02 17:05:28 UTC
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.”
Comment 2 dilyan.palauzov@aegee.org 2019-04-02 17:21:50 UTC
This works:

clang -flto -fuse-ld=lld -Wl,--wrap=read t.c 

with ld.LLD 8.0.0.
Comment 3 H.J. Lu 2019-04-02 22:13:17 UTC
A patch is posted at

https://sourceware.org/ml/binutils/2019-04/msg00011.html
Comment 4 dilyan.palauzov@aegee.org 2019-04-03 14:17:21 UTC
With the patch applied “clang -flto -fuse-ld=bfd  -Wl,--wrap=read t.c” does work.
Comment 5 dilyan.palauzov@aegee.org 2019-04-03 14:25:36 UTC
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
Comment 6 dilyan.palauzov@aegee.org 2019-04-03 14:35:31 UTC
Reported for gold at https://sourceware.org/bugzilla/show_bug.cgi?id=24415 .
Comment 7 cvs-commit@gcc.gnu.org 2019-04-25 14:55:22 UTC
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.
Comment 8 H.J. Lu 2019-04-25 14:55:39 UTC
Fixed for 2.33.
Comment 9 cvs-commit@gcc.gnu.org 2019-08-28 15:05:46 UTC
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.