Skip to content

[WebAssembly][Object]Use file offset as function symbol address for linked files #76198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions llvm/lib/Object/WasmObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1662,10 +1662,18 @@ Expected<StringRef> WasmObjectFile::getSymbolName(DataRefImpl Symb) const {
Expected<uint64_t> 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 {
Expand Down
74 changes: 74 additions & 0 deletions llvm/test/tools/llvm-nm/wasm/linked.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# RUN: yaml2obj %s -o %t.wasm
# RUN: llvm-nm %t.wasm | FileCheck %s

# CHECK: 0000009f T my_func_export
# CHECK-NEXT: 0000002a D my_global_export
# CHECK-NEXT: 00000000 D my_table_export

--- !WASM
FileHeader:
Version: 0x1
Sections:
- Type: TYPE
Signatures:
- Index: 0
ParamTypes: []
ReturnTypes: []
- Type: IMPORT
Imports:
- Module: env
Field: foo
Kind: FUNCTION
SigIndex: 0
- Module: env
Field: bar
Kind: GLOBAL
GlobalType: I32
GlobalMutable: true
- Module: env
Field: memory
Kind: MEMORY
Memory:
Minimum: 0x1
- Type: FUNCTION
FunctionTypes: [ 0 ]
- Type: TABLE
Tables:
- Index: 0
ElemType: FUNCREF
Limits:
Flags: [ HAS_MAX ]
Minimum: 0x1
Maximum: 0x1
- Type: GLOBAL
Globals:
- Index: 1
Mutable: false
Type: I32
InitExpr:
Opcode: I32_CONST
Value: 42
- Type: EXPORT
Exports:
- Name: my_func_export
Kind: FUNCTION
Index: 1
- Name: my_global_export
Kind: GLOBAL
Index: 1
- Name: my_table_export
Kind: TABLE
Index: 0
- Type: CODE
Functions:
- Index: 1
Locals:
Body: 00
- Type: DATA
Segments:
- SectionOffset: 0
InitFlags: 0
Offset:
Opcode: I32_CONST
Value: 0
Content: ''
75 changes: 75 additions & 0 deletions llvm/test/tools/llvm-objdump/wasm/linked-symbol-table.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# RUN: yaml2obj %s -o %t.wasm
# RUN: llvm-objdump -t %t.wasm | FileCheck %s
#
# CHECK: SYMBOL TABLE:
# CHECK-NEXT: 0000009f g F CODE my_func_export
# CHECK-NEXT: 0000002a g O DATA my_global_export
# CHECK-NEXT: 00000000 g TABLE my_table_export

--- !WASM
FileHeader:
Version: 0x1
Sections:
- Type: TYPE
Signatures:
- Index: 0
ParamTypes: []
ReturnTypes: []
- Type: IMPORT
Imports:
- Module: env
Field: foo
Kind: FUNCTION
SigIndex: 0
- Module: env
Field: bar
Kind: GLOBAL
GlobalType: I32
GlobalMutable: true
- Module: env
Field: memory
Kind: MEMORY
Memory:
Minimum: 0x1
- Type: FUNCTION
FunctionTypes: [ 0 ]
- Type: TABLE
Tables:
- Index: 0
ElemType: FUNCREF
Limits:
Flags: [ HAS_MAX ]
Minimum: 0x1
Maximum: 0x1
- Type: GLOBAL
Globals:
- Index: 1
Mutable: false
Type: I32
InitExpr:
Opcode: I32_CONST
Value: 42
- Type: EXPORT
Exports:
- Name: my_func_export
Kind: FUNCTION
Index: 1
- Name: my_global_export
Kind: GLOBAL
Index: 1
- Name: my_table_export
Kind: TABLE
Index: 0
- Type: CODE
Functions:
- Index: 1
Locals:
Body: 00
- Type: DATA
Segments:
- SectionOffset: 0
InitFlags: 0
Offset:
Opcode: I32_CONST
Value: 0
Content: ''