[lld] cb5bc75 - [lld][WebAssembly] Always include bss segments when `--emit-relocs` is used

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 15 17:03:53 PDT 2023


Author: Sam Clegg
Date: 2023-08-15T17:03:40-07:00
New Revision: cb5bc756808367d53c870716ce42611a563421e8

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

LOG: [lld][WebAssembly] Always include bss segments when `--emit-relocs` is used

If we don't do this then we end up with symbols that refer to
non-existent segments.

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

Added: 
    lld/test/wasm/emit-relocs.s

Modified: 
    lld/wasm/Writer.cpp

Removed: 
    lld/test/wasm/emit-relocs.ll


################################################################################
diff  --git a/lld/test/wasm/emit-relocs.ll b/lld/test/wasm/emit-relocs.ll
deleted file mode 100644
index c2d9e8fff12dc5..00000000000000
--- a/lld/test/wasm/emit-relocs.ll
+++ /dev/null
@@ -1,39 +0,0 @@
-; RUN: llc -filetype=obj %s -o %t.o
-; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/ret32.s -o %t.ret32.o
-; RUN: wasm-ld --emit-relocs -o %t.wasm %t.o %t.ret32.o
-; RUN: obj2yaml %t.wasm | FileCheck %s
-
-target triple = "wasm32-unknown-unknown"
-
-declare i32 @ret32(float)
-
-define void @unused_function() {
-  ret void
-}
-
-define hidden void @_start() local_unnamed_addr #0 {
-entry:
-  call i32 @ret32(float 0.0)
-  ret void
-}
-
-; CHECK:        - Type:            CODE
-; CHECK-NEXT:     Relocations:
-; CHECK-NEXT:       - Type:            R_WASM_FUNCTION_INDEX_LEB
-; CHECK-NEXT:         Index:           1
-; CHECK-NEXT:         Offset:          0x9
-
-; CHECK:        - Type:            CUSTOM
-; CHECK-NEXT:     Name:            linking
-; CHECK-NEXT:     Version:         2
-; CHECK-NEXT:     SymbolTable:
-; CHECK-NEXT:       - Index:           0
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Name:            _start
-; CHECK-NEXT:         Flags:           [  ]
-; CHECK-NEXT:         Function:        0
-; CHECK-NEXT:       - Index:           1
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Name:            ret32
-; CHECK-NEXT:         Flags:           [ VISIBILITY_HIDDEN ]
-; CHECK-NEXT:         Function:        1

diff  --git a/lld/test/wasm/emit-relocs.s b/lld/test/wasm/emit-relocs.s
new file mode 100644
index 00000000000000..28ce607167382e
--- /dev/null
+++ b/lld/test/wasm/emit-relocs.s
@@ -0,0 +1,56 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/ret32.s -o %t.ret32.o
+# RUN: wasm-ld --emit-relocs -o %t.wasm %t.o %t.ret32.o
+# RUN: obj2yaml %t.wasm | FileCheck %s
+
+.functype ret32 (f32) -> (i32)
+
+unused_function:
+  .functype unused_function () -> ()
+  end_function
+
+.globl _start
+_start:
+  .functype _start () -> ()
+  f32.const 0.0
+  call ret32
+  drop
+  i32.const foo
+  drop
+  end_function
+
+.section .bss.data,"",@
+.p2align        2
+foo:
+  .int32  0
+  .size   foo, 4
+
+# CHECK:        - Type:            CODE
+# CHECK-NEXT:     Relocations:
+# CHECK-NEXT:       - Type:            R_WASM_FUNCTION_INDEX_LEB
+# CHECK-NEXT:         Index:           1
+# CHECK-NEXT:         Offset:          0x9
+
+# CHECK:        - Type:            DATA
+# CHECK-NEXT:     Segments:
+# CHECK-NEXT:       - SectionOffset:   7
+# CHECK-NEXT:         InitFlags:       0
+# CHECK-NEXT:         Offset:
+# CHECK-NEXT:           Opcode:          I32_CONST
+# CHECK-NEXT:           Value:           1024
+# CHECK-NEXT:         Content:         '00000000'
+
+# CHECK:        - Type:            CUSTOM
+# CHECK-NEXT:     Name:            linking
+# CHECK-NEXT:     Version:         2
+# CHECK-NEXT:     SymbolTable:
+# CHECK-NEXT:       - Index:           0
+# CHECK-NEXT:         Kind:            FUNCTION
+# CHECK-NEXT:         Name:            _start
+# CHECK-NEXT:         Flags:           [  ]
+# CHECK-NEXT:         Function:        0
+# CHECK-NEXT:       - Index:           1
+# CHECK-NEXT:         Kind:            FUNCTION
+# CHECK-NEXT:         Name:            ret32
+# CHECK-NEXT:         Flags:           [ VISIBILITY_HIDDEN ]
+# CHECK-NEXT:         Function:        1

diff  --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index eb64783156e730..f25d358dc5bae6 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -676,7 +676,10 @@ void Writer::populateTargetFeatures() {
   // memory is not being imported then we can assume its zero initialized.
   // In the case the memory is imported, and we can use the memory.fill
   // instruction, then we can also avoid including the segments.
-  if (config->memoryImport.has_value() && !allowed.count("bulk-memory"))
+  // Finally, if we are emitting relocations, they may refer to locations within
+  // the bss segments, so these segments need to exist in the binary.
+  if (config->emitRelocs ||
+      (config->memoryImport.has_value() && !allowed.count("bulk-memory")))
     config->emitBssSegments = true;
 
   if (allowed.count("extended-const"))


        


More information about the llvm-commits mailing list