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