diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 5b90d4e..d495463 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -510,8 +510,7 @@ find_and_open_script (const char *script_file, int search_path,
 
   /* Search for and open 'file' on the search path used for source
      files.  Put the full location in *FULL_PATHP.  */
-  fd = openp (source_path, search_flags,
-	      file, O_RDONLY, full_pathp);
+  fd = openp (source_path, search_flags, file, full_pathp);
 
   if (fd == -1)
     {
diff --git a/gdb/defs.h b/gdb/defs.h
index ca95634..7795b4a 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -337,9 +337,12 @@ enum openp_flags
   /* Absolute names will also be searched in path (we usually want this
      for source files but not for executables).  */
   OPF_SEARCH_IN_PATH  = (1 << 1),
+
+  /* Open the file in read/write mode if WRITE_FILES says so.  */
+  OPF_OPEN_RW_TMP     = (1 << 3),
 };
 
-extern int openp (const char *, enum openp_flags, const char *, int, char **);
+extern int openp (const char *, enum openp_flags, const char *, char **);
 
 extern int source_full_path_of (const char *, char **);
 
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 835ba4f..ae06e14 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -10473,8 +10473,7 @@ try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
   flags = OPF_NONE;
   if (is_dwp)
     flags |= OPF_SEARCH_IN_PATH;
-  desc = openp (search_path, flags, file_name,
-		O_RDONLY | O_BINARY, &absolute_name);
+  desc = openp (search_path, flags, file_name, &absolute_name);
   xfree (search_path);
   if (desc < 0)
     return NULL;
diff --git a/gdb/exec.c b/gdb/exec.c
index f1b1049..e87d154 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -247,21 +247,18 @@ exec_file_attach (const char *filename, int from_tty)
 	}
       else
 	{
-	  scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
-				filename, write_files ?
-				O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
-				&scratch_pathname);
+	  scratch_chan = openp (getenv ("PATH"),
+				OPF_TRY_CWD_FIRST | OPF_OPEN_RW_TMP,
+				filename, &scratch_pathname);
 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
 	  if (scratch_chan < 0)
 	    {
 	      char *exename = alloca (strlen (filename) + 5);
 
 	      strcat (strcpy (exename, filename), ".exe");
-	      scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
-				    exename, write_files ?
-				    O_RDWR | O_BINARY
-				    : O_RDONLY | O_BINARY,
-				    &scratch_pathname);
+	      scratch_chan = openp (getenv ("PATH"),
+	                            OPF_TRY_CWD_FIRST | OPF_OPEN_RW_TMP,
+				    exename, &scratch_pathname);
 	    }
 #endif
 	  if (scratch_chan < 0)
diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c
index 72909fd..7c8b4ce 100644
--- a/gdb/nto-tdep.c
+++ b/gdb/nto-tdep.c
@@ -82,7 +82,7 @@ nto_map_arch_to_cputype (const char *arch)
 }
 
 int
-nto_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
+nto_find_and_open_solib (char *solib, char **temp_pathname)
 {
   char *buf, *arch_path, *nto_root;
   const char *endian;
@@ -127,12 +127,11 @@ nto_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
 	     arch_path);
 
   base = lbasename (solib);
-  ret = openp (buf, OPF_TRY_CWD_FIRST, base, o_flags,
-	       temp_pathname);
+  ret = openp (buf, OPF_TRY_CWD_FIRST, base, temp_pathname);
   if (ret < 0 && base != solib)
     {
       xsnprintf (arch_path, arch_len, "/%s", solib);
-      ret = open (arch_path, o_flags, 0);
+      ret = open (arch_path, O_RDONLY | O_BINARY);
       if (temp_pathname)
 	{
 	  if (ret >= 0)
diff --git a/gdb/nto-tdep.h b/gdb/nto-tdep.h
index bd85d2a..1f8045a 100644
--- a/gdb/nto-tdep.h
+++ b/gdb/nto-tdep.h
@@ -154,7 +154,7 @@ void nto_relocate_section_addresses (struct so_list *,
 
 int nto_map_arch_to_cputype (const char *);
 
-int nto_find_and_open_solib (char *, unsigned, char **);
+int nto_find_and_open_solib (char *, char **);
 
 enum gdb_osabi nto_elf_osabi_sniffer (bfd *abfd);
 
diff --git a/gdb/solib.c b/gdb/solib.c
index 1c408c0..b16f47b 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -337,7 +337,7 @@ solib_find_2 (char *in_pathname, int *fd, int is_solib, const char *sysroot)
      solib_search_path (if any).  */
   if (is_solib && found_file < 0 && solib_search_path != NULL)
     found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
-			in_pathname, O_RDONLY | O_BINARY, &temp_pathname);
+			in_pathname, &temp_pathname);
 
   /* If not found, and we're looking for a solib, next search the
      solib_search_path (if any) for the basename only (ignoring the
@@ -345,29 +345,25 @@ solib_find_2 (char *in_pathname, int *fd, int is_solib, const char *sysroot)
      from the opened path.  */
   if (is_solib && found_file < 0 && solib_search_path != NULL)
     found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
-			target_lbasename (fskind, in_pathname),
-			O_RDONLY | O_BINARY, &temp_pathname);
+			target_lbasename (fskind, in_pathname), &temp_pathname);
 
   /* If not found, and we're looking for a solib, try to use target
      supplied solib search method.  */
   if (is_solib && found_file < 0 && ops->find_and_open_solib)
-    found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
-					   &temp_pathname);
+    found_file = ops->find_and_open_solib (in_pathname, &temp_pathname);
 
   /* If not found, next search the inferior's $PATH environment variable.  */
   if (found_file < 0 && sysroot == NULL)
     found_file = openp (get_in_environ (current_inferior ()->environment,
 					"PATH"),
-			OPF_TRY_CWD_FIRST, in_pathname,
-			O_RDONLY | O_BINARY, &temp_pathname);
+			OPF_TRY_CWD_FIRST, in_pathname, &temp_pathname);
 
   /* If not found, and we're looking for a solib, next search the
      inferior's $LD_LIBRARY_PATH environment variable.  */
   if (is_solib && found_file < 0 && sysroot == NULL)
     found_file = openp (get_in_environ (current_inferior ()->environment,
 					"LD_LIBRARY_PATH"),
-			OPF_TRY_CWD_FIRST, in_pathname,
-			O_RDONLY | O_BINARY, &temp_pathname);
+			OPF_TRY_CWD_FIRST, in_pathname, &temp_pathname);
 
   if (found_file >= 0 && temp_pathname_is_realpath)
     temp_pathname = gdb_realpath_and_xfree (temp_pathname);
diff --git a/gdb/solist.h b/gdb/solist.h
index af9acc2..47e9d88 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -146,8 +146,7 @@ struct target_so_ops
        If TEMP_PATHNAME is non-NULL: If the file is successfully opened a
        pointer to a malloc'd and realpath'd copy of SONAME is stored there,
        otherwise NULL is stored there.  */
-    int (*find_and_open_solib) (char *soname,
-        unsigned o_flags, char **temp_pathname);
+    int (*find_and_open_solib) (char *soname, char **temp_pathname);
 
     /* Hook for looking up global symbols in a library-specific way.  */
     struct block_symbol (*lookup_lib_global_symbol)
diff --git a/gdb/source.c b/gdb/source.c
index 97849f8..8dfe580 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -737,9 +737,8 @@ dirnames_to_char_ptr_vec_target_exc (const char *string)
   return vec;
 }
 
-/* Open a file named STRING, searching path PATH (dir names sep by some char)
-   using mode MODE in the calls to open.  You cannot use this function to
-   create files (O_CREAT).
+/* Open a file named STRING, searching path PATH (dir names sep by some char).
+   You cannot use this function to create files.
 
    OPTS specifies the function behaviour in specific cases.
 
@@ -756,18 +755,16 @@ dirnames_to_char_ptr_vec_target_exc (const char *string)
     >>>>  eg executable, non-directory.  */
 int
 openp (const char *path, enum openp_flags opts, const char *string,
-       int mode, char **filename_opened)
+       char **filename_opened)
 {
   int fd;
   char *filename;
   int alloclen;
   VEC (char_ptr) *dir_vec;
   struct cleanup *back_to;
-  int ix;
+  int ix, mode;
   char *dir;
 
-  /* The open syscall MODE parameter is not specified.  */
-  gdb_assert ((mode & O_CREAT) == 0);
   gdb_assert (string != NULL);
 
   /* A file with an empty name cannot possibly exist.  Report a failure
@@ -786,7 +783,8 @@ openp (const char *path, enum openp_flags opts, const char *string,
   if (!path)
     path = ".";
 
-  mode |= O_BINARY;
+  mode = (O_BINARY | (((opts & OPF_OPEN_RW_TMP) && write_files)
+		      ? O_RDWR : O_RDONLY));
 
   if ((opts & OPF_TRY_CWD_FIRST) || IS_ABSOLUTE_PATH (string))
     {
@@ -932,7 +930,7 @@ source_full_path_of (const char *filename, char **full_pathname)
   int fd;
 
   fd = openp (source_path, OPF_TRY_CWD_FIRST | OPF_SEARCH_IN_PATH,
-	      filename, O_RDONLY, full_pathname);
+	      filename, full_pathname);
   if (fd < 0)
     {
       *full_pathname = NULL;
@@ -1106,13 +1104,17 @@ find_and_open_source (const char *filename,
         }
     }
 
-  result = openp (path, OPF_SEARCH_IN_PATH, filename, OPEN_MODE, fullname);
+  gdb_assert (OPEN_MODE == (O_RDONLY | O_BINARY));
+  result = openp (path, OPF_SEARCH_IN_PATH, filename, fullname);
   if (result < 0)
     {
       /* Didn't work.  Try using just the basename.  */
       p = lbasename (filename);
       if (p != filename)
-	result = openp (path, OPF_SEARCH_IN_PATH, p, OPEN_MODE, fullname);
+	{
+	  gdb_assert (OPEN_MODE == (O_RDONLY | O_BINARY));
+	  result = openp (path, OPF_SEARCH_IN_PATH, p, fullname);
+	}
     }
 
   if (result >= 0)
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 40f0451..c0eb4ce 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1732,7 +1732,7 @@ symfile_bfd_open (const char *name)
 
       /* Look down path for it, allocate 2nd new malloc'd copy.  */
       desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
-		    expanded_name, O_RDONLY | O_BINARY, &absolute_name);
+		    expanded_name, &absolute_name);
 #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
       if (desc < 0)
 	{
@@ -1740,7 +1740,7 @@ symfile_bfd_open (const char *name)
 
 	  strcat (strcpy (exename, expanded_name), ".exe");
 	  desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
-			exename, O_RDONLY | O_BINARY, &absolute_name);
+			exename, &absolute_name);
 	}
 #endif
       if (desc < 0)
