[PATCH] Add mman.h/.c for win32

2017-05-04 Thread Ulf Hermann
We cannot get mmap() and friends from gnulib and they don't exist on windows.
The functionality we need can be implemnted using native win32 functions, 
though.

Signed-off-by: Ulf Hermann 
---
 configure.ac|   7 +++
 libgnu/Makefile.am  |  17 +-
 libgnu/mman_win32.c | 140 
 libgnu/sys_mman.win32.h |  63 ++
 4 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/mman_win32.c
 create mode 100644 libgnu/sys_mman.win32.h

diff --git a/configure.ac b/configure.ac
index 1e6c844..6a2f991 100644
--- a/configure.ac
+++ b/configure.ac
@@ -550,6 +550,13 @@ AM_CONDITIONAL(HAVE_ENDIAN_H, [test 
"x$ac_cv_have_decl_BYTE_ORDER" = "xyes"])
 AC_CHECK_DECLS([bswap_32], [], [], [[#include ]])
 AM_CONDITIONAL(HAVE_BYTESWAP_H, [test "x$ac_cv_have_decl_bswap_32" = "xyes"])
 
+AC_CHECK_HEADERS(sys/mman.h)
+AM_CONDITIONAL(HAVE_SYS_MMAN_H, [test "x$ac_cv_header_sys_mman_h" = "xyes"])
+if test "x$ac_cv_header_sys_mman_h" != "xyes"; then
+   AC_CHECK_DECLS([MapViewOfFile], [], [], [[#include ]])
+fi
+AM_CONDITIONAL(USE_WIN32_MMAN, [test "x$ac_cv_have_decl_MapViewOfFile" = 
"xyes"])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 37fdb9c..5af121a 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -35,7 +35,7 @@ noinst_LIBRARIES =
 MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
-EXTRA_DIST = endian.in.h byteswap.in.h
+EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c
 CLEANFILES =
 SUFFIXES =
 
@@ -53,4 +53,19 @@ BUILT_SOURCES += byteswap.h
 MOSTLYCLEANFILES += byteswap.h
 endif
 
+if !HAVE_SYS_MMAN_H
+if USE_WIN32_MMAN
+sys/mman.h: sys_mman.win32.h
+   $(AM_V_GEN)rm -f $@ && mkdir -p sys && cat $< > $@
+BUILT_SOURCES += sys/mman.h
+MOSTLYCLEANFILES += sys/mman.h
+endif
+endif
+
 include gnulib.am
+
+if !HAVE_SYS_MMAN_H
+if USE_WIN32_MMAN
+libgnu_a_SOURCES += mman_win32.c
+endif
+endif
diff --git a/libgnu/mman_win32.c b/libgnu/mman_win32.c
new file mode 100644
index 000..78966c2
--- /dev/null
+++ b/libgnu/mman_win32.c
@@ -0,0 +1,140 @@
+/* Replacement for mmap(2) and friends on windows
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#include 
+#include 
+#include 
+#include 
+
+void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t 
offset)
+{
+if (fd == -1) {
+errno = EBADF;
+return MAP_FAILED;
+}
+
+HANDLE file = (HANDLE)_get_osfhandle(fd);
+if (file == INVALID_HANDLE_VALUE) {
+errno = EBADF;
+return MAP_FAILED;
+}
+
+// Apparently there is no writeonly - we might get the write-copy mode to 
work, though.
+DWORD flProtect = PROT_NONE;
+if (prot & PROT_READ) {
+if (prot & PROT_WRITE) {
+if (prot & PROT_EXEC) {
+if (flags & MAP_PRIVATE)
+flProtect = PAGE_EXECUTE_WRITECOPY;
+else
+flProtect = PAGE_EXECUTE_READWRITE;
+} else {
+if (flags & MAP_PRIVATE)
+flProtect = PAGE_WRITECOPY;
+else
+flProtect = PAGE_READWRITE;
+}
+} else if (prot & PROT_EXEC) {
+flProtect = PAGE_EXECUTE_READ;
+}
+} else if (prot & PROT_EXEC) {
+flProtect = PAGE_EXECUTE;
+} else {
+errno = EPERM;
+return MAP_FAILED;
+}
+
+HANDLE fileMapping = CreateFileMapping(file, NULL, flProtect, 0, 0, NULL);
+if (fileMapping == NULL) {
+errno = EINVAL; // windows docs say this happens on disk full. EINVAL 
is close enough.
+return MAP_FAILED;
+}
+
+// you can only have either read-only, read-write, copy-on-write access. 
Either can be combined
+// with exec. We try to map the given flags and prot parameters as best as 
we can.

[PATCH v2] Add mman.h/.c for win32

2017-05-04 Thread Ulf Hermann
We cannot get mmap() and friends from gnulib and they don't exist on windows.
The functionality we need can be implemnted using native win32 functions, 
though.

(changelog entries were missing in V1)

Signed-off-by: Ulf Hermann 
---
 ChangeLog   |   4 ++
 configure.ac|   7 +++
 libgnu/ChangeLog|   7 +++
 libgnu/Makefile.am  |  17 +-
 libgnu/mman_win32.c | 140 
 libgnu/sys_mman.win32.h |  63 ++
 6 files changed, 237 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/mman_win32.c
 create mode 100644 libgnu/sys_mman.win32.h

diff --git a/ChangeLog b/ChangeLog
index 01f3197..2c0e34d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  
+
+   * configure.ac: Check for sys/mman.h or alternatively MapViewOfFile().
+
 2017-05-03  Ulf Hermann  
 
* configure.ac: Check if endian.h and byteswap.h are available.
diff --git a/configure.ac b/configure.ac
index 1e6c844..6a2f991 100644
--- a/configure.ac
+++ b/configure.ac
@@ -550,6 +550,13 @@ AM_CONDITIONAL(HAVE_ENDIAN_H, [test 
"x$ac_cv_have_decl_BYTE_ORDER" = "xyes"])
 AC_CHECK_DECLS([bswap_32], [], [], [[#include ]])
 AM_CONDITIONAL(HAVE_BYTESWAP_H, [test "x$ac_cv_have_decl_bswap_32" = "xyes"])
 
+AC_CHECK_HEADERS(sys/mman.h)
+AM_CONDITIONAL(HAVE_SYS_MMAN_H, [test "x$ac_cv_header_sys_mman_h" = "xyes"])
+if test "x$ac_cv_header_sys_mman_h" != "xyes"; then
+   AC_CHECK_DECLS([MapViewOfFile], [], [], [[#include ]])
+fi
+AM_CONDITIONAL(USE_WIN32_MMAN, [test "x$ac_cv_have_decl_MapViewOfFile" = 
"xyes"])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index ca38be2..60a049f 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,3 +1,10 @@
+2017-05-04  Ulf Hermann  
+
+   * Makefile.am: If sys/mman.h is unavailable, but MapViewOfFile is,
+   then use our own implementation of mmap and related functions.
+   * mman_win32.c: New file.
+   * sys_mman.win32.h: New file.
+
 2017-05-03  Ulf Hermann  
 
* Makefile.am: Make endian.h and byteswap.h available if they don't
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 37fdb9c..5af121a 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -35,7 +35,7 @@ noinst_LIBRARIES =
 MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
-EXTRA_DIST = endian.in.h byteswap.in.h
+EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c
 CLEANFILES =
 SUFFIXES =
 
@@ -53,4 +53,19 @@ BUILT_SOURCES += byteswap.h
 MOSTLYCLEANFILES += byteswap.h
 endif
 
+if !HAVE_SYS_MMAN_H
+if USE_WIN32_MMAN
+sys/mman.h: sys_mman.win32.h
+   $(AM_V_GEN)rm -f $@ && mkdir -p sys && cat $< > $@
+BUILT_SOURCES += sys/mman.h
+MOSTLYCLEANFILES += sys/mman.h
+endif
+endif
+
 include gnulib.am
+
+if !HAVE_SYS_MMAN_H
+if USE_WIN32_MMAN
+libgnu_a_SOURCES += mman_win32.c
+endif
+endif
diff --git a/libgnu/mman_win32.c b/libgnu/mman_win32.c
new file mode 100644
index 000..78966c2
--- /dev/null
+++ b/libgnu/mman_win32.c
@@ -0,0 +1,140 @@
+/* Replacement for mmap(2) and friends on windows
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#include 
+#include 
+#include 
+#include 
+
+void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t 
offset)
+{
+if (fd == -1) {
+errno = EBADF;
+return MAP_FAILED;
+}
+
+HANDLE file = (HANDLE)_get_osfhandle(fd);
+if (file == INVALID_HANDLE_VALUE) {
+errno = EBADF;
+return MAP_FAILED;
+}
+
+// Apparently there is no writeonly - we might get the write-copy mode to 
work, though.
+DWORD flProtect = PROT_NONE;
+if (prot & PROT_READ) {
+if (prot & PROT_WRITE) {
+if (prot & PROT_EXEC) {
+if (flags & MAP_PRIVATE)
+flProtect = PAGE_EXECUTE_WRITECOPY;
+else
+   

[PATCH] Add sysconf replacement for win32

2017-05-04 Thread Ulf Hermann
We cannot get sysconf() from gnulib and the only thing we need it for
is getting the page size. Therefore, of the various possible sysconf
parameters we only define and implement _SC_PAGESIZE in our replacement.

Signed-off-by: Ulf Hermann 
---
 ChangeLog  |  6 ++
 configure.ac   | 20 
 libgnu/ChangeLog   |  6 ++
 libgnu/Makefile.am |  8 +++-
 libgnu/sysconf_win32.c | 42 ++
 5 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/sysconf_win32.c

diff --git a/ChangeLog b/ChangeLog
index 2c0e34d..71c73e5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for sysconf() and GetSystemInfo(). Add a
+   declaration of sysconf to config.h if GetSystemInfo() is available,
+   but not sysconf().
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for sys/mman.h or alternatively MapViewOfFile().
 
 2017-05-03  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index 6a2f991..2d3ad04 100644
--- a/configure.ac
+++ b/configure.ac
@@ -557,6 +557,26 @@ if test "x$ac_cv_header_sys_mman_h" != "xyes"; then
 fi
 AM_CONDITIONAL(USE_WIN32_MMAN, [test "x$ac_cv_have_decl_MapViewOfFile" = 
"xyes"])
 
+AC_CHECK_DECLS([sysconf], [], [], [[#include ]])
+AM_CONDITIONAL(HAVE_SYSCONF, [test "x$ac_cv_have_decl_sysconf" = "xyes"])
+if test "x$ac_cv_have_decl_sysconf" != "xyes"; then
+   AC_CHECK_DECLS([GetSystemInfo], [], [], [[#include ]])
+fi
+AM_CONDITIONAL(USE_WIN32_SYSCONF, [test "x$ac_cv_have_decl_GetSystemInfo" = 
"xyes"])
+
+if test "x$ac_cv_have_decl_GetSystemInfo" = "xyes"; then
+   AC_DEFINE([USE_WIN32_SYSCONF], [1], [Use sysconf replacement for win32])
+fi
+
+AH_VERBATIM([USE_WIN32_SYSCONF],
+   [/* Define sysconf(3) here if it is not available from a system header. 
*/
+#undef USE_WIN32_SYSCONF
+#ifdef USE_WIN32_SYSCONF
+#define _SC_PAGESIZE 1
+long sysconf(int name);
+#endif
+])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index 60a049f..aa0e603 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,5 +1,11 @@
 2017-05-04  Ulf Hermann  
 
+   * Makefile.am: If sysconf() is unavailable, but GetSystemInfo() is
+   available, compile the win32 version of sysconf.
+   * sysconf_win32.c: New file.
+
+2017-05-04  Ulf Hermann  
+
* Makefile.am: If sys/mman.h is unavailable, but MapViewOfFile is,
then use our own implementation of mmap and related functions.
* mman_win32.c: New file.
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 5af121a..05a8f61 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -35,7 +35,7 @@ noinst_LIBRARIES =
 MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
-EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c
+EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c
 CLEANFILES =
 SUFFIXES =
 
@@ -69,3 +69,9 @@ if USE_WIN32_MMAN
 libgnu_a_SOURCES += mman_win32.c
 endif
 endif
+
+if !HAVE_SYSCONF
+if USE_WIN32_SYSCONF
+libgnu_a_SOURCES += sysconf_win32.c
+endif
+endif
diff --git a/libgnu/sysconf_win32.c b/libgnu/sysconf_win32.c
new file mode 100644
index 000..34ddf4b
--- /dev/null
+++ b/libgnu/sysconf_win32.c
@@ -0,0 +1,42 @@
+/* Replacement for sysconf() on windows
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#include 
+#include 
+#include 
+
+long int sysconf(int name) {
+  if (name == _SC_PAGESIZE) {
+SYSTEM_INFO info;
+GetSystemInfo(&info);
+return info.dwPageSize;
+  } else {
+errno = EINVAL;
+return -1;
+  }
+}
-- 
2.1.4



[PATCH] Add ar.h for systems where it doesn't exist

2017-05-04 Thread Ulf Hermann
Signed-off-by: Ulf Hermann 
---
 ChangeLog  |  4 
 configure.ac   |  3 +++
 libgnu/ChangeLog   |  5 +
 libgnu/Makefile.am |  9 -
 libgnu/ar.in.h | 59 ++
 5 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/ar.in.h

diff --git a/ChangeLog b/ChangeLog
index 71c73e5..a2655c7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for ar.h.
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for sysconf() and GetSystemInfo(). Add a
declaration of sysconf to config.h if GetSystemInfo() is available,
but not sysconf().
diff --git a/configure.ac b/configure.ac
index 2d3ad04..ba560d6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -577,6 +577,9 @@ long sysconf(int name);
 #endif
 ])
 
+AC_CHECK_HEADERS(ar.h)
+AM_CONDITIONAL(HAVE_AR_H, [test "x$ac_cv_header_ar_h" = "xyes"])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index aa0e603..3d30ce1 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * Makefile.am: If ar.h is unavailable, use our own version.
+   * ar.in.h: New file.
+
+2017-05-04  Ulf Hermann  
+
* Makefile.am: If sysconf() is unavailable, but GetSystemInfo() is
available, compile the win32 version of sysconf.
* sysconf_win32.c: New file.
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 05a8f61..d7af59a 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -35,7 +35,7 @@ noinst_LIBRARIES =
 MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
-EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c
+EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h
 CLEANFILES =
 SUFFIXES =
 
@@ -53,6 +53,13 @@ BUILT_SOURCES += byteswap.h
 MOSTLYCLEANFILES += byteswap.h
 endif
 
+if !HAVE_AR_H
+ar.h: ar.in.h
+   $(AM_V_GEN)rm -f $@ && cat $< > $@
+BUILT_SOURCES += ar.h
+MOSTLYCLEANFILES += ar.h
+endif
+
 if !HAVE_SYS_MMAN_H
 if USE_WIN32_MMAN
 sys/mman.h: sys_mman.win32.h
diff --git a/libgnu/ar.in.h b/libgnu/ar.in.h
new file mode 100644
index 000..acdae2f
--- /dev/null
+++ b/libgnu/ar.in.h
@@ -0,0 +1,59 @@
+/* Header describing `ar' archive file format.
+   Copyright (C) 1996-2017 Free Software Foundation, Inc.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifndef _AR_H
+#define _AR_H 1
+
+/* Archive files start with the ARMAG identifying string.  Then follows a
+   `struct ar_hdr', and as many bytes of member file data as its `ar_size'
+   member indicates, for each member file.  */
+
+#define ARMAG   "!\n" /* String that begins an archive file.  */
+#define SARMAG  8   /* Size of that string.  */
+
+#define ARFMAG  "`\n"   /* String in ar_fmag at end of each header.  */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct ar_hdr
+{
+char ar_name[16];  /* Member file name, sometimes / terminated. */
+char ar_date[12];  /* File date, decimal seconds since Epoch.  */
+char ar_uid[6], ar_gid[6]; /* User and group IDs, in ASCII decimal.  */
+char ar_mode[8];   /* File mode, in ASCII octal.  */
+char ar_size[10];  /* File size, in ASCII decimal.  */
+char ar_fmag[2];   /* Always contains ARFMAG.  */
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ar.h */
-- 
2.1.4



[PATCH v2] Add a "selfcontained" mode to provide missing headers on windows

2017-05-04 Thread Ulf Hermann
With MSVC there is no features.h, uid_t, gid_t, mode_t, and pid_t are
not defined and there is also no elf.h. To make it possible to build
other software against libelf and libdw, install our own version of
elf.h, and a bare-bones features.h that provides exactly the above
declarations. The features.h declarations are provided like mingw
defines them.

(v1 had features.h directly in the install rule, which is impractical
as we will need to add more things later)

Signed-off-by: Ulf Hermann 
---
 ChangeLog  |  4 
 configure.ac   |  6 ++
 lib/ChangeLog  |  6 ++
 lib/Makefile.am| 17 -
 lib/features.h.in  | 43 +++
 libelf/Makefile.am |  9 -
 tests/ChangeLog|  5 +
 tests/Makefile.am  |  6 ++
 8 files changed, 94 insertions(+), 2 deletions(-)
 create mode 100644 lib/features.h.in

diff --git a/ChangeLog b/ChangeLog
index d43eeb6..cf14aec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-03  Ulf Hermann  
+
+   * configure.ac: Add an --enable-selfcontained switch.
+
 2017-04-28  Ulf Hermann  
 
* configure.ac: Determine the binary format we're building natively.
diff --git a/configure.ac b/configure.ac
index e4b2946..1e7778d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -124,6 +124,12 @@ AS_HELP_STRING([--enable-gnulib],
use_gnulib=$enableval, use_gnulib=no)
 AM_CONDITIONAL(USE_GNULIB, test "$use_gnulib" = yes)
 
+AC_ARG_ENABLE([selfcontained],
+AS_HELP_STRING([--enable-selfcontained],
+  [install extra headers to enable including and linking the 
libraries on non-GNU systems]),
+  selfcontained=$enableval, selfcontained=no)
+AM_CONDITIONAL(SELFCONTAINED, test "$selfcontained" = yes)
+
 AC_PROG_CC
 
 gl_EARLY
diff --git a/lib/ChangeLog b/lib/ChangeLog
index ecc6179..2da6235 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,9 @@
+2017-05-03  Ulf Hermann  
+
+   * features.h.in: New file.
+   * Makefile.am: In selfcontained mode, install features.h.in as
+   features.h.
+
 2017-04-27  Ulf Hermann  
 
* eu-config.h: Define attribute_hidden to be empty if the compiler
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 17d16d0..afff717 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -39,8 +39,23 @@ libeu_a_SOURCES = xstrdup.c xstrndup.c xmalloc.c 
next_prime.c \
 
 noinst_HEADERS = fixedsizehash.h libeu.h system.h dynamicsizehash.h list.h \
 md5.h sha1.h eu-config.h color.h printversion.h
-EXTRA_DIST = dynamicsizehash.c
+EXTRA_DIST = dynamicsizehash.c features.h.in
 
 if !GPROF
 xmalloc_CFLAGS = -ffunction-sections
 endif
+
+if SELFCONTAINED
+install-headers: install-am features.h.in
+   $(mkinstalldirs) $(DESTDIR)$(includedir)
+   $(INSTALL_HEADER) $(top_srcdir)/lib/features.h.in 
$(DESTDIR)$(includedir)/features.h
+
+uninstall-headers: uninstall-am
+   rm -f $(DESTDIR)$(includedir)/features.h
+else
+install-headers:
+uninstall-headers:
+endif
+
+install: install-am install-headers
+uninstall: uninstall-am uninstall-headers
diff --git a/lib/features.h.in b/lib/features.h.in
new file mode 100644
index 000..6eb3c67
--- /dev/null
+++ b/lib/features.h.in
@@ -0,0 +1,43 @@
+/* This file defines uid_t, gid_t, mode_t, pid_t
+   Copyright (C) 2017 The Qt Company Ltd
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifndef _FEATURES_H
+#define _FEATURES_H 1
+
+#include 
+
+typedef uint32_t uid_t;
+typedef uint32_t gid_t;
+typedef uint32_t mode_t;
+#ifdef _WIN64
+typedef int64_t  pid_t;
+#else
+typedef int32_t  pid_t;
+#endif
+
+#endif /* features.h */
diff --git a/libelf/Makefile.am b/libelf/Makefile.am
index 3286dda..1a2b85a 100644
--- a/libelf/Makefile.am
+++ b/libelf/Makefile.am
@@ -141,9 +141,16 @@ uninstall: uninstall-am uninstall-lib
rm -f $(DESTDIR)$(libdir)/$(libelf_SONAME)
rm -f $(DESTDIR)$(libdir)/$(libelf_BARE)
 
-noinst_HEADERS = elf.h abstract.h common.h exttypes.h gelf_xlate.h libelfP.h \
+noinst_HEADERS = abstract.h common.h exttypes.h g

[PATCH] Add empty features.h replacement

2017-05-04 Thread Ulf Hermann
gnulib #defines all the types we need from it in config.h, so it is empty.
We still need it because other files #include it. We use this for building
elfutils itself. The other features.h in lib gets installed in selfcontained
mode for other projects that include elfutils headers in a different
environment.

Signed-off-by: Ulf Hermann 
---
 ChangeLog|  4 
 configure.ac |  3 +++
 libgnu/ChangeLog |  5 +
 libgnu/Makefile.am   | 10 +-
 libgnu/features.in.h | 35 +++
 5 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/features.in.h

diff --git a/ChangeLog b/ChangeLog
index 814a133..4024550 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for features.h.
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for ar.h.
 
 2017-05-04  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index ba560d6..fc5edf8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -580,6 +580,9 @@ long sysconf(int name);
 AC_CHECK_HEADERS(ar.h)
 AM_CONDITIONAL(HAVE_AR_H, [test "x$ac_cv_header_ar_h" = "xyes"])
 
+AC_CHECK_HEADERS(features.h)
+AM_CONDITIONAL(HAVE_FEATURES_H, [test "x$ac_cv_header_features_h" = "xyes"])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index 3d30ce1..7d70bfc 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * Makefile.am: If features.h is unavailable, use our own.
+   * features.in.h: New file.
+
+2017-05-04  Ulf Hermann  
+
* Makefile.am: If ar.h is unavailable, use our own version.
* ar.in.h: New file.
 
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index d7af59a..ed8aff1 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -35,7 +35,8 @@ noinst_LIBRARIES =
 MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
-EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h
+EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h \
+ features.in.h
 CLEANFILES =
 SUFFIXES =
 
@@ -60,6 +61,13 @@ BUILT_SOURCES += ar.h
 MOSTLYCLEANFILES += ar.h
 endif
 
+if !HAVE_FEATURES_H
+features.h: features.in.h
+   $(AM_V_GEN)rm -f $@ && cat $< > $@
+BUILT_SOURCES += features.h
+MOSTLYCLEANFILES += features.h
+endif
+
 if !HAVE_SYS_MMAN_H
 if USE_WIN32_MMAN
 sys/mman.h: sys_mman.win32.h
diff --git a/libgnu/features.in.h b/libgnu/features.in.h
new file mode 100644
index 000..41f94a3
--- /dev/null
+++ b/libgnu/features.in.h
@@ -0,0 +1,35 @@
+/* This file defines uid_t, gid_t, mode_t, pid_t
+   Copyright (C) 2017 The Qt Company Ltd
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifndef _FEATURES_H
+#define _FEATURES_H 1
+
+// Don't define the actual types here.
+// Gnulib adds #define statements for them to config.h
+
+#endif /* features.h */
-- 
2.1.4



[PATCH] Add a stdio_ext.h that defines __fsetlocking away

2017-05-04 Thread Ulf Hermann
__fsetlocking is a nice optimization, but if it's unavailable, we can do
without.

Signed-off-by: Ulf Hermann 
---
 ChangeLog |  4 
 configure.ac  |  3 +++
 libgnu/ChangeLog  |  5 +
 libgnu/Makefile.am|  9 -
 libgnu/stdio_ext.in.h | 35 +++
 5 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/stdio_ext.in.h

diff --git a/ChangeLog b/ChangeLog
index 4024550..30b3884 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for stdio_ext.h.
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for features.h.
 
 2017-05-04  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index fc5edf8..2488bbc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -583,6 +583,9 @@ AM_CONDITIONAL(HAVE_AR_H, [test "x$ac_cv_header_ar_h" = 
"xyes"])
 AC_CHECK_HEADERS(features.h)
 AM_CONDITIONAL(HAVE_FEATURES_H, [test "x$ac_cv_header_features_h" = "xyes"])
 
+AC_CHECK_HEADERS(stdio_ext.h)
+AM_CONDITIONAL(HAVE_STDIO_EXT_H, [test "x$ac_cv_header_stdio_ext_h" = "xyes"])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index 7d70bfc..2cbc87f 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * Makefile.am: If stdio_ext.h is unavailable, use our own.
+   * stdio_ext.in.h: New file.
+
+2017-05-04  Ulf Hermann  
+
* Makefile.am: If features.h is unavailable, use our own.
* features.in.h: New file.
 
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index ed8aff1..ca1a078 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -36,7 +36,7 @@ MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
 EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h \
- features.in.h
+ features.in.h stdio_ext.in.h
 CLEANFILES =
 SUFFIXES =
 
@@ -77,6 +77,13 @@ MOSTLYCLEANFILES += sys/mman.h
 endif
 endif
 
+if !HAVE_STDIO_EXT_H
+stdio_ext.h: stdio_ext.in.h
+   $(AM_V_GEN)rm -f $@ && cat $< > $@
+BUILT_SOURCES += stdio_ext.h
+MOSTLYCLEANFILES += stdio_ext.h
+endif
+
 include gnulib.am
 
 if !HAVE_SYS_MMAN_H
diff --git a/libgnu/stdio_ext.in.h b/libgnu/stdio_ext.in.h
new file mode 100644
index 000..62565f4
--- /dev/null
+++ b/libgnu/stdio_ext.in.h
@@ -0,0 +1,35 @@
+/* This file defines __fsetlocking to noop
+   Copyright (C) 2017 The Qt Company Ltd
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifndef _STDIO_EXT_H
+#define _STDIO_EXT_H 1
+
+#define __fsetlocking(stream, type) (void)0
+
+#endif /* stdio_ext.h */
+
-- 
2.1.4



Re: [PATCH] Avoid double-including config.h

2017-05-04 Thread Ulf Hermann
On 05/02/2017 01:55 PM, Ulf Hermann wrote:
>> Maybe we can cleanup that last
>> one once we integrate gnulib (which I believe has a good/64-off_t fts.h
>> already).
> 
> gnulib is among the bad ones. Or, at least we detect it as bad.

Sorry, I meant the msys fts.h. That is also the one which needs sys/types.h to 
be included before. gnulib needs a different hack as it calls the header 
"fts_.h".

Ulf


[PATCH] Wrap gnulib fts_.h in fts.h

2017-05-04 Thread Ulf Hermann
For some reason gnulib calls the headers fts_.h. We need to wrap it into
an actual fts.h to make it available.

Signed-off-by: Ulf Hermann 
---
 ChangeLog  |  4 
 configure.ac   | 18 +++---
 libgnu/ChangeLog   |  5 +
 libgnu/Makefile.am |  9 -
 libgnu/fts.in.h| 34 ++
 5 files changed, 62 insertions(+), 8 deletions(-)
 create mode 100644 libgnu/fts.in.h

diff --git a/ChangeLog b/ChangeLog
index 30b3884..da4d288 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for fts.h before testing for BAD_FTS.
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for stdio_ext.h.
 
 2017-05-04  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index 2488bbc..f04a6c8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -302,13 +302,17 @@ dnl tests, because the choice of the file model can (in 
principle) affect
 dnl whether functions and headers are available, whether they work, etc.
 AC_SYS_LARGEFILE
 
-dnl Older glibc had a broken fts that didn't work with Large File Systems.
-dnl We want the version that can handler LFS, but include workaround if we
-dnl get a bad one. Add define to CFLAGS (not AC_DEFINE it) since we need to
-dnl check it before including config.h (which might define _FILE_OFFSET_BITS).
-AC_CACHE_CHECK([whether fts.h is bad when included (with LFS)], ac_cv_bad_fts,
-  [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#include ]])],
-ac_cv_bad_fts=no, ac_cv_bad_fts=yes)])
+AC_CHECK_HEADER(fts.h)
+AM_CONDITIONAL(HAVE_FTS_H, [test "x$ac_cv_header_fts_h" = "xyes"])
+if test "x$ac_cv_header_fts_h" = "xyes"; then
+  dnl Older glibc had a broken fts that didn't work with Large File Systems.
+  dnl We want the version that can handler LFS, but include workaround if we
+  dnl get a bad one. Add define to CFLAGS (not AC_DEFINE it) since we need to
+  dnl check it before including config.h (which might define 
_FILE_OFFSET_BITS).
+  AC_CACHE_CHECK([whether fts.h is bad when included (with LFS)], 
ac_cv_bad_fts,
+[AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#include ]])],
+   ac_cv_bad_fts=no, ac_cv_bad_fts=yes)])
+fi
 AS_IF([test "x$ac_cv_bad_fts" = "xyes"], [CFLAGS="$CFLAGS -DBAD_FTS=1"])
 
 # See if we can add -D_FORTIFY_SOURCE=2. Don't do it if it is already
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index 2cbc87f..d6ada7c 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * Makefile.am: If fts.h is unavailable, use our own.
+   * fts.in.h: New file.
+
+2017-05-04  Ulf Hermann  
+
* Makefile.am: If stdio_ext.h is unavailable, use our own.
* stdio_ext.in.h: New file.
 
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index ca1a078..21f9c5f 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -36,7 +36,7 @@ MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
 EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h \
- features.in.h stdio_ext.in.h
+ features.in.h stdio_ext.in.h fts.in.h
 CLEANFILES =
 SUFFIXES =
 
@@ -68,6 +68,13 @@ BUILT_SOURCES += features.h
 MOSTLYCLEANFILES += features.h
 endif
 
+if !HAVE_FTS_H
+fts.h: fts.in.h
+   $(AM_V_GEN)rm -f $@ && cat $< > $@
+BUILT_SOURCES += fts.h
+MOSTLYCLEANFILES += fts.h
+endif
+
 if !HAVE_SYS_MMAN_H
 if USE_WIN32_MMAN
 sys/mman.h: sys_mman.win32.h
diff --git a/libgnu/fts.in.h b/libgnu/fts.in.h
new file mode 100644
index 000..2fc57d1
--- /dev/null
+++ b/libgnu/fts.in.h
@@ -0,0 +1,34 @@
+/* Wrap fts_.h under real name
+   Copyright (C) 2017 The Qt Company Ltd
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifndef _FTS_REAL_H
+#define _FTS_REAL_H 1
+
+#include "fts_.h"
+
+#endif /* fts.h */
-- 
2.1.4



[PATCH] Define unlocked io functions to locked ones if they are unavailable

2017-05-04 Thread Ulf Hermann
The locked IO functions will still do the job, albeit a bit slower.

Signed-off-by: Ulf Hermann 
---
 ChangeLog   |  4 
 configure.ac|  4 
 lib/ChangeLog   |  5 +
 lib/eu-config.h | 28 
 4 files changed, 41 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index d43eeb6..29013e8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  
+
+   * configure.ac: Check for unlocked I/O functions.
+
 2017-04-28  Ulf Hermann  
 
* configure.ac: Determine the binary format we're building natively.
diff --git a/configure.ac b/configure.ac
index f04a6c8..0432bb1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -590,6 +590,10 @@ AM_CONDITIONAL(HAVE_FEATURES_H, [test 
"x$ac_cv_header_features_h" = "xyes"])
 AC_CHECK_HEADERS(stdio_ext.h)
 AM_CONDITIONAL(HAVE_STDIO_EXT_H, [test "x$ac_cv_header_stdio_ext_h" = "xyes"])
 
+AC_CHECK_DECLS([feof_unlocked, ferror_unlocked, fputc_unlocked, fputs_unlocked,
+   fwrite_unlocked, putc_unlocked, putchar_unlocked],
+   [], [], [[#include ]])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/lib/ChangeLog b/lib/ChangeLog
index ecc6179..321513c 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2017-05-04  Ulf Hermann  
+
+   * eu-config.h: Define unlocked I/O functions to locked ones if they
+   are unavailable.
+
 2017-04-27  Ulf Hermann  
 
* eu-config.h: Define attribute_hidden to be empty if the compiler
diff --git a/lib/eu-config.h b/lib/eu-config.h
index e69b213..6530044 100644
--- a/lib/eu-config.h
+++ b/lib/eu-config.h
@@ -206,4 +206,32 @@ asm (".section predict_data, \"aw\"; .previous\n"
 # define funlockfile(fp) /* nop */
 #endif
 
+#if !HAVE_DECL_FEOF_UNLOCKED
+#define feof_unlocked(x) feof (x)
+#endif
+
+#if !HAVE_DECL_FERROR_UNLOCKED
+#define ferror_unlocked(x) ferror (x)
+#endif
+
+#if !HAVE_DECL_FPUTC_UNLOCKED
+#define fputc_unlocked(x,y) fputc (x,y)
+#endif
+
+#if !HAVE_DECL_FPUTS_UNLOCKED
+#define fputs_unlocked(x,y) fputs (x,y)
+#endif
+
+#if !HAVE_DECL_FWRITE_UNLOCKED
+#define fwrite_unlocked(w,x,y,z) fwrite (w,x,y,z)
+#endif
+
+#if !HAVE_DECL_PUTC_UNLOCKED
+#define putc_unlocked(x,y) putc (x,y)
+#endif
+
+#if !HAVE_DECL_PUTCHAR_UNLOCKED
+#define putchar_unlocked(x) putchar (x)
+#endif
+
 #endif /* eu-config.h */
-- 
2.1.4



[PATCH] Check for existence of GNU-style basename()

2017-05-04 Thread Ulf Hermann
If it doesn't exist, add an implementation to libgnu.a and config.h.

Signed-off-by: Ulf Hermann 
---
 ChangeLog |  4 
 configure.ac  | 16 +++
 libgnu/ChangeLog  |  6 ++
 libgnu/Makefile.am|  6 +-
 libgnu/basename-gnu.c | 54 +++
 5 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/ChangeLog
 create mode 100644 libgnu/basename-gnu.c

diff --git a/ChangeLog b/ChangeLog
index 29013e8..aa0759c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,6 +36,10 @@
 
* .gitignore: Add fillfile and peel_type tests.
 
+2017-04-21  Ulf Hermann  
+
+   * configure.ac: Add check for GNU-style basename.
+
 2017-02-15  Ulf Hermann  
 
* configure.ac: Add check for mempcpy.
diff --git a/configure.ac b/configure.ac
index 0432bb1..bfdc53f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -440,6 +440,22 @@ AC_SUBST([zip_LIBS])
 
 AC_CHECK_DECLS([powerof2],[],[],[#include ])
 
+AC_CHECK_DECLS([basename],[],[],
+   [#define _GNU_SOURCE
+#include ])
+AM_CONDITIONAL(HAVE_BASENAME, [test "x$ac_cv_have_decl_basename" = "xyes"])
+
+if test "x$ac_cv_have_decl_basename" != "xyes"; then
+   AC_DEFINE([USE_REPLACEMENT_BASENAME], [1], [Use hand-rolled basename() 
replacement.])
+fi
+AH_VERBATIM([USE_REPLACEMENT_BASENAME],
+   [/* Define basename() here if it is not available from a system header. 
*/
+#undef USE_REPLACEMENT_BASENAME
+#ifdef USE_REPLACEMENT_BASENAME
+char *basename(const char *path);
+#endif
+])
+
 AC_CHECK_LIB([stdc++], [__cxa_demangle], [dnl
 AC_DEFINE([USE_DEMANGLE], [1], [Defined if demangling is enabled])])
 AM_CONDITIONAL(DEMANGLE, test "x$ac_cv_lib_stdcpp___cxa_demangle" = "xyes")
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
new file mode 100644
index 000..3394de6
--- /dev/null
+++ b/libgnu/ChangeLog
@@ -0,0 +1,6 @@
+2017-05-04  Ulf Hermann  
+
+   * Makefile.am: If GNU basename is unavailable add our own
+   implementation.
+   * basename-gnu.c: New file.
+
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 1c8e6b8..32c9aa7 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -36,7 +36,7 @@ MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
 EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h features.in.h \
- stdio_ext.in.h fts.in.h
+ stdio_ext.in.h fts.in.h basename-gnu.c
 CLEANFILES =
 SUFFIXES =
 
@@ -104,3 +104,7 @@ if USE_WIN32_SYSCONF
 libgnu_a_SOURCES += sysconf_win32.c
 endif
 endif
+
+if !HAVE_BASENAME
+libgnu_a_SOURCES += basename-gnu.c
+endif
diff --git a/libgnu/basename-gnu.c b/libgnu/basename-gnu.c
new file mode 100644
index 000..7feee81
--- /dev/null
+++ b/libgnu/basename-gnu.c
@@ -0,0 +1,54 @@
+/* Implementation of GNU-style basename()
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#include "dosname.h"
+#include 
+
+/* On windows, file names with ':' in them are invalid, so we don't have to
+   add a special case for them. If we get an invalid path as input, we may
+   return a nonsensical path as output. This assumption allows us to use the
+   simple strrpos() equivalent below, without any allocation. */
+
+char *
+basename (const char *name)
+{
+  size_t prefix = FILE_SYSTEM_PREFIX_LEN(name);
+  size_t length = strlen(name);
+
+  while (length > prefix) {
+--length;
+if (ISSLASH(name[length]))
+  return (char *)name + length + 1;
+  }
+
+  return (char *)name + prefix;
+}
-- 
2.1.4



[PATCH] Add our own tdestroy if search.h exposes a node_t struct

2017-05-04 Thread Ulf Hermann
tdestroy is not necessarily available from search.h, but we need it.
gnulib cannot help us here as it will detect search.h to be available
and functional in that case. However, some search.h expose a node_t
struct which can be used to implement tdestroy. If that is the case, add
an implementation to libgnu.a.

Signed-off-by: Ulf Hermann 
---
 ChangeLog  |  5 +
 configure.ac   | 21 +
 libgnu/ChangeLog   |  5 +
 libgnu/Makefile.am |  6 +-
 libgnu/tdestroy.c  | 47 +++
 5 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/tdestroy.c

diff --git a/ChangeLog b/ChangeLog
index aa0759c..5a86247 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for tdestroy and node_t. Declare tdestroy in
+   config.h if tdestroy is not available from search.h, but node_t is.
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for unlocked I/O functions.
 
 2017-04-28  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index bfdc53f..88b9d0a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -610,6 +610,27 @@ AC_CHECK_DECLS([feof_unlocked, ferror_unlocked, 
fputc_unlocked, fputs_unlocked,
fwrite_unlocked, putc_unlocked, putchar_unlocked],
[], [], [[#include ]])
 
+AC_CHECK_DECLS([tdestroy], [], [],
+  [#include ])
+if test "x$ac_cv_have_decl_tdestroy" != "xyes"; then
+   AC_CHECK_MEMBERS([node_t.key, node_t.rlink, node_t.llink],
+[have_node_t="yes"], [have_node_t="no"],
+[#define _SEARCH_PRIVATE
+ #include ])
+   if test "x$have_node_t" = "xyes"; then
+   AC_DEFINE([USE_PRIVATE_TDESTROY], [1], [Implement tdestroy 
using private node_t from search.h])
+   fi
+fi
+AM_CONDITIONAL(USE_PRIVATE_TDESTROY, [test "x$have_node_t" = "xyes"])
+
+AH_VERBATIM([USE_PRIVATE_TDESTROY], [
+/* Declare tdestroy here if it is not available from a system header. */
+#undef USE_PRIVATE_TDESTROY
+#ifdef USE_PRIVATE_TDESTROY
+void tdestroy(void *root, void (*free_node)(void *nodep));
+#endif
+])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
index 3394de6..7b146b6 100644
--- a/libgnu/ChangeLog
+++ b/libgnu/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * Makefile.am: Use our own implementation of tdestroy if we have to.
+   * tdestroy.c: New file.
+
+2017-05-04  Ulf Hermann  
+
* Makefile.am: If GNU basename is unavailable add our own
implementation.
* basename-gnu.c: New file.
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 32c9aa7..99abab0 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -36,7 +36,7 @@ MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
 EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h features.in.h \
- stdio_ext.in.h fts.in.h basename-gnu.c
+ stdio_ext.in.h fts.in.h basename-gnu.c tdestroy.c
 CLEANFILES =
 SUFFIXES =
 
@@ -108,3 +108,7 @@ endif
 if !HAVE_BASENAME
 libgnu_a_SOURCES += basename-gnu.c
 endif
+
+if USE_PRIVATE_TDESTROY
+libgnu_a_SOURCES += tdestroy.c
+endif
diff --git a/libgnu/tdestroy.c b/libgnu/tdestroy.c
new file mode 100644
index 000..d14b875
--- /dev/null
+++ b/libgnu/tdestroy.c
@@ -0,0 +1,47 @@
+/* tdestroy, on systems where node is exposed from search.h
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#include 
+
+#include 
+#define _SEARCH_PRIVATE
+#include 
+
+void
+tdestroy (void *vroot, void (*free_node)(void *nodep))
+{
+  if (vroot == NULL)
+return;
+
+  node_t *root = (node_t *) vroot;
+  tdestroy (root->llink, free_node);
+  tdestroy (root->rlink, free_node);
+  free_node ((void *) root->key);
+  free (root);
+}
+
-- 
2.

[PATCH] Define roundup() for strings.c if it doesn't exist

2017-05-04 Thread Ulf Hermann
Change-Id: I6ea7c1f894e89bbaaecb724473c4c00e67296f05
Signed-off-by: Ulf Hermann 
---
 src/ChangeLog | 4 
 src/strings.c | 3 +++
 2 files changed, 7 insertions(+)

diff --git a/src/ChangeLog b/src/ChangeLog
index 64db1ca..4a32604 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  
+
+   * strings.c: If roundup() is not defined, define it.
+
 2017-04-28  Ulf Hermann  
 
* Makefile.am: Use the predefined names for libelf, libdw, libasm,
diff --git a/src/strings.c b/src/strings.c
index d214356..22cbfac 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -450,6 +450,9 @@ process_chunk (const char *fname, const unsigned char *buf, 
off_t to,
 *unprinted = xstrndup ((const char *) start, curlen);
 }
 
+#ifndef roundup
+#define roundup(x, y) x) + ((y) - 1)) / (y)) * (y))
+#endif
 
 /* Map a file in as large chunks as possible.  */
 static void *
-- 
2.1.4



[PATCH] Skip fchown, fchmod, fadvise, fallocate if functions are unavailable

2017-05-04 Thread Ulf Hermann
If fchmod or fchown are unavailable, then the file permission model is
likely to be different from what we expect there. posix_fallocate is a
frather fragile affair already on linux, and not guaranteed to do
anything useful. If it's not available, the result will be the same as
when it's available and unreliable. fadvise is an optimization.

Signed-off-by: Ulf Hermann 
---
 ChangeLog   |  5 +
 configure.ac|  3 +++
 libasm/ChangeLog|  4 
 libasm/asm_end.c|  2 ++
 libelf/ChangeLog|  5 +
 libelf/elf_update.c |  4 
 src/ChangeLog   |  9 
 src/ar.c| 59 +++--
 src/elfcompress.c   |  4 
 src/ranlib.c| 21 +++
 src/strings.c   |  2 ++
 src/strip.c |  5 -
 tests/ChangeLog |  5 +
 tests/elfstrmerge.c |  4 
 14 files changed, 103 insertions(+), 29 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5a86247..7a40c9d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for posix_fallocate, posix_fadvise, fchown,
+   fchmod.
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for tdestroy and node_t. Declare tdestroy in
config.h if tdestroy is not available from search.h, but node_t is.
 
diff --git a/configure.ac b/configure.ac
index 88b9d0a..e55360d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -631,6 +631,9 @@ void tdestroy(void *root, void (*free_node)(void *nodep));
 #endif
 ])
 
+AC_CHECK_DECLS([posix_fallocate, posix_fadvise], [], [], [[#include 
]])
+AC_CHECK_DECLS([fchown, fchmod], [], [], [[#include ]])
+
 dnl Check if we have  for EM_BPF disassembly.
 AC_CHECK_HEADERS(linux/bpf.h)
 AM_CONDITIONAL(HAVE_LINUX_BPF_H, [test "x$ac_cv_header_linux_bpf_h" = "xyes"])
diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index 5321213..2b499c7 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  
+
+   * asm_end.c: Don't fchmod the new file if fchmod is unavailable.
+
 2017-02-28  Ulf Hermann  
 
* Makefile.am: Use the predefined common library names rather than
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
index ced24f5..7fabe94 100644
--- a/libasm/asm_end.c
+++ b/libasm/asm_end.c
@@ -512,12 +512,14 @@ asm_end (AsmCtx_t *ctx)
   if (result != 0)
 return result;
 
+#if HAVE_DECL_FCHMOD
   /* Make the new file globally readable and user/group-writable.  */
   if (fchmod (ctx->fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) != 0)
 {
   __libasm_seterrno (ASM_E_CANNOT_CHMOD);
   return -1;
 }
+#endif
 
   /* Rename output file.  */
   if (rename (ctx->tmp_fname, ctx->fname) != 0)
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 0a5200f..fd20ebb 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,8 @@
+2017-05-04  Ulf Hermann  
+
+   * 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.
+
 2017-02-28  Ulf Hermann  
 
* Makefile.am: Use the predefined common library names rather than
diff --git a/libelf/elf_update.c b/libelf/elf_update.c
index 8ce0782..b6014cd 100644
--- a/libelf/elf_update.c
+++ b/libelf/elf_update.c
@@ -80,6 +80,7 @@ write_file (Elf *elf, off_t size, int change_bo, size_t shnum)
 
   if (elf->map_address != NULL)
 {
+#if HAVE_DECL_POSIX_FALLOCATE
   /* When using mmap we want to make sure the file content is
 really there. Only using ftruncate might mean the file is
 extended, but space isn't allocated yet.  This might cause a
@@ -100,6 +101,7 @@ write_file (Elf *elf, off_t size, int change_bo, size_t 
shnum)
__libelf_seterrno (ELF_E_WRITE_ERROR);
return -1;
  }
+#endif
 
   /* The file is mmaped.  */
   if ((class == ELFCLASS32
@@ -129,6 +131,7 @@ write_file (Elf *elf, off_t size, int change_bo, size_t 
shnum)
   size = -1;
 }
 
+#if HAVE_DECL_FCHMOD
   /* POSIX says that ftruncate and write may clear the S_ISUID and S_ISGID
  mode bits.  So make sure we restore them afterwards if they were set.
  This is not atomic if someone else chmod's the file while we operate.  */
@@ -140,6 +143,7 @@ write_file (Elf *elf, off_t size, int change_bo, size_t 
shnum)
   __libelf_seterrno (ELF_E_WRITE_ERROR);
   size = -1;
 }
+#endif
 
   if (size != -1 && elf->parent == NULL)
 elf->maximum_size = size;
diff --git a/src/ChangeLog b/src/ChangeLog
index 4a32604..e0df2e1 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,14 @@
 2017-05-04  Ulf Hermann  
 
+   * ar.c: Don't fchmod or fchown the output file if fchmod or fchown
+   don't exist.
+   * elfcompress.c: Likewise.
+   * ranlib.c: Likewise.
+   * strip.c: Likewise.
+   * strings.c: Skip posix_fadvise if it doesn't exist.
+
+2017-05-04  Ulf Hermann  
+
* strings.c: If roundup() is not def

[PATCH v2] Check for existence of GNU-style strerror_r

2017-05-04 Thread Ulf Hermann
We cannot get GNU strerror_r from gnulib.

If we don't have it, we don't translate system error codes to strings in
dwfl_error.c.

(rebased on top of all the other patches)

Signed-off-by: Ulf Hermann 
---
 ChangeLog| 4 
 configure.ac | 2 ++
 libdwfl/ChangeLog| 5 +
 libdwfl/dwfl_error.c | 4 
 4 files changed, 15 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 7a40c9d..662c633 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -48,6 +48,10 @@
 
 2017-04-21  Ulf Hermann  
 
+   * configure.ac: Check for strerror_r and its variants.
+
+2017-04-21  Ulf Hermann  
+
* configure.ac: Add check for GNU-style basename.
 
 2017-02-15  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index e55360d..6cbd70d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -456,6 +456,8 @@ char *basename(const char *path);
 #endif
 ])
 
+AC_FUNC_STRERROR_R
+
 AC_CHECK_LIB([stdc++], [__cxa_demangle], [dnl
 AC_DEFINE([USE_DEMANGLE], [1], [Defined if demangling is enabled])])
 AM_CONDITIONAL(DEMANGLE, test "x$ac_cv_lib_stdcpp___cxa_demangle" = "xyes")
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index a7e6e30..26a3599 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -100,6 +100,11 @@
the note name data is the empty string.
(dwfl_core_file_attach): Likewise.
 
+2017-02-22  Ulf Hermann  
+
+   * dwfl_error.c: If we don't have a strerror_r returning a char*,
+   output a less useful message in case of a system error.
+
 2017-02-15  Ulf Hermann  
 
* linux-kernel-modules.c: Include system.h.
diff --git a/libdwfl/dwfl_error.c b/libdwfl/dwfl_error.c
index 7bcf61c..aba3cca 100644
--- a/libdwfl/dwfl_error.c
+++ b/libdwfl/dwfl_error.c
@@ -154,7 +154,11 @@ dwfl_errmsg (int error)
   switch (error &~ 0x)
 {
 case OTHER_ERROR (ERRNO):
+#ifdef STRERROR_R_CHAR_P
   return strerror_r (error & 0x, "bad", 0);
+#else
+  return "Unknown error. See errno";
+#endif
 case OTHER_ERROR (LIBELF):
   return elf_errmsg (error & 0x);
 case OTHER_ERROR (LIBDW):
-- 
2.1.4



[PATCH] Avoid using err() and errx() in tests

2017-05-04 Thread Ulf Hermann
fprintf() and exit() do the same job and are portable.

Signed-off-by: Ulf Hermann 
---
 tests/ChangeLog  |  6 ++
 tests/allfcts.c  | 46 +++---
 tests/buildid.c  |  6 +++---
 tests/debugaltlink.c |  6 +++---
 4 files changed, 43 insertions(+), 21 deletions(-)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index 6dab801..c2619d0 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,11 @@
 2017-05-04  Ulf Hermann  
 
+   * allfcts.c: Use fprintf and exit rather than err and errx.
+   * buildid.c: Likewise.
+   * debugaltlink.c: Likewise.
+
+2017-05-04  Ulf Hermann  
+
* elfstrmerge.c: Don't fchmod or fchown the output file if fchmod or
fchown don't exist.
 
diff --git a/tests/allfcts.c b/tests/allfcts.c
index d3c8d26..ad93dce 100644
--- a/tests/allfcts.c
+++ b/tests/allfcts.c
@@ -18,12 +18,13 @@
 # include 
 #endif
 
-#include 
 #include 
 #include ELFUTILS_HEADER(dw)
 #include ELFUTILS_HEADER(dwelf)
 #include 
 #include 
+#include 
+#include 
 
 
 static int
@@ -47,16 +48,24 @@ setup_alt (Dwarf *main)
   ssize_t ret = dwelf_dwarf_gnu_debugaltlink (main, &alt_name, &build_id);
   if (ret == 0)
 return NULL;
-  if (ret == -1)
-errx (1, "dwelf_dwarf_gnu_debugaltlink: %s", dwarf_errmsg (-1));
+  if (ret == -1) {
+fprintf (stderr, "allfcts: dwelf_dwarf_gnu_debugaltlink: %s\n", 
dwarf_errmsg (-1));
+exit(1);
+  }
   int fd = open (alt_name, O_RDONLY);
-  if (fd < 0)
-err (1, "open (%s)", alt_name);
+  if (fd < 0) {
+fprintf (stderr, "allfcts: open (%s): %s\n", alt_name, strerror(errno));
+exit(1);
+  }
   Dwarf *dbg_alt = dwarf_begin (fd, DWARF_C_READ);
-  if (dbg_alt == NULL)
-errx (1, "dwarf_begin (%s): %s", alt_name, dwarf_errmsg (-1));
-  if (elf_cntl (dwarf_getelf (dbg_alt), ELF_C_FDREAD) != 0)
-errx (1, "elf_cntl (%s, ELF_C_FDREAD): %s", alt_name, elf_errmsg (-1));
+  if (dbg_alt == NULL) {
+fprintf (stderr, "dwarf_begin (%s): %s\n", alt_name, dwarf_errmsg (-1));
+exit(1);
+  }
+  if (elf_cntl (dwarf_getelf (dbg_alt), ELF_C_FDREAD) != 0) {
+fprintf (stderr, "elf_cntl (%s, ELF_C_FDREAD): %s\n", alt_name, elf_errmsg 
(-1));
+exit(1);
+  }
   close (fd);
   dwarf_setalt (main, dbg_alt);
   return dbg_alt;
@@ -68,8 +77,10 @@ main (int argc, char *argv[])
   for (int i = 1; i < argc; ++i)
 {
   int fd = open (argv[i], O_RDONLY);
-  if (fd < 0)
-   err (1, "open (%s)", argv[i]);
+  if (fd < 0) {
+   fprintf (stderr, "open (%s): %s\n", argv[i], strerror(errno));
+   exit(1);
+  }
 
   Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
   if (dbg != NULL)
@@ -89,9 +100,11 @@ main (int argc, char *argv[])
  do
{
  doff = dwarf_getfuncs (die, cb, NULL, doff);
- if (dwarf_errno () != 0)
-   errx (1, "dwarf_getfuncs (%s): %s",
- argv[i], dwarf_errmsg (-1));
+ if (dwarf_errno () != 0) {
+   fprintf (stderr, "dwarf_getfuncs (%s): %s\n",
+argv[i], dwarf_errmsg (-1));
+   exit(1);
+ }
}
  while (doff != 0);
 
@@ -102,7 +115,10 @@ main (int argc, char *argv[])
  dwarf_end (dbg);
}
   else
-   errx (1, "dwarf_begin (%s): %s", argv[i], dwarf_errmsg (-1));
+{
+ fprintf (stderr, "dwarf_begin (%s): %s\n", argv[i], dwarf_errmsg 
(-1));
+ exit(1);
+   }
 
   close (fd);
 }
diff --git a/tests/buildid.c b/tests/buildid.c
index 87c1877..2d33402 100644
--- a/tests/buildid.c
+++ b/tests/buildid.c
@@ -18,7 +18,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include ELFUTILS_HEADER(elf)
 #include ELFUTILS_HEADER(dwelf)
@@ -62,8 +61,9 @@ main (int argc, char *argv[])
  printf ("%s: \n", file);
  break;
case -1:
- errx (1, "dwelf_elf_gnu_build_id (%s): %s",
-   file, elf_errmsg (-1));
+ fprintf (stderr, "dwelf_elf_gnu_build_id (%s): %s\n",
+  file, elf_errmsg (-1));
+ return 1;
default:
  printf ("%s: build ID: ", file);
  const unsigned char *p = build_id;
diff --git a/tests/debugaltlink.c b/tests/debugaltlink.c
index 6d97d50..b470e31 100644
--- a/tests/debugaltlink.c
+++ b/tests/debugaltlink.c
@@ -18,7 +18,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include ELFUTILS_HEADER(dw)
 #include ELFUTILS_HEADER(dwelf)
@@ -64,8 +63,9 @@ main (int argc, char *argv[])
  printf ("%s: \n", file);
  break;
case -1:
- errx (1, "dwelf_dwarf_gnu_debugaltlink (%s): %s",
-   file, dwarf_errmsg (-1));
+ fprintf (stderr, "dwelf_dwarf_gnu_debugaltlink (%s): %s\n",
+  file, dwarf_errmsg (-1));
+ return 1;
default:
  printf ("%s: %s, build ID: ", file, name);
  const unsigned char *p 

[PATCH] Skip deleted test if fork(2) is unavailable

2017-05-04 Thread Ulf Hermann
Signed-off-by: Ulf Hermann 
---
 ChangeLog   |  4 
 configure.ac|  1 +
 tests/ChangeLog |  4 
 tests/deleted.c | 14 ++
 4 files changed, 23 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 662c633..2cf7bd6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Check for fork().
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for posix_fallocate, posix_fadvise, fchown,
fchmod.
 
diff --git a/configure.ac b/configure.ac
index 6cbd70d..5fa20c0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -608,6 +608,7 @@ AM_CONDITIONAL(HAVE_FEATURES_H, [test 
"x$ac_cv_header_features_h" = "xyes"])
 AC_CHECK_HEADERS(stdio_ext.h)
 AM_CONDITIONAL(HAVE_STDIO_EXT_H, [test "x$ac_cv_header_stdio_ext_h" = "xyes"])
 
+AC_CHECK_DECLS([fork], [], [], [[#include ]])
 AC_CHECK_DECLS([feof_unlocked, ferror_unlocked, fputc_unlocked, fputs_unlocked,
fwrite_unlocked, putc_unlocked, putchar_unlocked],
[], [], [[#include ]])
diff --git a/tests/ChangeLog b/tests/ChangeLog
index c2619d0..611b88a 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * deleted.c: If fork() is unavailable, skip the test.
+
+2017-05-04  Ulf Hermann  
+
* allfcts.c: Use fprintf and exit rather than err and errx.
* buildid.c: Likewise.
* debugaltlink.c: Likewise.
diff --git a/tests/deleted.c b/tests/deleted.c
index 6be35bc..f11cb1b 100644
--- a/tests/deleted.c
+++ b/tests/deleted.c
@@ -29,6 +29,18 @@
 
 extern void libfunc (void);
 
+#if !HAVE_DECL_FORK
+
+int
+main (int argc __attribute__ ((unused)), char **argv)
+{
+  fprintf (stderr, "%s: fork() not supported for this architecture\n",
+  argv[0]);
+  return 77;
+}
+
+#else
+
 int
 main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
 {
@@ -56,3 +68,5 @@ main (int argc __attribute__ ((unused)), char **argv 
__attribute__ ((unused)))
   printf ("%d\n", pid);
   return EXIT_SUCCESS;
 }
+
+#endif
-- 
2.1.4



[PATCH] Open files in O_BINARY

2017-05-04 Thread Ulf Hermann
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 
---
 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  
 
+   * eu-config.h: Define O_BINARY to 0 if it doesn't exist.
+
+2017-05-04  Ulf Hermann  
+
* 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_BI

[PATCH] Have diff ignore line ending differences when testing

2017-05-04 Thread Ulf Hermann
This accounts for the CR/LF problem we get when producing text files on
windows.

Signed-off-by: Ulf Hermann 
---
 tests/ChangeLog | 7 +++
 tests/run-addr2line-test.sh | 8 
 tests/run-readelf-test1.sh  | 2 +-
 tests/run-unstrip-n.sh  | 2 +-
 tests/test-subr.sh  | 2 +-
 5 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index b091ae0..5616c74 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,12 @@
 2017-05-04  Ulf Hermann  
 
+   * run-addr2line-test.sh: Add --strip-trailing-cr option to diff.
+   * run-readelf-test1.sh: Likewise.
+   * run-unstrip-n.sh: Likewise.
+   * test-subr.sh: Likewise.
+
+2017-05-04  Ulf Hermann  
+
* alldts.c: Open files in O_BINARY.
* allfcts.c: Likewise.
* arextract.c: Likewise.
diff --git a/tests/run-addr2line-test.sh b/tests/run-addr2line-test.sh
index 1079c3e..0dae42d 100755
--- a/tests/run-addr2line-test.sh
+++ b/tests/run-addr2line-test.sh
@@ -56,7 +56,7 @@ EOF
 
 echo "# Everything from stdin (with newlines)."
 cat stdin.nl | testrun ${abs_top_builddir}/src/addr2line -f -e testfile > 
stdin.nl.out || exit 1
-cmp good.out stdin.nl.out || exit 1
+diff --strip-trailing-cr good.out stdin.nl.out || exit 1
 
 cat > foo.out <<\EOF
 foo
@@ -65,11 +65,11 @@ EOF
 
 echo "# stdin without newline address, just EOF."
 echo -n "0x08048468" | testrun ${abs_top_builddir}/src/addr2line -f -e 
testfile > stdin.nonl.out || exit 1
-cmp foo.out stdin.nonl.out || exit 1
+diff --strip-trailing-cr foo.out stdin.nonl.out || exit 1
 
 echo "# stdin without newline symbol, just EOF."
 echo -n "foo" | testrun ${abs_top_builddir}/src/addr2line -f -e testfile > 
stdin.nl.out || exit 1
-cmp foo.out stdin.nonl.out || exit 1
+diff --strip-trailing-cr foo.out stdin.nonl.out || exit 1
 
 tempfiles good.addr.out
 
@@ -105,7 +105,7 @@ cat good.addr.out | testrun_compare 
${abs_top_builddir}/src/addr2line -a -f -e t
 
 echo "# Everything from stdin (with newlines) with addresses."
 cat stdin.nl | testrun ${abs_top_builddir}/src/addr2line -a -f -e testfile > 
stdin.nl.out || exit 1
-cmp good.addr.out stdin.nl.out || exit 1
+diff --strip-trailing-cr good.addr.out stdin.nl.out || exit 1
 
 echo "# Pretty with functions and addresses."
 testrun_compare ${abs_top_builddir}/src/addr2line --pretty -a -f -e testfile 
0x08048468 0x0804845c << EOF
diff --git a/tests/run-readelf-test1.sh b/tests/run-readelf-test1.sh
index 4725049..40b664f 100755
--- a/tests/run-readelf-test1.sh
+++ b/tests/run-readelf-test1.sh
@@ -28,7 +28,7 @@ tempfiles testfile.temp
 
 testrun ${abs_top_builddir}/src/readelf -r testfile3 > testfile.temp
 
-diff -u - testfile.temp <$outfile2
-diff -u $outfile2 - <

[PATCH] Use OS-specific paths

2017-05-04 Thread Ulf Hermann
In general we need to use ';' as path separator and '\' and directory
separator on windows. The shell will automatically translate paths to
some extent, but we have to call "pwd -W" rather than plain "pwd" to
get something useful.

Signed-off-by: Ulf Hermann 
---
 lib/ChangeLog|   5 ++
 lib/system.h |  16 ++
 libdwfl/ChangeLog|   8 +++
 libdwfl/dwfl_build_id_find_elf.c |   5 +-
 libdwfl/find-debuginfo.c | 106 ++-
 libdwfl/libdwflP.h   |   4 ++
 libdwfl/linux-kernel-modules.c   |   3 +-
 libdwfl/linux-proc-maps.c|   4 +-
 src/ChangeLog|   6 ++
 src/addr2line.c  |   4 +-
 src/size.c   |   4 +-
 src/strip.c  |   4 +-
 tests/ChangeLog  |  10 
 tests/asm-tst4.c |   7 ++-
 tests/asm-tst5.c |   6 +-
 tests/asm-tst6.c |   6 +-
 tests/run-addr2line-alt-debugpath.sh |   4 +-
 tests/run-addrname-test.sh   |   8 +--
 tests/run-prelink-addr-test.sh   |  60 ++--
 tests/test-subr.sh   |   1 +
 20 files changed, 171 insertions(+), 100 deletions(-)

diff --git a/lib/ChangeLog b/lib/ChangeLog
index 9fa8ff0..59939bd 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * system.h: Define FILE_SYSTEM_PREFIX_LEN, ISDIRSEP, DIRSEP, PATHSEP,
+   and IS_ABSOLUTE_PATH to help with handling file system paths.
+
+2017-05-04  Ulf Hermann  
+
* eu-config.h: Define O_BINARY to 0 if it doesn't exist.
 
 2017-05-04  Ulf Hermann  
diff --git a/lib/system.h b/lib/system.h
index ffa2bc7..3a6b8e9 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -51,6 +51,22 @@
 # error "Unknown byte order"
 #endif
 
+#if (defined _WIN32 || defined __WIN32__)
+# define _IS_DRIVE_LETTER(c) (((unsigned int) (c) | ('a' - 'A')) - 'a' <= 'z' 
- 'a')
+# define FILE_SYSTEM_PREFIX_LEN(filename) \
+  (_IS_DRIVE_LETTER ((filename)[0]) && (filename)[1] == ':' ? 2 : 0)
+# define ISDIRSEP(c) ((c) == '/' || (c) == '\\')
+# define DIRSEP '\\'
+# define PATHSEP ';'
+# define IS_ABSOLUTE_PATH(f) ISDIRSEP ((f)[FILE_SYSTEM_PREFIX_LEN (f)])
+#else
+# define FILE_SYSTEM_PREFIX_LEN(filename) 0
+# define ISDIRSEP(c) ((c) == '/')
+# define DIRSEP '/'
+# define PATHSEP ':'
+# define IS_ABSOLUTE_PATH(p) (ISDIRSEP ((p)[0]))
+#endif
+
 #ifndef MAX
 #define MAX(m, n) ((m) < (n) ? (n) : (m))
 #endif
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 6f3a561..ee550d0 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,5 +1,13 @@
 2017-05-04  Ulf Hermann  
 
+   * dwfl_build_id_find_elf.c: Don't assume unix file system conventions.
+   * find-debuginfo.c: Likewise.
+   * linux-kernel-modules.c: Likewise.
+   * linux-proc-maps.c: Likewise.
+   * libdwflP.h: Add a windows-compatible default debuginfo path.
+
+2017-05-04  Ulf Hermann  
+
* argp-std.c: Open files in O_BINARY.
* dwfl_build_id_find_elf.c: Likewise.
* dwfl_build_id_find_elf.c: Likewise.
diff --git a/libdwfl/dwfl_build_id_find_elf.c b/libdwfl/dwfl_build_id_find_elf.c
index 6ce2c1c..cf2a0f1 100644
--- a/libdwfl/dwfl_build_id_find_elf.c
+++ b/libdwfl/dwfl_build_id_find_elf.c
@@ -79,13 +79,14 @@ __libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug, 
char **file_name,
   int fd = -1;
   char *dir;
   char *paths = path;
-  while (fd < 0 && (dir = strsep (&paths, ":")) != NULL)
+  char pathsep[] = { PATHSEP, '\0' };
+  while (fd < 0 && (dir = strsep (&paths, pathsep)) != NULL)
 {
   if (dir[0] == '+' || dir[0] == '-')
++dir;
 
   /* Only absolute directory names are useful to us.  */
-  if (dir[0] != '/')
+  if (IS_ABSOLUTE_PATH(dir))
continue;
 
   size_t dirlen = strlen (dir);
diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c
index 7f7e108..ac568d0 100644
--- a/libdwfl/find-debuginfo.c
+++ b/libdwfl/find-debuginfo.c
@@ -52,9 +52,10 @@ try_open (const struct stat *main_stat,
   if (unlikely (fname == NULL))
return -1;
 }
-  else if ((subdir == NULL ? asprintf (&fname, "%s/%s", dir, debuglink)
-   : dir == NULL ? asprintf (&fname, "%s/%s", subdir, debuglink)
-   : asprintf (&fname, "%s/%s/%s", dir, subdir, debuglink)) < 0)
+  else if ((subdir == NULL ? asprintf (&fname, "%s%c%s", dir, DIRSEP, 
debuglink)
+   : dir == NULL ? asprintf (&fname, "%s%c%s", subdir, DIRSEP, 
debuglink)
+   : asprintf (&fname, "%s%c%s%c%s", dir, DIRSEP, subdir, DIRSEP,
+debuglink)) < 0)
 return -1;
 
   struct stat st;
@@ -231,7 +232,8 @@ find_debuginfo_in_path (Dwfl_Module *mod, const char 
*file_name,
   return -1;
 }
   char *p;
-  while ((p = strsep (&path, ":")) != NULL)
+  const char pathsep[] = { PATHSEP, '\0' };
+  while ((p = st

[PATCH] Adapt debug info fstat check for windows

2017-05-04 Thread Ulf Hermann
On windows the resulting ino and dev entries are always 0, so the file
would always be discarded. We apply a heuristic instead: If the ctime,
mtime, mode and size of the two files are all equal then we consider
them to be the same. It's exceedingly unlikely to produce two different
files for which that holds by chance.

Signed-off-by: Ulf Hermann 
---
 libdwfl/ChangeLog| 5 +
 libdwfl/find-debuginfo.c | 9 +
 2 files changed, 14 insertions(+)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index ee550d0..517ba21 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * find-debuginfo.c: On windows, check various extra parameters of
+   struct stat to determine if two files are the same.
+
+2017-05-04  Ulf Hermann  
+
* dwfl_build_id_find_elf.c: Don't assume unix file system conventions.
* find-debuginfo.c: Likewise.
* linux-kernel-modules.c: Likewise.
diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c
index ac568d0..3a65eda 100644
--- a/libdwfl/find-debuginfo.c
+++ b/libdwfl/find-debuginfo.c
@@ -63,10 +63,19 @@ try_open (const struct stat *main_stat,
   if (fd < 0)
 free (fname);
   else if (fstat (fd, &st) == 0
+#if defined _WIN32 || defined __WIN32__
+  /* On windows, st_ino and st_dev are not unique, so we apply a 
heuristic */
+  && st.st_size == main_stat->st_size
+  && st.st_mode == main_stat->st_mode
+  && st.st_mtime == main_stat->st_mtime
+  && st.st_ctime == main_stat->st_ctime
+#endif
   && st.st_ino == main_stat->st_ino
   && st.st_dev == main_stat->st_dev)
 {
   /* This is the main file by another name.  Don't look at it again.  */
+  /* This doesn't happen on windows as windows doesn't have proper links. 
However, on windows
+ st_ino and st_dev is always 0. */
   free (fname);
   close (fd);
   errno = ENOENT;
-- 
2.1.4



[PATCH] Close files before renaming or unlinking them

2017-05-04 Thread Ulf Hermann
On windows we cannot rename or unlink open files.

Signed-off-by: Ulf Hermann 
---
 libasm/ChangeLog |  4 
 libasm/asm_end.c | 15 +++
 tests/ChangeLog  |  9 +
 tests/newfile.c  |  7 +--
 tests/newscn.c   |  3 ++-
 tests/update1.c  |  1 +
 tests/update2.c  |  1 +
 tests/update3.c  |  1 +
 tests/update4.c  |  1 +
 9 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index 2b499c7..0e67657 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * asm_end.c: Rename the output file only after freeing resources.
+
+2017-05-04  Ulf Hermann  
+
* asm_end.c: Don't fchmod the new file if fchmod is unavailable.
 
 2017-02-28  Ulf Hermann  
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
index 7fabe94..7891fbb 100644
--- a/libasm/asm_end.c
+++ b/libasm/asm_end.c
@@ -521,16 +521,23 @@ asm_end (AsmCtx_t *ctx)
 }
 #endif
 
+  char *tmp_fname = strdup (ctx->tmp_fname);
+  char *fname = strdup (ctx->fname);
+
+  /* Free the resources.  */
+  __libasm_finictx (ctx);
+
   /* Rename output file.  */
-  if (rename (ctx->tmp_fname, ctx->fname) != 0)
+  result = rename (tmp_fname, fname);
+  free (tmp_fname);
+  free (fname);
+
+  if (result != 0)
 {
   __libasm_seterrno (ASM_E_CANNOT_RENAME);
   return -1;
 }
 
-  /* Free the resources.  */
-  __libasm_finictx (ctx);
-
   return 0;
 }
 
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 5e29f82..b8de138 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,14 @@
 2017-05-04  Ulf Hermann  
 
+   * newfile.c: Close the file when we're done and unlink it afterwards.
+   * newscn.c: Likewise.
+   * update1.c: Likewise.
+   * update2.c: Likewise.
+   * update3.c: Likewise.
+   * update4.c: Likewise.
+
+2017-05-04  Ulf Hermann  
+
* asm-tst4.c: Don't assume unix file system conventions.
* asm-tst5.c: Likewise.
* asm-tst6.c: Likewise.
diff --git a/tests/newfile.c b/tests/newfile.c
index 5eabdcb..a279317 100644
--- a/tests/newfile.c
+++ b/tests/newfile.c
@@ -63,8 +63,6 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   printf ("cannot create temporary file: %m\n");
   exit (1);
 }
-  /* Remove the file when we exit.  */
-  unlink (fname);
 
   elf_version (EV_CURRENT);
   elf = elf_begin (fd, ELF_C_WRITE, NULL);
@@ -166,5 +164,10 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   (void) elf_end (elf);
 }
 
+  close (fd);
+
+  /* Remove the file when we exit.  */
+  unlink (fname);
+
   return result;
 }
diff --git a/tests/newscn.c b/tests/newscn.c
index 466f2f6..de8951d 100644
--- a/tests/newscn.c
+++ b/tests/newscn.c
@@ -46,7 +46,6 @@ main (void)
   fprintf (stderr, "Failed to open fdput file: %s\n", name);
   exit (1);
 }
-  unlink (name);
 
   elf = elf_begin (fd, ELF_C_WRITE, NULL);
   if (elf == NULL)
@@ -62,5 +61,7 @@ main (void)
   elf_end (elf);
   close (fd);
 
+  unlink (name);
+
   return 0;
 }
diff --git a/tests/update1.c b/tests/update1.c
index a571618..548c6d8 100644
--- a/tests/update1.c
+++ b/tests/update1.c
@@ -121,6 +121,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   exit (1);
 }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update2.c b/tests/update2.c
index 3e22879..1dcff3f 100644
--- a/tests/update2.c
+++ b/tests/update2.c
@@ -144,6 +144,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   exit (1);
 }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update3.c b/tests/update3.c
index d619bed..9d4f880 100644
--- a/tests/update3.c
+++ b/tests/update3.c
@@ -199,6 +199,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   exit (1);
 }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update4.c b/tests/update4.c
index 8196b8c..a762e0a 100644
--- a/tests/update4.c
+++ b/tests/update4.c
@@ -351,6 +351,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   exit (1);
 }
 
+  close (fd);
   unlink (fname);
 
   return 0;
-- 
2.1.4



[PATCH] Drop the trickery in printversion.h

2017-05-04 Thread Ulf Hermann
The mechanism of moving argp_program_version_hook and
argp_program_bug_address to .rodata is not portable and two pointers
per program are not worth the effort to make it portable. Revert the
pointers to be non-const.

Signed-off-by: Ulf Hermann 
---
 lib/ChangeLog  |  5 +
 lib/printversion.h | 10 +++---
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/ChangeLog b/lib/ChangeLog
index 59939bd..0433f02 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-04  Ulf Hermann  
 
+   * printversion.h: Define ARGP_PROGRAM_VERSION_HOOK_DEF and
+   ARGP_BUG_ADDRESS_DEF to be non-const and drop the asm tricks.
+
+2017-05-04  Ulf Hermann  
+
* system.h: Define FILE_SYSTEM_PREFIX_LEN, ISDIRSEP, DIRSEP, PATHSEP,
and IS_ABSOLUTE_PATH to help with handling file system paths.
 
diff --git a/lib/printversion.h b/lib/printversion.h
index a9e059f..090b53a 100644
--- a/lib/printversion.h
+++ b/lib/printversion.h
@@ -36,14 +36,10 @@
 void print_version (FILE *stream, struct argp_state *state);
 
 /* We need define two variables, argp_program_version_hook and
-   argp_program_bug_address, in all programs.  argp.h declares these
-   variables as non-const (which is correct in general).  But we can
-   do better, it is not going to change.  So we want to move them into
-   the .rodata section.  Define macros to do the trick.  */
+   argp_program_bug_address, in all programs. */
 #define ARGP_PROGRAM_VERSION_HOOK_DEF \
-  void (*const apvh) (FILE *, struct argp_state *) \
-   __asm ("argp_program_version_hook")
+  void (*argp_program_version_hook) (FILE *, struct argp_state *)
 #define ARGP_PROGRAM_BUG_ADDRESS_DEF \
-  const char *const apba__ __asm ("argp_program_bug_address")
+  const char *argp_program_bug_address
 
 #endif // PRINTVERSION_H
-- 
2.1.4



[PATCH] Provide a windows-specific ORIGINDIR for eblopenbackend

2017-05-04 Thread Ulf Hermann
$ORIGIN and $LIB are not supported by all implementations of dlopen() and
on windows we need backslashes as directory separators.

Signed-off-by: Ulf Hermann 
---
 libebl/ChangeLog| 5 +
 libebl/eblopenbackend.c | 4 
 2 files changed, 9 insertions(+)

diff --git a/libebl/ChangeLog b/libebl/ChangeLog
index f9f5d89..7de7236 100644
--- a/libebl/ChangeLog
+++ b/libebl/ChangeLog
@@ -1,3 +1,8 @@
+2017-05-04  Ulf Hermann  
+
+   * eblopenbackend.c: On windows use an ORIGINDIR without $ORIGIN and
+   $LIB.
+
 2017-04-28  Ulf Hermann  
 
* Makefile.am: Forward LIBPREFIX and LIBEXT as defined by configure.
diff --git a/libebl/eblopenbackend.c b/libebl/eblopenbackend.c
index 0b261e7..4048f12 100644
--- a/libebl/eblopenbackend.c
+++ b/libebl/eblopenbackend.c
@@ -312,7 +312,11 @@ openbackend (Elf *elf, const char *emulation, GElf_Half 
machine)
 #ifndef LIBEBL_SUBDIR
 # define LIBEBL_SUBDIR PACKAGE
 #endif
+#if (defined _WIN32 || defined __WIN32__)
+#define ORIGINDIR "..\\lib\\" LIBEBL_SUBDIR "\\"
+#else
 #define ORIGINDIR "$ORIGIN/../$LIB/" LIBEBL_SUBDIR "/"
+#endif
 #define LIBEBL_PREFIX LIBPREFIX "ebl_"
 
/* Give it a try.  At least the machine type matches.  First
-- 
2.1.4



[PATCH] Provide build rules for a compatibility library

2017-05-04 Thread Ulf Hermann
We need to export open(), close(), malloc(), and free() from the C
library we are using so that we can handle resources passed to and from
elfutils correctly. For example, on Windows, calling free() on memory
malloc()'d with a different C library will not work. In addition, having
__cxa_demangle from the GNU libstdc++ available is very helpful, so we
include that, too.

Signed-off-by: Ulf Hermann 
---
 ChangeLog|  4 
 configure.ac |  1 +
 lib/ChangeLog|  6 ++
 lib/Makefile.am  | 35 +--
 lib/eu_compat.def.in |  7 +++
 5 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 lib/eu_compat.def.in

diff --git a/ChangeLog b/ChangeLog
index 2cf7bd6..187ef59 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * configure.ac: Determine names for eu_compat library.
+
+2017-05-04  Ulf Hermann  
+
* configure.ac: Check for fork().
 
 2017-05-04  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index 5fa20c0..c0efdfc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -95,6 +95,7 @@ AM_CONDITIONAL(NATIVE_MACHO, test "$LIBEXT" = ".dylib")
 eu_LIBNAME(elf,1)
 eu_LIBNAME(dw,1)
 eu_LIBNAME(asm,1)
+eu_LIBNAME(eu_compat,1)
 
 AC_ARG_ENABLE(deterministic-archives,
 [AS_HELP_STRING([--enable-deterministic-archives],
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 0433f02..ceb6ac2 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,11 @@
 2017-05-04  Ulf Hermann  
 
+   * Makefile.am: On windows, build an eu_compat.dll to forward symbols
+   from the C and std C++ libraries elfutils links against.
+   * eu_compat.def.in: New file.
+
+2017-05-04  Ulf Hermann  
+
* printversion.h: Define ARGP_PROGRAM_VERSION_HOOK_DEF and
ARGP_BUG_ADDRESS_DEF to be non-const and drop the asm tricks.
 
diff --git a/lib/Makefile.am b/lib/Makefile.am
index afff717..48947c6 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -57,5 +57,36 @@ install-headers:
 uninstall-headers:
 endif
 
-install: install-am install-headers
-uninstall: uninstall-am uninstall-headers
+EXTRA_DIST += eu_compat.def.in
+if NATIVE_PE
+eu_compat.def: eu_compat.def.in
+   cp $< $@
+
+$(libeu_compat_BARE): eu_compat.def
+   $(CCLD) $(dso_LDFLAGS) $(LDFLAGS) -o $@ $< -lstdc++
+
+noinst_DATA = $(eu_compat_BARE)
+
+install-lib: $(libeu_compat_BARE) $(libeu_compat_BARE:.dll=.lib)
+   $(mkinstalldirs) $(DESTDIR)$(libdir)
+   $(INSTALL_PROGRAM) $(libeu_compat_BARE) 
$(DESTDIR)$(libdir)/$(libeu_compat_VERSIONED)
+   ln -fs $(libeu_compat_VERSIONED) 
$(DESTDIR)$(libdir)/$(libeu_compat_SONAME)
+   ln -fs $(libeu_compat_SONAME) $(DESTDIR)$(libdir)/$(libeu_compat_BARE)
+   $(INSTALL_PROGRAM) $(libeu_compat_BARE:.dll=.lib) 
$(DESTDIR)$(libdir)/$(libeu_compat_VERSIONED:.dll=.lib)
+   ln -fs $(libeu_compat_VERSIONED:.dll=.lib) 
$(DESTDIR)$(libdir)/$(libeu_compat_SONAME:.dll=.lib)
+   ln -fs $(libeu_compat_SONAME:.dll=.lib) 
$(DESTDIR)$(libdir)/$(libeu_compat_BARE:.dll=.lib)
+uninstall-lib:
+   rm -f $(DESTDIR)$(libdir)/$(libeu_compat_VERSIONED)
+   rm -f $(DESTDIR)$(libdir)/$(libeu_compat_SONAME)
+   rm -f $(DESTDIR)$(libdir)/$(libeu_compat_BARE)
+   rm -f $(DESTDIR)$(libdir)/$(libeu_compat_VERSIONED:.dll=.lib)
+   rm -f $(DESTDIR)$(libdir)/$(libeu_compat_SONAME:.dll=.lib)
+   rm -f $(DESTDIR)$(libdir)/$(libeu_compat_BARE:.dll=.lib)
+CLEANFILES += $(libeu_compat_BARE:.dll=.lib) $(libeu_compat_BARE:.dll=.exp)
+else
+install-lib:
+uninstall-lib:
+endif
+
+install: install-am install-headers install-lib
+uninstall: uninstall-am uninstall-headers install-lib
diff --git a/lib/eu_compat.def.in b/lib/eu_compat.def.in
new file mode 100644
index 000..c2aa88f
--- /dev/null
+++ b/lib/eu_compat.def.in
@@ -0,0 +1,7 @@
+LIBRARY "eu_compat.dll"
+EXPORTS
+eu_compat_demangle=__cxa_demangle
+eu_compat_open=msvcrt._open
+eu_compat_close=msvcrt._close
+eu_compat_malloc=msvcrt.malloc
+eu_compat_free=msvcrt.free
-- 
2.1.4



[PATCH] Increase stack usage limit to 512k

2017-05-04 Thread Ulf Hermann
On windows x86_64 we need more stack in some places.

Signed-off-by: Ulf Hermann 
---
 config/ChangeLog | 4 
 config/eu.am | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/config/ChangeLog b/config/ChangeLog
index 0f240ea..642d765 100644
--- a/config/ChangeLog
+++ b/config/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  
+
+   * eu.am: Increase stack limit to 512k.
+
 2017-04-28  Ulf Hermann  
 
* eu.am: Disable textrel_check if we're not building ELF files.
diff --git a/config/eu.am b/config/eu.am
index 637a2c2..d7befba 100644
--- a/config/eu.am
+++ b/config/eu.am
@@ -39,9 +39,9 @@ endif
 # with deterministic archives.
 ARFLAGS = cr
 
-# Warn about stack usage of more than 256K = 262144 bytes.
+# Warn about stack usage of more than 512K = 524288 bytes.
 if ADD_STACK_USAGE_WARNING
-STACK_USAGE_WARNING=-Wstack-usage=262144
+STACK_USAGE_WARNING=-Wstack-usage=524288
 else
 STACK_USAGE_WARNING=
 endif
-- 
2.1.4



[PATCH] Cast pid_t to int when printing it with %d

2017-05-04 Thread Ulf Hermann
On windows x86_64 pid_t is 64bit wide. We ignore the extra bits as those
messages are only informational anyway.

Signed-off-by: Ulf Hermann 
---
 src/ChangeLog |  4 
 src/stack.c   | 20 ++--
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 0d1e57d..3c880b4 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * stack.c: Cast pid_t to int when printing using %d.
+
+2017-05-04  Ulf Hermann  
+
* addr2line.c: Don't assume unix file system conventions.
* size.c: Likewise.
* strip.c: Likewise.
diff --git a/src/stack.c b/src/stack.c
index 1f5a1c6..eb62b8a 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -362,7 +362,7 @@ print_frames (struct frames *frames, pid_t tid, int 
dwflerr, const char *what)
   if (frames->frames > 0)
 frames_shown = true;
 
-  printf ("TID %d:\n", tid);
+  printf ("TID %d:\n", (int)tid);
   int frame_nr = 0;
   for (int nr = 0; nr < frames->frames && (maxframes == 0
   || frame_nr < maxframes); nr++)
@@ -420,7 +420,7 @@ print_frames (struct frames *frames, pid_t tid, int 
dwflerr, const char *what)
 
   if (frames->frames > 0 && frame_nr == maxframes)
 error (0, 0, "tid %d: shown max number of frames "
-  "(%d, use -n 0 for unlimited)", tid, maxframes);
+  "(%d, use -n 0 for unlimited)", (int)tid, maxframes);
   else if (dwflerr != 0)
 {
   if (frames->frames > 0)
@@ -440,11 +440,11 @@ print_frames (struct frames *frames, pid_t tid, int 
dwflerr, const char *what)
  else
modname = "";
}
- error (0, 0, "%s tid %d at 0x%" PRIx64 " in %s: %s", what, tid,
+ error (0, 0, "%s tid %d at 0x%" PRIx64 " in %s: %s", what, (int)tid,
 pc_adjusted, modname, dwfl_errmsg (dwflerr));
}
   else
-   error (0, 0, "%s tid %d: %s", what, tid, dwfl_errmsg (dwflerr));
+   error (0, 0, "%s tid %d: %s", what, (int)tid, dwfl_errmsg (dwflerr));
 }
 }
 
@@ -575,10 +575,10 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
 
  int err = dwfl_linux_proc_report (dwfl, pid);
  if (err < 0)
-   error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %d: %s", pid,
+   error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %d: %s", (int)pid,
   dwfl_errmsg (-1));
  else if (err > 0)
-   error (EXIT_BAD, err, "dwfl_linux_proc_report pid %d", pid);
+   error (EXIT_BAD, err, "dwfl_linux_proc_report pid %d", (int)pid);
}
 
   if (core != NULL)
@@ -597,10 +597,10 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
{
  int err = dwfl_linux_proc_attach (dwfl, pid, false);
  if (err < 0)
-   error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %d: %s", pid,
+   error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %d: %s", (int)pid,
   dwfl_errmsg (-1));
  else if (err > 0)
-   error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %d", pid);
+   error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %d", (int)pid);
}
 
   if (core != NULL)
@@ -688,7 +688,7 @@ invoked with bad or missing arguments it will exit with 
return code 64.")
 
   if (show_modules)
 {
-  printf ("PID %d - %s module memory map\n", dwfl_pid (dwfl),
+  printf ("PID %d - %s module memory map\n", (int)dwfl_pid (dwfl),
  pid != 0 ? "process" : "core");
   if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
error (EXIT_BAD, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
@@ -721,7 +721,7 @@ invoked with bad or missing arguments it will exit with 
return code 64.")
 }
   else
 {
-  printf ("PID %d - %s\n", dwfl_pid (dwfl), pid != 0 ? "process" : "core");
+  printf ("PID %d - %s\n", (int)dwfl_pid (dwfl), pid != 0 ? "process" : 
"core");
   switch (dwfl_getthreads (dwfl, thread_callback, &frames))
{
case DWARF_CB_OK:
-- 
2.1.4



[PATCH] Write to /dev/null rather than /dev/zero

2017-05-04 Thread Ulf Hermann
/dev/zero is meant for reading zeroes. /dev/null is for writing into
nirvana.

Signed-off-by: Ulf Hermann 
---
 tests/ChangeLog | 4 
 tests/elfshphehdr.c | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index b8de138..fef6f55 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * elfshphehdr.c: For writing, use /dev/null rather than /dev/zero.
+
+2017-05-04  Ulf Hermann  
+
* newfile.c: Close the file when we're done and unlink it afterwards.
* newscn.c: Likewise.
* update1.c: Likewise.
diff --git a/tests/elfshphehdr.c b/tests/elfshphehdr.c
index d1ab633..e0f0712 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 | O_BINARY);
+  int fd = fd = open("/dev/null", O_WRONLY | O_BINARY);
   check ("open", fd >= 0);
 
   Elf *elf;
-- 
2.1.4



[PATCH] Use octal numbers rather than permission macros

2017-05-04 Thread Ulf Hermann
The permission macros are not guaranteed to be defined and the octal
numbers are rather well known.

Signed-off-by: Ulf Hermann 
---
 src/ChangeLog   | 7 +++
 src/ar.c| 8 
 src/elfcompress.c   | 4 ++--
 src/ranlib.c| 2 +-
 src/strip.c | 2 +-
 tests/ChangeLog | 4 
 tests/elfstrmerge.c | 4 ++--
 7 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 44b4395..15cd55f 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,12 @@
 2017-05-04  Ulf Hermann  
 
+   * ar.c: Use octal numbers instead of permission macros.
+   * elfcompress.c: Likewise.
+   * ranlib.c: Likewise.
+   * strip.c: Likewise.
+
+2017-05-04  Ulf Hermann  
+
* unstrip.c: Use strndup and free instead of strndupa.
 
 2017-05-04  Ulf Hermann  
diff --git a/src/ar.c b/src/ar.c
index 91391b1..cc47f10 100644
--- a/src/ar.c
+++ b/src/ar.c
@@ -799,7 +799,7 @@ cannot rename temporary file to %.*s"),
 #if HAVE_DECL_FCHMOD
  /* Set the mode of the new file to the same values the
 original file has.  */
- fchmod (newfd, st.st_mode & ALLPERMS) != 0 ||
+ fchmod (newfd, st.st_mode & 0) != 0 ||
 #endif
  (
 #if HAVE_DECL_FCHOWN
@@ -1057,7 +1057,7 @@ do_oper_delete (const char *arfname, char **argv, int 
argc,
 #if HAVE_DECL_FCHMOD
   /* Set the mode of the new file to the same values the original file
  has.  */
-  fchmod (newfd, st.st_mode & ALLPERMS) != 0 ||
+  fchmod (newfd, st.st_mode & 0) != 0 ||
 #endif
   (
 #if HAVE_DECL_FCHOWN
@@ -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_BINARY | O_CREAT | O_EXCL, 
DEFFILEMODE);
+  newfd = open (arfname, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0666);
   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);
@@ -1521,7 +1521,7 @@ do_oper_insert (int oper, const char *arfname, char 
**argv, int argc,
 #if HAVE_DECL_FCHMOD
   /* Set the mode of the new file to the same values the original file
  has.  */
-  fchmod (newfd, st.st_mode & ALLPERMS) != 0 ||
+  fchmod (newfd, st.st_mode & 0) != 0 ||
 #endif
  (
 #if HAVE_DECL_FCHOWN
diff --git a/src/elfcompress.c b/src/elfcompress.c
index 5dbeb57..6080db7 100644
--- a/src/elfcompress.c
+++ b/src/elfcompress.c
@@ -542,7 +542,7 @@ process_file (const char *fname)
   else
 {
   fnew = xstrdup (foutput);
-  fdnew = open (fnew, O_WRONLY | O_BINARY | O_CREAT, st.st_mode & 
ALLPERMS);
+  fdnew = open (fnew, O_WRONLY | O_BINARY | O_CREAT, st.st_mode & 0);
 }
 
   if (fdnew < 0)
@@ -1237,7 +1237,7 @@ process_file (const char *fname)
 
 #if HAVE_DECL_FCHMOD
   /* Try to match mode and owner.group of the original file.  */
-  if (fchmod (fdnew, st.st_mode & ALLPERMS) != 0)
+  if (fchmod (fdnew, st.st_mode & 0) != 0)
 if (verbose >= 0)
   error (0, errno, "Couldn't fchmod %s", fnew);
 #endif
diff --git a/src/ranlib.c b/src/ranlib.c
index 41057de..22aac28 100644
--- a/src/ranlib.c
+++ b/src/ranlib.c
@@ -261,7 +261,7 @@ handle_file (const char *fname)
 #if HAVE_DECL_FCHMOD
  /* Set the mode of the new file to the same values the
 original file has.  */
- fchmod (newfd, st.st_mode & ALLPERMS) != 0 ||
+ fchmod (newfd, st.st_mode & 0) != 0 ||
 #endif
  (
 #if HAVE_DECL_FCHOWN
diff --git a/src/strip.c b/src/strip.c
index 60f6700..14d2249 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -347,7 +347,7 @@ process_file (const char *fname)
   switch (elf_kind (elf))
 {
 case ELF_K_ELF:
-  result = handle_elf (fd, elf, NULL, fname, st.st_mode & ACCESSPERMS,
+  result = handle_elf (fd, elf, NULL, fname, st.st_mode & 0777,
   preserve_dates ? tv : NULL);
   break;
 
diff --git a/tests/ChangeLog b/tests/ChangeLog
index fef6f55..9c04404 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * elfstrmerge.c: Use 0 instead of ALLPERMS.
+
+2017-05-04  Ulf Hermann  
+
* elfshphehdr.c: For writing, use /dev/null rather than /dev/zero.
 
 2017-05-04  Ulf Hermann  
diff --git a/tests/elfstrmerge.c b/tests/elfstrmerge.c
index ff15f57..5405ed8 100644
--- a/tests/elfstrmerge.c
+++ b/tests/elfstrmerge.c
@@ -367,7 +367,7 @@ main (int argc, char **argv)
   else
 {
   fnew = argv[2];
-  fdnew = open (fnew, O_WRONLY | O_CREAT | O_BINARY, st.st_mode & 
ALLPERMS);
+  fdnew = open (fnew, O_WRONLY | O_CREAT | O_BINARY, st.st_mode & 0);
 }
 
   if (fdnew < 0)
@@ -652,7 +652,7 @@ main (int argc, char **argv)
 
 #if HAVE_DECL_FCHMOD
   /* Try to match mode

[PATCH] Correctly determine STACKCMD on windows

2017-05-04 Thread Ulf Hermann
error() will only output the file name, but with ".exe" on windows.

Signed-off-by: Ulf Hermann 
---
 tests/ChangeLog   | 6 ++
 tests/run-stack-d-test.sh | 5 +
 tests/run-stack-demangled-test.sh | 5 +
 tests/run-stack-i-test.sh | 5 +
 4 files changed, 21 insertions(+)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index 9c04404..f43aeba 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,11 @@
 2017-05-04  Ulf Hermann  
 
+   * run-stack-d-test.sh: Correctly detect STACKCMD on windows.
+   * run-stack-demangled-test.sh: Likewise.
+   * run-stack-i-test.sh: Likewise.
+
+2017-05-04  Ulf Hermann  
+
* elfstrmerge.c: Use 0 instead of ALLPERMS.
 
 2017-05-04  Ulf Hermann  
diff --git a/tests/run-stack-d-test.sh b/tests/run-stack-d-test.sh
index a9f0380..ef3c6e8 100755
--- a/tests/run-stack-d-test.sh
+++ b/tests/run-stack-d-test.sh
@@ -64,6 +64,11 @@ testfiles testfiledwarfinlines testfiledwarfinlines.core
 # the error message, which we also try to match.
 if test "$elfutils_testrun" = "installed"; then
 STACKCMD=${bindir}/`program_transform stack`
+if test -f ${STACKCMD}.exe; then
+STACKCMD=`program_transform stack`.exe
+fi
+elif test -f ${abs_top_builddir}/src/stack.exe; then
+STACKCMD=stack.exe
 else
 STACKCMD=${abs_top_builddir}/src/stack
 fi
diff --git a/tests/run-stack-demangled-test.sh 
b/tests/run-stack-demangled-test.sh
index c26918f..f6899bb 100755
--- a/tests/run-stack-demangled-test.sh
+++ b/tests/run-stack-demangled-test.sh
@@ -33,6 +33,11 @@ testfiles testfiledwarfinlines testfiledwarfinlines.core
 # the error message, which we also try to match.
 if test "$elfutils_testrun" = "installed"; then
 STACKCMD=${bindir}/`program_transform stack`
+if test -f ${STACKCMD}.exe; then
+STACKCMD=`program_transform stack`.exe
+fi
+elif test -f ${abs_top_builddir}/src/stack.exe; then
+STACKCMD=stack.exe
 else
 STACKCMD=${abs_top_builddir}/src/stack
 fi
diff --git a/tests/run-stack-i-test.sh b/tests/run-stack-i-test.sh
index 3722ab0..a09e46d 100755
--- a/tests/run-stack-i-test.sh
+++ b/tests/run-stack-i-test.sh
@@ -25,6 +25,11 @@ testfiles testfiledwarfinlines testfiledwarfinlines.core
 # the error message, which we also try to match.
 if test "$elfutils_testrun" = "installed"; then
 STACKCMD=${bindir}/`program_transform stack`
+if test -f ${STACKCMD}.exe; then
+STACKCMD=`program_transform stack`.exe
+fi
+elif test -f ${abs_top_builddir}/src/stack.exe; then
+STACKCMD=stack.exe
 else
 STACKCMD=${abs_top_builddir}/src/stack
 fi
-- 
2.1.4



[PATCH] Remove previous test files before running the next round

2017-05-04 Thread Ulf Hermann
strip explicitly creates the new files. This will not work on windows if
the files already exist.

Signed-off-by: Ulf Hermann 
---
 tests/ChangeLog  | 5 +
 tests/run-strip-reloc.sh | 2 ++
 2 files changed, 7 insertions(+)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index f43aeba..1a77c02 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -6,6 +6,11 @@
 
 2017-05-04  Ulf Hermann  
 
+   * run-strip-reloc.sh: Remove previous testfiles before running the
+   next test.
+
+2017-05-04  Ulf Hermann  
+
* elfstrmerge.c: Use 0 instead of ALLPERMS.
 
 2017-05-04  Ulf Hermann  
diff --git a/tests/run-strip-reloc.sh b/tests/run-strip-reloc.sh
index 50dddbf..f44991d 100755
--- a/tests/run-strip-reloc.sh
+++ b/tests/run-strip-reloc.sh
@@ -32,6 +32,8 @@ runtest() {
   outfile2=out.stripped2
   debugfile2=out.debug2
 
+  rm -f $outfile1 $debugfile1 $outfile2 $debugfile2
+
   testrun ${abs_top_builddir}/src/strip -o $outfile1 -f $debugfile1 $infile ||
   { echo "*** failure strip $infile"; status=1; }
 
-- 
2.1.4



dwfl_module_addrdie fails for binaries built with clang++

2017-05-04 Thread Milian Wolff
Hey all,

I noticed that elfutils fails to handle clang binaries when we want to find a 
DIE for a certain address. I.e. dwfl_module_addrdie returns nullptr, and eu-
addr2line fails to resolve inlined frames. 

To reproduce this:

~~~
$ cat test.cpp
#include 
#include 
#include 

using namespace std;

int main()
{
uniform_real_distribution uniform(-1E5, 1E5);
default_random_engine engine;
double s = 0;
for (int i = 0; i < 1000; ++i) {
s += uniform(engine);
}
cout << "random sum: " << s << '\n';
return 0;
}
$ clang++ -O2 -g test.cpp -o test --std=c++11
$ objdump -Sl test | grep random.h -A2

  400a10:   48 89 f0mov%rsi,%rax
--
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.h:1818
operator()(_UniformRandomNumberGenerator& __urng,
   const param_type& __p)
$ addr2line -e test -i 400a10
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.h:143
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.h:151
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.h:332
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.tcc:3332
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.h:183
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.h:1818
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../include/c++/6.3.1/
bits/random.h:1809
/tmp/test.cpp:13
$ eu-addr2line -e test -i 400a10
??:0
~

This also affects us in our perfparser. Not being able to find a cudie means 
not finding inlined frames nor file/line mappings, which is quite a set-back.

I have noticed that backward-cpp contains a (partially) work-around for this:

https://github.com/bombela/backward-cpp/blob/master/backward.hpp#L1216

Is this the right approach and also what the non-eu addr2line does? If so, can 
that be added upstream too, such that dwfl_module_addrdie can be relied on?

I've seen it on clang 3.6, 4 and 5. Neither passing -g3 nor -gdwarf-aranges 
helps.

Thanks
-- 
Milian Wolff
m...@milianw.de
http://milianw.de


[PATCH] Define uid_t and gid_t in system-elf-libelf-test.c if necessary

2017-05-04 Thread Ulf Hermann
elf.h does include features.h which should define those. However, on
windows there is no features.h. We have the empty features.h in libgnu
that depends on config.h being included before (which we can't), and the
features.h in lib that is only available when installed in selfcontained
mode. Therefore we need a workaround here.

Signed-off-by: Ulf Hermann 
---
 tests/ChangeLog| 4 
 tests/system-elf-libelf-test.c | 5 +
 2 files changed, 9 insertions(+)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index 1a77c02..678a882 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -11,6 +11,10 @@
 
 2017-05-04  Ulf Hermann  
 
+   * system-elf-libelf-test.c: Define uid_t and gid_t on windows.
+
+2017-05-04  Ulf Hermann  
+
* elfstrmerge.c: Use 0 instead of ALLPERMS.
 
 2017-05-04  Ulf Hermann  
diff --git a/tests/system-elf-libelf-test.c b/tests/system-elf-libelf-test.c
index 7dfe498..d9b9cd5 100644
--- a/tests/system-elf-libelf-test.c
+++ b/tests/system-elf-libelf-test.c
@@ -16,6 +16,11 @@
You should have received a copy of the GNU General Public License
along with this program.  If not, see .  */
 
+#if ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
+#define uid_t int
+#define gid_t int
+#endif
+
 #include 
 #include 
 #include "../libelf/libelf.h"
-- 
2.1.4



[PATCH v2] Close files before renaming or unlinking them

2017-05-04 Thread Ulf Hermann
On windows we cannot rename or unlink open files.

(strip.c was missing)

Signed-off-by: Ulf Hermann 
---
 libasm/ChangeLog |  4 
 libasm/asm_end.c | 15 +++
 src/ChangeLog|  4 
 src/strip.c  |  5 -
 tests/ChangeLog  |  9 +
 tests/newfile.c  |  7 +--
 tests/newscn.c   |  3 ++-
 tests/update1.c  |  1 +
 tests/update2.c  |  1 +
 tests/update3.c  |  1 +
 tests/update4.c  |  1 +
 11 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index 2b499c7..0e67657 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * asm_end.c: Rename the output file only after freeing resources.
+
+2017-05-04  Ulf Hermann  
+
* asm_end.c: Don't fchmod the new file if fchmod is unavailable.
 
 2017-02-28  Ulf Hermann  
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
index 7fabe94..7891fbb 100644
--- a/libasm/asm_end.c
+++ b/libasm/asm_end.c
@@ -521,16 +521,23 @@ asm_end (AsmCtx_t *ctx)
 }
 #endif
 
+  char *tmp_fname = strdup (ctx->tmp_fname);
+  char *fname = strdup (ctx->fname);
+
+  /* Free the resources.  */
+  __libasm_finictx (ctx);
+
   /* Rename output file.  */
-  if (rename (ctx->tmp_fname, ctx->fname) != 0)
+  result = rename (tmp_fname, fname);
+  free (tmp_fname);
+  free (fname);
+
+  if (result != 0)
 {
   __libasm_seterrno (ASM_E_CANNOT_RENAME);
   return -1;
 }
 
-  /* Free the resources.  */
-  __libasm_finictx (ctx);
-
   return 0;
 }
 
diff --git a/src/ChangeLog b/src/ChangeLog
index 0d1e57d..a474331 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * strip.c: Close and reopen file when renaming.
+
+2017-05-04  Ulf Hermann  
+
* addr2line.c: Don't assume unix file system conventions.
* size.c: Likewise.
* strip.c: Likewise.
diff --git a/src/strip.c b/src/strip.c
index 60f6700..99d7dd1 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -2006,7 +2006,10 @@ handle_elf (int fd, Elf *elf, const char *prefix, const 
char *fname,
 
   /* Create the real output file.  First rename, then change the
 mode.  */
-  if (rename (tmp_debug_fname, debug_fname) != 0
+  close (debug_fd);
+  int rename_result = rename (tmp_debug_fname, debug_fname);
+  debug_fd = open (debug_fname, O_RDONLY | O_BINARY);
+  if (rename_result != 0 || debug_fd == -1
 #if HAVE_DECL_FCHMOD
  || fchmod (debug_fd, mode) != 0
 #endif
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 5e29f82..b8de138 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,14 @@
 2017-05-04  Ulf Hermann  
 
+   * newfile.c: Close the file when we're done and unlink it afterwards.
+   * newscn.c: Likewise.
+   * update1.c: Likewise.
+   * update2.c: Likewise.
+   * update3.c: Likewise.
+   * update4.c: Likewise.
+
+2017-05-04  Ulf Hermann  
+
* asm-tst4.c: Don't assume unix file system conventions.
* asm-tst5.c: Likewise.
* asm-tst6.c: Likewise.
diff --git a/tests/newfile.c b/tests/newfile.c
index 5eabdcb..a279317 100644
--- a/tests/newfile.c
+++ b/tests/newfile.c
@@ -63,8 +63,6 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   printf ("cannot create temporary file: %m\n");
   exit (1);
 }
-  /* Remove the file when we exit.  */
-  unlink (fname);
 
   elf_version (EV_CURRENT);
   elf = elf_begin (fd, ELF_C_WRITE, NULL);
@@ -166,5 +164,10 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   (void) elf_end (elf);
 }
 
+  close (fd);
+
+  /* Remove the file when we exit.  */
+  unlink (fname);
+
   return result;
 }
diff --git a/tests/newscn.c b/tests/newscn.c
index 466f2f6..de8951d 100644
--- a/tests/newscn.c
+++ b/tests/newscn.c
@@ -46,7 +46,6 @@ main (void)
   fprintf (stderr, "Failed to open fdput file: %s\n", name);
   exit (1);
 }
-  unlink (name);
 
   elf = elf_begin (fd, ELF_C_WRITE, NULL);
   if (elf == NULL)
@@ -62,5 +61,7 @@ main (void)
   elf_end (elf);
   close (fd);
 
+  unlink (name);
+
   return 0;
 }
diff --git a/tests/update1.c b/tests/update1.c
index a571618..548c6d8 100644
--- a/tests/update1.c
+++ b/tests/update1.c
@@ -121,6 +121,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   exit (1);
 }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update2.c b/tests/update2.c
index 3e22879..1dcff3f 100644
--- a/tests/update2.c
+++ b/tests/update2.c
@@ -144,6 +144,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   exit (1);
 }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update3.c b/tests/update3.c
index d619bed..9d4f880 100644
--- a/tests/update3.c
+++ b/tests/update3.c
@@ -199,6 +199,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   exit (1);
 }
 
+  close (fd);
   unlink (fname);
 
   return 0;
diff --git a/tests/update4.c b/tests/update4.c
index 8196b8c..a762e0a 100644
--- a/t

[PATCH] Use trees rather than hashes in ar.c

2017-05-04 Thread Ulf Hermann
The tree functions are more widely available.

Signed-off-by: Ulf Hermann 
---
 src/ChangeLog |  4 
 src/ar.c  | 77 ++-
 2 files changed, 48 insertions(+), 33 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 332b07c..32cd0c3 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-04  Ulf Hermann  
 
+   * ar.c: Use trees rather than hashes.
+
+2017-05-04  Ulf Hermann  
+
* ar.c: Use octal numbers instead of permission macros.
* elfcompress.c: Likewise.
* ranlib.c: Likewise.
diff --git a/src/ar.c b/src/ar.c
index cc47f10..a13420c 100644
--- a/src/ar.c
+++ b/src/ar.c
@@ -439,6 +439,20 @@ copy_content (Elf *elf, int newfd, off_t off, size_t n)
 
 
 static int
+string_compare (const void *a, const void *b)
+{
+  return strcmp((const char *)a, (const char *)b);
+}
+
+
+void
+free_node (void *node)
+{
+  (void) node;
+}
+
+
+static int
 do_oper_extract (int oper, const char *arfname, char **argv, int argc,
 long int instance)
 {
@@ -469,13 +483,11 @@ do_oper_extract (int oper, const char *arfname, char 
**argv, int argc,
   Elf *elf;
   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"));
+  void *root = NULL;
 
   for (int cnt = 0; cnt < argc; ++cnt)
 {
-  ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
-  if (hsearch (entry, ENTER) == NULL)
+  if (tsearch (argv[cnt], &root, &string_compare) == NULL)
error (EXIT_FAILURE, errno,
   gettext ("cannot insert into hash table"));
 }
@@ -517,12 +529,10 @@ do_oper_extract (int oper, const char *arfname, char 
**argv, int argc,
   bool do_extract = argc <= 0;
   if (!do_extract)
{
- ENTRY entry;
- entry.key = arhdr->ar_name;
- ENTRY *res = hsearch (entry, FIND);
+ void *res = tfind (arhdr->ar_name, &root, &string_compare);
  if (res != NULL && (instance < 0 || instance-- == 0)
- && !found[(char **) res->data - argv])
-   found[(char **) res->data - argv] = do_extract = true;
+ && !found[(char **) res - argv])
+   found[(char **) res - argv] = do_extract = true;
}
 
   if (do_extract)
@@ -741,7 +751,8 @@ cannot rename temporary file to %.*s"),
error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
 }
 
-  hdestroy ();
+  tdestroy(root, &free_node);
+  root = NULL;
 
   if (force_symtab)
 {
@@ -921,13 +932,11 @@ do_oper_delete (const char *arfname, char **argv, int 
argc,
   struct stat st;
   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"));
+  void *root = NULL;
 
   for (int cnt = 0; cnt < argc; ++cnt)
 {
-  ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
-  if (hsearch (entry, ENTER) == NULL)
+  if (tsearch (argv[cnt], &root, &string_compare) == NULL)
error (EXIT_FAILURE, errno,
   gettext ("cannot insert into hash table"));
 }
@@ -949,12 +958,10 @@ do_oper_delete (const char *arfname, char **argv, int 
argc,
   bool do_delete = argc <= 0;
   if (!do_delete)
{
- ENTRY entry;
- entry.key = arhdr->ar_name;
- ENTRY *res = hsearch (entry, FIND);
+ void *res = tfind (arhdr->ar_name, &root, &string_compare);
  if (res != NULL && (instance < 0 || instance-- == 0)
- && !found[(char **) res->data - argv])
-   found[(char **) res->data - argv] = do_delete = true;
+ && !found[(char **) res - argv])
+   found[(char **) res - argv] = do_delete = true;
}
 
   if (do_delete)
@@ -995,7 +1002,8 @@ do_oper_delete (const char *arfname, char **argv, int argc,
 
   arlib_finalize ();
 
-  hdestroy ();
+  tdestroy (root, &free_node);
+  root = NULL;
 
   /* Create a new, temporary file in the same directory as the
  original file.  */
@@ -1093,6 +1101,13 @@ no0print (bool ofmt, char *buf, int bufsize, long int 
val)
 
 
 static int
+basename_compare(const void *a, const void *b)
+{
+  return strcmp(basename((const char *)a), basename((const char *)b));
+}
+
+
+static int
 do_oper_insert (int oper, const char *arfname, char **argv, int argc,
const char *member)
 {
@@ -1100,6 +1115,7 @@ do_oper_insert (int oper, const char *arfname, char 
**argv, int argc,
   Elf *elf;
   struct stat st;
   int fd = open_archive (arfname, O_RDONLY | O_BINARY, 0, &elf, &st, oper != 
oper_move);
+  void *root = NULL;
 
   /* List of the files we keep.  */
   struct armem *all = NULL;
@@ -1127,15 +1143,9 @@ do_oper_insert (int oper, const char *arfname, char 
**argv, int argc,
  index.  */
   if (oper != oper_qappend)
 {
-  if (hcreate (2 * argc) == 0)
-   error (EXIT_

windows patches

2017-05-04 Thread Ulf Hermann
Hi,

all patches I consider suitable for upstream right now have been posted here 
now. With all of them applied you still don't get useful elfutils on windows, 
as for example the date preservation in some of the tools doesn't work. I have 
more patches that just blatantly drop functionality I don't need in order to 
make things work. I don't think you want those and in fact I'm fairly sure I'm 
stretching it already, so I'll stop here.

I might come back with more patches when I find a better solution to the 
remaining issues.

cheers,
Ulf