Blame elf/readlib.c

Packit 6c4009
/* Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Andreas Jaeger <aj@suse.de>, 1999 and
Packit 6c4009
		  Jakub Jelinek <jakub@redhat.com>, 1999.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or modify
Packit 6c4009
   it under the terms of the GNU General Public License as published
Packit 6c4009
   by the Free Software Foundation; version 2 of the License, or
Packit 6c4009
   (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* The code in this file and in readelflib is a heavily simplified
Packit 6c4009
   version of the readelf program that's part of the current binutils
Packit 6c4009
   development version.  Besides the simplification, it has also been
Packit 6c4009
   modified to read some other file formats.  */
Packit 6c4009
Packit 6c4009
#include <a.out.h>
Packit 6c4009
#include <elf.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <link.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <gnu/lib-names.h>
Packit 6c4009
Packit 6c4009
#include <ldconfig.h>
Packit 6c4009
Packit 6c4009
#define Elf32_CLASS ELFCLASS32
Packit 6c4009
#define Elf64_CLASS ELFCLASS64
Packit 6c4009
Packit 6c4009
struct known_names
Packit 6c4009
{
Packit 6c4009
  const char *soname;
Packit 6c4009
  int flag;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static struct known_names interpreters[] =
Packit 6c4009
{
Packit 6c4009
  { "/lib/" LD_SO, FLAG_ELF_LIBC6 },
Packit 6c4009
#ifdef SYSDEP_KNOWN_INTERPRETER_NAMES
Packit 6c4009
  SYSDEP_KNOWN_INTERPRETER_NAMES
Packit 6c4009
#endif
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static struct known_names known_libs[] =
Packit 6c4009
{
Packit 6c4009
  { LIBC_SO, FLAG_ELF_LIBC6 },
Packit 6c4009
  { LIBM_SO, FLAG_ELF_LIBC6 },
Packit 6c4009
#ifdef SYSDEP_KNOWN_LIBRARY_NAMES
Packit 6c4009
  SYSDEP_KNOWN_LIBRARY_NAMES
Packit 6c4009
#endif
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Check if string corresponds to a GDB Python file.  */
Packit 6c4009
static bool
Packit 6c4009
is_gdb_python_file (const char *name)
Packit 6c4009
{
Packit 6c4009
  size_t len = strlen (name);
Packit 6c4009
  return len > 7 && strcmp (name + len - 7, "-gdb.py") == 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Returns 0 if everything is ok, != 0 in case of error.  */
Packit 6c4009
int
Packit 6c4009
process_file (const char *real_file_name, const char *file_name,
Packit 6c4009
	      const char *lib, int *flag, unsigned int *osversion,
Packit 6c4009
	      char **soname, int is_link, struct stat64 *stat_buf)
Packit 6c4009
{
Packit 6c4009
  FILE *file;
Packit 6c4009
  struct stat64 statbuf;
Packit 6c4009
  void *file_contents;
Packit 6c4009
  int ret;
Packit 6c4009
  ElfW(Ehdr) *elf_header;
Packit 6c4009
  struct exec *aout_header;
Packit 6c4009
Packit 6c4009
  ret = 0;
Packit 6c4009
  *flag = FLAG_ANY;
Packit 6c4009
  *soname = NULL;
Packit 6c4009
Packit 6c4009
  file = fopen (real_file_name, "rb");
Packit 6c4009
  if (file == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* No error for stale symlink.  */
Packit 6c4009
      if (is_link && strstr (file_name, ".so") != NULL)
Packit 6c4009
	return 1;
Packit 6c4009
      error (0, 0, _("Input file %s not found.\n"), file_name);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fstat64 (fileno (file), &statbuf) < 0)
Packit 6c4009
    {
Packit 6c4009
      error (0, 0, _("Cannot fstat file %s.\n"), file_name);
Packit 6c4009
      fclose (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Check that the file is large enough so that we can access the
Packit 6c4009
     information.  We're only checking the size of the headers here.  */
Packit 6c4009
  if ((size_t) statbuf.st_size < sizeof (struct exec)
Packit 6c4009
      || (size_t) statbuf.st_size < sizeof (ElfW(Ehdr)))
Packit 6c4009
    {
Packit 6c4009
      if (statbuf.st_size == 0)
Packit 6c4009
	error (0, 0, _("File %s is empty, not checked."), file_name);
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  char buf[SELFMAG];
Packit 6c4009
	  size_t n = MIN (statbuf.st_size, SELFMAG);
Packit 6c4009
	  if (fread (buf, n, 1, file) == 1 && memcmp (buf, ELFMAG, n) == 0)
Packit 6c4009
	    error (0, 0, _("File %s is too small, not checked."), file_name);
Packit 6c4009
	}
Packit 6c4009
      fclose (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  file_contents = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED,
Packit 6c4009
			fileno (file), 0);
Packit 6c4009
  if (file_contents == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      error (0, 0, _("Cannot mmap file %s.\n"), file_name);
Packit 6c4009
      fclose (file);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* First check if this is an aout file.  */
Packit 6c4009
  aout_header = (struct exec *) file_contents;
Packit 6c4009
  if (N_MAGIC (*aout_header) == ZMAGIC
Packit 6c4009
#ifdef QMAGIC			/* Linuxism.  */
Packit 6c4009
      || N_MAGIC (*aout_header) == QMAGIC
Packit 6c4009
#endif
Packit 6c4009
      )
Packit 6c4009
    {
Packit 6c4009
      /* Aout files don't have a soname, just return the name
Packit 6c4009
	 including the major number.  */
Packit 6c4009
      char *copy, *major, *dot;
Packit 6c4009
      copy = xstrdup (lib);
Packit 6c4009
      major = strstr (copy, ".so.");
Packit 6c4009
      if (major)
Packit 6c4009
	{
Packit 6c4009
	  dot = strstr (major + 4, ".");
Packit 6c4009
	  if (dot)
Packit 6c4009
	    *dot = '\0';
Packit 6c4009
	}
Packit 6c4009
      *soname = copy;
Packit 6c4009
      *flag = FLAG_LIBC4;
Packit 6c4009
      goto done;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  elf_header = (ElfW(Ehdr) *) file_contents;
Packit 6c4009
  if (memcmp (elf_header->e_ident, ELFMAG, SELFMAG) != 0)
Packit 6c4009
    {
Packit 6c4009
      /* The file is neither ELF nor aout.  Check if it's a linker
Packit 6c4009
	 script, like libc.so - otherwise complain.  Only search the
Packit 6c4009
	 beginning of the file.  */
Packit 6c4009
      size_t len = MIN (statbuf.st_size, 512);
Packit 6c4009
      if (memmem (file_contents, len, "GROUP", 5) == NULL
Packit 6c4009
	  && memmem (file_contents, len, "GNU ld script", 13) == NULL
Packit 6c4009
	  && !is_gdb_python_file (file_name))
Packit 6c4009
	error (0, 0, _("%s is not an ELF file - it has the wrong magic bytes at the start.\n"),
Packit 6c4009
	       file_name);
Packit 6c4009
      ret = 1;
Packit 6c4009
    }
Packit 6c4009
  /* Libraries have to be shared object files.  */
Packit 6c4009
  else if (elf_header->e_type != ET_DYN)
Packit 6c4009
    ret = 1;
Packit 6c4009
  else if (process_elf_file (file_name, lib, flag, osversion, soname,
Packit 6c4009
			     file_contents, statbuf.st_size))
Packit 6c4009
    ret = 1;
Packit 6c4009
Packit 6c4009
 done:
Packit 6c4009
  /* Clean up allocated memory and resources.  */
Packit 6c4009
  munmap (file_contents, statbuf.st_size);
Packit 6c4009
  fclose (file);
Packit 6c4009
Packit 6c4009
  *stat_buf = statbuf;
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Returns made up soname if lib doesn't have explicit DT_SONAME.  */
Packit 6c4009
Packit 6c4009
char *
Packit 6c4009
implicit_soname (const char *lib, int flag)
Packit 6c4009
{
Packit 6c4009
  char *soname = xstrdup (lib);
Packit 6c4009
Packit 6c4009
  if ((flag & FLAG_TYPE_MASK) != FLAG_LIBC4)
Packit 6c4009
    return soname;
Packit 6c4009
Packit 6c4009
  /* Aout files don't have a soname, just return the name
Packit 6c4009
     including the major number.  */
Packit 6c4009
  char *major = strstr (soname, ".so.");
Packit 6c4009
  if (major)
Packit 6c4009
    {
Packit 6c4009
      char *dot = strstr (major + 4, ".");
Packit 6c4009
      if (dot)
Packit 6c4009
	*dot = '\0';
Packit 6c4009
    }
Packit 6c4009
  return soname;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Get architecture specific version of process_elf_file.  */
Packit 6c4009
#include <readelflib.c>