public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Mike Frysinger <vapier@gentoo.org>
To: gdb-patches@sourceware.org
Subject: [PATCH/committed 4/5] sim: m32r: fixup some of the int<->pointer casts
Date: Wed, 10 Jan 2024 22:37:46 -0500	[thread overview]
Message-ID: <20240111033747.17795-4-vapier@gentoo.org> (raw)
In-Reply-To: <20240111033747.17795-1-vapier@gentoo.org>

The m32r trap code was written for a 32-bit Linux host (and really, one
whose Linux ABI matched pretty exactly).  This has lead to conversions
between integers and pointers which breaks down hard on 64-bit hosts.

Clean up some of the functions where possible to avoid unnecessary
conversions, use uintptr_t to cast 32-bit target pointers to host
pointers in some places, and just stub out a few functions that can't
easily be salvaged currently when sizeof(void*) is not 32-bits.  This
is a bit ugly, but lets us enable warnings for the whole file.
---
 sim/config.h.in           |  3 ++
 sim/configure             | 39 +++++++++++++++++--
 sim/m32r/traps.c          | 81 +++++++++++++++++++++++++--------------
 sim/m4/sim_ac_platform.m4 |  2 +
 4 files changed, 94 insertions(+), 31 deletions(-)

diff --git a/sim/m32r/traps.c b/sim/m32r/traps.c
index 9ffa3bb15d94..d010863502db 100644
--- a/sim/m32r/traps.c
+++ b/sim/m32r/traps.c
@@ -436,7 +436,7 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	    break;
 
 	  case TARGET_LINUX_SYS_brk:
-	    result = brk ((void *) arg1);
+	    result = brk ((void *) (uintptr_t) arg1);
 	    errcode = errno;
 	    //result = arg1;
 	    break;
@@ -582,51 +582,51 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	    {
 	      int n;
 	      fd_set readfds;
-	      fd_set *treadfdsp;
+	      unsigned int treadfdsp;
 	      fd_set *hreadfdsp;
 	      fd_set writefds;
-	      fd_set *twritefdsp;
+	      unsigned int twritefdsp;
 	      fd_set *hwritefdsp;
 	      fd_set exceptfds;
-	      fd_set *texceptfdsp;
+	      unsigned int texceptfdsp;
 	      fd_set *hexceptfdsp;
-	      struct timeval *ttimeoutp;
+	      unsigned int ttimeoutp;
 	      struct timeval timeout;
 
 	      n = arg1;
 
-	      treadfdsp = (fd_set *) arg2;
-	      if (treadfdsp != NULL)
+	      treadfdsp = arg2;
+	      if (treadfdsp !=0)
 		{
-		  readfds = *((fd_set *) t2h_addr (cb, &s, (unsigned int) treadfdsp));
+		  readfds = *((fd_set *) t2h_addr (cb, &s, treadfdsp));
 		  translate_endian_t2h (&readfds, sizeof(readfds));
 		  hreadfdsp = &readfds;
 		}
 	      else
 		hreadfdsp = NULL;
 
-	      twritefdsp  = (fd_set *) arg3;
-	      if (twritefdsp != NULL)
+	      twritefdsp = arg3;
+	      if (twritefdsp != 0)
 		{
-		  writefds = *((fd_set *) t2h_addr (cb, &s, (unsigned int) twritefdsp));
+		  writefds = *((fd_set *) t2h_addr (cb, &s, twritefdsp));
 		  translate_endian_t2h (&writefds, sizeof(writefds));
 		  hwritefdsp = &writefds;
 		}
 	      else
 		hwritefdsp = NULL;
 
-	      texceptfdsp = (fd_set *) arg4;
-	      if (texceptfdsp != NULL)
+	      texceptfdsp = arg4;
+	      if (texceptfdsp != 0)
 		{
-		  exceptfds = *((fd_set *) t2h_addr (cb, &s, (unsigned int) texceptfdsp));
+		  exceptfds = *((fd_set *) t2h_addr (cb, &s, texceptfdsp));
 		  translate_endian_t2h (&exceptfds, sizeof(exceptfds));
 		  hexceptfdsp = &exceptfds;
 		}
 	      else
 		hexceptfdsp = NULL;
 
-	      ttimeoutp = (struct timeval *) arg5;
-	      timeout = *((struct timeval *) t2h_addr (cb, &s, (unsigned int) ttimeoutp));
+	      ttimeoutp = arg5;
+	      timeout = *((struct timeval *) t2h_addr (cb, &s, ttimeoutp));
 	      translate_endian_t2h (&timeout, sizeof(timeout));
 
 	      result = select (n, hreadfdsp, hwritefdsp, hexceptfdsp, &timeout);
@@ -635,10 +635,10 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	      if (result != 0)
 		break;
 
-	      if (treadfdsp != NULL)
+	      if (treadfdsp != 0)
 		{
 		  translate_endian_h2t (&readfds, sizeof(readfds));
-		  if ((s.write_mem) (cb, &s, (unsigned long) treadfdsp,
+		  if ((s.write_mem) (cb, &s, treadfdsp,
 		       (char *) &readfds, sizeof(readfds)) != sizeof(readfds))
 		    {
 		      result = -1;
@@ -646,10 +646,10 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 		    }
 		}
 
-	      if (twritefdsp != NULL)
+	      if (twritefdsp != 0)
 		{
 		  translate_endian_h2t (&writefds, sizeof(writefds));
-		  if ((s.write_mem) (cb, &s, (unsigned long) twritefdsp,
+		  if ((s.write_mem) (cb, &s, twritefdsp,
 		       (char *) &writefds, sizeof(writefds)) != sizeof(writefds))
 		    {
 		      result = -1;
@@ -657,10 +657,10 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 		    }
 		}
 
-	      if (texceptfdsp != NULL)
+	      if (texceptfdsp != 0)
 		{
 		  translate_endian_h2t (&exceptfds, sizeof(exceptfds));
-		  if ((s.write_mem) (cb, &s, (unsigned long) texceptfdsp,
+		  if ((s.write_mem) (cb, &s, texceptfdsp,
 		       (char *) &exceptfds, sizeof(exceptfds)) != sizeof(exceptfds))
 		    {
 		      result = -1;
@@ -669,7 +669,7 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 		}
 
 	      translate_endian_h2t (&timeout, sizeof(timeout));
-	      if ((s.write_mem) (cb, &s, (unsigned long) ttimeoutp,
+	      if ((s.write_mem) (cb, &s, ttimeoutp,
 		   (char *) &timeout, sizeof(timeout)) != sizeof(timeout))
 		{
 		  result = -1;
@@ -692,8 +692,13 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	    break;
 
 	  case TARGET_LINUX_SYS_readdir:
+#if SIZEOF_VOID_P == 4
 	    result = (int) readdir ((DIR *) t2h_addr (cb, &s, arg1));
 	    errcode = errno;
+#else
+	    result = 0;
+	    errcode = ENOSYS;
+#endif
 	    break;
 
 #if 0
@@ -714,6 +719,7 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 #endif
 	  case TARGET_LINUX_SYS_mmap2:
 	    {
+#if SIZEOF_VOID_P == 4  /* Code assumes m32r pointer size matches host.  */
 	      void *addr;
 	      size_t len;
 	      int prot, flags, fildes;
@@ -736,11 +742,16 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 				     0, access_read_write_exec, 0,
 				     result, len, 0, NULL, NULL);
 		}
+#else
+	      result = 0;
+	      errcode = ENOSYS;
+#endif
 	    }
 	    break;
 
 	  case TARGET_LINUX_SYS_mmap:
 	    {
+#if SIZEOF_VOID_P == 4  /* Code assumes m32r pointer size matches host.  */
 	      void *addr;
 	      size_t len;
 	      int prot, flags, fildes;
@@ -773,11 +784,15 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 				     0, access_read_write_exec, 0,
 				     result, len, 0, NULL, NULL);
 		}
+#else
+	      result = 0;
+	      errcode = ENOSYS;
+#endif
 	    }
 	    break;
 
 	  case TARGET_LINUX_SYS_munmap:
-	    result = munmap ((void *)arg1, arg2);
+	    result = munmap ((void *) (uintptr_t) arg1, arg2);
 	    errcode = errno;
 	    if (result != -1)
 	      sim_core_detach (sd, NULL, 0, arg2, result);
@@ -1078,7 +1093,7 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	    break;
 
 	  case TARGET_LINUX_SYS_mprotect:
-	    result = mprotect ((void *) arg1, arg2, arg3);
+	    result = mprotect ((void *) (uintptr_t) arg1, arg2, arg3);
 	    errcode = errno;
 	    break;
 
@@ -1149,7 +1164,7 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	    break;
 
 	  case TARGET_LINUX_SYS_msync:
-	    result = msync ((void *) arg1, arg2, arg3);
+	    result = msync ((void *) (uintptr_t) arg1, arg2, arg3);
 	    errcode = errno;
 	    break;
 
@@ -1216,8 +1231,13 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	    break;
 
 	  case TARGET_LINUX_SYS_mremap: /* FIXME */
+#if SIZEOF_VOID_P == 4  /* Code assumes m32r pointer size matches host.  */
 	    result = (int) mremap ((void *) t2h_addr (cb, &s, arg1), arg2, arg3, arg4);
 	    errcode = errno;
+#else
+	    result = -1;
+	    errcode = ENOSYS;
+#endif
 	    break;
 
 	  case TARGET_LINUX_SYS_getresuid32:
@@ -1285,8 +1305,13 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
 	    break;
 
 	  case TARGET_LINUX_SYS_getcwd:
-	    result = (int) getcwd ((char *) t2h_addr (cb, &s, arg1), arg2);
-	    errcode = errno;
+	    {
+	      void *ret;
+
+	      ret = getcwd ((char *) t2h_addr (cb, &s, arg1), arg2);
+	      result = ret == NULL ? 0 : arg1;
+	      errcode = errno;
+	    }
 	    break;
 
 	  case TARGET_LINUX_SYS_sendfile:
diff --git a/sim/m4/sim_ac_platform.m4 b/sim/m4/sim_ac_platform.m4
index fd6daeefbd1c..fa099f23e211 100644
--- a/sim/m4/sim_ac_platform.m4
+++ b/sim/m4/sim_ac_platform.m4
@@ -133,6 +133,8 @@ AC_CHECK_TYPES(socklen_t, [], [],
 #include <sys/socket.h>
 ])
 
+AC_CHECK_SIZEOF([void *])
+
 dnl Check for struct statfs.
 AC_CACHE_CHECK([for struct statfs],
   [sim_cv_struct_statfs],
-- 
2.43.0


  parent reply	other threads:[~2024-01-11  3:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  3:37 [PATCH/committed 1/5] sim: m32r: cleanup unused variables Mike Frysinger
2024-01-11  3:37 ` [PATCH/committed 2/5] sim: m32r: migrate ftime() to clock_gettime() Mike Frysinger
2024-01-11  3:37 ` [PATCH/committed 3/5] sim: m32r: fix missing break statement Mike Frysinger
2024-01-11  3:37 ` Mike Frysinger [this message]
2024-01-11  3:37 ` [PATCH/committed 5/5] sim: m32r: enable warnings in traps.c Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240111033747.17795-4-vapier@gentoo.org \
    --to=vapier@gentoo.org \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).