From 030f626f41b5d1d563c357743cfec492d4fa69a5 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Wed, 18 Jul 2012 13:14:30 +0200 Subject: [PATCH] elf_getshdr should work for elf->flags & ELF_F_MALLOCED. Reported-by: Nick Alcock Signed-off-by: Mark Wielaard --- ChangeLog | 4 ++ TODO | 5 ++ libelf/ChangeLog | 5 ++ libelf/elf32_getshdr.c | 11 +-- tests/ChangeLog | 9 +++ tests/Makefile.am | 9 ++- tests/run-elf_cntl_gelf_getshdr.sh | 30 +++++++++ tests/test-elf_cntl_gelf_getshdr.c | 103 +++++++++++++++++++++++++++++ 8 files changed, 169 insertions(+), 7 deletions(-) create mode 100755 tests/run-elf_cntl_gelf_getshdr.sh create mode 100644 tests/test-elf_cntl_gelf_getshdr.c diff --git a/ChangeLog b/ChangeLog index 2a9fcd868..5842492b8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2012-07-24 Mark Wielaard + + * TODO: Add note on shdrs after elf_cntl (ELF_C_FDREAD). + 2012-06-22 Mark Wielaard * configure.ac: Set version to 0.154. diff --git a/TODO b/TODO index 0012a5664..ad10a5ee0 100644 --- a/TODO +++ b/TODO @@ -29,6 +29,11 @@ Time-stamp: <2009-02-05 22:08:01 drepper> is allowed or the structure is aligned. Use ELF_F_MALLOCED flag to differentiate. +** shdrs after elf_cntl (ELF_C_FDREAD) + + Similar to the above. After ELF_C_FDREAD the file is completely + in memory. See also this mailing list thread: + https://fedorahosted.org/pipermail/elfutils-devel/2012-July/002368.html * libdw diff --git a/libelf/ChangeLog b/libelf/ChangeLog index 0a6bcba75..8c9ff8b65 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,8 @@ +2012-07-19 Mark Wielaard + + * elf32_getshdr.c (load_shdr_wrlock): Add elf->flags & ELF_F_MALLOCED + to asserts. + 2012-07-17 Petr Machata * elf32_xlatetom.c (elfw2(LIBELFBITS, xlatetom)): Do not check for diff --git a/libelf/elf32_getshdr.c b/libelf/elf32_getshdr.c index f0319cbb9..bd9340dd7 100644 --- a/libelf/elf32_getshdr.c +++ b/libelf/elf32_getshdr.c @@ -1,5 +1,5 @@ /* Return section header. - Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007, 2009 Red Hat, Inc. + Copyright (C) 1998-2002, 2005, 2007, 2009, 2012 Red Hat, Inc. This file is part of elfutils. Written by Ulrich Drepper , 1998. @@ -80,11 +80,14 @@ load_shdr_wrlock (Elf_Scn *scn) ElfW2(LIBELFBITS,Shdr) *notcvt; /* All the data is already mapped. If we could use it - directly this would already have happened. */ + directly this would already have happened. Unless + we allocated the memory ourselves and the ELF_F_MALLOCED + flag is set. */ void *file_shdr = ((char *) elf->map_address + elf->start_offset + ehdr->e_shoff); - assert (ehdr->e_ident[EI_DATA] != MY_ELFDATA + assert ((elf->flags & ELF_F_MALLOCED) + || ehdr->e_ident[EI_DATA] != MY_ELFDATA || (! ALLOW_UNALIGNED && ((uintptr_t) file_shdr & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0)); @@ -92,7 +95,7 @@ load_shdr_wrlock (Elf_Scn *scn) /* Now copy the data and at the same time convert the byte order. */ if (ehdr->e_ident[EI_DATA] == MY_ELFDATA) { - assert (! ALLOW_UNALIGNED); + assert ((elf->flags & ELF_F_MALLOCED) || ! ALLOW_UNALIGNED); memcpy (shdr, file_shdr, size); } else diff --git a/tests/ChangeLog b/tests/ChangeLog index 928002823..6133048f6 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,12 @@ +2012-07-19 Mark Wielaard + + * 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 * run-elflint-self.sh: runtests on ../backends/*so files. diff --git a/tests/Makefile.am b/tests/Makefile.am index fcbabf07c..a8b0f1649 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 000000000..41a7d15bb --- /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 . + +. $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 000000000..b561b53cb --- /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 . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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); +} -- 2.47.2