[llvm] [XCOFF][OBJECT] get symbol size by calling XCOFF interfaces (PR #67304)
Chen Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 9 22:54:33 PDT 2023
https://github.com/chenzheng1030 updated https://github.com/llvm/llvm-project/pull/67304
>From ad39c8f1bf88929fe52c2b0b09fba392f2a2eafd 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 1/3] 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 | 12 ++------
4 files changed, 42 insertions(+), 10 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 968c175ffdc6cd6..9ecec5e7590732b 100644
--- a/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll
+++ b/llvm/test/DebugInfo/Symbolize/XCOFF/xcoff-symbolize-data.ll
@@ -34,24 +34,18 @@
; CHECK-NEXT: /t.cpp:4
; CHECK-EMPTY:
-;; Test a function scope static variable.
-;; FIXME: fix the wrong size 152
; CHECK: f()::function_global
-; CHECK-NEXT: 144 152
+; CHECK-NEXT: 144 4
; CHECK-NEXT: /t.cpp:8
; CHECK-EMPTY:
-;; Test a global scope static variable that is used in current compilation unit.
-;; FIXME: fix the wrong size 152
; CHECK: beta
-; CHECK-NEXT: 148 152
+; CHECK-NEXT: 148 4
; CHECK-NEXT: /t.cpp:13
; CHECK-EMPTY:
-;; Test another global scope static variable that is used in current compilation unit.
-;; FIXME: fix the wrong size 152
; CHECK: alpha
-; CHECK-NEXT: 152 152
+; CHECK-NEXT: 152 4
; CHECK-NEXT: /t.cpp:12
; CHECK-EMPTY:
>From 05e3a1e56f1b67d0059018174729581c58ef6e17 Mon Sep 17 00:00:00 2001
From: Chen Zheng <czhengsz at cn.ibm.com>
Date: Wed, 27 Sep 2023 02:18:10 -0400
Subject: [PATCH 2/3] address comments
---
llvm/include/llvm/Object/XCOFFObjectFile.h | 10 ++++++----
llvm/lib/Object/XCOFFObjectFile.cpp | 16 ++++++++--------
2 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h
index 7f975e568b55257..c1072d20eaa637a 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -774,7 +774,7 @@ class XCOFFSymbolRef : public SymbolRef {
XCOFFSymbolRef(DataRefImpl SymEntDataRef,
const XCOFFObjectFile *OwningObjectPtr)
- : OwningObjectPtr(OwningObjectPtr) {
+ : SymbolRef(SymEntDataRef, OwningObjectPtr) {
assert(OwningObjectPtr && "OwningObjectPtr cannot be nullptr!");
assert(SymEntDataRef.p != 0 &&
"Symbol table entry pointer cannot be nullptr!");
@@ -795,8 +795,7 @@ class XCOFFSymbolRef : public SymbolRef {
uint64_t getValue64() const { return Entry64->Value; }
uint64_t getSize() const {
- return cast<XCOFFObjectFile>(BasicSymbolRef::getObject())
- ->getSymbolSize(getRawDataRefImpl());
+ return getObject()->getSymbolSize(getRawDataRefImpl());
}
#define GETVALUE(X) Entry32 ? Entry32->X : Entry64->X
@@ -834,7 +833,10 @@ class XCOFFSymbolRef : public SymbolRef {
Expected<XCOFFCsectAuxRef> getXCOFFCsectAuxRef() const;
private:
- const XCOFFObjectFile *OwningObjectPtr;
+ const XCOFFObjectFile *getObject() const {
+ return cast<XCOFFObjectFile>(BasicSymbolRef::getObject());
+ }
+
const XCOFFSymbolEntry32 *Entry32 = nullptr;
const XCOFFSymbolEntry64 *Entry64 = nullptr;
};
diff --git a/llvm/lib/Object/XCOFFObjectFile.cpp b/llvm/lib/Object/XCOFFObjectFile.cpp
index 7dcf344282e14fd..4fee4b089375161 100644
--- a/llvm/lib/Object/XCOFFObjectFile.cpp
+++ b/llvm/lib/Object/XCOFFObjectFile.cpp
@@ -1252,7 +1252,7 @@ bool XCOFFSymbolRef::isFunction() const {
return false;
const int16_t SectNum = getSectionNumber();
- Expected<DataRefImpl> SI = OwningObjectPtr->getSectionByNum(SectNum);
+ Expected<DataRefImpl> SI = getObject()->getSectionByNum(SectNum);
if (!SI) {
// If we could not get the section, then this symbol should not be
// a function. So consume the error and return `false` to move on.
@@ -1260,7 +1260,7 @@ bool XCOFFSymbolRef::isFunction() const {
return false;
}
- return (OwningObjectPtr->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT);
+ return (getObject()->getSectionFlags(SI.get()) & XCOFF::STYP_TEXT);
}
bool XCOFFSymbolRef::isCsectSymbol() const {
@@ -1279,13 +1279,13 @@ Expected<XCOFFCsectAuxRef> XCOFFSymbolRef::getXCOFFCsectAuxRef() const {
if (auto Err = NameOrErr.takeError())
return std::move(Err);
- uint32_t SymbolIdx = OwningObjectPtr->getSymbolIndex(getEntryAddress());
+ uint32_t SymbolIdx = getObject()->getSymbolIndex(getEntryAddress());
if (!NumberOfAuxEntries) {
return createError("csect symbol \"" + *NameOrErr + "\" with index " +
Twine(SymbolIdx) + " contains no auxiliary entry");
}
- if (!OwningObjectPtr->is64Bit()) {
+ if (!getObject()->is64Bit()) {
// In XCOFF32, the csect auxilliary entry is always the last auxiliary
// entry for the symbol.
uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
@@ -1298,10 +1298,10 @@ Expected<XCOFFCsectAuxRef> XCOFFSymbolRef::getXCOFFCsectAuxRef() const {
for (uint8_t Index = NumberOfAuxEntries; Index > 0; --Index) {
uintptr_t AuxAddr = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
getEntryAddress(), Index);
- if (*OwningObjectPtr->getSymbolAuxType(AuxAddr) ==
+ if (*getObject()->getSymbolAuxType(AuxAddr) ==
XCOFF::SymbolAuxType::AUX_CSECT) {
#ifndef NDEBUG
- OwningObjectPtr->checkSymbolEntryPointer(AuxAddr);
+ getObject()->checkSymbolEntryPointer(AuxAddr);
#endif
return XCOFFCsectAuxRef(viewAs<XCOFFCsectAuxEnt64>(AuxAddr));
}
@@ -1322,10 +1322,10 @@ Expected<StringRef> XCOFFSymbolRef::getName() const {
if (Entry32->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)
return generateXCOFFFixedNameStringRef(Entry32->SymbolName);
- return OwningObjectPtr->getStringTableEntry(Entry32->NameInStrTbl.Offset);
+ return getObject()->getStringTableEntry(Entry32->NameInStrTbl.Offset);
}
- return OwningObjectPtr->getStringTableEntry(Entry64->Offset);
+ return getObject()->getStringTableEntry(Entry64->Offset);
}
// Explictly instantiate template classes.
>From ca2fcaab0ad2105ed3bd6e475840e5e3e4b5fa1e Mon Sep 17 00:00:00 2001
From: Chen Zheng <czhengsz at cn.ibm.com>
Date: Thu, 28 Sep 2023 04:47:40 -0400
Subject: [PATCH 3/3] address comments
---
llvm/include/llvm/Object/XCOFFObjectFile.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h
index c1072d20eaa637a..a92a81d3535dcfb 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -844,8 +844,7 @@ class XCOFFSymbolRef : public SymbolRef {
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()))) {}
+ : symbol_iterator(B) {}
const XCOFFSymbolRef *operator->() const {
return static_cast<const XCOFFSymbolRef *>(symbol_iterator::operator->());
More information about the llvm-commits
mailing list