Skip to content

Commit 73eaa48

Browse files
committed
[WebAssembly] Disallow weak undefined globals in the object format
This implements WebAssembly/tool-conventions#47 Differential Revision: https://reviews.llvm.org/D44201 llvm-svn=327146
1 parent 7411810 commit 73eaa48

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

llvm/lib/MC/WasmObjectWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,9 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
10241024
Imports.push_back(Import);
10251025
WasmIndices[&WS] = NumFunctionImports++;
10261026
} else if (WS.isGlobal()) {
1027+
if (WS.isWeak())
1028+
report_fatal_error("undefined global symbol cannot be weak");
1029+
10271030
wasm::WasmImport Import;
10281031
Import.Module = WS.getModuleName();
10291032
Import.Field = WS.getName();

llvm/lib/Object/WasmObjectFile.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ Error WasmObjectFile::parseLinkingSectionSymtab(const uint8_t *&Ptr,
422422
IsDefined != isDefinedGlobalIndex(Info.ElementIndex))
423423
return make_error<GenericBinaryError>("invalid global symbol index",
424424
object_error::parse_failed);
425+
if (!IsDefined &&
426+
(Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
427+
wasm::WASM_SYMBOL_BINDING_WEAK)
428+
return make_error<GenericBinaryError>("undefined weak global symbol",
429+
object_error::parse_failed);
425430
if (IsDefined) {
426431
Info.Name = readString(Ptr);
427432
unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# RUN: yaml2obj < %s | not obj2yaml 2>&1 | FileCheck %s
2+
3+
--- !WASM
4+
FileHeader:
5+
Version: 0x00000001
6+
Sections:
7+
- Type: IMPORT
8+
Imports:
9+
- Module: fiz
10+
Field: imported_global
11+
Kind: GLOBAL
12+
GlobalType: I32
13+
GlobalMutable: false
14+
- Type: CUSTOM
15+
Name: linking
16+
SymbolTable:
17+
- Index: 0
18+
Kind: GLOBAL
19+
Name: imported_global
20+
Flags: [ BINDING_WEAK, UNDEFINED ]
21+
Global: 0
22+
...
23+
24+
# CHECK: Error reading file: <stdin>: undefined weak global symbol

llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@ Sections:
1818
Field: weak_import_func
1919
Kind: FUNCTION
2020
SigIndex: 0
21-
- Module: env
22-
Field: weak_import_global
23-
Kind: GLOBAL
24-
GlobalType: I32
25-
GlobalMutable: false
2621
- Type: FUNCTION
2722
FunctionTypes: [ 0 ]
2823
- Type: GLOBAL
2924
Globals:
30-
- Index: 1
25+
- Index: 0
3126
Type: I32
3227
Mutable: false
3328
InitExpr:
@@ -64,7 +59,7 @@ Sections:
6459
Kind: GLOBAL
6560
Name: weak_defined_global
6661
Flags: [ BINDING_WEAK ]
67-
Global: 1
62+
Global: 0
6863
- Index: 3
6964
Kind: DATA
7065
Name: weak_import_data
@@ -74,11 +69,6 @@ Sections:
7469
Name: weak_import_func
7570
Flags: [ BINDING_WEAK, UNDEFINED ]
7671
Function: 0
77-
- Index: 5
78-
Kind: GLOBAL
79-
Name: weak_import_global
80-
Flags: [ BINDING_WEAK, UNDEFINED ]
81-
Global: 0
8272
SegmentInfo:
8373
- Index: 0
8474
Name: .rodata.constantData
@@ -88,7 +78,6 @@ Sections:
8878

8979
# CHECK: 00000000 W weak_defined_data
9080
# CHECK-NEXT: 00000001 W weak_defined_func
91-
# CHECK-NEXT: 00000001 W weak_defined_global
81+
# CHECK-NEXT: 00000000 W weak_defined_global
9282
# CHECK-NEXT: w weak_import_data
9383
# CHECK-NEXT: w weak_import_func
94-
# CHECK-NEXT: w weak_import_global

0 commit comments

Comments
 (0)