Skip to content

Commit 9247be8

Browse files
authored
[lld][WebAssembly] Do not relocate ABSOLUTE symbols (#153763)
Fixes #153759
1 parent 25c137e commit 9247be8

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lld/test/wasm/pie.s

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-emscripten %S/Inputs/internal_func.s -o %t.internal_func.o
33
# RUN: wasm-ld --no-gc-sections --experimental-pic -pie --unresolved-symbols=import-dynamic -o %t.wasm %t.o %t.internal_func.o
44
# RUN: obj2yaml %t.wasm | FileCheck %s
5-
# RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors,__wasm_apply_data_relocs --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DISASSEM
5+
# RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors,__wasm_apply_data_relocs,__wasm_apply_global_relocs --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DISASSEM
66

77
.functype external_func () -> ()
88
.functype internal_func1 () -> (i32)
@@ -41,6 +41,9 @@ foo:
4141
drop
4242

4343
global.get __stack_pointer
44+
drop
45+
46+
global.get __wasm_first_page_end@GOT
4447
end_function
4548

4649
get_data_address:
@@ -145,6 +148,18 @@ _start:
145148
# DISASSEM-LABEL: <__wasm_apply_data_relocs>:
146149
# DISASSEM: end
147150

151+
# global 6 is __wasm_first_page_end, which is ABSOLUTE and
152+
# thus should not be relocated.
153+
#
154+
# DISASSEM-LABEL: <__wasm_apply_global_relocs>:
155+
# DISASSEM: global.set 4
156+
# DISASSEM: global.set 5
157+
# DISASSEM-NOT: global.set 6
158+
# DISASSEM: global.set 7
159+
# DISASSEM: global.set 8
160+
# DISASSEM: global.set 9
161+
# DISASSEM: end
162+
148163
# Run the same test with extended-const support. When this is available
149164
# we don't need __wasm_apply_global_relocs and instead rely on the add
150165
# instruction in the InitExpr. We also, therefore, do not need these globals
@@ -173,17 +188,23 @@ _start:
173188
# EXTENDED-CONST-NEXT: Type: I32
174189
# EXTENDED-CONST-NEXT: Mutable: false
175190
# EXTENDED-CONST-NEXT: InitExpr:
191+
# EXTENDED-CONST-NEXT: Opcode: I32_CONST
192+
# EXTENDED-CONST-NEXT: Value: 65536
193+
# EXTENDED-CONST-NEXT: - Index: 7
194+
# EXTENDED-CONST-NEXT: Type: I32
195+
# EXTENDED-CONST-NEXT: Mutable: false
196+
# EXTENDED-CONST-NEXT: InitExpr:
176197
# EXTENDED-CONST-NEXT: Extended: true
177198
# This instruction sequence decodes to:
178199
# (global.get[0x23] 0x1 i32.const[0x41] 0x0C i32.add[0x6A] end[0x0b])
179200
# EXTENDED-CONST-NEXT: Body: 2301410C6A0B
180-
# EXTENDED-CONST-NEXT: - Index: 7
201+
# EXTENDED-CONST-NEXT: - Index: 8
181202
# EXTENDED-CONST-NEXT: Type: I32
182203
# EXTENDED-CONST-NEXT: Mutable: false
183204
# EXTENDED-CONST-NEXT: InitExpr:
184205
# EXTENDED-CONST-NEXT: Opcode: GLOBAL_GET
185206
# EXTENDED-CONST-NEXT: Index: 2
186-
# EXTENDED-CONST-NEXT: - Index: 8
207+
# EXTENDED-CONST-NEXT: - Index: 9
187208
# EXTENDED-CONST-NEXT: Type: I32
188209
# EXTENDED-CONST-NEXT: Mutable: false
189210
# EXTENDED-CONST-NEXT: InitExpr:

lld/wasm/SyntheticSections.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
440440
: WASM_OPCODE_I32_ADD;
441441

442442
for (const Symbol *sym : internalGotSymbols) {
443+
if (sym->flags & WASM_SYMBOL_ABSOLUTE)
444+
continue;
443445
if (TLS != sym->isTLS())
444446
continue;
445447

@@ -503,7 +505,8 @@ void GlobalSection::writeBody() {
503505
bool useExtendedConst = false;
504506
uint32_t globalIdx;
505507
int64_t offset;
506-
if (ctx.arg.extendedConst && ctx.isPic) {
508+
if (ctx.arg.extendedConst && ctx.isPic &&
509+
(sym->flags & WASM_SYMBOL_ABSOLUTE) == 0) {
507510
if (auto *d = dyn_cast<DefinedData>(sym)) {
508511
if (!sym->isTLS()) {
509512
globalIdx = ctx.sym.memoryBase->getGlobalIndex();

0 commit comments

Comments
 (0)