diff --git a/lld/test/wasm/emit-relocs.ll b/lld/test/wasm/emit-relocs.ll deleted file mode 100644 --- 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 --- /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 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -676,7 +676,10 @@ // 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"))