This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Correct error message when OUTPUT_FORMAT is used
ClosedPublic

Authored by smeenai on Mar 12 2020, 3:59 PM.

Details

Summary

Any OUTPUT_FORMAT in a linker script overrides the emulation passed on
the command line, so record the passed bfdname and use that in the error
message about incompatible input files.

This prevents confusing error messages. For example, if you explicitly
pass -m elf_x86_64 to LLD but accidentally include a linker script
which sets OUTPUT_FORMAT(elf32-i386), LLD would previously complain
about your input files being compatible with elf_x86_64, which isn't the
actual issue, and is confusing because the input files are in fact
x86-64 ELF files.

Interestingly enough, this also prevents a segfault! When we don't pass
-m and we have an object file which is incompatible with the
OUTPUT_FORMAT set by a linker script, the object file is checked for
compatibility before it's added to the objectFiles vector.
config->emulation, objectFiles, and sharedFiles will all be empty, so
we'll attempt to access bitcodeFiles[0], but bitcodeFiles is also empty,
so we'll segfault. This commit prevents the segfault by adding
OUTPUT_FORMAT as a possible source of machine configuration, and it also
adds an llvm_unreachable to diagnose similar issues in the future.

Diff Detail

Event Timeline

smeenai created this revision.Mar 12 2020, 3:59 PM

The logic is good.

lld/ELF/ScriptParser.cpp
414–415

The local variable can be omitted now.

smeenai marked an inline comment as done.Mar 12 2020, 6:23 PM
smeenai added inline comments.
lld/ELF/ScriptParser.cpp
414–415

It's also used in line 423 below, but I'm happy to change all uses.

smeenai updated this revision to Diff 250116.Mar 12 2020, 6:27 PM

Eliminate local variable

ruiu accepted this revision.Mar 12 2020, 10:24 PM

LGTM

This revision is now accepted and ready to land.Mar 12 2020, 10:24 PM
This revision was automatically updated to reflect the committed changes.
grimar added inline comments.Mar 13 2020, 1:52 AM
lld/ELF/ScriptParser.cpp
422

I think we might want to have a test that uses something like 'foobar-freebsd'
and checks that we print foobar-freebsd and not just foobar contained in s.

smeenai marked an inline comment as done.Mar 13 2020, 2:23 PM
smeenai added inline comments.
lld/ELF/ScriptParser.cpp
422
grimar added inline comments.Mar 14 2020, 7:11 AM
lld/ELF/ScriptParser.cpp
422