[llvm] [XCOFF][OBJECT] get symbol size by calling XCOFF interfaces (PR #67304)

Chen Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 26 01:08:35 PDT 2023


https://github.com/chenzheng1030 updated https://github.com/llvm/llvm-project/pull/67304

>From f4361189262a1927d28d2fbcccdc5e7d08b85657 Mon Sep 17 00:00:00 2001
From: Chen Zheng <czhengsz at cn.ibm.com>
Date: Tue, 26 Sep 2023 03:49:18 -0400
Subject: [PATCH 1/2] base case, nfc

---
 .../Symbolize/XCOFF/xcoff-symbolize-data.ll   | 149 ++++++++++++++++++
 1 file changed, 149 insertions(+)
 create mode 100644 llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll

diff --git a/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll b/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll
new file mode 100644
index 000000000000000..25bec197d87be49
--- /dev/null
+++ b/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll
@@ -0,0 +1,149 @@
+; RUN: llc -filetype=obj -o %t -mtriple=powerpc-aix-ibm-xcoff < %s
+; RUN: llvm-symbolizer --obj=%t 'DATA 0x60' 'DATA 0x61' 'DATA 0x64' 'DATA 0X68' \
+; RUN:   'DATA 0x90' 'DATA 0x94' 'DATA 0X98' | FileCheck %s
+
+; CHECK: bss_global
+; CHECK-NEXT: 96 4
+; CHECK-NEXT: ??:?
+; CHECK-EMPTY:
+
+; CHECK: bss_global
+; CHECK-NEXT: 96 4
+; CHECK-NEXT: ??:?
+; CHECK-EMPTY:
+
+; CHECK: data_global
+; CHECK-NEXT: 100 4
+; CHECK-NEXT: ??:?
+; CHECK-EMPTY:
+
+; CHECK: str
+; CHECK-NEXT: 104 4
+; CHECK-NEXT: ??:?
+; CHECK-EMPTY:
+
+; FIXME: fix the wrong size 152
+; CHECK: f()::function_global
+; CHECK-NEXT: 144 152
+; CHECK-NEXT: ??:?
+; CHECK-EMPTY:
+
+; FIXME: fix the wrong size 152
+; CHECK: beta
+; CHECK-NEXT: 148 152
+; CHECK-NEXT: ??:?
+; CHECK-EMPTY:
+
+; FIXME: fix the wrong size 152
+; CHECK: alpha
+; CHECK-NEXT: 152 152
+; CHECK-NEXT: ??:?
+; CHECK-EMPTY:
+
+; The case is from `test/tools/llvm-symbolizer/data-location.yaml`, compiled with:
+; clang++ -g -gdwarf-3 -O3 t.cpp  -nostdlib  -target powerpc-aix-ibm-xcoff -S -emit-llvm
+
+;     cat t.cpp
+;     1	int bss_global;
+;     2	int data_global = 2;
+;     3
+;     4	const char* str =
+;     5	  "12345678";
+;     6
+;     7	int* f() {
+;     8	  static int function_global;
+;     9	  return &function_global;
+;    10	}
+;    11
+;    12	static int alpha;
+;    13	static int beta;
+;    14	int *f(bool b) { return beta ? &alpha : β }
+;    15
+
+; ModuleID = 't.cpp'
+source_filename = "t.cpp"
+target datalayout = "E-m:a-p:32:32-Fi32-i64:64-n32"
+target triple = "powerpc-ibm-aix-xcoff"
+
+ at bss_global = local_unnamed_addr global i32 0, align 4, !dbg !0
+ at data_global = local_unnamed_addr global i32 2, align 4, !dbg !5
+ at .str = private unnamed_addr constant [9 x i8] c"12345678\00", align 1, !dbg !8
+ at str = local_unnamed_addr global ptr @.str, align 4, !dbg !15
+ at _ZZ1fvE15function_global = internal global i32 0, align 4, !dbg !18
+ at _ZL4beta = internal global i32 0, align 4, !dbg !24
+ at _ZL5alpha = internal global i32 0, align 4, !dbg !26
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+define noundef nonnull ptr @_Z1fv() local_unnamed_addr #0 !dbg !20 {
+entry:
+  ret ptr @_ZZ1fvE15function_global, !dbg !34
+}
+
+; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none)
+define noundef nonnull ptr @_Z1fb(i1 noundef zeroext %b) local_unnamed_addr #1 !dbg !35 {
+entry:
+  call void @llvm.dbg.value(metadata i1 %b, metadata !40, metadata !DIExpression(DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value)), !dbg !41
+  %0 = load i32, ptr @_ZL4beta, align 4, !dbg !42, !tbaa !43
+  %tobool.not = icmp eq i32 %0, 0, !dbg !42
+  %cond = select i1 %tobool.not, ptr @_ZL4beta, ptr @_ZL5alpha, !dbg !42
+  ret ptr %cond, !dbg !42
+}
+
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
+declare void @llvm.dbg.value(metadata, metadata, metadata) #2
+
+attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="pwr7" "target-features"="+altivec,+bpermd,+extdiv,+isa-v206-instructions,+vsx,-aix-small-local-exec-tls,-crbits,-crypto,-direct-move,-htm,-isa-v207-instructions,-isa-v30-instructions,-power8-vector,-power9-vector,-privileged,-quadword-atomics,-rop-protect,-spe" }
+attributes #1 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="pwr7" "target-features"="+altivec,+bpermd,+extdiv,+isa-v206-instructions,+vsx,-aix-small-local-exec-tls,-crbits,-crypto,-direct-move,-htm,-isa-v207-instructions,-isa-v30-instructions,-power8-vector,-power9-vector,-privileged,-quadword-atomics,-rop-protect,-spe" }
+attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
+
+!llvm.dbg.cu = !{!2}
+!llvm.module.flags = !{!28, !29, !30, !31, !32}
+!llvm.ident = !{!33}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "bss_global", scope: !2, file: !3, line: 1, type: !7, isLocal: false, isDefinition: true)
+!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "clang version 18.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
+!3 = !DIFile(filename: "t.cpp", directory: "/")
+!4 = !{!0, !5, !8, !15, !18, !24, !26}
+!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
+!6 = distinct !DIGlobalVariable(name: "data_global", scope: !2, file: !3, line: 2, type: !7, isLocal: false, isDefinition: true)
+!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!8 = !DIGlobalVariableExpression(var: !9, expr: !DIExpression())
+!9 = distinct !DIGlobalVariable(scope: null, file: !3, line: 5, type: !10, isLocal: true, isDefinition: true)
+!10 = !DICompositeType(tag: DW_TAG_array_type, baseType: !11, size: 72, elements: !13)
+!11 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !12)
+!12 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_unsigned_char)
+!13 = !{!14}
+!14 = !DISubrange(count: 9)
+!15 = !DIGlobalVariableExpression(var: !16, expr: !DIExpression())
+!16 = distinct !DIGlobalVariable(name: "str", scope: !2, file: !3, line: 4, type: !17, isLocal: false, isDefinition: true)
+!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 32)
+!18 = !DIGlobalVariableExpression(var: !19, expr: !DIExpression())
+!19 = distinct !DIGlobalVariable(name: "function_global", scope: !20, file: !3, line: 8, type: !7, isLocal: true, isDefinition: true)
+!20 = distinct !DISubprogram(name: "f", linkageName: "_Z1fv", scope: !3, file: !3, line: 7, type: !21, scopeLine: 7, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2)
+!21 = !DISubroutineType(types: !22)
+!22 = !{!23}
+!23 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 32)
+!24 = !DIGlobalVariableExpression(var: !25, expr: !DIExpression())
+!25 = distinct !DIGlobalVariable(name: "beta", linkageName: "_ZL4beta", scope: !2, file: !3, line: 13, type: !7, isLocal: true, isDefinition: true)
+!26 = !DIGlobalVariableExpression(var: !27, expr: !DIExpression())
+!27 = distinct !DIGlobalVariable(name: "alpha", linkageName: "_ZL5alpha", scope: !2, file: !3, line: 12, type: !7, isLocal: true, isDefinition: true)
+!28 = !{i32 7, !"Dwarf Version", i32 3}
+!29 = !{i32 2, !"Debug Info Version", i32 3}
+!30 = !{i32 1, !"wchar_size", i32 2}
+!31 = !{i32 8, !"PIC Level", i32 2}
+!32 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
+!33 = !{!"clang version 18.0.0"}
+!34 = !DILocation(line: 9, scope: !20)
+!35 = distinct !DISubprogram(name: "f", linkageName: "_Z1fb", scope: !3, file: !3, line: 14, type: !36, scopeLine: 14, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !2, retainedNodes: !39)
+!36 = !DISubroutineType(types: !37)
+!37 = !{!23, !38}
+!38 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)
+!39 = !{!40}
+!40 = !DILocalVariable(name: "b", arg: 1, scope: !35, file: !3, line: 14, type: !38)
+!41 = !DILocation(line: 0, scope: !35)
+!42 = !DILocation(line: 14, scope: !35)
+!43 = !{!44, !44, i64 0}
+!44 = !{!"int", !45, i64 0}
+!45 = !{!"omnipotent char", !46, i64 0}
+!46 = !{!"Simple C++ TBAA"}

>From b9a3a7a6cda9d98520a7d314b4cbdd839407fce6 Mon Sep 17 00:00:00 2001
From: Chen Zheng <czhengsz at cn.ibm.com>
Date: Mon, 25 Sep 2023 05:24:25 -0400
Subject: [PATCH 2/2] get symbol size by calling xcoff interfaces.

---
 llvm/include/llvm/Object/XCOFFObjectFile.h    | 29 ++++++++++++++++++-
 llvm/lib/Object/SymbolSize.cpp                |  7 +++++
 llvm/lib/Object/XCOFFObjectFile.cpp           |  4 +++
 .../Symbolize/XCOFF/xcoff-symbolize-data.ll   |  9 ++----
 4 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h
index 5f51aacfabc0851..7f975e568b55257 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -15,6 +15,7 @@
 
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/BinaryFormat/XCOFF.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Endian.h"
@@ -23,6 +24,8 @@
 namespace llvm {
 namespace object {
 
+class xcoff_symbol_iterator;
+
 struct XCOFFFileHeader32 {
   support::ubig16_t Magic;
   support::ubig16_t NumberOfSections;
@@ -576,6 +579,10 @@ class XCOFFObjectFile : public ObjectFile {
   Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
   basic_symbol_iterator symbol_begin() const override;
   basic_symbol_iterator symbol_end() const override;
+
+  using xcoff_symbol_iterator_range = iterator_range<xcoff_symbol_iterator>;
+  xcoff_symbol_iterator_range symbols() const;
+
   bool is64Bit() const override;
   Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
   Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
@@ -761,7 +768,7 @@ struct XCOFFSymbolEntry64 {
   uint8_t NumberOfAuxEntries;
 };
 
-class XCOFFSymbolRef {
+class XCOFFSymbolRef : public SymbolRef {
 public:
   enum { NAME_IN_STR_TBL_MAGIC = 0x0 };
 
@@ -787,6 +794,11 @@ class XCOFFSymbolRef {
 
   uint64_t getValue64() const { return Entry64->Value; }
 
+  uint64_t getSize() const {
+    return cast<XCOFFObjectFile>(BasicSymbolRef::getObject())
+        ->getSymbolSize(getRawDataRefImpl());
+  }
+
 #define GETVALUE(X) Entry32 ? Entry32->X : Entry64->X
 
   int16_t getSectionNumber() const { return GETVALUE(SectionNumber); }
@@ -827,6 +839,21 @@ class XCOFFSymbolRef {
   const XCOFFSymbolEntry64 *Entry64 = nullptr;
 };
 
+class xcoff_symbol_iterator : public symbol_iterator {
+public:
+  xcoff_symbol_iterator(const basic_symbol_iterator &B)
+      : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
+                                  cast<XCOFFObjectFile>(B->getObject()))) {}
+
+  const XCOFFSymbolRef *operator->() const {
+    return static_cast<const XCOFFSymbolRef *>(symbol_iterator::operator->());
+  }
+
+  const XCOFFSymbolRef &operator*() const {
+    return static_cast<const XCOFFSymbolRef &>(symbol_iterator::operator*());
+  }
+};
+
 class TBVectorExt {
   uint16_t Data;
   SmallString<32> VecParmsInfo;
diff --git a/llvm/lib/Object/SymbolSize.cpp b/llvm/lib/Object/SymbolSize.cpp
index f93a5f7d9bd5442..c4f30b1072d52da 100644
--- a/llvm/lib/Object/SymbolSize.cpp
+++ b/llvm/lib/Object/SymbolSize.cpp
@@ -59,6 +59,13 @@ llvm::object::computeSymbolSizes(const ObjectFile &O) {
     return Ret;
   }
 
+  if (const auto *E = dyn_cast<XCOFFObjectFile>(&O)) {
+    auto Syms = E->symbols();
+    for (XCOFFSymbolRef Sym : Syms)
+      Ret.push_back({Sym, Sym.getSize()});
+    return Ret;
+  }
+
   // Collect sorted symbol addresses. Include dummy addresses for the end
   // of each section.
   std::vector<SymEntry> Addresses;
diff --git a/llvm/lib/Object/XCOFFObjectFile.cpp b/llvm/lib/Object/XCOFFObjectFile.cpp
index fa4917e354e92b1..7dcf344282e14fd 100644
--- a/llvm/lib/Object/XCOFFObjectFile.cpp
+++ b/llvm/lib/Object/XCOFFObjectFile.cpp
@@ -689,6 +689,10 @@ basic_symbol_iterator XCOFFObjectFile::symbol_end() const {
   return basic_symbol_iterator(SymbolRef(SymDRI, this));
 }
 
+XCOFFObjectFile::xcoff_symbol_iterator_range XCOFFObjectFile::symbols() const {
+  return xcoff_symbol_iterator_range(symbol_begin(), symbol_end());
+}
+
 section_iterator XCOFFObjectFile::section_begin() const {
   DataRefImpl DRI;
   DRI.p = getSectionHeaderTableAddress();
diff --git a/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll b/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll
index 25bec197d87be49..e4507df318efaf7 100644
--- a/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll
+++ b/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll
@@ -22,21 +22,18 @@
 ; CHECK-NEXT: ??:?
 ; CHECK-EMPTY:
 
-; FIXME: fix the wrong size 152
 ; CHECK: f()::function_global
-; CHECK-NEXT: 144 152
+; CHECK-NEXT: 144 4
 ; CHECK-NEXT: ??:?
 ; CHECK-EMPTY:
 
-; FIXME: fix the wrong size 152
 ; CHECK: beta
-; CHECK-NEXT: 148 152
+; CHECK-NEXT: 148 4
 ; CHECK-NEXT: ??:?
 ; CHECK-EMPTY:
 
-; FIXME: fix the wrong size 152
 ; CHECK: alpha
-; CHECK-NEXT: 152 152
+; CHECK-NEXT: 152 4
 ; CHECK-NEXT: ??:?
 ; CHECK-EMPTY:
 



More information about the llvm-commits mailing list