[llvm] [XCOFF][OBJECT] get symbol size by calling XCOFF interfaces (PR #67304)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 25 02:32:32 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-binary-utilities
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/67304.diff
5 Files Affected:
- (modified) llvm/include/llvm/Object/XCOFFObjectFile.h (+31-1)
- (modified) llvm/lib/Object/SymbolSize.cpp (+7)
- (modified) llvm/lib/Object/XCOFFObjectFile.cpp (+4)
- (added) llvm/test/tools/llvm-symbolizer/Inputs/xcoff-dwarf.o ()
- (added) llvm/test/tools/llvm-symbolizer/xcoff.test (+39)
``````````diff
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/Inputs/xcoff-dwarf.o b/llvm/test/tools/llvm-symbolizer/Inputs/xcoff-dwarf.o
new file mode 100644
index 000000000000000..04326b6ccedd046
Binary files /dev/null and b/llvm/test/tools/llvm-symbolizer/Inputs/xcoff-dwarf.o differ
diff --git a/llvm/test/tools/llvm-symbolizer/xcoff.test b/llvm/test/tools/llvm-symbolizer/xcoff.test
new file mode 100644
index 000000000000000..35dd72c3ab38508
--- /dev/null
+++ b/llvm/test/tools/llvm-symbolizer/xcoff.test
@@ -0,0 +1,39 @@
+
+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:
+
+CHECK: f()::function_global
+CHECK-NEXT: 144 4
+CHECK-NEXT: ??:?
+CHECK-EMPTY:
+
+CHECK: beta
+CHECK-NEXT: 148 4
+CHECK-NEXT: ??:?
+CHECK-EMPTY:
+
+CHECK: alpha
+CHECK-NEXT: 152 4
+CHECK-NEXT: ??:?
+CHECK-EMPTY:
``````````
</details>
https://github.com/llvm/llvm-project/pull/67304
More information about the llvm-commits
mailing list