Blame tests/xlate_notes.c

Packit 032894
/* Test program for extracting ELF Note headers and getting whole notes.
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
#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\n");
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': %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: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  GElf_Ehdr ehdr;
Packit 032894
  if (gelf_getehdr (elf, &ehdr) == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot get Ehdr: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Search for all SHT_NOTE sections.  */
Packit 032894
  Elf_Scn *scn = NULL;
Packit 032894
  while ((scn = elf_nextscn (elf, scn)) != NULL)
Packit 032894
    {
Packit 032894
      /* Get the header.  */
Packit 032894
      GElf_Shdr shdr;
Packit 032894
      if (gelf_getshdr (scn, &shdr) == NULL)
Packit 032894
	{
Packit 032894
	  printf ("couldn't get shdr: %s\n", elf_errmsg (-1));
Packit 032894
	  exit (1);
Packit 032894
	}
Packit 032894
Packit 032894
      if (shdr.sh_type == SHT_NOTE)
Packit 032894
	{
Packit 032894
	  printf ("Notes in section %zd:\n", elf_ndxscn (scn));
Packit 032894
Packit 032894
	  Elf_Data *raw = elf_rawdata (scn, NULL);
Packit 032894
	  if (raw == NULL)
Packit 032894
	    {
Packit 032894
	      printf ("couldn't get raw data: %s\n", elf_errmsg (-1));
Packit 032894
	      exit (1);
Packit 032894
	    }
Packit 032894
Packit 032894
	  Elf_Data *data = elf_getdata (scn, NULL);
Packit 032894
	  if (data == NULL)
Packit 032894
	    {
Packit 032894
	      printf ("couldn't get data: %s\n", elf_errmsg (-1));
Packit 032894
	      exit (1);
Packit 032894
	    }
Packit 032894
Packit 032894
	  size_t off = 0;
Packit 032894
	  size_t next;
Packit 032894
	  GElf_Nhdr nhdr;
Packit 032894
	  size_t n_off;
Packit 032894
	  size_t d_off;
Packit 032894
	  while ((next = gelf_getnote (data, off, &nhdr, &n_off, &d_off)) > 0)
Packit 032894
	    {
Packit 032894
	      /* Now just get the note header "raw" (don't
Packit 032894
		 copy/translate the note data). This only handles
Packit 032894
		 traditional GNU ELF Notes, so we still use the next
Packit 032894
		 from gelf_getnote (padding is different for new style
Packit 032894
		 ELF_T_NHDR8 notes).  */
Packit 032894
	      Elf32_Nhdr nh;
Packit 032894
	      Elf_Data src =
Packit 032894
                {
Packit 032894
                  .d_version = EV_CURRENT, .d_type = ELF_T_NHDR,
Packit 032894
		  .d_size = sizeof nh
Packit 032894
                };
Packit 032894
	      Elf_Data dst = src;
Packit 032894
	      src.d_buf = raw->d_buf + off;
Packit 032894
	      dst.d_buf = &nh;
Packit 032894
Packit 032894
	      if (elf32_xlatetom (&dst, &src, ehdr.e_ident[EI_DATA]) == NULL)
Packit 032894
		{
Packit 032894
		  printf ("couldn't xlate note: %s\n", elf_errmsg (-1));
Packit 032894
		  exit (1);
Packit 032894
		}
Packit 032894
Packit 032894
	      printf ("type: %" PRId32 ",%" PRId32
Packit 032894
		      ", namesz: %" PRId32 ",%" PRId32
Packit 032894
		      ", descsz: %" PRId32 ",%" PRId32 "\n",
Packit 032894
		      nhdr.n_type, nh.n_type,
Packit 032894
		      nhdr.n_namesz, nh.n_namesz,
Packit 032894
		      nhdr.n_descsz, nh.n_descsz);
Packit 032894
Packit 032894
	      if (nhdr.n_type != nh.n_type
Packit 032894
		  || nhdr.n_namesz != nh.n_namesz
Packit 032894
		  || nhdr.n_descsz != nh.n_descsz)
Packit 032894
		{
Packit 032894
		  printf ("Nhdrs not equal!\n");
Packit 032894
		  exit (1);
Packit 032894
		}
Packit 032894
Packit 032894
	      off = next;
Packit 032894
	    }
Packit 032894
	}
Packit 032894
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
  close (fd);
Packit 032894
Packit 032894
  return 0;
Packit 032894
}