[PATCH 09/10] Include the functions of qxfer_libraries_svr4 in netbsd_process_target

Kamil Rytarowski n54@gmx.com
Fri Oct 2 02:18:03 GMT 2020


Merge get_phdr_phnum_from_proc_auxv, get_dynamic, get_r_debug and read_one_ptr
in the netbsd_process_target class. All of them are called indirectly
from the public target method qxfer_libraries_svr4.

For code simplicity, merge netbsd_qxfer_libraries_svr4 into
netbsd_process_target::qxfer_libraries_svr4.

gdbserver/ChangeLog:

        * netbsd-low.cc (get_phdr_phnum_from_proc_auxv): Turn into...
        (netbsd_process_target::get_phdr_phnum_from_proc_auxv): ...this.
        (get_dynamic): Turn into...
        (netbsd_process_target::get_dynamic): ...this.
        (get_r_debug): Turn into...
        (netbsd_process_target::get_r_debug): ...this.
        (read_one_ptr): Turn into...
        (netbsd_process_target::read_one_ptr): ...this.
        (netbsd_qxfer_libraries_svr4): Merge into...
        (netbsd_process_target::qxfer_libraries_svr4): ...this.
        * netbsd-low.h
        (netbsd_process_target::get_phdr_phnum_from_proc_auxv)
        (netbsd_process_target::get_dynamic)
        (netbsd_process_target::get_r_debug)
        (netbsd_process_target::read_one_ptr): Add.
---
 gdbserver/ChangeLog     |  18 +++++
 gdbserver/netbsd-low.cc | 161 ++++++++++++++++++----------------------
 gdbserver/netbsd-low.h  |  20 +++++
 3 files changed, 111 insertions(+), 88 deletions(-)

diff --git a/gdbserver/ChangeLog b/gdbserver/ChangeLog
index d81d961432a..0c1c40e0581 100644
--- a/gdbserver/ChangeLog
+++ b/gdbserver/ChangeLog
@@ -1,3 +1,21 @@
+2020-10-01  Kamil Rytarowski  <n54@gmx.com>
+
+	* netbsd-low.cc (get_phdr_phnum_from_proc_auxv): Turn into...
+	(netbsd_process_target::get_phdr_phnum_from_proc_auxv): ...this.
+	(get_dynamic): Turn into...
+	(netbsd_process_target::get_dynamic): ...this.
+	(get_r_debug): Turn into...
+	(netbsd_process_target::get_r_debug): ...this.
+	(read_one_ptr): Turn into...
+	(netbsd_process_target::read_one_ptr): ...this.
+	(netbsd_qxfer_libraries_svr4): Merge into...
+	(netbsd_process_target::qxfer_libraries_svr4): ...this.
+	* netbsd-low.h
+	(netbsd_process_target::get_phdr_phnum_from_proc_auxv)
+	(netbsd_process_target::get_dynamic)
+	(netbsd_process_target::get_r_debug)
+	(netbsd_process_target::read_one_ptr): Add.
+
 2020-10-01  Kamil Rytarowski  <n54@gmx.com>

 	* netbsd-low.cc (netbsd_catch_this_syscall): Turn into...
diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc
index 9a55bd42713..f1011cdfe73 100644
--- a/gdbserver/netbsd-low.cc
+++ b/gdbserver/netbsd-low.cc
@@ -798,11 +798,13 @@ netbsd_process_target::supports_disable_randomization ()
   return false;
 }

-/* Extract &phdr and num_phdr in the inferior.  Return 0 on success.  */
+/* See netbsd-low.h.  */

 template <typename T>
-int get_phdr_phnum_from_proc_auxv (const pid_t pid,
-				   CORE_ADDR *phdr_memaddr, int *num_phdr)
+int
+netbsd_process_target::get_phdr_phnum_from_proc_auxv (const pid_t pid,
+						      CORE_ADDR *phdr_memaddr,
+						      int *num_phdr)
 {
   typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
 				    Aux64Info, Aux32Info>::type auxv_type;
@@ -848,11 +850,11 @@ int get_phdr_phnum_from_proc_auxv (const pid_t pid,
   return 0;
 }

-/* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present.  */
+/* See netbsd-low.h.  */

 template <typename T>
-static CORE_ADDR
-get_dynamic (netbsd_process_target *target, const pid_t pid)
+CORE_ADDR
+netbsd_process_target::get_dynamic (const pid_t pid)
 {
   typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
 				    Elf64_Phdr, Elf32_Phdr>::type phdr_type;
@@ -866,7 +868,7 @@ get_dynamic (netbsd_process_target *target, const pid_t pid)
   std::vector<unsigned char> phdr_buf;
   phdr_buf.resize (num_phdr * phdr_size);

-  if (target->read_memory (phdr_memaddr, phdr_buf.data (), phdr_buf.size ()))
+  if (read_memory (phdr_memaddr, phdr_buf.data (), phdr_buf.size ()))
     return 0;

   /* Compute relocation: it is expected to be 0 for "regular" executables,
@@ -906,14 +908,11 @@ get_dynamic (netbsd_process_target *target, const pid_t pid)
   return 0;
 }

-/* Return &_r_debug in the inferior, or -1 if not present.  Return value
-   can be 0 if the inferior does not yet have the library list initialized.
-   We look for DT_MIPS_RLD_MAP first.  MIPS executables use this instead of
-   DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too.  */
+/* See netbsd-low.h.  */

 template <typename T>
-static CORE_ADDR
-get_r_debug (netbsd_process_target *target, const int pid)
+CORE_ADDR
+netbsd_process_target::get_r_debug (const int pid)
 {
   typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
 				    Elf64_Dyn, Elf32_Dyn>::type dyn_type;
@@ -921,11 +920,11 @@ get_r_debug (netbsd_process_target *target, const int pid)
   unsigned char buf[sizeof (dyn_type)];  /* The larger of the two.  */
   CORE_ADDR map = -1;

-  CORE_ADDR dynamic_memaddr = get_dynamic<T> (target, pid);
+  CORE_ADDR dynamic_memaddr = get_dynamic<T> (pid);
   if (dynamic_memaddr == 0)
     return map;

-  while (target->read_memory (dynamic_memaddr, buf, dyn_size) == 0)
+  while (read_memory (dynamic_memaddr, buf, dyn_size) == 0)
     {
       dyn_type *const dyn = (dyn_type *) buf;
 #if defined DT_MIPS_RLD_MAP
@@ -958,11 +957,11 @@ get_r_debug (netbsd_process_target *target, const int pid)
   return map;
 }

-/* Read one pointer from MEMADDR in the inferior.  */
+/* See netbsd-low.h.  */

-static int
-read_one_ptr (netbsd_process_target *target, CORE_ADDR memaddr, CORE_ADDR *ptr,
-	      int ptr_size)
+int
+netbsd_process_target::read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr,
+				     int ptr_size)
 {
   /* Go through a union so this works on either big or little endian
      hosts, when the inferior's pointer size is smaller than the size
@@ -976,7 +975,7 @@ read_one_ptr (netbsd_process_target *target, CORE_ADDR memaddr, CORE_ADDR *ptr,
     unsigned char uc;
   } addr;

-  int ret = target->read_memory (memaddr, &addr.uc, ptr_size);
+  int ret = read_memory (memaddr, &addr.uc, ptr_size);
   if (ret == 0)
     {
       if (ptr_size == sizeof (CORE_ADDR))
@@ -989,15 +988,39 @@ read_one_ptr (netbsd_process_target *target, CORE_ADDR memaddr, CORE_ADDR *ptr,
   return ret;
 }

+/* See netbsd-low.h.  */
+
+bool
+netbsd_process_target::elf_64_file_p (const char *file)
+{
+  int fd = gdb::handle_eintr<int> (-1, ::open, file, O_RDONLY);
+  if (fd < 0)
+    perror_with_name (("open"));
+
+  Elf64_Ehdr header;
+  ssize_t ret = gdb::handle_eintr<ssize_t> (-1, ::read, fd, &header, sizeof (header));
+  if (ret == -1)
+    perror_with_name (("read"));
+  gdb::handle_eintr<int> (-1, ::close, fd);
+  if (ret != sizeof (header))
+    error ("Cannot read ELF file header: %s", file);
+
+  if (header.e_ident[EI_MAG0] != ELFMAG0
+      || header.e_ident[EI_MAG1] != ELFMAG1
+      || header.e_ident[EI_MAG2] != ELFMAG2
+      || header.e_ident[EI_MAG3] != ELFMAG3)
+    error ("Unrecognized ELF file header: %s", file);
+
+  return header.e_ident[EI_CLASS] == ELFCLASS64;
+}
+
 /* Construct qXfer:libraries-svr4:read reply.  */

-template <typename T>
 int
-netbsd_qxfer_libraries_svr4 (netbsd_process_target *target,
-			     const pid_t pid, const char *annex,
-			     unsigned char *readbuf,
-			     unsigned const char *writebuf,
-			     CORE_ADDR offset, int len)
+netbsd_process_target::qxfer_libraries_svr4 (const char *annex,
+					     unsigned char *readbuf,
+					     unsigned const char *writebuf,
+					     CORE_ADDR offset, int len)
 {
   struct link_map_offsets
   {
@@ -1049,10 +1072,18 @@ netbsd_qxfer_libraries_svr4 (netbsd_process_target *target,
   CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
   int header_done = 0;

+  if (writebuf != nullptr)
+    return -2;
+  if (readbuf == nullptr)
+    return -1;
+
+  struct process_info *proc = current_process ();
+  pid_t pid = proc->pid;
+  bool is_elf64 = elf_64_file_p (netbsd_nat::pid_to_exec_file (pid));
+
   const struct link_map_offsets *lmo
-    = ((sizeof (T) == sizeof (int64_t))
-       ? &lmo_64bit_offsets : &lmo_32bit_offsets);
-  int ptr_size = sizeof (T);
+    = (is_elf64 ? &lmo_64bit_offsets : &lmo_32bit_offsets);
+  int ptr_size = is_elf64 ? sizeof (int64_t) : sizeof (int32_t);

   while (annex[0] != '\0')
     {
@@ -1080,7 +1111,12 @@ netbsd_qxfer_libraries_svr4 (netbsd_process_target *target,

   if (lm_addr == 0)
     {
-      CORE_ADDR r_debug = get_r_debug<T> (target, pid);
+      CORE_ADDR r_debug;
+
+      if (is_elf64)
+	r_debug = get_r_debug<int64_t> (pid);
+      else
+	r_debug = get_r_debug<int32_t> (pid);

       /* We failed to find DT_DEBUG.  Such situation will not change
 	 for this inferior - do not retry it.  Report it to GDB as
@@ -1091,7 +1127,7 @@ netbsd_qxfer_libraries_svr4 (netbsd_process_target *target,
       if (r_debug != 0)
 	{
 	  CORE_ADDR map_offset = r_debug + lmo->r_map_offset;
-	  if (read_one_ptr (target, map_offset, &lm_addr, ptr_size) != 0)
+	  if (read_one_ptr (map_offset, &lm_addr, ptr_size) != 0)
 	    warning ("unable to read r_map from %s",
 		     core_addr_to_string (map_offset));
 	}
@@ -1100,15 +1136,15 @@ netbsd_qxfer_libraries_svr4 (netbsd_process_target *target,
   std::string document = "<library-list-svr4 version=\"1.0\"";

   while (lm_addr
-	 && read_one_ptr (target, lm_addr + lmo->l_name_offset,
+	 && read_one_ptr (lm_addr + lmo->l_name_offset,
 			  &l_name, ptr_size) == 0
-	 && read_one_ptr (target, lm_addr + lmo->l_addr_offset,
+	 && read_one_ptr (lm_addr + lmo->l_addr_offset,
 			  &l_addr, ptr_size) == 0
-	 && read_one_ptr (target, lm_addr + lmo->l_ld_offset,
+	 && read_one_ptr (lm_addr + lmo->l_ld_offset,
 			  &l_ld, ptr_size) == 0
-	 && read_one_ptr (target, lm_addr + lmo->l_prev_offset,
+	 && read_one_ptr (lm_addr + lmo->l_prev_offset,
 			  &l_prev, ptr_size) == 0
-	 && read_one_ptr (target, lm_addr + lmo->l_next_offset,
+	 && read_one_ptr (lm_addr + lmo->l_next_offset,
 			  &l_next, ptr_size) == 0)
     {
       if (lm_prev != l_prev)
@@ -1134,7 +1170,7 @@ netbsd_qxfer_libraries_svr4 (netbsd_process_target *target,
 	  /* Not checking for error because reading may stop before
 	     we've got PATH_MAX worth of characters.  */
 	  libname[0] = '\0';
-	  target->read_memory (l_name, libname, sizeof (libname) - 1);
+	  read_memory (l_name, libname, sizeof (libname) - 1);
 	  libname[sizeof (libname) - 1] = '\0';
 	  if (libname[0] != '\0')
 	    {
@@ -1179,57 +1215,6 @@ netbsd_qxfer_libraries_svr4 (netbsd_process_target *target,
   return len;
 }

-/* See netbsd-low.h.  */
-
-bool
-netbsd_process_target::elf_64_file_p (const char *file)
-{
-  int fd = gdb::handle_eintr<int> (-1, ::open, file, O_RDONLY);
-  if (fd < 0)
-    perror_with_name (("open"));
-
-  Elf64_Ehdr header;
-  ssize_t ret = gdb::handle_eintr<ssize_t> (-1, ::read, fd, &header, sizeof (header));
-  if (ret == -1)
-    perror_with_name (("read"));
-  gdb::handle_eintr<int> (-1, ::close, fd);
-  if (ret != sizeof (header))
-    error ("Cannot read ELF file header: %s", file);
-
-  if (header.e_ident[EI_MAG0] != ELFMAG0
-      || header.e_ident[EI_MAG1] != ELFMAG1
-      || header.e_ident[EI_MAG2] != ELFMAG2
-      || header.e_ident[EI_MAG3] != ELFMAG3)
-    error ("Unrecognized ELF file header: %s", file);
-
-  return header.e_ident[EI_CLASS] == ELFCLASS64;
-}
-
-/* Construct qXfer:libraries-svr4:read reply.  */
-
-int
-netbsd_process_target::qxfer_libraries_svr4 (const char *annex,
-					     unsigned char *readbuf,
-					     unsigned const char *writebuf,
-					     CORE_ADDR offset, int len)
-{
-  if (writebuf != nullptr)
-    return -2;
-  if (readbuf == nullptr)
-    return -1;
-
-  struct process_info *proc = current_process ();
-  pid_t pid = proc->pid;
-  bool is_elf64 = elf_64_file_p (netbsd_nat::pid_to_exec_file (pid));
-
-  if (is_elf64)
-    return netbsd_qxfer_libraries_svr4<int64_t> (this, pid, annex, readbuf,
-						 writebuf, offset, len);
-  else
-    return netbsd_qxfer_libraries_svr4<int32_t> (this, pid, annex, readbuf,
-						 writebuf, offset, len);
-}
-
 /* Implement the supports_qxfer_libraries_svr4 target_ops method.  */

 bool
diff --git a/gdbserver/netbsd-low.h b/gdbserver/netbsd-low.h
index 7ed08dc90fa..338293d74a6 100644
--- a/gdbserver/netbsd-low.h
+++ b/gdbserver/netbsd-low.h
@@ -156,6 +156,26 @@ class netbsd_process_target : public process_stratum_target
   /* Returns true if GDB is interested in the reported SYSNO syscall.  */
   bool netbsd_catch_this_syscall (int sysno);

+  /* Extract &phdr and num_phdr in the inferior.  Return 0 on success.  */
+  template <typename T> int
+  get_phdr_phnum_from_proc_auxv (const pid_t pid,
+				 CORE_ADDR *phdr_memaddr,
+				 int *num_phdr);
+
+  /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present.  */
+  template <typename T> CORE_ADDR
+  get_dynamic (const pid_t pid);
+
+  /* Return &_r_debug in the inferior, or -1 if not present.  Return value
+     can be 0 if the inferior does not yet have the library list initialized.
+     We look for DT_MIPS_RLD_MAP first.  MIPS executables use this instead of
+     DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too.  */
+  template <typename T>
+  CORE_ADDR get_r_debug (const int pid);
+
+  /* Read one pointer from MEMADDR in the inferior.  */
+  int read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size);
+
 protected:
   /* The architecture-specific "low" methods are listed below.  */

--
2.28.0



More information about the Gdb-patches mailing list