summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ChangeLog92
-rw-r--r--tests/Makefile.am25
-rw-r--r--tests/addsections.c44
-rw-r--r--tests/backtrace-dwarf.c2
-rw-r--r--tests/backtrace.c3
-rw-r--r--tests/dwelf_elf_e_machine_string.c64
-rw-r--r--tests/elfcopy.c99
-rw-r--r--tests/elfrdwrnop.c100
-rw-r--r--tests/hello_csky.ko.bz2bin0 -> 26448 bytes
-rwxr-xr-xtests/run-addrcfi.sh1
-rwxr-xr-xtests/run-dwelf_elf_e_machine_string.sh31
-rwxr-xr-xtests/run-elfclassify-self.sh36
-rwxr-xr-xtests/run-elfclassify.sh327
-rwxr-xr-xtests/run-large-elf-file.sh114
-rwxr-xr-xtests/run-readelf-discr.sh337
-rwxr-xr-xtests/run-reverse-sections-self.sh45
-rwxr-xr-xtests/run-reverse-sections.sh69
-rwxr-xr-xtests/run-xlate-note.sh93
-rw-r--r--tests/testfile-rng.debug.bz2bin0 -> 1286 bytes
-rw-r--r--tests/testfile-urng.debug.bz2bin0 -> 1178 bytes
-rw-r--r--tests/testfilecsky.bz2bin0 -> 3145 bytes
-rw-r--r--tests/xlate_notes.c157
22 files changed, 1615 insertions, 24 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog
index b76c44d9..2900bd82 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,95 @@
+2019-07-26 Florian Weimer <[email protected]>
+ Mark Wielaard <[email protected]>
+
+ * run-elfclassify.sh: New test.
+ * run-elfclassify-self.sh: Likewise.
+ * Makefile.sh (TESTS): Add run-elfclassify.sh and
+ run-elfclassify-self.sh.
+ (EXTRA_DIST): Likewise.
+
+2019-07-16 Mao Han <[email protected]>
+
+ * hello_csky.ko.bz2: New testfile.
+ * run-addrcfi.sh: Add C-SKY testfile.
+ * run-strip-reloc.sh: Likewise.
+ * testfilecsky.bz2: New testfile.
+ * Makefile.am (EXTRA_DIST): Add hello_csky.ko.bz2 and
+ testfilecsky.bz2.
+
+2019-06-28 Mark Wielaard <[email protected]>
+
+ * Makefile.am (check_PROGRAMS): Add dwelf_elf_e_machine_string.
+ (TESTS): Add run-dwelf_elf_e_machine_string.sh.
+ (EXTRA_DIST): Likewise.
+ (dwelf_elf_e_machine_string_LDADD): New variable.
+ * dwelf_elf_e_machine_string.c: New file.
+ * run-dwelf_elf_e_machine_string.sh: New test.
+
+2019-07-01 Mark Wielaard <[email protected]>
+
+ * run-large-elf-file.sh: Add 2GB to mem_needed when running under
+ valgrind.
+
+2019-06-18 Mark Wielaard <[email protected]>
+
+ * Makefile.am (TESTS): Add run-large-elf-file.sh.
+ (EXTRA_DIST): Likewise.
+ * addsections.c (add_sections): Add sec_size argument, use it
+ as the size of the section data.
+ (main): Handle extra sec_size argument. Pass to add_sections.
+ * run-large-elf-file.sh: New test.
+
+2019-06-03 Mark Wielaard <[email protected]>
+
+ * elfcopy.c (copy_elf): When swapping the sh_offsets of two sections,
+ make sure they are actually next to each other.
+
+2019-05-12 Mark Wielaard <[email protected]>
+
+ * Makefile.am (check_PROGRAMS): Add elfrdwrnop.
+ (TESTS): Add run-reverse-sections.sh and
+ run-reverse-sections-self.sh.
+ (EXTRA_DIST): Likewise.
+ (elfrdwrnop): New variable.
+ * elfcopy.c (copy_elf): Add reverse_off argument. Record offsets
+ of sections and swap them when possible.
+ (main): Check for --reverse-off argument. Pass reverse_offs to
+ copy_elf.
+ * run-reverse-sections.sh: New test.
+ * run-reverse-sections-self.sh: Likewise.
+ * elfrdwrnop.c: New file.
+
+2019-05-10 Mark Wielaard <[email protected]>
+
+ * Makefile.am (TESTS): Add run-readelf-discr.sh.
+ (EXTRA_DIST): Likewise and add testfile-rng.debug.bz2 and
+ testfile-urng.debug.bz2.
+ * run-readelf-discr.sh: New test.
+ * testfile-rng.debug.bz2: New test file.
+ * testfile-urng.debug.bz2: Likewise.
+
+2019-04-30 Mark Wielaard <[email protected]>
+
+ * 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-04-30 Mark Wielaard <[email protected]>
+
+ * backtrace-dwarf.c (frame_callback): Explicitly check symname is
+ NULL.
+
+2019-04-30 Mark Wielaard <[email protected]>
+
+ * backtrace.c (frame_callback): Explicitly check symname is NULL.
+
+2019-03-04 Mark Wielaard <[email protected]>
+
+ * backtrace.c (tgkill): Remove define.
+
2019-01-24 Mark Wielaard <[email protected]>
* Makefile.am (system_elf_libelf_test_CPPFLAGS): Guard by
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c74a6c1e..64857623 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -60,7 +60,8 @@ 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 elfrdwrnop \
+ dwelf_elf_e_machine_string
asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -83,6 +84,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
run-next-files.sh run-next-lines.sh \
run-get-pubnames.sh run-get-aranges.sh run-allfcts.sh \
run-show-abbrev.sh run-line2addr.sh hash \
+ run-large-elf-file.sh \
newscn run-strip-test.sh run-strip-test2.sh \
run-strip-test3.sh run-strip-test4.sh run-strip-test5.sh \
run-strip-test6.sh run-strip-test7.sh run-strip-test8.sh \
@@ -156,9 +158,13 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
run-all-dwarf-ranges.sh run-unit-info.sh \
run-reloc-bpf.sh \
run-next-cfi.sh run-next-cfi-self.sh \
+ run-reverse-sections.sh run-reverse-sections-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 \
+ run-readelf-discr.sh \
+ run-dwelf_elf_e_machine_string.sh \
+ run-elfclassify.sh run-elfclassify-self.sh
if !BIARCH
export ELFUTILS_DISABLE_BIARCH = 1
@@ -206,7 +212,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
testfile-nobitsalign.bz2 testfile-nobitsalign.strip.bz2 \
run-strip-reloc.sh hello_i386.ko.bz2 hello_x86_64.ko.bz2 \
hello_ppc64.ko.bz2 hello_s390.ko.bz2 hello_aarch64.ko.bz2 \
- hello_m68k.ko.bz2 hello_riscv64.ko.bz2 \
+ hello_m68k.ko.bz2 hello_riscv64.ko.bz2 hello_csky.ko.bz2 \
run-unstrip-test.sh run-unstrip-test2.sh \
testfile-info-link.bz2 testfile-info-link.debuginfo.bz2 \
testfile-info-link.stripped.bz2 run-unstrip-test3.sh \
@@ -223,6 +229,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
testfile11-debugframe.bz2 testfile12-debugframe.bz2 \
testfileaarch64-debugframe.bz2 testfilearm-debugframe.bz2 \
testfileppc32-debugframe.bz2 testfileppc64-debugframe.bz2 \
+ testfilecsky.bz2 \
run-varlocs-self.sh run-exprlocs-self.sh \
run-find-prologues.sh run-allregs.sh run-native-test.sh \
run-addrname-test.sh run-dwfl-bug-offline-rel.sh \
@@ -417,12 +424,19 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
run-unit-info.sh run-next-cfi.sh run-next-cfi-self.sh \
testfile-riscv64.bz2 testfile-riscv64-s.bz2 \
testfile-riscv64-core.bz2 \
+ run-reverse-sections.sh run-reverse-sections-self.sh \
run-copyadd-sections.sh run-copymany-sections.sh \
+ run-large-elf-file.sh \
run-typeiter-many.sh run-strip-test-many.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 \
+ run-readelf-discr.sh \
+ testfile-rng.debug.bz2 testfile-urng.debug.bz2 \
+ run-dwelf_elf_e_machine_string.sh \
+ run-elfclassify.sh run-elfclassify-self.sh
if USE_VALGRIND
valgrind_cmd='valgrind -q --leak-check=full --error-exitcode=1'
@@ -594,6 +608,9 @@ unit_info_LDADD = $(libdw)
next_cfi_LDADD = $(libelf) $(libdw)
elfcopy_LDADD = $(libelf)
addsections_LDADD = $(libelf)
+xlate_notes_LDADD = $(libelf)
+elfrdwrnop_LDADD = $(libelf)
+dwelf_elf_e_machine_string_LDADD = $(libelf) $(libdw)
# 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/addsections.c b/tests/addsections.c
index cc8d0655..c1b0fa81 100644
--- a/tests/addsections.c
+++ b/tests/addsections.c
@@ -70,9 +70,9 @@ setshstrndx (Elf *elf, size_t ndx)
/* Will add nr new '.extra' sections and a new '.new_shstrtab' section
at the end. */
static void
-add_sections (const char *name, size_t nr, int use_mmap)
+add_sections (const char *name, size_t nr, int use_mmap, size_t sec_size)
{
- printf ("add_sections '%s': %zd\n", name, nr);
+ printf ("add_sections '%s': %zd (sec_size: %zd)\n", name, nr, sec_size);
int fd = open (name, O_RDWR);
if (fd < 0)
@@ -149,6 +149,25 @@ add_sections (const char *name, size_t nr, int use_mmap)
exit (1);
}
+ void *buf;
+ size_t bufsz;
+ if (sec_size == 0)
+ {
+ buf = strdup ("extra");
+ bufsz = strlen ("extra") + 1;
+ }
+ else
+ {
+ buf = malloc (sec_size);
+ if (buf == NULL)
+ {
+ printf ("cannot allocate buffer data of %zd bytes\n", sec_size);
+ exit (1);
+ }
+ memset (buf, 0xAA, sec_size);
+ bufsz = sec_size;
+ }
+
// Add lots of .extra sections...
size_t cnt = 0;
while (cnt++ < nr)
@@ -169,8 +188,8 @@ add_sections (const char *name, size_t nr, int use_mmap)
exit (1);
}
- data->d_size = strlen ("extra") + 1;
- data->d_buf = "extra";
+ data->d_size = bufsz;
+ data->d_buf = buf;
data->d_type = ELF_T_BYTE;
data->d_align = 1;
@@ -274,6 +293,7 @@ add_sections (const char *name, size_t nr, int use_mmap)
exit (1);
}
+ free (buf);
free (new_shstrtab_buf);
}
@@ -282,10 +302,11 @@ main (int argc, char *argv[])
{
elf_version (EV_CURRENT);
- /* Takes the given file, and adds the given number of sections. */
- if (argc < 3 || argc > 4)
+ /* Takes the given file, and adds the given number of sections.
+ Optionally using mmap and optionally using a given section size. */
+ if (argc < 3 || argc > 5)
{
- fprintf (stderr, "addsections [--mmap] nr elf.file\n");
+ fprintf (stderr, "addsections [--mmap] nr elf.file [sec_size]\n");
exit (1);
}
@@ -298,8 +319,13 @@ main (int argc, char *argv[])
}
size_t nr = atoi (argv[argn++]);
- const char *file = argv[argn];
- add_sections (file, nr, use_mmap);
+ const char *file = argv[argn++];
+
+ size_t sec_size = 0;
+ if (argn < argc)
+ sec_size = atol (argv[argn++]);
+
+ add_sections (file, nr, use_mmap, sec_size);
return 0;
}
diff --git a/tests/backtrace-dwarf.c b/tests/backtrace-dwarf.c
index dfbf1856..f446bc3b 100644
--- a/tests/backtrace-dwarf.c
+++ b/tests/backtrace-dwarf.c
@@ -101,7 +101,7 @@ frame_callback (Dwfl_Frame *state, void *frame_arg)
if (mod)
symname = dwfl_module_addrname (mod, pc_adjusted);
- printf ("%#" PRIx64 "\t%s\n", (uint64_t) pc, symname);
+ printf ("%#" PRIx64 "\t%s\n", (uint64_t) pc, symname ?: "<null>");
if (symname && (strcmp (symname, "main") == 0
|| strcmp (symname, ".main") == 0))
diff --git a/tests/backtrace.c b/tests/backtrace.c
index 05e8ef82..36c8b8c4 100644
--- a/tests/backtrace.c
+++ b/tests/backtrace.c
@@ -185,7 +185,7 @@ frame_callback (Dwfl_Frame *state, void *frame_arg)
symname = dwfl_module_addrname (mod, pc_adjusted);
printf ("#%2d %#" PRIx64 "%4s\t%s\n", *framenop, (uint64_t) pc,
- ! isactivation ? "- 1" : "", symname);
+ ! isactivation ? "- 1" : "", symname ?: "<null>");
pid_t tid = dwfl_thread_tid (thread);
callback_verify (tid, *framenop, pc, symname, dwfl);
(*framenop)++;
@@ -296,7 +296,6 @@ prepare_thread (pid_t pid2 __attribute__ ((unused)),
#include <asm/unistd.h>
#include <unistd.h>
-#define tgkill(pid, tid, sig) syscall (__NR_tgkill, (pid), (tid), (sig))
static void
report_pid (Dwfl *dwfl, pid_t pid)
diff --git a/tests/dwelf_elf_e_machine_string.c b/tests/dwelf_elf_e_machine_string.c
new file mode 100644
index 00000000..1df2b233
--- /dev/null
+++ b/tests/dwelf_elf_e_machine_string.c
@@ -0,0 +1,64 @@
+/* Test program for dwelf_elf_e_machine_string
+ 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 <assert.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <string.h>
+#include <unistd.h>
+
+#include ELFUTILS_HEADER(dwelf)
+
+int
+main (int argc, char **argv)
+{
+ int i;
+ for (i = 1; i < argc; i++)
+ {
+ long val;
+ int em;
+ const char *machine;
+
+ if (strncmp ("0x", argv[i], 2) == 0)
+ val = strtol (&argv[i][2], NULL, 16);
+ else
+ val = strtol (argv[i], NULL, 10);
+
+ if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
+ || (errno != 0 && val == 0))
+ {
+ perror ("strtol");
+ exit (EXIT_FAILURE);
+ }
+
+ em = val;
+ assert (em == val);
+
+ machine = dwelf_elf_e_machine_string (em);
+ printf ("0x%x %s\n", em, machine);
+ assert (machine != NULL);
+ }
+
+ return 0;
+}
diff --git a/tests/elfcopy.c b/tests/elfcopy.c
index 9000cc96..4542222e 100644
--- a/tests/elfcopy.c
+++ b/tests/elfcopy.c
@@ -69,9 +69,11 @@ setshstrndx (Elf *elf, size_t ndx)
/* Copies all elements of an ELF file either using mmap or read. */
static void
-copy_elf (const char *in, const char *out, bool use_mmap)
+copy_elf (const char *in, const char *out, bool use_mmap, bool reverse_offs)
{
- printf ("\ncopy_elf: %s -> %s (%s)\n", in, out, use_mmap ? "mmap" : "read");
+ printf ("\ncopy_elf: %s -> %s (%s,%s)\n", in, out,
+ use_mmap ? "mmap" : "read",
+ reverse_offs ? "reverse" : "same");
/* Existing ELF file. */
int fda = open (in, O_RDONLY);
@@ -182,8 +184,28 @@ copy_elf (const char *in, const char *out, bool use_mmap)
}
}
+ GElf_Off *offs = NULL;
+ size_t shnum;
+ if (reverse_offs)
+ {
+ if (elf_getshdrnum (elfa, &shnum) < 0)
+ {
+ printf ("couldn't get shdrnum: %s\n", elf_errmsg (-1));
+ exit (1);
+ }
+
+ offs = (GElf_Off *) malloc (shnum * sizeof (GElf_Off));
+ if (offs == NULL)
+ {
+ printf ("couldn't allocate memory for offs\n");
+ exit (1);
+ }
+ }
+
/* Copy all sections, headers and data. */
Elf_Scn *scn = NULL;
+ size_t last_off = 0;
+ GElf_Shdr last_shdr = { .sh_type = SHT_NULL };
while ((scn = elf_nextscn (elfa, scn)) != NULL)
{
/* Get the header. */
@@ -194,6 +216,35 @@ copy_elf (const char *in, const char *out, bool use_mmap)
exit (1);
}
+ if (reverse_offs)
+ {
+ offs[last_off] = shdr.sh_offset;
+
+ if (last_shdr.sh_type != SHT_NULL
+ && last_shdr.sh_addralign == shdr.sh_addralign
+ && shdr.sh_addralign == 1
+ && last_shdr.sh_type != SHT_NOBITS
+ && shdr.sh_type != SHT_NOBITS
+ && last_shdr.sh_offset + last_shdr.sh_size == shdr.sh_offset
+ && (phnum == 0
+ || ((shdr.sh_flags & SHF_ALLOC) == 0
+ && (last_shdr.sh_flags & SHF_ALLOC) == 0)))
+ {
+ printf ("Swapping offsets of section %zd and %zd\n",
+ last_off, last_off + 1);
+ GElf_Word off = offs[last_off - 1];
+ offs[last_off - 1] = off + shdr.sh_size;
+ offs[last_off] = off;
+ last_shdr.sh_type = SHT_NULL;
+ }
+ else
+ {
+ last_shdr = shdr;
+ offs[last_off] = shdr.sh_offset;
+ }
+ last_off++;
+ }
+
/* Create new section. */
Elf_Scn *new_scn = elf_newscn (elfb);
if (new_scn == NULL)
@@ -223,9 +274,34 @@ copy_elf (const char *in, const char *out, bool use_mmap)
}
}
- /* Write everything to disk. If there are any phdrs then we want
- the exact same layout. Do we want ELF_F_PERMISSIVE? */
- if (phnum > 0)
+ if (reverse_offs)
+ {
+ last_off = 0;
+ scn = NULL;
+ while ((scn = elf_nextscn (elfb, scn)) != NULL)
+ {
+ GElf_Shdr shdr;
+ if (gelf_getshdr (scn, &shdr) == NULL)
+ {
+ printf ("couldn't get shdr for updating: %s\n", elf_errmsg (-1));
+ exit (1);
+ }
+
+ shdr.sh_offset = offs[last_off++];
+
+ if (gelf_update_shdr (scn, &shdr) == 0)
+ {
+ printf ("couldn't update shdr sh_off: %s\n", elf_errmsg (-1));
+ exit (1);
+ }
+ }
+ free (offs);
+ }
+
+ /* Write everything to disk. If there are any phdrs, or we want to
+ update the offsets, then we want the exact same layout. Do we
+ want ELF_F_PERMISSIVE? */
+ if (phnum > 0 || reverse_offs)
elf_flagelf (elfb, ELF_C_SET, ELF_F_LAYOUT);
if (elf_update (elfb, ELF_C_WRITE) < 0)
{
@@ -264,9 +340,9 @@ main (int argc, const char *argv[])
elf_version (EV_CURRENT);
/* Takes the given file, and create a new identical one. */
- if (argc < 3 || argc > 4)
+ if (argc < 3 || argc > 5)
{
- fprintf (stderr, "elfcopy [--mmap] in.elf out.elf\n");
+ fprintf (stderr, "elfcopy [--mmap] [--reverse-offs] in.elf out.elf\n");
exit (1);
}
@@ -278,9 +354,16 @@ main (int argc, const char *argv[])
argn++;
}
+ bool reverse_offs = false;
+ if (strcmp (argv[argn], "--reverse-offs") == 0)
+ {
+ reverse_offs = true;
+ argn++;
+ }
+
const char *in = argv[argn++];
const char *out = argv[argn];
- copy_elf (in, out, use_mmap);
+ copy_elf (in, out, use_mmap, reverse_offs);
return 0;
}
diff --git a/tests/elfrdwrnop.c b/tests/elfrdwrnop.c
new file mode 100644
index 00000000..997150b9
--- /dev/null
+++ b/tests/elfrdwrnop.c
@@ -0,0 +1,100 @@
+/* Test program for reading and writing out the same file in-place
+ 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 <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include ELFUTILS_HEADER(elf)
+#include <gelf.h>
+
+
+int
+main (int argc, const char *argv[])
+{
+ /* Takes the given file, and create a new identical one. */
+ if (argc != 2)
+ {
+ fprintf (stderr, "elfrdwrnop elf-file\n");
+ exit (1);
+ }
+
+ elf_version (EV_CURRENT);
+
+ const char *name = argv[1];
+ printf ("elfrdwrdnop %s\n", name);
+
+ int fd = open (name, O_RDWR);
+ if (fd < 0)
+ {
+ fprintf (stderr, "Couldn't open file '%s': %s\n",
+ name, strerror (errno));
+ exit (1);
+ }
+
+ Elf *elf = elf_begin (fd, ELF_C_RDWR, NULL);
+ if (elf == NULL)
+ {
+ fprintf (stderr, "Couldn't open ELF file '%s': %s\n",
+ name, elf_errmsg (-1));
+ exit (1);
+ }
+
+ /* Write everything to disk. If there are any phdrs, then we want
+ the exact same layout. */
+ size_t phnum;
+ if (elf_getphdrnum (elf, &phnum) != 0)
+ {
+ printf ("cannot get phdrs: %s\n", elf_errmsg (-1));
+ exit (1);
+ }
+
+ if (phnum > 0)
+ elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT);
+
+ if (elf_update (elf, ELF_C_WRITE) < 0)
+ {
+ printf ("failure in elf_update: %s\n", elf_errmsg (-1));
+ exit (1);
+ }
+
+ if (elf_end (elf) != 0)
+ {
+ printf ("couldn't cleanup elf '%s': %s\n", name, elf_errmsg (-1));
+ exit (1);
+ }
+
+ if (close (fd) != 0)
+ {
+ printf ("couldn't close '%s': %s\n", name, strerror (errno));
+ exit (1);
+ }
+
+ return 0;
+}
diff --git a/tests/hello_csky.ko.bz2 b/tests/hello_csky.ko.bz2
new file mode 100644
index 00000000..ed98c7f6
--- /dev/null
+++ b/tests/hello_csky.ko.bz2
Binary files differ
diff --git a/tests/run-addrcfi.sh b/tests/run-addrcfi.sh
index 76db7d2d..e9f55644 100755
--- a/tests/run-addrcfi.sh
+++ b/tests/run-addrcfi.sh
@@ -3671,3 +3671,4 @@ testrun_compare ${abs_builddir}/addrcfi -e testfile-x32 0x00400390 <<\EOF
control reg66 (%fsw): undefined
handle_cfi no CFI (.debug_frame): no error
EOF
+
diff --git a/tests/run-dwelf_elf_e_machine_string.sh b/tests/run-dwelf_elf_e_machine_string.sh
new file mode 100755
index 00000000..ba6d0e63
--- /dev/null
+++ b/tests/run-dwelf_elf_e_machine_string.sh
@@ -0,0 +1,31 @@
+#! /bin/bash
+# Test to make sure all EM values in elf.h are recognized
+# 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
+
+# Get all known EM values from elf.h and pass them through the
+# preprocessor to get the numbers. Call dwelf_elf_e_machine_string on
+# all of them.
+EM_VALUES=$(grep ^\#define\ EM_ ${abs_srcdir}/../libelf/elf.h \
+ | cut -f2 -d\ | cut -f1 | grep -v ^EM_NUM$ | xargs echo)
+# echo "EM_VALUES: $EM_VALUES"
+EM_NUMBERS=$((cat ${abs_srcdir}/../libelf/elf.h; echo "$EM_VALUES") \
+ | gcc -E - | tail -1)
+# echo "EM_NUMBERS: $EM_NUMBERS"
+
+testrun ${abs_top_builddir}/tests/dwelf_elf_e_machine_string $EM_NUMBERS
diff --git a/tests/run-elfclassify-self.sh b/tests/run-elfclassify-self.sh
new file mode 100755
index 00000000..c48ab9c9
--- /dev/null
+++ b/tests/run-elfclassify-self.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# 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
+
+testrun_on_self ${abs_top_builddir}/src/elfclassify --elf-file
+testrun_on_self ${abs_top_builddir}/src/elfclassify --not-core
+testrun_on_self ${abs_top_builddir}/src/elfclassify --unstripped
+testrun_on_self ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module
+
+testrun_on_self_lib ${abs_top_builddir}/src/elfclassify --shared
+testrun_on_self_lib ${abs_top_builddir}/src/elfclassify --loadable
+testrun_on_self_lib ${abs_top_builddir}/src/elfclassify --not-executable
+testrun_on_self_lib ${abs_top_builddir}/src/elfclassify --not-program
+
+testrun_on_self_exe ${abs_top_builddir}/src/elfclassify --executable
+testrun_on_self_exe ${abs_top_builddir}/src/elfclassify --program
+testrun_on_self_exe ${abs_top_builddir}/src/elfclassify --loadable
+testrun_on_self_exe ${abs_top_builddir}/src/elfclassify --not-shared
+
+testrun ${abs_top_builddir}/src/elfclassify --not-shared $self_test_files_obj
+testrun ${abs_top_builddir}/src/elfclassify --not-executable $self_test_files_obj
diff --git a/tests/run-elfclassify.sh b/tests/run-elfclassify.sh
new file mode 100755
index 00000000..5a849bbd
--- /dev/null
+++ b/tests/run-elfclassify.sh
@@ -0,0 +1,327 @@
+#!/bin/sh
+# 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
+
+core_files=\
+"testfile_aarch64_core \
+ testfile-backtrace-demangle.core \
+ testfiledwarfinlines.core \
+ testfile_i686_core \
+ testfile-m68k-core \
+ testfile-riscv64-core \
+ backtrace.aarch64.core \
+ backtrace.i386.core \
+ backtrace.ppc.core \
+ backtrace.s390.core"
+
+testfiles $core_files
+
+echo "elfclassify --core"
+testrun ${abs_top_builddir}/src/elfclassify --core $core_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --core --print $core_files <<EOF
+$(echo $core_files | sed -e "s/ /\n/g")
+EOF
+
+echo "core files are not programs"
+testrun ${abs_top_builddir}/src/elfclassify --not-program $core_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-program --print $core_files <<EOF
+$(echo $core_files | sed -e "s/ /\n/g")
+EOF
+
+echo "core files are not shared"
+testrun ${abs_top_builddir}/src/elfclassify --not-shared $core_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-shared --print $core_files <<EOF
+$(echo $core_files | sed -e "s/ /\n/g")
+EOF
+
+echo "core files are not kernel-modules"
+testrun ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module $core_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module --print $core_files <<EOF
+$(echo $core_files | sed -e "s/ /\n/g")
+EOF
+
+echo "core files are not debug-only"
+testrun ${abs_top_builddir}/src/elfclassify --not-debug-only $core_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-debug-only --print $core_files <<EOF
+$(echo $core_files | sed -e "s/ /\n/g")
+EOF
+
+object_files=\
+"debug-ranges-no-lowpc.o \
+ testfile-annobingroup-i386.o \
+ testfile-bpf-dis1.o \
+ testfile-debug-rel-g.o \
+ testfile-gnu-property-note.o"
+
+testfiles $object_files
+
+echo "elfclassify --elf-file"
+testrun ${abs_top_builddir}/src/elfclassify --elf-file $object_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --elf-file --print $object_files <<EOF
+$(echo $object_files | sed -e "s/ /\n/g")
+EOF
+
+echo "object files are not archives"
+testrun ${abs_top_builddir}/src/elfclassify --not-elf-archive $object_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-elf-archive --print $object_files <<EOF
+$(echo $object_files | sed -e "s/ /\n/g")
+EOF
+
+echo "object files are not core files"
+testrun ${abs_top_builddir}/src/elfclassify --not-core $object_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-core --print $object_files <<EOF
+$(echo $object_files | sed -e "s/ /\n/g")
+EOF
+
+echo "object files are not program files"
+testrun ${abs_top_builddir}/src/elfclassify --not-program $object_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-program --print $object_files <<EOF
+$(echo $object_files | sed -e "s/ /\n/g")
+EOF
+
+echo "object files are not shared files"
+testrun ${abs_top_builddir}/src/elfclassify --not-shared $object_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-shared --print $object_files <<EOF
+$(echo $object_files | sed -e "s/ /\n/g")
+EOF
+
+echo "object files are not kernel modules"
+testrun ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module $object_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module --print $object_files <<EOF
+$(echo $object_files | sed -e "s/ /\n/g")
+EOF
+
+echo "object files are not debug-only files"
+testrun ${abs_top_builddir}/src/elfclassify --not-debug-only $object_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-debug-only --print $object_files <<EOF
+$(echo $object_files | sed -e "s/ /\n/g")
+EOF
+
+ar_files="testarchive64.a"
+
+testfiles $ar_files
+
+echo "elfclassify --elf-archive"
+testrun ${abs_top_builddir}/src/elfclassify --elf-archive $ar_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --elf-archive --print $ar_files <<EOF
+$(echo $ar_files | sed -e "s/ /\n/g")
+EOF
+
+echo "archives are not elf-files"
+testrun ${abs_top_builddir}/src/elfclassify --not-elf-file $ar_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-elf-file --print $ar_files <<EOF
+$(echo $ar_files | sed -e "s/ /\n/g")
+EOF
+
+echo "archives are not core files"
+testrun ${abs_top_builddir}/src/elfclassify --not-core $ar_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-core --print $ar_files <<EOF
+$(echo $ar_files | sed -e "s/ /\n/g")
+EOF
+
+echo "archives are not program files"
+testrun ${abs_top_builddir}/src/elfclassify --not-program $ar_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-program --print $ar_files <<EOF
+$(echo $ar_files | sed -e "s/ /\n/g")
+EOF
+
+echo "archives are not shared files"
+testrun ${abs_top_builddir}/src/elfclassify --not-shared $ar_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-shared --print $ar_files <<EOF
+$(echo $ar_files | sed -e "s/ /\n/g")
+EOF
+
+lib_files=\
+"testfile52-32.noshdrs.so \
+ libtestfile_multi_shared.so \
+ testfile52-32.prelink.so \
+ testfile52-32.so
+ testfile54-64.noshdrs.so \
+ testfile54-64.prelink.so \
+ testfile54-64.so \
+ testlib_dynseg.so"
+
+testfiles $lib_files
+
+echo "elfclassify --shared"
+testrun ${abs_top_builddir}/src/elfclassify --shared $lib_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --shared --print $lib_files <<EOF
+$(echo $lib_files | sed -e "s/ /\n/g")
+EOF
+
+echo "shared files are loadable"
+testrun ${abs_top_builddir}/src/elfclassify --loadable $lib_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --loadable --print $lib_files <<EOF
+$(echo $lib_files | sed -e "s/ /\n/g")
+EOF
+
+echo "shared files are not executables"
+testrun ${abs_top_builddir}/src/elfclassify --not-executable $lib_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-executable --print $lib_files <<EOF
+$(echo $lib_files | sed -e "s/ /\n/g")
+EOF
+
+echo "shared files are not debug-only"
+testrun ${abs_top_builddir}/src/elfclassify --not-debug-only $lib_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-debug-only --print $lib_files <<EOF
+$(echo $lib_files | sed -e "s/ /\n/g")
+EOF
+
+echo "shared files are not kernel modules"
+testrun ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module $lib_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module --print $lib_files <<EOF
+$(echo $lib_files | sed -e "s/ /\n/g")
+EOF
+
+exe_files=\
+"backtrace.aarch64.exec \
+ backtrace.i386.exec \
+ backtrace.ppc.exec \
+ backtrace.s390x.exec \
+ testfile70.exec \
+ test-offset-loop \
+ testfilebaztab \
+ testfilebaztabppc64"
+
+testfiles $exe_files
+
+echo "elfclassify --program"
+testrun ${abs_top_builddir}/src/elfclassify --program $exe_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --program --print $exe_files <<EOF
+$(echo $exe_files | sed -e "s/ /\n/g")
+EOF
+
+echo "programs are executables (in this case)"
+testrun ${abs_top_builddir}/src/elfclassify --executable $exe_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --executable --print $exe_files <<EOF
+$(echo $exe_files | sed -e "s/ /\n/g")
+EOF
+
+echo "programs are not shared libraries (in this case)"
+testrun ${abs_top_builddir}/src/elfclassify --not-shared $exe_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-shared --print $exe_files <<EOF
+$(echo $exe_files | sed -e "s/ /\n/g")
+EOF
+
+echo "programs are not kernel-modules"
+testrun ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module $exe_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-linux-kernel-module --print $exe_files <<EOF
+$(echo $exe_files | sed -e "s/ /\n/g")
+EOF
+
+echo "programs are not debug-only"
+testrun ${abs_top_builddir}/src/elfclassify --not-debug-only $exe_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-debug-only --print $exe_files <<EOF
+$(echo $exe_files | sed -e "s/ /\n/g")
+EOF
+
+kmod_files=\
+"hello_aarch64.ko \
+ hello_csky.ko \
+ hello_i386.ko \
+ hello_m68k.ko \
+ hello_ppc64.ko \
+ hello_riscv64.ko \
+ hello_s390.ko \
+ hello_x86_64.ko"
+
+testfiles $kmod_files
+
+echo "elfclassify --linux-kernel-module"
+testrun ${abs_top_builddir}/src/elfclassify --linux-kernel-module $kmod_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --linux-kernel-module --print $kmod_files <<EOF
+$(echo $kmod_files | sed -e "s/ /\n/g")
+EOF
+
+echo "kmods are unstripped"
+testrun ${abs_top_builddir}/src/elfclassify --unstripped $kmod_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --unstripped --print $kmod_files <<EOF
+$(echo $kmod_files | sed -e "s/ /\n/g")
+EOF
+
+echo "kmods are not debug-only"
+testrun ${abs_top_builddir}/src/elfclassify --not-debug-only $kmod_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-debug-only --print $kmod_files <<EOF
+$(echo $kmod_files | sed -e "s/ /\n/g")
+EOF
+
+echo "kmods are not loabable (in the normal sense)"
+testrun ${abs_top_builddir}/src/elfclassify --not-loadable $kmod_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-loadable --print $kmod_files <<EOF
+$(echo $kmod_files | sed -e "s/ /\n/g")
+EOF
+
+echo "gnu compressed kmods are unstripped"
+${abs_top_builddir}/src/elfcompress -t gnu --force $kmod_files
+testrun ${abs_top_builddir}/src/elfclassify --unstripped $kmod_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --unstripped --print $kmod_files <<EOF
+$(echo $kmod_files | sed -e "s/ /\n/g")
+EOF
+
+debug_files=\
+"testfile15.debug \
+ testfile35.debug \
+ testfile40.debug \
+ testfile48.debug \
+ testfile53-32.debug \
+ testfile53-64.debug \
+ testfilebazdbg.debug \
+ testfilebazdbgppc64.debug \
+ addrx_constx-4.dwo \
+ addrx_constx-5.dwo"
+
+testfiles $debug_files
+
+echo "elfclassify --debug-only"
+testrun ${abs_top_builddir}/src/elfclassify --debug-only $debug_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --debug-only --print $debug_files <<EOF
+$(echo $debug_files | sed -e "s/ /\n/g")
+EOF
+
+echo "debug-only files are unstripped"
+testrun ${abs_top_builddir}/src/elfclassify --unstripped $debug_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --unstripped --print $debug_files <<EOF
+$(echo $debug_files | sed -e "s/ /\n/g")
+EOF
+
+echo "debug-only files are not programs"
+testrun ${abs_top_builddir}/src/elfclassify --not-program $debug_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-program --print $debug_files <<EOF
+$(echo $debug_files | sed -e "s/ /\n/g")
+EOF
+
+echo "debug-only files are not shared"
+testrun ${abs_top_builddir}/src/elfclassify --not-shared $debug_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --not-shared --print $debug_files <<EOF
+$(echo $debug_files | sed -e "s/ /\n/g")
+EOF
+
+echo "compress the debug sections and try again"
+${abs_top_builddir}/src/elfcompress -t gnu --force $debug_files
+
+echo "again unstripped"
+testrun ${abs_top_builddir}/src/elfclassify --unstripped $debug_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --unstripped --print $debug_files <<EOF
+$(echo $debug_files | sed -e "s/ /\n/g")
+EOF
+
+echo "again debug-only"
+testrun ${abs_top_builddir}/src/elfclassify --debug-only $debug_files
+testrun_compare ${abs_top_builddir}/src/elfclassify --debug-only --print $debug_files <<EOF
+$(echo $debug_files | sed -e "s/ /\n/g")
+EOF
diff --git a/tests/run-large-elf-file.sh b/tests/run-large-elf-file.sh
new file mode 100755
index 00000000..6146cfed
--- /dev/null
+++ b/tests/run-large-elf-file.sh
@@ -0,0 +1,114 @@
+#! /bin/bash
+# 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
+
+# Only run on 64bit systems, 32bit systems don't support > 4GB
+# ELF files.
+long_bit=$(getconf LONG_BIT)
+echo "long_bit: $long_bit"
+if test $long_bit -ne 64; then
+ echo "Only 64bit systems can create > 4GB ELF files"
+ exit 77
+fi
+
+# These tests need lots of disk space since they test files > 4GB.
+# Skip if there just isn't enough (2.5 * 4 = 10GB).
+space_available=$[$(stat -f --format="%a*%S" .)/(1024 * 1024 * 1024)]
+echo "space_available: $space_available"
+if test $space_available -lt 10; then
+ echo "Not enough disk space, need at least 10GB available"
+ exit 77
+fi
+
+# Make sure the files fit into memory, assume 6GB needed (2.5 * 2 + 1 extra).
+# Running under valgrind might need even more.
+mem_needed=6
+if [ "x$VALGRIND_CMD" != "x" ]; then
+ mem_needed=$[${mem_needed} + 2]
+fi
+echo "mem_needed: $mem_needed"
+mem_available=$(free -g | grep ^Mem: | awk -F ' +' '{print $7}')
+echo "mem_available: $mem_available"
+if test $mem_available -lt $mem_needed; then
+ echo "Need at least ${mem_needed}GB free available memory"
+ exit 77
+fi
+
+# Make sure the disk is reasonably fast, should be able to write 100MB/s
+fast_disk=1
+timeout -s9 10s dd conv=fsync if=/dev/zero of=tempfile bs=1M count=1K \
+ || fast_disk=0; rm tempfile
+if test $fast_disk -eq 0; then
+ echo "File system not fast enough, need at least 100MB/s"
+ exit 77
+fi
+
+# NOTE: test file will be mangled and removed!
+test_file ()
+{
+ in_file="$1"
+ readelf_out="${in_file}.readelf.out"
+ out_file_strip="${in_file}.strip"
+ out_file_debug="${in_file}.debug"
+
+ testfiles ${in_file}
+ tempfiles ${readelf_out} ${out_file_mmap} ${out_file_strip} ${out_file_debug}
+
+ # Add two 2GB sections to the file.
+ echo "addsections 2 ${in_file} 2147483648"
+ testrun ${abs_builddir}/addsections 2 ${in_file} 2147483648
+ testrun ${abs_top_builddir}/src/readelf -S ${in_file} > ${readelf_out}
+ nr=$(grep '.extra' ${readelf_out} | wc -l)
+ if test ${nr} != 2; then
+ # Show what went wrong
+ cat ${readelf_out}
+ exit 1
+ fi
+
+ echo "strip -o ${out_file_strip} -f ${out_file_debug} ${in_file}"
+ testrun ${abs_top_builddir}/src/strip -o ${out_file_strip} \
+ -f ${out_file_debug} ${in_file}
+
+ echo "elflint --gnu ${out_file_strip}"
+ testrun ${abs_top_builddir}/src/elflint --gnu ${out_file_strip}
+
+ echo "elflint --gnu -d ${out_file_debug}"
+ testrun ${abs_top_builddir}/src/elflint --gnu -d ${out_file_debug}
+
+ # Now test unstrip recombining those files.
+ echo "unstrip ${out_file_strip} ${out_file_debug}"
+ testrun ${abs_top_builddir}/src/unstrip ${out_file_strip} ${out_file_debug}
+
+ echo "elfcmp ${out_file} ${out_file_strip}"
+ testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file_debug}
+
+ # Remove the temp files immediately, they are big...
+ rm -f ${in_file} ${out_file_strip} ${out_file_debug}
+}
+
+# A collection of random testfiles to test 64bit, little/big endian
+# and non-ET_REL (with phdrs)/ET_REL (without phdrs).
+# Don't test 32bit, they cannot go beyond 4GB.
+
+# 64bit, little endian, rel
+test_file testfile38
+
+# 64bit, big endian, non-rel
+test_file testfile27
+
+exit 0
diff --git a/tests/run-readelf-discr.sh b/tests/run-readelf-discr.sh
new file mode 100755
index 00000000..dc84d827
--- /dev/null
+++ b/tests/run-readelf-discr.sh
@@ -0,0 +1,337 @@
+#! /bin/sh
+# 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
+
+# = rng.ads =
+# package Rng is
+#
+# type Rec (I : Integer) is record
+# case I is
+# when Positive =>
+# case I is
+# when 1..15 | 17 | 23 =>
+# null;
+# when others =>
+# J : Integer;
+# end case;
+# when -52..-1 =>
+# Q: Integer;
+# when -64 =>
+# R: Boolean;
+# when others =>
+# null;
+# end case;
+# end record;
+#
+# R : Rec (1);
+#
+# end Rng;
+
+# = urng.ads =
+#
+# package Urng is
+#
+# type Unsigned is mod 65536;
+# type Rec (U : Unsigned) is record
+# case U is
+# when 17 | 23 | 32768..65535 =>
+# null;
+# when 256 =>
+# B: Boolean;
+# when others =>
+# I : Integer;
+# end case;
+# end record;
+#
+# R : Rec (1);
+#
+# end Urng;
+
+# gcc -c -g -fgnat-encodings=minimal -gstrict-dwarf rng.ads
+# eu-strip -g -f rng.debug rng.o
+# gcc -c -g -fgnat-encodings=minimal -gstrict-dwarf urng.ads
+# eu-strip -g -f urng.debug urng.o
+
+testfiles testfile-rng.debug testfile-urng.debug
+
+testrun_compare ${abs_top_builddir}/src/readelf -U --debug-dump=info testfile-rng.debug testfile-urng.debug <<EOF
+
+testfile-rng.debug:
+
+
+DWARF section [ 5] '.debug_info' at offset 0x40:
+ [Offset]
+ Compilation unit at offset 0:
+ Version: 4, Abbreviation section offset: 0, Address size: 8, Offset size: 4
+ [ b] compile_unit abbrev: 1
+ producer (strp) "GNU Ada 9.1.1 20190503 (Red Hat 9.1.1-1) -g -fgnat-encodings=minimal -gstrict-dwarf -mtune=generic -march=x86-64"
+ language (data1) Ada95 (13)
+ name (strp) "rng.ads"
+ comp_dir (strp) "/home/mark"
+ low_pc (addr) 000000000000000000
+ high_pc (data8) 2016 (0x00000000000007e0)
+ stmt_list (sec_offset) 0
+ [ 2d] structure_type abbrev: 2
+ name (strp) "rng__rec"
+ byte_size (exprloc)
+ [ 0] push_object_address
+ [ 1] deref_size 4
+ [ 3] call4 [ c6]
+ [ 8] plus_uconst 7
+ [10] const1s -4
+ [12] and
+ decl_file (data1) rng.ads (1)
+ decl_line (data1) 3
+ decl_column (data1) 9
+ sibling (ref4) [ ab]
+ [ 47] member abbrev: 3
+ name (string) "i"
+ decl_file (data1) rng.ads (1)
+ decl_line (data1) 3
+ decl_column (data1) 14
+ type (ref4) [ fe]
+ data_member_location (data1) 0
+ [ 52] variant_part abbrev: 4
+ discr (ref4) [ 47]
+ [ 57] variant abbrev: 5
+ discr_list (block1) range 1..2147483647
+ sibling (ref4) [ 81]
+ [ 64] variant_part abbrev: 4
+ discr (ref4) [ 47]
+ [ 69] variant abbrev: 6
+ discr_list (block1) range 1..15, label 17, label 23
+ [ 72] variant abbrev: 7
+ [ 73] member abbrev: 3
+ name (string) "j"
+ decl_file (data1) rng.ads (1)
+ decl_line (data1) 10
+ decl_column (data1) 19
+ type (ref4) [ fe]
+ data_member_location (data1) 4
+ [ 81] variant abbrev: 5
+ discr_list (block1) range -52..-1
+ sibling (ref4) [ 96]
+ [ 8a] member abbrev: 3
+ name (string) "q"
+ decl_file (data1) rng.ads (1)
+ decl_line (data1) 13
+ decl_column (data1) 13
+ type (ref4) [ fe]
+ data_member_location (data1) 4
+ [ 96] variant abbrev: 8
+ discr_value (sdata) -64
+ sibling (ref4) [ a8]
+ [ 9c] member abbrev: 3
+ name (string) "r"
+ decl_file (data1) rng.ads (1)
+ decl_line (data1) 15
+ decl_column (data1) 13
+ type (ref4) [ 105]
+ data_member_location (data1) 4
+ [ a8] variant abbrev: 9
+ [ ab] dwarf_procedure abbrev: 10
+ location (exprloc)
+ [ 0] dup
+ [ 1] lit0
+ [ 2] gt
+ [ 3] over
+ [ 4] lit15
+ [ 5] le
+ [ 6] and
+ [ 7] over
+ [ 8] lit17
+ [ 9] eq
+ [10] or
+ [11] over
+ [12] lit23
+ [13] eq
+ [14] or
+ [15] bra 22
+ [18] lit4
+ [19] skip 23
+ [22] lit0
+ [23] swap
+ [24] drop
+ [ c6] dwarf_procedure abbrev: 10
+ location (exprloc)
+ [ 0] dup
+ [ 1] lit0
+ [ 2] gt
+ [ 3] bra 36
+ [ 6] dup
+ [ 7] const1s -52
+ [ 9] lt
+ [10] over
+ [11] lit0
+ [12] ge
+ [13] or
+ [14] bra 21
+ [17] lit4
+ [18] skip 33
+ [21] dup
+ [22] const1s -64
+ [24] eq
+ [25] bra 32
+ [28] lit0
+ [29] skip 33
+ [32] lit4
+ [33] skip 52
+ [36] dup
+ [37] call4 [ ab]
+ [42] plus_uconst 3
+ [44] const1s -4
+ [46] and
+ [47] plus_uconst 3
+ [49] const1s -4
+ [51] and
+ [52] swap
+ [53] drop
+ [ fe] base_type abbrev: 11
+ byte_size (data1) 4
+ encoding (data1) signed (5)
+ name (strp) "integer"
+ artificial (flag_present) yes
+ [ 105] base_type abbrev: 12
+ byte_size (data1) 1
+ encoding (data1) boolean (2)
+ name (strp) "boolean"
+ [ 10c] variable abbrev: 13
+ name (strp) "rng__r"
+ decl_file (data1) rng.ads (1)
+ decl_line (data1) 21
+ decl_column (data1) 4
+ type (ref4) [ 2d]
+ external (flag_present) yes
+ location (exprloc)
+ [ 0] addr 0x7e4
+ [ 122] subprogram abbrev: 14
+ external (flag_present) yes
+ name (strp) "rng___elabs"
+ artificial (flag_present) yes
+ low_pc (addr) 0x0000000000000734
+ high_pc (data8) 22 (0x000000000000074a)
+ frame_base (exprloc)
+ [ 0] call_frame_cfa
+
+testfile-urng.debug:
+
+
+DWARF section [ 5] '.debug_info' at offset 0x40:
+ [Offset]
+ Compilation unit at offset 0:
+ Version: 4, Abbreviation section offset: 0, Address size: 8, Offset size: 4
+ [ b] compile_unit abbrev: 1
+ producer (strp) "GNU Ada 9.1.1 20190503 (Red Hat 9.1.1-1) -g -fgnat-encodings=minimal -gstrict-dwarf -mtune=generic -march=x86-64"
+ language (data1) Ada95 (13)
+ name (strp) "urng.ads"
+ comp_dir (strp) "/home/mark"
+ low_pc (addr) 000000000000000000
+ high_pc (data8) 977 (0x00000000000003d1)
+ stmt_list (sec_offset) 0
+ [ 2d] base_type abbrev: 2
+ byte_size (data1) 2
+ encoding (data1) unsigned (7)
+ name (strp) "urng__unsigned"
+ [ 34] structure_type abbrev: 3
+ name (strp) "urng__rec"
+ byte_size (exprloc)
+ [ 0] push_object_address
+ [ 1] deref_size 2
+ [ 3] call4 [ 8d]
+ [ 8] plus_uconst 7
+ [10] const1s -4
+ [12] and
+ decl_file (data1) urng.ads (1)
+ decl_line (data1) 4
+ decl_column (data1) 9
+ sibling (ref4) [ 8d]
+ [ 4e] member abbrev: 4
+ name (string) "u"
+ decl_file (data1) urng.ads (1)
+ decl_line (data1) 4
+ decl_column (data1) 14
+ type (ref4) [ 2d]
+ data_member_location (data1) 0
+ [ 59] variant_part abbrev: 5
+ discr (ref4) [ 4e]
+ [ 5e] variant abbrev: 6
+ discr_list (block1) label 17, label 23, range 32768..65535
+ [ 6b] variant abbrev: 7
+ discr_value (udata) 256
+ sibling (ref4) [ 7e]
+ [ 72] member abbrev: 4
+ name (string) "b"
+ decl_file (data1) urng.ads (1)
+ decl_line (data1) 9
+ decl_column (data1) 13
+ type (ref4) [ a4]
+ data_member_location (data1) 4
+ [ 7e] variant abbrev: 8
+ [ 7f] member abbrev: 4
+ name (string) "i"
+ decl_file (data1) urng.ads (1)
+ decl_line (data1) 11
+ decl_column (data1) 13
+ type (ref4) [ ab]
+ data_member_location (data1) 4
+ [ 8d] dwarf_procedure abbrev: 9
+ location (exprloc)
+ [ 0] dup
+ [ 1] lit17
+ [ 2] ne
+ [ 3] over
+ [ 4] lit23
+ [ 5] ne
+ [ 6] and
+ [ 7] over
+ [ 8] lit0
+ [ 9] ge
+ [10] and
+ [11] bra 18
+ [14] lit0
+ [15] skip 19
+ [18] lit4
+ [19] swap
+ [20] drop
+ [ a4] base_type abbrev: 2
+ byte_size (data1) 1
+ encoding (data1) boolean (2)
+ name (strp) "boolean"
+ [ ab] base_type abbrev: 10
+ byte_size (data1) 4
+ encoding (data1) signed (5)
+ name (strp) "integer"
+ artificial (flag_present) yes
+ [ b2] variable abbrev: 11
+ name (strp) "urng__r"
+ decl_file (data1) urng.ads (1)
+ decl_line (data1) 15
+ decl_column (data1) 4
+ type (ref4) [ 34]
+ external (flag_present) yes
+ location (exprloc)
+ [ 0] addr 0x3d8
+ [ c8] subprogram abbrev: 12
+ external (flag_present) yes
+ name (strp) "urng___elabs"
+ artificial (flag_present) yes
+ low_pc (addr) 0x0000000000000386
+ high_pc (data8) 22 (0x000000000000039c)
+ frame_base (exprloc)
+ [ 0] call_frame_cfa
+EOF
diff --git a/tests/run-reverse-sections-self.sh b/tests/run-reverse-sections-self.sh
new file mode 100755
index 00000000..71afd6ac
--- /dev/null
+++ b/tests/run-reverse-sections-self.sh
@@ -0,0 +1,45 @@
+#! /bin/sh
+# 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
+
+test_reverse_self ()
+{
+ in_file="$1"
+ base_name="$(basename ${in_file})"
+ out_file="${base_name}.rev"
+ out_file_mmap="${out_file}.mmap"
+
+ tempfiles ${out_file} ${out_file_mmap}
+
+ # Reverse the offsets (the files should still be the same otherwise)
+ testrun ${abs_builddir}/elfcopy --reverse-offs ${in_file} ${out_file}
+ testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
+ testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
+ # An in-place nop will likely revert them back
+ testrun ${abs_builddir}/elfrdwrnop ${out_file}
+ testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
+ testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
+}
+
+# Only really makes sense for ET_REL files, but try all, just to check
+# it also works if we keep the order for the allocated sections.
+for file in $self_test_files; do
+ test_reverse_self $file
+done
+
+exit 0
diff --git a/tests/run-reverse-sections.sh b/tests/run-reverse-sections.sh
new file mode 100755
index 00000000..102a1261
--- /dev/null
+++ b/tests/run-reverse-sections.sh
@@ -0,0 +1,69 @@
+#! /bin/sh
+# 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
+
+test_reverse ()
+{
+ in_file="$1"
+ out_file="${in_file}.rev"
+ out_file_mmap="${out_file}.mmap"
+
+ testfiles ${in_file}
+ tempfiles ${out_file} ${out_file_mmap}
+
+ # Reverse the offsets (the files should still be the same otherwise)
+ testrun ${abs_builddir}/elfcopy --reverse-offs ${in_file} ${out_file}
+ testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
+ testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
+ # An in-place nop will likely revert them back
+ testrun ${abs_builddir}/elfrdwrnop ${out_file}
+ testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
+ testrun ${abs_top_builddir}/src/elflint --gnu ${out_file}
+}
+
+# A collection of random testfiles to test 32/64bit, little/big endian
+# and non-ET_REL (with phdrs)/ET_REL (without phdrs).
+
+# 32bit, big endian, rel
+test_reverse testfile29
+
+# 64bit, big endian, rel
+test_reverse testfile23
+
+# 32bit, little endian, rel
+test_reverse testfile9
+
+# 64bit, little endian, rel
+test_reverse testfile38
+
+# 32bit, big endian, non-rel
+test_reverse testfile26
+
+# 64bit, big endian, non-rel
+test_reverse testfile27
+
+# 32bit, little endian, non-rel
+test_reverse testfile
+
+# 64bit, little endian, non-rel
+# Don't use testfile10. It has section headers in the middle of the file.
+# Same for testfile12. It is legal, but not the point of this testcase.
+# test_reverse testfile10
+test_reverse testfile13
+
+exit 0
diff --git a/tests/run-xlate-note.sh b/tests/run-xlate-note.sh
new file mode 100755
index 00000000..a9074182
--- /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/testfile-rng.debug.bz2 b/tests/testfile-rng.debug.bz2
new file mode 100644
index 00000000..58c1b76b
--- /dev/null
+++ b/tests/testfile-rng.debug.bz2
Binary files differ
diff --git a/tests/testfile-urng.debug.bz2 b/tests/testfile-urng.debug.bz2
new file mode 100644
index 00000000..4697f364
--- /dev/null
+++ b/tests/testfile-urng.debug.bz2
Binary files differ
diff --git a/tests/testfilecsky.bz2 b/tests/testfilecsky.bz2
new file mode 100644
index 00000000..dfee449e
--- /dev/null
+++ b/tests/testfilecsky.bz2
Binary files differ
diff --git a/tests/xlate_notes.c b/tests/xlate_notes.c
new file mode 100644
index 00000000..90a4ae27
--- /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;
+}