Bug 17878 - Need support on bfd ld for LLVMgold.so plugin
Summary: Need support on bfd ld for LLVMgold.so plugin
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.26
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-26 19:41 UTC by Robert Cox
Modified: 2015-02-12 13:04 UTC (History)
1 user (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 Robert Cox 2015-01-26 19:41:32 UTC
On a Red Hat 7.0 system, there is in theory support for the LLVMgold.so plugin in the bfd linker.  But if I use clang:

-bash-4.2$ clang -c -flto sm1.c
-bash-4.2$ clang -c -flto sm2.c
-bash-4.2$ clang -flto sm1.o sm2.o
sm1.o: file not recognized: File format not recognized
clang-3.6: error: linker command failed with exit code 1 (use -v to see invocation)

-bash-4.2$ cat sm1.c
static int myfunc(int a, int b)
{
    return a - b;
}

extern int foo(int a, int b)
{
   return myfunc(a, b);
}

extern int goo(int a, int b);

extern int main(void)
{
   return foo(2, 3) + goo(4, 5);
}

-bash-4.2$ cat sm2.c
static int myfunc(int a, int b)
{
    return a + b;
}

extern int goo(int a, int b)
{
   return myfunc(a, b);
}
Comment 1 H.J. Lu 2015-01-26 21:22:13 UTC
Confirmed.
Comment 2 H.J. Lu 2015-01-26 22:55:32 UTC
BFD linker assumes LTO IR is contained in sections of the target
ELF object file.  It is true for GCC LTO IR, but false for llvm
LTO IR.  It won't be easy to fix it.
Comment 3 Sourceware Commits 2015-01-28 18:35:49 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=15f7a26b0f42be812b0471ec09d91bad696fe26e

commit 15f7a26b0f42be812b0471ec09d91bad696fe26e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Jan 28 10:27:31 2015 -0800

    The initial support for LDPT_GET_VIEW
    
    This patch adds the initial support for LDPT_GET_VIEW.
    
    ld/
    
    	PR ld/17878
    	* plugin.c (tv_header_tags(): Add LDPT_GET_VIEW.
    	(get_view): New function.
    	(set_tv_header): Handle LDPT_GET_VIEW.
    	* testplug.c (tag_names): Add LDPT_GET_VIEW.
    	(tv_get_view): New.
    	(dump_tv_tag): Handle LDPT_GET_VIEW.
    	(parse_tv_tag): Likewise.
    
    ld/testsuite/
    
    	PR ld/17878
    	* ld-plugin/plugin-1.d: Add LDPT_GET_VIEW.
    	* ld-plugin/plugin-10.d: Likewise.
    	* ld-plugin/plugin-11.d: Likewise.
    	* ld-plugin/plugin-2.d: Likewise.
    	* ld-plugin/plugin-3.d: Likewise.
    	* ld-plugin/plugin-4.d: Likewise.
    	* ld-plugin/plugin-5.d: Likewise.
    	* ld-plugin/plugin-6.d: Likewise.
    	* ld-plugin/plugin-7.d: Likewise.
    	* ld-plugin/plugin-8.d: Likewise.
    	* ld-plugin/plugin-9.d: Likewise.
Comment 4 Sourceware Commits 2015-02-05 13:07: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=f4b78d1898203363e7f551497b6231d0f891d6f9

commit f4b78d1898203363e7f551497b6231d0f891d6f9
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Feb 5 05:00:52 2015 -0800

    Add plugin_input_file_t
    
    This patchs adds plugin_input_file_t to implement get_input_file, get_view
    and release_input_file.  The maximum memeory overhead per IR input file
    are about 40 bytes for plugin_input_file_t plus the memory to store input
    IR filename.  According to
    
    http://gcc.gnu.org/wiki/whopr/driver
    
    RELEASE_INPUT_FILE: Function pointer to the linker interface that
    releases a file descriptor for a claimed input file. The plug-in library
    must call this interface for each file descriptor obtained by the "get
    input file" interface. It must release all such file descriptors before
    returning from the WPA phase.
    
    However, GCC plug-in library doesn't use the "get input file" interface.
    It processed the IR input in the claim file handler.  Since the the file
    descriptor opened for the IR input was unused after the claim file
    handler returns and GCC plug-in library before GCC 5 doesn't call the
    RELEASE_INPUT_FILE function pointer, ld closed the file descriptor to
    avoid leaking file descriptor.  But this approach doesn't work with
    other plug-in libraries which uses the "get input file", "get view" and
    "release input file" interfaces.  To avoid file descriptor leak with
    GCC prior to GCC 5 and support other plug-in libraries at the same time,
    we close the file descriptor only if the input IR file is a bfd_object
    file.  This scheme doesn't work when a plug-in library needs the file
    descriptor and its IR is stored in bfd_object file.
    
    	PR ld/17878
    	* plugin.c: Include <errno.h>.
    	(errno): New.  Declare if needed.
    	(plugin_input_file_t): New.
    	(get_input_file): Implemented.
    	(get_view): Likewise.
    	(release_input_file): Likewise.
    	(add_symbols): Updated.
    	(get_symbols): Likewise.
    	(plugin_maybe_claim): Allocate a plugin_input_file_t.  Close fd
    	only for a bfd_object input.
Comment 5 Sourceware Commits 2015-02-11 13:03:50 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=5ae0078cd2b6b69e6119864e20987c8724916b29

commit 5ae0078cd2b6b69e6119864e20987c8724916b29
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Feb 11 05:01:03 2015 -0800

    Merge linker plugin handling into BFD plugin support
    
    Linker plugin_maybe_claim is the interface of linker plugin support.
    This patch extracts linker plugin_maybe_claim into plugin_object_p and
    makes it available to BFD via a new function:
    
    void register_ld_plugin_object_p (const bfd_target *(*) (bfd *));
    
    bfd_plugin_object_p calls plugin_object_p registered by linker first.  It
    adds an enum bfd_plugin_format field and a pointer to plugin dummy BFD so
    that plugin_object_p stores plugin dummy BFD to allow plugin_maybe_claim
    to retrieve it later.
    
    bfd/
    
    	PR ld/17878
    	* bfd.c (bfd_plugin_format): New.
    	(bfd): Add plugin_format and plugin_dummy_bfd.
    	* plugin.c (try_load_plugin): Take a pointer to bfd_boolean
    	argument to return TRUE if any plugin is found.  Set plugin_format.
    	(has_plugin): New.
    	(bfd_plugin_target_p): New.
    	(bfd_plugin_specified_p): Likewise.
    	(bfd_plugin_target_p): Likewise.
    	(register_ld_plugin_object_p): Likewise.
    	(bfd_plugin_set_plugin): Set has_plugin.
    	(load_plugin): Cache try_load_plugin result.
    	(bfd_plugin_object_p): Try ld_plugin_object_p first.  Check
    	plugin_format.
    	* plugin.h (bfd_plugin_target_p): New.
    	(bfd_plugin_specified_p): Likewise.
    	(register_ld_plugin_object_p): Likewise.
    	* bfd-in2.h: Regenerated.
    
    ld/
    
    	PR ld/17878
    	* plugin.c: Include ../bfd/plugin.h.
    	(plugin_get_ir_dummy_bfd): Call bfd_create with
    	link_info.output_bfd instead of srctemplate.  Copy BFD info
    	from srctemplate only if it doesn't use BFD plugin target
    	vector.
    	(plugin_load_plugins): Call register_ld_plugin_object_p with
    	(plugin_object_p)
    	(plugin_maybe_claim): Renamed to ...
    	(plugin_object_p): This.  Return dummy BFD target vector if
    	input is calimed by plugin library, otherwise return NULL.
    	Update plugin_format and plugin_dummy_bfd.
    	(plugin_maybe_claim): New.  Use plugin_object_p.
    
    xx
Comment 6 Sourceware Commits 2015-02-11 13:03:55 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=439b7f41b5fee5ec5902aecc8f3fd105be6270b3

commit 439b7f41b5fee5ec5902aecc8f3fd105be6270b3
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Feb 11 05:01:37 2015 -0800

    Add tests for non-object IR file
    
    This patch adds linker, nm and ar tests for non-object IR file, including
    archive with non-object IR member.
    
    ld/
    
    	PR ld/17878
    	* Makefile.am (noinst_LTLIBRARIES): Add libldtestplug2.la and
    	libldtestplug3.la.
    	(libldtestplug2_la_SOURCES): New.
    	(libldtestplug2_la_CFLAGS): Likewise.
    	(libldtestplug2_la_LDFLAGS): Likewise.
    	(libldtestplug3_la_SOURCES): New.
    	(libldtestplug3_la_CFLAGS): Likewise.
    	(libldtestplug3_la_LDFLAGS): Likewise.
    	* Makefile.in: Regenerated.
    	* testplug2.c: New file.
    	* testplug3.c: Likewise.
    
    ld/testsuite/
    
    	PR ld/17878
    	* ld-plugin/func.c: Add some comments.
    	* ld-plugin/plugin-13.d: New file.
    	* ld-plugin/plugin-14.d: Likewise.
    	* ld-plugin/plugin-15.d: Likewise.
    	* ld-plugin/plugin-16.d: Likewise.
    	* ld-plugin/plugin-17.d: Likewise.
    	* ld-plugin/plugin-18.d: Likewise.
    	* ld-plugin/plugin-19.d: Likewise.
    	* ld-plugin/plugin-20.d: Likewise.
    	* ld-plugin/plugin-21.d: Likewise.
    	* ld-plugin/plugin-22.d: Likewise.
    	* ld-plugin/plugin-23.d: Likewise.
    	* ld-plugin/plugin-24.d: Likewise.
    	* ld-plugin/plugin-25.d: Likewise.
    	* ld-plugin/plugin-26.d: Likewise.
    	* ld-plugin/plugin.exp (plugin2_name): New.
    	(plugin3_name): Likewise.
    	(plugin2_path): Likewise.
    	(plugin3_path): Likewise.
    	(testsrcfiles): Likewise.
    	(testsrcfiles_notext): Likewise.
    	(plugin_tests): Add tests for non-object IR file.
    	(plugin_lib_tests): Likewise.
    	(plugin_extra_elf_tests): Likewise.
    	(plugin_src_tests): New tests for non-object IR file.
    	Run nm --plugin test.
    	Run ar --plugin test.
    	Run plugin_src_tests.
Comment 7 H.J. Lu 2015-02-12 13:04:55 UTC
Fixed.