From 207be05c44da5c4c31179a34660e176145ff8d4b Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Mon, 2 Jul 2018 09:52:23 +0800
Subject: [PATCH] build: Provide alternatives for glibc assumptions helps
 compiling it on musl

Upstream-Status: Pending
Signed-off-by: Khem Raj <raj.khem@gmail.com>

Rebase to 0.175

Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 Makefile.am                      |  2 +-
 lib/fixedsizehash.h              |  1 -
 lib/system.h                     | 12 +++++++++++-
 libdw/Makefile.am                |  3 ++-
 libdwfl/dwfl_build_id_find_elf.c |  1 +
 libdwfl/dwfl_error.c             |  4 +++-
 libdwfl/dwfl_module_getdwarf.c   |  1 +
 libdwfl/libdwfl_crc32_file.c     |  9 +++++++++
 libdwfl/linux-kernel-modules.c   |  1 +
 libelf/elf.h                     |  8 ++++++--
 libelf/libelf.h                  |  1 +
 libelf/libelfP.h                 |  1 +
 12 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 2ff444e..41f77df 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -28,7 +28,7 @@ pkginclude_HEADERS = version.h
 
 # Add doc back when we have some real content.
 SUBDIRS = config m4 lib libelf libebl libdwelf libdwfl libdw libcpu libasm \
-	  backends src po tests
+	  backends po tests
 
 EXTRA_DIST = elfutils.spec GPG-KEY NOTES CONTRIBUTING \
 	     COPYING COPYING-GPLV2 COPYING-LGPLV3
diff --git a/lib/fixedsizehash.h b/lib/fixedsizehash.h
index dac2a5f..43016fc 100644
--- a/lib/fixedsizehash.h
+++ b/lib/fixedsizehash.h
@@ -30,7 +30,6 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/cdefs.h>
 
 #include <system.h>
 
diff --git a/lib/system.h b/lib/system.h
index 292082b..308a762 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -30,7 +30,7 @@
 #define LIB_SYSTEM_H	1
 
 #include <errno.h>
-#include <error.h>
+#include <err.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/param.h>
@@ -51,6 +51,16 @@
 #else
 # error "Unknown byte order"
 #endif
+#ifndef TEMP_FAILURE_RETRY
+#define TEMP_FAILURE_RETRY(expression) \
+  (__extension__							      \
+    ({ long int __result;						      \
+       do __result = (long int) (expression);				      \
+       while (__result == -1L && errno == EINTR);			      \
+       __result; }))
+#endif
+
+#define error(status, errno, ...) err(status, __VA_ARGS__)
 
 #ifndef MAX
 #define MAX(m, n) ((m) < (n) ? (n) : (m))
diff --git a/libdw/Makefile.am b/libdw/Makefile.am
index 7a3d532..7ac1241 100644
--- a/libdw/Makefile.am
+++ b/libdw/Makefile.am
@@ -108,7 +108,8 @@ am_libdw_pic_a_OBJECTS = $(libdw_a_SOURCES:.c=.os)
 libdw_so_LIBS = libdw_pic.a ../libdwelf/libdwelf_pic.a \
 	  ../libdwfl/libdwfl_pic.a ../libebl/libebl.a
 libdw_so_DEPS = ../lib/libeu.a ../libelf/libelf.so
-libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(zip_LIBS)
+fts_LDADD = -lfts
+libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(zip_LIBS) $(fts_LDADD)
 libdw_so_SOURCES =
 libdw.so$(EXEEXT): $(srcdir)/libdw.map $(libdw_so_LIBS) $(libdw_so_DEPS)
 # The rpath is necessary for libebl because its $ORIGIN use will
diff --git a/libdwfl/dwfl_build_id_find_elf.c b/libdwfl/dwfl_build_id_find_elf.c
index cc6c3f6..b06ab59 100644
--- a/libdwfl/dwfl_build_id_find_elf.c
+++ b/libdwfl/dwfl_build_id_find_elf.c
@@ -31,6 +31,7 @@
 #endif
 
 #include "libdwflP.h"
+#include "system.h"
 #include <inttypes.h>
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/libdwfl/dwfl_error.c b/libdwfl/dwfl_error.c
index 7bcf61c..c345797 100644
--- a/libdwfl/dwfl_error.c
+++ b/libdwfl/dwfl_error.c
@@ -140,6 +140,7 @@ __libdwfl_seterrno (Dwfl_Error error)
 const char *
 dwfl_errmsg (int error)
 {
+  static __thread char s[64] = "";
   if (error == 0 || error == -1)
     {
       int last_error = global_error;
@@ -154,7 +155,8 @@ dwfl_errmsg (int error)
   switch (error &~ 0xffff)
     {
     case OTHER_ERROR (ERRNO):
-      return strerror_r (error & 0xffff, "bad", 0);
+      strerror_r (error & 0xffff, s, sizeof(s));
+      return s;
     case OTHER_ERROR (LIBELF):
       return elf_errmsg (error & 0xffff);
     case OTHER_ERROR (LIBDW):
diff --git a/libdwfl/dwfl_module_getdwarf.c b/libdwfl/dwfl_module_getdwarf.c
index 56e6105..f4a0649 100644
--- a/libdwfl/dwfl_module_getdwarf.c
+++ b/libdwfl/dwfl_module_getdwarf.c
@@ -35,6 +35,7 @@
 #include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 #include "../libdw/libdwP.h"	/* DWARF_E_* values are here.  */
 #include "../libelf/libelfP.h"
 #include "system.h"
diff --git a/libdwfl/libdwfl_crc32_file.c b/libdwfl/libdwfl_crc32_file.c
index f849128..6f0aca1 100644
--- a/libdwfl/libdwfl_crc32_file.c
+++ b/libdwfl/libdwfl_crc32_file.c
@@ -29,6 +29,15 @@
 # include <config.h>
 #endif
 
+#ifndef TEMP_FAILURE_RETRY
+#define TEMP_FAILURE_RETRY(expression) \
+  (__extension__                                                             \
+    ({ long int __result;                                                    \
+       do __result = (long int) (expression);                                \
+       while (__result == -1L && errno == EINTR);                            \
+       __result; }))
+#endif
+
 #define crc32_file attribute_hidden __libdwfl_crc32_file
 #define crc32 __libdwfl_crc32
 #include <libdwflP.h>
diff --git a/libdwfl/linux-kernel-modules.c b/libdwfl/linux-kernel-modules.c
index 360e4ee..b5aa397 100644
--- a/libdwfl/linux-kernel-modules.c
+++ b/libdwfl/linux-kernel-modules.c
@@ -41,6 +41,7 @@
 
 #include "libelfP.h"
 #include "libdwflP.h"
+#include "system.h"
 #include <inttypes.h>
 #include <errno.h>
 #include <stdio.h>
diff --git a/libelf/elf.h b/libelf/elf.h
index 5dc632b..14da1b7 100644
--- a/libelf/elf.h
+++ b/libelf/elf.h
@@ -21,7 +21,9 @@
 
 #include <features.h>
 
-__BEGIN_DECLS
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 /* Standard ELF types.  */
 
@@ -3937,6 +3939,8 @@ enum
 #define R_METAG_TLS_LE_HI16	60
 #define R_METAG_TLS_LE_LO16	61
 
-__END_DECLS
+#ifdef __cplusplus
+}
+#endif
 
 #endif	/* elf.h */
diff --git a/libelf/libelf.h b/libelf/libelf.h
index 1ff11c9..c21e018 100644
--- a/libelf/libelf.h
+++ b/libelf/libelf.h
@@ -29,6 +29,7 @@
 #ifndef _LIBELF_H
 #define _LIBELF_H 1
 
+#include <fcntl.h>
 #include <stdint.h>
 #include <sys/types.h>
 
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index 9f3e8e9..10a347a 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -32,6 +32,7 @@
 
 #include <ar.h>
 #include <gelf.h>
+#include <libelf.h>
 
 #include <errno.h>
 #include <stdbool.h>
-- 
2.7.4

