https://github.com/DhruvSrivastavaX updated 
https://github.com/llvm/llvm-project/pull/131304

>From 106e137fea7d4b420ce3d97a8df16c3a91400997 Mon Sep 17 00:00:00 2001
From: Dhruv-Srivastava <dhruv.srivast...@ibm.com>
Date: Fri, 14 Mar 2025 02:51:21 -0500
Subject: [PATCH 1/3] Support for XCOFF Sections

---
 .../ObjectFile/XCOFF/ObjectFileXCOFF.cpp      | 153 +++++++++++++-----
 1 file changed, 114 insertions(+), 39 deletions(-)

diff --git a/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp 
b/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
index b54d43c5dd737..0dd9126468923 100644
--- a/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
@@ -190,50 +190,125 @@ void ObjectFileXCOFF::ParseSymtab(Symtab &lldb_symtab) {}
 
 bool ObjectFileXCOFF::IsStripped() { return false; }
 
-void ObjectFileXCOFF::CreateSections(SectionList &unified_section_list) {}
-
-void ObjectFileXCOFF::Dump(Stream *s) {}
-
-ArchSpec ObjectFileXCOFF::GetArchitecture() {
-  ArchSpec arch_spec =
-      ArchSpec(eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
-  return arch_spec;
+void ObjectFileXCOFF::CreateSections(SectionList &unified_section_list) {
+
+  if (m_sections_up)
+    return;
+  m_sections_up = std::make_unique<SectionList>();
+  ModuleSP module_sp(GetModule());
+  if (module_sp) {
+    std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
+
+    ModuleSP module_sp(GetModule());
+    for (auto sIdx = m_binary->section_begin(); sIdx != 
m_binary->section_end();
+         ++sIdx) {
+      llvm::Expected<llvm::StringRef> name =
+          m_binary->getSectionName(sIdx->getRawDataRefImpl());
+      if (!name) {
+        llvm::Error err = name.takeError();
+      }
+      llvm::StringRef sect_name = *name;
+      ConstString const_sect_name(sect_name);
+      int sect_index = sIdx->getIndex(), idx = 1;
+      llvm::Expected<llvm::object::DataRefImpl> section =
+          m_binary->getSectionByNum(sect_index);
+      if (!section) {
+        llvm::Error err = section.takeError();
+      }
+      llvm::object::DataRefImpl dataref = section.get();
+      const llvm::object::XCOFFSectionHeader64 *sectionPtr =
+          reinterpret_cast<const llvm::object::XCOFFSectionHeader64 *>(
+              dataref.p);
+
+      SectionType section_type = lldb::eSectionTypeOther;
+      if (sectionPtr->Flags & XCOFF::STYP_TEXT)
+        section_type = eSectionTypeCode;
+      if (sectionPtr->Flags & XCOFF::STYP_DATA)
+        section_type = eSectionTypeData;
+      if (sectionPtr->Flags & XCOFF::STYP_BSS)
+        section_type = eSectionTypeZeroFill;
+      if (sectionPtr->Flags & XCOFF::STYP_DWARF) {
+        SectionType section_type =
+            llvm::StringSwitch<SectionType>(sect_name)
+                .Case(".dwinfo", eSectionTypeDWARFDebugInfo)
+                .Case(".dwline", eSectionTypeDWARFDebugLine)
+                .Case(".dwabrev", eSectionTypeDWARFDebugAbbrev)
+                .Default(eSectionTypeInvalid);
+
+        if (section_type == eSectionTypeInvalid)
+          section_type = lldb::eSectionTypeOther;
+      }
+      SectionSP section_sp(new Section(
+          module_sp,       // Module to which this section belongs
+          this,            // Object file to which this section belongs
+          idx++,           // Section ID is the 1 based section index.
+          const_sect_name, // Name of this section
+          section_type,
+          sectionPtr->VirtualAddress,      // File VM address == addresses as
+                                           // they are found in the object file
+          sectionPtr->SectionSize,         // VM size in bytes of this section
+          sectionPtr->FileOffsetToRawData, // Offset to the data for this
+                                           // section in the file
+          sectionPtr->SectionSize, // Size in bytes of this section as found in
+                                   // the file
+          0,                       // FIXME: alignment
+          sectionPtr->Flags));     // Flags for this section
+
+      uint32_t permissions = 0;
+      permissions |= ePermissionsReadable;
+      if (sectionPtr->Flags & (XCOFF::STYP_DATA | XCOFF::STYP_BSS))
+        permissions |= ePermissionsWritable;
+      if (sectionPtr->Flags & XCOFF::STYP_TEXT)
+        permissions |= ePermissionsExecutable;
+      section_sp->SetPermissions(permissions);
+
+      m_sections_up->AddSection(section_sp);
+      unified_section_list.AddSection(section_sp);
+    }
+  }
 }
+  void ObjectFileXCOFF::Dump(Stream * s) {}
 
-UUID ObjectFileXCOFF::GetUUID() { return UUID(); }
+  ArchSpec ObjectFileXCOFF::GetArchitecture() {
+    ArchSpec arch_spec =
+        ArchSpec(eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
+    return arch_spec;
+  }
 
-uint32_t ObjectFileXCOFF::GetDependentModules(FileSpecList &files) { return 0; 
}
+  UUID ObjectFileXCOFF::GetUUID() { return UUID(); }
 
-ObjectFile::Type ObjectFileXCOFF::CalculateType() {
-  if (m_binary->fileHeader64()->Flags & XCOFF::F_EXEC)
-    return eTypeExecutable;
-  else if (m_binary->fileHeader64()->Flags & XCOFF::F_SHROBJ)
-    return eTypeSharedLibrary;
-  return eTypeUnknown;
-}
+  uint32_t ObjectFileXCOFF::GetDependentModules(FileSpecList & files) {
+    return 0;
+  }
 
-ObjectFile::Strata ObjectFileXCOFF::CalculateStrata() { return eStrataUnknown; 
}
+  ObjectFile::Type ObjectFileXCOFF::CalculateType() {
+    if (m_binary->fileHeader64()->Flags & XCOFF::F_EXEC)
+      return eTypeExecutable;
+    else if (m_binary->fileHeader64()->Flags & XCOFF::F_SHROBJ)
+      return eTypeSharedLibrary;
+    return eTypeUnknown;
+  }
 
-lldb::WritableDataBufferSP
-ObjectFileXCOFF::MapFileDataWritable(const FileSpec &file, uint64_t Size,
-                                     uint64_t Offset) {
-  return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
-                                                         Offset);
-}
+  ObjectFile::Strata ObjectFileXCOFF::CalculateStrata() {
+    return eStrataUnknown;
+  }
 
-ObjectFileXCOFF::ObjectFileXCOFF(const lldb::ModuleSP &module_sp,
-                                 DataBufferSP data_sp,
-                                 lldb::offset_t data_offset,
-                                 const FileSpec *file,
-                                 lldb::offset_t file_offset,
-                                 lldb::offset_t length)
-    : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) {
-  if (file)
-    m_file = *file;
-}
+  lldb::WritableDataBufferSP ObjectFileXCOFF::MapFileDataWritable(
+      const FileSpec &file, uint64_t Size, uint64_t Offset) {
+    return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), 
Size,
+                                                           Offset);
+  }
+
+  ObjectFileXCOFF::ObjectFileXCOFF(
+      const lldb::ModuleSP &module_sp, DataBufferSP data_sp,
+      lldb::offset_t data_offset, const FileSpec *file,
+      lldb::offset_t file_offset, lldb::offset_t length)
+      : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) 
{
+    if (file)
+      m_file = *file;
+  }
 
-ObjectFileXCOFF::ObjectFileXCOFF(const lldb::ModuleSP &module_sp,
-                                 DataBufferSP header_data_sp,
-                                 const lldb::ProcessSP &process_sp,
-                                 addr_t header_addr)
-    : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}
+  ObjectFileXCOFF::ObjectFileXCOFF(
+      const lldb::ModuleSP &module_sp, DataBufferSP header_data_sp,
+      const lldb::ProcessSP &process_sp, addr_t header_addr)
+      : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}

>From 9c06cd5204c3a78a74947fd8c18c741d701e5d8d Mon Sep 17 00:00:00 2001
From: Dhruv-Srivastava <dhruv.srivast...@ibm.com>
Date: Fri, 14 Mar 2025 06:15:58 -0500
Subject: [PATCH 2/3] format fix

---
 .../ObjectFile/XCOFF/ObjectFileXCOFF.cpp      | 74 +++++++++----------
 1 file changed, 37 insertions(+), 37 deletions(-)

diff --git a/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp 
b/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
index 0dd9126468923..eebf6a3a657a5 100644
--- a/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
@@ -267,48 +267,48 @@ void ObjectFileXCOFF::CreateSections(SectionList 
&unified_section_list) {
     }
   }
 }
-  void ObjectFileXCOFF::Dump(Stream * s) {}
+void ObjectFileXCOFF::Dump(Stream *s) {}
 
-  ArchSpec ObjectFileXCOFF::GetArchitecture() {
-    ArchSpec arch_spec =
-        ArchSpec(eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
-    return arch_spec;
-  }
+ArchSpec ObjectFileXCOFF::GetArchitecture() {
+  ArchSpec arch_spec =
+      ArchSpec(eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE);
+  return arch_spec;
+}
 
-  UUID ObjectFileXCOFF::GetUUID() { return UUID(); }
+UUID ObjectFileXCOFF::GetUUID() { return UUID(); }
 
-  uint32_t ObjectFileXCOFF::GetDependentModules(FileSpecList & files) {
-    return 0;
-  }
+uint32_t ObjectFileXCOFF::GetDependentModules(FileSpecList &files) { return 0; 
}
 
-  ObjectFile::Type ObjectFileXCOFF::CalculateType() {
-    if (m_binary->fileHeader64()->Flags & XCOFF::F_EXEC)
-      return eTypeExecutable;
-    else if (m_binary->fileHeader64()->Flags & XCOFF::F_SHROBJ)
-      return eTypeSharedLibrary;
-    return eTypeUnknown;
-  }
+ObjectFile::Type ObjectFileXCOFF::CalculateType() {
+  if (m_binary->fileHeader64()->Flags & XCOFF::F_EXEC)
+    return eTypeExecutable;
+  else if (m_binary->fileHeader64()->Flags & XCOFF::F_SHROBJ)
+    return eTypeSharedLibrary;
+  return eTypeUnknown;
+}
 
-  ObjectFile::Strata ObjectFileXCOFF::CalculateStrata() {
-    return eStrataUnknown;
-  }
+ObjectFile::Strata ObjectFileXCOFF::CalculateStrata() { return eStrataUnknown; 
}
 
-  lldb::WritableDataBufferSP ObjectFileXCOFF::MapFileDataWritable(
-      const FileSpec &file, uint64_t Size, uint64_t Offset) {
-    return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), 
Size,
-                                                           Offset);
-  }
+lldb::WritableDataBufferSP
+ObjectFileXCOFF::MapFileDataWritable(const FileSpec &file, uint64_t Size,
+                                     uint64_t Offset) {
+  return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
+                                                         Offset);
+}
 
-  ObjectFileXCOFF::ObjectFileXCOFF(
-      const lldb::ModuleSP &module_sp, DataBufferSP data_sp,
-      lldb::offset_t data_offset, const FileSpec *file,
-      lldb::offset_t file_offset, lldb::offset_t length)
-      : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) 
{
-    if (file)
-      m_file = *file;
-  }
+ObjectFileXCOFF::ObjectFileXCOFF(const lldb::ModuleSP &module_sp,
+                                 DataBufferSP data_sp,
+                                 lldb::offset_t data_offset,
+                                 const FileSpec *file,
+                                 lldb::offset_t file_offset,
+                                 lldb::offset_t length)
+    : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) {
+  if (file)
+    m_file = *file;
+}
 
-  ObjectFileXCOFF::ObjectFileXCOFF(
-      const lldb::ModuleSP &module_sp, DataBufferSP header_data_sp,
-      const lldb::ProcessSP &process_sp, addr_t header_addr)
-      : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}
+ObjectFileXCOFF::ObjectFileXCOFF(const lldb::ModuleSP &module_sp,
+                                 DataBufferSP header_data_sp,
+                                 const lldb::ProcessSP &process_sp,
+                                 addr_t header_addr)
+    : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}

>From edb54c3ff6a63c97b4c1d46b046abde981109f40 Mon Sep 17 00:00:00 2001
From: Dhruv-Srivastava <dhruv.srivast...@ibm.com>
Date: Thu, 3 Apr 2025 01:56:56 -0500
Subject: [PATCH 3/3] Test case modification

---
 lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp | 5 +++--
 lldb/test/Shell/ObjectFile/XCOFF/basic-info.yaml         | 3 +++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp 
b/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
index eebf6a3a657a5..179b26df612a0 100644
--- a/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/XCOFF/ObjectFileXCOFF.cpp
@@ -200,6 +200,7 @@ void ObjectFileXCOFF::CreateSections(SectionList 
&unified_section_list) {
     std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
 
     ModuleSP module_sp(GetModule());
+    int idx = 0;
     for (auto sIdx = m_binary->section_begin(); sIdx != 
m_binary->section_end();
          ++sIdx) {
       llvm::Expected<llvm::StringRef> name =
@@ -209,7 +210,7 @@ void ObjectFileXCOFF::CreateSections(SectionList 
&unified_section_list) {
       }
       llvm::StringRef sect_name = *name;
       ConstString const_sect_name(sect_name);
-      int sect_index = sIdx->getIndex(), idx = 1;
+      int sect_index = sIdx->getIndex();
       llvm::Expected<llvm::object::DataRefImpl> section =
           m_binary->getSectionByNum(sect_index);
       if (!section) {
@@ -241,7 +242,7 @@ void ObjectFileXCOFF::CreateSections(SectionList 
&unified_section_list) {
       SectionSP section_sp(new Section(
           module_sp,       // Module to which this section belongs
           this,            // Object file to which this section belongs
-          idx++,           // Section ID is the 1 based section index.
+          ++idx,           // Section ID is the 1 based section index.
           const_sect_name, // Name of this section
           section_type,
           sectionPtr->VirtualAddress,      // File VM address == addresses as
diff --git a/lldb/test/Shell/ObjectFile/XCOFF/basic-info.yaml 
b/lldb/test/Shell/ObjectFile/XCOFF/basic-info.yaml
index 3c0037db36dbb..e5cc9d0bc5063 100644
--- a/lldb/test/Shell/ObjectFile/XCOFF/basic-info.yaml
+++ b/lldb/test/Shell/ObjectFile/XCOFF/basic-info.yaml
@@ -7,6 +7,9 @@
 # CHECK: Stripped: false
 # CHECK: Type: executable
 # CHECK: Strata: unknown
+# CHECK: Name: .text
+# CHECK-NEXT: code
+# CHECK-NEXT: r-x
 
 --- !XCOFF
 FileHeader:

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to