[lld] d01e673 - [lld][WebAssembly] Fix crash calling weakly undefined function in PIC code

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 17 17:10:19 PDT 2021


Author: Sam Clegg
Date: 2021-06-17T16:49:02-07:00
New Revision: d01e673a9f046913070bd950183480c9bd15956b

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

LOG: [lld][WebAssembly] Fix crash calling weakly undefined function in PIC code

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

Added: 
    

Modified: 
    lld/test/wasm/weak-undefined-pic.s
    lld/wasm/InputChunks.cpp
    lld/wasm/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/test/wasm/weak-undefined-pic.s b/lld/test/wasm/weak-undefined-pic.s
index b0f31e1a12d90..b0fb98583a3e8 100644
--- a/lld/test/wasm/weak-undefined-pic.s
+++ b/lld/test/wasm/weak-undefined-pic.s
@@ -22,6 +22,7 @@ get_foo_addr:
 _start:
   .functype _start () -> ()
   call get_foo_addr
+  call foo
   end_function
 
 .weak foo
@@ -75,7 +76,10 @@ _start:
 # RUN: obj2yaml %t3.wasm | FileCheck %s --check-prefix=IMPORT
 
 #      IMPORT:  - Type:            IMPORT
-#      IMPORT:      - Module:          GOT.func
+#      IMPORT:        Field:           foo
+# IMPORT-NEXT:        Kind:            FUNCTION
+# IMPORT-NEXT:        SigIndex:        0
+# IMPORT-NEXT:      - Module:          GOT.func
 # IMPORT-NEXT:        Field:           foo
 # IMPORT-NEXT:        Kind:            GLOBAL
 # IMPORT-NEXT:        GlobalType:      I32

diff  --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index 99819713c2817..4bf7b5b3f4ac8 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -106,13 +106,12 @@ void InputChunk::relocate(uint8_t *buf) const {
 
   for (const WasmRelocation &rel : relocations) {
     uint8_t *loc = buf + rel.Offset - inputSectionOffset;
-    auto value = file->calcNewValue(rel, tombstone, this);
     LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(rel.Type));
     if (rel.Type != R_WASM_TYPE_INDEX_LEB)
       LLVM_DEBUG(dbgs() << " sym=" << file->getSymbols()[rel.Index]->getName());
     LLVM_DEBUG(dbgs() << " addend=" << rel.Addend << " index=" << rel.Index
-                      << " value=" << value << " offset=" << rel.Offset
-                      << "\n");
+                      << " offset=" << rel.Offset << "\n");
+    auto value = file->calcNewValue(rel, tombstone, this);
 
     switch (rel.Type) {
     case R_WASM_TYPE_INDEX_LEB:

diff  --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 5512e5dcb0892..ddc79f8b434a5 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -546,7 +546,7 @@ void Writer::populateTargetFeatures() {
 static bool shouldImport(Symbol *sym) {
   if (!sym->isUndefined())
     return false;
-  if (sym->isWeak() && !config->relocatable)
+  if (sym->isWeak() && !config->relocatable && !config->isPic)
     return false;
   if (!sym->isLive())
     return false;


        


More information about the llvm-commits mailing list