Skip to content

Commit cb5bc75

Browse files
committed
[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
1 parent aa2c701 commit cb5bc75

File tree

3 files changed

+60
-40
lines changed

3 files changed

+60
-40
lines changed

lld/test/wasm/emit-relocs.ll

Lines changed: 0 additions & 39 deletions
This file was deleted.

lld/test/wasm/emit-relocs.s

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.o
2+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/ret32.s -o %t.ret32.o
3+
# RUN: wasm-ld --emit-relocs -o %t.wasm %t.o %t.ret32.o
4+
# RUN: obj2yaml %t.wasm | FileCheck %s
5+
6+
.functype ret32 (f32) -> (i32)
7+
8+
unused_function:
9+
.functype unused_function () -> ()
10+
end_function
11+
12+
.globl _start
13+
_start:
14+
.functype _start () -> ()
15+
f32.const 0.0
16+
call ret32
17+
drop
18+
i32.const foo
19+
drop
20+
end_function
21+
22+
.section .bss.data,"",@
23+
.p2align 2
24+
foo:
25+
.int32 0
26+
.size foo, 4
27+
28+
# CHECK: - Type: CODE
29+
# CHECK-NEXT: Relocations:
30+
# CHECK-NEXT: - Type: R_WASM_FUNCTION_INDEX_LEB
31+
# CHECK-NEXT: Index: 1
32+
# CHECK-NEXT: Offset: 0x9
33+
34+
# CHECK: - Type: DATA
35+
# CHECK-NEXT: Segments:
36+
# CHECK-NEXT: - SectionOffset: 7
37+
# CHECK-NEXT: InitFlags: 0
38+
# CHECK-NEXT: Offset:
39+
# CHECK-NEXT: Opcode: I32_CONST
40+
# CHECK-NEXT: Value: 1024
41+
# CHECK-NEXT: Content: '00000000'
42+
43+
# CHECK: - Type: CUSTOM
44+
# CHECK-NEXT: Name: linking
45+
# CHECK-NEXT: Version: 2
46+
# CHECK-NEXT: SymbolTable:
47+
# CHECK-NEXT: - Index: 0
48+
# CHECK-NEXT: Kind: FUNCTION
49+
# CHECK-NEXT: Name: _start
50+
# CHECK-NEXT: Flags: [ ]
51+
# CHECK-NEXT: Function: 0
52+
# CHECK-NEXT: - Index: 1
53+
# CHECK-NEXT: Kind: FUNCTION
54+
# CHECK-NEXT: Name: ret32
55+
# CHECK-NEXT: Flags: [ VISIBILITY_HIDDEN ]
56+
# CHECK-NEXT: Function: 1

lld/wasm/Writer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,10 @@ void Writer::populateTargetFeatures() {
676676
// memory is not being imported then we can assume its zero initialized.
677677
// In the case the memory is imported, and we can use the memory.fill
678678
// instruction, then we can also avoid including the segments.
679-
if (config->memoryImport.has_value() && !allowed.count("bulk-memory"))
679+
// Finally, if we are emitting relocations, they may refer to locations within
680+
// the bss segments, so these segments need to exist in the binary.
681+
if (config->emitRelocs ||
682+
(config->memoryImport.has_value() && !allowed.count("bulk-memory")))
680683
config->emitBssSegments = true;
681684

682685
if (allowed.count("extended-const"))

0 commit comments

Comments
 (0)