dlopen.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <libintl.h>
  17. #include <stddef.h>
  18. #include <unistd.h>
  19. #include <ldsodefs.h>
  20. #include <shlib-compat.h>
  21. struct dlopen_args
  22. {
  23. /* The arguments for dlopen_doit. */
  24. const char *file;
  25. int mode;
  26. /* The return value of dlopen_doit. */
  27. void *new;
  28. /* Address of the caller. */
  29. const void *caller;
  30. };
  31. /* Non-shared code has no support for multiple namespaces. */
  32. #ifdef SHARED
  33. # define NS __LM_ID_CALLER
  34. #else
  35. # define NS LM_ID_BASE
  36. #endif
  37. static void
  38. dlopen_doit (void *a)
  39. {
  40. struct dlopen_args *args = (struct dlopen_args *) a;
  41. if (args->mode & ~(RTLD_BINDING_MASK | RTLD_NOLOAD | RTLD_DEEPBIND
  42. | RTLD_GLOBAL | RTLD_LOCAL | RTLD_NODELETE
  43. | __RTLD_SPROF))
  44. _dl_signal_error (0, NULL, NULL, _("invalid mode parameter"));
  45. args->new = GLRO(dl_open) (args->file ?: "", args->mode | __RTLD_DLOPEN,
  46. args->caller,
  47. args->file == NULL ? LM_ID_BASE : NS,
  48. __libc_argc, __libc_argv, __environ);
  49. }
  50. static void *
  51. dlopen_implementation (const char *file, int mode, void *dl_caller)
  52. {
  53. struct dlopen_args args;
  54. args.file = file;
  55. args.mode = mode;
  56. args.caller = dl_caller;
  57. return _dlerror_run (dlopen_doit, &args) ? NULL : args.new;
  58. }
  59. #ifdef SHARED
  60. void *
  61. ___dlopen (const char *file, int mode)
  62. {
  63. if (GLRO (dl_dlfcn_hook) != NULL)
  64. return GLRO (dl_dlfcn_hook)->dlopen (file, mode, RETURN_ADDRESS (0));
  65. else
  66. return dlopen_implementation (file, mode, RETURN_ADDRESS (0));
  67. }
  68. versioned_symbol (libc, ___dlopen, dlopen, GLIBC_2_34);
  69. # if OTHER_SHLIB_COMPAT (libdl, GLIBC_2_1, GLIBC_2_34)
  70. compat_symbol (libdl, ___dlopen, dlopen, GLIBC_2_1);
  71. # endif
  72. #else /* !SHARED */
  73. /* Also used with _dlfcn_hook. */
  74. void *
  75. __dlopen (const char *file, int mode, void *dl_caller)
  76. {
  77. return dlopen_implementation (file, mode, dl_caller);
  78. }
  79. void *
  80. ___dlopen (const char *file, int mode)
  81. {
  82. return __dlopen (file, mode, RETURN_ADDRESS (0));
  83. }
  84. weak_alias (___dlopen, dlopen)
  85. static_link_warning (dlopen)
  86. #endif /* !SHARED */