Blame dlfcn/dlmopen.c

Packit 6c4009
/* Load a shared object at run time.
Packit 6c4009
   Copyright (C) 1995-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 <errno.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
Packit 6c4009
#if !defined SHARED && IS_IN (libdl)
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
dlmopen (Lmid_t nsid, const char *file, int mode)
Packit 6c4009
{
Packit 6c4009
  return __dlmopen (nsid, file, mode, RETURN_ADDRESS (0));
Packit 6c4009
}
Packit 6c4009
static_link_warning (dlmopen)
Packit 6c4009
Packit 6c4009
#else
Packit 6c4009
Packit 6c4009
struct dlmopen_args
Packit 6c4009
{
Packit 6c4009
  /* Namespace ID.  */
Packit 6c4009
  Lmid_t nsid;
Packit 6c4009
  /* The arguments for dlopen_doit.  */
Packit 6c4009
  const char *file;
Packit 6c4009
  int mode;
Packit 6c4009
  /* The return value of dlopen_doit.  */
Packit 6c4009
  void *new;
Packit 6c4009
  /* Address of the caller.  */
Packit 6c4009
  const void *caller;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
dlmopen_doit (void *a)
Packit 6c4009
{
Packit 6c4009
  struct dlmopen_args *args = (struct dlmopen_args *) a;
Packit 6c4009
Packit 6c4009
  /* Non-shared code has no support for multiple namespaces.  */
Packit 6c4009
  if (args->nsid != LM_ID_BASE)
Packit 6c4009
    {
Packit 6c4009
# ifdef SHARED
Packit 6c4009
      /* If trying to open the link map for the main executable the namespace
Packit 6c4009
	 must be the main one.  */
Packit 6c4009
      if (args->file == NULL)
Packit 6c4009
# endif
Packit 6c4009
	_dl_signal_error (EINVAL, NULL, NULL, N_("invalid namespace"));
Packit 6c4009
Packit 6c4009
      /* It makes no sense to use RTLD_GLOBAL when loading a DSO into
Packit 6c4009
	 a namespace other than the base namespace.  */
Packit 6c4009
      if (__glibc_unlikely (args->mode & RTLD_GLOBAL))
Packit 6c4009
	_dl_signal_error (EINVAL, NULL, NULL, N_("invalid mode"));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  args->new = GLRO(dl_open) (args->file ?: "", args->mode | __RTLD_DLOPEN,
Packit 6c4009
			     args->caller,
Packit 6c4009
			     args->nsid, __dlfcn_argc, __dlfcn_argv,
Packit 6c4009
			     __environ);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
__dlmopen (Lmid_t nsid, const char *file, int mode DL_CALLER_DECL)
Packit 6c4009
{
Packit 6c4009
# ifdef SHARED
Packit 6c4009
  if (!rtld_active ())
Packit 6c4009
    return _dlfcn_hook->dlmopen (nsid, file, mode, RETURN_ADDRESS (0));
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
  struct dlmopen_args args;
Packit 6c4009
  args.nsid = nsid;
Packit 6c4009
  args.file = file;
Packit 6c4009
  args.mode = mode;
Packit 6c4009
  args.caller = DL_CALLER;
Packit 6c4009
Packit 6c4009
# ifdef SHARED
Packit 6c4009
  return _dlerror_run (dlmopen_doit, &args) ? NULL : args.new;
Packit 6c4009
# else
Packit 6c4009
  if (_dlerror_run (dlmopen_doit, &args))
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  __libc_register_dl_open_hook ((struct link_map *) args.new);
Packit 6c4009
  __libc_register_dlfcn_hook ((struct link_map *) args.new);
Packit 6c4009
Packit 6c4009
  return args.new;
Packit 6c4009
# endif
Packit 6c4009
}
Packit 6c4009
# ifdef SHARED
Packit 6c4009
strong_alias (__dlmopen, dlmopen)
Packit 6c4009
# endif
Packit 6c4009
#endif