[Lldb-commits] [PATCH] D142672: [lldb] Make SBSection::GetSectionData call Section::GetSectionData.

Jorge Gorbe Moya via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Jan 26 14:30:00 PST 2023


jgorbe created this revision.
jgorbe added a reviewer: labath.
jgorbe added a project: LLDB.
Herald added a subscriber: JDevlieghere.
Herald added a project: All.
jgorbe requested review of this revision.

`SBSection::GetSectionData` and `Section::GetSectionData` are
implemented differently, and the `SBSection` method doesn't handle
compressed sections correctly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142672

Files:
  lldb/source/API/SBSection.cpp
  lldb/test/API/python_api/section/TestSectionAPI.py
  lldb/test/API/python_api/section/compressed-sections.yaml


Index: lldb/test/API/python_api/section/compressed-sections.yaml
===================================================================
--- /dev/null
+++ lldb/test/API/python_api/section/compressed-sections.yaml
@@ -0,0 +1,11 @@
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_386
+Sections:
+  - Name:            .compressed
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_COMPRESSED ]
+    Content:         010000000800000001000000789c5330700848286898000009c802c1
Index: lldb/test/API/python_api/section/TestSectionAPI.py
===================================================================
--- lldb/test/API/python_api/section/TestSectionAPI.py
+++ lldb/test/API/python_api/section/TestSectionAPI.py
@@ -48,3 +48,18 @@
         section = target.modules[0].sections[0]
         self.assertEqual(section.GetAlignment(), 0x1000)
         self.assertEqual(section.alignment, 0x1000)
+
+    def test_compressed_section_data(self):
+        exe = self.getBuildArtifact("compressed-sections.out")
+        self.yaml2obj("compressed-sections.yaml", exe)
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target, VALID_TARGET)
+
+        # exe contains a single section with SHF_COMPRESSED. Check that
+        # GetSectionData returns the uncompressed data and not the raw contents
+        # of the section.
+        section = target.modules[0].sections[0]
+        section_data = section.GetSectionData().uint8s
+        self.assertEqual(section_data,
+                         [0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90])
+
Index: lldb/source/API/SBSection.cpp
===================================================================
--- lldb/source/API/SBSection.cpp
+++ lldb/source/API/SBSection.cpp
@@ -182,35 +182,11 @@
   SBData sb_data;
   SectionSP section_sp(GetSP());
   if (section_sp) {
-    const uint64_t sect_file_size = section_sp->GetFileSize();
-    if (sect_file_size > 0) {
-      ModuleSP module_sp(section_sp->GetModule());
-      if (module_sp) {
-        ObjectFile *objfile = module_sp->GetObjectFile();
-        if (objfile) {
-          const uint64_t sect_file_offset =
-              objfile->GetFileOffset() + section_sp->GetFileOffset();
-          const uint64_t file_offset = sect_file_offset + offset;
-          uint64_t file_size = size;
-          if (file_size == UINT64_MAX) {
-            file_size = section_sp->GetByteSize();
-            if (file_size > offset)
-              file_size -= offset;
-            else
-              file_size = 0;
-          }
-          auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer(
-              objfile->GetFileSpec().GetPath(), file_size, file_offset);
-          if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
-            DataExtractorSP data_extractor_sp(
-                new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
-                                  objfile->GetAddressByteSize()));
-
-            sb_data.SetOpaque(data_extractor_sp);
-          }
-        }
-      }
-    }
+    DataExtractor section_data;
+    section_sp->GetSectionData(section_data);
+    DataExtractorSP result_data_sp =
+        std::make_shared<DataExtractor>(section_data, offset, size);
+    sb_data.SetOpaque(result_data_sp);
   }
   return sb_data;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142672.492571.patch
Type: text/x-patch
Size: 3376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230126/cb0b3feb/attachment-0001.bin>


More information about the lldb-commits mailing list