diff options
author | Sam Clegg <sbc@chromium.org> | 2017-09-15 19:22:01 +0000 |
---|---|---|
committer | Sam Clegg <sbc@chromium.org> | 2017-09-15 19:22:01 +0000 |
commit | aff1c4df2573db4c21143c990db268d85350fd54 (patch) | |
tree | 06aeda201073b355a77db9f8e185af590e0b1355 /llvm/lib/MC/WasmObjectWriter.cpp | |
parent | 89291f2ac80de08ed053178f2519ded4e5b257c3 (diff) | |
download | bcm5719-llvm-aff1c4df2573db4c21143c990db268d85350fd54.tar.gz bcm5719-llvm-aff1c4df2573db4c21143c990db268d85350fd54.zip |
[WebAssembly] MC: Fix crash in getProvitionalValue on weak references
- Create helper function for resolving weak references.
- Add test that preproduces the crash.
Differential Revision: https://reviews.llvm.org/D37916
llvm-svn: 313381
Diffstat (limited to 'llvm/lib/MC/WasmObjectWriter.cpp')
-rw-r--r-- | llvm/lib/MC/WasmObjectWriter.cpp | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp index 4093b550cc8..1ae292e5135 100644 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -477,13 +477,22 @@ static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) { Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset); } +static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) { + if (Symbol.isVariable()) { + const MCExpr *Expr = Symbol.getVariableValue(); + auto *Inner = cast<MCSymbolRefExpr>(Expr); + return cast<MCSymbolWasm>(&Inner->getSymbol()); + } + return &Symbol; +} + // Compute a value to write into the code at the location covered // by RelEntry. This value isn't used by the static linker, since // we have addends; it just serves to make the code more readable // and to make standalone wasm modules directly usable. uint32_t WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) { - const MCSymbolWasm *Sym = RelEntry.Symbol; + const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol); // For undefined symbols, use a hopefully invalid value. if (!Sym->isDefined(/*SetUsed=*/false)) @@ -934,16 +943,9 @@ uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) { assert(Symbol.isFunction()); WasmFunctionType F; - if (Symbol.isVariable()) { - const MCExpr *Expr = Symbol.getVariableValue(); - auto *Inner = cast<MCSymbolRefExpr>(Expr); - const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol()); - F.Returns = ResolvedSym->getReturns(); - F.Params = ResolvedSym->getParams(); - } else { - F.Returns = Symbol.getReturns(); - F.Params = Symbol.getParams(); - } + const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol); + F.Returns = ResolvedSym->getReturns(); + F.Params = ResolvedSym->getParams(); auto Pair = FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size())); @@ -1255,11 +1257,9 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm, continue; assert(S.isDefined(/*SetUsed=*/false)); - const auto &WS = static_cast<const MCSymbolWasm &>(S); // Find the target symbol of this weak alias and export that index - const MCExpr *Expr = WS.getVariableValue(); - auto *Inner = cast<MCSymbolRefExpr>(Expr); - const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol()); + const auto &WS = static_cast<const MCSymbolWasm &>(S); + const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS); DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n"); assert(SymbolIndices.count(ResolvedSym) > 0); uint32_t Index = SymbolIndices.find(ResolvedSym)->second; |