public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libelf: If xlate can only convert the ELF note header, just do that.
@ 2019-04-30 11:24 Mark Wielaard
  2019-04-30 22:04 ` Mark Wielaard
  0 siblings, 1 reply; 3+ messages in thread
From: Mark Wielaard @ 2019-04-30 11:24 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

When we started parsing new style ELF_T_NHDR8 notes we added extra
checks on alignment and padding. When those failed we would stop
converting and just return the rest of the ELF Note unconverted.
In the case were we just had enough data for just the ELF Note header
and the destionation and source weren't the same we would then
accidentially throw away the Note header conversion we just did.

Fix that by indicating we did correctly convert just the header.

Adds testcase that compares parsing ELF notes with gelf_getnote
and parsing the raw data by hand using elf32_xlatetom using just
the Note header and ignoring the (raw) note data.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libelf/note_xlate.h     |  12 +++-
 tests/ChangeLog         |   9 +++
 tests/Makefile.am       |   8 ++-
 tests/run-xlate-note.sh |  93 ++++++++++++++++++++++++++++
 tests/xlate_notes.c     | 157 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 274 insertions(+), 5 deletions(-)
 create mode 100755 tests/run-xlate-note.sh
 create mode 100644 tests/xlate_notes.c

diff --git a/libelf/note_xlate.h b/libelf/note_xlate.h
index bc9950f..4944a5a 100644
--- a/libelf/note_xlate.h
+++ b/libelf/note_xlate.h
@@ -47,13 +47,21 @@ elf_cvt_note (void *dest, const void *src, size_t len, int encode,
       note_len += n->n_namesz;
       note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
       if (note_len > len || note_len < sizeof *n)
-	break;
+	{
+	  /* Header was translated, nothing else.  */
+	  len -= sizeof *n;
+	  break;
+	}
 
       /* data as a whole needs to be aligned.  */
       note_len += n->n_descsz;
       note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
       if (note_len > len || note_len < sizeof *n)
-	break;
+	{
+	  /* Header was translated, nothing else.  */
+	  len -= sizeof *n;
+	  break;
+	}
 
       /* Copy or skip the note data.  */
       size_t note_data_len = note_len - sizeof *n;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 71aa178..761edd0 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,12 @@
+2019-04-30  Mark Wielaard  <mark@klomp.org>
+
+	* xlate_notes.c: New file.
+	* run-xlate-note.sh: New test.
+	* Makefile.am (check_PROGRAMS): Add xlate_notes.
+	(TESTS): Add run-xlate-note.sh.
+	(EXTRA_DIST): Likewise.
+	(xlate_notes_LDADD): New variable.
+
 2019-03-04  Mark Wielaard  <mark@klomp.org>
 
 	* backtrace.c (tgkill): Remove define.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 1b0c7d3..498c1db 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -60,7 +60,7 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
 		  fillfile dwarf_default_lower_bound dwarf-die-addr-die \
 		  get-units-invalid get-units-split attr-integrate-skel \
 		  all-dwarf-ranges unit-info next_cfi \
-		  elfcopy addsections
+		  elfcopy addsections xlate_notes
 
 asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
 	    asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -159,7 +159,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
 	run-next-cfi.sh run-next-cfi-self.sh \
 	run-copyadd-sections.sh run-copymany-sections.sh \
 	run-typeiter-many.sh run-strip-test-many.sh \
-	run-strip-version.sh
+	run-strip-version.sh run-xlate-note.sh
 
 if !BIARCH
 export ELFUTILS_DISABLE_BIARCH = 1
@@ -423,7 +423,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 	     testfile-debug-rel-ppc64-g.o.bz2 \
 	     testfile-debug-rel-ppc64-z.o.bz2 \
 	     testfile-debug-rel-ppc64.o.bz2 \
-	     run-strip-version.sh testfile-version.bz2
+	     run-strip-version.sh testfile-version.bz2 \
+	     run-xlate-note.sh
 
 if USE_VALGRIND
 valgrind_cmd='valgrind -q --leak-check=full --error-exitcode=1'
@@ -593,6 +594,7 @@ unit_info_LDADD = $(libdw)
 next_cfi_LDADD = $(libelf) $(libdw)
 elfcopy_LDADD = $(libelf)
 addsections_LDADD = $(libelf)
+xlate_notes_LDADD = $(libelf)
 
 # We want to test the libelf header against the system elf.h header.
 # Don't include any -I CPPFLAGS. Except when we install our own elf.h.
diff --git a/tests/run-xlate-note.sh b/tests/run-xlate-note.sh
new file mode 100755
index 0000000..a907418
--- /dev/null
+++ b/tests/run-xlate-note.sh
@@ -0,0 +1,93 @@
+# Copyright (C) 2019 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
+
+testfiles testfileppc32
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfileppc32 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfileppc64
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfileppc64 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfiles390
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfiles390 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfiles390x
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfiles390x << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfileaarch64
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfileaarch64 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfilearm
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfilearm << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfile_gnu_props.32be.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.32be.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 12,12
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
+
+testfiles testfile_gnu_props.32le.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.32le.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 12,12
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
+
+testfiles testfile_gnu_props.64be.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.64be.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 16,16
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
+
+testfiles testfile_gnu_props.64le.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.64le.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 16,16
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
diff --git a/tests/xlate_notes.c b/tests/xlate_notes.c
new file mode 100644
index 0000000..90a4ae2
--- /dev/null
+++ b/tests/xlate_notes.c
@@ -0,0 +1,157 @@
+/* Test program for extracting ELF Note headers and getting whole notes.
+   Copyright (C) 2019 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/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include ELFUTILS_HEADER(elf)
+#include <gelf.h>
+
+int
+main (int argc, char *argv[])
+{
+  if (argc != 2)
+    {
+      printf ("No ELF file given as argument\n");
+      exit (1);
+    }
+
+  const char *fname = argv[1];
+
+  // Initialize libelf.
+  elf_version (EV_CURRENT);
+
+  /* Read the ELF from disk now.  */
+  int fd = open (fname, O_RDONLY);
+  if (fd == -1)
+    {
+      printf ("cannot open '%s': %s\n", fname, strerror (errno));
+      exit (1);
+    }
+
+  Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
+  if (elf == NULL)
+    {
+      printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
+      exit (1);
+    }
+
+  GElf_Ehdr ehdr;
+  if (gelf_getehdr (elf, &ehdr) == NULL)
+    {
+      printf ("cannot get Ehdr: %s\n", elf_errmsg (-1));
+      exit (1);
+    }
+
+  /* Search for all SHT_NOTE sections.  */
+  Elf_Scn *scn = NULL;
+  while ((scn = elf_nextscn (elf, scn)) != NULL)
+    {
+      /* Get the header.  */
+      GElf_Shdr shdr;
+      if (gelf_getshdr (scn, &shdr) == NULL)
+	{
+	  printf ("couldn't get shdr: %s\n", elf_errmsg (-1));
+	  exit (1);
+	}
+
+      if (shdr.sh_type == SHT_NOTE)
+	{
+	  printf ("Notes in section %zd:\n", elf_ndxscn (scn));
+
+	  Elf_Data *raw = elf_rawdata (scn, NULL);
+	  if (raw == NULL)
+	    {
+	      printf ("couldn't get raw data: %s\n", elf_errmsg (-1));
+	      exit (1);
+	    }
+
+	  Elf_Data *data = elf_getdata (scn, NULL);
+	  if (data == NULL)
+	    {
+	      printf ("couldn't get data: %s\n", elf_errmsg (-1));
+	      exit (1);
+	    }
+
+	  size_t off = 0;
+	  size_t next;
+	  GElf_Nhdr nhdr;
+	  size_t n_off;
+	  size_t d_off;
+	  while ((next = gelf_getnote (data, off, &nhdr, &n_off, &d_off)) > 0)
+	    {
+	      /* Now just get the note header "raw" (don't
+		 copy/translate the note data). This only handles
+		 traditional GNU ELF Notes, so we still use the next
+		 from gelf_getnote (padding is different for new style
+		 ELF_T_NHDR8 notes).  */
+	      Elf32_Nhdr nh;
+	      Elf_Data src =
+                {
+                  .d_version = EV_CURRENT, .d_type = ELF_T_NHDR,
+		  .d_size = sizeof nh
+                };
+	      Elf_Data dst = src;
+	      src.d_buf = raw->d_buf + off;
+	      dst.d_buf = &nh;
+
+	      if (elf32_xlatetom (&dst, &src, ehdr.e_ident[EI_DATA]) == NULL)
+		{
+		  printf ("couldn't xlate note: %s\n", elf_errmsg (-1));
+		  exit (1);
+		}
+
+	      printf ("type: %" PRId32 ",%" PRId32
+		      ", namesz: %" PRId32 ",%" PRId32
+		      ", descsz: %" PRId32 ",%" PRId32 "\n",
+		      nhdr.n_type, nh.n_type,
+		      nhdr.n_namesz, nh.n_namesz,
+		      nhdr.n_descsz, nh.n_descsz);
+
+	      if (nhdr.n_type != nh.n_type
+		  || nhdr.n_namesz != nh.n_namesz
+		  || nhdr.n_descsz != nh.n_descsz)
+		{
+		  printf ("Nhdrs not equal!\n");
+		  exit (1);
+		}
+
+	      off = next;
+	    }
+	}
+
+    }
+
+  if (elf_end (elf) != 0)
+    {
+      printf ("failure in elf_end: %s\n", elf_errmsg (-1));
+      exit (1);
+    }
+
+  close (fd);
+
+  return 0;
+}
-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libelf: If xlate can only convert the ELF note header, just do that.
  2019-04-30 11:24 [PATCH] libelf: If xlate can only convert the ELF note header, just do that Mark Wielaard
@ 2019-04-30 22:04 ` Mark Wielaard
  2019-05-04 19:54   ` Mark Wielaard
  0 siblings, 1 reply; 3+ messages in thread
From: Mark Wielaard @ 2019-04-30 22:04 UTC (permalink / raw)
  To: elfutils-devel

[-- Attachment #1: Type: text/plain, Size: 999 bytes --]

On Tue, Apr 30, 2019 at 01:24:48PM +0200, Mark Wielaard wrote:
> When we started parsing new style ELF_T_NHDR8 notes we added extra
> checks on alignment and padding. When those failed we would stop
> converting and just return the rest of the ELF Note unconverted.
> In the case were we just had enough data for just the ELF Note header
> and the destionation and source weren't the same we would then
> accidentially throw away the Note header conversion we just did.
> 
> Fix that by indicating we did correctly convert just the header.
> 
> Adds testcase that compares parsing ELF notes with gelf_getnote
> and parsing the raw data by hand using elf32_xlatetom using just
> the Note header and ignoring the (raw) note data.

Here is a small update that adds a ChangeLog entry for the libelf
change, and also updates the src and dest pointers so that things keep
working even if the len is slightly larger than just the ELF Note
header, but one of the alignment or padding sanity checks fails.



[-- Attachment #2: 0001-libelf-If-xlate-can-only-convert-the-ELF-note-header.patch --]
[-- Type: text/x-diff, Size: 12204 bytes --]

From 28b5f578ae772bb2404c3847e4e22ad1c407af54 Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mark@klomp.org>
Date: Tue, 30 Apr 2019 13:00:17 +0200
Subject: [PATCH] libelf: If xlate can only convert the ELF note header, just
 do that.

When we started parsing new style ELF_T_NHDR8 notes we added extra
checks on alignment and padding. When those failed we would stop
converting and just return the rest of the ELF Note unconverted.
In the case were we just had enough data for just the ELF Note header
and the destionation and source weren't the same we would then
accidentially throw away the Note header conversion we just did.

Fix that by indicating we did correctly convert just the header.

Adds testcase that compares parsing ELF notes with gelf_getnote
and parsing the raw data by hand using elf32_xlatetom using just
the Note header and ignoring the (raw) note data.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libelf/ChangeLog        |   5 ++
 libelf/note_xlate.h     |  16 +++-
 tests/ChangeLog         |   9 +++
 tests/Makefile.am       |   8 +-
 tests/run-xlate-note.sh |  93 ++++++++++++++++++++++++
 tests/xlate_notes.c     | 157 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 283 insertions(+), 5 deletions(-)
 create mode 100755 tests/run-xlate-note.sh
 create mode 100644 tests/xlate_notes.c

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index d3bdac3d4..5eadaf76b 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,8 @@
+2019-04-30  Mark Wielaard  <mark@klomp.org>
+
+	* note_xlate.h (elf_cvt_note): Indicate we only translated the note
+	header if we ran out of data by updating len, src and dest.
+
 2019-04-01  Mao Han  <han_mao@c-sky.com>
 
 	* elf.h: Update from glibc.
diff --git a/libelf/note_xlate.h b/libelf/note_xlate.h
index bc9950ffb..7e2784b05 100644
--- a/libelf/note_xlate.h
+++ b/libelf/note_xlate.h
@@ -47,13 +47,25 @@ elf_cvt_note (void *dest, const void *src, size_t len, int encode,
       note_len += n->n_namesz;
       note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
       if (note_len > len || note_len < sizeof *n)
-	break;
+	{
+	  /* Header was translated, nothing else.  */
+	  len -= sizeof *n;
+	  src += sizeof *n;
+	  dest += sizeof *n;
+	  break;
+	}
 
       /* data as a whole needs to be aligned.  */
       note_len += n->n_descsz;
       note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
       if (note_len > len || note_len < sizeof *n)
-	break;
+	{
+	  /* Header was translated, nothing else.  */
+	  len -= sizeof *n;
+	  src += sizeof *n;
+	  dest += sizeof *n;
+	  break;
+	}
 
       /* Copy or skip the note data.  */
       size_t note_data_len = note_len - sizeof *n;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 71aa178d3..761edd0b4 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,12 @@
+2019-04-30  Mark Wielaard  <mark@klomp.org>
+
+	* xlate_notes.c: New file.
+	* run-xlate-note.sh: New test.
+	* Makefile.am (check_PROGRAMS): Add xlate_notes.
+	(TESTS): Add run-xlate-note.sh.
+	(EXTRA_DIST): Likewise.
+	(xlate_notes_LDADD): New variable.
+
 2019-03-04  Mark Wielaard  <mark@klomp.org>
 
 	* backtrace.c (tgkill): Remove define.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 1b0c7d333..498c1db26 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -60,7 +60,7 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
 		  fillfile dwarf_default_lower_bound dwarf-die-addr-die \
 		  get-units-invalid get-units-split attr-integrate-skel \
 		  all-dwarf-ranges unit-info next_cfi \
-		  elfcopy addsections
+		  elfcopy addsections xlate_notes
 
 asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
 	    asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -159,7 +159,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
 	run-next-cfi.sh run-next-cfi-self.sh \
 	run-copyadd-sections.sh run-copymany-sections.sh \
 	run-typeiter-many.sh run-strip-test-many.sh \
-	run-strip-version.sh
+	run-strip-version.sh run-xlate-note.sh
 
 if !BIARCH
 export ELFUTILS_DISABLE_BIARCH = 1
@@ -423,7 +423,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 	     testfile-debug-rel-ppc64-g.o.bz2 \
 	     testfile-debug-rel-ppc64-z.o.bz2 \
 	     testfile-debug-rel-ppc64.o.bz2 \
-	     run-strip-version.sh testfile-version.bz2
+	     run-strip-version.sh testfile-version.bz2 \
+	     run-xlate-note.sh
 
 if USE_VALGRIND
 valgrind_cmd='valgrind -q --leak-check=full --error-exitcode=1'
@@ -593,6 +594,7 @@ unit_info_LDADD = $(libdw)
 next_cfi_LDADD = $(libelf) $(libdw)
 elfcopy_LDADD = $(libelf)
 addsections_LDADD = $(libelf)
+xlate_notes_LDADD = $(libelf)
 
 # We want to test the libelf header against the system elf.h header.
 # Don't include any -I CPPFLAGS. Except when we install our own elf.h.
diff --git a/tests/run-xlate-note.sh b/tests/run-xlate-note.sh
new file mode 100755
index 000000000..a90741826
--- /dev/null
+++ b/tests/run-xlate-note.sh
@@ -0,0 +1,93 @@
+# Copyright (C) 2019 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
+
+testfiles testfileppc32
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfileppc32 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfileppc64
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfileppc64 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfiles390
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfiles390 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfiles390x
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfiles390x << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfileaarch64
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfileaarch64 << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfilearm
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfilearm << EOF
+Notes in section 2:
+type: 1,1, namesz: 4,4, descsz: 16,16
+Notes in section 3:
+type: 3,3, namesz: 4,4, descsz: 20,20
+EOF
+
+testfiles testfile_gnu_props.32be.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.32be.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 12,12
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
+
+testfiles testfile_gnu_props.32le.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.32le.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 12,12
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
+
+testfiles testfile_gnu_props.64be.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.64be.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 16,16
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
+
+testfiles testfile_gnu_props.64le.o
+testrun_compare ${abs_top_builddir}/tests/xlate_notes testfile_gnu_props.64le.o << EOF
+Notes in section 4:
+type: 5,5, namesz: 4,4, descsz: 16,16
+type: 5,5, namesz: 4,4, descsz: 8,8
+EOF
diff --git a/tests/xlate_notes.c b/tests/xlate_notes.c
new file mode 100644
index 000000000..90a4ae276
--- /dev/null
+++ b/tests/xlate_notes.c
@@ -0,0 +1,157 @@
+/* Test program for extracting ELF Note headers and getting whole notes.
+   Copyright (C) 2019 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/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include ELFUTILS_HEADER(elf)
+#include <gelf.h>
+
+int
+main (int argc, char *argv[])
+{
+  if (argc != 2)
+    {
+      printf ("No ELF file given as argument\n");
+      exit (1);
+    }
+
+  const char *fname = argv[1];
+
+  // Initialize libelf.
+  elf_version (EV_CURRENT);
+
+  /* Read the ELF from disk now.  */
+  int fd = open (fname, O_RDONLY);
+  if (fd == -1)
+    {
+      printf ("cannot open '%s': %s\n", fname, strerror (errno));
+      exit (1);
+    }
+
+  Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
+  if (elf == NULL)
+    {
+      printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
+      exit (1);
+    }
+
+  GElf_Ehdr ehdr;
+  if (gelf_getehdr (elf, &ehdr) == NULL)
+    {
+      printf ("cannot get Ehdr: %s\n", elf_errmsg (-1));
+      exit (1);
+    }
+
+  /* Search for all SHT_NOTE sections.  */
+  Elf_Scn *scn = NULL;
+  while ((scn = elf_nextscn (elf, scn)) != NULL)
+    {
+      /* Get the header.  */
+      GElf_Shdr shdr;
+      if (gelf_getshdr (scn, &shdr) == NULL)
+	{
+	  printf ("couldn't get shdr: %s\n", elf_errmsg (-1));
+	  exit (1);
+	}
+
+      if (shdr.sh_type == SHT_NOTE)
+	{
+	  printf ("Notes in section %zd:\n", elf_ndxscn (scn));
+
+	  Elf_Data *raw = elf_rawdata (scn, NULL);
+	  if (raw == NULL)
+	    {
+	      printf ("couldn't get raw data: %s\n", elf_errmsg (-1));
+	      exit (1);
+	    }
+
+	  Elf_Data *data = elf_getdata (scn, NULL);
+	  if (data == NULL)
+	    {
+	      printf ("couldn't get data: %s\n", elf_errmsg (-1));
+	      exit (1);
+	    }
+
+	  size_t off = 0;
+	  size_t next;
+	  GElf_Nhdr nhdr;
+	  size_t n_off;
+	  size_t d_off;
+	  while ((next = gelf_getnote (data, off, &nhdr, &n_off, &d_off)) > 0)
+	    {
+	      /* Now just get the note header "raw" (don't
+		 copy/translate the note data). This only handles
+		 traditional GNU ELF Notes, so we still use the next
+		 from gelf_getnote (padding is different for new style
+		 ELF_T_NHDR8 notes).  */
+	      Elf32_Nhdr nh;
+	      Elf_Data src =
+                {
+                  .d_version = EV_CURRENT, .d_type = ELF_T_NHDR,
+		  .d_size = sizeof nh
+                };
+	      Elf_Data dst = src;
+	      src.d_buf = raw->d_buf + off;
+	      dst.d_buf = &nh;
+
+	      if (elf32_xlatetom (&dst, &src, ehdr.e_ident[EI_DATA]) == NULL)
+		{
+		  printf ("couldn't xlate note: %s\n", elf_errmsg (-1));
+		  exit (1);
+		}
+
+	      printf ("type: %" PRId32 ",%" PRId32
+		      ", namesz: %" PRId32 ",%" PRId32
+		      ", descsz: %" PRId32 ",%" PRId32 "\n",
+		      nhdr.n_type, nh.n_type,
+		      nhdr.n_namesz, nh.n_namesz,
+		      nhdr.n_descsz, nh.n_descsz);
+
+	      if (nhdr.n_type != nh.n_type
+		  || nhdr.n_namesz != nh.n_namesz
+		  || nhdr.n_descsz != nh.n_descsz)
+		{
+		  printf ("Nhdrs not equal!\n");
+		  exit (1);
+		}
+
+	      off = next;
+	    }
+	}
+
+    }
+
+  if (elf_end (elf) != 0)
+    {
+      printf ("failure in elf_end: %s\n", elf_errmsg (-1));
+      exit (1);
+    }
+
+  close (fd);
+
+  return 0;
+}
-- 
2.20.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libelf: If xlate can only convert the ELF note header, just do that.
  2019-04-30 22:04 ` Mark Wielaard
@ 2019-05-04 19:54   ` Mark Wielaard
  0 siblings, 0 replies; 3+ messages in thread
From: Mark Wielaard @ 2019-05-04 19:54 UTC (permalink / raw)
  To: elfutils-devel

On Wed, May 01, 2019 at 12:04:17AM +0200, Mark Wielaard wrote:
> On Tue, Apr 30, 2019 at 01:24:48PM +0200, Mark Wielaard wrote:
> > When we started parsing new style ELF_T_NHDR8 notes we added extra
> > checks on alignment and padding. When those failed we would stop
> > converting and just return the rest of the ELF Note unconverted.
> > In the case were we just had enough data for just the ELF Note header
> > and the destionation and source weren't the same we would then
> > accidentially throw away the Note header conversion we just did.
> > 
> > Fix that by indicating we did correctly convert just the header.
> > 
> > Adds testcase that compares parsing ELF notes with gelf_getnote
> > and parsing the raw data by hand using elf32_xlatetom using just
> > the Note header and ignoring the (raw) note data.
> 
> Here is a small update that adds a ChangeLog entry for the libelf
> change, and also updates the src and dest pointers so that things keep
> working even if the len is slightly larger than just the ELF Note
> header, but one of the alignment or padding sanity checks fails.

I pushed this update to master.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-05-04 19:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-30 11:24 [PATCH] libelf: If xlate can only convert the ELF note header, just do that Mark Wielaard
2019-04-30 22:04 ` Mark Wielaard
2019-05-04 19:54   ` Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).