summaryrefslogtreecommitdiff
path: root/tests/elfrdwrnop.c
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2019-05-12 00:37:45 +0200
committerMark Wielaard <mark@klomp.org>2019-05-16 17:57:41 +0200
commitd7193bd7c9dc2a979352eee7fc446dacd3e97779 (patch)
tree3fbc54875c7c3a378d096a2689f4a19436e0bd2d /tests/elfrdwrnop.c
parenta117891a00507d188fdab415bd25554ace9ed7ba (diff)
downloadelfutils-d7193bd7c9dc2a979352eee7fc446dacd3e97779.tar.gz
libelf: Mark shdr_flags dirty if offset or size changes during update.
We forgot to mark the shdr_flags dirty when only the sh_size or sh_offset changed during elf_update (). This meant that if there were no other shdr changes we only wrote out the section data, but didn't write out the shdr table to the file. Add a testcase that puts some sections in the reverse order and then writes out the resulting file again without doing any other updates. This would show the issue after write out of the (re-reversed) ELF file (the .shstrtab section offset would be wrong causing all section names to be garbage). Also run a self test. Signed-off-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'tests/elfrdwrnop.c')
-rw-r--r--tests/elfrdwrnop.c100
1 files changed, 100 insertions, 0 deletions
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;
+}