From 785b49434d27377208f6b058e589507cd0feadb3 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Fri, 18 Dec 2020 07:28:57 -0800 Subject: [PATCH] c++: Fix windows binary files [PR 98362] 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. --- gcc/cp/module.cc | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index b2b81900fc3..fc918d296a2 100644 --- a/gcc/cp/module.cc +++ b/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) -- 2.30.2