[llvm] [llvm-nm][WebAssembly] Print function symbol sizes (PR #81315)

Derek Schuff via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 9 13:24:09 PST 2024


https://github.com/dschuff updated https://github.com/llvm/llvm-project/pull/81315

>From 9aa841ab8ddb8f5808f8ce2924fdf925f43ed266 Mon Sep 17 00:00:00 2001
From: Derek Schuff <dschuff at chromium.org>
Date: Fri, 9 Feb 2024 12:38:29 -0800
Subject: [PATCH 1/3] [llvm-nm][WebAssembly] Print function symbol sizes

nm already prints sizes for data symbols. Do that for function symbols too.
---
 llvm/test/tools/llvm-nm/wasm/linked.yaml     | 5 +++++
 llvm/test/tools/llvm-nm/wasm/print-size.test | 2 +-
 llvm/tools/llvm-nm/llvm-nm.cpp               | 5 +++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/llvm/test/tools/llvm-nm/wasm/linked.yaml b/llvm/test/tools/llvm-nm/wasm/linked.yaml
index 992c1811743b7a..6aee4b9fc184c4 100644
--- a/llvm/test/tools/llvm-nm/wasm/linked.yaml
+++ b/llvm/test/tools/llvm-nm/wasm/linked.yaml
@@ -1,10 +1,15 @@
 # RUN: yaml2obj %s -o %t.wasm
 # RUN: llvm-nm %t.wasm | FileCheck %s
+# RUN: llvm-nm -P %t.wasm | FileCheck %s --check-prefix=POSIX
 
 # CHECK: 0000009f T my_func_export
 # CHECK-NEXT: 0000002a D my_global_export
 # CHECK-NEXT: 00000000 D my_table_export
 
+# POSIX: my_func_export T 9f 3
+# POSIX-NEXT: my_global_export D 2a 0
+# POSIX-NEXT: my_table_export D 0 0
+
 --- !WASM
 FileHeader:
   Version:         0x1
diff --git a/llvm/test/tools/llvm-nm/wasm/print-size.test b/llvm/test/tools/llvm-nm/wasm/print-size.test
index c166edb4641c4b..610929b959b5f1 100644
--- a/llvm/test/tools/llvm-nm/wasm/print-size.test
+++ b/llvm/test/tools/llvm-nm/wasm/print-size.test
@@ -43,4 +43,4 @@ Sections:
         Size:            32
 
 # CHECK: 00000000 00000020 D a_data_symbol
-# CHECK: 00000001 00000000 T a_func
+# CHECK: 00000001 0000000d T a_func
diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index da5998b70ea3f3..51f7e417306cf6 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -1858,6 +1858,11 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
         const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym);
         if (WasmSym.isTypeData() && !WasmSym.isUndefined())
           S.Size = WasmSym.Info.DataRef.Size;
+        if (WasmSym.isTypeFunction() && !WasmSym.isUndefined())
+          S.Size = WasmObj
+                       ->functions()[WasmSym.Info.ElementIndex -
+                                     WasmObj->getNumImportedFunctions()]
+                       .Size;
       }
 
       if (PrintAddress && isa<ObjectFile>(Obj)) {

>From d57e2693fecfcaecf61a481e6305583f11bc0dc4 Mon Sep 17 00:00:00 2001
From: Derek Schuff <dschuff at chromium.org>
Date: Fri, 9 Feb 2024 12:41:28 -0800
Subject: [PATCH 2/3] use else

---
 llvm/tools/llvm-nm/llvm-nm.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index 51f7e417306cf6..1e69adc72ce310 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -1858,7 +1858,7 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
         const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym);
         if (WasmSym.isTypeData() && !WasmSym.isUndefined())
           S.Size = WasmSym.Info.DataRef.Size;
-        if (WasmSym.isTypeFunction() && !WasmSym.isUndefined())
+        else if (WasmSym.isTypeFunction() && !WasmSym.isUndefined())
           S.Size = WasmObj
                        ->functions()[WasmSym.Info.ElementIndex -
                                      WasmObj->getNumImportedFunctions()]

>From b444331a3a64520344d92ee4903c5829f1467ea5 Mon Sep 17 00:00:00 2001
From: Derek Schuff <dschuff at chromium.org>
Date: Fri, 9 Feb 2024 13:23:48 -0800
Subject: [PATCH 3/3] move getSymbolSize() to WasmObject and use it in
 llvm-objdump

---
 llvm/include/llvm/Object/Wasm.h                    |  3 +++
 llvm/lib/Object/WasmObjectFile.cpp                 | 14 ++++++++++++++
 .../wasm-linked-namesec-with-linkingsec.yaml       |  2 +-
 llvm/test/Object/wasm-linked-symbol-table.yaml     |  6 +++---
 .../llvm-objdump/wasm/dylink-symbol-table.yaml     |  4 ++--
 .../wasm/linked-symbol-table-namesec.yaml          | 12 ++++++------
 .../test/tools/llvm-objdump/wasm/symbol-table.test | 12 ++++++------
 llvm/tools/llvm-nm/llvm-nm.cpp                     | 12 ++----------
 llvm/tools/llvm-objdump/llvm-objdump.cpp           |  2 ++
 9 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h
index 13d9a17e24c3d9..cff29b2bc85530 100644
--- a/llvm/include/llvm/Object/Wasm.h
+++ b/llvm/include/llvm/Object/Wasm.h
@@ -98,6 +98,8 @@ class WasmSymbol {
     return Info.Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK;
   }
 
+  uint32_t getSize() const;
+
   void print(raw_ostream &Out) const;
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -179,6 +181,7 @@ class WasmObjectFile : public ObjectFile {
   Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
   Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
   uint32_t getSymbolSectionId(SymbolRef Sym) const;
+  uint32_t getSymbolSize(SymbolRef Sym) const;
 
   // Overrides from SectionRef.
   void moveSectionNext(DataRefImpl &Sec) const override;
diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp
index 1d686873f152bd..2c0c25875ff3f2 100644
--- a/llvm/lib/Object/WasmObjectFile.cpp
+++ b/llvm/lib/Object/WasmObjectFile.cpp
@@ -1932,6 +1932,20 @@ uint32_t WasmObjectFile::getSymbolSectionIdImpl(const WasmSymbol &Sym) const {
   }
 }
 
+uint32_t WasmObjectFile::getSymbolSize(SymbolRef Symb) const {
+  const WasmSymbol &Sym = getWasmSymbol(Symb);
+  if(!Sym.isDefined())
+    return 0;
+  if (Sym.isTypeData())
+    return Sym.Info.DataRef.Size;
+  if (Sym.isTypeFunction())
+    return functions()[Sym.Info.ElementIndex - getNumImportedFunctions()].Size;
+  // Currently symbol size is only tracked for data segments and functions. In
+  // principle we could also track size (e.g. binary size) for tables, globals
+  // and element segments etc too.
+  return 0;
+}
+
 void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; }
 
 Expected<StringRef> WasmObjectFile::getSectionName(DataRefImpl Sec) const {
diff --git a/llvm/test/Object/wasm-linked-namesec-with-linkingsec.yaml b/llvm/test/Object/wasm-linked-namesec-with-linkingsec.yaml
index c730417f0c3add..5dfa3945c8d9ee 100644
--- a/llvm/test/Object/wasm-linked-namesec-with-linkingsec.yaml
+++ b/llvm/test/Object/wasm-linked-namesec-with-linkingsec.yaml
@@ -2,7 +2,7 @@
 # RUN: llvm-nm -P %t.wasm | FileCheck %s
 #
 # Test that names from the linking section override those from the name section
-# CHECK:  foo T 1 0
+# CHECK:  foo T 1 3
 # CHECK-NOT: my_func_local_name
 
 --- !WASM
diff --git a/llvm/test/Object/wasm-linked-symbol-table.yaml b/llvm/test/Object/wasm-linked-symbol-table.yaml
index 6dd949a441496c..eccdc2c611a7dc 100644
--- a/llvm/test/Object/wasm-linked-symbol-table.yaml
+++ b/llvm/test/Object/wasm-linked-symbol-table.yaml
@@ -2,9 +2,9 @@
 # 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
+# CHECK-NEXT: 0000009f g F CODE 00000003 my_func_export
+# CHECK-NEXT: 0000002a g O DATA 00000000 my_global_export
+# CHECK-NEXT: 00000000 g   TABLE 00000000 my_table_export
 
 --- !WASM
 FileHeader:
diff --git a/llvm/test/tools/llvm-objdump/wasm/dylink-symbol-table.yaml b/llvm/test/tools/llvm-objdump/wasm/dylink-symbol-table.yaml
index 9c1e90a2d89675..f4abf1253d4b6a 100644
--- a/llvm/test/tools/llvm-objdump/wasm/dylink-symbol-table.yaml
+++ b/llvm/test/tools/llvm-objdump/wasm/dylink-symbol-table.yaml
@@ -2,8 +2,8 @@
 # RUN: llvm-objdump -t %t.so | FileCheck %s
 #
 # CHECK:      SYMBOL TABLE:
-# CHECK-NEXT: 00000001 g F CODE my_func_export
-# CHECK-NEXT: 0000002a g O DATA my_global_export
+# CHECK-NEXT: 00000001 g F CODE 00000003 my_func_export
+# CHECK-NEXT: 0000002a g O DATA 00000000 my_global_export
 
 --- !WASM
 FileHeader:
diff --git a/llvm/test/tools/llvm-objdump/wasm/linked-symbol-table-namesec.yaml b/llvm/test/tools/llvm-objdump/wasm/linked-symbol-table-namesec.yaml
index 622a6060b902a3..dc87e62bcaac37 100644
--- a/llvm/test/tools/llvm-objdump/wasm/linked-symbol-table-namesec.yaml
+++ b/llvm/test/tools/llvm-objdump/wasm/linked-symbol-table-namesec.yaml
@@ -2,12 +2,12 @@
 # RUN: llvm-objdump -t %t.wasm | FileCheck %s
 #
 # CHECK:      SYMBOL TABLE:
-# CHECK-NEXT: 00000000   F *UND* my_func_import_name
-# CHECK-NEXT: 00000083 g F CODE my_func_export_name
-# CHECK-NEXT: 00000086 l F CODE my_func_local_name
-# CHECK-NEXT: 00000000    *UND* my_global_import_name
-# CHECK-NEXT: 00000001 g  GLOBAL my_global_export_name
-# CHECK-NEXT: 00000000 l O DATA my_datasegment_name
+# CHECK-NEXT: 00000000   F *UND* 00000000 my_func_import_name
+# CHECK-NEXT: 00000083 g F CODE 00000003 my_func_export_name
+# CHECK-NEXT: 00000086 l F CODE 00000003 my_func_local_name
+# CHECK-NEXT: 00000000    *UND* 00000000 my_global_import_name
+# CHECK-NEXT: 00000001 g  GLOBAL 00000000 my_global_export_name
+# CHECK-NEXT: 00000000 l O DATA 00000004 my_datasegment_name
 
 --- !WASM
 FileHeader:
diff --git a/llvm/test/tools/llvm-objdump/wasm/symbol-table.test b/llvm/test/tools/llvm-objdump/wasm/symbol-table.test
index b7301a201fddc4..ccb07461545a74 100644
--- a/llvm/test/tools/llvm-objdump/wasm/symbol-table.test
+++ b/llvm/test/tools/llvm-objdump/wasm/symbol-table.test
@@ -1,9 +1,9 @@
 RUN: llvm-objdump -t %p/Inputs/trivial.obj.wasm | FileCheck %s
 
 CHECK:      SYMBOL TABLE:
-CHECK-NEXT: 00000001 g     F CODE	main
-CHECK-NEXT: 00000000 l     O DATA	.L.str
-CHECK-NEXT: 00000000       F *UND*	puts
-CHECK-NEXT: 00000019 l     F CODE	.LSomeOtherFunction_bitcast
-CHECK-NEXT: 00000000       F *UND*	SomeOtherFunction
-CHECK-NEXT: 00000010 g     O DATA	var
+CHECK-NEXT: 00000001 g     F CODE	00000018	main
+CHECK-NEXT: 00000000 l     O DATA	0000000d	.L.str
+CHECK-NEXT: 00000000       F *UND*	00000000	puts
+CHECK-NEXT: 00000019 l     F CODE	0000000b	.LSomeOtherFunction_bitcast
+CHECK-NEXT: 00000000       F *UND*	00000000	SomeOtherFunction
+CHECK-NEXT: 00000010 g     O DATA	00000004	var
diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index 1e69adc72ce310..e3b81451fcac91 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -1854,16 +1854,8 @@ static bool getSymbolNamesFromObject(SymbolicFile &Obj,
               dyn_cast<const XCOFFObjectFile>(&Obj))
         S.Size = XCOFFObj->getSymbolSize(Sym.getRawDataRefImpl());
 
-      if (const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(&Obj)) {
-        const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym);
-        if (WasmSym.isTypeData() && !WasmSym.isUndefined())
-          S.Size = WasmSym.Info.DataRef.Size;
-        else if (WasmSym.isTypeFunction() && !WasmSym.isUndefined())
-          S.Size = WasmObj
-                       ->functions()[WasmSym.Info.ElementIndex -
-                                     WasmObj->getNumImportedFunctions()]
-                       .Size;
-      }
+      if (const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(&Obj))
+        S.Size = WasmObj->getSymbolSize(Sym);
 
       if (PrintAddress && isa<ObjectFile>(Obj)) {
         SymbolRef SymRef(Sym);
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index de52ebcc72fded..cc1faab3cea317 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -2947,6 +2947,8 @@ void Dumper::printSymbol(const SymbolRef &Symbol,
                               Symbol.getRawDataRefImpl()));
   else if (O.isELF())
     outs() << '\t' << format(Fmt, ELFSymbolRef(Symbol).getSize());
+  else if (O.isWasm())
+    outs() << '\t' << format(Fmt, cast<WasmObjectFile>(O).getSymbolSize(Symbol));
 
   if (O.isELF()) {
     if (!SymbolVersions.empty()) {



More information about the llvm-commits mailing list