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