[Lldb-commits] [lldb] [lldb] Fix Block::GetRangeIndexContainingAddress for discontinuous functions (PR #124931)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue Feb 4 08:31:52 PST 2025


================
@@ -243,25 +243,15 @@ bool Block::GetRangeContainingAddress(const Address &addr,
                                       AddressRange &range) {
   Function *function = CalculateSymbolContextFunction();
   if (function) {
-    const AddressRange &func_range = function->GetAddressRange();
-    if (addr.GetModule() == func_range.GetBaseAddress().GetModule()) {
-      const addr_t file_addr = addr.GetFileAddress();
-      const addr_t func_file_addr =
-          func_range.GetBaseAddress().GetFileAddress();
-      if (file_addr >= func_file_addr &&
-          file_addr < func_file_addr + func_range.GetByteSize()) {
-        addr_t offset = file_addr - func_file_addr;
-
-        const Range *range_ptr = m_ranges.FindEntryThatContains(offset);
-
-        if (range_ptr) {
-          range.GetBaseAddress() =
-              Address(func_file_addr + range_ptr->GetRangeBase(),
-                      addr.GetModule()->GetSectionList());
-          range.SetByteSize(range_ptr->GetByteSize());
-          return true;
-        }
-      }
+    if (uint32_t idx = GetRangeIndexContainingAddress(addr);
+        idx != UINT32_MAX) {
+      const Range *range_ptr = m_ranges.GetEntryAtIndex(idx);
+      assert(range_ptr);
+
+      range.GetBaseAddress() = function->GetAddress();
+      range.GetBaseAddress().Slide(range_ptr->GetRangeBase());
----------------
labath wrote:

`function->GetAddress()` returns the address of the start of the function. The address is internally stored as a section+offset pair, but for the most part, you can think of it as an absolute address of the function. Block ranges are internally stored as pairs of integers (offset+size), where the offset is relative to the start of the function. This converts the internal (implicitly relative to section start) representation of the range to the external, absolute (well, section-relative) representation.
It basically does that by adjusting the section offset, but it actually does that incorrectly since different parts of the function may be in different sections. So, the correct thing to do is to convert the address into a file address (which is relative to the "base address" of the file -- what ELF would call "virtual address"), do the subtraction there, and then re-resolve the result into a section-relative address.

My updated version will do that.

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


More information about the lldb-commits mailing list