[llvm] [XCOFF][obj2yaml] support parsing auxiliary symbols for XCOFF (PR #70642)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 7 22:56:22 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/4] 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 63064abb4d3c322..9a3b4aa2623551c 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 44ef33501b65e43..a9af0405546a3c7 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 cd1e88dec11d29b..fbd5fa0629d10bd 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 000000000000000..a4d1cb1d33aeda3
--- /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 882c410496012a1..1960513c611e8e7 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/4] 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 1960513c611e8e7..b2799606e6db4f3 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/4] 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 b2799606e6db4f3..de1bf6d5d079a91 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/4] 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 a9af0405546a3c7..39008f796772980 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 de1bf6d5d079a91..f0856dc5f41737d 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));
 }



More information about the llvm-commits mailing list