[lld] b8c2d60 - [WebAssembly] Improved LLD error messages in case of mixed wasm32/wasm64 object files

Wouter van Oortmerssen via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 29 17:16:28 PDT 2020


Author: Wouter van Oortmerssen
Date: 2020-10-29T17:15:59-07:00
New Revision: b8c2d60df522d553a0752772aeffb4b2ad84678e

URL: https://github.com/llvm/llvm-project/commit/b8c2d60df522d553a0752772aeffb4b2ad84678e
DIFF: https://github.com/llvm/llvm-project/commit/b8c2d60df522d553a0752772aeffb4b2ad84678e.diff

LOG: [WebAssembly] Improved LLD error messages in case of mixed wasm32/wasm64 object files

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

Added: 
    lld/test/wasm/check-arch-32-in-64.test
    lld/test/wasm/check-arch-64-in-32.test

Modified: 
    lld/wasm/InputChunks.cpp
    lld/wasm/InputFiles.cpp
    lld/wasm/InputFiles.h

Removed: 
    


################################################################################
diff  --git a/lld/test/wasm/check-arch-32-in-64.test b/lld/test/wasm/check-arch-32-in-64.test
new file mode 100644
index 000000000000..b4eb925d0f04
--- /dev/null
+++ b/lld/test/wasm/check-arch-32-in-64.test
@@ -0,0 +1,4 @@
+RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/start.s -o %t.o
+RUN: not wasm-ld -mwasm64 %t.o -o %t.wasm 2>&1 | FileCheck %s
+
+CHECK: wasm32 object file can't be linked in wasm64 mode

diff  --git a/lld/test/wasm/check-arch-64-in-32.test b/lld/test/wasm/check-arch-64-in-32.test
new file mode 100644
index 000000000000..8b23e5f5c1de
--- /dev/null
+++ b/lld/test/wasm/check-arch-64-in-32.test
@@ -0,0 +1,4 @@
+RUN: llvm-mc -filetype=obj -triple=wasm64-unknown-unknown %p/Inputs/start.s -o %t.o
+RUN: not wasm-ld %t.o -o %t.wasm 2>&1 | FileCheck %s
+
+CHECK: must specify -mwasm64 to process wasm64 object files

diff  --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index b85c8cc5ab91..0db4d9d9f584 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -60,6 +60,7 @@ void InputChunk::verifyRelocTargets() const {
   for (const WasmRelocation &rel : relocations) {
     uint64_t existingValue;
     unsigned bytesRead = 0;
+    unsigned paddedLEBWidth = 5;
     auto offset = rel.Offset - getInputSectionOffset();
     const uint8_t *loc = data().data() + offset;
     switch (rel.Type) {
@@ -68,17 +69,23 @@ void InputChunk::verifyRelocTargets() const {
     case R_WASM_GLOBAL_INDEX_LEB:
     case R_WASM_EVENT_INDEX_LEB:
     case R_WASM_MEMORY_ADDR_LEB:
+      existingValue = decodeULEB128(loc, &bytesRead);
+      break;
     case R_WASM_MEMORY_ADDR_LEB64:
       existingValue = decodeULEB128(loc, &bytesRead);
+      paddedLEBWidth = 10;
       break;
     case R_WASM_TABLE_INDEX_SLEB:
-    case R_WASM_TABLE_INDEX_SLEB64:
     case R_WASM_TABLE_INDEX_REL_SLEB:
     case R_WASM_MEMORY_ADDR_SLEB:
-    case R_WASM_MEMORY_ADDR_SLEB64:
     case R_WASM_MEMORY_ADDR_REL_SLEB:
+      existingValue = static_cast<uint64_t>(decodeSLEB128(loc, &bytesRead));
+      break;
+    case R_WASM_TABLE_INDEX_SLEB64:
+    case R_WASM_MEMORY_ADDR_SLEB64:
     case R_WASM_MEMORY_ADDR_REL_SLEB64:
       existingValue = static_cast<uint64_t>(decodeSLEB128(loc, &bytesRead));
+      paddedLEBWidth = 10;
       break;
     case R_WASM_TABLE_INDEX_I32:
     case R_WASM_MEMORY_ADDR_I32:
@@ -95,8 +102,8 @@ void InputChunk::verifyRelocTargets() const {
       llvm_unreachable("unknown relocation type");
     }
 
-    if (bytesRead && bytesRead != 5)
-      warn("expected LEB at relocation site be 5-byte padded");
+    if (bytesRead && bytesRead != paddedLEBWidth)
+      warn("expected LEB at relocation site be 5/10-byte padded");
 
     if (rel.Type != R_WASM_GLOBAL_INDEX_LEB &&
         rel.Type != R_WASM_GLOBAL_INDEX_I32) {

diff  --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp
index d6848211c983..36df781c041d 100644
--- a/lld/wasm/InputFiles.cpp
+++ b/lld/wasm/InputFiles.cpp
@@ -40,6 +40,18 @@ std::string toString(const wasm::InputFile *file) {
 }
 
 namespace wasm {
+
+void InputFile::checkArch(Triple::ArchType arch) const {
+  bool is64 = arch == Triple::wasm64;
+  if (is64 && !config->is64.hasValue()) {
+    fatal(toString(this) +
+          ": must specify -mwasm64 to process wasm64 object files");
+  } else if (config->is64.getValueOr(false) != is64) {
+    fatal(toString(this) +
+          ": wasm32 object file can't be linked in wasm64 mode");
+  }
+}
+
 std::unique_ptr<llvm::TarWriter> tar;
 
 Optional<MemoryBufferRef> readFile(StringRef path) {
@@ -286,6 +298,8 @@ void ObjFile::parse(bool ignoreComdats) {
   bin.release();
   wasmObj.reset(obj);
 
+  checkArch(obj->getArch());
+
   // Build up a map of function indices to table indices for use when
   // verifying the existing table index relocations
   uint32_t totalFunctions =
@@ -583,12 +597,7 @@ void BitcodeFile::parse() {
     error(toString(this) + ": machine type must be wasm32 or wasm64");
     return;
   }
-  bool is64 = t.getArch() == Triple::wasm64;
-  if (config->is64.hasValue() && *config->is64 != is64) {
-    error(toString(this) + ": machine type for all bitcode files must match");
-    return;
-  }
-  config->is64 = is64;
+  checkArch(t.getArch());
   std::vector<bool> keptComdats;
   for (StringRef s : obj->getComdatTable())
     keptComdats.push_back(symtab->addComdat(s));

diff  --git a/lld/wasm/InputFiles.h b/lld/wasm/InputFiles.h
index 0abd47a0ac20..253224a011c7 100644
--- a/lld/wasm/InputFiles.h
+++ b/lld/wasm/InputFiles.h
@@ -13,6 +13,7 @@
 #include "lld/Common/LLVM.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/LTO/LTO.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/Wasm.h"
@@ -68,6 +69,9 @@ class InputFile {
 protected:
   InputFile(Kind k, MemoryBufferRef m)
       : mb(m), fileKind(k), live(!config->gcSections) {}
+
+  void checkArch(llvm::Triple::ArchType arch) const;
+
   MemoryBufferRef mb;
 
   // List of all symbols referenced or defined by this file.


        


More information about the llvm-commits mailing list