[llvm] SerializableObjectFile: Distinguish between object and linked files for Wasm (PR #191329)

Derek Schuff via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 17:09:48 PDT 2026


https://github.com/dschuff created https://github.com/llvm/llvm-project/pull/191329

A fix after #191068: The offset adjustment should not be applied for object
files, because section-relative addresses are always used (and all the
SectionIndex values are 0). And, for linked files, invalidate any address that
is outside the text section to prevent it from being matched in DWARF as a
section-relative address.
Add test cases that cover the distinction (e.g. address 3 should match in an
object file but not in a linked file).
Also, fix the comments in the test to match the updated line numbers.


>From f18af3c82f09a6b881e1e9be328588ec7c0b185f Mon Sep 17 00:00:00 2001
From: Derek Schuff <dschuff at chromium.org>
Date: Thu, 9 Apr 2026 23:31:41 +0000
Subject: [PATCH] SerializableObjectFile: Distinguish between object and linked
 files for Wasm

A fix after #191068: The offset adjustment should not be applied for object
files, because section-relative addresses are always used (and all the
SectionIndex values are 0). And, for linked files, invalidate any address that
is outside the text section to prevent it from being matched in DWARF as a
section-relative address.
Add test cases that cover the distinction (e.g. address 3 should match in an
object file but not in a linked file).
Also, fix the comments in the test to match the updated line numbers.
---
 .../Symbolize/SymbolizableObjectFile.cpp      | 15 ++++++++++-
 llvm/test/tools/llvm-symbolizer/wasm-basic.s  | 26 ++++++++++++-------
 2 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
index 9212e7f875e91..f1f3ca760690f 100644
--- a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
+++ b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
@@ -273,13 +273,26 @@ object::SectionedAddress SymbolizableObjectFile::convertDwarfOffsetForWasm(
     object::SectionedAddress ModuleOffset) const {
   if (!Module->isWasm())
     return ModuleOffset;
+
+  // For Wasm object files, addresses are already section-relative.
+  if (Module->isRelocatableObject())
+    return ModuleOffset;
+
+  // We are dealing with a linked Wasm file, which uses file offsets.
   for (const SectionRef &Sec : Module->sections()) {
     if (Sec.getIndex() == ModuleOffset.SectionIndex) {
-      if (Sec.isText())
+      if (Sec.isText()) {
         ModuleOffset.Address -= Sec.getAddress();
+        return ModuleOffset;
+      }
       break;
     }
   }
+
+  // If the address is not in a text section, or we couldn't find a
+  // matching section, invalidate it. This prevents it from incorrectly
+  // being interpreted as as section-relative DWARF offset.
+  ModuleOffset.Address = object::SectionedAddress::UndefSection;
   return ModuleOffset;
 }
 
diff --git a/llvm/test/tools/llvm-symbolizer/wasm-basic.s b/llvm/test/tools/llvm-symbolizer/wasm-basic.s
index f6f64cfd9889e..7f7885b97cadb 100644
--- a/llvm/test/tools/llvm-symbolizer/wasm-basic.s
+++ b/llvm/test/tools/llvm-symbolizer/wasm-basic.s
@@ -1,11 +1,11 @@
 # REQUIRES: webassembly-registered-target, lld
 # RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj %s -o %t.o -g
-# RUN: llvm-symbolizer --basenames --output-style=GNU -e %t.o 1 2 3 4 5 6 7 8 9 10 11 12 13 | FileCheck %s
+# RUN: llvm-symbolizer --basenames --output-style=GNU -e %t.o 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0x44 | FileCheck %s
 
 # This is the same test but on a linked wasm file, using file offsets rather
 # than section offsets.
 # RUN: wasm-ld %t.o -o %t.wasm --no-entry --export=foo --export=bar
-# RUN: llvm-symbolizer --basenames --output-style=GNU -e %t.wasm 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e | FileCheck %s
+# RUN: llvm-symbolizer --basenames --output-style=GNU -e %t.wasm 3 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x100 | FileCheck %s
 
 .globl foo
 foo:
@@ -27,30 +27,34 @@ bar:
 ## way to the next symbol start.
 ## TODO: create a loc for .functype? It could go with the local declarations.
 
+## Byte 0 (or 3, in the case of a linked file) never maps to a code section
+# CHECK: ??
+# CHECK-NEXT: ??:0
+
 ## Byte 1 is the function length, has no loc but the symbol table considers it
 ## the start of the function
 # CHECK: foo
 # CHECK-NEXT: ??:0
-## Byte 2 is the local declaration, but for some reason DWARF is marking it as line 7.
+## Byte 2 is the local declaration, but for some reason DWARF is marking it as line 13.
 ## TODO: figure out why.
 # CHECK-NEXT: foo
 # CHECK-NEXT: wasm-basic.s:13
-## Byte 3 is actually the nop, line 7
+## Byte 3 is actually the nop, line 13
 # CHECK-NEXT: foo
 # CHECK-NEXT: wasm-basic.s:13
-## Byte 4 is the return, line 8
+## Byte 4 is the return, line 14
 # CHECK-NEXT: foo
 # CHECK-NEXT: wasm-basic.s:14
-## Byte 5 is the end_function, line 9
+## Byte 5 is the end_function, line 15
 # CHECK-NEXT: foo
 # CHECK-NEXT: wasm-basic.s:15
 ## Byte 6 is bar's function length, symbol table considers it part of bar
 # CHECK-NEXT: bar
 # CHECK-NEXT: ??:0
-## Byte 7 bar's local declaration, but DWARF marks it as line 13, like above
+## Byte 7 bar's local declaration, but DWARF marks it as line 20, like above
 # CHECK-NEXT: bar
 # CHECK-NEXT: wasm-basic.s:20
-## Byte 8 and 9 are actually the local.get on line 13
+## Byte 8 and 9 are the local.get on line 20
 # CHECK-NEXT: bar
 # CHECK-NEXT: wasm-basic.s:20
 # CHECK-NEXT: bar
@@ -64,6 +68,10 @@ bar:
 ## Byte c is end_function
 # CHECK-NEXT: bar
 # CHECK-NEXT: wasm-basic.s:23
-## Byte d is past the function
+## Byte d is past the end of the function
 # CHECK-NEXT: ??
 # CHECK-NEXT: ??:0
+
+## Past the end of the section.
+# CHECK: ??
+# CHECK-NEXT: ??:0



More information about the llvm-commits mailing list