[clang-tools-extra] [clang] [llvm] [XCOFF][obj2yaml] support parsing auxiliary symbols for XCOFF (PR #70642)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 4 21:40:50 PST 2023


https://github.com/EsmeYi updated https://github.com/llvm/llvm-project/pull/70642

>From 7ba6a651bb84befdcd1566352d5340880591789d Mon Sep 17 00:00:00 2001
From: esmeyi <esme.yi at ibm.com>
Date: Mon, 30 Oct 2023 06:00:18 -0400
Subject: [PATCH 1/7] Add support for parsing auxiliary symbols of XCOFF object
 file for obj2yaml.

---
 llvm/include/llvm/Object/XCOFFObjectFile.h    |   2 +-
 llvm/lib/ObjectYAML/XCOFFYAML.cpp             |  27 +-
 llvm/test/tools/obj2yaml/XCOFF/aix.yaml       |  38 +++
 .../tools/obj2yaml/XCOFF/aux-symbols.yaml     | 264 ++++++++++++++++++
 llvm/tools/obj2yaml/xcoff2yaml.cpp            | 185 +++++++++++-
 5 files changed, 504 insertions(+), 12 deletions(-)
 create mode 100644 llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml

diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h
index 63064abb4d3c3..9a3b4aa262355 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -411,13 +411,13 @@ class XCOFFCsectAuxRef {
     return Entry64->AuxType;
   }
 
-private:
   uint8_t getSymbolAlignmentAndType() const {
     return GETVALUE(SymbolAlignmentAndType);
   }
 
 #undef GETVALUE
 
+private:
   const XCOFFCsectAuxEnt32 *Entry32 = nullptr;
   const XCOFFCsectAuxEnt64 *Entry64 = nullptr;
 };
diff --git a/llvm/lib/ObjectYAML/XCOFFYAML.cpp b/llvm/lib/ObjectYAML/XCOFFYAML.cpp
index 44ef33501b65e..a9af0405546a3 100644
--- a/llvm/lib/ObjectYAML/XCOFFYAML.cpp
+++ b/llvm/lib/ObjectYAML/XCOFFYAML.cpp
@@ -282,45 +282,53 @@ static void auxSymMapping(IO &IO, XCOFFYAML::SectAuxEntForStat &AuxSym) {
 
 void MappingTraits<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>>::mapping(
     IO &IO, std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym) {
-  assert(!IO.outputting() && "We don't dump aux symbols currently.");
   const bool Is64 =
       static_cast<XCOFFYAML::Object *>(IO.getContext())->Header.Magic ==
       (llvm::yaml::Hex16)XCOFF::XCOFF64;
   XCOFFYAML::AuxSymbolType AuxType;
+  if (IO.outputting())
+    AuxType = AuxSym.get()->Type;
   IO.mapRequired("Type", AuxType);
   switch (AuxType) {
   case XCOFFYAML::AUX_EXCEPT:
     if (!Is64)
       IO.setError("an auxiliary symbol of type AUX_EXCEPT cannot be defined in "
                   "XCOFF32");
-    AuxSym.reset(new XCOFFYAML::ExcpetionAuxEnt());
+    if (!IO.outputting())
+      AuxSym.reset(new XCOFFYAML::ExcpetionAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::ExcpetionAuxEnt>(AuxSym.get()));
     break;
   case XCOFFYAML::AUX_FCN:
-    AuxSym.reset(new XCOFFYAML::FunctionAuxEnt());
+    if (!IO.outputting())
+      AuxSym.reset(new XCOFFYAML::FunctionAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::FunctionAuxEnt>(AuxSym.get()), Is64);
     break;
   case XCOFFYAML::AUX_SYM:
-    AuxSym.reset(new XCOFFYAML::BlockAuxEnt());
+    if (!IO.outputting())
+      AuxSym.reset(new XCOFFYAML::BlockAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::BlockAuxEnt>(AuxSym.get()), Is64);
     break;
   case XCOFFYAML::AUX_FILE:
-    AuxSym.reset(new XCOFFYAML::FileAuxEnt());
+    if (!IO.outputting())
+      AuxSym.reset(new XCOFFYAML::FileAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::FileAuxEnt>(AuxSym.get()));
     break;
   case XCOFFYAML::AUX_CSECT:
-    AuxSym.reset(new XCOFFYAML::CsectAuxEnt());
+    if (!IO.outputting())
+      AuxSym.reset(new XCOFFYAML::CsectAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::CsectAuxEnt>(AuxSym.get()), Is64);
     break;
   case XCOFFYAML::AUX_SECT:
-    AuxSym.reset(new XCOFFYAML::SectAuxEntForDWARF());
+    if (!IO.outputting())
+      AuxSym.reset(new XCOFFYAML::SectAuxEntForDWARF());
     auxSymMapping(IO, *cast<XCOFFYAML::SectAuxEntForDWARF>(AuxSym.get()));
     break;
   case XCOFFYAML::AUX_STAT:
     if (Is64)
       IO.setError(
           "an auxiliary symbol of type AUX_STAT cannot be defined in XCOFF64");
-    AuxSym.reset(new XCOFFYAML::SectAuxEntForStat());
+    if (!IO.outputting())
+      AuxSym.reset(new XCOFFYAML::SectAuxEntForStat());
     auxSymMapping(IO, *cast<XCOFFYAML::SectAuxEntForStat>(AuxSym.get()));
     break;
   }
@@ -334,8 +342,7 @@ void MappingTraits<XCOFFYAML::Symbol>::mapping(IO &IO, XCOFFYAML::Symbol &S) {
   IO.mapOptional("Type", S.Type);
   IO.mapOptional("StorageClass", S.StorageClass);
   IO.mapOptional("NumberOfAuxEntries", S.NumberOfAuxEntries);
-  if (!IO.outputting())
-    IO.mapOptional("AuxEntries", S.AuxEntries);
+  IO.mapOptional("AuxEntries", S.AuxEntries);
 }
 
 void MappingTraits<XCOFFYAML::StringTable>::mapping(IO &IO, XCOFFYAML::StringTable &Str) {
diff --git a/llvm/test/tools/obj2yaml/XCOFF/aix.yaml b/llvm/test/tools/obj2yaml/XCOFF/aix.yaml
index cd1e88dec11d2..fbd5fa0629d10 100644
--- a/llvm/test/tools/obj2yaml/XCOFF/aix.yaml
+++ b/llvm/test/tools/obj2yaml/XCOFF/aix.yaml
@@ -52,12 +52,30 @@
 # CHECK32-NEXT:     Type:            0x0
 # CHECK32-NEXT:     StorageClass:    C_EXT
 # CHECK32-NEXT:     NumberOfAuxEntries: 1
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_CSECT
+# CHECK32-NEXT:         ParameterHashIndex: 0
+# CHECK32-NEXT:         TypeChkSectNum:  0
+# CHECK32-NEXT:         SymbolAlignmentAndType: 0
+# CHECK32-NEXT:         StorageMappingClass: XMC_PR
+# CHECK32-NEXT:         SectionOrLength: 0
+# CHECK32-NEXT:         StabInfoIndex:   0
+# CHECK32-NEXT:         StabSectNum:     0
 # CHECK32-NEXT:   - Name:            .data
 # CHECK32-NEXT:     Value:           0x70
 # CHECK32-NEXT:     Section:         .data
 # CHECK32-NEXT:     Type:            0x0
 # CHECK32-NEXT:     StorageClass:    C_HIDEXT
 # CHECK32-NEXT:     NumberOfAuxEntries: 1
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_CSECT
+# CHECK32-NEXT:         ParameterHashIndex: 0
+# CHECK32-NEXT:         TypeChkSectNum:  0
+# CHECK32-NEXT:         SymbolAlignmentAndType: 0
+# CHECK32-NEXT:         StorageMappingClass: XMC_PR
+# CHECK32-NEXT:         SectionOrLength: 0
+# CHECK32-NEXT:         StabInfoIndex:   0
+# CHECK32-NEXT:         StabSectNum:     0
 
 # CHECK64:      --- !XCOFF
 # CHECK64-NEXT: FileHeader:
@@ -106,12 +124,28 @@
 # CHECK64-NEXT:     Type:            0x0
 # CHECK64-NEXT:     StorageClass:    C_EXT
 # CHECK64-NEXT:     NumberOfAuxEntries: 1
+# CHECK64-NEXT:     AuxEntries:
+# CHECK64-NEXT:       - Type:            AUX_CSECT
+# CHECK64-NEXT:         ParameterHashIndex: 0
+# CHECK64-NEXT:         TypeChkSectNum:  0
+# CHECK64-NEXT:         SymbolAlignmentAndType: 0
+# CHECK64-NEXT:         StorageMappingClass: XMC_PR
+# CHECK64-NEXT:         SectionOrLengthLo: 0
+# CHECK64-NEXT:         SectionOrLengthHi: 0
 # CHECK64-NEXT:   - Name:            .data
 # CHECK64-NEXT:     Value:           0x70
 # CHECK64-NEXT:     Section:         .data
 # CHECK64-NEXT:     Type:            0x0
 # CHECK64-NEXT:     StorageClass:    C_HIDEXT
 # CHECK64-NEXT:     NumberOfAuxEntries: 1
+# CHECK64-NEXT:     AuxEntries:
+# CHECK64-NEXT:       - Type:            AUX_CSECT
+# CHECK64-NEXT:         ParameterHashIndex: 0
+# CHECK64-NEXT:         TypeChkSectNum:  0
+# CHECK64-NEXT:         SymbolAlignmentAndType: 0
+# CHECK64-NEXT:         StorageMappingClass: XMC_PR
+# CHECK64-NEXT:         SectionOrLengthLo: 0
+# CHECK64-NEXT:         SectionOrLengthHi: 0
 
 --- !XCOFF
 FileHeader:
@@ -140,9 +174,13 @@ Symbols:
     Type:               0x0
     StorageClass:       C_EXT
     NumberOfAuxEntries: 1
+    AuxEntries:
+       - Type:          AUX_CSECT
   - Name:               .data
     Value:              0x70
     Section:            .data
     Type:               0x0
     StorageClass:       C_HIDEXT
     NumberOfAuxEntries: 1
+    AuxEntries:
+       - Type:          AUX_CSECT
diff --git a/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml b/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
new file mode 100644
index 0000000000000..a4d1cb1d33aed
--- /dev/null
+++ b/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
@@ -0,0 +1,264 @@
+## Check that obj2yaml can parse auxiliary symbols for XCOFF object file correctly.
+
+## 32-bit
+# RUN: yaml2obj %s --docnum=1 -o %t32
+# RUN: obj2yaml %t32 | FileCheck %s --check-prefix=CHECK32
+
+# CHECK32:      --- !XCOFF
+# CHECK32-NEXT: FileHeader:
+# CHECK32-NEXT:   MagicNumber:     0x1DF
+# CHECK32-NEXT:   NumberOfSections: 0
+# CHECK32-NEXT:   CreationTime:    0
+# CHECK32-NEXT:   OffsetToSymbolTable: 0x14
+# CHECK32-NEXT:   EntriesInSymbolTable: 13
+# CHECK32-NEXT:   AuxiliaryHeaderSize: 0
+# CHECK32-NEXT:   Flags:           0x0
+# CHECK32-NEXT: Symbols:
+# CHECK32-NEXT:   - Name:            ''
+# CHECK32-NEXT:     Value:           0x0
+# CHECK32-NEXT:     Section:         N_UNDEF
+# CHECK32-NEXT:     Type:            0x0
+# CHECK32-NEXT:     StorageClass:    C_FILE
+# CHECK32-NEXT:     NumberOfAuxEntries: 1
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_FILE
+# CHECK32-NEXT:         FileNameOrString: FileName
+# CHECK32-NEXT:         FileStringType:  XFT_CD
+# CHECK32-NEXT:   - Name:            ''
+# CHECK32-NEXT:     Value:           0x0
+# CHECK32-NEXT:     Section:         N_UNDEF
+# CHECK32-NEXT:     Type:            0x0
+# CHECK32-NEXT:     StorageClass:    C_HIDEXT
+# CHECK32-NEXT:     NumberOfAuxEntries: 1
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_CSECT
+# CHECK32-NEXT:         ParameterHashIndex: 1
+# CHECK32-NEXT:         TypeChkSectNum:  2
+# CHECK32-NEXT:         SymbolAlignmentAndType: 41
+# CHECK32-NEXT:         StorageMappingClass: XMC_PR
+# CHECK32-NEXT:         SectionOrLength: 3
+# CHECK32-NEXT:         StabInfoIndex:   4
+# CHECK32-NEXT:         StabSectNum:     5
+# CHECK32-NEXT:   - Name:            ''
+# CHECK32-NEXT:     Value:           0x0
+# CHECK32-NEXT:     Section:         N_UNDEF
+# CHECK32-NEXT:     Type:            0x0
+# CHECK32-NEXT:     StorageClass:    C_EXT
+# CHECK32-NEXT:     NumberOfAuxEntries: 2
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_FCN
+# CHECK32-NEXT:         OffsetToExceptionTbl: 1
+# CHECK32-NEXT:         SizeOfFunction:  2
+# CHECK32-NEXT:         SymIdxOfNextBeyond: 3
+# CHECK32-NEXT:         PtrToLineNum:    4
+# CHECK32-NEXT:       - Type:            AUX_CSECT
+# CHECK32-NEXT:         ParameterHashIndex: 1
+# CHECK32-NEXT:         TypeChkSectNum:  2
+# CHECK32-NEXT:         SymbolAlignmentAndType: 17
+# CHECK32-NEXT:         StorageMappingClass: XMC_PR
+# CHECK32-NEXT:         SectionOrLength: 4
+# CHECK32-NEXT:         StabInfoIndex:   5
+# CHECK32-NEXT:         StabSectNum:     6
+# CHECK32-NEXT:   - Name:            ''
+# CHECK32-NEXT:     Value:           0x0
+# CHECK32-NEXT:     Section:         N_UNDEF
+# CHECK32-NEXT:     Type:            0x0
+# CHECK32-NEXT:     StorageClass:    C_DWARF
+# CHECK32-NEXT:     NumberOfAuxEntries: 1
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_SECT
+# CHECK32-NEXT:         LengthOfSectionPortion: 44
+# CHECK32-NEXT:         NumberOfRelocEnt: 1
+# CHECK32-NEXT:   - Name:            ''
+# CHECK32-NEXT:     Value:           0x0
+# CHECK32-NEXT:     Section:         N_UNDEF
+# CHECK32-NEXT:     Type:            0x0
+# CHECK32-NEXT:     StorageClass:    C_STAT
+# CHECK32-NEXT:     NumberOfAuxEntries: 1
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_STAT
+# CHECK32-NEXT:         SectionLength:   1
+# CHECK32-NEXT:         NumberOfRelocEnt: 2
+# CHECK32-NEXT:         NumberOfLineNum: 3
+# CHECK32-NEXT:   - Name:            ''
+# CHECK32-NEXT:     Value:           0x0
+# CHECK32-NEXT:     Section:         N_UNDEF
+# CHECK32-NEXT:     Type:            0x0
+# CHECK32-NEXT:     StorageClass:    C_BLOCK
+# CHECK32-NEXT:     NumberOfAuxEntries: 1
+# CHECK32-NEXT:     AuxEntries:
+# CHECK32-NEXT:       - Type:            AUX_SYM
+# CHECK32-NEXT:         LineNumHi:       1
+# CHECK32-NEXT:         LineNumLo:       2
+
+--- !XCOFF
+FileHeader:
+  MagicNumber: 0x01DF
+Symbols:
+  - StorageClass:    C_FILE
+    AuxEntries:
+      - Type:             AUX_FILE
+        FileNameOrString: FileName
+        FileStringType:   XFT_CD
+  - StorageClass:    C_HIDEXT
+    AuxEntries:
+      - Type:               AUX_CSECT
+        ParameterHashIndex: 1
+        TypeChkSectNum:     2
+        SymbolAlignmentAndType: 41
+        SectionOrLength:    3
+        StabInfoIndex:      4
+        StabSectNum:        5
+  - StorageClass:    C_EXT
+    AuxEntries:
+      - Type:                 AUX_FCN
+        OffsetToExceptionTbl: 1
+        SizeOfFunction:       2
+        SymIdxOfNextBeyond:   3
+        PtrToLineNum:         4
+      - Type:               AUX_CSECT
+        ParameterHashIndex: 1
+        TypeChkSectNum:     2
+        SymbolAlignmentAndType: 17
+        SectionOrLength:    4
+        StabInfoIndex:      5
+        StabSectNum:        6
+  - StorageClass:    C_DWARF
+    AuxEntries:
+      - Type:                   AUX_SECT
+        LengthOfSectionPortion: 44
+        NumberOfRelocEnt:       1
+  - StorageClass:    C_STAT
+    AuxEntries:
+      - Type:             AUX_STAT
+        SectionLength:    1
+        NumberOfRelocEnt: 2
+        NumberOfLineNum:  3
+  - StorageClass:    C_BLOCK
+    AuxEntries:
+      - Type:            AUX_SYM
+        LineNumHi:       1
+        LineNumLo:       2
+
+## 64-bit
+# RUN: yaml2obj %s --docnum=2 -o %t64
+# RUN: obj2yaml %t64 | FileCheck %s --check-prefix=CHECK64
+
+# CHECK64:      --- !XCOFF
+# CHECK64-NEXT: FileHeader:
+# CHECK64-NEXT:   MagicNumber:     0x1F7
+# CHECK64-NEXT:   NumberOfSections: 0
+# CHECK64-NEXT:   CreationTime:    0
+# CHECK64-NEXT:   OffsetToSymbolTable: 0x18
+# CHECK64-NEXT:   EntriesInSymbolTable: 12
+# CHECK64-NEXT:   AuxiliaryHeaderSize: 0
+# CHECK64-NEXT:   Flags:           0x0
+# CHECK64-NEXT: Symbols:
+# CHECK64-NEXT:   - Name:            ''
+# CHECK64-NEXT:     Value:           0x0
+# CHECK64-NEXT:     Section:         N_UNDEF
+# CHECK64-NEXT:     Type:            0x0
+# CHECK64-NEXT:     StorageClass:    C_FILE
+# CHECK64-NEXT:     NumberOfAuxEntries: 1
+# CHECK64-NEXT:     AuxEntries:
+# CHECK64-NEXT:       - Type:            AUX_FILE
+# CHECK64-NEXT:         FileNameOrString: FileName
+# CHECK64-NEXT:         FileStringType:  XFT_CD
+# CHECK64-NEXT:   - Name:            ''
+# CHECK64-NEXT:     Value:           0x0
+# CHECK64-NEXT:     Section:         N_UNDEF
+# CHECK64-NEXT:     Type:            0x0
+# CHECK64-NEXT:     StorageClass:    C_HIDEXT
+# CHECK64-NEXT:     NumberOfAuxEntries: 1
+# CHECK64-NEXT:     AuxEntries:
+# CHECK64-NEXT:       - Type:            AUX_CSECT
+# CHECK64-NEXT:         ParameterHashIndex: 1
+# CHECK64-NEXT:         TypeChkSectNum:  2
+# CHECK64-NEXT:         SymbolAlignmentAndType: 41
+# CHECK64-NEXT:         StorageMappingClass: XMC_PR
+# CHECK64-NEXT:         SectionOrLengthLo: 3
+# CHECK64-NEXT:         SectionOrLengthHi: 4
+# CHECK64-NEXT:   - Name:            ''
+# CHECK64-NEXT:     Value:           0x0
+# CHECK64-NEXT:     Section:         N_UNDEF
+# CHECK64-NEXT:     Type:            0x0
+# CHECK64-NEXT:     StorageClass:    C_EXT
+# CHECK64-NEXT:     NumberOfAuxEntries: 3
+# CHECK64-NEXT:     AuxEntries:
+# CHECK64-NEXT:       - Type:            AUX_FCN
+# CHECK64-NEXT:         SizeOfFunction:  3
+# CHECK64-NEXT:         SymIdxOfNextBeyond: 2
+# CHECK64-NEXT:         PtrToLineNum:    1
+# CHECK64-NEXT:       - Type:            AUX_EXCEPT
+# CHECK64-NEXT:         OffsetToExceptionTbl: 1
+# CHECK64-NEXT:         SizeOfFunction:  2
+# CHECK64-NEXT:         SymIdxOfNextBeyond: 3
+# CHECK64-NEXT:       - Type:            AUX_CSECT
+# CHECK64-NEXT:         ParameterHashIndex: 1
+# CHECK64-NEXT:         TypeChkSectNum:  2
+# CHECK64-NEXT:         SymbolAlignmentAndType: 17
+# CHECK64-NEXT:         StorageMappingClass: XMC_PR
+# CHECK64-NEXT:         SectionOrLengthLo: 3
+# CHECK64-NEXT:         SectionOrLengthHi: 4
+# CHECK64-NEXT:   - Name:            ''
+# CHECK64-NEXT:     Value:           0x0
+# CHECK64-NEXT:     Section:         N_UNDEF
+# CHECK64-NEXT:     Type:            0x0
+# CHECK64-NEXT:     StorageClass:    C_DWARF
+# CHECK64-NEXT:     NumberOfAuxEntries: 1
+# CHECK64-NEXT:     AuxEntries:
+# CHECK64-NEXT:       - Type:            AUX_SECT
+# CHECK64-NEXT:         LengthOfSectionPortion: 44
+# CHECK64-NEXT:         NumberOfRelocEnt: 1
+# CHECK64-NEXT:   - Name:            ''
+# CHECK64-NEXT:     Value:           0x0
+# CHECK64-NEXT:     Section:         N_UNDEF
+# CHECK64-NEXT:     Type:            0x0
+# CHECK64-NEXT:     StorageClass:    C_BLOCK
+# CHECK64-NEXT:     NumberOfAuxEntries: 1
+# CHECK64-NEXT:     AuxEntries:
+# CHECK64-NEXT:       - Type:            AUX_SYM
+# CHECK64-NEXT:         LineNum:         1
+
+--- !XCOFF
+FileHeader:
+  MagicNumber:     0x1F7
+Symbols:
+  - StorageClass:    C_FILE
+    AuxEntries:
+      - Type:             AUX_FILE
+        FileNameOrString: FileName
+        FileStringType:   XFT_CD
+  - StorageClass:    C_HIDEXT
+    AuxEntries:
+      - Type:               AUX_CSECT
+        ParameterHashIndex: 1
+        TypeChkSectNum:     2
+        SymbolAlignmentAndType: 41
+        SectionOrLengthLo:  3
+        SectionOrLengthHi:  4
+  - StorageClass:    C_EXT
+    AuxEntries:
+      - Type:               AUX_FCN
+        SizeOfFunction:     3
+        SymIdxOfNextBeyond: 2
+        PtrToLineNum:       1
+      - Type:                 AUX_EXCEPT
+        OffsetToExceptionTbl: 1
+        SizeOfFunction:       2
+        SymIdxOfNextBeyond:   3
+      - Type:               AUX_CSECT
+        ParameterHashIndex: 1
+        TypeChkSectNum:     2
+        SymbolAlignmentAndType: 17
+        SectionOrLengthLo:  3
+        SectionOrLengthHi:  4
+  - StorageClass:    C_DWARF
+    AuxEntries:
+      - Type:                   AUX_SECT
+        LengthOfSectionPortion: 44
+        NumberOfRelocEnt:       1
+  - StorageClass:    C_BLOCK
+    AuxEntries:
+      - Type:            AUX_SYM
+        LineNum:         1
diff --git a/llvm/tools/obj2yaml/xcoff2yaml.cpp b/llvm/tools/obj2yaml/xcoff2yaml.cpp
index 882c410496012..1960513c611e8 100644
--- a/llvm/tools/obj2yaml/xcoff2yaml.cpp
+++ b/llvm/tools/obj2yaml/xcoff2yaml.cpp
@@ -29,6 +29,11 @@ class XCOFFDumper {
   XCOFFDumper(const object::XCOFFObjectFile &obj) : Obj(obj) {}
   Error dump();
   XCOFFYAML::Object &getYAMLObj() { return YAMLObj; }
+
+  template <typename T> const T *getAuxEntPtr(uintptr_t AuxAddress) {
+    Obj.checkSymbolEntryPointer(AuxAddress);
+    return reinterpret_cast<const T *>(AuxAddress);
+  }
 };
 } // namespace
 
@@ -106,6 +111,70 @@ Error XCOFFDumper::dumpSections(ArrayRef<Shdr> Sections) {
   return Error::success();
 }
 
+static void
+dumpStatAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
+               const object::XCOFFSectAuxEntForStat *AuxEntPtr) {
+  XCOFFYAML::SectAuxEntForStat StatAuxSym;
+  StatAuxSym.SectionLength = AuxEntPtr->SectionLength;
+  StatAuxSym.NumberOfLineNum = AuxEntPtr->NumberOfLineNum;
+  StatAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
+  AuxEntTbl->push_back(
+      std::make_unique<XCOFFYAML::SectAuxEntForStat>(StatAuxSym));
+}
+
+static void
+dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
+              const object::XCOFFFunctionAuxEnt32 *AuxEntPtr) {
+  XCOFFYAML::FunctionAuxEnt FunAuxSym;
+  FunAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
+  FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
+  FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
+  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
+  AuxEntTbl->push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
+}
+
+static void
+dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
+              const object::XCOFFFunctionAuxEnt64 *AuxEntPtr) {
+  XCOFFYAML::FunctionAuxEnt FunAuxSym;
+  FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
+  FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
+  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
+  AuxEntTbl->push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
+}
+
+static void
+dumpExpAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
+              const object::XCOFFExceptionAuxEnt *AuxEntPtr) {
+  XCOFFYAML::ExcpetionAuxEnt ExceptAuxSym;
+  ExceptAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
+  ExceptAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
+  ExceptAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
+  AuxEntTbl->push_back(
+      std::make_unique<XCOFFYAML::ExcpetionAuxEnt>(ExceptAuxSym));
+}
+
+static void dumpCscetAuxSym(
+    std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
+    const object::XCOFFCsectAuxRef &AuxEntPtr, bool Is64bit) {
+  XCOFFYAML::CsectAuxEnt CsectAuxSym;
+  CsectAuxSym.ParameterHashIndex = AuxEntPtr.getParameterHashIndex();
+  CsectAuxSym.TypeChkSectNum = AuxEntPtr.getTypeChkSectNum();
+  CsectAuxSym.SymbolAlignmentAndType = AuxEntPtr.getSymbolAlignmentAndType();
+  CsectAuxSym.StorageMappingClass = AuxEntPtr.getStorageMappingClass();
+  if (Is64bit) {
+    CsectAuxSym.SectionOrLengthLo =
+        static_cast<uint32_t>(AuxEntPtr.getSectionOrLength64());
+    CsectAuxSym.SectionOrLengthHi =
+        static_cast<uint32_t>(AuxEntPtr.getSectionOrLength64() >> 32);
+  } else {
+    CsectAuxSym.SectionOrLength = AuxEntPtr.getSectionOrLength32();
+    CsectAuxSym.StabInfoIndex = AuxEntPtr.getStabInfoIndex32();
+    CsectAuxSym.StabSectNum = AuxEntPtr.getStabSectNum32();
+  }
+  AuxEntTbl->push_back(std::make_unique<XCOFFYAML::CsectAuxEnt>(CsectAuxSym));
+}
+
 Error XCOFFDumper::dumpSymbols() {
   std::vector<XCOFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
 
@@ -131,7 +200,121 @@ Error XCOFFDumper::dumpSymbols() {
 
     Sym.Type = SymbolEntRef.getSymbolType();
     Sym.StorageClass = SymbolEntRef.getStorageClass();
-    Sym.NumberOfAuxEntries = SymbolEntRef.getNumberOfAuxEntries();
+    uint8_t NumOfAuxSym = SymbolEntRef.getNumberOfAuxEntries();
+    Sym.NumberOfAuxEntries = NumOfAuxSym;
+
+    if (NumOfAuxSym) {
+      auto AuxEntTbl = &Sym.AuxEntries;
+      switch (Sym.StorageClass) {
+      case XCOFF::C_FILE: {
+        for (int I = 1; I <= NumOfAuxSym; I++) {
+          uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+              SymbolEntRef.getEntryAddress(), I);
+          const XCOFFFileAuxEnt *FileAuxEntPtr =
+              getAuxEntPtr<XCOFFFileAuxEnt>(AuxAddress);
+          auto FileNameOrError = Obj.getCFileName(FileAuxEntPtr);
+          if (!FileNameOrError)
+            return FileNameOrError.takeError();
+          XCOFFYAML::FileAuxEnt FileAuxSym;
+          FileAuxSym.FileNameOrString = FileNameOrError.get();
+          FileAuxSym.FileStringType = FileAuxEntPtr->Type;
+          AuxEntTbl->push_back(
+              std::make_unique<XCOFFYAML::FileAuxEnt>(FileAuxSym));
+        }
+        break;
+      }
+      case XCOFF::C_STAT: {
+        assert(NumOfAuxSym == 1 && "expected a single aux symbol for C_STAT!");
+        const XCOFFSectAuxEntForStat *AuxEntPtr =
+            getAuxEntPtr<XCOFFSectAuxEntForStat>(
+                XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+                    SymbolEntRef.getEntryAddress(), 1));
+        dumpStatAuxSym(AuxEntTbl, AuxEntPtr);
+        break;
+      }
+      case XCOFF::C_EXT:
+      case XCOFF::C_WEAKEXT:
+      case XCOFF::C_HIDEXT: {
+        for (int I = 1; I <= NumOfAuxSym; I++) {
+          if (I == NumOfAuxSym && !Obj.is64Bit())
+            break;
+
+          uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+              SymbolEntRef.getEntryAddress(), I);
+          if (!Obj.is64Bit()) {
+            const XCOFFFunctionAuxEnt32 *AuxEntPtr =
+                getAuxEntPtr<XCOFFFunctionAuxEnt32>(AuxAddress);
+            dumpFunAuxSym(AuxEntTbl, AuxEntPtr);
+          } else {
+            XCOFF::SymbolAuxType Type = *Obj.getSymbolAuxType(AuxAddress);
+            if (Type == XCOFF::SymbolAuxType::AUX_CSECT)
+              continue;
+            if (Type == XCOFF::SymbolAuxType::AUX_FCN) {
+              const XCOFFFunctionAuxEnt64 *AuxEntPtr =
+                  getAuxEntPtr<XCOFFFunctionAuxEnt64>(AuxAddress);
+              dumpFunAuxSym(AuxEntTbl, AuxEntPtr);
+            } else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT) {
+              const XCOFFExceptionAuxEnt *AuxEntPtr =
+                  getAuxEntPtr<XCOFFExceptionAuxEnt>(AuxAddress);
+              dumpExpAuxSym(AuxEntTbl, AuxEntPtr);
+            } else
+              llvm_unreachable("invalid aux symbol entry");
+          }
+        }
+        auto ErrOrCsectAuxRef = SymbolEntRef.getXCOFFCsectAuxRef();
+        if (!ErrOrCsectAuxRef)
+          return ErrOrCsectAuxRef.takeError();
+        dumpCscetAuxSym(AuxEntTbl, ErrOrCsectAuxRef.get(), Obj.is64Bit());
+        break;
+      }
+      case XCOFF::C_BLOCK:
+      case XCOFF::C_FCN: {
+        assert(NumOfAuxSym == 1 &&
+               "expected a single aux symbol for C_BLOCK or C_FCN!");
+
+        uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+            SymbolEntRef.getEntryAddress(), 1);
+        XCOFFYAML::BlockAuxEnt BlockAuxSym;
+        if (Obj.is64Bit()) {
+          const XCOFFBlockAuxEnt64 *AuxEntPtr =
+              getAuxEntPtr<XCOFFBlockAuxEnt64>(AuxAddress);
+          BlockAuxSym.LineNum = AuxEntPtr->LineNum;
+        } else {
+          const XCOFFBlockAuxEnt32 *AuxEntPtr =
+              getAuxEntPtr<XCOFFBlockAuxEnt32>(AuxAddress);
+          BlockAuxSym.LineNumLo = AuxEntPtr->LineNumLo;
+          BlockAuxSym.LineNumHi = AuxEntPtr->LineNumHi;
+        }
+        AuxEntTbl->push_back(
+            std::make_unique<XCOFFYAML::BlockAuxEnt>(BlockAuxSym));
+        break;
+      }
+      case XCOFF::C_DWARF: {
+        assert(NumOfAuxSym == 1 && "expected a single aux symbol for C_DWARF!");
+        uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+            SymbolEntRef.getEntryAddress(), 1);
+        XCOFFYAML::SectAuxEntForDWARF DwarfAuxSym;
+        if (Obj.is64Bit()) {
+          const XCOFFSectAuxEntForDWARF64 *AuxEntPtr =
+              getAuxEntPtr<XCOFFSectAuxEntForDWARF64>(AuxAddress);
+          DwarfAuxSym.LengthOfSectionPortion =
+              AuxEntPtr->LengthOfSectionPortion;
+          DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
+        } else {
+          const XCOFFSectAuxEntForDWARF32 *AuxEntPtr =
+              getAuxEntPtr<XCOFFSectAuxEntForDWARF32>(AuxAddress);
+          DwarfAuxSym.LengthOfSectionPortion =
+              AuxEntPtr->LengthOfSectionPortion;
+          DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
+        }
+        AuxEntTbl->push_back(
+            std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(DwarfAuxSym));
+        break;
+      }
+      default:
+        break;
+      }
+    }
 
     Symbols.push_back(std::move(Sym));
   }

>From 724fb0187d1947c6e177abe5aa558c691446386e Mon Sep 17 00:00:00 2001
From: esmeyi <esme.yi at ibm.com>
Date: Tue, 7 Nov 2023 00:57:31 -0500
Subject: [PATCH 2/7] Address James's comments.

---
 llvm/tools/obj2yaml/xcoff2yaml.cpp | 89 ++++++++++++++++--------------
 1 file changed, 48 insertions(+), 41 deletions(-)

diff --git a/llvm/tools/obj2yaml/xcoff2yaml.cpp b/llvm/tools/obj2yaml/xcoff2yaml.cpp
index 1960513c611e8..b2799606e6db4 100644
--- a/llvm/tools/obj2yaml/xcoff2yaml.cpp
+++ b/llvm/tools/obj2yaml/xcoff2yaml.cpp
@@ -112,50 +112,59 @@ Error XCOFFDumper::dumpSections(ArrayRef<Shdr> Sections) {
 }
 
 static void
-dumpStatAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
-               const object::XCOFFSectAuxEntForStat *AuxEntPtr) {
+dumpFileAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
+               XCOFF::CFileStringType Type, StringRef FileStr) {
+  XCOFFYAML::FileAuxEnt FileAuxSym;
+  FileAuxSym.FileNameOrString = FileStr;
+  FileAuxSym.FileStringType = Type;
+  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::FileAuxEnt>(FileAuxSym));
+}
+
+static void
+dumpStatAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
+               const object::XCOFFSectAuxEntForStat &AuxEntPtr) {
   XCOFFYAML::SectAuxEntForStat StatAuxSym;
-  StatAuxSym.SectionLength = AuxEntPtr->SectionLength;
-  StatAuxSym.NumberOfLineNum = AuxEntPtr->NumberOfLineNum;
-  StatAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
-  AuxEntTbl->push_back(
+  StatAuxSym.SectionLength = AuxEntPtr.SectionLength;
+  StatAuxSym.NumberOfLineNum = AuxEntPtr.NumberOfLineNum;
+  StatAuxSym.NumberOfRelocEnt = AuxEntPtr.NumberOfRelocEnt;
+  AuxEntTbl.push_back(
       std::make_unique<XCOFFYAML::SectAuxEntForStat>(StatAuxSym));
 }
 
 static void
-dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
-              const object::XCOFFFunctionAuxEnt32 *AuxEntPtr) {
+dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
+              const object::XCOFFFunctionAuxEnt32 &AuxEntPtr) {
   XCOFFYAML::FunctionAuxEnt FunAuxSym;
-  FunAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
-  FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
-  FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
-  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
-  AuxEntTbl->push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
+  FunAuxSym.OffsetToExceptionTbl = AuxEntPtr.OffsetToExceptionTbl;
+  FunAuxSym.PtrToLineNum = AuxEntPtr.PtrToLineNum;
+  FunAuxSym.SizeOfFunction = AuxEntPtr.SizeOfFunction;
+  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr.SymIdxOfNextBeyond;
+  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
 }
 
 static void
-dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
-              const object::XCOFFFunctionAuxEnt64 *AuxEntPtr) {
+dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
+              const object::XCOFFFunctionAuxEnt64 &AuxEntPtr) {
   XCOFFYAML::FunctionAuxEnt FunAuxSym;
-  FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
-  FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
-  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
-  AuxEntTbl->push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
+  FunAuxSym.PtrToLineNum = AuxEntPtr.PtrToLineNum;
+  FunAuxSym.SizeOfFunction = AuxEntPtr.SizeOfFunction;
+  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr.SymIdxOfNextBeyond;
+  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
 }
 
 static void
-dumpExpAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
-              const object::XCOFFExceptionAuxEnt *AuxEntPtr) {
+dumpExpAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
+              const object::XCOFFExceptionAuxEnt &AuxEntPtr) {
   XCOFFYAML::ExcpetionAuxEnt ExceptAuxSym;
-  ExceptAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
-  ExceptAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
-  ExceptAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
-  AuxEntTbl->push_back(
+  ExceptAuxSym.OffsetToExceptionTbl = AuxEntPtr.OffsetToExceptionTbl;
+  ExceptAuxSym.SizeOfFunction = AuxEntPtr.SizeOfFunction;
+  ExceptAuxSym.SymIdxOfNextBeyond = AuxEntPtr.SymIdxOfNextBeyond;
+  AuxEntTbl.push_back(
       std::make_unique<XCOFFYAML::ExcpetionAuxEnt>(ExceptAuxSym));
 }
 
 static void dumpCscetAuxSym(
-    std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> *AuxEntTbl,
+    std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
     const object::XCOFFCsectAuxRef &AuxEntPtr, bool Is64bit) {
   XCOFFYAML::CsectAuxEnt CsectAuxSym;
   CsectAuxSym.ParameterHashIndex = AuxEntPtr.getParameterHashIndex();
@@ -172,7 +181,7 @@ static void dumpCscetAuxSym(
     CsectAuxSym.StabInfoIndex = AuxEntPtr.getStabInfoIndex32();
     CsectAuxSym.StabSectNum = AuxEntPtr.getStabSectNum32();
   }
-  AuxEntTbl->push_back(std::make_unique<XCOFFYAML::CsectAuxEnt>(CsectAuxSym));
+  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::CsectAuxEnt>(CsectAuxSym));
 }
 
 Error XCOFFDumper::dumpSymbols() {
@@ -204,10 +213,10 @@ Error XCOFFDumper::dumpSymbols() {
     Sym.NumberOfAuxEntries = NumOfAuxSym;
 
     if (NumOfAuxSym) {
-      auto AuxEntTbl = &Sym.AuxEntries;
+      std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> AuxEntTbl;
       switch (Sym.StorageClass) {
       case XCOFF::C_FILE: {
-        for (int I = 1; I <= NumOfAuxSym; I++) {
+        for (uint8_t I = 1; I <= NumOfAuxSym; ++I) {
           uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
               SymbolEntRef.getEntryAddress(), I);
           const XCOFFFileAuxEnt *FileAuxEntPtr =
@@ -215,11 +224,8 @@ Error XCOFFDumper::dumpSymbols() {
           auto FileNameOrError = Obj.getCFileName(FileAuxEntPtr);
           if (!FileNameOrError)
             return FileNameOrError.takeError();
-          XCOFFYAML::FileAuxEnt FileAuxSym;
-          FileAuxSym.FileNameOrString = FileNameOrError.get();
-          FileAuxSym.FileStringType = FileAuxEntPtr->Type;
-          AuxEntTbl->push_back(
-              std::make_unique<XCOFFYAML::FileAuxEnt>(FileAuxSym));
+
+          dumpFileAuxSym(AuxEntTbl, FileAuxEntPtr->Type, FileNameOrError.get());
         }
         break;
       }
@@ -229,13 +235,13 @@ Error XCOFFDumper::dumpSymbols() {
             getAuxEntPtr<XCOFFSectAuxEntForStat>(
                 XCOFFObjectFile::getAdvancedSymbolEntryAddress(
                     SymbolEntRef.getEntryAddress(), 1));
-        dumpStatAuxSym(AuxEntTbl, AuxEntPtr);
+        dumpStatAuxSym(AuxEntTbl, *AuxEntPtr);
         break;
       }
       case XCOFF::C_EXT:
       case XCOFF::C_WEAKEXT:
       case XCOFF::C_HIDEXT: {
-        for (int I = 1; I <= NumOfAuxSym; I++) {
+        for (uint8_t I = 1; I <= NumOfAuxSym; ++I) {
           if (I == NumOfAuxSym && !Obj.is64Bit())
             break;
 
@@ -244,7 +250,7 @@ Error XCOFFDumper::dumpSymbols() {
           if (!Obj.is64Bit()) {
             const XCOFFFunctionAuxEnt32 *AuxEntPtr =
                 getAuxEntPtr<XCOFFFunctionAuxEnt32>(AuxAddress);
-            dumpFunAuxSym(AuxEntTbl, AuxEntPtr);
+            dumpFunAuxSym(AuxEntTbl, *AuxEntPtr);
           } else {
             XCOFF::SymbolAuxType Type = *Obj.getSymbolAuxType(AuxAddress);
             if (Type == XCOFF::SymbolAuxType::AUX_CSECT)
@@ -252,11 +258,11 @@ Error XCOFFDumper::dumpSymbols() {
             if (Type == XCOFF::SymbolAuxType::AUX_FCN) {
               const XCOFFFunctionAuxEnt64 *AuxEntPtr =
                   getAuxEntPtr<XCOFFFunctionAuxEnt64>(AuxAddress);
-              dumpFunAuxSym(AuxEntTbl, AuxEntPtr);
+              dumpFunAuxSym(AuxEntTbl, *AuxEntPtr);
             } else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT) {
               const XCOFFExceptionAuxEnt *AuxEntPtr =
                   getAuxEntPtr<XCOFFExceptionAuxEnt>(AuxAddress);
-              dumpExpAuxSym(AuxEntTbl, AuxEntPtr);
+              dumpExpAuxSym(AuxEntTbl, *AuxEntPtr);
             } else
               llvm_unreachable("invalid aux symbol entry");
           }
@@ -285,7 +291,7 @@ Error XCOFFDumper::dumpSymbols() {
           BlockAuxSym.LineNumLo = AuxEntPtr->LineNumLo;
           BlockAuxSym.LineNumHi = AuxEntPtr->LineNumHi;
         }
-        AuxEntTbl->push_back(
+        AuxEntTbl.push_back(
             std::make_unique<XCOFFYAML::BlockAuxEnt>(BlockAuxSym));
         break;
       }
@@ -307,13 +313,14 @@ Error XCOFFDumper::dumpSymbols() {
               AuxEntPtr->LengthOfSectionPortion;
           DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
         }
-        AuxEntTbl->push_back(
+        AuxEntTbl.push_back(
             std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(DwarfAuxSym));
         break;
       }
       default:
         break;
       }
+      Sym.AuxEntries = std::move(AuxEntTbl);
     }
 
     Symbols.push_back(std::move(Sym));

>From 1f12c621748ac50eaa9e9208862a5f303dc26e37 Mon Sep 17 00:00:00 2001
From: esmeyi <esme.yi at ibm.com>
Date: Wed, 8 Nov 2023 01:11:44 -0500
Subject: [PATCH 3/7] Address James's comments; move case bodies to separate
 functions.

---
 llvm/tools/obj2yaml/xcoff2yaml.cpp | 312 ++++++++++++++++-------------
 1 file changed, 169 insertions(+), 143 deletions(-)

diff --git a/llvm/tools/obj2yaml/xcoff2yaml.cpp b/llvm/tools/obj2yaml/xcoff2yaml.cpp
index b2799606e6db4..de1bf6d5d079a 100644
--- a/llvm/tools/obj2yaml/xcoff2yaml.cpp
+++ b/llvm/tools/obj2yaml/xcoff2yaml.cpp
@@ -25,6 +25,21 @@ class XCOFFDumper {
   template <typename Shdr, typename Reloc>
   Error dumpSections(ArrayRef<Shdr> Sections);
 
+  // Dump auxiliary symbols.
+  Error dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
+                       const XCOFFSymbolRef &SymbolEntRef);
+  void dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
+                      const XCOFFSymbolRef &SymbolEntRef);
+  void dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
+                       const XCOFFSymbolRef &SymbolEntRef);
+  void dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
+                       const XCOFFSymbolRef &SymbolEntRef);
+  Error dumpAuxSyms(XCOFFYAML::Symbol &Sym, const XCOFFSymbolRef &SymbolEntRef);
+  void dumpFuncAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
+  void dumpExpAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
+  void dumpCscetAuxSym(XCOFFYAML::Symbol &Sym,
+                       const object::XCOFFCsectAuxRef &AuxEntPtr);
+
 public:
   XCOFFDumper(const object::XCOFFObjectFile &obj) : Obj(obj) {}
   Error dump();
@@ -111,67 +126,85 @@ Error XCOFFDumper::dumpSections(ArrayRef<Shdr> Sections) {
   return Error::success();
 }
 
-static void
-dumpFileAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
-               XCOFF::CFileStringType Type, StringRef FileStr) {
-  XCOFFYAML::FileAuxEnt FileAuxSym;
-  FileAuxSym.FileNameOrString = FileStr;
-  FileAuxSym.FileStringType = Type;
-  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::FileAuxEnt>(FileAuxSym));
+Error XCOFFDumper::dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
+                                  const XCOFFSymbolRef &SymbolEntRef) {
+  for (uint8_t I = 1; I <= Sym.NumberOfAuxEntries; ++I) {
+    uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+        SymbolEntRef.getEntryAddress(), I);
+    const XCOFFFileAuxEnt *FileAuxEntPtr =
+        getAuxEntPtr<XCOFFFileAuxEnt>(AuxAddress);
+    auto FileNameOrError = Obj.getCFileName(FileAuxEntPtr);
+    if (!FileNameOrError)
+      return FileNameOrError.takeError();
+
+    XCOFFYAML::FileAuxEnt FileAuxSym;
+    FileAuxSym.FileNameOrString = FileNameOrError.get();
+    FileAuxSym.FileStringType = FileAuxEntPtr->Type;
+    Sym.AuxEntries.push_back(
+        std::make_unique<XCOFFYAML::FileAuxEnt>(FileAuxSym));
+  }
+  return Error::success();
 }
 
-static void
-dumpStatAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
-               const object::XCOFFSectAuxEntForStat &AuxEntPtr) {
+void XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
+                                 const XCOFFSymbolRef &SymbolEntRef) {
+  assert(Sym.NumberOfAuxEntries == 1 &&
+         "expected a single aux symbol for C_STAT!");
+
+  const XCOFFSectAuxEntForStat *AuxEntPtr =
+      getAuxEntPtr<XCOFFSectAuxEntForStat>(
+          XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+              SymbolEntRef.getEntryAddress(), 1));
   XCOFFYAML::SectAuxEntForStat StatAuxSym;
-  StatAuxSym.SectionLength = AuxEntPtr.SectionLength;
-  StatAuxSym.NumberOfLineNum = AuxEntPtr.NumberOfLineNum;
-  StatAuxSym.NumberOfRelocEnt = AuxEntPtr.NumberOfRelocEnt;
-  AuxEntTbl.push_back(
+  StatAuxSym.SectionLength = AuxEntPtr->SectionLength;
+  StatAuxSym.NumberOfLineNum = AuxEntPtr->NumberOfLineNum;
+  StatAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
+  Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::SectAuxEntForStat>(StatAuxSym));
 }
 
-static void
-dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
-              const object::XCOFFFunctionAuxEnt32 &AuxEntPtr) {
+void XCOFFDumper::dumpFuncAuxSym(XCOFFYAML::Symbol &Sym,
+                                 const uintptr_t AuxAddress) {
   XCOFFYAML::FunctionAuxEnt FunAuxSym;
-  FunAuxSym.OffsetToExceptionTbl = AuxEntPtr.OffsetToExceptionTbl;
-  FunAuxSym.PtrToLineNum = AuxEntPtr.PtrToLineNum;
-  FunAuxSym.SizeOfFunction = AuxEntPtr.SizeOfFunction;
-  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr.SymIdxOfNextBeyond;
-  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
-}
-
-static void
-dumpFunAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
-              const object::XCOFFFunctionAuxEnt64 &AuxEntPtr) {
-  XCOFFYAML::FunctionAuxEnt FunAuxSym;
-  FunAuxSym.PtrToLineNum = AuxEntPtr.PtrToLineNum;
-  FunAuxSym.SizeOfFunction = AuxEntPtr.SizeOfFunction;
-  FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr.SymIdxOfNextBeyond;
-  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
+  if (Obj.is64Bit()) {
+    const XCOFFFunctionAuxEnt64 *AuxEntPtr =
+        getAuxEntPtr<XCOFFFunctionAuxEnt64>(AuxAddress);
+    FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
+    FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
+    FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
+  } else {
+    const XCOFFFunctionAuxEnt32 *AuxEntPtr =
+        getAuxEntPtr<XCOFFFunctionAuxEnt32>(AuxAddress);
+    FunAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
+    FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
+    FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
+    FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
+  }
+  Sym.AuxEntries.push_back(
+      std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
 }
 
-static void
-dumpExpAuxSym(std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
-              const object::XCOFFExceptionAuxEnt &AuxEntPtr) {
+void XCOFFDumper::dumpExpAuxSym(XCOFFYAML::Symbol &Sym,
+                                const uintptr_t AuxAddress) {
+  const XCOFFExceptionAuxEnt *AuxEntPtr =
+      getAuxEntPtr<XCOFFExceptionAuxEnt>(AuxAddress);
   XCOFFYAML::ExcpetionAuxEnt ExceptAuxSym;
-  ExceptAuxSym.OffsetToExceptionTbl = AuxEntPtr.OffsetToExceptionTbl;
-  ExceptAuxSym.SizeOfFunction = AuxEntPtr.SizeOfFunction;
-  ExceptAuxSym.SymIdxOfNextBeyond = AuxEntPtr.SymIdxOfNextBeyond;
-  AuxEntTbl.push_back(
+  ExceptAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
+  ExceptAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
+  ExceptAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
+  Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::ExcpetionAuxEnt>(ExceptAuxSym));
 }
 
-static void dumpCscetAuxSym(
-    std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> &AuxEntTbl,
-    const object::XCOFFCsectAuxRef &AuxEntPtr, bool Is64bit) {
+void XCOFFDumper::dumpCscetAuxSym(XCOFFYAML::Symbol &Sym,
+                                  const object::XCOFFCsectAuxRef &AuxEntPtr) {
   XCOFFYAML::CsectAuxEnt CsectAuxSym;
   CsectAuxSym.ParameterHashIndex = AuxEntPtr.getParameterHashIndex();
   CsectAuxSym.TypeChkSectNum = AuxEntPtr.getTypeChkSectNum();
   CsectAuxSym.SymbolAlignmentAndType = AuxEntPtr.getSymbolAlignmentAndType();
   CsectAuxSym.StorageMappingClass = AuxEntPtr.getStorageMappingClass();
-  if (Is64bit) {
+
+  if (Obj.is64Bit()) {
     CsectAuxSym.SectionOrLengthLo =
         static_cast<uint32_t>(AuxEntPtr.getSectionOrLength64());
     CsectAuxSym.SectionOrLengthHi =
@@ -181,7 +214,85 @@ static void dumpCscetAuxSym(
     CsectAuxSym.StabInfoIndex = AuxEntPtr.getStabInfoIndex32();
     CsectAuxSym.StabSectNum = AuxEntPtr.getStabSectNum32();
   }
-  AuxEntTbl.push_back(std::make_unique<XCOFFYAML::CsectAuxEnt>(CsectAuxSym));
+  Sym.AuxEntries.push_back(
+      std::make_unique<XCOFFYAML::CsectAuxEnt>(CsectAuxSym));
+}
+
+Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
+                               const XCOFFSymbolRef &SymbolEntRef) {
+  for (uint8_t I = 1; I <= Sym.NumberOfAuxEntries; ++I) {
+    if (I == Sym.NumberOfAuxEntries && !Obj.is64Bit())
+      break;
+
+    uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+        SymbolEntRef.getEntryAddress(), I);
+
+    if (Obj.is64Bit()) {
+      XCOFF::SymbolAuxType Type = *Obj.getSymbolAuxType(AuxAddress);
+      if (Type == XCOFF::SymbolAuxType::AUX_CSECT)
+        continue;
+      if (Type == XCOFF::SymbolAuxType::AUX_FCN)
+        dumpFuncAuxSym(Sym, AuxAddress);
+      else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT)
+        dumpExpAuxSym(Sym, AuxAddress);
+      else
+        llvm_unreachable("invalid aux symbol entry");
+    } else
+      dumpFuncAuxSym(Sym, AuxAddress);
+  }
+
+  auto ErrOrCsectAuxRef = SymbolEntRef.getXCOFFCsectAuxRef();
+  if (!ErrOrCsectAuxRef)
+    return ErrOrCsectAuxRef.takeError();
+
+  dumpCscetAuxSym(Sym, ErrOrCsectAuxRef.get());
+  return Error::success();
+}
+
+void XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
+                                  const XCOFFSymbolRef &SymbolEntRef) {
+  assert(Sym.NumberOfAuxEntries == 1 &&
+         "expected a single aux symbol for C_BLOCK or C_FCN!");
+
+  uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+      SymbolEntRef.getEntryAddress(), 1);
+  XCOFFYAML::BlockAuxEnt BlockAuxSym;
+
+  if (Obj.is64Bit()) {
+    const XCOFFBlockAuxEnt64 *AuxEntPtr =
+        getAuxEntPtr<XCOFFBlockAuxEnt64>(AuxAddress);
+    BlockAuxSym.LineNum = AuxEntPtr->LineNum;
+  } else {
+    const XCOFFBlockAuxEnt32 *AuxEntPtr =
+        getAuxEntPtr<XCOFFBlockAuxEnt32>(AuxAddress);
+    BlockAuxSym.LineNumLo = AuxEntPtr->LineNumLo;
+    BlockAuxSym.LineNumHi = AuxEntPtr->LineNumHi;
+  }
+
+  Sym.AuxEntries.push_back(
+      std::make_unique<XCOFFYAML::BlockAuxEnt>(BlockAuxSym));
+}
+
+void XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
+                                  const XCOFFSymbolRef &SymbolEntRef) {
+  assert(Sym.NumberOfAuxEntries == 1 &&
+         "expected a single aux symbol for C_DWARF!");
+  uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
+      SymbolEntRef.getEntryAddress(), 1);
+  XCOFFYAML::SectAuxEntForDWARF DwarfAuxSym;
+  if (Obj.is64Bit()) {
+    const XCOFFSectAuxEntForDWARF64 *AuxEntPtr =
+        getAuxEntPtr<XCOFFSectAuxEntForDWARF64>(AuxAddress);
+    DwarfAuxSym.LengthOfSectionPortion = AuxEntPtr->LengthOfSectionPortion;
+    DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
+  } else {
+    const XCOFFSectAuxEntForDWARF32 *AuxEntPtr =
+        getAuxEntPtr<XCOFFSectAuxEntForDWARF32>(AuxAddress);
+    DwarfAuxSym.LengthOfSectionPortion = AuxEntPtr->LengthOfSectionPortion;
+    DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
+  }
+  Sym.AuxEntries.push_back(
+      std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(DwarfAuxSym));
 }
 
 Error XCOFFDumper::dumpSymbols() {
@@ -209,118 +320,33 @@ Error XCOFFDumper::dumpSymbols() {
 
     Sym.Type = SymbolEntRef.getSymbolType();
     Sym.StorageClass = SymbolEntRef.getStorageClass();
-    uint8_t NumOfAuxSym = SymbolEntRef.getNumberOfAuxEntries();
-    Sym.NumberOfAuxEntries = NumOfAuxSym;
+    Sym.NumberOfAuxEntries = SymbolEntRef.getNumberOfAuxEntries();
 
-    if (NumOfAuxSym) {
-      std::vector<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>> AuxEntTbl;
+    if (Sym.NumberOfAuxEntries) {
       switch (Sym.StorageClass) {
-      case XCOFF::C_FILE: {
-        for (uint8_t I = 1; I <= NumOfAuxSym; ++I) {
-          uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
-              SymbolEntRef.getEntryAddress(), I);
-          const XCOFFFileAuxEnt *FileAuxEntPtr =
-              getAuxEntPtr<XCOFFFileAuxEnt>(AuxAddress);
-          auto FileNameOrError = Obj.getCFileName(FileAuxEntPtr);
-          if (!FileNameOrError)
-            return FileNameOrError.takeError();
-
-          dumpFileAuxSym(AuxEntTbl, FileAuxEntPtr->Type, FileNameOrError.get());
-        }
+      case XCOFF::C_FILE:
+        if (Error E = dumpFileAuxSym(Sym, SymbolEntRef))
+          return E;
         break;
-      }
-      case XCOFF::C_STAT: {
-        assert(NumOfAuxSym == 1 && "expected a single aux symbol for C_STAT!");
-        const XCOFFSectAuxEntForStat *AuxEntPtr =
-            getAuxEntPtr<XCOFFSectAuxEntForStat>(
-                XCOFFObjectFile::getAdvancedSymbolEntryAddress(
-                    SymbolEntRef.getEntryAddress(), 1));
-        dumpStatAuxSym(AuxEntTbl, *AuxEntPtr);
+      case XCOFF::C_STAT:
+        dumpStatAuxSym(Sym, SymbolEntRef);
         break;
-      }
       case XCOFF::C_EXT:
       case XCOFF::C_WEAKEXT:
-      case XCOFF::C_HIDEXT: {
-        for (uint8_t I = 1; I <= NumOfAuxSym; ++I) {
-          if (I == NumOfAuxSym && !Obj.is64Bit())
-            break;
-
-          uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
-              SymbolEntRef.getEntryAddress(), I);
-          if (!Obj.is64Bit()) {
-            const XCOFFFunctionAuxEnt32 *AuxEntPtr =
-                getAuxEntPtr<XCOFFFunctionAuxEnt32>(AuxAddress);
-            dumpFunAuxSym(AuxEntTbl, *AuxEntPtr);
-          } else {
-            XCOFF::SymbolAuxType Type = *Obj.getSymbolAuxType(AuxAddress);
-            if (Type == XCOFF::SymbolAuxType::AUX_CSECT)
-              continue;
-            if (Type == XCOFF::SymbolAuxType::AUX_FCN) {
-              const XCOFFFunctionAuxEnt64 *AuxEntPtr =
-                  getAuxEntPtr<XCOFFFunctionAuxEnt64>(AuxAddress);
-              dumpFunAuxSym(AuxEntTbl, *AuxEntPtr);
-            } else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT) {
-              const XCOFFExceptionAuxEnt *AuxEntPtr =
-                  getAuxEntPtr<XCOFFExceptionAuxEnt>(AuxAddress);
-              dumpExpAuxSym(AuxEntTbl, *AuxEntPtr);
-            } else
-              llvm_unreachable("invalid aux symbol entry");
-          }
-        }
-        auto ErrOrCsectAuxRef = SymbolEntRef.getXCOFFCsectAuxRef();
-        if (!ErrOrCsectAuxRef)
-          return ErrOrCsectAuxRef.takeError();
-        dumpCscetAuxSym(AuxEntTbl, ErrOrCsectAuxRef.get(), Obj.is64Bit());
+      case XCOFF::C_HIDEXT:
+        if (Error E = dumpAuxSyms(Sym, SymbolEntRef))
+          return E;
         break;
-      }
       case XCOFF::C_BLOCK:
-      case XCOFF::C_FCN: {
-        assert(NumOfAuxSym == 1 &&
-               "expected a single aux symbol for C_BLOCK or C_FCN!");
-
-        uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
-            SymbolEntRef.getEntryAddress(), 1);
-        XCOFFYAML::BlockAuxEnt BlockAuxSym;
-        if (Obj.is64Bit()) {
-          const XCOFFBlockAuxEnt64 *AuxEntPtr =
-              getAuxEntPtr<XCOFFBlockAuxEnt64>(AuxAddress);
-          BlockAuxSym.LineNum = AuxEntPtr->LineNum;
-        } else {
-          const XCOFFBlockAuxEnt32 *AuxEntPtr =
-              getAuxEntPtr<XCOFFBlockAuxEnt32>(AuxAddress);
-          BlockAuxSym.LineNumLo = AuxEntPtr->LineNumLo;
-          BlockAuxSym.LineNumHi = AuxEntPtr->LineNumHi;
-        }
-        AuxEntTbl.push_back(
-            std::make_unique<XCOFFYAML::BlockAuxEnt>(BlockAuxSym));
+      case XCOFF::C_FCN:
+        dumpBlockAuxSym(Sym, SymbolEntRef);
         break;
-      }
-      case XCOFF::C_DWARF: {
-        assert(NumOfAuxSym == 1 && "expected a single aux symbol for C_DWARF!");
-        uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
-            SymbolEntRef.getEntryAddress(), 1);
-        XCOFFYAML::SectAuxEntForDWARF DwarfAuxSym;
-        if (Obj.is64Bit()) {
-          const XCOFFSectAuxEntForDWARF64 *AuxEntPtr =
-              getAuxEntPtr<XCOFFSectAuxEntForDWARF64>(AuxAddress);
-          DwarfAuxSym.LengthOfSectionPortion =
-              AuxEntPtr->LengthOfSectionPortion;
-          DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
-        } else {
-          const XCOFFSectAuxEntForDWARF32 *AuxEntPtr =
-              getAuxEntPtr<XCOFFSectAuxEntForDWARF32>(AuxAddress);
-          DwarfAuxSym.LengthOfSectionPortion =
-              AuxEntPtr->LengthOfSectionPortion;
-          DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
-        }
-        AuxEntTbl.push_back(
-            std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(DwarfAuxSym));
+      case XCOFF::C_DWARF:
+        dumpDwarfAuxSym(Sym, SymbolEntRef);
         break;
-      }
       default:
         break;
       }
-      Sym.AuxEntries = std::move(AuxEntTbl);
     }
 
     Symbols.push_back(std::move(Sym));

>From 70f83257d721d762a666f0fc094191fd9a4eb6c9 Mon Sep 17 00:00:00 2001
From: esmeyi <esme.yi at ibm.com>
Date: Wed, 8 Nov 2023 01:54:57 -0500
Subject: [PATCH 4/7] Address part of Digger's comments.

---
 llvm/lib/ObjectYAML/XCOFFYAML.cpp  | 28 ++++++++++++++--------------
 llvm/tools/obj2yaml/xcoff2yaml.cpp |  6 ++++++
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/ObjectYAML/XCOFFYAML.cpp b/llvm/lib/ObjectYAML/XCOFFYAML.cpp
index a9af0405546a3..39008f7967729 100644
--- a/llvm/lib/ObjectYAML/XCOFFYAML.cpp
+++ b/llvm/lib/ObjectYAML/XCOFFYAML.cpp
@@ -282,9 +282,16 @@ static void auxSymMapping(IO &IO, XCOFFYAML::SectAuxEntForStat &AuxSym) {
 
 void MappingTraits<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>>::mapping(
     IO &IO, std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym) {
+
+  auto ResetAuxSym = [&](auto *AuxEnt) {
+    if (!IO.outputting())
+      AuxSym.reset(AuxEnt);
+  };
+
   const bool Is64 =
       static_cast<XCOFFYAML::Object *>(IO.getContext())->Header.Magic ==
       (llvm::yaml::Hex16)XCOFF::XCOFF64;
+
   XCOFFYAML::AuxSymbolType AuxType;
   if (IO.outputting())
     AuxType = AuxSym.get()->Type;
@@ -294,41 +301,34 @@ void MappingTraits<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>>::mapping(
     if (!Is64)
       IO.setError("an auxiliary symbol of type AUX_EXCEPT cannot be defined in "
                   "XCOFF32");
-    if (!IO.outputting())
-      AuxSym.reset(new XCOFFYAML::ExcpetionAuxEnt());
+    ResetAuxSym(new XCOFFYAML::ExcpetionAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::ExcpetionAuxEnt>(AuxSym.get()));
     break;
   case XCOFFYAML::AUX_FCN:
-    if (!IO.outputting())
-      AuxSym.reset(new XCOFFYAML::FunctionAuxEnt());
+    ResetAuxSym(new XCOFFYAML::FunctionAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::FunctionAuxEnt>(AuxSym.get()), Is64);
     break;
   case XCOFFYAML::AUX_SYM:
-    if (!IO.outputting())
-      AuxSym.reset(new XCOFFYAML::BlockAuxEnt());
+    ResetAuxSym(new XCOFFYAML::BlockAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::BlockAuxEnt>(AuxSym.get()), Is64);
     break;
   case XCOFFYAML::AUX_FILE:
-    if (!IO.outputting())
-      AuxSym.reset(new XCOFFYAML::FileAuxEnt());
+    ResetAuxSym(new XCOFFYAML::FileAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::FileAuxEnt>(AuxSym.get()));
     break;
   case XCOFFYAML::AUX_CSECT:
-    if (!IO.outputting())
-      AuxSym.reset(new XCOFFYAML::CsectAuxEnt());
+    ResetAuxSym(new XCOFFYAML::CsectAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::CsectAuxEnt>(AuxSym.get()), Is64);
     break;
   case XCOFFYAML::AUX_SECT:
-    if (!IO.outputting())
-      AuxSym.reset(new XCOFFYAML::SectAuxEntForDWARF());
+    ResetAuxSym(new XCOFFYAML::SectAuxEntForDWARF());
     auxSymMapping(IO, *cast<XCOFFYAML::SectAuxEntForDWARF>(AuxSym.get()));
     break;
   case XCOFFYAML::AUX_STAT:
     if (Is64)
       IO.setError(
           "an auxiliary symbol of type AUX_STAT cannot be defined in XCOFF64");
-    if (!IO.outputting())
-      AuxSym.reset(new XCOFFYAML::SectAuxEntForStat());
+    ResetAuxSym(new XCOFFYAML::SectAuxEntForStat());
     auxSymMapping(IO, *cast<XCOFFYAML::SectAuxEntForStat>(AuxSym.get()));
     break;
   }
diff --git a/llvm/tools/obj2yaml/xcoff2yaml.cpp b/llvm/tools/obj2yaml/xcoff2yaml.cpp
index de1bf6d5d079a..f0856dc5f4173 100644
--- a/llvm/tools/obj2yaml/xcoff2yaml.cpp
+++ b/llvm/tools/obj2yaml/xcoff2yaml.cpp
@@ -166,6 +166,7 @@ void XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
 void XCOFFDumper::dumpFuncAuxSym(XCOFFYAML::Symbol &Sym,
                                  const uintptr_t AuxAddress) {
   XCOFFYAML::FunctionAuxEnt FunAuxSym;
+
   if (Obj.is64Bit()) {
     const XCOFFFunctionAuxEnt64 *AuxEntPtr =
         getAuxEntPtr<XCOFFFunctionAuxEnt64>(AuxAddress);
@@ -180,6 +181,7 @@ void XCOFFDumper::dumpFuncAuxSym(XCOFFYAML::Symbol &Sym,
     FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
     FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
   }
+
   Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::FunctionAuxEnt>(FunAuxSym));
 }
@@ -214,6 +216,7 @@ void XCOFFDumper::dumpCscetAuxSym(XCOFFYAML::Symbol &Sym,
     CsectAuxSym.StabInfoIndex = AuxEntPtr.getStabInfoIndex32();
     CsectAuxSym.StabSectNum = AuxEntPtr.getStabSectNum32();
   }
+
   Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::CsectAuxEnt>(CsectAuxSym));
 }
@@ -277,9 +280,11 @@ void XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
                                   const XCOFFSymbolRef &SymbolEntRef) {
   assert(Sym.NumberOfAuxEntries == 1 &&
          "expected a single aux symbol for C_DWARF!");
+
   uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
       SymbolEntRef.getEntryAddress(), 1);
   XCOFFYAML::SectAuxEntForDWARF DwarfAuxSym;
+
   if (Obj.is64Bit()) {
     const XCOFFSectAuxEntForDWARF64 *AuxEntPtr =
         getAuxEntPtr<XCOFFSectAuxEntForDWARF64>(AuxAddress);
@@ -291,6 +296,7 @@ void XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
     DwarfAuxSym.LengthOfSectionPortion = AuxEntPtr->LengthOfSectionPortion;
     DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
   }
+
   Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(DwarfAuxSym));
 }

>From d9f2abd23357cf0a889fca765ab7b10bd04ccd3b Mon Sep 17 00:00:00 2001
From: esmeyi <esme.yi at ibm.com>
Date: Tue, 21 Nov 2023 10:39:16 -0500
Subject: [PATCH 5/7] Address Digger's comments: 1) keep the order of AUX_CSECT
 symbol; 2) use reportError instead of asertion when the input object is
 illegal.

---
 llvm/tools/obj2yaml/xcoff2yaml.cpp | 74 ++++++++++++++++++------------
 1 file changed, 44 insertions(+), 30 deletions(-)

diff --git a/llvm/tools/obj2yaml/xcoff2yaml.cpp b/llvm/tools/obj2yaml/xcoff2yaml.cpp
index f0856dc5f4173..036cc2b38f916 100644
--- a/llvm/tools/obj2yaml/xcoff2yaml.cpp
+++ b/llvm/tools/obj2yaml/xcoff2yaml.cpp
@@ -28,12 +28,12 @@ class XCOFFDumper {
   // Dump auxiliary symbols.
   Error dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
                        const XCOFFSymbolRef &SymbolEntRef);
-  void dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
-                      const XCOFFSymbolRef &SymbolEntRef);
-  void dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
-                       const XCOFFSymbolRef &SymbolEntRef);
-  void dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
+  Error dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
                        const XCOFFSymbolRef &SymbolEntRef);
+  Error dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
+                        const XCOFFSymbolRef &SymbolEntRef);
+  Error dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
+                        const XCOFFSymbolRef &SymbolEntRef);
   Error dumpAuxSyms(XCOFFYAML::Symbol &Sym, const XCOFFSymbolRef &SymbolEntRef);
   void dumpFuncAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
   void dumpExpAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
@@ -146,10 +146,11 @@ Error XCOFFDumper::dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
   return Error::success();
 }
 
-void XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
-                                 const XCOFFSymbolRef &SymbolEntRef) {
-  assert(Sym.NumberOfAuxEntries == 1 &&
-         "expected a single aux symbol for C_STAT!");
+Error XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
+                                  const XCOFFSymbolRef &SymbolEntRef) {
+  if (Sym.NumberOfAuxEntries != 1)
+    return createError("expected a single aux symbol for C_STAT, while got: " +
+                       Twine(Sym.NumberOfAuxEntries.value()));
 
   const XCOFFSectAuxEntForStat *AuxEntPtr =
       getAuxEntPtr<XCOFFSectAuxEntForStat>(
@@ -161,6 +162,7 @@ void XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
   StatAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
   Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::SectAuxEntForStat>(StatAuxSym));
+  return Error::success();
 }
 
 void XCOFFDumper::dumpFuncAuxSym(XCOFFYAML::Symbol &Sym,
@@ -223,9 +225,17 @@ void XCOFFDumper::dumpCscetAuxSym(XCOFFYAML::Symbol &Sym,
 
 Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
                                const XCOFFSymbolRef &SymbolEntRef) {
+  auto ErrOrCsectAuxRef = SymbolEntRef.getXCOFFCsectAuxRef();
+  if (!ErrOrCsectAuxRef)
+    return ErrOrCsectAuxRef.takeError();
+  XCOFFCsectAuxRef CsectAuxRef = ErrOrCsectAuxRef.get();
+
   for (uint8_t I = 1; I <= Sym.NumberOfAuxEntries; ++I) {
-    if (I == Sym.NumberOfAuxEntries && !Obj.is64Bit())
-      break;
+
+    if (I == Sym.NumberOfAuxEntries && !Obj.is64Bit()) {
+      dumpCscetAuxSym(Sym, CsectAuxRef);
+      return Error::success();
+    }
 
     uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
         SymbolEntRef.getEntryAddress(), I);
@@ -233,29 +243,27 @@ Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
     if (Obj.is64Bit()) {
       XCOFF::SymbolAuxType Type = *Obj.getSymbolAuxType(AuxAddress);
       if (Type == XCOFF::SymbolAuxType::AUX_CSECT)
-        continue;
-      if (Type == XCOFF::SymbolAuxType::AUX_FCN)
+        dumpCscetAuxSym(Sym, CsectAuxRef);
+      else if (Type == XCOFF::SymbolAuxType::AUX_FCN)
         dumpFuncAuxSym(Sym, AuxAddress);
       else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT)
         dumpExpAuxSym(Sym, AuxAddress);
       else
-        llvm_unreachable("invalid aux symbol entry");
+        return createError("invalid auxiliary symbol type: " +
+                           Twine(static_cast<uint32_t>(Type)));
     } else
       dumpFuncAuxSym(Sym, AuxAddress);
   }
 
-  auto ErrOrCsectAuxRef = SymbolEntRef.getXCOFFCsectAuxRef();
-  if (!ErrOrCsectAuxRef)
-    return ErrOrCsectAuxRef.takeError();
-
-  dumpCscetAuxSym(Sym, ErrOrCsectAuxRef.get());
   return Error::success();
 }
 
-void XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
-                                  const XCOFFSymbolRef &SymbolEntRef) {
-  assert(Sym.NumberOfAuxEntries == 1 &&
-         "expected a single aux symbol for C_BLOCK or C_FCN!");
+Error XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
+                                   const XCOFFSymbolRef &SymbolEntRef) {
+  if (Sym.NumberOfAuxEntries != 1)
+    return createError(
+        "expected a single aux symbol for C_BLOCK or C_FCN, while got: " +
+        Twine(Sym.NumberOfAuxEntries.value()));
 
   uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
       SymbolEntRef.getEntryAddress(), 1);
@@ -274,12 +282,14 @@ void XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
 
   Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::BlockAuxEnt>(BlockAuxSym));
+  return Error::success();
 }
 
-void XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
-                                  const XCOFFSymbolRef &SymbolEntRef) {
-  assert(Sym.NumberOfAuxEntries == 1 &&
-         "expected a single aux symbol for C_DWARF!");
+Error XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
+                                   const XCOFFSymbolRef &SymbolEntRef) {
+  if (Sym.NumberOfAuxEntries != 1)
+    return createError("expected a single aux symbol for C_DWARF, while got: " +
+                       Twine(Sym.NumberOfAuxEntries.value()));
 
   uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
       SymbolEntRef.getEntryAddress(), 1);
@@ -299,6 +309,7 @@ void XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
 
   Sym.AuxEntries.push_back(
       std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(DwarfAuxSym));
+  return Error::success();
 }
 
 Error XCOFFDumper::dumpSymbols() {
@@ -335,7 +346,8 @@ Error XCOFFDumper::dumpSymbols() {
           return E;
         break;
       case XCOFF::C_STAT:
-        dumpStatAuxSym(Sym, SymbolEntRef);
+        if (Error E = dumpStatAuxSym(Sym, SymbolEntRef))
+          return E;
         break;
       case XCOFF::C_EXT:
       case XCOFF::C_WEAKEXT:
@@ -345,10 +357,12 @@ Error XCOFFDumper::dumpSymbols() {
         break;
       case XCOFF::C_BLOCK:
       case XCOFF::C_FCN:
-        dumpBlockAuxSym(Sym, SymbolEntRef);
+        if (Error E = dumpBlockAuxSym(Sym, SymbolEntRef))
+          return E;
         break;
       case XCOFF::C_DWARF:
-        dumpDwarfAuxSym(Sym, SymbolEntRef);
+        if (Error E = dumpDwarfAuxSym(Sym, SymbolEntRef))
+          return E;
         break;
       default:
         break;

>From be9326241f62439e34c6b5439b47aa9720df9fce Mon Sep 17 00:00:00 2001
From: esmeyi <esme.yi at ibm.com>
Date: Wed, 22 Nov 2023 00:53:21 -0500
Subject: [PATCH 6/7] Enrich error messages and test them.

---
 llvm/lib/ObjectYAML/XCOFFYAML.cpp             |  8 ++-
 .../tools/obj2yaml/XCOFF/aux-symbols.yaml     | 57 +++++++++++++++++++
 llvm/tools/obj2yaml/xcoff2yaml.cpp            | 18 +++---
 3 files changed, 74 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/ObjectYAML/XCOFFYAML.cpp b/llvm/lib/ObjectYAML/XCOFFYAML.cpp
index 39008f7967729..b71a7b8b6cb44 100644
--- a/llvm/lib/ObjectYAML/XCOFFYAML.cpp
+++ b/llvm/lib/ObjectYAML/XCOFFYAML.cpp
@@ -298,9 +298,11 @@ void MappingTraits<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>>::mapping(
   IO.mapRequired("Type", AuxType);
   switch (AuxType) {
   case XCOFFYAML::AUX_EXCEPT:
-    if (!Is64)
+    if (!Is64) {
       IO.setError("an auxiliary symbol of type AUX_EXCEPT cannot be defined in "
                   "XCOFF32");
+      return;
+    }
     ResetAuxSym(new XCOFFYAML::ExcpetionAuxEnt());
     auxSymMapping(IO, *cast<XCOFFYAML::ExcpetionAuxEnt>(AuxSym.get()));
     break;
@@ -325,9 +327,11 @@ void MappingTraits<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>>::mapping(
     auxSymMapping(IO, *cast<XCOFFYAML::SectAuxEntForDWARF>(AuxSym.get()));
     break;
   case XCOFFYAML::AUX_STAT:
-    if (Is64)
+    if (Is64) {
       IO.setError(
           "an auxiliary symbol of type AUX_STAT cannot be defined in XCOFF64");
+      return;
+    }
     ResetAuxSym(new XCOFFYAML::SectAuxEntForStat());
     auxSymMapping(IO, *cast<XCOFFYAML::SectAuxEntForStat>(AuxSym.get()));
     break;
diff --git a/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml b/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
index a4d1cb1d33aed..1581725bf43ef 100644
--- a/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
+++ b/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
@@ -262,3 +262,60 @@ Symbols:
     AuxEntries:
       - Type:            AUX_SYM
         LineNum:         1
+
+## Check that error messages are reported appropriately.
+
+# RUN: yaml2obj %s --docnum=3 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR1
+# ERROR1: failed to parse symbol "SYM_STAT": expected 1 aux symbol for C_STAT, while got 2
+
+--- !XCOFF
+FileHeader:
+  MagicNumber: 0x1DF
+Symbols:
+  - Name:               SYM_STAT
+    StorageClass:       C_STAT
+    NumberOfAuxEntries: 2
+    AuxEntries:
+      - Type: AUX_STAT
+      - Type: AUX_STAT
+
+# RUN: yaml2obj %s --docnum=4 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR2
+# ERROR2: failed to parse symbol "SYM_BLOCK": expected 1 aux symbol for C_BLOCK or C_FCN, while got 2
+
+--- !XCOFF
+FileHeader:
+  MagicNumber: 0x1DF
+Symbols:
+  - Name:               SYM_BLOCK
+    StorageClass:       C_BLOCK
+    NumberOfAuxEntries: 2
+    AuxEntries:
+      - Type: AUX_SYM
+      - Type: AUX_SYM
+
+# RUN: yaml2obj %s --docnum=5 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR3
+# ERROR3: failed to parse symbol "SYM_DWARF": expected 1 aux symbol for C_DWARF, while got 2
+
+--- !XCOFF
+FileHeader:
+  MagicNumber: 0x1DF
+Symbols:
+  - Name:               SYM_DWARF
+    StorageClass:       C_DWARF
+    NumberOfAuxEntries: 2
+    AuxEntries:
+      - Type: AUX_SECT
+      - Type: AUX_SECT
+
+# RUN: yaml2obj %s --docnum=6 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR4
+# ERROR4: failed to parse symbol "SYM_EXT": invalid auxiliary symbol type: 252
+
+--- !XCOFF
+FileHeader:
+  MagicNumber: 0x1F7
+Symbols:
+  - Name:         SYM_EXT
+    StorageClass: C_EXT
+    AuxEntries:
+      - Type: AUX_FILE
+      - Type: AUX_CSECT
diff --git a/llvm/tools/obj2yaml/xcoff2yaml.cpp b/llvm/tools/obj2yaml/xcoff2yaml.cpp
index 036cc2b38f916..beb4104e5136b 100644
--- a/llvm/tools/obj2yaml/xcoff2yaml.cpp
+++ b/llvm/tools/obj2yaml/xcoff2yaml.cpp
@@ -149,8 +149,9 @@ Error XCOFFDumper::dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
 Error XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
                                   const XCOFFSymbolRef &SymbolEntRef) {
   if (Sym.NumberOfAuxEntries != 1)
-    return createError("expected a single aux symbol for C_STAT, while got: " +
-                       Twine(Sym.NumberOfAuxEntries.value()));
+    return createError("failed to parse symbol \"" + Sym.SymbolName +
+                       "\": expected 1 aux symbol for C_STAT, while got " +
+                       Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
 
   const XCOFFSectAuxEntForStat *AuxEntPtr =
       getAuxEntPtr<XCOFFSectAuxEntForStat>(
@@ -249,7 +250,8 @@ Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
       else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT)
         dumpExpAuxSym(Sym, AuxAddress);
       else
-        return createError("invalid auxiliary symbol type: " +
+        return createError("failed to parse symbol \"" + Sym.SymbolName +
+                           "\": invalid auxiliary symbol type: " +
                            Twine(static_cast<uint32_t>(Type)));
     } else
       dumpFuncAuxSym(Sym, AuxAddress);
@@ -262,8 +264,9 @@ Error XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
                                    const XCOFFSymbolRef &SymbolEntRef) {
   if (Sym.NumberOfAuxEntries != 1)
     return createError(
-        "expected a single aux symbol for C_BLOCK or C_FCN, while got: " +
-        Twine(Sym.NumberOfAuxEntries.value()));
+        "failed to parse symbol \"" + Sym.SymbolName +
+        "\": expected 1 aux symbol for C_BLOCK or C_FCN, while got " +
+        Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
 
   uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
       SymbolEntRef.getEntryAddress(), 1);
@@ -288,8 +291,9 @@ Error XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
 Error XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
                                    const XCOFFSymbolRef &SymbolEntRef) {
   if (Sym.NumberOfAuxEntries != 1)
-    return createError("expected a single aux symbol for C_DWARF, while got: " +
-                       Twine(Sym.NumberOfAuxEntries.value()));
+    return createError("failed to parse symbol \"" + Sym.SymbolName +
+                       "\": expected 1 aux symbol for C_DWARF, while got " +
+                       Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
 
   uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
       SymbolEntRef.getEntryAddress(), 1);

>From 96aff599e3f9ac20ba238a8ff0a0eeb88cc76703 Mon Sep 17 00:00:00 2001
From: esmeyi <esme.yi at ibm.com>
Date: Thu, 23 Nov 2023 03:51:43 -0500
Subject: [PATCH 7/7] printf symbol index info for error message.

---
 .../tools/obj2yaml/XCOFF/aux-symbols.yaml     |  9 +++---
 llvm/tools/obj2yaml/xcoff2yaml.cpp            | 32 +++++++++++++------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml b/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
index 1581725bf43ef..7f93b8dae0ca9 100644
--- a/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
+++ b/llvm/test/tools/obj2yaml/XCOFF/aux-symbols.yaml
@@ -266,12 +266,13 @@ Symbols:
 ## Check that error messages are reported appropriately.
 
 # RUN: yaml2obj %s --docnum=3 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR1
-# ERROR1: failed to parse symbol "SYM_STAT": expected 1 aux symbol for C_STAT, while got 2
+# ERROR1: failed to parse symbol "SYM_STAT" with index of 1: expected 1 aux symbol for C_STAT, while got 2
 
 --- !XCOFF
 FileHeader:
   MagicNumber: 0x1DF
 Symbols:
+  - Name:               SYM
   - Name:               SYM_STAT
     StorageClass:       C_STAT
     NumberOfAuxEntries: 2
@@ -280,7 +281,7 @@ Symbols:
       - Type: AUX_STAT
 
 # RUN: yaml2obj %s --docnum=4 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR2
-# ERROR2: failed to parse symbol "SYM_BLOCK": expected 1 aux symbol for C_BLOCK or C_FCN, while got 2
+# ERROR2: failed to parse symbol "SYM_BLOCK" with index of 0: expected 1 aux symbol for C_BLOCK or C_FCN, while got 2
 
 --- !XCOFF
 FileHeader:
@@ -294,7 +295,7 @@ Symbols:
       - Type: AUX_SYM
 
 # RUN: yaml2obj %s --docnum=5 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR3
-# ERROR3: failed to parse symbol "SYM_DWARF": expected 1 aux symbol for C_DWARF, while got 2
+# ERROR3: failed to parse symbol "SYM_DWARF" with index of 0: expected 1 aux symbol for C_DWARF, while got 2
 
 --- !XCOFF
 FileHeader:
@@ -308,7 +309,7 @@ Symbols:
       - Type: AUX_SECT
 
 # RUN: yaml2obj %s --docnum=6 -o - | not obj2yaml 2>&1 | FileCheck %s --check-prefix=ERROR4
-# ERROR4: failed to parse symbol "SYM_EXT": invalid auxiliary symbol type: 252
+# ERROR4: failed to parse symbol "SYM_EXT" with index of 0: invalid auxiliary symbol type: 252
 
 --- !XCOFF
 FileHeader:
diff --git a/llvm/tools/obj2yaml/xcoff2yaml.cpp b/llvm/tools/obj2yaml/xcoff2yaml.cpp
index beb4104e5136b..f7c2bae747989 100644
--- a/llvm/tools/obj2yaml/xcoff2yaml.cpp
+++ b/llvm/tools/obj2yaml/xcoff2yaml.cpp
@@ -148,10 +148,13 @@ Error XCOFFDumper::dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
 
 Error XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
                                   const XCOFFSymbolRef &SymbolEntRef) {
-  if (Sym.NumberOfAuxEntries != 1)
+  if (Sym.NumberOfAuxEntries != 1) {
+    uint32_t SymbolIndex = Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
     return createError("failed to parse symbol \"" + Sym.SymbolName +
-                       "\": expected 1 aux symbol for C_STAT, while got " +
+                       "\" with index of " + Twine(SymbolIndex) +
+                       ": expected 1 aux symbol for C_STAT, while got " +
                        Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
+  }
 
   const XCOFFSectAuxEntForStat *AuxEntPtr =
       getAuxEntPtr<XCOFFSectAuxEntForStat>(
@@ -249,10 +252,15 @@ Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
         dumpFuncAuxSym(Sym, AuxAddress);
       else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT)
         dumpExpAuxSym(Sym, AuxAddress);
-      else
+      else {
+        uint32_t SymbolIndex =
+            Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
         return createError("failed to parse symbol \"" + Sym.SymbolName +
-                           "\": invalid auxiliary symbol type: " +
+                           "\" with index of " + Twine(SymbolIndex) +
+                           ": invalid auxiliary symbol type: " +
                            Twine(static_cast<uint32_t>(Type)));
+      }
+
     } else
       dumpFuncAuxSym(Sym, AuxAddress);
   }
@@ -262,11 +270,14 @@ Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
 
 Error XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
                                    const XCOFFSymbolRef &SymbolEntRef) {
-  if (Sym.NumberOfAuxEntries != 1)
+  if (Sym.NumberOfAuxEntries != 1) {
+    uint32_t SymbolIndex = Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
     return createError(
-        "failed to parse symbol \"" + Sym.SymbolName +
-        "\": expected 1 aux symbol for C_BLOCK or C_FCN, while got " +
+        "failed to parse symbol \"" + Sym.SymbolName + "\" with index of " +
+        Twine(SymbolIndex) +
+        ": expected 1 aux symbol for C_BLOCK or C_FCN, while got " +
         Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
+  }
 
   uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
       SymbolEntRef.getEntryAddress(), 1);
@@ -290,10 +301,13 @@ Error XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
 
 Error XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
                                    const XCOFFSymbolRef &SymbolEntRef) {
-  if (Sym.NumberOfAuxEntries != 1)
+  if (Sym.NumberOfAuxEntries != 1) {
+    uint32_t SymbolIndex = Obj.getSymbolIndex(SymbolEntRef.getEntryAddress());
     return createError("failed to parse symbol \"" + Sym.SymbolName +
-                       "\": expected 1 aux symbol for C_DWARF, while got " +
+                       "\" with index of " + Twine(SymbolIndex) +
+                       ": expected 1 aux symbol for C_DWARF, while got " +
                        Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
+  }
 
   uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
       SymbolEntRef.getEntryAddress(), 1);



More information about the cfe-commits mailing list