[PATCH v2 1/3] libgloss/arm: report semihosted file types in stat calls
Torbjörn SVENSSON
torbjorn.svensson@foss.st.com
Thu Oct 8 18:01:29 GMT 2026
Arm semihosting has no stat operation, so infer file types from the
operations available.
Use IsTTY to identify character devices and SYS_FLEN to identify
regular files and obtain their sizes. Probe "path/." in _stat to
recognize directories.
This reports consistent file type bits and gives callers such as
std::filesystem::canonical enough information to handle semihosted
paths correctly.
Discovered, and reported, in
https://gcc.gnu.org/pipermail/gcc-patches/2026-August/727185.html
Regression-tested with GCC r17-4804-gf7094fe5ff882f: six tests changed
from XFAIL to XPASS, and one from FAIL to PASS; no regressions were
observed.
Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
---
libgloss/arm/syscalls.c | 126 ++++++++++++++++++++++++++++++++--------
1 file changed, 103 insertions(+), 23 deletions(-)
diff --git a/libgloss/arm/syscalls.c b/libgloss/arm/syscalls.c
index 710a741ee..63e136849 100644
--- a/libgloss/arm/syscalls.c
+++ b/libgloss/arm/syscalls.c
@@ -15,6 +15,7 @@
#include <reent.h>
#include <unistd.h>
#include <sys/wait.h>
+#include <limits.h>
#include "swi.h"
/* Forward prototypes. */
@@ -46,6 +47,7 @@ void initialise_monitor_handles (void);
static int checkerror (int);
static int error (int);
static int get_errno (void);
+static int path_is_dir (const char *);
/* Semihosting utilities. */
static void initialise_semihosting_exts (void);
@@ -346,6 +348,46 @@ checkerror (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);
+
+ /* Error means that path either does not exist
+ * or is not a directory. */
+ if (fd == -1)
+ return 0;
+
+ /* Not interested in the error. */
+ _close (fd);
+ return 1;
+}
+
/* fh, is a valid internal file handle.
ptr, is a null terminated string.
len, is the length in bytes to read.
@@ -725,8 +767,8 @@ _sbrk (ptrdiff_t incr)
return (void *) prev_heap_end;
}
-int
-_swistat (int fd, struct stat * st)
+int
+_swistat (int fd, struct stat *st)
{
struct fdent *pfd;
int res;
@@ -738,30 +780,56 @@ _swistat (int fd, struct stat * st)
return -1;
}
- /* Always assume a character device,
- with 1024 byte blocks. */
- st->st_mode |= S_IFCHR;
- st->st_blksize = 1024;
+ 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 = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
+ res = checkerror (do_AngelSWI (AngelSWI_Reason_FLen, &pfd->handle));
#else
- asm ("mov r0, %2; swi %a1; mov %0, r0"
- : "=r" (res)
- : "i" (SWI_Flen), "r" (pfd->handle)
- : "r0");
- checkerror (res);
+ asm ("mov r0, %2; swi %a1; mov %0, r0"
+ : "=r" (res)
+ : "i" (SWI_Flen), "r" (pfd->handle)
+ : "r0");
+ res = checkerror (res);
#endif
- if (res == -1)
- return -1;
- /* Return the file size. */
- st->st_size = res;
+ 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 fd, struct stat * st)
{
- memset (st, 0, sizeof (* st));
return _swistat (fd, st);
}
@@ -769,15 +837,27 @@ int __attribute__((weak))
_stat (const char *fname, struct stat *st)
{
int fd, res;
- memset (st, 0, sizeof (* st));
- /* The best we can do is try to open the file readonly. If it exists,
- then we can guess a few things about it. */
+
+ 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;
+
+ /* 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 ((fd = _open (fname, O_RDONLY)) == -1)
return -1;
- st->st_mode |= S_IFREG | S_IREAD;
res = _swistat (fd, st);
- /* Not interested in the error. */
- _close (fd);
+ /* Not interested in the error. */
+ _close (fd);
return res;
}
--
2.43.0
More information about the Newlib
mailing list