Blame dlfcn/tststatic3.c

Packit 6c4009
/* Global-scope DSO mapping test with a static executable (BZ #15022).
Packit 6c4009
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library 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 GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
Packit 6c4009
#define MAGIC0 0
Packit 6c4009
#define MAGIC1 0x5500ffaa
Packit 6c4009
#define MAGIC2 0xaaff0055
Packit 6c4009
Packit 6c4009
/* Mapping a DSO into the global scope used to crash in static
Packit 6c4009
   executables.  Check that it succeeds and then that symbols from
Packit 6c4009
   the DSO can be accessed and operate as expected.  */
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  unsigned int (*getfoo) (void);
Packit 6c4009
  void (*setfoo) (unsigned int);
Packit 6c4009
  unsigned int *foop;
Packit 6c4009
  unsigned int foo;
Packit 6c4009
  void *handle;
Packit 6c4009
Packit 6c4009
  /* Try to map a module into the global scope.  */
Packit 6c4009
  handle = dlopen ("modstatic3.so", RTLD_LAZY | RTLD_GLOBAL);
Packit 6c4009
  if (handle == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("dlopen (modstatic3.so): %s\n", dlerror ());
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Get at its symbols.  */
Packit 6c4009
  foop = dlsym (handle, "foo");
Packit 6c4009
  if (foop == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("dlsym (foo): %s\n", dlerror ());
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  getfoo = dlsym (handle, "getfoo");
Packit 6c4009
  if (getfoo == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("dlsym (getfoo): %s\n", dlerror ());
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  setfoo = dlsym (handle, "setfoo");
Packit 6c4009
  if (setfoo == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("dlsym (setfoo): %s\n", dlerror ());
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Make sure the view of the initial state is consistent.  */
Packit 6c4009
  foo = *foop;
Packit 6c4009
  if (foo != MAGIC0)
Packit 6c4009
    {
Packit 6c4009
      printf ("*foop: got %#x, expected %#x\n", foo, MAGIC0);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  foo = getfoo ();
Packit 6c4009
  if (foo != MAGIC0)
Packit 6c4009
    {
Packit 6c4009
      printf ("getfoo: got %#x, expected %#x\n", foo, MAGIC0);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Likewise with one change to its state.  */
Packit 6c4009
  setfoo (MAGIC1);
Packit 6c4009
Packit 6c4009
  foo = *foop;
Packit 6c4009
  if (foo != MAGIC1)
Packit 6c4009
    {
Packit 6c4009
      printf ("*foop: got %#x, expected %#x\n", foo, MAGIC1);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  foo = getfoo ();
Packit 6c4009
  if (foo != MAGIC1)
Packit 6c4009
    {
Packit 6c4009
      printf ("getfoo: got %#x, expected %#x\n", foo, MAGIC1);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* And with another.  */
Packit 6c4009
  setfoo (MAGIC2);
Packit 6c4009
Packit 6c4009
  foo = *foop;
Packit 6c4009
  if (foo != MAGIC2)
Packit 6c4009
    {
Packit 6c4009
      printf ("*foop: got %#x, expected %#x\n", foo, MAGIC2);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  foo = getfoo ();
Packit 6c4009
  if (foo != MAGIC2)
Packit 6c4009
    {
Packit 6c4009
      printf ("getfoo: got %#x, expected %#x\n", foo, MAGIC2);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* All done, clean up.  */
Packit 6c4009
  getfoo = NULL;
Packit 6c4009
  setfoo = NULL;
Packit 6c4009
  foop = NULL;
Packit 6c4009
  dlclose (handle);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"