[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
This commit is contained in:
Sam Clegg 2023-08-15 14:49:37 -07:00
parent aa2c701b5d
commit cb5bc75680
3 changed files with 60 additions and 40 deletions

View File

@ -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

View File

@ -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

View File

@ -676,7 +676,10 @@ done:
// 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"))