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

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 1 13:50:15 PDT 2024


================
@@ -3704,3 +3790,83 @@ ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size,
   return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,
                                                          Offset);
 }
+
+std::optional<DataExtractor> ObjectFileELF::GetDynstrData() {
+
+  SectionList *section_list = GetSectionList();
+  if (section_list) {
+    // Find the SHT_DYNAMIC section.
+    Section *dynamic =
+        section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)
+            .get();
+    if (dynamic) {
+      assert(dynamic->GetObjectFile() == this);
+      const ELFSectionHeaderInfo *header =
+          GetSectionHeaderByIndex(dynamic->GetID());
+      if (header) {
+        // sh_link: section header index of string table used by entries in
+        // the section.
+        Section *dynstr = section_list->FindSectionByID(header->sh_link).get();
+        DataExtractor data;
+        if (dynstr && ReadSectionData(dynstr, data))
+          return data;
+      }
+    }
+  }
+
+  // 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;
+
+  DataBufferSP data_sp = ReadMemory(process_sp, strtab->d_ptr, strsz->d_val);
----------------
clayborg wrote:

The code above this checks that the ELF file was in memory, so this was only designed to work for in memory ELF files. But as you say we might be able to make this work for on disk files that have no section headers as well. Can we do this in a separate patch? I would like to make forward progress on ELF core files without making up new work.

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


More information about the lldb-commits mailing list