shared_library_load_mode.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2021 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_SHARED_LIBRARY_MODE_HPP
  8. #define BOOST_DLL_SHARED_LIBRARY_MODE_HPP
  9. #include <boost/dll/config.hpp>
  10. #include <boost/predef/os.h>
  11. #include <boost/predef/library/c.h>
  12. #if BOOST_OS_WINDOWS
  13. # include <boost/winapi/dll.hpp>
  14. #else
  15. # include <dlfcn.h>
  16. #endif
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. # pragma once
  19. #endif
  20. /// \file boost/dll/shared_library_load_mode.hpp
  21. /// \brief Contains only the boost::dll::load_mode::type enum and operators related to it.
  22. namespace boost { namespace dll { namespace load_mode {
  23. /*! Library load modes.
  24. *
  25. * Each of system family provides own modes. Flags not supported by a particular platform will be silently ignored.
  26. *
  27. * For a detailed description of platform specific options see:
  28. * <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx">Windows specific options</a>,
  29. * <a href="http://pubs.opengroup.org/onlinepubs/000095399/functions/dlopen.html">POSIX specific options</a>.
  30. *
  31. */
  32. enum type {
  33. #ifdef BOOST_DLL_DOXYGEN
  34. /*!
  35. * Default open mode. See the \b Default: comments below to find out the flags that are enabled by default.
  36. */
  37. default_mode,
  38. /*!
  39. * \b Platforms: Windows
  40. *
  41. * \b Default: disabled
  42. *
  43. * If this value is used, and the executable module is a DLL, the system does
  44. * not call DllMain for process and thread initialization and termination.
  45. * Also, the system does not load additional executable modules that are
  46. * referenced by the specified module.
  47. *
  48. * Note Do not use this value; it is provided only for backward compatibility.
  49. * If you are planning to access only data or resources in the DLL, use
  50. * LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE or LOAD_LIBRARY_AS_IMAGE_RESOURCE
  51. * or both.
  52. */
  53. dont_resolve_dll_references,
  54. /*!
  55. * \b Platforms: Windows
  56. *
  57. * \b Default: disabled
  58. *
  59. * If this value is used, the system does not check AppLocker rules or
  60. * apply Software Restriction Policies for the DLL.
  61. */
  62. load_ignore_code_authz_level,
  63. /*!
  64. * \b Platforms: Windows
  65. *
  66. * \b Default: disabled
  67. *
  68. * If this value is used and lpFileName specifies an absolute path,
  69. * the system uses the alternate file search strategy.
  70. *
  71. * This value cannot be combined with any LOAD_LIBRARY_SEARCH flag.
  72. */
  73. load_with_altered_search_path,
  74. /*!
  75. * \b Platforms: POSIX
  76. *
  77. * \b Default: enabled
  78. *
  79. * Relocations shall be performed at an implementation-defined time, ranging
  80. * from the time of the dlopen() call until the first reference to a given
  81. * symbol occurs.
  82. *
  83. * Specifying RTLD_LAZY should improve performance on implementations
  84. * supporting dynamic symbol binding as a process may not reference all of
  85. * the functions in any given object. And, for systems supporting dynamic
  86. * symbol resolution for normal process execution, this behavior mimics
  87. * the normal handling of process execution.
  88. */
  89. rtld_lazy,
  90. /*!
  91. * \b Platforms: POSIX
  92. *
  93. * \b Default: disabled
  94. *
  95. * All necessary relocations shall be performed when the object is first
  96. * loaded. This may waste some processing if relocations are performed for
  97. * functions that are never referenced. This behavior may be useful for
  98. * plugins that need to know as soon as an object is loaded that all
  99. * symbols referenced during execution are available.
  100. */
  101. rtld_now,
  102. /*!
  103. * \b Platforms: POSIX
  104. *
  105. * \b Default: disabled
  106. *
  107. * The object's symbols shall be made available for the relocation
  108. * processing of any other object. In addition, symbol lookup using
  109. * dlopen(0, mode) and an associated dlsym() allows objects loaded
  110. * with this mode to be searched.
  111. */
  112. rtld_global,
  113. /*!
  114. * \b Platforms: POSIX
  115. *
  116. * \b Default: enabled
  117. *
  118. * The object's symbols shall not be made available for the relocation
  119. * processing of any other object.
  120. *
  121. * This is a default Windows behavior that can not be changed.
  122. */
  123. rtld_local,
  124. /*!
  125. * \b Platforms: POSIX (requires glibc >= 2.3.4)
  126. *
  127. * \b Default: disabled
  128. *
  129. * The object will use its own symbols in preference to global symbols
  130. * with the same name contained in libraries that have already been loaded.
  131. * This flag is not specified in POSIX.1-2001.
  132. */
  133. rtld_deepbind,
  134. /*!
  135. * \b Platforms: Windows, POSIX
  136. *
  137. * \b Default: disabled
  138. *
  139. * Append a platform specific extension and prefix to shared library filename before trying to load it.
  140. * If load attempt fails, try to load with exactly specified name.
  141. *
  142. * \b Example:
  143. * \code
  144. * // Opens `./my_plugins/plugin1.dll` on Windows, `./my_plugins/libplugin1.so` on Linux, `./my_plugins/libplugin1.dylib` on MacOS.
  145. * // If that fails, loads `./my_plugins/plugin1`
  146. * boost::dll::shared_library lib("./my_plugins/plugin1", load_mode::append_decorations);
  147. * \endcode
  148. */
  149. append_decorations,
  150. /*!
  151. * \b Platforms: Windows, POSIX
  152. *
  153. * \b Default: disabled
  154. *
  155. * Allow loading from system folders if path to library contains no parent path.
  156. */
  157. search_system_folders
  158. #elif BOOST_OS_WINDOWS
  159. default_mode = 0,
  160. dont_resolve_dll_references = boost::winapi::DONT_RESOLVE_DLL_REFERENCES_,
  161. load_ignore_code_authz_level = boost::winapi::LOAD_IGNORE_CODE_AUTHZ_LEVEL_,
  162. load_with_altered_search_path = boost::winapi::LOAD_WITH_ALTERED_SEARCH_PATH_,
  163. rtld_lazy = 0,
  164. rtld_now = 0,
  165. rtld_global = 0,
  166. rtld_local = 0,
  167. rtld_deepbind = 0,
  168. append_decorations = 0x00800000,
  169. search_system_folders = (append_decorations << 1)
  170. #else
  171. default_mode = 0,
  172. dont_resolve_dll_references = 0,
  173. load_ignore_code_authz_level = 0,
  174. load_with_altered_search_path = 0,
  175. rtld_lazy = RTLD_LAZY,
  176. rtld_now = RTLD_NOW,
  177. rtld_global = RTLD_GLOBAL,
  178. rtld_local = RTLD_LOCAL,
  179. #if BOOST_LIB_C_GNU < BOOST_VERSION_NUMBER(2,3,4)
  180. rtld_deepbind = 0,
  181. #else
  182. rtld_deepbind = RTLD_DEEPBIND,
  183. #endif
  184. append_decorations = 0x00800000,
  185. search_system_folders = (append_decorations << 1)
  186. #endif
  187. };
  188. /// Free operators for load_mode::type flag manipulation.
  189. BOOST_CONSTEXPR inline type operator|(type left, type right) BOOST_NOEXCEPT {
  190. return static_cast<type>(
  191. static_cast<unsigned int>(left) | static_cast<unsigned int>(right)
  192. );
  193. }
  194. BOOST_CXX14_CONSTEXPR inline type& operator|=(type& left, type right) BOOST_NOEXCEPT {
  195. left = left | right;
  196. return left;
  197. }
  198. BOOST_CONSTEXPR inline type operator&(type left, type right) BOOST_NOEXCEPT {
  199. return static_cast<type>(
  200. static_cast<unsigned int>(left) & static_cast<unsigned int>(right)
  201. );
  202. }
  203. BOOST_CXX14_CONSTEXPR inline type& operator&=(type& left, type right) BOOST_NOEXCEPT {
  204. left = left & right;
  205. return left;
  206. }
  207. BOOST_CONSTEXPR inline type operator^(type left, type right) BOOST_NOEXCEPT {
  208. return static_cast<type>(
  209. static_cast<unsigned int>(left) ^ static_cast<unsigned int>(right)
  210. );
  211. }
  212. BOOST_CXX14_CONSTEXPR inline type& operator^=(type& left, type right) BOOST_NOEXCEPT {
  213. left = left ^ right;
  214. return left;
  215. }
  216. BOOST_CONSTEXPR inline type operator~(type left) BOOST_NOEXCEPT {
  217. return static_cast<type>(
  218. ~static_cast<unsigned int>(left)
  219. );
  220. }
  221. }}} // boost::dll::load_mode
  222. #endif // BOOST_DLL_SHARED_LIBRARY_MODE_HPP