Skip to contentSkip to conversation

semihosting: add O_BINARY flag in host_open for NT compatibility

PATCH Superseded Evgeny Iakovlev 3 participants 5 messages Jan 5, 2023 – Jan 6, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 semihosting: add O_BINARY flag in host_open for NT compatibility

Windows open(2) implementations opens files in text mode by default and needs a Windows-only O_BINARY flag to open files as binary. Qemu already knows about that flag in osdep.h, so we can just add it to the host_flags for better compatibility when running qemu on Windows.

---
 semihosting/syscalls.c | 2 ++
 1 file changed, 2 insertions(+)
semihosting/syscalls.c +2 -0
index 508a0ad88c..00f77507e5 100644
@@ -278,6 +278,8 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
         host_flags |= O_EXCL;
     }
 
+    host_flags |= O_BINARY;
+
     ret = open(p, host_flags, mode);
     if (ret < 0) {
         complete(cs, -1, errno);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Evgeny Iakovlev

Windows open(2) implementations opens files in text mode by default and needs a Windows-only O_BINARY flag to open files as binary. Qemu already

s/Qemu/QEMU/

Quoted 2 lines

knows about that flag in osdep.h, so we can just add it to the host_flags for better compatibility when running qemu on Windows.

s/qemu/QEMU/

Quoted 10 lines
---
  semihosting/syscalls.c | 2 ++
  1 file changed, 2 insertions(+)
semihosting/syscalls.c +0 -0
index 508a0ad88c..00f77507e5 100644

semihosting/syscalls.c static void host_open(CPUState *cs, gdb_syscall_complete_cb complete, · line 285

Quoted 8 lines
semihosting/syscalls.c +2 -0
@@ -278,6 +278,8 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
          host_flags |= O_EXCL;
      }
 
+    host_flags |= O_BINARY;
+
      ret = open(p, host_flags, mode);
      if (ret < 0) {
          complete(cs, -1, errno);

Alternatively with more churn:

-- >8 --

semihosting/syscalls.c +4 -4
index 508a0ad88c..b621d78c2d 100644
@@ -253,7 +253,7 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
 {
     CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
     char *p;
-    int ret, host_flags;
+    int ret, host_flags = O_BINARY;
 
     ret = validate_lock_user_string(&p, cs, fname, fname_len);
     if (ret < 0) {
@@ -262,11 +262,11 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
     }
 
     if (gdb_flags & GDB_O_WRONLY) {
-        host_flags = O_WRONLY;
+        host_flags |= O_WRONLY;
     } else if (gdb_flags & GDB_O_RDWR) {
-        host_flags = O_RDWR;
+        host_flags |= O_RDWR;
     } else {
-        host_flags = O_RDONLY;
+        host_flags |= O_RDONLY;
     }
     if (gdb_flags & GDB_O_CREAT) {
         host_flags |= O_CREAT;

---

Reviewed-by
#2 lore raw
Quoted 64 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Evgeny Iakovlev

Windows open(2) implementations opens files in text mode by default and needs a Windows-only O_BINARY flag to open files as binary. Qemu already

s/Qemu/QEMU/

Quoted 2 lines

knows about that flag in osdep.h, so we can just add it to the host_flags for better compatibility when running qemu on Windows.

s/qemu/QEMU/

Quoted 10 lines
---
  semihosting/syscalls.c | 2 ++
  1 file changed, 2 insertions(+)
semihosting/syscalls.c +0 -0
index 508a0ad88c..00f77507e5 100644

semihosting/syscalls.c static void host_open(CPUState *cs, gdb_syscall_complete_cb complete, · line 285

Quoted 8 lines
semihosting/syscalls.c +2 -0
@@ -278,6 +278,8 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
          host_flags |= O_EXCL;
      }
 
+    host_flags |= O_BINARY;
+
      ret = open(p, host_flags, mode);
      if (ret < 0) {
          complete(cs, -1, errno);

Alternatively with more churn:

-- >8 --

semihosting/syscalls.c +1 -1
index 508a0ad88c..b621d78c2d 100644
@@ -253,7 +253,7 @@ static void host_open(CPUState *cs,
 gdb_syscall_complete_cb complete,
  {
      CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
      char *p;
-    int ret, host_flags;
+    int ret, host_flags = O_BINARY;
 
      ret = validate_lock_user_string(&p, cs, fname, fname_len);
      if (ret < 0) {
semihosting/syscalls.c +3 -3
@@ -262,11 +262,11 @@ static void host_open(CPUState *cs,
 gdb_syscall_complete_cb complete,
      }
 
      if (gdb_flags & GDB_O_WRONLY) {
-        host_flags = O_WRONLY;
+        host_flags |= O_WRONLY;
      } else if (gdb_flags & GDB_O_RDWR) {
-        host_flags = O_RDWR;
+        host_flags |= O_RDWR;
      } else {
-        host_flags = O_RDONLY;
+        host_flags |= O_RDONLY;
      }
      if (gdb_flags & GDB_O_CREAT) {
          host_flags |= O_CREAT;
Quoted 4 lines

---

With Philippe's comments addressed,

#3 lore raw
Quoted 63 lines · Bin Meng
Quoted 62 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Evgeny Iakovlev

Windows open(2) implementations opens files in text mode by default and needs a Windows-only O_BINARY flag to open files as binary. Qemu already

s/Qemu/QEMU/

Quoted 2 lines

knows about that flag in osdep.h, so we can just add it to the host_flags for better compatibility when running qemu on Windows.

s/qemu/QEMU/

Quoted 10 lines
---
   semihosting/syscalls.c | 2 ++
   1 file changed, 2 insertions(+)
semihosting/syscalls.c +0 -0
index 508a0ad88c..00f77507e5 100644

semihosting/syscalls.c static void host_open(CPUState *cs, gdb_syscall_complete_cb complete, · line 285

Quoted 8 lines
semihosting/syscalls.c +2 -0
@@ -278,6 +278,8 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
           host_flags |= O_EXCL;
       }
 
+    host_flags |= O_BINARY;
+
       ret = open(p, host_flags, mode);
       if (ret < 0) {
           complete(cs, -1, errno);

Alternatively with more churn:

-- >8 --

semihosting/syscalls.c +1 -1
index 508a0ad88c..b621d78c2d 100644
@@ -253,7 +253,7 @@ static void host_open(CPUState *cs,
 gdb_syscall_complete_cb complete,
   {
       CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
       char *p;
-    int ret, host_flags;
+    int ret, host_flags = O_BINARY;
 
       ret = validate_lock_user_string(&p, cs, fname, fname_len);
       if (ret < 0) {
semihosting/syscalls.c +3 -3
@@ -262,11 +262,11 @@ static void host_open(CPUState *cs,
 gdb_syscall_complete_cb complete,
       }
 
       if (gdb_flags & GDB_O_WRONLY) {
-        host_flags = O_WRONLY;
+        host_flags |= O_WRONLY;
       } else if (gdb_flags & GDB_O_RDWR) {
-        host_flags = O_RDWR;
+        host_flags |= O_RDWR;
       } else {
-        host_flags = O_RDONLY;
+        host_flags |= O_RDONLY;
       }
       if (gdb_flags & GDB_O_CREAT) {
           host_flags |= O_CREAT;

---

Quoted 4 lines
Quoted 2 lines

With Philippe's comments addressed,

Done, sent out a v2, thanks!

#4 lore raw
In reply to Bin Meng
Quoted 63 lines · Bin Meng
Quoted 62 lines · Philippe Mathieu-Daudé
Quoted 2 lines · Evgeny Iakovlev

Windows open(2) implementations opens files in text mode by default and needs a Windows-only O_BINARY flag to open files as binary. Qemu already

s/Qemu/QEMU/

Quoted 2 lines

knows about that flag in osdep.h, so we can just add it to the host_flags for better compatibility when running qemu on Windows.

s/qemu/QEMU/

Quoted 10 lines
---
   semihosting/syscalls.c | 2 ++
   1 file changed, 2 insertions(+)
semihosting/syscalls.c +0 -0
index 508a0ad88c..00f77507e5 100644

semihosting/syscalls.c static void host_open(CPUState *cs, gdb_syscall_complete_cb complete, · line 285

Quoted 8 lines
semihosting/syscalls.c +2 -0
@@ -278,6 +278,8 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
           host_flags |= O_EXCL;
       }
 
+    host_flags |= O_BINARY;
+
       ret = open(p, host_flags, mode);
       if (ret < 0) {
           complete(cs, -1, errno);

Alternatively with more churn:

-- >8 --

semihosting/syscalls.c +1 -1
index 508a0ad88c..b621d78c2d 100644
@@ -253,7 +253,7 @@ static void host_open(CPUState *cs,
 gdb_syscall_complete_cb complete,
   {
       CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
       char *p;
-    int ret, host_flags;
+    int ret, host_flags = O_BINARY;
 
       ret = validate_lock_user_string(&p, cs, fname, fname_len);
       if (ret < 0) {
semihosting/syscalls.c +3 -3
@@ -262,11 +262,11 @@ static void host_open(CPUState *cs,
 gdb_syscall_complete_cb complete,
       }
 
       if (gdb_flags & GDB_O_WRONLY) {
-        host_flags = O_WRONLY;
+        host_flags |= O_WRONLY;
       } else if (gdb_flags & GDB_O_RDWR) {
-        host_flags = O_RDWR;
+        host_flags |= O_RDWR;
       } else {
-        host_flags = O_RDONLY;
+        host_flags |= O_RDONLY;
       }
       if (gdb_flags & GDB_O_CREAT) {
           host_flags |= O_CREAT;

---

Quoted 4 lines
Quoted 2 lines

With Philippe's comments addressed,

Done, sent out a v2, thanks!