From 73eaa48d602a27c88b6ce6e21073b1ccbd6b8c12 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Fri, 9 Mar 2018 16:30:44 +0000 Subject: [PATCH] [WebAssembly] Disallow weak undefined globals in the object format This implements https://github.com/WebAssembly/tool-conventions/pull/47 Differential Revision: https://reviews.llvm.org/D44201 llvm-svn=327146 --- llvm/lib/MC/WasmObjectWriter.cpp | 3 +++ llvm/lib/Object/WasmObjectFile.cpp | 5 ++++ .../ObjectYAML/wasm/invalid_global_weak.yaml | 24 +++++++++++++++++++ .../test/tools/llvm-nm/wasm/weak-symbols.yaml | 17 +++---------- 4 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 llvm/test/ObjectYAML/wasm/invalid_global_weak.yaml diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp index 87ef0d927ee..a05948e2946 100644 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -1024,6 +1024,9 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm, Imports.push_back(Import); WasmIndices[&WS] = NumFunctionImports++; } else if (WS.isGlobal()) { + if (WS.isWeak()) + report_fatal_error("undefined global symbol cannot be weak"); + wasm::WasmImport Import; Import.Module = WS.getModuleName(); Import.Field = WS.getName(); diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 3b18e194d02..65ae0639d4f 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -422,6 +422,11 @@ Error WasmObjectFile::parseLinkingSectionSymtab(const uint8_t *&Ptr, IsDefined != isDefinedGlobalIndex(Info.ElementIndex)) return make_error("invalid global symbol index", object_error::parse_failed); + if (!IsDefined && + (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == + wasm::WASM_SYMBOL_BINDING_WEAK) + return make_error("undefined weak global symbol", + object_error::parse_failed); if (IsDefined) { Info.Name = readString(Ptr); unsigned GlobalIndex = Info.ElementIndex - NumImportedGlobals; diff --git a/llvm/test/ObjectYAML/wasm/invalid_global_weak.yaml b/llvm/test/ObjectYAML/wasm/invalid_global_weak.yaml new file mode 100644 index 00000000000..557882dcaab --- /dev/null +++ b/llvm/test/ObjectYAML/wasm/invalid_global_weak.yaml @@ -0,0 +1,24 @@ +# RUN: yaml2obj < %s | not obj2yaml 2>&1 | FileCheck %s + +--- !WASM +FileHeader: + Version: 0x00000001 +Sections: + - Type: IMPORT + Imports: + - Module: fiz + Field: imported_global + Kind: GLOBAL + GlobalType: I32 + GlobalMutable: false + - Type: CUSTOM + Name: linking + SymbolTable: + - Index: 0 + Kind: GLOBAL + Name: imported_global + Flags: [ BINDING_WEAK, UNDEFINED ] + Global: 0 +... + +# CHECK: Error reading file: : undefined weak global symbol diff --git a/llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml b/llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml index 1d0b0d1f6b5..e758194e2ff 100644 --- a/llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml +++ b/llvm/test/tools/llvm-nm/wasm/weak-symbols.yaml @@ -18,16 +18,11 @@ Sections: Field: weak_import_func Kind: FUNCTION SigIndex: 0 - - Module: env - Field: weak_import_global - Kind: GLOBAL - GlobalType: I32 - GlobalMutable: false - Type: FUNCTION FunctionTypes: [ 0 ] - Type: GLOBAL Globals: - - Index: 1 + - Index: 0 Type: I32 Mutable: false InitExpr: @@ -64,7 +59,7 @@ Sections: Kind: GLOBAL Name: weak_defined_global Flags: [ BINDING_WEAK ] - Global: 1 + Global: 0 - Index: 3 Kind: DATA Name: weak_import_data @@ -74,11 +69,6 @@ Sections: Name: weak_import_func Flags: [ BINDING_WEAK, UNDEFINED ] Function: 0 - - Index: 5 - Kind: GLOBAL - Name: weak_import_global - Flags: [ BINDING_WEAK, UNDEFINED ] - Global: 0 SegmentInfo: - Index: 0 Name: .rodata.constantData @@ -88,7 +78,6 @@ Sections: # CHECK: 00000000 W weak_defined_data # CHECK-NEXT: 00000001 W weak_defined_func -# CHECK-NEXT: 00000001 W weak_defined_global +# CHECK-NEXT: 00000000 W weak_defined_global # CHECK-NEXT: w weak_import_data # CHECK-NEXT: w weak_import_func -# CHECK-NEXT: w weak_import_global