Blame libelf/common.h

Packit 032894
/* Common definitions for handling files in memory or only on disk.
Packit 032894
   Copyright (C) 1998, 1999, 2000, 2002, 2005, 2008 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
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
#ifndef _COMMON_H
Packit 032894
#define _COMMON_H       1
Packit 032894
Packit 032894
#include <ar.h>
Packit 032894
#include <byteswap.h>
Packit 032894
#include <endian.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
Packit 032894
#include "libelfP.h"
Packit 032894
Packit 032894
static inline Elf_Kind
Packit 032894
__attribute__ ((unused))
Packit 032894
determine_kind (void *buf, size_t len)
Packit 032894
{
Packit 032894
  /* First test for an archive.  */
Packit 032894
  if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
Packit 032894
    return ELF_K_AR;
Packit 032894
Packit 032894
  /* Next try ELF files.  */
Packit 032894
  if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
Packit 032894
    {
Packit 032894
      /* Could be an ELF file.  */
Packit 032894
      int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
Packit 032894
      int data = (int) ((unsigned char *) buf)[EI_DATA];
Packit 032894
      int version = (int) ((unsigned char *) buf)[EI_VERSION];
Packit 032894
Packit 032894
      if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
Packit 032894
	  && data > ELFDATANONE && data < ELFDATANUM
Packit 032894
	  && version == EV_CURRENT)
Packit 032894
	return ELF_K_ELF;
Packit 032894
    }
Packit 032894
Packit 032894
  /* We do not know this file type.  */
Packit 032894
  return ELF_K_NONE;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
/* Allocate an Elf descriptor and fill in the generic information.  */
Packit 032894
static inline Elf *
Packit 032894
__attribute__ ((unused))
Packit 032894
allocate_elf (int fildes, void *map_address, int64_t offset, size_t maxsize,
Packit 032894
              Elf_Cmd cmd, Elf *parent, Elf_Kind kind, size_t extra)
Packit 032894
{
Packit 032894
  Elf *result = (Elf *) calloc (1, sizeof (Elf) + extra);
Packit 032894
  if (result == NULL)
Packit 032894
    __libelf_seterrno (ELF_E_NOMEM);
Packit 032894
  else
Packit 032894
    {
Packit 032894
      result->kind = kind;
Packit 032894
      result->ref_count = 1;
Packit 032894
      result->cmd = cmd;
Packit 032894
      result->fildes = fildes;
Packit 032894
      result->start_offset = offset;
Packit 032894
      result->maximum_size = maxsize;
Packit 032894
      result->map_address = map_address;
Packit 032894
      result->parent = parent;
Packit 032894
Packit 032894
      rwlock_init (result->lock);
Packit 032894
    }
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
/* Acquire lock for the descriptor and all children.  */
Packit 032894
static void
Packit 032894
__attribute__ ((unused))
Packit 032894
libelf_acquire_all (Elf *elf)
Packit 032894
{
Packit 032894
  rwlock_wrlock (elf->lock);
Packit 032894
Packit 032894
  if (elf->kind == ELF_K_AR)
Packit 032894
    {
Packit 032894
      Elf *child = elf->state.ar.children;
Packit 032894
Packit 032894
      while (child != NULL)
Packit 032894
	{
Packit 032894
	  if (child->ref_count != 0)
Packit 032894
	    libelf_acquire_all (child);
Packit 032894
	  child = child->next;
Packit 032894
	}
Packit 032894
    }
Packit 032894
}
Packit 032894
Packit 032894
/* Release own lock and those of the children.  */
Packit 032894
static void
Packit 032894
__attribute__ ((unused))
Packit 032894
libelf_release_all (Elf *elf)
Packit 032894
{
Packit 032894
  if (elf->kind == ELF_K_AR)
Packit 032894
    {
Packit 032894
      Elf *child = elf->state.ar.children;
Packit 032894
Packit 032894
      while (child != NULL)
Packit 032894
	{
Packit 032894
	  if (child->ref_count != 0)
Packit 032894
	    libelf_release_all (child);
Packit 032894
	  child = child->next;
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  rwlock_unlock (elf->lock);
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
/* Macro to convert endianess in place.  It determines the function it
Packit 032894
   has to use itself.  */
Packit 032894
#define CONVERT(Var) \
Packit 032894
  (Var) = (sizeof (Var) == 1						      \
Packit 032894
	   ? (unsigned char) (Var)					      \
Packit 032894
	   : (sizeof (Var) == 2						      \
Packit 032894
	      ? bswap_16 (Var)						      \
Packit 032894
	      : (sizeof (Var) == 4					      \
Packit 032894
		 ? bswap_32 (Var)					      \
Packit 032894
		 : bswap_64 (Var))))
Packit 032894
Packit 032894
#define CONVERT_TO(Dst, Var) \
Packit 032894
  (Dst) = (sizeof (Var) == 1						      \
Packit 032894
	   ? (unsigned char) (Var)					      \
Packit 032894
	   : (sizeof (Var) == 2						      \
Packit 032894
	      ? bswap_16 (Var)						      \
Packit 032894
	      : (sizeof (Var) == 4					      \
Packit 032894
		 ? bswap_32 (Var)					      \
Packit 032894
		 : bswap_64 (Var))))
Packit 032894
Packit 032894
Packit 032894
#if __BYTE_ORDER == __LITTLE_ENDIAN
Packit 032894
# define MY_ELFDATA	ELFDATA2LSB
Packit 032894
#else
Packit 032894
# define MY_ELFDATA	ELFDATA2MSB
Packit 032894
#endif
Packit 032894
Packit 032894
#endif	/* common.h */