[PATCH] Open files in O_BINARY

Ulf Hermann ulf.hermann@qt.io
Thu May 4 10:48:00 GMT 2017


If O_BINARY is not defined, define it to 0, so that the change has no
effect then. Some systems have separate binary and text modes for files,
and we don't want the text mode to be used.

Change-Id: If7efb5bd448c2a1c7d1eb5dab276849b1b15a3ce
Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 lib/ChangeLog                        |  4 +++
 lib/eu-config.h                      |  4 +++
 libdwfl/ChangeLog                    | 15 ++++++++++
 libdwfl/argp-std.c                   |  2 +-
 libdwfl/dwfl_build_id_find_elf.c     |  4 +--
 libdwfl/dwfl_module_getdwarf.c       |  2 +-
 libdwfl/dwfl_report_elf.c            |  2 +-
 libdwfl/dwfl_segment_report_module.c |  2 +-
 libdwfl/find-debuginfo.c             |  2 +-
 libdwfl/link_map.c                   |  4 +--
 libdwfl/linux-kernel-modules.c       | 20 ++++++-------
 libdwfl/linux-pid-attach.c           |  6 ++--
 libdwfl/linux-proc-maps.c            | 10 +++----
 libdwfl/offline.c                    |  2 +-
 libelf/ChangeLog                     |  4 +++
 libelf/nlist.c                       |  2 +-
 src/ChangeLog                        | 17 +++++++++++
 src/ar.c                             | 16 +++++-----
 src/elfcmp.c                         |  2 +-
 src/elfcompress.c                    |  4 +--
 src/elflint.c                        |  2 +-
 src/findtextrel.c                    |  4 +--
 src/nm.c                             |  2 +-
 src/objdump.c                        |  2 +-
 src/ranlib.c                         |  2 +-
 src/readelf.c                        |  2 +-
 src/size.c                           |  2 +-
 src/stack.c                          |  2 +-
 src/strings.c                        |  2 +-
 src/strip.c                          |  4 +--
 src/unstrip.c                        |  4 +--
 tests/ChangeLog                      | 57 ++++++++++++++++++++++++++++++++++++
 tests/alldts.c                       |  2 +-
 tests/allfcts.c                      |  4 +--
 tests/arextract.c                    |  4 +--
 tests/arls.c                         |  2 +-
 tests/arsymtest.c                    |  4 +--
 tests/asm-tst1.c                     |  2 +-
 tests/asm-tst2.c                     |  2 +-
 tests/asm-tst3.c                     |  2 +-
 tests/asm-tst7.c                     |  2 +-
 tests/asm-tst8.c                     |  2 +-
 tests/asm-tst9.c                     |  2 +-
 tests/backtrace-data.c               |  2 +-
 tests/buildid.c                      |  2 +-
 tests/debugaltlink.c                 |  2 +-
 tests/debuglink.c                    |  2 +-
 tests/dwarf-getmacros.c              |  2 +-
 tests/dwarf-getstring.c              |  2 +-
 tests/dwarf-ranges.c                 |  2 +-
 tests/dwelfgnucompressed.c           |  2 +-
 tests/early-offscn.c                 |  2 +-
 tests/ecp.c                          |  5 +++-
 tests/elfgetchdr.c                   |  2 +-
 tests/elfgetzdata.c                  |  2 +-
 tests/elfputzdata.c                  |  2 +-
 tests/elfshphehdr.c                  |  2 +-
 tests/elfstrmerge.c                  |  4 +--
 tests/elfstrtab.c                    |  6 ++--
 tests/emptyfile.c                    |  6 ++--
 tests/fillfile.c                     |  6 ++--
 tests/get-aranges.c                  |  2 +-
 tests/get-files.c                    |  2 +-
 tests/get-lines.c                    |  2 +-
 tests/get-pubnames.c                 |  2 +-
 tests/getsrc_die.c                   |  2 +-
 tests/newdata.c                      |  8 ++---
 tests/rdwrmmap.c                     |  2 +-
 tests/rerequest_tag.c                |  2 +-
 tests/saridx.c                       |  2 +-
 tests/scnnames.c                     |  2 +-
 tests/sectiondump.c                  |  2 +-
 tests/show-abbrev.c                  |  2 +-
 tests/show-die-info.c                |  2 +-
 tests/showptable.c                   |  2 +-
 tests/strptr.c                       |  2 +-
 tests/test-elf_cntl_gelf_getshdr.c   |  2 +-
 tests/test-flag-nobits.c             |  2 +-
 tests/typeiter.c                     |  2 +-
 tests/typeiter2.c                    |  2 +-
 tests/update1.c                      |  2 +-
 tests/update2.c                      |  2 +-
 tests/update3.c                      |  2 +-
 tests/update4.c                      |  2 +-
 tests/vendorelf.c                    |  4 +--
 tests/zstrptr.c                      |  2 +-
 86 files changed, 226 insertions(+), 122 deletions(-)

diff --git a/lib/ChangeLog b/lib/ChangeLog
index 321513c..9fa8ff0 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* eu-config.h: Define O_BINARY to 0 if it doesn't exist.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* eu-config.h: Define unlocked I/O functions to locked ones if they
 	are unavailable.
 
diff --git a/lib/eu-config.h b/lib/eu-config.h
index 6530044..f2d9175 100644
--- a/lib/eu-config.h
+++ b/lib/eu-config.h
@@ -234,4 +234,8 @@ asm (".section predict_data, \"aw\"; .previous\n"
 #define putchar_unlocked(x) putchar (x)
 #endif
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 #endif	/* eu-config.h */
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 26a3599..6f3a561 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,18 @@
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* argp-std.c: Open files in O_BINARY.
+	* dwfl_build_id_find_elf.c: Likewise.
+	* dwfl_build_id_find_elf.c: Likewise.
+	* dwfl_module_getdwarf.c: Likewise.
+	* dwfl_report_elf.c: Likewise.
+	* dwfl_segment_report_module.c: Likewise.
+	* find-debuginfo.c: Likewise.
+	* link_map.c: Likewise.
+	* linux-kernel-modules.c: Likewise.
+	* linux-pid-attach.c: Likewise.
+	* linux-proc-maps.c: Likewise.
+	* offline.c: Likewise.
+
 2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* linux-kernel-modules.c: Don't include system.h.
diff --git a/libdwfl/argp-std.c b/libdwfl/argp-std.c
index 347a05b..012eb0c 100644
--- a/libdwfl/argp-std.c
+++ b/libdwfl/argp-std.c
@@ -277,7 +277,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
 
 	if (opt->core)
 	  {
-	    int fd = open (opt->core, O_RDONLY);
+	    int fd = open (opt->core, O_RDONLY | O_BINARY);
 	    if (fd < 0)
 	      {
 		int code = errno;
diff --git a/libdwfl/dwfl_build_id_find_elf.c b/libdwfl/dwfl_build_id_find_elf.c
index ee0c164..6ce2c1c 100644
--- a/libdwfl/dwfl_build_id_find_elf.c
+++ b/libdwfl/dwfl_build_id_find_elf.c
@@ -94,7 +94,7 @@ __libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug, char **file_name,
 	break;
       memcpy (mempcpy (name, dir, dirlen), id_name, sizeof id_name);
 
-      fd = TEMP_FAILURE_RETRY (open (name, O_RDONLY));
+      fd = TEMP_FAILURE_RETRY (open (name, O_RDONLY | O_BINARY));
       if (fd >= 0)
 	{
 	  if (*file_name != NULL)
@@ -154,7 +154,7 @@ dwfl_build_id_find_elf (Dwfl_Module *mod,
 	 recorded executable file when MOD was identified as main executable
 	 (which then triggers opening and reporting of the executable).  */
       const char *executable = mod->dwfl->user_core->executable_for_core;
-      int fd = open (executable, O_RDONLY);
+      int fd = open (executable, O_RDONLY | O_BINARY);
       if (fd >= 0)
 	{
 	  *file_name = strdup (executable);
diff --git a/libdwfl/dwfl_module_getdwarf.c b/libdwfl/dwfl_module_getdwarf.c
index 9775ace..f638093 100644
--- a/libdwfl/dwfl_module_getdwarf.c
+++ b/libdwfl/dwfl_module_getdwarf.c
@@ -51,7 +51,7 @@ open_elf_file (Elf **elf, int *fd, char **name)
       /* If there was a pre-primed file name left that the callback left
 	 behind, try to open that file name.  */
       if (*fd < 0 && *name != NULL)
-	*fd = TEMP_FAILURE_RETRY (open (*name, O_RDONLY));
+	*fd = TEMP_FAILURE_RETRY (open (*name, O_RDONLY | O_BINARY));
 
       if (*fd < 0)
 	return CBFAIL;
diff --git a/libdwfl/dwfl_report_elf.c b/libdwfl/dwfl_report_elf.c
index 6950a37..d4d3feb 100644
--- a/libdwfl/dwfl_report_elf.c
+++ b/libdwfl/dwfl_report_elf.c
@@ -295,7 +295,7 @@ dwfl_report_elf (Dwfl *dwfl, const char *name, const char *file_name, int fd,
   if (fd < 0)
     {
       closefd = true;
-      fd = open (file_name, O_RDONLY);
+      fd = open (file_name, O_RDONLY | O_BINARY);
       if (fd < 0)
 	{
 	  __libdwfl_seterrno (DWFL_E_ERRNO);
diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index 207a257..cd8f613 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -688,7 +688,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
       name = file_note_name;
       name_is_final = true;
       bool invalid = false;
-      fd = open (name, O_RDONLY);
+      fd = open (name, O_RDONLY | O_BINARY);
       if (fd >= 0)
 	{
 	  Dwfl_Error error = __libdw_open_file (&fd, &elf, true, false);
diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c
index 6d5a42a..7f7e108 100644
--- a/libdwfl/find-debuginfo.c
+++ b/libdwfl/find-debuginfo.c
@@ -58,7 +58,7 @@ try_open (const struct stat *main_stat,
     return -1;
 
   struct stat st;
-  int fd = TEMP_FAILURE_RETRY (open (fname, O_RDONLY));
+  int fd = TEMP_FAILURE_RETRY (open (fname, O_RDONLY | O_BINARY));
   if (fd < 0)
     free (fname);
   else if (fstat (fd, &st) == 0
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index 794668f..4a50c44 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -393,7 +393,7 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
 	{
 	  /* This code is mostly inlined dwfl_report_elf.  */
 	  // XXX hook for sysroot
-	  int fd = open (name, O_RDONLY);
+	  int fd = open (name, O_RDONLY | O_BINARY);
 	  if (fd >= 0)
 	    {
 	      Elf *elf;
@@ -808,7 +808,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 		 EXECUTABLE_FOR_CORE to find where DYNAMIC is located in the
 		 core file.  */
 
-	      int fd = open (dwfl->user_core->executable_for_core, O_RDONLY);
+	      int fd = open (dwfl->user_core->executable_for_core, O_RDONLY | O_BINARY);
 	      Elf *elf;
 	      Dwfl_Error error = DWFL_E_ERRNO;
 	      if (fd != -1)
diff --git a/libdwfl/linux-kernel-modules.c b/libdwfl/linux-kernel-modules.c
index 5f132b4..139a477 100644
--- a/libdwfl/linux-kernel-modules.c
+++ b/libdwfl/linux-kernel-modules.c
@@ -97,7 +97,7 @@ try_kernel_name (Dwfl *dwfl, char **fname, bool try_debug)
   int fd = ((((dwfl->callbacks->debuginfo_path
 	       ? *dwfl->callbacks->debuginfo_path : NULL)
 	      ?: DEFAULT_DEBUGINFO_PATH)[0] == ':') ? -1
-	    : TEMP_FAILURE_RETRY (open (*fname, O_RDONLY)));
+	    : TEMP_FAILURE_RETRY (open (*fname, O_RDONLY | O_BINARY)));
 
   if (fd < 0)
     {
@@ -132,7 +132,7 @@ try_kernel_name (Dwfl *dwfl, char **fname, bool try_debug)
 	char *zname;
 	if (asprintf (&zname, "%s%s", *fname, vmlinux_suffixes[i]) > 0)
 	  {
-	    fd = TEMP_FAILURE_RETRY (open (zname, O_RDONLY));
+	    fd = TEMP_FAILURE_RETRY (open (zname, O_RDONLY | O_BINARY));
 	    if (fd < 0)
 	      free (zname);
 	    else
@@ -488,7 +488,7 @@ intuit_kernel_bounds (Dwarf_Addr *start, Dwarf_Addr *end, Dwarf_Addr *notes)
 {
   struct read_address_state state = { NULL, NULL, 0, 0, NULL, NULL };
 
-  state.f = fopen (KSYMSFILE, "r");
+  state.f = fopen (KSYMSFILE, "rb");
   if (state.f == NULL)
     return errno;
 
@@ -531,7 +531,7 @@ static int
 check_notes (Dwfl_Module *mod, const char *notesfile,
 	     Dwarf_Addr vaddr, const char *secname)
 {
-  int fd = open (notesfile, O_RDONLY);
+  int fd = open (notesfile, O_RDONLY | O_BINARY);
   if (fd < 0)
     return 1;
 
@@ -789,7 +789,7 @@ dwfl_linux_kernel_find_elf (Dwfl_Module *mod,
 	      && (!memcmp (f->fts_name, module_name, namelen)
 		  || !memcmp (f->fts_name, alternate_name, namelen)))
 	    {
-	      int fd = open (f->fts_accpath, O_RDONLY);
+	      int fd = open (f->fts_accpath, O_RDONLY | O_BINARY);
 	      *file_name = strdup (f->fts_path);
 	      fts_close (fts);
 	      free (modulesdir[0]);
@@ -842,7 +842,7 @@ dwfl_linux_kernel_module_section_address
   if (asprintf (&sysfile, SECADDRDIRFMT "%s", modname, secname) < 0)
     return DWARF_CB_ABORT;
 
-  FILE *f = fopen (sysfile, "r");
+  FILE *f = fopen (sysfile, "rb");
   free (sysfile);
 
   if (f == NULL)
@@ -876,7 +876,7 @@ dwfl_linux_kernel_module_section_address
 	      if (asprintf (&sysfile, SECADDRDIRFMT "_%s",
 			    modname, &secname[1]) < 0)
 		return ENOMEM;
-	      f = fopen (sysfile, "r");
+	      f = fopen (sysfile, "rb");
 	      free (sysfile);
 	      if (f != NULL)
 		goto ok;
@@ -896,11 +896,11 @@ dwfl_linux_kernel_module_section_address
 	      do
 		{
 		  *--end = '\0';
-		  f = fopen (sysfile, "r");
+		  f = fopen (sysfile, "rb");
 		  if (is_init && f == NULL && errno == ENOENT)
 		    {
 		      sysfile[len - namelen] = '_';
-		      f = fopen (sysfile, "r");
+		      f = fopen (sysfile, "rb");
 		      sysfile[len - namelen] = '.';
 		    }
 		}
@@ -934,7 +934,7 @@ INTDEF (dwfl_linux_kernel_module_section_address)
 int
 dwfl_linux_kernel_report_modules (Dwfl *dwfl)
 {
-  FILE *f = fopen (MODULELIST, "r");
+  FILE *f = fopen (MODULELIST, "rb");
   if (f == NULL)
     return errno;
 
diff --git a/libdwfl/linux-pid-attach.c b/libdwfl/linux-pid-attach.c
index e6a5c41..1def7f1 100644
--- a/libdwfl/linux-pid-attach.c
+++ b/libdwfl/linux-pid-attach.c
@@ -52,7 +52,7 @@ linux_proc_pid_is_stopped (pid_t pid)
   bool retval, have_state;
 
   snprintf (buffer, sizeof (buffer), "/proc/%ld/status", (long) pid);
-  procfile = fopen (buffer, "r");
+  procfile = fopen (buffer, "rb");
   if (procfile == NULL)
     return false;
 
@@ -302,7 +302,7 @@ dwfl_linux_proc_attach (Dwfl *dwfl, pid_t pid, bool assume_ptrace_stopped)
   /* Make sure to report the actual PID (thread group leader) to
      dwfl_attach_state.  */
   snprintf (buffer, sizeof (buffer), "/proc/%ld/status", (long) pid);
-  procfile = fopen (buffer, "r");
+  procfile = fopen (buffer, "rb");
   if (procfile == NULL)
     {
       err = errno;
@@ -352,7 +352,7 @@ dwfl_linux_proc_attach (Dwfl *dwfl, pid_t pid, bool assume_ptrace_stopped)
   Elf *elf;
   i = snprintf (name, sizeof (name), "/proc/%ld/exe", (long) pid);
   assert (i > 0 && i < (ssize_t) sizeof (name) - 1);
-  int elf_fd = open (name, O_RDONLY);
+  int elf_fd = open (name, O_RDONLY | O_BINARY);
   if (elf_fd >= 0)
     {
       elf = elf_begin (elf_fd, ELF_C_READ_MMAP, NULL);
diff --git a/libdwfl/linux-proc-maps.c b/libdwfl/linux-proc-maps.c
index c4438c0..78f472a 100644
--- a/libdwfl/linux-proc-maps.c
+++ b/libdwfl/linux-proc-maps.c
@@ -63,7 +63,7 @@ get_pid_class (pid_t pid)
   if (asprintf (&fname, PROCEXEFMT, pid) < 0)
     return ELFCLASSNONE;
 
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   free (fname);
   if (fd < 0)
     return ELFCLASSNONE;
@@ -99,7 +99,7 @@ grovel_auxv (pid_t pid, Dwfl *dwfl, GElf_Addr *sysinfo_ehdr)
   if (asprintf (&fname, PROCAUXVFMT, pid) < 0)
     return ENOMEM;
 
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   free (fname);
   if (fd < 0)
     return errno == ENOENT ? 0 : errno;
@@ -306,7 +306,7 @@ dwfl_linux_proc_report (Dwfl *dwfl, pid_t pid)
   if (asprintf (&fname, PROCMAPSFMT, pid) < 0)
     return ENOMEM;
 
-  FILE *f = fopen (fname, "r");
+  FILE *f = fopen (fname, "rb");
   free (fname);
   if (f == NULL)
     return errno;
@@ -380,7 +380,7 @@ dwfl_linux_proc_find_elf (Dwfl_Module *mod __attribute__ ((unused)),
 
       if (pid == -1)
 	{
-	  int fd = open (module_name, O_RDONLY);
+	  int fd = open (module_name, O_RDONLY | O_BINARY);
 	  if (fd >= 0)
 	    {
 	      *file_name = strdup (module_name);
@@ -417,7 +417,7 @@ dwfl_linux_proc_find_elf (Dwfl_Module *mod __attribute__ ((unused)),
       if (asprintf (&fname, PROCMEMFMT, pid) < 0)
 	goto detach;
 
-      int fd = open (fname, O_RDONLY);
+      int fd = open (fname, O_RDONLY | O_BINARY);
       free (fname);
       if (fd < 0)
 	goto detach;
diff --git a/libdwfl/offline.c b/libdwfl/offline.c
index 80c80a1..7666358 100644
--- a/libdwfl/offline.c
+++ b/libdwfl/offline.c
@@ -302,7 +302,7 @@ dwfl_report_offline (Dwfl *dwfl, const char *name,
   if (fd < 0)
     {
       closefd = true;
-      fd = open (file_name, O_RDONLY);
+      fd = open (file_name, O_RDONLY | O_BINARY);
       if (fd < 0)
 	{
 	  __libdwfl_seterrno (DWFL_E_ERRNO);
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index fd20ebb..0f17347 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* nlist.c: Open files in O_BINARY.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* elf_update.c: Don't try to posix_fallocate on systems where it isn't
 	available. Don't fchmod the output file if there is no fchmod.
 
diff --git a/libelf/nlist.c b/libelf/nlist.c
index c7b32fd..f8488c1 100644
--- a/libelf/nlist.c
+++ b/libelf/nlist.c
@@ -71,7 +71,7 @@ nlist (const char *filename, struct nlist *nl)
   size_t cnt;
 
   /* Open the file.  */
-  fd = open (filename, O_RDONLY);
+  fd = open (filename, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       __libelf_seterrno (ELF_E_NOFILE);
diff --git a/src/ChangeLog b/src/ChangeLog
index e0df2e1..7103770 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,22 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* ar.c: Open files in O_BINARY.
+	* elfcmp.c: Likewise.
+	* elfcompress.c: Likewise.
+	* elflint.c: Likewise.
+	* findtextrel.c: Likewise.
+	* nm.c: Likewise.
+	* objdump.c: Likewise.
+	* ranlib.c: Likewise.
+	* readelf.c: Likewise.
+	* size.c: Likewise.
+	* stack.c: Likewise.
+	* strings.c: Likewise.
+	* strip.c: Likewise.
+	* unstrip.c: Likewise.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* ar.c: Don't fchmod or fchown the output file if fchmod or fchown
 	don't exist.
 	* elfcompress.c: Likewise.
diff --git a/src/ar.c b/src/ar.c
index 45b219a..91391b1 100644
--- a/src/ar.c
+++ b/src/ar.c
@@ -393,14 +393,14 @@ open_archive (const char *arfname, int flags, int mode, Elf **elf,
 
   if (elf != NULL)
     {
-      Elf_Cmd cmd = flags == O_RDONLY ? ELF_C_READ_MMAP : ELF_C_RDWR_MMAP;
+      Elf_Cmd cmd = flags == (O_RDONLY | O_BINARY) ? ELF_C_READ_MMAP : ELF_C_RDWR_MMAP;
 
       *elf = elf_begin (fd, cmd, NULL);
       if (*elf == NULL)
 	error (EXIT_FAILURE, 0, gettext ("cannot open archive '%s': %s"),
 	       arfname, elf_errmsg (-1));
 
-      if (flags == O_RDONLY && elf_kind (*elf) != ELF_K_AR)
+      if (flags == (O_RDONLY | O_BINARY) && elf_kind (*elf) != ELF_K_AR)
 	error (EXIT_FAILURE, 0, gettext ("%s: not an archive file"), arfname);
     }
 
@@ -467,7 +467,7 @@ do_oper_extract (int oper, const char *arfname, char **argv, int argc,
 
   int status = 0;
   Elf *elf;
-  int fd = open_archive (arfname, O_RDONLY, 0, &elf, NULL, false);
+  int fd = open_archive (arfname, O_RDONLY | O_BINARY, 0, &elf, NULL, false);
 
   if (hcreate (2 * argc) == 0)
     error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
@@ -600,7 +600,7 @@ do_oper_extract (int oper, const char *arfname, char **argv, int argc,
 		{
 		  /* We cannot create a temporary file.  Try to overwrite
 		     the file or create it if it does not exist.  */
-		  int flags = O_WRONLY | O_CREAT;
+		  int flags = O_WRONLY | O_BINARY | O_CREAT;
 		  if (dont_replace_existing)
 		    flags |= O_EXCL;
 		  else
@@ -919,7 +919,7 @@ do_oper_delete (const char *arfname, char **argv, int argc,
   int status = 0;
   Elf *elf;
   struct stat st;
-  int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, false);
+  int fd = open_archive (arfname, O_RDONLY | O_BINARY, 0, &elf, &st, false);
 
   if (hcreate (2 * argc) == 0)
     error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
@@ -1099,7 +1099,7 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc,
   int status = 0;
   Elf *elf;
   struct stat st;
-  int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, oper != oper_move);
+  int fd = open_archive (arfname, O_RDONLY | O_BINARY, 0, &elf, &st, oper != oper_move);
 
   /* List of the files we keep.  */
   struct armem *all = NULL;
@@ -1255,7 +1255,7 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc,
 
 	  struct stat newst;
 	  Elf *newelf;
-	  int newfd = open (argv[cnt], O_RDONLY);
+	  int newfd = open (argv[cnt], O_RDONLY | O_BINARY);
 	  if (newfd == -1)
 	    {
 	      error (0, errno, gettext ("cannot open %s"), argv[cnt]);
@@ -1399,7 +1399,7 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc,
     newfd = mkstemp (tmpfname);
   else
     {
-      newfd = open (arfname, O_RDWR | O_CREAT | O_EXCL, DEFFILEMODE);
+      newfd = open (arfname, O_RDWR | O_BINARY | O_CREAT | O_EXCL, DEFFILEMODE);
       if (newfd == -1 && errno == EEXIST)
 	/* Bah, first the file did not exist, now it does.  Restart.  */
 	return do_oper_insert (oper, arfname, argv, argc, member);
diff --git a/src/elfcmp.c b/src/elfcmp.c
index 5046420..7ca7efa 100644
--- a/src/elfcmp.c
+++ b/src/elfcmp.c
@@ -714,7 +714,7 @@ parse_opt (int key, char *arg,
 static Elf *
 open_file (const char *fname, int *fdp, Ebl **eblp)
 {
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (unlikely (fd == -1))
     error (2, errno, gettext ("cannot open '%s'"), fname);
   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
diff --git a/src/elfcompress.c b/src/elfcompress.c
index e092e13..5dbeb57 100644
--- a/src/elfcompress.c
+++ b/src/elfcompress.c
@@ -321,7 +321,7 @@ process_file (const char *fname)
     return res;
   }
 
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd < 0)
     {
       error (0, errno, "Couldn't open %s\n", fname);
@@ -542,7 +542,7 @@ process_file (const char *fname)
   else
     {
       fnew = xstrdup (foutput);
-      fdnew = open (fnew, O_WRONLY | O_CREAT, st.st_mode & ALLPERMS);
+      fdnew = open (fnew, O_WRONLY | O_BINARY | O_CREAT, st.st_mode & ALLPERMS);
     }
 
   if (fdnew < 0)
diff --git a/src/elflint.c b/src/elflint.c
index 51e53c2..18b5a4b 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -149,7 +149,7 @@ main (int argc, char *argv[])
   do
     {
       /* Open the file.  */
-      int fd = open (argv[remaining], O_RDONLY);
+      int fd = open (argv[remaining], O_RDONLY | O_BINARY);
       if (fd == -1)
 	{
 	  error (0, errno, gettext ("cannot open input file"));
diff --git a/src/findtextrel.c b/src/findtextrel.c
index 8f1e239..642c023 100644
--- a/src/findtextrel.c
+++ b/src/findtextrel.c
@@ -200,7 +200,7 @@ process_file (const char *fname, bool more_than_one)
       real_fname = new_fname;
     }
 
-  int fd = open (real_fname, O_RDONLY);
+  int fd = open (real_fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       error (0, errno, gettext ("cannot open '%s'"), fname);
@@ -373,7 +373,7 @@ cannot get program header index at offset %zd: %s"),
 			   fname, fname_len),
 		  ".debug");
 
-	  fd2 = open (difname, O_RDONLY);
+	  fd2 = open (difname, O_RDONLY | O_BINARY);
 	  if (fd2 != -1
 	      && (elf2 = elf_begin (fd2, ELF_C_READ_MMAP, NULL)) != NULL)
 	    dw = dwarf_begin_elf (elf2, DWARF_C_READ, NULL);
diff --git a/src/nm.c b/src/nm.c
index 969c6d3..6ab22b2 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -359,7 +359,7 @@ static int
 process_file (const char *fname, bool more_than_one)
 {
   /* Open the file.  */
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       error (0, errno, gettext ("cannot open '%s'"), fname);
diff --git a/src/objdump.c b/src/objdump.c
index 860cfac..13d3e0b 100644
--- a/src/objdump.c
+++ b/src/objdump.c
@@ -236,7 +236,7 @@ static int
 process_file (const char *fname, bool more_than_one)
 {
   /* Open the file.  */
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       error (0, errno, gettext ("cannot open %s"), fname);
diff --git a/src/ranlib.c b/src/ranlib.c
index ecaeb55..41057de 100644
--- a/src/ranlib.c
+++ b/src/ranlib.c
@@ -136,7 +136,7 @@ copy_content (Elf *elf, int newfd, off_t off, size_t n)
 static int
 handle_file (const char *fname)
 {
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       error (0, errno, gettext ("cannot open '%s'"), fname);
diff --git a/src/readelf.c b/src/readelf.c
index 40d4913..6811ace 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -297,7 +297,7 @@ main (int argc, char *argv[])
   do
     {
       /* Open the file.  */
-      int fd = open (argv[remaining], O_RDONLY);
+      int fd = open (argv[remaining], O_RDONLY | O_BINARY);
       if (fd == -1)
 	{
 	  error (0, errno, gettext ("cannot open input file"));
diff --git a/src/size.c b/src/size.c
index ad8dbcb..4774800 100644
--- a/src/size.c
+++ b/src/size.c
@@ -267,7 +267,7 @@ parse_opt (int key, char *arg,
 static int
 process_file (const char *fname)
 {
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (unlikely (fd == -1))
     {
       error (0, errno, gettext ("cannot open '%s'"), fname);
diff --git a/src/stack.c b/src/stack.c
index 6f2ff69..1f5a1c6 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -483,7 +483,7 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
       break;
 
     case OPT_COREFILE:
-      core_fd = open (arg, O_RDONLY);
+      core_fd = open (arg, O_RDONLY | O_BINARY);
       if (core_fd < 0)
 	error (EXIT_BAD, errno, N_("Cannot open core file '%s'"), arg);
       elf_version (EV_CURRENT);
diff --git a/src/strings.c b/src/strings.c
index 46b2356..8e2d098 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -180,7 +180,7 @@ main (int argc, char *argv[])
     do
       {
 	int fd = (strcmp (argv[remaining], "-") == 0
-		  ? STDIN_FILENO : open (argv[remaining], O_RDONLY));
+		  ? STDIN_FILENO : open (argv[remaining], O_RDONLY | O_BINARY));
 	if (unlikely (fd == -1))
 	  {
 	    error (0, errno, gettext ("cannot open '%s'"), argv[remaining]);
diff --git a/src/strip.c b/src/strip.c
index f592081..db8ff2f 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -315,7 +315,7 @@ process_file (const char *fname)
     }
 
   /* Open the file.  */
-  int fd = open (fname, output_fname == NULL ? O_RDWR : O_RDONLY);
+  int fd = open (fname, output_fname == NULL ? (O_RDWR | O_BINARY) : (O_RDONLY | O_BINARY));
   if (fd == -1)
     {
       error (0, errno, gettext ("while opening '%s'"), fname);
@@ -447,7 +447,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
   /* If we are not replacing the input file open a new file here.  */
   if (output_fname != NULL)
     {
-      fd = open (output_fname, O_RDWR | O_CREAT, mode);
+      fd = open (output_fname, O_RDWR | O_BINARY | O_CREAT, mode);
       if (unlikely (fd == -1))
 	{
 	  error (0, errno, gettext ("cannot open '%s'"), output_fname);
diff --git a/src/unstrip.c b/src/unstrip.c
index 5074909..8d5414f 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -2017,7 +2017,7 @@ DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
 	make_directories (output_file);
 
       /* Copy the unstripped file and then modify it.  */
-      int outfd = open (output_file, O_RDWR | O_CREAT,
+      int outfd = open (output_file, O_RDWR | O_BINARY | O_CREAT,
 			  stripped_ehdr->e_type == ET_REL ? 0666 : 0777);
       if (outfd < 0)
 	error (EXIT_FAILURE, errno, _("cannot open '%s'"), output_file);
@@ -2047,7 +2047,7 @@ DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
 static int
 open_file (const char *file, bool writable)
 {
-  int fd = open (file, writable ? O_RDWR : O_RDONLY);
+  int fd = open (file, writable ? (O_RDWR | O_BINARY) : (O_RDONLY | O_BINARY));
   if (fd < 0)
     error (EXIT_FAILURE, errno, _("cannot open '%s'"), file);
   return fd;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 611b88a..b091ae0 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,62 @@
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* alldts.c: Open files in O_BINARY.
+	* allfcts.c: Likewise.
+	* arextract.c: Likewise.
+	* arls.c: Likewise.
+	* arsymtest.c: Likewise.
+	* asm-tst1.c: Likewise.
+	* asm-tst2.c: Likewise.
+	* asm-tst3.c: Likewise.
+	* asm-tst7.c: Likewise.
+	* asm-tst8.c: Likewise.
+	* asm-tst9.c: Likewise.
+	* backtrace-data.c: Likewise.
+	* buildid.c: Likewise.
+	* debugaltlink.c: Likewise.
+	* debuglink.c: Likewise.
+	* dwarf-getmacros.c: Likewise.
+	* dwarf-getstring.c: Likewise.
+	* dwarf-ranges.c: Likewise.
+	* dwelfgnucompressed.c: Likewise.
+	* early-offscn.c: Likewise.
+	* ecp.c: Likewise.
+	* elfgetchdr.c: Likewise.
+	* elfgetzdata.c: Likewise.
+	* elfputzdata.c: Likewise.
+	* elfshphehdr.c: Likewise.
+	* elfstrmerge.c: Likewise.
+	* elfstrtab.c: Likewise.
+	* emptyfile.c: Likewise.
+	* fillfile.c: Likewise.
+	* get-aranges.c: Likewise.
+	* get-files.c: Likewise.
+	* get-lines.c: Likewise.
+	* get-pubnames.c: Likewise.
+	* getsrc_die.c: Likewise.
+	* newdata.c: Likewise.
+	* rdwrmmap.c: Likewise.
+	* rerequest_tag.c: Likewise.
+	* saridx.c: Likewise.
+	* scnnames.c: Likewise.
+	* sectiondump.c: Likewise.
+	* show-abbrev.c: Likewise.
+	* show-die-info.c: Likewise.
+	* showptable.c: Likewise.
+	* strptr.c: Likewise.
+	* test-elf_cntl_gelf_getshdr.c: Likewise.
+	* test-flag-nobits.c: Likewise.
+	* typeiter.c: Likewise.
+	* typeiter2.c: Likewise.
+	* update1.c: Likewise.
+	* update2.c: Likewise.
+	* update3.c: Likewise.
+	* update4.c: Likewise.
+	* vendorelf.c: Likewise.
+	* zstrptr.c: Likewise.
+
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* deleted.c: If fork() is unavailable, skip the test.
 
 2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/tests/alldts.c b/tests/alldts.c
index 28b3063..abddf08 100644
--- a/tests/alldts.c
+++ b/tests/alldts.c
@@ -68,7 +68,7 @@ main (void)
   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
 
   /* Open the file.  */
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %m\n", fname);
diff --git a/tests/allfcts.c b/tests/allfcts.c
index ad93dce..e4c3178 100644
--- a/tests/allfcts.c
+++ b/tests/allfcts.c
@@ -52,7 +52,7 @@ setup_alt (Dwarf *main)
     fprintf (stderr, "allfcts: dwelf_dwarf_gnu_debugaltlink: %s\n", dwarf_errmsg (-1));
     exit(1);
   }
-  int fd = open (alt_name, O_RDONLY);
+  int fd = open (alt_name, O_RDONLY | O_BINARY);
   if (fd < 0) {
     fprintf (stderr, "allfcts: open (%s): %s\n", alt_name, strerror(errno));
     exit(1);
@@ -76,7 +76,7 @@ main (int argc, char *argv[])
 {
   for (int i = 1; i < argc; ++i)
     {
-      int fd = open (argv[i], O_RDONLY);
+      int fd = open (argv[i], O_RDONLY | O_BINARY);
       if (fd < 0) {
 	fprintf (stderr, "open (%s): %s\n", argv[i], strerror(errno));
 	exit(1);
diff --git a/tests/arextract.c b/tests/arextract.c
index 2c4dc75..f3623b9 100644
--- a/tests/arextract.c
+++ b/tests/arextract.c
@@ -42,7 +42,7 @@ main (int argc, char *argv[])
     exit (1);
 
   /* Open the archive.  */
-  fd = open (argv[1], O_RDONLY);
+  fd = open (argv[1], O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("Cannot open input file: %m");
@@ -95,7 +95,7 @@ Failed to get base address for the archive element: %s\n",
 	    }
 
 	  /* Open the output file.  */
-	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, 0666);
+	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR | O_BINARY, 0666);
 	  if (outfd == -1)
 	    {
 	      printf ("cannot open output file: %m");
diff --git a/tests/arls.c b/tests/arls.c
index ca0d3e6..244b0fb 100644
--- a/tests/arls.c
+++ b/tests/arls.c
@@ -47,7 +47,7 @@ main (int argc, char *argv[])
 static int
 handle (const char *fname)
 {
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open '%s': %m\n", fname);
diff --git a/tests/arsymtest.c b/tests/arsymtest.c
index c724863..ca216eb 100644
--- a/tests/arsymtest.c
+++ b/tests/arsymtest.c
@@ -37,7 +37,7 @@ main (int argc, char *argv[])
     exit (1);
 
   /* Open the archive.  */
-  fd = open (argv[1], O_RDONLY);
+  fd = open (argv[1], O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("Cannot open input file: %m");
@@ -45,7 +45,7 @@ main (int argc, char *argv[])
     }
 
   /* Open the output file.  */
-  fp = fopen (argv[2], "w");
+  fp = fopen (argv[2], "wb");
   if (fp == NULL)
     {
       printf ("Cannot open output file: %m");
diff --git a/tests/asm-tst1.c b/tests/asm-tst1.c
index 9afc676..085ec3e 100644
--- a/tests/asm-tst1.c
+++ b/tests/asm-tst1.c
@@ -114,7 +114,7 @@ main (void)
     }
 
   /* Check the file.  */
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open generated file: %m\n");
diff --git a/tests/asm-tst2.c b/tests/asm-tst2.c
index 2556d0c..f7b3678 100644
--- a/tests/asm-tst2.c
+++ b/tests/asm-tst2.c
@@ -130,7 +130,7 @@ main (void)
     }
 
   /* Check the file.  */
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open generated file: %m\n");
diff --git a/tests/asm-tst3.c b/tests/asm-tst3.c
index e52cfbe..a8f145d 100644
--- a/tests/asm-tst3.c
+++ b/tests/asm-tst3.c
@@ -137,7 +137,7 @@ main (void)
     }
 
   /* Check the file.  */
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open generated file: %m\n");
diff --git a/tests/asm-tst7.c b/tests/asm-tst7.c
index 00cb2bf..aca91c7 100644
--- a/tests/asm-tst7.c
+++ b/tests/asm-tst7.c
@@ -71,7 +71,7 @@ main (void)
     }
 
   /* Check the file.  */
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open generated file: %m\n");
diff --git a/tests/asm-tst8.c b/tests/asm-tst8.c
index 4fb0d99..d1c64d2 100644
--- a/tests/asm-tst8.c
+++ b/tests/asm-tst8.c
@@ -72,7 +72,7 @@ main (void)
     }
 
   /* Check the file.  */
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open generated file: %m\n");
diff --git a/tests/asm-tst9.c b/tests/asm-tst9.c
index b6d0e43..368c075 100644
--- a/tests/asm-tst9.c
+++ b/tests/asm-tst9.c
@@ -164,7 +164,7 @@ main (void)
     }
 
   /* Check the file.  */
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open generated file: %m\n");
diff --git a/tests/backtrace-data.c b/tests/backtrace-data.c
index a387d8f..6db9878 100644
--- a/tests/backtrace-data.c
+++ b/tests/backtrace-data.c
@@ -98,7 +98,7 @@ maps_lookup (pid_t pid, Dwarf_Addr addr, GElf_Addr *basep)
   int i = asprintf (&fname, "/proc/%ld/maps", (long) pid);
   assert (errno == 0);
   assert (i > 0);
-  FILE *f = fopen (fname, "r");
+  FILE *f = fopen (fname, "rb");
   assert (errno == 0);
   assert (f);
   free (fname);
diff --git a/tests/buildid.c b/tests/buildid.c
index 2d33402..1049860 100644
--- a/tests/buildid.c
+++ b/tests/buildid.c
@@ -41,7 +41,7 @@ main (int argc, char *argv[])
   for (int i = 1; i < argc; i++)
     {
       const char *file = argv[i];
-      int fd = open (file, O_RDONLY);
+      int fd = open (file, O_RDONLY | O_BINARY);
       if (fd < 0)
 	error (EXIT_FAILURE, errno, "couldn't open file '%s'", file);
 
diff --git a/tests/debugaltlink.c b/tests/debugaltlink.c
index b470e31..4618b61 100644
--- a/tests/debugaltlink.c
+++ b/tests/debugaltlink.c
@@ -41,7 +41,7 @@ main (int argc, char *argv[])
   for (int i = 1; i < argc; i++)
     {
       const char *file = argv[i];
-      int fd = open (file, O_RDONLY);
+      int fd = open (file, O_RDONLY | O_BINARY);
       if (fd < 0)
 	error (EXIT_FAILURE, errno, "couldn't open file '%s'", file);
 
diff --git a/tests/debuglink.c b/tests/debuglink.c
index 935d102..ea5bd23 100644
--- a/tests/debuglink.c
+++ b/tests/debuglink.c
@@ -40,7 +40,7 @@ main (int argc, char *argv[])
   for (int i = 1; i < argc; i++)
     {
       const char *file = argv[i];
-      int fd = open (file, O_RDONLY);
+      int fd = open (file, O_RDONLY | O_BINARY);
       if (fd < 0)
 	error (EXIT_FAILURE, errno, "couldn't open file '%s'", file);
 
diff --git a/tests/dwarf-getmacros.c b/tests/dwarf-getmacros.c
index 92e093c..d5d1e2c 100644
--- a/tests/dwarf-getmacros.c
+++ b/tests/dwarf-getmacros.c
@@ -125,7 +125,7 @@ main (int argc, char *argv[])
   ptrdiff_t cuoff = strtol (argv[2], NULL, 0);
   bool new_style = argc > 3;
 
-  int fd = open (name, O_RDONLY);
+  int fd = open (name, O_RDONLY | O_BINARY);
   Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
 
   Dwarf_Die cudie_mem, *cudie = dwarf_offdie (dbg, cuoff, &cudie_mem);
diff --git a/tests/dwarf-getstring.c b/tests/dwarf-getstring.c
index ffa3e37..a735a80 100644
--- a/tests/dwarf-getstring.c
+++ b/tests/dwarf-getstring.c
@@ -37,7 +37,7 @@ main (int argc, char *argv[])
       Dwarf_Off offset = 0;
       size_t len;
 
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
       if (fd == -1)
 	{
 	  printf ("cannot open '%s': %m\n", argv[cnt]);
diff --git a/tests/dwarf-ranges.c b/tests/dwarf-ranges.c
index 4bcf96c..0eec90c 100644
--- a/tests/dwarf-ranges.c
+++ b/tests/dwarf-ranges.c
@@ -34,7 +34,7 @@ main (int argc, char *argv[])
   const char *name = argv[1];
   ptrdiff_t cuoff = strtol (argv[2], NULL, 0);
 
-  int fd = open (name, O_RDONLY);
+  int fd = open (name, O_RDONLY | O_BINARY);
   Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
 
   Dwarf_Die cudie_mem, *cudie = dwarf_offdie (dbg, cuoff, &cudie_mem);
diff --git a/tests/dwelfgnucompressed.c b/tests/dwelfgnucompressed.c
index 0132271..e842742 100644
--- a/tests/dwelfgnucompressed.c
+++ b/tests/dwelfgnucompressed.c
@@ -42,7 +42,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
 
       Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
       if (elf == NULL)
diff --git a/tests/early-offscn.c b/tests/early-offscn.c
index 924cb9e..d47abc8 100644
--- a/tests/early-offscn.c
+++ b/tests/early-offscn.c
@@ -35,7 +35,7 @@ main (int argc, char *argv[])
   elf_version (EV_CURRENT);
 
   /* Open the archive.  */
-  int fd = open (argv[1], O_RDONLY);
+  int fd = open (argv[1], O_RDONLY | O_BINARY);
   if (fd < 0)
     error (1, errno, "cannot open '%s'", argv[1]);
 
diff --git a/tests/ecp.c b/tests/ecp.c
index 38a6859..0882c54 100644
--- a/tests/ecp.c
+++ b/tests/ecp.c
@@ -34,7 +34,7 @@ main (int argc, char *argv[])
 
   elf_version (EV_CURRENT);
 
-  int infd = open (argv[1], O_RDONLY);
+  int infd = open (argv[1], O_RDONLY | O_BINARY);
   if (infd == -1)
     error (EXIT_FAILURE, errno, "cannot open input file '%s'", argv[1]);
 
@@ -46,6 +46,9 @@ main (int argc, char *argv[])
   int outfd = creat (argv[2], 0666);
   if (outfd == -1)
     error (EXIT_FAILURE, errno, "cannot open output file '%s'", argv[2]);
+#if (defined _WIN32 || defined __WIN32__)
+  _setmode (outfd, O_BINARY);
+#endif
 
   Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
   if (outelf == NULL)
diff --git a/tests/elfgetchdr.c b/tests/elfgetchdr.c
index 44ba178..f23b3af 100644
--- a/tests/elfgetchdr.c
+++ b/tests/elfgetchdr.c
@@ -42,7 +42,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
 
       Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
       if (elf == NULL)
diff --git a/tests/elfgetzdata.c b/tests/elfgetzdata.c
index 82afbe5..562adaf 100644
--- a/tests/elfgetzdata.c
+++ b/tests/elfgetzdata.c
@@ -50,7 +50,7 @@ main (int argc, char *argv[])
 
   for (cnt = 2; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
 
       Elf *elf = elf_begin (fd, mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL);
       if (elf == NULL)
diff --git a/tests/elfputzdata.c b/tests/elfputzdata.c
index 66ab77b..12b0571 100644
--- a/tests/elfputzdata.c
+++ b/tests/elfputzdata.c
@@ -55,7 +55,7 @@ main (int argc, char *argv[])
 
   for (cnt = 2; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
 
       Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
       if (elf == NULL)
diff --git a/tests/elfshphehdr.c b/tests/elfshphehdr.c
index 5a297e0..d1ab633 100644
--- a/tests/elfshphehdr.c
+++ b/tests/elfshphehdr.c
@@ -152,7 +152,7 @@ main (int argc __attribute__ ((unused)), char **argv __attribute ((unused)))
 {
   elf_version (EV_CURRENT);
 
-  int fd = fd = open("/dev/zero", O_WRONLY);
+  int fd = fd = open("/dev/zero", O_WRONLY | O_BINARY);
   check ("open", fd >= 0);
 
   Elf *elf;
diff --git a/tests/elfstrmerge.c b/tests/elfstrmerge.c
index 17f40c7..ff15f57 100644
--- a/tests/elfstrmerge.c
+++ b/tests/elfstrmerge.c
@@ -164,7 +164,7 @@ main (int argc, char **argv)
     fname = argv[1];
   else
     fname = argv[3];
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd < 0)
     fail_errno ("couldn't open", fname);
 
@@ -367,7 +367,7 @@ main (int argc, char **argv)
   else
     {
       fnew = argv[2];
-      fdnew = open (fnew, O_WRONLY | O_CREAT, st.st_mode & ALLPERMS);
+      fdnew = open (fnew, O_WRONLY | O_CREAT | O_BINARY, st.st_mode & ALLPERMS);
     }
 
   if (fdnew < 0)
diff --git a/tests/elfstrtab.c b/tests/elfstrtab.c
index c27d6cf..69ba46b 100644
--- a/tests/elfstrtab.c
+++ b/tests/elfstrtab.c
@@ -134,7 +134,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0;
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -280,7 +280,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Read the ELF from disk now.  */
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
@@ -349,7 +349,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   // And read it in one last time.
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/emptyfile.c b/tests/emptyfile.c
index 6d08624..ad02496 100644
--- a/tests/emptyfile.c
+++ b/tests/emptyfile.c
@@ -67,7 +67,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0; // Reset strtab strings index
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -125,7 +125,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Reread the ELF from disk now.  */
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
@@ -208,7 +208,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   // And read it in one last time.
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/fillfile.c b/tests/fillfile.c
index 915e249..4529637 100644
--- a/tests/fillfile.c
+++ b/tests/fillfile.c
@@ -201,7 +201,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0; // Reset strtab strings index
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -266,7 +266,7 @@ check_elf (const char *fname, int class, int use_mmap)
 
   /* Reread the ELF from disk now.  */
   printf ("Rereading %s\n", fname);
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
@@ -347,7 +347,7 @@ check_elf (const char *fname, int class, int use_mmap)
 
   // And read it in one last time.
   printf ("Rereading %s again\n", fname);
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/get-aranges.c b/tests/get-aranges.c
index 7f85cda..e41fb12 100644
--- a/tests/get-aranges.c
+++ b/tests/get-aranges.c
@@ -43,7 +43,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
 
       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
       if (dbg == NULL)
diff --git a/tests/get-files.c b/tests/get-files.c
index 0409173..205c10b 100644
--- a/tests/get-files.c
+++ b/tests/get-files.c
@@ -34,7 +34,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
 
       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
       if (dbg == NULL)
diff --git a/tests/get-lines.c b/tests/get-lines.c
index c361a2c..5934f8c 100644
--- a/tests/get-lines.c
+++ b/tests/get-lines.c
@@ -35,7 +35,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
 
       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
       if  (dbg == NULL)
diff --git a/tests/get-pubnames.c b/tests/get-pubnames.c
index 4777f49..63fe193 100644
--- a/tests/get-pubnames.c
+++ b/tests/get-pubnames.c
@@ -71,7 +71,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
       if (dbg == NULL)
 	{
diff --git a/tests/getsrc_die.c b/tests/getsrc_die.c
index 055aede..3c5060b 100644
--- a/tests/getsrc_die.c
+++ b/tests/getsrc_die.c
@@ -33,7 +33,7 @@ int
 main (int argc, char *argv[])
 {
   /* file addr+ */
-  int fd = open (argv[1], O_RDONLY);
+  int fd = open (argv[1], O_RDONLY | O_BINARY);
   Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
   if  (dbg == NULL)
     error (-1, 0, "dwarf_begin (%s): %s\n", argv[1], dwarf_errmsg (-1));
diff --git a/tests/newdata.c b/tests/newdata.c
index 9af9956..bb9f313 100644
--- a/tests/newdata.c
+++ b/tests/newdata.c
@@ -243,7 +243,7 @@ check_elf (int class, int use_mmap)
 
   printf ("\ncheck_elf: %s\n", fname);
 
-  int fd = open (fname, O_RDWR|O_CREAT|O_TRUNC, 00666);
+  int fd = open (fname, O_RDWR | O_BINARY|O_CREAT|O_TRUNC, 00666);
   if (fd == -1)
     {
       printf ("cannot create `%s': %s\n", fname, strerror (errno));
@@ -268,7 +268,7 @@ check_elf (int class, int use_mmap)
   close (fd);
 
   // Read the ELF from disk now.  And add new data directly.
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
@@ -298,7 +298,7 @@ check_elf (int class, int use_mmap)
   close (fd);
 
   // Read the ELF from disk now.  And add new data after raw reading.
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
@@ -350,7 +350,7 @@ check_elf (int class, int use_mmap)
   close (fd);
 
   // Read the ELF from disk now.  And add new data after data reading.
-  fd = open (fname, O_RDONLY);
+  fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/rdwrmmap.c b/tests/rdwrmmap.c
index 6f027df..1a968eb 100644
--- a/tests/rdwrmmap.c
+++ b/tests/rdwrmmap.c
@@ -28,7 +28,7 @@
 int
 main (int argc __attribute__ ((unused)), char *argv[])
 {
-  int fd = open (argv[1], O_RDWR);
+  int fd = open (argv[1], O_RDWR | O_BINARY);
   if (fd < 0)
     error (2, errno, "open: %s", argv[1]);
 
diff --git a/tests/rerequest_tag.c b/tests/rerequest_tag.c
index b4d4627..02f3902 100644
--- a/tests/rerequest_tag.c
+++ b/tests/rerequest_tag.c
@@ -27,7 +27,7 @@ main (int argc, char **argv)
 {
   assert (argc > 1);
 
-  int i = open (argv[1], O_RDONLY);
+  int i = open (argv[1], O_RDONLY | O_BINARY);
   assert (i >= 0);
 
   Dwarf *dw = dwarf_begin (i, DWARF_C_READ);
diff --git a/tests/saridx.c b/tests/saridx.c
index 8a450d8..37c0bbf 100644
--- a/tests/saridx.c
+++ b/tests/saridx.c
@@ -106,7 +106,7 @@ main (int argc, char *argv[])
     error (EXIT_FAILURE, 0, "No input file given");
 
   /* Open the input file.  */
-  fd = open (argv[arg], O_RDONLY);
+  fd = open (argv[arg], O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       perror ("cannot open input file");
diff --git a/tests/scnnames.c b/tests/scnnames.c
index 7f26825..0c5922f 100644
--- a/tests/scnnames.c
+++ b/tests/scnnames.c
@@ -39,7 +39,7 @@ main (int argc, char *argv[])
       exit (1);
     }
 
-  fd = open (argv[1], O_RDONLY);
+  fd = open (argv[1], O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
diff --git a/tests/sectiondump.c b/tests/sectiondump.c
index 3033fed..edec1a7 100644
--- a/tests/sectiondump.c
+++ b/tests/sectiondump.c
@@ -45,7 +45,7 @@ main (int argc, char *argv[])
 
   /* Open the test file.  This is given as the first parameter to the
      program.  */
-  fd = open (argv[1], O_RDONLY);
+  fd = open (argv[1], O_RDONLY | O_BINARY);
   if (fd == -1)
     error (EXIT_FAILURE, errno, "cannot open input file `%s'", argv[1]);
 
diff --git a/tests/show-abbrev.c b/tests/show-abbrev.c
index b0af029..3a0c91d 100644
--- a/tests/show-abbrev.c
+++ b/tests/show-abbrev.c
@@ -31,7 +31,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
       if  (dbg == NULL)
 	{
diff --git a/tests/show-die-info.c b/tests/show-die-info.c
index 34e27a3..1eb0bfb 100644
--- a/tests/show-die-info.c
+++ b/tests/show-die-info.c
@@ -315,7 +315,7 @@ main (int argc, char *argv[])
 
   for (cnt = 1; cnt < argc; ++cnt)
     {
-      int fd = open (argv[cnt], O_RDONLY);
+      int fd = open (argv[cnt], O_RDONLY | O_BINARY);
       Dwarf *dbg;
 
       printf ("file: %s\n", basename (argv[cnt]));
diff --git a/tests/showptable.c b/tests/showptable.c
index a794b0e..5586c5f 100644
--- a/tests/showptable.c
+++ b/tests/showptable.c
@@ -38,7 +38,7 @@ main (int argc, char *argv[])
       exit (1);
     }
 
-  fd = open (argv[1], O_RDONLY);
+  fd = open (argv[1], O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open \"%s\": %s\n", argv[1], strerror (errno));
diff --git a/tests/strptr.c b/tests/strptr.c
index 759664a..98f9c46 100644
--- a/tests/strptr.c
+++ b/tests/strptr.c
@@ -45,7 +45,7 @@ main (int argc, char *argv[])
   elf_version (EV_CURRENT);
 
   /* Read the ELF from disk now.  */
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/test-elf_cntl_gelf_getshdr.c b/tests/test-elf_cntl_gelf_getshdr.c
index 7371110..2bc822c 100644
--- a/tests/test-elf_cntl_gelf_getshdr.c
+++ b/tests/test-elf_cntl_gelf_getshdr.c
@@ -67,7 +67,7 @@ main (int argc, char *argv[])
 
   elf_version (EV_CURRENT);
 
-  int fd = open (argv[2], O_RDONLY);
+  int fd = open (argv[2], O_RDONLY | O_BINARY);
   if (fd < 0)
     {
       fprintf (stderr, "Cannot open input file %s: %s\n", argv[2],
diff --git a/tests/test-flag-nobits.c b/tests/test-flag-nobits.c
index 15d44ea..5472fe0 100644
--- a/tests/test-flag-nobits.c
+++ b/tests/test-flag-nobits.c
@@ -30,7 +30,7 @@ main (int argc, char **argv)
 
   elf_version (EV_CURRENT);
 
-  int fd = open (argv[1], O_RDONLY);
+  int fd = open (argv[1], O_RDONLY | O_BINARY);
   Elf *stripped = elf_begin (fd, ELF_C_READ, NULL);
 
   Elf_Scn *scn = NULL;
diff --git a/tests/typeiter.c b/tests/typeiter.c
index dff4526..a3ef823 100644
--- a/tests/typeiter.c
+++ b/tests/typeiter.c
@@ -29,7 +29,7 @@ main (int argc, char *argv[])
 {
   for (int i = 1; i < argc; ++i)
     {
-      int fd = open (argv[i], O_RDONLY);
+      int fd = open (argv[i], O_RDONLY | O_BINARY);
 
       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
       if (dbg != NULL)
diff --git a/tests/typeiter2.c b/tests/typeiter2.c
index 35b6a12..c1b19f7 100644
--- a/tests/typeiter2.c
+++ b/tests/typeiter2.c
@@ -30,7 +30,7 @@ main (int argc, char *argv[])
 {
   for (int i = 1; i < argc; ++i)
     {
-      int fd = open (argv[i], O_RDONLY);
+      int fd = open (argv[i], O_RDONLY | O_BINARY);
 
       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
       if (dbg != NULL)
diff --git a/tests/update1.c b/tests/update1.c
index f4c1475..a571618 100644
--- a/tests/update1.c
+++ b/tests/update1.c
@@ -38,7 +38,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Elf32_Ehdr *ehdr;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update2.c b/tests/update2.c
index 5805163..3e22879 100644
--- a/tests/update2.c
+++ b/tests/update2.c
@@ -39,7 +39,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Elf32_Phdr *phdr;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update3.c b/tests/update3.c
index 7a4224d..d619bed 100644
--- a/tests/update3.c
+++ b/tests/update3.c
@@ -46,7 +46,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Dwelf_Strent *shstrtabse;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update4.c b/tests/update4.c
index a9bd4bf..8196b8c 100644
--- a/tests/update4.c
+++ b/tests/update4.c
@@ -50,7 +50,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Dwelf_Strent *shstrtabse;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/vendorelf.c b/tests/vendorelf.c
index bc13cce..835e941 100644
--- a/tests/vendorelf.c
+++ b/tests/vendorelf.c
@@ -36,7 +36,7 @@ check_elf (const char *fname, int class, int use_mmap)
 {
   printf ("\nfname: %s\n", fname);
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -124,7 +124,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Reread the ELF from disk now.  */
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY | O_BINARY, 0666);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/zstrptr.c b/tests/zstrptr.c
index 6d8e19f..8560dfd 100644
--- a/tests/zstrptr.c
+++ b/tests/zstrptr.c
@@ -45,7 +45,7 @@ main (int argc, char *argv[])
   elf_version (EV_CURRENT);
 
   /* Read the ELF from disk now.  */
-  int fd = open (fname, O_RDONLY);
+  int fd = open (fname, O_RDONLY | O_BINARY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
-- 
2.1.4



More information about the Elfutils-devel mailing list