Bug 17068 - --as-needed and --start-group/--end-group interact in strange ways if .so files are mentioned in --start-group/--end-group
Summary: --as-needed and --start-group/--end-group interact in strange ways if .so fil...
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.22
: P2 normal
Target Milestone: ---
Assignee: Alan Modra
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-18 18:09 UTC by thakis
Modified: 2014-09-19 22:45 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments
possible fix (1.49 KB, patch)
2014-07-01 11:22 UTC, Alan Modra
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description thakis 2014-06-18 18:09:34 UTC
When doing a release components build of the target 'nacl_helper' in chromium, the link fails with

  c++ -Wl,-z,now -Wl,-z,relro -Wl,--fatal-warnings -pthread -Wl,-z,noexecstack -fPIC -L. -pie -Wl,-uIsHeapProfilerRunning,-uProfilerStart -Wl,-u_Z21InitialMallocHook_NewPKvj,-u_Z22InitialMallocHook_MMapPKvS0_jiiix,-u_Z22InitialMallocHook_SbrkPKvi -Wl,-u_Z21InitialMallocHook_NewPKvm,-u_Z22InitialMallocHook_MMapPKvS0_miiil,-u_Z22InitialMallocHook_SbrkPKvl -Wl,-u_ZN15HeapLeakChecker12IgnoreObjectEPKv,-u_ZN15HeapLeakChecker14UnIgnoreObjectEPKv -m64 -Wl,-O1 -Wl,--as-needed -Wl,--gc-sections -Wl,-rpath=\$ORIGIN/lib/ -Wl,-rpath-link=lib/ -o nacl_helper -Wl,--start-group obj/components/nacl/loader/nacl_helper.nacl_helper_linux.o obj/components/libnacl_linux.a obj/components/libnacl.a obj/base/libbase_static.a obj/native_client/src/trusted/service_runtime/libsel.a obj/native_client/src/trusted/service_runtime/libenv_cleanser.a obj/native_client/src/trusted/service_runtime/libnacl_error_code.a obj/native_client/src/shared/gio/libgio.a obj/native_client/src/shared/platform/libplatform.a obj/native_client/src/shared/srpc/libnonnacl_srpc.a obj/native_client/src/trusted/debug_stub/libdebug_stub.a obj/native_client/src/trusted/desc/libnrd_xfer.a obj/native_client/src/trusted/desc/libdesc_wrapper.a obj/native_client/src/shared/imc/libimc.a obj/native_client/src/trusted/nacl_base/libnacl_base.a obj/native_client/src/trusted/desc_cacheability/libdesc_cacheability.a obj/native_client/src/trusted/fault_injection/libnacl_fault_inject.a obj/native_client/src/trusted/gio/libgio_wrapped_desc.a obj/native_client/src/trusted/interval_multiset/libnacl_interval.a obj/native_client/src/trusted/perf_counter/libnacl_perf_counter.a obj/native_client/src/trusted/platform_qualify/libplatform_qual_lib.a obj/native_client/src/trusted/cpu_features/libcpu_features.a obj/native_client/src/trusted/manifest_name_service_proxy/libmanifest_proxy.a obj/native_client/src/trusted/threading/libthread_interface.a obj/native_client/src/trusted/simple_service/libsimple_service.a obj/native_client/src/trusted/validator/libvalidation_cache.a obj/native_client/src/trusted/validator/libvalidators.a obj/native_client/src/trusted/service_runtime/arch/x86/libservice_runtime_x86_common.a obj/native_client/src/trusted/validator_ragel/libdfa_validate_x86_64.a obj/native_client/src/trusted/service_runtime/arch/x86_64/libservice_runtime_x86_64.a obj/native_client/src/trusted/validator_x86/libnccopy_x86_64.a obj/native_client/src/trusted/service_runtime/libnacl_signal.a obj/components/libnacl_common.a obj/components/libnacl_switches.a obj/components/libtracing.a obj/sandbox/libc_urandom_override.a obj/base/allocator/liballocator.a obj/base/third_party/dynamic_annotations/libdynamic_annotations.a lib/libseccomp_bpf.so lib/libseccomp_bpf_helpers.so lib/libcrcrypto.so lib/libsuid_sandbox_client.so lib/libppapi_shared.so lib/libsandbox_services.so lib/libipc.so lib/libbase.so lib/libppapi_proxy.so lib/libcontent.so -Wl,--end-group -lrt -ldl -lpthread -lgmodule-2.0 -lgobject-2.0 -lgthread-2.0 -lglib-2.0
/usr/bin/ld: obj/components/../ppapi/nacl_irt/nacl_linux.plugin_main.o: undefined reference to symbol 'ppapi::PPB_Audio_Shared::SetThreadFunctions(PP_ThreadFunctions const*)'
/usr/bin/ld: note: 'ppapi::PPB_Audio_Shared::SetThreadFunctions(PP_ThreadFunctions const*)' is defined in DSO lib/libppapi_shared.so so try adding it to the linker command line

nacl_linux.plugin_main.o is in libnacl_linux.a

Note that ld tells me to add libppapi_shared.so to the link line, and it's even listed after libnacl_linux.a -- but both are in a --start-group/--end-group group, so the order shouldn't matter.

The error goes away if I do any of:
* Use gold to link
* Remove --as-needed
* Move all the lib/*.so files to appear after --end-group (this is what we went with)

I tried to make a reduced repro case for a bit, but didn't succeed. Maybe it's obvious to people familiar with ld's source what's going on. I'm not even sure if this is considered a bug, but it works fine with gold. (Though I only tried with gold 2.24, which is newer than the ld I'm using.)

This is with
$ /usr/bin/ld --version
GNU ld (GNU Binutils for Ubuntu) 2.22
Copyright 2011 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
Comment 1 Alan Modra 2014-07-01 11:22:54 UTC
Created attachment 7669 [details]
possible fix

I think the attached fix ought to cure this problem.  Please test it!
Comment 2 thakis 2014-07-02 23:38:52 UTC
That seems to do the trick. I built ld-new from source and checked that my build fails with that, then applied your patch and checked that it succeeds.
Comment 3 Sourceware Commits 2014-07-03 08:07:59 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  d215621ebe5a6b60dff2e60c788a0f761e3cb5bd (commit)
      from  fd48cee0093fef69608959d0f577c0c81fe24eec (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d215621ebe5a6b60dff2e60c788a0f761e3cb5bd

commit d215621ebe5a6b60dff2e60c788a0f761e3cb5bd
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Jul 3 16:32:45 2014 +0930

    Reload --as-needed libraries inside groups
    
    When a shared library appears within --start-group/--end-group ld may
    only discover a need for loading the library on the second or
    subsequent pass over archive libraries, as more objects are extracted.
    
    ld/
    	PR 17068
    	* ldlang.c (load_symbols): Always check flags.reload.
    	(open_input_bfds): Always reload --as-needed shared libraries,
    	not just when rescanning.
    	* ldlang.h (struct lang_input_statement_flags): Update reload comment.
    	* plugin.c (plugin_should_reload): Assume shared library arg.
    	* plugin.h (plugin_should_reload): Update comment.
    ld/testsuite
    	* ld-elf/pr17068.s: New.
    	* ld-elf/pr17068a.s: New.
    	* ld-elf/pr17068b.s: New.
    	* ld-elf/pr17068c.s: New.
    	* ld-elf/pr17068d.s: New.
    	* ld-elf/pr17068e.s: New.
    	* ld-elf/pr17068ez.s: New.
    	* ld-elf/elf.exp: Run new test.

-----------------------------------------------------------------------

Summary of changes:
 ld/ChangeLog                    |   10 ++++++++++
 ld/ldlang.c                     |   28 ++++++++++------------------
 ld/ldlang.h                     |    2 +-
 ld/plugin.c                     |    6 ++----
 ld/plugin.h                     |    2 +-
 ld/testsuite/ChangeLog          |   11 +++++++++++
 ld/testsuite/ld-elf/elf.exp     |   26 ++++++++++++++++++++------
 ld/testsuite/ld-elf/pr17068.s   |    2 ++
 ld/testsuite/ld-elf/pr17068a.s  |    4 ++++
 ld/testsuite/ld-elf/pr17068b.s  |    4 ++++
 ld/testsuite/ld-elf/pr17068c.s  |    4 ++++
 ld/testsuite/ld-elf/pr17068d.s  |    6 ++++++
 ld/testsuite/ld-elf/pr17068e.s  |    3 +++
 ld/testsuite/ld-elf/pr17068ez.s |    4 ++++
 14 files changed, 82 insertions(+), 30 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/pr17068.s
 create mode 100644 ld/testsuite/ld-elf/pr17068a.s
 create mode 100644 ld/testsuite/ld-elf/pr17068b.s
 create mode 100644 ld/testsuite/ld-elf/pr17068c.s
 create mode 100644 ld/testsuite/ld-elf/pr17068d.s
 create mode 100644 ld/testsuite/ld-elf/pr17068e.s
 create mode 100644 ld/testsuite/ld-elf/pr17068ez.s
Comment 4 Alan Modra 2014-07-03 08:09:20 UTC
Patch applied
Comment 5 thakis 2014-07-03 20:12:11 UTC
Thanks for the quick fix! :-)
Comment 6 Gregory Fong 2014-09-19 22:45:48 UTC
Could this be backported to binutils-2_24-branch?