Blame tests/zstrptr.c

Packit 032894
/* Test program for elf_strptr function.
Packit 032894
   Copyright (C) 2015 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
#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 <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
#include ELFUTILS_HEADER(elf)
Packit 032894
#include <gelf.h>
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  if (argc != 2)
Packit 032894
    {
Packit 032894
      printf ("No ELF file given as argument");
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  const char *fname = argv[1];
Packit 032894
Packit 032894
  // Initialize libelf.
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  /* Read the ELF from disk now.  */
Packit 032894
  int fd = open (fname, O_RDONLY);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create ELF descriptor read-only: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  size_t ndx;
Packit 032894
  if (elf_getshdrstrndx (elf, &ndx) != 0)
Packit 032894
    {
Packit 032894
      printf ("cannot get section header table index: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  if (ndx == SHN_UNDEF)
Packit 032894
    {
Packit 032894
      printf ("ELF file `%s' doesn't have a section header table index", fname);
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  Elf_Scn *scn = elf_getscn (elf, ndx);
Packit 032894
  if (scn == NULL)
Packit 032894
    {
Packit 032894
      printf ("Couldn't get section %zd: %s\n", ndx, elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  void print_strings (void)
Packit 032894
  {
Packit 032894
    GElf_Shdr shdr_mem;
Packit 032894
    GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
Packit 032894
Packit 032894
    printf ("Strings in section %zd (%s):\n", ndx,
Packit 032894
	    ((shdr->sh_flags & SHF_COMPRESSED) != 0
Packit 032894
	     ? "compressed" : "uncompressed"));
Packit 032894
Packit 032894
    size_t off = 0;
Packit 032894
    const char *str = elf_strptr (elf, ndx, off);
Packit 032894
    while (str != NULL)
Packit 032894
      {
Packit 032894
	printf ("[%zx] '%s'\n", off, str);
Packit 032894
	off += strlen (str) + 1;
Packit 032894
	str = elf_strptr (elf, ndx, off);
Packit 032894
      }
Packit 032894
  }
Packit 032894
Packit 032894
  if (elf_compress (scn, ELFCOMPRESS_ZLIB, 0) < 0)
Packit 032894
    {
Packit 032894
      printf ("Couldn't compress section %zd: %s\n", ndx, elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
  print_strings ();
Packit 032894
Packit 032894
  if (elf_compress (scn, 0, 0) < 0)
Packit 032894
    {
Packit 032894
      printf ("Couldn't decompress section %zd: %s\n", ndx, elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
  print_strings ();
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
  close (fd);
Packit 032894
Packit 032894
  return 0;
Packit 032894
}