[PATCH] RISC-V: fix setting up std streams in init_semihosting()

"Ram Nalamothu (QUIC)" <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <BY5PR02MB6387C6F3D1743FB714AFDA9FF060A@BY5PR02MB6387.namprd02.prod.outlook.com>
Without this patch, the following simple test would fail to run as expected.

#include <stdio.h>
int main() {
  int n;
  printf("Enter a number\n");
  scanf("%d", &n);
  printf("Entered number: %d\n", n);
  return 0;
}

If the patch looks good, could someone please commit it on behalf of me as I don't have commit rights.

-- 8< --

Currently init_semihosting() assumes the return value from _open()
call as the file descriptor handle and that is incorrect.

The semihost _open() call returns the fdtable index returned by the
__add_fdentry() for the file opened.
0001-RISC-V-fix-setting-up-std-streams-in-init_semihostin.patch (application/octet-stream, 2 KB)
From 966aa280a1c51759ff8ab8e6cdda225a2093f17b Mon Sep 17 00:00:00 2001
From: Venkata Ramanaiah Nalamothu <[email protected]>
Date: Wed, 3 Jan 2024 12:22:16 +0530
Subject: [PATCH] RISC-V: fix setting up std streams in init_semihosting()
To: [email protected],
    [email protected],
    [email protected]

Currently init_semihosting() assumes the return value from _open()
call as the file descriptor handle and that is incorrect.

The semihost _open() call returns the fdtable index returned by the
__add_fdentry() for the file opened.
---
 libgloss/riscv/semihost-sys_fdtable.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/libgloss/riscv/semihost-sys_fdtable.c b/libgloss/riscv/semihost-sys_fdtable.c
index 152c92d15..d62e3f970 100644
--- a/libgloss/riscv/semihost-sys_fdtable.c
+++ b/libgloss/riscv/semihost-sys_fdtable.c
@@ -24,25 +24,26 @@ static struct fdentry fdtable[RISCV_MAX_OPEN_FILES];
 void __attribute__ ((constructor))
 init_semihosting ()
 {
-  int handle;
+  int i;
 
-  for (int i=0; i<RISCV_MAX_OPEN_FILES; i++)
+  for (i=0; i<RISCV_MAX_OPEN_FILES; i++)
     fdtable[i].handle = -1;
 
-  /* Set up std streams.  */
+  /* Set up std streams. Note that the semihost _open() call returns an index
+     into the fdtable.  */
   /* stdin.  */
-  handle = _open (":tt", O_RDONLY);
-  fdtable[STDIN_FILENO].handle = handle;
+  i = _open (":tt", O_RDONLY);
+  fdtable[STDIN_FILENO].handle = fdtable[i].handle;
   fdtable[STDIN_FILENO].pos = 0;
 
   /* stdout.  */
-  handle = _open (":tt", O_WRONLY|O_CREAT|O_TRUNC);
-  fdtable[STDOUT_FILENO].handle = handle;
+  i = _open (":tt", O_WRONLY|O_CREAT|O_TRUNC);
+  fdtable[STDOUT_FILENO].handle = fdtable[i].handle;
   fdtable[STDOUT_FILENO].pos = 0;
 
   /* stderr.  */
-  handle = _open (":tt", O_WRONLY|O_CREAT|O_APPEND);
-  fdtable[STDERR_FILENO].handle = handle;
+  i = _open (":tt", O_WRONLY|O_CREAT|O_APPEND);
+  fdtable[STDERR_FILENO].handle = fdtable[i].handle;
   fdtable[STDERR_FILENO].pos = 0;
 }
 
-- 
2.17.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.