[v2,2/3] newlib: report semihosted file types in ARM stat calls

Message ID 20261008181335.2338595-3-torbjorn.svensson@foss.st.com
State New
Headers
Series Improve semihosted stat reporting for ARM and AArch64 |

Commit Message

Torbjorn SVENSSON Oct. 8, 2026, 6:01 p.m. UTC
  Arm semihosting has no stat operation, so infer file types from the
operations available.

Use IsTTY to identify character devices and FLEN to identify regular
files and obtain their sizes. Probe "path/." in _stat to recognize
directories, and use the same logic in _fstat.

Set the file type bits and populate regular-file size and block metadata.

Regression-tested with GCC r17-4804-gf7094fe5ff882f; no regressions were
observed.

Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
---
 newlib/libc/sys/arm/syscalls.c | 138 +++++++++++++++++++++++++++++----
 1 file changed, 121 insertions(+), 17 deletions(-)
  

Comments

Richard Earnshaw (foss) Oct. 15, 2026, 1:07 p.m. UTC | #1
On 08/10/2026 19:01, Torbjörn SVENSSON wrote:
> Arm semihosting has no stat operation, so infer file types from the
> operations available.
> 
> Use IsTTY to identify character devices and FLEN to identify regular
> files and obtain their sizes. Probe "path/." in _stat to recognize
> directories, and use the same logic in _fstat.
> 
> Set the file type bits and populate regular-file size and block metadata.
> 
> Regression-tested with GCC r17-4804-gf7094fe5ff882f; no regressions were
> observed.
> 
> Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
> ---
>   newlib/libc/sys/arm/syscalls.c | 138 +++++++++++++++++++++++++++++----
>   1 file changed, 121 insertions(+), 17 deletions(-)
> 
> diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
> index dd49ca061..6cc2fecf5 100644
> --- a/newlib/libc/sys/arm/syscalls.c
> +++ b/newlib/libc/sys/arm/syscalls.c
> @@ -7,6 +7,7 @@
>   #include <sys/stat.h>
>   #include <sys/fcntl.h>
>   #include <stdio.h>
> +#include <string.h>
>   #include <time.h>
>   #include <sys/time.h>
>   #include <sys/times.h>
> @@ -15,6 +16,7 @@
>   #include <signal.h>
>   #include <unistd.h>
>   #include <sys/wait.h>
> +#include <limits.h>
>   #include "swi.h"
>   
>   /* Forward prototypes.  */
> @@ -28,6 +30,7 @@ int	_unlink		(const char *);
>   int	_link		(const char *, const char *);
>   int	_stat		(const char *, struct stat *);
>   int	_fstat		(int, struct stat *);
> +int	_swistat	(int fd, struct stat * st);
>   void *	_sbrk		(ptrdiff_t);
>   pid_t	_getpid		(void);
>   int	_kill		(int, int);
> @@ -50,6 +53,7 @@ static int	get_errno	(void);
>   static int	remap_handle	(int);
>   static int	findslot	(int);
>   static int	_kill_shared	(int, int, int) __attribute__((__noreturn__));
> +static int	path_is_dir	(const char *);
>   
>   /* Register name faking - works in collusion with the linker.  */
>   register char * stack_ptr asm ("sp");
> @@ -195,6 +199,43 @@ wrap (int result)
>     return result;
>   }
>   
> +/* Check if the given path is likely a directory. */
> +static int
> +path_is_dir (const char *path)
> +{
> +  size_t path_len;
> +  size_t dir_len;
> +  char dir_path[PATH_MAX + 3];
> +  int fd;
> +
> +  path_len = strlen (path);
> +  if (path_len == 0)
> +    return 0;
> +
> +  if (path_len + 3 > sizeof (dir_path))
> +    {
> +      errno = ENAMETOOLONG;
> +      return -1;
> +    }
> +
> +  /* Build a new string that ends with "/.". */
> +  memcpy (dir_path, path, path_len);
> +  dir_len = path_len;
> +  if (dir_path[dir_len - 1] != '/')
> +    dir_path[dir_len++] = '/';
> +  dir_path[dir_len++] = '.';
> +  dir_path[dir_len] = '\0';
> +
> +  /* Try to open the directory. */
> +  fd = _open (dir_path, O_RDONLY);
> +  if (fd == -1)
> +    return 0;

This is a neat idea, but how thoroughly have you tested it when the 
remote host is running windows?  I suspect that the host in this case 
will try to use fopen (as it's not posix) and that will fail even if 
<dir>/. is a valid directory.

There are a few OS platforms where <dir>/. is not valid, but we probably 
don't care about those.  Windows, though does matter.

> +
> +  /* Not interested in the error. */
> +  _close (fd);
> +  return 1;
> +}
> +
>   /* file, is a valid user file handle.
>      ptr, is a null terminated string.
>      len, is the length in bytes to read.
> @@ -353,8 +394,6 @@ _write (int file, const void * ptr, size_t len)
>     return len - x;
>   }
>   
> -extern int strlen (const char *);
> -
>   int
>   _swiopen (const char * path, int flags)
>   {
> @@ -544,33 +583,98 @@ _sbrk (ptrdiff_t incr)
>     return (void *) prev_heap_end;
>   }
>   
> -extern void memset (struct stat *, int, unsigned int);
> +int
> +_swistat (int fd, struct stat * st)
> +{
> +  int res, slot, fh;
> +
> +  fh = remap_handle (fd);
> +  slot = findslot (fh);
> +  if (slot >= MAX_OPEN_FILES)
> +    {
> +      errno = EBADF;
> +      return -1;
> +    }
> +
> +  res = _isatty (fd);
> +  if (res != 0 && res != 1)
> +    /* errno already set. */
> +    return -1;
> +
> +  memset (st, 0, sizeof (*st));
> +
> +  if (res)
> +    {
> +      /* This is a tty. */
> +      st->st_mode = S_IFCHR;
> +    }
> +  else
> +    {
> +#ifdef ARM_RDI_MONITOR
> +      res = wrap (do_AngelSWI (AngelSWI_Reason_FLen, &fh));
> +#else
> +      asm ("mov r0, %2; swi %a1; mov %0, r0"
> +       : "=r" (res)
> +       : "i" (SWI_Flen), "r" (fh)
> +       : "r0");
> +      res = wrap (res);
> +#endif
> +      if (res == -1)
> +	{
> +	  if (errno != EISDIR)
> +	    return -1;
> +
> +	  /* This is a directory. */
> +	  st->st_mode = S_IFDIR;
> +	}
> +      else
> +	{
> +	  /* This is a file, return the file length.  */
> +	  st->st_mode = S_IFREG;
> +	  st->st_size = res;
> +	  st->st_blksize = 1024;
> +	  st->st_blocks = (res + 1023) / 1024;
> +	}
> +    }
> +
> +  /* Assume full permission (0777).  */
> +  st->st_mode |= S_IRWXU | S_IRWXG | S_IRWXO;
> +
> +  return 0;
> +}
>   
>   int __attribute__((weak))
>   _fstat (int file, struct stat * st)
>   {
> -  memset (st, 0, sizeof (* st));
> -  st->st_mode = S_IFCHR;
> -  st->st_blksize = 1024;
> -  return 0;
> -  file = file;
> +  return _swistat (file, st);
>   }
>   
>   int __attribute__((weak))
>   _stat (const char *fname, struct stat *st)
>   {
> -  int file;
> +  int file, res;
>   
> -  /* The best we can do is try to open the file readonly.  If it exists,
> -     then we can guess a few things about it.  */
> -  if ((file = _open (fname, O_RDONLY)) < 0)
> +  res = path_is_dir (fname);
> +  if (res == 1)
> +    {
> +      memset (st, 0, sizeof (*st));
> +      st->st_mode = S_IFDIR;
> +      /* Assume full permission (0777).  */
> +      st->st_mode |= S_IRWXU | S_IRWXG | S_IRWXO;
> +      return 0;
> +    }
> +  else if (res == -1)
> +    /* errno is already set. */
>       return -1;
>   
> -  memset (st, 0, sizeof (* st));
> -  st->st_mode = S_IFREG | S_IREAD;
> -  st->st_blksize = 1024;
> -  _swiclose (file); /* Not interested in the error.  */
> -  return 0;
> +  /* The best we can do is try to open the file readonly.
> +     If it exists, then we can guess a few things about it. */
> +  if ((file = _open (fname, O_RDONLY)) == -1)
> +    return -1;
> +  res = _swistat (file, st);
> +  /* Not interested in the error. */
> +  _swiclose (file);
> +  return res;
>   }
>   
>   int __attribute__((weak))

R.
  

Patch

diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
index dd49ca061..6cc2fecf5 100644
--- a/newlib/libc/sys/arm/syscalls.c
+++ b/newlib/libc/sys/arm/syscalls.c
@@ -7,6 +7,7 @@ 
 #include <sys/stat.h>
 #include <sys/fcntl.h>
 #include <stdio.h>
+#include <string.h>
 #include <time.h>
 #include <sys/time.h>
 #include <sys/times.h>
@@ -15,6 +16,7 @@ 
 #include <signal.h>
 #include <unistd.h>
 #include <sys/wait.h>
+#include <limits.h>
 #include "swi.h"
 
 /* Forward prototypes.  */
@@ -28,6 +30,7 @@  int	_unlink		(const char *);
 int	_link		(const char *, const char *);
 int	_stat		(const char *, struct stat *);
 int	_fstat		(int, struct stat *);
+int	_swistat	(int fd, struct stat * st);
 void *	_sbrk		(ptrdiff_t);
 pid_t	_getpid		(void);
 int	_kill		(int, int);
@@ -50,6 +53,7 @@  static int	get_errno	(void);
 static int	remap_handle	(int);
 static int	findslot	(int);
 static int	_kill_shared	(int, int, int) __attribute__((__noreturn__));
+static int	path_is_dir	(const char *);
 
 /* Register name faking - works in collusion with the linker.  */
 register char * stack_ptr asm ("sp");
@@ -195,6 +199,43 @@  wrap (int result)
   return result;
 }
 
+/* Check if the given path is likely a directory. */
+static int
+path_is_dir (const char *path)
+{
+  size_t path_len;
+  size_t dir_len;
+  char dir_path[PATH_MAX + 3];
+  int fd;
+
+  path_len = strlen (path);
+  if (path_len == 0)
+    return 0;
+
+  if (path_len + 3 > sizeof (dir_path))
+    {
+      errno = ENAMETOOLONG;
+      return -1;
+    }
+
+  /* Build a new string that ends with "/.". */
+  memcpy (dir_path, path, path_len);
+  dir_len = path_len;
+  if (dir_path[dir_len - 1] != '/')
+    dir_path[dir_len++] = '/';
+  dir_path[dir_len++] = '.';
+  dir_path[dir_len] = '\0';
+
+  /* Try to open the directory. */
+  fd = _open (dir_path, O_RDONLY);
+  if (fd == -1)
+    return 0;
+
+  /* Not interested in the error. */
+  _close (fd);
+  return 1;
+}
+
 /* file, is a valid user file handle.
    ptr, is a null terminated string.
    len, is the length in bytes to read. 
@@ -353,8 +394,6 @@  _write (int file, const void * ptr, size_t len)
   return len - x;
 }
 
-extern int strlen (const char *);
-
 int
 _swiopen (const char * path, int flags)
 {
@@ -544,33 +583,98 @@  _sbrk (ptrdiff_t incr)
   return (void *) prev_heap_end;
 }
 
-extern void memset (struct stat *, int, unsigned int);
+int
+_swistat (int fd, struct stat * st)
+{
+  int res, slot, fh;
+
+  fh = remap_handle (fd);
+  slot = findslot (fh);
+  if (slot >= MAX_OPEN_FILES)
+    {
+      errno = EBADF;
+      return -1;
+    }
+
+  res = _isatty (fd);
+  if (res != 0 && res != 1)
+    /* errno already set. */
+    return -1;
+
+  memset (st, 0, sizeof (*st));
+
+  if (res)
+    {
+      /* This is a tty. */
+      st->st_mode = S_IFCHR;
+    }
+  else
+    {
+#ifdef ARM_RDI_MONITOR
+      res = wrap (do_AngelSWI (AngelSWI_Reason_FLen, &fh));
+#else
+      asm ("mov r0, %2; swi %a1; mov %0, r0"
+       : "=r" (res)
+       : "i" (SWI_Flen), "r" (fh)
+       : "r0");
+      res = wrap (res);
+#endif
+      if (res == -1)
+	{
+	  if (errno != EISDIR)
+	    return -1;
+
+	  /* This is a directory. */
+	  st->st_mode = S_IFDIR;
+	}
+      else
+	{
+	  /* This is a file, return the file length.  */
+	  st->st_mode = S_IFREG;
+	  st->st_size = res;
+	  st->st_blksize = 1024;
+	  st->st_blocks = (res + 1023) / 1024;
+	}
+    }
+
+  /* Assume full permission (0777).  */
+  st->st_mode |= S_IRWXU | S_IRWXG | S_IRWXO;
+
+  return 0;
+}
 
 int __attribute__((weak))
 _fstat (int file, struct stat * st)
 {
-  memset (st, 0, sizeof (* st));
-  st->st_mode = S_IFCHR;
-  st->st_blksize = 1024;
-  return 0;
-  file = file;
+  return _swistat (file, st);
 }
 
 int __attribute__((weak))
 _stat (const char *fname, struct stat *st)
 {
-  int file;
+  int file, res;
 
-  /* The best we can do is try to open the file readonly.  If it exists,
-     then we can guess a few things about it.  */
-  if ((file = _open (fname, O_RDONLY)) < 0)
+  res = path_is_dir (fname);
+  if (res == 1)
+    {
+      memset (st, 0, sizeof (*st));
+      st->st_mode = S_IFDIR;
+      /* Assume full permission (0777).  */
+      st->st_mode |= S_IRWXU | S_IRWXG | S_IRWXO;
+      return 0;
+    }
+  else if (res == -1)
+    /* errno is already set. */
     return -1;
 
-  memset (st, 0, sizeof (* st));
-  st->st_mode = S_IFREG | S_IREAD;
-  st->st_blksize = 1024;
-  _swiclose (file); /* Not interested in the error.  */
-  return 0;
+  /* The best we can do is try to open the file readonly.
+     If it exists, then we can guess a few things about it. */
+  if ((file = _open (fname, O_RDONLY)) == -1)
+    return -1;
+  res = _swistat (file, st);
+  /* Not interested in the error. */
+  _swiclose (file);
+  return res;
 }
 
 int __attribute__((weak))