summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2012-07-18 13:14:30 +0200
committerMark Wielaard <[email protected]>2012-07-24 11:41:01 +0200
commit030f626f41b5d1d563c357743cfec492d4fa69a5 (patch)
treed8454ca3cf576a8bc824957e1642ce73c4709a57 /tests
parentadcd1dd5729fdee3e85253b541767cc28809e4d9 (diff)
elf_getshdr should work for elf->flags & ELF_F_MALLOCED.
Reported-by: Nick Alcock <[email protected]> Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r--tests/ChangeLog9
-rw-r--r--tests/Makefile.am9
-rwxr-xr-xtests/run-elf_cntl_gelf_getshdr.sh30
-rw-r--r--tests/test-elf_cntl_gelf_getshdr.c103
4 files changed, 148 insertions, 3 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 92800282..6133048f 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,14 @@
2012-07-19 Mark Wielaard <[email protected]>
+ * Makefile.am (check_PROGRAMS): Add test-elf_cntl_gelf_getshdr.
+ (TESTS): Add run-elf_cntl_gelf_getshdr.sh.
+ (EXTRA_DIST): Likewise.
+ (test_elf_cntl_gelf_getshdr_LDADD): New.
+ test-elf_cntl_gelf_getshdr.c: New test program.
+ run-elf_cntl_gelf_getshdr.sh: New test script.
+
+2012-07-19 Mark Wielaard <[email protected]>
+
* run-elflint-self.sh: runtests on ../backends/*so files.
2012-07-19 Mark Wielaard <[email protected]>
diff --git a/tests/Makefile.am b/tests/Makefile.am
index fcbabf07..a8b0f164 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -50,7 +50,8 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
dwfl-addr-sect dwfl-bug-report early-offscn \
dwfl-bug-getmodules dwarf-getmacros addrcfi \
test-flag-nobits dwarf-getstring rerequest_tag \
- alldts md5-sha1-test typeiter low_high_pc
+ alldts md5-sha1-test typeiter low_high_pc \
+ test-elf_cntl_gelf_getshdr
asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -81,7 +82,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \
run-test-flag-nobits.sh run-prelink-addr-test.sh \
run-dwarf-getstring.sh run-rerequest_tag.sh run-typeiter.sh \
run-readelf-d.sh run-readelf-gdb_index.sh run-unstrip-n.sh \
- run-low_high_pc.sh run-macro-test.sh
+ run-low_high_pc.sh run-macro-test.sh run-elf_cntl_gelf_getshdr.sh
if !STANDALONE
check_PROGRAMS += msg_tst md5-sha1-test
@@ -163,7 +164,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \
testfilegdbindex7.bz2 \
run-unstrip-n.sh testcore-rtlib.bz2 testcore-rtlib-ppc.bz2 \
run-low_high_pc.sh testfile_low_high_pc.bz2 \
- run-macro-test.sh testfile-macinfo.bz2 testfile-macros.bz2
+ run-macro-test.sh testfile-macinfo.bz2 testfile-macros.bz2 \
+ run-elf_cntl_gelf_getshdr.sh
installed_TESTS_ENVIRONMENT = libdir=$(DESTDIR)$(libdir) \
bindir=$(DESTDIR)$(bindir) \
@@ -263,6 +265,7 @@ alldts_LDADD = $(libebl) $(libelf) $(libmudflap)
md5_sha1_test_LDADD = $(libeu)
typeiter_LDADD = $(libdw) $(libelf) $(libmudflap)
low_high_pc_LDADD = $(libdw) $(libelf) $(libmudflap)
+test_elf_cntl_gelf_getshdr_LDADD = $(libelf) $(libmudflap)
if GCOV
check: check-am coverage
diff --git a/tests/run-elf_cntl_gelf_getshdr.sh b/tests/run-elf_cntl_gelf_getshdr.sh
new file mode 100755
index 00000000..41a7d15b
--- /dev/null
+++ b/tests/run-elf_cntl_gelf_getshdr.sh
@@ -0,0 +1,30 @@
+#! /bin/sh
+# Copyright (C) 2012 Red Hat, Inc.
+# This file is part of elfutils.
+#
+# This file 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; either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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 this program. If not, see <http://www.gnu.org/licenses/>.
+
+. $srcdir/test-subr.sh
+
+# However we open the elf file, the shdrs should be the same.
+testrun ./test-elf_cntl_gelf_getshdr READ test-elf_cntl_gelf_getshdr \
+ > test_shdr.out
+
+testrun_compare ./test-elf_cntl_gelf_getshdr MMAP test-elf_cntl_gelf_getshdr \
+ < test_shdr.out
+
+testrun_compare ./test-elf_cntl_gelf_getshdr FDREAD test-elf_cntl_gelf_getshdr \
+ < test_shdr.out
+
+rm -f test_shdr.out
diff --git a/tests/test-elf_cntl_gelf_getshdr.c b/tests/test-elf_cntl_gelf_getshdr.c
new file mode 100644
index 00000000..b561b53c
--- /dev/null
+++ b/tests/test-elf_cntl_gelf_getshdr.c
@@ -0,0 +1,103 @@
+/* Copyright (C) 2012 Red Hat, Inc.
+ This file is part of elfutils.
+
+ This file 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; either version 3 of the License, or
+ (at your option) any later version.
+
+ 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 this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <gelf.h>
+#include <libelf.h>
+#include <stdbool.h>
+#include <inttypes.h>
+
+int
+main (int argc, char *argv[])
+{
+ if (argc != 3)
+ {
+ fprintf (stderr, "Needs two arguments.\n");
+ fprintf (stderr, "First needs to be 'READ', 'MMAP' or 'FDREAD'\n");
+ fprintf (stderr, "Second is the ELF file to read.\n");
+ exit (2); /* user error */
+ }
+
+ bool do_mmap = false;
+ bool close_fd = false;
+ if (strcmp (argv[1], "READ") == 0)
+ {
+ do_mmap = false;
+ close_fd = false;
+ }
+ else if (strcmp (argv[1], "MMAP") == 0)
+ {
+ do_mmap = true;
+ close_fd = false;
+ }
+ else if (strcmp (argv[1], "FDREAD") == 0)
+ {
+ do_mmap = false;
+ close_fd = true;
+ }
+ else
+ {
+ fprintf (stderr, "First arg needs to be 'READ', 'MMAP' or 'FDREAD'\n");
+ exit (2); /* user error */
+ }
+
+ elf_version (EV_CURRENT);
+
+ int fd = open (argv[2], O_RDONLY);
+ if (fd < 0)
+ {
+ fprintf (stderr, "Cannot open input file %s: %s\n", argv[2],
+ strerror (errno));
+ exit (2);
+ }
+
+ Elf *elf = elf_begin (fd, do_mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL);
+ if (elf == NULL)
+ {
+ fprintf (stderr, "elf_begin failed for %s: %s\n", argv[2],
+ elf_errmsg (-1));
+ exit (2);
+ }
+
+ if (! do_mmap && close_fd)
+ {
+ if (elf_cntl (elf, ELF_C_FDREAD) < 0)
+ {
+ fprintf (stderr, "elf_cntl failed for %s: %s\n", argv[2],
+ elf_errmsg (-1));
+ exit (1);
+ }
+ close (fd);
+ }
+
+ Elf_Scn *scn = NULL;
+ while ((scn = elf_nextscn (elf, scn)) != NULL)
+ {
+ GElf_Shdr shdr_mem;
+ GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
+ printf ("Section at offset %#0" PRIx64 "\n", shdr->sh_offset);
+ }
+
+ elf_end (elf);
+ exit (0);
+}