From fc5f51cf5af4364b38bf22e491d46e1e892ade0c Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Tue, 2 Jan 2024 14:54:54 -0800 Subject: [WebAssembly][Object]Use file offset as function symbol address for linked files (#76198) WebAssembly doesn't have a single virtual memory space the way other object formats or architectures do, so "addresses" mean different things depending on the context. Function symbol addresses in object files are offsets from the start of the code section. This is good for linking and relocation. However when dealing with linked binaries, offsets from the start of the file/module are more often used (e.g. for stack traces in browsers), and are more useful for use cases like binary size attribution. This PR changes Object to use the file offset instead of the section offset for function symbols, but only for linked (non-DSO) files. This implements item number 4 from #76107 --- llvm/lib/Object/WasmObjectFile.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'llvm/lib/Object/WasmObjectFile.cpp') diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 40665d6..ccc29d0 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1667,10 +1667,18 @@ Expected WasmObjectFile::getSymbolName(DataRefImpl Symb) const { Expected WasmObjectFile::getSymbolAddress(DataRefImpl Symb) const { auto &Sym = getWasmSymbol(Symb); if (Sym.Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION && - isDefinedFunctionIndex(Sym.Info.ElementIndex)) - return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset; - else - return getSymbolValue(Symb); + isDefinedFunctionIndex(Sym.Info.ElementIndex)) { + // For object files, use the section offset. The linker relies on this. + // For linked files, use the file offset. This behavior matches the way + // browsers print stack traces and is useful for binary size analysis. + // (see https://webassembly.github.io/spec/web-api/index.html#conventions) + uint32_t Adjustment = isRelocatableObject() || isSharedObject() + ? 0 + : Sections[CodeSection].Offset; + return getDefinedFunction(Sym.Info.ElementIndex).CodeSectionOffset + + Adjustment; + } + return getSymbolValue(Symb); } uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const { -- cgit v1.1