1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_dynamic_link
18 #define __TBB_dynamic_link
19 
20 // Support for dynamic loading entry points from other shared libraries.
21 
22 #include "oneapi/tbb/detail/_config.h"
23 
24 #include <atomic>
25 #include <mutex>
26 
27 /** By default, symbols declared and defined here go into namespace tbb::internal.
28     To put them in other namespace, define macros OPEN_INTERNAL_NAMESPACE
29     and CLOSE_INTERNAL_NAMESPACE to override the following default definitions. **/
30 
31 #include <cstddef>
32 #if _WIN32
33 #include <Windows.h>
34 #endif /* _WIN32 */
35 
36 namespace tbb {
37 namespace detail {
38 namespace r1 {
39 
40 //! Type definition for a pointer to a void somefunc(void)
41 typedef void (*pointer_to_handler)();
42 
43 //! The helper to construct dynamic_link_descriptor structure
44 // Double cast through the void* in DLD macro is necessary to
45 // prevent warnings from some compilers (g++ 4.1)
46 #if __TBB_WEAK_SYMBOLS_PRESENT
47 #define DLD(s,h) {#s, (pointer_to_handler*)(void*)(&h), (pointer_to_handler)&s}
48 #define DLD_NOWEAK(s,h) {#s, (pointer_to_handler*)(void*)(&h), NULL}
49 #else
50 #define DLD(s,h) {#s, (pointer_to_handler*)(void*)(&h)}
51 #define DLD_NOWEAK(s,h) DLD(s,h)
52 #endif /* __TBB_WEAK_SYMBOLS_PRESENT */
53 //! Association between a handler name and location of pointer to it.
54 struct dynamic_link_descriptor {
55     //! Name of the handler
56     const char* name;
57     //! Pointer to the handler
58     pointer_to_handler* handler;
59 #if __TBB_WEAK_SYMBOLS_PRESENT
60     //! Weak symbol
61     pointer_to_handler ptr;
62 #endif
63 };
64 
65 #if _WIN32
66 using dynamic_link_handle = HMODULE;
67 #else
68 using dynamic_link_handle = void*;
69 #endif /* _WIN32 */
70 
71 const int DYNAMIC_LINK_GLOBAL        = 0x01;
72 const int DYNAMIC_LINK_LOAD          = 0x02;
73 const int DYNAMIC_LINK_WEAK          = 0x04;
74 const int DYNAMIC_LINK_LOCAL         = 0x08;
75 
76 const int DYNAMIC_LINK_LOCAL_BINDING = DYNAMIC_LINK_LOCAL | DYNAMIC_LINK_LOAD;
77 const int DYNAMIC_LINK_DEFAULT       = DYNAMIC_LINK_GLOBAL | DYNAMIC_LINK_LOAD | DYNAMIC_LINK_WEAK;
78 
79 //! Fill in dynamically linked handlers.
80 /** 'library' is the name of the requested library. It should not contain a full
81     path since dynamic_link adds the full path (from which the runtime itself
82     was loaded) to the library name.
83     'required' is the number of the initial entries in the array descriptors[]
84     that have to be found in order for the call to succeed. If the library and
85     all the required handlers are found, then the corresponding handler
86     pointers are set, and the return value is true.  Otherwise the original
87     array of descriptors is left untouched and the return value is false.
88     'required' is limited by 20 (exceeding of this value will result in failure
89     to load the symbols and the return value will be false).
90     'handle' is the handle of the library if it is loaded. Otherwise it is left
91     untouched.
92     'flags' is the set of DYNAMIC_LINK_* flags. Each of the DYNAMIC_LINK_* flags
93     allows its corresponding linking stage.
94 **/
95 bool dynamic_link( const char* library,
96                    const dynamic_link_descriptor descriptors[],
97                    std::size_t required,
98                    dynamic_link_handle* handle = 0,
99                    int flags = DYNAMIC_LINK_DEFAULT );
100 
101 void dynamic_unlink( dynamic_link_handle handle );
102 
103 void dynamic_unlink_all();
104 
105 enum dynamic_link_error_t {
106     dl_success = 0,
107     dl_lib_not_found,     // char const * lib, dlerr_t err
108     dl_sym_not_found,     // char const * sym, dlerr_t err
109                           // Note: dlerr_t depends on OS: it is char const * on Linux* and macOS*, int on Windows*.
110     dl_sys_fail,          // char const * func, int err
111     dl_buff_too_small     // none
112 }; // dynamic_link_error_t
113 
114 } // namespace r1
115 } // namespace detail
116 } // namespace tbb
117 
118 #endif /* __TBB_dynamic_link */
119