diff --git a/lld/test/wasm/archive-no-index.ll b/lld/test/wasm/archive-no-index.ll new file mode 100644 --- /dev/null +++ b/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 diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp --- a/lld/wasm/Driver.cpp +++ b/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 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(MBRef)); return;