[lld] [lld][WebAssembly] Report Unsupported PIC Relocations as Errors (PR #104926)

Luc Blaeser via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 16 10:34:44 PDT 2024


https://github.com/luc-blaeser updated https://github.com/llvm/llvm-project/pull/104926

>From 020ba6c601061ac982a4be24d2a49e34da5d5706 Mon Sep 17 00:00:00 2001
From: luc-blaeser <luc.blaeser at dfinity.org>
Date: Tue, 20 Aug 2024 14:35:37 +0200
Subject: [PATCH 1/3] Report unsupported PIC relocations in code segment

---
 lld/test/wasm/unsupported-pic-relocations.s   | 47 ++++++++++++++++++
 lld/test/wasm/unsupported-pic-relocations64.s | 48 +++++++++++++++++++
 lld/wasm/Relocations.cpp                      | 13 +++++
 3 files changed, 108 insertions(+)
 create mode 100644 lld/test/wasm/unsupported-pic-relocations.s
 create mode 100644 lld/test/wasm/unsupported-pic-relocations64.s

diff --git a/lld/test/wasm/unsupported-pic-relocations.s b/lld/test/wasm/unsupported-pic-relocations.s
new file mode 100644
index 00000000000000..81b9fdde83e677
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations.s
@@ -0,0 +1,47 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
+
+# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=report-all 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null  --warn-unresolved-symbols 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=ignore-all 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=import-dynamic 2>&1 | \
+# RUN:   FileCheck %s
+
+.globaltype __memory_base, i32, immutable
+.globaltype	__table_base, i32, immutable
+
+.functype external_func () -> ()
+
+call_undefined_function:
+    .functype call_undefined_function () -> ()
+    global.get  __table_base
+    i32.const external_func at TBREL
+    # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB is not supported against an undefined symbol `external_func`
+    i32.add
+    call_indirect () -> ()
+    end_function
+    
+access_undefined_data:
+    .functype access_undefined_data () -> ()
+    global.get  __memory_base
+    i32.const external_data at MBREL
+    # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB is not supported against an undefined symbol `external_data`
+    i32.add
+    i32.load 0
+    drop
+    end_function
+
+.globl _start
+_start:
+    .functype _start () -> ()
+    call call_undefined_function
+    call access_undefined_data
+    end_function
diff --git a/lld/test/wasm/unsupported-pic-relocations64.s b/lld/test/wasm/unsupported-pic-relocations64.s
new file mode 100644
index 00000000000000..cae78d95862b48
--- /dev/null
+++ b/lld/test/wasm/unsupported-pic-relocations64.s
@@ -0,0 +1,48 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm64-unknown-unknown -o %t.o %s
+
+# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=report-all 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null  --warn-unresolved-symbols 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=ignore-all 2>&1 | \
+# RUN:   FileCheck %s
+
+# RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=import-dynamic 2>&1 | \
+# RUN:   FileCheck %s
+
+.globaltype __memory_base, i64, immutable
+.globaltype	__table_base, i64, immutable
+
+.functype external_func () -> ()
+
+call_undefined_function:
+    .functype call_undefined_function () -> ()
+    global.get  __table_base
+    i64.const external_func at TBREL
+    # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB64 is not supported against an undefined symbol `external_func`
+    i64.add
+    i32.wrap_i64 # Remove when table64 is supported
+    call_indirect () -> ()
+    end_function
+    
+access_undefined_data:
+    .functype access_undefined_data () -> ()
+    global.get  __memory_base
+    i64.const external_data at MBREL
+    # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB64 is not supported against an undefined symbol `external_data`
+    i64.add
+    i64.load 0
+    drop
+    end_function
+
+.globl _start
+_start:
+    .functype _start () -> ()
+    call call_undefined_function
+    call access_undefined_data
+    end_function
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 6f33a4f28a9d09..6b177f4f34bb6f 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -170,6 +170,19 @@ void scanRelocations(InputChunk *chunk) {
         if (requiresGOTAccess(sym))
           addGOTEntry(sym);
         break;
+      case R_WASM_TABLE_INDEX_REL_SLEB:
+      case R_WASM_TABLE_INDEX_REL_SLEB64:
+      case R_WASM_MEMORY_ADDR_REL_SLEB:
+      case R_WASM_MEMORY_ADDR_REL_SLEB64:
+        // These relocation types are only present in the code section and
+        // are not supported. It would require replacing the constant by using
+        // a GOT global.
+        if (sym->isUndefined())
+          error(toString(file) + ": relocation " +
+                relocTypeToString(reloc.Type) +
+                " is not supported against an undefined symbol `" +
+                toString(*sym) + "`");
+        break;
       }
     }
 

>From 9684416239733bb4f94a60cf94cad6853b357c1a Mon Sep 17 00:00:00 2001
From: luc-blaeser <luc.blaeser at dfinity.org>
Date: Wed, 21 Aug 2024 09:35:55 +0200
Subject: [PATCH 2/3] Adjust condition for unsupported PIC relocations

---
 lld/wasm/Relocations.cpp | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 6b177f4f34bb6f..8465eab7639dbe 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -170,18 +170,21 @@ void scanRelocations(InputChunk *chunk) {
         if (requiresGOTAccess(sym))
           addGOTEntry(sym);
         break;
+      }
+    }
+
+    if (sym->isUndefined()) {
+      switch (reloc.Type) {
       case R_WASM_TABLE_INDEX_REL_SLEB:
       case R_WASM_TABLE_INDEX_REL_SLEB64:
       case R_WASM_MEMORY_ADDR_REL_SLEB:
       case R_WASM_MEMORY_ADDR_REL_SLEB64:
         // These relocation types are only present in the code section and
-        // are not supported. It would require replacing the constant by using
-        // a GOT global.
-        if (sym->isUndefined())
-          error(toString(file) + ": relocation " +
-                relocTypeToString(reloc.Type) +
-                " is not supported against an undefined symbol `" +
-                toString(*sym) + "`");
+        // are not supported for undefined symbols. It would require replacing
+        // the constant by using a GOT global.
+        error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
+              " is not supported against an undefined symbol `" +
+              toString(*sym) + "`");
         break;
       }
     }

>From 4a66aaff86f201b54c9fcdfbb5ca6c335a10b301 Mon Sep 17 00:00:00 2001
From: luc-blaeser <luc.blaeser at dfinity.org>
Date: Thu, 22 Aug 2024 15:26:16 +0200
Subject: [PATCH 3/3] Code and test refactoring

---
 lld/test/wasm/unsupported-pic-relocations.s   | 22 ++++++------------
 lld/test/wasm/unsupported-pic-relocations64.s | 23 ++++++-------------
 lld/wasm/Relocations.cpp                      |  6 ++---
 3 files changed, 17 insertions(+), 34 deletions(-)

diff --git a/lld/test/wasm/unsupported-pic-relocations.s b/lld/test/wasm/unsupported-pic-relocations.s
index 81b9fdde83e677..ea32e8468cdb4d 100644
--- a/lld/test/wasm/unsupported-pic-relocations.s
+++ b/lld/test/wasm/unsupported-pic-relocations.s
@@ -15,33 +15,25 @@
 # RUN: not wasm-ld --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=import-dynamic 2>&1 | \
 # RUN:   FileCheck %s
 
-.globaltype __memory_base, i32, immutable
-.globaltype	__table_base, i32, immutable
-
 .functype external_func () -> ()
 
-call_undefined_function:
-    .functype call_undefined_function () -> ()
-    global.get  __table_base
+use_undefined_function:
+    .functype use_undefined_function () -> ()
     i32.const external_func at TBREL
     # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB is not supported against an undefined symbol `external_func`
-    i32.add
-    call_indirect () -> ()
+    drop
     end_function
     
-access_undefined_data:
-    .functype access_undefined_data () -> ()
-    global.get  __memory_base
+use_undefined_data:
+    .functype use_undefined_data () -> ()
     i32.const external_data at MBREL
     # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB is not supported against an undefined symbol `external_data`
-    i32.add
-    i32.load 0
     drop
     end_function
 
 .globl _start
 _start:
     .functype _start () -> ()
-    call call_undefined_function
-    call access_undefined_data
+    call use_undefined_function
+    call use_undefined_data
     end_function
diff --git a/lld/test/wasm/unsupported-pic-relocations64.s b/lld/test/wasm/unsupported-pic-relocations64.s
index cae78d95862b48..db9707b7fbac5e 100644
--- a/lld/test/wasm/unsupported-pic-relocations64.s
+++ b/lld/test/wasm/unsupported-pic-relocations64.s
@@ -15,34 +15,25 @@
 # RUN: not wasm-ld -mwasm64 --experimental-pic -shared %t.o -o /dev/null  --unresolved-symbols=import-dynamic 2>&1 | \
 # RUN:   FileCheck %s
 
-.globaltype __memory_base, i64, immutable
-.globaltype	__table_base, i64, immutable
-
 .functype external_func () -> ()
 
-call_undefined_function:
-    .functype call_undefined_function () -> ()
-    global.get  __table_base
+use_undefined_function:
+    .functype use_undefined_function () -> ()
     i64.const external_func at TBREL
     # CHECK: error: {{.*}}.o: relocation R_WASM_TABLE_INDEX_REL_SLEB64 is not supported against an undefined symbol `external_func`
-    i64.add
-    i32.wrap_i64 # Remove when table64 is supported
-    call_indirect () -> ()
+    drop
     end_function
     
-access_undefined_data:
-    .functype access_undefined_data () -> ()
-    global.get  __memory_base
+use_undefined_data:
+    .functype use_undefined_data () -> ()
     i64.const external_data at MBREL
     # CHECK: error: {{.*}}.o: relocation R_WASM_MEMORY_ADDR_REL_SLEB64 is not supported against an undefined symbol `external_data`
-    i64.add
-    i64.load 0
     drop
     end_function
 
 .globl _start
 _start:
     .functype _start () -> ()
-    call call_undefined_function
-    call access_undefined_data
+    call use_undefined_function
+    call use_undefined_data
     end_function
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 8465eab7639dbe..2dbfe335494711 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -179,9 +179,9 @@ void scanRelocations(InputChunk *chunk) {
       case R_WASM_TABLE_INDEX_REL_SLEB64:
       case R_WASM_MEMORY_ADDR_REL_SLEB:
       case R_WASM_MEMORY_ADDR_REL_SLEB64:
-        // These relocation types are only present in the code section and
-        // are not supported for undefined symbols. It would require replacing
-        // the constant by using a GOT global.
+        // These relocation types are for symbols that exists relative to
+        // `__memory_base` or `__table_base` and as such only make sense for
+        // defined symbols.
         error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
               " is not supported against an undefined symbol `" +
               toString(*sym) + "`");



More information about the llvm-commits mailing list