[Lldb-commits] [PATCH] D139453: Switch to a different library-loaded notification function breakpoint in Darwin dyld

Jason Molenda via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Dec 6 12:54:56 PST 2022


jasonmolenda created this revision.
jasonmolenda added reviewers: jingham, JDevlieghere.
jasonmolenda added a project: LLDB.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
jasonmolenda requested review of this revision.
Herald added a subscriber: lldb-commits.

Darwin's dynamic linker, dyld, calls a no-op function whenever libraries are loaded or removed from a process.  There are two of these functions - one for older compatibility, which lldb is using, and actual one it calls first.

In a future patch, I'll change how lldb discovers the address of this function.  lldb is currently not using the "canonical" one that I'll discover that way, and their arguments are slightly different, so this patch lays the groundwork by switching to this canonical notification function with the same discovery mechanism.

The important difference between these two is that the old function (lldb currently uses) is passed an array of uint64_t addresses of binary mach-o headers.  In the new, canonical function, the headers are passed as an array of ptrsize -- so 32-bits, on a target like arm64_32 which is ILP32 (we don't have any genuine 32-bit targets that we still support).  Beyond that, it's only a different function name.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139453

Files:
  lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp


Index: lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp
===================================================================
--- lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp
+++ lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp
@@ -206,10 +206,15 @@
                                              lldb::user_id_t break_loc_id) {
   // Let the event know that the images have changed
   // DYLD passes three arguments to the notification breakpoint.
-  // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove
-  // all Arg2: unsigned long icount        - Number of shared libraries
-  // added/removed Arg3: uint64_t mach_headers[]     - Array of load addresses
-  // of binaries added/removed
+  // 
+  // Arg1: enum dyld_notify_mode mode
+  // 0 = adding, 1 = removing, 2 = remove all, 3 = dyld moved
+  //
+  // Arg2: unsigned long count
+  // Number of shared libraries added/removed 
+  //
+  // Arg3: struct dyld_image_info mach_headers[]
+  // Array of load addresses of binaries added/removed
 
   DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton;
 
@@ -239,9 +244,10 @@
     ValueList argument_values;
 
     Value mode_value;    // enum dyld_notify_mode { dyld_notify_adding=0,
-                         // dyld_notify_removing=1, dyld_notify_remove_all=2 };
+                         // dyld_notify_removing=1, dyld_notify_remove_all=2,
+                         // dyld_notify_dyld_moved=3 };
     Value count_value;   // unsigned long count
-    Value headers_value; // uint64_t machHeaders[] (aka void*)
+    Value headers_value; // struct dyld_image_info machHeaders[]
 
     CompilerType clang_void_ptr_type =
         clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
@@ -270,6 +276,9 @@
     argument_values.PushValue(count_value);
     argument_values.PushValue(headers_value);
 
+    // void lldb_image_notifier(enum dyld_image_mode mode, uint32_t infoCount,
+    // const dyld_image_info info[])
+
     if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) {
       uint32_t dyld_mode =
           argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1);
@@ -283,11 +292,26 @@
               argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1);
           if (header_array != static_cast<uint64_t>(-1)) {
             std::vector<addr_t> image_load_addresses;
+
+            // struct dyld_image_info_32 {
+            //     uint32_t                    imageLoadAddress;
+            //     uint32_t                    imageFilePath;
+            //     uint32_t                    imageFileModDate;
+            // };
+            // struct dyld_image_info_64 {
+            //     uint64_t                    imageLoadAddress;
+            //     uint64_t                    imageFilePath;
+            //     uint64_t                    imageFileModDate;
+            // };
+
+            int addr_size =
+                process->GetTarget().GetArchitecture().GetAddressByteSize();
             for (uint64_t i = 0; i < image_infos_count; i++) {
               Status error;
-              addr_t addr = process->ReadUnsignedIntegerFromMemory(
-                  header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error);
-              if (addr != LLDB_INVALID_ADDRESS) {
+              addr_t dyld_image_info = header_array + (3 * addr_size * i);
+              addr_t addr =
+                  process->ReadPointerFromMemory(dyld_image_info, error);
+              if (error.Success()) {
                 image_load_addresses.push_back(addr);
               }
             }
@@ -402,9 +426,9 @@
       Breakpoint *breakpoint =
           m_process->GetTarget()
               .CreateBreakpoint(&dyld_filelist, source_files,
-                                "_dyld_debugger_notification",
-                                eFunctionNameTypeFull, eLanguageTypeC, 0,
-                                skip_prologue, internal, hardware)
+                                "lldb_image_notifier", eFunctionNameTypeFull,
+                                eLanguageTypeC, 0, skip_prologue, internal,
+                                hardware)
               .get();
       breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this,
                               true);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139453.480582.patch
Type: text/x-patch
Size: 4332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20221206/3ab8708d/attachment.bin>


More information about the lldb-commits mailing list