[llvm] [XCOFF][OBJECT] get symbol size by calling XCOFF interfaces (PR #67304)
Chen Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 25 02:31:26 PDT 2023
https://github.com/chenzheng1030 created https://github.com/llvm/llvm-project/pull/67304
XCOFF has aux symbol to indicate a symbol size. For example:
```
[27] m 0x00000090 .bss 1 unamex _ZZ1fvE15function_global
[28] a4 0x00000004 0 0 CM BS 0 0
[29] m 0x00000094 .bss 1 unamex _ZL4beta
[30] a4 0x00000004 0 0 CM BS 0 0
[31] m 0x00000098 .bss 1 unamex _ZL5alpha
[32] a4 0x00000004 0 0 CM BS 0 0
```
The case is from https://github.com/llvm/llvm-project/pull/67284
>From e2659ff614b83a99be941c35a6a479d6d476597a Mon Sep 17 00:00:00 2001
From: Chen Zheng <czhengsz at cn.ibm.com>
Date: Mon, 25 Sep 2023 01:06:28 -0400
Subject: [PATCH 1/2] base case, nfc
---
.../llvm-symbolizer/Inputs/xcoff-dwarf.o | Bin 0 -> 2256 bytes
llvm/test/tools/llvm-symbolizer/xcoff.test | 42 ++++++++++++++++++
2 files changed, 42 insertions(+)
create mode 100644 llvm/test/tools/llvm-symbolizer/Inputs/xcoff-dwarf.o
create mode 100644 llvm/test/tools/llvm-symbolizer/xcoff.test
diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/xcoff-dwarf.o b/llvm/test/tools/llvm-symbolizer/Inputs/xcoff-dwarf.o
new file mode 100644
index 0000000000000000000000000000000000000000..04326b6ccedd0462394ebaafb3fe4d6eda50e0c1
GIT binary patch
literal 2256
zcmah~&u<$=6n?YzChK2KTnj;4AX`LAXp at cCWbLGtLQ0fFsz6JVLz|1UYp-L2V_S}s
z=FmeVklI5JIpmNN2gI=l4oKYi6SyEw94c|>-+)8}eDCer-b9Uf(#(AG%{TAO?@_O4
zgv9h;P<+JD>iGvf;Re|jk%|+=e-tJrxoK71o*PKePMq at 4RiXP8n8=WP$?|r01rZ&g
z<dhEU(c;jmK5 at OSzaJrA!rw`xP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hdn
z%qwKF{I?8k5^$*L&x_9{Y;ci?o)7$-(*#Pohw{b$XgJmp-x(QC=qDGhcO=&FMVd&g
zlZ!NG{$RxD at IJl_dxpiC{}+468vcQ!2F>vA#T^nq_<hgWhShEJGihOeZOdM>EJhT0
VS<9RJ2d~)9`ByCZx(=VE{{R!!2KN8}
literal 0
HcmV?d00001
diff --git a/llvm/test/tools/llvm-symbolizer/xcoff.test b/llvm/test/tools/llvm-symbolizer/xcoff.test
new file mode 100644
index 000000000000000..b42001ef3b1488c
--- /dev/null
+++ b/llvm/test/tools/llvm-symbolizer/xcoff.test
@@ -0,0 +1,42 @@
+
+RUN: llvm-symbolizer --obj=%p/Inputs/xcoff-dwarf.o 'DATA 0x60' \
+RUN: 'DATA 0x61' 'DATA 0x64' 'DATA 0X68' 'DATA 0x90' 'DATA 0x94' \
+RUN: '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:
>From cd5209b03f1fa7fd7516138642bd1b9e8960d46d 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 | 32 +++++++++++++++++++++-
llvm/lib/Object/SymbolSize.cpp | 7 +++++
llvm/lib/Object/XCOFFObjectFile.cpp | 4 +++
llvm/test/tools/llvm-symbolizer/xcoff.test | 9 ++----
4 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h
index 5f51aacfabc0851..f7fd3b38e059f96 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -13,6 +13,7 @@
#ifndef LLVM_OBJECT_XCOFFOBJECTFILE_H
#define LLVM_OBJECT_XCOFFOBJECTFILE_H
+#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/BinaryFormat/XCOFF.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,14 @@ class XCOFFSymbolRef {
uint64_t getValue64() const { return Entry64->Value; }
+ const XCOFFObjectFile *getObject() const {
+ return cast<XCOFFObjectFile>(BasicSymbolRef::getObject());
+ }
+
+ uint64_t getSize() const {
+ return getObject()->getSymbolSize(getRawDataRefImpl());
+ }
+
#define GETVALUE(X) Entry32 ? Entry32->X : Entry64->X
int16_t getSectionNumber() const { return GETVALUE(SectionNumber); }
@@ -827,6 +842,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/tools/llvm-symbolizer/xcoff.test b/llvm/test/tools/llvm-symbolizer/xcoff.test
index b42001ef3b1488c..35dd72c3ab38508 100644
--- a/llvm/test/tools/llvm-symbolizer/xcoff.test
+++ b/llvm/test/tools/llvm-symbolizer/xcoff.test
@@ -23,20 +23,17 @@ CHECK-NEXT: 104 4
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