summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--NEWS5
-rw-r--r--configure.ac155
-rw-r--r--lib/ChangeLog11
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/eu-config.h198
-rw-r--r--libasm/Makefile.am7
-rw-r--r--libdw/ChangeLog5
-rw-r--r--libdw/libdw.map9
-rw-r--r--libdwfl/ChangeLog50
-rw-r--r--libdwfl/Makefile.am6
-rw-r--r--libdwfl/argp-std.c59
-rw-r--r--libdwfl/core-file.c456
-rw-r--r--libdwfl/derelocate.c3
-rw-r--r--libdwfl/dwfl_addrmodule.c32
-rw-r--r--libdwfl/dwfl_end.c23
-rw-r--r--libdwfl/dwfl_getdwarf.c38
-rw-r--r--libdwfl/dwfl_getmodules.c48
-rw-r--r--libdwfl/dwfl_module.c77
-rw-r--r--libdwfl/dwfl_module_addrsym.c44
-rw-r--r--libdwfl/dwfl_segment_report_module.c645
-rw-r--r--libdwfl/libdwfl.h45
-rw-r--r--libdwfl/libdwflP.h72
-rw-r--r--libdwfl/link_map.c867
-rw-r--r--libdwfl/segment.c300
-rw-r--r--libelf/ChangeLog104
-rw-r--r--libelf/common.h30
-rw-r--r--libelf/elf32_checksum.c2
-rw-r--r--libelf/elf32_getehdr.c16
-rw-r--r--libelf/elf32_getphdr.c27
-rw-r--r--libelf/elf32_getshdr.c340
-rw-r--r--libelf/elf32_newehdr.c4
-rw-r--r--libelf/elf32_newphdr.c4
-rw-r--r--libelf/elf32_offscn.c4
-rw-r--r--libelf/elf32_updatenull.c13
-rw-r--r--libelf/elf_begin.c4
-rw-r--r--libelf/elf_clone.c4
-rw-r--r--libelf/elf_cntl.c4
-rw-r--r--libelf/elf_end.c12
-rw-r--r--libelf/elf_getarsym.c4
-rw-r--r--libelf/elf_getdata.c49
-rw-r--r--libelf/elf_getscn.c4
-rw-r--r--libelf/elf_getshnum.c17
-rw-r--r--libelf/elf_getshstrndx.c4
-rw-r--r--libelf/elf_newdata.c4
-rw-r--r--libelf/elf_newscn.c4
-rw-r--r--libelf/elf_nextscn.c4
-rw-r--r--libelf/elf_rawdata.c2
-rw-r--r--libelf/elf_readall.c6
-rw-r--r--libelf/elf_strptr.c17
-rw-r--r--libelf/elf_update.c8
-rw-r--r--libelf/gelf_getauxv.c4
-rw-r--r--libelf/gelf_getdyn.c4
-rw-r--r--libelf/gelf_getehdr.c16
-rw-r--r--libelf/gelf_getlib.c4
-rw-r--r--libelf/gelf_getmove.c4
-rw-r--r--libelf/gelf_getnote.c4
-rw-r--r--libelf/gelf_getphdr.c17
-rw-r--r--libelf/gelf_getrel.c4
-rw-r--r--libelf/gelf_getrela.c4
-rw-r--r--libelf/gelf_getshdr.c8
-rw-r--r--libelf/gelf_getsym.c4
-rw-r--r--libelf/gelf_getsyminfo.c4
-rw-r--r--libelf/gelf_getsymshndx.c4
-rw-r--r--libelf/gelf_getverdaux.c4
-rw-r--r--libelf/gelf_getverdef.c4
-rw-r--r--libelf/gelf_getvernaux.c4
-rw-r--r--libelf/gelf_getverneed.c4
-rw-r--r--libelf/gelf_getversym.c4
-rw-r--r--libelf/gelf_update_auxv.c4
-rw-r--r--libelf/gelf_update_dyn.c4
-rw-r--r--libelf/gelf_update_ehdr.c4
-rw-r--r--libelf/gelf_update_lib.c4
-rw-r--r--libelf/gelf_update_move.c4
-rw-r--r--libelf/gelf_update_phdr.c8
-rw-r--r--libelf/gelf_update_rel.c4
-rw-r--r--libelf/gelf_update_rela.c4
-rw-r--r--libelf/gelf_update_shdr.c8
-rw-r--r--libelf/gelf_update_sym.c4
-rw-r--r--libelf/gelf_update_syminfo.c4
-rw-r--r--libelf/gelf_update_symshndx.c4
-rw-r--r--libelf/gelf_update_verdaux.c4
-rw-r--r--libelf/gelf_update_verdef.c4
-rw-r--r--libelf/gelf_update_vernaux.c4
-rw-r--r--libelf/gelf_update_verneed.c4
-rw-r--r--libelf/gelf_update_versym.c4
-rw-r--r--libelf/libelfP.h95
-rw-r--r--libelf/nlist.c2
-rw-r--r--tests/ChangeLog6
-rw-r--r--tests/Makefile.am3
-rwxr-xr-xtests/run-addrname-test.sh137
-rw-r--r--tests/testfile49.bz2bin0 -> 337 bytes
92 files changed, 3450 insertions, 827 deletions
diff --git a/ChangeLog b/ChangeLog
index fee15a49..01f61eb3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-08-25 Roland McGrath <roland@redhat.com>
+
+ * configure.ac (--enable-tls): Set AM_CONDITIONAL USE_TLS too.
+
+2008-08-21 Roland McGrath <roland@redhat.com>
+
+ * configure.ac (AH_BOTTOM): Emit #include <eu-config.h> and
+ move the contents to lib/eu-config.h instead of keeping them here.
+
2007-12-20 Ulrich Drepper <drepper@redhat.com>
* configure.ac: Add support for --enable-debugpred.
diff --git a/NEWS b/NEWS
index 1a8ce5ac..2b24a6e5 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,8 @@
+Version 0.136:
+
+libdwfl: bug fixes; new "segment" interfaces;
+ all the libdwfl-based tools now support --core=COREFILE option
+
Version 0.135:
libdwfl: bug fixes
diff --git a/configure.ac b/configure.ac
index 4008efda..a7a4cce2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -225,157 +225,8 @@ AC_CONFIG_FILES([tests/Makefile])
AC_SUBST(USE_NLS, yes)
AM_PO_SUBDIRS
-
-dnl Test of the config.h file. We hide all kinds of configuration magic
-dnl in there.
-AH_BOTTOM([
-#ifdef USE_TLS
-# include <pthread.h>
-# define tls_key_t __thread void *
-# define key_create(keyp, freefct) (1)
-# define getspecific(key) key
-# define setspecific(key,val) key = val
-# define once_define(class,name) class struct { } name
-# define once_execute(name,fct) ((void) &name, (void) (fct))
-# define rwlock_define(class,name) class pthread_rwlock_t name
-# define rwlock_init(lock) pthread_rwlock_init (&lock, NULL)
-# define rwlock_fini(lock) pthread_rwlock_destroy (&lock)
-# define rwlock_rdlock(lock) pthread_rwlock_rdlock (&lock)
-# define rwlock_wrlock(lock) pthread_rwlock_wrlock (&lock)
-# define rwlock_unlock(lock) pthread_rwlock_unlock (&lock)
-#else
-/* Eventually we will allow multi-threaded applications to use the
- libraries. Therefore we will add the necessary locking although
- the macros used expand to nothing for now. */
-#define lock_lock(lock) ((void) (lock))
-#define rwlock_define(class,name) class int name
-#define rwlock_init(lock) ((void) (lock))
-#define rwlock_fini(lock) ((void) (lock))
-#define rwlock_rdlock(lock) ((void) (lock))
-#define rwlock_wrlock(lock) ((void) (lock))
-#define rwlock_unlock(lock) ((void) (lock))
-#define tls_key_t void *
-#define key_create(keyp, freefct) (1)
-#define getspecific(key) key
-#define setspecific(key,val) key = val
-#define once_define(class,name) class int name
-#define once_execute(name,fct) \
- do { \
- if (name == 0) \
- fct (); \
- name = 1; \
- } while (0)
-#endif
-
-/* gettext helper macro. */
-#define N_(Str) Str
-
-/* Compiler-specific definitions. */
-#define strong_alias(name, aliasname) \
- extern __typeof (name) aliasname __attribute__ ((alias (#name)));
-
-#ifdef __i386__
-# define internal_function __attribute__ ((regparm (3), stdcall))
-#else
-# define internal_function /* nothing */
-#endif
-
-#define internal_strong_alias(name, aliasname) \
- extern __typeof (name) aliasname __attribute__ ((alias (#name))) internal_function;
-
-#define attribute_hidden \
- __attribute__ ((visibility ("hidden")))
-
-/* Define ALLOW_UNALIGNED if the architecture allows operations on
- unaligned memory locations. */
-#if defined __i386__ || defined __x86_64__
-# define ALLOW_UNALIGNED 1
-#else
-# define ALLOW_UNALIGNED 0
-#endif
-
-#if DEBUGPRED
-# ifdef __x86_64__
-asm (".section predict_data, \"aw\"; .previous\n"
- ".section predict_line, \"a\"; .previous\n"
- ".section predict_file, \"a\"; .previous");
-# ifndef PIC
-# define debugpred__(e, E) \
- ({ long int _e = !!(e); \
- asm volatile (".pushsection predict_data; ..predictcnt%=: .quad 0; .quad 0\n" \
- ".section predict_line; .quad %c1\n" \
- ".section predict_file; .quad %c2; .popsection\n" \
- "addq $1,..predictcnt%=(,%0,8)" \
- : : "r" (_e == E), "i" (__LINE__), "i" (__FILE__)); \
- __builtin_expect (_e, E); \
- })
-# endif
-# elif defined __i386__
-asm (".section predict_data, \"aw\"; .previous\n"
- ".section predict_line, \"a\"; .previous\n"
- ".section predict_file, \"a\"; .previous");
-# ifndef PIC
-# define debugpred__(e, E) \
- ({ long int _e = !!(e); \
- asm volatile (".pushsection predict_data; ..predictcnt%=: .long 0; .long 0\n" \
- ".section predict_line; .long %c1\n" \
- ".section predict_file; .long %c2; .popsection\n" \
- "incl ..predictcnt%=(,%0,8)" \
- : : "r" (_e == E), "i" (__LINE__), "i" (__FILE__)); \
- __builtin_expect (_e, E); \
- })
-# endif
-# endif
-# ifdef debugpred__
-# define unlikely(e) debugpred__ (e,0)
-# define likely(e) debugpred__ (e,1)
-# endif
-#endif
-#ifndef likely
-# define unlikely(expr) __builtin_expect (!!(expr), 0)
-# define likely(expr) __builtin_expect (!!(expr), 1)
-#endif
-
-#define obstack_calloc(ob, size) \
- ({ size_t _s = (size); memset (obstack_alloc (ob, _s), '\0', _s); })
-#define obstack_strdup(ob, str) \
- ({ const char *_s = (str); obstack_copy0 (ob, _s, strlen (_s)); })
-#define obstack_strndup(ob, str, n) \
- ({ const char *_s = (str); obstack_copy0 (ob, _s, strnlen (_s, n)); })
-
-#if __STDC_VERSION__ >= 199901L
-# define flexarr_size /* empty */
-#else
-# define flexarr_size 0
-#endif
-
-/* Calling conventions. */
-#ifdef __i386__
-# define CALLING_CONVENTION regparm (3), stdcall
-# define AND_CALLING_CONVENTION , regparm (3), stdcall
-#else
-# define CALLING_CONVENTION
-# define AND_CALLING_CONVENTION
-#endif
-
-/* Avoid PLT entries. */
-#ifdef PIC
-# define INTUSE(name) _INTUSE(name)
-# define _INTUSE(name) __##name##_internal
-# define INTDEF(name) _INTDEF(name)
-# define _INTDEF(name) \
- extern __typeof__ (name) __##name##_internal __attribute__ ((alias (#name)));
-# define INTDECL(name) _INTDECL(name)
-# define _INTDECL(name) \
- extern __typeof__ (name) __##name##_internal attribute_hidden;
-#else
-# define INTUSE(name) name
-# define INTDEF(name) /* empty */
-# define INTDECL(name) /* empty */
-#endif
-
-/* This macro is used by the tests conditionalize for standalone building. */
-#define ELFUTILS_HEADER(name) <lib##name.h>
-])
+dnl Appended to the config.h file.
+dnl We hide all kinds of configuration magic in lib/eu-config.h.
+AH_BOTTOM([#include <eu-config.h>])
AC_OUTPUT
diff --git a/lib/ChangeLog b/lib/ChangeLog
index c3cd0acf..f4c261ab 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,14 @@
+2008-08-25 Roland McGrath <roland@redhat.com>
+
+ * eu-config.h [USE_TLS] (RWLOCK_CALL): New macro.
+ (rwlock_init, rwlock_fini, rwlock_rdlock, rwlock_wrlock, rwlock_unlock):
+ Use it.
+
+2008-08-24 Roland McGrath <roland@redhat.com>
+
+ * eu-config.h: New file.
+ * Makefile.am (noinst_HEADERS): Add it.
+
2008-02-01 Ulrich Drepper <drepper@redhat.com>
* Makefile.am (libeu_a_SOURCES): Add sha1.c.
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7662c380..b95d3e3b 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -39,7 +39,7 @@ libeu_a_SOURCES = xstrndup.c xmalloc.c next_prime.c \
crc32.c crc32_file.c md5.c sha1.c
noinst_HEADERS = fixedsizehash.h system.h dynamicsizehash.h list.h md5.h \
- sha1.h
+ sha1.h eu-config.h
EXTRA_DIST = dynamicsizehash.c
if !GPROF
diff --git a/lib/eu-config.h b/lib/eu-config.h
new file mode 100644
index 00000000..c41cbb0c
--- /dev/null
+++ b/lib/eu-config.h
@@ -0,0 +1,198 @@
+/* Configuration definitions.
+ Copyright (C) 2008 Red Hat, Inc.
+ This file is part of Red Hat elfutils.
+
+ Red Hat elfutils is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 2 of the License.
+
+ Red Hat 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 a copy of the GNU General Public License along
+ with Red Hat elfutils; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
+
+ In addition, as a special exception, Red Hat, Inc. gives You the
+ additional right to link the code of Red Hat elfutils with code licensed
+ under an Open Source Initiative certified open source license
+ (http://www.opensource.org/licenses/index.php) and to distribute linked
+ combinations including the two. Non-GPL Code permitted under this
+ exception must only link to the code of Red Hat elfutils through those
+ well defined interfaces identified in the file named EXCEPTION found in
+ the source code files (the "Approved Interfaces"). The files of Non-GPL
+ Code may instantiate templates or use macros or inline functions from
+ the Approved Interfaces without causing the resulting work to be covered
+ by the GNU General Public License. Only Red Hat, Inc. may make changes
+ or additions to the list of Approved Interfaces. Red Hat's grant of
+ this exception is conditioned upon your not adding any new exceptions.
+ If you wish to add a new Approved Interface or exception, please contact
+ Red Hat. You must obey the GNU General Public License in all respects
+ for all of the Red Hat elfutils code and other code used in conjunction
+ with Red Hat elfutils except the Non-GPL Code covered by this exception.
+ If you modify this file, you may extend this exception to your version
+ of the file, but you are not obligated to do so. If you do not wish to
+ provide this exception without modification, you must delete this
+ exception statement from your version and license this file solely under
+ the GPL without exception.
+
+ Red Hat elfutils is an included package of the Open Invention Network.
+ An included package of the Open Invention Network is a package for which
+ Open Invention Network licensees cross-license their patents. No patent
+ license is granted, either expressly or impliedly, by designation as an
+ included package. Should you wish to participate in the Open Invention
+ Network licensing program, please visit www.openinventionnetwork.com
+ <http://www.openinventionnetwork.com>. */
+
+#ifdef USE_TLS
+# include <pthread.h>
+# include <assert.h>
+# define tls_key_t __thread void *
+# define key_create(keyp, freefct) (1)
+# define getspecific(key) key
+# define setspecific(key,val) key = val
+# define once_define(class,name) class struct { } name
+# define once_execute(name,fct) ((void) &name, (void) (fct))
+# define rwlock_define(class,name) class pthread_rwlock_t name
+# define RWLOCK_CALL(call) \
+ ({ int _err = pthread_rwlock_ ## call; assert_perror (_err); })
+# define rwlock_init(lock) RWLOCK_CALL (init (&lock, NULL))
+# define rwlock_fini(lock) RWLOCK_CALL (destroy (&lock))
+# define rwlock_rdlock(lock) RWLOCK_CALL (rdlock (&lock))
+# define rwlock_wrlock(lock) RWLOCK_CALL (wrlock (&lock))
+# define rwlock_unlock(lock) RWLOCK_CALL (unlock (&lock))
+#else
+/* Eventually we will allow multi-threaded applications to use the
+ libraries. Therefore we will add the necessary locking although
+ the macros used expand to nothing for now. */
+# define lock_lock(lock) ((void) (lock))
+# define rwlock_define(class,name) class int name
+# define rwlock_init(lock) ((void) (lock))
+# define rwlock_fini(lock) ((void) (lock))
+# define rwlock_rdlock(lock) ((void) (lock))
+# define rwlock_wrlock(lock) ((void) (lock))
+# define rwlock_unlock(lock) ((void) (lock))
+# define tls_key_t void *
+# define key_create(keyp, freefct) (1)
+# define getspecific(key) key
+# define setspecific(key,val) key = val
+# define once_define(class,name) class int name
+# define once_execute(name,fct) \
+ do { \
+ if (name == 0) \
+ fct (); \
+ name = 1; \
+ } while (0)
+#endif /* USE_TLS */
+
+/* gettext helper macro. */
+#define N_(Str) Str
+
+/* Compiler-specific definitions. */
+#define strong_alias(name, aliasname) \
+ extern __typeof (name) aliasname __attribute__ ((alias (#name)));
+
+#ifdef __i386__
+# define internal_function __attribute__ ((regparm (3), stdcall))
+#else
+# define internal_function /* nothing */
+#endif
+
+#define internal_strong_alias(name, aliasname) \
+ extern __typeof (name) aliasname __attribute__ ((alias (#name))) internal_function;
+
+#define attribute_hidden \
+ __attribute__ ((visibility ("hidden")))
+
+/* Define ALLOW_UNALIGNED if the architecture allows operations on
+ unaligned memory locations. */
+#if defined __i386__ || defined __x86_64__
+# define ALLOW_UNALIGNED 1
+#else
+# define ALLOW_UNALIGNED 0
+#endif
+
+#if DEBUGPRED
+# ifdef __x86_64__
+asm (".section predict_data, \"aw\"; .previous\n"
+ ".section predict_line, \"a\"; .previous\n"
+ ".section predict_file, \"a\"; .previous");
+# ifndef PIC
+# define debugpred__(e, E) \
+ ({ long int _e = !!(e); \
+ asm volatile (".pushsection predict_data; ..predictcnt%=: .quad 0; .quad 0\n" \
+ ".section predict_line; .quad %c1\n" \
+ ".section predict_file; .quad %c2; .popsection\n" \
+ "addq $1,..predictcnt%=(,%0,8)" \
+ : : "r" (_e == E), "i" (__LINE__), "i" (__FILE__)); \
+ __builtin_expect (_e, E); \
+ })
+# endif
+# elif defined __i386__
+asm (".section predict_data, \"aw\"; .previous\n"
+ ".section predict_line, \"a\"; .previous\n"
+ ".section predict_file, \"a\"; .previous");
+# ifndef PIC
+# define debugpred__(e, E) \
+ ({ long int _e = !!(e); \
+ asm volatile (".pushsection predict_data; ..predictcnt%=: .long 0; .long 0\n" \
+ ".section predict_line; .long %c1\n" \
+ ".section predict_file; .long %c2; .popsection\n" \
+ "incl ..predictcnt%=(,%0,8)" \
+ : : "r" (_e == E), "i" (__LINE__), "i" (__FILE__)); \
+ __builtin_expect (_e, E); \
+ })
+# endif
+# endif
+# ifdef debugpred__
+# define unlikely(e) debugpred__ (e,0)
+# define likely(e) debugpred__ (e,1)
+# endif
+#endif
+#ifndef likely
+# define unlikely(expr) __builtin_expect (!!(expr), 0)
+# define likely(expr) __builtin_expect (!!(expr), 1)
+#endif
+
+#define obstack_calloc(ob, size) \
+ ({ size_t _s = (size); memset (obstack_alloc (ob, _s), '\0', _s); })
+#define obstack_strdup(ob, str) \
+ ({ const char *_s = (str); obstack_copy0 (ob, _s, strlen (_s)); })
+#define obstack_strndup(ob, str, n) \
+ ({ const char *_s = (str); obstack_copy0 (ob, _s, strnlen (_s, n)); })
+
+#if __STDC_VERSION__ >= 199901L
+# define flexarr_size /* empty */
+#else
+# define flexarr_size 0
+#endif
+
+/* Calling conventions. */
+#ifdef __i386__
+# define CALLING_CONVENTION regparm (3), stdcall
+# define AND_CALLING_CONVENTION , regparm (3), stdcall
+#else
+# define CALLING_CONVENTION
+# define AND_CALLING_CONVENTION
+#endif
+
+/* Avoid PLT entries. */
+#ifdef PIC
+# define INTUSE(name) _INTUSE(name)
+# define _INTUSE(name) __##name##_internal
+# define INTDEF(name) _INTDEF(name)
+# define _INTDEF(name) \
+ extern __typeof__ (name) __##name##_internal __attribute__ ((alias (#name)));
+# define INTDECL(name) _INTDECL(name)
+# define _INTDECL(name) \
+ extern __typeof__ (name) __##name##_internal attribute_hidden;
+#else
+# define INTUSE(name) name
+# define INTDEF(name) /* empty */
+# define INTDECL(name) /* empty */
+#endif
+
+/* This macro is used by the tests conditionalize for standalone building. */
+#define ELFUTILS_HEADER(name) <lib##name.h>
diff --git a/libasm/Makefile.am b/libasm/Makefile.am
index 2b39eaef..bd5779e8 100644
--- a/libasm/Makefile.am
+++ b/libasm/Makefile.am
@@ -65,17 +65,12 @@ if !MUDFLAP
libasm_pic_a_SOURCES =
am_libasm_pic_a_OBJECTS = $(libasm_a_SOURCES:.c=.os)
-libasm_so_LDLIBS =
-if USE_TLS
-libasm_so_LDLIBS += -lpthread
-endif
-
libasm_so_SOURCES =
libasm.so: libasm_pic.a libasm.map
$(LINK) -shared -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
-Wl,--version-script,$(srcdir)/libasm.map,--no-undefined \
-Wl,--soname,$@.$(VERSION) \
- ../libebl/libebl.a ../libelf/libelf.so $(libasm_so_LDLIBS)
+ ../libebl/libebl.a ../libelf/libelf.so
if readelf -d $@ | fgrep -q TEXTREL; then exit 1; fi
ln -fs $@ $@.$(VERSION)
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index d344baef..235fac01 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,8 @@
+2008-08-15 Roland McGrath <roland@redhat.com>
+
+ * libdw.map (ELFUTILS_0.136): New version set, inherits from
+ ELFUTILS_0.130. Add dwfl_addrsegment, dwfl_report_segment.
+
2008-01-21 Ulrich Drepper <drepper@redhat.com>
* dwarf_child.c: Minor optimizations.
diff --git a/libdw/libdw.map b/libdw/libdw.map
index 8ef5f633..8e2f6441 100644
--- a/libdw/libdw.map
+++ b/libdw/libdw.map
@@ -176,3 +176,12 @@ ELFUTILS_0.130 {
local:
*;
} ELFUTILS_0.127;
+
+ELFUTILS_0.136 {
+ global:
+ dwfl_addrsegment;
+ dwfl_report_segment;
+
+ local:
+ *;
+} ELFUTILS_0.130;
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 231f8af4..c7f0c4b5 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,8 +1,50 @@
-2008-07-15 Petr Machata <pmachata@redhat.com>
+2008-08-21 Denys Vlasenko <dvlasenk@redhat.com>
- * linux-kernel-modules.c: #undef __USE_FILE_OFFSET64, it doesn't
- work with fts. It gets defined through <pthread.h> include in
- <config.h>.
+ * dwfl_module_addrsym.c (dwfl_module_addrsym): Improve logic
+ which decides which symbol is "closest" to a given address.
+
+2008-08-15 Roland McGrath <roland@redhat.com>
+
+ * argp-std.c (offline_callbacks): Use dwfl_build_id_find_elf.
+ (options, parse_opt): Handle --core.
+
+ * core-file.c: New file.
+ * Makefile.am (libdwfl_a_SOURCES): Add it.
+ * libdwflP.h (dwfl_core_file_report): Declare it.
+
+ * link_map.c: New file.
+ * Makefile.am (libdwfl_a_SOURCES): Add it.
+ * libdwflP.h (dwfl_link_map_report): Declare it.
+
+ * libdwflP.h (MIN, MAX): New macros.
+ (Dwfl_Memory_Callback): New typedef.
+ (Dwfl_Module_Callback): New typedef.
+ (dwfl_segment_report_module): Declare it.
+ * dwfl_segment_report_module.c: New file.
+ * Makefile.am (libdwfl_a_SOURCES): Add it.
+
+ * derelocate.c (dwfl_module_address_section): Add INTDEF.
+ * libdwflP.h: Add INTDECL.
+
+ * segment.c: New file.
+ * Makefile.am (libdwfl_a_SOURCES): Add it.
+ * libdwfl.h: Declare dwfl_addrsegment, dwfl_report_segment.
+ * libdwflP.h (struct Dwfl): New members lookup_elts, lookup_alloc,
+ lookup_addr, lookup_module, lookup_segndx, replace removed members
+ modules, nmodules.
+ (struct Dwfl_Module): New member segment.
+ * dwfl_end.c (dwfl_end): Free the new ones. Iterate via modulelist
+ to each free module.
+ * dwfl_module.c (dwfl_report_begin_add): Do nothing.
+ (dwfl_report_begin): Don't call it. Truncate the segment table instead.
+ (dwfl_report_module): Don't touch DWFL->nmodules.
+ (dwfl_report_end): Don't touch DWFL->modules and DWFL->nmodules.
+ (compare_modules): Function removed.
+ * dwfl_getmodules.c: Rewritten.
+ Add INTDEF.
+ * libdwflP.h: Add INTDECLs.
+ * dwfl_getdwarf.c: Rewritten to call dwfl_getmodules.
+ * dwfl_addrmodule.c: Rewritten to just call dwfl_addrsegment.
2008-08-03 Roland McGrath <roland@redhat.com>
diff --git a/libdwfl/Makefile.am b/libdwfl/Makefile.am
index 03719937..db14db2a 100644
--- a/libdwfl/Makefile.am
+++ b/libdwfl/Makefile.am
@@ -50,7 +50,7 @@ pkginclude_HEADERS = libdwfl.h
libdwfl_a_SOURCES = dwfl_begin.c dwfl_end.c dwfl_error.c dwfl_version.c \
dwfl_module.c dwfl_report_elf.c relocate.c \
dwfl_module_build_id.c dwfl_module_report_build_id.c \
- derelocate.c offline.c \
+ derelocate.c offline.c segment.c \
dwfl_module_info.c dwfl_getmodules.c \
dwfl_module_getdwarf.c dwfl_getdwarf.c \
dwfl_validate_address.c \
@@ -71,7 +71,9 @@ libdwfl_a_SOURCES = dwfl_begin.c dwfl_end.c dwfl_error.c dwfl_version.c \
dwfl_module_getsym.c \
dwfl_module_addrname.c dwfl_module_addrsym.c \
dwfl_module_return_value_location.c \
- dwfl_module_register_names.c
+ dwfl_module_register_names.c \
+ dwfl_segment_report_module.c \
+ link_map.c core-file.c
if MUDFLAP
diff --git a/libdwfl/argp-std.c b/libdwfl/argp-std.c
index 1abb568f..0a0f7ebf 100644
--- a/libdwfl/argp-std.c
+++ b/libdwfl/argp-std.c
@@ -52,17 +52,22 @@
#include <stdlib.h>
#include <assert.h>
#include <libintl.h>
+#include <fcntl.h>
+#include <unistd.h>
/* gettext helper macros. */
#define _(Str) dgettext ("elfutils", Str)
-#define OPT_DEBUGINFO 0x100
+#define OPT_DEBUGINFO 0x100
+#define OPT_COREFILE 0x101
static const struct argp_option options[] =
{
{ NULL, 0, NULL, 0, N_("Input selection options:"), 0 },
{ "executable", 'e', "FILE", 0, N_("Find addresses in FILE"), 0 },
+ { "core", OPT_COREFILE, "COREFILE", 0,
+ N_("Find addresses from signatures found in COREFILE"), 0 },
{ "pid", 'p', "PID", 0,
N_("Find addresses in files mapped into process PID"), 0 },
{ "linux-process-map", 'M', "FILE", 0,
@@ -84,6 +89,9 @@ static const Dwfl_Callbacks offline_callbacks =
.debuginfo_path = &debuginfo_path,
.section_address = INTUSE(dwfl_offline_section_address),
+
+ /* We use this table for core files too. */
+ .find_elf = INTUSE(dwfl_build_id_find_elf),
};
static const Dwfl_Callbacks proc_callbacks =
@@ -151,8 +159,8 @@ parse_opt (int key, char *arg, struct argp_state *state)
else
{
toomany:
- argp_error (state,
- "%s", _("only one of -e, -p, -k, or -K allowed"));
+ argp_error (state, "%s",
+ _("only one of -e, -p, -k, -K, or --core allowed"));
return EINVAL;
}
}
@@ -176,6 +184,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
{
FILE *f = fopen (arg, "r");
if (f == NULL)
+ nofile:
{
int code = errno;
argp_failure (state, EXIT_FAILURE, code,
@@ -193,6 +202,50 @@ parse_opt (int key, char *arg, struct argp_state *state)
goto toomany;
break;
+ case OPT_COREFILE:
+ {
+ Dwfl *dwfl = state->hook;
+ if (dwfl == NULL)
+ state->hook = dwfl = INTUSE(dwfl_begin) (&offline_callbacks);
+ /* Permit -e and --core together. */
+ else if (dwfl->callbacks != &offline_callbacks)
+ goto toomany;
+
+ int fd = open64 (arg, O_RDONLY);
+ if (fd < 0)
+ goto nofile;
+
+ Elf *core = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
+ if (core == NULL)
+ {
+ close (fd);
+ argp_failure (state, EXIT_FAILURE, 0,
+ _("cannot read ELF core file: %s"),
+ elf_errmsg (-1));
+ return EIO;
+ }
+
+ GElf_Ehdr ehdr;
+ int result = INTUSE(dwfl_core_file_report) (dwfl, core,
+ gelf_getehdr (core, &ehdr));
+ if (result < 0)
+ {
+ elf_end (core);
+ close (fd);
+ return fail (dwfl, result, arg);
+ }
+
+ /* From now we leak FD and CORE. */
+
+ if (result == 0)
+ {
+ argp_failure (state, EXIT_FAILURE, 0,
+ _("No modules recognized in core file"));
+ return ENOENT;
+ }
+ }
+ break;
+
case 'k':
if (state->hook == NULL)
{
diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c
new file mode 100644
index 00000000..bc881eb9
--- /dev/null
+++ b/libdwfl/core-file.c
@@ -0,0 +1,456 @@
+/* Core file handling.
+ Copyright (C) 2008 Red Hat, Inc.
+ This file is part of Red Hat elfutils.
+
+ Red Hat elfutils is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 2 of the License.
+
+ Red Hat 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 a copy of the GNU General Public License along
+ with Red Hat elfutils; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
+
+ In addition, as a special exception, Red Hat, Inc. gives You the
+ additional right to link the code of Red Hat elfutils with code licensed
+ under any Open Source Initiative certified open source license
+ (http://www.opensource.org/licenses/index.php) which requires the
+ distribution of source code with any binary distribution and to
+ distribute linked combinations of the two. Non-GPL Code permitted under
+ this exception must only link to the code of Red Hat elfutils through
+ those well defined interfaces identified in the file named EXCEPTION
+ found in the source code files (the "Approved Interfaces"). The files
+ of Non-GPL Code may instantiate templates or use macros or inline
+ functions from the Approved Interfaces without causing the resulting
+ work to be covered by the GNU General Public License. Only Red Hat,
+ Inc. may make changes or additions to the list of Approved Interfaces.
+ Red Hat's grant of this exception is conditioned upon your not adding
+ any new exceptions. If you wish to add a new Approved Interface or
+ exception, please contact Red Hat. You must obey the GNU General Public
+ License in all respects for all of the Red Hat elfutils code and other
+ code used in conjunction with Red Hat elfutils except the Non-GPL Code
+ covered by this exception. If you modify this file, you may extend this
+ exception to your version of the file, but you are not obligated to do
+ so. If you do not wish to provide this exception without modification,
+ you must delete this exception statement from your version and license
+ this file solely under the GPL without exception.
+
+ Red Hat elfutils is an included package of the Open Invention Network.
+ An included package of the Open Invention Network is a package for which
+ Open Invention Network licensees cross-license their patents. No patent
+ license is granted, either expressly or impliedly, by designation as an
+ included package. Should you wish to participate in the Open Invention
+ Network licensing program, please visit www.openinventionnetwork.com
+ <http://www.openinventionnetwork.com>. */
+
+#include <config.h>
+#include "../libelf/libelfP.h" /* For NOTE_ALIGN. */
+#undef _
+#include "libdwflP.h"
+#include <gelf.h>
+
+#include <sys/param.h>
+#include <unistd.h>
+#include <endian.h>
+#include <byteswap.h>
+#include "system.h"
+
+
+/* This is a prototype of what a new libelf interface might be.
+ This implementation is pessimal for non-mmap cases and should
+ be replaced by more diddling inside libelf internals. */
+static Elf *
+elf_begin_rand (Elf *parent, loff_t offset, loff_t size, loff_t *next)
+{
+ if (parent == NULL)
+ return NULL;
+
+ /* On failure return, we update *NEXT to point back at OFFSET. */
+ inline Elf *fail (int error)
+ {
+ if (next != NULL)
+ *next = offset;
+ //__libelf_seterrno (error);
+ __libdwfl_seterrno (DWFL_E (LIBELF, error));
+ return NULL;
+ }
+
+ loff_t min = (parent->kind == ELF_K_ELF ?
+ (parent->class == ELFCLASS32
+ ? sizeof (Elf32_Ehdr) : sizeof (Elf64_Ehdr))
+ : parent->kind == ELF_K_AR ? SARMAG
+ : 0);
+
+ if (unlikely (offset < min)
+ || unlikely (offset >= (loff_t) parent->maximum_size))
+ return fail (ELF_E_RANGE);
+
+ /* For an archive, fetch just the size field
+ from the archive header to override SIZE. */
+ if (parent->kind == ELF_K_AR)
+ {
+ struct ar_hdr h = { .ar_size = "" };
+
+ if (unlikely (parent->maximum_size - offset < sizeof h))
+ return fail (ELF_E_RANGE);
+
+ if (parent->map_address != NULL)
+ memcpy (h.ar_size, parent->map_address + parent->start_offset + offset,
+ sizeof h.ar_size);
+ else if (unlikely (pread_retry (parent->fildes,
+ h.ar_size, sizeof (h.ar_size),
+ parent->start_offset + offset
+ + offsetof (struct ar_hdr, ar_size))
+ != sizeof (h.ar_size)))
+ return fail (ELF_E_READ_ERROR);
+
+ offset += sizeof h;
+
+ char *endp;
+ size = strtoll (h.ar_size, &endp, 10);
+ if (unlikely (endp == h.ar_size)
+ || unlikely ((loff_t) parent->maximum_size - offset < size))
+ return fail (ELF_E_INVALID_ARCHIVE);
+ }
+
+ if (unlikely ((loff_t) parent->maximum_size - offset < size))
+ return fail (ELF_E_RANGE);
+
+ /* Even if we fail at this point, update *NEXT to point past the file. */
+ if (next != NULL)
+ *next = offset + size;
+
+ if (unlikely (offset == 0)
+ && unlikely (size == (loff_t) parent->maximum_size))
+ return elf_clone (parent, parent->cmd);
+
+ /* Note the image is guaranteed live only as long as PARENT
+ lives. Using elf_memory is quite suboptimal if the whole
+ file is not mmap'd. We really should have something like
+ a generalization of the archive support. */
+ Elf_Data *data = elf_getdata_rawchunk (parent, offset, size, ELF_T_BYTE);
+ if (data == NULL)
+ return NULL;
+ assert ((loff_t) data->d_size == size);
+ return elf_memory (data->d_buf, size);
+}
+
+
+int
+dwfl_report_core_segments (Dwfl *dwfl, Elf *elf, const GElf_Ehdr *ehdr,
+ GElf_Phdr *notes)
+{
+ if (unlikely (dwfl == NULL))
+ return -1;
+
+ if (unlikely (elf == NULL) || unlikely (ehdr == NULL))
+ {
+ __libdw_seterrno (DWFL_E_LIBELF);
+ return -1;
+ }
+
+ int result = 0;
+
+ if (notes != NULL)
+ notes->p_type = PT_NULL;
+
+ for (int ndx = 0; result >= 0 && ndx < ehdr->e_phnum; ++ndx)
+ {
+ GElf_Phdr phdr_mem;
+ GElf_Phdr *phdr = gelf_getphdr (elf, ndx, &phdr_mem);
+ if (unlikely (phdr == NULL))
+ {
+ __libdwfl_seterrno (DWFL_E_LIBELF);
+ return -1;
+ }
+ switch (phdr->p_type)
+ {
+ case PT_LOAD:
+ result = dwfl_report_segment (dwfl, ndx, phdr, 0, NULL);
+ break;
+
+ case PT_NOTE:
+ if (notes != NULL)
+ {
+ *notes = *phdr;
+ notes = NULL;
+ }
+ break;
+ }
+ }
+
+ return result;
+}
+
+/* Never read more than this much without mmap. */
+#define MAX_EAGER_COST 8192
+
+static bool
+core_file_read_eagerly (Dwfl_Module *mod,
+ void **userdata __attribute__ ((unused)),
+ const char *name __attribute__ ((unused)),
+ Dwarf_Addr start __attribute__ ((unused)),
+ void **buffer, size_t *buffer_available,
+ GElf_Off cost, GElf_Off worthwhile,
+ GElf_Off whole,
+ GElf_Off contiguous __attribute__ ((unused)),
+ void *arg, Elf **elfp)
+{
+ Elf *core = arg;
+
+ if (whole <= *buffer_available)
+ {
+ /* All there ever was, we already have on hand. */
+
+ if (core->map_address == NULL)
+ {
+ /* We already malloc'd the buffer. */
+ *elfp = elf_memory (*buffer, whole);
+ if (unlikely (*elfp == NULL))
+ return false;
+
+ (*elfp)->flags |= ELF_F_MALLOCED;
+ *buffer = NULL;
+ *buffer_available = 0;
+ return true;
+ }
+
+ /* We can use the image inside the core file directly. */
+ *elfp = elf_begin_rand (core, *buffer - core->map_address, whole, NULL);
+ *buffer = NULL;
+ *buffer_available = 0;
+ return *elfp != NULL;
+ }
+
+ /* We don't have the whole file.
+ Figure out if this is better than nothing. */
+
+ if (worthwhile == 0)
+ /* Caller doesn't think so. */
+ return false;
+
+ /*
+ XXX would like to fall back to partial file via memory
+ when build id find_elf fails
+ also, link_map name may give file name from disk better than partial here
+ requires find_elf hook re-doing the magic to fall back if no file found
+ */
+
+ if (mod->build_id_len > 0)
+ /* There is a build ID that could help us find the whole file,
+ which might be more useful than what we have.
+ We'll just rely on that. */
+ return false;
+
+ if (core->map_address != NULL)
+ /* It's cheap to get, so get it. */
+ return true;
+
+ /* Only use it if there isn't too much to be read. */
+ return cost <= MAX_EAGER_COST;
+}
+
+bool
+dwfl_elf_phdr_memory_callback (Dwfl *dwfl, int ndx,
+ void **buffer, size_t *buffer_available,
+ GElf_Addr vaddr,
+ size_t minread,
+ void *arg)
+{
+ Elf *elf = arg;
+
+ if (ndx == -1)
+ {
+ /* Called for cleanup. */
+ if (elf->map_address == NULL)
+ free (*buffer);
+ *buffer = NULL;
+ *buffer_available = 0;
+ return false;
+ }
+
+ const GElf_Off align = dwfl->segment_align ?: 1;
+ GElf_Phdr phdr;
+
+ do
+ if (unlikely (gelf_getphdr (elf, ndx++, &phdr) == NULL))
+ return true;
+ while (phdr.p_type != PT_LOAD
+ || ((phdr.p_vaddr + phdr.p_memsz + align - 1) & -align) <= vaddr);
+
+ GElf_Off start = vaddr - phdr.p_vaddr + phdr.p_offset;
+ GElf_Off end = (phdr.p_offset + phdr.p_filesz + align - 1) & -align;
+
+ /* Use following contiguous segments to get towards SIZE. */
+ inline bool more (size_t size)
+ {
+ while (end <= start || end - start < size)
+ {
+ if (phdr.p_filesz < phdr.p_memsz)
+ /* This segment is truncated, so no following one helps us. */
+ return false;
+
+ if (unlikely (gelf_getphdr (elf, ndx++, &phdr) == NULL))
+ return false;
+
+ if (phdr.p_type == PT_LOAD)
+ {
+ if (phdr.p_offset > end)
+ /* It's discontiguous! */
+ return false;
+
+ end = (phdr.p_offset + phdr.p_filesz + align - 1) & -align;
+ }
+ }
+ return true;
+ }
+
+ /* We need at least this much. */
+ if (! more (minread))
+ return false;
+
+ /* See how much more we can get of what the caller wants. */
+ (void) more (*buffer_available);
+
+ /* If it's already on hand anyway, use as much as there is. */
+ if (elf->map_address != NULL)
+ (void) more (elf->maximum_size - start);
+
+ if (unlikely (end - start > elf->maximum_size))
+ end = start + elf->maximum_size;
+
+ if (elf->map_address != NULL)
+ {
+ void *contents = elf->map_address + elf->start_offset + start;
+ size_t size = end - start;
+
+ if (minread == 0) /* String mode. */
+ {
+ const void *eos = memchr (contents, '\0', size);
+ if (unlikely (eos == NULL) || unlikely (eos == contents))
+ return false;
+ size = eos + 1 - contents;
+ }
+
+ if (*buffer == NULL)
+ {
+ *buffer = contents;
+ *buffer_available = size;
+ }
+ else
+ {
+ *buffer_available = MIN (size, *buffer_available);
+ memcpy (*buffer, contents, *buffer_available);
+ }
+ }
+ else
+ {
+ void *into = *buffer;
+ if (*buffer == NULL)
+ {
+ *buffer_available = MIN (minread ?: 512,
+ MAX (4096, MIN (end - start,
+ *buffer_available)));
+ into = malloc (*buffer_available);
+ if (unlikely (into == NULL))
+ {
+ __libdwfl_seterrno (DWFL_E_NOMEM);
+ return false;
+ }
+ }
+
+ ssize_t nread = pread_retry (elf->fildes, into, *buffer_available, start);
+ if (nread < (ssize_t) minread)
+ {
+ if (into != *buffer)
+ free (into);
+ if (nread < 0)
+ __libdwfl_seterrno (DWFL_E_ERRNO);
+ return false;
+ }
+
+ if (minread == 0) /* String mode. */
+ {
+ const void *eos = memchr (into, '\0', nread);
+ if (unlikely (eos == NULL) || unlikely (eos == into))
+ {
+ if (*buffer == NULL)
+ free (into);
+ return false;
+ }
+ nread = eos + 1 - into;
+ }
+
+ if (*buffer == NULL)
+ *buffer = into;
+ *buffer_available = nread;
+ }
+
+ return true;
+}
+
+int
+dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const GElf_Ehdr *ehdr)
+{
+ GElf_Phdr notes_phdr;
+
+ /* First report each PT_LOAD segment. */
+ int ndx = dwfl_report_core_segments (dwfl, elf, ehdr, &notes_phdr);
+ if (unlikely (ndx <= 0))
+ return ndx;
+
+ /* Now sniff segment contents for modules. */
+ ndx = 0;
+ do
+ {
+ int seg = dwfl_segment_report_module (dwfl, ndx, NULL,
+ &dwfl_elf_phdr_memory_callback, elf,
+ core_file_read_eagerly, elf);
+ if (unlikely (seg < 0))
+ return seg;
+ ndx = seg > ndx ? seg : ndx + 1;
+ }
+ while (ndx < ehdr->e_phnum);
+
+ /* Next, we should follow the chain from DT_DEBUG. */
+
+ const void *auxv = NULL;
+ size_t auxv_size = 0;
+ if (likely (notes_phdr.p_type == PT_NOTE))
+ {
+ /* PT_NOTE -> NT_AUXV -> AT_PHDR -> PT_DYNAMIC -> DT_DEBUG */
+
+ Elf_Data *notes = elf_getdata_rawchunk (elf,
+ notes_phdr.p_offset,
+ notes_phdr.p_filesz,
+ ELF_T_NHDR);
+ if (likely (notes != NULL))
+ {
+ size_t pos = 0;
+ GElf_Nhdr nhdr;
+ size_t name_pos;
+ size_t desc_pos;
+ while ((pos = gelf_getnote (notes, pos, &nhdr,
+ &name_pos, &desc_pos)) > 0)
+ if (nhdr.n_type == NT_AUXV
+ && nhdr.n_namesz == sizeof "CORE"
+ && !memcmp (notes->d_buf + name_pos, "CORE", sizeof "CORE"))
+ {
+ auxv = notes->d_buf + desc_pos;
+ auxv_size = nhdr.n_descsz;
+ break;
+ }
+ }
+ }
+
+ /* Now we have NT_AUXV contents. From here on this processing could be
+ used for a live process with auxv read from /proc. */
+
+ return dwfl_link_map_report (dwfl, auxv, auxv_size,
+ dwfl_elf_phdr_memory_callback, elf);
+}
+INTDEF (dwfl_core_file_report)
diff --git a/libdwfl/derelocate.c b/libdwfl/derelocate.c
index 0877276d..402bc06f 100644
--- a/libdwfl/derelocate.c
+++ b/libdwfl/derelocate.c
@@ -1,5 +1,5 @@
/* Recover relocatibility for addresses computed from debug information.
- Copyright (C) 2005, 2006, 2007 Red Hat, Inc.
+ Copyright (C) 2005, 2006, 2007, 2008 Red Hat, Inc.
This file is part of Red Hat elfutils.
Red Hat elfutils is free software; you can redistribute it and/or modify
@@ -397,3 +397,4 @@ dwfl_module_address_section (Dwfl_Module *mod, Dwarf_Addr *address,
*bias = mod->main.bias;
return mod->reloc_info->refs[idx].scn;
}
+INTDEF (dwfl_module_address_section)
diff --git a/libdwfl/dwfl_addrmodule.c b/libdwfl/dwfl_addrmodule.c
index 54d9a3d9..64559436 100644
--- a/libdwfl/dwfl_addrmodule.c
+++ b/libdwfl/dwfl_addrmodule.c
@@ -1,5 +1,5 @@
/* Find module containing address.
- Copyright (C) 2005, 2006, 2007 Red Hat, Inc.
+ Copyright (C) 2005, 2006, 2007, 2008 Red Hat, Inc.
This file is part of Red Hat elfutils.
Red Hat elfutils is free software; you can redistribute it and/or modify
@@ -52,32 +52,8 @@
Dwfl_Module *
dwfl_addrmodule (Dwfl *dwfl, Dwarf_Addr address)
{
- if (dwfl == NULL || dwfl->modules == NULL)
- return NULL;
-
- Dwfl_Module *boundary = NULL;
-
- /* Do binary search on the array indexed by module load address. */
- size_t l = 0, u = dwfl->nmodules;
- while (l < u)
- {
- size_t idx = (l + u) / 2;
- Dwfl_Module *m = dwfl->modules[idx];
- if (address < m->low_addr)
- u = idx;
- else if (address >= m->high_addr)
- {
- l = idx + 1;
- if (address == m->high_addr)
- /* If the high bound of this module is not the low bound of
- another, then consider the boundary address to be inside
- this module. */
- boundary = m;
- }
- else
- return m;
- }
-
- return boundary;
+ Dwfl_Module *mod;
+ (void) INTUSE(dwfl_addrsegment) (dwfl, address, &mod);
+ return mod;
}
INTDEF (dwfl_addrmodule)
diff --git a/libdwfl/dwfl_end.c b/libdwfl/dwfl_end.c
index 4bd40052..429abb13 100644
--- a/libdwfl/dwfl_end.c
+++ b/libdwfl/dwfl_end.c
@@ -52,15 +52,20 @@
void
dwfl_end (Dwfl *dwfl)
{
- if (dwfl != NULL)
+ if (dwfl == NULL)
+ return;
+
+ free (dwfl->lookup_addr);
+ free (dwfl->lookup_module);
+ free (dwfl->lookup_segndx);
+
+ Dwfl_Module *next = dwfl->modulelist;
+ while (next != NULL)
{
- while (dwfl->modulelist != NULL)
- {
- Dwfl_Module *mod = dwfl->modulelist;
- dwfl->modulelist = mod->next;
- __libdwfl_module_free (mod);
- }
- free (dwfl->modules);
- free (dwfl);
+ Dwfl_Module *dead = next;
+ next = dead->next;
+ __libdwfl_module_free (dead);
}
+
+ free (dwfl);
}
diff --git a/libdwfl/dwfl_getdwarf.c b/libdwfl/dwfl_getdwarf.c
index e1c1aa76..0a0656f7 100644
--- a/libdwfl/dwfl_getdwarf.c
+++ b/libdwfl/dwfl_getdwarf.c
@@ -1,5 +1,5 @@
/* Iterate through modules to fetch Dwarf information.
- Copyright (C) 2005 Red Hat, Inc.
+ Copyright (C) 2005, 2008 Red Hat, Inc.
This file is part of Red Hat elfutils.
Red Hat elfutils is free software; you can redistribute it and/or modify
@@ -49,6 +49,24 @@
#include "libdwflP.h"
+struct module_callback_info
+{
+ int (*callback) (Dwfl_Module *, void **,
+ const char *, Dwarf_Addr,
+ Dwarf *, Dwarf_Addr, void *);
+ void *arg;
+};
+
+static int
+module_callback (Dwfl_Module *mod, void **userdata,
+ const char *name, Dwarf_Addr start, void *arg)
+{
+ const struct module_callback_info *info = arg;
+ Dwarf_Addr bias = 0;
+ Dwarf *dw = INTUSE(dwfl_module_getdwarf) (mod, &bias);
+ return (*info->callback) (mod, userdata, name, start, dw, bias, info->arg);
+}
+
ptrdiff_t
dwfl_getdwarf (Dwfl *dwfl,
int (*callback) (Dwfl_Module *, void **,
@@ -57,20 +75,6 @@ dwfl_getdwarf (Dwfl *dwfl,
void *arg,
ptrdiff_t offset)
{
- if (dwfl == NULL)
- return -1;
-
- if ((size_t) offset > dwfl->nmodules)
- return -1;
-
- while ((size_t) offset < dwfl->nmodules)
- {
- Dwfl_Module *mod = dwfl->modules[offset++];
- Dwarf_Addr bias = 0;
- Dwarf *dw = INTUSE(dwfl_module_getdwarf) (mod, &bias);
- if ((*callback) (MODCB_ARGS (mod), dw, bias, arg) != DWARF_CB_OK)
- return offset;
- }
-
- return 0;
+ struct module_callback_info info = { callback, arg };
+ return INTUSE(dwfl_getmodules) (dwfl, &module_callback, &info, offset);
}
diff --git a/libdwfl/dwfl_getmodules.c b/libdwfl/dwfl_getmodules.c
index 45e550a5..f205b899 100644
--- a/libdwfl/dwfl_getmodules.c
+++ b/libdwfl/dwfl_getmodules.c
@@ -1,5 +1,5 @@
/* Iterate through modules.
- Copyright (C) 2005 Red Hat, Inc.
+ Copyright (C) 2005, 2008 Red Hat, Inc.
This file is part of Red Hat elfutils.
Red Hat elfutils is free software; you can redistribute it and/or modify
@@ -59,15 +59,49 @@ dwfl_getmodules (Dwfl *dwfl,
if (dwfl == NULL)
return -1;
- if ((size_t) offset > dwfl->nmodules)
- return -1;
+ Dwfl_Module *m = dwfl->modulelist;
- while ((size_t) offset < dwfl->nmodules)
+ if (unlikely (dwfl->lookup_module == NULL))
+ {
+ for (ptrdiff_t pos = 0; pos < offset; ++pos)
+ if (m == NULL)
+ return -1;
+ else
+ m = m->next;
+ while (m != NULL)
+ {
+ ++offset;
+ if ((*callback) (MODCB_ARGS (m), arg) != DWARF_CB_OK)
+ return offset;
+ m = m->next;
+ }
+ }
+ else
{
- Dwfl_Module *mod = dwfl->modules[offset++];
- if ((*callback) (MODCB_ARGS (mod), arg) != DWARF_CB_OK)
- return offset;
+ if (offset > 0)
+ {
+ if ((size_t) offset - 1 == dwfl->lookup_elts)
+ return 0;
+
+ if (unlikely ((size_t) offset - 1 > dwfl->lookup_elts))
+ return -1;
+
+ m = dwfl->lookup_module[offset - 1];
+ if (unlikely (m == NULL))
+ return -1;
+ }
+
+ while (m != NULL)
+ {
+ int ok = (*callback) (MODCB_ARGS (m), arg);
+ m = m->next;
+ if (ok != DWARF_CB_OK)
+ return (m == NULL
+ ? (ptrdiff_t) dwfl->lookup_elts + 1
+ : m->segment + 1);
+ }
}
return 0;
}
+INTDEF (dwfl_getmodules)
diff --git a/libdwfl/dwfl_module.c b/libdwfl/dwfl_module.c
index 434f3ff9..d7e54138 100644
--- a/libdwfl/dwfl_module.c
+++ b/libdwfl/dwfl_module.c
@@ -1,5 +1,5 @@
/* Maintenance of module list in libdwfl.
- Copyright (C) 2005, 2006, 2007 Red Hat, Inc.
+ Copyright (C) 2005, 2006, 2007, 2008 Red Hat, Inc.
This file is part of Red Hat elfutils.
Red Hat elfutils is free software; you can redistribute it and/or modify
@@ -109,19 +109,18 @@ __libdwfl_module_free (Dwfl_Module *mod)
}
void
-dwfl_report_begin_add (Dwfl *dwfl)
+dwfl_report_begin_add (Dwfl *dwfl __attribute__ ((unused)))
{
- if (dwfl->modules != NULL)
- free (dwfl->modules);
- dwfl->modules = NULL;
- dwfl->nmodules = 0;
+ /* The lookup table will be cleared on demand, there is nothing we need
+ to do here. */
}
INTDEF (dwfl_report_begin_add)
void
dwfl_report_begin (Dwfl *dwfl)
{
- INTUSE(dwfl_report_begin_add) (dwfl);
+ /* Clear the segment lookup table. */
+ dwfl->lookup_elts = 0;
for (Dwfl_Module *m = dwfl->modulelist; m != NULL; m = m->next)
m->gc = true;
@@ -138,6 +137,21 @@ dwfl_report_module (Dwfl *dwfl, const char *name,
GElf_Addr start, GElf_Addr end)
{
Dwfl_Module **tailp = &dwfl->modulelist, **prevp = tailp;
+
+ inline Dwfl_Module *use (Dwfl_Module *mod)
+ {
+ mod->next = *tailp;
+ *tailp = mod;
+
+ if (unlikely (dwfl->lookup_module != NULL))
+ {
+ free (dwfl->lookup_module);
+ dwfl->lookup_module = NULL;
+ }
+
+ return mod;
+ }
+
for (Dwfl_Module *m = *prevp; m != NULL; m = *(prevp = &m->next))
{
if (m->low_addr == start && m->high_addr == end
@@ -145,13 +159,9 @@ dwfl_report_module (Dwfl *dwfl, const char *name,
{
/* This module is still here. Move it to the place in the list
after the last module already reported. */
-
*prevp = m->next;
- m->next = *tailp;
m->gc = false;
- *tailp = m;
- ++dwfl->nmodules;
- return m;
+ return use (m);
}
if (! m->gc)
@@ -175,35 +185,11 @@ dwfl_report_module (Dwfl *dwfl, const char *name,
mod->high_addr = end;
mod->dwfl = dwfl;
- mod->next = *tailp;
- *tailp = mod;
- ++dwfl->nmodules;
-
- return mod;
+ return use (mod);
}
INTDEF (dwfl_report_module)
-static int
-compare_modules (const void *a, const void *b)
-{
- Dwfl_Module *const *p1 = a, *const *p2 = b;
- const Dwfl_Module *m1 = *p1, *m2 = *p2;
- if (m1 == NULL)
- return -1;
- if (m2 == NULL)
- return 1;
-
- /* No signed difference calculation is correct here, since the
- terms are unsigned and could be more than INT64_MAX apart. */
- if (m1->low_addr < m2->low_addr)
- return -1;
- if (m1->low_addr > m2->low_addr)
- return 1;
- return 0;
-}
-
-
/* Finish reporting the current set of modules to the library.
If REMOVED is not null, it's called for each module that
existed before but was not included in the current report.
@@ -216,8 +202,6 @@ dwfl_report_end (Dwfl *dwfl,
void *arg),
void *arg)
{
- assert (dwfl->modules == NULL);
-
Dwfl_Module **tailp = &dwfl->modulelist;
while (*tailp != NULL)
{
@@ -237,21 +221,6 @@ dwfl_report_end (Dwfl *dwfl,
tailp = &m->next;
}
- dwfl->modules = malloc (dwfl->nmodules * sizeof dwfl->modules[0]);
- if (dwfl->modules == NULL && dwfl->nmodules != 0)
- return -1;
-
- size_t i = 0;
- for (Dwfl_Module *m = dwfl->modulelist; m != NULL; m = m->next)
- {
- assert (! m->gc);
- dwfl->modules[i++] = m;
- }
- assert (i == dwfl->nmodules);
-
- qsort (dwfl->modules, dwfl->nmodules, sizeof dwfl->modules[0],
- &compare_modules);
-
return 0;
}
INTDEF (dwfl_report_end)
diff --git a/libdwfl/dwfl_module_addrsym.c b/libdwfl/dwfl_module_addrsym.c
index bc1d556e..72280d11 100644
--- a/libdwfl/dwfl_module_addrsym.c
+++ b/libdwfl/dwfl_module_addrsym.c
@@ -49,6 +49,9 @@
#include "libdwflP.h"
+/* Returns the name of the symbol "closest" to ADDR.
+ Never returns symbols at addresses above ADDR. */
+
const char *
dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
GElf_Sym *closest_sym, GElf_Word *shndxp)
@@ -65,15 +68,6 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
if (shndx >= SHN_LORESERVE)
return sym->st_value == addr;
- /* Ignore section and other special symbols. */
- switch (GELF_ST_TYPE (sym->st_info))
- {
- case STT_SECTION:
- case STT_FILE:
- case STT_TLS:
- return false;
- }
-
/* Figure out what section ADDR lies in. */
if (addr_shndx == SHN_UNDEF)
{
@@ -108,7 +102,7 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
GElf_Word sizeless_shndx = SHN_UNDEF;
/* Keep track of the lowest address a relevant sizeless symbol could have. */
- GElf_Addr min_label = addr;
+ GElf_Addr min_label = 0;
/* Look through the symbol table for a matching symbol. */
for (int i = 1; i < syments; ++i)
@@ -116,24 +110,23 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
GElf_Sym sym;
GElf_Word shndx;
const char *name = INTUSE(dwfl_module_getsym) (mod, i, &sym, &shndx);
- if (name != NULL
+ if (name != NULL && name[0] != '\0'
&& sym.st_shndx != SHN_UNDEF
&& sym.st_value <= addr
- && (sym.st_size == 0 || addr - sym.st_value < sym.st_size))
+ && GELF_ST_TYPE (sym.st_info) != STT_SECTION
+ && GELF_ST_TYPE (sym.st_info) != STT_FILE
+ && GELF_ST_TYPE (sym.st_info) != STT_TLS)
{
- /* Even if we don't choose this symbol, its existence
- excludes any sizeless symbol (assembly label) that
- is inside its bounds. */
- if (sym.st_value + sym.st_size > addr)
+ /* Even if we don't choose this symbol, its existence excludes
+ any sizeless symbol (assembly label) that is below its upper
+ bound. */
+ if (sym.st_value + sym.st_size > min_label)
min_label = sym.st_value + sym.st_size;
- /* This symbol is a better candidate than the current one
- if it's a named symbol, not a section or file symbol,
- and is closer to ADDR or is global when it was local. */
- if (name[0] != '\0'
- && GELF_ST_TYPE (sym.st_info) != STT_SECTION
- && GELF_ST_TYPE (sym.st_info) != STT_FILE)
+ if (sym.st_size == 0 || addr - sym.st_value < sym.st_size)
{
+ /* This symbol is a better candidate than the current one
+ if it's closer to ADDR or is global when it was local. */
if (closest_name == NULL
|| closest_sym->st_value < sym.st_value
|| (GELF_ST_BIND (closest_sym->st_info)
@@ -157,10 +150,13 @@ dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr addr,
}
}
/* When the beginning of its range is no closer,
- the end of its range might be. */
+ the end of its range might be. But do not
+ replace a global symbol with a local! */
else if (sym.st_size != 0
&& closest_sym->st_value == sym.st_value
- && closest_sym->st_size > sym.st_size)
+ && closest_sym->st_size > sym.st_size
+ && (GELF_ST_BIND (closest_sym->st_info)
+ <= GELF_ST_BIND (sym.st_info)))
{
*closest_sym = sym;
closest_shndx = shndx;
diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
new file mode 100644
index 00000000..a627c471
--- /dev/null
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -0,0 +1,645 @@
+/* Sniff out modules from ELF headers visible in memory segments.
+ Copyright (C) 2008 Red Hat, Inc.
+ This file is part of Red Hat elfutils.
+
+ Red Hat elfutils is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 2 of the License.
+
+ Red Hat 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 a copy of the GNU General Public License along
+ with Red Hat elfutils; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
+
+ In addition, as a special exception, Red Hat, Inc. gives You the
+ additional right to link the code of Red Hat elfutils with code licensed
+ under any Open Source Initiative certified open source license
+ (http://www.opensource.org/licenses/index.php) which requires the
+ distribution of source code with any binary distribution and to
+ distribute linked combinations of the two. Non-GPL Code permitted under
+ this exception must only link to the code of Red Hat elfutils through
+ those well defined interfaces identified in the file named EXCEPTION
+ found in the source code files (the "Approved Interfaces"). The files
+ of Non-GPL Code may instantiate templates or use macros or inline
+ functions from the Approved Interfaces without causing the resulting
+ work to be covered by the GNU General Public License. Only Red Hat,
+ Inc. may make changes or additions to the list of Approved Interfaces.
+ Red Hat's grant of this exception is conditioned upon your not adding
+ any new exceptions. If you wish to add a new Approved Interface or
+ exception, please contact Red Hat. You must obey the GNU General Public
+ License in all respects for all of the Red Hat elfutils code and other
+ code used in conjunction with Red Hat elfutils except the Non-GPL Code
+ covered by this exception. If you modify this file, you may extend this
+ exception to your version of the file, but you are not obligated to do
+ so. If you do not wish to provide this exception without modification,
+ you must delete this exception statement from your version and license
+ this file solely under the GPL without exception.
+
+ Red Hat elfutils is an included package of the Open Invention Network.
+ An included package of the Open Invention Network is a package for which
+ Open Invention Network licensees cross-license their patents. No patent
+ license is granted, either expressly or impliedly, by designation as an
+ included package. Should you wish to participate in the Open Invention
+ Network licensing program, please visit www.openinventionnetwork.com
+ <http://www.openinventionnetwork.com>. */
+
+#include <config.h>
+#include "../libelf/libelfP.h" /* For NOTE_ALIGN. */
+#undef _
+#include "libdwflP.h"
+
+#include <elf.h>
+#include <gelf.h>
+#include <inttypes.h>
+#include <sys/param.h>
+#include <alloca.h>
+#include <endian.h>
+
+
+/* A good size for the initial read from memory, if it's not too costly.
+ This more than covers the phdrs and note segment in the average 64-bit
+ binary. */
+
+#define INITIAL_READ 1024
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+# define MY_ELFDATA ELFDATA2LSB
+#else
+# define MY_ELFDATA ELFDATA2MSB
+#endif
+
+
+/* Return user segment index closest to ADDR but not above it. */
+static int
+addr_segndx (Dwfl *dwfl, size_t segment, GElf_Addr addr)
+{
+ int ndx = dwfl->lookup_segndx[segment];
+ do
+ {
+ if (dwfl->lookup_segndx[segment] >= 0)
+ ndx = dwfl->lookup_segndx[segment];
+ ++segment;
+ }
+ while (segment < dwfl->lookup_elts - 1
+ && dwfl->lookup_addr[segment] < addr);
+
+ while (dwfl->lookup_segndx[segment] < 0
+ && segment < dwfl->lookup_elts - 1)
+ ++segment;
+
+ if (dwfl->lookup_segndx[segment] >= 0)
+ ndx = dwfl->lookup_segndx[segment];
+
+ return ndx;
+}
+
+int
+dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
+ Dwfl_Memory_Callback *memory_callback,
+ void *memory_callback_arg,
+ Dwfl_Module_Callback *read_eagerly,
+ void *read_eagerly_arg)
+{
+ size_t segment = ndx;
+
+ if (segment >= dwfl->lookup_elts)
+ segment = dwfl->lookup_elts - 1;
+
+ while (segment > 0 && dwfl->lookup_segndx[segment] > ndx)
+ --segment;
+
+ while (dwfl->lookup_segndx[segment] < ndx)
+ if (++segment == dwfl->lookup_elts)
+ return 0;
+
+ GElf_Addr start = dwfl->lookup_addr[segment];
+
+ inline bool segment_read (int segndx,
+ void **buffer, size_t *buffer_available,
+ GElf_Addr addr, size_t minread)
+ {
+ return ! (*memory_callback) (dwfl, segndx, buffer, buffer_available,
+ addr, minread, memory_callback_arg);
+ }
+
+ inline void release_buffer (void **buffer, size_t *buffer_available)
+ {
+ if (*buffer != NULL)
+ (void) segment_read (-1, buffer, buffer_available, 0, 0);
+ }
+
+ /* First read in the file header and check its sanity. */
+
+ void *buffer = NULL;
+ size_t buffer_available = INITIAL_READ;
+
+ inline int finish (void)
+ {
+ release_buffer (&buffer, &buffer_available);
+ return ndx;
+ }
+
+ if (segment_read (ndx, &buffer, &buffer_available,
+ start, sizeof (Elf64_Ehdr))
+ || memcmp (buffer, ELFMAG, SELFMAG) != 0)
+ return finish ();
+
+ inline bool read_portion (void **data, size_t *data_size,
+ GElf_Addr vaddr, size_t filesz)
+ {
+ if (vaddr - start + filesz > buffer_available)
+ {
+ *data = NULL;
+ *data_size = filesz;
+ return segment_read (addr_segndx (dwfl, segment, vaddr),
+ data, data_size, vaddr, filesz);
+ }
+
+ /* We already have this whole note segment from our initial read. */
+ *data = vaddr - start + buffer;
+ *data_size = 0;
+ return false;
+ }
+
+ inline void finish_portion (void **data, size_t *data_size)
+ {
+ if (*data_size != 0)
+ release_buffer (data, data_size);
+ }
+
+ /* Extract the information we need from the file header. */
+ union
+ {
+ Elf32_Ehdr e32;
+ Elf64_Ehdr e64;
+ } ehdr;
+ GElf_Off phoff;
+ uint_fast16_t phnum;
+ uint_fast16_t phentsize;
+ GElf_Off shdrs_end;
+ Elf_Data xlatefrom =
+ {
+ .d_type = ELF_T_EHDR,
+ .d_buf = (void *) buffer,
+ .d_version = EV_CURRENT,
+ };
+ Elf_Data xlateto =
+ {
+ .d_type = ELF_T_EHDR,
+ .d_buf = &ehdr,
+ .d_size = sizeof ehdr,
+ .d_version = EV_CURRENT,
+ };
+ switch (((const unsigned char *) buffer)[EI_CLASS])
+ {
+ case ELFCLASS32:
+ xlatefrom.d_size = sizeof (Elf32_Ehdr);
+ if (elf32_xlatetom (&xlateto, &xlatefrom,
+ ((const unsigned char *) buffer)[EI_DATA]) == NULL)
+ return finish ();
+ phoff = ehdr.e32.e_phoff;
+ phnum = ehdr.e32.e_phnum;
+ phentsize = ehdr.e32.e_phentsize;
+ if (phentsize != sizeof (Elf32_Phdr))
+ return finish ();
+ shdrs_end = ehdr.e32.e_shoff + ehdr.e32.e_shnum * ehdr.e32.e_shentsize;
+ break;
+
+ case ELFCLASS64:
+ xlatefrom.d_size = sizeof (Elf64_Ehdr);
+ if (elf64_xlatetom (&xlateto, &xlatefrom,
+ ((const unsigned char *) buffer)[EI_DATA]) == NULL)
+ return finish ();
+ phoff = ehdr.e64.e_phoff;
+ phnum = ehdr.e64.e_phnum;
+ phentsize = ehdr.e64.e_phentsize;
+ if (phentsize != sizeof (Elf64_Phdr))
+ return finish ();
+ shdrs_end = ehdr.e64.e_shoff + ehdr.e64.e_shnum * ehdr.e64.e_shentsize;
+ break;
+
+ default:
+ return finish ();
+ }
+
+ /* The file header tells where to find the program headers.
+ These are what we need to find the boundaries of the module.
+ Without them, we don't have a module to report. */
+
+ if (phnum == 0)
+ return finish ();
+
+ xlatefrom.d_type = xlateto.d_type = ELF_T_PHDR;
+ xlatefrom.d_size = phnum * phentsize;
+
+ void *ph_buffer = NULL;
+ size_t ph_buffer_size = 0;
+ if (read_portion (&ph_buffer, &ph_buffer_size,
+ start + phoff, xlatefrom.d_size))
+ return finish ();
+
+ xlatefrom.d_buf = ph_buffer;
+
+ union
+ {
+ Elf32_Phdr p32[phnum];
+ Elf64_Phdr p64[phnum];
+ } phdrs;
+
+ xlateto.d_buf = &phdrs;
+ xlateto.d_size = sizeof phdrs;
+
+ /* Track the bounds of the file visible in memory. */
+ GElf_Off file_trimmed_end = 0; /* Proper p_vaddr + p_filesz end. */
+ GElf_Off file_end = 0; /* Rounded up to effective page size. */
+ GElf_Off contiguous = 0; /* Visible as contiguous file from START. */
+ GElf_Off total_filesz = 0; /* Total size of data to read. */
+
+ /* Collect the bias between START and the containing PT_LOAD's p_vaddr. */
+ GElf_Addr bias = 0;
+ bool found_bias = false;
+
+ /* Collect the unbiased bounds of the module here. */
+ GElf_Addr module_start = -1l;
+ GElf_Addr module_end = 0;
+
+ /* If we see PT_DYNAMIC, record it here. */
+ GElf_Addr dyn_vaddr = 0;
+ GElf_Xword dyn_filesz = 0;
+
+ /* Collect the build ID bits here. */
+ void *build_id = NULL;
+ size_t build_id_len = 0;
+ GElf_Addr build_id_vaddr = 0;
+
+ /* Consider a PT_NOTE we've found in the image. */
+ inline void consider_notes (GElf_Addr vaddr, GElf_Xword filesz)
+ {
+ /* If we have already seen a build ID, we don't care any more. */
+ if (build_id != NULL || filesz == 0)
+ return;
+
+ void *data;
+ size_t data_size;
+ if (read_portion (&data, &data_size, vaddr, filesz))
+ return;
+
+ assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
+
+ void *notes;
+ if (ehdr.e32.e_ident[EI_DATA] == MY_ELFDATA)
+ notes = data;
+ else
+ {
+ notes = malloc (filesz);
+ if (unlikely (notes == NULL))
+ return;
+ xlatefrom.d_type = xlateto.d_type = ELF_T_NHDR;
+ xlatefrom.d_buf = (void *) data;
+ xlatefrom.d_size = filesz;
+ xlateto.d_buf = notes;
+ xlateto.d_size = filesz;
+ if (elf32_xlatetom (&xlateto, &xlatefrom,
+ ehdr.e32.e_ident[EI_DATA]) == NULL)
+ goto done;
+ }
+
+ const GElf_Nhdr *nh = notes;
+ while ((const void *) nh < (const void *) notes + filesz)
+ {
+ const void *note_name = nh + 1;
+ const void *note_desc = note_name + NOTE_ALIGN (nh->n_namesz);
+ if (unlikely ((const void *) notes + filesz - note_desc < nh->n_descsz))
+ break;
+
+ if (nh->n_type == NT_GNU_BUILD_ID
+ && nh->n_descsz > 0
+ && nh->n_namesz == sizeof "GNU"
+ && !memcmp (note_name, "GNU", sizeof "GNU"))
+ {
+ build_id_vaddr = note_desc - (const void *) notes + vaddr;
+ build_id_len = nh->n_descsz;
+ build_id = malloc (nh->n_descsz);
+ if (likely (build_id != NULL))
+ memcpy (build_id, note_desc, build_id_len);
+ break;
+ }
+
+ nh = note_desc + NOTE_ALIGN (nh->n_descsz);
+ }
+
+ done:
+ if (notes != data)
+ free (notes);
+ finish_portion (&data, &data_size);
+ }
+
+ /* Consider each of the program headers we've read from the image. */
+ inline void consider_phdr (GElf_Word type,
+ GElf_Addr vaddr, GElf_Xword memsz,
+ GElf_Off offset, GElf_Xword filesz,
+ GElf_Xword align)
+ {
+ switch (type)
+ {
+ case PT_DYNAMIC:
+ dyn_vaddr = vaddr;
+ dyn_filesz = filesz;
+ break;
+
+ case PT_NOTE:
+ /* We calculate from the p_offset of the note segment,
+ because we don't yet know the bias for its p_vaddr. */
+ consider_notes (start + offset, filesz);
+ break;
+
+ case PT_LOAD:
+ align = dwfl->segment_align > 1 ? dwfl->segment_align : align ?: 1;
+
+ GElf_Addr vaddr_end = (vaddr + memsz + align - 1) & -align;
+ GElf_Addr filesz_vaddr = filesz < memsz ? vaddr + filesz : vaddr_end;
+ GElf_Off filesz_offset = filesz_vaddr - vaddr + offset;
+
+ if (file_trimmed_end < offset + filesz)
+ {
+ file_trimmed_end = offset + filesz;
+
+ /* Trim the last segment so we don't bother with zeros
+ in the last page that are off the end of the file.
+ However, if the extra bit in that page includes the
+ section headers, keep them. */
+ if (shdrs_end <= filesz_offset && shdrs_end > file_trimmed_end)
+ {
+ filesz += shdrs_end - file_trimmed_end;
+ file_trimmed_end = shdrs_end;
+ }
+ }
+
+ total_filesz += filesz;
+
+ if (file_end < filesz_offset)
+ {
+ file_end = filesz_offset;
+ if (filesz_vaddr - start == filesz_offset)
+ contiguous = file_end;
+ }
+
+ if (!found_bias && (offset & -align) == 0
+ && likely (filesz_offset >= phoff + phnum * phentsize))
+ {
+ bias = start - vaddr;
+ found_bias = true;
+ }
+
+ vaddr &= -align;
+ if (vaddr < module_start)
+ module_start = vaddr;
+
+ if (module_end < vaddr_end)
+ module_end = vaddr_end;
+ break;
+ }
+ }
+ if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
+ {
+ if (elf32_xlatetom (&xlateto, &xlatefrom,
+ ehdr.e32.e_ident[EI_DATA]) == NULL)
+ found_bias = false; /* Trigger error check. */
+ else
+ for (uint_fast16_t i = 0; i < phnum; ++i)
+ consider_phdr (phdrs.p32[i].p_type,
+ phdrs.p32[i].p_vaddr, phdrs.p32[i].p_memsz,
+ phdrs.p32[i].p_offset, phdrs.p32[i].p_filesz,
+ phdrs.p32[i].p_align);
+ }
+ else
+ {
+ if (elf64_xlatetom (&xlateto, &xlatefrom,
+ ehdr.e32.e_ident[EI_DATA]) == NULL)
+ found_bias = false; /* Trigger error check. */
+ else
+ for (uint_fast16_t i = 0; i < phnum; ++i)
+ consider_phdr (phdrs.p64[i].p_type,
+ phdrs.p64[i].p_vaddr, phdrs.p64[i].p_memsz,
+ phdrs.p64[i].p_offset, phdrs.p64[i].p_filesz,
+ phdrs.p64[i].p_align);
+ }
+
+ finish_portion (&ph_buffer, &ph_buffer_size);
+
+ /* We must have seen the segment covering offset 0, or else the ELF
+ header we read at START was not produced by these program headers. */
+ if (unlikely (!found_bias))
+ return finish ();
+
+ /* Now we know enough to report a module for sure: its bounds. */
+ module_start += bias;
+ module_end += bias;
+
+ dyn_vaddr += bias;
+
+ /* Our return value now says to skip the segments contained
+ within the module.
+ XXX handle gaps
+ */
+ ndx = addr_segndx (dwfl, segment, module_end);
+
+ /* Examine its .dynamic section to get more interesting details.
+ If it has DT_SONAME, we'll use that as the module name.
+ We need its DT_STRTAB and DT_STRSZ to decipher DT_SONAME,
+ and they also tell us the essential portion of the file
+ for fetching symbols. */
+ GElf_Addr soname_stroff = 0;
+ GElf_Addr dynstr_vaddr = 0;
+ GElf_Xword dynstrsz = 0;
+ inline bool consider_dyn (GElf_Sxword tag, GElf_Xword val)
+ {
+ switch (tag)
+ {
+ default:
+ return false;
+
+ case DT_SONAME:
+ soname_stroff = val;
+ break;
+
+ case DT_STRTAB:
+ dynstr_vaddr = val;
+ break;
+
+ case DT_STRSZ:
+ dynstrsz = val;
+ break;
+ }
+
+ return soname_stroff != 0 && dynstr_vaddr != 0 && dynstrsz != 0;
+ }
+
+ const size_t dyn_entsize = (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32
+ ? sizeof (Elf32_Dyn) : sizeof (Elf64_Dyn));
+ void *dyn_data = NULL;
+ size_t dyn_data_size = 0;
+ if (dyn_filesz != 0 && dyn_filesz % dyn_entsize == 0
+ && ! read_portion (&dyn_data, &dyn_data_size, dyn_vaddr, dyn_filesz))
+ {
+ union
+ {
+ Elf32_Dyn d32[dyn_filesz / sizeof (Elf32_Dyn)];
+ Elf64_Dyn d64[dyn_filesz / sizeof (Elf64_Dyn)];
+ } dyn;
+
+ xlatefrom.d_type = xlateto.d_type = ELF_T_DYN;
+ xlatefrom.d_buf = (void *) dyn_data;
+ xlatefrom.d_size = dyn_filesz;
+ xlateto.d_buf = &dyn;
+ xlateto.d_size = sizeof dyn;
+
+ if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
+ {
+ if (elf32_xlatetom (&xlateto, &xlatefrom,
+ ehdr.e32.e_ident[EI_DATA]) != NULL)
+ for (size_t i = 0; i < dyn_filesz / sizeof dyn.d32[0]; ++i)
+ if (consider_dyn (dyn.d32[i].d_tag, dyn.d32[i].d_un.d_val))
+ break;
+ }
+ else
+ {
+ if (elf64_xlatetom (&xlateto, &xlatefrom,
+ ehdr.e32.e_ident[EI_DATA]) != NULL)
+ for (size_t i = 0; i < dyn_filesz / sizeof dyn.d64[0]; ++i)
+ if (consider_dyn (dyn.d64[i].d_tag, dyn.d64[i].d_un.d_val))
+ break;
+ }
+ }
+ finish_portion (&dyn_data, &dyn_data_size);
+
+ /* We'll use the name passed in or a stupid default if not DT_SONAME. */
+ if (name == NULL)
+ name = ehdr.e32.e_type == ET_EXEC ? "[exe]" : "[dso]";
+
+ void *soname = NULL;
+ size_t soname_size = 0;
+ if (dynstrsz != 0 && dynstr_vaddr != 0)
+ {
+ /* We know the bounds of the .dynstr section. */
+ dynstr_vaddr += bias;
+ if (unlikely (dynstr_vaddr + dynstrsz > module_end))
+ dynstrsz = 0;
+
+ /* Try to get the DT_SONAME string. */
+ if (soname_stroff != 0 && soname_stroff < dynstrsz - 1
+ && ! read_portion (&soname, &soname_size,
+ dynstr_vaddr + soname_stroff, 0))
+ name = soname;
+ }
+
+ /* Now that we have chosen the module's name and bounds, report it.
+ If we found a build ID, report that too. */
+
+ Dwfl_Module *mod = INTUSE(dwfl_report_module) (dwfl, name,
+ module_start, module_end);
+ if (likely (mod != NULL) && build_id != NULL
+ && unlikely (INTUSE(dwfl_module_report_build_id) (mod,
+ build_id,
+ build_id_len,
+ build_id_vaddr)))
+ {
+ mod->gc = true;
+ mod = NULL;
+ }
+
+ /* At this point we do not need BUILD_ID or NAME any more.
+ They have been copied. */
+ free (build_id);
+ finish_portion (&soname, &soname_size);
+
+ if (unlikely (mod == NULL))
+ {
+ ndx = -1;
+ return finish ();
+ }
+
+ /* We have reported the module. Now let the caller decide whether we
+ should read the whole thing in right now. */
+
+ const GElf_Off cost = (contiguous < file_trimmed_end ? total_filesz
+ : buffer_available >= contiguous ? 0
+ : contiguous - buffer_available);
+ const GElf_Off worthwhile = ((dynstr_vaddr == 0 || dynstrsz == 0) ? 0
+ : dynstr_vaddr + dynstrsz - start);
+ const GElf_Off whole = MAX (file_trimmed_end, shdrs_end);
+
+ Elf *elf = NULL;
+ if ((*read_eagerly) (MODCB_ARGS (mod), &buffer, &buffer_available,
+ cost, worthwhile, whole, contiguous,
+ read_eagerly_arg, &elf)
+ && elf == NULL)
+ {
+ /* The caller wants to read the whole file in right now, but hasn't
+ done it for us. Fill in a local image of the virtual file. */
+
+ void *contents = calloc (1, file_trimmed_end);
+ if (unlikely (contents == NULL))
+ return finish ();
+
+ inline void final_read (size_t offset, GElf_Addr vaddr, size_t size)
+ {
+ void *into = contents + offset;
+ size_t read_size = size;
+ (void) segment_read (addr_segndx (dwfl, segment, vaddr),
+ &into, &read_size, vaddr, size);
+ }
+
+ if (contiguous < file_trimmed_end)
+ {
+ /* We can't use the memory image verbatim as the file image.
+ So we'll be reading into a local image of the virtual file. */
+
+ inline void read_phdr (GElf_Word type, GElf_Addr vaddr,
+ GElf_Off offset, GElf_Xword filesz)
+ {
+ if (type == PT_LOAD)
+ final_read (offset, vaddr + bias, filesz);
+ }
+
+ if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
+ for (uint_fast16_t i = 0; i < phnum; ++i)
+ read_phdr (phdrs.p32[i].p_type, phdrs.p32[i].p_vaddr,
+ phdrs.p32[i].p_offset, phdrs.p32[i].p_filesz);
+ else
+ for (uint_fast16_t i = 0; i < phnum; ++i)
+ read_phdr (phdrs.p64[i].p_type, phdrs.p64[i].p_vaddr,
+ phdrs.p64[i].p_offset, phdrs.p64[i].p_filesz);
+ }
+ else
+ {
+ /* The whole file sits contiguous in memory,
+ but the caller didn't want to just do it. */
+
+ const size_t have = MIN (buffer_available, file_trimmed_end);
+ memcpy (contents, buffer, have);
+
+ if (have < file_trimmed_end)
+ final_read (have, start + have, file_trimmed_end - have);
+ }
+
+ elf = elf_memory (contents, file_trimmed_end);
+ if (unlikely (elf == NULL))
+ free (contents);
+ else
+ elf->flags |= ELF_F_MALLOCED;
+ }
+
+ if (elf != NULL)
+ {
+ /* Install the file in the module. */
+ mod->main.elf = elf;
+ mod->main.bias = bias;
+ }
+
+ return finish ();
+}
diff --git a/libdwfl/libdwfl.h b/libdwfl/libdwfl.h
index 5438ee2d..b6ae7765 100644
--- a/libdwfl/libdwfl.h
+++ b/libdwfl/libdwfl.h
@@ -1,5 +1,5 @@
/* Interfaces for libdwfl.
- Copyright (C) 2005, 2006, 2007 Red Hat, Inc.
+ Copyright (C) 2005, 2006, 2007, 2008 Red Hat, Inc.
This file is part of Red Hat elfutils.
Red Hat elfutils is free software; you can redistribute it and/or modify
@@ -116,10 +116,42 @@ extern int dwfl_errno (void);
extern const char *dwfl_errmsg (int err);
-/* Start reporting the current set of modules to the library. No calls but
- dwfl_report_* can be made on DWFL until dwfl_report_end is called. */
+/* Start reporting the current set of segments and modules to the library.
+ All existing segments are wiped. Existing modules are marked to be
+ deleted, and will not be found via dwfl_addrmodule et al if they are not
+ re-reported before dwfl_report_end is called. */
extern void dwfl_report_begin (Dwfl *dwfl);
+/* Report that segment NDX begins at PHDR->p_vaddr + BIAS.
+ If NDX is < 0, the value succeeding the last call's NDX
+ is used instead (zero on the first call).
+
+ If nonzero, the smallest PHDR->p_align value seen sets the
+ effective page size for the address space DWFL describes.
+ This is the granularity at which reported module boundary
+ addresses will be considered to fall in or out of a segment.
+
+ Returns -1 for errors, or NDX (or its assigned replacement) on success.
+
+ When NDX is the value succeeding the last call's NDX (or is implicitly
+ so as above), IDENT is nonnull and matches the value in the last call,
+ and the PHDR and BIAS values reflect a segment that would be contiguous,
+ in both memory and file, with the last segment reported, then this
+ segment may be coalesced internally with preceding segments. When given
+ an address inside this segment, dwfl_addrsegment may return the NDX of a
+ preceding contiguous segment. To prevent coalesced segments, always
+ pass a null pointer for IDENT.
+
+ The values passed are not stored (except to track coalescence).
+ The only information that can be extracted from DWFL later is the
+ mapping of an address to a segment index that starts at or below
+ it. Reporting segments at all is optional. Its only benefit to
+ the caller is to offer this quick lookup via dwfl_addrsegment,
+ or use other segment-based calls. */
+extern int dwfl_report_segment (Dwfl *dwfl, int ndx,
+ const GElf_Phdr *phdr, GElf_Addr bias,
+ const void *ident);
+
/* Report that a module called NAME spans addresses [START, END).
Returns the module handle, either existing or newly allocated,
or returns a null pointer for an allocation error. */
@@ -193,6 +225,13 @@ extern ptrdiff_t dwfl_getmodules (Dwfl *dwfl,
/* Find the module containing the given address. */
extern Dwfl_Module *dwfl_addrmodule (Dwfl *dwfl, Dwarf_Addr address);
+/* Find the segment, if any, and module, if any, containing ADDRESS.
+ Returns a segment index returned by dwfl_report_segment, or -1
+ if no segment matches the address. Regardless of the return value,
+ *MOD is always set to the module containing ADDRESS, or to null. */
+extern int dwfl_addrsegment (Dwfl *dwfl, Dwarf_Addr address, Dwfl_Module **mod);
+
+
/* Report the known build ID bits associated with a module.
If VADDR is nonzero, it gives the absolute address where those
diff --git a/libdwfl/libdwflP.h b/libdwfl/libdwflP.h
index 50c6cd83..6ba5c96e 100644
--- a/libdwfl/libdwflP.h
+++ b/libdwfl/libdwflP.h
@@ -108,10 +108,22 @@ struct Dwfl
Dwfl_Module *modulelist; /* List in order used by full traversals. */
- Dwfl_Module **modules;
- size_t nmodules;
-
GElf_Addr offline_next_address;
+
+ GElf_Addr segment_align; /* Smallest granularity of segments. */
+
+ /* Binary search table in three parallel malloc'd arrays. */
+ size_t lookup_elts; /* Elements in use. */
+ size_t lookup_alloc; /* Elements allococated. */
+ GElf_Addr *lookup_addr; /* Start address of segment. */
+ Dwfl_Module **lookup_module; /* Module associated with segment, or null. */
+ int *lookup_segndx; /* User segment index, or -1. */
+
+ /* Cache from last dwfl_report_segment call. */
+ const void *lookup_tail_ident;
+ GElf_Off lookup_tail_vaddr;
+ GElf_Off lookup_tail_offset;
+ int lookup_tail_ndx;
};
#define OFFLINE_REDZONE 0x10000
@@ -168,6 +180,7 @@ struct Dwfl_Module
struct dwfl_arange *aranges; /* Mapping of addresses in module to CUs. */
unsigned int naranges;
+ int segment; /* Index of first segment table entry. */
bool gc; /* Mark/sweep flag. */
};
@@ -303,13 +316,64 @@ extern Dwfl_Module *__libdwfl_report_offline (Dwfl *dwfl, const char *name,
internal_function;
+/* These are working nicely for --core, but are not ready to be
+ exported interfaces quite yet. */
+
+/* Type of callback function ...
+ */
+typedef bool Dwfl_Memory_Callback (Dwfl *dwfl, int segndx,
+ void **buffer, size_t *buffer_available,
+ GElf_Addr vaddr, size_t minread, void *arg);
+
+/* Type of callback function ...
+ */
+typedef bool Dwfl_Module_Callback (Dwfl_Module *mod, void **userdata,
+ const char *name, Dwarf_Addr base,
+ void **buffer, size_t *buffer_available,
+ GElf_Off cost, GElf_Off worthwhile,
+ GElf_Off whole, GElf_Off contiguous,
+ void *arg, Elf **elfp);
+
+/* ...
+ */
+extern int dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
+ Dwfl_Memory_Callback *memory_callback,
+ void *memory_callback_arg,
+ Dwfl_Module_Callback *read_eagerly,
+ void *read_eagerly_arg);
+
+/* Report a module for entry in the dynamic linker's struct link_map list.
+ For each link_map entry, if an existing module resides at its address,
+ this just modifies that module's name and suggested file name. If
+ no such module exists, this calls dwfl_report_elf on the l_name string.
+
+ If AUXV is not null, it points to AUXV_SIZE bytes of auxiliary vector
+ data as contained in an NT_AUXV note or read from a /proc/pid/auxv
+ file. When this is available, it guides the search. If AUXV is null
+ or the memory it points to is not accessible, then this search can
+ only find where to begin if the correct executable file was
+ previously reported and preloaded as with dwfl_report_elf.
+
+ Returns the number of modules found, or -1 for errors. */
+extern int dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
+ Dwfl_Memory_Callback *memory_callback,
+ void *memory_callback_arg);
+
+/* Examine an ET_CORE file and report modules based on its contents. */
+extern int dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const GElf_Ehdr *ehdr);
+
+
/* Avoid PLT entries. */
INTDECL (dwfl_begin)
INTDECL (dwfl_errmsg)
INTDECL (dwfl_addrmodule)
+INTDECL (dwfl_addrsegment)
INTDECL (dwfl_addrdwarf)
INTDECL (dwfl_addrdie)
+INTDECL (dwfl_core_file_report)
+INTDECL (dwfl_getmodules)
INTDECL (dwfl_module_addrdie)
+INTDECL (dwfl_module_address_section)
INTDECL (dwfl_module_addrsym)
INTDECL (dwfl_module_build_id)
INTDECL (dwfl_module_getdwarf)
@@ -322,11 +386,13 @@ INTDECL (dwfl_report_elf)
INTDECL (dwfl_report_begin)
INTDECL (dwfl_report_begin_add)
INTDECL (dwfl_report_module)
+INTDECL (dwfl_report_segment)
INTDECL (dwfl_report_offline)
INTDECL (dwfl_report_end)
INTDECL (dwfl_build_id_find_elf)
INTDECL (dwfl_build_id_find_debuginfo)
INTDECL (dwfl_standard_find_debuginfo)
+INTDECL (dwfl_link_map_report)
INTDECL (dwfl_linux_kernel_find_elf)
INTDECL (dwfl_linux_kernel_module_section_address)
INTDECL (dwfl_linux_proc_report)
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
new file mode 100644
index 00000000..e9890384
--- /dev/null
+++ b/libdwfl/link_map.c
@@ -0,0 +1,867 @@
+/* Report modules by examining dynamic linker data structures.
+ Copyright (C) 2008 Red Hat, Inc.
+ This file is part of Red Hat elfutils.
+
+ Red Hat elfutils is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 2 of the License.
+
+ Red Hat 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 a copy of the GNU General Public License along
+ with Red Hat elfutils; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
+
+ In addition, as a special exception, Red Hat, Inc. gives You the
+ additional right to link the code of Red Hat elfutils with code licensed
+ under any Open Source Initiative certified open source license
+ (http://www.opensource.org/licenses/index.php) which requires the
+ distribution of source code with any binary distribution and to
+ distribute linked combinations of the two. Non-GPL Code permitted under
+ this exception must only link to the code of Red Hat elfutils through
+ those well defined interfaces identified in the file named EXCEPTION
+ found in the source code files (the "Approved Interfaces"). The files
+ of Non-GPL Code may instantiate templates or use macros or inline
+ functions from the Approved Interfaces without causing the resulting
+ work to be covered by the GNU General Public License. Only Red Hat,
+ Inc. may make changes or additions to the list of Approved Interfaces.
+ Red Hat's grant of this exception is conditioned upon your not adding
+ any new exceptions. If you wish to add a new Approved Interface or
+ exception, please contact Red Hat. You must obey the GNU General Public
+ License in all respects for all of the Red Hat elfutils code and other
+ code used in conjunction with Red Hat elfutils except the Non-GPL Code
+ covered by this exception. If you modify this file, you may extend this
+ exception to your version of the file, but you are not obligated to do
+ so. If you do not wish to provide this exception without modification,
+ you must delete this exception statement from your version and license
+ this file solely under the GPL without exception.
+
+ Red Hat elfutils is an included package of the Open Invention Network.
+ An included package of the Open Invention Network is a package for which
+ Open Invention Network licensees cross-license their patents. No patent
+ license is granted, either expressly or impliedly, by designation as an
+ included package. Should you wish to participate in the Open Invention
+ Network licensing program, please visit www.openinventionnetwork.com
+ <http://www.openinventionnetwork.com>. */
+
+#include <config.h>
+#include "libdwflP.h"
+
+#include <byteswap.h>
+#include <endian.h>
+
+/* This element is always provided and always has a constant value.
+ This makes it an easy thing to scan for to discern the format. */
+#define PROBE_TYPE AT_PHENT
+#define PROBE_VAL32 sizeof (Elf32_Phdr)
+#define PROBE_VAL64 sizeof (Elf64_Phdr)
+
+#if BYTE_ORDER == BIG_ENDIAN
+# define BE32(x) (x)
+# define BE64(x) (x)
+# define LE32(x) bswap_32 (x)
+# define LE64(x) bswap_64 (x)
+#else
+# define LE32(x) (x)
+# define LE64(x) (x)
+# define BE32(x) bswap_32 (x)
+# define BE64(x) bswap_64 (x)
+#endif
+
+
+/* Examine an auxv data block and determine its format.
+ Return true iff we figured it out. */
+static bool
+auxv_format_probe (const void *auxv, size_t size,
+ uint_fast8_t *elfclass, uint_fast8_t *elfdata)
+{
+ const union
+ {
+ char buf[size];
+ Elf32_auxv_t a32[size / sizeof (Elf32_auxv_t)];
+ Elf64_auxv_t a64[size / sizeof (Elf64_auxv_t)];
+ } *u = auxv;
+
+ inline bool check64 (size_t i)
+ {
+ if (u->a64[i].a_type == BE64 (PROBE_TYPE)
+ && u->a64[i].a_un.a_val == BE64 (PROBE_VAL64))
+ {
+ *elfdata = ELFDATA2MSB;
+ return true;
+ }
+
+ if (u->a64[i].a_type == LE64 (PROBE_TYPE)
+ && u->a64[i].a_un.a_val == LE64 (PROBE_VAL64))
+ {
+ *elfdata = ELFDATA2LSB;
+ return true;
+ }
+
+ return false;
+ }
+
+ inline bool check32 (size_t i)
+ {
+ if (u->a32[i].a_type == BE32 (PROBE_TYPE)
+ && u->a32[i].a_un.a_val == BE32 (PROBE_VAL32))
+ {
+ *elfdata = ELFDATA2MSB;
+ return true;
+ }
+
+ if (u->a32[i].a_type == LE32 (PROBE_TYPE)
+ && u->a32[i].a_un.a_val == LE32 (PROBE_VAL32))
+ {
+ *elfdata = ELFDATA2LSB;
+ return true;
+ }
+
+ return false;
+ }
+
+ size_t i;
+ for (i = 0; i < size / sizeof (Elf64_auxv_t); ++i)
+ {
+ if (check64 (i))
+ {
+ *elfclass = ELFCLASS64;
+ return true;
+ }
+
+ if (check32 (i))
+ {
+ *elfclass = ELFCLASS32;
+ return true;
+ }
+ }
+ for (; i < size / sizeof (Elf64_auxv_t); ++i)
+ if (check32 (i))
+ {
+ *elfclass = ELFCLASS32;
+ return true;
+ }
+
+ return false;
+}
+
+/* This is a Dwfl_Memory_Callback that wraps another memory callback.
+ If the underlying callback cannot fill the data, then this will
+ fall back to fetching data from module files. */
+
+struct integrated_memory_callback
+{
+ Dwfl_Memory_Callback *memory_callback;
+ void *memory_callback_arg;
+ void *buffer;
+};
+
+static bool
+integrated_memory_callback (Dwfl *dwfl, int ndx,
+ void **buffer, size_t *buffer_available,
+ GElf_Addr vaddr,
+ size_t minread,
+ void *arg)
+{
+ struct integrated_memory_callback *info = arg;
+
+ if (ndx == -1)
+ {
+ /* Called for cleanup. */
+ if (info->buffer != NULL)
+ {
+ /* The last probe buffer came from the underlying callback.
+ Let it do its cleanup. */
+ assert (*buffer == info->buffer); /* XXX */
+ *buffer = info->buffer;
+ info->buffer = NULL;
+ return (*info->memory_callback) (dwfl, ndx, buffer, buffer_available,
+ vaddr, minread,
+ info->memory_callback_arg);
+ }
+ *buffer = NULL;
+ *buffer_available = 0;
+ return false;
+ }
+
+ if (*buffer != NULL)
+ /* For a final-read request, we only use the underlying callback. */
+ return (*info->memory_callback) (dwfl, ndx, buffer, buffer_available,
+ vaddr, minread, info->memory_callback_arg);
+
+ /* Let the underlying callback try to fill this request. */
+ if ((*info->memory_callback) (dwfl, ndx, &info->buffer, buffer_available,
+ vaddr, minread, info->memory_callback_arg))
+ {
+ *buffer = info->buffer;
+ return true;
+ }
+
+ /* Now look for module text covering this address. */
+
+ Dwfl_Module *mod;
+ (void) INTUSE(dwfl_addrsegment) (dwfl, vaddr, &mod);
+ if (mod == NULL)
+ return false;
+
+ Dwarf_Addr bias;
+ Elf_Scn *scn = INTUSE(dwfl_module_address_section) (mod, &vaddr, &bias);
+ if (unlikely (scn == NULL))
+ {
+#if 0 // XXX would have to handle ndx=-1 cleanup calls passed down.
+ /* If we have no sections we can try to fill it from the module file
+ based on its phdr mappings. */
+ if (likely (mod->e_type != ET_REL) && mod->main.elf != NULL)
+ return INTUSE(dwfl_elf_phdr_memory_callback)
+ (dwfl, 0, buffer, buffer_available,
+ vaddr - mod->main.bias, minread, mod->main.elf);
+#endif
+ return false;
+ }
+
+ Elf_Data *data = elf_rawdata (scn, NULL);
+ if (unlikely (data == NULL))
+ // XXX throw error?
+ return false;
+
+ if (unlikely (data->d_size < vaddr))
+ return false;
+
+ /* Provide as much data as we have. */
+ void *contents = data->d_buf + vaddr;
+ size_t avail = data->d_size - vaddr;
+ if (unlikely (avail < minread))
+ return false;
+
+ /* If probing for a string, make sure it's terminated. */
+ if (minread == 0 && unlikely (memchr (contents, '\0', avail) == NULL))
+ return false;
+
+ /* We have it! */
+ *buffer = contents;
+ *buffer_available = avail;
+ return true;
+}
+
+static size_t
+addrsize (uint_fast8_t elfclass)
+{
+ return elfclass * 4;
+}
+
+/* Report a module for each struct link_map in the linked list at r_map
+ in the struct r_debug at R_DEBUG_VADDR.
+
+ For each link_map entry, if an existing module resides at its address,
+ this just modifies that module's name and suggested file name. If
+ no such module exists, this calls dwfl_report_elf on the l_name string.
+
+ Returns the number of modules found, or -1 for errors. */
+
+static int
+report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
+ Dwfl *dwfl, GElf_Addr r_debug_vaddr,
+ Dwfl_Memory_Callback *memory_callback,
+ void *memory_callback_arg)
+{
+ /* Skip r_version, to aligned r_map field. */
+ GElf_Addr read_vaddr = r_debug_vaddr + addrsize (elfclass);
+
+ void *buffer = NULL;
+ size_t buffer_available = 0;
+ inline int release_buffer (int result)
+ {
+ if (buffer != NULL)
+ (void) (*memory_callback) (dwfl, -1, &buffer, &buffer_available, 0, 0,
+ memory_callback_arg);
+ return result;
+ }
+
+ GElf_Addr addrs[4];
+ inline bool read_addrs (GElf_Addr vaddr, size_t n)
+ {
+ size_t nb = n * addrsize (elfclass); /* Address words -> bytes to read. */
+
+ /* Read a new buffer if the old one doesn't cover these words. */
+ if (buffer == NULL
+ || vaddr < read_vaddr
+ || vaddr - read_vaddr + nb > buffer_available)
+ {
+ release_buffer (0);
+
+ read_vaddr = vaddr;
+ int segndx = INTUSE(dwfl_addrsegment) (dwfl, vaddr, NULL);
+ if (unlikely (segndx < 0)
+ || unlikely (! (*memory_callback) (dwfl, segndx,
+ &buffer, &buffer_available,
+ vaddr, nb, memory_callback_arg)))
+ return true;
+ }
+
+ const union
+ {
+ Elf32_Addr a32[n];
+ Elf64_Addr a64[n];
+ } *in = vaddr - read_vaddr + buffer;
+
+ if (elfclass == ELFCLASS32)
+ {
+ if (elfdata == ELFDATA2MSB)
+ for (size_t i = 0; i < n; ++i)
+ addrs[i] = BE32 (in->a32[i]);
+ else
+ for (size_t i = 0; i < n; ++i)
+ addrs[i] = LE32 (in->a32[i]);
+ }
+ else
+ {
+ if (elfdata == ELFDATA2MSB)
+ for (size_t i = 0; i < n; ++i)
+ addrs[i] = BE64 (in->a64[i]);
+ else
+ for (size_t i = 0; i < n; ++i)
+ addrs[i] = LE64 (in->a64[i]);
+ }
+
+ return false;
+ }
+
+ if (unlikely (read_addrs (read_vaddr, 1)))
+ return release_buffer (-1);
+
+ GElf_Addr next = addrs[0];
+
+ Dwfl_Module **lastmodp = &dwfl->modulelist;
+ int result = 0;
+ while (next != 0)
+ {
+ if (read_addrs (next, 4))
+ return release_buffer (-1);
+
+ GElf_Addr l_addr = addrs[0];
+ GElf_Addr l_name = addrs[1];
+ GElf_Addr l_ld = addrs[2];
+ next = addrs[3];
+
+ /* Fetch the string at the l_name address. */
+ const char *name = NULL;
+ if (buffer != NULL
+ && read_vaddr <= l_name
+ && l_name + 1 - read_vaddr < buffer_available
+ && memchr (l_name - read_vaddr + buffer, '\0',
+ buffer_available - (l_name - read_vaddr)) != NULL)
+ name = l_name - read_vaddr + buffer;
+ else
+ {
+ release_buffer (0);
+ read_vaddr = l_name;
+ int segndx = INTUSE(dwfl_addrsegment) (dwfl, l_name, NULL);
+ if (likely (segndx >= 0)
+ && (*memory_callback) (dwfl, segndx,
+ &buffer, &buffer_available,
+ l_name, 0, memory_callback_arg))
+ name = buffer;
+ }
+
+ if (name != NULL && name[0] == '\0')
+ name = NULL;
+
+ /* If content-sniffing already reported a module covering
+ the same area, find that existing module to adjust.
+ The l_ld address is the only one we know for sure
+ to be within the module's own segments (its .dynamic). */
+ Dwfl_Module *mod;
+ int segndx = INTUSE(dwfl_addrsegment) (dwfl, l_ld, &mod);
+ if (unlikely (segndx < 0))
+ return release_buffer (-1);
+
+ if (mod != NULL)
+ {
+ /* We have a module. We can give it a better name from l_name. */
+ if (name != NULL && mod->name[0] == '[')
+ {
+ char *newname = strdup (basename (name));
+ if (newname != NULL)
+ {
+ free (mod->name);
+ mod->name = newname;
+ }
+ }
+
+ if (name == NULL && mod->name[0] == '/')
+ name = mod->name;
+
+ /* If we don't have a file for it already, we can pre-install
+ the full file name from l_name. Opening the file by this
+ name will be the fallback when no build ID match is found.
+ XXX hook for sysroot */
+ if (name != NULL
+ && mod->main.elf == NULL
+ && mod->main.name == NULL)
+ mod->main.name = strdup (name);
+ }
+ else if (name != NULL)
+ {
+ /* We have to find the file's phdrs to compute along with l_addr
+ what its runtime address boundaries are. */
+
+ // XXX hook for sysroot
+ mod = INTUSE(dwfl_report_elf) (dwfl, basename (name),
+ name, -1, l_addr);
+ }
+
+ if (mod != NULL)
+ {
+ ++result;
+
+ /* Move this module to the end of the list, so that we end
+ up with a list in the same order as the link_map chain. */
+ if (mod->next != NULL)
+ {
+ if (*lastmodp != mod)
+ {
+ lastmodp = &dwfl->modulelist;
+ while (*lastmodp != mod)
+ lastmodp = &(*lastmodp)->next;
+ }
+ *lastmodp = mod->next;
+ mod->next = NULL;
+ while (*lastmodp != NULL)
+ lastmodp = &(*lastmodp)->next;
+ *lastmodp = mod;
+ }
+
+ lastmodp = &mod->next;
+ }
+ }
+
+ return release_buffer (result);
+}
+
+static GElf_Addr
+consider_executable (Dwfl_Module *mod, GElf_Addr at_phdr, GElf_Addr at_entry,
+ uint_fast8_t *elfclass, uint_fast8_t *elfdata,
+ Dwfl_Memory_Callback *memory_callback,
+ void *memory_callback_arg)
+{
+ GElf_Ehdr ehdr;
+ if (unlikely (gelf_getehdr (mod->main.elf, &ehdr) == NULL))
+ return 0;
+
+ if (at_entry != 0)
+ {
+ /* If we have an AT_ENTRY value, reject this executable if
+ its entry point address could not have supplied that. */
+
+ if (ehdr.e_entry == 0)
+ return 0;
+
+ if (mod->e_type == ET_EXEC)
+ {
+ if (ehdr.e_entry != at_entry)
+ return 0;
+ }
+ else
+ {
+ /* It could be a PIE. */
+ }
+ }
+
+ // XXX this could be saved in the file cache: phdr vaddr, DT_DEBUG d_val vaddr
+ /* Find the vaddr of the DT_DEBUG's d_ptr. This is the memory
+ address where &r_debug was written at runtime. */
+ GElf_Xword align = mod->dwfl->segment_align;
+ GElf_Addr d_val_vaddr = 0;
+ for (uint_fast16_t i = 0; i < ehdr.e_phnum; ++i)
+ {
+ GElf_Phdr phdr_mem;
+ GElf_Phdr *phdr = gelf_getphdr (mod->main.elf, i, &phdr_mem);
+ if (phdr == NULL)
+ break;
+
+ if (phdr->p_align > 1 && (align == 0 || phdr->p_align < align))
+ align = phdr->p_align;
+
+ if (at_phdr != 0
+ && phdr->p_type == PT_LOAD
+ && (phdr->p_offset & -align) == (ehdr.e_phoff & -align))
+ {
+ /* This is the segment that would map the phdrs.
+ If we have an AT_PHDR value, reject this executable
+ if its phdr mapping could not have supplied that. */
+ if (mod->e_type == ET_EXEC)
+ {
+ if (ehdr.e_phoff - phdr->p_offset + phdr->p_vaddr != at_phdr)
+ return 0;
+ }
+ else
+ {
+ /* It could be a PIE. If the AT_PHDR value and our
+ phdr address don't match modulo ALIGN, then this
+ could not have been the right PIE. */
+ if (((ehdr.e_phoff - phdr->p_offset + phdr->p_vaddr) & -align)
+ != (at_phdr & -align))
+ return 0;
+
+ /* Calculate the bias applied to the PIE's p_vaddr values. */
+ GElf_Addr bias = (at_phdr - (ehdr.e_phoff - phdr->p_offset
+ + phdr->p_vaddr));
+
+ /* Final sanity check: if we have an AT_ENTRY value,
+ reject this PIE unless its biased e_entry matches. */
+ if (at_entry != 0 && at_entry != ehdr.e_entry + bias)
+ return 0;
+
+ /* If we're changing the module's address range,
+ we've just invalidated the module lookup table. */
+ if (bias != mod->main.bias)
+ {
+ mod->low_addr -= mod->main.bias;
+ mod->high_addr -= mod->main.bias;
+ mod->main.bias = bias;
+ mod->low_addr += bias;
+ mod->high_addr += bias;
+
+ free (mod->dwfl->lookup_module);
+ mod->dwfl->lookup_module = NULL;
+ }
+ }
+ }
+
+ if (phdr->p_type == PT_DYNAMIC)
+ {
+ Elf_Data *data = elf_getdata_rawchunk (mod->main.elf, phdr->p_offset,
+ phdr->p_filesz, ELF_T_DYN);
+ if (data == NULL)
+ continue;
+ const size_t entsize = gelf_fsize (mod->main.elf,
+ ELF_T_DYN, 1, EV_CURRENT);
+ const size_t n = data->d_size / entsize;
+ for (size_t j = 0; j < n; ++j)
+ {
+ GElf_Dyn dyn_mem;
+ GElf_Dyn *dyn = gelf_getdyn (data, j, &dyn_mem);
+ if (dyn != NULL && dyn->d_tag == DT_DEBUG)
+ {
+ d_val_vaddr = phdr->p_vaddr + entsize * j + entsize / 2;
+ break;
+ }
+ }
+ }
+ }
+
+ if (d_val_vaddr != 0)
+ {
+ /* Now we have the final address from which to read &r_debug. */
+ d_val_vaddr += mod->main.bias;
+
+ void *buffer = NULL;
+ size_t buffer_available = addrsize (ehdr.e_ident[EI_CLASS]);
+
+ Dwfl_Module *m;
+ int segndx = INTUSE(dwfl_addrsegment) (mod->dwfl, d_val_vaddr, &m);
+ assert (m == mod);
+
+ if ((*memory_callback) (mod->dwfl, segndx,
+ &buffer, &buffer_available,
+ d_val_vaddr, buffer_available,
+ memory_callback_arg))
+ {
+ const union
+ {
+ Elf32_Addr a32;
+ Elf64_Addr a64;
+ } *u = buffer;
+
+ GElf_Addr vaddr;
+ if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+ vaddr = (ehdr.e_ident[EI_DATA] == ELFDATA2MSB
+ ? BE32 (u->a32) : LE32 (u->a32));
+ else
+ vaddr = (ehdr.e_ident[EI_DATA] == ELFDATA2MSB
+ ? BE64 (u->a64) : LE64 (u->a64));
+
+ (*memory_callback) (mod->dwfl, -1, &buffer, &buffer_available, 0, 0,
+ memory_callback_arg);
+
+
+ if (*elfclass == ELFCLASSNONE)
+ *elfclass = ehdr.e_ident[EI_CLASS];
+ else if (*elfclass != ehdr.e_ident[EI_CLASS])
+ return 0;
+
+ if (*elfdata == ELFDATANONE)
+ *elfdata = ehdr.e_ident[EI_DATA];
+ else if (*elfdata != ehdr.e_ident[EI_DATA])
+ return 0;
+
+ return vaddr;
+ }
+ }
+
+ return 0;
+}
+
+/* Try to find an existing executable module with a DT_DEBUG. */
+static GElf_Addr
+find_executable (Dwfl *dwfl, GElf_Addr at_phdr, GElf_Addr at_entry,
+ uint_fast8_t *elfclass, uint_fast8_t *elfdata,
+ Dwfl_Memory_Callback *memory_callback,
+ void *memory_callback_arg)
+{
+ for (Dwfl_Module *mod = dwfl->modulelist; mod != NULL; mod = mod->next)
+ if (mod->main.elf != NULL)
+ {
+ GElf_Addr r_debug_vaddr = consider_executable (mod, at_phdr, at_entry,
+ elfclass, elfdata,
+ memory_callback,
+ memory_callback_arg);
+ if (r_debug_vaddr != 0)
+ return r_debug_vaddr;
+ }
+
+ return 0;
+}
+
+
+int
+dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
+ Dwfl_Memory_Callback *memory_callback,
+ void *memory_callback_arg)
+{
+ GElf_Addr r_debug_vaddr = 0;
+
+ uint_fast8_t elfclass = ELFCLASSNONE;
+ uint_fast8_t elfdata = ELFDATANONE;
+ if (likely (auxv != NULL)
+ && likely (auxv_format_probe (auxv, auxv_size, &elfclass, &elfdata)))
+ {
+ GElf_Addr entry = 0;
+ GElf_Addr phdr = 0;
+ GElf_Xword phent = 0;
+ GElf_Xword phnum = 0;
+
+#define AUXV_SCAN(NN, BL) do \
+ { \
+ const Elf##NN##_auxv_t *av = auxv; \
+ for (size_t i = 0; i < auxv_size / sizeof av[0]; ++i) \
+ { \
+ Elf##NN##_Addr val = BL##NN (av[i].a_un.a_val); \
+ if (av[i].a_type == BL##NN (AT_ENTRY)) \
+ entry = val; \
+ else if (av[i].a_type == BL##NN (AT_PHDR)) \
+ phdr = val; \
+ else if (av[i].a_type == BL##NN (AT_PHNUM)) \
+ phnum = val; \
+ else if (av[i].a_type == BL##NN (AT_PHENT)) \
+ phent = val; \
+ else if (av[i].a_type == BL##NN (AT_PAGESZ)) \
+ { \
+ if (val > 1 \
+ && (dwfl->segment_align == 0 \
+ || val < dwfl->segment_align)) \
+ dwfl->segment_align = val; \
+ } \
+ } \
+ } \
+ while (0)
+
+ if (elfclass == ELFCLASS32)
+ {
+ if (elfdata == ELFDATA2MSB)
+ AUXV_SCAN (32, BE);
+ else
+ AUXV_SCAN (32, LE);
+ }
+ else
+ {
+ if (elfdata == ELFDATA2MSB)
+ AUXV_SCAN (64, BE);
+ else
+ AUXV_SCAN (64, LE);
+ }
+
+ /* If we found the phdr dimensions, search phdrs for PT_DYNAMIC. */
+ GElf_Addr dyn_vaddr = 0;
+ GElf_Xword dyn_filesz = 0;
+ if (phdr != 0 && phnum != 0)
+ {
+ Dwfl_Module *phdr_mod;
+ int phdr_segndx = INTUSE(dwfl_addrsegment) (dwfl, phdr, &phdr_mod);
+ Elf_Data in =
+ {
+ .d_type = ELF_T_PHDR,
+ .d_version = EV_CURRENT,
+ .d_size = phnum * phent,
+ .d_buf = NULL
+ };
+ if ((*memory_callback) (dwfl, phdr_segndx, &in.d_buf, &in.d_size,
+ phdr, phnum * phent, memory_callback_arg))
+ {
+ union
+ {
+ Elf32_Phdr p32;
+ Elf64_Phdr p64;
+ char data[phnum * phent];
+ } buf;
+ Elf_Data out =
+ {
+ .d_type = ELF_T_PHDR,
+ .d_version = EV_CURRENT,
+ .d_size = phnum * phent,
+ .d_buf = &buf
+ };
+ in.d_size = out.d_size;
+ if (likely ((elfclass == ELFCLASS32
+ ? elf32_xlatetom : elf64_xlatetom)
+ (&out, &in, elfdata) != NULL))
+ {
+ /* We are looking for PT_DYNAMIC. */
+ const union
+ {
+ Elf32_Phdr p32[phnum];
+ Elf64_Phdr p64[phnum];
+ } *u = (void *) &buf;
+ if (elfclass == ELFCLASS32)
+ {
+ for (size_t i = 0; i < phnum; ++i)
+ if (u->p32[i].p_type == PT_DYNAMIC)
+ {
+ dyn_vaddr = u->p32[i].p_vaddr;
+ dyn_filesz = u->p32[i].p_filesz;
+ break;
+ }
+ }
+ else
+ {
+ for (size_t i = 0; i < phnum; ++i)
+ if (u->p64[i].p_type == PT_DYNAMIC)
+ {
+ dyn_vaddr = u->p64[i].p_vaddr;
+ dyn_filesz = u->p64[i].p_filesz;
+ break;
+ }
+ }
+ }
+
+ (*memory_callback) (dwfl, -1, &in.d_buf, &in.d_size, 0, 0,
+ memory_callback_arg);
+ }
+ else
+ /* We could not read the executable's phdrs from the
+ memory image. If we have a presupplied executable,
+ we can still use the AT_PHDR and AT_ENTRY values to
+ verify it, and to adjust its bias if it's a PIE.
+
+ If there was an ET_EXEC module presupplied that contains
+ the AT_PHDR address, then we only consider that one.
+ We'll either accept it if its phdr location and e_entry
+ make sense or reject it if they don't. If there is no
+ presupplied ET_EXEC, then look for a presupplied module,
+ which might be a PIE (ET_DYN) that needs its bias adjusted. */
+ r_debug_vaddr = ((phdr_mod == NULL
+ || phdr_mod->main.elf == NULL
+ || phdr_mod->e_type != ET_EXEC)
+ ? find_executable (dwfl, phdr, entry,
+ &elfclass, &elfdata,
+ memory_callback,
+ memory_callback_arg)
+ : consider_executable (phdr_mod, phdr, entry,
+ &elfclass, &elfdata,
+ memory_callback,
+ memory_callback_arg));
+ }
+
+ /* If we found PT_DYNAMIC, search it for DT_DEBUG. */
+ if (dyn_filesz != 0)
+ {
+ Elf_Data in =
+ {
+ .d_type = ELF_T_DYN,
+ .d_version = EV_CURRENT,
+ .d_size = dyn_filesz,
+ .d_buf = NULL
+ };
+ int dyn_segndx = dwfl_addrsegment (dwfl, dyn_vaddr, NULL);
+ if ((*memory_callback) (dwfl, dyn_segndx, &in.d_buf, &in.d_size,
+ dyn_vaddr, dyn_filesz, memory_callback_arg))
+ {
+ union
+ {
+ Elf32_Dyn d32;
+ Elf64_Dyn d64;
+ char data[dyn_filesz];
+ } buf;
+ Elf_Data out =
+ {
+ .d_type = ELF_T_DYN,
+ .d_version = EV_CURRENT,
+ .d_size = dyn_filesz,
+ .d_buf = &buf
+ };
+ in.d_size = out.d_size;
+ if (likely ((elfclass == ELFCLASS32
+ ? elf32_xlatetom : elf64_xlatetom)
+ (&out, &in, elfdata) != NULL))
+ {
+ /* We are looking for PT_DYNAMIC. */
+ const union
+ {
+ Elf32_Dyn d32[dyn_filesz / sizeof (Elf32_Dyn)];
+ Elf64_Dyn d64[dyn_filesz / sizeof (Elf64_Dyn)];
+ } *u = (void *) &buf;
+ if (elfclass == ELFCLASS32)
+ {
+ size_t n = dyn_filesz / sizeof (Elf32_Dyn);
+ for (size_t i = 0; i < n; ++i)
+ if (u->d32[i].d_tag == DT_DEBUG)
+ {
+ r_debug_vaddr = u->d32[i].d_un.d_val;
+ break;
+ }
+ }
+ else
+ {
+ size_t n = dyn_filesz / sizeof (Elf64_Dyn);
+ for (size_t i = 0; i < n; ++i)
+ if (u->d64[i].d_tag == DT_DEBUG)
+ {
+ r_debug_vaddr = u->d64[i].d_un.d_val;
+ break;
+ }
+ }
+ }
+
+ (*memory_callback) (dwfl, -1, &in.d_buf, &in.d_size, 0, 0,
+ memory_callback_arg);
+ }
+ }
+ }
+ else
+ /* We have to look for a presupplied executable file to determine
+ the vaddr of its dynamic section and DT_DEBUG therein. */
+ r_debug_vaddr = find_executable (dwfl, 0, 0, &elfclass, &elfdata,
+ memory_callback, memory_callback_arg);
+
+ if (r_debug_vaddr == 0)
+ return 0;
+
+ /* For following pointers from struct link_map, we will use an
+ integrated memory access callback that can consult module text
+ elided from the core file. This is necessary when the l_name
+ pointer for the dynamic linker's own entry is a pointer into the
+ executable's .interp section. */
+ struct integrated_memory_callback mcb =
+ {
+ .memory_callback = memory_callback,
+ .memory_callback_arg = memory_callback_arg
+ };
+
+ /* Now we can follow the dynamic linker's library list. */
+ return report_r_debug (elfclass, elfdata, dwfl, r_debug_vaddr,
+ &integrated_memory_callback, &mcb);
+}
+INTDEF (dwfl_link_map_report)
diff --git a/libdwfl/segment.c b/libdwfl/segment.c
new file mode 100644
index 00000000..3c1f8bc6
--- /dev/null
+++ b/libdwfl/segment.c
@@ -0,0 +1,300 @@
+/* Manage address space lookup table for libdwfl.
+ Copyright (C) 2008 Red Hat, Inc.
+ This file is part of Red Hat elfutils.
+
+ Red Hat elfutils is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by the
+ Free Software Foundation; version 2 of the License.
+
+ Red Hat 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 a copy of the GNU General Public License along
+ with Red Hat elfutils; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
+
+ In addition, as a special exception, Red Hat, Inc. gives You the
+ additional right to link the code of Red Hat elfutils with code licensed
+ under any Open Source Initiative certified open source license
+ (http://www.opensource.org/licenses/index.php) which requires the
+ distribution of source code with any binary distribution and to
+ distribute linked combinations of the two. Non-GPL Code permitted under
+ this exception must only link to the code of Red Hat elfutils through
+ those well defined interfaces identified in the file named EXCEPTION
+ found in the source code files (the "Approved Interfaces"). The files
+ of Non-GPL Code may instantiate templates or use macros or inline
+ functions from the Approved Interfaces without causing the resulting
+ work to be covered by the GNU General Public License. Only Red Hat,
+ Inc. may make changes or additions to the list of Approved Interfaces.
+ Red Hat's grant of this exception is conditioned upon your not adding
+ any new exceptions. If you wish to add a new Approved Interface or
+ exception, please contact Red Hat. You must obey the GNU General Public
+ License in all respects for all of the Red Hat elfutils code and other
+ code used in conjunction with Red Hat elfutils except the Non-GPL Code
+ covered by this exception. If you modify this file, you may extend this
+ exception to your version of the file, but you are not obligated to do
+ so. If you do not wish to provide this exception without modification,
+ you must delete this exception statement from your version and license
+ this file solely under the GPL without exception.
+
+ Red Hat elfutils is an included package of the Open Invention Network.
+ An included package of the Open Invention Network is a package for which
+ Open Invention Network licensees cross-license their patents. No patent
+ license is granted, either expressly or impliedly, by designation as an
+ included package. Should you wish to participate in the Open Invention
+ Network licensing program, please visit www.openinventionnetwork.com
+ <http://www.openinventionnetwork.com>. */
+
+#include "libdwflP.h"
+
+static GElf_Addr
+segment_start (Dwfl *dwfl, GElf_Addr start)
+{
+ if (dwfl->segment_align > 1)
+ start &= -dwfl->segment_align;
+ return start;
+}
+
+static GElf_Addr
+segment_end (Dwfl *dwfl, GElf_Addr end)
+{
+ if (dwfl->segment_align > 1)
+ end = (end + dwfl->segment_align - 1) & -dwfl->segment_align;
+ return end;
+}
+
+static bool
+insert (Dwfl *dwfl, size_t i, GElf_Addr start, GElf_Addr end, int segndx)
+{
+ bool need_start = (i == 0 || dwfl->lookup_addr[i - 1] != start);
+ bool need_end = (i >= dwfl->lookup_elts || dwfl->lookup_addr[i + 1] != end);
+ size_t need = need_start + need_end;
+ if (need == 0)
+ return false;
+
+ if (dwfl->lookup_alloc - dwfl->lookup_elts < need)
+ {
+ size_t n = dwfl->lookup_alloc == 0 ? 16 : dwfl->lookup_alloc * 2;
+ GElf_Addr *naddr = realloc (dwfl->lookup_addr, sizeof naddr[0] * n);
+ if (unlikely (naddr == NULL))
+ return true;
+ int *nsegndx = realloc (dwfl->lookup_segndx, sizeof nsegndx[0] * n);
+ if (unlikely (nsegndx == NULL))
+ {
+ free (naddr);
+ return true;
+ }
+ dwfl->lookup_alloc = n;
+ dwfl->lookup_addr = naddr;
+ dwfl->lookup_segndx = nsegndx;
+ }
+
+ if (unlikely (i < dwfl->lookup_elts))
+ {
+ memcpy (&dwfl->lookup_addr[i + need], &dwfl->lookup_addr[i],
+ need * sizeof dwfl->lookup_addr[0]);
+ memcpy (&dwfl->lookup_segndx[i + need], &dwfl->lookup_segndx[i],
+ need * sizeof dwfl->lookup_segndx[0]);
+ if (dwfl->lookup_module != NULL)
+ memcpy (&dwfl->lookup_module[i + need], &dwfl->lookup_module[i],
+ need * sizeof dwfl->lookup_module[0]);
+ }
+
+ if (need_start)
+ {
+ dwfl->lookup_addr[i] = start;
+ dwfl->lookup_segndx[i] = segndx;
+ ++i;
+ }
+ else
+ dwfl->lookup_segndx[i - 1] = segndx;
+
+ if (need_end)
+ {
+ dwfl->lookup_addr[i] = end;
+ dwfl->lookup_segndx[i] = -1;
+ }
+
+ dwfl->lookup_elts += need;
+
+ return false;
+}
+
+static int
+lookup (Dwfl *dwfl, GElf_Addr address, int hint)
+{
+ if (hint >= 0
+ && address >= dwfl->lookup_addr[hint]
+ && ((size_t) hint + 1 == dwfl->lookup_elts
+ || address <= dwfl->lookup_addr[hint + 1]))
+ return hint;
+
+ /* Do binary search on the array indexed by module load address. */
+ size_t l = 0, u = dwfl->lookup_elts;
+ while (l < u)
+ {
+ size_t idx = (l + u) / 2;
+ if (address < dwfl->lookup_addr[idx])
+ u = idx;
+ else
+ {
+ l = idx + 1;
+ if (l == dwfl->lookup_elts || address < dwfl->lookup_addr[l])
+ return idx;
+ }
+ }
+
+ return -1;
+}
+
+static bool
+reify_segments (Dwfl *dwfl)
+{
+ int hint = -1;
+ for (Dwfl_Module *mod = dwfl->modulelist; mod != NULL; mod = mod->next)
+ if (! mod->gc)
+ {
+ const GElf_Addr start = segment_start (dwfl, mod->low_addr);
+ const GElf_Addr end = segment_end (dwfl, mod->high_addr);
+
+ int idx = lookup (dwfl, start, hint);
+ if (unlikely (idx < 0))
+ {
+ /* Module starts below any segment. Insert a low one. */
+ if (unlikely (insert (dwfl, 0, start, end, -1)))
+ return true;
+ idx = 0;
+ }
+ else if (dwfl->lookup_addr[idx] > start)
+ {
+ /* The module starts in the middle of this segment. Split it. */
+ if (unlikely (insert (dwfl, idx + 1, start, end,
+ dwfl->lookup_segndx[idx])))
+ return true;
+ ++idx;
+ }
+
+ if ((size_t) idx + 1 < dwfl->lookup_elts
+ && end < dwfl->lookup_addr[idx + 1]
+ /* The module ends in the middle of this segment. Split it. */
+ && unlikely (insert (dwfl, idx + 1,
+ end, dwfl->lookup_addr[idx + 1], -1)))
+ return true;
+
+ if (dwfl->lookup_module == NULL)
+ {
+ dwfl->lookup_module = calloc (dwfl->lookup_alloc,
+ sizeof dwfl->lookup_module[0]);
+ if (unlikely (dwfl->lookup_module == NULL))
+ return true;
+ }
+
+ /* Cache a backpointer in the module. */
+ mod->segment = idx;
+
+ /* Put MOD in the table for each segment that's inside it. */
+ do
+ dwfl->lookup_module[idx++] = mod;
+ while ((size_t) idx < dwfl->lookup_elts
+ && dwfl->lookup_addr[idx] < end);
+ hint = (size_t) idx < dwfl->lookup_elts ? idx : -1;
+ }
+
+ return false;
+}
+
+int
+dwfl_addrsegment (Dwfl *dwfl, Dwarf_Addr address, Dwfl_Module **mod)
+{
+ if (unlikely (dwfl == NULL))
+ return -1;
+
+ if (unlikely (dwfl->lookup_module == NULL)
+ && mod != NULL
+ && unlikely (reify_segments (dwfl)))
+ {
+ __libdwfl_seterrno (DWFL_E_NOMEM);
+ return -1;
+ }
+
+ int idx = lookup (dwfl, address, -1);
+ if (likely (mod != NULL))
+ {
+ if (unlikely (idx < 0) || unlikely (dwfl->lookup_module == NULL))
+ *mod = NULL;
+ else
+ {
+ *mod = dwfl->lookup_module[idx];
+
+ /* If this segment does not have a module, but the address is
+ the upper boundary of the previous segment's module, use that. */
+ if (*mod == NULL && idx > 0 && dwfl->lookup_addr[idx] == address)
+ {
+ *mod = dwfl->lookup_module[idx - 1];
+ if (*mod != NULL && (*mod)->high_addr != address)
+ *mod = NULL;
+ }
+ }
+ }
+
+ if (likely (idx >= 0))
+ /* Translate internal segment table index to user segment index. */
+ idx = dwfl->lookup_segndx[idx];
+
+ return idx;
+}
+INTDEF (dwfl_addrsegment)
+
+int
+dwfl_report_segment (Dwfl *dwfl, int ndx, const GElf_Phdr *phdr, GElf_Addr bias,
+ const void *ident)
+{
+ if (dwfl == NULL)
+ return -1;
+
+ if (ndx < 0)
+ ndx = dwfl->lookup_tail_ndx;
+
+ if (phdr->p_align > 1 && (dwfl->segment_align <= 1 ||
+ phdr->p_align < dwfl->segment_align))
+ dwfl->segment_align = phdr->p_align;
+
+ GElf_Addr start = segment_start (dwfl, bias + phdr->p_vaddr);
+ GElf_Addr end = segment_end (dwfl, bias + phdr->p_vaddr + phdr->p_memsz);
+
+ /* Coalesce into the last one if contiguous and matching. */
+ if (ndx != dwfl->lookup_tail_ndx
+ || ident == NULL
+ || ident != dwfl->lookup_tail_ident
+ || start != dwfl->lookup_tail_vaddr
+ || phdr->p_offset != dwfl->lookup_tail_offset)
+ {
+ /* Normally just appending keeps us sorted. */
+
+ size_t i = dwfl->lookup_elts;
+ while (i > 0 && unlikely (start < dwfl->lookup_addr[i - 1]))
+ --i;
+
+ if (unlikely (insert (dwfl, i, start, end, ndx)))
+ {
+ __libdwfl_seterrno (DWFL_E_NOMEM);
+ return -1;
+ }
+ }
+
+ dwfl->lookup_tail_ident = ident;
+ dwfl->lookup_tail_vaddr = end;
+ dwfl->lookup_tail_offset = end - bias - phdr->p_vaddr + phdr->p_offset;
+ dwfl->lookup_tail_ndx = ndx + 1;
+
+ if (unlikely (dwfl->lookup_module != NULL))
+ {
+ free (dwfl->lookup_module);
+ dwfl->lookup_module = NULL;
+ }
+
+ return ndx;
+}
+INTDEF (dwfl_report_segment)
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 0e4a9284..d258de8d 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,85 +1,43 @@
-2008-08-15 Ulrich Drepper <drepper@redhat.com>
+2008-08-25 Roland McGrath <roland@redhat.com>
- * libelfP.h (LIBELF_CHECKED_LOCK): Use dummy definition also if
- USE_TLS is not defined.
+ * Makefile.am (libelf_so_LDLIBS): New variable.
+ (libelf.so): Use it in the link.
-2008-08-05 Petr Machata <pmachata@redhat.com>
+2008-08-21 Petr Machata <pmachata@redhat.com>
- * elf_getdata.c, libelfP.h (__elf_getdata_internal):
- Add lockstat argument in a fashion similar to elf32_getehdr below.
- * elf32_updatenull.c (__elf32_updatenull, __elf64_updatenull):
- Call __elf32_getshdr_internal and __elf64_getshdr_internal
- explicitly, not via INTUSE.
- Call __elf_getdata_internal explicitly, not via INTUSE.
+ * elf_getdata.c, libelfP.h
+ (__elf_getdata_internal): Rename to __elf_getdata_rdlock.
+ (__libelf_set_rawdata_wrlock): New function.
+ (__libelf_set_rawdata): Make it a wrapper that calls *_wrlock.
+ * elf32_updatenull.c, libelfP.h
+ (__elfNN_updatenull): Rename to __elfNN_updatenull_wrlock.
-2008-08-05 Petr Machata <pmachata@redhat.com>
+2008-08-21 Petr Machata <pmachata@redhat.com>
- * gelf_getehdr.c, libelfP.h (__gelf_getehdr_internal):
- Add lockstat argument in a fashion similar to elf32_getehdr below.
- * elf_getdata.c, libelfP.h (__libelf_set_rawdata):
- Add lockstat argument.
- Call __gelf_getehdr_internal explicitly, not via INTUSE.
- Call rwlock_to_wrlock instead of RWLOCK_UNLOCK/_WRLOCK sequence.
- (elf_getdata): Pass lockstat argument to __libelf_set_rawdata.
- * elf32_updatenull.c, libelfP.h
- (__elf32_updatenull, __elf64_updatenull):
- Add lockstat argument in a fashion similar to elf32_getehdr below.
- Pass lockstat argument to __libelf_set_rawdata.
- * elf_strptr.c (elf_strptr):
- Pass lockstat argument to __libelf_set_rawdata.
- * elf_update.c (elf_update):
- Pass lockstat argument to __elf32_updatenull and
- __elf64_updatenull.
-
-2008-08-05 Petr Machata <pmachata@redhat.com>
-
- * common.h (rwlock_to_wrlock, rwlock_from_wrlock): New functions.
* elf32_getphdr.c, libelfP.h
- (__elf32_getphdr_internal, __elf64_getphdr_internal):
- Add lockstat argument in a fashion similar to elf32_getehdr below.
- * gelf_getphdr.c (gelf_getphdr): Call __elf32_getphdr_internal and
- __elf32_getphdr_internal explicitly, not via INTUSE.
+ (__elfNN_getphdr_internal): Drop. Move __elfNN_getphdr_internal
+ code to __elfNN_getphdr_wrlock.
+ (__elfNN_getphdr_rdlock, __elfNN_getphdr_wrlock): New functions.
+ (__elfNN_getphdr_rdlock, __elfNN_getphdr_wrlock): Make these
+ wrappers of getphdr_impl.
-2008-08-05 Petr Machata <pmachata@redhat.com>
+2008-08-21 Petr Machata <pmachata@redhat.com>
- * libelfP.h (lockstat_t): New enum that describes what type of
- lock is currently being held. To be used by _internal functions.
* elf32_getehdr.c, libelfP.h
- (__elf32_getehdr_internal, __elf64_getehdr_internal):
- Take extra lockstat argument.
- Move "meat" of the getehdr functinonality here.
- * elf32_getehdr.c (elf32_getehdr, elf64_getehdr):
- Make this a simple wrapper that calls _internal variant.
- * elf32_updatenull.c: Call __elf32_getehdr_internal explicitly,
- not via INTUSE.
- * elf32_getshdr.c, libelfP.h
- (__elf32_getshdr_internal, __elf64_getshdr_internal):
- Add lockstat argument in a fashion similar to elf32_getehdr.
- * elf_getshnum.c, libelfP.h (__elf_getshnum_internal):
- Likewise.
- * gelf_getshdr.c (gelf_getshdr): Call __elf32_getshdr_internal,
- __elf64_getshdr_internal explicitly, not via INTUSE.
-
-2008-08-04 Petr Machata <pmachata@redhat.com>
-
- * common.h, elf32_getehdr.c, elf32_getphdr.c, elf32_getshdr.c,
- elf32_newehdr.c, elf32_newphdr.c, elf32_offscn.c, elf_begin.c,
- elf_clone.c, elf_cntl.c, elf_end.c, elf_getarsym.c, elf_getdata.c,
- elf_getscn.c, elf_getshnum.c, elf_getshstrndx.c, elf_newdata.c,
- elf_newscn.c, elf_nextscn.c, elf_readall.c, elf_strptr.c,
- elf_update.c, gelf_getauxv.c, gelf_getdyn.c, gelf_getehdr.c,
- gelf_getlib.c, gelf_getmove.c, gelf_getnote.c, gelf_getphdr.c,
- gelf_getrel.c, gelf_getrela.c, gelf_getshdr.c, gelf_getsym.c,
- gelf_getsyminfo.c, gelf_getsymshndx.c, gelf_getverdaux.c,
- gelf_getverdef.c, gelf_getvernaux.c, gelf_getverneed.c,
- gelf_getversym.c, gelf_update_auxv.c, gelf_update_dyn.c,
- gelf_update_ehdr.c, gelf_update_lib.c, gelf_update_move.c,
- gelf_update_phdr.c, gelf_update_rel.c, gelf_update_rela.c,
- gelf_update_sym.c, gelf_update_syminfo.c, gelf_update_symshndx.c,
- gelf_update_verdaux.c, gelf_update_verdef.c,
- gelf_update_vernaux.c, gelf_update_verneed.c,
- gelf_update_versym.c:
- Change rwlock_{rd,wr,un}lock calls to RWLOCK_{RD,WR,UN}LOCK.
+ (__elfNN_getehdr_internal): Rename to __elfNN_getehdr_rdlock.
+ * gelf_getehdr, libelfP.h:
+ (__gelf_getehdr_internal): Rename to __gelf_getehdr_rdlock.
+
+2008-08-21 Petr Machata <pmachata@redhat.com>
+
+ * elf32_getshdr.c
+ (__elfNN_getshdr_internal): Drop.
+ (load_shdr_rwlock, scn_valid): New functions, contain bits of
+ behaviour from __elfNN_getshdr_internal.
+ (__elfNN_getshdr_rdlock, __elfNN_getshdr_wrlock): Replacements for
+ dropped _internal functions above.
+ * elf_getshnum.c
+ (__elf_getshnum_internal): Rename to __elf_getshnum_rdlock.
2008-08-04 Petr Machata <pmachata@redhat.com>
diff --git a/libelf/common.h b/libelf/common.h
index c00bfbc5..8eef2a7a 100644
--- a/libelf/common.h
+++ b/libelf/common.h
@@ -118,7 +118,7 @@ static void
__attribute__ ((unused))
libelf_acquire_all (Elf *elf)
{
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->kind == ELF_K_AR)
{
@@ -150,33 +150,7 @@ libelf_release_all (Elf *elf)
}
}
- RWLOCK_UNLOCK (elf->lock);
-}
-
-/* Convert given lock LOCK with lock state FROM to lock state
- LS_WRLOCKED. */
-static void
-__attribute__ ((unused))
-rwlock_to_wrlock(lockstat_t from, rwlock_define (,*lock))
-{
- if (from == LS_WRLOCKED)
- return;
- if (from == LS_RDLOCKED)
- RWLOCK_UNLOCK (*lock);
- RWLOCK_WRLOCK (*lock);
-}
-
-/* Convert given lock LOCK with lock state LS_WRLOCKED to lock state
- TO. */
-static void
-__attribute__ ((unused))
-rwlock_from_wrlock(lockstat_t to, rwlock_define (,*lock))
-{
- if (to == LS_WRLOCKED)
- return;
- RWLOCK_UNLOCK (*lock);
- if (to == LS_RDLOCKED)
- RWLOCK_RDLOCK (*lock);
+ rwlock_unlock (elf->lock);
}
diff --git a/libelf/elf32_checksum.c b/libelf/elf32_checksum.c
index ddc978dc..0e4ab9f7 100644
--- a/libelf/elf32_checksum.c
+++ b/libelf/elf32_checksum.c
@@ -150,7 +150,7 @@ elfw2(LIBELFBITS,checksum) (elf)
}
/* Iterate through the list of data blocks. */
- while ((data = __elf_getdata_internal (scn, data, LS_UNLOCKED)) != NULL)
+ while ((data = INTUSE(elf_getdata) (scn, data)) != NULL)
/* If the file byte order is the same as the host byte order
process the buffer directly. If the data is just a stream
of bytes which the library will not convert we can use it
diff --git a/libelf/elf32_getehdr.c b/libelf/elf32_getehdr.c
index e5c218a7..d62e255b 100644
--- a/libelf/elf32_getehdr.c
+++ b/libelf/elf32_getehdr.c
@@ -63,9 +63,8 @@
ElfW2(LIBELFBITS,Ehdr) *
-__elfw2(LIBELFBITS,getehdr_internal) (elf, locked)
+__elfw2(LIBELFBITS,getehdr_rdlock) (elf)
Elf *elf;
- lockstat_t locked;
{
ElfW2(LIBELFBITS,Ehdr) *result;
@@ -78,9 +77,6 @@ __elfw2(LIBELFBITS,getehdr_internal) (elf, locked)
return NULL;
}
- if (locked == LS_UNLOCKED)
- RWLOCK_RDLOCK (elf->lock);
-
if (elf->class == 0)
elf->class = ELFW(ELFCLASS,LIBELFBITS);
else if (unlikely (elf->class != ELFW(ELFCLASS,LIBELFBITS)))
@@ -93,9 +89,6 @@ __elfw2(LIBELFBITS,getehdr_internal) (elf, locked)
result = elf->state.ELFW(elf,LIBELFBITS).ehdr;
out:
- if (locked == LS_UNLOCKED)
- RWLOCK_UNLOCK (elf->lock);
-
return result;
}
@@ -103,8 +96,13 @@ ElfW2(LIBELFBITS,Ehdr) *
elfw2(LIBELFBITS,getehdr) (elf)
Elf *elf;
{
+ ElfW2(LIBELFBITS,Ehdr) *result;
if (elf == NULL)
return NULL;
- return __elfw2(LIBELFBITS,getehdr_internal) (elf, LS_UNLOCKED);
+ rwlock_rdlock (elf->lock);
+ result = __elfw2(LIBELFBITS,getehdr_rdlock) (elf);
+ rwlock_unlock (elf->lock);
+
+ return result;
}
diff --git a/libelf/elf32_getphdr.c b/libelf/elf32_getphdr.c
index 972132e4..c0b28aaf 100644
--- a/libelf/elf32_getphdr.c
+++ b/libelf/elf32_getphdr.c
@@ -53,6 +53,7 @@
#endif
#include <errno.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
@@ -65,23 +66,12 @@
# define LIBELFBITS 32
#endif
-
ElfW2(LIBELFBITS,Phdr) *
-__elfw2(LIBELFBITS,getphdr_internal) (elf, locked)
- lockstat_t locked;
+__elfw2(LIBELFBITS,getphdr_wrlock) (elf)
Elf *elf;
{
ElfW2(LIBELFBITS,Phdr) *result;
- if (elf == NULL)
- return NULL;
-
- if (unlikely (elf->kind != ELF_K_ELF))
- {
- __libelf_seterrno (ELF_E_INVALID_HANDLE);
- return NULL;
- }
-
/* If the program header entry has already been filled in the code
below must already have been run. So the class is set, too. No
need to waste any more time here. */
@@ -89,8 +79,6 @@ __elfw2(LIBELFBITS,getphdr_internal) (elf, locked)
if (likely (result != NULL))
return result;
- rwlock_to_wrlock (locked, &elf->lock);
-
if (elf->class == 0)
elf->class = ELFW(ELFCLASS,LIBELFBITS);
else if (elf->class != ELFW(ELFCLASS,LIBELFBITS))
@@ -235,7 +223,6 @@ __elfw2(LIBELFBITS,getphdr_internal) (elf, locked)
}
out:
- rwlock_from_wrlock (locked, &elf->lock);
return result;
}
@@ -243,8 +230,12 @@ ElfW2(LIBELFBITS,Phdr) *
elfw2(LIBELFBITS,getphdr) (elf)
Elf *elf;
{
- if (elf == NULL)
- return NULL;
+ ElfW2(LIBELFBITS,Phdr) *result;
- return __elfw2(LIBELFBITS,getphdr_internal) (elf, LS_UNLOCKED);
+ rwlock_wrlock (elf->lock);
+ result = __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
+ rwlock_unlock (elf->lock);
+
+ return result;
}
+INTDEF(elfw2(LIBELFBITS,getphdr))
diff --git a/libelf/elf32_getshdr.c b/libelf/elf32_getshdr.c
index 8d658752..b36e5437 100644
--- a/libelf/elf32_getshdr.c
+++ b/libelf/elf32_getshdr.c
@@ -54,6 +54,7 @@
#include <assert.h>
#include <errno.h>
+#include <stdbool.h>
#include <unistd.h>
#include <system.h>
@@ -65,187 +66,230 @@
#endif
-ElfW2(LIBELFBITS,Shdr) *
-__elfw2(LIBELFBITS,getshdr_internal) (scn, locked)
- Elf_Scn *scn;
- lockstat_t locked;
+static ElfW2(LIBELFBITS,Shdr) *
+load_shdr_rwlock (Elf_Scn *scn)
{
- /* XXX: no read locking here, figure out why is it not necessary. */
ElfW2(LIBELFBITS,Shdr) *result;
- if (scn == NULL)
- return NULL;
+ /* Read the section header table. */
+ Elf *elf = scn->elf;
+ ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
- if (unlikely (scn->elf->state.elf.ehdr == NULL))
+ /* Try again, maybe the data is there now. */
+ result = scn->shdr.ELFW(e,LIBELFBITS);
+ if (result != NULL)
+ goto out;
+
+ size_t shnum;
+ if (__elf_getshnum_rdlock (elf, &shnum) != 0)
+ goto out;
+ size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
+
+ /* Allocate memory for the section headers. We know the number
+ of entries from the ELF header. */
+ ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
+ (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
+ if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
{
- __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
- return NULL;
+ __libelf_seterrno (ELF_E_NOMEM);
+ goto out;
}
+ elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
- if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
+ if (elf->map_address != NULL)
{
- __libelf_seterrno (ELF_E_INVALID_CLASS);
- return NULL;
- }
+ ElfW2(LIBELFBITS,Shdr) *notcvt;
- result = scn->shdr.ELFW(e,LIBELFBITS);
- if (result == NULL)
- {
- /* Read the section header table. */
- Elf *elf = scn->elf;
- ElfW2(LIBELFBITS,Ehdr) *ehdr = elf->state.ELFW(elf,LIBELFBITS).ehdr;
+ /* All the data is already mapped. If we could use it
+ directly this would already have happened. */
+ void *file_shdr = ((char *) elf->map_address
+ + elf->start_offset + ehdr->e_shoff);
- rwlock_to_wrlock (locked, &elf->lock);
+ assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
+ || (! ALLOW_UNALIGNED
+ && ((uintptr_t) file_shdr
+ & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
- /* Try again, maybe the data is there now. */
- result = scn->shdr.ELFW(e,LIBELFBITS);
- if (result != NULL)
- goto out;
-
- size_t shnum;
- if (__elf_getshnum_internal (elf, &shnum, LS_WRLOCKED) != 0)
- goto out;
- size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
-
- /* Allocate memory for the section headers. We know the number
- of entries from the ELF header. */
- ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
- (ElfW2(LIBELFBITS,Shdr) *) malloc (size);
- if (elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
+ /* Now copy the data and at the same time convert the byte order. */
+ if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
{
- __libelf_seterrno (ELF_E_NOMEM);
- goto out;
+ assert (! ALLOW_UNALIGNED);
+ memcpy (shdr, file_shdr, size);
}
- elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 1;
-
- if (elf->map_address != NULL)
+ else
{
- ElfW2(LIBELFBITS,Shdr) *notcvt;
-
- /* All the data is already mapped. If we could use it
- directly this would already have happened. */
- void *file_shdr = ((char *) elf->map_address
- + elf->start_offset + ehdr->e_shoff);
-
- assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA
- || (! ALLOW_UNALIGNED
- && ((uintptr_t) file_shdr
- & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
-
- /* Now copy the data and at the same time convert the byte order. */
- if (ehdr->e_ident[EI_DATA] == MY_ELFDATA)
- {
- assert (! ALLOW_UNALIGNED);
- memcpy (shdr, file_shdr, size);
- }
+ if (ALLOW_UNALIGNED
+ || ((uintptr_t) file_shdr
+ & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
+ notcvt = (ElfW2(LIBELFBITS,Shdr) *)
+ ((char *) elf->map_address
+ + elf->start_offset + ehdr->e_shoff);
else
{
- if (ALLOW_UNALIGNED
- || ((uintptr_t) file_shdr
- & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) == 0)
- notcvt = (ElfW2(LIBELFBITS,Shdr) *)
- ((char *) elf->map_address
- + elf->start_offset + ehdr->e_shoff);
- else
- {
- notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
- memcpy (notcvt, ((char *) elf->map_address
- + elf->start_offset + ehdr->e_shoff),
- size);
- }
-
- for (size_t cnt = 0; cnt < shnum; ++cnt)
- {
- CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
- CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
- CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
- CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
- CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
- CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
- CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
- CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
- CONVERT_TO (shdr[cnt].sh_addralign,
- notcvt[cnt].sh_addralign);
- CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
-
- /* If this is a section with an extended index add a
- reference in the section which uses the extended
- index. */
- if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
- && shdr[cnt].sh_link < shnum)
- elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
- = cnt;
-
- /* Set the own shndx_index field in case it has not yet
- been set. */
- if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
- elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
- = -1;
- }
+ notcvt = (ElfW2(LIBELFBITS,Shdr) *) alloca (size);
+ memcpy (notcvt, ((char *) elf->map_address
+ + elf->start_offset + ehdr->e_shoff),
+ size);
}
- }
- else if (likely (elf->fildes != -1))
- {
- /* Read the header. */
- ssize_t n = pread_retry (elf->fildes,
- elf->state.ELFW(elf,LIBELFBITS).shdr, size,
- elf->start_offset + ehdr->e_shoff);
- if (unlikely ((size_t) n != size))
+
+ for (size_t cnt = 0; cnt < shnum; ++cnt)
{
- /* Severe problems. We cannot read the data. */
- __libelf_seterrno (ELF_E_READ_ERROR);
- goto free_and_out;
+ CONVERT_TO (shdr[cnt].sh_name, notcvt[cnt].sh_name);
+ CONVERT_TO (shdr[cnt].sh_type, notcvt[cnt].sh_type);
+ CONVERT_TO (shdr[cnt].sh_flags, notcvt[cnt].sh_flags);
+ CONVERT_TO (shdr[cnt].sh_addr, notcvt[cnt].sh_addr);
+ CONVERT_TO (shdr[cnt].sh_offset, notcvt[cnt].sh_offset);
+ CONVERT_TO (shdr[cnt].sh_size, notcvt[cnt].sh_size);
+ CONVERT_TO (shdr[cnt].sh_link, notcvt[cnt].sh_link);
+ CONVERT_TO (shdr[cnt].sh_info, notcvt[cnt].sh_info);
+ CONVERT_TO (shdr[cnt].sh_addralign,
+ notcvt[cnt].sh_addralign);
+ CONVERT_TO (shdr[cnt].sh_entsize, notcvt[cnt].sh_entsize);
+
+ /* If this is a section with an extended index add a
+ reference in the section which uses the extended
+ index. */
+ if (shdr[cnt].sh_type == SHT_SYMTAB_SHNDX
+ && shdr[cnt].sh_link < shnum)
+ elf->state.ELFW(elf,LIBELFBITS).scns.data[shdr[cnt].sh_link].shndx_index
+ = cnt;
+
+ /* Set the own shndx_index field in case it has not yet
+ been set. */
+ if (elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index == 0)
+ elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shndx_index
+ = -1;
}
-
- /* If the byte order of the file is not the same as the one
- of the host convert the data now. */
- if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
- for (size_t cnt = 0; cnt < shnum; ++cnt)
- {
- CONVERT (shdr[cnt].sh_name);
- CONVERT (shdr[cnt].sh_type);
- CONVERT (shdr[cnt].sh_flags);
- CONVERT (shdr[cnt].sh_addr);
- CONVERT (shdr[cnt].sh_offset);
- CONVERT (shdr[cnt].sh_size);
- CONVERT (shdr[cnt].sh_link);
- CONVERT (shdr[cnt].sh_info);
- CONVERT (shdr[cnt].sh_addralign);
- CONVERT (shdr[cnt].sh_entsize);
- }
}
- else
+ }
+ else if (likely (elf->fildes != -1))
+ {
+ /* Read the header. */
+ ssize_t n = pread_retry (elf->fildes,
+ elf->state.ELFW(elf,LIBELFBITS).shdr, size,
+ elf->start_offset + ehdr->e_shoff);
+ if (unlikely ((size_t) n != size))
{
- /* The file descriptor was already enabled and not all data was
- read. Undo the allocation. */
- __libelf_seterrno (ELF_E_FD_DISABLED);
+ /* Severe problems. We cannot read the data. */
+ __libelf_seterrno (ELF_E_READ_ERROR);
+ goto free_and_out;
+ }
- free_and_out:
- free (shdr);
- elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
- elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
+ /* If the byte order of the file is not the same as the one
+ of the host convert the data now. */
+ if (ehdr->e_ident[EI_DATA] != MY_ELFDATA)
+ for (size_t cnt = 0; cnt < shnum; ++cnt)
+ {
+ CONVERT (shdr[cnt].sh_name);
+ CONVERT (shdr[cnt].sh_type);
+ CONVERT (shdr[cnt].sh_flags);
+ CONVERT (shdr[cnt].sh_addr);
+ CONVERT (shdr[cnt].sh_offset);
+ CONVERT (shdr[cnt].sh_size);
+ CONVERT (shdr[cnt].sh_link);
+ CONVERT (shdr[cnt].sh_info);
+ CONVERT (shdr[cnt].sh_addralign);
+ CONVERT (shdr[cnt].sh_entsize);
+ }
+ }
+ else
+ {
+ /* The file descriptor was already enabled and not all data was
+ read. Undo the allocation. */
+ __libelf_seterrno (ELF_E_FD_DISABLED);
- goto out;
- }
+ free_and_out:
+ free (shdr);
+ elf->state.ELFW(elf,LIBELFBITS).shdr = NULL;
+ elf->state.ELFW(elf,LIBELFBITS).shdr_malloced = 0;
- /* Set the pointers in the `scn's. */
- for (size_t cnt = 0; cnt < shnum; ++cnt)
- elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
- = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
+ goto out;
+ }
- result = scn->shdr.ELFW(e,LIBELFBITS);
- assert (result != NULL);
+ /* Set the pointers in the `scn's. */
+ for (size_t cnt = 0; cnt < shnum; ++cnt)
+ elf->state.ELFW(elf,LIBELFBITS).scns.data[cnt].shdr.ELFW(e,LIBELFBITS)
+ = &elf->state.ELFW(elf,LIBELFBITS).shdr[cnt];
+
+ result = scn->shdr.ELFW(e,LIBELFBITS);
+ assert (result != NULL);
+
+out:
+ return result;
+}
+
+static bool
+scn_valid (Elf_Scn *scn)
+{
+ if (scn == NULL)
+ return false;
- out:
- rwlock_from_wrlock (locked, &elf->lock);
+ if (unlikely (scn->elf->state.elf.ehdr == NULL))
+ {
+ __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
+ return false;
+ }
+
+ if (unlikely (scn->elf->class != ELFW(ELFCLASS,LIBELFBITS)))
+ {
+ __libelf_seterrno (ELF_E_INVALID_CLASS);
+ return false;
+ }
+
+ return true;
+}
+
+ElfW2(LIBELFBITS,Shdr) *
+__elfw2(LIBELFBITS,getshdr_rdlock) (scn)
+ Elf_Scn *scn;
+{
+ /* XXX: no read locking here, figure out why is it not necessary. */
+ ElfW2(LIBELFBITS,Shdr) *result;
+
+ if (!scn_valid (scn))
+ return NULL;
+
+ result = scn->shdr.ELFW(e,LIBELFBITS);
+ if (result == NULL)
+ {
+ rwlock_unlock (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
+ result = scn->shdr.ELFW(e,LIBELFBITS);
+ if (result == NULL)
+ result = load_shdr_rwlock (scn);
}
return result;
}
ElfW2(LIBELFBITS,Shdr) *
+__elfw2(LIBELFBITS,getshdr_wrlock) (scn)
+ Elf_Scn *scn;
+{
+ ElfW2(LIBELFBITS,Shdr) *result;
+
+ if (!scn_valid (scn))
+ return NULL;
+
+ result = scn->shdr.ELFW(e,LIBELFBITS);
+ if (result == NULL)
+ result = load_shdr_rwlock (scn);
+
+ return result;
+}
+
+ElfW2(LIBELFBITS,Shdr) *
elfw2(LIBELFBITS,getshdr) (scn)
Elf_Scn *scn;
{
- return __elfw2(LIBELFBITS,getshdr_internal) (scn, LS_UNLOCKED);
+ ElfW2(LIBELFBITS,Shdr) *result;
+
+ if (!scn_valid (scn))
+ return NULL;
+
+ rwlock_rdlock (scn->elf->lock);
+ result = __elfw2(LIBELFBITS,getshdr_rdlock) (scn);
+ rwlock_unlock (scn->elf->lock);
+
+ return result;
}
diff --git a/libelf/elf32_newehdr.c b/libelf/elf32_newehdr.c
index 0ef68c5d..4e20056f 100644
--- a/libelf/elf32_newehdr.c
+++ b/libelf/elf32_newehdr.c
@@ -77,7 +77,7 @@ elfw2(LIBELFBITS,newehdr) (elf)
return NULL;
}
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->class == 0)
elf->class = ELFW(ELFCLASS,LIBELFBITS);
@@ -106,7 +106,7 @@ elfw2(LIBELFBITS,newehdr) (elf)
result = elf->state.ELFW(elf,LIBELFBITS).ehdr;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf32_newphdr.c b/libelf/elf32_newphdr.c
index bc937852..d1b16088 100644
--- a/libelf/elf32_newphdr.c
+++ b/libelf/elf32_newphdr.c
@@ -79,7 +79,7 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
return NULL;
}
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->class == 0)
elf->class = ELFW(ELFCLASS,LIBELFBITS);
@@ -164,7 +164,7 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
}
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf32_offscn.c b/libelf/elf32_offscn.c
index 2f9e1ced..86eff8b1 100644
--- a/libelf/elf32_offscn.c
+++ b/libelf/elf32_offscn.c
@@ -86,7 +86,7 @@ elfw2(LIBELFBITS,offscn) (elf, offset)
&& unlikely (elfw2(LIBELFBITS,getshdr) (&runp->data[0]) == NULL))
return NULL;
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
Elf_Scn *result = NULL;
@@ -114,7 +114,7 @@ elfw2(LIBELFBITS,offscn) (elf, offset)
}
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf32_updatenull.c b/libelf/elf32_updatenull.c
index dd50fb29..ae8696e7 100644
--- a/libelf/elf32_updatenull.c
+++ b/libelf/elf32_updatenull.c
@@ -133,14 +133,13 @@ ELFW(default_ehdr,LIBELFBITS) (Elf *elf, ElfW2(LIBELFBITS,Ehdr) *ehdr,
off_t
internal_function
-__elfw2(LIBELFBITS,updatenull) (Elf *elf, int *change_bop,
- size_t shnum, lockstat_t locked)
+__elfw2(LIBELFBITS,updatenull_wrlock) (Elf *elf, int *change_bop, size_t shnum)
{
ElfW2(LIBELFBITS,Ehdr) *ehdr;
int changed = 0;
int ehdr_flags = 0;
- ehdr = __elfw2(LIBELFBITS,getehdr_internal) (elf, locked);
+ ehdr = __elfw2(LIBELFBITS,getehdr_rdlock) (elf);
/* Set the default values. */
if (ELFW(default_ehdr,LIBELFBITS) (elf, ehdr, shnum, change_bop) != 0)
@@ -153,7 +152,7 @@ __elfw2(LIBELFBITS,updatenull) (Elf *elf, int *change_bop,
if (elf->state.ELFW(elf,LIBELFBITS).phdr == NULL
&& (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN
|| ehdr->e_type == ET_CORE))
- (void) __elfw2(LIBELFBITS,getphdr_internal) (elf, locked);
+ (void) __elfw2(LIBELFBITS,getphdr_wrlock) (elf);
if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL)
{
/* Only executables, shared objects, and core files have a program
@@ -207,7 +206,7 @@ __elfw2(LIBELFBITS,updatenull) (Elf *elf, int *change_bop,
/* Load the section headers if necessary. This loads the
headers for all sections. */
if (list->data[1].shdr.ELFW(e,LIBELFBITS) == NULL)
- (void) __elfw2(LIBELFBITS,getshdr_internal) (&list->data[1], locked);
+ (void) __elfw2(LIBELFBITS,getshdr_wrlock) (&list->data[1]);
do
{
@@ -269,7 +268,7 @@ __elfw2(LIBELFBITS,updatenull) (Elf *elf, int *change_bop,
scn->shdr_flags);
if (scn->data_read == 0
- && __libelf_set_rawdata (scn, locked) != 0)
+ && __libelf_set_rawdata_wrlock (scn) != 0)
/* Something went wrong. The error value is already set. */
return -1;
@@ -368,7 +367,7 @@ __elfw2(LIBELFBITS,updatenull) (Elf *elf, int *change_bop,
{
/* The position of the section in the file
changed. Create the section data list. */
- if (__elf_getdata_internal (scn, NULL, locked) == NULL)
+ if (__elf_getdata_rdlock (scn, NULL) == NULL)
return -1;
}
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 1ce8c6a2..b95b06bf 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -1005,7 +1005,7 @@ elf_begin (fildes, cmd, ref)
if (ref != NULL)
/* Make sure the descriptor is not suddenly going away. */
- RWLOCK_RDLOCK (ref->lock);
+ rwlock_rdlock (ref->lock);
else if (unlikely (fcntl (fildes, F_GETFL) == -1 && errno == EBADF))
{
/* We cannot do anything productive without a file descriptor. */
@@ -1077,7 +1077,7 @@ elf_begin (fildes, cmd, ref)
/* Release the lock. */
if (ref != NULL)
- RWLOCK_UNLOCK (ref->lock);
+ rwlock_unlock (ref->lock);
return retval;
}
diff --git a/libelf/elf_clone.c b/libelf/elf_clone.c
index 3ace80d6..8b699fa6 100644
--- a/libelf/elf_clone.c
+++ b/libelf/elf_clone.c
@@ -68,7 +68,7 @@ elf_clone (Elf *elf, Elf_Cmd cmd)
return NULL;
/* Make sure the descriptor is not suddenly going away. */
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
if (cmd != ELF_C_EMPTY)
// XXX TODO handle ELF_C_READ/WRITE etc
@@ -96,7 +96,7 @@ elf_clone (Elf *elf, Elf_Cmd cmd)
/* Release the lock. */
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return retval;
}
diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c
index 6c4fec3b..fd5b47b9 100644
--- a/libelf/elf_cntl.c
+++ b/libelf/elf_cntl.c
@@ -73,7 +73,7 @@ elf_cntl (elf, cmd)
return -1;
}
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
switch (cmd)
{
@@ -98,7 +98,7 @@ elf_cntl (elf, cmd)
break;
}
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf_end.c b/libelf/elf_end.c
index 1329ea16..5112eaea 100644
--- a/libelf/elf_end.c
+++ b/libelf/elf_end.c
@@ -71,13 +71,13 @@ elf_end (elf)
return 0;
/* Make sure we are alone. */
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->ref_count != 0 && --elf->ref_count != 0)
{
/* Not yet the last activation. */
int result = elf->ref_count;
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
@@ -106,9 +106,9 @@ elf_end (elf)
solve this problem by giving free the child lock. The
state of REF_COUNT==0 is handled all over the library, so
this should be ok. */
- RWLOCK_UNLOCK (elf->lock);
- RWLOCK_RDLOCK (parent->lock);
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
+ rwlock_rdlock (parent->lock);
+ rwlock_wrlock (elf->lock);
if (parent->state.ar.children == elf)
parent->state.ar.children = elf->next;
@@ -122,7 +122,7 @@ elf_end (elf)
child->next = elf->next;
}
- RWLOCK_UNLOCK (parent->lock);
+ rwlock_unlock (parent->lock);
}
/* This was the last activation. Free all resources. */
diff --git a/libelf/elf_getarsym.c b/libelf/elf_getarsym.c
index 59598057..e6ecaadc 100644
--- a/libelf/elf_getarsym.c
+++ b/libelf/elf_getarsym.c
@@ -94,7 +94,7 @@ elf_getarsym (elf, ptr)
if (result == NULL)
{
/* We have not yet read the index. */
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
/* In case we find no index remember this for the next call. */
elf->state.ar.ar_sym = (Elf_Arsym *) -1l;
@@ -268,7 +268,7 @@ elf_getarsym (elf, ptr)
result = elf->state.ar.ar_sym;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
}
if (ptr != NULL)
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index dc10a689..216905a6 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -189,7 +189,7 @@ convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass,
/* Store the information for the raw data in the `rawdata' element. */
int
internal_function
-__libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
+__libelf_set_rawdata_wrlock (Elf_Scn *scn)
{
size_t offset;
size_t size;
@@ -200,7 +200,7 @@ __libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
if (elf->class == ELFCLASS32)
{
Elf32_Shdr *shdr
- = scn->shdr.e32 ?: __elf32_getshdr_internal (scn, locked);
+ = scn->shdr.e32 ?: __elf32_getshdr_wrlock (scn);
if (shdr == NULL)
/* Something went terribly wrong. */
@@ -214,7 +214,7 @@ __libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
else
{
Elf64_Shdr *shdr
- = scn->shdr.e64 ?: __elf64_getshdr_internal (scn, locked);
+ = scn->shdr.e64 ?: __elf64_getshdr_wrlock (scn);
if (shdr == NULL)
/* Something went terribly wrong. */
@@ -236,7 +236,7 @@ __libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
if (type == SHT_HASH)
{
GElf_Ehdr ehdr_mem;
- GElf_Ehdr *ehdr = __gelf_getehdr_internal (elf, &ehdr_mem, locked);
+ GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
entsize = SH_ENTSIZE_HASH (ehdr);
}
else
@@ -313,7 +313,7 @@ __libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
if (type == SHT_HASH && elf->class == ELFCLASS64)
{
GElf_Ehdr ehdr_mem;
- GElf_Ehdr *ehdr = __gelf_getehdr_internal (elf, &ehdr_mem, locked);
+ GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
scn->rawdata.d.d_type
= (SH_ENTSIZE_HASH (ehdr) == 4 ? ELF_T_WORD : ELF_T_XWORD);
}
@@ -340,12 +340,27 @@ __libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
return 0;
}
+int
+internal_function
+__libelf_set_rawdata (Elf_Scn *scn)
+{
+ int result;
+
+ if (scn == NULL)
+ return 1;
+
+ rwlock_wrlock (scn->elf->lock);
+ result = __libelf_set_rawdata_wrlock (scn);
+ rwlock_unlock (scn->elf->lock);
+
+ return result;
+}
Elf_Data *
-__elf_getdata_internal (scn, data, locked)
+internal_function
+__elf_getdata_rdlock (scn, data)
Elf_Scn *scn;
Elf_Data *data;
- lockstat_t locked;
{
Elf_Data *result = NULL;
Elf *elf;
@@ -362,9 +377,6 @@ __elf_getdata_internal (scn, data, locked)
/* We will need this multiple times later on. */
elf = scn->elf;
- if (locked == LS_UNLOCKED)
- RWLOCK_RDLOCK (elf->lock);
-
/* If `data' is not NULL this means we are not addressing the initial
data in the file. But this also means this data is already read
(since otherwise it is not possible to have a valid `data' pointer)
@@ -417,12 +429,13 @@ __elf_getdata_internal (scn, data, locked)
lock. Therefore give up the read lock and then get the write
lock. But this means that the data could meanwhile be
modified, therefore start the tests again. */
- rwlock_to_wrlock (LS_RDLOCKED, &elf->lock);
+ rwlock_unlock (elf->lock);
+ rwlock_wrlock (elf->lock);
/* Read the data from the file. There is always a file (or
memory region) associated with this descriptor since
otherwise the `data_read' flag would be set. */
- if (scn->data_read == 0 && __libelf_set_rawdata (scn, LS_WRLOCKED) != 0)
+ if (scn->data_read == 0 && __libelf_set_rawdata_wrlock (scn) != 0)
/* Something went wrong. The error value is already set. */
goto out;
}
@@ -455,9 +468,6 @@ __elf_getdata_internal (scn, data, locked)
result = &scn->data_list.data.d;
out:
- if (locked == LS_UNLOCKED)
- RWLOCK_UNLOCK (elf->lock);
-
return result;
}
@@ -466,8 +476,15 @@ elf_getdata (scn, data)
Elf_Scn *scn;
Elf_Data *data;
{
+ Elf_Data *result;
+
if (scn == NULL)
return NULL;
- return __elf_getdata_internal (scn, data, LS_UNLOCKED);
+ rwlock_rdlock (scn->elf->lock);
+ result = __elf_getdata_rdlock (scn, data);
+ rwlock_unlock (scn->elf->lock);
+
+ return result;
}
+INTDEF(elf_getdata)
diff --git a/libelf/elf_getscn.c b/libelf/elf_getscn.c
index 5bcceb63..5c14a580 100644
--- a/libelf/elf_getscn.c
+++ b/libelf/elf_getscn.c
@@ -73,7 +73,7 @@ elf_getscn (elf, idx)
return NULL;
}
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
Elf_Scn *result = NULL;
@@ -103,7 +103,7 @@ elf_getscn (elf, idx)
}
}
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf_getshnum.c b/libelf/elf_getshnum.c
index 10402d83..3c65ef4c 100644
--- a/libelf/elf_getshnum.c
+++ b/libelf/elf_getshnum.c
@@ -60,10 +60,9 @@
int
-__elf_getshnum_internal (elf, dst, locked)
+__elf_getshnum_rdlock (elf, dst)
Elf *elf;
size_t *dst;
- lockstat_t locked;
{
int result = 0;
int idx;
@@ -77,9 +76,6 @@ __elf_getshnum_internal (elf, dst, locked)
return -1;
}
- if (locked == LS_UNLOCKED)
- RWLOCK_RDLOCK (elf->lock);
-
idx = elf->state.elf.scns_last->cnt;
if (idx != 0
|| (elf->state.elf.scns_last
@@ -92,9 +88,6 @@ __elf_getshnum_internal (elf, dst, locked)
else
*dst = 0;
- if (locked == LS_UNLOCKED)
- RWLOCK_UNLOCK (elf->lock);
-
return result;
}
@@ -103,8 +96,14 @@ elf_getshnum (elf, dst)
Elf *elf;
size_t *dst;
{
+ int result;
+
if (elf == NULL)
return -1;
- return __elf_getshnum_internal (elf, dst, LS_UNLOCKED);
+ rwlock_rdlock (elf->lock);
+ result = __elf_getshnum_rdlock (elf, dst);
+ rwlock_unlock (elf->lock);
+
+ return result;
}
diff --git a/libelf/elf_getshstrndx.c b/libelf/elf_getshstrndx.c
index 9d0d27b8..52516a8e 100644
--- a/libelf/elf_getshstrndx.c
+++ b/libelf/elf_getshstrndx.c
@@ -79,7 +79,7 @@ elf_getshstrndx (elf, dst)
return -1;
}
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
/* We rely here on the fact that the `elf' element is a common prefix
of `elf32' and `elf64'. */
@@ -196,7 +196,7 @@ elf_getshstrndx (elf, dst)
}
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf_newdata.c b/libelf/elf_newdata.c
index 1f42204d..db17ea58 100644
--- a/libelf/elf_newdata.c
+++ b/libelf/elf_newdata.c
@@ -83,7 +83,7 @@ elf_newdata (Elf_Scn *scn)
return NULL;
}
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
if (scn->data_read && scn->data_list_rear == NULL)
{
@@ -122,7 +122,7 @@ elf_newdata (Elf_Scn *scn)
scn->data_list_rear = result;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
/* Please note that the following is thread safe and is also defined
for RESULT == NULL since it still return NULL. */
diff --git a/libelf/elf_newscn.c b/libelf/elf_newscn.c
index da4ca723..aefab334 100644
--- a/libelf/elf_newscn.c
+++ b/libelf/elf_newscn.c
@@ -80,7 +80,7 @@ elf_newscn (elf)
assert (offsetof (Elf, state.elf32.scns)
== offsetof (Elf, state.elf64.scns));
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
again:
if (elf->state.elf.scns_last->cnt < elf->state.elf.scns_last->max)
@@ -170,7 +170,7 @@ elf_newscn (elf)
result->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf_nextscn.c b/libelf/elf_nextscn.c
index 0a21c76f..a1842bb3 100644
--- a/libelf/elf_nextscn.c
+++ b/libelf/elf_nextscn.c
@@ -69,7 +69,7 @@ elf_nextscn (elf, scn)
if (elf == NULL)
return NULL;
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
if (scn == NULL)
{
@@ -103,7 +103,7 @@ elf_nextscn (elf, scn)
}
}
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf_rawdata.c b/libelf/elf_rawdata.c
index b8be27c7..b8bb0f4b 100644
--- a/libelf/elf_rawdata.c
+++ b/libelf/elf_rawdata.c
@@ -88,7 +88,7 @@ elf_rawdata (scn, data)
/* First thing we do is to read the data from the file. There is
always a file (or memory region) associated with this descriptor
since otherwise the `data_read' flag would be set. */
- if (__libelf_set_rawdata (scn, LS_UNLOCKED) != 0)
+ if (__libelf_set_rawdata (scn) != 0)
/* Something went wrong. The error value is already set. */
return NULL;
}
diff --git a/libelf/elf_readall.c b/libelf/elf_readall.c
index 71daea4b..8f171b21 100644
--- a/libelf/elf_readall.c
+++ b/libelf/elf_readall.c
@@ -90,12 +90,12 @@ __libelf_readall (elf)
Elf *elf;
{
/* Get the file. */
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->map_address == NULL && unlikely (elf->fildes == -1))
{
__libelf_seterrno (ELF_E_INVALID_HANDLE);
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return NULL;
}
@@ -147,7 +147,7 @@ __libelf_readall (elf)
libelf_release_all (elf);
}
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return (char *) elf->map_address;
}
diff --git a/libelf/elf_strptr.c b/libelf/elf_strptr.c
index 6fb0c50c..35a0e9b6 100644
--- a/libelf/elf_strptr.c
+++ b/libelf/elf_strptr.c
@@ -73,7 +73,7 @@ elf_strptr (elf, idx, offset)
return NULL;
}
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
char *result = NULL;
Elf_Scn *strscn;
@@ -140,10 +140,15 @@ elf_strptr (elf, idx, offset)
}
}
- if (strscn->rawdata_base == NULL && ! strscn->data_read
- /* Read the section data. */
- && __libelf_set_rawdata (strscn, LS_RDLOCKED) != 0)
- goto out;
+ if (strscn->rawdata_base == NULL && ! strscn->data_read)
+ {
+ rwlock_unlock (elf->lock);
+ rwlock_wrlock (elf->lock);
+ if (strscn->rawdata_base == NULL && ! strscn->data_read
+ /* Read the section data. */
+ && __libelf_set_rawdata_wrlock (strscn) != 0)
+ goto out;
+ }
if (likely (strscn->rawdata_base != NULL))
result = &strscn->rawdata_base[offset];
@@ -166,7 +171,7 @@ elf_strptr (elf, idx, offset)
}
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/elf_update.c b/libelf/elf_update.c
index 8d605e05..5daece40 100644
--- a/libelf/elf_update.c
+++ b/libelf/elf_update.c
@@ -174,7 +174,7 @@ elf_update (elf, cmd)
return -1;
}
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
/* Make sure we have an ELF header. */
if (elf->state.elf.ehdr == NULL)
@@ -193,8 +193,8 @@ elf_update (elf, cmd)
will come right after the ELF header. The count the size of all
sections and finally place the section table. */
size = (elf->class == ELFCLASS32
- ? __elf32_updatenull (elf, &change_bo, shnum, LS_WRLOCKED)
- : __elf64_updatenull (elf, &change_bo, shnum, LS_WRLOCKED));
+ ? __elf32_updatenull_wrlock (elf, &change_bo, shnum)
+ : __elf64_updatenull_wrlock (elf, &change_bo, shnum));
if (likely (size != -1)
/* See whether we actually have to write out the data. */
&& (cmd == ELF_C_WRITE || cmd == ELF_C_WRITE_MMAP))
@@ -218,7 +218,7 @@ elf_update (elf, cmd)
}
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return size;
}
diff --git a/libelf/gelf_getauxv.c b/libelf/gelf_getauxv.c
index 450e81b1..036bdccf 100644
--- a/libelf/gelf_getauxv.c
+++ b/libelf/gelf_getauxv.c
@@ -79,7 +79,7 @@ gelf_getauxv (data, ndx, dst)
elf = data_scn->s->elf;
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
/* This is the one place where we have to take advantage of the fact
that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
@@ -124,7 +124,7 @@ gelf_getauxv (data, ndx, dst)
result = dst;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/gelf_getdyn.c b/libelf/gelf_getdyn.c
index c032e5b2..82f8e328 100644
--- a/libelf/gelf_getdyn.c
+++ b/libelf/gelf_getdyn.c
@@ -80,7 +80,7 @@ gelf_getdyn (data, ndx, dst)
elf = data_scn->s->elf;
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
/* This is the one place where we have to take advantage of the fact
that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
@@ -126,7 +126,7 @@ gelf_getdyn (data, ndx, dst)
result = dst;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/gelf_getehdr.c b/libelf/gelf_getehdr.c
index ddc77072..34b5db3e 100644
--- a/libelf/gelf_getehdr.c
+++ b/libelf/gelf_getehdr.c
@@ -61,10 +61,9 @@
GElf_Ehdr *
-__gelf_getehdr_internal (elf, dest, locked)
+__gelf_getehdr_rdlock (elf, dest)
Elf *elf;
GElf_Ehdr *dest;
- lockstat_t locked;
{
GElf_Ehdr *result = NULL;
@@ -77,9 +76,6 @@ __gelf_getehdr_internal (elf, dest, locked)
return NULL;
}
- if (locked == LS_UNLOCKED)
- RWLOCK_RDLOCK (elf->lock);
-
/* The following is an optimization: the ehdr element is at the same
position in both the elf32 and elf64 structure. */
if (offsetof (struct Elf, state.elf32.ehdr)
@@ -116,9 +112,6 @@ __gelf_getehdr_internal (elf, dest, locked)
else
result = memcpy (dest, elf->state.elf64.ehdr, sizeof (*dest));
- if (locked == LS_UNLOCKED)
- RWLOCK_UNLOCK (elf->lock);
-
return result;
}
@@ -127,8 +120,13 @@ gelf_getehdr (elf, dest)
Elf *elf;
GElf_Ehdr *dest;
{
+ GElf_Ehdr *result;
if (elf == NULL)
return NULL;
- return __gelf_getehdr_internal (elf, dest, LS_UNLOCKED);
+ rwlock_rdlock (elf->lock);
+ result = __gelf_getehdr_rdlock (elf, dest);
+ rwlock_unlock (elf->lock);
+
+ return result;
}
diff --git a/libelf/gelf_getlib.c b/libelf/gelf_getlib.c
index 75a4a4ad..aa91a73b 100644
--- a/libelf/gelf_getlib.c
+++ b/libelf/gelf_getlib.c
@@ -76,7 +76,7 @@ gelf_getlib (data, ndx, dst)
Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
- RWLOCK_RDLOCK (data_scn->s->elf->lock);
+ rwlock_rdlock (data_scn->s->elf->lock);
/* The on disk format of Elf32_Lib and Elf64_Lib is identical. So
we can simplify things significantly. */
@@ -95,7 +95,7 @@ gelf_getlib (data, ndx, dst)
result = dst;
}
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getmove.c b/libelf/gelf_getmove.c
index 728c54eb..6e769484 100644
--- a/libelf/gelf_getmove.c
+++ b/libelf/gelf_getmove.c
@@ -90,11 +90,11 @@ gelf_getmove (data, ndx, dst)
}
elf = ((Elf_Data_Scn *) data)->s->elf;
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
*dst = ((GElf_Move *) data->d_buf)[ndx];
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
result = dst;
diff --git a/libelf/gelf_getnote.c b/libelf/gelf_getnote.c
index 6867aeb4..e4900185 100644
--- a/libelf/gelf_getnote.c
+++ b/libelf/gelf_getnote.c
@@ -79,7 +79,7 @@ gelf_getnote (data, offset, result, name_offset, desc_offset)
assert (sizeof (GElf_Nhdr) == sizeof (Elf32_Nhdr));
assert (sizeof (GElf_Nhdr) == sizeof (Elf64_Nhdr));
- RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
/* The data is already in the correct form. Just make sure the
offset is OK. */
@@ -113,7 +113,7 @@ gelf_getnote (data, offset, result, name_offset, desc_offset)
}
}
- RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
return offset;
}
diff --git a/libelf/gelf_getphdr.c b/libelf/gelf_getphdr.c
index 286b253e..66cd143d 100644
--- a/libelf/gelf_getphdr.c
+++ b/libelf/gelf_getphdr.c
@@ -54,6 +54,7 @@
#include <gelf.h>
#include <string.h>
+#include <stdbool.h>
#include "libelfP.h"
@@ -81,7 +82,7 @@ gelf_getphdr (elf, ndx, dst)
return NULL;
}
- RWLOCK_RDLOCK (elf->lock);
+ rwlock_rdlock (elf->lock);
if (elf->class == ELFCLASS32)
{
@@ -90,10 +91,12 @@ gelf_getphdr (elf, ndx, dst)
if (phdr == NULL)
{
- phdr = __elf32_getphdr_internal (elf, LS_RDLOCKED);
+ rwlock_unlock (elf->lock);
+ phdr = INTUSE(elf32_getphdr) (elf);
if (phdr == NULL)
/* The error number is already set. */
- goto out;
+ return NULL;
+ rwlock_rdlock (elf->lock);
}
/* Test whether the index is ok. */
@@ -126,10 +129,12 @@ gelf_getphdr (elf, ndx, dst)
if (phdr == NULL)
{
- phdr = __elf64_getphdr_internal (elf, LS_RDLOCKED);
+ rwlock_unlock (elf->lock);
+ phdr = INTUSE(elf64_getphdr) (elf);
if (phdr == NULL)
/* The error number is already set. */
- goto out;
+ return NULL;
+ rwlock_rdlock (elf->lock);
}
/* Test whether the index is ok. */
@@ -144,7 +149,7 @@ gelf_getphdr (elf, ndx, dst)
}
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/gelf_getrel.c b/libelf/gelf_getrel.c
index 74a5595a..f3775bd9 100644
--- a/libelf/gelf_getrel.c
+++ b/libelf/gelf_getrel.c
@@ -88,7 +88,7 @@ gelf_getrel (data, ndx, dst)
The interface is broken so that it requires this hack. */
scn = data_scn->s;
- RWLOCK_RDLOCK (scn->elf->lock);
+ rwlock_rdlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
@@ -123,7 +123,7 @@ gelf_getrel (data, ndx, dst)
sizeof (Elf64_Rel));
}
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_getrela.c b/libelf/gelf_getrela.c
index f62bd286..0275c375 100644
--- a/libelf/gelf_getrela.c
+++ b/libelf/gelf_getrela.c
@@ -88,7 +88,7 @@ gelf_getrela (data, ndx, dst)
The interface is broken so that it requires this hack. */
scn = data_scn->s;
- RWLOCK_RDLOCK (scn->elf->lock);
+ rwlock_rdlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
@@ -124,7 +124,7 @@ gelf_getrela (data, ndx, dst)
sizeof (Elf64_Rela));
}
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_getshdr.c b/libelf/gelf_getshdr.c
index f5457a7a..8b46dfa8 100644
--- a/libelf/gelf_getshdr.c
+++ b/libelf/gelf_getshdr.c
@@ -74,13 +74,13 @@ gelf_getshdr (scn, dst)
return NULL;
}
- RWLOCK_RDLOCK (scn->elf->lock);
+ rwlock_rdlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
/* Copy the elements one-by-one. */
Elf32_Shdr *shdr
- = scn->shdr.e32 ?: __elf32_getshdr_internal (scn, LS_RDLOCKED);
+ = scn->shdr.e32 ?: __elf32_getshdr_rdlock (scn);
if (shdr == NULL)
{
@@ -106,7 +106,7 @@ gelf_getshdr (scn, dst)
else
{
Elf64_Shdr *shdr
- = scn->shdr.e64 ?: __elf64_getshdr_internal (scn, LS_RDLOCKED);
+ = scn->shdr.e64 ?: __elf64_getshdr_rdlock (scn);
if (shdr == NULL)
{
@@ -119,7 +119,7 @@ gelf_getshdr (scn, dst)
}
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_getsym.c b/libelf/gelf_getsym.c
index 4f2a2bec..162061f7 100644
--- a/libelf/gelf_getsym.c
+++ b/libelf/gelf_getsym.c
@@ -77,7 +77,7 @@ gelf_getsym (data, ndx, dst)
return NULL;
}
- RWLOCK_RDLOCK (data_scn->s->elf->lock);
+ rwlock_rdlock (data_scn->s->elf->lock);
/* This is the one place where we have to take advantage of the fact
that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
@@ -131,7 +131,7 @@ gelf_getsym (data, ndx, dst)
result = dst;
out:
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getsyminfo.c b/libelf/gelf_getsyminfo.c
index c5a312c4..2c07526a 100644
--- a/libelf/gelf_getsyminfo.c
+++ b/libelf/gelf_getsyminfo.c
@@ -80,7 +80,7 @@ gelf_getsyminfo (data, ndx, dst)
assert (sizeof (GElf_Syminfo) == sizeof (Elf32_Syminfo));
assert (sizeof (GElf_Syminfo) == sizeof (Elf64_Syminfo));
- RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
/* The data is already in the correct form. Just make sure the
index is OK. */
@@ -95,7 +95,7 @@ gelf_getsyminfo (data, ndx, dst)
result = dst;
out:
- RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getsymshndx.c b/libelf/gelf_getsymshndx.c
index 7fa09680..a9cfc181 100644
--- a/libelf/gelf_getsymshndx.c
+++ b/libelf/gelf_getsymshndx.c
@@ -84,7 +84,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
return NULL;
}
- RWLOCK_RDLOCK (symdata_scn->s->elf->lock);
+ rwlock_rdlock (symdata_scn->s->elf->lock);
/* The user is not required to pass a data descriptor for an extended
section index table. */
@@ -155,7 +155,7 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
result = dst;
out:
- RWLOCK_UNLOCK (symdata_scn->s->elf->lock);
+ rwlock_unlock (symdata_scn->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getverdaux.c b/libelf/gelf_getverdaux.c
index df796a8a..b8bcf14e 100644
--- a/libelf/gelf_getverdaux.c
+++ b/libelf/gelf_getverdaux.c
@@ -81,7 +81,7 @@ gelf_getverdaux (data, offset, dst)
assert (sizeof (GElf_Verdaux) == sizeof (Elf32_Verdaux));
assert (sizeof (GElf_Verdaux) == sizeof (Elf64_Verdaux));
- RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
/* The data is already in the correct form. Just make sure the
index is OK. */
@@ -97,7 +97,7 @@ gelf_getverdaux (data, offset, dst)
sizeof (GElf_Verdaux));
- RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getverdef.c b/libelf/gelf_getverdef.c
index babfa993..05cc2e89 100644
--- a/libelf/gelf_getverdef.c
+++ b/libelf/gelf_getverdef.c
@@ -81,7 +81,7 @@ gelf_getverdef (data, offset, dst)
assert (sizeof (GElf_Verdef) == sizeof (Elf32_Verdef));
assert (sizeof (GElf_Verdef) == sizeof (Elf64_Verdef));
- RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
/* The data is already in the correct form. Just make sure the
index is OK. */
@@ -96,7 +96,7 @@ gelf_getverdef (data, offset, dst)
result = (GElf_Verdef *) memcpy (dst, (char *) data->d_buf + offset,
sizeof (GElf_Verdef));
- RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getvernaux.c b/libelf/gelf_getvernaux.c
index 070e97ec..45d3300f 100644
--- a/libelf/gelf_getvernaux.c
+++ b/libelf/gelf_getvernaux.c
@@ -84,7 +84,7 @@ gelf_getvernaux (data, offset, dst)
assert (sizeof (GElf_Vernaux) == sizeof (Elf32_Vernaux));
assert (sizeof (GElf_Vernaux) == sizeof (Elf64_Vernaux));
- RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
/* The data is already in the correct form. Just make sure the
index is OK. */
@@ -99,7 +99,7 @@ gelf_getvernaux (data, offset, dst)
result = (GElf_Vernaux *) memcpy (dst, (char *) data->d_buf + offset,
sizeof (GElf_Verneed));
- RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getverneed.c b/libelf/gelf_getverneed.c
index 3e2f1486..e82a055d 100644
--- a/libelf/gelf_getverneed.c
+++ b/libelf/gelf_getverneed.c
@@ -84,7 +84,7 @@ gelf_getverneed (data, offset, dst)
assert (sizeof (GElf_Verneed) == sizeof (Elf32_Vernaux));
assert (sizeof (GElf_Verneed) == sizeof (Elf64_Vernaux));
- RWLOCK_RDLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_rdlock (((Elf_Data_Scn *) data)->s->elf->lock);
/* The data is already in the correct form. Just make sure the
index is OK. */
@@ -99,7 +99,7 @@ gelf_getverneed (data, offset, dst)
result = (GElf_Verneed *) memcpy (dst, (char *) data->d_buf + offset,
sizeof (GElf_Verneed));
- RWLOCK_UNLOCK (((Elf_Data_Scn *) data)->s->elf->lock);
+ rwlock_unlock (((Elf_Data_Scn *) data)->s->elf->lock);
return result;
}
diff --git a/libelf/gelf_getversym.c b/libelf/gelf_getversym.c
index 011a54af..397b7edb 100644
--- a/libelf/gelf_getversym.c
+++ b/libelf/gelf_getversym.c
@@ -88,7 +88,7 @@ gelf_getversym (data, ndx, dst)
assert (sizeof (GElf_Versym) == sizeof (Elf32_Versym));
assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
- RWLOCK_RDLOCK (scn->elf->lock);
+ rwlock_rdlock (scn->elf->lock);
/* The data is already in the correct form. Just make sure the
index is OK. */
@@ -104,7 +104,7 @@ gelf_getversym (data, ndx, dst)
result = dst;
}
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_auxv.c b/libelf/gelf_update_auxv.c
index dbcde82d..b294ead7 100644
--- a/libelf/gelf_update_auxv.c
+++ b/libelf/gelf_update_auxv.c
@@ -84,7 +84,7 @@ gelf_update_auxv (data, ndx, src)
}
scn = data_scn->s;
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
@@ -129,7 +129,7 @@ gelf_update_auxv (data, ndx, src)
scn->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_dyn.c b/libelf/gelf_update_dyn.c
index fce00bb9..10bfb6ab 100644
--- a/libelf/gelf_update_dyn.c
+++ b/libelf/gelf_update_dyn.c
@@ -85,7 +85,7 @@ gelf_update_dyn (data, ndx, src)
}
scn = data_scn->s;
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
@@ -131,7 +131,7 @@ gelf_update_dyn (data, ndx, src)
scn->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_ehdr.c b/libelf/gelf_update_ehdr.c
index 2da292c3..4d5c2b6c 100644
--- a/libelf/gelf_update_ehdr.c
+++ b/libelf/gelf_update_ehdr.c
@@ -72,7 +72,7 @@ gelf_update_ehdr (Elf *elf, GElf_Ehdr *src)
return 0;
}
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->class == ELFCLASS32)
{
@@ -130,7 +130,7 @@ gelf_update_ehdr (Elf *elf, GElf_Ehdr *src)
result = 1;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/gelf_update_lib.c b/libelf/gelf_update_lib.c
index 46409f33..9571016f 100644
--- a/libelf/gelf_update_lib.c
+++ b/libelf/gelf_update_lib.c
@@ -83,7 +83,7 @@ gelf_update_lib (data, ndx, src)
}
Elf_Scn *scn = data_scn->s;
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
/* Check whether we have to resize the data buffer. */
int result = 0;
@@ -99,7 +99,7 @@ gelf_update_lib (data, ndx, src)
scn->flags |= ELF_F_DIRTY;
}
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_move.c b/libelf/gelf_update_move.c
index 11bc8342..fd67be10 100644
--- a/libelf/gelf_update_move.c
+++ b/libelf/gelf_update_move.c
@@ -89,14 +89,14 @@ gelf_update_move (data, ndx, src)
return 0;
}
- RWLOCK_WRLOCK (data_scn->s->elf->lock);
+ rwlock_wrlock (data_scn->s->elf->lock);
((GElf_Move *) data_scn->d.d_buf)[ndx] = *src;
/* Mark the section as modified. */
data_scn->s->flags |= ELF_F_DIRTY;
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return 1;
}
diff --git a/libelf/gelf_update_phdr.c b/libelf/gelf_update_phdr.c
index 3305acbe..e8b7f788 100644
--- a/libelf/gelf_update_phdr.c
+++ b/libelf/gelf_update_phdr.c
@@ -72,7 +72,7 @@ gelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src)
return 0;
}
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->class == ELFCLASS32)
{
@@ -94,7 +94,7 @@ gelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src)
if (phdr == NULL)
{
- phdr = __elf32_getphdr_internal (elf, LS_WRLOCKED);
+ phdr = __elf32_getphdr_wrlock (elf);
if (phdr == NULL)
/* The error number is already set. */
goto out;
@@ -127,7 +127,7 @@ gelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src)
if (phdr == NULL)
{
- phdr = __elf64_getphdr_internal (elf, LS_WRLOCKED);
+ phdr = __elf64_getphdr_wrlock (elf);
if (phdr == NULL)
/* The error number is already set. */
goto out;
@@ -147,7 +147,7 @@ gelf_update_phdr (Elf *elf, int ndx, GElf_Phdr *src)
result = 1;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/gelf_update_rel.c b/libelf/gelf_update_rel.c
index b40ba27a..049b71a1 100644
--- a/libelf/gelf_update_rel.c
+++ b/libelf/gelf_update_rel.c
@@ -82,7 +82,7 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
}
scn = data_scn->s;
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
@@ -129,7 +129,7 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
scn->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_rela.c b/libelf/gelf_update_rela.c
index 86553bdb..dc99c681 100644
--- a/libelf/gelf_update_rela.c
+++ b/libelf/gelf_update_rela.c
@@ -82,7 +82,7 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
}
scn = data_scn->s;
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
@@ -132,7 +132,7 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
scn->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_shdr.c b/libelf/gelf_update_shdr.c
index dccf6ade..c57eab80 100644
--- a/libelf/gelf_update_shdr.c
+++ b/libelf/gelf_update_shdr.c
@@ -68,12 +68,12 @@ gelf_update_shdr (Elf_Scn *scn, GElf_Shdr *src)
return 0;
elf = scn->elf;
- RWLOCK_WRLOCK (elf->lock);
+ rwlock_wrlock (elf->lock);
if (elf->class == ELFCLASS32)
{
Elf32_Shdr *shdr
- = scn->shdr.e32 ?: __elf32_getshdr_internal (scn, LS_WRLOCKED);
+ = scn->shdr.e32 ?: __elf32_getshdr_wrlock (scn);
if (shdr == NULL)
{
@@ -108,7 +108,7 @@ gelf_update_shdr (Elf_Scn *scn, GElf_Shdr *src)
else
{
Elf64_Shdr *shdr
- = scn->shdr.e64 ?: __elf64_getshdr_internal (scn, LS_WRLOCKED);
+ = scn->shdr.e64 ?: __elf64_getshdr_wrlock (scn);
if (shdr == NULL)
{
@@ -123,7 +123,7 @@ gelf_update_shdr (Elf_Scn *scn, GElf_Shdr *src)
result = 1;
out:
- RWLOCK_UNLOCK (elf->lock);
+ rwlock_unlock (elf->lock);
return result;
}
diff --git a/libelf/gelf_update_sym.c b/libelf/gelf_update_sym.c
index d484e03a..fff45fdb 100644
--- a/libelf/gelf_update_sym.c
+++ b/libelf/gelf_update_sym.c
@@ -86,7 +86,7 @@ gelf_update_sym (data, ndx, src)
}
scn = data_scn->s;
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
if (scn->elf->class == ELFCLASS32)
{
@@ -140,7 +140,7 @@ gelf_update_sym (data, ndx, src)
scn->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_syminfo.c b/libelf/gelf_update_syminfo.c
index 83e92cee..a0e176dc 100644
--- a/libelf/gelf_update_syminfo.c
+++ b/libelf/gelf_update_syminfo.c
@@ -90,7 +90,7 @@ gelf_update_syminfo (data, ndx, src)
assert (sizeof (GElf_Syminfo) == sizeof (Elf64_Syminfo));
scn = data_scn->s;
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
/* Check whether we have to resize the data buffer. */
if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
@@ -107,7 +107,7 @@ gelf_update_syminfo (data, ndx, src)
scn->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_symshndx.c b/libelf/gelf_update_symshndx.c
index 5931b6bc..2d393c1f 100644
--- a/libelf/gelf_update_symshndx.c
+++ b/libelf/gelf_update_symshndx.c
@@ -93,7 +93,7 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
scn = symdata_scn->s;
/* We simply have to believe the user that the two sections belong to
the same ELF file. */
- RWLOCK_WRLOCK (scn->elf->lock);
+ rwlock_wrlock (scn->elf->lock);
/* The user is not required to pass a data descriptor for an extended
section index table. */
@@ -170,7 +170,7 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
scn->flags |= ELF_F_DIRTY;
out:
- RWLOCK_UNLOCK (scn->elf->lock);
+ rwlock_unlock (scn->elf->lock);
return result;
}
diff --git a/libelf/gelf_update_verdaux.c b/libelf/gelf_update_verdaux.c
index f1875c2d..17866efc 100644
--- a/libelf/gelf_update_verdaux.c
+++ b/libelf/gelf_update_verdaux.c
@@ -89,14 +89,14 @@ gelf_update_verdaux (data, offset, src)
return 0;
}
- RWLOCK_WRLOCK (data_scn->s->elf->lock);
+ rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verdaux));
/* Mark the section as modified. */
data_scn->s->flags |= ELF_F_DIRTY;
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return 1;
}
diff --git a/libelf/gelf_update_verdef.c b/libelf/gelf_update_verdef.c
index 94c805b0..cd19cb75 100644
--- a/libelf/gelf_update_verdef.c
+++ b/libelf/gelf_update_verdef.c
@@ -89,14 +89,14 @@ gelf_update_verdef (data, offset, src)
return 0;
}
- RWLOCK_WRLOCK (data_scn->s->elf->lock);
+ rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verdef));
/* Mark the section as modified. */
data_scn->s->flags |= ELF_F_DIRTY;
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return 1;
}
diff --git a/libelf/gelf_update_vernaux.c b/libelf/gelf_update_vernaux.c
index c7a341a7..2be69e1e 100644
--- a/libelf/gelf_update_vernaux.c
+++ b/libelf/gelf_update_vernaux.c
@@ -89,14 +89,14 @@ gelf_update_vernaux (data, offset, src)
return 0;
}
- RWLOCK_WRLOCK (data_scn->s->elf->lock);
+ rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Vernaux));
/* Mark the section as modified. */
data_scn->s->flags |= ELF_F_DIRTY;
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return 1;
}
diff --git a/libelf/gelf_update_verneed.c b/libelf/gelf_update_verneed.c
index ae23fdb4..95f2d18f 100644
--- a/libelf/gelf_update_verneed.c
+++ b/libelf/gelf_update_verneed.c
@@ -89,14 +89,14 @@ gelf_update_verneed (data, offset, src)
return 0;
}
- RWLOCK_WRLOCK (data_scn->s->elf->lock);
+ rwlock_wrlock (data_scn->s->elf->lock);
memcpy ((char *) data_scn->d.d_buf + offset, src, sizeof (GElf_Verneed));
/* Mark the section as modified. */
data_scn->s->flags |= ELF_F_DIRTY;
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return 1;
}
diff --git a/libelf/gelf_update_versym.c b/libelf/gelf_update_versym.c
index c2ec6e9f..905c8f78 100644
--- a/libelf/gelf_update_versym.c
+++ b/libelf/gelf_update_versym.c
@@ -89,14 +89,14 @@ gelf_update_versym (data, ndx, src)
return 0;
}
- RWLOCK_WRLOCK (data_scn->s->elf->lock);
+ rwlock_wrlock (data_scn->s->elf->lock);
((GElf_Versym *) data_scn->d.d_buf)[ndx] = *src;
/* Mark the section as modified. */
data_scn->s->flags |= ELF_F_DIRTY;
- RWLOCK_UNLOCK (data_scn->s->elf->lock);
+ rwlock_unlock (data_scn->s->elf->lock);
return 1;
}
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index c90e8954..1dc331fc 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -418,16 +418,6 @@ struct Elf
/* There absolutely never must be anything following the union. */
};
-/* For _locked calls. This gives the callee insight into how is the
- object locked. Some functions, e.g. elf32_getshdr, are called from
- both callers of both rwlock_wrlock and rwlock_rdlock. */
-typedef enum
-{
- LS_UNLOCKED = 0,
- LS_RDLOCKED,
- LS_WRLOCKED
-} lockstat_t;
-
/* Type of the conversion functions. These functions will convert the
byte order. */
typedef void (*xfct_t) (void *, const void *, size_t, int);
@@ -505,17 +495,15 @@ extern char *__libelf_readall (Elf *elf) internal_function;
extern int __libelf_readsections (Elf *elf) internal_function;
/* Store the information for the raw data in the `rawdata_list' element. */
-extern int __libelf_set_rawdata (Elf_Scn *scn, lockstat_t locked)
- internal_function;
+extern int __libelf_set_rawdata (Elf_Scn *scn) internal_function;
+extern int __libelf_set_rawdata_wrlock (Elf_Scn *scn) internal_function;
/* Helper functions for elf_update. */
-extern off_t __elf32_updatenull (Elf *elf, int *change_bop, size_t shnum,
- lockstat_t locked)
- internal_function;
-extern off_t __elf64_updatenull (Elf *elf, int *change_bop, size_t shnum,
- lockstat_t locked)
- internal_function;
+extern off_t __elf32_updatenull_wrlock (Elf *elf, int *change_bop,
+ size_t shnum) internal_function;
+extern off_t __elf64_updatenull_wrlock (Elf *elf, int *change_bop,
+ size_t shnum) internal_function;
extern int __elf32_updatemmap (Elf *elf, int change_bo, size_t shnum)
internal_function;
@@ -527,20 +515,19 @@ extern int __elf64_updatefile (Elf *elf, int change_bo, size_t shnum)
internal_function;
-/* Alias for exported functions to avoid PLT entries. */
-extern int __elf_end_internal (Elf *__elf);
+/* Alias for exported functions to avoid PLT entries, and
+ rdlock/wrlock variants of these functions. */
+extern int __elf_end_internal (Elf *__elf) attribute_hidden;
extern Elf *__elf_begin_internal (int __fildes, Elf_Cmd __cmd, Elf *__ref)
attribute_hidden;
-extern Elf32_Ehdr *__elf32_getehdr_internal (Elf *__elf, lockstat_t locked)
- attribute_hidden;
-extern Elf64_Ehdr *__elf64_getehdr_internal (Elf *__elf, lockstat_t locked)
- attribute_hidden;
+extern Elf32_Ehdr *__elf32_getehdr_rdlock (Elf *__elf) internal_function;
+extern Elf64_Ehdr *__elf64_getehdr_rdlock (Elf *__elf) internal_function;
extern Elf32_Ehdr *__elf32_newehdr_internal (Elf *__elf) attribute_hidden;
extern Elf64_Ehdr *__elf64_newehdr_internal (Elf *__elf) attribute_hidden;
-extern Elf32_Phdr *__elf32_getphdr_internal (Elf *__elf, lockstat_t locked)
- attribute_hidden;
-extern Elf64_Phdr *__elf64_getphdr_internal (Elf *__elf, lockstat_t locked)
- attribute_hidden;
+extern Elf32_Phdr *__elf32_getphdr_internal (Elf *__elf) attribute_hidden;
+extern Elf64_Phdr *__elf64_getphdr_internal (Elf *__elf) attribute_hidden;
+extern Elf32_Phdr *__elf32_getphdr_wrlock (Elf *__elf) attribute_hidden;
+extern Elf64_Phdr *__elf64_getphdr_wrlock (Elf *__elf) attribute_hidden;
extern Elf32_Phdr *__elf32_newphdr_internal (Elf *__elf, size_t __cnt)
attribute_hidden;
extern Elf64_Phdr *__elf64_newphdr_internal (Elf *__elf, size_t __cnt)
@@ -549,23 +536,22 @@ extern Elf_Scn *__elf32_offscn_internal (Elf *__elf, Elf32_Off __offset)
attribute_hidden;
extern Elf_Scn *__elf64_offscn_internal (Elf *__elf, Elf64_Off __offset)
attribute_hidden;
-extern int __elf_getshnum_internal (Elf *__elf, size_t *__dst,
- lockstat_t locked)
- attribute_hidden;
+extern int __elf_getshnum_rdlock (Elf *__elf, size_t *__dst) internal_function;
extern int __elf_getshstrndx_internal (Elf *__elf, size_t *__dst)
attribute_hidden;
-extern Elf32_Shdr *__elf32_getshdr_internal (Elf_Scn *__scn, lockstat_t locked)
- attribute_hidden;
-extern Elf64_Shdr *__elf64_getshdr_internal (Elf_Scn *__scn, lockstat_t locked)
- attribute_hidden;
+extern Elf32_Shdr *__elf32_getshdr_rdlock (Elf_Scn *__scn) internal_function;
+extern Elf64_Shdr *__elf64_getshdr_rdlock (Elf_Scn *__scn) internal_function;
+extern Elf32_Shdr *__elf32_getshdr_wrlock (Elf_Scn *__scn) internal_function;
+extern Elf64_Shdr *__elf64_getshdr_wrlock (Elf_Scn *__scn) internal_function;
extern Elf_Scn *__elf_getscn_internal (Elf *__elf, size_t __index)
attribute_hidden;
extern Elf_Scn *__elf_nextscn_internal (Elf *__elf, Elf_Scn *__scn)
attribute_hidden;
extern int __elf_scnshndx_internal (Elf_Scn *__scn) attribute_hidden;
-extern Elf_Data *__elf_getdata_internal (Elf_Scn *__scn, Elf_Data *__data,
- lockstat_t locked)
+extern Elf_Data *__elf_getdata_internal (Elf_Scn *__scn, Elf_Data *__data)
attribute_hidden;
+extern Elf_Data *__elf_getdata_rdlock (Elf_Scn *__scn, Elf_Data *__data)
+ internal_function;
extern Elf_Data *__elf_rawdata_internal (Elf_Scn *__scn, Elf_Data *__data)
attribute_hidden;
extern char *__elf_strptr_internal (Elf *__elf, size_t __index,
@@ -594,9 +580,8 @@ extern long int __elf32_checksum_internal (Elf *__elf) attribute_hidden;
extern long int __elf64_checksum_internal (Elf *__elf) attribute_hidden;
-extern GElf_Ehdr *__gelf_getehdr_internal (Elf *__elf, GElf_Ehdr *__dest,
- lockstat_t locked)
- attribute_hidden;
+extern GElf_Ehdr *__gelf_getehdr_rdlock (Elf *__elf, GElf_Ehdr *__dest)
+ internal_function;
extern size_t __gelf_fsize_internal (Elf *__elf, Elf_Type __type,
size_t __count, unsigned int __version)
attribute_hidden;
@@ -626,34 +611,4 @@ extern uint32_t __libelf_crc32 (uint32_t crc, unsigned char *buf, size_t len)
/* Align offset to 4 bytes as needed for note name and descriptor data. */
#define NOTE_ALIGN(n) (((n) + 3) & -4U)
-#if defined NDEBUG || !defined USE_TLS
-# define LIBELF_CHECKED_LOCK(V, S) ((void)(V))
-#else
-/* Checked locking primitives. Prints out an error to stderr if the
- locking or unlocking function returns an error code. A development
- aid similar to assert, the user is not supposed to ever see any of
- these. */
-# if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
-# error The XSI-compliant version of strerror_r() is provided, but in following, \
- the GNU version is assumed. Please rewrite.
-# endif
-
-# define LIBELF_CHECKED_LOCK(V, S) \
- do { \
- int err = (V); \
- if (err != 0) \
- { \
- char __buf[128]; \
- char *__ptr = strerror_r (err, __buf, sizeof __buf); \
- fprintf (stderr, "%s:%d: %s: %s\n", \
- __FILE__, __LINE__, (S), __ptr); \
- } \
- } while (0)
-
-#endif
-
-#define RWLOCK_RDLOCK(LOCK) LIBELF_CHECKED_LOCK (rwlock_rdlock (LOCK), "rwlock_rdlock")
-#define RWLOCK_WRLOCK(LOCK) LIBELF_CHECKED_LOCK (rwlock_wrlock (LOCK), "rwlock_wrlock")
-#define RWLOCK_UNLOCK(LOCK) LIBELF_CHECKED_LOCK (rwlock_unlock (LOCK), "rwlock_unlock")
-
#endif /* libelfP.h */
diff --git a/libelf/nlist.c b/libelf/nlist.c
index de18b8f5..f1fe1763 100644
--- a/libelf/nlist.c
+++ b/libelf/nlist.c
@@ -141,7 +141,7 @@ nlist (const char *filename, struct nlist *nl)
/* SHDR->SH_LINK now contains the index of the string section. */
/* Get the data for the symbol section. */
- data = __elf_getdata_internal (symscn, NULL, LS_UNLOCKED);
+ data = INTUSE(elf_getdata) (symscn, NULL);
if (data == NULL)
goto fail_close;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index c9bba45c..15e38181 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,9 @@
+2008-08-21 Denys Vlasenko <dvlasenk@redhat.com>
+
+ * run-addrname-test.sh: Add a new case.
+ * testfile49.bz2: New data file.
+ * Makefile.am (EXTRA_DIST): Add it.
+
2008-04-10 Roland McGrath <roland@redhat.com>
* testfile48.bz2, testfile48.bz2.debug: New data files.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 361c6581..f57e4328 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -139,7 +139,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
testfile41.bz2 testfile42.bz2 testfile43.bz2 \
testfile44.S.bz2 testfile44.expect.bz2 run-disasm-x86.sh \
testfile45.S.bz2 testfile45.expect.bz2 run-disasm-x86-64.sh \
- testfile46.bz2 testfile47.bz2 testfile48.bz2 testfile48.debug.bz2
+ testfile46.bz2 testfile47.bz2 testfile48.bz2 testfile48.debug.bz2 \
+ testfile49.bz2
installed_TESTS_ENVIRONMENT = libdir=$(DESTDIR)$(libdir) \
bindir=$(DESTDIR)$(bindir) \
diff --git a/tests/run-addrname-test.sh b/tests/run-addrname-test.sh
index ab37e286..d525523d 100755
--- a/tests/run-addrname-test.sh
+++ b/tests/run-addrname-test.sh
@@ -25,7 +25,7 @@
. $srcdir/test-subr.sh
-testfiles testfile34 testfile38 testfile41
+testfiles testfile34 testfile38 testfile41 testfile49
testrun_compare ../src/addr2line -f -e testfile34 \
0x08048074 0x08048075 0x08048076 \
@@ -84,4 +84,139 @@ foo+0xb
/home/drepper/local/elfutils-build/20030710/u.c:5
EOF
+# .section .text
+# nop #0
+#sizeless_foo:
+# nop #1
+# nop #2
+#sized_bar:
+# nop #3
+# nop #4
+#sizeless_baz:
+# nop #5
+# nop #6
+# .size sized_bar, . - sized_bar
+# nop #7
+# nop #8
+#sizeless_x:
+# nop #9
+# .org 0x100
+# nop #0
+# .globl global_outer
+#global_outer:
+# nop #1
+# nop #2
+# .globl global_in_global
+#global_in_global:
+# nop #3
+# nop #4
+# .size global_in_global, . - global_in_global
+#local_in_global:
+# nop #5
+# nop #6
+# .size local_in_global, . - local_in_global
+# nop #7
+# nop #8
+#.Lsizeless1:
+# nop #9
+# nop #10
+# .size global_outer, . - global_outer
+# nop #11
+# .org 0x200
+# nop #0
+#local_outer:
+# nop #1
+# nop #2
+# .globl global_in_local
+#global_in_local:
+# nop #3
+# nop #4
+# .size global_in_local, . - global_in_local
+#local_in_local:
+# nop #5
+# nop #6
+# .size local_in_local, . - local_in_local
+# nop #7
+# nop #8
+#.Lsizeless2:
+# nop #9
+# nop #10
+# .size local_outer, . - local_outer
+# nop #11
+testrun_compare ../src/addr2line -S -e testfile49 \
+ 0 1 2 3 4 5 6 7 8 9 \
+ 0x100 0x101 0x102 0x103 0x104 0x105 \
+ 0x106 0x107 0x108 0x109 0x10a 0x10b \
+ 0x200 0x201 0x202 0x203 0x204 0x205 \
+ 0x206 0x207 0x208 0x209 0x20a 0x20b <<\EOF
+(.text)+0
+??:0
+sizeless_foo
+??:0
+sizeless_foo+0x1
+??:0
+sized_bar
+??:0
+sized_bar+0x1
+??:0
+sized_bar+0x2
+??:0
+sized_bar+0x3
+??:0
+(.text)+0x7
+??:0
+(.text)+0x8
+??:0
+sizeless_x
+??:0
+sizeless_x+0xf7
+??:0
+global_outer
+??:0
+global_outer+0x1
+??:0
+global_in_global
+??:0
+global_in_global+0x1
+??:0
+global_outer+0x4
+??:0
+global_outer+0x5
+??:0
+global_outer+0x6
+??:0
+global_outer+0x7
+??:0
+global_outer+0x8
+??:0
+global_outer+0x9
+??:0
+(.text)+0x10b
+??:0
+(.text)+0x200
+??:0
+local_outer
+??:0
+local_outer+0x1
+??:0
+global_in_local
+??:0
+global_in_local+0x1
+??:0
+local_in_local
+??:0
+local_in_local+0x1
+??:0
+local_outer+0x6
+??:0
+local_outer+0x7
+??:0
+local_outer+0x8
+??:0
+local_outer+0x9
+??:0
+(.text)+0x20b
+??:0
+EOF
+
exit 0
diff --git a/tests/testfile49.bz2 b/tests/testfile49.bz2
new file mode 100644
index 00000000..8741a6b3
--- /dev/null
+++ b/tests/testfile49.bz2
Binary files differ