Bug 19119 - Gold accepts bogus target emulation
Summary: Gold accepts bogus target emulation
Status: RESOLVED WONTFIX
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.26
Assignee: Cary Coutant
URL:
Keywords:
Depends on: 19172 19200
Blocks:
  Show dependency treegraph
 
Reported: 2015-10-12 17:52 UTC by H.J. Lu
Modified: 2015-11-05 23:00 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 H.J. Lu 2015-10-12 17:52:49 UTC
[hjl@gnu-6 pr19118]$ make clean
rm -f *.o *.so *.a
[hjl@gnu-6 pr19118]$ cat x.s 
	.text
	.type _start,"function"
	.global _start
_start:
	.byte 0
[hjl@gnu-6 pr19118]$ make
./as --32 -o x.o x.s
./ld -m elf_foobar -o x x.o
[hjl@gnu-6 pr19118]$ ld -m elf_foobar -o x x.o
ld: unrecognised emulation mode: elf_foobar
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu i386linux elf_l1om elf_k1om
[hjl@gnu-6 pr19118]$
Comment 1 H.J. Lu 2015-10-14 14:39:22 UTC
[hjl@gnu-6 pr19119]$ cat x.s
	.text
	.type _start,"function"
	.global _start
_start:
	ret
[hjl@gnu-6 pr19119]$ make
./as --32 -o x.o x.s
./ld -m elf_iamcu -o x x.o
[hjl@gnu-6 pr19119]$ file x
x: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
[hjl@gnu-6 pr19119]$ ld -m elf_iamcu -o x x.o  
ld: i386 architecture of input file `x.o' is incompatible with iamcu output
[hjl@gnu-6 pr19119]$ 

Gold simply ignores -m elf_iamcu.
Comment 2 Cary Coutant 2015-10-14 17:03:53 UTC
Yes, gold uses the target of the first object file; only if there are
no object files does it use the -m option (note that the option is
marked "obsolete").

-cary


On Wed, Oct 14, 2015 at 7:39 AM, hjl.tools at gmail dot com
<sourceware-bugzilla@sourceware.org> wrote:
> https://sourceware.org/bugzilla/show_bug.cgi?id=19119
>
> --- Comment #1 from H.J. Lu <hjl.tools at gmail dot com> ---
> [hjl@gnu-6 pr19119]$ cat x.s
>         .text
>         .type _start,"function"
>         .global _start
> _start:
>         ret
> [hjl@gnu-6 pr19119]$ make
> ./as --32 -o x.o x.s
> ./ld -m elf_iamcu -o x x.o
> [hjl@gnu-6 pr19119]$ file x
> x: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked,
> not stripped
> [hjl@gnu-6 pr19119]$ ld -m elf_iamcu -o x x.o
> ld: i386 architecture of input file `x.o' is incompatible with iamcu output
> [hjl@gnu-6 pr19119]$
>
> Gold simply ignores -m elf_iamcu.
>
> --
> You are receiving this mail because:
> You are the assignee for the bug.
Comment 3 H.J. Lu 2015-10-14 19:43:37 UTC
(In reply to Cary Coutant from comment #2)
> Yes, gold uses the target of the first object file; only if there are
> no object files does it use the -m option (note that the option is
> marked "obsolete").

I think gold should honor -m option to avoid generating incorrect output.
Comment 4 H.J. Lu 2015-10-15 16:14:47 UTC
In llvm gold plugin tests, there are

test/tools/gold/PowerPC/mtriple.ll:; RUN: %gold -plugin
%llvmshlibdir/LLVMgold.so -m elf32ppc \
test/tools/gold/X86/bcsection.ll:; RUN: %gold -r -o %T/bcsection.o -m
elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so %T/bcsection.bco
test/tools/gold/X86/emit-llvm.ll:; RUN:     -m elf_x86_64
--plugin-opt=save-temps \
test/tools/gold/X86/emit-llvm.ll:; RUN:     -m elf_x86_64
--plugin-opt=disable-output \
test/tools/gold/X86/parallel.ll:; RUN: env
LD_PRELOAD=%llvmshlibdir/LLVMgold.so %gold -plugin
%llvmshlibdir/LLVMgold.so -u foo -u bar -plugin-opt jobs=2 -plugin-opt
save-temps -m elf_x86_64 -o %t %t.bc
test/tools/gold/X86/pr19901.ll:; RUN:     -shared -m elf_x86_64 -o
%t.so %t2.o %t.o
test/tools/gold/X86/remarks.ll:; RUN: %gold -m elf_x86_64 -plugin
%llvmshlibdir/LLVMgold.so \
test/tools/gold/X86/remarks.ll:; RUN: %gold -m elf_x86_64 -plugin
%llvmshlibdir/LLVMgold.so \
test/tools/gold/X86/slp-vectorize.ll:; RUN: %gold -m elf_x86_64
-plugin %llvmshlibdir/LLVMgold.so \
test/tools/gold/X86/vectorize.ll:; RUN: %gold -m elf_x86_64 -plugin
%llvmshlibdir/LLVMgold.so \

On Linux/x86-64, if I remove  -m elf32ppc from
test/tools/gold/PowerPC/mtriple.ll, I get

[hjl@gnu-6 bin]$ ./llvm-lit  --verbose
/export/gnu/import/git/llvm/test/tools/gold/PowerPC/mtriple.ll
-- Testing: 1 tests, 1 threads --
FAIL: LLVM :: tools/gold/PowerPC/mtriple.ll (1 of 1)
******************** TEST 'LLVM :: tools/gold/PowerPC/mtriple.ll'
FAILED ********************
Script:
--
llvm-as /export/ssd/git/llvm/test/tools/gold/PowerPC/mtriple.ll -o
/export/build/gnu/llvm-clang/build-x86_64-linux/test/tools/gold/PowerPC/Output/mtriple.ll.tmp.o
/export/build/gnu/binutils-gold/release/usr/local/bin/ld.gold -plugin
/export/build/gnu/llvm-clang/build-x86_64-linux/./lib/LLVMgold.so
-plugin-opt=mtriple=powerpc-linux-gnu
-plugin-opt=obj-path=/export/build/gnu/llvm-clang/build-x86_64-linux/test/tools/gold/PowerPC/Output/mtriple.ll.tmp3.o
    -shared /export/build/gnu/llvm-clang/build-x86_64-linux/test/tools/gold/PowerPC/Output/mtriple.ll.tmp.o
-o /export/build/gnu/llvm-clang/build-x86_64-linux/test/tools/gold/PowerPC/Output/mtriple.ll.tmp2
llvm-readobj --file-headers
/export/build/gnu/llvm-clang/build-x86_64-linux/test/tools/gold/PowerPC/Output/mtriple.ll.tmp2
| FileCheck  --check-prefix=DSO
/export/ssd/git/llvm/test/tools/gold/PowerPC/mtriple.ll
llvm-readobj --file-headers
/export/build/gnu/llvm-clang/build-x86_64-linux/test/tools/gold/PowerPC/Output/mtriple.ll.tmp3.o
| FileCheck --check-prefix=REL
/export/ssd/git/llvm/test/tools/gold/PowerPC/mtriple.ll
--
Exit Code: 1

Command Output (stderr):
--
/export/build/gnu/binutils-gold/release/usr/local/bin/ld.gold: error:
/export/build/gnu/llvm-clang/build-x86_64-linux/test/tools/gold/PowerPC/Output/mtriple.ll.tmp3.o:
incompatible target

--

********************
Testing Time: 0.02s
********************
Failing Tests (1):
    LLVM :: tools/gold/PowerPC/mtriple.ll

  Unexpected Failures: 1
[hjl@gnu-6 bin]$

It shows that -m elf32ppc is required.
Comment 5 Sourceware Commits 2015-10-16 15:09:16 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=6457197210144f50a696097c0d308d81d46d5510

commit 6457197210144f50a696097c0d308d81d46d5510
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Oct 16 08:04:58 2015 -0700

    Don't ignore "-m emulation" command line option
    
    Gold shouldn't ignore "-m emulation" command line option, which may
    lead to incorrect output.
    
    	PR gold/19119
    	* options.h (General_options): Remove "obsolete" from -m.
    	* parameters.cc (set_parameters_target): Check if input target
    	is compatible with output emulation set by "-m emulation".
Comment 6 H.J. Lu 2015-10-16 15:12:10 UTC
Fixed.
Comment 7 Sourceware Commits 2015-11-05 21:19:15 UTC
The master branch has been updated by Cary Coutant <ccoutant@sourceware.org>:

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

commit 96f9814df23564e16909bb5ba00de4a202c63417
Author: Cary Coutant <ccoutant@gmail.com>
Date:   Thu Nov 5 12:59:02 2015 -0800

    Revert patch for PR 19119, which led to PR 19172 and 19197.
    
    Gold does not support all the emulations that Gnu ld does, and supports
    only one spelling per target. The -m option is used only in the rare case
    where there are no ELF input files, and we produce an empty output file.
    In those cases, users are expected to supply a -m option naming one of
    the supported emulations. In the many cases where a build script provides
    an unnecessary -m option naming an emulation that gold does not support,
    we will simply ignore the option, as we did before the reverted patch.
    
    gold/
    	PR gold/19119
    	PR gold/19172
    	PR gold/19197
    	Revert commit 6457197210144f50a696097c0d308d81d46d5510:
    
    	2015-10-16  H.J. Lu  <hongjiu.lu@intel.com>
    
    		* options.h (General_options): Remove "obsolete" from -m.
    		* parameters.cc (set_parameters_target): Check if input target
    		is compatible with output emulation set by "-m emulation".
Comment 8 Cary Coutant 2015-11-05 22:35:44 UTC
The fix for this bug caused PR 19172 and PR 19197, so I'm reverting the patch and declaring this as "won't fix".
Comment 9 H.J. Lu 2015-11-05 23:00:12 UTC
Just for the record, when -m is really used to select the correct
emulation, gold and ld may require different emulations since ld
may not be able to use the one working for gold and gold may ignore
the one used by ld.