Blame dlfcn/bug-dl-leaf-lib.c

Packit 6c4009
/* Make sure dlopen/dlclose are not marked as leaf functions.
Packit 6c4009
Packit 6c4009
   Copyright (C) 2013-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Mike Frysinger <vapier@gentoo.org>
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
/* The bug-dl-leaf.c file will call our lib_main directly.  We do this to
Packit 6c4009
   keep things simple -- no need to use --export-dynamic with the linker
Packit 6c4009
   or build the main ELF as a PIE.
Packit 6c4009
Packit 6c4009
   The lib_main func will modify some of its state while dlopening and
Packit 6c4009
   dlclosing the bug-dl-leaf-lib-cb.so library.  The constructors and
Packit 6c4009
   destructors in that library will call back into this library to also
Packit 6c4009
   muck with state (the check_val_xxx funcs).
Packit 6c4009
Packit 6c4009
   If dlclose/dlopen are marked as "leaf" functions, then with newer
Packit 6c4009
   versions of gcc, the state modification won't work correctly.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
Packit 6c4009
static int val = 1;
Packit 6c4009
static int called = 0;
Packit 6c4009
Packit 6c4009
void check_val_init (void)
Packit 6c4009
{
Packit 6c4009
  called = 1;
Packit 6c4009
  assert (val == 2);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void check_val_fini (void)
Packit 6c4009
{
Packit 6c4009
  called = 2;
Packit 6c4009
  assert (val == 4);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int lib_main (void)
Packit 6c4009
{
Packit 6c4009
  int ret __attribute__ ((unused));
Packit 6c4009
  void *hdl;
Packit 6c4009
Packit 6c4009
  /* Make sure the constructor sees the updated val.  */
Packit 6c4009
  val = 2;
Packit 6c4009
  hdl = dlopen ("bug-dl-leaf-lib-cb.so", RTLD_GLOBAL | RTLD_LAZY);
Packit 6c4009
  val = 3;
Packit 6c4009
  assert (hdl);
Packit 6c4009
  assert (called == 1);
Packit 6c4009
Packit 6c4009
  /* Make sure the destructor sees the updated val.  */
Packit 6c4009
  val = 4;
Packit 6c4009
  ret = dlclose (hdl);
Packit 6c4009
  val = 5;
Packit 6c4009
  assert (ret == 0);
Packit 6c4009
  assert (called == 2);
Packit 6c4009
Packit 6c4009
  return !val;
Packit 6c4009
}