elf_getshdr should work for elf->flags & ELF_F_MALLOCED.
authorMark Wielaard <mjw@redhat.com>
Wed, 18 Jul 2012 11:14:30 +0000 (13:14 +0200)
committerMark Wielaard <mjw@redhat.com>
Tue, 24 Jul 2012 09:41:01 +0000 (11:41 +0200)
Reported-by: Nick Alcock <nix@esperi.org.uk>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
ChangeLog
TODO
libelf/ChangeLog
libelf/elf32_getshdr.c
tests/ChangeLog
tests/Makefile.am
tests/run-elf_cntl_gelf_getshdr.sh [new file with mode: 0755]
tests/test-elf_cntl_gelf_getshdr.c [new file with mode: 0644]

index 2a9fcd8..5842492 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2012-07-24  Mark Wielaard  <mjw@redhat.com>
+
+       * TODO: Add note on shdrs after elf_cntl (ELF_C_FDREAD).
+
 2012-06-22  Mark Wielaard  <mjw@redhat.com>
 
        * configure.ac: Set version to 0.154.
diff --git a/TODO b/TODO
index 0012a56..ad10a5e 100644 (file)
--- 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
 
index 0a6bcba..8c9ff8b 100644 (file)
@@ -1,3 +1,8 @@
+2012-07-19  Mark Wielaard  <mjw@redhat.com>
+
+       * elf32_getshdr.c (load_shdr_wrlock): Add elf->flags & ELF_F_MALLOCED
+       to asserts.
+
 2012-07-17  Petr Machata  <pmachata@redhat.com>
 
        * elf32_xlatetom.c (elfw2(LIBELFBITS, xlatetom)): Do not check for
index f0319cb..bd9340d 100644 (file)
@@ -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 <drepper@redhat.com>, 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
index 9280028..6133048 100644 (file)
@@ -1,5 +1,14 @@
 2012-07-19  Mark Wielaard  <mjw@redhat.com>
 
+       * 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  <mjw@redhat.com>
+
        * run-elflint-self.sh: runtests on ../backends/*so files.
 
 2012-07-19  Mark Wielaard  <mjw@redhat.com>
index fcbabf0..a8b0f16 100644 (file)
@@ -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 (executable)
index 0000000..41a7d15
--- /dev/null
@@ -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 (file)
index 0000000..b561b53
--- /dev/null
@@ -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);
+}