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