diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h --- a/llvm/include/llvm/Object/Wasm.h +++ b/llvm/include/llvm/Object/Wasm.h @@ -168,6 +168,7 @@ 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; @@ -229,6 +230,7 @@ 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 --- 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 @@ 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 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1365,26 +1365,30 @@ 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 --- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp +++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp @@ -879,6 +879,9 @@ 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 --- /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:7:0 +# CHECK: wasm-basic.s:8:0 +# CHECK: wasm-basic.s:12:0 +# CHECK: wasm-basic.s:13:0