diff mbox series

c++: Fix windows binary files [PR 98362]

Message ID a2ebb65e-0bd2-5727-692b-759a0b32a9ba@acm.org
State New
Headers show
Series c++: Fix windows binary files [PR 98362] | expand

Commit Message

Nathan Sidwell Dec. 18, 2020, 3:31 p.m. UTC
Windows	has unique and special needs for open(2).

	gcc/cp/
         * module.cc (O_CLOEXEC, O_BINARY): Add window's	support.
         (elf_in::defrost, module_state::do_import)
         (finish_module_processing): Use O_BINARY.
diff mbox series

Patch

diff --git i/gcc/cp/module.cc w/gcc/cp/module.cc
index b2b81900fc3..fc918d296a2 100644
--- i/gcc/cp/module.cc
+++ w/gcc/cp/module.cc
@@ -252,9 +252,23 @@  Classes used:
 #endif
 #endif
 
-#if !HOST_HAS_O_CLOEXEC
+/* Some open(2) flag differences, what a colourful world it is!  */
+#if defined (O_CLOEXEC)
+// OK
+#elif defined (_O_NOINHERIT)
+/* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
+#define O_CLOEXEC _O_NOINHERIT
+#else
 #define O_CLOEXEC 0
 #endif
+#if defined (O_BINARY)
+// Ok?
+#elif defined (_O_BINARY)
+/* Windows' open(2) call defaults to text!  */
+#define O_BINARY _O_BINARY
+#else
+#define O_BINARY 0
+#endif
 
 static inline cpp_hashnode *cpp_node (tree id)
 {
@@ -1596,7 +1610,7 @@  elf_in::defrost (const char *name)
   gcc_checking_assert (is_frozen ());
   struct stat stat;
 
-  fd = open (name, O_RDONLY | O_CLOEXEC);
+  fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
   if (fd < 0 || fstat (fd, &stat) < 0)
     set_error (errno);
   else
@@ -18568,7 +18582,7 @@  module_state::do_import (cpp_reader *reader, bool outermost)
     {
       const char *file = maybe_add_cmi_prefix (filename);
       dump () && dump ("CMI is %s", file);
-      fd = open (file, O_RDONLY | O_CLOEXEC);
+      fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
       e = errno;
     }
 
@@ -19704,7 +19718,8 @@  finish_module_processing (cpp_reader *reader)
 	  if (!errorcount)
 	    for (unsigned again = 2; ; again--)
 	      {
-		fd = open (tmp_name, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC,
+		fd = open (tmp_name,
+			   O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
 			   S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
 		e = errno;
 		if (fd >= 0 || !again || e != ENOENT)