1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LOS_LOAD_ELF_H
33 #define _LOS_LOAD_ELF_H
34
35 #include "los_ld_elf_pri.h"
36 #include "los_elf_auxvec_pri.h"
37 #include "los_process_pri.h"
38 #include "los_memory.h"
39 #include "los_strncpy_from_user.h"
40 #include "los_strnlen_user.h"
41 #include "los_user_put.h"
42 #include "los_user_get.h"
43 #include "user_copy.h"
44 #include "sys/stat.h"
45 #ifdef LOSCFG_DRIVERS_TZDRIVER
46 #include "fs/file.h"
47 #endif
48 #include "unistd.h"
49
50 #ifdef __cplusplus
51 #if __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
54 #endif /* __cplusplus */
55
56 #define INTERP_FULL_PATH "/lib/libc.so"
57 #define INVALID_FD (-1)
58 #define STRINGS_COUNT_MAX 256
59 #define ELF_PHDR_NUM_MAX 128
60 #define FILE_LENGTH_MAX 0x1000000
61 #define MEM_SIZE_MAX 0x1000000
62
63 #ifndef FILE_PATH_MAX
64 #define FILE_PATH_MAX PATH_MAX
65 #endif
66 #ifndef FILE_PATH_MIN
67 #define FILE_PATH_MIN 2
68 #endif
69
70 #define USER_STACK_SIZE 0x100000
71 #define USER_PARAM_BYTE_MAX 0x1000
72 #define USER_STACK_TOP_MAX USER_ASPACE_TOP_MAX
73
74 #define EXEC_MMAP_BASE 0x02000000
75
76 #ifdef LOSCFG_ASLR
77 #define RANDOM_MASK ((((USER_ASPACE_TOP_MAX + GB - 1) & (-GB)) >> 3) - 1)
78 #endif
79
80 #define STACK_ALIGN_SIZE 0x10
81 #define RANDOM_VECTOR_SIZE 1
82
83 /* The permissions on sections in the program header. */
84 #define PF_R 0x4
85 #define PF_W 0x2
86 #define PF_X 0x1
87
88 typedef struct {
89 LD_ELF_EHDR elfEhdr;
90 LD_ELF_PHDR *elfPhdr;
91 UINT32 fileLen;
92 INT32 procfd;
93 } ELFInfo;
94
95 typedef struct {
96 ELFInfo execInfo;
97 ELFInfo interpInfo;
98 const CHAR *fileName;
99 CHAR *execName;
100 INT32 argc;
101 INT32 envc;
102 CHAR * const *argv;
103 CHAR * const *envp;
104 UINTPTR stackTop;
105 UINTPTR stackTopMax;
106 UINTPTR stackBase;
107 UINTPTR stackParamBase;
108 UINT32 stackSize;
109 INT32 stackProt;
110 UINTPTR argStart;
111 UINTPTR loadAddr;
112 UINTPTR elfEntry;
113 UINTPTR topOfMem;
114 UINTPTR oldFiles;
115 LosVmSpace *newSpace;
116 LosVmSpace *oldSpace;
117 INT32 randomDevFD;
118 } ELFLoadInfo;
119
OsIsBadUserAddress(VADDR_T vaddr)120 STATIC INLINE BOOL OsIsBadUserAddress(VADDR_T vaddr)
121 {
122 return (vaddr >= USER_STACK_TOP_MAX);
123 }
124
125 extern UINT32 OsGetRndOffset(INT32 randomDevFD);
126 extern INT32 OsLoadELFFile(ELFLoadInfo *loadInfo);
127
128 #ifdef __cplusplus
129 #if __cplusplus
130 }
131 #endif /* __cplusplus */
132 #endif /* __cplusplus */
133
134 #endif /* _LOS_ELF_LIB_H */
135