From da6a896e6b1b1e397297b08a565940f1e0391cb7 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Wed, 22 Jan 2020 17:40:11 -0800 Subject: [PATCH] [WebAssembly] Add WebAssembly support to llvm-symbolizer The only thing missing for basic llvm-symbolizer support is the ability on lib/Object to get a wasm symbol's section ID, which allows sorting and computation of the symbols' sizes. Also, when the WasmAsmParser switches sections on new functions, also add the section to the list of Dwarf sections if Dwarf is being generated for assembly; this allows writing of simple tests. Reviewers: sbc100, jhenderson, aardappel Differential Revision: https://reviews.llvm.org/D73246 --- llvm/include/llvm/Object/Wasm.h | 2 ++ llvm/lib/Object/SymbolSize.cpp | 6 +++++ llvm/lib/Object/WasmObjectFile.cpp | 26 +++++++++++++--------- .../WebAssembly/AsmParser/WebAssemblyAsmParser.cpp | 3 +++ llvm/test/tools/llvm-symbolizer/wasm-basic.s | 23 +++++++++++++++++++ 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 llvm/test/tools/llvm-symbolizer/wasm-basic.s diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h index 933d732..b3cbb57 100644 --- a/llvm/include/llvm/Object/Wasm.h +++ b/llvm/include/llvm/Object/Wasm.h @@ -169,6 +169,7 @@ public: uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; Expected getSymbolType(DataRefImpl Symb) const override; Expected getSymbolSection(DataRefImpl Symb) const override; + uint32_t getSymbolSectionId(SymbolRef Sym) const; // Overrides from SectionRef. void moveSectionNext(DataRefImpl &Sec) const override; @@ -230,6 +231,7 @@ private: const WasmSection &getWasmSection(DataRefImpl Ref) const; const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const; + uint32_t getSymbolSectionIdImpl(const WasmSymbol &Symb) const; Error parseSection(WasmSection &Sec); Error parseCustomSection(WasmSection &Sec, ReadContext &Ctx); diff --git a/llvm/lib/Object/SymbolSize.cpp b/llvm/lib/Object/SymbolSize.cpp index bdf4dc5..1378899 100644 --- a/llvm/lib/Object/SymbolSize.cpp +++ b/llvm/lib/Object/SymbolSize.cpp @@ -11,6 +11,7 @@ #include "llvm/Object/COFF.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/MachO.h" +#include "llvm/Object/Wasm.h" using namespace llvm; using namespace object; @@ -27,12 +28,17 @@ int llvm::object::compareAddress(const SymEntry *A, const SymEntry *B) { static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) { if (auto *M = dyn_cast(&O)) return M->getSectionID(Sec); + if (const auto *M = dyn_cast(&O)) + return Sec.getIndex(); + return cast(O).getSectionID(Sec); } static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) { if (auto *M = dyn_cast(&O)) return M->getSymbolSectionID(Sym); + if (const auto *M = dyn_cast(&O)) + return M->getSymbolSectionId(Sym); return cast(O).getSymbolSectionID(Sym); } diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index ab8918c..813ced1 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1365,26 +1365,30 @@ WasmObjectFile::getSymbolSection(DataRefImpl Symb) const { return section_end(); DataRefImpl Ref; + Ref.d.a = getSymbolSectionIdImpl(Sym); + return section_iterator(SectionRef(Ref, this)); +} + +uint32_t WasmObjectFile::getSymbolSectionId(SymbolRef Symb) const { + const WasmSymbol &Sym = getWasmSymbol(Symb); + return getSymbolSectionIdImpl(Sym); +} + +uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const { switch (Sym.Info.Kind) { case wasm::WASM_SYMBOL_TYPE_FUNCTION: - Ref.d.a = CodeSection; - break; + return CodeSection; case wasm::WASM_SYMBOL_TYPE_GLOBAL: - Ref.d.a = GlobalSection; - break; + return GlobalSection; case wasm::WASM_SYMBOL_TYPE_DATA: - Ref.d.a = DataSection; - break; + return DataSection; case wasm::WASM_SYMBOL_TYPE_SECTION: - Ref.d.a = Sym.Info.ElementIndex; - break; + return Sym.Info.ElementIndex; case wasm::WASM_SYMBOL_TYPE_EVENT: - Ref.d.a = EventSection; - break; + return EventSection; default: llvm_unreachable("Unknown WasmSymbol::SymbolType"); } - return section_iterator(SectionRef(Ref, this)); } void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } diff --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp index ea99cee..a46bf06 100644 --- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp +++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp @@ -879,6 +879,9 @@ public: auto SecName = ".text." + SymName; auto WS = getContext().getWasmSection(SecName, SectionKind::getText()); getStreamer().SwitchSection(WS); + // Also generate DWARF for this section if requested. + if (getContext().getGenDwarfForAssembly()) + getContext().addGenDwarfSection(WS); } void onEndOfFunction() { diff --git a/llvm/test/tools/llvm-symbolizer/wasm-basic.s b/llvm/test/tools/llvm-symbolizer/wasm-basic.s new file mode 100644 index 0000000..b17bfb8 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/wasm-basic.s @@ -0,0 +1,23 @@ +# REQUIRES: webassembly-registered-target +# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj %s -o %t.o -g + +foo: + .functype foo () -> () + nop + end_function + +bar: + .functype bar (i32) -> (i32) + return + end_function + +# RUN: llvm-symbolizer -e %t.o 3 4 7 8 | FileCheck %s +## Byte 1 is the function length and 2 is the locals declaration. +## Currently no line corresponds to them. +## TODO: create a loc for .functype? + +## Test 2 functions to ensure wasm's function-sections system works. +# CHECK: wasm-basic.s:6:0 +# CHECK: wasm-basic.s:7:0 +# CHECK: wasm-basic.s:11:0 +# CHECK: wasm-basic.s:12:0 -- 2.7.4