[PATCH] D63739: [WebAssembly] Error out on archives without and index

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 14:37:57 PDT 2019


sbc100 created this revision.
Herald added subscribers: llvm-commits, arphaman, sunfish, aheejin, jgravelle-google, dschuff.
Herald added a project: LLVM.
sbc100 added a reviewer: ruiu.

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.

An alternative would be to behave like the ELF version lld 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 appoach for
now.

Fixes: PR42376


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D63739

Files:
  lld/test/wasm/archive-no-index.ll
  lld/wasm/Driver.cpp


Index: lld/wasm/Driver.cpp
===================================================================
--- lld/wasm/Driver.cpp
+++ lld/wasm/Driver.cpp
@@ -225,6 +225,11 @@
 
   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 @@
       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;
Index: lld/test/wasm/archive-no-index.ll
===================================================================
--- /dev/null
+++ lld/test/wasm/archive-no-index.ll
@@ -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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63739.206301.patch
Type: text/x-patch
Size: 1627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190624/3986a470/attachment.bin>


More information about the llvm-commits mailing list