aboutsummaryrefslogtreecommitdiff
path: root/libgloss
diff options
context:
space:
mode:
authorVenkata Ramanaiah Nalamothu <quic_vnalamot@quicinc.com>2024-01-03 12:22:16 +0530
committerCorinna Vinschen <corinna@vinschen.de>2024-01-10 10:46:56 +0100
commitc419bbb341528a97d2de88539caf200c9dec1ffb (patch)
tree3e6cf9e8142431369664c05d8fefe7cb1e7ce94e /libgloss
parent9a863f713af24a3b18c165c640c936a4c0b707ea (diff)
downloadnewlib-c419bbb341528a97d2de88539caf200c9dec1ffb.zip
newlib-c419bbb341528a97d2de88539caf200c9dec1ffb.tar.gz
newlib-c419bbb341528a97d2de88539caf200c9dec1ffb.tar.bz2
RISC-V: fix setting up std streams in init_semihosting()
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.
Diffstat (limited to 'libgloss')
-rw-r--r--libgloss/riscv/semihost-sys_fdtable.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/libgloss/riscv/semihost-sys_fdtable.c b/libgloss/riscv/semihost-sys_fdtable.c
index 152c92d..d62e3f9 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;
}