• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DLFCN_EXT_H
17 #define DLFCN_EXT_H
18 
19 #include <dlfcn.h>
20 #include <stddef.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* for dl_extinfo flag */
27 #define DL_EXT_WRITE_RELRO 0x1
28 #define DL_EXT_USE_RELRO 0x2
29 #define DL_EXT_RESERVED_ADDRESS_RECURSIVE 0x4
30 #define DL_EXT_RESERVED_ADDRESS 0x8
31 #define DL_EXT_RESERVED_ADDRESS_HINT 0x10
32 
33 /** Mask of valid bits. */
34 #define DL_EXT_VALID_FLAG_BITS                                                                             \
35     (DL_EXT_WRITE_RELRO | DL_EXT_USE_RELRO | DL_EXT_RESERVED_ADDRESS_RECURSIVE | DL_EXT_RESERVED_ADDRESS | \
36         DL_EXT_RESERVED_ADDRESS_HINT)
37 
38 typedef struct {
39     int flag;
40     int relro_fd;
41     void *reserved_addr;
42     size_t reserved_size;
43 } dl_extinfo;
44 
45 /**
46  * @brief Loads the dynamic shared object (shared library) file with the extended feature.
47  *        If extinfo is NULL, it is equivalent to dlopen.
48  *        If DL_EXT_WRITE_RELRO is set in extinfo, the GNU RELRO section will be written to relro_fd and allowed to
49  *        reused by other process loading the same library at the same address.
50  *        IF DL_EXT_USE_RELRO is set in extinfo, the GNU RELRO section written in relro_fd will be reused.
51  * @param file Equivalent to the argument of dlopen.
52  * @param mode Equivalent to the argument of dlopen.
53  * @param extinfo Indicates the dl_extinfo struct.
54  * @return Returns a non-NULL handle for the loaded object on success. On error returns NULL.
55  */
56 void *dlopen_ext(const char *file, int mode, const dl_extinfo *extinfo);
57 
58 /**
59   * @brief open dso in given namespace which has own lib search paths,
60   *        when namespace is null, it's same to dlopen_ext().
61   *        avoid using "default" as namespace, which is the default namespace.
62   * @param Dl_namespace * Carry the naming information of the namespace.
63   * @param char * the name of the so file you want to open.
64   * @param int open file mode.
65   *    -- RTLD_LAZY.
66   *    -- RTLD_NOW.
67   *    -- RTLD_NOLOAD.
68   *    -- RTLD_NODELETE.
69   *    -- RTLD_GLOBAL.
70   *    -- RTLD_LOCAL.
71   * @param dl_extinfo * indicates the dl_extinfo struct,include flag and relro_fd.
72   * @return success: dynamic library handleoid,failed: NULL.
73   * @retval none.
74   */
75 void *dlopen_ns_ext(Dl_namespace *, const char *, int, const dl_extinfo *);
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 
81 #endif
82