Blame tests/alldts.c

Packit 032894
/* Create an ELF file with all the DT_* flags set.
Packit 032894
   Copyright (C) 2011, 2016 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Marek Polacek <mpolacek@redhat.com>, 2011.
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 ELFUTILS_HEADER(dwelf)
Packit 032894
#include <elf.h>
Packit 032894
#include <gelf.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include <libelf.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdio_ext.h>
Packit 032894
#include <stdint.h>
Packit 032894
#include <stdbool.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (void)
Packit 032894
{
Packit 032894
  static const char fname[] = "testfile-alldts";
Packit 032894
  Dwelf_Strtab *shst;
Packit 032894
  Dwelf_Strent *dynscn;
Packit 032894
  Dwelf_Strent *shstrtabse;
Packit 032894
  const Elf32_Sword dtflags[] =
Packit 032894
    {
Packit 032894
      DT_NULL, DT_NEEDED, DT_PLTRELSZ, DT_PLTGOT,
Packit 032894
      DT_HASH, DT_STRTAB, DT_SYMTAB, DT_RELA,
Packit 032894
      DT_RELASZ, DT_RELAENT, DT_STRSZ, DT_SYMENT,
Packit 032894
      DT_INIT, DT_FINI, DT_SONAME, DT_RPATH,
Packit 032894
      DT_SYMBOLIC, DT_REL, DT_RELSZ, DT_RELENT,
Packit 032894
      DT_PLTREL, DT_DEBUG, DT_TEXTREL, DT_JMPREL,
Packit 032894
      DT_BIND_NOW, DT_INIT_ARRAY, DT_FINI_ARRAY,
Packit 032894
      DT_INIT_ARRAYSZ, DT_FINI_ARRAYSZ, DT_RUNPATH,
Packit 032894
      DT_FLAGS, DT_ENCODING, DT_PREINIT_ARRAY,
Packit 032894
      DT_PREINIT_ARRAYSZ, DT_VERSYM, DT_GNU_PRELINKED,
Packit 032894
      DT_GNU_CONFLICTSZ, DT_GNU_LIBLISTSZ, DT_CHECKSUM,
Packit 032894
      DT_PLTPADSZ, DT_MOVEENT, DT_MOVESZ, DT_FEATURE_1,
Packit 032894
      DT_POSFLAG_1, DT_SYMINSZ, DT_SYMINENT, DT_GNU_HASH,
Packit 032894
      DT_TLSDESC_PLT, DT_TLSDESC_GOT, DT_GNU_CONFLICT,
Packit 032894
      DT_GNU_LIBLIST, DT_CONFIG, DT_DEPAUDIT, DT_AUDIT,
Packit 032894
      DT_PLTPAD, DT_MOVETAB, DT_SYMINFO, DT_RELACOUNT,
Packit 032894
      DT_RELCOUNT, DT_FLAGS_1, DT_VERDEF, DT_VERDEFNUM,
Packit 032894
      DT_VERNEED, DT_VERNEEDNUM, DT_AUXILIARY, DT_FILTER
Packit 032894
    };
Packit 032894
  const int ndtflags = sizeof (dtflags) / sizeof (dtflags[0]);
Packit 032894
Packit 032894
  /* We use no threads here which can interfere with handling a stream.  */
Packit 032894
  (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
Packit 032894
Packit 032894
  /* Open the file.  */
Packit 032894
  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      printf ("cannot open `%s': %m\n", fname);
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Tell the library which version are we expecting.  */
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  /* Create an ELF descriptor.  */
Packit 032894
  Elf *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
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Create an ELF header.  */
Packit 032894
  Elf32_Ehdr *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
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  ehdr->e_ident[0] = 42;
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_machine = EM_386;
Packit 032894
  ehdr->e_version = 1;
Packit 032894
  ehdr->e_ehsize = 1;
Packit 032894
  ehdr->e_shnum = 3;
Packit 032894
Packit 032894
  elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
Packit 032894
Packit 032894
  /* Create the program headers.  */
Packit 032894
  Elf32_Phdr *phdr = elf32_newphdr (elf, 2);
Packit 032894
  if (phdr == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create program headers: %s\n", elf_errmsg (-1));
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  phdr[0].p_type = PT_PHDR;
Packit 032894
  phdr[1].p_type = PT_DYNAMIC;
Packit 032894
Packit 032894
  elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
Packit 032894
  shst = dwelf_strtab_init (true);
Packit 032894
Packit 032894
  /* Create the .dynamic section.  */
Packit 032894
  Elf_Scn *scn = elf_newscn (elf);
Packit 032894
  if (scn == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create DYNAMIC section: %s\n", elf_errmsg (-1));
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  Elf32_Shdr *shdr = elf32_getshdr (scn);
Packit 032894
  if (shdr == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot get header for DYNAMIC section: %s\n", elf_errmsg (-1));
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  dynscn = dwelf_strtab_add (shst, ".dynamic");
Packit 032894
Packit 032894
  /* We'll need to know the section offset.  But this will be set up
Packit 032894
     by elf_update later, so for now just store the address.  */
Packit 032894
  const Elf32_Off *const dynscn_offset = &shdr->sh_offset;
Packit 032894
  shdr->sh_type = SHT_DYNAMIC;
Packit 032894
  shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
Packit 032894
  shdr->sh_link = SHN_UNDEF;
Packit 032894
  shdr->sh_info = SHN_UNDEF;
Packit 032894
  /* This section will start here.  */
Packit 032894
  shdr->sh_addr = 0x1a0;
Packit 032894
Packit 032894
  /* Create new section data.  */
Packit 032894
  Elf_Data *data = elf_newdata (scn);
Packit 032894
  if (data == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create data for DYNAMIC section: %s\n", elf_errmsg (-1));
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Allocate memory for all the .dynamic entries.  */
Packit 032894
  Elf32_Dyn *dyn = malloc (ndtflags * sizeof (Elf32_Dyn));
Packit 032894
  if (dyn == NULL)
Packit 032894
    {
Packit 032894
      printf ("malloc failed: %m\n");
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Now write all the DT_* flags.  */
Packit 032894
  for (int i = 0; i < ndtflags; ++i)
Packit 032894
    {
Packit 032894
      dyn[i].d_tag = dtflags[i];
Packit 032894
      dyn[i].d_un.d_val = 0xdeadbeef;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Set the pointer to allocated memory.  */
Packit 032894
  data->d_buf = dyn;
Packit 032894
  data->d_type = ELF_T_DYN;
Packit 032894
  data->d_version = EV_CURRENT;
Packit 032894
  data->d_size = ndtflags * sizeof (Elf32_Dyn);
Packit 032894
  data->d_align = 0x8;
Packit 032894
Packit 032894
  /* Create .shstrtab section.  */
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
      return 1;
Packit 032894
    }
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
      return 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_entsize = 1;
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
      return 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
  elf32_getshdr (elf_getscn (elf, 1))->sh_name = dwelf_strent_off (dynscn);
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
      return 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_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
  phdr[1].p_flags = PF_W | PF_R;
Packit 032894
  phdr[1].p_offset = *dynscn_offset;
Packit 032894
  /* Set up the start of this segment to equal start address of the
Packit 032894
     .dynamic section.  */
Packit 032894
  phdr[1].p_vaddr = 0x1a0;
Packit 032894
  phdr[1].p_paddr = 0x1a0;
Packit 032894
  phdr[1].p_align = 2 * sizeof (Elf32_Word);
Packit 032894
  phdr[1].p_filesz = ndtflags * sizeof (Elf32_Dyn);
Packit 032894
  phdr[1].p_memsz = ndtflags * sizeof (Elf32_Dyn);
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
      return 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
  /* And the dynamic entries.  */
Packit 032894
  free (dyn);
Packit 032894
Packit 032894
  /* All done.  */
Packit 032894
  if (elf_end (elf) != 0)
Packit 032894
    {
Packit 032894
      printf ("failure in elf_end: %s\n", elf_errmsg (-1));
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  return 0;
Packit 032894
}