[PATCH] D76109: [ELF] Correct error message when OUTPUT_FORMAT is used

Shoaib Meenai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 16:18:34 PDT 2020


smeenai created this revision.
smeenai added reviewers: ruiu, grimar, MaskRay.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76109

Files:
  lld/ELF/Config.h
  lld/ELF/InputFiles.cpp
  lld/ELF/ScriptParser.cpp
  lld/test/ELF/incompatible.s


Index: lld/test/ELF/incompatible.s
===================================================================
--- lld/test/ELF/incompatible.s
+++ lld/test/ELF/incompatible.s
@@ -44,6 +44,14 @@
 // RUN:   FileCheck --check-prefix=SO-AND-C-I386 %s
 // SO-AND-C-I386: c.o is incompatible with elf_i386
 
+// RUN: echo 'OUTPUT_FORMAT(elf32-i386)' > %t.script
+// RUN: not ld.lld %t.script %ta.o -o /dev/null 2>&1 | \
+// RUN:   FileCheck --check-prefix=A-AND-SCRIPT %s
+// RUN: not ld.lld %ta.o %t.script -o /dev/null 2>&1 | \
+// RUN:   FileCheck --check-prefix=A-AND-SCRIPT %s
+// RUN: not ld.lld -m elf_x86_64 %ta.o %t.script -o /dev/null 2>&1 | \
+// RUN:   FileCheck --check-prefix=A-AND-SCRIPT %s
+// A-AND-SCRIPT: a.o is incompatible with elf32-i386
 
 // We used to fail to identify this incompatibility and crash trying to
 // read a 64 bit file as a 32 bit one.
Index: lld/ELF/ScriptParser.cpp
===================================================================
--- lld/ELF/ScriptParser.cpp
+++ lld/ELF/ScriptParser.cpp
@@ -413,6 +413,7 @@
   expect("(");
 
   StringRef name = unquote(next());
+  config->bfdname = name;
   StringRef s = name;
   if (s.consume_back("-freebsd"))
     config->osabi = ELFOSABI_FREEBSD;
Index: lld/ELF/InputFiles.cpp
===================================================================
--- lld/ELF/InputFiles.cpp
+++ lld/ELF/InputFiles.cpp
@@ -138,8 +138,10 @@
       return true;
   }
 
-  if (!config->emulation.empty()) {
-    error(toString(file) + " is incompatible with " + config->emulation);
+  StringRef target =
+      !config->bfdname.empty() ? config->bfdname : config->emulation;
+  if (!target.empty()) {
+    error(toString(file) + " is incompatible with " + target);
     return false;
   }
 
@@ -148,8 +150,11 @@
     existing = objectFiles[0];
   else if (!sharedFiles.empty())
     existing = sharedFiles[0];
-  else
+  else if (!bitcodeFiles.empty())
     existing = bitcodeFiles[0];
+  else
+    llvm_unreachable("Must have -m, OUTPUT_FORMAT or existing input file to "
+                     "determine target emulation");
 
   error(toString(file) + " is incompatible with " + toString(existing));
   return false;
Index: lld/ELF/Config.h
===================================================================
--- lld/ELF/Config.h
+++ lld/ELF/Config.h
@@ -90,6 +90,7 @@
   uint32_t andFeatures = 0;
   llvm::CachePruningPolicy thinLTOCachePolicy;
   llvm::StringMap<uint64_t> sectionStartMap;
+  llvm::StringRef bfdname;
   llvm::StringRef chroot;
   llvm::StringRef dynamicLinker;
   llvm::StringRef dwoDir;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76109.250084.patch
Type: text/x-patch
Size: 2558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200312/31d3d1cb/attachment.bin>


More information about the llvm-commits mailing list