1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this list of 8 * conditions and the following disclaimer. 9 * 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef _LOS_DYNLINK_H 31 #define _LOS_DYNLINK_H 32 33 #include "elf.h" 34 #include "los_compiler.h" 35 #include "los_list.h" 36 #include "los_memory.h" 37 38 #ifdef __cplusplus 39 #if __cplusplus 40 extern "C" { 41 #endif /* __cplusplus */ 42 #endif /* __cplusplus */ 43 44 #ifdef LOSCFG_AARCH64 45 typedef Elf64_Ehdr LD_ELF_EHDR; 46 typedef Elf64_Phdr LD_ELF_PHDR; 47 typedef Elf64_Sym LD_ELF_SYM; 48 typedef Elf64_Dyn LD_ELF_DYN; 49 typedef Elf64_Rel LD_ELF_REL; 50 typedef Elf64_Rela LD_ELF_RELA; 51 #define REL_TYPE(x) ELF64_R_TYPE(x) 52 #define REL_SYM(x) ELF64_R_SYM(x) 53 #define REL_INFO ELF64_R_INFO 54 #else 55 typedef Elf32_Ehdr LD_ELF_EHDR; 56 typedef Elf32_Phdr LD_ELF_PHDR; 57 typedef Elf32_Sym LD_ELF_SYM; 58 typedef Elf32_Dyn LD_ELF_DYN; 59 typedef Elf32_Rel LD_ELF_REL; 60 typedef Elf32_Rela LD_ELF_RELA; 61 #define REL_TYPE(x) ELF32_R_TYPE(x) 62 #define REL_SYM(x) ELF32_R_SYM(x) 63 #define REL_INFO ELF32_R_INFO 64 #endif 65 66 #define LD_ELFMAG "\177ELF" 67 #define LD_SELFMAG 4 68 #define PHDR_NUM_MAX 128 69 #define DYNAMIC_CNT 32 70 #define HASH_MASK 0xf0000000 71 #define WORD_SHIFT 4 72 #define HASH_SHIFT 24 73 #define BUCKET_IDX 2 74 75 #define ELF_ALIGN_UP(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) 76 #define ELF_ALIGN_DOWN(a, b) ((a) & ~((b) - 1)) 77 #define ELF_ALIGN_OFFSET(a, b) ((a) & ((b) - 1)) 78 #ifndef PATH_MAX 79 #define PATH_MAX 256 80 #endif 81 #ifndef FILE_LENGTH_MAX 82 #define FILE_LENGTH_MAX 0x40000 83 #endif 84 typedef VOID (*INIT_FINI_FUNC)(VOID); 85 86 typedef struct { 87 UINTPTR relTab; 88 UINT32 relTabSize; 89 UINT32 relEntSize; 90 } RelocInfo; 91 92 typedef struct { 93 RelocInfo rel; 94 RelocInfo rela; 95 RelocInfo jmpRel; 96 } RelocInfoTab; 97 98 typedef struct { 99 UINTPTR func; 100 UINTPTR array; 101 UINT32 arraySz; 102 } InitFiniInfo; 103 104 typedef struct { 105 InitFiniInfo init; 106 InitFiniInfo fini; 107 } InitFiniTab; 108 109 typedef struct { 110 LD_ELF_EHDR elfEhdr; 111 LD_ELF_PHDR *elfPhdr; 112 UINTPTR dynBase; 113 UINT32 *hashTab; 114 LD_ELF_SYM *symTab; 115 CHAR *symStrings; 116 RelocInfoTab relInfoTab; 117 } DynLinkInfo; 118 119 typedef struct { 120 CHAR *fileName; 121 INT32 fd; 122 DynLinkInfo *dlInfo; 123 UINTPTR loadBase; 124 InitFiniTab initFiniTab; 125 LOS_DL_LIST dsoNode; 126 UINT32 ref; 127 VOID *pool; 128 CHAR buf[]; 129 } DynSharedObj; 130 131 typedef struct { 132 CHAR *name; 133 UINTPTR addr; 134 } SymInfo; 135 136 #define SYM_EXPORT(func) \ 137 const SymInfo sym_##func __attribute__((__used__, section(".sym."#func))) = { \ 138 .name = #func, \ 139 .addr = (UINTPTR)func \ 140 }; 141 142 /* 143 * @brief Load the shared library file named by the NULL-terminated string filename and 144 * return a valid handle for the loaded library. 145 * 146 * @param fileName The name pointer of shared library. 147 * @param pool The heap for shared library to load. If the parameter, pool, is NULL, then 148 * the dynamic loader & linker module will use the default heap and the pool is not NULL, 149 * then the shared library will be loaded to the heap by pool. 150 * 151 * @note When the heap, pool, is not NULL, you should call LOS_MemInit() to initialize the 152 * pool before calling LOS_SoLoad(). By the way, the system will consume a certain amount 153 * of memory to initialize the pool. LOS_SoLoad must not be called in interrupt callback. 154 * 155 * @return Return NULL if error. Return non-NULL if success. 156 */ 157 VOID *LOS_SoLoad(const CHAR *fileName, VOID *pool); 158 159 /* 160 * @brief Get the address of symbol named by the parameter, name, from the parameter, handle. 161 * 162 * @param handle The handle for the loaded shared library. 163 * @param name The name of symbol to search. 164 * 165 * @note LOS_FindSym must not be called in interrupt callback. 166 * 167 * @return Return NULL if error. Return non-NULL if success. 168 */ 169 VOID *LOS_FindSym(VOID *handle, const CHAR *name); 170 171 /* 172 * @brief Decrement the reference count on the loaded shared library referred to by handle. 173 * If the reference count drops to zero, then the library is unloaded. 174 * 175 * This function validates that the handle is valid. 176 * 177 * @param handle The handle for the loaded shared library by LOS_SoLoad() interface. 178 * 179 * @note LOS_SoUnload must not be called in interrupt callback. 180 * 181 * @return Return 1 if error. Return 0 if success. 182 */ 183 INT32 LOS_SoUnload(VOID *handle); 184 185 /* 186 * @brief Initialization for dynamic loader & linker module. 187 * 188 * @param VOID. 189 * 190 * @return Return LOS_NOK if error. Return LOS_OK if success. 191 */ 192 INT32 LOS_DynlinkInit(VOID); 193 194 #ifdef __cplusplus 195 #if __cplusplus 196 } 197 #endif /* __cplusplus */ 198 #endif /* __cplusplus */ 199 200 #endif /* _LOS_DYNLINK_H */ 201 202