[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


================
@@ -873,33 +873,29 @@ Address ObjectFileELF::GetImageInfoAddress(Target *target) {
   if (!section_list)
     return Address();
 
-  // Find the SHT_DYNAMIC (.dynamic) section.
-  SectionSP dynsym_section_sp(
-      section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true));
-  if (!dynsym_section_sp)
-    return Address();
-  assert(dynsym_section_sp->GetObjectFile() == this);
-
-  user_id_t dynsym_id = dynsym_section_sp->GetID();
-  const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
-  if (!dynsym_hdr)
-    return Address();
-
   for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) {
-    ELFDynamic &symbol = m_dynamic_symbols[i];
+    const ELFDynamic &symbol = m_dynamic_symbols[i].symbol;
 
     if (symbol.d_tag == DT_DEBUG) {
       // Compute the offset as the number of previous entries plus the size of
       // d_tag.
-      addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
-      return Address(dynsym_section_sp, offset);
+      addr_t offset = (i * 2 + 1) * GetAddressByteSize();
+      addr_t file_addr = m_dynamic_base_addr + offset;
+      Address addr;
+      if (addr.ResolveAddressUsingFileSections(file_addr, GetSectionList()))
+        return addr;
     }
     // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP
     // exists in non-PIE.
     else if ((symbol.d_tag == DT_MIPS_RLD_MAP ||
               symbol.d_tag == DT_MIPS_RLD_MAP_REL) &&
              target) {
-      addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
+      SectionSP dynsym_section_sp(section_list->FindSectionByType(
+          eSectionTypeELFDynamicLinkInfo, true));
+      if (!dynsym_section_sp)
+        return Address();
+
+      addr_t offset = (i * 2 + 1) * GetAddressByteSize();
       addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);
       if (dyn_base == LLDB_INVALID_ADDRESS)
         return Address();
----------------
labath wrote:

I'm not sure what you mean, the code does go about it in slightly different way (because that was convenient at the time), but I think it should be equivalent to:
```
addr_t offset = (i * 2 + 1) * GetAddressByteSize();
addr_t file_addr = m_dynamic_base_addr + offset;
Address addr;
if (!addr.ResolveAddressUsingFileSections(file_addr, GetSectionList()))
  return Address();
Status error;
if (symbol.d_tag == DT_MIPS_RLD_MAP) {
    // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.
    Address pointer_addr;
    if (target->ReadPointerFromMemory(addr, error, pointer_addr, true))
      return pointer_addr;
}
```
Basically, I think it does the same thing as DT_DEBUG, but with an extra level of indirection.


Also, this is kind of the reason why I'd really like to have a test for this patch which does not require running an executable. I don't want the next person to come in here to be afraid of making any changes because he can't test his change.

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


More information about the lldb-commits mailing list