[llvm] [llvm-objdump] Print out xcoff file header for xcoff object file with option private-headers (PR #96350)

zhijian lin via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 12 12:05:37 PDT 2024


https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/96350

>From e2e335cadeb32d5eb10175793978f504c101ffef Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Fri, 21 Jun 2024 15:29:23 -0400
Subject: [PATCH 1/9] implement the print xcoff file header and load section
 header for option private-headers

---
 llvm/tools/llvm-objdump/XCOFFDump.cpp | 124 +++++++++++++++++++++++++-
 1 file changed, 122 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index d9c00c0962098..54b94ba7161b9 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -20,7 +20,9 @@
 #include "llvm/MC/MCSubtargetInfo.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Endian.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/FormattedStream.h"
+#include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 
 using namespace llvm;
@@ -30,10 +32,128 @@ using namespace llvm::support;
 
 namespace {
 class XCOFFDumper : public objdump::Dumper {
+  const XCOFFObjectFile &Obj;
+  unsigned Width;
 public:
-  XCOFFDumper(const object::XCOFFObjectFile &O) : Dumper(O) {}
-  void printPrivateHeaders() override {}
+  XCOFFDumper(const object::XCOFFObjectFile &O) : Dumper(O), Obj(O) {}
+  void printPrivateHeaders() override;
+  void printFileHeader();
+  void printAuxiliaryHeader(){};
+  void printLoaderSectionHeader();
+  FormattedString formatName(StringRef Name);
+  void printHex(StringRef Name, uint64_t Value);
+  void printNumber(StringRef Name, uint64_t Value);
+  void printStrHex(StringRef Name, StringRef Str, uint64_t Value);
+  void setWidth(unsigned W) {Width =W;};
 };
+
+void XCOFFDumper::printPrivateHeaders() {
+  printFileHeader();
+  printAuxiliaryHeader();
+  printLoaderSectionHeader();
+}
+
+FormattedString XCOFFDumper::formatName(StringRef Name) {
+  return FormattedString(Name, Width, FormattedString::JustifyLeft);
+}
+
+void XCOFFDumper::printHex(StringRef Name, uint64_t Value) {
+  outs() << formatName(Name) << format_hex(Value, 0) << "\n";
+}
+
+void XCOFFDumper::printNumber(StringRef Name, uint64_t Value) {
+  outs() << formatName(Name) << format_decimal(Value, 0) << "\n";
+}
+
+void XCOFFDumper::printStrHex(StringRef Name, StringRef Str, uint64_t Value) {
+  outs() << formatName(Name) << Str << " (" << format_decimal(Value, 0) << ")"
+         << "\n";
+}
+
+void XCOFFDumper::printFileHeader() {
+  setWidth(20);
+  outs() << "\n---File Header:\n";
+  printHex("Magic:", Obj.getMagic());
+  printNumber("NumberOfSections:", Obj.getNumberOfSections());
+
+  // Negative timestamp values are reserved for future use.
+  int32_t TimeStamp = Obj.getTimeStamp();
+  if (TimeStamp > 0) {
+    // This handling of the time stamp assumes that the host
+    // system's time_t is
+    //  compatible with AIX time_t. If a platform is not
+    //  compatible, the lit
+    // tests will let us know.
+    time_t TimeDate = TimeStamp;
+
+    char FormattedTime[80] = {};
+
+    size_t BytesFormatted = strftime(FormattedTime, sizeof(FormattedTime),
+                                     "%F %T", gmtime(&TimeDate));
+    if (BytesFormatted)
+      printStrHex("TimeStamp:", FormattedTime, TimeStamp);
+    else
+      printHex("Timestamp:", TimeStamp);
+  } else {
+    printStrHex("TimeStamp:", TimeStamp == 0 ? "None" : "Reserved Value",
+                TimeStamp);
+  }
+
+  // The number of symbol table entries is an unsigned value in
+  // 64-bit objects and a signed value (with negative values
+  // being 'reserved') in 32-bit objects.
+
+  if (Obj.is64Bit()) {
+    printHex("SymbolTableOffset:", Obj.getSymbolTableOffset64());
+    printNumber("SymbolTableEntries:", Obj.getNumberOfSymbolTableEntries64());
+  } else {
+    printHex("SymbolTableOffset:", Obj.getSymbolTableOffset32());
+    int32_t SymTabEntries = Obj.getRawNumberOfSymbolTableEntries32();
+    if (SymTabEntries >= 0)
+      printNumber("SymbolTableEntries:", SymTabEntries);
+    else
+      printStrHex("SymbolTableEntries:", "Reserved Value", SymTabEntries);
+  }
+
+  printHex("OptionalHeaderSize:", Obj.getOptionalHeaderSize());
+  printHex("Flags:", Obj.getFlags());
+}
+
+void XCOFFDumper::printLoaderSectionHeader() {
+  Expected<uintptr_t> LoaderSectionAddrOrError =
+      Obj.getSectionFileOffsetToRawData(XCOFF::STYP_LOADER);
+  if (!LoaderSectionAddrOrError) {
+    reportUniqueWarning(LoaderSectionAddrOrError.takeError());
+    return;
+  }
+  uintptr_t LoaderSectionAddr = LoaderSectionAddrOrError.get();
+
+  auto PrintLoadSecHeaderCommon = [&](const auto *LDHeader) {
+    printNumber("Version:", LDHeader->Version);
+    printNumber("NumberOfSymbolEntries:", LDHeader->NumberOfSymTabEnt);
+    printNumber("NumberOfRelocationEntries:", LDHeader->NumberOfRelTabEnt);
+    printNumber("LengthOfImportFileIDStringTable:",
+                LDHeader->LengthOfImpidStrTbl);
+    printNumber("NumberOfImportFileIDs:", LDHeader->NumberOfImpid);
+    printHex("OffsetToImportFileIDs:", LDHeader->OffsetToImpid);
+    printNumber("LengthOfStringTable:", LDHeader->LengthOfStrTbl);
+    printHex("OffsetToStringTable:", LDHeader->OffsetToStrTbl);
+  };
+
+  setWidth(35);
+  outs() << "\n---Loader Section Header:\n";
+  if (Obj.is64Bit()) {
+    const LoaderSectionHeader64 *LoaderSec64 =
+        reinterpret_cast<const LoaderSectionHeader64 *>(LoaderSectionAddr);
+    PrintLoadSecHeaderCommon(LoaderSec64);
+    printHex("OffsetToSymbolTable", LoaderSec64->OffsetToSymTbl);
+    printHex("OffsetToRelocationEntries", LoaderSec64->OffsetToRelEnt);
+  } else {
+    const LoaderSectionHeader32 *LoaderSec32 =
+        reinterpret_cast<const LoaderSectionHeader32 *>(LoaderSectionAddr);
+    PrintLoadSecHeaderCommon(LoaderSec32);
+  }
+}
 } // namespace
 
 std::unique_ptr<objdump::Dumper>

>From 553e8f556e75766efb2dd18243a360a20abccf5b Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Fri, 21 Jun 2024 15:38:16 -0400
Subject: [PATCH 2/9] add test case

---
 .../XCOFF/private-headers-option.test         | 83 +++++++++++++++++++
 1 file changed, 83 insertions(+)
 create mode 100644 llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test

diff --git a/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test b/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
new file mode 100644
index 0000000000000..21469ec67d150
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
@@ -0,0 +1,83 @@
+## Test the --privated-headers option for XCOFF object files.
+
+# RUN: yaml2obj --docnum=1 %s -o %t_xcoff32.o
+# RUN: yaml2obj --docnum=2  %s -o %t_xcoff64.o
+# RUN: llvm-objdump --private-headers %t_xcoff32.o |\
+# RUN:   FileCheck %s --check-prefixes=CHECK32 --match-full-lines
+# RUN: llvm-objdump --private-headers %t_xcoff64.o |\
+# RUN:   FileCheck %s --check-prefixes=CHECK64 --match-full-lines
+
+--- !XCOFF
+FileHeader:
+  MagicNumber:       0x1DF
+Sections:
+  - Name:            .loader
+    Flags:           [ STYP_LOADER ]
+    SectionData:     "0000000100000003000000050000016D00000001000000A40000001800000211"
+##                    ^-------                                                           -Version=1
+##                            ^-------                                                   -NumberOfSymbolEntries=3
+##                                    ^-------                                           -NumberOfRelocationEntries=5
+##                                            ^-------                                   -LengthOfImportFileIDStringTable=365
+##                                                    ^-------                           -NumberOfImportFileIDs=1
+##                                                            ^-------                   -OffsetToImportFileIDs=0xA4
+##                                                                    ^-------           -LengthOfStringTable=24
+##                                                                            ^-------   -OffsetToStringTable=0x211
+
+
+--- !XCOFF
+FileHeader:
+  MagicNumber:       0x1F7
+Sections:
+  - Name:            .loader
+    Flags:           [ STYP_LOADER ]
+    SectionData:     "0000000200000003000000050000016D000000010000002300000000000000D0000000000000023D00000000000000380000000000000080"
+##                    ^-------                                                           -Version=2
+##                            ^-------                                                   -NumberOfSymbolEntries=3
+##                                    ^-------                                           -NumberOfRelocationEntries=5
+##                                            ^-------                                   -LengthOfImportFileIDStringTable=365
+##                                                    ^-------                           -NumberOfImportFileIDs=1
+##                                                            ^-------                   --LengthOfStringTable=0x23
+##                                                                    ^---------------   -OffsetToImportFileIDs=0xD0
+##                                                                                    ^---------------                                        -OffsetToStringTable=0x23D
+##                                                                                                    ^--------------                         -OffsetToSymbolTable=0x38
+##                                                                                                                    ^---------------        -OffsetToRelocationEntries=0x80
+
+# CHECK32:      ---File Header:
+# CHECK32-NEXT: Magic:              0x1df
+# CHECK32-NEXT: NumberOfSections:   1
+# CHECK32-NEXT: TimeStamp:          None (0)
+# CHECK32-NEXT: SymbolTableOffset:  0x0
+# CHECK32-NEXT: SymbolTableEntries: 0
+# CHECK32-NEXT: OptionalHeaderSize: 0x0
+# CHECK32-NEXT: Flags:              0x0
+
+# CHECK32:      ---Loader Section Header:
+# CHECK32-NEXT: Version:                           1
+# CHECK32-NEXT: NumberOfSymbolEntries:             3
+# CHECK32-NEXT: NumberOfRelocationEntries:         5
+# CHECK32-NEXT: LengthOfImportFileIDStringTable:   365
+# CHECK32-NEXT: NumberOfImportFileIDs:             1
+# CHECK32-NEXT: OffsetToImportFileIDs:             0xa4
+# CHECK32-NEXT: LengthOfStringTable:               24
+# CHECK32-NEXT: OffsetToStringTable:               0x211
+
+# CHECK64:      ---File Header:
+# CHECK64-NEXT: Magic:              0x1f7
+# CHECK64-NEXT: NumberOfSections:   1
+# CHECK64-NEXT: TimeStamp:          None (0)
+# CHECK64-NEXT: SymbolTableOffset:  0x0
+# CHECK64-NEXT: SymbolTableEntries: 0
+# CHECK64-NEXT: OptionalHeaderSize: 0x0
+# CHECK64-NEXT: Flags:              0x0
+
+# CHECK64:      ---Loader Section Header:
+# CHECK64-NEXT: Version:                           2
+# CHECK64-NEXT: NumberOfSymbolEntries:             3
+# CHECK64-NEXT: NumberOfRelocationEntries:         5
+# CHECK64-NEXT: LengthOfImportFileIDStringTable:   365
+# CHECK64-NEXT: NumberOfImportFileIDs:             1
+# CHECK64-NEXT: OffsetToImportFileIDs:             0xd0
+# CHECK64-NEXT: LengthOfStringTable:               35
+# CHECK64-NEXT: OffsetToStringTable:               0x23d
+# CHECK64-NEXT: OffsetToSymbolTable                0x38
+# CHECK64-NEXT: OffsetToRelocationEntries          0x80

>From 80c454b7e040a51444cfbb00c3313be0b56c6eaa Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Mon, 24 Jun 2024 16:04:59 -0400
Subject: [PATCH 3/9] fix a potential bug

---
 llvm/tools/llvm-objdump/XCOFFDump.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index 54b94ba7161b9..2ee9b882335d2 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -128,6 +128,9 @@ void XCOFFDumper::printLoaderSectionHeader() {
   }
   uintptr_t LoaderSectionAddr = LoaderSectionAddrOrError.get();
 
+  if (LoaderSectionAddr == 0)
+    return;
+
   auto PrintLoadSecHeaderCommon = [&](const auto *LDHeader) {
     printNumber("Version:", LDHeader->Version);
     printNumber("NumberOfSymbolEntries:", LDHeader->NumberOfSymTabEnt);

>From 1ab19e6a0e814e994e0efca05c85b52cedacdac7 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Tue, 2 Jul 2024 10:05:47 -0400
Subject: [PATCH 4/9] address comment

---
 llvm/tools/llvm-objdump/XCOFFDump.cpp | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index 2ee9b882335d2..10421b2764efc 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -36,6 +36,8 @@ class XCOFFDumper : public objdump::Dumper {
   unsigned Width;
 public:
   XCOFFDumper(const object::XCOFFObjectFile &O) : Dumper(O), Obj(O) {}
+
+private:
   void printPrivateHeaders() override;
   void printFileHeader();
   void printAuxiliaryHeader(){};
@@ -44,7 +46,7 @@ class XCOFFDumper : public objdump::Dumper {
   void printHex(StringRef Name, uint64_t Value);
   void printNumber(StringRef Name, uint64_t Value);
   void printStrHex(StringRef Name, StringRef Str, uint64_t Value);
-  void setWidth(unsigned W) {Width =W;};
+  void setWidth(unsigned W) { Width = W; };
 };
 
 void XCOFFDumper::printPrivateHeaders() {
@@ -76,14 +78,12 @@ void XCOFFDumper::printFileHeader() {
   printHex("Magic:", Obj.getMagic());
   printNumber("NumberOfSections:", Obj.getNumberOfSections());
 
-  // Negative timestamp values are reserved for future use.
   int32_t TimeStamp = Obj.getTimeStamp();
+  // Negative timestamp values are reserved for future use.
   if (TimeStamp > 0) {
-    // This handling of the time stamp assumes that the host
-    // system's time_t is
-    //  compatible with AIX time_t. If a platform is not
-    //  compatible, the lit
-    // tests will let us know.
+    // This handling of the time stamp assumes that the host  system's time_t is
+    // compatible with AIX time_t. If a platform is not
+    //  compatible, the lit tests will let us know.
     time_t TimeDate = TimeStamp;
 
     char FormattedTime[80] = {};
@@ -102,7 +102,6 @@ void XCOFFDumper::printFileHeader() {
   // The number of symbol table entries is an unsigned value in
   // 64-bit objects and a signed value (with negative values
   // being 'reserved') in 32-bit objects.
-
   if (Obj.is64Bit()) {
     printHex("SymbolTableOffset:", Obj.getSymbolTableOffset64());
     printNumber("SymbolTableEntries:", Obj.getNumberOfSymbolTableEntries64());

>From 39e9fc18c19fdb07783b20f43ea6fcea210a76d5 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Wed, 3 Jul 2024 09:38:54 -0400
Subject: [PATCH 5/9] clang-format

---
 llvm/tools/llvm-objdump/XCOFFDump.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index 10421b2764efc..39a4199cd8f29 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -34,6 +34,7 @@ namespace {
 class XCOFFDumper : public objdump::Dumper {
   const XCOFFObjectFile &Obj;
   unsigned Width;
+
 public:
   XCOFFDumper(const object::XCOFFObjectFile &O) : Dumper(O), Obj(O) {}
 

>From 3f0626378f505cb2e848bbd46f2706a407697558 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Thu, 4 Jul 2024 10:18:14 -0400
Subject: [PATCH 6/9] minnor change

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

diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index 39a4199cd8f29..552f43e206fc4 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -41,7 +41,7 @@ class XCOFFDumper : public objdump::Dumper {
 private:
   void printPrivateHeaders() override;
   void printFileHeader();
-  void printAuxiliaryHeader(){};
+  void printAuxiliaryHeader() {};
   void printLoaderSectionHeader();
   FormattedString formatName(StringRef Name);
   void printHex(StringRef Name, uint64_t Value);

>From 747198942d311dcdd7f2cf19cb0bf1c0768445b2 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Fri, 2 Aug 2024 09:38:23 -0400
Subject: [PATCH 7/9] only putting printFileHeader in first patch

---
 .../XCOFF/private-headers-option.test         | 69 ++++---------------
 llvm/tools/llvm-objdump/XCOFFDump.cpp         | 48 +------------
 2 files changed, 15 insertions(+), 102 deletions(-)

diff --git a/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test b/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
index 21469ec67d150..f93740c4a5f76 100644
--- a/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
+++ b/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
@@ -1,7 +1,7 @@
-## Test the --privated-headers option for XCOFF object files.
+## Test the --private-headers option for XCOFF object files.
 
-# RUN: yaml2obj --docnum=1 %s -o %t_xcoff32.o
-# RUN: yaml2obj --docnum=2  %s -o %t_xcoff64.o
+# RUN: yaml2obj -DMAGIC=0x1DF %s -o %t_xcoff32.o
+# RUN: yaml2obj -DMAGIC=0x1F7 %s -o %t_xcoff64.o
 # RUN: llvm-objdump --private-headers %t_xcoff32.o |\
 # RUN:   FileCheck %s --check-prefixes=CHECK32 --match-full-lines
 # RUN: llvm-objdump --private-headers %t_xcoff64.o |\
@@ -9,75 +9,30 @@
 
 --- !XCOFF
 FileHeader:
-  MagicNumber:       0x1DF
+  MagicNumber:       [[MAGIC]]
 Sections:
-  - Name:            .loader
-    Flags:           [ STYP_LOADER ]
-    SectionData:     "0000000100000003000000050000016D00000001000000A40000001800000211"
-##                    ^-------                                                           -Version=1
-##                            ^-------                                                   -NumberOfSymbolEntries=3
-##                                    ^-------                                           -NumberOfRelocationEntries=5
-##                                            ^-------                                   -LengthOfImportFileIDStringTable=365
-##                                                    ^-------                           -NumberOfImportFileIDs=1
-##                                                            ^-------                   -OffsetToImportFileIDs=0xA4
-##                                                                    ^-------           -LengthOfStringTable=24
-##                                                                            ^-------   -OffsetToStringTable=0x211
-
-
---- !XCOFF
-FileHeader:
-  MagicNumber:       0x1F7
-Sections:
-  - Name:            .loader
-    Flags:           [ STYP_LOADER ]
-    SectionData:     "0000000200000003000000050000016D000000010000002300000000000000D0000000000000023D00000000000000380000000000000080"
-##                    ^-------                                                           -Version=2
-##                            ^-------                                                   -NumberOfSymbolEntries=3
-##                                    ^-------                                           -NumberOfRelocationEntries=5
-##                                            ^-------                                   -LengthOfImportFileIDStringTable=365
-##                                                    ^-------                           -NumberOfImportFileIDs=1
-##                                                            ^-------                   --LengthOfStringTable=0x23
-##                                                                    ^---------------   -OffsetToImportFileIDs=0xD0
-##                                                                                    ^---------------                                        -OffsetToStringTable=0x23D
-##                                                                                                    ^--------------                         -OffsetToSymbolTable=0x38
-##                                                                                                                    ^---------------        -OffsetToRelocationEntries=0x80
+  - Name:            .text
+    Flags:           [ STYP_TEXT ]
+    SectionData:     "9061FFF880820000"
+  - Name:            .data
+    Flags:           [ STYP_DATA ]
+    SectionData:     "0000000000000FC0"
 
 # CHECK32:      ---File Header:
 # CHECK32-NEXT: Magic:              0x1df
-# CHECK32-NEXT: NumberOfSections:   1
+# CHECK32-NEXT: NumberOfSections:   2
 # CHECK32-NEXT: TimeStamp:          None (0)
 # CHECK32-NEXT: SymbolTableOffset:  0x0
 # CHECK32-NEXT: SymbolTableEntries: 0
 # CHECK32-NEXT: OptionalHeaderSize: 0x0
 # CHECK32-NEXT: Flags:              0x0
 
-# CHECK32:      ---Loader Section Header:
-# CHECK32-NEXT: Version:                           1
-# CHECK32-NEXT: NumberOfSymbolEntries:             3
-# CHECK32-NEXT: NumberOfRelocationEntries:         5
-# CHECK32-NEXT: LengthOfImportFileIDStringTable:   365
-# CHECK32-NEXT: NumberOfImportFileIDs:             1
-# CHECK32-NEXT: OffsetToImportFileIDs:             0xa4
-# CHECK32-NEXT: LengthOfStringTable:               24
-# CHECK32-NEXT: OffsetToStringTable:               0x211
-
 # CHECK64:      ---File Header:
 # CHECK64-NEXT: Magic:              0x1f7
-# CHECK64-NEXT: NumberOfSections:   1
+# CHECK64-NEXT: NumberOfSections:   2
 # CHECK64-NEXT: TimeStamp:          None (0)
 # CHECK64-NEXT: SymbolTableOffset:  0x0
 # CHECK64-NEXT: SymbolTableEntries: 0
 # CHECK64-NEXT: OptionalHeaderSize: 0x0
 # CHECK64-NEXT: Flags:              0x0
 
-# CHECK64:      ---Loader Section Header:
-# CHECK64-NEXT: Version:                           2
-# CHECK64-NEXT: NumberOfSymbolEntries:             3
-# CHECK64-NEXT: NumberOfRelocationEntries:         5
-# CHECK64-NEXT: LengthOfImportFileIDStringTable:   365
-# CHECK64-NEXT: NumberOfImportFileIDs:             1
-# CHECK64-NEXT: OffsetToImportFileIDs:             0xd0
-# CHECK64-NEXT: LengthOfStringTable:               35
-# CHECK64-NEXT: OffsetToStringTable:               0x23d
-# CHECK64-NEXT: OffsetToSymbolTable                0x38
-# CHECK64-NEXT: OffsetToRelocationEntries          0x80
diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index 552f43e206fc4..ae592a600f9a6 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -41,8 +41,6 @@ class XCOFFDumper : public objdump::Dumper {
 private:
   void printPrivateHeaders() override;
   void printFileHeader();
-  void printAuxiliaryHeader() {};
-  void printLoaderSectionHeader();
   FormattedString formatName(StringRef Name);
   void printHex(StringRef Name, uint64_t Value);
   void printNumber(StringRef Name, uint64_t Value);
@@ -52,8 +50,6 @@ class XCOFFDumper : public objdump::Dumper {
 
 void XCOFFDumper::printPrivateHeaders() {
   printFileHeader();
-  printAuxiliaryHeader();
-  printLoaderSectionHeader();
 }
 
 FormattedString XCOFFDumper::formatName(StringRef Name) {
@@ -80,11 +76,10 @@ void XCOFFDumper::printFileHeader() {
   printNumber("NumberOfSections:", Obj.getNumberOfSections());
 
   int32_t TimeStamp = Obj.getTimeStamp();
-  // Negative timestamp values are reserved for future use.
   if (TimeStamp > 0) {
     // This handling of the time stamp assumes that the host  system's time_t is
-    // compatible with AIX time_t. If a platform is not
-    //  compatible, the lit tests will let us know.
+    // compatible with AIX time_t. If a platform is not  compatible, the lit
+    // tests will let us know.
     time_t TimeDate = TimeStamp;
 
     char FormattedTime[80] = {};
@@ -96,6 +91,7 @@ void XCOFFDumper::printFileHeader() {
     else
       printHex("Timestamp:", TimeStamp);
   } else {
+    // Negative timestamp values are reserved for future use.
     printStrHex("TimeStamp:", TimeStamp == 0 ? "None" : "Reserved Value",
                 TimeStamp);
   }
@@ -119,44 +115,6 @@ void XCOFFDumper::printFileHeader() {
   printHex("Flags:", Obj.getFlags());
 }
 
-void XCOFFDumper::printLoaderSectionHeader() {
-  Expected<uintptr_t> LoaderSectionAddrOrError =
-      Obj.getSectionFileOffsetToRawData(XCOFF::STYP_LOADER);
-  if (!LoaderSectionAddrOrError) {
-    reportUniqueWarning(LoaderSectionAddrOrError.takeError());
-    return;
-  }
-  uintptr_t LoaderSectionAddr = LoaderSectionAddrOrError.get();
-
-  if (LoaderSectionAddr == 0)
-    return;
-
-  auto PrintLoadSecHeaderCommon = [&](const auto *LDHeader) {
-    printNumber("Version:", LDHeader->Version);
-    printNumber("NumberOfSymbolEntries:", LDHeader->NumberOfSymTabEnt);
-    printNumber("NumberOfRelocationEntries:", LDHeader->NumberOfRelTabEnt);
-    printNumber("LengthOfImportFileIDStringTable:",
-                LDHeader->LengthOfImpidStrTbl);
-    printNumber("NumberOfImportFileIDs:", LDHeader->NumberOfImpid);
-    printHex("OffsetToImportFileIDs:", LDHeader->OffsetToImpid);
-    printNumber("LengthOfStringTable:", LDHeader->LengthOfStrTbl);
-    printHex("OffsetToStringTable:", LDHeader->OffsetToStrTbl);
-  };
-
-  setWidth(35);
-  outs() << "\n---Loader Section Header:\n";
-  if (Obj.is64Bit()) {
-    const LoaderSectionHeader64 *LoaderSec64 =
-        reinterpret_cast<const LoaderSectionHeader64 *>(LoaderSectionAddr);
-    PrintLoadSecHeaderCommon(LoaderSec64);
-    printHex("OffsetToSymbolTable", LoaderSec64->OffsetToSymTbl);
-    printHex("OffsetToRelocationEntries", LoaderSec64->OffsetToRelEnt);
-  } else {
-    const LoaderSectionHeader32 *LoaderSec32 =
-        reinterpret_cast<const LoaderSectionHeader32 *>(LoaderSectionAddr);
-    PrintLoadSecHeaderCommon(LoaderSec32);
-  }
-}
 } // namespace
 
 std::unique_ptr<objdump::Dumper>

>From 59e04756c8bfcd016e3af0c8b0475bf72d845c28 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Wed, 7 Aug 2024 13:31:24 -0400
Subject: [PATCH 8/9]  address a git clang format error

---
 llvm/tools/llvm-objdump/XCOFFDump.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index ae592a600f9a6..6a5ba4ab33f34 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -48,9 +48,7 @@ class XCOFFDumper : public objdump::Dumper {
   void setWidth(unsigned W) { Width = W; };
 };
 
-void XCOFFDumper::printPrivateHeaders() {
-  printFileHeader();
-}
+void XCOFFDumper::printPrivateHeaders() { printFileHeader(); }
 
 FormattedString XCOFFDumper::formatName(StringRef Name) {
   return FormattedString(Name, Width, FormattedString::JustifyLeft);

>From 893cfffc4a3f1097be8f6d4d75d84dce8cabae03 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Mon, 12 Aug 2024 19:15:29 +0000
Subject: [PATCH 9/9] address comment

---
 .../XCOFF/private-headers-option.test         | 56 +++++++++++--------
 llvm/tools/llvm-objdump/XCOFFDump.cpp         | 24 ++++----
 2 files changed, 46 insertions(+), 34 deletions(-)

diff --git a/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test b/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
index f93740c4a5f76..14a79ed136f7a 100644
--- a/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
+++ b/llvm/test/tools/llvm-objdump/XCOFF/private-headers-option.test
@@ -1,11 +1,15 @@
 ## Test the --private-headers option for XCOFF object files.
 
-# RUN: yaml2obj -DMAGIC=0x1DF %s -o %t_xcoff32.o
-# RUN: yaml2obj -DMAGIC=0x1F7 %s -o %t_xcoff64.o
-# RUN: llvm-objdump --private-headers %t_xcoff32.o |\
-# RUN:   FileCheck %s --check-prefixes=CHECK32 --match-full-lines
-# RUN: llvm-objdump --private-headers %t_xcoff64.o |\
-# RUN:   FileCheck %s --check-prefixes=CHECK64 --match-full-lines
+# RUN: yaml2obj -DMAGIC=0x1DF --docnum=1 %s -o %t_xcoff32.o
+# RUN: yaml2obj -DMAGIC=0x1F7 --docnum=1 %s -o %t_xcoff64.o
+# RUN: llvm-objdump --private-headers %t_xcoff32.o | \
+# RUN:   FileCheck %s --check-prefixes=CHECK32 --match-full-lines --strict-whitespace
+# RUN: llvm-objdump --private-headers %t_xcoff64.o | \
+# RUN:   FileCheck %s --check-prefixes=CHECK64 --match-full-lines --strict-whitespace
+
+# RUN: yaml2obj -DMAGIC=0x1DF --docnum=2 %s -o %t_xcoff_timestamp.o
+# RUN: llvm-objdump --private-headers %t_xcoff_timestamp.o | \
+# RUN:   FileCheck %s --match-full-lines
 
 --- !XCOFF
 FileHeader:
@@ -18,21 +22,29 @@ Sections:
     Flags:           [ STYP_DATA ]
     SectionData:     "0000000000000FC0"
 
-# CHECK32:      ---File Header:
-# CHECK32-NEXT: Magic:              0x1df
-# CHECK32-NEXT: NumberOfSections:   2
-# CHECK32-NEXT: TimeStamp:          None (0)
-# CHECK32-NEXT: SymbolTableOffset:  0x0
-# CHECK32-NEXT: SymbolTableEntries: 0
-# CHECK32-NEXT: OptionalHeaderSize: 0x0
-# CHECK32-NEXT: Flags:              0x0
+# CHECK32:---File Header:
+# CHECK32-NEXT:Magic:              0x1df
+# CHECK32-NEXT:NumberOfSections:   2
+# CHECK32-NEXT:TimeStamp:          None (0)
+# CHECK32-NEXT:SymbolTableOffset:  0x0
+# CHECK32-NEXT:SymbolTableEntries: 0
+# CHECK32-NEXT:OptionalHeaderSize: 0x0
+# CHECK32-NEXT:Flags:              0x0
+
+# CHECK64:---File Header:
+# CHECK64-NEXT:Magic:              0x1f7
+# CHECK64-NEXT:NumberOfSections:   2
+# CHECK64-NEXT:TimeStamp:          None (0)
+# CHECK64-NEXT:SymbolTableOffset:  0x0
+# CHECK64-NEXT:SymbolTableEntries: 0
+# CHECK64-NEXT:OptionalHeaderSize: 0x0
+# CHECK64-NEXT:Flags:              0x0
 
-# CHECK64:      ---File Header:
-# CHECK64-NEXT: Magic:              0x1f7
-# CHECK64-NEXT: NumberOfSections:   2
-# CHECK64-NEXT: TimeStamp:          None (0)
-# CHECK64-NEXT: SymbolTableOffset:  0x0
-# CHECK64-NEXT: SymbolTableEntries: 0
-# CHECK64-NEXT: OptionalHeaderSize: 0x0
-# CHECK64-NEXT: Flags:              0x0
+--- !XCOFF
+FileHeader:
+  MagicNumber:       0x1DF
+  CreationTime:      1234
+  EntriesInSymbolTable: -1
 
+# CHECK: TimeStamp:          1970-01-01 00:20:34 (1234)
+# CHECK: SymbolTableEntries: Reserved Value (-1)
diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp
index 6a5ba4ab33f34..001aea4cc9eeb 100644
--- a/llvm/tools/llvm-objdump/XCOFFDump.cpp
+++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp
@@ -63,8 +63,8 @@ void XCOFFDumper::printNumber(StringRef Name, uint64_t Value) {
 }
 
 void XCOFFDumper::printStrHex(StringRef Name, StringRef Str, uint64_t Value) {
-  outs() << formatName(Name) << Str << " (" << format_decimal(Value, 0) << ")"
-         << "\n";
+  outs() << formatName(Name) << Str << " (" << format_decimal(Value, 0)
+         << ")\n";
 }
 
 void XCOFFDumper::printFileHeader() {
@@ -73,30 +73,30 @@ void XCOFFDumper::printFileHeader() {
   printHex("Magic:", Obj.getMagic());
   printNumber("NumberOfSections:", Obj.getNumberOfSections());
 
-  int32_t TimeStamp = Obj.getTimeStamp();
+  int32_t Timestamp = Obj.getTimeStamp();
   if (TimeStamp > 0) {
-    // This handling of the time stamp assumes that the host  system's time_t is
-    // compatible with AIX time_t. If a platform is not  compatible, the lit
+    // This handling of the timestamp assumes that the host system's time_t is
+    // compatible with AIX time_t. If a platform is not compatible, the lit
     // tests will let us know.
     time_t TimeDate = TimeStamp;
 
     char FormattedTime[80] = {};
 
-    size_t BytesFormatted = strftime(FormattedTime, sizeof(FormattedTime),
-                                     "%F %T", gmtime(&TimeDate));
+    size_t BytesFormatted = std::strftime(FormattedTime, sizeof(FormattedTime),
+                                          "%F %T", gmtime(&TimeDate));
     if (BytesFormatted)
-      printStrHex("TimeStamp:", FormattedTime, TimeStamp);
+      printStrHex("Timestamp:", FormattedTime, TimeStamp);
     else
       printHex("Timestamp:", TimeStamp);
   } else {
     // Negative timestamp values are reserved for future use.
-    printStrHex("TimeStamp:", TimeStamp == 0 ? "None" : "Reserved Value",
+    printStrHex("Timestamp:", TimeStamp == 0 ? "None" : "Reserved Value",
                 TimeStamp);
   }
 
-  // The number of symbol table entries is an unsigned value in
-  // 64-bit objects and a signed value (with negative values
-  // being 'reserved') in 32-bit objects.
+  // The number of symbol table entries is an unsigned value in 64-bit objects
+  // and a signed value (with negative values being 'reserved') in 32-bit
+  // objects.
   if (Obj.is64Bit()) {
     printHex("SymbolTableOffset:", Obj.getSymbolTableOffset64());
     printNumber("SymbolTableEntries:", Obj.getNumberOfSymbolTableEntries64());



More information about the llvm-commits mailing list