dlmopen.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Load a shared object at run time.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <errno.h>
  17. #include <libintl.h>
  18. #include <stddef.h>
  19. #include <unistd.h>
  20. #include <ldsodefs.h>
  21. #include <shlib-compat.h>
  22. struct dlmopen_args
  23. {
  24. /* Namespace ID. */
  25. Lmid_t nsid;
  26. /* The arguments for dlopen_doit. */
  27. const char *file;
  28. int mode;
  29. /* The return value of dlopen_doit. */
  30. void *new;
  31. /* Address of the caller. */
  32. const void *caller;
  33. };
  34. static void
  35. dlmopen_doit (void *a)
  36. {
  37. struct dlmopen_args *args = (struct dlmopen_args *) a;
  38. /* Non-shared code has no support for multiple namespaces. */
  39. if (args->nsid != LM_ID_BASE)
  40. {
  41. # ifdef SHARED
  42. /* If trying to open the link map for the main executable the namespace
  43. must be the main one. */
  44. if (args->file == NULL)
  45. # endif
  46. _dl_signal_error (EINVAL, NULL, NULL, N_("invalid namespace"));
  47. /* It makes no sense to use RTLD_GLOBAL when loading a DSO into
  48. a namespace other than the base namespace. */
  49. if (__glibc_unlikely (args->mode & RTLD_GLOBAL))
  50. _dl_signal_error (EINVAL, NULL, NULL, N_("invalid mode"));
  51. }
  52. args->new = GLRO(dl_open) (args->file ?: "", args->mode | __RTLD_DLOPEN,
  53. args->caller,
  54. args->nsid, __libc_argc, __libc_argv, __environ);
  55. }
  56. static void *
  57. dlmopen_implementation (Lmid_t nsid, const char *file, int mode,
  58. void *dl_caller)
  59. {
  60. struct dlmopen_args args;
  61. args.nsid = nsid;
  62. args.file = file;
  63. args.mode = mode;
  64. args.caller = dl_caller;
  65. return _dlerror_run (dlmopen_doit, &args) ? NULL : args.new;
  66. }
  67. #ifdef SHARED
  68. void *
  69. ___dlmopen (Lmid_t nsid, const char *file, int mode)
  70. {
  71. if (GLRO (dl_dlfcn_hook) != NULL)
  72. return GLRO (dl_dlfcn_hook)->dlmopen (nsid, file, mode, RETURN_ADDRESS (0));
  73. else
  74. return dlmopen_implementation (nsid, file, mode, RETURN_ADDRESS (0));
  75. }
  76. versioned_symbol (libc, ___dlmopen, dlmopen, GLIBC_2_34);
  77. # if OTHER_SHLIB_COMPAT (libdl, GLIBC_2_3_4, GLIBC_2_34)
  78. compat_symbol (libdl, ___dlmopen, dlmopen, GLIBC_2_3_4);
  79. # endif
  80. #else /* !SHARED */
  81. /* Also used with _dlfcn_hook. */
  82. void *
  83. __dlmopen (Lmid_t nsid, const char *file, int mode, void *dl_caller)
  84. {
  85. return dlmopen_implementation (nsid, file, mode, RETURN_ADDRESS (0));
  86. }
  87. void *
  88. ___dlmopen (Lmid_t nsid, const char *file, int mode)
  89. {
  90. return __dlmopen (nsid, file, mode, RETURN_ADDRESS (0));
  91. }
  92. weak_alias (___dlmopen, dlmopen)
  93. static_link_warning (dlmopen)
  94. #endif /* !SHARED */