Blame libelf/elf_getshdrstrndx.c

Packit 032894
/* Return section index of section header string table.
Packit 032894
   Copyright (C) 2002, 2005, 2009, 2014, 2015 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
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 GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   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 <assert.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <gelf.h>
Packit 032894
#include <stddef.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
#include <system.h>
Packit 032894
#include "libelfP.h"
Packit 032894
#include "common.h"
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
elf_getshdrstrndx (Elf *elf, size_t *dst)
Packit 032894
{
Packit 032894
  int result = 0;
Packit 032894
Packit 032894
  if (elf == NULL)
Packit 032894
    return -1;
Packit 032894
Packit 032894
  if (unlikely (elf->kind != ELF_K_ELF))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_INVALID_HANDLE);
Packit 032894
      return -1;
Packit 032894
    }
Packit 032894
Packit 032894
  rwlock_rdlock (elf->lock);
Packit 032894
Packit 032894
  /* We rely here on the fact that the `elf' element is a common prefix
Packit 032894
     of `elf32' and `elf64'.  */
Packit 032894
  assert (offsetof (struct Elf, state.elf.ehdr)
Packit 032894
	  == offsetof (struct Elf, state.elf32.ehdr));
Packit 032894
  assert (sizeof (elf->state.elf.ehdr)
Packit 032894
	  == sizeof (elf->state.elf32.ehdr));
Packit 032894
  assert (offsetof (struct Elf, state.elf.ehdr)
Packit 032894
	  == offsetof (struct Elf, state.elf64.ehdr));
Packit 032894
  assert (sizeof (elf->state.elf.ehdr)
Packit 032894
	  == sizeof (elf->state.elf64.ehdr));
Packit 032894
Packit 032894
  if (unlikely (elf->state.elf.ehdr == NULL))
Packit 032894
    {
Packit 032894
      __libelf_seterrno (ELF_E_WRONG_ORDER_EHDR);
Packit 032894
      result = -1;
Packit 032894
    }
Packit 032894
  else
Packit 032894
    {
Packit 032894
      Elf32_Word num;
Packit 032894
Packit 032894
      num = (elf->class == ELFCLASS32
Packit 032894
	     ? elf->state.elf32.ehdr->e_shstrndx
Packit 032894
	     : elf->state.elf64.ehdr->e_shstrndx);
Packit 032894
Packit 032894
      /* Determine whether the index is too big to fit in the ELF
Packit 032894
	 header.  */
Packit 032894
      if (unlikely (num == SHN_XINDEX))
Packit 032894
	{
Packit 032894
	  /* Yes.  Search the zeroth section header.  */
Packit 032894
	  if (elf->class == ELFCLASS32)
Packit 032894
	    {
Packit 032894
	      size_t offset;
Packit 032894
	      if (unlikely (elf->state.elf32.scns.cnt == 0))
Packit 032894
		{
Packit 032894
		  /* Cannot use SHN_XINDEX without section headers.  */
Packit 032894
		  __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
Packit 032894
		  result = -1;
Packit 032894
		  goto out;
Packit 032894
		}
Packit 032894
Packit 032894
	      if (elf->state.elf32.scns.data[0].shdr.e32 != NULL)
Packit 032894
		{
Packit 032894
		  num = elf->state.elf32.scns.data[0].shdr.e32->sh_link;
Packit 032894
		  goto success;
Packit 032894
		}
Packit 032894
Packit 032894
	      offset = elf->state.elf32.ehdr->e_shoff;
Packit 032894
Packit 032894
	      if (elf->map_address != NULL
Packit 032894
		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
Packit 032894
		  && (ALLOW_UNALIGNED
Packit 032894
		      || (((size_t) ((char *) elf->map_address
Packit 032894
			   + elf->start_offset + offset))
Packit 032894
			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
Packit 032894
		{
Packit 032894
		  /* First see whether the information in the ELF header is
Packit 032894
		     valid and it does not ask for too much.  */
Packit 032894
		  if (unlikely (elf->maximum_size - offset
Packit 032894
				< sizeof (Elf32_Shdr)))
Packit 032894
		    {
Packit 032894
		      /* Something is wrong.  */
Packit 032894
		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
Packit 032894
		      result = -1;
Packit 032894
		      goto out;
Packit 032894
		    }
Packit 032894
Packit 032894
		  /* We can directly access the memory.  */
Packit 032894
		  num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
Packit 032894
					 + offset))->sh_link;
Packit 032894
		}
Packit 032894
	      else
Packit 032894
		{
Packit 032894
		  /* We avoid reading in all the section headers.  Just read
Packit 032894
		     the first one.  */
Packit 032894
		  Elf32_Shdr shdr_mem;
Packit 032894
		  ssize_t r;
Packit 032894
Packit 032894
		  if (unlikely ((r = pread_retry (elf->fildes, &shdr_mem,
Packit 032894
						  sizeof (Elf32_Shdr), offset))
Packit 032894
				!= sizeof (Elf32_Shdr)))
Packit 032894
		    {
Packit 032894
		      /* We must be able to read this ELF section header.  */
Packit 032894
		      if (r < 0)
Packit 032894
			__libelf_seterrno (ELF_E_INVALID_FILE);
Packit 032894
		      else
Packit 032894
			__libelf_seterrno (ELF_E_INVALID_ELF);
Packit 032894
		      result = -1;
Packit 032894
		      goto out;
Packit 032894
		    }
Packit 032894
Packit 032894
		  if (elf->state.elf32.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
Packit 032894
		    CONVERT (shdr_mem.sh_link);
Packit 032894
		  num = shdr_mem.sh_link;
Packit 032894
		}
Packit 032894
	    }
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      if (unlikely (elf->state.elf64.scns.cnt == 0))
Packit 032894
		{
Packit 032894
		  /* Cannot use SHN_XINDEX without section headers.  */
Packit 032894
		  __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
Packit 032894
		  result = -1;
Packit 032894
		  goto out;
Packit 032894
		}
Packit 032894
Packit 032894
	      if (elf->state.elf64.scns.data[0].shdr.e64 != NULL)
Packit 032894
		{
Packit 032894
		  num = elf->state.elf64.scns.data[0].shdr.e64->sh_link;
Packit 032894
		  goto success;
Packit 032894
		}
Packit 032894
Packit 032894
	      size_t offset = elf->state.elf64.ehdr->e_shoff;
Packit 032894
Packit 032894
	      if (elf->map_address != NULL
Packit 032894
		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
Packit 032894
		  && (ALLOW_UNALIGNED
Packit 032894
		      || (((size_t) ((char *) elf->map_address
Packit 032894
			   + elf->start_offset + offset))
Packit 032894
			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
Packit 032894
		{
Packit 032894
		  /* First see whether the information in the ELF header is
Packit 032894
		     valid and it does not ask for too much.  */
Packit 032894
		  if (unlikely (elf->maximum_size - offset
Packit 032894
				< sizeof (Elf64_Shdr)))
Packit 032894
		    {
Packit 032894
		      /* Something is wrong.  */
Packit 032894
		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
Packit 032894
		      result = -1;
Packit 032894
		      goto out;
Packit 032894
		    }
Packit 032894
Packit 032894
		  /* We can directly access the memory.  */
Packit 032894
		  num = ((Elf64_Shdr *) (elf->map_address + elf->start_offset
Packit 032894
					 + offset))->sh_link;
Packit 032894
		}
Packit 032894
	      else
Packit 032894
		{
Packit 032894
		  /* We avoid reading in all the section headers.  Just read
Packit 032894
		     the first one.  */
Packit 032894
		  Elf64_Shdr shdr_mem;
Packit 032894
		  ssize_t r;
Packit 032894
Packit 032894
		  if (unlikely ((r = pread_retry (elf->fildes, &shdr_mem,
Packit 032894
						  sizeof (Elf64_Shdr), offset))
Packit 032894
				!= sizeof (Elf64_Shdr)))
Packit 032894
		    {
Packit 032894
		      /* We must be able to read this ELF section header.  */
Packit 032894
		      if (r < 0)
Packit 032894
			__libelf_seterrno (ELF_E_INVALID_FILE);
Packit 032894
		      else
Packit 032894
			__libelf_seterrno (ELF_E_INVALID_ELF);
Packit 032894
		      result = -1;
Packit 032894
		      goto out;
Packit 032894
		    }
Packit 032894
Packit 032894
		  if (elf->state.elf64.ehdr->e_ident[EI_DATA] != MY_ELFDATA)
Packit 032894
		    CONVERT (shdr_mem.sh_link);
Packit 032894
		  num = shdr_mem.sh_link;
Packit 032894
		}
Packit 032894
	    }
Packit 032894
	}
Packit 032894
Packit 032894
      /* Store the result.  */
Packit 032894
    success:
Packit 032894
      *dst = num;
Packit 032894
    }
Packit 032894
Packit 032894
 out:
Packit 032894
  rwlock_unlock (elf->lock);
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
INTDEF(elf_getshdrstrndx)
Packit 032894
/* Alias for the deprecated name.  */
Packit 032894
strong_alias (elf_getshdrstrndx, elf_getshstrndx)