[Lldb-commits] [lldb] [lldb] Scope symbol lookups to specific modules in ObjC/SystemRuntime plugins (PR #193379)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Tue Apr 21 21:54:39 PDT 2026


https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/193379

>From d3b5fd2957462b7d9ab2599df636dc5f40ecac3a Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Tue, 21 Apr 2026 18:29:47 -0700
Subject: [PATCH] [lldb] Scope symbol lookups to specific modules in
 ObjC/SystemRuntime plugins

This narrows `FindSymbolsWithNameAndType` calls from searching all loaded
images to the specific module that owns the symbol (Foundation,
CoreFoundation, libBacktraceRecording.dylib).

The arclite fallback in `CalculateHasNewLiteralsAndIndexing` still
searches all images because libarclite is a static library linked into
the main executable.
---
 .../AppleObjCRuntime/AppleObjCRuntime.cpp     | 22 ++++++++---
 .../AppleObjCRuntime/AppleObjCRuntimeV2.cpp   | 39 ++++++++++---------
 .../MacOSX/SystemRuntimeMacOSX.cpp            | 13 +++++--
 3 files changed, 45 insertions(+), 29 deletions(-)

diff --git a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
index ad60290382c02..6c4c1b6d877c8 100644
--- a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
@@ -459,20 +459,30 @@ bool AppleObjCRuntime::CalculateHasNewLiteralsAndIndexing() {
   if (!m_process)
     return false;
 
-  Target &target(m_process->GetTarget());
+  Target &target = m_process->GetTarget();
 
   static ConstString s_method_signature(
       "-[NSDictionary objectForKeyedSubscript:]");
   static ConstString s_arclite_method_signature(
       "__arclite_objectForKeyedSubscript");
+  // NSDictionary is toll-free bridged with CFDictionary, so the
+  // implementation lives in CoreFoundation, not Foundation.
+  static ModuleSpec corefoundation_module_spec(FileSpec("CoreFoundation"));
+
+  if (ModuleSP corefoundation_module_sp =
+          target.GetImages().FindFirstModule(corefoundation_module_spec)) {
+    const Symbol *method_symbol =
+        corefoundation_module_sp->FindFirstSymbolWithNameAndType(
+            s_method_signature, eSymbolTypeCode);
+    if (method_symbol)
+      return true;
+  }
 
+  // The arclite variant is a static library linked into the main executable,
+  // not part of CoreFoundation, so search all images.
   SymbolContextList sc_list;
-
-  target.GetImages().FindSymbolsWithNameAndType(s_method_signature,
+  target.GetImages().FindSymbolsWithNameAndType(s_arclite_method_signature,
                                                 eSymbolTypeCode, sc_list);
-  if (sc_list.IsEmpty())
-    target.GetImages().FindSymbolsWithNameAndType(s_arclite_method_signature,
-                                                  eSymbolTypeCode, sc_list);
   return !sc_list.IsEmpty();
 }
 
diff --git a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp
index 305c8dd24332d..23cf460f2298f 100644
--- a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp
@@ -3481,28 +3481,29 @@ bool AppleObjCRuntimeV2::GetCFBooleanValuesIfNeeded() {
   static ConstString g_kCFBooleanFalse("kCFBooleanFalse");
   static ConstString g_kCFBooleanTrue("kCFBooleanTrue");
 
-  std::function<lldb::addr_t(ConstString, ConstString)> get_symbol =
-      [this](ConstString sym, ConstString real_sym) -> lldb::addr_t {
-    SymbolContextList sc_list;
-    GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType(
-        sym, lldb::eSymbolTypeData, sc_list);
-    if (sc_list.GetSize() == 1) {
-      SymbolContext sc;
-      sc_list.GetContextAtIndex(0, sc);
-      if (sc.symbol)
-        return sc.symbol->GetLoadAddress(&GetProcess()->GetTarget());
-    }
-    GetProcess()->GetTarget().GetImages().FindSymbolsWithNameAndType(
-        real_sym, lldb::eSymbolTypeData, sc_list);
-    if (sc_list.GetSize() != 1)
-      return LLDB_INVALID_ADDRESS;
+  static ModuleSpec corefoundation_module_spec(FileSpec("CoreFoundation"));
+
+  ModuleSP corefoundation_module_sp =
+      GetProcess()->GetTarget().GetImages().FindFirstModule(
+          corefoundation_module_spec);
+
+  if (!corefoundation_module_sp)
+    return false;
 
-    SymbolContext sc;
-    sc_list.GetContextAtIndex(0, sc);
-    if (!sc.symbol)
+  auto get_symbol = [this, &corefoundation_module_sp](
+                        ConstString sym, ConstString real_sym) -> lldb::addr_t {
+    const Symbol *symbol =
+        corefoundation_module_sp->FindFirstSymbolWithNameAndType(
+            sym, lldb::eSymbolTypeData);
+    if (symbol)
+      return symbol->GetLoadAddress(&GetProcess()->GetTarget());
+
+    symbol = corefoundation_module_sp->FindFirstSymbolWithNameAndType(
+        real_sym, lldb::eSymbolTypeData);
+    if (!symbol)
       return LLDB_INVALID_ADDRESS;
 
-    lldb::addr_t addr = sc.symbol->GetLoadAddress(&GetProcess()->GetTarget());
+    lldb::addr_t addr = symbol->GetLoadAddress(&GetProcess()->GetTarget());
     Status error;
     addr = GetProcess()->ReadPointerFromMemory(addr, error);
     if (error.Fail())
diff --git a/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp b/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
index 5dea97cec5b71..728239230a6fb 100644
--- a/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
+++ b/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
@@ -620,10 +620,15 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
   addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS;
   Target &target = m_process->GetTarget();
 
+  ModuleSpec lookup_spec(FileSpec("libBacktraceRecording.dylib"));
+  ModuleSP module_sp(target.GetImages().FindFirstModule(lookup_spec));
+  if (!module_sp)
+    return false;
+
   static ConstString introspection_dispatch_queue_info_version(
       "__introspection_dispatch_queue_info_version");
   SymbolContextList sc_list;
-  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
+  module_sp->FindSymbolsWithNameAndType(
       introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list);
   if (!sc_list.IsEmpty()) {
     SymbolContext sc;
@@ -635,7 +640,7 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
 
   static ConstString introspection_dispatch_queue_info_data_offset(
       "__introspection_dispatch_queue_info_data_offset");
-  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
+  module_sp->FindSymbolsWithNameAndType(
       introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list);
   if (!sc_list.IsEmpty()) {
     SymbolContext sc;
@@ -647,7 +652,7 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
 
   static ConstString introspection_dispatch_item_info_version(
       "__introspection_dispatch_item_info_version");
-  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
+  module_sp->FindSymbolsWithNameAndType(
       introspection_dispatch_item_info_version, eSymbolTypeData, sc_list);
   if (!sc_list.IsEmpty()) {
     SymbolContext sc;
@@ -659,7 +664,7 @@ bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() {
 
   static ConstString introspection_dispatch_item_info_data_offset(
       "__introspection_dispatch_item_info_data_offset");
-  m_process->GetTarget().GetImages().FindSymbolsWithNameAndType(
+  module_sp->FindSymbolsWithNameAndType(
       introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list);
   if (!sc_list.IsEmpty()) {
     SymbolContext sc;



More information about the lldb-commits mailing list