Bug 17306 - ld aborts when linking with --just-symbols
Summary: ld aborts when linking with --just-symbols
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.24
: P2 normal
Target Milestone: 2.25
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-08-25 10:14 UTC by Fam Zheng
Modified: 2014-08-28 00:04 UTC (History)
1 user (show)

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


Attachments
Reproducer (2.17 MB, application/x-bzip)
2014-08-26 02:46 UTC, Fam Zheng
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Fam Zheng 2014-08-25 10:14:20 UTC
I hit this error while I am building qemu-img with the source code from qemu-project.org:

/usr/bin/ld: BFD (GNU Binutils) 2.24 internal error, aborting at /build/binutils/src/binutils-2.24/bfd/elf64-x86-64.c line 3641 in elf_x86_64_relocate_section

/usr/bin/ld: Please report this bug.

collect2: error: ld returned 1 exit status


My ld version:

$ ld --version
GNU ld (GNU Binutils) 2.24
Copyright 2013 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.

Command line: The below line, as a modified command from the one that would be generated by QEMU's build system:

$ c++ -Werror -fPIE -DPIE -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common -Wno-error=deprecated-declarations  -Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wold-style-declaration -Wold-style-definition -Wtype-limits -fstack-protector-strong  -I/usr/include/p11-kit-1  -I/usr/include/p11-kit-1    -I/usr/include/libpng16  -I/usr/include/spice-server -I/usr/include/cacard -I/usr/include/nss -I/usr/include/nspr -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/spice-1  -I/usr/include/libusb-1.0   -I/usr/include/pixman-1  -I/home/fam/qemu/dtc/libfdt -I/home/fam/qemu/tests -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -pthread -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include  -pthread -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include  -g  -Wl,--warn-common -Wl,-z,relro -Wl,-z,now -pie -m64 -g -lrt  -o qemu-img qemu-img.o async.o thread-pool.o nbd.o block.o blockjob.o main-loop.o iohandler.o qemu-timer.o aio-posix.o qemu-io-cmds.o qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o qemu-coroutine-sleep.o coroutine-ucontext.o block/raw_bsd.o block/cow.o block/qcow.o block/vdi.o block/vmdk.o block/cloop.o block/dmg.o block/bochs.o block/vpc.o block/vvfat.o block/qcow2.o block/qcow2-refcount.o block/qcow2-cluster.o block/qcow2-snapshot.o block/qcow2-cache.o block/qed.o block/qed-gencb.o block/qed-l2-cache.o block/qed-table.o block/qed-cluster.o block/qed-check.o block/vhdx.o block/vhdx-endian.o block/vhdx-log.o block/quorum.o block/parallels.o block/blkdebug.o block/blkverify.o block/snapshot.o block/qapi.o block/raw-posix.o block/linux-aio.o block/nbd.o block/nbd-client.o block/sheepdog.o block/nfs.o -Wl,--just-symbols=block/iscsi.o libqemuutil.a libqemustub.a   -lz -laio -lm -Wl,--export-dynamic -lgmodule-2.0 -pthread -lglib-2.0  -lgthread-2.0 -pthread -lglib-2.0   -lz -lnfs  -lz -lcap-ng -lvdeplug -luuid -lgnutls  -lgnutls  -lgnutls  -lSDL -lpthread  -lX11 -lvte -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lgdk_pixbuf-2.0 -lpangoft2-1.0 -lpango-1.0 -lfontconfig -lfreetype -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lcairo -lX11 -lXext  -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lfontconfig -lfreetype  -lX11  -llzo2 -lsnappy -lseccomp  -lutil

The base command is from QEMU's build command:

"cd $QEMU_SRC; ./configure --enable-modules --target-list=x86_64-softmmu; make qemu-img V=1".

It's the last command to actually link many objects, libraries and archives to the executable qemu-img.

The only difference is the manually added "-Wl,--just-symbols=block/iscsi.o", which I used here to try to fix the problem described in

http://lists.gnu.org/archive/html/qemu-devel/2014-08/msg03397.html

The purpose of this --just-symbols option, is to force qemu-img to include the definition of function "qmp_query_uuid" with --just-symbols=block/iscsi.o. Because block/iscsi.o references this symbol but qemu-img itself doesn't.
Comment 1 H.J. Lu 2014-08-26 00:04:01 UTC
Please provide a small testcase.

BTW, you can try -Wl,-u,qmp_query_uuid for your original problem.
Comment 2 Fam Zheng 2014-08-26 02:46:59 UTC
Created attachment 7758 [details]
Reproducer

I linked all the normal .o files from comment 0 to qemu-img-partial.o with "ld -r", and put it into the archive.

Now if I link it with --just-symbols, ld aborts:

"ld -o qemu-img qemu-img-partial.o --just-symbols=iscsi.o libqemuutil.a libqemustub.a $LDFLAGS"

See link.sh in the archive for the necessary libs in $LDFLAGS.

iscsi.o libqemuutil.a libqemustub.a are all included in the archived too, so you can test this command line.
Comment 3 Fam Zheng 2014-08-26 02:52:52 UTC
(In reply to H.J. Lu from comment #1)
> BTW, you can try -Wl,-u,qmp_query_uuid for your original problem.

Yes it does help, Thanks. But there are a few more symbols than qmp_query_uuid that need to be pulled in from libqemustub.a, and more will come in the future, so special casing several limited symbols with -Wl,-u,foo is not a good solution compared to --just-symbols.

Fam
Comment 4 H.J. Lu 2014-08-26 15:45:49 UTC
(In reply to Fam Zheng from comment #2)
> Created attachment 7758 [details]
> Reproducer
> 

Please provide a self-contained testcase.  I don't have all libraries
this one needed.
Comment 5 H.J. Lu 2014-08-26 15:49:42 UTC
(In reply to Fam Zheng from comment #3)
> (In reply to H.J. Lu from comment #1)
> > BTW, you can try -Wl,-u,qmp_query_uuid for your original problem.
> 
> Yes it does help, Thanks. But there are a few more symbols than
> qmp_query_uuid that need to be pulled in from libqemustub.a, and more will
> come in the future, so special casing several limited symbols with
> -Wl,-u,foo is not a good solution compared to --just-symbols.

--just-symbols may not do what you wanted:

'--just-symbols=FILENAME'
     Read symbol names and their addresses from FILENAME, but do not
     relocate it or include it in the output.  This allows your output
     file to refer symbolically to absolute locations of memory defined
     in other programs.  You may use this option more than once.

You can use

for s in $(nm -u iscsi.o | awk '{ print $2 }'); do echo "-Wl,-u,$s"; done

to generate all -Wl,u,xxx.
Comment 6 H.J. Lu 2014-08-26 17:27:01 UTC
A small testcase:

[hjl@gnu-6 pr17306]$ cat foo.s
	.data
	.globl foo
foo:
	.quad -1
	.text
	.globl	_start
	.type	_start, @function
_start:
	pushq	foo@GOTPCREL(%rip)
[hjl@gnu-6 pr17306]$ cat lea.s 
	.text
	.globl	bar
	.type	bar, @function
bar:
	movq	foo@GOTPCREL(%rip), %rax
[hjl@gnu-6 pr17306]$ make
gcc -c  -o lea.o lea.s
gcc -c  -o foo.o foo.s
./ld -m elf_x86_64 --just-symbols=lea.o -Bsymbolic -shared -o libfoo.so foo.o
./ld: BFD (GNU Binutils) 2.24.51.20140826 internal error, aborting at /export/gnu/import/git/sources/binutils-gdb/bfd/elf64-x86-64.c line 3824 in elf_x86_64_relocate_section

./ld: Please report this bug.

make: *** [libfoo.so] Error 1
[hjl@gnu-6 pr17306]$
Comment 7 cvs-commit@gcc.gnu.org 2014-08-27 15:00:58 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  c8831961f9fecf81e90fe7c8c94b47e01ee64662 (commit)
      from  29a9f53e857c5d55df852d0a5d5c41a9247c16a2 (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=c8831961f9fecf81e90fe7c8c94b47e01ee64662

commit c8831961f9fecf81e90fe7c8c94b47e01ee64662
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Aug 27 07:59:48 2014 -0700

    Use bfd_is_abs_section to check discarded input section
    
    bfd/
    
    	PR ld/17306
    	* elf32-i386.c (elf_i386_convert_mov_to_lea): Use bfd_is_abs_section
    	to check discarded input section.
    	* elf64-x86-64.c (elf_x86_64_convert_mov_to_lea): Likewise.
    
    ld/testsuite/
    
    	PR ld/17306
    	* ld-i386/i386.exp (i386tests): Add tests for PR ld/17306.
    	* ld-x86-64/x86-64.exp (x86_64tests): Likewise.
    
    	* ld-i386/pr17306a.s: New file.
    	* ld-i386/pr17306b.s: Likewise.
    	* ld-x86-64/pr17306a.s: Likewise.
    	* ld-x86-64/pr17306b.s: Likewise.

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

Summary of changes:
 bfd/ChangeLog                     |    7 +++++++
 bfd/elf32-i386.c                  |    2 +-
 bfd/elf64-x86-64.c                |    2 +-
 ld/testsuite/ChangeLog            |   11 +++++++++++
 ld/testsuite/ld-i386/i386.exp     |    4 ++++
 ld/testsuite/ld-i386/pr17306a.s   |    9 +++++++++
 ld/testsuite/ld-i386/pr17306b.s   |    2 ++
 ld/testsuite/ld-x86-64/pr17306a.s |    9 +++++++++
 ld/testsuite/ld-x86-64/pr17306b.s |    2 ++
 ld/testsuite/ld-x86-64/x86-64.exp |    4 ++++
 10 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100644 ld/testsuite/ld-i386/pr17306a.s
 create mode 100644 ld/testsuite/ld-i386/pr17306b.s
 create mode 100644 ld/testsuite/ld-x86-64/pr17306a.s
 create mode 100644 ld/testsuite/ld-x86-64/pr17306b.s
Comment 8 H.J. Lu 2014-08-27 15:09:57 UTC
Linker crash is fixed now.  But I still don't think --just-symbols is
appropriate for your usage since symbols defined in block/iscsi.o are
converted to absolute symbols with value == 0.  You should use -Wl,-u
or -Wl,--whole-archive instead.
Comment 9 Fam Zheng 2014-08-28 00:04:54 UTC
Thanks for the fix!

Fam