• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _INTERNAL_RELOC_H
2 #define _INTERNAL_RELOC_H
3 
4 #include <features.h>
5 #include <elf.h>
6 #include <stdint.h>
7 #include <stddef.h>
8 #include <stdarg.h>
9 #ifndef __LITEOS__
10 #include <link.h>
11 #include <sys/types.h>
12 #include <stdlib.h>
13 #include "libc.h"
14 #include "stdatomic_impl.h"
15 #ifdef __HISPARK_LINUX__
16 #include "../../ldso/namespace.h"
17 #else
18 #include "../../ldso/linux/namespace.h"
19 #endif
20 #endif
21 
22 #if UINTPTR_MAX == 0xffffffff
23 typedef Elf32_Ehdr Ehdr;
24 typedef Elf32_Phdr Phdr;
25 typedef Elf32_Sym Sym;
26 #ifndef __LITEOS__
27 typedef Elf32_Shdr Shdr;
28 typedef Elf32_Verdaux Verdaux;
29 typedef Elf32_Verdef Verdef;
30 typedef Elf32_Vernaux Vernaux;
31 typedef Elf32_Verneed Verneed;
32 #endif
33 #define R_TYPE(x) ((x)&255)
34 #define R_SYM(x) ((x)>>8)
35 #define R_INFO ELF32_R_INFO
36 #else
37 typedef Elf64_Ehdr Ehdr;
38 typedef Elf64_Phdr Phdr;
39 typedef Elf64_Sym Sym;
40 #ifndef __LITEOS__
41 typedef Elf64_Shdr Shdr;
42 typedef Elf64_Verdaux Verdaux;
43 typedef Elf64_Verdef Verdef;
44 typedef Elf64_Vernaux Vernaux;
45 typedef Elf64_Verneed Verneed;
46 #endif
47 #define R_TYPE(x) ((x)&0x7fffffff)
48 #define R_SYM(x) ((x)>>32)
49 #define R_INFO ELF64_R_INFO
50 #endif
51 
52 /* These enum constants provide unmatchable default values for
53  * any relocation type the arch does not use. */
54 enum {
55 	REL_NONE = 0,
56 	REL_SYMBOLIC = -100,
57 	REL_USYMBOLIC,
58 	REL_GOT,
59 	REL_PLT,
60 	REL_RELATIVE,
61 	REL_OFFSET,
62 	REL_OFFSET32,
63 	REL_COPY,
64 	REL_SYM_OR_REL,
65 	REL_DTPMOD,
66 	REL_DTPOFF,
67 	REL_TPOFF,
68 	REL_TPOFF_NEG,
69 	REL_TLSDESC,
70 	REL_FUNCDESC,
71 	REL_FUNCDESC_VAL,
72 	REL_AUTH_SYMBOLIC,
73 	REL_AUTH_RELATIVE,
74 	REL_AUTH_GLOB_DAT,
75 };
76 #ifndef __LITEOS__
77 struct td_index {
78 	size_t args[2];
79 	struct td_index *next;
80 };
81 
82 struct verinfo {
83 	const char *s;
84 	const char *v;
85 	bool use_vna_hash;
86 	uint32_t vna_hash;
87 };
88 
89 struct sym_info_pair {
90 	uint_fast32_t sym_h;
91 	uint32_t sym_l;
92 };
93 
94 struct cfi_modifier {
95 	size_t offset;
96 	size_t modifier;
97 };
98 
99 struct icall_item {
100 	_Atomic(uint64_t) pc_check;
101 	int valid;
102 	size_t base;
103 	size_t modifier_begin;
104 	size_t modifier_end;
105 };
106 
107 struct dso {
108 #if DL_FDPIC
109 	struct fdpic_loadmap *loadmap;
110 #else
111 	unsigned char *base;
112 #endif
113 	char *name;
114 	size_t *dynv;
115 	struct dso *next, *prev;
116 	/* add namespace */
117 	ns_t *namespace;
118 	int cache_sym_index;
119 	struct dso *cache_dso;
120 	Sym *cache_sym;
121 	Phdr *phdr;
122 	int phnum;
123 	size_t phentsize;
124 	Sym *syms;
125 	Elf_Symndx *hashtab;
126 	uint32_t *ghashtab;
127 	int16_t *versym;
128 	Verdef *verdef;
129 	Verneed *verneed;
130 	char *strings;
131 	struct dso *syms_next, *lazy_next;
132 	size_t modifier_begin;
133 	size_t modifier_end;
134 	struct icall_item *item;
135 	size_t *lazy, lazy_cnt;
136 	unsigned char *map;
137 	size_t map_len;
138 	dev_t dev;
139 	ino_t ino;
140 	uint64_t file_offset;
141 	pthread_t ctor_visitor;
142 	char *rpath_orig, *rpath;
143 	struct tls_module tls;
144 	size_t tls_id;
145 	size_t relro_start, relro_end;
146 	uintptr_t *new_dtv;
147 	unsigned char *new_tls;
148 	struct td_index *td_index;
149 	struct dso *fini_next;
150 	char *shortname;
151 #if DL_FDPIC
152 	unsigned char *base;
153 #else
154 	struct fdpic_loadmap *loadmap;
155 #endif
156 	struct funcdesc {
157 		void *addr;
158 		size_t *got;
159 	} *funcdescs;
160 	size_t *got;
161 	struct dso **deps, *needed_by;
162 	/* only assigned when a thread local destructor is added */
163 	struct dso **deps_all;
164 	uint16_t ndeps_direct;
165 	uint16_t next_dep;
166 	uint16_t parents_count;
167 	uint16_t parents_capacity;
168 	struct dso **parents;
169 	struct dso **reloc_can_search_dso_list;
170 	uint16_t reloc_can_search_dso_count;
171 	uint16_t reloc_can_search_dso_capacity;
172 	/* mark the dso status */
173 	uint32_t flags;
174 	uint32_t nr_dlopen;
175 	bool is_global;
176 	bool is_preload;
177 	bool is_reloc_head_so_dep;
178 	char relocated;
179 	char constructed;
180 	char kernel_mapped;
181 	char mark;
182 	char bfs_built;
183 	char deps_all_built;
184 	char runtime_loaded;
185 	char by_dlopen;
186 	bool is_mapped_to_shadow;
187 	struct dso_debug_info *debug_info;
188 	/* do not add new elements after buf[]*/
189 	char buf[];
190 };
191 
192 struct dso_debug_info {
193 #if DL_FDPIC
194 	struct fdpic_loadmap *loadmap;
195 #else
196 	unsigned char *base;
197 #endif
198 	char *name;
199 	size_t *dynv;
200 	struct dso_debug_info *next, *prev;
201 };
202 
203 struct symdef {
204 	Sym *sym;
205 	struct dso *dso;
206 };
207 
208 struct dlopen_time_info {
209 	int entry_header_time;
210 	int deps_header_time;
211 	int map_so_time;
212 	int reloc_time;
213 	int map_cfi_time;
214 	int init_time;
215 	int total_time;
216 };
217 #endif
218 
219 struct notify_dso {
220 	struct dso **dso_list;
221 	size_t capacity;
222 	size_t length;
223 };
224 
225 struct fdpic_loadseg {
226 	uintptr_t addr, p_vaddr, p_memsz;
227 };
228 
229 struct fdpic_loadmap {
230 	unsigned short version, nsegs;
231 	struct fdpic_loadseg segs[];
232 };
233 
234 struct fdpic_dummy_loadmap {
235 	unsigned short version, nsegs;
236 	struct fdpic_loadseg segs[1];
237 };
238 
239 #include "reloc.h"
240 
241 #ifndef FDPIC_CONSTDISP_FLAG
242 #define FDPIC_CONSTDISP_FLAG 0
243 #endif
244 
245 #ifndef DL_FDPIC
246 #define DL_FDPIC 0
247 #endif
248 
249 #ifndef DL_NOMMU_SUPPORT
250 #define DL_NOMMU_SUPPORT 0
251 #endif
252 
253 #ifndef TLSDESC_BACKWARDS
254 #define TLSDESC_BACKWARDS 0
255 #endif
256 
257 #if !DL_FDPIC
258 #define IS_RELATIVE(x,s) ( \
259 	(R_TYPE(x) == REL_RELATIVE) || \
260 	(R_TYPE(x) == REL_SYM_OR_REL && !R_SYM(x)) )
261 #else
262 #define IS_RELATIVE(x,s) ( ( \
263 	(R_TYPE(x) == REL_FUNCDESC_VAL) || \
264 	(R_TYPE(x) == REL_SYMBOLIC) ) \
265 	&& (((s)[R_SYM(x)].st_info & 0xf) == STT_SECTION) )
266 #endif
267 
268 #ifndef NEED_MIPS_GOT_RELOCS
269 #define NEED_MIPS_GOT_RELOCS 0
270 #endif
271 
272 #ifndef DT_DEBUG_INDIRECT
273 #define DT_DEBUG_INDIRECT 0
274 #endif
275 
276 #ifndef DT_DEBUG_INDIRECT_REL
277 #define DT_DEBUG_INDIRECT_REL 0
278 #endif
279 
280 #define AUX_CNT 32
281 #ifndef __LITEOS__
282 #define DYN_CNT 37
283 
284 #define DT_ANDROID_REL (DT_LOOS + 2)
285 #define DT_ANDROID_RELSZ (DT_LOOS + 3)
286 
287 #define DT_ANDROID_RELA (DT_LOOS + 4)
288 #define DT_ANDROID_RELASZ (DT_LOOS + 5)
289 
290 #define DT_AARCH64_AUTH_RELRSZ 0x70000011
291 #define DT_AARCH64_AUTH_RELR 0x70000012
292 #define DT_AARCH64_AUTH_RELRENT 0x70000013
293 #define APIA 0
294 #define APIB 1
295 #define APDA 2
296 #define APDB 3
297 
298 #define ANDROID_REL_SIGN_SIZE 4
299 
300 #define RELOCATION_GROUPED_BY_INFO_FLAG 1
301 #define RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG 2
302 #define RELOCATION_GROUPED_BY_ADDEND_FLAG 4
303 #define RELOCATION_GROUP_HAS_ADDEND_FLAG 8
304 
305 #define CLOCK_NANO_TO_MILLI 1000000
306 #define CLOCK_SECOND_TO_MILLI 1000
307 #define DLOPEN_TIME_THRESHOLD 1000
308 
309 typedef void (*stage2_func)(unsigned char *, size_t *);
310 
311 #if DL_FDPIC
312 void *laddr(const struct dso *p, size_t v);
313 #endif
314 
315 #ifdef UNIT_TEST_STATIC
316     #define UT_STATIC
317 #else
318     #define UT_STATIC static
319 #endif
320 
321 void *addr2dso(size_t a);
322 UT_STATIC size_t count_syms(struct dso *p);
323 struct sym_info_pair gnu_hash(const char *s0);
324 struct symdef find_sym_impl(
325 	struct dso *dso, struct verinfo *verinfo, struct sym_info_pair s_info_p, int need_def, ns_t *ns);
326 
327 hidden void *__dlsym(void *restrict, const char *restrict, void *restrict);
328 hidden void *__dlvsym(void *restrict, const char *restrict, const char *restrict, void *restrict);
329 hidden int __dlclose(void *p);
330 #else
331 #define DYN_CNT 37
332 
333 typedef void (*stage2_func)(unsigned char *, size_t *);
334 
335 hidden void *__dlsym(void *restrict, const char *restrict, void *restrict);
336 #endif
337 hidden void __dl_seterr(const char *, ...);
338 hidden int __dl_invalid_handle(void *);
339 hidden void __dl_vseterr(const char *, va_list);
340 typedef void(*notify_call)(uintptr_t dso_base, size_t dso_len, const char *dso_path);
341 int register_ldso_func_for_add_dso(notify_call callback);
342 hidden ptrdiff_t __tlsdesc_static(), __tlsdesc_dynamic();
343 
344 hidden extern int __malloc_replaced;
345 hidden extern int __aligned_alloc_replaced;
346 hidden void __malloc_donate(char *, char *);
347 hidden int __malloc_allzerop(void *);
348 
349 #endif
350