[lld] r364338 - [WebAssembly] Error on archives without a symbol index

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 25 10:49:36 PDT 2019


Author: sbc
Date: Tue Jun 25 10:49:35 2019
New Revision: 364338

URL: http://llvm.org/viewvc/llvm-project?rev=364338&view=rev
Log:
[WebAssembly] Error on archives without a symbol index

This is fairly common with wasm since GNU ar (most likely the system ar)
doesn't support the wasm object format so user who don't override AR
will end up with archives without an index.  We don't want to silently
ignore this issue.

In the future we could choose to instead behave like the ELF backend and
read the symbols from each object file in the archive if they are all of
the same type.  However, error'ing out seem like a conservative approach
for now.

Fixes: PR42376

Differential Revision: https://reviews.llvm.org/D63739

Added:
    lld/trunk/test/wasm/archive-no-index.ll
Modified:
    lld/trunk/wasm/Driver.cpp

Added: lld/trunk/test/wasm/archive-no-index.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/archive-no-index.ll?rev=364338&view=auto
==============================================================================
--- lld/trunk/test/wasm/archive-no-index.ll (added)
+++ lld/trunk/test/wasm/archive-no-index.ll Tue Jun 25 10:49:35 2019
@@ -0,0 +1,13 @@
+; Tests error on archive file without a symbol table
+; RUN: llvm-as -o %t.o %s
+; RUN: llvm-as -o %t.archive.o %S/Inputs/archive1.ll
+; RUN: rm -f %t.a
+; RUN: llvm-ar crS %t.a %t.archive.o
+
+; RUN: not wasm-ld -o out.wasm %t.o %t.a 2>&1 | FileCheck %s
+
+define i32 @_start() {
+  ret i32 0
+}
+
+; CHECK: archive has no index; run ranlib to add one

Modified: lld/trunk/wasm/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Driver.cpp?rev=364338&r1=364337&r2=364338&view=diff
==============================================================================
--- lld/trunk/wasm/Driver.cpp (original)
+++ lld/trunk/wasm/Driver.cpp Tue Jun 25 10:49:35 2019
@@ -225,6 +225,11 @@ void LinkerDriver::addFile(StringRef Pat
 
   switch (identify_magic(MBRef.getBuffer())) {
   case file_magic::archive: {
+    SmallString<128> ImportFile = Path;
+    path::replace_extension(ImportFile, ".imports");
+    if (fs::exists(ImportFile))
+      readImportFile(ImportFile.str());
+
     // Handle -whole-archive.
     if (InWholeArchive) {
       for (MemoryBufferRef &M : getArchiveMembers(MBRef))
@@ -232,10 +237,13 @@ void LinkerDriver::addFile(StringRef Pat
       return;
     }
 
-    SmallString<128> ImportFile = Path;
-    path::replace_extension(ImportFile, ".imports");
-    if (fs::exists(ImportFile))
-      readImportFile(ImportFile.str());
+    std::unique_ptr<Archive> File =
+        CHECK(Archive::create(MBRef), Path + ": failed to parse archive");
+
+    if (!File->isEmpty() && !File->hasSymbolTable()) {
+      error(MBRef.getBufferIdentifier() +
+            ": archive has no index; run ranlib to add one");
+    }
 
     Files.push_back(make<ArchiveFile>(MBRef));
     return;




More information about the llvm-commits mailing list