[PATCH] Check for existence of asprintf and vasprintf

Ulf Hermann ulf.hermann@qt.io
Wed Feb 22 12:50:00 GMT 2017


Add replacements to libeu.a if they don't exist. Include system.h
and link against libeu.a where they are used.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 ChangeLog              |  4 ++++
 configure.ac           | 10 ++++++++++
 lib/ChangeLog          | 10 ++++++++++
 lib/Makefile.am        |  8 ++++++++
 lib/asprintf.c         | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/color.c            |  1 +
 lib/system.h           |  9 +++++++++
 lib/vasprintf.c        | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 libdwfl/ChangeLog      |  4 ++++
 libdwfl/offline.c      |  1 +
 src/ChangeLog          |  5 +++++
 src/arlib-argp.c       |  1 +
 src/unstrip.c          |  1 +
 tests/ChangeLog        |  7 +++++++
 tests/Makefile.am      |  4 ++--
 tests/backtrace-data.c |  2 ++
 tests/backtrace.c      |  2 ++
 17 files changed, 162 insertions(+), 2 deletions(-)
 create mode 100644 lib/asprintf.c
 create mode 100644 lib/vasprintf.c

diff --git a/ChangeLog b/ChangeLog
index e97e02b..8e01ce2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* configure.ac: Add checks for asprintf and vasprintf.
+
 2017-02-20  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* configure.ac: Track which components are enabled and output
diff --git a/configure.ac b/configure.ac
index ed60be5..3d4bb70 100644
--- a/configure.ac
+++ b/configure.ac
@@ -290,6 +290,16 @@ AC_CHECK_DECLS([mempcpy],[],[],
                 #include <string.h>])
 AM_CONDITIONAL(HAVE_MEMPCPY, [test "x$ac_cv_have_decl_mempcpy" = "xyes"])
 
+AC_CHECK_DECLS([asprintf],[],[],
+               [#define _GNU_SOURCE
+                #include <stdio.h>])
+AM_CONDITIONAL(HAVE_ASPRINTF, [test "x$ac_cv_have_decl_asprintf" = "xyes"])
+
+AC_CHECK_DECLS([vasprintf],[],[],
+               [#define _GNU_SOURCE
+                #include <stdio.h>])
+AM_CONDITIONAL(HAVE_VASPRINTF, [test "x$ac_cv_have_decl_vasprintf" = "xyes"])
+
 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/lib/ChangeLog b/lib/ChangeLog
index c97537e..d0229ec 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,13 @@
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* Makefile.am (libeu_a_SOURCES): Add asprintf.c and vasprintf.c
+	if !HAVE_ASPRINTF and !HAVE_VASPRINTF, respectively.
+	* asprintf.c: New file.
+	* vasprintf.c: New file.
+	* color.c: Include system.h.
+	* system.h: Include stdarg.h, add declarations for asprintf and
+	vasprintf if they are not available from libc.
+
 2017-02-20  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* Makefile.am (libeu_a_SOURCES): Skip printversion.c and
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 93a965c..02e6bd9 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -44,6 +44,14 @@ if !HAVE_MEMPCPY
 libeu_a_SOURCES += mempcpy.c
 endif
 
+if !HAVE_ASPRINTF
+libeu_a_SOURCES += asprintf.c
+endif
+
+if !HAVE_VASPRINTF
+libeu_a_SOURCES += vasprintf.c
+endif
+
 if ARGP
 libeu_a_SOURCES += printversion.c color.c
 noinst_HEADERS += printversion.h color.h
diff --git a/lib/asprintf.c b/lib/asprintf.c
new file mode 100644
index 0000000..4e10f5c
--- /dev/null
+++ b/lib/asprintf.c
@@ -0,0 +1,46 @@
+/* Implementation of asprintf, using vasprintf
+   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 <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "system.h"
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+asprintf(char **strp, const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    int result = vasprintf(strp, fmt, ap);
+    va_end(ap);
+    return result;
+}
diff --git a/lib/color.c b/lib/color.c
index f62389d..3362f83 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -39,6 +39,7 @@
 #include <unistd.h>
 #include "libeu.h"
 #include "color.h"
+#include "system.h"
 
 /* Prototype for option handler.  */
 static error_t parse_opt (int key, char *arg, struct argp_state *state);
diff --git a/lib/system.h b/lib/system.h
index 6ddbb2d..b6f2269 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -40,6 +40,7 @@
 #include <endian.h>
 #include <byteswap.h>
 #include <unistd.h>
+#include <stdarg.h>
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 # define LE32(n)	(n)
@@ -71,6 +72,14 @@
 void *mempcpy(void *dest, const void *src, size_t n);
 #endif
 
+#if !HAVE_DECL_ASPRINTF
+int asprintf (char **strp, const char *fmt, ...);
+#endif
+
+#if !HAVE_DECL_VASPRINTF
+int vasprintf (char **strp, const char *fmt, va_list ap);
+#endif
+
 /* A special gettext function we use if the strings are too short.  */
 #define sgettext(Str) \
   ({ const char *__res = strrchr (gettext (Str), '|');			      \
diff --git a/lib/vasprintf.c b/lib/vasprintf.c
new file mode 100644
index 0000000..0f41fbc
--- /dev/null
+++ b/lib/vasprintf.c
@@ -0,0 +1,49 @@
+/* Implementation of vasprintf, using vsnprintf
+   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 <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "system.h"
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+vasprintf(char **strp, const char *fmt, va_list ap)
+{
+    va_list test;
+    va_copy(test, ap);
+    int length = vsnprintf(NULL, 0, fmt, test);
+    va_end(test);
+    *strp = malloc(length + 1);
+    if (*strp == NULL)
+        return -1;
+    return vsnprintf(*strp, length + 1, fmt, ap);
+}
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 570c66f..75358dc 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,7 @@
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* offline.c: Include system.h.
+
 2017-02-20  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* argp-dummy.c: New file.
diff --git a/libdwfl/offline.c b/libdwfl/offline.c
index c0a2599..24a9b6c 100644
--- a/libdwfl/offline.c
+++ b/libdwfl/offline.c
@@ -27,6 +27,7 @@
    not, see <http://www.gnu.org/licenses/>.  */
 
 #include "libdwflP.h"
+#include <system.h>
 #include <fcntl.h>
 #include <unistd.h>
 
diff --git a/src/ChangeLog b/src/ChangeLog
index d26169b..bdf04cc 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* arlib-argp.c: Include system.h.
+	* unstrip.c: Likewise.
+
 2017-02-17  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* elfcompress.c: Include system.h.
diff --git a/src/arlib-argp.c b/src/arlib-argp.c
index 1bdd8d0..b2847ac 100644
--- a/src/arlib-argp.c
+++ b/src/arlib-argp.c
@@ -21,6 +21,7 @@
 
 #include <argp.h>
 #include <libintl.h>
+#include <system.h>
 
 #include "arlib.h"
 
diff --git a/src/unstrip.c b/src/unstrip.c
index 6e57a6b..7a86ead 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -51,6 +51,7 @@
 #include "libdwelf.h"
 #include "libeu.h"
 #include "printversion.h"
+#include "system.h"
 
 #ifndef _
 # define _(str) gettext (str)
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 7b15ec5..0e7e28c 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,10 @@
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* Makefile.am (backtrace_LDADD): Link against libeu.a.
+	(backtrace_data_LDADD): Likewise.
+	* backtrace.c: Include system.h.
+	* backtrace-data.c: Likewise.
+
 2017-02-15  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* Makefile.am: Link asm tests and elfstrmerge against libeu.a.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 1dc85f9..5826703 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -465,12 +465,12 @@ dwflsyms_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 dwfllines_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 dwfl_report_elf_align_LDADD = $(libdw)
 varlocs_LDADD = $(libdw) $(libelf) $(argp_LDADD)
-backtrace_LDADD = $(libdw) $(libelf) $(argp_LDADD)
+backtrace_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD)
 # backtrace-child-biarch also uses those *_CFLAGS and *_LDLAGS variables:
 backtrace_child_CFLAGS = -fPIE
 backtrace_child_LDFLAGS = -pie -pthread
 backtrace_child_biarch_SOURCES = backtrace-child.c
-backtrace_data_LDADD = $(libdw) $(libelf)
+backtrace_data_LDADD = $(libdw) $(libelf) $(libeu)
 backtrace_dwarf_CFLAGS = -Wno-unused-parameter
 backtrace_dwarf_LDADD = $(libdw) $(libelf)
 debuglink_LDADD = $(libdw) $(libelf)
diff --git a/tests/backtrace-data.c b/tests/backtrace-data.c
index a387d8f..68508c0 100644
--- a/tests/backtrace-data.c
+++ b/tests/backtrace-data.c
@@ -42,6 +42,8 @@
 #include ELFUTILS_HEADER(dwfl)
 #endif
 
+#include <system.h>
+
 #if !defined(__x86_64__) || !defined(__linux__)
 
 int
diff --git a/tests/backtrace.c b/tests/backtrace.c
index 34a2ab0..e35563b 100644
--- a/tests/backtrace.c
+++ b/tests/backtrace.c
@@ -40,6 +40,8 @@
 #include ELFUTILS_HEADER(dwfl)
 #endif
 
+#include <system.h>
+
 #ifndef __linux__
 
 int
-- 
2.1.4



More information about the Elfutils-devel mailing list