From 4b8e2d8e81adf3aa1ea79fe0b908100c4ea69f65 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 23 Apr 2020 17:57:00 -0700 Subject: [PATCH] [lld][WebAssembly] Fix crash on function signature mismatch with --relocatable These stub new function were not being added to the symbol table which in turn meant that we were crashing when trying to output relocations against them. Differential Revision: https://reviews.llvm.org/D78779 --- lld/test/wasm/signature-mismatch.ll | 40 +++++++++++++++++++++++++++++++++++++ lld/wasm/SymbolTable.cpp | 6 ++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lld/test/wasm/signature-mismatch.ll b/lld/test/wasm/signature-mismatch.ll index 116d818..bb9204e 100644 --- a/lld/test/wasm/signature-mismatch.ll +++ b/lld/test/wasm/signature-mismatch.ll @@ -1,8 +1,13 @@ ; RUN: llc -filetype=obj %p/Inputs/ret32.ll -o %t.ret32.o ; RUN: llc -filetype=obj %p/Inputs/call-ret32.ll -o %t.call.o ; RUN: llc -filetype=obj %s -o %t.main.o + ; RUN: wasm-ld --export=call_ret32 --export=ret32 -o %t.wasm %t.main.o %t.ret32.o %t.call.o 2>&1 | FileCheck %s -check-prefix=WARN ; RUN: obj2yaml %t.wasm | FileCheck %s -check-prefix=YAML + +; RUN: wasm-ld -r -o %t.reloc.o %t.main.o %t.ret32.o %t.call.o 2>&1 | FileCheck %s -check-prefix=WARN +; RUN: obj2yaml %t.reloc.o | FileCheck %s -check-prefix=RELOC + ; RUN: not wasm-ld --fatal-warnings -o %t.wasm %t.main.o %t.ret32.o %t.call.o 2>&1 | FileCheck %s -check-prefix=ERROR target triple = "wasm32-unknown-unknown" @@ -49,3 +54,38 @@ declare i32 @ret32(i32, i64, i32) local_unnamed_addr ; YAML-NEXT: Name: call_ret32 ; YAML-NEXT: ... +; RELOC: Name: linking +; RELOC-NEXT: Version: 2 +; RELOC-NEXT: SymbolTable: +; RELOC-NEXT: - Index: 0 +; RELOC-NEXT: Kind: FUNCTION +; RELOC-NEXT: Name: _start +; RELOC-NEXT: Flags: [ VISIBILITY_HIDDEN ] +; RELOC-NEXT: Function: 1 +; RELOC-NEXT: - Index: 1 +; RELOC-NEXT: Kind: FUNCTION +; RELOC-NEXT: Name: ret32 +; RELOC-NEXT: Flags: [ VISIBILITY_HIDDEN ] +; RELOC-NEXT: Function: 2 +; RELOC-NEXT: - Index: 2 +; RELOC-NEXT: Kind: DATA +; RELOC-NEXT: Name: ret32_address_main +; RELOC-NEXT: Flags: [ ] +; RELOC-NEXT: Segment: 0 +; RELOC-NEXT: Size: 4 +; RELOC-NEXT: - Index: 3 +; RELOC-NEXT: Kind: FUNCTION +; RELOC-NEXT: Name: call_ret32 +; RELOC-NEXT: Flags: [ VISIBILITY_HIDDEN ] +; RELOC-NEXT: Function: 3 +; RELOC-NEXT: - Index: 4 +; RELOC-NEXT: Kind: DATA +; RELOC-NEXT: Name: ret32_address +; RELOC-NEXT: Flags: [ ] +; RELOC-NEXT: Segment: 1 +; RELOC-NEXT: Size: 4 +; RELOC-NEXT: - Index: 5 +; RELOC-NEXT: Kind: FUNCTION +; RELOC-NEXT: Name: 'signature_mismatch:ret32' +; RELOC-NEXT: Flags: [ BINDING_LOCAL ] +; RELOC-NEXT: Function: 0 diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp index 99d1817..6f6706e 100644 --- a/lld/wasm/SymbolTable.cpp +++ b/lld/wasm/SymbolTable.cpp @@ -642,8 +642,10 @@ InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym, auto *func = make(sig, sym->getName(), debugName); func->setBody(unreachableFn); syntheticFunctions.emplace_back(func); - replaceSymbol(sym, sym->getName(), sym->flags, nullptr, - func); + // Mark new symbols as local. For relocatable output we don't want them + // to be exported outside the object file. + replaceSymbol(sym, debugName, WASM_SYMBOL_BINDING_LOCAL, + nullptr, func); return func; } -- 2.7.4