Blame tests/elfrdwrnop.c

Packit 032894
/* Test program for reading and writing out the same file in-place
Packit 032894
   Copyright (C) 2019 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of the GNU General Public License as published by
Packit 032894
   the Free Software Foundation; either version 3 of the License, or
Packit 032894
   (at your option) any later version.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 032894
   GNU General Public License for more details.
Packit 032894
Packit 032894
   You should have received a copy of the GNU General Public License
Packit 032894
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <errno.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include <stdbool.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include <sys/types.h>
Packit 032894
#include <sys/stat.h>
Packit 032894
Packit 032894
#include ELFUTILS_HEADER(elf)
Packit 032894
#include <gelf.h>
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, const char *argv[])
Packit 032894
{
Packit 032894
  /* Takes the given file, and create a new identical one.  */
Packit 032894
  if (argc != 2)
Packit 032894
    {
Packit 032894
      fprintf (stderr, "elfrdwrnop elf-file\n");
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  const char *name = argv[1];
Packit 032894
  printf ("elfrdwrdnop %s\n", name);
Packit 032894
Packit 032894
  int fd = open (name, O_RDWR);
Packit 032894
  if (fd < 0)
Packit 032894
    {
Packit 032894
      fprintf (stderr, "Couldn't open file '%s': %s\n",
Packit 032894
	       name, strerror (errno));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  Elf *elf = elf_begin (fd, ELF_C_RDWR, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      fprintf (stderr, "Couldn't open ELF file '%s': %s\n",
Packit 032894
	       name, elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Write everything to disk.  If there are any phdrs, then we want
Packit 032894
     the exact same layout.  */
Packit 032894
  size_t phnum;
Packit 032894
  if (elf_getphdrnum (elf, &phnum) != 0)
Packit 032894
    {
Packit 032894
      printf ("cannot get phdrs: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  if (phnum > 0)
Packit 032894
    elf_flagelf (elf, ELF_C_SET, ELF_F_LAYOUT);
Packit 032894
Packit 032894
  if (elf_update (elf, ELF_C_WRITE) < 0)
Packit 032894
    {
Packit 032894
      printf ("failure in elf_update: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  if (elf_end (elf) != 0)
Packit 032894
    {
Packit 032894
      printf ("couldn't cleanup elf '%s': %s\n", name, elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  if (close (fd) != 0)
Packit 032894
    {
Packit 032894
      printf ("couldn't close '%s': %s\n", name, strerror (errno));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  return 0;
Packit 032894
}