* [PATCH] Do not depend on non-POSIX features
@ 2016-10-11 14:10 Akihiko Odaki
0 siblings, 0 replies; 3+ messages in thread
From: Akihiko Odaki @ 2016-10-11 14:10 UTC (permalink / raw)
To: elfutils-devel
[-- Attachment #1: Type: text/plain, Size: 5223 bytes --]
Signed-off-by: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
---
configure.ac | 3 +++
lib/fixedsizehash.h | 6 ++++++
lib/system.h | 9 +++++++++
libelf/elf_getarsym.c | 8 ++++++++
libelf/elf_strptr.c | 30 ++++++++++++++++++++++--------
libelf/libelfP.h | 4 ++++
6 files changed, 52 insertions(+), 8 deletions(-)
diff --git a/configure.ac b/configure.ac
index e5503f1..feb2ed5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -245,6 +245,9 @@ zip_LIBS="$LIBS"
LIBS="$save_LIBS"
AC_SUBST([zip_LIBS])
+AC_CHECK_DECLS([memrchr rawmemchr])
+AC_CHECK_DECLS(powerof2, , , [#include <sys/param.h>])
+
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/fixedsizehash.h b/lib/fixedsizehash.h
index 06ce6a2..762ebaa 100644
--- a/lib/fixedsizehash.h
+++ b/lib/fixedsizehash.h
@@ -35,7 +35,13 @@
#include <system.h>
+#ifdef __CONCAT
#define CONCAT(t1,t2) __CONCAT (t1,t2)
+#else
+#define STROF(t2) t2
+#define CONCAT_EXPANDED(t1,t2) t1 ## t2
+#define CONCAT(t1,t2) CONCAT_EXPANDED(t1,t2)
+#endif
/* Before including this file the following macros must be defined:
diff --git a/lib/system.h b/lib/system.h
index f31cfd0..72005d4 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -30,6 +30,7 @@
#define LIB_SYSTEM_H 1
#include <argp.h>
+#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <endian.h>
@@ -68,6 +69,14 @@ extern int crc32_file (int fd, uint32_t *resp);
#define gettext_noop(Str) Str
+#ifndef TEMP_FAILURE_RETRY
+#define TEMP_FAILURE_RETRY(expression) \
+ ({ ssize_t __res; \
+ do \
+ __res = expression; \
+ while (__res == -1 && errno == EINTR); \
+ __res; });
+#endif
static inline ssize_t __attribute__ ((unused))
pwrite_retry (int fd, const void *buf, size_t len, off_t off)
diff --git a/libelf/elf_getarsym.c b/libelf/elf_getarsym.c
index 65c67cc..d5f0ba4 100644
--- a/libelf/elf_getarsym.c
+++ b/libelf/elf_getarsym.c
@@ -297,7 +297,15 @@ elf_getarsym (Elf *elf, size_t *ptr)
arsym[cnt].as_off = (*u32)[cnt];
arsym[cnt].as_hash = _dl_elf_hash (str_data);
+#if HAVE_DECL_RAWMEMCHR
str_data = rawmemchr (str_data, '\0') + 1;
+#else
+ char c;
+ do {
+ c = *str_data;
+ str_data++;
+ } while (c);
+#endif
}
/* At the end a special entry. */
diff --git a/libelf/elf_strptr.c b/libelf/elf_strptr.c
index ea21045..e72a3a3 100644
--- a/libelf/elf_strptr.c
+++ b/libelf/elf_strptr.c
@@ -32,6 +32,7 @@
#endif
#include <libelf.h>
+#include <stdbool.h>
#include <stddef.h>
#include "libelfP.h"
@@ -52,6 +53,22 @@ get_zdata (Elf_Scn *strscn)
return zdata;
}
+static bool validate_str (const char *str, size_t from, size_t to)
+{
+#if HAVE_DECL_MEMRCHR
+ return memrchr (&str[from], '\0', to - from) != NULL;
+#else
+ do {
+ if (to <= from)
+ return false;
+
+ to--;
+ } while (str[to]);
+
+ return true;
+#endif
+}
+
char *
elf_strptr (Elf *elf, size_t idx, size_t offset)
{
@@ -166,8 +183,7 @@ elf_strptr (Elf *elf, size_t idx, size_t offset)
{
/* Make sure the string is NUL terminated. Start from the end,
which very likely is a NUL char. */
- if (likely (memrchr (&strscn->zdata_base[offset],
- '\0', sh_size - offset) != NULL))
+ if (likely (validate_str (strscn->zdata_base, offset, sh_size)))
result = &strscn->zdata_base[offset];
else
__libelf_seterrno (ELF_E_INVALID_INDEX);
@@ -185,8 +201,7 @@ elf_strptr (Elf *elf, size_t idx, size_t offset)
/* Make sure the string is NUL terminated. Start from the end,
which very likely is a NUL char. */
- if (likely (memrchr (&strscn->rawdata_base[offset],
- '\0', sh_size - offset) != NULL))
+ if (likely (validate_str (strscn->rawdata_base, offset, sh_size)))
result = &strscn->rawdata_base[offset];
else
__libelf_seterrno (ELF_E_INVALID_INDEX);
@@ -203,10 +218,9 @@ elf_strptr (Elf *elf, size_t idx, size_t offset)
{
/* Make sure the string is NUL terminated. Start from
the end, which very likely is a NUL char. */
- if (likely (memrchr ((char *) dl->data.d.d_buf
- + (offset - dl->data.d.d_off), '\0',
- (dl->data.d.d_size
- - (offset - dl->data.d.d_off))) != NULL))
+ if (likely (validate_str ((char *) dl->data.d.d_buf,
+ offset - dl->data.d.d_off,
+ dl->data.d.d_size)))
result = ((char *) dl->data.d.d_buf
+ (offset - dl->data.d.d_off));
else
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index 4459982..88e5c13 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -46,6 +46,10 @@
/* gettext helper macros. */
#define _(Str) dgettext ("elfutils", Str)
+#if !HAVE_DECL_POWEROF2
+#define powerof2(x) (((x) & ((x) - 1)) == 0)
+#endif
+
/* Helper Macros to write 32 bit and 64 bit functions. */
#define __elfw2_(Bits, Name) __elf##Bits##_##Name
--
2.10.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Do not depend on non-POSIX features
@ 2016-10-12 14:41 Mark Wielaard
0 siblings, 0 replies; 3+ messages in thread
From: Mark Wielaard @ 2016-10-12 14:41 UTC (permalink / raw)
To: elfutils-devel
[-- Attachment #1: Type: text/plain, Size: 1088 bytes --]
On Wed, 2016-10-12 at 16:22 +0200, Mark Wielaard wrote:
> I am no fan of redefining/open coding things which reasonable can be
> expected to be provided through normal GNU system headers/libs. But
> these seem fairly minor. However if there are larger changes needed we
> should probably pull in the relevant gnulib modules.
Hmmm, I remember why I don't like such conditional defines/code.
The configure check was wrong... It should be:
diff --git a/configure.ac b/configure.ac
index feb2ed5..5360447 100644
--- a/configure.ac
+++ b/configure.ac
@@ -245,7 +245,9 @@ zip_LIBS="$LIBS"
LIBS="$save_LIBS"
AC_SUBST([zip_LIBS])
-AC_CHECK_DECLS([memrchr rawmemchr])
+AC_CHECK_DECLS([memrchr, rawmemchr],[],[],
+ [#define _GNU_SOURCE
+ #include <string.h>])
AC_CHECK_DECLS(powerof2, , , [#include <sys/param.h>])
AC_CHECK_LIB([stdc++], [__cxa_demangle], [dnl
At least now we know the fallback code really works, but please
explicitly say what kind of environment you tested on (and which you
didn't) next time.
Thanks,
Mark
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Do not depend on non-POSIX features
@ 2016-10-12 14:22 Mark Wielaard
0 siblings, 0 replies; 3+ messages in thread
From: Mark Wielaard @ 2016-10-12 14:22 UTC (permalink / raw)
To: elfutils-devel
[-- Attachment #1: Type: text/plain, Size: 465 bytes --]
Hi,
I am no fan of redefining/open coding things which reasonable can be
expected to be provided through normal GNU system headers/libs. But
these seem fairly minor. However if there are larger changes needed we
should probably pull in the relevant gnulib modules.
I added ChangeLog entries and moved the powerof2 redefinition to
system.h since that is included by all files that use it. Does the
attached patch work on your setup?
Thanks,
Mark
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Do-not-depend-on-some-non-POSIX-features.patch --]
[-- Type: text/x-patch, Size: 6925 bytes --]
From 4e850b5e8618ac46bddb7c003a4168cb62f03f4f Mon Sep 17 00:00:00 2001
From: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
Date: Tue, 11 Oct 2016 23:10:07 +0900
Subject: [PATCH] Do not depend on some non-POSIX features.
Define/open code memrchr, rawmemchr, powerof2 and TEMP_FAILURE_RETRY if
not available through system headers.
Signed-off-by: Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
ChangeLog | 4 ++++
configure.ac | 3 +++
lib/ChangeLog | 7 +++++++
lib/fixedsizehash.h | 6 ++++++
lib/system.h | 16 ++++++++++++++++
libelf/ChangeLog | 8 ++++++++
libelf/elf_getarsym.c | 8 ++++++++
libelf/elf_strptr.c | 30 ++++++++++++++++++++++--------
8 files changed, 74 insertions(+), 8 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 07e8f57..8d61572 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2015-10-11 Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
+
+ * configure.ac: Add memrchr, rawmemchr and powerof2 checks.
+
2016-08-04 Mark Wielaard <mjw@redhat.com>
* configure.ac: Set version to 0.167.
diff --git a/configure.ac b/configure.ac
index e5503f1..feb2ed5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -245,6 +245,9 @@ zip_LIBS="$LIBS"
LIBS="$save_LIBS"
AC_SUBST([zip_LIBS])
+AC_CHECK_DECLS([memrchr rawmemchr])
+AC_CHECK_DECLS(powerof2, , , [#include <sys/param.h>])
+
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 88c71c9..afb18b1 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,12 @@
2015-10-11 Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
+ * fixedsizehash.h (CONCAT): Use __CONCAT when available.
+ * system.h: Include config.h and errno.h.
+ (powerof2): Define if not already defined.
+ (TEMP_FAILURE_RETRY): Define when not yet defined.
+
+2015-10-11 Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
+
* Makefile.am (noinst_HEADERS): Add libeu.h.
* color.c: Remove system.h include, add libeu.h include.
* crc32_file.c: Likewise.
diff --git a/lib/fixedsizehash.h b/lib/fixedsizehash.h
index 18921a4..dac2a5f 100644
--- a/lib/fixedsizehash.h
+++ b/lib/fixedsizehash.h
@@ -34,7 +34,13 @@
#include <system.h>
+#ifdef __CONCAT
#define CONCAT(t1,t2) __CONCAT (t1,t2)
+#else
+#define STROF(t2) t2
+#define CONCAT_EXPANDED(t1,t2) t1 ## t2
+#define CONCAT(t1,t2) CONCAT_EXPANDED(t1,t2)
+#endif
/* Before including this file the following macros must be defined:
diff --git a/lib/system.h b/lib/system.h
index ec387c3..3a3a073 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -29,7 +29,12 @@
#ifndef LIB_SYSTEM_H
#define LIB_SYSTEM_H 1
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
#include <argp.h>
+#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/param.h>
@@ -59,6 +64,9 @@
#define MIN(m, n) ((m) < (n) ? (m) : (n))
#endif
+#ifndef HAVE_DECL_POWEROF2
+#define powerof2(x) (((x) & ((x) - 1)) == 0)
+#endif
/* A special gettext function we use if the strings are too short. */
#define sgettext(Str) \
@@ -67,6 +75,14 @@
#define gettext_noop(Str) Str
+#ifndef TEMP_FAILURE_RETRY
+#define TEMP_FAILURE_RETRY(expression) \
+ ({ ssize_t __res; \
+ do \
+ __res = expression; \
+ while (__res == -1 && errno == EINTR); \
+ __res; });
+#endif
static inline ssize_t __attribute__ ((unused))
pwrite_retry (int fd, const void *buf, size_t len, off_t off)
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index cf672bf..35af786 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,5 +1,13 @@
2015-10-11 Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
+ * elf_getarsym.c (elf_getarsym): Open code rawmemchr when not
+ available.
+ * elf_strptr.c: Include stdbool.h.
+ (validate_str): New function.
+ (elf_strptr): Use validate_str instead of memrchr.
+
+2015-10-11 Akihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
+
* elf32_updatefile.c: Remove sys/param.h include.
* elf32_updatenull.c: Likewise. Add system.h include.
* elf_begin.c: Remove sys/param.h.
diff --git a/libelf/elf_getarsym.c b/libelf/elf_getarsym.c
index 65c67cc..d5f0ba4 100644
--- a/libelf/elf_getarsym.c
+++ b/libelf/elf_getarsym.c
@@ -297,7 +297,15 @@ elf_getarsym (Elf *elf, size_t *ptr)
arsym[cnt].as_off = (*u32)[cnt];
arsym[cnt].as_hash = _dl_elf_hash (str_data);
+#if HAVE_DECL_RAWMEMCHR
str_data = rawmemchr (str_data, '\0') + 1;
+#else
+ char c;
+ do {
+ c = *str_data;
+ str_data++;
+ } while (c);
+#endif
}
/* At the end a special entry. */
diff --git a/libelf/elf_strptr.c b/libelf/elf_strptr.c
index ea21045..e72a3a3 100644
--- a/libelf/elf_strptr.c
+++ b/libelf/elf_strptr.c
@@ -32,6 +32,7 @@
#endif
#include <libelf.h>
+#include <stdbool.h>
#include <stddef.h>
#include "libelfP.h"
@@ -52,6 +53,22 @@ get_zdata (Elf_Scn *strscn)
return zdata;
}
+static bool validate_str (const char *str, size_t from, size_t to)
+{
+#if HAVE_DECL_MEMRCHR
+ return memrchr (&str[from], '\0', to - from) != NULL;
+#else
+ do {
+ if (to <= from)
+ return false;
+
+ to--;
+ } while (str[to]);
+
+ return true;
+#endif
+}
+
char *
elf_strptr (Elf *elf, size_t idx, size_t offset)
{
@@ -166,8 +183,7 @@ elf_strptr (Elf *elf, size_t idx, size_t offset)
{
/* Make sure the string is NUL terminated. Start from the end,
which very likely is a NUL char. */
- if (likely (memrchr (&strscn->zdata_base[offset],
- '\0', sh_size - offset) != NULL))
+ if (likely (validate_str (strscn->zdata_base, offset, sh_size)))
result = &strscn->zdata_base[offset];
else
__libelf_seterrno (ELF_E_INVALID_INDEX);
@@ -185,8 +201,7 @@ elf_strptr (Elf *elf, size_t idx, size_t offset)
/* Make sure the string is NUL terminated. Start from the end,
which very likely is a NUL char. */
- if (likely (memrchr (&strscn->rawdata_base[offset],
- '\0', sh_size - offset) != NULL))
+ if (likely (validate_str (strscn->rawdata_base, offset, sh_size)))
result = &strscn->rawdata_base[offset];
else
__libelf_seterrno (ELF_E_INVALID_INDEX);
@@ -203,10 +218,9 @@ elf_strptr (Elf *elf, size_t idx, size_t offset)
{
/* Make sure the string is NUL terminated. Start from
the end, which very likely is a NUL char. */
- if (likely (memrchr ((char *) dl->data.d.d_buf
- + (offset - dl->data.d.d_off), '\0',
- (dl->data.d.d_size
- - (offset - dl->data.d.d_off))) != NULL))
+ if (likely (validate_str ((char *) dl->data.d.d_buf,
+ offset - dl->data.d.d_off,
+ dl->data.d.d_size)))
result = ((char *) dl->data.d.d_buf
+ (offset - dl->data.d.d_off));
else
--
1.8.3.1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-10-12 14:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-11 14:10 [PATCH] Do not depend on non-POSIX features Akihiko Odaki
2016-10-12 14:22 Mark Wielaard
2016-10-12 14:41 Mark Wielaard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).