[Lldb-commits] [lldb] [LLDB] Impove ObjectFileELF's .dynamic parsing and usage. (PR #101237)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Aug 5 07:54:24 PDT 2024


================
@@ -0,0 +1,58 @@
+// REQUIRES: system-linux, native
----------------
labath wrote:

If it was a lot of work then sure, I'd agree with you -- but I don't think that's the case here. I just gave it a shot, and this is the only change I needed to make on top of your patch to be able to parse DT_NEEDED of a section-free file (disclaimer: This code is mainly for demonstration, it's possible there is a better way to do that):
```
diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index d26215a9ce3f..5f0d2233b865 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -3810,28 +3810,26 @@ std::optional<DataExtractor> ObjectFileELF::GetDynstrData() {
     }
   }
 
-  // Every ELF file which represents an executable or shared library has
-  // mandatory .dynamic entries. Two of these values are DT_STRTAB and DT_STRSZ
-  // and represent the dynamic symbol tables's string table. These are needed
-  // by the dynamic loader and we can read them from a process' address space.
-  //
-  // When loading and ELF file from memory, only the program headers end up
-  // being mapped into memory, and we can find these values in the PT_DYNAMIC
-  // segment.
-  if (!IsInMemory())
-    return std::nullopt;
-  ProcessSP process_sp(m_process_wp.lock());
-  if (!process_sp)
-    return std::nullopt;
-
   const ELFDynamic *strtab = FindDynamicSymbol(DT_STRTAB);
   const ELFDynamic *strsz = FindDynamicSymbol(DT_STRSZ);
   if (strtab == nullptr || strsz == nullptr)
     return std::nullopt;
 
-  if (DataBufferSP data_sp =
-          ReadMemory(process_sp, strtab->d_ptr, strsz->d_val))
-    return DataExtractor(data_sp, GetByteOrder(), GetAddressByteSize());
+  if (ProcessSP process_sp = m_process_wp.lock()) {
+    if (DataBufferSP data_sp =
+            ReadMemory(process_sp, strtab->d_ptr, strsz->d_val))
+      return DataExtractor(data_sp, GetByteOrder(), GetAddressByteSize());
+  } else {
+    Address addr;
+    if (addr.ResolveAddressUsingFileSections(strtab->d_ptr, GetSectionList())) {
+      DataExtractor data;
+      addr.GetSection()->GetSectionData(data);
+      return DataExtractor(data,
+                           strtab->d_ptr - addr.GetSection()->GetFileOffset(),
+                           strsz->d_val);
+    }
+  }
+
   return std::nullopt;
 }
 
```

And I think this goes a long way towards improving the testability of this patch. I know section-free files aren't likely to be of much use, but they're really great for testing, and in terms of raw LOC, such a test may cover 80% of this patch.

https://github.com/llvm/llvm-project/pull/101237


More information about the lldb-commits mailing list