Blame tests/update3.c

Packit 032894
/* Test program for elf_update function.
Packit 032894
   Copyright (C) 2000, 2002, 2005, 2016 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2000.
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
#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 <libelf.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
#include ELFUTILS_HEADER(dwelf)
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[] __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  const char *fname = "xxx_update3";
Packit 032894
  int fd;
Packit 032894
  Elf *elf;
Packit 032894
  Elf32_Ehdr *ehdr;
Packit 032894
  Elf32_Phdr *phdr;
Packit 032894
  Elf_Scn *scn;
Packit 032894
  Elf32_Shdr *shdr;
Packit 032894
  Elf_Data *data;
Packit 032894
  Dwelf_Strtab *shst;
Packit 032894
  Dwelf_Strent *shstrtabse;
Packit 032894
  int i;
Packit 032894
Packit 032894
  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      printf ("cannot open `%s': %s\n", fname, strerror (errno));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  elf_fill (0x42);
Packit 032894
Packit 032894
  elf = elf_begin (fd, ELF_C_WRITE, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Create an ELF header.  */
Packit 032894
  ehdr = elf32_newehdr (elf);
Packit 032894
  if (ehdr == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Print the ELF header values.  */
Packit 032894
  if (argc > 1)
Packit 032894
    {
Packit 032894
      for (i = 0; i < EI_NIDENT; ++i)
Packit 032894
	printf (" %02x", ehdr->e_ident[i]);
Packit 032894
      printf ("\
Packit 032894
\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
Packit 032894
	      "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
Packit 032894
	      "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
Packit 032894
	      ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
Packit 032894
	      ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
Packit 032894
	      ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
Packit 032894
	      ehdr->e_shnum, ehdr->e_shstrndx);
Packit 032894
    }
Packit 032894
Packit 032894
  ehdr->e_ident[0] = 42;
Packit 032894
  ehdr->e_ident[4] = 1;
Packit 032894
  ehdr->e_ident[5] = 1;
Packit 032894
  ehdr->e_ident[6] = 2;
Packit 032894
  ehdr->e_type = ET_EXEC;
Packit 032894
  ehdr->e_version = 1;
Packit 032894
  ehdr->e_ehsize = 1;
Packit 032894
  elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
Packit 032894
Packit 032894
  /* Create the program header.  */
Packit 032894
  phdr = elf32_newphdr (elf, 1);
Packit 032894
  if (phdr == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create program header: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  phdr[0].p_type = PT_PHDR;
Packit 032894
  elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
Packit 032894
Packit 032894
  shst = dwelf_strtab_init (true);
Packit 032894
Packit 032894
  scn = elf_newscn (elf);
Packit 032894
  if (scn == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
  shdr = elf32_getshdr (scn);
Packit 032894
  if (shdr == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  shstrtabse = dwelf_strtab_add (shst, ".shstrtab");
Packit 032894
Packit 032894
  shdr->sh_type = SHT_STRTAB;
Packit 032894
  shdr->sh_flags = 0;
Packit 032894
  shdr->sh_addr = 0;
Packit 032894
  shdr->sh_link = SHN_UNDEF;
Packit 032894
  shdr->sh_info = SHN_UNDEF;
Packit 032894
  shdr->sh_addralign = 1;
Packit 032894
  shdr->sh_entsize = 0;
Packit 032894
Packit 032894
  /* We have to store the section index in the ELF header.  */
Packit 032894
  ehdr->e_shstrndx = elf_ndxscn (scn);
Packit 032894
Packit 032894
  data = elf_newdata (scn);
Packit 032894
  if (data == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* No more sections, finalize the section header string table.  */
Packit 032894
  dwelf_strtab_finalize (shst, data);
Packit 032894
Packit 032894
  shdr->sh_name = dwelf_strent_off (shstrtabse);
Packit 032894
Packit 032894
  /* Let the library compute the internal structure information.  */
Packit 032894
  if (elf_update (elf, ELF_C_NULL) < 0)
Packit 032894
    {
Packit 032894
      printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  ehdr = elf32_getehdr (elf);
Packit 032894
Packit 032894
  phdr[0].p_offset = ehdr->e_phoff;
Packit 032894
  phdr[0].p_offset = ehdr->e_phoff;
Packit 032894
  phdr[0].p_vaddr = ehdr->e_phoff;
Packit 032894
  phdr[0].p_paddr = ehdr->e_phoff;
Packit 032894
  phdr[0].p_flags = PF_R | PF_X;
Packit 032894
  phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
Packit 032894
  phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
Packit 032894
  phdr[0].p_align = sizeof (Elf32_Word);
Packit 032894
Packit 032894
  /* Write out the file.  */
Packit 032894
  if (elf_update (elf, ELF_C_WRITE) < 0)
Packit 032894
    {
Packit 032894
      printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* We don't need the string table anymore.  */
Packit 032894
  dwelf_strtab_free (shst);
Packit 032894
Packit 032894
  /* And the data allocated in the .shstrtab section.  */
Packit 032894
  free (data->d_buf);
Packit 032894
Packit 032894
  /* Print the ELF header values.  */
Packit 032894
  if (argc > 1)
Packit 032894
    {
Packit 032894
      for (i = 0; i < EI_NIDENT; ++i)
Packit 032894
	printf (" %02x", ehdr->e_ident[i]);
Packit 032894
      printf ("\
Packit 032894
\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
Packit 032894
	      "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
Packit 032894
	      "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
Packit 032894
	      ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
Packit 032894
	      ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
Packit 032894
	      ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
Packit 032894
	      ehdr->e_shnum, ehdr->e_shstrndx);
Packit 032894
    }
Packit 032894
Packit 032894
  if (elf_end (elf) != 0)
Packit 032894
    {
Packit 032894
      printf ("failure in elf_end: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  unlink (fname);
Packit 032894
Packit 032894
  return 0;
Packit 032894
}