[lldb] Invert relationship between Process and AddressableBits (#85858)

AddressableBits is in the Utility module of LLDB. It currently directly
refers to Process, which is from the Target LLDB module. This is a
layering violation which concretely means that it is impossible to link
anything that uses Utility without it also using Target as well. This is
generally not an issue for LLDB (since everything is built together) but
it may make it difficult to write unit tests for AddressableBits later
on.

GitOrigin-RevId: 10b0e355372fab1f4d585555536525545eef8523
diff --git a/include/lldb/Target/Process.h b/include/lldb/Target/Process.h
index e260e1b..2f3a3c2 100644
--- a/include/lldb/Target/Process.h
+++ b/include/lldb/Target/Process.h
@@ -43,6 +43,7 @@
 #include "lldb/Target/ThreadList.h"
 #include "lldb/Target/ThreadPlanStack.h"
 #include "lldb/Target/Trace.h"
+#include "lldb/Utility/AddressableBits.h"
 #include "lldb/Utility/ArchSpec.h"
 #include "lldb/Utility/Broadcaster.h"
 #include "lldb/Utility/Event.h"
@@ -3219,6 +3220,8 @@
 
   void LoadOperatingSystemPlugin(bool flush);
 
+  void SetAddressableBitMasks(AddressableBits bit_masks);
+
 private:
   Status DestroyImpl(bool force_kill);
 
diff --git a/include/lldb/Utility/AddressableBits.h b/include/lldb/Utility/AddressableBits.h
index 75752fc..0d27c35 100644
--- a/include/lldb/Utility/AddressableBits.h
+++ b/include/lldb/Utility/AddressableBits.h
@@ -32,11 +32,13 @@
 
   void SetLowmemAddressableBits(uint32_t lowmem_addressing_bits);
 
+  uint32_t GetLowmemAddressableBits() const;
+
   void SetHighmemAddressableBits(uint32_t highmem_addressing_bits);
 
-  static lldb::addr_t AddressableBitToMask(uint32_t addressable_bits);
+  uint32_t GetHighmemAddressableBits() const;
 
-  void SetProcessMasks(lldb_private::Process &process);
+  static lldb::addr_t AddressableBitToMask(uint32_t addressable_bits);
 
 private:
   uint32_t m_low_memory_addr_bits;
diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index 5b9a9d7..871683a 100644
--- a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -900,7 +900,7 @@
   }
 
   AddressableBits addressable_bits = m_gdb_comm.GetAddressableBits();
-  addressable_bits.SetProcessMasks(*this);
+  SetAddressableBitMasks(addressable_bits);
 
   if (process_arch.IsValid()) {
     const ArchSpec &target_arch = GetTarget().GetArchitecture();
@@ -2337,7 +2337,7 @@
       }
     }
 
-    addressable_bits.SetProcessMasks(*this);
+    SetAddressableBitMasks(addressable_bits);
 
     ThreadSP thread_sp = SetThreadStopInfo(
         tid, expedited_register_map, signo, thread_name, reason, description,
diff --git a/source/Plugins/Process/mach-core/ProcessMachCore.cpp b/source/Plugins/Process/mach-core/ProcessMachCore.cpp
index 7b9938d..1da7696 100644
--- a/source/Plugins/Process/mach-core/ProcessMachCore.cpp
+++ b/source/Plugins/Process/mach-core/ProcessMachCore.cpp
@@ -574,7 +574,7 @@
   CleanupMemoryRegionPermissions();
 
   AddressableBits addressable_bits = core_objfile->GetAddressableBits();
-  addressable_bits.SetProcessMasks(*this);
+  SetAddressableBitMasks(addressable_bits);
 
   return error;
 }
diff --git a/source/Target/Process.cpp b/source/Target/Process.cpp
index 6d58873..f02ec37 100644
--- a/source/Target/Process.cpp
+++ b/source/Target/Process.cpp
@@ -63,6 +63,7 @@
 #include "lldb/Target/ThreadPlanCallFunction.h"
 #include "lldb/Target/ThreadPlanStack.h"
 #include "lldb/Target/UnixSignals.h"
+#include "lldb/Utility/AddressableBits.h"
 #include "lldb/Utility/Event.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
@@ -6453,3 +6454,25 @@
 
   return Status(); // Success!
 }
+
+void Process::SetAddressableBitMasks(AddressableBits bit_masks) {
+  uint32_t low_memory_addr_bits = bit_masks.GetLowmemAddressableBits();
+  uint32_t high_memory_addr_bits = bit_masks.GetHighmemAddressableBits();
+
+  if (low_memory_addr_bits == 0 && high_memory_addr_bits == 0)
+    return;
+
+  if (low_memory_addr_bits != 0) {
+    addr_t low_addr_mask =
+        AddressableBits::AddressableBitToMask(low_memory_addr_bits);
+    SetCodeAddressMask(low_addr_mask);
+    SetDataAddressMask(low_addr_mask);
+  }
+
+  if (high_memory_addr_bits != 0) {
+    addr_t high_addr_mask =
+        AddressableBits::AddressableBitToMask(high_memory_addr_bits);
+    SetHighmemCodeAddressMask(high_addr_mask);
+    SetHighmemDataAddressMask(high_addr_mask);
+  }
+}
diff --git a/source/Utility/AddressableBits.cpp b/source/Utility/AddressableBits.cpp
index 7f9d7ec..4c98add 100644
--- a/source/Utility/AddressableBits.cpp
+++ b/source/Utility/AddressableBits.cpp
@@ -7,9 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Utility/AddressableBits.h"
-#include "lldb/Target/Process.h"
 #include "lldb/lldb-types.h"
 
+#include <cassert>
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -28,11 +29,19 @@
   m_low_memory_addr_bits = lowmem_addressing_bits;
 }
 
+uint32_t AddressableBits::GetLowmemAddressableBits() const {
+  return m_low_memory_addr_bits;
+}
+
 void AddressableBits::SetHighmemAddressableBits(
     uint32_t highmem_addressing_bits) {
   m_high_memory_addr_bits = highmem_addressing_bits;
 }
 
+uint32_t AddressableBits::GetHighmemAddressableBits() const {
+  return m_high_memory_addr_bits;
+}
+
 addr_t AddressableBits::AddressableBitToMask(uint32_t addressable_bits) {
   assert(addressable_bits <= sizeof(addr_t) * 8);
   if (addressable_bits == 64)
@@ -40,20 +49,3 @@
   else
     return ~((1ULL << addressable_bits) - 1);
 }
-
-void AddressableBits::SetProcessMasks(Process &process) {
-  if (m_low_memory_addr_bits == 0 && m_high_memory_addr_bits == 0)
-    return;
-
-  if (m_low_memory_addr_bits != 0) {
-    addr_t low_addr_mask = AddressableBitToMask(m_low_memory_addr_bits);
-    process.SetCodeAddressMask(low_addr_mask);
-    process.SetDataAddressMask(low_addr_mask);
-  }
-
-  if (m_high_memory_addr_bits != 0) {
-    addr_t hi_addr_mask = AddressableBitToMask(m_high_memory_addr_bits);
-    process.SetHighmemCodeAddressMask(hi_addr_mask);
-    process.SetHighmemDataAddressMask(hi_addr_mask);
-  }
-}