jasonmolenda updated this revision to Diff 550148.
jasonmolenda added a comment.

Incorporate Jonas' suggestion of a AddressableBits class that 
GDBRemoteCommunicationClient and ObjectFileMachO could use to store the 
zero/one/two addressable bits values back to a Process, and centralizing the 
logic for how those 0-2 values are used to set the Process address masks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157667/new/

https://reviews.llvm.org/D157667

Files:
  lldb/docs/lldb-gdb-remote.txt
  lldb/include/lldb/Symbol/ObjectFile.h
  lldb/include/lldb/Utility/AddressableBits.h
  lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
  lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
  lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
  lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
  lldb/source/Utility/AddressableBits.cpp
  lldb/source/Utility/CMakeLists.txt

Index: lldb/source/Utility/CMakeLists.txt
===================================================================
--- lldb/source/Utility/CMakeLists.txt
+++ lldb/source/Utility/CMakeLists.txt
@@ -24,6 +24,7 @@
 endif()
 
 add_lldb_library(lldbUtility NO_INTERNAL_DEPENDENCIES
+  AddressableBits.cpp
   ArchSpec.cpp
   Args.cpp
   Baton.cpp
Index: lldb/source/Utility/AddressableBits.cpp
===================================================================
--- /dev/null
+++ lldb/source/Utility/AddressableBits.cpp
@@ -0,0 +1,50 @@
+//===-- AddressableBits.cpp -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Utility/AddressableBits.h"
+#include "lldb/Target/Process.h"
+#include "lldb/lldb-types.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+void AddressableBits::SetAddressableBits(uint32_t addressing_bits) {
+  m_low_memory_addr_bits = m_high_memory_addr_bits = addressing_bits;
+}
+
+void AddressableBits::SetAddressableBits(uint32_t lowmem_addressing_bits,
+                                         uint32_t highmem_addressing_bits) {
+  m_low_memory_addr_bits = lowmem_addressing_bits;
+  m_high_memory_addr_bits = highmem_addressing_bits;
+}
+
+void AddressableBits::Clear() {
+  m_low_memory_addr_bits = m_high_memory_addr_bits = 0;
+}
+
+void AddressableBits::SetProcessMasks(Process &process) {
+  // In case either value is set to 0, indicating it was not set, use the
+  // other value.
+  if (m_low_memory_addr_bits == 0)
+    m_low_memory_addr_bits = m_high_memory_addr_bits;
+  if (m_high_memory_addr_bits == 0)
+    m_high_memory_addr_bits = m_low_memory_addr_bits;
+
+  if (m_low_memory_addr_bits == 0)
+    return;
+
+  addr_t address_mask = ~((1ULL << m_low_memory_addr_bits) - 1);
+  process.SetCodeAddressMask(address_mask);
+  process.SetDataAddressMask(address_mask);
+
+  if (m_low_memory_addr_bits != m_high_memory_addr_bits) {
+    lldb::addr_t hi_address_mask = ~((1ULL << m_high_memory_addr_bits) - 1);
+    process.SetHighmemCodeAddressMask(hi_address_mask);
+    process.SetHighmemDataAddressMask(hi_address_mask);
+  }
+}
Index: lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
===================================================================
--- lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
+++ lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
@@ -509,10 +509,9 @@
 
   CleanupMemoryRegionPermissions();
 
-  addr_t address_mask = core_objfile->GetAddressMask();
-  if (address_mask != 0) {
-    SetCodeAddressMask(address_mask);
-    SetDataAddressMask(address_mask);
+  AddressableBits addressable_bits;
+  if (core_objfile->GetAddressableBits(addressable_bits)) {
+    addressable_bits.SetProcessMasks(*this);
   }
   return error;
 }
Index: lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -899,10 +899,9 @@
              process_arch.GetTriple().getTriple());
   }
 
-  if (int addressable_bits = m_gdb_comm.GetAddressingBits()) {
-    lldb::addr_t address_mask = ~((1ULL << addressable_bits) - 1);
-    SetCodeAddressMask(address_mask);
-    SetDataAddressMask(address_mask);
+  AddressableBits addressable_bits;
+  if (m_gdb_comm.GetAddressableBits(addressable_bits)) {
+    addressable_bits.SetProcessMasks(*this);
   }
 
   if (process_arch.IsValid()) {
Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
+++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
@@ -19,6 +19,7 @@
 #include <vector>
 
 #include "lldb/Host/File.h"
+#include "lldb/Utility/AddressableBits.h"
 #include "lldb/Utility/ArchSpec.h"
 #include "lldb/Utility/GDBRemote.h"
 #include "lldb/Utility/ProcessInfo.h"
@@ -237,7 +238,7 @@
 
   ArchSpec GetSystemArchitecture();
 
-  uint32_t GetAddressingBits();
+  bool GetAddressableBits(lldb_private::AddressableBits &addressable_bits);
 
   bool GetHostname(std::string &s);
 
@@ -580,7 +581,8 @@
   lldb::tid_t m_curr_tid_run = LLDB_INVALID_THREAD_ID;
 
   uint32_t m_num_supported_hardware_watchpoints = 0;
-  uint32_t m_addressing_bits = 0;
+  uint32_t m_low_mem_addressing_bits = 0;
+  uint32_t m_high_mem_addressing_bits = 0;
 
   ArchSpec m_host_arch;
   std::string m_host_distribution_id;
Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
+++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
@@ -1265,7 +1265,15 @@
             if (!value.getAsInteger(0, pointer_byte_size))
               ++num_keys_decoded;
           } else if (name.equals("addressing_bits")) {
-            if (!value.getAsInteger(0, m_addressing_bits))
+            if (!value.getAsInteger(0, m_low_mem_addressing_bits)) {
+              m_high_mem_addressing_bits = m_low_mem_addressing_bits;
+              ++num_keys_decoded;
+            }
+          } else if (name.equals("high_mem_addressing_bits")) {
+            if (!value.getAsInteger(0, m_high_mem_addressing_bits))
+              ++num_keys_decoded;
+          } else if (name.equals("low_mem_addressing_bits")) {
+            if (!value.getAsInteger(0, m_low_mem_addressing_bits))
               ++num_keys_decoded;
           } else if (name.equals("os_version") ||
                      name.equals("version")) // Older debugserver binaries used
@@ -1407,11 +1415,19 @@
   return m_host_arch;
 }
 
-uint32_t GDBRemoteCommunicationClient::GetAddressingBits() {
+bool GDBRemoteCommunicationClient::GetAddressableBits(
+    lldb_private::AddressableBits &addressable_bits) {
+  addressable_bits.Clear();
   if (m_qHostInfo_is_valid == eLazyBoolCalculate)
     GetHostInfo();
-  return m_addressing_bits;
+  if (m_low_mem_addressing_bits != 0 || m_high_mem_addressing_bits != 0) {
+    addressable_bits.SetAddressableBits(m_low_mem_addressing_bits,
+                                        m_high_mem_addressing_bits);
+    return true;
+  }
+  return false;
 }
+
 seconds GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout() {
   if (m_qHostInfo_is_valid == eLazyBoolCalculate)
     GetHostInfo();
Index: lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h
===================================================================
--- lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h
+++ lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h
@@ -122,7 +122,7 @@
 
   std::string GetIdentifierString() override;
 
-  lldb::addr_t GetAddressMask() override;
+  bool GetAddressableBits(lldb_private::AddressableBits &address_bits) override;
 
   bool GetCorefileMainBinaryInfo(lldb::addr_t &value, bool &value_is_offset,
                                  lldb_private::UUID &uuid,
Index: lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
===================================================================
--- lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -5486,8 +5486,9 @@
   return result;
 }
 
-addr_t ObjectFileMachO::GetAddressMask() {
-  addr_t mask = 0;
+bool ObjectFileMachO::GetAddressableBits(AddressableBits &address_bits) {
+  address_bits.Clear();
+
   Log *log(GetLog(LLDBLog::Process));
   ModuleSP module_sp(GetModule());
   if (module_sp) {
@@ -5514,13 +5515,24 @@
             if (version == 3) {
               uint32_t num_addr_bits = m_data.GetU32_unchecked(&offset);
               if (num_addr_bits != 0) {
-                mask = ~((1ULL << num_addr_bits) - 1);
+                address_bits.SetAddressableBits(num_addr_bits);
               }
               LLDB_LOGF(log,
-                        "LC_NOTE 'addrable bits' found, value %d bits, "
-                        "mask 0x%" PRIx64,
-                        num_addr_bits, mask);
-              break;
+                        "LC_NOTE 'addrable bits' v3 found, value %d "
+                        "bits",
+                        num_addr_bits);
+              return true;
+            }
+            if (version == 4) {
+              uint32_t lo_addr_bits = m_data.GetU32_unchecked(&offset);
+              uint32_t hi_addr_bits = m_data.GetU32_unchecked(&offset);
+
+              address_bits.SetAddressableBits(lo_addr_bits, hi_addr_bits);
+              LLDB_LOGF(log,
+                        "LC_NOTE 'addrable bits' v4 found, value %d & %d bits",
+                        lo_addr_bits, hi_addr_bits);
+
+              return true;
             }
           }
         }
@@ -5528,7 +5540,7 @@
       offset = cmd_offset + lc.cmdsize;
     }
   }
-  return mask;
+  return false;
 }
 
 bool ObjectFileMachO::GetCorefileMainBinaryInfo(addr_t &value,
@@ -6669,10 +6681,12 @@
           addrable_bits_lcnote_up->name = "addrable bits";
           addrable_bits_lcnote_up->payload_file_offset = file_offset;
           int bits = std::bitset<64>(~address_mask).count();
-          addrable_bits_lcnote_up->payload.PutHex32(3); // version
+          addrable_bits_lcnote_up->payload.PutHex32(4); // version
+          addrable_bits_lcnote_up->payload.PutHex32(
+              bits); // # of bits used for low addresses
           addrable_bits_lcnote_up->payload.PutHex32(
-              bits); // # of bits used for addressing
-          addrable_bits_lcnote_up->payload.PutHex64(0); // unused
+              bits); // # of bits used for high addresses
+          addrable_bits_lcnote_up->payload.PutHex32(0); // reserved
 
           file_offset += addrable_bits_lcnote_up->payload.GetSize();
 
Index: lldb/include/lldb/Utility/AddressableBits.h
===================================================================
--- /dev/null
+++ lldb/include/lldb/Utility/AddressableBits.h
@@ -0,0 +1,43 @@
+//===-- AddressableBits.h ---------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_UTILITY_ADDRESSABLEBITS_H
+#define LLDB_UTILITY_ADDRESSABLEBITS_H
+
+#include "lldb/lldb-forward.h"
+
+namespace lldb_private {
+
+/// \class AddressableBits AddressableBits.h "lldb/Core/AddressableBits.h"
+/// A class which holds the metadata from a remote stub/corefile note
+/// about how many bits are used for addressing on this target.
+///
+class AddressableBits {
+public:
+  AddressableBits() : m_low_memory_addr_bits(0), m_high_memory_addr_bits(0) {}
+
+  /// When a single value is available for the number of bits.
+  void SetAddressableBits(uint32_t addressing_bits);
+
+  /// When we have separate values for low memory addresses and high memory
+  /// addresses.
+  void SetAddressableBits(uint32_t lowmem_addressing_bits,
+                          uint32_t highmem_addressing_bits);
+
+  void SetProcessMasks(lldb_private::Process &process);
+
+  void Clear();
+
+private:
+  uint32_t m_low_memory_addr_bits;
+  uint32_t m_high_memory_addr_bits;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_UTILITY_ADDRESSABLEBITS_H
Index: lldb/include/lldb/Symbol/ObjectFile.h
===================================================================
--- lldb/include/lldb/Symbol/ObjectFile.h
+++ lldb/include/lldb/Symbol/ObjectFile.h
@@ -13,6 +13,7 @@
 #include "lldb/Core/PluginInterface.h"
 #include "lldb/Symbol/Symtab.h"
 #include "lldb/Symbol/UnwindTable.h"
+#include "lldb/Utility/AddressableBits.h"
 #include "lldb/Utility/DataExtractor.h"
 #include "lldb/Utility/Endian.h"
 #include "lldb/Utility/FileSpec.h"
@@ -494,13 +495,18 @@
 
   /// Some object files may have the number of bits used for addressing
   /// embedded in them, e.g. a Mach-O core file using an LC_NOTE.  These
-  /// object files can return the address mask that should be used in
-  /// the Process.
+  /// object files can return an AddressableBits object that can can be
+  /// used to set the address masks in the Process.
+  ///
+  /// \param[out] address_bits
+  ///     Can be used to set the Process address masks.
+  ///
   /// \return
-  ///     The mask will have bits set which aren't used for addressing --
-  ///     typically, the high bits.
-  ///     Zero is returned when no address bits mask is available.
-  virtual lldb::addr_t GetAddressMask() { return 0; }
+  ///     Returns true if addressable bits metadata was found.
+  virtual bool GetAddressableBits(lldb_private::AddressableBits &address_bits) {
+    address_bits.Clear();
+    return false;
+  }
 
   /// When the ObjectFile is a core file, lldb needs to locate the "binary" in
   /// the core file.  lldb can iterate over the pages looking for a valid
Index: lldb/docs/lldb-gdb-remote.txt
===================================================================
--- lldb/docs/lldb-gdb-remote.txt
+++ lldb/docs/lldb-gdb-remote.txt
@@ -978,6 +978,14 @@
 		 v8.3 ABIs that use pointer authentication, so lldb
 		 knows which bits to clear/set to get the actual
 		 addresses.
+low_mem_addressing_bits: optional, specifies how many bits in 
+     addresses in low memory are significant for addressing, base 10.  
+     AArch64 can have different page table setups for low and high
+     memory, and therefore a different number of bits used for addressing.
+high_mem_addressing_bits: optional, specifies how many bits in 
+     addresses in high memory are significant for addressing, base 10.  
+     AArch64 can have different page table setups for low and high
+     memory, and therefore a different number of bits used for addressing.
 
 //----------------------------------------------------------------------
 // "qGDBServerVersion"
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to